]>
git.ipfire.org Git - thirdparty/mdadm.git/blob - mdmon.c
2 * mdmon - monitor external metadata arrays
4 * Copyright (C) 2007-2008 Neil Brown <neilb@suse.de>
5 * Copyright (C) 2007-2008 Intel Corporation
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.
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
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.
23 * When md arrays have user-space managed metadata, this is the program
24 * that does the managing.
26 * Given one argument: the name of the array (e.g. /dev/md0) that is
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.
34 * The main thread communicates with the priority thread by writing over
36 * Separate programs can communicate with the main thread via Unix-domain
38 * The two threads share address space and open file table.
48 #include <sys/types.h>
50 #include <sys/socket.h>
53 #include <sys/syscall.h>
67 struct active_array
*discard_this
;
68 struct active_array
*pending_discard
;
74 int run_child(void *v
)
76 struct supertype
*c
= v
;
83 int __clone2(int (*fn
)(void *),
84 void *child_stack_base
, size_t stack_size
,
85 int flags
, void *arg
, ...
86 /* pid_t *pid, struct user_desc *tls, pid_t *ctid */ );
88 int clone_monitor(struct supertype
*container
)
90 static char stack
[4096];
93 mon_tid
= __clone2(run_child
, stack
, sizeof(stack
),
94 CLONE_FS
|CLONE_FILES
|CLONE_VM
|CLONE_SIGHAND
|CLONE_THREAD
,
97 mon_tid
= clone(run_child
, stack
+4096-64,
98 CLONE_FS
|CLONE_FILES
|CLONE_VM
|CLONE_SIGHAND
|CLONE_THREAD
,
102 mgr_tid
= syscall(SYS_gettid
);
107 static struct superswitch
*find_metadata_methods(char *vers
)
109 if (strcmp(vers
, "ddf") == 0)
111 if (strcmp(vers
, "imsm") == 0)
117 int make_pidfile(char *devname
, int o_excl
)
127 sprintf(path
, "/var/run/mdadm/%s.pid", devname
);
129 fd
= open(path
, O_RDWR
|O_CREAT
|o_excl
, 0600);
132 sprintf(pid
, "%d\n", getpid());
133 n
= write(fd
, pid
, strlen(pid
));
140 int is_container_member(struct mdstat_ent
*mdstat
, char *container
)
142 if (mdstat
->metadata_version
== NULL
||
143 strncmp(mdstat
->metadata_version
, "external:", 9) != 0 ||
144 !is_subarray(mdstat
->metadata_version
+9) ||
145 strncmp(mdstat
->metadata_version
+10, container
, strlen(container
)) != 0 ||
146 mdstat
->metadata_version
[10+strlen(container
)] != '/')
152 void remove_pidfile(char *devname
);
153 static void try_kill_monitor(char *devname
)
158 struct mdstat_ent
*mdstat
;
160 sprintf(buf
, "/var/run/mdadm/%s.pid", devname
);
161 fd
= open(buf
, O_RDONLY
);
165 if (read(fd
, buf
, sizeof(buf
)) < 0) {
171 pid
= strtoul(buf
, NULL
, 10);
173 /* first rule of survival... don't off yourself */
177 /* kill this process if it is mdmon */
178 sprintf(buf
, "/proc/%lu/cmdline", (unsigned long) pid
);
179 fd
= open(buf
, O_RDONLY
);
183 if (read(fd
, buf
, sizeof(buf
)) < 0) {
188 if (!strstr(buf
, "mdmon"))
193 mdstat
= mdstat_read(0, 0);
194 for ( ; mdstat
; mdstat
= mdstat
->next
)
195 if (is_container_member(mdstat
, devname
)) {
196 sprintf(buf
, "/dev/%s", mdstat
->dev
);
200 remove_pidfile(devname
);
203 void remove_pidfile(char *devname
)
210 sprintf(buf
, "/var/run/mdadm/%s.pid", devname
);
212 sprintf(buf
, "/var/run/mdadm/%s.sock", devname
);
216 int make_control_sock(char *devname
)
221 struct sockaddr_un addr
;
226 sprintf(path
, "/var/run/mdadm/%s.sock", devname
);
228 sfd
= socket(PF_LOCAL
, SOCK_STREAM
, 0);
232 addr
.sun_family
= PF_LOCAL
;
233 strcpy(addr
.sun_path
, path
);
234 if (bind(sfd
, &addr
, sizeof(addr
)) < 0) {
239 fl
= fcntl(sfd
, F_GETFL
, 0);
241 fcntl(sfd
, F_SETFL
, fl
);
245 int socket_hup_requested
;
246 static void hup(int sig
)
248 socket_hup_requested
= 1;
251 static void term(int sig
)
256 static void wake_me(int sig
)
261 /* if we are debugging and starting mdmon by hand then don't fork */
262 static int do_fork(void)
265 if (check_env("MDADM_NO_MDMON"))
274 fprintf(stderr
, "Usage: mdmon [--switch-root dir] /device/name/for/container\n");
278 int mdmon(char *devname
, int devnum
, int scan
, char *switchroot
);
280 int main(int argc
, char *argv
[])
282 char *container_name
= NULL
;
283 char *switchroot
= NULL
;
291 container_name
= argv
[1];
294 if (strcmp(argv
[1], "--switch-root") != 0) {
295 fprintf(stderr
, "mdmon: unknown argument %s\n", argv
[1]);
298 switchroot
= argv
[2];
299 container_name
= argv
[3];
305 if (strcmp(container_name
, "/proc/mdstat") == 0) {
306 struct mdstat_ent
*mdstat
, *e
;
308 /* launch an mdmon instance for each container found */
310 mdstat
= mdstat_read(0, 0);
311 for (e
= mdstat
; e
; e
= e
->next
) {
312 if (strncmp(e
->metadata_version
, "external:", 9) == 0 &&
313 !is_subarray(&e
->metadata_version
[9])) {
314 devname
= devnum2devname(e
->devnum
);
315 status
|= mdmon(devname
, e
->devnum
, scan
,
322 } else if (strncmp(container_name
, "md", 2) == 0) {
323 devnum
= devname2devnum(container_name
);
324 devname
= devnum2devname(devnum
);
325 if (strcmp(container_name
, devname
) != 0)
331 if (stat(container_name
, &st
) == 0)
332 devnum
= stat2devnum(&st
);
333 if (devnum
== NoMdDev
)
336 devname
= devnum2devname(devnum
);
340 fprintf(stderr
, "mdmon: %s is not a valid md device name\n",
344 return mdmon(devname
, devnum
, scan
, switchroot
);
347 int mdmon(char *devname
, int devnum
, int scan
, char *switchroot
)
350 struct mdinfo
*mdi
, *di
;
351 struct supertype
*container
;
353 struct sigaction act
;
358 mdfd
= open_dev(devnum
);
360 fprintf(stderr
, "mdmon: %s: %s\n", devname
,
364 if (md_get_version(mdfd
) < 0) {
365 fprintf(stderr
, "mdmon: %s: Not an md device\n",
370 /* Fork, and have the child tell us when they are ready */
371 if (do_fork() || scan
) {
372 if (pipe(pfd
) != 0) {
373 fprintf(stderr
, "mdmon: failed to create pipe\n");
378 fprintf(stderr
, "mdmon: failed to fork: %s\n",
384 default: /* parent */
386 if (read(pfd
[0], &status
, sizeof(status
)) != sizeof(status
)) {
388 status
= WEXITSTATUS(status
);
393 pfd
[0] = pfd
[1] = -1;
395 container
= malloc(sizeof(*container
));
396 container
->devnum
= devnum
;
397 container
->devname
= devname
;
398 container
->arrays
= NULL
;
399 container
->subarray
[0] = 0;
401 if (!container
->devname
) {
402 fprintf(stderr
, "mdmon: failed to allocate container name string\n");
406 mdi
= sysfs_read(mdfd
, container
->devnum
,
407 GET_VERSION
|GET_LEVEL
|GET_DEVS
);
410 fprintf(stderr
, "mdmon: failed to load sysfs info for %s\n",
414 if (mdi
->array
.level
!= UnSet
) {
415 fprintf(stderr
, "mdmon: %s is not a container - cannot monitor\n",
419 if (mdi
->array
.major_version
!= -1 ||
420 mdi
->array
.minor_version
!= -2) {
421 fprintf(stderr
, "mdmon: %s does not use external metadata - cannot monitor\n",
426 container
->ss
= find_metadata_methods(mdi
->text_version
);
427 if (container
->ss
== NULL
) {
428 fprintf(stderr
, "mdmon: %s uses unknown metadata: %s\n",
429 devname
, mdi
->text_version
);
433 container
->devs
= NULL
;
434 for (di
= mdi
->devs
; di
; di
= di
->next
) {
435 struct mdinfo
*cd
= malloc(sizeof(*cd
));
437 cd
->next
= container
->devs
;
438 container
->devs
= cd
;
442 /* SIGUSR is sent between parent and child. So both block it
443 * and enable it only with pselect.
446 sigaddset(&set
, SIGUSR1
);
447 sigaddset(&set
, SIGHUP
);
448 sigaddset(&set
, SIGALRM
);
449 sigaddset(&set
, SIGTERM
);
450 sigprocmask(SIG_BLOCK
, &set
, NULL
);
451 act
.sa_handler
= wake_me
;
453 sigaction(SIGUSR1
, &act
, NULL
);
454 sigaction(SIGALRM
, &act
, NULL
);
455 act
.sa_handler
= hup
;
456 sigaction(SIGHUP
, &act
, NULL
);
457 act
.sa_handler
= term
;
458 sigaction(SIGTERM
, &act
, NULL
);
459 act
.sa_handler
= SIG_IGN
;
460 sigaction(SIGPIPE
, &act
, NULL
);
463 /* we assume we assume that /sys /proc /dev are available in
464 * the new root (see nash:setuproot)
466 * kill any monitors in the current namespace and change
469 try_kill_monitor(container
->devname
);
470 if (chroot(switchroot
) != 0) {
471 fprintf(stderr
, "mdmon: failed to chroot to '%s': %s\n",
472 switchroot
, strerror(errno
));
477 /* If this fails, we hope it already exists
478 * pid file lives in /var/run/mdadm/mdXX.pid
481 mkdir("/var/run", 0600);
482 mkdir("/var/run/mdadm", 0600);
484 if (make_pidfile(container
->devname
, O_EXCL
) < 0) {
485 if (ping_monitor(container
->devname
) == 0) {
486 fprintf(stderr
, "mdmon: %s already managed\n",
492 /* cleanup the old monitor, this one is taking over */
493 try_kill_monitor(container
->devname
);
494 err
= make_pidfile(container
->devname
, 0);
496 fprintf(stderr
, "mdmon: %s Cannot create pidfile\n",
499 /* FIXME implement a mechanism to
500 * prevent duplicate monitor instances
503 "mdmon: continuing on read-only file system\n");
509 container
->sock
= make_control_sock(container
->devname
);
511 if (container
->ss
->load_super(container
, mdfd
, devname
)) {
512 fprintf(stderr
, "mdmon: Cannot load metadata for %s\n",
518 /* Ok, this is close enough. We can say goodbye to our parent now.
521 if (write(pfd
[1], &status
, sizeof(status
)) < 0)
522 fprintf(stderr
, "mdmon: failed to notify our parent: %d\n",
528 open("/dev/null", O_RDWR
);
536 mlockall(MCL_FUTURE
);
538 if (clone_monitor(container
) < 0) {
539 fprintf(stderr
, "mdmon: failed to start monitor process: %s\n",
544 do_manager(container
);