/* give the monitor a chance to update the metadata */ int ping_monitor(char *devname) { int sfd = connect_monitor(devname); int err = fping_monitor(sfd); close(sfd); return err; }
static int mdmon(char *devnm, int must_fork, int takeover) { int mdfd; struct mdinfo *mdi, *di; struct supertype *container; sigset_t set; struct sigaction act; int pfd[2]; int status; int ignore; pid_t victim = -1; int victim_sock = -1; dprintf("starting mdmon for %s\n", devnm); mdfd = open_dev(devnm); if (mdfd < 0) { pr_err("%s: %s\n", devnm, strerror(errno)); return 1; } if (md_get_version(mdfd) < 0) { pr_err("%s: Not an md device\n", devnm); return 1; } /* Fork, and have the child tell us when they are ready */ if (must_fork) { if (pipe(pfd) != 0) { pr_err("failed to create pipe\n"); return 1; } switch(fork()) { case -1: pr_err("failed to fork: %s\n", strerror(errno)); return 1; case 0: /* child */ close(pfd[0]); break; default: /* parent */ close(pfd[1]); if (read(pfd[0], &status, sizeof(status)) != sizeof(status)) { wait(&status); status = WEXITSTATUS(status); } close(pfd[0]); return status; } } else pfd[0] = pfd[1] = -1; container = xcalloc(1, sizeof(*container)); strcpy(container->devnm, devnm); container->arrays = NULL; container->sock = -1; mdi = sysfs_read(mdfd, container->devnm, GET_VERSION|GET_LEVEL|GET_DEVS); if (!mdi) { pr_err("failed to load sysfs info for %s\n", container->devnm); exit(3); } if (mdi->array.level != UnSet) { pr_err("%s is not a container - cannot monitor\n", devnm); exit(3); } if (mdi->array.major_version != -1 || mdi->array.minor_version != -2) { pr_err("%s does not use external metadata - cannot monitor\n", devnm); exit(3); } container->ss = version_to_superswitch(mdi->text_version); if (container->ss == NULL) { pr_err("%s uses unsupported metadata: %s\n", devnm, mdi->text_version); exit(3); } container->devs = NULL; for (di = mdi->devs; di; di = di->next) { struct mdinfo *cd = xmalloc(sizeof(*cd)); *cd = *di; cd->next = container->devs; container->devs = cd; } sysfs_free(mdi); /* SIGUSR is sent between parent and child. So both block it * and enable it only with pselect. */ sigemptyset(&set); sigaddset(&set, SIGUSR1); sigaddset(&set, SIGTERM); sigprocmask(SIG_BLOCK, &set, NULL); act.sa_handler = wake_me; act.sa_flags = 0; sigaction(SIGUSR1, &act, NULL); act.sa_handler = term; sigaction(SIGTERM, &act, NULL); act.sa_handler = SIG_IGN; sigaction(SIGPIPE, &act, NULL); victim = mdmon_pid(container->devnm); if (victim >= 0) victim_sock = connect_monitor(container->devnm); ignore = chdir("/"); if (!takeover && victim > 0 && victim_sock >= 0) { if (fping_monitor(victim_sock) == 0) { pr_err("%s already managed\n", container->devnm); exit(3); } close(victim_sock); victim_sock = -1; } if (container->ss->load_container(container, mdfd, devnm)) { pr_err("Cannot load metadata for %s\n", devnm); exit(3); } close(mdfd); /* Ok, this is close enough. We can say goodbye to our parent now. */ if (victim > 0) remove_pidfile(devnm); if (make_pidfile(devnm) < 0) { exit(3); } container->sock = make_control_sock(devnm); status = 0; if (pfd[1] >= 0) { if (write(pfd[1], &status, sizeof(status)) < 0) pr_err("failed to notify our parent: %d\n", getppid()); close(pfd[1]); } mlockall(MCL_CURRENT | MCL_FUTURE); if (clone_monitor(container) < 0) { pr_err("failed to start monitor process: %s\n", strerror(errno)); exit(2); } if (victim > 0) { try_kill_monitor(victim, container->devnm, victim_sock); if (victim_sock >= 0) close(victim_sock); } setsid(); close(0); open("/dev/null", O_RDWR); close(1); ignore = dup(0); #ifndef DEBUG close(2); ignore = dup(0); #endif /* This silliness is to stop the compiler complaining * that we ignore 'ignore' */ if (ignore) ignore++; do_manager(container); exit(0); }
int WaitClean(char *dev, int sock, int verbose) { int fd; struct mdinfo *mdi; int rv = 1; char devnm[32]; fd = open(dev, O_RDONLY); if (fd < 0) { if (verbose) pr_err("Couldn't open %s: %s\n", dev, strerror(errno)); return 1; } strcpy(devnm, fd2devnm(fd)); mdi = sysfs_read(fd, devnm, GET_VERSION|GET_LEVEL|GET_SAFEMODE); if (!mdi) { if (verbose) pr_err("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(fd2devnm(fd), NULL, "array_state"); char buf[20]; int delay = 5000; /* minimize the safe_mode_delay and prepare to wait up to 5s * for writes to quiesce */ sysfs_set_safemode(mdi, 1); /* 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; rv = sysfs_wait(state_fd, &delay); if (rv < 0 && errno != EINTR) break; lseek(state_fd, 0, SEEK_SET); } if (rv < 0) rv = 1; else if (fping_monitor(sock) == 0 || 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) pr_err("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; }