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.

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