]>
Commit | Line | Data |
---|---|---|
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> | |
29 | #include <sys/stat.h> | |
30 | #include <sys/socket.h> | |
31 | #include <sys/un.h> | |
32 | #include <sys/mman.h> | |
33 | #include <stdio.h> | |
34 | #include <errno.h> | |
35 | #include <string.h> | |
36 | #include <fcntl.h> | |
b109d928 | 37 | #include <signal.h> |
549e9569 NB |
38 | |
39 | #include <sched.h> | |
40 | ||
41 | #include "mdadm.h" | |
42 | #include "mdmon.h" | |
43 | ||
549e9569 NB |
44 | struct active_array *discard_this; |
45 | struct active_array *pending_discard; | |
3e70c845 | 46 | struct md_generic_cmd *active_cmd; |
549e9569 NB |
47 | |
48 | int run_child(void *v) | |
49 | { | |
50 | struct supertype *c = v; | |
51 | do_monitor(c); | |
52 | return 0; | |
53 | } | |
54 | ||
55 | int clone_monitor(struct supertype *container) | |
56 | { | |
549e9569 NB |
57 | static char stack[4096]; |
58 | int rv; | |
59 | ||
3e70c845 DW |
60 | rv = pipe(container->mgr_pipe); |
61 | if (rv < 0) | |
62 | return rv; | |
63 | rv = pipe(container->mon_pipe); | |
64 | if (rv < 0) | |
65 | goto err_mon_pipe; | |
549e9569 NB |
66 | |
67 | rv = clone(run_child, stack+4096-64, | |
68 | CLONE_FS|CLONE_FILES|CLONE_VM|CLONE_SIGHAND|CLONE_THREAD, | |
69 | container); | |
549e9569 | 70 | if (rv < 0) |
3e70c845 DW |
71 | goto err_clone; |
72 | else | |
549e9569 | 73 | return rv; |
3e70c845 DW |
74 | |
75 | err_clone: | |
76 | close(container->mon_pipe[0]); | |
77 | close(container->mon_pipe[1]); | |
78 | err_mon_pipe: | |
79 | close(container->mgr_pipe[0]); | |
80 | close(container->mgr_pipe[1]); | |
81 | ||
82 | return rv; | |
549e9569 NB |
83 | } |
84 | ||
85 | static struct superswitch *find_metadata_methods(char *vers) | |
86 | { | |
87 | if (strcmp(vers, "ddf") == 0) | |
88 | return &super_ddf; | |
89 | return NULL; | |
90 | } | |
91 | ||
92 | ||
b109d928 | 93 | static int make_pidfile(char *devname, int o_excl) |
549e9569 NB |
94 | { |
95 | char path[100]; | |
96 | char pid[10]; | |
97 | int fd; | |
98 | sprintf(path, "/var/run/mdadm/%s.pid", devname); | |
99 | ||
b109d928 | 100 | fd = open(path, O_RDWR|O_CREAT|o_excl, 0600); |
549e9569 NB |
101 | if (fd < 0) |
102 | return -1; | |
103 | sprintf(pid, "%d\n", getpid()); | |
104 | write(fd, pid, strlen(pid)); | |
105 | close(fd); | |
106 | return 0; | |
107 | } | |
108 | ||
b109d928 DW |
109 | static void try_kill_monitor(char *devname) |
110 | { | |
111 | char buf[100]; | |
112 | int fd; | |
113 | pid_t pid; | |
114 | ||
115 | sprintf(buf, "/var/run/mdadm/%s.pid", devname); | |
116 | fd = open(buf, O_RDONLY); | |
117 | if (fd < 0) | |
118 | return; | |
119 | ||
120 | if (read(fd, buf, sizeof(buf)) < 0) { | |
121 | close(fd); | |
122 | return; | |
123 | } | |
124 | ||
125 | close(fd); | |
126 | pid = strtoul(buf, NULL, 10); | |
127 | ||
128 | /* kill this process if it is mdmon */ | |
129 | sprintf(buf, "/proc/%lu/cmdline", (unsigned long) pid); | |
130 | fd = open(buf, O_RDONLY); | |
131 | if (fd < 0) | |
132 | return; | |
133 | ||
134 | if (read(fd, buf, sizeof(buf)) < 0) { | |
135 | close(fd); | |
136 | return; | |
137 | } | |
138 | ||
139 | if (strstr(buf, "mdmon") != NULL) | |
140 | kill(pid, SIGTERM); | |
141 | } | |
142 | ||
549e9569 NB |
143 | static int make_control_sock(char *devname) |
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 | ||
169 | int main(int argc, char *argv[]) | |
170 | { | |
171 | int mdfd; | |
549e9569 NB |
172 | struct mdinfo *mdi, *di; |
173 | struct supertype *container; | |
174 | if (argc != 2) { | |
175 | fprintf(stderr, "Usage: md-manage /device/name/for/container\n"); | |
176 | exit(2); | |
177 | } | |
178 | mdfd = open(argv[1], O_RDWR); | |
179 | if (mdfd < 0) { | |
180 | fprintf(stderr, "md-manage: %s: %s\n", argv[1], | |
181 | strerror(errno)); | |
182 | exit(1); | |
183 | } | |
184 | if (md_get_version(mdfd) < 0) { | |
185 | fprintf(stderr, "md-manage: %s: Not an md device\n", | |
186 | argv[1]); | |
187 | exit(1); | |
188 | } | |
189 | ||
190 | /* hopefully it is a container - we'll check later */ | |
191 | ||
192 | container = malloc(sizeof(*container)); | |
193 | container->devfd = mdfd; | |
194 | container->devnum = fd2devnum(mdfd); | |
195 | container->devname = devnum2devname(container->devnum); | |
196 | ||
197 | /* If this fails, we hope it already exists */ | |
198 | mkdir("/var/run/mdadm", 0600); | |
199 | /* pid file lives in /var/run/mdadm/mdXX.pid */ | |
b109d928 DW |
200 | if (make_pidfile(container->devname, O_EXCL) < 0) { |
201 | if (ping_monitor(container->devname) == 0) { | |
202 | fprintf(stderr, "mdmon: %s already managed\n", | |
203 | container->devname); | |
204 | exit(3); | |
205 | } else { | |
206 | /* cleanup the old monitor, this one is taking over */ | |
207 | try_kill_monitor(container->devname); | |
208 | if (make_pidfile(container->devname, 0) < 0) { | |
209 | fprintf(stderr, "mdmon: %s Cannot create pidfile\n", | |
210 | container->devname); | |
211 | exit(3); | |
212 | } | |
213 | } | |
549e9569 NB |
214 | } |
215 | ||
216 | container->sock = make_control_sock(container->devname); | |
217 | if (container->sock < 0) { | |
218 | fprintf(stderr, "mdmon: Cannot create socket in /var/run/mdadm\n"); | |
219 | exit(3); | |
220 | } | |
221 | container->arrays = NULL; | |
222 | ||
223 | mdi = sysfs_read(mdfd, container->devnum, | |
224 | GET_VERSION|GET_LEVEL|GET_DEVS); | |
225 | ||
226 | if (!mdi) { | |
227 | fprintf(stderr, "mdmon: failed to load sysfs info for %s\n", | |
228 | container->devname); | |
229 | exit(3); | |
230 | } | |
231 | if (mdi->array.level != UnSet) { | |
232 | fprintf(stderr, "mdmon: %s is not a container - cannot monitor\n", | |
233 | argv[1]); | |
234 | exit(3); | |
235 | } | |
236 | if (mdi->array.major_version != -1 || | |
237 | mdi->array.minor_version != -2) { | |
238 | fprintf(stderr, "mdmon: %s does not use external metadata - cannot monitor\n", | |
239 | argv[1]); | |
240 | exit(3); | |
241 | } | |
242 | ||
243 | container->ss = find_metadata_methods(mdi->text_version); | |
244 | if (container->ss == NULL) { | |
245 | fprintf(stderr, "mdmon: %s uses unknown metadata: %s\n", | |
246 | argv[1], mdi->text_version); | |
247 | exit(3); | |
248 | } | |
249 | ||
250 | container->devs = NULL; | |
251 | for (di = mdi->devs; di; di = di->next) { | |
252 | struct mdinfo *cd = malloc(sizeof(*cd)); | |
253 | cd = di; | |
254 | cd->next = container->devs; | |
255 | container->devs = cd; | |
256 | } | |
257 | sysfs_free(mdi); | |
258 | ||
259 | ||
260 | if (container->ss->load_super(container, mdfd, argv[1])) { | |
261 | fprintf(stderr, "mdmon: Cannot load metadata for %s\n", | |
262 | argv[1]); | |
263 | exit(3); | |
264 | } | |
265 | ||
266 | ||
267 | mlockall(MCL_FUTURE); | |
268 | ||
3e70c845 | 269 | if (clone_monitor(container) < 0) { |
549e9569 NB |
270 | fprintf(stderr, "md-manage: failed to start monitor process: %s\n", |
271 | strerror(errno)); | |
272 | exit(2); | |
273 | } | |
274 | ||
275 | do_manager(container); | |
276 | ||
277 | exit(0); | |
278 | } |