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 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,

View File

@ -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);
}
}

View File

@ -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)
@ -11437,45 +11442,43 @@ qemuDomainDiskCachemodeFlags(int cachemode,
* directsync false true false
* unsafe true false true
*/
switch ((virDomainDiskCache) cachemode) {
switch (cachemode) {
case VIR_DOMAIN_DISK_CACHE_DISABLE: /* 'none' */
*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;
}

View File

@ -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);