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