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