]> git.ipfire.org Git - thirdparty/mdadm.git/blobdiff - Grow.c
Grow: fix problems with prematurely aborting of reshapes.
[thirdparty/mdadm.git] / Grow.c
diff --git a/Grow.c b/Grow.c
index 8ca57e84ee6b460ff3af67a1a01c5ff25128fadf..6f556f65af78749c70c7287c298342ec42806c92 100644 (file)
--- 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
@@ -25,6 +25,7 @@
 #include       "dlink.h"
 #include       <sys/mman.h>
 #include       <stdint.h>
+#include       <signal.h>
 
 #if ! defined(__BIG_ENDIAN) && ! defined(__LITTLE_ENDIAN)
 #error no endian defined
@@ -611,9 +612,14 @@ 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) {
+                       printf("unfreeze\n");
                        sysfs_set_str(sra, NULL, "sync_action", "idle");
+               }
                sysfs_free(sra);
        }
 }
@@ -729,7 +735,8 @@ void abort_reshape(struct mdinfo *sra)
        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,
@@ -2664,7 +2671,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;
@@ -2726,6 +2733,12 @@ 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 reshape_array(char *container, int fd, char *devname,
                         struct supertype *st, struct mdinfo *info,
                         int force, struct mddev_dev *devlist,
@@ -3086,9 +3099,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) {
@@ -3228,7 +3243,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 &&
@@ -3248,9 +3263,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);
@@ -3258,6 +3273,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(
@@ -3475,7 +3492,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);
@@ -3526,7 +3543,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
@@ -3743,7 +3760,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.
@@ -3825,6 +3843,7 @@ check_progress:
                 */
                int wait = 10000;
                int rv = -2;
+               unsigned long long new_sync_max;
                while (fd >= 0 && rv < 0 && wait > 0) {
                        if (sysfs_wait(fd, &wait) != 1)
                                break;
@@ -3832,6 +3851,11 @@ check_progress:
                        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 */
                                wait = 0;
@@ -4126,6 +4150,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.
@@ -4203,9 +4228,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;
 
@@ -4231,7 +4258,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;
@@ -4239,6 +4267,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;
                }
@@ -4292,7 +4321,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);
@@ -4673,6 +4707,8 @@ int Grow_continue_command(char *devname, int fd,
                                continue;
                        err = st->ss->load_super(st, fd2, NULL);
                        close(fd2);
+                       /* invalidate fd2 to avoid possible double close() */
+                       fd2 = -1;
                        if (err)
                                continue;
                        break;
@@ -4836,7 +4872,8 @@ int Grow_continue(int mdfd, struct supertype *st, struct mdinfo *info,
                                            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, 0,
                                        1 | info->reshape_active,
                                        freeze_reshape);