9ae3a8
From 27a96310e27b1b3880935430d58330a90ccc0176 Mon Sep 17 00:00:00 2001
9ae3a8
From: Max Reitz <mreitz@redhat.com>
9ae3a8
Date: Mon, 4 Nov 2013 22:32:02 +0100
9ae3a8
Subject: [PATCH 09/87] qcow2-refcount: Move OFLAG_COPIED checks
9ae3a8
9ae3a8
RH-Author: Max Reitz <mreitz@redhat.com>
9ae3a8
Message-id: <1383604354-12743-12-git-send-email-mreitz@redhat.com>
9ae3a8
Patchwork-id: 55311
9ae3a8
O-Subject: [RHEL-7.0 qemu-kvm PATCH 11/43] qcow2-refcount: Move OFLAG_COPIED checks
9ae3a8
Bugzilla: 1004347
9ae3a8
RH-Acked-by: Kevin Wolf <kwolf@redhat.com>
9ae3a8
RH-Acked-by: Laszlo Ersek <lersek@redhat.com>
9ae3a8
RH-Acked-by: Fam Zheng <famz@redhat.com>
9ae3a8
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
9ae3a8
9ae3a8
BZ: 1004347
9ae3a8
9ae3a8
Move the OFLAG_COPIED checks out of check_refcounts_l1 and
9ae3a8
check_refcounts_l2 and after the actual refcount checks/fixes (since the
9ae3a8
refcounts might actually change there).
9ae3a8
9ae3a8
Signed-off-by: Max Reitz <mreitz@redhat.com>
9ae3a8
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
9ae3a8
(cherry picked from commit 4f6ed88c03c4026e31ce152ea760a0da839f0dda)
9ae3a8
9ae3a8
Signed-off-by: Max Reitz <mreitz@redhat.com>
9ae3a8
---
9ae3a8
 block/qcow2-refcount.c | 115 +++++++++++++++++++++++++++++++++++--------------
9ae3a8
 1 file changed, 82 insertions(+), 33 deletions(-)
9ae3a8
9ae3a8
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
9ae3a8
---
9ae3a8
 block/qcow2-refcount.c |  115 ++++++++++++++++++++++++++++++++++--------------
9ae3a8
 1 files changed, 82 insertions(+), 33 deletions(-)
