]>
git.ipfire.org Git - thirdparty/mdadm.git/blob - monitor.c
4 #include <sys/syscall.h>
5 #include <sys/select.h>
8 static char *array_states
[] = {
9 "clear", "inactive", "suspended", "readonly", "read-auto",
10 "clean", "active", "write-pending", "active-idle", NULL
};
11 static char *sync_actions
[] = {
12 "idle", "reshape", "resync", "recover", "check", "repair", NULL
15 static int write_attr(char *attr
, int fd
)
17 return write(fd
, attr
, strlen(attr
));
20 static void add_fd(fd_set
*fds
, int *maxfd
, int fd
)
29 static int read_attr(char *buf
, int len
, int fd
)
38 n
= read(fd
, buf
, len
- 1);
50 int get_resync_start(struct active_array
*a
)
55 n
= read_attr(buf
, 30, a
->resync_start_fd
);
59 a
->resync_start
= strtoull(buf
, NULL
, 10);
65 static enum array_state
read_state(int fd
)
68 int n
= read_attr(buf
, 20, fd
);
72 return (enum array_state
) sysfs_match_word(buf
, array_states
);
75 static enum sync_action
read_action( int fd
)
78 int n
= read_attr(buf
, 20, fd
);
82 return (enum sync_action
) sysfs_match_word(buf
, sync_actions
);
85 int read_dev_state(int fd
)
88 int n
= read_attr(buf
, 60, fd
);
97 if (sysfs_attr_match(cp
, "faulty"))
99 if (sysfs_attr_match(cp
, "in_sync"))
101 if (sysfs_attr_match(cp
, "write_mostly"))
102 rv
|= DS_WRITE_MOSTLY
;
103 if (sysfs_attr_match(cp
, "spare"))
105 if (sysfs_attr_match(cp
, "blocked"))
107 cp
= strchr(cp
, ',');
114 static void signal_manager(void)
116 /* tgkill(getpid(), mon_tid, SIGUSR1); */
118 syscall(SYS_tgkill
, pid
, mgr_tid
, SIGUSR1
);
121 /* Monitor a set of active md arrays - all of which share the
122 * same metadata - and respond to events that require
125 * New arrays are detected by another thread which allocates
126 * required memory and attaches the data structure to our list.
130 * This is detected by array_state going to 'clear' or 'inactive'.
131 * while we thought it was active.
132 * Response is to mark metadata as clean and 'clear' the array(??)
134 * array_state if 'write-pending'
135 * We mark metadata as 'dirty' then set array to 'active'.
137 * Either ignore, or mark clean, then mark metadata as clean.
140 * detected by rd-N/state reporting "faulty"
141 * mark device as 'failed' in metadata, let the kernel release the
142 * device by writing '-blocked' to rd/state, and finally write 'remove' to
143 * rd/state. Before a disk can be replaced it must be failed and removed
144 * from all container members, this will be preemptive for the other
148 * sync_action was 'resync' and becomes 'idle' and resync_start becomes
150 * Notify metadata that sync is complete.
153 * sync_action changes from 'recover' to 'idle'
154 * Check each device state and mark metadata if 'faulty' or 'in_sync'.
157 * This only happens on finding a new array... mdadm will have set
158 * 'resync_start' to the correct value. If 'resync_start' indicates that an
159 * resync needs to occur set the array to the 'active' state rather than the
160 * initial read-auto state.
164 * We wait for a change (poll/select) on array_state, sync_action, and
165 * each rd-X/state file.
166 * When we get any change, we check everything. So read each state file,
167 * then decide what to do.
169 * The core action is to write new metadata to all devices in the array.
170 * This is done at most once on any wakeup.
171 * After that we might:
172 * - update the array_state
173 * - set the role of some devices.
174 * - request a sync_action
178 static int read_and_act(struct active_array
*a
)
180 int check_degraded
= 0;
184 a
->next_state
= bad_word
;
185 a
->next_action
= bad_action
;
187 a
->curr_state
= read_state(a
->info
.state_fd
);
188 a
->curr_action
= read_action(a
->action_fd
);
189 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
) {
191 if (mdi
->state_fd
>= 0)
192 mdi
->curr_state
= read_dev_state(mdi
->state_fd
);
195 if (a
->curr_state
<= inactive
&&
196 a
->prev_state
> inactive
) {
197 /* array has been stopped */
199 a
->container
->ss
->set_array_state(a
, 1);
200 a
->next_state
= clear
;
203 if (a
->curr_state
== write_pending
) {
205 a
->container
->ss
->set_array_state(a
, 0);
206 a
->next_state
= active
;
208 if (a
->curr_state
== active_idle
) {
209 /* Set array to 'clean' FIRST, then mark clean
212 a
->next_state
= clean
;
214 if (a
->curr_state
== clean
) {
216 a
->container
->ss
->set_array_state(a
, 1);
219 if (a
->curr_state
== readonly
) {
220 /* Well, I'm ready to handle things. If readonly
221 * wasn't requested, transition to read-auto.
224 read_attr(buf
, sizeof(buf
), a
->metadata_fd
);
225 if (strncmp(buf
, "external:-", 10) == 0) {
226 /* explicit request for readonly array. Leave it alone */
230 if (a
->container
->ss
->set_array_state(a
, 2))
231 a
->next_state
= read_auto
; /* array is clean */
233 a
->next_state
= active
; /* Now active for recovery etc */
238 a
->curr_action
== idle
&&
239 a
->prev_action
== resync
) {
240 /* A resync has finished. The endpoint is recorded in
241 * 'sync_start'. We don't update the metadata
242 * until the array goes inactive or readonly though.
243 * Just check if we need to fiddle spares.
246 a
->container
->ss
->set_array_state(a
, a
->curr_state
<= clean
);
251 a
->curr_action
== idle
&&
252 a
->prev_action
== recover
) {
253 /* A recovery has finished. Some disks may be in sync now,
254 * and the array may no longer be degraded
256 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
) {
257 a
->container
->ss
->set_disk(a
, mdi
->disk
.raid_disk
,
259 if (! (mdi
->curr_state
& DS_INSYNC
))
264 /* Check for failures and if found:
265 * 1/ Record the failure in the metadata and unblock the device.
266 * FIXME update the kernel to stop notifying on failed drives when
267 * the array is readonly and we have cleared 'blocked'
268 * 2/ Try to remove the device if the array is writable, or can be
271 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
) {
272 if (mdi
->curr_state
& DS_FAULTY
) {
273 a
->container
->ss
->set_disk(a
, mdi
->disk
.raid_disk
,
276 mdi
->next_state
|= DS_UNBLOCK
;
277 if (a
->curr_state
== read_auto
) {
278 a
->container
->ss
->set_array_state(a
, 0);
279 a
->next_state
= active
;
281 if (a
->curr_state
> readonly
)
282 mdi
->next_state
|= DS_REMOVE
;
286 a
->container
->ss
->sync_metadata(a
->container
);
287 dprintf("%s(%d): state:%s action:%s next(", __func__
, a
->info
.container_member
,
288 array_states
[a
->curr_state
], sync_actions
[a
->curr_action
]);
290 /* Effect state changes in the array */
291 if (a
->next_state
!= bad_word
) {
292 dprintf(" state:%s", array_states
[a
->next_state
]);
293 write_attr(array_states
[a
->next_state
], a
->info
.state_fd
);
295 if (a
->next_action
!= bad_action
) {
296 write_attr(sync_actions
[a
->next_action
], a
->action_fd
);
297 dprintf(" action:%s", sync_actions
[a
->next_action
]);
299 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
) {
300 if (mdi
->next_state
& DS_UNBLOCK
) {
301 dprintf(" %d:-blocked", mdi
->disk
.raid_disk
);
302 write_attr("-blocked", mdi
->state_fd
);
305 if ((mdi
->next_state
& DS_REMOVE
) && mdi
->state_fd
>= 0) {
308 /* the kernel may not be able to immediately remove the
309 * disk, we can simply wait until the next event to try
312 remove_result
= write_attr("remove", mdi
->state_fd
);
313 if (remove_result
> 0) {
314 dprintf(" %d:removed", mdi
->disk
.raid_disk
);
315 close(mdi
->state_fd
);
319 if (mdi
->next_state
& DS_INSYNC
) {
320 write_attr("+in_sync", mdi
->state_fd
);
321 dprintf(" %d:+in_sync", mdi
->disk
.raid_disk
);
326 /* move curr_ to prev_ */
327 a
->prev_state
= a
->curr_state
;
329 a
->prev_action
= a
->curr_action
;
331 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
) {
332 mdi
->prev_state
= mdi
->curr_state
;
336 if (check_degraded
) {
337 /* manager will do the actual check */
338 a
->check_degraded
= 1;
348 static struct mdinfo
*
349 find_device(struct active_array
*a
, int major
, int minor
)
353 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
354 if (mdi
->disk
.major
== major
&& mdi
->disk
.minor
== minor
)
360 static void reconcile_failed(struct active_array
*aa
, struct mdinfo
*failed
)
362 struct active_array
*a
;
363 struct mdinfo
*victim
;
365 for (a
= aa
; a
; a
= a
->next
) {
368 victim
= find_device(a
, failed
->disk
.major
, failed
->disk
.minor
);
372 if (!(victim
->curr_state
& DS_FAULTY
))
373 write_attr("faulty", victim
->state_fd
);
378 static void dprint_wake_reasons(fd_set
*fds
)
386 fprintf(stderr
, "monitor: wake ( ");
387 for (i
= 0; i
< FD_SETSIZE
; i
++) {
388 if (FD_ISSET(i
, fds
)) {
389 sprintf(proc_path
, "/proc/%d/fd/%d",
392 rv
= readlink(proc_path
, link
, sizeof(link
) - 1);
394 fprintf(stderr
, "%d:unknown ", i
);
398 basename
= strrchr(link
, '/');
399 fprintf(stderr
, "%d:%s ",
400 i
, basename
? ++basename
: link
);
403 fprintf(stderr
, ")\n");
407 int monitor_loop_cnt
;
409 static int wait_and_act(struct supertype
*container
, int nowait
)
413 struct active_array
**aap
= &container
->arrays
;
414 struct active_array
*a
, **ap
;
420 for (ap
= aap
; *ap
;) {
422 /* once an array has been deactivated we want to
423 * ask the manager to discard it.
437 add_fd(&rfds
, &maxfd
, a
->info
.state_fd
);
438 add_fd(&rfds
, &maxfd
, a
->action_fd
);
439 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
440 add_fd(&rfds
, &maxfd
, mdi
->state_fd
);
445 if (manager_ready
&& *aap
== NULL
) {
446 /* No interesting arrays. Lets see about exiting.
447 * Note that blocking at this point is not a problem
448 * as there are no active arrays, there is nothing that
449 * we need to be ready to do.
451 int fd
= open(container
->device_name
, O_RDONLY
|O_EXCL
);
452 if (fd
>= 0 || errno
!= EBUSY
) {
453 /* OK, we are safe to leave */
454 dprintf("no arrays to monitor... exiting\n");
455 remove_pidfile(container
->devname
);
464 sigprocmask(SIG_UNBLOCK
, NULL
, &set
);
465 sigdelset(&set
, SIGUSR1
);
466 monitor_loop_cnt
|= 1;
467 rv
= pselect(maxfd
+1, &rfds
, NULL
, NULL
, NULL
, &set
);
468 monitor_loop_cnt
+= 1;
469 if (rv
== -1 && errno
== EINTR
)
472 dprint_wake_reasons(&rfds
);
478 struct metadata_update
*this;
480 for (this = update_queue
; this ; this = this->next
)
481 container
->ss
->process_update(container
, this);
483 update_queue_handled
= update_queue
;
486 container
->ss
->sync_metadata(container
);
489 for (a
= *aap
; a
; a
= a
->next
) {
490 if (a
->replaces
&& !discard_this
) {
491 struct active_array
**ap
;
492 for (ap
= &a
->next
; *ap
&& *ap
!= a
->replaces
;
497 discard_this
= a
->replaces
;
499 /* FIXME check if device->state_fd need to be cleared?*/
503 rv
+= read_and_act(a
);
506 /* propagate failures across container members */
507 for (a
= *aap
; a
; a
= a
->next
) {
510 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
511 if (mdi
->curr_state
& DS_FAULTY
)
512 reconcile_failed(*aap
, mdi
);
518 void do_monitor(struct supertype
*container
)
523 rv
= wait_and_act(container
, first
);