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