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