From nobody Mon Feb 9 02:28:37 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1507780354511166.37997562723092; Wed, 11 Oct 2017 20:52:34 -0700 (PDT) Received: from localhost ([::1]:43543 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1e2UXx-00066T-ID for importer@patchew.org; Wed, 11 Oct 2017 23:52:33 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:58066) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1e2UTD-0002Rf-7z for qemu-devel@nongnu.org; Wed, 11 Oct 2017 23:47:41 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1e2UTB-0007CE-Eh for qemu-devel@nongnu.org; Wed, 11 Oct 2017 23:47:39 -0400 Received: from mx1.redhat.com ([209.132.183.28]:36784) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1e2UT7-00077J-AM; Wed, 11 Oct 2017 23:47:33 -0400 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 530113DBCE; Thu, 12 Oct 2017 03:47:32 +0000 (UTC) Received: from red.redhat.com (ovpn-122-223.rdu2.redhat.com [10.10.122.223]) by smtp.corp.redhat.com (Postfix) with ESMTP id 7D08B675F0; Thu, 12 Oct 2017 03:47:28 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 530113DBCE Authentication-Results: ext-mx06.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx06.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=eblake@redhat.com From: Eric Blake To: qemu-devel@nongnu.org Date: Wed, 11 Oct 2017 22:46:57 -0500 Message-Id: <20171012034720.11947-2-eblake@redhat.com> In-Reply-To: <20171012034720.11947-1-eblake@redhat.com> References: <20171012034720.11947-1-eblake@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.30]); Thu, 12 Oct 2017 03:47:32 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v6 01/24] block: Allow NULL file for bdrv_get_block_status() X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, famz@redhat.com, qemu-block@nongnu.org, Jeff Cody , Max Reitz , Stefan Hajnoczi , jsnow@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" 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 --- v6: drop local_pnum experiment, tweak label indent [Kevin] 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/msg056= 45.html --- include/block/block_int.h | 10 ++++++---- block/io.c | 49 ++++++++++++++++++++++++++-----------------= ---- block/mirror.c | 3 +-- block/qcow2.c | 8 ++------ qemu-img.c | 10 ++++------ 5 files changed, 40 insertions(+), 40 deletions(-) diff --git a/include/block/block_int.h b/include/block/block_int.h index 7e8a206239..489b390fd6 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 8e419070b5..51d2b219b8 100644 --- a/block/io.c +++ b/block/io.c @@ -698,7 +698,6 @@ int bdrv_make_zero(BdrvChild *child, BdrvRequestFlags f= lags) { int64_t target_sectors, ret, nb_sectors, sector_num =3D 0; BlockDriverState *bs =3D child->bs; - BlockDriverState *file; int n; target_sectors =3D bdrv_nb_sectors(bs); @@ -711,7 +710,7 @@ int bdrv_make_zero(BdrvChild *child, BdrvRequestFlags f= lags) if (nb_sectors <=3D 0) { return 0; } - ret =3D bdrv_get_block_status(bs, sector_num, nb_sectors, &n, &fil= e); + ret =3D 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_ba= cking(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_E= OF. * - * 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 ran= ge + * is allocated in. */ static int64_t coroutine_fn bdrv_co_get_block_status(BlockDriverState *bs, int64_t sector_num, @@ -1811,20 +1811,23 @@ static int64_t coroutine_fn bdrv_co_get_block_statu= s(BlockDriverState *bs, int64_t total_sectors; int64_t n; int64_t ret, ret2; + BlockDriverState *local_file =3D NULL; - *file =3D NULL; + assert(pnum); + *pnum =3D 0; total_sectors =3D bdrv_nb_sectors(bs); if (total_sectors < 0) { - return total_sectors; + ret =3D total_sectors; + goto early_out; } if (sector_num >=3D total_sectors) { - *pnum =3D 0; - return BDRV_BLOCK_EOF; + ret =3D BDRV_BLOCK_EOF; + goto early_out; } if (!nb_sectors) { - *pnum =3D 0; - return 0; + ret =3D 0; + goto early_out; } n =3D total_sectors - sector_num; @@ -1840,23 +1843,23 @@ static int64_t coroutine_fn bdrv_co_get_block_statu= s(BlockDriverState *bs, } if (bs->drv->protocol_name) { ret |=3D BDRV_BLOCK_OFFSET_VALID | (sector_num * BDRV_SECTOR_S= IZE); - *file =3D bs; + local_file =3D bs; } - return ret; + goto early_out; } bdrv_inc_in_flight(bs); ret =3D bs->drv->bdrv_co_get_block_status(bs, sector_num, nb_sectors, = pnum, - file); + &local_file); if (ret < 0) { *pnum =3D 0; goto out; } if (ret & BDRV_BLOCK_RAW) { - assert(ret & BDRV_BLOCK_OFFSET_VALID && *file); - ret =3D bdrv_co_get_block_status(*file, ret >> BDRV_SECTOR_BITS, - *pnum, pnum, file); + assert(ret & BDRV_BLOCK_OFFSET_VALID && local_file); + ret =3D bdrv_co_get_block_status(local_file, ret >> BDRV_SECTOR_BI= TS, + *pnum, pnum, &local_file); goto out; } @@ -1874,14 +1877,13 @@ static int64_t coroutine_fn bdrv_co_get_block_statu= s(BlockDriverState *bs, } } - if (*file && *file !=3D bs && + if (local_file && local_file !=3D bs && (ret & BDRV_BLOCK_DATA) && !(ret & BDRV_BLOCK_ZERO) && (ret & BDRV_BLOCK_OFFSET_VALID)) { - BlockDriverState *file2; int file_pnum; - ret2 =3D bdrv_co_get_block_status(*file, ret >> BDRV_SECTOR_BITS, - *pnum, &file_pnum, &file2); + ret2 =3D bdrv_co_get_block_status(local_file, ret >> BDRV_SECTOR_B= ITS, + *pnum, &file_pnum, NULL); if (ret2 >=3D 0) { /* Ignore errors. This is just providing extra information, it * is useful but not necessary. @@ -1907,6 +1909,10 @@ out: if (ret >=3D 0 && sector_num + *pnum =3D=3D total_sectors) { ret |=3D BDRV_BLOCK_EOF; } +early_out: + if (file) { + *file =3D local_file; + } return ret; } @@ -2006,7 +2012,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 =3D offset >> BDRV_SECTOR_BITS; int nb_sectors =3D bytes >> BDRV_SECTOR_BITS; int64_t ret; @@ -2015,7 +2020,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 =3D 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 153758ca9f..e76e754d26 100644 --- a/block/mirror.c +++ b/block/mirror.c @@ -390,7 +390,6 @@ static uint64_t coroutine_fn mirror_iteration(MirrorBlo= ckJob *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(MirrorBlo= ckJob *s) ret =3D bdrv_get_block_status_above(source, NULL, offset >> BDRV_SECTOR_BITS, nb_chunks * sectors_per_chunk, - &io_sectors, &file); + &io_sectors, NULL); io_bytes =3D io_sectors * BDRV_SECTOR_SIZE; if (ret < 0) { io_bytes =3D MIN(nb_chunks * s->granularity, max_io_bytes); diff --git a/block/qcow2.c b/block/qcow2.c index d742b146da..990917096f 100644 --- a/block/qcow2.c +++ b/block/qcow2.c @@ -3003,7 +3003,6 @@ static bool is_zero_sectors(BlockDriverState *bs, int= 64_t start, uint32_t count) { int nr; - BlockDriverState *file; int64_t res; if (start + count > bs->total_sectors) { @@ -3013,8 +3012,7 @@ static bool is_zero_sectors(BlockDriverState *bs, int= 64_t start, if (!count) { return true; } - res =3D bdrv_get_block_status_above(bs, NULL, start, count, - &nr, &file); + res =3D bdrv_get_block_status_above(bs, NULL, start, count, &nr, NULL); return res >=3D 0 && (res & BDRV_BLOCK_ZERO) && nr =3D=3D count; } @@ -3732,13 +3730,11 @@ static BlockMeasureInfo *qcow2_measure(QemuOpts *op= ts, BlockDriverState *in_bs, offset +=3D pnum * BDRV_SECTOR_SIZE) { int nb_sectors =3D MIN(ssize - offset, BDRV_REQUEST_MAX_BYTES) / BDRV_SECTOR= _SIZE; - BlockDriverState *file; int64_t ret; ret =3D bdrv_get_block_status_above(in_bs, NULL, offset >> BDRV_SECTOR_BI= TS, - 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 d6007b2a6d..e9c7b30c91 100644 --- a/qemu-img.c +++ b/qemu-img.c @@ -1375,7 +1375,6 @@ static int img_compare(int argc, char **argv) for (;;) { int64_t status1, status2; - BlockDriverState *file; nb_sectors =3D sectors_to_process(total_sectors, sector_num); if (nb_sectors <=3D 0) { @@ -1383,7 +1382,7 @@ static int img_compare(int argc, char **argv) } status1 =3D bdrv_get_block_status_above(bs1, NULL, sector_num, total_sectors1 - sector_num, - &pnum1, &file); + &pnum1, NULL); if (status1 < 0) { ret =3D 3; error_report("Sector allocation test failed for %s", filename1= ); @@ -1393,7 +1392,7 @@ static int img_compare(int argc, char **argv) status2 =3D bdrv_get_block_status_above(bs2, NULL, sector_num, total_sectors2 - sector_num, - &pnum2, &file); + &pnum2, NULL); if (status2 < 0) { ret =3D 3; error_report("Sector allocation test failed for %s", filename2= ); @@ -1599,15 +1598,14 @@ static int convert_iteration_sectors(ImgConvertStat= e *s, int64_t sector_num) n =3D MIN(s->total_sectors - sector_num, BDRV_REQUEST_MAX_SECTORS); if (s->sector_next_status <=3D sector_num) { - BlockDriverState *file; if (s->target_has_backing) { ret =3D bdrv_get_block_status(blk_bs(s->src[src_cur]), sector_num - src_cur_offset, - n, &n, &file); + n, &n, NULL); } else { ret =3D bdrv_get_block_status_above(blk_bs(s->src[src_cur]), N= ULL, sector_num - src_cur_offset, - n, &n, &file); + n, &n, NULL); } if (ret < 0) { return ret; --=20 2.13.6