]>
git.ipfire.org Git - thirdparty/mdadm.git/blob - monitor.c
a7e530801dd03006441acb2722d473bbb878e1d2
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 static int get_sync_pos(struct active_array
*a
)
55 n
= read_attr(buf
, 30, a
->sync_pos_fd
);
59 if (strncmp(buf
, "max", 3) == 0) {
60 a
->sync_pos
= ~(unsigned long long)0;
63 a
->sync_pos
= strtoull(buf
, NULL
, 10);
67 static int get_resync_start(struct active_array
*a
)
72 n
= read_attr(buf
, 30, a
->resync_start_fd
);
76 a
->resync_start
= strtoull(buf
, NULL
, 10);
81 static int attr_match(const char *attr
, const char *str
)
83 /* See if attr, read from a sysfs file, matches
84 * str. They must either be the same, or attr can
85 * have a trailing newline or comma
87 while (*attr
&& *str
&& *attr
== *str
) {
92 if (*str
|| (*attr
&& *attr
!= ',' && *attr
!= '\n'))
97 static int match_word(const char *word
, char **list
)
100 for (n
=0; list
[n
]; n
++)
101 if (attr_match(word
, list
[n
]))
106 static enum array_state
read_state(int fd
)
109 int n
= read_attr(buf
, 20, fd
);
113 return (enum array_state
) match_word(buf
, array_states
);
116 static enum sync_action
read_action( int fd
)
119 int n
= read_attr(buf
, 20, fd
);
123 return (enum sync_action
) match_word(buf
, sync_actions
);
126 int read_dev_state(int fd
)
129 int n
= read_attr(buf
, 60, fd
);
138 if (attr_match(cp
, "faulty"))
140 if (attr_match(cp
, "in_sync"))
142 if (attr_match(cp
, "write_mostly"))
143 rv
|= DS_WRITE_MOSTLY
;
144 if (attr_match(cp
, "spare"))
146 if (attr_match(cp
, "blocked"))
148 cp
= strchr(cp
, ',');
155 static void signal_manager(void)
157 kill(getpid(), SIGUSR1
);
160 /* Monitor a set of active md arrays - all of which share the
161 * same metadata - and respond to events that require
164 * New arrays are detected by another thread which allocates
165 * required memory and attaches the data structure to our list.
169 * This is detected by array_state going to 'clear' or 'inactive'.
170 * while we thought it was active.
171 * Response is to mark metadata as clean and 'clear' the array(??)
173 * array_state if 'write-pending'
174 * We mark metadata as 'dirty' then set array to 'active'.
176 * Either ignore, or mark clean, then mark metadata as clean.
179 * detected by rd-N/state reporting "faulty"
180 * mark device as 'failed' in metadata, let the kernel release the
181 * device by writing '-blocked' to rd/state, and finally write 'remove' to
182 * rd/state. Before a disk can be replaced it must be failed and removed
183 * from all container members, this will be preemptive for the other
187 * sync_action was 'resync' and becomes 'idle' and resync_start becomes
189 * Notify metadata that sync is complete.
190 * "Deal with Degraded"
193 * sync_action changes from 'recover' to 'idle'
194 * Check each device state and mark metadata if 'faulty' or 'in_sync'.
195 * "Deal with Degraded"
197 * deal with degraded array
198 * We only do this when first noticing the array is degraded.
199 * This can be when we first see the array, when sync completes or
200 * when recovery completes.
202 * Check if number of failed devices suggests recovery is needed, and
204 * Ask metadata for a spare device
205 * Add device as not in_sync and give a role
210 * This only happens on finding a new array... mdadm will have set
211 * 'resync_start' to the correct value. If 'resync_start' indicates that an
212 * resync needs to occur set the array to the 'active' state rather than the
213 * initial read-auto state.
217 * We wait for a change (poll/select) on array_state, sync_action, and
218 * each rd-X/state file.
219 * When we get any change, we check everything. So read each state file,
220 * then decide what to do.
222 * The core action is to write new metadata to all devices in the array.
223 * This is done at most once on any wakeup.
224 * After that we might:
225 * - update the array_state
226 * - set the role of some devices.
227 * - request a sync_action
231 static int read_and_act(struct active_array
*a
)
237 a
->next_state
= bad_word
;
238 a
->next_action
= bad_action
;
240 a
->curr_state
= read_state(a
->info
.state_fd
);
241 a
->curr_action
= read_action(a
->action_fd
);
242 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
) {
244 if (mdi
->state_fd
> 0)
245 mdi
->curr_state
= read_dev_state(mdi
->state_fd
);
248 if (a
->curr_state
<= inactive
&&
249 a
->prev_state
> inactive
) {
250 /* array has been stopped */
252 a
->container
->ss
->mark_clean(a
, a
->sync_pos
);
253 a
->next_state
= clear
;
256 if (a
->curr_state
== write_pending
) {
257 a
->container
->ss
->mark_clean(a
, 0);
258 a
->next_state
= active
;
260 if (a
->curr_state
== active_idle
) {
261 /* Set array to 'clean' FIRST, then
262 * a->ss->mark_clean(a, ~0ULL);
263 * just ignore for now.
267 if (a
->curr_state
== readonly
) {
268 /* Well, I'm ready to handle things, so
269 * read-auto is OK. FIXME what if we really want
273 if (a
->resync_start
== ~0ULL)
274 a
->next_state
= read_auto
; /* array is clean */
276 a
->container
->ss
->mark_clean(a
, 0);
277 a
->next_state
= active
;
281 if (a
->curr_action
== idle
&&
282 a
->prev_action
== resync
) {
283 /* A resync has finished. The endpoint is recorded in
284 * 'sync_start'. We don't update the metadata
285 * until the array goes inactive or readonly though.
286 * Just check if we need to fiddle spares.
291 if (a
->curr_action
== idle
&&
292 a
->prev_action
== recover
) {
293 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
) {
294 a
->container
->ss
->set_disk(a
, mdi
->disk
.raid_disk
,
296 if (! (mdi
->curr_state
& DS_INSYNC
))
302 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
) {
303 if (mdi
->curr_state
& DS_FAULTY
) {
304 a
->container
->ss
->set_disk(a
, mdi
->disk
.raid_disk
,
307 mdi
->next_state
= DS_REMOVE
;
311 if (check_degraded
) {
315 a
->container
->ss
->sync_metadata(a
);
317 /* Effect state changes in the array */
318 if (a
->next_state
!= bad_word
)
319 write_attr(array_states
[a
->next_state
], a
->info
.state_fd
);
320 if (a
->next_action
!= bad_action
)
321 write_attr(sync_actions
[a
->next_action
], a
->action_fd
);
322 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
) {
323 if (mdi
->next_state
== DS_REMOVE
&& mdi
->state_fd
> 0) {
326 write_attr("-blocked", mdi
->state_fd
);
327 /* the kernel may not be able to immediately remove the
328 * disk, we can simply wait until the next event to try
331 remove_err
= write_attr("remove", mdi
->state_fd
);
333 close(mdi
->state_fd
);
337 if (mdi
->next_state
& DS_INSYNC
)
338 write_attr("+in_sync", mdi
->state_fd
);
341 /* move curr_ to prev_ */
342 a
->prev_state
= a
->curr_state
;
344 a
->prev_action
= a
->curr_action
;
346 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
) {
347 mdi
->prev_state
= mdi
->curr_state
;
357 static struct mdinfo
*
358 find_device(struct active_array
*a
, int major
, int minor
)
362 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
363 if (mdi
->disk
.major
== major
&& mdi
->disk
.minor
== minor
)
369 static void reconcile_failed(struct active_array
*aa
, struct mdinfo
*failed
)
371 struct active_array
*a
;
372 struct mdinfo
*victim
;
374 for (a
= aa
; a
; a
= a
->next
) {
377 victim
= find_device(a
, failed
->disk
.major
, failed
->disk
.minor
);
381 if (!(victim
->curr_state
& DS_FAULTY
))
382 write_attr("faulty", victim
->state_fd
);
386 static int handle_remove_device(struct md_remove_device_cmd
*cmd
, struct active_array
*aa
)
388 struct active_array
*a
;
389 struct mdinfo
*victim
;
392 /* scan all arrays for the given device, if ->state_fd is closed (-1)
393 * in all cases then mark the disk as removed in the metadata.
394 * Otherwise reply that it is busy.
397 /* pass1 check that it is not in use anywhere */
398 /* note: we are safe from re-adds as long as the device exists in the
401 for (a
= aa
; a
; a
= a
->next
) {
404 victim
= find_device(a
, major(cmd
->rdev
), minor(cmd
->rdev
));
407 if (victim
->state_fd
> 0)
411 /* pass2 schedule and process removal per array */
412 for (a
= aa
; a
; a
= a
->next
) {
415 victim
= find_device(a
, major(cmd
->rdev
), minor(cmd
->rdev
));
418 victim
->curr_state
|= DS_REMOVE
;
419 rv
= read_and_act(a
);
427 static int handle_pipe(struct md_generic_cmd
*cmd
, struct active_array
*aa
)
429 switch (cmd
->action
) {
430 case md_action_ping_monitor
:
432 case md_action_remove_device
:
433 return handle_remove_device((void *) cmd
, aa
);
439 static int wait_and_act(struct supertype
*container
, int pfd
,
440 int monfd
, int nowait
)
444 struct active_array
**aap
= &container
->arrays
;
445 struct active_array
*a
, **ap
;
451 add_fd(&rfds
, &maxfd
, pfd
);
452 for (ap
= aap
; *ap
;) {
454 /* once an array has been deactivated we want to
455 * ask the manager to discard it.
469 add_fd(&rfds
, &maxfd
, a
->info
.state_fd
);
470 add_fd(&rfds
, &maxfd
, a
->action_fd
);
471 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
472 add_fd(&rfds
, &maxfd
, mdi
->state_fd
);
477 if (manager_ready
&& *aap
== NULL
) {
478 /* No interesting arrays. Lets see about exiting.
479 * Note that blocking at this point is not a problem
480 * as there are no active arrays, there is nothing that
481 * we need to be ready to do.
483 int fd
= open(container
->device_name
, O_RDONLY
|O_EXCL
);
484 if (fd
>= 0 || errno
!= EBUSY
) {
485 /* OK, we are safe to leave */
488 remove_pidfile(container
->devname
);
494 rv
= select(maxfd
+1, &rfds
, NULL
, NULL
, NULL
);
499 if (FD_ISSET(pfd
, &rfds
)) {
502 if (read(pfd
, &err
, 1) > 0)
503 err
= handle_pipe(active_cmd
, *aap
);
504 write(monfd
, &err
, 1);
508 for (a
= *aap
; a
; a
= a
->next
) {
509 if (a
->replaces
&& !discard_this
) {
510 struct active_array
**ap
;
511 for (ap
= &a
->next
; *ap
&& *ap
!= a
->replaces
;
516 discard_this
= a
->replaces
;
521 rv
+= read_and_act(a
);
524 /* propagate failures across container members */
525 for (a
= *aap
; a
; a
= a
->next
) {
528 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
529 if (mdi
->curr_state
& DS_FAULTY
)
530 reconcile_failed(*aap
, mdi
);
536 void do_monitor(struct supertype
*container
)
541 rv
= wait_and_act(container
, container
->mgr_pipe
[0],
542 container
->mon_pipe
[1], first
);