]> git.ipfire.org Git - thirdparty/mdadm.git/blobdiff - super-intel.c
Add raid10 -> raid0 takeover support
[thirdparty/mdadm.git] / super-intel.c
index eca6e3f7b2c8c952232b2e12cbb9556baa142693..0178f289f51f942a980fa125ec688fba8f9c8f4e 100644 (file)
@@ -233,6 +233,10 @@ struct intel_dev {
        unsigned index;
 };
 
+enum action {
+       DISK_REMOVE = 1,
+       DISK_ADD
+};
 /* internal representation of IMSM metadata */
 struct intel_super {
        union {
@@ -258,8 +262,10 @@ struct intel_super {
                int extent_cnt;
                struct extent *e; /* for determining freespace @ create */
                int raiddisk; /* slot to fill in autolayout */
+               enum action action;
        } *disks;
-       struct dl *add; /* list of disks to add while mdmon active */
+       struct dl *disk_mgmt_list; /* list of disks to add/remove while mdmon
+                                     active */
        struct dl *missing; /* disks removed while we weren't looking */
        struct bbm_log *bbm_log;
        const char *hba; /* device path of the raid controller for this metadata */
@@ -278,13 +284,22 @@ struct extent {
        unsigned long long start, size;
 };
 
+/* definitions of reshape process types */
+enum imsm_reshape_type {
+       CH_TAKEOVER,
+       CH_CHUNK_MIGR,
+       CH_LEVEL_MIGRATION
+};
+
 /* definition of messages passed to imsm_process_update */
 enum imsm_update_type {
        update_activate_spare,
        update_create_array,
        update_kill_array,
        update_rename_array,
-       update_add_disk,
+       update_add_remove_disk,
+       update_reshape_container_disks,
+       update_takeover
 };
 
 struct imsm_update_activate_spare {
@@ -295,6 +310,33 @@ struct imsm_update_activate_spare {
        struct imsm_update_activate_spare *next;
 };
 
+struct geo_params {
+       int dev_id;
+       char *dev_name;
+       long long size;
+       int level;
+       int layout;
+       int chunksize;
+       int raid_disks;
+};
+
+enum takeover_direction {
+       R10_TO_R0,
+       R0_TO_R10
+};
+struct imsm_update_takeover {
+       enum imsm_update_type type;
+       int subarray;
+       enum takeover_direction direction;
+};
+
+struct imsm_update_reshape {
+       enum imsm_update_type type;
+       int old_raid_disks;
+       int new_raid_disks;
+       int new_disks[1]; /* new_raid_disks - old_raid_disks makedev number */
+};
+
 struct disk_info {
        __u8 serial[MAX_RAID_SERIAL_LEN];
 };
@@ -316,7 +358,7 @@ struct imsm_update_rename_array {
        int dev_idx;
 };
 
-struct imsm_update_add_disk {
+struct imsm_update_add_remove_disk {
        enum imsm_update_type type;
 };
 
@@ -358,15 +400,28 @@ static struct imsm_disk *__get_imsm_disk(struct imsm_super *mpb, __u8 index)
        return &mpb->disk[index];
 }
 
-/* retrieve a disk from the parsed metadata */
-static struct imsm_disk *get_imsm_disk(struct intel_super *super, __u8 index)
+/* retrieve the disk description based on a index of the disk
+ * in the sub-array
+ */
+static struct dl *get_imsm_dl_disk(struct intel_super *super, __u8 index)
 {
        struct dl *d;
 
        for (d = super->disks; d; d = d->next)
                if (d->index == index)
-                       return &d->disk;
-       
+                       return d;
+
+       return NULL;
+}
+/* retrieve a disk from the parsed metadata */
+static struct imsm_disk *get_imsm_disk(struct intel_super *super, __u8 index)
+{
+       struct dl *dl;
+
+       dl = get_imsm_dl_disk(super, index);
+       if (dl)
+               return &dl->disk;
+
        return NULL;
 }
 
@@ -471,23 +526,35 @@ static struct imsm_dev *get_imsm_dev(struct intel_super *super, __u8 index)
        return NULL;
 }
 
-static __u32 get_imsm_ord_tbl_ent(struct imsm_dev *dev, int slot)
+/*
+ * for second_map:
+ *  == 0 get first map
+ *  == 1 get second map
+ *  == -1 than get map according to the current migr_state
+ */
+static __u32 get_imsm_ord_tbl_ent(struct imsm_dev *dev,
+                                 int slot,
+                                 int second_map)
 {
        struct imsm_map *map;
 
-       if (dev->vol.migr_state)
-               map = get_imsm_map(dev, 1);
-       else
-               map = get_imsm_map(dev, 0);
+       if (second_map == -1) {
+               if (dev->vol.migr_state)
+                       map = get_imsm_map(dev, 1);
+               else
+                       map = get_imsm_map(dev, 0);
+       } else {
+               map = get_imsm_map(dev, second_map);
+       }
 
        /* top byte identifies disk under rebuild */
        return __le32_to_cpu(map->disk_ord_tbl[slot]);
 }
 
 #define ord_to_idx(ord) (((ord) << 8) >> 8)
-static __u32 get_imsm_disk_idx(struct imsm_dev *dev, int slot)
+static __u32 get_imsm_disk_idx(struct imsm_dev *dev, int slot, int second_map)
 {
-       __u32 ord = get_imsm_ord_tbl_ent(dev, slot);
+       __u32 ord = get_imsm_ord_tbl_ent(dev, slot, second_map);
 
        return ord_to_idx(ord);
 }
@@ -686,22 +753,38 @@ static void print_imsm_dev(struct imsm_dev *dev, char *uuid, int disk_idx)
        __u64 sz;
        int slot, i;
        struct imsm_map *map = get_imsm_map(dev, 0);
+       struct imsm_map *map2 = get_imsm_map(dev, 1);
        __u32 ord;
 
        printf("\n");
        printf("[%.16s]:\n", dev->volume);
        printf("           UUID : %s\n", uuid);
-       printf("     RAID Level : %d\n", get_imsm_raid_level(map));
-       printf("        Members : %d\n", map->num_members);
+       printf("     RAID Level : %d", get_imsm_raid_level(map));
+       if (map2)
+               printf(" <-- %d", get_imsm_raid_level(map2));
+       printf("\n");
+       printf("        Members : %d", map->num_members);
+       if (map2)
+               printf(" <-- %d", map2->num_members);
+       printf("\n");
        printf("          Slots : [");
        for (i = 0; i < map->num_members; i++) {
-               ord = get_imsm_ord_tbl_ent(dev, i);
+               ord = get_imsm_ord_tbl_ent(dev, i, 0);
                printf("%s", ord & IMSM_ORD_REBUILD ? "_" : "U");
        }
-       printf("]\n");
+       printf("]");
+       if (map2) {
+               printf(" <-- [");
+               for (i = 0; i < map2->num_members; i++) {
+                       ord = get_imsm_ord_tbl_ent(dev, i, 1);
+                       printf("%s", ord & IMSM_ORD_REBUILD ? "_" : "U");
+               }
+               printf("]");
+       }
+       printf("\n");
        slot = get_imsm_disk_slot(map, disk_idx);
        if (slot >= 0) {
-               ord = get_imsm_ord_tbl_ent(dev, slot);
+               ord = get_imsm_ord_tbl_ent(dev, slot, -1);
                printf("      This Slot : %d%s\n", slot,
                       ord & IMSM_ORD_REBUILD ? " (out-of-sync)" : "");
        } else
@@ -718,8 +801,12 @@ static void print_imsm_dev(struct imsm_dev *dev, char *uuid, int disk_idx)
                __le32_to_cpu(map->pba_of_lba0));
        printf("    Num Stripes : %u\n",
                __le32_to_cpu(map->num_data_stripes));
-       printf("     Chunk Size : %u KiB\n",
+       printf("     Chunk Size : %u KiB",
                __le16_to_cpu(map->blocks_per_strip) / 2);
+       if (map2)
+               printf(" <-- %u KiB",
+                       __le16_to_cpu(map2->blocks_per_strip) / 2);
+       printf("\n");
        printf("       Reserved : %d\n", __le32_to_cpu(dev->reserved_blocks));
        printf("  Migrate State : ");
        if (dev->vol.migr_state) {
@@ -785,7 +872,7 @@ static void examine_super_imsm(struct supertype *st, char *homehost)
        char nbuf[64];
        __u32 sum;
        __u32 reserved = imsm_reserved_sectors(super, super->disks);
-
+       struct dl *dl;
 
        snprintf(str, MPB_SIG_LEN, "%s", mpb->sig);
        printf("          Magic : %s\n", str);
@@ -830,6 +917,26 @@ static void examine_super_imsm(struct supertype *st, char *homehost)
                        continue;
                print_imsm_disk(mpb, 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));
+       }
 }
 
 static void brief_examine_super_imsm(struct supertype *st, int verbose)
