From nobody Mon Feb 9 04:31:12 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=1599833471; cv=none; d=zohomail.com; s=zohoarc; b=STgEcdi8zwjJzkvzw006Wca28e/r7gjxbJOAPwXwA01jN+5DCtn24RahLnUK5icEvOeu84KshnXtQc4JQLGO2NHdnYk0frRVC+YE6cBYluc501Bu/41mAHrT4HKh+TA3QzQ4+oQ2JJzKdE4AdFRgGJ5rcRIERNavGvLdP0IFa1w= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1599833471; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=Uj8oGFGFid1keo97GpxzEqjju8VM0l2+7NhMncE/GrM=; b=ISTe9YAYspyGBr3Tjh4YIRhcwdgpEM6qQ/GXZ/TidK7BQ35/wNicu67OLtF56dO/OUDyIrEBX3egOUxcMiD5uV5Mq3U2q+FIRMjXW+dYHoBe71ATCW2Xu5PjrcAQh422Il9M/zwTygswQTCMoOSrLJ5MuC7JXqJN7gtXp24rinc= 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 1599833470530763.5807078064524; Fri, 11 Sep 2020 07:11:10 -0700 (PDT) Received: from localhost ([::1]:53684 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kGjld-0005Zq-4Z for importer@patchew.org; Fri, 11 Sep 2020 10:11:09 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:45376) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kGjka-0004OH-1V; Fri, 11 Sep 2020 10:10:04 -0400 Received: from fanzine.igalia.com ([178.60.130.6]:50976) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1kGjkW-0000h9-Kw; Fri, 11 Sep 2020 10:10:03 -0400 Received: from [81.0.39.67] (helo=perseus.local) by fanzine.igalia.com with esmtpsa (Cipher TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim) id 1kGjkR-0004ol-Su; Fri, 11 Sep 2020 16:09:55 +0200 Received: from berto by perseus.local with local (Exim 4.92) (envelope-from ) id 1kGjkF-0000zu-2E; Fri, 11 Sep 2020 16:09:43 +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:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From; bh=Uj8oGFGFid1keo97GpxzEqjju8VM0l2+7NhMncE/GrM=; b=AsPPcU4mGQMmt3hBprEZemf94oHY08xaCIuTf/b5CNxRyjiR5sX3OfYG4Pyo6swmfLxy6LaqUKv/OWrYzsd39fW9k/W64L70DoNOX92KBAJ7ZFCp1yS3k2tVKdldtf/LBxuNK4ZKidYpfEQJxnp3TMUuu0muiO0PuDubpn33yB7+qXYjXBBZuiGOLBYSYcs5YRMj71lOf/TIIzAJbqcPjpTM135rMoWZJdDXi7LL8zubvdAGGwwXkD9n7fg7karsvDnL4IcdFsF1TKehblvApe7tdXK//BgrNGxMauIONqU9Xt67uldPXCMmK2HpW3QDGHdCs1ODFGa8YES2Y8K3Hw==; From: Alberto Garcia To: qemu-devel@nongnu.org Subject: [PATCH 1/2] qcow2: Make preallocate_co() resize the image to the correct size Date: Fri, 11 Sep 2020 16:09:41 +0200 Message-Id: X-Mailer: git-send-email 2.20.1 In-Reply-To: References: 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/09/11 10:09:56 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 autolearn=ham autolearn_force=no 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" This function preallocates metadata structures and then extends the image to its new size, but that new size calculation is wrong because it doesn't take into account that the host_offset variable is always cluster-aligned. This problem can be reproduced with preallocation=3Dmetadata when the original size is not cluster-aligned but the new size is. In this case the final image size will be shorter than expected. qemu-img create -f qcow2 img.qcow2 31k qemu-img resize --preallocation=3Dmetadata img.qcow2 128k Signed-off-by: Alberto Garcia Reviewed-by: Max Reitz --- block/qcow2.c | 1 + tests/qemu-iotests/125 | 40 +++++++++++++++++++++----------------- tests/qemu-iotests/125.out | 28 ++++++++++++++++++++++++-- 3 files changed, 49 insertions(+), 20 deletions(-) diff --git a/block/qcow2.c b/block/qcow2.c index 77c43ce178..1cb5daf39a 100644 --- a/block/qcow2.c +++ b/block/qcow2.c @@ -3135,6 +3135,7 @@ static int coroutine_fn preallocate_co(BlockDriverSta= te *bs, uint64_t offset, error_setg_errno(errp, -ret, "Allocating clusters failed"); goto out; } + host_offset +=3D offset_into_cluster(s, offset); =20 for (m =3D meta; m !=3D NULL; m =3D m->next) { m->prealloc =3D true; diff --git a/tests/qemu-iotests/125 b/tests/qemu-iotests/125 index 7cb1c19730..1f35598b2b 100755 --- a/tests/qemu-iotests/125 +++ b/tests/qemu-iotests/125 @@ -168,24 +168,28 @@ done $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 + for dst_size in 96k 128k; do + for prealloc in metadata full; do + echo "--- Resizing image from $orig_size to $dst_size (preallo= cation=3D$prealloc) ---" + _make_test_img -F raw -b "$TEST_IMG.base" -o cluster_size=3D64= k "$orig_size" + $QEMU_IMG resize -f "$IMGFMT" --preallocation=3D"$prealloc" "$= TEST_IMG" "$dst_size" + # The first part of the image should contain data from the bac= king 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 clusters + 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 $expec= ted_file_length)" + fi + fi + echo + done + done done =20 # success, all done diff --git a/tests/qemu-iotests/125.out b/tests/qemu-iotests/125.out index 7f76f7af20..63a6e9e8a9 100644 --- a/tests/qemu-iotests/125.out +++ b/tests/qemu-iotests/125.out @@ -768,11 +768,35 @@ 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 --- +--- Resizing image from 31k to 96k (preallocation=3Dmetadata) --- Formatting 'TEST_DIR/t.IMGFMT', fmt=3DIMGFMT size=3D31744 backing_file=3DT= EST_DIR/t.IMGFMT.base backing_fmt=3Draw Image resized. =20 ---- Resizing image from 33k to 96k --- +--- Resizing image from 31k to 96k (preallocation=3Dfull) --- +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 31k to 128k (preallocation=3Dmetadata) --- +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 31k to 128k (preallocation=3Dfull) --- +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 (preallocation=3Dmetadata) --- +Formatting 'TEST_DIR/t.IMGFMT', fmt=3DIMGFMT size=3D33792 backing_file=3DT= EST_DIR/t.IMGFMT.base backing_fmt=3Draw +Image resized. + +--- Resizing image from 33k to 96k (preallocation=3Dfull) --- +Formatting 'TEST_DIR/t.IMGFMT', fmt=3DIMGFMT size=3D33792 backing_file=3DT= EST_DIR/t.IMGFMT.base backing_fmt=3Draw +Image resized. + +--- Resizing image from 33k to 128k (preallocation=3Dmetadata) --- +Formatting 'TEST_DIR/t.IMGFMT', fmt=3DIMGFMT size=3D33792 backing_file=3DT= EST_DIR/t.IMGFMT.base backing_fmt=3Draw +Image resized. + +--- Resizing image from 33k to 128k (preallocation=3Dfull) --- Formatting 'TEST_DIR/t.IMGFMT', fmt=3DIMGFMT size=3D33792 backing_file=3DT= EST_DIR/t.IMGFMT.base backing_fmt=3Draw Image resized. =20 --=20 2.20.1