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.
---
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 6a2b58d..eec5457 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -16339,10 +16339,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"),
@@ -16532,23 +16530,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 5da0b8f..aae259f 100644
--- a/src/qemu/qemu_monitor.c
+++ b/src/qemu/qemu_monitor.c
@@ -3606,6 +3606,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 c7c3dab..9cd0d9d 100644
--- a/src/qemu/qemu_monitor.h
+++ b/src/qemu/qemu_monitor.h
@@ -759,7 +759,6 @@ int qemuMonitorSendKey(qemuMonitorPtr mon,
typedef enum {
BLOCK_JOB_ABORT,
- BLOCK_JOB_SPEED,
BLOCK_JOB_PULL,
} qemuMonitorBlockJobCmd;
@@ -772,6 +771,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 edd0a3f..34a6d56 100644
--- a/src/qemu/qemu_monitor_json.c
+++ b/src/qemu/qemu_monitor_json.c
@@ -4333,14 +4333,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,
@@ -4369,6 +4361,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 1be3b98..09f898c 100644
--- a/src/qemu/qemu_monitor_json.h
+++ b/src/qemu/qemu_monitor_json.h
@@ -308,6 +308,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