递交一个bio的主要工作是从generic_make_request()函数开始的,我们以此为入口来分析一个bio的递交过程。在每个进程的task_struct中,都包含有两个变量----struct bio *bio_list, **bio_tail,generic_make_request()的主要工作就是用这两个变量来维护当前待添加的bio链表,实际的提交操作会由generic_make_request()调用__generic_make_request()函数完成。而在__generic_make_request()中,会调用到queue_list中定义的make_request_fn函数,也就是特定于设备的提交请求函数来完成后续的工作。在这里便会有一些问题,大部分设备的make_request_fn都可以直接定义为内核实现的__make_request函数,而一些设备需要使用自己的make_request_fn,而自行实现的make_request_fn有可能会递归调用gerneric_make_request(),由于内核的堆栈十分有限,因此在generic_make_request()的实现中,玩了一些小把戏,使得递归的深度不会超过一层。我们注意到bio_tail是一个二级指针,这个值最初是NULL,当有bio添加进来,bio_tail将会指向bio->bi_next(如果bio全都递交上去了,则bio_tail将会指向bio_list),也就是说除了第一次调用外,其他每次递归调用generic_make_request()函数都会出现bio_tail不为NULL的情形,因此当bio_tail不为NULL时,则只将bio添加到由bio_list和bio_tail维护的链表中,然后直接返回,而不调用__generic_make_request(),这样便防止了多重递归的产生
void generic_make_request(struct bio *bio)
{
if (current->bio_tail) {//current->bio_tail不为空则表明有bio正在提交,也就是说是处于递归调用
/* make_request is active */
bio->bi_next = NULL;
/*这里current->tail有两种情况,当current的bio链表为空时,bio_tail指向的是bio_list
当current的bio链表不为空时,bio_tail指向的是最后一个bio的bi_next指针,因此
这句的实际作用就是将bio添加到了current的bio链表的尾部*/
*(current->bio_tail) = bio;
current->bio_tail = &bio->bi_next;
/*这里直接返回,遍历并且提交bio的工作永远都是交给最先调用的generic_make_request来处理的,避免了多重递归*/
return;
}
/* following loop may be a bit non-obvious, and so deserves some
* explanation.
* Before entering the loop, bio->bi_next is NULL (as all callers
* ensure that) so we have a list with a single bio.
* We pretend that we have just taken it off a longer list, so
* we assign bio_list to the next (which is NULL) and bio_tail
* to &bio_list, thus initialising the bio_list of new bios to be
* added. __generic_make_request may indeed add some more bios
* through a recursive call to generic_make_request. If it
* did, we find a non-NULL value in bio_list and re-enter the loop
* from the top. In this case we really did just take the bio
* of the top of the list (no pretending) and so fixup bio_list and
* bio_tail or bi_next, and call into __generic_make_request again.
*
* The loop was structured like this to make only one call to
* __generic_make_request (which is important as it is large and
* inlined) and to keep the structure simple.
*/
BUG_ON(bio->bi_next);
do {
current->bio_list = bio->bi_next;//这里取current的待提交bio链表的下一个bio
if (bio->bi_next == NULL)//bi_next为空,也就是说待提交链表已经空了,只剩下最后一个bio了
current->bio_tail = ¤t->bio_list;//bio_tail指向bio_list
else
bio->bi_next = NULL;//否则将bio提取出来
__generic_make_request(bio);//提交bio
bio = current->bio_list;//取新的待提交bio
} while (bio);
current->bio_tail = NULL; /* deactivate */
}
__generic_make_request()首先由bio对应的block_device获取等待队列q,然后要检查对应的设备是不是分区,如果是分区的话要将扇区地址进行重新计算,最后调用make_request_fn完成bio的递交
static inline void __generic_make_request(struct bio *bio)
{
struct request_queue *q;
sector_t old_sector;
int ret, nr_sectors = bio_sectors(bio);//提取bio的大小,以扇区为单位
dev_t old_dev;
int err = -EIO;
might_sleep();
//这里检查bio的传输起始扇区是否超过设备的最大扇区,并且两者之间的差不能小于nr_sector
if (bio_check_eod(bio, nr_sectors))
goto end_io;
/*
* Resolve the mapping until finished. (drivers are
* still free to implement/resolve their own stacking
* by explicitly returning 0)
*
* NOTE: we don't repeat the blk_size check for each new device.
* Stacking drivers are expected to know what they are doing.
*/
old_sector = -1;
old_dev = 0;
do {
char b[BDEVNAME_SIZE];
q = bdev_get_queue(bio->bi_bdev);//获取对应设备的请求队列
if (unlikely(!q)) {
printk(KERN_ERR
"generic_make_request: Trying to access "
"nonexistent block-device %s (%Lu)\n",
bdevname(bio->bi_bdev, b),
(long long) bio->bi_sector);
goto end_io;
}
/*下面做一些必要的检查*/
if (unlikely(!bio_rw_flagged(bio, BIO_RW_DISCARD) &&
nr_sectors > queue_max_hw_sectors(q))) {
printk(KERN_ERR "bio too big device %s (%u > %u)\n",
bdevname(bio->bi_bdev, b),
bio_sectors(bio),
queue_max_hw_sectors(q));
goto end_io;
}
if (unlikely(test_bit(QUEUE_FLAG_DEAD, &q->queue_flags)))
goto end_io;
if (should_fail_request(bio))
goto end_io;
/*
* If this device has partitions, remap block n
* of partition p to block n+start(p) of the disk.
*/
//如果bio指定的是一个分区,则传输点要重新进行计算
blk_partition_remap(bio);
if (bio_integrity_enabled(bio) && bio_integrity_prep(bio))
goto end_io;
if (old_sector != -1)
trace_block_remap(q, bio, old_dev, old_sector);
old_sector = bio->bi_sector;
old_dev = bio->bi_bdev->bd_dev;
if (bio_check_eod(bio, nr_sectors))
goto end_io;
if (bio_rw_flagged(bio, BIO_RW_DISCARD) &&
!blk_queue_discard(q)) {
err = -EOPNOTSUPP;
goto end_io;
}
trace_block_bio_queue(q, bio);
ret = q->make_request_fn(q, bio);//这里是关键,调用请求队列中的make_request_fn函数处理请求
} while (ret);
return;
end_io:
bio_endio(bio, err);
}
辅助函数blk_partition_remap():
static inline void blk_partition_remap(struct bio *bio)
{
struct block_device *bdev = bio->bi_bdev;
/*首先要保证传输的大小不能小于1个扇区并且bdev确实是分区*/
if (bio_sectors(bio) && bdev != bdev->bd_contains) {
struct hd_struct *p = bdev->bd_part;//获取分区信息
bio->bi_sector += p->start_sect;//在传输起点的原基础上加上分区的起始扇区号
bio->bi_bdev = bdev->bd_contains;//将bio的bdev置为主设备
trace_block_remap(bdev_get_queue(bio->bi_bdev), bio,
bdev->bd_dev,
bio->bi_sector - p->start_sect);
}
}
