]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - fs/dlm/lowcomms.c
5c33233bc2d59543633113cb5c1a31d042a9e133
[linux-2.6-omap-h63xx.git] / fs / dlm / lowcomms.c
1 /******************************************************************************
2 *******************************************************************************
3 **
4 **  Copyright (C) Sistina Software, Inc.  1997-2003  All rights reserved.
5 **  Copyright (C) 2004-2007 Red Hat, Inc.  All rights reserved.
6 **
7 **  This copyrighted material is made available to anyone wishing to use,
8 **  modify, copy, or redistribute it subject to the terms and conditions
9 **  of the GNU General Public License v.2.
10 **
11 *******************************************************************************
12 ******************************************************************************/
13
14 /*
15  * lowcomms.c
16  *
17  * This is the "low-level" comms layer.
18  *
19  * It is responsible for sending/receiving messages
20  * from other nodes in the cluster.
21  *
22  * Cluster nodes are referred to by their nodeids. nodeids are
23  * simply 32 bit numbers to the locking module - if they need to
24  * be expanded for the cluster infrastructure then that is it's
25  * responsibility. It is this layer's
26  * responsibility to resolve these into IP address or
27  * whatever it needs for inter-node communication.
28  *
29  * The comms level is two kernel threads that deal mainly with
30  * the receiving of messages from other nodes and passing them
31  * up to the mid-level comms layer (which understands the
32  * message format) for execution by the locking core, and
33  * a send thread which does all the setting up of connections
34  * to remote nodes and the sending of data. Threads are not allowed
35  * to send their own data because it may cause them to wait in times
36  * of high load. Also, this way, the sending thread can collect together
37  * messages bound for one node and send them in one block.
38  *
39  * lowcomms will choose to use wither TCP or SCTP as its transport layer
40  * depending on the configuration variable 'protocol'. This should be set
41  * to 0 (default) for TCP or 1 for SCTP. It shouldbe configured using a
42  * cluster-wide mechanism as it must be the same on all nodes of the cluster
43  * for the DLM to function.
44  *
45  */
46
47 #include <asm/ioctls.h>
48 #include <net/sock.h>
49 #include <net/tcp.h>
50 #include <linux/pagemap.h>
51 #include <linux/idr.h>
52 #include <linux/file.h>
53 #include <linux/sctp.h>
54 #include <net/sctp/user.h>
55
56 #include "dlm_internal.h"
57 #include "lowcomms.h"
58 #include "midcomms.h"
59 #include "config.h"
60
61 #define NEEDED_RMEM (4*1024*1024)
62
63 struct cbuf {
64         unsigned int base;
65         unsigned int len;
66         unsigned int mask;
67 };
68
69 static void cbuf_add(struct cbuf *cb, int n)
70 {
71         cb->len += n;
72 }
73
74 static int cbuf_data(struct cbuf *cb)
75 {
76         return ((cb->base + cb->len) & cb->mask);
77 }
78
79 static void cbuf_init(struct cbuf *cb, int size)
80 {
81         cb->base = cb->len = 0;
82         cb->mask = size-1;
83 }
84
85 static void cbuf_eat(struct cbuf *cb, int n)
86 {
87         cb->len  -= n;
88         cb->base += n;
89         cb->base &= cb->mask;
90 }
91
92 static bool cbuf_empty(struct cbuf *cb)
93 {
94         return cb->len == 0;
95 }
96
97 struct connection {
98         struct socket *sock;    /* NULL if not connected */
99         uint32_t nodeid;        /* So we know who we are in the list */
100         struct mutex sock_mutex;
101         unsigned long flags;
102 #define CF_READ_PENDING 1
103 #define CF_WRITE_PENDING 2
104 #define CF_CONNECT_PENDING 3
105 #define CF_INIT_PENDING 4
106 #define CF_IS_OTHERCON 5
107         struct list_head writequeue;  /* List of outgoing writequeue_entries */
108         spinlock_t writequeue_lock;
109         int (*rx_action) (struct connection *); /* What to do when active */
110         void (*connect_action) (struct connection *);   /* What to do to connect */
111         struct page *rx_page;
112         struct cbuf cb;
113         int retries;
114 #define MAX_CONNECT_RETRIES 3
115         int sctp_assoc;
116         struct connection *othercon;
117         struct work_struct rwork; /* Receive workqueue */
118         struct work_struct swork; /* Send workqueue */
119 };
120 #define sock2con(x) ((struct connection *)(x)->sk_user_data)
121
122 /* An entry waiting to be sent */
123 struct writequeue_entry {
124         struct list_head list;
125         struct page *page;
126         int offset;
127         int len;
128         int end;
129         int users;
130         struct connection *con;
131 };
132
133 static struct sockaddr_storage *dlm_local_addr[DLM_MAX_ADDR_COUNT];
134 static int dlm_local_count;
135
136 /* Work queues */
137 static struct workqueue_struct *recv_workqueue;
138 static struct workqueue_struct *send_workqueue;
139
140 static DEFINE_IDR(connections_idr);
141 static DECLARE_MUTEX(connections_lock);
142 static int max_nodeid;
143 static struct kmem_cache *con_cache;
144
145 static void process_recv_sockets(struct work_struct *work);
146 static void process_send_sockets(struct work_struct *work);
147
148 /*
149  * If 'allocation' is zero then we don't attempt to create a new
150  * connection structure for this node.
151  */
152 static struct connection *__nodeid2con(int nodeid, gfp_t alloc)
153 {
154         struct connection *con = NULL;
155         int r;
156         int n;
157
158         con = idr_find(&connections_idr, nodeid);
159         if (con || !alloc)
160                 return con;
161
162         r = idr_pre_get(&connections_idr, alloc);
163         if (!r)
164                 return NULL;
165
166         con = kmem_cache_zalloc(con_cache, alloc);
167         if (!con)
168                 return NULL;
169
170         r = idr_get_new_above(&connections_idr, con, nodeid, &n);
171         if (r) {
172                 kmem_cache_free(con_cache, con);
173                 return NULL;
174         }
175
176         if (n != nodeid) {
177                 idr_remove(&connections_idr, n);
178                 kmem_cache_free(con_cache, con);
179                 return NULL;
180         }
181
182         con->nodeid = nodeid;
183         mutex_init(&con->sock_mutex);
184         INIT_LIST_HEAD(&con->writequeue);
185         spin_lock_init(&con->writequeue_lock);
186         INIT_WORK(&con->swork, process_send_sockets);
187         INIT_WORK(&con->rwork, process_recv_sockets);
188
189         /* Setup action pointers for child sockets */
190         if (con->nodeid) {
191                 struct connection *zerocon = idr_find(&connections_idr, 0);
192
193                 con->connect_action = zerocon->connect_action;
194                 if (!con->rx_action)
195                         con->rx_action = zerocon->rx_action;
196         }
197
198         if (nodeid > max_nodeid)
199                 max_nodeid = nodeid;
200
201         return con;
202 }
203
204 static struct connection *nodeid2con(int nodeid, gfp_t allocation)
205 {
206         struct connection *con;
207
208         down(&connections_lock);
209         con = __nodeid2con(nodeid, allocation);
210         up(&connections_lock);
211
212         return con;
213 }
214
215 /* This is a bit drastic, but only called when things go wrong */
216 static struct connection *assoc2con(int assoc_id)
217 {
218         int i;
219         struct connection *con;
220
221         down(&connections_lock);
222         for (i=0; i<=max_nodeid; i++) {
223                 con = __nodeid2con(i, 0);
224                 if (con && con->sctp_assoc == assoc_id) {
225                         up(&connections_lock);
226                         return con;
227                 }
228         }
229         up(&connections_lock);
230         return NULL;
231 }
232
233 static int nodeid_to_addr(int nodeid, struct sockaddr *retaddr)
234 {
235         struct sockaddr_storage addr;
236         int error;
237
238         if (!dlm_local_count)
239                 return -1;
240
241         error = dlm_nodeid_to_addr(nodeid, &addr);
242         if (error)
243                 return error;
244
245         if (dlm_local_addr[0]->ss_family == AF_INET) {
246                 struct sockaddr_in *in4  = (struct sockaddr_in *) &addr;
247                 struct sockaddr_in *ret4 = (struct sockaddr_in *) retaddr;
248                 ret4->sin_addr.s_addr = in4->sin_addr.s_addr;
249         } else {
250                 struct sockaddr_in6 *in6  = (struct sockaddr_in6 *) &addr;
251                 struct sockaddr_in6 *ret6 = (struct sockaddr_in6 *) retaddr;
252                 memcpy(&ret6->sin6_addr, &in6->sin6_addr,
253                        sizeof(in6->sin6_addr));
254         }
255
256         return 0;
257 }
258
259 /* Data available on socket or listen socket received a connect */
260 static void lowcomms_data_ready(struct sock *sk, int count_unused)
261 {
262         struct connection *con = sock2con(sk);
263         if (!test_and_set_bit(CF_READ_PENDING, &con->flags))
264                 queue_work(recv_workqueue, &con->rwork);
265 }
266
267 static void lowcomms_write_space(struct sock *sk)
268 {
269         struct connection *con = sock2con(sk);
270
271         if (!test_and_set_bit(CF_WRITE_PENDING, &con->flags))
272                 queue_work(send_workqueue, &con->swork);
273 }
274
275 static inline void lowcomms_connect_sock(struct connection *con)
276 {
277         if (!test_and_set_bit(CF_CONNECT_PENDING, &con->flags))
278                 queue_work(send_workqueue, &con->swork);
279 }
280
281 static void lowcomms_state_change(struct sock *sk)
282 {
283         if (sk->sk_state == TCP_ESTABLISHED)
284                 lowcomms_write_space(sk);
285 }
286
287 /* Make a socket active */
288 static int add_sock(struct socket *sock, struct connection *con)
289 {
290         con->sock = sock;
291
292         /* Install a data_ready callback */
293         con->sock->sk->sk_data_ready = lowcomms_data_ready;
294         con->sock->sk->sk_write_space = lowcomms_write_space;
295         con->sock->sk->sk_state_change = lowcomms_state_change;
296         con->sock->sk->sk_user_data = con;
297         return 0;
298 }
299
300 /* Add the port number to an IPv6 or 4 sockaddr and return the address
301    length */
302 static void make_sockaddr(struct sockaddr_storage *saddr, uint16_t port,
303                           int *addr_len)
304 {
305         saddr->ss_family =  dlm_local_addr[0]->ss_family;
306         if (saddr->ss_family == AF_INET) {
307                 struct sockaddr_in *in4_addr = (struct sockaddr_in *)saddr;
308                 in4_addr->sin_port = cpu_to_be16(port);
309                 *addr_len = sizeof(struct sockaddr_in);
310                 memset(&in4_addr->sin_zero, 0, sizeof(in4_addr->sin_zero));
311         } else {
312                 struct sockaddr_in6 *in6_addr = (struct sockaddr_in6 *)saddr;
313                 in6_addr->sin6_port = cpu_to_be16(port);
314                 *addr_len = sizeof(struct sockaddr_in6);
315         }
316 }
317
318 /* Close a remote connection and tidy up */
319 static void close_connection(struct connection *con, bool and_other)
320 {
321         mutex_lock(&con->sock_mutex);
322
323         if (con->sock) {
324                 sock_release(con->sock);
325                 con->sock = NULL;
326         }
327         if (con->othercon && and_other) {
328                 /* Will only re-enter once. */
329                 close_connection(con->othercon, false);
330         }
331         if (con->rx_page) {
332                 __free_page(con->rx_page);
333                 con->rx_page = NULL;
334         }
335         con->retries = 0;
336         mutex_unlock(&con->sock_mutex);
337 }
338
339 /* We only send shutdown messages to nodes that are not part of the cluster */
340 static void sctp_send_shutdown(sctp_assoc_t associd)
341 {
342         static char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
343         struct msghdr outmessage;
344         struct cmsghdr *cmsg;
345         struct sctp_sndrcvinfo *sinfo;
346         int ret;
347         struct connection *con;
348
349         con = nodeid2con(0,0);
350         BUG_ON(con == NULL);
351
352         outmessage.msg_name = NULL;
353         outmessage.msg_namelen = 0;
354         outmessage.msg_control = outcmsg;
355         outmessage.msg_controllen = sizeof(outcmsg);
356         outmessage.msg_flags = MSG_EOR;
357
358         cmsg = CMSG_FIRSTHDR(&outmessage);
359         cmsg->cmsg_level = IPPROTO_SCTP;
360         cmsg->cmsg_type = SCTP_SNDRCV;
361         cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
362         outmessage.msg_controllen = cmsg->cmsg_len;
363         sinfo = CMSG_DATA(cmsg);
364         memset(sinfo, 0x00, sizeof(struct sctp_sndrcvinfo));
365
366         sinfo->sinfo_flags |= MSG_EOF;
367         sinfo->sinfo_assoc_id = associd;
368
369         ret = kernel_sendmsg(con->sock, &outmessage, NULL, 0, 0);
370
371         if (ret != 0)
372                 log_print("send EOF to node failed: %d", ret);
373 }
374
375 /* INIT failed but we don't know which node...
376    restart INIT on all pending nodes */
377 static void sctp_init_failed(void)
378 {
379         int i;
380         struct connection *con;
381
382         down(&connections_lock);
383         for (i=1; i<=max_nodeid; i++) {
384                 con = __nodeid2con(i, 0);
385                 if (!con)
386                         continue;
387                 con->sctp_assoc = 0;
388                 if (test_and_clear_bit(CF_CONNECT_PENDING, &con->flags)) {
389                         if (!test_and_set_bit(CF_WRITE_PENDING, &con->flags)) {
390                                 queue_work(send_workqueue, &con->swork);
391                         }
392                 }
393         }
394         up(&connections_lock);
395 }
396
397 /* Something happened to an association */
398 static void process_sctp_notification(struct connection *con, struct msghdr *msg, char *buf)
399 {
400         union sctp_notification *sn = (union sctp_notification *)buf;
401
402         if (sn->sn_header.sn_type == SCTP_ASSOC_CHANGE) {
403                 switch (sn->sn_assoc_change.sac_state) {
404
405                 case SCTP_COMM_UP:
406                 case SCTP_RESTART:
407                 {
408                         /* Check that the new node is in the lockspace */
409                         struct sctp_prim prim;
410                         int nodeid;
411                         int prim_len, ret;
412                         int addr_len;
413                         struct connection *new_con;
414                         struct file *file;
415                         sctp_peeloff_arg_t parg;
416                         int parglen = sizeof(parg);
417
418                         /*
419                          * We get this before any data for an association.
420                          * We verify that the node is in the cluster and
421                          * then peel off a socket for it.
422                          */
423                         if ((int)sn->sn_assoc_change.sac_assoc_id <= 0) {
424                                 log_print("COMM_UP for invalid assoc ID %d",
425                                           (int)sn->sn_assoc_change.sac_assoc_id);
426                                 sctp_init_failed();
427                                 return;
428                         }
429                         memset(&prim, 0, sizeof(struct sctp_prim));
430                         prim_len = sizeof(struct sctp_prim);
431                         prim.ssp_assoc_id = sn->sn_assoc_change.sac_assoc_id;
432
433                         ret = kernel_getsockopt(con->sock,
434                                                 IPPROTO_SCTP,
435                                                 SCTP_PRIMARY_ADDR,
436                                                 (char*)&prim,
437                                                 &prim_len);
438                         if (ret < 0) {
439                                 log_print("getsockopt/sctp_primary_addr on "
440                                           "new assoc %d failed : %d",
441                                           (int)sn->sn_assoc_change.sac_assoc_id,
442                                           ret);
443
444                                 /* Retry INIT later */
445                                 new_con = assoc2con(sn->sn_assoc_change.sac_assoc_id);
446                                 if (new_con)
447                                         clear_bit(CF_CONNECT_PENDING, &con->flags);
448                                 return;
449                         }
450                         make_sockaddr(&prim.ssp_addr, 0, &addr_len);
451                         if (dlm_addr_to_nodeid(&prim.ssp_addr, &nodeid)) {
452                                 int i;
453                                 unsigned char *b=(unsigned char *)&prim.ssp_addr;
454                                 log_print("reject connect from unknown addr");
455                                 for (i=0; i<sizeof(struct sockaddr_storage);i++)
456                                         printk("%02x ", b[i]);
457                                 printk("\n");
458                                 sctp_send_shutdown(prim.ssp_assoc_id);
459                                 return;
460                         }
461
462                         new_con = nodeid2con(nodeid, GFP_KERNEL);
463                         if (!new_con)
464                                 return;
465
466                         /* Peel off a new sock */
467                         parg.associd = sn->sn_assoc_change.sac_assoc_id;
468                         ret = kernel_getsockopt(con->sock, IPPROTO_SCTP, SCTP_SOCKOPT_PEELOFF,
469                                                 (void *)&parg, &parglen);
470                         if (ret) {
471                                 log_print("Can't peel off a socket for connection %d to node %d: err=%d\n",
472                                           parg.associd, nodeid, ret);
473                         }
474                         file = fget(parg.sd);
475                         new_con->sock = SOCKET_I(file->f_dentry->d_inode);
476                         add_sock(new_con->sock, new_con);
477                         fput(file);
478                         put_unused_fd(parg.sd);
479
480                         log_print("got new/restarted association %d nodeid %d",
481                                   (int)sn->sn_assoc_change.sac_assoc_id, nodeid);
482
483                         /* Send any pending writes */
484                         clear_bit(CF_CONNECT_PENDING, &new_con->flags);
485                         clear_bit(CF_INIT_PENDING, &con->flags);
486                         if (!test_and_set_bit(CF_WRITE_PENDING, &new_con->flags)) {
487                                 queue_work(send_workqueue, &new_con->swork);
488                         }
489                         if (!test_and_set_bit(CF_READ_PENDING, &new_con->flags))
490                                 queue_work(recv_workqueue, &new_con->rwork);
491                 }
492                 break;
493
494                 case SCTP_COMM_LOST:
495                 case SCTP_SHUTDOWN_COMP:
496                 {
497                         con = assoc2con(sn->sn_assoc_change.sac_assoc_id);
498                         if (con) {
499                                 con->sctp_assoc = 0;
500                         }
501                 }
502                 break;
503
504                 /* We don't know which INIT failed, so clear the PENDING flags
505                  * on them all.  if assoc_id is zero then it will then try
506                  * again */
507
508                 case SCTP_CANT_STR_ASSOC:
509                 {
510                         log_print("Can't start SCTP association - retrying");
511                         sctp_init_failed();
512                 }
513                 break;
514
515                 default:
516                         log_print("unexpected SCTP assoc change id=%d state=%d",
517                                   (int)sn->sn_assoc_change.sac_assoc_id,
518                                   sn->sn_assoc_change.sac_state);
519                 }
520         }
521 }
522
523 /* Data received from remote end */
524 static int receive_from_sock(struct connection *con)
525 {
526         int ret = 0;
527         struct msghdr msg = {};
528         struct kvec iov[2];
529         unsigned len;
530         int r;
531         int call_again_soon = 0;
532         int nvec;
533         char incmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
534
535         mutex_lock(&con->sock_mutex);
536
537         if (con->sock == NULL) {
538                 ret = -EAGAIN;
539                 goto out_close;
540         }
541
542         if (con->rx_page == NULL) {
543                 /*
544                  * This doesn't need to be atomic, but I think it should
545                  * improve performance if it is.
546                  */
547                 con->rx_page = alloc_page(GFP_ATOMIC);
548                 if (con->rx_page == NULL)
549                         goto out_resched;
550                 cbuf_init(&con->cb, PAGE_CACHE_SIZE);
551         }
552
553         /* Only SCTP needs these really */
554         memset(&incmsg, 0, sizeof(incmsg));
555         msg.msg_control = incmsg;
556         msg.msg_controllen = sizeof(incmsg);
557
558         /*
559          * iov[0] is the bit of the circular buffer between the current end
560          * point (cb.base + cb.len) and the end of the buffer.
561          */
562         iov[0].iov_len = con->cb.base - cbuf_data(&con->cb);
563         iov[0].iov_base = page_address(con->rx_page) + cbuf_data(&con->cb);
564         iov[1].iov_len = 0;
565         nvec = 1;
566
567         /*
568          * iov[1] is the bit of the circular buffer between the start of the
569          * buffer and the start of the currently used section (cb.base)
570          */
571         if (cbuf_data(&con->cb) >= con->cb.base) {
572                 iov[0].iov_len = PAGE_CACHE_SIZE - cbuf_data(&con->cb);
573                 iov[1].iov_len = con->cb.base;
574                 iov[1].iov_base = page_address(con->rx_page);
575                 nvec = 2;
576         }
577         len = iov[0].iov_len + iov[1].iov_len;
578
579         r = ret = kernel_recvmsg(con->sock, &msg, iov, nvec, len,
580                                MSG_DONTWAIT | MSG_NOSIGNAL);
581         if (ret <= 0)
582                 goto out_close;
583
584         /* Process SCTP notifications */
585         if (msg.msg_flags & MSG_NOTIFICATION) {
586                 msg.msg_control = incmsg;
587                 msg.msg_controllen = sizeof(incmsg);
588
589                 process_sctp_notification(con, &msg,
590                                           page_address(con->rx_page) + con->cb.base);
591                 mutex_unlock(&con->sock_mutex);
592                 return 0;
593         }
594         BUG_ON(con->nodeid == 0);
595
596         if (ret == len)
597                 call_again_soon = 1;
598         cbuf_add(&con->cb, ret);
599         ret = dlm_process_incoming_buffer(con->nodeid,
600                                           page_address(con->rx_page),
601                                           con->cb.base, con->cb.len,
602                                           PAGE_CACHE_SIZE);
603         if (ret == -EBADMSG) {
604                 printk(KERN_INFO "dlm: lowcomms: addr=%p, base=%u, len=%u, "
605                        "iov_len=%u, iov_base[0]=%p, read=%d\n",
606                        page_address(con->rx_page), con->cb.base, con->cb.len,
607                        len, iov[0].iov_base, r);
608         }
609         if (ret < 0)
610                 goto out_close;
611         cbuf_eat(&con->cb, ret);
612
613         if (cbuf_empty(&con->cb) && !call_again_soon) {
614                 __free_page(con->rx_page);
615                 con->rx_page = NULL;
616         }
617
618         if (call_again_soon)
619                 goto out_resched;
620         mutex_unlock(&con->sock_mutex);
621         return 0;
622
623 out_resched:
624         if (!test_and_set_bit(CF_READ_PENDING, &con->flags))
625                 queue_work(recv_workqueue, &con->rwork);
626         mutex_unlock(&con->sock_mutex);
627         return -EAGAIN;
628
629 out_close:
630         mutex_unlock(&con->sock_mutex);
631         if (ret != -EAGAIN && !test_bit(CF_IS_OTHERCON, &con->flags)) {
632                 close_connection(con, false);
633                 /* Reconnect when there is something to send */
634         }
635         /* Don't return success if we really got EOF */
636         if (ret == 0)
637                 ret = -EAGAIN;
638
639         return ret;
640 }
641
642 /* Listening socket is busy, accept a connection */
643 static int tcp_accept_from_sock(struct connection *con)
644 {
645         int result;
646         struct sockaddr_storage peeraddr;
647         struct socket *newsock;
648         int len;
649         int nodeid;
650         struct connection *newcon;
651         struct connection *addcon;
652
653         memset(&peeraddr, 0, sizeof(peeraddr));
654         result = sock_create_kern(dlm_local_addr[0]->ss_family, SOCK_STREAM,
655                                   IPPROTO_TCP, &newsock);
656         if (result < 0)
657                 return -ENOMEM;
658
659         mutex_lock_nested(&con->sock_mutex, 0);
660
661         result = -ENOTCONN;
662         if (con->sock == NULL)
663                 goto accept_err;
664
665         newsock->type = con->sock->type;
666         newsock->ops = con->sock->ops;
667
668         result = con->sock->ops->accept(con->sock, newsock, O_NONBLOCK);
669         if (result < 0)
670                 goto accept_err;
671
672         /* Get the connected socket's peer */
673         memset(&peeraddr, 0, sizeof(peeraddr));
674         if (newsock->ops->getname(newsock, (struct sockaddr *)&peeraddr,
675                                   &len, 2)) {
676                 result = -ECONNABORTED;
677                 goto accept_err;
678         }
679
680         /* Get the new node's NODEID */
681         make_sockaddr(&peeraddr, 0, &len);
682         if (dlm_addr_to_nodeid(&peeraddr, &nodeid)) {
683                 printk("dlm: connect from non cluster node\n");
684                 sock_release(newsock);
685                 mutex_unlock(&con->sock_mutex);
686                 return -1;
687         }
688
689         log_print("got connection from %d", nodeid);
690
691         /*  Check to see if we already have a connection to this node. This
692          *  could happen if the two nodes initiate a connection at roughly
693          *  the same time and the connections cross on the wire.
694          *  In this case we store the incoming one in "othercon"
695          */
696         newcon = nodeid2con(nodeid, GFP_KERNEL);
697         if (!newcon) {
698                 result = -ENOMEM;
699                 goto accept_err;
700         }
701         mutex_lock_nested(&newcon->sock_mutex, 1);
702         if (newcon->sock) {
703                 struct connection *othercon = newcon->othercon;
704
705                 if (!othercon) {
706                         othercon = kmem_cache_zalloc(con_cache, GFP_KERNEL);
707                         if (!othercon) {
708                                 printk("dlm: failed to allocate incoming socket\n");
709                                 mutex_unlock(&newcon->sock_mutex);
710                                 result = -ENOMEM;
711                                 goto accept_err;
712                         }
713                         othercon->nodeid = nodeid;
714                         othercon->rx_action = receive_from_sock;
715                         mutex_init(&othercon->sock_mutex);
716                         INIT_WORK(&othercon->swork, process_send_sockets);
717                         INIT_WORK(&othercon->rwork, process_recv_sockets);
718                         set_bit(CF_IS_OTHERCON, &othercon->flags);
719                         newcon->othercon = othercon;
720                 }
721                 othercon->sock = newsock;
722                 newsock->sk->sk_user_data = othercon;
723                 add_sock(newsock, othercon);
724                 addcon = othercon;
725         }
726         else {
727                 newsock->sk->sk_user_data = newcon;
728                 newcon->rx_action = receive_from_sock;
729                 add_sock(newsock, newcon);
730                 addcon = newcon;
731         }
732
733         mutex_unlock(&newcon->sock_mutex);
734
735         /*
736          * Add it to the active queue in case we got data
737          * beween processing the accept adding the socket
738          * to the read_sockets list
739          */
740         if (!test_and_set_bit(CF_READ_PENDING, &addcon->flags))
741                 queue_work(recv_workqueue, &addcon->rwork);
742         mutex_unlock(&con->sock_mutex);
743
744         return 0;
745
746 accept_err:
747         mutex_unlock(&con->sock_mutex);
748         sock_release(newsock);
749
750         if (result != -EAGAIN)
751                 printk("dlm: error accepting connection from node: %d\n", result);
752         return result;
753 }
754
755 static void free_entry(struct writequeue_entry *e)
756 {
757         __free_page(e->page);
758         kfree(e);
759 }
760
761 /* Initiate an SCTP association.
762    This is a special case of send_to_sock() in that we don't yet have a
763    peeled-off socket for this association, so we use the listening socket
764    and add the primary IP address of the remote node.
765  */
766 static void sctp_init_assoc(struct connection *con)
767 {
768         struct sockaddr_storage rem_addr;
769         char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
770         struct msghdr outmessage;
771         struct cmsghdr *cmsg;
772         struct sctp_sndrcvinfo *sinfo;
773         struct connection *base_con;
774         struct writequeue_entry *e;
775         int len, offset;
776         int ret;
777         int addrlen;
778         struct kvec iov[1];
779
780         if (test_and_set_bit(CF_INIT_PENDING, &con->flags))
781                 return;
782
783         if (con->retries++ > MAX_CONNECT_RETRIES)
784                 return;
785
786         log_print("Initiating association with node %d", con->nodeid);
787
788         if (nodeid_to_addr(con->nodeid, (struct sockaddr *)&rem_addr)) {
789                 log_print("no address for nodeid %d", con->nodeid);
790                 return;
791         }
792         base_con = nodeid2con(0, 0);
793         BUG_ON(base_con == NULL);
794
795         make_sockaddr(&rem_addr, dlm_config.ci_tcp_port, &addrlen);
796
797         outmessage.msg_name = &rem_addr;
798         outmessage.msg_namelen = addrlen;
799         outmessage.msg_control = outcmsg;
800         outmessage.msg_controllen = sizeof(outcmsg);
801         outmessage.msg_flags = MSG_EOR;
802
803         spin_lock(&con->writequeue_lock);
804         e = list_entry(con->writequeue.next, struct writequeue_entry,
805                        list);
806
807         BUG_ON((struct list_head *) e == &con->writequeue);
808
809         len = e->len;
810         offset = e->offset;
811         spin_unlock(&con->writequeue_lock);
812         kmap(e->page);
813
814         /* Send the first block off the write queue */
815         iov[0].iov_base = page_address(e->page)+offset;
816         iov[0].iov_len = len;
817
818         cmsg = CMSG_FIRSTHDR(&outmessage);
819         cmsg->cmsg_level = IPPROTO_SCTP;
820         cmsg->cmsg_type = SCTP_SNDRCV;
821         cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
822         sinfo = CMSG_DATA(cmsg);
823         memset(sinfo, 0x00, sizeof(struct sctp_sndrcvinfo));
824         sinfo->sinfo_ppid = cpu_to_le32(dlm_our_nodeid());
825         outmessage.msg_controllen = cmsg->cmsg_len;
826
827         ret = kernel_sendmsg(base_con->sock, &outmessage, iov, 1, len);
828         if (ret < 0) {
829                 log_print("Send first packet to node %d failed: %d", con->nodeid, ret);
830
831                 /* Try again later */
832                 clear_bit(CF_CONNECT_PENDING, &con->flags);
833                 clear_bit(CF_INIT_PENDING, &con->flags);
834         }
835         else {
836                 spin_lock(&con->writequeue_lock);
837                 e->offset += ret;
838                 e->len -= ret;
839
840                 if (e->len == 0 && e->users == 0) {
841                         list_del(&e->list);
842                         kunmap(e->page);
843                         free_entry(e);
844                 }
845                 spin_unlock(&con->writequeue_lock);
846         }
847 }
848
849 /* Connect a new socket to its peer */
850 static void tcp_connect_to_sock(struct connection *con)
851 {
852         int result = -EHOSTUNREACH;
853         struct sockaddr_storage saddr;
854         int addr_len;
855         struct socket *sock;
856
857         if (con->nodeid == 0) {
858                 log_print("attempt to connect sock 0 foiled");
859                 return;
860         }
861
862         mutex_lock(&con->sock_mutex);
863         if (con->retries++ > MAX_CONNECT_RETRIES)
864                 goto out;
865
866         /* Some odd races can cause double-connects, ignore them */
867         if (con->sock) {
868                 result = 0;
869                 goto out;
870         }
871
872         /* Create a socket to communicate with */
873         result = sock_create_kern(dlm_local_addr[0]->ss_family, SOCK_STREAM,
874                                   IPPROTO_TCP, &sock);
875         if (result < 0)
876                 goto out_err;
877
878         memset(&saddr, 0, sizeof(saddr));
879         if (dlm_nodeid_to_addr(con->nodeid, &saddr))
880                 goto out_err;
881
882         sock->sk->sk_user_data = con;
883         con->rx_action = receive_from_sock;
884         con->connect_action = tcp_connect_to_sock;
885         add_sock(sock, con);
886
887         make_sockaddr(&saddr, dlm_config.ci_tcp_port, &addr_len);
888
889         log_print("connecting to %d", con->nodeid);
890         result =
891                 sock->ops->connect(sock, (struct sockaddr *)&saddr, addr_len,
892                                    O_NONBLOCK);
893         if (result == -EINPROGRESS)
894                 result = 0;
895         if (result == 0)
896                 goto out;
897
898 out_err:
899         if (con->sock) {
900                 sock_release(con->sock);
901                 con->sock = NULL;
902         }
903         /*
904          * Some errors are fatal and this list might need adjusting. For other
905          * errors we try again until the max number of retries is reached.
906          */
907         if (result != -EHOSTUNREACH && result != -ENETUNREACH &&
908             result != -ENETDOWN && result != EINVAL
909             && result != -EPROTONOSUPPORT) {
910                 lowcomms_connect_sock(con);
911                 result = 0;
912         }
913 out:
914         mutex_unlock(&con->sock_mutex);
915         return;
916 }
917
918 static struct socket *tcp_create_listen_sock(struct connection *con,
919                                              struct sockaddr_storage *saddr)
920 {
921         struct socket *sock = NULL;
922         int result = 0;
923         int one = 1;
924         int addr_len;
925
926         if (dlm_local_addr[0]->ss_family == AF_INET)
927                 addr_len = sizeof(struct sockaddr_in);
928         else
929                 addr_len = sizeof(struct sockaddr_in6);
930
931         /* Create a socket to communicate with */
932         result = sock_create_kern(dlm_local_addr[0]->ss_family, SOCK_STREAM, IPPROTO_TCP, &sock);
933         if (result < 0) {
934                 printk("dlm: Can't create listening comms socket\n");
935                 goto create_out;
936         }
937
938         result = kernel_setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
939                                    (char *)&one, sizeof(one));
940
941         if (result < 0) {
942                 printk("dlm: Failed to set SO_REUSEADDR on socket: result=%d\n",
943                        result);
944         }
945         sock->sk->sk_user_data = con;
946         con->rx_action = tcp_accept_from_sock;
947         con->connect_action = tcp_connect_to_sock;
948         con->sock = sock;
949
950         /* Bind to our port */
951         make_sockaddr(saddr, dlm_config.ci_tcp_port, &addr_len);
952         result = sock->ops->bind(sock, (struct sockaddr *) saddr, addr_len);
953         if (result < 0) {
954                 printk("dlm: Can't bind to port %d\n", dlm_config.ci_tcp_port);
955                 sock_release(sock);
956                 sock = NULL;
957                 con->sock = NULL;
958                 goto create_out;
959         }
960         result = kernel_setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE,
961                                  (char *)&one, sizeof(one));
962         if (result < 0) {
963                 printk("dlm: Set keepalive failed: %d\n", result);
964         }
965
966         result = sock->ops->listen(sock, 5);
967         if (result < 0) {
968                 printk("dlm: Can't listen on port %d\n", dlm_config.ci_tcp_port);
969                 sock_release(sock);
970                 sock = NULL;
971                 goto create_out;
972         }
973
974 create_out:
975         return sock;
976 }
977
978 /* Get local addresses */
979 static void init_local(void)
980 {
981         struct sockaddr_storage sas, *addr;
982         int i;
983
984         dlm_local_count = 0;
985         for (i = 0; i < DLM_MAX_ADDR_COUNT - 1; i++) {
986                 if (dlm_our_addr(&sas, i))
987                         break;
988
989                 addr = kmalloc(sizeof(*addr), GFP_KERNEL);
990                 if (!addr)
991                         break;
992                 memcpy(addr, &sas, sizeof(*addr));
993                 dlm_local_addr[dlm_local_count++] = addr;
994         }
995 }
996
997 /* Bind to an IP address. SCTP allows multiple address so it can do multi-homing */
998 static int add_sctp_bind_addr(struct connection *sctp_con, struct sockaddr_storage *addr, int addr_len, int num)
999 {
1000         int result = 0;
1001
1002         if (num == 1)
1003                 result = kernel_bind(sctp_con->sock,
1004                                      (struct sockaddr *) addr,
1005                                      addr_len);
1006         else
1007                 result = kernel_setsockopt(sctp_con->sock, SOL_SCTP,
1008                                            SCTP_SOCKOPT_BINDX_ADD,
1009                                            (char *)addr, addr_len);
1010
1011         if (result < 0)
1012                 log_print("Can't bind to port %d addr number %d",
1013                           dlm_config.ci_tcp_port, num);
1014
1015         return result;
1016 }
1017
1018 /* Initialise SCTP socket and bind to all interfaces */
1019 static int sctp_listen_for_all(void)
1020 {
1021         struct socket *sock = NULL;
1022         struct sockaddr_storage localaddr;
1023         struct sctp_event_subscribe subscribe;
1024         int result = -EINVAL, num = 1, i, addr_len;
1025         struct connection *con = nodeid2con(0, GFP_KERNEL);
1026         int bufsize = NEEDED_RMEM;
1027
1028         if (!con)
1029                 return -ENOMEM;
1030
1031         log_print("Using SCTP for communications");
1032
1033         result = sock_create_kern(dlm_local_addr[0]->ss_family, SOCK_SEQPACKET,
1034                                   IPPROTO_SCTP, &sock);
1035         if (result < 0) {
1036                 log_print("Can't create comms socket, check SCTP is loaded");
1037                 goto out;
1038         }
1039
1040         /* Listen for events */
1041         memset(&subscribe, 0, sizeof(subscribe));
1042         subscribe.sctp_data_io_event = 1;
1043         subscribe.sctp_association_event = 1;
1044         subscribe.sctp_send_failure_event = 1;
1045         subscribe.sctp_shutdown_event = 1;
1046         subscribe.sctp_partial_delivery_event = 1;
1047
1048         result = kernel_setsockopt(sock, SOL_SOCKET, SO_RCVBUF,
1049                                  (char *)&bufsize, sizeof(bufsize));
1050         if (result)
1051                 log_print("Error increasing buffer space on socket: %d", result);
1052
1053         result = kernel_setsockopt(sock, SOL_SCTP, SCTP_EVENTS,
1054                                        (char *)&subscribe, sizeof(subscribe));
1055         if (result < 0) {
1056                 log_print("Failed to set SCTP_EVENTS on socket: result=%d",
1057                           result);
1058                 goto create_delsock;
1059         }
1060
1061         /* Init con struct */
1062         sock->sk->sk_user_data = con;
1063         con->sock = sock;
1064         con->sock->sk->sk_data_ready = lowcomms_data_ready;
1065         con->rx_action = receive_from_sock;
1066         con->connect_action = sctp_init_assoc;
1067
1068         /* Bind to all interfaces. */
1069         for (i = 0; i < dlm_local_count; i++) {
1070                 memcpy(&localaddr, dlm_local_addr[i], sizeof(localaddr));
1071                 make_sockaddr(&localaddr, dlm_config.ci_tcp_port, &addr_len);
1072
1073                 result = add_sctp_bind_addr(con, &localaddr, addr_len, num);
1074                 if (result)
1075                         goto create_delsock;
1076                 ++num;
1077         }
1078
1079         result = sock->ops->listen(sock, 5);
1080         if (result < 0) {
1081                 log_print("Can't set socket listening");
1082                 goto create_delsock;
1083         }
1084
1085         return 0;
1086
1087 create_delsock:
1088         sock_release(sock);
1089         con->sock = NULL;
1090 out:
1091         return result;
1092 }
1093
1094 static int tcp_listen_for_all(void)
1095 {
1096         struct socket *sock = NULL;
1097         struct connection *con = nodeid2con(0, GFP_KERNEL);
1098         int result = -EINVAL;
1099
1100         if (!con)
1101                 return -ENOMEM;
1102
1103         /* We don't support multi-homed hosts */
1104         if (dlm_local_addr[1] != NULL) {
1105                 log_print("TCP protocol can't handle multi-homed hosts, try SCTP");
1106                 return -EINVAL;
1107         }
1108
1109         log_print("Using TCP for communications");
1110
1111         set_bit(CF_IS_OTHERCON, &con->flags);
1112
1113         sock = tcp_create_listen_sock(con, dlm_local_addr[0]);
1114         if (sock) {
1115                 add_sock(sock, con);
1116                 result = 0;
1117         }
1118         else {
1119                 result = -EADDRINUSE;
1120         }
1121
1122         return result;
1123 }
1124
1125
1126
1127 static struct writequeue_entry *new_writequeue_entry(struct connection *con,
1128                                                      gfp_t allocation)
1129 {
1130         struct writequeue_entry *entry;
1131
1132         entry = kmalloc(sizeof(struct writequeue_entry), allocation);
1133         if (!entry)
1134                 return NULL;
1135
1136         entry->page = alloc_page(allocation);
1137         if (!entry->page) {
1138                 kfree(entry);
1139                 return NULL;
1140         }
1141
1142         entry->offset = 0;
1143         entry->len = 0;
1144         entry->end = 0;
1145         entry->users = 0;
1146         entry->con = con;
1147
1148         return entry;
1149 }
1150
1151 void *dlm_lowcomms_get_buffer(int nodeid, int len,
1152                               gfp_t allocation, char **ppc)
1153 {
1154         struct connection *con;
1155         struct writequeue_entry *e;
1156         int offset = 0;
1157         int users = 0;
1158
1159         con = nodeid2con(nodeid, allocation);
1160         if (!con)
1161                 return NULL;
1162
1163         spin_lock(&con->writequeue_lock);
1164         e = list_entry(con->writequeue.prev, struct writequeue_entry, list);
1165         if ((&e->list == &con->writequeue) ||
1166             (PAGE_CACHE_SIZE - e->end < len)) {
1167                 e = NULL;
1168         } else {
1169                 offset = e->end;
1170                 e->end += len;
1171                 users = e->users++;
1172         }
1173         spin_unlock(&con->writequeue_lock);
1174
1175         if (e) {
1176         got_one:
1177                 if (users == 0)
1178                         kmap(e->page);
1179                 *ppc = page_address(e->page) + offset;
1180                 return e;
1181         }
1182
1183         e = new_writequeue_entry(con, allocation);
1184         if (e) {
1185                 spin_lock(&con->writequeue_lock);
1186                 offset = e->end;
1187                 e->end += len;
1188                 users = e->users++;
1189                 list_add_tail(&e->list, &con->writequeue);
1190                 spin_unlock(&con->writequeue_lock);
1191                 goto got_one;
1192         }
1193         return NULL;
1194 }
1195
1196 void dlm_lowcomms_commit_buffer(void *mh)
1197 {
1198         struct writequeue_entry *e = (struct writequeue_entry *)mh;
1199         struct connection *con = e->con;
1200         int users;
1201
1202         spin_lock(&con->writequeue_lock);
1203         users = --e->users;
1204         if (users)
1205                 goto out;
1206         e->len = e->end - e->offset;
1207         kunmap(e->page);
1208         spin_unlock(&con->writequeue_lock);
1209
1210         if (!test_and_set_bit(CF_WRITE_PENDING, &con->flags)) {
1211                 queue_work(send_workqueue, &con->swork);
1212         }
1213         return;
1214
1215 out:
1216         spin_unlock(&con->writequeue_lock);
1217         return;
1218 }
1219
1220 /* Send a message */
1221 static void send_to_sock(struct connection *con)
1222 {
1223         int ret = 0;
1224         ssize_t(*sendpage) (struct socket *, struct page *, int, size_t, int);
1225         const int msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL;
1226         struct writequeue_entry *e;
1227         int len, offset;
1228
1229         mutex_lock(&con->sock_mutex);
1230         if (con->sock == NULL)
1231                 goto out_connect;
1232
1233         sendpage = con->sock->ops->sendpage;
1234
1235         spin_lock(&con->writequeue_lock);
1236         for (;;) {
1237                 e = list_entry(con->writequeue.next, struct writequeue_entry,
1238                                list);
1239                 if ((struct list_head *) e == &con->writequeue)
1240                         break;
1241
1242                 len = e->len;
1243                 offset = e->offset;
1244                 BUG_ON(len == 0 && e->users == 0);
1245                 spin_unlock(&con->writequeue_lock);
1246                 kmap(e->page);
1247
1248                 ret = 0;
1249                 if (len) {
1250                         ret = sendpage(con->sock, e->page, offset, len,
1251                                        msg_flags);
1252                         if (ret == -EAGAIN || ret == 0)
1253                                 goto out;
1254                         if (ret <= 0)
1255                                 goto send_error;
1256                 }
1257                 else {
1258                         /* Don't starve people filling buffers */
1259                         cond_resched();
1260                 }
1261
1262                 spin_lock(&con->writequeue_lock);
1263                 e->offset += ret;
1264                 e->len -= ret;
1265
1266                 if (e->len == 0 && e->users == 0) {
1267                         list_del(&e->list);
1268                         kunmap(e->page);
1269                         free_entry(e);
1270                         continue;
1271                 }
1272         }
1273         spin_unlock(&con->writequeue_lock);
1274 out:
1275         mutex_unlock(&con->sock_mutex);
1276         return;
1277
1278 send_error:
1279         mutex_unlock(&con->sock_mutex);
1280         close_connection(con, false);
1281         lowcomms_connect_sock(con);
1282         return;
1283
1284 out_connect:
1285         mutex_unlock(&con->sock_mutex);
1286         if (!test_bit(CF_INIT_PENDING, &con->flags))
1287                 lowcomms_connect_sock(con);
1288         return;
1289 }
1290
1291 static void clean_one_writequeue(struct connection *con)
1292 {
1293         struct list_head *list;
1294         struct list_head *temp;
1295
1296         spin_lock(&con->writequeue_lock);
1297         list_for_each_safe(list, temp, &con->writequeue) {
1298                 struct writequeue_entry *e =
1299                         list_entry(list, struct writequeue_entry, list);
1300                 list_del(&e->list);
1301                 free_entry(e);
1302         }
1303         spin_unlock(&con->writequeue_lock);
1304 }
1305
1306 /* Called from recovery when it knows that a node has
1307    left the cluster */
1308 int dlm_lowcomms_close(int nodeid)
1309 {
1310         struct connection *con;
1311
1312         log_print("closing connection to node %d", nodeid);
1313         con = nodeid2con(nodeid, 0);
1314         if (con) {
1315                 clean_one_writequeue(con);
1316                 close_connection(con, true);
1317         }
1318         return 0;
1319 }
1320
1321 /* Receive workqueue function */
1322 static void process_recv_sockets(struct work_struct *work)
1323 {
1324         struct connection *con = container_of(work, struct connection, rwork);
1325         int err;
1326
1327         clear_bit(CF_READ_PENDING, &con->flags);
1328         do {
1329                 err = con->rx_action(con);
1330         } while (!err);
1331 }
1332
1333 /* Send workqueue function */
1334 static void process_send_sockets(struct work_struct *work)
1335 {
1336         struct connection *con = container_of(work, struct connection, swork);
1337
1338         if (test_and_clear_bit(CF_CONNECT_PENDING, &con->flags)) {
1339                 con->connect_action(con);
1340         }
1341         clear_bit(CF_WRITE_PENDING, &con->flags);
1342         send_to_sock(con);
1343 }
1344
1345
1346 /* Discard all entries on the write queues */
1347 static void clean_writequeues(void)
1348 {
1349         int nodeid;
1350
1351         for (nodeid = 1; nodeid <= max_nodeid; nodeid++) {
1352                 struct connection *con = __nodeid2con(nodeid, 0);
1353
1354                 if (con)
1355                         clean_one_writequeue(con);
1356         }
1357 }
1358
1359 static void work_stop(void)
1360 {
1361         destroy_workqueue(recv_workqueue);
1362         destroy_workqueue(send_workqueue);
1363 }
1364
1365 static int work_start(void)
1366 {
1367         int error;
1368         recv_workqueue = create_workqueue("dlm_recv");
1369         error = IS_ERR(recv_workqueue);
1370         if (error) {
1371                 log_print("can't start dlm_recv %d", error);
1372                 return error;
1373         }
1374
1375         send_workqueue = create_singlethread_workqueue("dlm_send");
1376         error = IS_ERR(send_workqueue);
1377         if (error) {
1378                 log_print("can't start dlm_send %d", error);
1379                 destroy_workqueue(recv_workqueue);
1380                 return error;
1381         }
1382
1383         return 0;
1384 }
1385
1386 void dlm_lowcomms_stop(void)
1387 {
1388         int i;
1389         struct connection *con;
1390
1391         /* Set all the flags to prevent any
1392            socket activity.
1393         */
1394         down(&connections_lock);
1395         for (i = 0; i <= max_nodeid; i++) {
1396                 con = __nodeid2con(i, 0);
1397                 if (con)
1398                         con->flags |= 0xFF;
1399         }
1400         up(&connections_lock);
1401
1402         work_stop();
1403
1404         down(&connections_lock);
1405         clean_writequeues();
1406
1407         for (i = 0; i <= max_nodeid; i++) {
1408                 con = __nodeid2con(i, 0);
1409                 if (con) {
1410                         close_connection(con, true);
1411                         if (con->othercon)
1412                                 kmem_cache_free(con_cache, con->othercon);
1413                         kmem_cache_free(con_cache, con);
1414                 }
1415         }
1416         max_nodeid = 0;
1417         up(&connections_lock);
1418         kmem_cache_destroy(con_cache);
1419         idr_init(&connections_idr);
1420 }
1421
1422 int dlm_lowcomms_start(void)
1423 {
1424         int error = -EINVAL;
1425         struct connection *con;
1426
1427         init_local();
1428         if (!dlm_local_count) {
1429                 log_print("no local IP address has been set");
1430                 goto out;
1431         }
1432
1433         error = -ENOMEM;
1434         con_cache = kmem_cache_create("dlm_conn", sizeof(struct connection),
1435                                       __alignof__(struct connection), 0,
1436                                       NULL, NULL);
1437         if (!con_cache)
1438                 goto out;
1439
1440         /* Set some sysctl minima */
1441         if (sysctl_rmem_max < NEEDED_RMEM)
1442                 sysctl_rmem_max = NEEDED_RMEM;
1443
1444         /* Start listening */
1445         if (dlm_config.ci_protocol == 0)
1446                 error = tcp_listen_for_all();
1447         else
1448                 error = sctp_listen_for_all();
1449         if (error)
1450                 goto fail_unlisten;
1451
1452         error = work_start();
1453         if (error)
1454                 goto fail_unlisten;
1455
1456         return 0;
1457
1458 fail_unlisten:
1459         con = nodeid2con(0,0);
1460         if (con) {
1461                 close_connection(con, false);
1462                 kmem_cache_free(con_cache, con);
1463         }
1464         kmem_cache_destroy(con_cache);
1465
1466 out:
1467         return error;
1468 }