block: Find bio sector offset given idx and offset
authorMartin K. Petersen <martin.petersen@oracle.com>
Thu, 2 Oct 2008 02:42:53 +0000 (22:42 -0400)
committerJens Axboe <jens.axboe@oracle.com>
Thu, 9 Oct 2008 06:56:22 +0000 (08:56 +0200)
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>
Signed-off-by: Jens Axboe <jens.axboe@oracle.com>
fs/bio.c
include/linux/bio.h

index e56e7685af9cf352b086580cfe6886c64895d0fa..a5af5809f566b6c3d3ff88e43d22deeaafa38a2a 100644 (file)
--- 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.
index d86d39d490e68d789c97d74fef03b8fffba33aff..fe12d0f9ebaa4b5d606f3ff6a299866804401728 100644 (file)
@@ -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;
This page took 0.031116 seconds and 5 git commands to generate.