]>
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);
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
, ',');
156 /* Monitor a set of active md arrays - all of which share the
157 * same metadata - and respond to events that require
160 * New arrays are detected by another thread which allocates
161 * required memory and attaches the data structure to our list.
165 * This is detected by array_state going to 'clear' or 'inactive'.
166 * while we thought it was active.
167 * Response is to mark metadata as clean and 'clear' the array(??)
169 * array_state if 'write-pending'
170 * We mark metadata as 'dirty' then set array to 'active'.
172 * Either ignore, or mark clean, then mark metadata as clean.
175 * detected by rd-N/state reporting "faulty"
176 * mark device as 'failed' in metadata, let the kernel release the
177 * device by writing '-blocked' to rd/state, and finally write 'remove' to
178 * rd/state. Before a disk can be replaced it must be failed and removed
179 * from all container members, this will be preemptive for the other
183 * sync_action was 'resync' and becomes 'idle' and resync_start becomes
185 * Notify metadata that sync is complete.
186 * "Deal with Degraded"
189 * sync_action changes from 'recover' to 'idle'
190 * Check each device state and mark metadata if 'faulty' or 'in_sync'.
191 * "Deal with Degraded"
193 * deal with degraded array
194 * We only do this when first noticing the array is degraded.
195 * This can be when we first see the array, when sync completes or
196 * when recovery completes.
198 * Check if number of failed devices suggests recovery is needed, and
200 * Ask metadata for a spare device
201 * Add device as not in_sync and give a role
206 * This only happens on finding a new array... mdadm will have set
207 * 'resync_start' to the correct value. If 'resync_start' indicates that an
208 * resync needs to occur set the array to the 'active' state rather than the
209 * initial read-auto state.
213 * We wait for a change (poll/select) on array_state, sync_action, and
214 * each rd-X/state file.
215 * When we get any change, we check everything. So read each state file,
216 * then decide what to do.
218 * The core action is to write new metadata to all devices in the array.
219 * This is done at most once on any wakeup.
220 * After that we might:
221 * - update the array_state
222 * - set the role of some devices.
223 * - request a sync_action
227 static int read_and_act(struct active_array
*a
)
233 a
->next_state
= bad_word
;
234 a
->next_action
= bad_action
;
236 a
->curr_state
= read_state(a
->info
.state_fd
);
237 a
->curr_action
= read_action(a
->action_fd
);
238 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
) {
240 if (mdi
->state_fd
> 0)
241 mdi
->curr_state
= read_dev_state(mdi
->state_fd
);
244 if (a
->curr_state
<= inactive
&&
245 a
->prev_state
> inactive
) {
246 /* array has been stopped */
248 a
->container
->ss
->mark_clean(a
, a
->sync_pos
);
249 a
->next_state
= clear
;
252 if (a
->curr_state
== write_pending
) {
253 a
->container
->ss
->mark_dirty(a
);
254 a
->next_state
= active
;
256 if (a
->curr_state
== active_idle
) {
257 /* Set array to 'clean' FIRST, then
258 * a->ss->mark_clean(a);
259 * just ignore for now.
263 if (a
->curr_state
== readonly
) {
264 /* Well, I'm ready to handle things, so
265 * read-auto is OK. FIXME what if we really want
269 if (a
->resync_start
== ~0ULL)
270 a
->next_state
= read_auto
; /* array is clean */
272 a
->container
->ss
->mark_dirty(a
);
273 a
->next_state
= active
;
277 if (a
->curr_action
== idle
&&
278 a
->prev_action
== resync
) {
279 /* check resync_start to see if it is 'max' */
281 a
->container
->ss
->mark_sync(a
, a
->resync_start
);
285 if (a
->curr_action
== idle
&&
286 a
->prev_action
== recover
) {
287 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
) {
288 a
->container
->ss
->set_disk(a
, mdi
->disk
.raid_disk
,
290 if (! (mdi
->curr_state
& DS_INSYNC
))
296 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
) {
297 if (mdi
->curr_state
& DS_FAULTY
) {
298 a
->container
->ss
->set_disk(a
, mdi
->disk
.raid_disk
,
301 mdi
->next_state
= DS_REMOVE
;
305 if (check_degraded
) {
309 a
->container
->ss
->sync_metadata(a
);
311 /* Effect state changes in the array */
312 if (a
->next_state
!= bad_word
)
313 write_attr(array_states
[a
->next_state
], a
->info
.state_fd
);
314 if (a
->next_action
!= bad_action
)
315 write_attr(sync_actions
[a
->next_action
], a
->action_fd
);
316 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
) {
317 if (mdi
->next_state
== DS_REMOVE
&& mdi
->state_fd
> 0) {
320 write_attr("-blocked", mdi
->state_fd
);
321 /* the kernel may not be able to immediately remove the
322 * disk, we can simply wait until the next event to try
325 remove_err
= write_attr("remove", mdi
->state_fd
);
327 close(mdi
->state_fd
);
331 if (mdi
->next_state
& DS_INSYNC
)
332 write_attr("+in_sync", mdi
->state_fd
);
335 /* move curr_ to prev_ */
336 a
->prev_state
= a
->curr_state
;
338 a
->prev_action
= a
->curr_action
;
340 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
) {
341 mdi
->prev_state
= mdi
->curr_state
;
351 static struct mdinfo
*
352 find_device(struct active_array
*a
, int major
, int minor
)
356 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
357 if (mdi
->disk
.major
== major
&& mdi
->disk
.minor
== minor
)
363 static void reconcile_failed(struct active_array
*aa
, struct mdinfo
*failed
)
365 struct active_array
*a
;
366 struct mdinfo
*victim
;
368 for (a
= aa
; a
; a
= a
->next
) {
371 victim
= find_device(a
, failed
->disk
.major
, failed
->disk
.minor
);
375 if (!(victim
->curr_state
& DS_FAULTY
))
376 write_attr("faulty", victim
->state_fd
);
380 static int handle_remove_device(struct md_remove_device_cmd
*cmd
, struct active_array
*aa
)
382 struct active_array
*a
;
383 struct mdinfo
*victim
;
386 /* scan all arrays for the given device, if ->state_fd is closed (-1)
387 * in all cases then mark the disk as removed in the metadata.
388 * Otherwise reply that it is busy.
391 /* pass1 check that it is not in use anywhere */
392 /* note: we are safe from re-adds as long as the device exists in the
395 for (a
= aa
; a
; a
= a
->next
) {
398 victim
= find_device(a
, major(cmd
->rdev
), minor(cmd
->rdev
));
401 if (victim
->state_fd
> 0)
405 /* pass2 schedule and process removal per array */
406 for (a
= aa
; a
; a
= a
->next
) {
409 victim
= find_device(a
, major(cmd
->rdev
), minor(cmd
->rdev
));
412 victim
->curr_state
|= DS_REMOVE
;
413 rv
= read_and_act(a
);
421 static int handle_pipe(struct md_generic_cmd
*cmd
, struct active_array
*aa
)
423 switch (cmd
->action
) {
424 case md_action_ping_monitor
:
426 case md_action_remove_device
:
427 return handle_remove_device((void *) cmd
, aa
);
433 static int wait_and_act(struct active_array
*aa
, int pfd
, int monfd
, int nowait
)
437 struct active_array
*a
;
443 add_fd(&rfds
, &maxfd
, pfd
);
444 for (a
= aa
; a
; a
= a
->next
) {
445 /* once an array has been deactivated only the manager
446 * thread can make us care about it again
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
);
458 rv
= select(maxfd
+1, &rfds
, NULL
, NULL
, NULL
);
463 if (FD_ISSET(pfd
, &rfds
)) {
466 if (read(pfd
, &err
, 1) > 0)
467 err
= handle_pipe(active_cmd
, aa
);
468 write(monfd
, &err
, 1);
472 for (a
= aa
; a
; a
= a
->next
) {
473 if (a
->replaces
&& !discard_this
) {
474 struct active_array
**ap
;
475 for (ap
= &a
->next
; *ap
&& *ap
!= a
->replaces
;
480 discard_this
= a
->replaces
;
484 rv
+= read_and_act(a
);
487 /* propagate failures across container members */
488 for (a
= aa
; a
; a
= a
->next
) {
491 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
492 if (mdi
->curr_state
& DS_FAULTY
)
493 reconcile_failed(aa
, mdi
);
499 void do_monitor(struct supertype
*container
)
504 rv
= wait_and_act(container
->arrays
, container
->mgr_pipe
[0],
505 container
->mon_pipe
[1], first
);