9ae3a8
From 707d22d6fe9d2f43d0372472b62d40ddb652385d Mon Sep 17 00:00:00 2001
9ae3a8
From: Max Reitz <mreitz@redhat.com>
9ae3a8
Date: Sat, 13 Jun 2015 16:22:20 +0200
9ae3a8
Subject: [PATCH 26/42] qcow2: Fix refcount blocks beyond image end
9ae3a8
9ae3a8
Message-id: <1434212556-3927-27-git-send-email-mreitz@redhat.com>
9ae3a8
Patchwork-id: 66045
9ae3a8
O-Subject: [RHEL-7.2 qemu-kvm PATCH 26/42] qcow2: Fix refcount blocks beyond image end
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
If the qcow2 check function detects a refcount block located beyond the
9ae3a8
image end, grow the image appropriately. This cannot break anything and
9ae3a8
is the logical fix for such a case.
9ae3a8
9ae3a8
Signed-off-by: Max Reitz <mreitz@redhat.com>
9ae3a8
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
9ae3a8
(cherry picked from commit 001c158defb65e88e6c50c85d6f20501f7149ddd)
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 | 67 +++++++++++++++++++++++++++++++++++++++++++++++---
9ae3a8
 1 file changed, 63 insertions(+), 4 deletions(-)
9ae3a8
9ae3a8
diff --git a/block/qcow2-refcount.c b/block/qcow2-refcount.c
9ae3a8
index 8f1215c..8ce0447 100644
9ae3a8
--- a/block/qcow2-refcount.c
9ae3a8
+++ b/block/qcow2-refcount.c
9ae3a8
@@ -1545,7 +1545,7 @@ static int check_refblocks(BlockDriverState *bs, BdrvCheckResult *res,
9ae3a8
                            int64_t *nb_clusters)
9ae3a8
 {
9ae3a8
     BDRVQcowState *s = bs->opaque;
9ae3a8
-    int64_t i;
9ae3a8
+    int64_t i, size;
9ae3a8
     int ret;
9ae3a8
 
9ae3a8
     for(i = 0; i < s->refcount_table_size; i++) {
9ae3a8
@@ -1562,9 +1562,68 @@ static int check_refblocks(BlockDriverState *bs, BdrvCheckResult *res,
9ae3a8
         }
9ae3a8
 
9ae3a8
         if (cluster >= *nb_clusters) {
9ae3a8
-            fprintf(stderr, "ERROR refcount block %" PRId64
9ae3a8
-                    " is outside image\n", i);
9ae3a8
-            res->corruptions++;
9ae3a8
+            fprintf(stderr, "%s refcount block %" PRId64 " is outside image\n",
9ae3a8
+                    fix & BDRV_FIX_ERRORS ? "Repairing" : "ERROR", i);
9ae3a8
+
9ae3a8
+            if (fix & BDRV_FIX_ERRORS) {
9ae3a8
+                int64_t old_nb_clusters = *nb_clusters;
9ae3a8
+                uint16_t *new_refcount_table;
9ae3a8
+
9ae3a8
+                if (offset > INT64_MAX - s->cluster_size) {
9ae3a8
+                    ret = -EINVAL;
9ae3a8
+                    goto resize_fail;
9ae3a8
+                }
9ae3a8
+
9ae3a8
+                ret = bdrv_truncate(bs->file, offset + s->cluster_size);
9ae3a8
+                if (ret < 0) {
9ae3a8
+                    goto resize_fail;
9ae3a8
+                }
9ae3a8
+                size = bdrv_getlength(bs->file);
9ae3a8
+                if (size < 0) {
9ae3a8
+                    ret = size;
9ae3a8
+                    goto resize_fail;
9ae3a8
+                }
9ae3a8
+
9ae3a8
+                *nb_clusters = size_to_clusters(s, size);
9ae3a8
+                assert(*nb_clusters >= old_nb_clusters);
9ae3a8
+
9ae3a8
+                new_refcount_table = g_try_realloc(*refcount_table,
9ae3a8
+                                                   *nb_clusters *
9ae3a8
+                                                   sizeof(**refcount_table));
9ae3a8
+                if (!new_refcount_table) {
9ae3a8
+                    *nb_clusters = old_nb_clusters;
9ae3a8
+                    res->check_errors++;
9ae3a8
+                    return -ENOMEM;
9ae3a8
+                }
9ae3a8
+                *refcount_table = new_refcount_table;
9ae3a8
+
9ae3a8
+                memset(*refcount_table + old_nb_clusters, 0,
9ae3a8
+                       (*nb_clusters - old_nb_clusters) *
9ae3a8
+                       sizeof(**refcount_table));
9ae3a8
+
9ae3a8
+                if (cluster >= *nb_clusters) {
9ae3a8
+                    ret = -EINVAL;
9ae3a8
+                    goto resize_fail;
9ae3a8
+                }
9ae3a8
+
9ae3a8
+                res->corruptions_fixed++;
9ae3a8
+                ret = inc_refcounts(bs, res, refcount_table, nb_clusters,
9ae3a8
+                                    offset, s->cluster_size);
9ae3a8
+                if (ret < 0) {
9ae3a8
+                    return ret;
9ae3a8
+                }
9ae3a8
+                /* No need to check whether the refcount is now greater than 1:
9ae3a8
+                 * This area was just allocated and zeroed, so it can only be
9ae3a8
+                 * exactly 1 after inc_refcounts() */
9ae3a8
+                continue;
9ae3a8
+
9ae3a8
+resize_fail:
9ae3a8
+                res->corruptions++;
9ae3a8
+                fprintf(stderr, "ERROR could not resize image: %s\n",
9ae3a8
+                        strerror(-ret));
9ae3a8
+            } else {
9ae3a8
+                res->corruptions++;
9ae3a8
+            }
9ae3a8
             continue;
9ae3a8
         }
9ae3a8
 
9ae3a8
-- 
9ae3a8
1.8.3.1
9ae3a8