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