]> git.ipfire.org Git - thirdparty/mdadm.git/blame - Monitor.c
Monitor: reduce default poll interval if mdstat is pollable.
[thirdparty/mdadm.git] / Monitor.c
CommitLineData
52826846 1/*
9a9dab36 2 * mdadm - manage Linux "md" devices aka RAID arrays.
52826846 3 *
4f589ad0 4 * Copyright (C) 2001-2006 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
22 * Email: <neilb@cse.unsw.edu.au>
23 * Paper: Neil Brown
24 * School of Computer Science and Engineering
25 * The University of New South Wales
26 * Sydney, 2052
27 * Australia
28 */
29
9a9dab36 30#include "mdadm.h"
52826846
NB
31#include "md_p.h"
32#include "md_u.h"
e0d19036 33#include <sys/wait.h>
98127a6d 34#include <signal.h>
4450e59f 35#include <limits.h>
773135f5 36#include <syslog.h>
52826846 37
4948b8f7
NB
38static void alert(char *event, char *dev, char *disc, char *mailaddr, char *mailfrom,
39 char *cmd, int dosyslog);
52826846 40
aba69144 41static char *percentalerts[] = {
e0d19036
NB
42 "RebuildStarted",
43 "Rebuild20",
44 "Rebuild40",
45 "Rebuild60",
46 "Rebuild80",
47};
48
e27d562b
NB
49/* The largest number of disks current arrays can manage is 384
50 * This really should be dynamically, but that will have to wait
51 * At least it isn't MD_SB_DISKS.
52 */
53#define MaxDisks 384
cd29a5c8 54int Monitor(mddev_dev_t devlist,
52826846 55 char *mailaddr, char *alert_cmd,
aa88f531 56 int period, int daemonise, int scan, int oneshot,
8aec876d 57 int dosyslog, int test, char* pidfile)
52826846
NB
58{
59 /*
60 * Every few seconds, scan every md device looking for changes
61 * When a change is found, log it, possibly run the alert command,
62 * and possibly send Email
63 *
64 * For each array, we record:
65 * Update time
66 * active/working/failed/spare drives
67 * State of each device.
e0d19036 68 * %rebuilt if rebuilding
52826846
NB
69 *
70 * If the update time changes, check out all the data again
71 * It is possible that we cannot get the state of each device
72 * due to bugs in the md kernel module.
e0d19036
NB
73 * We also read /proc/mdstat to get rebuild percent,
74 * and to get state on all active devices incase of kernel bug.
52826846 75 *
e0d19036
NB
76 * Events are:
77 * Fail
78 * An active device had Faulty set or Active/Sync removed
79 * FailSpare
80 * A spare device had Faulty set
81 * SpareActive
82 * An active device had a reverse transition
83 * RebuildStarted
84 * percent went from -1 to +ve
85 * Rebuild20 Rebuild40 Rebuild60 Rebuild80
86 * percent went from below to not-below that number
87 * DeviceDisappeared
88 * Couldn't access a device which was previously visible
52826846
NB
89 *
90 * if we detect an array with active<raid and spare==0
91 * we look at other arrays that have same spare-group
92 * If we find one with active==raid and spare>0,
93 * and if we can get_disk_info and find a name
94 * Then we hot-remove and hot-add to the other array
95 *
e0d19036
NB
96 * If devlist is NULL, then we can monitor everything because --scan
97 * was given. We get an initial list from config file and add anything
98 * that appears in /proc/mdstat
52826846
NB
99 */
100
101 struct state {
102 char *devname;
e0d19036 103 int devnum; /* to sync with mdstat info */
52826846
NB
104 long utime;
105 int err;
e0d19036
NB
106 char *spare_group;
107 int active, working, failed, spare, raid;
feb716e9 108 int expected_spares;
e27d562b
NB
109 int devstate[MaxDisks];
110 int devid[MaxDisks];
e0d19036 111 int percent;
52826846
NB
112 struct state *next;
113 } *statelist = NULL;
114 int finished = 0;
e0d19036 115 struct mdstat_ent *mdstat = NULL;
4948b8f7 116 char *mailfrom = NULL;
e0d19036 117
d013a55e 118 if (!mailaddr) {
8aec876d 119 mailaddr = conf_get_mailaddr();
d013a55e 120 if (mailaddr && ! scan)
56eedc1a 121 fprintf(stderr, Name ": Monitor using email address \"%s\" from config file\n",
d013a55e
NB
122 mailaddr);
123 }
8aec876d 124 mailfrom = conf_get_mailfrom();
4948b8f7 125
d013a55e 126 if (!alert_cmd) {
8aec876d 127 alert_cmd = conf_get_program();
d013a55e 128 if (alert_cmd && ! scan)
56eedc1a 129 fprintf(stderr, Name ": Monitor using program \"%s\" from config file\n",
d013a55e
NB
130 alert_cmd);
131 }
56eedc1a
NB
132 if (scan && !mailaddr && !alert_cmd) {
133 fprintf(stderr, Name ": No mail address or alert command - not monitoring.\n");
d013a55e 134 return 1;
56eedc1a 135 }
d013a55e
NB
136
137 if (daemonise) {
138 int pid = fork();
139 if (pid > 0) {
b5e64645
NB
140 if (!pidfile)
141 printf("%d\n", pid);
142 else {
143 FILE *pid_file;
144 pid_file=fopen(pidfile, "w");
145 if (!pid_file)
146 perror("cannot create pid file");
147 else {
148 fprintf(pid_file,"%d\n", pid);
149 fclose(pid_file);
150 }
151 }
d013a55e
NB
152 return 0;
153 }
154 if (pid < 0) {
155 perror("daemonise");
156 return 1;
157 }
158 close(0);
fb97b4d6 159 open("/dev/null", O_RDWR);
d013a55e
NB
160 dup2(0,1);
161 dup2(0,2);
162 setsid();
163 }
e0d19036
NB
164
165 if (devlist == NULL) {
8aec876d 166 mddev_ident_t mdlist = conf_get_ident(NULL);
e0d19036 167 for (; mdlist; mdlist=mdlist->next) {
fe056d1f
N
168 struct state *st;
169 if (mdlist->devname == NULL)
170 continue;
112cace6
N
171 if (strcasecmp(mdlist->devname, "<ignore>") == 0)
172 continue;
fe056d1f 173 st = malloc(sizeof *st);
e0d19036
NB
174 if (st == NULL)
175 continue;
db2d001c
N
176 if (mdlist->devname[0] == '/')
177 st->devname = strdup(mdlist->devname);
178 else {
179 st->devname = malloc(8+strlen(mdlist->devname)+1);
180 strcpy(strcpy(st->devname, "/dev/md/"),
181 mdlist->devname);
182 }
e0d19036
NB
183 st->utime = 0;
184 st->next = statelist;
d013a55e 185 st->err = 0;
4450e59f 186 st->devnum = INT_MAX;
e0d19036 187 st->percent = -2;
feb716e9 188 st->expected_spares = mdlist->spare_disks;
e0d19036
NB
189 if (mdlist->spare_group)
190 st->spare_group = strdup(mdlist->spare_group);
191 else
192 st->spare_group = NULL;
193 statelist = st;
194 }
195 } else {
cd29a5c8 196 mddev_dev_t dv;
e0d19036 197 for (dv=devlist ; dv; dv=dv->next) {
8aec876d 198 mddev_ident_t mdlist = conf_get_ident(dv->devname);
e0d19036
NB
199 struct state *st = malloc(sizeof *st);
200 if (st == NULL)
201 continue;
202 st->devname = strdup(dv->devname);
203 st->utime = 0;
204 st->next = statelist;
d013a55e 205 st->err = 0;
4450e59f 206 st->devnum = INT_MAX;
e0d19036 207 st->percent = -2;
feb716e9 208 st->expected_spares = -1;
e0d19036 209 st->spare_group = NULL;
e5329c37
NB
210 if (mdlist) {
211 st->expected_spares = mdlist->spare_disks;
212 if (mdlist->spare_group)
213 st->spare_group = strdup(mdlist->spare_group);
214 }
e0d19036
NB
215 statelist = st;
216 }
217 }
218
219
220 while (! finished) {
aa88f531 221 int new_found = 0;
e0d19036
NB
222 struct state *st;
223
224 if (mdstat)
225 free_mdstat(mdstat);
22a88995 226 mdstat = mdstat_read(oneshot?0:1, 0);
e0d19036
NB
227
228 for (st=statelist; st; st=st->next) {
ab5303d6 229 struct { int state, major, minor; } info[MaxDisks];
52826846 230 mdu_array_info_t array;
dd0781e5 231 struct mdstat_ent *mse = NULL, *mse2;
e0d19036 232 char *dev = st->devname;
52826846 233 int fd;
98c6faba 234 unsigned int i;
e0d19036 235
98c6faba 236 if (test)
4948b8f7 237 alert("TestMessage", dev, NULL, mailaddr, mailfrom, alert_cmd, dosyslog);
52826846
NB
238 fd = open(dev, O_RDONLY);
239 if (fd < 0) {
240 if (!st->err)
e0d19036 241 alert("DeviceDisappeared", dev, NULL,
4948b8f7 242 mailaddr, mailfrom, alert_cmd, dosyslog);
e0d19036 243/* fprintf(stderr, Name ": cannot open %s: %s\n",
52826846 244 dev, strerror(errno));
e0d19036 245*/ st->err=1;
52826846
NB
246 continue;
247 }
e4dc5106 248 fcntl(fd, F_SETFD, FD_CLOEXEC);
52826846
NB
249 if (ioctl(fd, GET_ARRAY_INFO, &array)<0) {
250 if (!st->err)
e0d19036 251 alert("DeviceDisappeared", dev, NULL,
4948b8f7 252 mailaddr, mailfrom, alert_cmd, dosyslog);
e0d19036 253/* fprintf(stderr, Name ": cannot get array info for %s: %s\n",
52826846 254 dev, strerror(errno));
e0d19036 255*/ st->err=1;
52826846
NB
256 close(fd);
257 continue;
258 }
66f8bbbe
DL
259 /* It's much easier to list what array levels can't
260 * have a device disappear than all of them that can
261 */
262 if (array.level == 0 || array.level == -1) {
5787fa49
NB
263 if (!st->err)
264 alert("DeviceDisappeared", dev, "Wrong-Level",
4948b8f7 265 mailaddr, mailfrom, alert_cmd, dosyslog);
5787fa49
NB
266 st->err = 1;
267 close(fd);
268 continue;
269 }
4450e59f 270 if (st->devnum == INT_MAX) {
e0d19036
NB
271 struct stat stb;
272 if (fstat(fd, &stb) == 0 &&
98c6faba 273 (S_IFMT&stb.st_mode)==S_IFBLK) {
0df46c2a
NB
274 if (major(stb.st_rdev) == MD_MAJOR)
275 st->devnum = minor(stb.st_rdev);
98c6faba 276 else
0df46c2a 277 st->devnum = -1- (minor(stb.st_rdev)>>6);
98c6faba 278 }
e0d19036
NB
279 }
280
dd0781e5
NB
281 for (mse2 = mdstat ; mse2 ; mse2=mse2->next)
282 if (mse2->devnum == st->devnum) {
4450e59f 283 mse2->devnum = INT_MAX; /* flag it as "used" */
dd0781e5
NB
284 mse = mse2;
285 }
e0d19036 286
4736b5dc
N
287 if (array.utime == 0)
288 /* external arrays don't update utime */
289 array.utime = time(0);
290
52826846 291 if (st->utime == array.utime &&
e0d19036
NB
292 st->failed == array.failed_disks &&
293 st->working == array.working_disks &&
294 st->spare == array.spare_disks &&
295 (mse == NULL || (
296 mse->percent == st->percent
297 ))) {
52826846 298 close(fd);
e0d19036 299 st->err = 0;
52826846
NB
300 continue;
301 }
aa88f531
NB
302 if (st->utime == 0 && /* new array */
303 mse && /* is in /proc/mdstat */
304 mse->pattern && strchr(mse->pattern, '_') /* degraded */
305 )
4948b8f7 306 alert("DegradedArray", dev, NULL, mailaddr, mailfrom, alert_cmd, dosyslog);
aa88f531 307
feb716e9 308 if (st->utime == 0 && /* new array */
aba69144
NB
309 st->expected_spares > 0 &&
310 array.spare_disks < st->expected_spares)
4948b8f7 311 alert("SparesMissing", dev, NULL, mailaddr, mailfrom, alert_cmd, dosyslog);
e0d19036 312 if (mse &&
aba69144 313 st->percent == -1 &&
e0d19036 314 mse->percent >= 0)
4948b8f7 315 alert("RebuildStarted", dev, NULL, mailaddr, mailfrom, alert_cmd, dosyslog);
e0d19036
NB
316 if (mse &&
317 st->percent >= 0 &&
318 mse->percent >= 0 &&
319 (mse->percent / 20) > (st->percent / 20))
320 alert(percentalerts[mse->percent/20],
4948b8f7 321 dev, NULL, mailaddr, mailfrom, alert_cmd, dosyslog);
e0d19036 322
98c6faba
NB
323 if (mse &&
324 mse->percent == -1 &&
37dfc3d6
NB
325 st->percent >= 0) {
326 /* Rebuild/sync/whatever just finished.
327 * If there is a number in /mismatch_cnt,
328 * we should report that.
329 */
7e0f6979 330 struct mdinfo *sra =
37dfc3d6
NB
331 sysfs_read(-1, st->devnum, GET_MISMATCH);
332 if (sra && sra->mismatch_cnt > 0) {
333 char cnt[40];
334 sprintf(cnt, " mismatches found: %d", sra->mismatch_cnt);
335 alert("RebuildFinished", dev, cnt, mailaddr, mailfrom, alert_cmd, dosyslog);
336 } else
337 alert("RebuildFinished", dev, NULL, mailaddr, mailfrom, alert_cmd, dosyslog);
338 if (sra)
339 free(sra);
340 }
98c6faba 341
e0d19036
NB
342 if (mse)
343 st->percent = mse->percent;
e27d562b 344
ab5303d6
NB
345
346 for (i=0; i<MaxDisks && i <= array.raid_disks + array.nr_disks;
347 i++) {
348 mdu_disk_info_t disc;
01d9299c 349 disc.number = i;
ab5303d6
NB
350 if (ioctl(fd, GET_DISK_INFO, &disc) >= 0) {
351 info[i].state = disc.state;
352 info[i].major = disc.major;
353 info[i].minor = disc.minor;
354 } else
355 info[i].major = info[i].minor = 0;
356 }
357 close(fd);
358
e27d562b 359 for (i=0; i<MaxDisks; i++) {
f8409e54 360 mdu_disk_info_t disc = {0};
e0d19036
NB
361 int newstate=0;
362 int change;
363 char *dv = NULL;
52826846 364 disc.number = i;
e27d562b
NB
365 if (i > array.raid_disks + array.nr_disks) {
366 newstate = 0;
367 disc.major = disc.minor = 0;
ab5303d6
NB
368 } else if (info[i].major || info[i].minor) {
369 newstate = info[i].state;
370 dv = map_dev(info[i].major, info[i].minor, 1);
371 disc.state = newstate;
372 disc.major = info[i].major;
373 disc.minor = info[i].minor;
e27d562b 374 } else if (mse && mse->pattern && i < strlen(mse->pattern)) {
e0d19036
NB
375 switch(mse->pattern[i]) {
376 case 'U': newstate = 6 /* ACTIVE/SYNC */; break;
377 case '_': newstate = 0; break;
52826846 378 }
e27d562b
NB
379 disc.major = disc.minor = 0;
380 }
fe394e5e
NB
381 if (dv == NULL && st->devid[i])
382 dv = map_dev(major(st->devid[i]),
16c6fa80 383 minor(st->devid[i]), 1);
e0d19036
NB
384 change = newstate ^ st->devstate[i];
385 if (st->utime && change && !st->err) {
98c6faba 386 if (i < (unsigned)array.raid_disks &&
e0d19036
NB
387 (((newstate&change)&(1<<MD_DISK_FAULTY)) ||
388 ((st->devstate[i]&change)&(1<<MD_DISK_ACTIVE)) ||
389 ((st->devstate[i]&change)&(1<<MD_DISK_SYNC)))
390 )
4948b8f7 391 alert("Fail", dev, dv, mailaddr, mailfrom, alert_cmd, dosyslog);
98c6faba 392 else if (i >= (unsigned)array.raid_disks &&
e0d19036 393 (disc.major || disc.minor) &&
0df46c2a 394 st->devid[i] == makedev(disc.major, disc.minor) &&
e0d19036
NB
395 ((newstate&change)&(1<<MD_DISK_FAULTY))
396 )
4948b8f7 397 alert("FailSpare", dev, dv, mailaddr, mailfrom, alert_cmd, dosyslog);
98c6faba 398 else if (i < (unsigned)array.raid_disks &&
e0d19036
NB
399 (((st->devstate[i]&change)&(1<<MD_DISK_FAULTY)) ||
400 ((newstate&change)&(1<<MD_DISK_ACTIVE)) ||
401 ((newstate&change)&(1<<MD_DISK_SYNC)))
402 )
4948b8f7 403 alert("SpareActive", dev, dv, mailaddr, mailfrom, alert_cmd, dosyslog);
52826846 404 }
3e6944b2 405 st->devstate[i] = newstate;
0df46c2a 406 st->devid[i] = makedev(disc.major, disc.minor);
52826846 407 }
52826846
NB
408 st->active = array.active_disks;
409 st->working = array.working_disks;
410 st->spare = array.spare_disks;
411 st->failed = array.failed_disks;
412 st->utime = array.utime;
e0d19036
NB
413 st->raid = array.raid_disks;
414 st->err = 0;
52826846 415 }
e0d19036
NB
416 /* now check if there are any new devices found in mdstat */
417 if (scan) {
418 struct mdstat_ent *mse;
aba69144 419 for (mse=mdstat; mse; mse=mse->next)
4450e59f 420 if (mse->devnum != INT_MAX &&
2cdb6489 421 mse->level &&
66f8bbbe
DL
422 (strcmp(mse->level, "raid0")!=0 &&
423 strcmp(mse->level, "linear")!=0)
5787fa49 424 ) {
e0d19036 425 struct state *st = malloc(sizeof *st);
98c6faba
NB
426 mdu_array_info_t array;
427 int fd;
e0d19036
NB
428 if (st == NULL)
429 continue;
430 st->devname = strdup(get_md_name(mse->devnum));
98c6faba
NB
431 if ((fd = open(st->devname, O_RDONLY)) < 0 ||
432 ioctl(fd, GET_ARRAY_INFO, &array)< 0) {
433 /* no such array */
434 if (fd >=0) close(fd);
8bd2e0c3 435 put_md_name(st->devname);
98c6faba
NB
436 free(st->devname);
437 free(st);
438 continue;
439 }
dd0781e5 440 close(fd);
e0d19036
NB
441 st->utime = 0;
442 st->next = statelist;
443 st->err = 1;
444 st->devnum = mse->devnum;
445 st->percent = -2;
446 st->spare_group = NULL;
98c6faba 447 st->expected_spares = -1;
e0d19036 448 statelist = st;
bc854448
N
449 if (test)
450 alert("TestMessage", st->devname, NULL, mailaddr, mailfrom, alert_cmd, dosyslog);
4948b8f7 451 alert("NewArray", st->devname, NULL, mailaddr, mailfrom, alert_cmd, dosyslog);
aa88f531 452 new_found = 1;
e0d19036
NB
453 }
454 }
455 /* If an array has active < raid && spare == 0 && spare_group != NULL
456 * Look for another array with spare > 0 and active == raid and same spare_group
457 * if found, choose a device and hotremove/hotadd
458 */
459 for (st = statelist; st; st=st->next)
460 if (st->active < st->raid &&
461 st->spare == 0 &&
462 st->spare_group != NULL) {
463 struct state *st2;
464 for (st2=statelist ; st2 ; st2=st2->next)
465 if (st2 != st &&
466 st2->spare > 0 &&
467 st2->active == st2->raid &&
468 st2->spare_group != NULL &&
469 strcmp(st->spare_group, st2->spare_group) == 0) {
470 /* try to remove and add */
471 int fd1 = open(st->devname, O_RDONLY);
472 int fd2 = open(st2->devname, O_RDONLY);
473 int dev = -1;
474 int d;
475 if (fd1 < 0 || fd2 < 0) {
476 if (fd1>=0) close(fd1);
477 if (fd2>=0) close(fd2);
478 continue;
479 }
e27d562b 480 for (d=st2->raid; d < MaxDisks; d++) {
e0d19036
NB
481 if (st2->devid[d] > 0 &&
482 st2->devstate[d] == 0) {
483 dev = st2->devid[d];
484 break;
485 }
486 }
487 if (dev > 0) {
aba69144 488 if (ioctl(fd2, HOT_REMOVE_DISK,
e0d19036
NB
489 (unsigned long)dev) == 0) {
490 if (ioctl(fd1, HOT_ADD_DISK,
491 (unsigned long)dev) == 0) {
4948b8f7 492 alert("MoveSpare", st->devname, st2->devname, mailaddr, mailfrom, alert_cmd, dosyslog);
e0d19036
NB
493 close(fd1);
494 close(fd2);
495 break;
496 }
497 else ioctl(fd2, HOT_ADD_DISK, (unsigned long) dev);
498 }
499 }
500 close(fd1);
501 close(fd2);
502 }
503 }
aa88f531
NB
504 if (!new_found) {
505 if (oneshot)
506 break;
507 else
dd0781e5 508 mdstat_wait(period);
aa88f531 509 }
98c6faba 510 test = 0;
52826846 511 }
b5e64645
NB
512 if (pidfile)
513 unlink(pidfile);
52826846
NB
514 return 0;
515}
516
517
4948b8f7 518static void alert(char *event, char *dev, char *disc, char *mailaddr, char *mailfrom, char *cmd,
773135f5 519 int dosyslog)
52826846 520{
773135f5
NB
521 int priority;
522
cd29a5c8
NB
523 if (!cmd && !mailaddr) {
524 time_t now = time(0);
aba69144 525
e0d19036 526 printf("%1.15s: %s on %s %s\n", ctime(&now)+4, event, dev, disc?disc:"unknown device");
cd29a5c8 527 }
52826846
NB
528 if (cmd) {
529 int pid = fork();
530 switch(pid) {
531 default:
532 waitpid(pid, NULL, 0);
533 break;
534 case -1:
535 break;
536 case 0:
537 execl(cmd, cmd, event, dev, disc, NULL);
538 exit(2);
539 }
540 }
aba69144
NB
541 if (mailaddr &&
542 (strncmp(event, "Fail", 4)==0 ||
98c6faba 543 strncmp(event, "Test", 4)==0 ||
d1732eeb 544 strncmp(event, "Spares", 6)==0 ||
aa88f531 545 strncmp(event, "Degrade", 7)==0)) {
52826846
NB
546 FILE *mp = popen(Sendmail, "w");
547 if (mp) {
008e1100 548 FILE *mdstat;
52826846
NB
549 char hname[256];
550 gethostname(hname, sizeof(hname));
551 signal(SIGPIPE, SIG_IGN);
4948b8f7
NB
552 if (mailfrom)
553 fprintf(mp, "From: %s\n", mailfrom);
554 else
555 fprintf(mp, "From: " Name " monitoring <root>\n");
52826846
NB
556 fprintf(mp, "To: %s\n", mailaddr);
557 fprintf(mp, "Subject: %s event on %s:%s\n\n", event, dev, hname);
558
559 fprintf(mp, "This is an automatically generated mail message from " Name "\n");
560 fprintf(mp, "running on %s\n\n", hname);
561
562 fprintf(mp, "A %s event had been detected on md device %s.\n\n", event, dev);
563
37dfc3d6 564 if (disc && disc[0] != ' ')
56eedc1a 565 fprintf(mp, "It could be related to component device %s.\n\n", disc);
37dfc3d6
NB
566 if (disc && disc[0] == ' ')
567 fprintf(mp, "Extra information:%s.\n\n", disc);
52826846
NB
568
569 fprintf(mp, "Faithfully yours, etc.\n");
008e1100
NB
570
571 mdstat = fopen("/proc/mdstat", "r");
572 if (mdstat) {
573 char buf[8192];
574 int n;
a524a7ee 575 fprintf(mp, "\nP.S. The /proc/mdstat file currently contains the following:\n\n");
008e1100 576 while ( (n=fread(buf, 1, sizeof(buf), mdstat)) > 0)
9fca7d62 577 n=fwrite(buf, 1, n, mp); /* yes, i don't care about the result */
008e1100
NB
578 fclose(mdstat);
579 }
52826846
NB
580 fclose(mp);
581 }
582
583 }
773135f5
NB
584
585 /* log the event to syslog maybe */
586 if (dosyslog) {
587 /* Log at a different severity depending on the event.
588 *
589 * These are the critical events: */
590 if (strncmp(event, "Fail", 4)==0 ||
591 strncmp(event, "Degrade", 7)==0 ||
592 strncmp(event, "DeviceDisappeared", 17)==0)
593 priority = LOG_CRIT;
594 /* Good to know about, but are not failures: */
595 else if (strncmp(event, "Rebuild", 7)==0 ||
d1732eeb
NB
596 strncmp(event, "MoveSpare", 9)==0 ||
597 strncmp(event, "Spares", 6) != 0)
773135f5
NB
598 priority = LOG_WARNING;
599 /* Everything else: */
600 else
601 priority = LOG_INFO;
602
603 if (disc)
604 syslog(priority, "%s event detected on md device %s, component device %s", event, dev, disc);
605 else
606 syslog(priority, "%s event detected on md device %s", event, dev);
607 }
52826846 608}
b90c0e9a
NB
609
610/* Not really Monitor but ... */
611int Wait(char *dev)
612{
613 struct stat stb;
614 int devnum;
615 int rv = 1;
616
617 if (stat(dev, &stb) != 0) {
618 fprintf(stderr, Name ": Cannot find %s: %s\n", dev,
619 strerror(errno));
620 return 2;
621 }
c94709e8 622 devnum = stat2devnum(&stb);
b90c0e9a
NB
623
624 while(1) {
625 struct mdstat_ent *ms = mdstat_read(1, 0);
626 struct mdstat_ent *e;
627
628 for (e=ms ; e; e=e->next)
629 if (e->devnum == devnum)
630 break;
631
632 if (!e || e->percent < 0) {
e7783ee6 633 if (e && e->metadata_version &&
c94709e8
DW
634 strncmp(e->metadata_version, "external:", 9) == 0) {
635 if (is_subarray(&e->metadata_version[9]))
636 ping_monitor(&e->metadata_version[9]);
637 else
638 ping_monitor(devnum2devname(devnum));
639 }
b90c0e9a
NB
640 free_mdstat(ms);
641 return rv;
642 }
89a10d84 643 free_mdstat(ms);
b90c0e9a
NB
644 rv = 0;
645 mdstat_wait(5);
646 }
647}
1770662b
DW
648
649static char *clean_states[] = {
650 "clear", "inactive", "readonly", "read-auto", "clean", NULL };
651
27dec8fa 652int WaitClean(char *dev, int verbose)
1770662b
DW
653{
654 int fd;
655 struct mdinfo *mdi;
656 int rv = 1;
657 int devnum;
658
659 fd = open(dev, O_RDONLY);
660 if (fd < 0) {
27dec8fa
DW
661 if (verbose)
662 fprintf(stderr, Name ": Couldn't open %s: %s\n", dev, strerror(errno));
1770662b
DW
663 return 1;
664 }
665
666 devnum = fd2devnum(fd);
667 mdi = sysfs_read(fd, devnum, GET_VERSION|GET_LEVEL|GET_SAFEMODE);
668 if (!mdi) {
27dec8fa
DW
669 if (verbose)
670 fprintf(stderr, Name ": Failed to read sysfs attributes for "
671 "%s\n", dev);
1770662b
DW
672 close(fd);
673 return 0;
674 }
675
676 switch(mdi->array.level) {
677 case LEVEL_LINEAR:
678 case LEVEL_MULTIPATH:
679 case 0:
680 /* safemode delay is irrelevant for these levels */
681 rv = 0;
682
683 }
684
685 /* for internal metadata the kernel handles the final clean
686 * transition, containers can never be dirty
687 */
688 if (!is_subarray(mdi->text_version))
689 rv = 0;
690
691 /* safemode disabled ? */
692 if (mdi->safe_mode_delay == 0)
693 rv = 0;
694
695 if (rv) {
696 int state_fd = sysfs_open(fd2devnum(fd), NULL, "array_state");
1770662b 697 char buf[20];
0dd3ba30
DW
698 fd_set fds;
699 struct timeval tm;
1770662b 700
0dd3ba30
DW
701 /* minimize the safe_mode_delay and prepare to wait up to 5s
702 * for writes to quiesce
703 */
704 sysfs_set_safemode(mdi, 1);
705 tm.tv_sec = 5;
706 tm.tv_usec = 0;
707
7146ec6a
DW
708 /* give mdmon a chance to checkpoint resync */
709 sysfs_set_str(mdi, NULL, "sync_action", "idle");
710
0dd3ba30 711 FD_ZERO(&fds);
1770662b 712
0dd3ba30
DW
713 /* wait for array_state to be clean */
714 while (1) {
1770662b
DW
715 rv = read(state_fd, buf, sizeof(buf));
716 if (rv < 0)
717 break;
718 if (sysfs_match_word(buf, clean_states) <= 4)
719 break;
0dd3ba30 720 FD_SET(state_fd, &fds);
28005287 721 rv = select(state_fd + 1, NULL, NULL, &fds, &tm);
0dd3ba30
DW
722 if (rv < 0 && errno != EINTR)
723 break;
1770662b
DW
724 lseek(state_fd, 0, SEEK_SET);
725 }
726 if (rv < 0)
727 rv = 1;
0dd3ba30 728 else if (ping_monitor(mdi->text_version) == 0) {
1770662b
DW
729 /* we need to ping to close the window between array
730 * state transitioning to clean and the metadata being
731 * marked clean
732 */
0dd3ba30
DW
733 rv = 0;
734 } else
735 rv = 1;
27dec8fa 736 if (rv && verbose)
1770662b
DW
737 fprintf(stderr, Name ": Error waiting for %s to be clean\n",
738 dev);
739
0dd3ba30
DW
740 /* restore the original safe_mode_delay */
741 sysfs_set_safemode(mdi, mdi->safe_mode_delay);
1770662b
DW
742 close(state_fd);
743 }
744
0dd3ba30 745 sysfs_free(mdi);
1770662b
DW
746 close(fd);
747
748 return rv;
749}
750
751