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