6443c2
From 3daca05a8f845d2a389a6cf767314bcb72109578 Mon Sep 17 00:00:00 2001
6443c2
From: Hanna Reitz <hreitz@redhat.com>
6443c2
Date: Tue, 5 Apr 2022 15:46:50 +0200
6443c2
Subject: [PATCH 08/11] qcow2: Improve refcount structure rebuilding
6443c2
6443c2
RH-Author: Hanna Reitz <hreitz@redhat.com>
6443c2
RH-MergeRequest: 173: qcow2: Improve refcount structure rebuilding
6443c2
RH-Commit: [1/4] 586e7a0fc3cb7cc2296b544ffcef34d8395fa74c
6443c2
RH-Bugzilla: 2072242
6443c2
RH-Acked-by: Miroslav Rezanina <mrezanin@redhat.com>
6443c2
RH-Acked-by: Eric Blake <eblake@redhat.com>
6443c2
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
6443c2
6443c2
When rebuilding the refcount structures (when qemu-img check -r found
6443c2
errors with refcount = 0, but reference count > 0), the new refcount
6443c2
table defaults to being put at the image file end[1].  There is no good
6443c2
reason for that except that it means we will not have to rewrite any
6443c2
refblocks we already wrote to disk.
6443c2
6443c2
Changing the code to rewrite those refblocks is not too difficult,
6443c2
though, so let us do that.  That is beneficial for images on block
6443c2
devices, where we cannot really write beyond the end of the image file.
6443c2
6443c2
Use this opportunity to add extensive comments to the code, and refactor
6443c2
it a bit, getting rid of the backwards-jumping goto.
6443c2
6443c2
[1] Unless there is something allocated in the area pointed to by the
6443c2
    last refblock, so we have to write that refblock.  In that case, we
6443c2
    try to put the reftable in there.
6443c2
6443c2
Buglink: https://bugzilla.redhat.com/show_bug.cgi?id=1519071
6443c2
Closes: https://gitlab.com/qemu-project/qemu/-/issues/941
6443c2
Reviewed-by: Eric Blake <eblake@redhat.com>
6443c2
Signed-off-by: Hanna Reitz <hreitz@redhat.com>
6443c2
Message-Id: <20220405134652.19278-2-hreitz@redhat.com>
6443c2
(cherry picked from commit a8c07ec287554dcefd33733f0e5888a281ddc95e)
6443c2
Signed-off-by: Hanna Reitz <hreitz@redhat.com>
6443c2
---
6443c2
 block/qcow2-refcount.c | 332 +++++++++++++++++++++++++++++------------
6443c2
 1 file changed, 235 insertions(+), 97 deletions(-)
6443c2
6443c2
diff --git a/block/qcow2-refcount.c b/block/qcow2-refcount.c
6443c2
index 4614572252..555d8ba5ac 100644
6443c2
--- a/block/qcow2-refcount.c
6443c2
+++ b/block/qcow2-refcount.c
6443c2
@@ -2435,111 +2435,140 @@ static int64_t alloc_clusters_imrt(BlockDriverState *bs,
6443c2
 }
6443c2
 
6443c2
 /*
6443c2
- * Creates a new refcount structure based solely on the in-memory information
6443c2
- * given through *refcount_table. All necessary allocations will be reflected
6443c2
- * in that array.
6443c2
+ * Helper function for rebuild_refcount_structure().
6443c2
  *
6443c2
- * On success, the old refcount structure is leaked (it will be covered by the
6443c2
- * new refcount structure).
6443c2
+ * Scan the range of clusters [first_cluster, end_cluster) for allocated
6443c2
+ * clusters and write all corresponding refblocks to disk.  The refblock
6443c2
+ * and allocation data is taken from the in-memory refcount table
6443c2
+ * *refcount_table[] (of size *nb_clusters), which is basically one big
6443c2
+ * (unlimited size) refblock for the whole image.
6443c2
+ *
6443c2
+ * For these refblocks, clusters are allocated using said in-memory
6443c2
+ * refcount table.  Care is taken that these allocations are reflected
6443c2
+ * in the refblocks written to disk.
6443c2
+ *
6443c2
+ * The refblocks' offsets are written into a reftable, which is
6443c2
+ * *on_disk_reftable_ptr[] (of size *on_disk_reftable_entries_ptr).  If
6443c2
+ * that reftable is of insufficient size, it will be resized to fit.
6443c2
+ * This reftable is not written to disk.
6443c2
+ *
6443c2
+ * (If *on_disk_reftable_ptr is not NULL, the entries within are assumed
6443c2
+ * to point to existing valid refblocks that do not need to be allocated
6443c2
+ * again.)
6443c2
+ *
6443c2
+ * Return whether the on-disk reftable array was resized (true/false),
6443c2
+ * or -errno on error.
6443c2
  */
