render / rpms / libvirt

Forked from rpms/libvirt 10 months ago
Clone
b971b8
From 629e54cc030235909720da73d6367fc0b703d062 Mon Sep 17 00:00:00 2001
b971b8
Message-Id: <629e54cc030235909720da73d6367fc0b703d062@dist-git>
b971b8
From: Peter Krempa <pkrempa@redhat.com>
b971b8
Date: Tue, 23 Jun 2020 12:23:56 +0200
b971b8
Subject: [PATCH] qemu: block: Add universal helper for merging dirty bitmaps
b971b8
 for all scenarios
b971b8
MIME-Version: 1.0
b971b8
Content-Type: text/plain; charset=UTF-8
b971b8
Content-Transfer-Encoding: 8bit
b971b8
b971b8
Add a function which allows merging bitmaps according to the new
b971b8
semantics and will allow replacing all the specific ad-hoc functions
b971b8
currently in use for 'backup', 'block commit', 'block copy' and will
b971b8
also be usable in the future for 'block pull' and non-shared storage
b971b8
migration.
b971b8
b971b8
The semantics are a bit quirky for the 'backup' case but these quirks
b971b8
are documented and will prevent us from having two slightly different
b971b8
algorithms.
b971b8
b971b8
Signed-off-by: Peter Krempa <pkrempa@redhat.com>
b971b8
Reviewed-by: Eric Blake <eblake@redhat.com>
b971b8
(cherry picked from commit 4fa8654eced8b0362d3f3ff33eebb108fe833869)
b971b8
https://bugzilla.redhat.com/show_bug.cgi?id=1804593
b971b8
Message-Id: <25acdc313844a20a9c884048498c42b9a8105de7.1592906423.git.pkrempa@redhat.com>
b971b8
Reviewed-by: Ján Tomko <jtomko@redhat.com>
b971b8
---
b971b8
 src/qemu/qemu_block.c | 172 ++++++++++++++++++++++++++++++++++++++++++
b971b8
 src/qemu/qemu_block.h |  10 +++
b971b8
 2 files changed, 182 insertions(+)
b971b8
b971b8
diff --git a/src/qemu/qemu_block.c b/src/qemu/qemu_block.c
b971b8
index 38c8269721..b2296c2b4c 100644
b971b8
--- a/src/qemu/qemu_block.c
b971b8
+++ b/src/qemu/qemu_block.c
b971b8
@@ -2832,6 +2832,178 @@ qemuBlockGetNamedNodeData(virDomainObjPtr vm,
b971b8
 }
b971b8
 
b971b8
 
