]>
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 | ||
549e9569 | 50 | |
c052ba30 DW |
51 | static int get_resync_start(struct active_array *a) |
52 | { | |
53 | char buf[30]; | |
54 | int n; | |
55 | ||
56 | n = read_attr(buf, 30, a->resync_start_fd); | |
57 | if (n <= 0) | |
58 | return n; | |
59 | ||
60 | a->resync_start = strtoull(buf, NULL, 10); | |
61 | ||
62 | return 1; | |
63 | } | |
549e9569 NB |
64 | |
65 | static int attr_match(const char *attr, const char *str) | |
66 | { | |
67 | /* See if attr, read from a sysfs file, matches | |
68 | * str. They must either be the same, or attr can | |
69 | * have a trailing newline or comma | |
70 | */ | |
71 | while (*attr && *str && *attr == *str) { | |
72 | attr++; | |
73 | str++; | |
74 | } | |
75 | ||
76 | if (*str || (*attr && *attr != ',' && *attr != '\n')) | |
77 | return 0; | |
78 | return 1; | |
79 | } | |
80 | ||
81 | static int match_word(const char *word, char **list) | |
82 | { | |
83 | int n; | |
84 | for (n=0; list[n]; n++) | |
85 | if (attr_match(word, list[n])) | |
86 | break; | |
87 | return n; | |
88 | } | |
89 | ||
90 | static enum array_state read_state(int fd) | |
91 | { | |
92 | char buf[20]; | |
93 | int n = read_attr(buf, 20, fd); | |
94 | ||
95 | if (n <= 0) | |
96 | return bad_word; | |
97 | return (enum array_state) match_word(buf, array_states); | |
98 | } | |
99 | ||
100 | static enum sync_action read_action( int fd) | |
101 | { | |
102 | char buf[20]; | |
103 | int n = read_attr(buf, 20, fd); | |
104 | ||
105 | if (n <= 0) | |
106 | return bad_action; | |
107 | return (enum sync_action) match_word(buf, sync_actions); | |
108 | } | |
109 | ||
549e9569 NB |
110 | int read_dev_state(int fd) |
111 | { | |
112 | char buf[60]; | |
113 | int n = read_attr(buf, 60, fd); | |
114 | char *cp; | |
115 | int rv = 0; | |
116 | ||
117 | if (n <= 0) | |
118 | return 0; | |
119 | ||
120 | cp = buf; | |
121 | while (cp) { | |
8d45d196 | 122 | if (attr_match(cp, "faulty")) |
549e9569 | 123 | rv |= DS_FAULTY; |
8d45d196 | 124 | if (attr_match(cp, "in_sync")) |
549e9569 | 125 | rv |= DS_INSYNC; |
8d45d196 | 126 | if (attr_match(cp, "write_mostly")) |
549e9569 | 127 | rv |= DS_WRITE_MOSTLY; |
8d45d196 | 128 | if (attr_match(cp, "spare")) |
549e9569 | 129 | rv |= DS_SPARE; |
8d45d196 DW |
130 | if (attr_match(cp, "blocked")) |
131 | rv |= DS_BLOCKED; | |
549e9569 NB |
132 | cp = strchr(cp, ','); |
133 | if (cp) | |
134 | cp++; | |
135 | } | |
136 | return rv; | |
137 | } | |
138 | ||
1ed3f387 NB |
139 | static void signal_manager(void) |
140 | { | |
141 | kill(getpid(), SIGUSR1); | |
142 | } | |
549e9569 NB |
143 | |
144 | /* Monitor a set of active md arrays - all of which share the | |
145 | * same metadata - and respond to events that require | |
146 | * metadata update. | |
147 | * | |
148 | * New arrays are detected by another thread which allocates | |
149 | * required memory and attaches the data structure to our list. | |
150 | * | |
151 | * Events: | |
152 | * Array stops. | |
153 | * This is detected by array_state going to 'clear' or 'inactive'. | |
154 | * while we thought it was active. | |
155 | * Response is to mark metadata as clean and 'clear' the array(??) | |
156 | * write-pending | |
157 | * array_state if 'write-pending' | |
158 | * We mark metadata as 'dirty' then set array to 'active'. | |
159 | * active_idle | |
160 | * Either ignore, or mark clean, then mark metadata as clean. | |
161 | * | |
162 | * device fails | |
163 | * detected by rd-N/state reporting "faulty" | |
8d45d196 DW |
164 | * mark device as 'failed' in metadata, let the kernel release the |
165 | * device by writing '-blocked' to rd/state, and finally write 'remove' to | |
0af73f61 DW |
166 | * rd/state. Before a disk can be replaced it must be failed and removed |
167 | * from all container members, this will be preemptive for the other | |
168 | * arrays... safe? | |
549e9569 NB |
169 | * |
170 | * sync completes | |
171 | * sync_action was 'resync' and becomes 'idle' and resync_start becomes | |
172 | * MaxSector | |
173 | * Notify metadata that sync is complete. | |
549e9569 NB |
174 | * |
175 | * recovery completes | |
176 | * sync_action changes from 'recover' to 'idle' | |
177 | * Check each device state and mark metadata if 'faulty' or 'in_sync'. | |
549e9569 NB |
178 | * |
179 | * deal with resync | |
c052ba30 DW |
180 | * This only happens on finding a new array... mdadm will have set |
181 | * 'resync_start' to the correct value. If 'resync_start' indicates that an | |
182 | * resync needs to occur set the array to the 'active' state rather than the | |
183 | * initial read-auto state. | |
549e9569 NB |
184 | * |
185 | * | |
186 | * | |
187 | * We wait for a change (poll/select) on array_state, sync_action, and | |
188 | * each rd-X/state file. | |
189 | * When we get any change, we check everything. So read each state file, | |
190 | * then decide what to do. | |
191 | * | |
192 | * The core action is to write new metadata to all devices in the array. | |
193 | * This is done at most once on any wakeup. | |
194 | * After that we might: | |
195 | * - update the array_state | |
196 | * - set the role of some devices. | |
197 | * - request a sync_action | |
198 | * | |
199 | */ | |
200 | ||
201 | static int read_and_act(struct active_array *a) | |
202 | { | |
6c3fb95c | 203 | int check_degraded = 0; |
2a0bb19e | 204 | int deactivate = 0; |
549e9569 NB |
205 | struct mdinfo *mdi; |
206 | ||
207 | a->next_state = bad_word; | |
208 | a->next_action = bad_action; | |
209 | ||
210 | a->curr_state = read_state(a->info.state_fd); | |
211 | a->curr_action = read_action(a->action_fd); | |
212 | for (mdi = a->info.devs; mdi ; mdi = mdi->next) { | |
213 | mdi->next_state = 0; | |
57632f4a | 214 | if (mdi->state_fd >= 0) |
8d45d196 | 215 | mdi->curr_state = read_dev_state(mdi->state_fd); |
549e9569 NB |
216 | } |
217 | ||
218 | if (a->curr_state <= inactive && | |
219 | a->prev_state > inactive) { | |
220 | /* array has been stopped */ | |
ed9d66aa | 221 | a->container->ss->set_array_state(a, 1); |
549e9569 | 222 | a->next_state = clear; |
2a0bb19e | 223 | deactivate = 1; |
549e9569 NB |
224 | } |
225 | if (a->curr_state == write_pending) { | |
ed9d66aa NB |
226 | get_resync_start(a); |
227 | a->container->ss->set_array_state(a, 0); | |
549e9569 NB |
228 | a->next_state = active; |
229 | } | |
230 | if (a->curr_state == active_idle) { | |
231 | /* Set array to 'clean' FIRST, then | |
4e5528c6 | 232 | * a->ss->mark_clean(a, ~0ULL); |
549e9569 NB |
233 | * just ignore for now. |
234 | */ | |
235 | } | |
236 | ||
237 | if (a->curr_state == readonly) { | |
238 | /* Well, I'm ready to handle things, so | |
239 | * read-auto is OK. FIXME what if we really want | |
240 | * readonly ??? | |
241 | */ | |
c052ba30 | 242 | get_resync_start(a); |
ed9d66aa | 243 | printf("Found a readonly array at %llu\n", a->resync_start); |
c052ba30 DW |
244 | if (a->resync_start == ~0ULL) |
245 | a->next_state = read_auto; /* array is clean */ | |
246 | else { | |
ed9d66aa | 247 | a->container->ss->set_array_state(a, 0); |
c052ba30 DW |
248 | a->next_state = active; |
249 | } | |
549e9569 NB |
250 | } |
251 | ||
252 | if (a->curr_action == idle && | |
253 | a->prev_action == resync) { | |
4e5528c6 NB |
254 | /* A resync has finished. The endpoint is recorded in |
255 | * 'sync_start'. We don't update the metadata | |
256 | * until the array goes inactive or readonly though. | |
257 | * Just check if we need to fiddle spares. | |
258 | */ | |
77402e51 | 259 | get_resync_start(a); |
ed9d66aa | 260 | a->container->ss->set_array_state(a, 0); |
549e9569 NB |
261 | check_degraded = 1; |
262 | } | |
263 | ||
264 | if (a->curr_action == idle && | |
265 | a->prev_action == recover) { | |
266 | for (mdi = a->info.devs ; mdi ; mdi = mdi->next) { | |
8d45d196 DW |
267 | a->container->ss->set_disk(a, mdi->disk.raid_disk, |
268 | mdi->curr_state); | |
549e9569 NB |
269 | if (! (mdi->curr_state & DS_INSYNC)) |
270 | check_degraded = 1; | |
271 | } | |
272 | } | |
273 | ||
549e9569 NB |
274 | for (mdi = a->info.devs ; mdi ; mdi = mdi->next) { |
275 | if (mdi->curr_state & DS_FAULTY) { | |
8d45d196 DW |
276 | a->container->ss->set_disk(a, mdi->disk.raid_disk, |
277 | mdi->curr_state); | |
549e9569 NB |
278 | check_degraded = 1; |
279 | mdi->next_state = DS_REMOVE; | |
280 | } | |
281 | } | |
282 | ||
2e735d19 | 283 | a->container->ss->sync_metadata(a->container); |
4e6e574a | 284 | dprintf("%s: update[%d]: (", __func__, a->info.container_member); |
549e9569 NB |
285 | |
286 | /* Effect state changes in the array */ | |
4e6e574a DW |
287 | if (a->next_state != bad_word) { |
288 | dprintf(" state:%s", array_states[a->next_state]); | |
549e9569 | 289 | write_attr(array_states[a->next_state], a->info.state_fd); |
4e6e574a DW |
290 | } |
291 | if (a->next_action != bad_action) { | |
549e9569 | 292 | write_attr(sync_actions[a->next_action], a->action_fd); |
4e6e574a DW |
293 | dprintf(" action:%s", array_states[a->next_state]); |
294 | } | |
549e9569 | 295 | for (mdi = a->info.devs; mdi ; mdi = mdi->next) { |
57632f4a NB |
296 | if (mdi->next_state == DS_REMOVE && mdi->state_fd >= 0) { |
297 | int remove_result; | |
8d45d196 DW |
298 | |
299 | write_attr("-blocked", mdi->state_fd); | |
300 | /* the kernel may not be able to immediately remove the | |
301 | * disk, we can simply wait until the next event to try | |
302 | * again. | |
303 | */ | |
4e6e574a | 304 | dprintf(" %d:-blocked", mdi->disk.raid_disk); |
57632f4a NB |
305 | remove_result = write_attr("remove", mdi->state_fd); |
306 | if (remove_result > 0) { | |
4e6e574a | 307 | dprintf(" %d:removed", mdi->disk.raid_disk); |
8d45d196 DW |
308 | close(mdi->state_fd); |
309 | mdi->state_fd = -1; | |
310 | } | |
311 | } | |
4e6e574a | 312 | if (mdi->next_state & DS_INSYNC) { |
549e9569 | 313 | write_attr("+in_sync", mdi->state_fd); |
4e6e574a DW |
314 | dprintf(" %d:+in_sync", mdi->disk.raid_disk); |
315 | } | |
549e9569 | 316 | } |
4e6e574a | 317 | dprintf(" )\n"); |
549e9569 NB |
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 | ||
7e1432fb NB |
329 | if (check_degraded) { |
330 | /* manager will do the actual check */ | |
331 | a->check_degraded = 1; | |
332 | signal_manager(); | |
333 | } | |
334 | ||
2a0bb19e DW |
335 | if (deactivate) |
336 | a->container = NULL; | |
337 | ||
549e9569 NB |
338 | return 1; |
339 | } | |
340 | ||
0af73f61 DW |
341 | static struct mdinfo * |
342 | find_device(struct active_array *a, int major, int minor) | |
343 | { | |
344 | struct mdinfo *mdi; | |
345 | ||
346 | for (mdi = a->info.devs ; mdi ; mdi = mdi->next) | |
347 | if (mdi->disk.major == major && mdi->disk.minor == minor) | |
348 | return mdi; | |
349 | ||
350 | return NULL; | |
351 | } | |
352 | ||
353 | static void reconcile_failed(struct active_array *aa, struct mdinfo *failed) | |
354 | { | |
355 | struct active_array *a; | |
356 | struct mdinfo *victim; | |
357 | ||
358 | for (a = aa; a; a = a->next) { | |
359 | if (!a->container) | |
360 | continue; | |
361 | victim = find_device(a, failed->disk.major, failed->disk.minor); | |
362 | if (!victim) | |
363 | continue; | |
364 | ||
365 | if (!(victim->curr_state & DS_FAULTY)) | |
366 | write_attr("faulty", victim->state_fd); | |
367 | } | |
368 | } | |
369 | ||
3e70c845 DW |
370 | static int handle_remove_device(struct md_remove_device_cmd *cmd, struct active_array *aa) |
371 | { | |
372 | struct active_array *a; | |
373 | struct mdinfo *victim; | |
374 | int rv; | |
375 | ||
376 | /* scan all arrays for the given device, if ->state_fd is closed (-1) | |
377 | * in all cases then mark the disk as removed in the metadata. | |
378 | * Otherwise reply that it is busy. | |
379 | */ | |
380 | ||
381 | /* pass1 check that it is not in use anywhere */ | |
382 | /* note: we are safe from re-adds as long as the device exists in the | |
383 | * container | |
384 | */ | |
385 | for (a = aa; a; a = a->next) { | |
386 | if (!a->container) | |
387 | continue; | |
388 | victim = find_device(a, major(cmd->rdev), minor(cmd->rdev)); | |
389 | if (!victim) | |
390 | continue; | |
391 | if (victim->state_fd > 0) | |
392 | return -EBUSY; | |
393 | } | |
394 | ||
395 | /* pass2 schedule and process removal per array */ | |
396 | for (a = aa; a; a = a->next) { | |
397 | if (!a->container) | |
398 | continue; | |
399 | victim = find_device(a, major(cmd->rdev), minor(cmd->rdev)); | |
400 | if (!victim) | |
401 | continue; | |
402 | victim->curr_state |= DS_REMOVE; | |
403 | rv = read_and_act(a); | |
404 | if (rv < 0) | |
405 | return rv; | |
406 | } | |
407 | ||
408 | return 0; | |
409 | } | |
410 | ||
411 | static int handle_pipe(struct md_generic_cmd *cmd, struct active_array *aa) | |
412 | { | |
413 | switch (cmd->action) { | |
414 | case md_action_ping_monitor: | |
415 | return 0; | |
416 | case md_action_remove_device: | |
417 | return handle_remove_device((void *) cmd, aa); | |
418 | } | |
419 | ||
420 | return -1; | |
421 | } | |
422 | ||
4e6e574a DW |
423 | #ifdef DEBUG |
424 | static void dprint_wake_reasons(fd_set *fds) | |
425 | { | |
426 | int i; | |
427 | char proc_path[256]; | |
428 | char link[256]; | |
429 | char *basename; | |
430 | int rv; | |
431 | ||
432 | fprintf(stderr, "monitor: wake ( "); | |
433 | for (i = 0; i < FD_SETSIZE; i++) { | |
434 | if (FD_ISSET(i, fds)) { | |
435 | sprintf(proc_path, "/proc/%d/fd/%d", | |
436 | (int) getpid(), i); | |
437 | ||
438 | rv = readlink(proc_path, link, sizeof(link) - 1); | |
439 | if (rv < 0) { | |
440 | fprintf(stderr, "%d:unknown ", i); | |
441 | continue; | |
442 | } | |
443 | link[rv] = '\0'; | |
444 | basename = strrchr(link, '/'); | |
445 | fprintf(stderr, "%d:%s ", | |
446 | i, basename ? ++basename : link); | |
447 | } | |
448 | } | |
449 | fprintf(stderr, ")\n"); | |
450 | } | |
451 | #endif | |
452 | ||
e0d6609f | 453 | static int wait_and_act(struct supertype *container, int pfd, |
1ed3f387 | 454 | int monfd, int nowait) |
549e9569 NB |
455 | { |
456 | fd_set rfds; | |
457 | int maxfd = 0; | |
e0d6609f | 458 | struct active_array **aap = &container->arrays; |
1ed3f387 | 459 | struct active_array *a, **ap; |
549e9569 | 460 | int rv; |
0af73f61 | 461 | struct mdinfo *mdi; |
549e9569 NB |
462 | |
463 | FD_ZERO(&rfds); | |
464 | ||
465 | add_fd(&rfds, &maxfd, pfd); | |
1ed3f387 NB |
466 | for (ap = aap ; *ap ;) { |
467 | a = *ap; | |
468 | /* once an array has been deactivated we want to | |
469 | * ask the manager to discard it. | |
2a0bb19e | 470 | */ |
1ed3f387 NB |
471 | if (!a->container) { |
472 | if (discard_this) { | |
473 | ap = &(*ap)->next; | |
474 | continue; | |
475 | } | |
476 | *ap = a->next; | |
477 | a->next = NULL; | |
478 | discard_this = a; | |
479 | signal_manager(); | |
2a0bb19e | 480 | continue; |
1ed3f387 | 481 | } |
2a0bb19e | 482 | |
549e9569 NB |
483 | add_fd(&rfds, &maxfd, a->info.state_fd); |
484 | add_fd(&rfds, &maxfd, a->action_fd); | |
485 | for (mdi = a->info.devs ; mdi ; mdi = mdi->next) | |
486 | add_fd(&rfds, &maxfd, mdi->state_fd); | |
1ed3f387 NB |
487 | |
488 | ap = &(*ap)->next; | |
549e9569 NB |
489 | } |
490 | ||
e0d6609f NB |
491 | if (manager_ready && *aap == NULL) { |
492 | /* No interesting arrays. Lets see about exiting. | |
493 | * Note that blocking at this point is not a problem | |
494 | * as there are no active arrays, there is nothing that | |
495 | * we need to be ready to do. | |
496 | */ | |
497 | int fd = open(container->device_name, O_RDONLY|O_EXCL); | |
498 | if (fd >= 0 || errno != EBUSY) { | |
499 | /* OK, we are safe to leave */ | |
4e6e574a | 500 | dprintf("no arrays to monitor... exiting\n"); |
e0d6609f NB |
501 | exit_now = 1; |
502 | signal_manager(); | |
503 | remove_pidfile(container->devname); | |
504 | exit(0); | |
505 | } | |
506 | } | |
507 | ||
549e9569 NB |
508 | if (!nowait) { |
509 | rv = select(maxfd+1, &rfds, NULL, NULL, NULL); | |
510 | ||
4e6e574a DW |
511 | #ifdef DEBUG |
512 | dprint_wake_reasons(&rfds); | |
513 | #endif | |
514 | ||
549e9569 NB |
515 | if (rv <= 0) |
516 | return rv; | |
517 | ||
518 | if (FD_ISSET(pfd, &rfds)) { | |
3e70c845 DW |
519 | int err = -1; |
520 | ||
521 | if (read(pfd, &err, 1) > 0) | |
1ed3f387 | 522 | err = handle_pipe(active_cmd, *aap); |
3e70c845 | 523 | write(monfd, &err, 1); |
549e9569 NB |
524 | } |
525 | } | |
526 | ||
2e735d19 NB |
527 | if (update_queue) { |
528 | struct metadata_update *this; | |
529 | ||
530 | for (this = update_queue; this ; this = this->next) | |
531 | container->ss->process_update(container, this); | |
532 | ||
533 | update_queue_handled = update_queue; | |
534 | update_queue = NULL; | |
535 | signal_manager(); | |
536 | container->ss->sync_metadata(container); | |
537 | } | |
538 | ||
1ed3f387 | 539 | for (a = *aap; a ; a = a->next) { |
2a0bb19e | 540 | if (a->replaces && !discard_this) { |
549e9569 NB |
541 | struct active_array **ap; |
542 | for (ap = &a->next; *ap && *ap != a->replaces; | |
543 | ap = & (*ap)->next) | |
544 | ; | |
545 | if (*ap) | |
546 | *ap = (*ap)->next; | |
547 | discard_this = a->replaces; | |
548 | a->replaces = NULL; | |
6c3fb95c | 549 | /* FIXME check if device->state_fd need to be cleared?*/ |
1ed3f387 | 550 | signal_manager(); |
549e9569 | 551 | } |
2a0bb19e DW |
552 | if (a->container) |
553 | rv += read_and_act(a); | |
549e9569 | 554 | } |
0af73f61 DW |
555 | |
556 | /* propagate failures across container members */ | |
1ed3f387 | 557 | for (a = *aap; a ; a = a->next) { |
0af73f61 DW |
558 | if (!a->container) |
559 | continue; | |
560 | for (mdi = a->info.devs ; mdi ; mdi = mdi->next) | |
561 | if (mdi->curr_state & DS_FAULTY) | |
1ed3f387 | 562 | reconcile_failed(*aap, mdi); |
0af73f61 DW |
563 | } |
564 | ||
549e9569 NB |
565 | return rv; |
566 | } | |
567 | ||
568 | void do_monitor(struct supertype *container) | |
569 | { | |
570 | int rv; | |
571 | int first = 1; | |
572 | do { | |
e0d6609f | 573 | rv = wait_and_act(container, container->mgr_pipe[0], |
3e70c845 | 574 | container->mon_pipe[1], first); |
549e9569 NB |
575 | first = 0; |
576 | } while (rv >= 0); | |
577 | } |