]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
qemu: domain: Add helper for translating disk cachemode to qemu flags
authorPeter Krempa <pkrempa@redhat.com>
Wed, 4 Apr 2018 06:43:06 +0000 (08:43 +0200)
committerPeter Krempa <pkrempa@redhat.com>
Thu, 19 Apr 2018 09:20:34 +0000 (11:20 +0200)
Add helper which will map values of disk cache mode to the flags which
are accepted by various parts of the qemu block layer.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
src/qemu/qemu_domain.c
src/qemu/qemu_domain.h

index 21897cb47ae147ac8f8d51942bb06562199118bc..e2a8450e2e8df43cf284841504ef3d498797e6c8 100644 (file)
@@ -11866,6 +11866,81 @@ qemuDomainPrepareDiskSource(virDomainDiskDefPtr disk,
 }
 
 
+/**
+ * qemuDomainDiskCachemodeFlags:
+ *
+ * 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.
+ */
+int
+qemuDomainDiskCachemodeFlags(int cachemode,
+                             bool *writeback,
+                             bool *direct,
+                             bool *noflush)
+{
+    bool dummy;
+
+    if (!writeback)
+        writeback = &dummy;
+
+    if (!direct)
+        direct = &dummy;
+
+    if (!noflush)
+        noflush = &dummy;
+
+    /* Mapping of cache modes to the attributes according to qemu-options.hx
+     *              │ cache.writeback   cache.direct   cache.no-flush
+     * ─────────────┼─────────────────────────────────────────────────
+     * writeback    │ true              false          false
+     * none         │ true              true           false
+     * writethrough │ false             false          false
+     * directsync   │ false             true           false
+     * unsafe       │ true              false          true
+     */
+    switch ((virDomainDiskCache) cachemode) {
+    case VIR_DOMAIN_DISK_CACHE_DISABLE: /* 'none' */
+        *writeback = true;
+        *direct = true;
+        *noflush = false;
+        break;
+
+    case VIR_DOMAIN_DISK_CACHE_WRITETHRU:
+        *writeback = false;
+        *direct = false;
+        *noflush = false;
+        break;
+
+    case VIR_DOMAIN_DISK_CACHE_WRITEBACK:
+        *writeback = true;
+        *direct = false;
+        *noflush = false;
+        break;
+
+    case VIR_DOMAIN_DISK_CACHE_DIRECTSYNC:
+        *writeback = false;
+        *direct = true;
+        *noflush = false;
+        break;
+
+    case VIR_DOMAIN_DISK_CACHE_UNSAFE:
+        *writeback = true;
+        *direct = false;
+        *noflush = true;
+        break;
+
+    case VIR_DOMAIN_DISK_CACHE_DEFAULT:
+    case VIR_DOMAIN_DISK_CACHE_LAST:
+    default:
+        virReportEnumRangeError(virDomainDiskCache, cachemode);
+        return -1;
+    }
+
+    return 0;
+}
+
+
 void
 qemuProcessEventFree(struct qemuProcessEvent *event)
 {
index 40d9a6f2472c963f2ae76fff334af3a250fecebc..2c27dfb9f31a38a607b2898117f3852013c5f9b0 100644 (file)
@@ -994,4 +994,10 @@ qemuDomainPrepareDiskSource(virDomainDiskDefPtr disk,
                             qemuDomainObjPrivatePtr priv,
                             virQEMUDriverConfigPtr cfg);
 
+int
+qemuDomainDiskCachemodeFlags(int cachemode,
+                             bool *writeback,
+                             bool *direct,
+                             bool *noflush);
+
 #endif /* __QEMU_DOMAIN_H__ */