218e99
From d4803ddf6139cf2ad7e2d53035b5f828da97b51c Mon Sep 17 00:00:00 2001
218e99
From: Max Reitz <mreitz@redhat.com>
218e99
Date: Mon, 4 Nov 2013 22:32:04 +0100
218e99
Subject: [PATCH 11/87] qcow2-refcount: Repair shared refcount blocks
218e99
218e99
RH-Author: Max Reitz <mreitz@redhat.com>
218e99
Message-id: <1383604354-12743-14-git-send-email-mreitz@redhat.com>
218e99
Patchwork-id: 55313
218e99
O-Subject: [RHEL-7.0 qemu-kvm PATCH 13/43] qcow2-refcount: Repair shared refcount blocks
218e99
Bugzilla: 1004347
218e99
RH-Acked-by: Kevin Wolf <kwolf@redhat.com>
218e99
RH-Acked-by: Laszlo Ersek <lersek@redhat.com>
218e99
RH-Acked-by: Fam Zheng <famz@redhat.com>
218e99
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
218e99
218e99
BZ: 1004347
218e99
218e99
If the refcount of a refcount block is greater than one, we can at least
218e99
try to repair that problem by duplicating the affected block.
218e99
218e99
Signed-off-by: Max Reitz <mreitz@redhat.com>
218e99
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
218e99
(cherry picked from commit afa50193cde574528a130a25544fd6f3aa8da069)
218e99
218e99
Signed-off-by: Max Reitz <mreitz@redhat.com>
218e99
---
218e99
 block/blkdebug.c       |   1 +
218e99
 block/qcow2-refcount.c | 148 ++++++++++++++++++++++++++++++++++++++++++++++++-
218e99
 include/block/block.h  |   1 +
218e99
 3 files changed, 148 insertions(+), 2 deletions(-)
218e99
218e99
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
218e99
---
218e99
 block/blkdebug.c       |    1 +
218e99
 block/qcow2-refcount.c |  148 +++++++++++++++++++++++++++++++++++++++++++++++-
218e99
 include/block/block.h  |    1 +
218e99
 3 files changed, 148 insertions(+), 2 deletions(-)
218e99
218e99
diff --git a/block/blkdebug.c b/block/blkdebug.c
218e99
index 71f99e4..d659d38 100644
218e99
--- a/block/blkdebug.c
218e99
+++ b/block/blkdebug.c
218e99
@@ -168,6 +168,7 @@ static const char *event_names[BLKDBG_EVENT_MAX] = {
218e99
 
218e99
     [BLKDBG_REFTABLE_LOAD]                  = "reftable_load",
218e99
     [BLKDBG_REFTABLE_GROW]                  = "reftable_grow",
218e99
+    [BLKDBG_REFTABLE_UPDATE]                = "reftable_update",
218e99
 
218e99
     [BLKDBG_REFBLOCK_LOAD]                  = "refblock_load",
218e99
     [BLKDBG_REFBLOCK_UPDATE]                = "refblock_update",
218e99
diff --git a/block/qcow2-refcount.c b/block/qcow2-refcount.c
218e99
index 92ecc64..927bdeb 100644
218e99
--- a/block/qcow2-refcount.c
218e99
+++ b/block/qcow2-refcount.c
218e99
@@ -1320,6 +1320,121 @@ fail:
218e99
 }
218e99
 
