X-Git-Url: http://git.ipfire.org/?a=blobdiff_plain;f=Manage.c;h=8d1bbd9779810bcd86daa91d255e48a354c8fcdd;hb=62ff3c40c1b42f85b902b8010ee69d9382cfc407;hp=6267c0ca28c71e4a4c5105f67183b01a10bca398;hpb=4dd2df0966ec2e43ea404df5de7adf9f0e1a8e40;p=thirdparty%2Fmdadm.git diff --git a/Manage.c b/Manage.c index 6267c0ca..8d1bbd97 100644 --- a/Manage.c +++ b/Manage.c @@ -27,9 +27,9 @@ #include "md_p.h" #include -#define REGISTER_DEV _IO (MD_MAJOR, 1) -#define START_MD _IO (MD_MAJOR, 2) -#define STOP_MD _IO (MD_MAJOR, 3) +#define REGISTER_DEV _IO (MD_MAJOR, 1) +#define START_MD _IO (MD_MAJOR, 2) +#define STOP_MD _IO (MD_MAJOR, 3) int Manage_ro(char *devname, int fd, int readonly) { @@ -170,21 +170,49 @@ static void remove_devices(char *devnm, char *path) free(path2); } -int Manage_runstop(char *devname, int fd, int runstop, - int verbose, int will_retry) +int Manage_run(char *devname, int fd, int verbose) { - /* Run or stop the array. Array must already be configured - * 'Run' requires >= 0.90.0 - * 'will_retry' is only relevant for 'stop', and means - * that error messages are not wanted. + /* Run the array. Array must already be configured + * Requires >= 0.90.0 */ mdu_param_t param; /* unused */ int rv = 0; + if (md_get_version(fd) < 9000) { + pr_err("need md driver version 0.90.0 or later\n"); + return 1; + } + + if (ioctl(fd, RUN_ARRAY, ¶m)) { + if (verbose >= 0) + pr_err("failed to run array %s: %s\n", + devname, strerror(errno)); + return 1; + } + if (verbose >= 0) + pr_err("started %s\n", devname); + return rv; +} + +int Manage_stop(char *devname, int fd, int verbose, int will_retry) +{ + /* Stop the array. Array must already be configured + * 'will_retry' means that error messages are not wanted. + */ + int rv = 0; + struct map_ent *map = NULL; + struct mdinfo *mdi; + char devnm[32]; + char container[32]; + int err; + int count; + char buf[32]; + unsigned long long rd1, rd2; + if (will_retry && verbose == 0) verbose = -1; - if (runstop == -1 && md_get_version(fd) < 9000) { + if (md_get_version(fd) < 9000) { if (ioctl(fd, STOP_MD, 0) == 0) return 0; pr_err("stopping device %s " @@ -193,169 +221,294 @@ int Manage_runstop(char *devname, int fd, int runstop, return 1; } - if (md_get_version(fd) < 9000) { - pr_err("need md driver version 0.90.0 or later\n"); + /* If this is an mdmon managed array, just write 'inactive' + * to the array state and let mdmon clear up. + */ + strcpy(devnm, fd2devnm(fd)); + /* Get EXCL access first. If this fails, then attempting + * to stop is probably a bad idea. + */ + mdi = sysfs_read(fd, NULL, GET_LEVEL|GET_COMPONENT|GET_VERSION); + if (mdi && is_subarray(mdi->text_version)) { + char *sl; + strncpy(container, mdi->text_version+1, sizeof(container)); + container[sizeof(container)-1] = 0; + sl = strchr(container, '/'); + if (sl) + *sl = 0; + } else + container[0] = 0; + close(fd); + count = 5; + while (((fd = ((devnm[0] == '/') + ?open(devname, O_RDONLY|O_EXCL) + :open_dev_flags(devnm, O_RDONLY|O_EXCL))) < 0 + || strcmp(fd2devnm(fd), devnm) != 0) + && container[0] + && mdmon_running(container) + && count) { + if (fd >= 0) + close(fd); + flush_mdmon(container); + count--; + } + if (fd < 0 || strcmp(fd2devnm(fd), devnm) != 0) { + if (fd >= 0) + close(fd); + if (verbose >= 0) + pr_err("Cannot get exclusive access to %s:" + "Perhaps a running " + "process, mounted filesystem " + "or active volume group?\n", + devname); return 1; } + if (mdi && + mdi->array.level > 0 && + is_subarray(mdi->text_version)) { + int err; + /* This is mdmon managed. */ + close(fd); - if (runstop > 0) { - if (ioctl(fd, RUN_ARRAY, ¶m)) { + /* As we have an O_EXCL open, any use of the device + * which blocks STOP_ARRAY is probably a transient use, + * so it is reasonable to retry for a while - 5 seconds. + */ + count = 25; + while (count && + (err = sysfs_set_str(mdi, NULL, + "array_state", + "inactive")) < 0 + && errno == EBUSY) { + usleep(200000); + count--; + } + if (err) { if (verbose >= 0) - pr_err("failed to run array %s: %s\n", + pr_err("failed to stop array %s: %s\n", devname, strerror(errno)); - return 1; + rv = 1; + goto out; } - if (verbose >= 0) - pr_err("started %s\n", devname); - } else if (runstop < 0){ - struct map_ent *map = NULL; - struct stat stb; - struct mdinfo *mdi; - char devnm[32]; - int err; - int count; - /* If this is an mdmon managed array, just write 'inactive' - * to the array state and let mdmon clear up. - */ - strcpy(devnm, fd2devnm(fd)); - /* Get EXCL access first. If this fails, then attempting - * to stop is probably a bad idea. - */ - close(fd); - fd = open(devname, O_RDONLY|O_EXCL); - if (fd < 0 || strcmp(fd2devnm(fd), devnm) != 0) { - if (fd >= 0) - close(fd); + + /* Give monitor a chance to act */ + ping_monitor(mdi->text_version); + + fd = open_dev_excl(devnm); + if (fd < 0) { if (verbose >= 0) - pr_err("Cannot get exclusive access to %s:" - "Perhaps a running " - "process, mounted filesystem " - "or active volume group?\n", + pr_err("failed to completely stop %s" + ": Device is busy\n", devname); - return 1; + rv = 1; + goto out; } - mdi = sysfs_read(fd, NULL, GET_LEVEL|GET_VERSION); - if (mdi && - mdi->array.level > 0 && - is_subarray(mdi->text_version)) { - int err; - /* This is mdmon managed. */ - close(fd); - - /* As we have an O_EXCL open, any use of the device - * which blocks STOP_ARRAY is probably a transient use, - * so it is reasonable to retry for a while - 5 seconds. - */ - count = 25; - while (count && - (err = sysfs_set_str(mdi, NULL, - "array_state", - "inactive")) < 0 - && errno == EBUSY) { - usleep(200000); - count--; - } - if (err) { - if (verbose >= 0) - pr_err("failed to stop array %s: %s\n", - devname, strerror(errno)); - rv = 1; - goto out; - } - - /* Give monitor a chance to act */ - ping_monitor(mdi->text_version); + } else if (mdi && + mdi->array.major_version == -1 && + mdi->array.minor_version == -2 && + !is_subarray(mdi->text_version)) { + struct mdstat_ent *mds, *m; + /* container, possibly mdmon-managed. + * Make sure mdmon isn't opening it, which + * would interfere with the 'stop' + */ + ping_monitor(mdi->sys_name); - fd = open_dev_excl(devnm); - if (fd < 0) { + /* now check that there are no existing arrays + * which are members of this array + */ + mds = mdstat_read(0, 0); + for (m = mds; m; m = m->next) + if (m->metadata_version && + strncmp(m->metadata_version, "external:", 9)==0 && + metadata_container_matches(m->metadata_version+9, + devnm)) { if (verbose >= 0) - pr_err("failed to completely stop %s" - ": Device is busy\n", - devname); + pr_err("Cannot stop container %s: " + "member %s still active\n", + devname, m->dev); + free_mdstat(mds); rv = 1; goto out; } - } else if (mdi && - mdi->array.major_version == -1 && - mdi->array.minor_version == -2 && - !is_subarray(mdi->text_version)) { - struct mdstat_ent *mds, *m; - /* container, possibly mdmon-managed. - * Make sure mdmon isn't opening it, which - * would interfere with the 'stop' - */ - ping_monitor(mdi->sys_name); + } - /* now check that there are no existing arrays - * which are members of this array + /* If the array is undergoing a reshape which changes the number + * of devices, then it would be nice to stop it at a point where + * it has completed a full number of stripes in both old and + * new layouts as this will allow the reshape to be reverted. + * So if 'sync_action' is "reshape" and 'raid_disks' shows two + * different numbers, then + * - freeze reshape + * - set sync_max to next multiple of both data_disks and + * chunk sizes (or next but one) + * - unfreeze reshape + * - wait on 'sync_completed' for that point to be reached. + */ + if (mdi && (mdi->array.level >= 4 && mdi->array.level <= 6) && + sysfs_attribute_available(mdi, NULL, "sync_action") && + sysfs_attribute_available(mdi, NULL, "reshape_direction") && + sysfs_get_str(mdi, NULL, "sync_action", buf, 20) > 0 && + strcmp(buf, "reshape\n") == 0 && + sysfs_get_two(mdi, NULL, "raid_disks", &rd1, &rd2) == 2 && + sysfs_set_str(mdi, NULL, "sync_action", "frozen") == 0) { + /* Array is frozen */ + unsigned long long position, curr; + unsigned long long chunk1, chunk2; + unsigned long long rddiv, chunkdiv; + unsigned long long sectors; + unsigned long long sync_max, old_sync_max; + unsigned long long completed; + int backwards = 0; + int delay; + int scfd; + + rd1 -= mdi->array.level == 6 ? 2 : 1; + rd2 -= mdi->array.level == 6 ? 2 : 1; + sysfs_get_str(mdi, NULL, "reshape_direction", buf, sizeof(buf)); + if (strncmp(buf, "back", 4) == 0) + backwards = 1; + sysfs_get_ll(mdi, NULL, "reshape_position", &position); + sysfs_get_two(mdi, NULL, "chunk_size", &chunk1, &chunk2); + chunk1 /= 512; + chunk2 /= 512; + rddiv = GCD(rd1, rd2); + chunkdiv = GCD(chunk1, chunk2); + sectors = (chunk1/chunkdiv) * chunk2 * (rd1/rddiv) * rd2; + + if (backwards) { + /* Need to subtract 'reshape_position' from + * array size to get equivalent of sync_max. + * Size calculation based on raid5_size in kernel. */ - mds = mdstat_read(0, 0); - for (m = mds; m; m = m->next) - if (m->metadata_version && - strncmp(m->metadata_version, "external:", 9)==0 && - metadata_container_matches(m->metadata_version+9, - devnm)) { - if (verbose >= 0) - pr_err("Cannot stop container %s: " - "member %s still active\n", - devname, m->dev); - free_mdstat(mds); - rv = 1; - goto out; - } + unsigned long long size = mdi->component_size; + size &= ~(chunk1-1); + size &= ~(chunk2-1); + /* rd1 must be smaller */ + position = (position / sectors - 1) * sectors; + sync_max = size - position/rd1; + } else { + position = (position / sectors + 2) * sectors; + sync_max = position/rd1; } - - /* As we have an O_EXCL open, any use of the device - * which blocks STOP_ARRAY is probably a transient use, - * so it is reasonable to retry for a while - 5 seconds. + if (sysfs_get_ll(mdi, NULL, "sync_max", &old_sync_max) < 0) + old_sync_max = mdi->component_size; + /* Must not advance sync_max as that could confuse + * the reshape monitor */ + if (sync_max < old_sync_max) + sysfs_set_num(mdi, NULL, "sync_max", sync_max); + sysfs_set_str(mdi, NULL, "sync_action", "idle"); + + /* That should have set things going again. Now we + * wait a little while (3 second max) for sync_completed + * to reach the target. + * The reshape process can block for 500msec if + * the sync speed limit is hit, so we need to wait + * a lot longer than that. 1 second is usually + * enough. 3 is safe. */ - count = 25; err = 0; - while (count && fd >= 0 - && (err = ioctl(fd, STOP_ARRAY, NULL)) < 0 - && errno == EBUSY) { - usleep(200000); - count --; - } - if (fd >= 0 && err) { - if (verbose >= 0) { - pr_err("failed to stop array %s: %s\n", - devname, strerror(errno)); - if (errno == EBUSY) - fprintf(stderr, "Perhaps a running " - "process, mounted filesystem " - "or active volume group?\n"); + delay = 3000; + scfd = sysfs_open(mdi->sys_name, NULL, "sync_completed"); + while (scfd >= 0 && delay > 0 && old_sync_max > 0) { + sysfs_get_ll(mdi, NULL, "reshape_position", &curr); + sysfs_fd_get_str(scfd, buf, sizeof(buf)); + if (strncmp(buf, "none", 4) == 0) { + /* Either reshape has aborted, or hasn't + * quite started yet. Wait a bit and + * check 'sync_action' to see. + */ + usleep(10000); + sysfs_get_str(mdi, NULL, "sync_action", buf, sizeof(buf)); + if (strncmp(buf, "reshape", 7) != 0) + break; } - rv = 1; - goto out; + + if (sysfs_fd_get_ll(scfd, &completed) == 0 && + (completed > sync_max || + (completed == sync_max && curr != position))) { + while (completed > sync_max) { + sync_max += sectors / rd1; + if (backwards) + position -= sectors; + else + position += sectors; + } + if (sync_max < old_sync_max) + sysfs_set_num(mdi, NULL, "sync_max", sync_max); + } + + if (!backwards && curr >= position) + break; + if (backwards && curr <= position) + break; + sysfs_wait(scfd, &delay); } - /* prior to 2.6.28, KOBJ_CHANGE was not sent when an md array - * was stopped, so We'll do it here just to be sure. Drop any - * partitions as well... - */ - if (fd >= 0) - ioctl(fd, BLKRRPART, 0); - if (mdi) - sysfs_uevent(mdi, "change"); - - if (devnm[0] && - (stat("/dev/.udev", &stb) != 0 || - check_env("MDADM_NO_UDEV"))) { - struct map_ent *mp = map_by_devnm(&map, devnm); - remove_devices(devnm, mp ? mp->path : NULL); + if (scfd >= 0) + close(scfd); + + } + + /* As we have an O_EXCL open, any use of the device + * which blocks STOP_ARRAY is probably a transient use, + * so it is reasonable to retry for a while - 5 seconds. + */ + count = 25; err = 0; + while (count && fd >= 0 + && (err = ioctl(fd, STOP_ARRAY, NULL)) < 0 + && errno == EBUSY) { + usleep(200000); + count --; + } + if (fd >= 0 && err) { + if (verbose >= 0) { + pr_err("failed to stop array %s: %s\n", + devname, strerror(errno)); + if (errno == EBUSY) + cont_err("Perhaps a running " + "process, mounted filesystem " + "or active volume group?\n"); } + rv = 1; + goto out; + } + /* prior to 2.6.28, KOBJ_CHANGE was not sent when an md array + * was stopped, so We'll do it here just to be sure. Drop any + * partitions as well... + */ + if (fd >= 0) + ioctl(fd, BLKRRPART, 0); + if (mdi) + sysfs_uevent(mdi, "change"); - if (verbose >= 0) - pr_err("stopped %s\n", devname); - map_lock(&map); - map_remove(&map, devnm); - map_unlock(&map); - out: - if (mdi) - sysfs_free(mdi); + if (devnm[0] && use_udev()) { + struct map_ent *mp = map_by_devnm(&map, devnm); + remove_devices(devnm, mp ? mp->path : NULL); } + + if (verbose >= 0) + pr_err("stopped %s\n", devname); + map_lock(&map); + map_remove(&map, devnm); + map_unlock(&map); +out: + if (mdi) + sysfs_free(mdi); + return rv; } +static struct mddev_dev *add_one(struct mddev_dev *dv, char *name, char disp) +{ + struct mddev_dev *new; + new = xmalloc(sizeof(*new)); + memset(new, 0, sizeof(*new)); + new->devname = xstrdup(name); + new->disposition = disp; + new->next = dv->next; + dv->next = new; + return new; +} + static void add_faulty(struct mddev_dev *dv, int fd, char disp) { mdu_array_info_t array; @@ -368,7 +521,6 @@ static void add_faulty(struct mddev_dev *dv, int fd, char disp) remaining_disks = array.nr_disks; for (i = 0; i < MAX_DISKS && remaining_disks > 0; i++) { - struct mddev_dev *new; char buf[40]; disk.number = i; if (ioctl(fd, GET_DISK_INFO, &disk) != 0) @@ -379,12 +531,7 @@ static void add_faulty(struct mddev_dev *dv, int fd, char disp) if ((disk.state & 1) == 0) /* not faulty */ continue; sprintf(buf, "%d:%d", disk.major, disk.minor); - new = xmalloc(sizeof(*new)); - new->devname = xstrdup(buf); - new->disposition = disp; - new->next = dv->next; - dv->next = new; - dv = new; + dv = add_one(dv, buf, disp); } } @@ -400,7 +547,6 @@ static void add_detached(struct mddev_dev *dv, int fd, char disp) remaining_disks = array.nr_disks; for (i = 0; i < MAX_DISKS && remaining_disks > 0; i++) { - struct mddev_dev *new; char buf[40]; int sfd; disk.number = i; @@ -421,12 +567,41 @@ static void add_detached(struct mddev_dev *dv, int fd, char disp) if (errno != ENXIO) /* Probably not detached */ continue; - new = xmalloc(sizeof(*new)); - new->devname = xstrdup(buf); - new->disposition = disp; - new->next = dv->next; - dv->next = new; - dv = new; + dv = add_one(dv, buf, disp); + } +} + +static void add_set(struct mddev_dev *dv, int fd, char set_char) +{ + mdu_array_info_t array; + mdu_disk_info_t disk; + int remaining_disks; + int copies, set; + int i; + + if (ioctl(fd, GET_ARRAY_INFO, &array) != 0) + return; + if (array.level != 10) + return; + copies = ((array.layout & 0xff) * + ((array.layout >> 8) & 0xff)); + if (array.raid_disks % copies) + return; + + remaining_disks = array.nr_disks; + for (i = 0; i < MAX_DISKS && remaining_disks > 0; i++) { + char buf[40]; + disk.number = i; + if (ioctl(fd, GET_DISK_INFO, &disk) != 0) + continue; + if (disk.major == 0 && disk.minor == 0) + continue; + remaining_disks--; + set = disk.raid_disk % copies; + if (set_char != set + 'A') + continue; + sprintf(buf, "%d:%d", disk.major, disk.minor); + dv = add_one(dv, buf, dv->disposition); } } @@ -1043,7 +1218,7 @@ int Manage_with(struct supertype *tst, int fd, struct mddev_dev *dv, "slot", dv->used); if (rv) { sysfs_free(mdi); - pr_err("Failed to %s as preferred replacement.\n", + pr_err("Failed to set %s as preferred replacement.\n", dv->devname); return -1; } @@ -1100,6 +1275,7 @@ int Manage_subdevs(char *devname, int fd, int count = 0; /* number of actions taken */ struct mdinfo info; int frozen = 0; + int busy = 0; if (ioctl(fd, GET_ARRAY_INFO, &array)) { pr_err("Cannot get array info for %s\n", @@ -1172,6 +1348,35 @@ int Manage_subdevs(char *devname, int fd, continue; } + if (strncmp(dv->devname, "set-", 4) == 0 && + strlen(dv->devname) == 5) { + int copies; + + if (dv->disposition != 'r' && + dv->disposition != 'f') { + pr_err("'%s' only meaningful with -r or -f\n", + dv->devname); + goto abort; + } + if (array.level != 10) { + pr_err("'%s' only meaningful with RAID10 arrays\n", + dv->devname); + goto abort; + } + copies = ((array.layout & 0xff) * + ((array.layout >> 8) & 0xff)); + if (array.raid_disks % copies != 0 || + dv->devname[4] < 'A' || + dv->devname[4] >= 'A' + copies || + copies > 26) { + pr_err("'%s' not meaningful with this array\n", + dv->devname); + goto abort; + } + add_set(dv, fd, dv->devname[4]); + continue; + } + if (strchr(dv->devname, '/') == NULL && strchr(dv->devname, ':') == NULL && strlen(dv->devname) < 50) { @@ -1270,7 +1475,7 @@ int Manage_subdevs(char *devname, int fd, */ close(tfd); tfd = dev_open(dv->devname, O_RDONLY); - } + } if (tfd < 0) { if (dv->disposition == 'M') continue; @@ -1320,6 +1525,8 @@ int Manage_subdevs(char *devname, int fd, if ((sysfd >= 0 && write(sysfd, "faulty", 6) != 6) || (sysfd < 0 && ioctl(fd, SET_DISK_FAULTY, (unsigned long) stb.st_rdev))) { + if (errno == EBUSY) + busy = 1; pr_err("set device faulty failed for %s: %s\n", dv->devname, strerror(errno)); if (sysfd >= 0) @@ -1377,7 +1584,7 @@ int Manage_subdevs(char *devname, int fd, abort: if (frozen > 0) sysfs_set_str(&info, NULL, "sync_action","idle"); - return 1; + return !test && busy ? 2 : 1; } int autodetect(void)