]>
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);
51 int get_resync_start(struct active_array
*a
)
56 n
= read_attr(buf
, 30, a
->resync_start_fd
);
60 a
->resync_start
= strtoull(buf
, NULL
, 10);
65 static int attr_match(const char *attr
, const char *str
)
67 /* See if attr, read from a sysfs file, matches
68 * str. They must either be the same, or attr can
69 * have a trailing newline or comma
71 while (*attr
&& *str
&& *attr
== *str
) {
76 if (*str
|| (*attr
&& *attr
!= ',' && *attr
!= '\n'))
81 static int match_word(const char *word
, char **list
)
84 for (n
=0; list
[n
]; n
++)
85 if (attr_match(word
, list
[n
]))
90 static enum array_state
read_state(int fd
)
93 int n
= read_attr(buf
, 20, fd
);
97 return (enum array_state
) match_word(buf
, array_states
);
100 static enum sync_action
read_action( int fd
)
103 int n
= read_attr(buf
, 20, fd
);
107 return (enum sync_action
) match_word(buf
, sync_actions
);
110 int read_dev_state(int fd
)
113 int n
= read_attr(buf
, 60, fd
);
122 if (attr_match(cp
, "faulty"))
124 if (attr_match(cp
, "in_sync"))
126 if (attr_match(cp
, "write_mostly"))
127 rv
|= DS_WRITE_MOSTLY
;
128 if (attr_match(cp
, "spare"))
130 if (attr_match(cp
, "blocked"))
132 cp
= strchr(cp
, ',');
139 static void signal_manager(void)
141 /* tgkill(getpid(), mon_tid, SIGUSR1); */
143 syscall(SYS_tgkill
, pid
, mgr_tid
, SIGUSR1
);
146 /* Monitor a set of active md arrays - all of which share the
147 * same metadata - and respond to events that require
150 * New arrays are detected by another thread which allocates
151 * required memory and attaches the data structure to our list.
155 * This is detected by array_state going to 'clear' or 'inactive'.
156 * while we thought it was active.
157 * Response is to mark metadata as clean and 'clear' the array(??)
159 * array_state if 'write-pending'
160 * We mark metadata as 'dirty' then set array to 'active'.
162 * Either ignore, or mark clean, then mark metadata as clean.
165 * detected by rd-N/state reporting "faulty"
166 * mark device as 'failed' in metadata, let the kernel release the
167 * device by writing '-blocked' to rd/state, and finally write 'remove' to
168 * rd/state. Before a disk can be replaced it must be failed and removed
169 * from all container members, this will be preemptive for the other
173 * sync_action was 'resync' and becomes 'idle' and resync_start becomes
175 * Notify metadata that sync is complete.
178 * sync_action changes from 'recover' to 'idle'
179 * Check each device state and mark metadata if 'faulty' or 'in_sync'.
182 * This only happens on finding a new array... mdadm will have set
183 * 'resync_start' to the correct value. If 'resync_start' indicates that an
184 * resync needs to occur set the array to the 'active' state rather than the
185 * initial read-auto state.
189 * We wait for a change (poll/select) on array_state, sync_action, and
190 * each rd-X/state file.
191 * When we get any change, we check everything. So read each state file,
192 * then decide what to do.
194 * The core action is to write new metadata to all devices in the array.
195 * This is done at most once on any wakeup.
196 * After that we might:
197 * - update the array_state
198 * - set the role of some devices.
199 * - request a sync_action
203 static int read_and_act(struct active_array
*a
)
205 int check_degraded
= 0;
209 a
->next_state
= bad_word
;
210 a
->next_action
= bad_action
;
212 a
->curr_state
= read_state(a
->info
.state_fd
);
213 a
->curr_action
= read_action(a
->action_fd
);
214 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
) {
216 if (mdi
->state_fd
>= 0)
217 mdi
->curr_state
= read_dev_state(mdi
->state_fd
);
220 if (a
->curr_state
<= inactive
&&
221 a
->prev_state
> inactive
) {
222 /* array has been stopped */
224 a
->container
->ss
->set_array_state(a
, 1);
225 a
->next_state
= clear
;
228 if (a
->curr_state
== write_pending
) {
230 a
->container
->ss
->set_array_state(a
, 0);
231 a
->next_state
= active
;
233 if (a
->curr_state
== active_idle
) {
234 /* Set array to 'clean' FIRST, then
235 * a->ss->mark_clean(a, ~0ULL);
236 * just ignore for now.
240 if (a
->curr_state
== readonly
) {
241 /* Well, I'm ready to handle things, so
242 * read-auto is OK. FIXME what if we really want
246 if (a
->container
->ss
->set_array_state(a
, 2))
247 a
->next_state
= read_auto
; /* array is clean */
249 a
->next_state
= active
; /* Now active for recovery etc */
253 a
->curr_action
== idle
&&
254 a
->prev_action
== resync
) {
255 /* A resync has finished. The endpoint is recorded in
256 * 'sync_start'. We don't update the metadata
257 * until the array goes inactive or readonly though.
258 * Just check if we need to fiddle spares.
261 a
->container
->ss
->set_array_state(a
, 0);
266 a
->curr_action
== idle
&&
267 a
->prev_action
== recover
) {
268 /* A recovery has finished. Some disks may be in sync now,
269 * and the array may no longer be degraded
271 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
) {
272 a
->container
->ss
->set_disk(a
, mdi
->disk
.raid_disk
,
274 if (! (mdi
->curr_state
& DS_INSYNC
))
279 /* Check for failures and if found:
280 * 1/ Record the failure in the metadata and unblock the device.
281 * FIXME update the kernel to stop notifying on failed drives when
282 * the array is readonly and we have cleared 'blocked'
283 * 2/ Try to remove the device if the array is writable, or can be
286 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
) {
287 if (mdi
->curr_state
& DS_FAULTY
) {
288 a
->container
->ss
->set_disk(a
, mdi
->disk
.raid_disk
,
291 mdi
->next_state
|= DS_UNBLOCK
;
292 if (a
->curr_state
== read_auto
) {
293 a
->container
->ss
->set_array_state(a
, 0);
294 a
->next_state
= active
;
296 if (a
->curr_state
> readonly
)
297 mdi
->next_state
|= DS_REMOVE
;
301 a
->container
->ss
->sync_metadata(a
->container
);
302 dprintf("%s: update[%d]: (", __func__
, a
->info
.container_member
);
304 /* Effect state changes in the array */
305 if (a
->next_state
!= bad_word
) {
306 dprintf(" state:%s", array_states
[a
->next_state
]);
307 write_attr(array_states
[a
->next_state
], a
->info
.state_fd
);
309 if (a
->next_action
!= bad_action
) {
310 write_attr(sync_actions
[a
->next_action
], a
->action_fd
);
311 dprintf(" action:%s", array_states
[a
->next_state
]);
313 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
) {
314 if (mdi
->next_state
& DS_UNBLOCK
) {
315 dprintf(" %d:-blocked", mdi
->disk
.raid_disk
);
316 write_attr("-blocked", mdi
->state_fd
);
319 if ((mdi
->next_state
& DS_REMOVE
) && mdi
->state_fd
>= 0) {
322 /* the kernel may not be able to immediately remove the
323 * disk, we can simply wait until the next event to try
326 remove_result
= write_attr("remove", mdi
->state_fd
);
327 if (remove_result
> 0) {
328 dprintf(" %d:removed", mdi
->disk
.raid_disk
);
329 close(mdi
->state_fd
);
333 if (mdi
->next_state
& DS_INSYNC
) {
334 write_attr("+in_sync", mdi
->state_fd
);
335 dprintf(" %d:+in_sync", mdi
->disk
.raid_disk
);
340 /* move curr_ to prev_ */
341 a
->prev_state
= a
->curr_state
;
343 a
->prev_action
= a
->curr_action
;
345 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
) {
346 mdi
->prev_state
= mdi
->curr_state
;
350 if (check_degraded
) {
351 /* manager will do the actual check */
352 a
->check_degraded
= 1;
362 static struct mdinfo
*
363 find_device(struct active_array
*a
, int major
, int minor
)
367 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
368 if (mdi
->disk
.major
== major
&& mdi
->disk
.minor
== minor
)
374 static void reconcile_failed(struct active_array
*aa
, struct mdinfo
*failed
)
376 struct active_array
*a
;
377 struct mdinfo
*victim
;
379 for (a
= aa
; a
; a
= a
->next
) {
382 victim
= find_device(a
, failed
->disk
.major
, failed
->disk
.minor
);
386 if (!(victim
->curr_state
& DS_FAULTY
))
387 write_attr("faulty", victim
->state_fd
);
392 static void dprint_wake_reasons(fd_set
*fds
)
400 fprintf(stderr
, "monitor: wake ( ");
401 for (i
= 0; i
< FD_SETSIZE
; i
++) {
402 if (FD_ISSET(i
, fds
)) {
403 sprintf(proc_path
, "/proc/%d/fd/%d",
406 rv
= readlink(proc_path
, link
, sizeof(link
) - 1);
408 fprintf(stderr
, "%d:unknown ", i
);
412 basename
= strrchr(link
, '/');
413 fprintf(stderr
, "%d:%s ",
414 i
, basename
? ++basename
: link
);
417 fprintf(stderr
, ")\n");
421 int monitor_loop_cnt
;
423 static int wait_and_act(struct supertype
*container
, int nowait
)
427 struct active_array
**aap
= &container
->arrays
;
428 struct active_array
*a
, **ap
;
434 for (ap
= aap
; *ap
;) {
436 /* once an array has been deactivated we want to
437 * ask the manager to discard it.
451 add_fd(&rfds
, &maxfd
, a
->info
.state_fd
);
452 add_fd(&rfds
, &maxfd
, a
->action_fd
);
453 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
454 add_fd(&rfds
, &maxfd
, mdi
->state_fd
);
459 if (manager_ready
&& *aap
== NULL
) {
460 /* No interesting arrays. Lets see about exiting.
461 * Note that blocking at this point is not a problem
462 * as there are no active arrays, there is nothing that
463 * we need to be ready to do.
465 int fd
= open(container
->device_name
, O_RDONLY
|O_EXCL
);
466 if (fd
>= 0 || errno
!= EBUSY
) {
467 /* OK, we are safe to leave */
468 dprintf("no arrays to monitor... exiting\n");
469 remove_pidfile(container
->devname
);
478 sigprocmask(SIG_UNBLOCK
, NULL
, &set
);
479 sigdelset(&set
, SIGUSR1
);
480 monitor_loop_cnt
|= 1;
481 rv
= pselect(maxfd
+1, &rfds
, NULL
, NULL
, NULL
, &set
);
482 monitor_loop_cnt
+= 1;
483 if (rv
== -1 && errno
== EINTR
)
486 dprint_wake_reasons(&rfds
);
492 struct metadata_update
*this;
494 for (this = update_queue
; this ; this = this->next
)
495 container
->ss
->process_update(container
, this);
497 update_queue_handled
= update_queue
;
500 container
->ss
->sync_metadata(container
);
503 for (a
= *aap
; a
; a
= a
->next
) {
504 if (a
->replaces
&& !discard_this
) {
505 struct active_array
**ap
;
506 for (ap
= &a
->next
; *ap
&& *ap
!= a
->replaces
;
511 discard_this
= a
->replaces
;
513 /* FIXME check if device->state_fd need to be cleared?*/
517 rv
+= read_and_act(a
);
520 /* propagate failures across container members */
521 for (a
= *aap
; a
; a
= a
->next
) {
524 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
525 if (mdi
->curr_state
& DS_FAULTY
)
526 reconcile_failed(*aap
, mdi
);
532 void do_monitor(struct supertype
*container
)
537 rv
= wait_and_act(container
, first
);