1b87cf
From b2529d5ff3a18a2b0022da75431cea5bf037819e Mon Sep 17 00:00:00 2001
1b87cf
Message-Id: <b2529d5ff3a18a2b0022da75431cea5bf037819e@dist-git>
1b87cf
From: Peter Krempa <pkrempa@redhat.com>
1b87cf
Date: Wed, 28 Jul 2021 17:37:21 +0200
1b87cf
Subject: [PATCH] RHEL: Enable usage of x-blockdev-reopen
1b87cf
MIME-Version: 1.0
1b87cf
Content-Type: text/plain; charset=UTF-8
1b87cf
Content-Transfer-Encoding: 8bit
1b87cf
1b87cf
RHEL-only
1b87cf
1b87cf
Introduce a new capability QEMU_CAPS_BLOCKDEV_REOPEN_COM_REDHAT_AV_8_2_0_API
1b87cf
based on the presence of '__com.redhat_rhel-av-8_2_0-api' feature for
1b87cf
'x-blockdev-reopen' which states that reopen works for what libvirt
1b87cf
is going to use it and wire up code to call the x- prefixed command.
1b87cf
1b87cf
This implementation will become dormant once qemu starts supporting
1b87cf
upstream-stable blockdev-reopen.
1b87cf
1b87cf
https://bugzilla.redhat.com/show_bug.cgi?id=1799013
1b87cf
1b87cf
Starting with libvirt-7.6, upstream has adapted to the new format of
1b87cf
arguments so this patch was modified to support blockdev-reopen which
1b87cf
takes an array of nodes to reopen.
1b87cf
1b87cf
https://bugzilla.redhat.com/show_bug.cgi?id=1929765
1b87cf
Message-Id: <3fcde2fc6add36d5276ae224caf18adc8bca7d48.1627486352.git.pkrempa@redhat.com>
1b87cf
Reviewed-by: Ján Tomko <jtomko@redhat.com>
1b87cf
---
1b87cf
 src/qemu/qemu_block.c        | 24 +++++++++++++++---------
1b87cf
 src/qemu/qemu_block.h        |  3 ++-
1b87cf
 src/qemu/qemu_capabilities.c | 13 +++++++++++++
1b87cf
 src/qemu/qemu_capabilities.h |  3 +++
1b87cf
 src/qemu/qemu_monitor.c      |  5 +++--
1b87cf
 src/qemu/qemu_monitor.h      |  3 ++-
1b87cf
 src/qemu/qemu_monitor_json.c | 12 +++++++++---
1b87cf
 src/qemu/qemu_monitor_json.h |  3 ++-
1b87cf
 tests/qemumonitorjsontest.c  |  2 +-
1b87cf
 9 files changed, 50 insertions(+), 18 deletions(-)
