]>
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>
37 char devnm
[32]; /* to sync with mdstat info */
41 int active
, working
, failed
, spare
, raid
;
45 int devstate
[MAX_DISKS
];
46 dev_t devid
[MAX_DISKS
];
48 char parent_devnm
[32]; /* For subarray, devnm of parent.
51 struct supertype
*metadata
;
52 struct state
*subarray
;/* for a container it is a link to first subarray
53 * for a subarray it is a link to next subarray
54 * in the same container */
55 struct state
*parent
; /* for a subarray it is a link to its container
66 static int make_daemon(char *pidfile
);
67 static int check_one_sharer(int scan
);
68 static void write_autorebuild_pid(void);
69 static void alert(char *event
, char *dev
, char *disc
, struct alert_info
*info
);
70 static int check_array(struct state
*st
, struct mdstat_ent
*mdstat
,
71 int test
, struct alert_info
*info
,
72 int increments
, char *prefer
);
73 static int add_new_arrays(struct mdstat_ent
*mdstat
, struct state
**statelist
,
74 int test
, struct alert_info
*info
);
75 static void try_spare_migration(struct state
*statelist
, struct alert_info
*info
);
76 static void link_containers_with_subarrays(struct state
*list
);
78 static int check_udev_activity(void);
81 int Monitor(struct mddev_dev
*devlist
,
82 char *mailaddr
, char *alert_cmd
,
84 int daemonise
, int oneshot
,
85 int dosyslog
, char *pidfile
, int increments
,
89 * Every few seconds, scan every md device looking for changes
90 * When a change is found, log it, possibly run the alert command,
91 * and possibly send Email
93 * For each array, we record:
95 * active/working/failed/spare drives
96 * State of each device.
97 * %rebuilt if rebuilding
99 * If the update time changes, check out all the data again
100 * It is possible that we cannot get the state of each device
101 * due to bugs in the md kernel module.
102 * We also read /proc/mdstat to get rebuild percent,
103 * and to get state on all active devices incase of kernel bug.
107 * An active device had Faulty set or Active/Sync removed
109 * A spare device had Faulty set
111 * An active device had a reverse transition
113 * percent went from -1 to +ve
115 * percent went from below to not-below NN%
117 * Couldn't access a device which was previously visible
119 * if we detect an array with active<raid and spare==0
120 * we look at other arrays that have same spare-group
121 * If we find one with active==raid and spare>0,
122 * and if we can get_disk_info and find a name
123 * Then we hot-remove and hot-add to the other array
125 * If devlist is NULL, then we can monitor everything because --scan
126 * was given. We get an initial list from config file and add anything
127 * that appears in /proc/mdstat
130 struct state
*statelist
= NULL
;
133 struct mdstat_ent
*mdstat
= NULL
;
135 struct alert_info info
;
136 struct mddev_ident
*mdlist
;
137 int delay_for_event
= c
->delay
;
140 mailaddr
= conf_get_mailaddr();
141 if (mailaddr
&& ! c
->scan
)
142 pr_err("Monitor using email address \"%s\" from config file\n",
145 mailfrom
= conf_get_mailfrom();
148 alert_cmd
= conf_get_program();
149 if (alert_cmd
&& !c
->scan
)
150 pr_err("Monitor using program \"%s\" from config file\n",
153 if (c
->scan
&& !mailaddr
&& !alert_cmd
&& !dosyslog
) {
154 pr_err("No mail address or alert command - not monitoring.\n");
157 info
.alert_cmd
= alert_cmd
;
158 info
.mailaddr
= mailaddr
;
159 info
.mailfrom
= mailfrom
;
160 info
.dosyslog
= dosyslog
;
163 if (check_one_sharer(c
->scan
))
168 int rv
= make_daemon(pidfile
);
174 write_autorebuild_pid();
176 if (devlist
== NULL
) {
177 mdlist
= conf_get_ident(NULL
);
178 for (; mdlist
; mdlist
= mdlist
->next
) {
181 if (mdlist
->devname
== NULL
)
183 if (strcasecmp(mdlist
->devname
, "<ignore>") == 0)
185 st
= xcalloc(1, sizeof *st
);
186 if (mdlist
->devname
[0] == '/')
187 st
->devname
= xstrdup(mdlist
->devname
);
189 st
->devname
= xmalloc(8+strlen(mdlist
->devname
)+1);
190 strcpy(strcpy(st
->devname
, "/dev/md/"),
193 st
->next
= statelist
;
195 st
->percent
= RESYNC_UNKNOWN
;
197 st
->expected_spares
= mdlist
->spare_disks
;
198 if (mdlist
->spare_group
)
199 st
->spare_group
= xstrdup(mdlist
->spare_group
);
203 struct mddev_dev
*dv
;
205 for (dv
= devlist
; dv
; dv
= dv
->next
) {
206 struct state
*st
= xcalloc(1, sizeof *st
);
207 mdlist
= conf_get_ident(dv
->devname
);
208 st
->devname
= xstrdup(dv
->devname
);
209 st
->next
= statelist
;
211 st
->percent
= RESYNC_UNKNOWN
;
212 st
->expected_spares
= -1;
214 st
->expected_spares
= mdlist
->spare_disks
;
215 if (mdlist
->spare_group
)
216 st
->spare_group
= xstrdup(mdlist
->spare_group
);
224 struct state
*st
, **stp
;
226 int anyredundant
= 0;
230 mdstat
= mdstat_read(oneshot
? 0 : 1, 0);
232 for (st
= statelist
; st
; st
= st
->next
) {
233 if (check_array(st
, mdstat
, c
->test
, &info
,
234 increments
, c
->prefer
))
236 /* for external arrays, metadata is filled for
239 if (st
->metadata
&& st
->metadata
->ss
->external
)
241 if (st
->err
== 0 && !anyredundant
)
245 /* now check if there are any new devices found in mdstat */
247 new_found
= add_new_arrays(mdstat
, &statelist
, c
->test
,
250 /* If an array has active < raid && spare == 0 && spare_group != NULL
251 * Look for another array with spare > 0 and active == raid and same spare_group
252 * if found, choose a device and hotremove/hotadd
254 if (share
&& anydegraded
)
255 try_spare_migration(statelist
, &info
);
259 else if (!anyredundant
) {
260 pr_err("No array with redundancy detected, stopping\n");
266 * Wait for udevd to finish new devices
269 if (mdstat_wait(delay_for_event
) &&
270 check_udev_activity())
271 pr_err("Error while waiting for UDEV to complete new devices processing\n");
273 int wait_result
= mdstat_wait(delay_for_event
);
275 * Give chance to process new device
277 if (wait_result
!= 0) {
281 delay_for_event
= c
->delay
;
288 for (stp
= &statelist
; (st
= *stp
) != NULL
; ) {
289 if (st
->from_auto
&& st
->err
> 5) {
292 free(st
->spare_group
);
298 for (st2
= statelist
; st2
; st2
= statelist
) {
299 statelist
= st2
->next
;
308 static int make_daemon(char *pidfile
)
311 * -1 in the forked daemon
314 * so a none-negative becomes the exit code.
321 FILE *pid_file
= NULL
;
322 int fd
= open(pidfile
, O_WRONLY
| O_CREAT
| O_TRUNC
,
325 pid_file
= fdopen(fd
, "w");
327 perror("cannot create pid file");
329 fprintf(pid_file
,"%d\n", pid
);
344 static int check_one_sharer(int scan
)
349 char comm_path
[PATH_MAX
];
353 sprintf(path
, "%s/autorebuild.pid", MDMON_DIR
);
354 fp
= fopen(path
, "r");
356 if (fscanf(fp
, "%d", &pid
) != 1)
358 snprintf(comm_path
, sizeof(comm_path
),
359 "/proc/%d/comm", pid
);
360 comm_fp
= fopen(comm_path
, "r");
362 if (fscanf(comm_fp
, "%19s", comm
) &&
363 strncmp(basename(comm
), Name
, strlen(Name
)) == 0) {
365 pr_err("Only one autorebuild process allowed in scan mode, aborting\n");
370 pr_err("Warning: One autorebuild process already running.\n");
380 static void write_autorebuild_pid()
385 sprintf(path
, "%s/autorebuild.pid", MDMON_DIR
);
387 if (mkdir(MDMON_DIR
, 0700) < 0 && errno
!= EEXIST
) {
388 pr_err("Can't create autorebuild.pid file\n");
390 int fd
= open(path
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0700);
393 fp
= fdopen(fd
, "w");
396 pr_err("Can't create autorebuild.pid file\n");
399 fprintf(fp
, "%d\n", pid
);
405 static void alert(char *event
, char *dev
, char *disc
, struct alert_info
*info
)
409 if (!info
->alert_cmd
&& !info
->mailaddr
&& !info
->dosyslog
) {
410 time_t now
= time(0);
412 printf("%1.15s: %s on %s %s\n", ctime(&now
) + 4,
413 event
, dev
, disc
?disc
:"unknown device");
415 if (info
->alert_cmd
) {
419 waitpid(pid
, NULL
, 0);
424 execl(info
->alert_cmd
, info
->alert_cmd
,
425 event
, dev
, disc
, NULL
);
429 if (info
->mailaddr
&& (strncmp(event
, "Fail", 4) == 0 ||
430 strncmp(event
, "Test", 4) == 0 ||
431 strncmp(event
, "Spares", 6) == 0 ||
432 strncmp(event
, "Degrade", 7) == 0)) {
433 FILE *mp
= popen(Sendmail
, "w");
438 gethostname(hname
, sizeof(hname
));
439 signal_s(SIGPIPE
, SIG_IGN
);
442 fprintf(mp
, "From: %s\n", info
->mailfrom
);
444 fprintf(mp
, "From: %s monitoring <root>\n",
446 fprintf(mp
, "To: %s\n", info
->mailaddr
);
447 fprintf(mp
, "Subject: %s event on %s:%s\n\n",
451 "This is an automatically generated mail message from %s\n", Name
);
452 fprintf(mp
, "running on %s\n\n", hname
);
455 "A %s event had been detected on md device %s.\n\n", event
, dev
);
457 if (disc
&& disc
[0] != ' ')
459 "It could be related to component device %s.\n\n", disc
);
460 if (disc
&& disc
[0] == ' ')
461 fprintf(mp
, "Extra information:%s.\n\n", disc
);
463 fprintf(mp
, "Faithfully yours, etc.\n");
465 mdstat
= fopen("/proc/mdstat", "r");
470 "\nP.S. The /proc/mdstat file currently contains the following:\n\n");
471 while ((n
= fread(buf
, 1, sizeof(buf
),
473 n
= fwrite(buf
, 1, n
, mp
);
480 /* log the event to syslog maybe */
481 if (info
->dosyslog
) {
482 /* Log at a different severity depending on the event.
484 * These are the critical events: */
485 if (strncmp(event
, "Fail", 4) == 0 ||
486 strncmp(event
, "Degrade", 7) == 0 ||
487 strncmp(event
, "DeviceDisappeared", 17) == 0)
489 /* Good to know about, but are not failures: */
490 else if (strncmp(event
, "Rebuild", 7) == 0 ||
491 strncmp(event
, "MoveSpare", 9) == 0 ||
492 strncmp(event
, "Spares", 6) != 0)
493 priority
= LOG_WARNING
;
494 /* Everything else: */
498 if (disc
&& disc
[0] != ' ')
500 "%s event detected on md device %s, component device %s", event
, dev
, disc
);
503 "%s event detected on md device %s: %s",
507 "%s event detected on md device %s",
512 static int check_array(struct state
*st
, struct mdstat_ent
*mdstat
,
513 int test
, struct alert_info
*ainfo
,
514 int increments
, char *prefer
)
516 /* Update the state 'st' to reflect any changes shown in mdstat,
517 * or found by directly examining the array, and return
518 * '1' if the array is degraded, or '0' if it is optimal (or dead).
520 struct { int state
, major
, minor
; } info
[MAX_DISKS
];
521 struct mdinfo
*sra
= NULL
;
522 mdu_array_info_t array
;
523 struct mdstat_ent
*mse
= NULL
, *mse2
;
524 char *dev
= st
->devname
;
531 int is_container
= 0;
532 unsigned long redundancy_only_flags
= 0;
535 alert("TestMessage", dev
, NULL
, ainfo
);
539 fd
= open(dev
, O_RDONLY
);
543 if (st
->devnm
[0] == 0)
544 strcpy(st
->devnm
, fd2devnm(fd
));
546 for (mse2
= mdstat
; mse2
; mse2
= mse2
->next
)
547 if (strcmp(mse2
->devnm
, st
->devnm
) == 0) {
548 mse2
->devnm
[0] = 0; /* flag it as "used" */
553 /* duplicated array in statelist
554 * or re-created after reading mdstat
560 if (mse
->level
== NULL
)
563 if (!is_container
&& !md_array_active(fd
))
566 fcntl(fd
, F_SETFD
, FD_CLOEXEC
);
567 if (md_get_array_info(fd
, &array
) < 0)
570 if (!is_container
&& map_name(pers
, mse
->level
) > 0)
571 redundancy_only_flags
|= GET_MISMATCH
;
573 sra
= sysfs_read(-1, st
->devnm
, GET_LEVEL
| GET_DISKS
| GET_DEVS
|
574 GET_STATE
| redundancy_only_flags
);
579 /* It's much easier to list what array levels can't
580 * have a device disappear than all of them that can
582 if (sra
->array
.level
== 0 || sra
->array
.level
== -1) {
583 if (!st
->err
&& !st
->from_config
)
584 alert("DeviceDisappeared", dev
, " Wrong-Level", ainfo
);
589 /* this array is in /proc/mdstat */
590 if (array
.utime
== 0)
591 /* external arrays don't update utime, so
592 * just make sure it is always different. */
593 array
.utime
= st
->utime
+ 1;;
596 /* New array appeared where previously had an error */
598 st
->percent
= RESYNC_NONE
;
601 alert("NewArray", st
->devname
, NULL
, ainfo
);
604 if (st
->utime
== array
.utime
&& st
->failed
== sra
->array
.failed_disks
&&
605 st
->working
== sra
->array
.working_disks
&&
606 st
->spare
== sra
->array
.spare_disks
&&
607 (mse
== NULL
|| (mse
->percent
== st
->percent
))) {
608 if ((st
->active
< st
->raid
) && st
->spare
== 0)
612 if (st
->utime
== 0 && /* new array */
613 mse
->pattern
&& strchr(mse
->pattern
, '_') /* degraded */)
614 alert("DegradedArray", dev
, NULL
, ainfo
);
616 if (st
->utime
== 0 && /* new array */ st
->expected_spares
> 0 &&
617 sra
->array
.spare_disks
< st
->expected_spares
)
618 alert("SparesMissing", dev
, NULL
, ainfo
);
619 if (st
->percent
< 0 && st
->percent
!= RESYNC_UNKNOWN
&&
621 alert("RebuildStarted", dev
, NULL
, ainfo
);
622 if (st
->percent
>= 0 && mse
->percent
>= 0 &&
623 (mse
->percent
/ increments
) > (st
->percent
/ increments
)) {
624 char percentalert
[18];
626 * "RebuildNN" (10 chars) or "RebuildStarted" (15 chars)
629 if((mse
->percent
/ increments
) == 0)
630 snprintf(percentalert
, sizeof(percentalert
),
633 snprintf(percentalert
, sizeof(percentalert
),
634 "Rebuild%02d", mse
->percent
);
636 alert(percentalert
, dev
, NULL
, ainfo
);
639 if (mse
->percent
== RESYNC_NONE
&& st
->percent
>= 0) {
640 /* Rebuild/sync/whatever just finished.
641 * If there is a number in /mismatch_cnt,
642 * we should report that.
644 if (sra
&& sra
->mismatch_cnt
> 0) {
646 snprintf(cnt
, sizeof(cnt
),
647 " mismatches found: %d (on raid level %d)",
648 sra
->mismatch_cnt
, sra
->array
.level
);
649 alert("RebuildFinished", dev
, cnt
, ainfo
);
651 alert("RebuildFinished", dev
, NULL
, ainfo
);
653 st
->percent
= mse
->percent
;
655 remaining_disks
= sra
->array
.nr_disks
;
656 for (i
= 0; i
< MAX_DISKS
&& remaining_disks
> 0; i
++) {
657 mdu_disk_info_t disc
;
659 if (md_get_disk_info(fd
, &disc
) >= 0) {
660 info
[i
].state
= disc
.state
;
661 info
[i
].major
= disc
.major
;
662 info
[i
].minor
= disc
.minor
;
663 if (disc
.major
|| disc
.minor
)
666 info
[i
].major
= info
[i
].minor
= 0;
670 if (mse
->metadata_version
&&
671 strncmp(mse
->metadata_version
, "external:", 9) == 0 &&
672 is_subarray(mse
->metadata_version
+9)) {
674 strcpy(st
->parent_devnm
, mse
->metadata_version
+ 10);
675 sl
= strchr(st
->parent_devnm
, '/');
679 st
->parent_devnm
[0] = 0;
680 if (st
->metadata
== NULL
&& st
->parent_devnm
[0] == 0)
681 st
->metadata
= super_by_fd(fd
, NULL
);
683 for (i
= 0; i
< MAX_DISKS
; i
++) {
684 mdu_disk_info_t disc
= {0, 0, 0, 0, 0};
689 if (i
< last_disk
&& (info
[i
].major
|| info
[i
].minor
)) {
690 newstate
= info
[i
].state
;
691 dv
= map_dev_preferred(info
[i
].major
, info
[i
].minor
, 1,
693 disc
.state
= newstate
;
694 disc
.major
= info
[i
].major
;
695 disc
.minor
= info
[i
].minor
;
697 newstate
= (1 << MD_DISK_REMOVED
);
699 if (dv
== NULL
&& st
->devid
[i
])
700 dv
= map_dev_preferred(major(st
->devid
[i
]),
701 minor(st
->devid
[i
]), 1, prefer
);
702 change
= newstate
^ st
->devstate
[i
];
703 if (st
->utime
&& change
&& !st
->err
&& !new_array
) {
704 if ((st
->devstate
[i
]&change
) & (1 << MD_DISK_SYNC
))
705 alert("Fail", dev
, dv
, ainfo
);
706 else if ((newstate
& (1 << MD_DISK_FAULTY
)) &&
707 (disc
.major
|| disc
.minor
) &&
708 st
->devid
[i
] == makedev(disc
.major
,
710 alert("FailSpare", dev
, dv
, ainfo
);
711 else if ((newstate
&change
) & (1 << MD_DISK_SYNC
))
712 alert("SpareActive", dev
, dv
, ainfo
);
714 st
->devstate
[i
] = newstate
;
715 st
->devid
[i
] = makedev(disc
.major
, disc
.minor
);
717 st
->active
= sra
->array
.active_disks
;
718 st
->working
= sra
->array
.working_disks
;
719 st
->spare
= sra
->array
.spare_disks
;
720 st
->failed
= sra
->array
.failed_disks
;
721 st
->utime
= array
.utime
;
722 st
->raid
= sra
->array
.raid_disks
;
724 if ((st
->active
< st
->raid
) && st
->spare
== 0)
735 if (!st
->err
&& !is_container
)
736 alert("DeviceDisappeared", dev
, NULL
, ainfo
);
741 static int add_new_arrays(struct mdstat_ent
*mdstat
, struct state
**statelist
,
742 int test
, struct alert_info
*info
)
744 struct mdstat_ent
*mse
;
748 for (mse
= mdstat
; mse
; mse
= mse
->next
)
749 if (mse
->devnm
[0] && (!mse
->level
|| /* retrieve containers */
750 (strcmp(mse
->level
, "raid0") != 0 &&
751 strcmp(mse
->level
, "linear") != 0))) {
752 struct state
*st
= xcalloc(1, sizeof *st
);
753 mdu_array_info_t array
;
756 name
= get_md_name(mse
->devnm
);
762 st
->devname
= xstrdup(name
);
763 if ((fd
= open(st
->devname
, O_RDONLY
)) < 0 ||
764 md_get_array_info(fd
, &array
) < 0) {
768 put_md_name(st
->devname
);
771 st
->metadata
->ss
->free_super(st
->metadata
);
778 st
->next
= *statelist
;
781 strcpy(st
->devnm
, mse
->devnm
);
782 st
->percent
= RESYNC_UNKNOWN
;
783 st
->expected_spares
= -1;
784 if (mse
->metadata_version
&&
785 strncmp(mse
->metadata_version
,
786 "external:", 9) == 0 &&
787 is_subarray(mse
->metadata_version
+9)) {
789 strcpy(st
->parent_devnm
,
790 mse
->metadata_version
+10);
791 sl
= strchr(st
->parent_devnm
, '/');
794 st
->parent_devnm
[0] = 0;
797 alert("TestMessage", st
->devname
, NULL
, info
);
803 static int get_required_spare_criteria(struct state
*st
,
804 struct spare_criteria
*sc
)
808 if (!st
->metadata
|| !st
->metadata
->ss
->get_spare_criteria
) {
814 fd
= open(st
->devname
, O_RDONLY
);
817 if (st
->metadata
->ss
->external
)
818 st
->metadata
->ss
->load_container(st
->metadata
, fd
, st
->devname
);
820 st
->metadata
->ss
->load_super(st
->metadata
, fd
, st
->devname
);
822 if (!st
->metadata
->sb
)
825 st
->metadata
->ss
->get_spare_criteria(st
->metadata
, sc
);
826 st
->metadata
->ss
->free_super(st
->metadata
);
831 static int check_donor(struct state
*from
, struct state
*to
)
838 /* Cannot move from a member */
842 for (sub
= from
->subarray
; sub
; sub
= sub
->subarray
)
843 /* If source array has degraded subarrays, don't
846 if (sub
->active
< sub
->raid
)
848 if (from
->metadata
->ss
->external
== 0)
849 if (from
->active
< from
->raid
)
851 if (from
->spare
<= 0)
856 static dev_t
choose_spare(struct state
*from
, struct state
*to
,
857 struct domainlist
*domlist
, struct spare_criteria
*sc
)
862 for (d
= from
->raid
; !dev
&& d
< MAX_DISKS
; d
++) {
863 if (from
->devid
[d
] > 0 && from
->devstate
[d
] == 0) {
864 struct dev_policy
*pol
;
865 unsigned long long dev_size
;
866 unsigned int dev_sector_size
;
868 if (to
->metadata
->ss
->external
&&
869 test_partition_from_id(from
->devid
[d
]))
873 dev_size_from_id(from
->devid
[d
], &dev_size
) &&
874 dev_size
< sc
->min_size
)
877 if (sc
->sector_size
&&
878 dev_sector_size_from_id(from
->devid
[d
],
880 sc
->sector_size
!= dev_sector_size
)
883 pol
= devid_policy(from
->devid
[d
]);
884 if (from
->spare_group
)
885 pol_add(&pol
, pol_domain
,
886 from
->spare_group
, NULL
);
887 if (domain_test(domlist
, pol
,
888 to
->metadata
->ss
->name
) == 1)
889 dev
= from
->devid
[d
];
890 dev_policy_free(pol
);
896 static dev_t
container_choose_spare(struct state
*from
, struct state
*to
,
897 struct domainlist
*domlist
,
898 struct spare_criteria
*sc
, int active
)
900 /* This is similar to choose_spare, but we cannot trust devstate,
901 * so we need to read the metadata instead
904 struct supertype
*st
= from
->metadata
;
905 int fd
= open(from
->devname
, O_RDONLY
);
911 if (!st
->ss
->getinfo_super_disks
) {
916 err
= st
->ss
->load_container(st
, fd
, NULL
);
922 /* We must check if number of active disks has not increased
923 * since ioctl in main loop. mdmon may have added spare
924 * to subarray. If so we do not need to look for more spares
925 * so return non zero value */
928 list
= st
->ss
->getinfo_super_disks(st
);
930 st
->ss
->free_super(st
);
935 if (dp
->disk
.state
& (1 << MD_DISK_SYNC
) &&
936 !(dp
->disk
.state
& (1 << MD_DISK_FAULTY
)))
941 if (active
< active_cnt
) {
942 /* Spare just activated.*/
943 st
->ss
->free_super(st
);
948 /* We only need one spare so full list not needed */
949 list
= container_choose_spares(st
, sc
, domlist
, from
->spare_group
,
950 to
->metadata
->ss
->name
, 1);
952 struct mdinfo
*disks
= list
->devs
;
954 dev
= makedev(disks
->disk
.major
, disks
->disk
.minor
);
957 st
->ss
->free_super(st
);
961 static void try_spare_migration(struct state
*statelist
, struct alert_info
*info
)
965 struct spare_criteria sc
;
967 link_containers_with_subarrays(statelist
);
968 for (st
= statelist
; st
; st
= st
->next
)
969 if (st
->active
< st
->raid
&& st
->spare
== 0 && !st
->err
) {
970 struct domainlist
*domlist
= NULL
;
972 struct state
*to
= st
;
974 if (to
->parent_devnm
[0] && !to
->parent
)
975 /* subarray monitored without parent container
976 * we can't move spares here */
980 /* member of a container */
983 if (get_required_spare_criteria(to
, &sc
))
985 if (to
->metadata
->ss
->external
) {
986 /* We must make sure there is
987 * no suitable spare in container already.
988 * If there is we don't add more */
989 dev_t devid
= container_choose_spare(
990 to
, to
, NULL
, &sc
, st
->active
);
994 for (d
= 0; d
< MAX_DISKS
; d
++)
996 domainlist_add_dev(&domlist
,
998 to
->metadata
->ss
->name
);
1000 domain_add(&domlist
, to
->spare_group
);
1002 * No spare migration if the destination
1003 * has no domain. Skip this array.
1007 for (from
=statelist
; from
; from
=from
->next
) {
1009 if (!check_donor(from
, to
))
1011 if (from
->metadata
->ss
->external
)
1012 devid
= container_choose_spare(
1013 from
, to
, domlist
, &sc
, 0);
1015 devid
= choose_spare(from
, to
, domlist
,
1018 move_spare(from
->devname
, to
->devname
,
1020 alert("MoveSpare", to
->devname
,
1021 from
->devname
, info
);
1025 domain_free(domlist
);
1029 /* search the statelist to connect external
1030 * metadata subarrays with their containers
1031 * We always completely rebuild the tree from scratch as
1032 * that is safest considering the possibility of entries
1033 * disappearing or changing.
1035 static void link_containers_with_subarrays(struct state
*list
)
1039 for (st
= list
; st
; st
= st
->next
) {
1041 st
->subarray
= NULL
;
1043 for (st
= list
; st
; st
= st
->next
)
1044 if (st
->parent_devnm
[0])
1045 for (cont
= list
; cont
; cont
= cont
->next
)
1046 if (!cont
->err
&& cont
->parent_devnm
[0] == 0 &&
1047 strcmp(cont
->devnm
, st
->parent_devnm
) == 0) {
1049 st
->subarray
= cont
->subarray
;
1050 cont
->subarray
= st
;
1056 /* function: check_udev_activity
1057 * Description: Function waits for udev to finish
1058 * events processing.
1060 * 1 - detected error while opening udev
1062 * 0 - successfull completion
1064 static int check_udev_activity(void)
1066 struct udev
*udev
= NULL
;
1067 struct udev_queue
*udev_queue
= NULL
;
1068 int timeout_cnt
= 30;
1072 * In rare cases systemd may not have udevm,
1073 * in such cases just exit with rc 0
1084 udev_queue
= udev_queue_new(udev
);
1090 if (udev_queue_get_queue_is_empty(udev_queue
))
1093 while (!udev_queue_get_queue_is_empty(udev_queue
)) {
1106 udev_queue_unref(udev_queue
);
1113 /* Not really Monitor but ... */
1120 int frozen_remaining
= 3;
1122 if (!stat_is_blkdev(dev
, &rdev
))
1125 tmp
= devid2devnm(rdev
);
1127 pr_err("Cannot get md device name.\n");
1134 struct mdstat_ent
*ms
= mdstat_read(1, 0);
1135 struct mdstat_ent
*e
;
1137 for (e
= ms
; e
; e
= e
->next
)
1138 if (strcmp(e
->devnm
, devnm
) == 0)
1141 if (e
&& e
->percent
== RESYNC_NONE
) {
1142 /* We could be in the brief pause before something
1143 * starts. /proc/mdstat doesn't show that, but
1149 if (sysfs_init(&mdi
, -1, devnm
))
1151 if (sysfs_get_str(&mdi
, NULL
, "sync_action",
1153 strcmp(buf
,"idle\n") != 0) {
1154 e
->percent
= RESYNC_UNKNOWN
;
1155 if (strcmp(buf
, "frozen\n") == 0) {
1156 if (frozen_remaining
== 0)
1157 e
->percent
= RESYNC_NONE
;
1159 frozen_remaining
-= 1;
1163 if (!e
|| e
->percent
== RESYNC_NONE
) {
1164 if (e
&& e
->metadata_version
&&
1165 strncmp(e
->metadata_version
, "external:", 9) == 0) {
1166 if (is_subarray(&e
->metadata_version
[9]))
1167 ping_monitor(&e
->metadata_version
[9]);
1169 ping_monitor(devnm
);
1180 /* The state "broken" is used only for RAID0/LINEAR - it's the same as
1181 * "clean", but used in case the array has one or more members missing.
1183 static char *clean_states
[] = {
1184 "clear", "inactive", "readonly", "read-auto", "clean", "broken", NULL
};
1186 int WaitClean(char *dev
, int verbose
)
1193 if (!stat_is_blkdev(dev
, NULL
))
1195 fd
= open(dev
, O_RDONLY
);
1198 pr_err("Couldn't open %s: %s\n", dev
, strerror(errno
));
1202 strcpy(devnm
, fd2devnm(fd
));
1203 mdi
= sysfs_read(fd
, devnm
, GET_VERSION
|GET_LEVEL
|GET_SAFEMODE
);
1206 pr_err("Failed to read sysfs attributes for %s\n", dev
);
1211 switch(mdi
->array
.level
) {
1213 case LEVEL_MULTIPATH
:
1215 /* safemode delay is irrelevant for these levels */
1219 /* for internal metadata the kernel handles the final clean
1220 * transition, containers can never be dirty
1222 if (!is_subarray(mdi
->text_version
))
1225 /* safemode disabled ? */
1226 if (mdi
->safe_mode_delay
== 0)
1230 int state_fd
= sysfs_open(fd2devnm(fd
), NULL
, "array_state");
1234 /* minimize the safe_mode_delay and prepare to wait up to 5s
1235 * for writes to quiesce
1237 sysfs_set_safemode(mdi
, 1);
1239 /* wait for array_state to be clean */
1241 rv
= read(state_fd
, buf
, sizeof(buf
));
1244 if (sysfs_match_word(buf
, clean_states
) <
1245 (int)ARRAY_SIZE(clean_states
) - 1)
1247 rv
= sysfs_wait(state_fd
, &delay
);
1248 if (rv
< 0 && errno
!= EINTR
)
1250 lseek(state_fd
, 0, SEEK_SET
);
1254 else if (ping_monitor(mdi
->text_version
) == 0) {
1255 /* we need to ping to close the window between array
1256 * state transitioning to clean and the metadata being
1262 pr_err("Error connecting monitor with %s\n", dev
);
1265 pr_err("Error waiting for %s to be clean\n", dev
);
1267 /* restore the original safe_mode_delay */
1268 sysfs_set_safemode(mdi
, mdi
->safe_mode_delay
);