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