render / rpms / libvirt

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