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