]>
Commit | Line | Data |
---|---|---|
549e9569 NB |
1 | |
2 | #include "mdadm.h" | |
3 | #include "mdmon.h" | |
4 | ||
5 | #include <sys/select.h> | |
6 | ||
7 | ||
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 | |
13 | }; | |
14 | ||
15 | static int write_attr(char *attr, int fd) | |
16 | { | |
17 | return write(fd, attr, strlen(attr)); | |
18 | } | |
19 | ||
20 | static void add_fd(fd_set *fds, int *maxfd, int fd) | |
21 | { | |
22 | if (fd < 0) | |
23 | return; | |
24 | if (fd > *maxfd) | |
25 | *maxfd = fd; | |
26 | FD_SET(fd, fds); | |
27 | } | |
28 | ||
29 | static int read_attr(char *buf, int len, int fd) | |
30 | { | |
31 | int n; | |
32 | ||
33 | if (fd < 0) { | |
34 | buf[0] = 0; | |
35 | return 0; | |
36 | } | |
37 | lseek(fd, 0, 0); | |
38 | n = read(fd, buf, len - 1); | |
39 | ||
40 | if (n <= 0) { | |
41 | buf[0] = 0; | |
42 | return 0; | |
43 | } | |
44 | buf[n] = 0; | |
45 | if (buf[n-1] == '\n') | |
46 | buf[n-1] = 0; | |
47 | return n; | |
48 | } | |
49 | ||
50 | static int get_sync_pos(struct active_array *a) | |
51 | { | |
52 | char buf[30]; | |
53 | int n; | |
54 | ||
55 | n = read_attr(buf, 30, a->sync_pos_fd); | |
56 | if (n <= 0) | |
57 | return n; | |
58 | ||
59 | if (strncmp(buf, "max", 3) == 0) { | |
60 | a->sync_pos = ~(unsigned long long)0; | |
61 | return 1; | |
62 | } | |
63 | a->sync_pos = strtoull(buf, NULL, 10); | |
64 | return 1; | |
65 | } | |
66 | ||
c052ba30 DW |
67 | static int get_resync_start(struct active_array *a) |
68 | { | |
69 | char buf[30]; | |
70 | int n; | |
71 | ||
72 | n = read_attr(buf, 30, a->resync_start_fd); | |
73 | if (n <= 0) | |
74 | return n; | |
75 | ||
76 | a->resync_start = strtoull(buf, NULL, 10); | |
77 | ||
78 | return 1; | |
79 | } | |
549e9569 NB |
80 | |
81 | static int attr_match(const char *attr, const char *str) | |
82 | { | |
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 | |
86 | */ | |
87 | while (*attr && *str && *attr == *str) { | |
88 | attr++; | |
89 | str++; | |
90 | } | |
91 | ||
92 | if (*str || (*attr && *attr != ',' && *attr != '\n')) | |
93 | return 0; | |
94 | return 1; | |
95 | } | |
96 | ||
97 | static int match_word(const char *word, char **list) | |
98 | { | |
99 | int n; | |
100 | for (n=0; list[n]; n++) | |
101 | if (attr_match(word, list[n])) | |
102 | break; | |
103 | return n; | |
104 | } | |
105 | ||
106 | static enum array_state read_state(int fd) | |
107 | { | |
108 | char buf[20]; | |
109 | int n = read_attr(buf, 20, fd); | |
110 | ||
111 | if (n <= 0) | |
112 | return bad_word; | |
113 | return (enum array_state) match_word(buf, array_states); | |
114 | } | |
115 | ||
116 | static enum sync_action read_action( int fd) | |
117 | { | |
118 | char buf[20]; | |
119 | int n = read_attr(buf, 20, fd); | |
120 | ||
121 | if (n <= 0) | |
122 | return bad_action; | |
123 | return (enum sync_action) match_word(buf, sync_actions); | |
124 | } | |
125 | ||
126 | #define DS_FAULTY 1 | |
127 | #define DS_INSYNC 2 | |
128 | #define DS_WRITE_MOSTLY 4 | |
129 | #define DS_SPARE 8 | |
130 | #define DS_REMOVE 1024 | |
131 | ||
132 | int read_dev_state(int fd) | |
133 | { | |
134 | char buf[60]; | |
135 | int n = read_attr(buf, 60, fd); | |
136 | char *cp; | |
137 | int rv = 0; | |
138 | ||
139 | if (n <= 0) | |
140 | return 0; | |
141 | ||
142 | cp = buf; | |
143 | while (cp) { | |
144 | if (attr_match("faulty", cp)) | |
145 | rv |= DS_FAULTY; | |
146 | if (attr_match("in_sync", cp)) | |
147 | rv |= DS_INSYNC; | |
148 | if (attr_match("write_mostly", cp)) | |
149 | rv |= DS_WRITE_MOSTLY; | |
150 | if (attr_match("spare", cp)) | |
151 | rv |= DS_SPARE; | |
152 | cp = strchr(cp, ','); | |
153 | if (cp) | |
154 | cp++; | |
155 | } | |
156 | return rv; | |
157 | } | |
158 | ||
159 | ||
160 | /* Monitor a set of active md arrays - all of which share the | |
161 | * same metadata - and respond to events that require | |
162 | * metadata update. | |
163 | * | |
164 | * New arrays are detected by another thread which allocates | |
165 | * required memory and attaches the data structure to our list. | |
166 | * | |
167 | * Events: | |
168 | * Array stops. | |
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(??) | |
172 | * write-pending | |
173 | * array_state if 'write-pending' | |
174 | * We mark metadata as 'dirty' then set array to 'active'. | |
175 | * active_idle | |
176 | * Either ignore, or mark clean, then mark metadata as clean. | |
177 | * | |
178 | * device fails | |
179 | * detected by rd-N/state reporting "faulty" | |
180 | * mark device as 'failed' in metadata, the remove device | |
181 | * by writing 'remove' to rd/state. | |
182 | * | |
183 | * sync completes | |
184 | * sync_action was 'resync' and becomes 'idle' and resync_start becomes | |
185 | * MaxSector | |
186 | * Notify metadata that sync is complete. | |
187 | * "Deal with Degraded" | |
188 | * | |
189 | * recovery completes | |
190 | * sync_action changes from 'recover' to 'idle' | |
191 | * Check each device state and mark metadata if 'faulty' or 'in_sync'. | |
192 | * "Deal with Degraded" | |
193 | * | |
194 | * deal with degraded array | |
195 | * We only do this when first noticing the array is degraded. | |
196 | * This can be when we first see the array, when sync completes or | |
197 | * when recovery completes. | |
198 | * | |
199 | * Check if number of failed devices suggests recovery is needed, and | |
200 | * skip if not. | |
201 | * Ask metadata for a spare device | |
202 | * Add device as not in_sync and give a role | |
203 | * Update metadata. | |
204 | * Start recovery. | |
205 | * | |
206 | * deal with resync | |
c052ba30 DW |
207 | * This only happens on finding a new array... mdadm will have set |
208 | * 'resync_start' to the correct value. If 'resync_start' indicates that an | |
209 | * resync needs to occur set the array to the 'active' state rather than the | |
210 | * initial read-auto state. | |
549e9569 NB |
211 | * |
212 | * | |
213 | * | |
214 | * We wait for a change (poll/select) on array_state, sync_action, and | |
215 | * each rd-X/state file. | |
216 | * When we get any change, we check everything. So read each state file, | |
217 | * then decide what to do. | |
218 | * | |
219 | * The core action is to write new metadata to all devices in the array. | |
220 | * This is done at most once on any wakeup. | |
221 | * After that we might: | |
222 | * - update the array_state | |
223 | * - set the role of some devices. | |
224 | * - request a sync_action | |
225 | * | |
226 | */ | |
227 | ||
228 | static int read_and_act(struct active_array *a) | |
229 | { | |
230 | int check_degraded; | |
231 | struct mdinfo *mdi; | |
232 | ||
233 | a->next_state = bad_word; | |
234 | a->next_action = bad_action; | |
235 | ||
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) { | |
239 | mdi->next_state = 0; | |
240 | mdi->curr_state = read_dev_state(mdi->state_fd); | |
241 | } | |
242 | ||
243 | if (a->curr_state <= inactive && | |
244 | a->prev_state > inactive) { | |
245 | /* array has been stopped */ | |
246 | get_sync_pos(a); | |
247 | a->container->ss->mark_clean(a, a->sync_pos); | |
248 | a->next_state = clear; | |
249 | } | |
250 | if (a->curr_state == write_pending) { | |
251 | a->container->ss->mark_dirty(a); | |
252 | a->next_state = active; | |
253 | } | |
254 | if (a->curr_state == active_idle) { | |
255 | /* Set array to 'clean' FIRST, then | |
256 | * a->ss->mark_clean(a); | |
257 | * just ignore for now. | |
258 | */ | |
259 | } | |
260 | ||
261 | if (a->curr_state == readonly) { | |
262 | /* Well, I'm ready to handle things, so | |
263 | * read-auto is OK. FIXME what if we really want | |
264 | * readonly ??? | |
265 | */ | |
c052ba30 DW |
266 | get_resync_start(a); |
267 | if (a->resync_start == ~0ULL) | |
268 | a->next_state = read_auto; /* array is clean */ | |
269 | else { | |
270 | a->container->ss->mark_dirty(a); | |
271 | a->next_state = active; | |
272 | } | |
549e9569 NB |
273 | } |
274 | ||
275 | if (a->curr_action == idle && | |
276 | a->prev_action == resync) { | |
fd7cde1b DW |
277 | /* check resync_start to see if it is 'max' */ |
278 | get_resync_start(a); | |
279 | a->container->ss->mark_sync(a, a->resync_start); | |
549e9569 NB |
280 | check_degraded = 1; |
281 | } | |
282 | ||
283 | if (a->curr_action == idle && | |
284 | a->prev_action == recover) { | |
285 | for (mdi = a->info.devs ; mdi ; mdi = mdi->next) { | |
286 | a->container->ss->set_disk(a, mdi->disk.raid_disk); | |
287 | if (! (mdi->curr_state & DS_INSYNC)) | |
288 | check_degraded = 1; | |
289 | } | |
290 | } | |
291 | ||
292 | ||
293 | for (mdi = a->info.devs ; mdi ; mdi = mdi->next) { | |
294 | if (mdi->curr_state & DS_FAULTY) { | |
295 | a->container->ss->set_disk(a, mdi->disk.raid_disk); | |
296 | check_degraded = 1; | |
297 | mdi->next_state = DS_REMOVE; | |
298 | } | |
299 | } | |
300 | ||
301 | if (check_degraded) { | |
302 | // FIXME; | |
303 | } | |
304 | ||
305 | a->container->ss->sync_metadata(a); | |
306 | ||
307 | /* Effect state changes in the array */ | |
308 | if (a->next_state != bad_word) | |
309 | write_attr(array_states[a->next_state], a->info.state_fd); | |
310 | if (a->next_action != bad_action) | |
311 | write_attr(sync_actions[a->next_action], a->action_fd); | |
312 | for (mdi = a->info.devs; mdi ; mdi = mdi->next) { | |
313 | if (mdi->next_state == DS_REMOVE) | |
314 | write_attr("remove", mdi->state_fd); | |
315 | if (mdi->next_state & DS_INSYNC) | |
316 | write_attr("+in_sync", mdi->state_fd); | |
317 | } | |
318 | ||
319 | /* move curr_ to prev_ */ | |
320 | a->prev_state = a->curr_state; | |
321 | ||
322 | a->prev_action = a->curr_action; | |
323 | ||
324 | for (mdi = a->info.devs; mdi ; mdi = mdi->next) { | |
325 | mdi->prev_state = mdi->curr_state; | |
326 | mdi->next_state = 0; | |
327 | } | |
328 | ||
329 | return 1; | |
330 | } | |
331 | ||
332 | static int wait_and_act(struct active_array *aa, int pfd, int nowait) | |
333 | { | |
334 | fd_set rfds; | |
335 | int maxfd = 0; | |
336 | struct active_array *a; | |
337 | int rv; | |
338 | ||
339 | FD_ZERO(&rfds); | |
340 | ||
341 | add_fd(&rfds, &maxfd, pfd); | |
342 | for (a = aa ; a ; a = a->next) { | |
343 | struct mdinfo *mdi; | |
344 | ||
345 | add_fd(&rfds, &maxfd, a->info.state_fd); | |
346 | add_fd(&rfds, &maxfd, a->action_fd); | |
347 | for (mdi = a->info.devs ; mdi ; mdi = mdi->next) | |
348 | add_fd(&rfds, &maxfd, mdi->state_fd); | |
349 | } | |
350 | ||
351 | if (!nowait) { | |
352 | rv = select(maxfd+1, &rfds, NULL, NULL, NULL); | |
353 | ||
354 | if (rv <= 0) | |
355 | return rv; | |
356 | ||
357 | if (FD_ISSET(pfd, &rfds)) { | |
358 | char buf[4]; | |
359 | read(pfd, buf, 4); | |
360 | ; // FIXME read from the pipe | |
361 | } | |
362 | } | |
363 | ||
364 | for (a = aa; a ; a = a->next) { | |
365 | if (a->replaces) { | |
366 | struct active_array **ap; | |
367 | for (ap = &a->next; *ap && *ap != a->replaces; | |
368 | ap = & (*ap)->next) | |
369 | ; | |
370 | if (*ap) | |
371 | *ap = (*ap)->next; | |
372 | discard_this = a->replaces; | |
373 | a->replaces = NULL; | |
374 | } | |
375 | rv += read_and_act(a); | |
376 | } | |
377 | return rv; | |
378 | } | |
379 | ||
380 | void do_monitor(struct supertype *container) | |
381 | { | |
382 | int rv; | |
383 | int first = 1; | |
384 | do { | |
385 | rv = wait_and_act(container->arrays, container->pipe[0], first); | |
386 | first = 0; | |
387 | } while (rv >= 0); | |
388 | } |