]> git.ipfire.org Git - thirdparty/mdadm.git/blame - Monitor.c
util: fix test for text_version
[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
52826846 287 if (st->utime == array.utime &&
e0d19036
NB
288 st->failed == array.failed_disks &&
289 st->working == array.working_disks &&
290 st->spare == array.spare_disks &&
291 (mse == NULL || (
292 mse->percent == st->percent
293 ))) {
52826846 294 close(fd);
e0d19036 295 st->err = 0;
52826846
NB
296 continue;
297 }
aa88f531
NB
298 if (st->utime == 0 && /* new array */
299 mse && /* is in /proc/mdstat */
300 mse->pattern && strchr(mse->pattern, '_') /* degraded */
301 )
4948b8f7 302 alert("DegradedArray", dev, NULL, mailaddr, mailfrom, alert_cmd, dosyslog);
aa88f531 303
feb716e9 304 if (st->utime == 0 && /* new array */
aba69144
NB
305 st->expected_spares > 0 &&
306 array.spare_disks < st->expected_spares)
4948b8f7 307 alert("SparesMissing", dev, NULL, mailaddr, mailfrom, alert_cmd, dosyslog);
e0d19036 308 if (mse &&
aba69144 309 st->percent == -1 &&
e0d19036 310 mse->percent >= 0)
4948b8f7 311 alert("RebuildStarted", dev, NULL, mailaddr, mailfrom, alert_cmd, dosyslog);
e0d19036
NB
312 if (mse &&
313 st->percent >= 0 &&
314 mse->percent >= 0 &&
315 (mse->percent / 20) > (st->percent / 20))
316 alert(percentalerts[mse->percent/20],
4948b8f7 317 dev, NULL, mailaddr, mailfrom, alert_cmd, dosyslog);
e0d19036 318
98c6faba
NB
319 if (mse &&
320 mse->percent == -1 &&
37dfc3d6
NB
321 st->percent >= 0) {
322 /* Rebuild/sync/whatever just finished.
323 * If there is a number in /mismatch_cnt,
324 * we should report that.
325 */
7e0f6979 326 struct mdinfo *sra =
37dfc3d6
NB
327 sysfs_read(-1, st->devnum, GET_MISMATCH);
328 if (sra && sra->mismatch_cnt > 0) {
329 char cnt[40];
330 sprintf(cnt, " mismatches found: %d", sra->mismatch_cnt);
331 alert("RebuildFinished", dev, cnt, mailaddr, mailfrom, alert_cmd, dosyslog);
332 } else
333 alert("RebuildFinished", dev, NULL, mailaddr, mailfrom, alert_cmd, dosyslog);
334 if (sra)
335 free(sra);
336 }
98c6faba 337
e0d19036
NB
338 if (mse)
339 st->percent = mse->percent;
e27d562b 340
ab5303d6
NB
341
342 for (i=0; i<MaxDisks && i <= array.raid_disks + array.nr_disks;
343 i++) {
344 mdu_disk_info_t disc;
01d9299c 345 disc.number = i;
ab5303d6
NB
346 if (ioctl(fd, GET_DISK_INFO, &disc) >= 0) {
347 info[i].state = disc.state;
348 info[i].major = disc.major;
349 info[i].minor = disc.minor;
350 } else
351 info[i].major = info[i].minor = 0;
352 }
353 close(fd);
354
e27d562b 355 for (i=0; i<MaxDisks; i++) {
f8409e54 356 mdu_disk_info_t disc = {0};
e0d19036
NB
357 int newstate=0;
358 int change;
359 char *dv = NULL;
52826846 360 disc.number = i;
e27d562b
NB
361 if (i > array.raid_disks + array.nr_disks) {
362 newstate = 0;
363 disc.major = disc.minor = 0;
ab5303d6
NB
364 } else if (info[i].major || info[i].minor) {
365 newstate = info[i].state;
366 dv = map_dev(info[i].major, info[i].minor, 1);
367 disc.state = newstate;
368 disc.major = info[i].major;
369 disc.minor = info[i].minor;
e27d562b 370 } else if (mse && mse->pattern && i < strlen(mse->pattern)) {
e0d19036
NB
371 switch(mse->pattern[i]) {
372 case 'U': newstate = 6 /* ACTIVE/SYNC */; break;
373 case '_': newstate = 0; break;
52826846 374 }
e27d562b
NB
375 disc.major = disc.minor = 0;
376 }
fe394e5e
NB
377 if (dv == NULL && st->devid[i])
378 dv = map_dev(major(st->devid[i]),
16c6fa80 379 minor(st->devid[i]), 1);
e0d19036
NB
380 change = newstate ^ st->devstate[i];
381 if (st->utime && change && !st->err) {
98c6faba 382 if (i < (unsigned)array.raid_disks &&
e0d19036
NB
383 (((newstate&change)&(1<<MD_DISK_FAULTY)) ||
384 ((st->devstate[i]&change)&(1<<MD_DISK_ACTIVE)) ||
385 ((st->devstate[i]&change)&(1<<MD_DISK_SYNC)))
386 )
4948b8f7 387 alert("Fail", dev, dv, mailaddr, mailfrom, alert_cmd, dosyslog);
98c6faba 388 else if (i >= (unsigned)array.raid_disks &&
e0d19036 389 (disc.major || disc.minor) &&
0df46c2a 390 st->devid[i] == makedev(disc.major, disc.minor) &&
e0d19036
NB
391 ((newstate&change)&(1<<MD_DISK_FAULTY))
392 )
4948b8f7 393 alert("FailSpare", dev, dv, mailaddr, mailfrom, alert_cmd, dosyslog);
98c6faba 394 else if (i < (unsigned)array.raid_disks &&
e0d19036
NB
395 (((st->devstate[i]&change)&(1<<MD_DISK_FAULTY)) ||
396 ((newstate&change)&(1<<MD_DISK_ACTIVE)) ||
397 ((newstate&change)&(1<<MD_DISK_SYNC)))
398 )
4948b8f7 399 alert("SpareActive", dev, dv, mailaddr, mailfrom, alert_cmd, dosyslog);
52826846 400 }
3e6944b2 401 st->devstate[i] = newstate;
0df46c2a 402 st->devid[i] = makedev(disc.major, disc.minor);
52826846 403 }
52826846
NB
404 st->active = array.active_disks;
405 st->working = array.working_disks;
406 st->spare = array.spare_disks;
407 st->failed = array.failed_disks;
408 st->utime = array.utime;
e0d19036
NB
409 st->raid = array.raid_disks;
410 st->err = 0;
52826846 411 }
e0d19036
NB
412 /* now check if there are any new devices found in mdstat */
413 if (scan) {
414 struct mdstat_ent *mse;
aba69144 415 for (mse=mdstat; mse; mse=mse->next)
4450e59f 416 if (mse->devnum != INT_MAX &&
2cdb6489 417 mse->level &&
66f8bbbe
DL
418 (strcmp(mse->level, "raid0")!=0 &&
419 strcmp(mse->level, "linear")!=0)
5787fa49 420 ) {
e0d19036 421 struct state *st = malloc(sizeof *st);
98c6faba
NB
422 mdu_array_info_t array;
423 int fd;
e0d19036
NB
424 if (st == NULL)
425 continue;
426 st->devname = strdup(get_md_name(mse->devnum));
98c6faba
NB
427 if ((fd = open(st->devname, O_RDONLY)) < 0 ||
428 ioctl(fd, GET_ARRAY_INFO, &array)< 0) {
429 /* no such array */
430 if (fd >=0) close(fd);
8bd2e0c3 431 put_md_name(st->devname);
98c6faba
NB
432 free(st->devname);
433 free(st);
434 continue;
435 }
dd0781e5 436 close(fd);
e0d19036
NB
437 st->utime = 0;
438 st->next = statelist;
439 st->err = 1;
440 st->devnum = mse->devnum;
441 st->percent = -2;
442 st->spare_group = NULL;
98c6faba 443 st->expected_spares = -1;
e0d19036 444 statelist = st;
bc854448
N
445 if (test)
446 alert("TestMessage", st->devname, NULL, mailaddr, mailfrom, alert_cmd, dosyslog);
4948b8f7 447 alert("NewArray", st->devname, NULL, mailaddr, mailfrom, alert_cmd, dosyslog);
aa88f531 448 new_found = 1;
e0d19036
NB
449 }
450 }
451 /* If an array has active < raid && spare == 0 && spare_group != NULL
452 * Look for another array with spare > 0 and active == raid and same spare_group
453 * if found, choose a device and hotremove/hotadd
454 */
455 for (st = statelist; st; st=st->next)
456 if (st->active < st->raid &&
457 st->spare == 0 &&
458 st->spare_group != NULL) {
459 struct state *st2;
460 for (st2=statelist ; st2 ; st2=st2->next)
461 if (st2 != st &&
462 st2->spare > 0 &&
463 st2->active == st2->raid &&
464 st2->spare_group != NULL &&
465 strcmp(st->spare_group, st2->spare_group) == 0) {
466 /* try to remove and add */
467 int fd1 = open(st->devname, O_RDONLY);
468 int fd2 = open(st2->devname, O_RDONLY);
469 int dev = -1;
470 int d;
471 if (fd1 < 0 || fd2 < 0) {
472 if (fd1>=0) close(fd1);
473 if (fd2>=0) close(fd2);
474 continue;
475 }
e27d562b 476 for (d=st2->raid; d < MaxDisks; d++) {
e0d19036
NB
477 if (st2->devid[d] > 0 &&
478 st2->devstate[d] == 0) {
479 dev = st2->devid[d];
480 break;
481 }
482 }
483 if (dev > 0) {
aba69144 484 if (ioctl(fd2, HOT_REMOVE_DISK,
e0d19036
NB
485 (unsigned long)dev) == 0) {
486 if (ioctl(fd1, HOT_ADD_DISK,
487 (unsigned long)dev) == 0) {
4948b8f7 488 alert("MoveSpare", st->devname, st2->devname, mailaddr, mailfrom, alert_cmd, dosyslog);
e0d19036
NB
489 close(fd1);
490 close(fd2);
491 break;
492 }
493 else ioctl(fd2, HOT_ADD_DISK, (unsigned long) dev);
494 }
495 }
496 close(fd1);
497 close(fd2);
498 }
499 }
aa88f531
NB
500 if (!new_found) {
501 if (oneshot)
502 break;
503 else
dd0781e5 504 mdstat_wait(period);
aa88f531 505 }
98c6faba 506 test = 0;
52826846 507 }
b5e64645
NB
508 if (pidfile)
509 unlink(pidfile);
52826846
NB
510 return 0;
511}
512
513
4948b8f7 514static void alert(char *event, char *dev, char *disc, char *mailaddr, char *mailfrom, char *cmd,
773135f5 515 int dosyslog)
52826846 516{
773135f5
NB
517 int priority;
518
cd29a5c8
NB
519 if (!cmd && !mailaddr) {
520 time_t now = time(0);
aba69144 521
e0d19036 522 printf("%1.15s: %s on %s %s\n", ctime(&now)+4, event, dev, disc?disc:"unknown device");
cd29a5c8 523 }
52826846
NB
524 if (cmd) {
525 int pid = fork();
526 switch(pid) {
527 default:
528 waitpid(pid, NULL, 0);
529 break;
530 case -1:
531 break;
532 case 0:
533 execl(cmd, cmd, event, dev, disc, NULL);
534 exit(2);
535 }
536 }
aba69144
NB
537 if (mailaddr &&
538 (strncmp(event, "Fail", 4)==0 ||
98c6faba 539 strncmp(event, "Test", 4)==0 ||
d1732eeb 540 strncmp(event, "Spares", 6)==0 ||
aa88f531 541 strncmp(event, "Degrade", 7)==0)) {
52826846
NB
542 FILE *mp = popen(Sendmail, "w");
543 if (mp) {
008e1100 544 FILE *mdstat;
52826846
NB
545 char hname[256];
546 gethostname(hname, sizeof(hname));
547 signal(SIGPIPE, SIG_IGN);
4948b8f7
NB
548 if (mailfrom)
549 fprintf(mp, "From: %s\n", mailfrom);
550 else
551 fprintf(mp, "From: " Name " monitoring <root>\n");
52826846
NB
552 fprintf(mp, "To: %s\n", mailaddr);
553 fprintf(mp, "Subject: %s event on %s:%s\n\n", event, dev, hname);
554
555 fprintf(mp, "This is an automatically generated mail message from " Name "\n");
556 fprintf(mp, "running on %s\n\n", hname);
557
558 fprintf(mp, "A %s event had been detected on md device %s.\n\n", event, dev);
559
37dfc3d6 560 if (disc && disc[0] != ' ')
56eedc1a 561 fprintf(mp, "It could be related to component device %s.\n\n", disc);
37dfc3d6
NB
562 if (disc && disc[0] == ' ')
563 fprintf(mp, "Extra information:%s.\n\n", disc);
52826846
NB
564
565 fprintf(mp, "Faithfully yours, etc.\n");
008e1100
NB
566
567 mdstat = fopen("/proc/mdstat", "r");
568 if (mdstat) {
569 char buf[8192];
570 int n;
a524a7ee 571 fprintf(mp, "\nP.S. The /proc/mdstat file currently contains the following:\n\n");
008e1100 572 while ( (n=fread(buf, 1, sizeof(buf), mdstat)) > 0)
9fca7d62 573 n=fwrite(buf, 1, n, mp); /* yes, i don't care about the result */
008e1100
NB
574 fclose(mdstat);
575 }
52826846
NB
576 fclose(mp);
577 }
578
579 }
773135f5
NB
580
581 /* log the event to syslog maybe */
582 if (dosyslog) {
583 /* Log at a different severity depending on the event.
584 *
585 * These are the critical events: */
586 if (strncmp(event, "Fail", 4)==0 ||
587 strncmp(event, "Degrade", 7)==0 ||
588 strncmp(event, "DeviceDisappeared", 17)==0)
589 priority = LOG_CRIT;
590 /* Good to know about, but are not failures: */
591 else if (strncmp(event, "Rebuild", 7)==0 ||
d1732eeb
NB
592 strncmp(event, "MoveSpare", 9)==0 ||
593 strncmp(event, "Spares", 6) != 0)
773135f5
NB
594 priority = LOG_WARNING;
595 /* Everything else: */
596 else
597 priority = LOG_INFO;
598
599 if (disc)
600 syslog(priority, "%s event detected on md device %s, component device %s", event, dev, disc);
601 else
602 syslog(priority, "%s event detected on md device %s", event, dev);
603 }
52826846 604}
b90c0e9a
NB
605
606/* Not really Monitor but ... */
607int Wait(char *dev)
608{
609 struct stat stb;
610 int devnum;
611 int rv = 1;
612
613 if (stat(dev, &stb) != 0) {
614 fprintf(stderr, Name ": Cannot find %s: %s\n", dev,
615 strerror(errno));
616 return 2;
617 }
c94709e8 618 devnum = stat2devnum(&stb);
b90c0e9a
NB
619
620 while(1) {
621 struct mdstat_ent *ms = mdstat_read(1, 0);
622 struct mdstat_ent *e;
623
624 for (e=ms ; e; e=e->next)
625 if (e->devnum == devnum)
626 break;
627
628 if (!e || e->percent < 0) {
e7783ee6 629 if (e && e->metadata_version &&
c94709e8
DW
630 strncmp(e->metadata_version, "external:", 9) == 0) {
631 if (is_subarray(&e->metadata_version[9]))
632 ping_monitor(&e->metadata_version[9]);
633 else
634 ping_monitor(devnum2devname(devnum));
635 }
b90c0e9a
NB
636 free_mdstat(ms);
637 return rv;
638 }
89a10d84 639 free_mdstat(ms);
b90c0e9a
NB
640 rv = 0;
641 mdstat_wait(5);
642 }
643}
1770662b
DW
644
645static char *clean_states[] = {
646 "clear", "inactive", "readonly", "read-auto", "clean", NULL };
647
27dec8fa 648int WaitClean(char *dev, int verbose)
1770662b
DW
649{
650 int fd;
651 struct mdinfo *mdi;
652 int rv = 1;
653 int devnum;
654
655 fd = open(dev, O_RDONLY);
656 if (fd < 0) {
27dec8fa
DW
657 if (verbose)
658 fprintf(stderr, Name ": Couldn't open %s: %s\n", dev, strerror(errno));
1770662b
DW
659 return 1;
660 }
661
662 devnum = fd2devnum(fd);
663 mdi = sysfs_read(fd, devnum, GET_VERSION|GET_LEVEL|GET_SAFEMODE);
664 if (!mdi) {
27dec8fa
DW
665 if (verbose)
666 fprintf(stderr, Name ": Failed to read sysfs attributes for "
667 "%s\n", dev);
1770662b
DW
668 close(fd);
669 return 0;
670 }
671
672 switch(mdi->array.level) {
673 case LEVEL_LINEAR:
674 case LEVEL_MULTIPATH:
675 case 0:
676 /* safemode delay is irrelevant for these levels */
677 rv = 0;
678
679 }
680
681 /* for internal metadata the kernel handles the final clean
682 * transition, containers can never be dirty
683 */
684 if (!is_subarray(mdi->text_version))
685 rv = 0;
686
687 /* safemode disabled ? */
688 if (mdi->safe_mode_delay == 0)
689 rv = 0;
690
691 if (rv) {
692 int state_fd = sysfs_open(fd2devnum(fd), NULL, "array_state");
1770662b 693 char buf[20];
0dd3ba30
DW
694 fd_set fds;
695 struct timeval tm;
1770662b 696
0dd3ba30
DW
697 /* minimize the safe_mode_delay and prepare to wait up to 5s
698 * for writes to quiesce
699 */
700 sysfs_set_safemode(mdi, 1);
701 tm.tv_sec = 5;
702 tm.tv_usec = 0;
703
7146ec6a
DW
704 /* give mdmon a chance to checkpoint resync */
705 sysfs_set_str(mdi, NULL, "sync_action", "idle");
706
0dd3ba30 707 FD_ZERO(&fds);
1770662b 708
0dd3ba30
DW
709 /* wait for array_state to be clean */
710 while (1) {
1770662b
DW
711 rv = read(state_fd, buf, sizeof(buf));
712 if (rv < 0)
713 break;
714 if (sysfs_match_word(buf, clean_states) <= 4)
715 break;
0dd3ba30 716 FD_SET(state_fd, &fds);
28005287 717 rv = select(state_fd + 1, NULL, NULL, &fds, &tm);
0dd3ba30
DW
718 if (rv < 0 && errno != EINTR)
719 break;
1770662b
DW
720 lseek(state_fd, 0, SEEK_SET);
721 }
722 if (rv < 0)
723 rv = 1;
0dd3ba30 724 else if (ping_monitor(mdi->text_version) == 0) {
1770662b
DW
725 /* we need to ping to close the window between array
726 * state transitioning to clean and the metadata being
727 * marked clean
728 */
0dd3ba30
DW
729 rv = 0;
730 } else
731 rv = 1;
27dec8fa 732 if (rv && verbose)
1770662b
DW
733 fprintf(stderr, Name ": Error waiting for %s to be clean\n",
734 dev);
735
0dd3ba30
DW
736 /* restore the original safe_mode_delay */
737 sysfs_set_safemode(mdi, mdi->safe_mode_delay);
1770662b
DW
738 close(state_fd);
739 }
740
0dd3ba30 741 sysfs_free(mdi);
1770662b
DW
742 close(fd);
743
744 return rv;
745}
746
747