From patchwork Fri Nov 10 20:37:59 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Max Reitz X-Patchwork-Id: 10053727 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id B4BDB603FA for ; Fri, 10 Nov 2017 20:40:05 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A90222B404 for ; Fri, 10 Nov 2017 20:40:05 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 9D6A42B433; Fri, 10 Nov 2017 20:40:05 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 451232B404 for ; Fri, 10 Nov 2017 20:40:03 +0000 (UTC) Received: from localhost ([::1]:43414 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eDG5q-0007MX-FU for patchwork-qemu-devel@patchwork.kernel.org; Fri, 10 Nov 2017 15:40:02 -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+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP 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_offset, - int flags) + int flags, BdrvCheckMode fix) { BDRVQcow2State *s = bs->opaque; uint64_t *l2_table, l2_entry; @@ -1579,6 +1579,57 @@ static int check_refcounts_l2(BlockDriverState *bs, BdrvCheckResult *res, next_contiguous_offset = offset + s->cluster_size; } + /* Correct offsets are cluster aligned */ + if (offset_into_cluster(s, offset)) { + if (qcow2_get_cluster_type(l2_entry) == + QCOW2_CLUSTER_ZERO_ALLOC) + { + fprintf(stderr, "%s offset=%" 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 = + l2_offset + (uint64_t)i * sizeof(uint64_t); + + l2_entry = QCOW_OFLAG_ZERO; + l2_table[i] = cpu_to_be64(l2_entry); + ret = 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 checking + * this L2 table */ + goto fail; + } + + ret = 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=%" PRIx64 ": Data cluster is " + "not properly aligned; L2 entry corrupted.\n", offset); + res->corruptions++; + } + } + /* Mark cluster as used */ ret = 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=%" PRIx64 ": Cluster is not " - "properly aligned; L2 entry corrupted.\n", offset); - res->corruptions++; - } break; } @@ -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 = bs->opaque; uint64_t *l1_table = NULL, l2_offset, l1_size2; @@ -1681,7 +1725,8 @@ static int check_refcounts_l1(BlockDriverState *bs, /* Process and check L2 entries */ ret = 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, /* current L1 table */ ret = check_refcounts_l1(bs, res, refcount_table, nb_clusters, - s->l1_table_offset, s->l1_size, CHECK_FRAG_INFO); + s->l1_table_offset, s->l1_size, CHECK_FRAG_INFO, + fix); if (ret < 0) { return ret; } @@ -1966,7 +2012,7 @@ static int calculate_refcounts(BlockDriverState *bs, BdrvCheckResult *res, for (i = 0; i < s->nb_snapshots; i++) { sn = s->snapshots + i; ret = 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\x00\x00\x2a\x01" # Let's write to it! $QEMU_IO -c "write 0 64k" "$TEST_IMG" | _filter_qemu_io -# Can't repair this yet (TODO: We can just deallocate the cluster) +echo '--- Repairing ---' +_check_test_img -r all echo echo '=== Discarding with an unaligned refblock ===' 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 unaligned (guest offset: 0); further corruption events will be suppressed write failed: Input/output error +--- Repairing --- +Repairing offset=2a00: 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. === Discarding with an unaligned refblock ===