]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
qemuDomainDiskCachemodeFlags: Simplify usage
authorPeter Krempa <pkrempa@redhat.com>
Wed, 18 Oct 2023 11:19:08 +0000 (13:19 +0200)
committerPeter Krempa <pkrempa@redhat.com>
Thu, 26 Oct 2023 07:02:23 +0000 (09:02 +0200)
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>
src/qemu/qemu_block.c
src/qemu/qemu_command.c
src/qemu/qemu_domain.c
src/qemu/qemu_domain.h

index 3be12b47e33e5c58da79c3765c0d0b5eb640d1b8..0f47b5b37fa59ef0d747f9e64e1806592118cf9a 100644 (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,
index dbef8d006835495fe82125a166c9ac100415b36e..fd0f12f30401b6f6638080185d88961cc6af4edb 100644 (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);
         }
     }
index 995aa3f79c62c82fc6b274089711649f5ea8899f..ae19ce884b0cd8b1bcd62267e26b8ff41d02215f 100644 (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;
 }
 
 
index a3fc6acaaa10b7c526f5310d6b61530173aadd5c..1e56e50672a447c94e90fb77d05a5e2c5fe6cf93 100644 (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);