X-Git-Url: http://git.ipfire.org/?a=blobdiff_plain;f=Monitor.c;h=0e2e6bc7011ceeec31c929c480c74186616299e8;hb=9008ed1c9623b7c11775ea8a7be17eb1bde5fd09;hp=6a4c8dec0ba27c1e0d5dc4eb8e0fd339e8fe9da4;hpb=ab5303d695eb7249b9462f9ad4e973711622a9c5;p=thirdparty%2Fmdadm.git diff --git a/Monitor.c b/Monitor.c index 6a4c8dec..0e2e6bc7 100644 --- a/Monitor.c +++ b/Monitor.c @@ -31,14 +31,14 @@ #include "md_p.h" #include "md_u.h" #include -#include -#include +#include +#include #include static void alert(char *event, char *dev, char *disc, char *mailaddr, char *mailfrom, char *cmd, int dosyslog); -static char *percentalerts[] = { +static char *percentalerts[] = { "RebuildStarted", "Rebuild20", "Rebuild40", @@ -156,7 +156,7 @@ int Monitor(mddev_dev_t devlist, return 1; } close(0); - open("/dev/null", 3); + open("/dev/null", O_RDWR); dup2(0,1); dup2(0,2); setsid(); @@ -165,14 +165,17 @@ int Monitor(mddev_dev_t devlist, if (devlist == NULL) { mddev_ident_t mdlist = conf_get_ident(NULL); for (; mdlist; mdlist=mdlist->next) { - struct state *st = malloc(sizeof *st); + struct state *st; + if (mdlist->devname == NULL) + continue; + st = malloc(sizeof *st); if (st == NULL) continue; st->devname = strdup(mdlist->devname); st->utime = 0; st->next = statelist; st->err = 0; - st->devnum = MAXINT; + st->devnum = INT_MAX; st->percent = -2; st->expected_spares = mdlist->spare_disks; if (mdlist->spare_group) @@ -192,7 +195,7 @@ int Monitor(mddev_dev_t devlist, st->utime = 0; st->next = statelist; st->err = 0; - st->devnum = MAXINT; + st->devnum = INT_MAX; st->percent = -2; st->expected_spares = -1; st->spare_group = NULL; @@ -234,6 +237,7 @@ int Monitor(mddev_dev_t devlist, */ st->err=1; continue; } + fcntl(fd, F_SETFD, FD_CLOEXEC); if (ioctl(fd, GET_ARRAY_INFO, &array)<0) { if (!st->err) alert("DeviceDisappeared", dev, NULL, @@ -244,8 +248,10 @@ int Monitor(mddev_dev_t devlist, close(fd); continue; } - if (array.level != 1 && array.level != 5 && array.level != -4 && - array.level != 6 && array.level != 10) { + /* It's much easier to list what array levels can't + * have a device disappear than all of them that can + */ + if (array.level == 0 || array.level == -1) { if (!st->err) alert("DeviceDisappeared", dev, "Wrong-Level", mailaddr, mailfrom, alert_cmd, dosyslog); @@ -253,7 +259,7 @@ int Monitor(mddev_dev_t devlist, close(fd); continue; } - if (st->devnum == MAXINT) { + if (st->devnum == INT_MAX) { struct stat stb; if (fstat(fd, &stb) == 0 && (S_IFMT&stb.st_mode)==S_IFBLK) { @@ -266,7 +272,7 @@ int Monitor(mddev_dev_t devlist, for (mse2 = mdstat ; mse2 ; mse2=mse2->next) if (mse2->devnum == st->devnum) { - mse2->devnum = MAXINT; /* flag it as "used" */ + mse2->devnum = INT_MAX; /* flag it as "used" */ mse = mse2; } @@ -288,11 +294,11 @@ int Monitor(mddev_dev_t devlist, alert("DegradedArray", dev, NULL, mailaddr, mailfrom, alert_cmd, dosyslog); if (st->utime == 0 && /* new array */ - st->expected_spares > 0 && - array.spare_disks < st->expected_spares) + st->expected_spares > 0 && + array.spare_disks < st->expected_spares) alert("SparesMissing", dev, NULL, mailaddr, mailfrom, alert_cmd, dosyslog); if (mse && - st->percent == -1 && + st->percent == -1 && mse->percent >= 0) alert("RebuildStarted", dev, NULL, mailaddr, mailfrom, alert_cmd, dosyslog); if (mse && @@ -309,7 +315,7 @@ int Monitor(mddev_dev_t devlist, * If there is a number in /mismatch_cnt, * we should report that. */ - struct sysarray *sra = + struct mdinfo *sra = sysfs_read(-1, st->devnum, GET_MISMATCH); if (sra && sra->mismatch_cnt > 0) { char cnt[40]; @@ -328,6 +334,7 @@ int Monitor(mddev_dev_t devlist, for (i=0; i= 0) { info[i].state = disc.state; info[i].major = disc.major; @@ -338,7 +345,7 @@ int Monitor(mddev_dev_t devlist, close(fd); for (i=0; idevstate[i] = disc.state; + st->devstate[i] = newstate; st->devid[i] = makedev(disc.major, disc.minor); } st->active = array.active_disks; @@ -397,11 +404,11 @@ int Monitor(mddev_dev_t devlist, /* now check if there are any new devices found in mdstat */ if (scan) { struct mdstat_ent *mse; - for (mse=mdstat; mse; mse=mse->next) - if (mse->devnum != MAXINT && - (strcmp(mse->level, "raid1")==0 || - strcmp(mse->level, "raid5")==0 || - strcmp(mse->level, "multipath")==0) + for (mse=mdstat; mse; mse=mse->next) + if (mse->devnum != INT_MAX && + mse->level && + (strcmp(mse->level, "raid0")!=0 && + strcmp(mse->level, "linear")!=0) ) { struct state *st = malloc(sizeof *st); mdu_array_info_t array; @@ -464,7 +471,7 @@ int Monitor(mddev_dev_t devlist, } } if (dev > 0) { - if (ioctl(fd2, HOT_REMOVE_DISK, + if (ioctl(fd2, HOT_REMOVE_DISK, (unsigned long)dev) == 0) { if (ioctl(fd1, HOT_ADD_DISK, (unsigned long)dev) == 0) { @@ -501,7 +508,7 @@ static void alert(char *event, char *dev, char *disc, char *mailaddr, char *mail if (!cmd && !mailaddr) { time_t now = time(0); - + printf("%1.15s: %s on %s %s\n", ctime(&now)+4, event, dev, disc?disc:"unknown device"); } if (cmd) { @@ -517,8 +524,8 @@ static void alert(char *event, char *dev, char *disc, char *mailaddr, char *mail exit(2); } } - if (mailaddr && - (strncmp(event, "Fail", 4)==0 || + if (mailaddr && + (strncmp(event, "Fail", 4)==0 || strncmp(event, "Test", 4)==0 || strncmp(event, "Spares", 6)==0 || strncmp(event, "Degrade", 7)==0)) { @@ -598,10 +605,7 @@ int Wait(char *dev) strerror(errno)); return 2; } - if (major(stb.st_rdev) == MD_MAJOR) - devnum = minor(stb.st_rdev); - else - devnum = -minor(stb.st_rdev)/16; + devnum = stat2devnum(&stb); while(1) { struct mdstat_ent *ms = mdstat_read(1, 0); @@ -612,6 +616,13 @@ int Wait(char *dev) break; if (!e || e->percent < 0) { + if (e && + strncmp(e->metadata_version, "external:", 9) == 0) { + if (is_subarray(&e->metadata_version[9])) + ping_monitor(&e->metadata_version[9]); + else + ping_monitor(devnum2devname(devnum)); + } free_mdstat(ms); return rv; } @@ -620,3 +631,107 @@ int Wait(char *dev) mdstat_wait(5); } } + +static char *clean_states[] = { + "clear", "inactive", "readonly", "read-auto", "clean", NULL }; + +int WaitClean(char *dev, int verbose) +{ + int fd; + struct mdinfo *mdi; + int rv = 1; + int devnum; + + fd = open(dev, O_RDONLY); + if (fd < 0) { + if (verbose) + fprintf(stderr, Name ": Couldn't open %s: %s\n", dev, strerror(errno)); + return 1; + } + + devnum = fd2devnum(fd); + mdi = sysfs_read(fd, devnum, GET_VERSION|GET_LEVEL|GET_SAFEMODE); + if (!mdi) { + if (verbose) + fprintf(stderr, Name ": Failed to read sysfs attributes for " + "%s\n", dev); + close(fd); + return 0; + } + + switch(mdi->array.level) { + case LEVEL_LINEAR: + case LEVEL_MULTIPATH: + case 0: + /* safemode delay is irrelevant for these levels */ + rv = 0; + + } + + /* for internal metadata the kernel handles the final clean + * transition, containers can never be dirty + */ + if (!is_subarray(mdi->text_version)) + rv = 0; + + /* safemode disabled ? */ + if (mdi->safe_mode_delay == 0) + rv = 0; + + if (rv) { + int state_fd = sysfs_open(fd2devnum(fd), NULL, "array_state"); + char buf[20]; + fd_set fds; + struct timeval tm; + + /* minimize the safe_mode_delay and prepare to wait up to 5s + * for writes to quiesce + */ + sysfs_set_safemode(mdi, 1); + tm.tv_sec = 5; + tm.tv_usec = 0; + + /* give mdmon a chance to checkpoint resync */ + sysfs_set_str(mdi, NULL, "sync_action", "idle"); + + FD_ZERO(&fds); + + /* wait for array_state to be clean */ + while (1) { + rv = read(state_fd, buf, sizeof(buf)); + if (rv < 0) + break; + if (sysfs_match_word(buf, clean_states) <= 4) + break; + FD_SET(state_fd, &fds); + rv = select(state_fd + 1, &fds, NULL, NULL, &tm); + if (rv < 0 && errno != EINTR) + break; + lseek(state_fd, 0, SEEK_SET); + } + if (rv < 0) + rv = 1; + else if (ping_monitor(mdi->text_version) == 0) { + /* we need to ping to close the window between array + * state transitioning to clean and the metadata being + * marked clean + */ + rv = 0; + } else + rv = 1; + if (rv && verbose) + fprintf(stderr, Name ": Error waiting for %s to be clean\n", + dev); + + /* restore the original safe_mode_delay */ + sysfs_set_safemode(mdi, mdi->safe_mode_delay); + close(state_fd); + } + + sysfs_free(mdi); + close(fd); + + return rv; +} + +