]>
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> |
549e9569 NB |
25 | |
26 | static char *array_states[] = { | |
27 | "clear", "inactive", "suspended", "readonly", "read-auto", | |
43ebc910 | 28 | "clean", "active", "write-pending", "active-idle", "broken", NULL }; |
549e9569 NB |
29 | static char *sync_actions[] = { |
30 | "idle", "reshape", "resync", "recover", "check", "repair", NULL | |
31 | }; | |
32 | ||
1ab97c97 TM |
33 | enum bb_action { |
34 | RECORD_BB = 1, | |
42d902d9 | 35 | COMPARE_BB, |
1ab97c97 TM |
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 | ||
42d902d9 TM |
187 | int compare_bb(struct active_array *a, struct mdinfo *mdi, const unsigned long |
188 | long sector, const unsigned int length, void *arg) | |
189 | { | |
190 | struct superswitch *ss = a->container->ss; | |
191 | struct md_bb *bb = (struct md_bb *) arg; | |
192 | int record = 1; | |
193 | int i; | |
194 | ||
195 | for (i = 0; i < bb->count; i++) { | |
196 | unsigned long long start = bb->entries[i].sector; | |
197 | unsigned long long len = bb->entries[i].length; | |
198 | ||
199 | /* | |
200 | * bad block in metadata exactly matches bad block in kernel | |
201 | * list, just remove it from a list | |
202 | */ | |
203 | if ((start == sector) && (len == length)) { | |
204 | if (i < bb->count - 1) | |
205 | bb->entries[i] = bb->entries[bb->count - 1]; | |
206 | bb->count -= 1; | |
207 | record = 0; | |
208 | break; | |
209 | } | |
210 | /* | |
211 | * bad block in metadata spans bad block in kernel list, | |
212 | * clear it and record new bad block | |
213 | */ | |
214 | if ((sector >= start) && (sector + length <= start + len)) { | |
215 | ss->clear_bad_block(a, mdi->disk.raid_disk, start, len); | |
216 | break; | |
217 | } | |
218 | } | |
219 | ||
220 | /* record all bad blocks not in metadata list */ | |
221 | if (record && (ss->record_bad_block(a, mdi->disk.raid_disk, sector, | |
222 | length) <= 0)) { | |
223 | sysfs_set_str(&a->info, mdi, "state", "-external_bbl"); | |
224 | return -1; | |
225 | } | |
226 | ||
227 | return 1; | |
228 | } | |
229 | ||
1ab97c97 TM |
230 | static int read_bb_file(int fd, struct active_array *a, struct mdinfo *mdi, |
231 | enum bb_action action, void *arg) | |
232 | { | |
233 | char buf[30]; | |
234 | int n = 0; | |
235 | int ret = 0; | |
236 | int read_again = 0; | |
237 | int off = 0; | |
238 | int pos = 0; | |
239 | int preserve_pos = (action == RECORD_BB ? 0 : 1); | |
240 | ||
241 | if (lseek(fd, 0, SEEK_SET) == (off_t) -1) | |
242 | return -1; | |
243 | ||
244 | do { | |
245 | read_again = 0; | |
246 | n = read(fd, buf + pos, sizeof(buf) - 1 - pos); | |
247 | if (n < 0) | |
248 | return -1; | |
249 | n += pos; | |
250 | ||
251 | buf[n] = '\0'; | |
252 | off = 0; | |
253 | ||
254 | while (off < n) { | |
255 | unsigned long long sector; | |
256 | int length; | |
257 | char newline; | |
258 | int consumed; | |
259 | int matched; | |
260 | int rc; | |
261 | ||
262 | /* kernel sysfs file format: "sector length\n" */ | |
263 | matched = sscanf(buf + off, "%llu %d%c%n", §or, | |
264 | &length, &newline, &consumed); | |
265 | if ((matched != 3) && (off > 0)) { | |
266 | /* truncated entry, read again */ | |
267 | if (preserve_pos) { | |
268 | pos = sizeof(buf) - off - 1; | |
269 | memmove(buf, buf + off, pos); | |
270 | } else { | |
271 | if (lseek(fd, 0, SEEK_SET) == | |
272 | (off_t) -1) | |
273 | return -1; | |
274 | } | |
275 | read_again = 1; | |
276 | break; | |
277 | } | |
278 | if (matched != 3) | |
279 | return -1; | |
280 | if (newline != '\n') | |
281 | return -1; | |
282 | if (length <= 0) | |
283 | return -1; | |
284 | ||
285 | if (action == RECORD_BB) | |
286 | rc = process_ubb(a, mdi, sector, length, | |
287 | buf + off, consumed); | |
42d902d9 TM |
288 | else if (action == COMPARE_BB) |
289 | rc = compare_bb(a, mdi, sector, length, arg); | |
1ab97c97 TM |
290 | else |
291 | rc = -1; | |
292 | ||
293 | if (rc < 0) | |
294 | return rc; | |
295 | ret += rc; | |
296 | off += consumed; | |
297 | } | |
298 | } while (read_again); | |
299 | ||
300 | return ret; | |
301 | } | |
302 | ||
303 | static int process_dev_ubb(struct active_array *a, struct mdinfo *mdi) | |
304 | { | |
305 | return read_bb_file(mdi->ubb_fd, a, mdi, RECORD_BB, NULL); | |
306 | } | |
307 | ||
42d902d9 TM |
308 | static int check_for_cleared_bb(struct active_array *a, struct mdinfo *mdi) |
309 | { | |
310 | struct superswitch *ss = a->container->ss; | |
311 | struct md_bb *bb; | |
312 | int i; | |
313 | ||
9ae62977 LG |
314 | if (!ss->get_bad_blocks) |
315 | return -1; | |
316 | ||
42d902d9 TM |
317 | /* |
318 | * Get a list of bad blocks for an array, then read list of | |
319 | * acknowledged bad blocks from kernel and compare it against metadata | |
320 | * list, clear all bad blocks remaining in metadata list | |
321 | */ | |
322 | bb = ss->get_bad_blocks(a, mdi->disk.raid_disk); | |
323 | if (!bb) | |
324 | return -1; | |
325 | ||
326 | if (read_bb_file(mdi->bb_fd, a, mdi, COMPARE_BB, bb) < 0) | |
327 | return -1; | |
328 | ||
329 | for (i = 0; i < bb->count; i++) { | |
330 | unsigned long long sector = bb->entries[i].sector; | |
331 | int length = bb->entries[i].length; | |
332 | ||
333 | ss->clear_bad_block(a, mdi->disk.raid_disk, sector, length); | |
334 | } | |
335 | ||
336 | return 0; | |
337 | } | |
338 | ||
1ed3f387 NB |
339 | static void signal_manager(void) |
340 | { | |
4d43913c NB |
341 | /* tgkill(getpid(), mon_tid, SIGUSR1); */ |
342 | int pid = getpid(); | |
343 | syscall(SYS_tgkill, pid, mgr_tid, SIGUSR1); | |
1ed3f387 | 344 | } |
549e9569 NB |
345 | |
346 | /* Monitor a set of active md arrays - all of which share the | |
347 | * same metadata - and respond to events that require | |
348 | * metadata update. | |
349 | * | |
350 | * New arrays are detected by another thread which allocates | |
351 | * required memory and attaches the data structure to our list. | |
352 | * | |
353 | * Events: | |
354 | * Array stops. | |
355 | * This is detected by array_state going to 'clear' or 'inactive'. | |
356 | * while we thought it was active. | |
357 | * Response is to mark metadata as clean and 'clear' the array(??) | |
358 | * write-pending | |
359 | * array_state if 'write-pending' | |
360 | * We mark metadata as 'dirty' then set array to 'active'. | |
361 | * active_idle | |
362 | * Either ignore, or mark clean, then mark metadata as clean. | |
363 | * | |
364 | * device fails | |
365 | * detected by rd-N/state reporting "faulty" | |
8d45d196 DW |
366 | * mark device as 'failed' in metadata, let the kernel release the |
367 | * device by writing '-blocked' to rd/state, and finally write 'remove' to | |
0af73f61 DW |
368 | * rd/state. Before a disk can be replaced it must be failed and removed |
369 | * from all container members, this will be preemptive for the other | |
370 | * arrays... safe? | |
549e9569 NB |
371 | * |
372 | * sync completes | |
373 | * sync_action was 'resync' and becomes 'idle' and resync_start becomes | |
374 | * MaxSector | |
375 | * Notify metadata that sync is complete. | |
549e9569 NB |
376 | * |
377 | * recovery completes | |
378 | * sync_action changes from 'recover' to 'idle' | |
379 | * Check each device state and mark metadata if 'faulty' or 'in_sync'. | |
549e9569 NB |
380 | * |
381 | * deal with resync | |
c052ba30 DW |
382 | * This only happens on finding a new array... mdadm will have set |
383 | * 'resync_start' to the correct value. If 'resync_start' indicates that an | |
384 | * resync needs to occur set the array to the 'active' state rather than the | |
385 | * initial read-auto state. | |
549e9569 NB |
386 | * |
387 | * | |
388 | * | |
389 | * We wait for a change (poll/select) on array_state, sync_action, and | |
390 | * each rd-X/state file. | |
391 | * When we get any change, we check everything. So read each state file, | |
392 | * then decide what to do. | |
393 | * | |
394 | * The core action is to write new metadata to all devices in the array. | |
395 | * This is done at most once on any wakeup. | |
396 | * After that we might: | |
397 | * - update the array_state | |
398 | * - set the role of some devices. | |
399 | * - request a sync_action | |
400 | * | |
401 | */ | |
402 | ||
77b3ac8c | 403 | #define ARRAY_DIRTY 1 |
68226a80 | 404 | #define ARRAY_BUSY 2 |
42d902d9 | 405 | static int read_and_act(struct active_array *a, fd_set *fds) |
549e9569 | 406 | { |
484240d8 | 407 | unsigned long long sync_completed; |
6c3fb95c | 408 | int check_degraded = 0; |
0f99b4bd | 409 | int check_reshape = 0; |
2a0bb19e | 410 | int deactivate = 0; |
549e9569 | 411 | struct mdinfo *mdi; |
77b3ac8c | 412 | int ret = 0; |
3a5d0473 | 413 | int count = 0; |
39da26ec | 414 | struct timeval tv; |
549e9569 NB |
415 | |
416 | a->next_state = bad_word; | |
417 | a->next_action = bad_action; | |
418 | ||
419 | a->curr_state = read_state(a->info.state_fd); | |
420 | a->curr_action = read_action(a->action_fd); | |
40ae6f5f | 421 | if (a->curr_state != clear) |
422 | /* | |
423 | * In "clear" state, resync_start may wrongly be set to "0" | |
424 | * when the kernel called md_clean but didn't remove the | |
425 | * sysfs attributes yet | |
426 | */ | |
427 | read_resync_start(a->resync_start_fd, &a->info.resync_start); | |
484240d8 | 428 | sync_completed = read_sync_completed(a->sync_completed_fd); |
549e9569 NB |
429 | for (mdi = a->info.devs; mdi ; mdi = mdi->next) { |
430 | mdi->next_state = 0; | |
10ce1808 | 431 | mdi->curr_state = 0; |
e1516be1 | 432 | if (mdi->state_fd >= 0) { |
40ae6f5f | 433 | read_resync_start(mdi->recovery_fd, |
434 | &mdi->recovery_start); | |
8d45d196 | 435 | mdi->curr_state = read_dev_state(mdi->state_fd); |
e1516be1 | 436 | } |
1ab97c97 TM |
437 | /* |
438 | * If array is blocked and metadata handler is able to handle | |
439 | * BB, check if you can acknowledge them to md driver. If | |
440 | * successful, clear faulty state and unblock the array. | |
441 | */ | |
442 | if ((mdi->curr_state & DS_BLOCKED) && | |
443 | a->container->ss->record_bad_block && | |
444 | (process_dev_ubb(a, mdi) > 0)) { | |
445 | mdi->next_state |= DS_UNBLOCK; | |
446 | } | |
42d902d9 TM |
447 | if (FD_ISSET(mdi->bb_fd, fds)) |
448 | check_for_cleared_bb(a, mdi); | |
549e9569 NB |
449 | } |
450 | ||
39da26ec | 451 | gettimeofday(&tv, NULL); |
1ade5cc1 N |
452 | dprintf("(%d): %ld.%06ld state:%s prev:%s action:%s prev: %s start:%llu\n", |
453 | a->info.container_member, | |
39da26ec MW |
454 | tv.tv_sec, tv.tv_usec, |
455 | array_states[a->curr_state], | |
456 | array_states[a->prev_state], | |
457 | sync_actions[a->curr_action], | |
458 | sync_actions[a->prev_action], | |
459 | a->info.resync_start | |
460 | ); | |
461 | ||
6b374ba3 | 462 | if ((a->curr_state == bad_word || a->curr_state <= inactive) && |
549e9569 NB |
463 | a->prev_state > inactive) { |
464 | /* array has been stopped */ | |
ed9d66aa | 465 | a->container->ss->set_array_state(a, 1); |
549e9569 | 466 | a->next_state = clear; |
2a0bb19e | 467 | deactivate = 1; |
549e9569 NB |
468 | } |
469 | if (a->curr_state == write_pending) { | |
ed9d66aa | 470 | a->container->ss->set_array_state(a, 0); |
549e9569 | 471 | a->next_state = active; |
77b3ac8c | 472 | ret |= ARRAY_DIRTY; |
549e9569 NB |
473 | } |
474 | if (a->curr_state == active_idle) { | |
d797a062 DW |
475 | /* Set array to 'clean' FIRST, then mark clean |
476 | * in the metadata | |
549e9569 | 477 | */ |
d797a062 | 478 | a->next_state = clean; |
77b3ac8c | 479 | ret |= ARRAY_DIRTY; |
d797a062 | 480 | } |
43ebc910 | 481 | if ((a->curr_state == clean) || (a->curr_state == broken)) { |
d797a062 | 482 | a->container->ss->set_array_state(a, 1); |
549e9569 | 483 | } |
140d3685 | 484 | if (a->curr_state == active || |
6b374ba3 | 485 | a->curr_state == suspended) |
77b3ac8c | 486 | ret |= ARRAY_DIRTY; |
549e9569 | 487 | if (a->curr_state == readonly) { |
e9dd1598 N |
488 | /* Well, I'm ready to handle things. If readonly |
489 | * wasn't requested, transition to read-auto. | |
549e9569 | 490 | */ |
e9dd1598 N |
491 | char buf[64]; |
492 | read_attr(buf, sizeof(buf), a->metadata_fd); | |
493 | if (strncmp(buf, "external:-", 10) == 0) { | |
494 | /* explicit request for readonly array. Leave it alone */ | |
495 | ; | |
496 | } else { | |
e9dd1598 N |
497 | if (a->container->ss->set_array_state(a, 2)) |
498 | a->next_state = read_auto; /* array is clean */ | |
140d3685 | 499 | else { |
e9dd1598 | 500 | a->next_state = active; /* Now active for recovery etc */ |
77b3ac8c | 501 | ret |= ARRAY_DIRTY; |
140d3685 | 502 | } |
e9dd1598 | 503 | } |
549e9569 NB |
504 | } |
505 | ||
00e02142 DW |
506 | if (!deactivate && |
507 | a->curr_action == idle && | |
549e9569 | 508 | a->prev_action == resync) { |
4e5528c6 NB |
509 | /* A resync has finished. The endpoint is recorded in |
510 | * 'sync_start'. We don't update the metadata | |
511 | * until the array goes inactive or readonly though. | |
512 | * Just check if we need to fiddle spares. | |
513 | */ | |
0c0c44db | 514 | a->container->ss->set_array_state(a, a->curr_state <= clean); |
549e9569 NB |
515 | check_degraded = 1; |
516 | } | |
517 | ||
00e02142 DW |
518 | if (!deactivate && |
519 | a->curr_action == idle && | |
549e9569 | 520 | a->prev_action == recover) { |
0a6bdbee DW |
521 | /* A recovery has finished. Some disks may be in sync now, |
522 | * and the array may no longer be degraded | |
523 | */ | |
549e9569 | 524 | for (mdi = a->info.devs ; mdi ; mdi = mdi->next) { |
8d45d196 DW |
525 | a->container->ss->set_disk(a, mdi->disk.raid_disk, |
526 | mdi->curr_state); | |
549e9569 NB |
527 | if (! (mdi->curr_state & DS_INSYNC)) |
528 | check_degraded = 1; | |
3a5d0473 | 529 | count++; |
549e9569 | 530 | } |
3a5d0473 KW |
531 | if (count != a->info.array.raid_disks) |
532 | check_degraded = 1; | |
549e9569 NB |
533 | } |
534 | ||
0f99b4bd N |
535 | if (!deactivate && |
536 | a->curr_action == reshape && | |
537 | a->prev_action != reshape) | |
538 | /* reshape was requested by mdadm. Need to see if | |
539 | * new devices have been added. Manager does that | |
540 | * when it sees check_reshape | |
541 | */ | |
542 | check_reshape = 1; | |
543 | ||
92967543 DW |
544 | /* Check for failures and if found: |
545 | * 1/ Record the failure in the metadata and unblock the device. | |
546 | * FIXME update the kernel to stop notifying on failed drives when | |
547 | * the array is readonly and we have cleared 'blocked' | |
548 | * 2/ Try to remove the device if the array is writable, or can be | |
549 | * made writable. | |
550 | */ | |
549e9569 NB |
551 | for (mdi = a->info.devs ; mdi ; mdi = mdi->next) { |
552 | if (mdi->curr_state & DS_FAULTY) { | |
8d45d196 DW |
553 | a->container->ss->set_disk(a, mdi->disk.raid_disk, |
554 | mdi->curr_state); | |
549e9569 | 555 | check_degraded = 1; |
1c278e81 N |
556 | if (mdi->curr_state & DS_BLOCKED) |
557 | mdi->next_state |= DS_UNBLOCK; | |
92967543 DW |
558 | if (a->curr_state == read_auto) { |
559 | a->container->ss->set_array_state(a, 0); | |
560 | a->next_state = active; | |
561 | } | |
562 | if (a->curr_state > readonly) | |
563 | mdi->next_state |= DS_REMOVE; | |
549e9569 NB |
564 | } |
565 | } | |
566 | ||
484240d8 DW |
567 | /* Check for recovery checkpoint notifications. We need to be a |
568 | * minimum distance away from the last checkpoint to prevent | |
aad6f216 N |
569 | * over checkpointing. Note reshape checkpointing is handled |
570 | * in the second branch. | |
484240d8 DW |
571 | */ |
572 | if (sync_completed > a->last_checkpoint && | |
573 | sync_completed - a->last_checkpoint > a->info.component_size >> 4 && | |
4f0a7acc DW |
574 | a->curr_action > reshape) { |
575 | /* A (non-reshape) sync_action has reached a checkpoint. | |
576 | * Record the updated position in the metadata | |
577 | */ | |
578 | a->last_checkpoint = sync_completed; | |
579 | a->container->ss->set_array_state(a, a->curr_state <= clean); | |
aad6f216 | 580 | } else if ((a->curr_action == idle && a->prev_action == reshape) || |
fc54fe7a JS |
581 | (a->curr_action == reshape && |
582 | sync_completed > a->last_checkpoint)) { | |
aad6f216 N |
583 | /* Reshape has progressed or completed so we need to |
584 | * update the array state - and possibly the array size | |
585 | */ | |
2a9f8409 AK |
586 | if (sync_completed != 0) |
587 | a->last_checkpoint = sync_completed; | |
6d4225a1 AK |
588 | /* We might need to update last_checkpoint depending on |
589 | * the reason that reshape finished. | |
590 | * if array reshape is really finished: | |
591 | * set check point to the end, this allows | |
592 | * set_array_state() to finalize reshape in metadata | |
593 | * if reshape if broken: do not set checkpoint to the end | |
594 | * this allows for reshape restart from checkpoint | |
595 | */ | |
596 | if ((a->curr_action != reshape) && | |
597 | (a->prev_action == reshape)) { | |
598 | char buf[40]; | |
599 | if ((sysfs_get_str(&a->info, NULL, | |
600 | "reshape_position", | |
601 | buf, | |
602 | sizeof(buf)) >= 0) && | |
603 | strncmp(buf, "none", 4) == 0) | |
604 | a->last_checkpoint = a->info.component_size; | |
605 | } | |
aad6f216 | 606 | a->container->ss->set_array_state(a, a->curr_state <= clean); |
2a9f8409 | 607 | a->last_checkpoint = sync_completed; |
aad6f216 N |
608 | } |
609 | ||
610 | if (sync_completed > a->last_checkpoint) | |
484240d8 | 611 | a->last_checkpoint = sync_completed; |
484240d8 | 612 | |
955aa6cf PB |
613 | if (sync_completed >= a->info.component_size) |
614 | a->last_checkpoint = 0; | |
615 | ||
a4921f30 | 616 | a->container->ss->sync_metadata(a->container); |
1ade5cc1 | 617 | dprintf("(%d): state:%s action:%s next(", a->info.container_member, |
4065aa81 | 618 | array_states[a->curr_state], sync_actions[a->curr_action]); |
549e9569 NB |
619 | |
620 | /* Effect state changes in the array */ | |
4e6e574a | 621 | if (a->next_state != bad_word) { |
1ade5cc1 | 622 | dprintf_cont(" state:%s", array_states[a->next_state]); |
549e9569 | 623 | write_attr(array_states[a->next_state], a->info.state_fd); |
4e6e574a DW |
624 | } |
625 | if (a->next_action != bad_action) { | |
549e9569 | 626 | write_attr(sync_actions[a->next_action], a->action_fd); |
1ade5cc1 | 627 | dprintf_cont(" action:%s", sync_actions[a->next_action]); |
4e6e574a | 628 | } |
549e9569 | 629 | for (mdi = a->info.devs; mdi ; mdi = mdi->next) { |
92967543 | 630 | if (mdi->next_state & DS_UNBLOCK) { |
1ade5cc1 | 631 | dprintf_cont(" %d:-blocked", mdi->disk.raid_disk); |
92967543 DW |
632 | write_attr("-blocked", mdi->state_fd); |
633 | } | |
634 | ||
635 | if ((mdi->next_state & DS_REMOVE) && mdi->state_fd >= 0) { | |
57632f4a | 636 | int remove_result; |
8d45d196 | 637 | |
68226a80 N |
638 | /* The kernel may not be able to immediately remove the |
639 | * disk. In that case we wait a little while and | |
640 | * try again. | |
8d45d196 | 641 | */ |
57632f4a NB |
642 | remove_result = write_attr("remove", mdi->state_fd); |
643 | if (remove_result > 0) { | |
1ade5cc1 | 644 | dprintf_cont(" %d:removed", mdi->disk.raid_disk); |
8d45d196 | 645 | close(mdi->state_fd); |
e40512fd | 646 | close(mdi->recovery_fd); |
6dc1785f TM |
647 | close(mdi->bb_fd); |
648 | close(mdi->ubb_fd); | |
8d45d196 | 649 | mdi->state_fd = -1; |
68226a80 N |
650 | } else |
651 | ret |= ARRAY_BUSY; | |
8d45d196 | 652 | } |
4e6e574a | 653 | if (mdi->next_state & DS_INSYNC) { |
549e9569 | 654 | write_attr("+in_sync", mdi->state_fd); |
1ade5cc1 | 655 | dprintf_cont(" %d:+in_sync", mdi->disk.raid_disk); |
4e6e574a | 656 | } |
549e9569 | 657 | } |
1ade5cc1 | 658 | dprintf_cont(" )\n"); |
549e9569 NB |
659 | |
660 | /* move curr_ to prev_ */ | |
661 | a->prev_state = a->curr_state; | |
662 | ||
663 | a->prev_action = a->curr_action; | |
664 | ||
665 | for (mdi = a->info.devs; mdi ; mdi = mdi->next) { | |
666 | mdi->prev_state = mdi->curr_state; | |
667 | mdi->next_state = 0; | |
668 | } | |
669 | ||
0f99b4bd | 670 | if (check_degraded || check_reshape) { |
7e1432fb | 671 | /* manager will do the actual check */ |
0f99b4bd N |
672 | if (check_degraded) |
673 | a->check_degraded = 1; | |
674 | if (check_reshape) | |
675 | a->check_reshape = 1; | |
7e1432fb NB |
676 | signal_manager(); |
677 | } | |
678 | ||
2a0bb19e DW |
679 | if (deactivate) |
680 | a->container = NULL; | |
681 | ||
77b3ac8c | 682 | return ret; |
549e9569 NB |
683 | } |
684 | ||
0af73f61 DW |
685 | static struct mdinfo * |
686 | find_device(struct active_array *a, int major, int minor) | |
687 | { | |
688 | struct mdinfo *mdi; | |
689 | ||
690 | for (mdi = a->info.devs ; mdi ; mdi = mdi->next) | |
691 | if (mdi->disk.major == major && mdi->disk.minor == minor) | |
692 | return mdi; | |
693 | ||
694 | return NULL; | |
695 | } | |
696 | ||
697 | static void reconcile_failed(struct active_array *aa, struct mdinfo *failed) | |
698 | { | |
699 | struct active_array *a; | |
700 | struct mdinfo *victim; | |
701 | ||
702 | for (a = aa; a; a = a->next) { | |
ba714450 | 703 | if (!a->container || a->to_remove) |
0af73f61 DW |
704 | continue; |
705 | victim = find_device(a, failed->disk.major, failed->disk.minor); | |
706 | if (!victim) | |
707 | continue; | |
708 | ||
709 | if (!(victim->curr_state & DS_FAULTY)) | |
710 | write_attr("faulty", victim->state_fd); | |
711 | } | |
712 | } | |
713 | ||
4e6e574a DW |
714 | #ifdef DEBUG |
715 | static void dprint_wake_reasons(fd_set *fds) | |
716 | { | |
717 | int i; | |
718 | char proc_path[256]; | |
719 | char link[256]; | |
720 | char *basename; | |
721 | int rv; | |
722 | ||
723 | fprintf(stderr, "monitor: wake ( "); | |
724 | for (i = 0; i < FD_SETSIZE; i++) { | |
725 | if (FD_ISSET(i, fds)) { | |
726 | sprintf(proc_path, "/proc/%d/fd/%d", | |
727 | (int) getpid(), i); | |
728 | ||
729 | rv = readlink(proc_path, link, sizeof(link) - 1); | |
730 | if (rv < 0) { | |
731 | fprintf(stderr, "%d:unknown ", i); | |
732 | continue; | |
733 | } | |
734 | link[rv] = '\0'; | |
735 | basename = strrchr(link, '/'); | |
736 | fprintf(stderr, "%d:%s ", | |
737 | i, basename ? ++basename : link); | |
738 | } | |
739 | } | |
740 | fprintf(stderr, ")\n"); | |
741 | } | |
742 | #endif | |
743 | ||
1eb252b8 N |
744 | int monitor_loop_cnt; |
745 | ||
4d43913c | 746 | static int wait_and_act(struct supertype *container, int nowait) |
549e9569 NB |
747 | { |
748 | fd_set rfds; | |
749 | int maxfd = 0; | |
e0d6609f | 750 | struct active_array **aap = &container->arrays; |
1ed3f387 | 751 | struct active_array *a, **ap; |
549e9569 | 752 | int rv; |
0af73f61 | 753 | struct mdinfo *mdi; |
6144ed44 | 754 | static unsigned int dirty_arrays = ~0; /* start at some non-zero value */ |
549e9569 NB |
755 | |
756 | FD_ZERO(&rfds); | |
757 | ||
1ed3f387 NB |
758 | for (ap = aap ; *ap ;) { |
759 | a = *ap; | |
760 | /* once an array has been deactivated we want to | |
761 | * ask the manager to discard it. | |
2a0bb19e | 762 | */ |
ba714450 | 763 | if (!a->container || a->to_remove) { |
1ed3f387 NB |
764 | if (discard_this) { |
765 | ap = &(*ap)->next; | |
766 | continue; | |
767 | } | |
768 | *ap = a->next; | |
769 | a->next = NULL; | |
770 | discard_this = a; | |
771 | signal_manager(); | |
2a0bb19e | 772 | continue; |
1ed3f387 | 773 | } |
2a0bb19e | 774 | |
549e9569 NB |
775 | add_fd(&rfds, &maxfd, a->info.state_fd); |
776 | add_fd(&rfds, &maxfd, a->action_fd); | |
484240d8 | 777 | add_fd(&rfds, &maxfd, a->sync_completed_fd); |
6dc1785f | 778 | for (mdi = a->info.devs ; mdi ; mdi = mdi->next) { |
549e9569 | 779 | add_fd(&rfds, &maxfd, mdi->state_fd); |
6dc1785f TM |
780 | add_fd(&rfds, &maxfd, mdi->bb_fd); |
781 | add_fd(&rfds, &maxfd, mdi->ubb_fd); | |
782 | } | |
1ed3f387 NB |
783 | |
784 | ap = &(*ap)->next; | |
549e9569 NB |
785 | } |
786 | ||
6144ed44 DW |
787 | if (manager_ready && (*aap == NULL || (sigterm && !dirty_arrays))) { |
788 | /* No interesting arrays, or we have been told to | |
789 | * terminate and everything is clean. Lets see about | |
790 | * exiting. Note that blocking at this point is not a | |
791 | * problem as there are no active arrays, there is | |
792 | * nothing that we need to be ready to do. | |
e0d6609f | 793 | */ |
d998b738 N |
794 | int fd; |
795 | if (sigterm) | |
4dd2df09 | 796 | fd = open_dev_excl(container->devnm); |
d998b738 | 797 | else |
4dd2df09 | 798 | fd = open_dev_flags(container->devnm, O_RDONLY|O_EXCL); |
e0d6609f NB |
799 | if (fd >= 0 || errno != EBUSY) { |
800 | /* OK, we are safe to leave */ | |
6144ed44 DW |
801 | if (sigterm && !dirty_arrays) |
802 | dprintf("caught sigterm, all clean... exiting\n"); | |
803 | else | |
804 | dprintf("no arrays to monitor... exiting\n"); | |
fa716c83 N |
805 | if (!sigterm) |
806 | /* On SIGTERM, someone (the take-over mdmon) will | |
807 | * clean up | |
808 | */ | |
4dd2df09 | 809 | remove_pidfile(container->devnm); |
e0d6609f NB |
810 | exit_now = 1; |
811 | signal_manager(); | |
6f4cdfd9 | 812 | close(fd); |
e0d6609f NB |
813 | exit(0); |
814 | } | |
815 | } | |
816 | ||
549e9569 | 817 | if (!nowait) { |
4d43913c | 818 | sigset_t set; |
d998b738 N |
819 | struct timespec ts; |
820 | ts.tv_sec = 24*3600; | |
821 | ts.tv_nsec = 0; | |
68226a80 | 822 | if (*aap == NULL || container->retry_soon) { |
d998b738 N |
823 | /* just waiting to get O_EXCL access */ |
824 | ts.tv_sec = 0; | |
825 | ts.tv_nsec = 20000000ULL; | |
826 | } | |
4d43913c NB |
827 | sigprocmask(SIG_UNBLOCK, NULL, &set); |
828 | sigdelset(&set, SIGUSR1); | |
1eb252b8 | 829 | monitor_loop_cnt |= 1; |
d998b738 | 830 | rv = pselect(maxfd+1, NULL, NULL, &rfds, &ts, &set); |
1eb252b8 | 831 | monitor_loop_cnt += 1; |
c3719360 MW |
832 | if (rv == -1) { |
833 | if (errno == EINTR) { | |
834 | rv = 0; | |
42d902d9 | 835 | FD_ZERO(&rfds); |
c3719360 MW |
836 | dprintf("monitor: caught signal\n"); |
837 | } else | |
838 | dprintf("monitor: error %d in pselect\n", | |
839 | errno); | |
840 | } | |
4e6e574a | 841 | #ifdef DEBUG |
c3719360 MW |
842 | else |
843 | dprint_wake_reasons(&rfds); | |
4e6e574a | 844 | #endif |
68226a80 | 845 | container->retry_soon = 0; |
549e9569 NB |
846 | } |
847 | ||
2e735d19 NB |
848 | if (update_queue) { |
849 | struct metadata_update *this; | |
850 | ||
851 | for (this = update_queue; this ; this = this->next) | |
852 | container->ss->process_update(container, this); | |
853 | ||
854 | update_queue_handled = update_queue; | |
855 | update_queue = NULL; | |
856 | signal_manager(); | |
857 | container->ss->sync_metadata(container); | |
858 | } | |
859 | ||
3d2c4fc7 | 860 | rv = 0; |
6144ed44 | 861 | dirty_arrays = 0; |
1ed3f387 | 862 | for (a = *aap; a ; a = a->next) { |
6144ed44 | 863 | |
2a0bb19e | 864 | if (a->replaces && !discard_this) { |
549e9569 NB |
865 | struct active_array **ap; |
866 | for (ap = &a->next; *ap && *ap != a->replaces; | |
867 | ap = & (*ap)->next) | |
868 | ; | |
869 | if (*ap) | |
870 | *ap = (*ap)->next; | |
871 | discard_this = a->replaces; | |
872 | a->replaces = NULL; | |
6c3fb95c | 873 | /* FIXME check if device->state_fd need to be cleared?*/ |
1ed3f387 | 874 | signal_manager(); |
549e9569 | 875 | } |
ba714450 | 876 | if (a->container && !a->to_remove) { |
42d902d9 | 877 | int ret = read_and_act(a, &rfds); |
140d3685 | 878 | rv |= 1; |
77b3ac8c | 879 | dirty_arrays += !!(ret & ARRAY_DIRTY); |
140d3685 DW |
880 | /* when terminating stop manipulating the array after it |
881 | * is clean, but make sure read_and_act() is given a | |
882 | * chance to handle 'active_idle' | |
883 | */ | |
77b3ac8c | 884 | if (sigterm && !(ret & ARRAY_DIRTY)) |
140d3685 | 885 | a->container = NULL; /* stop touching this array */ |
68226a80 N |
886 | if (ret & ARRAY_BUSY) |
887 | container->retry_soon = 1; | |
6144ed44 | 888 | } |
549e9569 | 889 | } |
0af73f61 DW |
890 | |
891 | /* propagate failures across container members */ | |
1ed3f387 | 892 | for (a = *aap; a ; a = a->next) { |
ba714450 | 893 | if (!a->container || a->to_remove) |
0af73f61 DW |
894 | continue; |
895 | for (mdi = a->info.devs ; mdi ; mdi = mdi->next) | |
896 | if (mdi->curr_state & DS_FAULTY) | |
1ed3f387 | 897 | reconcile_failed(*aap, mdi); |
0af73f61 DW |
898 | } |
899 | ||
549e9569 NB |
900 | return rv; |
901 | } | |
902 | ||
903 | void do_monitor(struct supertype *container) | |
904 | { | |
905 | int rv; | |
906 | int first = 1; | |
907 | do { | |
4d43913c | 908 | rv = wait_and_act(container, first); |
549e9569 NB |
909 | first = 0; |
910 | } while (rv >= 0); | |
911 | } |