1b87cf
1b87cf
diff --git a/src/qemu/qemu_block.c b/src/qemu/qemu_block.c
1b87cf
index e5ff653a60..aa566d0097 100644
1b87cf
--- a/src/qemu/qemu_block.c
1b87cf
+++ b/src/qemu/qemu_block.c
1b87cf
@@ -3331,7 +3331,8 @@ qemuBlockBitmapsHandleCommitFinish(virStorageSource *topsrc,
1b87cf
 
1b87cf
 int
1b87cf
 qemuBlockReopenFormatMon(qemuMonitor *mon,
1b87cf
-                         virStorageSource *src)
1b87cf
+                         virStorageSource *src,
1b87cf
+                         bool downstream)
1b87cf
 {
1b87cf
     g_autoptr(virJSONValue) reopenprops = NULL;
1b87cf
     g_autoptr(virJSONValue) srcprops = NULL;
1b87cf
@@ -3340,15 +3341,19 @@ qemuBlockReopenFormatMon(qemuMonitor *mon,
1b87cf
     if (!(srcprops = qemuBlockStorageSourceGetBlockdevProps(src, src->backingStore)))
1b87cf
         return -1;
1b87cf
 
1b87cf
-    if (virJSONValueArrayAppend(reopenoptions, &srcprops) < 0)
1b87cf
-        return -1;
1b87cf
+    if (downstream) {
1b87cf
+        reopenprops = g_steal_pointer(&srcprops);
1b87cf
+    } else {
1b87cf
+        if (virJSONValueArrayAppend(reopenoptions, &srcprops) < 0)
1b87cf
+            return -1;
1b87cf
 
1b87cf
-    if (virJSONValueObjectAdd(&reopenprops,
1b87cf
-                              "a:options", &reopenoptions,
1b87cf
-                              NULL) < 0)
1b87cf
-        return -1;
1b87cf
+        if (virJSONValueObjectAdd(&reopenprops,
1b87cf
+                                  "a:options", &reopenoptions,
1b87cf
+                                  NULL) < 0)
1b87cf
+            return -1;
1b87cf
+    }
1b87cf
 
1b87cf
-    if (qemuMonitorBlockdevReopen(mon, &reopenprops) < 0)
1b87cf
+    if (qemuMonitorBlockdevReopen(mon, &reopenprops, downstream) < 0)
1b87cf
         return -1;
1b87cf
 
1b87cf
     return 0;
1b87cf
@@ -3372,6 +3377,7 @@ qemuBlockReopenFormat(virDomainObj *vm,
1b87cf
 {
1b87cf
     qemuDomainObjPrivate *priv = vm->privateData;
1b87cf
     virQEMUDriver *driver = priv->driver;
1b87cf
+    bool downstream = virQEMUCapsGet(priv->qemuCaps, QEMU_CAPS_BLOCKDEV_REOPEN_COM_REDHAT_AV_8_2_0_API);
1b87cf
     int rc;
1b87cf
 
1b87cf
     /* If we are lacking the object here, qemu might have opened an image with
1b87cf
@@ -3385,7 +3391,7 @@ qemuBlockReopenFormat(virDomainObj *vm,
1b87cf
     if (qemuDomainObjEnterMonitorAsync(driver, vm, asyncJob) < 0)
1b87cf
         return -1;
1b87cf
 
1b87cf
-    rc = qemuBlockReopenFormatMon(priv->mon, src);
1b87cf
+    rc = qemuBlockReopenFormatMon(priv->mon, src, downstream);
1b87cf
 
1b87cf
     qemuDomainObjExitMonitor(driver, vm);
1b87cf
     if (rc < 0)
1b87cf
diff --git a/src/qemu/qemu_block.h b/src/qemu/qemu_block.h
1b87cf
index 184a549d5c..130cfcdefd 100644
1b87cf
--- a/src/qemu/qemu_block.h
1b87cf
+++ b/src/qemu/qemu_block.h
1b87cf
@@ -267,7 +267,8 @@ qemuBlockBitmapsHandleCommitFinish(virStorageSource *topsrc,
1b87cf
 /* only for use in qemumonitorjsontest */
1b87cf
 int
1b87cf
 qemuBlockReopenFormatMon(qemuMonitor *mon,
1b87cf
-                         virStorageSource *src);
1b87cf
+                         virStorageSource *src,
1b87cf
+                         bool downstream);
1b87cf
 
1b87cf
 int
1b87cf
 qemuBlockReopenReadWrite(virDomainObj *vm,
1b87cf
diff --git a/src/qemu/qemu_capabilities.c b/src/qemu/qemu_capabilities.c
1b87cf
index 5f1eb5014c..8ae80ef8d7 100644
1b87cf
--- a/src/qemu/qemu_capabilities.c
1b87cf
+++ b/src/qemu/qemu_capabilities.c
1b87cf
@@ -654,6 +654,9 @@ VIR_ENUM_IMPL(virQEMUCaps,
1b87cf
               "rbd-encryption", /* QEMU_CAPS_RBD_ENCRYPTION */
1b87cf
               "sev-guest-kernel-hashes", /* QEMU_CAPS_SEV_GUEST_KERNEL_HASHES */
1b87cf
               "sev-inject-launch-secret", /* QEMU_CAPS_SEV_INJECT_LAUNCH_SECRET */
1b87cf
+
1b87cf
+              /* 420 */
1b87cf
+              "blockdev-reopen.__com.redhat_rhel-av-8_2_0-api", /* QEMU_CAPS_BLOCKDEV_REOPEN_COM_REDHAT_AV_8_2_0_API */
1b87cf
     );
1b87cf
 
1b87cf
 
1b87cf
@@ -1540,6 +1543,7 @@ static struct virQEMUCapsDevicePropsFlags virQEMUCapsDevicePropsVhostUserFS[] =
1b87cf
 /* see documentation for virQEMUQAPISchemaPathGet for the query format */
1b87cf
 static struct virQEMUCapsStringFlags virQEMUCapsQMPSchemaQueries[] = {
1b87cf
     { "block-commit/arg-type/*top",  QEMU_CAPS_ACTIVE_COMMIT },
1b87cf
+    { "x-blockdev-reopen/$__com.redhat_rhel-av-8_2_0-api", QEMU_CAPS_BLOCKDEV_REOPEN_COM_REDHAT_AV_8_2_0_API },
1b87cf
     { "blockdev-add/arg-type/options/+gluster/debug-level", QEMU_CAPS_GLUSTER_DEBUG_LEVEL},
1b87cf
     { "blockdev-add/arg-type/+gluster/debug", QEMU_CAPS_GLUSTER_DEBUG_LEVEL},
1b87cf
     { "blockdev-add/arg-type/+vxhs", QEMU_CAPS_VXHS},
1b87cf
@@ -5235,6 +5239,15 @@ virQEMUCapsInitProcessCaps(virQEMUCaps *qemuCaps)
1b87cf
         qemuCaps->arch == VIR_ARCH_MIPS)
1b87cf
         virQEMUCapsSet(qemuCaps, QEMU_CAPS_SCSI_NCR53C90);
1b87cf
 
1b87cf
+    /* RHEL-only:
1b87cf
+     * - if upstream blockdev-reopen is enabled, clear the downstream flag
1b87cf
+     * - if the downstream flag is present but not the upstream, assert the upstream flag too
1b87cf
+     */
1b87cf
+    if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_BLOCKDEV_REOPEN))
1b87cf
+        virQEMUCapsClear(qemuCaps, QEMU_CAPS_BLOCKDEV_REOPEN_COM_REDHAT_AV_8_2_0_API);
1b87cf
+    if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_BLOCKDEV_REOPEN_COM_REDHAT_AV_8_2_0_API))
1b87cf
+        virQEMUCapsSet(qemuCaps, QEMU_CAPS_BLOCKDEV_REOPEN);
1b87cf
+
1b87cf
     virQEMUCapsInitProcessCapsInterlock(qemuCaps);
