]> 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 5f2f1673252c07a8a41c0aeed79aedb74a24fd25..cd044bf6dedae2f7388f4a62f531b8e2571d060b 100644 (file)
--- a/Grow.c
+++ b/Grow.c
@@ -633,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");
 
@@ -650,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;
@@ -789,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 */
@@ -798,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) {
@@ -909,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 */
@@ -924,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.
@@ -952,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);
                }
@@ -966,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;
@@ -1003,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;
@@ -1218,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;
        }
@@ -1242,7 +1261,8 @@ 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,
@@ -1445,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)
@@ -1461,6 +1483,8 @@ 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;
@@ -1561,7 +1585,7 @@ 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;
        }
 release:
@@ -1573,7 +1597,8 @@ release:
 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;
@@ -1583,7 +1608,6 @@ 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;
@@ -1594,16 +1618,23 @@ static int reshape_array(char *container, int fd, char *devname,
        unsigned long cache;
        unsigned long long array_size;
        int done;
-       struct mdinfo *sra;
+       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)
@@ -1619,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) {
@@ -1637,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
@@ -1686,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);
@@ -1696,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);
@@ -1706,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;
        }
 
        /*
@@ -1748,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)) {
@@ -1757,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;
        }
 
@@ -1793,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;
        }
 
@@ -1804,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;
        }
 
@@ -1812,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) {
@@ -1820,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++;
@@ -1864,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));
@@ -1886,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);
 
@@ -1915,6 +1949,8 @@ static int reshape_array(char *container, int fd, char *devname,
        }
 
        start_reshape(sra);
+       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...
@@ -1924,9 +1960,8 @@ static int reshape_array(char *container, int fd, char *devname,
        case -1:
                fprintf(stderr, Name ": Cannot run child to monitor reshape: %s\n",
                        strerror(errno));
-               rv = 1;
                abort_reshape(sra);
-               break;
+               goto release;
        default:
                return 0;
        case 0:
@@ -2045,7 +2080,7 @@ release:
        }
        if (!forked)
                unfreeze(st);
-       return rv;
+       return 1;
 }
 
 int reshape_container(char *container, int cfd, char *devname,
@@ -2116,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),
@@ -2129,7 +2164,7 @@ 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;
@@ -2218,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;
@@ -2227,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
@@ -2260,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;
+                       }
                }
        }
 
@@ -2329,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;
                }
@@ -2377,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
@@ -2393,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 */
@@ -2417,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 */
+       }
 }
 
 
@@ -2451,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 */
@@ -2742,7 +2822,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;
        }
 
@@ -2796,7 +2876,8 @@ int child_monitor(int afd, struct mdinfo *sra, struct reshape *reshape,
                }
 
                if (rv < 0) {
-                       done = 1;
+                       if (rv == -1)
+                               done = 1;
                        break;
                }
 
@@ -2851,7 +2932,7 @@ 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;
 }
 
 /*
@@ -2888,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
@@ -2971,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 */
                }
@@ -3070,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') {
@@ -3148,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);
 }