鍧楄澶囬┍鍔ㄧ▼搴忕殑妗嗘灦
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
/* 鍙傝€? * 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");
娴嬭瘯鏂规硶锛?/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
以上是关于鍧楄澶囬┍鍔ㄧ▼搴忕殑妗嗘灦的主要内容,如果未能解决你的问题,请参考以下文章