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