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