]> git.ipfire.org Git - thirdparty/mdadm.git/blobdiff - super-intel.c
Fix memory leak
[thirdparty/mdadm.git] / super-intel.c
index e9d9e35c0e8222174d940a33dec4a26dfdf42484..3525dae6681dff82483ca5e2d9fb679f2cf93413 100644 (file)
 
 /* Define all supported attributes that have to be accepted by mdadm
  */
-#define MPB_ATTRIB_SUPPORTED           MPB_ATTRIB_CHECKSUM_VERIFY | \
+#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
+                                       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
+#define NUM_BLOCKS_DIRTY_STRIPE_REGION 2056
 #define SECT_PER_MB_SHIFT 11
 
 /* Disk configuration info. */
@@ -341,7 +345,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 */
@@ -824,13 +828,24 @@ static int count_memberships(struct dl *dl, struct intel_super *super)
        return memberships;
 }
 
+static __u32 imsm_min_reserved_sectors(struct intel_super *super);
+
 static struct extent *get_extents(struct intel_super *super, struct dl *dl)
 {
        /* find a list of used extents on the given physical device */
        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 = imsm_min_reserved_sectors(super);
+       else
+               reservation = MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS;
 
        rv = malloc(sizeof(struct extent) * (memberships + 1));
        if (!rv)
@@ -921,6 +936,51 @@ static int is_failed(struct imsm_disk *disk)
        return (disk->status & FAILED_DISK) == FAILED_DISK;
 }
 
+/* try to determine how much space is reserved for metadata from
+ * the last get_extents() entry on the smallest active disk,
+ * otherwise fallback to the default
+ */
+static __u32 imsm_min_reserved_sectors(struct intel_super *super)
+{
+       struct extent *e;
+       int i;
+       __u32 min_active, remainder;
+       __u32 rv = MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS;
+       struct dl *dl, *dl_min = NULL;
+
+       if (!super)
+               return rv;
+
+       min_active = 0;
+       for (dl = super->disks; dl; dl = dl->next) {
+               if (dl->index < 0)
+                       continue;
+               if (dl->disk.total_blocks < min_active || min_active == 0) {
+                       dl_min = dl;
+                       min_active = dl->disk.total_blocks;
+               }
+       }
+       if (!dl_min)
+               return rv;
+
+       /* find last lba used by subarrays on the smallest active disk */
+       e = get_extents(super, dl_min);
+       if (!e)
+               return rv;
+       for (i = 0; e[i].size; i++)
+               continue;
+
+       remainder = min_active - e[i].start;
+       free(e);
+
+       /* to give priority to recovery we should not require full
+          IMSM_RESERVED_SECTORS from the spare */
+       rv = MPB_SECTOR_CNT + NUM_BLOCKS_DIRTY_STRIPE_REGION;
+
+       /* if real reservation is smaller use that value */
+       return  (remainder < rv) ? remainder : rv;
+}
+
 /* Return minimum size of a spare that can be used in this array*/
 static unsigned long long min_acceptable_spare_size_imsm(struct supertype *st)
 {
@@ -947,8 +1007,10 @@ static unsigned long long min_acceptable_spare_size_imsm(struct supertype *st)
        if (i > 0)
                rv = e[i-1].start + e[i-1].size;
        free(e);
+
        /* add the amount of space needed for metadata */
-       rv = rv + MPB_SECTOR_CNT + IMSM_RESERVED_SECTORS;
+       rv = rv + imsm_min_reserved_sectors(super);
+
        return rv * 512;
 }
 
@@ -1056,18 +1118,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" : "");
@@ -1141,11 +1205,14 @@ void examine_migr_rec_imsm(struct intel_super *super)
 static int imsm_check_attributes(__u32 attributes)
 {
        int ret_val = 1;
-       __u32 not_supported = (MPB_ATTRIB_SUPPORTED)^0xffffffff;
+       __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", 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;
@@ -1248,7 +1315,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;
 
@@ -1273,28 +1340,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);
 }
@@ -1518,9 +1569,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);
@@ -2184,6 +2235,30 @@ static int write_imsm_migr_rec(struct supertype *st)
 }
 #endif /* MDASSEMBLE */
 
