]> git.ipfire.org Git - thirdparty/mdadm.git/commitdiff
imsm: add devpath_to_char method
authorMariusz Tkaczyk <mariusz.tkaczyk@linux.intel.com>
Mon, 17 May 2021 14:39:01 +0000 (16:39 +0200)
committerJes Sorensen <jsorensen@fb.com>
Wed, 26 May 2021 11:26:30 +0000 (07:26 -0400)
Add method for reading sysfs attributes and propagate it across IMSM code.

Signed-off-by: Mariusz Tkaczyk <mariusz.tkaczyk@linux.intel.com>
Signed-off-by: Jes Sorensen <jsorensen@fb.com>
platform-intel.c
platform-intel.h
super-intel.c

index 2ed63ed3fd34bd45d826579fb38028ef8d3c6bd0..94017841fa5398f14c5c256d0ba5926796d99c16 100644 (file)
@@ -239,6 +239,29 @@ __u16 devpath_to_vendor(const char *dev_path)
        return id;
 }
 
+/* Description: Read text value of dev_path/entry field
+ * Parameters:
+ *     dev_path - sysfs path to the device
+ *     entry - entry to be read
+ *     buf - buffer for read value
+ *     len - size of buf
+ *     verbose - error logging level
+ */
+int devpath_to_char(const char *dev_path, const char *entry, char *buf, int len,
+                   int verbose)
+{
+       char path[PATH_MAX];
+
+       snprintf(path, sizeof(path), "%s/%s", dev_path, entry);
+       if (load_sys(path, buf, len)) {
+               if (verbose)
+                       pr_err("Cannot read %s, aborting\n", path);
+               return 1;
+       }
+
+       return 0;
+}
+
 struct sys_dev *find_intel_devices(void)
 {
        struct sys_dev *ahci, *isci, *nvme;
index f93add51f0856cb28ddf37f39c2b4d42da7d2bab..45d98cddf56a46355e4a0d1ddeb902ebd7cc1023 100644 (file)
@@ -238,6 +238,8 @@ static inline char *guid_str(char *buf, struct efi_guid guid)
 
 char *get_nvme_multipath_dev_hw_path(const char *dev_path);
 char *diskfd_to_devpath(int fd, int dev_level, char *buf);
+int devpath_to_char(const char *dev_path, const char *entry, char *buf,
+                   int len, int verbose);
 __u16 devpath_to_vendor(const char *dev_path);
 struct sys_dev *find_driver_devices(const char *bus, const char *driver);
 struct sys_dev *find_intel_devices(void);
index cff8550bb4d16bc1b145fe3cf2c99f53d3e5aed4..c352f50cf7c6bd02e0e9b91eb19d45d8078e87a8 100644 (file)
@@ -2246,7 +2246,7 @@ static int ahci_enumerate_ports(const char *hba_path, int port_count, int host_b
                char vendor[64];
                char buf[1024];
                int major, minor;
-               char *device;
+               char device[PATH_MAX];
                char *c;
                int port;
                int type;
@@ -2262,20 +2262,15 @@ static int ahci_enumerate_ports(const char *hba_path, int port_count, int host_b
                        continue;
                }
 
-               /* retrieve the scsi device type */
-               if (asprintf(&device, "/sys/dev/block/%d:%d/device/xxxxxxx", major, minor) < 0) {
+               /* retrieve the scsi device */
+               if (!devt_to_devpath(makedev(major, minor), 1, device)) {
                        if (verbose > 0)
-                               pr_err("failed to allocate 'device'\n");
+                               pr_err("failed to get device\n");
                        err = 2;
                        break;
                }
-               sprintf(device, "/sys/dev/block/%d:%d/device/type", major, minor);
-               if (load_sys(device, buf, sizeof(buf)) != 0) {
-                       if (verbose > 0)
-                               pr_err("failed to read device type for %s\n",
-                                       path);
+               if (devpath_to_char(device, "type", buf, sizeof(buf), 0)) {
                        err = 2;
-                       free(device);
                        break;
                }
                type = strtoul(buf, NULL, 10);
@@ -2284,8 +2279,9 @@ static int ahci_enumerate_ports(const char *hba_path, int port_count, int host_b
                if (!(type == 0 || type == 7 || type == 14)) {
                        vendor[0] = '\0';
                        model[0] = '\0';
-                       sprintf(device, "/sys/dev/block/%d:%d/device/vendor", major, minor);
-                       if (load_sys(device, buf, sizeof(buf)) == 0) {
+
+                       if (devpath_to_char(device, "vendor", buf,
+                                           sizeof(buf), 0) == 0) {
                                strncpy(vendor, buf, sizeof(vendor));
                                vendor[sizeof(vendor) - 1] = '\0';
                                c = (char *) &vendor[sizeof(vendor) - 1];
@@ -2293,8 +2289,9 @@ static int ahci_enumerate_ports(const char *hba_path, int port_count, int host_b
                                        *c-- = '\0';
 
                        }
-                       sprintf(device, "/sys/dev/block/%d:%d/device/model", major, minor);
-                       if (load_sys(device, buf, sizeof(buf)) == 0) {
+
+                       if (devpath_to_char(device, "model", buf,
+                                           sizeof(buf), 0) == 0) {
                                strncpy(model, buf, sizeof(model));
                                model[sizeof(model) - 1] = '\0';
                                c = (char *) &model[sizeof(model) - 1];
@@ -2319,7 +2316,6 @@ static int ahci_enumerate_ports(const char *hba_path, int port_count, int host_b
                                }
                } else
                        buf[0] = '\0';
-               free(device);
 
                /* chop device path to 'host%d' and calculate the port number */
                c = strchr(&path[hba_len], '/');
@@ -4026,7 +4022,7 @@ static void fd2devname(int fd, char *name)
 
 static int nvme_get_serial(int fd, void *buf, size_t buf_len)
 {
-       char path[60];
+       char path[PATH_MAX];
        char *name = fd2kname(fd);
 
        if (!name)
@@ -4035,9 +4031,10 @@ static int nvme_get_serial(int fd, void *buf, size_t buf_len)
        if (strncmp(name, "nvme", 4) != 0)
                return 1;
 
-       snprintf(path, sizeof(path) - 1, "/sys/block/%s/device/serial", name);
+       if (!diskfd_to_devpath(fd, 1, path))
+               return 1;
 
-       return load_sys(path, buf, buf_len);
+       return devpath_to_char(path, "serial", buf, buf_len, 0);
 }
 
 extern int scsi_get_serial(int fd, void *buf, size_t buf_len);