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>
This commit is contained in:
Erik Skultety 2017-04-26 14:47:15 +02:00
parent 1128769f9e
commit 3a2a2a7401
6 changed files with 21 additions and 62 deletions

View File

@ -7258,7 +7258,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;
@ -7359,11 +7358,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;
@ -7419,7 +7414,6 @@ qemuDomainGetHostdevPath(virDomainDefPtr def,
virUSBDeviceFree(usb);
virSCSIDeviceFree(scsi);
virSCSIVHostDeviceFree(host);
virMediatedDeviceFree(mdev);
if (freeTmpPath)
VIR_FREE(tmpPath);
return ret;

View File

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

View File

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

View File

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

View File

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

View File

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