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