鍧楄澶囬┍鍔ㄧ▼搴忕殑妗嗘灦

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了鍧楄澶囬┍鍔ㄧ▼搴忕殑妗嗘灦相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/mnt' title='mnt'>mnt   head   椹卞姩   for   from   娴嬭瘯鏂规硶   isp   鍏朵粬   mod   

鍧楄澶囧拰鍓嶉潰鐨勫瓧绗﹁澶囬┍鍔ㄤ富浣撴鏋跺拰缂栫▼鎬濇兂涓婂熀鏈浉鍚岋紝浣嗘槸鍦ㄦ搷浣滅殑鏃跺€欏氨涓嶄竴鏍蜂簡銆?/span>

妗嗘灦锛?/span>

app: open,read,write "1.txt"
--------------------------------------------- 鏂囦欢鐨勮鍐?/span>
鏂囦欢绯荤粺: vfat, ext2, ext3, yaffs2, jffs2 (鎶婃枃浠剁殑璇诲啓杞崲涓烘墖鍖虹殑璇诲啓)
-----------------ll_rw_block----------------- 鎵囧尯鐨勮鍐?/span>
1. 鎶?璇诲啓"鏀惧叆闃熷垪
2. 璋冪敤闃熷垪鐨勫鐞嗗嚱鏁?浼樺寲/璋冮『搴?鍚堝苟)
鍧楄澶囬┍鍔ㄧ▼搴?
---------------------------------------------
纭欢: 纭洏,flash

 

