From nobody Sat Apr 27 23:14:18 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@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 (208.118.235.17 [208.118.235.17]) by mx.zohomail.com with SMTPS id 1510346439998417.7943525135772; Fri, 10 Nov 2017 12:40:39 -0800 (PST) Received: from localhost ([::1]:43425 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eDG6C-0008Cd-6M for importer@patchew.org; Fri, 10 Nov 2017 15:40:24 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:56912) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eDG4A-0006oP-5j for qemu-devel@nongnu.org; Fri, 10 Nov 2017 15:38:19 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eDG46-0007jT-Vp for qemu-devel@nongnu.org; Fri, 10 Nov 2017 15:38:18 -0500 Received: from mx1.redhat.com ([209.132.183.28]:50590) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1eDG43-0007hx-RE; Fri, 10 Nov 2017 15:38:12 -0500 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id EE3DC80467; Fri, 10 Nov 2017 20:38:10 +0000 (UTC) Received: from localhost (ovpn-204-126.brq.redhat.com [10.40.204.126]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 9D6E36269D; Fri, 10 Nov 2017 20:38:07 +0000 (UTC) From: Max Reitz To: qemu-block@nongnu.org Date: Fri, 10 Nov 2017 21:37:59 +0100 Message-Id: <20171110203759.14018-2-mreitz@redhat.com> In-Reply-To: <20171110203759.14018-1-mreitz@redhat.com> References: <20171110203759.14018-1-mreitz@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.28]); Fri, 10 Nov 2017 20:38:11 +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 for-2.12 1/1] qcow2: Repair unaligned preallocated zero clusters 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: Kevin Wolf , John Snow , Alberto Garcia , qemu-devel@nongnu.org, Max Reitz 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 can easily repair unaligned preallocated zero clusters by discarding them, so why not do it? Signed-off-by: Max Reitz Reviewed-by: Eric Blake --- block/qcow2-refcount.c | 70 ++++++++++++++++++++++++++++++++++++++----= ---- tests/qemu-iotests/060 | 3 +- tests/qemu-iotests/060.out | 9 ++++++ 3 files changed, 69 insertions(+), 13 deletions(-) diff --git a/block/qcow2-refcount.c b/block/qcow2-refcount.c index 3de1ab51ba..92701ab7af 100644 --- a/block/qcow2-refcount.c +++ b/block/qcow2-refcount.c @@ -1508,7 +1508,7 @@ enum { static int check_refcounts_l2(BlockDriverState *bs, BdrvCheckResult *res, void **refcount_table, int64_t *refcount_table_size, int64_t l2_off= set, - int flags) + int flags, BdrvCheckMode fix) { BDRVQcow2State *s =3D bs->opaque; uint64_t *l2_table, l2_entry; @@ -1579,6 +1579,57 @@ static int check_refcounts_l2(BlockDriverState *bs, = BdrvCheckResult *res, next_contiguous_offset =3D offset + s->cluster_size; } =20 + /* Correct offsets are cluster aligned */ + if (offset_into_cluster(s, offset)) { + if (qcow2_get_cluster_type(l2_entry) =3D=3D + QCOW2_CLUSTER_ZERO_ALLOC) + { + fprintf(stderr, "%s offset=3D%" PRIx64 ": Preallocated= zero " + "cluster is not properly aligned; L2 entry " + "corrupted.\n", + fix & BDRV_FIX_ERRORS ? "Repairing" : "ERROR", + offset); + if (fix & BDRV_FIX_ERRORS) { + uint64_t l2e_offset =3D + l2_offset + (uint64_t)i * sizeof(uint64_t); + + l2_entry =3D QCOW_OFLAG_ZERO; + l2_table[i] =3D cpu_to_be64(l2_entry); + ret =3D qcow2_pre_write_overlap_check(bs, + QCOW2_OL_ACTIVE_L2 | QCOW2_OL_INACTIVE_L2, + l2e_offset, sizeof(uint64_t)); + if (ret < 0) { + fprintf(stderr, "ERROR: Overlap check failed\n= "); + res->check_errors++; + /* Something is seriously wrong, so abort chec= king + * this L2 table */ + goto fail; + } + + ret =3D bdrv_pwrite_sync(bs->file, l2e_offset, + &l2_table[i], sizeof(uint64= _t)); + if (ret < 0) { + fprintf(stderr, "ERROR: Failed to overwrite L2= " + "table entry: %s\n", strerror(-ret)); + res->check_errors++; + /* Do not abort, continue checking the rest of= this + * L2 table's entries */ + } else { + res->corruptions_fixed++; + /* Skip marking the cluster as used + * (it is unused now) */ + continue; + } + } else { + res->corruptions++; + } + } else { + fprintf(stderr, "ERROR offset=3D%" PRIx64 ": Data clus= ter is " + "not properly aligned; L2 entry corrupted.\n", off= set); + res->corruptions++; + } + } + /* Mark cluster as used */ ret =3D qcow2_inc_refcounts_imrt(bs, res, refcount_table, refcount_table_= size, @@ -1586,13 +1637,6 @@ static int check_refcounts_l2(BlockDriverState *bs, = BdrvCheckResult *res, if (ret < 0) { goto fail; } - - /* Correct offsets are cluster aligned */ - if (offset_into_cluster(s, offset)) { - fprintf(stderr, "ERROR offset=3D%" PRIx64 ": Cluster is no= t " - "properly aligned; L2 entry corrupted.\n", offset); - res->corruptions++; - } break; } =20 @@ -1626,7 +1670,7 @@ static int check_refcounts_l1(BlockDriverState *bs, void **refcount_table, int64_t *refcount_table_size, int64_t l1_table_offset, int l1_size, - int flags) + int flags, BdrvCheckMode fix) { BDRVQcow2State *s =3D bs->opaque; uint64_t *l1_table =3D NULL, l2_offset, l1_size2; @@ -1681,7 +1725,8 @@ static int check_refcounts_l1(BlockDriverState *bs, =20 /* Process and check L2 entries */ ret =3D check_refcounts_l2(bs, res, refcount_table, - refcount_table_size, l2_offset, flags= ); + refcount_table_size, l2_offset, flags, + fix); if (ret < 0) { goto fail; } @@ -1957,7 +2002,8 @@ static int calculate_refcounts(BlockDriverState *bs, = BdrvCheckResult *res, =20 /* current L1 table */ ret =3D check_refcounts_l1(bs, res, refcount_table, nb_clusters, - s->l1_table_offset, s->l1_size, CHECK_FRAG_IN= FO); + s->l1_table_offset, s->l1_size, CHECK_FRAG_IN= FO, + fix); if (ret < 0) { return ret; } @@ -1966,7 +2012,7 @@ static int calculate_refcounts(BlockDriverState *bs, = BdrvCheckResult *res, for (i =3D 0; i < s->nb_snapshots; i++) { sn =3D s->snapshots + i; ret =3D check_refcounts_l1(bs, res, refcount_table, nb_clusters, - sn->l1_table_offset, sn->l1_size, 0); + sn->l1_table_offset, sn->l1_size, 0, fix); if (ret < 0) { return ret; } diff --git a/tests/qemu-iotests/060 b/tests/qemu-iotests/060 index 1eca09417b..8e4d27a77a 100755 --- a/tests/qemu-iotests/060 +++ b/tests/qemu-iotests/060 @@ -335,7 +335,8 @@ poke_file "$TEST_IMG" "$l2_offset" "\x80\x00\x00\x00\x0= 0\x00\x2a\x01" # Let's write to it! $QEMU_IO -c "write 0 64k" "$TEST_IMG" | _filter_qemu_io =20 -# Can't repair this yet (TODO: We can just deallocate the cluster) +echo '--- Repairing ---' +_check_test_img -r all =20 echo echo '=3D=3D=3D Discarding with an unaligned refblock =3D=3D=3D' diff --git a/tests/qemu-iotests/060.out b/tests/qemu-iotests/060.out index 56f5eb15d8..16d5142eed 100644 --- a/tests/qemu-iotests/060.out +++ b/tests/qemu-iotests/060.out @@ -317,6 +317,15 @@ discard 65536/65536 bytes at offset 0 64 KiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec) qcow2: Marking image as corrupt: Preallocated zero cluster offset 0x2a00 u= naligned (guest offset: 0); further corruption events will be suppressed write failed: Input/output error +--- Repairing --- +Repairing offset=3D2a00: Preallocated zero cluster is not properly aligned= ; L2 entry corrupted. +The following inconsistencies were found and repaired: + + 0 leaked clusters + 1 corruptions + +Double checking the fixed image now... +No errors were found on the image. =20 =3D=3D=3D Discarding with an unaligned refblock =3D=3D=3D =20 --=20 2.13.6