d759b5
From 1f219561662a850309e4c19418167fc1d33a8cf7 Mon Sep 17 00:00:00 2001
d759b5
Message-Id: <1f219561662a850309e4c19418167fc1d33a8cf7@dist-git>
d759b5
From: Michal Privoznik <mprivozn@redhat.com>
d759b5
Date: Tue, 8 Oct 2019 14:14:21 +0200
d759b5
Subject: [PATCH] RHEL: virscsi: Introduce and use
d759b5
 virSCSIDeviceGetUnprivSGIOSysfsPath()
d759b5
d759b5
When constructing a path to the 'unpriv_sgio' file of given SCSI
d759b5
device we don't need to go through /dev/* and major() + minor()
d759b5
path. The generated path points to
d759b5
/sys/dev/block/MAJ:MIN/queue/unpriv_sgio which is wrong if the
d759b5
SCSI device in question is not a block device. We can generate a
d759b5
different path: /sys/bus/scsi/devices/X:X:X:X/unpriv_sgio where
d759b5
the file is directly accessible regardless of the SCSI device
d759b5
type.
d759b5
d759b5
https://bugzilla.redhat.com/show_bug.cgi?id=1754241
d759b5
d759b5
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
d759b5
Message-Id: <36bac569fd66de8dcbec73085016b16a0371dfe6.1570536610.git.mprivozn@redhat.com>
d759b5
Reviewed-by: Jiri Denemark <jdenemar@redhat.com>
d759b5
---
d759b5
 src/libvirt_private.syms |  1 +
d759b5
 src/qemu/qemu_conf.c     | 19 +++++++++++--------
d759b5
 src/util/virscsi.c       | 21 +++++++++++++++++++++
d759b5
 src/util/virscsi.h       |  5 +++++
d759b5
 4 files changed, 38 insertions(+), 8 deletions(-)
d759b5
d759b5
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
d759b5
index b213e5764b..1bc43293fd 100644
d759b5
--- a/src/libvirt_private.syms
d759b5
+++ b/src/libvirt_private.syms
d759b5
@@ -2704,6 +2704,7 @@ virSCSIDeviceGetSgName;
d759b5
 virSCSIDeviceGetShareable;
d759b5
 virSCSIDeviceGetTarget;
d759b5
 virSCSIDeviceGetUnit;
d759b5
+virSCSIDeviceGetUnprivSGIOSysfsPath;
d759b5
 virSCSIDeviceIsAvailable;
d759b5
 virSCSIDeviceListAdd;
d759b5
 virSCSIDeviceListCount;
d759b5
diff --git a/src/qemu/qemu_conf.c b/src/qemu/qemu_conf.c
d759b5
index a81298326f..5636277888 100644
d759b5
--- a/src/qemu/qemu_conf.c
d759b5
+++ b/src/qemu/qemu_conf.c
d759b5
@@ -1648,7 +1648,6 @@ qemuSetUnprivSGIO(virDomainDeviceDefPtr dev)
d759b5
     virDomainDiskDefPtr disk = NULL;
d759b5
     virDomainHostdevDefPtr hostdev = NULL;
d759b5
     char *sysfs_path = NULL;
d759b5
-    char *hostdev_path = NULL;
d759b5
     const char *path = NULL;
d759b5
     int val = 0;
d759b5
     int ret = -1;
d759b5
@@ -1664,24 +1663,29 @@ qemuSetUnprivSGIO(virDomainDeviceDefPtr dev)
d759b5
             return 0;
d759b5
 
d759b5
         path = virDomainDiskGetSource(disk);
