]> git.ipfire.org Git - thirdparty/mdadm.git/blame - Monitor.c
Don't close fds in write_init_super
[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];
0f0749ad 47 dev_t devid[MaxDisks];
2e0172b1
N
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);
83f3bc5f 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
44d337f0
AC
155 if (daemonise) {
156 int rv = make_daemon(pidfile);
157 if (rv >= 0)
158 return rv;
159 }
e0d19036 160
2e0172b1
N
161 if (share)
162 if (check_one_sharer(scan))
163 return 1;
edde9560 164
e0d19036 165 if (devlist == NULL) {
fa56eddb 166 struct mddev_ident *mdlist = conf_get_ident(NULL);
e0d19036 167 for (; mdlist; mdlist=mdlist->next) {
fe056d1f
N
168 struct state *st;
169 if (mdlist->devname == NULL)
170 continue;
112cace6
N
171 if (strcasecmp(mdlist->devname, "<ignore>") == 0)
172 continue;
9bfc6a7d 173 st = calloc(1, sizeof *st);
e0d19036
NB
174 if (st == NULL)
175 continue;
db2d001c
N
176 if (mdlist->devname[0] == '/')
177 st->devname = strdup(mdlist->devname);
178 else {
179 st->devname = malloc(8+strlen(mdlist->devname)+1);
180 strcpy(strcpy(st->devname, "/dev/md/"),
181 mdlist->devname);
182 }
e0d19036 183 st->next = statelist;
4450e59f 184 st->devnum = INT_MAX;
e0d19036 185 st->percent = -2;
feb716e9 186 st->expected_spares = mdlist->spare_disks;
e0d19036
NB
187 if (mdlist->spare_group)
188 st->spare_group = strdup(mdlist->spare_group);
e0d19036
NB
189 statelist = st;
190 }
191 } else {
a655e550 192 struct mddev_dev *dv;
e0d19036 193 for (dv=devlist ; dv; dv=dv->next) {
fa56eddb 194 struct mddev_ident *mdlist = conf_get_ident(dv->devname);
9bfc6a7d 195 struct state *st = calloc(1, sizeof *st);
e0d19036
NB
196 if (st == NULL)
197 continue;
198 st->devname = strdup(dv->devname);
e0d19036 199 st->next = statelist;
4450e59f 200 st->devnum = INT_MAX;
e0d19036 201 st->percent = -2;
feb716e9 202 st->expected_spares = -1;
e5329c37
NB
203 if (mdlist) {
204 st->expected_spares = mdlist->spare_disks;
205 if (mdlist->spare_group)
206 st->spare_group = strdup(mdlist->spare_group);
207 }
e0d19036
NB
208 statelist = st;
209 }
210 }
211
212
213 while (! finished) {
aa88f531 214 int new_found = 0;
e0d19036 215 struct state *st;
a90e1050 216 int anydegraded = 0;
e0d19036
NB
217
218 if (mdstat)
219 free_mdstat(mdstat);
22a88995 220 mdstat = mdstat_read(oneshot?0:1, 0);
e0d19036 221
2e0172b1 222 for (st=statelist; st; st=st->next)
e0bd6a96 223 if (check_array(st, mdstat, test, &info, increments))
a90e1050 224 anydegraded = 1;
2e0172b1 225
e0d19036 226 /* now check if there are any new devices found in mdstat */
2e0172b1 227 if (scan)
83f3bc5f 228 new_found = add_new_arrays(mdstat, &statelist, test,
e0bd6a96 229 &info);
2e0172b1 230
e0d19036
NB
231 /* If an array has active < raid && spare == 0 && spare_group != NULL
232 * Look for another array with spare > 0 and active == raid and same spare_group
233 * if found, choose a device and hotremove/hotadd
234 */
a90e1050 235 if (share && anydegraded)
e0bd6a96 236 try_spare_migration(statelist, &info);
aa88f531
NB
237 if (!new_found) {
238 if (oneshot)
239 break;
240 else
dd0781e5 241 mdstat_wait(period);
aa88f531 242 }
98c6faba 243 test = 0;
52826846 244 }
b5e64645
NB
245 if (pidfile)
246 unlink(pidfile);
52826846
NB
247 return 0;
248}
249
2e0172b1
N
250static int make_daemon(char *pidfile)
251{
44d337f0
AC
252 /* Return:
253 * -1 in the forked daemon
254 * 0 in the parent
255 * 1 on error
256 * so a none-negative becomes the exit code.
257 */
2e0172b1
N
258 int pid = fork();
259 if (pid > 0) {
260 if (!pidfile)
261 printf("%d\n", pid);
262 else {
263 FILE *pid_file;
264 pid_file=fopen(pidfile, "w");
265 if (!pid_file)
266 perror("cannot create pid file");
267 else {
268 fprintf(pid_file,"%d\n", pid);
269 fclose(pid_file);
270 }
271 }
272 return 0;
273 }
274 if (pid < 0) {
275 perror("daemonise");
276 return 1;
277 }
278 close(0);
279 open("/dev/null", O_RDWR);
280 dup2(0,1);
281 dup2(0,2);
282 setsid();
44d337f0 283 return -1;
2e0172b1
N
284}
285
286static int check_one_sharer(int scan)
287{
288 int pid, rv;
289 FILE *fp;
290 char dir[20];
291 struct stat buf;
292 fp = fopen("/var/run/mdadm/autorebuild.pid", "r");
293 if (fp) {
294 fscanf(fp, "%d", &pid);
295 sprintf(dir, "/proc/%d", pid);
296 rv = stat(dir, &buf);
297 if (rv != -1) {
298 if (scan) {
299 fprintf(stderr, Name ": Only one "
300 "autorebuild process allowed"
301 " in scan mode, aborting\n");
302 fclose(fp);
303 return 1;
304 } else {
305 fprintf(stderr, Name ": Warning: One"
306 " autorebuild process already"
a1bb2065 307 " running.\n");
2e0172b1
N
308 }
309 }
310 fclose(fp);
311 }
312 if (scan) {
a1bb2065
AC
313 if (mkdir("/var/run/mdadm", S_IRWXU) < 0 &&
314 errno != EEXIST) {
315 fprintf(stderr, Name ": Can't create "
316 "autorebuild.pid file\n");
317 } else {
318 fp = fopen("/var/run/mdadm/autorebuild.pid", "w");
319 if (!fp)
320 fprintf(stderr, Name ": Cannot create"
321 " autorebuild.pid"
322 "file\n");
323 else {
324 pid = getpid();
325 fprintf(fp, "%d\n", pid);
326 fclose(fp);
327 }
2e0172b1
N
328 }
329 }
330 return 0;
331}
52826846 332
e0bd6a96 333static void alert(char *event, char *dev, char *disc, struct alert_info *info)
52826846 334{
773135f5
NB
335 int priority;
336
e0bd6a96 337 if (!info->alert_cmd && !info->mailaddr) {
cd29a5c8 338 time_t now = time(0);
aba69144 339
e0d19036 340 printf("%1.15s: %s on %s %s\n", ctime(&now)+4, event, dev, disc?disc:"unknown device");
cd29a5c8 341 }
e0bd6a96 342 if (info->alert_cmd) {
52826846
NB
343 int pid = fork();
344 switch(pid) {
345 default:
346 waitpid(pid, NULL, 0);
347 break;
348 case -1:
349 break;
350 case 0:
e0bd6a96
N
351 execl(info->alert_cmd, info->alert_cmd,
352 event, dev, disc, NULL);
52826846
NB
353 exit(2);
354 }
355 }
e0bd6a96 356 if (info->mailaddr &&
aba69144 357 (strncmp(event, "Fail", 4)==0 ||
98c6faba 358 strncmp(event, "Test", 4)==0 ||
d1732eeb 359 strncmp(event, "Spares", 6)==0 ||
aa88f531 360 strncmp(event, "Degrade", 7)==0)) {
52826846
NB
361 FILE *mp = popen(Sendmail, "w");
362 if (mp) {
008e1100 363 FILE *mdstat;
52826846
NB
364 char hname[256];
365 gethostname(hname, sizeof(hname));
366 signal(SIGPIPE, SIG_IGN);
e0bd6a96
N
367 if (info->mailfrom)
368 fprintf(mp, "From: %s\n", info->mailfrom);
4948b8f7
NB
369 else
370 fprintf(mp, "From: " Name " monitoring <root>\n");
e0bd6a96
N
371 fprintf(mp, "To: %s\n", info->mailaddr);
372 fprintf(mp, "Subject: %s event on %s:%s\n\n",
373 event, dev, hname);
52826846 374
e0bd6a96
N
375 fprintf(mp,
376 "This is an automatically generated"
377 " mail message from " Name "\n");
52826846
NB
378 fprintf(mp, "running on %s\n\n", hname);
379
e0bd6a96
N
380 fprintf(mp,
381 "A %s event had been detected on"
382 " md device %s.\n\n", event, dev);
52826846 383
37dfc3d6 384 if (disc && disc[0] != ' ')
e0bd6a96
N
385 fprintf(mp,
386 "It could be related to"
387 " component device %s.\n\n", disc);
37dfc3d6
NB
388 if (disc && disc[0] == ' ')
389 fprintf(mp, "Extra information:%s.\n\n", disc);
52826846
NB
390
391 fprintf(mp, "Faithfully yours, etc.\n");
008e1100
NB
392
393 mdstat = fopen("/proc/mdstat", "r");
394 if (mdstat) {
395 char buf[8192];
396 int n;
e0bd6a96
N
397 fprintf(mp,
398 "\nP.S. The /proc/mdstat file"
399 " currently contains the following:\n\n");
008e1100 400 while ( (n=fread(buf, 1, sizeof(buf), mdstat)) > 0)
e0bd6a96 401 n=fwrite(buf, 1, n, mp);
008e1100
NB
402 fclose(mdstat);
403 }
6278fb3a 404 pclose(mp);
52826846 405 }
52826846 406 }
773135f5
NB
407
408 /* log the event to syslog maybe */
e0bd6a96 409 if (info->dosyslog) {
773135f5
NB
410 /* Log at a different severity depending on the event.
411 *
412 * These are the critical events: */
413 if (strncmp(event, "Fail", 4)==0 ||
414 strncmp(event, "Degrade", 7)==0 ||
415 strncmp(event, "DeviceDisappeared", 17)==0)
416 priority = LOG_CRIT;
417 /* Good to know about, but are not failures: */
418 else if (strncmp(event, "Rebuild", 7)==0 ||
d1732eeb
NB
419 strncmp(event, "MoveSpare", 9)==0 ||
420 strncmp(event, "Spares", 6) != 0)
773135f5
NB
421 priority = LOG_WARNING;
422 /* Everything else: */
423 else
424 priority = LOG_INFO;
425
426 if (disc)
e0bd6a96
N
427 syslog(priority,
428 "%s event detected on md device %s,"
429 " component device %s", event, dev, disc);
773135f5 430 else
e0bd6a96
N
431 syslog(priority,
432 "%s event detected on md device %s",
433 event, dev);
773135f5 434 }
52826846 435}
b90c0e9a 436
a90e1050 437static int check_array(struct state *st, struct mdstat_ent *mdstat,
e0bd6a96 438 int test, struct alert_info *ainfo,
a90e1050 439 int increments)
2e0172b1 440{
ff044d6b
AC
441 /* Update the state 'st' to reflect any changes shown in mdstat,
442 * or found by directly examining the array, and return
443 * '1' if the array is degraded, or '0' if it is optimal (or dead).
444 */
2e0172b1
N
445 struct { int state, major, minor; } info[MaxDisks];
446 mdu_array_info_t array;
447 struct mdstat_ent *mse = NULL, *mse2;
448 char *dev = st->devname;
449 int fd;
450 int i;
451
452 if (test)
e0bd6a96 453 alert("TestMessage", dev, NULL, ainfo);
2e0172b1
N
454 fd = open(dev, O_RDONLY);
455 if (fd < 0) {
456 if (!st->err)
e0bd6a96
N
457 alert("DeviceDisappeared", dev, NULL, ainfo);
458 st->err=1;
a90e1050 459 return 0;
2e0172b1
N
460 }
461 fcntl(fd, F_SETFD, FD_CLOEXEC);
462 if (ioctl(fd, GET_ARRAY_INFO, &array)<0) {
463 if (!st->err)
e0bd6a96
N
464 alert("DeviceDisappeared", dev, NULL, ainfo);
465 st->err=1;
2e0172b1 466 close(fd);
a90e1050 467 return 0;
2e0172b1
N
468 }
469 /* It's much easier to list what array levels can't
470 * have a device disappear than all of them that can
471 */
472 if (array.level == 0 || array.level == -1) {
473 if (!st->err)
e0bd6a96 474 alert("DeviceDisappeared", dev, "Wrong-Level", ainfo);
2e0172b1
N
475 st->err = 1;
476 close(fd);
a90e1050 477 return 0;
2e0172b1
N
478 }
479 if (st->devnum == INT_MAX) {
480 struct stat stb;
481 if (fstat(fd, &stb) == 0 &&
482 (S_IFMT&stb.st_mode)==S_IFBLK) {
483 if (major(stb.st_rdev) == MD_MAJOR)
484 st->devnum = minor(stb.st_rdev);
485 else
486 st->devnum = -1- (minor(stb.st_rdev)>>6);
487 }
488 }
489
490 for (mse2 = mdstat ; mse2 ; mse2=mse2->next)
491 if (mse2->devnum == st->devnum) {
492 mse2->devnum = INT_MAX; /* flag it as "used" */
493 mse = mse2;
494 }
495
496 if (!mse) {
497 /* duplicated array in statelist
498 * or re-created after reading mdstat*/
499 st->err = 1;
500 close(fd);
a90e1050 501 return 0;
2e0172b1
N
502 }
503 /* this array is in /proc/mdstat */
504 if (array.utime == 0)
505 /* external arrays don't update utime, so
506 * just make sure it is always different. */
507 array.utime = st->utime + 1;;
508
509 if (st->utime == array.utime &&
510 st->failed == array.failed_disks &&
511 st->working == array.working_disks &&
512 st->spare == array.spare_disks &&
513 (mse == NULL || (
514 mse->percent == st->percent
515 ))) {
516 close(fd);
517 st->err = 0;
ff044d6b
AC
518 if ((st->active < st->raid) && st->spare == 0)
519 return 1;
520 else
521 return 0;
2e0172b1
N
522 }
523 if (st->utime == 0 && /* new array */
524 mse->pattern && strchr(mse->pattern, '_') /* degraded */
525 )
e0bd6a96 526 alert("DegradedArray", dev, NULL, ainfo);
2e0172b1
N
527
528 if (st->utime == 0 && /* new array */
529 st->expected_spares > 0 &&
530 array.spare_disks < st->expected_spares)
e0bd6a96 531 alert("SparesMissing", dev, NULL, ainfo);
2e0172b1
N
532 if (st->percent == -1 &&
533 mse->percent >= 0)
e0bd6a96 534 alert("RebuildStarted", dev, NULL, ainfo);
2e0172b1
N
535 if (st->percent >= 0 &&
536 mse->percent >= 0 &&
537 (mse->percent / increments) > (st->percent / increments)) {
538 char percentalert[15]; // "RebuildNN" (10 chars) or "RebuildStarted" (15 chars)
539
540 if((mse->percent / increments) == 0)
541 snprintf(percentalert, sizeof(percentalert), "RebuildStarted");
542 else
543 snprintf(percentalert, sizeof(percentalert), "Rebuild%02d", mse->percent);
544
e0bd6a96 545 alert(percentalert, dev, NULL, ainfo);
2e0172b1
N
546 }
547
548 if (mse->percent == -1 &&
549 st->percent >= 0) {
550 /* Rebuild/sync/whatever just finished.
551 * If there is a number in /mismatch_cnt,
552 * we should report that.
553 */
554 struct mdinfo *sra =
555 sysfs_read(-1, st->devnum, GET_MISMATCH);
556 if (sra && sra->mismatch_cnt > 0) {
557 char cnt[40];
558 sprintf(cnt, " mismatches found: %d", sra->mismatch_cnt);
e0bd6a96 559 alert("RebuildFinished", dev, cnt, ainfo);
2e0172b1 560 } else
e0bd6a96 561 alert("RebuildFinished", dev, NULL, ainfo);
2e0172b1
N
562 if (sra)
563 free(sra);
564 }
565 st->percent = mse->percent;
566
567 for (i=0; i<MaxDisks && i <= array.raid_disks + array.nr_disks;
568 i++) {
569 mdu_disk_info_t disc;
570 disc.number = i;
571 if (ioctl(fd, GET_DISK_INFO, &disc) >= 0) {
572 info[i].state = disc.state;
573 info[i].major = disc.major;
574 info[i].minor = disc.minor;
575 } else
576 info[i].major = info[i].minor = 0;
577 }
578
579 if (strncmp(mse->metadata_version, "external:", 9) == 0 &&
580 is_subarray(mse->metadata_version+9))
581 st->parent_dev =
582 devname2devnum(mse->metadata_version+10);
583 else
584 st->parent_dev = NoMdDev;
585 if (st->metadata == NULL &&
586 st->parent_dev == NoMdDev)
587 st->metadata = super_by_fd(fd, NULL);
588
589 close(fd);
590
591 for (i=0; i<MaxDisks; i++) {
592 mdu_disk_info_t disc = {0,0,0,0,0};
593 int newstate=0;
594 int change;
595 char *dv = NULL;
596 disc.number = i;
597 if (i > array.raid_disks + array.nr_disks) {
598 newstate = 0;
599 disc.major = disc.minor = 0;
600 } else if (info[i].major || info[i].minor) {
601 newstate = info[i].state;
602 dv = map_dev(info[i].major, info[i].minor, 1);
603 disc.state = newstate;
604 disc.major = info[i].major;
605 disc.minor = info[i].minor;
606 } else if (mse && mse->pattern && i < (int)strlen(mse->pattern)) {
607 switch(mse->pattern[i]) {
608 case 'U': newstate = 6 /* ACTIVE/SYNC */; break;
609 case '_': newstate = 0; break;
610 }
611 disc.major = disc.minor = 0;
612 }
613 if (dv == NULL && st->devid[i])
614 dv = map_dev(major(st->devid[i]),
615 minor(st->devid[i]), 1);
616 change = newstate ^ st->devstate[i];
617 if (st->utime && change && !st->err) {
618 if (i < array.raid_disks &&
619 (((newstate&change)&(1<<MD_DISK_FAULTY)) ||
620 ((st->devstate[i]&change)&(1<<MD_DISK_ACTIVE)) ||
621 ((st->devstate[i]&change)&(1<<MD_DISK_SYNC)))
622 )
e0bd6a96 623 alert("Fail", dev, dv, ainfo);
2e0172b1
N
624 else if (i >= array.raid_disks &&
625 (disc.major || disc.minor) &&
626 st->devid[i] == makedev(disc.major, disc.minor) &&
627 ((newstate&change)&(1<<MD_DISK_FAULTY))
628 )
e0bd6a96 629 alert("FailSpare", dev, dv, ainfo);
2e0172b1
N
630 else if (i < array.raid_disks &&
631 ! (newstate & (1<<MD_DISK_REMOVED)) &&
632 (((st->devstate[i]&change)&(1<<MD_DISK_FAULTY)) ||
633 ((newstate&change)&(1<<MD_DISK_ACTIVE)) ||
634 ((newstate&change)&(1<<MD_DISK_SYNC)))
635 )
e0bd6a96 636 alert("SpareActive", dev, dv, ainfo);
2e0172b1
N
637 }
638 st->devstate[i] = newstate;
639 st->devid[i] = makedev(disc.major, disc.minor);
640 }
641 st->active = array.active_disks;
642 st->working = array.working_disks;
643 st->spare = array.spare_disks;
644 st->failed = array.failed_disks;
645 st->utime = array.utime;
646 st->raid = array.raid_disks;
647 st->err = 0;
a90e1050
N
648 if ((st->active < st->raid) && st->spare == 0)
649 return 1;
650 return 0;
2e0172b1
N
651}
652
83f3bc5f 653static int add_new_arrays(struct mdstat_ent *mdstat, struct state **statelist,
e0bd6a96 654 int test, struct alert_info *info)
2e0172b1
N
655{
656 struct mdstat_ent *mse;
657 int new_found = 0;
658
659 for (mse=mdstat; mse; mse=mse->next)
660 if (mse->devnum != INT_MAX &&
661 (!mse->level || /* retrieve containers */
662 (strcmp(mse->level, "raid0") != 0 &&
663 strcmp(mse->level, "linear") != 0))
664 ) {
9bfc6a7d 665 struct state *st = calloc(1, sizeof *st);
2e0172b1
N
666 mdu_array_info_t array;
667 int fd;
668 if (st == NULL)
669 continue;
670 st->devname = strdup(get_md_name(mse->devnum));
671 if ((fd = open(st->devname, O_RDONLY)) < 0 ||
672 ioctl(fd, GET_ARRAY_INFO, &array)< 0) {
673 /* no such array */
674 if (fd >=0) close(fd);
675 put_md_name(st->devname);
676 free(st->devname);
677 if (st->metadata) {
678 st->metadata->ss->free_super(st->metadata);
679 free(st->metadata);
680 }
681 free(st);
682 continue;
683 }
684 close(fd);
83f3bc5f 685 st->next = *statelist;
2e0172b1
N
686 st->err = 1;
687 st->devnum = mse->devnum;
688 st->percent = -2;
2e0172b1
N
689 st->expected_spares = -1;
690 if (strncmp(mse->metadata_version, "external:", 9) == 0 &&
691 is_subarray(mse->metadata_version+9))
692 st->parent_dev =
693 devname2devnum(mse->metadata_version+10);
694 else
695 st->parent_dev = NoMdDev;
83f3bc5f 696 *statelist = st;
2e0172b1 697 if (test)
e0bd6a96
N
698 alert("TestMessage", st->devname, NULL, info);
699 alert("NewArray", st->devname, NULL, info);
2e0172b1
N
700 new_found = 1;
701 }
702 return new_found;
703}
704
de697acc 705static int get_min_spare_size_required(struct state *st, unsigned long long *sizep)
80e7f8c3
AC
706{
707 int fd;
80e7f8c3
AC
708
709 if (!st->metadata ||
300f5033
CA
710 !st->metadata->ss->min_acceptable_spare_size) {
711 *sizep = 0;
de697acc 712 return 0;
300f5033 713 }
80e7f8c3
AC
714
715 fd = open(st->devname, O_RDONLY);
716 if (fd < 0)
de697acc
AC
717 return 1;
718 if (st->metadata->ss->external)
719 st->metadata->ss->load_container(st->metadata, fd, st->devname);
720 else
721 st->metadata->ss->load_super(st->metadata, fd, st->devname);
80e7f8c3 722 close(fd);
de697acc
AC
723 if (!st->metadata->sb)
724 return 1;
725 *sizep = st->metadata->ss->min_acceptable_spare_size(st->metadata);
80e7f8c3
AC
726 st->metadata->ss->free_super(st->metadata);
727
de697acc 728 return 0;
80e7f8c3
AC
729}
730
5ec0f373 731static int check_donor(struct state *from, struct state *to)
2feb22ef 732{
66f5c4b6
N
733 struct state *sub;
734
2feb22ef
N
735 if (from == to)
736 return 0;
66f5c4b6
N
737 if (from->parent)
738 /* Cannot move from a member */
2feb22ef 739 return 0;
ff044d6b
AC
740 if (from->err)
741 return 0;
66f5c4b6
N
742 for (sub = from->subarray; sub; sub = sub->subarray)
743 /* If source array has degraded subarrays, don't
744 * remove anything
745 */
746 if (sub->active < sub->raid)
747 return 0;
748 if (from->metadata->ss->external == 0)
749 if (from->active < from->raid)
750 return 0;
2feb22ef
N
751 if (from->spare <= 0)
752 return 0;
e78dda3b 753 return 1;
2feb22ef
N
754}
755
0f0749ad 756static dev_t choose_spare(struct state *from, struct state *to,
f0b85306 757 struct domainlist *domlist, unsigned long long min_size)
0fa21e85
N
758{
759 int d;
0f0749ad 760 dev_t dev = 0;
0fa21e85
N
761
762 for (d = from->raid; !dev && d < MaxDisks; d++) {
763 if (from->devid[d] > 0 &&
764 from->devstate[d] == 0) {
765 struct dev_policy *pol;
766 unsigned long long dev_size;
767
768 if (min_size &&
769 dev_size_from_id(from->devid[d], &dev_size) &&
770 dev_size < min_size)
771 continue;
772
773 pol = devnum_policy(from->devid[d]);
774 if (from->spare_group)
775 pol_add(&pol, pol_domain,
776 from->spare_group, NULL);
777 if (domain_test(domlist, pol, to->metadata->ss->name))
778 dev = from->devid[d];
779 dev_policy_free(pol);
780 }
781 }
782 return dev;
783}
784
0f0749ad 785static dev_t container_choose_spare(struct state *from, struct state *to,
f0b85306
AC
786 struct domainlist *domlist,
787 unsigned long long min_size)
5739e0d0
N
788{
789 /* This is similar to choose_spare, but we cannot trust devstate,
790 * so we need to read the metadata instead
791 */
326727d9 792 struct mdinfo *list;
5739e0d0 793 struct supertype *st = from->metadata;
ff044d6b 794 int fd = open(from->devname, O_RDONLY);
5739e0d0 795 int err;
0f0749ad 796 dev_t dev = 0;
5739e0d0
N
797
798 if (fd < 0)
799 return 0;
326727d9
AC
800 if (!st->ss->getinfo_super_disks) {
801 close(fd);
5739e0d0 802 return 0;
326727d9 803 }
5739e0d0
N
804
805 err = st->ss->load_container(st, fd, NULL);
806 close(fd);
807 if (err)
808 return 0;
5739e0d0 809
326727d9
AC
810 /* We only need one spare so full list not needed */
811 list = container_choose_spares(st, min_size, domlist, from->spare_group,
812 to->metadata->ss->name, 1);
813 if (list) {
814 struct mdinfo *disks = list->devs;
815 if (disks)
816 dev = makedev(disks->disk.major, disks->disk.minor);
817 sysfs_free(list);
5739e0d0 818 }
326727d9 819 st->ss->free_super(st);
5739e0d0
N
820 return dev;
821}
822
823
e0bd6a96 824static void try_spare_migration(struct state *statelist, struct alert_info *info)
2e0172b1 825{
66f5c4b6
N
826 struct state *from;
827 struct state *st;
c3621c0a
ML
828
829 link_containers_with_subarrays(statelist);
66f5c4b6
N
830 for (st = statelist; st; st = st->next)
831 if (st->active < st->raid &&
ef15641f 832 st->spare == 0 && !st->err) {
e78dda3b
N
833 struct domainlist *domlist = NULL;
834 int d;
66f5c4b6 835 struct state *to = st;
f0b85306 836 unsigned long long min_size;
66f5c4b6 837
c0dc0ad5
CA
838 if (to->parent_dev != NoMdDev && !to->parent)
839 /* subarray monitored without parent container
840 * we can't move spares here */
841 continue;
842
66f5c4b6
N
843 if (to->parent)
844 /* member of a container */
845 to = to->parent;
e78dda3b 846
de697acc
AC
847 if (get_min_spare_size_required(to, &min_size))
848 continue;
e9a2ac02
AC
849 if (to->metadata->ss->external) {
850 /* We must make sure there is
851 * no suitable spare in container already.
852 * If there is we don't add more */
853 dev_t devid = container_choose_spare(
854 to, to, NULL, min_size);
855 if (devid > 0)
856 continue;
857 }
e78dda3b
N
858 for (d = 0; d < MaxDisks; d++)
859 if (to->devid[d])
860 domainlist_add_dev(&domlist,
861 to->devid[d],
862 to->metadata->ss->name);
863 if (to->spare_group)
864 domain_add(&domlist, to->spare_group);
5ec0f373
ML
865 /*
866 * No spare migration if the destination
867 * has no domain. Skip this array.
868 */
869 if (!domlist)
870 continue;
0fa21e85 871 for (from=statelist ; from ; from=from->next) {
0f0749ad 872 dev_t devid;
5ec0f373 873 if (!check_donor(from, to))
0fa21e85 874 continue;
5739e0d0
N
875 if (from->metadata->ss->external)
876 devid = container_choose_spare(
f0b85306 877 from, to, domlist, min_size);
5739e0d0 878 else
f0b85306
AC
879 devid = choose_spare(from, to, domlist,
880 min_size);
0fa21e85 881 if (devid > 0
d52bb542
AC
882 && move_spare(from->devname, to->devname, devid)) {
883 alert("MoveSpare", to->devname, from->devname, info);
884 break;
885 }
0fa21e85 886 }
e78dda3b 887 domain_free(domlist);
2e0172b1
N
888 }
889}
c3621c0a
ML
890
891/* search the statelist to connect external
892 * metadata subarrays with their containers
893 * We always completely rebuild the tree from scratch as
894 * that is safest considering the possibility of entries
895 * disappearing or changing.
896 */
897static void link_containers_with_subarrays(struct state *list)
898{
899 struct state *st;
900 struct state *cont;
901 for (st = list; st; st = st->next) {
902 st->parent = NULL;
903 st->subarray = NULL;
904 }
905 for (st = list; st; st = st->next)
906 if (st->parent_dev != NoMdDev)
907 for (cont = list; cont; cont = cont->next)
908 if (!cont->err &&
909 cont->parent_dev == NoMdDev &&
910 cont->devnum == st->parent_dev) {
911 st->parent = cont;
912 st->subarray = cont->subarray;
913 cont->subarray = st;
914 break;
915 }
916}
917
b90c0e9a
NB
918/* Not really Monitor but ... */
919int Wait(char *dev)
920{
921 struct stat stb;
922 int devnum;
923 int rv = 1;
924
925 if (stat(dev, &stb) != 0) {
926 fprintf(stderr, Name ": Cannot find %s: %s\n", dev,
927 strerror(errno));
928 return 2;
929 }
c94709e8 930 devnum = stat2devnum(&stb);
b90c0e9a
NB
931
932 while(1) {
933 struct mdstat_ent *ms = mdstat_read(1, 0);
934 struct mdstat_ent *e;
935
936 for (e=ms ; e; e=e->next)
937 if (e->devnum == devnum)
938 break;
939
940 if (!e || e->percent < 0) {
e7783ee6 941 if (e && e->metadata_version &&
c94709e8
DW
942 strncmp(e->metadata_version, "external:", 9) == 0) {
943 if (is_subarray(&e->metadata_version[9]))
944 ping_monitor(&e->metadata_version[9]);
945 else
946 ping_monitor(devnum2devname(devnum));
947 }
b90c0e9a
NB
948 free_mdstat(ms);
949 return rv;
950 }
89a10d84 951 free_mdstat(ms);
b90c0e9a
NB
952 rv = 0;
953 mdstat_wait(5);
954 }
955}