~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/fs/dlm/lowcomms.c

Version: ~ [ linux-5.9 ] ~ [ linux-5.8.14 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.70 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.150 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.200 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.238 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.238 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.85 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp