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