fbe740
From 8d813e074a1bbe7636c487487e661a5e1a713fd1 Mon Sep 17 00:00:00 2001
fbe740
Message-Id: <8d813e074a1bbe7636c487487e661a5e1a713fd1@dist-git>
fbe740
From: Peter Krempa <pkrempa@redhat.com>
fbe740
Date: Tue, 23 Jun 2020 12:24:04 +0200
fbe740
Subject: [PATCH] qemu: Rewrite bitmap handling for block copy
fbe740
MIME-Version: 1.0
fbe740
Content-Type: text/plain; charset=UTF-8
fbe740
Content-Transfer-Encoding: 8bit
fbe740
fbe740
Reuse qemuBlockGetBitmapMergeActions which allows the removal of the
fbe740
ad-hoc implementation of bitmap merging for block copy.
fbe740
fbe740
Signed-off-by: Peter Krempa <pkrempa@redhat.com>
fbe740
Reviewed-by: Eric Blake <eblake@redhat.com>
fbe740
(cherry picked from commit 7bfff40fdfe5410c446c1cd8ec413e00530faf7d)
fbe740
https://bugzilla.redhat.com/show_bug.cgi?id=1804593
fbe740
Message-Id: <6a254120769a0071ef6867e4c15aca7d3760cf3c.1592906423.git.pkrempa@redhat.com>
fbe740
Reviewed-by: Ján Tomko <jtomko@redhat.com>
fbe740
---
fbe740
 src/qemu/qemu_block.c    | 113 ++-------------------------------------
fbe740
 src/qemu/qemu_blockjob.c |  40 ++++++++++++++
fbe740
 src/qemu/qemu_driver.c   |  13 ++---
fbe740
 tests/qemublocktest.c    |   6 ++-
fbe740
 4 files changed, 57 insertions(+), 115 deletions(-)
fbe740
fbe740
diff --git a/src/qemu/qemu_block.c b/src/qemu/qemu_block.c
fbe740
index ff9e000461..18c3861a2e 100644
fbe740
--- a/src/qemu/qemu_block.c
fbe740
+++ b/src/qemu/qemu_block.c
fbe740
@@ -3053,38 +3053,6 @@ qemuBlockBitmapChainIsValid(virStorageSourcePtr src,
fbe740
 }
fbe740
 
fbe740
 
