]> git.ipfire.org Git - thirdparty/mdadm.git/blame - monitor.c
imsm: determine failed indexes from the most up-to-date disk
[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);
4065aa81
DW
287 dprintf("%s(%d): state:%s action:%s next(", __func__, a->info.container_member,
288 array_states[a->curr_state], sync_actions[a->curr_action]);
549e9569
NB
289
290 /* Effect state changes in the array */
4e6e574a
DW
291 if (a->next_state != bad_word) {
292 dprintf(" state:%s", array_states[a->next_state]);
549e9569 293 write_attr(array_states[a->next_state], a->info.state_fd);
4e6e574a
DW
294 }
295 if (a->next_action != bad_action) {
549e9569 296 write_attr(sync_actions[a->next_action], a->action_fd);
4065aa81 297 dprintf(" action:%s", sync_actions[a->next_action]);
4e6e574a 298 }
549e9569 299 for (mdi = a->info.devs; mdi ; mdi = mdi->next) {
92967543
DW
300 if (mdi->next_state & DS_UNBLOCK) {
301 dprintf(" %d:-blocked", mdi->disk.raid_disk);
302 write_attr("-blocked", mdi->state_fd);
303 }
304
305 if ((mdi->next_state & DS_REMOVE) && mdi->state_fd >= 0) {
57632f4a 306 int remove_result;
8d45d196 307
8d45d196
DW
308 /* the kernel may not be able to immediately remove the
309 * disk, we can simply wait until the next event to try
310 * again.
311 */
57632f4a
NB
312 remove_result = write_attr("remove", mdi->state_fd);
313 if (remove_result > 0) {
4e6e574a 314 dprintf(" %d:removed", mdi->disk.raid_disk);
8d45d196
DW
315 close(mdi->state_fd);
316 mdi->state_fd = -1;
317 }
318 }
4e6e574a 319 if (mdi->next_state & DS_INSYNC) {
549e9569 320 write_attr("+in_sync", mdi->state_fd);
4e6e574a
DW
321 dprintf(" %d:+in_sync", mdi->disk.raid_disk);
322 }
549e9569 323 }
4e6e574a 324 dprintf(" )\n");
549e9569
NB
325
326 /* move curr_ to prev_ */
327 a->prev_state = a->curr_state;
328
329 a->prev_action = a->curr_action;
330
331 for (mdi = a->info.devs; mdi ; mdi = mdi->next) {
332 mdi->prev_state = mdi->curr_state;
333 mdi->next_state = 0;
334 }
335
7e1432fb
NB
336 if (check_degraded) {
337 /* manager will do the actual check */
338 a->check_degraded = 1;
339 signal_manager();
340 }
341
2a0bb19e
DW
342 if (deactivate)
343 a->container = NULL;
344
549e9569
NB
345 return 1;
346}
347
0af73f61
DW
348static struct mdinfo *
349find_device(struct active_array *a, int major, int minor)
350{
351 struct mdinfo *mdi;
352
353 for (mdi = a->info.devs ; mdi ; mdi = mdi->next)
354 if (mdi->disk.major == major && mdi->disk.minor == minor)
355 return mdi;
356
357 return NULL;
358}
359
360static void reconcile_failed(struct active_array *aa, struct mdinfo *failed)
361{
362 struct active_array *a;
363 struct mdinfo *victim;
364
365 for (a = aa; a; a = a->next) {
366 if (!a->container)
367 continue;
368 victim = find_device(a, failed->disk.major, failed->disk.minor);
369 if (!victim)
370 continue;
371
372 if (!(victim->curr_state & DS_FAULTY))
373 write_attr("faulty", victim->state_fd);
374 }
375}
376
4e6e574a
DW
377#ifdef DEBUG
378static void dprint_wake_reasons(fd_set *fds)
379{
380 int i;
381 char proc_path[256];
382 char link[256];
383 char *basename;
384 int rv;
385
386 fprintf(stderr, "monitor: wake ( ");
387 for (i = 0; i < FD_SETSIZE; i++) {
388 if (FD_ISSET(i, fds)) {
389 sprintf(proc_path, "/proc/%d/fd/%d",
390 (int) getpid(), i);
391
392 rv = readlink(proc_path, link, sizeof(link) - 1);
393 if (rv < 0) {
394 fprintf(stderr, "%d:unknown ", i);
395 continue;
396 }
397 link[rv] = '\0';
398 basename = strrchr(link, '/');
399 fprintf(stderr, "%d:%s ",
400 i, basename ? ++basename : link);
401 }
402 }
403 fprintf(stderr, ")\n");
404}
405#endif
406
1eb252b8
N
407int monitor_loop_cnt;
408
4d43913c 409static int wait_and_act(struct supertype *container, int nowait)
549e9569
NB
410{
411 fd_set rfds;
412 int maxfd = 0;
e0d6609f 413 struct active_array **aap = &container->arrays;
1ed3f387 414 struct active_array *a, **ap;
549e9569 415 int rv;
0af73f61 416 struct mdinfo *mdi;
549e9569
NB
417
418 FD_ZERO(&rfds);
419
1ed3f387
NB
420 for (ap = aap ; *ap ;) {
421 a = *ap;
422 /* once an array has been deactivated we want to
423 * ask the manager to discard it.
2a0bb19e 424 */
1ed3f387
NB
425 if (!a->container) {
426 if (discard_this) {
427 ap = &(*ap)->next;
428 continue;
429 }
430 *ap = a->next;
431 a->next = NULL;
432 discard_this = a;
433 signal_manager();
2a0bb19e 434 continue;
1ed3f387 435 }
2a0bb19e 436
549e9569
NB
437 add_fd(&rfds, &maxfd, a->info.state_fd);
438 add_fd(&rfds, &maxfd, a->action_fd);
439 for (mdi = a->info.devs ; mdi ; mdi = mdi->next)
440 add_fd(&rfds, &maxfd, mdi->state_fd);
1ed3f387
NB
441
442 ap = &(*ap)->next;
549e9569
NB
443 }
444
e0d6609f
NB
445 if (manager_ready && *aap == NULL) {
446 /* No interesting arrays. Lets see about exiting.
447 * Note that blocking at this point is not a problem
448 * as there are no active arrays, there is nothing that
449 * we need to be ready to do.
450 */
451 int fd = open(container->device_name, O_RDONLY|O_EXCL);
452 if (fd >= 0 || errno != EBUSY) {
453 /* OK, we are safe to leave */
4e6e574a 454 dprintf("no arrays to monitor... exiting\n");
207aac36 455 remove_pidfile(container->devname);
e0d6609f
NB
456 exit_now = 1;
457 signal_manager();
e0d6609f
NB
458 exit(0);
459 }
460 }
461
549e9569 462 if (!nowait) {
4d43913c
NB
463 sigset_t set;
464 sigprocmask(SIG_UNBLOCK, NULL, &set);
465 sigdelset(&set, SIGUSR1);
1eb252b8 466 monitor_loop_cnt |= 1;
4d43913c 467 rv = pselect(maxfd+1, &rfds, NULL, NULL, NULL, &set);
1eb252b8 468 monitor_loop_cnt += 1;
bfa44e2e
NB
469 if (rv == -1 && errno == EINTR)
470 rv = 0;
4e6e574a
DW
471 #ifdef DEBUG
472 dprint_wake_reasons(&rfds);
473 #endif
474
549e9569
NB
475 }
476
2e735d19
NB
477 if (update_queue) {
478 struct metadata_update *this;
479
480 for (this = update_queue; this ; this = this->next)
481 container->ss->process_update(container, this);
482
483 update_queue_handled = update_queue;
484 update_queue = NULL;
485 signal_manager();
486 container->ss->sync_metadata(container);
487 }
488
1ed3f387 489 for (a = *aap; a ; a = a->next) {
2a0bb19e 490 if (a->replaces && !discard_this) {
549e9569
NB
491 struct active_array **ap;
492 for (ap = &a->next; *ap && *ap != a->replaces;
493 ap = & (*ap)->next)
494 ;
495 if (*ap)
496 *ap = (*ap)->next;
497 discard_this = a->replaces;
498 a->replaces = NULL;
6c3fb95c 499 /* FIXME check if device->state_fd need to be cleared?*/
1ed3f387 500 signal_manager();
549e9569 501 }
2a0bb19e
DW
502 if (a->container)
503 rv += read_and_act(a);
549e9569 504 }
0af73f61
DW
505
506 /* propagate failures across container members */
1ed3f387 507 for (a = *aap; a ; a = a->next) {
0af73f61
DW
508 if (!a->container)
509 continue;
510 for (mdi = a->info.devs ; mdi ; mdi = mdi->next)
511 if (mdi->curr_state & DS_FAULTY)
1ed3f387 512 reconcile_failed(*aap, mdi);
0af73f61
DW
513 }
514
549e9569
NB
515 return rv;
516}
517
518void do_monitor(struct supertype *container)
519{
520 int rv;
521 int first = 1;
522 do {
4d43913c 523 rv = wait_and_act(container, first);
549e9569
NB
524 first = 0;
525 } while (rv >= 0);
526}