Re: [PATCH 5/7] block: Find bio sector offset given idx and offset

Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
From: Martin K. Petersen
Date: Wednesday, October 1, 2008 - 7:42 pm

>>>>> "Jens" == Jens Axboe <jens.axboe@oracle.com> writes:

Jens> Too may BUG's there, me thinks, and the interface looks
Jens> fragile. What if the bio is alreday done (bi_idx == bi_vcnt)?

Good point!  Obviously I've only used the function for bio splitting
and not at completion time.  Updated patch below.


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 <martin.petersen@oracle.com>
---
 fs/bio.c            |   36 ++++++++++++++++++++++++++++++++++++
 include/linux/bio.h |    1 +
 2 files changed, 37 insertions(+)

diff --git a/fs/bio.c b/fs/bio.c
index e56e768..a5af580 100644
--- a/fs/bio.c
+++ b/fs/bio.c
@@ -1300,6 +1300,42 @@ struct bio_pair *bio_split(struct bio *bi, mempool_t *pool, int first_sectors)
 	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)
+{
+	unsigned int sector_sz = queue_hardsect_size(bio->bi_bdev->bd_disk->queue);
+	struct bio_vec *bv;
+	sector_t sectors;
+	int i;
+
+	sectors = 0;
+
+	if (index >= bio->bi_idx)
+		index = bio->bi_vcnt - 1;
+
+	__bio_for_each_segment(bv, bio, i, 0) {
+		if (i == index) {
+			if (offset > bv->bv_offset)
+				sectors += (offset - bv->bv_offset) / sector_sz;
+			break;
+		}
+
+		sectors += bv->bv_len / sector_sz;
+	}
+
+	return sectors;
+}
+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
index 6aba97d..386a1df 100644
--- a/include/linux/bio.h
+++ b/include/linux/bio.h
@@ -327,6 +327,7 @@ extern int bio_add_page(struct bio *, struct page *, unsigned int,unsigned int);
 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, gfp_t);
 struct sg_iovec;
--
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]

Messages in current thread:
Block integrity patches for 2.6.28, Martin K. Petersen, (Wed Oct 1, 12:38 am)
[PATCH 1/7] block: Introduce integrity data ownership flag, Martin K. Petersen, (Wed Oct 1, 12:38 am)
[PATCH 2/7] block: Fix double put in blk_integrity_unregister, Martin K. Petersen, (Wed Oct 1, 12:38 am)
[PATCH 3/7] block: Switch blk_integrity_compare from bdev ..., Martin K. Petersen, (Wed Oct 1, 12:38 am)
[PATCH 4/7] block: gendisk integrity wrapper, Martin K. Petersen, (Wed Oct 1, 12:38 am)
[PATCH 5/7] block: Find bio sector offset given idx and offset, Martin K. Petersen, (Wed Oct 1, 12:38 am)
[PATCH 6/7] dm: Add support for data integrity to DM, Martin K. Petersen, (Wed Oct 1, 12:38 am)
[PATCH 7/7] md: Add support for data integrity to MD, Martin K. Petersen, (Wed Oct 1, 12:38 am)
Re: [PATCH 5/7] block: Find bio sector offset given idx an ..., Martin K. Petersen, (Wed Oct 1, 7:42 pm)
Re: Block integrity patches for 2.6.28, Jens Axboe, (Thu Oct 2, 3:56 am)
Re: Block integrity patches for 2.6.28, Martin K. Petersen, (Thu Oct 2, 6:54 am)
Re: Block integrity patches for 2.6.28, Jens Axboe, (Thu Oct 2, 9:29 am)