]>
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 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
)
84 char buf
[SYSFS_MAX_BUF_SIZE
];
87 n
= read_attr(buf
, sizeof(buf
), fd
);
89 dprintf("Failed to read resync_start (%d)\n", fd
);
92 if (str_is_none(buf
) == true)
95 *v
= strtoull(buf
, NULL
, 10);
98 static unsigned long long read_sync_completed(int fd
)
100 unsigned long long val
;
101 char buf
[SYSFS_MAX_BUF_SIZE
];
105 n
= read_attr(buf
, sizeof(buf
), 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
)
118 char buf
[SYSFS_MAX_BUF_SIZE
];
119 int n
= read_attr(buf
, sizeof(buf
), fd
);
123 return (enum array_state
) sysfs_match_word(buf
, array_states
);
126 static enum sync_action
read_action( int fd
)
128 char buf
[SYSFS_MAX_BUF_SIZE
];
129 int n
= read_attr(buf
, sizeof(buf
), fd
);
133 return (enum sync_action
) sysfs_match_word(buf
, sync_actions
);
136 int read_dev_state(int fd
)
138 char buf
[SYSFS_MAX_BUF_SIZE
];
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 int compare_bb(struct active_array
*a
, struct mdinfo
*mdi
, const unsigned long
188 long sector
, const unsigned int length
, void *arg
)
190 struct superswitch
*ss
= a
->container
->ss
;
191 struct md_bb
*bb
= (struct md_bb
*) arg
;
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
;
200 * bad block in metadata exactly matches bad block in kernel
201 * list, just remove it from a list
203 if ((start
== sector
) && (len
== length
)) {
204 if (i
< bb
->count
- 1)
205 bb
->entries
[i
] = bb
->entries
[bb
->count
- 1];
211 * bad block in metadata spans bad block in kernel list,
212 * clear it and record new bad block
214 if ((sector
>= start
) && (sector
+ length
<= start
+ len
)) {
215 ss
->clear_bad_block(a
, mdi
->disk
.raid_disk
, start
, len
);
220 /* record all bad blocks not in metadata list */
221 if (record
&& (ss
->record_bad_block(a
, mdi
->disk
.raid_disk
, sector
,
223 sysfs_set_str(&a
->info
, mdi
, "state", "-external_bbl");
230 static int read_bb_file(int fd
, struct active_array
*a
, struct mdinfo
*mdi
,
231 enum bb_action action
, void *arg
)
239 int preserve_pos
= (action
== RECORD_BB
? 0 : 1);
241 if (lseek(fd
, 0, SEEK_SET
) == (off_t
) -1)
246 n
= read(fd
, buf
+ pos
, sizeof(buf
) - 1 - pos
);
255 unsigned long long sector
;
262 /* kernel sysfs file format: "sector length\n" */
263 matched
= sscanf(buf
+ off
, "%llu %d%c%n", §or
,
264 &length
, &newline
, &consumed
);
265 if ((matched
!= 3) && (off
> 0)) {
266 /* truncated entry, read again */
268 pos
= sizeof(buf
) - off
- 1;
269 memmove(buf
, buf
+ off
, pos
);
271 if (lseek(fd
, 0, SEEK_SET
) ==
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
);
298 } while (read_again
);
303 static int process_dev_ubb(struct active_array
*a
, struct mdinfo
*mdi
)
305 return read_bb_file(mdi
->ubb_fd
, a
, mdi
, RECORD_BB
, NULL
);
308 static int check_for_cleared_bb(struct active_array
*a
, struct mdinfo
*mdi
)
310 struct superswitch
*ss
= a
->container
->ss
;
314 if (!ss
->get_bad_blocks
)
318 * Get a list of bad blocks for an array, then read list of
319 * acknowledged bad blocks from kernel and compare it against metadata
320 * list, clear all bad blocks remaining in metadata list
322 bb
= ss
->get_bad_blocks(a
, mdi
->disk
.raid_disk
);
326 if (read_bb_file(mdi
->bb_fd
, a
, mdi
, COMPARE_BB
, bb
) < 0)
329 for (i
= 0; i
< bb
->count
; i
++) {
330 unsigned long long sector
= bb
->entries
[i
].sector
;
331 int length
= bb
->entries
[i
].length
;
333 ss
->clear_bad_block(a
, mdi
->disk
.raid_disk
, sector
, length
);
339 static void signal_manager(void)
341 /* tgkill(getpid(), mon_tid, SIGUSR1); */
343 syscall(SYS_tgkill
, pid
, mgr_tid
, SIGUSR1
);
346 /* Monitor a set of active md arrays - all of which share the
347 * same metadata - and respond to events that require
350 * New arrays are detected by another thread which allocates
351 * required memory and attaches the data structure to our list.
355 * This is detected by array_state going to 'clear' or 'inactive'.
356 * while we thought it was active.
357 * Response is to mark metadata as clean and 'clear' the array(??)
359 * array_state if 'write-pending'
360 * We mark metadata as 'dirty' then set array to 'active'.
362 * Either ignore, or mark clean, then mark metadata as clean.
365 * detected by rd-N/state reporting "faulty"
366 * mark device as 'failed' in metadata, let the kernel release the
367 * device by writing '-blocked' to rd/state, and finally write 'remove' to
368 * rd/state. Before a disk can be replaced it must be failed and removed
369 * from all container members, this will be preemptive for the other
373 * sync_action was 'resync' and becomes 'idle' and resync_start becomes
375 * Notify metadata that sync is complete.
378 * sync_action changes from 'recover' to 'idle'
379 * Check each device state and mark metadata if 'faulty' or 'in_sync'.
382 * This only happens on finding a new array... mdadm will have set
383 * 'resync_start' to the correct value. If 'resync_start' indicates that an
384 * resync needs to occur set the array to the 'active' state rather than the
385 * initial read-auto state.
389 * We wait for a change (poll/select) on array_state, sync_action, and
390 * each rd-X/state file.
391 * When we get any change, we check everything. So read each state file,
392 * then decide what to do.
394 * The core action is to write new metadata to all devices in the array.
395 * This is done at most once on any wakeup.
396 * After that we might:
397 * - update the array_state
398 * - set the role of some devices.
399 * - request a sync_action
403 #define ARRAY_DIRTY 1
405 static int read_and_act(struct active_array
*a
, fd_set
*fds
)
407 unsigned long long sync_completed
;
408 int check_degraded
= 0;
409 int check_reshape
= 0;
415 bool write_checkpoint
= false;
417 a
->next_state
= bad_word
;
418 a
->next_action
= bad_action
;
420 a
->curr_state
= read_state(a
->info
.state_fd
);
421 a
->curr_action
= read_action(a
->action_fd
);
422 if (a
->curr_state
!= clear
)
424 * In "clear" state, resync_start may wrongly be set to "0"
425 * when the kernel called md_clean but didn't remove the
426 * sysfs attributes yet
428 read_resync_start(a
->resync_start_fd
, &a
->info
.resync_start
);
429 sync_completed
= read_sync_completed(a
->sync_completed_fd
);
430 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
) {
433 if (mdi
->state_fd
>= 0) {
434 read_resync_start(mdi
->recovery_fd
,
435 &mdi
->recovery_start
);
436 mdi
->curr_state
= read_dev_state(mdi
->state_fd
);
439 * If array is blocked and metadata handler is able to handle
440 * BB, check if you can acknowledge them to md driver. If
441 * successful, clear faulty state and unblock the array.
443 if ((mdi
->curr_state
& DS_BLOCKED
) &&
444 a
->container
->ss
->record_bad_block
&&
445 (process_dev_ubb(a
, mdi
) > 0)) {
446 mdi
->next_state
|= DS_UNBLOCK
;
448 if (FD_ISSET(mdi
->bb_fd
, fds
))
449 check_for_cleared_bb(a
, mdi
);
452 gettimeofday(&tv
, NULL
);
453 dprintf("(%d): %ld.%06ld state:%s prev:%s action:%s prev: %s start:%llu\n",
454 a
->info
.container_member
,
455 tv
.tv_sec
, tv
.tv_usec
,
456 array_states
[a
->curr_state
],
457 array_states
[a
->prev_state
],
458 sync_actions
[a
->curr_action
],
459 sync_actions
[a
->prev_action
],
463 if ((a
->curr_state
== bad_word
|| a
->curr_state
<= inactive
) &&
464 a
->prev_state
> inactive
) {
465 /* array has been stopped */
466 a
->container
->ss
->set_array_state(a
, 1);
467 a
->next_state
= clear
;
470 if (a
->curr_state
== write_pending
) {
471 a
->container
->ss
->set_array_state(a
, 0);
472 a
->next_state
= active
;
475 if (a
->curr_state
== active_idle
) {
476 /* Set array to 'clean' FIRST, then mark clean
479 a
->next_state
= clean
;
482 if ((a
->curr_state
== clean
) || (a
->curr_state
== broken
)) {
483 a
->container
->ss
->set_array_state(a
, 1);
485 if (a
->curr_state
== active
||
486 a
->curr_state
== suspended
)
488 if (a
->curr_state
== readonly
) {
489 /* Well, I'm ready to handle things. If readonly
490 * wasn't requested, transition to read-auto.
493 read_attr(buf
, sizeof(buf
), a
->metadata_fd
);
494 if (strncmp(buf
, "external:-", 10) == 0) {
495 /* explicit request for readonly array. Leave it alone */
498 if (a
->container
->ss
->set_array_state(a
, 2))
499 a
->next_state
= read_auto
; /* array is clean */
501 a
->next_state
= active
; /* Now active for recovery etc */
508 a
->curr_action
== idle
&&
509 a
->prev_action
== resync
) {
510 /* A resync has finished. The endpoint is recorded in
511 * 'sync_start'. We don't update the metadata
512 * until the array goes inactive or readonly though.
513 * Just check if we need to fiddle spares.
515 a
->container
->ss
->set_array_state(a
, a
->curr_state
<= clean
);
520 a
->curr_action
== idle
&&
521 a
->prev_action
== recover
) {
522 /* A recovery has finished. Some disks may be in sync now,
523 * and the array may no longer be degraded
525 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
) {
526 a
->container
->ss
->set_disk(a
, mdi
->disk
.raid_disk
,
528 if (! (mdi
->curr_state
& DS_INSYNC
))
532 if (count
!= a
->info
.array
.raid_disks
)
537 a
->curr_action
== reshape
&&
538 a
->prev_action
!= reshape
)
539 /* reshape was requested by mdadm. Need to see if
540 * new devices have been added. Manager does that
541 * when it sees check_reshape
545 /* Check for failures and if found:
546 * 1/ Record the failure in the metadata and unblock the device.
547 * FIXME update the kernel to stop notifying on failed drives when
548 * the array is readonly and we have cleared 'blocked'
549 * 2/ Try to remove the device if the array is writable, or can be
552 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
) {
553 if (mdi
->curr_state
& DS_FAULTY
) {
554 a
->container
->ss
->set_disk(a
, mdi
->disk
.raid_disk
,
557 if (mdi
->curr_state
& DS_BLOCKED
)
558 mdi
->next_state
|= DS_UNBLOCK
;
559 if (a
->curr_state
== read_auto
) {
560 a
->container
->ss
->set_array_state(a
, 0);
561 a
->next_state
= active
;
563 if (a
->curr_state
> readonly
)
564 mdi
->next_state
|= DS_REMOVE
;
568 /* Update reshape checkpoint, depending if it finished or progressed */
569 if (a
->curr_action
== idle
&& a
->prev_action
== reshape
) {
570 char buf
[SYSFS_MAX_BUF_SIZE
];
572 if (sync_completed
!= 0)
573 a
->last_checkpoint
= sync_completed
;
576 * If reshape really finished, set checkpoint to the end to finalize it.
577 * Do not set checkpoint if reshape is broken.
578 * Reshape will restart from last checkpoint.
580 if (sysfs_get_str(&a
->info
, NULL
, "reshape_position", buf
, sizeof(buf
)) >= 0)
581 if (str_is_none(buf
) == true)
582 a
->last_checkpoint
= a
->info
.component_size
;
584 write_checkpoint
= true;
587 if (a
->curr_action
>= reshape
&& sync_completed
> a
->last_checkpoint
) {
588 /* Update checkpoint if neither reshape nor idle action */
589 a
->last_checkpoint
= sync_completed
;
591 write_checkpoint
= true;
594 /* Save checkpoint */
595 if (write_checkpoint
) {
596 a
->container
->ss
->set_array_state(a
, a
->curr_state
<= clean
);
598 if (a
->curr_action
<= reshape
)
599 a
->last_checkpoint
= sync_completed
;
602 if (sync_completed
>= a
->info
.component_size
)
603 a
->last_checkpoint
= 0;
605 a
->container
->ss
->sync_metadata(a
->container
);
606 dprintf("(%d): state:%s action:%s next(", a
->info
.container_member
,
607 array_states
[a
->curr_state
], sync_actions
[a
->curr_action
]);
609 /* Effect state changes in the array */
610 if (a
->next_state
!= bad_word
) {
611 dprintf_cont(" state:%s", array_states
[a
->next_state
]);
612 write_attr(array_states
[a
->next_state
], a
->info
.state_fd
);
614 if (a
->next_action
!= bad_action
) {
615 write_attr(sync_actions
[a
->next_action
], a
->action_fd
);
616 dprintf_cont(" action:%s", sync_actions
[a
->next_action
]);
618 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
) {
619 if (mdi
->next_state
& DS_UNBLOCK
) {
620 dprintf_cont(" %d:-blocked", mdi
->disk
.raid_disk
);
621 write_attr("-blocked", mdi
->state_fd
);
624 if ((mdi
->next_state
& DS_REMOVE
) && mdi
->state_fd
>= 0) {
627 /* The kernel may not be able to immediately remove the
628 * disk. In that case we wait a little while and
631 remove_result
= write_attr("remove", mdi
->state_fd
);
632 if (remove_result
> 0) {
633 dprintf_cont(" %d:removed", mdi
->disk
.raid_disk
);
634 close(mdi
->state_fd
);
635 close(mdi
->recovery_fd
);
642 if (mdi
->next_state
& DS_INSYNC
) {
643 write_attr("+in_sync", mdi
->state_fd
);
644 dprintf_cont(" %d:+in_sync", mdi
->disk
.raid_disk
);
647 dprintf_cont(" )\n");
649 /* move curr_ to prev_ */
650 a
->prev_state
= a
->curr_state
;
652 a
->prev_action
= a
->curr_action
;
654 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
) {
655 mdi
->prev_state
= mdi
->curr_state
;
659 if (check_degraded
|| check_reshape
) {
660 /* manager will do the actual check */
662 a
->check_degraded
= 1;
664 a
->check_reshape
= 1;
674 static struct mdinfo
*
675 find_device(struct active_array
*a
, int major
, int minor
)
679 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
680 if (mdi
->disk
.major
== major
&& mdi
->disk
.minor
== minor
)
686 static void reconcile_failed(struct active_array
*aa
, struct mdinfo
*failed
)
688 struct active_array
*a
;
689 struct mdinfo
*victim
;
691 for (a
= aa
; a
; a
= a
->next
) {
692 if (!a
->container
|| a
->to_remove
)
694 victim
= find_device(a
, failed
->disk
.major
, failed
->disk
.minor
);
698 if (!(victim
->curr_state
& DS_FAULTY
))
699 write_attr("faulty", victim
->state_fd
);
704 static void dprint_wake_reasons(fd_set
*fds
)
712 fprintf(stderr
, "monitor: wake ( ");
713 for (i
= 0; i
< FD_SETSIZE
; i
++) {
714 if (FD_ISSET(i
, fds
)) {
715 sprintf(proc_path
, "/proc/%d/fd/%d",
718 rv
= readlink(proc_path
, link
, sizeof(link
) - 1);
720 fprintf(stderr
, "%d:unknown ", i
);
724 basename
= strrchr(link
, '/');
725 fprintf(stderr
, "%d:%s ",
726 i
, basename
? ++basename
: link
);
729 fprintf(stderr
, ")\n");
733 int monitor_loop_cnt
;
735 static int wait_and_act(struct supertype
*container
, int nowait
)
739 struct active_array
**aap
= &container
->arrays
;
740 struct active_array
*a
, **ap
;
743 static unsigned int dirty_arrays
= ~0; /* start at some non-zero value */
747 for (ap
= aap
; *ap
;) {
749 /* once an array has been deactivated we want to
750 * ask the manager to discard it.
752 if (!a
->container
|| a
->to_remove
) {
764 add_fd(&rfds
, &maxfd
, a
->info
.state_fd
);
765 add_fd(&rfds
, &maxfd
, a
->action_fd
);
766 add_fd(&rfds
, &maxfd
, a
->sync_completed_fd
);
767 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
) {
768 add_fd(&rfds
, &maxfd
, mdi
->state_fd
);
769 add_fd(&rfds
, &maxfd
, mdi
->bb_fd
);
770 add_fd(&rfds
, &maxfd
, mdi
->ubb_fd
);
776 if (manager_ready
&& (*aap
== NULL
|| (sigterm
&& !dirty_arrays
))) {
777 /* No interesting arrays, or we have been told to
778 * terminate and everything is clean. Lets see about
779 * exiting. Note that blocking at this point is not a
780 * problem as there are no active arrays, there is
781 * nothing that we need to be ready to do.
785 fd
= open_dev_excl(container
->devnm
);
787 fd
= open_dev_flags(container
->devnm
, O_RDONLY
|O_EXCL
);
788 if (fd
>= 0 || errno
!= EBUSY
) {
789 /* OK, we are safe to leave */
790 if (sigterm
&& !dirty_arrays
)
791 dprintf("caught sigterm, all clean... exiting\n");
793 dprintf("no arrays to monitor... exiting\n");
795 /* On SIGTERM, someone (the take-over mdmon) will
798 remove_pidfile(container
->devnm
);
811 if (*aap
== NULL
|| container
->retry_soon
) {
812 /* just waiting to get O_EXCL access */
814 ts
.tv_nsec
= 20000000ULL;
816 sigprocmask(SIG_UNBLOCK
, NULL
, &set
);
817 sigdelset(&set
, SIGUSR1
);
818 monitor_loop_cnt
|= 1;
819 rv
= pselect(maxfd
+1, NULL
, NULL
, &rfds
, &ts
, &set
);
820 monitor_loop_cnt
+= 1;
822 if (errno
== EINTR
) {
825 dprintf("monitor: caught signal\n");
827 dprintf("monitor: error %d in pselect\n",
832 dprint_wake_reasons(&rfds
);
834 container
->retry_soon
= 0;
838 struct metadata_update
*this;
840 for (this = update_queue
; this ; this = this->next
)
841 container
->ss
->process_update(container
, this);
843 update_queue_handled
= update_queue
;
846 container
->ss
->sync_metadata(container
);
851 for (a
= *aap
; a
; a
= a
->next
) {
853 if (a
->replaces
&& !discard_this
) {
854 struct active_array
**ap
;
855 for (ap
= &a
->next
; *ap
&& *ap
!= a
->replaces
;
860 discard_this
= a
->replaces
;
862 /* FIXME check if device->state_fd need to be cleared?*/
865 if (a
->container
&& !a
->to_remove
) {
866 int ret
= read_and_act(a
, &rfds
);
868 dirty_arrays
+= !!(ret
& ARRAY_DIRTY
);
869 /* when terminating stop manipulating the array after it
870 * is clean, but make sure read_and_act() is given a
871 * chance to handle 'active_idle'
873 if (sigterm
&& !(ret
& ARRAY_DIRTY
))
874 a
->container
= NULL
; /* stop touching this array */
875 if (ret
& ARRAY_BUSY
)
876 container
->retry_soon
= 1;
880 /* propagate failures across container members */
881 for (a
= *aap
; a
; a
= a
->next
) {
882 if (!a
->container
|| a
->to_remove
)
884 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
885 if (mdi
->curr_state
& DS_FAULTY
)
886 reconcile_failed(*aap
, mdi
);
892 void do_monitor(struct supertype
*container
)
897 rv
= wait_and_act(container
, first
);