qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Kevin Wolf <kwolf@redhat.com>
To: Eric Blake <eblake@redhat.com>
Cc: qemu-devel@nongnu.org, jsnow@redhat.com, famz@redhat.com,
	qemu-block@nongnu.org, Stefan Hajnoczi <stefanha@redhat.com>,
	Max Reitz <mreitz@redhat.com>, Jeff Cody <jcody@redhat.com>
Subject: Re: [Qemu-devel] [PATCH v5 01/23] block: Allow NULL file for bdrv_get_block_status()
Date: Tue, 10 Oct 2017 15:59:40 +0200	[thread overview]
Message-ID: <20171010135940.GJ4177@dhcp-200-186.str.redhat.com> (raw)
In-Reply-To: <20171004020048.26379-2-eblake@redhat.com>

Am 04.10.2017 um 04:00 hat Eric Blake geschrieben:
> Not all callers care about which BDS owns the mapping for a given
> range of the file.  This patch merely simplifies the callers by
> consolidating the logic in the common call point, while guaranteeing
> a non-NULL file to all the driver callbacks, for no semantic change.
> The only caller that does not care about pnum is bdrv_is_allocated,
> as invoked by vvfat; we can likewise add assertions that the rest
> of the stack does not have to worry about a NULL pnum.
> 
> Furthermore, this will also set the stage for a future cleanup: when
> a caller does not care about which BDS owns an offset, it would be
> nice to allow the driver to optimize things to not have to return
> BDRV_BLOCK_OFFSET_VALID in the first place.  In the case of fragmented
> allocation (for example, it's fairly easy to create a qcow2 image
> where consecutive guest addresses are not at consecutive host
> addresses), the current contract requires bdrv_get_block_status()
> to clamp *pnum to the limit where host addresses are no longer
> consecutive, but allowing a NULL file means that *pnum could be
> set to the full length of known-allocated data.
> 
> Signed-off-by: Eric Blake <eblake@redhat.com>
> 
> ---
> v5: use second label for cleaner exit logic [John], use local_pnum
> v4: only context changes
> v3: rebase to recent changes (qcow2_measure), dropped R-b
> v2: use local variable and final transfer, rather than assignment
> of parameter to local
> [previously in different series]:
> v2: new patch, https://lists.gnu.org/archive/html/qemu-devel/2017-05/msg05645.html
> ---
>  include/block/block_int.h | 10 ++++----
>  block/io.c                | 58 ++++++++++++++++++++++++++---------------------
>  block/mirror.c            |  3 +--
>  block/qcow2.c             |  8 ++-----
>  qemu-img.c                | 10 ++++----
>  5 files changed, 45 insertions(+), 44 deletions(-)
> 
> diff --git a/include/block/block_int.h b/include/block/block_int.h
> index 79366b94b5..3b4158f576 100644
> --- a/include/block/block_int.h
> +++ b/include/block/block_int.h
> @@ -202,10 +202,12 @@ struct BlockDriver {
>          int64_t offset, int bytes);
> 
>      /*
> -     * Building block for bdrv_block_status[_above]. The driver should
> -     * answer only according to the current layer, and should not
> -     * set BDRV_BLOCK_ALLOCATED, but may set BDRV_BLOCK_RAW.  See block.h
> -     * for the meaning of _DATA, _ZERO, and _OFFSET_VALID.
> +     * Building block for bdrv_block_status[_above] and
> +     * bdrv_is_allocated[_above].  The driver should answer only
> +     * according to the current layer, and should not set
> +     * BDRV_BLOCK_ALLOCATED, but may set BDRV_BLOCK_RAW.  See block.h
> +     * for the meaning of _DATA, _ZERO, and _OFFSET_VALID.  The block
> +     * layer guarantees non-NULL pnum and file.
>       */
>      int64_t coroutine_fn (*bdrv_co_get_block_status)(BlockDriverState *bs,
>          int64_t sector_num, int nb_sectors, int *pnum,
> diff --git a/block/io.c b/block/io.c
> index 1e246315a7..e5a6f63eea 100644
> --- a/block/io.c
> +++ b/block/io.c
> @@ -698,7 +698,6 @@ int bdrv_make_zero(BdrvChild *child, BdrvRequestFlags flags)
>  {
>      int64_t target_sectors, ret, nb_sectors, sector_num = 0;
>      BlockDriverState *bs = child->bs;
> -    BlockDriverState *file;
>      int n;
> 
>      target_sectors = bdrv_nb_sectors(bs);
> @@ -711,7 +710,7 @@ int bdrv_make_zero(BdrvChild *child, BdrvRequestFlags flags)
>          if (nb_sectors <= 0) {
>              return 0;
>          }
> -        ret = bdrv_get_block_status(bs, sector_num, nb_sectors, &n, &file);
> +        ret = bdrv_get_block_status(bs, sector_num, nb_sectors, &n, NULL);
>          if (ret < 0) {
>              error_report("error getting block status at sector %" PRId64 ": %s",
>                           sector_num, strerror(-ret));
> @@ -1800,8 +1799,9 @@ int64_t coroutine_fn bdrv_co_get_block_status_from_backing(BlockDriverState *bs,
>   * beyond the end of the disk image it will be clamped; if 'pnum' is set to
>   * the end of the image, then the returned value will include BDRV_BLOCK_EOF.
>   *
> - * If returned value is positive and BDRV_BLOCK_OFFSET_VALID bit is set, 'file'
> - * points to the BDS which the sector range is allocated in.
> + * If returned value is positive, BDRV_BLOCK_OFFSET_VALID bit is set, and
> + * 'file' is non-NULL, then '*file' points to the BDS which the sector range
> + * is allocated in.
>   */
>  static int64_t coroutine_fn bdrv_co_get_block_status(BlockDriverState *bs,
>                                                       int64_t sector_num,
> @@ -1811,16 +1811,19 @@ static int64_t coroutine_fn bdrv_co_get_block_status(BlockDriverState *bs,
>      int64_t total_sectors;
>      int64_t n;
>      int64_t ret, ret2;
> +    BlockDriverState *local_file = NULL;
> +    int local_pnum = 0;

I don't quite see what the point of local_pnum is if we assert anyway
that the real pnum is non-NULL.

> -    *file = NULL;
> +    assert(pnum);
>      total_sectors = bdrv_nb_sectors(bs);
>      if (total_sectors < 0) {
> -        return total_sectors;
> +        ret = total_sectors;
> +        goto early_out;
>      }
> 
>      if (sector_num >= total_sectors || !nb_sectors) {
> -        *pnum = 0;
> -        return sector_num >= total_sectors ? BDRV_BLOCK_EOF : 0;
> +        ret = sector_num >= total_sectors ? BDRV_BLOCK_EOF : 0;
> +        goto early_out;
>      }
> 
>      n = total_sectors - sector_num;
> @@ -1829,30 +1832,30 @@ static int64_t coroutine_fn bdrv_co_get_block_status(BlockDriverState *bs,
>      }
> 
>      if (!bs->drv->bdrv_co_get_block_status) {
> -        *pnum = nb_sectors;
> +        local_pnum = nb_sectors;
>          ret = BDRV_BLOCK_DATA | BDRV_BLOCK_ALLOCATED;
>          if (sector_num + nb_sectors == total_sectors) {
>              ret |= BDRV_BLOCK_EOF;
>          }
>          if (bs->drv->protocol_name) {
>              ret |= BDRV_BLOCK_OFFSET_VALID | (sector_num * BDRV_SECTOR_SIZE);
> -            *file = bs;
> +            local_file = bs;
>          }
> -        return ret;
> +        goto early_out;
>      }
> 
>      bdrv_inc_in_flight(bs);
> -    ret = bs->drv->bdrv_co_get_block_status(bs, sector_num, nb_sectors, pnum,
> -                                            file);
> +    ret = bs->drv->bdrv_co_get_block_status(bs, sector_num, nb_sectors,
> +                                            &local_pnum, &local_file);
>      if (ret < 0) {
> -        *pnum = 0;
> +        local_pnum = 0;
>          goto out;
>      }
> 
>      if (ret & BDRV_BLOCK_RAW) {
> -        assert(ret & BDRV_BLOCK_OFFSET_VALID && *file);
> -        ret = bdrv_co_get_block_status(*file, ret >> BDRV_SECTOR_BITS,
> -                                       *pnum, pnum, file);
> +        assert(ret & BDRV_BLOCK_OFFSET_VALID && local_file);
> +        ret = bdrv_co_get_block_status(local_file, ret >> BDRV_SECTOR_BITS,
> +                                       local_pnum, &local_pnum, &local_file);
>          goto out;
>      }
> 
> @@ -1870,14 +1873,13 @@ static int64_t coroutine_fn bdrv_co_get_block_status(BlockDriverState *bs,
>          }
>      }
> 
> -    if (*file && *file != bs &&
> +    if (local_file && local_file != bs &&
>          (ret & BDRV_BLOCK_DATA) && !(ret & BDRV_BLOCK_ZERO) &&
>          (ret & BDRV_BLOCK_OFFSET_VALID)) {
> -        BlockDriverState *file2;
>          int file_pnum;
> 
> -        ret2 = bdrv_co_get_block_status(*file, ret >> BDRV_SECTOR_BITS,
> -                                        *pnum, &file_pnum, &file2);
> +        ret2 = bdrv_co_get_block_status(local_file, ret >> BDRV_SECTOR_BITS,
> +                                        local_pnum, &file_pnum, NULL);
>          if (ret2 >= 0) {
>              /* Ignore errors.  This is just providing extra information, it
>               * is useful but not necessary.
> @@ -1892,17 +1894,22 @@ static int64_t coroutine_fn bdrv_co_get_block_status(BlockDriverState *bs,
>                  ret |= BDRV_BLOCK_ZERO;
>              } else {
>                  /* Limit request to the range reported by the protocol driver */
> -                *pnum = file_pnum;
> +                local_pnum = file_pnum;
>                  ret |= (ret2 & BDRV_BLOCK_ZERO);
>              }
>          }
>      }
> 
> -out:
> + out:

git grep tells me that the old style (unindented label, as opposed to
indented by a single space) is a lot more common both within the block
layer and across qemu as a whole.

>      bdrv_dec_in_flight(bs);
> -    if (ret >= 0 && sector_num + *pnum == total_sectors) {
> +    if (ret >= 0 && sector_num + local_pnum == total_sectors) {
>          ret |= BDRV_BLOCK_EOF;
>      }
> + early_out:

So it's better to change this one to match the old out: than the other
way round.

> +    *pnum = local_pnum;
> +    if (file) {
> +        *file = local_file;
> +    }
>      return ret;
>  }

Kevin

  reply	other threads:[~2017-10-10 14:00 UTC|newest]

Thread overview: 39+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-10-04  2:00 [Qemu-devel] [PATCH v5 00/23] make bdrv_get_block_status byte-based Eric Blake
2017-10-04  2:00 ` [Qemu-devel] [PATCH v5 01/23] block: Allow NULL file for bdrv_get_block_status() Eric Blake
2017-10-10 13:59   ` Kevin Wolf [this message]
2017-10-10 14:43     ` Eric Blake
2017-10-10 19:00       ` Eric Blake
2017-10-10 19:24         ` John Snow
2017-10-11  8:42           ` Kevin Wolf
2017-10-11 17:42             ` John Snow
2017-10-04  2:00 ` [Qemu-devel] [PATCH v5 02/23] block: Add flag to avoid wasted work in bdrv_is_allocated() Eric Blake
2017-10-04  2:00 ` [Qemu-devel] [PATCH v5 03/23] block: Make bdrv_round_to_clusters() signature more useful Eric Blake
2017-10-04  2:00 ` [Qemu-devel] [PATCH v5 04/23] qcow2: Switch is_zero_sectors() to byte-based Eric Blake
2017-10-10 14:15   ` Kevin Wolf
2017-10-10 14:47     ` Eric Blake
2017-10-04  2:00 ` [Qemu-devel] [PATCH v5 05/23] block: Switch bdrv_make_zero() " Eric Blake
2017-10-04  2:00 ` [Qemu-devel] [PATCH v5 06/23] qemu-img: Switch get_block_status() " Eric Blake
2017-10-04  2:00 ` [Qemu-devel] [PATCH v5 07/23] block: Convert bdrv_get_block_status() to bytes Eric Blake
2017-10-10 14:46   ` Kevin Wolf
2017-10-10 15:38     ` Eric Blake
2017-10-04  2:00 ` [Qemu-devel] [PATCH v5 08/23] block: Switch bdrv_co_get_block_status() to byte-based Eric Blake
2017-10-04  2:00 ` [Qemu-devel] [PATCH v5 09/23] block: Switch BdrvCoGetBlockStatusData " Eric Blake
2017-10-09 20:07   ` [Qemu-devel] [Qemu-block] " Jeff Cody
2017-10-09 21:30     ` Eric Blake
2017-10-04  2:00 ` [Qemu-devel] [PATCH v5 10/23] block: Switch bdrv_common_block_status_above() " Eric Blake
2017-10-04  2:00 ` [Qemu-devel] [PATCH v5 11/23] block: Switch bdrv_co_get_block_status_above() " Eric Blake
2017-10-04  2:00 ` [Qemu-devel] [PATCH v5 12/23] block: Convert bdrv_get_block_status_above() to bytes Eric Blake
2017-10-04  2:00 ` [Qemu-devel] [PATCH v5 13/23] qemu-img: Simplify logic in img_compare() Eric Blake
2017-10-04  2:00 ` [Qemu-devel] [PATCH v5 14/23] qemu-img: Speed up compare on pre-allocated larger file Eric Blake
2017-10-11 18:33   ` Eric Blake
2017-10-04  2:00 ` [Qemu-devel] [PATCH v5 15/23] qemu-img: Add find_nonzero() Eric Blake
2017-10-04  2:00 ` [Qemu-devel] [PATCH v5 16/23] qemu-img: Drop redundant error message in compare Eric Blake
2017-10-04  2:00 ` [Qemu-devel] [PATCH v5 17/23] qemu-img: Change check_empty_sectors() to byte-based Eric Blake
2017-10-04  2:00 ` [Qemu-devel] [PATCH v5 18/23] qemu-img: Change compare_sectors() to be byte-based Eric Blake
2017-10-04  2:00 ` [Qemu-devel] [PATCH v5 19/23] qemu-img: Change img_rebase() " Eric Blake
2017-10-04  2:00 ` [Qemu-devel] [PATCH v5 20/23] qemu-img: Change img_compare() " Eric Blake
2017-10-04  2:00 ` [Qemu-devel] [PATCH v5 21/23] block: Align block status requests Eric Blake
2017-10-04  2:00 ` [Qemu-devel] [PATCH v5 22/23] block: Relax bdrv_aligned_preadv() assertion Eric Blake
2017-10-04  2:00 ` [Qemu-devel] [PATCH v5 23/23] qemu-io: Relax 'alloc' now that block-status doesn't assert Eric Blake
2017-10-10 12:58 ` [Qemu-devel] [PATCH v5 00/23] make bdrv_get_block_status byte-based Kevin Wolf
2017-10-10 14:48   ` Eric Blake

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=20171010135940.GJ4177@dhcp-200-186.str.redhat.com \
    --to=kwolf@redhat.com \
    --cc=eblake@redhat.com \
    --cc=famz@redhat.com \
    --cc=jcody@redhat.com \
    --cc=jsnow@redhat.com \
    --cc=mreitz@redhat.com \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    --cc=stefanha@redhat.com \
    /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;
as well as URLs for NNTP newsgroup(s).