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