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