]>
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>
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
38 static void add_fd(fd_set
*fds
, int *maxfd
, int fd
)
43 if (fstat(fd
, &st
) == -1) {
44 dprintf("Invalid fd %d\n", fd
);
47 if (st
.st_nlink
== 0) {
48 dprintf("fd %d was deleted\n", fd
);
56 static int read_attr(char *buf
, int len
, int fd
)
65 n
= read(fd
, buf
, len
- 1);
77 static void read_resync_start(int fd
, unsigned long long *v
)
79 char buf
[SYSFS_MAX_BUF_SIZE
];
82 n
= read_attr(buf
, sizeof(buf
), fd
);
84 dprintf("Failed to read resync_start (%d)\n", fd
);
87 if (str_is_none(buf
) == true)
90 *v
= strtoull(buf
, NULL
, 10);
93 static unsigned long long read_sync_completed(int fd
)
95 unsigned long long val
;
96 char buf
[SYSFS_MAX_BUF_SIZE
];
100 n
= read_attr(buf
, sizeof(buf
), fd
);
105 val
= strtoull(buf
, &ep
, 0);
106 if (ep
== buf
|| (*ep
!= 0 && *ep
!= '\n' && *ep
!= ' '))
111 static enum array_state
read_state(int fd
)
113 char buf
[SYSFS_MAX_BUF_SIZE
];
114 int n
= read_attr(buf
, sizeof(buf
), fd
);
118 return (enum array_state
) sysfs_match_word(buf
, array_states
);
121 static enum sync_action
read_action( int fd
)
123 char buf
[SYSFS_MAX_BUF_SIZE
];
124 int n
= read_attr(buf
, sizeof(buf
), fd
);
128 return (enum sync_action
) sysfs_match_word(buf
, sync_actions
);
131 int read_dev_state(int fd
)
133 char buf
[SYSFS_MAX_BUF_SIZE
];
134 int n
= read_attr(buf
, sizeof(buf
), fd
);
143 if (sysfs_attr_match(cp
, map_memb_state(MEMB_STATE_FAULTY
)))
145 if (sysfs_attr_match(cp
, map_memb_state(MEMB_STATE_IN_SYNC
)))
147 if (sysfs_attr_match(cp
, map_memb_state(MEMB_STATE_WRITE_MOSTLY
)))
148 rv
|= DS_WRITE_MOSTLY
;
149 if (sysfs_attr_match(cp
, map_memb_state(MEMB_STATE_SPARE
)))
151 if (sysfs_attr_match(cp
, map_memb_state(MEMB_STATE_BLOCKED
)))
153 if (sysfs_attr_match(cp
, map_memb_state(MEMB_STATE_EXTERNAL_BBL
)))
154 rv
|= DS_EXTERNAL_BB
;
155 cp
= strchr(cp
, ',');
162 int process_ubb(struct active_array
*a
, struct mdinfo
*mdi
, const unsigned long
163 long sector
, const int length
, const char *buf
,
166 struct superswitch
*ss
= a
->container
->ss
;
169 * record bad block in metadata first, then acknowledge it to the driver
172 if ((ss
->record_bad_block(a
, mdi
->disk
.raid_disk
, sector
, length
)) &&
173 (sysfs_write_descriptor(mdi
->bb_fd
, buf
, buf_len
, NULL
) == MDADM_STATUS_SUCCESS
))
177 * failed to store or acknowledge bad block, switch of bad block support
178 * to get it out of blocked state
180 sysfs_set_str(&a
->info
, mdi
, "state", "-external_bbl");
184 int compare_bb(struct active_array
*a
, struct mdinfo
*mdi
, const unsigned long
185 long sector
, const unsigned int length
, void *arg
)
187 struct superswitch
*ss
= a
->container
->ss
;
188 struct md_bb
*bb
= (struct md_bb
*) arg
;
192 for (i
= 0; i
< bb
->count
; i
++) {
193 unsigned long long start
= bb
->entries
[i
].sector
;
194 unsigned long long len
= bb
->entries
[i
].length
;
197 * bad block in metadata exactly matches bad block in kernel
198 * list, just remove it from a list
200 if ((start
== sector
) && (len
== length
)) {
201 if (i
< bb
->count
- 1)
202 bb
->entries
[i
] = bb
->entries
[bb
->count
- 1];
208 * bad block in metadata spans bad block in kernel list,
209 * clear it and record new bad block
211 if ((sector
>= start
) && (sector
+ length
<= start
+ len
)) {
212 ss
->clear_bad_block(a
, mdi
->disk
.raid_disk
, start
, len
);
217 /* record all bad blocks not in metadata list */
218 if (record
&& (ss
->record_bad_block(a
, mdi
->disk
.raid_disk
, sector
,
220 sysfs_set_str(&a
->info
, mdi
, "state", "-external_bbl");
227 static int read_bb_file(int fd
, struct active_array
*a
, struct mdinfo
*mdi
,
228 enum bb_action action
, void *arg
)
236 int preserve_pos
= (action
== RECORD_BB
? 0 : 1);
238 if (lseek(fd
, 0, SEEK_SET
) == (off_t
) -1)
243 n
= read(fd
, buf
+ pos
, sizeof(buf
) - 1 - pos
);
252 unsigned long long sector
;
259 /* kernel sysfs file format: "sector length\n" */
260 matched
= sscanf(buf
+ off
, "%llu %d%c%n", §or
,
261 &length
, &newline
, &consumed
);
262 if ((matched
!= 3) && (off
> 0)) {
263 /* truncated entry, read again */
265 pos
= sizeof(buf
) - off
- 1;
266 memmove(buf
, buf
+ off
, pos
);
268 if (lseek(fd
, 0, SEEK_SET
) ==
282 if (action
== RECORD_BB
)
283 rc
= process_ubb(a
, mdi
, sector
, length
,
284 buf
+ off
, consumed
);
285 else if (action
== COMPARE_BB
)
286 rc
= compare_bb(a
, mdi
, sector
, length
, arg
);
295 } while (read_again
);
300 static int process_dev_ubb(struct active_array
*a
, struct mdinfo
*mdi
)
302 return read_bb_file(mdi
->ubb_fd
, a
, mdi
, RECORD_BB
, NULL
);
305 static int check_for_cleared_bb(struct active_array
*a
, struct mdinfo
*mdi
)
307 struct superswitch
*ss
= a
->container
->ss
;
312 * Get a list of bad blocks for an array, then read list of
313 * acknowledged bad blocks from kernel and compare it against metadata
314 * list, clear all bad blocks remaining in metadata list
316 bb
= ss
->get_bad_blocks(a
, mdi
->disk
.raid_disk
);
320 if (read_bb_file(mdi
->bb_fd
, a
, mdi
, COMPARE_BB
, bb
) < 0)
323 for (i
= 0; i
< bb
->count
; i
++) {
324 unsigned long long sector
= bb
->entries
[i
].sector
;
325 int length
= bb
->entries
[i
].length
;
327 ss
->clear_bad_block(a
, mdi
->disk
.raid_disk
, sector
, length
);
333 static void signal_manager(void)
335 /* tgkill(getpid(), mon_tid, SIGUSR1); */
337 syscall(SYS_tgkill
, pid
, mgr_tid
, SIGUSR1
);
340 /* Monitor a set of active md arrays - all of which share the
341 * same metadata - and respond to events that require
344 * New arrays are detected by another thread which allocates
345 * required memory and attaches the data structure to our list.
349 * This is detected by array_state going to 'clear' or 'inactive'.
350 * while we thought it was active.
351 * Response is to mark metadata as clean and 'clear' the array(??)
353 * array_state if 'write-pending'
354 * We mark metadata as 'dirty' then set array to 'active'.
356 * Either ignore, or mark clean, then mark metadata as clean.
359 * detected by rd-N/state reporting "faulty"
360 * mark device as 'failed' in metadata, let the kernel release the
361 * device by writing '-blocked' to rd/state, and finally write 'remove' to
362 * rd/state. Before a disk can be replaced it must be failed and removed
363 * from all container members, this will be preemptive for the other
367 * sync_action was 'resync' and becomes 'idle' and resync_start becomes
369 * Notify metadata that sync is complete.
372 * sync_action changes from 'recover' to 'idle'
373 * Check each device state and mark metadata if 'faulty' or 'in_sync'.
376 * This only happens on finding a new array... mdadm will have set
377 * 'resync_start' to the correct value. If 'resync_start' indicates that an
378 * resync needs to occur set the array to the 'active' state rather than the
379 * initial read-auto state.
383 * We wait for a change (poll/select) on array_state, sync_action, and
384 * each rd-X/state file.
385 * When we get any change, we check everything. So read each state file,
386 * then decide what to do.
388 * The core action is to write new metadata to all devices in the array.
389 * This is done at most once on any wakeup.
390 * After that we might:
391 * - update the array_state
392 * - set the role of some devices.
393 * - request a sync_action
397 #define ARRAY_DIRTY 1
399 static int read_and_act(struct active_array
*a
)
401 unsigned long long sync_completed
;
402 bool disks_to_remove
= false;
403 bool check_degraded
= false;
404 bool check_reshape
= false;
409 bool write_checkpoint
= false;
411 a
->next_state
= bad_word
;
412 a
->next_action
= bad_action
;
414 a
->curr_state
= read_state(a
->info
.state_fd
);
415 a
->curr_action
= read_action(a
->action_fd
);
416 if (a
->curr_state
!= clear
)
418 * In "clear" state, resync_start may wrongly be set to "0"
419 * when the kernel called md_clean but didn't remove the
420 * sysfs attributes yet
422 read_resync_start(a
->resync_start_fd
, &a
->info
.resync_start
);
423 sync_completed
= read_sync_completed(a
->sync_completed_fd
);
424 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
) {
428 if (mdi
->man_disk_to_remove
)
429 /* We are removing this device, skip it then */
432 read_resync_start(mdi
->recovery_fd
, &mdi
->recovery_start
);
433 mdi
->curr_state
= read_dev_state(mdi
->state_fd
);
435 if (!(mdi
->curr_state
& DS_EXTERNAL_BB
))
437 * It assumes that superswitch badblock functions are set if disk
438 * has external badblocks support configured.
442 if ((mdi
->curr_state
& DS_BLOCKED
) && process_dev_ubb(a
, mdi
) > 0)
444 * Blocked has two meanings: we need to acknowledge failure or badblocks
445 * (if supported). Here, badblocks are handled.
447 * If successful, unblock the array. This is not perfect but
448 * process_dev_ubb() may disable badblock support in case of failure.
450 mdi
->next_state
|= DS_UNBLOCK
;
452 check_for_cleared_bb(a
, mdi
);
455 dprintf("(%d): state:%s prev:%s action:%s prev: %s start:%llu\n",
456 a
->info
.container_member
,
457 array_states
[a
->curr_state
],
458 array_states
[a
->prev_state
],
459 sync_actions
[a
->curr_action
],
460 sync_actions
[a
->prev_action
],
464 if ((a
->curr_state
== bad_word
|| a
->curr_state
<= inactive
) &&
465 a
->prev_state
> inactive
) {
466 /* array has been stopped */
467 a
->container
->ss
->set_array_state(a
, 1);
468 a
->next_state
= clear
;
471 if (a
->curr_state
== write_pending
) {
472 a
->container
->ss
->set_array_state(a
, 0);
473 a
->next_state
= active
;
476 if (a
->curr_state
== active_idle
) {
477 /* Set array to 'clean' FIRST, then mark clean
480 a
->next_state
= clean
;
483 if ((a
->curr_state
== clean
) || (a
->curr_state
== broken
)) {
484 a
->container
->ss
->set_array_state(a
, 1);
486 if (a
->curr_state
== active
||
487 a
->curr_state
== suspended
)
489 if (a
->curr_state
== readonly
) {
490 /* Well, I'm ready to handle things. If readonly
491 * wasn't requested, transition to read-auto.
494 read_attr(buf
, sizeof(buf
), a
->metadata_fd
);
495 if (strncmp(buf
, "external:-", 10) == 0) {
496 /* explicit request for readonly array. Leave it alone */
499 if (a
->container
->ss
->set_array_state(a
, 2))
500 a
->next_state
= read_auto
; /* array is clean */
502 a
->next_state
= active
; /* Now active for recovery etc */
509 a
->curr_action
== idle
&&
510 a
->prev_action
== resync
) {
511 /* A resync has finished. The endpoint is recorded in
512 * 'sync_start'. We don't update the metadata
513 * until the array goes inactive or readonly though.
514 * Just check if we need to fiddle spares.
516 a
->container
->ss
->set_array_state(a
, a
->curr_state
<= clean
);
521 a
->curr_action
== idle
&&
522 a
->prev_action
== recover
) {
523 /* A recovery has finished. Some disks may be in sync now,
524 * and the array may no longer be degraded
526 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
) {
527 a
->container
->ss
->set_disk(a
, mdi
->disk
.raid_disk
,
529 if (! (mdi
->curr_state
& DS_INSYNC
))
533 if (count
!= a
->info
.array
.raid_disks
)
538 a
->curr_action
== reshape
&&
539 a
->prev_action
!= reshape
)
540 /* reshape was requested by mdadm. Need to see if
541 * new devices have been added. Manager does that
542 * when it sees check_reshape
546 /* Check for failures and if found:
547 * 1/ Record the failure in the metadata and unblock the device.
548 * FIXME update the kernel to stop notifying on failed drives when
549 * the array is readonly and we have cleared 'blocked'
550 * 2/ Try to remove the device if the array is writable, or can be
553 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
) {
554 if (mdi
->curr_state
& DS_FAULTY
) {
555 a
->container
->ss
->set_disk(a
, mdi
->disk
.raid_disk
,
558 if (mdi
->curr_state
& DS_BLOCKED
)
559 mdi
->next_state
|= DS_UNBLOCK
;
560 if (a
->curr_state
== read_auto
) {
561 a
->container
->ss
->set_array_state(a
, 0);
562 a
->next_state
= active
;
564 if (a
->curr_state
> readonly
)
565 mdi
->next_state
|= DS_REMOVE
;
569 /* Update reshape checkpoint, depending if it finished or progressed */
570 if (a
->curr_action
== idle
&& a
->prev_action
== reshape
) {
571 char buf
[SYSFS_MAX_BUF_SIZE
];
573 if (sync_completed
!= 0)
574 a
->last_checkpoint
= sync_completed
;
577 * If reshape really finished, set checkpoint to the end to finalize it.
578 * Do not set checkpoint if reshape is broken.
579 * Reshape will restart from last checkpoint.
581 if (sysfs_get_str(&a
->info
, NULL
, "reshape_position", buf
, sizeof(buf
)) >= 0)
582 if (str_is_none(buf
) == true)
583 a
->last_checkpoint
= a
->info
.component_size
;
585 write_checkpoint
= true;
588 if (a
->curr_action
>= reshape
&& sync_completed
> a
->last_checkpoint
) {
589 /* Update checkpoint if neither reshape nor idle action */
590 a
->last_checkpoint
= sync_completed
;
592 write_checkpoint
= true;
595 /* Save checkpoint */
596 if (write_checkpoint
) {
597 a
->container
->ss
->set_array_state(a
, a
->curr_state
<= clean
);
599 if (a
->curr_action
<= reshape
)
600 a
->last_checkpoint
= sync_completed
;
603 if (sync_completed
>= a
->info
.component_size
)
604 a
->last_checkpoint
= 0;
606 a
->container
->ss
->sync_metadata(a
->container
);
607 dprintf("(%d): state:%s action:%s next(", a
->info
.container_member
,
608 array_states
[a
->curr_state
], sync_actions
[a
->curr_action
]);
610 /* Effect state changes in the array */
611 if (a
->next_state
!= bad_word
) {
612 dprintf_cont(" state:%s", array_states
[a
->next_state
]);
613 write_attr(array_states
[a
->next_state
], a
->info
.state_fd
);
615 if (a
->next_action
!= bad_action
) {
616 write_attr(sync_actions
[a
->next_action
], a
->action_fd
);
617 dprintf_cont(" action:%s", sync_actions
[a
->next_action
]);
619 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
) {
620 if (mdi
->next_state
& DS_UNBLOCK
) {
621 dprintf_cont(" %d:-blocked", mdi
->disk
.raid_disk
);
622 write_attr("-blocked", mdi
->state_fd
);
625 if ((mdi
->next_state
& DS_REMOVE
) && !mdi
->man_disk_to_remove
) {
626 dprintf_cont(" %d:disk_to_remove", mdi
->disk
.raid_disk
);
627 mdi
->man_disk_to_remove
= true;
628 disks_to_remove
= true;
631 if (mdi
->next_state
& DS_INSYNC
) {
632 write_attr("+in_sync", mdi
->state_fd
);
633 dprintf_cont(" %d:+in_sync", mdi
->disk
.raid_disk
);
636 dprintf_cont(" )\n");
638 /* move curr_ to prev_ */
639 a
->prev_state
= a
->curr_state
;
641 a
->prev_action
= a
->curr_action
;
643 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
644 mdi
->prev_state
= mdi
->curr_state
;
646 if (check_degraded
|| check_reshape
|| disks_to_remove
) {
648 a
->check_member_remove
|= disks_to_remove
;
649 a
->check_degraded
|= check_degraded
;
650 a
->check_reshape
|= check_reshape
;
660 static struct mdinfo
*
661 find_device(struct active_array
*a
, int major
, int minor
)
665 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
666 if (mdi
->disk
.major
== major
&& mdi
->disk
.minor
== minor
)
672 static void reconcile_failed(struct active_array
*aa
, struct mdinfo
*failed
)
674 struct active_array
*a
;
675 struct mdinfo
*victim
;
677 for (a
= aa
; a
; a
= a
->next
) {
678 if (!a
->container
|| a
->to_remove
)
680 victim
= find_device(a
, failed
->disk
.major
, failed
->disk
.minor
);
684 if (!(victim
->curr_state
& DS_FAULTY
))
685 write_attr("faulty", victim
->state_fd
);
690 static void dprint_wake_reasons(fd_set
*fds
)
698 fprintf(stderr
, "monitor: wake ( ");
699 for (i
= 0; i
< FD_SETSIZE
; i
++) {
700 if (FD_ISSET(i
, fds
)) {
701 sprintf(proc_path
, "/proc/%d/fd/%d",
704 rv
= readlink(proc_path
, link
, sizeof(link
) - 1);
706 fprintf(stderr
, "%d:unknown ", i
);
710 basename
= strrchr(link
, '/');
711 fprintf(stderr
, "%d:%s ",
712 i
, basename
? ++basename
: link
);
715 fprintf(stderr
, ")\n");
719 int monitor_loop_cnt
;
721 static int wait_and_act(struct supertype
*container
, int nowait
)
723 struct active_array
*a
, **ap
, **aap
= &container
->arrays
;
724 static unsigned int dirty_arrays
= ~0; /* start at some non-zero value */
731 for (ap
= aap
; *ap
;) {
733 /* once an array has been deactivated we want to
734 * ask the manager to discard it.
736 if (!a
->container
|| a
->to_remove
) {
748 add_fd(&rfds
, &maxfd
, a
->info
.state_fd
);
749 add_fd(&rfds
, &maxfd
, a
->action_fd
);
750 add_fd(&rfds
, &maxfd
, a
->sync_completed_fd
);
752 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
) {
753 if (mdi
->man_disk_to_remove
) {
754 mdi
->mon_descriptors_not_used
= true;
756 /* Managemon could be blocked on suspend in kernel.
757 * Monitor must respond if any badblock is recorded in this time.
759 container
->retry_soon
= 1;
763 add_fd(&rfds
, &maxfd
, mdi
->state_fd
);
764 add_fd(&rfds
, &maxfd
, mdi
->bb_fd
);
765 add_fd(&rfds
, &maxfd
, mdi
->ubb_fd
);
771 if (manager_ready
&& (*aap
== NULL
|| (sigterm
&& !dirty_arrays
))) {
772 /* No interesting arrays, or we have been told to
773 * terminate and everything is clean. Lets see about
774 * exiting. Note that blocking at this point is not a
775 * problem as there are no active arrays, there is
776 * nothing that we need to be ready to do.
780 fd
= open_dev_excl(container
->devnm
);
782 fd
= open_dev_flags(container
->devnm
, O_RDONLY
|O_EXCL
);
783 if (fd
>= 0 || errno
!= EBUSY
) {
784 /* OK, we are safe to leave */
785 if (sigterm
&& !dirty_arrays
)
786 dprintf("caught sigterm, all clean... exiting\n");
788 dprintf("no arrays to monitor... exiting\n");
790 /* On SIGTERM, someone (the take-over mdmon) will
793 remove_pidfile(container
->devnm
);
806 if (*aap
== NULL
|| container
->retry_soon
) {
807 /* just waiting to get O_EXCL access */
809 ts
.tv_nsec
= 20000000ULL;
811 sigprocmask(SIG_UNBLOCK
, NULL
, &set
);
812 sigdelset(&set
, SIGUSR1
);
813 monitor_loop_cnt
|= 1;
814 rv
= pselect(maxfd
+1, NULL
, NULL
, &rfds
, &ts
, &set
);
815 monitor_loop_cnt
+= 1;
817 if (errno
== EINTR
) {
820 dprintf("monitor: caught signal\n");
822 dprintf("monitor: error %d in pselect\n",
827 dprint_wake_reasons(&rfds
);
829 container
->retry_soon
= 0;
833 struct metadata_update
*this;
835 for (this = update_queue
; this ; this = this->next
)
836 container
->ss
->process_update(container
, this);
838 update_queue_handled
= update_queue
;
841 container
->ss
->sync_metadata(container
);
846 for (a
= *aap
; a
; a
= a
->next
) {
848 if (a
->replaces
&& !discard_this
) {
849 struct active_array
**ap
;
850 for (ap
= &a
->next
; *ap
&& *ap
!= a
->replaces
;
855 discard_this
= a
->replaces
;
857 /* FIXME check if device->state_fd need to be cleared?*/
860 if (a
->container
&& !a
->to_remove
) {
861 int ret
= read_and_act(a
);
864 dirty_arrays
+= !!(ret
& ARRAY_DIRTY
);
865 /* when terminating stop manipulating the array after it
866 * is clean, but make sure read_and_act() is given a
867 * chance to handle 'active_idle'
869 if (sigterm
&& !(ret
& ARRAY_DIRTY
))
870 a
->container
= NULL
; /* stop touching this array */
871 if (ret
& ARRAY_BUSY
)
872 container
->retry_soon
= 1;
876 /* propagate failures across container members */
877 for (a
= *aap
; a
; a
= a
->next
) {
878 if (!a
->container
|| a
->to_remove
)
880 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
881 if (mdi
->curr_state
& DS_FAULTY
)
882 reconcile_failed(*aap
, mdi
);
888 void do_monitor(struct supertype
*container
)
893 rv
= wait_and_act(container
, first
);