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 int devnum
; /* to sync with mdstat info */
39 int active
, working
, failed
, spare
, raid
;
41 int devstate
[MAX_DISKS
];
42 dev_t devid
[MAX_DISKS
];
44 int parent_dev
; /* For subarray, devnum of parent.
47 struct supertype
*metadata
;
48 struct state
*subarray
;/* for a container it is a link to first subarray
49 * for a subarray it is a link to next subarray
50 * in the same container */
51 struct state
*parent
; /* for a subarray it is a link to its container
62 static int make_daemon(char *pidfile
);
63 static int check_one_sharer(int scan
);
64 static void alert(char *event
, char *dev
, char *disc
, struct alert_info
*info
);
65 static int check_array(struct state
*st
, struct mdstat_ent
*mdstat
,
66 int test
, struct alert_info
*info
,
67 int increments
, char *prefer
);
68 static int add_new_arrays(struct mdstat_ent
*mdstat
, struct state
**statelist
,
69 int test
, struct alert_info
*info
);
70 static void try_spare_migration(struct state
*statelist
, struct alert_info
*info
);
71 static void link_containers_with_subarrays(struct state
*list
);
73 int Monitor(struct mddev_dev
*devlist
,
74 char *mailaddr
, char *alert_cmd
,
75 int period
, int daemonise
, int scan
, int oneshot
,
76 int dosyslog
, int test
, char *pidfile
, int increments
,
77 int share
, char *prefer
)
80 * Every few seconds, scan every md device looking for changes
81 * When a change is found, log it, possibly run the alert command,
82 * and possibly send Email
84 * For each array, we record:
86 * active/working/failed/spare drives
87 * State of each device.
88 * %rebuilt if rebuilding
90 * If the update time changes, check out all the data again
91 * It is possible that we cannot get the state of each device
92 * due to bugs in the md kernel module.
93 * We also read /proc/mdstat to get rebuild percent,
94 * and to get state on all active devices incase of kernel bug.
98 * An active device had Faulty set or Active/Sync removed
100 * A spare device had Faulty set
102 * An active device had a reverse transition
104 * percent went from -1 to +ve
106 * percent went from below to not-below NN%
108 * Couldn't access a device which was previously visible
110 * if we detect an array with active<raid and spare==0
111 * we look at other arrays that have same spare-group
112 * If we find one with active==raid and spare>0,
113 * and if we can get_disk_info and find a name
114 * Then we hot-remove and hot-add to the other array
116 * If devlist is NULL, then we can monitor everything because --scan
117 * was given. We get an initial list from config file and add anything
118 * that appears in /proc/mdstat
121 struct state
*statelist
= NULL
;
124 struct mdstat_ent
*mdstat
= NULL
;
125 char *mailfrom
= NULL
;
126 struct alert_info info
;
129 mailaddr
= conf_get_mailaddr();
130 if (mailaddr
&& ! scan
)
131 pr_err("Monitor using email address \"%s\" from config file\n",
134 mailfrom
= conf_get_mailfrom();
137 alert_cmd
= conf_get_program();
138 if (alert_cmd
&& ! scan
)
139 pr_err("Monitor using program \"%s\" from config file\n",
142 if (scan
&& !mailaddr
&& !alert_cmd
&& !dosyslog
) {
143 pr_err("No mail address or alert command - not monitoring.\n");
146 info
.alert_cmd
= alert_cmd
;
147 info
.mailaddr
= mailaddr
;
148 info
.mailfrom
= mailfrom
;
149 info
.dosyslog
= dosyslog
;
152 int rv
= make_daemon(pidfile
);
158 if (check_one_sharer(scan
))
161 if (devlist
== NULL
) {
162 struct mddev_ident
*mdlist
= conf_get_ident(NULL
);
163 for (; mdlist
; mdlist
=mdlist
->next
) {
165 if (mdlist
->devname
== NULL
)
167 if (strcasecmp(mdlist
->devname
, "<ignore>") == 0)
169 st
= xcalloc(1, sizeof *st
);
170 if (mdlist
->devname
[0] == '/')
171 st
->devname
= xstrdup(mdlist
->devname
);
173 st
->devname
= xmalloc(8+strlen(mdlist
->devname
)+1);
174 strcpy(strcpy(st
->devname
, "/dev/md/"),
177 st
->next
= statelist
;
178 st
->devnum
= INT_MAX
;
179 st
->percent
= RESYNC_UNKNOWN
;
180 st
->expected_spares
= mdlist
->spare_disks
;
181 if (mdlist
->spare_group
)
182 st
->spare_group
= xstrdup(mdlist
->spare_group
);
186 struct mddev_dev
*dv
;
187 for (dv
=devlist
; dv
; dv
=dv
->next
) {
188 struct mddev_ident
*mdlist
= conf_get_ident(dv
->devname
);
189 struct state
*st
= xcalloc(1, sizeof *st
);
190 st
->devname
= xstrdup(dv
->devname
);
191 st
->next
= statelist
;
192 st
->devnum
= INT_MAX
;
193 st
->percent
= RESYNC_UNKNOWN
;
194 st
->expected_spares
= -1;
196 st
->expected_spares
= mdlist
->spare_disks
;
197 if (mdlist
->spare_group
)
198 st
->spare_group
= xstrdup(mdlist
->spare_group
);
212 mdstat
= mdstat_read(oneshot
?0:1, 0);
214 for (st
=statelist
; st
; st
=st
->next
)
215 if (check_array(st
, mdstat
, test
, &info
,
219 /* now check if there are any new devices found in mdstat */
221 new_found
= add_new_arrays(mdstat
, &statelist
, test
,
224 /* If an array has active < raid && spare == 0 && spare_group != NULL
225 * Look for another array with spare > 0 and active == raid and same spare_group
226 * if found, choose a device and hotremove/hotadd
228 if (share
&& anydegraded
)
229 try_spare_migration(statelist
, &info
);
238 for (st2
= statelist
; st2
; st2
= statelist
) {
239 statelist
= st2
->next
;
248 static int make_daemon(char *pidfile
)
251 * -1 in the forked daemon
254 * so a none-negative becomes the exit code.
262 pid_file
=fopen(pidfile
, "w");
264 perror("cannot create pid file");
266 fprintf(pid_file
,"%d\n", pid
);
277 open("/dev/null", O_RDWR
);
284 static int check_one_sharer(int scan
)
291 sprintf(path
, "%s/autorebuild.pid", MDMON_DIR
);
292 fp
= fopen(path
, "r");
294 if (fscanf(fp
, "%d", &pid
) != 1)
296 sprintf(dir
, "/proc/%d", pid
);
297 rv
= stat(dir
, &buf
);
301 "autorebuild process allowed"
302 " in scan mode, aborting\n");
306 pr_err("Warning: One"
307 " autorebuild process already"
314 if (mkdir(MDMON_DIR
, S_IRWXU
) < 0 &&
316 pr_err("Can't create "
317 "autorebuild.pid file\n");
319 fp
= fopen(path
, "w");
321 pr_err("Cannot create"
326 fprintf(fp
, "%d\n", pid
);
334 static void alert(char *event
, char *dev
, char *disc
, struct alert_info
*info
)
338 if (!info
->alert_cmd
&& !info
->mailaddr
&& !info
->dosyslog
) {
339 time_t now
= time(0);
341 printf("%1.15s: %s on %s %s\n", ctime(&now
)+4, event
, dev
, disc
?disc
:"unknown device");
343 if (info
->alert_cmd
) {
347 waitpid(pid
, NULL
, 0);
352 execl(info
->alert_cmd
, info
->alert_cmd
,
353 event
, dev
, disc
, NULL
);
357 if (info
->mailaddr
&&
358 (strncmp(event
, "Fail", 4)==0 ||
359 strncmp(event
, "Test", 4)==0 ||
360 strncmp(event
, "Spares", 6)==0 ||
361 strncmp(event
, "Degrade", 7)==0)) {
362 FILE *mp
= popen(Sendmail
, "w");
366 gethostname(hname
, sizeof(hname
));
367 signal(SIGPIPE
, SIG_IGN
);
369 fprintf(mp
, "From: %s\n", info
->mailfrom
);
371 fprintf(mp
, "From: " Name
" monitoring <root>\n");
372 fprintf(mp
, "To: %s\n", info
->mailaddr
);
373 fprintf(mp
, "Subject: %s event on %s:%s\n\n",
377 "This is an automatically generated"
378 " mail message from " Name
"\n");
379 fprintf(mp
, "running on %s\n\n", hname
);
382 "A %s event had been detected on"
383 " md device %s.\n\n", event
, dev
);
385 if (disc
&& disc
[0] != ' ')
387 "It could be related to"
388 " component device %s.\n\n", disc
);
389 if (disc
&& disc
[0] == ' ')
390 fprintf(mp
, "Extra information:%s.\n\n", disc
);
392 fprintf(mp
, "Faithfully yours, etc.\n");
394 mdstat
= fopen("/proc/mdstat", "r");
399 "\nP.S. The /proc/mdstat file"
400 " currently contains the following:\n\n");
401 while ( (n
=fread(buf
, 1, sizeof(buf
), mdstat
)) > 0)
402 n
=fwrite(buf
, 1, n
, mp
);
409 /* log the event to syslog maybe */
410 if (info
->dosyslog
) {
411 /* Log at a different severity depending on the event.
413 * These are the critical events: */
414 if (strncmp(event
, "Fail", 4)==0 ||
415 strncmp(event
, "Degrade", 7)==0 ||
416 strncmp(event
, "DeviceDisappeared", 17)==0)
418 /* Good to know about, but are not failures: */
419 else if (strncmp(event
, "Rebuild", 7)==0 ||
420 strncmp(event
, "MoveSpare", 9)==0 ||
421 strncmp(event
, "Spares", 6) != 0)
422 priority
= LOG_WARNING
;
423 /* Everything else: */
429 "%s event detected on md device %s,"
430 " component device %s", event
, dev
, disc
);
433 "%s event detected on md device %s",
438 static int check_array(struct state
*st
, struct mdstat_ent
*mdstat
,
439 int test
, struct alert_info
*ainfo
,
440 int increments
, char *prefer
)
442 /* Update the state 'st' to reflect any changes shown in mdstat,
443 * or found by directly examining the array, and return
444 * '1' if the array is degraded, or '0' if it is optimal (or dead).
446 struct { int state
, major
, minor
; } info
[MAX_DISKS
];
447 mdu_array_info_t array
;
448 struct mdstat_ent
*mse
= NULL
, *mse2
;
449 char *dev
= st
->devname
;
457 alert("TestMessage", dev
, NULL
, ainfo
);
458 fd
= open(dev
, O_RDONLY
);
461 alert("DeviceDisappeared", dev
, NULL
, ainfo
);
465 fcntl(fd
, F_SETFD
, FD_CLOEXEC
);
466 if (ioctl(fd
, GET_ARRAY_INFO
, &array
)<0) {
468 alert("DeviceDisappeared", dev
, NULL
, ainfo
);
473 /* It's much easier to list what array levels can't
474 * have a device disappear than all of them that can
476 if (array
.level
== 0 || array
.level
== -1) {
478 alert("DeviceDisappeared", dev
, "Wrong-Level", ainfo
);
483 if (st
->devnum
== INT_MAX
) {
485 if (fstat(fd
, &stb
) == 0 &&
486 (S_IFMT
&stb
.st_mode
)==S_IFBLK
) {
487 if (major(stb
.st_rdev
) == MD_MAJOR
)
488 st
->devnum
= minor(stb
.st_rdev
);
490 st
->devnum
= -1- (minor(stb
.st_rdev
)>>6);
494 for (mse2
= mdstat
; mse2
; mse2
=mse2
->next
)
495 if (mse2
->devnum
== st
->devnum
) {
496 mse2
->devnum
= INT_MAX
; /* flag it as "used" */
501 /* duplicated array in statelist
502 * or re-created after reading mdstat*/
507 /* this array is in /proc/mdstat */
508 if (array
.utime
== 0)
509 /* external arrays don't update utime, so
510 * just make sure it is always different. */
511 array
.utime
= st
->utime
+ 1;;
514 /* New array appeared where previously had and error */
516 st
->percent
= RESYNC_NONE
;
518 alert("NewArray", st
->devname
, NULL
, ainfo
);
521 if (st
->utime
== array
.utime
&&
522 st
->failed
== array
.failed_disks
&&
523 st
->working
== array
.working_disks
&&
524 st
->spare
== array
.spare_disks
&&
526 mse
->percent
== st
->percent
529 if ((st
->active
< st
->raid
) && st
->spare
== 0)
534 if (st
->utime
== 0 && /* new array */
535 mse
->pattern
&& strchr(mse
->pattern
, '_') /* degraded */
537 alert("DegradedArray", dev
, NULL
, ainfo
);
539 if (st
->utime
== 0 && /* new array */
540 st
->expected_spares
> 0 &&
541 array
.spare_disks
< st
->expected_spares
)
542 alert("SparesMissing", dev
, NULL
, ainfo
);
543 if (st
->percent
< 0 && st
->percent
!= RESYNC_UNKNOWN
&&
545 alert("RebuildStarted", dev
, NULL
, ainfo
);
546 if (st
->percent
>= 0 &&
548 (mse
->percent
/ increments
) > (st
->percent
/ increments
)) {
549 char percentalert
[15]; // "RebuildNN" (10 chars) or "RebuildStarted" (15 chars)
551 if((mse
->percent
/ increments
) == 0)
552 snprintf(percentalert
, sizeof(percentalert
), "RebuildStarted");
554 snprintf(percentalert
, sizeof(percentalert
), "Rebuild%02d", mse
->percent
);
556 alert(percentalert
, dev
, NULL
, ainfo
);
559 if (mse
->percent
== RESYNC_NONE
&&
561 /* Rebuild/sync/whatever just finished.
562 * If there is a number in /mismatch_cnt,
563 * we should report that.
566 sysfs_read(-1, st
->devnum
, GET_MISMATCH
);
567 if (sra
&& sra
->mismatch_cnt
> 0) {
569 snprintf(cnt
, sizeof(cnt
),
570 " mismatches found: %d (on raid level %d)",
571 sra
->mismatch_cnt
, array
.level
);
572 alert("RebuildFinished", dev
, cnt
, ainfo
);
574 alert("RebuildFinished", dev
, NULL
, ainfo
);
578 st
->percent
= mse
->percent
;
580 remaining_disks
= array
.nr_disks
;
581 for (i
=0; i
<MAX_DISKS
&& remaining_disks
> 0;
583 mdu_disk_info_t disc
;
585 if (ioctl(fd
, GET_DISK_INFO
, &disc
) >= 0) {
586 info
[i
].state
= disc
.state
;
587 info
[i
].major
= disc
.major
;
588 info
[i
].minor
= disc
.minor
;
589 if (disc
.major
|| disc
.minor
)
592 info
[i
].major
= info
[i
].minor
= 0;
596 if (mse
->metadata_version
&&
597 strncmp(mse
->metadata_version
, "external:", 9) == 0 &&
598 is_subarray(mse
->metadata_version
+9))
600 devname2devnum(mse
->metadata_version
+10);
602 st
->parent_dev
= NoMdDev
;
603 if (st
->metadata
== NULL
&&
604 st
->parent_dev
== NoMdDev
)
605 st
->metadata
= super_by_fd(fd
, NULL
);
609 for (i
=0; i
<MAX_DISKS
; i
++) {
610 mdu_disk_info_t disc
= {0,0,0,0,0};
616 (info
[i
].major
|| info
[i
].minor
)) {
617 newstate
= info
[i
].state
;
618 dv
= map_dev_preferred(
619 info
[i
].major
, info
[i
].minor
, 1,
621 disc
.state
= newstate
;
622 disc
.major
= info
[i
].major
;
623 disc
.minor
= info
[i
].minor
;
625 newstate
= (1 << MD_DISK_REMOVED
);
627 if (dv
== NULL
&& st
->devid
[i
])
628 dv
= map_dev_preferred(
630 minor(st
->devid
[i
]), 1, prefer
);
631 change
= newstate
^ st
->devstate
[i
];
632 if (st
->utime
&& change
&& !st
->err
&& !new_array
) {
633 if ((st
->devstate
[i
]&change
)&(1<<MD_DISK_SYNC
))
634 alert("Fail", dev
, dv
, ainfo
);
635 else if ((newstate
& (1<<MD_DISK_FAULTY
)) &&
636 (disc
.major
|| disc
.minor
) &&
637 st
->devid
[i
] == makedev(disc
.major
, disc
.minor
))
638 alert("FailSpare", dev
, dv
, ainfo
);
639 else if ((newstate
&change
)&(1<<MD_DISK_SYNC
))
640 alert("SpareActive", dev
, dv
, ainfo
);
642 st
->devstate
[i
] = newstate
;
643 st
->devid
[i
] = makedev(disc
.major
, disc
.minor
);
645 st
->active
= array
.active_disks
;
646 st
->working
= array
.working_disks
;
647 st
->spare
= array
.spare_disks
;
648 st
->failed
= array
.failed_disks
;
649 st
->utime
= array
.utime
;
650 st
->raid
= array
.raid_disks
;
652 if ((st
->active
< st
->raid
) && st
->spare
== 0)
657 static int add_new_arrays(struct mdstat_ent
*mdstat
, struct state
**statelist
,
658 int test
, struct alert_info
*info
)
660 struct mdstat_ent
*mse
;
663 for (mse
=mdstat
; mse
; mse
=mse
->next
)
664 if (mse
->devnum
!= INT_MAX
&&
665 (!mse
->level
|| /* retrieve containers */
666 (strcmp(mse
->level
, "raid0") != 0 &&
667 strcmp(mse
->level
, "linear") != 0))
669 struct state
*st
= xcalloc(1, sizeof *st
);
670 mdu_array_info_t array
;
672 st
->devname
= xstrdup(get_md_name(mse
->devnum
));
673 if ((fd
= open(st
->devname
, O_RDONLY
)) < 0 ||
674 ioctl(fd
, GET_ARRAY_INFO
, &array
)< 0) {
676 if (fd
>=0) close(fd
);
677 put_md_name(st
->devname
);
680 st
->metadata
->ss
->free_super(st
->metadata
);
687 st
->next
= *statelist
;
689 st
->devnum
= mse
->devnum
;
690 st
->percent
= RESYNC_UNKNOWN
;
691 st
->expected_spares
= -1;
692 if (mse
->metadata_version
&&
693 strncmp(mse
->metadata_version
, "external:", 9) == 0 &&
694 is_subarray(mse
->metadata_version
+9))
696 devname2devnum(mse
->metadata_version
+10);
698 st
->parent_dev
= NoMdDev
;
701 alert("TestMessage", st
->devname
, NULL
, info
);
707 static int get_min_spare_size_required(struct state
*st
, unsigned long long *sizep
)
712 !st
->metadata
->ss
->min_acceptable_spare_size
) {
717 fd
= open(st
->devname
, O_RDONLY
);
720 if (st
->metadata
->ss
->external
)
721 st
->metadata
->ss
->load_container(st
->metadata
, fd
, st
->devname
);
723 st
->metadata
->ss
->load_super(st
->metadata
, fd
, st
->devname
);
725 if (!st
->metadata
->sb
)
727 *sizep
= st
->metadata
->ss
->min_acceptable_spare_size(st
->metadata
);
728 st
->metadata
->ss
->free_super(st
->metadata
);
733 static int check_donor(struct state
*from
, struct state
*to
)
740 /* Cannot move from a member */
744 for (sub
= from
->subarray
; sub
; sub
= sub
->subarray
)
745 /* If source array has degraded subarrays, don't
748 if (sub
->active
< sub
->raid
)
750 if (from
->metadata
->ss
->external
== 0)
751 if (from
->active
< from
->raid
)
753 if (from
->spare
<= 0)
758 static dev_t
choose_spare(struct state
*from
, struct state
*to
,
759 struct domainlist
*domlist
, unsigned long long min_size
)
764 for (d
= from
->raid
; !dev
&& d
< MAX_DISKS
; d
++) {
765 if (from
->devid
[d
] > 0 &&
766 from
->devstate
[d
] == 0) {
767 struct dev_policy
*pol
;
768 unsigned long long dev_size
;
770 if (to
->metadata
->ss
->external
&&
771 test_partition_from_id(from
->devid
[d
]))
775 dev_size_from_id(from
->devid
[d
], &dev_size
) &&
779 pol
= devnum_policy(from
->devid
[d
]);
780 if (from
->spare_group
)
781 pol_add(&pol
, pol_domain
,
782 from
->spare_group
, NULL
);
783 if (domain_test(domlist
, pol
, to
->metadata
->ss
->name
) == 1)
784 dev
= from
->devid
[d
];
785 dev_policy_free(pol
);
791 static dev_t
container_choose_spare(struct state
*from
, struct state
*to
,
792 struct domainlist
*domlist
,
793 unsigned long long min_size
, int active
)
795 /* This is similar to choose_spare, but we cannot trust devstate,
796 * so we need to read the metadata instead
799 struct supertype
*st
= from
->metadata
;
800 int fd
= open(from
->devname
, O_RDONLY
);
806 if (!st
->ss
->getinfo_super_disks
) {
811 err
= st
->ss
->load_container(st
, fd
, NULL
);
817 /* We must check if number of active disks has not increased
818 * since ioctl in main loop. mdmon may have added spare
819 * to subarray. If so we do not need to look for more spares
820 * so return non zero value */
823 list
= st
->ss
->getinfo_super_disks(st
);
825 st
->ss
->free_super(st
);
830 if (dp
->disk
.state
& (1<<MD_DISK_SYNC
) &&
831 !(dp
->disk
.state
& (1<<MD_DISK_FAULTY
)))
836 if (active
< active_cnt
) {
837 /* Spare just activated.*/
838 st
->ss
->free_super(st
);
843 /* We only need one spare so full list not needed */
844 list
= container_choose_spares(st
, min_size
, domlist
, from
->spare_group
,
845 to
->metadata
->ss
->name
, 1);
847 struct mdinfo
*disks
= list
->devs
;
849 dev
= makedev(disks
->disk
.major
, disks
->disk
.minor
);
852 st
->ss
->free_super(st
);
857 static void try_spare_migration(struct state
*statelist
, struct alert_info
*info
)
862 link_containers_with_subarrays(statelist
);
863 for (st
= statelist
; st
; st
= st
->next
)
864 if (st
->active
< st
->raid
&&
865 st
->spare
== 0 && !st
->err
) {
866 struct domainlist
*domlist
= NULL
;
868 struct state
*to
= st
;
869 unsigned long long min_size
;
871 if (to
->parent_dev
!= NoMdDev
&& !to
->parent
)
872 /* subarray monitored without parent container
873 * we can't move spares here */
877 /* member of a container */
880 if (get_min_spare_size_required(to
, &min_size
))
882 if (to
->metadata
->ss
->external
) {
883 /* We must make sure there is
884 * no suitable spare in container already.
885 * If there is we don't add more */
886 dev_t devid
= container_choose_spare(
887 to
, to
, NULL
, min_size
, st
->active
);
891 for (d
= 0; d
< MAX_DISKS
; d
++)
893 domainlist_add_dev(&domlist
,
895 to
->metadata
->ss
->name
);
897 domain_add(&domlist
, to
->spare_group
);
899 * No spare migration if the destination
900 * has no domain. Skip this array.
904 for (from
=statelist
; from
; from
=from
->next
) {
906 if (!check_donor(from
, to
))
908 if (from
->metadata
->ss
->external
)
909 devid
= container_choose_spare(
910 from
, to
, domlist
, min_size
, 0);
912 devid
= choose_spare(from
, to
, domlist
,
915 && move_spare(from
->devname
, to
->devname
, devid
)) {
916 alert("MoveSpare", to
->devname
, from
->devname
, info
);
920 domain_free(domlist
);
924 /* search the statelist to connect external
925 * metadata subarrays with their containers
926 * We always completely rebuild the tree from scratch as
927 * that is safest considering the possibility of entries
928 * disappearing or changing.
930 static void link_containers_with_subarrays(struct state
*list
)
934 for (st
= list
; st
; st
= st
->next
) {
938 for (st
= list
; st
; st
= st
->next
)
939 if (st
->parent_dev
!= NoMdDev
)
940 for (cont
= list
; cont
; cont
= cont
->next
)
942 cont
->parent_dev
== NoMdDev
&&
943 cont
->devnum
== st
->parent_dev
) {
945 st
->subarray
= cont
->subarray
;
951 /* Not really Monitor but ... */
958 if (stat(dev
, &stb
) != 0) {
959 pr_err("Cannot find %s: %s\n", dev
,
963 devnum
= stat2devnum(&stb
);
966 struct mdstat_ent
*ms
= mdstat_read(1, 0);
967 struct mdstat_ent
*e
;
969 for (e
=ms
; e
; e
=e
->next
)
970 if (e
->devnum
== devnum
)
973 if (!e
|| e
->percent
< 0) {
974 if (e
&& e
->metadata_version
&&
975 strncmp(e
->metadata_version
, "external:", 9) == 0) {
976 if (is_subarray(&e
->metadata_version
[9]))
977 ping_monitor(&e
->metadata_version
[9]);
979 ping_monitor_by_id(devnum
);
992 static char *clean_states
[] = {
993 "clear", "inactive", "readonly", "read-auto", "clean", NULL
};
995 int WaitClean(char *dev
, int sock
, int verbose
)
1002 fd
= open(dev
, O_RDONLY
);
1005 pr_err("Couldn't open %s: %s\n", dev
, strerror(errno
));
1009 devnum
= fd2devnum(fd
);
1010 mdi
= sysfs_read(fd
, devnum
, GET_VERSION
|GET_LEVEL
|GET_SAFEMODE
);
1013 pr_err("Failed to read sysfs attributes for "
1019 switch(mdi
->array
.level
) {
1021 case LEVEL_MULTIPATH
:
1023 /* safemode delay is irrelevant for these levels */
1027 /* for internal metadata the kernel handles the final clean
1028 * transition, containers can never be dirty
1030 if (!is_subarray(mdi
->text_version
))
1033 /* safemode disabled ? */
1034 if (mdi
->safe_mode_delay
== 0)
1038 int state_fd
= sysfs_open(fd2devnum(fd
), NULL
, "array_state");
1043 /* minimize the safe_mode_delay and prepare to wait up to 5s
1044 * for writes to quiesce
1046 sysfs_set_safemode(mdi
, 1);
1052 /* wait for array_state to be clean */
1054 rv
= read(state_fd
, buf
, sizeof(buf
));
1057 if (sysfs_match_word(buf
, clean_states
) <= 4)
1059 FD_SET(state_fd
, &fds
);
1060 rv
= select(state_fd
+ 1, NULL
, NULL
, &fds
, &tm
);
1061 if (rv
< 0 && errno
!= EINTR
)
1063 lseek(state_fd
, 0, SEEK_SET
);
1067 else if (fping_monitor(sock
) == 0 ||
1068 ping_monitor(mdi
->text_version
) == 0) {
1069 /* we need to ping to close the window between array
1070 * state transitioning to clean and the metadata being
1077 pr_err("Error waiting for %s to be clean\n",
1080 /* restore the original safe_mode_delay */
1081 sysfs_set_safemode(mdi
, mdi
->safe_mode_delay
);
1090 #endif /* MDASSEMBLE */