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