+/* spare/missing disks activations are not allowe when
+ * array/container performs reshape operation, because
+ * all arrays in container works on the same disks set
+ */
+int imsm_reshape_blocks_arrays_changes(struct intel_super *super)
+{
+       int rv = 0;
+       struct intel_dev *i_dev;
+       struct imsm_dev *dev;
+
+       /* check whole container
+        */
+       for (i_dev = super->devlist; i_dev; i_dev = i_dev->next) {
+               dev = i_dev->dev;
+               if (is_gen_migration(dev)) {
+                       /* No repair during any migration in container
+                        */
+                       rv = 1;
+                       break;
+               }
+       }
+       return rv;
+}
+
 static void getinfo_super_imsm_volume(struct supertype *st, struct mdinfo *info, char *dmap)
 {
        struct intel_super *super = st->sb;
@@ -2201,7 +2276,7 @@ static void getinfo_super_imsm_volume(struct supertype *st, struct mdinfo *info,
        if (prev_map)
                map_to_analyse = prev_map;
 
-       dl = super->disks;
+       dl = super->current_disk;
 
        info->container_member    = super->current_vol;
        info->array.raid_disks    = map->num_members;
@@ -2216,7 +2291,10 @@ static void getinfo_super_imsm_volume(struct supertype *st, struct mdinfo *info,
        info->custom_array_size   = __le32_to_cpu(dev->size_high);
        info->custom_array_size   <<= 32;
        info->custom_array_size   |= __le32_to_cpu(dev->size_low);
-       if (prev_map && map->map_state == prev_map->map_state) {
+       info->recovery_blocked = imsm_reshape_blocks_arrays_changes(st->sb);
+
+       if (prev_map && map->map_state == prev_map->map_state &&
+           (migr_type(dev) == MIGR_GEN_MIGR)) {
                info->reshape_active = 1;
                info->new_level = get_imsm_raid_level(map);
                info->new_layout = imsm_level_to_layout(info->new_level);
@@ -2226,7 +2304,7 @@ static void getinfo_super_imsm_volume(struct supertype *st, struct mdinfo *info,
                        /* this needs to be applied to every array
                         * in the container.
                         */
-                       info->reshape_active = 2;
+                       info->reshape_active = CONTAINER_RESHAPE;
                }
                /* We shape information that we give to md might have to be
                 * modify to cope with md's requirement for reshaping arrays.
@@ -2263,11 +2341,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);
@@ -2293,8 +2373,9 @@ static void getinfo_super_imsm_volume(struct supertype *st, struct mdinfo *info,
 
        info->reshape_progress = 0;
        info->resync_start = MaxSector;
-       if (map_to_analyse->map_state == IMSM_T_STATE_UNINITIALIZED ||
-           dev->vol.dirty) {
+       if ((map_to_analyse->map_state == IMSM_T_STATE_UNINITIALIZED ||
+           dev->vol.dirty) &&
+           imsm_reshape_blocks_arrays_changes(super) == 0) {
                info->resync_start = 0;
        }
        if (dev->vol.migr_state) {
@@ -2326,7 +2407,9 @@ static void getinfo_super_imsm_volume(struct supertype *st, struct mdinfo *info,
 
                        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) {
@@ -2433,6 +2516,7 @@ static void getinfo_super_imsm(struct supertype *st, struct mdinfo *info, char *
        info->disk.state = 0;
        info->name[0] = 0;
        info->recovery_start = MaxSector;
+       info->recovery_blocked = imsm_reshape_blocks_arrays_changes(st->sb);
 
        /* do we have the all the insync disks that we expect? */
        mpb = super->anchor;
@@ -2778,7 +2862,7 @@ static void fd2devname(int fd, char *name)
        sprintf(path, "/sys/dev/block/%d:%d",
                major(st.st_rdev), minor(st.st_rdev));
 
-       rv = readlink(path, dname, sizeof(dname));
+       rv = readlink(path, dname, sizeof(dname)-1);
        if (rv <= 0)
                return;
        
@@ -2870,7 +2954,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;
@@ -2881,7 +2964,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)
@@ -3028,7 +3110,7 @@ static void end_migration(struct imsm_dev *dev, __u8 map_state)
                        }
 
        dev->vol.migr_state = 0;
-       dev->vol.migr_type = 0;
+       set_migr_type(dev, 0);
        dev->vol.curr_migr_unit = 0;
        map->map_state = map_state;
 }
@@ -3439,7 +3521,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.
@@ -3475,6 +3556,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;
@@ -4128,12 +4210,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;
@@ -4165,15 +4275,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) {
@@ -4281,9 +4390,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);
@@ -4327,15 +4437,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;
@@ -4347,10 +4482,41 @@ 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;
 }
 
