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