]> git.ipfire.org Git - thirdparty/mdadm.git/blame - Monitor.c
FIX: resolve make everything compilation error
[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];
0011874f 297 char path[100];
2e0172b1 298 struct stat buf;
0011874f
JS
299 sprintf(path, "%s/autorebuild.pid", MDMON_DIR);
300 fp = fopen(path, "r");
2e0172b1 301 if (fp) {
71204a50
N
302 if (fscanf(fp, "%d", &pid) != 1)
303 pid = -1;
2e0172b1
N
304 sprintf(dir, "/proc/%d", pid);
305 rv = stat(dir, &buf);
306 if (rv != -1) {
307 if (scan) {
308 fprintf(stderr, Name ": Only one "
309 "autorebuild process allowed"
310 " in scan mode, aborting\n");
311 fclose(fp);
312 return 1;
313 } else {
314 fprintf(stderr, Name ": Warning: One"
315 " autorebuild process already"
a1bb2065 316 " running.\n");
2e0172b1
N
317 }
318 }
319 fclose(fp);
320 }
321 if (scan) {
0011874f 322 if (mkdir(MDMON_DIR, S_IRWXU) < 0 &&
a1bb2065
AC
323 errno != EEXIST) {
324 fprintf(stderr, Name ": Can't create "
325 "autorebuild.pid file\n");
326 } else {
0011874f 327 fp = fopen(path, "w");
a1bb2065
AC
328 if (!fp)
329 fprintf(stderr, Name ": Cannot create"
330 " autorebuild.pid"
331 "file\n");
332 else {
333 pid = getpid();
334 fprintf(fp, "%d\n", pid);
335 fclose(fp);
336 }
2e0172b1
N
337 }
338 }
339 return 0;
340}
52826846 341
e0bd6a96 342static void alert(char *event, char *dev, char *disc, struct alert_info *info)
52826846 343{
773135f5
NB
344 int priority;
345
b1bac75b 346 if (!info->alert_cmd && !info->mailaddr && !info->dosyslog) {
cd29a5c8 347 time_t now = time(0);
aba69144 348
e0d19036 349 printf("%1.15s: %s on %s %s\n", ctime(&now)+4, event, dev, disc?disc:"unknown device");
cd29a5c8 350 }
e0bd6a96 351 if (info->alert_cmd) {
52826846
NB
352 int pid = fork();
353 switch(pid) {
354 default:
355 waitpid(pid, NULL, 0);
356 break;
357 case -1:
358 break;
359 case 0:
e0bd6a96
N
360 execl(info->alert_cmd, info->alert_cmd,
361 event, dev, disc, NULL);
52826846
NB
362 exit(2);
363 }
364 }
e0bd6a96 365 if (info->mailaddr &&
aba69144 366 (strncmp(event, "Fail", 4)==0 ||
98c6faba 367 strncmp(event, "Test", 4)==0 ||
d1732eeb 368 strncmp(event, "Spares", 6)==0 ||
aa88f531 369 strncmp(event, "Degrade", 7)==0)) {
52826846
NB
370 FILE *mp = popen(Sendmail, "w");
371 if (mp) {
008e1100 372 FILE *mdstat;
52826846
NB
373 char hname[256];
374 gethostname(hname, sizeof(hname));
375 signal(SIGPIPE, SIG_IGN);
e0bd6a96
N
376 if (info->mailfrom)
377 fprintf(mp, "From: %s\n", info->mailfrom);
4948b8f7
NB
378 else
379 fprintf(mp, "From: " Name " monitoring <root>\n");
e0bd6a96
N
380 fprintf(mp, "To: %s\n", info->mailaddr);
381 fprintf(mp, "Subject: %s event on %s:%s\n\n",
382 event, dev, hname);
52826846 383
e0bd6a96
N
384 fprintf(mp,
385 "This is an automatically generated"
386 " mail message from " Name "\n");
52826846
NB
387 fprintf(mp, "running on %s\n\n", hname);
388
e0bd6a96
N
389 fprintf(mp,
390 "A %s event had been detected on"
391 " md device %s.\n\n", event, dev);
52826846 392
37dfc3d6 393 if (disc && disc[0] != ' ')
e0bd6a96
N
394 fprintf(mp,
395 "It could be related to"
396 " component device %s.\n\n", disc);
37dfc3d6
NB
397 if (disc && disc[0] == ' ')
398 fprintf(mp, "Extra information:%s.\n\n", disc);
52826846
NB
399
400 fprintf(mp, "Faithfully yours, etc.\n");
008e1100
NB
401
402 mdstat = fopen("/proc/mdstat", "r");
403 if (mdstat) {
404 char buf[8192];
405 int n;
e0bd6a96
N
406 fprintf(mp,
407 "\nP.S. The /proc/mdstat file"
408 " currently contains the following:\n\n");
008e1100 409 while ( (n=fread(buf, 1, sizeof(buf), mdstat)) > 0)
e0bd6a96 410 n=fwrite(buf, 1, n, mp);
008e1100
NB
411 fclose(mdstat);
412 }
6278fb3a 413 pclose(mp);
52826846 414 }
52826846 415 }
773135f5
NB
416
417 /* log the event to syslog maybe */
e0bd6a96 418 if (info->dosyslog) {
773135f5
NB
419 /* Log at a different severity depending on the event.
420 *
421 * These are the critical events: */
422 if (strncmp(event, "Fail", 4)==0 ||
423 strncmp(event, "Degrade", 7)==0 ||
424 strncmp(event, "DeviceDisappeared", 17)==0)
425 priority = LOG_CRIT;
426 /* Good to know about, but are not failures: */
427 else if (strncmp(event, "Rebuild", 7)==0 ||
d1732eeb
NB
428 strncmp(event, "MoveSpare", 9)==0 ||
429 strncmp(event, "Spares", 6) != 0)
773135f5
NB
430 priority = LOG_WARNING;
431 /* Everything else: */
432 else
433 priority = LOG_INFO;
434
435 if (disc)
e0bd6a96
N
436 syslog(priority,
437 "%s event detected on md device %s,"
438 " component device %s", event, dev, disc);
773135f5 439 else
e0bd6a96
N
440 syslog(priority,
441 "%s event detected on md device %s",
442 event, dev);
773135f5 443 }
52826846 444}
b90c0e9a 445
a90e1050 446static int check_array(struct state *st, struct mdstat_ent *mdstat,
e0bd6a96 447 int test, struct alert_info *ainfo,
a90e1050 448 int increments)
2e0172b1 449{
ff044d6b
AC
450 /* Update the state 'st' to reflect any changes shown in mdstat,
451 * or found by directly examining the array, and return
452 * '1' if the array is degraded, or '0' if it is optimal (or dead).
453 */
2e0172b1
N
454 struct { int state, major, minor; } info[MaxDisks];
455 mdu_array_info_t array;
456 struct mdstat_ent *mse = NULL, *mse2;
457 char *dev = st->devname;
458 int fd;
459 int i;
9e6d9291
N
460 int remaining_disks;
461 int last_disk;
2e0172b1
N
462
463 if (test)
e0bd6a96 464 alert("TestMessage", dev, NULL, ainfo);
2e0172b1
N
465 fd = open(dev, O_RDONLY);
466 if (fd < 0) {
467 if (!st->err)
e0bd6a96
N
468 alert("DeviceDisappeared", dev, NULL, ainfo);
469 st->err=1;
a90e1050 470 return 0;
2e0172b1
N
471 }
472 fcntl(fd, F_SETFD, FD_CLOEXEC);
473 if (ioctl(fd, GET_ARRAY_INFO, &array)<0) {
474 if (!st->err)
e0bd6a96
N
475 alert("DeviceDisappeared", dev, NULL, ainfo);
476 st->err=1;
2e0172b1 477 close(fd);
a90e1050 478 return 0;
2e0172b1
N
479 }
480 /* It's much easier to list what array levels can't
481 * have a device disappear than all of them that can
482 */
483 if (array.level == 0 || array.level == -1) {
484 if (!st->err)
e0bd6a96 485 alert("DeviceDisappeared", dev, "Wrong-Level", ainfo);
2e0172b1
N
486 st->err = 1;
487 close(fd);
a90e1050 488 return 0;
2e0172b1
N
489 }
490 if (st->devnum == INT_MAX) {
491 struct stat stb;
492 if (fstat(fd, &stb) == 0 &&
493 (S_IFMT&stb.st_mode)==S_IFBLK) {
494 if (major(stb.st_rdev) == MD_MAJOR)
495 st->devnum = minor(stb.st_rdev);
496 else
497 st->devnum = -1- (minor(stb.st_rdev)>>6);
498 }
499 }
500
501 for (mse2 = mdstat ; mse2 ; mse2=mse2->next)
502 if (mse2->devnum == st->devnum) {
503 mse2->devnum = INT_MAX; /* flag it as "used" */
504 mse = mse2;
505 }
506
507 if (!mse) {
508 /* duplicated array in statelist
509 * or re-created after reading mdstat*/
510 st->err = 1;
511 close(fd);
a90e1050 512 return 0;
2e0172b1
N
513 }
514 /* this array is in /proc/mdstat */
515 if (array.utime == 0)
516 /* external arrays don't update utime, so
517 * just make sure it is always different. */
518 array.utime = st->utime + 1;;
519
520 if (st->utime == array.utime &&
521 st->failed == array.failed_disks &&
522 st->working == array.working_disks &&
523 st->spare == array.spare_disks &&
524 (mse == NULL || (
525 mse->percent == st->percent
526 ))) {
527 close(fd);
528 st->err = 0;
ff044d6b
AC
529 if ((st->active < st->raid) && st->spare == 0)
530 return 1;
531 else
532 return 0;
2e0172b1
N
533 }
534 if (st->utime == 0 && /* new array */
535 mse->pattern && strchr(mse->pattern, '_') /* degraded */
536 )
e0bd6a96 537 alert("DegradedArray", dev, NULL, ainfo);
2e0172b1
N
538
539 if (st->utime == 0 && /* new array */
540 st->expected_spares > 0 &&
541 array.spare_disks < st->expected_spares)
e0bd6a96 542 alert("SparesMissing", dev, NULL, ainfo);
2e0172b1
N
543 if (st->percent == -1 &&
544 mse->percent >= 0)
e0bd6a96 545 alert("RebuildStarted", dev, NULL, ainfo);
2e0172b1
N
546 if (st->percent >= 0 &&
547 mse->percent >= 0 &&
548 (mse->percent / increments) > (st->percent / increments)) {
549 char percentalert[15]; // "RebuildNN" (10 chars) or "RebuildStarted" (15 chars)
550
551 if((mse->percent / increments) == 0)
552 snprintf(percentalert, sizeof(percentalert), "RebuildStarted");
553 else
554 snprintf(percentalert, sizeof(percentalert), "Rebuild%02d", mse->percent);
555
e0bd6a96 556 alert(percentalert, dev, NULL, ainfo);
2e0172b1
N
557 }
558
559 if (mse->percent == -1 &&
560 st->percent >= 0) {
561 /* Rebuild/sync/whatever just finished.
562 * If there is a number in /mismatch_cnt,
563 * we should report that.
564 */
565 struct mdinfo *sra =
566 sysfs_read(-1, st->devnum, GET_MISMATCH);
567 if (sra && sra->mismatch_cnt > 0) {
8453f8d0
LD
568 char cnt[80];
569 snprintf(cnt, sizeof(cnt),
570 " mismatches found: %d (on raid level %d)",
d97a5e60 571 sra->mismatch_cnt, array.level);
e0bd6a96 572 alert("RebuildFinished", dev, cnt, ainfo);
2e0172b1 573 } else
e0bd6a96 574 alert("RebuildFinished", dev, NULL, ainfo);
2e0172b1
N
575 if (sra)
576 free(sra);
577 }
578 st->percent = mse->percent;
579
9e6d9291
N
580 remaining_disks = array.nr_disks;
581 for (i=0; i<MaxDisks && remaining_disks > 0;
2e0172b1
N
582 i++) {
583 mdu_disk_info_t disc;
584 disc.number = i;
585 if (ioctl(fd, GET_DISK_INFO, &disc) >= 0) {
586 info[i].state = disc.state;
587 info[i].major = disc.major;
588 info[i].minor = disc.minor;
9e6d9291
N
589 if (disc.major || disc.minor)
590 remaining_disks --;
2e0172b1
N
591 } else
592 info[i].major = info[i].minor = 0;
593 }
9e6d9291 594 last_disk = i;
2e0172b1 595
4019ad07
JL
596 if (mse->metadata_version &&
597 strncmp(mse->metadata_version, "external:", 9) == 0 &&
2e0172b1
N
598 is_subarray(mse->metadata_version+9))
599 st->parent_dev =
600 devname2devnum(mse->metadata_version+10);
601 else
602 st->parent_dev = NoMdDev;
603 if (st->metadata == NULL &&
604 st->parent_dev == NoMdDev)
605 st->metadata = super_by_fd(fd, NULL);
606
607 close(fd);
608
609 for (i=0; i<MaxDisks; i++) {
610 mdu_disk_info_t disc = {0,0,0,0,0};
611 int newstate=0;
612 int change;
613 char *dv = NULL;
614 disc.number = i;
9e6d9291 615 if (i >= last_disk) {
2e0172b1
N
616 newstate = 0;
617 disc.major = disc.minor = 0;
618 } else if (info[i].major || info[i].minor) {
619 newstate = info[i].state;
620 dv = map_dev(info[i].major, info[i].minor, 1);
621 disc.state = newstate;
622 disc.major = info[i].major;
623 disc.minor = info[i].minor;
624 } else if (mse && mse->pattern && i < (int)strlen(mse->pattern)) {
625 switch(mse->pattern[i]) {
626 case 'U': newstate = 6 /* ACTIVE/SYNC */; break;
38376c2e 627 case '_': newstate = 8 /* REMOVED */; break;
2e0172b1
N
628 }
629 disc.major = disc.minor = 0;
630 }
631 if (dv == NULL && st->devid[i])
632 dv = map_dev(major(st->devid[i]),
633 minor(st->devid[i]), 1);
634 change = newstate ^ st->devstate[i];
635 if (st->utime && change && !st->err) {
636 if (i < array.raid_disks &&
637 (((newstate&change)&(1<<MD_DISK_FAULTY)) ||
638 ((st->devstate[i]&change)&(1<<MD_DISK_ACTIVE)) ||
639 ((st->devstate[i]&change)&(1<<MD_DISK_SYNC)))
640 )
e0bd6a96 641 alert("Fail", dev, dv, ainfo);
2e0172b1
N
642 else if (i >= array.raid_disks &&
643 (disc.major || disc.minor) &&
644 st->devid[i] == makedev(disc.major, disc.minor) &&
645 ((newstate&change)&(1<<MD_DISK_FAULTY))
646 )
e0bd6a96 647 alert("FailSpare", dev, dv, ainfo);
2e0172b1
N
648 else if (i < array.raid_disks &&
649 ! (newstate & (1<<MD_DISK_REMOVED)) &&
650 (((st->devstate[i]&change)&(1<<MD_DISK_FAULTY)) ||
651 ((newstate&change)&(1<<MD_DISK_ACTIVE)) ||
652 ((newstate&change)&(1<<MD_DISK_SYNC)))
653 )
e0bd6a96 654 alert("SpareActive", dev, dv, ainfo);
2e0172b1
N
655 }
656 st->devstate[i] = newstate;
657 st->devid[i] = makedev(disc.major, disc.minor);
658 }
659 st->active = array.active_disks;
660 st->working = array.working_disks;
661 st->spare = array.spare_disks;
662 st->failed = array.failed_disks;
663 st->utime = array.utime;
664 st->raid = array.raid_disks;
665 st->err = 0;
a90e1050
N
666 if ((st->active < st->raid) && st->spare == 0)
667 return 1;
668 return 0;
2e0172b1
N
669}
670
83f3bc5f 671static int add_new_arrays(struct mdstat_ent *mdstat, struct state **statelist,
e0bd6a96 672 int test, struct alert_info *info)
2e0172b1
N
673{
674 struct mdstat_ent *mse;
675 int new_found = 0;
676
677 for (mse=mdstat; mse; mse=mse->next)
678 if (mse->devnum != INT_MAX &&
679 (!mse->level || /* retrieve containers */
680 (strcmp(mse->level, "raid0") != 0 &&
681 strcmp(mse->level, "linear") != 0))
682 ) {
9bfc6a7d 683 struct state *st = calloc(1, sizeof *st);
2e0172b1
N
684 mdu_array_info_t array;
685 int fd;
686 if (st == NULL)
687 continue;
688 st->devname = strdup(get_md_name(mse->devnum));
689 if ((fd = open(st->devname, O_RDONLY)) < 0 ||
690 ioctl(fd, GET_ARRAY_INFO, &array)< 0) {
691 /* no such array */
692 if (fd >=0) close(fd);
693 put_md_name(st->devname);
694 free(st->devname);
695 if (st->metadata) {
696 st->metadata->ss->free_super(st->metadata);
697 free(st->metadata);
698 }
699 free(st);
700 continue;
701 }
702 close(fd);
83f3bc5f 703 st->next = *statelist;
2e0172b1
N
704 st->err = 1;
705 st->devnum = mse->devnum;
706 st->percent = -2;
2e0172b1 707 st->expected_spares = -1;
eb28e119
N
708 if (mse->metadata_version &&
709 strncmp(mse->metadata_version, "external:", 9) == 0 &&
2e0172b1
N
710 is_subarray(mse->metadata_version+9))
711 st->parent_dev =
712 devname2devnum(mse->metadata_version+10);
713 else
714 st->parent_dev = NoMdDev;
83f3bc5f 715 *statelist = st;
2e0172b1 716 if (test)
e0bd6a96
N
717 alert("TestMessage", st->devname, NULL, info);
718 alert("NewArray", st->devname, NULL, info);
2e0172b1
N
719 new_found = 1;
720 }
721 return new_found;
722}
723
de697acc 724static int get_min_spare_size_required(struct state *st, unsigned long long *sizep)
80e7f8c3
AC
725{
726 int fd;
80e7f8c3
AC
727
728 if (!st->metadata ||
300f5033
CA
729 !st->metadata->ss->min_acceptable_spare_size) {
730 *sizep = 0;
de697acc 731 return 0;
300f5033 732 }
80e7f8c3
AC
733
734 fd = open(st->devname, O_RDONLY);
735 if (fd < 0)
de697acc
AC
736 return 1;
737 if (st->metadata->ss->external)
738 st->metadata->ss->load_container(st->metadata, fd, st->devname);
739 else
740 st->metadata->ss->load_super(st->metadata, fd, st->devname);
80e7f8c3 741 close(fd);
de697acc
AC
742 if (!st->metadata->sb)
743 return 1;
744 *sizep = st->metadata->ss->min_acceptable_spare_size(st->metadata);
80e7f8c3
AC
745 st->metadata->ss->free_super(st->metadata);
746
de697acc 747 return 0;
80e7f8c3
AC
748}
749
5ec0f373 750static int check_donor(struct state *from, struct state *to)
2feb22ef 751{
66f5c4b6
N
752 struct state *sub;
753
2feb22ef
N
754 if (from == to)
755 return 0;
66f5c4b6
N
756 if (from->parent)
757 /* Cannot move from a member */
2feb22ef 758 return 0;
ff044d6b
AC
759 if (from->err)
760 return 0;
66f5c4b6
N
761 for (sub = from->subarray; sub; sub = sub->subarray)
762 /* If source array has degraded subarrays, don't
763 * remove anything
764 */
765 if (sub->active < sub->raid)
766 return 0;
767 if (from->metadata->ss->external == 0)
768 if (from->active < from->raid)
769 return 0;
2feb22ef
N
770 if (from->spare <= 0)
771 return 0;
e78dda3b 772 return 1;
2feb22ef
N
773}
774
0f0749ad 775static dev_t choose_spare(struct state *from, struct state *to,
f0b85306 776 struct domainlist *domlist, unsigned long long min_size)
0fa21e85
N
777{
778 int d;
0f0749ad 779 dev_t dev = 0;
0fa21e85
N
780
781 for (d = from->raid; !dev && d < MaxDisks; d++) {
782 if (from->devid[d] > 0 &&
783 from->devstate[d] == 0) {
784 struct dev_policy *pol;
785 unsigned long long dev_size;
786
bfd76b93
CA
787 if (to->metadata->ss->external &&
788 test_partition_from_id(from->devid[d]))
789 continue;
790
0fa21e85
N
791 if (min_size &&
792 dev_size_from_id(from->devid[d], &dev_size) &&
793 dev_size < min_size)
794 continue;
795
796 pol = devnum_policy(from->devid[d]);
797 if (from->spare_group)
798 pol_add(&pol, pol_domain,
799 from->spare_group, NULL);
e5508b36 800 if (domain_test(domlist, pol, to->metadata->ss->name) == 1)
0fa21e85
N
801 dev = from->devid[d];
802 dev_policy_free(pol);
803 }
804 }
805 return dev;
806}
807
0f0749ad 808static dev_t container_choose_spare(struct state *from, struct state *to,
f0b85306 809 struct domainlist *domlist,
a1e49d69 810 unsigned long long min_size, int active)
5739e0d0
N
811{
812 /* This is similar to choose_spare, but we cannot trust devstate,
813 * so we need to read the metadata instead
814 */
326727d9 815 struct mdinfo *list;
5739e0d0 816 struct supertype *st = from->metadata;
ff044d6b 817 int fd = open(from->devname, O_RDONLY);
5739e0d0 818 int err;
0f0749ad 819 dev_t dev = 0;
5739e0d0
N
820
821 if (fd < 0)
822 return 0;
326727d9
AC
823 if (!st->ss->getinfo_super_disks) {
824 close(fd);
5739e0d0 825 return 0;
326727d9 826 }
5739e0d0
N
827
828 err = st->ss->load_container(st, fd, NULL);
829 close(fd);
830 if (err)
831 return 0;
5739e0d0 832
a1e49d69
CA
833 if (from == to) {
834 /* We must check if number of active disks has not increased
835 * since ioctl in main loop. mdmon may have added spare
836 * to subarray. If so we do not need to look for more spares
837 * so return non zero value */
838 int active_cnt = 0;
839 struct mdinfo *dp;
840 list = st->ss->getinfo_super_disks(st);
841 if (!list) {
842 st->ss->free_super(st);
843 return 1;
844 }
845 dp = list->devs;
846 while (dp) {
847 if (dp->disk.state & (1<<MD_DISK_SYNC) &&
848 !(dp->disk.state & (1<<MD_DISK_FAULTY)))
849 active_cnt++;
850 dp = dp->next;
851 }
852 sysfs_free(list);
853 if (active < active_cnt) {
854 /* Spare just activated.*/
855 st->ss->free_super(st);
856 return 1;
857 }
858 }
859
326727d9
AC
860 /* We only need one spare so full list not needed */
861 list = container_choose_spares(st, min_size, domlist, from->spare_group,
862 to->metadata->ss->name, 1);
863 if (list) {
864 struct mdinfo *disks = list->devs;
865 if (disks)
866 dev = makedev(disks->disk.major, disks->disk.minor);
867 sysfs_free(list);
5739e0d0 868 }
326727d9 869 st->ss->free_super(st);
5739e0d0
N
870 return dev;
871}
872
873
e0bd6a96 874static void try_spare_migration(struct state *statelist, struct alert_info *info)
2e0172b1 875{
66f5c4b6
N
876 struct state *from;
877 struct state *st;
c3621c0a
ML
878
879 link_containers_with_subarrays(statelist);
66f5c4b6
N
880 for (st = statelist; st; st = st->next)
881 if (st->active < st->raid &&
ef15641f 882 st->spare == 0 && !st->err) {
e78dda3b
N
883 struct domainlist *domlist = NULL;
884 int d;
66f5c4b6 885 struct state *to = st;
f0b85306 886 unsigned long long min_size;
66f5c4b6 887
c0dc0ad5
CA
888 if (to->parent_dev != NoMdDev && !to->parent)
889 /* subarray monitored without parent container
890 * we can't move spares here */
891 continue;
892
66f5c4b6
N
893 if (to->parent)
894 /* member of a container */
895 to = to->parent;
e78dda3b 896
de697acc
AC
897 if (get_min_spare_size_required(to, &min_size))
898 continue;
e9a2ac02
AC
899 if (to->metadata->ss->external) {
900 /* We must make sure there is
901 * no suitable spare in container already.
902 * If there is we don't add more */
903 dev_t devid = container_choose_spare(
a1e49d69 904 to, to, NULL, min_size, st->active);
e9a2ac02
AC
905 if (devid > 0)
906 continue;
907 }
e78dda3b
N
908 for (d = 0; d < MaxDisks; d++)
909 if (to->devid[d])
910 domainlist_add_dev(&domlist,
911 to->devid[d],
912 to->metadata->ss->name);
913 if (to->spare_group)
914 domain_add(&domlist, to->spare_group);
5ec0f373
ML
915 /*
916 * No spare migration if the destination
917 * has no domain. Skip this array.
918 */
919 if (!domlist)
920 continue;
0fa21e85 921 for (from=statelist ; from ; from=from->next) {
0f0749ad 922 dev_t devid;
5ec0f373 923 if (!check_donor(from, to))
0fa21e85 924 continue;
5739e0d0
N
925 if (from->metadata->ss->external)
926 devid = container_choose_spare(
a1e49d69 927 from, to, domlist, min_size, 0);
5739e0d0 928 else
f0b85306
AC
929 devid = choose_spare(from, to, domlist,
930 min_size);
0fa21e85 931 if (devid > 0
d52bb542
AC
932 && move_spare(from->devname, to->devname, devid)) {
933 alert("MoveSpare", to->devname, from->devname, info);
934 break;
935 }
0fa21e85 936 }
e78dda3b 937 domain_free(domlist);
2e0172b1
N
938 }
939}
c3621c0a
ML
940
941/* search the statelist to connect external
942 * metadata subarrays with their containers
943 * We always completely rebuild the tree from scratch as
944 * that is safest considering the possibility of entries
945 * disappearing or changing.
946 */
947static void link_containers_with_subarrays(struct state *list)
948{
949 struct state *st;
950 struct state *cont;
951 for (st = list; st; st = st->next) {
952 st->parent = NULL;
953 st->subarray = NULL;
954 }
955 for (st = list; st; st = st->next)
956 if (st->parent_dev != NoMdDev)
957 for (cont = list; cont; cont = cont->next)
958 if (!cont->err &&
959 cont->parent_dev == NoMdDev &&
960 cont->devnum == st->parent_dev) {
961 st->parent = cont;
962 st->subarray = cont->subarray;
963 cont->subarray = st;
964 break;
965 }
966}
967
b90c0e9a
NB
968/* Not really Monitor but ... */
969int Wait(char *dev)
970{
971 struct stat stb;
972 int devnum;
973 int rv = 1;
974
975 if (stat(dev, &stb) != 0) {
976 fprintf(stderr, Name ": Cannot find %s: %s\n", dev,
977 strerror(errno));
978 return 2;
979 }
c94709e8 980 devnum = stat2devnum(&stb);
b90c0e9a
NB
981
982 while(1) {
983 struct mdstat_ent *ms = mdstat_read(1, 0);
984 struct mdstat_ent *e;
985
986 for (e=ms ; e; e=e->next)
987 if (e->devnum == devnum)
988 break;
989
990 if (!e || e->percent < 0) {
e7783ee6 991 if (e && e->metadata_version &&
c94709e8
DW
992 strncmp(e->metadata_version, "external:", 9) == 0) {
993 if (is_subarray(&e->metadata_version[9]))
994 ping_monitor(&e->metadata_version[9]);
995 else
983fff45 996 ping_monitor_by_id(devnum);
c94709e8 997 }
b90c0e9a
NB
998 free_mdstat(ms);
999 return rv;
1000 }
89a10d84 1001 free_mdstat(ms);
b90c0e9a
NB
1002 rv = 0;
1003 mdstat_wait(5);
1004 }
1005}
679eb882
N
1006
1007#ifndef MDASSEMBLE
1008
1009static char *clean_states[] = {
1010 "clear", "inactive", "readonly", "read-auto", "clean", NULL };
1011
1012int WaitClean(char *dev, int sock, int verbose)
1013{
1014 int fd;
1015 struct mdinfo *mdi;
1016 int rv = 1;
1017 int devnum;
1018
1019 fd = open(dev, O_RDONLY);
1020 if (fd < 0) {
1021 if (verbose)
1022 fprintf(stderr, Name ": Couldn't open %s: %s\n", dev, strerror(errno));
1023 return 1;
1024 }
1025
1026 devnum = fd2devnum(fd);
1027 mdi = sysfs_read(fd, devnum, GET_VERSION|GET_LEVEL|GET_SAFEMODE);
1028 if (!mdi) {
1029 if (verbose)
1030 fprintf(stderr, Name ": Failed to read sysfs attributes for "
1031 "%s\n", dev);
1032 close(fd);
1033 return 0;
1034 }
1035
1036 switch(mdi->array.level) {
1037 case LEVEL_LINEAR:
1038 case LEVEL_MULTIPATH:
1039 case 0:
1040 /* safemode delay is irrelevant for these levels */
1041 rv = 0;
1042 }
1043
1044 /* for internal metadata the kernel handles the final clean
1045 * transition, containers can never be dirty
1046 */
1047 if (!is_subarray(mdi->text_version))
1048 rv = 0;
1049
1050 /* safemode disabled ? */
1051 if (mdi->safe_mode_delay == 0)
1052 rv = 0;
1053
1054 if (rv) {
1055 int state_fd = sysfs_open(fd2devnum(fd), NULL, "array_state");
1056 char buf[20];
1057 fd_set fds;
1058 struct timeval tm;
1059
1060 /* minimize the safe_mode_delay and prepare to wait up to 5s
1061 * for writes to quiesce
1062 */
1063 sysfs_set_safemode(mdi, 1);
1064 tm.tv_sec = 5;
1065 tm.tv_usec = 0;
1066
1067 FD_ZERO(&fds);
1068
1069 /* wait for array_state to be clean */
1070 while (1) {
1071 rv = read(state_fd, buf, sizeof(buf));
1072 if (rv < 0)
1073 break;
1074 if (sysfs_match_word(buf, clean_states) <= 4)
1075 break;
1076 FD_SET(state_fd, &fds);
1077 rv = select(state_fd + 1, NULL, NULL, &fds, &tm);
1078 if (rv < 0 && errno != EINTR)
1079 break;
1080 lseek(state_fd, 0, SEEK_SET);
1081 }
1082 if (rv < 0)
1083 rv = 1;
1084 else if (fping_monitor(sock) == 0 ||
1085 ping_monitor(mdi->text_version) == 0) {
1086 /* we need to ping to close the window between array
1087 * state transitioning to clean and the metadata being
1088 * marked clean
1089 */
1090 rv = 0;
1091 } else
1092 rv = 1;
1093 if (rv && verbose)
1094 fprintf(stderr, Name ": Error waiting for %s to be clean\n",
1095 dev);
1096
1097 /* restore the original safe_mode_delay */
1098 sysfs_set_safemode(mdi, mdi->safe_mode_delay);
1099 close(state_fd);
1100 }
1101
1102 sysfs_free(mdi);
1103 close(fd);
1104
1105 return rv;
1106}
1107#endif /* MDASSEMBLE */