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