@@ -1339,12 +1446,12 @@ static __u32 num_stripes_per_unit_rebuild(struct imsm_dev *dev)
                return num_stripes_per_unit_resync(dev);
 }
 
-static __u8 imsm_num_data_members(struct imsm_dev *dev)
+static __u8 imsm_num_data_members(struct imsm_dev *dev, int second_map)
 {
        /* named 'imsm_' because raid0, raid1 and raid10
         * counter-intuitively have the same number of data disks
         */
-       struct imsm_map *map = get_imsm_map(dev, 0);
+       struct imsm_map *map = get_imsm_map(dev, second_map);
 
        switch (get_imsm_raid_level(map)) {
        case 0:
@@ -1408,6 +1515,7 @@ static __u64 blocks_per_migr_unit(struct imsm_dev *dev)
                return 0;
 
        switch (migr_type(dev)) {
+       case MIGR_GEN_MIGR:
        case MIGR_VERIFY:
        case MIGR_REPAIR:
        case MIGR_INIT: {
@@ -1427,7 +1535,7 @@ static __u64 blocks_per_migr_unit(struct imsm_dev *dev)
                 */
                stripes_per_unit = num_stripes_per_unit_resync(dev);
                migr_chunk = migr_strip_blocks_resync(dev);
-               disks = imsm_num_data_members(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;
                segment = blocks_per_unit / stripe;
@@ -1444,7 +1552,6 @@ static __u64 blocks_per_migr_unit(struct imsm_dev *dev)
                migr_chunk = migr_strip_blocks_rebuild(dev);
                return migr_chunk * stripes_per_unit;
        }
-       case MIGR_GEN_MIGR:
        case MIGR_STATE_CHANGE:
        default:
                return 0;
@@ -1471,26 +1578,40 @@ static void getinfo_super_imsm_volume(struct supertype *st, struct mdinfo *info,
        struct intel_super *super = st->sb;
        struct imsm_dev *dev = get_imsm_dev(super, super->current_vol);
        struct imsm_map *map = get_imsm_map(dev, 0);
+       struct imsm_map *prev_map = get_imsm_map(dev, 1);
+       struct imsm_map *map_to_analyse = map;
        struct dl *dl;
        char *devname;
        int map_disks = info->array.raid_disks;
 
+       if (prev_map)
+               map_to_analyse = prev_map;
+
        for (dl = super->disks; dl; dl = dl->next)
                if (dl->raiddisk == info->disk.raid_disk)
                        break;
        info->container_member    = super->current_vol;
-       info->array.raid_disks    = map->num_members;
-       info->array.level         = get_imsm_raid_level(map);
+       info->array.raid_disks    = map_to_analyse->num_members;
+       info->array.level         = get_imsm_raid_level(map_to_analyse);
        info->array.layout        = imsm_level_to_layout(info->array.level);
        info->array.md_minor      = -1;
        info->array.ctime         = 0;
        info->array.utime         = 0;
-       info->array.chunk_size    = __le16_to_cpu(map->blocks_per_strip) << 9;
+       info->array.chunk_size    =
+               __le16_to_cpu(map_to_analyse->blocks_per_strip) << 9;
        info->array.state         = !dev->vol.dirty;
        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) {
+               info->new_level = get_imsm_raid_level(map);
+               info->new_layout = imsm_level_to_layout(info->new_level);
+               info->new_chunk = __le16_to_cpu(map->blocks_per_strip) << 9;
+       } else {
+               info->new_level = UnSet;
+               info->new_layout = UnSet;
+               info->new_chunk = info->array.chunk_size;
+       }
        info->disk.major = 0;
        info->disk.minor = 0;
        if (dl) {
@@ -1498,13 +1619,19 @@ static void getinfo_super_imsm_volume(struct supertype *st, struct mdinfo *info,
                info->disk.minor = dl->minor;
        }
 
-       info->data_offset         = __le32_to_cpu(map->pba_of_lba0);
-       info->component_size      = __le32_to_cpu(map->blocks_per_member);
+       info->data_offset         = __le32_to_cpu(map_to_analyse->pba_of_lba0);
+       info->component_size      =
+               __le32_to_cpu(map_to_analyse->blocks_per_member);
        memset(info->uuid, 0, sizeof(info->uuid));
        info->recovery_start = MaxSector;
-       info->reshape_active = 0;
+       info->reshape_active = (prev_map != NULL);
+       if (info->reshape_active)
+               info->delta_disks = map->num_members - prev_map->num_members;
+       else
+               info->delta_disks = 0;
 
-       if (map->map_state == IMSM_T_STATE_UNINITIALIZED || dev->vol.dirty) {
+       if (map_to_analyse->map_state == IMSM_T_STATE_UNINITIALIZED ||
+           dev->vol.dirty) {
                info->resync_start = 0;
        } else if (dev->vol.migr_state) {
                switch (migr_type(dev)) {
@@ -1552,47 +1679,15 @@ static void getinfo_super_imsm_volume(struct supertype *st, struct mdinfo *info,
                        dmap[i] = 0;
                        if (i < info->array.raid_disks) {
                                struct imsm_disk *dsk;
-                               j = get_imsm_disk_idx(dev, i);
+                               j = get_imsm_disk_idx(dev, i, -1);
                                dsk = get_imsm_disk(super, j);
                                if (dsk && (dsk->status & CONFIGURED_DISK))
                                        dmap[i] = 1;
                        }
                }
        }
-}                              
-
-/* check the config file to see if we can return a real uuid for this spare */
-static void fixup_container_spare_uuid(struct mdinfo *inf)
-{
-       struct mddev_ident *array_list;
-
-       if (inf->array.level != LEVEL_CONTAINER ||
-           memcmp(inf->uuid, uuid_match_any, sizeof(int[4])) != 0)
-               return;
-
-       array_list = conf_get_ident(NULL);
-
-       for (; array_list; array_list = array_list->next) {
-               if (array_list->uuid_set) {
-                       struct supertype *_sst; /* spare supertype */
-                       struct supertype *_cst; /* container supertype */
-
-                       _cst = array_list->st;
-                       if (_cst)
-                               _sst = _cst->ss->match_metadata_desc(inf->text_version);
-                       else
-                               _sst = NULL;
-
-                       if (_sst) {
-                               memcpy(inf->uuid, array_list->uuid, sizeof(int[4]));
-                               free(_sst);
-                               break;
-                       }
-               }
-       }
 }
 
-
 static __u8 imsm_check_degraded(struct intel_super *super, struct imsm_dev *dev, int failed);
 static int imsm_count_failed(struct intel_super *super, struct imsm_dev *dev);
 
@@ -1661,7 +1756,7 @@ static void getinfo_super_imsm(struct supertype *st, struct mdinfo *info, char *
                 * (catches single-degraded vs double-degraded)
                 */
                for (j = 0; j < map->num_members; j++) {
-                       __u32 ord = get_imsm_ord_tbl_ent(dev, i);
+                       __u32 ord = get_imsm_ord_tbl_ent(dev, i, -1);
                        __u32 idx = ord_to_idx(ord);
 
                        if (!(ord & IMSM_ORD_REBUILD) &&
@@ -1707,10 +1802,8 @@ static void getinfo_super_imsm(struct supertype *st, struct mdinfo *info, char *
         */
        if (info->disk.state & (1 << MD_DISK_SYNC) || super->anchor->num_raid_devs)
                uuid_from_super_imsm(st, info->uuid);
-       else {
-               memcpy(info->uuid, uuid_match_any, sizeof(int[4]));
-               fixup_container_spare_uuid(info);
-       }
+       else
+               memcpy(info->uuid, uuid_zero, sizeof(uuid_zero));
 
        /* I don't know how to compute 'map' on imsm, so use safe default */
        if (map) {
@@ -2437,6 +2530,7 @@ static void __free_imsm_disk(struct dl *d)
        free(d);
 
 }
+
 static void free_imsm_disks(struct intel_super *super)
 {
        struct dl *d;
@@ -2970,11 +3064,6 @@ static int load_super_imsm(struct supertype *st, int fd, char *devname)
        struct intel_super *super;
        int rv;
 
-#ifndef MDASSEMBLE
-       if (load_super_imsm_all(st, fd, &st->sb, devname) == 0)
-               return 0;
-#endif
-
        if (test_partition(fd))
                /* IMSM not allowed on partitions */
                return 1;
@@ -3336,7 +3425,7 @@ static int add_to_super_imsm_volume(struct supertype *st, mdu_disk_info_t *dk,
        /* Check the device has not already been added */
        slot = get_imsm_disk_slot(map, dl->index);
        if (slot >= 0 &&
-           (get_imsm_ord_tbl_ent(dev, slot) & IMSM_ORD_REBUILD) == 0) {
+           (get_imsm_ord_tbl_ent(dev, slot, -1) & IMSM_ORD_REBUILD) == 0) {
                fprintf(stderr, Name ": %s has been included in this array twice\n",
                        devname);
                return 1;
@@ -3402,6 +3491,7 @@ static int add_to_super_imsm(struct supertype *st, mdu_disk_info_t *dk,
        dd->devname = devname ? strdup(devname) : NULL;
        dd->fd = fd;
        dd->e = NULL;
+       dd->action = DISK_ADD;
        rv = imsm_read_serial(fd, devname, dd->serial);
        if (rv) {
                fprintf(stderr,
@@ -3421,8 +3511,8 @@ static int add_to_super_imsm(struct supertype *st, mdu_disk_info_t *dk,
                dd->disk.scsi_id = __cpu_to_le32(0);
 
        if (st->update_tail) {
-               dd->next = super->add;
-               super->add = dd;
+               dd->next = super->disk_mgmt_list;
+               super->disk_mgmt_list = dd;
        } else {
                dd->next = super->disks;
                super->disks = dd;
@@ -3431,6 +3521,43 @@ static int add_to_super_imsm(struct supertype *st, mdu_disk_info_t *dk,
        return 0;
 }
 
+
+static int remove_from_super_imsm(struct supertype *st, mdu_disk_info_t *dk)
+{
+       struct intel_super *super = st->sb;
+       struct dl *dd;
+
+       /* remove from super works only in mdmon - for communication
+        * manager - monitor. Check if communication memory buffer
+        * is prepared.
+        */
+       if (!st->update_tail) {
+               fprintf(stderr,
+                       Name ": %s shall be used in mdmon context only"
+                       "(line %d).\n", __func__, __LINE__);
+               return 1;
+       }
+       dd = malloc(sizeof(*dd));
+       if (!dd) {
+               fprintf(stderr,
+                       Name ": malloc failed %s:%d.\n", __func__, __LINE__);
+               return 1;
+       }
+       memset(dd, 0, sizeof(*dd));
+       dd->major = dk->major;
+       dd->minor = dk->minor;
+       dd->index = -1;
+       dd->fd = -1;
+       dd->disk.status = SPARE_DISK;
+       dd->action = DISK_REMOVE;
+
+       dd->next = super->disk_mgmt_list;
+       super->disk_mgmt_list = dd;
+
+
+       return 0;
+}
+
 static int store_imsm_mpb(int fd, struct imsm_super *mpb);
 
 static union {
@@ -3582,7 +3709,7 @@ static int create_array(struct supertype *st, int dev_idx)
        imsm_copy_dev(&u->dev, dev);
        inf = get_disk_info(u);
        for (i = 0; i < map->num_members; i++) {
-               int idx = get_imsm_disk_idx(dev, i);
+               int idx = get_imsm_disk_idx(dev, i, -1);
 
                disk = get_imsm_disk(super, idx);
                serialcpy(inf[i].serial, disk->serial);
@@ -3592,13 +3719,13 @@ static int create_array(struct supertype *st, int dev_idx)
        return 0;
 }
 
-static int _add_disk(struct supertype *st)
+static int mgmt_disk(struct supertype *st)
 {
        struct intel_super *super = st->sb;
        size_t len;
-       struct imsm_update_add_disk *u;
+       struct imsm_update_add_remove_disk *u;
 
-       if (!super->add)
+       if (!super->disk_mgmt_list)
                return 0;
 
        len = sizeof(*u);
@@ -3609,7 +3736,7 @@ static int _add_disk(struct supertype *st)
                return 1;
        }
 
-       u->type = update_add_disk;
+       u->type = update_add_remove_disk;
        append_metadata_update(st, u, len);
 
        return 0;
@@ -3626,23 +3753,17 @@ static int write_init_super_imsm(struct supertype *st)
        if (st->update_tail) {
                /* queue the recently created array / added disk
                 * as a metadata update */
-               struct dl *d;
                int rv;
 
                /* determine if we are creating a volume or adding a disk */
                if (current_vol < 0) {
-                       /* in the add disk case we are running in mdmon
-                        * context, so don't close fd's
+                       /* in the mgmt (add/remove) disk case we are running
+                        * in mdmon context, so don't close fd's
                         */
-                       return _add_disk(st);
+                       return mgmt_disk(st);
                } else
                        rv = create_array(st, current_vol);
 
-               for (d = super->disks; d ; d = d->next) {
-                       close(d->fd);
-                       d->fd = -1;
-               }
-
                return rv;
        } else {
                struct dl *d;
@@ -4208,7 +4329,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->orom)
+       if (chunk && (*chunk == UnSet || *chunk == 0) && 
+           super && super->orom)
                *chunk = imsm_orom_default_chunk(super->orom);
 }
 
@@ -4416,17 +4538,16 @@ 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;
 
-       /* do not assemble arrays that might have bad blocks */
-       if (imsm_bbm_log_size(super->anchor)) {
-               fprintf(stderr, Name ": BBM log found in metadata. "
-                               "Cannot activate array(s).\n");
-               return NULL;
-       }
+       /* check for bad blocks */
+       if (imsm_bbm_log_size(super->anchor))
+               bbm_errors = 1;
 
        for (i = 0; i < mpb->num_raid_devs; i++) {
                struct imsm_dev *dev;
                struct imsm_map *map;
+               struct imsm_map *map2;
                struct mdinfo *this;
                int slot;
                char *ep;
@@ -4437,6 +4558,7 @@ static struct mdinfo *container_content_imsm(struct supertype *st, char *subarra
 
                dev = get_imsm_dev(super, i);
                map = get_imsm_map(dev, 0);
+               map2 = get_imsm_map(dev, 1);
 
                /* do not publish arrays that are in the middle of an
                 * unsupported migration
@@ -4469,8 +4591,8 @@ static struct mdinfo *container_content_imsm(struct supertype *st, char *subarra
                        __u32 ord;
 
                        skip = 0;
-                       idx = get_imsm_disk_idx(dev, slot);
-                       ord = get_imsm_ord_tbl_ent(dev, slot); 
+                       idx = get_imsm_disk_idx(dev, slot, 0);
+                       ord = get_imsm_ord_tbl_ent(dev, slot, 0);
                        for (d = super->disks; d ; d = d->next)
                                if (d->index == idx)
                                        break;
@@ -4518,7 +4640,17 @@ static struct mdinfo *container_content_imsm(struct supertype *st, char *subarra
                        info_d->disk.minor = d->minor;
                        info_d->disk.raid_disk = slot;
                        info_d->recovery_start = recovery_start;
-
+                       if (map2) {
+                               if (slot < map2->num_members)
+                                       info_d->disk.state = (1 << MD_DISK_ACTIVE);
+                               else
+                                       this->array.spare_disks++;
+                       } else {
+                               if (slot < map->num_members)
+                                       info_d->disk.state = (1 << MD_DISK_ACTIVE);
+                               else
+                                       this->array.spare_disks++;
+                       }
                        if (info_d->recovery_start == MaxSector)
                                this->array.working_disks++;
 
@@ -4531,6 +4663,10 @@ 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;
 }
 
@@ -4565,7 +4701,7 @@ static __u8 imsm_check_degraded(struct intel_super *super, struct imsm_dev *dev,
                int insync = insync;
 
                for (i = 0; i < map->num_members; i++) {
-                       __u32 ord = get_imsm_ord_tbl_ent(dev, i);
+                       __u32 ord = get_imsm_ord_tbl_ent(dev, i, -1);
                        int idx = ord_to_idx(ord);
                        struct imsm_disk *disk;
 
@@ -4662,9 +4798,13 @@ static int is_resyncing(struct imsm_dev *dev)
            migr_type(dev) == MIGR_REPAIR)
                return 1;
 
+       if (migr_type(dev) == MIGR_GEN_MIGR)
+               return 0;
+
        migr_map = get_imsm_map(dev, 1);
 
-       if (migr_map->map_state == IMSM_T_STATE_NORMAL)
+       if ((migr_map->map_state == IMSM_T_STATE_NORMAL) &&
+           (dev->vol.migr_type != MIGR_GEN_MIGR))
                return 1;
        else
                return 0;
@@ -4727,7 +4867,74 @@ static void handle_missing(struct intel_super *super, struct imsm_dev *dev)
 
 static void imsm_set_disk(struct active_array *a, int n, int state);
 
-/* Handle dirty -> clean transititions and resync.  Degraded and rebuild
+static void imsm_progress_container_reshape(struct intel_super *super)
+{
+       /* if no device has a migr_state, but some device has a
+        * different number of members than the previous device, start
+        * changing the number of devices in this device to match
+        * previous.
+        */
+       struct imsm_super *mpb = super->anchor;
+       int prev_disks = -1;
+       int i;
+
+       for (i = 0; i < mpb->num_raid_devs; i++) {
+               struct imsm_dev *dev = get_imsm_dev(super, i);
+               struct imsm_map *map = get_imsm_map(dev, 0);
+               struct imsm_map *map2;
+               int prev_num_members;
+               int used_disks;
+
+               if (dev->vol.migr_state)
+                       return;
+
+               if (prev_disks == -1)
+                       prev_disks = map->num_members;
+               if (prev_disks == map->num_members)
+                       continue;
+
+               /* OK, this array needs to enter reshape mode.
+                * i.e it needs a migr_state
+                */
+
+               prev_num_members = map->num_members;
+               map->num_members = prev_disks;
+               dev->vol.migr_state = 1;
+               dev->vol.curr_migr_unit = 0;
+               dev->vol.migr_type = MIGR_GEN_MIGR;
+               for (i = prev_num_members;
+                    i < map->num_members; i++)
+                       set_imsm_ord_tbl_ent(map, i, i);
+               map2 = get_imsm_map(dev, 1);
+               /* Copy the current map */
+               memcpy(map2, map, sizeof_imsm_map(map));
+               map2->num_members = prev_num_members;
+
+               /* calculate new size
+                */
+               used_disks = imsm_num_data_members(dev, 0);
+               if (used_disks) {
+                       unsigned long long array_blocks;
+
+                       array_blocks =
+                               map->blocks_per_member
+                               * used_disks;
+                       /* round array size down to closest MB
+                        */
+                       array_blocks = (array_blocks
+                                       >> SECT_PER_MB_SHIFT)
+                               << SECT_PER_MB_SHIFT;
+                       dev->size_low =
+                               __cpu_to_le32((__u32)array_blocks);
+                       dev->size_high =
+                               __cpu_to_le32(
+                                       (__u32)(array_blocks >> 32));
+               }
+               super->updates_pending++;
+       }
+}
+
+/* Handle dirty -> clean transititions, resync and reshape.  Degraded and rebuild
  * states are handled in imsm_set_disk() with one exception, when a
  * resync is stopped due to a new failure this routine will set the
  * 'degraded' state for the array.
@@ -4742,6 +4949,65 @@ static int imsm_set_array_state(struct active_array *a, int consistent)
        __u8 map_state = imsm_check_degraded(super, dev, failed);
        __u32 blocks_per_unit;
 
+       if (dev->vol.migr_state &&
+           dev->vol.migr_type  == MIGR_GEN_MIGR) {
+               /* array state change is blocked due to reshape action
+                * We might need to
+                * - abort the reshape (if last_checkpoint is 0 and action!= reshape)
+                * - finish the reshape (if last_checkpoint is big and action != reshape)
+                * - update curr_migr_unit
+                */
+               if (a->curr_action == reshape) {
+                       /* still reshaping, maybe update curr_migr_unit */
+                       long long blocks_per_unit = blocks_per_migr_unit(dev);
+                       long long unit = a->last_checkpoint;
+                       if (blocks_per_unit) {
+                               unit /= blocks_per_unit;
+                               if (unit >
+                                   __le32_to_cpu(dev->vol.curr_migr_unit)) {
+                                       dev->vol.curr_migr_unit =
+                                               __cpu_to_le32(unit);
+                                       super->updates_pending++;
+                               }
+                       }
+                       return 0;
+               } else {
+                       if (a->last_checkpoint == 0 && a->prev_action == reshape) {
+                               /* for some reason we aborted the reshape.
+                                * Better clean up
+                                */
+                               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;
+                               int used_disks;
+                               /* it seems the reshape is all done */
+                               dev->vol.migr_state = 0;
+                               dev->vol.migr_type = 0;
+                               dev->vol.curr_migr_unit = 0;
+
+                               used_disks = imsm_num_data_members(dev, -1);
+                               array_blocks = map->blocks_per_member * used_disks;
+                               /* round array size down to closest MB */
+                               array_blocks = (array_blocks >> SECT_PER_MB_SHIFT)
+                                       << SECT_PER_MB_SHIFT;
+                               dev->size_low = __cpu_to_le32((__u32) array_blocks);
+                               dev->size_high = __cpu_to_le32((__u32) (array_blocks >> 32));
+                               a->info.custom_array_size = array_blocks;
+                               a->check_reshape = 1; /* encourage manager to update
+                                                      * array size
+                                                      */
+                               super->updates_pending++;
+                               imsm_progress_container_reshape(super);
+                       }                               
+               }
+       }
+
        /* before we activate this array handle any missing disks */
        if (consistent == 2)
                handle_missing(super, dev);
@@ -4835,7 +5101,7 @@ static void imsm_set_disk(struct active_array *a, int n, int state)
 
        dprintf("imsm: set_disk %d:%x\n", n, state);
 
-       ord = get_imsm_ord_tbl_ent(dev, n);
+       ord = get_imsm_ord_tbl_ent(dev, n, -1);
        disk = get_imsm_disk(super, ord_to_idx(ord));
 
        /* check for new failures */
@@ -4931,6 +5197,7 @@ static void imsm_sync_metadata(struct supertype *container)
 {
        struct intel_super *super = container->sb;
 
+       dprintf("sync metadata: %d\n", super->updates_pending);
        if (!super->updates_pending)
                return;
 
@@ -4942,7 +5209,7 @@ static void imsm_sync_metadata(struct supertype *container)
 static struct dl *imsm_readd(struct intel_super *super, int idx, struct active_array *a)
 {
        struct imsm_dev *dev = get_imsm_dev(super, a->info.container_member);
-       int i = get_imsm_disk_idx(dev, idx);
+       int i = get_imsm_disk_idx(dev, idx, -1);
        struct dl *dl;
 
        for (dl = super->disks; dl; dl = dl->next)
@@ -4959,10 +5226,11 @@ static struct dl *imsm_readd(struct intel_super *super, int idx, struct active_a
 }
 
 static struct dl *imsm_add_spare(struct intel_super *super, int slot,
-                                struct active_array *a, int activate_new)
+                                struct active_array *a, int activate_new,
+                                struct mdinfo *additional_test_list)
 {
        struct imsm_dev *dev = get_imsm_dev(super, a->info.container_member);
-       int idx = get_imsm_disk_idx(dev, slot);
+       int idx = get_imsm_disk_idx(dev, slot, -1);
        struct imsm_super *mpb = super->anchor;
        struct imsm_map *map;
        unsigned long long pos;
@@ -4973,6 +5241,7 @@ static struct dl *imsm_add_spare(struct intel_super *super, int slot,
        __u32 array_start = 0;
        __u32 array_end = 0;
        struct dl *dl;
+       struct mdinfo *test_list;
 
        for (dl = super->disks; dl; dl = dl->next) {
                /* If in this array, skip */
@@ -4980,11 +5249,24 @@ static struct dl *imsm_add_spare(struct intel_super *super, int slot,
                        if (d->state_fd >= 0 &&
                            d->disk.major == dl->major &&
                            d->disk.minor == dl->minor) {
-                               dprintf("%x:%x already in array\n", dl->major, dl->minor);
+                               dprintf("%x:%x already in array\n",
+                                       dl->major, dl->minor);
                                break;
                        }
                if (d)
                        continue;
+               test_list = additional_test_list;
+               while (test_list) {
+                       if (test_list->disk.major == dl->major &&
+                           test_list->disk.minor == dl->minor) {
+                               dprintf("%x:%x already in additional test list\n",
+                                       dl->major, dl->minor);
+                               break;
+                       }
+                       test_list = test_list->next;
+               }
+               if (test_list)
+                       continue;
 
                /* skip in use or failed drives */
                if (is_failed(&dl->disk) || idx == dl->index ||
@@ -5054,6 +5336,45 @@ static struct dl *imsm_add_spare(struct intel_super *super, int slot,
        return dl;
 }
 
+
+static int imsm_rebuild_allowed(struct supertype *cont, int dev_idx, int failed)
+{
+       struct imsm_dev *dev2;
+       struct imsm_map *map;
+       struct dl *idisk;
+       int slot;
+       int idx;
+       __u8 state;
+
+       dev2 = get_imsm_dev(cont->sb, dev_idx);
+       if (dev2) {
+               state = imsm_check_degraded(cont->sb, dev2, failed);
+               if (state == IMSM_T_STATE_FAILED) {
+                       map = get_imsm_map(dev2, 0);
+                       if (!map)
+                               return 1;
+                       for (slot = 0; slot < map->num_members; slot++) {
+                               /*
+                                * Check if failed disks are deleted from intel
+                                * disk list or are marked to be deleted
+                                */
+                               idx = get_imsm_disk_idx(dev2, slot, -1);
+                               idisk = get_imsm_dl_disk(cont->sb, idx);
+                               /*
+                                * Do not rebuild the array if failed disks
+                                * from failed sub-array are not removed from
+                                * container.
+                                */
+                               if (idisk &&
+                                   is_failed(&idisk->disk) &&
+                                   (idisk->action != DISK_REMOVE))
+                                       return 0;
+                       }
+               }
+       }
+       return 1;
+}
+
 static struct mdinfo *imsm_activate_spare(struct active_array *a,
                                          struct metadata_update **updates)
 {
@@ -5081,6 +5402,7 @@ static struct mdinfo *imsm_activate_spare(struct active_array *a,
        struct imsm_update_activate_spare *u;
        int num_spares = 0;
        int i;
+       int allowed;
 
        for (d = a->info.devs ; d ; d = d->next) {
                if ((d->curr_state & DS_FAULTY) &&
@@ -5093,9 +5415,41 @@ 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 (a->info.array.level == 4)
+               /* No repair for takeovered array
+                * imsm doesn't support raid4
+                */
+               return NULL;
+
        if (imsm_check_degraded(super, dev, failed) != IMSM_T_STATE_DEGRADED)
                return NULL;
 
+       /*
+        * If there are any failed disks check state of the other volume.
+        * Block rebuild if the another one is failed until failed disks
+        * are removed from container.
+        */
+       if (failed) {
+               dprintf("found failed disks in %s, check if there another"
+                       "failed sub-array.\n",
+                       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) {
+                               allowed = imsm_rebuild_allowed(a->container,
+                                                              i, failed);
+                               if (!allowed)
+                                       return NULL;
+                       }
+               }
+       }
+
        /* For each slot, if it is not working, find a spare */
        for (i = 0; i < a->info.array.raid_disks; i++) {
                for (d = a->info.devs ; d ; d = d->next)
@@ -5114,9 +5468,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);
+                       dl = imsm_add_spare(super, i, a, 0, NULL);
                if (!dl)
-                       dl = imsm_add_spare(super, i, a, 1);
+                       dl = imsm_add_spare(super, i, a, 1, NULL);
                if (!dl)
                        continue;
  
@@ -5183,6 +5537,7 @@ static struct mdinfo *imsm_activate_spare(struct active_array *a,
        }
                        
        mu->space = NULL;
+       mu->space_list = NULL;
        mu->len = sizeof(struct imsm_update_activate_spare) * num_spares;
        mu->next = *updates;
        u = (struct imsm_update_activate_spare *) mu->buf;
@@ -5213,7 +5568,7 @@ static int disks_overlap(struct intel_super *super, int idx, struct imsm_update_
        int j;
 
        for (i = 0; i < map->num_members; i++) {
-               disk = get_imsm_disk(super, get_imsm_disk_idx(dev, i));
+               disk = get_imsm_disk(super, get_imsm_disk_idx(dev, i, -1));
                for (j = 0; j < new_map->num_members; j++)
                        if (serialcmp(disk->serial, inf[j].serial) == 0)
                                return 1;
@@ -5222,18 +5577,267 @@ static int disks_overlap(struct intel_super *super, int idx, struct imsm_update_
        return 0;
 }
 
+
+static struct dl *get_disk_super(struct intel_super *super, int major, int minor)
+{
+       struct dl *dl = NULL;
+       for (dl = super->disks; dl; dl = dl->next)
+               if ((dl->major == major) &&  (dl->minor == minor))
+                       return dl;
+       return NULL;
+}
+
+static int remove_disk_super(struct intel_super *super, int major, int minor)
+{
+       struct dl *prev = NULL;
+       struct dl *dl;
+
+       prev = NULL;
+       for (dl = super->disks; dl; dl = dl->next) {
+               if ((dl->major == major) && (dl->minor == minor)) {
+                       /* remove */
+                       if (prev)
+                               prev->next = dl->next;
+                       else
+                               super->disks = dl->next;
+                       dl->next = NULL;
+                       __free_imsm_disk(dl);
+                       dprintf("%s: removed %x:%x\n",
+                               __func__, major, minor);
+                       break;
+               }
+               prev = dl;
+       }
+       return 0;
+}
+
 static void imsm_delete(struct intel_super *super, struct dl **dlp, unsigned index);
 
+static int add_remove_disk_update(struct intel_super *super)
+{
+       int check_degraded = 0;
+       struct dl *disk = NULL;
+       /* add/remove some spares to/from the metadata/contrainer */
+       while (super->disk_mgmt_list) {
+               struct dl *disk_cfg;
+
+               disk_cfg = super->disk_mgmt_list;
+               super->disk_mgmt_list = disk_cfg->next;
+               disk_cfg->next = NULL;
+
+               if (disk_cfg->action == DISK_ADD) {
+                       disk_cfg->next = super->disks;
+                       super->disks = disk_cfg;
+                       check_degraded = 1;
+                       dprintf("%s: added %x:%x\n",
+                               __func__, disk_cfg->major,
+                               disk_cfg->minor);
+               } else if (disk_cfg->action == DISK_REMOVE) {
+                       dprintf("Disk remove action processed: %x.%x\n",
+                               disk_cfg->major, disk_cfg->minor);
+                       disk = get_disk_super(super,
+                                             disk_cfg->major,
+                                             disk_cfg->minor);
+                       if (disk) {
+                               /* store action status */
+                               disk->action = DISK_REMOVE;
+                               /* remove spare disks only */
+                               if (disk->index == -1) {
+                                       remove_disk_super(super,
+                                                         disk_cfg->major,
+                                                         disk_cfg->minor);
+                               }
+                       }
+                       /* release allocate disk structure */
+                       __free_imsm_disk(disk_cfg);
+               }
+       }
+       return check_degraded;
+}
+
+static int apply_reshape_container_disks_update(struct imsm_update_reshape *u,
+                                               struct intel_super *super,
+                                               void ***space_list)
+{
+       struct dl *new_disk;
+       struct intel_dev *id;
+       int i;
+       int delta_disks = u->new_raid_disks - u->old_raid_disks;
+       int disk_count = u->old_raid_disks;
+       void **tofree = NULL;
+       int devices_to_reshape = 1;
+       struct imsm_super *mpb = super->anchor;
+       int ret_val = 0;
+
+       dprintf("imsm: imsm_process_update() for update_reshape\n");
+
+       /* enable spares to use in array */
+       for (i = 0; i < delta_disks; i++) {
+               new_disk = get_disk_super(super,
+                                         major(u->new_disks[i]),
+                                         minor(u->new_disks[i]));
+               dprintf("imsm: imsm_process_update(): new disk "
+                       "for reshape is: %i:%i (%p, index = %i)\n",
+                       major(u->new_disks[i]), minor(u->new_disks[i]),
+                       new_disk, new_disk->index);
+               if ((new_disk == NULL) ||
+                   ((new_disk->index >= 0) &&
+                    (new_disk->index < u->old_raid_disks)))
+                       goto update_reshape_exit;
+               new_disk->index = disk_count++;
+               /* slot to fill in autolayout
+                */
+               new_disk->raiddisk = new_disk->index;
+               new_disk->disk.status |=
+                       CONFIGURED_DISK;
+               new_disk->disk.status &= ~SPARE_DISK;
+       }
+
+       dprintf("imsm: process_update(): update_reshape: volume set"
+               " mpb->num_raid_devs = %i\n", mpb->num_raid_devs);
+       /* manage changes in volume
+        */
+       for (id = super->devlist ; id; id = id->next) {
+               void **sp = *space_list;
+               struct imsm_dev *newdev;
+               struct imsm_map *newmap, *oldmap;
+
+               if (!sp)
+                       continue;
+               *space_list = *sp;
+               newdev = (void*)sp;
+               /* Copy the dev, but not (all of) the map */
+               memcpy(newdev, id->dev, sizeof(*newdev));
+               oldmap = get_imsm_map(id->dev, 0);
+               newmap = get_imsm_map(newdev, 0);
+               /* Copy the current map */
+               memcpy(newmap, oldmap, sizeof_imsm_map(oldmap));
+               /* update one device only
+                */
+               if (devices_to_reshape) {
+                       int used_disks;
+
+                       dprintf("process_update(): modifying "
+                               "subdev: %i\n", id->index);
+                       devices_to_reshape--;
+                       newdev->vol.migr_state = 1;
+                       newdev->vol.curr_migr_unit = 0;
+                       newdev->vol.migr_type = MIGR_GEN_MIGR;
+                       newmap->num_members = u->new_raid_disks;
+                       for (i = 0; i < delta_disks; i++) {
+                               set_imsm_ord_tbl_ent(newmap,
+                                                    u->old_raid_disks + i,
+                                                    u->old_raid_disks + i);
+                       }
+                       /* New map is correct, now need to save old map
+                        */
+                       newmap = get_imsm_map(newdev, 1);
+                       memcpy(newmap, oldmap, sizeof_imsm_map(oldmap));
+
+                       /* calculate new size
+                        */
+                       used_disks = imsm_num_data_members(newdev, 0);
+                       if (used_disks) {
+                               unsigned long long array_blocks;
+
+                               array_blocks =
+                                       newmap->blocks_per_member * used_disks;
+                               /* round array size down to closest MB
+                                */
+                               array_blocks = (array_blocks
+                                               >> SECT_PER_MB_SHIFT)
+                                       << SECT_PER_MB_SHIFT;
+                               newdev->size_low =
+                                       __cpu_to_le32((__u32)array_blocks);
+                               newdev->size_high =
+                                       __cpu_to_le32((__u32)(array_blocks >> 32));
+                       }
+               }
+
+               sp = (void **)id->dev;
+               id->dev = newdev;
+               *sp = tofree;
+               tofree = sp;
+       }
+       if (tofree)
+               *space_list = tofree;
+       ret_val = 1;
+
+update_reshape_exit:
+
+       return ret_val;
+}
+
+static int apply_takeover_update(struct imsm_update_takeover *u,
+                                struct intel_super *super)
+{
+       struct imsm_dev *dev = NULL;
+       struct imsm_map *map;
+       struct dl *dm, *du;
+       struct intel_dev *dv;
+
+       for (dv = super->devlist; dv; dv = dv->next)
+               if (dv->index == (unsigned int)u->subarray) {
+                       dev = dv->dev;
+                       break;
+               }
+
+       if (dev == NULL)
+               return 0;
+
+       map = get_imsm_map(dev, 0);
+
+       if (u->direction == R10_TO_R0) {
+               /* iterate through devices to mark removed disks as spare */
+               for (dm = super->disks; dm; dm = dm->next) {
+                       if (dm->disk.status & FAILED_DISK) {
+                               int idx = dm->index;
+                               /* update indexes on the disk list */
+/* FIXME this loop-with-the-loop looks wrong,  I'm not convinced
+   the index values will end up being correct.... NB */
+                               for (du = super->disks; du; du = du->next)
+                                       if (du->index > idx)
+                                               du->index--;
+                               /* mark as spare disk */
+                               dm->disk.status = SPARE_DISK;
+                               dm->index = -1;
+                       }
+               }
+
+               /* update map */
+               map->num_members = map->num_members / 2;
+               map->map_state = IMSM_T_STATE_NORMAL;
+               map->num_domains = 1;
+               map->raid_level = 0;
+               map->failed_disk_num = -1;
+       }
+
+       /* update disk order table */
+       for (du = super->disks; du; du = du->next)
+               if (du->index >= 0)
+                       set_imsm_ord_tbl_ent(map, du->index, du->index);
+
+       return 1;
+}
+
 static void imsm_process_update(struct supertype *st,
                                struct metadata_update *update)
 {
        /**
         * crack open the metadata_update envelope to find the update record
         * update can be one of:
-        *      update_activate_spare - a spare device has replaced a failed
+        *    update_reshape_container_disks - all the arrays in the container
+        *      are being reshaped to have more devices.  We need to mark
+        *      the arrays for general migration and convert selected spares
+        *      into active devices.
+        *    update_activate_spare - a spare device has replaced a failed
         *      device in an array, update the disk_ord_tbl.  If this disk is
         *      present in all member arrays then also clear the SPARE_DISK
         *      flag
+        *    update_create_array
+        *    update_kill_array
+        *    update_rename_array
+        *    update_add_remove_disk
         */
        struct intel_super *super = st->sb;
        struct imsm_super *mpb;
@@ -5258,6 +5862,20 @@ static void imsm_process_update(struct supertype *st,
        mpb = super->anchor;
 
        switch (type) {
+       case update_takeover: {
+               struct imsm_update_takeover *u = (void *)update->buf;
+               if (apply_takeover_update(u, super))
+                       super->updates_pending++;
+               break;
+       }
+
+       case update_reshape_container_disks: {
+               struct imsm_update_reshape *u = (void *)update->buf;
+               if (apply_reshape_container_disks_update(
+                           u, super, &update->space_list))
+                       super->updates_pending++;
+               break;
+       }
        case update_activate_spare: {
                struct imsm_update_activate_spare *u = (void *) update->buf; 
                struct imsm_dev *dev = get_imsm_dev(super, u->array);
@@ -5269,7 +5887,7 @@ static void imsm_process_update(struct supertype *st,
                struct dl *dl;
                unsigned int found;
                int failed;
-               int victim = get_imsm_disk_idx(dev, u->slot);
+               int victim = get_imsm_disk_idx(dev, u->slot, -1);
                int i;
 
                for (dl = super->disks; dl; dl = dl->next)
@@ -5292,7 +5910,8 @@ static void imsm_process_update(struct supertype *st,
                for (i = 0; i < map->num_members; i++) {
                        if (i == u->slot)
                                continue;
-                       disk = get_imsm_disk(super, get_imsm_disk_idx(dev, i));
+                       disk = get_imsm_disk(super,
+                                            get_imsm_disk_idx(dev, i, -1));
                        if (!disk || is_failed(disk))
                                failed++;
                }
@@ -5533,31 +6152,24 @@ static void imsm_process_update(struct supertype *st,
                super->updates_pending++;
                break;
        }
-       case update_add_disk:
-
+       case update_add_remove_disk: {
                /* we may be able to repair some arrays if disks are
-                * being added */
-               if (super->add) {
+                * being added, check teh status of add_remove_disk
+                * if discs has been added.
+                */
+               if (add_remove_disk_update(super)) {
                        struct active_array *a;
 
                        super->updates_pending++;
-                       for (a = st->arrays; a; a = a->next)
+                       for (a = st->arrays; a; a = a->next)
                                a->check_degraded = 1;
                }
-               /* add some spares to the metadata */
-               while (super->add) {
-                       struct dl *al;
-
-                       al = super->add;
-                       super->add = al->next;
-                       al->next = super->disks;
-                       super->disks = al;
-                       dprintf("%s: added %x:%x\n",
-                               __func__, al->major, al->minor);
-               }
-
                break;
        }
+       default:
+               fprintf(stderr, "error: unsuported process update type:"
+                       "(type: %d)\n", type);
+       }
 }
 
 static void imsm_prepare_update(struct supertype *st,
@@ -5577,6 +6189,39 @@ static void imsm_prepare_update(struct supertype *st,
        size_t len = 0;
 
        switch (type) {
+       case update_reshape_container_disks: {
+               /* Every raid device in the container is about to
+                * gain some more devices, and we will enter a
+                * reconfiguration.
+                * So each 'imsm_map' will be bigger, and the imsm_vol
+                * will now hold 2 of them.
+                * Thus we need new 'struct imsm_dev' allocations sized
+                * as sizeof_imsm_dev but with more devices in both maps.
+                */
+               struct imsm_update_reshape *u = (void *)update->buf;
+               struct intel_dev *dl;
+               void **space_tail = (void**)&update->space_list;
+
+               dprintf("imsm: imsm_prepare_update() for update_reshape\n");
+
+               for (dl = super->devlist; dl; dl = dl->next) {
+                       int size = sizeof_imsm_dev(dl->dev, 1);
+                       void *s;
+                       if (u->new_raid_disks > u->old_raid_disks)
+                               size += sizeof(__u32)*2*
+                                       (u->new_raid_disks - u->old_raid_disks);
+                       s = malloc(size);
+                       if (!s)
+                               break;
+                       *space_tail = s;
+                       space_tail = s;
+                       *space_tail = NULL;
+               }
+
+               len = disks_to_mpb_size(u->new_raid_disks);
+               dprintf("New anchor length is %llu\n", (unsigned long long)len);
+               break;
+       }
        case update_create_array: {
                struct imsm_update_create_array *u = (void *) update->buf;
                struct intel_dev *dv;
@@ -5672,7 +6317,7 @@ static void imsm_delete(struct intel_super *super, struct dl **dlp, unsigned ind
                        /* update ord entries being careful not to propagate
                         * ord-flags to the first map
                         */
-                       ord = get_imsm_ord_tbl_ent(dev, j);
+                       ord = get_imsm_ord_tbl_ent(dev, j, -1);
 
                        if (ord_to_idx(ord) <= index)
                                continue;
@@ -5730,6 +6375,489 @@ static const char *imsm_get_disk_controller_domain(const char *path)
                return NULL;
 }
 
+static int imsm_find_array_minor_by_subdev(int subdev, int container, int *minor)
+{
+       char subdev_name[20];
+       struct mdstat_ent *mdstat;
+
+       sprintf(subdev_name, "%d", subdev);
+       mdstat = mdstat_by_subdev(subdev_name, container);
+       if (!mdstat)
+               return -1;
+
+       *minor = mdstat->devnum;
+       free_mdstat(mdstat);
+       return 0;
+}
+
+static int imsm_reshape_is_allowed_on_container(struct supertype *st,
+                                               struct geo_params *geo,
+                                               int *old_raid_disks)
+{
+       /* currently we only support increasing the number of devices
+        * for a container.  This increases the number of device for each
+        * member array.  They must all be RAID0 or RAID5.
+        */
+       int ret_val = 0;
+       struct mdinfo *info, *member;
+       int devices_that_can_grow = 0;
+
+       dprintf("imsm: imsm_reshape_is_allowed_on_container(ENTER): "
+               "st->devnum = (%i)\n",
+               st->devnum);
+
+       if (geo->size != -1 ||
+           geo->level != UnSet ||
+           geo->layout != UnSet ||
+           geo->chunksize != 0 ||
+           geo->raid_disks == UnSet) {
+               dprintf("imsm: Container operation is allowed for "
+                       "raid disks number change only.\n");
+               return ret_val;
+       }
+
+       info = container_content_imsm(st, NULL);
+       for (member = info; member; member = member->next) {
+               int result;
+               int minor;
+
+               dprintf("imsm: checking device_num: %i\n",
+                       member->container_member);
+
+               if (geo->raid_disks < member->array.raid_disks) {
+                       /* we work on container for Online Capacity Expansion
+                        * only so raid_disks has to grow
+                        */
+                       dprintf("imsm: for container operation raid disks "
+                               "increase is required\n");
+                       break;
+               }
+
+               if ((info->array.level != 0) &&
+                   (info->array.level != 5)) {
+                       /* we cannot use this container with other raid level
+                        */
+                       dprintf("imsm: for container operation wrong"
+                               " raid level (%i) detected\n",
+                               info->array.level);
+                       break;
+               } else {
+                       /* check for platform support
+                        * for this raid level configuration
+                        */
+                       struct intel_super *super = st->sb;
+                       if (!is_raid_level_supported(super->orom,
+                                                    member->array.level,
+                                                    geo->raid_disks)) {
+                               dprintf("platform does not support raid%d with"
+                                       " %d disk%s\n",
+                                        info->array.level,
+                                        geo->raid_disks,
+                                        geo->raid_disks > 1 ? "s" : "");
+                               break;
+                       }
+               }
+
+               if (*old_raid_disks &&
+                   info->array.raid_disks != *old_raid_disks)
+                       break;
+               *old_raid_disks = info->array.raid_disks;
+
+               /* All raid5 and raid0 volumes in container
+                * have to be ready for Online Capacity Expansion
+                * so they need to be assembled.  We have already
+                * checked that no recovery etc is happening.
+                */
+               result = imsm_find_array_minor_by_subdev(member->container_member,
+                                                        st->container_dev,
+                                                        &minor);
+               if (result < 0) {
+                       dprintf("imsm: cannot find array\n");
+                       break;
+               }
+               devices_that_can_grow++;
+       }
+       sysfs_free(info);
+       if (!member && devices_that_can_grow)
+               ret_val = 1;
+
+       if (ret_val)
+               dprintf("\tContainer operation allowed\n");
+       else
+               dprintf("\tError: %i\n", ret_val);
+
+       return ret_val;
+}
+
+/* Function: get_spares_for_grow
+ * Description: Allocates memory and creates list of spare devices
+ *             avaliable in container. Checks if spare drive size is acceptable.
+ * Parameters: Pointer to the supertype structure
+ * Returns: Pointer to the list of spare devices (mdinfo structure) on success,
+ *             NULL if fail
+ */
+static struct mdinfo *get_spares_for_grow(struct supertype *st)
+{
+       unsigned long long min_size = min_acceptable_spare_size_imsm(st);
+       return container_choose_spares(st, min_size, NULL, NULL, NULL, 0);
+}
+
+/******************************************************************************
+ * function: imsm_create_metadata_update_for_reshape
+ * Function creates update for whole IMSM container.
+ *
+ ******************************************************************************/
+static int imsm_create_metadata_update_for_reshape(
+       struct supertype *st,
+       struct geo_params *geo,
+       int old_raid_disks,
+       struct imsm_update_reshape **updatep)
+{
+       struct intel_super *super = st->sb;
+       struct imsm_super *mpb = super->anchor;
+       int update_memory_size = 0;
+       struct imsm_update_reshape *u = NULL;
+       struct mdinfo *spares = NULL;
+       int i;
+       int delta_disks = 0;
+       struct mdinfo *dev;
+
+       dprintf("imsm_update_metadata_for_reshape(enter) raid_disks = %i\n",
+               geo->raid_disks);
+
+       delta_disks = geo->raid_disks - old_raid_disks;
+
+       /* size of all update data without anchor */
+       update_memory_size = sizeof(struct imsm_update_reshape);
+
+       /* now add space for spare disks that we need to add. */
+       update_memory_size += sizeof(u->new_disks[0]) * (delta_disks - 1);
+
+       u = calloc(1, update_memory_size);
+       if (u == NULL) {
+               dprintf("error: "
+                       "cannot get memory for imsm_update_reshape update\n");
+               return 0;
+       }
+       u->type = update_reshape_container_disks;
+       u->old_raid_disks = old_raid_disks;
+       u->new_raid_disks = geo->raid_disks;
+
+       /* now get spare disks list
+        */
+       spares = get_spares_for_grow(st);
+
+       if (spares == NULL
+           || delta_disks > spares->array.spare_disks) {
+               dprintf("imsm: ERROR: Cannot get spare devices.\n");
+               goto abort;
+       }
+
+       /* we have got spares
+        * update disk list in imsm_disk list table in anchor
+        */
+       dprintf("imsm: %i spares are available.\n\n",
+               spares->array.spare_disks);
+
+       dev = spares->devs;
+       for (i = 0; i < delta_disks; i++) {
+               struct dl *dl;
+
+               if (dev == NULL)
+                       break;
+               u->new_disks[i] = makedev(dev->disk.major,
+                                         dev->disk.minor);
+               dl = get_disk_super(super, dev->disk.major, dev->disk.minor);
+               dl->index = mpb->num_disks;
+               mpb->num_disks++;
+               dev = dev->next;
+       }
+
+abort:
+       /* free spares
+        */
+       sysfs_free(spares);
+
+       dprintf("imsm: reshape update preparation :");
+       if (i == delta_disks) {
+               dprintf(" OK\n");
+               *updatep = u;
+               return update_memory_size;
+       }
+       free(u);
+       dprintf(" Error\n");
+
+       return 0;
+}
+
+static void imsm_update_metadata_locally(struct supertype *st,
+                                        void *buf, int len)
+{
+       struct metadata_update mu;
+
+       mu.buf = buf;
+       mu.len = len;
+       mu.space = NULL;
+       mu.space_list = NULL;
+       mu.next = NULL;
+       imsm_prepare_update(st, &mu);
+       imsm_process_update(st, &mu);
+
+       while (mu.space_list) {
+               void **space = mu.space_list;
+               mu.space_list = *space;
+               free(space);
+       }
+}
+
+/***************************************************************************
+* Function:    imsm_analyze_change
+* Description: Function analyze change for single volume
+*              and validate if transition is supported
+* Parameters:  Geometry parameters, supertype structure
+* Returns:     Operation type code on success, -1 if fail
+****************************************************************************/
+enum imsm_reshape_type imsm_analyze_change(struct supertype *st,
+                                          struct geo_params *geo)
+{
+       struct mdinfo info;
+       int change = -1;
+       int check_devs = 0;
+
+       getinfo_super_imsm_volume(st, &info, NULL);
+
+       if ((geo->level != info.array.level) &&
+           (geo->level >= 0) &&
+           (geo->level != UnSet)) {
+               switch (info.array.level) {
+               case 0:
+                       if (geo->level == 5) {
+                               change = CH_LEVEL_MIGRATION;
+                               check_devs = 1;
+                       }
+                       if (geo->level == 10) {
+                               change = CH_TAKEOVER;
+                               check_devs = 1;
+                       }
+                       break;
+               case 5:
+                       if (geo->level != 0)
+                               change = CH_LEVEL_MIGRATION;
+                       break;
+               case 10:
+                       if (geo->level == 0) {
+                               change = CH_TAKEOVER;
+                               check_devs = 1;
+                       }
+                       break;
+               }
+               if (change == -1) {
+                       fprintf(stderr,
+                               Name " Error. Level Migration from %d to %d "
+                               "not supported!\n",
+                               info.array.level, geo->level);
+                       goto analyse_change_exit;
+               }
+       } else
+               geo->level = info.array.level;
+
+       if ((geo->layout != info.array.layout)
+           && ((geo->layout != UnSet) && (geo->layout != -1))) {
+               change = CH_LEVEL_MIGRATION;
+               if ((info.array.layout == 0)
+                   && (info.array.level == 5)
+                   && (geo->layout == 5)) {
+                       /* reshape 5 -> 4 */
+               } else if ((info.array.layout == 5)
+                          && (info.array.level == 5)
+                          && (geo->layout == 0)) {
+                       /* reshape 4 -> 5 */
+                       geo->layout = 0;
+                       geo->level = 5;
+               } else {
+                       fprintf(stderr,
+                               Name " Error. Layout Migration from %d to %d "
+                               "not supported!\n",
+                               info.array.layout, geo->layout);
+                       change = -1;
+                       goto analyse_change_exit;
+               }
+       } else
+               geo->layout = info.array.layout;
+
+       if ((geo->chunksize > 0) && (geo->chunksize != UnSet)
+           && (geo->chunksize != info.array.chunk_size))
+               change = CH_CHUNK_MIGR;
+       else
+               geo->chunksize = info.array.chunk_size;
+
+       if (!validate_geometry_imsm(st,
+                                   geo->level,
+                                   geo->layout,
+                                   geo->raid_disks,
+                                   (geo->chunksize / 1024),
+                                   geo->size,
+                                   0, 0, 1))
+               change = -1;
+
+       if (check_devs) {
+               struct intel_super *super = st->sb;
+               struct imsm_super *mpb = super->anchor;
+
+               if (mpb->num_raid_devs > 1) {
+                       fprintf(stderr,
+                               Name " Error. Cannot perform operation on %s"
+                               "- for this operation it MUST be single "
+                               "array in container\n",
+                               geo->dev_name);
+                       change = -1;
+               }
+       }
+
+analyse_change_exit:
+
+       return change;
+}
+
+int imsm_takeover(struct supertype *st, struct geo_params *geo)
+{
+       struct intel_super *super = st->sb;
+       struct imsm_update_takeover *u;
+
+       u = malloc(sizeof(struct imsm_update_takeover));
+       if (u == NULL)
+               return 1;
+
+       u->type = update_takeover;
+       u->subarray = super->current_vol;
+
+       /* 10->0 transition */
+       if (geo->level == 0)
+               u->direction = R10_TO_R0;
+
+       /* update metadata locally */
+       imsm_update_metadata_locally(st, u,
+                                       sizeof(struct imsm_update_takeover));
+       /* and possibly remotely */
+       if (st->update_tail)
+               append_metadata_update(st, u,
+                                       sizeof(struct imsm_update_takeover));
+       else
+               free(u);
+
+       return 0;
+}
+
+static int imsm_reshape_super(struct supertype *st, long long size, int level,
+                             int layout, int chunksize, int raid_disks,
+                             char *backup, char *dev, int verbose)
+{
+       int ret_val = 1;
+       struct geo_params geo;
+
+       dprintf("imsm: reshape_super called.\n");
+
+       memset(&geo, sizeof(struct geo_params), 0);
+
+       geo.dev_name = dev;
+       geo.dev_id = st->devnum;
+       geo.size = size;
+       geo.level = level;
+       geo.layout = layout;
+       geo.chunksize = chunksize;
+       geo.raid_disks = raid_disks;
+
+       dprintf("\tfor level      : %i\n", geo.level);
+       dprintf("\tfor raid_disks : %i\n", geo.raid_disks);
+
+       if (experimental() == 0)
+               return ret_val;
+
+       if (st->container_dev == st->devnum) {
+               /* On container level we can only increase number of devices. */
+               dprintf("imsm: info: Container operation\n");
+               int old_raid_disks = 0;
+               if (imsm_reshape_is_allowed_on_container(
+                           st, &geo, &old_raid_disks)) {
+                       struct imsm_update_reshape *u = NULL;
+                       int len;
+
+                       len = imsm_create_metadata_update_for_reshape(
+                               st, &geo, old_raid_disks, &u);
+
+                       if (len <= 0) {
+                               dprintf("imsm: Cannot prepare update\n");
+                               goto exit_imsm_reshape_super;
+                       }
+
+                       ret_val = 0;
+                       /* update metadata locally */
+                       imsm_update_metadata_locally(st, u, len);
+                       /* and possibly remotely */
+                       if (st->update_tail)
+                               append_metadata_update(st, u, len);
+                       else
+                               free(u);
+
+               } else {
+                       fprintf(stderr, Name "imsm: Operation is not allowed "
+                               "on this container\n");
+               }
+       } else {
+               /* On volume level we support following operations
+                * - takeover: raid10 -> raid0; raid0 -> raid10
+                * - chunk size migration
+                * - migration: raid5 -> raid0; raid0 -> raid5
+                */
+               struct intel_super *super = st->sb;
+               struct intel_dev *dev = super->devlist;
+               int change, devnum;
+               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)
+                               break;
+                       dev = dev->next;
+               }
+               if (dev == NULL) {
+                       fprintf(stderr, Name " Cannot find %s (%i) subarray\n",
+                               geo.dev_name, geo.dev_id);
+                       goto exit_imsm_reshape_super;
+               }
+               super->current_vol = dev->index;
+               change = imsm_analyze_change(st, &geo);
+               switch (change) {
+               case CH_TAKEOVER:
+                       ret_val = imsm_takeover(st, &geo);
+                       break;
+               case CH_CHUNK_MIGR:
+                       ret_val = 0;
+                       break;
+               case CH_LEVEL_MIGRATION:
+                       ret_val = 0;
+                       break;
+               default:
+                       ret_val = 1;
+               }
+       }
+
+exit_imsm_reshape_super:
+       dprintf("imsm: reshape_super Exit code = %i\n", ret_val);
+       return ret_val;
+}
+
+static int imsm_manage_reshape(
+       int afd, struct mdinfo *sra, struct reshape *reshape,
+       struct supertype *st, unsigned long stripes,
+       int *fds, unsigned long long *offsets,
+       int dests, int *destfd, unsigned long long *destoffsets)
+{
+       /* Just use child_monitor for now */
+       return child_monitor(
+               afd, sra, reshape, st, stripes,
+               fds, offsets, dests, destfd, destoffsets);
+}
 
 struct superswitch super_imsm = {
 #ifndef        MDASSEMBLE
@@ -5742,6 +6870,7 @@ struct superswitch super_imsm = {
        .write_init_super = write_init_super_imsm,
        .validate_geometry = validate_geometry_imsm,
        .add_to_super   = add_to_super_imsm,
+       .remove_from_super = remove_from_super_imsm,
        .detail_platform = detail_platform_imsm,
        .kill_subarray = kill_subarray_imsm,
        .update_subarray = update_subarray_imsm,
@@ -5766,6 +6895,8 @@ struct superswitch super_imsm = {
        .container_content = container_content_imsm,
        .default_geometry = default_geometry_imsm,
        .get_disk_controller_domain = imsm_get_disk_controller_domain,
+       .reshape_super  = imsm_reshape_super,
+       .manage_reshape = imsm_manage_reshape,
 
        .external       = 1,
        .name = "imsm",