Blame SOURCES/kvm-qcow2-Repair-OFLAG_COPIED-when-fixing-leaks.patch

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