b971b8
+/**
b971b8
+ * qemuBlockGetBitmapMergeActionsGetBitmaps:
b971b8
+ *
b971b8
+ * Collect a list of bitmaps which need to be handled in
b971b8
+ * qemuBlockGetBitmapMergeActions. The list contains only valid bitmaps in the
b971b8
+ * sub-chain which is being processed.
b971b8
+ *
b971b8
+ * Note that the returned GSList contains bitmap names string pointers borrowed
b971b8
+ * from @blockNamedNodeData so they must not be freed.
b971b8
+ */
b971b8
+static GSList *
b971b8
+qemuBlockGetBitmapMergeActionsGetBitmaps(virStorageSourcePtr topsrc,
b971b8
+                                         const char *bitmapname,
b971b8
+                                         virHashTablePtr blockNamedNodeData)
b971b8
+{
b971b8
+    g_autoptr(GSList) ret = NULL;
b971b8
+    qemuBlockNamedNodeDataPtr entry;
b971b8
+    size_t i;
b971b8
+
b971b8
+    /* for now it doesn't make sense to consider bitmaps which are not present
b971b8
+     * in @topsrc as we can't recreate a bitmap for a layer if it's missing */
b971b8
+
b971b8
+    if (!(entry = virHashLookup(blockNamedNodeData, topsrc->nodeformat)))
b971b8
+        return NULL;
b971b8
+
b971b8
+    for (i = 0; i < entry->nbitmaps; i++) {
b971b8
+        qemuBlockNamedNodeDataBitmapPtr bitmap = entry->bitmaps[i];
b971b8
+
b971b8
+        if (bitmapname &&
b971b8
+            STRNEQ(bitmapname, bitmap->name))
b971b8
+            continue;
b971b8
+
b971b8
+        if (!qemuBlockBitmapChainIsValid(topsrc, bitmap->name, blockNamedNodeData))
b971b8
+            continue;
b971b8
+
b971b8
+        ret = g_slist_prepend(ret, bitmap->name);
b971b8
+    }
b971b8
+
b971b8
+    return g_steal_pointer(&ret;;
b971b8
+}
b971b8
+
b971b8
+
b971b8
+/**
b971b8
+ * qemuBlockGetBitmapMergeActions:
b971b8
+ * @topsrc: top of the chain to merge bitmaps in
b971b8
+ * @basesrc: bottom of the chain to merge bitmaps in (NULL for full chain)
b971b8
+ * @target: destination storage source of the merge (may be part of original chain)
b971b8
+ * @bitmapname: name of bitmap to perform the merge (NULL for all bitmaps)
b971b8
+ * @dstbitmapname: name of destination bitmap of the merge (see below for caveats)
b971b8
+ * @writebitmapsrc: storage source corresponding to the node containing the write temporary bitmap
b971b8
+ * @actions: returns actions for a 'transaction' QMP command for executing the merge
b971b8
+ * @blockNamedNodeData: hash table filled with qemuBlockNamedNodeData
b971b8
+ *
b971b8
+ * Calculate handling of dirty block bitmaps between @topsrc and @basesrc. If
b971b8
+ * @basesrc is NULL the end of the chain is considered. @target is the destination
b971b8
+ * storage source definition of the merge and may or may not be part of the
b971b8
+ * merged chain.
b971b8
+ *
b971b8
+ * Specifically the merging algorithm ensures that each considered bitmap is
b971b8
+ * merged with the appropriate bitmaps so that it properly describes
b971b8
+ * the state of dirty blocks when looked at from @topsrc based on the depth
b971b8
+ * of the backing chain where the bitmap is placed.
b971b8
+ *
b971b8
+ * If @bitmapname is non-NULL only bitmaps with that name are handled, otherwise
b971b8
+ * all bitmaps are considered.
b971b8
+ *
b971b8
+ * If @dstbitmap is non-NULL everything is merged into a bitmap with that name,
b971b8
+ * otherwise each bitmap is merged into a bitmap with the same name into @target.
b971b8
+ * Additionally if @dstbitmap is non-NULL the target bitmap is created as 'inactive'
b971b8
+ * and 'transient' as a special case for the backup operation.
b971b8
+ *
b971b8
+ * If @writebitmapsrc is non-NULL, the 'libvirt-tmp-activewrite' bitmap from
b971b8
+ * given node is merged along with others. This bitmap corresponds to the writes
b971b8
+ * which occurred between an active layer job finished and the rest of the bitmap
b971b8
+ * merging.
b971b8
+ *
b971b8
+ * If the bitmap is not valid somehow (see qemuBlockBitmapChainIsValid) given
b971b8
+ * bitmap is silently skipped, so callers must ensure that given bitmap is valid
b971b8
+ * if they care about it.
b971b8
+ *
b971b8
+ * The resulting 'transaction' QMP command actions are filled in and returned via
b971b8
+ * @actions.
b971b8
+ *
b971b8
+ * Note that @actions may be NULL if no merging is required.
b971b8
+ */
b971b8
+int
b971b8
+qemuBlockGetBitmapMergeActions(virStorageSourcePtr topsrc,
b971b8
+                               virStorageSourcePtr basesrc,
b971b8
+                               virStorageSourcePtr target,
b971b8
+                               const char *bitmapname,
b971b8
+                               const char *dstbitmapname,
b971b8
+                               virStorageSourcePtr writebitmapsrc,
b971b8
+                               virJSONValuePtr *actions,
b971b8
+                               virHashTablePtr blockNamedNodeData)
b971b8
+{
b971b8
+    g_autoptr(virJSONValue) act = virJSONValueNewArray();
b971b8
+    virStorageSourcePtr n;
b971b8
+
b971b8
+    g_autoptr(GSList) bitmaps = NULL;
b971b8
+    GSList *next;
b971b8
+
b971b8
+    if (!(bitmaps = qemuBlockGetBitmapMergeActionsGetBitmaps(topsrc, bitmapname,
b971b8
+                                                             blockNamedNodeData)))
b971b8
+        return 0;
b971b8
+
b971b8
+    for (next = bitmaps; next; next = next->next) {
b971b8
+        const char *curbitmap = next->data;
b971b8
+        const char *mergebitmapname = dstbitmapname;
b971b8
+        bool mergebitmappersistent = false;
b971b8
+        bool mergebitmapdisabled = true;
b971b8
+        g_autoptr(virJSONValue) merge = virJSONValueNewArray();
b971b8
+        unsigned long long granularity = 0;
b971b8
+        qemuBlockNamedNodeDataBitmapPtr bitmap;
b971b8
+
b971b8
+        /* explicitly named destinations mean that we want a temporary
b971b8
+         * disabled bitmap only, so undo the default for non-explicit cases  */
b971b8
+        if (!mergebitmapname) {
b971b8
+            mergebitmapname = curbitmap;
b971b8
+            mergebitmappersistent = true;
b971b8
+            mergebitmapdisabled = false;
b971b8
+        }
b971b8
+
b971b8
+        for (n = topsrc; virStorageSourceIsBacking(n) && n != basesrc; n = n->backingStore) {
b971b8
+            if (!(bitmap = qemuBlockNamedNodeDataGetBitmapByName(blockNamedNodeData,
b971b8
+                                                                 n, curbitmap)))
b971b8
+                continue;
b971b8
+
b971b8
+            if (granularity == 0)
b971b8
+                granularity = bitmap->granularity;
b971b8
+
b971b8
+            if (qemuMonitorTransactionBitmapMergeSourceAddBitmap(merge,
b971b8
+                                                                 n->nodeformat,
b971b8
+                                                                 bitmap->name) < 0)
b971b8
+                return -1;
b971b8
+        }
b971b8
+
b971b8
+        if (dstbitmapname ||
b971b8
+            !(bitmap = qemuBlockNamedNodeDataGetBitmapByName(blockNamedNodeData,
b971b8
+                                                             target, curbitmap))) {
b971b8
+
b971b8
+            if (qemuMonitorTransactionBitmapAdd(act,
b971b8
+                                                target->nodeformat,
b971b8
+                                                mergebitmapname,
b971b8
+                                                mergebitmappersistent,
b971b8
+                                                mergebitmapdisabled,
b971b8
+                                                granularity) < 0)
b971b8
+                return -1;
b971b8
+        }
b971b8
+
b971b8
+        if (writebitmapsrc &&
b971b8
+            qemuMonitorTransactionBitmapMergeSourceAddBitmap(merge,
b971b8
+                                                             writebitmapsrc->nodeformat,
b971b8
+                                                             "libvirt-tmp-activewrite") < 0)
b971b8
+            return -1;
b971b8
+
b971b8
+        if (qemuMonitorTransactionBitmapMerge(act, target->nodeformat,
b971b8
+                                              mergebitmapname, &merge) < 0)
b971b8
+            return -1;
b971b8
+    }
b971b8
+
b971b8
+    if (writebitmapsrc &&
b971b8
+        qemuMonitorTransactionBitmapRemove(act, writebitmapsrc->nodeformat,
b971b8
+                                           "libvirt-tmp-activewrite") < 0)
b971b8
+        return -1;
b971b8
+
b971b8
+    if (virJSONValueArraySize(act) > 0)
b971b8
+        *actions = g_steal_pointer(&act;;
b971b8
+
b971b8
+    return 0;
b971b8
+}
b971b8
+
b971b8
+
b971b8
 /**
b971b8
  * qemuBlockBitmapChainIsValid:
b971b8
  *
b971b8
diff --git a/src/qemu/qemu_block.h b/src/qemu/qemu_block.h
b971b8
index 06afa54115..2500390734 100644
b971b8
--- a/src/qemu/qemu_block.h
b971b8
+++ b/src/qemu/qemu_block.h
b971b8
@@ -221,6 +221,16 @@ virHashTablePtr
b971b8
 qemuBlockGetNamedNodeData(virDomainObjPtr vm,
b971b8
                           qemuDomainAsyncJob asyncJob);
b971b8
 
b971b8
+int
b971b8
+qemuBlockGetBitmapMergeActions(virStorageSourcePtr topsrc,
b971b8
+                               virStorageSourcePtr basesrc,
b971b8
+                               virStorageSourcePtr target,
b971b8
+                               const char *bitmapname,
b971b8
+                               const char *dstbitmapname,
b971b8
+                               virStorageSourcePtr writebitmapsrc,
b971b8
+                               virJSONValuePtr *actions,
b971b8
+                               virHashTablePtr blockNamedNodeData);
b971b8
+
b971b8
 bool
b971b8
 qemuBlockBitmapChainIsValid(virStorageSourcePtr src,
b971b8
                             const char *bitmapname,
b971b8
-- 
b971b8
2.27.0
b971b8