Blob Blame History Raw
From c9fc757c867d197c17350b6a9cabc63cc08105d2 Mon Sep 17 00:00:00 2001
Message-Id: <c9fc757c867d197c17350b6a9cabc63cc08105d2@dist-git>
From: Michal Privoznik <mprivozn@redhat.com>
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 <mprivozn@redhat.com>
Signed-off-by: Andrea Bolognani <abologna@redhat.com>
Message-Id: <20200306145226.1610708-4-abologna@redhat.com>
Reviewed-by: Jiri Denemark <jdenemar@redhat.com>
---
 src/libvirt_private.syms |  1 +
 src/qemu/qemu_conf.c     | 18 +++++++++++-------
 src/util/virscsi.c       | 18 ++++++++++++++++++
 src/util/virscsi.h       |  5 +++++
 4 files changed, 35 insertions(+), 7 deletions(-)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 5dc99e03cf..1f97879faa 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -2959,6 +2959,7 @@ virSCSIDeviceGetSgName;
 virSCSIDeviceGetShareable;
 virSCSIDeviceGetTarget;
 virSCSIDeviceGetUnit;
+virSCSIDeviceGetUnprivSGIOSysfsPath;
 virSCSIDeviceIsAvailable;
 virSCSIDeviceListAdd;
 virSCSIDeviceListCount;
diff --git a/src/qemu/qemu_conf.c b/src/qemu/qemu_conf.c
index 7aaf2862a4..6d6feb97cd 100644
--- a/src/qemu/qemu_conf.c
+++ b/src/qemu/qemu_conf.c
@@ -1789,7 +1789,6 @@ qemuSetUnprivSGIO(virDomainDeviceDefPtr dev)
     virDomainDiskDefPtr disk = NULL;
     virDomainHostdevDefPtr hostdev = NULL;
     g_autofree char *sysfs_path = NULL;
-    g_autofree char *hostdev_path = NULL;
     const char *path = NULL;
     int val = 0;
 
@@ -1804,24 +1803,29 @@ qemuSetUnprivSGIO(virDomainDeviceDefPtr dev)
             return 0;
 
         path = virDomainDiskGetSource(disk);
+
+        if (!(sysfs_path = virGetUnprivSGIOSysfsPath(path, NULL)))
+            return -1;
+
     } else if (dev->type == VIR_DOMAIN_DEVICE_HOSTDEV) {
         hostdev = dev->data.hostdev;
+        virDomainHostdevSubsysSCSIPtr scsisrc = &hostdev->source.subsys.u.scsi;
+        virDomainHostdevSubsysSCSIHostPtr 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)
diff --git a/src/util/virscsi.c b/src/util/virscsi.c
index 57958c06ea..1bba4051b6 100644
--- a/src/util/virscsi.c
+++ b/src/util/virscsi.c
@@ -322,6 +322,24 @@ 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);
+}
+
+
 virSCSIDevicePtr
 virSCSIDeviceNew(const char *sysfs_prefix,
                  const char *adapter,
diff --git a/src/util/virscsi.h b/src/util/virscsi.h
index 51627e0c05..c040d76716 100644
--- a/src/util/virscsi.h
+++ b/src/util/virscsi.h
@@ -42,6 +42,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);
 
 virSCSIDevicePtr virSCSIDeviceNew(const char *sysfs_prefix,
                                   const char *adapter,
-- 
2.25.1