cryptospore / rpms / qemu-kvm

Forked from rpms/qemu-kvm 2 years ago
Clone

Blame SOURCES/kvm-qcow2-Rebuild-refcount-structure-during-check.patch

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