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