]> git.ipfire.org Git - thirdparty/mdadm.git/blob - monitor.c
tests: add test that DDF marks missing devices as failed on assembly.
[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 static int write_attr(char *attr, int fd)
35 {
36 return write(fd, attr, strlen(attr));
37 }
38
39 static void add_fd(fd_set *fds, int *maxfd, int fd)
40 {
41 struct stat st;
42 if (fd < 0)
43 return;
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 }
52 if (fd > *maxfd)
53 *maxfd = fd;
54 FD_SET(fd, fds);
55 }
56
57 static 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
78 static void read_resync_start(int fd, unsigned long long *v)
79 {
80 char buf[30];
81 int n;
82
83 n = read_attr(buf, 30, fd);
84 if (n <= 0) {
85 dprintf("%s: Failed to read resync_start (%d)\n",
86 __func__, fd);
87 return;
88 }
89 if (strncmp(buf, "none", 4) == 0)
90 *v = MaxSector;
91 else
92 *v = strtoull(buf, NULL, 10);
93 }
94
95 static 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
113 static 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;
120 return (enum array_state) sysfs_match_word(buf, array_states);
121 }
122
123 static 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;
130 return (enum sync_action) sysfs_match_word(buf, sync_actions);
131 }
132
133 int 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) {
145 if (sysfs_attr_match(cp, "faulty"))
146 rv |= DS_FAULTY;
147 if (sysfs_attr_match(cp, "in_sync"))
148 rv |= DS_INSYNC;
149 if (sysfs_attr_match(cp, "write_mostly"))
150 rv |= DS_WRITE_MOSTLY;
151 if (sysfs_attr_match(cp, "spare"))
152 rv |= DS_SPARE;
153 if (sysfs_attr_match(cp, "blocked"))
154 rv |= DS_BLOCKED;
155 cp = strchr(cp, ',');
156 if (cp)
157 cp++;
158 }
159 return rv;
160 }
161
162 static void signal_manager(void)
163 {
164 /* tgkill(getpid(), mon_tid, SIGUSR1); */
165 int pid = getpid();
166 syscall(SYS_tgkill, pid, mgr_tid, SIGUSR1);
167 }
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"
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
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?
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.
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'.
203 *
204 * deal with resync
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.
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
226 #define ARRAY_DIRTY 1
227 #define ARRAY_BUSY 2
228 static int read_and_act(struct active_array *a)
229 {
230 unsigned long long sync_completed;
231 int check_degraded = 0;
232 int check_reshape = 0;
233 int deactivate = 0;
234 struct mdinfo *mdi;
235 int ret = 0;
236 int count = 0;
237 struct timeval tv;
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);
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);
251 sync_completed = read_sync_completed(a->sync_completed_fd);
252 for (mdi = a->info.devs; mdi ; mdi = mdi->next) {
253 mdi->next_state = 0;
254 mdi->curr_state = 0;
255 if (mdi->state_fd >= 0) {
256 read_resync_start(mdi->recovery_fd,
257 &mdi->recovery_start);
258 mdi->curr_state = read_dev_state(mdi->state_fd);
259 }
260 }
261
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
273 if ((a->curr_state == bad_word || a->curr_state <= inactive) &&
274 a->prev_state > inactive) {
275 /* array has been stopped */
276 a->container->ss->set_array_state(a, 1);
277 a->next_state = clear;
278 deactivate = 1;
279 }
280 if (a->curr_state == write_pending) {
281 a->container->ss->set_array_state(a, 0);
282 a->next_state = active;
283 ret |= ARRAY_DIRTY;
284 }
285 if (a->curr_state == active_idle) {
286 /* Set array to 'clean' FIRST, then mark clean
287 * in the metadata
288 */
289 a->next_state = clean;
290 ret |= ARRAY_DIRTY;
291 }
292 if (a->curr_state == clean) {
293 a->container->ss->set_array_state(a, 1);
294 }
295 if (a->curr_state == active ||
296 a->curr_state == suspended)
297 ret |= ARRAY_DIRTY;
298 if (a->curr_state == readonly) {
299 /* Well, I'm ready to handle things. If readonly
300 * wasn't requested, transition to read-auto.
301 */
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 {
308 if (a->container->ss->set_array_state(a, 2))
309 a->next_state = read_auto; /* array is clean */
310 else {
311 a->next_state = active; /* Now active for recovery etc */
312 ret |= ARRAY_DIRTY;
313 }
314 }
315 }
316
317 if (!deactivate &&
318 a->curr_action == idle &&
319 a->prev_action == resync) {
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 */
325 a->container->ss->set_array_state(a, a->curr_state <= clean);
326 check_degraded = 1;
327 }
328
329 if (!deactivate &&
330 a->curr_action == idle &&
331 a->prev_action == recover) {
332 /* A recovery has finished. Some disks may be in sync now,
333 * and the array may no longer be degraded
334 */
335 for (mdi = a->info.devs ; mdi ; mdi = mdi->next) {
336 a->container->ss->set_disk(a, mdi->disk.raid_disk,
337 mdi->curr_state);
338 if (! (mdi->curr_state & DS_INSYNC))
339 check_degraded = 1;
340 count++;
341 }
342 if (count != a->info.array.raid_disks)
343 check_degraded = 1;
344 }
345
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
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 */
362 for (mdi = a->info.devs ; mdi ; mdi = mdi->next) {
363 if (mdi->curr_state & DS_FAULTY) {
364 a->container->ss->set_disk(a, mdi->disk.raid_disk,
365 mdi->curr_state);
366 check_degraded = 1;
367 if (mdi->curr_state & DS_BLOCKED)
368 mdi->next_state |= DS_UNBLOCK;
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;
375 }
376 }
377
378 /* Check for recovery checkpoint notifications. We need to be a
379 * minimum distance away from the last checkpoint to prevent
380 * over checkpointing. Note reshape checkpointing is handled
381 * in the second branch.
382 */
383 if (sync_completed > a->last_checkpoint &&
384 sync_completed - a->last_checkpoint > a->info.component_size >> 4 &&
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);
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 */
397 if (sync_completed != 0)
398 a->last_checkpoint = sync_completed;
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 }
417 a->container->ss->set_array_state(a, a->curr_state <= clean);
418 a->last_checkpoint = sync_completed;
419 }
420
421 if (sync_completed > a->last_checkpoint)
422 a->last_checkpoint = sync_completed;
423
424 a->container->ss->sync_metadata(a->container);
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]);
427
428 /* Effect state changes in the array */
429 if (a->next_state != bad_word) {
430 dprintf(" state:%s", array_states[a->next_state]);
431 write_attr(array_states[a->next_state], a->info.state_fd);
432 }
433 if (a->next_action != bad_action) {
434 write_attr(sync_actions[a->next_action], a->action_fd);
435 dprintf(" action:%s", sync_actions[a->next_action]);
436 }
437 for (mdi = a->info.devs; mdi ; mdi = mdi->next) {
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) {
444 int remove_result;
445
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.
449 */
450 remove_result = write_attr("remove", mdi->state_fd);
451 if (remove_result > 0) {
452 dprintf(" %d:removed", mdi->disk.raid_disk);
453 close(mdi->state_fd);
454 close(mdi->recovery_fd);
455 mdi->state_fd = -1;
456 } else
457 ret |= ARRAY_BUSY;
458 }
459 if (mdi->next_state & DS_INSYNC) {
460 write_attr("+in_sync", mdi->state_fd);
461 dprintf(" %d:+in_sync", mdi->disk.raid_disk);
462 }
463 }
464 dprintf(" )\n");
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
476 if (check_degraded || check_reshape) {
477 /* manager will do the actual check */
478 if (check_degraded)
479 a->check_degraded = 1;
480 if (check_reshape)
481 a->check_reshape = 1;
482 signal_manager();
483 }
484
485 if (deactivate)
486 a->container = NULL;
487
488 return ret;
489 }
490
491 static struct mdinfo *
492 find_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
503 static 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) {
509 if (!a->container || a->to_remove)
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
520 #ifdef DEBUG
521 static 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
550 int monitor_loop_cnt;
551
552 static int wait_and_act(struct supertype *container, int nowait)
553 {
554 fd_set rfds;
555 int maxfd = 0;
556 struct active_array **aap = &container->arrays;
557 struct active_array *a, **ap;
558 int rv;
559 struct mdinfo *mdi;
560 static unsigned int dirty_arrays = ~0; /* start at some non-zero value */
561
562 FD_ZERO(&rfds);
563
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.
568 */
569 if (!a->container || a->to_remove) {
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();
578 continue;
579 }
580
581 add_fd(&rfds, &maxfd, a->info.state_fd);
582 add_fd(&rfds, &maxfd, a->action_fd);
583 add_fd(&rfds, &maxfd, a->sync_completed_fd);
584 for (mdi = a->info.devs ; mdi ; mdi = mdi->next)
585 add_fd(&rfds, &maxfd, mdi->state_fd);
586
587 ap = &(*ap)->next;
588 }
589
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.
596 */
597 int fd;
598 if (sigterm)
599 fd = open_dev_excl(container->devnm);
600 else
601 fd = open_dev_flags(container->devnm, O_RDONLY|O_EXCL);
602 if (fd >= 0 || errno != EBUSY) {
603 /* OK, we are safe to leave */
604 if (sigterm && !dirty_arrays)
605 dprintf("caught sigterm, all clean... exiting\n");
606 else
607 dprintf("no arrays to monitor... exiting\n");
608 if (!sigterm)
609 /* On SIGTERM, someone (the take-over mdmon) will
610 * clean up
611 */
612 remove_pidfile(container->devnm);
613 exit_now = 1;
614 signal_manager();
615 close(fd);
616 exit(0);
617 }
618 }
619
620 if (!nowait) {
621 sigset_t set;
622 struct timespec ts;
623 ts.tv_sec = 24*3600;
624 ts.tv_nsec = 0;
625 if (*aap == NULL || container->retry_soon) {
626 /* just waiting to get O_EXCL access */
627 ts.tv_sec = 0;
628 ts.tv_nsec = 20000000ULL;
629 }
630 sigprocmask(SIG_UNBLOCK, NULL, &set);
631 sigdelset(&set, SIGUSR1);
632 monitor_loop_cnt |= 1;
633 rv = pselect(maxfd+1, NULL, NULL, &rfds, &ts, &set);
634 monitor_loop_cnt += 1;
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 }
643 #ifdef DEBUG
644 else
645 dprint_wake_reasons(&rfds);
646 #endif
647 container->retry_soon = 0;
648 }
649
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
662 rv = 0;
663 dirty_arrays = 0;
664 for (a = *aap; a ; a = a->next) {
665
666 if (a->replaces && !discard_this) {
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;
675 /* FIXME check if device->state_fd need to be cleared?*/
676 signal_manager();
677 }
678 if (a->container && !a->to_remove) {
679 int ret = read_and_act(a);
680 rv |= 1;
681 dirty_arrays += !!(ret & ARRAY_DIRTY);
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 */
686 if (sigterm && !(ret & ARRAY_DIRTY))
687 a->container = NULL; /* stop touching this array */
688 if (ret & ARRAY_BUSY)
689 container->retry_soon = 1;
690 }
691 }
692
693 /* propagate failures across container members */
694 for (a = *aap; a ; a = a->next) {
695 if (!a->container || a->to_remove)
696 continue;
697 for (mdi = a->info.devs ; mdi ; mdi = mdi->next)
698 if (mdi->curr_state & DS_FAULTY)
699 reconcile_failed(*aap, mdi);
700 }
701
702 return rv;
703 }
704
705 void do_monitor(struct supertype *container)
706 {
707 int rv;
708 int first = 1;
709 do {
710 rv = wait_and_act(container, first);
711 first = 0;
712 } while (rv >= 0);
713 }