]> git.ipfire.org Git - thirdparty/mdadm.git/blob - Monitor.c
Monitor: use devname as char array instead of pointer
[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;
37 char devnm[32]; /* 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[32]; /* 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 if (mdlist->devname[0] == '/')
191 st->devname = xstrdup(mdlist->devname);
192 else {
193 /* length of "/dev/md/" + device name + terminating byte */
194 size_t _len = sizeof("/dev/md/") + strnlen(mdlist->devname, PATH_MAX);
195
196 st->devname = xcalloc(_len, sizeof(char));
197 snprintf(st->devname, _len, "/dev/md/%s", mdlist->devname);
198 }
199 if (!is_mddev(mdlist->devname))
200 return 1;
201 st->next = statelist;
202 st->devnm[0] = 0;
203 st->percent = RESYNC_UNKNOWN;
204 st->from_config = 1;
205 st->expected_spares = mdlist->spare_disks;
206 if (mdlist->spare_group)
207 st->spare_group = xstrdup(mdlist->spare_group);
208 statelist = st;
209 }
210 } else {
211 struct mddev_dev *dv;
212
213 for (dv = devlist; dv; dv = dv->next) {
214 struct state *st;
215
216 if (!is_mddev(dv->devname))
217 return 1;
218
219 st = xcalloc(1, sizeof *st);
220 mdlist = conf_get_ident(dv->devname);
221 st->devname = xstrdup(dv->devname);
222 st->next = statelist;
223 st->devnm[0] = 0;
224 st->percent = RESYNC_UNKNOWN;
225 st->expected_spares = -1;
226 if (mdlist) {
227 st->expected_spares = mdlist->spare_disks;
228 if (mdlist->spare_group)
229 st->spare_group = xstrdup(mdlist->spare_group);
230 }
231 statelist = st;
232 }
233 }
234
235 while (!finished) {
236 int new_found = 0;
237 struct state *st, **stp;
238 int anydegraded = 0;
239 int anyredundant = 0;
240
241 if (mdstat)
242 free_mdstat(mdstat);
243 mdstat = mdstat_read(oneshot ? 0 : 1, 0);
244
245 for (st = statelist; st; st = st->next) {
246 if (check_array(st, mdstat, c->test, &info,
247 increments, c->prefer))
248 anydegraded = 1;
249 /* for external arrays, metadata is filled for
250 * containers only
251 */
252 if (st->metadata && st->metadata->ss->external)
253 continue;
254 if (st->err == 0 && !anyredundant)
255 anyredundant = 1;
256 }
257
258 /* now check if there are any new devices found in mdstat */
259 if (c->scan)
260 new_found = add_new_arrays(mdstat, &statelist, c->test,
261 &info);
262
263 /* If an array has active < raid && spare == 0 && spare_group != NULL
264 * Look for another array with spare > 0 and active == raid and same spare_group
265 * if found, choose a device and hotremove/hotadd
266 */
267 if (share && anydegraded)
268 try_spare_migration(statelist, &info);
269 if (!new_found) {
270 if (oneshot)
271 break;
272 else if (!anyredundant) {
273 pr_err("No array with redundancy detected, stopping\n");
274 break;
275 }
276 else {
277 #ifndef NO_LIBUDEV
278 /*
279 * Wait for udevd to finish new devices
280 * processing.
281 */
282 if (mdstat_wait(delay_for_event) &&
283 check_udev_activity())
284 pr_err("Error while waiting for UDEV to complete new devices processing\n");
285 #else
286 int wait_result = mdstat_wait(delay_for_event);
287 /*
288 * Give chance to process new device
289 */
290 if (wait_result != 0) {
291 if (c->delay > 5)
292 delay_for_event = 5;
293 } else
294 delay_for_event = c->delay;
295 #endif
296 mdstat_close();
297 }
298 }
299 c->test = 0;
300
301 for (stp = &statelist; (st = *stp) != NULL; ) {
302 if (st->from_auto && st->err > 5) {
303 *stp = st->next;
304 free(st->devname);
305 free(st->spare_group);
306 free(st);
307 } else
308 stp = &st->next;
309 }
310 }
311 for (st2 = statelist; st2; st2 = statelist) {
312 statelist = st2->next;
313 free(st2);
314 }
315
316 if (pidfile)
317 unlink(pidfile);
318 return 0;
319 }
320
321 static int make_daemon(char *pidfile)
322 {
323 /* Return:
324 * -1 in the forked daemon
325 * 0 in the parent
326 * 1 on error
327 * so a none-negative becomes the exit code.
328 */
329 int pid = fork();
330 if (pid > 0) {
331 if (!pidfile)
332 printf("%d\n", pid);
333 else {
334 FILE *pid_file = NULL;
335 int fd = open(pidfile, O_WRONLY | O_CREAT | O_TRUNC,
336 0644);
337 if (fd >= 0)
338 pid_file = fdopen(fd, "w");
339 if (!pid_file)
340 perror("cannot create pid file");
341 else {
342 fprintf(pid_file,"%d\n", pid);
343 fclose(pid_file);
344 }
345 }
346 return 0;
347 }
348 if (pid < 0) {
349 perror("daemonise");
350 return 1;
351 }
352 manage_fork_fds(0);
353 setsid();
354 return -1;
355 }
356
357 static int check_one_sharer(int scan)
358 {
359 int pid;
360 FILE *comm_fp;
361 FILE *fp;
362 char comm_path[PATH_MAX];
363 char path[PATH_MAX];
364 char comm[20];
365
366 sprintf(path, "%s/autorebuild.pid", MDMON_DIR);
367 fp = fopen(path, "r");
368 if (fp) {
369 if (fscanf(fp, "%d", &pid) != 1)
370 pid = -1;
371 snprintf(comm_path, sizeof(comm_path),
372 "/proc/%d/comm", pid);
373 comm_fp = fopen(comm_path, "r");
374 if (comm_fp) {
375 if (fscanf(comm_fp, "%19s", comm) &&
376 strncmp(basename(comm), Name, strlen(Name)) == 0) {
377 if (scan) {
378 pr_err("Only one autorebuild process allowed in scan mode, aborting\n");
379 fclose(comm_fp);
380 fclose(fp);
381 return 1;
382 } else {
383 pr_err("Warning: One autorebuild process already running.\n");
384 }
385 }
386 fclose(comm_fp);
387 }
388 fclose(fp);
389 }
390 return 0;
391 }
392
393 static void write_autorebuild_pid()
394 {
395 char path[PATH_MAX];
396 int pid;
397 FILE *fp = NULL;
398 sprintf(path, "%s/autorebuild.pid", MDMON_DIR);
399
400 if (mkdir(MDMON_DIR, 0700) < 0 && errno != EEXIST) {
401 pr_err("Can't create autorebuild.pid file\n");
402 } else {
403 int fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0700);
404
405 if (fd >= 0)
406 fp = fdopen(fd, "w");
407
408 if (!fp)
409 pr_err("Can't create autorebuild.pid file\n");
410 else {
411 pid = getpid();
412 fprintf(fp, "%d\n", pid);
413 fclose(fp);
414 }
415 }
416 }
417
418 static void alert(char *event, char *dev, char *disc, struct alert_info *info)
419 {
420 int priority;
421
422 if (!info->alert_cmd && !info->mailaddr && !info->dosyslog) {
423 time_t now = time(0);
424
425 printf("%1.15s: %s on %s %s\n", ctime(&now) + 4,
426 event, dev, disc?disc:"unknown device");
427 }
428 if (info->alert_cmd) {
429 int pid = fork();
430 switch(pid) {
431 default:
432 waitpid(pid, NULL, 0);
433 break;
434 case -1:
435 break;
436 case 0:
437 execl(info->alert_cmd, info->alert_cmd,
438 event, dev, disc, NULL);
439 exit(2);
440 }
441 }
442 if (info->mailaddr && (strncmp(event, "Fail", 4) == 0 ||
443 strncmp(event, "Test", 4) == 0 ||
444 strncmp(event, "Spares", 6) == 0 ||
445 strncmp(event, "Degrade", 7) == 0)) {
446 FILE *mp = popen(Sendmail, "w");
447 if (mp) {
448 FILE *mdstat;
449 char hname[256];
450
451 gethostname(hname, sizeof(hname));
452 signal_s(SIGPIPE, SIG_IGN);
453
454 if (info->mailfrom)
455 fprintf(mp, "From: %s\n", info->mailfrom);
456 else
457 fprintf(mp, "From: %s monitoring <root>\n",
458 Name);
459 fprintf(mp, "To: %s\n", info->mailaddr);
460 fprintf(mp, "Subject: %s event on %s:%s\n\n",
461 event, dev, hname);
462
463 fprintf(mp,
464 "This is an automatically generated mail message from %s\n", Name);
465 fprintf(mp, "running on %s\n\n", hname);
466
467 fprintf(mp,
468 "A %s event had been detected on md device %s.\n\n", event, dev);
469
470 if (disc && disc[0] != ' ')
471 fprintf(mp,
472 "It could be related to component device %s.\n\n", disc);
473 if (disc && disc[0] == ' ')
474 fprintf(mp, "Extra information:%s.\n\n", disc);
475
476 fprintf(mp, "Faithfully yours, etc.\n");
477
478 mdstat = fopen("/proc/mdstat", "r");
479 if (mdstat) {
480 char buf[8192];
481 int n;
482 fprintf(mp,
483 "\nP.S. The /proc/mdstat file currently contains the following:\n\n");
484 while ((n = fread(buf, 1, sizeof(buf),
485 mdstat)) > 0)
486 n = fwrite(buf, 1, n, mp);
487 fclose(mdstat);
488 }
489 pclose(mp);
490 }
491 }
492
493 /* log the event to syslog maybe */
494 if (info->dosyslog) {
495 /* Log at a different severity depending on the event.
496 *
497 * These are the critical events: */
498 if (strncmp(event, "Fail", 4) == 0 ||
499 strncmp(event, "Degrade", 7) == 0 ||
500 strncmp(event, "DeviceDisappeared", 17) == 0)
501 priority = LOG_CRIT;
502 /* Good to know about, but are not failures: */
503 else if (strncmp(event, "Rebuild", 7) == 0 ||
504 strncmp(event, "MoveSpare", 9) == 0 ||
505 strncmp(event, "Spares", 6) != 0)
506 priority = LOG_WARNING;
507 /* Everything else: */
508 else
509 priority = LOG_INFO;
510
511 if (disc && disc[0] != ' ')
512 syslog(priority,
513 "%s event detected on md device %s, component device %s", event, dev, disc);
514 else if (disc)
515 syslog(priority,
516 "%s event detected on md device %s: %s",
517 event, dev, disc);
518 else
519 syslog(priority,
520 "%s event detected on md device %s",
521 event, dev);
522 }
523 }
524
525 static int check_array(struct state *st, struct mdstat_ent *mdstat,
526 int test, struct alert_info *ainfo,
527 int increments, char *prefer)
528 {
529 /* Update the state 'st' to reflect any changes shown in mdstat,
530 * or found by directly examining the array, and return
531 * '1' if the array is degraded, or '0' if it is optimal (or dead).
532 */
533 struct { int state, major, minor; } info[MAX_DISKS];
534 struct mdinfo *sra = NULL;
535 mdu_array_info_t array;
536 struct mdstat_ent *mse = NULL, *mse2;
537 char *dev = st->devname;
538 int fd;
539 int i;
540 int remaining_disks;
541 int last_disk;
542 int new_array = 0;
543 int retval;
544 int is_container = 0;
545 unsigned long redundancy_only_flags = 0;
546
547 if (test)
548 alert("TestMessage", dev, NULL, ainfo);
549
550 retval = 0;
551
552 fd = open(dev, O_RDONLY);
553 if (fd < 0)
554 goto disappeared;
555
556 if (st->devnm[0] == 0)
557 strcpy(st->devnm, fd2devnm(fd));
558
559 for (mse2 = mdstat; mse2; mse2 = mse2->next)
560 if (strcmp(mse2->devnm, st->devnm) == 0) {
561 mse2->devnm[0] = 0; /* flag it as "used" */
562 mse = mse2;
563 }
564
565 if (!mse) {
566 /* duplicated array in statelist
567 * or re-created after reading mdstat
568 */
569 st->err++;
570 goto out;
571 }
572
573 if (mse->level == NULL)
574 is_container = 1;
575
576 if (!is_container && !md_array_active(fd))
577 goto disappeared;
578
579 fcntl(fd, F_SETFD, FD_CLOEXEC);
580 if (md_get_array_info(fd, &array) < 0)
581 goto disappeared;
582
583 if (!is_container && map_name(pers, mse->level) > 0)
584 redundancy_only_flags |= GET_MISMATCH;
585
586 sra = sysfs_read(-1, st->devnm, GET_LEVEL | GET_DISKS | GET_DEVS |
587 GET_STATE | redundancy_only_flags);
588
589 if (!sra)
590 goto disappeared;
591
592 /* It's much easier to list what array levels can't
593 * have a device disappear than all of them that can
594 */
595 if (sra->array.level == 0 || sra->array.level == -1) {
596 if (!st->err && !st->from_config)
597 alert("DeviceDisappeared", dev, " Wrong-Level", ainfo);
598 st->err++;
599 goto out;
600 }
601
602 /* this array is in /proc/mdstat */
603 if (array.utime == 0)
604 /* external arrays don't update utime, so
605 * just make sure it is always different. */
606 array.utime = st->utime + 1;;
607
608 if (st->err) {
609 /* New array appeared where previously had an error */
610 st->err = 0;
611 st->percent = RESYNC_NONE;
612 new_array = 1;
613 if (!is_container)
614 alert("NewArray", st->devname, NULL, ainfo);
615 }
616
617 if (st->utime == array.utime && st->failed == sra->array.failed_disks &&
618 st->working == sra->array.working_disks &&
619 st->spare == sra->array.spare_disks &&
620 (mse == NULL || (mse->percent == st->percent))) {
621 if ((st->active < st->raid) && st->spare == 0)
622 retval = 1;
623 goto out;
624 }
625 if (st->utime == 0 && /* new array */
626 mse->pattern && strchr(mse->pattern, '_') /* degraded */)
627 alert("DegradedArray", dev, NULL, ainfo);
628
629 if (st->utime == 0 && /* new array */ st->expected_spares > 0 &&
630 sra->array.spare_disks < st->expected_spares)
631 alert("SparesMissing", dev, NULL, ainfo);
632 if (st->percent < 0 && st->percent != RESYNC_UNKNOWN &&
633 mse->percent >= 0)
634 alert("RebuildStarted", dev, NULL, ainfo);
635 if (st->percent >= 0 && mse->percent >= 0 &&
636 (mse->percent / increments) > (st->percent / increments)) {
637 char percentalert[18];
638 /*
639 * "RebuildNN" (10 chars) or "RebuildStarted" (15 chars)
640 */
641
642 if((mse->percent / increments) == 0)
643 snprintf(percentalert, sizeof(percentalert),
644 "RebuildStarted");
645 else
646 snprintf(percentalert, sizeof(percentalert),
647 "Rebuild%02d", mse->percent);
648
649 alert(percentalert, dev, NULL, ainfo);
650 }
651
652 if (mse->percent == RESYNC_NONE && st->percent >= 0) {
653 /* Rebuild/sync/whatever just finished.
654 * If there is a number in /mismatch_cnt,
655 * we should report that.
656 */
657 if (sra && sra->mismatch_cnt > 0) {
658 char cnt[80];
659 snprintf(cnt, sizeof(cnt),
660 " mismatches found: %d (on raid level %d)",
661 sra->mismatch_cnt, sra->array.level);
662 alert("RebuildFinished", dev, cnt, ainfo);
663 } else
664 alert("RebuildFinished", dev, NULL, ainfo);
665 }
666 st->percent = mse->percent;
667
668 remaining_disks = sra->array.nr_disks;
669 for (i = 0; i < MAX_DISKS && remaining_disks > 0; i++) {
670 mdu_disk_info_t disc;
671 disc.number = i;
672 if (md_get_disk_info(fd, &disc) >= 0) {
673 info[i].state = disc.state;
674 info[i].major = disc.major;
675 info[i].minor = disc.minor;
676 if (disc.major || disc.minor)
677 remaining_disks --;
678 } else
679 info[i].major = info[i].minor = 0;
680 }
681 last_disk = i;
682
683 if (mse->metadata_version &&
684 strncmp(mse->metadata_version, "external:", 9) == 0 &&
685 is_subarray(mse->metadata_version+9)) {
686 char *sl;
687 strcpy(st->parent_devnm, mse->metadata_version + 10);
688 sl = strchr(st->parent_devnm, '/');
689 if (sl)
690 *sl = 0;
691 } else
692 st->parent_devnm[0] = 0;
693 if (st->metadata == NULL && st->parent_devnm[0] == 0)
694 st->metadata = super_by_fd(fd, NULL);
695
696 for (i = 0; i < MAX_DISKS; i++) {
697 mdu_disk_info_t disc = {0, 0, 0, 0, 0};
698 int newstate = 0;
699 int change;
700 char *dv = NULL;
701 disc.number = i;
702 if (i < last_disk && (info[i].major || info[i].minor)) {
703 newstate = info[i].state;
704 dv = map_dev_preferred(info[i].major, info[i].minor, 1,
705 prefer);
706 disc.state = newstate;
707 disc.major = info[i].major;
708 disc.minor = info[i].minor;
709 } else
710 newstate = (1 << MD_DISK_REMOVED);
711
712 if (dv == NULL && st->devid[i])
713 dv = map_dev_preferred(major(st->devid[i]),
714 minor(st->devid[i]), 1, prefer);
715 change = newstate ^ st->devstate[i];
716 if (st->utime && change && !st->err && !new_array) {
717 if ((st->devstate[i]&change) & (1 << MD_DISK_SYNC))
718 alert("Fail", dev, dv, ainfo);
719 else if ((newstate & (1 << MD_DISK_FAULTY)) &&
720 (disc.major || disc.minor) &&
721 st->devid[i] == makedev(disc.major,
722 disc.minor))
723 alert("FailSpare", dev, dv, ainfo);
724 else if ((newstate&change) & (1 << MD_DISK_SYNC))
725 alert("SpareActive", dev, dv, ainfo);
726 }
727 st->devstate[i] = newstate;
728 st->devid[i] = makedev(disc.major, disc.minor);
729 }
730 st->active = sra->array.active_disks;
731 st->working = sra->array.working_disks;
732 st->spare = sra->array.spare_disks;
733 st->failed = sra->array.failed_disks;
734 st->utime = array.utime;
735 st->raid = sra->array.raid_disks;
736 st->err = 0;
737 if ((st->active < st->raid) && st->spare == 0)
738 retval = 1;
739
740 out:
741 if (sra)
742 sysfs_free(sra);
743 if (fd >= 0)
744 close(fd);
745 return retval;
746
747 disappeared:
748 if (!st->err && !is_container)
749 alert("DeviceDisappeared", dev, NULL, ainfo);
750 st->err++;
751 goto out;
752 }
753
754 static int add_new_arrays(struct mdstat_ent *mdstat, struct state **statelist,
755 int test, struct alert_info *info)
756 {
757 struct mdstat_ent *mse;
758 int new_found = 0;
759 char *name;
760
761 for (mse = mdstat; mse; mse = mse->next)
762 if (mse->devnm[0] && (!mse->level || /* retrieve containers */
763 (strcmp(mse->level, "raid0") != 0 &&
764 strcmp(mse->level, "linear") != 0))) {
765 struct state *st = xcalloc(1, sizeof *st);
766 mdu_array_info_t array;
767 int fd;
768
769 name = get_md_name(mse->devnm);
770 if (!name) {
771 free(st);
772 continue;
773 }
774
775 st->devname = xstrdup(name);
776 if ((fd = open(st->devname, O_RDONLY)) < 0 ||
777 md_get_array_info(fd, &array) < 0) {
778 /* no such array */
779 if (fd >= 0)
780 close(fd);
781 put_md_name(st->devname);
782 free(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 strcpy(st->devnm, 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 strcpy(st->parent_devnm,
803 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 #ifndef NO_LIBUDEV
1069 /* function: check_udev_activity
1070 * Description: Function waits for udev to finish
1071 * events processing.
1072 * Returns:
1073 * 1 - detected error while opening udev
1074 * 2 - timeout
1075 * 0 - successfull completion
1076 */
1077 static int check_udev_activity(void)
1078 {
1079 struct udev *udev = NULL;
1080 struct udev_queue *udev_queue = NULL;
1081 int timeout_cnt = 30;
1082 int rc = 0;
1083
1084 /*
1085 * In rare cases systemd may not have udevm,
1086 * in such cases just exit with rc 0
1087 */
1088 if (!use_udev())
1089 goto out;
1090
1091 udev = udev_new();
1092 if (!udev) {
1093 rc = 1;
1094 goto out;
1095 }
1096
1097 udev_queue = udev_queue_new(udev);
1098 if (!udev_queue) {
1099 rc = 1;
1100 goto out;
1101 }
1102
1103 if (udev_queue_get_queue_is_empty(udev_queue))
1104 goto out;
1105
1106 while (!udev_queue_get_queue_is_empty(udev_queue)) {
1107 sleep(1);
1108
1109 if (timeout_cnt)
1110 timeout_cnt--;
1111 else {
1112 rc = 2;
1113 goto out;
1114 }
1115 }
1116
1117 out:
1118 if (udev_queue)
1119 udev_queue_unref(udev_queue);
1120 if (udev)
1121 udev_unref(udev);
1122 return rc;
1123 }
1124 #endif
1125
1126 /* Not really Monitor but ... */
1127 int Wait(char *dev)
1128 {
1129 char devnm[32];
1130 dev_t rdev;
1131 char *tmp;
1132 int rv = 1;
1133 int frozen_remaining = 3;
1134
1135 if (!stat_is_blkdev(dev, &rdev))
1136 return 2;
1137
1138 tmp = devid2devnm(rdev);
1139 if (!tmp) {
1140 pr_err("Cannot get md device name.\n");
1141 return 2;
1142 }
1143
1144 strcpy(devnm, tmp);
1145
1146 while(1) {
1147 struct mdstat_ent *ms = mdstat_read(1, 0);
1148 struct mdstat_ent *e;
1149
1150 for (e = ms; e; e = e->next)
1151 if (strcmp(e->devnm, devnm) == 0)
1152 break;
1153
1154 if (e && e->percent == RESYNC_NONE) {
1155 /* We could be in the brief pause before something
1156 * starts. /proc/mdstat doesn't show that, but
1157 * sync_action does.
1158 */
1159 struct mdinfo mdi;
1160 char buf[21];
1161
1162 if (sysfs_init(&mdi, -1, devnm))
1163 return 2;
1164 if (sysfs_get_str(&mdi, NULL, "sync_action",
1165 buf, 20) > 0 &&
1166 strcmp(buf,"idle\n") != 0) {
1167 e->percent = RESYNC_UNKNOWN;
1168 if (strcmp(buf, "frozen\n") == 0) {
1169 if (frozen_remaining == 0)
1170 e->percent = RESYNC_NONE;
1171 else
1172 frozen_remaining -= 1;
1173 }
1174 }
1175 }
1176 if (!e || e->percent == RESYNC_NONE) {
1177 if (e && e->metadata_version &&
1178 strncmp(e->metadata_version, "external:", 9) == 0) {
1179 if (is_subarray(&e->metadata_version[9]))
1180 ping_monitor(&e->metadata_version[9]);
1181 else
1182 ping_monitor(devnm);
1183 }
1184 free_mdstat(ms);
1185 return rv;
1186 }
1187 free_mdstat(ms);
1188 rv = 0;
1189 mdstat_wait(5);
1190 }
1191 }
1192
1193 /* The state "broken" is used only for RAID0/LINEAR - it's the same as
1194 * "clean", but used in case the array has one or more members missing.
1195 */
1196 static char *clean_states[] = {
1197 "clear", "inactive", "readonly", "read-auto", "clean", "broken", NULL };
1198
1199 int WaitClean(char *dev, int verbose)
1200 {
1201 int fd;
1202 struct mdinfo *mdi;
1203 int rv = 1;
1204 char devnm[32];
1205
1206 if (!stat_is_blkdev(dev, NULL))
1207 return 2;
1208 fd = open(dev, O_RDONLY);
1209 if (fd < 0) {
1210 if (verbose)
1211 pr_err("Couldn't open %s: %s\n", dev, strerror(errno));
1212 return 1;
1213 }
1214
1215 strcpy(devnm, fd2devnm(fd));
1216 mdi = sysfs_read(fd, devnm, GET_VERSION|GET_LEVEL|GET_SAFEMODE);
1217 if (!mdi) {
1218 if (verbose)
1219 pr_err("Failed to read sysfs attributes for %s\n", dev);
1220 close(fd);
1221 return 0;
1222 }
1223
1224 switch(mdi->array.level) {
1225 case LEVEL_LINEAR:
1226 case LEVEL_MULTIPATH:
1227 case 0:
1228 /* safemode delay is irrelevant for these levels */
1229 rv = 0;
1230 }
1231
1232 /* for internal metadata the kernel handles the final clean
1233 * transition, containers can never be dirty
1234 */
1235 if (!is_subarray(mdi->text_version))
1236 rv = 0;
1237
1238 /* safemode disabled ? */
1239 if (mdi->safe_mode_delay == 0)
1240 rv = 0;
1241
1242 if (rv) {
1243 int state_fd = sysfs_open(fd2devnm(fd), NULL, "array_state");
1244 char buf[20];
1245 int delay = 5000;
1246
1247 /* minimize the safe_mode_delay and prepare to wait up to 5s
1248 * for writes to quiesce
1249 */
1250 sysfs_set_safemode(mdi, 1);
1251
1252 /* wait for array_state to be clean */
1253 while (1) {
1254 rv = read(state_fd, buf, sizeof(buf));
1255 if (rv < 0)
1256 break;
1257 if (sysfs_match_word(buf, clean_states) <
1258 (int)ARRAY_SIZE(clean_states) - 1)
1259 break;
1260 rv = sysfs_wait(state_fd, &delay);
1261 if (rv < 0 && errno != EINTR)
1262 break;
1263 lseek(state_fd, 0, SEEK_SET);
1264 }
1265 if (rv < 0)
1266 rv = 1;
1267 else if (ping_monitor(mdi->text_version) == 0) {
1268 /* we need to ping to close the window between array
1269 * state transitioning to clean and the metadata being
1270 * marked clean
1271 */
1272 rv = 0;
1273 } else {
1274 rv = 1;
1275 pr_err("Error connecting monitor with %s\n", dev);
1276 }
1277 if (rv && verbose)
1278 pr_err("Error waiting for %s to be clean\n", dev);
1279
1280 /* restore the original safe_mode_delay */
1281 sysfs_set_safemode(mdi, mdi->safe_mode_delay);
1282 close(state_fd);
1283 }
1284
1285 sysfs_free(mdi);
1286 close(fd);
1287
1288 return rv;
1289 }