render / rpms / libvirt

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