]> git.ipfire.org Git - thirdparty/mdadm.git/blob - Monitor.c
658ba9cbde5821bdbd330ed45d3c3f374630f640
[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 /* The largest number of disks current arrays can manage is 384
34 * This really should be dynamically, but that will have to wait
35 * At least it isn't MD_SB_DISKS.
36 */
37 #define MaxDisks 384
38 struct state {
39 char *devname;
40 int devnum; /* to sync with mdstat info */
41 long utime;
42 int err;
43 char *spare_group;
44 int active, working, failed, spare, raid;
45 int expected_spares;
46 int devstate[MaxDisks];
47 unsigned devid[MaxDisks];
48 int percent;
49 int parent_dev; /* For subarray, devnum of parent.
50 * For others, NoMdDev
51 */
52 struct supertype *metadata;
53 struct state *subarray;/* for a container it is a link to first subarray
54 * for a subarray it is a link to next subarray
55 * in the same container */
56 struct state *parent; /* for a subarray it is a link to its container
57 */
58 struct state *next;
59 };
60
61 static int make_daemon(char *pidfile);
62 static int check_one_sharer(int scan);
63 static void alert(char *event, char *dev, char *disc, char *mailaddr, char *mailfrom,
64 char *cmd, int dosyslog);
65 static int check_array(struct state *st, struct mdstat_ent *mdstat,
66 int test, char *mailaddr,
67 char *mailfrom, char *alert_cmd, int dosyslog,
68 int increments);
69 static int add_new_arrays(struct mdstat_ent *mdstat, struct state *statelist,
70 int test, char *mailaddr, char *mailfrom,
71 char *alert_cmd, int dosyslog);
72 static void try_spare_migration(struct state *statelist,
73 char *mailaddr, char *mailfrom,
74 char *alert_cmd, int dosyslog);
75 static void link_containers_with_subarrays(struct state *list);
76
77 int Monitor(struct mddev_dev *devlist,
78 char *mailaddr, char *alert_cmd,
79 int period, int daemonise, int scan, int oneshot,
80 int dosyslog, int test, char *pidfile, int increments,
81 int share)
82 {
83 /*
84 * Every few seconds, scan every md device looking for changes
85 * When a change is found, log it, possibly run the alert command,
86 * and possibly send Email
87 *
88 * For each array, we record:
89 * Update time
90 * active/working/failed/spare drives
91 * State of each device.
92 * %rebuilt if rebuilding
93 *
94 * If the update time changes, check out all the data again
95 * It is possible that we cannot get the state of each device
96 * due to bugs in the md kernel module.
97 * We also read /proc/mdstat to get rebuild percent,
98 * and to get state on all active devices incase of kernel bug.
99 *
100 * Events are:
101 * Fail
102 * An active device had Faulty set or Active/Sync removed
103 * FailSpare
104 * A spare device had Faulty set
105 * SpareActive
106 * An active device had a reverse transition
107 * RebuildStarted
108 * percent went from -1 to +ve
109 * RebuildNN
110 * percent went from below to not-below NN%
111 * DeviceDisappeared
112 * Couldn't access a device which was previously visible
113 *
114 * if we detect an array with active<raid and spare==0
115 * we look at other arrays that have same spare-group
116 * If we find one with active==raid and spare>0,
117 * and if we can get_disk_info and find a name
118 * Then we hot-remove and hot-add to the other array
119 *
120 * If devlist is NULL, then we can monitor everything because --scan
121 * was given. We get an initial list from config file and add anything
122 * that appears in /proc/mdstat
123 */
124
125 struct state *statelist = NULL;
126 int finished = 0;
127 struct mdstat_ent *mdstat = NULL;
128 char *mailfrom = NULL;
129
130 if (!mailaddr) {
131 mailaddr = conf_get_mailaddr();
132 if (mailaddr && ! scan)
133 fprintf(stderr, Name ": 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 && ! scan)
141 fprintf(stderr, Name ": Monitor using program \"%s\" from config file\n",
142 alert_cmd);
143 }
144 if (scan && !mailaddr && !alert_cmd) {
145 fprintf(stderr, Name ": No mail address or alert command - not monitoring.\n");
146 return 1;
147 }
148
149 if (daemonise)
150 if (make_daemon(pidfile))
151 return 1;
152
153 if (share)
154 if (check_one_sharer(scan))
155 return 1;
156
157 if (devlist == NULL) {
158 struct mddev_ident *mdlist = conf_get_ident(NULL);
159 for (; mdlist; mdlist=mdlist->next) {
160 struct state *st;
161 if (mdlist->devname == NULL)
162 continue;
163 if (strcasecmp(mdlist->devname, "<ignore>") == 0)
164 continue;
165 st = calloc(1, sizeof *st);
166 if (st == NULL)
167 continue;
168 if (mdlist->devname[0] == '/')
169 st->devname = strdup(mdlist->devname);
170 else {
171 st->devname = malloc(8+strlen(mdlist->devname)+1);
172 strcpy(strcpy(st->devname, "/dev/md/"),
173 mdlist->devname);
174 }
175 st->next = statelist;
176 st->devnum = INT_MAX;
177 st->percent = -2;
178 st->expected_spares = mdlist->spare_disks;
179 if (mdlist->spare_group)
180 st->spare_group = strdup(mdlist->spare_group);
181 statelist = st;
182 }
183 } else {
184 struct mddev_dev *dv;
185 for (dv=devlist ; dv; dv=dv->next) {
186 struct mddev_ident *mdlist = conf_get_ident(dv->devname);
187 struct state *st = calloc(1, sizeof *st);
188 if (st == NULL)
189 continue;
190 st->devname = strdup(dv->devname);
191 st->next = statelist;
192 st->devnum = INT_MAX;
193 st->percent = -2;
194 st->expected_spares = -1;
195 if (mdlist) {
196 st->expected_spares = mdlist->spare_disks;
197 if (mdlist->spare_group)
198 st->spare_group = strdup(mdlist->spare_group);
199 }
200 statelist = st;
201 }
202 }
203
204
205 while (! finished) {
206 int new_found = 0;
207 struct state *st;
208 int anydegraded = 0;
209
210 if (mdstat)
211 free_mdstat(mdstat);
212 mdstat = mdstat_read(oneshot?0:1, 0);
213
214 for (st=statelist; st; st=st->next)
215 if (check_array(st, mdstat, test, mailaddr, mailfrom,
216 alert_cmd, dosyslog, increments))
217 anydegraded = 1;
218
219 /* now check if there are any new devices found in mdstat */
220 if (scan)
221 new_found = add_new_arrays(mdstat, statelist, test,
222 mailaddr, mailfrom, alert_cmd,
223 dosyslog);
224
225 /* If an array has active < raid && spare == 0 && spare_group != NULL
226 * Look for another array with spare > 0 and active == raid and same spare_group
227 * if found, choose a device and hotremove/hotadd
228 */
229 if (share && anydegraded)
230 try_spare_migration(statelist, mailaddr, mailfrom,
231 alert_cmd, dosyslog);
232 if (!new_found) {
233 if (oneshot)
234 break;
235 else
236 mdstat_wait(period);
237 }
238 test = 0;
239 }
240 if (pidfile)
241 unlink(pidfile);
242 return 0;
243 }
244
245 static int make_daemon(char *pidfile)
246 {
247 int pid = fork();
248 if (pid > 0) {
249 if (!pidfile)
250 printf("%d\n", pid);
251 else {
252 FILE *pid_file;
253 pid_file=fopen(pidfile, "w");
254 if (!pid_file)
255 perror("cannot create pid file");
256 else {
257 fprintf(pid_file,"%d\n", pid);
258 fclose(pid_file);
259 }
260 }
261 return 0;
262 }
263 if (pid < 0) {
264 perror("daemonise");
265 return 1;
266 }
267 close(0);
268 open("/dev/null", O_RDWR);
269 dup2(0,1);
270 dup2(0,2);
271 setsid();
272 return 0;
273 }
274
275 static int check_one_sharer(int scan)
276 {
277 int pid, rv;
278 FILE *fp;
279 char dir[20];
280 struct stat buf;
281 fp = fopen("/var/run/mdadm/autorebuild.pid", "r");
282 if (fp) {
283 fscanf(fp, "%d", &pid);
284 sprintf(dir, "/proc/%d", pid);
285 rv = stat(dir, &buf);
286 if (rv != -1) {
287 if (scan) {
288 fprintf(stderr, Name ": Only one "
289 "autorebuild process allowed"
290 " in scan mode, aborting\n");
291 fclose(fp);
292 return 1;
293 } else {
294 fprintf(stderr, Name ": Warning: One"
295 " autorebuild process already"
296 " running.");
297 }
298 }
299 fclose(fp);
300 }
301 if (scan) {
302 fp = fopen("/var/run/mdadm/autorebuild.pid", "w");
303 if (!fp)
304 fprintf(stderr, Name ": Cannot create"
305 " autorebuild.pid "
306 "file\n");
307 else {
308 pid = getpid();
309 fprintf(fp, "%d\n", pid);
310 fclose(fp);
311 }
312 }
313 return 0;
314 }
315
316 static void alert(char *event, char *dev, char *disc, char *mailaddr, char *mailfrom, char *cmd,
317 int dosyslog)
318 {
319 int priority;
320
321 if (!cmd && !mailaddr) {
322 time_t now = time(0);
323
324 printf("%1.15s: %s on %s %s\n", ctime(&now)+4, event, dev, disc?disc:"unknown device");
325 }
326 if (cmd) {
327 int pid = fork();
328 switch(pid) {
329 default:
330 waitpid(pid, NULL, 0);
331 break;
332 case -1:
333 break;
334 case 0:
335 execl(cmd, cmd, event, dev, disc, NULL);
336 exit(2);
337 }
338 }
339 if (mailaddr &&
340 (strncmp(event, "Fail", 4)==0 ||
341 strncmp(event, "Test", 4)==0 ||
342 strncmp(event, "Spares", 6)==0 ||
343 strncmp(event, "Degrade", 7)==0)) {
344 FILE *mp = popen(Sendmail, "w");
345 if (mp) {
346 FILE *mdstat;
347 char hname[256];
348 gethostname(hname, sizeof(hname));
349 signal(SIGPIPE, SIG_IGN);
350 if (mailfrom)
351 fprintf(mp, "From: %s\n", mailfrom);
352 else
353 fprintf(mp, "From: " Name " monitoring <root>\n");
354 fprintf(mp, "To: %s\n", mailaddr);
355 fprintf(mp, "Subject: %s event on %s:%s\n\n", event, dev, hname);
356
357 fprintf(mp, "This is an automatically generated mail message from " Name "\n");
358 fprintf(mp, "running on %s\n\n", hname);
359
360 fprintf(mp, "A %s event had been detected on md device %s.\n\n", event, dev);
361
362 if (disc && disc[0] != ' ')
363 fprintf(mp, "It could be related to component device %s.\n\n", disc);
364 if (disc && disc[0] == ' ')
365 fprintf(mp, "Extra information:%s.\n\n", disc);
366
367 fprintf(mp, "Faithfully yours, etc.\n");
368
369 mdstat = fopen("/proc/mdstat", "r");
370 if (mdstat) {
371 char buf[8192];
372 int n;
373 fprintf(mp, "\nP.S. The /proc/mdstat file currently contains the following:\n\n");
374 while ( (n=fread(buf, 1, sizeof(buf), mdstat)) > 0)
375 n=fwrite(buf, 1, n, mp); /* yes, i don't care about the result */
376 fclose(mdstat);
377 }
378 pclose(mp);
379 }
380
381 }
382
383 /* log the event to syslog maybe */
384 if (dosyslog) {
385 /* Log at a different severity depending on the event.
386 *
387 * These are the critical events: */
388 if (strncmp(event, "Fail", 4)==0 ||
389 strncmp(event, "Degrade", 7)==0 ||
390 strncmp(event, "DeviceDisappeared", 17)==0)
391 priority = LOG_CRIT;
392 /* Good to know about, but are not failures: */
393 else if (strncmp(event, "Rebuild", 7)==0 ||
394 strncmp(event, "MoveSpare", 9)==0 ||
395 strncmp(event, "Spares", 6) != 0)
396 priority = LOG_WARNING;
397 /* Everything else: */
398 else
399 priority = LOG_INFO;
400
401 if (disc)
402 syslog(priority, "%s event detected on md device %s, component device %s", event, dev, disc);
403 else
404 syslog(priority, "%s event detected on md device %s", event, dev);
405 }
406 }
407
408 static int check_array(struct state *st, struct mdstat_ent *mdstat,
409 int test, char *mailaddr,
410 char *mailfrom, char *alert_cmd, int dosyslog,
411 int increments)
412 {
413 struct { int state, major, minor; } info[MaxDisks];
414 mdu_array_info_t array;
415 struct mdstat_ent *mse = NULL, *mse2;
416 char *dev = st->devname;
417 int fd;
418 int i;
419
420 if (test)
421 alert("TestMessage", dev, NULL, mailaddr, mailfrom, alert_cmd, dosyslog);
422 fd = open(dev, O_RDONLY);
423 if (fd < 0) {
424 if (!st->err)
425 alert("DeviceDisappeared", dev, NULL,
426 mailaddr, mailfrom, alert_cmd, dosyslog);
427 /* fprintf(stderr, Name ": cannot open %s: %s\n",
428 dev, strerror(errno));
429 */ st->err=1;
430 return 0;
431 }
432 fcntl(fd, F_SETFD, FD_CLOEXEC);
433 if (ioctl(fd, GET_ARRAY_INFO, &array)<0) {
434 if (!st->err)
435 alert("DeviceDisappeared", dev, NULL,
436 mailaddr, mailfrom, alert_cmd, dosyslog);
437 /* fprintf(stderr, Name ": cannot get array info for %s: %s\n",
438 dev, strerror(errno));
439 */ st->err=1;
440 close(fd);
441 return 0;
442 }
443 /* It's much easier to list what array levels can't
444 * have a device disappear than all of them that can
445 */
446 if (array.level == 0 || array.level == -1) {
447 if (!st->err)
448 alert("DeviceDisappeared", dev, "Wrong-Level",
449 mailaddr, mailfrom, alert_cmd, dosyslog);
450 st->err = 1;
451 close(fd);
452 return 0;
453 }
454 if (st->devnum == INT_MAX) {
455 struct stat stb;
456 if (fstat(fd, &stb) == 0 &&
457 (S_IFMT&stb.st_mode)==S_IFBLK) {
458 if (major(stb.st_rdev) == MD_MAJOR)
459 st->devnum = minor(stb.st_rdev);
460 else
461 st->devnum = -1- (minor(stb.st_rdev)>>6);
462 }
463 }
464
465 for (mse2 = mdstat ; mse2 ; mse2=mse2->next)
466 if (mse2->devnum == st->devnum) {
467 mse2->devnum = INT_MAX; /* flag it as "used" */
468 mse = mse2;
469 }
470
471 if (!mse) {
472 /* duplicated array in statelist
473 * or re-created after reading mdstat*/
474 st->err = 1;
475 close(fd);
476 return 0;
477 }
478 /* this array is in /proc/mdstat */
479 if (array.utime == 0)
480 /* external arrays don't update utime, so
481 * just make sure it is always different. */
482 array.utime = st->utime + 1;;
483
484 if (st->utime == array.utime &&
485 st->failed == array.failed_disks &&
486 st->working == array.working_disks &&
487 st->spare == array.spare_disks &&
488 (mse == NULL || (
489 mse->percent == st->percent
490 ))) {
491 close(fd);
492 st->err = 0;
493 return 0;
494 }
495 if (st->utime == 0 && /* new array */
496 mse->pattern && strchr(mse->pattern, '_') /* degraded */
497 )
498 alert("DegradedArray", dev, NULL, mailaddr, mailfrom, alert_cmd, dosyslog);
499
500 if (st->utime == 0 && /* new array */
501 st->expected_spares > 0 &&
502 array.spare_disks < st->expected_spares)
503 alert("SparesMissing", dev, NULL, mailaddr, mailfrom, alert_cmd, dosyslog);
504 if (st->percent == -1 &&
505 mse->percent >= 0)
506 alert("RebuildStarted", dev, NULL, mailaddr, mailfrom, alert_cmd, dosyslog);
507 if (st->percent >= 0 &&
508 mse->percent >= 0 &&
509 (mse->percent / increments) > (st->percent / increments)) {
510 char percentalert[15]; // "RebuildNN" (10 chars) or "RebuildStarted" (15 chars)
511
512 if((mse->percent / increments) == 0)
513 snprintf(percentalert, sizeof(percentalert), "RebuildStarted");
514 else
515 snprintf(percentalert, sizeof(percentalert), "Rebuild%02d", mse->percent);
516
517 alert(percentalert,
518 dev, NULL, mailaddr, mailfrom, alert_cmd, dosyslog);
519 }
520
521 if (mse->percent == -1 &&
522 st->percent >= 0) {
523 /* Rebuild/sync/whatever just finished.
524 * If there is a number in /mismatch_cnt,
525 * we should report that.
526 */
527 struct mdinfo *sra =
528 sysfs_read(-1, st->devnum, GET_MISMATCH);
529 if (sra && sra->mismatch_cnt > 0) {
530 char cnt[40];
531 sprintf(cnt, " mismatches found: %d", sra->mismatch_cnt);
532 alert("RebuildFinished", dev, cnt, mailaddr, mailfrom, alert_cmd, dosyslog);
533 } else
534 alert("RebuildFinished", dev, NULL, mailaddr, mailfrom, alert_cmd, dosyslog);
535 if (sra)
536 free(sra);
537 }
538 st->percent = mse->percent;
539
540 for (i=0; i<MaxDisks && i <= array.raid_disks + array.nr_disks;
541 i++) {
542 mdu_disk_info_t disc;
543 disc.number = i;
544 if (ioctl(fd, GET_DISK_INFO, &disc) >= 0) {
545 info[i].state = disc.state;
546 info[i].major = disc.major;
547 info[i].minor = disc.minor;
548 } else
549 info[i].major = info[i].minor = 0;
550 }
551
552 if (strncmp(mse->metadata_version, "external:", 9) == 0 &&
553 is_subarray(mse->metadata_version+9))
554 st->parent_dev =
555 devname2devnum(mse->metadata_version+10);
556 else
557 st->parent_dev = NoMdDev;
558 if (st->metadata == NULL &&
559 st->parent_dev == NoMdDev)
560 st->metadata = super_by_fd(fd, NULL);
561
562 close(fd);
563
564 for (i=0; i<MaxDisks; i++) {
565 mdu_disk_info_t disc = {0,0,0,0,0};
566 int newstate=0;
567 int change;
568 char *dv = NULL;
569 disc.number = i;
570 if (i > array.raid_disks + array.nr_disks) {
571 newstate = 0;
572 disc.major = disc.minor = 0;
573 } else if (info[i].major || info[i].minor) {
574 newstate = info[i].state;
575 dv = map_dev(info[i].major, info[i].minor, 1);
576 disc.state = newstate;
577 disc.major = info[i].major;
578 disc.minor = info[i].minor;
579 } else if (mse && mse->pattern && i < (int)strlen(mse->pattern)) {
580 switch(mse->pattern[i]) {
581 case 'U': newstate = 6 /* ACTIVE/SYNC */; break;
582 case '_': newstate = 0; break;
583 }
584 disc.major = disc.minor = 0;
585 }
586 if (dv == NULL && st->devid[i])
587 dv = map_dev(major(st->devid[i]),
588 minor(st->devid[i]), 1);
589 change = newstate ^ st->devstate[i];
590 if (st->utime && change && !st->err) {
591 if (i < array.raid_disks &&
592 (((newstate&change)&(1<<MD_DISK_FAULTY)) ||
593 ((st->devstate[i]&change)&(1<<MD_DISK_ACTIVE)) ||
594 ((st->devstate[i]&change)&(1<<MD_DISK_SYNC)))
595 )
596 alert("Fail", dev, dv, mailaddr, mailfrom, alert_cmd, dosyslog);
597 else if (i >= array.raid_disks &&
598 (disc.major || disc.minor) &&
599 st->devid[i] == makedev(disc.major, disc.minor) &&
600 ((newstate&change)&(1<<MD_DISK_FAULTY))
601 )
602 alert("FailSpare", dev, dv, mailaddr, mailfrom, alert_cmd, dosyslog);
603 else if (i < array.raid_disks &&
604 ! (newstate & (1<<MD_DISK_REMOVED)) &&
605 (((st->devstate[i]&change)&(1<<MD_DISK_FAULTY)) ||
606 ((newstate&change)&(1<<MD_DISK_ACTIVE)) ||
607 ((newstate&change)&(1<<MD_DISK_SYNC)))
608 )
609 alert("SpareActive", dev, dv, mailaddr, mailfrom, alert_cmd, dosyslog);
610 }
611 st->devstate[i] = newstate;
612 st->devid[i] = makedev(disc.major, disc.minor);
613 }
614 st->active = array.active_disks;
615 st->working = array.working_disks;
616 st->spare = array.spare_disks;
617 st->failed = array.failed_disks;
618 st->utime = array.utime;
619 st->raid = array.raid_disks;
620 st->err = 0;
621 if ((st->active < st->raid) && st->spare == 0)
622 return 1;
623 return 0;
624 }
625
626 static int add_new_arrays(struct mdstat_ent *mdstat, struct state *statelist,
627 int test, char *mailaddr, char *mailfrom,
628 char *alert_cmd, int dosyslog)
629 {
630 struct mdstat_ent *mse;
631 int new_found = 0;
632
633 for (mse=mdstat; mse; mse=mse->next)
634 if (mse->devnum != INT_MAX &&
635 (!mse->level || /* retrieve containers */
636 (strcmp(mse->level, "raid0") != 0 &&
637 strcmp(mse->level, "linear") != 0))
638 ) {
639 struct state *st = calloc(1, sizeof *st);
640 mdu_array_info_t array;
641 int fd;
642 if (st == NULL)
643 continue;
644 st->devname = strdup(get_md_name(mse->devnum));
645 if ((fd = open(st->devname, O_RDONLY)) < 0 ||
646 ioctl(fd, GET_ARRAY_INFO, &array)< 0) {
647 /* no such array */
648 if (fd >=0) close(fd);
649 put_md_name(st->devname);
650 free(st->devname);
651 if (st->metadata) {
652 st->metadata->ss->free_super(st->metadata);
653 free(st->metadata);
654 }
655 free(st);
656 continue;
657 }
658 close(fd);
659 st->next = statelist;
660 st->err = 1;
661 st->devnum = mse->devnum;
662 st->percent = -2;
663 st->expected_spares = -1;
664 if (strncmp(mse->metadata_version, "external:", 9) == 0 &&
665 is_subarray(mse->metadata_version+9))
666 st->parent_dev =
667 devname2devnum(mse->metadata_version+10);
668 else
669 st->parent_dev = NoMdDev;
670 statelist = st;
671 if (test)
672 alert("TestMessage", st->devname, NULL, mailaddr, mailfrom, alert_cmd, dosyslog);
673 alert("NewArray", st->devname, NULL, mailaddr, mailfrom, alert_cmd, dosyslog);
674 new_found = 1;
675 }
676 return new_found;
677 }
678
679 static void try_spare_migration(struct state *statelist,
680 char *mailaddr, char *mailfrom,
681 char *alert_cmd, int dosyslog)
682 {
683 struct state *st;
684
685 link_containers_with_subarrays(statelist);
686 for (st = statelist; st; st=st->next)
687 if (st->active < st->raid &&
688 st->spare == 0 &&
689 st->spare_group != NULL) {
690 struct state *st2;
691 for (st2=statelist ; st2 ; st2=st2->next)
692 if (st2 != st &&
693 st2->spare > 0 &&
694 st2->active == st2->raid &&
695 st2->spare_group != NULL &&
696 strcmp(st->spare_group, st2->spare_group) == 0) {
697 /* try to remove and add */
698 int fd1 = open(st->devname, O_RDONLY);
699 int fd2 = open(st2->devname, O_RDONLY);
700 int dev = -1;
701 int d;
702 if (fd1 < 0 || fd2 < 0) {
703 if (fd1>=0) close(fd1);
704 if (fd2>=0) close(fd2);
705 continue;
706 }
707 for (d=st2->raid; d < MaxDisks; d++) {
708 if (st2->devid[d] > 0 &&
709 st2->devstate[d] == 0) {
710 dev = st2->devid[d];
711 break;
712 }
713 }
714 if (dev > 0) {
715 struct mddev_dev devlist;
716 char devname[20];
717 devlist.next = NULL;
718 devlist.used = 0;
719 devlist.re_add = 0;
720 devlist.writemostly = 0;
721 devlist.devname = devname;
722 sprintf(devname, "%d:%d", major(dev), minor(dev));
723
724 devlist.disposition = 'r';
725 if (Manage_subdevs(st2->devname, fd2, &devlist, -1, 0) == 0) {
726 devlist.disposition = 'a';
727 if (Manage_subdevs(st->devname, fd1, &devlist, -1, 0) == 0) {
728 alert("MoveSpare", st->devname, st2->devname, mailaddr, mailfrom, alert_cmd, dosyslog);
729 close(fd1);
730 close(fd2);
731 break;
732 }
733 else Manage_subdevs(st2->devname, fd2, &devlist, -1, 0);
734 }
735 }
736 close(fd1);
737 close(fd2);
738 }
739 }
740 }
741
742 /* search the statelist to connect external
743 * metadata subarrays with their containers
744 * We always completely rebuild the tree from scratch as
745 * that is safest considering the possibility of entries
746 * disappearing or changing.
747 */
748 static void link_containers_with_subarrays(struct state *list)
749 {
750 struct state *st;
751 struct state *cont;
752 for (st = list; st; st = st->next) {
753 st->parent = NULL;
754 st->subarray = NULL;
755 }
756 for (st = list; st; st = st->next)
757 if (st->parent_dev != NoMdDev)
758 for (cont = list; cont; cont = cont->next)
759 if (!cont->err &&
760 cont->parent_dev == NoMdDev &&
761 cont->devnum == st->parent_dev) {
762 st->parent = cont;
763 st->subarray = cont->subarray;
764 cont->subarray = st;
765 break;
766 }
767 }
768
769 /* Not really Monitor but ... */
770 int Wait(char *dev)
771 {
772 struct stat stb;
773 int devnum;
774 int rv = 1;
775
776 if (stat(dev, &stb) != 0) {
777 fprintf(stderr, Name ": Cannot find %s: %s\n", dev,
778 strerror(errno));
779 return 2;
780 }
781 devnum = stat2devnum(&stb);
782
783 while(1) {
784 struct mdstat_ent *ms = mdstat_read(1, 0);
785 struct mdstat_ent *e;
786
787 for (e=ms ; e; e=e->next)
788 if (e->devnum == devnum)
789 break;
790
791 if (!e || e->percent < 0) {
792 if (e && e->metadata_version &&
793 strncmp(e->metadata_version, "external:", 9) == 0) {
794 if (is_subarray(&e->metadata_version[9]))
795 ping_monitor(&e->metadata_version[9]);
796 else
797 ping_monitor(devnum2devname(devnum));
798 }
799 free_mdstat(ms);
800 return rv;
801 }
802 free_mdstat(ms);
803 rv = 0;
804 mdstat_wait(5);
805 }
806 }