]> git.ipfire.org Git - thirdparty/mdadm.git/blobdiff - super-intel.c
IMSM: Enable spanning between VMD domains
[thirdparty/mdadm.git] / super-intel.c
index 90b7b6dee5d0ab4c2b01239667eaac44c61c237d..21e853224ee07f7a9c08cd5480b7ac344aee6daf 100644 (file)
@@ -537,7 +537,8 @@ static struct intel_hba * alloc_intel_hba(struct sys_dev *device)
 
 static struct intel_hba * find_intel_hba(struct intel_hba *hba, struct sys_dev *device)
 {
-       struct intel_hba *result=NULL;
+       struct intel_hba *result;
+
        for (result = hba; result; result = result->next) {
                if (result->type == device->type && strcmp(result->path, device->path) == 0)
                        break;
@@ -566,10 +567,6 @@ static int attach_hba_to_super(struct intel_super *super, struct sys_dev *device
        if (device->type != hba->type)
                return 2;
 
-       /* Always forbid spanning between VMD domains (seen as different controllers by mdadm) */
-       if (device->type == SYS_DEV_VMD && !path_attached_to_hba(device->path, hba->path))
-               return 2;
-
        /* Multiple same type HBAs can be used if they share the same OROM */
        const struct imsm_orom *device_orom = get_orom_by_device_id(device->dev_id);
 
@@ -1222,7 +1219,7 @@ static void print_imsm_dev(struct intel_super *super,
                printf(" <-- %s", map_state_str[map->map_state]);
                printf("\n     Checkpoint : %u ",
                           __le32_to_cpu(dev->vol.curr_migr_unit));
-               if ((is_gen_migration(dev)) && ((slot > 1) || (slot < 0)))
+               if (is_gen_migration(dev) && (slot > 1 || slot < 0))
                        printf("(N/A)");
                else
                        printf("(%llu)", (unsigned long long)
@@ -1275,7 +1272,7 @@ void examine_migr_rec_imsm(struct intel_super *super)
                map = get_imsm_map(dev, MAP_0);
                if (map)
                        slot = get_imsm_disk_slot(map, super->disks->index);
-               if ((map == NULL) || (slot > 1) || (slot < 0)) {
+               if (map == NULL || slot > 1 || slot < 0) {
                        printf(" Empty\n                              ");
                        printf("Examine one of first two disks in array\n");
                        break;
@@ -1624,7 +1621,10 @@ static int ahci_enumerate_ports(const char *hba_path, int port_count, int host_b
         * this hba
         */
        dir = opendir("/sys/dev/block");
-       for (ent = dir ? readdir(dir) : NULL; ent; ent = readdir(dir)) {
+       if (!dir)
+               return 1;
+
+       for (ent = readdir(dir); ent; ent = readdir(dir)) {
                int fd;
                char model[64];
                char vendor[64];
@@ -1654,7 +1654,7 @@ static int ahci_enumerate_ports(const char *hba_path, int port_count, int host_b
                        break;
                }
                sprintf(device, "/sys/dev/block/%d:%d/device/type", major, minor);
-               if (load_sys(device, buf) != 0) {
+               if (load_sys(device, buf, sizeof(buf)) != 0) {
                        if (verbose > 0)
                                pr_err("failed to read device type for %s\n",
                                        path);
@@ -1669,7 +1669,7 @@ static int ahci_enumerate_ports(const char *hba_path, int port_count, int host_b
                        vendor[0] = '\0';
                        model[0] = '\0';
                        sprintf(device, "/sys/dev/block/%d:%d/device/vendor", major, minor);
-                       if (load_sys(device, buf) == 0) {
+                       if (load_sys(device, buf, sizeof(buf)) == 0) {
                                strncpy(vendor, buf, sizeof(vendor));
                                vendor[sizeof(vendor) - 1] = '\0';
                                c = (char *) &vendor[sizeof(vendor) - 1];
@@ -1678,7 +1678,7 @@ static int ahci_enumerate_ports(const char *hba_path, int port_count, int host_b
 
                        }
                        sprintf(device, "/sys/dev/block/%d:%d/device/model", major, minor);
-                       if (load_sys(device, buf) == 0) {
+                       if (load_sys(device, buf, sizeof(buf)) == 0) {
                                strncpy(model, buf, sizeof(model));
                                model[sizeof(model) - 1] = '\0';
                                c = (char *) &model[sizeof(model) - 1];
@@ -1781,7 +1781,10 @@ static int print_vmd_attached_devs(struct sys_dev *hba)
         * this hba
         */
        dir = opendir("/sys/bus/pci/drivers/nvme");
-       for (ent = dir ? readdir(dir) : NULL; ent; ent = readdir(dir)) {
+       if (!dir)
+               return 1;
+
+       for (ent = readdir(dir); ent; ent = readdir(dir)) {
                int n;
 
                /* is 'ent' a device? check that the 'subsystem' link exists and
@@ -1814,6 +1817,7 @@ static int print_vmd_attached_devs(struct sys_dev *hba)
                free(rp);
        }
 
+       closedir(dir);
        return 0;
 }
 
@@ -2015,16 +2019,20 @@ static int detail_platform_imsm(int verbose, int enumerate_only, char *controlle
 
        for (entry = orom_entries; entry; entry = entry->next) {
                if (entry->type == SYS_DEV_VMD) {
+                       print_imsm_capability(&entry->orom);
                        for (hba = list; hba; hba = hba->next) {
                                if (hba->type == SYS_DEV_VMD) {
                                        char buf[PATH_MAX];
-                                       print_imsm_capability(&entry->orom);
                                        printf(" I/O Controller : %s (%s)\n",
                                                vmd_domain_to_controller(hba, buf), get_sys_dev_type(hba->type));
-                                       print_vmd_attached_devs(hba);
-                                       printf("\n");
+                                       if (print_vmd_attached_devs(hba)) {
+                                               if (verbose > 0)
+                                                       pr_err("failed to get devices attached to VMD domain.\n");
+                                               result |= 2;
+                                       }
                                }
                        }
+                       printf("\n");
                        continue;
                }
 
@@ -2451,12 +2459,12 @@ static struct imsm_dev *imsm_get_device_during_migration(
 static int load_imsm_migr_rec(struct intel_super *super, struct mdinfo *info)
 {
        struct mdinfo *sd;
-       struct dl *dl = NULL;
+       struct dl *dl;
        char nm[30];
        int retval = -1;
        int fd = -1;
        struct imsm_dev *dev;
-       struct imsm_map *map = NULL;
+       struct imsm_map *map;
        int slot = -1;
 
        /* find map under migration */
@@ -2465,19 +2473,12 @@ static int load_imsm_migr_rec(struct intel_super *super, struct mdinfo *info)
        */
        if (dev == NULL)
                return -2;
-       map = get_imsm_map(dev, MAP_0);
 
        if (info) {
                for (sd = info->devs ; sd ; sd = sd->next) {
-                       /* skip spare and failed disks
-                        */
-                       if (sd->disk.raid_disk < 0)
-                               continue;
                        /* read only from one of the first two slots */
-                       if (map)
-                               slot = get_imsm_disk_slot(map,
-                                                         sd->disk.raid_disk);
-                       if ((map == NULL) || (slot > 1) || (slot < 0))
+                       if ((sd->disk.raid_disk < 0) ||
+                           (sd->disk.raid_disk > 1))
                                continue;
 
                        sprintf(nm, "%d:%d", sd->disk.major, sd->disk.minor);
@@ -2487,6 +2488,7 @@ static int load_imsm_migr_rec(struct intel_super *super, struct mdinfo *info)
                }
        }
        if (fd < 0) {
+               map = get_imsm_map(dev, MAP_0);
                for (dl = super->disks; dl; dl = dl->next) {
                        /* skip spare and failed disks
                        */
@@ -2495,7 +2497,7 @@ static int load_imsm_migr_rec(struct intel_super *super, struct mdinfo *info)
                        /* read only from one of the first two slots */
                        if (map)
                                slot = get_imsm_disk_slot(map, dl->index);
-                       if ((map == NULL) || (slot > 1) || (slot < 0))
+                       if (map == NULL || slot > 1 || slot < 0)
                                continue;
                        sprintf(nm, "%d:%d", dl->major, dl->minor);
                        fd = dev_open(nm, O_RDONLY);
@@ -2577,7 +2579,7 @@ static int write_imsm_migr_rec(struct supertype *st)
        int len;
        struct imsm_update_general_migration_checkpoint *u;
        struct imsm_dev *dev;
-       struct imsm_map *map = NULL;
+       struct imsm_map *map;
 
        /* find map under migration */
        dev = imsm_get_device_during_migration(super);
@@ -2599,7 +2601,7 @@ static int write_imsm_migr_rec(struct supertype *st)
                /* write to 2 first slots only */
                if (map)
                        slot = get_imsm_disk_slot(map, sd->index);
-               if ((map == NULL) || (slot > 1) || (slot < 0))
+               if (map == NULL || slot > 1 || slot < 0)
                        continue;
 
                sprintf(nm, "%d:%d", sd->major, sd->minor);
@@ -3049,7 +3051,7 @@ static void getinfo_super_imsm(struct supertype *st, struct mdinfo *info, char *
  * for each disk in array */
 struct mdinfo *getinfo_super_disks_imsm(struct supertype *st)
 {
-       struct mdinfo *mddev = NULL;
+       struct mdinfo *mddev;
        struct intel_super *super = st->sb;
        struct imsm_disk *disk;
        int count = 0;
@@ -3319,23 +3321,40 @@ static void fd2devname(int fd, char *name)
        }
 }
 
+static int nvme_get_serial(int fd, void *buf, size_t buf_len)
+{
+       char path[60];
+       char *name = fd2kname(fd);
+
+       if (!name)
+               return 1;
+
+       if (strncmp(name, "nvme", 4) != 0)
+               return 1;
+
+       snprintf(path, sizeof(path) - 1, "/sys/block/%s/device/serial", name);
+
+       return load_sys(path, buf, buf_len);
+}
+
 extern int scsi_get_serial(int fd, void *buf, size_t buf_len);
 
 static int imsm_read_serial(int fd, char *devname,
                            __u8 serial[MAX_RAID_SERIAL_LEN])
 {
-       unsigned char scsi_serial[255];
+       char buf[50];
        int rv;
-       int rsp_len;
        int len;
        char *dest;
        char *src;
-       char *rsp_buf;
-       int i;
+       unsigned int i;
 
-       memset(scsi_serial, 0, sizeof(scsi_serial));
+       memset(buf, 0, sizeof(buf));
 
-       rv = scsi_get_serial(fd, scsi_serial, sizeof(scsi_serial));
+       rv = nvme_get_serial(fd, buf, sizeof(buf));
+
+       if (rv)
+               rv = scsi_get_serial(fd, buf, sizeof(buf));
 
        if (rv && check_env("IMSM_DEVNAME_AS_SERIAL")) {
                memset(serial, 0, MAX_RAID_SERIAL_LEN);
@@ -3350,20 +3369,11 @@ static int imsm_read_serial(int fd, char *devname,
                return rv;
        }
 
-       rsp_len = scsi_serial[3];
-       if (!rsp_len) {
-               if (devname)
-                       pr_err("Failed to retrieve serial for %s\n",
-                              devname);
-               return 2;
-       }
-       rsp_buf = (char *) &scsi_serial[4];
-
        /* trim all whitespace and non-printable characters and convert
         * ':' to ';'
         */
-       for (i = 0, dest = rsp_buf; i < rsp_len; i++) {
-               src = &rsp_buf[i];
+       for (i = 0, dest = buf; i < sizeof(buf) && buf[i]; i++) {
+               src = &buf[i];
                if (*src > 0x20) {
                        /* ':' is reserved for use in placeholder serial
                         * numbers for missing disks
@@ -3374,8 +3384,8 @@ static int imsm_read_serial(int fd, char *devname,
                                *dest++ = *src;
                }
        }
-       len = dest - rsp_buf;
-       dest = rsp_buf;
+       len = dest - buf;
+       dest = buf;
 
        /* truncate leading characters */
        if (len > MAX_RAID_SERIAL_LEN) {
@@ -3507,8 +3517,7 @@ static void migrate(struct imsm_dev *dev, struct intel_super *super,
 
        /* duplicate and then set the target end state in map[0] */
        memcpy(dest, src, sizeof_imsm_map(src));
-       if ((migr_type == MIGR_REBUILD) ||
-           (migr_type ==  MIGR_GEN_MIGR)) {
+       if (migr_type == MIGR_REBUILD || migr_type ==  MIGR_GEN_MIGR) {
                __u32 ord;
                int i;
 
@@ -3538,8 +3547,8 @@ static void end_migration(struct imsm_dev *dev, struct intel_super *super,
         *
         * FIXME add support for raid-level-migration
         */
-       if ((map_state != map->map_state) && (is_gen_migration(dev) == 0) &&
-               (prev->map_state != IMSM_T_STATE_UNINITIALIZED)) {
+       if (map_state != map->map_state && (is_gen_migration(dev) == 0) &&
+           prev->map_state != IMSM_T_STATE_UNINITIALIZED) {
                /* when final map state is other than expected
                 * merge maps (not for migration)
                 */
@@ -3931,7 +3940,7 @@ static int find_intel_hba_capability(int fd, struct intel_super *super, char *de
        struct sys_dev *hba_name;
        int rv = 0;
 
-       if ((fd < 0) || check_env("IMSM_NO_PLATFORM")) {
+       if (fd < 0 || check_env("IMSM_NO_PLATFORM")) {
                super->orom = NULL;
                super->hba = NULL;
                return 0;
@@ -4374,7 +4383,7 @@ static int load_super_imsm_all(struct supertype *st, int fd, void **sbp,
        }
 
        /* Check migration compatibility */
-       if ((err == 0) && (check_mpb_migr_compatibility(super) != 0)) {
+       if (err == 0 && check_mpb_migr_compatibility(super) != 0) {
                pr_err("Unsupported migration detected");
                if (devname)
                        fprintf(stderr, " on %s\n", devname);
@@ -4463,7 +4472,7 @@ get_devlist_super_block(struct md_list *devlist, struct intel_super **super_list
 static int get_super_block(struct intel_super **super_list, char *devnm, char *devname,
                           int major, int minor, int keep_fd)
 {
-       struct intel_super*s = NULL;
+       struct intel_super *s;
        char nm[32];
        int dfd = -1;
        int err = 0;
@@ -4503,7 +4512,7 @@ static int get_super_block(struct intel_super **super_list, char *devnm, char *d
                if (dfd >= 0)
                        close(dfd);
        }
-       if ((dfd >= 0) && (!keep_fd))
+       if (dfd >= 0 && !keep_fd)
                close(dfd);
        return err;
 
@@ -4567,7 +4576,7 @@ static int load_super_imsm(struct supertype *st, int fd, char *devname)
         */
        rv = find_intel_hba_capability(fd, super, devname);
        /* no orom/efi or non-intel hba of the disk */
-       if ((rv != 0) && (st->ignore_hw_compat == 0)) {
+       if (rv != 0 && st->ignore_hw_compat == 0) {
                if (devname)
                        pr_err("No OROM/EFI properties for %s\n", devname);
                free_imsm(super);
@@ -4577,7 +4586,11 @@ static int load_super_imsm(struct supertype *st, int fd, char *devname)
 
        /* retry the load if we might have raced against mdmon */
        if (rv == 3) {
-               struct mdstat_ent *mdstat = mdstat_by_component(fd2devnm(fd));
+               struct mdstat_ent *mdstat = NULL;
+               char *name = fd2kname(fd);
+
+               if (name)
+                       mdstat = mdstat_by_component(name);
 
                if (mdstat && mdmon_running(mdstat->devnm) && getpid() != mdmon_pid(mdstat->devnm)) {
                        for (retry = 0; retry < 3; retry++) {
@@ -5013,8 +5026,7 @@ static int add_to_super_imsm_volume(struct supertype *st, mdu_disk_info_t *dk,
                                struct imsm_map *map2 = get_imsm_map(dev,
                                                                     MAP_1);
                                int slot2 = get_imsm_disk_slot(map2, df->index);
-                               if ((slot2 < map2->num_members) &&
-                                   (slot2 >= 0)) {
+                               if (slot2 < map2->num_members && slot2 >= 0) {
                                        __u32 ord2 = get_imsm_ord_tbl_ent(dev,
                                                                         slot2,
                                                                         MAP_1);
@@ -5121,9 +5133,53 @@ static int add_to_super_imsm(struct supertype *st, mdu_disk_info_t *dk,
        rv = imsm_read_serial(fd, devname, dd->serial);
        if (rv) {
                pr_err("failed to retrieve scsi serial, aborting\n");
+               if (dd->devname)
+                       free(dd->devname);
                free(dd);
                abort();
        }
+       if (super->hba && ((super->hba->type == SYS_DEV_NVME) ||
+          (super->hba->type == SYS_DEV_VMD))) {
+               int i;
+               char *devpath = diskfd_to_devpath(fd);
+               char controller_path[PATH_MAX];
+
+               if (!devpath) {
+                       pr_err("failed to get devpath, aborting\n");
+                       if (dd->devname)
+                               free(dd->devname);
+                       free(dd);
+                       return 1;
+               }
+
+               snprintf(controller_path, PATH_MAX-1, "%s/device", devpath);
+               free(devpath);
+
+               if (devpath_to_vendor(controller_path) == 0x8086) {
+                       /*
+                        * If Intel's NVMe drive has serial ended with
+                        * "-A","-B","-1" or "-2" it means that this is "x8"
+                        * device (double drive on single PCIe card).
+                        * User should be warned about potential data loss.
+                        */
+                       for (i = MAX_RAID_SERIAL_LEN-1; i > 0; i--) {
+                               /* Skip empty character at the end */
+                               if (dd->serial[i] == 0)
+                                       continue;
+
+                               if (((dd->serial[i] == 'A') ||
+                                  (dd->serial[i] == 'B') ||
+                                  (dd->serial[i] == '1') ||
+                                  (dd->serial[i] == '2')) &&
+                                  (dd->serial[i-1] == '-'))
+                                       pr_err("\tThe action you are about to take may put your data at risk.\n"
+                                               "\tPlease note that x8 devices may consist of two separate x4 devices "
+                                               "located on a single PCIe port.\n"
+                                               "\tRAID 0 is the only supported configuration for this type of x8 device.\n");
+                               break;
+                       }
+               }
+       }
 
        get_dev_size(fd, NULL, &size);
        /* clear migr_rec when adding disk to container */
@@ -5449,7 +5505,7 @@ static int validate_geometry_imsm_container(struct supertype *st, int level,
 {
        int fd;
        unsigned long long ldsize;
-       struct intel_super *super=NULL;
+       struct intel_super *super;
        int rv = 0;
 
        if (level != LEVEL_CONTAINER)
@@ -5642,10 +5698,10 @@ active_arrays_by_format(char *name, char* hba, struct md_list **devlist,
                        int dpa, int verbose)
 {
        struct mdstat_ent *mdstat = mdstat_read(0, 0);
-       struct mdstat_ent *memb = NULL;
+       struct mdstat_ent *memb;
        int count = 0;
        int num = 0;
-       struct md_list *dv = NULL;
+       struct md_list *dv;
        int found;
 
        for (memb = mdstat ; memb ; memb = memb->next) {
@@ -5661,18 +5717,18 @@ active_arrays_by_format(char *name, char* hba, struct md_list **devlist,
                                num = sprintf(path, "%s%s", "/dev/", dev->name);
                                if (num > 0)
                                        fd = open(path, O_RDONLY, 0);
-                               if ((num <= 0) || (fd < 0)) {
-                                       pr_vrb("Cannot open %s: %s\n",
+                               if (num <= 0 || fd < 0) {
+                                       pr_vrb("Cannot open %s: %s\n",
                                               dev->name, strerror(errno));
                                }
                                free(path);
                                dev = dev->next;
                        }
                        found = 0;
-                       if ((fd >= 0) && disk_attached_to_hba(fd, hba)) {
+                       if (fd >= 0 && disk_attached_to_hba(fd, hba)) {
                                struct mdstat_ent *vol;
                                for (vol = mdstat ; vol ; vol = vol->next) {
-                                       if ((vol->active > 0) &&
+                                       if (vol->active > 0 &&
                                            vol->metadata_version &&
                                            is_container_member(vol, memb->devnm)) {
                                                found++;
@@ -5703,7 +5759,7 @@ get_loop_devices(void)
 {
        int i;
        struct md_list *devlist = NULL;
-       struct md_list *dv = NULL;
+       struct md_list *dv;
 
        for(i = 0; i < 12; i++) {
                dv = xcalloc(1, sizeof(*dv));
@@ -5720,7 +5776,7 @@ static struct md_list*
 get_devices(const char *hba_path)
 {
        struct md_list *devlist = NULL;
-       struct md_list *dv = NULL;
+       struct md_list *dv;
        struct dirent *ent;
        DIR *dir;
        int err = 0;
@@ -5783,7 +5839,7 @@ count_volumes_list(struct md_list *devlist, char *homehost,
 {
        struct md_list *tmpdev;
        int count = 0;
-       struct supertype *st = NULL;
+       struct supertype *st;
 
        /* first walk the list of devices to find a consistent set
         * that match the criterea, if that is possible.
@@ -5792,7 +5848,7 @@ count_volumes_list(struct md_list *devlist, char *homehost,
        *found = 0;
        st = match_metadata_desc_imsm("imsm");
        if (st == NULL) {
-               pr_vrb("cannot allocate memory for imsm supertype\n");
+               pr_vrb("cannot allocate memory for imsm supertype\n");
                return 0;
        }
 
@@ -5805,7 +5861,7 @@ count_volumes_list(struct md_list *devlist, char *homehost,
                        continue;
                tst = dup_super(st);
                if (tst == NULL) {
-                       pr_vrb("cannot allocate memory for imsm supertype\n");
+                       pr_vrb("cannot allocate memory for imsm supertype\n");
                        goto err_1;
                }
                tmpdev->container = 0;
@@ -5924,7 +5980,7 @@ count_volumes_list(struct md_list *devlist, char *homehost,
        }
 
        for (tmpdev = devlist; tmpdev; tmpdev = tmpdev->next) {
-               if ((tmpdev->used == 1) && (tmpdev->found)) {
+               if (tmpdev->used == 1 && tmpdev->found) {
                        if (count) {
                                if (count < tmpdev->found)
                                        count = 0;
@@ -5967,7 +6023,7 @@ count_volumes(struct intel_hba *hba, int dpa, int verbose)
 
        devid_list = entry->devid_list;
        for (dv = devid_list; dv; dv = dv->next) {
-               struct md_list *devlist = NULL;
+               struct md_list *devlist;
                struct sys_dev *device = device_by_id(dv->devid);
                char *hba_path;
                int found = 0;
@@ -5977,14 +6033,6 @@ count_volumes(struct intel_hba *hba, int dpa, int verbose)
                else
                        return 0;
 
-               /* VMD has one orom entry for all domain, but spanning is not allowed.
-                * VMD arrays should be counted per domain (controller), so skip
-                * domains that are not the given one.
-                */
-               if ((hba->type == SYS_DEV_VMD) &&
-                  (strncmp(device->path, hba->path, strlen(device->path)) != 0))
-                       continue;
-
                devlist = get_devices(hba_path);
                /* if no intel devices return zero volumes */
                if (devlist == NULL)
@@ -6031,14 +6079,14 @@ validate_geometry_imsm_orom(struct intel_super *super, int level, int layout,
 {
        /* check/set platform and metadata limits/defaults */
        if (super->orom && raiddisks > super->orom->dpa) {
-               pr_vrb("platform supports a maximum of %d disks per array\n",
+               pr_vrb("platform supports a maximum of %d disks per array\n",
                       super->orom->dpa);
                return 0;
        }
 
        /* capabilities of OROM tested - copied from validate_geometry_imsm_volume */
        if (!is_raid_level_supported(super->orom, level, raiddisks)) {
-               pr_vrb("platform does not support raid%d with %d disk%s\n",
+               pr_vrb("platform does not support raid%d with %d disk%s\n",
                        level, raiddisks, raiddisks > 1 ? "s" : "");
                return 0;
        }
@@ -6047,24 +6095,24 @@ validate_geometry_imsm_orom(struct intel_super *super, int level, int layout,
                *chunk = imsm_default_chunk(super->orom);
 
        if (super->orom && !imsm_orom_has_chunk(super->orom, *chunk)) {
-               pr_vrb("platform does not support a chunk size of: %d\n", *chunk);
+               pr_vrb("platform does not support a chunk size of: %d\n", *chunk);
                return 0;
        }
 
        if (layout != imsm_level_to_layout(level)) {
                if (level == 5)
-                       pr_vrb("imsm raid 5 only supports the left-asymmetric layout\n");
+                       pr_vrb("imsm raid 5 only supports the left-asymmetric layout\n");
                else if (level == 10)
-                       pr_vrb("imsm raid 10 only supports the n2 layout\n");
+                       pr_vrb("imsm raid 10 only supports the n2 layout\n");
                else
-                       pr_vrb("imsm unknown layout %#x for this raid level %d\n",
+                       pr_vrb("imsm unknown layout %#x for this raid level %d\n",
                                layout, level);
                return 0;
        }
 
        if (super->orom && (super->orom->attr & IMSM_OROM_ATTR_2TB) == 0 &&
                        (calc_array_size(level, raiddisks, layout, *chunk, size) >> 32) > 0) {
-               pr_vrb("platform does not support a volume size over 2TB\n");
+               pr_vrb("platform does not support a volume size over 2TB\n");
                return 0;
        }
 
@@ -6235,7 +6283,7 @@ static int validate_geometry_imsm_volume(struct supertype *st, int level,
                int count = count_volumes(super->hba,
                                      super->orom->dpa, verbose);
                if (super->orom->vphba <= count) {
-                       pr_vrb("platform does not support more than %d raid volumes.\n",
+                       pr_vrb("platform does not support more than %d raid volumes.\n",
                               super->orom->vphba);
                        return 0;
                }
@@ -6391,7 +6439,7 @@ static int validate_geometry_imsm(struct supertype *st, int level, int layout,
                                count = count_volumes(super->hba,
                                                      super->orom->dpa, verbose);
                                if (super->orom->vphba <= count) {
-                                       pr_vrb("platform does not support more than %d raid volumes.\n",
+                                       pr_vrb("platform does not support more than %d raid volumes.\n",
                                               super->orom->vphba);
                                        return 0;
                                }
@@ -6958,8 +7006,8 @@ static int imsm_count_failed(struct intel_super *super, struct imsm_dev *dev,
                /* when MAP_X is passed both maps failures are counted
                 */
                if (prev &&
-                   ((look_in_map == MAP_1) || (look_in_map == MAP_X)) &&
-                   (i < prev->num_members)) {
+                   (look_in_map == MAP_1 || look_in_map == MAP_X) &&
+                   i < prev->num_members) {
                        ord = __le32_to_cpu(prev->disk_ord_tbl[i]);
                        idx_1 = ord_to_idx(ord);
 
@@ -6967,8 +7015,8 @@ static int imsm_count_failed(struct intel_super *super, struct imsm_dev *dev,
                        if (!disk || is_failed(disk) || ord & IMSM_ORD_REBUILD)
                                failed++;
                }
-               if (((look_in_map == MAP_0) || (look_in_map == MAP_X)) &&
-                   (i < map->num_members)) {
+               if ((look_in_map == MAP_0 || look_in_map == MAP_X) &&
+                   i < map->num_members) {
                        ord = __le32_to_cpu(map->disk_ord_tbl[i]);
                        idx = ord_to_idx(ord);
 
@@ -7017,8 +7065,8 @@ static int is_resyncing(struct imsm_dev *dev)
 
        migr_map = get_imsm_map(dev, MAP_1);
 
-       if ((migr_map->map_state == IMSM_T_STATE_NORMAL) &&
-           (dev->vol.migr_type != MIGR_GEN_MIGR))
+       if (migr_map->map_state == IMSM_T_STATE_NORMAL &&
+           dev->vol.migr_type != MIGR_GEN_MIGR)
                return 1;
        else
                return 0;
@@ -7061,8 +7109,7 @@ static int mark_failure(struct imsm_dev *dev, struct imsm_disk *disk, int idx)
                struct imsm_map *map2 = get_imsm_map(dev, MAP_1);
                int slot2 = get_imsm_disk_slot(map2, idx);
 
-               if ((slot2 < map2->num_members) &&
-                   (slot2 >= 0))
+               if (slot2 < map2->num_members && slot2 >= 0)
                        set_imsm_ord_tbl_ent(map2, slot2,
                                             idx | IMSM_ORD_REBUILD);
        }
@@ -7442,8 +7489,7 @@ static void imsm_set_disk(struct active_array *a, int n, int state)
        break;
        case IMSM_T_STATE_DEGRADED: /* transition to degraded state */
                dprintf_cont("degraded: ");
-               if ((map->map_state != map_state) &&
-                   !dev->vol.migr_state) {
+               if (map->map_state != map_state && !dev->vol.migr_state) {
                        dprintf_cont("mark degraded");
                        map->map_state = map_state;
                        super->updates_pending++;
@@ -7773,6 +7819,11 @@ static struct mdinfo *imsm_activate_spare(struct active_array *a,
                        IMSM_T_STATE_DEGRADED)
                return NULL;
 
+       if (get_imsm_map(dev, MAP_0)->map_state == IMSM_T_STATE_UNINITIALIZED) {
+               dprintf("imsm: No spare activation allowed. Volume is not initialized.\n");
+               return NULL;
+       }
+
        /*
         * If there are any failed disks check state of the other volume.
         * Block rebuild if the another one is failed until failed disks
@@ -7901,21 +7952,22 @@ static int disks_overlap(struct intel_super *super, int idx, struct imsm_update_
 
 static struct dl *get_disk_super(struct intel_super *super, int major, int minor)
 {
-       struct dl *dl = NULL;
+       struct dl *dl;
+
        for (dl = super->disks; dl; dl = dl->next)
-               if ((dl->major == major) &&  (dl->minor == minor))
+               if (dl->major == major &&  dl->minor == minor)
                        return dl;
        return NULL;
 }
 
 static int remove_disk_super(struct intel_super *super, int major, int minor)
 {
-       struct dl *prev = NULL;
+       struct dl *prev;
        struct dl *dl;
 
        prev = NULL;
        for (dl = super->disks; dl; dl = dl->next) {
-               if ((dl->major == major) && (dl->minor == minor)) {
+               if (dl->major == major && dl->minor == minor) {
                        /* remove */
                        if (prev)
                                prev->next = dl->next;
@@ -7936,7 +7988,8 @@ static void imsm_delete(struct intel_super *super, struct dl **dlp, unsigned ind
 static int add_remove_disk_update(struct intel_super *super)
 {
        int check_degraded = 0;
-       struct dl *disk = NULL;
+       struct dl *disk;
+
        /* add/remove some spares to/from the metadata/contrainer */
        while (super->disk_mgmt_list) {
                struct dl *disk_cfg;
@@ -7983,12 +8036,11 @@ static int apply_reshape_migration_update(struct imsm_update_reshape_migration *
        int ret_val = 0;
 
        dprintf("(enter)\n");
-       if ((u->subdev < 0) ||
-           (u->subdev > 1)) {
+       if (u->subdev < 0 || u->subdev > 1) {
                dprintf("imsm: Error: Wrong subdev: %i\n", u->subdev);
                return ret_val;
        }
-       if ((space_list == NULL) || (*space_list == NULL)) {
+       if (space_list == NULL || *space_list == NULL) {
                dprintf("imsm: Error: Memory is not allocated\n");
                return ret_val;
        }
@@ -8049,9 +8101,8 @@ static int apply_reshape_migration_update(struct imsm_update_reshape_migration *
 
                        /* add disk
                         */
-                       if ((u->new_level != 5) ||
-                           (migr_map->raid_level != 0) ||
-                           (migr_map->raid_level == map->raid_level))
+                       if (u->new_level != 5 || migr_map->raid_level != 0 ||
+                           migr_map->raid_level == map->raid_level)
                                goto skip_disk_add;
 
                        if (u->new_disks[0] >= 0) {
@@ -8102,8 +8153,7 @@ static int apply_size_change_update(struct imsm_update_size_change *u,
        int ret_val = 0;
 
        dprintf("(enter)\n");
-       if ((u->subdev < 0) ||
-           (u->subdev > 1)) {
+       if (u->subdev < 0 || u->subdev > 1) {
                dprintf("imsm: Error: Wrong subdev: %i\n", u->subdev);
                return ret_val;
        }
@@ -8270,9 +8320,9 @@ static int apply_reshape_container_disks_update(struct imsm_update_reshape *u,
                dprintf("imsm: new disk for reshape is: %i:%i (%p, index = %i)\n",
                        major(u->new_disks[i]), minor(u->new_disks[i]),
                        new_disk, new_disk->index);
-               if ((new_disk == NULL) ||
-                   ((new_disk->index >= 0) &&
-                    (new_disk->index < u->old_raid_disks)))
+               if (new_disk == NULL ||
+                   (new_disk->index >= 0 &&
+                    new_disk->index < u->old_raid_disks))
                        goto update_reshape_exit;
                new_disk->index = disk_count++;
                /* slot to fill in autolayout
@@ -8894,7 +8944,7 @@ static int imsm_prepare_update(struct supertype *st,
                                current_level = map->raid_level;
                                break;
                        }
-               if ((u->new_level == 5) && (u->new_level != current_level)) {
+               if (u->new_level == 5 && u->new_level != current_level) {
                        struct mdinfo *spares;
 
                        spares = get_spares_for_grow(st);
@@ -9158,8 +9208,7 @@ int open_backup_targets(struct mdinfo *info, int raid_disks, int *raid_fds,
                        continue;
                }
 
-               if ((sd->disk.raid_disk >= raid_disks) ||
-                   (sd->disk.raid_disk < 0))
+               if (sd->disk.raid_disk >= raid_disks || sd->disk.raid_disk < 0)
                        continue;
 
                dn = map_dev(sd->disk.major,
@@ -9174,9 +9223,8 @@ int open_backup_targets(struct mdinfo *info, int raid_disks, int *raid_fds,
        /* check if maximum array degradation level is not exceeded
        */
        if ((raid_disks - opened) >
-                       imsm_get_allowed_degradation(info->new_level,
-                                                    raid_disks,
-                                                    super, dev)) {
+           imsm_get_allowed_degradation(info->new_level, raid_disks,
+                                        super, dev)) {
                pr_err("Not enough disks can be opened.\n");
                close_targets(raid_fds, raid_disks);
                return -2;
@@ -9246,7 +9294,7 @@ int validate_container_imsm(struct mdinfo *info)
                        return 1;
                }
 
-               if ((orom != orom2) || ((hba->type == SYS_DEV_VMD) && (hba != hba2))) {
+               if (orom != orom2) {
                        pr_err("WARNING - IMSM container assembled with disks under different HBAs!\n"
                                "       This operation is not supported and can lead to data loss.\n");
                        return 1;
@@ -9358,8 +9406,8 @@ int save_backup_imsm(struct supertype *st,
 {
        int rv = -1;
        struct intel_super *super = st->sb;
-       unsigned long long *target_offsets = NULL;
-       int *targets = NULL;
+       unsigned long long *target_offsets;
+       int *targets;
        int i;
        struct imsm_map *map_dest = get_imsm_map(dev, MAP_0);
        int new_disks = map_dest->num_members;
@@ -9486,7 +9534,7 @@ int recover_backup_imsm(struct supertype *st, struct mdinfo *info)
 {
        struct intel_super *super = st->sb;
        struct migr_record *migr_rec = super->migr_rec;
-       struct imsm_map *map_dest = NULL;
+       struct imsm_map *map_dest;
        struct intel_dev *id = NULL;
        unsigned long long read_offset;
        unsigned long long write_offset;
@@ -9609,7 +9657,7 @@ static const char *imsm_get_disk_controller_domain(const char *path)
        strncat(disk_path, path, PATH_MAX - strlen(disk_path) - 1);
        if (stat(disk_path, &st) == 0) {
                struct sys_dev* hba;
-               char *path=NULL;
+               char *path;
 
                path = devt_to_devpath(st.st_rdev);
                if (path == NULL)
@@ -9688,8 +9736,7 @@ static int imsm_reshape_is_allowed_on_container(struct supertype *st,
                        break;
                }
 
-               if ((info->array.level != 0) &&
-                   (info->array.level != 5)) {
+               if (info->array.level != 0 && info->array.level != 5) {
                        /* we cannot use this container with other raid level
                         */
                        dprintf("imsm: for container operation wrong raid level (%i) detected\n",
@@ -9774,11 +9821,11 @@ static int imsm_create_metadata_update_for_reshape(
 {
        struct intel_super *super = st->sb;
        struct imsm_super *mpb = super->anchor;
-       int update_memory_size = 0;
-       struct imsm_update_reshape *u = NULL;
-       struct mdinfo *spares = NULL;
+       int update_memory_size;
+       struct imsm_update_reshape *u;
+       struct mdinfo *spares;
        int i;
-       int delta_disks = 0;
+       int delta_disks;
        struct mdinfo *dev;
 
        dprintf("(enter) raid_disks = %i\n", geo->raid_disks);
@@ -9855,8 +9902,8 @@ static int imsm_create_metadata_update_for_size_change(
                                struct imsm_update_size_change **updatep)
 {
        struct intel_super *super = st->sb;
-       int update_memory_size = 0;
-       struct imsm_update_size_change *u = NULL;
+       int update_memory_size;
+       struct imsm_update_size_change *u;
 
        dprintf("(enter) New size = %llu\n", geo->size);
 
@@ -9885,8 +9932,8 @@ static int imsm_create_metadata_update_for_migration(
                                        struct imsm_update_reshape_migration **updatep)
 {
        struct intel_super *super = st->sb;
-       int update_memory_size = 0;
-       struct imsm_update_reshape_migration *u = NULL;
+       int update_memory_size;
+       struct imsm_update_reshape_migration *u;
        struct imsm_dev *dev;
        int previous_level = -1;
 
@@ -9921,12 +9968,12 @@ static int imsm_create_metadata_update_for_migration(
                        previous_level = map->raid_level;
                }
        }
-       if ((geo->level == 5) && (previous_level == 0)) {
+       if (geo->level == 5 && previous_level == 0) {
                struct mdinfo *spares = NULL;
 
                u->new_raid_disks++;
                spares = get_spares_for_grow(st);
-               if ((spares == NULL) || (spares->array.spare_disks < 1)) {
+               if (spares == NULL || spares->array.spare_disks < 1) {
                        free(u);
                        sysfs_free(spares);
                        update_memory_size = 0;
@@ -9990,9 +10037,8 @@ enum imsm_reshape_type imsm_analyze_change(struct supertype *st,
        int rv;
 
        getinfo_super_imsm_volume(st, &info, NULL);
-       if ((geo->level != info.array.level) &&
-           (geo->level >= 0) &&
-           (geo->level != UnSet)) {
+       if (geo->level != info.array.level && geo->level >= 0 &&
+           geo->level != UnSet) {
                switch (info.array.level) {
                case 0:
                        if (geo->level == 5) {
@@ -10030,16 +10076,14 @@ enum imsm_reshape_type imsm_analyze_change(struct supertype *st,
        } else
                geo->level = info.array.level;
 
-       if ((geo->layout != info.array.layout)
-           && ((geo->layout != UnSet) && (geo->layout != -1))) {
+       if (geo->layout != info.array.layout &&
+           (geo->layout != UnSet && geo->layout != -1)) {
                change = CH_MIGRATION;
-               if ((info.array.layout == 0)
-                   && (info.array.level == 5)
-                   && (geo->layout == 5)) {
+               if (info.array.layout == 0 && info.array.level == 5 &&
+                   geo->layout == 5) {
                        /* reshape 5 -> 4 */
-               } else if ((info.array.layout == 5)
-                          && (info.array.level == 5)
-                          && (geo->layout == 0)) {
+               } else if (info.array.layout == 5 && info.array.level == 5 &&
+                          geo->layout == 0) {
                        /* reshape 4 -> 5 */
                        geo->layout = 0;
                        geo->level = 5;
@@ -10055,11 +10099,17 @@ enum imsm_reshape_type imsm_analyze_change(struct supertype *st,
                        imsm_layout = info.array.layout;
        }
 
-       if ((geo->chunksize > 0) && (geo->chunksize != UnSet)
-           && (geo->chunksize != info.array.chunk_size))
+       if (geo->chunksize > 0 && geo->chunksize != UnSet &&
+           geo->chunksize != info.array.chunk_size) {
+               if (info.array.level == 10) {
+                       pr_err("Error. Chunk size change for RAID 10 is not supported.\n");
+                       change = -1;
+                       goto analyse_change_exit;
+               }
                change = CH_MIGRATION;
-       else
+       } else {
                geo->chunksize = info.array.chunk_size;
+       }
 
        chunk = geo->chunksize / 1024;
 
@@ -10070,7 +10120,7 @@ enum imsm_reshape_type imsm_analyze_change(struct supertype *st,
         */
        current_size = info.custom_array_size / data_disks;
 
-       if ((geo->size > 0) && (geo->size != MAX_SIZE)) {
+       if (geo->size > 0 && geo->size != MAX_SIZE) {
                /* align component size
                 */
                geo->size = imsm_component_size_aligment_check(
@@ -10084,7 +10134,7 @@ enum imsm_reshape_type imsm_analyze_change(struct supertype *st,
                }
        }
 
-       if ((current_size != geo->size) && (geo->size > 0)) {
+       if (current_size != geo->size && geo->size > 0) {
                if (change != -1) {
                        pr_err("Error. Size change should be the only one at a time.\n");
                        change = -1;
@@ -10123,7 +10173,7 @@ enum imsm_reshape_type imsm_analyze_change(struct supertype *st,
                                geo->size = max_size;
                }
 
-               if ((direction == ROLLBACK_METADATA_CHANGES)) {
+               if (direction == ROLLBACK_METADATA_CHANGES) {
                        /* accept size for rollback only
                        */
                } else {
@@ -10169,8 +10219,8 @@ enum imsm_reshape_type imsm_analyze_change(struct supertype *st,
        }
 
 analyse_change_exit:
-       if ((direction == ROLLBACK_METADATA_CHANGES) &&
-            ((change == CH_MIGRATION) || (change == CH_TAKEOVER))) {
+       if (direction == ROLLBACK_METADATA_CHANGES &&
+           (change == CH_MIGRATION || change == CH_TAKEOVER)) {
                dprintf("imsm: Metadata changes rollback is not supported for migration and takeover operations.\n");
                change = -1;
        }
@@ -10345,6 +10395,33 @@ exit_imsm_reshape_super:
        return ret_val;
 }
 
+#define COMPLETED_OK           0
+#define COMPLETED_NONE         1
+#define COMPLETED_DELAYED      2
+
+static int read_completed(int fd, unsigned long long *val)
+{
+       int ret;
+       char buf[50];
+
+       ret = sysfs_fd_get_str(fd, buf, 50);
+       if (ret < 0)
+               return ret;
+
+       ret = COMPLETED_OK;
+       if (strncmp(buf, "none", 4) == 0) {
+               ret = COMPLETED_NONE;
+       } else if (strncmp(buf, "delayed", 7) == 0) {
+               ret = COMPLETED_DELAYED;
+       } else {
+               char *ep;
+               *val = strtoull(buf, &ep, 0);
+               if (ep == buf || (*ep != 0 && *ep != '\n' && *ep != ' '))
+                       ret = -1;
+       }
+       return ret;
+}
+
 /*******************************************************************************
  * Function:   wait_for_reshape_imsm
  * Description:        Function writes new sync_max value and waits until
@@ -10360,6 +10437,7 @@ exit_imsm_reshape_super:
 int wait_for_reshape_imsm(struct mdinfo *sra, int ndata)
 {
        int fd = sysfs_get_fd(sra, NULL, "sync_completed");
+       int retry = 3;
        unsigned long long completed;
        /* to_complete : new sync_max position */
        unsigned long long to_complete = sra->reshape_progress;
@@ -10370,11 +10448,17 @@ int wait_for_reshape_imsm(struct mdinfo *sra, int ndata)
                return 1;
        }
 
-       if (sysfs_fd_get_ll(fd, &completed) < 0) {
-               dprintf("cannot read reshape_position (no reshape in progres)\n");
-               close(fd);
-               return 1;
-       }
+       do {
+               if (sysfs_fd_get_ll(fd, &completed) < 0) {
+                       if (!retry) {
+                               dprintf("cannot read reshape_position (no reshape in progres)\n");
+                               close(fd);
+                               return 1;
+                       }
+                       usleep(30000);
+               } else
+                       break;
+       } while (retry--);
 
        if (completed > position_to_set) {
                dprintf("wrong next position to set %llu (%llu)\n",
@@ -10392,24 +10476,31 @@ int wait_for_reshape_imsm(struct mdinfo *sra, int ndata)
        }
 
        do {
+               int rc;
                char action[20];
                int timeout = 3000;
+
                sysfs_wait(fd, &timeout);
                if (sysfs_get_str(sra, NULL, "sync_action",
                                  action, 20) > 0 &&
                                strncmp(action, "reshape", 7) != 0) {
+                       if (strncmp(action, "idle", 4) == 0)
+                               break;
                        close(fd);
                        return -1;
                }
-               if (sysfs_fd_get_ll(fd, &completed) < 0) {
+
+               rc = read_completed(fd, &completed);
+               if (rc < 0) {
                        dprintf("cannot read reshape_position (in loop)\n");
                        close(fd);
                        return 1;
-               }
+               } else if (rc == COMPLETED_NONE)
+                       break;
        } while (completed < position_to_set);
+
        close(fd);
        return 0;
-
 }
 
 /*******************************************************************************
@@ -10430,7 +10521,7 @@ int check_degradation_change(struct mdinfo *info,
        int rv;
 
        rv = sysfs_get_ll(info, NULL, "degraded", &new_degraded);
-       if ((rv == -1) || (new_degraded != (unsigned long long)degraded)) {
+       if (rv == -1 || (new_degraded != (unsigned long long)degraded)) {
                /* check each device to ensure it is still working */
                struct mdinfo *sd;
                new_degraded = 0;
@@ -10465,7 +10556,7 @@ int check_degradation_change(struct mdinfo *info,
  * Function:   imsm_manage_reshape
  * Description:        Function finds array under reshape and it manages reshape
  *             process. It creates stripes backups (if required) and sets
- *             checheckpoits.
+ *             checkpoints.
  * Parameters:
  *     afd             : Backup handle (nattive) - not used
  *     sra             : general array info
@@ -10489,7 +10580,7 @@ static int imsm_manage_reshape(
 {
        int ret_val = 0;
        struct intel_super *super = st->sb;
-       struct intel_dev *dv = NULL;
+       struct intel_dev *dv;
        struct imsm_dev *dev = NULL;
        struct imsm_map *map_src;
        int migr_vol_qan = 0;
@@ -10507,7 +10598,10 @@ static int imsm_manage_reshape(
        int degraded = 0;
        int source_layout = 0;
 
-       if (!fds || !offsets || !sra)
+       if (!sra)
+               return ret_val;
+
+       if (!fds || !offsets)
                goto abort;
 
        /* Find volume during the reshape */
@@ -10520,7 +10614,7 @@ static int imsm_manage_reshape(
        }
        /* Only one volume can migrate at the same time */
        if (migr_vol_qan != 1) {
-               pr_err("%s", migr_vol_qan ?
+               pr_err("%s", migr_vol_qan ?
                        "Number of migrating volumes greater than 1\n" :
                        "There is no volume during migrationg\n");
                goto abort;
@@ -10595,7 +10689,7 @@ static int imsm_manage_reshape(
 
                start = current_position * 512;
 
-               /* allign reading start to old geometry */
+               /* align reading start to old geometry */
                start_buf_shift = start % old_data_stripe_length;
                start_src = start - start_buf_shift;
 
@@ -10609,7 +10703,7 @@ static int imsm_manage_reshape(
                         *             to backup alligned to source array
                         *             [bytes]
                         */
-                       unsigned long long next_step_filler = 0;
+                       unsigned long long next_step_filler;
                        unsigned long long copy_length = next_step * 512;
 
                        /* allign copy area length to stripe in old geometry */
@@ -10700,6 +10794,10 @@ static int imsm_manage_reshape(
        ret_val = 1;
 abort:
        free(buf);
+       /* See Grow.c: abort_reshape() for further explanation */
+       sysfs_set_num(sra, NULL, "suspend_lo", 0x7FFFFFFFFFFFFFFFULL);
+       sysfs_set_num(sra, NULL, "suspend_hi", 0);
+       sysfs_set_num(sra, NULL, "suspend_lo", 0);
 
        return ret_val;
 }