]> git.ipfire.org Git - thirdparty/mdadm.git/blob - monitor.c
'mdadm --wait-clean' wait for array to be marked clean
[thirdparty/mdadm.git] / monitor.c
1
2 #include "mdadm.h"
3 #include "mdmon.h"
4 #include <sys/syscall.h>
5 #include <sys/select.h>
6 #include <signal.h>
7
8 static char *array_states[] = {
9 "clear", "inactive", "suspended", "readonly", "read-auto",
10 "clean", "active", "write-pending", "active-idle", NULL };
11 static char *sync_actions[] = {
12 "idle", "reshape", "resync", "recover", "check", "repair", NULL
13 };
14
15 static int write_attr(char *attr, int fd)
16 {
17 return write(fd, attr, strlen(attr));
18 }
19
20 static 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
29 static 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
50 int get_resync_start(struct active_array *a)
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 }
63
64
65 static 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;
72 return (enum array_state) sysfs_match_word(buf, array_states);
73 }
74
75 static 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;
82 return (enum sync_action) sysfs_match_word(buf, sync_actions);
83 }
84
85 int 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) {
97 if (sysfs_attr_match(cp, "faulty"))
98 rv |= DS_FAULTY;
99 if (sysfs_attr_match(cp, "in_sync"))
100 rv |= DS_INSYNC;
101 if (sysfs_attr_match(cp, "write_mostly"))
102 rv |= DS_WRITE_MOSTLY;
103 if (sysfs_attr_match(cp, "spare"))
104 rv |= DS_SPARE;
105 if (sysfs_attr_match(cp, "blocked"))
106 rv |= DS_BLOCKED;
107 cp = strchr(cp, ',');
108 if (cp)
109 cp++;
110 }
111 return rv;
112 }
113
114 static void signal_manager(void)
115 {
116 /* tgkill(getpid(), mon_tid, SIGUSR1); */
117 int pid = getpid();
118 syscall(SYS_tgkill, pid, mgr_tid, SIGUSR1);
119 }
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"
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
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?
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.
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'.
155 *
156 * deal with resync
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.
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
178 static int read_and_act(struct active_array *a)
179 {
180 int check_degraded = 0;
181 int deactivate = 0;
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;
191 if (mdi->state_fd >= 0)
192 mdi->curr_state = read_dev_state(mdi->state_fd);
193 }
194
195 if (a->curr_state <= inactive &&
196 a->prev_state > inactive) {
197 /* array has been stopped */
198 get_resync_start(a);
199 a->container->ss->set_array_state(a, 1);
200 a->next_state = clear;
201 deactivate = 1;
202 }
203 if (a->curr_state == write_pending) {
204 get_resync_start(a);
205 a->container->ss->set_array_state(a, 0);
206 a->next_state = active;
207 }
208 if (a->curr_state == active_idle) {
209 /* Set array to 'clean' FIRST, then mark clean
210 * in the metadata
211 */
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);
217 }
218
219 if (a->curr_state == readonly) {
220 /* Well, I'm ready to handle things. If readonly
221 * wasn't requested, transition to read-auto.
222 */
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 }
235 }
236
237 if (!deactivate &&
238 a->curr_action == idle &&
239 a->prev_action == resync) {
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 */
245 get_resync_start(a);
246 a->container->ss->set_array_state(a, a->curr_state <= clean);
247 check_degraded = 1;
248 }
249
250 if (!deactivate &&
251 a->curr_action == idle &&
252 a->prev_action == recover) {
253 /* A recovery has finished. Some disks may be in sync now,
254 * and the array may no longer be degraded
255 */
256 for (mdi = a->info.devs ; mdi ; mdi = mdi->next) {
257 a->container->ss->set_disk(a, mdi->disk.raid_disk,
258 mdi->curr_state);
259 if (! (mdi->curr_state & DS_INSYNC))
260 check_degraded = 1;
261 }
262 }
263
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 */
271 for (mdi = a->info.devs ; mdi ; mdi = mdi->next) {
272 if (mdi->curr_state & DS_FAULTY) {
273 a->container->ss->set_disk(a, mdi->disk.raid_disk,
274 mdi->curr_state);
275 check_degraded = 1;
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;
283 }
284 }
285
286 a->container->ss->sync_metadata(a->container);
287 dprintf("%s: update[%d]: (", __func__, a->info.container_member);
288
289 /* Effect state changes in the array */
290 if (a->next_state != bad_word) {
291 dprintf(" state:%s", array_states[a->next_state]);
292 write_attr(array_states[a->next_state], a->info.state_fd);
293 }
294 if (a->next_action != bad_action) {
295 write_attr(sync_actions[a->next_action], a->action_fd);
296 dprintf(" action:%s", array_states[a->next_state]);
297 }
298 for (mdi = a->info.devs; mdi ; mdi = mdi->next) {
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) {
305 int remove_result;
306
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 */
311 remove_result = write_attr("remove", mdi->state_fd);
312 if (remove_result > 0) {
313 dprintf(" %d:removed", mdi->disk.raid_disk);
314 close(mdi->state_fd);
315 mdi->state_fd = -1;
316 }
317 }
318 if (mdi->next_state & DS_INSYNC) {
319 write_attr("+in_sync", mdi->state_fd);
320 dprintf(" %d:+in_sync", mdi->disk.raid_disk);
321 }
322 }
323 dprintf(" )\n");
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
335 if (check_degraded) {
336 /* manager will do the actual check */
337 a->check_degraded = 1;
338 signal_manager();
339 }
340
341 if (deactivate)
342 a->container = NULL;
343
344 return 1;
345 }
346
347 static struct mdinfo *
348 find_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
359 static 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
376 #ifdef DEBUG
377 static 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
406 int monitor_loop_cnt;
407
408 static int wait_and_act(struct supertype *container, int nowait)
409 {
410 fd_set rfds;
411 int maxfd = 0;
412 struct active_array **aap = &container->arrays;
413 struct active_array *a, **ap;
414 int rv;
415 struct mdinfo *mdi;
416
417 FD_ZERO(&rfds);
418
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.
423 */
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();
433 continue;
434 }
435
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);
440
441 ap = &(*ap)->next;
442 }
443
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 */
453 dprintf("no arrays to monitor... exiting\n");
454 remove_pidfile(container->devname);
455 exit_now = 1;
456 signal_manager();
457 exit(0);
458 }
459 }
460
461 if (!nowait) {
462 sigset_t set;
463 sigprocmask(SIG_UNBLOCK, NULL, &set);
464 sigdelset(&set, SIGUSR1);
465 monitor_loop_cnt |= 1;
466 rv = pselect(maxfd+1, &rfds, NULL, NULL, NULL, &set);
467 monitor_loop_cnt += 1;
468 if (rv == -1 && errno == EINTR)
469 rv = 0;
470 #ifdef DEBUG
471 dprint_wake_reasons(&rfds);
472 #endif
473
474 }
475
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
488 for (a = *aap; a ; a = a->next) {
489 if (a->replaces && !discard_this) {
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;
498 /* FIXME check if device->state_fd need to be cleared?*/
499 signal_manager();
500 }
501 if (a->container)
502 rv += read_and_act(a);
503 }
504
505 /* propagate failures across container members */
506 for (a = *aap; a ; a = a->next) {
507 if (!a->container)
508 continue;
509 for (mdi = a->info.devs ; mdi ; mdi = mdi->next)
510 if (mdi->curr_state & DS_FAULTY)
511 reconcile_failed(*aap, mdi);
512 }
513
514 return rv;
515 }
516
517 void do_monitor(struct supertype *container)
518 {
519 int rv;
520 int first = 1;
521 do {
522 rv = wait_and_act(container, first);
523 first = 0;
524 } while (rv >= 0);
525 }