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