9ae3a8
From 3be020259d1627f335fe27aac1dc55e1249b7390 Mon Sep 17 00:00:00 2001
9ae3a8
From: Max Reitz <mreitz@redhat.com>
9ae3a8
Date: Sat, 13 Jun 2015 16:22:22 +0200
9ae3a8
Subject: [PATCH 28/42] qcow2: Rebuild refcount structure during check
9ae3a8
9ae3a8
Message-id: <1434212556-3927-29-git-send-email-mreitz@redhat.com>
9ae3a8
Patchwork-id: 66047
9ae3a8
O-Subject: [RHEL-7.2 qemu-kvm PATCH 28/42] qcow2: Rebuild refcount structure during check
9ae3a8
Bugzilla: 1129893
9ae3a8
RH-Acked-by: Jeffrey Cody <jcody@redhat.com>
9ae3a8
RH-Acked-by: Fam Zheng <famz@redhat.com>
9ae3a8
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
9ae3a8
9ae3a8
BZ: 1129893
9ae3a8
9ae3a8
The previous commit introduced the "rebuild" variable to qcow2's
9ae3a8
implementation of the image consistency check. Now make use of this by
9ae3a8
adding a function which creates a completely new refcount structure
9ae3a8
based solely on the in-memory information gathered before.
9ae3a8
9ae3a8
The old refcount structure will be leaked, however. This leak will be
9ae3a8
dealt with in a follow-up commit.
9ae3a8
9ae3a8
Signed-off-by: Max Reitz <mreitz@redhat.com>
9ae3a8
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
9ae3a8
(cherry picked from commit c7c0681bc8a781e0319b7cf969b904dfe50d083e)
9ae3a8
9ae3a8
Signed-off-by: Max Reitz <mreitz@redhat.com>
9ae3a8
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
9ae3a8
---
9ae3a8
 block/qcow2-refcount.c | 311 ++++++++++++++++++++++++++++++++++++++++++++++++-
9ae3a8
 1 file changed, 305 insertions(+), 6 deletions(-)
9ae3a8
9ae3a8
diff --git a/block/qcow2-refcount.c b/block/qcow2-refcount.c
9ae3a8
index 3d66e7c..651ddb6 100644
9ae3a8
--- a/block/qcow2-refcount.c
9ae3a8
+++ b/block/qcow2-refcount.c
9ae3a8
@@ -1660,6 +1660,285 @@ static void compare_refcounts(BlockDriverState *bs, BdrvCheckResult *res,
9ae3a8
 }
9ae3a8
 