d759b5
+
d759b5
+        if (!(sysfs_path = virGetUnprivSGIOSysfsPath(path, NULL)))
d759b5
+            goto cleanup;
d759b5
+
d759b5
     } else if (dev->type == VIR_DOMAIN_DEVICE_HOSTDEV) {
d759b5
         hostdev = dev->data.hostdev;
d759b5
+        virDomainHostdevSubsysSCSIPtr scsisrc = &hostdev->source.subsys.u.scsi;
d759b5
+        virDomainHostdevSubsysSCSIHostPtr scsihostsrc = &scsisrc->u.host;
d759b5
 
d759b5
         if (hostdev->source.subsys.u.scsi.protocol ==
d759b5
             VIR_DOMAIN_HOSTDEV_SCSI_PROTOCOL_TYPE_ISCSI)
d759b5
             return 0;
d759b5
 
d759b5
-        if (!(hostdev_path = qemuGetHostdevPath(hostdev)))
d759b5
+        if (!(sysfs_path = virSCSIDeviceGetUnprivSGIOSysfsPath(NULL,
d759b5
+                                                               scsihostsrc->adapter,
d759b5
+                                                               scsihostsrc->bus,
d759b5
+                                                               scsihostsrc->target,
d759b5
+                                                               scsihostsrc->unit)))
d759b5
             goto cleanup;
d759b5
-
d759b5
-        path = hostdev_path;
d759b5
     } else {
d759b5
         return 0;
d759b5
     }
d759b5
 
d759b5
-    if (!(sysfs_path = virGetUnprivSGIOSysfsPath(path, NULL)))
d759b5
-        goto cleanup;
d759b5
-
d759b5
     /* By default, filter the SG_IO commands, i.e. set unpriv_sgio to 0.  */
d759b5
     if (dev->type == VIR_DOMAIN_DEVICE_DISK) {
d759b5
         if (disk->sgio == VIR_DOMAIN_DEVICE_SGIO_UNFILTERED)
d759b5
@@ -1705,7 +1709,6 @@ qemuSetUnprivSGIO(virDomainDeviceDefPtr dev)
d759b5
     ret = 0;
d759b5
 
d759b5
  cleanup:
d759b5
-    VIR_FREE(hostdev_path);
d759b5
     VIR_FREE(sysfs_path);
d759b5
     return ret;
d759b5
 }
d759b5
diff --git a/src/util/virscsi.c b/src/util/virscsi.c
d759b5
index 6c3fd8a562..5aab43fc88 100644
d759b5
--- a/src/util/virscsi.c
d759b5
+++ b/src/util/virscsi.c
d759b5
@@ -342,6 +342,27 @@ virSCSIDeviceGetDevName(const char *sysfs_prefix,
d759b5
 }
d759b5
 
d759b5
 
d759b5
+char *
d759b5
+virSCSIDeviceGetUnprivSGIOSysfsPath(const char *sysfs_prefix,
d759b5
+                                    const char *adapter,
d759b5
+                                    unsigned int bus,
d759b5
+                                    unsigned int target,
d759b5
+                                    unsigned long long unit)
d759b5
+{
d759b5
+    char *path = NULL;
d759b5
+    unsigned int adapter_id;
d759b5
+    const char *prefix = sysfs_prefix ? sysfs_prefix : SYSFS_SCSI_DEVICES;
d759b5
+
d759b5
+    if (virSCSIDeviceGetAdapterId(adapter, &adapter_id) < 0)
d759b5
+        return NULL;
d759b5
+
d759b5
+    ignore_value(virAsprintf(&path,
d759b5
+                             "%s/%d:%u:%u:%llu/unpriv_sgio",
d759b5
+                             prefix, adapter_id, bus, target, unit));
d759b5
+    return path;
d759b5
+}
d759b5
+
d759b5
+
d759b5
 virSCSIDevicePtr
d759b5
 virSCSIDeviceNew(const char *sysfs_prefix,
d759b5
                  const char *adapter,
d759b5
diff --git a/src/util/virscsi.h b/src/util/virscsi.h
d759b5
index 9f8b3ecf1e..5dea2a9f5d 100644
d759b5
--- a/src/util/virscsi.h
d759b5
+++ b/src/util/virscsi.h
d759b5
@@ -43,6 +43,11 @@ char *virSCSIDeviceGetDevName(const char *sysfs_prefix,
d759b5
                               unsigned int bus,
d759b5
                               unsigned int target,
d759b5
                               unsigned long long unit);
d759b5
+char *virSCSIDeviceGetUnprivSGIOSysfsPath(const char *sysfs_prefix,
d759b5
+                                          const char *adapter,
d759b5
+                                          unsigned int bus,
d759b5
+                                          unsigned int target,
d759b5
+                                          unsigned long long unit);
d759b5
 
d759b5
 virSCSIDevicePtr virSCSIDeviceNew(const char *sysfs_prefix,
d759b5
                                   const char *adapter,
d759b5
-- 
d759b5
2.23.0
d759b5