]>
Commit | Line | Data |
---|---|---|
549e9569 NB |
1 | |
2 | #include "mdadm.h" | |
3 | #include "mdmon.h" | |
4 | ||
5 | #include <sys/select.h> | |
1ed3f387 | 6 | #include <signal.h> |
549e9569 NB |
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 | ||
549e9569 NB |
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) { | |
8d45d196 | 138 | if (attr_match(cp, "faulty")) |
549e9569 | 139 | rv |= DS_FAULTY; |
8d45d196 | 140 | if (attr_match(cp, "in_sync")) |
549e9569 | 141 | rv |= DS_INSYNC; |
8d45d196 | 142 | if (attr_match(cp, "write_mostly")) |
549e9569 | 143 | rv |= DS_WRITE_MOSTLY; |
8d45d196 | 144 | if (attr_match(cp, "spare")) |
549e9569 | 145 | rv |= DS_SPARE; |
8d45d196 DW |
146 | if (attr_match(cp, "blocked")) |
147 | rv |= DS_BLOCKED; | |
549e9569 NB |
148 | cp = strchr(cp, ','); |
149 | if (cp) | |
150 | cp++; | |
151 | } | |
152 | return rv; | |
153 | } | |
154 | ||
1ed3f387 NB |
155 | static void signal_manager(void) |
156 | { | |
157 | kill(getpid(), SIGUSR1); | |
158 | } | |
549e9569 NB |
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" | |
8d45d196 DW |
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 | |
0af73f61 DW |
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 | |
184 | * arrays... safe? | |
549e9569 NB |
185 | * |
186 | * sync completes | |
187 | * sync_action was 'resync' and becomes 'idle' and resync_start becomes | |
188 | * MaxSector | |
189 | * Notify metadata that sync is complete. | |
190 | * "Deal with Degraded" | |
191 | * | |
192 | * recovery completes | |
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" | |
196 | * | |
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. | |
201 | * | |
202 | * Check if number of failed devices suggests recovery is needed, and | |
203 | * skip if not. | |
204 | * Ask metadata for a spare device | |
205 | * Add device as not in_sync and give a role | |
206 | * Update metadata. | |
207 | * Start recovery. | |
208 | * | |
209 | * deal with resync | |
c052ba30 DW |
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. | |
549e9569 NB |
214 | * |
215 | * | |
216 | * | |
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. | |
221 | * | |
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 | |
228 | * | |
229 | */ | |
230 | ||
231 | static int read_and_act(struct active_array *a) | |
232 | { | |
233 | int check_degraded; | |
2a0bb19e | 234 | int deactivate = 0; |
549e9569 NB |
235 | struct mdinfo *mdi; |
236 | ||
237 | a->next_state = bad_word; | |
238 | a->next_action = bad_action; | |
239 | ||
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) { | |
243 | mdi->next_state = 0; | |
8d45d196 DW |
244 | if (mdi->state_fd > 0) |
245 | mdi->curr_state = read_dev_state(mdi->state_fd); | |
549e9569 NB |
246 | } |
247 | ||
248 | if (a->curr_state <= inactive && | |
249 | a->prev_state > inactive) { | |
250 | /* array has been stopped */ | |
251 | get_sync_pos(a); | |
252 | a->container->ss->mark_clean(a, a->sync_pos); | |
253 | a->next_state = clear; | |
2a0bb19e | 254 | deactivate = 1; |
549e9569 NB |
255 | } |
256 | if (a->curr_state == write_pending) { | |
4e5528c6 | 257 | a->container->ss->mark_clean(a, 0); |
549e9569 NB |
258 | a->next_state = active; |
259 | } | |
260 | if (a->curr_state == active_idle) { | |
261 | /* Set array to 'clean' FIRST, then | |
4e5528c6 | 262 | * a->ss->mark_clean(a, ~0ULL); |
549e9569 NB |
263 | * just ignore for now. |
264 | */ | |
265 | } | |
266 | ||
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 | |
270 | * readonly ??? | |
271 | */ | |
c052ba30 DW |
272 | get_resync_start(a); |
273 | if (a->resync_start == ~0ULL) | |
274 | a->next_state = read_auto; /* array is clean */ | |
275 | else { | |
4e5528c6 | 276 | a->container->ss->mark_clean(a, 0); |
c052ba30 DW |
277 | a->next_state = active; |
278 | } | |
549e9569 NB |
279 | } |
280 | ||
281 | if (a->curr_action == idle && | |
282 | a->prev_action == resync) { | |
4e5528c6 NB |
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. | |
287 | */ | |
549e9569 NB |
288 | check_degraded = 1; |
289 | } | |
290 | ||
291 | if (a->curr_action == idle && | |
292 | a->prev_action == recover) { | |
293 | for (mdi = a->info.devs ; mdi ; mdi = mdi->next) { | |
8d45d196 DW |
294 | a->container->ss->set_disk(a, mdi->disk.raid_disk, |
295 | mdi->curr_state); | |
549e9569 NB |
296 | if (! (mdi->curr_state & DS_INSYNC)) |
297 | check_degraded = 1; | |
298 | } | |
299 | } | |
300 | ||
301 | ||
302 | for (mdi = a->info.devs ; mdi ; mdi = mdi->next) { | |
303 | if (mdi->curr_state & DS_FAULTY) { | |
8d45d196 DW |
304 | a->container->ss->set_disk(a, mdi->disk.raid_disk, |
305 | mdi->curr_state); | |
549e9569 NB |
306 | check_degraded = 1; |
307 | mdi->next_state = DS_REMOVE; | |
308 | } | |
309 | } | |
310 | ||
311 | if (check_degraded) { | |
312 | // FIXME; | |
313 | } | |
314 | ||
315 | a->container->ss->sync_metadata(a); | |
316 | ||
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) { | |
8d45d196 DW |
323 | if (mdi->next_state == DS_REMOVE && mdi->state_fd > 0) { |
324 | int remove_err; | |
325 | ||
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 | |
329 | * again. | |
330 | */ | |
331 | remove_err = write_attr("remove", mdi->state_fd); | |
332 | if (!remove_err) { | |
333 | close(mdi->state_fd); | |
334 | mdi->state_fd = -1; | |
335 | } | |
336 | } | |
549e9569 NB |
337 | if (mdi->next_state & DS_INSYNC) |
338 | write_attr("+in_sync", mdi->state_fd); | |
339 | } | |
340 | ||
341 | /* move curr_ to prev_ */ | |
342 | a->prev_state = a->curr_state; | |
343 | ||
344 | a->prev_action = a->curr_action; | |
345 | ||
346 | for (mdi = a->info.devs; mdi ; mdi = mdi->next) { | |
347 | mdi->prev_state = mdi->curr_state; | |
348 | mdi->next_state = 0; | |
349 | } | |
350 | ||
2a0bb19e DW |
351 | if (deactivate) |
352 | a->container = NULL; | |
353 | ||
549e9569 NB |
354 | return 1; |
355 | } | |
356 | ||
0af73f61 DW |
357 | static struct mdinfo * |
358 | find_device(struct active_array *a, int major, int minor) | |
359 | { | |
360 | struct mdinfo *mdi; | |
361 | ||
362 | for (mdi = a->info.devs ; mdi ; mdi = mdi->next) | |
363 | if (mdi->disk.major == major && mdi->disk.minor == minor) | |
364 | return mdi; | |
365 | ||
366 | return NULL; | |
367 | } | |
368 | ||
369 | static void reconcile_failed(struct active_array *aa, struct mdinfo *failed) | |
370 | { | |
371 | struct active_array *a; | |
372 | struct mdinfo *victim; | |
373 | ||
374 | for (a = aa; a; a = a->next) { | |
375 | if (!a->container) | |
376 | continue; | |
377 | victim = find_device(a, failed->disk.major, failed->disk.minor); | |
378 | if (!victim) | |
379 | continue; | |
380 | ||
381 | if (!(victim->curr_state & DS_FAULTY)) | |
382 | write_attr("faulty", victim->state_fd); | |
383 | } | |
384 | } | |
385 | ||
3e70c845 DW |
386 | static int handle_remove_device(struct md_remove_device_cmd *cmd, struct active_array *aa) |
387 | { | |
388 | struct active_array *a; | |
389 | struct mdinfo *victim; | |
390 | int rv; | |
391 | ||
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. | |
395 | */ | |
396 | ||
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 | |
399 | * container | |
400 | */ | |
401 | for (a = aa; a; a = a->next) { | |
402 | if (!a->container) | |
403 | continue; | |
404 | victim = find_device(a, major(cmd->rdev), minor(cmd->rdev)); | |
405 | if (!victim) | |
406 | continue; | |
407 | if (victim->state_fd > 0) | |
408 | return -EBUSY; | |
409 | } | |
410 | ||
411 | /* pass2 schedule and process removal per array */ | |
412 | for (a = aa; a; a = a->next) { | |
413 | if (!a->container) | |
414 | continue; | |
415 | victim = find_device(a, major(cmd->rdev), minor(cmd->rdev)); | |
416 | if (!victim) | |
417 | continue; | |
418 | victim->curr_state |= DS_REMOVE; | |
419 | rv = read_and_act(a); | |
420 | if (rv < 0) | |
421 | return rv; | |
422 | } | |
423 | ||
424 | return 0; | |
425 | } | |
426 | ||
427 | static int handle_pipe(struct md_generic_cmd *cmd, struct active_array *aa) | |
428 | { | |
429 | switch (cmd->action) { | |
430 | case md_action_ping_monitor: | |
431 | return 0; | |
432 | case md_action_remove_device: | |
433 | return handle_remove_device((void *) cmd, aa); | |
434 | } | |
435 | ||
436 | return -1; | |
437 | } | |
438 | ||
e0d6609f | 439 | static int wait_and_act(struct supertype *container, int pfd, |
1ed3f387 | 440 | int monfd, int nowait) |
549e9569 NB |
441 | { |
442 | fd_set rfds; | |
443 | int maxfd = 0; | |
e0d6609f | 444 | struct active_array **aap = &container->arrays; |
1ed3f387 | 445 | struct active_array *a, **ap; |
549e9569 | 446 | int rv; |
0af73f61 | 447 | struct mdinfo *mdi; |
549e9569 NB |
448 | |
449 | FD_ZERO(&rfds); | |
450 | ||
451 | add_fd(&rfds, &maxfd, pfd); | |
1ed3f387 NB |
452 | for (ap = aap ; *ap ;) { |
453 | a = *ap; | |
454 | /* once an array has been deactivated we want to | |
455 | * ask the manager to discard it. | |
2a0bb19e | 456 | */ |
1ed3f387 NB |
457 | if (!a->container) { |
458 | if (discard_this) { | |
459 | ap = &(*ap)->next; | |
460 | continue; | |
461 | } | |
462 | *ap = a->next; | |
463 | a->next = NULL; | |
464 | discard_this = a; | |
465 | signal_manager(); | |
2a0bb19e | 466 | continue; |
1ed3f387 | 467 | } |
2a0bb19e | 468 | |
549e9569 NB |
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); | |
1ed3f387 NB |
473 | |
474 | ap = &(*ap)->next; | |
549e9569 NB |
475 | } |
476 | ||
e0d6609f NB |
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. | |
482 | */ | |
483 | int fd = open(container->device_name, O_RDONLY|O_EXCL); | |
484 | if (fd >= 0 || errno != EBUSY) { | |
485 | /* OK, we are safe to leave */ | |
486 | exit_now = 1; | |
487 | signal_manager(); | |
488 | remove_pidfile(container->devname); | |
489 | exit(0); | |
490 | } | |
491 | } | |
492 | ||
549e9569 NB |
493 | if (!nowait) { |
494 | rv = select(maxfd+1, &rfds, NULL, NULL, NULL); | |
495 | ||
496 | if (rv <= 0) | |
497 | return rv; | |
498 | ||
499 | if (FD_ISSET(pfd, &rfds)) { | |
3e70c845 DW |
500 | int err = -1; |
501 | ||
502 | if (read(pfd, &err, 1) > 0) | |
1ed3f387 | 503 | err = handle_pipe(active_cmd, *aap); |
3e70c845 | 504 | write(monfd, &err, 1); |
549e9569 NB |
505 | } |
506 | } | |
507 | ||
1ed3f387 | 508 | for (a = *aap; a ; a = a->next) { |
2a0bb19e | 509 | if (a->replaces && !discard_this) { |
549e9569 NB |
510 | struct active_array **ap; |
511 | for (ap = &a->next; *ap && *ap != a->replaces; | |
512 | ap = & (*ap)->next) | |
513 | ; | |
514 | if (*ap) | |
515 | *ap = (*ap)->next; | |
516 | discard_this = a->replaces; | |
517 | a->replaces = NULL; | |
1ed3f387 | 518 | signal_manager(); |
549e9569 | 519 | } |
2a0bb19e DW |
520 | if (a->container) |
521 | rv += read_and_act(a); | |
549e9569 | 522 | } |
0af73f61 DW |
523 | |
524 | /* propagate failures across container members */ | |
1ed3f387 | 525 | for (a = *aap; a ; a = a->next) { |
0af73f61 DW |
526 | if (!a->container) |
527 | continue; | |
528 | for (mdi = a->info.devs ; mdi ; mdi = mdi->next) | |
529 | if (mdi->curr_state & DS_FAULTY) | |
1ed3f387 | 530 | reconcile_failed(*aap, mdi); |
0af73f61 DW |
531 | } |
532 | ||
549e9569 NB |
533 | return rv; |
534 | } | |
535 | ||
536 | void do_monitor(struct supertype *container) | |
537 | { | |
538 | int rv; | |
539 | int first = 1; | |
540 | do { | |
e0d6609f | 541 | rv = wait_and_act(container, container->mgr_pipe[0], |
3e70c845 | 542 | container->mon_pipe[1], first); |
549e9569 NB |
543 | first = 0; |
544 | } while (rv >= 0); | |
545 | } |