]>
Commit | Line | Data |
---|---|---|
a54d5262 DW |
1 | /* |
2 | * mdmon - monitor external metadata arrays | |
3 | * | |
e736b623 N |
4 | * Copyright (C) 2007-2009 Neil Brown <neilb@suse.de> |
5 | * Copyright (C) 2007-2009 Intel Corporation | |
a54d5262 DW |
6 | * |
7 | * This program is free software; you can redistribute it and/or modify it | |
8 | * under the terms and conditions of the GNU General Public License, | |
9 | * version 2, as published by the Free Software Foundation. | |
10 | * | |
11 | * This program is distributed in the hope it will be useful, but WITHOUT | |
12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
14 | * more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License along with | |
17 | * this program; if not, write to the Free Software Foundation, Inc., | |
18 | * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. | |
19 | */ | |
549e9569 NB |
20 | |
21 | #include "mdadm.h" | |
22 | #include "mdmon.h" | |
4d43913c | 23 | #include <sys/syscall.h> |
549e9569 | 24 | #include <sys/select.h> |
1ed3f387 | 25 | #include <signal.h> |
549e9569 NB |
26 | |
27 | static char *array_states[] = { | |
28 | "clear", "inactive", "suspended", "readonly", "read-auto", | |
29 | "clean", "active", "write-pending", "active-idle", NULL }; | |
30 | static char *sync_actions[] = { | |
31 | "idle", "reshape", "resync", "recover", "check", "repair", NULL | |
32 | }; | |
33 | ||
1ab97c97 TM |
34 | enum bb_action { |
35 | RECORD_BB = 1, | |
36 | }; | |
37 | ||
549e9569 NB |
38 | static int write_attr(char *attr, int fd) |
39 | { | |
40 | return write(fd, attr, strlen(attr)); | |
41 | } | |
42 | ||
43 | static void add_fd(fd_set *fds, int *maxfd, int fd) | |
44 | { | |
2b60d289 | 45 | struct stat st; |
549e9569 NB |
46 | if (fd < 0) |
47 | return; | |
2b60d289 | 48 | if (fstat(fd, &st) == -1) { |
1ade5cc1 | 49 | dprintf("Invalid fd %d\n", fd); |
2b60d289 | 50 | return; |
51 | } | |
52 | if (st.st_nlink == 0) { | |
1ade5cc1 | 53 | dprintf("fd %d was deleted\n", fd); |
2b60d289 | 54 | return; |
55 | } | |
549e9569 NB |
56 | if (fd > *maxfd) |
57 | *maxfd = fd; | |
58 | FD_SET(fd, fds); | |
59 | } | |
60 | ||
61 | static int read_attr(char *buf, int len, int fd) | |
62 | { | |
63 | int n; | |
64 | ||
65 | if (fd < 0) { | |
66 | buf[0] = 0; | |
67 | return 0; | |
68 | } | |
69 | lseek(fd, 0, 0); | |
70 | n = read(fd, buf, len - 1); | |
71 | ||
72 | if (n <= 0) { | |
73 | buf[0] = 0; | |
74 | return 0; | |
75 | } | |
76 | buf[n] = 0; | |
77 | if (buf[n-1] == '\n') | |
78 | buf[n-1] = 0; | |
79 | return n; | |
80 | } | |
81 | ||
40ae6f5f | 82 | static void read_resync_start(int fd, unsigned long long *v) |
c052ba30 DW |
83 | { |
84 | char buf[30]; | |
85 | int n; | |
86 | ||
b7941fd6 | 87 | n = read_attr(buf, 30, fd); |
40ae6f5f | 88 | if (n <= 0) { |
1ade5cc1 | 89 | dprintf("Failed to read resync_start (%d)\n", fd); |
40ae6f5f | 90 | return; |
91 | } | |
7e7fffc4 | 92 | if (strncmp(buf, "none", 4) == 0) |
40ae6f5f | 93 | *v = MaxSector; |
7e7fffc4 | 94 | else |
40ae6f5f | 95 | *v = strtoull(buf, NULL, 10); |
c052ba30 | 96 | } |
549e9569 | 97 | |
484240d8 DW |
98 | static unsigned long long read_sync_completed(int fd) |
99 | { | |
100 | unsigned long long val; | |
101 | char buf[50]; | |
102 | int n; | |
103 | char *ep; | |
104 | ||
105 | n = read_attr(buf, 50, fd); | |
106 | ||
107 | if (n <= 0) | |
108 | return 0; | |
109 | buf[n] = 0; | |
110 | val = strtoull(buf, &ep, 0); | |
111 | if (ep == buf || (*ep != 0 && *ep != '\n' && *ep != ' ')) | |
112 | return 0; | |
113 | return val; | |
114 | } | |
115 | ||
549e9569 NB |
116 | static enum array_state read_state(int fd) |
117 | { | |
118 | char buf[20]; | |
119 | int n = read_attr(buf, 20, fd); | |
120 | ||
121 | if (n <= 0) | |
122 | return bad_word; | |
1770662b | 123 | return (enum array_state) sysfs_match_word(buf, array_states); |
549e9569 NB |
124 | } |
125 | ||
126 | static enum sync_action read_action( int fd) | |
127 | { | |
128 | char buf[20]; | |
129 | int n = read_attr(buf, 20, fd); | |
130 | ||
131 | if (n <= 0) | |
132 | return bad_action; | |
1770662b | 133 | return (enum sync_action) sysfs_match_word(buf, sync_actions); |
549e9569 NB |
134 | } |
135 | ||
549e9569 NB |
136 | int read_dev_state(int fd) |
137 | { | |
cf52eff5 TM |
138 | char buf[100]; |
139 | int n = read_attr(buf, sizeof(buf), fd); | |
549e9569 NB |
140 | char *cp; |
141 | int rv = 0; | |
142 | ||
143 | if (n <= 0) | |
144 | return 0; | |
145 | ||
146 | cp = buf; | |
147 | while (cp) { | |
1770662b | 148 | if (sysfs_attr_match(cp, "faulty")) |
549e9569 | 149 | rv |= DS_FAULTY; |
1770662b | 150 | if (sysfs_attr_match(cp, "in_sync")) |
549e9569 | 151 | rv |= DS_INSYNC; |
1770662b | 152 | if (sysfs_attr_match(cp, "write_mostly")) |
549e9569 | 153 | rv |= DS_WRITE_MOSTLY; |
1770662b | 154 | if (sysfs_attr_match(cp, "spare")) |
549e9569 | 155 | rv |= DS_SPARE; |
1770662b | 156 | if (sysfs_attr_match(cp, "blocked")) |
8d45d196 | 157 | rv |= DS_BLOCKED; |
549e9569 NB |
158 | cp = strchr(cp, ','); |
159 | if (cp) | |
160 | cp++; | |
161 | } | |
162 | return rv; | |
163 | } | |
164 | ||
1ab97c97 TM |
165 | int process_ubb(struct active_array *a, struct mdinfo *mdi, const unsigned long |
166 | long sector, const int length, const char *buf, | |
167 | const int buf_len) | |
168 | { | |
169 | struct superswitch *ss = a->container->ss; | |
170 | ||
171 | /* | |
172 | * record bad block in metadata first, then acknowledge it to the driver | |
173 | * via sysfs file | |
174 | */ | |
175 | if ((ss->record_bad_block(a, mdi->disk.raid_disk, sector, length)) && | |
176 | (write(mdi->bb_fd, buf, buf_len) == buf_len)) | |
177 | return 1; | |
178 | ||
179 | /* | |
180 | * failed to store or acknowledge bad block, switch of bad block support | |
181 | * to get it out of blocked state | |
182 | */ | |
183 | sysfs_set_str(&a->info, mdi, "state", "-external_bbl"); | |
184 | return -1; | |
185 | } | |
186 | ||
187 | static int read_bb_file(int fd, struct active_array *a, struct mdinfo *mdi, | |
188 | enum bb_action action, void *arg) | |
189 | { | |
190 | char buf[30]; | |
191 | int n = 0; | |
192 | int ret = 0; | |
193 | int read_again = 0; | |
194 | int off = 0; | |
195 | int pos = 0; | |
196 | int preserve_pos = (action == RECORD_BB ? 0 : 1); | |
197 | ||
198 | if (lseek(fd, 0, SEEK_SET) == (off_t) -1) | |
199 | return -1; | |
200 | ||
201 | do { | |
202 | read_again = 0; | |
203 | n = read(fd, buf + pos, sizeof(buf) - 1 - pos); | |
204 | if (n < 0) | |
205 | return -1; | |
206 | n += pos; | |
207 | ||
208 | buf[n] = '\0'; | |
209 | off = 0; | |
210 | ||
211 | while (off < n) { | |
212 | unsigned long long sector; | |
213 | int length; | |
214 | char newline; | |
215 | int consumed; | |
216 | int matched; | |
217 | int rc; | |
218 | ||
219 | /* kernel sysfs file format: "sector length\n" */ | |
220 | matched = sscanf(buf + off, "%llu %d%c%n", §or, | |
221 | &length, &newline, &consumed); | |
222 | if ((matched != 3) && (off > 0)) { | |
223 | /* truncated entry, read again */ | |
224 | if (preserve_pos) { | |
225 | pos = sizeof(buf) - off - 1; | |
226 | memmove(buf, buf + off, pos); | |
227 | } else { | |
228 | if (lseek(fd, 0, SEEK_SET) == | |
229 | (off_t) -1) | |
230 | return -1; | |
231 | } | |
232 | read_again = 1; | |
233 | break; | |
234 | } | |
235 | if (matched != 3) | |
236 | return -1; | |
237 | if (newline != '\n') | |
238 | return -1; | |
239 | if (length <= 0) | |
240 | return -1; | |
241 | ||
242 | if (action == RECORD_BB) | |
243 | rc = process_ubb(a, mdi, sector, length, | |
244 | buf + off, consumed); | |
245 | else | |
246 | rc = -1; | |
247 | ||
248 | if (rc < 0) | |
249 | return rc; | |
250 | ret += rc; | |
251 | off += consumed; | |
252 | } | |
253 | } while (read_again); | |
254 | ||
255 | return ret; | |
256 | } | |
257 | ||
258 | static int process_dev_ubb(struct active_array *a, struct mdinfo *mdi) | |
259 | { | |
260 | return read_bb_file(mdi->ubb_fd, a, mdi, RECORD_BB, NULL); | |
261 | } | |
262 | ||
1ed3f387 NB |
263 | static void signal_manager(void) |
264 | { | |
4d43913c NB |
265 | /* tgkill(getpid(), mon_tid, SIGUSR1); */ |
266 | int pid = getpid(); | |
267 | syscall(SYS_tgkill, pid, mgr_tid, SIGUSR1); | |
1ed3f387 | 268 | } |
549e9569 NB |
269 | |
270 | /* Monitor a set of active md arrays - all of which share the | |
271 | * same metadata - and respond to events that require | |
272 | * metadata update. | |
273 | * | |
274 | * New arrays are detected by another thread which allocates | |
275 | * required memory and attaches the data structure to our list. | |
276 | * | |
277 | * Events: | |
278 | * Array stops. | |
279 | * This is detected by array_state going to 'clear' or 'inactive'. | |
280 | * while we thought it was active. | |
281 | * Response is to mark metadata as clean and 'clear' the array(??) | |
282 | * write-pending | |
283 | * array_state if 'write-pending' | |
284 | * We mark metadata as 'dirty' then set array to 'active'. | |
285 | * active_idle | |
286 | * Either ignore, or mark clean, then mark metadata as clean. | |
287 | * | |
288 | * device fails | |
289 | * detected by rd-N/state reporting "faulty" | |
8d45d196 DW |
290 | * mark device as 'failed' in metadata, let the kernel release the |
291 | * device by writing '-blocked' to rd/state, and finally write 'remove' to | |
0af73f61 DW |
292 | * rd/state. Before a disk can be replaced it must be failed and removed |
293 | * from all container members, this will be preemptive for the other | |
294 | * arrays... safe? | |
549e9569 NB |
295 | * |
296 | * sync completes | |
297 | * sync_action was 'resync' and becomes 'idle' and resync_start becomes | |
298 | * MaxSector | |
299 | * Notify metadata that sync is complete. | |
549e9569 NB |
300 | * |
301 | * recovery completes | |
302 | * sync_action changes from 'recover' to 'idle' | |
303 | * Check each device state and mark metadata if 'faulty' or 'in_sync'. | |
549e9569 NB |
304 | * |
305 | * deal with resync | |
c052ba30 DW |
306 | * This only happens on finding a new array... mdadm will have set |
307 | * 'resync_start' to the correct value. If 'resync_start' indicates that an | |
308 | * resync needs to occur set the array to the 'active' state rather than the | |
309 | * initial read-auto state. | |
549e9569 NB |
310 | * |
311 | * | |
312 | * | |
313 | * We wait for a change (poll/select) on array_state, sync_action, and | |
314 | * each rd-X/state file. | |
315 | * When we get any change, we check everything. So read each state file, | |
316 | * then decide what to do. | |
317 | * | |
318 | * The core action is to write new metadata to all devices in the array. | |
319 | * This is done at most once on any wakeup. | |
320 | * After that we might: | |
321 | * - update the array_state | |
322 | * - set the role of some devices. | |
323 | * - request a sync_action | |
324 | * | |
325 | */ | |
326 | ||
77b3ac8c | 327 | #define ARRAY_DIRTY 1 |
68226a80 | 328 | #define ARRAY_BUSY 2 |
549e9569 NB |
329 | static int read_and_act(struct active_array *a) |
330 | { | |
484240d8 | 331 | unsigned long long sync_completed; |
6c3fb95c | 332 | int check_degraded = 0; |
0f99b4bd | 333 | int check_reshape = 0; |
2a0bb19e | 334 | int deactivate = 0; |
549e9569 | 335 | struct mdinfo *mdi; |
77b3ac8c | 336 | int ret = 0; |
3a5d0473 | 337 | int count = 0; |
39da26ec | 338 | struct timeval tv; |
549e9569 NB |
339 | |
340 | a->next_state = bad_word; | |
341 | a->next_action = bad_action; | |
342 | ||
343 | a->curr_state = read_state(a->info.state_fd); | |
344 | a->curr_action = read_action(a->action_fd); | |
40ae6f5f | 345 | if (a->curr_state != clear) |
346 | /* | |
347 | * In "clear" state, resync_start may wrongly be set to "0" | |
348 | * when the kernel called md_clean but didn't remove the | |
349 | * sysfs attributes yet | |
350 | */ | |
351 | read_resync_start(a->resync_start_fd, &a->info.resync_start); | |
484240d8 | 352 | sync_completed = read_sync_completed(a->sync_completed_fd); |
549e9569 NB |
353 | for (mdi = a->info.devs; mdi ; mdi = mdi->next) { |
354 | mdi->next_state = 0; | |
10ce1808 | 355 | mdi->curr_state = 0; |
e1516be1 | 356 | if (mdi->state_fd >= 0) { |
40ae6f5f | 357 | read_resync_start(mdi->recovery_fd, |
358 | &mdi->recovery_start); | |
8d45d196 | 359 | mdi->curr_state = read_dev_state(mdi->state_fd); |
e1516be1 | 360 | } |
1ab97c97 TM |
361 | /* |
362 | * If array is blocked and metadata handler is able to handle | |
363 | * BB, check if you can acknowledge them to md driver. If | |
364 | * successful, clear faulty state and unblock the array. | |
365 | */ | |
366 | if ((mdi->curr_state & DS_BLOCKED) && | |
367 | a->container->ss->record_bad_block && | |
368 | (process_dev_ubb(a, mdi) > 0)) { | |
369 | mdi->next_state |= DS_UNBLOCK; | |
370 | } | |
549e9569 NB |
371 | } |
372 | ||
39da26ec | 373 | gettimeofday(&tv, NULL); |
1ade5cc1 N |
374 | dprintf("(%d): %ld.%06ld state:%s prev:%s action:%s prev: %s start:%llu\n", |
375 | a->info.container_member, | |
39da26ec MW |
376 | tv.tv_sec, tv.tv_usec, |
377 | array_states[a->curr_state], | |
378 | array_states[a->prev_state], | |
379 | sync_actions[a->curr_action], | |
380 | sync_actions[a->prev_action], | |
381 | a->info.resync_start | |
382 | ); | |
383 | ||
6b374ba3 | 384 | if ((a->curr_state == bad_word || a->curr_state <= inactive) && |
549e9569 NB |
385 | a->prev_state > inactive) { |
386 | /* array has been stopped */ | |
ed9d66aa | 387 | a->container->ss->set_array_state(a, 1); |
549e9569 | 388 | a->next_state = clear; |
2a0bb19e | 389 | deactivate = 1; |
549e9569 NB |
390 | } |
391 | if (a->curr_state == write_pending) { | |
ed9d66aa | 392 | a->container->ss->set_array_state(a, 0); |
549e9569 | 393 | a->next_state = active; |
77b3ac8c | 394 | ret |= ARRAY_DIRTY; |
549e9569 NB |
395 | } |
396 | if (a->curr_state == active_idle) { | |
d797a062 DW |
397 | /* Set array to 'clean' FIRST, then mark clean |
398 | * in the metadata | |
549e9569 | 399 | */ |
d797a062 | 400 | a->next_state = clean; |
77b3ac8c | 401 | ret |= ARRAY_DIRTY; |
d797a062 DW |
402 | } |
403 | if (a->curr_state == clean) { | |
d797a062 | 404 | a->container->ss->set_array_state(a, 1); |
549e9569 | 405 | } |
140d3685 | 406 | if (a->curr_state == active || |
6b374ba3 | 407 | a->curr_state == suspended) |
77b3ac8c | 408 | ret |= ARRAY_DIRTY; |
549e9569 | 409 | if (a->curr_state == readonly) { |
e9dd1598 N |
410 | /* Well, I'm ready to handle things. If readonly |
411 | * wasn't requested, transition to read-auto. | |
549e9569 | 412 | */ |
e9dd1598 N |
413 | char buf[64]; |
414 | read_attr(buf, sizeof(buf), a->metadata_fd); | |
415 | if (strncmp(buf, "external:-", 10) == 0) { | |
416 | /* explicit request for readonly array. Leave it alone */ | |
417 | ; | |
418 | } else { | |
e9dd1598 N |
419 | if (a->container->ss->set_array_state(a, 2)) |
420 | a->next_state = read_auto; /* array is clean */ | |
140d3685 | 421 | else { |
e9dd1598 | 422 | a->next_state = active; /* Now active for recovery etc */ |
77b3ac8c | 423 | ret |= ARRAY_DIRTY; |
140d3685 | 424 | } |
e9dd1598 | 425 | } |
549e9569 NB |
426 | } |
427 | ||
00e02142 DW |
428 | if (!deactivate && |
429 | a->curr_action == idle && | |
549e9569 | 430 | a->prev_action == resync) { |
4e5528c6 NB |
431 | /* A resync has finished. The endpoint is recorded in |
432 | * 'sync_start'. We don't update the metadata | |
433 | * until the array goes inactive or readonly though. | |
434 | * Just check if we need to fiddle spares. | |
435 | */ | |
0c0c44db | 436 | a->container->ss->set_array_state(a, a->curr_state <= clean); |
549e9569 NB |
437 | check_degraded = 1; |
438 | } | |
439 | ||
00e02142 DW |
440 | if (!deactivate && |
441 | a->curr_action == idle && | |
549e9569 | 442 | a->prev_action == recover) { |
0a6bdbee DW |
443 | /* A recovery has finished. Some disks may be in sync now, |
444 | * and the array may no longer be degraded | |
445 | */ | |
549e9569 | 446 | for (mdi = a->info.devs ; mdi ; mdi = mdi->next) { |
8d45d196 DW |
447 | a->container->ss->set_disk(a, mdi->disk.raid_disk, |
448 | mdi->curr_state); | |
549e9569 NB |
449 | if (! (mdi->curr_state & DS_INSYNC)) |
450 | check_degraded = 1; | |
3a5d0473 | 451 | count++; |
549e9569 | 452 | } |
3a5d0473 KW |
453 | if (count != a->info.array.raid_disks) |
454 | check_degraded = 1; | |
549e9569 NB |
455 | } |
456 | ||
0f99b4bd N |
457 | if (!deactivate && |
458 | a->curr_action == reshape && | |
459 | a->prev_action != reshape) | |
460 | /* reshape was requested by mdadm. Need to see if | |
461 | * new devices have been added. Manager does that | |
462 | * when it sees check_reshape | |
463 | */ | |
464 | check_reshape = 1; | |
465 | ||
92967543 DW |
466 | /* Check for failures and if found: |
467 | * 1/ Record the failure in the metadata and unblock the device. | |
468 | * FIXME update the kernel to stop notifying on failed drives when | |
469 | * the array is readonly and we have cleared 'blocked' | |
470 | * 2/ Try to remove the device if the array is writable, or can be | |
471 | * made writable. | |
472 | */ | |
549e9569 NB |
473 | for (mdi = a->info.devs ; mdi ; mdi = mdi->next) { |
474 | if (mdi->curr_state & DS_FAULTY) { | |
8d45d196 DW |
475 | a->container->ss->set_disk(a, mdi->disk.raid_disk, |
476 | mdi->curr_state); | |
549e9569 | 477 | check_degraded = 1; |
1c278e81 N |
478 | if (mdi->curr_state & DS_BLOCKED) |
479 | mdi->next_state |= DS_UNBLOCK; | |
92967543 DW |
480 | if (a->curr_state == read_auto) { |
481 | a->container->ss->set_array_state(a, 0); | |
482 | a->next_state = active; | |
483 | } | |
484 | if (a->curr_state > readonly) | |
485 | mdi->next_state |= DS_REMOVE; | |
549e9569 NB |
486 | } |
487 | } | |
488 | ||
484240d8 DW |
489 | /* Check for recovery checkpoint notifications. We need to be a |
490 | * minimum distance away from the last checkpoint to prevent | |
aad6f216 N |
491 | * over checkpointing. Note reshape checkpointing is handled |
492 | * in the second branch. | |
484240d8 DW |
493 | */ |
494 | if (sync_completed > a->last_checkpoint && | |
495 | sync_completed - a->last_checkpoint > a->info.component_size >> 4 && | |
4f0a7acc DW |
496 | a->curr_action > reshape) { |
497 | /* A (non-reshape) sync_action has reached a checkpoint. | |
498 | * Record the updated position in the metadata | |
499 | */ | |
500 | a->last_checkpoint = sync_completed; | |
501 | a->container->ss->set_array_state(a, a->curr_state <= clean); | |
aad6f216 N |
502 | } else if ((a->curr_action == idle && a->prev_action == reshape) || |
503 | (a->curr_action == reshape | |
504 | && sync_completed > a->last_checkpoint) ) { | |
505 | /* Reshape has progressed or completed so we need to | |
506 | * update the array state - and possibly the array size | |
507 | */ | |
2a9f8409 AK |
508 | if (sync_completed != 0) |
509 | a->last_checkpoint = sync_completed; | |
6d4225a1 AK |
510 | /* We might need to update last_checkpoint depending on |
511 | * the reason that reshape finished. | |
512 | * if array reshape is really finished: | |
513 | * set check point to the end, this allows | |
514 | * set_array_state() to finalize reshape in metadata | |
515 | * if reshape if broken: do not set checkpoint to the end | |
516 | * this allows for reshape restart from checkpoint | |
517 | */ | |
518 | if ((a->curr_action != reshape) && | |
519 | (a->prev_action == reshape)) { | |
520 | char buf[40]; | |
521 | if ((sysfs_get_str(&a->info, NULL, | |
522 | "reshape_position", | |
523 | buf, | |
524 | sizeof(buf)) >= 0) && | |
525 | strncmp(buf, "none", 4) == 0) | |
526 | a->last_checkpoint = a->info.component_size; | |
527 | } | |
aad6f216 | 528 | a->container->ss->set_array_state(a, a->curr_state <= clean); |
2a9f8409 | 529 | a->last_checkpoint = sync_completed; |
aad6f216 N |
530 | } |
531 | ||
532 | if (sync_completed > a->last_checkpoint) | |
484240d8 | 533 | a->last_checkpoint = sync_completed; |
484240d8 | 534 | |
955aa6cf PB |
535 | if (sync_completed >= a->info.component_size) |
536 | a->last_checkpoint = 0; | |
537 | ||
a4921f30 | 538 | a->container->ss->sync_metadata(a->container); |
1ade5cc1 | 539 | dprintf("(%d): state:%s action:%s next(", a->info.container_member, |
4065aa81 | 540 | array_states[a->curr_state], sync_actions[a->curr_action]); |
549e9569 NB |
541 | |
542 | /* Effect state changes in the array */ | |
4e6e574a | 543 | if (a->next_state != bad_word) { |
1ade5cc1 | 544 | dprintf_cont(" state:%s", array_states[a->next_state]); |
549e9569 | 545 | write_attr(array_states[a->next_state], a->info.state_fd); |
4e6e574a DW |
546 | } |
547 | if (a->next_action != bad_action) { | |
549e9569 | 548 | write_attr(sync_actions[a->next_action], a->action_fd); |
1ade5cc1 | 549 | dprintf_cont(" action:%s", sync_actions[a->next_action]); |
4e6e574a | 550 | } |
549e9569 | 551 | for (mdi = a->info.devs; mdi ; mdi = mdi->next) { |
92967543 | 552 | if (mdi->next_state & DS_UNBLOCK) { |
1ade5cc1 | 553 | dprintf_cont(" %d:-blocked", mdi->disk.raid_disk); |
92967543 DW |
554 | write_attr("-blocked", mdi->state_fd); |
555 | } | |
556 | ||
557 | if ((mdi->next_state & DS_REMOVE) && mdi->state_fd >= 0) { | |
57632f4a | 558 | int remove_result; |
8d45d196 | 559 | |
68226a80 N |
560 | /* The kernel may not be able to immediately remove the |
561 | * disk. In that case we wait a little while and | |
562 | * try again. | |
8d45d196 | 563 | */ |
57632f4a NB |
564 | remove_result = write_attr("remove", mdi->state_fd); |
565 | if (remove_result > 0) { | |
1ade5cc1 | 566 | dprintf_cont(" %d:removed", mdi->disk.raid_disk); |
8d45d196 | 567 | close(mdi->state_fd); |
e40512fd | 568 | close(mdi->recovery_fd); |
6dc1785f TM |
569 | close(mdi->bb_fd); |
570 | close(mdi->ubb_fd); | |
8d45d196 | 571 | mdi->state_fd = -1; |
68226a80 N |
572 | } else |
573 | ret |= ARRAY_BUSY; | |
8d45d196 | 574 | } |
4e6e574a | 575 | if (mdi->next_state & DS_INSYNC) { |
549e9569 | 576 | write_attr("+in_sync", mdi->state_fd); |
1ade5cc1 | 577 | dprintf_cont(" %d:+in_sync", mdi->disk.raid_disk); |
4e6e574a | 578 | } |
549e9569 | 579 | } |
1ade5cc1 | 580 | dprintf_cont(" )\n"); |
549e9569 NB |
581 | |
582 | /* move curr_ to prev_ */ | |
583 | a->prev_state = a->curr_state; | |
584 | ||
585 | a->prev_action = a->curr_action; | |
586 | ||
587 | for (mdi = a->info.devs; mdi ; mdi = mdi->next) { | |
588 | mdi->prev_state = mdi->curr_state; | |
589 | mdi->next_state = 0; | |
590 | } | |
591 | ||
0f99b4bd | 592 | if (check_degraded || check_reshape) { |
7e1432fb | 593 | /* manager will do the actual check */ |
0f99b4bd N |
594 | if (check_degraded) |
595 | a->check_degraded = 1; | |
596 | if (check_reshape) | |
597 | a->check_reshape = 1; | |
7e1432fb NB |
598 | signal_manager(); |
599 | } | |
600 | ||
2a0bb19e DW |
601 | if (deactivate) |
602 | a->container = NULL; | |
603 | ||
77b3ac8c | 604 | return ret; |
549e9569 NB |
605 | } |
606 | ||
0af73f61 DW |
607 | static struct mdinfo * |
608 | find_device(struct active_array *a, int major, int minor) | |
609 | { | |
610 | struct mdinfo *mdi; | |
611 | ||
612 | for (mdi = a->info.devs ; mdi ; mdi = mdi->next) | |
613 | if (mdi->disk.major == major && mdi->disk.minor == minor) | |
614 | return mdi; | |
615 | ||
616 | return NULL; | |
617 | } | |
618 | ||
619 | static void reconcile_failed(struct active_array *aa, struct mdinfo *failed) | |
620 | { | |
621 | struct active_array *a; | |
622 | struct mdinfo *victim; | |
623 | ||
624 | for (a = aa; a; a = a->next) { | |
ba714450 | 625 | if (!a->container || a->to_remove) |
0af73f61 DW |
626 | continue; |
627 | victim = find_device(a, failed->disk.major, failed->disk.minor); | |
628 | if (!victim) | |
629 | continue; | |
630 | ||
631 | if (!(victim->curr_state & DS_FAULTY)) | |
632 | write_attr("faulty", victim->state_fd); | |
633 | } | |
634 | } | |
635 | ||
4e6e574a DW |
636 | #ifdef DEBUG |
637 | static void dprint_wake_reasons(fd_set *fds) | |
638 | { | |
639 | int i; | |
640 | char proc_path[256]; | |
641 | char link[256]; | |
642 | char *basename; | |
643 | int rv; | |
644 | ||
645 | fprintf(stderr, "monitor: wake ( "); | |
646 | for (i = 0; i < FD_SETSIZE; i++) { | |
647 | if (FD_ISSET(i, fds)) { | |
648 | sprintf(proc_path, "/proc/%d/fd/%d", | |
649 | (int) getpid(), i); | |
650 | ||
651 | rv = readlink(proc_path, link, sizeof(link) - 1); | |
652 | if (rv < 0) { | |
653 | fprintf(stderr, "%d:unknown ", i); | |
654 | continue; | |
655 | } | |
656 | link[rv] = '\0'; | |
657 | basename = strrchr(link, '/'); | |
658 | fprintf(stderr, "%d:%s ", | |
659 | i, basename ? ++basename : link); | |
660 | } | |
661 | } | |
662 | fprintf(stderr, ")\n"); | |
663 | } | |
664 | #endif | |
665 | ||
1eb252b8 N |
666 | int monitor_loop_cnt; |
667 | ||
4d43913c | 668 | static int wait_and_act(struct supertype *container, int nowait) |
549e9569 NB |
669 | { |
670 | fd_set rfds; | |
671 | int maxfd = 0; | |
e0d6609f | 672 | struct active_array **aap = &container->arrays; |
1ed3f387 | 673 | struct active_array *a, **ap; |
549e9569 | 674 | int rv; |
0af73f61 | 675 | struct mdinfo *mdi; |
6144ed44 | 676 | static unsigned int dirty_arrays = ~0; /* start at some non-zero value */ |
549e9569 NB |
677 | |
678 | FD_ZERO(&rfds); | |
679 | ||
1ed3f387 NB |
680 | for (ap = aap ; *ap ;) { |
681 | a = *ap; | |
682 | /* once an array has been deactivated we want to | |
683 | * ask the manager to discard it. | |
2a0bb19e | 684 | */ |
ba714450 | 685 | if (!a->container || a->to_remove) { |
1ed3f387 NB |
686 | if (discard_this) { |
687 | ap = &(*ap)->next; | |
688 | continue; | |
689 | } | |
690 | *ap = a->next; | |
691 | a->next = NULL; | |
692 | discard_this = a; | |
693 | signal_manager(); | |
2a0bb19e | 694 | continue; |
1ed3f387 | 695 | } |
2a0bb19e | 696 | |
549e9569 NB |
697 | add_fd(&rfds, &maxfd, a->info.state_fd); |
698 | add_fd(&rfds, &maxfd, a->action_fd); | |
484240d8 | 699 | add_fd(&rfds, &maxfd, a->sync_completed_fd); |
6dc1785f | 700 | for (mdi = a->info.devs ; mdi ; mdi = mdi->next) { |
549e9569 | 701 | add_fd(&rfds, &maxfd, mdi->state_fd); |
6dc1785f TM |
702 | add_fd(&rfds, &maxfd, mdi->bb_fd); |
703 | add_fd(&rfds, &maxfd, mdi->ubb_fd); | |
704 | } | |
1ed3f387 NB |
705 | |
706 | ap = &(*ap)->next; | |
549e9569 NB |
707 | } |
708 | ||
6144ed44 DW |
709 | if (manager_ready && (*aap == NULL || (sigterm && !dirty_arrays))) { |
710 | /* No interesting arrays, or we have been told to | |
711 | * terminate and everything is clean. Lets see about | |
712 | * exiting. Note that blocking at this point is not a | |
713 | * problem as there are no active arrays, there is | |
714 | * nothing that we need to be ready to do. | |
e0d6609f | 715 | */ |
d998b738 N |
716 | int fd; |
717 | if (sigterm) | |
4dd2df09 | 718 | fd = open_dev_excl(container->devnm); |
d998b738 | 719 | else |
4dd2df09 | 720 | fd = open_dev_flags(container->devnm, O_RDONLY|O_EXCL); |
e0d6609f NB |
721 | if (fd >= 0 || errno != EBUSY) { |
722 | /* OK, we are safe to leave */ | |
6144ed44 DW |
723 | if (sigterm && !dirty_arrays) |
724 | dprintf("caught sigterm, all clean... exiting\n"); | |
725 | else | |
726 | dprintf("no arrays to monitor... exiting\n"); | |
fa716c83 N |
727 | if (!sigterm) |
728 | /* On SIGTERM, someone (the take-over mdmon) will | |
729 | * clean up | |
730 | */ | |
4dd2df09 | 731 | remove_pidfile(container->devnm); |
e0d6609f NB |
732 | exit_now = 1; |
733 | signal_manager(); | |
6f4cdfd9 | 734 | close(fd); |
e0d6609f NB |
735 | exit(0); |
736 | } | |
737 | } | |
738 | ||
549e9569 | 739 | if (!nowait) { |
4d43913c | 740 | sigset_t set; |
d998b738 N |
741 | struct timespec ts; |
742 | ts.tv_sec = 24*3600; | |
743 | ts.tv_nsec = 0; | |
68226a80 | 744 | if (*aap == NULL || container->retry_soon) { |
d998b738 N |
745 | /* just waiting to get O_EXCL access */ |
746 | ts.tv_sec = 0; | |
747 | ts.tv_nsec = 20000000ULL; | |
748 | } | |
4d43913c NB |
749 | sigprocmask(SIG_UNBLOCK, NULL, &set); |
750 | sigdelset(&set, SIGUSR1); | |
1eb252b8 | 751 | monitor_loop_cnt |= 1; |
d998b738 | 752 | rv = pselect(maxfd+1, NULL, NULL, &rfds, &ts, &set); |
1eb252b8 | 753 | monitor_loop_cnt += 1; |
c3719360 MW |
754 | if (rv == -1) { |
755 | if (errno == EINTR) { | |
756 | rv = 0; | |
757 | dprintf("monitor: caught signal\n"); | |
758 | } else | |
759 | dprintf("monitor: error %d in pselect\n", | |
760 | errno); | |
761 | } | |
4e6e574a | 762 | #ifdef DEBUG |
c3719360 MW |
763 | else |
764 | dprint_wake_reasons(&rfds); | |
4e6e574a | 765 | #endif |
68226a80 | 766 | container->retry_soon = 0; |
549e9569 NB |
767 | } |
768 | ||
2e735d19 NB |
769 | if (update_queue) { |
770 | struct metadata_update *this; | |
771 | ||
772 | for (this = update_queue; this ; this = this->next) | |
773 | container->ss->process_update(container, this); | |
774 | ||
775 | update_queue_handled = update_queue; | |
776 | update_queue = NULL; | |
777 | signal_manager(); | |
778 | container->ss->sync_metadata(container); | |
779 | } | |
780 | ||
3d2c4fc7 | 781 | rv = 0; |
6144ed44 | 782 | dirty_arrays = 0; |
1ed3f387 | 783 | for (a = *aap; a ; a = a->next) { |
6144ed44 | 784 | |
2a0bb19e | 785 | if (a->replaces && !discard_this) { |
549e9569 NB |
786 | struct active_array **ap; |
787 | for (ap = &a->next; *ap && *ap != a->replaces; | |
788 | ap = & (*ap)->next) | |
789 | ; | |
790 | if (*ap) | |
791 | *ap = (*ap)->next; | |
792 | discard_this = a->replaces; | |
793 | a->replaces = NULL; | |
6c3fb95c | 794 | /* FIXME check if device->state_fd need to be cleared?*/ |
1ed3f387 | 795 | signal_manager(); |
549e9569 | 796 | } |
ba714450 | 797 | if (a->container && !a->to_remove) { |
77b3ac8c | 798 | int ret = read_and_act(a); |
140d3685 | 799 | rv |= 1; |
77b3ac8c | 800 | dirty_arrays += !!(ret & ARRAY_DIRTY); |
140d3685 DW |
801 | /* when terminating stop manipulating the array after it |
802 | * is clean, but make sure read_and_act() is given a | |
803 | * chance to handle 'active_idle' | |
804 | */ | |
77b3ac8c | 805 | if (sigterm && !(ret & ARRAY_DIRTY)) |
140d3685 | 806 | a->container = NULL; /* stop touching this array */ |
68226a80 N |
807 | if (ret & ARRAY_BUSY) |
808 | container->retry_soon = 1; | |
6144ed44 | 809 | } |
549e9569 | 810 | } |
0af73f61 DW |
811 | |
812 | /* propagate failures across container members */ | |
1ed3f387 | 813 | for (a = *aap; a ; a = a->next) { |
ba714450 | 814 | if (!a->container || a->to_remove) |
0af73f61 DW |
815 | continue; |
816 | for (mdi = a->info.devs ; mdi ; mdi = mdi->next) | |
817 | if (mdi->curr_state & DS_FAULTY) | |
1ed3f387 | 818 | reconcile_failed(*aap, mdi); |
0af73f61 DW |
819 | } |
820 | ||
549e9569 NB |
821 | return rv; |
822 | } | |
823 | ||
824 | void do_monitor(struct supertype *container) | |
825 | { | |
826 | int rv; | |
827 | int first = 1; | |
828 | do { | |
4d43913c | 829 | rv = wait_and_act(container, first); |
549e9569 NB |
830 | first = 0; |
831 | } while (rv >= 0); | |
832 | } |