public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Jens Axboe <jens.axboe@oracle.com>
To: "Martin K. Petersen" <martin.petersen@oracle.com>
Cc: neilb@suse.de, agk@sourceware.org, linux-kernel@vger.kernel.org,
	dm-devel@redhat.com
Subject: Re: [PATCH 5/7] block: Find bio sector offset given idx and offset
Date: Wed, 1 Oct 2008 10:10:56 +0200	[thread overview]
Message-ID: <20081001081056.GB19428@kernel.dk> (raw)
In-Reply-To: <1222846723-18213-6-git-send-email-martin.petersen@oracle.com>

On Wed, Oct 01 2008, Martin K. Petersen wrote:
> 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            |   34 ++++++++++++++++++++++++++++++++++
>  include/linux/bio.h |    1 +
>  2 files changed, 35 insertions(+), 0 deletions(-)
> 
> diff --git a/fs/bio.c b/fs/bio.c
> index e56e768..55ee2fd 100644
> --- a/fs/bio.c
> +++ b/fs/bio.c
> @@ -1300,6 +1300,40 @@ 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)
> +{
> +	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();
> +}

Too may BUG's there, me thinks, and the interface looks fragile. What if
the bio is alreday done (bi_idx == bi_vcnt)? I'd prefer something ala:

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;

        /* total start of bio current start? */
#if 0
        if (index >= bio->bi_vcnt)
                index = bio->bi_vcnt - 1;

        __bio_for_each_segment(bv, bio, i, 0) {
#else
        if (index > bio->bi_idx)
                return 0;

	bio_for_each_segment(bv, bio, i) {
#endif
		if (i == index) {
			if (offset > bv->bv_offset)
				sectors += (offset - bv->bv_offset) / sector_sz;
                        break;
		}

		sectors += bv->bv_len / sector_sz;
	}

        return sectors;
}

Depending on which interface you want, pick one of the if/else parts :-)

-- 
Jens Axboe


  reply	other threads:[~2008-10-01  8:11 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-10-01  7:38 Block integrity patches for 2.6.28 Martin K. Petersen
2008-10-01  7:38 ` [PATCH 1/7] block: Introduce integrity data ownership flag Martin K. Petersen
2008-10-01  7:38 ` [PATCH 2/7] block: Fix double put in blk_integrity_unregister Martin K. Petersen
2008-10-01  7:38 ` [PATCH 3/7] block: Switch blk_integrity_compare from bdev to gendisk Martin K. Petersen
2008-10-01  7:38 ` [PATCH 4/7] block: gendisk integrity wrapper Martin K. Petersen
2008-10-01  7:38 ` [PATCH 5/7] block: Find bio sector offset given idx and offset Martin K. Petersen
2008-10-01  8:10   ` Jens Axboe [this message]
2008-10-02  2:42     ` Martin K. Petersen
2008-10-02 17:07       ` Jens Axboe
2008-10-01  7:38 ` [PATCH 6/7] dm: Add support for data integrity to DM Martin K. Petersen
2008-10-01  7:38 ` [PATCH 7/7] md: Add support for data integrity to MD Martin K. Petersen
2008-10-02 10:56 ` Block integrity patches for 2.6.28 Jens Axboe
2008-10-02 13:54   ` Martin K. Petersen
2008-10-02 16:29     ` Jens Axboe

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20081001081056.GB19428@kernel.dk \
    --to=jens.axboe@oracle.com \
    --cc=agk@sourceware.org \
    --cc=dm-devel@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=martin.petersen@oracle.com \
    --cc=neilb@suse.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox