[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[libvirt] [PATCHv3 3/7] qemu: blockjob: Split qemuDomainBlockJobSetSpeed from qemuDomainBlockJobImpl



qemuDomainBlockJobImpl become an unmaintainable mess over the years of
adding new stuff to it. This patch starts splitting up individual
functions from it until it can be killed entirely.

In bulk this will add lines of code rather than delete them but it will
be traded for maintainability.
---

Notes:
    Version 3:
    - no change, already ACKed

 src/qemu/qemu_driver.c       | 66 ++++++++++++++++++++++++++++++++++++--------
 src/qemu/qemu_monitor.c      | 19 +++++++++++++
 src/qemu/qemu_monitor.h      |  6 +++-
 src/qemu/qemu_monitor_json.c | 41 +++++++++++++++++++++------
 src/qemu/qemu_monitor_json.h |  6 ++++
 5 files changed, 118 insertions(+), 20 deletions(-)

diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index 00e4e17..ab962f3 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -16292,10 +16292,8 @@ qemuDomainBlockJobImpl(virDomainObjPtr vm,
     }

     /* Convert bandwidth MiB to bytes, if needed */
-    if ((mode == BLOCK_JOB_SPEED &&
-         !(flags & VIR_DOMAIN_BLOCK_JOB_SPEED_BANDWIDTH_BYTES)) ||
-        (mode == BLOCK_JOB_PULL &&
-         !(flags & VIR_DOMAIN_BLOCK_PULL_BANDWIDTH_BYTES))) {
+    if (mode == BLOCK_JOB_PULL &&
+        !(flags & VIR_DOMAIN_BLOCK_PULL_BANDWIDTH_BYTES)) {
         if (speed > LLONG_MAX >> 20) {
             virReportError(VIR_ERR_OVERFLOW,
                            _("bandwidth must be less than %llu"),
@@ -16486,23 +16484,69 @@ qemuDomainGetBlockJobInfo(virDomainPtr dom, const char *path,
     return ret;
 }

+
 static int
-qemuDomainBlockJobSetSpeed(virDomainPtr dom, const char *path,
-                           unsigned long bandwidth, unsigned int flags)
+qemuDomainBlockJobSetSpeed(virDomainPtr dom,
+                           const char *path,
+                           unsigned long bandwidth,
+                           unsigned int flags)
 {
+    virQEMUDriverPtr driver = dom->conn->privateData;
+    int ret = -1;
     virDomainObjPtr vm;
+    bool modern;
+    const char *device;
+    unsigned long long speed = bandwidth;
+
     virCheckFlags(VIR_DOMAIN_BLOCK_JOB_SPEED_BANDWIDTH_BYTES, -1);

+    /* Convert bandwidth MiB to bytes, if needed */
+    if (!(flags & VIR_DOMAIN_BLOCK_JOB_SPEED_BANDWIDTH_BYTES)) {
+        if (speed > LLONG_MAX >> 20) {
+            virReportError(VIR_ERR_OVERFLOW,
+                           _("bandwidth must be less than %llu"),
+                           LLONG_MAX >> 20);
+            return -1;
+        }
+        speed <<= 20;
+    }
+
     if (!(vm = qemuDomObjFromDomain(dom)))
         return -1;

-    if (virDomainBlockJobSetSpeedEnsureACL(dom->conn, vm->def) < 0) {
-        qemuDomObjEndAPI(&vm);
-        return -1;
+    if (virDomainBlockJobSetSpeedEnsureACL(dom->conn, vm->def) < 0)
+        goto cleanup;
+
+    if (qemuDomainSupportsBlockJobs(vm, &modern) < 0)
+        goto cleanup;
+
+    if (qemuDomainObjBeginJob(driver, vm, QEMU_JOB_MODIFY) < 0)
+        goto cleanup;
+
+    if (!virDomainObjIsActive(vm)) {
+        virReportError(VIR_ERR_OPERATION_INVALID, "%s",
+                       _("domain is not running"));
+        goto endjob;
     }

-    return qemuDomainBlockJobImpl(vm, dom->conn, path, NULL, bandwidth,
-                                  BLOCK_JOB_SPEED, flags);
+    if (!(device = qemuDiskPathToAlias(vm, path, NULL)))
+        goto endjob;
+
+    qemuDomainObjEnterMonitor(driver, vm);
+    ret = qemuMonitorBlockJobSetSpeed(qemuDomainGetMonitor(vm),
+                                      device,
+                                      speed,
+                                      modern);
+    if (qemuDomainObjExitMonitor(driver, vm) < 0)
+        ret = -1;
+
+ endjob:
+    qemuDomainObjEndJob(driver, vm);
+
+ cleanup:
+    qemuDomObjEndAPI(&vm);
+
+    return ret;
 }


diff --git a/src/qemu/qemu_monitor.c b/src/qemu/qemu_monitor.c
index 2b0e1a5..3a6a746 100644
--- a/src/qemu/qemu_monitor.c
+++ b/src/qemu/qemu_monitor.c
@@ -3603,6 +3603,25 @@ qemuMonitorBlockJob(qemuMonitorPtr mon,


 int
+qemuMonitorBlockJobSetSpeed(qemuMonitorPtr mon,
+                            const char *device,
+                            unsigned long long bandwidth,
+                            bool modern)
+{
+    VIR_DEBUG("mon=%p, device=%s, bandwidth=%lluB, modern=%d",
+              mon, device, bandwidth, modern);
+
+    if (!mon->json) {
+        virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s",
+                       _("block jobs require JSON monitor"));
+        return -1;
+    }
+
+    return qemuMonitorJSONBlockJobSetSpeed(mon, device, bandwidth, modern);
+}
+
+
+int
 qemuMonitorBlockJobInfo(qemuMonitorPtr mon,
                         const char *device,
                         virDomainBlockJobInfoPtr info,
diff --git a/src/qemu/qemu_monitor.h b/src/qemu/qemu_monitor.h
index a3514cf..78f4648 100644
--- a/src/qemu/qemu_monitor.h
+++ b/src/qemu/qemu_monitor.h
@@ -757,7 +757,6 @@ int qemuMonitorSendKey(qemuMonitorPtr mon,

 typedef enum {
     BLOCK_JOB_ABORT,
-    BLOCK_JOB_SPEED,
     BLOCK_JOB_PULL,
 } qemuMonitorBlockJobCmd;

@@ -770,6 +769,11 @@ int qemuMonitorBlockJob(qemuMonitorPtr mon,
                         bool modern)
     ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);

+int qemuMonitorBlockJobSetSpeed(qemuMonitorPtr mon,
+                                const char *device,
+                                unsigned long long bandwidth,
+                                bool modern);
+
 int qemuMonitorBlockJobInfo(qemuMonitorPtr mon,
                             const char *device,
                             virDomainBlockJobInfoPtr info,
diff --git a/src/qemu/qemu_monitor_json.c b/src/qemu/qemu_monitor_json.c
index f449d0e..9482982 100644
--- a/src/qemu/qemu_monitor_json.c
+++ b/src/qemu/qemu_monitor_json.c
@@ -4328,14 +4328,6 @@ qemuMonitorJSONBlockJob(qemuMonitorPtr mon,
                                          NULL);
         break;

-    case BLOCK_JOB_SPEED:
-        cmd_name = modern ? "block-job-set-speed" : "block_job_set_speed";
-        cmd = qemuMonitorJSONMakeCommand(cmd_name,
-                                         "s:device", device,
-                                         modern ? "J:speed" : "J:value", speed,
-                                         NULL);
-        break;
-
     case BLOCK_JOB_PULL:
         cmd_name = modern ? "block-stream" : "block_stream";
         cmd = qemuMonitorJSONMakeCommand(cmd_name,
@@ -4364,6 +4356,39 @@ qemuMonitorJSONBlockJob(qemuMonitorPtr mon,
     return ret;
 }

+
+int
+qemuMonitorJSONBlockJobSetSpeed(qemuMonitorPtr mon,
+                                const char *device,
+                                unsigned long long speed,
+                                bool modern)
+{
+    int ret = -1;
+    virJSONValuePtr cmd;
+    virJSONValuePtr reply = NULL;
+    const char *cmd_name = modern ? "block-job-set-speed" : "block_job_set_speed";
+
+    if (!(cmd = qemuMonitorJSONMakeCommand(cmd_name,
+                                           "s:device", device,
+                                           modern ? "J:speed" : "J:value", speed,
+                                           NULL)))
+        return -1;
+
+    if (qemuMonitorJSONCommand(mon, cmd, &reply) < 0)
+        goto cleanup;
+
+    if (qemuMonitorJSONBlockJobError(reply, cmd_name, device) < 0)
+        goto cleanup;
+
+    ret = 0;
+
+ cleanup:
+    virJSONValueFree(cmd);
+    virJSONValueFree(reply);
+    return ret;
+}
+
+
 int qemuMonitorJSONOpenGraphics(qemuMonitorPtr mon,
                                 const char *protocol,
                                 const char *fdname,
diff --git a/src/qemu/qemu_monitor_json.h b/src/qemu/qemu_monitor_json.h
index fd86447..5185bbf 100644
--- a/src/qemu/qemu_monitor_json.h
+++ b/src/qemu/qemu_monitor_json.h
@@ -306,6 +306,12 @@ int qemuMonitorJSONBlockJob(qemuMonitorPtr mon,
                             bool modern)
     ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);

+int qemuMonitorJSONBlockJobSetSpeed(qemuMonitorPtr mon,
+                                    const char *device,
+                                    unsigned long long speed,
+                                    bool modern)
+    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
+
 int qemuMonitorJSONBlockJobInfo(qemuMonitorPtr mon,
                                 const char *device,
                                 virDomainBlockJobInfoPtr info,
-- 
2.2.2


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]