[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[libvirt] [PATCH v4 1/6] mdev: Pass a uuidstr rather than an mdev object to some util functions



Namely, this patch is about virMediatedDeviceGetIOMMUGroup{Dev,Num}
functions. There's no compelling reason why these functions should take
an object, on the contrary, having to create an object every time one
needs to query the IOMMU group number, discarding the object afterwards,
seems odd.

Signed-off-by: Erik Skultety <eskultet redhat com>
---
 src/qemu/qemu_domain.c           |  8 +-------
 src/security/security_apparmor.c | 10 +---------
 src/security/security_dac.c      | 20 ++------------------
 src/security/security_selinux.c  | 20 ++------------------
 src/util/virmdev.c               | 21 +++++++++++++--------
 src/util/virmdev.h               |  4 ++--
 6 files changed, 21 insertions(+), 62 deletions(-)

diff --git a/src/qemu/qemu_domain.c b/src/qemu/qemu_domain.c
index cc02c801e..76acb1c3f 100644
--- a/src/qemu/qemu_domain.c
+++ b/src/qemu/qemu_domain.c
@@ -7181,7 +7181,6 @@ qemuDomainGetHostdevPath(virDomainDefPtr def,
     virUSBDevicePtr usb = NULL;
     virSCSIDevicePtr scsi = NULL;
     virSCSIVHostDevicePtr host = NULL;
-    virMediatedDevicePtr mdev = NULL;
     char *tmpPath = NULL;
     bool freeTmpPath = false;
     bool includeVFIO = false;
@@ -7282,11 +7281,7 @@ qemuDomainGetHostdevPath(virDomainDefPtr def,
         }
 
         case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_MDEV:
-            if (!(mdev = virMediatedDeviceNew(mdevsrc->uuidstr,
-                                              mdevsrc->model)))
-                goto cleanup;
-
-            if (!(tmpPath = virMediatedDeviceGetIOMMUGroupDev(mdev)))
+            if (!(tmpPath = virMediatedDeviceGetIOMMUGroupDev(mdevsrc->uuidstr)))
                 goto cleanup;
 
             freeTmpPath = true;
@@ -7342,7 +7337,6 @@ qemuDomainGetHostdevPath(virDomainDefPtr def,
     virUSBDeviceFree(usb);
     virSCSIDeviceFree(scsi);
     virSCSIVHostDeviceFree(host);
-    virMediatedDeviceFree(mdev);
     if (freeTmpPath)
         VIR_FREE(tmpPath);
     return ret;
diff --git a/src/security/security_apparmor.c b/src/security/security_apparmor.c
index fc5581526..62672b0af 100644
--- a/src/security/security_apparmor.c
+++ b/src/security/security_apparmor.c
@@ -905,21 +905,13 @@ AppArmorSetSecurityHostdevLabel(virSecurityManagerPtr mgr,
 
     case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_MDEV: {
         char *vfiodev = NULL;
-        virMediatedDevicePtr mdev = virMediatedDeviceNew(mdevsrc->uuidstr,
-                                                         mdevsrc->model);
 
-        if (!mdev)
+        if (!(vfiodev = virMediatedDeviceGetIOMMUGroupDev(mdevsrc->uuidstr)))
             goto done;
 
-        if (!(vfiodev = virMediatedDeviceGetIOMMUGroupDev(mdev))) {
-            virMediatedDeviceFree(mdev);
-            goto done;
-        }
-
         ret = AppArmorSetSecurityHostdevLabelHelper(vfiodev, ptr);
 
         VIR_FREE(vfiodev);
-        virMediatedDeviceFree(mdev);
         break;
     }
 
diff --git a/src/security/security_dac.c b/src/security/security_dac.c
index 922e48494..7dcf4c15f 100644
--- a/src/security/security_dac.c
+++ b/src/security/security_dac.c
@@ -968,21 +968,13 @@ virSecurityDACSetHostdevLabel(virSecurityManagerPtr mgr,
 
     case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_MDEV: {
         char *vfiodev = NULL;
-        virMediatedDevicePtr mdev = virMediatedDeviceNew(mdevsrc->uuidstr,
-                                                         mdevsrc->model);
 
-        if (!mdev)
+        if (!(vfiodev = virMediatedDeviceGetIOMMUGroupDev(mdevsrc->uuidstr)))
             goto done;
 
-        if (!(vfiodev = virMediatedDeviceGetIOMMUGroupDev(mdev))) {
-            virMediatedDeviceFree(mdev);
-            goto done;
-        }
-
         ret = virSecurityDACSetHostdevLabelHelper(vfiodev, &cbdata);
 
         VIR_FREE(vfiodev);
-        virMediatedDeviceFree(mdev);
         break;
     }
 
@@ -1144,21 +1136,13 @@ virSecurityDACRestoreHostdevLabel(virSecurityManagerPtr mgr,
 
     case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_MDEV: {
         char *vfiodev = NULL;
-        virMediatedDevicePtr mdev = virMediatedDeviceNew(mdevsrc->uuidstr,
-                                                         mdevsrc->model);
 
-        if (!mdev)
+        if (!(vfiodev = virMediatedDeviceGetIOMMUGroupDev(mdevsrc->uuidstr)))
             goto done;
 
-        if (!(vfiodev = virMediatedDeviceGetIOMMUGroupDev(mdev))) {
-            virMediatedDeviceFree(mdev);
-            goto done;
-        }
-
         ret = virSecurityDACRestoreFileLabel(virSecurityManagerGetPrivateData(mgr),
                                              vfiodev);
         VIR_FREE(vfiodev);
-        virMediatedDeviceFree(mdev);
         break;
     }
 
diff --git a/src/security/security_selinux.c b/src/security/security_selinux.c
index df7c96833..c7a2dfe98 100644
--- a/src/security/security_selinux.c
+++ b/src/security/security_selinux.c
@@ -1843,21 +1843,13 @@ virSecuritySELinuxSetHostdevSubsysLabel(virSecurityManagerPtr mgr,
 
     case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_MDEV: {
         char *vfiodev = NULL;
-        virMediatedDevicePtr mdev = virMediatedDeviceNew(mdevsrc->uuidstr,
-                                                         mdevsrc->model);
 
-        if (!mdev)
+        if (!(vfiodev = virMediatedDeviceGetIOMMUGroupDev(mdevsrc->uuidstr)))
             goto done;
 
-        if (!(vfiodev = virMediatedDeviceGetIOMMUGroupDev(mdev))) {
-            virMediatedDeviceFree(mdev);
-            goto done;
-        }
-
         ret = virSecuritySELinuxSetHostdevLabelHelper(vfiodev, &data);
 
         VIR_FREE(vfiodev);
-        virMediatedDeviceFree(mdev);
         break;
     }
 
@@ -2092,21 +2084,13 @@ virSecuritySELinuxRestoreHostdevSubsysLabel(virSecurityManagerPtr mgr,
 
     case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_MDEV: {
         char *vfiodev = NULL;
-        virMediatedDevicePtr mdev = virMediatedDeviceNew(mdevsrc->uuidstr,
-                                                         mdevsrc->model);
 
-        if (!mdev)
+        if (!(vfiodev = virMediatedDeviceGetIOMMUGroupDev(mdevsrc->uuidstr)))
             goto done;
 
-        if (!(vfiodev = virMediatedDeviceGetIOMMUGroupDev(mdev))) {
-            virMediatedDeviceFree(mdev);
-            goto done;
-        }
-
         ret = virSecuritySELinuxRestoreFileLabel(mgr, vfiodev);
 
         VIR_FREE(vfiodev);
-        virMediatedDeviceFree(mdev);
         break;
     }
 
diff --git a/src/util/virmdev.c b/src/util/virmdev.c
index bd8e3f8de..a5f52d10f 100644
--- a/src/util/virmdev.c
+++ b/src/util/virmdev.c
@@ -209,43 +209,48 @@ virMediatedDeviceGetPath(virMediatedDevicePtr dev)
  * for freeing the result.
  */
 char *
-virMediatedDeviceGetIOMMUGroupDev(virMediatedDevicePtr dev)
+virMediatedDeviceGetIOMMUGroupDev(const char *uuidstr)
 {
-    char *resultpath = NULL;
+    char *result_path = NULL;
     char *iommu_path = NULL;
     char *vfio_path = NULL;
+    char *dev_path = virMediatedDeviceGetSysfsPath(uuidstr);
 
-    if (virAsprintf(&iommu_path, "%s/iommu_group", dev->path) < 0)
+    if (!dev_path)
         return NULL;
 
+    if (virAsprintf(&iommu_path, "%s/iommu_group", dev_path) < 0)
+        goto cleanup;
+
     if (!virFileExists(iommu_path)) {
         virReportSystemError(errno, _("failed to access '%s'"), iommu_path);
         goto cleanup;
     }
 
-    if (virFileResolveLink(iommu_path, &resultpath) < 0) {
+    if (virFileResolveLink(iommu_path, &result_path) < 0) {
         virReportSystemError(errno, _("failed to resolve '%s'"), iommu_path);
         goto cleanup;
     }
 
-    if (virAsprintf(&vfio_path, "/dev/vfio/%s", last_component(resultpath)) < 0)
+    if (virAsprintf(&vfio_path, "/dev/vfio/%s", last_component(result_path)) < 0)
         goto cleanup;
 
  cleanup:
-    VIR_FREE(resultpath);
+    VIR_FREE(result_path);
     VIR_FREE(iommu_path);
+    VIR_FREE(dev_path);
     return vfio_path;
 }
 
 
 int
-virMediatedDeviceGetIOMMUGroupNum(virMediatedDevicePtr dev)
+virMediatedDeviceGetIOMMUGroupNum(const char *uuidstr)
 {
     char *vfio_path = NULL;
     char *group_num_str = NULL;
     unsigned int group_num = -1;
 
-    if (!(vfio_path = virMediatedDeviceGetIOMMUGroupDev(dev)))
+    if (!(vfio_path = virMediatedDeviceGetIOMMUGroupDev(uuidstr)))
         return -1;
 
     group_num_str = last_component(vfio_path);
diff --git a/src/util/virmdev.h b/src/util/virmdev.h
index 8bb46b9c5..0b8e830f4 100644
--- a/src/util/virmdev.h
+++ b/src/util/virmdev.h
@@ -65,10 +65,10 @@ virMediatedDeviceSetUsedBy(virMediatedDevicePtr dev,
                            const char *domname);
 
 char *
-virMediatedDeviceGetIOMMUGroupDev(virMediatedDevicePtr dev);
+virMediatedDeviceGetIOMMUGroupDev(const char *uuidstr);
 
 int
-virMediatedDeviceGetIOMMUGroupNum(virMediatedDevicePtr dev);
+virMediatedDeviceGetIOMMUGroupNum(const char *uuidstr);
 
 char *
 virMediatedDeviceGetSysfsPath(const char *uuidstr);
-- 
2.13.0


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]