]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * mdmon - monitor external metadata arrays | |
3 | * | |
4 | * Copyright (C) 2007-2009 Neil Brown <neilb@suse.de> | |
5 | * Copyright (C) 2007-2009 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 | * md array manager. | |
23 | * When md arrays have user-space managed metadata, this is the program | |
24 | * that does the managing. | |
25 | * | |
26 | * Given one argument: the name of the array (e.g. /dev/md0) that is | |
27 | * the container. | |
28 | * We fork off a helper that runs high priority and mlocked. It responds to | |
29 | * device failures and other events that might stop writeout, or that are | |
30 | * trivial to deal with. | |
31 | * The main thread then watches for new arrays being created in the container | |
32 | * and starts monitoring them too ... along with a few other tasks. | |
33 | * | |
34 | * The main thread communicates with the priority thread by writing over | |
35 | * a pipe. | |
36 | * Separate programs can communicate with the main thread via Unix-domain | |
37 | * socket. | |
38 | * The two threads share address space and open file table. | |
39 | * | |
40 | */ | |
41 | ||
42 | #ifndef _GNU_SOURCE | |
43 | #define _GNU_SOURCE | |
44 | #endif | |
45 | ||
46 | #include <unistd.h> | |
47 | #include <stdlib.h> | |
48 | #include <sys/types.h> | |
49 | #include <sys/stat.h> | |
50 | #include <sys/socket.h> | |
51 | #include <sys/un.h> | |
52 | #include <sys/mman.h> | |
53 | #include <sys/syscall.h> | |
54 | #include <sys/wait.h> | |
55 | #include <stdio.h> | |
56 | #include <errno.h> | |
57 | #include <string.h> | |
58 | #include <fcntl.h> | |
59 | #include <signal.h> | |
60 | #include <dirent.h> | |
61 | #ifdef USE_PTHREADS | |
62 | #include <pthread.h> | |
63 | #else | |
64 | #include <sched.h> | |
65 | #endif | |
66 | ||
67 | #include "mdadm.h" | |
68 | #include "mdmon.h" | |
69 | ||
70 | struct active_array *discard_this; | |
71 | struct active_array *pending_discard; | |
72 | ||
73 | int mon_tid, mgr_tid; | |
74 | ||
75 | int sigterm; | |
76 | ||
77 | #ifdef USE_PTHREADS | |
78 | static void *run_child(void *v) | |
79 | { | |
80 | struct supertype *c = v; | |
81 | ||
82 | mon_tid = syscall(SYS_gettid); | |
83 | do_monitor(c); | |
84 | return 0; | |
85 | } | |
86 | ||
87 | static int clone_monitor(struct supertype *container) | |
88 | { | |
89 | pthread_attr_t attr; | |
90 | pthread_t thread; | |
91 | int rc; | |
92 | ||
93 | mon_tid = -1; | |
94 | pthread_attr_init(&attr); | |
95 | pthread_attr_setstacksize(&attr, 4096); | |
96 | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); | |
97 | rc = pthread_create(&thread, &attr, run_child, container); | |
98 | if (rc) | |
99 | return rc; | |
100 | while (mon_tid == -1) | |
101 | usleep(10); | |
102 | pthread_attr_destroy(&attr); | |
103 | ||
104 | mgr_tid = syscall(SYS_gettid); | |
105 | ||
106 | return mon_tid; | |
107 | } | |
108 | #else /* USE_PTHREADS */ | |
109 | static int run_child(void *v) | |
110 | { | |
111 | struct supertype *c = v; | |
112 | ||
113 | do_monitor(c); | |
114 | return 0; | |
115 | } | |
116 | ||
117 | #ifdef __ia64__ | |
118 | int __clone2(int (*fn)(void *), | |
119 | void *child_stack_base, size_t stack_size, | |
120 | int flags, void *arg, ... | |
121 | /* pid_t *pid, struct user_desc *tls, pid_t *ctid */ ); | |
122 | #endif | |
123 | static int clone_monitor(struct supertype *container) | |
124 | { | |
125 | static char stack[4096]; | |
126 | ||
127 | #ifdef __ia64__ | |
128 | mon_tid = __clone2(run_child, stack, sizeof(stack), | |
129 | CLONE_FS|CLONE_FILES|CLONE_VM|CLONE_SIGHAND|CLONE_THREAD, | |
130 | container); | |
131 | #else | |
132 | mon_tid = clone(run_child, stack+4096-64, | |
133 | CLONE_FS|CLONE_FILES|CLONE_VM|CLONE_SIGHAND|CLONE_THREAD, | |
134 | container); | |
135 | #endif | |
136 | ||
137 | mgr_tid = syscall(SYS_gettid); | |
138 | ||
139 | return mon_tid; | |
140 | } | |
141 | #endif /* USE_PTHREADS */ | |
142 | ||
143 | static int make_pidfile(char *devname) | |
144 | { | |
145 | char path[100]; | |
146 | char pid[10]; | |
147 | int fd; | |
148 | int n; | |
149 | ||
150 | if (mkdir(MDMON_DIR, 0755) < 0 && | |
151 | errno != EEXIST) | |
152 | return -errno; | |
153 | sprintf(path, "%s/%s.pid", MDMON_DIR, devname); | |
154 | ||
155 | fd = open(path, O_RDWR|O_CREAT|O_EXCL, 0600); | |
156 | if (fd < 0) | |
157 | return -errno; | |
158 | sprintf(pid, "%d\n", getpid()); | |
159 | n = write(fd, pid, strlen(pid)); | |
160 | close(fd); | |
161 | if (n < 0) | |
162 | return -errno; | |
163 | return 0; | |
164 | } | |
165 | ||
166 | static void try_kill_monitor(pid_t pid, char *devname, int sock) | |
167 | { | |
168 | char buf[100]; | |
169 | int fd; | |
170 | int n; | |
171 | long fl; | |
172 | ||
173 | /* first rule of survival... don't off yourself */ | |
174 | if (pid == getpid()) | |
175 | return; | |
176 | ||
177 | /* kill this process if it is mdmon */ | |
178 | sprintf(buf, "/proc/%lu/cmdline", (unsigned long) pid); | |
179 | fd = open(buf, O_RDONLY); | |
180 | if (fd < 0) | |
181 | return; | |
182 | ||
183 | n = read(fd, buf, sizeof(buf)-1); | |
184 | buf[sizeof(buf)-1] = 0; | |
185 | close(fd); | |
186 | ||
187 | if (n < 0 || !strstr(buf, "mdmon")) | |
188 | return; | |
189 | ||
190 | kill(pid, SIGTERM); | |
191 | ||
192 | /* Wait for monitor to exit by reading from the socket, after | |
193 | * clearing the non-blocking flag */ | |
194 | fl = fcntl(sock, F_GETFL, 0); | |
195 | fl &= ~O_NONBLOCK; | |
196 | fcntl(sock, F_SETFL, fl); | |
197 | n = read(sock, buf, 100); | |
198 | /* Ignore result, it is just the wait that | |
199 | * matters | |
200 | */ | |
201 | } | |
202 | ||
203 | void remove_pidfile(char *devname) | |
204 | { | |
205 | char buf[100]; | |
206 | ||
207 | sprintf(buf, "%s/%s.pid", MDMON_DIR, devname); | |
208 | unlink(buf); | |
209 | sprintf(buf, "%s/%s.sock", MDMON_DIR, devname); | |
210 | unlink(buf); | |
211 | } | |
212 | ||
213 | static int make_control_sock(char *devname) | |
214 | { | |
215 | char path[100]; | |
216 | int sfd; | |
217 | long fl; | |
218 | struct sockaddr_un addr; | |
219 | ||
220 | if (sigterm) | |
221 | return -1; | |
222 | ||
223 | sprintf(path, "%s/%s.sock", MDMON_DIR, devname); | |
224 | unlink(path); | |
225 | sfd = socket(PF_LOCAL, SOCK_STREAM, 0); | |
226 | if (sfd < 0) | |
227 | return -1; | |
228 | ||
229 | addr.sun_family = PF_LOCAL; | |
230 | strcpy(addr.sun_path, path); | |
231 | if (bind(sfd, &addr, sizeof(addr)) < 0) { | |
232 | close(sfd); | |
233 | return -1; | |
234 | } | |
235 | listen(sfd, 10); | |
236 | fl = fcntl(sfd, F_GETFL, 0); | |
237 | fl |= O_NONBLOCK; | |
238 | fcntl(sfd, F_SETFL, fl); | |
239 | return sfd; | |
240 | } | |
241 | ||
242 | static void term(int sig) | |
243 | { | |
244 | sigterm = 1; | |
245 | } | |
246 | ||
247 | static void wake_me(int sig) | |
248 | { | |
249 | ||
250 | } | |
251 | ||
252 | /* if we are debugging and starting mdmon by hand then don't fork */ | |
253 | static int do_fork(void) | |
254 | { | |
255 | #ifdef DEBUG | |
256 | if (check_env("MDADM_NO_MDMON")) | |
257 | return 0; | |
258 | #endif | |
259 | ||
260 | return 1; | |
261 | } | |
262 | ||
263 | void usage(void) | |
264 | { | |
265 | fprintf(stderr, "Usage: mdmon [--all] [--takeover] CONTAINER\n"); | |
266 | exit(2); | |
267 | } | |
268 | ||
269 | static int mdmon(char *devname, int devnum, int must_fork, int takeover); | |
270 | ||
271 | int main(int argc, char *argv[]) | |
272 | { | |
273 | char *container_name = NULL; | |
274 | int devnum; | |
275 | char *devname; | |
276 | int status = 0; | |
277 | int arg; | |
278 | int all = 0; | |
279 | int takeover = 0; | |
280 | ||
281 | for (arg = 1; arg < argc; arg++) { | |
282 | if (strncmp(argv[arg], "--all",5) == 0 || | |
283 | strcmp(argv[arg], "/proc/mdstat") == 0) { | |
284 | container_name = argv[arg]; | |
285 | all = 1; | |
286 | } else if (strcmp(argv[arg], "--takeover") == 0) | |
287 | takeover = 1; | |
288 | else if (container_name == NULL) | |
289 | container_name = argv[arg]; | |
290 | else | |
291 | usage(); | |
292 | } | |
293 | if (container_name == NULL) | |
294 | usage(); | |
295 | ||
296 | if (all) { | |
297 | struct mdstat_ent *mdstat, *e; | |
298 | int container_len = strlen(container_name); | |
299 | ||
300 | /* launch an mdmon instance for each container found */ | |
301 | mdstat = mdstat_read(0, 0); | |
302 | for (e = mdstat; e; e = e->next) { | |
303 | if (strncmp(e->metadata_version, "external:", 9) == 0 && | |
304 | !is_subarray(&e->metadata_version[9])) { | |
305 | devname = devnum2devname(e->devnum); | |
306 | /* update cmdline so this mdmon instance can be | |
307 | * distinguished from others in a call to ps(1) | |
308 | */ | |
309 | if (strlen(devname) <= (unsigned)container_len) { | |
310 | memset(container_name, 0, container_len); | |
311 | sprintf(container_name, "%s", devname); | |
312 | } | |
313 | status |= mdmon(devname, e->devnum, 1, | |
314 | takeover); | |
315 | } | |
316 | } | |
317 | free_mdstat(mdstat); | |
318 | ||
319 | return status; | |
320 | } else if (strncmp(container_name, "md", 2) == 0) { | |
321 | devnum = devname2devnum(container_name); | |
322 | devname = devnum2devname(devnum); | |
323 | if (strcmp(container_name, devname) != 0) | |
324 | devname = NULL; | |
325 | } else { | |
326 | struct stat st; | |
327 | ||
328 | devnum = NoMdDev; | |
329 | if (stat(container_name, &st) == 0) | |
330 | devnum = stat2devnum(&st); | |
331 | if (devnum == NoMdDev) | |
332 | devname = NULL; | |
333 | else | |
334 | devname = devnum2devname(devnum); | |
335 | } | |
336 | ||
337 | if (!devname) { | |
338 | fprintf(stderr, "mdmon: %s is not a valid md device name\n", | |
339 | container_name); | |
340 | exit(1); | |
341 | } | |
342 | return mdmon(devname, devnum, do_fork(), takeover); | |
343 | } | |
344 | ||
345 | static int mdmon(char *devname, int devnum, int must_fork, int takeover) | |
346 | { | |
347 | int mdfd; | |
348 | struct mdinfo *mdi, *di; | |
349 | struct supertype *container; | |
350 | sigset_t set; | |
351 | struct sigaction act; | |
352 | int pfd[2]; | |
353 | int status; | |
354 | int ignore; | |
355 | pid_t victim = -1; | |
356 | int victim_sock = -1; | |
357 | ||
358 | dprintf("starting mdmon for %s\n", devname); | |
359 | ||
360 | mdfd = open_dev(devnum); | |
361 | if (mdfd < 0) { | |
362 | fprintf(stderr, "mdmon: %s: %s\n", devname, | |
363 | strerror(errno)); | |
364 | return 1; | |
365 | } | |
366 | if (md_get_version(mdfd) < 0) { | |
367 | fprintf(stderr, "mdmon: %s: Not an md device\n", | |
368 | devname); | |
369 | return 1; | |
370 | } | |
371 | ||
372 | /* Fork, and have the child tell us when they are ready */ | |
373 | if (must_fork) { | |
374 | if (pipe(pfd) != 0) { | |
375 | fprintf(stderr, "mdmon: failed to create pipe\n"); | |
376 | return 1; | |
377 | } | |
378 | switch(fork()) { | |
379 | case -1: | |
380 | fprintf(stderr, "mdmon: failed to fork: %s\n", | |
381 | strerror(errno)); | |
382 | return 1; | |
383 | case 0: /* child */ | |
384 | close(pfd[0]); | |
385 | break; | |
386 | default: /* parent */ | |
387 | close(pfd[1]); | |
388 | if (read(pfd[0], &status, sizeof(status)) != sizeof(status)) { | |
389 | wait(&status); | |
390 | status = WEXITSTATUS(status); | |
391 | } | |
392 | return status; | |
393 | } | |
394 | } else | |
395 | pfd[0] = pfd[1] = -1; | |
396 | ||
397 | container = calloc(1, sizeof(*container)); | |
398 | container->devnum = devnum; | |
399 | container->devname = devname; | |
400 | container->arrays = NULL; | |
401 | container->sock = -1; | |
402 | ||
403 | if (!container->devname) { | |
404 | fprintf(stderr, "mdmon: failed to allocate container name string\n"); | |
405 | exit(3); | |
406 | } | |
407 | ||
408 | mdi = sysfs_read(mdfd, container->devnum, GET_VERSION|GET_LEVEL|GET_DEVS); | |
409 | ||
410 | if (!mdi) { | |
411 | fprintf(stderr, "mdmon: failed to load sysfs info for %s\n", | |
412 | container->devname); | |
413 | exit(3); | |
414 | } | |
415 | if (mdi->array.level != UnSet) { | |
416 | fprintf(stderr, "mdmon: %s is not a container - cannot monitor\n", | |
417 | devname); | |
418 | exit(3); | |
419 | } | |
420 | if (mdi->array.major_version != -1 || | |
421 | mdi->array.minor_version != -2) { | |
422 | fprintf(stderr, "mdmon: %s does not use external metadata - cannot monitor\n", | |
423 | devname); | |
424 | exit(3); | |
425 | } | |
426 | ||
427 | container->ss = version_to_superswitch(mdi->text_version); | |
428 | if (container->ss == NULL) { | |
429 | fprintf(stderr, "mdmon: %s uses unsupported metadata: %s\n", | |
430 | devname, mdi->text_version); | |
431 | exit(3); | |
432 | } | |
433 | ||
434 | container->devs = NULL; | |
435 | for (di = mdi->devs; di; di = di->next) { | |
436 | struct mdinfo *cd = malloc(sizeof(*cd)); | |
437 | *cd = *di; | |
438 | cd->next = container->devs; | |
439 | container->devs = cd; | |
440 | } | |
441 | sysfs_free(mdi); | |
442 | ||
443 | /* SIGUSR is sent between parent and child. So both block it | |
444 | * and enable it only with pselect. | |
445 | */ | |
446 | sigemptyset(&set); | |
447 | sigaddset(&set, SIGUSR1); | |
448 | sigaddset(&set, SIGTERM); | |
449 | sigprocmask(SIG_BLOCK, &set, NULL); | |
450 | act.sa_handler = wake_me; | |
451 | act.sa_flags = 0; | |
452 | sigaction(SIGUSR1, &act, NULL); | |
453 | act.sa_handler = term; | |
454 | sigaction(SIGTERM, &act, NULL); | |
455 | act.sa_handler = SIG_IGN; | |
456 | sigaction(SIGPIPE, &act, NULL); | |
457 | ||
458 | victim = mdmon_pid(container->devnum); | |
459 | if (victim >= 0) | |
460 | victim_sock = connect_monitor(container->devname); | |
461 | ||
462 | ignore = chdir("/"); | |
463 | if (!takeover && victim > 0 && victim_sock >= 0) { | |
464 | if (fping_monitor(victim_sock) == 0) { | |
465 | fprintf(stderr, "mdmon: %s already managed\n", | |
466 | container->devname); | |
467 | exit(3); | |
468 | } | |
469 | close(victim_sock); | |
470 | } | |
471 | if (container->ss->load_container(container, mdfd, devname)) { | |
472 | fprintf(stderr, "mdmon: Cannot load metadata for %s\n", | |
473 | devname); | |
474 | exit(3); | |
475 | } | |
476 | close(mdfd); | |
477 | ||
478 | /* Ok, this is close enough. We can say goodbye to our parent now. | |
479 | */ | |
480 | if (victim > 0) | |
481 | remove_pidfile(devname); | |
482 | if (make_pidfile(devname) < 0) { | |
483 | exit(3); | |
484 | } | |
485 | container->sock = make_control_sock(devname); | |
486 | ||
487 | status = 0; | |
488 | if (write(pfd[1], &status, sizeof(status)) < 0) | |
489 | fprintf(stderr, "mdmon: failed to notify our parent: %d\n", | |
490 | getppid()); | |
491 | close(pfd[1]); | |
492 | ||
493 | mlockall(MCL_CURRENT | MCL_FUTURE); | |
494 | ||
495 | if (clone_monitor(container) < 0) { | |
496 | fprintf(stderr, "mdmon: failed to start monitor process: %s\n", | |
497 | strerror(errno)); | |
498 | exit(2); | |
499 | } | |
500 | ||
501 | if (victim > 0) { | |
502 | try_kill_monitor(victim, container->devname, victim_sock); | |
503 | close(victim_sock); | |
504 | } | |
505 | ||
506 | setsid(); | |
507 | close(0); | |
508 | open("/dev/null", O_RDWR); | |
509 | close(1); | |
510 | ignore = dup(0); | |
511 | #ifndef DEBUG | |
512 | close(2); | |
513 | ignore = dup(0); | |
514 | #endif | |
515 | ||
516 | do_manager(container); | |
517 | ||
518 | exit(0); | |
519 | } | |
520 | ||
521 | /* Some stub functions so super-* can link with us */ | |
522 | int child_monitor(int afd, struct mdinfo *sra, struct reshape *reshape, | |
523 | struct supertype *st, unsigned long blocks, | |
524 | int *fds, unsigned long long *offsets, | |
525 | int dests, int *destfd, unsigned long long *destoffsets) | |
526 | { | |
527 | return 0; | |
528 | } |