9ae3a8
 /*
9ae3a8
+ * Allocates clusters using an in-memory refcount table (IMRT) in contrast to
9ae3a8
+ * the on-disk refcount structures.
9ae3a8
+ *
9ae3a8
+ * On input, *first_free_cluster tells where to start looking, and need not
9ae3a8
+ * actually be a free cluster; the returned offset will not be before that
9ae3a8
+ * cluster.  On output, *first_free_cluster points to the first gap found, even
9ae3a8
+ * if that gap was too small to be used as the returned offset.
9ae3a8
+ *
9ae3a8
+ * Note that *first_free_cluster is a cluster index whereas the return value is
9ae3a8
+ * an offset.
9ae3a8
+ */
9ae3a8
+static int64_t alloc_clusters_imrt(BlockDriverState *bs,
9ae3a8
+                                   int cluster_count,
9ae3a8
+                                   uint16_t **refcount_table,
9ae3a8
+                                   int64_t *imrt_nb_clusters,
9ae3a8
+                                   int64_t *first_free_cluster)
9ae3a8
+{
9ae3a8
+    BDRVQcowState *s = bs->opaque;
9ae3a8
+    int64_t cluster = *first_free_cluster, i;
9ae3a8
+    bool first_gap = true;
9ae3a8
+    int contiguous_free_clusters;
9ae3a8
+
9ae3a8
+    /* Starting at *first_free_cluster, find a range of at least cluster_count
9ae3a8
+     * continuously free clusters */
9ae3a8
+    for (contiguous_free_clusters = 0;
9ae3a8
+         cluster < *imrt_nb_clusters &&
9ae3a8
+         contiguous_free_clusters < cluster_count;
9ae3a8
+         cluster++)
9ae3a8
+    {
9ae3a8
+        if (!(*refcount_table)[cluster]) {
9ae3a8
+            contiguous_free_clusters++;
9ae3a8
+            if (first_gap) {
9ae3a8
+                /* If this is the first free cluster found, update
9ae3a8
+                 * *first_free_cluster accordingly */
9ae3a8
+                *first_free_cluster = cluster;
9ae3a8
+                first_gap = false;
9ae3a8
+            }
9ae3a8
+        } else if (contiguous_free_clusters) {
9ae3a8
+            contiguous_free_clusters = 0;
9ae3a8
+        }
9ae3a8
+    }
9ae3a8
+
9ae3a8
+    /* If contiguous_free_clusters is greater than zero, it contains the number
9ae3a8
+     * of continuously free clusters until the current cluster; the first free
9ae3a8
+     * cluster in the current "gap" is therefore
9ae3a8
+     * cluster - contiguous_free_clusters */
9ae3a8
+
9ae3a8
+    /* If no such range could be found, grow the in-memory refcount table
9ae3a8
+     * accordingly to append free clusters at the end of the image */
9ae3a8
+    if (contiguous_free_clusters < cluster_count) {
9ae3a8
+        int64_t old_imrt_nb_clusters = *imrt_nb_clusters;
9ae3a8
+        uint16_t *new_refcount_table;
9ae3a8
+
9ae3a8
+        /* contiguous_free_clusters clusters are already empty at the image end;
9ae3a8
+         * we need cluster_count clusters; therefore, we have to allocate
9ae3a8
+         * cluster_count - contiguous_free_clusters new clusters at the end of
9ae3a8
+         * the image (which is the current value of cluster; note that cluster
9ae3a8
+         * may exceed old_imrt_nb_clusters if *first_free_cluster pointed beyond
9ae3a8
+         * the image end) */
9ae3a8
+        *imrt_nb_clusters = cluster + cluster_count - contiguous_free_clusters;
9ae3a8
+        new_refcount_table = g_try_realloc(*refcount_table,
9ae3a8
+                                           *imrt_nb_clusters *
9ae3a8
+                                           sizeof(**refcount_table));
9ae3a8
+        if (!new_refcount_table) {
9ae3a8
+            *imrt_nb_clusters = old_imrt_nb_clusters;
9ae3a8
+            return -ENOMEM;
9ae3a8
+        }
9ae3a8
+        *refcount_table = new_refcount_table;
9ae3a8
+
9ae3a8
+        memset(*refcount_table + old_imrt_nb_clusters, 0,
9ae3a8
+               (*imrt_nb_clusters - old_imrt_nb_clusters) *
9ae3a8
+               sizeof(**refcount_table));
9ae3a8
+    }
9ae3a8
+
9ae3a8
+    /* Go back to the first free cluster */
9ae3a8
+    cluster -= contiguous_free_clusters;
9ae3a8
+    for (i = 0; i < cluster_count; i++) {
9ae3a8
+        (*refcount_table)[cluster + i] = 1;
9ae3a8
+    }
9ae3a8
+
9ae3a8
+    return cluster << s->cluster_bits;
9ae3a8
+}
9ae3a8
+
9ae3a8
+/*
9ae3a8
+ * Creates a new refcount structure based solely on the in-memory information
9ae3a8
+ * given through *refcount_table. All necessary allocations will be reflected
9ae3a8
+ * in that array.
9ae3a8
+ *
9ae3a8
+ * On success, the old refcount structure is leaked (it will be covered by the
9ae3a8
+ * new refcount structure).
9ae3a8
+ */
9ae3a8
+static int rebuild_refcount_structure(BlockDriverState *bs,
9ae3a8
+                                      BdrvCheckResult *res,
9ae3a8
+                                      uint16_t **refcount_table,
9ae3a8
+                                      int64_t *nb_clusters)
9ae3a8
+{
9ae3a8
+    BDRVQcowState *s = bs->opaque;
9ae3a8
+    int64_t first_free_cluster = 0, reftable_offset = -1, cluster = 0;
9ae3a8
+    int64_t refblock_offset, refblock_start, refblock_index;
9ae3a8
+    uint32_t reftable_size = 0;
9ae3a8
+    uint64_t *on_disk_reftable = NULL;
9ae3a8
+    uint16_t *on_disk_refblock;
9ae3a8
+    int i, ret = 0;
9ae3a8
+    struct {
9ae3a8
+        uint64_t reftable_offset;
9ae3a8
+        uint32_t reftable_clusters;
9ae3a8
+    } QEMU_PACKED reftable_offset_and_clusters;
9ae3a8
+
9ae3a8
+    qcow2_cache_empty(bs, s->refcount_block_cache);
9ae3a8
+
9ae3a8
+write_refblocks:
9ae3a8
+    for (; cluster < *nb_clusters; cluster++) {
9ae3a8
+        if (!(*refcount_table)[cluster]) {
9ae3a8
+            continue;
9ae3a8
+        }
9ae3a8
+
9ae3a8
+        refblock_index = cluster >> s->refcount_block_bits;
9ae3a8
+        refblock_start = refblock_index << s->refcount_block_bits;
9ae3a8
+
9ae3a8
+        /* Don't allocate a cluster in a refblock already written to disk */
9ae3a8
+        if (first_free_cluster < refblock_start) {
9ae3a8
+            first_free_cluster = refblock_start;
9ae3a8
+        }
9ae3a8
+        refblock_offset = alloc_clusters_imrt(bs, 1, refcount_table,
9ae3a8
+                                              nb_clusters, &first_free_cluster);
9ae3a8
+        if (refblock_offset < 0) {
9ae3a8
+            fprintf(stderr, "ERROR allocating refblock: %s\n",
9ae3a8
+                    strerror(-refblock_offset));
9ae3a8
+            res->check_errors++;
9ae3a8
+            ret = refblock_offset;
9ae3a8
+            goto fail;
9ae3a8
+        }
9ae3a8
+
9ae3a8
+        if (reftable_size <= refblock_index) {
9ae3a8
+            uint32_t old_reftable_size = reftable_size;
9ae3a8
+            uint64_t *new_on_disk_reftable;
9ae3a8
+
9ae3a8
+            reftable_size = ROUND_UP((refblock_index + 1) * sizeof(uint64_t),
9ae3a8
+                                     s->cluster_size) / sizeof(uint64_t);
9ae3a8
+            new_on_disk_reftable = g_try_realloc(on_disk_reftable,
9ae3a8
+                                                 reftable_size *
9ae3a8
+                                                 sizeof(uint64_t));
9ae3a8
+            if (!new_on_disk_reftable) {
9ae3a8
+                res->check_errors++;
9ae3a8
+                ret = -ENOMEM;
9ae3a8
+                goto fail;
9ae3a8
+            }
9ae3a8
+            on_disk_reftable = new_on_disk_reftable;
9ae3a8
+
9ae3a8
+            memset(on_disk_reftable + old_reftable_size, 0,
9ae3a8
+                   (reftable_size - old_reftable_size) * sizeof(uint64_t));
9ae3a8
+
9ae3a8
+            /* The offset we have for the reftable is now no longer valid;
9ae3a8
+             * this will leak that range, but we can easily fix that by running
9ae3a8
+             * a leak-fixing check after this rebuild operation */
9ae3a8
+            reftable_offset = -1;
9ae3a8
+        }
9ae3a8
+        on_disk_reftable[refblock_index] = refblock_offset;
9ae3a8
+
9ae3a8
+        /* If this is apparently the last refblock (for now), try to squeeze the
9ae3a8
+         * reftable in */
9ae3a8
+        if (refblock_index == (*nb_clusters - 1) >> s->refcount_block_bits &&
9ae3a8
+            reftable_offset < 0)
9ae3a8
+        {
9ae3a8
+            uint64_t reftable_clusters = size_to_clusters(s, reftable_size *
9ae3a8
+                                                          sizeof(uint64_t));
9ae3a8
+            reftable_offset = alloc_clusters_imrt(bs, reftable_clusters,
9ae3a8
+                                                  refcount_table, nb_clusters,
9ae3a8
+                                                  &first_free_cluster);
9ae3a8
+            if (reftable_offset < 0) {
9ae3a8
+                fprintf(stderr, "ERROR allocating reftable: %s\n",
9ae3a8
+                        strerror(-reftable_offset));
9ae3a8
+                res->check_errors++;
9ae3a8
+                ret = reftable_offset;
9ae3a8
+                goto fail;
9ae3a8
+            }
9ae3a8
+        }
9ae3a8
+
9ae3a8
+        ret = qcow2_pre_write_overlap_check(bs, 0, refblock_offset,
9ae3a8
+                                            s->cluster_size);
9ae3a8
+        if (ret < 0) {
9ae3a8
+            fprintf(stderr, "ERROR writing refblock: %s\n", strerror(-ret));
9ae3a8
+            goto fail;
9ae3a8
+        }
9ae3a8
+
9ae3a8
+        on_disk_refblock = qemu_blockalign0(bs->file, s->cluster_size);
9ae3a8
+        for (i = 0; i < s->refcount_block_size &&
9ae3a8
+                    refblock_start + i < *nb_clusters; i++)
9ae3a8
+        {
9ae3a8
+            on_disk_refblock[i] =
9ae3a8
+                cpu_to_be16((*refcount_table)[refblock_start + i]);
9ae3a8
+        }
9ae3a8
+
9ae3a8
+        ret = bdrv_write(bs->file, refblock_offset / BDRV_SECTOR_SIZE,
9ae3a8
+                         (void *)on_disk_refblock, s->cluster_sectors);
9ae3a8
+        qemu_vfree(on_disk_refblock);
9ae3a8
+        if (ret < 0) {
9ae3a8
+            fprintf(stderr, "ERROR writing refblock: %s\n", strerror(-ret));
9ae3a8
+            goto fail;
9ae3a8
+        }
9ae3a8
+
9ae3a8
+        /* Go to the end of this refblock */
9ae3a8
+        cluster = refblock_start + s->refcount_block_size - 1;
9ae3a8
+    }
9ae3a8
+
9ae3a8
+    if (reftable_offset < 0) {
9ae3a8
+        uint64_t post_refblock_start, reftable_clusters;
9ae3a8
+
9ae3a8
+        post_refblock_start = ROUND_UP(*nb_clusters, s->refcount_block_size);
9ae3a8
+        reftable_clusters = size_to_clusters(s,
9ae3a8
+                                             reftable_size * sizeof(uint64_t));
9ae3a8
+        /* Not pretty but simple */
9ae3a8
+        if (first_free_cluster < post_refblock_start) {
9ae3a8
+            first_free_cluster = post_refblock_start;
9ae3a8
+        }
9ae3a8
+        reftable_offset = alloc_clusters_imrt(bs, reftable_clusters,
9ae3a8
+                                              refcount_table, nb_clusters,
9ae3a8
+                                              &first_free_cluster);
9ae3a8
+        if (reftable_offset < 0) {
9ae3a8
+            fprintf(stderr, "ERROR allocating reftable: %s\n",
9ae3a8
+                    strerror(-reftable_offset));
9ae3a8
+            res->check_errors++;
9ae3a8
+            ret = reftable_offset;
9ae3a8
+            goto fail;
9ae3a8
+        }
9ae3a8
+
9ae3a8
+        goto write_refblocks;
9ae3a8
+    }
9ae3a8
+
9ae3a8
+    assert(on_disk_reftable);
9ae3a8
+
9ae3a8
+    for (refblock_index = 0; refblock_index < reftable_size; refblock_index++) {
9ae3a8
+        cpu_to_be64s(&on_disk_reftable[refblock_index]);
9ae3a8
+    }
9ae3a8
+
9ae3a8
+    ret = qcow2_pre_write_overlap_check(bs, 0, reftable_offset,
9ae3a8
+                                        reftable_size * sizeof(uint64_t));
9ae3a8
+    if (ret < 0) {
9ae3a8
+        fprintf(stderr, "ERROR writing reftable: %s\n", strerror(-ret));
9ae3a8
+        goto fail;
9ae3a8
+    }
9ae3a8
+
9ae3a8
+    assert(reftable_size < INT_MAX / sizeof(uint64_t));
9ae3a8
+    ret = bdrv_pwrite(bs->file, reftable_offset, on_disk_reftable,
9ae3a8
+                      reftable_size * sizeof(uint64_t));
9ae3a8
+    if (ret < 0) {
9ae3a8
+        fprintf(stderr, "ERROR writing reftable: %s\n", strerror(-ret));
9ae3a8
+        goto fail;
9ae3a8
+    }
9ae3a8
+
9ae3a8
+    /* Enter new reftable into the image header */
9ae3a8
+    cpu_to_be64w(&reftable_offset_and_clusters.reftable_offset,
9ae3a8
+                 reftable_offset);
9ae3a8
+    cpu_to_be32w(&reftable_offset_and_clusters.reftable_clusters,
9ae3a8
+                 size_to_clusters(s, reftable_size * sizeof(uint64_t)));
9ae3a8
+    ret = bdrv_pwrite_sync(bs->file, offsetof(QCowHeader,
9ae3a8
+                                              refcount_table_offset),
9ae3a8
+                           &reftable_offset_and_clusters,
9ae3a8
+                           sizeof(reftable_offset_and_clusters));
9ae3a8
+    if (ret < 0) {
9ae3a8
+        fprintf(stderr, "ERROR setting reftable: %s\n", strerror(-ret));
9ae3a8
+        goto fail;
9ae3a8
+    }
9ae3a8
+
9ae3a8
+    for (refblock_index = 0; refblock_index < reftable_size; refblock_index++) {
9ae3a8
+        be64_to_cpus(&on_disk_reftable[refblock_index]);
9ae3a8
+    }
9ae3a8
+    s->refcount_table = on_disk_reftable;
9ae3a8
+    s->refcount_table_offset = reftable_offset;
9ae3a8
+    s->refcount_table_size = reftable_size;
9ae3a8
+
9ae3a8
+    return 0;
9ae3a8
+
9ae3a8
+fail:
9ae3a8
+    g_free(on_disk_reftable);
9ae3a8
+    return ret;
9ae3a8
+}
9ae3a8
+
9ae3a8
+/*
9ae3a8
  * Checks an image for refcount consistency.
9ae3a8
  *
9ae3a8
  * Returns 0 if no errors are found, the number of errors in case the image is
9ae3a8
@@ -1669,6 +1948,7 @@ int qcow2_check_refcounts(BlockDriverState *bs, BdrvCheckResult *res,
9ae3a8
                           BdrvCheckMode fix)
9ae3a8
 {
9ae3a8
     BDRVQcowState *s = bs->opaque;
9ae3a8
+    BdrvCheckResult pre_compare_res;
9ae3a8
     int64_t size, highest_cluster, nb_clusters;
9ae3a8
     uint16_t *refcount_table = NULL;
9ae3a8
     bool rebuild = false;
9ae3a8
@@ -1695,14 +1975,33 @@ int qcow2_check_refcounts(BlockDriverState *bs, BdrvCheckResult *res,
9ae3a8
         goto fail;
9ae3a8
     }
9ae3a8
 
9ae3a8
-    compare_refcounts(bs, res, fix, &rebuild, &highest_cluster, refcount_table,
9ae3a8
+    /* In case we don't need to rebuild the refcount structure (but want to fix
9ae3a8
+     * something), this function is immediately called again, in which case the
9ae3a8
+     * result should be ignored */
9ae3a8
+    pre_compare_res = *res;
9ae3a8
+    compare_refcounts(bs, res, 0, &rebuild, &highest_cluster, refcount_table,
9ae3a8
                       nb_clusters);
