]> git.ipfire.org Git - thirdparty/mdadm.git/blob - Monitor.c
Monitor: don't open md array that doesn't exist.
[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 if (!st->err)
487 alert("DeviceDisappeared", dev, NULL, ainfo);
488 st->err++;
489 return 0;
490 }
491 }
492 fd = open(dev, O_RDONLY);
493 if (fd < 0) {
494 if (!st->err)
495 alert("DeviceDisappeared", dev, NULL, ainfo);
496 st->err++;
497 return 0;
498 }
499 fcntl(fd, F_SETFD, FD_CLOEXEC);
500 if (ioctl(fd, GET_ARRAY_INFO, &array)<0) {
501 if (!st->err)
502 alert("DeviceDisappeared", dev, NULL, ainfo);
503 st->err++;
504 close(fd);
505 return 0;
506 }
507 /* It's much easier to list what array levels can't
508 * have a device disappear than all of them that can
509 */
510 if (array.level == 0 || array.level == -1) {
511 if (!st->err && !st->from_config)
512 alert("DeviceDisappeared", dev, "Wrong-Level", ainfo);
513 st->err++;
514 close(fd);
515 return 0;
516 }
517 if (st->devnm[0] == 0)
518 strcpy(st->devnm, fd2devnm(fd));
519
520 for (mse2 = mdstat ; mse2 ; mse2=mse2->next)
521 if (strcmp(mse2->devnm, st->devnm) == 0) {
522 mse2->devnm[0] = 0; /* flag it as "used" */
523 mse = mse2;
524 }
525
526 if (!mse) {
527 /* duplicated array in statelist
528 * or re-created after reading mdstat*/
529 st->err++;
530 close(fd);
531 return 0;
532 }
533 /* this array is in /proc/mdstat */
534 if (array.utime == 0)
535 /* external arrays don't update utime, so
536 * just make sure it is always different. */
537 array.utime = st->utime + 1;;
538
539 if (st->err) {
540 /* New array appeared where previously had an error */
541 st->err = 0;
542 st->percent = RESYNC_NONE;
543 new_array = 1;
544 alert("NewArray", st->devname, NULL, ainfo);
545 }
546
547 if (st->utime == array.utime &&
548 st->failed == array.failed_disks &&
549 st->working == array.working_disks &&
550 st->spare == array.spare_disks &&
551 (mse == NULL || (
552 mse->percent == st->percent
553 ))) {
554 close(fd);
555 if ((st->active < st->raid) && st->spare == 0)
556 return 1;
557 else
558 return 0;
559 }
560 if (st->utime == 0 && /* new array */
561 mse->pattern && strchr(mse->pattern, '_') /* degraded */
562 )
563 alert("DegradedArray", dev, NULL, ainfo);
564
565 if (st->utime == 0 && /* new array */
566 st->expected_spares > 0 &&
567 array.spare_disks < st->expected_spares)
568 alert("SparesMissing", dev, NULL, ainfo);
569 if (st->percent < 0 && st->percent != RESYNC_UNKNOWN &&
570 mse->percent >= 0)
571 alert("RebuildStarted", dev, NULL, ainfo);
572 if (st->percent >= 0 &&
573 mse->percent >= 0 &&
574 (mse->percent / increments) > (st->percent / increments)) {
575 char percentalert[15]; // "RebuildNN" (10 chars) or "RebuildStarted" (15 chars)
576
577 if((mse->percent / increments) == 0)
578 snprintf(percentalert, sizeof(percentalert), "RebuildStarted");
579 else
580 snprintf(percentalert, sizeof(percentalert), "Rebuild%02d", mse->percent);
581
582 alert(percentalert, dev, NULL, ainfo);
583 }
584
585 if (mse->percent == RESYNC_NONE &&
586 st->percent >= 0) {
587 /* Rebuild/sync/whatever just finished.
588 * If there is a number in /mismatch_cnt,
589 * we should report that.
590 */
591 struct mdinfo *sra =
592 sysfs_read(-1, st->devnm, GET_MISMATCH);
593 if (sra && sra->mismatch_cnt > 0) {
594 char cnt[80];
595 snprintf(cnt, sizeof(cnt),
596 " mismatches found: %d (on raid level %d)",
597 sra->mismatch_cnt, array.level);
598 alert("RebuildFinished", dev, cnt, ainfo);
599 } else
600 alert("RebuildFinished", dev, NULL, ainfo);
601 if (sra)
602 free(sra);
603 }
604 st->percent = mse->percent;
605
606 remaining_disks = array.nr_disks;
607 for (i=0; i<MAX_DISKS && remaining_disks > 0;
608 i++) {
609 mdu_disk_info_t disc;
610 disc.number = i;
611 if (ioctl(fd, GET_DISK_INFO, &disc) >= 0) {
612 info[i].state = disc.state;
613 info[i].major = disc.major;
614 info[i].minor = disc.minor;
615 if (disc.major || disc.minor)
616 remaining_disks --;
617 } else
618 info[i].major = info[i].minor = 0;
619 }
620 last_disk = i;
621
622 if (mse->metadata_version &&
623 strncmp(mse->metadata_version, "external:", 9) == 0 &&
624 is_subarray(mse->metadata_version+9)) {
625 char *sl;
626 strcpy(st->parent_devnm,
627 mse->metadata_version+10);
628 sl = strchr(st->parent_devnm, '/');
629 if (sl)
630 *sl = 0;
631 } else
632 st->parent_devnm[0] = 0;
633 if (st->metadata == NULL &&
634 st->parent_devnm[0] == 0)
635 st->metadata = super_by_fd(fd, NULL);
636
637 close(fd);
638
639 for (i=0; i<MAX_DISKS; i++) {
640 mdu_disk_info_t disc = {0,0,0,0,0};
641 int newstate=0;
642 int change;
643 char *dv = NULL;
644 disc.number = i;
645 if (i < last_disk &&
646 (info[i].major || info[i].minor)) {
647 newstate = info[i].state;
648 dv = map_dev_preferred(
649 info[i].major, info[i].minor, 1,
650 prefer);
651 disc.state = newstate;
652 disc.major = info[i].major;
653 disc.minor = info[i].minor;
654 } else
655 newstate = (1 << MD_DISK_REMOVED);
656
657 if (dv == NULL && st->devid[i])
658 dv = map_dev_preferred(
659 major(st->devid[i]),
660 minor(st->devid[i]), 1, prefer);
661 change = newstate ^ st->devstate[i];
662 if (st->utime && change && !st->err && !new_array) {
663 if ((st->devstate[i]&change)&(1<<MD_DISK_SYNC))
664 alert("Fail", dev, dv, ainfo);
665 else if ((newstate & (1<<MD_DISK_FAULTY)) &&
666 (disc.major || disc.minor) &&
667 st->devid[i] == makedev(disc.major, disc.minor))
668 alert("FailSpare", dev, dv, ainfo);
669 else if ((newstate&change)&(1<<MD_DISK_SYNC))
670 alert("SpareActive", dev, dv, ainfo);
671 }
672 st->devstate[i] = newstate;
673 st->devid[i] = makedev(disc.major, disc.minor);
674 }
675 st->active = array.active_disks;
676 st->working = array.working_disks;
677 st->spare = array.spare_disks;
678 st->failed = array.failed_disks;
679 st->utime = array.utime;
680 st->raid = array.raid_disks;
681 st->err = 0;
682 if ((st->active < st->raid) && st->spare == 0)
683 return 1;
684 return 0;
685 }
686
687 static int add_new_arrays(struct mdstat_ent *mdstat, struct state **statelist,
688 int test, struct alert_info *info)
689 {
690 struct mdstat_ent *mse;
691 int new_found = 0;
692
693 for (mse=mdstat; mse; mse=mse->next)
694 if (mse->devnm[0] &&
695 (!mse->level || /* retrieve containers */
696 (strcmp(mse->level, "raid0") != 0 &&
697 strcmp(mse->level, "linear") != 0))
698 ) {
699 struct state *st = xcalloc(1, sizeof *st);
700 mdu_array_info_t array;
701 int fd;
702 st->devname = xstrdup(get_md_name(mse->devnm));
703 if ((fd = open(st->devname, O_RDONLY)) < 0 ||
704 ioctl(fd, GET_ARRAY_INFO, &array)< 0) {
705 /* no such array */
706 if (fd >=0) close(fd);
707 put_md_name(st->devname);
708 free(st->devname);
709 if (st->metadata) {
710 st->metadata->ss->free_super(st->metadata);
711 free(st->metadata);
712 }
713 free(st);
714 continue;
715 }
716 close(fd);
717 st->next = *statelist;
718 st->err = 1;
719 st->from_auto = 1;
720 strcpy(st->devnm, mse->devnm);
721 st->percent = RESYNC_UNKNOWN;
722 st->expected_spares = -1;
723 if (mse->metadata_version &&
724 strncmp(mse->metadata_version, "external:", 9) == 0 &&
725 is_subarray(mse->metadata_version+9)) {
726 char *sl;
727 strcpy(st->parent_devnm,
728 mse->metadata_version+10);
729 sl = strchr(st->parent_devnm, '/');
730 *sl = 0;
731 } else
732 st->parent_devnm[0] = 0;
733 *statelist = st;
734 if (test)
735 alert("TestMessage", st->devname, NULL, info);
736 new_found = 1;
737 }
738 return new_found;
739 }
740
741 static int get_min_spare_size_required(struct state *st, unsigned long long *sizep)
742 {
743 int fd;
744
745 if (!st->metadata ||
746 !st->metadata->ss->min_acceptable_spare_size) {
747 *sizep = 0;
748 return 0;
749 }
750
751 fd = open(st->devname, O_RDONLY);
752 if (fd < 0)
753 return 1;
754 if (st->metadata->ss->external)
755 st->metadata->ss->load_container(st->metadata, fd, st->devname);
756 else
757 st->metadata->ss->load_super(st->metadata, fd, st->devname);
758 close(fd);
759 if (!st->metadata->sb)
760 return 1;
761 *sizep = st->metadata->ss->min_acceptable_spare_size(st->metadata);
762 st->metadata->ss->free_super(st->metadata);
763
764 return 0;
765 }
766
767 static int check_donor(struct state *from, struct state *to)
768 {
769 struct state *sub;
770
771 if (from == to)
772 return 0;
773 if (from->parent)
774 /* Cannot move from a member */
775 return 0;
776 if (from->err)
777 return 0;
778 for (sub = from->subarray; sub; sub = sub->subarray)
779 /* If source array has degraded subarrays, don't
780 * remove anything
781 */
782 if (sub->active < sub->raid)
783 return 0;
784 if (from->metadata->ss->external == 0)
785 if (from->active < from->raid)
786 return 0;
787 if (from->spare <= 0)
788 return 0;
789 return 1;
790 }
791
792 static dev_t choose_spare(struct state *from, struct state *to,
793 struct domainlist *domlist, unsigned long long min_size)
794 {
795 int d;
796 dev_t dev = 0;
797
798 for (d = from->raid; !dev && d < MAX_DISKS; d++) {
799 if (from->devid[d] > 0 &&
800 from->devstate[d] == 0) {
801 struct dev_policy *pol;
802 unsigned long long dev_size;
803
804 if (to->metadata->ss->external &&
805 test_partition_from_id(from->devid[d]))
806 continue;
807
808 if (min_size &&
809 dev_size_from_id(from->devid[d], &dev_size) &&
810 dev_size < min_size)
811 continue;
812
813 pol = devid_policy(from->devid[d]);
814 if (from->spare_group)
815 pol_add(&pol, pol_domain,
816 from->spare_group, NULL);
817 if (domain_test(domlist, pol, to->metadata->ss->name) == 1)
818 dev = from->devid[d];
819 dev_policy_free(pol);
820 }
821 }
822 return dev;
823 }
824
825 static dev_t container_choose_spare(struct state *from, struct state *to,
826 struct domainlist *domlist,
827 unsigned long long min_size, int active)
828 {
829 /* This is similar to choose_spare, but we cannot trust devstate,
830 * so we need to read the metadata instead
831 */
832 struct mdinfo *list;
833 struct supertype *st = from->metadata;
834 int fd = open(from->devname, O_RDONLY);
835 int err;
836 dev_t dev = 0;
837
838 if (fd < 0)
839 return 0;
840 if (!st->ss->getinfo_super_disks) {
841 close(fd);
842 return 0;
843 }
844
845 err = st->ss->load_container(st, fd, NULL);
846 close(fd);
847 if (err)
848 return 0;
849
850 if (from == to) {
851 /* We must check if number of active disks has not increased
852 * since ioctl in main loop. mdmon may have added spare
853 * to subarray. If so we do not need to look for more spares
854 * so return non zero value */
855 int active_cnt = 0;
856 struct mdinfo *dp;
857 list = st->ss->getinfo_super_disks(st);
858 if (!list) {
859 st->ss->free_super(st);
860 return 1;
861 }
862 dp = list->devs;
863 while (dp) {
864 if (dp->disk.state & (1<<MD_DISK_SYNC) &&
865 !(dp->disk.state & (1<<MD_DISK_FAULTY)))
866 active_cnt++;
867 dp = dp->next;
868 }
869 sysfs_free(list);
870 if (active < active_cnt) {
871 /* Spare just activated.*/
872 st->ss->free_super(st);
873 return 1;
874 }
875 }
876
877 /* We only need one spare so full list not needed */
878 list = container_choose_spares(st, min_size, domlist, from->spare_group,
879 to->metadata->ss->name, 1);
880 if (list) {
881 struct mdinfo *disks = list->devs;
882 if (disks)
883 dev = makedev(disks->disk.major, disks->disk.minor);
884 sysfs_free(list);
885 }
886 st->ss->free_super(st);
887 return dev;
888 }
889
890 static void try_spare_migration(struct state *statelist, struct alert_info *info)
891 {
892 struct state *from;
893 struct state *st;
894
895 link_containers_with_subarrays(statelist);
896 for (st = statelist; st; st = st->next)
897 if (st->active < st->raid &&
898 st->spare == 0 && !st->err) {
899 struct domainlist *domlist = NULL;
900 int d;
901 struct state *to = st;
902 unsigned long long min_size;
903
904 if (to->parent_devnm[0] && !to->parent)
905 /* subarray monitored without parent container
906 * we can't move spares here */
907 continue;
908
909 if (to->parent)
910 /* member of a container */
911 to = to->parent;
912
913 if (get_min_spare_size_required(to, &min_size))
914 continue;
915 if (to->metadata->ss->external) {
916 /* We must make sure there is
917 * no suitable spare in container already.
918 * If there is we don't add more */
919 dev_t devid = container_choose_spare(
920 to, to, NULL, min_size, st->active);
921 if (devid > 0)
922 continue;
923 }
924 for (d = 0; d < MAX_DISKS; d++)
925 if (to->devid[d])
926 domainlist_add_dev(&domlist,
927 to->devid[d],
928 to->metadata->ss->name);
929 if (to->spare_group)
930 domain_add(&domlist, to->spare_group);
931 /*
932 * No spare migration if the destination
933 * has no domain. Skip this array.
934 */
935 if (!domlist)
936 continue;
937 for (from=statelist ; from ; from=from->next) {
938 dev_t devid;
939 if (!check_donor(from, to))
940 continue;
941 if (from->metadata->ss->external)
942 devid = container_choose_spare(
943 from, to, domlist, min_size, 0);
944 else
945 devid = choose_spare(from, to, domlist,
946 min_size);
947 if (devid > 0
948 && move_spare(from->devname, to->devname, devid)) {
949 alert("MoveSpare", to->devname, from->devname, info);
950 break;
951 }
952 }
953 domain_free(domlist);
954 }
955 }
956
957 /* search the statelist to connect external
958 * metadata subarrays with their containers
959 * We always completely rebuild the tree from scratch as
960 * that is safest considering the possibility of entries
961 * disappearing or changing.
962 */
963 static void link_containers_with_subarrays(struct state *list)
964 {
965 struct state *st;
966 struct state *cont;
967 for (st = list; st; st = st->next) {
968 st->parent = NULL;
969 st->subarray = NULL;
970 }
971 for (st = list; st; st = st->next)
972 if (st->parent_devnm[0])
973 for (cont = list; cont; cont = cont->next)
974 if (!cont->err &&
975 cont->parent_devnm[0] == 0 &&
976 strcmp(cont->devnm, st->parent_devnm) == 0) {
977 st->parent = cont;
978 st->subarray = cont->subarray;
979 cont->subarray = st;
980 break;
981 }
982 }
983
984 /* Not really Monitor but ... */
985 int Wait(char *dev)
986 {
987 struct stat stb;
988 char devnm[32];
989 int rv = 1;
990
991 if (stat(dev, &stb) != 0) {
992 pr_err("Cannot find %s: %s\n", dev,
993 strerror(errno));
994 return 2;
995 }
996 strcpy(devnm, stat2devnm(&stb));
997
998 while(1) {
999 struct mdstat_ent *ms = mdstat_read(1, 0);
1000 struct mdstat_ent *e;
1001
1002 for (e=ms ; e; e=e->next)
1003 if (strcmp(e->devnm, devnm) == 0)
1004 break;
1005
1006 if (e->percent == RESYNC_NONE) {
1007 /* We could be in the brief pause before something
1008 * starts. /proc/mdstat doesn't show that, but
1009 * sync_action does.
1010 */
1011 struct mdinfo mdi;
1012 char buf[21];
1013 sysfs_init(&mdi, -1, devnm);
1014 if (sysfs_get_str(&mdi, NULL, "sync_action",
1015 buf, 20) > 0 &&
1016 strcmp(buf,"idle\n") != 0)
1017 e->percent = RESYNC_UNKNOWN;
1018 }
1019 if (!e || e->percent == RESYNC_NONE) {
1020 if (e && e->metadata_version &&
1021 strncmp(e->metadata_version, "external:", 9) == 0) {
1022 if (is_subarray(&e->metadata_version[9]))
1023 ping_monitor(&e->metadata_version[9]);
1024 else
1025 ping_monitor(devnm);
1026 }
1027 free_mdstat(ms);
1028 return rv;
1029 }
1030 free_mdstat(ms);
1031 rv = 0;
1032 mdstat_wait(5);
1033 }
1034 }
1035
1036 #ifndef MDASSEMBLE
1037
1038 static char *clean_states[] = {
1039 "clear", "inactive", "readonly", "read-auto", "clean", NULL };
1040
1041 int WaitClean(char *dev, int sock, int verbose)
1042 {
1043 int fd;
1044 struct mdinfo *mdi;
1045 int rv = 1;
1046 char devnm[32];
1047
1048 fd = open(dev, O_RDONLY);
1049 if (fd < 0) {
1050 if (verbose)
1051 pr_err("Couldn't open %s: %s\n", dev, strerror(errno));
1052 return 1;
1053 }
1054
1055 strcpy(devnm, fd2devnm(fd));
1056 mdi = sysfs_read(fd, devnm, GET_VERSION|GET_LEVEL|GET_SAFEMODE);
1057 if (!mdi) {
1058 if (verbose)
1059 pr_err("Failed to read sysfs attributes for "
1060 "%s\n", dev);
1061 close(fd);
1062 return 0;
1063 }
1064
1065 switch(mdi->array.level) {
1066 case LEVEL_LINEAR:
1067 case LEVEL_MULTIPATH:
1068 case 0:
1069 /* safemode delay is irrelevant for these levels */
1070 rv = 0;
1071 }
1072
1073 /* for internal metadata the kernel handles the final clean
1074 * transition, containers can never be dirty
1075 */
1076 if (!is_subarray(mdi->text_version))
1077 rv = 0;
1078
1079 /* safemode disabled ? */
1080 if (mdi->safe_mode_delay == 0)
1081 rv = 0;
1082
1083 if (rv) {
1084 int state_fd = sysfs_open(fd2devnm(fd), NULL, "array_state");
1085 char buf[20];
1086 int delay = 5000;
1087
1088 /* minimize the safe_mode_delay and prepare to wait up to 5s
1089 * for writes to quiesce
1090 */
1091 sysfs_set_safemode(mdi, 1);
1092
1093 /* wait for array_state to be clean */
1094 while (1) {
1095 rv = read(state_fd, buf, sizeof(buf));
1096 if (rv < 0)
1097 break;
1098 if (sysfs_match_word(buf, clean_states) <= 4)
1099 break;
1100 rv = sysfs_wait(state_fd, &delay);
1101 if (rv < 0 && errno != EINTR)
1102 break;
1103 lseek(state_fd, 0, SEEK_SET);
1104 }
1105 if (rv < 0)
1106 rv = 1;
1107 else if (fping_monitor(sock) == 0 ||
1108 ping_monitor(mdi->text_version) == 0) {
1109 /* we need to ping to close the window between array
1110 * state transitioning to clean and the metadata being
1111 * marked clean
1112 */
1113 rv = 0;
1114 } else
1115 rv = 1;
1116 if (rv && verbose)
1117 pr_err("Error waiting for %s to be clean\n",
1118 dev);
1119
1120 /* restore the original safe_mode_delay */
1121 sysfs_set_safemode(mdi, mdi->safe_mode_delay);
1122 close(state_fd);
1123 }
1124
1125 sysfs_free(mdi);
1126 close(fd);
1127
1128 return rv;
1129 }
1130 #endif /* MDASSEMBLE */