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