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