d76c62
From 3366d4df5aac258013848cf5df7c95d45693c1e0 Mon Sep 17 00:00:00 2001
d76c62
Message-Id: <3366d4df5aac258013848cf5df7c95d45693c1e0@dist-git>
d76c62
From: =?UTF-8?q?J=C3=A1n=20Tomko?= <jtomko@redhat.com>
d76c62
Date: Wed, 4 Mar 2020 12:42:44 +0100
d76c62
Subject: [PATCH] qemu: validate virtiofs filesystems
d76c62
MIME-Version: 1.0
d76c62
Content-Type: text/plain; charset=UTF-8
d76c62
Content-Transfer-Encoding: 8bit
d76c62
d76c62
Reject unsupported configurations.
d76c62
d76c62
Signed-off-by: Ján Tomko <jtomko@redhat.com>
d76c62
Reviewed-by: Peter Krempa <pkrempa@redhat.com>
d76c62
Tested-by: Andrea Bolognani <abologna@redhat.com>
d76c62
Reviewed-by: Masayoshi Mizuma <m.mizuma@jp.fujitsu.com>
d76c62
(cherry picked from commit efaf46811c909ee5333360fba1d75ae82352964a)
d76c62
Signed-off-by: Ján Tomko <jtomko@redhat.com>
d76c62
https://bugzilla.redhat.com/show_bug.cgi?id=1694166
d76c62
Message-Id: <1fcb6b7d58c3791799c5d436edaa2faa07e92305.1583322091.git.jtomko@redhat.com>
d76c62
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
d76c62
---
d76c62
 src/qemu/qemu_domain.c | 82 +++++++++++++++++++++++++++++++++++++++---
d76c62
 1 file changed, 77 insertions(+), 5 deletions(-)
d76c62
d76c62
diff --git a/src/qemu/qemu_domain.c b/src/qemu/qemu_domain.c
d76c62
index 402b079b09..79d8de2e42 100644
d76c62
--- a/src/qemu/qemu_domain.c
d76c62
+++ b/src/qemu/qemu_domain.c
d76c62
@@ -8329,11 +8329,52 @@ qemuDomainDeviceDefValidateIOMMU(const virDomainIOMMUDef *iommu,
d76c62
     return 0;
d76c62
 }
d76c62
 
d76c62
+static int
d76c62
+qemuDomainDefValidateVirtioFSSharedMemory(const virDomainDef *def)
d76c62
+{
d76c62
+    size_t numa_nodes = virDomainNumaGetNodeCount(def->numa);
d76c62
+    size_t i;
d76c62
+
d76c62
+    if (numa_nodes == 0) {
d76c62
+        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
d76c62
+                       _("virtiofs requires one or more NUMA nodes"));
d76c62
+        return -1;
d76c62
+    }
d76c62
+
d76c62
+    for (i = 0; i < numa_nodes; i++) {
d76c62
+        virDomainMemoryAccess node_access =
d76c62
+            virDomainNumaGetNodeMemoryAccessMode(def->numa, i);
d76c62
+
d76c62
+        switch (node_access) {
d76c62
+        case VIR_DOMAIN_MEMORY_ACCESS_DEFAULT:
d76c62
+            if (def->mem.access != VIR_DOMAIN_MEMORY_ACCESS_SHARED) {
d76c62
+                virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
d76c62
+                               _("virtiofs requires shared memory"));
d76c62
+                return -1;
d76c62
+            }
d76c62
+            break;
d76c62
+        case VIR_DOMAIN_MEMORY_ACCESS_SHARED:
d76c62
+            break;
d76c62
+        case VIR_DOMAIN_MEMORY_ACCESS_PRIVATE:
d76c62
+            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
d76c62
+                           _("virtiofs requires shared memory"));
d76c62
+            return -1;
d76c62
+
d76c62
+        case VIR_DOMAIN_MEMORY_ACCESS_LAST:
d76c62
+        default:
d76c62
+            virReportEnumRangeError(virDomainMemoryAccess, node_access);
d76c62
+            return -1;
d76c62
+
d76c62
+        }
d76c62
+    }
d76c62
+    return 0;
d76c62
+}
d76c62
 
d76c62
 static int
d76c62
 qemuDomainDeviceDefValidateFS(virDomainFSDefPtr fs,
d76c62
-                              const virDomainDef *def G_GNUC_UNUSED,
d76c62
-                              virQEMUCapsPtr qemuCaps G_GNUC_UNUSED)
d76c62
+                              const virDomainDef *def,
d76c62
+                              virQEMUDriverPtr driver,
d76c62
+                              virQEMUCapsPtr qemuCaps)
d76c62
 {
d76c62
     if (fs->type != VIR_DOMAIN_FS_TYPE_MOUNT) {
d76c62
         virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
d76c62
@@ -8362,8 +8403,39 @@ qemuDomainDeviceDefValidateFS(virDomainFSDefPtr fs,
d76c62
         return -1;
d76c62
 
d76c62
     case VIR_DOMAIN_FS_DRIVER_TYPE_VIRTIOFS:
d76c62
-        /* TODO: vhost-user-fs-pci */
d76c62
-        return 0;
d76c62
+        if (!virQEMUDriverIsPrivileged(driver)) {
d76c62
+            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
d76c62
+                           _("virtiofs is not yet supported in session mode"));
d76c62
+            return -1;
d76c62
+        }
d76c62
+        if (fs->accessmode != VIR_DOMAIN_FS_ACCESSMODE_PASSTHROUGH) {
d76c62
+            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
d76c62
+                           _("virtiofs only supports passthrough accessmode"));
d76c62
+            return -1;
d76c62
+        }
d76c62
+        if (fs->wrpolicy != VIR_DOMAIN_FS_WRPOLICY_DEFAULT) {
d76c62
+            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
d76c62
+                           _("virtiofs does not support wrpolicy"));
d76c62
+            return -1;
d76c62
+        }
d76c62
+        if (fs->model != VIR_DOMAIN_FS_MODEL_DEFAULT) {
d76c62
+            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
d76c62
+                           _("virtiofs does not support model"));
d76c62
+            return -1;
d76c62
+        }
d76c62
+        if (fs->format != VIR_STORAGE_FILE_NONE) {
d76c62
+            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
d76c62
+                           _("virtiofs does not support format"));
d76c62
+            return -1;
d76c62
+        }
d76c62
+        if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_DEVICE_VHOST_USER_FS)) {
d76c62
+            virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
d76c62
+                           _("virtiofs is not supported with this QEMU binary"));
d76c62
+            return -1;
d76c62
+        }
d76c62
+        if (qemuDomainDefValidateVirtioFSSharedMemory(def) < 0)
d76c62
+            return -1;
d76c62
+        break;
d76c62
 
d76c62
     case VIR_DOMAIN_FS_DRIVER_TYPE_LAST:
d76c62
     default:
d76c62
@@ -8542,7 +8614,7 @@ qemuDomainDeviceDefValidate(const virDomainDeviceDef *dev,
d76c62
         break;
d76c62
 
d76c62
     case VIR_DOMAIN_DEVICE_FS:
d76c62
-        ret = qemuDomainDeviceDefValidateFS(dev->data.fs, def, qemuCaps);
d76c62
+        ret = qemuDomainDeviceDefValidateFS(dev->data.fs, def, driver, qemuCaps);
d76c62
         break;
d76c62
 
d76c62
     case VIR_DOMAIN_DEVICE_NVRAM:
d76c62
-- 
d76c62
2.25.1
d76c62