fbe740
-struct qemuBlockBitmapsHandleBlockcopyConcatData {
fbe740
-    virHashTablePtr bitmaps_merge;
fbe740
-    virJSONValuePtr actions;
fbe740
-    const char *mirrornodeformat;
fbe740
-    bool has_bitmaps;
fbe740
-};
fbe740
-
fbe740
-
fbe740
-static int
fbe740
-qemuBlockBitmapsHandleBlockcopyConcatActions(void *payload,
fbe740
-                                             const void *name,
fbe740
-                                             void *opaque)
fbe740
-{
fbe740
-    struct qemuBlockBitmapsHandleBlockcopyConcatData *data = opaque;
fbe740
-    virJSONValuePtr createactions = payload;
fbe740
-    const char *bitmapname = name;
fbe740
-    g_autoptr(virJSONValue) mergebitmaps = virHashSteal(data->bitmaps_merge, bitmapname);
fbe740
-
fbe740
-    data->has_bitmaps = true;
fbe740
-
fbe740
-    virJSONValueArrayConcat(data->actions, createactions);
fbe740
-
fbe740
-    if (qemuMonitorTransactionBitmapMerge(data->actions,
fbe740
-                                          data->mirrornodeformat,
fbe740
-                                          bitmapname,
fbe740
-                                          &mergebitmaps) < 0)
fbe740
-        return -1;
fbe740
-
fbe740
-    return 0;
fbe740
-}
fbe740
-
fbe740
-
fbe740
 /**
fbe740
  * qemuBlockBitmapsHandleBlockcopy:
fbe740
  * @src: disk source
fbe740
@@ -3107,86 +3075,15 @@ qemuBlockBitmapsHandleBlockcopy(virStorageSourcePtr src,
fbe740
                                 bool shallow,
fbe740
                                 virJSONValuePtr *actions)
fbe740
 {
fbe740
-    g_autoptr(virHashTable) bitmaps = virHashNew(virJSONValueHashFree);
fbe740
-    g_autoptr(virHashTable) bitmaps_merge = virHashNew(virJSONValueHashFree);
fbe740
-    g_autoptr(virHashTable) bitmaps_skip = virHashNew(NULL);
fbe740
-    g_autoptr(virJSONValue) tmpactions = virJSONValueNewArray();
fbe740
-    qemuBlockNamedNodeDataPtr entry;
fbe740
-    virStorageSourcePtr n;
fbe740
-    size_t i;
fbe740
-    struct qemuBlockBitmapsHandleBlockcopyConcatData data = { .bitmaps_merge = bitmaps_merge,
fbe740
-                                                              .actions = tmpactions,
fbe740
-                                                              .mirrornodeformat = mirror->nodeformat,
fbe740
-                                                              .has_bitmaps = false, };
fbe740
-
fbe740
-    for (n = src; n; n = n->backingStore) {
fbe740
-        if (!(entry = virHashLookup(blockNamedNodeData, n->nodeformat)))
fbe740
-            continue;
fbe740
-
fbe740
-        for (i = 0; i < entry->nbitmaps; i++) {
fbe740
-            qemuBlockNamedNodeDataBitmapPtr bitmap = entry->bitmaps[i];
fbe740
-            virJSONValuePtr bitmap_merge;
fbe740
-
fbe740
-            if (virHashHasEntry(bitmaps_skip, bitmap->name))
fbe740
-                continue;
fbe740
-
fbe740
-            if (!(bitmap_merge = virHashLookup(bitmaps_merge, bitmap->name))) {
fbe740
-                g_autoptr(virJSONValue) tmp = NULL;
fbe740
-                bool disabled = !bitmap->recording;
fbe740
-
fbe740
-                /* disable any non top-layer bitmaps */
fbe740
-                if (n != src)
fbe740
-                    disabled = true;
fbe740
-
fbe740
-                if (!bitmap->persistent ||
fbe740
-                    !(qemuBlockBitmapChainIsValid(n, bitmap->name,
fbe740
-                                                  blockNamedNodeData))) {
fbe740
-                    ignore_value(virHashAddEntry(bitmaps_skip, bitmap->name, NULL));
fbe740
-                    continue;
fbe740
-                }
fbe740
-
fbe740
-                /* prepare the data for adding the bitmap to the mirror */
fbe740
-                tmp = virJSONValueNewArray();
fbe740
-
fbe740
-                if (qemuMonitorTransactionBitmapAdd(tmp,
fbe740
-                                                    mirror->nodeformat,
fbe740
-                                                    bitmap->name,
fbe740
-                                                    true,
fbe740
-                                                    disabled,
fbe740
-                                                    bitmap->granularity) < 0)
fbe740
-                    return -1;
fbe740
+    virStorageSourcePtr base = NULL;
fbe740
 
fbe740
-                if (virHashAddEntry(bitmaps, bitmap->name, tmp) < 0)
fbe740
-                    return -1;
fbe740
-
fbe740
-                tmp = NULL;
fbe740
-
fbe740
-                /* prepare array for merging all the bitmaps from the original chain */
fbe740
-                tmp = virJSONValueNewArray();
fbe740
-
fbe740
-                if (virHashAddEntry(bitmaps_merge, bitmap->name, tmp) < 0)
fbe740
-                    return -1;
fbe740
-
fbe740
-                bitmap_merge = g_steal_pointer(&tmp);
fbe740
-            }
fbe740
+    if (shallow)
fbe740
+        base = src->backingStore;
fbe740
 
