]> git.ipfire.org Git - thirdparty/mdadm.git/blobdiff - super-intel.c
fix: segfault when killing subarray of non-existent container
[thirdparty/mdadm.git] / super-intel.c
index 7654a11721f14d71f7b98707daefdf466a2dd4fd..a78d7238a755aefaa88353db44719736e8bed163 100644 (file)
 #define MAX_SIGNATURE_LENGTH  32
 #define MAX_RAID_SERIAL_LEN   16
 
-#define MPB_ATTRIB_CHECKSUM_VERIFY __cpu_to_le32(0x80000000)
-#define MPB_ATTRIB_PM      __cpu_to_le32(0x40000000)
-#define MPB_ATTRIB_2TB     __cpu_to_le32(0x20000000)
-#define MPB_ATTRIB_RAID0   __cpu_to_le32(0x00000001)
-#define MPB_ATTRIB_RAID1   __cpu_to_le32(0x00000002)
-#define MPB_ATTRIB_RAID10  __cpu_to_le32(0x00000004)
-#define MPB_ATTRIB_RAID1E  __cpu_to_le32(0x00000008)
-#define MPB_ATTRIB_RAID5   __cpu_to_le32(0x00000010)
-#define MPB_ATTRIB_RAIDCNG __cpu_to_le32(0x00000020)
+/* supports RAID0 */
+#define MPB_ATTRIB_RAID0               __cpu_to_le32(0x00000001)
+/* supports RAID1 */
+#define MPB_ATTRIB_RAID1               __cpu_to_le32(0x00000002)
+/* supports RAID10 */
+#define MPB_ATTRIB_RAID10              __cpu_to_le32(0x00000004)
+/* supports RAID1E */
+#define MPB_ATTRIB_RAID1E              __cpu_to_le32(0x00000008)
+/* supports RAID5 */
+#define MPB_ATTRIB_RAID5               __cpu_to_le32(0x00000010)
+/* supports RAID CNG */
+#define MPB_ATTRIB_RAIDCNG             __cpu_to_le32(0x00000020)
+/* supports expanded stripe sizes of  256K, 512K and 1MB */
+#define MPB_ATTRIB_EXP_STRIPE_SIZE     __cpu_to_le32(0x00000040)
+
+/* The OROM Support RST Caching of Volumes */
+#define MPB_ATTRIB_NVM                 __cpu_to_le32(0x02000000)
+/* The OROM supports creating disks greater than 2TB */
+#define MPB_ATTRIB_2TB_DISK            __cpu_to_le32(0x04000000)
+/* The OROM supports Bad Block Management */
+#define MPB_ATTRIB_BBM                 __cpu_to_le32(0x08000000)
+
+/* THe OROM Supports NVM Caching of Volumes */
+#define MPB_ATTRIB_NEVER_USE2           __cpu_to_le32(0x10000000)
+/* The OROM supports creating volumes greater than 2TB */
+#define MPB_ATTRIB_2TB                 __cpu_to_le32(0x20000000)
+/* originally for PMP, now it's wasted b/c. Never use this bit! */
+#define MPB_ATTRIB_NEVER_USE           __cpu_to_le32(0x40000000)
+/* Verify MPB contents against checksum after reading MPB */
+#define MPB_ATTRIB_CHECKSUM_VERIFY     __cpu_to_le32(0x80000000)
+
+/* Define all supported attributes that have to be accepted by mdadm
+ */
+#define MPB_ATTRIB_SUPPORTED          (MPB_ATTRIB_CHECKSUM_VERIFY | \
+                                       MPB_ATTRIB_2TB             | \
+                                       MPB_ATTRIB_2TB_DISK        | \
+                                       MPB_ATTRIB_RAID0           | \
+                                       MPB_ATTRIB_RAID1           | \
+                                       MPB_ATTRIB_RAID10          | \
+                                       MPB_ATTRIB_RAID5           | \
+                                       MPB_ATTRIB_EXP_STRIPE_SIZE)
+
+/* Define attributes that are unused but not harmful */
+#define MPB_ATTRIB_IGNORED             (MPB_ATTRIB_NEVER_USE)
 
 #define MPB_SECTOR_CNT 2210
 #define IMSM_RESERVED_SECTORS 4096
@@ -309,7 +344,7 @@ struct intel_super {
                struct extent *e; /* for determining freespace @ create */
                int raiddisk; /* slot to fill in autolayout */
                enum action action;
-       } *disks;
+       } *disks, *current_disk;
        struct dl *disk_mgmt_list; /* list of disks to add/remove while mdmon
                                      active */
        struct dl *missing; /* disks removed while we weren't looking */
@@ -345,7 +380,8 @@ enum imsm_update_type {
        update_add_remove_disk,
        update_reshape_container_disks,
        update_reshape_migration,
-       update_takeover
+       update_takeover,
+       update_general_migration_checkpoint,
 };
 
 struct imsm_update_activate_spare {
@@ -398,6 +434,11 @@ struct imsm_update_reshape_migration {
        int new_disks[1]; /* new_raid_disks - old_raid_disks makedev number */
 };
 
+struct imsm_update_general_migration_checkpoint {
+       enum imsm_update_type type;
+       __u32 curr_migr_unit;
+};
+
 struct disk_info {
        __u8 serial[MAX_RAID_SERIAL_LEN];
 };
@@ -557,7 +598,7 @@ static __u8 *get_imsm_version(struct imsm_super *mpb)
 {
        return &mpb->sig[MPB_SIG_LEN];
 }
-#endif 
+#endif
 
 /* retrieve a disk directly from the anchor when the anchor is known to be
  * up-to-date, currently only at load time
@@ -792,7 +833,16 @@ static struct extent *get_extents(struct intel_super *super, struct dl *dl)
        struct extent *rv, *e;
        int i;
        int memberships = count_memberships(dl, super);
-       __u32 reservation = MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS;
+       __u32 reservation;
+
+       /* trim the reserved area for spares, so they can join any array
+        * regardless of whether the OROM has assigned sectors from the
+        * IMSM_RESERVED_SECTORS region
+        */
+       if (dl->index == -1)
+               reservation = MPB_SECTOR_CNT;
+       else
+               reservation = MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS;
 
        rv = malloc(sizeof(struct extent) * (memberships + 1));
        if (!rv)
@@ -1018,18 +1068,20 @@ static void print_imsm_dev(struct intel_super *super,
        printf("    Dirty State : %s\n", dev->vol.dirty ? "dirty" : "clean");
 }
 
-static void print_imsm_disk(struct imsm_super *mpb, int index, __u32 reserved)
+static void print_imsm_disk(struct imsm_disk *disk, int index, __u32 reserved)
 {
-       struct imsm_disk *disk = __get_imsm_disk(mpb, index);
        char str[MAX_RAID_SERIAL_LEN + 1];
        __u64 sz;
 
-       if (index < 0 || !disk)
+       if (index < -1 || !disk)
                return;
 
        printf("\n");
        snprintf(str, MAX_RAID_SERIAL_LEN + 1, "%s", disk->serial);
-       printf("  Disk%02d Serial : %s\n", index, str);
+       if (index >= 0)
+               printf("  Disk%02d Serial : %s\n", index, str);
+       else
+               printf("    Disk Serial : %s\n", str);
        printf("          State :%s%s%s\n", is_spare(disk) ? " spare" : "",
                                            is_configured(disk) ? " active" : "",
                                            is_failed(disk) ? " failed" : "");
@@ -1089,7 +1141,95 @@ void examine_migr_rec_imsm(struct intel_super *super)
                break;
        }
 }
