Tizen 2.1 base
[external/device-mapper.git] / daemons / cmirrord / local.c
1 /*
2  * Copyright (C) 2004-2009 Red Hat, Inc. All rights reserved.
3  *
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.
7  *
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
11  */
12 #include "logging.h"
13 #include "common.h"
14 #include "functions.h"
15 #include "link_mon.h"
16 #include "local.h"
17
18 #include <errno.h>
19 #include <sys/socket.h>
20 #include <linux/connector.h>
21 #include <linux/netlink.h>
22 #include <unistd.h>
23
24 #ifndef CN_IDX_DM
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
28 #endif
29
30 static int cn_fd;  /* Connector (netlink) socket fd */
31 static char recv_buf[2048];
32 static char send_buf[2048];
33
34
35 /* FIXME: merge this function with kernel_send_helper */
36 static int kernel_ack(uint32_t seq, int error)
37 {
38         int r;
39         struct nlmsghdr *nlh = (struct nlmsghdr *)send_buf;
40         struct cn_msg *msg = NLMSG_DATA(nlh);
41
42         if (error < 0) {
43                 LOG_ERROR("Programmer error: error codes must be positive");
44                 return -EINVAL;
45         }
46
47         memset(send_buf, 0, sizeof(send_buf));
48
49         nlh->nlmsg_seq = 0;
50         nlh->nlmsg_pid = getpid();
51         nlh->nlmsg_type = NLMSG_DONE;
52         nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct cn_msg));
53         nlh->nlmsg_flags = 0;
54
55         msg->len = 0;
56         msg->id.idx = CN_IDX_DM;
57         msg->id.val = CN_VAL_DM_USERSPACE_LOG;
58         msg->seq = seq;
59         msg->ack = error;
60
61         r = send(cn_fd, nlh, NLMSG_LENGTH(sizeof(struct cn_msg)), 0);
62         /* FIXME: do better error processing */
63         if (r <= 0)
64                 return -EBADE;
65
66         return 0;
67 }
68
69
70 /*
71  * kernel_recv
72  * @rq: the newly allocated request from kernel
73  *
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.
76  *
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.
79  *
80  * Returns: 0 on success, -EXXX on error
81  */
82 static int kernel_recv(struct clog_request **rq)
83 {
84         int r = 0;
85         ssize_t len;
86         char *foo;
87         struct cn_msg *msg;
88         struct dm_ulog_request *u_rq;
89         struct nlmsghdr *nlmsg_h;
90
91         *rq = NULL;
92         memset(recv_buf, 0, sizeof(recv_buf));
93
94         len = recv(cn_fd, recv_buf, sizeof(recv_buf), 0);
95         if (len < 0) {
96                 LOG_ERROR("Failed to recv message from kernel");
97                 r = -errno;
98                 goto fail;
99         }
100
101         nlmsg_h = (struct nlmsghdr *)recv_buf;
102         switch (nlmsg_h->nlmsg_type) {
103         case NLMSG_ERROR:
104                 LOG_ERROR("Unable to recv message from kernel: NLMSG_ERROR");
105                 r = -EBADE;
106                 goto fail;
107         case NLMSG_DONE:
108                 msg = (struct cn_msg *)NLMSG_DATA((struct nlmsghdr *)recv_buf);
109                 len -= (ssize_t)sizeof(struct nlmsghdr);
110
111                 if (len < (ssize_t)sizeof(struct cn_msg)) {
112                         LOG_ERROR("Incomplete request from kernel received");
113                         r = -EBADE;
114                         goto fail;
115                 }
116
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);
120                         r = -EBADE;
121                         goto fail;
122                 }
123
124                 if (!msg->len)
125                         LOG_ERROR("Zero length message received");
126
127                 len -= (ssize_t)sizeof(struct cn_msg);
128
129                 if (len < msg->len)
130                         LOG_ERROR("len = %zd, msg->len = %" PRIu16, len, msg->len);
131
132                 msg->data[msg->len] = '\0'; /* Cleaner way to ensure this? */
133                 u_rq = (struct dm_ulog_request *)msg->data;
134
135                 if (!u_rq->request_type) {
136                         LOG_DBG("Bad transmission, requesting resend [%u]",
137                                 msg->seq);
138                         r = -EAGAIN;
139
140                         if (kernel_ack(msg->seq, EAGAIN)) {
141                                 LOG_ERROR("Failed to NACK kernel transmission [%u]",
142                                           msg->seq);
143                                 r = -EBADE;
144                         }
145                 }
146
147                 /*
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.
153                  *
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?
157                  */
158                 foo = (char *)u_rq;
159                 foo -= (sizeof(struct clog_request) - sizeof(struct dm_ulog_request));
160                 *rq = (struct clog_request *) foo;
161
162                 /* Clear the wrapper container fields */
163                 memset(*rq, 0, (size_t)((char *)u_rq - (char *)(*rq)));
164                 break;
165         default:
166                 LOG_ERROR("Unknown nlmsg_type");
167                 r = -EBADE;
168         }
169
170 fail:
171         if (r)
172                 *rq = NULL;
173
174         return (r == -EAGAIN) ? 0 : r;
175 }
176
177 static int kernel_send_helper(void *data, uint16_t out_size)
178 {
179         int r;
180         struct nlmsghdr *nlh;
181         struct cn_msg *msg;
182
183         memset(send_buf, 0, sizeof(send_buf));
184
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;
191
192         msg = NLMSG_DATA(nlh);
193         memcpy(msg->data, data, out_size);
194         msg->len = out_size;
195         msg->id.idx = CN_IDX_DM;
196         msg->id.val = CN_VAL_DM_USERSPACE_LOG;
197         msg->seq = 0;
198
199         r = send(cn_fd, nlh, NLMSG_LENGTH(out_size + sizeof(struct cn_msg)), 0);
200         /* FIXME: do better error processing */
201         if (r <= 0)
202                 return -EBADE;
203
204         return 0;
205 }
206
207 /*
208  * do_local_work
209  *
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
213  * return an int.
214  *
215  * Returns: 0 on success, -EXXX on failure
216  */
217 static int do_local_work(void *data __attribute__((unused)))
218 {
219         int r;
220         struct clog_request *rq;
221         struct dm_ulog_request *u_rq = NULL;
222
223         r = kernel_recv(&rq);
224         if (r)
225                 return r;
226
227         if (!rq)
228                 return 0;
229
230         u_rq = &rq->u_rq;
231         LOG_DBG("[%s]  Request from kernel received: [%s/%u]",
232                 SHORT_UUID(u_rq->uuid), RQ_TYPE(u_rq->request_type),
233                 u_rq->seq);
234         switch (u_rq->request_type) {
235         case DM_ULOG_CTR:
236         case DM_ULOG_DTR:
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);
245                 if (r)
246                         LOG_DBG("Returning failed request to kernel [%s]",
247                                 RQ_TYPE(u_rq->request_type));
248                 r = kernel_send(u_rq);
249                 if (r)
250                         LOG_ERROR("Failed to respond to kernel [%s]",
251                                   RQ_TYPE(u_rq->request_type));
252                         
253                 break;
254         case DM_ULOG_RESUME:
255                 /*
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.
259                  */
260                 r = local_resume(u_rq);
261                 if (r) {
262                         LOG_DBG("Returning failed request to kernel [%s]",
263                                 RQ_TYPE(u_rq->request_type));
264                         r = kernel_send(u_rq);
265                         if (r)
266                                 LOG_ERROR("Failed to respond to kernel [%s]",
267                                           RQ_TYPE(u_rq->request_type));
268                         break;
269                 }
270                 /* ELSE, fall through */
271         case DM_ULOG_IS_CLEAN:
272         case DM_ULOG_FLUSH:
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);
279                 if (r) {
280                         u_rq->data_size = 0;
281                         u_rq->error = r;
282                         kernel_send(u_rq);
283                 }
284
285                 break;
286         case DM_ULOG_CLEAR_REGION:
287                 r = kernel_ack(u_rq->seq, 0);
288
289                 r = cluster_send(rq);
290                 if (r) {
291                         /*
292                          * FIXME: store error for delivery on flush
293                          *        This would allow us to optimize MARK_REGION
294                          *        too.
295                          */
296                 }
297
298                 break;
299         default:
300                 LOG_ERROR("Invalid log request received (%u), ignoring.",
301                           u_rq->request_type);
302
303                 return 0;
304         }
305
306         if (r && !u_rq->error)
307                 u_rq->error = r;
308
309         return r;
310 }
311
312 /*
313  * kernel_send
314  * @u_rq: result to pass back to kernel
315  *
316  * This function returns the u_rq structure
317  * (containing the results) to the kernel.
318  * It then frees the structure.
319  *
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.
324  *
325  * Returns: 0 on success, -EXXX on failure
326  */
327 int kernel_send(struct dm_ulog_request *u_rq)
328 {
329         int r;
330         uint16_t size;
331
332         if (!u_rq)
333                 return -EINVAL;
334
335         size = (uint16_t)(sizeof(struct dm_ulog_request) + u_rq->data_size);
336
337         if (!u_rq->data_size && !u_rq->error) {
338                 /* An ACK is all that is needed */
339
340                 /* FIXME: add ACK code */
341         } else if (size > DM_ULOG_REQUEST_SIZE) {
342                 /*
343                  * If we gotten here, we've already overrun
344                  * our allotted space somewhere.
345                  *
346                  * We must do something, because the kernel
347                  * is waiting for a response.
348                  */
349                 LOG_ERROR("Not enough space to respond to server");
350                 u_rq->error = -ENOSPC;
351                 size = sizeof(struct dm_ulog_request);
352         }
353
354         r = kernel_send_helper(u_rq, size);
355         if (r)
356                 LOG_ERROR("Failed to send msg to kernel.");
357
358         return r;
359 }
360
361 /*
362  * init_local
363  *
364  * Initialize kernel communication socket (netlink)
365  *
366  * Returns: 0 on success, values from common.h on failure
367  */
368 int init_local(void)
369 {
370         int r = 0;
371         unsigned opt;
372         struct sockaddr_nl addr;
373
374         cn_fd = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_CONNECTOR);
375         if (cn_fd < 0)
376                 return EXIT_KERNEL_SOCKET;
377
378         /* memset to fix valgrind complaint */
379         memset(&addr, 0, sizeof(struct sockaddr_nl));
380
381         addr.nl_family = AF_NETLINK;
382         addr.nl_groups = CN_IDX_DM;
383         addr.nl_pid = 0;
384
385         r = bind(cn_fd, (struct sockaddr *) &addr, sizeof(addr));
386         if (r < 0) {
387                 close(cn_fd);
388                 return EXIT_KERNEL_BIND;
389         }
390
391         opt = addr.nl_groups;
392         r = setsockopt(cn_fd, 270, NETLINK_ADD_MEMBERSHIP, &opt, sizeof(opt));
393         if (r) {
394                 close(cn_fd);
395                 return EXIT_KERNEL_SETSOCKOPT;
396         }
397
398         /*
399         r = fcntl(cn_fd, F_SETFL, FNDELAY);
400         */
401
402         links_register(cn_fd, "local", do_local_work, NULL);
403
404         return 0;
405 }
406
407 /*
408  * cleanup_local
409  *
410  * Clean up before exiting
411  */
412 void cleanup_local(void)
413 {
414         links_unregister(cn_fd);
415         close(cn_fd);
416 }