]>
git.ipfire.org Git - thirdparty/mdadm.git/blob - Monitor.c
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
;
129 struct alert_info info
;
130 struct mddev_ident
*mdlist
;
133 mailaddr
= conf_get_mailaddr();
134 if (mailaddr
&& ! c
->scan
)
135 pr_err("Monitor using email address \"%s\" from config file\n",
138 mailfrom
= conf_get_mailfrom();
141 alert_cmd
= conf_get_program();
142 if (alert_cmd
&& !c
->scan
)
143 pr_err("Monitor using program \"%s\" from config file\n",
146 if (c
->scan
&& !mailaddr
&& !alert_cmd
&& !dosyslog
) {
147 pr_err("No mail address or alert command - not monitoring.\n");
150 info
.alert_cmd
= alert_cmd
;
151 info
.mailaddr
= mailaddr
;
152 info
.mailfrom
= mailfrom
;
153 info
.dosyslog
= dosyslog
;
156 int rv
= make_daemon(pidfile
);
162 if (check_one_sharer(c
->scan
))
165 if (devlist
== NULL
) {
166 mdlist
= conf_get_ident(NULL
);
167 for (; mdlist
; mdlist
= mdlist
->next
) {
170 if (mdlist
->devname
== NULL
)
172 if (strcasecmp(mdlist
->devname
, "<ignore>") == 0)
174 st
= xcalloc(1, sizeof *st
);
175 if (mdlist
->devname
[0] == '/')
176 st
->devname
= xstrdup(mdlist
->devname
);
178 st
->devname
= xmalloc(8+strlen(mdlist
->devname
)+1);
179 strcpy(strcpy(st
->devname
, "/dev/md/"),
182 st
->next
= statelist
;
184 st
->percent
= RESYNC_UNKNOWN
;
186 st
->expected_spares
= mdlist
->spare_disks
;
187 if (mdlist
->spare_group
)
188 st
->spare_group
= xstrdup(mdlist
->spare_group
);
192 struct mddev_dev
*dv
;
194 for (dv
= devlist
; dv
; dv
= dv
->next
) {
195 struct state
*st
= xcalloc(1, sizeof *st
);
196 mdlist
= conf_get_ident(dv
->devname
);
197 st
->devname
= xstrdup(dv
->devname
);
198 st
->next
= statelist
;
200 st
->percent
= RESYNC_UNKNOWN
;
201 st
->expected_spares
= -1;
203 st
->expected_spares
= mdlist
->spare_disks
;
204 if (mdlist
->spare_group
)
205 st
->spare_group
= xstrdup(mdlist
->spare_group
);
213 struct state
*st
, **stp
;
215 int anyredundant
= 0;
219 mdstat
= mdstat_read(oneshot
? 0 : 1, 0);
221 for (st
= statelist
; st
; st
= st
->next
) {
222 if (check_array(st
, mdstat
, c
->test
, &info
,
223 increments
, c
->prefer
))
225 /* for external arrays, metadata is filled for
228 if (st
->metadata
&& st
->metadata
->ss
->external
)
230 if (st
->err
== 0 && !anyredundant
)
234 /* now check if there are any new devices found in mdstat */
236 new_found
= add_new_arrays(mdstat
, &statelist
, c
->test
,
239 /* If an array has active < raid && spare == 0 && spare_group != NULL
240 * Look for another array with spare > 0 and active == raid and same spare_group
241 * if found, choose a device and hotremove/hotadd
243 if (share
&& anydegraded
)
244 try_spare_migration(statelist
, &info
);
248 else if (!anyredundant
) {
252 mdstat_wait(c
->delay
);
258 for (stp
= &statelist
; (st
= *stp
) != NULL
; ) {
259 if (st
->from_auto
&& st
->err
> 5) {
262 free(st
->spare_group
);
268 for (st2
= statelist
; st2
; st2
= statelist
) {
269 statelist
= st2
->next
;
278 static int make_daemon(char *pidfile
)
281 * -1 in the forked daemon
284 * so a none-negative becomes the exit code.
292 pid_file
=fopen(pidfile
, "w");
294 perror("cannot create pid file");
296 fprintf(pid_file
,"%d\n", pid
);
307 open("/dev/null", O_RDWR
);
314 static int check_one_sharer(int scan
)
323 sprintf(path
, "%s/autorebuild.pid", MDMON_DIR
);
324 fp
= fopen(path
, "r");
326 if (fscanf(fp
, "%d", &pid
) != 1)
328 snprintf(comm_path
, sizeof(comm_path
),
329 "/proc/%d/comm", pid
);
330 comm_fp
= fopen(comm_path
, "r");
332 if (fscanf(comm_fp
, "%s", comm
) &&
333 strncmp(basename(comm
), Name
, strlen(Name
)) == 0) {
335 pr_err("Only one autorebuild process allowed in scan mode, aborting\n");
340 pr_err("Warning: One autorebuild process already running.\n");
348 if (mkdir(MDMON_DIR
, S_IRWXU
) < 0 && errno
!= EEXIST
) {
349 pr_err("Can't create autorebuild.pid file\n");
351 fp
= fopen(path
, "w");
353 pr_err("Cannot create autorebuild.pidfile\n");
356 fprintf(fp
, "%d\n", pid
);
364 static void alert(char *event
, char *dev
, char *disc
, struct alert_info
*info
)
368 if (!info
->alert_cmd
&& !info
->mailaddr
&& !info
->dosyslog
) {
369 time_t now
= time(0);
371 printf("%1.15s: %s on %s %s\n", ctime(&now
) + 4,
372 event
, dev
, disc
?disc
:"unknown device");
374 if (info
->alert_cmd
) {
378 waitpid(pid
, NULL
, 0);
383 execl(info
->alert_cmd
, info
->alert_cmd
,
384 event
, dev
, disc
, NULL
);
388 if (info
->mailaddr
&& (strncmp(event
, "Fail", 4) == 0 ||
389 strncmp(event
, "Test", 4) == 0 ||
390 strncmp(event
, "Spares", 6) == 0 ||
391 strncmp(event
, "Degrade", 7) == 0)) {
392 FILE *mp
= popen(Sendmail
, "w");
396 gethostname(hname
, sizeof(hname
));
397 signal(SIGPIPE
, SIG_IGN
);
399 fprintf(mp
, "From: %s\n", info
->mailfrom
);
401 fprintf(mp
, "From: %s monitoring <root>\n",
403 fprintf(mp
, "To: %s\n", info
->mailaddr
);
404 fprintf(mp
, "Subject: %s event on %s:%s\n\n",
408 "This is an automatically generated mail message from %s\n", Name
);
409 fprintf(mp
, "running on %s\n\n", hname
);
412 "A %s event had been detected on md device %s.\n\n", event
, dev
);
414 if (disc
&& disc
[0] != ' ')
416 "It could be related to component device %s.\n\n", disc
);
417 if (disc
&& disc
[0] == ' ')
418 fprintf(mp
, "Extra information:%s.\n\n", disc
);
420 fprintf(mp
, "Faithfully yours, etc.\n");
422 mdstat
= fopen("/proc/mdstat", "r");
427 "\nP.S. The /proc/mdstat file currently contains the following:\n\n");
428 while ((n
= fread(buf
, 1, sizeof(buf
),
430 n
= fwrite(buf
, 1, n
, mp
);
437 /* log the event to syslog maybe */
438 if (info
->dosyslog
) {
439 /* Log at a different severity depending on the event.
441 * These are the critical events: */
442 if (strncmp(event
, "Fail", 4) == 0 ||
443 strncmp(event
, "Degrade", 7) == 0 ||
444 strncmp(event
, "DeviceDisappeared", 17) == 0)
446 /* Good to know about, but are not failures: */
447 else if (strncmp(event
, "Rebuild", 7) == 0 ||
448 strncmp(event
, "MoveSpare", 9) == 0 ||
449 strncmp(event
, "Spares", 6) != 0)
450 priority
= LOG_WARNING
;
451 /* Everything else: */
455 if (disc
&& disc
[0] != ' ')
457 "%s event detected on md device %s, component device %s", event
, dev
, disc
);
460 "%s event detected on md device %s: %s",
464 "%s event detected on md device %s",
469 static int check_array(struct state
*st
, struct mdstat_ent
*mdstat
,
470 int test
, struct alert_info
*ainfo
,
471 int increments
, char *prefer
)
473 /* Update the state 'st' to reflect any changes shown in mdstat,
474 * or found by directly examining the array, and return
475 * '1' if the array is degraded, or '0' if it is optimal (or dead).
477 struct { int state
, major
, minor
; } info
[MAX_DISKS
];
478 struct mdinfo
*sra
= NULL
;
479 mdu_array_info_t array
;
480 struct mdstat_ent
*mse
= NULL
, *mse2
;
481 char *dev
= st
->devname
;
488 int is_container
= 0;
489 unsigned long redundancy_only_flags
= 0;
492 alert("TestMessage", dev
, NULL
, ainfo
);
496 fd
= open(dev
, O_RDONLY
);
500 if (st
->devnm
[0] == 0)
501 strcpy(st
->devnm
, fd2devnm(fd
));
503 for (mse2
= mdstat
; mse2
; mse2
= mse2
->next
)
504 if (strcmp(mse2
->devnm
, st
->devnm
) == 0) {
505 mse2
->devnm
[0] = 0; /* flag it as "used" */
510 /* duplicated array in statelist
511 * or re-created after reading mdstat
517 if (mse
->level
== NULL
)
520 if (!is_container
&& !md_array_active(fd
))
523 fcntl(fd
, F_SETFD
, FD_CLOEXEC
);
524 if (md_get_array_info(fd
, &array
) < 0)
527 if (!is_container
&& map_name(pers
, mse
->level
) > 0)
528 redundancy_only_flags
|= GET_MISMATCH
;
530 sra
= sysfs_read(-1, st
->devnm
, GET_LEVEL
| GET_DISKS
| GET_DEVS
|
531 GET_STATE
| redundancy_only_flags
);
536 /* It's much easier to list what array levels can't
537 * have a device disappear than all of them that can
539 if (sra
->array
.level
== 0 || sra
->array
.level
== -1) {
540 if (!st
->err
&& !st
->from_config
)
541 alert("DeviceDisappeared", dev
, " Wrong-Level", ainfo
);
546 /* this array is in /proc/mdstat */
547 if (array
.utime
== 0)
548 /* external arrays don't update utime, so
549 * just make sure it is always different. */
550 array
.utime
= st
->utime
+ 1;;
553 /* New array appeared where previously had an error */
555 st
->percent
= RESYNC_NONE
;
558 alert("NewArray", st
->devname
, NULL
, ainfo
);
561 if (st
->utime
== array
.utime
&& st
->failed
== sra
->array
.failed_disks
&&
562 st
->working
== sra
->array
.working_disks
&&
563 st
->spare
== sra
->array
.spare_disks
&&
564 (mse
== NULL
|| (mse
->percent
== st
->percent
))) {
565 if ((st
->active
< st
->raid
) && st
->spare
== 0)
569 if (st
->utime
== 0 && /* new array */
570 mse
->pattern
&& strchr(mse
->pattern
, '_') /* degraded */)
571 alert("DegradedArray", dev
, NULL
, ainfo
);
573 if (st
->utime
== 0 && /* new array */ st
->expected_spares
> 0 &&
574 sra
->array
.spare_disks
< st
->expected_spares
)
575 alert("SparesMissing", dev
, NULL
, ainfo
);
576 if (st
->percent
< 0 && st
->percent
!= RESYNC_UNKNOWN
&&
578 alert("RebuildStarted", dev
, NULL
, ainfo
);
579 if (st
->percent
>= 0 && mse
->percent
>= 0 &&
580 (mse
->percent
/ increments
) > (st
->percent
/ increments
)) {
581 char percentalert
[18];
583 * "RebuildNN" (10 chars) or "RebuildStarted" (15 chars)
586 if((mse
->percent
/ increments
) == 0)
587 snprintf(percentalert
, sizeof(percentalert
),
590 snprintf(percentalert
, sizeof(percentalert
),
591 "Rebuild%02d", mse
->percent
);
593 alert(percentalert
, dev
, NULL
, ainfo
);
596 if (mse
->percent
== RESYNC_NONE
&& st
->percent
>= 0) {
597 /* Rebuild/sync/whatever just finished.
598 * If there is a number in /mismatch_cnt,
599 * we should report that.
601 if (sra
&& sra
->mismatch_cnt
> 0) {
603 snprintf(cnt
, sizeof(cnt
),
604 " mismatches found: %d (on raid level %d)",
605 sra
->mismatch_cnt
, sra
->array
.level
);
606 alert("RebuildFinished", dev
, cnt
, ainfo
);
608 alert("RebuildFinished", dev
, NULL
, ainfo
);
610 st
->percent
= mse
->percent
;
612 remaining_disks
= sra
->array
.nr_disks
;
613 for (i
= 0; i
< MAX_DISKS
&& remaining_disks
> 0; i
++) {
614 mdu_disk_info_t disc
;
616 if (md_get_disk_info(fd
, &disc
) >= 0) {
617 info
[i
].state
= disc
.state
;
618 info
[i
].major
= disc
.major
;
619 info
[i
].minor
= disc
.minor
;
620 if (disc
.major
|| disc
.minor
)
623 info
[i
].major
= info
[i
].minor
= 0;
627 if (mse
->metadata_version
&&
628 strncmp(mse
->metadata_version
, "external:", 9) == 0 &&
629 is_subarray(mse
->metadata_version
+9)) {
631 strcpy(st
->parent_devnm
, mse
->metadata_version
+ 10);
632 sl
= strchr(st
->parent_devnm
, '/');
636 st
->parent_devnm
[0] = 0;
637 if (st
->metadata
== NULL
&& st
->parent_devnm
[0] == 0)
638 st
->metadata
= super_by_fd(fd
, NULL
);
640 for (i
= 0; i
< MAX_DISKS
; i
++) {
641 mdu_disk_info_t disc
= {0, 0, 0, 0, 0};
646 if (i
< last_disk
&& (info
[i
].major
|| info
[i
].minor
)) {
647 newstate
= info
[i
].state
;
648 dv
= map_dev_preferred(info
[i
].major
, info
[i
].minor
, 1,
650 disc
.state
= newstate
;
651 disc
.major
= info
[i
].major
;
652 disc
.minor
= info
[i
].minor
;
654 newstate
= (1 << MD_DISK_REMOVED
);
656 if (dv
== NULL
&& st
->devid
[i
])
657 dv
= map_dev_preferred(major(st
->devid
[i
]),
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
,
667 alert("FailSpare", dev
, dv
, ainfo
);
668 else if ((newstate
&change
) & (1 << MD_DISK_SYNC
))
669 alert("SpareActive", dev
, dv
, ainfo
);
671 st
->devstate
[i
] = newstate
;
672 st
->devid
[i
] = makedev(disc
.major
, disc
.minor
);
674 st
->active
= sra
->array
.active_disks
;
675 st
->working
= sra
->array
.working_disks
;
676 st
->spare
= sra
->array
.spare_disks
;
677 st
->failed
= sra
->array
.failed_disks
;
678 st
->utime
= array
.utime
;
679 st
->raid
= sra
->array
.raid_disks
;
681 if ((st
->active
< st
->raid
) && st
->spare
== 0)
692 if (!st
->err
&& !is_container
)
693 alert("DeviceDisappeared", dev
, NULL
, ainfo
);
698 static int add_new_arrays(struct mdstat_ent
*mdstat
, struct state
**statelist
,
699 int test
, struct alert_info
*info
)
701 struct mdstat_ent
*mse
;
705 for (mse
= mdstat
; mse
; mse
= mse
->next
)
706 if (mse
->devnm
[0] && (!mse
->level
|| /* retrieve containers */
707 (strcmp(mse
->level
, "raid0") != 0 &&
708 strcmp(mse
->level
, "linear") != 0))) {
709 struct state
*st
= xcalloc(1, sizeof *st
);
710 mdu_array_info_t array
;
713 name
= get_md_name(mse
->devnm
);
719 st
->devname
= xstrdup(name
);
720 if ((fd
= open(st
->devname
, O_RDONLY
)) < 0 ||
721 md_get_array_info(fd
, &array
) < 0) {
725 put_md_name(st
->devname
);
728 st
->metadata
->ss
->free_super(st
->metadata
);
735 st
->next
= *statelist
;
738 strcpy(st
->devnm
, mse
->devnm
);
739 st
->percent
= RESYNC_UNKNOWN
;
740 st
->expected_spares
= -1;
741 if (mse
->metadata_version
&&
742 strncmp(mse
->metadata_version
,
743 "external:", 9) == 0 &&
744 is_subarray(mse
->metadata_version
+9)) {
746 strcpy(st
->parent_devnm
,
747 mse
->metadata_version
+10);
748 sl
= strchr(st
->parent_devnm
, '/');
751 st
->parent_devnm
[0] = 0;
754 alert("TestMessage", st
->devname
, NULL
, info
);
760 static int get_required_spare_criteria(struct state
*st
,
761 struct spare_criteria
*sc
)
765 if (!st
->metadata
|| !st
->metadata
->ss
->get_spare_criteria
) {
771 fd
= open(st
->devname
, O_RDONLY
);
774 if (st
->metadata
->ss
->external
)
775 st
->metadata
->ss
->load_container(st
->metadata
, fd
, st
->devname
);
777 st
->metadata
->ss
->load_super(st
->metadata
, fd
, st
->devname
);
779 if (!st
->metadata
->sb
)
782 st
->metadata
->ss
->get_spare_criteria(st
->metadata
, sc
);
783 st
->metadata
->ss
->free_super(st
->metadata
);
788 static int check_donor(struct state
*from
, struct state
*to
)
795 /* Cannot move from a member */
799 for (sub
= from
->subarray
; sub
; sub
= sub
->subarray
)
800 /* If source array has degraded subarrays, don't
803 if (sub
->active
< sub
->raid
)
805 if (from
->metadata
->ss
->external
== 0)
806 if (from
->active
< from
->raid
)
808 if (from
->spare
<= 0)
813 static dev_t
choose_spare(struct state
*from
, struct state
*to
,
814 struct domainlist
*domlist
, struct spare_criteria
*sc
)
819 for (d
= from
->raid
; !dev
&& d
< MAX_DISKS
; d
++) {
820 if (from
->devid
[d
] > 0 && from
->devstate
[d
] == 0) {
821 struct dev_policy
*pol
;
822 unsigned long long dev_size
;
823 unsigned int dev_sector_size
;
825 if (to
->metadata
->ss
->external
&&
826 test_partition_from_id(from
->devid
[d
]))
830 dev_size_from_id(from
->devid
[d
], &dev_size
) &&
831 dev_size
< sc
->min_size
)
834 if (sc
->sector_size
&&
835 dev_sector_size_from_id(from
->devid
[d
],
837 sc
->sector_size
!= dev_sector_size
)
840 pol
= devid_policy(from
->devid
[d
]);
841 if (from
->spare_group
)
842 pol_add(&pol
, pol_domain
,
843 from
->spare_group
, NULL
);
844 if (domain_test(domlist
, pol
,
845 to
->metadata
->ss
->name
) == 1)
846 dev
= from
->devid
[d
];
847 dev_policy_free(pol
);
853 static dev_t
container_choose_spare(struct state
*from
, struct state
*to
,
854 struct domainlist
*domlist
,
855 struct spare_criteria
*sc
, int active
)
857 /* This is similar to choose_spare, but we cannot trust devstate,
858 * so we need to read the metadata instead
861 struct supertype
*st
= from
->metadata
;
862 int fd
= open(from
->devname
, O_RDONLY
);
868 if (!st
->ss
->getinfo_super_disks
) {
873 err
= st
->ss
->load_container(st
, fd
, NULL
);
879 /* We must check if number of active disks has not increased
880 * since ioctl in main loop. mdmon may have added spare
881 * to subarray. If so we do not need to look for more spares
882 * so return non zero value */
885 list
= st
->ss
->getinfo_super_disks(st
);
887 st
->ss
->free_super(st
);
892 if (dp
->disk
.state
& (1 << MD_DISK_SYNC
) &&
893 !(dp
->disk
.state
& (1 << MD_DISK_FAULTY
)))
898 if (active
< active_cnt
) {
899 /* Spare just activated.*/
900 st
->ss
->free_super(st
);
905 /* We only need one spare so full list not needed */
906 list
= container_choose_spares(st
, sc
, domlist
, from
->spare_group
,
907 to
->metadata
->ss
->name
, 1);
909 struct mdinfo
*disks
= list
->devs
;
911 dev
= makedev(disks
->disk
.major
, disks
->disk
.minor
);
914 st
->ss
->free_super(st
);
918 static void try_spare_migration(struct state
*statelist
, struct alert_info
*info
)
922 struct spare_criteria sc
;
924 link_containers_with_subarrays(statelist
);
925 for (st
= statelist
; st
; st
= st
->next
)
926 if (st
->active
< st
->raid
&& st
->spare
== 0 && !st
->err
) {
927 struct domainlist
*domlist
= NULL
;
929 struct state
*to
= st
;
931 if (to
->parent_devnm
[0] && !to
->parent
)
932 /* subarray monitored without parent container
933 * we can't move spares here */
937 /* member of a container */
940 if (get_required_spare_criteria(to
, &sc
))
942 if (to
->metadata
->ss
->external
) {
943 /* We must make sure there is
944 * no suitable spare in container already.
945 * If there is we don't add more */
946 dev_t devid
= container_choose_spare(
947 to
, to
, NULL
, &sc
, st
->active
);
951 for (d
= 0; d
< MAX_DISKS
; d
++)
953 domainlist_add_dev(&domlist
,
955 to
->metadata
->ss
->name
);
957 domain_add(&domlist
, to
->spare_group
);
959 * No spare migration if the destination
960 * has no domain. Skip this array.
964 for (from
=statelist
; from
; from
=from
->next
) {
966 if (!check_donor(from
, to
))
968 if (from
->metadata
->ss
->external
)
969 devid
= container_choose_spare(
970 from
, to
, domlist
, &sc
, 0);
972 devid
= choose_spare(from
, to
, domlist
,
975 move_spare(from
->devname
, to
->devname
,
977 alert("MoveSpare", to
->devname
,
978 from
->devname
, info
);
982 domain_free(domlist
);
986 /* search the statelist to connect external
987 * metadata subarrays with their containers
988 * We always completely rebuild the tree from scratch as
989 * that is safest considering the possibility of entries
990 * disappearing or changing.
992 static void link_containers_with_subarrays(struct state
*list
)
996 for (st
= list
; st
; st
= st
->next
) {
1000 for (st
= list
; st
; st
= st
->next
)
1001 if (st
->parent_devnm
[0])
1002 for (cont
= list
; cont
; cont
= cont
->next
)
1003 if (!cont
->err
&& cont
->parent_devnm
[0] == 0 &&
1004 strcmp(cont
->devnm
, st
->parent_devnm
) == 0) {
1006 st
->subarray
= cont
->subarray
;
1007 cont
->subarray
= st
;
1012 /* Not really Monitor but ... */
1019 int frozen_remaining
= 3;
1021 if (!stat_is_blkdev(dev
, &rdev
))
1024 tmp
= devid2devnm(rdev
);
1026 pr_err("Cannot get md device name.\n");
1033 struct mdstat_ent
*ms
= mdstat_read(1, 0);
1034 struct mdstat_ent
*e
;
1036 for (e
= ms
; e
; e
= e
->next
)
1037 if (strcmp(e
->devnm
, devnm
) == 0)
1040 if (e
&& e
->percent
== RESYNC_NONE
) {
1041 /* We could be in the brief pause before something
1042 * starts. /proc/mdstat doesn't show that, but
1048 if (sysfs_init(&mdi
, -1, devnm
))
1050 if (sysfs_get_str(&mdi
, NULL
, "sync_action",
1052 strcmp(buf
,"idle\n") != 0) {
1053 e
->percent
= RESYNC_UNKNOWN
;
1054 if (strcmp(buf
, "frozen\n") == 0) {
1055 if (frozen_remaining
== 0)
1056 e
->percent
= RESYNC_NONE
;
1058 frozen_remaining
-= 1;
1062 if (!e
|| e
->percent
== RESYNC_NONE
) {
1063 if (e
&& e
->metadata_version
&&
1064 strncmp(e
->metadata_version
, "external:", 9) == 0) {
1065 if (is_subarray(&e
->metadata_version
[9]))
1066 ping_monitor(&e
->metadata_version
[9]);
1068 ping_monitor(devnm
);
1079 /* The state "broken" is used only for RAID0/LINEAR - it's the same as
1080 * "clean", but used in case the array has one or more members missing.
1082 static char *clean_states
[] = {
1083 "clear", "inactive", "readonly", "read-auto", "clean", "broken", NULL
};
1085 int WaitClean(char *dev
, int verbose
)
1092 if (!stat_is_blkdev(dev
, NULL
))
1094 fd
= open(dev
, O_RDONLY
);
1097 pr_err("Couldn't open %s: %s\n", dev
, strerror(errno
));
1101 strcpy(devnm
, fd2devnm(fd
));
1102 mdi
= sysfs_read(fd
, devnm
, GET_VERSION
|GET_LEVEL
|GET_SAFEMODE
);
1105 pr_err("Failed to read sysfs attributes for %s\n", dev
);
1110 switch(mdi
->array
.level
) {
1112 case LEVEL_MULTIPATH
:
1114 /* safemode delay is irrelevant for these levels */
1118 /* for internal metadata the kernel handles the final clean
1119 * transition, containers can never be dirty
1121 if (!is_subarray(mdi
->text_version
))
1124 /* safemode disabled ? */
1125 if (mdi
->safe_mode_delay
== 0)
1129 int state_fd
= sysfs_open(fd2devnm(fd
), NULL
, "array_state");
1133 /* minimize the safe_mode_delay and prepare to wait up to 5s
1134 * for writes to quiesce
1136 sysfs_set_safemode(mdi
, 1);
1138 /* wait for array_state to be clean */
1140 rv
= read(state_fd
, buf
, sizeof(buf
));
1143 if (sysfs_match_word(buf
, clean_states
) <
1144 (int)ARRAY_SIZE(clean_states
) - 1)
1146 rv
= sysfs_wait(state_fd
, &delay
);
1147 if (rv
< 0 && errno
!= EINTR
)
1149 lseek(state_fd
, 0, SEEK_SET
);
1153 else if (ping_monitor(mdi
->text_version
) == 0) {
1154 /* we need to ping to close the window between array
1155 * state transitioning to clean and the metadata being
1161 pr_err("Error connecting monitor with %s\n", dev
);
1164 pr_err("Error waiting for %s to be clean\n", dev
);
1166 /* restore the original safe_mode_delay */
1167 sysfs_set_safemode(mdi
, mdi
->safe_mode_delay
);