From nobody Wed May 1 03:15:31 2024 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@gnu.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@gnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1506801343559385.34093681991465; Sat, 30 Sep 2017 12:55:43 -0700 (PDT) Received: from localhost ([::1]:40240 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dyNrS-0008Aq-Sz for importer@patchew.org; Sat, 30 Sep 2017 15:55:42 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:55746) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dyNpl-00075d-9q for qemu-devel@nongnu.org; Sat, 30 Sep 2017 15:53:59 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dyNpk-00073Z-Bx for qemu-devel@nongnu.org; Sat, 30 Sep 2017 15:53:57 -0400 Received: from mx1.redhat.com ([209.132.183.28]:34092) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dyNpg-000704-Ej; Sat, 30 Sep 2017 15:53:52 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 759B9356E8; Sat, 30 Sep 2017 19:53:51 +0000 (UTC) Received: from red.redhat.com (ovpn-116-51.phx2.redhat.com [10.3.116.51]) by smtp.corp.redhat.com (Postfix) with ESMTP id EEA548B547; Sat, 30 Sep 2017 19:53:50 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 759B9356E8 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: Sat, 30 Sep 2017 14:53:42 -0500 Message-Id: <20170930195345.23338-2-eblake@redhat.com> In-Reply-To: <20170930195345.23338-1-eblake@redhat.com> References: <20170930195345.23338-1-eblake@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.30]); Sat, 30 Sep 2017 19:53:51 +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 1/4] qemu-io: Add -C for opening with copy-on-read X-BeenThere: qemu-devel@gnu.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, jsnow@redhat.com, qemu-block@nongnu.org, Max Reitz Errors-To: qemu-devel-bounces+importer=patchew.org@gnu.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" Make it easier to enable copy-on-read during iotests, by exposing a new bool option to main and open. Signed-off-by: Eric Blake Reviewed-by: Jeff Cody Reviewed-by: John Snow Reviewed-by: Kevin Wolf Reviewed-by: Stefan Hajnoczi --- qemu-io.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/qemu-io.c b/qemu-io.c index 265445ad89..c70bde3eb1 100644 --- a/qemu-io.c +++ b/qemu-io.c @@ -102,6 +102,7 @@ static void open_help(void) " Opens a file for subsequent use by all of the other qemu-io commands.\n" " -r, -- open file read-only\n" " -s, -- use snapshot file\n" +" -C, -- use copy-on-read\n" " -n, -- disable host cache, short for -t none\n" " -U, -- force shared permissions\n" " -k, -- use kernel AIO implementation (on Linux only)\n" @@ -120,7 +121,7 @@ static const cmdinfo_t open_cmd =3D { .argmin =3D 1, .argmax =3D -1, .flags =3D CMD_NOFILE_OK, - .args =3D "[-rsnkU] [-t cache] [-d discard] [-o options] [path]", + .args =3D "[-rsCnkU] [-t cache] [-d discard] [-o options] [path]= ", .oneline =3D "open the file specified by path", .help =3D open_help, }; @@ -145,7 +146,7 @@ static int open_f(BlockBackend *blk, int argc, char **a= rgv) QDict *opts; bool force_share =3D false; - while ((c =3D getopt(argc, argv, "snro:kt:d:U")) !=3D -1) { + while ((c =3D getopt(argc, argv, "snCro:kt:d:U")) !=3D -1) { switch (c) { case 's': flags |=3D BDRV_O_SNAPSHOT; @@ -154,6 +155,9 @@ static int open_f(BlockBackend *blk, int argc, char **a= rgv) flags |=3D BDRV_O_NOCACHE; writethrough =3D false; break; + case 'C': + flags |=3D BDRV_O_COPY_ON_READ; + break; case 'r': readonly =3D 1; break; @@ -251,6 +255,7 @@ static void usage(const char *name) " -r, --read-only export read-only\n" " -s, --snapshot use snapshot file\n" " -n, --nocache disable host cache, short for -t none\n" +" -C, --copy-on-read enable copy-on-read\n" " -m, --misalign misalign allocations for O_DIRECT\n" " -k, --native-aio use kernel AIO implementation (on Linux only)\n" " -t, --cache=3DMODE use the given cache mode for the image\n" @@ -439,7 +444,7 @@ static QemuOptsList file_opts =3D { int main(int argc, char **argv) { int readonly =3D 0; - const char *sopt =3D "hVc:d:f:rsnmkt:T:U"; + const char *sopt =3D "hVc:d:f:rsnCmkt:T:U"; const struct option lopt[] =3D { { "help", no_argument, NULL, 'h' }, { "version", no_argument, NULL, 'V' }, @@ -448,6 +453,7 @@ int main(int argc, char **argv) { "read-only", no_argument, NULL, 'r' }, { "snapshot", no_argument, NULL, 's' }, { "nocache", no_argument, NULL, 'n' }, + { "copy-on-read", no_argument, NULL, 'C' }, { "misalign", no_argument, NULL, 'm' }, { "native-aio", no_argument, NULL, 'k' }, { "discard", required_argument, NULL, 'd' }, @@ -492,6 +498,9 @@ int main(int argc, char **argv) flags |=3D BDRV_O_NOCACHE; writethrough =3D false; break; + case 'C': + flags |=3D BDRV_O_COPY_ON_READ; + break; case 'd': if (bdrv_parse_discard_flags(optarg, &flags) < 0) { error_report("Invalid discard option: %s", optarg); --=20 2.13.6 From nobody Wed May 1 03:15:31 2024 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@gnu.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@gnu.org Return-Path: Received: from lists.gnu.org (208.118.235.17 [208.118.235.17]) by mx.zohomail.com with SMTPS id 1506801443700434.1430543201568; Sat, 30 Sep 2017 12:57:23 -0700 (PDT) Received: from localhost ([::1]:40247 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dyNsz-0000uJ-Ov for importer@patchew.org; Sat, 30 Sep 2017 15:57:17 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:55977) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dyNpt-0007Ay-Bl for qemu-devel@nongnu.org; Sat, 30 Sep 2017 15:54:10 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dyNpo-00077J-WD for qemu-devel@nongnu.org; Sat, 30 Sep 2017 15:54:05 -0400 Received: from mx1.redhat.com ([209.132.183.28]:36964) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dyNpm-00074x-Gp; Sat, 30 Sep 2017 15:53:58 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 6D0B4C0587E6; Sat, 30 Sep 2017 19:53:57 +0000 (UTC) Received: from red.redhat.com (ovpn-116-51.phx2.redhat.com [10.3.116.51]) by smtp.corp.redhat.com (Postfix) with ESMTP id 9B3E317C5A; Sat, 30 Sep 2017 19:53:51 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 6D0B4C0587E6 Authentication-Results: ext-mx08.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx08.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=eblake@redhat.com From: Eric Blake To: qemu-devel@nongnu.org Date: Sat, 30 Sep 2017 14:53:43 -0500 Message-Id: <20170930195345.23338-3-eblake@redhat.com> In-Reply-To: <20170930195345.23338-1-eblake@redhat.com> References: <20170930195345.23338-1-eblake@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.32]); Sat, 30 Sep 2017 19:53:57 +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 2/4] block: Add blkdebug hook for copy-on-read X-BeenThere: qemu-devel@gnu.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, Fam Zheng , qemu-block@nongnu.org, Markus Armbruster , Max Reitz , Stefan Hajnoczi , jsnow@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@gnu.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" Make it possible to inject errors on writes performed during a read operation due to copy-on-read semantics. Signed-off-by: Eric Blake Reviewed-by: Jeff Cody Reviewed-by: John Snow Reviewed-by: Kevin Wolf Reviewed-by: Stefan Hajnoczi --- qapi/block-core.json | 5 ++++- block/io.c | 1 + 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/qapi/block-core.json b/qapi/block-core.json index 750bb0c77c..ab96e348e6 100644 --- a/qapi/block-core.json +++ b/qapi/block-core.json @@ -2538,6 +2538,8 @@ # # @l1_shrink_free_l2_clusters: discard the l2 tables. (since 2.11) # +# @cor_write: a write due to copy-on-read (since 2.11) +# # Since: 2.9 ## { 'enum': 'BlkdebugEvent', 'prefix': 'BLKDBG', @@ -2555,7 +2557,8 @@ 'flush_to_disk', 'pwritev_rmw_head', 'pwritev_rmw_after_head', 'pwritev_rmw_tail', 'pwritev_rmw_after_tail', 'pwritev', 'pwritev_zero', 'pwritev_done', 'empty_image_prepare', - 'l1_shrink_write_table', 'l1_shrink_free_l2_clusters' ] } + 'l1_shrink_write_table', 'l1_shrink_free_l2_clusters', + 'cor_write'] } ## # @BlkdebugInjectErrorOptions: diff --git a/block/io.c b/block/io.c index d633b0f851..97d9fa655d 100644 --- a/block/io.c +++ b/block/io.c @@ -983,6 +983,7 @@ static int coroutine_fn bdrv_co_do_copy_on_readv(BdrvCh= ild *child, goto err; } + bdrv_debug_event(bs, BLKDBG_COR_WRITE); if (drv->bdrv_co_pwrite_zeroes && buffer_is_zero(bounce_buffer, iov.iov_len)) { /* FIXME: Should we (perhaps conditionally) be setting --=20 2.13.6 From nobody Wed May 1 03:15:31 2024 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@gnu.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@gnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 15068015089451021.9317272869706; Sat, 30 Sep 2017 12:58:28 -0700 (PDT) Received: from localhost ([::1]:40249 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dyNu7-0001Sg-9V for importer@patchew.org; Sat, 30 Sep 2017 15:58:27 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:56065) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dyNpw-0007FE-VP for qemu-devel@nongnu.org; Sat, 30 Sep 2017 15:54:11 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dyNpv-0007Ds-6E for qemu-devel@nongnu.org; Sat, 30 Sep 2017 15:54:08 -0400 Received: from mx1.redhat.com ([209.132.183.28]:36814) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dyNpo-00075z-2Z; Sat, 30 Sep 2017 15:54:00 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 0DB64883B8; Sat, 30 Sep 2017 19:53:59 +0000 (UTC) Received: from red.redhat.com (ovpn-116-51.phx2.redhat.com [10.3.116.51]) by smtp.corp.redhat.com (Postfix) with ESMTP id AE7EB6017C; Sat, 30 Sep 2017 19:53:57 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 0DB64883B8 Authentication-Results: ext-mx02.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx02.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=eblake@redhat.com From: Eric Blake To: qemu-devel@nongnu.org Date: Sat, 30 Sep 2017 14:53:44 -0500 Message-Id: <20170930195345.23338-4-eblake@redhat.com> In-Reply-To: <20170930195345.23338-1-eblake@redhat.com> References: <20170930195345.23338-1-eblake@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.26]); Sat, 30 Sep 2017 19:53:59 +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 3/4] block: Perform copy-on-read in loop X-BeenThere: qemu-devel@gnu.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, Fam Zheng , qemu-block@nongnu.org, qemu-stable@nongnu.org, Max Reitz , Stefan Hajnoczi , jsnow@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@gnu.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" Improve our braindead copy-on-read implementation. Pre-patch, we have multiple issues: - we create a bounce buffer and perform a write for the entire request, even if the active image already has 99% of the clusters occupied, and really only needs to copy-on-read the remaining 1% of the clusters - our bounce buffer was as large as the read request, and can needlessly exhaust our memory by using double the memory of the request size (the original request plus our bounce buffer), rather than a capped maximum overhead beyond the original - if a driver has a max_transfer limit, we are bypassing the normal code in bdrv_aligned_preadv() that fragments to that limit, and instead attempt to read the entire buffer from the driver in one go, which some drivers may assert on - a client can request a large request of nearly 2G such that rounding the request out to cluster boundaries results in a byte count larger than 2G. While this cannot exceed 32 bits, it DOES have some follow-on problems: -- the call to bdrv_driver_pread() can assert for exceeding BDRV_REQUEST_MAX_BYTES, if the driver is old and lacks .bdrv_co_preadv -- if the buffer is all zeroes, the subsequent call to bdrv_co_do_pwrite_zeroes is a no-op due to a negative size, which means we did not actually copy on read Fix all of these issues by breaking up the action into a loop, where each iteration is capped to sane limits. Also, querying the allocation status allows us to optimize: when data is already present in the active layer, we don't need to bounce. Note that the code has a telling comment that copy-on-read should probably be a filter driver rather than a bolt-in hack in io.c; but that remains a task for another day. CC: qemu-stable@nongnu.org Signed-off-by: Eric Blake --- block/io.c | 119 +++++++++++++++++++++++++++++++++++++++++----------------= ---- 1 file changed, 81 insertions(+), 38 deletions(-) diff --git a/block/io.c b/block/io.c index 97d9fa655d..5ef5adc7a7 100644 --- a/block/io.c +++ b/block/io.c @@ -34,6 +34,9 @@ #define NOT_DONE 0x7fffffff /* used while emulated sync operation in progr= ess */ +/* Maximum bounce buffer for copy-on-read and write zeroes, in bytes */ +#define MAX_BOUNCE_BUFFER (32768 << BDRV_SECTOR_BITS) + static int coroutine_fn bdrv_co_do_pwrite_zeroes(BlockDriverState *bs, int64_t offset, int bytes, BdrvRequestFlags flags); @@ -945,11 +948,14 @@ static int coroutine_fn bdrv_co_do_copy_on_readv(Bdrv= Child *child, BlockDriver *drv =3D bs->drv; struct iovec iov; - QEMUIOVector bounce_qiov; + QEMUIOVector local_qiov; int64_t cluster_offset; unsigned int cluster_bytes; size_t skip_bytes; int ret; + int max_transfer =3D MIN_NON_ZERO(bs->bl.max_transfer, + BDRV_REQUEST_MAX_BYTES); + unsigned int progress =3D 0; /* FIXME We cannot require callers to have write permissions when all = they * are doing is a read request. If we did things right, write permissi= ons @@ -961,53 +967,94 @@ static int coroutine_fn bdrv_co_do_copy_on_readv(Bdrv= Child *child, // assert(child->perm & (BLK_PERM_WRITE_UNCHANGED | BLK_PERM_WRITE)); /* Cover entire cluster so no additional backing file I/O is required = when - * allocating cluster in the image file. + * allocating cluster in the image file. Note that this value may exc= eed + * BDRV_REQUEST_MAX_BYTES (even when the original read did not), which + * is one reason we loop rather than doing it all at once. */ bdrv_round_to_clusters(bs, offset, bytes, &cluster_offset, &cluster_by= tes); + skip_bytes =3D offset - cluster_offset; trace_bdrv_co_do_copy_on_readv(bs, offset, bytes, cluster_offset, cluster_bytes); - iov.iov_len =3D cluster_bytes; - iov.iov_base =3D bounce_buffer =3D qemu_try_blockalign(bs, iov.iov_len= ); + bounce_buffer =3D qemu_try_blockalign(bs, + MIN(MIN(max_transfer, cluster_byte= s), + MAX_BOUNCE_BUFFER)); if (bounce_buffer =3D=3D NULL) { ret =3D -ENOMEM; goto err; } - qemu_iovec_init_external(&bounce_qiov, &iov, 1); + while (cluster_bytes) { + int64_t pnum; - ret =3D bdrv_driver_preadv(bs, cluster_offset, cluster_bytes, - &bounce_qiov, 0); - if (ret < 0) { - goto err; - } + ret =3D bdrv_is_allocated(bs, cluster_offset, + MIN(cluster_bytes, max_transfer), &pnum); + if (ret < 0) { + /* Safe to treat errors in querying allocation as if + * unallocated; we'll probably fail again soon on the + * read, but at least that will set a decent errno. + */ + pnum =3D MIN(cluster_bytes, max_transfer); + } - bdrv_debug_event(bs, BLKDBG_COR_WRITE); - if (drv->bdrv_co_pwrite_zeroes && - buffer_is_zero(bounce_buffer, iov.iov_len)) { - /* FIXME: Should we (perhaps conditionally) be setting - * BDRV_REQ_MAY_UNMAP, if it will allow for a sparser copy - * that still correctly reads as zero? */ - ret =3D bdrv_co_do_pwrite_zeroes(bs, cluster_offset, cluster_bytes= , 0); - } else { - /* This does not change the data on the disk, it is not necessary - * to flush even in cache=3Dwritethrough mode. - */ - ret =3D bdrv_driver_pwritev(bs, cluster_offset, cluster_bytes, - &bounce_qiov, 0); - } + assert(skip_bytes < pnum); - if (ret < 0) { - /* It might be okay to ignore write errors for guest requests. If= this - * is a deliberate copy-on-read then we don't want to ignore the e= rror. - * Simply report it in all cases. - */ - goto err; - } + if (ret <=3D 0) { + /* Must copy-on-read; use the bounce buffer */ + iov.iov_base =3D bounce_buffer; + iov.iov_len =3D pnum =3D MIN(pnum, MAX_BOUNCE_BUFFER); + qemu_iovec_init_external(&local_qiov, &iov, 1); + + ret =3D bdrv_driver_preadv(bs, cluster_offset, pnum, + &local_qiov, 0); + if (ret < 0) { + goto err; + } - skip_bytes =3D offset - cluster_offset; - qemu_iovec_from_buf(qiov, 0, bounce_buffer + skip_bytes, bytes); + bdrv_debug_event(bs, BLKDBG_COR_WRITE); + if (drv->bdrv_co_pwrite_zeroes && + buffer_is_zero(bounce_buffer, pnum)) { + /* FIXME: Should we (perhaps conditionally) be setting + * BDRV_REQ_MAY_UNMAP, if it will allow for a sparser copy + * that still correctly reads as zero? */ + ret =3D bdrv_co_do_pwrite_zeroes(bs, cluster_offset, pnum,= 0); + } else { + /* This does not change the data on the disk, it is not + * necessary to flush even in cache=3Dwritethrough mode. + */ + ret =3D bdrv_driver_pwritev(bs, cluster_offset, pnum, + &local_qiov, 0); + } + + if (ret < 0) { + /* It might be okay to ignore write errors for guest + * requests. If this is a deliberate copy-on-read + * then we don't want to ignore the error. Simply + * report it in all cases. + */ + goto err; + } + + qemu_iovec_from_buf(qiov, progress, bounce_buffer + skip_bytes, + pnum - skip_bytes); + } else { + /* Read directly into the destination */ + qemu_iovec_init(&local_qiov, qiov->niov); + qemu_iovec_concat(&local_qiov, qiov, progress, pnum - skip_byt= es); + ret =3D bdrv_driver_preadv(bs, offset + progress, local_qiov.s= ize, + &local_qiov, 0); + qemu_iovec_destroy(&local_qiov); + if (ret < 0) { + goto err; + } + } + + cluster_offset +=3D pnum; + cluster_bytes -=3D pnum; + progress +=3D pnum - skip_bytes; + skip_bytes =3D 0; + } err: qemu_vfree(bounce_buffer); @@ -1213,9 +1260,6 @@ int coroutine_fn bdrv_co_readv(BdrvChild *child, int6= 4_t sector_num, return bdrv_co_do_readv(child, sector_num, nb_sectors, qiov, 0); } -/* Maximum buffer for write zeroes fallback, in bytes */ -#define MAX_WRITE_ZEROES_BOUNCE_BUFFER (32768 << BDRV_SECTOR_BITS) - static int coroutine_fn bdrv_co_do_pwrite_zeroes(BlockDriverState *bs, int64_t offset, int bytes, BdrvRequestFlags flags) { @@ -1230,8 +1274,7 @@ static int coroutine_fn bdrv_co_do_pwrite_zeroes(Bloc= kDriverState *bs, int max_write_zeroes =3D MIN_NON_ZERO(bs->bl.max_pwrite_zeroes, INT_MA= X); int alignment =3D MAX(bs->bl.pwrite_zeroes_alignment, bs->bl.request_alignment); - int max_transfer =3D MIN_NON_ZERO(bs->bl.max_transfer, - MAX_WRITE_ZEROES_BOUNCE_BUFFER); + int max_transfer =3D MIN_NON_ZERO(bs->bl.max_transfer, MAX_BOUNCE_BUFF= ER); assert(alignment % bs->bl.request_alignment =3D=3D 0); head =3D offset % alignment; --=20 2.13.6 From nobody Wed May 1 03:15:31 2024 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@gnu.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@gnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1506802364680706.937294788079; Sat, 30 Sep 2017 13:12:44 -0700 (PDT) Received: from localhost ([::1]:40283 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dyO7s-0004EM-JL for importer@patchew.org; Sat, 30 Sep 2017 16:12:40 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:44608) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dyO6v-0003pL-8u for qemu-devel@nongnu.org; Sat, 30 Sep 2017 16:11:44 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dyO6t-0005gj-Nm for qemu-devel@nongnu.org; Sat, 30 Sep 2017 16:11:41 -0400 Received: from mx1.redhat.com ([209.132.183.28]:38744) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dyO6n-0005bU-Qj; Sat, 30 Sep 2017 16:11:34 -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 9A8D74E049; Sat, 30 Sep 2017 20:11:31 +0000 (UTC) Received: from red.redhat.com (ovpn-116-73.phx2.redhat.com [10.3.116.73]) by smtp.corp.redhat.com (Postfix) with ESMTP id 0B1CB17BB6; Sat, 30 Sep 2017 20:11:28 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 9A8D74E049 Authentication-Results: ext-mx09.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx09.extmail.prod.ext.phx2.redhat.com; spf=fail smtp.mailfrom=eblake@redhat.com From: Eric Blake To: qemu-devel@nongnu.org Date: Sat, 30 Sep 2017 15:11:21 -0500 Message-Id: <20170930201121.24157-1-eblake@redhat.com> In-Reply-To: <20170930195345.23338-1-eblake@redhat.com> References: <20170930195345.23338-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.38]); Sat, 30 Sep 2017 20:11:31 +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 4/4] iotests: Add test 197 for covering copy-on-read X-BeenThere: qemu-devel@gnu.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, jsnow@redhat.com, qemu-block@nongnu.org, Max Reitz Errors-To: qemu-devel-bounces+importer=patchew.org@gnu.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" Add a test for qcow2 copy-on-read behavior, including exposure for the just-fixed bugs. The copy-on-read behavior is always to a qcow2 image, but the test is careful to allow running with most image protocol/format combos as the backing file being copied from (luks being the exception, as it is harder to pass the right secret to all the right places). In fact, for './check nbd', this appears to be the first time we've had a qcow2 image wrapping NBD, requiring an additional line in _filter_img_create to match the similar line in _filter_img_info. Signed-off-by: Eric Blake --- I only tested with -raw, -qcow2, -qed, and -nbd. I won't be surprised if the test fails in some other setup... --- tests/qemu-iotests/common.filter | 1 + tests/qemu-iotests/197 | 93 ++++++++++++++++++++++++++++++++++++= ++++ tests/qemu-iotests/197.out | 24 +++++++++++ tests/qemu-iotests/group | 1 + 4 files changed, 119 insertions(+) create mode 100755 tests/qemu-iotests/197 create mode 100644 tests/qemu-iotests/197.out diff --git a/tests/qemu-iotests/common.filter b/tests/qemu-iotests/common.f= ilter index 9d5442ecd9..227b37e941 100644 --- a/tests/qemu-iotests/common.filter +++ b/tests/qemu-iotests/common.filter @@ -111,6 +111,7 @@ _filter_img_create() sed -e "s#$IMGPROTO:$TEST_DIR#TEST_DIR#g" \ -e "s#$TEST_DIR#TEST_DIR#g" \ -e "s#$IMGFMT#IMGFMT#g" \ + -e 's#nbd:127.0.0.1:10810#TEST_DIR/t.IMGFMT#g' \ -e "s# encryption=3Doff##g" \ -e "s# cluster_size=3D[0-9]\\+##g" \ -e "s# table_size=3D[0-9]\\+##g" \ diff --git a/tests/qemu-iotests/197 b/tests/qemu-iotests/197 new file mode 100755 index 0000000000..8463e0d1cc --- /dev/null +++ b/tests/qemu-iotests/197 @@ -0,0 +1,93 @@ +#!/bin/bash +# +# Test case for copy-on-read into qcow2 +# +# Copyright (C) 2017 Red Hat, Inc. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# + +# creator +owner=3Deblake@redhat.com + +seq=3D"$(basename $0)" +echo "QA output created by $seq" + +here=3D"$PWD" +status=3D1 # failure is the default! + +TEST_WRAP=3D"$TEST_DIR/t.wrap.qcow2" +BLKDBG_CONF=3D"$TEST_DIR/blkdebug.conf" + +_cleanup() +{ + _cleanup_test_img + rm -f "$BLKDBG_CONF" +} +trap "_cleanup; exit \$status" 0 1 2 3 15 + +# get standard environment, filters and checks +. ./common.rc +. ./common.filter + +# Test is supported for any backing file; but we force qcow2 for our wrapp= er. +_supported_fmt generic +_supported_proto generic +_supported_os Linux +# LUKS support may be possible, but it complicates things. +_unsupported_fmt luks + +echo +echo '=3D=3D=3D Copy-on-read =3D=3D=3D' +echo + +# Prep the images +_make_test_img 4G +$QEMU_IO -c "write -P 55 3G 1k" "$TEST_IMG" | _filter_qemu_io +IMGPROTO=3Dfile IMGFMT=3Dqcow2 IMGOPTS=3D TEST_IMG_FILE=3D"$TEST_WRAP" \ + _make_test_img -F "$IMGFMT" -b "$TEST_IMG" | _filter_img_create +$QEMU_IO -f qcow2 -c "write -z -u 1M 64k" "$TEST_WRAP" | _filter_qemu_io + +# Ensure that a read of two clusters, but where one is already allocated, +# does not re-write the allocated cluster +cat > "$BLKDBG_CONF" <&1 | _filter_testdir + +# Break the backing chain, and show that images are identical, and that +# we properly copied over explicit zeros. +$QEMU_IMG rebase -u -b "" -f qcow2 "$TEST_WRAP" +$QEMU_IO -f qcow2 -c map "$TEST_WRAP" +_check_test_img +$QEMU_IMG compare -f $IMGFMT -F qcow2 "$TEST_IMG" "$TEST_WRAP" + +# success, all done +echo '*** done' +status=3D0 diff --git a/tests/qemu-iotests/197.out b/tests/qemu-iotests/197.out new file mode 100644 index 0000000000..baaa6b69e4 --- /dev/null +++ b/tests/qemu-iotests/197.out @@ -0,0 +1,24 @@ +QA output created by 197 + +=3D=3D=3D Copy-on-read =3D=3D=3D + +Formatting 'TEST_DIR/t.IMGFMT', fmt=3DIMGFMT size=3D4294967296 +wrote 1024/1024 bytes at offset 3221225472 +1 KiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) +Formatting 'TEST_DIR/t.wrap.IMGFMT', fmt=3DIMGFMT size=3D4294967296 backin= g_file=3DTEST_DIR/t.IMGFMT backing_fmt=3DIMGFMT +wrote 65536/65536 bytes at offset 1048576 +64 KiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) +read 131072/131072 bytes at offset 1048576 +128 KiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) +read 2147483136/2147483136 bytes at offset 1024 +2 GiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) +read 1024/1024 bytes at offset 3221226496 +1 KiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) +can't open device TEST_DIR/t.wrap.qcow2: Can't use copy-on-read on read-on= ly device +2 GiB (0x80010000) bytes allocated at offset 0 bytes (0x0) +1023.938 MiB (0x3fff0000) bytes not allocated at offset 2 GiB (0x80010000) +64 KiB (0x10000) bytes allocated at offset 3 GiB (0xc0000000) +1023.938 MiB (0x3fff0000) bytes not allocated at offset 3 GiB (0xc0010000) +No errors were found on the image. +Images are identical. +*** done diff --git a/tests/qemu-iotests/group b/tests/qemu-iotests/group index cdccee319e..7a302a67cf 100644 --- a/tests/qemu-iotests/group +++ b/tests/qemu-iotests/group @@ -191,3 +191,4 @@ 192 rw auto quick 194 rw auto migration quick 195 rw auto quick +197 rw auto quick --=20 2.13.6