linux 块驱动程序,linux 块设备驱动程序2:详细分析

Linux操作系统有两类主要的设备文件:

1.字符设备:以字节为单位进行顺序I/O操作的设备,无需缓冲区且被直接读写。

2.块设备:只能以块单位接收输入返回,对于I/O请求有对应的缓冲区,可以随机访问,块设备的访问位置必须能够在介质的不同区间前后移动。在块设备中,最小的可寻址单元是扇区,扇区的大小一般是2的整数倍,常见的大小为512个字节。

0b81c504f5fe07467f1b4d3ee6a652c2.png

上图是一个块设备操作的分层实现图

1.

当一个进程被Read时,内核会通过VFS层去读取要读的文件块有没有被cache了,这个cache由一个buffer_head结构读取。如果要读取

的文件块还没有被cache,则就要从文件系统中去读取,通过一个address_space结构来引用,如果调用文件系统读取函数去读取一个扇区的数

据。当它从磁盘读出数据时,将数据页连入到cache中,当下一次在读取时,就不需要从磁盘去读取了。Read完后将请求初始化成一个bio结构,并提交

给通用块层。

2.它通过submit_bio()去完成,通用层在调用相应的设备IO调度器,这个调度器的调度算法,将这个bio合并到已经存在的request中,或者创建一个新的request,并将创建的插入到请求队列中,最后就剩下块设备驱动层来完成后面的所有工作。

内核中块得I/O操作的是由bio结构表示的

点击(此处)折叠或打开

struct bio {

sector_t        bi_sector;    /*该BIO结构所要传输的第一个(512字节)扇区*/

struct bio        *bi_next;    /*请求链表*/

struct block_device    *bi_bdev;/*相关的块设备*/

unsigned long        bi_flags;    /*状态和命令的标志*/

unsigned long        bi_rw;        /*读写*/

unsigned short        bi_vcnt;    /* bio_vec的偏移个数 */

unsigned short        bi_idx;        /* bvl_vec */

unsigned short        bi_phys_segments;

/* Number of segments after physical and DMA remapping

* hardware coalescing is performed.

*/

unsigned short        bi_hw_segments;

unsigned int        bi_size;    /* residual I/O count */

/*

* To keep track of the max hw size, we account for the

* sizes of the first and last virtually mergeable segments

* in this bio

*/

unsigned int        bi_hw_front_size;

unsigned int        bi_hw_back_size;

unsigned int        bi_max_vecs;    /* max bvl_vecs we can hold */

struct bio_vec        *bi_io_vec;    /* the actual vec list */

bio_end_io_t        *bi_end_io;

atomic_t        bi_cnt;        /* pin count */

void            *bi_private;

bio_destructor_t    *bi_destructor;    /* destructor */

};

此结构体的目的主要是正在执行的I/O操作,其中的bi_io_vecs、bi_vcnt、bi_idx三者都可以相互找到。bio_vec描述一个特定的片段,片段所在的物理页,块在物理页中的偏移页,整个bio_io_vec结构表示一个完整的缓冲区。

点击(此处)折叠或打开

struct bio_vec {

struct page    *bv_page;

unsigned int    bv_len;

unsigned int    bv_offset;

};

当一个块被调用内存时,要储存在一个缓冲区,每个缓冲区与一个块对应,所以每一个缓冲区独有一个对应的描述符,该描述符用buffer_head结构表示

点击(此处)折叠或打开

struct buffer_head {

unsigned long b_state;        /* buffer state bitmap (see above) */

struct buffer_head *b_this_page;/* circular list of page's buffers */

struct page *b_page;        /* the page this bh is mapped to */

sector_t b_blocknr;        /* start block number */

size_t b_size;            /* size of mapping */

char *b_data;            /* pointer to data within the page */

struct block_device *b_bdev;

bh_end_io_t *b_end_io;        /* I/O completion */

void *b_private;        /* reserved for b_end_io */

struct list_head b_assoc_buffers; /* associated with another mapping */

struct address_space *b_assoc_map;    /* mapping this buffer is

associated with */

atomic_t b_count;        /* users using this buffer_head */

};

下面来看看块设备的核心ll_rw_block函数

点击(此处)折叠或打开

