]> git.ipfire.org Git - thirdparty/mdadm.git/blobdiff - Grow.c
Check all member devices in enough_fd
[thirdparty/mdadm.git] / Grow.c
diff --git a/Grow.c b/Grow.c
index e9a26749a893c454223cb46d07ea80ea22d55800..62622bd28b5afb25bc1bcae5c43bbf6a26155bc3 100644 (file)
--- a/Grow.c
+++ b/Grow.c
@@ -521,9 +521,15 @@ static int freeze(struct supertype *st)
        else {
                struct mdinfo *sra = sysfs_read(-1, st->devnum, GET_VERSION);
                int err;
+               char buf[20];
 
                if (!sra)
                        return -1;
+               /* Need to clear any 'read-auto' status */
+               if (sysfs_get_str(sra, NULL, "array_state", buf, 20) > 0 &&
+                   strncmp(buf, "read-auto", 9) == 0)
+                       sysfs_set_str(sra, NULL, "array_state", "clean");
+
                err = sysfs_freeze_array(sra);
                sysfs_free(sra);
                return err;
@@ -565,7 +571,8 @@ static void wait_reshape(struct mdinfo *sra)
 
 static int reshape_super(struct supertype *st, long long size, int level,
                         int layout, int chunksize, int raid_disks,
-                        char *backup_file, char *dev, int verbose)
+                        int delta_disks, char *backup_file, char *dev,
+                        int verbose)
 {
        /* nothing extra to check in the native case */
        if (!st->ss->external)
@@ -578,7 +585,8 @@ static int reshape_super(struct supertype *st, long long size, int level,
        }
 
        return st->ss->reshape_super(st, size, level, layout, chunksize,
-                                    raid_disks, backup_file, dev, verbose);
+                                    raid_disks, delta_disks, backup_file, dev,
+                                    verbose);
 }
 
 static void sync_metadata(struct supertype *st)
@@ -627,18 +635,17 @@ static int subarray_set_num(char *container, struct mdinfo *sra, char *name, int
        return rc;
 }
 
-int start_reshape(struct mdinfo *sra)
+int start_reshape(struct mdinfo *sra, int already_running)
 {
        int err;
        sysfs_set_num(sra, NULL, "suspend_lo", 0x7FFFFFFFFFFFFFFFULL);
        err = sysfs_set_num(sra, NULL, "suspend_hi", 0);
        err = err ?: sysfs_set_num(sra, NULL, "suspend_lo", 0);
-       /* Setting sync_min can fail if the recovery is already 'running',
-        * which can happen when restarting an array which is reshaping.
-        * So don't worry about errors here */
-       sysfs_set_num(sra, NULL, "sync_min", 0);
+       if (!already_running)
+               sysfs_set_num(sra, NULL, "sync_min", 0);
        err = err ?: sysfs_set_num(sra, NULL, "sync_max", 0);
-       err = err ?: sysfs_set_str(sra, NULL, "sync_action", "reshape");
+       if (!already_running)
+               err = err ?: sysfs_set_str(sra, NULL, "sync_action", "reshape");
 
        return err;
 }
@@ -653,15 +660,20 @@ void abort_reshape(struct mdinfo *sra)
        sysfs_set_str(sra, NULL, "sync_max", "max");
 }
 
