]> git.ipfire.org Git - thirdparty/mdadm.git/blame - Monitor.c
Revert "mdadm: fix coredump of mdadm --monitor -r"
[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>
4450e59f 29#include <limits.h>
773135f5 30#include <syslog.h>
af3396da 31#ifndef NO_LIBUDEV
49b69533 32#include <libudev.h>
af3396da 33#endif
52826846 34
2e0172b1
N
35struct state {
36 char *devname;
4dd2df09 37 char devnm[32]; /* to sync with mdstat info */
1d13b599 38 unsigned int utime;
2e0172b1
N
39 int err;
40 char *spare_group;
41 int active, working, failed, spare, raid;
f1661bd7 42 int from_config;
73ff0732 43 int from_auto;
2e0172b1 44 int expected_spares;
b0599bda
N
45 int devstate[MAX_DISKS];
46 dev_t devid[MAX_DISKS];
2e0172b1 47 int percent;
4dd2df09
N
48 char parent_devnm[32]; /* For subarray, devnm of parent.
49 * For others, ""
50 */
2e0172b1 51 struct supertype *metadata;
c3621c0a
ML
52 struct state *subarray;/* for a container it is a link to first subarray
53 * for a subarray it is a link to next subarray
54 * in the same container */
55 struct state *parent; /* for a subarray it is a link to its container
56 */
2e0172b1
N
57 struct state *next;
58};
59
e0bd6a96
N
60struct alert_info {
61 char *mailaddr;
62 char *mailfrom;
63 char *alert_cmd;
64 int dosyslog;
65};
2e0172b1
N
66static int make_daemon(char *pidfile);
67static int check_one_sharer(int scan);
7f3b2d1d 68static void write_autorebuild_pid(void);
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,
c2ecf5f6 72 int increments, char *prefer);
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);
af3396da 77#ifndef NO_LIBUDEV
49b69533 78static int check_udev_activity(void);
af3396da 79#endif
2e0172b1 80
a655e550 81int Monitor(struct mddev_dev *devlist,
52826846 82 char *mailaddr, char *alert_cmd,
95c50205
N
83 struct context *c,
84 int daemonise, int oneshot,
85 int dosyslog, char *pidfile, int increments,
86 int share)
52826846
NB
87{
88 /*
89 * Every few seconds, scan every md device looking for changes
90 * When a change is found, log it, possibly run the alert command,
91 * and possibly send Email
92 *
93 * For each array, we record:
94 * Update time
95 * active/working/failed/spare drives
96 * State of each device.
e0d19036 97 * %rebuilt if rebuilding
52826846
NB
98 *
99 * If the update time changes, check out all the data again
100 * It is possible that we cannot get the state of each device
101 * due to bugs in the md kernel module.
e0d19036
NB
102 * We also read /proc/mdstat to get rebuild percent,
103 * and to get state on all active devices incase of kernel bug.
52826846 104 *
e0d19036
NB
105 * Events are:
106 * Fail
107 * An active device had Faulty set or Active/Sync removed
108 * FailSpare
109 * A spare device had Faulty set
110 * SpareActive
111 * An active device had a reverse transition
112 * RebuildStarted
113 * percent went from -1 to +ve
9a36a9b7
ZB
114 * RebuildNN
115 * percent went from below to not-below NN%
e0d19036
NB
116 * DeviceDisappeared
117 * Couldn't access a device which was previously visible
52826846
NB
118 *
119 * if we detect an array with active<raid and spare==0
120 * we look at other arrays that have same spare-group
121 * If we find one with active==raid and spare>0,
122 * and if we can get_disk_info and find a name
123 * Then we hot-remove and hot-add to the other array
124 *
e0d19036
NB
125 * If devlist is NULL, then we can monitor everything because --scan
126 * was given. We get an initial list from config file and add anything
127 * that appears in /proc/mdstat
52826846
NB
128 */
129
2e0172b1 130 struct state *statelist = NULL;
b657208c 131 struct state *st2;
52826846 132 int finished = 0;
e0d19036 133 struct mdstat_ent *mdstat = NULL;
72362f18 134 char *mailfrom;
e0bd6a96 135 struct alert_info info;
9f3dd454 136 struct mddev_ident *mdlist;
af3396da 137 int delay_for_event = c->delay;
e0d19036 138
f5ff2988 139 if (!mailaddr)
8aec876d 140 mailaddr = conf_get_mailaddr();
4948b8f7 141
f5ff2988 142 if (!alert_cmd)
8aec876d 143 alert_cmd = conf_get_program();
f5ff2988
KT
144
145 mailfrom = conf_get_mailfrom();
146
95c50205 147 if (c->scan && !mailaddr && !alert_cmd && !dosyslog) {
e7b84f9d 148 pr_err("No mail address or alert command - not monitoring.\n");
d013a55e 149 return 1;
56eedc1a 150 }
f5ff2988
KT
151
152 if (c->verbose) {
153 pr_err("Monitor is started with delay %ds\n", c->delay);
154 if (mailaddr)
155 pr_err("Monitor using email address %s\n", mailaddr);
156 if (alert_cmd)
157 pr_err("Monitor using program %s\n", alert_cmd);
158 }
159
e0bd6a96
N
160 info.alert_cmd = alert_cmd;
161 info.mailaddr = mailaddr;
162 info.mailfrom = mailfrom;
163 info.dosyslog = dosyslog;
d013a55e 164
7f3b2d1d
BK
165 if (share){
166 if (check_one_sharer(c->scan))
167 return 1;
168 }
169
44d337f0
AC
170 if (daemonise) {
171 int rv = make_daemon(pidfile);
172 if (rv >= 0)
173 return rv;
174 }
e0d19036 175
1011e834 176 if (share)
7f3b2d1d 177 write_autorebuild_pid();
edde9560 178
e0d19036 179 if (devlist == NULL) {
9f3dd454 180 mdlist = conf_get_ident(NULL);
f566ef45 181 for (; mdlist; mdlist = mdlist->next) {
fe056d1f 182 struct state *st;
f566ef45 183
fe056d1f
N
184 if (mdlist->devname == NULL)
185 continue;
112cace6
N
186 if (strcasecmp(mdlist->devname, "<ignore>") == 0)
187 continue;
e702f392 188
503975b9 189 st = xcalloc(1, sizeof *st);
db2d001c 190 if (mdlist->devname[0] == '/')
503975b9 191 st->devname = xstrdup(mdlist->devname);
db2d001c 192 else {
503975b9 193 st->devname = xmalloc(8+strlen(mdlist->devname)+1);
db2d001c
N
194 strcpy(strcpy(st->devname, "/dev/md/"),
195 mdlist->devname);
196 }
e702f392
KT
197 if (!is_mddev(mdlist->devname))
198 return 1;
e0d19036 199 st->next = statelist;
4dd2df09 200 st->devnm[0] = 0;
9dad51d4 201 st->percent = RESYNC_UNKNOWN;
f1661bd7 202 st->from_config = 1;
feb716e9 203 st->expected_spares = mdlist->spare_disks;
e0d19036 204 if (mdlist->spare_group)
503975b9 205 st->spare_group = xstrdup(mdlist->spare_group);
e0d19036
NB
206 statelist = st;
207 }
208 } else {
a655e550 209 struct mddev_dev *dv;
f566ef45
JS
210
211 for (dv = devlist; dv; dv = dv->next) {
e702f392
KT
212 struct state *st;
213
214 if (!is_mddev(dv->devname))
215 return 1;
216
217 st = xcalloc(1, sizeof *st);
9f3dd454 218 mdlist = conf_get_ident(dv->devname);
503975b9 219 st->devname = xstrdup(dv->devname);
e0d19036 220 st->next = statelist;
4dd2df09 221 st->devnm[0] = 0;
9dad51d4 222 st->percent = RESYNC_UNKNOWN;
feb716e9 223 st->expected_spares = -1;
e5329c37
NB
224 if (mdlist) {
225 st->expected_spares = mdlist->spare_disks;
226 if (mdlist->spare_group)
503975b9 227 st->spare_group = xstrdup(mdlist->spare_group);
e5329c37 228 }
e0d19036
NB
229 statelist = st;
230 }
231 }
232
f566ef45 233 while (!finished) {
aa88f531 234 int new_found = 0;
73ff0732 235 struct state *st, **stp;
a90e1050 236 int anydegraded = 0;
007087d0 237 int anyredundant = 0;
e0d19036
NB
238
239 if (mdstat)
240 free_mdstat(mdstat);
f566ef45 241 mdstat = mdstat_read(oneshot ? 0 : 1, 0);
e0d19036 242
007087d0 243 for (st = statelist; st; st = st->next) {
95c50205
N
244 if (check_array(st, mdstat, c->test, &info,
245 increments, c->prefer))
a90e1050 246 anydegraded = 1;
007087d0
MT
247 /* for external arrays, metadata is filled for
248 * containers only
249 */
250 if (st->metadata && st->metadata->ss->external)
251 continue;
252 if (st->err == 0 && !anyredundant)
253 anyredundant = 1;
254 }
1011e834 255
e0d19036 256 /* now check if there are any new devices found in mdstat */
95c50205
N
257 if (c->scan)
258 new_found = add_new_arrays(mdstat, &statelist, c->test,
e0bd6a96 259 &info);
2e0172b1 260
e0d19036
NB
261 /* If an array has active < raid && spare == 0 && spare_group != NULL
262 * Look for another array with spare > 0 and active == raid and same spare_group
49b69533 263 * if found, choose a device and hotremove/hotadd
e0d19036 264 */
a90e1050 265 if (share && anydegraded)
e0bd6a96 266 try_spare_migration(statelist, &info);
aa88f531
NB
267 if (!new_found) {
268 if (oneshot)
269 break;
007087d0 270 else if (!anyredundant) {
8c80d305 271 pr_err("No array with redundancy detected, stopping\n");
007087d0
MT
272 break;
273 }
e2308733 274 else {
af3396da 275#ifndef NO_LIBUDEV
cab9c67d 276 /*
af3396da
MT
277 * Wait for udevd to finish new devices
278 * processing.
cab9c67d 279 */
af3396da
MT
280 if (mdstat_wait(delay_for_event) &&
281 check_udev_activity())
49b69533 282 pr_err("Error while waiting for UDEV to complete new devices processing\n");
af3396da
MT
283#else
284 int wait_result = mdstat_wait(delay_for_event);
285 /*
286 * Give chance to process new device
287 */
288 if (wait_result != 0) {
289 if (c->delay > 5)
290 delay_for_event = 5;
291 } else
292 delay_for_event = c->delay;
293#endif
e2308733
MT
294 mdstat_close();
295 }
aa88f531 296 }
95c50205 297 c->test = 0;
73ff0732
N
298
299 for (stp = &statelist; (st = *stp) != NULL; ) {
300 if (st->from_auto && st->err > 5) {
301 *stp = st->next;
302 free(st->devname);
303 free(st->spare_group);
304 free(st);
305 } else
306 stp = &st->next;
307 }
52826846 308 }
b657208c
JS
309 for (st2 = statelist; st2; st2 = statelist) {
310 statelist = st2->next;
311 free(st2);
312 }
313
b5e64645
NB
314 if (pidfile)
315 unlink(pidfile);
52826846
NB
316 return 0;
317}
318
2e0172b1
N
319static int make_daemon(char *pidfile)
320{
44d337f0
AC
321 /* Return:
322 * -1 in the forked daemon
323 * 0 in the parent
324 * 1 on error
325 * so a none-negative becomes the exit code.
326 */
2e0172b1
N
327 int pid = fork();
328 if (pid > 0) {
329 if (!pidfile)
330 printf("%d\n", pid);
331 else {
ca4b156b
MT
332 FILE *pid_file = NULL;
333 int fd = open(pidfile, O_WRONLY | O_CREAT | O_TRUNC,
334 0644);
335 if (fd >= 0)
336 pid_file = fdopen(fd, "w");
2e0172b1
N
337 if (!pid_file)
338 perror("cannot create pid file");
339 else {
340 fprintf(pid_file,"%d\n", pid);
341 fclose(pid_file);
342 }
343 }
344 return 0;
345 }
346 if (pid < 0) {
347 perror("daemonise");
348 return 1;
349 }
ff6bb131 350 manage_fork_fds(0);
2e0172b1 351 setsid();
44d337f0 352 return -1;
2e0172b1
N
353}
354
355static int check_one_sharer(int scan)
356{
185ec439
CL
357 int pid;
358 FILE *comm_fp;
2e0172b1 359 FILE *fp;
7f3b2d1d
BK
360 char comm_path[PATH_MAX];
361 char path[PATH_MAX];
185ec439
CL
362 char comm[20];
363
0011874f
JS
364 sprintf(path, "%s/autorebuild.pid", MDMON_DIR);
365 fp = fopen(path, "r");
2e0172b1 366 if (fp) {
71204a50
N
367 if (fscanf(fp, "%d", &pid) != 1)
368 pid = -1;
185ec439
CL
369 snprintf(comm_path, sizeof(comm_path),
370 "/proc/%d/comm", pid);
371 comm_fp = fopen(comm_path, "r");
372 if (comm_fp) {
1c66260d 373 if (fscanf(comm_fp, "%19s", comm) &&
185ec439
CL
374 strncmp(basename(comm), Name, strlen(Name)) == 0) {
375 if (scan) {
376 pr_err("Only one autorebuild process allowed in scan mode, aborting\n");
377 fclose(comm_fp);
378 fclose(fp);
379 return 1;
380 } else {
381 pr_err("Warning: One autorebuild process already running.\n");
382 }
2e0172b1 383 }
185ec439 384 fclose(comm_fp);
2e0172b1
N
385 }
386 fclose(fp);
387 }
7f3b2d1d
BK
388 return 0;
389}
390
391static void write_autorebuild_pid()
392{
393 char path[PATH_MAX];
394 int pid;
ca4b156b 395 FILE *fp = NULL;
7f3b2d1d
BK
396 sprintf(path, "%s/autorebuild.pid", MDMON_DIR);
397
ca4b156b 398 if (mkdir(MDMON_DIR, 0700) < 0 && errno != EEXIST) {
7f3b2d1d
BK
399 pr_err("Can't create autorebuild.pid file\n");
400 } else {
ca4b156b
MT
401 int fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0700);
402
403 if (fd >= 0)
404 fp = fdopen(fd, "w");
405
7f3b2d1d 406 if (!fp)
7a862a02 407 pr_err("Can't create autorebuild.pid file\n");
7f3b2d1d
BK
408 else {
409 pid = getpid();
410 fprintf(fp, "%d\n", pid);
411 fclose(fp);
2e0172b1
N
412 }
413 }
2e0172b1 414}
52826846 415
e0bd6a96 416static void alert(char *event, char *dev, char *disc, struct alert_info *info)
52826846 417{
773135f5
NB
418 int priority;
419
b1bac75b 420 if (!info->alert_cmd && !info->mailaddr && !info->dosyslog) {
cd29a5c8 421 time_t now = time(0);
aba69144 422
f566ef45
JS
423 printf("%1.15s: %s on %s %s\n", ctime(&now) + 4,
424 event, dev, disc?disc:"unknown device");
cd29a5c8 425 }
e0bd6a96 426 if (info->alert_cmd) {
52826846
NB
427 int pid = fork();
428 switch(pid) {
429 default:
430 waitpid(pid, NULL, 0);
431 break;
432 case -1:
433 break;
434 case 0:
e0bd6a96
N
435 execl(info->alert_cmd, info->alert_cmd,
436 event, dev, disc, NULL);
52826846
NB
437 exit(2);
438 }
439 }
f566ef45
JS
440 if (info->mailaddr && (strncmp(event, "Fail", 4) == 0 ||
441 strncmp(event, "Test", 4) == 0 ||
442 strncmp(event, "Spares", 6) == 0 ||
443 strncmp(event, "Degrade", 7) == 0)) {
52826846
NB
444 FILE *mp = popen(Sendmail, "w");
445 if (mp) {
008e1100 446 FILE *mdstat;
52826846 447 char hname[256];
83a379cf 448
52826846 449 gethostname(hname, sizeof(hname));
83a379cf
LF
450 signal_s(SIGPIPE, SIG_IGN);
451
e0bd6a96
N
452 if (info->mailfrom)
453 fprintf(mp, "From: %s\n", info->mailfrom);
4948b8f7 454 else
f566ef45
JS
455 fprintf(mp, "From: %s monitoring <root>\n",
456 Name);
e0bd6a96
N
457 fprintf(mp, "To: %s\n", info->mailaddr);
458 fprintf(mp, "Subject: %s event on %s:%s\n\n",
459 event, dev, hname);
52826846 460
e0bd6a96 461 fprintf(mp,
7a862a02 462 "This is an automatically generated mail message from %s\n", Name);
52826846
NB
463 fprintf(mp, "running on %s\n\n", hname);
464
e0bd6a96 465 fprintf(mp,
7a862a02 466 "A %s event had been detected on md device %s.\n\n", event, dev);
52826846 467
37dfc3d6 468 if (disc && disc[0] != ' ')
e0bd6a96 469 fprintf(mp,
7a862a02 470 "It could be related to component device %s.\n\n", disc);
37dfc3d6
NB
471 if (disc && disc[0] == ' ')
472 fprintf(mp, "Extra information:%s.\n\n", disc);
52826846
NB
473
474 fprintf(mp, "Faithfully yours, etc.\n");
008e1100
NB
475
476 mdstat = fopen("/proc/mdstat", "r");
477 if (mdstat) {
478 char buf[8192];
479 int n;
e0bd6a96 480 fprintf(mp,
7a862a02 481 "\nP.S. The /proc/mdstat file currently contains the following:\n\n");
f566ef45
JS
482 while ((n = fread(buf, 1, sizeof(buf),
483 mdstat)) > 0)
484 n = fwrite(buf, 1, n, mp);
008e1100
NB
485 fclose(mdstat);
486 }
6278fb3a 487 pclose(mp);
52826846 488 }
52826846 489 }
773135f5
NB
490
491 /* log the event to syslog maybe */
e0bd6a96 492 if (info->dosyslog) {
773135f5
NB
493 /* Log at a different severity depending on the event.
494 *
495 * These are the critical events: */
f566ef45
JS
496 if (strncmp(event, "Fail", 4) == 0 ||
497 strncmp(event, "Degrade", 7) == 0 ||
498 strncmp(event, "DeviceDisappeared", 17) == 0)
773135f5
NB
499 priority = LOG_CRIT;
500 /* Good to know about, but are not failures: */
f566ef45
JS
501 else if (strncmp(event, "Rebuild", 7) == 0 ||
502 strncmp(event, "MoveSpare", 9) == 0 ||
d1732eeb 503 strncmp(event, "Spares", 6) != 0)
773135f5
NB
504 priority = LOG_WARNING;
505 /* Everything else: */
506 else
507 priority = LOG_INFO;
508
b033913a 509 if (disc && disc[0] != ' ')
e0bd6a96 510 syslog(priority,
7a862a02 511 "%s event detected on md device %s, component device %s", event, dev, disc);
b033913a
N
512 else if (disc)
513 syslog(priority,
514 "%s event detected on md device %s: %s",
515 event, dev, disc);
773135f5 516 else
e0bd6a96
N
517 syslog(priority,
518 "%s event detected on md device %s",
519 event, dev);
773135f5 520 }
52826846 521}
b90c0e9a 522
a90e1050 523static int check_array(struct state *st, struct mdstat_ent *mdstat,
e0bd6a96 524 int test, struct alert_info *ainfo,
c2ecf5f6 525 int increments, char *prefer)
2e0172b1 526{
ff044d6b
AC
527 /* Update the state 'st' to reflect any changes shown in mdstat,
528 * or found by directly examining the array, and return
529 * '1' if the array is degraded, or '0' if it is optimal (or dead).
530 */
b0599bda 531 struct { int state, major, minor; } info[MAX_DISKS];
aed5f5c3 532 struct mdinfo *sra = NULL;
2e0172b1
N
533 mdu_array_info_t array;
534 struct mdstat_ent *mse = NULL, *mse2;
535 char *dev = st->devname;
b9a0309c 536 int fd;
2e0172b1 537 int i;
9e6d9291
N
538 int remaining_disks;
539 int last_disk;
721b662b 540 int new_array = 0;
1830e74b 541 int retval;
802961a2 542 int is_container = 0;
b3ab4e4d 543 unsigned long redundancy_only_flags = 0;
2e0172b1
N
544
545 if (test)
e0bd6a96 546 alert("TestMessage", dev, NULL, ainfo);
b9a0309c 547
1830e74b
JS
548 retval = 0;
549
2e0172b1 550 fd = open(dev, O_RDONLY);
13e5d845
JS
551 if (fd < 0)
552 goto disappeared;
b9a0309c 553
802961a2
MT
554 if (st->devnm[0] == 0)
555 strcpy(st->devnm, fd2devnm(fd));
556
557 for (mse2 = mdstat; mse2; mse2 = mse2->next)
558 if (strcmp(mse2->devnm, st->devnm) == 0) {
559 mse2->devnm[0] = 0; /* flag it as "used" */
560 mse = mse2;
561 }
562
563 if (!mse) {
564 /* duplicated array in statelist
565 * or re-created after reading mdstat
566 */
567 st->err++;
568 goto out;
569 }
570
571 if (mse->level == NULL)
572 is_container = 1;
573
2dab69c9 574 if (!is_container && !md_array_active(fd))
13e5d845 575 goto disappeared;
b9a0309c 576
2e0172b1 577 fcntl(fd, F_SETFD, FD_CLOEXEC);
13e5d845
JS
578 if (md_get_array_info(fd, &array) < 0)
579 goto disappeared;
580
b3ab4e4d
MT
581 if (!is_container && map_name(pers, mse->level) > 0)
582 redundancy_only_flags |= GET_MISMATCH;
802961a2
MT
583
584 sra = sysfs_read(-1, st->devnm, GET_LEVEL | GET_DISKS | GET_DEVS |
b3ab4e4d 585 GET_STATE | redundancy_only_flags);
aed5f5c3 586
aed5f5c3
JS
587 if (!sra)
588 goto disappeared;
589
2e0172b1
N
590 /* It's much easier to list what array levels can't
591 * have a device disappear than all of them that can
592 */
48bc2ade 593 if (sra->array.level == 0 || sra->array.level == -1) {
f1661bd7 594 if (!st->err && !st->from_config)
04e27c20 595 alert("DeviceDisappeared", dev, " Wrong-Level", ainfo);
73ff0732 596 st->err++;
1830e74b 597 goto out;
2e0172b1 598 }
2e0172b1 599
2e0172b1
N
600 /* this array is in /proc/mdstat */
601 if (array.utime == 0)
602 /* external arrays don't update utime, so
603 * just make sure it is always different. */
604 array.utime = st->utime + 1;;
605
0f760384 606 if (st->err) {
73ff0732 607 /* New array appeared where previously had an error */
0f760384
N
608 st->err = 0;
609 st->percent = RESYNC_NONE;
721b662b 610 new_array = 1;
007087d0
MT
611 if (!is_container)
612 alert("NewArray", st->devname, NULL, ainfo);
0f760384
N
613 }
614
b8e5713c 615 if (st->utime == array.utime && st->failed == sra->array.failed_disks &&
e5eb6857 616 st->working == sra->array.working_disks &&
b98943a4 617 st->spare == sra->array.spare_disks &&
d7be7d87 618 (mse == NULL || (mse->percent == st->percent))) {
ff044d6b 619 if ((st->active < st->raid) && st->spare == 0)
1830e74b
JS
620 retval = 1;
621 goto out;
2e0172b1
N
622 }
623 if (st->utime == 0 && /* new array */
f27904a5 624 mse->pattern && strchr(mse->pattern, '_') /* degraded */)
e0bd6a96 625 alert("DegradedArray", dev, NULL, ainfo);
2e0172b1 626
b98943a4
JS
627 if (st->utime == 0 && /* new array */ st->expected_spares > 0 &&
628 sra->array.spare_disks < st->expected_spares)
e0bd6a96 629 alert("SparesMissing", dev, NULL, ainfo);
9dad51d4 630 if (st->percent < 0 && st->percent != RESYNC_UNKNOWN &&
2e0172b1 631 mse->percent >= 0)
e0bd6a96 632 alert("RebuildStarted", dev, NULL, ainfo);
f27904a5 633 if (st->percent >= 0 && mse->percent >= 0 &&
2e0172b1 634 (mse->percent / increments) > (st->percent / increments)) {
97589839 635 char percentalert[18];
f27904a5
JS
636 /*
637 * "RebuildNN" (10 chars) or "RebuildStarted" (15 chars)
638 */
2e0172b1
N
639
640 if((mse->percent / increments) == 0)
f27904a5
JS
641 snprintf(percentalert, sizeof(percentalert),
642 "RebuildStarted");
2e0172b1 643 else
f27904a5
JS
644 snprintf(percentalert, sizeof(percentalert),
645 "Rebuild%02d", mse->percent);
2e0172b1 646
e0bd6a96 647 alert(percentalert, dev, NULL, ainfo);
2e0172b1
N
648 }
649
f27904a5 650 if (mse->percent == RESYNC_NONE && st->percent >= 0) {
2e0172b1
N
651 /* Rebuild/sync/whatever just finished.
652 * If there is a number in /mismatch_cnt,
653 * we should report that.
654 */
2e0172b1 655 if (sra && sra->mismatch_cnt > 0) {
8453f8d0
LD
656 char cnt[80];
657 snprintf(cnt, sizeof(cnt),
658 " mismatches found: %d (on raid level %d)",
f566ef45 659 sra->mismatch_cnt, sra->array.level);
e0bd6a96 660 alert("RebuildFinished", dev, cnt, ainfo);
2e0172b1 661 } else
e0bd6a96 662 alert("RebuildFinished", dev, NULL, ainfo);
2e0172b1
N
663 }
664 st->percent = mse->percent;
665
b98943a4 666 remaining_disks = sra->array.nr_disks;
f27904a5 667 for (i = 0; i < MAX_DISKS && remaining_disks > 0; i++) {
2e0172b1
N
668 mdu_disk_info_t disc;
669 disc.number = i;
d97572f5 670 if (md_get_disk_info(fd, &disc) >= 0) {
2e0172b1
N
671 info[i].state = disc.state;
672 info[i].major = disc.major;
673 info[i].minor = disc.minor;
9e6d9291
N
674 if (disc.major || disc.minor)
675 remaining_disks --;
2e0172b1
N
676 } else
677 info[i].major = info[i].minor = 0;
678 }
9e6d9291 679 last_disk = i;
2e0172b1 680
4019ad07
JL
681 if (mse->metadata_version &&
682 strncmp(mse->metadata_version, "external:", 9) == 0 &&
4dd2df09
N
683 is_subarray(mse->metadata_version+9)) {
684 char *sl;
f566ef45 685 strcpy(st->parent_devnm, mse->metadata_version + 10);
4dd2df09
N
686 sl = strchr(st->parent_devnm, '/');
687 if (sl)
688 *sl = 0;
689 } else
690 st->parent_devnm[0] = 0;
f27904a5 691 if (st->metadata == NULL && st->parent_devnm[0] == 0)
2e0172b1
N
692 st->metadata = super_by_fd(fd, NULL);
693
f566ef45
JS
694 for (i = 0; i < MAX_DISKS; i++) {
695 mdu_disk_info_t disc = {0, 0, 0, 0, 0};
696 int newstate = 0;
2e0172b1
N
697 int change;
698 char *dv = NULL;
699 disc.number = i;
f27904a5 700 if (i < last_disk && (info[i].major || info[i].minor)) {
2e0172b1 701 newstate = info[i].state;
f27904a5
JS
702 dv = map_dev_preferred(info[i].major, info[i].minor, 1,
703 prefer);
2e0172b1
N
704 disc.state = newstate;
705 disc.major = info[i].major;
706 disc.minor = info[i].minor;
721b662b
N
707 } else
708 newstate = (1 << MD_DISK_REMOVED);
709
2e0172b1 710 if (dv == NULL && st->devid[i])
f27904a5
JS
711 dv = map_dev_preferred(major(st->devid[i]),
712 minor(st->devid[i]), 1, prefer);
2e0172b1 713 change = newstate ^ st->devstate[i];
721b662b 714 if (st->utime && change && !st->err && !new_array) {
f27904a5 715 if ((st->devstate[i]&change) & (1 << MD_DISK_SYNC))
e0bd6a96 716 alert("Fail", dev, dv, ainfo);
f27904a5 717 else if ((newstate & (1 << MD_DISK_FAULTY)) &&
2e0172b1 718 (disc.major || disc.minor) &&
f27904a5
JS
719 st->devid[i] == makedev(disc.major,
720 disc.minor))
e0bd6a96 721 alert("FailSpare", dev, dv, ainfo);
f27904a5 722 else if ((newstate&change) & (1 << MD_DISK_SYNC))
e0bd6a96 723 alert("SpareActive", dev, dv, ainfo);
2e0172b1
N
724 }
725 st->devstate[i] = newstate;
726 st->devid[i] = makedev(disc.major, disc.minor);
727 }
b98943a4 728 st->active = sra->array.active_disks;
e5eb6857 729 st->working = sra->array.working_disks;
b98943a4 730 st->spare = sra->array.spare_disks;
b8e5713c 731 st->failed = sra->array.failed_disks;
2e0172b1 732 st->utime = array.utime;
12a9d21f 733 st->raid = sra->array.raid_disks;
2e0172b1 734 st->err = 0;
a90e1050 735 if ((st->active < st->raid) && st->spare == 0)
1830e74b
JS
736 retval = 1;
737
738 out:
aed5f5c3
JS
739 if (sra)
740 sysfs_free(sra);
802961a2 741 if (fd >= 0)
13e5d845 742 close(fd);
1830e74b 743 return retval;
13e5d845
JS
744
745 disappeared:
007087d0 746 if (!st->err && !is_container)
13e5d845
JS
747 alert("DeviceDisappeared", dev, NULL, ainfo);
748 st->err++;
749 goto out;
2e0172b1
N
750}
751
83f3bc5f 752static int add_new_arrays(struct mdstat_ent *mdstat, struct state **statelist,
e0bd6a96 753 int test, struct alert_info *info)
2e0172b1
N
754{
755 struct mdstat_ent *mse;
756 int new_found = 0;
1e08717f 757 char *name;
2e0172b1 758
f566ef45 759 for (mse = mdstat; mse; mse = mse->next)
d7be7d87 760 if (mse->devnm[0] && (!mse->level || /* retrieve containers */
f566ef45
JS
761 (strcmp(mse->level, "raid0") != 0 &&
762 strcmp(mse->level, "linear") != 0))) {
503975b9 763 struct state *st = xcalloc(1, sizeof *st);
2e0172b1
N
764 mdu_array_info_t array;
765 int fd;
1e08717f
SV
766
767 name = get_md_name(mse->devnm);
768 if (!name) {
769 free(st);
770 continue;
771 }
772
773 st->devname = xstrdup(name);
2e0172b1 774 if ((fd = open(st->devname, O_RDONLY)) < 0 ||
9cd39f01 775 md_get_array_info(fd, &array) < 0) {
2e0172b1 776 /* no such array */
9cd39f01
JS
777 if (fd >= 0)
778 close(fd);
2e0172b1
N
779 put_md_name(st->devname);
780 free(st->devname);
781 if (st->metadata) {
782 st->metadata->ss->free_super(st->metadata);
783 free(st->metadata);
784 }
785 free(st);
786 continue;
787 }
788 close(fd);
83f3bc5f 789 st->next = *statelist;
2e0172b1 790 st->err = 1;
73ff0732 791 st->from_auto = 1;
4dd2df09 792 strcpy(st->devnm, mse->devnm);
9dad51d4 793 st->percent = RESYNC_UNKNOWN;
2e0172b1 794 st->expected_spares = -1;
eb28e119 795 if (mse->metadata_version &&
f566ef45
JS
796 strncmp(mse->metadata_version,
797 "external:", 9) == 0 &&
4dd2df09
N
798 is_subarray(mse->metadata_version+9)) {
799 char *sl;
800 strcpy(st->parent_devnm,
801 mse->metadata_version+10);
802 sl = strchr(st->parent_devnm, '/');
803 *sl = 0;
804 } else
805 st->parent_devnm[0] = 0;
83f3bc5f 806 *statelist = st;
2e0172b1 807 if (test)
e0bd6a96 808 alert("TestMessage", st->devname, NULL, info);
2e0172b1
N
809 new_found = 1;
810 }
811 return new_found;
812}
813
fbfdcb06
AO
814static int get_required_spare_criteria(struct state *st,
815 struct spare_criteria *sc)
80e7f8c3
AC
816{
817 int fd;
80e7f8c3 818
f566ef45 819 if (!st->metadata || !st->metadata->ss->get_spare_criteria) {
fbfdcb06 820 sc->min_size = 0;
4b57ecf6 821 sc->sector_size = 0;
de697acc 822 return 0;
300f5033 823 }
80e7f8c3
AC
824
825 fd = open(st->devname, O_RDONLY);
826 if (fd < 0)
de697acc
AC
827 return 1;
828 if (st->metadata->ss->external)
829 st->metadata->ss->load_container(st->metadata, fd, st->devname);
830 else
831 st->metadata->ss->load_super(st->metadata, fd, st->devname);
80e7f8c3 832 close(fd);
de697acc
AC
833 if (!st->metadata->sb)
834 return 1;
fbfdcb06
AO
835
836 st->metadata->ss->get_spare_criteria(st->metadata, sc);
80e7f8c3
AC
837 st->metadata->ss->free_super(st->metadata);
838
de697acc 839 return 0;
80e7f8c3
AC
840}
841
5ec0f373 842static int check_donor(struct state *from, struct state *to)
2feb22ef 843{
66f5c4b6
N
844 struct state *sub;
845
2feb22ef
N
846 if (from == to)
847 return 0;
66f5c4b6
N
848 if (from->parent)
849 /* Cannot move from a member */
2feb22ef 850 return 0;
ff044d6b
AC
851 if (from->err)
852 return 0;
66f5c4b6
N
853 for (sub = from->subarray; sub; sub = sub->subarray)
854 /* If source array has degraded subarrays, don't
855 * remove anything
856 */
857 if (sub->active < sub->raid)
858 return 0;
859 if (from->metadata->ss->external == 0)
860 if (from->active < from->raid)
861 return 0;
2feb22ef
N
862 if (from->spare <= 0)
863 return 0;
e78dda3b 864 return 1;
2feb22ef
N
865}
866
0f0749ad 867static dev_t choose_spare(struct state *from, struct state *to,
f566ef45 868 struct domainlist *domlist, struct spare_criteria *sc)
0fa21e85
N
869{
870 int d;
0f0749ad 871 dev_t dev = 0;
0fa21e85 872
b0599bda 873 for (d = from->raid; !dev && d < MAX_DISKS; d++) {
f566ef45 874 if (from->devid[d] > 0 && from->devstate[d] == 0) {
0fa21e85
N
875 struct dev_policy *pol;
876 unsigned long long dev_size;
4b57ecf6 877 unsigned int dev_sector_size;
0fa21e85 878
bfd76b93
CA
879 if (to->metadata->ss->external &&
880 test_partition_from_id(from->devid[d]))
881 continue;
882
fbfdcb06 883 if (sc->min_size &&
0fa21e85 884 dev_size_from_id(from->devid[d], &dev_size) &&
fbfdcb06 885 dev_size < sc->min_size)
0fa21e85
N
886 continue;
887
4b57ecf6
AO
888 if (sc->sector_size &&
889 dev_sector_size_from_id(from->devid[d],
890 &dev_sector_size) &&
891 sc->sector_size != dev_sector_size)
892 continue;
893
4dd2df09 894 pol = devid_policy(from->devid[d]);
0fa21e85
N
895 if (from->spare_group)
896 pol_add(&pol, pol_domain,
897 from->spare_group, NULL);
f566ef45
JS
898 if (domain_test(domlist, pol,
899 to->metadata->ss->name) == 1)
0fa21e85
N
900 dev = from->devid[d];
901 dev_policy_free(pol);
902 }
903 }
904 return dev;
905}
906
0f0749ad 907static dev_t container_choose_spare(struct state *from, struct state *to,
f0b85306 908 struct domainlist *domlist,
fbfdcb06 909 struct spare_criteria *sc, int active)
5739e0d0
N
910{
911 /* This is similar to choose_spare, but we cannot trust devstate,
912 * so we need to read the metadata instead
913 */
326727d9 914 struct mdinfo *list;
5739e0d0 915 struct supertype *st = from->metadata;
ff044d6b 916 int fd = open(from->devname, O_RDONLY);
5739e0d0 917 int err;
0f0749ad 918 dev_t dev = 0;
5739e0d0
N
919
920 if (fd < 0)
921 return 0;
326727d9
AC
922 if (!st->ss->getinfo_super_disks) {
923 close(fd);
5739e0d0 924 return 0;
326727d9 925 }
1011e834 926
5739e0d0
N
927 err = st->ss->load_container(st, fd, NULL);
928 close(fd);
929 if (err)
930 return 0;
1011e834 931
a1e49d69
CA
932 if (from == to) {
933 /* We must check if number of active disks has not increased
934 * since ioctl in main loop. mdmon may have added spare
935 * to subarray. If so we do not need to look for more spares
936 * so return non zero value */
937 int active_cnt = 0;
938 struct mdinfo *dp;
939 list = st->ss->getinfo_super_disks(st);
940 if (!list) {
941 st->ss->free_super(st);
942 return 1;
943 }
944 dp = list->devs;
945 while (dp) {
f566ef45
JS
946 if (dp->disk.state & (1 << MD_DISK_SYNC) &&
947 !(dp->disk.state & (1 << MD_DISK_FAULTY)))
a1e49d69
CA
948 active_cnt++;
949 dp = dp->next;
950 }
951 sysfs_free(list);
952 if (active < active_cnt) {
953 /* Spare just activated.*/
954 st->ss->free_super(st);
955 return 1;
956 }
957 }
958
326727d9 959 /* We only need one spare so full list not needed */
fbfdcb06 960 list = container_choose_spares(st, sc, domlist, from->spare_group,
326727d9
AC
961 to->metadata->ss->name, 1);
962 if (list) {
963 struct mdinfo *disks = list->devs;
964 if (disks)
965 dev = makedev(disks->disk.major, disks->disk.minor);
966 sysfs_free(list);
5739e0d0 967 }
326727d9 968 st->ss->free_super(st);
5739e0d0
N
969 return dev;
970}
971
e0bd6a96 972static void try_spare_migration(struct state *statelist, struct alert_info *info)
2e0172b1 973{
66f5c4b6
N
974 struct state *from;
975 struct state *st;
fbfdcb06 976 struct spare_criteria sc;
c3621c0a
ML
977
978 link_containers_with_subarrays(statelist);
66f5c4b6 979 for (st = statelist; st; st = st->next)
f566ef45 980 if (st->active < st->raid && st->spare == 0 && !st->err) {
e78dda3b
N
981 struct domainlist *domlist = NULL;
982 int d;
66f5c4b6
N
983 struct state *to = st;
984
4dd2df09 985 if (to->parent_devnm[0] && !to->parent)
c0dc0ad5
CA
986 /* subarray monitored without parent container
987 * we can't move spares here */
988 continue;
1011e834 989
66f5c4b6
N
990 if (to->parent)
991 /* member of a container */
992 to = to->parent;
e78dda3b 993
fbfdcb06 994 if (get_required_spare_criteria(to, &sc))
de697acc 995 continue;
e9a2ac02
AC
996 if (to->metadata->ss->external) {
997 /* We must make sure there is
998 * no suitable spare in container already.
999 * If there is we don't add more */
1000 dev_t devid = container_choose_spare(
fbfdcb06 1001 to, to, NULL, &sc, st->active);
e9a2ac02
AC
1002 if (devid > 0)
1003 continue;
1004 }
b0599bda 1005 for (d = 0; d < MAX_DISKS; d++)
e78dda3b
N
1006 if (to->devid[d])
1007 domainlist_add_dev(&domlist,
1008 to->devid[d],
1009 to->metadata->ss->name);
1010 if (to->spare_group)
1011 domain_add(&domlist, to->spare_group);
5ec0f373
ML
1012 /*
1013 * No spare migration if the destination
1014 * has no domain. Skip this array.
1015 */
1016 if (!domlist)
1017 continue;
0fa21e85 1018 for (from=statelist ; from ; from=from->next) {
0f0749ad 1019 dev_t devid;
5ec0f373 1020 if (!check_donor(from, to))
0fa21e85 1021 continue;
5739e0d0
N
1022 if (from->metadata->ss->external)
1023 devid = container_choose_spare(
fbfdcb06 1024 from, to, domlist, &sc, 0);
5739e0d0 1025 else
f0b85306 1026 devid = choose_spare(from, to, domlist,
fbfdcb06 1027 &sc);
f566ef45
JS
1028 if (devid > 0 &&
1029 move_spare(from->devname, to->devname,
1030 devid)) {
1031 alert("MoveSpare", to->devname,
1032 from->devname, info);
d52bb542
AC
1033 break;
1034 }
0fa21e85 1035 }
e78dda3b 1036 domain_free(domlist);
2e0172b1
N
1037 }
1038}
c3621c0a
ML
1039
1040/* search the statelist to connect external
1041 * metadata subarrays with their containers
1042 * We always completely rebuild the tree from scratch as
1043 * that is safest considering the possibility of entries
1044 * disappearing or changing.
1045 */
1046static void link_containers_with_subarrays(struct state *list)
1047{
1048 struct state *st;
1049 struct state *cont;
1050 for (st = list; st; st = st->next) {
1051 st->parent = NULL;
1052 st->subarray = NULL;
1053 }
1054 for (st = list; st; st = st->next)
4dd2df09 1055 if (st->parent_devnm[0])
c3621c0a 1056 for (cont = list; cont; cont = cont->next)
f566ef45 1057 if (!cont->err && cont->parent_devnm[0] == 0 &&
4dd2df09 1058 strcmp(cont->devnm, st->parent_devnm) == 0) {
c3621c0a
ML
1059 st->parent = cont;
1060 st->subarray = cont->subarray;
1061 cont->subarray = st;
1062 break;
1063 }
1064}
1065
af3396da 1066#ifndef NO_LIBUDEV
49b69533
OS
1067/* function: check_udev_activity
1068 * Description: Function waits for udev to finish
1069 * events processing.
1070 * Returns:
1071 * 1 - detected error while opening udev
1072 * 2 - timeout
1073 * 0 - successfull completion
1074 */
1075static int check_udev_activity(void)
1076{
1077 struct udev *udev = NULL;
1078 struct udev_queue *udev_queue = NULL;
1079 int timeout_cnt = 30;
1080 int rc = 0;
1081
1082 /*
1083 * In rare cases systemd may not have udevm,
1084 * in such cases just exit with rc 0
1085 */
1086 if (!use_udev())
1087 goto out;
1088
1089 udev = udev_new();
1090 if (!udev) {
1091 rc = 1;
1092 goto out;
1093 }
1094
1095 udev_queue = udev_queue_new(udev);
1096 if (!udev_queue) {
1097 rc = 1;
1098 goto out;
1099 }
1100
1101 if (udev_queue_get_queue_is_empty(udev_queue))
1102 goto out;
1103
1104 while (!udev_queue_get_queue_is_empty(udev_queue)) {
1105 sleep(1);
1106
1107 if (timeout_cnt)
1108 timeout_cnt--;
1109 else {
1110 rc = 2;
1111 goto out;
1112 }
1113 }
1114
1115out:
1116 if (udev_queue)
1117 udev_queue_unref(udev_queue);
1118 if (udev)
1119 udev_unref(udev);
1120 return rc;
1121}
af3396da 1122#endif
49b69533 1123
b90c0e9a
NB
1124/* Not really Monitor but ... */
1125int Wait(char *dev)
1126{
4dd2df09 1127 char devnm[32];
cb91230c
TM
1128 dev_t rdev;
1129 char *tmp;
b90c0e9a 1130 int rv = 1;
d3f6cf4f 1131 int frozen_remaining = 3;
b90c0e9a 1132
cb91230c 1133 if (!stat_is_blkdev(dev, &rdev))
b90c0e9a 1134 return 2;
cb91230c
TM
1135
1136 tmp = devid2devnm(rdev);
1137 if (!tmp) {
1138 pr_err("Cannot get md device name.\n");
1139 return 2;
1140 }
1141
1142 strcpy(devnm, tmp);
b90c0e9a
NB
1143
1144 while(1) {
1145 struct mdstat_ent *ms = mdstat_read(1, 0);
1146 struct mdstat_ent *e;
1147
f566ef45 1148 for (e = ms; e; e = e->next)
4dd2df09 1149 if (strcmp(e->devnm, devnm) == 0)
b90c0e9a
NB
1150 break;
1151
d3f6cf4f 1152 if (e && e->percent == RESYNC_NONE) {
276be514
N
1153 /* We could be in the brief pause before something
1154 * starts. /proc/mdstat doesn't show that, but
1155 * sync_action does.
1156 */
1157 struct mdinfo mdi;
1158 char buf[21];
dae13137
JS
1159
1160 if (sysfs_init(&mdi, -1, devnm))
1161 return 2;
276be514
N
1162 if (sysfs_get_str(&mdi, NULL, "sync_action",
1163 buf, 20) > 0 &&
d3f6cf4f 1164 strcmp(buf,"idle\n") != 0) {
276be514 1165 e->percent = RESYNC_UNKNOWN;
d3f6cf4f
N
1166 if (strcmp(buf, "frozen\n") == 0) {
1167 if (frozen_remaining == 0)
1168 e->percent = RESYNC_NONE;
1169 else
1170 frozen_remaining -= 1;
1171 }
1172 }
276be514 1173 }
639c3c10 1174 if (!e || e->percent == RESYNC_NONE) {
e7783ee6 1175 if (e && e->metadata_version &&
c94709e8
DW
1176 strncmp(e->metadata_version, "external:", 9) == 0) {
1177 if (is_subarray(&e->metadata_version[9]))
1178 ping_monitor(&e->metadata_version[9]);
1179 else
4dd2df09 1180 ping_monitor(devnm);
c94709e8 1181 }
b90c0e9a
NB
1182 free_mdstat(ms);
1183 return rv;
1184 }
89a10d84 1185 free_mdstat(ms);
b90c0e9a
NB
1186 rv = 0;
1187 mdstat_wait(5);
1188 }
1189}
679eb882 1190
43ebc910
GP
1191/* The state "broken" is used only for RAID0/LINEAR - it's the same as
1192 * "clean", but used in case the array has one or more members missing.
1193 */
679eb882 1194static char *clean_states[] = {
43ebc910 1195 "clear", "inactive", "readonly", "read-auto", "clean", "broken", NULL };
679eb882 1196
1ea04629 1197int WaitClean(char *dev, int verbose)
679eb882
N
1198{
1199 int fd;
1200 struct mdinfo *mdi;
1201 int rv = 1;
4dd2df09 1202 char devnm[32];
679eb882 1203
9e04ac1c
ZL
1204 if (!stat_is_blkdev(dev, NULL))
1205 return 2;
679eb882
N
1206 fd = open(dev, O_RDONLY);
1207 if (fd < 0) {
1208 if (verbose)
e7b84f9d 1209 pr_err("Couldn't open %s: %s\n", dev, strerror(errno));
679eb882
N
1210 return 1;
1211 }
1212
4dd2df09
N
1213 strcpy(devnm, fd2devnm(fd));
1214 mdi = sysfs_read(fd, devnm, GET_VERSION|GET_LEVEL|GET_SAFEMODE);
679eb882
N
1215 if (!mdi) {
1216 if (verbose)
7a862a02 1217 pr_err("Failed to read sysfs attributes for %s\n", dev);
679eb882
N
1218 close(fd);
1219 return 0;
1220 }
1221
1222 switch(mdi->array.level) {
1223 case LEVEL_LINEAR:
1224 case LEVEL_MULTIPATH:
1225 case 0:
1226 /* safemode delay is irrelevant for these levels */
1227 rv = 0;
1228 }
1229
1230 /* for internal metadata the kernel handles the final clean
1231 * transition, containers can never be dirty
1232 */
1233 if (!is_subarray(mdi->text_version))
1234 rv = 0;
1235
1236 /* safemode disabled ? */
1237 if (mdi->safe_mode_delay == 0)
1238 rv = 0;
1239
1240 if (rv) {
4dd2df09 1241 int state_fd = sysfs_open(fd2devnm(fd), NULL, "array_state");
679eb882 1242 char buf[20];
efc67e8e 1243 int delay = 5000;
679eb882
N
1244
1245 /* minimize the safe_mode_delay and prepare to wait up to 5s
1246 * for writes to quiesce
1247 */
1248 sysfs_set_safemode(mdi, 1);
679eb882
N
1249
1250 /* wait for array_state to be clean */
1251 while (1) {
1252 rv = read(state_fd, buf, sizeof(buf));
1253 if (rv < 0)
1254 break;
43ebc910
GP
1255 if (sysfs_match_word(buf, clean_states) <
1256 (int)ARRAY_SIZE(clean_states) - 1)
679eb882 1257 break;
efc67e8e 1258 rv = sysfs_wait(state_fd, &delay);
679eb882
N
1259 if (rv < 0 && errno != EINTR)
1260 break;
1261 lseek(state_fd, 0, SEEK_SET);
1262 }
1263 if (rv < 0)
1264 rv = 1;
1ea04629 1265 else if (ping_monitor(mdi->text_version) == 0) {
679eb882
N
1266 /* we need to ping to close the window between array
1267 * state transitioning to clean and the metadata being
1268 * marked clean
1269 */
1270 rv = 0;
1ea04629 1271 } else {
679eb882 1272 rv = 1;
1ea04629
MT
1273 pr_err("Error connecting monitor with %s\n", dev);
1274 }
679eb882 1275 if (rv && verbose)
f566ef45 1276 pr_err("Error waiting for %s to be clean\n", dev);
679eb882
N
1277
1278 /* restore the original safe_mode_delay */
1279 sysfs_set_safemode(mdi, mdi->safe_mode_delay);
1280 close(state_fd);
1281 }
1282
1283 sysfs_free(mdi);
1284 close(fd);
1285
1286 return rv;
1287}