]> git.ipfire.org Git - thirdparty/mdadm.git/blobdiff - platform-intel.c
Provide a mdstat_ent to subarray helper
[thirdparty/mdadm.git] / platform-intel.c
index 6cd5830aa446802e0f996d9c93e0a62efc5c810c..61749085307b4b826ceee3528f2786448107a681 100644 (file)
@@ -44,7 +44,7 @@ void free_sys_dev(struct sys_dev **list)
 struct sys_dev *find_driver_devices(const char *bus, const char *driver)
 {
        /* search sysfs for devices driven by 'driver' */
-       char path[256];
+       char path[292];
        char link[256];
        char *c;
        DIR *driver_dir;
@@ -57,13 +57,17 @@ struct sys_dev *find_driver_devices(const char *bus, const char *driver)
        if (!driver_dir)
                return NULL;
        for (de = readdir(driver_dir); de; de = readdir(driver_dir)) {
+               int n;
+
                /* is 'de' a device? check that the 'subsystem' link exists and
                 * that its target matches 'bus'
                 */
                sprintf(path, "/sys/bus/%s/drivers/%s/%s/subsystem",
                        bus, driver, de->d_name);
-               if (readlink(path, link, sizeof(link)) < 0)
+               n = readlink(path, link, sizeof(link));
+               if (n < 0 || n >= (int)sizeof(link))
                        continue;
+               link[n] = '\0';
                c = strrchr(link, '/');
                if (!c)
                        continue;
@@ -90,7 +94,7 @@ struct sys_dev *find_driver_devices(const char *bus, const char *driver)
                list->path = canonicalize_file_name(path);
                list->next = NULL;
        }
-
+       closedir(driver_dir);
        return head;
 }
 
@@ -157,16 +161,39 @@ static int scan(const void *start, const void *end)
 const struct imsm_orom *find_imsm_orom(void)
 {
        static int populated = 0;
+       unsigned long align;
 
        /* it's static data so we only need to read it once */
        if (populated)
                return &imsm_orom;
 
+       if (check_env("IMSM_TEST_OROM")) {
+               memset(&imsm_orom, 0, sizeof(imsm_orom));
+               imsm_orom.rlc = IMSM_OROM_RLC_RAID0 | IMSM_OROM_RLC_RAID1 |
+                               IMSM_OROM_RLC_RAID10 | IMSM_OROM_RLC_RAID5;
+               imsm_orom.sss = IMSM_OROM_SSS_4kB | IMSM_OROM_SSS_8kB |
+                               IMSM_OROM_SSS_16kB | IMSM_OROM_SSS_32kB |
+                               IMSM_OROM_SSS_64kB | IMSM_OROM_SSS_128kB |
+                               IMSM_OROM_SSS_256kB | IMSM_OROM_SSS_512kB |
+                               IMSM_OROM_SSS_1MB | IMSM_OROM_SSS_2MB;
+               imsm_orom.dpa = 6;
+               imsm_orom.tds = 6;
+               imsm_orom.vpa = 2;
+               imsm_orom.vphba = 4;
+               imsm_orom.attr = imsm_orom.rlc | IMSM_OROM_ATTR_ChecksumVerify;
+               populated = 1;
+               return &imsm_orom;
+       }
+
        if (!platform_has_intel_ahci())
                return NULL;
 
        /* scan option-rom memory looking for an imsm signature */
-       if (probe_roms_init() != 0)
+       if (check_env("IMSM_SAFE_OROM_SCAN"))
+               align = 2048;
+       else
+               align = 512;
+       if (probe_roms_init(align) != 0)
                return NULL;
        probe_roms();
        populated = scan_adapter_roms(scan);
@@ -176,3 +203,64 @@ const struct imsm_orom *find_imsm_orom(void)
                return &imsm_orom;
        return NULL;
 }
+
+char *devt_to_devpath(dev_t dev)
+{
+       char device[46];
+
+       sprintf(device, "/sys/dev/block/%d:%d/device", major(dev), minor(dev));
+       return canonicalize_file_name(device);
+}
+
+static char *diskfd_to_devpath(int fd)
+{
+       /* return the device path for a disk, return NULL on error or fd
+        * refers to a partition
+        */
+       struct stat st;
+
+       if (fstat(fd, &st) != 0)
+               return NULL;
+       if (!S_ISBLK(st.st_mode))
+               return NULL;
+
+       return devt_to_devpath(st.st_rdev);
+}
+
+int path_attached_to_hba(const char *disk_path, const char *hba_path)
+{
+       int rc;
+
+       if (!disk_path || !hba_path)
+               return 0;
+
+       if (strncmp(disk_path, hba_path, strlen(hba_path)) == 0)
+               rc = 1;
+       else
+               rc = 0;
+
+       return rc;
+}
+
+int devt_attached_to_hba(dev_t dev, const char *hba_path)
+{
+       char *disk_path = devt_to_devpath(dev);
+       int rc = path_attached_to_hba(disk_path, hba_path);
+
+       if (disk_path)
+               free(disk_path);
+
+       return rc;
+}
+
+int disk_attached_to_hba(int fd, const char *hba_path)
+{
+       char *disk_path = diskfd_to_devpath(fd);
+       int rc = path_attached_to_hba(disk_path, hba_path);
+
+       if (disk_path)
+               free(disk_path);
+
+       return rc;
+}
+