qemuDomainDiskCachemodeFlags: Simplify usage

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@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
This commit is contained in:
Peter Krempa 2023-10-18 13:19:08 +02:00
parent 91661eb19f
commit 4cf223962a
4 changed files with 27 additions and 30 deletions

View File

@ -919,12 +919,9 @@ qemuBlockStorageSourceGetBlockdevGetCacheProps(virStorageSource *src,
bool direct = false; bool direct = false;
bool noflush = false; bool noflush = false;
if (src->cachemode == VIR_DOMAIN_DISK_CACHE_DEFAULT) if (!qemuDomainDiskCachemodeFlags(src->cachemode, NULL, &direct, &noflush))
return 0; return 0;
if (qemuDomainDiskCachemodeFlags(src->cachemode, NULL, &direct, &noflush) < 0)
return -1;
if (virJSONValueObjectAdd(&cacheobj, if (virJSONValueObjectAdd(&cacheobj,
"b:direct", direct, "b:direct", direct,
"b:no-flush", noflush, "b:no-flush", noflush,

View File

@ -1889,16 +1889,13 @@ qemuBuildDiskDeviceProps(const virDomainDef *def,
wwn = virJSONValueNewNumberUlong(w); wwn = virJSONValueNewNumberUlong(w);
} }
if (disk->cachemode != VIR_DOMAIN_DISK_CACHE_DEFAULT) { /* 'write-cache' component of disk->cachemode is set on device level.
/* VIR_DOMAIN_DISK_DEVICE_LUN translates into 'scsi-block' * VIR_DOMAIN_DISK_DEVICE_LUN translates into 'scsi-block' where any
* where any caching setting makes no sense. */ * caching setting makes no sense. */
if (disk->device != VIR_DOMAIN_DISK_DEVICE_LUN) { if (disk->device != VIR_DOMAIN_DISK_DEVICE_LUN) {
bool wb; bool wb;
if (qemuDomainDiskCachemodeFlags(disk->cachemode, &wb, NULL,
NULL) < 0)
return NULL;
if (qemuDomainDiskCachemodeFlags(disk->cachemode, &wb, NULL, NULL)) {
writeCache = virTristateSwitchFromBool(wb); writeCache = virTristateSwitchFromBool(wb);
} }
} }

View File

@ -11406,13 +11406,18 @@ qemuDomainPrepareHostdev(virDomainHostdevDef *hostdev,
/** /**
* qemuDomainDiskCachemodeFlags: * 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 * Converts disk @cachemode to the cache mode options for qemu according to the
* invalid @cachemode values and fills the flags and returns 0 on success. * table below.
* Flags may be NULL. *
* Returns true if @cachemode is a relevant cache mode setting.
*/ */
int bool
qemuDomainDiskCachemodeFlags(int cachemode, qemuDomainDiskCachemodeFlags(virDomainDiskCache cachemode,
bool *writeback, bool *writeback,
bool *direct, bool *direct,
bool *noflush) bool *noflush)
@ -11437,45 +11442,43 @@ qemuDomainDiskCachemodeFlags(int cachemode,
* directsync false true false * directsync false true false
* unsafe true false true * unsafe true false true
*/ */
switch ((virDomainDiskCache) cachemode) { switch (cachemode) {
case VIR_DOMAIN_DISK_CACHE_DISABLE: /* 'none' */ case VIR_DOMAIN_DISK_CACHE_DISABLE: /* 'none' */
*writeback = true; *writeback = true;
*direct = true; *direct = true;
*noflush = false; *noflush = false;
break; return true;
case VIR_DOMAIN_DISK_CACHE_WRITETHRU: case VIR_DOMAIN_DISK_CACHE_WRITETHRU:
*writeback = false; *writeback = false;
*direct = false; *direct = false;
*noflush = false; *noflush = false;
break; return true;
case VIR_DOMAIN_DISK_CACHE_WRITEBACK: case VIR_DOMAIN_DISK_CACHE_WRITEBACK:
*writeback = true; *writeback = true;
*direct = false; *direct = false;
*noflush = false; *noflush = false;
break; return true;
case VIR_DOMAIN_DISK_CACHE_DIRECTSYNC: case VIR_DOMAIN_DISK_CACHE_DIRECTSYNC:
*writeback = false; *writeback = false;
*direct = true; *direct = true;
*noflush = false; *noflush = false;
break; return true;
case VIR_DOMAIN_DISK_CACHE_UNSAFE: case VIR_DOMAIN_DISK_CACHE_UNSAFE:
*writeback = true; *writeback = true;
*direct = false; *direct = false;
*noflush = true; *noflush = true;
break; return true;
case VIR_DOMAIN_DISK_CACHE_DEFAULT: case VIR_DOMAIN_DISK_CACHE_DEFAULT:
case VIR_DOMAIN_DISK_CACHE_LAST: case VIR_DOMAIN_DISK_CACHE_LAST:
default: return false;
virReportEnumRangeError(virDomainDiskCache, cachemode);
return -1;
} }
return 0; return false;
} }

View File

@ -1008,8 +1008,8 @@ qemuDomainPrepareDiskSource(virDomainDiskDef *disk,
qemuDomainObjPrivate *priv, qemuDomainObjPrivate *priv,
virQEMUDriverConfig *cfg); virQEMUDriverConfig *cfg);
int bool
qemuDomainDiskCachemodeFlags(int cachemode, qemuDomainDiskCachemodeFlags(virDomainDiskCache cachemode,
bool *writeback, bool *writeback,
bool *direct, bool *direct,
bool *noflush); bool *noflush);