]> git.ipfire.org Git - thirdparty/mdadm.git/blob - msg.c
Check all member devices in enough_fd
[thirdparty/mdadm.git] / msg.c
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 = malloc(len);
110 if (msg->buf == NULL)
111 return -1;
112 rv = recv_buf(fd, msg->buf, len, tmo);
113 if (rv < 0) {
114 free(msg->buf);
115 return -1;
116 }
117 } else
118 msg->buf = NULL;
119 rv = recv_buf(fd, &magic, 4, tmo);
120 if (rv < 0 || magic != end_magic) {
121 free(msg->buf);
122 return -1;
123 }
124 msg->len = len;
125 return 0;
126 }
127
128 int ack(int fd, int tmo)
129 {
130 struct metadata_update msg = { .len = 0 };
131
132 return send_message(fd, &msg, tmo);
133 }
134
135 int wait_reply(int fd, int tmo)
136 {
137 struct metadata_update msg;
138 int err = receive_message(fd, &msg, tmo);
139
140 /* mdmon sent extra data, but caller only cares that we got a
141 * successful reply
142 */
143 if (err == 0 && msg.len > 0)
144 free(msg.buf);
145
146 return err;
147 }
148
149 int connect_monitor(char *devname)
150 {
151 char path[100];
152 int sfd;
153 long fl;
154 struct sockaddr_un addr;
155 int pos;
156 char *c;
157
158 pos = sprintf(path, "%s/", MDMON_DIR);
159 if (is_subarray(devname)) {
160 devname++;
161 c = strchr(devname, '/');
162 if (!c)
163 return -1;
164 snprintf(&path[pos], c - devname + 1, "%s", devname);
165 pos += c - devname;
166 } else
167 pos += sprintf(&path[pos], "%s", devname);
168 sprintf(&path[pos], ".sock");
169
170 sfd = socket(PF_LOCAL, SOCK_STREAM, 0);
171 if (sfd < 0)
172 return -1;
173
174 addr.sun_family = PF_LOCAL;
175 strcpy(addr.sun_path, path);
176 if (connect(sfd, &addr, sizeof(addr)) < 0) {
177 close(sfd);
178 return -1;
179 }
180
181 fl = fcntl(sfd, F_GETFL, 0);
182 fl |= O_NONBLOCK;
183 fcntl(sfd, F_SETFL, fl);
184
185 return sfd;
186 }
187
188 int fping_monitor(int sfd)
189 {
190 int err = 0;
191
192 if (sfd < 0)
193 return sfd;
194
195 /* try to ping existing socket */
196 if (ack(sfd, 20) != 0)
197 err = -1;
198
199 /* check the reply */
200 if (!err && wait_reply(sfd, 20) != 0)
201 err = -1;
202
203 return err;
204 }
205
206 /* give the monitor a chance to update the metadata */
207 int ping_monitor(char *devname)
208 {
209 int sfd = connect_monitor(devname);
210 int err = fping_monitor(sfd);
211
212 close(sfd);
213 return err;
214 }
215
216 /* ping monitor using device number */
217 int ping_monitor_by_id(int devnum)
218 {
219 int err = -1;
220 char *container = devnum2devname(devnum);
221
222 if (container) {
223 err = ping_monitor(container);
224 free(container);
225 }
226
227 return err;
228 }
229
230 static char *ping_monitor_version(char *devname)
231 {
232 int sfd = connect_monitor(devname);
233 struct metadata_update msg;
234 int err = 0;
235
236 if (sfd < 0)
237 return NULL;
238
239 if (ack(sfd, 20) != 0)
240 err = -1;
241
242 if (!err && receive_message(sfd, &msg, 20) != 0)
243 err = -1;
244
245 close(sfd);
246
247 if (err || !msg.len || !msg.buf)
248 return NULL;
249 return msg.buf;
250 }
251
252 int unblock_subarray(struct mdinfo *sra, const int unfreeze)
253 {
254 char buf[64];
255 int rc = 0;
256
257 if (sra) {
258 sprintf(buf, "external:%s\n", sra->text_version);
259 buf[9] = '/';
260 } else
261 buf[9] = '-';
262
263 if (buf[9] == '-' ||
264 sysfs_set_str(sra, NULL, "metadata_version", buf) ||
265 (unfreeze &&
266 sysfs_attribute_available(sra, NULL, "sync_action") &&
267 sysfs_set_str(sra, NULL, "sync_action", "idle")))
268 rc = -1;
269 return rc;
270 }
271
272 int block_subarray(struct mdinfo *sra)
273 {
274 char buf[64];
275 int rc = 0;
276
277 sprintf(buf, "external:%s\n", sra->text_version);
278 buf[9] = '-';
279 if (sysfs_set_str(sra, NULL, "metadata_version", buf))
280 rc = -1;
281
282 return rc;
283 }
284 /**
285 * block_monitor - prevent mdmon spare assignment
286 * @container - container to block
287 * @freeze - flag to additionally freeze sync_action
288 *
289 * This is used by the reshape code to freeze the container, and the
290 * auto-rebuild implementation to atomically move spares.
291 * In both cases we need to stop mdmon from assigning spares to replace
292 * failed devices as we might have other plans for the spare.
293 * For the reshape case we also need to 'freeze' sync_action so that
294 * no recovery happens until we have fully prepared for the reshape.
295 *
296 * We tell mdmon that the array is frozen by marking the 'metadata' name
297 * with a leading '-'. The previously told mdmon "Don't make this array
298 * read/write, leave it readonly". Now it means a more general "Don't
299 * reconfigure this array at all".
300 * As older versions of mdmon (which might run from initrd) don't understand
301 * this, we first check that the running mdmon is new enough.
302 */
303 int block_monitor(char *container, const int freeze)
304 {
305 int devnum = devname2devnum(container);
306 struct mdstat_ent *ent, *e, *e2;
307 struct mdinfo *sra = NULL;
308 char *version = NULL;
309 char buf[64];
310 int rv = 0;
311
312 if (!mdmon_running(devnum)) {
313 /* if mdmon is not active we assume that any instance that is
314 * later started will match the current mdadm version, if this
315 * assumption is violated we may inadvertantly rebuild an array
316 * that was meant for reshape, or start rebuild on a spare that
317 * was to be moved to another container
318 */
319 /* pass */;
320 } else {
321 int ver;
322
323 version = ping_monitor_version(container);
324 ver = version ? mdadm_version(version) : -1;
325 free(version);
326 if (ver < 3002000) {
327 fprintf(stderr, Name
328 ": mdmon instance for %s cannot be disabled\n",
329 container);
330 return -1;
331 }
332 }
333
334 ent = mdstat_read(0, 0);
335 if (!ent) {
336 fprintf(stderr, Name
337 ": failed to read /proc/mdstat while disabling mdmon\n");
338 return -1;
339 }
340
341 /* freeze container contents */
342 for (e = ent; e; e = e->next) {
343 if (!is_container_member(e, container))
344 continue;
345 sysfs_free(sra);
346 sra = sysfs_read(-1, e->devnum, GET_VERSION);
347 if (!sra) {
348 fprintf(stderr, Name
349 ": failed to read sysfs for subarray%s\n",
350 to_subarray(e, container));
351 break;
352 }
353 /* can't reshape an array that we can't monitor */
354 if (sra->text_version[0] == '-')
355 break;
356
357 if (freeze && sysfs_freeze_array(sra) < 1)
358 break;
359 /* flag this array to not be modified by mdmon (close race with
360 * takeover in reshape case and spare reassignment in the
361 * auto-rebuild case)
362 */
363 if (block_subarray(sra))
364 break;
365 ping_monitor(container);
366
367 /* check that we did not race with recovery */
368 if ((freeze &&
369 !sysfs_attribute_available(sra, NULL, "sync_action")) ||
370 (freeze &&
371 sysfs_attribute_available(sra, NULL, "sync_action") &&
372 sysfs_get_str(sra, NULL, "sync_action", buf, 20) > 0 &&
373 strcmp(buf, "frozen\n") == 0))
374 /* pass */;
375 else {
376 unblock_subarray(sra, 0);
377 break;
378 }
379 /* Double check against races - there should be no spares
380 * or part-spares
381 */
382 sysfs_free(sra);
383 sra = sysfs_read(-1, e->devnum, GET_DEVS | GET_STATE);
384 if (sra && sra->array.spare_disks > 0) {
385 unblock_subarray(sra, freeze);
386 break;
387 }
388 }
389
390 if (e) {
391 fprintf(stderr, Name ": failed to freeze subarray%s\n",
392 to_subarray(e, container));
393
394 /* thaw the partially frozen container */
395 for (e2 = ent; e2 && e2 != e; e2 = e2->next) {
396 if (!is_container_member(e2, container))
397 continue;
398 sysfs_free(sra);
399 sra = sysfs_read(-1, e2->devnum, GET_VERSION);
400 if (unblock_subarray(sra, freeze))
401 fprintf(stderr, Name ": Failed to unfreeze %s\n", e2->dev);
402 }
403
404 ping_monitor(container); /* cleared frozen */
405 rv = -1;
406 }
407
408 sysfs_free(sra);
409 free_mdstat(ent);
410
411 return rv;
412 }
413
414 void unblock_monitor(char *container, const int unfreeze)
415 {
416 struct mdstat_ent *ent, *e;
417 struct mdinfo *sra = NULL;
418 int to_ping = 0;
419
420 ent = mdstat_read(0, 0);
421 if (!ent) {
422 fprintf(stderr, Name
423 ": failed to read /proc/mdstat while unblocking container\n");
424 return;
425 }
426
427 /* unfreeze container contents */
428 for (e = ent; e; e = e->next) {
429 if (!is_container_member(e, container))
430 continue;
431 sysfs_free(sra);
432 sra = sysfs_read(-1, e->devnum, GET_VERSION|GET_LEVEL);
433 if (sra->array.level > 0)
434 to_ping++;
435 if (unblock_subarray(sra, unfreeze))
436 fprintf(stderr, Name ": Failed to unfreeze %s\n", e->dev);
437 }
438 if (to_ping)
439 ping_monitor(container);
440
441 sysfs_free(sra);
442 free_mdstat(ent);
443 }
444
445
446
447 /* give the manager a chance to view the updated container state. This
448 * would naturally happen due to the manager noticing a change in
449 * /proc/mdstat; however, pinging encourages this detection to happen
450 * while an exclusive open() on the container is active
451 */
452 int ping_manager(char *devname)
453 {
454 int sfd = connect_monitor(devname);
455 struct metadata_update msg = { .len = -1 };
456 int err = 0;
457
458 if (sfd < 0)
459 return sfd;
460
461 err = send_message(sfd, &msg, 20);
462
463 /* check the reply */
464 if (!err && wait_reply(sfd, 20) != 0)
465 err = -1;
466
467 close(sfd);
468 return err;
469 }