]> git.ipfire.org Git - thirdparty/mdadm.git/blame - monitor.c
monitor: read_and_act: log status when called
[thirdparty/mdadm.git] / monitor.c
CommitLineData
a54d5262
DW
1/*
2 * mdmon - monitor external metadata arrays
3 *
e736b623
N
4 * Copyright (C) 2007-2009 Neil Brown <neilb@suse.de>
5 * Copyright (C) 2007-2009 Intel Corporation
a54d5262
DW
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms and conditions of the GNU General Public License,
9 * version 2, as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
19 */
549e9569
NB
20
21#include "mdadm.h"
22#include "mdmon.h"
4d43913c 23#include <sys/syscall.h>
549e9569 24#include <sys/select.h>
1ed3f387 25#include <signal.h>
549e9569
NB
26
27static char *array_states[] = {
28 "clear", "inactive", "suspended", "readonly", "read-auto",
29 "clean", "active", "write-pending", "active-idle", NULL };
30static char *sync_actions[] = {
31 "idle", "reshape", "resync", "recover", "check", "repair", NULL
32};
33
34static int write_attr(char *attr, int fd)
35{
36 return write(fd, attr, strlen(attr));
37}
38
39static void add_fd(fd_set *fds, int *maxfd, int fd)
40{
2b60d289 41 struct stat st;
549e9569
NB
42 if (fd < 0)
43 return;
2b60d289 44 if (fstat(fd, &st) == -1) {
45 dprintf("%s: Invalid fd %d\n", __func__, fd);
46 return;
47 }
48 if (st.st_nlink == 0) {
49 dprintf("%s: fd %d was deleted\n", __func__, fd);
50 return;
51 }
549e9569
NB
52 if (fd > *maxfd)
53 *maxfd = fd;
54 FD_SET(fd, fds);
55}
56
57static int read_attr(char *buf, int len, int fd)
58{
59 int n;
60
61 if (fd < 0) {
62 buf[0] = 0;
63 return 0;
64 }
65 lseek(fd, 0, 0);
66 n = read(fd, buf, len - 1);
67
68 if (n <= 0) {
69 buf[0] = 0;
70 return 0;
71 }
72 buf[n] = 0;
73 if (buf[n-1] == '\n')
74 buf[n-1] = 0;
75 return n;
76}
77
40ae6f5f 78static void read_resync_start(int fd, unsigned long long *v)
c052ba30
DW
79{
80 char buf[30];
81 int n;
82
b7941fd6 83 n = read_attr(buf, 30, fd);
40ae6f5f 84 if (n <= 0) {
85 dprintf("%s: Failed to read resync_start (%d)\n",
86 __func__, fd);
87 return;
88 }
7e7fffc4 89 if (strncmp(buf, "none", 4) == 0)
40ae6f5f 90 *v = MaxSector;
7e7fffc4 91 else
40ae6f5f 92 *v = strtoull(buf, NULL, 10);
c052ba30 93}
549e9569 94
484240d8
DW
95static unsigned long long read_sync_completed(int fd)
96{
97 unsigned long long val;
98 char buf[50];
99 int n;
100 char *ep;
101
102 n = read_attr(buf, 50, fd);
103
104 if (n <= 0)
105 return 0;
106 buf[n] = 0;
107 val = strtoull(buf, &ep, 0);
108 if (ep == buf || (*ep != 0 && *ep != '\n' && *ep != ' '))
109 return 0;
110 return val;
111}
112
549e9569
NB
113static enum array_state read_state(int fd)
114{
115 char buf[20];
116 int n = read_attr(buf, 20, fd);
117
118 if (n <= 0)
119 return bad_word;
1770662b 120 return (enum array_state) sysfs_match_word(buf, array_states);
549e9569
NB
121}
122
123static enum sync_action read_action( int fd)
124{
125 char buf[20];
126 int n = read_attr(buf, 20, fd);
127
128 if (n <= 0)
129 return bad_action;
1770662b 130 return (enum sync_action) sysfs_match_word(buf, sync_actions);
549e9569
NB
131}
132
549e9569
NB
133int read_dev_state(int fd)
134{
135 char buf[60];
136 int n = read_attr(buf, 60, fd);
137 char *cp;
138 int rv = 0;
139
140 if (n <= 0)
141 return 0;
142
143 cp = buf;
144 while (cp) {
1770662b 145 if (sysfs_attr_match(cp, "faulty"))
549e9569 146 rv |= DS_FAULTY;
1770662b 147 if (sysfs_attr_match(cp, "in_sync"))
549e9569 148 rv |= DS_INSYNC;
1770662b 149 if (sysfs_attr_match(cp, "write_mostly"))
549e9569 150 rv |= DS_WRITE_MOSTLY;
1770662b 151 if (sysfs_attr_match(cp, "spare"))
549e9569 152 rv |= DS_SPARE;
1770662b 153 if (sysfs_attr_match(cp, "blocked"))
8d45d196 154 rv |= DS_BLOCKED;
549e9569
NB
155 cp = strchr(cp, ',');
156 if (cp)
157 cp++;
158 }
159 return rv;
160}
161
1ed3f387
NB
162static void signal_manager(void)
163{
4d43913c
NB
164 /* tgkill(getpid(), mon_tid, SIGUSR1); */
165 int pid = getpid();
166 syscall(SYS_tgkill, pid, mgr_tid, SIGUSR1);
1ed3f387 167}
549e9569
NB
168
169/* Monitor a set of active md arrays - all of which share the
170 * same metadata - and respond to events that require
171 * metadata update.
172 *
173 * New arrays are detected by another thread which allocates
174 * required memory and attaches the data structure to our list.
175 *
176 * Events:
177 * Array stops.
178 * This is detected by array_state going to 'clear' or 'inactive'.
179 * while we thought it was active.
180 * Response is to mark metadata as clean and 'clear' the array(??)
181 * write-pending
182 * array_state if 'write-pending'
183 * We mark metadata as 'dirty' then set array to 'active'.
184 * active_idle
185 * Either ignore, or mark clean, then mark metadata as clean.
186 *
187 * device fails
188 * detected by rd-N/state reporting "faulty"
8d45d196
DW
189 * mark device as 'failed' in metadata, let the kernel release the
190 * device by writing '-blocked' to rd/state, and finally write 'remove' to
0af73f61
DW
191 * rd/state. Before a disk can be replaced it must be failed and removed
192 * from all container members, this will be preemptive for the other
193 * arrays... safe?
549e9569
NB
194 *
195 * sync completes
196 * sync_action was 'resync' and becomes 'idle' and resync_start becomes
197 * MaxSector
198 * Notify metadata that sync is complete.
549e9569
NB
199 *
200 * recovery completes
201 * sync_action changes from 'recover' to 'idle'
202 * Check each device state and mark metadata if 'faulty' or 'in_sync'.
549e9569
NB
203 *
204 * deal with resync
c052ba30
DW
205 * This only happens on finding a new array... mdadm will have set
206 * 'resync_start' to the correct value. If 'resync_start' indicates that an
207 * resync needs to occur set the array to the 'active' state rather than the
208 * initial read-auto state.
549e9569
NB
209 *
210 *
211 *
212 * We wait for a change (poll/select) on array_state, sync_action, and
213 * each rd-X/state file.
214 * When we get any change, we check everything. So read each state file,
215 * then decide what to do.
216 *
217 * The core action is to write new metadata to all devices in the array.
218 * This is done at most once on any wakeup.
219 * After that we might:
220 * - update the array_state
221 * - set the role of some devices.
222 * - request a sync_action
223 *
224 */
225
77b3ac8c 226#define ARRAY_DIRTY 1
68226a80 227#define ARRAY_BUSY 2
549e9569
NB
228static int read_and_act(struct active_array *a)
229{
484240d8 230 unsigned long long sync_completed;
6c3fb95c 231 int check_degraded = 0;
0f99b4bd 232 int check_reshape = 0;
2a0bb19e 233 int deactivate = 0;
549e9569 234 struct mdinfo *mdi;
77b3ac8c 235 int ret = 0;
3a5d0473 236 int count = 0;
39da26ec 237 struct timeval tv;
549e9569
NB
238
239 a->next_state = bad_word;
240 a->next_action = bad_action;
241
242 a->curr_state = read_state(a->info.state_fd);
243 a->curr_action = read_action(a->action_fd);
40ae6f5f 244 if (a->curr_state != clear)
245 /*
246 * In "clear" state, resync_start may wrongly be set to "0"
247 * when the kernel called md_clean but didn't remove the
248 * sysfs attributes yet
249 */
250 read_resync_start(a->resync_start_fd, &a->info.resync_start);
484240d8 251 sync_completed = read_sync_completed(a->sync_completed_fd);
549e9569
NB
252 for (mdi = a->info.devs; mdi ; mdi = mdi->next) {
253 mdi->next_state = 0;
10ce1808 254 mdi->curr_state = 0;
e1516be1 255 if (mdi->state_fd >= 0) {
40ae6f5f 256 read_resync_start(mdi->recovery_fd,
257 &mdi->recovery_start);
8d45d196 258 mdi->curr_state = read_dev_state(mdi->state_fd);
e1516be1 259 }
549e9569
NB
260 }
261
39da26ec
MW
262 gettimeofday(&tv, NULL);
263 dprintf("%s(%d): %ld.%06ld state:%s prev:%s action:%s prev: %s start:%llu\n",
264 __func__, a->info.container_member,
265 tv.tv_sec, tv.tv_usec,
266 array_states[a->curr_state],
267 array_states[a->prev_state],
268 sync_actions[a->curr_action],
269 sync_actions[a->prev_action],
270 a->info.resync_start
271 );
272
4867e068
AK
273 if (a->curr_state > inactive &&
274 a->prev_state == inactive) {
275 /* array has been started
276 * possible that container operation has to be completed
277 */
278 a->container->ss->set_array_state(a, 0);
279 }
6b374ba3 280 if ((a->curr_state == bad_word || a->curr_state <= inactive) &&
549e9569
NB
281 a->prev_state > inactive) {
282 /* array has been stopped */
ed9d66aa 283 a->container->ss->set_array_state(a, 1);
549e9569 284 a->next_state = clear;
2a0bb19e 285 deactivate = 1;
549e9569
NB
286 }
287 if (a->curr_state == write_pending) {
ed9d66aa 288 a->container->ss->set_array_state(a, 0);
549e9569 289 a->next_state = active;
77b3ac8c 290 ret |= ARRAY_DIRTY;
549e9569
NB
291 }
292 if (a->curr_state == active_idle) {
d797a062
DW
293 /* Set array to 'clean' FIRST, then mark clean
294 * in the metadata
549e9569 295 */
d797a062 296 a->next_state = clean;
77b3ac8c 297 ret |= ARRAY_DIRTY;
d797a062
DW
298 }
299 if (a->curr_state == clean) {
d797a062 300 a->container->ss->set_array_state(a, 1);
549e9569 301 }
140d3685 302 if (a->curr_state == active ||
6b374ba3 303 a->curr_state == suspended)
77b3ac8c 304 ret |= ARRAY_DIRTY;
549e9569 305 if (a->curr_state == readonly) {
e9dd1598
N
306 /* Well, I'm ready to handle things. If readonly
307 * wasn't requested, transition to read-auto.
549e9569 308 */
e9dd1598
N
309 char buf[64];
310 read_attr(buf, sizeof(buf), a->metadata_fd);
311 if (strncmp(buf, "external:-", 10) == 0) {
312 /* explicit request for readonly array. Leave it alone */
313 ;
314 } else {
e9dd1598
N
315 if (a->container->ss->set_array_state(a, 2))
316 a->next_state = read_auto; /* array is clean */
140d3685 317 else {
e9dd1598 318 a->next_state = active; /* Now active for recovery etc */
77b3ac8c 319 ret |= ARRAY_DIRTY;
140d3685 320 }
e9dd1598 321 }
549e9569
NB
322 }
323
00e02142
DW
324 if (!deactivate &&
325 a->curr_action == idle &&
549e9569 326 a->prev_action == resync) {
4e5528c6
NB
327 /* A resync has finished. The endpoint is recorded in
328 * 'sync_start'. We don't update the metadata
329 * until the array goes inactive or readonly though.
330 * Just check if we need to fiddle spares.
331 */
0c0c44db 332 a->container->ss->set_array_state(a, a->curr_state <= clean);
549e9569
NB
333 check_degraded = 1;
334 }
335
00e02142
DW
336 if (!deactivate &&
337 a->curr_action == idle &&
549e9569 338 a->prev_action == recover) {
0a6bdbee
DW
339 /* A recovery has finished. Some disks may be in sync now,
340 * and the array may no longer be degraded
341 */
549e9569 342 for (mdi = a->info.devs ; mdi ; mdi = mdi->next) {
8d45d196
DW
343 a->container->ss->set_disk(a, mdi->disk.raid_disk,
344 mdi->curr_state);
549e9569
NB
345 if (! (mdi->curr_state & DS_INSYNC))
346 check_degraded = 1;
3a5d0473 347 count++;
549e9569 348 }
3a5d0473
KW
349 if (count != a->info.array.raid_disks)
350 check_degraded = 1;
549e9569
NB
351 }
352
0f99b4bd
N
353 if (!deactivate &&
354 a->curr_action == reshape &&
355 a->prev_action != reshape)
356 /* reshape was requested by mdadm. Need to see if
357 * new devices have been added. Manager does that
358 * when it sees check_reshape
359 */
360 check_reshape = 1;
361
92967543
DW
362 /* Check for failures and if found:
363 * 1/ Record the failure in the metadata and unblock the device.
364 * FIXME update the kernel to stop notifying on failed drives when
365 * the array is readonly and we have cleared 'blocked'
366 * 2/ Try to remove the device if the array is writable, or can be
367 * made writable.
368 */
549e9569
NB
369 for (mdi = a->info.devs ; mdi ; mdi = mdi->next) {
370 if (mdi->curr_state & DS_FAULTY) {
8d45d196
DW
371 a->container->ss->set_disk(a, mdi->disk.raid_disk,
372 mdi->curr_state);
549e9569 373 check_degraded = 1;
1c278e81
N
374 if (mdi->curr_state & DS_BLOCKED)
375 mdi->next_state |= DS_UNBLOCK;
92967543
DW
376 if (a->curr_state == read_auto) {
377 a->container->ss->set_array_state(a, 0);
378 a->next_state = active;
379 }
380 if (a->curr_state > readonly)
381 mdi->next_state |= DS_REMOVE;
549e9569
NB
382 }
383 }
384
484240d8
DW
385 /* Check for recovery checkpoint notifications. We need to be a
386 * minimum distance away from the last checkpoint to prevent
aad6f216
N
387 * over checkpointing. Note reshape checkpointing is handled
388 * in the second branch.
484240d8
DW
389 */
390 if (sync_completed > a->last_checkpoint &&
391 sync_completed - a->last_checkpoint > a->info.component_size >> 4 &&
4f0a7acc
DW
392 a->curr_action > reshape) {
393 /* A (non-reshape) sync_action has reached a checkpoint.
394 * Record the updated position in the metadata
395 */
396 a->last_checkpoint = sync_completed;
397 a->container->ss->set_array_state(a, a->curr_state <= clean);
aad6f216
N
398 } else if ((a->curr_action == idle && a->prev_action == reshape) ||
399 (a->curr_action == reshape
400 && sync_completed > a->last_checkpoint) ) {
401 /* Reshape has progressed or completed so we need to
402 * update the array state - and possibly the array size
403 */
2a9f8409
AK
404 if (sync_completed != 0)
405 a->last_checkpoint = sync_completed;
6d4225a1
AK
406 /* We might need to update last_checkpoint depending on
407 * the reason that reshape finished.
408 * if array reshape is really finished:
409 * set check point to the end, this allows
410 * set_array_state() to finalize reshape in metadata
411 * if reshape if broken: do not set checkpoint to the end
412 * this allows for reshape restart from checkpoint
413 */
414 if ((a->curr_action != reshape) &&
415 (a->prev_action == reshape)) {
416 char buf[40];
417 if ((sysfs_get_str(&a->info, NULL,
418 "reshape_position",
419 buf,
420 sizeof(buf)) >= 0) &&
421 strncmp(buf, "none", 4) == 0)
422 a->last_checkpoint = a->info.component_size;
423 }
aad6f216 424 a->container->ss->set_array_state(a, a->curr_state <= clean);
2a9f8409 425 a->last_checkpoint = sync_completed;
aad6f216
N
426 }
427
428 if (sync_completed > a->last_checkpoint)
484240d8 429 a->last_checkpoint = sync_completed;
484240d8 430
24a216bf 431 if (deactivate || a->curr_state >= clean)
432 a->container->ss->sync_metadata(a->container);
4065aa81
DW
433 dprintf("%s(%d): state:%s action:%s next(", __func__, a->info.container_member,
434 array_states[a->curr_state], sync_actions[a->curr_action]);
549e9569
NB
435
436 /* Effect state changes in the array */
4e6e574a
DW
437 if (a->next_state != bad_word) {
438 dprintf(" state:%s", array_states[a->next_state]);
549e9569 439 write_attr(array_states[a->next_state], a->info.state_fd);
4e6e574a
DW
440 }
441 if (a->next_action != bad_action) {
549e9569 442 write_attr(sync_actions[a->next_action], a->action_fd);
4065aa81 443 dprintf(" action:%s", sync_actions[a->next_action]);
4e6e574a 444 }
549e9569 445 for (mdi = a->info.devs; mdi ; mdi = mdi->next) {
92967543
DW
446 if (mdi->next_state & DS_UNBLOCK) {
447 dprintf(" %d:-blocked", mdi->disk.raid_disk);
448 write_attr("-blocked", mdi->state_fd);
449 }
450
451 if ((mdi->next_state & DS_REMOVE) && mdi->state_fd >= 0) {
57632f4a 452 int remove_result;
8d45d196 453
68226a80
N
454 /* The kernel may not be able to immediately remove the
455 * disk. In that case we wait a little while and
456 * try again.
8d45d196 457 */
57632f4a
NB
458 remove_result = write_attr("remove", mdi->state_fd);
459 if (remove_result > 0) {
4e6e574a 460 dprintf(" %d:removed", mdi->disk.raid_disk);
8d45d196 461 close(mdi->state_fd);
e40512fd 462 close(mdi->recovery_fd);
8d45d196 463 mdi->state_fd = -1;
68226a80
N
464 } else
465 ret |= ARRAY_BUSY;
8d45d196 466 }
4e6e574a 467 if (mdi->next_state & DS_INSYNC) {
549e9569 468 write_attr("+in_sync", mdi->state_fd);
4e6e574a
DW
469 dprintf(" %d:+in_sync", mdi->disk.raid_disk);
470 }
549e9569 471 }
4e6e574a 472 dprintf(" )\n");
549e9569
NB
473
474 /* move curr_ to prev_ */
475 a->prev_state = a->curr_state;
476
477 a->prev_action = a->curr_action;
478
479 for (mdi = a->info.devs; mdi ; mdi = mdi->next) {
480 mdi->prev_state = mdi->curr_state;
481 mdi->next_state = 0;
482 }
483
0f99b4bd 484 if (check_degraded || check_reshape) {
7e1432fb 485 /* manager will do the actual check */
0f99b4bd
N
486 if (check_degraded)
487 a->check_degraded = 1;
488 if (check_reshape)
489 a->check_reshape = 1;
7e1432fb
NB
490 signal_manager();
491 }
492
2a0bb19e
DW
493 if (deactivate)
494 a->container = NULL;
495
77b3ac8c 496 return ret;
549e9569
NB
497}
498
0af73f61
DW
499static struct mdinfo *
500find_device(struct active_array *a, int major, int minor)
501{
502 struct mdinfo *mdi;
503
504 for (mdi = a->info.devs ; mdi ; mdi = mdi->next)
505 if (mdi->disk.major == major && mdi->disk.minor == minor)
506 return mdi;
507
508 return NULL;
509}
510
511static void reconcile_failed(struct active_array *aa, struct mdinfo *failed)
512{
513 struct active_array *a;
514 struct mdinfo *victim;
515
516 for (a = aa; a; a = a->next) {
ba714450 517 if (!a->container || a->to_remove)
0af73f61
DW
518 continue;
519 victim = find_device(a, failed->disk.major, failed->disk.minor);
520 if (!victim)
521 continue;
522
523 if (!(victim->curr_state & DS_FAULTY))
524 write_attr("faulty", victim->state_fd);
525 }
526}
527
4e6e574a
DW
528#ifdef DEBUG
529static void dprint_wake_reasons(fd_set *fds)
530{
531 int i;
532 char proc_path[256];
533 char link[256];
534 char *basename;
535 int rv;
536
537 fprintf(stderr, "monitor: wake ( ");
538 for (i = 0; i < FD_SETSIZE; i++) {
539 if (FD_ISSET(i, fds)) {
540 sprintf(proc_path, "/proc/%d/fd/%d",
541 (int) getpid(), i);
542
543 rv = readlink(proc_path, link, sizeof(link) - 1);
544 if (rv < 0) {
545 fprintf(stderr, "%d:unknown ", i);
546 continue;
547 }
548 link[rv] = '\0';
549 basename = strrchr(link, '/');
550 fprintf(stderr, "%d:%s ",
551 i, basename ? ++basename : link);
552 }
553 }
554 fprintf(stderr, ")\n");
555}
556#endif
557
1eb252b8
N
558int monitor_loop_cnt;
559
4d43913c 560static int wait_and_act(struct supertype *container, int nowait)
549e9569
NB
561{
562 fd_set rfds;
563 int maxfd = 0;
e0d6609f 564 struct active_array **aap = &container->arrays;
1ed3f387 565 struct active_array *a, **ap;
549e9569 566 int rv;
0af73f61 567 struct mdinfo *mdi;
6144ed44 568 static unsigned int dirty_arrays = ~0; /* start at some non-zero value */
549e9569
NB
569
570 FD_ZERO(&rfds);
571
1ed3f387
NB
572 for (ap = aap ; *ap ;) {
573 a = *ap;
574 /* once an array has been deactivated we want to
575 * ask the manager to discard it.
2a0bb19e 576 */
ba714450 577 if (!a->container || a->to_remove) {
1ed3f387
NB
578 if (discard_this) {
579 ap = &(*ap)->next;
580 continue;
581 }
582 *ap = a->next;
583 a->next = NULL;
584 discard_this = a;
585 signal_manager();
2a0bb19e 586 continue;
1ed3f387 587 }
2a0bb19e 588
549e9569
NB
589 add_fd(&rfds, &maxfd, a->info.state_fd);
590 add_fd(&rfds, &maxfd, a->action_fd);
484240d8 591 add_fd(&rfds, &maxfd, a->sync_completed_fd);
549e9569
NB
592 for (mdi = a->info.devs ; mdi ; mdi = mdi->next)
593 add_fd(&rfds, &maxfd, mdi->state_fd);
1ed3f387
NB
594
595 ap = &(*ap)->next;
549e9569
NB
596 }
597
6144ed44
DW
598 if (manager_ready && (*aap == NULL || (sigterm && !dirty_arrays))) {
599 /* No interesting arrays, or we have been told to
600 * terminate and everything is clean. Lets see about
601 * exiting. Note that blocking at this point is not a
602 * problem as there are no active arrays, there is
603 * nothing that we need to be ready to do.
e0d6609f 604 */
d998b738
N
605 int fd;
606 if (sigterm)
4dd2df09 607 fd = open_dev_excl(container->devnm);
d998b738 608 else
4dd2df09 609 fd = open_dev_flags(container->devnm, O_RDONLY|O_EXCL);
e0d6609f
NB
610 if (fd >= 0 || errno != EBUSY) {
611 /* OK, we are safe to leave */
6144ed44
DW
612 if (sigterm && !dirty_arrays)
613 dprintf("caught sigterm, all clean... exiting\n");
614 else
615 dprintf("no arrays to monitor... exiting\n");
fa716c83
N
616 if (!sigterm)
617 /* On SIGTERM, someone (the take-over mdmon) will
618 * clean up
619 */
4dd2df09 620 remove_pidfile(container->devnm);
e0d6609f
NB
621 exit_now = 1;
622 signal_manager();
6f4cdfd9 623 close(fd);
e0d6609f
NB
624 exit(0);
625 }
626 }
627
549e9569 628 if (!nowait) {
4d43913c 629 sigset_t set;
d998b738
N
630 struct timespec ts;
631 ts.tv_sec = 24*3600;
632 ts.tv_nsec = 0;
68226a80 633 if (*aap == NULL || container->retry_soon) {
d998b738
N
634 /* just waiting to get O_EXCL access */
635 ts.tv_sec = 0;
636 ts.tv_nsec = 20000000ULL;
637 }
4d43913c
NB
638 sigprocmask(SIG_UNBLOCK, NULL, &set);
639 sigdelset(&set, SIGUSR1);
1eb252b8 640 monitor_loop_cnt |= 1;
d998b738 641 rv = pselect(maxfd+1, NULL, NULL, &rfds, &ts, &set);
1eb252b8 642 monitor_loop_cnt += 1;
bfa44e2e
NB
643 if (rv == -1 && errno == EINTR)
644 rv = 0;
4e6e574a
DW
645 #ifdef DEBUG
646 dprint_wake_reasons(&rfds);
647 #endif
68226a80 648 container->retry_soon = 0;
549e9569
NB
649 }
650
2e735d19
NB
651 if (update_queue) {
652 struct metadata_update *this;
653
654 for (this = update_queue; this ; this = this->next)
655 container->ss->process_update(container, this);
656
657 update_queue_handled = update_queue;
658 update_queue = NULL;
659 signal_manager();
660 container->ss->sync_metadata(container);
661 }
662
3d2c4fc7 663 rv = 0;
6144ed44 664 dirty_arrays = 0;
1ed3f387 665 for (a = *aap; a ; a = a->next) {
6144ed44 666
2a0bb19e 667 if (a->replaces && !discard_this) {
549e9569
NB
668 struct active_array **ap;
669 for (ap = &a->next; *ap && *ap != a->replaces;
670 ap = & (*ap)->next)
671 ;
672 if (*ap)
673 *ap = (*ap)->next;
674 discard_this = a->replaces;
675 a->replaces = NULL;
6c3fb95c 676 /* FIXME check if device->state_fd need to be cleared?*/
1ed3f387 677 signal_manager();
549e9569 678 }
ba714450 679 if (a->container && !a->to_remove) {
77b3ac8c 680 int ret = read_and_act(a);
140d3685 681 rv |= 1;
77b3ac8c 682 dirty_arrays += !!(ret & ARRAY_DIRTY);
140d3685
DW
683 /* when terminating stop manipulating the array after it
684 * is clean, but make sure read_and_act() is given a
685 * chance to handle 'active_idle'
686 */
77b3ac8c 687 if (sigterm && !(ret & ARRAY_DIRTY))
140d3685 688 a->container = NULL; /* stop touching this array */
68226a80
N
689 if (ret & ARRAY_BUSY)
690 container->retry_soon = 1;
6144ed44 691 }
549e9569 692 }
0af73f61
DW
693
694 /* propagate failures across container members */
1ed3f387 695 for (a = *aap; a ; a = a->next) {
ba714450 696 if (!a->container || a->to_remove)
0af73f61
DW
697 continue;
698 for (mdi = a->info.devs ; mdi ; mdi = mdi->next)
699 if (mdi->curr_state & DS_FAULTY)
1ed3f387 700 reconcile_failed(*aap, mdi);
0af73f61
DW
701 }
702
549e9569
NB
703 return rv;
704}
705
706void do_monitor(struct supertype *container)
707{
708 int rv;
709 int first = 1;
710 do {
4d43913c 711 rv = wait_and_act(container, first);
549e9569
NB
712 first = 0;
713 } while (rv >= 0);
714}