]>
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);
68 static int attr_match(const char *attr
, const char *str
)
70 /* See if attr, read from a sysfs file, matches
71 * str. They must either be the same, or attr can
72 * have a trailing newline or comma
74 while (*attr
&& *str
&& *attr
== *str
) {
79 if (*str
|| (*attr
&& *attr
!= ',' && *attr
!= '\n'))
84 static int match_word(const char *word
, char **list
)
87 for (n
=0; list
[n
]; n
++)
88 if (attr_match(word
, list
[n
]))
93 static enum array_state
read_state(int fd
)
96 int n
= read_attr(buf
, 20, fd
);
100 return (enum array_state
) match_word(buf
, array_states
);
103 static enum sync_action
read_action( int fd
)
106 int n
= read_attr(buf
, 20, fd
);
110 return (enum sync_action
) match_word(buf
, sync_actions
);
115 #define DS_WRITE_MOSTLY 4
117 #define DS_REMOVE 1024
119 int read_dev_state(int fd
)
122 int n
= read_attr(buf
, 60, fd
);
131 if (attr_match("faulty", cp
))
133 if (attr_match("in_sync", cp
))
135 if (attr_match("write_mostly", cp
))
136 rv
|= DS_WRITE_MOSTLY
;
137 if (attr_match("spare", cp
))
139 cp
= strchr(cp
, ',');
147 /* Monitor a set of active md arrays - all of which share the
148 * same metadata - and respond to events that require
151 * New arrays are detected by another thread which allocates
152 * required memory and attaches the data structure to our list.
156 * This is detected by array_state going to 'clear' or 'inactive'.
157 * while we thought it was active.
158 * Response is to mark metadata as clean and 'clear' the array(??)
160 * array_state if 'write-pending'
161 * We mark metadata as 'dirty' then set array to 'active'.
163 * Either ignore, or mark clean, then mark metadata as clean.
166 * detected by rd-N/state reporting "faulty"
167 * mark device as 'failed' in metadata, the remove device
168 * by writing 'remove' to rd/state.
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....
195 * Maybe this is done by mdadm before passing the array to us?
197 * If array is 'clean' but metadata is 'dirty', start a resync
198 * and mark array as 'dirty'.
203 * We wait for a change (poll/select) on array_state, sync_action, and
204 * each rd-X/state file.
205 * When we get any change, we check everything. So read each state file,
206 * then decide what to do.
208 * The core action is to write new metadata to all devices in the array.
209 * This is done at most once on any wakeup.
210 * After that we might:
211 * - update the array_state
212 * - set the role of some devices.
213 * - request a sync_action
217 static int read_and_act(struct active_array
*a
)
222 a
->next_state
= bad_word
;
223 a
->next_action
= bad_action
;
225 a
->curr_state
= read_state(a
->info
.state_fd
);
226 a
->curr_action
= read_action(a
->action_fd
);
227 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
) {
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 */
236 a
->container
->ss
->mark_clean(a
, a
->sync_pos
);
237 a
->next_state
= clear
;
239 if (a
->curr_state
== write_pending
) {
240 a
->container
->ss
->mark_dirty(a
);
241 a
->next_state
= active
;
243 if (a
->curr_state
== active_idle
) {
244 /* Set array to 'clean' FIRST, then
245 * a->ss->mark_clean(a);
246 * just ignore for now.
250 if (a
->curr_state
== readonly
) {
251 /* Well, I'm ready to handle things, so
252 * read-auto is OK. FIXME what if we really want
255 a
->next_state
= read_auto
;
258 if (a
->curr_action
== idle
&&
259 a
->prev_action
== resync
) {
260 /* check resync_start to see if it is 'max'.
261 * Do I open here, or have it open the whole time?
267 if (a
->curr_action
== idle
&&
268 a
->prev_action
== recover
) {
269 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
) {
270 a
->container
->ss
->set_disk(a
, mdi
->disk
.raid_disk
);
271 if (! (mdi
->curr_state
& DS_INSYNC
))
277 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
) {
278 if (mdi
->curr_state
& DS_FAULTY
) {
279 a
->container
->ss
->set_disk(a
, mdi
->disk
.raid_disk
);
281 mdi
->next_state
= DS_REMOVE
;
285 if (check_degraded
) {
289 a
->container
->ss
->sync_metadata(a
);
291 /* Effect state changes in the array */
292 if (a
->next_state
!= bad_word
)
293 write_attr(array_states
[a
->next_state
], a
->info
.state_fd
);
294 if (a
->next_action
!= bad_action
)
295 write_attr(sync_actions
[a
->next_action
], a
->action_fd
);
296 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
) {
297 if (mdi
->next_state
== DS_REMOVE
)
298 write_attr("remove", mdi
->state_fd
);
299 if (mdi
->next_state
& DS_INSYNC
)
300 write_attr("+in_sync", mdi
->state_fd
);
303 /* move curr_ to prev_ */
304 a
->prev_state
= a
->curr_state
;
306 a
->prev_action
= a
->curr_action
;
308 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
) {
309 mdi
->prev_state
= mdi
->curr_state
;
316 static int wait_and_act(struct active_array
*aa
, int pfd
, int nowait
)
320 struct active_array
*a
;
325 add_fd(&rfds
, &maxfd
, pfd
);
326 for (a
= aa
; a
; a
= a
->next
) {
329 add_fd(&rfds
, &maxfd
, a
->info
.state_fd
);
330 add_fd(&rfds
, &maxfd
, a
->action_fd
);
331 for (mdi
= a
->info
.devs
; mdi
; mdi
= mdi
->next
)
332 add_fd(&rfds
, &maxfd
, mdi
->state_fd
);
336 rv
= select(maxfd
+1, &rfds
, NULL
, NULL
, NULL
);
341 if (FD_ISSET(pfd
, &rfds
)) {
344 ; // FIXME read from the pipe
348 for (a
= aa
; a
; a
= a
->next
) {
350 struct active_array
**ap
;
351 for (ap
= &a
->next
; *ap
&& *ap
!= a
->replaces
;
356 discard_this
= a
->replaces
;
359 rv
+= read_and_act(a
);
364 void do_monitor(struct supertype
*container
)
369 rv
= wait_and_act(container
->arrays
, container
->pipe
[0], first
);