fbe740
-            if (qemuMonitorTransactionBitmapMergeSourceAddBitmap(bitmap_merge,
fbe740
-                                                                 n->nodeformat,
fbe740
-                                                                 bitmap->name) < 0)
fbe740
-                return -1;
fbe740
-        }
fbe740
-
fbe740
-        if (shallow)
fbe740
-            break;
fbe740
-    }
fbe740
-
fbe740
-    if (virHashForEach(bitmaps, qemuBlockBitmapsHandleBlockcopyConcatActions,
fbe740
-                       &data) < 0)
fbe740
+    if (qemuBlockGetBitmapMergeActions(src, base, mirror, NULL, NULL, mirror, actions,
fbe740
+                                       blockNamedNodeData) < 0)
fbe740
         return -1;
fbe740
 
fbe740
-    if (data.has_bitmaps)
fbe740
-        *actions = g_steal_pointer(&tmpactions);
fbe740
-
fbe740
     return 0;
fbe740
 }
fbe740
 
fbe740
diff --git a/src/qemu/qemu_blockjob.c b/src/qemu/qemu_blockjob.c
fbe740
index d106165175..6e33f8666c 100644
fbe740
--- a/src/qemu/qemu_blockjob.c
fbe740
+++ b/src/qemu/qemu_blockjob.c
fbe740
@@ -1281,6 +1281,43 @@ qemuBlockJobProcessEventCompletedActiveCommit(virQEMUDriverPtr driver,
fbe740
 }
fbe740
 
fbe740
 
fbe740
+static int
fbe740
+qemuBlockJobProcessEventCompletedCopyBitmaps(virDomainObjPtr vm,
fbe740
+                                             qemuBlockJobDataPtr job,
fbe740
+                                             qemuDomainAsyncJob asyncJob)
fbe740
+{
fbe740
+    qemuDomainObjPrivatePtr priv = vm->privateData;
fbe740
+    g_autoptr(virHashTable) blockNamedNodeData = NULL;
fbe740
+    g_autoptr(virJSONValue) actions = NULL;
fbe740
+    bool shallow = job->jobflags & VIR_DOMAIN_BLOCK_COPY_SHALLOW;
fbe740
+
fbe740
+    if (!virQEMUCapsGet(priv->qemuCaps, QEMU_CAPS_BLOCKDEV_REOPEN))
fbe740
+        return 0;
fbe740
+
fbe740
+    if (!(blockNamedNodeData = qemuBlockGetNamedNodeData(vm, asyncJob)))
fbe740
+        return -1;
fbe740
+
fbe740
+    if (qemuBlockBitmapsHandleBlockcopy(job->disk->src,
fbe740
+                                        job->disk->mirror,
fbe740
+                                        blockNamedNodeData,
fbe740
+                                        shallow,
fbe740
+                                        &actions) < 0)
fbe740
+        return 0;
fbe740
+
fbe740
+    if (!actions)
fbe740
+        return 0;
fbe740
+
fbe740
+    if (qemuDomainObjEnterMonitorAsync(priv->driver, vm, asyncJob) < 0)
fbe740
+        return -1;
fbe740
+
fbe740
+    qemuMonitorTransaction(priv->mon, &actions);
fbe740
+
fbe740
+    if (qemuDomainObjExitMonitor(priv->driver, vm) < 0)
fbe740
+        return -1;
fbe740
+
fbe740
+    return 0;
fbe740
+}
fbe740
+
fbe740
 static void
