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

1b87cf
From 9c5daeb8c99ca12a66387de448f585742887fd75 Mon Sep 17 00:00:00 2001
1b87cf
Message-Id: <9c5daeb8c99ca12a66387de448f585742887fd75@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
---
1b87cf
 src/util/virscsi.c             | 140 ++++++++++++++++++++++++++++++---
073345
 tests/virscsidata/0-0-0-0/type |   1 +
073345
 tests/virscsidata/1-0-0-0/type |   1 +
1b87cf
 3 files changed, 131 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
1b87cf
index 6a90d9002f..770f727cac 100644
073345
--- a/src/util/virscsi.c
073345
+++ b/src/util/virscsi.c
1b87cf
@@ -47,6 +47,32 @@ struct _virUsedByInfo {
1b87cf
 };
a41c76
 typedef struct _virUsedByInfo virUsedByInfo;
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;
1b87cf
@@ -126,6 +152,78 @@ virSCSIDeviceGetSgName(const char *sysfs_prefix,
1b87cf
     return NULL;
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
+{
1b87cf
+    g_autoptr(DIR) dir = NULL;
073345
+    struct dirent *entry;
a41c76
+    g_autofree char *path = 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)
1b87cf
+        return NULL;
073345
+
1b87cf
+    if (virDirRead(dir, &entry, path) > 0)
1b87cf
+        return g_strdup(entry->d_name);
a41c76
+
1b87cf
+    return NULL;
073345
+}
073345
+
073345
+
073345
 /* Returns device name (e.g. "sdc") on success, or NULL
073345
  * on failure.
073345
  */
1b87cf
@@ -136,25 +234,45 @@ virSCSIDeviceGetDevName(const char *sysfs_prefix,
073345
                         unsigned int target,
073345
                         unsigned long long unit)
073345
 {
1b87cf
-    g_autoptr(DIR) dir = NULL;
073345
-    struct dirent *entry;
a41c76
-    g_autofree char *path = 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);
1b87cf
-
1b87cf
-    if (virDirOpen(&dir, path) < 0)
073345
+    if (virSCSIDeviceGetType(prefix, adapter_id,
073345
+                             bus, target, unit, &type) < 0)
1b87cf
         return NULL;
073345
 
1b87cf
-    if (virDirRead(dir, &entry, path) > 0)
1b87cf
-        return g_strdup(entry->d_name);
1b87cf
-
1b87cf
-    return NULL;
073345
+    switch (type) {
073345
+    case VIR_SCSI_DEVICE_TYPE_DISK:
1b87cf
+        return virSCSIDeviceGetDevNameBlock(prefix, adapter_id, bus, target, unit);
1b87cf
+
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);
1b87cf
+        return NULL;
1b87cf
+    }
073345
 }
073345
 
1b87cf
 virSCSIDevice *
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
-- 
1b87cf
2.34.1
073345