Blame SOURCES/libvirt-qemu-block-Introduce-function-to-calculate-bitmap-handling-for-block-copy.patch

fbe740
From 0890cd78e9391625a591a7a9327601ee79ba6636 Mon Sep 17 00:00:00 2001
fbe740
Message-Id: <0890cd78e9391625a591a7a9327601ee79ba6636@dist-git>
fbe740
From: Peter Krempa <pkrempa@redhat.com>
fbe740
Date: Tue, 4 Feb 2020 15:08:25 +0100
fbe740
Subject: [PATCH] qemu: block: Introduce function to calculate bitmap handling
fbe740
 for block-copy
fbe740
MIME-Version: 1.0
fbe740
Content-Type: text/plain; charset=UTF-8
fbe740
Content-Transfer-Encoding: 8bit
fbe740
fbe740
Add a function calculating which bitmaps to copy to the mirror during
fbe740
a block-copy operation.
fbe740
fbe740
Signed-off-by: Peter Krempa <pkrempa@redhat.com>
fbe740
Reviewed-by: Ján Tomko <jtomko@redhat.com>
fbe740
(cherry picked from commit 26f07f68707e16bef3d48e1c0b138791ab5df346)
fbe740
fbe740
https://bugzilla.redhat.com/show_bug.cgi?id=1207659
fbe740
Message-Id: <6d4bd72b975f9f690cb34784ed22bb5fad6bbf2c.1580824112.git.pkrempa@redhat.com>
fbe740
Reviewed-by: Ján Tomko <jtomko@redhat.com>
fbe740
---
fbe740
 src/qemu/qemu_block.c | 138 ++++++++++++++++++++++++++++++++++++++++++
fbe740
 src/qemu/qemu_block.h |   7 +++
fbe740
 2 files changed, 145 insertions(+)
fbe740
fbe740
diff --git a/src/qemu/qemu_block.c b/src/qemu/qemu_block.c
fbe740
index b19290e677..63116ef5f2 100644
fbe740
--- a/src/qemu/qemu_block.c
fbe740
+++ b/src/qemu/qemu_block.c
fbe740
@@ -2728,3 +2728,141 @@ qemuBlockBitmapChainIsValid(virStorageSourcePtr src,
fbe740
 
fbe740
     return chain_started;
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
+ * @mirror: mirror source
fbe740
+ * @blockNamedNodeData: hash table containing data about bitmaps
fbe740
+ * @shallow: whether shallow copy is requested
fbe740
+ * @actions: filled with arguments for a 'transaction' command
fbe740
+ *
fbe740
+ * Calculates which bitmaps to copy and merge during a virDomainBlockCopy job.
fbe740
+ * This is designed to be called when the job is already synchronised as it
fbe740
+ * may result in active bitmaps being created.
fbe740
+ *
fbe740
+ * Returns 0 on success and -1 on error. If @actions is NULL when 0 is returned
fbe740
+ * there are no actions to perform for the given job.
fbe740
+ */
fbe740
+int
fbe740
+qemuBlockBitmapsHandleBlockcopy(virStorageSourcePtr src,
fbe740
+                                virStorageSourcePtr mirror,
fbe740
+                                virHashTablePtr blockNamedNodeData,
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
+
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
+
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
+        return -1;
fbe740
+
fbe740
+    if (data.has_bitmaps)
fbe740
+        *actions = g_steal_pointer(&tmpactions);
fbe740
+
fbe740
+    return 0;
fbe740
+}
fbe740
diff --git a/src/qemu/qemu_block.h b/src/qemu/qemu_block.h
fbe740
index cf51b9bf4e..a816190bb7 100644
fbe740
--- a/src/qemu/qemu_block.h
fbe740
+++ b/src/qemu/qemu_block.h
fbe740
@@ -217,3 +217,10 @@ bool
fbe740
 qemuBlockBitmapChainIsValid(virStorageSourcePtr src,
fbe740
                             const char *bitmapname,
fbe740
                             virHashTablePtr blockNamedNodeData);
fbe740
+
fbe740
+int
fbe740
+qemuBlockBitmapsHandleBlockcopy(virStorageSourcePtr src,
fbe740
+                                virStorageSourcePtr mirror,
fbe740
+                                virHashTablePtr blockNamedNodeData,
fbe740
+                                bool shallow,
fbe740
+                                virJSONValuePtr *actions);
fbe740
-- 
fbe740
2.25.0
fbe740