]> git.ipfire.org Git - thirdparty/mdadm.git/blame - msg.c
Remove re_add flag in favour of new disposition.
[thirdparty/mdadm.git] / msg.c
CommitLineData
f7dd881f
DW
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"
bfa44e2e 32#include "mdmon.h"
f7dd881f 33
bfa44e2e
NB
34static const __u32 start_magic = 0x5a5aa5a5;
35static const __u32 end_magic = 0xa5a55a5a;
36
37static int send_buf(int fd, const void* buf, int len, int tmo)
f7dd881f 38{
bfa44e2e
NB
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}
f7dd881f 58
bfa44e2e
NB
59static 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;
f7dd881f
DW
79}
80
81
bfa44e2e 82int send_message(int fd, struct metadata_update *msg, int tmo)
f7dd881f 83{
313a4a82 84 __s32 len = msg->len;
bfa44e2e
NB
85 int rv;
86
87 rv = send_buf(fd, &start_magic, 4, tmo);
88 rv = rv ?: send_buf(fd, &len, 4, tmo);
313a4a82 89 if (len > 0)
bfa44e2e
NB
90 rv = rv ?: send_buf(fd, msg->buf, msg->len, tmo);
91 rv = send_buf(fd, &end_magic, 4, tmo);
92
93 return rv;
f7dd881f
DW
94}
95
bfa44e2e 96int receive_message(int fd, struct metadata_update *msg, int tmo)
f7dd881f 97{
bfa44e2e 98 __u32 magic;
313a4a82 99 __s32 len;
bfa44e2e
NB
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)
f7dd881f 107 return -1;
313a4a82 108 if (len > 0) {
bfa44e2e
NB
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;
f7dd881f
DW
126}
127
bfa44e2e 128int ack(int fd, int tmo)
f7dd881f 129{
bfa44e2e 130 struct metadata_update msg = { .len = 0 };
f7dd881f
DW
131
132 return send_message(fd, &msg, tmo);
133}
134
bfa44e2e 135int wait_reply(int fd, int tmo)
f7dd881f 136{
bfa44e2e 137 struct metadata_update msg;
bc77ed53
DW
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;
f7dd881f
DW
147}
148
f7dd881f
DW
149int connect_monitor(char *devname)
150{
151 char path[100];
152 int sfd;
153 long fl;
154 struct sockaddr_un addr;
c94709e8
DW
155 int pos;
156 char *c;
157
753cf905 158 pos = sprintf(path, "%s/", MDMON_DIR);
c94709e8
DW
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");
f7dd881f 169
f7dd881f
DW
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
9f1da824 188int fping_monitor(int sfd)
f7dd881f 189{
f7dd881f
DW
190 int err = 0;
191
192 if (sfd < 0)
193 return sfd;
194
195 /* try to ping existing socket */
eb2c876f 196 if (ack(sfd, 20) != 0)
f7dd881f
DW
197 err = -1;
198
199 /* check the reply */
eb2c876f 200 if (!err && wait_reply(sfd, 20) != 0)
f7dd881f
DW
201 err = -1;
202
9f1da824
DW
203 return err;
204}
205
9f1da824
DW
206/* give the monitor a chance to update the metadata */
207int ping_monitor(char *devname)
208{
209 int sfd = connect_monitor(devname);
b36c8935
JS
210 int err;
211
212 if (sfd >= 0) {
213 err = fping_monitor(sfd);
214 close(sfd);
215 } else
216 err = -1;
9f1da824 217
f7dd881f
DW
218 return err;
219}
313a4a82 220
983fff45
AK
221/* ping monitor using device number */
222int ping_monitor_by_id(int devnum)
223{
224 int err = -1;
225 char *container = devnum2devname(devnum);
226
227 if (container) {
228 err = ping_monitor(container);
229 free(container);
230 }
231
232 return err;
233}
234
bc77ed53
DW
235static char *ping_monitor_version(char *devname)
236{
237 int sfd = connect_monitor(devname);
238 struct metadata_update msg;
239 int err = 0;
240
241 if (sfd < 0)
242 return NULL;
243
244 if (ack(sfd, 20) != 0)
245 err = -1;
246
247 if (!err && receive_message(sfd, &msg, 20) != 0)
248 err = -1;
249
250 close(sfd);
251
252 if (err || !msg.len || !msg.buf)
253 return NULL;
254 return msg.buf;
255}
256
2a093697 257int unblock_subarray(struct mdinfo *sra, const int unfreeze)
bc77ed53
DW
258{
259 char buf[64];
260 int rc = 0;
261
262 if (sra) {
263 sprintf(buf, "external:%s\n", sra->text_version);
264 buf[9] = '/';
265 } else
266 buf[9] = '-';
267
268 if (buf[9] == '-' ||
269 sysfs_set_str(sra, NULL, "metadata_version", buf) ||
270 (unfreeze &&
271 sysfs_attribute_available(sra, NULL, "sync_action") &&
272 sysfs_set_str(sra, NULL, "sync_action", "idle")))
273 rc = -1;
274 return rc;
275}
276
2a093697
AK
277int block_subarray(struct mdinfo *sra)
278{
279 char buf[64];
280 int rc = 0;
281
282 sprintf(buf, "external:%s\n", sra->text_version);
283 buf[9] = '-';
284 if (sysfs_set_str(sra, NULL, "metadata_version", buf))
285 rc = -1;
286
287 return rc;
288}
7aa437c2
AK
289
290/* check mdmon version if it supports
291 * array blocking mechanism
292 */
293int check_mdmon_version(char *container)
294{
295 char *version = NULL;
296 int devnum = devname2devnum(container);
297
298 if (!mdmon_running(devnum)) {
299 /* if mdmon is not active we assume that any instance that is
300 * later started will match the current mdadm version, if this
301 * assumption is violated we may inadvertantly rebuild an array
302 * that was meant for reshape, or start rebuild on a spare that
303 * was to be moved to another container
304 */
305 /* pass */;
306 } else {
307 int ver;
308
309 version = ping_monitor_version(container);
310 ver = version ? mdadm_version(version) : -1;
311 free(version);
312 if (ver < 3002000) {
e7b84f9d
N
313 pr_err("mdmon instance for %s cannot be disabled\n",
314 container);
7aa437c2
AK
315 return -1;
316 }
317 }
318
319 return 0;
320}
321
bc77ed53
DW
322/**
323 * block_monitor - prevent mdmon spare assignment
324 * @container - container to block
325 * @freeze - flag to additionally freeze sync_action
326 *
327 * This is used by the reshape code to freeze the container, and the
746a6567
N
328 * auto-rebuild implementation to atomically move spares.
329 * In both cases we need to stop mdmon from assigning spares to replace
330 * failed devices as we might have other plans for the spare.
331 * For the reshape case we also need to 'freeze' sync_action so that
332 * no recovery happens until we have fully prepared for the reshape.
333 *
334 * We tell mdmon that the array is frozen by marking the 'metadata' name
335 * with a leading '-'. The previously told mdmon "Don't make this array
336 * read/write, leave it readonly". Now it means a more general "Don't
337 * reconfigure this array at all".
338 * As older versions of mdmon (which might run from initrd) don't understand
339 * this, we first check that the running mdmon is new enough.
bc77ed53
DW
340 */
341int block_monitor(char *container, const int freeze)
342{
bc77ed53
DW
343 struct mdstat_ent *ent, *e, *e2;
344 struct mdinfo *sra = NULL;
bc77ed53
DW
345 char buf[64];
346 int rv = 0;
347
7aa437c2
AK
348 if (check_mdmon_version(container))
349 return -1;
bc77ed53
DW
350
351 ent = mdstat_read(0, 0);
352 if (!ent) {
e7b84f9d 353 pr_err("failed to read /proc/mdstat while disabling mdmon\n");
bc77ed53
DW
354 return -1;
355 }
356
357 /* freeze container contents */
358 for (e = ent; e; e = e->next) {
359 if (!is_container_member(e, container))
360 continue;
361 sysfs_free(sra);
362 sra = sysfs_read(-1, e->devnum, GET_VERSION);
363 if (!sra) {
e7b84f9d
N
364 pr_err("failed to read sysfs for subarray%s\n",
365 to_subarray(e, container));
bc77ed53
DW
366 break;
367 }
368 /* can't reshape an array that we can't monitor */
369 if (sra->text_version[0] == '-')
370 break;
371
372 if (freeze && sysfs_freeze_array(sra) < 1)
373 break;
374 /* flag this array to not be modified by mdmon (close race with
375 * takeover in reshape case and spare reassignment in the
376 * auto-rebuild case)
377 */
2a093697 378 if (block_subarray(sra))
bc77ed53
DW
379 break;
380 ping_monitor(container);
381
382 /* check that we did not race with recovery */
383 if ((freeze &&
384 !sysfs_attribute_available(sra, NULL, "sync_action")) ||
385 (freeze &&
386 sysfs_attribute_available(sra, NULL, "sync_action") &&
387 sysfs_get_str(sra, NULL, "sync_action", buf, 20) > 0 &&
388 strcmp(buf, "frozen\n") == 0))
389 /* pass */;
885f9845
N
390 else {
391 unblock_subarray(sra, 0);
bc77ed53 392 break;
885f9845
N
393 }
394 /* Double check against races - there should be no spares
395 * or part-spares
396 */
397 sysfs_free(sra);
398 sra = sysfs_read(-1, e->devnum, GET_DEVS | GET_STATE);
399 if (sra && sra->array.spare_disks > 0) {
400 unblock_subarray(sra, freeze);
401 break;
402 }
bc77ed53
DW
403 }
404
405 if (e) {
e7b84f9d 406 pr_err("failed to freeze subarray%s\n",
bc77ed53
DW
407 to_subarray(e, container));
408
409 /* thaw the partially frozen container */
410 for (e2 = ent; e2 && e2 != e; e2 = e2->next) {
411 if (!is_container_member(e2, container))
412 continue;
413 sysfs_free(sra);
414 sra = sysfs_read(-1, e2->devnum, GET_VERSION);
415 if (unblock_subarray(sra, freeze))
e7b84f9d 416 pr_err("Failed to unfreeze %s\n", e2->dev);
bc77ed53
DW
417 }
418
419 ping_monitor(container); /* cleared frozen */
420 rv = -1;
421 }
422
423 sysfs_free(sra);
424 free_mdstat(ent);
bc77ed53
DW
425
426 return rv;
427}
428
429void unblock_monitor(char *container, const int unfreeze)
430{
431 struct mdstat_ent *ent, *e;
432 struct mdinfo *sra = NULL;
5158aef1 433 int to_ping = 0;
bc77ed53
DW
434
435 ent = mdstat_read(0, 0);
436 if (!ent) {
e7b84f9d 437 pr_err("failed to read /proc/mdstat while unblocking container\n");
bc77ed53
DW
438 return;
439 }
440
441 /* unfreeze container contents */
442 for (e = ent; e; e = e->next) {
443 if (!is_container_member(e, container))
444 continue;
445 sysfs_free(sra);
5158aef1 446 sra = sysfs_read(-1, e->devnum, GET_VERSION|GET_LEVEL);
29b59ca5
JS
447 if (!sra)
448 continue;
5158aef1
AK
449 if (sra->array.level > 0)
450 to_ping++;
bc77ed53 451 if (unblock_subarray(sra, unfreeze))
e7b84f9d 452 pr_err("Failed to unfreeze %s\n", e->dev);
bc77ed53 453 }
5158aef1
AK
454 if (to_ping)
455 ping_monitor(container);
bc77ed53
DW
456
457 sysfs_free(sra);
458 free_mdstat(ent);
459}
460
461
462
313a4a82
DW
463/* give the manager a chance to view the updated container state. This
464 * would naturally happen due to the manager noticing a change in
465 * /proc/mdstat; however, pinging encourages this detection to happen
466 * while an exclusive open() on the container is active
467 */
468int ping_manager(char *devname)
469{
470 int sfd = connect_monitor(devname);
471 struct metadata_update msg = { .len = -1 };
472 int err = 0;
473
474 if (sfd < 0)
475 return sfd;
476
477 err = send_message(sfd, &msg, 20);
478
479 /* check the reply */
480 if (!err && wait_reply(sfd, 20) != 0)
481 err = -1;
482
483 close(sfd);
484 return err;
485}
78340e26
AK
486
487/* using takeover operation for grow purposes, mdadm has to be sure
488 * that mdmon processes all updates, and if necessary it will be closed
489 * at takeover to raid0 operation
490 */
491void flush_mdmon(char *container)
492{
493 ping_manager(container);
494 ping_monitor(container);
495}