]> git.ipfire.org Git - thirdparty/mdadm.git/blob - Monitor.c
Monitor: block if monitor modes are combined.
[thirdparty/mdadm.git] / Monitor.c
1 /*
2 * mdadm - manage Linux "md" devices aka RAID arrays.
3 *
4 * Copyright (C) 2001-2009 Neil Brown <neilb@suse.de>
5 *
6 *
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.
11 *
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.
16 *
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
20 *
21 * Author: Neil Brown
22 * Email: <neilb@suse.de>
23 */
24
25 #include "mdadm.h"
26 #include "md_p.h"
27 #include "md_u.h"
28 #include <sys/wait.h>
29 #include <limits.h>
30 #include <syslog.h>
31 #ifndef NO_LIBUDEV
32 #include <libudev.h>
33 #endif
34
35 struct state {
36 char devname[MD_NAME_MAX + sizeof("/dev/md/")]; /* length of "/dev/md/" + device name + terminating byte*/
37 char devnm[MD_NAME_MAX]; /* to sync with mdstat info */
38 unsigned int utime;
39 int err;
40 char *spare_group;
41 int active, working, failed, spare, raid;
42 int from_config;
43 int from_auto;
44 int expected_spares;
45 int devstate[MAX_DISKS];
46 dev_t devid[MAX_DISKS];
47 int percent;
48 char parent_devnm[MD_NAME_MAX]; /* For subarray, devnm of parent.
49 * For others, ""
50 */
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
56 */
57 struct state *next;
58 };
59
60 struct alert_info {
61 char *mailaddr;
62 char *mailfrom;
63 char *alert_cmd;
64 int dosyslog;
65 };
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(const char *event, const char *dev, const 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);
77 static void free_statelist(struct state *statelist);
78 #ifndef NO_LIBUDEV
79 static int check_udev_activity(void);
80 #endif
81
82 int Monitor(struct mddev_dev *devlist,
83 char *mailaddr, char *alert_cmd,
84 struct context *c,
85 int daemonise, int oneshot,
86 int dosyslog, char *pidfile, int increments,
87 int share)
88 {
89 /*
90 * Every few seconds, scan every md device looking for changes
91 * When a change is found, log it, possibly run the alert command,
92 * and possibly send Email
93 *
94 * For each array, we record:
95 * Update time
96 * active/working/failed/spare drives
97 * State of each device.
98 * %rebuilt if rebuilding
99 *
100 * If the update time changes, check out all the data again
101 * It is possible that we cannot get the state of each device
102 * due to bugs in the md kernel module.
103 * We also read /proc/mdstat to get rebuild percent,
104 * and to get state on all active devices incase of kernel bug.
105 *
106 * Events are:
107 * Fail
108 * An active device had Faulty set or Active/Sync removed
109 * FailSpare
110 * A spare device had Faulty set
111 * SpareActive
112 * An active device had a reverse transition
113 * RebuildStarted
114 * percent went from -1 to +ve
115 * RebuildNN
116 * percent went from below to not-below NN%
117 * DeviceDisappeared
118 * Couldn't access a device which was previously visible
119 *
120 * if we detect an array with active<raid and spare==0
121 * we look at other arrays that have same spare-group
122 * If we find one with active==raid and spare>0,
123 * and if we can get_disk_info and find a name
124 * Then we hot-remove and hot-add to the other array
125 *
126 * If devlist is NULL, then we can monitor everything if --scan
127 * was given. We get an initial list from config file and add anything
128 * that appears in /proc/mdstat
129 */
130
131 struct state *statelist = NULL;
132 int finished = 0;
133 struct mdstat_ent *mdstat = NULL;
134 char *mailfrom;
135 struct alert_info info;
136 struct mddev_ident *mdlist;
137 int delay_for_event = c->delay;
138
139 if (devlist && c->scan) {
140 pr_err("Devices list and --scan option cannot be combined - not monitoring.\n");
141 return 1;
142 }
143
144 if (!mailaddr)
145 mailaddr = conf_get_mailaddr();
146
147 if (!alert_cmd)
148 alert_cmd = conf_get_program();
149
150 mailfrom = conf_get_mailfrom();
151
152 if (c->scan && !mailaddr && !alert_cmd && !dosyslog) {
153 pr_err("No mail address or alert command - not monitoring.\n");
154 return 1;
155 }
156
157 if (c->verbose) {
158 pr_err("Monitor is started with delay %ds\n", c->delay);
159 if (mailaddr)
160 pr_err("Monitor using email address %s\n", mailaddr);
161 if (alert_cmd)
162 pr_err("Monitor using program %s\n", alert_cmd);
163 }
164
165 info.alert_cmd = alert_cmd;
166 info.mailaddr = mailaddr;
167 info.mailfrom = mailfrom;
168 info.dosyslog = dosyslog;
169
170 if (share){
171 if (check_one_sharer(c->scan))
172 return 1;
173 }
174
175 if (daemonise) {
176 int rv = make_daemon(pidfile);
177 if (rv >= 0)
178 return rv;
179 }
180
181 if (share)
182 write_autorebuild_pid();
183
184 if (devlist == NULL) {
185 mdlist = conf_get_ident(NULL);
186 for (; mdlist; mdlist = mdlist->next) {
187 struct state *st;
188
189 if (mdlist->devname == NULL)
190 continue;
191 if (strcasecmp(mdlist->devname, "<ignore>") == 0)
192 continue;
193 if (!is_mddev(mdlist->devname))
194 continue;
195
196 st = xcalloc(1, sizeof *st);
197 snprintf(st->devname, MD_NAME_MAX + sizeof("/dev/md/"),
198 "/dev/md/%s", basename(mdlist->devname));
199 st->next = statelist;
200 st->devnm[0] = 0;
201 st->percent = RESYNC_UNKNOWN;
202 st->from_config = 1;
203 st->expected_spares = mdlist->spare_disks;
204 if (mdlist->spare_group)
205 st->spare_group = xstrdup(mdlist->spare_group);
206 statelist = st;
207 }
208 } else {
209 struct mddev_dev *dv;
210
211 for (dv = devlist; dv; dv = dv->next) {
212 struct state *st;
213
214 if (!is_mddev(dv->devname))
215 continue;
216
217 st = xcalloc(1, sizeof *st);
218 mdlist = conf_get_ident(dv->devname);
219 snprintf(st->devname, MD_NAME_MAX + sizeof("/dev/md/"), "%s", dv->devname);
220 st->next = statelist;
221 st->devnm[0] = 0;
222 st->percent = RESYNC_UNKNOWN;
223 st->expected_spares = -1;
224 if (mdlist) {
225 st->expected_spares = mdlist->spare_disks;
226 if (mdlist->spare_group)
227 st->spare_group = xstrdup(mdlist->spare_group);
228 }
229 statelist = st;
230 }
231 }
232
233 while (!finished) {
234 int new_found = 0;
235 struct state *st, **stp;
236 int anydegraded = 0;
237 int anyredundant = 0;
238
239 if (mdstat)
240 free_mdstat(mdstat);
241 mdstat = mdstat_read(oneshot ? 0 : 1, 0);
242
243 for (st = statelist; st; st = st->next) {
244 if (check_array(st, mdstat, c->test, &info,
245 increments, c->prefer))
246 anydegraded = 1;
247 /* for external arrays, metadata is filled for
248 * containers only
249 */
250 if (st->metadata && st->metadata->ss->external)
251 continue;
252 if (st->err == 0 && !anyredundant)
253 anyredundant = 1;
254 }
255
256 /* now check if there are any new devices found in mdstat */
257 if (c->scan)
258 new_found = add_new_arrays(mdstat, &statelist, c->test,
259 &info);
260
261 /* If an array has active < raid && spare == 0 && spare_group != NULL
262 * Look for another array with spare > 0 and active == raid and same spare_group
263 * if found, choose a device and hotremove/hotadd
264 */
265 if (share && anydegraded)
266 try_spare_migration(statelist, &info);
267 if (!new_found) {
268 if (oneshot)
269 break;
270 else if (!anyredundant) {
271 pr_err("No array with redundancy detected, stopping\n");
272 break;
273 }
274 else {
275 #ifndef NO_LIBUDEV
276 /*
277 * Wait for udevd to finish new devices
278 * processing.
279 */
280 if (mdstat_wait(delay_for_event) &&
281 check_udev_activity())
282 pr_err("Error while waiting for UDEV to complete new devices processing\n");
283 #else
284 int wait_result = mdstat_wait(delay_for_event);
285 /*
286 * Give chance to process new device
287 */
288 if (wait_result != 0) {
289 if (c->delay > 5)
290 delay_for_event = 5;
291 } else
292 delay_for_event = c->delay;
293 #endif
294 mdstat_close();
295 }
296 }
297 c->test = 0;
298
299 for (stp = &statelist; (st = *stp) != NULL; ) {
300 if (st->from_auto && st->err > 5) {
301 *stp = st->next;
302 if (st->spare_group)
303 free(st->spare_group);
304
305 free(st);
306 } else
307 stp = &st->next;
308 }
309 }
310
311 free_statelist(statelist);
312
313 if (pidfile)
314 unlink(pidfile);
315 return 0;
316 }
317
318 static int make_daemon(char *pidfile)
319 {
320 /* Return:
321 * -1 in the forked daemon
322 * 0 in the parent
323 * 1 on error
324 * so a none-negative becomes the exit code.
325 */
326 int pid = fork();
327 if (pid > 0) {
328 if (!pidfile)
329 printf("%d\n", pid);
330 else {
331 FILE *pid_file = NULL;
332 int fd = open(pidfile, O_WRONLY | O_CREAT | O_TRUNC,
333 0644);
334 if (fd >= 0)
335 pid_file = fdopen(fd, "w");
336 if (!pid_file)
337 perror("cannot create pid file");
338 else {
339 fprintf(pid_file,"%d\n", pid);
340 fclose(pid_file);
341 }
342 }
343 return 0;
344 }
345 if (pid < 0) {
346 perror("daemonise");
347 return 1;
348 }
349 manage_fork_fds(0);
350 setsid();
351 return -1;
352 }
353
354 static int check_one_sharer(int scan)
355 {
356 int pid;
357 FILE *comm_fp;
358 FILE *fp;
359 char comm_path[PATH_MAX];
360 char path[PATH_MAX];
361 char comm[20];
362
363 sprintf(path, "%s/autorebuild.pid", MDMON_DIR);
364 fp = fopen(path, "r");
365 if (fp) {
366 if (fscanf(fp, "%d", &pid) != 1)
367 pid = -1;
368 snprintf(comm_path, sizeof(comm_path),
369 "/proc/%d/comm", pid);
370 comm_fp = fopen(comm_path, "r");
371 if (comm_fp) {
372 if (fscanf(comm_fp, "%19s", comm) &&
373 strncmp(basename(comm), Name, strlen(Name)) == 0) {
374 if (scan) {
375 pr_err("Only one autorebuild process allowed in scan mode, aborting\n");
376 fclose(comm_fp);
377 fclose(fp);
378 return 1;
379 } else {
380 pr_err("Warning: One autorebuild process already running.\n");
381 }
382 }
383 fclose(comm_fp);
384 }
385 fclose(fp);
386 }
387 return 0;
388 }
389
390 static void write_autorebuild_pid()
391 {
392 char path[PATH_MAX];
393 int pid;
394 FILE *fp = NULL;
395 sprintf(path, "%s/autorebuild.pid", MDMON_DIR);
396
397 if (mkdir(MDMON_DIR, 0700) < 0 && errno != EEXIST) {
398 pr_err("Can't create autorebuild.pid file\n");
399 } else {
400 int fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0700);
401
402 if (fd >= 0)
403 fp = fdopen(fd, "w");
404
405 if (!fp)
406 pr_err("Can't create autorebuild.pid file\n");
407 else {
408 pid = getpid();
409 fprintf(fp, "%d\n", pid);
410 fclose(fp);
411 }
412 }
413 }
414
415 static void execute_alert_cmd(const char *event, const char *dev, const char *disc, struct alert_info *info)
416 {
417 int pid = fork();
418
419 switch (pid) {
420 default:
421 waitpid(pid, NULL, 0);
422 break;
423 case -1:
424 pr_err("Cannot fork to execute alert command");
425 break;
426 case 0:
427 execl(info->alert_cmd, info->alert_cmd, event, dev, disc, NULL);
428 exit(2);
429 }
430 }
431
432 static void send_event_email(const char *event, const char *dev, const char *disc, struct alert_info *info)
433 {
434 FILE *mp, *mdstat;
435 char hname[256];
436 char buf[BUFSIZ];
437 int n;
438
439 mp = popen(Sendmail, "w");
440 if (!mp) {
441 pr_err("Cannot open pipe stream for sendmail.\n");
442 return;
443 }
444
445 gethostname(hname, sizeof(hname));
446 signal(SIGPIPE, SIG_IGN);
447 if (info->mailfrom)
448 fprintf(mp, "From: %s\n", info->mailfrom);
449 else
450 fprintf(mp, "From: %s monitoring <root>\n", Name);
451 fprintf(mp, "To: %s\n", info->mailaddr);
452 fprintf(mp, "Subject: %s event on %s:%s\n\n", event, dev, hname);
453 fprintf(mp, "This is an automatically generated mail message. \n");
454 fprintf(mp, "A %s event had been detected on md device %s.\n\n", event, dev);
455
456 if (disc && disc[0] != ' ')
457 fprintf(mp,
458 "It could be related to component device %s.\n\n", disc);
459 if (disc && disc[0] == ' ')
460 fprintf(mp, "Extra information:%s.\n\n", disc);
461
462 mdstat = fopen("/proc/mdstat", "r");
463 if (!mdstat) {
464 pr_err("Cannot open /proc/mdstat\n");
465 pclose(mp);
466 return;
467 }
468
469 fprintf(mp, "The /proc/mdstat file currently contains the following:\n\n");
470 while ((n = fread(buf, 1, sizeof(buf), mdstat)) > 0)
471 n = fwrite(buf, 1, n, mp);
472 fclose(mdstat);
473 pclose(mp);
474 }
475
476 static void log_event_to_syslog(const char *event, const char *dev, const char *disc)
477 {
478 int priority;
479 /* Log at a different severity depending on the event.
480 *
481 * These are the critical events: */
482 if (strncmp(event, "Fail", 4) == 0 ||
483 strncmp(event, "Degrade", 7) == 0 ||
484 strncmp(event, "DeviceDisappeared", 17) == 0)
485 priority = LOG_CRIT;
486 /* Good to know about, but are not failures: */
487 else if (strncmp(event, "Rebuild", 7) == 0 ||
488 strncmp(event, "MoveSpare", 9) == 0 ||
489 strncmp(event, "Spares", 6) != 0)
490 priority = LOG_WARNING;
491 /* Everything else: */
492 else
493 priority = LOG_INFO;
494
495 if (disc && disc[0] != ' ')
496 syslog(priority,
497 "%s event detected on md device %s, component device %s", event, dev, disc);
498 else if (disc)
499 syslog(priority, "%s event detected on md device %s: %s", event, dev, disc);
500 else
501 syslog(priority, "%s event detected on md device %s", event, dev);
502 }
503
504 static void alert(const char *event, const char *dev, const char *disc, struct alert_info *info)
505 {
506 if (!info->alert_cmd && !info->mailaddr && !info->dosyslog) {
507 time_t now = time(0);
508
509 printf("%1.15s: %s on %s %s\n", ctime(&now) + 4,
510 event, dev, disc?disc:"unknown device");
511 }
512 if (info->alert_cmd)
513 execute_alert_cmd(event, dev, disc, info);
514
515 if (info->mailaddr && (strncmp(event, "Fail", 4) == 0 ||
516 strncmp(event, "Test", 4) == 0 ||
517 strncmp(event, "Spares", 6) == 0 ||
518 strncmp(event, "Degrade", 7) == 0)) {
519 send_event_email(event, dev, disc, info);
520 }
521
522 if (info->dosyslog)
523 log_event_to_syslog(event, dev, disc);
524 }
525
526 static int check_array(struct state *st, struct mdstat_ent *mdstat,
527 int test, struct alert_info *ainfo,
528 int increments, char *prefer)
529 {
530 /* Update the state 'st' to reflect any changes shown in mdstat,
531 * or found by directly examining the array, and return
532 * '1' if the array is degraded, or '0' if it is optimal (or dead).
533 */
534 struct { int state, major, minor; } info[MAX_DISKS];
535 struct mdinfo *sra = NULL;
536 mdu_array_info_t array;
537 struct mdstat_ent *mse = NULL, *mse2;
538 char *dev = st->devname;
539 int fd;
540 int i;
541 int remaining_disks;
542 int last_disk;
543 int new_array = 0;
544 int retval;
545 int is_container = 0;
546 unsigned long redundancy_only_flags = 0;
547
548 if (test)
549 alert("TestMessage", dev, NULL, ainfo);
550
551 retval = 0;
552
553 fd = open(dev, O_RDONLY);
554 if (fd < 0)
555 goto disappeared;
556
557 if (st->devnm[0] == 0)
558 snprintf(st->devnm, MD_NAME_MAX, "%s", fd2devnm(fd));
559
560 for (mse2 = mdstat; mse2; mse2 = mse2->next)
561 if (strcmp(mse2->devnm, st->devnm) == 0) {
562 mse2->devnm[0] = 0; /* flag it as "used" */
563 mse = mse2;
564 }
565
566 if (!mse) {
567 /* duplicated array in statelist
568 * or re-created after reading mdstat
569 */
570 st->err++;
571 goto out;
572 }
573
574 if (mse->level == NULL)
575 is_container = 1;
576
577 if (!is_container && !md_array_active(fd))
578 goto disappeared;
579
580 fcntl(fd, F_SETFD, FD_CLOEXEC);
581 if (md_get_array_info(fd, &array) < 0)
582 goto disappeared;
583
584 if (!is_container && map_name(pers, mse->level) > 0)
585 redundancy_only_flags |= GET_MISMATCH;
586
587 sra = sysfs_read(-1, st->devnm, GET_LEVEL | GET_DISKS | GET_DEVS |
588 GET_STATE | redundancy_only_flags);
589
590 if (!sra)
591 goto disappeared;
592
593 /* It's much easier to list what array levels can't
594 * have a device disappear than all of them that can
595 */
596 if (sra->array.level == 0 || sra->array.level == -1) {
597 if (!st->err && !st->from_config)
598 alert("DeviceDisappeared", dev, " Wrong-Level", ainfo);
599 st->err++;
600 goto out;
601 }
602
603 /* this array is in /proc/mdstat */
604 if (array.utime == 0)
605 /* external arrays don't update utime, so
606 * just make sure it is always different. */
607 array.utime = st->utime + 1;;
608
609 if (st->err) {
610 /* New array appeared where previously had an error */
611 st->err = 0;
612 st->percent = RESYNC_NONE;
613 new_array = 1;
614 if (!is_container)
615 alert("NewArray", st->devname, NULL, ainfo);
616 }
617
618 if (st->utime == array.utime && st->failed == sra->array.failed_disks &&
619 st->working == sra->array.working_disks &&
620 st->spare == sra->array.spare_disks &&
621 (mse == NULL || (mse->percent == st->percent))) {
622 if ((st->active < st->raid) && st->spare == 0)
623 retval = 1;
624 goto out;
625 }
626 if (st->utime == 0 && /* new array */
627 mse->pattern && strchr(mse->pattern, '_') /* degraded */)
628 alert("DegradedArray", dev, NULL, ainfo);
629
630 if (st->utime == 0 && /* new array */ st->expected_spares > 0 &&
631 sra->array.spare_disks < st->expected_spares)
632 alert("SparesMissing", dev, NULL, ainfo);
633 if (st->percent < 0 && st->percent != RESYNC_UNKNOWN &&
634 mse->percent >= 0)
635 alert("RebuildStarted", dev, NULL, ainfo);
636 if (st->percent >= 0 && mse->percent >= 0 &&
637 (mse->percent / increments) > (st->percent / increments)) {
638 char percentalert[18];
639 /*
640 * "RebuildNN" (10 chars) or "RebuildStarted" (15 chars)
641 */
642
643 if((mse->percent / increments) == 0)
644 snprintf(percentalert, sizeof(percentalert),
645 "RebuildStarted");
646 else
647 snprintf(percentalert, sizeof(percentalert),
648 "Rebuild%02d", mse->percent);
649
650 alert(percentalert, dev, NULL, ainfo);
651 }
652
653 if (mse->percent == RESYNC_NONE && st->percent >= 0) {
654 /* Rebuild/sync/whatever just finished.
655 * If there is a number in /mismatch_cnt,
656 * we should report that.
657 */
658 if (sra && sra->mismatch_cnt > 0) {
659 char cnt[80];
660 snprintf(cnt, sizeof(cnt),
661 " mismatches found: %d (on raid level %d)",
662 sra->mismatch_cnt, sra->array.level);
663 alert("RebuildFinished", dev, cnt, ainfo);
664 } else
665 alert("RebuildFinished", dev, NULL, ainfo);
666 }
667 st->percent = mse->percent;
668
669 remaining_disks = sra->array.nr_disks;
670 for (i = 0; i < MAX_DISKS && remaining_disks > 0; i++) {
671 mdu_disk_info_t disc;
672 disc.number = i;
673 if (md_get_disk_info(fd, &disc) >= 0) {
674 info[i].state = disc.state;
675 info[i].major = disc.major;
676 info[i].minor = disc.minor;
677 if (disc.major || disc.minor)
678 remaining_disks --;
679 } else
680 info[i].major = info[i].minor = 0;
681 }
682 last_disk = i;
683
684 if (mse->metadata_version &&
685 strncmp(mse->metadata_version, "external:", 9) == 0 &&
686 is_subarray(mse->metadata_version+9)) {
687 char *sl;
688 snprintf(st->parent_devnm, MD_NAME_MAX, "%s", mse->metadata_version + 10);
689 sl = strchr(st->parent_devnm, '/');
690 if (sl)
691 *sl = 0;
692 } else
693 st->parent_devnm[0] = 0;
694 if (st->metadata == NULL && st->parent_devnm[0] == 0)
695 st->metadata = super_by_fd(fd, NULL);
696
697 for (i = 0; i < MAX_DISKS; i++) {
698 mdu_disk_info_t disc = {0, 0, 0, 0, 0};
699 int newstate = 0;
700 int change;
701 char *dv = NULL;
702 disc.number = i;
703 if (i < last_disk && (info[i].major || info[i].minor)) {
704 newstate = info[i].state;
705 dv = map_dev_preferred(info[i].major, info[i].minor, 1,
706 prefer);
707 disc.state = newstate;
708 disc.major = info[i].major;
709 disc.minor = info[i].minor;
710 } else
711 newstate = (1 << MD_DISK_REMOVED);
712
713 if (dv == NULL && st->devid[i])
714 dv = map_dev_preferred(major(st->devid[i]),
715 minor(st->devid[i]), 1, prefer);
716 change = newstate ^ st->devstate[i];
717 if (st->utime && change && !st->err && !new_array) {
718 if ((st->devstate[i]&change) & (1 << MD_DISK_SYNC))
719 alert("Fail", dev, dv, ainfo);
720 else if ((newstate & (1 << MD_DISK_FAULTY)) &&
721 (disc.major || disc.minor) &&
722 st->devid[i] == makedev(disc.major,
723 disc.minor))
724 alert("FailSpare", dev, dv, ainfo);
725 else if ((newstate&change) & (1 << MD_DISK_SYNC))
726 alert("SpareActive", dev, dv, ainfo);
727 }
728 st->devstate[i] = newstate;
729 st->devid[i] = makedev(disc.major, disc.minor);
730 }
731 st->active = sra->array.active_disks;
732 st->working = sra->array.working_disks;
733 st->spare = sra->array.spare_disks;
734 st->failed = sra->array.failed_disks;
735 st->utime = array.utime;
736 st->raid = sra->array.raid_disks;
737 st->err = 0;
738 if ((st->active < st->raid) && st->spare == 0)
739 retval = 1;
740
741 out:
742 if (sra)
743 sysfs_free(sra);
744 if (fd >= 0)
745 close(fd);
746 return retval;
747
748 disappeared:
749 if (!st->err && !is_container)
750 alert("DeviceDisappeared", dev, NULL, ainfo);
751 st->err++;
752 goto out;
753 }
754
755 static int add_new_arrays(struct mdstat_ent *mdstat, struct state **statelist,
756 int test, struct alert_info *info)
757 {
758 struct mdstat_ent *mse;
759 int new_found = 0;
760 char *name;
761
762 for (mse = mdstat; mse; mse = mse->next)
763 if (mse->devnm[0] && (!mse->level || /* retrieve containers */
764 (strcmp(mse->level, "raid0") != 0 &&
765 strcmp(mse->level, "linear") != 0))) {
766 struct state *st = xcalloc(1, sizeof *st);
767 mdu_array_info_t array;
768 int fd;
769
770 name = get_md_name(mse->devnm);
771 if (!name) {
772 free(st);
773 continue;
774 }
775
776 snprintf(st->devname, MD_NAME_MAX + sizeof("/dev/md/"), "%s", name);
777 if ((fd = open(st->devname, O_RDONLY)) < 0 ||
778 md_get_array_info(fd, &array) < 0) {
779 /* no such array */
780 if (fd >= 0)
781 close(fd);
782 put_md_name(st->devname);
783 if (st->metadata) {
784 st->metadata->ss->free_super(st->metadata);
785 free(st->metadata);
786 }
787 free(st);
788 continue;
789 }
790 close(fd);
791 st->next = *statelist;
792 st->err = 1;
793 st->from_auto = 1;
794 snprintf(st->devnm, MD_NAME_MAX, "%s", mse->devnm);
795 st->percent = RESYNC_UNKNOWN;
796 st->expected_spares = -1;
797 if (mse->metadata_version &&
798 strncmp(mse->metadata_version,
799 "external:", 9) == 0 &&
800 is_subarray(mse->metadata_version+9)) {
801 char *sl;
802 snprintf(st->parent_devnm, MD_NAME_MAX,
803 "%s", mse->metadata_version + 10);
804 sl = strchr(st->parent_devnm, '/');
805 *sl = 0;
806 } else
807 st->parent_devnm[0] = 0;
808 *statelist = st;
809 if (test)
810 alert("TestMessage", st->devname, NULL, info);
811 new_found = 1;
812 }
813 return new_found;
814 }
815
816 static int get_required_spare_criteria(struct state *st,
817 struct spare_criteria *sc)
818 {
819 int fd;
820
821 if (!st->metadata || !st->metadata->ss->get_spare_criteria) {
822 sc->min_size = 0;
823 sc->sector_size = 0;
824 return 0;
825 }
826
827 fd = open(st->devname, O_RDONLY);
828 if (fd < 0)
829 return 1;
830 if (st->metadata->ss->external)
831 st->metadata->ss->load_container(st->metadata, fd, st->devname);
832 else
833 st->metadata->ss->load_super(st->metadata, fd, st->devname);
834 close(fd);
835 if (!st->metadata->sb)
836 return 1;
837
838 st->metadata->ss->get_spare_criteria(st->metadata, sc);
839 st->metadata->ss->free_super(st->metadata);
840
841 return 0;
842 }
843
844 static int check_donor(struct state *from, struct state *to)
845 {
846 struct state *sub;
847
848 if (from == to)
849 return 0;
850 if (from->parent)
851 /* Cannot move from a member */
852 return 0;
853 if (from->err)
854 return 0;
855 for (sub = from->subarray; sub; sub = sub->subarray)
856 /* If source array has degraded subarrays, don't
857 * remove anything
858 */
859 if (sub->active < sub->raid)
860 return 0;
861 if (from->metadata->ss->external == 0)
862 if (from->active < from->raid)
863 return 0;
864 if (from->spare <= 0)
865 return 0;
866 return 1;
867 }
868
869 static dev_t choose_spare(struct state *from, struct state *to,
870 struct domainlist *domlist, struct spare_criteria *sc)
871 {
872 int d;
873 dev_t dev = 0;
874
875 for (d = from->raid; !dev && d < MAX_DISKS; d++) {
876 if (from->devid[d] > 0 && from->devstate[d] == 0) {
877 struct dev_policy *pol;
878 unsigned long long dev_size;
879 unsigned int dev_sector_size;
880
881 if (to->metadata->ss->external &&
882 test_partition_from_id(from->devid[d]))
883 continue;
884
885 if (sc->min_size &&
886 dev_size_from_id(from->devid[d], &dev_size) &&
887 dev_size < sc->min_size)
888 continue;
889
890 if (sc->sector_size &&
891 dev_sector_size_from_id(from->devid[d],
892 &dev_sector_size) &&
893 sc->sector_size != dev_sector_size)
894 continue;
895
896 pol = devid_policy(from->devid[d]);
897 if (from->spare_group)
898 pol_add(&pol, pol_domain,
899 from->spare_group, NULL);
900 if (domain_test(domlist, pol,
901 to->metadata->ss->name) == 1)
902 dev = from->devid[d];
903 dev_policy_free(pol);
904 }
905 }
906 return dev;
907 }
908
909 static dev_t container_choose_spare(struct state *from, struct state *to,
910 struct domainlist *domlist,
911 struct spare_criteria *sc, int active)
912 {
913 /* This is similar to choose_spare, but we cannot trust devstate,
914 * so we need to read the metadata instead
915 */
916 struct mdinfo *list;
917 struct supertype *st = from->metadata;
918 int fd = open(from->devname, O_RDONLY);
919 int err;
920 dev_t dev = 0;
921
922 if (fd < 0)
923 return 0;
924 if (!st->ss->getinfo_super_disks) {
925 close(fd);
926 return 0;
927 }
928
929 err = st->ss->load_container(st, fd, NULL);
930 close(fd);
931 if (err)
932 return 0;
933
934 if (from == to) {
935 /* We must check if number of active disks has not increased
936 * since ioctl in main loop. mdmon may have added spare
937 * to subarray. If so we do not need to look for more spares
938 * so return non zero value */
939 int active_cnt = 0;
940 struct mdinfo *dp;
941 list = st->ss->getinfo_super_disks(st);
942 if (!list) {
943 st->ss->free_super(st);
944 return 1;
945 }
946 dp = list->devs;
947 while (dp) {
948 if (dp->disk.state & (1 << MD_DISK_SYNC) &&
949 !(dp->disk.state & (1 << MD_DISK_FAULTY)))
950 active_cnt++;
951 dp = dp->next;
952 }
953 sysfs_free(list);
954 if (active < active_cnt) {
955 /* Spare just activated.*/
956 st->ss->free_super(st);
957 return 1;
958 }
959 }
960
961 /* We only need one spare so full list not needed */
962 list = container_choose_spares(st, sc, domlist, from->spare_group,
963 to->metadata->ss->name, 1);
964 if (list) {
965 struct mdinfo *disks = list->devs;
966 if (disks)
967 dev = makedev(disks->disk.major, disks->disk.minor);
968 sysfs_free(list);
969 }
970 st->ss->free_super(st);
971 return dev;
972 }
973
974 static void try_spare_migration(struct state *statelist, struct alert_info *info)
975 {
976 struct state *from;
977 struct state *st;
978 struct spare_criteria sc;
979
980 link_containers_with_subarrays(statelist);
981 for (st = statelist; st; st = st->next)
982 if (st->active < st->raid && st->spare == 0 && !st->err) {
983 struct domainlist *domlist = NULL;
984 int d;
985 struct state *to = st;
986
987 if (to->parent_devnm[0] && !to->parent)
988 /* subarray monitored without parent container
989 * we can't move spares here */
990 continue;
991
992 if (to->parent)
993 /* member of a container */
994 to = to->parent;
995
996 if (get_required_spare_criteria(to, &sc))
997 continue;
998 if (to->metadata->ss->external) {
999 /* We must make sure there is
1000 * no suitable spare in container already.
1001 * If there is we don't add more */
1002 dev_t devid = container_choose_spare(
1003 to, to, NULL, &sc, st->active);
1004 if (devid > 0)
1005 continue;
1006 }
1007 for (d = 0; d < MAX_DISKS; d++)
1008 if (to->devid[d])
1009 domainlist_add_dev(&domlist,
1010 to->devid[d],
1011 to->metadata->ss->name);
1012 if (to->spare_group)
1013 domain_add(&domlist, to->spare_group);
1014 /*
1015 * No spare migration if the destination
1016 * has no domain. Skip this array.
1017 */
1018 if (!domlist)
1019 continue;
1020 for (from=statelist ; from ; from=from->next) {
1021 dev_t devid;
1022 if (!check_donor(from, to))
1023 continue;
1024 if (from->metadata->ss->external)
1025 devid = container_choose_spare(
1026 from, to, domlist, &sc, 0);
1027 else
1028 devid = choose_spare(from, to, domlist,
1029 &sc);
1030 if (devid > 0 &&
1031 move_spare(from->devname, to->devname,
1032 devid)) {
1033 alert("MoveSpare", to->devname,
1034 from->devname, info);
1035 break;
1036 }
1037 }
1038 domain_free(domlist);
1039 }
1040 }
1041
1042 /* search the statelist to connect external
1043 * metadata subarrays with their containers
1044 * We always completely rebuild the tree from scratch as
1045 * that is safest considering the possibility of entries
1046 * disappearing or changing.
1047 */
1048 static void link_containers_with_subarrays(struct state *list)
1049 {
1050 struct state *st;
1051 struct state *cont;
1052 for (st = list; st; st = st->next) {
1053 st->parent = NULL;
1054 st->subarray = NULL;
1055 }
1056 for (st = list; st; st = st->next)
1057 if (st->parent_devnm[0])
1058 for (cont = list; cont; cont = cont->next)
1059 if (!cont->err && cont->parent_devnm[0] == 0 &&
1060 strcmp(cont->devnm, st->parent_devnm) == 0) {
1061 st->parent = cont;
1062 st->subarray = cont->subarray;
1063 cont->subarray = st;
1064 break;
1065 }
1066 }
1067
1068 /**
1069 * free_statelist() - Frees statelist.
1070 * @statelist: statelist to free
1071 */
1072 static void free_statelist(struct state *statelist)
1073 {
1074 struct state *tmp = NULL;
1075
1076 while (statelist) {
1077 if (statelist->spare_group)
1078 free(statelist->spare_group);
1079
1080 tmp = statelist;
1081 statelist = statelist->next;
1082 free(tmp);
1083 }
1084 }
1085
1086 #ifndef NO_LIBUDEV
1087 /* function: check_udev_activity
1088 * Description: Function waits for udev to finish
1089 * events processing.
1090 * Returns:
1091 * 1 - detected error while opening udev
1092 * 2 - timeout
1093 * 0 - successfull completion
1094 */
1095 static int check_udev_activity(void)
1096 {
1097 struct udev *udev = NULL;
1098 struct udev_queue *udev_queue = NULL;
1099 int timeout_cnt = 30;
1100 int rc = 0;
1101
1102 /*
1103 * In rare cases systemd may not have udevm,
1104 * in such cases just exit with rc 0
1105 */
1106 if (!use_udev())
1107 goto out;
1108
1109 udev = udev_new();
1110 if (!udev) {
1111 rc = 1;
1112 goto out;
1113 }
1114
1115 udev_queue = udev_queue_new(udev);
1116 if (!udev_queue) {
1117 rc = 1;
1118 goto out;
1119 }
1120
1121 if (udev_queue_get_queue_is_empty(udev_queue))
1122 goto out;
1123
1124 while (!udev_queue_get_queue_is_empty(udev_queue)) {
1125 sleep(1);
1126
1127 if (timeout_cnt)
1128 timeout_cnt--;
1129 else {
1130 rc = 2;
1131 goto out;
1132 }
1133 }
1134
1135 out:
1136 if (udev_queue)
1137 udev_queue_unref(udev_queue);
1138 if (udev)
1139 udev_unref(udev);
1140 return rc;
1141 }
1142 #endif
1143
1144 /* Not really Monitor but ... */
1145 int Wait(char *dev)
1146 {
1147 char devnm[32];
1148 dev_t rdev;
1149 char *tmp;
1150 int rv = 1;
1151 int frozen_remaining = 3;
1152
1153 if (!stat_is_blkdev(dev, &rdev))
1154 return 2;
1155
1156 tmp = devid2devnm(rdev);
1157 if (!tmp) {
1158 pr_err("Cannot get md device name.\n");
1159 return 2;
1160 }
1161
1162 strcpy(devnm, tmp);
1163
1164 while(1) {
1165 struct mdstat_ent *ms = mdstat_read(1, 0);
1166 struct mdstat_ent *e;
1167
1168 for (e = ms; e; e = e->next)
1169 if (strcmp(e->devnm, devnm) == 0)
1170 break;
1171
1172 if (e && e->percent == RESYNC_NONE) {
1173 /* We could be in the brief pause before something
1174 * starts. /proc/mdstat doesn't show that, but
1175 * sync_action does.
1176 */
1177 struct mdinfo mdi;
1178 char buf[21];
1179
1180 if (sysfs_init(&mdi, -1, devnm))
1181 return 2;
1182 if (sysfs_get_str(&mdi, NULL, "sync_action",
1183 buf, 20) > 0 &&
1184 strcmp(buf,"idle\n") != 0) {
1185 e->percent = RESYNC_UNKNOWN;
1186 if (strcmp(buf, "frozen\n") == 0) {
1187 if (frozen_remaining == 0)
1188 e->percent = RESYNC_NONE;
1189 else
1190 frozen_remaining -= 1;
1191 }
1192 }
1193 }
1194 if (!e || e->percent == RESYNC_NONE) {
1195 if (e && e->metadata_version &&
1196 strncmp(e->metadata_version, "external:", 9) == 0) {
1197 if (is_subarray(&e->metadata_version[9]))
1198 ping_monitor(&e->metadata_version[9]);
1199 else
1200 ping_monitor(devnm);
1201 }
1202 free_mdstat(ms);
1203 return rv;
1204 }
1205 free_mdstat(ms);
1206 rv = 0;
1207 mdstat_wait(5);
1208 }
1209 }
1210
1211 /* The state "broken" is used only for RAID0/LINEAR - it's the same as
1212 * "clean", but used in case the array has one or more members missing.
1213 */
1214 static char *clean_states[] = {
1215 "clear", "inactive", "readonly", "read-auto", "clean", "broken", NULL };
1216
1217 int WaitClean(char *dev, int verbose)
1218 {
1219 int fd;
1220 struct mdinfo *mdi;
1221 int rv = 1;
1222 char devnm[32];
1223
1224 if (!stat_is_blkdev(dev, NULL))
1225 return 2;
1226 fd = open(dev, O_RDONLY);
1227 if (fd < 0) {
1228 if (verbose)
1229 pr_err("Couldn't open %s: %s\n", dev, strerror(errno));
1230 return 1;
1231 }
1232
1233 strcpy(devnm, fd2devnm(fd));
1234 mdi = sysfs_read(fd, devnm, GET_VERSION|GET_LEVEL|GET_SAFEMODE);
1235 if (!mdi) {
1236 if (verbose)
1237 pr_err("Failed to read sysfs attributes for %s\n", dev);
1238 close(fd);
1239 return 0;
1240 }
1241
1242 switch(mdi->array.level) {
1243 case LEVEL_LINEAR:
1244 case LEVEL_MULTIPATH:
1245 case 0:
1246 /* safemode delay is irrelevant for these levels */
1247 rv = 0;
1248 }
1249
1250 /* for internal metadata the kernel handles the final clean
1251 * transition, containers can never be dirty
1252 */
1253 if (!is_subarray(mdi->text_version))
1254 rv = 0;
1255
1256 /* safemode disabled ? */
1257 if (mdi->safe_mode_delay == 0)
1258 rv = 0;
1259
1260 if (rv) {
1261 int state_fd = sysfs_open(fd2devnm(fd), NULL, "array_state");
1262 char buf[20];
1263 int delay = 5000;
1264
1265 /* minimize the safe_mode_delay and prepare to wait up to 5s
1266 * for writes to quiesce
1267 */
1268 sysfs_set_safemode(mdi, 1);
1269
1270 /* wait for array_state to be clean */
1271 while (1) {
1272 rv = read(state_fd, buf, sizeof(buf));
1273 if (rv < 0)
1274 break;
1275 if (sysfs_match_word(buf, clean_states) <
1276 (int)ARRAY_SIZE(clean_states) - 1)
1277 break;
1278 rv = sysfs_wait(state_fd, &delay);
1279 if (rv < 0 && errno != EINTR)
1280 break;
1281 lseek(state_fd, 0, SEEK_SET);
1282 }
1283 if (rv < 0)
1284 rv = 1;
1285 else if (ping_monitor(mdi->text_version) == 0) {
1286 /* we need to ping to close the window between array
1287 * state transitioning to clean and the metadata being
1288 * marked clean
1289 */
1290 rv = 0;
1291 } else {
1292 rv = 1;
1293 pr_err("Error connecting monitor with %s\n", dev);
1294 }
1295 if (rv && verbose)
1296 pr_err("Error waiting for %s to be clean\n", dev);
1297
1298 /* restore the original safe_mode_delay */
1299 sysfs_set_safemode(mdi, mdi->safe_mode_delay);
1300 close(state_fd);
1301 }
1302
1303 sysfs_free(mdi);
1304 close(fd);
1305
1306 return rv;
1307 }