From patchwork Mon Jun 4 11:13:27 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kevin Wolf X-Patchwork-Id: 162775 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 67774B7011 for ; Mon, 4 Jun 2012 22:01:02 +1000 (EST) Received: from localhost ([::1]:33076 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SbVEq-0006Cj-I6 for incoming@patchwork.ozlabs.org; Mon, 04 Jun 2012 07:14:20 -0400 Received: from eggs.gnu.org ([208.118.235.92]:56122) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SbVEI-0004Um-Ki for qemu-devel@nongnu.org; Mon, 04 Jun 2012 07:13:50 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1SbVEG-00061v-S9 for qemu-devel@nongnu.org; Mon, 04 Jun 2012 07:13:46 -0400 Received: from mx1.redhat.com ([209.132.183.28]:47674) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SbVEG-00061K-Jn for qemu-devel@nongnu.org; Mon, 04 Jun 2012 07:13:44 -0400 Received: from int-mx01.intmail.prod.int.phx2.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id q54BDgiF028013 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Mon, 4 Jun 2012 07:13:42 -0400 Received: from dhcp-5-188.str.redhat.com (dhcp-5-175.str.redhat.com [10.32.5.175]) by int-mx01.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id q54BDUCv004265; Mon, 4 Jun 2012 07:13:41 -0400 From: Kevin Wolf To: anthony@codemonkey.ws Date: Mon, 4 Jun 2012 13:13:27 +0200 Message-Id: <1338808409-19501-11-git-send-email-kwolf@redhat.com> In-Reply-To: <1338808409-19501-1-git-send-email-kwolf@redhat.com> References: <1338808409-19501-1-git-send-email-kwolf@redhat.com> X-Scanned-By: MIMEDefang 2.67 on 10.5.11.11 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 209.132.183.28 Cc: kwolf@redhat.com, qemu-devel@nongnu.org Subject: [Qemu-devel] [PATCH 10/12] qcow2: Support for fixing refcount inconsistencies X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Signed-off-by: Kevin Wolf --- block/qcow2-refcount.c | 31 +++++++++++++++++++++++++++---- block/qcow2.c | 6 +----- block/qcow2.h | 3 ++- 3 files changed, 30 insertions(+), 10 deletions(-) diff --git a/block/qcow2-refcount.c b/block/qcow2-refcount.c index 443c021..3544fcf 100644 --- a/block/qcow2-refcount.c +++ b/block/qcow2-refcount.c @@ -1122,11 +1122,12 @@ fail: * Returns 0 if no errors are found, the number of errors in case the image is * detected as corrupted, and -errno when an internal error occurred. */ -int qcow2_check_refcounts(BlockDriverState *bs, BdrvCheckResult *res) +int qcow2_check_refcounts(BlockDriverState *bs, BdrvCheckResult *res, + BdrvCheckMode fix) { BDRVQcowState *s = bs->opaque; - int64_t size; - int nb_clusters, refcount1, refcount2, i; + int64_t size, i; + int nb_clusters, refcount1, refcount2; QCowSnapshot *sn; uint16_t *refcount_table; int ret; @@ -1205,9 +1206,31 @@ int qcow2_check_refcounts(BlockDriverState *bs, BdrvCheckResult *res) refcount2 = refcount_table[i]; if (refcount1 != refcount2) { + + /* Check if we're allowed to fix the mismatch */ + int *num_fixed = NULL; + if (refcount1 > refcount2 && (fix & BDRV_FIX_LEAKS)) { + num_fixed = &res->leaks_fixed; + } else if (refcount1 < refcount2 && (fix & BDRV_FIX_ERRORS)) { + num_fixed = &res->corruptions_fixed; + } + fprintf(stderr, "%s cluster %d refcount=%d reference=%d\n", - refcount1 < refcount2 ? "ERROR" : "Leaked", + num_fixed != NULL ? "Repairing" : + refcount1 < refcount2 ? "ERROR" : + "Leaked", i, refcount1, refcount2); + + if (num_fixed) { + ret = update_refcount(bs, i << s->cluster_bits, 1, + refcount2 - refcount1); + if (ret >= 0) { + (*num_fixed)++; + continue; + } + } + + /* And if we couldn't, print an error */ if (refcount1 < refcount2) { res->corruptions++; } else { diff --git a/block/qcow2.c b/block/qcow2.c index 7797015..d66de58 100644 --- a/block/qcow2.c +++ b/block/qcow2.c @@ -1473,11 +1473,7 @@ static int qcow2_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) static int qcow2_check(BlockDriverState *bs, BdrvCheckResult *result, BdrvCheckMode fix) { - if (fix) { - return -ENOTSUP; - } - - return qcow2_check_refcounts(bs, result); + return qcow2_check_refcounts(bs, result, fix); } #if 0 diff --git a/block/qcow2.h b/block/qcow2.h index 93567f6..c6e7237 100644 --- a/block/qcow2.h +++ b/block/qcow2.h @@ -261,7 +261,8 @@ void qcow2_free_any_clusters(BlockDriverState *bs, int qcow2_update_snapshot_refcount(BlockDriverState *bs, int64_t l1_table_offset, int l1_size, int addend); -int qcow2_check_refcounts(BlockDriverState *bs, BdrvCheckResult *res); +int qcow2_check_refcounts(BlockDriverState *bs, BdrvCheckResult *res, + BdrvCheckMode fix); /* qcow2-cluster.c functions */ int qcow2_grow_l1_table(BlockDriverState *bs, int min_size, bool exact_size);