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

a41c76
From f66beef45382be2aed6d021a409e90f8114c8671 Mon Sep 17 00:00:00 2001
a41c76
Message-Id: <f66beef45382be2aed6d021a409e90f8114c8671@dist-git>
073345
From: Michal Privoznik <mprivozn@redhat.com>
a41c76
Date: Fri, 6 Mar 2020 15:52:21 +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
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-2-abologna@redhat.com>
073345
Reviewed-by: Jiri Denemark <jdenemar@redhat.com>
073345
---
a41c76
 src/util/virscsi.c             | 146 ++++++++++++++++++++++++++++++---
073345
 tests/virscsidata/0-0-0-0/type |   1 +
073345
 tests/virscsidata/1-0-0-0/type |   1 +
a41c76
 3 files changed, 137 insertions(+), 11 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
a41c76
index 06659c45c7..c40857977f 100644
073345
--- a/src/util/virscsi.c
073345
+++ b/src/util/virscsi.c
a41c76
@@ -50,6 +50,32 @@ struct _virUsedByInfo {
a41c76
 typedef struct _virUsedByInfo virUsedByInfo;
a41c76
 typedef 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;
a41c76
@@ -134,6 +160,84 @@ 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;
a41c76
+    g_autofree char *path = NULL;
073345
+    char *name = NULL;
073345
+
a41c76
+    path = g_strdup_printf("%s/%d:%u:%u:%llu/block",
a41c76
+                           prefix, adapter, bus, target, unit);
073345
+
073345
+    if (virDirOpen(&dir, path) < 0)
073345
+        goto cleanup;
073345
+
073345
+    while (virDirRead(dir, &entry, path) > 0) {
a41c76
+        name = g_strdup(entry->d_name);
073345
+        break;
073345
+    }
073345
+
073345
+ cleanup:
073345
+    VIR_DIR_CLOSE(dir);
a41c76
+
073345
+    return name;
073345
+}
073345
+
073345
+
073345
 /* Returns device name (e.g. "sdc") on success, or NULL
073345
  * on failure.
073345
  */
a41c76
@@ -144,32 +248,52 @@ virSCSIDeviceGetDevName(const char *sysfs_prefix,
073345
                         unsigned int target,
073345
                         unsigned long long unit)
073345
 {
073345
-    DIR *dir = NULL;
073345
-    struct dirent *entry;
a41c76
-    g_autofree 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
 
a41c76
-    path = g_strdup_printf("%s/%d:%u:%u:%llu/block", prefix, adapter_id, bus,
a41c76
-                           target, unit);
073345
+    if (virSCSIDeviceGetType(prefix, adapter_id,
073345
+                             bus, target, unit, &type) < 0)
a41c76
+        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) {
a41c76
-        name = g_strdup(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
     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