Return whether a relevant cachemode was presented rather than returning
an error, so that callers can be simplified. Use the proper enum type as
argument rather than typecasting in the switch statement.
Signed-off-by: Peter Krempa <pkrempa(a)redhat.com>
---
src/qemu/qemu_block.c | 5 +----
src/qemu/qemu_command.c | 15 ++++++---------
src/qemu/qemu_domain.c | 31 +++++++++++++++++--------------
src/qemu/qemu_domain.h | 4 ++--
4 files changed, 26 insertions(+), 29 deletions(-)
diff --git a/src/qemu/qemu_block.c b/src/qemu/qemu_block.c
index 3be12b47e3..0f47b5b37f 100644
--- a/src/qemu/qemu_block.c
+++ b/src/qemu/qemu_block.c
@@ -919,12 +919,9 @@ qemuBlockStorageSourceGetBlockdevGetCacheProps(virStorageSource
*src,
bool direct = false;
bool noflush = false;
- if (src->cachemode == VIR_DOMAIN_DISK_CACHE_DEFAULT)
+ if (!qemuDomainDiskCachemodeFlags(src->cachemode, NULL, &direct,
&noflush))
return 0;
- if (qemuDomainDiskCachemodeFlags(src->cachemode, NULL, &direct, &noflush)
< 0)
- return -1;
-
if (virJSONValueObjectAdd(&cacheobj,
"b:direct", direct,
"b:no-flush", noflush,
diff --git a/src/qemu/qemu_command.c b/src/qemu/qemu_command.c
index dbef8d0068..fd0f12f304 100644
--- a/src/qemu/qemu_command.c
+++ b/src/qemu/qemu_command.c
@@ -1889,16 +1889,13 @@ qemuBuildDiskDeviceProps(const virDomainDef *def,
wwn = virJSONValueNewNumberUlong(w);
}
- if (disk->cachemode != VIR_DOMAIN_DISK_CACHE_DEFAULT) {
- /* VIR_DOMAIN_DISK_DEVICE_LUN translates into 'scsi-block'
- * where any caching setting makes no sense. */
- if (disk->device != VIR_DOMAIN_DISK_DEVICE_LUN) {
- bool wb;
-
- if (qemuDomainDiskCachemodeFlags(disk->cachemode, &wb, NULL,
- NULL) < 0)
- return NULL;
+ /* 'write-cache' component of disk->cachemode is set on device level.
+ * VIR_DOMAIN_DISK_DEVICE_LUN translates into 'scsi-block' where any
+ * caching setting makes no sense. */
+ if (disk->device != VIR_DOMAIN_DISK_DEVICE_LUN) {
+ bool wb;
+ if (qemuDomainDiskCachemodeFlags(disk->cachemode, &wb, NULL, NULL)) {
writeCache = virTristateSwitchFromBool(wb);
}
}
diff --git a/src/qemu/qemu_domain.c b/src/qemu/qemu_domain.c
index 995aa3f79c..0b36a49bdf 100644
--- a/src/qemu/qemu_domain.c
+++ b/src/qemu/qemu_domain.c
@@ -11406,13 +11406,18 @@ qemuDomainPrepareHostdev(virDomainHostdevDef *hostdev,
/**
* qemuDomainDiskCachemodeFlags:
+ * @cachemode: aggregated cache mode
+ * @writeback: populated with 'writeback' component of @cachemode (may be NULL)
+ * @direct: populated with 'direct' component of @cachemode (may be NULL)
+ * @noflush: populated with 'noflush' component of @cachemode (may be NULL)
*
- * Converts disk cachemode to the cache mode options for qemu. Returns -1 for
- * invalid @cachemode values and fills the flags and returns 0 on success.
- * Flags may be NULL.
+ * Converts disk @cachemode to the cache mode options for qemu according to the
+ * table below.
+ *
+ * Returns true if @cachemode is a relevant cache mode setting.
*/
-int
-qemuDomainDiskCachemodeFlags(int cachemode,
+bool
+qemuDomainDiskCachemodeFlags(virDomainDiskCache cachemode,
bool *writeback,
bool *direct,
bool *noflush)
@@ -11442,40 +11447,38 @@ qemuDomainDiskCachemodeFlags(int cachemode,
*writeback = true;
*direct = true;
*noflush = false;
- break;
+ return true;
case VIR_DOMAIN_DISK_CACHE_WRITETHRU:
*writeback = false;
*direct = false;
*noflush = false;
- break;
+ return true;
case VIR_DOMAIN_DISK_CACHE_WRITEBACK:
*writeback = true;
*direct = false;
*noflush = false;
- break;
+ return true;
case VIR_DOMAIN_DISK_CACHE_DIRECTSYNC:
*writeback = false;
*direct = true;
*noflush = false;
- break;
+ return true;
case VIR_DOMAIN_DISK_CACHE_UNSAFE:
*writeback = true;
*direct = false;
*noflush = true;
- break;
+ return true;
case VIR_DOMAIN_DISK_CACHE_DEFAULT:
case VIR_DOMAIN_DISK_CACHE_LAST:
- default:
- virReportEnumRangeError(virDomainDiskCache, cachemode);
- return -1;
+ return false;
}
- return 0;
+ return false;
}
diff --git a/src/qemu/qemu_domain.h b/src/qemu/qemu_domain.h
index a3fc6acaaa..1e56e50672 100644
--- a/src/qemu/qemu_domain.h
+++ b/src/qemu/qemu_domain.h
@@ -1008,8 +1008,8 @@ qemuDomainPrepareDiskSource(virDomainDiskDef *disk,
qemuDomainObjPrivate *priv,
virQEMUDriverConfig *cfg);
-int
-qemuDomainDiskCachemodeFlags(int cachemode,
+bool
+qemuDomainDiskCachemodeFlags(virDomainDiskCache cachemode,
bool *writeback,
bool *direct,
bool *noflush);
--
2.41.0