cryptospore / rpms / qemu-kvm

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