+#endif /* MDASSEMBLE */
+/*******************************************************************************
+ * function: imsm_check_attributes
+ * Description: Function checks if features represented by attributes flags
+ *             are supported by mdadm.
+ * Parameters:
+ *             attributes - Attributes read from metadata
+ * Returns:
+ *             0 - passed attributes contains unsupported features flags
+ *             1 - all features are supported
+ ******************************************************************************/
+static int imsm_check_attributes(__u32 attributes)
+{
+       int ret_val = 1;
+       __u32 not_supported = MPB_ATTRIB_SUPPORTED^0xffffffff;
+
+       not_supported &= ~MPB_ATTRIB_IGNORED;
+
+       not_supported &= attributes;
+       if (not_supported) {
+               fprintf(stderr, Name "(IMSM): Unsupported attributes : %x\n",
+                       (unsigned)__le32_to_cpu(not_supported));
+               if (not_supported & MPB_ATTRIB_CHECKSUM_VERIFY) {
+                       dprintf("\t\tMPB_ATTRIB_CHECKSUM_VERIFY \n");
+                       not_supported ^= MPB_ATTRIB_CHECKSUM_VERIFY;
+               }
+               if (not_supported & MPB_ATTRIB_2TB) {
+                       dprintf("\t\tMPB_ATTRIB_2TB\n");
+                       not_supported ^= MPB_ATTRIB_2TB;
+               }
+               if (not_supported & MPB_ATTRIB_RAID0) {
+                       dprintf("\t\tMPB_ATTRIB_RAID0\n");
+                       not_supported ^= MPB_ATTRIB_RAID0;
+               }
+               if (not_supported & MPB_ATTRIB_RAID1) {
+                       dprintf("\t\tMPB_ATTRIB_RAID1\n");
+                       not_supported ^= MPB_ATTRIB_RAID1;
+               }
+               if (not_supported & MPB_ATTRIB_RAID10) {
+                       dprintf("\t\tMPB_ATTRIB_RAID10\n");
+                       not_supported ^= MPB_ATTRIB_RAID10;
+               }
+               if (not_supported & MPB_ATTRIB_RAID1E) {
+                       dprintf("\t\tMPB_ATTRIB_RAID1E\n");
+                       not_supported ^= MPB_ATTRIB_RAID1E;
+               }
+               if (not_supported & MPB_ATTRIB_RAID5) {
+               dprintf("\t\tMPB_ATTRIB_RAID5\n");
+                       not_supported ^= MPB_ATTRIB_RAID5;
+               }
+               if (not_supported & MPB_ATTRIB_RAIDCNG) {
+                       dprintf("\t\tMPB_ATTRIB_RAIDCNG\n");
+                       not_supported ^= MPB_ATTRIB_RAIDCNG;
+               }
+               if (not_supported & MPB_ATTRIB_BBM) {
+                       dprintf("\t\tMPB_ATTRIB_BBM\n");
+               not_supported ^= MPB_ATTRIB_BBM;
+               }
+               if (not_supported & MPB_ATTRIB_CHECKSUM_VERIFY) {
+                       dprintf("\t\tMPB_ATTRIB_CHECKSUM_VERIFY (== MPB_ATTRIB_LEGACY)\n");
+                       not_supported ^= MPB_ATTRIB_CHECKSUM_VERIFY;
+               }
+               if (not_supported & MPB_ATTRIB_EXP_STRIPE_SIZE) {
+                       dprintf("\t\tMPB_ATTRIB_EXP_STRIP_SIZE\n");
+                       not_supported ^= MPB_ATTRIB_EXP_STRIPE_SIZE;
+               }
+               if (not_supported & MPB_ATTRIB_2TB_DISK) {
+                       dprintf("\t\tMPB_ATTRIB_2TB_DISK\n");
+                       not_supported ^= MPB_ATTRIB_2TB_DISK;
+               }
+               if (not_supported & MPB_ATTRIB_NEVER_USE2) {
+                       dprintf("\t\tMPB_ATTRIB_NEVER_USE2\n");
+                       not_supported ^= MPB_ATTRIB_NEVER_USE2;
+               }
+               if (not_supported & MPB_ATTRIB_NEVER_USE) {
+                       dprintf("\t\tMPB_ATTRIB_NEVER_USE\n");
+                       not_supported ^= MPB_ATTRIB_NEVER_USE;
+               }
+
+               if (not_supported)
+                       dprintf(Name "(IMSM): Unknown attributes : %x\n", not_supported);
+
+               ret_val = 0;
+       }
+
+       return ret_val;
+}
 
+#ifndef MDASSEMBLE
 static void getinfo_super_imsm(struct supertype *st, struct mdinfo *info, char *map);
 
 static void examine_super_imsm(struct supertype *st, char *homehost)
@@ -1111,6 +1251,11 @@ static void examine_super_imsm(struct supertype *st, char *homehost)
        printf("    Orig Family : %08x\n", __le32_to_cpu(mpb->orig_family_num));
        printf("         Family : %08x\n", __le32_to_cpu(mpb->family_num));
        printf("     Generation : %08x\n", __le32_to_cpu(mpb->generation_num));
+       printf("     Attributes : ");
+       if (imsm_check_attributes(mpb->attributes))
+               printf("All supported\n");
+       else
+               printf("not supported\n");
        getinfo_super_imsm(st, &info, NULL);
        fname_from_uuid(st, &info, nbuf, ':');
        printf("           UUID : %s\n", nbuf + 5);
@@ -1120,7 +1265,7 @@ static void examine_super_imsm(struct supertype *st, char *homehost)
        printf("    MPB Sectors : %d\n", mpb_sectors(mpb));
        printf("          Disks : %d\n", mpb->num_disks);
        printf("   RAID Devices : %d\n", mpb->num_raid_devs);
-       print_imsm_disk(mpb, super->disks->index, reserved);
+       print_imsm_disk(__get_imsm_disk(mpb, super->disks->index), super->disks->index, reserved);
        if (super->bbm_log) {
                struct bbm_log *log = super->bbm_log;
 
@@ -1145,28 +1290,12 @@ static void examine_super_imsm(struct supertype *st, char *homehost)
        for (i = 0; i < mpb->num_disks; i++) {
                if (i == super->disks->index)
                        continue;
-               print_imsm_disk(mpb, i, reserved);
+               print_imsm_disk(__get_imsm_disk(mpb, i), i, reserved);
        }
-       for (dl = super->disks ; dl; dl = dl->next) {
-               struct imsm_disk *disk;
-               char str[MAX_RAID_SERIAL_LEN + 1];
-               __u64 sz;
-
-               if (dl->index >= 0)
-                       continue;
 
-               disk = &dl->disk;
-               printf("\n");
-               snprintf(str, MAX_RAID_SERIAL_LEN + 1, "%s", disk->serial);
-               printf("    Disk Serial : %s\n", str);
-               printf("          State :%s%s%s\n", is_spare(disk) ? " spare" : "",
-                      is_configured(disk) ? " active" : "",
-                      is_failed(disk) ? " failed" : "");
-               printf("             Id : %08x\n", __le32_to_cpu(disk->scsi_id));
-               sz = __le32_to_cpu(disk->total_blocks) - reserved;
-               printf("    Usable Size : %llu%s\n", (unsigned long long)sz,
-                      human_size(sz * 512));
-       }
+       for (dl = super->disks; dl; dl = dl->next)
+               if (dl->index == -1)
+                       print_imsm_disk(&dl->disk, -1, reserved);
 
        examine_migr_rec_imsm(super);
 }
@@ -1390,9 +1519,9 @@ static int ahci_enumerate_ports(const char *hba_path, int port_count, int host_b
                        fd2devname(fd, buf);
                        printf("          Port%d : %s", port, buf);
                        if (imsm_read_serial(fd, NULL, (__u8 *) buf) == 0)
-                               printf(" (%s)\n", buf);
+                               printf(" (%.*s)\n", MAX_RAID_SERIAL_LEN, buf);
                        else
-                               printf("()\n");
+                               printf(" ()\n");
                }
                close(fd);
                free(path);
@@ -1413,8 +1542,6 @@ static int ahci_enumerate_ports(const char *hba_path, int port_count, int host_b
        return err;
 }
 
