]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Copyright (C) 2008 Intel Corporation | |
3 | * | |
4 | * mdmon socket / message handling | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify it | |
7 | * under the terms and conditions of the GNU General Public License, | |
8 | * version 2, as published by the Free Software Foundation. | |
9 | * | |
10 | * This program is distributed in the hope it will be useful, but WITHOUT | |
11 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
12 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
13 | * more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License along with | |
16 | * this program; if not, write to the Free Software Foundation, Inc., | |
17 | * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. | |
18 | */ | |
19 | #ifndef _GNU_SOURCE | |
20 | #define _GNU_SOURCE | |
21 | #endif | |
22 | #include <unistd.h> | |
23 | #include <stdio.h> | |
24 | #include <string.h> | |
25 | #include <stdlib.h> | |
26 | #include <errno.h> | |
27 | #include <fcntl.h> | |
28 | #include <sys/types.h> | |
29 | #include <sys/socket.h> | |
30 | #include <sys/un.h> | |
31 | #include "mdadm.h" | |
32 | #include "mdmon.h" | |
33 | ||
34 | static const __u32 start_magic = 0x5a5aa5a5; | |
35 | static const __u32 end_magic = 0xa5a55a5a; | |
36 | ||
37 | static int send_buf(int fd, const void* buf, int len, int tmo) | |
38 | { | |
39 | fd_set set; | |
40 | int rv; | |
41 | struct timeval timeout = {tmo, 0}; | |
42 | struct timeval *ptmo = tmo ? &timeout : NULL; | |
43 | ||
44 | while (len) { | |
45 | FD_ZERO(&set); | |
46 | FD_SET(fd, &set); | |
47 | rv = select(fd+1, NULL, &set, NULL, ptmo); | |
48 | if (rv <= 0) | |
49 | return -1; | |
50 | rv = write(fd, buf, len); | |
51 | if (rv <= 0) | |
52 | return -1; | |
53 | len -= rv; | |
54 | buf += rv; | |
55 | } | |
56 | return 0; | |
57 | } | |
58 | ||
59 | static int recv_buf(int fd, void* buf, int len, int tmo) | |
60 | { | |
61 | fd_set set; | |
62 | int rv; | |
63 | struct timeval timeout = {tmo, 0}; | |
64 | struct timeval *ptmo = tmo ? &timeout : NULL; | |
65 | ||
66 | while (len) { | |
67 | FD_ZERO(&set); | |
68 | FD_SET(fd, &set); | |
69 | rv = select(fd+1, &set, NULL, NULL, ptmo); | |
70 | if (rv <= 0) | |
71 | return -1; | |
72 | rv = read(fd, buf, len); | |
73 | if (rv <= 0) | |
74 | return -1; | |
75 | len -= rv; | |
76 | buf += rv; | |
77 | } | |
78 | return 0; | |
79 | } | |
80 | ||
81 | ||
82 | int send_message(int fd, struct metadata_update *msg, int tmo) | |
83 | { | |
84 | __s32 len = msg->len; | |
85 | int rv; | |
86 | ||
87 | rv = send_buf(fd, &start_magic, 4, tmo); | |
88 | rv = rv ?: send_buf(fd, &len, 4, tmo); | |
89 | if (len > 0) | |
90 | rv = rv ?: send_buf(fd, msg->buf, msg->len, tmo); | |
91 | rv = send_buf(fd, &end_magic, 4, tmo); | |
92 | ||
93 | return rv; | |
94 | } | |
95 | ||
96 | int receive_message(int fd, struct metadata_update *msg, int tmo) | |
97 | { | |
98 | __u32 magic; | |
99 | __s32 len; | |
100 | int rv; | |
101 | ||
102 | rv = recv_buf(fd, &magic, 4, tmo); | |
103 | if (rv < 0 || magic != start_magic) | |
104 | return -1; | |
105 | rv = recv_buf(fd, &len, 4, tmo); | |
106 | if (rv < 0 || len > MSG_MAX_LEN) | |
107 | return -1; | |
108 | if (len > 0) { | |
109 | msg->buf = xmalloc(len); | |
110 | rv = recv_buf(fd, msg->buf, len, tmo); | |
111 | if (rv < 0) { | |
112 | free(msg->buf); | |
113 | return -1; | |
114 | } | |
115 | } else | |
116 | msg->buf = NULL; | |
117 | rv = recv_buf(fd, &magic, 4, tmo); | |
118 | if (rv < 0 || magic != end_magic) { | |
119 | free(msg->buf); | |
120 | return -1; | |
121 | } | |
122 | msg->len = len; | |
123 | return 0; | |
124 | } | |
125 | ||
126 | int ack(int fd, int tmo) | |
127 | { | |
128 | struct metadata_update msg = { .len = 0 }; | |
129 | ||
130 | return send_message(fd, &msg, tmo); | |
131 | } | |
132 | ||
133 | int wait_reply(int fd, int tmo) | |
134 | { | |
135 | struct metadata_update msg; | |
136 | int err = receive_message(fd, &msg, tmo); | |
137 | ||
138 | /* mdmon sent extra data, but caller only cares that we got a | |
139 | * successful reply | |
140 | */ | |
141 | if (err == 0 && msg.len > 0) | |
142 | free(msg.buf); | |
143 | ||
144 | return err; | |
145 | } | |
146 | ||
147 | int connect_monitor(char *devname) | |
148 | { | |
149 | char path[100]; | |
150 | int sfd; | |
151 | long fl; | |
152 | struct sockaddr_un addr; | |
153 | int pos; | |
154 | char *c; | |
155 | ||
156 | pos = sprintf(path, "%s/", MDMON_DIR); | |
157 | if (is_subarray(devname)) { | |
158 | devname++; | |
159 | c = strchr(devname, '/'); | |
160 | if (!c) | |
161 | return -1; | |
162 | snprintf(&path[pos], c - devname + 1, "%s", devname); | |
163 | pos += c - devname; | |
164 | } else | |
165 | pos += sprintf(&path[pos], "%s", devname); | |
166 | sprintf(&path[pos], ".sock"); | |
167 | ||
168 | sfd = socket(PF_LOCAL, SOCK_STREAM, 0); | |
169 | if (sfd < 0) | |
170 | return -1; | |
171 | ||
172 | addr.sun_family = PF_LOCAL; | |
173 | strcpy(addr.sun_path, path); | |
174 | if (connect(sfd, &addr, sizeof(addr)) < 0) { | |
175 | close(sfd); | |
176 | return -1; | |
177 | } | |
178 | ||
179 | fl = fcntl(sfd, F_GETFL, 0); | |
180 | fl |= O_NONBLOCK; | |
181 | fcntl(sfd, F_SETFL, fl); | |
182 | ||
183 | return sfd; | |
184 | } | |
185 | ||
186 | int fping_monitor(int sfd) | |
187 | { | |
188 | int err = 0; | |
189 | ||
190 | if (sfd < 0) | |
191 | return sfd; | |
192 | ||
193 | /* try to ping existing socket */ | |
194 | if (ack(sfd, 20) != 0) | |
195 | err = -1; | |
196 | ||
197 | /* check the reply */ | |
198 | if (!err && wait_reply(sfd, 20) != 0) | |
199 | err = -1; | |
200 | ||
201 | return err; | |
202 | } | |
203 | ||
204 | /* give the monitor a chance to update the metadata */ | |
205 | int ping_monitor(char *devname) | |
206 | { | |
207 | int sfd = connect_monitor(devname); | |
208 | int err; | |
209 | ||
210 | if (sfd >= 0) { | |
211 | err = fping_monitor(sfd); | |
212 | close(sfd); | |
213 | } else | |
214 | err = -1; | |
215 | ||
216 | return err; | |
217 | } | |
218 | ||
219 | static char *ping_monitor_version(char *devname) | |
220 | { | |
221 | int sfd = connect_monitor(devname); | |
222 | struct metadata_update msg; | |
223 | int err = 0; | |
224 | ||
225 | if (sfd < 0) | |
226 | return NULL; | |
227 | ||
228 | if (ack(sfd, 20) != 0) | |
229 | err = -1; | |
230 | ||
231 | if (!err && receive_message(sfd, &msg, 20) != 0) | |
232 | err = -1; | |
233 | ||
234 | close(sfd); | |
235 | ||
236 | if (err || !msg.len || !msg.buf) | |
237 | return NULL; | |
238 | return msg.buf; | |
239 | } | |
240 | ||
241 | int unblock_subarray(struct mdinfo *sra, const int unfreeze) | |
242 | { | |
243 | char buf[64]; | |
244 | int rc = 0; | |
245 | ||
246 | if (sra) { | |
247 | sprintf(buf, "external:%s\n", sra->text_version); | |
248 | buf[9] = '/'; | |
249 | } else | |
250 | buf[9] = '-'; | |
251 | ||
252 | if (buf[9] == '-' || | |
253 | sysfs_set_str(sra, NULL, "metadata_version", buf) || | |
254 | (unfreeze && | |
255 | sysfs_attribute_available(sra, NULL, "sync_action") && | |
256 | sysfs_set_str(sra, NULL, "sync_action", "idle"))) | |
257 | rc = -1; | |
258 | return rc; | |
259 | } | |
260 | ||
261 | int block_subarray(struct mdinfo *sra) | |
262 | { | |
263 | char buf[64]; | |
264 | int rc = 0; | |
265 | ||
266 | sprintf(buf, "external:%s\n", sra->text_version); | |
267 | buf[9] = '-'; | |
268 | if (sysfs_set_str(sra, NULL, "metadata_version", buf)) | |
269 | rc = -1; | |
270 | ||
271 | return rc; | |
272 | } | |
273 | ||
274 | /* check mdmon version if it supports | |
275 | * array blocking mechanism | |
276 | */ | |
277 | int check_mdmon_version(char *container) | |
278 | { | |
279 | char *version = NULL; | |
280 | ||
281 | if (!mdmon_running(container)) { | |
282 | /* if mdmon is not active we assume that any instance that is | |
283 | * later started will match the current mdadm version, if this | |
284 | * assumption is violated we may inadvertantly rebuild an array | |
285 | * that was meant for reshape, or start rebuild on a spare that | |
286 | * was to be moved to another container | |
287 | */ | |
288 | /* pass */; | |
289 | } else { | |
290 | int ver; | |
291 | ||
292 | version = ping_monitor_version(container); | |
293 | ver = version ? mdadm_version(version) : -1; | |
294 | free(version); | |
295 | if (ver < 3002000) { | |
296 | pr_err("mdmon instance for %s cannot be disabled\n", | |
297 | container); | |
298 | return -1; | |
299 | } | |
300 | } | |
301 | ||
302 | return 0; | |
303 | } | |
304 | ||
305 | /** | |
306 | * block_monitor - prevent mdmon spare assignment | |
307 | * @container - container to block | |
308 | * @freeze - flag to additionally freeze sync_action | |
309 | * | |
310 | * This is used by the reshape code to freeze the container, and the | |
311 | * auto-rebuild implementation to atomically move spares. | |
312 | * In both cases we need to stop mdmon from assigning spares to replace | |
313 | * failed devices as we might have other plans for the spare. | |
314 | * For the reshape case we also need to 'freeze' sync_action so that | |
315 | * no recovery happens until we have fully prepared for the reshape. | |
316 | * | |
317 | * We tell mdmon that the array is frozen by marking the 'metadata' name | |
318 | * with a leading '-'. The previously told mdmon "Don't make this array | |
319 | * read/write, leave it readonly". Now it means a more general "Don't | |
320 | * reconfigure this array at all". | |
321 | * As older versions of mdmon (which might run from initrd) don't understand | |
322 | * this, we first check that the running mdmon is new enough. | |
323 | */ | |
324 | int block_monitor(char *container, const int freeze) | |
325 | { | |
326 | struct mdstat_ent *ent, *e, *e2; | |
327 | struct mdinfo *sra = NULL; | |
328 | char buf[64]; | |
329 | int rv = 0; | |
330 | ||
331 | if (check_mdmon_version(container)) | |
332 | return -1; | |
333 | ||
334 | ent = mdstat_read(0, 0); | |
335 | if (!ent) { | |
336 | pr_err("failed to read /proc/mdstat while disabling mdmon\n"); | |
337 | return -1; | |
338 | } | |
339 | ||
340 | /* freeze container contents */ | |
341 | for (e = ent; e; e = e->next) { | |
342 | if (!is_container_member(e, container)) | |
343 | continue; | |
344 | sysfs_free(sra); | |
345 | sra = sysfs_read(-1, e->devnm, GET_VERSION); | |
346 | if (!sra) { | |
347 | pr_err("failed to read sysfs for subarray%s\n", | |
348 | to_subarray(e, container)); | |
349 | break; | |
350 | } | |
351 | /* can't reshape an array that we can't monitor */ | |
352 | if (sra->text_version[0] == '-') | |
353 | break; | |
354 | ||
355 | if (freeze && sysfs_freeze_array(sra) < 1) | |
356 | break; | |
357 | /* flag this array to not be modified by mdmon (close race with | |
358 | * takeover in reshape case and spare reassignment in the | |
359 | * auto-rebuild case) | |
360 | */ | |
361 | if (block_subarray(sra)) | |
362 | break; | |
363 | ping_monitor(container); | |
364 | ||
365 | /* check that we did not race with recovery */ | |
366 | if ((freeze && | |
367 | !sysfs_attribute_available(sra, NULL, "sync_action")) || | |
368 | (freeze && | |
369 | sysfs_attribute_available(sra, NULL, "sync_action") && | |
370 | sysfs_get_str(sra, NULL, "sync_action", buf, 20) > 0 && | |
371 | strcmp(buf, "frozen\n") == 0)) | |
372 | /* pass */; | |
373 | else { | |
374 | unblock_subarray(sra, 0); | |
375 | break; | |
376 | } | |
377 | /* Double check against races - there should be no spares | |
378 | * or part-spares | |
379 | */ | |
380 | sysfs_free(sra); | |
381 | sra = sysfs_read(-1, e->devnm, GET_DEVS | GET_STATE); | |
382 | if (sra && sra->array.spare_disks > 0) { | |
383 | unblock_subarray(sra, freeze); | |
384 | break; | |
385 | } | |
386 | } | |
387 | ||
388 | if (e) { | |
389 | pr_err("failed to freeze subarray%s\n", | |
390 | to_subarray(e, container)); | |
391 | ||
392 | /* thaw the partially frozen container */ | |
393 | for (e2 = ent; e2 && e2 != e; e2 = e2->next) { | |
394 | if (!is_container_member(e2, container)) | |
395 | continue; | |
396 | sysfs_free(sra); | |
397 | sra = sysfs_read(-1, e2->devnm, GET_VERSION); | |
398 | if (unblock_subarray(sra, freeze)) | |
399 | pr_err("Failed to unfreeze %s\n", e2->dev); | |
400 | } | |
401 | ||
402 | ping_monitor(container); /* cleared frozen */ | |
403 | rv = -1; | |
404 | } | |
405 | ||
406 | sysfs_free(sra); | |
407 | free_mdstat(ent); | |
408 | ||
409 | return rv; | |
410 | } | |
411 | ||
412 | void unblock_monitor(char *container, const int unfreeze) | |
413 | { | |
414 | struct mdstat_ent *ent, *e; | |
415 | struct mdinfo *sra = NULL; | |
416 | int to_ping = 0; | |
417 | ||
418 | ent = mdstat_read(0, 0); | |
419 | if (!ent) { | |
420 | pr_err("failed to read /proc/mdstat while unblocking container\n"); | |
421 | return; | |
422 | } | |
423 | ||
424 | /* unfreeze container contents */ | |
425 | for (e = ent; e; e = e->next) { | |
426 | if (!is_container_member(e, container)) | |
427 | continue; | |
428 | sysfs_free(sra); | |
429 | sra = sysfs_read(-1, e->devnm, GET_VERSION|GET_LEVEL); | |
430 | if (!sra) | |
431 | continue; | |
432 | if (sra->array.level > 0) | |
433 | to_ping++; | |
434 | if (unblock_subarray(sra, unfreeze)) | |
435 | pr_err("Failed to unfreeze %s\n", e->dev); | |
436 | } | |
437 | if (to_ping) | |
438 | ping_monitor(container); | |
439 | ||
440 | sysfs_free(sra); | |
441 | free_mdstat(ent); | |
442 | } | |
443 | ||
444 | /* give the manager a chance to view the updated container state. This | |
445 | * would naturally happen due to the manager noticing a change in | |
446 | * /proc/mdstat; however, pinging encourages this detection to happen | |
447 | * while an exclusive open() on the container is active | |
448 | */ | |
449 | int ping_manager(char *devname) | |
450 | { | |
451 | int sfd = connect_monitor(devname); | |
452 | struct metadata_update msg = { .len = -1 }; | |
453 | int err = 0; | |
454 | ||
455 | if (sfd < 0) | |
456 | return sfd; | |
457 | ||
458 | err = send_message(sfd, &msg, 20); | |
459 | ||
460 | /* check the reply */ | |
461 | if (!err && wait_reply(sfd, 20) != 0) | |
462 | err = -1; | |
463 | ||
464 | close(sfd); | |
465 | return err; | |
466 | } | |
467 | ||
468 | /* using takeover operation for grow purposes, mdadm has to be sure | |
469 | * that mdmon processes all updates, and if necessary it will be closed | |
470 | * at takeover to raid0 operation | |
471 | */ | |
472 | void flush_mdmon(char *container) | |
473 | { | |
474 | ping_manager(container); | |
475 | ping_monitor(container); | |
476 | } |