1b87cf
 }
1b87cf
 
1b87cf
diff --git a/src/qemu/qemu_capabilities.h b/src/qemu/qemu_capabilities.h
1b87cf
index e3a3ab4445..cde6c18b4c 100644
1b87cf
--- a/src/qemu/qemu_capabilities.h
1b87cf
+++ b/src/qemu/qemu_capabilities.h
1b87cf
@@ -634,6 +634,9 @@ typedef enum { /* virQEMUCapsFlags grouping marker for syntax-check */
1b87cf
     QEMU_CAPS_SEV_GUEST_KERNEL_HASHES, /* sev-guest.kernel-hashes= */
1b87cf
     QEMU_CAPS_SEV_INJECT_LAUNCH_SECRET, /* 'sev-inject-launch-secret' qmp command present */
1b87cf
 
1b87cf
+    /* 420 */
1b87cf
+    QEMU_CAPS_BLOCKDEV_REOPEN_COM_REDHAT_AV_8_2_0_API, /* downstream support for blockdev reopen in rhel-av-8.2.0 */
1b87cf
+
1b87cf
     QEMU_CAPS_LAST /* this must always be the last item */
1b87cf
 } virQEMUCapsFlags;
1b87cf
 
1b87cf
diff --git a/src/qemu/qemu_monitor.c b/src/qemu/qemu_monitor.c
1b87cf
index babf9e62fb..23638d3fe8 100644
1b87cf
--- a/src/qemu/qemu_monitor.c
1b87cf
+++ b/src/qemu/qemu_monitor.c
1b87cf
@@ -4247,14 +4247,15 @@ qemuMonitorBlockdevAdd(qemuMonitor *mon,
1b87cf
 
1b87cf
 int
1b87cf
 qemuMonitorBlockdevReopen(qemuMonitor *mon,
1b87cf
-                          virJSONValue **props)
1b87cf
+                          virJSONValue **props,
1b87cf
+                          bool downstream)
1b87cf
 {
1b87cf
     VIR_DEBUG("props=%p (node-name=%s)", *props,
1b87cf
               NULLSTR(virJSONValueObjectGetString(*props, "node-name")));
1b87cf
 
1b87cf
     QEMU_CHECK_MONITOR(mon);
1b87cf
 
1b87cf
-    return qemuMonitorJSONBlockdevReopen(mon, props);
1b87cf
+    return qemuMonitorJSONBlockdevReopen(mon, props, downstream);
1b87cf
 }
