2 * Copyright (C) 2004-2009 Red Hat, Inc. All rights reserved.
4 * This copyrighted material is made available to anyone wishing to use,
5 * modify, copy, or redistribute it subject to the terms and conditions
6 * of the GNU Lesser General Public License v.2.1.
8 * You should have received a copy of the GNU Lesser General Public License
9 * along with this program; if not, write to the Free Software Foundation,
10 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
14 #include "functions.h"
19 #include <sys/socket.h>
20 #include <linux/connector.h>
21 #include <linux/netlink.h>
25 /* Kernel 2.6.31 is required to run this code */
26 #define CN_IDX_DM 0x7 /* Device Mapper */
27 #define CN_VAL_DM_USERSPACE_LOG 0x1
30 static int cn_fd; /* Connector (netlink) socket fd */
31 static char recv_buf[2048];
32 static char send_buf[2048];
35 /* FIXME: merge this function with kernel_send_helper */
36 static int kernel_ack(uint32_t seq, int error)
39 struct nlmsghdr *nlh = (struct nlmsghdr *)send_buf;
40 struct cn_msg *msg = NLMSG_DATA(nlh);
43 LOG_ERROR("Programmer error: error codes must be positive");
47 memset(send_buf, 0, sizeof(send_buf));
50 nlh->nlmsg_pid = getpid();
51 nlh->nlmsg_type = NLMSG_DONE;
52 nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct cn_msg));
56 msg->id.idx = CN_IDX_DM;
57 msg->id.val = CN_VAL_DM_USERSPACE_LOG;
61 r = send(cn_fd, nlh, NLMSG_LENGTH(sizeof(struct cn_msg)), 0);
62 /* FIXME: do better error processing */
72 * @rq: the newly allocated request from kernel
74 * Read requests from the kernel and allocate space for the new request.
75 * If there is no request from the kernel, *rq is NULL.
77 * This function is not thread safe due to returned stack pointer. In fact,
78 * the returned pointer must not be in-use when this function is called again.
80 * Returns: 0 on success, -EXXX on error
82 static int kernel_recv(struct clog_request **rq)
88 struct dm_ulog_request *u_rq;
89 struct nlmsghdr *nlmsg_h;
92 memset(recv_buf, 0, sizeof(recv_buf));
94 len = recv(cn_fd, recv_buf, sizeof(recv_buf), 0);
96 LOG_ERROR("Failed to recv message from kernel");
101 nlmsg_h = (struct nlmsghdr *)recv_buf;
102 switch (nlmsg_h->nlmsg_type) {
104 LOG_ERROR("Unable to recv message from kernel: NLMSG_ERROR");
108 msg = (struct cn_msg *)NLMSG_DATA((struct nlmsghdr *)recv_buf);
109 len -= (ssize_t)sizeof(struct nlmsghdr);
111 if (len < (ssize_t)sizeof(struct cn_msg)) {
112 LOG_ERROR("Incomplete request from kernel received");
117 if (msg->len > DM_ULOG_REQUEST_SIZE) {
118 LOG_ERROR("Not enough space to receive kernel request (%d/%d)",
119 msg->len, DM_ULOG_REQUEST_SIZE);
125 LOG_ERROR("Zero length message received");
127 len -= (ssize_t)sizeof(struct cn_msg);
130 LOG_ERROR("len = %zd, msg->len = %" PRIu16, len, msg->len);
132 msg->data[msg->len] = '\0'; /* Cleaner way to ensure this? */
133 u_rq = (struct dm_ulog_request *)msg->data;
135 if (!u_rq->request_type) {
136 LOG_DBG("Bad transmission, requesting resend [%u]",
140 if (kernel_ack(msg->seq, EAGAIN)) {
141 LOG_ERROR("Failed to NACK kernel transmission [%u]",
148 * Now we've got sizeof(struct cn_msg) + sizeof(struct nlmsghdr)
149 * worth of space that precede the request structure from the
150 * kernel. Since that space isn't going to be used again, we
151 * can take it for our purposes; rather than allocating a whole
152 * new structure and doing a memcpy.
154 * We should really make sure 'clog_request' doesn't grow
155 * beyond what is available to us, but we need only check it
156 * once... perhaps at compile time?
159 foo -= (sizeof(struct clog_request) - sizeof(struct dm_ulog_request));
160 *rq = (struct clog_request *) foo;
162 /* Clear the wrapper container fields */
163 memset(*rq, 0, (size_t)((char *)u_rq - (char *)(*rq)));
166 LOG_ERROR("Unknown nlmsg_type");
174 return (r == -EAGAIN) ? 0 : r;
177 static int kernel_send_helper(void *data, uint16_t out_size)
180 struct nlmsghdr *nlh;
183 memset(send_buf, 0, sizeof(send_buf));
185 nlh = (struct nlmsghdr *)send_buf;
186 nlh->nlmsg_seq = 0; /* FIXME: Is this used? */
187 nlh->nlmsg_pid = getpid();
188 nlh->nlmsg_type = NLMSG_DONE;
189 nlh->nlmsg_len = NLMSG_LENGTH(out_size + sizeof(struct cn_msg));
190 nlh->nlmsg_flags = 0;
192 msg = NLMSG_DATA(nlh);
193 memcpy(msg->data, data, out_size);
195 msg->id.idx = CN_IDX_DM;
196 msg->id.val = CN_VAL_DM_USERSPACE_LOG;
199 r = send(cn_fd, nlh, NLMSG_LENGTH(out_size + sizeof(struct cn_msg)), 0);
200 /* FIXME: do better error processing */
210 * Any processing errors are placed in the 'rq'
211 * structure to be reported back to the kernel.
212 * It may be pointless for this function to
215 * Returns: 0 on success, -EXXX on failure
217 static int do_local_work(void *data __attribute__((unused)))
220 struct clog_request *rq;
221 struct dm_ulog_request *u_rq = NULL;
223 r = kernel_recv(&rq);
231 LOG_DBG("[%s] Request from kernel received: [%s/%u]",
232 SHORT_UUID(u_rq->uuid), RQ_TYPE(u_rq->request_type),
234 switch (u_rq->request_type) {
237 case DM_ULOG_GET_REGION_SIZE:
238 case DM_ULOG_IN_SYNC:
239 case DM_ULOG_GET_SYNC_COUNT:
240 case DM_ULOG_STATUS_INFO:
241 case DM_ULOG_STATUS_TABLE:
242 case DM_ULOG_PRESUSPEND:
243 /* We do not specify ourselves as server here */
244 r = do_request(rq, 0);
246 LOG_DBG("Returning failed request to kernel [%s]",
247 RQ_TYPE(u_rq->request_type));
248 r = kernel_send(u_rq);
250 LOG_ERROR("Failed to respond to kernel [%s]",
251 RQ_TYPE(u_rq->request_type));
256 * Resume is a special case that requires a local
257 * component to join the CPG, and a cluster component
258 * to handle the request.
260 r = local_resume(u_rq);
262 LOG_DBG("Returning failed request to kernel [%s]",
263 RQ_TYPE(u_rq->request_type));
264 r = kernel_send(u_rq);
266 LOG_ERROR("Failed to respond to kernel [%s]",
267 RQ_TYPE(u_rq->request_type));
270 /* ELSE, fall through */
271 case DM_ULOG_IS_CLEAN:
273 case DM_ULOG_MARK_REGION:
274 case DM_ULOG_GET_RESYNC_WORK:
275 case DM_ULOG_SET_REGION_SYNC:
276 case DM_ULOG_IS_REMOTE_RECOVERING:
277 case DM_ULOG_POSTSUSPEND:
278 r = cluster_send(rq);
286 case DM_ULOG_CLEAR_REGION:
287 r = kernel_ack(u_rq->seq, 0);
289 r = cluster_send(rq);
292 * FIXME: store error for delivery on flush
293 * This would allow us to optimize MARK_REGION
300 LOG_ERROR("Invalid log request received (%u), ignoring.",
306 if (r && !u_rq->error)
314 * @u_rq: result to pass back to kernel
316 * This function returns the u_rq structure
317 * (containing the results) to the kernel.
318 * It then frees the structure.
320 * WARNING: should the structure be freed if
321 * there is an error? I vote 'yes'. If the
322 * kernel doesn't get the response, it should
323 * resend the request.
325 * Returns: 0 on success, -EXXX on failure
327 int kernel_send(struct dm_ulog_request *u_rq)
335 size = (uint16_t)(sizeof(struct dm_ulog_request) + u_rq->data_size);
337 if (!u_rq->data_size && !u_rq->error) {
338 /* An ACK is all that is needed */
340 /* FIXME: add ACK code */
341 } else if (size > DM_ULOG_REQUEST_SIZE) {
343 * If we gotten here, we've already overrun
344 * our allotted space somewhere.
346 * We must do something, because the kernel
347 * is waiting for a response.
349 LOG_ERROR("Not enough space to respond to server");
350 u_rq->error = -ENOSPC;
351 size = sizeof(struct dm_ulog_request);
354 r = kernel_send_helper(u_rq, size);
356 LOG_ERROR("Failed to send msg to kernel.");
364 * Initialize kernel communication socket (netlink)
366 * Returns: 0 on success, values from common.h on failure
372 struct sockaddr_nl addr;
374 cn_fd = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_CONNECTOR);
376 return EXIT_KERNEL_SOCKET;
378 /* memset to fix valgrind complaint */
379 memset(&addr, 0, sizeof(struct sockaddr_nl));
381 addr.nl_family = AF_NETLINK;
382 addr.nl_groups = CN_IDX_DM;
385 r = bind(cn_fd, (struct sockaddr *) &addr, sizeof(addr));
388 return EXIT_KERNEL_BIND;
391 opt = addr.nl_groups;
392 r = setsockopt(cn_fd, 270, NETLINK_ADD_MEMBERSHIP, &opt, sizeof(opt));
395 return EXIT_KERNEL_SETSOCKOPT;
399 r = fcntl(cn_fd, F_SETFL, FNDELAY);
402 links_register(cn_fd, "local", do_local_work, NULL);
410 * Clean up before exiting
412 void cleanup_local(void)
414 links_unregister(cn_fd);