]> git.ipfire.org Git - thirdparty/mdadm.git/blobdiff - Grow.c
Grow: Use 'forked' also for reshape_container in Grow_continue
[thirdparty/mdadm.git] / Grow.c
diff --git a/Grow.c b/Grow.c
index 6f556f65af78749c70c7287c298342ec42806c92..2de821d0b9d432b8b6945ed417f91d1f6e188a0a 100644 (file)
--- a/Grow.c
+++ b/Grow.c
 #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;
@@ -49,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);
@@ -70,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
@@ -886,6 +890,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 */
@@ -933,6 +938,12 @@ int reshape_open_backup_file(char *backup_file,
                return 0;
        }
 
+       if (!restart && strncmp(backup_file, MAP_DIR, strlen(MAP_DIR)) != 0) {
+               char *bu = make_backup(sys_name);
+               symlink(backup_file, bu);
+               free(bu);
+       }
+
        return 1;
 }
 
@@ -1311,7 +1322,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:
@@ -1500,8 +1510,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,
@@ -2053,7 +2063,7 @@ size_change_error:
                 * performed at the level of the container
                 */
                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
@@ -2739,6 +2749,48 @@ 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,
@@ -2843,6 +2895,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.
@@ -3172,6 +3228,7 @@ started:
                        if (!reshape_open_backup_file(backup_file, fd, devname,
                                                      (signed)blocks,
                                                      fdlist+d, offsets+d,
+                                                     sra->sys_name,
                                                      restart)) {
                                goto release;
                        }
@@ -3213,6 +3270,14 @@ started:
                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.
@@ -3292,8 +3357,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));
+                       if (l > 0) {
+                               buf[l]=0;
+                               unlink(buf);
+                       }
+                       unlink(bul);
+                       free(bul);
+               }
                unlink(backup_file);
+       }
        if (!done) {
                abort_reshape(sra);
                goto out;
@@ -3367,8 +3445,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;
@@ -3393,7 +3471,11 @@ int reshape_container(char *container, char *devname,
         */
        ping_monitor(container);
 
-       switch (fork()) {
+       if (!forked && !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);
@@ -4834,7 +4916,7 @@ 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)
@@ -4850,7 +4932,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;
 
@@ -4867,15 +4949,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, INVALID_SECTORS,
-                                       backup_file, 0, 0,
+                                       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;
+}