218e99
 /*
218e99
+ * Writes one sector of the refcount table to the disk
218e99
+ */
218e99
+#define RT_ENTRIES_PER_SECTOR (512 / sizeof(uint64_t))
218e99
+static int write_reftable_entry(BlockDriverState *bs, int rt_index)
218e99
+{
218e99
+    BDRVQcowState *s = bs->opaque;
218e99
+    uint64_t buf[RT_ENTRIES_PER_SECTOR];
218e99
+    int rt_start_index;
218e99
+    int i, ret;
218e99
+
218e99
+    rt_start_index = rt_index & ~(RT_ENTRIES_PER_SECTOR - 1);
218e99
+    for (i = 0; i < RT_ENTRIES_PER_SECTOR; i++) {
218e99
+        buf[i] = cpu_to_be64(s->refcount_table[rt_start_index + i]);
218e99
+    }
218e99
+
218e99
+    ret = qcow2_pre_write_overlap_check(bs,
218e99
+            QCOW2_OL_DEFAULT & ~QCOW2_OL_REFCOUNT_TABLE,
218e99
+            s->refcount_table_offset + rt_start_index * sizeof(uint64_t),
218e99
+            sizeof(buf));
218e99
+    if (ret < 0) {
218e99
+        return ret;
218e99
+    }
218e99
+
218e99
+    BLKDBG_EVENT(bs->file, BLKDBG_REFTABLE_UPDATE);
218e99
+    ret = bdrv_pwrite_sync(bs->file, s->refcount_table_offset +
218e99
+            rt_start_index * sizeof(uint64_t), buf, sizeof(buf));
218e99
+    if (ret < 0) {
218e99
+        return ret;
218e99
+    }
218e99
+
218e99
+    return 0;
218e99
+}
218e99
+
218e99
+/*
218e99
+ * Allocates a new cluster for the given refcount block (represented by its
218e99
+ * offset in the image file) and copies the current content there. This function
218e99
+ * does _not_ decrement the reference count for the currently occupied cluster.
218e99
+ *
218e99
+ * This function prints an informative message to stderr on error (and returns
218e99
+ * -errno); on success, 0 is returned.
218e99
+ */
218e99
+static int64_t realloc_refcount_block(BlockDriverState *bs, int reftable_index,
218e99
+                                      uint64_t offset)
218e99
+{
218e99
+    BDRVQcowState *s = bs->opaque;
218e99
+    int64_t new_offset = 0;
218e99
+    void *refcount_block = NULL;
218e99
+    int ret;
218e99
+
218e99
+    /* allocate new refcount block */
218e99
+    new_offset = qcow2_alloc_clusters(bs, s->cluster_size);
218e99
+    if (new_offset < 0) {
218e99
+        fprintf(stderr, "Could not allocate new cluster: %s\n",
218e99
+                strerror(-new_offset));
218e99
+        ret = new_offset;
218e99
+        goto fail;
218e99
+    }
218e99
+
218e99
+    /* fetch current refcount block content */
218e99
+    ret = qcow2_cache_get(bs, s->refcount_block_cache, offset, &refcount_block);
218e99
+    if (ret < 0) {
218e99
+        fprintf(stderr, "Could not fetch refcount block: %s\n", strerror(-ret));
218e99
+        goto fail;
218e99
+    }
218e99
+
218e99
+    /* new block has not yet been entered into refcount table, therefore it is
218e99
+     * no refcount block yet (regarding this check) */
218e99
+    ret = qcow2_pre_write_overlap_check(bs, QCOW2_OL_DEFAULT, new_offset,
218e99
+            s->cluster_size);
218e99
+    if (ret < 0) {
218e99
+        fprintf(stderr, "Could not write refcount block; metadata overlap "
218e99
+                "check failed: %s\n", strerror(-ret));
218e99
+        /* the image will be marked corrupt, so don't even attempt on freeing
218e99
+         * the cluster */
218e99
+        new_offset = 0;
218e99
+        goto fail;
218e99
+    }
218e99
+
218e99
+    /* write to new block */
218e99
+    ret = bdrv_write(bs->file, new_offset / BDRV_SECTOR_SIZE, refcount_block,
218e99
+            s->cluster_sectors);
218e99
+    if (ret < 0) {
218e99
+        fprintf(stderr, "Could not write refcount block: %s\n", strerror(-ret));
218e99
+        goto fail;
218e99
+    }
218e99
+
218e99
+    /* update refcount table */
218e99
+    assert(!(new_offset & (s->cluster_size - 1)));
218e99
+    s->refcount_table[reftable_index] = new_offset;
218e99
+    ret = write_reftable_entry(bs, reftable_index);
218e99
+    if (ret < 0) {
218e99
+        fprintf(stderr, "Could not update refcount table: %s\n",
218e99
+                strerror(-ret));
218e99
+        goto fail;
218e99
+    }
218e99
+
218e99
+fail:
218e99
+    if (new_offset && (ret < 0)) {
218e99
+        qcow2_free_clusters(bs, new_offset, s->cluster_size,
218e99
+                QCOW2_DISCARD_ALWAYS);
218e99
+    }
218e99
+    if (refcount_block) {
218e99
+        if (ret < 0) {
218e99
+            qcow2_cache_put(bs, s->refcount_block_cache, &refcount_block);
218e99
+        } else {
218e99
+            ret = qcow2_cache_put(bs, s->refcount_block_cache, &refcount_block);
218e99
+        }
218e99
+    }
218e99
+    if (ret < 0) {
218e99
+        return ret;
218e99
+    }
218e99
+    return new_offset;
218e99
+}
218e99
+
218e99
+/*
218e99
  * Checks an image for refcount consistency.
218e99
  *
218e99
  * Returns 0 if no errors are found, the number of errors in case the image is
218e99
@@ -1395,10 +1510,39 @@ int qcow2_check_refcounts(BlockDriverState *bs, BdrvCheckResult *res,
218e99
             inc_refcounts(bs, res, refcount_table, nb_clusters,
218e99
                 offset, s->cluster_size);
218e99
             if (refcount_table[cluster] != 1) {
218e99
-                fprintf(stderr, "ERROR refcount block %" PRId64
218e99
+                fprintf(stderr, "%s refcount block %" PRId64
218e99
                     " refcount=%d\n",
218e99
+                    fix & BDRV_FIX_ERRORS ? "Repairing" :
218e99
+                                            "ERROR",
218e99
                     i, refcount_table[cluster]);
218e99
-                res->corruptions++;
218e99
+
218e99
+                if (fix & BDRV_FIX_ERRORS) {
218e99
+                    int64_t new_offset;
218e99
+
218e99
+                    new_offset = realloc_refcount_block(bs, i, offset);
218e99
+                    if (new_offset < 0) {
218e99
+                        res->corruptions++;
218e99
+                        continue;
218e99
+                    }
218e99
+
218e99
+                    /* update refcounts */
218e99
+                    if ((new_offset >> s->cluster_bits) >= nb_clusters) {
218e99
+                        /* increase refcount_table size if necessary */
218e99
+                        int old_nb_clusters = nb_clusters;
218e99
+                        nb_clusters = (new_offset >> s->cluster_bits) + 1;
218e99
+                        refcount_table = g_realloc(refcount_table,
218e99
+                                nb_clusters * sizeof(uint16_t));
218e99
+                        memset(&refcount_table[old_nb_clusters], 0, (nb_clusters
218e99
+                                - old_nb_clusters) * sizeof(uint16_t));
218e99
+                    }
218e99
+                    refcount_table[cluster]--;
218e99
+                    inc_refcounts(bs, res, refcount_table, nb_clusters,
218e99
+                            new_offset, s->cluster_size);
218e99
+
218e99
+                    res->corruptions_fixed++;
218e99
+                } else {
218e99
+                    res->corruptions++;
218e99
+                }
218e99
             }
218e99
         }
218e99
     }
218e99
diff --git a/include/block/block.h b/include/block/block.h
218e99
index 03ebc47..39770a3 100644
218e99
--- a/include/block/block.h
218e99
+++ b/include/block/block.h
218e99
@@ -447,6 +447,7 @@ typedef enum {
218e99
 
218e99
     BLKDBG_REFTABLE_LOAD,
218e99
     BLKDBG_REFTABLE_GROW,
218e99
+    BLKDBG_REFTABLE_UPDATE,
218e99
 
218e99
     BLKDBG_REFBLOCK_LOAD,
218e99
     BLKDBG_REFBLOCK_UPDATE,
218e99
-- 
218e99
1.7.1
218e99