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