qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Kevin Wolf <kwolf@redhat.com>
To: anthony@codemonkey.ws
Cc: kwolf@redhat.com, qemu-devel@nongnu.org
Subject: [Qemu-devel] [PATCH 11/39] qcow2: Support for fixing refcount inconsistencies
Date: Fri, 15 Jun 2012 15:33:11 +0200	[thread overview]
Message-ID: <1339767219-24297-12-git-send-email-kwolf@redhat.com> (raw)
In-Reply-To: <1339767219-24297-1-git-send-email-kwolf@redhat.com>

Signed-off-by: Kevin Wolf <kwolf@redhat.com>
---
 block/qcow2-refcount.c |   43 ++++++++++++++++++++++++++++++++++---------
 block/qcow2.c          |    6 +-----
 block/qcow2.h          |    3 ++-
 3 files changed, 37 insertions(+), 15 deletions(-)

diff --git a/block/qcow2-refcount.c b/block/qcow2-refcount.c
index 443c021..5d6ea72 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;
@@ -1170,14 +1171,15 @@ int qcow2_check_refcounts(BlockDriverState *bs, BdrvCheckResult *res)
 
         /* Refcount blocks are cluster aligned */
         if (offset & (s->cluster_size - 1)) {
-            fprintf(stderr, "ERROR refcount block %d is not "
+            fprintf(stderr, "ERROR refcount block %" PRId64 " is not "
                 "cluster aligned; refcount table entry corrupted\n", i);
             res->corruptions++;
             continue;
         }
 
         if (cluster >= nb_clusters) {
-            fprintf(stderr, "ERROR refcount block %d is outside image\n", i);
+            fprintf(stderr, "ERROR refcount block %" PRId64
+                    " is outside image\n", i);
             res->corruptions++;
             continue;
         }
@@ -1186,7 +1188,8 @@ int qcow2_check_refcounts(BlockDriverState *bs, BdrvCheckResult *res)
             inc_refcounts(bs, res, refcount_table, nb_clusters,
                 offset, s->cluster_size);
             if (refcount_table[cluster] != 1) {
-                fprintf(stderr, "ERROR refcount block %d refcount=%d\n",
+                fprintf(stderr, "ERROR refcount block %" PRId64
+                    " refcount=%d\n",
                     i, refcount_table[cluster]);
                 res->corruptions++;
             }
@@ -1197,7 +1200,7 @@ int qcow2_check_refcounts(BlockDriverState *bs, BdrvCheckResult *res)
     for(i = 0; i < nb_clusters; i++) {
         refcount1 = get_refcount(bs, i);
         if (refcount1 < 0) {
-            fprintf(stderr, "Can't get refcount for cluster %d: %s\n",
+            fprintf(stderr, "Can't get refcount for cluster %" PRId64 ": %s\n",
                 i, strerror(-refcount1));
             res->check_errors++;
             continue;
@@ -1205,9 +1208,31 @@ int qcow2_check_refcounts(BlockDriverState *bs, BdrvCheckResult *res)
 
         refcount2 = refcount_table[i];
         if (refcount1 != refcount2) {
-            fprintf(stderr, "%s cluster %d refcount=%d reference=%d\n",
-                   refcount1 < refcount2 ? "ERROR" : "Leaked",
+
+            /* 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 %" PRId64 " refcount=%d reference=%d\n",
+                   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);
-- 
1.7.6.5

  parent reply	other threads:[~2012-06-15 13:34 UTC|newest]

Thread overview: 50+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-06-15 13:33 [Qemu-devel] [PULL 00/39] Block patches Kevin Wolf
2012-06-15 13:33 ` [Qemu-devel] [PATCH 01/39] qcow2: remove a line of unnecessary code Kevin Wolf
2012-06-15 13:33 ` [Qemu-devel] [PATCH 02/39] qcow2: fix endianness conversion Kevin Wolf
2012-06-15 13:33 ` [Qemu-devel] [PATCH 03/39] block: implement is_allocated for raw Kevin Wolf
2012-06-19 12:37   ` Alexander Graf
2012-06-15 13:33 ` [Qemu-devel] [PATCH 04/39] stream: tweak usage of bdrv_co_is_allocated Kevin Wolf
2012-06-15 13:33 ` [Qemu-devel] [PATCH 05/39] stream: move is_allocated_above to block.c Kevin Wolf
2012-06-15 13:33 ` [Qemu-devel] [PATCH 06/39] stream: move rate limiting to a separate header file Kevin Wolf
2012-06-15 13:33 ` [Qemu-devel] [PATCH 07/39] xtensa_lx60: add missing #include "blockdev.h" Kevin Wolf
2012-06-15 13:33 ` [Qemu-devel] [PATCH 08/39] Un-inline fdctrl_init_isa() Kevin Wolf
2012-06-15 13:33 ` [Qemu-devel] [PATCH 09/39] qemu-img check -r for repairing images Kevin Wolf
2012-06-15 13:33 ` [Qemu-devel] [PATCH 10/39] qemu-img check: Print fixed clusters and recheck Kevin Wolf
2012-06-15 13:33 ` Kevin Wolf [this message]
2012-06-15 13:33 ` [Qemu-devel] [PATCH 12/39] rbd: hook up cache options Kevin Wolf
2012-06-15 13:33 ` [Qemu-devel] [PATCH 13/39] sheepdog: add coroutine_fn markers to coroutine functions Kevin Wolf
2012-06-15 13:33 ` [Qemu-devel] [PATCH 14/39] block: Simplify how drive_init() computes default ID Kevin Wolf
2012-06-15 13:33 ` [Qemu-devel] [PATCH 15/39] Prevent disk data loss when closing qemu Kevin Wolf
2012-06-15 13:33 ` [Qemu-devel] [PATCH 16/39] block: New bdrv_get_flags() Kevin Wolf
2012-06-15 13:33 ` [Qemu-devel] [PATCH 17/39] scsi-disk: Don't peek behind the BlockDriverState abstraction Kevin Wolf
2012-06-15 13:33 ` [Qemu-devel] [PATCH 18/39] qemu-iotests: fill streaming test image with data Kevin Wolf
2012-06-15 13:33 ` [Qemu-devel] [PATCH 19/39] qemu-iotests: start vms in qtest mode Kevin Wolf
2012-06-15 13:33 ` [Qemu-devel] [PATCH 20/39] block: flush in writethrough mode after writes Kevin Wolf
2012-06-15 13:33 ` [Qemu-devel] [PATCH 21/39] savevm: flush after saving vm state Kevin Wolf
2012-06-15 13:33 ` [Qemu-devel] [PATCH 22/39] block: copy enable_write_cache in bdrv_append Kevin Wolf
2012-06-15 13:33 ` [Qemu-devel] [PATCH 23/39] block: add bdrv_set_enable_write_cache Kevin Wolf
2012-06-15 13:33 ` [Qemu-devel] [PATCH 24/39] block: always open drivers in writeback mode Kevin Wolf
2012-06-15 13:33 ` [Qemu-devel] [PATCH 25/39] ide: support enable/disable write cache Kevin Wolf
2012-06-15 13:33 ` [Qemu-devel] [PATCH 26/39] qcow2: always operate caches in writeback mode Kevin Wolf
2012-06-15 13:33 ` [Qemu-devel] [PATCH 27/39] qcow2: Simplify calculation for COW area at the end Kevin Wolf
2012-06-15 13:33 ` [Qemu-devel] [PATCH 28/39] qcow2: Fix avail_sectors in cluster allocation code Kevin Wolf
2012-12-12 13:25   ` [Qemu-devel] [BUG] qemu-1.1.2 [FIXED-BY] " Philipp Hahn
2012-12-12 13:41     ` Kevin Wolf
2012-12-12 14:09       ` Philipp Hahn
2012-12-12 16:54         ` Kevin Wolf
2012-12-12 17:29           ` Philipp Hahn
2012-12-14 13:03             ` Philipp Hahn
2012-12-18  9:46           ` Philipp Hahn
2012-12-18 12:12             ` Michael Tokarev
2012-06-15 13:33 ` [Qemu-devel] [PATCH 29/39] qemu-iotests: Some backing file COW tests Kevin Wolf
2012-06-15 13:33 ` [Qemu-devel] [PATCH 30/39] qemu-iotests: COW with many AIO requests on the same cluster Kevin Wolf
2012-06-15 13:33 ` [Qemu-devel] [PATCH 31/39] qemu-img: document qed format on qemu-img man page Kevin Wolf
2012-06-15 13:33 ` [Qemu-devel] [PATCH 32/39] block: Replace bdrv_get_format() by bdrv_get_format_name() Kevin Wolf
2012-06-15 13:33 ` [Qemu-devel] [PATCH 33/39] xen: Don't change -drive if=xen device name during machine init Kevin Wolf
2012-06-15 13:33 ` [Qemu-devel] [PATCH 34/39] xen: Don't peek behind the BlockDriverState abstraction Kevin Wolf
2012-06-15 13:33 ` [Qemu-devel] [PATCH 35/39] qcow2: fix autoclear image header update Kevin Wolf
2012-06-15 13:33 ` [Qemu-devel] [PATCH 36/39] fdc: fix implied seek while there is no media in drive Kevin Wolf
2012-06-15 13:33 ` [Qemu-devel] [PATCH 37/39] fdc-test: introduced qtest read_without_media Kevin Wolf
2012-06-15 13:33 ` [Qemu-devel] [PATCH 38/39] qemu-iotests: add qcow2.py set-feature-bit command Kevin Wolf
2012-06-15 13:33 ` [Qemu-devel] [PATCH 39/39] qemu-iotests: add 036 autoclear feature bit test Kevin Wolf
2012-06-20 13:09 ` [Qemu-devel] [PULL 00/39] Block patches Anthony Liguori

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1339767219-24297-12-git-send-email-kwolf@redhat.com \
    --to=kwolf@redhat.com \
    --cc=anthony@codemonkey.ws \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).