]> git.ipfire.org Git - thirdparty/mdadm.git/blame - Monitor.c
mdmonitor: set small delay once
[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
2e0172b1
N
33struct state {
34 char *devname;
4dd2df09 35 char devnm[32]; /* to sync with mdstat info */
1d13b599 36 unsigned int utime;
2e0172b1
N
37 int err;
38 char *spare_group;
39 int active, working, failed, spare, raid;
f1661bd7 40 int from_config;
73ff0732 41 int from_auto;
2e0172b1 42 int expected_spares;
b0599bda
N
43 int devstate[MAX_DISKS];
44 dev_t devid[MAX_DISKS];
2e0172b1 45 int percent;
4dd2df09
N
46 char parent_devnm[32]; /* For subarray, devnm of parent.
47 * For others, ""
48 */
2e0172b1 49 struct supertype *metadata;
c3621c0a
ML
50 struct state *subarray;/* for a container it is a link to first subarray
51 * for a subarray it is a link to next subarray
52 * in the same container */
53 struct state *parent; /* for a subarray it is a link to its container
54 */
2e0172b1
N
55 struct state *next;
56};
57
e0bd6a96
N
58struct alert_info {
59 char *mailaddr;
60 char *mailfrom;
61 char *alert_cmd;
62 int dosyslog;
63};
2e0172b1
N
64static int make_daemon(char *pidfile);
65static int check_one_sharer(int scan);
e0bd6a96 66static void alert(char *event, char *dev, char *disc, struct alert_info *info);
a90e1050 67static int check_array(struct state *st, struct mdstat_ent *mdstat,
e0bd6a96 68 int test, struct alert_info *info,
c2ecf5f6 69 int increments, char *prefer);
83f3bc5f 70static int add_new_arrays(struct mdstat_ent *mdstat, struct state **statelist,
e0bd6a96
N
71 int test, struct alert_info *info);
72static void try_spare_migration(struct state *statelist, struct alert_info *info);
c3621c0a 73static void link_containers_with_subarrays(struct state *list);
2e0172b1 74
a655e550 75int Monitor(struct mddev_dev *devlist,
52826846 76 char *mailaddr, char *alert_cmd,
95c50205
N
77 struct context *c,
78 int daemonise, int oneshot,
79 int dosyslog, char *pidfile, int increments,
80 int share)
52826846
NB
81{
82 /*
83 * Every few seconds, scan every md device looking for changes
84 * When a change is found, log it, possibly run the alert command,
85 * and possibly send Email
86 *
87 * For each array, we record:
88 * Update time
89 * active/working/failed/spare drives
90 * State of each device.
e0d19036 91 * %rebuilt if rebuilding
52826846
NB
92 *
93 * If the update time changes, check out all the data again
94 * It is possible that we cannot get the state of each device
95 * due to bugs in the md kernel module.
e0d19036
NB
96 * We also read /proc/mdstat to get rebuild percent,
97 * and to get state on all active devices incase of kernel bug.
52826846 98 *
e0d19036
NB
99 * Events are:
100 * Fail
101 * An active device had Faulty set or Active/Sync removed
102 * FailSpare
103 * A spare device had Faulty set
104 * SpareActive
105 * An active device had a reverse transition
106 * RebuildStarted
107 * percent went from -1 to +ve
9a36a9b7
ZB
108 * RebuildNN
109 * percent went from below to not-below NN%
e0d19036
NB
110 * DeviceDisappeared
111 * Couldn't access a device which was previously visible
52826846
NB
112 *
113 * if we detect an array with active<raid and spare==0
114 * we look at other arrays that have same spare-group
115 * If we find one with active==raid and spare>0,
116 * and if we can get_disk_info and find a name
117 * Then we hot-remove and hot-add to the other array
118 *
e0d19036
NB
119 * If devlist is NULL, then we can monitor everything because --scan
120 * was given. We get an initial list from config file and add anything
121 * that appears in /proc/mdstat
52826846
NB
122 */
123
2e0172b1 124 struct state *statelist = NULL;
b657208c 125 struct state *st2;
52826846 126 int finished = 0;
e0d19036 127 struct mdstat_ent *mdstat = NULL;
72362f18 128 char *mailfrom;
e0bd6a96 129 struct alert_info info;
9f3dd454 130 struct mddev_ident *mdlist;
cab9c67d 131 int delay_for_event = c->delay;
e0d19036 132
d013a55e 133 if (!mailaddr) {
8aec876d 134 mailaddr = conf_get_mailaddr();
95c50205 135 if (mailaddr && ! c->scan)
e7b84f9d 136 pr_err("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();
f566ef45 143 if (alert_cmd && !c->scan)
e7b84f9d 144 pr_err("Monitor using program \"%s\" from config file\n",
d013a55e
NB
145 alert_cmd);
146 }
95c50205 147 if (c->scan && !mailaddr && !alert_cmd && !dosyslog) {
e7b84f9d 148 pr_err("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
1011e834 162 if (share)
95c50205 163 if (check_one_sharer(c->scan))
2e0172b1 164 return 1;
edde9560 165
e0d19036 166 if (devlist == NULL) {
9f3dd454 167 mdlist = conf_get_ident(NULL);
f566ef45 168 for (; mdlist; mdlist = mdlist->next) {
fe056d1f 169 struct state *st;
f566ef45 170
fe056d1f
N
171 if (mdlist->devname == NULL)
172 continue;
112cace6
N
173 if (strcasecmp(mdlist->devname, "<ignore>") == 0)
174 continue;
503975b9 175 st = xcalloc(1, sizeof *st);
db2d001c 176 if (mdlist->devname[0] == '/')
503975b9 177 st->devname = xstrdup(mdlist->devname);
db2d001c 178 else {
503975b9 179 st->devname = xmalloc(8+strlen(mdlist->devname)+1);
db2d001c
N
180 strcpy(strcpy(st->devname, "/dev/md/"),
181 mdlist->devname);
182 }
e0d19036 183 st->next = statelist;
4dd2df09 184 st->devnm[0] = 0;
9dad51d4 185 st->percent = RESYNC_UNKNOWN;
f1661bd7 186 st->from_config = 1;
feb716e9 187 st->expected_spares = mdlist->spare_disks;
e0d19036 188 if (mdlist->spare_group)
503975b9 189 st->spare_group = xstrdup(mdlist->spare_group);
e0d19036
NB
190 statelist = st;
191 }
192 } else {
a655e550 193 struct mddev_dev *dv;
f566ef45
JS
194
195 for (dv = devlist; dv; dv = dv->next) {
503975b9 196 struct state *st = xcalloc(1, sizeof *st);
9f3dd454 197 mdlist = conf_get_ident(dv->devname);
503975b9 198 st->devname = xstrdup(dv->devname);
e0d19036 199 st->next = statelist;
4dd2df09 200 st->devnm[0] = 0;
9dad51d4 201 st->percent = RESYNC_UNKNOWN;
feb716e9 202 st->expected_spares = -1;
e5329c37
NB
203 if (mdlist) {
204 st->expected_spares = mdlist->spare_disks;
205 if (mdlist->spare_group)
503975b9 206 st->spare_group = xstrdup(mdlist->spare_group);
e5329c37 207 }
e0d19036
NB
208 statelist = st;
209 }
210 }
211
f566ef45 212 while (!finished) {
aa88f531 213 int new_found = 0;
73ff0732 214 struct state *st, **stp;
a90e1050 215 int anydegraded = 0;
007087d0 216 int anyredundant = 0;
e0d19036
NB
217
218 if (mdstat)
219 free_mdstat(mdstat);
f566ef45 220 mdstat = mdstat_read(oneshot ? 0 : 1, 0);
e0d19036 221
007087d0 222 for (st = statelist; st; st = st->next) {
95c50205
N
223 if (check_array(st, mdstat, c->test, &info,
224 increments, c->prefer))
a90e1050 225 anydegraded = 1;
007087d0
MT
226 /* for external arrays, metadata is filled for
227 * containers only
228 */
229 if (st->metadata && st->metadata->ss->external)
230 continue;
231 if (st->err == 0 && !anyredundant)
232 anyredundant = 1;
233 }
1011e834 234
e0d19036 235 /* now check if there are any new devices found in mdstat */
95c50205
N
236 if (c->scan)
237 new_found = add_new_arrays(mdstat, &statelist, c->test,
e0bd6a96 238 &info);
2e0172b1 239
e0d19036
NB
240 /* If an array has active < raid && spare == 0 && spare_group != NULL
241 * Look for another array with spare > 0 and active == raid and same spare_group
242 * if found, choose a device and hotremove/hotadd
243 */
a90e1050 244 if (share && anydegraded)
e0bd6a96 245 try_spare_migration(statelist, &info);
aa88f531
NB
246 if (!new_found) {
247 if (oneshot)
248 break;
007087d0
MT
249 else if (!anyredundant) {
250 break;
251 }
e2308733 252 else {
cab9c67d
BK
253 int wait_result = mdstat_wait(delay_for_event);
254
255 /*
256 * If mdmonitor is awaken by event, set small delay once
257 * to deal with udev and mdadm.
258 */
259 if (wait_result != 0) {
260 if (c->delay > 5)
261 delay_for_event = 5;
262 } else
263 delay_for_event = c->delay;
264
e2308733
MT
265 mdstat_close();
266 }
aa88f531 267 }
95c50205 268 c->test = 0;
73ff0732
N
269
270 for (stp = &statelist; (st = *stp) != NULL; ) {
271 if (st->from_auto && st->err > 5) {
272 *stp = st->next;
273 free(st->devname);
274 free(st->spare_group);
275 free(st);
276 } else
277 stp = &st->next;
278 }
52826846 279 }
b657208c
JS
280 for (st2 = statelist; st2; st2 = statelist) {
281 statelist = st2->next;
282 free(st2);
283 }
284
b5e64645
NB
285 if (pidfile)
286 unlink(pidfile);
52826846
NB
287 return 0;
288}
289
2e0172b1
N
290static int make_daemon(char *pidfile)
291{
44d337f0
AC
292 /* Return:
293 * -1 in the forked daemon
294 * 0 in the parent
295 * 1 on error
296 * so a none-negative becomes the exit code.
297 */
2e0172b1
N
298 int pid = fork();
299 if (pid > 0) {
300 if (!pidfile)
301 printf("%d\n", pid);
302 else {
303 FILE *pid_file;
304 pid_file=fopen(pidfile, "w");
305 if (!pid_file)
306 perror("cannot create pid file");
307 else {
308 fprintf(pid_file,"%d\n", pid);
309 fclose(pid_file);
310 }
311 }
312 return 0;
313 }
314 if (pid < 0) {
315 perror("daemonise");
316 return 1;
317 }
318 close(0);
319 open("/dev/null", O_RDWR);
f566ef45
JS
320 dup2(0, 1);
321 dup2(0, 2);
2e0172b1 322 setsid();
44d337f0 323 return -1;
2e0172b1
N
324}
325
326static int check_one_sharer(int scan)
327{
185ec439
CL
328 int pid;
329 FILE *comm_fp;
2e0172b1 330 FILE *fp;
185ec439 331 char comm_path[100];
0011874f 332 char path[100];
185ec439
CL
333 char comm[20];
334
0011874f
JS
335 sprintf(path, "%s/autorebuild.pid", MDMON_DIR);
336 fp = fopen(path, "r");
2e0172b1 337 if (fp) {
71204a50
N
338 if (fscanf(fp, "%d", &pid) != 1)
339 pid = -1;
185ec439
CL
340 snprintf(comm_path, sizeof(comm_path),
341 "/proc/%d/comm", pid);
342 comm_fp = fopen(comm_path, "r");
343 if (comm_fp) {
344 if (fscanf(comm_fp, "%s", comm) &&
345 strncmp(basename(comm), Name, strlen(Name)) == 0) {
346 if (scan) {
347 pr_err("Only one autorebuild process allowed in scan mode, aborting\n");
348 fclose(comm_fp);
349 fclose(fp);
350 return 1;
351 } else {
352 pr_err("Warning: One autorebuild process already running.\n");
353 }
2e0172b1 354 }
185ec439 355 fclose(comm_fp);
2e0172b1
N
356 }
357 fclose(fp);
358 }
359 if (scan) {
f566ef45 360 if (mkdir(MDMON_DIR, S_IRWXU) < 0 && errno != EEXIST) {
7a862a02 361 pr_err("Can't create autorebuild.pid file\n");
a1bb2065 362 } else {
0011874f 363 fp = fopen(path, "w");
a1bb2065 364 if (!fp)
7a862a02 365 pr_err("Cannot create autorebuild.pidfile\n");
a1bb2065
AC
366 else {
367 pid = getpid();
368 fprintf(fp, "%d\n", pid);
369 fclose(fp);
370 }
2e0172b1
N
371 }
372 }
373 return 0;
374}
52826846 375
e0bd6a96 376static void alert(char *event, char *dev, char *disc, struct alert_info *info)
52826846 377{
773135f5
NB
378 int priority;
379
b1bac75b 380 if (!info->alert_cmd && !info->mailaddr && !info->dosyslog) {
cd29a5c8 381 time_t now = time(0);
aba69144 382
f566ef45
JS
383 printf("%1.15s: %s on %s %s\n", ctime(&now) + 4,
384 event, dev, disc?disc:"unknown device");
cd29a5c8 385 }
e0bd6a96 386 if (info->alert_cmd) {
52826846
NB
387 int pid = fork();
388 switch(pid) {
389 default:
390 waitpid(pid, NULL, 0);
391 break;
392 case -1:
393 break;
394 case 0:
e0bd6a96
N
395 execl(info->alert_cmd, info->alert_cmd,
396 event, dev, disc, NULL);
52826846
NB
397 exit(2);
398 }
399 }
f566ef45
JS
400 if (info->mailaddr && (strncmp(event, "Fail", 4) == 0 ||
401 strncmp(event, "Test", 4) == 0 ||
402 strncmp(event, "Spares", 6) == 0 ||
403 strncmp(event, "Degrade", 7) == 0)) {
52826846
NB
404 FILE *mp = popen(Sendmail, "w");
405 if (mp) {
008e1100 406 FILE *mdstat;
52826846
NB
407 char hname[256];
408 gethostname(hname, sizeof(hname));
409 signal(SIGPIPE, SIG_IGN);
e0bd6a96
N
410 if (info->mailfrom)
411 fprintf(mp, "From: %s\n", info->mailfrom);
4948b8f7 412 else
f566ef45
JS
413 fprintf(mp, "From: %s monitoring <root>\n",
414 Name);
e0bd6a96
N
415 fprintf(mp, "To: %s\n", info->mailaddr);
416 fprintf(mp, "Subject: %s event on %s:%s\n\n",
417 event, dev, hname);
52826846 418
e0bd6a96 419 fprintf(mp,
7a862a02 420 "This is an automatically generated mail message from %s\n", Name);
52826846
NB
421 fprintf(mp, "running on %s\n\n", hname);
422
e0bd6a96 423 fprintf(mp,
7a862a02 424 "A %s event had been detected on md device %s.\n\n", event, dev);
52826846 425
37dfc3d6 426 if (disc && disc[0] != ' ')
e0bd6a96 427 fprintf(mp,
7a862a02 428 "It could be related to component device %s.\n\n", disc);
37dfc3d6
NB
429 if (disc && disc[0] == ' ')
430 fprintf(mp, "Extra information:%s.\n\n", disc);
52826846
NB
431
432 fprintf(mp, "Faithfully yours, etc.\n");
008e1100
NB
433
434 mdstat = fopen("/proc/mdstat", "r");
435 if (mdstat) {
436 char buf[8192];
437 int n;
e0bd6a96 438 fprintf(mp,
7a862a02 439 "\nP.S. The /proc/mdstat file currently contains the following:\n\n");
f566ef45
JS
440 while ((n = fread(buf, 1, sizeof(buf),
441 mdstat)) > 0)
442 n = fwrite(buf, 1, n, mp);
008e1100
NB
443 fclose(mdstat);
444 }
6278fb3a 445 pclose(mp);
52826846 446 }
52826846 447 }
773135f5
NB
448
449 /* log the event to syslog maybe */
e0bd6a96 450 if (info->dosyslog) {
773135f5
NB
451 /* Log at a different severity depending on the event.
452 *
453 * These are the critical events: */
f566ef45
JS
454 if (strncmp(event, "Fail", 4) == 0 ||
455 strncmp(event, "Degrade", 7) == 0 ||
456 strncmp(event, "DeviceDisappeared", 17) == 0)
773135f5
NB
457 priority = LOG_CRIT;
458 /* Good to know about, but are not failures: */
f566ef45
JS
459 else if (strncmp(event, "Rebuild", 7) == 0 ||
460 strncmp(event, "MoveSpare", 9) == 0 ||
d1732eeb 461 strncmp(event, "Spares", 6) != 0)
773135f5
NB
462 priority = LOG_WARNING;
463 /* Everything else: */
464 else
465 priority = LOG_INFO;
466
b033913a 467 if (disc && disc[0] != ' ')
e0bd6a96 468 syslog(priority,
7a862a02 469 "%s event detected on md device %s, component device %s", event, dev, disc);
b033913a
N
470 else if (disc)
471 syslog(priority,
472 "%s event detected on md device %s: %s",
473 event, dev, disc);
773135f5 474 else
e0bd6a96
N
475 syslog(priority,
476 "%s event detected on md device %s",
477 event, dev);
773135f5 478 }
52826846 479}
b90c0e9a 480
a90e1050 481static int check_array(struct state *st, struct mdstat_ent *mdstat,
e0bd6a96 482 int test, struct alert_info *ainfo,
c2ecf5f6 483 int increments, char *prefer)
2e0172b1 484{
ff044d6b
AC
485 /* Update the state 'st' to reflect any changes shown in mdstat,
486 * or found by directly examining the array, and return
487 * '1' if the array is degraded, or '0' if it is optimal (or dead).
488 */
b0599bda 489 struct { int state, major, minor; } info[MAX_DISKS];
aed5f5c3 490 struct mdinfo *sra = NULL;
2e0172b1
N
491 mdu_array_info_t array;
492 struct mdstat_ent *mse = NULL, *mse2;
493 char *dev = st->devname;
b9a0309c 494 int fd;
2e0172b1 495 int i;
9e6d9291
N
496 int remaining_disks;
497 int last_disk;
721b662b 498 int new_array = 0;
1830e74b 499 int retval;
802961a2 500 int is_container = 0;
b3ab4e4d 501 unsigned long redundancy_only_flags = 0;
2e0172b1
N
502
503 if (test)
e0bd6a96 504 alert("TestMessage", dev, NULL, ainfo);
b9a0309c 505
1830e74b
JS
506 retval = 0;
507
2e0172b1 508 fd = open(dev, O_RDONLY);
13e5d845
JS
509 if (fd < 0)
510 goto disappeared;
b9a0309c 511
802961a2
MT
512 if (st->devnm[0] == 0)
513 strcpy(st->devnm, fd2devnm(fd));
514
515 for (mse2 = mdstat; mse2; mse2 = mse2->next)
516 if (strcmp(mse2->devnm, st->devnm) == 0) {
517 mse2->devnm[0] = 0; /* flag it as "used" */
518 mse = mse2;
519 }
520
521 if (!mse) {
522 /* duplicated array in statelist
523 * or re-created after reading mdstat
524 */
525 st->err++;
526 goto out;
527 }
528
529 if (mse->level == NULL)
530 is_container = 1;
531
2dab69c9 532 if (!is_container && !md_array_active(fd))
13e5d845 533 goto disappeared;
b9a0309c 534
2e0172b1 535 fcntl(fd, F_SETFD, FD_CLOEXEC);
13e5d845
JS
536 if (md_get_array_info(fd, &array) < 0)
537 goto disappeared;
538
b3ab4e4d
MT
539 if (!is_container && map_name(pers, mse->level) > 0)
540 redundancy_only_flags |= GET_MISMATCH;
802961a2
MT
541
542 sra = sysfs_read(-1, st->devnm, GET_LEVEL | GET_DISKS | GET_DEVS |
b3ab4e4d 543 GET_STATE | redundancy_only_flags);
aed5f5c3 544
aed5f5c3
JS
545 if (!sra)
546 goto disappeared;
547
2e0172b1
N
548 /* It's much easier to list what array levels can't
549 * have a device disappear than all of them that can
550 */
48bc2ade 551 if (sra->array.level == 0 || sra->array.level == -1) {
f1661bd7 552 if (!st->err && !st->from_config)
04e27c20 553 alert("DeviceDisappeared", dev, " Wrong-Level", ainfo);
73ff0732 554 st->err++;
1830e74b 555 goto out;
2e0172b1 556 }
2e0172b1 557
2e0172b1
N
558 /* this array is in /proc/mdstat */
559 if (array.utime == 0)
560 /* external arrays don't update utime, so
561 * just make sure it is always different. */
562 array.utime = st->utime + 1;;
563
0f760384 564 if (st->err) {
73ff0732 565 /* New array appeared where previously had an error */
0f760384
N
566 st->err = 0;
567 st->percent = RESYNC_NONE;
721b662b 568 new_array = 1;
007087d0
MT
569 if (!is_container)
570 alert("NewArray", st->devname, NULL, ainfo);
0f760384
N
571 }
572
b8e5713c 573 if (st->utime == array.utime && st->failed == sra->array.failed_disks &&
e5eb6857 574 st->working == sra->array.working_disks &&
b98943a4 575 st->spare == sra->array.spare_disks &&
d7be7d87 576 (mse == NULL || (mse->percent == st->percent))) {
ff044d6b 577 if ((st->active < st->raid) && st->spare == 0)
1830e74b
JS
578 retval = 1;
579 goto out;
2e0172b1
N
580 }
581 if (st->utime == 0 && /* new array */
f27904a5 582 mse->pattern && strchr(mse->pattern, '_') /* degraded */)
e0bd6a96 583 alert("DegradedArray", dev, NULL, ainfo);
2e0172b1 584
b98943a4
JS
585 if (st->utime == 0 && /* new array */ st->expected_spares > 0 &&
586 sra->array.spare_disks < st->expected_spares)
e0bd6a96 587 alert("SparesMissing", dev, NULL, ainfo);
9dad51d4 588 if (st->percent < 0 && st->percent != RESYNC_UNKNOWN &&
2e0172b1 589 mse->percent >= 0)
e0bd6a96 590 alert("RebuildStarted", dev, NULL, ainfo);
f27904a5 591 if (st->percent >= 0 && mse->percent >= 0 &&
2e0172b1 592 (mse->percent / increments) > (st->percent / increments)) {
97589839 593 char percentalert[18];
f27904a5
JS
594 /*
595 * "RebuildNN" (10 chars) or "RebuildStarted" (15 chars)
596 */
2e0172b1
N
597
598 if((mse->percent / increments) == 0)
f27904a5
JS
599 snprintf(percentalert, sizeof(percentalert),
600 "RebuildStarted");
2e0172b1 601 else
f27904a5
JS
602 snprintf(percentalert, sizeof(percentalert),
603 "Rebuild%02d", mse->percent);
2e0172b1 604
e0bd6a96 605 alert(percentalert, dev, NULL, ainfo);
2e0172b1
N
606 }
607
f27904a5 608 if (mse->percent == RESYNC_NONE && st->percent >= 0) {
2e0172b1
N
609 /* Rebuild/sync/whatever just finished.
610 * If there is a number in /mismatch_cnt,
611 * we should report that.
612 */
2e0172b1 613 if (sra && sra->mismatch_cnt > 0) {
8453f8d0
LD
614 char cnt[80];
615 snprintf(cnt, sizeof(cnt),
616 " mismatches found: %d (on raid level %d)",
f566ef45 617 sra->mismatch_cnt, sra->array.level);
e0bd6a96 618 alert("RebuildFinished", dev, cnt, ainfo);
2e0172b1 619 } else
e0bd6a96 620 alert("RebuildFinished", dev, NULL, ainfo);
2e0172b1
N
621 }
622 st->percent = mse->percent;
623
b98943a4 624 remaining_disks = sra->array.nr_disks;
f27904a5 625 for (i = 0; i < MAX_DISKS && remaining_disks > 0; i++) {
2e0172b1
N
626 mdu_disk_info_t disc;
627 disc.number = i;
d97572f5 628 if (md_get_disk_info(fd, &disc) >= 0) {
2e0172b1
N
629 info[i].state = disc.state;
630 info[i].major = disc.major;
631 info[i].minor = disc.minor;
9e6d9291
N
632 if (disc.major || disc.minor)
633 remaining_disks --;
2e0172b1
N
634 } else
635 info[i].major = info[i].minor = 0;
636 }
9e6d9291 637 last_disk = i;
2e0172b1 638
4019ad07
JL
639 if (mse->metadata_version &&
640 strncmp(mse->metadata_version, "external:", 9) == 0 &&
4dd2df09
N
641 is_subarray(mse->metadata_version+9)) {
642 char *sl;
f566ef45 643 strcpy(st->parent_devnm, mse->metadata_version + 10);
4dd2df09
N
644 sl = strchr(st->parent_devnm, '/');
645 if (sl)
646 *sl = 0;
647 } else
648 st->parent_devnm[0] = 0;
f27904a5 649 if (st->metadata == NULL && st->parent_devnm[0] == 0)
2e0172b1
N
650 st->metadata = super_by_fd(fd, NULL);
651
f566ef45
JS
652 for (i = 0; i < MAX_DISKS; i++) {
653 mdu_disk_info_t disc = {0, 0, 0, 0, 0};
654 int newstate = 0;
2e0172b1
N
655 int change;
656 char *dv = NULL;
657 disc.number = i;
f27904a5 658 if (i < last_disk && (info[i].major || info[i].minor)) {
2e0172b1 659 newstate = info[i].state;
f27904a5
JS
660 dv = map_dev_preferred(info[i].major, info[i].minor, 1,
661 prefer);
2e0172b1
N
662 disc.state = newstate;
663 disc.major = info[i].major;
664 disc.minor = info[i].minor;
721b662b
N
665 } else
666 newstate = (1 << MD_DISK_REMOVED);
667
2e0172b1 668 if (dv == NULL && st->devid[i])
f27904a5
JS
669 dv = map_dev_preferred(major(st->devid[i]),
670 minor(st->devid[i]), 1, prefer);
2e0172b1 671 change = newstate ^ st->devstate[i];
721b662b 672 if (st->utime && change && !st->err && !new_array) {
f27904a5 673 if ((st->devstate[i]&change) & (1 << MD_DISK_SYNC))
e0bd6a96 674 alert("Fail", dev, dv, ainfo);
f27904a5 675 else if ((newstate & (1 << MD_DISK_FAULTY)) &&
2e0172b1 676 (disc.major || disc.minor) &&
f27904a5
JS
677 st->devid[i] == makedev(disc.major,
678 disc.minor))
e0bd6a96 679 alert("FailSpare", dev, dv, ainfo);
f27904a5 680 else if ((newstate&change) & (1 << MD_DISK_SYNC))
e0bd6a96 681 alert("SpareActive", dev, dv, ainfo);
2e0172b1
N
682 }
683 st->devstate[i] = newstate;
684 st->devid[i] = makedev(disc.major, disc.minor);
685 }
b98943a4 686 st->active = sra->array.active_disks;
e5eb6857 687 st->working = sra->array.working_disks;
b98943a4 688 st->spare = sra->array.spare_disks;
b8e5713c 689 st->failed = sra->array.failed_disks;
2e0172b1 690 st->utime = array.utime;
12a9d21f 691 st->raid = sra->array.raid_disks;
2e0172b1 692 st->err = 0;
a90e1050 693 if ((st->active < st->raid) && st->spare == 0)
1830e74b
JS
694 retval = 1;
695
696 out:
aed5f5c3
JS
697 if (sra)
698 sysfs_free(sra);
802961a2 699 if (fd >= 0)
13e5d845 700 close(fd);
1830e74b 701 return retval;
13e5d845
JS
702
703 disappeared:
007087d0 704 if (!st->err && !is_container)
13e5d845
JS
705 alert("DeviceDisappeared", dev, NULL, ainfo);
706 st->err++;
707 goto out;
2e0172b1
N
708}
709
83f3bc5f 710static int add_new_arrays(struct mdstat_ent *mdstat, struct state **statelist,
e0bd6a96 711 int test, struct alert_info *info)
2e0172b1
N
712{
713 struct mdstat_ent *mse;
714 int new_found = 0;
1e08717f 715 char *name;
2e0172b1 716
f566ef45 717 for (mse = mdstat; mse; mse = mse->next)
d7be7d87 718 if (mse->devnm[0] && (!mse->level || /* retrieve containers */
f566ef45
JS
719 (strcmp(mse->level, "raid0") != 0 &&
720 strcmp(mse->level, "linear") != 0))) {
503975b9 721 struct state *st = xcalloc(1, sizeof *st);
2e0172b1
N
722 mdu_array_info_t array;
723 int fd;
1e08717f
SV
724
725 name = get_md_name(mse->devnm);
726 if (!name) {
727 free(st);
728 continue;
729 }
730
731 st->devname = xstrdup(name);
2e0172b1 732 if ((fd = open(st->devname, O_RDONLY)) < 0 ||
9cd39f01 733 md_get_array_info(fd, &array) < 0) {
2e0172b1 734 /* no such array */
9cd39f01
JS
735 if (fd >= 0)
736 close(fd);
2e0172b1
N
737 put_md_name(st->devname);
738 free(st->devname);
739 if (st->metadata) {
740 st->metadata->ss->free_super(st->metadata);
741 free(st->metadata);
742 }
743 free(st);
744 continue;
745 }
746 close(fd);
83f3bc5f 747 st->next = *statelist;
2e0172b1 748 st->err = 1;
73ff0732 749 st->from_auto = 1;
4dd2df09 750 strcpy(st->devnm, mse->devnm);
9dad51d4 751 st->percent = RESYNC_UNKNOWN;
2e0172b1 752 st->expected_spares = -1;
eb28e119 753 if (mse->metadata_version &&
f566ef45
JS
754 strncmp(mse->metadata_version,
755 "external:", 9) == 0 &&
4dd2df09
N
756 is_subarray(mse->metadata_version+9)) {
757 char *sl;
758 strcpy(st->parent_devnm,
759 mse->metadata_version+10);
760 sl = strchr(st->parent_devnm, '/');
761 *sl = 0;
762 } else
763 st->parent_devnm[0] = 0;
83f3bc5f 764 *statelist = st;
2e0172b1 765 if (test)
e0bd6a96 766 alert("TestMessage", st->devname, NULL, info);
2e0172b1
N
767 new_found = 1;
768 }
769 return new_found;
770}
771
fbfdcb06
AO
772static int get_required_spare_criteria(struct state *st,
773 struct spare_criteria *sc)
80e7f8c3
AC
774{
775 int fd;
80e7f8c3 776
f566ef45 777 if (!st->metadata || !st->metadata->ss->get_spare_criteria) {
fbfdcb06 778 sc->min_size = 0;
4b57ecf6 779 sc->sector_size = 0;
de697acc 780 return 0;
300f5033 781 }
80e7f8c3
AC
782
783 fd = open(st->devname, O_RDONLY);
784 if (fd < 0)
de697acc
AC
785 return 1;
786 if (st->metadata->ss->external)
787 st->metadata->ss->load_container(st->metadata, fd, st->devname);
788 else
789 st->metadata->ss->load_super(st->metadata, fd, st->devname);
80e7f8c3 790 close(fd);
de697acc
AC
791 if (!st->metadata->sb)
792 return 1;
fbfdcb06
AO
793
794 st->metadata->ss->get_spare_criteria(st->metadata, sc);
80e7f8c3
AC
795 st->metadata->ss->free_super(st->metadata);
796
de697acc 797 return 0;
80e7f8c3
AC
798}
799
5ec0f373 800static int check_donor(struct state *from, struct state *to)
2feb22ef 801{
66f5c4b6
N
802 struct state *sub;
803
2feb22ef
N
804 if (from == to)
805 return 0;
66f5c4b6
N
806 if (from->parent)
807 /* Cannot move from a member */
2feb22ef 808 return 0;
ff044d6b
AC
809 if (from->err)
810 return 0;
66f5c4b6
N
811 for (sub = from->subarray; sub; sub = sub->subarray)
812 /* If source array has degraded subarrays, don't
813 * remove anything
814 */
815 if (sub->active < sub->raid)
816 return 0;
817 if (from->metadata->ss->external == 0)
818 if (from->active < from->raid)
819 return 0;
2feb22ef
N
820 if (from->spare <= 0)
821 return 0;
e78dda3b 822 return 1;
2feb22ef
N
823}
824
0f0749ad 825static dev_t choose_spare(struct state *from, struct state *to,
f566ef45 826 struct domainlist *domlist, struct spare_criteria *sc)
0fa21e85
N
827{
828 int d;
0f0749ad 829 dev_t dev = 0;
0fa21e85 830
b0599bda 831 for (d = from->raid; !dev && d < MAX_DISKS; d++) {
f566ef45 832 if (from->devid[d] > 0 && from->devstate[d] == 0) {
0fa21e85
N
833 struct dev_policy *pol;
834 unsigned long long dev_size;
4b57ecf6 835 unsigned int dev_sector_size;
0fa21e85 836
bfd76b93
CA
837 if (to->metadata->ss->external &&
838 test_partition_from_id(from->devid[d]))
839 continue;
840
fbfdcb06 841 if (sc->min_size &&
0fa21e85 842 dev_size_from_id(from->devid[d], &dev_size) &&
fbfdcb06 843 dev_size < sc->min_size)
0fa21e85
N
844 continue;
845
4b57ecf6
AO
846 if (sc->sector_size &&
847 dev_sector_size_from_id(from->devid[d],
848 &dev_sector_size) &&
849 sc->sector_size != dev_sector_size)
850 continue;
851
4dd2df09 852 pol = devid_policy(from->devid[d]);
0fa21e85
N
853 if (from->spare_group)
854 pol_add(&pol, pol_domain,
855 from->spare_group, NULL);
f566ef45
JS
856 if (domain_test(domlist, pol,
857 to->metadata->ss->name) == 1)
0fa21e85
N
858 dev = from->devid[d];
859 dev_policy_free(pol);
860 }
861 }
862 return dev;
863}
864
0f0749ad 865static dev_t container_choose_spare(struct state *from, struct state *to,
f0b85306 866 struct domainlist *domlist,
fbfdcb06 867 struct spare_criteria *sc, int active)
5739e0d0
N
868{
869 /* This is similar to choose_spare, but we cannot trust devstate,
870 * so we need to read the metadata instead
871 */
326727d9 872 struct mdinfo *list;
5739e0d0 873 struct supertype *st = from->metadata;
ff044d6b 874 int fd = open(from->devname, O_RDONLY);
5739e0d0 875 int err;
0f0749ad 876 dev_t dev = 0;
5739e0d0
N
877
878 if (fd < 0)
879 return 0;
326727d9
AC
880 if (!st->ss->getinfo_super_disks) {
881 close(fd);
5739e0d0 882 return 0;
326727d9 883 }
1011e834 884
5739e0d0
N
885 err = st->ss->load_container(st, fd, NULL);
886 close(fd);
887 if (err)
888 return 0;
1011e834 889
a1e49d69
CA
890 if (from == to) {
891 /* We must check if number of active disks has not increased
892 * since ioctl in main loop. mdmon may have added spare
893 * to subarray. If so we do not need to look for more spares
894 * so return non zero value */
895 int active_cnt = 0;
896 struct mdinfo *dp;
897 list = st->ss->getinfo_super_disks(st);
898 if (!list) {
899 st->ss->free_super(st);
900 return 1;
901 }
902 dp = list->devs;
903 while (dp) {
f566ef45
JS
904 if (dp->disk.state & (1 << MD_DISK_SYNC) &&
905 !(dp->disk.state & (1 << MD_DISK_FAULTY)))
a1e49d69
CA
906 active_cnt++;
907 dp = dp->next;
908 }
909 sysfs_free(list);
910 if (active < active_cnt) {
911 /* Spare just activated.*/
912 st->ss->free_super(st);
913 return 1;
914 }
915 }
916
326727d9 917 /* We only need one spare so full list not needed */
fbfdcb06 918 list = container_choose_spares(st, sc, domlist, from->spare_group,
326727d9
AC
919 to->metadata->ss->name, 1);
920 if (list) {
921 struct mdinfo *disks = list->devs;
922 if (disks)
923 dev = makedev(disks->disk.major, disks->disk.minor);
924 sysfs_free(list);
5739e0d0 925 }
326727d9 926 st->ss->free_super(st);
5739e0d0
N
927 return dev;
928}
929
e0bd6a96 930static void try_spare_migration(struct state *statelist, struct alert_info *info)
2e0172b1 931{
66f5c4b6
N
932 struct state *from;
933 struct state *st;
fbfdcb06 934 struct spare_criteria sc;
c3621c0a
ML
935
936 link_containers_with_subarrays(statelist);
66f5c4b6 937 for (st = statelist; st; st = st->next)
f566ef45 938 if (st->active < st->raid && st->spare == 0 && !st->err) {
e78dda3b
N
939 struct domainlist *domlist = NULL;
940 int d;
66f5c4b6
N
941 struct state *to = st;
942
4dd2df09 943 if (to->parent_devnm[0] && !to->parent)
c0dc0ad5
CA
944 /* subarray monitored without parent container
945 * we can't move spares here */
946 continue;
1011e834 947
66f5c4b6
N
948 if (to->parent)
949 /* member of a container */
950 to = to->parent;
e78dda3b 951
fbfdcb06 952 if (get_required_spare_criteria(to, &sc))
de697acc 953 continue;
e9a2ac02
AC
954 if (to->metadata->ss->external) {
955 /* We must make sure there is
956 * no suitable spare in container already.
957 * If there is we don't add more */
958 dev_t devid = container_choose_spare(
fbfdcb06 959 to, to, NULL, &sc, st->active);
e9a2ac02
AC
960 if (devid > 0)
961 continue;
962 }
b0599bda 963 for (d = 0; d < MAX_DISKS; d++)
e78dda3b
N
964 if (to->devid[d])
965 domainlist_add_dev(&domlist,
966 to->devid[d],
967 to->metadata->ss->name);
968 if (to->spare_group)
969 domain_add(&domlist, to->spare_group);
5ec0f373
ML
970 /*
971 * No spare migration if the destination
972 * has no domain. Skip this array.
973 */
974 if (!domlist)
975 continue;
0fa21e85 976 for (from=statelist ; from ; from=from->next) {
0f0749ad 977 dev_t devid;
5ec0f373 978 if (!check_donor(from, to))
0fa21e85 979 continue;
5739e0d0
N
980 if (from->metadata->ss->external)
981 devid = container_choose_spare(
fbfdcb06 982 from, to, domlist, &sc, 0);
5739e0d0 983 else
f0b85306 984 devid = choose_spare(from, to, domlist,
fbfdcb06 985 &sc);
f566ef45
JS
986 if (devid > 0 &&
987 move_spare(from->devname, to->devname,
988 devid)) {
989 alert("MoveSpare", to->devname,
990 from->devname, info);
d52bb542
AC
991 break;
992 }
0fa21e85 993 }
e78dda3b 994 domain_free(domlist);
2e0172b1
N
995 }
996}
c3621c0a
ML
997
998/* search the statelist to connect external
999 * metadata subarrays with their containers
1000 * We always completely rebuild the tree from scratch as
1001 * that is safest considering the possibility of entries
1002 * disappearing or changing.
1003 */
1004static void link_containers_with_subarrays(struct state *list)
1005{
1006 struct state *st;
1007 struct state *cont;
1008 for (st = list; st; st = st->next) {
1009 st->parent = NULL;
1010 st->subarray = NULL;
1011 }
1012 for (st = list; st; st = st->next)
4dd2df09 1013 if (st->parent_devnm[0])
c3621c0a 1014 for (cont = list; cont; cont = cont->next)
f566ef45 1015 if (!cont->err && cont->parent_devnm[0] == 0 &&
4dd2df09 1016 strcmp(cont->devnm, st->parent_devnm) == 0) {
c3621c0a
ML
1017 st->parent = cont;
1018 st->subarray = cont->subarray;
1019 cont->subarray = st;
1020 break;
1021 }
1022}
1023
b90c0e9a
NB
1024/* Not really Monitor but ... */
1025int Wait(char *dev)
1026{
4dd2df09 1027 char devnm[32];
cb91230c
TM
1028 dev_t rdev;
1029 char *tmp;
b90c0e9a 1030 int rv = 1;
d3f6cf4f 1031 int frozen_remaining = 3;
b90c0e9a 1032
cb91230c 1033 if (!stat_is_blkdev(dev, &rdev))
b90c0e9a 1034 return 2;
cb91230c
TM
1035
1036 tmp = devid2devnm(rdev);
1037 if (!tmp) {
1038 pr_err("Cannot get md device name.\n");
1039 return 2;
1040 }
1041
1042 strcpy(devnm, tmp);
b90c0e9a
NB
1043
1044 while(1) {
1045 struct mdstat_ent *ms = mdstat_read(1, 0);
1046 struct mdstat_ent *e;
1047
f566ef45 1048 for (e = ms; e; e = e->next)
4dd2df09 1049 if (strcmp(e->devnm, devnm) == 0)
b90c0e9a
NB
1050 break;
1051
d3f6cf4f 1052 if (e && e->percent == RESYNC_NONE) {
276be514
N
1053 /* We could be in the brief pause before something
1054 * starts. /proc/mdstat doesn't show that, but
1055 * sync_action does.
1056 */
1057 struct mdinfo mdi;
1058 char buf[21];
dae13137
JS
1059
1060 if (sysfs_init(&mdi, -1, devnm))
1061 return 2;
276be514
N
1062 if (sysfs_get_str(&mdi, NULL, "sync_action",
1063 buf, 20) > 0 &&
d3f6cf4f 1064 strcmp(buf,"idle\n") != 0) {
276be514 1065 e->percent = RESYNC_UNKNOWN;
d3f6cf4f
N
1066 if (strcmp(buf, "frozen\n") == 0) {
1067 if (frozen_remaining == 0)
1068 e->percent = RESYNC_NONE;
1069 else
1070 frozen_remaining -= 1;
1071 }
1072 }
276be514 1073 }
639c3c10 1074 if (!e || e->percent == RESYNC_NONE) {
e7783ee6 1075 if (e && e->metadata_version &&
c94709e8
DW
1076 strncmp(e->metadata_version, "external:", 9) == 0) {
1077 if (is_subarray(&e->metadata_version[9]))
1078 ping_monitor(&e->metadata_version[9]);
1079 else
4dd2df09 1080 ping_monitor(devnm);
c94709e8 1081 }
b90c0e9a
NB
1082 free_mdstat(ms);
1083 return rv;
1084 }
89a10d84 1085 free_mdstat(ms);
b90c0e9a
NB
1086 rv = 0;
1087 mdstat_wait(5);
1088 }
1089}
679eb882 1090
43ebc910
GP
1091/* The state "broken" is used only for RAID0/LINEAR - it's the same as
1092 * "clean", but used in case the array has one or more members missing.
1093 */
679eb882 1094static char *clean_states[] = {
43ebc910 1095 "clear", "inactive", "readonly", "read-auto", "clean", "broken", NULL };
679eb882 1096
1ea04629 1097int WaitClean(char *dev, int verbose)
679eb882
N
1098{
1099 int fd;
1100 struct mdinfo *mdi;
1101 int rv = 1;
4dd2df09 1102 char devnm[32];
679eb882 1103
9e04ac1c
ZL
1104 if (!stat_is_blkdev(dev, NULL))
1105 return 2;
679eb882
N
1106 fd = open(dev, O_RDONLY);
1107 if (fd < 0) {
1108 if (verbose)
e7b84f9d 1109 pr_err("Couldn't open %s: %s\n", dev, strerror(errno));
679eb882
N
1110 return 1;
1111 }
1112
4dd2df09
N
1113 strcpy(devnm, fd2devnm(fd));
1114 mdi = sysfs_read(fd, devnm, GET_VERSION|GET_LEVEL|GET_SAFEMODE);
679eb882
N
1115 if (!mdi) {
1116 if (verbose)
7a862a02 1117 pr_err("Failed to read sysfs attributes for %s\n", dev);
679eb882
N
1118 close(fd);
1119 return 0;
1120 }
1121
1122 switch(mdi->array.level) {
1123 case LEVEL_LINEAR:
1124 case LEVEL_MULTIPATH:
1125 case 0:
1126 /* safemode delay is irrelevant for these levels */
1127 rv = 0;
1128 }
1129
1130 /* for internal metadata the kernel handles the final clean
1131 * transition, containers can never be dirty
1132 */
1133 if (!is_subarray(mdi->text_version))
1134 rv = 0;
1135
1136 /* safemode disabled ? */
1137 if (mdi->safe_mode_delay == 0)
1138 rv = 0;
1139
1140 if (rv) {
4dd2df09 1141 int state_fd = sysfs_open(fd2devnm(fd), NULL, "array_state");
679eb882 1142 char buf[20];
efc67e8e 1143 int delay = 5000;
679eb882
N
1144
1145 /* minimize the safe_mode_delay and prepare to wait up to 5s
1146 * for writes to quiesce
1147 */
1148 sysfs_set_safemode(mdi, 1);
679eb882
N
1149
1150 /* wait for array_state to be clean */
1151 while (1) {
1152 rv = read(state_fd, buf, sizeof(buf));
1153 if (rv < 0)
1154 break;
43ebc910
GP
1155 if (sysfs_match_word(buf, clean_states) <
1156 (int)ARRAY_SIZE(clean_states) - 1)
679eb882 1157 break;
efc67e8e 1158 rv = sysfs_wait(state_fd, &delay);
679eb882
N
1159 if (rv < 0 && errno != EINTR)
1160 break;
1161 lseek(state_fd, 0, SEEK_SET);
1162 }
1163 if (rv < 0)
1164 rv = 1;
1ea04629 1165 else if (ping_monitor(mdi->text_version) == 0) {
679eb882
N
1166 /* we need to ping to close the window between array
1167 * state transitioning to clean and the metadata being
1168 * marked clean
1169 */
1170 rv = 0;
1ea04629 1171 } else {
679eb882 1172 rv = 1;
1ea04629
MT
1173 pr_err("Error connecting monitor with %s\n", dev);
1174 }
679eb882 1175 if (rv && verbose)
f566ef45 1176 pr_err("Error waiting for %s to be clean\n", dev);
679eb882
N
1177
1178 /* restore the original safe_mode_delay */
1179 sysfs_set_safemode(mdi, mdi->safe_mode_delay);
1180 close(state_fd);
1181 }
1182
1183 sysfs_free(mdi);
1184 close(fd);
1185
1186 return rv;
1187}