X-Git-Url: http://git.ipfire.org/?a=blobdiff_plain;f=Grow.c;h=2af6c96e764a67b6125bea90775342b462f61a45;hb=04f903b21ad4d68aa8630433dc2b07edfbc0ad0f;hp=e27d29ff0c3aa5ee6e81ce2e7580317a63473f12;hpb=2eba849621011a5160b4597f82aa4ed0de7d4e64;p=thirdparty%2Fmdadm.git diff --git a/Grow.c b/Grow.c index e27d29ff..2af6c96e 100644 --- a/Grow.c +++ b/Grow.c @@ -1,7 +1,7 @@ /* * mdadm - manage Linux "md" devices aka RAID arrays. * - * Copyright (C) 2001-2012 Neil Brown + * Copyright (C) 2001-2013 Neil Brown * * * This program is free software; you can redistribute it and/or modify @@ -24,7 +24,10 @@ #include "mdadm.h" #include "dlink.h" #include +#include #include +#include +#include #if ! defined(__BIG_ENDIAN) && ! defined(__LITTLE_ENDIAN) #error no endian defined @@ -32,15 +35,11 @@ #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 @@ -611,9 +616,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 +739,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, @@ -879,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 */ @@ -926,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; } @@ -1493,8 +1511,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, @@ -2046,7 +2064,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 @@ -2664,7 +2682,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 +2744,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, @@ -2830,6 +2896,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. @@ -3086,9 +3156,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) { @@ -3143,8 +3215,9 @@ 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 - " @@ -3156,6 +3229,7 @@ started: if (!reshape_open_backup_file(backup_file, fd, devname, (signed)blocks, fdlist+d, offsets+d, + sra->sys_name, restart)) { goto release; } @@ -3197,6 +3271,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. @@ -3227,7 +3309,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 && @@ -3247,9 +3329,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); @@ -3257,6 +3339,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( @@ -3274,8 +3358,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; @@ -3349,8 +3446,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; @@ -3375,7 +3472,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); @@ -3474,7 +3575,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); @@ -3525,7 +3626,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 @@ -3742,7 +3843,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. @@ -3824,6 +3926,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; @@ -3831,6 +3934,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; @@ -4125,6 +4233,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. @@ -4202,9 +4311,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; @@ -4230,7 +4341,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; @@ -4238,6 +4350,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; } @@ -4291,7 +4404,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); @@ -4672,6 +4790,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; @@ -4797,7 +4917,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) @@ -4813,7 +4933,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; @@ -4830,14 +4950,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, 1, 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; +}