-int remove_disks_on_raid10_to_raid0_takeover(struct supertype *st,
-                                            struct mdinfo *sra,
-                                            int layout)
+int remove_disks_for_takeover(struct supertype *st,
+                             struct mdinfo *sra,
+                             int layout)
 {
        int nr_of_copies;
        struct mdinfo *remaining;
        int slot;
 
-       nr_of_copies = layout & 0xff;
+       if (sra->array.level == 10)
+               nr_of_copies = layout & 0xff;
+       else if (sra->array.level == 1)
+               nr_of_copies = sra->array.raid_disks;
+       else
+               return 1;
 
        remaining = sra->devs;
        sra->devs = NULL;
@@ -714,7 +726,9 @@ int remove_disks_on_raid10_to_raid0_takeover(struct supertype *st,
 
                sysfs_set_str(sra, sd, "state", "faulty");
                sysfs_set_str(sra, sd, "slot", "none");
-               sysfs_set_str(sra, sd, "state", "remove");
+               /* for external metadata disks should be removed in mdmon */
+               if (!st->ss->external)
+                       sysfs_set_str(sra, sd, "state", "remove");
                sd->disk.state |= (1<<MD_DISK_REMOVED);
                sd->disk.state &= ~(1<<MD_DISK_SYNC);
                sd->next = sra->devs;
@@ -826,7 +840,7 @@ int reshape_open_backup_file(char *backup_file,
        }
 
        memset(buf, 0, 512);
-       for (i=0; i < blocks + 1 ; i++) {
+       for (i=0; i < blocks + 8 ; i++) {
                if (write(*fdlist, buf, 512) != 512) {
                        fprintf(stderr, Name ": %s: cannot create"
                                " backup file %s: %s\n",
@@ -885,6 +899,10 @@ char *analyse_change(struct mdinfo *info, struct reshape *re)
         * when assembling an array that is undergoing reshape.
         */
        int new_disks;
+       /* delta_parity records change in number of devices
+        * caused by level change
+        */
+       int delta_parity = 0;
 
        /* If a new level not explicitly given, we assume no-change */
        if (info->new_level == UnSet)
@@ -913,29 +931,47 @@ char *analyse_change(struct mdinfo *info, struct reshape *re)
        switch (info->array.level) {
        case 1:
                /* RAID1 can convert to RAID1 with different disks, or
-                * raid5 with 2 disks
+                * raid5 with 2 disks, or
+                * raid0 with 1 disk
                 */
+               if (info->new_level == 0) {
+                       if (info->delta_disks != UnSet &&
+                           info->delta_disks != 0)
+                               return "Cannot change number of disks "
+                                       "with RAID1->RAID0 conversion";
+                       re->level = 0;
+                       re->before.data_disks = 1;
+                       re->after.data_disks = 1;
+                       re->before.layout = 0;
+                       re->backup_blocks = 0;
+                       re->parity = 0;
+                       return NULL;
+               }
                if (info->new_level == 1) {
                        if (info->delta_disks == UnSet)
                                /* Don't know what to do */
                                return "no change requested for Growing RAID1";
                        re->level = 1;
-                       re->before.data_disks = (info->array.raid_disks +
-                                                info->delta_disks);
-                       re->before.layout = 0;
                        re->backup_blocks = 0;
                        re->parity = 0;
                        return NULL;
                }
                if (info->array.raid_disks == 2 &&
-                   info->array.raid_disks == 5) {
-                       /* simple in-place conversion */
+                   info->new_level == 5) {
+
                        re->level = 5;
-                       re->parity = 1;
                        re->before.data_disks = 1;
+                       if (info->delta_disks != UnSet &&
+                           info->delta_disks != 0)
+                               re->after.data_disks = 1 + info->delta_disks;
+                       else
+                               re->after.data_disks = 1;
+                       if (re->after.data_disks < 1)
+                               return "Number of disks too small for RAID5";
+
                        re->before.layout = ALGORITHM_LEFT_SYMMETRIC;
-                       re->backup_blocks = 0;
-                       return NULL;
+                       info->array.chunk_size = 65536;
+                       break;
                }
                /* Could do some multi-stage conversions, but leave that to
                 * later.
@@ -956,10 +992,10 @@ char *analyse_change(struct mdinfo *info, struct reshape *re)
                        return "RAID10 can only be changed to RAID0";
                new_disks = (info->array.raid_disks
                             / (info->array.layout & 0xff));
-               if (info->delta_disks == UnSet) {
+               if (info->delta_disks == UnSet)
                        info->delta_disks = (new_disks
                                             - info->array.raid_disks);
-               }
+
                if (info->delta_disks != new_disks - info->array.raid_disks)
                        return "New number of raid-devices impossible for RAID10";
                if (info->new_chunk &&
@@ -1018,16 +1054,19 @@ char *analyse_change(struct mdinfo *info, struct reshape *re)
                 * a raid4 style layout of the final level.
                 */
                switch (info->new_level) {
-               case 0:
                case 4:
+                       delta_parity = 1;
+               case 0:
                        re->level = 4;
                        re->before.layout = 0;
                        break;
                case 5:
+                       delta_parity = 1;
                        re->level = 5;
                        re->before.layout = ALGORITHM_PARITY_N;
                        break;
                case 6:
+                       delta_parity = 2;
                        re->level = 6;
                        re->before.layout = ALGORITHM_PARITY_N;
                        break;
@@ -1042,6 +1081,8 @@ char *analyse_change(struct mdinfo *info, struct reshape *re)
                info->array.layout = ALGORITHM_PARITY_N;
        case 5:
                switch (info->new_level) {
+               case 0:
+                       delta_parity = -1;
                case 4:
                        re->level = info->array.level;
                        re->before.data_disks = info->array.raid_disks - 1;
@@ -1053,6 +1094,7 @@ char *analyse_change(struct mdinfo *info, struct reshape *re)
                        re->before.layout = info->array.layout;
                        break;
                case 6:
+                       delta_parity = 1;
                        re->level = 6;
                        re->before.data_disks = info->array.raid_disks - 1;
                        switch (info->array.layout) {
@@ -1081,9 +1123,11 @@ char *analyse_change(struct mdinfo *info, struct reshape *re)
                case 1:
                        if (info->array.raid_disks != 2)
                                return "Can only convert a 2-device array to RAID1";
+                       if (info->delta_disks != UnSet &&
+                           info->delta_disks != 0)
+                               return "Cannot set raid_disk when "
+                                       "converting RAID5->RAID1";
                        re->level = 1;
-                       re->before.data_disks = 2;
-                       re->before.layout = 0;
                        break;
                default:
                        return "Impossible level change requested";
@@ -1093,6 +1137,7 @@ char *analyse_change(struct mdinfo *info, struct reshape *re)
                switch (info->new_level) {
                case 4:
                case 5:
+                       delta_parity = -1;
                case 6:
                        re->level = 6;
                        re->before.data_disks = info->array.raid_disks - 2;
@@ -1118,11 +1163,7 @@ char *analyse_change(struct mdinfo *info, struct reshape *re)
                 */
                if (re->level != 4 && re->level != 5)
                        return "Cannot covert to RAID0 from this level";
-               if (info->delta_disks == UnSet)
-                       re->after.data_disks = re->before.data_disks;
-               else
-                       re->after.data_disks =
-                               info->array.raid_disks + info->delta_disks;
+
                switch (re->level) {
                case 4:
                        re->after.layout = 0 ; break;
@@ -1135,11 +1176,7 @@ char *analyse_change(struct mdinfo *info, struct reshape *re)
                /* We can only get to RAID4 from RAID5 */
                if (re->level != 4 && re->level != 5)
                        return "Cannot convert to RAID4 from this level";
-               if (info->delta_disks == UnSet)
-                       re->after.data_disks = re->before.data_disks;
-               else
-                       re->after.data_disks =
-                               re->before.data_disks + info->delta_disks;
+
                switch (re->level) {
                case 4:
                        re->after.layout = 0 ; break;
@@ -1152,14 +1189,7 @@ char *analyse_change(struct mdinfo *info, struct reshape *re)
                /* We get to RAID5 for RAID5 or RAID6 */
                if (re->level != 5 && re->level != 6)
                        return "Cannot convert to RAID5 from this level";
-               if (info->delta_disks == UnSet)
-                       re->after.data_disks = re->before.data_disks;
-               else if (re->level == 5)
-                       re->after.data_disks =
-                               re->before.data_disks + info->delta_disks;
-               else
-                       re->after.data_disks =
-                               info->array.raid_disks + info->delta_disks - 1;
+
                switch (re->level) {
                case 5:
                        if (info->new_layout == UnSet)
@@ -1192,11 +1222,6 @@ char *analyse_change(struct mdinfo *info, struct reshape *re)
                /* We must already be at level 6 */
                if (re->level != 6)
                        return "Impossible level change";
-               if (info->delta_disks == UnSet)
-                       re->after.data_disks = re->before.data_disks;
-               else
-                       re->after.data_disks = (info->array.raid_disks +
-                                               info->delta_disks) - 2;
                if (info->new_layout == UnSet)
                        re->after.layout = info->array.layout;
                else
@@ -1205,6 +1230,12 @@ char *analyse_change(struct mdinfo *info, struct reshape *re)
        default:
                return "Impossible level change requested";
        }
+       if (info->delta_disks == UnSet)
+               info->delta_disks = delta_parity;
+
+       re->after.data_disks = (re->before.data_disks
+                               + info->delta_disks
+                               - delta_parity);
        switch (re->level) {
        case 6: re->parity = 2; break;
        case 4:
@@ -1224,7 +1255,7 @@ char *analyse_change(struct mdinfo *info, struct reshape *re)
                return NULL;
        }
        if (re->after.data_disks == 1 && re->before.data_disks == 1) {
-               /* chunks can layout changes make no difference */
+               /* chunk and layout changes make no difference */
                re->backup_blocks = 0;
                return NULL;
        }
@@ -1248,19 +1279,21 @@ char *analyse_change(struct mdinfo *info, struct reshape *re)
 
 static int reshape_array(char *container, int fd, char *devname,
                         struct supertype *st, struct mdinfo *info,
-                        int force, char *backup_file, int quiet, int forked,
+                        int force, struct mddev_dev *devlist,
+                        char *backup_file, int quiet, int forked,
                         int restart);
-static int reshape_container(char *container, int cfd, char *devname,
+static int reshape_container(char *container, char *devname,
                             struct supertype *st, 
                             struct mdinfo *info,
                             int force,
                             char *backup_file,
-                            int quiet);
+                            int quiet, int restart);
 
 int Grow_reshape(char *devname, int fd, int quiet, char *backup_file,
                 long long size,
                 int level, char *layout_str, int chunksize, int raid_disks,
-                int force)
+                struct mddev_dev *devlist,
+                int assume_clean, int force)
 {
        /* Make some changes in the shape of an array.
         * The kernel must support the change.
@@ -1290,6 +1323,9 @@ int Grow_reshape(char *devname, int fd, int quiet, char *backup_file,
        char container_buf[20];
        int cfd = -1;
 
+       struct mddev_dev *dv;
+       int added_disks;
+
        struct mdinfo info;
        struct mdinfo *sra;
 
@@ -1365,10 +1401,13 @@ int Grow_reshape(char *devname, int fd, int quiet, char *backup_file,
 
                if (mdmon_running(container_dev))
                        st->update_tail = &st->updates;
-       } 
+       }
 
+       added_disks = 0;
+       for (dv = devlist; dv; dv = dv->next)
+               added_disks++;
        if (raid_disks > array.raid_disks &&
-           array.spare_disks < (raid_disks - array.raid_disks) &&
+           array.spare_disks +added_disks < (raid_disks - array.raid_disks) &&
            !force) {
                fprintf(stderr,
                        Name ": Need %d spare%s to avoid degraded array,"
@@ -1376,7 +1415,7 @@ int Grow_reshape(char *devname, int fd, int quiet, char *backup_file,
                        "       Use --force to over-ride this check.\n",
                        raid_disks - array.raid_disks, 
                        raid_disks - array.raid_disks == 1 ? "" : "s", 
-                       array.spare_disks);
+                       array.spare_disks + added_disks);
                return 1;
        }
 
@@ -1404,13 +1443,27 @@ int Grow_reshape(char *devname, int fd, int quiet, char *backup_file,
 
        /* ========= set size =============== */
        if (size >= 0 && (size == 0 || size != array.size)) {
-               long long orig_size = array.size;
+               long long orig_size = get_component_size(fd)/2;
+               struct mdinfo *mdi;
 
-               if (reshape_super(st, size, UnSet, UnSet, 0, 0, NULL, devname, !quiet)) {
+               if (orig_size == 0)
+                       orig_size = array.size;
+
+               if (reshape_super(st, size, UnSet, UnSet, 0, 0, UnSet, NULL,
+                                 devname, !quiet)) {
                        rv = 1;
                        goto release;
                }
                sync_metadata(st);
+
+               /* Update the size of each member device in case
+                * they have been resized.  This will never reduce
+                * below the current used-size.  The "size" attribute
+                * understand '0' to mean 'max'.
+                */
+               for (mdi = sra->devs; mdi; mdi = mdi->next)
+                       sysfs_set_num(sra, mdi, "size", size);
+
                array.size = size;
                if (array.size != size) {
                        /* got truncated to 32bit, write to
@@ -1428,7 +1481,7 @@ int Grow_reshape(char *devname, int fd, int quiet, char *backup_file,
 
                        /* restore metadata */
                        if (reshape_super(st, orig_size, UnSet, UnSet, 0, 0,
-                                         NULL, devname, !quiet) == 0)
+                                         UnSet, NULL, devname, !quiet) == 0)
                                sync_metadata(st);
                        fprintf(stderr, Name ": Cannot set device size for %s: %s\n",
                                devname, strerror(err));
@@ -1438,13 +1491,28 @@ int Grow_reshape(char *devname, int fd, int quiet, char *backup_file,
                        rv = 1;
                        goto release;
                }
+               if (assume_clean) {
+                       /* This will fail on kernels newer than 2.6.40 unless
+                        * a backport has been arranged.
+                        */
+                       if (sra == NULL ||
+                           sysfs_set_str(sra, NULL, "resync_start", "none") < 0)
+                               fprintf(stderr, Name ": --assume-clean not support with --grow on this kernel\n");
+               }
                ioctl(fd, GET_ARRAY_INFO, &array);
                size = get_component_size(fd)/2;
                if (size == 0)
                        size = array.size;
-               if (!quiet)
-                       fprintf(stderr, Name ": component size of %s has been set to %lluK\n",
-                               devname, size);
+               if (!quiet) {
+                       if (size == orig_size)
+                               fprintf(stderr, Name ": component size of %s "
+                                       "unchanged at %lluK\n",
+                                       devname, size);
+                       else
+                               fprintf(stderr, Name ": component size of %s "
+                                       "has been set to %lluK\n",
+                                       devname, size);
+               }
                changed = 1;
        } else if (array.level != LEVEL_CONTAINER) {
                size = get_component_size(fd)/2;
@@ -1452,15 +1520,29 @@ int Grow_reshape(char *devname, int fd, int quiet, char *backup_file,
                        size = array.size;
        }
 
-       /* ========= check for Raid10 -> Raid0 conversion ===============
+       /* See if there is anything else to do */
+       if ((level == UnSet || level == array.level) &&
+           (layout_str == NULL) &&
+           (chunksize == 0 || chunksize == array.chunk_size) &&
+           (raid_disks == 0 || raid_disks == array.raid_disks)) {
+               /* Nothing more to do */
+               if (!changed && !quiet)
+                       fprintf(stderr, Name ": %s: no change requested\n",
+                               devname);
+               goto release;
+       }
+
+       /* ========= check for Raid10/Raid1 -> Raid0 conversion ===============
         * current implementation assumes that following conditions must be met:
-        * - far_copies == 1
-        * - near_copies == 2
+        * - RAID10:
+        *      - far_copies == 1
+        *      - near_copies == 2
         */
-       if (level == 0 && array.level == 10 && sra &&
-           array.layout == ((1 << 8) + 2) && !(array.raid_disks & 1)) {
+       if ((level == 0 && array.level == 10 && sra &&
+           array.layout == ((1 << 8) + 2) && !(array.raid_disks & 1)) ||
+           (level == 0 && array.level == 1 && sra)) {
                int err;
-               err = remove_disks_on_raid10_to_raid0_takeover(st, sra, array.layout);
+               err = remove_disks_for_takeover(st, sra, array.layout);
                if (err) {
                        dprintf(Name": Array cannot be reshaped\n");
                        if (cfd > -1)
@@ -1468,15 +1550,21 @@ int Grow_reshape(char *devname, int fd, int quiet, char *backup_file,
                        rv = 1;
                        goto release;
                }
+               /* FIXME this is added with no justification - why is it here */
+               ping_monitor(container);
        }
 
+       memset(&info, 0, sizeof(info));
        info.array = array;
        sysfs_init(&info, fd, NoMdDev);
        strcpy(info.text_version, sra->text_version);
        info.component_size = size*2;
        info.new_level = level;
        info.new_chunk = chunksize * 1024;
-       if (raid_disks)
+       if (info.array.level == LEVEL_CONTAINER) {
+               info.delta_disks = UnSet;
+               info.array.raid_disks = raid_disks;
+       } else if (raid_disks)
                info.delta_disks = raid_disks - info.array.raid_disks;
        else
                info.delta_disks = UnSet;
@@ -1543,7 +1631,33 @@ int Grow_reshape(char *devname, int fd, int quiet, char *backup_file,
                }
        }
 
-       if (array.level == LEVEL_CONTAINER) {
+       if (array.level == LEVEL_FAULTY) {
+               if (level != UnSet && level != array.level) {
+                       fprintf(stderr, Name ": cannot change level of Faulty device\n");
+                       rv =1 ;
+               }
+               if (chunksize) {
+                       fprintf(stderr, Name ": cannot set chunksize of Faulty device\n");
+                       rv =1 ;
+               }
+               if (raid_disks && raid_disks != 1) {
+                       fprintf(stderr, Name ": cannot set raid_disks of Faulty device\n");
+                       rv =1 ;
+               }
+               if (layout_str) {
+                       if (ioctl(fd, GET_ARRAY_INFO, &array) != 0) {
+                               dprintf("Cannot get array information.\n");
+                               goto release;
+                       }
+                       array.layout = info.new_layout;
+                       if (ioctl(fd, SET_ARRAY_INFO, &array) != 0) {
+                               fprintf(stderr, Name ": failed to set new layout\n");
+                               rv = 1;
+                       } else if (!quiet)
+                               printf("layout for %s set to %d\n",
+                                      devname, array.layout);
+               }
+       } else if (array.level == LEVEL_CONTAINER) {
                /* This change is to be applied to every array in the
                 * container.  This is only needed when the metadata imposes
                 * restraints of the various arrays in the container.
@@ -1552,23 +1666,36 @@ int Grow_reshape(char *devname, int fd, int quiet, char *backup_file,
                 * number of devices (On-Line Capacity Expansion) must be
                 * performed at the level of the container
                 */
-               rv = reshape_container(container, fd, devname, st, &info,
-                                      force, backup_file, quiet);
+               rv = reshape_container(container, devname, st, &info,
+                                      force, backup_file, quiet, 0);
                frozen = 0;
        } else {
+               /* get spare devices from external metadata
+                */
+               if (st->ss->external) {
+                       struct mdinfo *info2;
+
+                       info2 = st->ss->container_content(st, subarray);
+                       if (info2) {
+                               info.array.spare_disks =
+                                       info2->array.spare_disks;
+                               sysfs_free(info2);
+                       }
+               }
+
                /* Impose these changes on a single array.  First
                 * check that the metadata is OK with the change. */
 
                if (reshape_super(st, info.component_size, info.new_level,
                                  info.new_layout, info.new_chunk,
-                                 info.array.raid_disks + info.delta_disks,
+                                 info.array.raid_disks, info.delta_disks,
                                  backup_file, devname, quiet)) {
                        rv = 1;
                        goto release;
                }
                sync_metadata(st);
                rv = reshape_array(container, fd, devname, st, &info, force,
-                                  backup_file, quiet, 0, 0);
+                                  devlist, backup_file, quiet, 0, 0);
                frozen = 0;
        }
 release:
@@ -1579,7 +1706,7 @@ release:
 
 static int reshape_array(char *container, int fd, char *devname,
                         struct supertype *st, struct mdinfo *info,
-                        int force,
+                        int force, struct mddev_dev *devlist,
                         char *backup_file, int quiet, int forked,
                         int restart)
 {
@@ -1592,6 +1719,9 @@ static int reshape_array(char *container, int fd, char *devname,
        struct mdu_array_info_s array;
        char *c;
 
+       struct mddev_dev *dv;
+       int added_disks;
+
        int *fdlist;
        unsigned long long *offsets;
        int d;
@@ -1603,13 +1733,41 @@ static int reshape_array(char *container, int fd, char *devname,
        int done;
        struct mdinfo *sra = NULL;
 
-       msg = analyse_change(info, &reshape);
+       /* when reshaping a RAID0, the component_size might be zero.
+        * So try to fix that up.
+        */
+       if (ioctl(fd, GET_ARRAY_INFO, &array) != 0) {
+               dprintf("Cannot get array information.\n");
+               goto release;
+       }
+       if (array.level == 0 && info->component_size == 0) {
+               get_dev_size(fd, NULL, &array_size);
+               info->component_size = array_size / array.raid_disks;
+       }
+
+       if (info->reshape_active) {
+               int new_level = info->new_level;
+               info->new_level = UnSet;
+               info->array.raid_disks -= info->delta_disks;
+               msg = analyse_change(info, &reshape);
+               info->new_level = new_level;
+               info->array.raid_disks += info->delta_disks;
+               if (!restart)
+                       /* Make sure the array isn't read-only */
+                       ioctl(fd, RESTART_ARRAY_RW, 0);
+       } else
+               msg = analyse_change(info, &reshape);
        if (msg) {
                fprintf(stderr, Name ": %s\n", msg);
                goto release;
        }
-       if (ioctl(fd, GET_ARRAY_INFO, &array) != 0) {
-               dprintf("Canot get array information.\n");
+       if (restart &&
+           (reshape.level != info->array.level ||
+            reshape.before.layout != info->array.layout ||
+            reshape.before.data_disks + reshape.parity
+            != info->array.raid_disks - info->delta_disks)) {
+               fprintf(stderr, Name ": reshape info is not in native format -"
+                       " cannot continue.\n");
                goto release;
        }
 
@@ -1619,24 +1777,48 @@ static int reshape_array(char *container, int fd, char *devname,
                        return 0;
                goto started;
        }
+       /* The container is frozen but the array may not be.
+        * So freeze the array so spares don't get put to the wrong use
+        * FIXME there should probably be a cleaner separation between
+        * freeze_array and freeze_container.
+        */
+       sysfs_freeze_array(info);
+       /* Check we have enough spares to not be degraded */
+       added_disks = 0;
+       for (dv = devlist; dv ; dv=dv->next)
+               added_disks++;
        spares_needed = max(reshape.before.data_disks,
                            reshape.after.data_disks)
                + reshape.parity - array.raid_disks;
 
        if (!force &&
-           info->new_level > 0 &&
-           spares_needed > info->array.spare_disks) {
+           info->new_level > 1 && info->array.level > 1 &&
+           spares_needed > info->array.spare_disks + added_disks) {
                fprintf(stderr,
                        Name ": Need %d spare%s to avoid degraded array,"
                        " and only have %d.\n"
                        "       Use --force to over-ride this check.\n",
                        spares_needed,
                        spares_needed == 1 ? "" : "s", 
-                       info->array.spare_disks);
+                       info->array.spare_disks + added_disks);
+               goto release;
+       }
+       /* Check we have enough spares to not fail */
+       spares_needed = max(reshape.before.data_disks,
+                           reshape.after.data_disks)
+               - array.raid_disks;
+       if ((info->new_level > 1 || info->new_level == 0) &&
+           spares_needed > info->array.spare_disks +added_disks) {
+               fprintf(stderr,
+                       Name ": Need %d spare%s to create working array,"
+                       " and only have %d.\n",
+                       spares_needed,
+                       spares_needed == 1 ? "" : "s", 
+                       info->array.spare_disks + added_disks);
                goto release;
        }
 
-       if (reshape.level != info->array.level) {
+       if (reshape.level != array.level) {
                char *c = map_num(pers, reshape.level);
                int err;
                if (c == NULL)
@@ -1656,15 +1838,16 @@ static int reshape_array(char *container, int fd, char *devname,
                if (!quiet)
                        fprintf(stderr, Name ": level of %s changed to %s\n",
                                devname, c);    
-               orig_level = info->array.level;
-       }
+               orig_level = array.level;
+               sysfs_freeze_array(info);
 
-       if (reshape.level > 0 && st->ss->external &&
-           !mdmon_running(st->container_dev)) {
-               start_mdmon(st->container_dev);
-               ping_monitor(container);
+               if (reshape.level > 0 && st->ss->external) {
+                       /* make sure mdmon is aware of the new level */
+                       if (!mdmon_running(st->container_dev))
+                               start_mdmon(st->container_dev);
+                       ping_monitor(container);
+               }
        }
-
        /* ->reshape_super might have chosen some spares from the
         * container that it wants to be part of the new array.
         * We can collect them with ->container_content and give
@@ -1678,6 +1861,15 @@ static int reshape_array(char *container, int fd, char *devname,
 
                if (info2) {
                        sysfs_init(info2, fd, st->devnum);
+                       /* When increasing number of devices, we need to set
+                        * new raid_disks before adding these, or they might
+                        * be rejected.
+                        */
+                       if (reshape.backup_blocks &&
+                           reshape.after.data_disks > reshape.before.data_disks)
+                               subarray_set_num(container, info2, "raid_disks",
+                                                reshape.after.data_disks +
+                                                reshape.parity);
                        for (d = info2->devs; d; d = d->next) {
                                if (d->disk.state == 0 &&
                                    d->disk.raid_disk >= 0) {
@@ -1690,44 +1882,58 @@ static int reshape_array(char *container, int fd, char *devname,
                        sysfs_free(info2);
                }
        }
+       /* We might have been given some devices to add to the
+        * array.  Now that the array has been changed to the right
+        * level and frozen, we can safely add them.
+        */
+       if (devlist)
+               Manage_subdevs(devname, fd, devlist, !quiet,
+                              0,NULL);
 
        if (reshape.backup_blocks == 0) {
                /* No restriping needed, but we might need to impose
                 * some more changes: layout, raid_disks, chunk_size
                 */
+               /* read current array info */
+               if (ioctl(fd, GET_ARRAY_INFO, &array) != 0) {
+                       dprintf("Cannot get array information.\n");
+                       goto release;
+               }
+               /* compare current array info with new values and if
+                * it is different update them to new */
                if (info->new_layout != UnSet &&
-                   info->new_layout != info->array.layout) {
-                       info->array.layout = info->new_layout;
-                       if (ioctl(fd, SET_ARRAY_INFO, &info->array) != 0) {
+                   info->new_layout != array.layout) {
+                       array.layout = info->new_layout;
+                       if (ioctl(fd, SET_ARRAY_INFO, &array) != 0) {
                                fprintf(stderr, Name ": failed to set new layout\n");
                                goto release;
                        } else if (!quiet)
                                printf("layout for %s set to %d\n",
-                                      devname, info->array.layout);
+                                      devname, array.layout);
                }
                if (info->delta_disks != UnSet &&
-                   info->delta_disks != 0) {
-                       info->array.raid_disks += info->delta_disks;
-                       if (ioctl(fd, SET_ARRAY_INFO, &info->array) != 0) {
+                   info->delta_disks != 0 &&
+                   array.raid_disks != (info->array.raid_disks + info->delta_disks)) {
+                       array.raid_disks += info->delta_disks;
+                       if (ioctl(fd, SET_ARRAY_INFO, &array) != 0) {
                                fprintf(stderr, Name ": failed to set raid disks\n");
                                goto release;
-                       } else if (!quiet)
+                       } else if (!quiet) {
                                printf("raid_disks for %s set to %d\n",
-                                      devname, info->array.raid_disks);
+                                      devname, array.raid_disks);
+                       }
                }
                if (info->new_chunk != 0 &&
-                   info->new_chunk != info->array.chunk_size) {
+                   info->new_chunk != array.chunk_size) {
                        if (sysfs_set_num(info, NULL,
                                          "chunk_size", info->new_chunk) != 0) {
                                fprintf(stderr, Name ": failed to set chunk size\n");
                                goto release;
                        } else if (!quiet)
                                printf("chunk size for %s set to %d\n",
-                                      devname, info->array.chunk_size);
+                                      devname, array.chunk_size);
                }
-
-               if (!forked)
-                       unfreeze(st);
+               unfreeze(st);
                return 0;
        }
 
@@ -1811,7 +2017,9 @@ started:
 
        /* Now we need to open all these devices so we can read/write.
         */
-       nrdisks = array.raid_disks + sra->array.spare_disks;
+       nrdisks = max(reshape.before.data_disks,
+                     reshape.after.data_disks) + reshape.parity
+               + sra->array.spare_disks;
        fdlist = malloc((1+nrdisks) * sizeof(int));
        offsets = malloc((1+nrdisks) * sizeof(offsets[0]));
        if (!fdlist || !offsets) {
@@ -1819,7 +2027,8 @@ started:
                goto release;
        }
 
-       d = reshape_prepare_fdlist(devname, sra, array.raid_disks,
+       odisks = reshape.before.data_disks + reshape.parity;
+       d = reshape_prepare_fdlist(devname, sra, odisks,
                                   nrdisks, blocks, backup_file,
                                   fdlist, offsets);
        if (d < 0) {
@@ -1871,14 +2080,25 @@ started:
        sync_metadata(st);
 
        sra->new_chunk = info->new_chunk;
-       
-       if (info->reshape_active)
-               /* nothing needed here */;
-       else if (info->array.chunk_size == info->new_chunk &&
+
+       if (restart)
+               sra->reshape_progress = info->reshape_progress;
+       else {
+               sra->reshape_progress = 0;
+               if (reshape.after.data_disks < reshape.before.data_disks)
+                       /* start from the end of the new array */
+                       sra->reshape_progress = (sra->component_size
+                                                * reshape.after.data_disks);
+       }
+
+       if (info->array.chunk_size == info->new_chunk &&
            reshape.before.layout == reshape.after.layout &&
            st->ss->external == 0) {
+               /* use SET_ARRAY_INFO but only if reshape hasn't started */
+               ioctl(fd, GET_ARRAY_INFO, &array);
                array.raid_disks = reshape.after.data_disks + reshape.parity;
-               if (ioctl(fd, SET_ARRAY_INFO, &array) != 0) {
+               if (!restart &&
+                   ioctl(fd, SET_ARRAY_INFO, &array) != 0) {
                        int err = errno;
 
                        fprintf(stderr,
@@ -1893,9 +2113,9 @@ started:
 
                        goto release;
                }
-       } else {
+       } else if (!restart) {
                /* set them all just in case some old 'new_*' value
-                * persists from some earlier problem
+                * persists from some earlier problem.
                 */
                int err = 0;
                if (sysfs_set_num(sra, NULL, "chunk_size", info->new_chunk) < 0)
@@ -1920,7 +2140,14 @@ started:
                }
        }
 
-       start_reshape(sra);
+       err = start_reshape(sra, restart);
+       if (err) {
+               fprintf(stderr, 
+                       Name ": Cannot %s reshape for %s\n",
+                       restart ? "continue" : "start",
+                       devname);
+               goto release;
+       }
        if (restart)
                sysfs_set_str(sra, NULL, "array_state", "active");
 
@@ -1947,8 +2174,6 @@ started:
                fd = -1;
        mlockall(MCL_FUTURE);
 
-       odisks = reshape.before.data_disks + reshape.parity;
-
        if (st->ss->external) {
                /* metadata handler takes it from here */
                done = st->ss->manage_reshape(
@@ -2042,6 +2267,7 @@ started:
 out:
        if (forked)
                return 0;
+       unfreeze(st);
        exit(0);
 
 release:
@@ -2055,23 +2281,27 @@ release:
        return 1;
 }
 
-int reshape_container(char *container, int cfd, char *devname,
+int reshape_container(char *container, char *devname,
                      struct supertype *st, 
                      struct mdinfo *info,
                      int force,
                      char *backup_file,
-                     int quiet)
+                     int quiet, int restart)
 {
        struct mdinfo *cc = NULL;
+       int rv = restart;
 
        /* component_size is not meaningful for a container,
         * so pass '-1' meaning 'no change'
         */
-       if (reshape_super(st, -1, info->new_level,
+       if (!restart &&
+           reshape_super(st, -1, info->new_level,
                          info->new_layout, info->new_chunk,
-                         info->array.raid_disks + info->delta_disks,
-                         backup_file, devname, quiet))
+                         info->array.raid_disks, info->delta_disks,
+                         backup_file, devname, quiet)) {
+               unfreeze(st);
                return 1;
+       }
 
        sync_metadata(st);
 
@@ -2082,6 +2312,7 @@ int reshape_container(char *container, int cfd, char *devname,
        switch (fork()) {
        case -1: /* error */
                perror("Cannot fork to complete reshape\n");
+               unfreeze(st);
                return 1;
        default: /* parent */
                printf(Name ": multi-array reshape continues in background\n");
@@ -2097,9 +2328,12 @@ int reshape_container(char *container, int cfd, char *devname,
                 * reshape it.  reshape_array() will re-read the metadata
                 * so the next time through a different array should be
                 * ready for reshape.
+                * It is possible that the 'different' array will not
+                * be assembled yet.  In that case we simple exit.
+                * When it is assembled, the mdadm which assembles it
+                * will take over the reshape.
                 */
                struct mdinfo *content;
-               int rv;
                int fd;
                struct mdstat_ent *mdstat;
                char *adev;
@@ -2123,7 +2357,7 @@ int reshape_container(char *container, int cfd, char *devname,
                if (!content)
                        break;
 
-               fd = open_dev_excl(mdstat->devnum);
+               fd = open_dev(mdstat->devnum);
                if (fd < 0)
                        break;
                adev = map_dev(dev2major(mdstat->devnum),
@@ -2135,13 +2369,15 @@ int reshape_container(char *container, int cfd, char *devname,
                sysfs_init(content, fd, mdstat->devnum);
 
                rv = reshape_array(container, fd, adev, st,
-                                  content, force,
-                                  backup_file, quiet, 1, 0);
+                                  content, force, NULL,
+                                  backup_file, quiet, 1, restart);
                close(fd);
+               restart = 0;
                if (rv)
                        break;
        }
-       unfreeze(st);
+       if (!rv)
+               unfreeze(st);
        sysfs_free(cc);
        exit(0);
 }
@@ -2219,14 +2455,15 @@ int progress_reshape(struct mdinfo *info, struct reshape *reshape,
         *   1 if more data from backup_point - but only as far as suspend_point,
         *     should be backed up
         *   0 if things are progressing smoothly
-        *  -1 if the reshape is finished, either because it is all done,
-        *     or due to an error.
+        *  -1 if the reshape is finished because it is all done,
+        *  -2 if the reshape is finished due to an error.
         */
 
        int advancing = (reshape->after.data_disks
                         >= reshape->before.data_disks);
-       unsigned long long need_backup; /* need to eventually backup all the way
-                                        * to here
+       unsigned long long need_backup; /* All data between start of array and
+                                        * here will at some point need to
+                                        * be backed up.
                                         */
        unsigned long long read_offset, write_offset;
        unsigned long long write_range;
@@ -2268,37 +2505,37 @@ int progress_reshape(struct mdinfo *info, struct reshape *reshape,
        read_offset = info->reshape_progress / reshape->before.data_disks;
        write_offset = info->reshape_progress / reshape->after.data_disks;
        write_range = info->new_chunk/512;
+       if (reshape->before.data_disks == reshape->after.data_disks)
+               need_backup = array_size;
+       else
+               need_backup = reshape->backup_blocks;
        if (advancing) {
-               need_backup = 0;
-               if (read_offset < write_offset + write_range) {
+               if (read_offset < write_offset + write_range)
                        max_progress = backup_point;
-                       if (reshape->before.data_disks == reshape->after.data_disks)
-                               need_backup = array_size;
-                       else
-                               need_backup = reshape->backup_blocks;
-               } else {
+               else
                        max_progress =
                                read_offset *
                                reshape->after.data_disks;
-               }
        } else {
-               need_backup = array_size;
-               if (read_offset > write_offset - write_range) {
+               if (read_offset > write_offset - write_range)
+                       /* Can only progress as far as has been backed up,
+                        * which must be suspended */
                        max_progress = backup_point;
-                       if (max_progress >= info->reshape_progress)
-                               need_backup = 0;
-               } else {
-                       max_progress =
-                               read_offset *
-                               reshape->after.data_disks;
-                       /* If we are using internal metadata, then we can
-                        * progress all the way to the suspend_point without
-                        * worrying about backing-up/suspending along the
-                        * way.
-                        */
-                       if (max_progress < *suspend_point &&
-                               info->array.major_version >= 0)
-                               max_progress = *suspend_point;
+               else if (info->reshape_progress <= need_backup)
+                       max_progress = backup_point;
+               else {
+                       if (info->array.major_version >= 0)
+                               /* Can progress until backup is needed */
+                               max_progress = need_backup;
+                       else {
+                               /* Can progress until metadata update is required */
+                               max_progress =
+                                       read_offset *
+                                       reshape->after.data_disks;
+                               /* but data must be suspended */
+                               if (max_progress < *suspend_point)
+                                       max_progress = *suspend_point;
+                       }
                }
        }
 
@@ -2337,16 +2574,28 @@ int progress_reshape(struct mdinfo *info, struct reshape *reshape,
                                max_progress = *suspend_point;
                }
        } else {
-               if ((need_backup < info->reshape_progress
-                    || info->array.major_version < 0) &&
-                   *suspend_point > info->reshape_progress - target) {
-                       if (need_backup > *suspend_point - 2 * target)
-                               *suspend_point = need_backup;
-                       else if (*suspend_point >= 2 * target)
-                               *suspend_point -= 2 * target;
-                       else
+               if (info->array.major_version >= 0) {
+                       /* Only need to suspend when about to backup */
+                       if (info->reshape_progress < need_backup * 2 &&
+                           *suspend_point > 0) {
                                *suspend_point = 0;
-                       sysfs_set_num(info, NULL, "suspend_lo", *suspend_point);
+                               sysfs_set_num(info, NULL, "suspend_lo", 0);
+                               sysfs_set_num(info, NULL, "suspend_hi", need_backup);
+                       }
+               } else {
+                       /* Need to suspend continually */
+                       if (info->reshape_progress < *suspend_point)
+                               *suspend_point = info->reshape_progress;
+                       if (*suspend_point + target < info->reshape_progress)
+                               /* No need to move suspend region yet */;
+                       else {
+                               if (*suspend_point >= 2 * target)
+                                       *suspend_point -= 2 * target;
+                               else
+                                       *suspend_point = 0;
+                               sysfs_set_num(info, NULL, "suspend_lo",
+                                             *suspend_point);
+                       }
                        if (max_progress < *suspend_point)
                                max_progress = *suspend_point;
                }
@@ -2365,6 +2614,9 @@ int progress_reshape(struct mdinfo *info, struct reshape *reshape,
        /* Round to chunk size as some kernels give an erroneously high number */
        max_progress /= info->new_chunk/512;
        max_progress *= info->new_chunk/512;
+       /* And round to old chunk size as the kernel wants that */
+       max_progress /= info->array.chunk_size/512;
+       max_progress *= info->array.chunk_size/512;
        /* Limit progress to the whole device */
        if (max_progress > info->component_size)
                max_progress = info->component_size;
@@ -2387,10 +2639,9 @@ int progress_reshape(struct mdinfo *info, struct reshape *reshape,
        if (fd < 0)
                goto check_progress;
 
-       if (sysfs_fd_get_ll(fd, &completed) < 0) {
-               close(fd);
+       if (sysfs_fd_get_ll(fd, &completed) < 0)
                goto check_progress;
-       }
+
        while (completed < max_progress && completed < wait_point) {
                /* Check that sync_action is still 'reshape' to avoid
                 * waiting forever on a dead array
@@ -2401,14 +2652,29 @@ int progress_reshape(struct mdinfo *info, struct reshape *reshape,
                                  action, 20) <= 0 ||
                    strncmp(action, "reshape", 7) != 0)
                        break;
+               /* Some kernels reset 'sync_completed' to zero
+                * before setting 'sync_action' to 'idle'.
+                * So we need these extra tests.
+                */
+               if (completed == 0 && advancing
+                   && info->reshape_progress > 0)
+                       break;
+               if (completed == 0 && !advancing
+                   && info->reshape_progress < (info->component_size
+                                                * reshape->after.data_disks))
+                       break;
                FD_ZERO(&rfds);
                FD_SET(fd, &rfds);
                select(fd+1, NULL, NULL, &rfds, NULL);
-               if (sysfs_fd_get_ll(fd, &completed) < 0) {
-                       close(fd);
+               if (sysfs_fd_get_ll(fd, &completed) < 0)
                        goto check_progress;
-               }
        }
+       /* Some kernels reset 'sync_completed' to zero,
+        * we need to have real point we are in md
+        */
+       if (completed == 0)
+               completed = max_progress;
+
        /* some kernels can give an incorrectly high 'completed' number */
        completed /= (info->new_chunk/512);
        completed *= (info->new_chunk/512);
@@ -2425,21 +2691,46 @@ int progress_reshape(struct mdinfo *info, struct reshape *reshape,
        /* We return the need_backup flag.  Caller will decide
         * how much - a multiple of ->backup_blocks up to *suspend_point
         */
-       return advancing
-               ? (need_backup > info->reshape_progress)
-               : (need_backup < info->reshape_progress);
+       if (advancing)
+               return need_backup > info->reshape_progress;
+       else
+               return need_backup >= info->reshape_progress;
 
 check_progress:
        /* if we couldn't read a number from sync_completed, then
         * either the reshape did complete, or it aborted.
         * We can tell which by checking for 'none' in reshape_position.
+        * If it did abort, then it might immediately restart if it
+        * it was just a device failure that leaves us degraded but
+        * functioning.
         */
        strcpy(buf, "hi");
        if (sysfs_get_str(info, NULL, "reshape_position", buf, sizeof(buf)) < 0
-           || strncmp(buf, "none", 4) != 0)
-               return -2; /* abort */
-       else {
+           || strncmp(buf, "none", 4) != 0) {
+               /* The abort might only be temporary.  Wait up to 10
+                * seconds for fd to contain a valid number again.
+                */
+               struct timeval tv;
+               int rv = -2;
+               tv.tv_sec = 10;
+               tv.tv_usec = 0;
+               while (fd >= 0 && rv < 0) {
+                       fd_set rfds;
+                       FD_ZERO(&rfds);
+                       FD_SET(fd, &rfds);
+                       if (select(fd+1, NULL, NULL, &rfds, &tv) != 1)
+                               break;
+                       if (sysfs_fd_get_ll(fd, &completed) >= 0)
+                               /* all good again */
+                               rv = 1;
+               }
+               if (fd >= 0)
+                       close(fd);
+               return rv; /* abort */
+       } else {
                /* Maybe racing with array shutdown - check state */
+               if (fd >= 0)
+                       close(fd);
                if (sysfs_get_str(info, NULL, "array_state", buf, sizeof(buf)) < 0
                    || strncmp(buf, "inactive", 8) == 0
                    || strncmp(buf, "clear",5) == 0)
@@ -2477,7 +2768,8 @@ static int grow_backup(struct mdinfo *sra,
                odata--;
 
        /* Check that array hasn't become degraded, else we might backup the wrong data */
-       sysfs_get_ll(sra, NULL, "degraded", &ll);
+       if (sysfs_get_ll(sra, NULL, "degraded", &ll) < 0)
+               return -1; /* FIXME this error is ignored */
        new_degraded = (int)ll;
        if (new_degraded != *degraded) {
                /* check each device to ensure it is still working */
@@ -2737,7 +3029,7 @@ int child_monitor(int afd, struct mdinfo *sra, struct reshape *reshape,
                        continue;
                ok = st->ss->load_super(st, devfd, NULL);
                close(devfd);
-               if (ok >= 0)
+               if (ok == 0)
                        break;
        }
        if (!sd) {
@@ -2768,7 +3060,7 @@ int child_monitor(int afd, struct mdinfo *sra, struct reshape *reshape,
                suspend_point = 0;
        } else {
                array_size = sra->component_size * reshape->before.data_disks;
-               backup_point = array_size;
+               backup_point = reshape->backup_blocks;
                suspend_point = array_size;
        }
 
@@ -2804,19 +3096,23 @@ int child_monitor(int afd, struct mdinfo *sra, struct reshape *reshape,
 
                /* Clear any backup region that is before 'here' */
                if (increasing) {
-                       if (reshape_completed >= (__le64_to_cpu(bsb.arraystart) +
+                       if (__le64_to_cpu(bsb.length) > 0 &&
+                           reshape_completed >= (__le64_to_cpu(bsb.arraystart) +
                                                  __le64_to_cpu(bsb.length)))
                                forget_backup(dests, destfd,
                                              destoffsets, 0);
-                       if (reshape_completed >= (__le64_to_cpu(bsb.arraystart2) +
+                       if (__le64_to_cpu(bsb.length2) > 0 &&
+                           reshape_completed >= (__le64_to_cpu(bsb.arraystart2) +
                                                  __le64_to_cpu(bsb.length2)))
                                forget_backup(dests, destfd,
                                              destoffsets, 1);
                } else {
-                       if (reshape_completed <= (__le64_to_cpu(bsb.arraystart)))
+                       if (__le64_to_cpu(bsb.length) > 0 &&
+                           reshape_completed <= (__le64_to_cpu(bsb.arraystart)))
                                forget_backup(dests, destfd,
                                              destoffsets, 0);
-                       if (reshape_completed <= (__le64_to_cpu(bsb.arraystart2)))
+                       if (__le64_to_cpu(bsb.length2) > 0 &&
+                           reshape_completed <= (__le64_to_cpu(bsb.arraystart2)))
                                forget_backup(dests, destfd,
                                              destoffsets, 1);
                }
@@ -2826,6 +3122,11 @@ int child_monitor(int afd, struct mdinfo *sra, struct reshape *reshape,
                                done = 1;
                        break;
                }
+               if (rv == 0 && increasing && !st->ss->external) {
+                       /* No longer need to monitor this reshape */
+                       done = 1;
+                       break;
+               }
 
                while (rv) {
                        unsigned long long offset;
@@ -2858,6 +3159,8 @@ int child_monitor(int afd, struct mdinfo *sra, struct reshape *reshape,
                                if (offset < suspend_point/data)
                                        break;
                        }
+                       if (actual_stripes == 0)
+                               break;
                        grow_backup(sra, offset, actual_stripes,
                                    fds, offsets,
                                    disks, chunk, level, layout,
@@ -2895,9 +3198,6 @@ int Grow_restart(struct supertype *st, struct mdinfo *info, int *fdlist, int cnt
        unsigned long long  nstripe, ostripe;
        int ndata, odata;
 
-       if (info->new_level != info->array.level)
-               return 1; /* Cannot handle level changes (they are instantaneous) */
-
        odata = info->array.raid_disks - info->delta_disks - 1;
        if (info->array.level == 6) odata--; /* number of data disks */
        ndata = info->array.raid_disks - 1;
@@ -2915,6 +3215,7 @@ int Grow_restart(struct supertype *st, struct mdinfo *info, int *fdlist, int cnt
                int fd;
                int bsbsize;
                char *devname, namebuf[20];
+               unsigned long long lo, hi;
 
                /* This was a spare and may have some saved data on it.
                 * Load the superblock, find and load the
@@ -2998,42 +3299,52 @@ int Grow_restart(struct supertype *st, struct mdinfo *info, int *fdlist, int cnt
                }
 
                if (bsb.magic[15] == '1') {
-               if (info->delta_disks >= 0) {
-                       /* reshape_progress is increasing */
-                       if (__le64_to_cpu(bsb.arraystart) + __le64_to_cpu(bsb.length) <
-                           info->reshape_progress) {
-                       nonew:
-                               if (verbose)
-                                       fprintf(stderr, Name ": backup-metadata found on %s but is not needed\n", devname);
-                               continue; /* No new data here */
+                       if (bsb.length == 0)
+                               continue;
+                       if (info->delta_disks >= 0) {
+                               /* reshape_progress is increasing */
+                               if (__le64_to_cpu(bsb.arraystart)
+                                   + __le64_to_cpu(bsb.length)
+                                   < info->reshape_progress) {
+                               nonew:
+                                       if (verbose)
+                                               fprintf(stderr, Name
+                  ": backup-metadata found on %s but is not needed\n", devname);
+                                       continue; /* No new data here */
+                               }
+                       } else {
+                               /* reshape_progress is decreasing */
+                               if (__le64_to_cpu(bsb.arraystart) >=
+                                   info->reshape_progress)
+                                       goto nonew; /* No new data here */
                        }
                } else {
-                       /* reshape_progress is decreasing */
-                       if (__le64_to_cpu(bsb.arraystart) >=
-                           info->reshape_progress)
-                               goto nonew; /* No new data here */
-               }
-               } else {
-               if (info->delta_disks >= 0) {
-                       /* reshape_progress is increasing */
-                       if (__le64_to_cpu(bsb.arraystart) + __le64_to_cpu(bsb.length) <
-                           info->reshape_progress &&
-                           __le64_to_cpu(bsb.arraystart2) + __le64_to_cpu(bsb.length2) <
-                           info->reshape_progress)
-                               goto nonew; /* No new data here */
-               } else {
-                       /* reshape_progress is decreasing */
-                       if (__le64_to_cpu(bsb.arraystart) >=
-                           info->reshape_progress &&
-                           __le64_to_cpu(bsb.arraystart2) >=
-                           info->reshape_progress)
-                               goto nonew; /* No new data here */
-               }
+                       if (bsb.length == 0 && bsb.length2 == 0)
+                               continue;
+                       if (info->delta_disks >= 0) {
+                               /* reshape_progress is increasing */
+                               if ((__le64_to_cpu(bsb.arraystart)
+                                    + __le64_to_cpu(bsb.length)
+                                    < info->reshape_progress)
+                                   &&
+                                   (__le64_to_cpu(bsb.arraystart2)
+                                    + __le64_to_cpu(bsb.length2)
+                                    < info->reshape_progress))
+                                       goto nonew; /* No new data here */
+                       } else {
+                               /* reshape_progress is decreasing */
+                               if (__le64_to_cpu(bsb.arraystart) >=
+                                   info->reshape_progress &&
+                                   __le64_to_cpu(bsb.arraystart2) >=
+                                   info->reshape_progress)
+                                       goto nonew; /* No new data here */
+                       }
                }
                if (lseek64(fd, __le64_to_cpu(bsb.devstart)*512, 0)< 0) {
                second_fail:
                        if (verbose)
-                               fprintf(stderr, Name ": Failed to verify secondary backup-metadata block on %s\n",
+                               fprintf(stderr, Name
+                    ": Failed to verify secondary backup-metadata block on %s\n",
                                        devname);
                        continue; /* Cannot seek */
                }
@@ -3097,7 +3408,28 @@ int Grow_restart(struct supertype *st, struct mdinfo *info, int *fdlist, int cnt
 
                /* Ok, so the data is restored. Let's update those superblocks. */
 
-               if (info->delta_disks >= 0) {
+               lo = hi = 0;
+               if (bsb.length) {
+                       lo = __le64_to_cpu(bsb.arraystart);
+                       hi = lo + __le64_to_cpu(bsb.length);
+               }
+               if (bsb.magic[15] == '2' && bsb.length2) {
+                       unsigned long long lo1, hi1;
+                       lo1 = __le64_to_cpu(bsb.arraystart2);
+                       hi1 = lo1 + __le64_to_cpu(bsb.length2);
+                       if (lo == hi) {
+                               lo = lo1;
+                               hi = hi1;
+                       } else if (lo < lo1)
+                               hi = hi1;
+                       else
+                               lo = lo1;
+               }
+               if (lo < hi &&
+                   (info->reshape_progress < lo ||
+                    info->reshape_progress > hi))
+                       /* backup does not affect reshape_progress*/ ;
+               else if (info->delta_disks >= 0) {
                        info->reshape_progress = __le64_to_cpu(bsb.arraystart) +
                                __le64_to_cpu(bsb.length);
                        if (bsb.magic[15] == '2') {
@@ -3172,10 +3504,34 @@ int Grow_restart(struct supertype *st, struct mdinfo *info, int *fdlist, int cnt
 int Grow_continue(int mdfd, struct supertype *st, struct mdinfo *info,
                  char *backup_file)
 {
-       int err = sysfs_set_str(info, NULL, "array_state", "readonly");
+       char buf[40];
+       char *container = NULL;
+       int err;
+
+       err = sysfs_set_str(info, NULL, "array_state", "readonly");
        if (err)
                return err;
-       return reshape_array(NULL, mdfd, "array", st, info, 1, backup_file, 0, 0, 1);
-}
+       if (st->ss->external) {
+               fmt_devname(buf, st->container_dev);
+               container = buf;
+               freeze(st);
+
+               if (!mdmon_running(st->container_dev))
+                       start_mdmon(st->container_dev);
+               ping_monitor_by_id(st->container_dev);
 
 
+               if (info->reshape_active == 2) {
+                       int cfd = open_dev(st->container_dev);
+                       if (cfd < 0)
+                               return 1;
+                       st->ss->load_container(st, cfd, container);
+                       close(cfd);
+                       return reshape_container(container, NULL,
+                                                st, info, 0, backup_file,
+                                                0, 1);
+               }
+       }
+       return reshape_array(container, mdfd, "array", st, info, 1,
+                            NULL, backup_file, 0, 0, 1);
+}