From 27dee7b5a4152ceea00662a6f112c994d7f2b818 Mon Sep 17 00:00:00 2001 Message-Id: <27dee7b5a4152ceea00662a6f112c994d7f2b818@dist-git> From: Michal Privoznik Date: Fri, 6 Mar 2020 15:52:23 +0100 Subject: [PATCH] RHEL: virscsi: Introduce and use virSCSIDeviceGetUnprivSGIOSysfsPath() When constructing a path to the 'unpriv_sgio' file of given SCSI device we don't need to go through /dev/* and major() + minor() path. The generated path points to /sys/dev/block/MAJ:MIN/queue/unpriv_sgio which is wrong if the SCSI device in question is not a block device. We can generate a different path: /sys/bus/scsi/devices/X:X:X:X/unpriv_sgio where the file is directly accessible regardless of the SCSI device type. https://bugzilla.redhat.com/show_bug.cgi?id=1808390 Signed-off-by: Michal Privoznik Signed-off-by: Andrea Bolognani Message-Id: <20200306145226.1610708-4-abologna@redhat.com> Reviewed-by: Jiri Denemark --- src/libvirt_private.syms | 1 + src/qemu/qemu_conf.c | 29 ++++++++++++++++++----------- src/util/virscsi.c | 19 +++++++++++++++++++ src/util/virscsi.h | 5 +++++ src/util/virutil.c | 24 ++++++------------------ src/util/virutil.h | 2 -- 6 files changed, 49 insertions(+), 31 deletions(-) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 43493ea76e..0f701ed1ae 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -3168,6 +3168,7 @@ virSCSIDeviceGetSgName; virSCSIDeviceGetShareable; virSCSIDeviceGetTarget; virSCSIDeviceGetUnit; +virSCSIDeviceGetUnprivSGIOSysfsPath; virSCSIDeviceIsAvailable; virSCSIDeviceListAdd; virSCSIDeviceListCount; diff --git a/src/qemu/qemu_conf.c b/src/qemu/qemu_conf.c index 2beef89cd1..90ff7f56f0 100644 --- a/src/qemu/qemu_conf.c +++ b/src/qemu/qemu_conf.c @@ -1500,7 +1500,7 @@ qemuCheckUnprivSGIO(GHashTable *sharedDevices, if (!(virHashLookup(sharedDevices, key))) return 0; - if (virGetDeviceUnprivSGIO(device_path, NULL, &val) < 0) + if (virGetDeviceUnprivSGIO(sysfs_path, &val) < 0) return -1; /* Error message on failure needs to be handled in caller @@ -1852,39 +1852,46 @@ qemuSetUnprivSGIO(virDomainDeviceDef *dev) virDomainDiskDef *disk = NULL; virDomainHostdevDef *hostdev = NULL; g_autofree char *sysfs_path = NULL; - g_autofree char *hostdev_path = NULL; - const char *path = NULL; int val = 0; /* "sgio" is only valid for block disk; cdrom * and floopy disk can have empty source. */ if (dev->type == VIR_DOMAIN_DEVICE_DISK) { + const char *path; + disk = dev->data.disk; + path = virDomainDiskGetSource(disk); if (disk->device != VIR_DOMAIN_DISK_DEVICE_LUN || !virStorageSourceIsBlockLocal(disk->src)) return 0; - path = virDomainDiskGetSource(disk); + if (!(sysfs_path = virGetUnprivSGIOSysfsPath(path, NULL))) + return -1; + } else if (dev->type == VIR_DOMAIN_DEVICE_HOSTDEV) { + virDomainHostdevSubsysSCSI *scsisrc; + virDomainHostdevSubsysSCSIHost *scsihostsrc; + hostdev = dev->data.hostdev; + scsisrc = &hostdev->source.subsys.u.scsi; + scsihostsrc = &scsisrc->u.host; if (hostdev->source.subsys.u.scsi.protocol == VIR_DOMAIN_HOSTDEV_SCSI_PROTOCOL_TYPE_ISCSI) return 0; - if (!(hostdev_path = qemuGetHostdevPath(hostdev))) + if (!(sysfs_path = virSCSIDeviceGetUnprivSGIOSysfsPath(NULL, + scsihostsrc->adapter, + scsihostsrc->bus, + scsihostsrc->target, + scsihostsrc->unit))) return -1; - - path = hostdev_path; } else { return 0; } - if (!(sysfs_path = virGetUnprivSGIOSysfsPath(path, NULL))) - return -1; - /* By default, filter the SG_IO commands, i.e. set unpriv_sgio to 0. */ if (dev->type == VIR_DOMAIN_DEVICE_DISK) { if (disk->sgio == VIR_DOMAIN_DEVICE_SGIO_UNFILTERED) @@ -1902,7 +1909,7 @@ qemuSetUnprivSGIO(virDomainDeviceDef *dev) * virSetDeviceUnprivSGIO, to report an error for unsupported unpriv_sgio. */ if ((virFileExists(sysfs_path) || val == 1) && - virSetDeviceUnprivSGIO(path, NULL, val) < 0) + virSetDeviceUnprivSGIO(sysfs_path, val) < 0) return -1; return 0; diff --git a/src/util/virscsi.c b/src/util/virscsi.c index d6c10c0667..7a8e6b3b23 100644 --- a/src/util/virscsi.c +++ b/src/util/virscsi.c @@ -302,6 +302,25 @@ virSCSIDeviceGetDevName(const char *sysfs_prefix, } } + +char * +virSCSIDeviceGetUnprivSGIOSysfsPath(const char *sysfs_prefix, + const char *adapter, + unsigned int bus, + unsigned int target, + unsigned long long unit) +{ + unsigned int adapter_id; + const char *prefix = sysfs_prefix ? sysfs_prefix : SYSFS_SCSI_DEVICES; + + if (virSCSIDeviceGetAdapterId(adapter, &adapter_id) < 0) + return NULL; + + return g_strdup_printf("%s/%d:%u:%u:%llu/unpriv_sgio", + prefix, adapter_id, bus, target, unit); +} + + virSCSIDevice * virSCSIDeviceNew(const char *sysfs_prefix, const char *adapter, diff --git a/src/util/virscsi.h b/src/util/virscsi.h index 65ad15ed76..5721985939 100644 --- a/src/util/virscsi.h +++ b/src/util/virscsi.h @@ -40,6 +40,11 @@ char *virSCSIDeviceGetDevName(const char *sysfs_prefix, unsigned int bus, unsigned int target, unsigned long long unit); +char *virSCSIDeviceGetUnprivSGIOSysfsPath(const char *sysfs_prefix, + const char *adapter, + unsigned int bus, + unsigned int target, + unsigned long long unit); virSCSIDevice *virSCSIDeviceNew(const char *sysfs_prefix, const char *adapter, diff --git a/src/util/virutil.c b/src/util/virutil.c index 00cd56e2b2..d0fc49c39f 100644 --- a/src/util/virutil.c +++ b/src/util/virutil.c @@ -1382,18 +1382,13 @@ virGetUnprivSGIOSysfsPath(const char *path, int virSetDeviceUnprivSGIO(const char *path, - const char *sysfs_dir, int unpriv_sgio) { - char *sysfs_path = NULL; char *val = NULL; int ret = -1; int rc; - if (!(sysfs_path = virGetUnprivSGIOSysfsPath(path, sysfs_dir))) - return -1; - - if (!virFileExists(sysfs_path)) { + if (!virFileExists(path)) { virReportError(VIR_ERR_OPERATION_INVALID, "%s", _("unpriv_sgio is not supported by this kernel")); goto cleanup; @@ -1401,38 +1396,32 @@ virSetDeviceUnprivSGIO(const char *path, val = g_strdup_printf("%d", unpriv_sgio); - if ((rc = virFileWriteStr(sysfs_path, val, 0)) < 0) { - virReportSystemError(-rc, _("failed to set %s"), sysfs_path); + if ((rc = virFileWriteStr(path, val, 0)) < 0) { + virReportSystemError(-rc, _("failed to set %s"), path); goto cleanup; } ret = 0; cleanup: - VIR_FREE(sysfs_path); VIR_FREE(val); return ret; } int virGetDeviceUnprivSGIO(const char *path, - const char *sysfs_dir, int *unpriv_sgio) { - char *sysfs_path = NULL; char *buf = NULL; char *tmp = NULL; int ret = -1; - if (!(sysfs_path = virGetUnprivSGIOSysfsPath(path, sysfs_dir))) - return -1; - - if (!virFileExists(sysfs_path)) { + if (!virFileExists(path)) { virReportError(VIR_ERR_OPERATION_INVALID, "%s", _("unpriv_sgio is not supported by this kernel")); goto cleanup; } - if (virFileReadAll(sysfs_path, 1024, &buf) < 0) + if (virFileReadAll(path, 1024, &buf) < 0) goto cleanup; if ((tmp = strchr(buf, '\n'))) @@ -1440,13 +1429,12 @@ virGetDeviceUnprivSGIO(const char *path, if (virStrToLong_i(buf, NULL, 10, unpriv_sgio) < 0) { virReportError(VIR_ERR_INTERNAL_ERROR, - _("failed to parse value of %s"), sysfs_path); + _("failed to parse value of %s"), path); goto cleanup; } ret = 0; cleanup: - VIR_FREE(sysfs_path); VIR_FREE(buf); return ret; } diff --git a/src/util/virutil.h b/src/util/virutil.h index 854b494890..da267c6446 100644 --- a/src/util/virutil.h +++ b/src/util/virutil.h @@ -120,10 +120,8 @@ int virGetDeviceID(const char *path, int *maj, int *min) G_GNUC_NO_INLINE; int virSetDeviceUnprivSGIO(const char *path, - const char *sysfs_dir, int unpriv_sgio); int virGetDeviceUnprivSGIO(const char *path, - const char *sysfs_dir, int *unpriv_sgio); char *virGetUnprivSGIOSysfsPath(const char *path, const char *sysfs_dir); -- 2.32.0