From: John Snow <jsnow@redhat.com>
To: Eric Blake <eblake@redhat.com>, qemu-devel@nongnu.org
Cc: kwolf@redhat.com, famz@redhat.com, qemu-block@nongnu.org,
Jeff Cody <jcody@redhat.com>, Max Reitz <mreitz@redhat.com>,
Stefan Hajnoczi <stefanha@redhat.com>
Subject: Re: [Qemu-devel] [PATCH v4 01/23] block: Allow NULL file for bdrv_get_block_status()
Date: Mon, 25 Sep 2017 18:43:37 -0400 [thread overview]
Message-ID: <8ded19fb-d0dc-d816-b045-07184bf77db3@redhat.com> (raw)
In-Reply-To: <20170913160333.23622-2-eblake@redhat.com>
On 09/13/2017 12:03 PM, Eric Blake wrote:
> 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>
>
> ---
> 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 | 44 ++++++++++++++++++++++++++++----------------
> block/mirror.c | 3 +--
> block/qcow2.c | 8 ++------
> qemu-img.c | 10 ++++------
> 5 files changed, 41 insertions(+), 34 deletions(-)
>
> diff --git a/include/block/block_int.h b/include/block/block_int.h
> index 55c5d573d4..7f71c585a0 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 8a0cd8835a..f250029395 100644
> --- a/block/io.c
> +++ b/block/io.c
> @@ -695,7 +695,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);
> @@ -708,7 +707,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));
> @@ -1755,8 +1754,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,
> @@ -1766,15 +1766,22 @@ 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;
>
> - *file = NULL;
> + assert(pnum);
nice assert..
> total_sectors = bdrv_nb_sectors(bs);
> if (total_sectors < 0) {
> + if (file) {
> + *file = NULL;
> + }
Function reads slightly worse for the wear now with all of the return
logic handled at various places within, but unifying it might be even
stranger, perhaps..
Let's see if I hate this more:
out:
bdrv_dec_in_flight(bs);
bdrv_dec_in_flight(bs);
if (ret >= 0 && sector_num + *pnum == total_sectors) {
ret |= BDRV_BLOCK_EOF;
}
early_out:
if (file) {
*file = local_file;
}
return ret;
and then earlier in the function, we can just:
if (total_sectors < 0) {
ret = total_sectors;
goto early_out;
}
> return total_sectors;
> }
>
> if (sector_num >= total_sectors) {
> *pnum = 0;
> + if (file) {
> + *file = NULL;
> + }
ret = BDRV_BLOCK_EOF;
goto early_out;
> return BDRV_BLOCK_EOF;
> }
>
> @@ -1791,23 +1798,27 @@ static int64_t coroutine_fn bdrv_co_get_block_status(BlockDriverState *bs,
> }
> if (bs->drv->protocol_name) {
> ret |= BDRV_BLOCK_OFFSET_VALID | (sector_num * BDRV_SECTOR_SIZE);
> - *file = bs;
> + if (file) {
> + *file = bs;
> + }
local_file = bs;
> + } else if (file) {
> + *file = NULL;
no longer needed
> }
> return ret;
replaced with:
goto early_out;
> }
>
> bdrv_inc_in_flight(bs);
> ret = bs->drv->bdrv_co_get_block_status(bs, sector_num, nb_sectors, pnum,
> - file);
> + &local_file);
> if (ret < 0) {
> *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,
> + *pnum, pnum, &local_file);
> goto out;
> }
>
> @@ -1825,14 +1836,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,
> + *pnum, &file_pnum, NULL);
> if (ret2 >= 0) {
> /* Ignore errors. This is just providing extra information, it
> * is useful but not necessary.
> @@ -1854,6 +1864,9 @@ static int64_t coroutine_fn bdrv_co_get_block_status(BlockDriverState *bs,
> }
>
> out:
> + if (file) {
> + *file = local_file;
> + }
> bdrv_dec_in_flight(bs);
> if (ret >= 0 && sector_num + *pnum == total_sectors) {
> ret |= BDRV_BLOCK_EOF;
> @@ -1957,7 +1970,6 @@ int64_t bdrv_get_block_status(BlockDriverState *bs,
> int coroutine_fn bdrv_is_allocated(BlockDriverState *bs, int64_t offset,
> int64_t bytes, int64_t *pnum)
> {
> - BlockDriverState *file;
> int64_t sector_num = offset >> BDRV_SECTOR_BITS;
> int nb_sectors = bytes >> BDRV_SECTOR_BITS;
> int64_t ret;
> @@ -1966,7 +1978,7 @@ int coroutine_fn bdrv_is_allocated(BlockDriverState *bs, int64_t offset,
> assert(QEMU_IS_ALIGNED(offset, BDRV_SECTOR_SIZE));
> assert(QEMU_IS_ALIGNED(bytes, BDRV_SECTOR_SIZE) && bytes < INT_MAX);
> ret = bdrv_get_block_status(bs, sector_num, nb_sectors, &psectors,
> - &file);
> + NULL);
> if (ret < 0) {
> return ret;
> }
> diff --git a/block/mirror.c b/block/mirror.c
> index 5cdaaed7be..032cfe91fa 100644
> --- a/block/mirror.c
> +++ b/block/mirror.c
> @@ -390,7 +390,6 @@ static uint64_t coroutine_fn mirror_iteration(MirrorBlockJob *s)
> int io_sectors;
> unsigned int io_bytes;
> int64_t io_bytes_acct;
> - BlockDriverState *file;
> enum MirrorMethod {
> MIRROR_METHOD_COPY,
> MIRROR_METHOD_ZERO,
> @@ -401,7 +400,7 @@ static uint64_t coroutine_fn mirror_iteration(MirrorBlockJob *s)
> ret = bdrv_get_block_status_above(source, NULL,
> offset >> BDRV_SECTOR_BITS,
> nb_chunks * sectors_per_chunk,
> - &io_sectors, &file);
> + &io_sectors, NULL);
> io_bytes = io_sectors * BDRV_SECTOR_SIZE;
> if (ret < 0) {
> io_bytes = MIN(nb_chunks * s->granularity, max_io_bytes);
> diff --git a/block/qcow2.c b/block/qcow2.c
> index 64dcd98a91..9a7b5cd41f 100644
> --- a/block/qcow2.c
> +++ b/block/qcow2.c
> @@ -2975,7 +2975,6 @@ static bool is_zero_sectors(BlockDriverState *bs, int64_t start,
> uint32_t count)
> {
> int nr;
> - BlockDriverState *file;
> int64_t res;
>
> if (start + count > bs->total_sectors) {
> @@ -2985,8 +2984,7 @@ static bool is_zero_sectors(BlockDriverState *bs, int64_t start,
> if (!count) {
> return true;
> }
> - res = bdrv_get_block_status_above(bs, NULL, start, count,
> - &nr, &file);
> + res = bdrv_get_block_status_above(bs, NULL, start, count, &nr, NULL);
> return res >= 0 && (res & BDRV_BLOCK_ZERO) && nr == count;
> }
>
> @@ -3654,13 +3652,11 @@ static BlockMeasureInfo *qcow2_measure(QemuOpts *opts, BlockDriverState *in_bs,
> offset += pnum * BDRV_SECTOR_SIZE) {
> int nb_sectors = MIN(ssize - offset,
> BDRV_REQUEST_MAX_BYTES) / BDRV_SECTOR_SIZE;
> - BlockDriverState *file;
> int64_t ret;
>
> ret = bdrv_get_block_status_above(in_bs, NULL,
> offset >> BDRV_SECTOR_BITS,
> - nb_sectors,
> - &pnum, &file);
> + nb_sectors, &pnum, NULL);
> if (ret < 0) {
> error_setg_errno(&local_err, -ret,
> "Unable to get block status");
> diff --git a/qemu-img.c b/qemu-img.c
> index df984b11b9..0c12e1c240 100644
> --- a/qemu-img.c
> +++ b/qemu-img.c
> @@ -1374,7 +1374,6 @@ static int img_compare(int argc, char **argv)
>
> for (;;) {
> int64_t status1, status2;
> - BlockDriverState *file;
>
> nb_sectors = sectors_to_process(total_sectors, sector_num);
> if (nb_sectors <= 0) {
> @@ -1382,7 +1381,7 @@ static int img_compare(int argc, char **argv)
> }
> status1 = bdrv_get_block_status_above(bs1, NULL, sector_num,
> total_sectors1 - sector_num,
> - &pnum1, &file);
> + &pnum1, NULL);
> if (status1 < 0) {
> ret = 3;
> error_report("Sector allocation test failed for %s", filename1);
> @@ -1392,7 +1391,7 @@ static int img_compare(int argc, char **argv)
>
> status2 = bdrv_get_block_status_above(bs2, NULL, sector_num,
> total_sectors2 - sector_num,
> - &pnum2, &file);
> + &pnum2, NULL);
> if (status2 < 0) {
> ret = 3;
> error_report("Sector allocation test failed for %s", filename2);
> @@ -1598,15 +1597,14 @@ static int convert_iteration_sectors(ImgConvertState *s, int64_t sector_num)
> n = MIN(s->total_sectors - sector_num, BDRV_REQUEST_MAX_SECTORS);
>
> if (s->sector_next_status <= sector_num) {
> - BlockDriverState *file;
> if (s->target_has_backing) {
> ret = bdrv_get_block_status(blk_bs(s->src[src_cur]),
> sector_num - src_cur_offset,
> - n, &n, &file);
> + n, &n, NULL);
> } else {
> ret = bdrv_get_block_status_above(blk_bs(s->src[src_cur]), NULL,
> sector_num - src_cur_offset,
> - n, &n, &file);
> + n, &n, NULL);
> }
> if (ret < 0) {
> return ret;
>
It's only shed paint, though:
Reviewed-by: John Snow <jsnow@redhat.com>
I'm looking at the rest of the series now, so please stand by.
next prev parent reply other threads:[~2017-09-25 22:43 UTC|newest]
Thread overview: 64+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-09-13 16:03 [Qemu-devel] [PATCH v4 00/23] make bdrv_get_block_status byte-based Eric Blake
2017-09-13 16:03 ` [Qemu-devel] [PATCH v4 01/23] block: Allow NULL file for bdrv_get_block_status() Eric Blake
2017-09-25 22:43 ` John Snow [this message]
2017-09-27 21:46 ` Eric Blake
2017-09-13 16:03 ` [Qemu-devel] [PATCH v4 02/23] block: Add flag to avoid wasted work in bdrv_is_allocated() Eric Blake
2017-09-26 18:31 ` John Snow
2017-09-28 14:58 ` Eric Blake
2017-09-13 16:03 ` [Qemu-devel] [PATCH v4 03/23] block: Make bdrv_round_to_clusters() signature more useful Eric Blake
2017-09-26 18:51 ` John Snow
2017-09-26 19:18 ` Eric Blake
2017-09-26 19:29 ` John Snow
2017-09-28 22:29 ` Eric Blake
2017-09-29 20:03 ` Eric Blake
2017-09-13 16:03 ` [Qemu-devel] [PATCH v4 04/23] qcow2: Switch is_zero_sectors() to byte-based Eric Blake
2017-09-26 19:06 ` John Snow
2017-09-13 16:03 ` [Qemu-devel] [PATCH v4 05/23] block: Switch bdrv_make_zero() " Eric Blake
2017-09-26 19:13 ` John Snow
2017-09-13 16:03 ` [Qemu-devel] [PATCH v4 06/23] qemu-img: Switch get_block_status() " Eric Blake
2017-09-26 19:16 ` John Snow
2017-09-13 16:03 ` [Qemu-devel] [PATCH v4 07/23] block: Convert bdrv_get_block_status() to bytes Eric Blake
2017-09-26 19:39 ` John Snow
2017-09-26 19:57 ` Eric Blake
2017-09-13 16:03 ` [Qemu-devel] [PATCH v4 08/23] block: Switch bdrv_co_get_block_status() to byte-based Eric Blake
2017-09-26 20:15 ` John Snow
2017-09-13 16:03 ` [Qemu-devel] [PATCH v4 09/23] block: Switch BdrvCoGetBlockStatusData " Eric Blake
2017-09-26 20:20 ` John Snow
2017-09-13 16:03 ` [Qemu-devel] [PATCH v4 10/23] block: Switch bdrv_common_block_status_above() " Eric Blake
2017-09-27 18:26 ` John Snow
2017-09-13 16:03 ` [Qemu-devel] [PATCH v4 11/23] block: Switch bdrv_co_get_block_status_above() " Eric Blake
2017-09-27 18:31 ` John Snow
2017-09-13 16:03 ` [Qemu-devel] [PATCH v4 12/23] block: Convert bdrv_get_block_status_above() to bytes Eric Blake
2017-09-27 18:41 ` John Snow
2017-09-27 18:57 ` Eric Blake
2017-09-27 19:40 ` John Snow
2017-09-13 16:03 ` [Qemu-devel] [PATCH v4 13/23] qemu-img: Simplify logic in img_compare() Eric Blake
2017-09-27 19:05 ` John Snow
2017-09-27 19:15 ` Eric Blake
2017-09-13 16:03 ` [Qemu-devel] [PATCH v4 14/23] qemu-img: Speed up compare on pre-allocated larger file Eric Blake
2017-09-27 20:54 ` John Snow
2017-10-03 9:32 ` Vladimir Sementsov-Ogievskiy
2017-09-13 16:03 ` [Qemu-devel] [PATCH v4 15/23] qemu-img: Add find_nonzero() Eric Blake
2017-09-27 21:16 ` John Snow
2017-09-13 16:03 ` [Qemu-devel] [PATCH v4 16/23] qemu-img: Drop redundant error message in compare Eric Blake
2017-09-27 21:35 ` John Snow
2017-09-13 16:03 ` [Qemu-devel] [PATCH v4 17/23] qemu-img: Change check_empty_sectors() to byte-based Eric Blake
2017-09-27 21:43 ` John Snow
2017-09-13 16:03 ` [Qemu-devel] [PATCH v4 18/23] qemu-img: Change compare_sectors() to be byte-based Eric Blake
2017-09-27 22:25 ` John Snow
2017-09-13 16:03 ` [Qemu-devel] [PATCH v4 19/23] qemu-img: Change img_rebase() " Eric Blake
2017-09-29 19:38 ` John Snow
2017-09-13 16:03 ` [Qemu-devel] [PATCH v4 20/23] qemu-img: Change img_compare() " Eric Blake
2017-09-29 20:42 ` John Snow
2017-09-13 16:03 ` [Qemu-devel] [PATCH v4 21/23] block: Align block status requests Eric Blake
2017-09-13 19:26 ` Eric Blake
2017-09-13 20:36 ` Eric Blake
2017-10-02 20:24 ` John Snow
2017-10-02 23:51 ` Eric Blake
2017-09-13 16:03 ` [Qemu-devel] [PATCH v4 22/23] block: Relax bdrv_aligned_preadv() assertion Eric Blake
2017-10-02 21:20 ` John Snow
2017-09-13 16:03 ` [Qemu-devel] [PATCH v4 23/23] qemu-io: Relax 'alloc' now that block-status doesn't assert Eric Blake
2017-10-02 21:27 ` John Snow
2017-10-02 23:56 ` Eric Blake
2017-10-03 3:18 ` John Snow
2017-09-13 21:05 ` [Qemu-devel] [PATCH v4 00/23] make bdrv_get_block_status byte-based 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=8ded19fb-d0dc-d816-b045-07184bf77db3@redhat.com \
--to=jsnow@redhat.com \
--cc=eblake@redhat.com \
--cc=famz@redhat.com \
--cc=jcody@redhat.com \
--cc=kwolf@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).