Blame SOURCES/libvirt-RHEL-virscsi-Check-device-type-before-getting-it-s-dev-node-name.patch

073345
From 521a2285cfee3d2fdd59cb7a3270e9ef91bcc14f Mon Sep 17 00:00:00 2001
073345
Message-Id: <521a2285cfee3d2fdd59cb7a3270e9ef91bcc14f@dist-git>
073345
From: Michal Privoznik <mprivozn@redhat.com>
073345
Date: Fri, 6 Mar 2020 15:51:44 +0100
073345
Subject: [PATCH] RHEL: virscsi: Check device type before getting it's /dev
073345
 node name
073345
073345
Not all SCSI devices are block devices, therefore
073345
/sys/bus/scsi/devices/X:X:X:X/block/ directory does not always
073345
exist. Check if the SCSI device is a block device beforehand.
073345
073345
https://bugzilla.redhat.com/show_bug.cgi?id=1808388
073345
073345
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
073345
Signed-off-by: Andrea Bolognani <abologna@redhat.com>
073345
Message-Id: <20200306145149.1610286-2-abologna@redhat.com>
073345
Reviewed-by: Jiri Denemark <jdenemar@redhat.com>
073345
---
073345
 src/util/virscsi.c             | 149 ++++++++++++++++++++++++++++++---
073345
 tests/virscsidata/0-0-0-0/type |   1 +
073345
 tests/virscsidata/1-0-0-0/type |   1 +
073345
 3 files changed, 138 insertions(+), 13 deletions(-)
073345
 create mode 100644 tests/virscsidata/0-0-0-0/type
073345
 create mode 100644 tests/virscsidata/1-0-0-0/type
073345
073345
diff --git a/src/util/virscsi.c b/src/util/virscsi.c
073345
index b51103a86d..af908107d9 100644
073345
--- a/src/util/virscsi.c
073345
+++ b/src/util/virscsi.c
073345
@@ -56,6 +56,32 @@ struct _virUsedByInfo {
073345
 };
073345
 typedef struct _virUsedByInfo *virUsedByInfoPtr;
073345
 
