]>
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
;
218 mdstat
= mdstat_read(oneshot
? 0 : 1, 0);
222 for (st
= statelist
; st
; st
= st
->next
)
223 if (check_array(st
, mdstat
, c
->test
, &info
,
224 increments
, c
->prefer
))
227 /* now check if there are any new devices found in mdstat */
229 new_found
= add_new_arrays(mdstat
, &statelist
, c
->test
,
232 /* If an array has active < raid && spare == 0 && spare_group != NULL
233 * Look for another array with spare > 0 and active == raid and same spare_group
234 * if found, choose a device and hotremove/hotadd
236 if (share
&& anydegraded
)
237 try_spare_migration(statelist
, &info
);
242 mdstat_wait(c
->delay
);
246 for (stp
= &statelist
; (st
= *stp
) != NULL
; ) {
247 if (st
->from_auto
&& st
->err
> 5) {
250 free(st
->spare_group
);
256 for (st2
= statelist
; st2
; st2
= statelist
) {
257 statelist
= st2
->next
;
266 static int make_daemon(char *pidfile
)
269 * -1 in the forked daemon
272 * so a none-negative becomes the exit code.
280 pid_file
=fopen(pidfile
, "w");
282 perror("cannot create pid file");
284 fprintf(pid_file
,"%d\n", pid
);
295 open("/dev/null", O_RDWR
);
302 static int check_one_sharer(int scan
)
311 sprintf(path
, "%s/autorebuild.pid", MDMON_DIR
);
312 fp
= fopen(path
, "r");
314 if (fscanf(fp
, "%d", &pid
) != 1)
316 snprintf(comm_path
, sizeof(comm_path
),
317 "/proc/%d/comm", pid
);
318 comm_fp
= fopen(comm_path
, "r");
320 if (fscanf(comm_fp
, "%s", comm
) &&
321 strncmp(basename(comm
), Name
, strlen(Name
)) == 0) {
323 pr_err("Only one autorebuild process allowed in scan mode, aborting\n");
328 pr_err("Warning: One autorebuild process already running.\n");
336 if (mkdir(MDMON_DIR
, S_IRWXU
) < 0 && errno
!= EEXIST
) {
337 pr_err("Can't create autorebuild.pid file\n");
339 fp
= fopen(path
, "w");
341 pr_err("Cannot create autorebuild.pidfile\n");
344 fprintf(fp
, "%d\n", pid
);
352 static void alert(char *event
, char *dev
, char *disc
, struct alert_info
*info
)
356 if (!info
->alert_cmd
&& !info
->mailaddr
&& !info
->dosyslog
) {
357 time_t now
= time(0);
359 printf("%1.15s: %s on %s %s\n", ctime(&now
) + 4,
360 event
, dev
, disc
?disc
:"unknown device");
362 if (info
->alert_cmd
) {
366 waitpid(pid
, NULL
, 0);
371 execl(info
->alert_cmd
, info
->alert_cmd
,
372 event
, dev
, disc
, NULL
);
376 if (info
->mailaddr
&& (strncmp(event
, "Fail", 4) == 0 ||
377 strncmp(event
, "Test", 4) == 0 ||
378 strncmp(event
, "Spares", 6) == 0 ||
379 strncmp(event
, "Degrade", 7) == 0)) {
380 FILE *mp
= popen(Sendmail
, "w");
384 gethostname(hname
, sizeof(hname
));
385 signal(SIGPIPE
, SIG_IGN
);
387 fprintf(mp
, "From: %s\n", info
->mailfrom
);
389 fprintf(mp
, "From: %s monitoring <root>\n",
391 fprintf(mp
, "To: %s\n", info
->mailaddr
);
392 fprintf(mp
, "Subject: %s event on %s:%s\n\n",
396 "This is an automatically generated mail message from %s\n", Name
);
397 fprintf(mp
, "running on %s\n\n", hname
);
400 "A %s event had been detected on md device %s.\n\n", event
, dev
);
402 if (disc
&& disc
[0] != ' ')
404 "It could be related to component device %s.\n\n", disc
);
405 if (disc
&& disc
[0] == ' ')
406 fprintf(mp
, "Extra information:%s.\n\n", disc
);
408 fprintf(mp
, "Faithfully yours, etc.\n");
410 mdstat
= fopen("/proc/mdstat", "r");
415 "\nP.S. The /proc/mdstat file currently contains the following:\n\n");
416 while ((n
= fread(buf
, 1, sizeof(buf
),
418 n
= fwrite(buf
, 1, n
, mp
);
425 /* log the event to syslog maybe */
426 if (info
->dosyslog
) {
427 /* Log at a different severity depending on the event.
429 * These are the critical events: */
430 if (strncmp(event
, "Fail", 4) == 0 ||
431 strncmp(event
, "Degrade", 7) == 0 ||
432 strncmp(event
, "DeviceDisappeared", 17) == 0)
434 /* Good to know about, but are not failures: */
435 else if (strncmp(event
, "Rebuild", 7) == 0 ||
436 strncmp(event
, "MoveSpare", 9) == 0 ||
437 strncmp(event
, "Spares", 6) != 0)
438 priority
= LOG_WARNING
;
439 /* Everything else: */
443 if (disc
&& disc
[0] != ' ')
445 "%s event detected on md device %s, component device %s", event
, dev
, disc
);
448 "%s event detected on md device %s: %s",
452 "%s event detected on md device %s",
457 static int check_array(struct state
*st
, struct mdstat_ent
*mdstat
,
458 int test
, struct alert_info
*ainfo
,
459 int increments
, char *prefer
)
461 /* Update the state 'st' to reflect any changes shown in mdstat,
462 * or found by directly examining the array, and return
463 * '1' if the array is degraded, or '0' if it is optimal (or dead).
465 struct { int state
, major
, minor
; } info
[MAX_DISKS
];
466 struct mdinfo
*sra
= NULL
;
467 mdu_array_info_t array
;
468 struct mdstat_ent
*mse
= NULL
, *mse2
;
469 char *dev
= st
->devname
;
476 int is_container
= 0;
477 unsigned long redundancy_only_flags
= 0;
480 alert("TestMessage", dev
, NULL
, ainfo
);
484 fd
= open(dev
, O_RDONLY
);
488 if (st
->devnm
[0] == 0)
489 strcpy(st
->devnm
, fd2devnm(fd
));
491 for (mse2
= mdstat
; mse2
; mse2
= mse2
->next
)
492 if (strcmp(mse2
->devnm
, st
->devnm
) == 0) {
493 mse2
->devnm
[0] = 0; /* flag it as "used" */
498 /* duplicated array in statelist
499 * or re-created after reading mdstat
505 if (mse
->level
== NULL
)
508 if (!is_container
&& !md_array_active(fd
))
511 fcntl(fd
, F_SETFD
, FD_CLOEXEC
);
512 if (md_get_array_info(fd
, &array
) < 0)
515 if (!is_container
&& map_name(pers
, mse
->level
) > 0)
516 redundancy_only_flags
|= GET_MISMATCH
;
518 sra
= sysfs_read(-1, st
->devnm
, GET_LEVEL
| GET_DISKS
| GET_DEVS
|
519 GET_STATE
| redundancy_only_flags
);
524 /* It's much easier to list what array levels can't
525 * have a device disappear than all of them that can
527 if (sra
->array
.level
== 0 || sra
->array
.level
== -1) {
528 if (!st
->err
&& !st
->from_config
)
529 alert("DeviceDisappeared", dev
, " Wrong-Level", ainfo
);
534 /* this array is in /proc/mdstat */
535 if (array
.utime
== 0)
536 /* external arrays don't update utime, so
537 * just make sure it is always different. */
538 array
.utime
= st
->utime
+ 1;;
541 /* New array appeared where previously had an error */
543 st
->percent
= RESYNC_NONE
;
545 alert("NewArray", st
->devname
, NULL
, ainfo
);
548 if (st
->utime
== array
.utime
&& st
->failed
== sra
->array
.failed_disks
&&
549 st
->working
== sra
->array
.working_disks
&&
550 st
->spare
== sra
->array
.spare_disks
&&
551 (mse
== NULL
|| (mse
->percent
== st
->percent
))) {
552 if ((st
->active
< st
->raid
) && st
->spare
== 0)
556 if (st
->utime
== 0 && /* new array */
557 mse
->pattern
&& strchr(mse
->pattern
, '_') /* degraded */)
558 alert("DegradedArray", dev
, NULL
, ainfo
);
560 if (st
->utime
== 0 && /* new array */ st
->expected_spares
> 0 &&
561 sra
->array
.spare_disks
< st
->expected_spares
)
562 alert("SparesMissing", dev
, NULL
, ainfo
);
563 if (st
->percent
< 0 && st
->percent
!= RESYNC_UNKNOWN
&&
565 alert("RebuildStarted", dev
, NULL
, ainfo
);
566 if (st
->percent
>= 0 && mse
->percent
>= 0 &&
567 (mse
->percent
/ increments
) > (st
->percent
/ increments
)) {
568 char percentalert
[18];
570 * "RebuildNN" (10 chars) or "RebuildStarted" (15 chars)
573 if((mse
->percent
/ increments
) == 0)
574 snprintf(percentalert
, sizeof(percentalert
),
577 snprintf(percentalert
, sizeof(percentalert
),
578 "Rebuild%02d", mse
->percent
);
580 alert(percentalert
, dev
, NULL
, ainfo
);
583 if (mse
->percent
== RESYNC_NONE
&& st
->percent
>= 0) {
584 /* Rebuild/sync/whatever just finished.
585 * If there is a number in /mismatch_cnt,
586 * we should report that.
588 if (sra
&& sra
->mismatch_cnt
> 0) {
590 snprintf(cnt
, sizeof(cnt
),
591 " mismatches found: %d (on raid level %d)",
592 sra
->mismatch_cnt
, sra
->array
.level
);
593 alert("RebuildFinished", dev
, cnt
, ainfo
);
595 alert("RebuildFinished", dev
, NULL
, ainfo
);
597 st
->percent
= mse
->percent
;
599 remaining_disks
= sra
->array
.nr_disks
;
600 for (i
= 0; i
< MAX_DISKS
&& remaining_disks
> 0; i
++) {
601 mdu_disk_info_t disc
;
603 if (md_get_disk_info(fd
, &disc
) >= 0) {
604 info
[i
].state
= disc
.state
;
605 info
[i
].major
= disc
.major
;
606 info
[i
].minor
= disc
.minor
;
607 if (disc
.major
|| disc
.minor
)
610 info
[i
].major
= info
[i
].minor
= 0;
614 if (mse
->metadata_version
&&
615 strncmp(mse
->metadata_version
, "external:", 9) == 0 &&
616 is_subarray(mse
->metadata_version
+9)) {
618 strcpy(st
->parent_devnm
, mse
->metadata_version
+ 10);
619 sl
= strchr(st
->parent_devnm
, '/');
623 st
->parent_devnm
[0] = 0;
624 if (st
->metadata
== NULL
&& st
->parent_devnm
[0] == 0)
625 st
->metadata
= super_by_fd(fd
, NULL
);
627 for (i
= 0; i
< MAX_DISKS
; i
++) {
628 mdu_disk_info_t disc
= {0, 0, 0, 0, 0};
633 if (i
< last_disk
&& (info
[i
].major
|| info
[i
].minor
)) {
634 newstate
= info
[i
].state
;
635 dv
= map_dev_preferred(info
[i
].major
, info
[i
].minor
, 1,
637 disc
.state
= newstate
;
638 disc
.major
= info
[i
].major
;
639 disc
.minor
= info
[i
].minor
;
641 newstate
= (1 << MD_DISK_REMOVED
);
643 if (dv
== NULL
&& st
->devid
[i
])
644 dv
= map_dev_preferred(major(st
->devid
[i
]),
645 minor(st
->devid
[i
]), 1, prefer
);
646 change
= newstate
^ st
->devstate
[i
];
647 if (st
->utime
&& change
&& !st
->err
&& !new_array
) {
648 if ((st
->devstate
[i
]&change
) & (1 << MD_DISK_SYNC
))
649 alert("Fail", dev
, dv
, ainfo
);
650 else if ((newstate
& (1 << MD_DISK_FAULTY
)) &&
651 (disc
.major
|| disc
.minor
) &&
652 st
->devid
[i
] == makedev(disc
.major
,
654 alert("FailSpare", dev
, dv
, ainfo
);
655 else if ((newstate
&change
) & (1 << MD_DISK_SYNC
))
656 alert("SpareActive", dev
, dv
, ainfo
);
658 st
->devstate
[i
] = newstate
;
659 st
->devid
[i
] = makedev(disc
.major
, disc
.minor
);
661 st
->active
= sra
->array
.active_disks
;
662 st
->working
= sra
->array
.working_disks
;
663 st
->spare
= sra
->array
.spare_disks
;
664 st
->failed
= sra
->array
.failed_disks
;
665 st
->utime
= array
.utime
;
666 st
->raid
= sra
->array
.raid_disks
;
668 if ((st
->active
< st
->raid
) && st
->spare
== 0)
680 alert("DeviceDisappeared", dev
, NULL
, ainfo
);
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
)
693 if (mse
->devnm
[0] && (!mse
->level
|| /* retrieve containers */
694 (strcmp(mse
->level
, "raid0") != 0 &&
695 strcmp(mse
->level
, "linear") != 0))) {
696 struct state
*st
= xcalloc(1, sizeof *st
);
697 mdu_array_info_t array
;
700 name
= get_md_name(mse
->devnm
);
706 st
->devname
= xstrdup(name
);
707 if ((fd
= open(st
->devname
, O_RDONLY
)) < 0 ||
708 md_get_array_info(fd
, &array
) < 0) {
712 put_md_name(st
->devname
);
715 st
->metadata
->ss
->free_super(st
->metadata
);
722 st
->next
= *statelist
;
725 strcpy(st
->devnm
, mse
->devnm
);
726 st
->percent
= RESYNC_UNKNOWN
;
727 st
->expected_spares
= -1;
728 if (mse
->metadata_version
&&
729 strncmp(mse
->metadata_version
,
730 "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_required_spare_criteria(struct state
*st
,
748 struct spare_criteria
*sc
)
752 if (!st
->metadata
|| !st
->metadata
->ss
->get_spare_criteria
) {
758 fd
= open(st
->devname
, O_RDONLY
);
761 if (st
->metadata
->ss
->external
)
762 st
->metadata
->ss
->load_container(st
->metadata
, fd
, st
->devname
);
764 st
->metadata
->ss
->load_super(st
->metadata
, fd
, st
->devname
);
766 if (!st
->metadata
->sb
)
769 st
->metadata
->ss
->get_spare_criteria(st
->metadata
, sc
);
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
, struct spare_criteria
*sc
)
806 for (d
= from
->raid
; !dev
&& d
< MAX_DISKS
; d
++) {
807 if (from
->devid
[d
] > 0 && from
->devstate
[d
] == 0) {
808 struct dev_policy
*pol
;
809 unsigned long long dev_size
;
810 unsigned int dev_sector_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
) &&
818 dev_size
< sc
->min_size
)
821 if (sc
->sector_size
&&
822 dev_sector_size_from_id(from
->devid
[d
],
824 sc
->sector_size
!= dev_sector_size
)
827 pol
= devid_policy(from
->devid
[d
]);
828 if (from
->spare_group
)
829 pol_add(&pol
, pol_domain
,
830 from
->spare_group
, NULL
);
831 if (domain_test(domlist
, pol
,
832 to
->metadata
->ss
->name
) == 1)
833 dev
= from
->devid
[d
];
834 dev_policy_free(pol
);
840 static dev_t
container_choose_spare(struct state
*from
, struct state
*to
,
841 struct domainlist
*domlist
,
842 struct spare_criteria
*sc
, int active
)
844 /* This is similar to choose_spare, but we cannot trust devstate,
845 * so we need to read the metadata instead
848 struct supertype
*st
= from
->metadata
;
849 int fd
= open(from
->devname
, O_RDONLY
);
855 if (!st
->ss
->getinfo_super_disks
) {
860 err
= st
->ss
->load_container(st
, fd
, NULL
);
866 /* We must check if number of active disks has not increased
867 * since ioctl in main loop. mdmon may have added spare
868 * to subarray. If so we do not need to look for more spares
869 * so return non zero value */
872 list
= st
->ss
->getinfo_super_disks(st
);
874 st
->ss
->free_super(st
);
879 if (dp
->disk
.state
& (1 << MD_DISK_SYNC
) &&
880 !(dp
->disk
.state
& (1 << MD_DISK_FAULTY
)))
885 if (active
< active_cnt
) {
886 /* Spare just activated.*/
887 st
->ss
->free_super(st
);
892 /* We only need one spare so full list not needed */
893 list
= container_choose_spares(st
, sc
, domlist
, from
->spare_group
,
894 to
->metadata
->ss
->name
, 1);
896 struct mdinfo
*disks
= list
->devs
;
898 dev
= makedev(disks
->disk
.major
, disks
->disk
.minor
);
901 st
->ss
->free_super(st
);
905 static void try_spare_migration(struct state
*statelist
, struct alert_info
*info
)
909 struct spare_criteria sc
;
911 link_containers_with_subarrays(statelist
);
912 for (st
= statelist
; st
; st
= st
->next
)
913 if (st
->active
< st
->raid
&& st
->spare
== 0 && !st
->err
) {
914 struct domainlist
*domlist
= NULL
;
916 struct state
*to
= st
;
918 if (to
->parent_devnm
[0] && !to
->parent
)
919 /* subarray monitored without parent container
920 * we can't move spares here */
924 /* member of a container */
927 if (get_required_spare_criteria(to
, &sc
))
929 if (to
->metadata
->ss
->external
) {
930 /* We must make sure there is
931 * no suitable spare in container already.
932 * If there is we don't add more */
933 dev_t devid
= container_choose_spare(
934 to
, to
, NULL
, &sc
, st
->active
);
938 for (d
= 0; d
< MAX_DISKS
; d
++)
940 domainlist_add_dev(&domlist
,
942 to
->metadata
->ss
->name
);
944 domain_add(&domlist
, to
->spare_group
);
946 * No spare migration if the destination
947 * has no domain. Skip this array.
951 for (from
=statelist
; from
; from
=from
->next
) {
953 if (!check_donor(from
, to
))
955 if (from
->metadata
->ss
->external
)
956 devid
= container_choose_spare(
957 from
, to
, domlist
, &sc
, 0);
959 devid
= choose_spare(from
, to
, domlist
,
962 move_spare(from
->devname
, to
->devname
,
964 alert("MoveSpare", to
->devname
,
965 from
->devname
, info
);
969 domain_free(domlist
);
973 /* search the statelist to connect external
974 * metadata subarrays with their containers
975 * We always completely rebuild the tree from scratch as
976 * that is safest considering the possibility of entries
977 * disappearing or changing.
979 static void link_containers_with_subarrays(struct state
*list
)
983 for (st
= list
; st
; st
= st
->next
) {
987 for (st
= list
; st
; st
= st
->next
)
988 if (st
->parent_devnm
[0])
989 for (cont
= list
; cont
; cont
= cont
->next
)
990 if (!cont
->err
&& cont
->parent_devnm
[0] == 0 &&
991 strcmp(cont
->devnm
, st
->parent_devnm
) == 0) {
993 st
->subarray
= cont
->subarray
;
999 /* Not really Monitor but ... */
1006 int frozen_remaining
= 3;
1008 if (!stat_is_blkdev(dev
, &rdev
))
1011 tmp
= devid2devnm(rdev
);
1013 pr_err("Cannot get md device name.\n");
1020 struct mdstat_ent
*ms
= mdstat_read(1, 0);
1021 struct mdstat_ent
*e
;
1023 for (e
= ms
; e
; e
= e
->next
)
1024 if (strcmp(e
->devnm
, devnm
) == 0)
1027 if (e
&& e
->percent
== RESYNC_NONE
) {
1028 /* We could be in the brief pause before something
1029 * starts. /proc/mdstat doesn't show that, but
1035 if (sysfs_init(&mdi
, -1, devnm
))
1037 if (sysfs_get_str(&mdi
, NULL
, "sync_action",
1039 strcmp(buf
,"idle\n") != 0) {
1040 e
->percent
= RESYNC_UNKNOWN
;
1041 if (strcmp(buf
, "frozen\n") == 0) {
1042 if (frozen_remaining
== 0)
1043 e
->percent
= RESYNC_NONE
;
1045 frozen_remaining
-= 1;
1049 if (!e
|| e
->percent
== RESYNC_NONE
) {
1050 if (e
&& e
->metadata_version
&&
1051 strncmp(e
->metadata_version
, "external:", 9) == 0) {
1052 if (is_subarray(&e
->metadata_version
[9]))
1053 ping_monitor(&e
->metadata_version
[9]);
1055 ping_monitor(devnm
);
1066 /* The state "broken" is used only for RAID0/LINEAR - it's the same as
1067 * "clean", but used in case the array has one or more members missing.
1069 static char *clean_states
[] = {
1070 "clear", "inactive", "readonly", "read-auto", "clean", "broken", NULL
};
1072 int WaitClean(char *dev
, int verbose
)
1079 if (!stat_is_blkdev(dev
, NULL
))
1081 fd
= open(dev
, O_RDONLY
);
1084 pr_err("Couldn't open %s: %s\n", dev
, strerror(errno
));
1088 strcpy(devnm
, fd2devnm(fd
));
1089 mdi
= sysfs_read(fd
, devnm
, GET_VERSION
|GET_LEVEL
|GET_SAFEMODE
);
1092 pr_err("Failed to read sysfs attributes for %s\n", dev
);
1097 switch(mdi
->array
.level
) {
1099 case LEVEL_MULTIPATH
:
1101 /* safemode delay is irrelevant for these levels */
1105 /* for internal metadata the kernel handles the final clean
1106 * transition, containers can never be dirty
1108 if (!is_subarray(mdi
->text_version
))
1111 /* safemode disabled ? */
1112 if (mdi
->safe_mode_delay
== 0)
1116 int state_fd
= sysfs_open(fd2devnm(fd
), NULL
, "array_state");
1120 /* minimize the safe_mode_delay and prepare to wait up to 5s
1121 * for writes to quiesce
1123 sysfs_set_safemode(mdi
, 1);
1125 /* wait for array_state to be clean */
1127 rv
= read(state_fd
, buf
, sizeof(buf
));
1130 if (sysfs_match_word(buf
, clean_states
) <
1131 (int)ARRAY_SIZE(clean_states
) - 1)
1133 rv
= sysfs_wait(state_fd
, &delay
);
1134 if (rv
< 0 && errno
!= EINTR
)
1136 lseek(state_fd
, 0, SEEK_SET
);
1140 else if (ping_monitor(mdi
->text_version
) == 0) {
1141 /* we need to ping to close the window between array
1142 * state transitioning to clean and the metadata being
1148 pr_err("Error connecting monitor with %s\n", dev
);
1151 pr_err("Error waiting for %s to be clean\n", dev
);
1153 /* restore the original safe_mode_delay */
1154 sysfs_set_safemode(mdi
, mdi
->safe_mode_delay
);