+/* mark_spare()
+ *   Function marks disk as spare and restores disk serial
+ *   in case it was previously marked as failed by takeover operation
+ * reruns:
+ *   -1 : critical error
+ *    0 : disk is marked as spare but serial is not set
+ *    1 : success
+ */
+int mark_spare(struct dl *disk)
+{
+       __u8 serial[MAX_RAID_SERIAL_LEN];
+       int ret_val = -1;
+
+       if (!disk)
+               return ret_val;
+
+       ret_val = 0;
+       if (!imsm_read_serial(disk->fd, NULL, serial)) {
+               /* Restore disk serial number, because takeover marks disk
+                * as failed and adds to serial ':0' before it becomes
+                * a spare disk.
+                */
+               serialcpy(disk->serial, serial);
+               serialcpy(disk->disk.serial, serial);
+               ret_val = 1;
+       }
+       disk->disk.status = SPARE_DISK;
+       disk->index = -1;
+
+       return ret_val;
+}
 
 static int add_to_super_imsm(struct supertype *st, mdu_disk_info_t *dk,
                             int fd, char *devname)
@@ -4388,7 +4554,6 @@ static int add_to_super_imsm(struct supertype *st, mdu_disk_info_t *dk,
        memset(dd, 0, sizeof(*dd));
        dd->major = major(stb.st_rdev);
        dd->minor = minor(stb.st_rdev);
-       dd->index = -1;
        dd->devname = devname ? strdup(devname) : NULL;
        dd->fd = fd;
        dd->e = NULL;
@@ -4405,7 +4570,7 @@ static int add_to_super_imsm(struct supertype *st, mdu_disk_info_t *dk,
        size /= 512;
        serialcpy(dd->disk.serial, dd->serial);
        dd->disk.total_blocks = __cpu_to_le32(size);
-       dd->disk.status = SPARE_DISK;
+       mark_spare(dd);
        if (sysfs_disk_to_scsi_id(fd, &id) == 0)
                dd->disk.scsi_id = __cpu_to_le32(id);
        else
@@ -4448,9 +4613,8 @@ static int remove_from_super_imsm(struct supertype *st, mdu_disk_info_t *dk)
        memset(dd, 0, sizeof(*dd));
        dd->major = dk->major;
        dd->minor = dk->minor;
-       dd->index = -1;
        dd->fd = -1;
-       dd->disk.status = SPARE_DISK;
+       mark_spare(dd);
        dd->action = DISK_REMOVE;
 
        dd->next = super->disk_mgmt_list;
@@ -4574,7 +4738,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",
@@ -4912,43 +5076,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");
@@ -4984,6 +5149,12 @@ static int validate_geometry_imsm_volume(struct supertype *st, int level,
        if (!super)
                return 0;
 
+       if (mpb->num_raid_devs > 0 && mpb->num_disks != raiddisks) {
+               fprintf(stderr, Name ": the option-rom requires all "
+                       "member disks to be a member of all volumes.\n");
+               return 0;
+       }
+
        if (!validate_geometry_imsm_orom(super, level, layout, raiddisks, chunk, verbose)) {
                fprintf(stderr, Name ": RAID gemetry validation failed. "
                        "Cannot proceed with the action(s).\n");
@@ -5277,7 +5448,8 @@ static int validate_geometry_imsm(struct supertype *st, int level, int layout,
                        return validate_geometry_imsm_volume(st, level, layout,
                                                             raiddisks, chunk,
                                                             size, dev,
-                                                            freesize, verbose);
+                                                            freesize, 1)
+                               ? 1 : -1;
                }
        }
 
@@ -5298,9 +5470,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);
@@ -5368,10 +5539,8 @@ static int kill_subarray_imsm(struct supertype *st)
                struct dl *d;
 
                for (d = super->disks; d; d = d->next)
-                       if (d->index > -2) {
-                               d->index = -1;
-                               d->disk.status = SPARE_DISK;
-                       }
+                       if (d->index > -2)
+                               mark_spare(d);
        }
 
        super->updates_pending++;
