ae23c9
From 28e64491eecd735d36af79c83226682e097af1ef Mon Sep 17 00:00:00 2001
ae23c9
From: Max Reitz <mreitz@redhat.com>
ae23c9
Date: Mon, 18 Jun 2018 17:16:54 +0200
ae23c9
Subject: [PATCH 054/268] qcow2: Repair OFLAG_COPIED when fixing leaks
ae23c9
ae23c9
RH-Author: Max Reitz <mreitz@redhat.com>
ae23c9
Message-id: <20180618171655.25987-2-mreitz@redhat.com>
ae23c9
Patchwork-id: 80782
ae23c9
O-Subject: [RHV-7.6 qemu-kvm-rhev PATCH 1/2] qcow2: Repair OFLAG_COPIED when fixing leaks
ae23c9
Bugzilla: 1527085
ae23c9
RH-Acked-by: John Snow <jsnow@redhat.com>
ae23c9
RH-Acked-by: Kevin Wolf <kwolf@redhat.com>
ae23c9
RH-Acked-by: Miroslav Rezanina <mrezanin@redhat.com>
ae23c9
ae23c9
Repairing OFLAG_COPIED is usually safe because it is done after the
ae23c9
refcounts have been repaired.  Therefore, it we did not find anyone else
ae23c9
referencing a data or L2 cluster, it makes no sense to not set
ae23c9
OFLAG_COPIED -- and the other direction (clearing OFLAG_COPIED) is
ae23c9
always safe, anyway, it may just induce leaks.
ae23c9
ae23c9
Furthermore, if OFLAG_COPIED is actually consistent with a wrong (leaky)
ae23c9
refcount, we will decrement the refcount with -r leaks, but OFLAG_COPIED
ae23c9
will then be wrong.  qemu-img check should not produce images that are
ae23c9
more corrupted afterwards then they were before.
ae23c9
ae23c9
Buglink: https://bugzilla.redhat.com/show_bug.cgi?id=1527085
ae23c9
Signed-off-by: Max Reitz <mreitz@redhat.com>
ae23c9
Reviewed-by: Eric Blake <eblake@redhat.com>
ae23c9
Message-id: 20180509200059.31125-2-mreitz@redhat.com
ae23c9
Signed-off-by: Max Reitz <mreitz@redhat.com>
ae23c9
(cherry picked from commit 3cce51c919c7b4028cf6676dfcb80a45741b5117)
ae23c9
Signed-off-by: Max Reitz <mreitz@redhat.com>
ae23c9
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
ae23c9
---
ae23c9
 block/qcow2-refcount.c | 25 +++++++++++++++++--------
ae23c9
 1 file changed, 17 insertions(+), 8 deletions(-)
ae23c9
ae23c9
diff --git a/block/qcow2-refcount.c b/block/qcow2-refcount.c
ae23c9
index 6b8b635..4e14c0a 100644
ae23c9
--- a/block/qcow2-refcount.c
ae23c9
+++ b/block/qcow2-refcount.c
ae23c9
@@ -1799,6 +1799,19 @@ static int check_oflag_copied(BlockDriverState *bs, BdrvCheckResult *res,
ae23c9
     int ret;
ae23c9
     uint64_t refcount;
ae23c9
     int i, j;
ae23c9
+    bool repair;
ae23c9
+
ae23c9
+    if (fix & BDRV_FIX_ERRORS) {
ae23c9
+        /* Always repair */
ae23c9
+        repair = true;
ae23c9
+    } else if (fix & BDRV_FIX_LEAKS) {
ae23c9
+        /* Repair only if that seems safe: This function is always
ae23c9
+         * called after the refcounts have been fixed, so the refcount
ae23c9
+         * is accurate if that repair was successful */
ae23c9
+        repair = !res->check_errors && !res->corruptions && !res->leaks;
ae23c9
+    } else {
ae23c9
+        repair = false;
ae23c9
+    }
ae23c9
 
ae23c9
     for (i = 0; i < s->l1_size; i++) {
ae23c9
         uint64_t l1_entry = s->l1_table[i];
ae23c9
@@ -1818,10 +1831,8 @@ static int check_oflag_copied(BlockDriverState *bs, BdrvCheckResult *res,
ae23c9
         if ((refcount == 1) != ((l1_entry & QCOW_OFLAG_COPIED) != 0)) {
ae23c9
             fprintf(stderr, "%s OFLAG_COPIED L2 cluster: l1_index=%d "
ae23c9
                     "l1_entry=%" PRIx64 " refcount=%" PRIu64 "\n",
ae23c9
-                    fix & BDRV_FIX_ERRORS ? "Repairing" :
ae23c9
-                                            "ERROR",
ae23c9
-                    i, l1_entry, refcount);
ae23c9
-            if (fix & BDRV_FIX_ERRORS) {
ae23c9
+                    repair ? "Repairing" : "ERROR", i, l1_entry, refcount);
ae23c9
+            if (repair) {
ae23c9
                 s->l1_table[i] = refcount == 1
ae23c9
                                ? l1_entry |  QCOW_OFLAG_COPIED
ae23c9
                                : l1_entry & ~QCOW_OFLAG_COPIED;
ae23c9
@@ -1862,10 +1873,8 @@ static int check_oflag_copied(BlockDriverState *bs, BdrvCheckResult *res,
ae23c9
                 if ((refcount == 1) != ((l2_entry & QCOW_OFLAG_COPIED) != 0)) {
ae23c9
                     fprintf(stderr, "%s OFLAG_COPIED data cluster: "
ae23c9
                             "l2_entry=%" PRIx64 " refcount=%" PRIu64 "\n",
ae23c9
-                            fix & BDRV_FIX_ERRORS ? "Repairing" :
ae23c9
-                                                    "ERROR",
ae23c9
-                            l2_entry, refcount);
ae23c9
-                    if (fix & BDRV_FIX_ERRORS) {
ae23c9
+                            repair ? "Repairing" : "ERROR", l2_entry, refcount);
ae23c9
+                    if (repair) {
ae23c9
                         l2_table[j] = cpu_to_be64(refcount == 1
ae23c9
                                     ? l2_entry |  QCOW_OFLAG_COPIED
ae23c9
                                     : l2_entry & ~QCOW_OFLAG_COPIED);
ae23c9
-- 
ae23c9
1.8.3.1
ae23c9