鍒嗘瀽ll_rw_block
for (i = 0; i < nr; i++) {
銆€銆€struct buffer_head *bh = bhs[i];
銆€銆€submit_bh(rw, bh);
銆€銆€銆€銆€struct bio *bio; // 浣跨敤bh鏉ユ瀯閫燽io (block input/output)
銆€銆€銆€銆€submit_bio(rw, bio);
銆€銆€銆€銆€銆€銆€// 閫氱敤鐨勬瀯閫犺姹? 浣跨敤bio鏉ユ瀯閫犺姹?request)
銆€銆€銆€銆€銆€銆€generic_make_request(bio);
銆€銆€銆€銆€銆€銆€__generic_make_request(bio);
銆€銆€銆€銆€銆€銆€銆€銆€request_queue_t *q = bdev_get_queue(bio->bi_bdev); // 鎵惧埌闃熷垪

銆€銆€銆€銆€銆€銆€// 璋冪敤闃熷垪鐨?鏋勯€犺姹傚嚱鏁?
銆€銆€銆€銆€銆€銆€ret = q->make_request_fn(q, bio);
銆€銆€銆€銆€銆€銆€銆€銆€// 榛樿鐨勫嚱鏁版槸__make_request
銆€銆€銆€銆€銆€銆€銆€銆€__make_request
銆€銆€銆€銆€銆€銆€銆€銆€銆€銆€// 鍏堝皾璇曞悎骞?/span>
銆€銆€銆€銆€銆€銆€銆€銆€銆€銆€elv_merge(q, &req, bio);

銆€銆€銆€銆€銆€銆€銆€銆€銆€銆€// 濡傛灉鍚堝苟涓嶆垚锛屼娇鐢╞io鏋勯€犺姹?/span>
銆€銆€銆€銆€銆€銆€銆€銆€銆€銆€init_request_from_bio(req, bio);

銆€銆€銆€銆€銆€銆€銆€銆€銆€銆€// 鎶婅姹傛斁鍏ラ槦鍒?/span>
銆€銆€銆€銆€銆€銆€銆€銆€銆€銆€add_request(q, req);

銆€銆€銆€銆€銆€銆€銆€銆€銆€銆€// 鎵ц闃熷垪
銆€銆€銆€銆€銆€銆€銆€銆€銆€銆€__generic_unplug_device(q);
銆€銆€銆€銆€銆€銆€銆€銆€銆€銆€// 璋冪敤闃熷垪鐨?澶勭悊鍑芥暟"
銆€銆€銆€銆€銆€銆€銆€銆€銆€銆€q->request_fn(q);

鎬庝箞鍐欏潡璁惧椹卞姩绋嬪簭鍛紵
1. 鍒嗛厤gendisk: alloc_disk
2. 璁剧疆
2.1 鍒嗛厤/璁剧疆闃熷垪: request_queue_t // 瀹冩彁渚涜鍐欒兘鍔?/span>
blk_init_queue
2.2 璁剧疆gendisk鍏朵粬淇℃伅 // 瀹冩彁渚涘睘鎬? 姣斿瀹归噺
3. 娉ㄥ唽: add_disk

鍙傝€冿細
driverslockxd.c
driverslockz2ram.c

鎶€鏈浘鐗? id=
/* 鍙傝€?
 * driverslockxd.c
 * driverslockz2ram.c
 */

#include <linux/module.h>
#include <linux/errno.h>
#include <linux/interrupt.h>
#include <linux/mm.h>
#include <linux/fs.h>
#include <linux/kernel.h>
#include <linux/timer.h>
#include <linux/genhd.h>
#include <linux/hdreg.h>
#include <linux/ioport.h>
#include <linux/init.h>
#include <linux/wait.h>
#include <linux/blkdev.h>
#include <linux/blkpg.h>
#include <linux/delay.h>
#include <linux/io.h>

#include <asm/system.h>
#include <asm/uaccess.h>
#include <asm/dma.h>

static struct gendisk *ramblock_disk;
static request_queue_t *ramblock_queue;

static int major;

static DEFINE_SPINLOCK(ramblock_lock);

#define RAMBLOCK_SIZE (1024*1024)
static unsigned char *ramblock_buf;

static int ramblock_getgeo(struct block_device *bdev, struct hd_geometry *geo)
{
    /* 瀹归噺=heads*cylinders*sectors*512 */
    geo->heads     = 2;
    geo->cylinders = 32;
    geo->sectors   = RAMBLOCK_SIZE/2/32/512;
    return 0;
}


static struct block_device_operations ramblock_fops = {
    .owner    = THIS_MODULE,
    .getgeo    = ramblock_getgeo,
};

static void do_ramblock_request(request_queue_t * q)
{
    static int r_cnt = 0;
    static int w_cnt = 0;
    struct request *req;
    
    //printk("do_ramblock_request %d
", ++cnt);

    while ((req = elv_next_request(q)) != NULL) {
        /* 鏁版嵁浼犺緭涓夎绱? 婧?鐩殑,闀垮害 */
        /* 婧?鐩殑: */
        unsigned long offset = req->sector * 512;

        /* 鐩殑/婧? */
        // req->buffer

        /* 闀垮害: */        
        unsigned long len = req->current_nr_sectors * 512;

        if (rq_data_dir(req) == READ)
        {
            //printk("do_ramblock_request read %d
", ++r_cnt);
            memcpy(req->buffer, ramblock_buf+offset, len);
        }
        else
        {
            //printk("do_ramblock_request write %d
", ++w_cnt);
            memcpy(ramblock_buf+offset, req->buffer, len);
        }        
        
        end_request(req, 1);
    }
}

static int ramblock_init(void)
{
    /* 1. 鍒嗛厤涓€涓猤endisk缁撴瀯浣?*/
    ramblock_disk = alloc_disk(16); /* 娆¤澶囧彿涓暟: 鍒嗗尯涓暟+1 */

    /* 2. 璁剧疆 */
    /* 2.1 鍒嗛厤/璁剧疆闃熷垪: 鎻愪緵璇诲啓鑳藉姏 */
    ramblock_queue = blk_init_queue(do_ramblock_request, &ramblock_lock);
    ramblock_disk->queue = ramblock_queue;
    
    /* 2.2 璁剧疆鍏朵粬灞炴€? 姣斿瀹归噺 */
    major = register_blkdev(0, "ramblock");  /* cat /proc/devices */    
    ramblock_disk->major       = major;
    ramblock_disk->first_minor = 0;
    sprintf(ramblock_disk->disk_name, "ramblock");
    ramblock_disk->fops        = &ramblock_fops;
    set_capacity(ramblock_disk, RAMBLOCK_SIZE / 512);

    /* 3. 纭欢鐩稿叧鎿嶄綔 */
    ramblock_buf = kzalloc(RAMBLOCK_SIZE, GFP_KERNEL);

    /* 4. 娉ㄥ唽 */
    add_disk(ramblock_disk);

    return 0;
}

static void ramblock_exit(void)
{
    unregister_blkdev(major, "ramblock");
    del_gendisk(ramblock_disk);
    put_disk(ramblock_disk);
    blk_cleanup_queue(ramblock_queue);

    kfree(ramblock_buf);
}

module_init(ramblock_init);
module_exit(ramblock_exit);

MODULE_LICENSE("GPL");
View Code

娴嬭瘯鏂规硶锛?/span>

鍦ㄥ紑鍙戞澘涓?
1. insmod ramblock.ko
2. 鏍煎紡鍖? mkdosfs /dev/ramblock
3. 鎸傛帴: mount /dev/ramblock /tmp/
4. 璇诲啓鏂囦欢: cd /tmp, 鍦ㄩ噷闈i鏂囦欢
5. cd /; umount /tmp/
6. cat /dev/ramblock > /mnt/ramblock.bin
7. 鍦≒C涓婃煡鐪媟amblock.bin
sudo mount -o loop ramblock.bin /mnt

以上是关于鍧楄澶囬┍鍔ㄧ▼搴忕殑妗嗘灦的主要内容,如果未能解决你的问题,请参考以下文章

鍐呮牳妯″潡鍙傛暟璁捐

hdu-4738.Caocao's Bridges(鍥句腑鏉冨€兼渶灏忕殑妗?

Linux鏌ョ湅鍜屽叧闂悗鍙拌繍琛岀▼搴忕殑鏂规硶

MyBatis妗嗘灦鍏ラ棬

绉诲姩绔垨鑰呯Щ鍔ㄧh5闇€瑕佹敞鎰忕殑涓€浜涗簨