imsm: determine failed indexes from the most up-to-date disk
[thirdparty/mdadm.git] / mdmon.c
CommitLineData
549e9569
NB
1
2/*
3 * md array manager.
4 * When md arrays have user-space managed metadata, this is the program
5 * that does the managing.
6 *
7 * Given one argument: the name of the array (e.g. /dev/md0) that is
8 * the container.
9 * We fork off a helper that runs high priority and mlocked. It responds to
10 * device failures and other events that might stop writeout, or that are
11 * trivial to deal with.
12 * The main thread then watches for new arrays being created in the container
13 * and starts monitoring them too ... along with a few other tasks.
14 *
15 * The main thread communicates with the priority thread by writing over
16 * a pipe.
17 * Separate programs can communicate with the main thread via Unix-domain
18 * socket.
19 * The two threads share address space and open file table.
20 *
21 */
22
23#ifndef _GNU_SOURCE
24#define _GNU_SOURCE
25#endif
26
27#include <unistd.h>
28#include <stdlib.h>
4d43913c 29#include <sys/types.h>
549e9569
NB
30#include <sys/stat.h>
31#include <sys/socket.h>
32#include <sys/un.h>
33#include <sys/mman.h>
4d43913c 34#include <sys/syscall.h>
9fe32043 35#include <sys/wait.h>
549e9569
NB
36#include <stdio.h>
37#include <errno.h>
38#include <string.h>
39#include <fcntl.h>
b109d928 40#include <signal.h>
549e9569
NB
41
42#include <sched.h>
43
44#include "mdadm.h"
45#include "mdmon.h"
46
549e9569
NB
47struct active_array *discard_this;
48struct active_array *pending_discard;
4d43913c
NB
49
50int mon_tid, mgr_tid;
549e9569
NB
51
52int run_child(void *v)
53{
54 struct supertype *c = v;
1ed3f387 55
549e9569
NB
56 do_monitor(c);
57 return 0;
58}
59
60int clone_monitor(struct supertype *container)
61{
549e9569 62 static char stack[4096];
549e9569 63
2cc98f9e 64 mon_tid = clone(run_child, stack+4096-64,
549e9569
NB
65 CLONE_FS|CLONE_FILES|CLONE_VM|CLONE_SIGHAND|CLONE_THREAD,
66 container);
3e70c845 67
4d43913c 68 mgr_tid = syscall(SYS_gettid);
2cc98f9e
DW
69
70 return mon_tid;
549e9569
NB
71}
72
73static struct superswitch *find_metadata_methods(char *vers)
74{
75 if (strcmp(vers, "ddf") == 0)
76 return &super_ddf;
5b65005f
DW
77 if (strcmp(vers, "imsm") == 0)
78 return &super_imsm;
549e9569
NB
79 return NULL;
80}
81
82
295646b3 83int make_pidfile(char *devname, int o_excl)
549e9569
NB
84{
85 char path[100];
86 char pid[10];
87 int fd;
88 sprintf(path, "/var/run/mdadm/%s.pid", devname);
89
b109d928 90 fd = open(path, O_RDWR|O_CREAT|o_excl, 0600);
549e9569 91 if (fd < 0)
295646b3 92 return -errno;
549e9569
NB
93 sprintf(pid, "%d\n", getpid());
94 write(fd, pid, strlen(pid));
95 close(fd);
96 return 0;
97}
98
b109d928
DW
99static void try_kill_monitor(char *devname)
100{
101 char buf[100];
102 int fd;
103 pid_t pid;
104
105 sprintf(buf, "/var/run/mdadm/%s.pid", devname);
106 fd = open(buf, O_RDONLY);
107 if (fd < 0)
108 return;
109
110 if (read(fd, buf, sizeof(buf)) < 0) {
111 close(fd);
112 return;
113 }
114
115 close(fd);
116 pid = strtoul(buf, NULL, 10);
117
118 /* kill this process if it is mdmon */
119 sprintf(buf, "/proc/%lu/cmdline", (unsigned long) pid);
120 fd = open(buf, O_RDONLY);
121 if (fd < 0)
122 return;
123
124 if (read(fd, buf, sizeof(buf)) < 0) {
125 close(fd);
126 return;
127 }
128
129 if (strstr(buf, "mdmon") != NULL)
130 kill(pid, SIGTERM);
131}
132
e0d6609f
NB
133void remove_pidfile(char *devname)
134{
135 char buf[100];
136
137 sprintf(buf, "/var/run/mdadm/%s.pid", devname);
138 unlink(buf);
57752795
N
139 sprintf(buf, "/var/run/mdadm/%s.sock", devname);
140 unlink(buf);
e0d6609f
NB
141}
142
295646b3 143int make_control_sock(char *devname)
549e9569
NB
144{
145 char path[100];
146 int sfd;
147 long fl;
148 struct sockaddr_un addr;
149
150 sprintf(path, "/var/run/mdadm/%s.sock", devname);
151 unlink(path);
152 sfd = socket(PF_LOCAL, SOCK_STREAM, 0);
153 if (sfd < 0)
154 return -1;
155
156 addr.sun_family = PF_LOCAL;
157 strcpy(addr.sun_path, path);
158 if (bind(sfd, &addr, sizeof(addr)) < 0) {
159 close(sfd);
160 return -1;
161 }
162 listen(sfd, 10);
163 fl = fcntl(sfd, F_GETFL, 0);
164 fl |= O_NONBLOCK;
165 fcntl(sfd, F_SETFL, fl);
166 return sfd;
167}
168
295646b3
DW
169int socket_hup_requested;
170static void hup(int sig)
171{
172 socket_hup_requested = 1;
173}
174
4d43913c
NB
175static void wake_me(int sig)
176{
177
178}
179
16ddab0d
DW
180/* if we are debugging and starting mdmon by hand then don't fork */
181static int do_fork(void)
182{
183 #ifdef DEBUG
184 if (env_no_mdmon())
185 return 0;
186 #endif
187
188 return 1;
189}
190
191
192
549e9569
NB
193int main(int argc, char *argv[])
194{
195 int mdfd;
549e9569
NB
196 struct mdinfo *mdi, *di;
197 struct supertype *container;
4d43913c 198 sigset_t set;
bfa44e2e 199 struct sigaction act;
9fe32043
N
200 int pfd[2];
201 int status;
4d43913c 202
549e9569
NB
203 if (argc != 2) {
204 fprintf(stderr, "Usage: md-manage /device/name/for/container\n");
205 exit(2);
206 }
207 mdfd = open(argv[1], O_RDWR);
208 if (mdfd < 0) {
209 fprintf(stderr, "md-manage: %s: %s\n", argv[1],
210 strerror(errno));
211 exit(1);
212 }
213 if (md_get_version(mdfd) < 0) {
214 fprintf(stderr, "md-manage: %s: Not an md device\n",
215 argv[1]);
216 exit(1);
217 }
218
9fe32043 219 /* Fork, and have the child tell us when they are ready */
16ddab0d
DW
220 if (do_fork()) {
221 pipe(pfd);
222 switch(fork()) {
223 case -1:
224 fprintf(stderr, "mdmon: failed to fork: %s\n",
225 strerror(errno));
226 exit(1);
227 case 0: /* child */
228 close(pfd[0]);
229 break;
230 default: /* parent */
231 close(pfd[1]);
232 if (read(pfd[0], &status, sizeof(status)) != sizeof(status)) {
233 wait(&status);
234 status = WEXITSTATUS(status);
235 }
236 exit(status);
9fe32043 237 }
16ddab0d
DW
238 } else
239 pfd[0] = pfd[1] = -1;
549e9569
NB
240 /* hopefully it is a container - we'll check later */
241
242 container = malloc(sizeof(*container));
549e9569
NB
243 container->devnum = fd2devnum(mdfd);
244 container->devname = devnum2devname(container->devnum);
e0d6609f 245 container->device_name = argv[1];
549e9569
NB
246
247 /* If this fails, we hope it already exists */
248 mkdir("/var/run/mdadm", 0600);
249 /* pid file lives in /var/run/mdadm/mdXX.pid */
b109d928
DW
250 if (make_pidfile(container->devname, O_EXCL) < 0) {
251 if (ping_monitor(container->devname) == 0) {
252 fprintf(stderr, "mdmon: %s already managed\n",
253 container->devname);
254 exit(3);
255 } else {
295646b3
DW
256 int err;
257
b109d928
DW
258 /* cleanup the old monitor, this one is taking over */
259 try_kill_monitor(container->devname);
295646b3
DW
260 err = make_pidfile(container->devname, 0);
261 if (err < 0) {
b109d928
DW
262 fprintf(stderr, "mdmon: %s Cannot create pidfile\n",
263 container->devname);
295646b3
DW
264 if (err == -EROFS) {
265 /* FIXME implement a mechanism to
266 * prevent duplicate monitor instances
267 */
268 fprintf(stderr,
269 "mdmon: continuing on read-only file system\n");
270 } else
271 exit(3);
b109d928
DW
272 }
273 }
549e9569
NB
274 }
275
276 container->sock = make_control_sock(container->devname);
295646b3 277 if (container->sock < 0)
549e9569 278 fprintf(stderr, "mdmon: Cannot create socket in /var/run/mdadm\n");
549e9569
NB
279 container->arrays = NULL;
280
281 mdi = sysfs_read(mdfd, container->devnum,
282 GET_VERSION|GET_LEVEL|GET_DEVS);
283
284 if (!mdi) {
285 fprintf(stderr, "mdmon: failed to load sysfs info for %s\n",
286 container->devname);
287 exit(3);
288 }
289 if (mdi->array.level != UnSet) {
290 fprintf(stderr, "mdmon: %s is not a container - cannot monitor\n",
291 argv[1]);
292 exit(3);
293 }
294 if (mdi->array.major_version != -1 ||
295 mdi->array.minor_version != -2) {
296 fprintf(stderr, "mdmon: %s does not use external metadata - cannot monitor\n",
297 argv[1]);
298 exit(3);
299 }
300
301 container->ss = find_metadata_methods(mdi->text_version);
302 if (container->ss == NULL) {
303 fprintf(stderr, "mdmon: %s uses unknown metadata: %s\n",
304 argv[1], mdi->text_version);
305 exit(3);
306 }
307
308 container->devs = NULL;
309 for (di = mdi->devs; di; di = di->next) {
310 struct mdinfo *cd = malloc(sizeof(*cd));
7bc1962f 311 *cd = *di;
549e9569
NB
312 cd->next = container->devs;
313 container->devs = cd;
314 }
315 sysfs_free(mdi);
316
317
318 if (container->ss->load_super(container, mdfd, argv[1])) {
319 fprintf(stderr, "mdmon: Cannot load metadata for %s\n",
320 argv[1]);
321 exit(3);
322 }
549e9569 323
9fe32043
N
324 /* Ok, this is close enough. We can say goodbye to our parent now.
325 */
326 status = 0;
327 write(pfd[1], &status, sizeof(status));
328 close(pfd[1]);
329
330 chdir("/");
331 setsid();
332 close(0);
333 open("/dev/null", O_RDWR);
334 close(1);
335 dup(0);
336#ifndef DEBUG
337 close(2);
338 dup(0);
339#endif
340
549e9569
NB
341 mlockall(MCL_FUTURE);
342
4d43913c
NB
343 /* SIGUSR is sent between parent and child. So both block it
344 * and enable it only with pselect.
345 */
346 sigemptyset(&set);
347 sigaddset(&set, SIGUSR1);
295646b3 348 sigaddset(&set, SIGHUP);
4d43913c 349 sigprocmask(SIG_BLOCK, &set, NULL);
bfa44e2e
NB
350 act.sa_handler = wake_me;
351 act.sa_flags = 0;
352 sigaction(SIGUSR1, &act, NULL);
295646b3
DW
353 act.sa_handler = hup;
354 sigaction(SIGHUP, &act, NULL);
bfa44e2e
NB
355 act.sa_handler = SIG_IGN;
356 sigaction(SIGPIPE, &act, NULL);
4d43913c 357
3e70c845 358 if (clone_monitor(container) < 0) {
295646b3 359 fprintf(stderr, "mdmon: failed to start monitor process: %s\n",
549e9569
NB
360 strerror(errno));
361 exit(2);
362 }
363
364 do_manager(container);
365
366 exit(0);
367}