块设备驱动程序

来源:互联网 发布:淘宝买家的信用等级 编辑:程序博客网 时间:2024/05/21 22:48
框架:


app:      open,read,write "1.txt"
---------------------------------------------  文件的读写
文件系统: vfat, ext2, ext3, yaffs2, jffs2      (把文件的读写转换为扇区的读写)
-----------------ll_rw_block-----------------  扇区的读写
                       1. 把"读写"放入队列
                       2. 调用队列的处理函数(优化/调顺序/合并)
            块设备驱动程序     
---------------------------------------------
硬件:        硬盘,flash




<LINUX内核源代码情景分析>


分析ll_rw_block
        for (i = 0; i < nr; i++) {
            struct buffer_head *bh = bhs[i];
            submit_bh(rw, bh);
                struct bio *bio; // 使用bh来构造bio (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_fn  找到 q->make_request_fn = mfn;
                                    // 默认的函数是__make_request
                                    __make_request
                                        // 先尝试合并  ;elevator :电梯
                                        elv_merge(q, &req, bio); 
                                        
                                        // 如果合并不成,使用bio构造请求
                                        init_request_from_bio(req, bio);
                                        
                                        // 把请求放入队列
                                        add_request(q, req);
                                        
                                        // 执行队列
                                        __generic_unplug_device(q);
                                                // 调用队列的"处理函数"
                                                q->request_fn(q);
            
怎么写块设备驱动程序呢?
1. 分配gendisk: alloc_disk
2. 设置
2.1 分配/设置队列: request_queue_t  // 它提供读写能力
    blk_init_queue
2.2 设置gendisk其他信息             // 它提供属性: 比如容量
3. 注册: add_disk


参考:
drivers\block\xd.c
drivers\block\z2ram.c


(分配一块内存,在这个内存里模拟硬盘)
测试3th,4th:
在开发板上:
1. insmod ramblock.ko
2. 格式化: mkdosfs /dev/ramblock
3. 挂接: mount /dev/ramblock /tmp/
4. 读写文件: cd /tmp, 在里面vi文件
5. cd /; umount /tmp/
6. cat /dev/ramblock > /mnt/ramblock.bin     (把 /dev/ramblock拷贝到 /mnt/ramblock.bin文件里)
7. 在PC上查看ramblock.bin
   sudo mount -o loop ramblock.bin /mnt      ( -o  loop 可以把普通文件当做块设备挂接)


测试5th:
1. insmod ramblock.ko
2. ls /dev/ramblock*

3. fdisk /dev/ramblock               (fdisk 对硬盘进行分区)


# fdisk /dev/ramblock             (分区)
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel. Changes will remain in memory only,
until you decide to write them. After that the previous content
won't be recoverable.


Warning: invalid flag 0x00,0x00 of partition table 4 will be corrected by w(rite)


Command (m for help): m
Command Action
a       toggle a bootable flag
b       edit bsd disklabel
c       toggle the dos compatibility flag
d       delete a partition
l       list known partition types
n       add a new partition
o       create a new empty DOS partition table
p       print the partition table
q       quit without saving changes
s       create a new empty Sun disklabel
t       change a partition's system id
u       change display/entry units
v       verify the partition table
w       write table to disk and exit            (分区后,最后一步要把分区表写进磁盘,并退出。  也就是保存分区)
x       extra functionality (experts only)


Command (m for help): n
Command action
   e   extended (扩展分区)
   p   primary partition (1-4)              (主分区)
p
Partition number (1-4): 1                 //第一个主分区
First cylinder (1-32, default 1): 1      //第一个柱面
Last cylinder or +size or +sizeM or +sizeK (6-32, default 32): 10          //第10个柱面

-----------------------------------------------------------------------------------------------------------------------------

//用内存来模拟块设备驱动程序

/* 参考:
 * drivers\block\xd.c
 * drivers\block\z2ram.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\n", ++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)
{//从磁盘里读数据,读到req->buffer里
//printk("do_ramblock_request read %d\n", ++r_cnt);
memcpy(req->buffer, ramblock_buf+offset, len);
}//ramblock_buf+offset  起始地址加上偏移值
else
{//向磁盘里写数据
//printk("do_ramblock_request write %d\n", ++w_cnt);
memcpy(ramblock_buf+offset, req->buffer, len);
}

end_request(req, 1);
}
}


static int ramblock_init(void)
{
/* 1. 分配一个gendisk结构体 */
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");


0 0
原创粉丝点击