]> git.ipfire.org Git - thirdparty/mdadm.git/blobdiff - Grow.c
sysfs: Avoid if and return on the same line
[thirdparty/mdadm.git] / Grow.c
diff --git a/Grow.c b/Grow.c
old mode 100644 (file)
new mode 100755 (executable)
index 1d0c7ce..628f0e7
--- a/Grow.c
+++ b/Grow.c
@@ -1,7 +1,7 @@
 /*
  * mdadm - manage Linux "md" devices aka RAID arrays.
  *
- * Copyright (C) 2001-2012 Neil Brown <neilb@suse.de>
+ * Copyright (C) 2001-2013 Neil Brown <neilb@suse.de>
  *
  *
  *    This program is free software; you can redistribute it and/or modify
 #include       "mdadm.h"
 #include       "dlink.h"
 #include       <sys/mman.h>
+#include       <stddef.h>
 #include       <stdint.h>
+#include       <signal.h>
+#include       <sys/wait.h>
 
 #if ! defined(__BIG_ENDIAN) && ! defined(__LITTLE_ENDIAN)
 #error no endian defined
 #include       "md_u.h"
 #include       "md_p.h"
 
-#ifndef offsetof
-#define offsetof(t,f) ((size_t)&(((t*)0)->f))
-#endif
-
 int restore_backup(struct supertype *st,
                   struct mdinfo *content,
                   int working_disks,
                   int next_spare,
-                  char *backup_file,
+                  char **backup_filep,
                   int verbose)
 {
        int i;
@@ -48,6 +47,7 @@ int restore_backup(struct supertype *st,
        struct mdinfo *dev;
        int err;
        int disk_count = next_spare + working_disks;
+       char *backup_file = *backup_filep;
 
        dprintf("Called restore_backup()\n");
        fdlist = xmalloc(sizeof(int) * disk_count);
@@ -69,6 +69,11 @@ int restore_backup(struct supertype *st,
                        fdlist[next_spare++] = fd;
        }
 
+       if (!backup_file) {
+               backup_file = locate_backup(content->sys_name);
+               *backup_filep = backup_file;
+       }
+
        if (st->ss->external && st->ss->recover_backup)
                err = st->ss->recover_backup(st, content);
        else
@@ -82,11 +87,9 @@ int restore_backup(struct supertype *st,
        }
        free(fdlist);
        if (err) {
-               pr_err("Failed to restore critical"
-                      " section for reshape - sorry.\n");
+               pr_err("Failed to restore critical section for reshape - sorry.\n");
                if (!backup_file)
-                       pr_err("Possibly you need"
-                               " to specify a --backup-file\n");
+                       pr_err("Possibly you need to specify a --backup-file\n");
                return 1;
        }
 
@@ -290,12 +293,20 @@ int Grow_addbitmap(char *devname, int fd, struct context *c, struct shape *s)
 
        if (vers < 9003) {
                major = BITMAP_MAJOR_HOSTENDIAN;
-               pr_err("Warning - bitmaps created on this kernel"
-                       " are not portable\n"
-                       "  between different architectures.  Consider upgrading"
-                       " the Linux kernel.\n");
+               pr_err("Warning - bitmaps created on this kernel are not portable\n"
+                       "  between different architectures.  Consider upgrading the Linux kernel.\n");
        }
 
+       /*
+        * We only ever get called if s->bitmap_file is != NULL, so this check
+        * is just here to quiet down static code checkers.
+        */
+       if (!s->bitmap_file)
+               return 1;
+
+       if (strcmp(s->bitmap_file, "clustered") == 0)
+               major = BITMAP_MAJOR_CLUSTERED;
+
        if (ioctl(fd, GET_BITMAP_FILE, &bmf) != 0) {
                if (errno == ENOMEM)
                        pr_err("Memory allocation failure.\n");
@@ -304,8 +315,8 @@ int Grow_addbitmap(char *devname, int fd, struct context *c, struct shape *s)
                return 1;
        }
        if (bmf.pathname[0]) {
-               if (strcmp(s->bitmap_file,"none")==0) {
-                       if (ioctl(fd, SET_BITMAP_FILE, -1)!= 0) {
+               if (strcmp(s->bitmap_file,"none") == 0) {
+                       if (ioctl(fd, SET_BITMAP_FILE, -1) != 0) {
                                pr_err("failed to remove bitmap %s\n",
                                        bmf.pathname);
                                return 1;
@@ -320,17 +331,19 @@ int Grow_addbitmap(char *devname, int fd, struct context *c, struct shape *s)
                pr_err("cannot get array status for %s\n", devname);
                return 1;
        }
-       if (array.state & (1<<MD_SB_BITMAP_PRESENT)) {
+       if (array.state & (1 << MD_SB_BITMAP_PRESENT)) {
                if (strcmp(s->bitmap_file, "none")==0) {
-                       array.state &= ~(1<<MD_SB_BITMAP_PRESENT);
-                       if (ioctl(fd, SET_ARRAY_INFO, &array)!= 0) {
-                               pr_err("failed to remove internal bitmap.\n");
+                       array.state &= ~(1 << MD_SB_BITMAP_PRESENT);
+                       if (ioctl(fd, SET_ARRAY_INFO, &array) != 0) {
+                               if (array.state & (1 << MD_SB_CLUSTERED))
+                                       pr_err("failed to remove clustered bitmap.\n");
+                               else
+                                       pr_err("failed to remove internal bitmap.\n");
                                return 1;
                        }
                        return 0;
                }
-               pr_err("Internal bitmap already present on %s\n",
-                       devname);
+               pr_err("bitmap already present on %s\n", devname);
                return 1;
        }
 
@@ -346,7 +359,7 @@ int Grow_addbitmap(char *devname, int fd, struct context *c, struct shape *s)
        bitmapsize = array.size;
        bitmapsize <<= 1;
        if (get_dev_size(fd, NULL, &array_size) &&
-           array_size > (0x7fffffffULL<<9)) {
+           array_size > (0x7fffffffULL << 9)) {
                /* Array is big enough that we cannot trust array.size
                 * try other approaches
                 */
@@ -358,7 +371,9 @@ int Grow_addbitmap(char *devname, int fd, struct context *c, struct shape *s)
        }
 
        if (array.level == 10) {
-               int ncopies = (array.layout&255)*((array.layout>>8)&255);
+               int ncopies;
+
+               ncopies = (array.layout & 255) * ((array.layout >> 8) & 255);
                bitmapsize = bitmapsize * array.raid_disks / ncopies;
        }
 
@@ -374,52 +389,57 @@ int Grow_addbitmap(char *devname, int fd, struct context *c, struct shape *s)
                free(st);
                return 1;
        }
-       if (strcmp(s->bitmap_file, "internal") == 0) {
+       if (strcmp(s->bitmap_file, "internal") == 0 ||
+           strcmp(s->bitmap_file, "clustered") == 0) {
                int rv;
                int d;
                int offset_setable = 0;
                struct mdinfo *mdi;
                if (st->ss->add_internal_bitmap == NULL) {
-                       pr_err("Internal bitmaps not supported "
-                               "with %s metadata\n", st->ss->name);
+                       pr_err("Internal bitmaps not supported with %s metadata\n", st->ss->name);
                        return 1;
                }
+               st->nodes = c->nodes;
+               st->cluster_name = c->homecluster;
                mdi = sysfs_read(fd, NULL, GET_BITMAP_LOCATION);
                if (mdi)
                        offset_setable = 1;
-               for (d=0; d< st->max_devs; d++) {
+               for (d = 0; d < st->max_devs; d++) {
                        mdu_disk_info_t disk;
                        char *dv;
+                       int fd2;
+
                        disk.number = d;
                        if (ioctl(fd, GET_DISK_INFO, &disk) < 0)
                                continue;
-                       if (disk.major == 0 &&
-                           disk.minor == 0)
+                       if (disk.major == 0 && disk.minor == 0)
                                continue;
-                       if ((disk.state & (1<<MD_DISK_SYNC))==0)
+                       if ((disk.state & (1 << MD_DISK_SYNC)) == 0)
                                continue;
                        dv = map_dev(disk.major, disk.minor, 1);
-                       if (dv) {
-                               int fd2 = dev_open(dv, O_RDWR);
-                               if (fd2 < 0)
-                                       continue;
-                               if (st->ss->load_super(st, fd2, NULL)==0) {
-                                       if (st->ss->add_internal_bitmap(
-                                                   st,
-                                                   &s->bitmap_chunk, c->delay, s->write_behind,
-                                                   bitmapsize, offset_setable,
-                                                   major)
-                                               )
-                                               st->ss->write_bitmap(st, fd2);
-                                       else {
-                                               pr_err("failed to create internal bitmap"
-                                                      " - chunksize problem.\n");
-                                               close(fd2);
-                                               return 1;
-                                       }
+                       if (!dv)
+                               continue;
+                       fd2 = dev_open(dv, O_RDWR);
+                       if (fd2 < 0)
+                               continue;
+                       rv = st->ss->load_super(st, fd2, NULL);
+                       if (!rv) {
+                               rv = st->ss->add_internal_bitmap(
+                                       st, &s->bitmap_chunk, c->delay,
+                                       s->write_behind, bitmapsize,
+                                       offset_setable, major);
+                               if (!rv) {
+                                       st->ss->write_bitmap(st, fd2,
+                                                            NodeNumUpdate);
+                               } else {
+                                       pr_err("failed to create internal bitmap - chunksize problem.\n");
                                }
-                               close(fd2);
+                       } else {
+                               pr_err("failed to load super-block.\n");
                        }
+                       close(fd2);
+                       if (rv)
+                               return 1;
                }
                if (offset_setable) {
                        st->ss->getinfo_super(st, mdi, NULL);
@@ -427,13 +447,14 @@ int Grow_addbitmap(char *devname, int fd, struct context *c, struct shape *s)
                        rv = sysfs_set_num_signed(mdi, NULL, "bitmap/location",
                                                  mdi->bitmap_offset);
                } else {
-                       array.state |= (1<<MD_SB_BITMAP_PRESENT);
+                       if (strcmp(s->bitmap_file, "clustered") == 0)
+                               array.state |= (1 << MD_SB_CLUSTERED);
+                       array.state |= (1 << MD_SB_BITMAP_PRESENT);
                        rv = ioctl(fd, SET_ARRAY_INFO, &array);
                }
                if (rv < 0) {
                        if (errno == EBUSY)
-                               pr_err("Cannot add bitmap while array is"
-                                      " resyncing or reshaping etc.\n");
+                               pr_err("Cannot add bitmap while array is resyncing or reshaping etc.\n");
                        pr_err("failed to set internal bitmap.\n");
                        return 1;
                }
@@ -451,8 +472,8 @@ int Grow_addbitmap(char *devname, int fd, struct context *c, struct shape *s)
                        disk.number = d;
                        if (ioctl(fd, GET_DISK_INFO, &disk) < 0)
                                continue;
-                       if ((disk.major==0 && disk.minor==0) ||
-                           (disk.state & (1<<MD_DISK_REMOVED)))
+                       if ((disk.major==0 && disk.minor == 0) ||
+                           (disk.state & (1 << MD_DISK_REMOVED)))
                                continue;
                        dv = map_dev(disk.major, disk.minor, 1);
                        if (!dv)
@@ -471,21 +492,20 @@ int Grow_addbitmap(char *devname, int fd, struct context *c, struct shape *s)
                        pr_err("cannot find UUID for array!\n");
                        return 1;
                }
-               if (CreateBitmap(s->bitmap_file, c->force, (char*)uuid, s->bitmap_chunk,
-                                c->delay, s->write_behind, bitmapsize, major)) {
+               if (CreateBitmap(s->bitmap_file, c->force, (char*)uuid,
+                                s->bitmap_chunk, c->delay, s->write_behind,
+                                bitmapsize, major)) {
                        return 1;
                }
                bitmap_fd = open(s->bitmap_file, O_RDWR);
                if (bitmap_fd < 0) {
-                       pr_err("weird: %s cannot be opened\n",
-                               s->bitmap_file);
+                       pr_err("weird: %s cannot be opened\n", s->bitmap_file);
                        return 1;
                }
                if (ioctl(fd, SET_BITMAP_FILE, bitmap_fd) < 0) {
                        int err = errno;
                        if (errno == EBUSY)
-                               pr_err("Cannot add bitmap while array is"
-                                      " resyncing or reshaping etc.\n");
+                               pr_err("Cannot add bitmap while array is resyncing or reshaping etc.\n");
                        pr_err("Cannot set bitmap file for %s: %s\n",
                                devname, strerror(err));
                        return 1;
@@ -611,8 +631,11 @@ static void unfreeze(struct supertype *st)
                return unfreeze_container(st);
        else {
                struct mdinfo *sra = sysfs_read(-1, st->devnm, GET_VERSION);
+               char buf[20];
 
-               if (sra)
+               if (sra &&
+                   sysfs_get_str(sra, NULL, "sync_action", buf, 20) > 0
+                   && strcmp(buf, "frozen\n") == 0)
                        sysfs_set_str(sra, NULL, "sync_action", "idle");
                sysfs_free(sra);
        }
@@ -626,13 +649,9 @@ static void wait_reshape(struct mdinfo *sra)
        if (fd < 0)
                return;
 
-       while  (sysfs_fd_get_str(fd, action, 20) > 0 &&
-               strncmp(action, "reshape", 7) == 0) {
-               fd_set rfds;
-               FD_ZERO(&rfds);
-               FD_SET(fd, &rfds);
-               select(fd+1, NULL, NULL, &rfds, NULL);
-       }
+       while (sysfs_fd_get_str(fd, action, 20) > 0 &&
+              strncmp(action, "reshape", 7) == 0)
+               sysfs_wait(fd, NULL);
        close(fd);
 }
 
@@ -720,20 +739,34 @@ int start_reshape(struct mdinfo *sra, int already_running,
        if (!already_running)
                sysfs_set_num(sra, NULL, "sync_min", sync_max_to_set);
        err = err ?: sysfs_set_num(sra, NULL, "sync_max", sync_max_to_set);
-       if (!already_running)
-               err = err ?: sysfs_set_str(sra, NULL, "sync_action", "reshape");
-
+       if (!already_running && err == 0) {
+               int cnt = 5;
+               do {
+                       err = sysfs_set_str(sra, NULL, "sync_action", "reshape");
+                       if (err)
+                               sleep(1);
+               } while (err && errno == EBUSY && cnt-- > 0);
+       }
        return err;
 }
 
 void abort_reshape(struct mdinfo *sra)
 {
        sysfs_set_str(sra, NULL, "sync_action", "idle");
+       /*
+        * Prior to kernel commit: 23ddff3792f6 ("md: allow suspend_lo and
+        * suspend_hi to decrease as well as increase.")
+        * you could only increase suspend_{lo,hi} unless the region they
+        * covered was empty.  So to reset to 0, you need to push suspend_lo
+        * up past suspend_hi first.  So to maximize the chance of mdadm
+        * working on all kernels, we want to keep doing that.
+        */
        sysfs_set_num(sra, NULL, "suspend_lo", 0x7FFFFFFFFFFFFFFFULL);
        sysfs_set_num(sra, NULL, "suspend_hi", 0);
        sysfs_set_num(sra, NULL, "suspend_lo", 0);
        sysfs_set_num(sra, NULL, "sync_min", 0);
-       sysfs_set_str(sra, NULL, "sync_max", "max");
+       // It isn't safe to reset sync_max as we aren't monitoring.
+       // Array really should be stopped at this point.
 }
 
 int remove_disks_for_takeover(struct supertype *st,
@@ -846,7 +879,8 @@ int reshape_prepare_fdlist(char *devname,
        for (sd = sra->devs; sd; sd = sd->next) {
                if (sd->disk.state & (1<<MD_DISK_FAULTY))
                        continue;
-               if (sd->disk.state & (1<<MD_DISK_SYNC)) {
+               if (sd->disk.state & (1<<MD_DISK_SYNC) &&
+                   sd->disk.raid_disk < raid_disks) {
                        char *dn = map_dev(sd->disk.major,
                                           sd->disk.minor, 1);
                        fdlist[sd->disk.raid_disk]
@@ -883,6 +917,7 @@ int reshape_open_backup_file(char *backup_file,
                             long blocks,
                             int *fdlist,
                             unsigned long long *offsets,
+                            char *sys_name,
                             int restart)
 {
        /* Return 1 on success, 0 on any form of failure */
@@ -909,8 +944,7 @@ int reshape_open_backup_file(char *backup_file,
        dev = stb.st_dev;
        fstat(fd, &stb);
        if (stb.st_rdev == dev) {
-               pr_err("backup file must NOT be"
-                       " on the array being reshaped.\n");
+               pr_err("backup file must NOT be on the array being reshaped.\n");
                close(*fdlist);
                return 0;
        }
@@ -918,8 +952,7 @@ int reshape_open_backup_file(char *backup_file,
        memset(buf, 0, 512);
        for (i=0; i < blocks + 8 ; i++) {
                if (write(*fdlist, buf, 512) != 512) {
-                       pr_err("%s: cannot create"
-                               " backup file %s: %s\n",
+                       pr_err("%s: cannot create backup file %s: %s\n",
                                devname, backup_file, strerror(errno));
                        return 0;
                }
@@ -930,18 +963,15 @@ int reshape_open_backup_file(char *backup_file,
                return 0;
        }
 
-       return 1;
-}
-
-unsigned long GCD(unsigned long a, unsigned long b)
-{
-       while (a != b) {
-               if (a < b)
-                       b -= a;
-               if (b < a)
-                       a -= b;
+       if (!restart && strncmp(backup_file, MAP_DIR, strlen(MAP_DIR)) != 0) {
+               char *bu = make_backup(sys_name);
+               if (symlink(backup_file, bu))
+                       pr_err("Recording backup file in " MAP_DIR " failed: %s\n",
+                              strerror(errno));
+               free(bu);
        }
-       return a;
+
+       return 1;
 }
 
 unsigned long compute_backup_blocks(int nchunk, int ochunk,
@@ -1023,7 +1053,12 @@ char *analyse_change(char *devname, struct mdinfo *info, struct reshape *re)
 
        switch (info->array.level) {
        default:
-               return "Cannot understand this RAID level";
+               return "No reshape is possibly for this RAID level";
+       case LEVEL_LINEAR:
+               if (info->delta_disks != UnSet)
+                       return "Only --add is supported for LINEAR, setting --raid-disks is not needed";
+               else
+                       return "Only --add is supported for LINEAR, other --grow options are not meaningful";
        case 1:
                /* RAID1 can convert to RAID1 with different disks, or
                 * raid5 with 2 disks, or
@@ -1031,13 +1066,11 @@ char *analyse_change(char *devname, struct mdinfo *info, struct reshape *re)
                 */
                if (info->new_level > 1 &&
                    (info->component_size & 7))
-                       return "Cannot convert RAID1 of this size - "
-                               "reduce size to multiple of 4K first.";
+                       return "Cannot convert RAID1 of this size - reduce size to multiple of 4K first.";
                if (info->new_level == 0) {
                        if (info->delta_disks != UnSet &&
                            info->delta_disks != 0)
-                               return "Cannot change number of disks "
-                                       "with RAID1->RAID0 conversion";
+                               return "Cannot change number of disks with RAID1->RAID0 conversion";
                        re->level = 0;
                        re->before.data_disks = 1;
                        re->after.data_disks = 1;
@@ -1050,6 +1083,9 @@ char *analyse_change(char *devname, struct mdinfo *info, struct reshape *re)
                        re->level = 1;
                        return NULL;
                }
+               if (info->array.raid_disks != 2 &&
+                   info->new_level == 5)
+                       return "Can only convert a 2-device array to RAID5";
                if (info->array.raid_disks == 2 &&
                    info->new_level == 5) {
 
@@ -1123,8 +1159,7 @@ char *analyse_change(char *devname, struct mdinfo *info, struct reshape *re)
                                if (far > 1 && !offset)
                                        return "Cannot reshape RAID10 to far-mode";
                                if (near * far != copies)
-                                       return "Cannot change number of copies"
-                                               " when reshaping RAID10";
+                                       return "Cannot change number of copies when reshaping RAID10";
                        }
                        if (info->delta_disks == UnSet)
                                info->delta_disks = 0;
@@ -1174,15 +1209,13 @@ char *analyse_change(char *devname, struct mdinfo *info, struct reshape *re)
                                                  / info->array.raid_disks);
                                if (info->array.raid_disks * (copies-1)
                                    != info->delta_disks)
-                                       return "Impossible number of devices"
-                                               " for RAID0->RAID10";
+                                       return "Impossible number of devices for RAID0->RAID10";
                                info->new_layout = 0x100 + copies;
                        }
                        if (info->delta_disks == UnSet) {
                                int copies = info->new_layout & 0xff;
                                if (info->new_layout != 0x100 + copies)
-                                       return "New layout impossible"
-                                               " for RAID0->RAID10";;
+                                       return "New layout impossible for RAID0->RAID10";;
                                info->delta_disks = (copies - 1) *
                                        info->array.raid_disks;
                        }
@@ -1277,8 +1310,7 @@ char *analyse_change(char *devname, struct mdinfo *info, struct reshape *re)
                                return "Can only convert a 2-device array to RAID1";
                        if (info->delta_disks != UnSet &&
                            info->delta_disks != 0)
-                               return "Cannot set raid_disk when "
-                                       "converting RAID5->RAID1";
+                               return "Cannot set raid_disk when converting RAID5->RAID1";
                        re->level = 1;
                        info->new_chunk = 0;
                        return NULL;
@@ -1335,7 +1367,6 @@ char *analyse_change(char *devname, struct mdinfo *info, struct reshape *re)
 
                switch (re->level) {
                case 4:
-                       re->before.layout = 0;
                        re->after.layout = 0;
                        break;
                case 5:
@@ -1374,8 +1405,7 @@ char *analyse_change(char *devname, struct mdinfo *info, struct reshape *re)
                                        strcat(strcpy(layout, ls), "-6");
                                        l = map_name(r6layout, layout);
                                        if (l == UnSet)
-                                               return "Cannot find RAID6 layout"
-                                                       " to convert to";
+                                               return "Cannot find RAID6 layout to convert to";
                                } else {
                                        /* Current RAID6 has no equivalent.
                                         * If it is already a '-6' layout we
@@ -1487,8 +1517,8 @@ static int set_array_size(struct supertype *st, struct mdinfo *sra,
                                ret_val = 0;
                                dprintf("Array size changed");
                        }
-                       dprintf(" from %llu to %llu.\n",
-                               current_size, new_size);
+                       dprintf_cont(" from %llu to %llu.\n",
+                                    current_size, new_size);
                }
                sysfs_free(info);
        } else
@@ -1508,8 +1538,8 @@ static int reshape_container(char *container, char *devname,
                             struct supertype *st,
                             struct mdinfo *info,
                             int force,
-                            char *backup_file,
-                            int verbose, int restart, int freeze_reshape);
+                            char *backup_file, int verbose,
+                            int forked, int restart, int freeze_reshape);
 
 int Grow_reshape(char *devname, int fd,
                 struct mddev_dev *devlist,
@@ -1562,10 +1592,8 @@ int Grow_reshape(char *devname, int fd,
 
        if (s->size > 0 &&
            (s->chunk || s->level!= UnSet || s->layout_str || s->raiddisks)) {
-               pr_err("cannot change component size at the same time "
-                       "as other changes.\n"
-                       "   Change size first, then check data is intact before "
-                       "making other changes.\n");
+               pr_err("cannot change component size at the same time as other changes.\n"
+                       "   Change size first, then check data is intact before making other changes.\n");
                return 1;
        }
 
@@ -1583,10 +1611,18 @@ int Grow_reshape(char *devname, int fd,
                return 1;
        }
        if (s->raiddisks > st->max_devs) {
-               pr_err("Cannot increase raid-disks on this array"
-                       " beyond %d\n", st->max_devs);
+               pr_err("Cannot increase raid-disks on this array beyond %d\n", st->max_devs);
                return 1;
        }
+       if (s->level == 0 &&
+           (array.state & (1<<MD_SB_BITMAP_PRESENT)) &&
+           !(array.state & (1<<MD_SB_CLUSTERED))) {
+                array.state &= ~(1<<MD_SB_BITMAP_PRESENT);
+                if (ioctl(fd, SET_ARRAY_INFO, &array)!= 0) {
+                        pr_err("failed to remove internal bitmap.\n");
+                        return 1;
+                }
+        }
 
        /* in the external case we need to check that the requested reshape is
         * supported, and perform an initial check that the container holds the
@@ -1638,9 +1674,7 @@ int Grow_reshape(char *devname, int fd,
                                    & (1<<MD_SB_BLOCK_CONTAINER_RESHAPE))
                                        allow_reshape = 0;
                                if (!allow_reshape) {
-                                       pr_err("cannot reshape arrays in"
-                                              " container with unsupported"
-                                              " metadata: %s(%s)\n",
+                                       pr_err("cannot reshape arrays in container with unsupported metadata: %s(%s)\n",
                                               devname, container);
                                        sysfs_free(cc);
                                        free(subarray);
@@ -1659,8 +1693,7 @@ int Grow_reshape(char *devname, int fd,
        if (s->raiddisks > array.raid_disks &&
            array.spare_disks +added_disks < (s->raiddisks - array.raid_disks) &&
            !c->force) {
-               pr_err("Need %d spare%s to avoid degraded array,"
-                      " and only have %d.\n"
+               pr_err("Need %d spare%s to avoid degraded array, and only have %d.\n"
                       "       Use --force to over-ride this check.\n",
                       s->raiddisks - array.raid_disks,
                       s->raiddisks - array.raid_disks == 1 ? "" : "s",
@@ -1686,8 +1719,7 @@ int Grow_reshape(char *devname, int fd,
                sysfs_free(sra);
                return 1;
        } else if (frozen < 0) {
-               pr_err("%s is performing resync/recovery and cannot"
-                       " be reshaped\n", devname);
+               pr_err("%s is performing resync/recovery and cannot be reshaped\n", devname);
                sysfs_free(sra);
                return 1;
        }
@@ -1728,8 +1760,7 @@ int Grow_reshape(char *devname, int fd,
                                                sizeinfo->array.layout,
                                                sizeinfo->array.raid_disks);
                                new_size /= data_disks;
-                               dprintf("Metadata size correction from %llu to "
-                                       "%llu (%llu)\n", orig_size, new_size,
+                               dprintf("Metadata size correction from %llu to %llu (%llu)\n", orig_size, new_size,
                                        new_size * data_disks);
                                s->size = new_size;
                                sysfs_free(sizeinfo);
@@ -1766,13 +1797,11 @@ int Grow_reshape(char *devname, int fd,
                        }
                }
                if (rv) {
-                       pr_err("Cannot set size on "
-                               "array members.\n");
+                       pr_err("Cannot set size on array members.\n");
                        goto size_change_error;
                }
                if (min_csize && s->size > min_csize) {
-                       pr_err("Cannot safely make this array "
-                               "use more than 2TB per device on this kernel.\n");
+                       pr_err("Cannot safely make this array use more than 2TB per device on this kernel.\n");
                        rv = 1;
                        goto size_change_error;
                }
@@ -1780,8 +1809,7 @@ int Grow_reshape(char *devname, int fd,
                        /* Don't let the kernel choose a size - it will get
                         * it wrong
                         */
-                       pr_err("Limited v0.90 array to "
-                              "2TB per device\n");
+                       pr_err("Limited v0.90 array to 2TB per device\n");
                        s->size = min_csize;
                }
                if (st->ss->external) {
@@ -1790,8 +1818,8 @@ int Grow_reshape(char *devname, int fd,
                                                   "raid5");
                                if (!rv) {
                                        raid0_takeover = 1;
-                                       /* get array parametes after takeover
-                                        * to chane one parameter at time only
+                                       /* get array parameters after takeover
+                                        * to change one parameter at time only
                                         */
                                        rv = ioctl(fd, GET_ARRAY_INFO, &array);
                                }
@@ -1809,7 +1837,7 @@ int Grow_reshape(char *devname, int fd,
                if (s->size == MAX_SIZE)
                        s->size = 0;
                array.size = s->size;
-               if ((unsigned)array.size != s->size) {
+               if (s->size & ~INT32_MAX) {
                        /* got truncated to 32bit, write to
                         * component_size instead
                         */
@@ -1870,12 +1898,10 @@ size_change_error:
                        s->size = array.size;
                if (c->verbose >= 0) {
                        if (s->size == orig_size)
-                               pr_err("component size of %s "
-                                       "unchanged at %lluK\n",
+                               pr_err("component size of %s unchanged at %lluK\n",
                                        devname, s->size);
                        else
-                               pr_err("component size of %s "
-                                       "has been set to %lluK\n",
+                               pr_err("component size of %s has been set to %lluK\n",
                                        devname, s->size);
                }
                changed = 1;
@@ -1910,7 +1936,7 @@ size_change_error:
                int err;
                err = remove_disks_for_takeover(st, sra, array.layout);
                if (err) {
-                       dprintf(Name": Array cannot be reshaped\n");
+                       dprintf("Array cannot be reshaped\n");
                        if (cfd > -1)
                                close(cfd);
                        rv = 1;
@@ -1943,12 +1969,9 @@ size_change_error:
                if (info.array.level == 6 &&
                    (info.new_level == 6 || info.new_level == UnSet) &&
                    info.array.layout >= 16) {
-                       pr_err("%s has a non-standard layout.  If you"
-                              " wish to preserve this\n", devname);
-                       cont_err("during the reshape, please specify"
-                                " --layout=preserve\n");
-                       cont_err("If you want to change it, specify a"
-                                " layout or use --layout=normalise\n");
+                       pr_err("%s has a non-standard layout.  If you wish to preserve this\n", devname);
+                       cont_err("during the reshape, please specify --layout=preserve\n");
+                       cont_err("If you want to change it, specify a layout or use --layout=normalise\n");
                        rv = 1;
                        goto release;
                }
@@ -1965,8 +1988,7 @@ size_change_error:
                                info.new_layout = map_name(r6layout, l);
                        }
                } else {
-                       pr_err("%s is only meaningful when reshaping"
-                              " a RAID6 array.\n", s->layout_str);
+                       pr_err("%s is only meaningful when reshaping a RAID6 array.\n", s->layout_str);
                        rv = 1;
                        goto release;
                }
@@ -1988,8 +2010,7 @@ size_change_error:
                        strcat(l, "-6");
                        info.new_layout = map_name(r6layout, l);
                } else {
-                       pr_err("%s in only meaningful when reshaping"
-                              " to RAID6\n", s->layout_str);
+                       pr_err("%s in only meaningful when reshaping to RAID6\n", s->layout_str);
                        rv = 1;
                        goto release;
                }
@@ -2011,14 +2032,12 @@ size_change_error:
                        info.new_layout = parse_layout_faulty(s->layout_str);
                        break;
                default:
-                       pr_err("layout not meaningful"
-                               " with this level\n");
+                       pr_err("layout not meaningful with this level\n");
                        rv = 1;
                        goto release;
                }
                if (info.new_layout == UnSet) {
-                       pr_err("layout %s not understood"
-                               " for this level\n",
+                       pr_err("layout %s not understood for this level\n",
                                s->layout_str);
                        rv = 1;
                        goto release;
@@ -2060,8 +2079,12 @@ size_change_error:
                 * number of devices (On-Line Capacity Expansion) must be
                 * performed at the level of the container
                 */
+               if (fd > 0) {
+                       close(fd);
+                       fd = -1;
+               }
                rv = reshape_container(container, devname, -1, st, &info,
-                                      c->force, c->backup_file, c->verbose, 0, 0);
+                                      c->force, c->backup_file, c->verbose, 0, 0, 0);
                frozen = 0;
        } else {
                /* get spare devices from external metadata
@@ -2124,27 +2147,22 @@ static int verify_reshape_position(struct mdinfo *info, int level)
                char *ep;
                unsigned long long position = strtoull(buf, &ep, 0);
 
-               dprintf(Name": Read sync_max sysfs entry is: %s\n", buf);
+               dprintf("Read sync_max sysfs entry is: %s\n", buf);
                if (!(ep == buf || (*ep != 0 && *ep != '\n' && *ep != ' '))) {
                        position *= get_data_disks(level,
                                                   info->new_layout,
                                                   info->array.raid_disks);
                        if (info->reshape_progress < position) {
-                               dprintf("Corrected reshape progress (%llu) to "
-                                       "md position (%llu)\n",
+                               dprintf("Corrected reshape progress (%llu) to md position (%llu)\n",
                                        info->reshape_progress, position);
                                info->reshape_progress = position;
                                ret_val = 1;
                        } else if (info->reshape_progress > position) {
-                               pr_err("Fatal error: array "
-                                      "reshape was not properly frozen "
-                                      "(expected reshape position is %llu, "
-                                      "but reshape progress is %llu.\n",
+                               pr_err("Fatal error: array reshape was not properly frozen (expected reshape position is %llu, but reshape progress is %llu.\n",
                                       position, info->reshape_progress);
                                ret_val = -1;
                        } else {
-                               dprintf("Reshape position in md and metadata "
-                                       "are the same;");
+                               dprintf("Reshape position in md and metadata are the same;");
                                ret_val = 1;
                        }
                }
@@ -2246,7 +2264,10 @@ static int set_new_data_offset(struct mdinfo *sra, struct supertype *st,
                if (info2.space_before == 0 &&
                    info2.space_after == 0) {
                        /* Metadata doesn't support data_offset changes */
-                       return 1;
+                       if (!can_fallback)
+                               pr_err("%s: Metadata version doesn't support data_offset changes\n",
+                                      devname);
+                       goto fallback;
                }
                if (before > info2.space_before)
                        before = info2.space_before;
@@ -2491,8 +2512,7 @@ static int raid10_reshape(char *container, int fd, char *devname,
                       devname);
                if (err == EBUSY &&
                    (info->array.state & (1<<MD_SB_BITMAP_PRESENT)))
-                       cont_err("       Bitmap must be removed before"
-                                " shape can be changed\n");
+                       cont_err("       Bitmap must be removed before shape can be changed\n");
                goto release;
        }
        sysfs_free(sra);
@@ -2565,7 +2585,7 @@ static void update_cache_size(char *container, struct mdinfo *sra,
        /* make sure there is room for 'blocks' with a bit to spare */
        if (cache < 16 + blocks / disks)
                cache = 16 + blocks / disks;
-       cache /= (4096/512); /* Covert from sectors to pages */
+       cache /= (4096/512); /* Convert from sectors to pages */
 
        if (sra->cache_size < cache)
                subarray_set_num(container, sra, "stripe_cache_size",
@@ -2616,8 +2636,7 @@ static int impose_reshape(struct mdinfo *sra,
 
                        if (err == EBUSY &&
                            (array.state & (1<<MD_SB_BITMAP_PRESENT)))
-                               cont_err("Bitmap must be removed before"
-                                        " shape can be changed\n");
+                               cont_err("Bitmap must be removed before shape can be changed\n");
 
                        goto release;
                }
@@ -2641,8 +2660,7 @@ static int impose_reshape(struct mdinfo *sra,
 
                        if (err == EBUSY &&
                            (array.state & (1<<MD_SB_BITMAP_PRESENT)))
-                               cont_err("Bitmap must be removed before"
-                                        " shape can be changed\n");
+                               cont_err("Bitmap must be removed before shape can be changed\n");
                        goto release;
                }
        }
@@ -2679,7 +2697,7 @@ static int impose_level(int fd, int level, char *devname, int verbose)
                for (d = 0, found = 0;
                     d < MAX_DISKS && found < array.nr_disks;
                     d++) {
-                               mdu_disk_info_t disk;
+                       mdu_disk_info_t disk;
                        disk.number = d;
                        if (ioctl(fd, GET_DISK_INFO, &disk) < 0)
                                continue;
@@ -2730,8 +2748,7 @@ static int impose_level(int fd, int level, char *devname, int verbose)
                                devname, c);
                        if (err == EBUSY &&
                            (array.state & (1<<MD_SB_BITMAP_PRESENT)))
-                               cont_err("Bitmap must be removed"
-                                        " before level can be changed\n");
+                               cont_err("Bitmap must be removed before level can be changed\n");
                        return err;
                }
                if (verbose >= 0)
@@ -2741,6 +2758,54 @@ static int impose_level(int fd, int level, char *devname, int verbose)
        return 0;
 }
 
+int sigterm = 0;
+static void catch_term(int sig)
+{
+       sigterm = 1;
+}
+
+static int continue_via_systemd(char *devnm)
+{
+       int skipped, i, pid, status;
+       char pathbuf[1024];
+       /* In a systemd/udev world, it is best to get systemd to
+        * run "mdadm --grow --continue" rather than running in the
+        * background.
+        */
+       switch(fork()) {
+       case  0:
+               /* FIXME yuk. CLOSE_EXEC?? */
+               skipped = 0;
+               for (i = 3; skipped < 20; i++)
+                       if (close(i) < 0)
+                               skipped++;
+                       else
+                               skipped = 0;
+
+               /* Don't want to see error messages from
+                * systemctl.  If the service doesn't exist,
+                * we fork ourselves.
+                */
+               close(2);
+               open("/dev/null", O_WRONLY);
+               snprintf(pathbuf, sizeof(pathbuf), "mdadm-grow-continue@%s.service",
+                        devnm);
+               status = execl("/usr/bin/systemctl", "systemctl",
+                              "start",
+                              pathbuf, NULL);
+               status = execl("/bin/systemctl", "systemctl", "start",
+                              pathbuf, NULL);
+               exit(1);
+       case -1: /* Just do it ourselves. */
+               break;
+       default: /* parent - good */
+               pid = wait(&status);
+               if (pid >= 0 && status == 0)
+                       return 1;
+       }
+       return 0;
+}
+
 static int reshape_array(char *container, int fd, char *devname,
                         struct supertype *st, struct mdinfo *info,
                         int force, struct mddev_dev *devlist,
@@ -2770,6 +2835,7 @@ static int reshape_array(char *container, int fd, char *devname,
        unsigned long long array_size;
        int done;
        struct mdinfo *sra = NULL;
+       char buf[20];
 
        /* when reshaping a RAID0, the component_size might be zero.
         * So try to fix that up.
@@ -2812,12 +2878,13 @@ static int reshape_array(char *container, int fd, char *devname,
             reshape.before.layout != info->array.layout ||
             reshape.before.data_disks + reshape.parity
             != info->array.raid_disks - max(0, info->delta_disks))) {
-               pr_err("reshape info is not in native format -"
-                       " cannot continue.\n");
+               pr_err("reshape info is not in native format - cannot continue.\n");
                goto release;
        }
 
-       if (st->ss->external && restart && (info->reshape_progress == 0)) {
+       if (st->ss->external && restart && (info->reshape_progress == 0) &&
+           !((sysfs_get_str(info, NULL, "sync_action", buf, sizeof(buf)) > 0) &&
+             (strncmp(buf, "reshape", 7) == 0))) {
                /* When reshape is restarted from '0', very begin of array
                 * it is possible that for external metadata reshape and array
                 * configuration doesn't happen.
@@ -2845,6 +2912,10 @@ static int reshape_array(char *container, int fd, char *devname,
                               devname);
                        goto release;
                }
+
+               if (!backup_file)
+                       backup_file = locate_backup(sra->sys_name);
+
                goto started;
        }
        /* The container is frozen but the array may not be.
@@ -2864,8 +2935,7 @@ static int reshape_array(char *container, int fd, char *devname,
        if (!force &&
            info->new_level > 1 && info->array.level > 1 &&
            spares_needed > info->array.spare_disks + added_disks) {
-               pr_err("Need %d spare%s to avoid degraded array,"
-                      " and only have %d.\n"
+               pr_err("Need %d spare%s to avoid degraded array, and only have %d.\n"
                       "       Use --force to over-ride this check.\n",
                       spares_needed,
                       spares_needed == 1 ? "" : "s",
@@ -2878,8 +2948,7 @@ static int reshape_array(char *container, int fd, char *devname,
                - array.raid_disks;
        if ((info->new_level > 1 || info->new_level == 0) &&
            spares_needed > info->array.spare_disks +added_disks) {
-               pr_err("Need %d spare%s to create working array,"
-                      " and only have %d.\n",
+               pr_err("Need %d spare%s to create working array, and only have %d.\n",
                       spares_needed,
                       spares_needed == 1 ? "" : "s",
                       info->array.spare_disks + added_disks);
@@ -2946,9 +3015,11 @@ static int reshape_array(char *container, int fd, char *devname,
         * array.  Now that the array has been changed to the right
         * level and frozen, we can safely add them.
         */
-       if (devlist)
-               Manage_subdevs(devname, fd, devlist, verbose,
-                              0,NULL, 0);
+       if (devlist) {
+               if (Manage_subdevs(devname, fd, devlist, verbose,
+                                  0, NULL, 0))
+                       goto release;
+       }
 
        if (reshape.backup_blocks == 0 && data_offset != INVALID_SECTORS)
                reshape.backup_blocks = reshape.before.data_disks * info->array.chunk_size/512;
@@ -3086,8 +3157,19 @@ static int reshape_array(char *container, int fd, char *devname,
                                   devname, container, &reshape) < 0)
                        goto release;
                if (sysfs_set_str(sra, NULL, "sync_action", "reshape") < 0) {
-                       pr_err("Failed to initiate reshape!\n");
-                       goto release;
+                       struct mdinfo *sd;
+                       if (errno != EINVAL) {
+                               pr_err("Failed to initiate reshape!\n");
+                               goto release;
+                       }
+                       /* revert data_offset and try the old way */
+                       for (sd = sra->devs; sd; sd = sd->next) {
+                               sysfs_set_num(sra, sd, "new_offset",
+                                             sd->data_offset);
+                               sysfs_set_str(sra, NULL, "reshape_direction",
+                                             "forwards");
+                       }
+                       break;
                }
                if (info->new_level == reshape.level)
                        return 0;
@@ -3101,9 +3183,11 @@ static int reshape_array(char *container, int fd, char *devname,
                        map_fork();
                        break;
                }
+               close(fd);
                wait_reshape(sra);
-               impose_level(fd, info->new_level, devname, verbose);
-
+               fd = open_dev(sra->sys_name);
+               if (fd >= 0)
+                       impose_level(fd, info->new_level, devname, verbose);
                return 0;
        case 1: /* Couldn't set data_offset, try the old way */
                if (data_offset != INVALID_SECTORS) {
@@ -3128,12 +3212,10 @@ started:
                       blocks < 16*1024*2)
                        blocks *= 2;
        } else
-               pr_err("Need to backup %luK of critical "
-                       "section..\n", blocks/2);
+               pr_err("Need to backup %luK of critical section..\n", blocks/2);
 
        if (blocks >= sra->component_size/2) {
-               pr_err("%s: Something wrong"
-                       " - reshape aborted\n",
+               pr_err("%s: Something wrong - reshape aborted\n",
                        devname);
                goto release;
        }
@@ -3150,7 +3232,7 @@ started:
        d = reshape_prepare_fdlist(devname, sra, odisks,
                                   nrdisks, blocks, backup_file,
                                   fdlist, offsets);
-       if (d < 0) {
+       if (d < odisks) {
                goto release;
        }
        if ((st->ss->manage_reshape == NULL) ||
@@ -3158,19 +3240,19 @@ started:
                if (backup_file == NULL) {
                        if (reshape.after.data_disks <=
                            reshape.before.data_disks) {
-                               pr_err("%s: Cannot grow - "
-                                       "need backup-file\n", devname);
+                               pr_err("%s: Cannot grow - need backup-file\n",
+                                      devname);
+                               pr_err(" Please provide one with \"--backup=...\"\n");
                                goto release;
-                       } else if (sra->array.spare_disks == 0) {
-                               pr_err("%s: Cannot grow - "
-                                       "need a spare or backup-file to backup "
-                                       "critical section\n", devname);
+                       } else if (d == odisks) {
+                               pr_err("%s: Cannot grow - need a spare or backup-file to backup critical section\n", devname);
                                goto release;
                        }
                } else {
                        if (!reshape_open_backup_file(backup_file, fd, devname,
                                                      (signed)blocks,
                                                      fdlist+d, offsets+d,
+                                                     sra->sys_name,
                                                      restart)) {
                                goto release;
                        }
@@ -3206,12 +3288,19 @@ started:
                free(fdlist);
                free(offsets);
                sysfs_free(sra);
-               pr_err("Reshape has to be continued from"
-                       " location %llu when root filesystem has been mounted.\n",
+               pr_err("Reshape has to be continued from location %llu when root filesystem has been mounted.\n",
                        sra->reshape_progress);
                return 1;
        }
 
+       if (!forked && !check_env("MDADM_NO_SYSTEMCTL"))
+               if (continue_via_systemd(container ?: sra->sys_name)) {
+                       free(fdlist);
+                       free(offsets);
+                       sysfs_free(sra);
+                       return 0;
+               }
+
        /* 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.
@@ -3242,7 +3331,7 @@ started:
        do {
                struct mdstat_ent *mds, *m;
                delayed = 0;
-               mds = mdstat_read(0, 0);
+               mds = mdstat_read(1, 0);
                for (m = mds; m; m = m->next)
                        if (strcmp(m->devnm, sra->sys_name) == 0) {
                                if (m->resync &&
@@ -3262,9 +3351,9 @@ started:
                        delayed = 0;
                }
                if (delayed)
-                       sleep(30 - (delayed-1) * 25);
+                       mdstat_wait(30 - (delayed-1) * 25);
        } while (delayed);
-
+       mdstat_close();
        close(fd);
        if (check_env("MDADM_GROW_VERIFY"))
                fd = open(devname, O_RDONLY | O_DIRECT);
@@ -3272,6 +3361,8 @@ started:
                fd = -1;
        mlockall(MCL_FUTURE);
 
+       signal(SIGTERM, catch_term);
+
        if (st->ss->external) {
                /* metadata handler takes it from here */
                done = st->ss->manage_reshape(
@@ -3289,8 +3380,21 @@ started:
        free(fdlist);
        free(offsets);
 
-       if (backup_file && done)
+       if (backup_file && done) {
+               char *bul;
+               bul = make_backup(sra->sys_name);
+               if (bul) {
+                       char buf[1024];
+                       int l = readlink(bul, buf, sizeof(buf) - 1);
+                       if (l > 0) {
+                               buf[l]=0;
+                               unlink(buf);
+                       }
+                       unlink(bul);
+                       free(bul);
+               }
                unlink(backup_file);
+       }
        if (!done) {
                abort_reshape(sra);
                goto out;
@@ -3364,8 +3468,8 @@ int reshape_container(char *container, char *devname,
                      struct supertype *st,
                      struct mdinfo *info,
                      int force,
-                     char *backup_file,
-                     int verbose, int restart, int freeze_reshape)
+                     char *backup_file, int verbose,
+                     int forked, int restart, int freeze_reshape)
 {
        struct mdinfo *cc = NULL;
        int rv = restart;
@@ -3390,15 +3494,18 @@ int reshape_container(char *container, char *devname,
         */
        ping_monitor(container);
 
-       switch (fork()) {
+       if (!forked && !freeze_reshape && !check_env("MDADM_NO_SYSTEMCTL"))
+               if (continue_via_systemd(container))
+                       return 0;
+
+       switch (forked ? 0 : fork()) {
        case -1: /* error */
                perror("Cannot fork to complete reshape\n");
                unfreeze(st);
                return 1;
        default: /* parent */
                if (!freeze_reshape)
-                       printf(Name ": multi-array reshape continues"
-                              " in background\n");
+                       printf("%s: multi-array reshape continues in background\n", Name);
                return 0;
        case 0: /* child */
                map_fork();
@@ -3426,7 +3533,7 @@ int reshape_container(char *container, char *devname,
                int fd;
                struct mdstat_ent *mdstat;
                char *adev;
-               int devid;
+               dev_t devid;
 
                sysfs_free(cc);
 
@@ -3460,8 +3567,7 @@ int reshape_container(char *container, char *devname,
 
                fd = open_dev(mdstat->devnm);
                if (fd < 0) {
-                       printf(Name ": Device %s cannot be opened for reshape.",
-                              adev);
+                       pr_err("Device %s cannot be opened for reshape.\n", adev);
                        break;
                }
 
@@ -3476,8 +3582,7 @@ int reshape_container(char *container, char *devname,
                         * This is possibly interim until the behaviour of
                         * reshape_array is resolved().
                         */
-                       printf(Name ": Multiple reshape execution detected for "
-                              "device  %s.", adev);
+                       printf("%s: Multiple reshape execution detected for device  %s.\n", Name, adev);
                        close(fd);
                        break;
                }
@@ -3489,7 +3594,7 @@ int reshape_container(char *container, char *devname,
                        flush_mdmon(container);
 
                rv = reshape_array(container, fd, adev, st,
-                                  content, force, NULL, 0ULL,
+                                  content, force, NULL, INVALID_SECTORS,
                                   backup_file, verbose, 1, restart,
                                   freeze_reshape);
                close(fd);
@@ -3540,7 +3645,7 @@ int progress_reshape(struct mdinfo *info, struct reshape *reshape,
                     unsigned long long backup_point,
                     unsigned long long wait_point,
                     unsigned long long *suspend_point,
-                    unsigned long long *reshape_completed)
+                    unsigned long long *reshape_completed, int *frozen)
 {
        /* This function is called repeatedly by the reshape manager.
         * It determines how much progress can safely be made and allows
@@ -3757,7 +3862,8 @@ int progress_reshape(struct mdinfo *info, struct reshape *reshape,
                wait_point = info->component_size - wait_point;
        }
 
-       sysfs_set_num(info, NULL, "sync_max", max_progress);
+       if (!*frozen)
+               sysfs_set_num(info, NULL, "sync_max", max_progress);
 
        /* Now wait.  If we have already reached the point that we were
         * asked to wait to, don't wait at all, else wait for any change.
@@ -3777,7 +3883,6 @@ int progress_reshape(struct mdinfo *info, struct reshape *reshape,
                 * waiting forever on a dead array
                 */
                char action[20];
-               fd_set rfds;
                if (sysfs_get_str(info, NULL, "sync_action",
                                  action, 20) <= 0 ||
                    strncmp(action, "reshape", 7) != 0)
@@ -3787,34 +3892,44 @@ int progress_reshape(struct mdinfo *info, struct reshape *reshape,
                 * So we need these extra tests.
                 */
                if (completed == 0 && advancing
+                   && strncmp(action, "idle", 4) == 0
                    && info->reshape_progress > 0)
                        break;
                if (completed == 0 && !advancing
+                   && strncmp(action, "idle", 4) == 0
                    && 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);
+               sysfs_wait(fd, NULL);
                if (sysfs_fd_get_ll(fd, &completed) < 0)
                        goto check_progress;
        }
        /* Some kernels reset 'sync_completed' to zero,
-        * we need to have real point we are in md
+        * we need to have real point we are in md.
+        * So in that case, read 'reshape_position' from sysfs.
         */
-       if (completed == 0)
-               completed = max_progress;
-
-       /* some kernels can give an incorrectly high 'completed' number */
-       completed /= (info->new_chunk/512);
-       completed *= (info->new_chunk/512);
-       /* Convert 'completed' back in to a 'progress' number */
-       completed *= reshape->after.data_disks;
-       if (!advancing) {
-               completed = info->component_size * reshape->after.data_disks
-                       - completed;
+       if (completed == 0) {
+               unsigned long long reshapep;
+               char action[20];
+               if (sysfs_get_str(info, NULL, "sync_action",
+                                 action, 20) > 0 &&
+                   strncmp(action, "idle", 4) == 0 &&
+                   sysfs_get_ll(info, NULL,
+                                "reshape_position", &reshapep) == 0)
+                       *reshape_completed = reshapep;
+       } else {
+               /* some kernels can give an incorrectly high
+                * 'completed' number, so round down */
+               completed /= (info->new_chunk/512);
+               completed *= (info->new_chunk/512);
+               /* Convert 'completed' back in to a 'progress' number */
+               completed *= reshape->after.data_disks;
+               if (!advancing)
+                       completed = (info->component_size
+                                    * reshape->after.data_disks
+                                    - completed);
+               *reshape_completed = completed;
        }
-       *reshape_completed = completed;
 
        close(fd);
 
@@ -3834,29 +3949,29 @@ check_progress:
         * it was just a device failure that leaves us degraded but
         * functioning.
         */
-       strcpy(buf, "hi");
        if (sysfs_get_str(info, NULL, "reshape_position", buf, sizeof(buf)) < 0
            || strncmp(buf, "none", 4) != 0) {
                /* The abort might only be temporary.  Wait up to 10
                 * seconds for fd to contain a valid number again.
                 */
-               struct timeval tv;
+               int wait = 10000;
                int rv = -2;
-               tv.tv_sec = 10;
-               tv.tv_usec = 0;
-               while (fd >= 0 && rv < 0 && tv.tv_sec > 0) {
-                       fd_set rfds;
-                       FD_ZERO(&rfds);
-                       FD_SET(fd, &rfds);
-                       if (select(fd+1, NULL, NULL, &rfds, &tv) != 1)
+               unsigned long long new_sync_max;
+               while (fd >= 0 && rv < 0 && wait > 0) {
+                       if (sysfs_wait(fd, &wait) != 1)
                                break;
                        switch (sysfs_fd_get_ll(fd, &completed)) {
                        case 0:
                                /* all good again */
                                rv = 1;
+                               /* If "sync_max" is no longer max_progress
+                                * we need to freeze things
+                                */
+                               sysfs_get_ll(info, NULL, "sync_max", &new_sync_max);
+                               *frozen = (new_sync_max != max_progress);
                                break;
                        case -2: /* read error - abort */
-                               tv.tv_sec = 0;
+                               wait = 0;
                                break;
                        }
                }
@@ -4148,6 +4263,7 @@ int child_monitor(int afd, struct mdinfo *sra, struct reshape *reshape,
        struct mdinfo *sd;
        unsigned long stripes;
        int uuid[4];
+       int frozen = 0;
 
        /* set up the backup-super-block.  This requires the
         * uuid from the array.
@@ -4225,9 +4341,11 @@ int child_monitor(int afd, struct mdinfo *sra, struct reshape *reshape,
                                wait_point = __le64_to_cpu(bsb.arraystart2);
                }
 
+               reshape_completed = sra->reshape_progress;
                rv = progress_reshape(sra, reshape,
                                      backup_point, wait_point,
-                                     &suspend_point, &reshape_completed);
+                                     &suspend_point, &reshape_completed,
+                                     &frozen);
                /* external metadata would need to ping_monitor here */
                sra->reshape_progress = reshape_completed;
 
@@ -4253,7 +4371,8 @@ int child_monitor(int afd, struct mdinfo *sra, struct reshape *reshape,
                                forget_backup(dests, destfd,
                                              destoffsets, 1);
                }
-
+               if (sigterm)
+                       rv = -2;
                if (rv < 0) {
                        if (rv == -1)
                                done = 1;
@@ -4261,6 +4380,7 @@ int child_monitor(int afd, struct mdinfo *sra, struct reshape *reshape,
                }
                if (rv == 0 && increasing && !st->ss->external) {
                        /* No longer need to monitor this reshape */
+                       sysfs_set_str(sra, NULL, "sync_max", "max");
                        done = 1;
                        break;
                }
@@ -4314,7 +4434,12 @@ int child_monitor(int afd, struct mdinfo *sra, struct reshape *reshape,
        }
 
        /* FIXME maybe call progress_reshape one more time instead */
-       abort_reshape(sra); /* remove any remaining suspension */
+       /* remove any remaining suspension */
+       sysfs_set_num(sra, NULL, "suspend_lo", 0x7FFFFFFFFFFFFFFFULL);
+       sysfs_set_num(sra, NULL, "suspend_hi", 0);
+       sysfs_set_num(sra, NULL, "suspend_lo", 0);
+       sysfs_set_num(sra, NULL, "sync_min", 0);
+
        if (reshape->before.data_disks == reshape->after.data_disks)
                sysfs_set_num(sra, NULL, "sync_speed_min", speed);
        free(buf);
@@ -4420,11 +4545,10 @@ int Grow_restart(struct supertype *st, struct mdinfo *info, int *fdlist, int cnt
                 * sometimes they aren't... So allow considerable flexability in matching, and allow
                 * this test to be overridden by an environment variable.
                 */
-               if (info->array.utime > (int)__le64_to_cpu(bsb.mtime) + 2*60*60 ||
-                   info->array.utime < (int)__le64_to_cpu(bsb.mtime) - 10*60) {
+               if(time_after(info->array.utime, (unsigned int)__le64_to_cpu(bsb.mtime) + 2*60*60) ||
+                  time_before(info->array.utime, (unsigned int)__le64_to_cpu(bsb.mtime) - 10*60)) {
                        if (check_env("MDADM_GROW_ALLOW_OLD")) {
-                               pr_err("accepting backup with timestamp %lu "
-                                       "for array with timestamp %lu\n",
+                               pr_err("accepting backup with timestamp %lu for array with timestamp %lu\n",
                                        (unsigned long)__le64_to_cpu(bsb.mtime),
                                        (unsigned long)info->array.utime);
                        } else {
@@ -4505,7 +4629,7 @@ int Grow_restart(struct supertype *st, struct mdinfo *info, int *fdlist, int cnt
                        st->ss->free_super(st);
                        offsets[j] = dinfo.data_offset * 512;
                }
-               printf(Name ": restoring critical section\n");
+               printf("%s: restoring critical section\n", Name);
 
                if (restore_stripes(fdlist, offsets,
                                    info->array.raid_disks,
@@ -4650,7 +4774,7 @@ int Grow_continue_command(char *devname, int fd,
        struct mdinfo *cc = NULL;
        struct mdstat_ent *mdstat = NULL;
        int cfd = -1;
-       int fd2 = -1;
+       int fd2;
 
        dprintf("Grow continue from command line called for %s\n",
                devname);
@@ -4664,10 +4788,10 @@ int Grow_continue_command(char *devname, int fd,
        dprintf("Grow continue is run for ");
        if (st->ss->external == 0) {
                int d;
-               dprintf("native array (%s)\n", devname);
+               int cnt = 5;
+               dprintf_cont("native array (%s)\n", devname);
                if (ioctl(fd, GET_ARRAY_INFO, &array.array) < 0) {
-                       pr_err("%s is not an active md array -"
-                               " aborting\n", devname);
+                       pr_err("%s is not an active md array - aborting\n", devname);
                        ret_val = 1;
                        goto Grow_continue_command_exit;
                }
@@ -4676,53 +4800,58 @@ int Grow_continue_command(char *devname, int fd,
                 * FIXME we should really get what we need from
                 * sysfs
                 */
-               for (d = 0; d < MAX_DISKS; d++) {
-                       mdu_disk_info_t disk;
-                       char *dv;
-                       int err;
-                       disk.number = d;
-                       if (ioctl(fd, GET_DISK_INFO, &disk) < 0)
-                               continue;
-                       if (disk.major == 0 && disk.minor == 0)
-                               continue;
-                       if ((disk.state & (1 << MD_DISK_ACTIVE)) == 0)
-                               continue;
-                       dv = map_dev(disk.major, disk.minor, 1);
-                       if (!dv)
-                               continue;
-                       fd2 = dev_open(dv, O_RDONLY);
-                       if (fd2 < 0)
-                               continue;
-                       err = st->ss->load_super(st, fd2, NULL);
-                       close(fd2);
-                       if (err)
-                               continue;
-                       break;
-               }
-               if (d == MAX_DISKS) {
-                       pr_err("Unable to load metadata for %s\n",
-                              devname);
-                       ret_val = 1;
-                       goto Grow_continue_command_exit;
-               }
-               st->ss->getinfo_super(st, content, NULL);
+               do {
+                       for (d = 0; d < MAX_DISKS; d++) {
+                               mdu_disk_info_t disk;
+                               char *dv;
+                               int err;
+                               disk.number = d;
+                               if (ioctl(fd, GET_DISK_INFO, &disk) < 0)
+                                       continue;
+                               if (disk.major == 0 && disk.minor == 0)
+                                       continue;
+                               if ((disk.state & (1 << MD_DISK_ACTIVE)) == 0)
+                                       continue;
+                               dv = map_dev(disk.major, disk.minor, 1);
+                               if (!dv)
+                                       continue;
+                               fd2 = dev_open(dv, O_RDONLY);
+                               if (fd2 < 0)
+                                       continue;
+                               err = st->ss->load_super(st, fd2, NULL);
+                               close(fd2);
+                               if (err)
+                                       continue;
+                               break;
+                       }
+                       if (d == MAX_DISKS) {
+                               pr_err("Unable to load metadata for %s\n",
+                                      devname);
+                               ret_val = 1;
+                               goto Grow_continue_command_exit;
+                       }
+                       st->ss->getinfo_super(st, content, NULL);
+                       if (!content->reshape_active)
+                               sleep(3);
+                       else
+                               break;
+               } while (cnt-- > 0);
        } else {
                char *container;
 
                if (subarray) {
-                       dprintf("subarray (%s)\n", subarray);
+                       dprintf_cont("subarray (%s)\n", subarray);
                        container = st->container_devnm;
                        cfd = open_dev_excl(st->container_devnm);
                } else {
                        container = st->devnm;
                        close(fd);
                        cfd = open_dev_excl(st->devnm);
-                       dprintf("container (%s)\n", container);
+                       dprintf_cont("container (%s)\n", container);
                        fd = cfd;
                }
                if (cfd < 0) {
-                       pr_err("Unable to open container "
-                               "for %s\n", devname);
+                       pr_err("Unable to open container for %s\n", devname);
                        ret_val = 1;
                        goto Grow_continue_command_exit;
                }
@@ -4758,9 +4887,7 @@ int Grow_continue_command(char *devname, int fd,
                                allow_reshape = 0;
 
                        if (!allow_reshape) {
-                               pr_err("cannot continue reshape of an array"
-                                      " in container with unsupported"
-                                      " metadata: %s(%s)\n",
+                               pr_err("cannot continue reshape of an array in container with unsupported metadata: %s(%s)\n",
                                       devname, container);
                                ret_val = 1;
                                goto Grow_continue_command_exit;
@@ -4780,8 +4907,7 @@ int Grow_continue_command(char *devname, int fd,
                        break;
                }
                if (!content) {
-                       pr_err("Unable to determine reshaped "
-                              "array for %s\n", devname);
+                       pr_err("Unable to determine reshaped array for %s\n", devname);
                        ret_val = 1;
                        goto Grow_continue_command_exit;
                }
@@ -4794,6 +4920,8 @@ int Grow_continue_command(char *devname, int fd,
 
                sysfs_init(content, fd2, mdstat->devnm);
 
+               close(fd2);
+
                /* start mdmon in case it is not running
                 */
                if (!mdmon_running(container))
@@ -4803,8 +4931,7 @@ int Grow_continue_command(char *devname, int fd,
                if (mdmon_running(container))
                        st->update_tail = &st->updates;
                else {
-                       pr_err("No mdmon found. "
-                               "Grow cannot continue.\n");
+                       pr_err("No mdmon found. Grow cannot continue.\n");
                        ret_val = 1;
                        goto Grow_continue_command_exit;
                }
@@ -4820,11 +4947,9 @@ int Grow_continue_command(char *devname, int fd,
 
        /* continue reshape
         */
-       ret_val = Grow_continue(fd, st, content, backup_file, 0);
+       ret_val = Grow_continue(fd, st, content, backup_file, 1, 0);
 
 Grow_continue_command_exit:
-       if (fd2 > -1)
-               close(fd2);
        if (cfd > -1)
                close(cfd);
        st->ss->free_super(st);
@@ -4836,7 +4961,7 @@ Grow_continue_command_exit:
 }
 
 int Grow_continue(int mdfd, struct supertype *st, struct mdinfo *info,
-                 char *backup_file, int freeze_reshape)
+                 char *backup_file, int forked, int freeze_reshape)
 {
        int ret_val = 2;
 
@@ -4853,14 +4978,40 @@ int Grow_continue(int mdfd, struct supertype *st, struct mdinfo *info,
                close(cfd);
                ret_val = reshape_container(st->container_devnm, NULL, mdfd,
                                            st, info, 0, backup_file,
-                                           0,
+                                           0, forked,
                                            1 | info->reshape_active,
                                            freeze_reshape);
        } else
                ret_val = reshape_array(NULL, mdfd, "array", st, info, 1,
-                                       NULL, 0ULL, backup_file, 0, 0,
+                                       NULL, INVALID_SECTORS,
+                                       backup_file, 0, forked,
                                        1 | info->reshape_active,
                                        freeze_reshape);
 
        return ret_val;
 }
+
+char *make_backup(char *name)
+{
+       char *base = "backup_file-";
+       int len;
+       char *fname;
+
+       len = strlen(MAP_DIR) + 1 + strlen(base) + strlen(name)+1;
+       fname = xmalloc(len);
+       sprintf(fname, "%s/%s%s", MAP_DIR, base, name);
+       return fname;
+}
+
+char *locate_backup(char *name)
+{
+       char *fl = make_backup(name);
+       struct stat stb;
+
+       if (stat(fl, &stb) == 0 &&
+           S_ISREG(stb.st_mode))
+               return fl;
+
+       free(fl);
+       return NULL;
+}