]> git.ipfire.org Git - thirdparty/mdadm.git/blame - monitor.c
DDF: Don't fail compare_super_ddf due to re-configure changes.
[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
6b374ba3 273 if ((a->curr_state == bad_word || a->curr_state <= inactive) &&
549e9569
NB
274 a->prev_state > inactive) {
275 /* array has been stopped */
ed9d66aa 276 a->container->ss->set_array_state(a, 1);
549e9569 277 a->next_state = clear;
2a0bb19e 278 deactivate = 1;
549e9569
NB
279 }
280 if (a->curr_state == write_pending) {
ed9d66aa 281 a->container->ss->set_array_state(a, 0);
549e9569 282 a->next_state = active;
77b3ac8c 283 ret |= ARRAY_DIRTY;
549e9569
NB
284 }
285 if (a->curr_state == active_idle) {
d797a062
DW
286 /* Set array to 'clean' FIRST, then mark clean
287 * in the metadata
549e9569 288 */
d797a062 289 a->next_state = clean;
77b3ac8c 290 ret |= ARRAY_DIRTY;
d797a062
DW
291 }
292 if (a->curr_state == clean) {
d797a062 293 a->container->ss->set_array_state(a, 1);
549e9569 294 }
140d3685 295 if (a->curr_state == active ||
6b374ba3 296 a->curr_state == suspended)
77b3ac8c 297 ret |= ARRAY_DIRTY;
549e9569 298 if (a->curr_state == readonly) {
e9dd1598
N
299 /* Well, I'm ready to handle things. If readonly
300 * wasn't requested, transition to read-auto.
549e9569 301 */
e9dd1598
N
302 char buf[64];
303 read_attr(buf, sizeof(buf), a->metadata_fd);
304 if (strncmp(buf, "external:-", 10) == 0) {
305 /* explicit request for readonly array. Leave it alone */
306 ;
307 } else {
e9dd1598
N
308 if (a->container->ss->set_array_state(a, 2))
309 a->next_state = read_auto; /* array is clean */
140d3685 310 else {
e9dd1598 311 a->next_state = active; /* Now active for recovery etc */
77b3ac8c 312 ret |= ARRAY_DIRTY;
140d3685 313 }
e9dd1598 314 }
549e9569
NB
315 }
316
00e02142
DW
317 if (!deactivate &&
318 a->curr_action == idle &&
549e9569 319 a->prev_action == resync) {
4e5528c6
NB
320 /* A resync has finished. The endpoint is recorded in
321 * 'sync_start'. We don't update the metadata
322 * until the array goes inactive or readonly though.
323 * Just check if we need to fiddle spares.
324 */
0c0c44db 325 a->container->ss->set_array_state(a, a->curr_state <= clean);
549e9569
NB
326 check_degraded = 1;
327 }
328
00e02142
DW
329 if (!deactivate &&
330 a->curr_action == idle &&
549e9569 331 a->prev_action == recover) {
0a6bdbee
DW
332 /* A recovery has finished. Some disks may be in sync now,
333 * and the array may no longer be degraded
334 */
549e9569 335 for (mdi = a->info.devs ; mdi ; mdi = mdi->next) {
8d45d196
DW
336 a->container->ss->set_disk(a, mdi->disk.raid_disk,
337 mdi->curr_state);
549e9569
NB
338 if (! (mdi->curr_state & DS_INSYNC))
339 check_degraded = 1;
3a5d0473 340 count++;
549e9569 341 }
3a5d0473
KW
342 if (count != a->info.array.raid_disks)
343 check_degraded = 1;
549e9569
NB
344 }
345
0f99b4bd
N
346 if (!deactivate &&
347 a->curr_action == reshape &&
348 a->prev_action != reshape)
349 /* reshape was requested by mdadm. Need to see if
350 * new devices have been added. Manager does that
351 * when it sees check_reshape
352 */
353 check_reshape = 1;
354
92967543
DW
355 /* Check for failures and if found:
356 * 1/ Record the failure in the metadata and unblock the device.
357 * FIXME update the kernel to stop notifying on failed drives when
358 * the array is readonly and we have cleared 'blocked'
359 * 2/ Try to remove the device if the array is writable, or can be
360 * made writable.
361 */
549e9569
NB
362 for (mdi = a->info.devs ; mdi ; mdi = mdi->next) {
363 if (mdi->curr_state & DS_FAULTY) {
8d45d196
DW
364 a->container->ss->set_disk(a, mdi->disk.raid_disk,
365 mdi->curr_state);
549e9569 366 check_degraded = 1;
1c278e81
N
367 if (mdi->curr_state & DS_BLOCKED)
368 mdi->next_state |= DS_UNBLOCK;
92967543
DW
369 if (a->curr_state == read_auto) {
370 a->container->ss->set_array_state(a, 0);
371 a->next_state = active;
372 }
373 if (a->curr_state > readonly)
374 mdi->next_state |= DS_REMOVE;
549e9569
NB
375 }
376 }
377
484240d8
DW
378 /* Check for recovery checkpoint notifications. We need to be a
379 * minimum distance away from the last checkpoint to prevent
aad6f216
N
380 * over checkpointing. Note reshape checkpointing is handled
381 * in the second branch.
484240d8
DW
382 */
383 if (sync_completed > a->last_checkpoint &&
384 sync_completed - a->last_checkpoint > a->info.component_size >> 4 &&
4f0a7acc
DW
385 a->curr_action > reshape) {
386 /* A (non-reshape) sync_action has reached a checkpoint.
387 * Record the updated position in the metadata
388 */
389 a->last_checkpoint = sync_completed;
390 a->container->ss->set_array_state(a, a->curr_state <= clean);
aad6f216
N
391 } else if ((a->curr_action == idle && a->prev_action == reshape) ||
392 (a->curr_action == reshape
393 && sync_completed > a->last_checkpoint) ) {
394 /* Reshape has progressed or completed so we need to
395 * update the array state - and possibly the array size
396 */
2a9f8409
AK
397 if (sync_completed != 0)
398 a->last_checkpoint = sync_completed;
6d4225a1
AK
399 /* We might need to update last_checkpoint depending on
400 * the reason that reshape finished.
401 * if array reshape is really finished:
402 * set check point to the end, this allows
403 * set_array_state() to finalize reshape in metadata
404 * if reshape if broken: do not set checkpoint to the end
405 * this allows for reshape restart from checkpoint
406 */
407 if ((a->curr_action != reshape) &&
408 (a->prev_action == reshape)) {
409 char buf[40];
410 if ((sysfs_get_str(&a->info, NULL,
411 "reshape_position",
412 buf,
413 sizeof(buf)) >= 0) &&
414 strncmp(buf, "none", 4) == 0)
415 a->last_checkpoint = a->info.component_size;
416 }
aad6f216 417 a->container->ss->set_array_state(a, a->curr_state <= clean);
2a9f8409 418 a->last_checkpoint = sync_completed;
aad6f216
N
419 }
420
421 if (sync_completed > a->last_checkpoint)
484240d8 422 a->last_checkpoint = sync_completed;
484240d8 423
a4921f30 424 a->container->ss->sync_metadata(a->container);
4065aa81
DW
425 dprintf("%s(%d): state:%s action:%s next(", __func__, a->info.container_member,
426 array_states[a->curr_state], sync_actions[a->curr_action]);
549e9569
NB
427
428 /* Effect state changes in the array */
4e6e574a
DW
429 if (a->next_state != bad_word) {
430 dprintf(" state:%s", array_states[a->next_state]);
549e9569 431 write_attr(array_states[a->next_state], a->info.state_fd);
4e6e574a
DW
432 }
433 if (a->next_action != bad_action) {
549e9569 434 write_attr(sync_actions[a->next_action], a->action_fd);
4065aa81 435 dprintf(" action:%s", sync_actions[a->next_action]);
4e6e574a 436 }
549e9569 437 for (mdi = a->info.devs; mdi ; mdi = mdi->next) {
92967543
DW
438 if (mdi->next_state & DS_UNBLOCK) {
439 dprintf(" %d:-blocked", mdi->disk.raid_disk);
440 write_attr("-blocked", mdi->state_fd);
441 }
442
443 if ((mdi->next_state & DS_REMOVE) && mdi->state_fd >= 0) {
57632f4a 444 int remove_result;
8d45d196 445
68226a80
N
446 /* The kernel may not be able to immediately remove the
447 * disk. In that case we wait a little while and
448 * try again.
8d45d196 449 */
57632f4a
NB
450 remove_result = write_attr("remove", mdi->state_fd);
451 if (remove_result > 0) {
4e6e574a 452 dprintf(" %d:removed", mdi->disk.raid_disk);
8d45d196 453 close(mdi->state_fd);
e40512fd 454 close(mdi->recovery_fd);
8d45d196 455 mdi->state_fd = -1;
68226a80
N
456 } else
457 ret |= ARRAY_BUSY;
8d45d196 458 }
4e6e574a 459 if (mdi->next_state & DS_INSYNC) {
549e9569 460 write_attr("+in_sync", mdi->state_fd);
4e6e574a
DW
461 dprintf(" %d:+in_sync", mdi->disk.raid_disk);
462 }
549e9569 463 }
4e6e574a 464 dprintf(" )\n");
549e9569
NB
465
466 /* move curr_ to prev_ */
467 a->prev_state = a->curr_state;
468
469 a->prev_action = a->curr_action;
470
471 for (mdi = a->info.devs; mdi ; mdi = mdi->next) {
472 mdi->prev_state = mdi->curr_state;
473 mdi->next_state = 0;
474 }
475
0f99b4bd 476 if (check_degraded || check_reshape) {
7e1432fb 477 /* manager will do the actual check */
0f99b4bd
N
478 if (check_degraded)
479 a->check_degraded = 1;
480 if (check_reshape)
481 a->check_reshape = 1;
7e1432fb
NB
482 signal_manager();
483 }
484
2a0bb19e
DW
485 if (deactivate)
486 a->container = NULL;
487
77b3ac8c 488 return ret;
549e9569
NB
489}
490
0af73f61
DW
491static struct mdinfo *
492find_device(struct active_array *a, int major, int minor)
493{
494 struct mdinfo *mdi;
495
496 for (mdi = a->info.devs ; mdi ; mdi = mdi->next)
497 if (mdi->disk.major == major && mdi->disk.minor == minor)
498 return mdi;
499
500 return NULL;
501}
502
503static void reconcile_failed(struct active_array *aa, struct mdinfo *failed)
504{
505 struct active_array *a;
506 struct mdinfo *victim;
507
508 for (a = aa; a; a = a->next) {
ba714450 509 if (!a->container || a->to_remove)
0af73f61
DW
510 continue;
511 victim = find_device(a, failed->disk.major, failed->disk.minor);
512 if (!victim)
513 continue;
514
515 if (!(victim->curr_state & DS_FAULTY))
516 write_attr("faulty", victim->state_fd);
517 }
518}
519
4e6e574a
DW
520#ifdef DEBUG
521static void dprint_wake_reasons(fd_set *fds)
522{
523 int i;
524 char proc_path[256];
525 char link[256];
526 char *basename;
527 int rv;
528
529 fprintf(stderr, "monitor: wake ( ");
530 for (i = 0; i < FD_SETSIZE; i++) {
531 if (FD_ISSET(i, fds)) {
532 sprintf(proc_path, "/proc/%d/fd/%d",
533 (int) getpid(), i);
534
535 rv = readlink(proc_path, link, sizeof(link) - 1);
536 if (rv < 0) {
537 fprintf(stderr, "%d:unknown ", i);
538 continue;
539 }
540 link[rv] = '\0';
541 basename = strrchr(link, '/');
542 fprintf(stderr, "%d:%s ",
543 i, basename ? ++basename : link);
544 }
545 }
546 fprintf(stderr, ")\n");
547}
548#endif
549
1eb252b8
N
550int monitor_loop_cnt;
551
4d43913c 552static int wait_and_act(struct supertype *container, int nowait)
549e9569
NB
553{
554 fd_set rfds;
555 int maxfd = 0;
e0d6609f 556 struct active_array **aap = &container->arrays;
1ed3f387 557 struct active_array *a, **ap;
549e9569 558 int rv;
0af73f61 559 struct mdinfo *mdi;
6144ed44 560 static unsigned int dirty_arrays = ~0; /* start at some non-zero value */
549e9569
NB
561
562 FD_ZERO(&rfds);
563
1ed3f387
NB
564 for (ap = aap ; *ap ;) {
565 a = *ap;
566 /* once an array has been deactivated we want to
567 * ask the manager to discard it.
2a0bb19e 568 */
ba714450 569 if (!a->container || a->to_remove) {
1ed3f387
NB
570 if (discard_this) {
571 ap = &(*ap)->next;
572 continue;
573 }
574 *ap = a->next;
575 a->next = NULL;
576 discard_this = a;
577 signal_manager();
2a0bb19e 578 continue;
1ed3f387 579 }
2a0bb19e 580
549e9569
NB
581 add_fd(&rfds, &maxfd, a->info.state_fd);
582 add_fd(&rfds, &maxfd, a->action_fd);
484240d8 583 add_fd(&rfds, &maxfd, a->sync_completed_fd);
549e9569
NB
584 for (mdi = a->info.devs ; mdi ; mdi = mdi->next)
585 add_fd(&rfds, &maxfd, mdi->state_fd);
1ed3f387
NB
586
587 ap = &(*ap)->next;
549e9569
NB
588 }
589
6144ed44
DW
590 if (manager_ready && (*aap == NULL || (sigterm && !dirty_arrays))) {
591 /* No interesting arrays, or we have been told to
592 * terminate and everything is clean. Lets see about
593 * exiting. Note that blocking at this point is not a
594 * problem as there are no active arrays, there is
595 * nothing that we need to be ready to do.
e0d6609f 596 */
d998b738
N
597 int fd;
598 if (sigterm)
4dd2df09 599 fd = open_dev_excl(container->devnm);
d998b738 600 else
4dd2df09 601 fd = open_dev_flags(container->devnm, O_RDONLY|O_EXCL);
e0d6609f
NB
602 if (fd >= 0 || errno != EBUSY) {
603 /* OK, we are safe to leave */
6144ed44
DW
604 if (sigterm && !dirty_arrays)
605 dprintf("caught sigterm, all clean... exiting\n");
606 else
607 dprintf("no arrays to monitor... exiting\n");
fa716c83
N
608 if (!sigterm)
609 /* On SIGTERM, someone (the take-over mdmon) will
610 * clean up
611 */
4dd2df09 612 remove_pidfile(container->devnm);
e0d6609f
NB
613 exit_now = 1;
614 signal_manager();
6f4cdfd9 615 close(fd);
e0d6609f
NB
616 exit(0);
617 }
618 }
619
549e9569 620 if (!nowait) {
4d43913c 621 sigset_t set;
d998b738
N
622 struct timespec ts;
623 ts.tv_sec = 24*3600;
624 ts.tv_nsec = 0;
68226a80 625 if (*aap == NULL || container->retry_soon) {
d998b738
N
626 /* just waiting to get O_EXCL access */
627 ts.tv_sec = 0;
628 ts.tv_nsec = 20000000ULL;
629 }
4d43913c
NB
630 sigprocmask(SIG_UNBLOCK, NULL, &set);
631 sigdelset(&set, SIGUSR1);
1eb252b8 632 monitor_loop_cnt |= 1;
d998b738 633 rv = pselect(maxfd+1, NULL, NULL, &rfds, &ts, &set);
1eb252b8 634 monitor_loop_cnt += 1;
c3719360
MW
635 if (rv == -1) {
636 if (errno == EINTR) {
637 rv = 0;
638 dprintf("monitor: caught signal\n");
639 } else
640 dprintf("monitor: error %d in pselect\n",
641 errno);
642 }
4e6e574a 643 #ifdef DEBUG
c3719360
MW
644 else
645 dprint_wake_reasons(&rfds);
4e6e574a 646 #endif
68226a80 647 container->retry_soon = 0;
549e9569
NB
648 }
649
2e735d19
NB
650 if (update_queue) {
651 struct metadata_update *this;
652
653 for (this = update_queue; this ; this = this->next)
654 container->ss->process_update(container, this);
655
656 update_queue_handled = update_queue;
657 update_queue = NULL;
658 signal_manager();
659 container->ss->sync_metadata(container);
660 }
661
3d2c4fc7 662 rv = 0;
6144ed44 663 dirty_arrays = 0;
1ed3f387 664 for (a = *aap; a ; a = a->next) {
6144ed44 665
2a0bb19e 666 if (a->replaces && !discard_this) {
549e9569
NB
667 struct active_array **ap;
668 for (ap = &a->next; *ap && *ap != a->replaces;
669 ap = & (*ap)->next)
670 ;
671 if (*ap)
672 *ap = (*ap)->next;
673 discard_this = a->replaces;
674 a->replaces = NULL;
6c3fb95c 675 /* FIXME check if device->state_fd need to be cleared?*/
1ed3f387 676 signal_manager();
549e9569 677 }
ba714450 678 if (a->container && !a->to_remove) {
77b3ac8c 679 int ret = read_and_act(a);
140d3685 680 rv |= 1;
77b3ac8c 681 dirty_arrays += !!(ret & ARRAY_DIRTY);
140d3685
DW
682 /* when terminating stop manipulating the array after it
683 * is clean, but make sure read_and_act() is given a
684 * chance to handle 'active_idle'
685 */
77b3ac8c 686 if (sigterm && !(ret & ARRAY_DIRTY))
140d3685 687 a->container = NULL; /* stop touching this array */
68226a80
N
688 if (ret & ARRAY_BUSY)
689 container->retry_soon = 1;
6144ed44 690 }
549e9569 691 }
0af73f61
DW
692
693 /* propagate failures across container members */
1ed3f387 694 for (a = *aap; a ; a = a->next) {
ba714450 695 if (!a->container || a->to_remove)
0af73f61
DW
696 continue;
697 for (mdi = a->info.devs ; mdi ; mdi = mdi->next)
698 if (mdi->curr_state & DS_FAULTY)
1ed3f387 699 reconcile_failed(*aap, mdi);
0af73f61
DW
700 }
701
549e9569
NB
702 return rv;
703}
704
705void do_monitor(struct supertype *container)
706{
707 int rv;
708 int first = 1;
709 do {
4d43913c 710 rv = wait_and_act(container, first);
549e9569
NB
711 first = 0;
712 } while (rv >= 0);
713}