block: Find bio sector offset given idx and offset Helper function to find the sector offset in a bio given bvec index and page offset. Signed-off-by: Martin K. Petersen --- diff --git a/fs/bio.c b/fs/bio.c --- a/fs/bio.c +++ b/fs/bio.c @@ -1274,6 +1274,40 @@ struct bio_pair *bio_split(struct bio *b return bp; } +/** + * bio_sector_offset - Find hardware sector offset in bio + * @bio: bio to inspect + * @index: bio_vec index + * @offset: offset in bv_page + * + * Return the number of hardware sectors between beginning of bio + * and an end point indicated by a bio_vec index and an offset + * within that vector's page. + */ +sector_t bio_sector_offset(struct bio *bio, unsigned short index, unsigned int offset) +{ + struct bio_vec *bv; + unsigned int sector_sz = bio->bi_bdev->bd_disk->queue->hardsect_size; + sector_t sectors; + int i; + + sectors = 0; + + BUG_ON(index >= bio->bi_vcnt); + + bio_for_each_segment(bv, bio, i) { + if (i == index) { + if (offset > bv->bv_offset) + sectors += (offset - bv->bv_offset) / sector_sz; + return sectors; + } + + sectors += bv->bv_len / sector_sz; + } + + BUG(); +} +EXPORT_SYMBOL(bio_sector_offset); /* * create memory pools for biovec's in a bio_set. diff --git a/include/linux/bio.h b/include/linux/bio.h --- a/include/linux/bio.h +++ b/include/linux/bio.h @@ -347,6 +347,8 @@ extern int bio_add_pc_page(struct reques extern int bio_add_pc_page(struct request_queue *, struct bio *, struct page *, unsigned int, unsigned int); extern int bio_get_nr_vecs(struct block_device *); +extern sector_t bio_sector_offset(struct bio *, unsigned short, unsigned int); + extern struct bio *bio_map_user(struct request_queue *, struct block_device *, unsigned long, unsigned int, int); struct sg_iovec;