]>
Commit | Line | Data |
---|---|---|
549e9569 NB |
1 | |
2 | #include "mdadm.h" | |
3 | #include "mdmon.h" | |
4d43913c | 4 | #include <sys/syscall.h> |
549e9569 | 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 | |
103f2410 | 51 | int get_resync_start(struct active_array *a) |
c052ba30 DW |
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 | { | |
4d43913c NB |
141 | /* tgkill(getpid(), mon_tid, SIGUSR1); */ |
142 | int pid = getpid(); | |
143 | syscall(SYS_tgkill, pid, mgr_tid, SIGUSR1); | |
1ed3f387 | 144 | } |
549e9569 NB |
145 | |
146 | /* Monitor a set of active md arrays - all of which share the | |
147 | * same metadata - and respond to events that require | |
148 | * metadata update. | |
149 | * | |
150 | * New arrays are detected by another thread which allocates | |
151 | * required memory and attaches the data structure to our list. | |
152 | * | |
153 | * Events: | |
154 | * Array stops. | |
155 | * This is detected by array_state going to 'clear' or 'inactive'. | |
156 | * while we thought it was active. | |
157 | * Response is to mark metadata as clean and 'clear' the array(??) | |
158 | * write-pending | |
159 | * array_state if 'write-pending' | |
160 | * We mark metadata as 'dirty' then set array to 'active'. | |
161 | * active_idle | |
162 | * Either ignore, or mark clean, then mark metadata as clean. | |
163 | * | |
164 | * device fails | |
165 | * detected by rd-N/state reporting "faulty" | |
8d45d196 DW |
166 | * mark device as 'failed' in metadata, let the kernel release the |
167 | * device by writing '-blocked' to rd/state, and finally write 'remove' to | |
0af73f61 DW |
168 | * rd/state. Before a disk can be replaced it must be failed and removed |
169 | * from all container members, this will be preemptive for the other | |
170 | * arrays... safe? | |
549e9569 NB |
171 | * |
172 | * sync completes | |
173 | * sync_action was 'resync' and becomes 'idle' and resync_start becomes | |
174 | * MaxSector | |
175 | * Notify metadata that sync is complete. | |
549e9569 NB |
176 | * |
177 | * recovery completes | |
178 | * sync_action changes from 'recover' to 'idle' | |
179 | * Check each device state and mark metadata if 'faulty' or 'in_sync'. | |
549e9569 NB |
180 | * |
181 | * deal with resync | |
c052ba30 DW |
182 | * This only happens on finding a new array... mdadm will have set |
183 | * 'resync_start' to the correct value. If 'resync_start' indicates that an | |
184 | * resync needs to occur set the array to the 'active' state rather than the | |
185 | * initial read-auto state. | |
549e9569 NB |
186 | * |
187 | * | |
188 | * | |
189 | * We wait for a change (poll/select) on array_state, sync_action, and | |
190 | * each rd-X/state file. | |
191 | * When we get any change, we check everything. So read each state file, | |
192 | * then decide what to do. | |
193 | * | |
194 | * The core action is to write new metadata to all devices in the array. | |
195 | * This is done at most once on any wakeup. | |
196 | * After that we might: | |
197 | * - update the array_state | |
198 | * - set the role of some devices. | |
199 | * - request a sync_action | |
200 | * | |
201 | */ | |
202 | ||
203 | static int read_and_act(struct active_array *a) | |
204 | { | |
6c3fb95c | 205 | int check_degraded = 0; |
2a0bb19e | 206 | int deactivate = 0; |
549e9569 NB |
207 | struct mdinfo *mdi; |
208 | ||
209 | a->next_state = bad_word; | |
210 | a->next_action = bad_action; | |
211 | ||
212 | a->curr_state = read_state(a->info.state_fd); | |
213 | a->curr_action = read_action(a->action_fd); | |
214 | for (mdi = a->info.devs; mdi ; mdi = mdi->next) { | |
215 | mdi->next_state = 0; | |
57632f4a | 216 | if (mdi->state_fd >= 0) |
8d45d196 | 217 | mdi->curr_state = read_dev_state(mdi->state_fd); |
549e9569 NB |
218 | } |
219 | ||
220 | if (a->curr_state <= inactive && | |
221 | a->prev_state > inactive) { | |
222 | /* array has been stopped */ | |
33af8567 | 223 | get_resync_start(a); |
ed9d66aa | 224 | a->container->ss->set_array_state(a, 1); |
549e9569 | 225 | a->next_state = clear; |
2a0bb19e | 226 | deactivate = 1; |
549e9569 NB |
227 | } |
228 | if (a->curr_state == write_pending) { | |
ed9d66aa NB |
229 | get_resync_start(a); |
230 | a->container->ss->set_array_state(a, 0); | |
549e9569 NB |
231 | a->next_state = active; |
232 | } | |
233 | if (a->curr_state == active_idle) { | |
d797a062 DW |
234 | /* Set array to 'clean' FIRST, then mark clean |
235 | * in the metadata | |
549e9569 | 236 | */ |
d797a062 DW |
237 | a->next_state = clean; |
238 | } | |
239 | if (a->curr_state == clean) { | |
240 | get_resync_start(a); | |
241 | a->container->ss->set_array_state(a, 1); | |
549e9569 NB |
242 | } |
243 | ||
244 | if (a->curr_state == readonly) { | |
e9dd1598 N |
245 | /* Well, I'm ready to handle things. If readonly |
246 | * wasn't requested, transition to read-auto. | |
549e9569 | 247 | */ |
e9dd1598 N |
248 | char buf[64]; |
249 | read_attr(buf, sizeof(buf), a->metadata_fd); | |
250 | if (strncmp(buf, "external:-", 10) == 0) { | |
251 | /* explicit request for readonly array. Leave it alone */ | |
252 | ; | |
253 | } else { | |
254 | get_resync_start(a); | |
255 | if (a->container->ss->set_array_state(a, 2)) | |
256 | a->next_state = read_auto; /* array is clean */ | |
257 | else | |
258 | a->next_state = active; /* Now active for recovery etc */ | |
259 | } | |
549e9569 NB |
260 | } |
261 | ||
00e02142 DW |
262 | if (!deactivate && |
263 | a->curr_action == idle && | |
549e9569 | 264 | a->prev_action == resync) { |
4e5528c6 NB |
265 | /* A resync has finished. The endpoint is recorded in |
266 | * 'sync_start'. We don't update the metadata | |
267 | * until the array goes inactive or readonly though. | |
268 | * Just check if we need to fiddle spares. | |
269 | */ | |
77402e51 | 270 | get_resync_start(a); |
ed9d66aa | 271 | a->container->ss->set_array_state(a, 0); |
549e9569 NB |
272 | check_degraded = 1; |
273 | } | |
274 | ||
00e02142 DW |
275 | if (!deactivate && |
276 | a->curr_action == idle && | |
549e9569 | 277 | a->prev_action == recover) { |
0a6bdbee DW |
278 | /* A recovery has finished. Some disks may be in sync now, |
279 | * and the array may no longer be degraded | |
280 | */ | |
549e9569 | 281 | for (mdi = a->info.devs ; mdi ; mdi = mdi->next) { |
8d45d196 DW |
282 | a->container->ss->set_disk(a, mdi->disk.raid_disk, |
283 | mdi->curr_state); | |
549e9569 NB |
284 | if (! (mdi->curr_state & DS_INSYNC)) |
285 | check_degraded = 1; | |
286 | } | |
287 | } | |
288 | ||
92967543 DW |
289 | /* Check for failures and if found: |
290 | * 1/ Record the failure in the metadata and unblock the device. | |
291 | * FIXME update the kernel to stop notifying on failed drives when | |
292 | * the array is readonly and we have cleared 'blocked' | |
293 | * 2/ Try to remove the device if the array is writable, or can be | |
294 | * made writable. | |
295 | */ | |
549e9569 NB |
296 | for (mdi = a->info.devs ; mdi ; mdi = mdi->next) { |
297 | if (mdi->curr_state & DS_FAULTY) { | |
8d45d196 DW |
298 | a->container->ss->set_disk(a, mdi->disk.raid_disk, |
299 | mdi->curr_state); | |
549e9569 | 300 | check_degraded = 1; |
92967543 DW |
301 | mdi->next_state |= DS_UNBLOCK; |
302 | if (a->curr_state == read_auto) { | |
303 | a->container->ss->set_array_state(a, 0); | |
304 | a->next_state = active; | |
305 | } | |
306 | if (a->curr_state > readonly) | |
307 | mdi->next_state |= DS_REMOVE; | |
549e9569 NB |
308 | } |
309 | } | |
310 | ||
2e735d19 | 311 | a->container->ss->sync_metadata(a->container); |
4e6e574a | 312 | dprintf("%s: update[%d]: (", __func__, a->info.container_member); |
549e9569 NB |
313 | |
314 | /* Effect state changes in the array */ | |
4e6e574a DW |
315 | if (a->next_state != bad_word) { |
316 | dprintf(" state:%s", array_states[a->next_state]); | |
549e9569 | 317 | write_attr(array_states[a->next_state], a->info.state_fd); |
4e6e574a DW |
318 | } |
319 | if (a->next_action != bad_action) { | |
549e9569 | 320 | write_attr(sync_actions[a->next_action], a->action_fd); |
4e6e574a DW |
321 | dprintf(" action:%s", array_states[a->next_state]); |
322 | } | |
549e9569 | 323 | for (mdi = a->info.devs; mdi ; mdi = mdi->next) { |
92967543 DW |
324 | if (mdi->next_state & DS_UNBLOCK) { |
325 | dprintf(" %d:-blocked", mdi->disk.raid_disk); | |
326 | write_attr("-blocked", mdi->state_fd); | |
327 | } | |
328 | ||
329 | if ((mdi->next_state & DS_REMOVE) && mdi->state_fd >= 0) { | |
57632f4a | 330 | int remove_result; |
8d45d196 | 331 | |
8d45d196 DW |
332 | /* the kernel may not be able to immediately remove the |
333 | * disk, we can simply wait until the next event to try | |
334 | * again. | |
335 | */ | |
57632f4a NB |
336 | remove_result = write_attr("remove", mdi->state_fd); |
337 | if (remove_result > 0) { | |
4e6e574a | 338 | dprintf(" %d:removed", mdi->disk.raid_disk); |
8d45d196 DW |
339 | close(mdi->state_fd); |
340 | mdi->state_fd = -1; | |
341 | } | |
342 | } | |
4e6e574a | 343 | if (mdi->next_state & DS_INSYNC) { |
549e9569 | 344 | write_attr("+in_sync", mdi->state_fd); |
4e6e574a DW |
345 | dprintf(" %d:+in_sync", mdi->disk.raid_disk); |
346 | } | |
549e9569 | 347 | } |
4e6e574a | 348 | dprintf(" )\n"); |
549e9569 NB |
349 | |
350 | /* move curr_ to prev_ */ | |
351 | a->prev_state = a->curr_state; | |
352 | ||
353 | a->prev_action = a->curr_action; | |
354 | ||
355 | for (mdi = a->info.devs; mdi ; mdi = mdi->next) { | |
356 | mdi->prev_state = mdi->curr_state; | |
357 | mdi->next_state = 0; | |
358 | } | |
359 | ||
7e1432fb NB |
360 | if (check_degraded) { |
361 | /* manager will do the actual check */ | |
362 | a->check_degraded = 1; | |
363 | signal_manager(); | |
364 | } | |
365 | ||
2a0bb19e DW |
366 | if (deactivate) |
367 | a->container = NULL; | |
368 | ||
549e9569 NB |
369 | return 1; |
370 | } | |
371 | ||
0af73f61 DW |
372 | static struct mdinfo * |
373 | find_device(struct active_array *a, int major, int minor) | |
374 | { | |
375 | struct mdinfo *mdi; | |
376 | ||
377 | for (mdi = a->info.devs ; mdi ; mdi = mdi->next) | |
378 | if (mdi->disk.major == major && mdi->disk.minor == minor) | |
379 | return mdi; | |
380 | ||
381 | return NULL; | |
382 | } | |
383 | ||
384 | static void reconcile_failed(struct active_array *aa, struct mdinfo *failed) | |
385 | { | |
386 | struct active_array *a; | |
387 | struct mdinfo *victim; | |
388 | ||
389 | for (a = aa; a; a = a->next) { | |
390 | if (!a->container) | |
391 | continue; | |
392 | victim = find_device(a, failed->disk.major, failed->disk.minor); | |
393 | if (!victim) | |
394 | continue; | |
395 | ||
396 | if (!(victim->curr_state & DS_FAULTY)) | |
397 | write_attr("faulty", victim->state_fd); | |
398 | } | |
399 | } | |
400 | ||
4e6e574a DW |
401 | #ifdef DEBUG |
402 | static void dprint_wake_reasons(fd_set *fds) | |
403 | { | |
404 | int i; | |
405 | char proc_path[256]; | |
406 | char link[256]; | |
407 | char *basename; | |
408 | int rv; | |
409 | ||
410 | fprintf(stderr, "monitor: wake ( "); | |
411 | for (i = 0; i < FD_SETSIZE; i++) { | |
412 | if (FD_ISSET(i, fds)) { | |
413 | sprintf(proc_path, "/proc/%d/fd/%d", | |
414 | (int) getpid(), i); | |
415 | ||
416 | rv = readlink(proc_path, link, sizeof(link) - 1); | |
417 | if (rv < 0) { | |
418 | fprintf(stderr, "%d:unknown ", i); | |
419 | continue; | |
420 | } | |
421 | link[rv] = '\0'; | |
422 | basename = strrchr(link, '/'); | |
423 | fprintf(stderr, "%d:%s ", | |
424 | i, basename ? ++basename : link); | |
425 | } | |
426 | } | |
427 | fprintf(stderr, ")\n"); | |
428 | } | |
429 | #endif | |
430 | ||
1eb252b8 N |
431 | int monitor_loop_cnt; |
432 | ||
4d43913c | 433 | static int wait_and_act(struct supertype *container, int nowait) |
549e9569 NB |
434 | { |
435 | fd_set rfds; | |
436 | int maxfd = 0; | |
e0d6609f | 437 | struct active_array **aap = &container->arrays; |
1ed3f387 | 438 | struct active_array *a, **ap; |
549e9569 | 439 | int rv; |
0af73f61 | 440 | struct mdinfo *mdi; |
549e9569 NB |
441 | |
442 | FD_ZERO(&rfds); | |
443 | ||
1ed3f387 NB |
444 | for (ap = aap ; *ap ;) { |
445 | a = *ap; | |
446 | /* once an array has been deactivated we want to | |
447 | * ask the manager to discard it. | |
2a0bb19e | 448 | */ |
1ed3f387 NB |
449 | if (!a->container) { |
450 | if (discard_this) { | |
451 | ap = &(*ap)->next; | |
452 | continue; | |
453 | } | |
454 | *ap = a->next; | |
455 | a->next = NULL; | |
456 | discard_this = a; | |
457 | signal_manager(); | |
2a0bb19e | 458 | continue; |
1ed3f387 | 459 | } |
2a0bb19e | 460 | |
549e9569 NB |
461 | add_fd(&rfds, &maxfd, a->info.state_fd); |
462 | add_fd(&rfds, &maxfd, a->action_fd); | |
463 | for (mdi = a->info.devs ; mdi ; mdi = mdi->next) | |
464 | add_fd(&rfds, &maxfd, mdi->state_fd); | |
1ed3f387 NB |
465 | |
466 | ap = &(*ap)->next; | |
549e9569 NB |
467 | } |
468 | ||
e0d6609f NB |
469 | if (manager_ready && *aap == NULL) { |
470 | /* No interesting arrays. Lets see about exiting. | |
471 | * Note that blocking at this point is not a problem | |
472 | * as there are no active arrays, there is nothing that | |
473 | * we need to be ready to do. | |
474 | */ | |
475 | int fd = open(container->device_name, O_RDONLY|O_EXCL); | |
476 | if (fd >= 0 || errno != EBUSY) { | |
477 | /* OK, we are safe to leave */ | |
4e6e574a | 478 | dprintf("no arrays to monitor... exiting\n"); |
207aac36 | 479 | remove_pidfile(container->devname); |
e0d6609f NB |
480 | exit_now = 1; |
481 | signal_manager(); | |
e0d6609f NB |
482 | exit(0); |
483 | } | |
484 | } | |
485 | ||
549e9569 | 486 | if (!nowait) { |
4d43913c NB |
487 | sigset_t set; |
488 | sigprocmask(SIG_UNBLOCK, NULL, &set); | |
489 | sigdelset(&set, SIGUSR1); | |
1eb252b8 | 490 | monitor_loop_cnt |= 1; |
4d43913c | 491 | rv = pselect(maxfd+1, &rfds, NULL, NULL, NULL, &set); |
1eb252b8 | 492 | monitor_loop_cnt += 1; |
bfa44e2e NB |
493 | if (rv == -1 && errno == EINTR) |
494 | rv = 0; | |
4e6e574a DW |
495 | #ifdef DEBUG |
496 | dprint_wake_reasons(&rfds); | |
497 | #endif | |
498 | ||
549e9569 NB |
499 | } |
500 | ||
2e735d19 NB |
501 | if (update_queue) { |
502 | struct metadata_update *this; | |
503 | ||
504 | for (this = update_queue; this ; this = this->next) | |
505 | container->ss->process_update(container, this); | |
506 | ||
507 | update_queue_handled = update_queue; | |
508 | update_queue = NULL; | |
509 | signal_manager(); | |
510 | container->ss->sync_metadata(container); | |
511 | } | |
512 | ||
1ed3f387 | 513 | for (a = *aap; a ; a = a->next) { |
2a0bb19e | 514 | if (a->replaces && !discard_this) { |
549e9569 NB |
515 | struct active_array **ap; |
516 | for (ap = &a->next; *ap && *ap != a->replaces; | |
517 | ap = & (*ap)->next) | |
518 | ; | |
519 | if (*ap) | |
520 | *ap = (*ap)->next; | |
521 | discard_this = a->replaces; | |
522 | a->replaces = NULL; | |
6c3fb95c | 523 | /* FIXME check if device->state_fd need to be cleared?*/ |
1ed3f387 | 524 | signal_manager(); |
549e9569 | 525 | } |
2a0bb19e DW |
526 | if (a->container) |
527 | rv += read_and_act(a); | |
549e9569 | 528 | } |
0af73f61 DW |
529 | |
530 | /* propagate failures across container members */ | |
1ed3f387 | 531 | for (a = *aap; a ; a = a->next) { |
0af73f61 DW |
532 | if (!a->container) |
533 | continue; | |
534 | for (mdi = a->info.devs ; mdi ; mdi = mdi->next) | |
535 | if (mdi->curr_state & DS_FAULTY) | |
1ed3f387 | 536 | reconcile_failed(*aap, mdi); |
0af73f61 DW |
537 | } |
538 | ||
549e9569 NB |
539 | return rv; |
540 | } | |
541 | ||
542 | void do_monitor(struct supertype *container) | |
543 | { | |
544 | int rv; | |
545 | int first = 1; | |
546 | do { | |
4d43913c | 547 | rv = wait_and_act(container, first); |
549e9569 NB |
548 | first = 0; |
549 | } while (rv >= 0); | |
550 | } |