]>
git.ipfire.org Git - thirdparty/mdadm.git/blob - monitor.c
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 static 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 kill(getpid(), SIGUSR1
);
144 /* Monitor a set of active md arrays - all of which share the
145 * same metadata - and respond to events that require
148 * New arrays are detected by another thread which allocates
149 * required memory and attaches the data structure to our list.
153 * This is detected by array_state going to 'clear' or 'inactive'.
154 * while we thought it was active.
155 * Response is to mark metadata as clean and 'clear' the array(??)
157 * array_state if 'write-pending'
158 * We mark metadata as 'dirty' then set array to 'active'.
160 * Either ignore, or mark clean, then mark metadata as clean.
163 * detected by rd-N/state reporting "faulty"
164 * mark device as 'failed' in metadata, let the kernel release the
165 * device by writing '-blocked' to rd/state, and finally write 'remove' to
166 * rd/state. Before a disk can be replaced it must be failed and removed
167 * from all container members, this will be preemptive for the other
171 * sync_action was 'resync' and becomes 'idle' and resync_start becomes
173 * Notify metadata that sync is complete.
174 * "Deal with Degraded"
177 * sync_action changes from 'recover' to 'idle'
178 * Check each device state and mark metadata if 'faulty' or 'in_sync'.
179 * "Deal with Degraded"
181 * deal with degraded array
182 * We only do this when first noticing the array is degraded.
183 * This can be when we first see the array, when sync completes or
184 * when recovery completes.
186 * Check if number of failed devices suggests recovery is needed, and
188 * Ask metadata for a spare device
189 * Add device as not in_sync and give a role
194 * This only happens on finding a new array... mdadm will have set
195 * 'resync_start' to the correct value. If 'resync_start' indicates that an
196 * resync needs to occur set the array to the 'active' state rather than the
197 * initial read-auto state.
201 * We wait for a change (poll/select) on array_state, sync_action, and
202 * each rd-X/state file.
203 * When we get any change, we check everything. So read each state file,
204 * then decide what to do.
206 * The core action is to write new metadata to all devices in the array.
207 * This is done at most once on any wakeup.
208 * After that we might:
209 * - update the array_state
210 * - set the role of some devices.
211 * - request a sync_action
215 static int read_and_act(struct active_array
*a
)
221 a
->next_state
= bad_word
;
222 a
->next_action
= bad_action
;
224 a
->curr_state
= read_state(a
->info
.state_fd
);
225 a
->curr_action
= read_action(a
->action_fd
);
226 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
) {
228 if (mdi
->state_fd
> 0)
229 mdi
->curr_state
= read_dev_state(mdi
->state_fd
);
232 if (a
->curr_state
<= inactive
&&
233 a
->prev_state
> inactive
) {
234 /* array has been stopped */
235 a
->container
->ss
->set_array_state(a
, 1);
236 a
->next_state
= clear
;
239 if (a
->curr_state
== write_pending
) {
241 a
->container
->ss
->set_array_state(a
, 0);
242 a
->next_state
= active
;
244 if (a
->curr_state
== active_idle
) {
245 /* Set array to 'clean' FIRST, then
246 * a->ss->mark_clean(a, ~0ULL);
247 * just ignore for now.
251 if (a
->curr_state
== readonly
) {
252 /* Well, I'm ready to handle things, so
253 * read-auto is OK. FIXME what if we really want
257 printf("Found a readonly array at %llu\n", a
->resync_start
);
258 if (a
->resync_start
== ~0ULL)
259 a
->next_state
= read_auto
; /* array is clean */
261 a
->container
->ss
->set_array_state(a
, 0);
262 a
->next_state
= active
;
266 if (a
->curr_action
== idle
&&
267 a
->prev_action
== resync
) {
268 /* A resync has finished. The endpoint is recorded in
269 * 'sync_start'. We don't update the metadata
270 * until the array goes inactive or readonly though.
271 * Just check if we need to fiddle spares.
274 a
->container
->ss
->set_array_state(a
, 0);
278 if (a
->curr_action
== idle
&&
279 a
->prev_action
== recover
) {
280 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
) {
281 a
->container
->ss
->set_disk(a
, mdi
->disk
.raid_disk
,
283 if (! (mdi
->curr_state
& DS_INSYNC
))
289 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
) {
290 if (mdi
->curr_state
& DS_FAULTY
) {
291 a
->container
->ss
->set_disk(a
, mdi
->disk
.raid_disk
,
294 mdi
->next_state
= DS_REMOVE
;
298 if (check_degraded
) {
302 a
->container
->ss
->sync_metadata(a
);
304 /* Effect state changes in the array */
305 if (a
->next_state
!= bad_word
)
306 write_attr(array_states
[a
->next_state
], a
->info
.state_fd
);
307 if (a
->next_action
!= bad_action
)
308 write_attr(sync_actions
[a
->next_action
], a
->action_fd
);
309 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
) {
310 if (mdi
->next_state
== DS_REMOVE
&& mdi
->state_fd
> 0) {
313 write_attr("-blocked", mdi
->state_fd
);
314 /* the kernel may not be able to immediately remove the
315 * disk, we can simply wait until the next event to try
318 remove_err
= write_attr("remove", mdi
->state_fd
);
320 close(mdi
->state_fd
);
324 if (mdi
->next_state
& DS_INSYNC
)
325 write_attr("+in_sync", mdi
->state_fd
);
328 /* move curr_ to prev_ */
329 a
->prev_state
= a
->curr_state
;
331 a
->prev_action
= a
->curr_action
;
333 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
) {
334 mdi
->prev_state
= mdi
->curr_state
;
344 static struct mdinfo
*
345 find_device(struct active_array
*a
, int major
, int minor
)
349 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
350 if (mdi
->disk
.major
== major
&& mdi
->disk
.minor
== minor
)
356 static void reconcile_failed(struct active_array
*aa
, struct mdinfo
*failed
)
358 struct active_array
*a
;
359 struct mdinfo
*victim
;
361 for (a
= aa
; a
; a
= a
->next
) {
364 victim
= find_device(a
, failed
->disk
.major
, failed
->disk
.minor
);
368 if (!(victim
->curr_state
& DS_FAULTY
))
369 write_attr("faulty", victim
->state_fd
);
373 static int handle_remove_device(struct md_remove_device_cmd
*cmd
, struct active_array
*aa
)
375 struct active_array
*a
;
376 struct mdinfo
*victim
;
379 /* scan all arrays for the given device, if ->state_fd is closed (-1)
380 * in all cases then mark the disk as removed in the metadata.
381 * Otherwise reply that it is busy.
384 /* pass1 check that it is not in use anywhere */
385 /* note: we are safe from re-adds as long as the device exists in the
388 for (a
= aa
; a
; a
= a
->next
) {
391 victim
= find_device(a
, major(cmd
->rdev
), minor(cmd
->rdev
));
394 if (victim
->state_fd
> 0)
398 /* pass2 schedule and process removal per array */
399 for (a
= aa
; a
; a
= a
->next
) {
402 victim
= find_device(a
, major(cmd
->rdev
), minor(cmd
->rdev
));
405 victim
->curr_state
|= DS_REMOVE
;
406 rv
= read_and_act(a
);
414 static int handle_pipe(struct md_generic_cmd
*cmd
, struct active_array
*aa
)
416 switch (cmd
->action
) {
417 case md_action_ping_monitor
:
419 case md_action_remove_device
:
420 return handle_remove_device((void *) cmd
, aa
);
426 static int wait_and_act(struct supertype
*container
, int pfd
,
427 int monfd
, int nowait
)
431 struct active_array
**aap
= &container
->arrays
;
432 struct active_array
*a
, **ap
;
438 add_fd(&rfds
, &maxfd
, pfd
);
439 for (ap
= aap
; *ap
;) {
441 /* once an array has been deactivated we want to
442 * ask the manager to discard it.
456 add_fd(&rfds
, &maxfd
, a
->info
.state_fd
);
457 add_fd(&rfds
, &maxfd
, a
->action_fd
);
458 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
459 add_fd(&rfds
, &maxfd
, mdi
->state_fd
);
464 if (manager_ready
&& *aap
== NULL
) {
465 /* No interesting arrays. Lets see about exiting.
466 * Note that blocking at this point is not a problem
467 * as there are no active arrays, there is nothing that
468 * we need to be ready to do.
470 int fd
= open(container
->device_name
, O_RDONLY
|O_EXCL
);
471 if (fd
>= 0 || errno
!= EBUSY
) {
472 /* OK, we are safe to leave */
475 remove_pidfile(container
->devname
);
481 rv
= select(maxfd
+1, &rfds
, NULL
, NULL
, NULL
);
486 if (FD_ISSET(pfd
, &rfds
)) {
489 if (read(pfd
, &err
, 1) > 0)
490 err
= handle_pipe(active_cmd
, *aap
);
491 write(monfd
, &err
, 1);
495 for (a
= *aap
; a
; a
= a
->next
) {
496 if (a
->replaces
&& !discard_this
) {
497 struct active_array
**ap
;
498 for (ap
= &a
->next
; *ap
&& *ap
!= a
->replaces
;
503 discard_this
= a
->replaces
;
508 rv
+= read_and_act(a
);
511 /* propagate failures across container members */
512 for (a
= *aap
; a
; a
= a
->next
) {
515 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
516 if (mdi
->curr_state
& DS_FAULTY
)
517 reconcile_failed(*aap
, mdi
);
523 void do_monitor(struct supertype
*container
)
528 rv
= wait_and_act(container
, container
->mgr_pipe
[0],
529 container
->mon_pipe
[1], first
);