]> git.ipfire.org Git - thirdparty/mdadm.git/blob - managemon.c
util: make env checking more generic
[thirdparty/mdadm.git] / managemon.c
1 /*
2 * mdmon - monitor external metadata arrays
3 *
4 * Copyright (C) 2007-2008 Neil Brown <neilb@suse.de>
5 * Copyright (C) 2007-2008 Intel Corporation
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 */
20
21 /*
22 * The management thread for monitoring active md arrays.
23 * This thread does things which might block such as memory
24 * allocation.
25 * In particular:
26 *
27 * - Find out about new arrays in this container.
28 * Allocate the data structures and open the files.
29 *
30 * For this we watch /proc/mdstat and find new arrays with
31 * metadata type that confirms sharing. e.g. "md4"
32 * When we find a new array we slip it into the list of
33 * arrays and signal 'monitor' by writing to a pipe.
34 *
35 * - Respond to reshape requests by allocating new data structures
36 * and opening new files.
37 *
38 * These come as a change to raid_disks. We allocate a new
39 * version of the data structures and slip it into the list.
40 * 'monitor' will notice and release the old version.
41 * Changes to level, chunksize, layout.. do not need re-allocation.
42 * Reductions in raid_disks don't really either, but we handle
43 * them the same way for consistency.
44 *
45 * - When a device is added to the container, we add it to the metadata
46 * as a spare.
47 *
48 * - Deal with degraded array
49 * We only do this when first noticing the array is degraded.
50 * This can be when we first see the array, when sync completes or
51 * when recovery completes.
52 *
53 * Check if number of failed devices suggests recovery is needed, and
54 * skip if not.
55 * Ask metadata to allocate a spare device
56 * Add device as not in_sync and give a role
57 * Update metadata.
58 * Open sysfs files and pass to monitor.
59 * Make sure that monitor Starts recovery....
60 *
61 * - Pass on metadata updates from external programs such as
62 * mdadm creating a new array.
63 *
64 * This is most-messy.
65 * It might involve adding a new array or changing the status of
66 * a spare, or any reconfig that the kernel doesn't get involved in.
67 *
68 * The required updates are received via a named pipe. There will
69 * be one named pipe for each container. Each message contains a
70 * sync marker: 0x5a5aa5a5, A byte count, and the message. This is
71 * passed to the metadata handler which will interpret and process it.
72 * For 'DDF' messages are internal data blocks with the leading
73 * 'magic number' signifying what sort of data it is.
74 *
75 */
76
77 /*
78 * We select on /proc/mdstat and the named pipe.
79 * We create new arrays or updated version of arrays and slip
80 * them into the head of the list, then signal 'monitor' via a pipe write.
81 * 'monitor' will notice and place the old array on a return list.
82 * Metadata updates are placed on a queue just like they arrive
83 * from the named pipe.
84 *
85 * When new arrays are found based on correct metadata string, we
86 * need to identify them with an entry in the metadata. Maybe we require
87 * the metadata to be mdX/NN when NN is the index into an appropriate table.
88 *
89 */
90
91 /*
92 * List of tasks:
93 * - Watch for spares to be added to the container, and write updated
94 * metadata to them.
95 * - Watch for new arrays using this container, confirm they match metadata
96 * and if so, start monitoring them
97 * - Watch for spares being added to monitored arrays. This shouldn't
98 * happen, as we should do all the adding. Just remove them.
99 * - Watch for change in raid-disks, chunk-size, etc. Update metadata and
100 * start a reshape.
101 */
102 #ifndef _GNU_SOURCE
103 #define _GNU_SOURCE
104 #endif
105 #include "mdadm.h"
106 #include "mdmon.h"
107 #include <sys/syscall.h>
108 #include <sys/socket.h>
109 #include <signal.h>
110
111 static void close_aa(struct active_array *aa)
112 {
113 struct mdinfo *d;
114
115 for (d = aa->info.devs; d; d = d->next)
116 close(d->state_fd);
117
118 close(aa->action_fd);
119 close(aa->info.state_fd);
120 close(aa->resync_start_fd);
121 }
122
123 static void free_aa(struct active_array *aa)
124 {
125 /* Note that this doesn't close fds if they are being used
126 * by a clone. ->container will be set for a clone
127 */
128 dprintf("%s: devnum: %d\n", __func__, aa->devnum);
129 if (!aa->container)
130 close_aa(aa);
131 while (aa->info.devs) {
132 struct mdinfo *d = aa->info.devs;
133 aa->info.devs = d->next;
134 free(d);
135 }
136 free(aa);
137 }
138
139 static struct active_array *duplicate_aa(struct active_array *aa)
140 {
141 struct active_array *newa = malloc(sizeof(*newa));
142 struct mdinfo **dp1, **dp2;
143
144 *newa = *aa;
145 newa->next = NULL;
146 newa->replaces = NULL;
147 newa->info.next = NULL;
148
149 dp2 = &newa->info.devs;
150
151 for (dp1 = &aa->info.devs; *dp1; dp1 = &(*dp1)->next) {
152 struct mdinfo *d;
153 if ((*dp1)->state_fd < 0)
154 continue;
155
156 d = malloc(sizeof(*d));
157 *d = **dp1;
158 *dp2 = d;
159 dp2 = & d->next;
160 }
161 *dp2 = NULL;
162
163 return newa;
164 }
165
166 static void wakeup_monitor(void)
167 {
168 /* tgkill(getpid(), mon_tid, SIGUSR1); */
169 int pid = getpid();
170 syscall(SYS_tgkill, pid, mon_tid, SIGUSR1);
171 }
172
173 static void remove_old(void)
174 {
175 if (discard_this) {
176 discard_this->next = NULL;
177 free_aa(discard_this);
178 if (pending_discard == discard_this)
179 pending_discard = NULL;
180 discard_this = NULL;
181 wakeup_monitor();
182 }
183 }
184
185 static void replace_array(struct supertype *container,
186 struct active_array *old,
187 struct active_array *new)
188 {
189 /* To replace an array, we add it to the top of the list
190 * marked with ->replaces to point to the original.
191 * 'monitor' will take the original out of the list
192 * and put it on 'discard_this'. We take it from there
193 * and discard it.
194 */
195 remove_old();
196 while (pending_discard) {
197 while (discard_this == NULL)
198 sleep(1);
199 remove_old();
200 }
201 pending_discard = old;
202 new->replaces = old;
203 new->next = container->arrays;
204 container->arrays = new;
205 wakeup_monitor();
206 }
207
208 struct metadata_update *update_queue = NULL;
209 struct metadata_update *update_queue_handled = NULL;
210 struct metadata_update *update_queue_pending = NULL;
211
212 void check_update_queue(struct supertype *container)
213 {
214 while (update_queue_handled) {
215 struct metadata_update *this = update_queue_handled;
216 update_queue_handled = this->next;
217 free(this->buf);
218 if (this->space)
219 free(this->space);
220 free(this);
221 }
222 if (update_queue == NULL &&
223 update_queue_pending) {
224 update_queue = update_queue_pending;
225 update_queue_pending = NULL;
226 wakeup_monitor();
227 }
228 }
229
230 static void queue_metadata_update(struct metadata_update *mu)
231 {
232 struct metadata_update **qp;
233
234 qp = &update_queue_pending;
235 while (*qp)
236 qp = & ((*qp)->next);
237 *qp = mu;
238 }
239
240 static void add_disk_to_container(struct supertype *st, struct mdinfo *sd)
241 {
242 int dfd;
243 char nm[20];
244 struct metadata_update *update = NULL;
245 mdu_disk_info_t dk = {
246 .number = -1,
247 .major = sd->disk.major,
248 .minor = sd->disk.minor,
249 .raid_disk = -1,
250 .state = 0,
251 };
252
253 dprintf("%s: add %d:%d to container\n",
254 __func__, sd->disk.major, sd->disk.minor);
255
256 sprintf(nm, "%d:%d", sd->disk.major, sd->disk.minor);
257 dfd = dev_open(nm, O_RDWR);
258 if (dfd < 0)
259 return;
260
261 st->update_tail = &update;
262 st->ss->add_to_super(st, &dk, dfd, NULL);
263 st->ss->write_init_super(st);
264 queue_metadata_update(update);
265 st->update_tail = NULL;
266 }
267
268 static void manage_container(struct mdstat_ent *mdstat,
269 struct supertype *container)
270 {
271 /* The only thing of interest here is if a new device
272 * has been added to the container. We add it to the
273 * array ignoring any metadata on it.
274 * FIXME should we look for compatible metadata and take hints
275 * about spare assignment.... probably not.
276 */
277 if (mdstat->devcnt != container->devcnt) {
278 struct mdinfo **cdp, *cd, *di, *mdi;
279 int found;
280
281 /* read /sys/block/NAME/md/dev-??/block/dev to find out
282 * what is there, and compare with container->info.devs
283 * To see what is removed and what is added.
284 * These need to be remove from, or added to, the array
285 */
286 mdi = sysfs_read(-1, mdstat->devnum, GET_DEVS);
287 if (!mdi) {
288 /* invalidate the current count so we can try again */
289 container->devcnt = -1;
290 return;
291 }
292
293 /* check for removals */
294 for (cdp = &container->devs; *cdp; ) {
295 found = 0;
296 for (di = mdi->devs; di; di = di->next)
297 if (di->disk.major == (*cdp)->disk.major &&
298 di->disk.minor == (*cdp)->disk.minor) {
299 found = 1;
300 break;
301 }
302 if (!found) {
303 cd = *cdp;
304 *cdp = (*cdp)->next;
305 free(cd);
306 } else
307 cdp = &(*cdp)->next;
308 }
309
310 /* check for additions */
311 for (di = mdi->devs; di; di = di->next) {
312 for (cd = container->devs; cd; cd = cd->next)
313 if (di->disk.major == cd->disk.major &&
314 di->disk.minor == cd->disk.minor)
315 break;
316 if (!cd)
317 add_disk_to_container(container, di);
318 }
319 sysfs_free(mdi);
320 container->devcnt = mdstat->devcnt;
321 }
322 }
323
324 static void manage_member(struct mdstat_ent *mdstat,
325 struct active_array *a)
326 {
327 /* Compare mdstat info with known state of member array.
328 * We do not need to look for device state changes here, that
329 * is dealt with by the monitor.
330 *
331 * We just look for changes which suggest that a reshape is
332 * being requested.
333 * Unfortunately decreases in raid_disks don't show up in
334 * mdstat until the reshape completes FIXME.
335 *
336 * Actually, we also want to handle degraded arrays here by
337 * trying to find and assign a spare.
338 * We do that whenever the monitor tells us too.
339 */
340 // FIXME
341 a->info.array.raid_disks = mdstat->raid_disks;
342 a->info.array.chunk_size = mdstat->chunk_size;
343 // MORE
344
345 if (a->check_degraded) {
346 struct metadata_update *updates = NULL;
347 struct mdinfo *newdev;
348 struct active_array *newa;
349
350 a->check_degraded = 0;
351
352 /* The array may not be degraded, this is just a good time
353 * to check.
354 */
355 newdev = a->container->ss->activate_spare(a, &updates);
356 if (newdev) {
357 struct mdinfo *d;
358 /* Cool, we can add a device or several. */
359 newa = duplicate_aa(a);
360 /* suspend recovery - maybe not needed */
361
362 /* Add device to array and set offset/size/slot.
363 * and open files for each newdev */
364 for (d = newdev; d ; d = d->next) {
365 struct mdinfo *newd;
366 if (sysfs_add_disk(&newa->info, d) < 0)
367 continue;
368 newd = malloc(sizeof(*newd));
369 *newd = *d;
370 newd->next = newa->info.devs;
371 newa->info.devs = newd;
372
373 newd->state_fd = sysfs_open(a->devnum,
374 newd->sys_name,
375 "state");
376 newd->prev_state
377 = read_dev_state(newd->state_fd);
378 newd->curr_state = newd->prev_state;
379 }
380 queue_metadata_update(updates);
381 replace_array(a->container, a, newa);
382 sysfs_set_str(&a->info, NULL, "sync_action", "recover");
383 }
384 }
385 }
386
387 static int aa_ready(struct active_array *aa)
388 {
389 struct mdinfo *d;
390 int level = aa->info.array.level;
391
392 for (d = aa->info.devs; d; d = d->next)
393 if (d->state_fd < 0)
394 return 0;
395
396 if (aa->info.state_fd < 0)
397 return 0;
398
399 if (level > 0 && (aa->action_fd < 0 || aa->resync_start_fd < 0))
400 return 0;
401
402 if (!aa->container)
403 return 0;
404
405 return 1;
406 }
407
408 static void manage_new(struct mdstat_ent *mdstat,
409 struct supertype *container,
410 struct active_array *victim)
411 {
412 /* A new array has appeared in this container.
413 * Hopefully it is already recorded in the metadata.
414 * Check, then create the new array to report it to
415 * the monitor.
416 */
417
418 struct active_array *new;
419 struct mdinfo *mdi, *di;
420 char *inst;
421 int i;
422 int failed = 0;
423
424 /* check if array is ready to be monitored */
425 if (!mdstat->active)
426 return;
427
428 mdi = sysfs_read(-1, mdstat->devnum,
429 GET_LEVEL|GET_CHUNK|GET_DISKS|GET_COMPONENT|
430 GET_DEGRADED|GET_DEVS|GET_OFFSET|GET_SIZE|GET_STATE);
431
432 new = malloc(sizeof(*new));
433
434 if (!new || !mdi) {
435 if (mdi)
436 sysfs_free(mdi);
437 if (new)
438 free(new);
439 return;
440 }
441 memset(new, 0, sizeof(*new));
442
443 new->devnum = mdstat->devnum;
444 strcpy(new->info.sys_name, devnum2devname(new->devnum));
445
446 new->prev_state = new->curr_state = new->next_state = inactive;
447 new->prev_action= new->curr_action= new->next_action= idle;
448
449 new->container = container;
450
451 inst = &mdstat->metadata_version[10+strlen(container->devname)+1];
452
453 new->info.array = mdi->array;
454 new->info.component_size = mdi->component_size;
455
456 for (i = 0; i < new->info.array.raid_disks; i++) {
457 struct mdinfo *newd = malloc(sizeof(*newd));
458
459 for (di = mdi->devs; di; di = di->next)
460 if (i == di->disk.raid_disk)
461 break;
462
463 if (di) {
464 memcpy(newd, di, sizeof(*newd));
465
466 newd->state_fd = sysfs_open(new->devnum,
467 newd->sys_name,
468 "state");
469
470 newd->prev_state = read_dev_state(newd->state_fd);
471 newd->curr_state = newd->prev_state;
472 } else if (failed + 1 > new->info.array.failed_disks) {
473 /* we cannot properly monitor without all working disks */
474 new->container = NULL;
475 break;
476 } else {
477 failed++;
478 free(newd);
479 continue;
480 }
481 sprintf(newd->sys_name, "rd%d", i);
482 newd->next = new->info.devs;
483 new->info.devs = newd;
484 }
485
486 new->action_fd = sysfs_open(new->devnum, NULL, "sync_action");
487 new->info.state_fd = sysfs_open(new->devnum, NULL, "array_state");
488 new->resync_start_fd = sysfs_open(new->devnum, NULL, "resync_start");
489 new->metadata_fd = sysfs_open(new->devnum, NULL, "metadata_version");
490 get_resync_start(new);
491 dprintf("%s: inst: %d action: %d state: %d\n", __func__, atoi(inst),
492 new->action_fd, new->info.state_fd);
493
494 sysfs_free(mdi);
495
496 /* if everything checks out tell the metadata handler we want to
497 * manage this instance
498 */
499 if (!aa_ready(new) || container->ss->open_new(container, new, inst) < 0) {
500 fprintf(stderr, "mdmon: failed to monitor %s\n",
501 mdstat->metadata_version);
502 new->container = NULL;
503 free_aa(new);
504 } else {
505 replace_array(container, victim, new);
506 if (failed) {
507 new->check_degraded = 1;
508 manage_member(mdstat, new);
509 }
510 }
511 }
512
513 void manage(struct mdstat_ent *mdstat, struct supertype *container)
514 {
515 /* We have just read mdstat and need to compare it with
516 * the known active arrays.
517 * Arrays with the wrong metadata are ignored.
518 */
519
520 for ( ; mdstat ; mdstat = mdstat->next) {
521 struct active_array *a;
522 if (mdstat->devnum == container->devnum) {
523 manage_container(mdstat, container);
524 continue;
525 }
526 if (!is_container_member(mdstat, container->devname))
527 /* Not for this array */
528 continue;
529 /* Looks like a member of this container */
530 for (a = container->arrays; a; a = a->next) {
531 if (mdstat->devnum == a->devnum) {
532 if (a->container)
533 manage_member(mdstat, a);
534 break;
535 }
536 }
537 if (a == NULL || !a->container)
538 manage_new(mdstat, container, a);
539 }
540 }
541
542 static void handle_message(struct supertype *container, struct metadata_update *msg)
543 {
544 /* queue this metadata update through to the monitor */
545
546 struct metadata_update *mu;
547
548 if (msg->len <= 0)
549 while (update_queue_pending || update_queue) {
550 check_update_queue(container);
551 usleep(15*1000);
552 }
553
554 if (msg->len == 0) { /* ping_monitor */
555 int cnt;
556
557 cnt = monitor_loop_cnt;
558 if (cnt & 1)
559 cnt += 2; /* wait until next pselect */
560 else
561 cnt += 3; /* wait for 2 pselects */
562 wakeup_monitor();
563
564 while (monitor_loop_cnt - cnt < 0)
565 usleep(10 * 1000);
566 } else if (msg->len == -1) { /* ping_manager */
567 struct mdstat_ent *mdstat = mdstat_read(1, 0);
568
569 manage(mdstat, container);
570 free_mdstat(mdstat);
571 } else if (!sigterm) {
572 mu = malloc(sizeof(*mu));
573 mu->len = msg->len;
574 mu->buf = msg->buf;
575 msg->buf = NULL;
576 mu->space = NULL;
577 mu->next = NULL;
578 if (container->ss->prepare_update)
579 container->ss->prepare_update(container, mu);
580 queue_metadata_update(mu);
581 }
582 }
583
584 void read_sock(struct supertype *container)
585 {
586 int fd;
587 struct metadata_update msg;
588 int terminate = 0;
589 long fl;
590 int tmo = 3; /* 3 second timeout before hanging up the socket */
591
592 fd = accept(container->sock, NULL, NULL);
593 if (fd < 0)
594 return;
595
596 fl = fcntl(fd, F_GETFL, 0);
597 fl |= O_NONBLOCK;
598 fcntl(fd, F_SETFL, fl);
599
600 do {
601 msg.buf = NULL;
602
603 /* read and validate the message */
604 if (receive_message(fd, &msg, tmo) == 0) {
605 handle_message(container, &msg);
606 if (ack(fd, tmo) < 0)
607 terminate = 1;
608 } else
609 terminate = 1;
610
611 } while (!terminate);
612
613 close(fd);
614 }
615
616 int exit_now = 0;
617 int manager_ready = 0;
618 void do_manager(struct supertype *container)
619 {
620 struct mdstat_ent *mdstat;
621 sigset_t set;
622 int proc_fd;
623
624 sigprocmask(SIG_UNBLOCK, NULL, &set);
625 sigdelset(&set, SIGUSR1);
626 sigdelset(&set, SIGHUP);
627 sigdelset(&set, SIGALRM);
628 sigdelset(&set, SIGTERM);
629 proc_fd = open("/proc/mounts", O_RDONLY);
630
631 do {
632
633 if (exit_now)
634 exit(0);
635
636 /* Can only 'manage' things if 'monitor' is not making
637 * structural changes to metadata, so need to check
638 * update_queue
639 */
640 if (update_queue == NULL) {
641 mdstat = mdstat_read(1, 0);
642
643 manage(mdstat, container);
644
645 read_sock(container);
646
647 if (container->sock < 0 || socket_hup_requested) {
648 close(container->sock);
649 container->sock = make_control_sock(container->devname);
650 make_pidfile(container->devname, 0);
651 socket_hup_requested = 0;
652 }
653 if (container->sock < 0)
654 alarm(30);
655
656 free_mdstat(mdstat);
657 }
658 remove_old();
659
660 check_update_queue(container);
661
662 manager_ready = 1;
663
664 if (sigterm)
665 wakeup_monitor();
666
667 if (update_queue == NULL) {
668 if (container->sock < 0)
669 mdstat_wait_fd(proc_fd, &set);
670 else
671 mdstat_wait_fd(container->sock, &set);
672 } else
673 /* If an update is happening, just wait for signal */
674 pselect(0, NULL, NULL, NULL, NULL, &set);
675 } while(1);
676 }