]>
git.ipfire.org Git - thirdparty/mdadm.git/blob - monitor.c
2 * mdmon - monitor external metadata arrays
4 * Copyright (C) 2007-2009 Neil Brown <neilb@suse.de>
5 * Copyright (C) 2007-2009 Intel Corporation
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.
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
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.
23 #include <sys/syscall.h>
24 #include <sys/select.h>
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
38 static int write_attr(char *attr
, int fd
)
40 return write(fd
, attr
, strlen(attr
));
43 static void add_fd(fd_set
*fds
, int *maxfd
, int fd
)
48 if (fstat(fd
, &st
) == -1) {
49 dprintf("Invalid fd %d\n", fd
);
52 if (st
.st_nlink
== 0) {
53 dprintf("fd %d was deleted\n", fd
);
61 static int read_attr(char *buf
, int len
, int fd
)
70 n
= read(fd
, buf
, len
- 1);
82 static void read_resync_start(int fd
, unsigned long long *v
)
87 n
= read_attr(buf
, 30, fd
);
89 dprintf("Failed to read resync_start (%d)\n", fd
);
92 if (strncmp(buf
, "none", 4) == 0)
95 *v
= strtoull(buf
, NULL
, 10);
98 static unsigned long long read_sync_completed(int fd
)
100 unsigned long long val
;
105 n
= read_attr(buf
, 50, fd
);
110 val
= strtoull(buf
, &ep
, 0);
111 if (ep
== buf
|| (*ep
!= 0 && *ep
!= '\n' && *ep
!= ' '))
116 static enum array_state
read_state(int fd
)
119 int n
= read_attr(buf
, 20, fd
);
123 return (enum array_state
) sysfs_match_word(buf
, array_states
);
126 static enum sync_action
read_action( int fd
)
129 int n
= read_attr(buf
, 20, fd
);
133 return (enum sync_action
) sysfs_match_word(buf
, sync_actions
);
136 int read_dev_state(int fd
)
139 int n
= read_attr(buf
, sizeof(buf
), fd
);
148 if (sysfs_attr_match(cp
, "faulty"))
150 if (sysfs_attr_match(cp
, "in_sync"))
152 if (sysfs_attr_match(cp
, "write_mostly"))
153 rv
|= DS_WRITE_MOSTLY
;
154 if (sysfs_attr_match(cp
, "spare"))
156 if (sysfs_attr_match(cp
, "blocked"))
158 cp
= strchr(cp
, ',');
165 int process_ubb(struct active_array
*a
, struct mdinfo
*mdi
, const unsigned long
166 long sector
, const int length
, const char *buf
,
169 struct superswitch
*ss
= a
->container
->ss
;
172 * record bad block in metadata first, then acknowledge it to the driver
175 if ((ss
->record_bad_block(a
, mdi
->disk
.raid_disk
, sector
, length
)) &&
176 (write(mdi
->bb_fd
, buf
, buf_len
) == buf_len
))
180 * failed to store or acknowledge bad block, switch of bad block support
181 * to get it out of blocked state
183 sysfs_set_str(&a
->info
, mdi
, "state", "-external_bbl");
187 static int read_bb_file(int fd
, struct active_array
*a
, struct mdinfo
*mdi
,
188 enum bb_action action
, void *arg
)
196 int preserve_pos
= (action
== RECORD_BB
? 0 : 1);
198 if (lseek(fd
, 0, SEEK_SET
) == (off_t
) -1)
203 n
= read(fd
, buf
+ pos
, sizeof(buf
) - 1 - pos
);
212 unsigned long long sector
;
219 /* kernel sysfs file format: "sector length\n" */
220 matched
= sscanf(buf
+ off
, "%llu %d%c%n", §or
,
221 &length
, &newline
, &consumed
);
222 if ((matched
!= 3) && (off
> 0)) {
223 /* truncated entry, read again */
225 pos
= sizeof(buf
) - off
- 1;
226 memmove(buf
, buf
+ off
, pos
);
228 if (lseek(fd
, 0, SEEK_SET
) ==
242 if (action
== RECORD_BB
)
243 rc
= process_ubb(a
, mdi
, sector
, length
,
244 buf
+ off
, consumed
);
253 } while (read_again
);
258 static int process_dev_ubb(struct active_array
*a
, struct mdinfo
*mdi
)
260 return read_bb_file(mdi
->ubb_fd
, a
, mdi
, RECORD_BB
, NULL
);
263 static void signal_manager(void)
265 /* tgkill(getpid(), mon_tid, SIGUSR1); */
267 syscall(SYS_tgkill
, pid
, mgr_tid
, SIGUSR1
);
270 /* Monitor a set of active md arrays - all of which share the
271 * same metadata - and respond to events that require
274 * New arrays are detected by another thread which allocates
275 * required memory and attaches the data structure to our list.
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(??)
283 * array_state if 'write-pending'
284 * We mark metadata as 'dirty' then set array to 'active'.
286 * Either ignore, or mark clean, then mark metadata as clean.
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
297 * sync_action was 'resync' and becomes 'idle' and resync_start becomes
299 * Notify metadata that sync is complete.
302 * sync_action changes from 'recover' to 'idle'
303 * Check each device state and mark metadata if 'faulty' or 'in_sync'.
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.
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.
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
327 #define ARRAY_DIRTY 1
329 static int read_and_act(struct active_array
*a
)
331 unsigned long long sync_completed
;
332 int check_degraded
= 0;
333 int check_reshape
= 0;
340 a
->next_state
= bad_word
;
341 a
->next_action
= bad_action
;
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
)
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
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
) {
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
);
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.
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
;
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
],
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
;
391 if (a
->curr_state
== write_pending
) {
392 a
->container
->ss
->set_array_state(a
, 0);
393 a
->next_state
= active
;
396 if (a
->curr_state
== active_idle
) {
397 /* Set array to 'clean' FIRST, then mark clean
400 a
->next_state
= clean
;
403 if (a
->curr_state
== clean
) {
404 a
->container
->ss
->set_array_state(a
, 1);
406 if (a
->curr_state
== active
||
407 a
->curr_state
== suspended
)
409 if (a
->curr_state
== readonly
) {
410 /* Well, I'm ready to handle things. If readonly
411 * wasn't requested, transition to read-auto.
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 */
419 if (a
->container
->ss
->set_array_state(a
, 2))
420 a
->next_state
= read_auto
; /* array is clean */
422 a
->next_state
= active
; /* Now active for recovery etc */
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.
436 a
->container
->ss
->set_array_state(a
, a
->curr_state
<= clean
);
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
446 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
) {
447 a
->container
->ss
->set_disk(a
, mdi
->disk
.raid_disk
,
449 if (! (mdi
->curr_state
& DS_INSYNC
))
453 if (count
!= a
->info
.array
.raid_disks
)
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
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
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
,
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
;
484 if (a
->curr_state
> readonly
)
485 mdi
->next_state
|= DS_REMOVE
;
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.
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
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
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
518 if ((a
->curr_action
!= reshape
) &&
519 (a
->prev_action
== reshape
)) {
521 if ((sysfs_get_str(&a
->info
, NULL
,
524 sizeof(buf
)) >= 0) &&
525 strncmp(buf
, "none", 4) == 0)
526 a
->last_checkpoint
= a
->info
.component_size
;
528 a
->container
->ss
->set_array_state(a
, a
->curr_state
<= clean
);
529 a
->last_checkpoint
= sync_completed
;
532 if (sync_completed
> a
->last_checkpoint
)
533 a
->last_checkpoint
= sync_completed
;
535 if (sync_completed
>= a
->info
.component_size
)
536 a
->last_checkpoint
= 0;
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
]);
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
);
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
]);
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
);
557 if ((mdi
->next_state
& DS_REMOVE
) && mdi
->state_fd
>= 0) {
560 /* The kernel may not be able to immediately remove the
561 * disk. In that case we wait a little while and
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
);
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
);
580 dprintf_cont(" )\n");
582 /* move curr_ to prev_ */
583 a
->prev_state
= a
->curr_state
;
585 a
->prev_action
= a
->curr_action
;
587 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
) {
588 mdi
->prev_state
= mdi
->curr_state
;
592 if (check_degraded
|| check_reshape
) {
593 /* manager will do the actual check */
595 a
->check_degraded
= 1;
597 a
->check_reshape
= 1;
607 static struct mdinfo
*
608 find_device(struct active_array
*a
, int major
, int minor
)
612 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
613 if (mdi
->disk
.major
== major
&& mdi
->disk
.minor
== minor
)
619 static void reconcile_failed(struct active_array
*aa
, struct mdinfo
*failed
)
621 struct active_array
*a
;
622 struct mdinfo
*victim
;
624 for (a
= aa
; a
; a
= a
->next
) {
625 if (!a
->container
|| a
->to_remove
)
627 victim
= find_device(a
, failed
->disk
.major
, failed
->disk
.minor
);
631 if (!(victim
->curr_state
& DS_FAULTY
))
632 write_attr("faulty", victim
->state_fd
);
637 static void dprint_wake_reasons(fd_set
*fds
)
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",
651 rv
= readlink(proc_path
, link
, sizeof(link
) - 1);
653 fprintf(stderr
, "%d:unknown ", i
);
657 basename
= strrchr(link
, '/');
658 fprintf(stderr
, "%d:%s ",
659 i
, basename
? ++basename
: link
);
662 fprintf(stderr
, ")\n");
666 int monitor_loop_cnt
;
668 static int wait_and_act(struct supertype
*container
, int nowait
)
672 struct active_array
**aap
= &container
->arrays
;
673 struct active_array
*a
, **ap
;
676 static unsigned int dirty_arrays
= ~0; /* start at some non-zero value */
680 for (ap
= aap
; *ap
;) {
682 /* once an array has been deactivated we want to
683 * ask the manager to discard it.
685 if (!a
->container
|| a
->to_remove
) {
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
);
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.
718 fd
= open_dev_excl(container
->devnm
);
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");
726 dprintf("no arrays to monitor... exiting\n");
728 /* On SIGTERM, someone (the take-over mdmon) will
731 remove_pidfile(container
->devnm
);
744 if (*aap
== NULL
|| container
->retry_soon
) {
745 /* just waiting to get O_EXCL access */
747 ts
.tv_nsec
= 20000000ULL;
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;
755 if (errno
== EINTR
) {
757 dprintf("monitor: caught signal\n");
759 dprintf("monitor: error %d in pselect\n",
764 dprint_wake_reasons(&rfds
);
766 container
->retry_soon
= 0;
770 struct metadata_update
*this;
772 for (this = update_queue
; this ; this = this->next
)
773 container
->ss
->process_update(container
, this);
775 update_queue_handled
= update_queue
;
778 container
->ss
->sync_metadata(container
);
783 for (a
= *aap
; a
; a
= a
->next
) {
785 if (a
->replaces
&& !discard_this
) {
786 struct active_array
**ap
;
787 for (ap
= &a
->next
; *ap
&& *ap
!= a
->replaces
;
792 discard_this
= a
->replaces
;
794 /* FIXME check if device->state_fd need to be cleared?*/
797 if (a
->container
&& !a
->to_remove
) {
798 int ret
= read_and_act(a
);
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'
805 if (sigterm
&& !(ret
& ARRAY_DIRTY
))
806 a
->container
= NULL
; /* stop touching this array */
807 if (ret
& ARRAY_BUSY
)
808 container
->retry_soon
= 1;
812 /* propagate failures across container members */
813 for (a
= *aap
; a
; a
= a
->next
) {
814 if (!a
->container
|| a
->to_remove
)
816 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
817 if (mdi
->curr_state
& DS_FAULTY
)
818 reconcile_failed(*aap
, mdi
);
824 void do_monitor(struct supertype
*container
)
829 rv
= wait_and_act(container
, first
);