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