fbe740
 qemuBlockJobProcessEventConcludedCopyPivot(virQEMUDriverPtr driver,
fbe740
                                            virDomainObjPtr vm,
fbe740
@@ -1295,6 +1332,8 @@ qemuBlockJobProcessEventConcludedCopyPivot(virQEMUDriverPtr driver,
fbe740
         !job->disk->mirror)
fbe740
         return;
fbe740
 
fbe740
+    qemuBlockJobProcessEventCompletedCopyBitmaps(vm, job, asyncJob);
fbe740
+
fbe740
     /* for shallow copy without reusing external image the user can either not
fbe740
      * specify the backing chain in which case libvirt will open and use the
fbe740
      * chain the user provided or not specify a chain in which case we'll
fbe740
@@ -1328,6 +1367,7 @@ qemuBlockJobProcessEventConcludedCopyAbort(virQEMUDriverPtr driver,
fbe740
         !job->disk->mirror)
fbe740
         return;
fbe740
 
fbe740
+    /* activeWrite bitmap is removed automatically here */
fbe740
     qemuBlockJobEventProcessConcludedRemoveChain(driver, vm, asyncJob, job->disk->mirror);
fbe740
     virObjectUnref(job->disk->mirror);
fbe740
     job->disk->mirror = NULL;
fbe740
diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
fbe740
index ed5498d2bc..9e3a455814 100644
fbe740
--- a/src/qemu/qemu_driver.c
fbe740
+++ b/src/qemu/qemu_driver.c
fbe740
@@ -17596,14 +17596,15 @@ qemuDomainBlockPivot(virQEMUDriverPtr driver,
fbe740
         if (blockdev && !job->jobflagsmissing) {
fbe740
             bool shallow = job->jobflags & VIR_DOMAIN_BLOCK_COPY_SHALLOW;
fbe740
             bool reuse = job->jobflags & VIR_DOMAIN_BLOCK_COPY_REUSE_EXT;
fbe740
-            g_autoptr(virHashTable) blockNamedNodeData = NULL;
fbe740
 
fbe740
-            if (!(blockNamedNodeData = qemuBlockGetNamedNodeData(vm, QEMU_ASYNC_JOB_NONE)))
fbe740
-                return -1;
fbe740
+            actions = virJSONValueNewArray();
fbe740
 
fbe740
-            if (qemuBlockBitmapsHandleBlockcopy(disk->src, disk->mirror,
fbe740
-                                                blockNamedNodeData,
fbe740
-                                                shallow, &actions) < 0)
fbe740
+            if (qemuMonitorTransactionBitmapAdd(actions,
fbe740
+                                                disk->mirror->nodeformat,
fbe740
+                                                "libvirt-tmp-activewrite",
fbe740
+                                                false,
fbe740
+                                                false,
fbe740
+                                                0) < 0)
fbe740
                 return -1;
fbe740
 
fbe740
             /* Open and install the backing chain of 'mirror' late if we can use
fbe740
diff --git a/tests/qemublocktest.c b/tests/qemublocktest.c
fbe740
index e1468f13fa..193b4aaed0 100644
fbe740
--- a/tests/qemublocktest.c
fbe740
+++ b/tests/qemublocktest.c
fbe740
@@ -881,6 +881,7 @@ testQemuBlockBitmapBlockcopy(const void *opaque)
fbe740
     g_autoptr(virJSONValue) nodedatajson = NULL;
fbe740
     g_autoptr(virHashTable) nodedata = NULL;
fbe740
     g_autoptr(virStorageSource) fakemirror = virStorageSourceNew();
fbe740
+    g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER;
fbe740
 
fbe740
     if (!fakemirror)
fbe740
         return -1;
fbe740
@@ -903,10 +904,13 @@ testQemuBlockBitmapBlockcopy(const void *opaque)
fbe740
                                         data->shallow, &actions) < 0)
fbe740
         return -1;
fbe740
 
fbe740
+
fbe740
     if (actions &&
fbe740
-        !(actual = virJSONValueToString(actions, true)))
fbe740
+        virJSONValueToBuffer(actions, &buf, true) < 0)
fbe740
         return -1;
fbe740
 
fbe740
+    actual = virBufferContentAndReset(&buf;;
fbe740
+
fbe740
     return virTestCompareToFile(actual, expectpath);
fbe740
 }
fbe740
 
fbe740
-- 
fbe740
2.27.0
fbe740