-
-
 static void print_found_intel_controllers(struct sys_dev *elem)
 {
        for (; elem; elem = elem->next) {
@@ -1816,7 +1943,7 @@ static __u64 blocks_per_migr_unit(struct intel_super *super,
                migr_chunk = migr_strip_blocks_resync(dev);
                disks = imsm_num_data_members(dev, 0);
                blocks_per_unit = stripes_per_unit * migr_chunk * disks;
-               stripe = __le32_to_cpu(map->blocks_per_strip) * disks;
+               stripe = __le16_to_cpu(map->blocks_per_strip) * disks;
                segment = blocks_per_unit / stripe;
                block_rel = blocks_per_unit - segment * stripe;
                parity_depth = parity_segment_depth(dev);
@@ -1938,6 +2065,52 @@ out:
        return retval;
 }
 
+#ifndef MDASSEMBLE
+/*******************************************************************************
+ * function: imsm_create_metadata_checkpoint_update
+ * Description: It creates update for checkpoint change.
+ * Parameters:
+ *     super   : imsm internal array info
+ *     u       : pointer to prepared update
+ * Returns:
+ *     Uptate length.
+ *     If length is equal to 0, input pointer u contains no update
+ ******************************************************************************/
+static int imsm_create_metadata_checkpoint_update(
+       struct intel_super *super,
+       struct imsm_update_general_migration_checkpoint **u)
+{
+
+       int update_memory_size = 0;
+
+       dprintf("imsm_create_metadata_checkpoint_update(enter)\n");
+
+       if (u == NULL)
+               return 0;
+       *u = NULL;
+
+       /* size of all update data without anchor */
+       update_memory_size =
+               sizeof(struct imsm_update_general_migration_checkpoint);
+
+       *u = calloc(1, update_memory_size);
+       if (*u == NULL) {
+               dprintf("error: cannot get memory for "
+                       "imsm_create_metadata_checkpoint_update update\n");
+               return 0;
+       }
+       (*u)->type = update_general_migration_checkpoint;
+       (*u)->curr_migr_unit = __le32_to_cpu(super->migr_rec->curr_migr_unit);
+       dprintf("imsm_create_metadata_checkpoint_update: prepared for %u\n",
+               (*u)->curr_migr_unit);
+
+       return update_memory_size;
+}
+
+
+static void imsm_update_metadata_locally(struct supertype *st,
+                                        void *buf, int len);
+
 /*******************************************************************************
  * Function:   write_imsm_migr_rec
  * Description:        Function writes imsm migration record
@@ -1956,6 +2129,8 @@ static int write_imsm_migr_rec(struct supertype *st)
        int fd = -1;
        int retval = -1;
        struct dl *sd;
+       int len;
+       struct imsm_update_general_migration_checkpoint *u;
 
        for (sd = super->disks ; sd ; sd = sd->next) {
                /* write to 2 first slots only */
@@ -1981,6 +2156,26 @@ static int write_imsm_migr_rec(struct supertype *st)
                close(fd);
                fd = -1;
        }
+       /* update checkpoint information in metadata */
+       len = imsm_create_metadata_checkpoint_update(super, &u);
+
+       if (len <= 0) {
+               dprintf("imsm: Cannot prepare update\n");
+               goto out;
+       }
+       /* update metadata locally */
+       imsm_update_metadata_locally(st, u, len);
+       /* and possibly remotely */
+       if (st->update_tail) {
+               append_metadata_update(st, u, len);
+               /* during reshape we do all work inside metadata handler
+                * manage_reshape(), so metadata update has to be triggered
+                * insida it
+                */
+               flush_metadata_updates(st);
+               st->update_tail = &st->updates;
+       } else
+               free(u);
 
        retval = 0;
  out:
@@ -1988,6 +2183,7 @@ static int write_imsm_migr_rec(struct supertype *st)
                close(fd);
        return retval;
 }