073345
+
073345
+/* Keep in sync with scsi/scsi_proto.h */
073345
+typedef enum {
073345
+    VIR_SCSI_DEVICE_TYPE_NONE = -1,
073345
+    VIR_SCSI_DEVICE_TYPE_DISK = 0x00,
073345
+    VIR_SCSI_DEVICE_TYPE_TAPE = 0x01,
073345
+    VIR_SCSI_DEVICE_TYPE_PRINTER = 0x02,
073345
+    VIR_SCSI_DEVICE_TYPE_PROCESSOR = 0x03,
073345
+    VIR_SCSI_DEVICE_TYPE_WORM = 0x04,
073345
+    VIR_SCSI_DEVICE_TYPE_ROM = 0x05,
073345
+    VIR_SCSI_DEVICE_TYPE_SCANNER = 0x06,
073345
+    VIR_SCSI_DEVICE_TYPE_MOD = 0x07,
073345
+    VIR_SCSI_DEVICE_TYPE_MEDIUM_CHANGER = 0x08,
073345
+    VIR_SCSI_DEVICE_TYPE_COMM = 0x09,
073345
+    VIR_SCSI_DEVICE_TYPE_RAID = 0x0c,
073345
+    VIR_SCSI_DEVICE_TYPE_ENCLOSURE = 0x0d,
073345
+    VIR_SCSI_DEVICE_TYPE_RBC = 0x0e,
073345
+    VIR_SCSI_DEVICE_TYPE_OSD = 0x11,
073345
+    VIR_SCSI_DEVICE_TYPE_ZBC = 0x14,
073345
+    VIR_SCSI_DEVICE_TYPE_WLUN = 0x1e,
073345
+    VIR_SCSI_DEVICE_TYPE_NO_LUN = 0x7f,
073345
+
073345
+    VIR_SCSI_DEVICE_TYPE_LAST,
073345
+} virSCSIDeviceType;
073345
+
073345
+
073345
 struct _virSCSIDevice {
073345
     unsigned int adapter;
073345
     unsigned int bus;
073345
@@ -143,6 +169,86 @@ virSCSIDeviceGetSgName(const char *sysfs_prefix,
073345
     return sg;
073345
 }
073345
 
073345
+
073345
+static int
073345
+virSCSIDeviceGetType(const char *prefix,
073345
+                     unsigned int adapter,
073345
+                     unsigned int bus,
073345
+                     unsigned int target,
073345
+                     unsigned long long unit,
073345
+                     virSCSIDeviceType *type)
073345
+{
073345
+    int intType;
073345
+
073345
+    if (virFileReadValueInt(&intType,
073345
+                            "%s/%d:%u:%u:%llu/type",
073345
+                            prefix, adapter, bus, target, unit) < 0)
073345
+        return -1;
073345
+
073345
+    switch (intType) {
073345
+    case VIR_SCSI_DEVICE_TYPE_DISK:
073345
+    case VIR_SCSI_DEVICE_TYPE_TAPE:
073345
+    case VIR_SCSI_DEVICE_TYPE_PRINTER:
073345
+    case VIR_SCSI_DEVICE_TYPE_PROCESSOR:
073345
+    case VIR_SCSI_DEVICE_TYPE_WORM:
073345
+    case VIR_SCSI_DEVICE_TYPE_ROM:
073345
+    case VIR_SCSI_DEVICE_TYPE_SCANNER:
073345
+    case VIR_SCSI_DEVICE_TYPE_MOD:
073345
+    case VIR_SCSI_DEVICE_TYPE_MEDIUM_CHANGER:
073345
+    case VIR_SCSI_DEVICE_TYPE_COMM:
073345
+    case VIR_SCSI_DEVICE_TYPE_RAID:
073345
+    case VIR_SCSI_DEVICE_TYPE_ENCLOSURE:
073345
+    case VIR_SCSI_DEVICE_TYPE_RBC:
073345
+    case VIR_SCSI_DEVICE_TYPE_OSD:
073345
+    case VIR_SCSI_DEVICE_TYPE_ZBC:
073345
+    case VIR_SCSI_DEVICE_TYPE_WLUN:
073345
+    case VIR_SCSI_DEVICE_TYPE_NO_LUN:
073345
+        *type = intType;
073345
+        break;
073345
+
073345
+    default:
073345
+        virReportError(VIR_ERR_INTERNAL_ERROR,
073345
+                       _("unknown SCSI device type: %x"),
073345
+                       intType);
073345
+        return -1;
073345
+    }
073345
+
073345
+    return 0;
073345
+}
073345
+
073345
+
073345
+static char *
073345
+virSCSIDeviceGetDevNameBlock(const char *prefix,
073345
+                             unsigned int adapter,
073345
+                             unsigned int bus,
073345
+                             unsigned int target,
073345
+                             unsigned long long unit)
073345
+{
073345
+    DIR *dir = NULL;
073345
+    struct dirent *entry;
073345
+    char *path = NULL;
073345
+    char *name = NULL;
073345
+
073345
+    if (virAsprintf(&path,
073345
+                    "%s/%d:%u:%u:%llu/block",
073345
+                    prefix, adapter, bus, target, unit) < 0)
073345
+        return NULL;
073345
+
073345
+    if (virDirOpen(&dir, path) < 0)
073345
+        goto cleanup;
073345
+
073345
+    while (virDirRead(dir, &entry, path) > 0) {
073345
+        ignore_value(VIR_STRDUP(name, entry->d_name));
073345
+        break;
073345
+    }
073345
+
073345
+ cleanup:
073345
+    VIR_DIR_CLOSE(dir);
073345
+    VIR_FREE(path);
073345
+    return name;
073345
+}
073345
+
073345
+
073345
 /* Returns device name (e.g. "sdc") on success, or NULL
073345
  * on failure.
073345
  */
073345
@@ -153,35 +259,52 @@ virSCSIDeviceGetDevName(const char *sysfs_prefix,
073345
                         unsigned int target,
073345
                         unsigned long long unit)
073345
 {
073345
-    DIR *dir = NULL;
073345
-    struct dirent *entry;
073345
-    char *path = NULL;
073345
     char *name = NULL;
073345
     unsigned int adapter_id;
073345
+    virSCSIDeviceType type;
073345
     const char *prefix = sysfs_prefix ? sysfs_prefix : SYSFS_SCSI_DEVICES;
073345
 
073345
     if (virSCSIDeviceGetAdapterId(adapter, &adapter_id) < 0)
073345
         return NULL;
073345
 
073345
-    if (virAsprintf(&path,
073345
-                    "%s/%d:%u:%u:%llu/block",
073345
-                    prefix, adapter_id, bus, target, unit) < 0)
073345
+    if (virSCSIDeviceGetType(prefix, adapter_id,
073345
+                             bus, target, unit, &type) < 0)
073345
         return NULL;
073345
 
073345
-    if (virDirOpen(&dir, path) < 0)
073345
-        goto cleanup;
073345
+    switch (type) {
073345
+    case VIR_SCSI_DEVICE_TYPE_DISK:
073345
+        name = virSCSIDeviceGetDevNameBlock(prefix, adapter_id, bus, target, unit);
073345
+        break;
073345
 
073345
-    while (virDirRead(dir, &entry, path) > 0) {
073345
-        ignore_value(VIR_STRDUP(name, entry->d_name));
073345
+    case VIR_SCSI_DEVICE_TYPE_TAPE:
073345
+    case VIR_SCSI_DEVICE_TYPE_PRINTER:
073345
+    case VIR_SCSI_DEVICE_TYPE_PROCESSOR:
073345
+    case VIR_SCSI_DEVICE_TYPE_WORM:
073345
+    case VIR_SCSI_DEVICE_TYPE_ROM:
073345
+    case VIR_SCSI_DEVICE_TYPE_SCANNER:
073345
+    case VIR_SCSI_DEVICE_TYPE_MOD:
073345
+    case VIR_SCSI_DEVICE_TYPE_MEDIUM_CHANGER:
073345
+    case VIR_SCSI_DEVICE_TYPE_COMM:
073345
+    case VIR_SCSI_DEVICE_TYPE_RAID:
073345
+    case VIR_SCSI_DEVICE_TYPE_ENCLOSURE:
073345
+    case VIR_SCSI_DEVICE_TYPE_RBC:
073345
+    case VIR_SCSI_DEVICE_TYPE_OSD:
073345
+    case VIR_SCSI_DEVICE_TYPE_ZBC:
073345
+    case VIR_SCSI_DEVICE_TYPE_WLUN:
073345
+    case VIR_SCSI_DEVICE_TYPE_NO_LUN:
073345
+    case VIR_SCSI_DEVICE_TYPE_NONE:
073345
+    case VIR_SCSI_DEVICE_TYPE_LAST:
073345
+    default:
073345
+        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
073345
+                       _("unsupported SCSI device type: %x"),
073345
+                       type);
073345
         break;
073345
     }
073345
 
073345
- cleanup:
073345
-    VIR_DIR_CLOSE(dir);
073345
-    VIR_FREE(path);
073345
     return name;
073345
 }
073345
 
073345
+
073345
 virSCSIDevicePtr
073345
 virSCSIDeviceNew(const char *sysfs_prefix,
073345
                  const char *adapter,
073345
diff --git a/tests/virscsidata/0-0-0-0/type b/tests/virscsidata/0-0-0-0/type
073345
new file mode 100644
073345
index 0000000000..573541ac97
073345
--- /dev/null
073345
+++ b/tests/virscsidata/0-0-0-0/type
073345
@@ -0,0 +1 @@
073345
+0
073345
diff --git a/tests/virscsidata/1-0-0-0/type b/tests/virscsidata/1-0-0-0/type
073345
new file mode 100644
073345
index 0000000000..573541ac97
073345
--- /dev/null
073345
+++ b/tests/virscsidata/1-0-0-0/type
073345
@@ -0,0 +1 @@
073345
+0
073345
-- 
073345
2.25.1
073345