9ae3a8
 
9ae3a8
-    if (rebuild) {
9ae3a8
-        fprintf(stderr, "ERROR need to rebuild refcount structures\n");
9ae3a8
-        res->check_errors++;
9ae3a8
-        /* Just carry on, the rest does not rely on the on-disk refcount
9ae3a8
-         * structures */
9ae3a8
+    if (rebuild && (fix & BDRV_FIX_ERRORS)) {
9ae3a8
+        fprintf(stderr, "Rebuilding refcount structure\n");
9ae3a8
+        ret = rebuild_refcount_structure(bs, res, &refcount_table,
9ae3a8
+                                         &nb_clusters);
9ae3a8
+        if (ret < 0) {
9ae3a8
+            goto fail;
9ae3a8
+        }
9ae3a8
+    } else if (fix) {
9ae3a8
+        if (rebuild) {
9ae3a8
+            fprintf(stderr, "ERROR need to rebuild refcount structures\n");
9ae3a8
+            res->check_errors++;
9ae3a8
+            ret = -EIO;
9ae3a8
+            goto fail;
9ae3a8
+        }
9ae3a8
+
9ae3a8
+        if (res->leaks || res->corruptions) {
9ae3a8
+            *res = pre_compare_res;
9ae3a8
+            compare_refcounts(bs, res, fix, &rebuild, &highest_cluster,
9ae3a8
+                              refcount_table, nb_clusters);
9ae3a8
+        }
9ae3a8
     }
9ae3a8
 
9ae3a8
     /* check OFLAG_COPIED */
9ae3a8
-- 
9ae3a8
1.8.3.1
9ae3a8