9ae3a8
9ae3a8
diff --git a/block/qcow2-refcount.c b/block/qcow2-refcount.c
9ae3a8
index 310efcc..ddc3029 100644
9ae3a8
--- a/block/qcow2-refcount.c
9ae3a8
+++ b/block/qcow2-refcount.c
9ae3a8
@@ -1035,7 +1035,7 @@ static int check_refcounts_l2(BlockDriverState *bs, BdrvCheckResult *res,
9ae3a8
     BDRVQcowState *s = bs->opaque;
9ae3a8
     uint64_t *l2_table, l2_entry;
9ae3a8
     uint64_t next_contiguous_offset = 0;
9ae3a8
-    int i, l2_size, nb_csectors, refcount;
9ae3a8
+    int i, l2_size, nb_csectors;
9ae3a8
 
9ae3a8
     /* Read L2 table from disk */
9ae3a8
     l2_size = s->l2_size * sizeof(uint64_t);
9ae3a8
@@ -1087,23 +1087,8 @@ static int check_refcounts_l2(BlockDriverState *bs, BdrvCheckResult *res,
9ae3a8
 
9ae3a8
         case QCOW2_CLUSTER_NORMAL:
9ae3a8
         {
9ae3a8
-            /* QCOW_OFLAG_COPIED must be set iff refcount == 1 */
9ae3a8
             uint64_t offset = l2_entry & L2E_OFFSET_MASK;
9ae3a8
 
9ae3a8
-            if (flags & CHECK_OFLAG_COPIED) {
9ae3a8
-                refcount = get_refcount(bs, offset >> s->cluster_bits);
9ae3a8
-                if (refcount < 0) {
9ae3a8
-                    fprintf(stderr, "Can't get refcount for offset %"
9ae3a8
-                        PRIx64 ": %s\n", l2_entry, strerror(-refcount));
9ae3a8
-                    goto fail;
9ae3a8
-                }
9ae3a8
-                if ((refcount == 1) != ((l2_entry & QCOW_OFLAG_COPIED) != 0)) {
9ae3a8
-                    fprintf(stderr, "ERROR OFLAG_COPIED: offset=%"
9ae3a8
-                        PRIx64 " refcount=%d\n", l2_entry, refcount);
9ae3a8
-                    res->corruptions++;
9ae3a8
-                }
9ae3a8
-            }
9ae3a8
-
9ae3a8
             if (flags & CHECK_FRAG_INFO) {
9ae3a8
                 res->bfi.allocated_clusters++;
9ae3a8
                 if (next_contiguous_offset &&
9ae3a8
@@ -1160,7 +1145,7 @@ static int check_refcounts_l1(BlockDriverState *bs,
9ae3a8
 {
9ae3a8
     BDRVQcowState *s = bs->opaque;
9ae3a8
     uint64_t *l1_table, l2_offset, l1_size2;
9ae3a8
-    int i, refcount, ret;
9ae3a8
+    int i, ret;
9ae3a8
 
9ae3a8
     l1_size2 = l1_size * sizeof(uint64_t);
9ae3a8
 
9ae3a8
@@ -1184,22 +1169,6 @@ static int check_refcounts_l1(BlockDriverState *bs,
9ae3a8
     for(i = 0; i < l1_size; i++) {
9ae3a8
         l2_offset = l1_table[i];
9ae3a8
         if (l2_offset) {
9ae3a8
-            /* QCOW_OFLAG_COPIED must be set iff refcount == 1 */
9ae3a8
-            if (flags & CHECK_OFLAG_COPIED) {
9ae3a8
-                refcount = get_refcount(bs, (l2_offset & ~QCOW_OFLAG_COPIED)
9ae3a8
-                    >> s->cluster_bits);
9ae3a8
-                if (refcount < 0) {
9ae3a8
-                    fprintf(stderr, "Can't get refcount for l2_offset %"
9ae3a8
-                        PRIx64 ": %s\n", l2_offset, strerror(-refcount));
9ae3a8
-                    goto fail;
9ae3a8
-                }
9ae3a8
-                if ((refcount == 1) != ((l2_offset & QCOW_OFLAG_COPIED) != 0)) {
9ae3a8
-                    fprintf(stderr, "ERROR OFLAG_COPIED: l2_offset=%" PRIx64
9ae3a8
-                        " refcount=%d\n", l2_offset, refcount);
9ae3a8
-                    res->corruptions++;
9ae3a8
-                }
9ae3a8
-            }
9ae3a8
-
9ae3a8
             /* Mark L2 table as used */
9ae3a8
             l2_offset &= L1E_OFFSET_MASK;
9ae3a8
             inc_refcounts(bs, res, refcount_table, refcount_table_size,
9ae3a8
@@ -1231,6 +1200,80 @@ fail:
9ae3a8
 }
9ae3a8
 
9ae3a8
 /*
9ae3a8
+ * Checks the OFLAG_COPIED flag for all L1 and L2 entries.
9ae3a8
+ *
9ae3a8
+ * This function does not print an error message nor does it increment
9ae3a8
+ * check_errors if get_refcount fails (this is because such an error will have
9ae3a8
+ * been already detected and sufficiently signaled by the calling function
9ae3a8
+ * (qcow2_check_refcounts) by the time this function is called).
9ae3a8
+ */
9ae3a8
+static int check_oflag_copied(BlockDriverState *bs, BdrvCheckResult *res)
9ae3a8
+{
9ae3a8
+    BDRVQcowState *s = bs->opaque;
9ae3a8
+    uint64_t *l2_table = qemu_blockalign(bs, s->cluster_size);
9ae3a8
+    int ret;
9ae3a8
+    int refcount;
9ae3a8
+    int i, j;
9ae3a8
+
9ae3a8
+    for (i = 0; i < s->l1_size; i++) {
9ae3a8
+        uint64_t l1_entry = s->l1_table[i];
9ae3a8
+        uint64_t l2_offset = l1_entry & L1E_OFFSET_MASK;
9ae3a8
+
9ae3a8
+        if (!l2_offset) {
9ae3a8
+            continue;
9ae3a8
+        }
9ae3a8
+
9ae3a8
+        refcount = get_refcount(bs, l2_offset >> s->cluster_bits);
9ae3a8
+        if (refcount < 0) {
9ae3a8
+            /* don't print message nor increment check_errors */
9ae3a8
+            continue;
9ae3a8
+        }
9ae3a8
+        if ((refcount == 1) != ((l1_entry & QCOW_OFLAG_COPIED) != 0)) {
9ae3a8
+            fprintf(stderr, "ERROR OFLAG_COPIED L2 cluster: l1_index=%d "
9ae3a8
+                    "l1_entry=%" PRIx64 " refcount=%d\n",
9ae3a8
+                    i, l1_entry, refcount);
9ae3a8
+            res->corruptions++;
9ae3a8
+        }
9ae3a8
+
9ae3a8
+        ret = bdrv_pread(bs->file, l2_offset, l2_table,
9ae3a8
+                         s->l2_size * sizeof(uint64_t));
9ae3a8
+        if (ret < 0) {
9ae3a8
+            fprintf(stderr, "ERROR: Could not read L2 table: %s\n",
9ae3a8
+                    strerror(-ret));
9ae3a8
+            res->check_errors++;
9ae3a8
+            goto fail;
9ae3a8
+        }
9ae3a8
+
9ae3a8
+        for (j = 0; j < s->l2_size; j++) {
9ae3a8
+            uint64_t l2_entry = be64_to_cpu(l2_table[j]);
9ae3a8
+            uint64_t data_offset = l2_entry & L2E_OFFSET_MASK;
9ae3a8
+            int cluster_type = qcow2_get_cluster_type(l2_entry);
9ae3a8
+
9ae3a8
+            if ((cluster_type == QCOW2_CLUSTER_NORMAL) ||
9ae3a8
+                ((cluster_type == QCOW2_CLUSTER_ZERO) && (data_offset != 0))) {
9ae3a8
+                refcount = get_refcount(bs, data_offset >> s->cluster_bits);
9ae3a8
+                if (refcount < 0) {
9ae3a8
+                    /* don't print message nor increment check_errors */
9ae3a8
+                    continue;
9ae3a8
+                }
9ae3a8
+                if ((refcount == 1) != ((l2_entry & QCOW_OFLAG_COPIED) != 0)) {
9ae3a8
+                    fprintf(stderr, "ERROR OFLAG_COPIED data cluster: "
9ae3a8
+                            "l2_entry=%" PRIx64 " refcount=%d\n",
9ae3a8
+                            l2_entry, refcount);
9ae3a8
+                    res->corruptions++;
9ae3a8
+                }
9ae3a8
+            }
9ae3a8
+        }
9ae3a8
+    }
9ae3a8
+
9ae3a8
+    ret = 0;
9ae3a8
+
9ae3a8
+fail:
9ae3a8
+    qemu_vfree(l2_table);
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
@@ -1365,6 +1408,12 @@ int qcow2_check_refcounts(BlockDriverState *bs, BdrvCheckResult *res,
9ae3a8
         }
9ae3a8
     }
9ae3a8
 
9ae3a8
+    /* check OFLAG_COPIED */
9ae3a8
+    ret = check_oflag_copied(bs, res);
9ae3a8
+    if (ret < 0) {
9ae3a8
+        goto fail;
9ae3a8
+    }
9ae3a8
+
9ae3a8
     res->image_end_offset = (highest_cluster + 1) * s->cluster_size;
9ae3a8
     ret = 0;
9ae3a8
 
9ae3a8
-- 
9ae3a8
1.7.1
9ae3a8