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);
217 for (st
=statelist
; st
; st
=st
->next
)
218 if (check_array(st
, mdstat
, c
->test
, &info
,
219 increments
, c
->prefer
))
222 /* now check if there are any new devices found in mdstat */
224 new_found
= add_new_arrays(mdstat
, &statelist
, c
->test
,
227 /* If an array has active < raid && spare == 0 && spare_group != NULL
228 * Look for another array with spare > 0 and active == raid and same spare_group
229 * if found, choose a device and hotremove/hotadd
231 if (share
&& anydegraded
)
232 try_spare_migration(statelist
, &info
);
237 mdstat_wait(c
->delay
);
241 for (stp
= &statelist
; (st
= *stp
) != NULL
; ) {
242 if (st
->from_auto
&& st
->err
> 5) {
245 free(st
->spare_group
);
251 for (st2
= statelist
; st2
; st2
= statelist
) {
252 statelist
= st2
->next
;
261 static int make_daemon(char *pidfile
)
264 * -1 in the forked daemon
267 * so a none-negative becomes the exit code.
275 pid_file
=fopen(pidfile
, "w");
277 perror("cannot create pid file");
279 fprintf(pid_file
,"%d\n", pid
);
290 open("/dev/null", O_RDWR
);
297 static int check_one_sharer(int scan
)
304 sprintf(path
, "%s/autorebuild.pid", MDMON_DIR
);
305 fp
= fopen(path
, "r");
307 if (fscanf(fp
, "%d", &pid
) != 1)
309 sprintf(dir
, "/proc/%d", pid
);
310 rv
= stat(dir
, &buf
);
313 pr_err("Only one autorebuild process allowed in scan mode, aborting\n");
317 pr_err("Warning: One autorebuild process already running.\n");
323 if (mkdir(MDMON_DIR
, S_IRWXU
) < 0 &&
325 pr_err("Can't create autorebuild.pid file\n");
327 fp
= fopen(path
, "w");
329 pr_err("Cannot create autorebuild.pidfile\n");
332 fprintf(fp
, "%d\n", pid
);
340 static void alert(char *event
, char *dev
, char *disc
, struct alert_info
*info
)
344 if (!info
->alert_cmd
&& !info
->mailaddr
&& !info
->dosyslog
) {
345 time_t now
= time(0);
347 printf("%1.15s: %s on %s %s\n", ctime(&now
)+4, event
, dev
, disc
?disc
:"unknown device");
349 if (info
->alert_cmd
) {
353 waitpid(pid
, NULL
, 0);
358 execl(info
->alert_cmd
, info
->alert_cmd
,
359 event
, dev
, disc
, NULL
);
363 if (info
->mailaddr
&&
364 (strncmp(event
, "Fail", 4)==0 ||
365 strncmp(event
, "Test", 4)==0 ||
366 strncmp(event
, "Spares", 6)==0 ||
367 strncmp(event
, "Degrade", 7)==0)) {
368 FILE *mp
= popen(Sendmail
, "w");
372 gethostname(hname
, sizeof(hname
));
373 signal(SIGPIPE
, SIG_IGN
);
375 fprintf(mp
, "From: %s\n", info
->mailfrom
);
377 fprintf(mp
, "From: %s monitoring <root>\n", Name
);
378 fprintf(mp
, "To: %s\n", info
->mailaddr
);
379 fprintf(mp
, "Subject: %s event on %s:%s\n\n",
383 "This is an automatically generated mail message from %s\n", Name
);
384 fprintf(mp
, "running on %s\n\n", hname
);
387 "A %s event had been detected on md device %s.\n\n", event
, dev
);
389 if (disc
&& disc
[0] != ' ')
391 "It could be related to component device %s.\n\n", disc
);
392 if (disc
&& disc
[0] == ' ')
393 fprintf(mp
, "Extra information:%s.\n\n", disc
);
395 fprintf(mp
, "Faithfully yours, etc.\n");
397 mdstat
= fopen("/proc/mdstat", "r");
402 "\nP.S. The /proc/mdstat file currently contains the following:\n\n");
403 while ( (n
=fread(buf
, 1, sizeof(buf
), mdstat
)) > 0)
404 n
=fwrite(buf
, 1, n
, mp
);
411 /* log the event to syslog maybe */
412 if (info
->dosyslog
) {
413 /* Log at a different severity depending on the event.
415 * These are the critical events: */
416 if (strncmp(event
, "Fail", 4)==0 ||
417 strncmp(event
, "Degrade", 7)==0 ||
418 strncmp(event
, "DeviceDisappeared", 17)==0)
420 /* Good to know about, but are not failures: */
421 else if (strncmp(event
, "Rebuild", 7)==0 ||
422 strncmp(event
, "MoveSpare", 9)==0 ||
423 strncmp(event
, "Spares", 6) != 0)
424 priority
= LOG_WARNING
;
425 /* Everything else: */
429 if (disc
&& disc
[0] != ' ')
431 "%s event detected on md device %s, component device %s", event
, dev
, disc
);
434 "%s event detected on md device %s: %s",
438 "%s event detected on md device %s",
443 static int check_array(struct state
*st
, struct mdstat_ent
*mdstat
,
444 int test
, struct alert_info
*ainfo
,
445 int increments
, char *prefer
)
447 /* Update the state 'st' to reflect any changes shown in mdstat,
448 * or found by directly examining the array, and return
449 * '1' if the array is degraded, or '0' if it is optimal (or dead).
451 struct { int state
, major
, minor
; } info
[MAX_DISKS
];
452 mdu_array_info_t array
;
453 struct mdstat_ent
*mse
= NULL
, *mse2
;
454 char *dev
= st
->devname
;
462 alert("TestMessage", dev
, NULL
, ainfo
);
464 fd
= open("/sys/block", O_RDONLY
|O_DIRECTORY
);
466 /* Don't open the device unless it is present and
471 fd
= sysfs_open(st
->devnm
, NULL
, "array_state");
473 read(fd
, buf
, 10) < 5 ||
474 strncmp(buf
,"clear",5) == 0 ||
475 strncmp(buf
,"inact",5) == 0) {
478 fd
= sysfs_open(st
->devnm
, NULL
, "level");
479 if (fd
< 0 || read(fd
, buf
, 10) != 0) {
483 alert("DeviceDisappeared", dev
, NULL
, ainfo
);
490 fd
= open(dev
, O_RDONLY
);
493 alert("DeviceDisappeared", dev
, NULL
, ainfo
);
497 fcntl(fd
, F_SETFD
, FD_CLOEXEC
);
498 if (ioctl(fd
, GET_ARRAY_INFO
, &array
)<0) {
500 alert("DeviceDisappeared", dev
, NULL
, ainfo
);
505 /* It's much easier to list what array levels can't
506 * have a device disappear than all of them that can
508 if (array
.level
== 0 || array
.level
== -1) {
509 if (!st
->err
&& !st
->from_config
)
510 alert("DeviceDisappeared", dev
, " Wrong-Level", ainfo
);
515 if (st
->devnm
[0] == 0)
516 strcpy(st
->devnm
, fd2devnm(fd
));
518 for (mse2
= mdstat
; mse2
; mse2
=mse2
->next
)
519 if (strcmp(mse2
->devnm
, st
->devnm
) == 0) {
520 mse2
->devnm
[0] = 0; /* flag it as "used" */
525 /* duplicated array in statelist
526 * or re-created after reading mdstat*/
531 /* this array is in /proc/mdstat */
532 if (array
.utime
== 0)
533 /* external arrays don't update utime, so
534 * just make sure it is always different. */
535 array
.utime
= st
->utime
+ 1;;
538 /* New array appeared where previously had an error */
540 st
->percent
= RESYNC_NONE
;
542 alert("NewArray", st
->devname
, NULL
, ainfo
);
545 if (st
->utime
== array
.utime
&&
546 st
->failed
== array
.failed_disks
&&
547 st
->working
== array
.working_disks
&&
548 st
->spare
== array
.spare_disks
&&
550 mse
->percent
== st
->percent
553 if ((st
->active
< st
->raid
) && st
->spare
== 0)
558 if (st
->utime
== 0 && /* new array */
559 mse
->pattern
&& strchr(mse
->pattern
, '_') /* degraded */
561 alert("DegradedArray", dev
, NULL
, ainfo
);
563 if (st
->utime
== 0 && /* new array */
564 st
->expected_spares
> 0 &&
565 array
.spare_disks
< st
->expected_spares
)
566 alert("SparesMissing", dev
, NULL
, ainfo
);
567 if (st
->percent
< 0 && st
->percent
!= RESYNC_UNKNOWN
&&
569 alert("RebuildStarted", dev
, NULL
, ainfo
);
570 if (st
->percent
>= 0 &&
572 (mse
->percent
/ increments
) > (st
->percent
/ increments
)) {
573 char percentalert
[15]; // "RebuildNN" (10 chars) or "RebuildStarted" (15 chars)
575 if((mse
->percent
/ increments
) == 0)
576 snprintf(percentalert
, sizeof(percentalert
), "RebuildStarted");
578 snprintf(percentalert
, sizeof(percentalert
), "Rebuild%02d", mse
->percent
);
580 alert(percentalert
, dev
, NULL
, ainfo
);
583 if (mse
->percent
== RESYNC_NONE
&&
585 /* Rebuild/sync/whatever just finished.
586 * If there is a number in /mismatch_cnt,
587 * we should report that.
590 sysfs_read(-1, st
->devnm
, GET_MISMATCH
);
591 if (sra
&& sra
->mismatch_cnt
> 0) {
593 snprintf(cnt
, sizeof(cnt
),
594 " mismatches found: %d (on raid level %d)",
595 sra
->mismatch_cnt
, array
.level
);
596 alert("RebuildFinished", dev
, cnt
, ainfo
);
598 alert("RebuildFinished", dev
, NULL
, ainfo
);
602 st
->percent
= mse
->percent
;
604 remaining_disks
= array
.nr_disks
;
605 for (i
=0; i
<MAX_DISKS
&& remaining_disks
> 0;
607 mdu_disk_info_t disc
;
609 if (ioctl(fd
, GET_DISK_INFO
, &disc
) >= 0) {
610 info
[i
].state
= disc
.state
;
611 info
[i
].major
= disc
.major
;
612 info
[i
].minor
= disc
.minor
;
613 if (disc
.major
|| disc
.minor
)
616 info
[i
].major
= info
[i
].minor
= 0;
620 if (mse
->metadata_version
&&
621 strncmp(mse
->metadata_version
, "external:", 9) == 0 &&
622 is_subarray(mse
->metadata_version
+9)) {
624 strcpy(st
->parent_devnm
,
625 mse
->metadata_version
+10);
626 sl
= strchr(st
->parent_devnm
, '/');
630 st
->parent_devnm
[0] = 0;
631 if (st
->metadata
== NULL
&&
632 st
->parent_devnm
[0] == 0)
633 st
->metadata
= super_by_fd(fd
, NULL
);
637 for (i
=0; i
<MAX_DISKS
; i
++) {
638 mdu_disk_info_t disc
= {0,0,0,0,0};
644 (info
[i
].major
|| info
[i
].minor
)) {
645 newstate
= info
[i
].state
;
646 dv
= map_dev_preferred(
647 info
[i
].major
, info
[i
].minor
, 1,
649 disc
.state
= newstate
;
650 disc
.major
= info
[i
].major
;
651 disc
.minor
= info
[i
].minor
;
653 newstate
= (1 << MD_DISK_REMOVED
);
655 if (dv
== NULL
&& st
->devid
[i
])
656 dv
= map_dev_preferred(
658 minor(st
->devid
[i
]), 1, prefer
);
659 change
= newstate
^ st
->devstate
[i
];
660 if (st
->utime
&& change
&& !st
->err
&& !new_array
) {
661 if ((st
->devstate
[i
]&change
)&(1<<MD_DISK_SYNC
))
662 alert("Fail", dev
, dv
, ainfo
);
663 else if ((newstate
& (1<<MD_DISK_FAULTY
)) &&
664 (disc
.major
|| disc
.minor
) &&
665 st
->devid
[i
] == makedev(disc
.major
, disc
.minor
))
666 alert("FailSpare", dev
, dv
, ainfo
);
667 else if ((newstate
&change
)&(1<<MD_DISK_SYNC
))
668 alert("SpareActive", dev
, dv
, ainfo
);
670 st
->devstate
[i
] = newstate
;
671 st
->devid
[i
] = makedev(disc
.major
, disc
.minor
);
673 st
->active
= array
.active_disks
;
674 st
->working
= array
.working_disks
;
675 st
->spare
= array
.spare_disks
;
676 st
->failed
= array
.failed_disks
;
677 st
->utime
= array
.utime
;
678 st
->raid
= array
.raid_disks
;
680 if ((st
->active
< st
->raid
) && st
->spare
== 0)
685 static int add_new_arrays(struct mdstat_ent
*mdstat
, struct state
**statelist
,
686 int test
, struct alert_info
*info
)
688 struct mdstat_ent
*mse
;
692 for (mse
=mdstat
; mse
; mse
=mse
->next
)
694 (!mse
->level
|| /* retrieve containers */
695 (strcmp(mse
->level
, "raid0") != 0 &&
696 strcmp(mse
->level
, "linear") != 0))
698 struct state
*st
= xcalloc(1, sizeof *st
);
699 mdu_array_info_t array
;
702 name
= get_md_name(mse
->devnm
);
708 st
->devname
= xstrdup(name
);
709 if ((fd
= open(st
->devname
, O_RDONLY
)) < 0 ||
710 ioctl(fd
, GET_ARRAY_INFO
, &array
)< 0) {
712 if (fd
>=0) close(fd
);
713 put_md_name(st
->devname
);
716 st
->metadata
->ss
->free_super(st
->metadata
);
723 st
->next
= *statelist
;
726 strcpy(st
->devnm
, mse
->devnm
);
727 st
->percent
= RESYNC_UNKNOWN
;
728 st
->expected_spares
= -1;
729 if (mse
->metadata_version
&&
730 strncmp(mse
->metadata_version
, "external:", 9) == 0 &&
731 is_subarray(mse
->metadata_version
+9)) {
733 strcpy(st
->parent_devnm
,
734 mse
->metadata_version
+10);
735 sl
= strchr(st
->parent_devnm
, '/');
738 st
->parent_devnm
[0] = 0;
741 alert("TestMessage", st
->devname
, NULL
, info
);
747 static int get_min_spare_size_required(struct state
*st
, unsigned long long *sizep
)
752 !st
->metadata
->ss
->min_acceptable_spare_size
) {
757 fd
= open(st
->devname
, O_RDONLY
);
760 if (st
->metadata
->ss
->external
)
761 st
->metadata
->ss
->load_container(st
->metadata
, fd
, st
->devname
);
763 st
->metadata
->ss
->load_super(st
->metadata
, fd
, st
->devname
);
765 if (!st
->metadata
->sb
)
767 *sizep
= st
->metadata
->ss
->min_acceptable_spare_size(st
->metadata
);
768 st
->metadata
->ss
->free_super(st
->metadata
);
773 static int check_donor(struct state
*from
, struct state
*to
)
780 /* Cannot move from a member */
784 for (sub
= from
->subarray
; sub
; sub
= sub
->subarray
)
785 /* If source array has degraded subarrays, don't
788 if (sub
->active
< sub
->raid
)
790 if (from
->metadata
->ss
->external
== 0)
791 if (from
->active
< from
->raid
)
793 if (from
->spare
<= 0)
798 static dev_t
choose_spare(struct state
*from
, struct state
*to
,
799 struct domainlist
*domlist
, unsigned long long min_size
)
804 for (d
= from
->raid
; !dev
&& d
< MAX_DISKS
; d
++) {
805 if (from
->devid
[d
] > 0 &&
806 from
->devstate
[d
] == 0) {
807 struct dev_policy
*pol
;
808 unsigned long long dev_size
;
810 if (to
->metadata
->ss
->external
&&
811 test_partition_from_id(from
->devid
[d
]))
815 dev_size_from_id(from
->devid
[d
], &dev_size
) &&
819 pol
= devid_policy(from
->devid
[d
]);
820 if (from
->spare_group
)
821 pol_add(&pol
, pol_domain
,
822 from
->spare_group
, NULL
);
823 if (domain_test(domlist
, pol
, to
->metadata
->ss
->name
) == 1)
824 dev
= from
->devid
[d
];
825 dev_policy_free(pol
);
831 static dev_t
container_choose_spare(struct state
*from
, struct state
*to
,
832 struct domainlist
*domlist
,
833 unsigned long long min_size
, int active
)
835 /* This is similar to choose_spare, but we cannot trust devstate,
836 * so we need to read the metadata instead
839 struct supertype
*st
= from
->metadata
;
840 int fd
= open(from
->devname
, O_RDONLY
);
846 if (!st
->ss
->getinfo_super_disks
) {
851 err
= st
->ss
->load_container(st
, fd
, NULL
);
857 /* We must check if number of active disks has not increased
858 * since ioctl in main loop. mdmon may have added spare
859 * to subarray. If so we do not need to look for more spares
860 * so return non zero value */
863 list
= st
->ss
->getinfo_super_disks(st
);
865 st
->ss
->free_super(st
);
870 if (dp
->disk
.state
& (1<<MD_DISK_SYNC
) &&
871 !(dp
->disk
.state
& (1<<MD_DISK_FAULTY
)))
876 if (active
< active_cnt
) {
877 /* Spare just activated.*/
878 st
->ss
->free_super(st
);
883 /* We only need one spare so full list not needed */
884 list
= container_choose_spares(st
, min_size
, domlist
, from
->spare_group
,
885 to
->metadata
->ss
->name
, 1);
887 struct mdinfo
*disks
= list
->devs
;
889 dev
= makedev(disks
->disk
.major
, disks
->disk
.minor
);
892 st
->ss
->free_super(st
);
896 static void try_spare_migration(struct state
*statelist
, struct alert_info
*info
)
901 link_containers_with_subarrays(statelist
);
902 for (st
= statelist
; st
; st
= st
->next
)
903 if (st
->active
< st
->raid
&&
904 st
->spare
== 0 && !st
->err
) {
905 struct domainlist
*domlist
= NULL
;
907 struct state
*to
= st
;
908 unsigned long long min_size
;
910 if (to
->parent_devnm
[0] && !to
->parent
)
911 /* subarray monitored without parent container
912 * we can't move spares here */
916 /* member of a container */
919 if (get_min_spare_size_required(to
, &min_size
))
921 if (to
->metadata
->ss
->external
) {
922 /* We must make sure there is
923 * no suitable spare in container already.
924 * If there is we don't add more */
925 dev_t devid
= container_choose_spare(
926 to
, to
, NULL
, min_size
, st
->active
);
930 for (d
= 0; d
< MAX_DISKS
; d
++)
932 domainlist_add_dev(&domlist
,
934 to
->metadata
->ss
->name
);
936 domain_add(&domlist
, to
->spare_group
);
938 * No spare migration if the destination
939 * has no domain. Skip this array.
943 for (from
=statelist
; from
; from
=from
->next
) {
945 if (!check_donor(from
, to
))
947 if (from
->metadata
->ss
->external
)
948 devid
= container_choose_spare(
949 from
, to
, domlist
, min_size
, 0);
951 devid
= choose_spare(from
, to
, domlist
,
954 && move_spare(from
->devname
, to
->devname
, devid
)) {
955 alert("MoveSpare", to
->devname
, from
->devname
, info
);
959 domain_free(domlist
);
963 /* search the statelist to connect external
964 * metadata subarrays with their containers
965 * We always completely rebuild the tree from scratch as
966 * that is safest considering the possibility of entries
967 * disappearing or changing.
969 static void link_containers_with_subarrays(struct state
*list
)
973 for (st
= list
; st
; st
= st
->next
) {
977 for (st
= list
; st
; st
= st
->next
)
978 if (st
->parent_devnm
[0])
979 for (cont
= list
; cont
; cont
= cont
->next
)
981 cont
->parent_devnm
[0] == 0 &&
982 strcmp(cont
->devnm
, st
->parent_devnm
) == 0) {
984 st
->subarray
= cont
->subarray
;
990 /* Not really Monitor but ... */
996 int frozen_remaining
= 3;
998 if (stat(dev
, &stb
) != 0) {
999 pr_err("Cannot find %s: %s\n", dev
,
1003 strcpy(devnm
, stat2devnm(&stb
));
1006 struct mdstat_ent
*ms
= mdstat_read(1, 0);
1007 struct mdstat_ent
*e
;
1009 for (e
=ms
; e
; e
=e
->next
)
1010 if (strcmp(e
->devnm
, devnm
) == 0)
1013 if (e
&& e
->percent
== RESYNC_NONE
) {
1014 /* We could be in the brief pause before something
1015 * starts. /proc/mdstat doesn't show that, but
1020 sysfs_init(&mdi
, -1, devnm
);
1021 if (sysfs_get_str(&mdi
, NULL
, "sync_action",
1023 strcmp(buf
,"idle\n") != 0) {
1024 e
->percent
= RESYNC_UNKNOWN
;
1025 if (strcmp(buf
, "frozen\n") == 0) {
1026 if (frozen_remaining
== 0)
1027 e
->percent
= RESYNC_NONE
;
1029 frozen_remaining
-= 1;
1033 if (!e
|| e
->percent
== RESYNC_NONE
) {
1034 if (e
&& e
->metadata_version
&&
1035 strncmp(e
->metadata_version
, "external:", 9) == 0) {
1036 if (is_subarray(&e
->metadata_version
[9]))
1037 ping_monitor(&e
->metadata_version
[9]);
1039 ping_monitor(devnm
);
1052 static char *clean_states
[] = {
1053 "clear", "inactive", "readonly", "read-auto", "clean", NULL
};
1055 int WaitClean(char *dev
, int sock
, int verbose
)
1062 fd
= open(dev
, O_RDONLY
);
1065 pr_err("Couldn't open %s: %s\n", dev
, strerror(errno
));
1069 strcpy(devnm
, fd2devnm(fd
));
1070 mdi
= sysfs_read(fd
, devnm
, GET_VERSION
|GET_LEVEL
|GET_SAFEMODE
);
1073 pr_err("Failed to read sysfs attributes for %s\n", dev
);
1078 switch(mdi
->array
.level
) {
1080 case LEVEL_MULTIPATH
:
1082 /* safemode delay is irrelevant for these levels */
1086 /* for internal metadata the kernel handles the final clean
1087 * transition, containers can never be dirty
1089 if (!is_subarray(mdi
->text_version
))
1092 /* safemode disabled ? */
1093 if (mdi
->safe_mode_delay
== 0)
1097 int state_fd
= sysfs_open(fd2devnm(fd
), NULL
, "array_state");
1101 /* minimize the safe_mode_delay and prepare to wait up to 5s
1102 * for writes to quiesce
1104 sysfs_set_safemode(mdi
, 1);
1106 /* wait for array_state to be clean */
1108 rv
= read(state_fd
, buf
, sizeof(buf
));
1111 if (sysfs_match_word(buf
, clean_states
) <= 4)
1113 rv
= sysfs_wait(state_fd
, &delay
);
1114 if (rv
< 0 && errno
!= EINTR
)
1116 lseek(state_fd
, 0, SEEK_SET
);
1120 else if (fping_monitor(sock
) == 0 ||
1121 ping_monitor(mdi
->text_version
) == 0) {
1122 /* we need to ping to close the window between array
1123 * state transitioning to clean and the metadata being
1130 pr_err("Error waiting for %s to be clean\n",
1133 /* restore the original safe_mode_delay */
1134 sysfs_set_safemode(mdi
, mdi
->safe_mode_delay
);
1143 #endif /* MDASSEMBLE */