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