9ae3a8
From f9a81072210afed390c21465ba973fc557105b79 Mon Sep 17 00:00:00 2001
9ae3a8
From: Max Reitz <mreitz@redhat.com>
9ae3a8
Date: Sat, 13 Jun 2015 16:22:18 +0200
9ae3a8
Subject: [PATCH 24/42] qcow2: Let inc_refcounts() resize the reftable
9ae3a8
9ae3a8
Message-id: <1434212556-3927-25-git-send-email-mreitz@redhat.com>
9ae3a8
Patchwork-id: 66043
9ae3a8
O-Subject: [RHEL-7.2 qemu-kvm PATCH 24/42] qcow2: Let inc_refcounts() resize the reftable
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
Now that the refcount table can be passed around by reference, do that
9ae3a8
for inc_refcounts() (and subsequently check_refcounts_l1() and
9ae3a8
check_refcounts_l2()) and use it for resizing it when a cluster after
9ae3a8
the image end is encountered.
9ae3a8
9ae3a8
Signed-off-by: Max Reitz <mreitz@redhat.com>
9ae3a8
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
9ae3a8
(cherry picked from commit 641bb63cd6b003ab0ca2e312a014449037d71647)
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 | 57 +++++++++++++++++++++++++++++++-------------------
9ae3a8
 1 file changed, 35 insertions(+), 22 deletions(-)
9ae3a8
9ae3a8
diff --git a/block/qcow2-refcount.c b/block/qcow2-refcount.c
9ae3a8
index 4655fa2..7d03e65 100644
9ae3a8
--- a/block/qcow2-refcount.c
9ae3a8
+++ b/block/qcow2-refcount.c
9ae3a8
@@ -1055,8 +1055,8 @@ fail:
9ae3a8
  */
9ae3a8
 static int inc_refcounts(BlockDriverState *bs,
9ae3a8
                          BdrvCheckResult *res,
9ae3a8
-                         uint16_t *refcount_table,
9ae3a8
-                         int64_t refcount_table_size,
9ae3a8
+                         uint16_t **refcount_table,
9ae3a8
+                         int64_t *refcount_table_size,
9ae3a8
                          int64_t offset, int64_t size)
