]> git.neil.brown.name Git - mdadm.git/blob - msg.c
mdadm: add man page for --add-journal
[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 int send_message(int fd, struct metadata_update *msg, int tmo)
82 {
83         __s32 len = msg->len;
84         int rv;
85
86         rv = send_buf(fd, &start_magic, 4, tmo);
87         rv = rv ?: send_buf(fd, &len, 4, tmo);
88         if (len > 0)
89                 rv = rv ?: send_buf(fd, msg->buf, msg->len, tmo);
90         rv = send_buf(fd, &end_magic, 4, tmo);
91
92         return rv;
93 }
94
95 int receive_message(int fd, struct metadata_update *msg, int tmo)
96 {
97         __u32 magic;
98         __s32 len;
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)
106                 return -1;
107         if (len > 0) {
108                 msg->buf = xmalloc(len);
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;
123 }
124
125 int ack(int fd, int tmo)
126 {
127         struct metadata_update msg = { .len = 0 };
128
129         return send_message(fd, &msg, tmo);
130 }
131
132 int wait_reply(int fd, int tmo)
133 {
134         struct metadata_update msg;
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;
144 }
145
146 int connect_monitor(char *devname)
147 {
148         char path[100];
149         int sfd;
150         long fl;
151         struct sockaddr_un addr;
152         int pos;
153         char *c;
154
155         pos = sprintf(path, "%s/", MDMON_DIR);
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");
166
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);
173         if (connect(sfd, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
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
185 int fping_monitor(int sfd)
186 {
187         int err = 0;
188
189         if (sfd < 0)
190                 return sfd;
191
192         /* try to ping existing socket */
193         if (ack(sfd, 20) != 0)
194                 err = -1;
195
196         /* check the reply */
197         if (!err && wait_reply(sfd, 20) != 0)
198                 err = -1;
199
200         return err;
201 }
202
203 /* give the monitor a chance to update the metadata */
204 int ping_monitor(char *devname)
205 {
206         int sfd = connect_monitor(devname);
207         int err;
208
209         if (sfd >= 0) {
210                 err = fping_monitor(sfd);
211                 close(sfd);
212         } else
213                 err = -1;
214
215         return err;
216 }
217
218 static 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
240 int unblock_subarray(struct mdinfo *sra, const int unfreeze)
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
260 int 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 }
272
273 /* check mdmon version if it supports
274  * array blocking mechanism
275  */
276 int check_mdmon_version(char *container)
277 {
278         char *version = NULL;
279
280         if (!mdmon_running(container)) {
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) {
295                         pr_err("mdmon instance for %s cannot be disabled\n",
296                                container);
297                         return -1;
298                 }
299         }
300
301         return 0;
302 }
303
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
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.
322  */
323 int block_monitor(char *container, const int freeze)
324 {
325         struct mdstat_ent *ent, *e, *e2;
326         struct mdinfo *sra = NULL;
327         char buf[64];
328         int rv = 0;
329
330         if (check_mdmon_version(container))
331                 return -1;
332
333         ent = mdstat_read(0, 0);
334         if (!ent) {
335                 pr_err("failed to read /proc/mdstat while disabling mdmon\n");
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);
344                 sra = sysfs_read(-1, e->devnm, GET_VERSION);
345                 if (!sra) {
346                         pr_err("failed to read sysfs for subarray%s\n",
347                                to_subarray(e, container));
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                  */
360                 if (block_subarray(sra))
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 */;
372                 else {
373                         unblock_subarray(sra, 0);
374                         break;
375                 }
376                 /* Double check against races - there should be no spares
377                  * or part-spares
378                  */
379                 sysfs_free(sra);
380                 sra = sysfs_read(-1, e->devnm, GET_DEVS | GET_STATE);
381                 if (sra && sra->array.spare_disks > 0) {
382                         unblock_subarray(sra, freeze);
383                         break;
384                 }
385         }
386
387         if (e) {
388                 pr_err("failed to freeze subarray%s\n",
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);
396                         sra = sysfs_read(-1, e2->devnm, GET_VERSION);
397                         if (unblock_subarray(sra, freeze))
398                                 pr_err("Failed to unfreeze %s\n", e2->devnm);
399                 }
400
401                 ping_monitor(container); /* cleared frozen */
402                 rv = -1;
403         }
404
405         sysfs_free(sra);
406         free_mdstat(ent);
407
408         return rv;
409 }
410
411 void unblock_monitor(char *container, const int unfreeze)
412 {
413         struct mdstat_ent *ent, *e;
414         struct mdinfo *sra = NULL;
415         int to_ping = 0;
416
417         ent = mdstat_read(0, 0);
418         if (!ent) {
419                 pr_err("failed to read /proc/mdstat while unblocking container\n");
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);
428                 sra = sysfs_read(-1, e->devnm, GET_VERSION|GET_LEVEL);
429                 if (!sra)
430                         continue;
431                 if (sra->array.level > 0)
432                         to_ping++;
433                 if (unblock_subarray(sra, unfreeze))
434                         pr_err("Failed to unfreeze %s\n", e->devnm);
435         }
436         if (to_ping)
437                 ping_monitor(container);
438
439         sysfs_free(sra);
440         free_mdstat(ent);
441 }
442
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  */
448 int 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 }
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   */
471 void flush_mdmon(char *container)
472 {
473         ping_manager(container);
474         ping_monitor(container);
475 }