6443c2
-static int rebuild_refcount_structure(BlockDriverState *bs,
6443c2
-                                      BdrvCheckResult *res,
6443c2
-                                      void **refcount_table,
6443c2
-                                      int64_t *nb_clusters)
6443c2
+static int rebuild_refcounts_write_refblocks(
6443c2
+        BlockDriverState *bs, void **refcount_table, int64_t *nb_clusters,
6443c2
+        int64_t first_cluster, int64_t end_cluster,
6443c2
+        uint64_t **on_disk_reftable_ptr, uint32_t *on_disk_reftable_entries_ptr
6443c2
+    )
6443c2
 {
6443c2
     BDRVQcow2State *s = bs->opaque;
6443c2
-    int64_t first_free_cluster = 0, reftable_offset = -1, cluster = 0;
6443c2
+    int64_t cluster;
6443c2
     int64_t refblock_offset, refblock_start, refblock_index;
6443c2
-    uint32_t reftable_size = 0;
6443c2
-    uint64_t *on_disk_reftable = NULL;
6443c2
+    int64_t first_free_cluster = 0;
6443c2
+    uint64_t *on_disk_reftable = *on_disk_reftable_ptr;
6443c2
+    uint32_t on_disk_reftable_entries = *on_disk_reftable_entries_ptr;
6443c2
     void *on_disk_refblock;
6443c2
-    int ret = 0;
6443c2
-    struct {
6443c2
-        uint64_t reftable_offset;
6443c2
-        uint32_t reftable_clusters;
6443c2
-    } QEMU_PACKED reftable_offset_and_clusters;
6443c2
-
6443c2
-    qcow2_cache_empty(bs, s->refcount_block_cache);
6443c2
+    bool reftable_grown = false;
6443c2
+    int ret;
6443c2
 
6443c2
-write_refblocks:
6443c2
-    for (; cluster < *nb_clusters; cluster++) {
6443c2
+    for (cluster = first_cluster; cluster < end_cluster; cluster++) {
6443c2
+        /* Check all clusters to find refblocks that contain non-zero entries */
6443c2
         if (!s->get_refcount(*refcount_table, cluster)) {
6443c2
             continue;
6443c2
         }
6443c2
 
6443c2
+        /*
6443c2
+         * This cluster is allocated, so we need to create a refblock
6443c2
+         * for it.  The data we will write to disk is just the
6443c2
+         * respective slice from *refcount_table, so it will contain
6443c2
+         * accurate refcounts for all clusters belonging to this
6443c2
+         * refblock.  After we have written it, we will therefore skip
6443c2
+         * all remaining clusters in this refblock.
6443c2
+         */
6443c2
+
6443c2
         refblock_index = cluster >> s->refcount_block_bits;
6443c2
         refblock_start = refblock_index << s->refcount_block_bits;
6443c2
 
6443c2
-        /* Don't allocate a cluster in a refblock already written to disk */
6443c2
-        if (first_free_cluster < refblock_start) {
6443c2
-            first_free_cluster = refblock_start;
6443c2
-        }
6443c2
-        refblock_offset = alloc_clusters_imrt(bs, 1, refcount_table,
6443c2
-                                              nb_clusters, &first_free_cluster);
6443c2
-        if (refblock_offset < 0) {
6443c2
-            fprintf(stderr, "ERROR allocating refblock: %s\n",
6443c2
-                    strerror(-refblock_offset));
6443c2
-            res->check_errors++;
6443c2
-            ret = refblock_offset;
6443c2
-            goto fail;
6443c2
-        }
6443c2
+        if (on_disk_reftable_entries > refblock_index &&
6443c2
+            on_disk_reftable[refblock_index])
6443c2
+        {
6443c2
+            /*
6443c2
+             * We can get here after a `goto write_refblocks`: We have a
6443c2
+             * reftable from a previous run, and the refblock is already
6443c2
+             * allocated.  No need to allocate it again.
6443c2
+             */
6443c2
+            refblock_offset = on_disk_reftable[refblock_index];
6443c2
+        } else {
6443c2
+            int64_t refblock_cluster_index;
6443c2
 
6443c2
-        if (reftable_size <= refblock_index) {
6443c2
-            uint32_t old_reftable_size = reftable_size;
6443c2
-            uint64_t *new_on_disk_reftable;
6443c2
+            /* Don't allocate a cluster in a refblock already written to disk */
6443c2
+            if (first_free_cluster < refblock_start) {
6443c2
+                first_free_cluster = refblock_start;
6443c2
+            }
6443c2
+            refblock_offset = alloc_clusters_imrt(bs, 1, refcount_table,
6443c2
+                                                  nb_clusters,
6443c2
+                                                  &first_free_cluster);
6443c2
+            if (refblock_offset < 0) {
6443c2
+                fprintf(stderr, "ERROR allocating refblock: %s\n",
6443c2
+                        strerror(-refblock_offset));
6443c2
+                return refblock_offset;
6443c2
+            }
6443c2
 
6443c2
-            reftable_size = ROUND_UP((refblock_index + 1) * REFTABLE_ENTRY_SIZE,
6443c2
-                                     s->cluster_size) / REFTABLE_ENTRY_SIZE;
6443c2
-            new_on_disk_reftable = g_try_realloc(on_disk_reftable,
6443c2
-                                                 reftable_size *
6443c2
-                                                 REFTABLE_ENTRY_SIZE);
6443c2
-            if (!new_on_disk_reftable) {
6443c2
-                res->check_errors++;
6443c2
-                ret = -ENOMEM;
6443c2
-                goto fail;
6443c2
+            refblock_cluster_index = refblock_offset / s->cluster_size;
6443c2
+            if (refblock_cluster_index >= end_cluster) {
6443c2
+                /*
6443c2
+                 * We must write the refblock that holds this refblock's
6443c2
+                 * refcount
6443c2
+                 */
6443c2
+                end_cluster = refblock_cluster_index + 1;
6443c2
             }
6443c2
-            on_disk_reftable = new_on_disk_reftable;
6443c2
 
6443c2
-            memset(on_disk_reftable + old_reftable_size, 0,
6443c2
-                   (reftable_size - old_reftable_size) * REFTABLE_ENTRY_SIZE);
6443c2
+            if (on_disk_reftable_entries <= refblock_index) {
6443c2
+                on_disk_reftable_entries =
6443c2
+                    ROUND_UP((refblock_index + 1) * REFTABLE_ENTRY_SIZE,
6443c2
+                             s->cluster_size) / REFTABLE_ENTRY_SIZE;
6443c2
+                on_disk_reftable =
6443c2
+                    g_try_realloc(on_disk_reftable,
6443c2
+                                  on_disk_reftable_entries *
6443c2
+                                  REFTABLE_ENTRY_SIZE);
6443c2
+                if (!on_disk_reftable) {
6443c2
+                    return -ENOMEM;
6443c2
+                }
6443c2
 
6443c2
-            /* The offset we have for the reftable is now no longer valid;
6443c2
-             * this will leak that range, but we can easily fix that by running
6443c2
-             * a leak-fixing check after this rebuild operation */
6443c2
-            reftable_offset = -1;
6443c2
-        } else {
6443c2
-            assert(on_disk_reftable);
6443c2
-        }
6443c2
-        on_disk_reftable[refblock_index] = refblock_offset;
6443c2
+                memset(on_disk_reftable + *on_disk_reftable_entries_ptr, 0,
6443c2
+                       (on_disk_reftable_entries -
6443c2
+                        *on_disk_reftable_entries_ptr) *
6443c2
+                       REFTABLE_ENTRY_SIZE);
6443c2
 
6443c2
-        /* If this is apparently the last refblock (for now), try to squeeze the
6443c2
-         * reftable in */
6443c2
-        if (refblock_index == (*nb_clusters - 1) >> s->refcount_block_bits &&
6443c2
-            reftable_offset < 0)
6443c2
-        {
6443c2
-            uint64_t reftable_clusters = size_to_clusters(s, reftable_size *
6443c2
-                                                          REFTABLE_ENTRY_SIZE);
6443c2
-            reftable_offset = alloc_clusters_imrt(bs, reftable_clusters,
6443c2
-                                                  refcount_table, nb_clusters,
6443c2
-                                                  &first_free_cluster);
6443c2
-            if (reftable_offset < 0) {
6443c2
-                fprintf(stderr, "ERROR allocating reftable: %s\n",
6443c2
-                        strerror(-reftable_offset));
6443c2
-                res->check_errors++;
6443c2
-                ret = reftable_offset;
6443c2
-                goto fail;
6443c2
+                *on_disk_reftable_ptr = on_disk_reftable;
6443c2
+                *on_disk_reftable_entries_ptr = on_disk_reftable_entries;
6443c2
+
6443c2
+                reftable_grown = true;
6443c2
+            } else {
6443c2
+                assert(on_disk_reftable);
6443c2
             }
6443c2
+            on_disk_reftable[refblock_index] = refblock_offset;
6443c2
         }
6443c2
 
6443c2
+        /* Refblock is allocated, write it to disk */
6443c2
+
6443c2
         ret = qcow2_pre_write_overlap_check(bs, 0, refblock_offset,
6443c2
                                             s->cluster_size, false);
6443c2
         if (ret < 0) {
6443c2
             fprintf(stderr, "ERROR writing refblock: %s\n", strerror(-ret));
6443c2
-            goto fail;
6443c2
+            return ret;
6443c2
         }
6443c2
 
6443c2
-        /* The size of *refcount_table is always cluster-aligned, therefore the
6443c2
-         * write operation will not overflow */
6443c2
+        /*
6443c2
+         * The refblock is simply a slice of *refcount_table.
6443c2
+         * Note that the size of *refcount_table is always aligned to
6443c2
+         * whole clusters, so the write operation will not result in
6443c2
+         * out-of-bounds accesses.
6443c2
+         */
6443c2
         on_disk_refblock = (void *)((char *) *refcount_table +
6443c2
                                     refblock_index * s->cluster_size);
6443c2
 
6443c2
@@ -2547,23 +2576,99 @@ write_refblocks:
6443c2
                           s->cluster_size);
6443c2
         if (ret < 0) {
6443c2
             fprintf(stderr, "ERROR writing refblock: %s\n", strerror(-ret));
6443c2
-            goto fail;
6443c2
+            return ret;
6443c2
         }
6443c2
 
6443c2
-        /* Go to the end of this refblock */
6443c2
+        /* This refblock is done, skip to its end */
6443c2
         cluster = refblock_start + s->refcount_block_size - 1;
6443c2
     }
6443c2
 
6443c2
-    if (reftable_offset < 0) {
6443c2
-        uint64_t post_refblock_start, reftable_clusters;
6443c2
+    return reftable_grown;
6443c2
+}
6443c2
+
6443c2
+/*
6443c2
+ * Creates a new refcount structure based solely on the in-memory information
6443c2
+ * given through *refcount_table (this in-memory information is basically just
6443c2
+ * the concatenation of all refblocks).  All necessary allocations will be
6443c2
+ * reflected in that array.
6443c2
+ *
6443c2
+ * On success, the old refcount structure is leaked (it will be covered by the
6443c2
+ * new refcount structure).
6443c2
+ */
6443c2
+static int rebuild_refcount_structure(BlockDriverState *bs,
6443c2
+                                      BdrvCheckResult *res,
6443c2
+                                      void **refcount_table,
6443c2
+                                      int64_t *nb_clusters)
6443c2
+{
6443c2
+    BDRVQcow2State *s = bs->opaque;
6443c2
+    int64_t reftable_offset = -1;
6443c2
+    int64_t reftable_length = 0;
6443c2
+    int64_t reftable_clusters;
6443c2
+    int64_t refblock_index;
6443c2
+    uint32_t on_disk_reftable_entries = 0;
6443c2
+    uint64_t *on_disk_reftable = NULL;
6443c2
+    int ret = 0;
6443c2
+    int reftable_size_changed = 0;
6443c2
+    struct {
6443c2
+        uint64_t reftable_offset;
6443c2
+        uint32_t reftable_clusters;
6443c2
+    } QEMU_PACKED reftable_offset_and_clusters;
6443c2
+
6443c2
+    qcow2_cache_empty(bs, s->refcount_block_cache);
6443c2
+
6443c2
+    /*
6443c2
+     * For each refblock containing entries, we try to allocate a
6443c2
+     * cluster (in the in-memory refcount table) and write its offset
6443c2
+     * into on_disk_reftable[].  We then write the whole refblock to
6443c2
+     * disk (as a slice of the in-memory refcount table).
6443c2
+     * This is done by rebuild_refcounts_write_refblocks().
6443c2
+     *
6443c2
+     * Once we have scanned all clusters, we try to find space for the
6443c2
+     * reftable.  This will dirty the in-memory refcount table (i.e.
6443c2
+     * make it differ from the refblocks we have already written), so we
6443c2
+     * need to run rebuild_refcounts_write_refblocks() again for the
6443c2
+     * range of clusters where the reftable has been allocated.
6443c2
+     *
6443c2
+     * This second run might make the reftable grow again, in which case
6443c2
+     * we will need to allocate another space for it, which is why we
6443c2
+     * repeat all this until the reftable stops growing.
6443c2
+     *
6443c2
+     * (This loop will terminate, because with every cluster the
6443c2
+     * reftable grows, it can accomodate a multitude of more refcounts,
6443c2
+     * so that at some point this must be able to cover the reftable
6443c2
+     * and all refblocks describing it.)
6443c2
+     *
6443c2
+     * We then convert the reftable to big-endian and write it to disk.
6443c2
+     *
6443c2
+     * Note that we never free any reftable allocations.  Doing so would
6443c2
+     * needlessly complicate the algorithm: The eventual second check
6443c2
+     * run we do will clean up all leaks we have caused.
6443c2
+     */
6443c2
+
6443c2
+    reftable_size_changed =
6443c2
+        rebuild_refcounts_write_refblocks(bs, refcount_table, nb_clusters,
6443c2
+                                          0, *nb_clusters,
6443c2
+                                          &on_disk_reftable,
6443c2
+                                          &on_disk_reftable_entries);
6443c2
+    if (reftable_size_changed < 0) {
6443c2
+        res->check_errors++;
6443c2
+        ret = reftable_size_changed;
6443c2
+        goto fail;
6443c2
+    }
6443c2
+
6443c2
+    /*
6443c2
+     * There was no reftable before, so rebuild_refcounts_write_refblocks()
6443c2
+     * must have increased its size (from 0 to something).
6443c2
+     */
6443c2
+    assert(reftable_size_changed);
6443c2
+
6443c2
+    do {
6443c2
+        int64_t reftable_start_cluster, reftable_end_cluster;
6443c2
+        int64_t first_free_cluster = 0;
6443c2
+
6443c2
+        reftable_length = on_disk_reftable_entries * REFTABLE_ENTRY_SIZE;
6443c2
+        reftable_clusters = size_to_clusters(s, reftable_length);
6443c2
 
6443c2
-        post_refblock_start = ROUND_UP(*nb_clusters, s->refcount_block_size);
6443c2
-        reftable_clusters =
6443c2
-            size_to_clusters(s, reftable_size * REFTABLE_ENTRY_SIZE);
6443c2
-        /* Not pretty but simple */
6443c2
-        if (first_free_cluster < post_refblock_start) {
6443c2
-            first_free_cluster = post_refblock_start;
6443c2
-        }
6443c2
         reftable_offset = alloc_clusters_imrt(bs, reftable_clusters,
6443c2
                                               refcount_table, nb_clusters,
6443c2
                                               &first_free_cluster);
6443c2
@@ -2575,24 +2680,55 @@ write_refblocks:
6443c2
             goto fail;
6443c2
         }
6443c2
 
6443c2
-        goto write_refblocks;
6443c2
-    }
6443c2
+        /*
6443c2
+         * We need to update the affected refblocks, so re-run the
6443c2
+         * write_refblocks loop for the reftable's range of clusters.
6443c2
+         */
6443c2
+        assert(offset_into_cluster(s, reftable_offset) == 0);
6443c2
+        reftable_start_cluster = reftable_offset / s->cluster_size;
6443c2
+        reftable_end_cluster = reftable_start_cluster + reftable_clusters;
6443c2
+        reftable_size_changed =
6443c2
+            rebuild_refcounts_write_refblocks(bs, refcount_table, nb_clusters,
6443c2
+                                              reftable_start_cluster,
6443c2
+                                              reftable_end_cluster,
6443c2
+                                              &on_disk_reftable,
6443c2
+                                              &on_disk_reftable_entries);
6443c2
+        if (reftable_size_changed < 0) {
6443c2
+            res->check_errors++;
6443c2
+            ret = reftable_size_changed;
6443c2
+            goto fail;
6443c2
+        }
6443c2
+
6443c2
+        /*
6443c2
+         * If the reftable size has changed, we will need to find a new
6443c2
+         * allocation, repeating the loop.
6443c2
+         */
6443c2
+    } while (reftable_size_changed);
6443c2
 
6443c2
-    for (refblock_index = 0; refblock_index < reftable_size; refblock_index++) {
6443c2
+    /* The above loop must have run at least once */
6443c2
+    assert(reftable_offset >= 0);
6443c2
+
6443c2
+    /*
6443c2
+     * All allocations are done, all refblocks are written, convert the
6443c2
+     * reftable to big-endian and write it to disk.
6443c2
+     */
6443c2
+
6443c2
+    for (refblock_index = 0; refblock_index < on_disk_reftable_entries;
6443c2
+         refblock_index++)
6443c2
+    {
6443c2
         cpu_to_be64s(&on_disk_reftable[refblock_index]);
6443c2
     }
6443c2
 
6443c2
-    ret = qcow2_pre_write_overlap_check(bs, 0, reftable_offset,
6443c2
-                                        reftable_size * REFTABLE_ENTRY_SIZE,
6443c2
+    ret = qcow2_pre_write_overlap_check(bs, 0, reftable_offset, reftable_length,
6443c2
                                         false);
6443c2
     if (ret < 0) {
6443c2
         fprintf(stderr, "ERROR writing reftable: %s\n", strerror(-ret));
6443c2
         goto fail;
6443c2
     }
6443c2
 
6443c2
-    assert(reftable_size < INT_MAX / REFTABLE_ENTRY_SIZE);
6443c2
+    assert(reftable_length < INT_MAX);
6443c2
     ret = bdrv_pwrite(bs->file, reftable_offset, on_disk_reftable,
6443c2
-                      reftable_size * REFTABLE_ENTRY_SIZE);
6443c2
+                      reftable_length);
6443c2
     if (ret < 0) {
6443c2
         fprintf(stderr, "ERROR writing reftable: %s\n", strerror(-ret));
6443c2
         goto fail;
6443c2
@@ -2601,7 +2737,7 @@ write_refblocks:
6443c2
     /* Enter new reftable into the image header */
6443c2
     reftable_offset_and_clusters.reftable_offset = cpu_to_be64(reftable_offset);
6443c2
     reftable_offset_and_clusters.reftable_clusters =
6443c2
-        cpu_to_be32(size_to_clusters(s, reftable_size * REFTABLE_ENTRY_SIZE));
6443c2
+        cpu_to_be32(reftable_clusters);
6443c2
     ret = bdrv_pwrite_sync(bs->file,
6443c2
                            offsetof(QCowHeader, refcount_table_offset),
6443c2
                            &reftable_offset_and_clusters,
6443c2
@@ -2611,12 +2747,14 @@ write_refblocks:
6443c2
         goto fail;
6443c2
     }
6443c2
 
6443c2
-    for (refblock_index = 0; refblock_index < reftable_size; refblock_index++) {
6443c2
+    for (refblock_index = 0; refblock_index < on_disk_reftable_entries;
6443c2
+         refblock_index++)
6443c2
+    {
6443c2
         be64_to_cpus(&on_disk_reftable[refblock_index]);
6443c2
     }
6443c2
     s->refcount_table = on_disk_reftable;
6443c2
     s->refcount_table_offset = reftable_offset;
6443c2
-    s->refcount_table_size = reftable_size;
6443c2
+    s->refcount_table_size = on_disk_reftable_entries;
6443c2
     update_max_refcount_table_index(s);
6443c2
 
6443c2
     return 0;
6443c2
-- 
6443c2
2.27.0
6443c2