From nobody Sun Feb 8 15:01:46 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 1499961552191422.6802397251489; Thu, 13 Jul 2017 08:59:12 -0700 (PDT) Received: from localhost ([::1]:60858 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dVgWE-0008VS-UK for importer@patchew.org; Thu, 13 Jul 2017 11:59:10 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:53951) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dVgLJ-0004wM-2H for qemu-devel@nongnu.org; Thu, 13 Jul 2017 11:47:57 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dVgLF-0005rR-Uh for qemu-devel@nongnu.org; Thu, 13 Jul 2017 11:47:53 -0400 Received: from mx1.redhat.com ([209.132.183.28]:37956) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dVgLC-0005pV-9f; Thu, 13 Jul 2017 11:47:46 -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 F19CEC04D2B3; Thu, 13 Jul 2017 15:47:44 +0000 (UTC) Received: from red.redhat.com (ovpn-121-60.rdu2.redhat.com [10.10.121.60]) by smtp.corp.redhat.com (Postfix) with ESMTP id 35534709EB; Thu, 13 Jul 2017 15:47:40 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com F19CEC04D2B3 Authentication-Results: ext-mx07.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx07.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=eblake@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com F19CEC04D2B3 From: Eric Blake To: qemu-devel@nongnu.org Date: Thu, 13 Jul 2017 10:46:56 -0500 Message-Id: <20170713154711.32374-9-eblake@redhat.com> In-Reply-To: <20170713154711.32374-1-eblake@redhat.com> References: <20170713154711.32374-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.31]); Thu, 13 Jul 2017 15:47:45 +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 v3 08/23] block: Switch bdrv_co_get_block_status() to byte-based 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, el13635@mail.ntua.gr, 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" We are gradually converting to byte-based interfaces, as they are easier to reason about than sector-based. Convert another internal function (no semantic change); and as with its public counterpart, rename to bdrv_co_block_status() to make the compiler enforce that we catch all uses. For now, we assert that callers still pass aligned data, but ultimately, this will be the function where we hand off to a byte-based driver callback, and will eventually need to add logic to ensure we round calls according to the driver's request_alignment then touch up the result handed back to the caller, to start permitting a caller to pass unaligned offsets. Note that we are now prepared to accepts 'bytes' larger than INT_MAX; this is okay as long as we clamp things internally before violating any 32-bit limits, and makes no difference to how a client will use the information (clients looping over the entire file must already be prepared for consecutive calls to return the same status, as drivers are already free to return shorter-than-maximal status due to any other convenient split points, such as when the L2 table crosses cluster boundaries in qcow2). Signed-off-by: Eric Blake --- v3: rebase to allocation/mapping sense change, clamp bytes to 32-bits when needed, drop R-b v2: rebase to earlier changes --- block/io.c | 91 +++++++++++++++++++++++++++++++++++++---------------------= ---- 1 file changed, 54 insertions(+), 37 deletions(-) diff --git a/block/io.c b/block/io.c index dcea3e3..6be3650 100644 --- a/block/io.c +++ b/block/io.c @@ -1724,42 +1724,43 @@ int64_t coroutine_fn bdrv_co_get_block_status_from_= backing(BlockDriverState *bs, * possible; otherwise, the result may omit that bit particularly if * it allows for a larger value in 'pnum'. * - * If 'sector_num' is beyond the end of the disk image the return value is + * If 'offset' is beyond the end of the disk image the return value is * BDRV_BLOCK_EOF and 'pnum' is set to 0. * - * 'pnum' is set to the number of sectors (including and immediately follo= wing - * the specified sector) that are known to be in the same - * allocated/unallocated state. + * 'pnum' is set to the number of bytes (including and immediately followi= ng + * the specified offset) that are known to be in the same + * allocated/unallocated state. It may be NULL. * - * 'nb_sectors' is the max value 'pnum' should be set to. If nb_sectors g= oes + * 'bytes' is the max value 'pnum' should be set to. If bytes goes * 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, 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. + * 'file' is non-NULL, then '*file' points to the BDS which owns the + * allocated sector that contains offset. */ -static int64_t coroutine_fn bdrv_co_get_block_status(BlockDriverState *bs, - bool mapping, - int64_t sector_num, - int nb_sectors, int *= pnum, - BlockDriverState **fi= le) +static int64_t coroutine_fn bdrv_co_block_status(BlockDriverState *bs, + bool mapping, + int64_t offset, int64_t b= ytes, + int64_t *pnum, + BlockDriverState **file) { - int64_t total_sectors; - int64_t n; + int64_t total_size; + int64_t n; /* bytes */ int64_t ret, ret2; BlockDriverState *local_file =3D NULL; + int count; /* sectors */ assert(pnum); - total_sectors =3D bdrv_nb_sectors(bs); - if (total_sectors < 0) { + total_size =3D bdrv_getlength(bs); + if (total_size < 0) { if (file) { *file =3D NULL; } - return total_sectors; + return total_size; } - if (sector_num >=3D total_sectors) { + if (offset >=3D total_size) { *pnum =3D 0; if (file) { *file =3D NULL; @@ -1767,19 +1768,19 @@ static int64_t coroutine_fn bdrv_co_get_block_statu= s(BlockDriverState *bs, return BDRV_BLOCK_EOF; } - n =3D total_sectors - sector_num; - if (n < nb_sectors) { - nb_sectors =3D n; + n =3D total_size - offset; + if (n < bytes) { + bytes =3D n; } if (!bs->drv->bdrv_co_get_block_status) { - *pnum =3D nb_sectors; + *pnum =3D bytes; ret =3D BDRV_BLOCK_DATA | BDRV_BLOCK_ALLOCATED; - if (sector_num + nb_sectors =3D=3D total_sectors) { + if (offset + bytes =3D=3D total_size) { ret |=3D BDRV_BLOCK_EOF; } if (bs->drv->protocol_name) { - ret |=3D BDRV_BLOCK_OFFSET_VALID | (sector_num * BDRV_SECTOR_S= IZE); + ret |=3D BDRV_BLOCK_OFFSET_VALID | (offset & BDRV_BLOCK_OFFSET= _MASK); if (file) { *file =3D bs; } @@ -1790,18 +1791,28 @@ static int64_t coroutine_fn bdrv_co_get_block_statu= s(BlockDriverState *bs, } bdrv_inc_in_flight(bs); - ret =3D bs->drv->bdrv_co_get_block_status(bs, sector_num, nb_sectors, = pnum, + /* + * TODO: Rather than require aligned offsets, we could instead + * round to the driver's request_alignment here, then touch up + * count afterwards back to the caller's expectations. + */ + assert(QEMU_IS_ALIGNED(offset | bytes, BDRV_SECTOR_SIZE)); + bytes =3D MIN(bytes, BDRV_REQUEST_MAX_BYTES); + ret =3D bs->drv->bdrv_co_get_block_status(bs, offset >> BDRV_SECTOR_BI= TS, + bytes >> BDRV_SECTOR_BITS, &co= unt, &local_file); if (ret < 0) { *pnum =3D 0; goto out; } + *pnum =3D count * BDRV_SECTOR_SIZE; if (ret & BDRV_BLOCK_RAW) { assert(ret & BDRV_BLOCK_OFFSET_VALID && local_file); - ret =3D bdrv_co_get_block_status(local_file, mapping, - ret >> BDRV_SECTOR_BITS, - *pnum, pnum, &local_file); + ret =3D bdrv_co_block_status(local_file, mapping, + ret & BDRV_BLOCK_OFFSET_MASK, + *pnum, pnum, &local_file); + assert(QEMU_IS_ALIGNED(*pnum, BDRV_SECTOR_SIZE)); goto out; } @@ -1812,8 +1823,8 @@ static int64_t coroutine_fn bdrv_co_get_block_status(= BlockDriverState *bs, ret |=3D BDRV_BLOCK_ZERO; } else if (bs->backing) { BlockDriverState *bs2 =3D bs->backing->bs; - int64_t nb_sectors2 =3D bdrv_nb_sectors(bs2); - if (nb_sectors2 >=3D 0 && sector_num >=3D nb_sectors2) { + int64_t size2 =3D bdrv_getlength(bs2); + if (size2 >=3D 0 && offset >=3D size2) { ret |=3D BDRV_BLOCK_ZERO; } } @@ -1822,11 +1833,11 @@ static int64_t coroutine_fn bdrv_co_get_block_statu= s(BlockDriverState *bs, if (mapping && local_file && local_file !=3D bs && (ret & BDRV_BLOCK_DATA) && !(ret & BDRV_BLOCK_ZERO) && (ret & BDRV_BLOCK_OFFSET_VALID)) { - int file_pnum; + int64_t file_pnum; - ret2 =3D bdrv_co_get_block_status(local_file, mapping, - ret >> BDRV_SECTOR_BITS, - *pnum, &file_pnum, NULL); + ret2 =3D bdrv_co_block_status(local_file, mapping, + ret & BDRV_BLOCK_OFFSET_MASK, + *pnum, &file_pnum, NULL); if (ret2 >=3D 0) { /* Ignore errors. This is just providing extra information, it * is useful but not necessary. @@ -1852,7 +1863,7 @@ out: *file =3D local_file; } bdrv_dec_in_flight(bs); - if (ret >=3D 0 && sector_num + *pnum =3D=3D total_sectors) { + if (ret >=3D 0 && offset + *pnum =3D=3D total_size) { ret |=3D BDRV_BLOCK_EOF; } return ret; @@ -1872,11 +1883,17 @@ static int64_t coroutine_fn bdrv_co_get_block_statu= s_above(BlockDriverState *bs, assert(bs !=3D base); for (p =3D bs; p !=3D base; p =3D backing_bs(p)) { - ret =3D bdrv_co_get_block_status(p, mapping, sector_num, nb_sector= s, - pnum, file); + int64_t count; + + ret =3D bdrv_co_block_status(p, mapping, + sector_num * BDRV_SECTOR_SIZE, + nb_sectors * BDRV_SECTOR_SIZE, &count, + file); if (ret < 0) { break; } + assert(QEMU_IS_ALIGNED(count, BDRV_SECTOR_SIZE)); + *pnum =3D count >> BDRV_SECTOR_BITS; if (ret & BDRV_BLOCK_ZERO && ret & BDRV_BLOCK_EOF && !first) { /* * Reading beyond the end of the file continues to read --=20 2.9.4