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