]>
git.ipfire.org Git - thirdparty/mdadm.git/blob - msg.c
2 * Copyright (C) 2008 Intel Corporation
4 * mdmon socket / message handling
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.
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
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.
28 #include <sys/types.h>
29 #include <sys/socket.h>
34 static const __u32 start_magic
= 0x5a5aa5a5;
35 static const __u32 end_magic
= 0xa5a55a5a;
37 static int send_buf(int fd
, const void* buf
, int len
, int tmo
)
41 struct timeval timeout
= {tmo
, 0};
42 struct timeval
*ptmo
= tmo
? &timeout
: NULL
;
47 rv
= select(fd
+1, NULL
, &set
, NULL
, ptmo
);
50 rv
= write(fd
, buf
, len
);
59 static int recv_buf(int fd
, void* buf
, int len
, int tmo
)
63 struct timeval timeout
= {tmo
, 0};
64 struct timeval
*ptmo
= tmo
? &timeout
: NULL
;
69 rv
= select(fd
+1, &set
, NULL
, NULL
, ptmo
);
72 rv
= read(fd
, buf
, len
);
81 int send_message(int fd
, struct metadata_update
*msg
, int tmo
)
86 rv
= send_buf(fd
, &start_magic
, 4, tmo
);
87 rv
= rv
?: send_buf(fd
, &len
, 4, tmo
);
89 rv
= rv
?: send_buf(fd
, msg
->buf
, msg
->len
, tmo
);
90 rv
= send_buf(fd
, &end_magic
, 4, tmo
);
95 int receive_message(int fd
, struct metadata_update
*msg
, int tmo
)
101 rv
= recv_buf(fd
, &magic
, 4, tmo
);
102 if (rv
< 0 || magic
!= start_magic
)
104 rv
= recv_buf(fd
, &len
, 4, tmo
);
105 if (rv
< 0 || len
> MSG_MAX_LEN
)
108 msg
->buf
= xmalloc(len
);
109 rv
= recv_buf(fd
, msg
->buf
, len
, tmo
);
116 rv
= recv_buf(fd
, &magic
, 4, tmo
);
117 if (rv
< 0 || magic
!= end_magic
) {
125 int ack(int fd
, int tmo
)
127 struct metadata_update msg
= { .len
= 0 };
129 return send_message(fd
, &msg
, tmo
);
132 int wait_reply(int fd
, int tmo
)
134 struct metadata_update msg
;
135 int err
= receive_message(fd
, &msg
, tmo
);
137 /* mdmon sent extra data, but caller only cares that we got a
140 if (err
== 0 && msg
.len
> 0)
146 int connect_monitor(char *devname
)
151 struct sockaddr_un addr
;
155 pos
= sprintf(path
, "%s/", MDMON_DIR
);
156 if (is_subarray(devname
)) {
158 c
= strchr(devname
, '/');
161 snprintf(&path
[pos
], c
- devname
+ 1, "%s", devname
);
164 pos
+= sprintf(&path
[pos
], "%s", devname
);
165 sprintf(&path
[pos
], ".sock");
167 sfd
= socket(PF_LOCAL
, SOCK_STREAM
, 0);
171 addr
.sun_family
= PF_LOCAL
;
172 strcpy(addr
.sun_path
, path
);
173 if (connect(sfd
, (struct sockaddr
*)&addr
, sizeof(addr
)) < 0) {
174 pr_err("Error connecting monitor with %s: %s\n",
175 addr
.sun_path
, strerror(errno
));
180 fl
= fcntl(sfd
, F_GETFL
, 0);
182 fcntl(sfd
, F_SETFL
, fl
);
187 int fping_monitor(int sfd
)
194 /* try to ping existing socket */
195 if (ack(sfd
, 20) != 0)
198 /* check the reply */
199 if (!err
&& wait_reply(sfd
, 20) != 0)
205 /* give the monitor a chance to update the metadata */
206 int ping_monitor(char *devname
)
208 int sfd
= connect_monitor(devname
);
212 err
= fping_monitor(sfd
);
220 static char *ping_monitor_version(char *devname
)
222 int sfd
= connect_monitor(devname
);
223 struct metadata_update msg
;
229 if (ack(sfd
, 20) != 0)
232 if (!err
&& receive_message(sfd
, &msg
, 20) != 0)
237 if (err
|| !msg
.len
|| !msg
.buf
)
242 int unblock_subarray(struct mdinfo
*sra
, const int unfreeze
)
248 sprintf(buf
, "external:%s\n", sra
->text_version
);
254 sysfs_set_str(sra
, NULL
, "metadata_version", buf
) ||
256 sysfs_attribute_available(sra
, NULL
, "sync_action") &&
257 sysfs_set_str(sra
, NULL
, "sync_action", "idle")))
262 int block_subarray(struct mdinfo
*sra
)
267 sprintf(buf
, "external:%s\n", sra
->text_version
);
269 if (sysfs_set_str(sra
, NULL
, "metadata_version", buf
))
275 /* check mdmon version if it supports
276 * array blocking mechanism
278 int check_mdmon_version(char *container
)
280 char *version
= NULL
;
282 if (!mdmon_running(container
)) {
283 /* if mdmon is not active we assume that any instance that is
284 * later started will match the current mdadm version, if this
285 * assumption is violated we may inadvertantly rebuild an array
286 * that was meant for reshape, or start rebuild on a spare that
287 * was to be moved to another container
293 version
= ping_monitor_version(container
);
294 ver
= version
? mdadm_version(version
) : -1;
297 pr_err("mdmon instance for %s cannot be disabled\n",
307 * block_monitor - prevent mdmon spare assignment
308 * @container - container to block
309 * @freeze - flag to additionally freeze sync_action
311 * This is used by the reshape code to freeze the container, and the
312 * auto-rebuild implementation to atomically move spares.
313 * In both cases we need to stop mdmon from assigning spares to replace
314 * failed devices as we might have other plans for the spare.
315 * For the reshape case we also need to 'freeze' sync_action so that
316 * no recovery happens until we have fully prepared for the reshape.
318 * We tell mdmon that the array is frozen by marking the 'metadata' name
319 * with a leading '-'. The previously told mdmon "Don't make this array
320 * read/write, leave it readonly". Now it means a more general "Don't
321 * reconfigure this array at all".
322 * As older versions of mdmon (which might run from initrd) don't understand
323 * this, we first check that the running mdmon is new enough.
325 int block_monitor(char *container
, const int freeze
)
327 struct mdstat_ent
*ent
, *e
, *e2
;
328 struct mdinfo
*sra
= NULL
;
332 if (check_mdmon_version(container
))
335 ent
= mdstat_read(0, 0);
337 pr_err("failed to read /proc/mdstat while disabling mdmon\n");
341 /* freeze container contents */
342 for (e
= ent
; e
; e
= e
->next
) {
343 if (!is_container_member(e
, container
))
346 sra
= sysfs_read(-1, e
->devnm
, GET_VERSION
);
348 pr_err("failed to read sysfs for subarray%s\n",
349 to_subarray(e
, container
));
352 /* can't reshape an array that we can't monitor */
353 if (sra
->text_version
[0] == '-')
356 if (freeze
&& sysfs_freeze_array(sra
) < 1)
358 /* flag this array to not be modified by mdmon (close race with
359 * takeover in reshape case and spare reassignment in the
362 if (block_subarray(sra
))
364 ping_monitor(container
);
366 /* check that we did not race with recovery */
368 !sysfs_attribute_available(sra
, NULL
, "sync_action")) ||
370 sysfs_attribute_available(sra
, NULL
, "sync_action") &&
371 sysfs_get_str(sra
, NULL
, "sync_action", buf
, 20) > 0 &&
372 strcmp(buf
, "frozen\n") == 0))
375 unblock_subarray(sra
, 0);
378 /* Double check against races - there should be no spares
382 sra
= sysfs_read(-1, e
->devnm
, GET_DEVS
| GET_STATE
);
383 if (sra
&& sra
->array
.spare_disks
> 0) {
384 unblock_subarray(sra
, freeze
);
390 pr_err("failed to freeze subarray%s\n",
391 to_subarray(e
, container
));
393 /* thaw the partially frozen container */
394 for (e2
= ent
; e2
&& e2
!= e
; e2
= e2
->next
) {
395 if (!is_container_member(e2
, container
))
398 sra
= sysfs_read(-1, e2
->devnm
, GET_VERSION
);
399 if (unblock_subarray(sra
, freeze
))
400 pr_err("Failed to unfreeze %s\n", e2
->devnm
);
403 ping_monitor(container
); /* cleared frozen */
413 void unblock_monitor(char *container
, const int unfreeze
)
415 struct mdstat_ent
*ent
, *e
;
416 struct mdinfo
*sra
= NULL
;
419 ent
= mdstat_read(0, 0);
421 pr_err("failed to read /proc/mdstat while unblocking container\n");
425 /* unfreeze container contents */
426 for (e
= ent
; e
; e
= e
->next
) {
427 if (!is_container_member(e
, container
))
430 sra
= sysfs_read(-1, e
->devnm
, GET_VERSION
|GET_LEVEL
);
433 if (sra
->array
.level
> 0)
435 if (unblock_subarray(sra
, unfreeze
))
436 pr_err("Failed to unfreeze %s\n", e
->devnm
);
439 ping_monitor(container
);
445 /* give the manager a chance to view the updated container state. This
446 * would naturally happen due to the manager noticing a change in
447 * /proc/mdstat; however, pinging encourages this detection to happen
448 * while an exclusive open() on the container is active
450 int ping_manager(char *devname
)
452 int sfd
= connect_monitor(devname
);
453 struct metadata_update msg
= { .len
= -1 };
459 err
= send_message(sfd
, &msg
, 20);
461 /* check the reply */
462 if (!err
&& wait_reply(sfd
, 20) != 0)
469 /* using takeover operation for grow purposes, mdadm has to be sure
470 * that mdmon processes all updates, and if necessary it will be closed
471 * at takeover to raid0 operation
473 void flush_mdmon(char *container
)
475 ping_manager(container
);
476 ping_monitor(container
);