@@ -5515,20 +5684,24 @@ static struct mdinfo *container_content_imsm(struct supertype *st, char *subarra
        struct imsm_super *mpb = super->anchor;
        struct mdinfo *rest = NULL;
        unsigned int i;
-       int bbm_errors = 0;
+       int sb_errors = 0;
        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;
+               sb_errors = 1;
+               fprintf(stderr, Name ": Unsupported attributes in IMSM metadata."
+                       "Arrays activation is blocked.\n");
        }
 
        /* check for bad blocks */
-       if (imsm_bbm_log_size(super->anchor))
-               bbm_errors = 1;
+       if (imsm_bbm_log_size(super->anchor)) {
+               fprintf(stderr, Name ": BBM log found in IMSM metadata."
+                       "Arrays activation is blocked.\n");
+               sb_errors = 1;
+       }
+
 
        /* count spare devices, not used in maps
         */
@@ -5567,18 +5740,6 @@ static struct mdinfo *container_content_imsm(struct supertype *st, char *subarra
                 */
 
                chunk = __le16_to_cpu(map->blocks_per_strip) >> 1;
-#ifndef MDASSEMBLE
-               if (!validate_geometry_imsm_orom(super,
-                                                get_imsm_raid_level(map), /* RAID level */
-                                                imsm_level_to_layout(get_imsm_raid_level(map)),
-                                                map->num_members, /* raid disks */
-                                                &chunk,
-                                                1 /* verbose */)) {
-                       fprintf(stderr, Name ": RAID gemetry validation failed. "
-                               "Cannot proceed with the action(s).\n");
-                       continue;
-               }
-#endif /* MDASSEMBLE */
                this = malloc(sizeof(*this));
                if (!this) {
                        fprintf(stderr, Name ": failed to allocate %zu bytes\n",
@@ -5589,6 +5750,29 @@ static struct mdinfo *container_content_imsm(struct supertype *st, char *subarra
                super->current_vol = i;
                getinfo_super_imsm_volume(st, this, NULL);
                this->next = rest;
+#ifndef MDASSEMBLE
+               /* mdadm does not support all metadata features- set the bit in all arrays state */
+               if (!validate_geometry_imsm_orom(super,
+                                                get_imsm_raid_level(map), /* RAID level */
+                                                imsm_level_to_layout(get_imsm_raid_level(map)),
+                                                map->num_members, /* raid disks */
+                                                &chunk,
+                                                1 /* verbose */)) {
+                       fprintf(stderr, Name ": IMSM RAID gemetry validation failed. "
+                               "Array %s activation is blocked.\n",
+                               dev->volume);
+                       this->array.state |=
+                         (1<<MD_SB_BLOCK_CONTAINER_RESHAPE) |
+                         (1<<MD_SB_BLOCK_VOLUME);
+               }
+#endif
+
+               /* if array has bad blocks, set suitable bit in all arrays state */
+               if (sb_errors)
+                       this->array.state |=
+                         (1<<MD_SB_BLOCK_CONTAINER_RESHAPE) |
+                         (1<<MD_SB_BLOCK_VOLUME);
+
                for (slot = 0 ; slot <  map->num_members; slot++) {
                        unsigned long long recovery_start;
                        struct mdinfo *info_d;
@@ -5677,10 +5861,6 @@ static struct mdinfo *container_content_imsm(struct supertype *st, char *subarra
                rest = this;
        }
 
-       /* if array has bad blocks, set suitable bit in array status */
-       if (bbm_errors)
-               rest->array.state |= (1<<MD_SB_BBM_ERRORS);
-
        return rest;
 }
 
@@ -5830,6 +6010,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);
@@ -5842,6 +6024,13 @@ static int mark_failure(struct imsm_dev *dev, struct imsm_disk *disk, int idx)
        if (is_failed(disk) && (ord & IMSM_ORD_REBUILD))
                return 0;
 
+       memcpy(buf, disk->serial, MAX_RAID_SERIAL_LEN);
+       buf[MAX_RAID_SERIAL_LEN] = '\000';
+       strcat(buf, ":0");
+       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)
@@ -5946,7 +6135,7 @@ static void imsm_progress_container_reshape(struct intel_super *super)
                map->num_members = prev_disks;
                dev->vol.migr_state = 1;
                dev->vol.curr_migr_unit = 0;
-               dev->vol.migr_type = MIGR_GEN_MIGR;
+               set_migr_type(dev, MIGR_GEN_MIGR);
                for (i = prev_num_members;
                     i < map->num_members; i++)
                        set_imsm_ord_tbl_ent(map, i, i);
@@ -5996,7 +6185,7 @@ static int imsm_set_array_state(struct active_array *a, int consistent)
                                if (0) {
                                struct imsm_map *map2 = get_imsm_map(dev, 1);
                                dev->vol.migr_state = 0;
-                               dev->vol.migr_type = 0;
+                               set_migr_type(dev, 0);
                                dev->vol.curr_migr_unit = 0;
                                memcpy(map, map2, sizeof_imsm_map(map2));
                                super->updates_pending++;
@@ -6056,7 +6245,8 @@ static int imsm_set_array_state(struct active_array *a, int consistent)
                        super->updates_pending++;
                        a->last_checkpoint = 0;
                }
-       } else if (!is_resyncing(dev) && !failed) {
+       } else if ((!is_resyncing(dev) && !failed) &&
+                  (imsm_reshape_blocks_arrays_changes(super) == 0)) {
                /* mark the start of the init process if nothing is failed */
                dprintf("imsm: mark resync start\n");
                if (map->map_state == IMSM_T_STATE_UNINITIALIZED)
@@ -6443,10 +6633,8 @@ static struct mdinfo *imsm_activate_spare(struct active_array *a,
        dprintf("imsm: activate spare: inst=%d failed=%d (%d) level=%d\n",
                inst, failed, a->info.array.raid_disks, a->info.array.level);
 
-       if (dev->vol.migr_state &&
-           dev->vol.migr_type == MIGR_GEN_MIGR)
-               /* No repair during migration */
-               return NULL;
+       if (imsm_reshape_blocks_arrays_changes(super))
+                       return NULL;
 
        if (a->info.array.level == 4)
                /* No repair for takeovered array
@@ -6463,9 +6651,9 @@ static struct mdinfo *imsm_activate_spare(struct active_array *a,
         * are removed from container.
         */
        if (failed) {
-               dprintf("found failed disks in %s, check if there another"
+               dprintf("found failed disks in %.*s, check if there another"
                        "failed sub-array.\n",
-                       dev->volume);
+                       MAX_RAID_SERIAL_LEN, dev->volume);
                /* check if states of the other volumes allow for rebuild */
                for (i = 0; i <  super->anchor->num_raid_devs; i++) {
                        if (i != inst) {
@@ -6495,9 +6683,9 @@ static struct mdinfo *imsm_activate_spare(struct active_array *a,
                 */
                dl = imsm_readd(super, i, a);
                if (!dl)
-                       dl = imsm_add_spare(super, i, a, 0, NULL);
+                       dl = imsm_add_spare(super, i, a, 0, rv);
                if (!dl)
-                       dl = imsm_add_spare(super, i, a, 1, NULL);
+                       dl = imsm_add_spare(super, i, a, 1, rv);
                if (!dl)
                        continue;
  
@@ -6534,8 +6722,6 @@ static struct mdinfo *imsm_activate_spare(struct active_array *a,
                num_spares++;
                dprintf("%x:%x to be %d at %llu\n", dl->major, dl->minor,
                        i, di->data_offset);
-
-               break;
        }
 
        if (!rv)
@@ -6805,6 +6991,120 @@ error_disk_add:
        return ret_val;
 }
 
+static int apply_update_activate_spare(struct imsm_update_activate_spare *u,
+                                      struct intel_super *super,       
+                                      struct active_array *active_array)
+{
+       struct imsm_super *mpb = super->anchor;
+       struct imsm_dev *dev = get_imsm_dev(super, u->array);
+       struct imsm_map *map = get_imsm_map(dev, 0);
+       struct imsm_map *migr_map;
+       struct active_array *a;
+       struct imsm_disk *disk;
+       __u8 to_state;
+       struct dl *dl;
+       unsigned int found;
+       int failed;
+       int victim;
+       int i;
+       int second_map_created = 0;
+
+       for (; u; u = u->next) {
+               victim = get_imsm_disk_idx(dev, u->slot, -1);
+
+               if (victim < 0)
+                       return 0;
+
+               for (dl = super->disks; dl; dl = dl->next)
+                       if (dl == u->dl)
+                               break;
+
+               if (!dl) {
+                       fprintf(stderr, "error: imsm_activate_spare passed "
+                               "an unknown disk (index: %d)\n",
+                               u->dl->index);
+                       return 0;
+               }
+
+               /* count failures (excluding rebuilds and the victim)
+                * to determine map[0] state
+                */
+               failed = 0;
+               for (i = 0; i < map->num_members; i++) {
+                       if (i == u->slot)
+                               continue;
+                       disk = get_imsm_disk(super,
+                                            get_imsm_disk_idx(dev, i, -1));
+                       if (!disk || is_failed(disk))
+                               failed++;
+               }
+
+               /* adding a pristine spare, assign a new index */
+               if (dl->index < 0) {
+                       dl->index = super->anchor->num_disks;
+                       super->anchor->num_disks++;
+               }
+               disk = &dl->disk;
+               disk->status |= CONFIGURED_DISK;
+               disk->status &= ~SPARE_DISK;
+
+               /* mark rebuild */
+               to_state = imsm_check_degraded(super, dev, failed);
+               if (!second_map_created) {
+                       second_map_created = 1;
+                       map->map_state = IMSM_T_STATE_DEGRADED;
+                       migrate(dev, super, to_state, MIGR_REBUILD);
+               } else
+                       map->map_state = to_state;
+               migr_map = get_imsm_map(dev, 1);
+               set_imsm_ord_tbl_ent(map, u->slot, dl->index);
+               set_imsm_ord_tbl_ent(migr_map, u->slot,
+                                    dl->index | IMSM_ORD_REBUILD);
+
+               /* update the family_num to mark a new container
+                * generation, being careful to record the existing
+                * family_num in orig_family_num to clean up after
+                * earlier mdadm versions that neglected to set it.
+                */
+               if (mpb->orig_family_num == 0)
+                       mpb->orig_family_num = mpb->family_num;
+               mpb->family_num += super->random;
+
+               /* count arrays using the victim in the metadata */
+               found = 0;
+               for (a = active_array; a ; a = a->next) {
+                       dev = get_imsm_dev(super, a->info.container_member);
+                       map = get_imsm_map(dev, 0);
+
+                       if (get_imsm_disk_slot(map, victim) >= 0)
+                               found++;
+               }
+
+               /* delete the victim if it is no longer being
+                * utilized anywhere
+                */
+               if (!found) {
+                       struct dl **dlp;
+
+                       /* We know that 'manager' isn't touching anything,
+                        * so it is safe to delete
+                        */
+                       for (dlp = &super->disks; *dlp; dlp = &(*dlp)->next)
+                               if ((*dlp)->index == victim)
+                                       break;
+
+                       /* victim may be on the missing list */
+                       if (!*dlp)
+                               for (dlp = &super->missing; *dlp;
+                                    dlp = &(*dlp)->next)
+                                       if ((*dlp)->index == victim)
+                                               break;
+                       imsm_delete(super, dlp, victim);
+               }
+       }
+
+       return 1;
+}
 
 static int apply_reshape_container_disks_update(struct imsm_update_reshape *u,
                                                struct intel_super *super,
@@ -6878,7 +7178,7 @@ static int apply_reshape_container_disks_update(struct imsm_update_reshape *u,
                        devices_to_reshape--;
                        newdev->vol.migr_state = 1;
                        newdev->vol.curr_migr_unit = 0;
-                       newdev->vol.migr_type = MIGR_GEN_MIGR;
+                       set_migr_type(newdev, MIGR_GEN_MIGR);
                        newmap->num_members = u->new_raid_disks;
                        for (i = 0; i < delta_disks; i++) {
                                set_imsm_ord_tbl_ent(newmap,
@@ -6948,8 +7248,7 @@ static int apply_takeover_update(struct imsm_update_takeover *u,
                                        if (du->index > idx)
                                                du->index--;
                                /* mark as spare disk */
-                               dm->disk.status = SPARE_DISK;
-                               dm->index = -1;
+                               mark_spare(dm);
                        }
                }
                /* update map */
@@ -7009,7 +7308,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;
@@ -7100,99 +7399,8 @@ static void imsm_process_update(struct supertype *st,
        }
        case update_activate_spare: {
                struct imsm_update_activate_spare *u = (void *) update->buf; 
-               struct imsm_dev *dev = get_imsm_dev(super, u->array);
-               struct imsm_map *map = get_imsm_map(dev, 0);
-               struct imsm_map *migr_map;
-               struct active_array *a;
-               struct imsm_disk *disk;
-               __u8 to_state;
-               struct dl *dl;
-               unsigned int found;
-               int failed;
-               int victim = get_imsm_disk_idx(dev, u->slot, -1);
-               int i;
-
-               for (dl = super->disks; dl; dl = dl->next)
-                       if (dl == u->dl)
-                               break;
-
-               if (!dl) {
-                       fprintf(stderr, "error: imsm_activate_spare passed "
-                               "an unknown disk (index: %d)\n",
-                               u->dl->index);
-                       return;
-               }
-
-               super->updates_pending++;
-               /* count failures (excluding rebuilds and the victim)
-                * to determine map[0] state
-                */
-               failed = 0;
-               for (i = 0; i < map->num_members; i++) {
-                       if (i == u->slot)
-                               continue;
-                       disk = get_imsm_disk(super,
-                                            get_imsm_disk_idx(dev, i, -1));
-                       if (!disk || is_failed(disk))
-                               failed++;
-               }
-
-               /* adding a pristine spare, assign a new index */
-               if (dl->index < 0) {
-                       dl->index = super->anchor->num_disks;
-                       super->anchor->num_disks++;
-               }
-               disk = &dl->disk;
-               disk->status |= CONFIGURED_DISK;
-               disk->status &= ~SPARE_DISK;
-
-               /* mark rebuild */
-               to_state = imsm_check_degraded(super, dev, failed);
-               map->map_state = IMSM_T_STATE_DEGRADED;
-               migrate(dev, super, to_state, MIGR_REBUILD);
-               migr_map = get_imsm_map(dev, 1);
-               set_imsm_ord_tbl_ent(map, u->slot, dl->index);
-               set_imsm_ord_tbl_ent(migr_map, u->slot, dl->index | IMSM_ORD_REBUILD);
-
-               /* update the family_num to mark a new container
-                * generation, being careful to record the existing
-                * family_num in orig_family_num to clean up after
-                * earlier mdadm versions that neglected to set it.
-                */
-               if (mpb->orig_family_num == 0)
-                       mpb->orig_family_num = mpb->family_num;
-               mpb->family_num += super->random;
-
-               /* count arrays using the victim in the metadata */
-               found = 0;
-               for (a = st->arrays; a ; a = a->next) {
-                       dev = get_imsm_dev(super, a->info.container_member);
-                       map = get_imsm_map(dev, 0);
-
-                       if (get_imsm_disk_slot(map, victim) >= 0)
-                               found++;
-               }
-
-               /* delete the victim if it is no longer being
-                * utilized anywhere
-                */
-               if (!found) {
-                       struct dl **dlp;
-
-                       /* We know that 'manager' isn't touching anything,
-                        * so it is safe to delete
-                        */
-                       for (dlp = &super->disks; *dlp; dlp = &(*dlp)->next)
-                               if ((*dlp)->index == victim)
-                                       break;
-
-                       /* victim may be on the missing list */
-                       if (!*dlp)
-                               for (dlp = &super->missing; *dlp; dlp = &(*dlp)->next)
-                                       if ((*dlp)->index == victim)
-                                               break;
-                       imsm_delete(super, dlp, victim);
-               }
+               if (apply_update_activate_spare(u, super, st->arrays))
+                       super->updates_pending++;
                break;
        }
        case update_create_array: {
@@ -8301,6 +8509,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;
        }
 
@@ -8660,8 +8869,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;
                }