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