bio中bi_sector扇区的转换

在"块存储:AIO的直接读流程注释"文中,描述了调用块设备__blkdev_direct_IO发起IO直读,在提交IO(io_submit)之前设置了bio的起始扇区,但这个起始扇区只是相对于块设备某个分区的偏移,不是相对于整个块设备或磁盘的偏移,还不能发给设备驱动执行:
static ssize_t  __blkdev_direct_IO(struct kiocb *iocb, struct iov_iter *iter, int nr_pages)
{
        struct file *file = iocb->ki_filp;
        struct inode *inode = bdev_file_inode(file);
        struct block_device *bdev = I_BDEV(inode);
                   loff_t pos = iocb->ki_pos; //读操作所读的设备分区相对于设备分区起始位置的偏移
                   ...
                   bio_set_dev(bio, bdev);//设置bio参数:bd_partno,bd_disk
                   bio->bi_iter.bi_sector = pos >> 9; //相对于块设备分区(分区号bd_partno)偏移转化为扇区, 作为本次bio所要操作的磁盘的起始扇区,长度是bio->bi_iter.bi_size
                   ...
}
                 
如上述代码, 在__blkdev_direct_IO函数中为bio设置了其所描述的扇区所在的设备分区编号bd_partno:          
#define bio_set_dev(bio, bdev)                  \
do {                                            \
        (bio)->bi_disk = (bdev)->bd_disk;       \
        (bio)->bi_partno = (bdev)->bd_partno;   \
} while (0)

在将bio信息提交给设备驱动执行之前,要在通用块层将bio中bi_sector转化为相对于整个块设备(磁盘)的绝对扇区偏移,blk_partition_remap就是用于完成此工作,blk_partition_remap在通用块层被调用,该函数用于块设备分区信息转换,如将相对于一个分区的扇区偏移转换为相对于整个块设备(磁盘)的绝对扇区偏移:
generic_make_request :
                   > generic_make_request_checks: blk_partition_remap
                   > q->make_request_fn(q, bio);
 
Partition re-mapping handled by the generic block layer 
In 2.5 some of the gendisk/partition related code has been reorganized. Now the generic block layer performs partition-remapping early and thus provides drivers with a sector number relative to whole device, rather than
having to take partition number into account in order to arrive at the true sector number. The routine blk_partition_remap() is invoked by generic_make_request even before invoking the queue specific make_request_fn,
so the i/o scheduler also gets to operate on whole disk sector numbers. This should typically not require changes to block drivers, it just never gets to invoke its own partition sector offset calculations since all bios sent are offset from the beginning of the device.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章