From nobody Mon Feb 9 02:51:52 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1592402632; cv=none; d=zohomail.com; s=zohoarc; b=VcFE0biu+sExx5g0nrRdTfZt5k8PX535229UPQlBdgDucvLkjzLmeNoS2o7AxzXHrsL2ceXOlUuWFBRKC2OTmXmKwhSNT1sXOdkheCRE9E6RKykhrMF6UD89B8onzQvkvgu5VJLaOvW0+rjcWwinV2VDF1eSjCbGdNxwFXNgrB4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1592402632; h=Content-Transfer-Encoding:Cc:Date:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:To; bh=TmErKO0VZu/gix+Nq8uoZFpa4F1ZP0HInm6Howj4D6c=; b=hPYj8N5HQnmzGVSupDiAX8XIG9Wd10VzzZWS/lH0hQaX28dZ0wWswCXSZxAeH91Js26HCkJcKOvEO+Z921wvKXgWSBPwnb4GQzgzvzSi0QGI/LuKnHpKoVtlIRgLrD9FcVuB2c6F063BnGt19IKbfvKB8hVJ8hQSspad+8X+S1o= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1592402632560979.9241215346299; Wed, 17 Jun 2020 07:03:52 -0700 (PDT) Received: from localhost ([::1]:51752 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jlYer-0005En-03 for importer@patchew.org; Wed, 17 Jun 2020 10:03:17 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:59516) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jlYd0-000426-BM; Wed, 17 Jun 2020 10:01:22 -0400 Received: from fanzine.igalia.com ([178.60.130.6]:60824) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1jlYcx-0001Ya-Ir; Wed, 17 Jun 2020 10:01:22 -0400 Received: from [81.0.49.0] (helo=perseus.local) by fanzine.igalia.com with esmtpsa (Cipher TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim) id 1jlYcY-0001N8-Dk; Wed, 17 Jun 2020 16:00:54 +0200 Received: from berto by perseus.local with local (Exim 4.92) (envelope-from ) id 1jlYcK-0005IM-DL; Wed, 17 Jun 2020 16:00:40 +0200 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=igalia.com; s=20170329; h=Content-Transfer-Encoding:MIME-Version:Message-Id:Date:Subject:Cc:To:From; bh=TmErKO0VZu/gix+Nq8uoZFpa4F1ZP0HInm6Howj4D6c=; b=P6hlejJBRV+Gc5NW54KZORNYhDZnLwNj/OCCgh2PzGLR79RRoXqjhj4GDpKl52HMlr/WY8JAwhB+1KKn8DyxxX6NGPNgr6WRagvCGsVXdBlaIGNCoF3XX2MDnU/xB18cSu5vlQ0GTWirQTq5nyl9XvvrfYCLovzsvrOgKd7bVxYev1KfdoKquyej/UiW6M6TalEtKj5mKc4hUyeHhZ6ZuwqiD5BREOlBObAVmKZtipEdt+fWBedOOirDBNXlB7FFUNlzjwm8fNpcwbTcAZL1vJzLHVXYsndeEaNviLkYkKXtfmUlhp7xU6KQL+kJM7Kq7zCUEjqq1gWrRGeEbEes5w==; From: Alberto Garcia To: qemu-devel@nongnu.org Subject: [PATCH v2] qcow2: Fix preallocation on images with unaligned sizes Date: Wed, 17 Jun 2020 16:00:36 +0200 Message-Id: <20200617140036.20311-1-berto@igalia.com> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=178.60.130.6; envelope-from=berto@igalia.com; helo=fanzine.igalia.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/06/17 10:00:55 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x (no timestamps) [generic] [fuzzy] X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, URIBL_BLOCKED=0.001 autolearn=_AUTOLEARN X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , Alberto Garcia , qemu-block@nongnu.org, Max Reitz Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" When resizing an image with qcow2_co_truncate() using the falloc or full preallocation modes the code assumes that both the old and new sizes are cluster-aligned. There are two problems with this: 1) The calculation of how many clusters are involved does not always get the right result. Example: creating a 60KB image and resizing it (with preallocation=3Dfull) to 80KB won't allocate the second cluster. 2) No copy-on-write is performed, so in the previous example if there is a backing file then the first 60KB of the first cluster won't be filled with data from the backing file. This patch fixes both issues. Signed-off-by: Alberto Garcia Reviewed-by: Eric Blake --- v2: iotests: don't check the image size if data_file is set [Max] block/qcow2.c | 17 ++++++++++++++--- tests/qemu-iotests/125 | 24 ++++++++++++++++++++++++ tests/qemu-iotests/125.out | 9 +++++++++ 3 files changed, 47 insertions(+), 3 deletions(-) diff --git a/block/qcow2.c b/block/qcow2.c index 0cd2e6757e..e20590c3b7 100644 --- a/block/qcow2.c +++ b/block/qcow2.c @@ -4239,8 +4239,8 @@ static int coroutine_fn qcow2_co_truncate(BlockDriver= State *bs, int64_t offset, old_file_size =3D ROUND_UP(old_file_size, s->cluster_size); } =20 - nb_new_data_clusters =3D DIV_ROUND_UP(offset - old_length, - s->cluster_size); + nb_new_data_clusters =3D (ROUND_UP(offset, s->cluster_size) - + start_of_cluster(s, old_length)) >> s->cluster_bits; =20 /* This is an overestimation; we will not actually allocate space = for * these in the file but just make sure the new refcount structure= s are @@ -4317,10 +4317,21 @@ static int coroutine_fn qcow2_co_truncate(BlockDriv= erState *bs, int64_t offset, int64_t nb_clusters =3D MIN( nb_new_data_clusters, s->l2_slice_size - offset_to_l2_slice_index(s, guest_offse= t)); - QCowL2Meta allocation =3D { + unsigned cow_start_length =3D offset_into_cluster(s, guest_off= set); + QCowL2Meta allocation; + guest_offset =3D start_of_cluster(s, guest_offset); + allocation =3D (QCowL2Meta) { .offset =3D guest_offset, .alloc_offset =3D host_offset, .nb_clusters =3D nb_clusters, + .cow_start =3D { + .offset =3D 0, + .nb_bytes =3D cow_start_length, + }, + .cow_end =3D { + .offset =3D nb_clusters << s->cluster_bits, + .nb_bytes =3D 0, + }, }; qemu_co_queue_init(&allocation.dependent_requests); =20 diff --git a/tests/qemu-iotests/125 b/tests/qemu-iotests/125 index d510984045..7cb1c19730 100755 --- a/tests/qemu-iotests/125 +++ b/tests/qemu-iotests/125 @@ -164,6 +164,30 @@ for GROWTH_SIZE in 16 48 80; do done done =20 +# Test image resizing using preallocation and unaligned offsets +$QEMU_IMG create -f raw "$TEST_IMG.base" 128k | _filter_img_create +$QEMU_IO -c 'write -q -P 1 0 128k' -f raw "$TEST_IMG.base" +for orig_size in 31k 33k; do + echo "--- Resizing image from $orig_size to 96k ---" + _make_test_img -F raw -b "$TEST_IMG.base" -o cluster_size=3D64k "$orig= _size" + $QEMU_IMG resize -f "$IMGFMT" --preallocation=3Dfull "$TEST_IMG" 96k + # The first part of the image should contain data from the backing file + $QEMU_IO -c "read -q -P 1 0 ${orig_size}" "$TEST_IMG" + # The resized part of the image should contain zeroes + $QEMU_IO -c "read -q -P 0 ${orig_size} 63k" "$TEST_IMG" + # If the image does not have an external data file we can also verify = its + # actual size. The resized image should have 7 clusters: + # header, L1 table, L2 table, refcount table, refcount block, 2 data c= lusters + if ! _get_data_file "$TEST_IMG" > /dev/null; then + expected_file_length=3D$((65536 * 7)) + file_length=3D$(stat -c '%s' "$TEST_IMG_FILE") + if [ "$file_length" !=3D "$expected_file_length" ]; then + echo "ERROR: file length $file_length (expected $expected_file= _length)" + fi + fi + echo +done + # success, all done echo '*** done' rm -f $seq.full diff --git a/tests/qemu-iotests/125.out b/tests/qemu-iotests/125.out index 596905f533..7f76f7af20 100644 --- a/tests/qemu-iotests/125.out +++ b/tests/qemu-iotests/125.out @@ -767,4 +767,13 @@ wrote 2048000/2048000 bytes at offset 0 wrote 81920/81920 bytes at offset 2048000 80 KiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) =20 +Formatting 'TEST_DIR/t.IMGFMT.base', fmt=3Draw size=3D131072 +--- Resizing image from 31k to 96k --- +Formatting 'TEST_DIR/t.IMGFMT', fmt=3DIMGFMT size=3D31744 backing_file=3DT= EST_DIR/t.IMGFMT.base backing_fmt=3Draw +Image resized. + +--- Resizing image from 33k to 96k --- +Formatting 'TEST_DIR/t.IMGFMT', fmt=3DIMGFMT size=3D33792 backing_file=3DT= EST_DIR/t.IMGFMT.base backing_fmt=3Draw +Image resized. + *** done --=20 2.20.1