From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([208.118.235.92]:50595) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SSubo-0004lN-FF for qemu-devel@nongnu.org; Fri, 11 May 2012 14:30:33 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1SSubm-0005Zr-DG for qemu-devel@nongnu.org; Fri, 11 May 2012 14:30:32 -0400 Received: from mx1.redhat.com ([209.132.183.28]:7023) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SSubm-0005ZH-4w for qemu-devel@nongnu.org; Fri, 11 May 2012 14:30:30 -0400 From: Kevin Wolf Date: Fri, 11 May 2012 18:48:51 +0200 Message-Id: <1336754931-20058-4-git-send-email-kwolf@redhat.com> In-Reply-To: <1336754931-20058-1-git-send-email-kwolf@redhat.com> References: <1336754931-20058-1-git-send-email-kwolf@redhat.com> Subject: [Qemu-devel] [PATCH 3/3] qcow2: Support for fixing refcount inconsistencies List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: kwolf@redhat.com, stefanha@gmail.com Signed-off-by: Kevin Wolf --- block/qcow2-refcount.c | 27 +++++++++++++++++++++++++-- block/qcow2.c | 6 +----- block/qcow2.h | 3 ++- 3 files changed, 28 insertions(+), 8 deletions(-) diff --git a/block/qcow2-refcount.c b/block/qcow2-refcount.c index 443c021..6a9a672 100644 --- a/block/qcow2-refcount.c +++ b/block/qcow2-refcount.c @@ -1122,7 +1122,8 @@ 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; @@ -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 23cc920..cc751d2 100644 --- a/block/qcow2.c +++ b/block/qcow2.c @@ -1511,11 +1511,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 9b7b2d6..5fb7f61 100644 --- a/block/qcow2.h +++ b/block/qcow2.h @@ -275,7 +275,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); -- 1.7.6.5