9ae3a8
 {
9ae3a8
     BDRVQcowState *s = bs->opaque;
9ae3a8
@@ -1071,17 +1071,30 @@ static int inc_refcounts(BlockDriverState *bs,
9ae3a8
     for(cluster_offset = start; cluster_offset <= last;
9ae3a8
         cluster_offset += s->cluster_size) {
9ae3a8
         k = cluster_offset >> s->cluster_bits;
9ae3a8
-        if (k >= refcount_table_size) {
9ae3a8
-            fprintf(stderr, "Warning: cluster offset=0x%" PRIx64 " is after "
9ae3a8
-                "the end of the image file, can't properly check refcounts.\n",
9ae3a8
-                cluster_offset);
9ae3a8
-            res->check_errors++;
9ae3a8
-        } else {
9ae3a8
-            if (++refcount_table[k] == 0) {
9ae3a8
-                fprintf(stderr, "ERROR: overflow cluster offset=0x%" PRIx64
9ae3a8
-                    "\n", cluster_offset);
9ae3a8
-                res->corruptions++;
9ae3a8
+        if (k >= *refcount_table_size) {
9ae3a8
+            int64_t old_refcount_table_size = *refcount_table_size;
9ae3a8
+            uint16_t *new_refcount_table;
9ae3a8
+
9ae3a8
+            *refcount_table_size = k + 1;
9ae3a8
+            new_refcount_table = g_try_realloc(*refcount_table,
9ae3a8
+                                               *refcount_table_size *
9ae3a8
+                                               sizeof(**refcount_table));
9ae3a8
+            if (!new_refcount_table) {
9ae3a8
+                *refcount_table_size = old_refcount_table_size;
9ae3a8
+                res->check_errors++;
9ae3a8
+                return -ENOMEM;
9ae3a8
             }
9ae3a8
+            *refcount_table = new_refcount_table;
9ae3a8
+
9ae3a8
+            memset(*refcount_table + old_refcount_table_size, 0,
9ae3a8
+                   (*refcount_table_size - old_refcount_table_size) *
9ae3a8
+                   sizeof(**refcount_table));
9ae3a8
+        }
9ae3a8
+
9ae3a8
+        if (++(*refcount_table)[k] == 0) {
9ae3a8
+            fprintf(stderr, "ERROR: overflow cluster offset=0x%" PRIx64
9ae3a8
+                    "\n", cluster_offset);
9ae3a8
+            res->corruptions++;
9ae3a8
         }
9ae3a8
     }
9ae3a8
 
9ae3a8
@@ -1102,7 +1115,7 @@ enum {
9ae3a8
  * error occurred.
9ae3a8
  */
9ae3a8
 static int check_refcounts_l2(BlockDriverState *bs, BdrvCheckResult *res,
9ae3a8
-    uint16_t *refcount_table, int64_t refcount_table_size, int64_t l2_offset,
9ae3a8
+    uint16_t **refcount_table, int64_t *refcount_table_size, int64_t l2_offset,
9ae3a8
     int flags)
9ae3a8
 {
9ae3a8
     BDRVQcowState *s = bs->opaque;
9ae3a8
@@ -1220,8 +1233,8 @@ fail:
9ae3a8
  */
9ae3a8
 static int check_refcounts_l1(BlockDriverState *bs,
9ae3a8
                               BdrvCheckResult *res,
9ae3a8
-                              uint16_t *refcount_table,
9ae3a8
-                              int64_t refcount_table_size,
9ae3a8
+                              uint16_t **refcount_table,
9ae3a8
+                              int64_t *refcount_table_size,
9ae3a8
                               int64_t l1_table_offset, int l1_size,
9ae3a8
                               int flags)
9ae3a8
 {
9ae3a8
@@ -1556,7 +1569,7 @@ static int check_refblocks(BlockDriverState *bs, BdrvCheckResult *res,
9ae3a8
         }
9ae3a8
 
9ae3a8
         if (offset != 0) {
9ae3a8
-            ret = inc_refcounts(bs, res, *refcount_table, *nb_clusters,
9ae3a8
+            ret = inc_refcounts(bs, res, refcount_table, nb_clusters,
9ae3a8
                                 offset, s->cluster_size);
9ae3a8
             if (ret < 0) {
9ae3a8
                 return ret;
9ae3a8
@@ -1589,7 +1602,7 @@ static int check_refblocks(BlockDriverState *bs, BdrvCheckResult *res,
9ae3a8
                                sizeof(**refcount_table));
9ae3a8
                     }
9ae3a8
                     (*refcount_table)[cluster]--;
9ae3a8
-                    ret = inc_refcounts(bs, res, *refcount_table, *nb_clusters,
9ae3a8
+                    ret = inc_refcounts(bs, res, refcount_table, nb_clusters,
9ae3a8
                                         new_offset, s->cluster_size);
9ae3a8
                     if (ret < 0) {
9ae3a8
                         return ret;
9ae3a8
@@ -1625,14 +1638,14 @@ static int calculate_refcounts(BlockDriverState *bs, BdrvCheckResult *res,
9ae3a8
     }
9ae3a8
 
9ae3a8
     /* header */
9ae3a8
-    ret = inc_refcounts(bs, res, *refcount_table, *nb_clusters,
9ae3a8
+    ret = inc_refcounts(bs, res, refcount_table, nb_clusters,
9ae3a8
                         0, s->cluster_size);
9ae3a8
     if (ret < 0) {
9ae3a8
         return ret;
9ae3a8
     }
9ae3a8
 
9ae3a8
     /* current L1 table */
9ae3a8
-    ret = check_refcounts_l1(bs, res, *refcount_table, *nb_clusters,
9ae3a8
+    ret = check_refcounts_l1(bs, res, refcount_table, nb_clusters,
9ae3a8
                              s->l1_table_offset, s->l1_size, CHECK_FRAG_INFO);
9ae3a8
     if (ret < 0) {
9ae3a8
         return ret;
9ae3a8
@@ -1641,20 +1654,20 @@ static int calculate_refcounts(BlockDriverState *bs, BdrvCheckResult *res,
9ae3a8
     /* snapshots */
9ae3a8
     for (i = 0; i < s->nb_snapshots; i++) {
9ae3a8
         sn = s->snapshots + i;
9ae3a8
-        ret = check_refcounts_l1(bs, res, *refcount_table, *nb_clusters,
9ae3a8
+        ret = check_refcounts_l1(bs, res, refcount_table, nb_clusters,
9ae3a8
                                  sn->l1_table_offset, sn->l1_size, 0);
9ae3a8
         if (ret < 0) {
9ae3a8
             return ret;
9ae3a8
         }
9ae3a8
     }
9ae3a8
-    ret = inc_refcounts(bs, res, *refcount_table, *nb_clusters,
9ae3a8
+    ret = inc_refcounts(bs, res, refcount_table, nb_clusters,
9ae3a8
                         s->snapshots_offset, s->snapshots_size);
9ae3a8
     if (ret < 0) {
9ae3a8
         return ret;
9ae3a8
     }
9ae3a8
 
9ae3a8
     /* refcount data */
9ae3a8
-    ret = inc_refcounts(bs, res, *refcount_table, *nb_clusters,
9ae3a8
+    ret = inc_refcounts(bs, res, refcount_table, nb_clusters,
9ae3a8
                         s->refcount_table_offset,
9ae3a8
                         s->refcount_table_size * sizeof(uint64_t));
9ae3a8
     if (ret < 0) {
9ae3a8
-- 
9ae3a8
1.8.3.1
9ae3a8