]>
Commit | Line | Data |
---|---|---|
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 | ||
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 | } | |
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 | int read_dev_state(int fd) | |
127 | { | |
128 | char buf[60]; | |
129 | int n = read_attr(buf, 60, fd); | |
130 | char *cp; | |
131 | int rv = 0; | |
132 | ||
133 | if (n <= 0) | |
134 | return 0; | |
135 | ||
136 | cp = buf; | |
137 | while (cp) { | |
138 | if (attr_match(cp, "faulty")) | |
139 | rv |= DS_FAULTY; | |
140 | if (attr_match(cp, "in_sync")) | |
141 | rv |= DS_INSYNC; | |
142 | if (attr_match(cp, "write_mostly")) | |
143 | rv |= DS_WRITE_MOSTLY; | |
144 | if (attr_match(cp, "spare")) | |
145 | rv |= DS_SPARE; | |
146 | if (attr_match(cp, "blocked")) | |
147 | rv |= DS_BLOCKED; | |
148 | cp = strchr(cp, ','); | |
149 | if (cp) | |
150 | cp++; | |
151 | } | |
152 | return rv; | |
153 | } | |
154 | ||
155 | ||
156 | /* Monitor a set of active md arrays - all of which share the | |
157 | * same metadata - and respond to events that require | |
158 | * metadata update. | |
159 | * | |
160 | * New arrays are detected by another thread which allocates | |
161 | * required memory and attaches the data structure to our list. | |
162 | * | |
163 | * Events: | |
164 | * Array stops. | |
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(??) | |
168 | * write-pending | |
169 | * array_state if 'write-pending' | |
170 | * We mark metadata as 'dirty' then set array to 'active'. | |
171 | * active_idle | |
172 | * Either ignore, or mark clean, then mark metadata as clean. | |
173 | * | |
174 | * device fails | |
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 | |
180 | * arrays... safe? | |
181 | * | |
182 | * sync completes | |
183 | * sync_action was 'resync' and becomes 'idle' and resync_start becomes | |
184 | * MaxSector | |
185 | * Notify metadata that sync is complete. | |
186 | * "Deal with Degraded" | |
187 | * | |
188 | * recovery completes | |
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" | |
192 | * | |
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. | |
197 | * | |
198 | * Check if number of failed devices suggests recovery is needed, and | |
199 | * skip if not. | |
200 | * Ask metadata for a spare device | |
201 | * Add device as not in_sync and give a role | |
202 | * Update metadata. | |
203 | * Start recovery. | |
204 | * | |
205 | * deal with resync | |
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. | |
210 | * | |
211 | * | |
212 | * | |
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. | |
217 | * | |
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 | |
224 | * | |
225 | */ | |
226 | ||
227 | static int read_and_act(struct active_array *a) | |
228 | { | |
229 | int check_degraded; | |
230 | int deactivate = 0; | |
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 | if (mdi->state_fd > 0) | |
241 | mdi->curr_state = read_dev_state(mdi->state_fd); | |
242 | } | |
243 | ||
244 | if (a->curr_state <= inactive && | |
245 | a->prev_state > inactive) { | |
246 | /* array has been stopped */ | |
247 | get_sync_pos(a); | |
248 | a->container->ss->mark_clean(a, a->sync_pos); | |
249 | a->next_state = clear; | |
250 | deactivate = 1; | |
251 | } | |
252 | if (a->curr_state == write_pending) { | |
253 | a->container->ss->mark_clean(a, 0); | |
254 | a->next_state = active; | |
255 | } | |
256 | if (a->curr_state == active_idle) { | |
257 | /* Set array to 'clean' FIRST, then | |
258 | * a->ss->mark_clean(a, ~0ULL); | |
259 | * just ignore for now. | |
260 | */ | |
261 | } | |
262 | ||
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 | |
266 | * readonly ??? | |
267 | */ | |
268 | get_resync_start(a); | |
269 | if (a->resync_start == ~0ULL) | |
270 | a->next_state = read_auto; /* array is clean */ | |
271 | else { | |
272 | a->container->ss->mark_clean(a, 0); | |
273 | a->next_state = active; | |
274 | } | |
275 | } | |
276 | ||
277 | if (a->curr_action == idle && | |
278 | a->prev_action == resync) { | |
279 | /* A resync has finished. The endpoint is recorded in | |
280 | * 'sync_start'. We don't update the metadata | |
281 | * until the array goes inactive or readonly though. | |
282 | * Just check if we need to fiddle spares. | |
283 | */ | |
284 | check_degraded = 1; | |
285 | } | |
286 | ||
287 | if (a->curr_action == idle && | |
288 | a->prev_action == recover) { | |
289 | for (mdi = a->info.devs ; mdi ; mdi = mdi->next) { | |
290 | a->container->ss->set_disk(a, mdi->disk.raid_disk, | |
291 | mdi->curr_state); | |
292 | if (! (mdi->curr_state & DS_INSYNC)) | |
293 | check_degraded = 1; | |
294 | } | |
295 | } | |
296 | ||
297 | ||
298 | for (mdi = a->info.devs ; mdi ; mdi = mdi->next) { | |
299 | if (mdi->curr_state & DS_FAULTY) { | |
300 | a->container->ss->set_disk(a, mdi->disk.raid_disk, | |
301 | mdi->curr_state); | |
302 | check_degraded = 1; | |
303 | mdi->next_state = DS_REMOVE; | |
304 | } | |
305 | } | |
306 | ||
307 | if (check_degraded) { | |
308 | // FIXME; | |
309 | } | |
310 | ||
311 | a->container->ss->sync_metadata(a); | |
312 | ||
313 | /* Effect state changes in the array */ | |
314 | if (a->next_state != bad_word) | |
315 | write_attr(array_states[a->next_state], a->info.state_fd); | |
316 | if (a->next_action != bad_action) | |
317 | write_attr(sync_actions[a->next_action], a->action_fd); | |
318 | for (mdi = a->info.devs; mdi ; mdi = mdi->next) { | |
319 | if (mdi->next_state == DS_REMOVE && mdi->state_fd > 0) { | |
320 | int remove_err; | |
321 | ||
322 | write_attr("-blocked", mdi->state_fd); | |
323 | /* the kernel may not be able to immediately remove the | |
324 | * disk, we can simply wait until the next event to try | |
325 | * again. | |
326 | */ | |
327 | remove_err = write_attr("remove", mdi->state_fd); | |
328 | if (!remove_err) { | |
329 | close(mdi->state_fd); | |
330 | mdi->state_fd = -1; | |
331 | } | |
332 | } | |
333 | if (mdi->next_state & DS_INSYNC) | |
334 | write_attr("+in_sync", mdi->state_fd); | |
335 | } | |
336 | ||
337 | /* move curr_ to prev_ */ | |
338 | a->prev_state = a->curr_state; | |
339 | ||
340 | a->prev_action = a->curr_action; | |
341 | ||
342 | for (mdi = a->info.devs; mdi ; mdi = mdi->next) { | |
343 | mdi->prev_state = mdi->curr_state; | |
344 | mdi->next_state = 0; | |
345 | } | |
346 | ||
347 | if (deactivate) | |
348 | a->container = NULL; | |
349 | ||
350 | return 1; | |
351 | } | |
352 | ||
353 | static struct mdinfo * | |
354 | find_device(struct active_array *a, int major, int minor) | |
355 | { | |
356 | struct mdinfo *mdi; | |
357 | ||
358 | for (mdi = a->info.devs ; mdi ; mdi = mdi->next) | |
359 | if (mdi->disk.major == major && mdi->disk.minor == minor) | |
360 | return mdi; | |
361 | ||
362 | return NULL; | |
363 | } | |
364 | ||
365 | static void reconcile_failed(struct active_array *aa, struct mdinfo *failed) | |
366 | { | |
367 | struct active_array *a; | |
368 | struct mdinfo *victim; | |
369 | ||
370 | for (a = aa; a; a = a->next) { | |
371 | if (!a->container) | |
372 | continue; | |
373 | victim = find_device(a, failed->disk.major, failed->disk.minor); | |
374 | if (!victim) | |
375 | continue; | |
376 | ||
377 | if (!(victim->curr_state & DS_FAULTY)) | |
378 | write_attr("faulty", victim->state_fd); | |
379 | } | |
380 | } | |
381 | ||
382 | static int handle_remove_device(struct md_remove_device_cmd *cmd, struct active_array *aa) | |
383 | { | |
384 | struct active_array *a; | |
385 | struct mdinfo *victim; | |
386 | int rv; | |
387 | ||
388 | /* scan all arrays for the given device, if ->state_fd is closed (-1) | |
389 | * in all cases then mark the disk as removed in the metadata. | |
390 | * Otherwise reply that it is busy. | |
391 | */ | |
392 | ||
393 | /* pass1 check that it is not in use anywhere */ | |
394 | /* note: we are safe from re-adds as long as the device exists in the | |
395 | * container | |
396 | */ | |
397 | for (a = aa; a; a = a->next) { | |
398 | if (!a->container) | |
399 | continue; | |
400 | victim = find_device(a, major(cmd->rdev), minor(cmd->rdev)); | |
401 | if (!victim) | |
402 | continue; | |
403 | if (victim->state_fd > 0) | |
404 | return -EBUSY; | |
405 | } | |
406 | ||
407 | /* pass2 schedule and process removal per array */ | |
408 | for (a = aa; a; a = a->next) { | |
409 | if (!a->container) | |
410 | continue; | |
411 | victim = find_device(a, major(cmd->rdev), minor(cmd->rdev)); | |
412 | if (!victim) | |
413 | continue; | |
414 | victim->curr_state |= DS_REMOVE; | |
415 | rv = read_and_act(a); | |
416 | if (rv < 0) | |
417 | return rv; | |
418 | } | |
419 | ||
420 | return 0; | |
421 | } | |
422 | ||
423 | static int handle_pipe(struct md_generic_cmd *cmd, struct active_array *aa) | |
424 | { | |
425 | switch (cmd->action) { | |
426 | case md_action_ping_monitor: | |
427 | return 0; | |
428 | case md_action_remove_device: | |
429 | return handle_remove_device((void *) cmd, aa); | |
430 | } | |
431 | ||
432 | return -1; | |
433 | } | |
434 | ||
435 | static int wait_and_act(struct active_array *aa, int pfd, int monfd, int nowait) | |
436 | { | |
437 | fd_set rfds; | |
438 | int maxfd = 0; | |
439 | struct active_array *a; | |
440 | int rv; | |
441 | struct mdinfo *mdi; | |
442 | ||
443 | FD_ZERO(&rfds); | |
444 | ||
445 | add_fd(&rfds, &maxfd, pfd); | |
446 | for (a = aa ; a ; a = a->next) { | |
447 | /* once an array has been deactivated only the manager | |
448 | * thread can make us care about it again | |
449 | */ | |
450 | if (!a->container) | |
451 | continue; | |
452 | ||
453 | add_fd(&rfds, &maxfd, a->info.state_fd); | |
454 | add_fd(&rfds, &maxfd, a->action_fd); | |
455 | for (mdi = a->info.devs ; mdi ; mdi = mdi->next) | |
456 | add_fd(&rfds, &maxfd, mdi->state_fd); | |
457 | } | |
458 | ||
459 | if (!nowait) { | |
460 | rv = select(maxfd+1, &rfds, NULL, NULL, NULL); | |
461 | ||
462 | if (rv <= 0) | |
463 | return rv; | |
464 | ||
465 | if (FD_ISSET(pfd, &rfds)) { | |
466 | int err = -1; | |
467 | ||
468 | if (read(pfd, &err, 1) > 0) | |
469 | err = handle_pipe(active_cmd, aa); | |
470 | write(monfd, &err, 1); | |
471 | } | |
472 | } | |
473 | ||
474 | for (a = aa; a ; a = a->next) { | |
475 | if (a->replaces && !discard_this) { | |
476 | struct active_array **ap; | |
477 | for (ap = &a->next; *ap && *ap != a->replaces; | |
478 | ap = & (*ap)->next) | |
479 | ; | |
480 | if (*ap) | |
481 | *ap = (*ap)->next; | |
482 | discard_this = a->replaces; | |
483 | a->replaces = NULL; | |
484 | } | |
485 | if (a->container) | |
486 | rv += read_and_act(a); | |
487 | } | |
488 | ||
489 | /* propagate failures across container members */ | |
490 | for (a = aa; a ; a = a->next) { | |
491 | if (!a->container) | |
492 | continue; | |
493 | for (mdi = a->info.devs ; mdi ; mdi = mdi->next) | |
494 | if (mdi->curr_state & DS_FAULTY) | |
495 | reconcile_failed(aa, mdi); | |
496 | } | |
497 | ||
498 | return rv; | |
499 | } | |
500 | ||
501 | void do_monitor(struct supertype *container) | |
502 | { | |
503 | int rv; | |
504 | int first = 1; | |
505 | do { | |
506 | rv = wait_and_act(container->arrays, container->mgr_pipe[0], | |
507 | container->mon_pipe[1], first); | |
508 | first = 0; | |
509 | } while (rv >= 0); | |
510 | } |