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