2 * mdadm - manage Linux "md" devices aka RAID arrays.
4 * Copyright (C) 2001-2009 Neil Brown <neilb@suse.de>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * Email: <neilb@suse.de>
35 char devnm[32]; /* to sync with mdstat info */
39 int active, working, failed, spare, raid;
43 int devstate[MAX_DISKS];
44 dev_t devid[MAX_DISKS];
46 char parent_devnm[32]; /* For subarray, devnm of parent.
49 struct supertype *metadata;
50 struct state *subarray;/* for a container it is a link to first subarray
51 * for a subarray it is a link to next subarray
52 * in the same container */
53 struct state *parent; /* for a subarray it is a link to its container
64 static int make_daemon(char *pidfile);
65 static int check_one_sharer(int scan);
66 static void alert(char *event, char *dev, char *disc, struct alert_info *info);
67 static int check_array(struct state *st, struct mdstat_ent *mdstat,
68 int test, struct alert_info *info,
69 int increments, char *prefer);
70 static int add_new_arrays(struct mdstat_ent *mdstat, struct state **statelist,
71 int test, struct alert_info *info);
72 static void try_spare_migration(struct state *statelist, struct alert_info *info);
73 static void link_containers_with_subarrays(struct state *list);
75 int Monitor(struct mddev_dev *devlist,
76 char *mailaddr, char *alert_cmd,
78 int daemonise, int oneshot,
79 int dosyslog, char *pidfile, int increments,
83 * Every few seconds, scan every md device looking for changes
84 * When a change is found, log it, possibly run the alert command,
85 * and possibly send Email
87 * For each array, we record:
89 * active/working/failed/spare drives
90 * State of each device.
91 * %rebuilt if rebuilding
93 * If the update time changes, check out all the data again
94 * It is possible that we cannot get the state of each device
95 * due to bugs in the md kernel module.
96 * We also read /proc/mdstat to get rebuild percent,
97 * and to get state on all active devices incase of kernel bug.
101 * An active device had Faulty set or Active/Sync removed
103 * A spare device had Faulty set
105 * An active device had a reverse transition
107 * percent went from -1 to +ve
109 * percent went from below to not-below NN%
111 * Couldn't access a device which was previously visible
113 * if we detect an array with active<raid and spare==0
114 * we look at other arrays that have same spare-group
115 * If we find one with active==raid and spare>0,
116 * and if we can get_disk_info and find a name
117 * Then we hot-remove and hot-add to the other array
119 * If devlist is NULL, then we can monitor everything because --scan
120 * was given. We get an initial list from config file and add anything
121 * that appears in /proc/mdstat
124 struct state *statelist = NULL;
127 struct mdstat_ent *mdstat = NULL;
128 char *mailfrom = NULL;
129 struct alert_info info;
132 mailaddr = conf_get_mailaddr();
133 if (mailaddr && ! c->scan)
134 pr_err("Monitor using email address \"%s\" from config file\n",
137 mailfrom = conf_get_mailfrom();
140 alert_cmd = conf_get_program();
141 if (alert_cmd && ! c->scan)
142 pr_err("Monitor using program \"%s\" from config file\n",
145 if (c->scan && !mailaddr && !alert_cmd && !dosyslog) {
146 pr_err("No mail address or alert command - not monitoring.\n");
149 info.alert_cmd = alert_cmd;
150 info.mailaddr = mailaddr;
151 info.mailfrom = mailfrom;
152 info.dosyslog = dosyslog;
155 int rv = make_daemon(pidfile);
161 if (check_one_sharer(c->scan))
164 if (devlist == NULL) {
165 struct mddev_ident *mdlist = conf_get_ident(NULL);
166 for (; mdlist; mdlist=mdlist->next) {
168 if (mdlist->devname == NULL)
170 if (strcasecmp(mdlist->devname, "<ignore>") == 0)
172 st = xcalloc(1, sizeof *st);
173 if (mdlist->devname[0] == '/')
174 st->devname = xstrdup(mdlist->devname);
176 st->devname = xmalloc(8+strlen(mdlist->devname)+1);
177 strcpy(strcpy(st->devname, "/dev/md/"),
180 st->next = statelist;
182 st->percent = RESYNC_UNKNOWN;
184 st->expected_spares = mdlist->spare_disks;
185 if (mdlist->spare_group)
186 st->spare_group = xstrdup(mdlist->spare_group);
190 struct mddev_dev *dv;
191 for (dv=devlist ; dv; dv=dv->next) {
192 struct mddev_ident *mdlist = conf_get_ident(dv->devname);
193 struct state *st = xcalloc(1, sizeof *st);
194 st->devname = xstrdup(dv->devname);
195 st->next = statelist;
197 st->percent = RESYNC_UNKNOWN;
198 st->expected_spares = -1;
200 st->expected_spares = mdlist->spare_disks;
201 if (mdlist->spare_group)
202 st->spare_group = xstrdup(mdlist->spare_group);
210 struct state *st, **stp;
215 mdstat = mdstat_read(oneshot?0:1, 0);
219 for (st=statelist; st; st=st->next)
220 if (check_array(st, mdstat, c->test, &info,
221 increments, c->prefer))
224 /* now check if there are any new devices found in mdstat */
226 new_found = add_new_arrays(mdstat, &statelist, c->test,
229 /* If an array has active < raid && spare == 0 && spare_group != NULL
230 * Look for another array with spare > 0 and active == raid and same spare_group
231 * if found, choose a device and hotremove/hotadd
233 if (share && anydegraded)
234 try_spare_migration(statelist, &info);
239 mdstat_wait(c->delay);
243 for (stp = &statelist; (st = *stp) != NULL; ) {
244 if (st->from_auto && st->err > 5) {
247 free(st->spare_group);
253 for (st2 = statelist; st2; st2 = statelist) {
254 statelist = st2->next;
263 static int make_daemon(char *pidfile)
266 * -1 in the forked daemon
269 * so a none-negative becomes the exit code.
277 pid_file=fopen(pidfile, "w");
279 perror("cannot create pid file");
281 fprintf(pid_file,"%d\n", pid);
292 open("/dev/null", O_RDWR);
299 static int check_one_sharer(int scan)
306 sprintf(path, "%s/autorebuild.pid", MDMON_DIR);
307 fp = fopen(path, "r");
309 if (fscanf(fp, "%d", &pid) != 1)
311 sprintf(dir, "/proc/%d", pid);
312 rv = stat(dir, &buf);
315 pr_err("Only one autorebuild process allowed in scan mode, aborting\n");
319 pr_err("Warning: One autorebuild process already running.\n");
325 if (mkdir(MDMON_DIR, S_IRWXU) < 0 &&
327 pr_err("Can't create autorebuild.pid file\n");
329 fp = fopen(path, "w");
331 pr_err("Cannot create autorebuild.pidfile\n");
334 fprintf(fp, "%d\n", pid);
342 static void alert(char *event, char *dev, char *disc, struct alert_info *info)
346 if (!info->alert_cmd && !info->mailaddr && !info->dosyslog) {
347 time_t now = time(0);
349 printf("%1.15s: %s on %s %s\n", ctime(&now)+4, event, dev, disc?disc:"unknown device");
351 if (info->alert_cmd) {
355 waitpid(pid, NULL, 0);
360 execl(info->alert_cmd, info->alert_cmd,
361 event, dev, disc, NULL);
365 if (info->mailaddr &&
366 (strncmp(event, "Fail", 4)==0 ||
367 strncmp(event, "Test", 4)==0 ||
368 strncmp(event, "Spares", 6)==0 ||
369 strncmp(event, "Degrade", 7)==0)) {
370 FILE *mp = popen(Sendmail, "w");
374 gethostname(hname, sizeof(hname));
375 signal(SIGPIPE, SIG_IGN);
377 fprintf(mp, "From: %s\n", info->mailfrom);
379 fprintf(mp, "From: %s monitoring <root>\n", Name);
380 fprintf(mp, "To: %s\n", info->mailaddr);
381 fprintf(mp, "Subject: %s event on %s:%s\n\n",
385 "This is an automatically generated mail message from %s\n", Name);
386 fprintf(mp, "running on %s\n\n", hname);
389 "A %s event had been detected on md device %s.\n\n", event, dev);
391 if (disc && disc[0] != ' ')
393 "It could be related to component device %s.\n\n", disc);
394 if (disc && disc[0] == ' ')
395 fprintf(mp, "Extra information:%s.\n\n", disc);
397 fprintf(mp, "Faithfully yours, etc.\n");
399 mdstat = fopen("/proc/mdstat", "r");
404 "\nP.S. The /proc/mdstat file currently contains the following:\n\n");
405 while ( (n=fread(buf, 1, sizeof(buf), mdstat)) > 0)
406 n=fwrite(buf, 1, n, mp);
413 /* log the event to syslog maybe */
414 if (info->dosyslog) {
415 /* Log at a different severity depending on the event.
417 * These are the critical events: */
418 if (strncmp(event, "Fail", 4)==0 ||
419 strncmp(event, "Degrade", 7)==0 ||
420 strncmp(event, "DeviceDisappeared", 17)==0)
422 /* Good to know about, but are not failures: */
423 else if (strncmp(event, "Rebuild", 7)==0 ||
424 strncmp(event, "MoveSpare", 9)==0 ||
425 strncmp(event, "Spares", 6) != 0)
426 priority = LOG_WARNING;
427 /* Everything else: */
431 if (disc && disc[0] != ' ')
433 "%s event detected on md device %s, component device %s", event, dev, disc);
436 "%s event detected on md device %s: %s",
440 "%s event detected on md device %s",
445 static int check_array(struct state *st, struct mdstat_ent *mdstat,
446 int test, struct alert_info *ainfo,
447 int increments, char *prefer)
449 /* Update the state 'st' to reflect any changes shown in mdstat,
450 * or found by directly examining the array, and return
451 * '1' if the array is degraded, or '0' if it is optimal (or dead).
453 struct { int state, major, minor; } info[MAX_DISKS];
454 mdu_array_info_t array;
455 struct mdstat_ent *mse = NULL, *mse2;
456 char *dev = st->devname;
464 alert("TestMessage", dev, NULL, ainfo);
466 fd = open("/sys/block", O_RDONLY|O_DIRECTORY);
468 /* Don't open the device unless it is present and
473 fd = sysfs_open(st->devnm, NULL, "array_state");
475 read(fd, buf, 10) < 5 ||
476 strncmp(buf,"clear",5) == 0 ||
477 strncmp(buf,"inact",5) == 0) {
480 fd = sysfs_open(st->devnm, NULL, "level");
481 if (fd < 0 || read(fd, buf, 10) != 0) {
485 alert("DeviceDisappeared", dev, NULL, ainfo);
492 fd = open(dev, O_RDONLY);
495 alert("DeviceDisappeared", dev, NULL, ainfo);
499 fcntl(fd, F_SETFD, FD_CLOEXEC);
500 if (ioctl(fd, GET_ARRAY_INFO, &array)<0) {
502 alert("DeviceDisappeared", dev, NULL, ainfo);
507 /* It's much easier to list what array levels can't
508 * have a device disappear than all of them that can
510 if (array.level == 0 || array.level == -1) {
511 if (!st->err && !st->from_config)
512 alert("DeviceDisappeared", dev, " Wrong-Level", ainfo);
517 if (st->devnm[0] == 0)
518 strcpy(st->devnm, fd2devnm(fd));
520 for (mse2 = mdstat ; mse2 ; mse2=mse2->next)
521 if (strcmp(mse2->devnm, st->devnm) == 0) {
522 mse2->devnm[0] = 0; /* flag it as "used" */
527 /* duplicated array in statelist
528 * or re-created after reading mdstat*/
533 /* this array is in /proc/mdstat */
534 if (array.utime == 0)
535 /* external arrays don't update utime, so
536 * just make sure it is always different. */
537 array.utime = st->utime + 1;;
540 /* New array appeared where previously had an error */
542 st->percent = RESYNC_NONE;
544 alert("NewArray", st->devname, NULL, ainfo);
547 if (st->utime == array.utime &&
548 st->failed == array.failed_disks &&
549 st->working == array.working_disks &&
550 st->spare == array.spare_disks &&
552 mse->percent == st->percent
555 if ((st->active < st->raid) && st->spare == 0)
560 if (st->utime == 0 && /* new array */
561 mse->pattern && strchr(mse->pattern, '_') /* degraded */
563 alert("DegradedArray", dev, NULL, ainfo);
565 if (st->utime == 0 && /* new array */
566 st->expected_spares > 0 &&
567 array.spare_disks < st->expected_spares)
568 alert("SparesMissing", dev, NULL, ainfo);
569 if (st->percent < 0 && st->percent != RESYNC_UNKNOWN &&
571 alert("RebuildStarted", dev, NULL, ainfo);
572 if (st->percent >= 0 &&
574 (mse->percent / increments) > (st->percent / increments)) {
575 char percentalert[15]; // "RebuildNN" (10 chars) or "RebuildStarted" (15 chars)
577 if((mse->percent / increments) == 0)
578 snprintf(percentalert, sizeof(percentalert), "RebuildStarted");
580 snprintf(percentalert, sizeof(percentalert), "Rebuild%02d", mse->percent);
582 alert(percentalert, dev, NULL, ainfo);
585 if (mse->percent == RESYNC_NONE &&
587 /* Rebuild/sync/whatever just finished.
588 * If there is a number in /mismatch_cnt,
589 * we should report that.
592 sysfs_read(-1, st->devnm, GET_MISMATCH);
593 if (sra && sra->mismatch_cnt > 0) {
595 snprintf(cnt, sizeof(cnt),
596 " mismatches found: %d (on raid level %d)",
597 sra->mismatch_cnt, array.level);
598 alert("RebuildFinished", dev, cnt, ainfo);
600 alert("RebuildFinished", dev, NULL, ainfo);
604 st->percent = mse->percent;
606 remaining_disks = array.nr_disks;
607 for (i=0; i<MAX_DISKS && remaining_disks > 0;
609 mdu_disk_info_t disc;
611 if (ioctl(fd, GET_DISK_INFO, &disc) >= 0) {
612 info[i].state = disc.state;
613 info[i].major = disc.major;
614 info[i].minor = disc.minor;
615 if (disc.major || disc.minor)
618 info[i].major = info[i].minor = 0;
622 if (mse->metadata_version &&
623 strncmp(mse->metadata_version, "external:", 9) == 0 &&
624 is_subarray(mse->metadata_version+9)) {
626 strcpy(st->parent_devnm,
627 mse->metadata_version+10);
628 sl = strchr(st->parent_devnm, '/');
632 st->parent_devnm[0] = 0;
633 if (st->metadata == NULL &&
634 st->parent_devnm[0] == 0)
635 st->metadata = super_by_fd(fd, NULL);
639 for (i=0; i<MAX_DISKS; i++) {
640 mdu_disk_info_t disc = {0,0,0,0,0};
646 (info[i].major || info[i].minor)) {
647 newstate = info[i].state;
648 dv = map_dev_preferred(
649 info[i].major, info[i].minor, 1,
651 disc.state = newstate;
652 disc.major = info[i].major;
653 disc.minor = info[i].minor;
655 newstate = (1 << MD_DISK_REMOVED);
657 if (dv == NULL && st->devid[i])
658 dv = map_dev_preferred(
660 minor(st->devid[i]), 1, prefer);
661 change = newstate ^ st->devstate[i];
662 if (st->utime && change && !st->err && !new_array) {
663 if ((st->devstate[i]&change)&(1<<MD_DISK_SYNC))
664 alert("Fail", dev, dv, ainfo);
665 else if ((newstate & (1<<MD_DISK_FAULTY)) &&
666 (disc.major || disc.minor) &&
667 st->devid[i] == makedev(disc.major, disc.minor))
668 alert("FailSpare", dev, dv, ainfo);
669 else if ((newstate&change)&(1<<MD_DISK_SYNC))
670 alert("SpareActive", dev, dv, ainfo);
672 st->devstate[i] = newstate;
673 st->devid[i] = makedev(disc.major, disc.minor);
675 st->active = array.active_disks;
676 st->working = array.working_disks;
677 st->spare = array.spare_disks;
678 st->failed = array.failed_disks;
679 st->utime = array.utime;
680 st->raid = array.raid_disks;
682 if ((st->active < st->raid) && st->spare == 0)
687 static int add_new_arrays(struct mdstat_ent *mdstat, struct state **statelist,
688 int test, struct alert_info *info)
690 struct mdstat_ent *mse;
694 for (mse=mdstat; mse; mse=mse->next)
696 (!mse->level || /* retrieve containers */
697 (strcmp(mse->level, "raid0") != 0 &&
698 strcmp(mse->level, "linear") != 0))
700 struct state *st = xcalloc(1, sizeof *st);
701 mdu_array_info_t array;
704 name = get_md_name(mse->devnm);
710 st->devname = xstrdup(name);
711 if ((fd = open(st->devname, O_RDONLY)) < 0 ||
712 ioctl(fd, GET_ARRAY_INFO, &array)< 0) {
714 if (fd >=0) close(fd);
715 put_md_name(st->devname);
718 st->metadata->ss->free_super(st->metadata);
725 st->next = *statelist;
728 strcpy(st->devnm, mse->devnm);
729 st->percent = RESYNC_UNKNOWN;
730 st->expected_spares = -1;
731 if (mse->metadata_version &&
732 strncmp(mse->metadata_version, "external:", 9) == 0 &&
733 is_subarray(mse->metadata_version+9)) {
735 strcpy(st->parent_devnm,
736 mse->metadata_version+10);
737 sl = strchr(st->parent_devnm, '/');
740 st->parent_devnm[0] = 0;
743 alert("TestMessage", st->devname, NULL, info);
749 static int get_min_spare_size_required(struct state *st, unsigned long long *sizep)
754 !st->metadata->ss->min_acceptable_spare_size) {
759 fd = open(st->devname, O_RDONLY);
762 if (st->metadata->ss->external)
763 st->metadata->ss->load_container(st->metadata, fd, st->devname);
765 st->metadata->ss->load_super(st->metadata, fd, st->devname);
767 if (!st->metadata->sb)
769 *sizep = st->metadata->ss->min_acceptable_spare_size(st->metadata);
770 st->metadata->ss->free_super(st->metadata);
775 static int check_donor(struct state *from, struct state *to)
782 /* Cannot move from a member */
786 for (sub = from->subarray; sub; sub = sub->subarray)
787 /* If source array has degraded subarrays, don't
790 if (sub->active < sub->raid)
792 if (from->metadata->ss->external == 0)
793 if (from->active < from->raid)
795 if (from->spare <= 0)
800 static dev_t choose_spare(struct state *from, struct state *to,
801 struct domainlist *domlist, unsigned long long min_size)
806 for (d = from->raid; !dev && d < MAX_DISKS; d++) {
807 if (from->devid[d] > 0 &&
808 from->devstate[d] == 0) {
809 struct dev_policy *pol;
810 unsigned long long dev_size;
812 if (to->metadata->ss->external &&
813 test_partition_from_id(from->devid[d]))
817 dev_size_from_id(from->devid[d], &dev_size) &&
821 pol = devid_policy(from->devid[d]);
822 if (from->spare_group)
823 pol_add(&pol, pol_domain,
824 from->spare_group, NULL);
825 if (domain_test(domlist, pol, to->metadata->ss->name) == 1)
826 dev = from->devid[d];
827 dev_policy_free(pol);
833 static dev_t container_choose_spare(struct state *from, struct state *to,
834 struct domainlist *domlist,
835 unsigned long long min_size, int active)
837 /* This is similar to choose_spare, but we cannot trust devstate,
838 * so we need to read the metadata instead
841 struct supertype *st = from->metadata;
842 int fd = open(from->devname, O_RDONLY);
848 if (!st->ss->getinfo_super_disks) {
853 err = st->ss->load_container(st, fd, NULL);
859 /* We must check if number of active disks has not increased
860 * since ioctl in main loop. mdmon may have added spare
861 * to subarray. If so we do not need to look for more spares
862 * so return non zero value */
865 list = st->ss->getinfo_super_disks(st);
867 st->ss->free_super(st);
872 if (dp->disk.state & (1<<MD_DISK_SYNC) &&
873 !(dp->disk.state & (1<<MD_DISK_FAULTY)))
878 if (active < active_cnt) {
879 /* Spare just activated.*/
880 st->ss->free_super(st);
885 /* We only need one spare so full list not needed */
886 list = container_choose_spares(st, min_size, domlist, from->spare_group,
887 to->metadata->ss->name, 1);
889 struct mdinfo *disks = list->devs;
891 dev = makedev(disks->disk.major, disks->disk.minor);
894 st->ss->free_super(st);
898 static void try_spare_migration(struct state *statelist, struct alert_info *info)
903 link_containers_with_subarrays(statelist);
904 for (st = statelist; st; st = st->next)
905 if (st->active < st->raid &&
906 st->spare == 0 && !st->err) {
907 struct domainlist *domlist = NULL;
909 struct state *to = st;
910 unsigned long long min_size;
912 if (to->parent_devnm[0] && !to->parent)
913 /* subarray monitored without parent container
914 * we can't move spares here */
918 /* member of a container */
921 if (get_min_spare_size_required(to, &min_size))
923 if (to->metadata->ss->external) {
924 /* We must make sure there is
925 * no suitable spare in container already.
926 * If there is we don't add more */
927 dev_t devid = container_choose_spare(
928 to, to, NULL, min_size, st->active);
932 for (d = 0; d < MAX_DISKS; d++)
934 domainlist_add_dev(&domlist,
936 to->metadata->ss->name);
938 domain_add(&domlist, to->spare_group);
940 * No spare migration if the destination
941 * has no domain. Skip this array.
945 for (from=statelist ; from ; from=from->next) {
947 if (!check_donor(from, to))
949 if (from->metadata->ss->external)
950 devid = container_choose_spare(
951 from, to, domlist, min_size, 0);
953 devid = choose_spare(from, to, domlist,
956 && move_spare(from->devname, to->devname, devid)) {
957 alert("MoveSpare", to->devname, from->devname, info);
961 domain_free(domlist);
965 /* search the statelist to connect external
966 * metadata subarrays with their containers
967 * We always completely rebuild the tree from scratch as
968 * that is safest considering the possibility of entries
969 * disappearing or changing.
971 static void link_containers_with_subarrays(struct state *list)
975 for (st = list; st; st = st->next) {
979 for (st = list; st; st = st->next)
980 if (st->parent_devnm[0])
981 for (cont = list; cont; cont = cont->next)
983 cont->parent_devnm[0] == 0 &&
984 strcmp(cont->devnm, st->parent_devnm) == 0) {
986 st->subarray = cont->subarray;
992 /* Not really Monitor but ... */
998 int frozen_remaining = 3;
1000 if (stat(dev, &stb) != 0) {
1001 pr_err("Cannot find %s: %s\n", dev,
1005 strcpy(devnm, stat2devnm(&stb));
1008 struct mdstat_ent *ms = mdstat_read(1, 0);
1009 struct mdstat_ent *e;
1011 for (e=ms ; e; e=e->next)
1012 if (strcmp(e->devnm, devnm) == 0)
1015 if (e && e->percent == RESYNC_NONE) {
1016 /* We could be in the brief pause before something
1017 * starts. /proc/mdstat doesn't show that, but
1022 sysfs_init(&mdi, -1, devnm);
1023 if (sysfs_get_str(&mdi, NULL, "sync_action",
1025 strcmp(buf,"idle\n") != 0) {
1026 e->percent = RESYNC_UNKNOWN;
1027 if (strcmp(buf, "frozen\n") == 0) {
1028 if (frozen_remaining == 0)
1029 e->percent = RESYNC_NONE;
1031 frozen_remaining -= 1;
1035 if (!e || e->percent == RESYNC_NONE) {
1036 if (e && e->metadata_version &&
1037 strncmp(e->metadata_version, "external:", 9) == 0) {
1038 if (is_subarray(&e->metadata_version[9]))
1039 ping_monitor(&e->metadata_version[9]);
1041 ping_monitor(devnm);
1054 static char *clean_states[] = {
1055 "clear", "inactive", "readonly", "read-auto", "clean", NULL };
1057 int WaitClean(char *dev, int sock, int verbose)
1064 fd = open(dev, O_RDONLY);
1067 pr_err("Couldn't open %s: %s\n", dev, strerror(errno));
1071 strcpy(devnm, fd2devnm(fd));
1072 mdi = sysfs_read(fd, devnm, GET_VERSION|GET_LEVEL|GET_SAFEMODE);
1075 pr_err("Failed to read sysfs attributes for %s\n", dev);
1080 switch(mdi->array.level) {
1082 case LEVEL_MULTIPATH:
1084 /* safemode delay is irrelevant for these levels */
1088 /* for internal metadata the kernel handles the final clean
1089 * transition, containers can never be dirty
1091 if (!is_subarray(mdi->text_version))
1094 /* safemode disabled ? */
1095 if (mdi->safe_mode_delay == 0)
1099 int state_fd = sysfs_open(fd2devnm(fd), NULL, "array_state");
1103 /* minimize the safe_mode_delay and prepare to wait up to 5s
1104 * for writes to quiesce
1106 sysfs_set_safemode(mdi, 1);
1108 /* wait for array_state to be clean */
1110 rv = read(state_fd, buf, sizeof(buf));
1113 if (sysfs_match_word(buf, clean_states) <= 4)
1115 rv = sysfs_wait(state_fd, &delay);
1116 if (rv < 0 && errno != EINTR)
1118 lseek(state_fd, 0, SEEK_SET);
1122 else if (fping_monitor(sock) == 0 ||
1123 ping_monitor(mdi->text_version) == 0) {
1124 /* we need to ping to close the window between array
1125 * state transitioning to clean and the metadata being
1132 pr_err("Error waiting for %s to be clean\n",
1135 /* restore the original safe_mode_delay */
1136 sysfs_set_safemode(mdi, mdi->safe_mode_delay);
1145 #endif /* MDASSEMBLE */