+#endif /* MDASSEMBLE */
 
 static void getinfo_super_imsm_volume(struct supertype *st, struct mdinfo *info, char *dmap)
 {
@@ -2006,9 +2202,8 @@ static void getinfo_super_imsm_volume(struct supertype *st, struct mdinfo *info,
        if (prev_map)
                map_to_analyse = prev_map;
 
-       for (dl = super->disks; dl; dl = dl->next)
-               if (dl->raiddisk == info->disk.raid_disk)
-                       break;
+       dl = super->current_disk;
+
        info->container_member    = super->current_vol;
        info->array.raid_disks    = map->num_members;
        info->array.level         = get_imsm_raid_level(map_to_analyse);
@@ -2054,7 +2249,6 @@ static void getinfo_super_imsm_volume(struct supertype *st, struct mdinfo *info,
                                /* conversion is happening as RAID5 */
                                info->array.level = 5;
                                info->array.layout = ALGORITHM_PARITY_N;
-                               info->array.raid_disks += 1;
                                info->delta_disks -= 1;
                                break;
                        default:
@@ -2070,11 +2264,13 @@ static void getinfo_super_imsm_volume(struct supertype *st, struct mdinfo *info,
                info->new_chunk = info->array.chunk_size;
                info->delta_disks = 0;
        }
-       info->disk.major = 0;
-       info->disk.minor = 0;
+
        if (dl) {
                info->disk.major = dl->major;
                info->disk.minor = dl->minor;
+               info->disk.number = dl->index;
+               info->disk.raid_disk = get_imsm_disk_slot(map_to_analyse,
+                                                         dl->index);
        }
 
        info->data_offset         = __le32_to_cpu(map_to_analyse->pba_of_lba0);
@@ -2122,11 +2318,20 @@ static void getinfo_super_imsm_volume(struct supertype *st, struct mdinfo *info,
                        unsigned long long array_blocks;
                        int used_disks;
 
+                       if (__le32_to_cpu(migr_rec->ascending_migr) &&
+                           (units <
+                               (__le32_to_cpu(migr_rec->num_migr_units)-1)) &&
+                           (super->migr_rec->rec_status ==
+                                       __cpu_to_le32(UNIT_SRC_IN_CP_AREA)))
+                               units++;
+
                        info->reshape_progress = blocks_per_unit * units;
 
                        dprintf("IMSM: General Migration checkpoint : %llu "
                               "(%llu) -> read reshape progress : %llu\n",
-                               units, blocks_per_unit, info->reshape_progress);
+                               (unsigned long long)units,
+                               (unsigned long long)blocks_per_unit,
+                               info->reshape_progress);
 
                        used_disks = imsm_num_data_members(dev, 1);
                        if (used_disks > 0) {
@@ -2670,7 +2875,6 @@ static void serialcpy(__u8 *dest, __u8 *src)
        strncpy((char *) dest, (char *) src, MAX_RAID_SERIAL_LEN);
 }
 
-#ifndef MDASSEMBLE
 static struct dl *serial_to_dl(__u8 *serial, struct intel_super *super)
 {
        struct dl *dl;
@@ -2681,7 +2885,6 @@ static struct dl *serial_to_dl(__u8 *serial, struct intel_super *super)
 
        return dl;
 }
-#endif
 
 static struct imsm_disk *
 __serial_to_disk(__u8 *serial, struct imsm_super *mpb, int *idx)
@@ -2966,8 +3169,8 @@ static int load_imsm_mpb(int fd, struct intel_super *super, char *devname)
 
        if (lseek64(fd, dsize - (512 * 2), SEEK_SET) < 0) {
                if (devname)
-                       fprintf(stderr,
-                               Name ": Cannot seek to anchor block on %s: %s\n",
+                       fprintf(stderr, Name
+                               ": Cannot seek to anchor block on %s: %s\n",
                                devname, strerror(errno));
                return 1;
        }
@@ -3239,7 +3442,6 @@ static int find_intel_hba_capability(int fd, struct intel_super *super, char *de
        return 0;
 }
 
-#ifndef MDASSEMBLE
 /* find_missing - helper routine for load_super_imsm_all that identifies
  * disks that have disappeared from the system.  This routine relies on
  * the mpb being uptodate, which it is at load time.
@@ -3275,6 +3477,7 @@ static int find_missing(struct intel_super *super)
        return 0;
 }
 
+#ifndef MDASSEMBLE
 static struct intel_disk *disk_list_get(__u8 *serial, struct intel_disk *disk_list)
 {
        struct intel_disk *idisk = disk_list;
@@ -3764,16 +3967,17 @@ static int load_super_imsm(struct supertype *st, int fd, char *devname)
        }
 
        /* load migration record */
-       load_imsm_migr_rec(super, NULL);
-
-       /* Check for unsupported migration features */
-       if (check_mpb_migr_compatibility(super) != 0) {
-               fprintf(stderr, Name ": Unsupported migration detected");
-               if (devname)
-                       fprintf(stderr, " on %s\n", devname);
-               else
-                       fprintf(stderr, " (IMSM).\n");
-               return 3;
+       if (load_imsm_migr_rec(super, NULL) == 0) {
+               /* Check for unsupported migration features */
+               if (check_mpb_migr_compatibility(super) != 0) {
+                       fprintf(stderr,
+                               Name ": Unsupported migration detected");
+                       if (devname)
+                               fprintf(stderr, " on %s\n", devname);
+                       else
+                               fprintf(stderr, " (IMSM).\n");
+                       return 3;
+               }
        }
 
        return 0;
@@ -3927,12 +4131,40 @@ static int init_super_imsm_volume(struct supertype *st, mdu_array_info_t *info,
                memset(mpb_new + size_old, 0, size_round - size_old);
        }
        super->current_vol = idx;
-       /* when creating the first raid device in this container set num_disks
-        * to zero, i.e. delete this spare and add raid member devices in
-        * add_to_super_imsm_volume()
+
+       /* handle 'failed_disks' by either:
+        * a) create dummy disk entries in the table if this the first
+        *    volume in the array.  We add them here as this is the only
+        *    opportunity to add them. add_to_super_imsm_volume()
+        *    handles the non-failed disks and continues incrementing
+        *    mpb->num_disks.
+        * b) validate that 'failed_disks' matches the current number
+        *    of missing disks if the container is populated
         */
-       if (super->current_vol == 0)
+       if (super->current_vol == 0) {
                mpb->num_disks = 0;
+               for (i = 0; i < info->failed_disks; i++) {
+                       struct imsm_disk *disk;
+
+                       mpb->num_disks++;
+                       disk = __get_imsm_disk(mpb, i);
+                       disk->status = CONFIGURED_DISK | FAILED_DISK;
+                       disk->scsi_id = __cpu_to_le32(~(__u32)0);
+                       snprintf((char *) disk->serial, MAX_RAID_SERIAL_LEN,
+                                "missing:%d", i);
+               }
+               find_missing(super);
+       } else {
+               int missing = 0;
+               struct dl *d;
+
+               for (d = super->missing; d; d = d->next)
+                       missing++;
+               if (info->failed_disks > missing) {
+                       fprintf(stderr, Name": unable to add 'missing' disk to container\n");
+                       return 0;
+               }
+       }
 
        if (!check_name(super, name, 0))
                return 0;
@@ -3964,15 +4196,14 @@ static int init_super_imsm_volume(struct supertype *st, mdu_array_info_t *info,
        vol = &dev->vol;
        vol->migr_state = 0;
        set_migr_type(dev, MIGR_INIT);
-       vol->dirty = 0;
+       vol->dirty = !info->state;
        vol->curr_migr_unit = 0;
        map = get_imsm_map(dev, 0);
        map->pba_of_lba0 = __cpu_to_le32(super->create_offset);
        map->blocks_per_member = __cpu_to_le32(info_to_blocks_per_member(info));
        map->blocks_per_strip = __cpu_to_le16(info_to_blocks_per_strip(info));
        map->failed_disk_num = ~0;
-       map->map_state = info->level ? IMSM_T_STATE_UNINITIALIZED :
-                                      IMSM_T_STATE_NORMAL;
+       map->map_state = info->failed_disks ? IMSM_T_STATE_DEGRADED : IMSM_T_STATE_NORMAL;
        map->ddf = 1;
 
        if (info->level == 1 && info->raid_disks > 2) {
@@ -4080,9 +4311,10 @@ static int add_to_super_imsm_volume(struct supertype *st, mdu_disk_info_t *dk,
 {
        struct intel_super *super = st->sb;
        struct imsm_super *mpb = super->anchor;
-       struct dl *dl;
+       struct imsm_disk *_disk;
        struct imsm_dev *dev;
        struct imsm_map *map;
+       struct dl *dl, *df;
        int slot;
 
        dev = get_imsm_dev(super, super->current_vol);
@@ -4126,15 +4358,40 @@ static int add_to_super_imsm_volume(struct supertype *st, mdu_disk_info_t *dk,
                        devname);
                return 1;
        }
-       set_imsm_ord_tbl_ent(map, dk->number, dl->index);
+       set_imsm_ord_tbl_ent(map, dk->raid_disk, dl->index);
        dl->disk.status = CONFIGURED_DISK;
 
+       /* update size of 'missing' disks to be at least as large as the
+        * largest acitve member (we only have dummy missing disks when
+        * creating the first volume)
+        */
+       if (super->current_vol == 0) {
+               for (df = super->missing; df; df = df->next) {
+                       if (dl->disk.total_blocks > df->disk.total_blocks)
+                               df->disk.total_blocks = dl->disk.total_blocks;
+                       _disk = __get_imsm_disk(mpb, df->index);
+                       *_disk = df->disk;
+               }
+       }
+
+       /* refresh unset/failed slots to point to valid 'missing' entries */
+       for (df = super->missing; df; df = df->next)
+               for (slot = 0; slot < mpb->num_disks; slot++) {
+                       __u32 ord = get_imsm_ord_tbl_ent(dev, slot, -1);
+
+                       if ((ord & IMSM_ORD_REBUILD) == 0)
+                               continue;
+                       set_imsm_ord_tbl_ent(map, slot, df->index | IMSM_ORD_REBUILD);
+                       dprintf("set slot:%d to missing disk:%d\n", slot, df->index);
+                       break;
+               }
+
        /* if we are creating the first raid device update the family number */
        if (super->current_vol == 0) {
                __u32 sum;
                struct imsm_dev *_dev = __get_imsm_dev(mpb, 0);
-               struct imsm_disk *_disk = __get_imsm_disk(mpb, dl->index);
 
+               _disk = __get_imsm_disk(mpb, dl->index);
                if (!_dev || !_disk) {
                        fprintf(stderr, Name ": BUG mpb setup error\n");
                        return 1;
@@ -4146,7 +4403,7 @@ static int add_to_super_imsm_volume(struct supertype *st, mdu_disk_info_t *dk,
                mpb->family_num = __cpu_to_le32(sum);
                mpb->orig_family_num = mpb->family_num;
        }
-
+       super->current_disk = dl;
        return 0;
 }
 
@@ -4312,8 +4569,6 @@ static int write_super_imsm_spares(struct intel_super *super, int doclose)
        return 0;
 }
 
-static int is_gen_migration(struct imsm_dev *dev);
-
 static int write_super_imsm(struct supertype *st, int doclose)
 {
        struct intel_super *super = st->sb;
@@ -4375,7 +4630,7 @@ static int write_super_imsm(struct supertype *st, int doclose)
 
        /* write the mpb for disks that compose raid devices */
        for (d = super->disks; d ; d = d->next) {
-               if (d->index < 0)
+               if (d->index < 0 || is_failed(&d->disk))
                        continue;
                if (store_imsm_mpb(d->fd, mpb))
                        fprintf(stderr, "%s: failed for device %d:%d %s\n",
@@ -4385,7 +4640,8 @@ static int write_super_imsm(struct supertype *st, int doclose)
 
                        get_dev_size(d->fd, NULL, &dsize);
                        if (lseek64(d->fd, dsize - 512, SEEK_SET) >= 0) {
-                               write(d->fd, super->migr_rec_buf, 512);
+                               if (write(d->fd, super->migr_rec_buf, 512) != 512)
+                                       perror("Write migr_rec failed");
                        }
                }
                if (doclose) {
@@ -4712,43 +4968,44 @@ static int is_raid_level_supported(const struct imsm_orom *orom, int level, int
        return 0;
 }
 
+static int imsm_default_chunk(const struct imsm_orom *orom)
+{
+       /* up to 512 if the plaform supports it, otherwise the platform max.
+        * 128 if no platform detected
+        */
+       int fs = max(7, orom ? fls(orom->sss) : 0);
+
+       return min(512, (1 << fs));
+}
 
 #define pr_vrb(fmt, arg...) (void) (verbose && fprintf(stderr, Name fmt, ##arg))
-/*
- * validate volume parameters with OROM/EFI capabilities
- */
 static int
 validate_geometry_imsm_orom(struct intel_super *super, int level, int layout,
                            int raiddisks, int *chunk, int verbose)
 {
-#if DEBUG
-       verbose = 1;
-#endif
-       /* validate container capabilities */
-       if (super->orom && raiddisks > super->orom->tds) {
-               if (verbose)
-                       fprintf(stderr, Name ": %d exceeds maximum number of"
-                               " platform supported disks: %d\n",
-                               raiddisks, super->orom->tds);
+       /* 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",
+                      super->orom->dpa);
                return 0;
        }
 
         /* capabilities of OROM tested - copied from validate_geometry_imsm_volume */
-       if (super->orom && (!is_raid_level_supported(super->orom, level,
-                                                    raiddisks))) {
+       if (!is_raid_level_supported(super->orom, level, raiddisks)) {
                pr_vrb(": platform does not support raid%d with %d disk%s\n",
                        level, raiddisks, raiddisks > 1 ? "s" : "");
                return 0;
        }
-       if (super->orom && level != 1) {
-               if (chunk && (*chunk == 0 || *chunk == UnSet))
-                       *chunk = imsm_orom_default_chunk(super->orom);
-               else if (chunk && !imsm_orom_has_chunk(super->orom, *chunk)) {
-                       pr_vrb(": platform does not support a chunk size of: "
-                              "%d\n", *chunk);
-                       return 0;
-               }
+
+       if (chunk && (*chunk == 0 || *chunk == UnSet))
+               *chunk = imsm_default_chunk(super->orom);
+
+       if (super->orom && chunk && !imsm_orom_has_chunk(super->orom, *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");
@@ -5098,9 +5355,8 @@ static void default_geometry_imsm(struct supertype *st, int *level, int *layout,
        if (level && layout && *layout == UnSet)
                *layout = imsm_level_to_layout(*level);
 
-       if (chunk && (*chunk == UnSet || *chunk == 0) && 
-           super && super->orom)
-               *chunk = imsm_orom_default_chunk(super->orom);
+       if (chunk && (*chunk == UnSet || *chunk == 0))
+               *chunk = imsm_default_chunk(super->orom);
 }
 
 static void handle_missing(struct intel_super *super, struct imsm_dev *dev);
@@ -5233,6 +5489,9 @@ static int update_subarray_imsm(struct supertype *st, char *subarray,
 
 static int is_gen_migration(struct imsm_dev *dev)
 {
+       if (dev == NULL)
+               return 0;
+
        if (!dev->vol.migr_state)
                return 0;
 
@@ -5293,7 +5552,9 @@ static void update_recovery_start(struct intel_super *super,
        rebuild->recovery_start = units * blocks_per_migr_unit(super, dev);
 }
 
+#ifndef MDASSEMBLE
 static int recover_backup_imsm(struct supertype *st, struct mdinfo *info);
+#endif
 
 static struct mdinfo *container_content_imsm(struct supertype *st, char *subarray)
 {
@@ -5314,6 +5575,13 @@ static struct mdinfo *container_content_imsm(struct supertype *st, char *subarra
        struct dl *d;
        int spare_disks = 0;
 
+       /* do not assemble arrays when not all attributes are supported */
+       if (imsm_check_attributes(mpb->attributes) == 0) {
+               fprintf(stderr, Name ": IMSM metadata loading not allowed "
+                       "due to attributes incompatibility.\n");
+               return NULL;
+       }
+
        /* check for bad blocks */
        if (imsm_bbm_log_size(super->anchor))
                bbm_errors = 1;
@@ -5373,11 +5641,10 @@ static struct mdinfo *container_content_imsm(struct supertype *st, char *subarra
                                sizeof(*this));
                        break;
                }
-               memset(this, 0, sizeof(*this));
-               this->next = rest;
 
                super->current_vol = i;
                getinfo_super_imsm_volume(st, this, NULL);
+               this->next = rest;
                for (slot = 0 ; slot <  map->num_members; slot++) {
                        unsigned long long recovery_start;
                        struct mdinfo *info_d;
@@ -5458,10 +5725,11 @@ static struct mdinfo *container_content_imsm(struct supertype *st, char *subarra
                update_recovery_start(super, dev, this);
                this->array.spare_disks += spare_disks;
 
+#ifndef MDASSEMBLE
                /* check for reshape */
                if (this->reshape_active == 1)
                        recover_backup_imsm(st, this);
-
+#endif
                rest = this;
        }
 
@@ -5618,6 +5886,8 @@ static int mark_failure(struct imsm_dev *dev, struct imsm_disk *disk, int idx)
        __u32 ord;
        int slot;
        struct imsm_map *map;
+       char buf[MAX_RAID_SERIAL_LEN+3];
+       unsigned int len, shift = 0;
 
        /* new failures are always set in map[0] */
        map = get_imsm_map(dev, 0);
@@ -5630,6 +5900,11 @@ static int mark_failure(struct imsm_dev *dev, struct imsm_disk *disk, int idx)
        if (is_failed(disk) && (ord & IMSM_ORD_REBUILD))
                return 0;
 
+       sprintf(buf, "%s:0", disk->serial);
+       if ((len = strlen(buf)) >= MAX_RAID_SERIAL_LEN)
+               shift = len - MAX_RAID_SERIAL_LEN + 1;
+       strncpy((char *)disk->serial, &buf[shift], MAX_RAID_SERIAL_LEN);
+
        disk->status |= FAILED_DISK;
        set_imsm_ord_tbl_ent(map, slot, idx | IMSM_ORD_REBUILD);
        if (map->failed_disk_num == 0xff)
@@ -5777,14 +6052,18 @@ static int imsm_set_array_state(struct active_array *a, int consistent)
                } else {
                        if (a->last_checkpoint == 0 && a->prev_action == reshape) {
                                /* for some reason we aborted the reshape.
-                                * Better clean up
+                                *
+                                * disable automatic metadata rollback
+                                * user action is required to recover process
                                 */
+                               if (0) {
                                struct imsm_map *map2 = get_imsm_map(dev, 1);
                                dev->vol.migr_state = 0;
                                dev->vol.migr_type = 0;
                                dev->vol.curr_migr_unit = 0;
                                memcpy(map, map2, sizeof_imsm_map(map2));
                                super->updates_pending++;
+                               }
                        }
                        if (a->last_checkpoint >= a->info.component_size) {
                                unsigned long long array_blocks;
@@ -6793,7 +7072,7 @@ static int apply_takeover_update(struct imsm_update_takeover *u,
        for (du = super->missing; du; du = du->next)
                if (du->index >= 0) {
                        set_imsm_ord_tbl_ent(map, du->index, du->index);
-                       mark_missing(dev_new, &du->disk, du->index);
+                       mark_missing(dv->dev, &du->disk, du->index);
                }
 
        return 1;
@@ -6841,6 +7120,24 @@ static void imsm_process_update(struct supertype *st,
        mpb = super->anchor;
 
        switch (type) {
+       case update_general_migration_checkpoint: {
+               struct intel_dev *id;
+               struct imsm_update_general_migration_checkpoint *u =
+                                                       (void *)update->buf;
+
+               dprintf("imsm: process_update() "
+                       "for update_general_migration_checkpoint called\n");
+
+               /* find device under general migration */
+               for (id = super->devlist ; id; id = id->next) {
+                       if (is_gen_migration(id->dev)) {
+                               id->dev->vol.curr_migr_unit =
+                                       __cpu_to_le32(u->curr_migr_unit);
+                               super->updates_pending++;
+                       }
+               }
+               break;
+       }
        case update_takeover: {
                struct imsm_update_takeover *u = (void *)update->buf;
                if (apply_takeover_update(u, super, &update->space_list)) {
@@ -7178,6 +7475,10 @@ static void imsm_prepare_update(struct supertype *st,
        size_t len = 0;
 
        switch (type) {
+       case update_general_migration_checkpoint:
+               dprintf("imsm: prepare_update() "
+                       "for update_general_migration_checkpoint called\n");
+               break;
        case update_takeover: {
                struct imsm_update_takeover *u = (void *)update->buf;
                if (u->direction == R0_TO_R10) {
@@ -7462,7 +7763,7 @@ static void imsm_delete(struct intel_super *super, struct dl **dlp, unsigned ind
                __free_imsm_disk(dl);
        }
 }
-
+#endif /* MDASSEMBLE */
 /*******************************************************************************
  * Function:   open_backup_targets
  * Description:        Function opens file descriptors for all devices given in
@@ -7502,6 +7803,7 @@ int open_backup_targets(struct mdinfo *info, int raid_disks, int *raid_fds)
        return 0;
 }
 
+#ifndef MDASSEMBLE
 /*******************************************************************************
  * Function:   init_migr_record_imsm
  * Description:        Function inits imsm migration record
@@ -7526,10 +7828,7 @@ void init_migr_record_imsm(struct supertype *st, struct imsm_dev *dev,
        struct imsm_map *map_dest = get_imsm_map(dev, 0);
        struct imsm_map *map_src = get_imsm_map(dev, 1);
        unsigned long long num_migr_units;
-
-       unsigned long long array_blocks =
-               (((unsigned long long)__le32_to_cpu(dev->size_high)) << 32) +
-               __le32_to_cpu(dev->size_low);
+       unsigned long long array_blocks;
 
        memset(migr_rec, 0, sizeof(struct migr_record));
        migr_rec->family_num = __cpu_to_le32(super->anchor->family_num);
@@ -7545,7 +7844,7 @@ void init_migr_record_imsm(struct supertype *st, struct imsm_dev *dev,
                __cpu_to_le32(migr_rec->dest_depth_per_unit * new_data_disks);
        migr_rec->dest_depth_per_unit =
                __cpu_to_le32(migr_rec->dest_depth_per_unit);
-
+       array_blocks = info->component_size * new_data_disks;
        num_migr_units =
                array_blocks / __le32_to_cpu(migr_rec->blocks_per_unit);
 
@@ -7585,9 +7884,9 @@ void init_migr_record_imsm(struct supertype *st, struct imsm_dev *dev,
  *             and to write it to the Copy Area.
  * Parameters:
  *     st              : supertype information
+ *     dev             : imsm device that backup is saved for
  *     info            : general array info
  *     buf             : input buffer
- *     write_offset    : address of data to backup
  *     length          : length of data to backup (blocks_per_unit)
  * Returns:
  *      0 : success
@@ -7597,7 +7896,6 @@ int save_backup_imsm(struct supertype *st,
                     struct imsm_dev *dev,
                     struct mdinfo *info,
                     void *buf,
-                    int new_data,
                     int length)
 {
        int rv = -1;
@@ -7607,34 +7905,48 @@ int save_backup_imsm(struct supertype *st,
        int i;
        struct imsm_map *map_dest = get_imsm_map(dev, 0);
        int new_disks = map_dest->num_members;
+       int dest_layout = 0;
+       int dest_chunk;
+       unsigned long long start;
+       int data_disks = imsm_num_data_members(dev, 0);
 
        targets = malloc(new_disks * sizeof(int));
        if (!targets)
                goto abort;
 
+       for (i = 0; i < new_disks; i++)
+               targets[i] = -1;
+
        target_offsets = malloc(new_disks * sizeof(unsigned long long));
        if (!target_offsets)
                goto abort;
 
+       start = info->reshape_progress * 512;
        for (i = 0; i < new_disks; i++) {
-               targets[i] = -1;
                target_offsets[i] = (unsigned long long)
                  __le32_to_cpu(super->migr_rec->ckpt_area_pba) * 512;
+               /* move back copy area adderss, it will be moved forward
+                * in restore_stripes() using start input variable
+                */
+               target_offsets[i] -= start/data_disks;
        }
 
        if (open_backup_targets(info, new_disks, targets))
                goto abort;
 
+       dest_layout = imsm_level_to_layout(map_dest->raid_level);
+       dest_chunk = __le16_to_cpu(map_dest->blocks_per_strip) * 512;
+
        if (restore_stripes(targets, /* list of dest devices */
                            target_offsets, /* migration record offsets */
                            new_disks,
-                           info->new_chunk,
-                           info->new_level,
-                           info->new_layout,
-                           -1,         /* source backup file descriptor */
-                           0,          /* input buf offset
-                                        * always 0 buf is already offset */
-                           0,
+                           dest_chunk,
+                           map_dest->raid_level,
+                           dest_layout,
+                           -1,    /* source backup file descriptor */
+                           0,     /* input buf offset
+                                   * always 0 buf is already offseted */
+                           start,
                            length,
                            buf) != 0) {
                fprintf(stderr, Name ": Error restoring stripes\n");
@@ -7665,23 +7977,40 @@ abort:
  * Returns:
  *     0: success
  *     1: failure
+ *     2: failure, means no valid migration record
+ *                / no general migration in progress /
  ******************************************************************************/
 int save_checkpoint_imsm(struct supertype *st, struct mdinfo *info, int state)
 {
        struct intel_super *super = st->sb;
-       load_imsm_migr_rec(super, info);
-       if (__le32_to_cpu(super->migr_rec->blocks_per_unit) == 0) {
-               dprintf("ERROR: blocks_per_unit = 0!!!\n");
+       unsigned long long blocks_per_unit;
+       unsigned long long curr_migr_unit;
+
+       if (load_imsm_migr_rec(super, info) != 0) {
+               dprintf("imsm: ERROR: Cannot read migration record "
+                       "for checkpoint save.\n");
                return 1;
        }
 
+       blocks_per_unit = __le32_to_cpu(super->migr_rec->blocks_per_unit);
+       if (blocks_per_unit == 0) {
+               dprintf("imsm: no migration in progress.\n");
+               return 2;
+       }
+       curr_migr_unit = info->reshape_progress / blocks_per_unit;
+       /* check if array is alligned to copy area
+        * if it is not alligned, add one to current migration unit value
+        * this can happend on array reshape finish only
+        */
+       if (info->reshape_progress % blocks_per_unit)
+               curr_migr_unit++;
+
        super->migr_rec->curr_migr_unit =
-         __cpu_to_le32(info->reshape_progress /
-                       __le32_to_cpu(super->migr_rec->blocks_per_unit));
+               __cpu_to_le32(curr_migr_unit);
        super->migr_rec->rec_status = __cpu_to_le32(state);
        super->migr_rec->dest_1st_member_lba =
-         __cpu_to_le32((__le32_to_cpu(super->migr_rec->curr_migr_unit))
-                       * __le32_to_cpu(super->migr_rec->dest_depth_per_unit));
+               __cpu_to_le32(curr_migr_unit *
+                             __le32_to_cpu(super->migr_rec->dest_depth_per_unit));
        if (write_imsm_migr_rec(st) < 0) {
                dprintf("imsm: Cannot write migration record "
                        "outside backup area\n");
@@ -7691,9 +8020,6 @@ int save_checkpoint_imsm(struct supertype *st, struct mdinfo *info, int state)
        return 0;
 }
 
-static __u64 blocks_per_migr_unit(struct intel_super *super,
-                                 struct imsm_dev *dev);
-
 /*******************************************************************************
  * Function:   recover_backup_imsm
  * Description:        Function recovers critical data from the Migration Copy Area
@@ -7720,8 +8046,9 @@ int recover_backup_imsm(struct supertype *st, struct mdinfo *info)
        int retval = 1;
        unsigned long curr_migr_unit = __le32_to_cpu(migr_rec->curr_migr_unit);
        unsigned long num_migr_units = __le32_to_cpu(migr_rec->num_migr_units);
-       int ascending = __le32_to_cpu(migr_rec->ascending_migr);
        char buffer[20];
+       int skipped_disks = 0;
+       int max_degradation;
 
        err = sysfs_get_str(info, NULL, "array_state", (char *)buffer, 20);
        if (err < 1)
@@ -7745,13 +8072,14 @@ int recover_backup_imsm(struct supertype *st, struct mdinfo *info)
 
        map_dest = get_imsm_map(id->dev, 0);
        new_disks = map_dest->num_members;
+       max_degradation = new_disks - imsm_num_data_members(id->dev, 0);
 
        read_offset = (unsigned long long)
                        __le32_to_cpu(migr_rec->ckpt_area_pba) * 512;
 
        write_offset = ((unsigned long long)
                        __le32_to_cpu(migr_rec->dest_1st_member_lba) +
-                       info->data_offset) * 512;
+                       __le32_to_cpu(map_dest->pba_of_lba0)) * 512;
 
        unit_len = __le32_to_cpu(migr_rec->dest_depth_per_unit) * 512;
        if (posix_memalign((void **)&buf, 512, unit_len) != 0)
@@ -7763,13 +8091,17 @@ int recover_backup_imsm(struct supertype *st, struct mdinfo *info)
        open_backup_targets(info, new_disks, targets);
 
        for (i = 0; i < new_disks; i++) {
+               if (targets[i] < 0) {
+                       skipped_disks++;
+                       continue;
+               }
                if (lseek64(targets[i], read_offset, SEEK_SET) < 0) {
                        fprintf(stderr,
                                Name ": Cannot seek to block: %s\n",
                                strerror(errno));
                        goto abort;
                }
-               if (read(targets[i], buf, unit_len) != unit_len) {
+               if ((unsigned)read(targets[i], buf, unit_len) != unit_len) {
                        fprintf(stderr,
                                Name ": Cannot read copy area block: %s\n",
                                strerror(errno));
@@ -7781,7 +8113,7 @@ int recover_backup_imsm(struct supertype *st, struct mdinfo *info)
                                strerror(errno));
                        goto abort;
                }
-               if (write(targets[i], buf, unit_len) != unit_len) {
+               if ((unsigned)write(targets[i], buf, unit_len) != unit_len) {
                        fprintf(stderr,
                                Name ": Cannot restore block: %s\n",
                                strerror(errno));
@@ -7789,16 +8121,20 @@ int recover_backup_imsm(struct supertype *st, struct mdinfo *info)
                }
        }
 
-       if (ascending && curr_migr_unit < (num_migr_units-1))
-               curr_migr_unit++;
+       if (skipped_disks > max_degradation) {
+               fprintf(stderr,
+                       Name ": Cannot restore data from backup."
+                       " Too many failed disks\n");
+               goto abort;
+       }
 
-       migr_rec->curr_migr_unit = __le32_to_cpu(curr_migr_unit);
-       super->migr_rec->rec_status = __cpu_to_le32(UNIT_SRC_NORMAL);
-       if (write_imsm_migr_rec(st) == 0) {
-               __u64 blocks_per_unit = blocks_per_migr_unit(super, id->dev);
-               info->reshape_progress = curr_migr_unit * blocks_per_unit;
+       if (save_checkpoint_imsm(st, info, UNIT_SRC_NORMAL)) {
+               /* ignore error == 2, this can mean end of reshape here
+                */
+               dprintf("imsm: Cannot write checkpoint to "
+                       "migration record (UNIT_SRC_NORMAL) during restart\n");
+       } else
                retval = 0;
-       }
 
 abort:
        if (targets) {
@@ -8028,6 +8364,7 @@ static int imsm_create_metadata_update_for_reshape(
            || delta_disks > spares->array.spare_disks) {
                fprintf(stderr, Name ": imsm: ERROR: Cannot get spare devices "
                        "for %s.\n", geo->dev_name);
+               i = -1;
                goto abort;
        }
 
@@ -8179,7 +8516,6 @@ enum imsm_reshape_type imsm_analyze_change(struct supertype *st,
        int chunk;
 
        getinfo_super_imsm_volume(st, &info, NULL);
-
        if ((geo->level != info.array.level) &&
            (geo->level >= 0) &&
            (geo->level != UnSet)) {
@@ -8187,6 +8523,14 @@ enum imsm_reshape_type imsm_analyze_change(struct supertype *st,
                case 0:
                        if (geo->level == 5) {
                                change = CH_MIGRATION;
+                               if (geo->layout != ALGORITHM_LEFT_ASYMMETRIC) {
+                                       fprintf(stderr,
+                                       Name " Error. Requested Layout "
+                                       "not supported (left-asymmetric layout "
+                                       "is supported only)!\n");
+                                       change = -1;
+                                       goto analyse_change_exit;
+                               }
                                check_devs = 1;
                        }
                        if (geo->level == 10) {
@@ -8309,30 +8653,6 @@ int imsm_takeover(struct supertype *st, struct geo_params *geo)
        return 0;
 }
 
-static int warn_user_about_risk(void)
-{
-       int rv = 0;
-
-       fprintf(stderr,
-               "\nThis is an experimental feature. Data on the RAID volume(s) "
-               "can be lost!!!\n\n"
-               "To continue command execution please make sure that\n"
-               "the grow process will not be interrupted. Use safe power\n"
-               "supply to avoid unexpected system reboot. Make sure that\n"
-               "reshaped container is not assembled automatically during\n"
-               "system boot.\n"
-               "If reshape is interrupted, assemble array manually\n"
-               "using e.g. '-Ac' option and up to date mdadm.conf file.\n"
-               "Assembly in scan mode is not possible in such case.\n"
-               "Growing container with boot array is not possible.\n"
-               "If boot array reshape is interrupted, whole file system\n"
-               "can be lost.\n\n");
-       rv = ask("Do you want to continue? ");
-       fprintf(stderr, "\n");
-
-       return rv;
-}
-
 static int imsm_reshape_super(struct supertype *st, long long size, int level,
                              int layout, int chunksize, int raid_disks,
                              int delta_disks, char *backup, char *dev,
@@ -8366,13 +8686,6 @@ static int imsm_reshape_super(struct supertype *st, long long size, int level,
                dprintf("imsm: info: Container operation\n");
                int old_raid_disks = 0;
 
-               /* this warning will be removed when imsm checkpointing
-                * will be implemented, and restoring from check-point
-                * operation will be transparent for reboot process
-                */
-               if (warn_user_about_risk() == 0)
-                       return ret_val;
-
                if (imsm_reshape_is_allowed_on_container(
                            st, &geo, &old_raid_disks)) {
                        struct imsm_update_reshape *u = NULL;
@@ -8411,8 +8724,9 @@ static int imsm_reshape_super(struct supertype *st, long long size, int level,
                dprintf("imsm: info: Volume operation\n");
                /* find requested device */
                while (dev) {
-                       imsm_find_array_minor_by_subdev(dev->index, st->container_dev, &devnum);
-                       if (devnum == geo.dev_id)
+                       if (imsm_find_array_minor_by_subdev(
+                                   dev->index, st->container_dev, &devnum) == 0
+                           && devnum == geo.dev_id)
                                break;
                        dev = dev->next;
                }
@@ -8463,56 +8777,66 @@ exit_imsm_reshape_super:
  *             reshape process reach new position
  * Parameters:
  *     sra             : general array info
- *     to_complete     : new sync_max position
  *     ndata           : number of disks in new array's layout
  * Returns:
  *      0 : success,
  *      1 : there is no reshape in progress,
  *     -1 : fail
  ******************************************************************************/
-int wait_for_reshape_imsm(struct mdinfo *sra, unsigned long long to_complete,
-                         int ndata)
+int wait_for_reshape_imsm(struct mdinfo *sra, int ndata)
 {
        int fd = sysfs_get_fd(sra, NULL, "reshape_position");
        unsigned long long completed;
+       /* to_complete : new sync_max position */
+       unsigned long long to_complete = sra->reshape_progress;
+       unsigned long long position_to_set = to_complete / ndata;
 
-       struct timeval timeout;
-
-       if (fd < 0)
+       if (fd < 0) {
+               dprintf("imsm: wait_for_reshape_imsm() "
+                       "cannot open reshape_position\n");
                return 1;
+       }
 
-       sysfs_fd_get_ll(fd, &completed);
+       if (sysfs_fd_get_ll(fd, &completed) < 0) {
+               dprintf("imsm: wait_for_reshape_imsm() "
+                       "cannot read reshape_position (no reshape in progres)\n");
+               close(fd);
+               return 0;
+       }
 
-       if (to_complete == 0) {/* reshape till the end of array */
-               sysfs_set_str(sra, NULL, "sync_max", "max");
-               to_complete = MaxSector;
-       } else {
-               if (completed > to_complete)
-                       return -1;
-               if (sysfs_set_num(sra, NULL, "sync_max",
-                                 to_complete / ndata) != 0) {
-                       close(fd);
-                       return -1;
-               }
+       if (completed > to_complete) {
+               dprintf("imsm: wait_for_reshape_imsm() "
+                       "wrong next position to set %llu (%llu)\n",
+                       to_complete, completed);
+               close(fd);
+               return -1;
+       }
+       dprintf("Position set: %llu\n", position_to_set);
+       if (sysfs_set_num(sra, NULL, "sync_max",
+                         position_to_set) != 0) {
+               dprintf("imsm: wait_for_reshape_imsm() "
+                       "cannot set reshape position to %llu\n",
+                       position_to_set);
+               close(fd);
+               return -1;
        }
 
-       /* FIXME should not need a timeout at all */
-       timeout.tv_sec = 30;
-       timeout.tv_usec = 0;
        do {
                char action[20];
                fd_set rfds;
                FD_ZERO(&rfds);
                FD_SET(fd, &rfds);
-               select(fd+1, NULL, NULL, &rfds, &timeout);
+               select(fd+1, &rfds, NULL, NULL, NULL);
+               if (sysfs_get_str(sra, NULL, "sync_action",
+                                 action, 20) > 0 &&
+                               strncmp(action, "reshape", 7) != 0)
+                       break;
                if (sysfs_fd_get_ll(fd, &completed) < 0) {
+                       dprintf("imsm: wait_for_reshape_imsm() "
+                               "cannot read reshape_position (in loop)\n");
                        close(fd);
                        return 1;
                }
-               if (sysfs_get_str(sra, NULL, "sync_action",
-                                 action, 20) > 0 &&
-                   strncmp(action, "reshape", 7) != 0)
-                       break;
        } while (completed < to_complete);
        close(fd);
        return 0;
@@ -8596,7 +8920,7 @@ static int imsm_manage_reshape(
        struct intel_super *super = st->sb;
        struct intel_dev *dv = NULL;
        struct imsm_dev *dev = NULL;
-       struct imsm_map *map_src, *map_dest;
+       struct imsm_map *map_src;
        int migr_vol_qan = 0;
        int ndata, odata; /* [bytes] */
        int chunk; /* [bytes] */
@@ -8606,13 +8930,13 @@ static int imsm_manage_reshape(
        unsigned long long max_position; /* array size [bytes] */
        unsigned long long next_step; /* [blocks]/[bytes] */
        unsigned long long old_data_stripe_length;
-       unsigned long long new_data_stripe_length;
        unsigned long long start_src; /* [bytes] */
        unsigned long long start; /* [bytes] */
        unsigned long long start_buf_shift; /* [bytes] */
        int degraded = 0;
+       int source_layout = 0;
 
-       if (!fds || !offsets || !destfd || !destoffsets || !sra)
+       if (!fds || !offsets || !sra)
                goto abort;
 
        /* Find volume during the reshape */
@@ -8634,24 +8958,25 @@ static int imsm_manage_reshape(
        map_src = get_imsm_map(dev, 1);
        if (map_src == NULL)
                goto abort;
-       map_dest = get_imsm_map(dev, 0);
 
        ndata = imsm_num_data_members(dev, 0);
        odata = imsm_num_data_members(dev, 1);
 
-       chunk = map_src->blocks_per_strip * 512;
+       chunk = __le16_to_cpu(map_src->blocks_per_strip) * 512;
        old_data_stripe_length = odata * chunk;
 
        migr_rec = super->migr_rec;
 
-       /* [bytes] */
-       sra->new_chunk = __le16_to_cpu(map_dest->blocks_per_strip) * 512;
-       sra->new_level = map_dest->raid_level;
-       new_data_stripe_length = sra->new_chunk * ndata;
-
        /* initialize migration record for start condition */
        if (sra->reshape_progress == 0)
                init_migr_record_imsm(st, dev, sra);
+       else {
+               if (__le32_to_cpu(migr_rec->rec_status) != UNIT_SRC_NORMAL) {
+                       dprintf("imsm: cannot restart migration when data "
+                               "are present in copy area.\n");
+                       goto abort;
+               }
+       }
 
        /* size for data */
        buf_size = __le32_to_cpu(migr_rec->blocks_per_unit) * 512;
@@ -8664,10 +8989,8 @@ static int imsm_manage_reshape(
                goto abort;
        }
 
-       max_position =
-               __le32_to_cpu(migr_rec->post_migr_vol_cap) +
-               ((unsigned long long)__le32_to_cpu(
-                        migr_rec->post_migr_vol_cap_hi) << 32);
+       max_position = sra->component_size * ndata;
+       source_layout = imsm_level_to_layout(map_src->raid_level);
 
        while (__le32_to_cpu(migr_rec->curr_migr_unit) <
               __le32_to_cpu(migr_rec->num_migr_units)) {
@@ -8691,8 +9014,7 @@ static int imsm_manage_reshape(
                if ((current_position + next_step) > max_position)
                        next_step = max_position - current_position;
 
-               start = (map_src->pba_of_lba0 + dev->reserved_blocks +
-                        current_position) * 512;
+               start = current_position * 512;
 
                /* allign reading start to old geometry */
                start_buf_shift = start % old_data_stripe_length;
@@ -8725,8 +9047,8 @@ static int imsm_manage_reshape(
                                start_buf_shift, next_step_filler);
 
                        if (save_stripes(fds, offsets, map_src->num_members,
-                                        chunk, sra->array.level,
-                                        sra->array.layout, 0, NULL, start_src,
+                                        chunk, map_src->raid_level,
+                                        source_layout, 0, NULL, start_src,
                                         copy_length +
                                         next_step_filler + start_buf_shift,
                                         buf)) {
@@ -8738,8 +9060,7 @@ static int imsm_manage_reshape(
                         * in backup general migration area
                         */
                        if (save_backup_imsm(st, dev, sra,
-                                            buf + start_buf_shift,
-                                            ndata, copy_length)) {
+                               buf + start_buf_shift, copy_length)) {
                                dprintf("imsm: Cannot save stripes to "
                                        "target devices\n");
                                goto abort;
@@ -8750,28 +9071,32 @@ static int imsm_manage_reshape(
                                        "migration record (UNIT_SRC_IN_CP_AREA)\n");
                                goto abort;
                        }
-                       /* decrease backup_blocks */
-                       if (backup_blocks > (unsigned long)next_step)
-                               backup_blocks -= next_step;
-                       else
-                               backup_blocks = 0;
+               } else {
+                       /* set next step to use whole border area */
+                       border /= next_step;
+                       if (border > 1)
+                               next_step *= border;
                }
                /* When data backed up, checkpoint stored,
                 * kick the kernel to reshape unit of data
                 */
                next_step = next_step + sra->reshape_progress;
+               /* limit next step to array max position */
+               if (next_step > max_position)
+                       next_step = max_position;
                sysfs_set_num(sra, NULL, "suspend_lo", sra->reshape_progress);
                sysfs_set_num(sra, NULL, "suspend_hi", next_step);
+               sra->reshape_progress = next_step;
 
                /* wait until reshape finish */
-               if (wait_for_reshape_imsm(sra, next_step, ndata) < 0) {
+               if (wait_for_reshape_imsm(sra, ndata) < 0) {
                        dprintf("wait_for_reshape_imsm returned error!\n");
                        goto abort;
                }
 
-               sra->reshape_progress = next_step;
-
-               if (save_checkpoint_imsm(st, sra, UNIT_SRC_NORMAL)) {
+               if (save_checkpoint_imsm(st, sra, UNIT_SRC_NORMAL) == 1) {
+                       /* ignore error == 2, this can mean end of reshape here
+                        */
                        dprintf("imsm: Cannot write checkpoint to "
                                "migration record (UNIT_SRC_NORMAL)\n");
                        goto abort;
@@ -8809,6 +9134,7 @@ struct superswitch super_imsm = {
        .get_disk_controller_domain = imsm_get_disk_controller_domain,
        .reshape_super  = imsm_reshape_super,
        .manage_reshape = imsm_manage_reshape,
+       .recover_backup = recover_backup_imsm,
 #endif
        .match_home     = match_home_imsm,
        .uuid_from_super= uuid_from_super_imsm,
@@ -8828,7 +9154,6 @@ struct superswitch super_imsm = {
        .match_metadata_desc = match_metadata_desc_imsm,
        .container_content = container_content_imsm,
 
-       .recover_backup = recover_backup_imsm,
 
        .external       = 1,
        .name = "imsm",