void ll_rw_block(int rw, int nr, struct buffer_head *bhs[])

{

int i;

for (i = 0; i < nr; i) {

struct buffer_head *bh = bhs[i];

if (!trylock_buffer(bh))

continue;

if (rw == WRITE) {

if (test_clear_buffer_dirty(bh)) {

bh->b_end_io = end_buffer_write_sync;

get_bh(bh);

submit_bh(WRITE, bh);

continue;

}

} else {

if (!buffer_uptodate(bh)) {

bh->b_end_io = end_buffer_read_sync;

get_bh(bh);

submit_bh(rw, bh);

continue;

}

}

unlock_buffer(bh);

}

}

请求块设备驱动将多个物理块读出或者写到块设备,块设备的读写都是在块缓冲区中进行。

点击(此处)折叠或打开

int submit_bh(int rw, struct buffer_head * bh)

{

struct bio *bio;

int ret = 0;

BUG_ON(!buffer_locked(bh));

BUG_ON(!buffer_mapped(bh));

BUG_ON(!bh->b_end_io);

BUG_ON(buffer_delay(bh));

BUG_ON(buffer_unwritten(bh));

/*

* Only clear out a write error when rewriting

*/

if (test_set_buffer_req(bh) && (rw & WRITE))

clear_buffer_write_io_error(bh);

/*

* from here on down, it's all bio -- do the initial mapping,

* submit_bio -> generic_make_request may further map this bio around

*/

bio = bio_alloc(GFP_NOIO, 1);

bio->bi_sector = bh->b_blocknr * (bh->b_size >> 9);

bio->bi_bdev = bh->b_bdev;

bio->bi_io_vec[0].bv_page = bh->b_page;

bio->bi_io_vec[0].bv_len = bh->b_size;

bio->bi_io_vec[0].bv_offset = bh_offset(bh);

bio->bi_vcnt = 1;

bio->bi_idx = 0;

bio->bi_size = bh->b_size;

bio->bi_end_io = end_bio_bh_io_sync;

bio->bi_private = bh;

bio_get(bio);

submit_bio(rw, bio);

if (bio_flagged(bio, BIO_EOPNOTSUPP))

ret = -EOPNOTSUPP;

bio_put(bio);

return ret;

这个函数主要是调用submit_bio,最终调用generic_make_request去完成将bio传递给驱动去处理。

点击(此处)折叠或打开

void generic_make_request(struct bio *bio)

{

struct bio_list bio_list_on_stack;

if (!generic_make_request_checks(bio))

return;

if (current->bio_list) {

bio_list_add(current->bio_list, bio);

return;

}

BUG_ON(bio->bi_next);

bio_list_init(&bio_list_on_stack);

current->bio_list = &bio_list_on_stack;

do {

struct request_queue *q = bdev_get_queue(bio->bi_bdev);

q->make_request_fn(q, bio);

bio = bio_list_pop(current->bio_list);

} while (bio);

current->bio_list = NULL; /* deactivate */

}

这个函数主要是取出块设备相应的队列中的每个设备,在调用块设备驱动的make_request,如果没有指定make_request就调用内核默认的__make_request,这个函数主要作用就是调用I/O调度算法将bio合并,或插入到队列中合适的位置中去。

整个流程为:

0b6d9cd24ab9f0d1a2ff2bedcbc97b52.png

那么request_fn指向那个函数呢?在内核中搜搜request_fn,发现

点击(此处)折叠或打开

request_queue_t *blk_init_queue(request_fn_proc *rfn, spinlock_t *lock)

{

return blk_init_queue_node(rfn, lock, -1);

}

EXPORT_SYMBOL(blk_init_queue);

request_queue_t *

blk_init_queue_node(request_fn_proc *rfn, spinlock_t *lock, int node_id)

{

request_queue_t *q = blk_alloc_queue_node(GFP_KERNEL, node_id);

if (!q)

return NULL;

q->node = node_id;

if (blk_init_free_list(q)) {

kmem_cache_free(requestq_cachep, q);

return NULL;

}

if (!lock) {

spin_lock_init(&q->__queue_lock);

lock = &q->__queue_lock;

}

q->request_fn        = rfn;

q->prep_rq_fn        = NULL;

q->unplug_fn        = generic_unplug_device;

q->queue_flags        = (1 << QUEUE_FLAG_CLUSTER);

q->queue_lock        = lock;

blk_queue_segment_boundary(q, 0xffffffff);

blk_queue_make_request(q, __make_request);

blk_queue_max_segment_size(q, MAX_SEGMENT_SIZE);

blk_queue_max_hw_segments(q, MAX_HW_SEGMENTS);

blk_queue_max_phys_segments(q, MAX_PHYS_SEGMENTS);

q->sg_reserved_size = INT_MAX;

/*

* all done

*/

if (!elevator_init(q, NULL)) {

blk_queue_congestion_threshold(q);

return q;

}

blk_put_queue(q);

return NULL;

}

原来request_queue的make_request_fn指向__make_request()函数,这个函数复杂I/O调度,并对bio做些合并等。下面来看看__make_request()做了些什么?

a164f45b7db7f93546c34e718a4776ef.png

由上面可以分析得出,其中有一个很重要的结构体

点击(此处)折叠或打开

struct request {

struct list_head queuelist;//连接这个请求到请求队列.

//追踪请求硬件完成的扇区的成员. 第一个尚未被传送的扇区被存储到 hard_sector, 已经传送的扇区总数在 ha//rd_nr_sectors, 并且在当前 bio 中剩余的扇区数是 hard_cur_sectors. 这些成员打算只用在块子系统; 驱动//不应当使用它们.

struct request_queue *q;

sector_t hard_sector;

unsigned long hard_nr_sectors;

unsigned int hard_cur_sectors;

struct bio *bio;//bio 是给这个请求的 bio 结构的链表. 你不应当直接存取这个成员; 使用 rq_for_each_bio(后面描述) 代替.

unsigned short nr_phys_segments;//被这个请求在物理内存中占用的独特段的数目, 在邻近页已被合并后

char *buffer;//随着深入理解,可见到这个成员仅仅是在当前 bio 上调用 bio_data 的结果.

};

request_queue只是一个请求队列,通过可以找到requeue,然后通过bio结构体对应的page读取物理内存中的信息。

下面看看内核使用的块设备的例子

点击(此处)折叠或打开

static int jsfd_init(void)

{

static DEFINE_SPINLOCK(lock);

struct jsflash *jsf;

struct jsfd_part *jdp;

int err;

int i;

if (jsf0.base == 0)

return -ENXIO;

err = -ENOMEM;

//1. 分配gendisk: alloc_disk

for (i = 0; i < JSF_MAX; i++) {

struct gendisk *disk = alloc_disk(1);

if (!disk)

goto out;

jsfd_disk[i] = disk;

}

//2.设置,分配/设置队列: request_queue_t  // 它提供读写能力

if (register_blkdev(JSFD_MAJOR, "jsfd")) {

err = -EIO;

goto out;

}

jsf_queue = blk_init_queue(jsfd_do_request, &lock);

if (!jsf_queue) {

err = -ENOMEM;

unregister_blkdev(JSFD_MAJOR, "jsfd");

goto out;

}

//3.设置gendisk其他信息             // 它提供属性: 比如容量

for (i = 0; i < JSF_MAX; i++) {

struct gendisk *disk = jsfd_disk[i];

if ((i & JSF_PART_MASK) >= JSF_NPART) continue;

jsf = &jsf0;    /* actually, &jsfv[i >> JSF_PART_BITS] */

jdp = &jsf->dv[i&JSF_PART_MASK];

disk->major = JSFD_MAJOR;

disk->first_minor = i;

sprintf(disk->disk_name, "jsfd%d", i);

disk->fops = &jsfd_fops;

set_capacity(disk, jdp->dsize >> 9);

disk->private_data = jdp;

disk->queue = jsf_queue;

//4 注册: add_disk

add_disk(disk);

set_disk_ro(disk, 1);

}

return 0;

out:

while (i--)

put_disk(jsfd_disk[i]);

return err;

}

转载博客:

http://blog.chinaunix.net/uid-27664726-id-3349507.html

相关知识博客:

http://blog.csdn.net/jianchi88/article/details/7212370

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值