[DLM] zero unused parts of sockaddr_storage
[powerpc.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 (con && !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 (con && !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         memset((char *)saddr + *addr_len, 0, sizeof(struct sockaddr_storage) - *addr_len);
317 }
318
319 /* Close a remote connection and tidy up */
320 static void close_connection(struct connection *con, bool and_other)
321 {
322         mutex_lock(&con->sock_mutex);
323
324         if (con->sock) {
325                 sock_release(con->sock);
326                 con->sock = NULL;
327         }
328         if (con->othercon && and_other) {
329                 /* Will only re-enter once. */
330                 close_connection(con->othercon, false);
331                 kmem_cache_free(con_cache, con->othercon);
332                 con->othercon = NULL;
333         }
334         if (con->rx_page) {
335                 __free_page(con->rx_page);
336                 con->rx_page = NULL;
337         }
338         con->retries = 0;
339         mutex_unlock(&con->sock_mutex);
340 }
341
342 /* We only send shutdown messages to nodes that are not part of the cluster */
343 static void sctp_send_shutdown(sctp_assoc_t associd)
344 {
345         static char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
346         struct msghdr outmessage;
347         struct cmsghdr *cmsg;
348         struct sctp_sndrcvinfo *sinfo;
349         int ret;
350         struct connection *con;
351
352         con = nodeid2con(0,0);
353         BUG_ON(con == NULL);
354
355         outmessage.msg_name = NULL;
356         outmessage.msg_namelen = 0;
357         outmessage.msg_control = outcmsg;
358         outmessage.msg_controllen = sizeof(outcmsg);
359         outmessage.msg_flags = MSG_EOR;
360
361         cmsg = CMSG_FIRSTHDR(&outmessage);
362         cmsg->cmsg_level = IPPROTO_SCTP;
363         cmsg->cmsg_type = SCTP_SNDRCV;
364         cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
365         outmessage.msg_controllen = cmsg->cmsg_len;
366         sinfo = CMSG_DATA(cmsg);
367         memset(sinfo, 0x00, sizeof(struct sctp_sndrcvinfo));
368
369         sinfo->sinfo_flags |= MSG_EOF;
370         sinfo->sinfo_assoc_id = associd;
371
372         ret = kernel_sendmsg(con->sock, &outmessage, NULL, 0, 0);
373
374         if (ret != 0)
375                 log_print("send EOF to node failed: %d", ret);
376 }
377
378 /* INIT failed but we don't know which node...
379    restart INIT on all pending nodes */
380 static void sctp_init_failed(void)
381 {
382         int i;
383         struct connection *con;
384
385         down(&connections_lock);
386         for (i=1; i<=max_nodeid; i++) {
387                 con = __nodeid2con(i, 0);
388                 if (!con)
389                         continue;
390                 con->sctp_assoc = 0;
391                 if (test_and_clear_bit(CF_CONNECT_PENDING, &con->flags)) {
392                         if (!test_and_set_bit(CF_WRITE_PENDING, &con->flags)) {
393                                 queue_work(send_workqueue, &con->swork);
394                         }
395                 }
396         }
397         up(&connections_lock);
398 }
399
400 /* Something happened to an association */
401 static void process_sctp_notification(struct connection *con,
402                                       struct msghdr *msg, char *buf)
403 {
404         union sctp_notification *sn = (union sctp_notification *)buf;
405
406         if (sn->sn_header.sn_type == SCTP_ASSOC_CHANGE) {
407                 switch (sn->sn_assoc_change.sac_state) {
408
409                 case SCTP_COMM_UP:
410                 case SCTP_RESTART:
411                 {
412                         /* Check that the new node is in the lockspace */
413                         struct sctp_prim prim;
414                         int nodeid;
415                         int prim_len, ret;
416                         int addr_len;
417                         struct connection *new_con;
418                         struct file *file;
419                         sctp_peeloff_arg_t parg;
420                         int parglen = sizeof(parg);
421
422                         /*
423                          * We get this before any data for an association.
424                          * We verify that the node is in the cluster and
425                          * then peel off a socket for it.
426                          */
427                         if ((int)sn->sn_assoc_change.sac_assoc_id <= 0) {
428                                 log_print("COMM_UP for invalid assoc ID %d",
429                                          (int)sn->sn_assoc_change.sac_assoc_id);
430                                 sctp_init_failed();
431                                 return;
432                         }
433                         memset(&prim, 0, sizeof(struct sctp_prim));
434                         prim_len = sizeof(struct sctp_prim);
435                         prim.ssp_assoc_id = sn->sn_assoc_change.sac_assoc_id;
436
437                         ret = kernel_getsockopt(con->sock,
438                                                 IPPROTO_SCTP,
439                                                 SCTP_PRIMARY_ADDR,
440                                                 (char*)&prim,
441                                                 &prim_len);
442                         if (ret < 0) {
443                                 log_print("getsockopt/sctp_primary_addr on "
444                                           "new assoc %d failed : %d",
445                                           (int)sn->sn_assoc_change.sac_assoc_id,
446                                           ret);
447
448                                 /* Retry INIT later */
449                                 new_con = assoc2con(sn->sn_assoc_change.sac_assoc_id);
450                                 if (new_con)
451                                         clear_bit(CF_CONNECT_PENDING, &con->flags);
452                                 return;
453                         }
454                         make_sockaddr(&prim.ssp_addr, 0, &addr_len);
455                         if (dlm_addr_to_nodeid(&prim.ssp_addr, &nodeid)) {
456                                 int i;
457                                 unsigned char *b=(unsigned char *)&prim.ssp_addr;
458                                 log_print("reject connect from unknown addr");
459                                 for (i=0; i<sizeof(struct sockaddr_storage);i++)
460                                         printk("%02x ", b[i]);
461                                 printk("\n");
462                                 sctp_send_shutdown(prim.ssp_assoc_id);
463                                 return;
464                         }
465
466                         new_con = nodeid2con(nodeid, GFP_KERNEL);
467                         if (!new_con)
468                                 return;
469
470                         /* Peel off a new sock */
471                         parg.associd = sn->sn_assoc_change.sac_assoc_id;
472                         ret = kernel_getsockopt(con->sock, IPPROTO_SCTP,
473                                                 SCTP_SOCKOPT_PEELOFF,
474                                                 (void *)&parg, &parglen);
475                         if (ret) {
476                                 log_print("Can't peel off a socket for "
477                                           "connection %d to node %d: err=%d\n",
478                                           parg.associd, nodeid, ret);
479                         }
480                         file = fget(parg.sd);
481                         new_con->sock = SOCKET_I(file->f_dentry->d_inode);
482                         add_sock(new_con->sock, new_con);
483                         fput(file);
484                         put_unused_fd(parg.sd);
485
486                         log_print("got new/restarted association %d nodeid %d",
487                                  (int)sn->sn_assoc_change.sac_assoc_id, nodeid);
488
489                         /* Send any pending writes */
490                         clear_bit(CF_CONNECT_PENDING, &new_con->flags);
491                         clear_bit(CF_INIT_PENDING, &con->flags);
492                         if (!test_and_set_bit(CF_WRITE_PENDING, &new_con->flags)) {
493                                 queue_work(send_workqueue, &new_con->swork);
494                         }
495                         if (!test_and_set_bit(CF_READ_PENDING, &new_con->flags))
496                                 queue_work(recv_workqueue, &new_con->rwork);
497                 }
498                 break;
499
500                 case SCTP_COMM_LOST:
501                 case SCTP_SHUTDOWN_COMP:
502                 {
503                         con = assoc2con(sn->sn_assoc_change.sac_assoc_id);
504                         if (con) {
505                                 con->sctp_assoc = 0;
506                         }
507                 }
508                 break;
509
510                 /* We don't know which INIT failed, so clear the PENDING flags
511                  * on them all.  if assoc_id is zero then it will then try
512                  * again */
513
514                 case SCTP_CANT_STR_ASSOC:
515                 {
516                         log_print("Can't start SCTP association - retrying");
517                         sctp_init_failed();
518                 }
519                 break;
520
521                 default:
522                         log_print("unexpected SCTP assoc change id=%d state=%d",
523                                   (int)sn->sn_assoc_change.sac_assoc_id,
524                                   sn->sn_assoc_change.sac_state);
525                 }
526         }
527 }
528
529 /* Data received from remote end */
530 static int receive_from_sock(struct connection *con)
531 {
532         int ret = 0;
533         struct msghdr msg = {};
534         struct kvec iov[2];
535         unsigned len;
536         int r;
537         int call_again_soon = 0;
538         int nvec;
539         char incmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
540
541         mutex_lock(&con->sock_mutex);
542
543         if (con->sock == NULL) {
544                 ret = -EAGAIN;
545                 goto out_close;
546         }
547
548         if (con->rx_page == NULL) {
549                 /*
550                  * This doesn't need to be atomic, but I think it should
551                  * improve performance if it is.
552                  */
553                 con->rx_page = alloc_page(GFP_ATOMIC);
554                 if (con->rx_page == NULL)
555                         goto out_resched;
556                 cbuf_init(&con->cb, PAGE_CACHE_SIZE);
557         }
558
559         /* Only SCTP needs these really */
560         memset(&incmsg, 0, sizeof(incmsg));
561         msg.msg_control = incmsg;
562         msg.msg_controllen = sizeof(incmsg);
563
564         /*
565          * iov[0] is the bit of the circular buffer between the current end
566          * point (cb.base + cb.len) and the end of the buffer.
567          */
568         iov[0].iov_len = con->cb.base - cbuf_data(&con->cb);
569         iov[0].iov_base = page_address(con->rx_page) + cbuf_data(&con->cb);
570         iov[1].iov_len = 0;
571         nvec = 1;
572
573         /*
574          * iov[1] is the bit of the circular buffer between the start of the
575          * buffer and the start of the currently used section (cb.base)
576          */
577         if (cbuf_data(&con->cb) >= con->cb.base) {
578                 iov[0].iov_len = PAGE_CACHE_SIZE - cbuf_data(&con->cb);
579                 iov[1].iov_len = con->cb.base;
580                 iov[1].iov_base = page_address(con->rx_page);
581                 nvec = 2;
582         }
583         len = iov[0].iov_len + iov[1].iov_len;
584
585         r = ret = kernel_recvmsg(con->sock, &msg, iov, nvec, len,
586                                MSG_DONTWAIT | MSG_NOSIGNAL);
587         if (ret <= 0)
588                 goto out_close;
589
590         /* Process SCTP notifications */
591         if (msg.msg_flags & MSG_NOTIFICATION) {
592                 msg.msg_control = incmsg;
593                 msg.msg_controllen = sizeof(incmsg);
594
595                 process_sctp_notification(con, &msg,
596                                 page_address(con->rx_page) + con->cb.base);
597                 mutex_unlock(&con->sock_mutex);
598                 return 0;
599         }
600         BUG_ON(con->nodeid == 0);
601
602         if (ret == len)
603                 call_again_soon = 1;
604         cbuf_add(&con->cb, ret);
605         ret = dlm_process_incoming_buffer(con->nodeid,
606                                           page_address(con->rx_page),
607                                           con->cb.base, con->cb.len,
608                                           PAGE_CACHE_SIZE);
609         if (ret == -EBADMSG) {
610                 log_print("lowcomms: addr=%p, base=%u, len=%u, "
611                           "iov_len=%u, iov_base[0]=%p, read=%d",
612                           page_address(con->rx_page), con->cb.base, con->cb.len,
613                           len, iov[0].iov_base, r);
614         }
615         if (ret < 0)
616                 goto out_close;
617         cbuf_eat(&con->cb, ret);
618
619         if (cbuf_empty(&con->cb) && !call_again_soon) {
620                 __free_page(con->rx_page);
621                 con->rx_page = NULL;
622         }
623
624         if (call_again_soon)
625                 goto out_resched;
626         mutex_unlock(&con->sock_mutex);
627         return 0;
628
629 out_resched:
630         if (!test_and_set_bit(CF_READ_PENDING, &con->flags))
631                 queue_work(recv_workqueue, &con->rwork);
632         mutex_unlock(&con->sock_mutex);
633         return -EAGAIN;
634
635 out_close:
636         mutex_unlock(&con->sock_mutex);
637         if (ret != -EAGAIN && !test_bit(CF_IS_OTHERCON, &con->flags)) {
638                 close_connection(con, false);
639                 /* Reconnect when there is something to send */
640         }
641         /* Don't return success if we really got EOF */
642         if (ret == 0)
643                 ret = -EAGAIN;
644
645         return ret;
646 }
647
648 /* Listening socket is busy, accept a connection */
649 static int tcp_accept_from_sock(struct connection *con)
650 {
651         int result;
652         struct sockaddr_storage peeraddr;
653         struct socket *newsock;
654         int len;
655         int nodeid;
656         struct connection *newcon;
657         struct connection *addcon;
658
659         memset(&peeraddr, 0, sizeof(peeraddr));
660         result = sock_create_kern(dlm_local_addr[0]->ss_family, SOCK_STREAM,
661                                   IPPROTO_TCP, &newsock);
662         if (result < 0)
663                 return -ENOMEM;
664
665         mutex_lock_nested(&con->sock_mutex, 0);
666
667         result = -ENOTCONN;
668         if (con->sock == NULL)
669                 goto accept_err;
670
671         newsock->type = con->sock->type;
672         newsock->ops = con->sock->ops;
673
674         result = con->sock->ops->accept(con->sock, newsock, O_NONBLOCK);
675         if (result < 0)
676                 goto accept_err;
677
678         /* Get the connected socket's peer */
679         memset(&peeraddr, 0, sizeof(peeraddr));
680         if (newsock->ops->getname(newsock, (struct sockaddr *)&peeraddr,
681                                   &len, 2)) {
682                 result = -ECONNABORTED;
683                 goto accept_err;
684         }
685
686         /* Get the new node's NODEID */
687         make_sockaddr(&peeraddr, 0, &len);
688         if (dlm_addr_to_nodeid(&peeraddr, &nodeid)) {
689                 log_print("connect from non cluster node");
690                 sock_release(newsock);
691                 mutex_unlock(&con->sock_mutex);
692                 return -1;
693         }
694
695         log_print("got connection from %d", nodeid);
696
697         /*  Check to see if we already have a connection to this node. This
698          *  could happen if the two nodes initiate a connection at roughly
699          *  the same time and the connections cross on the wire.
700          *  In this case we store the incoming one in "othercon"
701          */
702         newcon = nodeid2con(nodeid, GFP_KERNEL);
703         if (!newcon) {
704                 result = -ENOMEM;
705                 goto accept_err;
706         }
707         mutex_lock_nested(&newcon->sock_mutex, 1);
708         if (newcon->sock) {
709                 struct connection *othercon = newcon->othercon;
710
711                 if (!othercon) {
712                         othercon = kmem_cache_zalloc(con_cache, GFP_KERNEL);
713                         if (!othercon) {
714                                 log_print("failed to allocate incoming socket");
715                                 mutex_unlock(&newcon->sock_mutex);
716                                 result = -ENOMEM;
717                                 goto accept_err;
718                         }
719                         othercon->nodeid = nodeid;
720                         othercon->rx_action = receive_from_sock;
721                         mutex_init(&othercon->sock_mutex);
722                         INIT_WORK(&othercon->swork, process_send_sockets);
723                         INIT_WORK(&othercon->rwork, process_recv_sockets);
724                         set_bit(CF_IS_OTHERCON, &othercon->flags);
725                         newcon->othercon = othercon;
726                         othercon->sock = newsock;
727                         newsock->sk->sk_user_data = othercon;
728                         add_sock(newsock, othercon);
729                         addcon = othercon;
730                 }
731                 else {
732                         printk("Extra connection from node %d attempted\n", nodeid);
733                         result = -EAGAIN;
734                         mutex_unlock(&newcon->sock_mutex);
735                         goto accept_err;
736                 }
737         }
738         else {
739                 newsock->sk->sk_user_data = newcon;
740                 newcon->rx_action = receive_from_sock;
741                 add_sock(newsock, newcon);
742                 addcon = newcon;
743         }
744
745         mutex_unlock(&newcon->sock_mutex);
746
747         /*
748          * Add it to the active queue in case we got data
749          * beween processing the accept adding the socket
750          * to the read_sockets list
751          */
752         if (!test_and_set_bit(CF_READ_PENDING, &addcon->flags))
753                 queue_work(recv_workqueue, &addcon->rwork);
754         mutex_unlock(&con->sock_mutex);
755
756         return 0;
757
758 accept_err:
759         mutex_unlock(&con->sock_mutex);
760         sock_release(newsock);
761
762         if (result != -EAGAIN)
763                 log_print("error accepting connection from node: %d", result);
764         return result;
765 }
766
767 static void free_entry(struct writequeue_entry *e)
768 {
769         __free_page(e->page);
770         kfree(e);
771 }
772
773 /* Initiate an SCTP association.
774    This is a special case of send_to_sock() in that we don't yet have a
775    peeled-off socket for this association, so we use the listening socket
776    and add the primary IP address of the remote node.
777  */
778 static void sctp_init_assoc(struct connection *con)
779 {
780         struct sockaddr_storage rem_addr;
781         char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
782         struct msghdr outmessage;
783         struct cmsghdr *cmsg;
784         struct sctp_sndrcvinfo *sinfo;
785         struct connection *base_con;
786         struct writequeue_entry *e;
787         int len, offset;
788         int ret;
789         int addrlen;
790         struct kvec iov[1];
791
792         if (test_and_set_bit(CF_INIT_PENDING, &con->flags))
793                 return;
794
795         if (con->retries++ > MAX_CONNECT_RETRIES)
796                 return;
797
798         log_print("Initiating association with node %d", con->nodeid);
799
800         if (nodeid_to_addr(con->nodeid, (struct sockaddr *)&rem_addr)) {
801                 log_print("no address for nodeid %d", con->nodeid);
802                 return;
803         }
804         base_con = nodeid2con(0, 0);
805         BUG_ON(base_con == NULL);
806
807         make_sockaddr(&rem_addr, dlm_config.ci_tcp_port, &addrlen);
808
809         outmessage.msg_name = &rem_addr;
810         outmessage.msg_namelen = addrlen;
811         outmessage.msg_control = outcmsg;
812         outmessage.msg_controllen = sizeof(outcmsg);
813         outmessage.msg_flags = MSG_EOR;
814
815         spin_lock(&con->writequeue_lock);
816         e = list_entry(con->writequeue.next, struct writequeue_entry,
817                        list);
818
819         BUG_ON((struct list_head *) e == &con->writequeue);
820
821         len = e->len;
822         offset = e->offset;
823         spin_unlock(&con->writequeue_lock);
824         kmap(e->page);
825
826         /* Send the first block off the write queue */
827         iov[0].iov_base = page_address(e->page)+offset;
828         iov[0].iov_len = len;
829
830         cmsg = CMSG_FIRSTHDR(&outmessage);
831         cmsg->cmsg_level = IPPROTO_SCTP;
832         cmsg->cmsg_type = SCTP_SNDRCV;
833         cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
834         sinfo = CMSG_DATA(cmsg);
835         memset(sinfo, 0x00, sizeof(struct sctp_sndrcvinfo));
836         sinfo->sinfo_ppid = cpu_to_le32(dlm_our_nodeid());
837         outmessage.msg_controllen = cmsg->cmsg_len;
838
839         ret = kernel_sendmsg(base_con->sock, &outmessage, iov, 1, len);
840         if (ret < 0) {
841                 log_print("Send first packet to node %d failed: %d",
842                           con->nodeid, ret);
843
844                 /* Try again later */
845                 clear_bit(CF_CONNECT_PENDING, &con->flags);
846                 clear_bit(CF_INIT_PENDING, &con->flags);
847         }
848         else {
849                 spin_lock(&con->writequeue_lock);
850                 e->offset += ret;
851                 e->len -= ret;
852
853                 if (e->len == 0 && e->users == 0) {
854                         list_del(&e->list);
855                         kunmap(e->page);
856                         free_entry(e);
857                 }
858                 spin_unlock(&con->writequeue_lock);
859         }
860 }
861
862 /* Connect a new socket to its peer */
863 static void tcp_connect_to_sock(struct connection *con)
864 {
865         int result = -EHOSTUNREACH;
866         struct sockaddr_storage saddr;
867         int addr_len;
868         struct socket *sock;
869
870         if (con->nodeid == 0) {
871                 log_print("attempt to connect sock 0 foiled");
872                 return;
873         }
874
875         mutex_lock(&con->sock_mutex);
876         if (con->retries++ > MAX_CONNECT_RETRIES)
877                 goto out;
878
879         /* Some odd races can cause double-connects, ignore them */
880         if (con->sock) {
881                 result = 0;
882                 goto out;
883         }
884
885         /* Create a socket to communicate with */
886         result = sock_create_kern(dlm_local_addr[0]->ss_family, SOCK_STREAM,
887                                   IPPROTO_TCP, &sock);
888         if (result < 0)
889                 goto out_err;
890
891         memset(&saddr, 0, sizeof(saddr));
892         if (dlm_nodeid_to_addr(con->nodeid, &saddr))
893                 goto out_err;
894
895         sock->sk->sk_user_data = con;
896         con->rx_action = receive_from_sock;
897         con->connect_action = tcp_connect_to_sock;
898         add_sock(sock, con);
899
900         make_sockaddr(&saddr, dlm_config.ci_tcp_port, &addr_len);
901
902         log_print("connecting to %d", con->nodeid);
903         result =
904                 sock->ops->connect(sock, (struct sockaddr *)&saddr, addr_len,
905                                    O_NONBLOCK);
906         if (result == -EINPROGRESS)
907                 result = 0;
908         if (result == 0)
909                 goto out;
910
911 out_err:
912         if (con->sock) {
913                 sock_release(con->sock);
914                 con->sock = NULL;
915         }
916         /*
917          * Some errors are fatal and this list might need adjusting. For other
918          * errors we try again until the max number of retries is reached.
919          */
920         if (result != -EHOSTUNREACH && result != -ENETUNREACH &&
921             result != -ENETDOWN && result != EINVAL
922             && result != -EPROTONOSUPPORT) {
923                 lowcomms_connect_sock(con);
924                 result = 0;
925         }
926 out:
927         mutex_unlock(&con->sock_mutex);
928         return;
929 }
930
931 static struct socket *tcp_create_listen_sock(struct connection *con,
932                                              struct sockaddr_storage *saddr)
933 {
934         struct socket *sock = NULL;
935         int result = 0;
936         int one = 1;
937         int addr_len;
938
939         if (dlm_local_addr[0]->ss_family == AF_INET)
940                 addr_len = sizeof(struct sockaddr_in);
941         else
942                 addr_len = sizeof(struct sockaddr_in6);
943
944         /* Create a socket to communicate with */
945         result = sock_create_kern(dlm_local_addr[0]->ss_family, SOCK_STREAM,
946                                   IPPROTO_TCP, &sock);
947         if (result < 0) {
948                 log_print("Can't create listening comms socket");
949                 goto create_out;
950         }
951
952         result = kernel_setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
953                                    (char *)&one, sizeof(one));
954
955         if (result < 0) {
956                 log_print("Failed to set SO_REUSEADDR on socket: %d", result);
957         }
958         sock->sk->sk_user_data = con;
959         con->rx_action = tcp_accept_from_sock;
960         con->connect_action = tcp_connect_to_sock;
961         con->sock = sock;
962
963         /* Bind to our port */
964         make_sockaddr(saddr, dlm_config.ci_tcp_port, &addr_len);
965         result = sock->ops->bind(sock, (struct sockaddr *) saddr, addr_len);
966         if (result < 0) {
967                 log_print("Can't bind to port %d", dlm_config.ci_tcp_port);
968                 sock_release(sock);
969                 sock = NULL;
970                 con->sock = NULL;
971                 goto create_out;
972         }
973         result = kernel_setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE,
974                                  (char *)&one, sizeof(one));
975         if (result < 0) {
976                 log_print("Set keepalive failed: %d", result);
977         }
978
979         result = sock->ops->listen(sock, 5);
980         if (result < 0) {
981                 log_print("Can't listen on port %d", dlm_config.ci_tcp_port);
982                 sock_release(sock);
983                 sock = NULL;
984                 goto create_out;
985         }
986
987 create_out:
988         return sock;
989 }
990
991 /* Get local addresses */
992 static void init_local(void)
993 {
994         struct sockaddr_storage sas, *addr;
995         int i;
996
997         dlm_local_count = 0;
998         for (i = 0; i < DLM_MAX_ADDR_COUNT - 1; i++) {
999                 if (dlm_our_addr(&sas, i))
1000                         break;
1001
1002                 addr = kmalloc(sizeof(*addr), GFP_KERNEL);
1003                 if (!addr)
1004                         break;
1005                 memcpy(addr, &sas, sizeof(*addr));
1006                 dlm_local_addr[dlm_local_count++] = addr;
1007         }
1008 }
1009
1010 /* Bind to an IP address. SCTP allows multiple address so it can do
1011    multi-homing */
1012 static int add_sctp_bind_addr(struct connection *sctp_con,
1013                               struct sockaddr_storage *addr,
1014                               int addr_len, int num)
1015 {
1016         int result = 0;
1017
1018         if (num == 1)
1019                 result = kernel_bind(sctp_con->sock,
1020                                      (struct sockaddr *) addr,
1021                                      addr_len);
1022         else
1023                 result = kernel_setsockopt(sctp_con->sock, SOL_SCTP,
1024                                            SCTP_SOCKOPT_BINDX_ADD,
1025                                            (char *)addr, addr_len);
1026
1027         if (result < 0)
1028                 log_print("Can't bind to port %d addr number %d",
1029                           dlm_config.ci_tcp_port, num);
1030
1031         return result;
1032 }
1033
1034 /* Initialise SCTP socket and bind to all interfaces */
1035 static int sctp_listen_for_all(void)
1036 {
1037         struct socket *sock = NULL;
1038         struct sockaddr_storage localaddr;
1039         struct sctp_event_subscribe subscribe;
1040         int result = -EINVAL, num = 1, i, addr_len;
1041         struct connection *con = nodeid2con(0, GFP_KERNEL);
1042         int bufsize = NEEDED_RMEM;
1043
1044         if (!con)
1045                 return -ENOMEM;
1046
1047         log_print("Using SCTP for communications");
1048
1049         result = sock_create_kern(dlm_local_addr[0]->ss_family, SOCK_SEQPACKET,
1050                                   IPPROTO_SCTP, &sock);
1051         if (result < 0) {
1052                 log_print("Can't create comms socket, check SCTP is loaded");
1053                 goto out;
1054         }
1055
1056         /* Listen for events */
1057         memset(&subscribe, 0, sizeof(subscribe));
1058         subscribe.sctp_data_io_event = 1;
1059         subscribe.sctp_association_event = 1;
1060         subscribe.sctp_send_failure_event = 1;
1061         subscribe.sctp_shutdown_event = 1;
1062         subscribe.sctp_partial_delivery_event = 1;
1063
1064         result = kernel_setsockopt(sock, SOL_SOCKET, SO_RCVBUF,
1065                                  (char *)&bufsize, sizeof(bufsize));
1066         if (result)
1067                 log_print("Error increasing buffer space on socket %d", result);
1068
1069         result = kernel_setsockopt(sock, SOL_SCTP, SCTP_EVENTS,
1070                                    (char *)&subscribe, sizeof(subscribe));
1071         if (result < 0) {
1072                 log_print("Failed to set SCTP_EVENTS on socket: result=%d",
1073                           result);
1074                 goto create_delsock;
1075         }
1076
1077         /* Init con struct */
1078         sock->sk->sk_user_data = con;
1079         con->sock = sock;
1080         con->sock->sk->sk_data_ready = lowcomms_data_ready;
1081         con->rx_action = receive_from_sock;
1082         con->connect_action = sctp_init_assoc;
1083
1084         /* Bind to all interfaces. */
1085         for (i = 0; i < dlm_local_count; i++) {
1086                 memcpy(&localaddr, dlm_local_addr[i], sizeof(localaddr));
1087                 make_sockaddr(&localaddr, dlm_config.ci_tcp_port, &addr_len);
1088
1089                 result = add_sctp_bind_addr(con, &localaddr, addr_len, num);
1090                 if (result)
1091                         goto create_delsock;
1092                 ++num;
1093         }
1094
1095         result = sock->ops->listen(sock, 5);
1096         if (result < 0) {
1097                 log_print("Can't set socket listening");
1098                 goto create_delsock;
1099         }
1100
1101         return 0;
1102
1103 create_delsock:
1104         sock_release(sock);
1105         con->sock = NULL;
1106 out:
1107         return result;
1108 }
1109
1110 static int tcp_listen_for_all(void)
1111 {
1112         struct socket *sock = NULL;
1113         struct connection *con = nodeid2con(0, GFP_KERNEL);
1114         int result = -EINVAL;
1115
1116         if (!con)
1117                 return -ENOMEM;
1118
1119         /* We don't support multi-homed hosts */
1120         if (dlm_local_addr[1] != NULL) {
1121                 log_print("TCP protocol can't handle multi-homed hosts, "
1122                           "try SCTP");
1123                 return -EINVAL;
1124         }
1125
1126         log_print("Using TCP for communications");
1127
1128         set_bit(CF_IS_OTHERCON, &con->flags);
1129
1130         sock = tcp_create_listen_sock(con, dlm_local_addr[0]);
1131         if (sock) {
1132                 add_sock(sock, con);
1133                 result = 0;
1134         }
1135         else {
1136                 result = -EADDRINUSE;
1137         }
1138
1139         return result;
1140 }
1141
1142
1143
1144 static struct writequeue_entry *new_writequeue_entry(struct connection *con,
1145                                                      gfp_t allocation)
1146 {
1147         struct writequeue_entry *entry;
1148
1149         entry = kmalloc(sizeof(struct writequeue_entry), allocation);
1150         if (!entry)
1151                 return NULL;
1152
1153         entry->page = alloc_page(allocation);
1154         if (!entry->page) {
1155                 kfree(entry);
1156                 return NULL;
1157         }
1158
1159         entry->offset = 0;
1160         entry->len = 0;
1161         entry->end = 0;
1162         entry->users = 0;
1163         entry->con = con;
1164
1165         return entry;
1166 }
1167
1168 void *dlm_lowcomms_get_buffer(int nodeid, int len, gfp_t allocation, char **ppc)
1169 {
1170         struct connection *con;
1171         struct writequeue_entry *e;
1172         int offset = 0;
1173         int users = 0;
1174
1175         con = nodeid2con(nodeid, allocation);
1176         if (!con)
1177                 return NULL;
1178
1179         spin_lock(&con->writequeue_lock);
1180         e = list_entry(con->writequeue.prev, struct writequeue_entry, list);
1181         if ((&e->list == &con->writequeue) ||
1182             (PAGE_CACHE_SIZE - e->end < len)) {
1183                 e = NULL;
1184         } else {
1185                 offset = e->end;
1186                 e->end += len;
1187                 users = e->users++;
1188         }
1189         spin_unlock(&con->writequeue_lock);
1190
1191         if (e) {
1192         got_one:
1193                 if (users == 0)
1194                         kmap(e->page);
1195                 *ppc = page_address(e->page) + offset;
1196                 return e;
1197         }
1198
1199         e = new_writequeue_entry(con, allocation);
1200         if (e) {
1201                 spin_lock(&con->writequeue_lock);
1202                 offset = e->end;
1203                 e->end += len;
1204                 users = e->users++;
1205                 list_add_tail(&e->list, &con->writequeue);
1206                 spin_unlock(&con->writequeue_lock);
1207                 goto got_one;
1208         }
1209         return NULL;
1210 }
1211
1212 void dlm_lowcomms_commit_buffer(void *mh)
1213 {
1214         struct writequeue_entry *e = (struct writequeue_entry *)mh;
1215         struct connection *con = e->con;
1216         int users;
1217
1218         spin_lock(&con->writequeue_lock);
1219         users = --e->users;
1220         if (users)
1221                 goto out;
1222         e->len = e->end - e->offset;
1223         kunmap(e->page);
1224         spin_unlock(&con->writequeue_lock);
1225
1226         if (!test_and_set_bit(CF_WRITE_PENDING, &con->flags)) {
1227                 queue_work(send_workqueue, &con->swork);
1228         }
1229         return;
1230
1231 out:
1232         spin_unlock(&con->writequeue_lock);
1233         return;
1234 }
1235
1236 /* Send a message */
1237 static void send_to_sock(struct connection *con)
1238 {
1239         int ret = 0;
1240         ssize_t(*sendpage) (struct socket *, struct page *, int, size_t, int);
1241         const int msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL;
1242         struct writequeue_entry *e;
1243         int len, offset;
1244
1245         mutex_lock(&con->sock_mutex);
1246         if (con->sock == NULL)
1247                 goto out_connect;
1248
1249         sendpage = con->sock->ops->sendpage;
1250
1251         spin_lock(&con->writequeue_lock);
1252         for (;;) {
1253                 e = list_entry(con->writequeue.next, struct writequeue_entry,
1254                                list);
1255                 if ((struct list_head *) e == &con->writequeue)
1256                         break;
1257
1258                 len = e->len;
1259                 offset = e->offset;
1260                 BUG_ON(len == 0 && e->users == 0);
1261                 spin_unlock(&con->writequeue_lock);
1262                 kmap(e->page);
1263
1264                 ret = 0;
1265                 if (len) {
1266                         ret = sendpage(con->sock, e->page, offset, len,
1267                                        msg_flags);
1268                         if (ret == -EAGAIN || ret == 0)
1269                                 goto out;
1270                         if (ret <= 0)
1271                                 goto send_error;
1272                 } else {
1273                         /* Don't starve people filling buffers */
1274                         cond_resched();
1275                 }
1276
1277                 spin_lock(&con->writequeue_lock);
1278                 e->offset += ret;
1279                 e->len -= ret;
1280
1281                 if (e->len == 0 && e->users == 0) {
1282                         list_del(&e->list);
1283                         kunmap(e->page);
1284                         free_entry(e);
1285                         continue;
1286                 }
1287         }
1288         spin_unlock(&con->writequeue_lock);
1289 out:
1290         mutex_unlock(&con->sock_mutex);
1291         return;
1292
1293 send_error:
1294         mutex_unlock(&con->sock_mutex);
1295         close_connection(con, false);
1296         lowcomms_connect_sock(con);
1297         return;
1298
1299 out_connect:
1300         mutex_unlock(&con->sock_mutex);
1301         if (!test_bit(CF_INIT_PENDING, &con->flags))
1302                 lowcomms_connect_sock(con);
1303         return;
1304 }
1305
1306 static void clean_one_writequeue(struct connection *con)
1307 {
1308         struct list_head *list;
1309         struct list_head *temp;
1310
1311         spin_lock(&con->writequeue_lock);
1312         list_for_each_safe(list, temp, &con->writequeue) {
1313                 struct writequeue_entry *e =
1314                         list_entry(list, struct writequeue_entry, list);
1315                 list_del(&e->list);
1316                 free_entry(e);
1317         }
1318         spin_unlock(&con->writequeue_lock);
1319 }
1320
1321 /* Called from recovery when it knows that a node has
1322    left the cluster */
1323 int dlm_lowcomms_close(int nodeid)
1324 {
1325         struct connection *con;
1326
1327         log_print("closing connection to node %d", nodeid);
1328         con = nodeid2con(nodeid, 0);
1329         if (con) {
1330                 clean_one_writequeue(con);
1331                 close_connection(con, true);
1332         }
1333         return 0;
1334 }
1335
1336 /* Receive workqueue function */
1337 static void process_recv_sockets(struct work_struct *work)
1338 {
1339         struct connection *con = container_of(work, struct connection, rwork);
1340         int err;
1341
1342         clear_bit(CF_READ_PENDING, &con->flags);
1343         do {
1344                 err = con->rx_action(con);
1345         } while (!err);
1346 }
1347
1348 /* Send workqueue function */
1349 static void process_send_sockets(struct work_struct *work)
1350 {
1351         struct connection *con = container_of(work, struct connection, swork);
1352
1353         if (test_and_clear_bit(CF_CONNECT_PENDING, &con->flags)) {
1354                 con->connect_action(con);
1355         }
1356         clear_bit(CF_WRITE_PENDING, &con->flags);
1357         send_to_sock(con);
1358 }
1359
1360
1361 /* Discard all entries on the write queues */
1362 static void clean_writequeues(void)
1363 {
1364         int nodeid;
1365
1366         for (nodeid = 1; nodeid <= max_nodeid; nodeid++) {
1367                 struct connection *con = __nodeid2con(nodeid, 0);
1368
1369                 if (con)
1370                         clean_one_writequeue(con);
1371         }
1372 }
1373
1374 static void work_stop(void)
1375 {
1376         destroy_workqueue(recv_workqueue);
1377         destroy_workqueue(send_workqueue);
1378 }
1379
1380 static int work_start(void)
1381 {
1382         int error;
1383         recv_workqueue = create_workqueue("dlm_recv");
1384         error = IS_ERR(recv_workqueue);
1385         if (error) {
1386                 log_print("can't start dlm_recv %d", error);
1387                 return error;
1388         }
1389
1390         send_workqueue = create_singlethread_workqueue("dlm_send");
1391         error = IS_ERR(send_workqueue);
1392         if (error) {
1393                 log_print("can't start dlm_send %d", error);
1394                 destroy_workqueue(recv_workqueue);
1395                 return error;
1396         }
1397
1398         return 0;
1399 }
1400
1401 void dlm_lowcomms_stop(void)
1402 {
1403         int i;
1404         struct connection *con;
1405
1406         /* Set all the flags to prevent any
1407            socket activity.
1408         */
1409         down(&connections_lock);
1410         for (i = 0; i <= max_nodeid; i++) {
1411                 con = __nodeid2con(i, 0);
1412                 if (con) {
1413                         con->flags |= 0xFF;
1414                         if (con->sock)
1415                                 con->sock->sk->sk_user_data = NULL;
1416                 }
1417         }
1418         up(&connections_lock);
1419
1420         work_stop();
1421
1422         down(&connections_lock);
1423         clean_writequeues();
1424
1425         for (i = 0; i <= max_nodeid; i++) {
1426                 con = __nodeid2con(i, 0);
1427                 if (con) {
1428                         close_connection(con, true);
1429                         if (con->othercon)
1430                                 kmem_cache_free(con_cache, con->othercon);
1431                         kmem_cache_free(con_cache, con);
1432                 }
1433         }
1434         max_nodeid = 0;
1435         up(&connections_lock);
1436         kmem_cache_destroy(con_cache);
1437         idr_init(&connections_idr);
1438 }
1439
1440 int dlm_lowcomms_start(void)
1441 {
1442         int error = -EINVAL;
1443         struct connection *con;
1444
1445         init_local();
1446         if (!dlm_local_count) {
1447                 error = -ENOTCONN;
1448                 log_print("no local IP address has been set");
1449                 goto out;
1450         }
1451
1452         error = -ENOMEM;
1453         con_cache = kmem_cache_create("dlm_conn", sizeof(struct connection),
1454                                       __alignof__(struct connection), 0,
1455                                       NULL);
1456         if (!con_cache)
1457                 goto out;
1458
1459         /* Set some sysctl minima */
1460         if (sysctl_rmem_max < NEEDED_RMEM)
1461                 sysctl_rmem_max = NEEDED_RMEM;
1462
1463         /* Start listening */
1464         if (dlm_config.ci_protocol == 0)
1465                 error = tcp_listen_for_all();
1466         else
1467                 error = sctp_listen_for_all();
1468         if (error)
1469                 goto fail_unlisten;
1470
1471         error = work_start();
1472         if (error)
1473                 goto fail_unlisten;
1474
1475         return 0;
1476
1477 fail_unlisten:
1478         con = nodeid2con(0,0);
1479         if (con) {
1480                 close_connection(con, false);
1481                 kmem_cache_free(con_cache, con);
1482         }
1483         kmem_cache_destroy(con_cache);
1484
1485 out:
1486         return error;
1487 }