1b87cf
 
1b87cf
 
1b87cf
diff --git a/src/qemu/qemu_monitor.h b/src/qemu/qemu_monitor.h
1b87cf
index 9b2e4e1421..d2037914be 100644
1b87cf
--- a/src/qemu/qemu_monitor.h
1b87cf
+++ b/src/qemu/qemu_monitor.h
1b87cf
@@ -1426,7 +1426,8 @@ int qemuMonitorBlockdevAdd(qemuMonitor *mon,
1b87cf
                            virJSONValue **props);
1b87cf
 
1b87cf
 int qemuMonitorBlockdevReopen(qemuMonitor *mon,
1b87cf
-                              virJSONValue **props);
1b87cf
+                              virJSONValue **props,
1b87cf
+                              bool downstream);
1b87cf
 
1b87cf
 int qemuMonitorBlockdevDel(qemuMonitor *mon,
1b87cf
                            const char *nodename);
1b87cf
diff --git a/src/qemu/qemu_monitor_json.c b/src/qemu/qemu_monitor_json.c
1b87cf
index b0b513683b..34a46b9b41 100644
1b87cf
--- a/src/qemu/qemu_monitor_json.c
1b87cf
+++ b/src/qemu/qemu_monitor_json.c
1b87cf
@@ -8051,13 +8051,19 @@ qemuMonitorJSONBlockdevAdd(qemuMonitor *mon,
1b87cf
 
1b87cf
 int
1b87cf
 qemuMonitorJSONBlockdevReopen(qemuMonitor *mon,
1b87cf
-                              virJSONValue **props)
1b87cf
+                              virJSONValue **props,
1b87cf
+                              bool downstream)
1b87cf
 {
1b87cf
     g_autoptr(virJSONValue) cmd = NULL;
1b87cf
     g_autoptr(virJSONValue) reply = NULL;
1b87cf
 
1b87cf
-    if (!(cmd = qemuMonitorJSONMakeCommandInternal("blockdev-reopen", props)))
1b87cf
-        return -1;
1b87cf
+    if (downstream) {
1b87cf
+        if (!(cmd = qemuMonitorJSONMakeCommandInternal("x-blockdev-reopen", props)))
1b87cf
+            return -1;
1b87cf
+    } else {
1b87cf
+        if (!(cmd = qemuMonitorJSONMakeCommandInternal("blockdev-reopen", props)))
1b87cf
+            return -1;
1b87cf
+    }
1b87cf
 
1b87cf
     if (qemuMonitorJSONCommand(mon, cmd, &reply) < 0)
1b87cf
         return -1;
1b87cf
diff --git a/src/qemu/qemu_monitor_json.h b/src/qemu/qemu_monitor_json.h
1b87cf
index 64d9ebdaa3..15ce03d7af 100644
1b87cf
--- a/src/qemu/qemu_monitor_json.h
1b87cf
+++ b/src/qemu/qemu_monitor_json.h
1b87cf
@@ -748,7 +748,8 @@ qemuMonitorJSONBlockdevAdd(qemuMonitor *mon,
1b87cf
 
1b87cf
 int
1b87cf
 qemuMonitorJSONBlockdevReopen(qemuMonitor *mon,
1b87cf
-                              virJSONValue **props)
1b87cf
+                              virJSONValue **props,
1b87cf
+                              bool downstream)
1b87cf
     ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
1b87cf
 
1b87cf
 int
1b87cf
diff --git a/tests/qemumonitorjsontest.c b/tests/qemumonitorjsontest.c
1b87cf
index 48e2a457ab..8624a547b5 100644
1b87cf
--- a/tests/qemumonitorjsontest.c
1b87cf
+++ b/tests/qemumonitorjsontest.c
1b87cf
@@ -2780,7 +2780,7 @@ testQemuMonitorJSONBlockdevReopen(const void *opaque)
1b87cf
     if (qemuMonitorTestAddItem(test, "blockdev-reopen", "{\"return\":{}}") < 0)
1b87cf
         return -1;
1b87cf
 
1b87cf
-    if (qemuBlockReopenFormatMon(qemuMonitorTestGetMonitor(test), src) < 0)
1b87cf
+    if (qemuBlockReopenFormatMon(qemuMonitorTestGetMonitor(test), src, false) < 0)
1b87cf
         return -1;
1b87cf
 
1b87cf
     return 0;
1b87cf
-- 
1b87cf
2.34.1
1b87cf