9ae3a8
From ec8057f43c44075e02b59078b38b40340220f955 Mon Sep 17 00:00:00 2001
9ae3a8
From: Max Reitz <mreitz@redhat.com>
9ae3a8
Date: Mon, 18 Jun 2018 17:24:53 +0200
9ae3a8
Subject: [PATCH 05/17] qcow2: Repair OFLAG_COPIED when fixing leaks
9ae3a8
9ae3a8
RH-Author: Max Reitz <mreitz@redhat.com>
9ae3a8
Message-id: <20180618172454.27434-2-mreitz@redhat.com>
9ae3a8
Patchwork-id: 80785
9ae3a8
O-Subject: [RHEL-7.6 qemu-kvm PATCH 1/2] qcow2: Repair OFLAG_COPIED when fixing leaks
9ae3a8
Bugzilla: 1527122
9ae3a8
RH-Acked-by: John Snow <jsnow@redhat.com>
9ae3a8
RH-Acked-by: Kevin Wolf <kwolf@redhat.com>
9ae3a8
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
9ae3a8
9ae3a8
Repairing OFLAG_COPIED is usually safe because it is done after the
9ae3a8
refcounts have been repaired.  Therefore, it we did not find anyone else
9ae3a8
referencing a data or L2 cluster, it makes no sense to not set
9ae3a8
OFLAG_COPIED -- and the other direction (clearing OFLAG_COPIED) is
9ae3a8
always safe, anyway, it may just induce leaks.
9ae3a8
9ae3a8
Furthermore, if OFLAG_COPIED is actually consistent with a wrong (leaky)
9ae3a8
refcount, we will decrement the refcount with -r leaks, but OFLAG_COPIED
9ae3a8
will then be wrong.  qemu-img check should not produce images that are
9ae3a8
more corrupted afterwards then they were before.
9ae3a8
9ae3a8
Buglink: https://bugzilla.redhat.com/show_bug.cgi?id=1527085
9ae3a8
Signed-off-by: Max Reitz <mreitz@redhat.com>
9ae3a8
Reviewed-by: Eric Blake <eblake@redhat.com>
9ae3a8
Message-id: 20180509200059.31125-2-mreitz@redhat.com
9ae3a8
Signed-off-by: Max Reitz <mreitz@redhat.com>
9ae3a8
(cherry picked from commit 3cce51c919c7b4028cf6676dfcb80a45741b5117)
9ae3a8
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
9ae3a8
9ae3a8
Conflicts:
9ae3a8
	block/qcow2-refcount.c
9ae3a8
9ae3a8
Conflicts due to refcounts being fixed to 16 bit downstream, which means
9ae3a8
that every instance of the "refcount" variable is an int instead of
9ae3a8
uint64_t.  This results in contextual conflicts in the corruption
9ae3a8
printf()s.
9ae3a8
9ae3a8
Signed-off-by: Max Reitz <mreitz@redhat.com>
9ae3a8
---
9ae3a8
 block/qcow2-refcount.c | 25 +++++++++++++++++--------
9ae3a8
 1 file changed, 17 insertions(+), 8 deletions(-)
9ae3a8
9ae3a8
diff --git a/block/qcow2-refcount.c b/block/qcow2-refcount.c
9ae3a8
index 848fd31..7a69bcd 100644
9ae3a8
--- a/block/qcow2-refcount.c
9ae3a8
+++ b/block/qcow2-refcount.c
9ae3a8
@@ -1333,6 +1333,19 @@ static int check_oflag_copied(BlockDriverState *bs, BdrvCheckResult *res,
9ae3a8
     int ret;
9ae3a8
     int refcount;
9ae3a8
     int i, j;
9ae3a8
+    bool repair;
9ae3a8
+
9ae3a8
+    if (fix & BDRV_FIX_ERRORS) {
9ae3a8
+        /* Always repair */
9ae3a8
+        repair = true;
9ae3a8
+    } else if (fix & BDRV_FIX_LEAKS) {
9ae3a8
+        /* Repair only if that seems safe: This function is always
9ae3a8
+         * called after the refcounts have been fixed, so the refcount
9ae3a8
+         * is accurate if that repair was successful */
9ae3a8
+        repair = !res->check_errors && !res->corruptions && !res->leaks;
9ae3a8
+    } else {
9ae3a8
+        repair = false;
9ae3a8
+    }
9ae3a8
 
9ae3a8
     for (i = 0; i < s->l1_size; i++) {
9ae3a8
         uint64_t l1_entry = s->l1_table[i];
9ae3a8
@@ -1351,10 +1364,8 @@ static int check_oflag_copied(BlockDriverState *bs, BdrvCheckResult *res,
9ae3a8
         if ((refcount == 1) != ((l1_entry & QCOW_OFLAG_COPIED) != 0)) {
9ae3a8
             fprintf(stderr, "%s OFLAG_COPIED L2 cluster: l1_index=%d "
9ae3a8
                     "l1_entry=%" PRIx64 " refcount=%d\n",
9ae3a8
-                    fix & BDRV_FIX_ERRORS ? "Repairing" :
9ae3a8
-                                            "ERROR",
9ae3a8
-                    i, l1_entry, refcount);
9ae3a8
-            if (fix & BDRV_FIX_ERRORS) {
9ae3a8
+                    repair ? "Repairing" : "ERROR", i, l1_entry, refcount);
9ae3a8
+            if (repair) {
9ae3a8
                 s->l1_table[i] = refcount == 1
9ae3a8
                                ? l1_entry |  QCOW_OFLAG_COPIED
9ae3a8
                                : l1_entry & ~QCOW_OFLAG_COPIED;
9ae3a8
@@ -1393,10 +1404,8 @@ static int check_oflag_copied(BlockDriverState *bs, BdrvCheckResult *res,
9ae3a8
                 if ((refcount == 1) != ((l2_entry & QCOW_OFLAG_COPIED) != 0)) {
9ae3a8
                     fprintf(stderr, "%s OFLAG_COPIED data cluster: "
9ae3a8
                             "l2_entry=%" PRIx64 " refcount=%d\n",
9ae3a8
-                            fix & BDRV_FIX_ERRORS ? "Repairing" :
9ae3a8
-                                                    "ERROR",
9ae3a8
-                            l2_entry, refcount);
9ae3a8
-                    if (fix & BDRV_FIX_ERRORS) {
9ae3a8
+                            repair ? "Repairing" : "ERROR", l2_entry, refcount);
9ae3a8
+                    if (repair) {
9ae3a8
                         l2_table[j] = cpu_to_be64(refcount == 1
9ae3a8
                                     ? l2_entry |  QCOW_OFLAG_COPIED
9ae3a8
                                     : l2_entry & ~QCOW_OFLAG_COPIED);
9ae3a8
-- 
9ae3a8
1.8.3.1
9ae3a8