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