]> git.ipfire.org Git - thirdparty/mdadm.git/blobdiff - Grow.c
Add raid1->raid0 takeover support
[thirdparty/mdadm.git] / Grow.c
diff --git a/Grow.c b/Grow.c
index 8b53c927bd058f08eaad83b4d09a27fb1defedc2..cd044bf6dedae2f7388f4a62f531b8e2571d060b 100644 (file)
--- a/Grow.c
+++ b/Grow.c
@@ -530,12 +530,8 @@ static int freeze(struct supertype *st)
        }
 }
 
-static void unfreeze(struct supertype *st, int frozen)
+static void unfreeze(struct supertype *st)
 {
-       /* If 'frozen' is 1, unfreeze the array */
-       if (frozen <= 0)
-               return;
-
        if (st->ss->external)
                return unfreeze_container(st);
        else {
@@ -637,7 +633,10 @@ int start_reshape(struct mdinfo *sra)
        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);
-       err = err ?: sysfs_set_num(sra, NULL, "sync_min", 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);
        err = err ?: sysfs_set_num(sra, NULL, "sync_max", 0);
        err = err ?: sysfs_set_str(sra, NULL, "sync_action", "reshape");
 
@@ -654,15 +653,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;
@@ -793,7 +797,8 @@ int reshape_open_backup_file(char *backup_file,
                             char *devname,
                             long blocks,
                             int *fdlist,
-                            unsigned long long *offsets)
+                            unsigned long long *offsets,
+                            int restart)
 {
        /* Return 1 on success, 0 on any form of failure */
        /* need to check backup file is large enough */
@@ -802,7 +807,7 @@ int reshape_open_backup_file(char *backup_file,
        unsigned int dev;
        int i;
 
-       *fdlist = open(backup_file, O_RDWR|O_CREAT|O_EXCL,
+       *fdlist = open(backup_file, O_RDWR|O_CREAT|(restart ? O_TRUNC : O_EXCL),
                       S_IRUSR | S_IWUSR);
        *offsets = 8 * 512;
        if (*fdlist < 0) {
@@ -867,30 +872,6 @@ unsigned long compute_backup_blocks(int nchunk, int ochunk,
        return blocks;
 }
 
-/* 'struct reshape' records the intermediate states
- * a general reshape.
- * The starting geometry is converted to the 'before' geometry
- * by at most an atomic level change. They could be the same.
- * Similarly the 'after' geometry is converted to the final
- * geometry by at most a level change.
- * Note that 'before' and 'after' must have the same level.
- * 'blocks' is the minimum number of sectors for a reshape unit.
- * This will be a multiple of the stripe size in each of the
- * 'before' and 'after' geometries.
- * If 'blocks' is 0, no restriping is necessary.
- */
-struct reshape {
-       int level;
-       int parity; /* number of parity blocks/devices */
-       struct {
-               int layout;
-               int data_disks;
-       } before, after;
-       unsigned long long backup_blocks;
-       unsigned long long stripes; /* number of old stripes that comprise 'blocks'*/
-       unsigned long long new_size; /* New size of array in sectors */
-};
-
 char *analyse_change(struct mdinfo *info, struct reshape *re)
 {
        /* Based on the current array state in info->array and
@@ -937,8 +918,18 @@ 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) {
+                       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 */
@@ -952,14 +943,12 @@ char *analyse_change(struct mdinfo *info, struct reshape *re)
                        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;
                        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.
@@ -980,7 +969,7 @@ 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);
                }
@@ -994,6 +983,7 @@ char *analyse_change(struct mdinfo *info, struct reshape *re)
                re->level = 0;
                re->parity = 0;
                re->before.data_disks = new_disks;
+               re->after.data_disks = re->before.data_disks;
                re->before.layout = 0;
                re->backup_blocks = 0;
                return NULL;
@@ -1031,6 +1021,7 @@ char *analyse_change(struct mdinfo *info, struct reshape *re)
                        re->parity = 0;
                        re->before.data_disks = (info->array.raid_disks +
                                                 info->delta_disks);
+                       re->after.data_disks = re->before.data_disks;
                        re->before.layout = info->new_layout;
                        re->backup_blocks = 0;
                        return NULL;
@@ -1246,7 +1237,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;
        }
@@ -1270,18 +1261,14 @@ 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, char *backup_file, int quiet, int forked,
+                        int restart);
 static int reshape_container(char *container, int cfd, char *devname,
                             struct supertype *st, 
                             struct mdinfo *info,
                             int force,
                             char *backup_file,
                             int quiet);
-static int child_monitor(int afd, struct mdinfo *sra, struct reshape *reshape,
-                        unsigned long stripes,
-                        int *fds, unsigned long long *offsets,
-                        int dests, int *destfd, unsigned long long *destoffsets);
-
 
 int Grow_reshape(char *devname, int fd, int quiet, char *backup_file,
                 long long size,
@@ -1380,10 +1367,8 @@ int Grow_reshape(char *devname, int fd, int quiet, char *backup_file,
                fmt_devname(container_buf, container_dev);
                container = container_buf;
 
-               if (subarray)
-                       rv = st->ss->load_container(st, cfd, NULL);
-               else
-                       rv = st->ss->load_super(st, cfd, NULL);
+               rv = st->ss->load_container(st, cfd, NULL);
+
                if (rv) {
                        fprintf(stderr, Name ": Cannot read superblock for %s\n",
                                devname);
@@ -1408,8 +1393,9 @@ int Grow_reshape(char *devname, int fd, int quiet, char *backup_file,
                return 1;
        }
 
-       sra = sysfs_read(fd, 0, GET_LEVEL | GET_DISKS | GET_DEVS | GET_STATE);
-       if (sra) {
+       sra = sysfs_read(fd, 0, GET_LEVEL | GET_DISKS | GET_DEVS
+                        | GET_STATE | GET_VERSION);
+       if (sra) {
                if (st->ss->external && subarray == NULL) {
                        array.level = LEVEL_CONTAINER;
                        sra->array.level = LEVEL_CONTAINER;
@@ -1479,15 +1465,17 @@ int Grow_reshape(char *devname, int fd, int quiet, char *backup_file,
                        size = array.size;
        }
 
-       /* ========= check for Raid10 -> Raid0 conversion ===============
+       /* ========= 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)
@@ -1495,10 +1483,13 @@ 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);
        }
 
        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;
@@ -1580,6 +1571,7 @@ int Grow_reshape(char *devname, int fd, int quiet, char *backup_file,
                 */
                rv = reshape_container(container, fd, devname, st, &info,
                                       force, backup_file, quiet);
+               frozen = 0;
        } else {
                /* Impose these changes on a single array.  First
                 * check that the metadata is OK with the change. */
@@ -1593,19 +1585,20 @@ int Grow_reshape(char *devname, int fd, int quiet, char *backup_file,
                }
                sync_metadata(st);
                rv = reshape_array(container, fd, devname, st, &info, force,
-                                  backup_file, quiet, 0);
+                                  backup_file, quiet, 0, 0);
+               frozen = 0;
        }
-       /* reshape_* released the array */
-       return rv;
 release:
-       unfreeze(st, frozen);
+       if (frozen > 0)
+               unfreeze(st);
        return rv;
 }
 
 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)
+                        char *backup_file, int quiet, int forked,
+                        int restart)
 {
        struct reshape reshape;
        int spares_needed;
@@ -1615,34 +1608,41 @@ static int reshape_array(char *container, int fd, char *devname,
 
        struct mdu_array_info_s array;
        char *c;
-       int rv = 0;
 
        int *fdlist;
        unsigned long long *offsets;
        int d;
        int nrdisks;
        int err;
-       int frozen;
-       unsigned long blocks, stripes;
+       unsigned long blocks;
        unsigned long cache;
        unsigned long long array_size;
        int done;
-       struct mdinfo *sra, *sd;
+       struct mdinfo *sra = NULL;
 
        msg = analyse_change(info, &reshape);
        if (msg) {
                fprintf(stderr, Name ": %s\n", msg);
-               return 1;
+               goto release;
        }
        if (ioctl(fd, GET_ARRAY_INFO, &array) != 0) {
                dprintf("Canot get array information.\n");
-               return 1;
+               goto release;
+       }
+
+       if (restart) {
+               /* reshape already started. just skip to monitoring the reshape */
+               if (reshape.backup_blocks == 0)
+                       return 0;
+               goto started;
        }
        spares_needed = max(reshape.before.data_disks,
                            reshape.after.data_disks)
                + reshape.parity - array.raid_disks;
 
-       if (!force && spares_needed > info->array.spare_disks) {
+       if (!force &&
+           info->new_level > 0 &&
+           spares_needed > info->array.spare_disks) {
                fprintf(stderr,
                        Name ": Need %d spare%s to avoid degraded array,"
                        " and only have %d.\n"
@@ -1650,14 +1650,14 @@ static int reshape_array(char *container, int fd, char *devname,
                        spares_needed,
                        spares_needed == 1 ? "" : "s", 
                        info->array.spare_disks);
-               return 1;
+               goto release;
        }
 
        if (reshape.level != info->array.level) {
                char *c = map_num(pers, reshape.level);
                int err;
                if (c == NULL)
-                       return 1; /* This should not be possible */
+                       goto release;
 
                err = sysfs_set_str(info, NULL, "level", c);
                if (err) {
@@ -1668,20 +1668,20 @@ static int reshape_array(char *container, int fd, char *devname,
                            (info->array.state & (1<<MD_SB_BITMAP_PRESENT)))
                                fprintf(stderr, "       Bitmap must be removed"
                                        " before level can be changed\n");
-                       return 1;
+                       goto release;
                }
                if (!quiet)
                        fprintf(stderr, Name ": level of %s changed to %s\n",
                                devname, c);    
                orig_level = info->array.level;
-       }
 
-       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
@@ -1693,7 +1693,8 @@ static int reshape_array(char *container, int fd, char *devname,
                        st->ss->container_content(st, subarray);
                struct mdinfo *d;
 
-               if (info2)
+               if (info2) {
+                       sysfs_init(info2, fd, st->devnum);
                        for (d = info2->devs; d; d = d->next) {
                                if (d->disk.state == 0 &&
                                    d->disk.raid_disk >= 0) {
@@ -1703,7 +1704,8 @@ static int reshape_array(char *container, int fd, char *devname,
                                        add_disk(fd, st, info2, d);
                                }
                        }
-               sysfs_free(info2);
+                       sysfs_free(info2);
+               }
        }
 
        if (reshape.backup_blocks == 0) {
@@ -1715,7 +1717,7 @@ static int reshape_array(char *container, int fd, char *devname,
                        info->array.layout = info->new_layout;
                        if (ioctl(fd, SET_ARRAY_INFO, &info->array) != 0) {
                                fprintf(stderr, Name ": failed to set new layout\n");
-                               rv = 1;
+                               goto release;
                        } else if (!quiet)
                                printf("layout for %s set to %d\n",
                                       devname, info->array.layout);
@@ -1725,7 +1727,7 @@ static int reshape_array(char *container, int fd, char *devname,
                        info->array.raid_disks += info->delta_disks;
                        if (ioctl(fd, SET_ARRAY_INFO, &info->array) != 0) {
                                fprintf(stderr, Name ": failed to set raid disks\n");
-                               rv = 1;
+                               goto release;
                        } else if (!quiet)
                                printf("raid_disks for %s set to %d\n",
                                       devname, info->array.raid_disks);
@@ -1735,13 +1737,13 @@ static int reshape_array(char *container, int fd, char *devname,
                        if (sysfs_set_num(info, NULL,
                                          "chunk_size", info->new_chunk) != 0) {
                                fprintf(stderr, Name ": failed to set chunk size\n");
-                               rv = 1;
+                               goto release;
                        } else if (!quiet)
                                printf("chunk size for %s set to %d\n",
                                       devname, info->array.chunk_size);
                }
-
-               return rv;
+               unfreeze(st);
+               return 0;
        }
 
        /*
@@ -1777,7 +1779,7 @@ static int reshape_array(char *container, int fd, char *devname,
         *   -  request the shape change.
         *   -  fork to handle backup etc.
         */
-
+started:
        /* Check that we can hold all the data */
        get_dev_size(fd, NULL, &array_size);
        if (reshape.new_size < (array_size/512)) {
@@ -1786,18 +1788,15 @@ static int reshape_array(char *container, int fd, char *devname,
                        "       use --grow --array-size first to truncate array.\n"
                        "       e.g. mdadm --grow %s --array-size %llu\n",
                        devname, reshape.new_size/2);
-               rv = 1;
                goto release;
        }
 
        sra = sysfs_read(fd, 0,
                         GET_COMPONENT|GET_DEVS|GET_OFFSET|GET_STATE|GET_CHUNK|
                         GET_CACHE);
-
        if (!sra) {
                fprintf(stderr, Name ": %s: Cannot get array details from sysfs\n",
                        devname);
-               rv = 1;
                goto release;
        }
 
@@ -1822,7 +1821,6 @@ static int reshape_array(char *container, int fd, char *devname,
                fprintf(stderr, Name ": %s: Something wrong"
                        " - reshape aborted\n",
                        devname);
-               rv = 1;
                goto release;
        }
 
@@ -1833,7 +1831,6 @@ static int reshape_array(char *container, int fd, char *devname,
        offsets = malloc((1+nrdisks) * sizeof(offsets[0]));
        if (!fdlist || !offsets) {
                fprintf(stderr, Name ": malloc failed: grow aborted\n");
-               rv = 1;
                goto release;
        }
 
@@ -1841,7 +1838,6 @@ static int reshape_array(char *container, int fd, char *devname,
                                   nrdisks, blocks, backup_file,
                                   fdlist, offsets);
        if (d < 0) {
-               rv = 1;
                goto release;
        }
        if (backup_file == NULL) {
@@ -1849,20 +1845,17 @@ static int reshape_array(char *container, int fd, char *devname,
                        fprintf(stderr,
                                Name ": %s: Cannot grow - need backup-file\n", 
                                devname);
-                       rv = 1;
                        goto release;
                } else if (sra->array.spare_disks == 0) {
                        fprintf(stderr, Name ": %s: Cannot grow - need a spare or "
                                "backup-file to backup critical section\n",
                                devname);
-                       rv = 1;
                        goto release;
                }
        } else {
                if (!reshape_open_backup_file(backup_file, fd, devname,
                                              (signed)blocks,
-                                             fdlist+d, offsets+d)) {
-                       rv = 1;
+                                             fdlist+d, offsets+d, restart)) {
                        goto release;
                }
                d++;
@@ -1893,14 +1886,26 @@ static int reshape_array(char *container, int fd, char *devname,
        sync_metadata(st);
 
        sra->new_chunk = info->new_chunk;
-       
+
+       if (info->reshape_active)
+               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 */
                array.raid_disks = reshape.after.data_disks + reshape.parity;
-               if (ioctl(fd, SET_ARRAY_INFO, &array) != 0) {
+               if (!info->reshape_active &&
+                   ioctl(fd, SET_ARRAY_INFO, &array) != 0) {
                        int err = errno;
-                       rv = 1;
+
                        fprintf(stderr,
                                Name ": Cannot set device shape for %s: %s\n",
                                devname, strerror(errno));
@@ -1915,22 +1920,22 @@ static int reshape_array(char *container, int fd, char *devname,
                }
        } else {
                /* set them all just in case some old 'new_*' value
-                * persists from some earlier problem
+                * persists from some earlier problem.
+                * We even set them when restarting in the middle.  They will
+                * already be set in that case so this will be a no-op,
+                * but it is hard to tell the difference.
                 */
-               int err = err; /* only used if rv==1, and always set if
-                               * rv==1, so initialisation not needed,
-                               * despite gcc warning
-                               */
+               int err = 0;
                if (sysfs_set_num(sra, NULL, "chunk_size", info->new_chunk) < 0)
-                       rv = 1, err = errno;
-               if (!rv && sysfs_set_num(sra, NULL, "layout", 
+                       err = errno;
+               if (!err && sysfs_set_num(sra, NULL, "layout", 
                                         reshape.after.layout) < 0)
-                       rv = 1, err = errno;
-               if (!rv && subarray_set_num(container, sra, "raid_disks",
+                       err = errno;
+               if (!err && subarray_set_num(container, sra, "raid_disks",
                                            reshape.after.data_disks +
                                            reshape.parity) < 0)
-                       rv = 1, err = errno;
-               if (rv) {
+                       err = errno;
+               if (err) {
                        fprintf(stderr, Name ": Cannot set device shape for %s\n",
                                devname);
 
@@ -1944,171 +1949,138 @@ static int reshape_array(char *container, int fd, char *devname,
        }
 
        start_reshape(sra);
-       if (st->ss->external) {
-               /* metadata handler takes it from here */
-               ping_manager(container);
-               st->ss->manage_reshape(st, backup_file);
-               frozen = 0;
-               goto release;
-       }
-
-       /* set up the backup-super-block.  This requires the
-        * uuid from the array.
-        */
-       /* Find a superblock */
-       for (sd = sra->devs; sd; sd = sd->next) {
-               char *dn;
-               int devfd;
-               int ok;
-               if (sd->disk.state & (1<<MD_DISK_FAULTY))
-                       continue;
-               dn = map_dev(sd->disk.major, sd->disk.minor, 1);
-               devfd = dev_open(dn, O_RDONLY);
-               if (devfd < 0)
-                       continue;
-               ok = st->ss->load_super(st, devfd, NULL);
-               close(devfd);
-               if (ok >= 0)
-                       break;
-       }
-       if (!sd) {
-               fprintf(stderr, Name ": %s: Cannot find a superblock\n",
-                       devname);
-               rv = 1;
-               abort_reshape(sra);
-               goto release;
-       }
-
-       memset(&bsb, 0, 512);
-       memcpy(bsb.magic, "md_backup_data-1", 16);
-       st->ss->uuid_from_super(st, (int*)&bsb.set_uuid);
-       bsb.mtime = __cpu_to_le64(time(0));
-       bsb.devstart2 = blocks;
-
-       stripes = blocks / (info->array.chunk_size/512) /
-               reshape.before.data_disks;
+       if (restart)
+               sysfs_set_str(sra, NULL, "array_state", "active");
 
        /* Now we just need to kick off the reshape and watch, while
         * handling backups of the data...
         * This is all done by a forked background process.
         */
        switch(forked ? 0 : fork()) {
+       case -1:
+               fprintf(stderr, Name ": Cannot run child to monitor reshape: %s\n",
+                       strerror(errno));
+               abort_reshape(sra);
+               goto release;
+       default:
+               return 0;
        case 0:
-               close(fd);
-               if (check_env("MDADM_GROW_VERIFY"))
-                       fd = open(devname, O_RDONLY | O_DIRECT);
-               else
-                       fd = -1;
-               mlockall(MCL_FUTURE);
+               break;
+       }
 
-               odisks = reshape.before.data_disks + reshape.parity;
+       close(fd);
+       if (check_env("MDADM_GROW_VERIFY"))
+               fd = open(devname, O_RDONLY | O_DIRECT);
+       else
+               fd = -1;
+       mlockall(MCL_FUTURE);
 
-               done = child_monitor(fd, sra, &reshape, stripes,
-                                    fdlist, offsets,
-                                    d - odisks, fdlist+odisks, offsets+odisks);
+       odisks = reshape.before.data_disks + reshape.parity;
 
-               if (backup_file && done)
-                       unlink(backup_file);
-               if (!done) {
-                       abort_reshape(sra);
-                       goto out;
-               }
-               /* set new array size if required customer_array_size is used
-                * by this metadata.
+       if (st->ss->external) {
+               /* metadata handler takes it from here */
+               done = st->ss->manage_reshape(
+                       fd, sra, &reshape, st, blocks,
+                       fdlist, offsets,
+                       d - odisks, fdlist+odisks,
+                       offsets+odisks);
+       } else
+               done = child_monitor(
+                       fd, sra, &reshape, st, blocks,
+                       fdlist, offsets,
+                       d - odisks, fdlist+odisks,
+                       offsets+odisks);
+
+       if (backup_file && done)
+               unlink(backup_file);
+       if (!done) {
+               abort_reshape(sra);
+               goto out;
+       }
+
+       if (!st->ss->external &&
+           !(reshape.before.data_disks != reshape.after.data_disks
+             && info->custom_array_size) &&
+           info->new_level == reshape.level &&
+           !forked) {
+               /* no need to wait for the reshape to finish as
+                * there is nothing more to do.
                 */
-               if (reshape.before.data_disks !=
-                   reshape.after.data_disks &&
-                   info->custom_array_size) {
-                       struct mdinfo *info2;
-                       char *subarray = strchr(info->text_version+1, '/')+1;
+               exit(0);
+       }
+       wait_reshape(sra);
 
-                       wait_reshape(sra);
+       if (st->ss->external) {
+               /* Re-load the metadata as much could have changed */
+               int cfd = open_dev(st->container_dev);
+               if (cfd >= 0) {
                        ping_monitor(container);
-
-                       info2 = st->ss->container_content(st, subarray);
-                       if (info2) {
-                               unsigned long long current_size = 0;
-                               unsigned long long new_size =
-                                       info2->custom_array_size/2;
-
-                               if (sysfs_get_ll(sra,
-                                                NULL,
-                                                "array_size",
-                                                &current_size) == 0 &&
-                                   new_size > current_size) {
-                                       if (sysfs_set_num(sra, NULL,
-                                                         "array_size", new_size)
-                                           < 0)
-                                               dprintf("Error: Cannot"
-                                                       " set array size");
-                                       else
-                                               dprintf("Array size "
-                                                       "changed");
-                                       dprintf(" from %llu to %llu.\n",
-                                               current_size, new_size);
-                               }
-                               sysfs_free(info2);
-                       }
+                       st->ss->free_super(st);
+                       st->ss->load_container(st, cfd, container);
+                       close(cfd);
                }
+       }
 
-               if (info->new_level != reshape.level) {
-                       /* We need to wait for the reshape to finish
-                        * (which will have happened unless
-                        * odata < ndata) and then set the level
-                        */
+       /* set new array size if required customer_array_size is used
+        * by this metadata.
+        */
+       if (reshape.before.data_disks !=
+           reshape.after.data_disks &&
+           info->custom_array_size) {
+               struct mdinfo *info2;
+               char *subarray = strchr(info->text_version+1, '/')+1;
 
-                       if (reshape.before.data_disks <
-                           reshape.after.data_disks)
-                               wait_reshape(sra);
+               info2 = st->ss->container_content(st, subarray);
+               if (info2) {
+                       unsigned long long current_size = 0;
+                       unsigned long long new_size =
+                               info2->custom_array_size/2;
+
+                       if (sysfs_get_ll(sra,
+                                        NULL,
+                                        "array_size",
+                                        &current_size) == 0 &&
+                           new_size > current_size) {
+                               if (sysfs_set_num(sra, NULL,
+                                                 "array_size", new_size)
+                                   < 0)
+                                       dprintf("Error: Cannot"
+                                               " set array size");
+                               else
+                                       dprintf("Array size "
+                                               "changed");
+                               dprintf(" from %llu to %llu.\n",
+                                       current_size, new_size);
+                       }
+                       sysfs_free(info2);
+               }
+       }
 
-                       c = map_num(pers, info->new_level);
-                       if (c == NULL)
-                               goto out;/* not possible */
+       if (info->new_level != reshape.level) {
 
+               c = map_num(pers, info->new_level);
+               if (c) {
                        err = sysfs_set_str(sra, NULL, "level", c);
                        if (err)
                                fprintf(stderr, Name\
                                        ": %s: could not set level "
                                        "to %s\n", devname, c);
                }
-       out:
-               if (forked)
-                       return 0;
-               exit(0);
-       case -1:
-               fprintf(stderr, Name ": Cannot run child to monitor reshape: %s\n",
-                       strerror(errno));
-               rv = 1;
-               abort_reshape(sra);
-               break;
-       default:
-               /* The child will take care of unfreezing the array */
-               frozen = 0;
-               break;
        }
+out:
+       if (forked)
+               return 0;
+       exit(0);
 
-
- release:
-       if (!rv) {
-               if (container)
-                       ping_monitor(container);
-               if (st->ss->external) {
-                       /* Re-load the metadata as much could have changed */
-                       int cfd = open_dev(st->container_dev);
-                       if (cfd >= 0) {
-                               st->ss->free_super(st);
-                               st->ss->load_container(st, cfd, container);
-                               close(cfd);
-                       }
-               }
-       }
-       if (rv && orig_level != UnSet && sra) {
+release:
+       if (orig_level != UnSet && sra) {
                c = map_num(pers, orig_level);
                if (c && sysfs_set_str(sra, NULL, "level", c) == 0)
                        fprintf(stderr, Name ": aborting level change\n");
        }
-       unfreeze(st, frozen);
-       return rv;
+       if (!forked)
+               unfreeze(st);
+       return 1;
 }
 
 int reshape_container(char *container, int cfd, char *devname,
@@ -2120,7 +2092,10 @@ int reshape_container(char *container, int cfd, char *devname,
 {
        struct mdinfo *cc = NULL;
 
-       if (reshape_super(st, info->component_size, info->new_level,
+       /* component_size is not meaningful for a container,
+        * so pass '-1' meaning 'no change'
+        */
+       if (reshape_super(st, -1, info->new_level,
                          info->new_layout, info->new_chunk,
                          info->array.raid_disks + info->delta_disks,
                          backup_file, devname, quiet))
@@ -2176,7 +2151,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),
@@ -2189,11 +2164,12 @@ int reshape_container(char *container, int cfd, char *devname,
 
                rv = reshape_array(container, fd, adev, st,
                                   content, force,
-                                  backup_file, quiet, 1);
+                                  backup_file, quiet, 1, 0);
                close(fd);
                if (rv)
                        break;
        }
+       unfreeze(st);
        sysfs_free(cc);
        exit(0);
 }
@@ -2277,8 +2253,9 @@ int progress_reshape(struct mdinfo *info, struct reshape *reshape,
 
        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;
@@ -2286,6 +2263,7 @@ int progress_reshape(struct mdinfo *info, struct reshape *reshape,
        unsigned long long array_size = (info->component_size
                                         * reshape->before.data_disks);
        int fd;
+       char buf[20];
 
        /* First, we unsuspend any region that is now known to be safe.
         * If suspend_point is on the 'wrong' side of reshape_progress, then
@@ -2319,37 +2297,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;
+                       }
                }
        }
 
@@ -2388,16 +2366,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;
                }
@@ -2436,11 +2426,11 @@ int progress_reshape(struct mdinfo *info, struct reshape *reshape,
         */
        fd = sysfs_get_fd(info, NULL, "sync_completed");
        if (fd < 0)
-               return -1;
+               goto check_progress;
 
        if (sysfs_fd_get_ll(fd, &completed) < 0) {
                close(fd);
-               return -1;
+               goto check_progress;
        }
        while (completed < max_progress && completed < wait_point) {
                /* Check that sync_action is still 'reshape' to avoid
@@ -2452,12 +2442,23 @@ 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);
-                       return -1;
+                       goto check_progress;
                }
        }
        /* some kernels can give an incorrectly high 'completed' number */
@@ -2476,9 +2477,28 @@ 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.
+        */
+       strcpy(buf, "hi");
+       if (sysfs_get_str(info, NULL, "reshape_position", buf, sizeof(buf)) < 0
+           || strncmp(buf, "none", 4) != 0)
+               return -2; /* abort */
+       else {
+               /* Maybe racing with array shutdown - check state */
+               if (sysfs_get_str(info, NULL, "array_state", buf, sizeof(buf)) < 0
+                   || strncmp(buf, "inactive", 8) == 0
+                   || strncmp(buf, "clear",5) == 0)
+                       return -2; /* abort */
+               return -1; /* complete */
+       }
 }
 
 
@@ -2510,7 +2530,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 */
@@ -2727,10 +2748,10 @@ static void validate(int afd, int bfd, unsigned long long offset)
        }
 }
 
-static int child_monitor(int afd, struct mdinfo *sra, struct reshape *reshape,
-                        unsigned long stripes,
-                        int *fds, unsigned long long *offsets,
-                        int dests, int *destfd, unsigned long long *destoffsets)
+int child_monitor(int afd, struct mdinfo *sra, struct reshape *reshape,
+                 struct supertype *st, unsigned long blocks,
+                 int *fds, unsigned long long *offsets,
+                 int dests, int *destfd, unsigned long long *destoffsets)
 {
        /* Monitor a reshape where backup is being performed using
         * 'native' mechanism - either to a backup file, or
@@ -2751,6 +2772,41 @@ static int child_monitor(int afd, struct mdinfo *sra, struct reshape *reshape,
        int data = reshape->before.data_disks;
        int disks = reshape->before.data_disks + reshape->parity;
        int chunk = sra->array.chunk_size;
+       struct mdinfo *sd;
+       unsigned long stripes;
+
+       /* set up the backup-super-block.  This requires the
+        * uuid from the array.
+        */
+       /* Find a superblock */
+       for (sd = sra->devs; sd; sd = sd->next) {
+               char *dn;
+               int devfd;
+               int ok;
+               if (sd->disk.state & (1<<MD_DISK_FAULTY))
+                       continue;
+               dn = map_dev(sd->disk.major, sd->disk.minor, 1);
+               devfd = dev_open(dn, O_RDONLY);
+               if (devfd < 0)
+                       continue;
+               ok = st->ss->load_super(st, devfd, NULL);
+               close(devfd);
+               if (ok >= 0)
+                       break;
+       }
+       if (!sd) {
+               fprintf(stderr, Name ": Cannot find a superblock\n");
+               return 0;
+       }
+
+       memset(&bsb, 0, 512);
+       memcpy(bsb.magic, "md_backup_data-1", 16);
+       st->ss->uuid_from_super(st, (int*)&bsb.set_uuid);
+       bsb.mtime = __cpu_to_le64(time(0));
+       bsb.devstart2 = blocks;
+
+       stripes = blocks / (sra->array.chunk_size/512) /
+               reshape->before.data_disks;
 
        if (posix_memalign((void**)&buf, 4096, disks * chunk))
                /* Don't start the 'reshape' */
@@ -2766,7 +2822,7 @@ static 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;
        }
 
@@ -2820,7 +2876,8 @@ static int child_monitor(int afd, struct mdinfo *sra, struct reshape *reshape,
                }
 
                if (rv < 0) {
-                       done = 1;
+                       if (rv == -1)
+                               done = 1;
                        break;
                }
 
@@ -2875,7 +2932,7 @@ static int child_monitor(int afd, struct mdinfo *sra, struct reshape *reshape,
        if (reshape->before.data_disks == reshape->after.data_disks)
                sysfs_set_num(sra, NULL, "sync_speed_min", speed);
        free(buf);
-       return 1; /* FIXME what does this mean? */
+       return done;
 }
 
 /*
@@ -2912,6 +2969,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
@@ -2995,42 +3053,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 */
                }
@@ -3094,7 +3162,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,7 +3261,7 @@ int Grow_continue(int mdfd, struct supertype *st, struct mdinfo *info,
        int 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);
+       return reshape_array(NULL, mdfd, "array", st, info, 1, backup_file, 0, 0, 1);
 }