blob: 56015c9e8d00a0c7c3dcb983183a1f3d6f9a14d2 [file] [log] [blame]
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001/******************************************************************************
2*******************************************************************************
3**
4** Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
Christine Caulfield5e9ccc32009-01-28 12:57:40 -06005** Copyright (C) 2004-2009 Red Hat, Inc. All rights reserved.
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01006**
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
Joe Perches2cf12c02009-01-22 13:26:47 -080024 * be expanded for the cluster infrastructure then that is its
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +010025 * 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 *
Joe Perches2cf12c02009-01-22 13:26:47 -080039 * lowcomms will choose to use either TCP or SCTP as its transport layer
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +010040 * depending on the configuration variable 'protocol'. This should be set
Joe Perches2cf12c02009-01-22 13:26:47 -080041 * to 0 (default) for TCP or 1 for SCTP. It should be configured using a
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +010042 * cluster-wide mechanism as it must be the same on all nodes of the cluster
43 * for the DLM to function.
44 *
45 */
46
47#include <asm/ioctls.h>
48#include <net/sock.h>
49#include <net/tcp.h>
50#include <linux/pagemap.h>
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +010051#include <linux/file.h>
Matthias Kaehlcke7a936ce2008-05-12 10:04:51 -050052#include <linux/mutex.h>
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +010053#include <linux/sctp.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090054#include <linux/slab.h>
Daniel Borkmann1b866432013-04-09 05:54:01 +000055#include <linux/sctp.h>
Benjamin Poirier2f2d76c2012-03-08 05:55:59 +000056#include <net/sctp/sctp.h>
Joe Perches44ad5322009-01-22 13:24:49 -080057#include <net/ipv6.h>
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +010058
59#include "dlm_internal.h"
60#include "lowcomms.h"
61#include "midcomms.h"
62#include "config.h"
63
64#define NEEDED_RMEM (4*1024*1024)
Christine Caulfield5e9ccc32009-01-28 12:57:40 -060065#define CONN_HASH_SIZE 32
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +010066
Bob Petersonf92c8dd2010-11-12 11:15:20 -060067/* Number of messages to send before rescheduling */
68#define MAX_SEND_MSG_COUNT 25
69
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +010070struct cbuf {
71 unsigned int base;
72 unsigned int len;
73 unsigned int mask;
74};
75
76static void cbuf_add(struct cbuf *cb, int n)
77{
78 cb->len += n;
79}
80
81static int cbuf_data(struct cbuf *cb)
82{
83 return ((cb->base + cb->len) & cb->mask);
84}
85
86static void cbuf_init(struct cbuf *cb, int size)
87{
88 cb->base = cb->len = 0;
89 cb->mask = size-1;
90}
91
92static void cbuf_eat(struct cbuf *cb, int n)
93{
94 cb->len -= n;
95 cb->base += n;
96 cb->base &= cb->mask;
97}
98
99static bool cbuf_empty(struct cbuf *cb)
100{
101 return cb->len == 0;
102}
103
104struct connection {
105 struct socket *sock; /* NULL if not connected */
106 uint32_t nodeid; /* So we know who we are in the list */
107 struct mutex sock_mutex;
108 unsigned long flags;
109#define CF_READ_PENDING 1
110#define CF_WRITE_PENDING 2
111#define CF_CONNECT_PENDING 3
112#define CF_INIT_PENDING 4
113#define CF_IS_OTHERCON 5
Lars Marowsky-Bree063c4c92009-08-11 16:18:23 -0500114#define CF_CLOSE 6
David Millerb36930d2010-11-10 21:56:39 -0800115#define CF_APP_LIMITED 7
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100116 struct list_head writequeue; /* List of outgoing writequeue_entries */
117 spinlock_t writequeue_lock;
118 int (*rx_action) (struct connection *); /* What to do when active */
119 void (*connect_action) (struct connection *); /* What to do to connect */
120 struct page *rx_page;
121 struct cbuf cb;
122 int retries;
123#define MAX_CONNECT_RETRIES 3
124 int sctp_assoc;
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600125 struct hlist_node list;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100126 struct connection *othercon;
127 struct work_struct rwork; /* Receive workqueue */
128 struct work_struct swork; /* Send workqueue */
Mike Christie98e1b602013-06-14 04:56:12 -0500129 bool try_new_addr;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100130};
131#define sock2con(x) ((struct connection *)(x)->sk_user_data)
132
133/* An entry waiting to be sent */
134struct writequeue_entry {
135 struct list_head list;
136 struct page *page;
137 int offset;
138 int len;
139 int end;
140 int users;
141 struct connection *con;
142};
143
David Teigland36b71a82012-07-26 12:44:30 -0500144struct dlm_node_addr {
145 struct list_head list;
146 int nodeid;
147 int addr_count;
Mike Christie98e1b602013-06-14 04:56:12 -0500148 int curr_addr_index;
David Teigland36b71a82012-07-26 12:44:30 -0500149 struct sockaddr_storage *addr[DLM_MAX_ADDR_COUNT];
150};
151
152static LIST_HEAD(dlm_node_addrs);
153static DEFINE_SPINLOCK(dlm_node_addrs_spin);
154
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100155static struct sockaddr_storage *dlm_local_addr[DLM_MAX_ADDR_COUNT];
156static int dlm_local_count;
David Teigland513ef592012-03-30 11:46:08 -0500157static int dlm_allow_conn;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100158
159/* Work queues */
160static struct workqueue_struct *recv_workqueue;
161static struct workqueue_struct *send_workqueue;
162
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600163static struct hlist_head connection_hash[CONN_HASH_SIZE];
Matthias Kaehlcke7a936ce2008-05-12 10:04:51 -0500164static DEFINE_MUTEX(connections_lock);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100165static struct kmem_cache *con_cache;
166
167static void process_recv_sockets(struct work_struct *work);
168static void process_send_sockets(struct work_struct *work);
169
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600170
171/* This is deliberately very simple because most clusters have simple
172 sequential nodeids, so we should be able to go straight to a connection
173 struct in the array */
174static inline int nodeid_hash(int nodeid)
175{
176 return nodeid & (CONN_HASH_SIZE-1);
177}
178
179static struct connection *__find_con(int nodeid)
180{
181 int r;
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600182 struct connection *con;
183
184 r = nodeid_hash(nodeid);
185
Sasha Levinb67bfe02013-02-27 17:06:00 -0800186 hlist_for_each_entry(con, &connection_hash[r], list) {
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600187 if (con->nodeid == nodeid)
188 return con;
189 }
190 return NULL;
191}
192
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100193/*
194 * If 'allocation' is zero then we don't attempt to create a new
195 * connection structure for this node.
196 */
197static struct connection *__nodeid2con(int nodeid, gfp_t alloc)
198{
199 struct connection *con = NULL;
200 int r;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100201
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600202 con = __find_con(nodeid);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100203 if (con || !alloc)
204 return con;
205
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100206 con = kmem_cache_zalloc(con_cache, alloc);
207 if (!con)
208 return NULL;
209
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600210 r = nodeid_hash(nodeid);
211 hlist_add_head(&con->list, &connection_hash[r]);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100212
213 con->nodeid = nodeid;
214 mutex_init(&con->sock_mutex);
215 INIT_LIST_HEAD(&con->writequeue);
216 spin_lock_init(&con->writequeue_lock);
217 INIT_WORK(&con->swork, process_send_sockets);
218 INIT_WORK(&con->rwork, process_recv_sockets);
219
220 /* Setup action pointers for child sockets */
221 if (con->nodeid) {
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600222 struct connection *zerocon = __find_con(0);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100223
224 con->connect_action = zerocon->connect_action;
225 if (!con->rx_action)
226 con->rx_action = zerocon->rx_action;
227 }
228
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100229 return con;
230}
231
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600232/* Loop round all connections */
233static void foreach_conn(void (*conn_func)(struct connection *c))
234{
235 int i;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800236 struct hlist_node *n;
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600237 struct connection *con;
238
239 for (i = 0; i < CONN_HASH_SIZE; i++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800240 hlist_for_each_entry_safe(con, n, &connection_hash[i], list)
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600241 conn_func(con);
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600242 }
243}
244
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100245static struct connection *nodeid2con(int nodeid, gfp_t allocation)
246{
247 struct connection *con;
248
Matthias Kaehlcke7a936ce2008-05-12 10:04:51 -0500249 mutex_lock(&connections_lock);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100250 con = __nodeid2con(nodeid, allocation);
Matthias Kaehlcke7a936ce2008-05-12 10:04:51 -0500251 mutex_unlock(&connections_lock);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100252
253 return con;
254}
255
256/* This is a bit drastic, but only called when things go wrong */
257static struct connection *assoc2con(int assoc_id)
258{
259 int i;
260 struct connection *con;
261
Matthias Kaehlcke7a936ce2008-05-12 10:04:51 -0500262 mutex_lock(&connections_lock);
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600263
264 for (i = 0 ; i < CONN_HASH_SIZE; i++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800265 hlist_for_each_entry(con, &connection_hash[i], list) {
Julia Lawallf70cb332010-08-03 23:34:16 +0200266 if (con->sctp_assoc == assoc_id) {
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600267 mutex_unlock(&connections_lock);
268 return con;
269 }
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100270 }
271 }
Matthias Kaehlcke7a936ce2008-05-12 10:04:51 -0500272 mutex_unlock(&connections_lock);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100273 return NULL;
274}
275
David Teigland36b71a82012-07-26 12:44:30 -0500276static struct dlm_node_addr *find_node_addr(int nodeid)
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100277{
David Teigland36b71a82012-07-26 12:44:30 -0500278 struct dlm_node_addr *na;
279
280 list_for_each_entry(na, &dlm_node_addrs, list) {
281 if (na->nodeid == nodeid)
282 return na;
283 }
284 return NULL;
285}
286
287static int addr_compare(struct sockaddr_storage *x, struct sockaddr_storage *y)
288{
289 switch (x->ss_family) {
290 case AF_INET: {
291 struct sockaddr_in *sinx = (struct sockaddr_in *)x;
292 struct sockaddr_in *siny = (struct sockaddr_in *)y;
293 if (sinx->sin_addr.s_addr != siny->sin_addr.s_addr)
294 return 0;
295 if (sinx->sin_port != siny->sin_port)
296 return 0;
297 break;
298 }
299 case AF_INET6: {
300 struct sockaddr_in6 *sinx = (struct sockaddr_in6 *)x;
301 struct sockaddr_in6 *siny = (struct sockaddr_in6 *)y;
302 if (!ipv6_addr_equal(&sinx->sin6_addr, &siny->sin6_addr))
303 return 0;
304 if (sinx->sin6_port != siny->sin6_port)
305 return 0;
306 break;
307 }
308 default:
309 return 0;
310 }
311 return 1;
312}
313
314static int nodeid_to_addr(int nodeid, struct sockaddr_storage *sas_out,
Mike Christie98e1b602013-06-14 04:56:12 -0500315 struct sockaddr *sa_out, bool try_new_addr)
David Teigland36b71a82012-07-26 12:44:30 -0500316{
317 struct sockaddr_storage sas;
318 struct dlm_node_addr *na;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100319
320 if (!dlm_local_count)
321 return -1;
322
David Teigland36b71a82012-07-26 12:44:30 -0500323 spin_lock(&dlm_node_addrs_spin);
324 na = find_node_addr(nodeid);
Mike Christie98e1b602013-06-14 04:56:12 -0500325 if (na && na->addr_count) {
326 if (try_new_addr) {
327 na->curr_addr_index++;
328 if (na->curr_addr_index == na->addr_count)
329 na->curr_addr_index = 0;
330 }
331
332 memcpy(&sas, na->addr[na->curr_addr_index ],
333 sizeof(struct sockaddr_storage));
334 }
David Teigland36b71a82012-07-26 12:44:30 -0500335 spin_unlock(&dlm_node_addrs_spin);
336
337 if (!na)
338 return -EEXIST;
339
340 if (!na->addr_count)
341 return -ENOENT;
342
343 if (sas_out)
344 memcpy(sas_out, &sas, sizeof(struct sockaddr_storage));
345
346 if (!sa_out)
347 return 0;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100348
349 if (dlm_local_addr[0]->ss_family == AF_INET) {
David Teigland36b71a82012-07-26 12:44:30 -0500350 struct sockaddr_in *in4 = (struct sockaddr_in *) &sas;
351 struct sockaddr_in *ret4 = (struct sockaddr_in *) sa_out;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100352 ret4->sin_addr.s_addr = in4->sin_addr.s_addr;
353 } else {
David Teigland36b71a82012-07-26 12:44:30 -0500354 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) &sas;
355 struct sockaddr_in6 *ret6 = (struct sockaddr_in6 *) sa_out;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000356 ret6->sin6_addr = in6->sin6_addr;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100357 }
358
359 return 0;
360}
361
David Teigland36b71a82012-07-26 12:44:30 -0500362static int addr_to_nodeid(struct sockaddr_storage *addr, int *nodeid)
363{
364 struct dlm_node_addr *na;
365 int rv = -EEXIST;
Mike Christie98e1b602013-06-14 04:56:12 -0500366 int addr_i;
David Teigland36b71a82012-07-26 12:44:30 -0500367
368 spin_lock(&dlm_node_addrs_spin);
369 list_for_each_entry(na, &dlm_node_addrs, list) {
370 if (!na->addr_count)
371 continue;
372
Mike Christie98e1b602013-06-14 04:56:12 -0500373 for (addr_i = 0; addr_i < na->addr_count; addr_i++) {
374 if (addr_compare(na->addr[addr_i], addr)) {
375 *nodeid = na->nodeid;
376 rv = 0;
377 goto unlock;
378 }
379 }
David Teigland36b71a82012-07-26 12:44:30 -0500380 }
Mike Christie98e1b602013-06-14 04:56:12 -0500381unlock:
David Teigland36b71a82012-07-26 12:44:30 -0500382 spin_unlock(&dlm_node_addrs_spin);
383 return rv;
384}
385
386int dlm_lowcomms_addr(int nodeid, struct sockaddr_storage *addr, int len)
387{
388 struct sockaddr_storage *new_addr;
389 struct dlm_node_addr *new_node, *na;
390
391 new_node = kzalloc(sizeof(struct dlm_node_addr), GFP_NOFS);
392 if (!new_node)
393 return -ENOMEM;
394
395 new_addr = kzalloc(sizeof(struct sockaddr_storage), GFP_NOFS);
396 if (!new_addr) {
397 kfree(new_node);
398 return -ENOMEM;
399 }
400
401 memcpy(new_addr, addr, len);
402
403 spin_lock(&dlm_node_addrs_spin);
404 na = find_node_addr(nodeid);
405 if (!na) {
406 new_node->nodeid = nodeid;
407 new_node->addr[0] = new_addr;
408 new_node->addr_count = 1;
409 list_add(&new_node->list, &dlm_node_addrs);
410 spin_unlock(&dlm_node_addrs_spin);
411 return 0;
412 }
413
414 if (na->addr_count >= DLM_MAX_ADDR_COUNT) {
415 spin_unlock(&dlm_node_addrs_spin);
416 kfree(new_addr);
417 kfree(new_node);
418 return -ENOSPC;
419 }
420
421 na->addr[na->addr_count++] = new_addr;
422 spin_unlock(&dlm_node_addrs_spin);
423 kfree(new_node);
424 return 0;
425}
426
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100427/* Data available on socket or listen socket received a connect */
428static void lowcomms_data_ready(struct sock *sk, int count_unused)
429{
430 struct connection *con = sock2con(sk);
Patrick Caulfieldafb853f2007-06-01 10:07:26 -0500431 if (con && !test_and_set_bit(CF_READ_PENDING, &con->flags))
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100432 queue_work(recv_workqueue, &con->rwork);
433}
434
435static void lowcomms_write_space(struct sock *sk)
436{
437 struct connection *con = sock2con(sk);
438
David Millerb36930d2010-11-10 21:56:39 -0800439 if (!con)
440 return;
441
442 clear_bit(SOCK_NOSPACE, &con->sock->flags);
443
444 if (test_and_clear_bit(CF_APP_LIMITED, &con->flags)) {
445 con->sock->sk->sk_write_pending--;
446 clear_bit(SOCK_ASYNC_NOSPACE, &con->sock->flags);
447 }
448
449 if (!test_and_set_bit(CF_WRITE_PENDING, &con->flags))
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100450 queue_work(send_workqueue, &con->swork);
451}
452
453static inline void lowcomms_connect_sock(struct connection *con)
454{
Lars Marowsky-Bree063c4c92009-08-11 16:18:23 -0500455 if (test_bit(CF_CLOSE, &con->flags))
456 return;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100457 if (!test_and_set_bit(CF_CONNECT_PENDING, &con->flags))
458 queue_work(send_workqueue, &con->swork);
459}
460
461static void lowcomms_state_change(struct sock *sk)
462{
463 if (sk->sk_state == TCP_ESTABLISHED)
464 lowcomms_write_space(sk);
465}
466
Christine Caulfield391fbdc2009-05-07 10:54:16 -0500467int dlm_lowcomms_connect_node(int nodeid)
468{
469 struct connection *con;
470
David Teigland04bedd72009-09-18 14:31:47 -0500471 /* with sctp there's no connecting without sending */
472 if (dlm_config.ci_protocol != 0)
473 return 0;
474
Christine Caulfield391fbdc2009-05-07 10:54:16 -0500475 if (nodeid == dlm_our_nodeid())
476 return 0;
477
478 con = nodeid2con(nodeid, GFP_NOFS);
479 if (!con)
480 return -ENOMEM;
481 lowcomms_connect_sock(con);
482 return 0;
483}
484
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100485/* Make a socket active */
Ying Xue4dd40f02012-08-10 14:58:42 +0800486static void add_sock(struct socket *sock, struct connection *con)
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100487{
488 con->sock = sock;
489
490 /* Install a data_ready callback */
491 con->sock->sk->sk_data_ready = lowcomms_data_ready;
492 con->sock->sk->sk_write_space = lowcomms_write_space;
493 con->sock->sk->sk_state_change = lowcomms_state_change;
494 con->sock->sk->sk_user_data = con;
Steven Whitehoused6d7b702008-11-12 16:49:48 -0600495 con->sock->sk->sk_allocation = GFP_NOFS;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100496}
497
498/* Add the port number to an IPv6 or 4 sockaddr and return the address
499 length */
500static void make_sockaddr(struct sockaddr_storage *saddr, uint16_t port,
501 int *addr_len)
502{
503 saddr->ss_family = dlm_local_addr[0]->ss_family;
504 if (saddr->ss_family == AF_INET) {
505 struct sockaddr_in *in4_addr = (struct sockaddr_in *)saddr;
506 in4_addr->sin_port = cpu_to_be16(port);
507 *addr_len = sizeof(struct sockaddr_in);
508 memset(&in4_addr->sin_zero, 0, sizeof(in4_addr->sin_zero));
509 } else {
510 struct sockaddr_in6 *in6_addr = (struct sockaddr_in6 *)saddr;
511 in6_addr->sin6_port = cpu_to_be16(port);
512 *addr_len = sizeof(struct sockaddr_in6);
513 }
Patrick Caulfield01c8cab2007-07-17 16:53:15 +0100514 memset((char *)saddr + *addr_len, 0, sizeof(struct sockaddr_storage) - *addr_len);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100515}
516
517/* Close a remote connection and tidy up */
518static void close_connection(struct connection *con, bool and_other)
519{
520 mutex_lock(&con->sock_mutex);
521
522 if (con->sock) {
523 sock_release(con->sock);
524 con->sock = NULL;
525 }
526 if (con->othercon && and_other) {
527 /* Will only re-enter once. */
528 close_connection(con->othercon, false);
529 }
530 if (con->rx_page) {
531 __free_page(con->rx_page);
532 con->rx_page = NULL;
533 }
Patrick Caulfield9e5f2822007-08-02 14:58:14 +0100534
Patrick Caulfield61d96be2007-08-20 15:13:38 +0100535 con->retries = 0;
536 mutex_unlock(&con->sock_mutex);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100537}
538
539/* We only send shutdown messages to nodes that are not part of the cluster */
540static void sctp_send_shutdown(sctp_assoc_t associd)
541{
542 static char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
543 struct msghdr outmessage;
544 struct cmsghdr *cmsg;
545 struct sctp_sndrcvinfo *sinfo;
546 int ret;
547 struct connection *con;
548
549 con = nodeid2con(0,0);
550 BUG_ON(con == NULL);
551
552 outmessage.msg_name = NULL;
553 outmessage.msg_namelen = 0;
554 outmessage.msg_control = outcmsg;
555 outmessage.msg_controllen = sizeof(outcmsg);
556 outmessage.msg_flags = MSG_EOR;
557
558 cmsg = CMSG_FIRSTHDR(&outmessage);
559 cmsg->cmsg_level = IPPROTO_SCTP;
560 cmsg->cmsg_type = SCTP_SNDRCV;
561 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
562 outmessage.msg_controllen = cmsg->cmsg_len;
563 sinfo = CMSG_DATA(cmsg);
564 memset(sinfo, 0x00, sizeof(struct sctp_sndrcvinfo));
565
566 sinfo->sinfo_flags |= MSG_EOF;
567 sinfo->sinfo_assoc_id = associd;
568
569 ret = kernel_sendmsg(con->sock, &outmessage, NULL, 0, 0);
570
571 if (ret != 0)
572 log_print("send EOF to node failed: %d", ret);
573}
574
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600575static void sctp_init_failed_foreach(struct connection *con)
576{
Mike Christie98e1b602013-06-14 04:56:12 -0500577
578 /*
579 * Don't try to recover base con and handle race where the
580 * other node's assoc init creates a assoc and we get that
581 * notification, then we get a notification that our attempt
582 * failed due. This happens when we are still trying the primary
583 * address, but the other node has already tried secondary addrs
584 * and found one that worked.
585 */
586 if (!con->nodeid || con->sctp_assoc)
587 return;
588
589 log_print("Retrying SCTP association init for node %d\n", con->nodeid);
590
591 con->try_new_addr = true;
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600592 con->sctp_assoc = 0;
Mike Christieb390ca32013-06-14 04:56:11 -0500593 if (test_and_clear_bit(CF_INIT_PENDING, &con->flags)) {
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600594 if (!test_and_set_bit(CF_WRITE_PENDING, &con->flags))
595 queue_work(send_workqueue, &con->swork);
596 }
597}
598
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100599/* INIT failed but we don't know which node...
600 restart INIT on all pending nodes */
601static void sctp_init_failed(void)
602{
Matthias Kaehlcke7a936ce2008-05-12 10:04:51 -0500603 mutex_lock(&connections_lock);
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600604
605 foreach_conn(sctp_init_failed_foreach);
606
Matthias Kaehlcke7a936ce2008-05-12 10:04:51 -0500607 mutex_unlock(&connections_lock);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100608}
609
610/* Something happened to an association */
David Teigland617e82e2007-04-26 13:46:49 -0500611static void process_sctp_notification(struct connection *con,
612 struct msghdr *msg, char *buf)
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100613{
614 union sctp_notification *sn = (union sctp_notification *)buf;
615
616 if (sn->sn_header.sn_type == SCTP_ASSOC_CHANGE) {
617 switch (sn->sn_assoc_change.sac_state) {
618
619 case SCTP_COMM_UP:
620 case SCTP_RESTART:
621 {
622 /* Check that the new node is in the lockspace */
623 struct sctp_prim prim;
624 int nodeid;
625 int prim_len, ret;
626 int addr_len;
627 struct connection *new_con;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100628
629 /*
630 * We get this before any data for an association.
631 * We verify that the node is in the cluster and
632 * then peel off a socket for it.
633 */
634 if ((int)sn->sn_assoc_change.sac_assoc_id <= 0) {
635 log_print("COMM_UP for invalid assoc ID %d",
David Teigland617e82e2007-04-26 13:46:49 -0500636 (int)sn->sn_assoc_change.sac_assoc_id);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100637 sctp_init_failed();
638 return;
639 }
640 memset(&prim, 0, sizeof(struct sctp_prim));
641 prim_len = sizeof(struct sctp_prim);
642 prim.ssp_assoc_id = sn->sn_assoc_change.sac_assoc_id;
643
644 ret = kernel_getsockopt(con->sock,
645 IPPROTO_SCTP,
646 SCTP_PRIMARY_ADDR,
647 (char*)&prim,
648 &prim_len);
649 if (ret < 0) {
650 log_print("getsockopt/sctp_primary_addr on "
651 "new assoc %d failed : %d",
652 (int)sn->sn_assoc_change.sac_assoc_id,
653 ret);
654
655 /* Retry INIT later */
656 new_con = assoc2con(sn->sn_assoc_change.sac_assoc_id);
657 if (new_con)
658 clear_bit(CF_CONNECT_PENDING, &con->flags);
659 return;
660 }
661 make_sockaddr(&prim.ssp_addr, 0, &addr_len);
David Teigland36b71a82012-07-26 12:44:30 -0500662 if (addr_to_nodeid(&prim.ssp_addr, &nodeid)) {
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100663 unsigned char *b=(unsigned char *)&prim.ssp_addr;
664 log_print("reject connect from unknown addr");
Masatake YAMATObcaadf52011-07-04 12:25:51 +0900665 print_hex_dump_bytes("ss: ", DUMP_PREFIX_NONE,
666 b, sizeof(struct sockaddr_storage));
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100667 sctp_send_shutdown(prim.ssp_assoc_id);
668 return;
669 }
670
David Teigland748285c2009-05-15 10:50:57 -0500671 new_con = nodeid2con(nodeid, GFP_NOFS);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100672 if (!new_con)
673 return;
674
675 /* Peel off a new sock */
Benjamin Poirier2f2d76c2012-03-08 05:55:59 +0000676 sctp_lock_sock(con->sock->sk);
677 ret = sctp_do_peeloff(con->sock->sk,
678 sn->sn_assoc_change.sac_assoc_id,
679 &new_con->sock);
680 sctp_release_sock(con->sock->sk);
David Teigland6861f352009-09-24 15:58:23 -0500681 if (ret < 0) {
David Teigland617e82e2007-04-26 13:46:49 -0500682 log_print("Can't peel off a socket for "
David Teigland6861f352009-09-24 15:58:23 -0500683 "connection %d to node %d: err=%d",
Benjamin Poirier2f2d76c2012-03-08 05:55:59 +0000684 (int)sn->sn_assoc_change.sac_assoc_id,
685 nodeid, ret);
David Teigland6861f352009-09-24 15:58:23 -0500686 return;
687 }
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100688 add_sock(new_con->sock, new_con);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100689
David Teigland6861f352009-09-24 15:58:23 -0500690 log_print("connecting to %d sctp association %d",
691 nodeid, (int)sn->sn_assoc_change.sac_assoc_id);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100692
Mike Christiee1631d02013-06-14 04:56:10 -0500693 new_con->sctp_assoc = sn->sn_assoc_change.sac_assoc_id;
Mike Christie98e1b602013-06-14 04:56:12 -0500694 new_con->try_new_addr = false;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100695 /* Send any pending writes */
696 clear_bit(CF_CONNECT_PENDING, &new_con->flags);
Mike Christieefad7e62013-06-14 04:56:09 -0500697 clear_bit(CF_INIT_PENDING, &new_con->flags);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100698 if (!test_and_set_bit(CF_WRITE_PENDING, &new_con->flags)) {
699 queue_work(send_workqueue, &new_con->swork);
700 }
701 if (!test_and_set_bit(CF_READ_PENDING, &new_con->flags))
702 queue_work(recv_workqueue, &new_con->rwork);
703 }
704 break;
705
706 case SCTP_COMM_LOST:
707 case SCTP_SHUTDOWN_COMP:
708 {
709 con = assoc2con(sn->sn_assoc_change.sac_assoc_id);
710 if (con) {
711 con->sctp_assoc = 0;
712 }
713 }
714 break;
715
716 /* We don't know which INIT failed, so clear the PENDING flags
717 * on them all. if assoc_id is zero then it will then try
718 * again */
719
720 case SCTP_CANT_STR_ASSOC:
721 {
722 log_print("Can't start SCTP association - retrying");
723 sctp_init_failed();
724 }
725 break;
726
727 default:
728 log_print("unexpected SCTP assoc change id=%d state=%d",
729 (int)sn->sn_assoc_change.sac_assoc_id,
730 sn->sn_assoc_change.sac_state);
731 }
732 }
733}
734
735/* Data received from remote end */
736static int receive_from_sock(struct connection *con)
737{
738 int ret = 0;
739 struct msghdr msg = {};
740 struct kvec iov[2];
741 unsigned len;
742 int r;
743 int call_again_soon = 0;
744 int nvec;
745 char incmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
746
747 mutex_lock(&con->sock_mutex);
748
749 if (con->sock == NULL) {
750 ret = -EAGAIN;
751 goto out_close;
752 }
753
754 if (con->rx_page == NULL) {
755 /*
756 * This doesn't need to be atomic, but I think it should
757 * improve performance if it is.
758 */
759 con->rx_page = alloc_page(GFP_ATOMIC);
760 if (con->rx_page == NULL)
761 goto out_resched;
762 cbuf_init(&con->cb, PAGE_CACHE_SIZE);
763 }
764
765 /* Only SCTP needs these really */
766 memset(&incmsg, 0, sizeof(incmsg));
767 msg.msg_control = incmsg;
768 msg.msg_controllen = sizeof(incmsg);
769
770 /*
771 * iov[0] is the bit of the circular buffer between the current end
772 * point (cb.base + cb.len) and the end of the buffer.
773 */
774 iov[0].iov_len = con->cb.base - cbuf_data(&con->cb);
775 iov[0].iov_base = page_address(con->rx_page) + cbuf_data(&con->cb);
776 iov[1].iov_len = 0;
777 nvec = 1;
778
779 /*
780 * iov[1] is the bit of the circular buffer between the start of the
781 * buffer and the start of the currently used section (cb.base)
782 */
783 if (cbuf_data(&con->cb) >= con->cb.base) {
784 iov[0].iov_len = PAGE_CACHE_SIZE - cbuf_data(&con->cb);
785 iov[1].iov_len = con->cb.base;
786 iov[1].iov_base = page_address(con->rx_page);
787 nvec = 2;
788 }
789 len = iov[0].iov_len + iov[1].iov_len;
790
791 r = ret = kernel_recvmsg(con->sock, &msg, iov, nvec, len,
792 MSG_DONTWAIT | MSG_NOSIGNAL);
793 if (ret <= 0)
794 goto out_close;
795
796 /* Process SCTP notifications */
797 if (msg.msg_flags & MSG_NOTIFICATION) {
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100798 msg.msg_control = incmsg;
799 msg.msg_controllen = sizeof(incmsg);
800
801 process_sctp_notification(con, &msg,
David Teigland617e82e2007-04-26 13:46:49 -0500802 page_address(con->rx_page) + con->cb.base);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100803 mutex_unlock(&con->sock_mutex);
804 return 0;
805 }
806 BUG_ON(con->nodeid == 0);
807
808 if (ret == len)
809 call_again_soon = 1;
810 cbuf_add(&con->cb, ret);
811 ret = dlm_process_incoming_buffer(con->nodeid,
812 page_address(con->rx_page),
813 con->cb.base, con->cb.len,
814 PAGE_CACHE_SIZE);
815 if (ret == -EBADMSG) {
David Teigland617e82e2007-04-26 13:46:49 -0500816 log_print("lowcomms: addr=%p, base=%u, len=%u, "
817 "iov_len=%u, iov_base[0]=%p, read=%d",
818 page_address(con->rx_page), con->cb.base, con->cb.len,
819 len, iov[0].iov_base, r);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100820 }
821 if (ret < 0)
822 goto out_close;
823 cbuf_eat(&con->cb, ret);
824
825 if (cbuf_empty(&con->cb) && !call_again_soon) {
826 __free_page(con->rx_page);
827 con->rx_page = NULL;
828 }
829
830 if (call_again_soon)
831 goto out_resched;
832 mutex_unlock(&con->sock_mutex);
833 return 0;
834
835out_resched:
836 if (!test_and_set_bit(CF_READ_PENDING, &con->flags))
837 queue_work(recv_workqueue, &con->rwork);
838 mutex_unlock(&con->sock_mutex);
839 return -EAGAIN;
840
841out_close:
842 mutex_unlock(&con->sock_mutex);
Patrick Caulfield9e5f2822007-08-02 14:58:14 +0100843 if (ret != -EAGAIN) {
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100844 close_connection(con, false);
845 /* Reconnect when there is something to send */
846 }
847 /* Don't return success if we really got EOF */
848 if (ret == 0)
849 ret = -EAGAIN;
850
851 return ret;
852}
853
854/* Listening socket is busy, accept a connection */
855static int tcp_accept_from_sock(struct connection *con)
856{
857 int result;
858 struct sockaddr_storage peeraddr;
859 struct socket *newsock;
860 int len;
861 int nodeid;
862 struct connection *newcon;
863 struct connection *addcon;
864
David Teigland513ef592012-03-30 11:46:08 -0500865 mutex_lock(&connections_lock);
866 if (!dlm_allow_conn) {
867 mutex_unlock(&connections_lock);
868 return -1;
869 }
870 mutex_unlock(&connections_lock);
871
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100872 memset(&peeraddr, 0, sizeof(peeraddr));
873 result = sock_create_kern(dlm_local_addr[0]->ss_family, SOCK_STREAM,
874 IPPROTO_TCP, &newsock);
875 if (result < 0)
876 return -ENOMEM;
877
878 mutex_lock_nested(&con->sock_mutex, 0);
879
880 result = -ENOTCONN;
881 if (con->sock == NULL)
882 goto accept_err;
883
884 newsock->type = con->sock->type;
885 newsock->ops = con->sock->ops;
886
887 result = con->sock->ops->accept(con->sock, newsock, O_NONBLOCK);
888 if (result < 0)
889 goto accept_err;
890
891 /* Get the connected socket's peer */
892 memset(&peeraddr, 0, sizeof(peeraddr));
893 if (newsock->ops->getname(newsock, (struct sockaddr *)&peeraddr,
894 &len, 2)) {
895 result = -ECONNABORTED;
896 goto accept_err;
897 }
898
899 /* Get the new node's NODEID */
900 make_sockaddr(&peeraddr, 0, &len);
David Teigland36b71a82012-07-26 12:44:30 -0500901 if (addr_to_nodeid(&peeraddr, &nodeid)) {
Masatake YAMATObcaadf52011-07-04 12:25:51 +0900902 unsigned char *b=(unsigned char *)&peeraddr;
David Teigland617e82e2007-04-26 13:46:49 -0500903 log_print("connect from non cluster node");
Masatake YAMATObcaadf52011-07-04 12:25:51 +0900904 print_hex_dump_bytes("ss: ", DUMP_PREFIX_NONE,
905 b, sizeof(struct sockaddr_storage));
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100906 sock_release(newsock);
907 mutex_unlock(&con->sock_mutex);
908 return -1;
909 }
910
911 log_print("got connection from %d", nodeid);
912
913 /* Check to see if we already have a connection to this node. This
914 * could happen if the two nodes initiate a connection at roughly
915 * the same time and the connections cross on the wire.
916 * In this case we store the incoming one in "othercon"
917 */
David Teigland748285c2009-05-15 10:50:57 -0500918 newcon = nodeid2con(nodeid, GFP_NOFS);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100919 if (!newcon) {
920 result = -ENOMEM;
921 goto accept_err;
922 }
923 mutex_lock_nested(&newcon->sock_mutex, 1);
924 if (newcon->sock) {
925 struct connection *othercon = newcon->othercon;
926
927 if (!othercon) {
David Teigland748285c2009-05-15 10:50:57 -0500928 othercon = kmem_cache_zalloc(con_cache, GFP_NOFS);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100929 if (!othercon) {
David Teigland617e82e2007-04-26 13:46:49 -0500930 log_print("failed to allocate incoming socket");
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100931 mutex_unlock(&newcon->sock_mutex);
932 result = -ENOMEM;
933 goto accept_err;
934 }
935 othercon->nodeid = nodeid;
936 othercon->rx_action = receive_from_sock;
937 mutex_init(&othercon->sock_mutex);
938 INIT_WORK(&othercon->swork, process_send_sockets);
939 INIT_WORK(&othercon->rwork, process_recv_sockets);
940 set_bit(CF_IS_OTHERCON, &othercon->flags);
Patrick Caulfield61d96be2007-08-20 15:13:38 +0100941 }
942 if (!othercon->sock) {
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100943 newcon->othercon = othercon;
Patrick Caulfield97d84832007-06-27 11:36:23 +0100944 othercon->sock = newsock;
945 newsock->sk->sk_user_data = othercon;
946 add_sock(newsock, othercon);
947 addcon = othercon;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100948 }
Patrick Caulfield97d84832007-06-27 11:36:23 +0100949 else {
950 printk("Extra connection from node %d attempted\n", nodeid);
951 result = -EAGAIN;
akpm@linux-foundation.orgf4fadb23c2007-06-27 14:43:37 -0700952 mutex_unlock(&newcon->sock_mutex);
Patrick Caulfield97d84832007-06-27 11:36:23 +0100953 goto accept_err;
954 }
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100955 }
956 else {
957 newsock->sk->sk_user_data = newcon;
958 newcon->rx_action = receive_from_sock;
959 add_sock(newsock, newcon);
960 addcon = newcon;
961 }
962
963 mutex_unlock(&newcon->sock_mutex);
964
965 /*
966 * Add it to the active queue in case we got data
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300967 * between processing the accept adding the socket
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100968 * to the read_sockets list
969 */
970 if (!test_and_set_bit(CF_READ_PENDING, &addcon->flags))
971 queue_work(recv_workqueue, &addcon->rwork);
972 mutex_unlock(&con->sock_mutex);
973
974 return 0;
975
976accept_err:
977 mutex_unlock(&con->sock_mutex);
978 sock_release(newsock);
979
980 if (result != -EAGAIN)
David Teigland617e82e2007-04-26 13:46:49 -0500981 log_print("error accepting connection from node: %d", result);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100982 return result;
983}
984
985static void free_entry(struct writequeue_entry *e)
986{
987 __free_page(e->page);
988 kfree(e);
989}
990
991/* Initiate an SCTP association.
992 This is a special case of send_to_sock() in that we don't yet have a
993 peeled-off socket for this association, so we use the listening socket
994 and add the primary IP address of the remote node.
995 */
996static void sctp_init_assoc(struct connection *con)
997{
998 struct sockaddr_storage rem_addr;
999 char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
1000 struct msghdr outmessage;
1001 struct cmsghdr *cmsg;
1002 struct sctp_sndrcvinfo *sinfo;
1003 struct connection *base_con;
1004 struct writequeue_entry *e;
1005 int len, offset;
1006 int ret;
1007 int addrlen;
1008 struct kvec iov[1];
1009
1010 if (test_and_set_bit(CF_INIT_PENDING, &con->flags))
1011 return;
1012
1013 if (con->retries++ > MAX_CONNECT_RETRIES)
1014 return;
1015
Mike Christie98e1b602013-06-14 04:56:12 -05001016 if (nodeid_to_addr(con->nodeid, NULL, (struct sockaddr *)&rem_addr,
1017 con->try_new_addr)) {
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001018 log_print("no address for nodeid %d", con->nodeid);
1019 return;
1020 }
1021 base_con = nodeid2con(0, 0);
1022 BUG_ON(base_con == NULL);
1023
1024 make_sockaddr(&rem_addr, dlm_config.ci_tcp_port, &addrlen);
1025
1026 outmessage.msg_name = &rem_addr;
1027 outmessage.msg_namelen = addrlen;
1028 outmessage.msg_control = outcmsg;
1029 outmessage.msg_controllen = sizeof(outcmsg);
1030 outmessage.msg_flags = MSG_EOR;
1031
1032 spin_lock(&con->writequeue_lock);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001033
David Teigland04bedd72009-09-18 14:31:47 -05001034 if (list_empty(&con->writequeue)) {
1035 spin_unlock(&con->writequeue_lock);
1036 log_print("writequeue empty for nodeid %d", con->nodeid);
1037 return;
1038 }
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001039
David Teigland04bedd72009-09-18 14:31:47 -05001040 e = list_first_entry(&con->writequeue, struct writequeue_entry, list);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001041 len = e->len;
1042 offset = e->offset;
1043 spin_unlock(&con->writequeue_lock);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001044
1045 /* Send the first block off the write queue */
1046 iov[0].iov_base = page_address(e->page)+offset;
1047 iov[0].iov_len = len;
1048
Mike Christie98e1b602013-06-14 04:56:12 -05001049 if (rem_addr.ss_family == AF_INET) {
1050 struct sockaddr_in *sin = (struct sockaddr_in *)&rem_addr;
1051 log_print("Trying to connect to %pI4", &sin->sin_addr.s_addr);
1052 } else {
1053 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&rem_addr;
1054 log_print("Trying to connect to %pI6", &sin6->sin6_addr);
1055 }
1056
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001057 cmsg = CMSG_FIRSTHDR(&outmessage);
1058 cmsg->cmsg_level = IPPROTO_SCTP;
1059 cmsg->cmsg_type = SCTP_SNDRCV;
1060 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
1061 sinfo = CMSG_DATA(cmsg);
1062 memset(sinfo, 0x00, sizeof(struct sctp_sndrcvinfo));
1063 sinfo->sinfo_ppid = cpu_to_le32(dlm_our_nodeid());
1064 outmessage.msg_controllen = cmsg->cmsg_len;
Mike Christie98e1b602013-06-14 04:56:12 -05001065 sinfo->sinfo_flags |= SCTP_ADDR_OVER;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001066
1067 ret = kernel_sendmsg(base_con->sock, &outmessage, iov, 1, len);
1068 if (ret < 0) {
David Teigland617e82e2007-04-26 13:46:49 -05001069 log_print("Send first packet to node %d failed: %d",
1070 con->nodeid, ret);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001071
1072 /* Try again later */
1073 clear_bit(CF_CONNECT_PENDING, &con->flags);
1074 clear_bit(CF_INIT_PENDING, &con->flags);
1075 }
1076 else {
1077 spin_lock(&con->writequeue_lock);
1078 e->offset += ret;
1079 e->len -= ret;
1080
1081 if (e->len == 0 && e->users == 0) {
1082 list_del(&e->list);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001083 free_entry(e);
1084 }
1085 spin_unlock(&con->writequeue_lock);
1086 }
1087}
1088
1089/* Connect a new socket to its peer */
1090static void tcp_connect_to_sock(struct connection *con)
1091{
Lon Hohberger6bd8fed2007-10-25 18:51:54 -04001092 struct sockaddr_storage saddr, src_addr;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001093 int addr_len;
Casey Dahlina89d63a2009-07-14 12:17:51 -05001094 struct socket *sock = NULL;
David Teiglandcb2d45d2010-11-12 11:12:55 -06001095 int one = 1;
David Teigland36b71a82012-07-26 12:44:30 -05001096 int result;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001097
1098 if (con->nodeid == 0) {
1099 log_print("attempt to connect sock 0 foiled");
1100 return;
1101 }
1102
1103 mutex_lock(&con->sock_mutex);
1104 if (con->retries++ > MAX_CONNECT_RETRIES)
1105 goto out;
1106
1107 /* Some odd races can cause double-connects, ignore them */
David Teigland36b71a82012-07-26 12:44:30 -05001108 if (con->sock)
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001109 goto out;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001110
1111 /* Create a socket to communicate with */
1112 result = sock_create_kern(dlm_local_addr[0]->ss_family, SOCK_STREAM,
1113 IPPROTO_TCP, &sock);
1114 if (result < 0)
1115 goto out_err;
1116
1117 memset(&saddr, 0, sizeof(saddr));
Mike Christie98e1b602013-06-14 04:56:12 -05001118 result = nodeid_to_addr(con->nodeid, &saddr, NULL, false);
David Teigland36b71a82012-07-26 12:44:30 -05001119 if (result < 0) {
1120 log_print("no address for nodeid %d", con->nodeid);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001121 goto out_err;
David Teigland36b71a82012-07-26 12:44:30 -05001122 }
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001123
1124 sock->sk->sk_user_data = con;
1125 con->rx_action = receive_from_sock;
1126 con->connect_action = tcp_connect_to_sock;
1127 add_sock(sock, con);
1128
Lon Hohberger6bd8fed2007-10-25 18:51:54 -04001129 /* Bind to our cluster-known address connecting to avoid
1130 routing problems */
1131 memcpy(&src_addr, dlm_local_addr[0], sizeof(src_addr));
1132 make_sockaddr(&src_addr, 0, &addr_len);
1133 result = sock->ops->bind(sock, (struct sockaddr *) &src_addr,
1134 addr_len);
1135 if (result < 0) {
1136 log_print("could not bind for connect: %d", result);
1137 /* This *may* not indicate a critical error */
1138 }
1139
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001140 make_sockaddr(&saddr, dlm_config.ci_tcp_port, &addr_len);
1141
1142 log_print("connecting to %d", con->nodeid);
David Teiglandcb2d45d2010-11-12 11:12:55 -06001143
1144 /* Turn off Nagle's algorithm */
1145 kernel_setsockopt(sock, SOL_TCP, TCP_NODELAY, (char *)&one,
1146 sizeof(one));
1147
David Teigland36b71a82012-07-26 12:44:30 -05001148 result = sock->ops->connect(sock, (struct sockaddr *)&saddr, addr_len,
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001149 O_NONBLOCK);
1150 if (result == -EINPROGRESS)
1151 result = 0;
1152 if (result == 0)
1153 goto out;
1154
1155out_err:
1156 if (con->sock) {
1157 sock_release(con->sock);
1158 con->sock = NULL;
Casey Dahlina89d63a2009-07-14 12:17:51 -05001159 } else if (sock) {
1160 sock_release(sock);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001161 }
1162 /*
1163 * Some errors are fatal and this list might need adjusting. For other
1164 * errors we try again until the max number of retries is reached.
1165 */
David Teigland36b71a82012-07-26 12:44:30 -05001166 if (result != -EHOSTUNREACH &&
1167 result != -ENETUNREACH &&
1168 result != -ENETDOWN &&
1169 result != -EINVAL &&
1170 result != -EPROTONOSUPPORT) {
1171 log_print("connect %d try %d error %d", con->nodeid,
1172 con->retries, result);
1173 mutex_unlock(&con->sock_mutex);
1174 msleep(1000);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001175 lowcomms_connect_sock(con);
David Teigland36b71a82012-07-26 12:44:30 -05001176 return;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001177 }
1178out:
1179 mutex_unlock(&con->sock_mutex);
1180 return;
1181}
1182
1183static struct socket *tcp_create_listen_sock(struct connection *con,
1184 struct sockaddr_storage *saddr)
1185{
1186 struct socket *sock = NULL;
1187 int result = 0;
1188 int one = 1;
1189 int addr_len;
1190
1191 if (dlm_local_addr[0]->ss_family == AF_INET)
1192 addr_len = sizeof(struct sockaddr_in);
1193 else
1194 addr_len = sizeof(struct sockaddr_in6);
1195
1196 /* Create a socket to communicate with */
David Teigland617e82e2007-04-26 13:46:49 -05001197 result = sock_create_kern(dlm_local_addr[0]->ss_family, SOCK_STREAM,
1198 IPPROTO_TCP, &sock);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001199 if (result < 0) {
David Teigland617e82e2007-04-26 13:46:49 -05001200 log_print("Can't create listening comms socket");
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001201 goto create_out;
1202 }
1203
David Teiglandcb2d45d2010-11-12 11:12:55 -06001204 /* Turn off Nagle's algorithm */
1205 kernel_setsockopt(sock, SOL_TCP, TCP_NODELAY, (char *)&one,
1206 sizeof(one));
1207
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001208 result = kernel_setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
1209 (char *)&one, sizeof(one));
1210
1211 if (result < 0) {
David Teigland617e82e2007-04-26 13:46:49 -05001212 log_print("Failed to set SO_REUSEADDR on socket: %d", result);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001213 }
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001214 con->rx_action = tcp_accept_from_sock;
1215 con->connect_action = tcp_connect_to_sock;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001216
1217 /* Bind to our port */
1218 make_sockaddr(saddr, dlm_config.ci_tcp_port, &addr_len);
1219 result = sock->ops->bind(sock, (struct sockaddr *) saddr, addr_len);
1220 if (result < 0) {
David Teigland617e82e2007-04-26 13:46:49 -05001221 log_print("Can't bind to port %d", dlm_config.ci_tcp_port);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001222 sock_release(sock);
1223 sock = NULL;
1224 con->sock = NULL;
1225 goto create_out;
1226 }
1227 result = kernel_setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE,
1228 (char *)&one, sizeof(one));
1229 if (result < 0) {
David Teigland617e82e2007-04-26 13:46:49 -05001230 log_print("Set keepalive failed: %d", result);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001231 }
1232
1233 result = sock->ops->listen(sock, 5);
1234 if (result < 0) {
David Teigland617e82e2007-04-26 13:46:49 -05001235 log_print("Can't listen on port %d", dlm_config.ci_tcp_port);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001236 sock_release(sock);
1237 sock = NULL;
1238 goto create_out;
1239 }
1240
1241create_out:
1242 return sock;
1243}
1244
1245/* Get local addresses */
1246static void init_local(void)
1247{
1248 struct sockaddr_storage sas, *addr;
1249 int i;
1250
Patrick Caulfield30d3a232007-04-23 16:26:21 +01001251 dlm_local_count = 0;
David Teigland1b189b82012-03-21 09:18:34 -05001252 for (i = 0; i < DLM_MAX_ADDR_COUNT; i++) {
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001253 if (dlm_our_addr(&sas, i))
1254 break;
1255
David Teigland573c24c2009-11-30 16:34:43 -06001256 addr = kmalloc(sizeof(*addr), GFP_NOFS);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001257 if (!addr)
1258 break;
1259 memcpy(addr, &sas, sizeof(*addr));
1260 dlm_local_addr[dlm_local_count++] = addr;
1261 }
1262}
1263
David Teigland617e82e2007-04-26 13:46:49 -05001264/* Bind to an IP address. SCTP allows multiple address so it can do
1265 multi-homing */
1266static int add_sctp_bind_addr(struct connection *sctp_con,
1267 struct sockaddr_storage *addr,
1268 int addr_len, int num)
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001269{
1270 int result = 0;
1271
1272 if (num == 1)
1273 result = kernel_bind(sctp_con->sock,
1274 (struct sockaddr *) addr,
1275 addr_len);
1276 else
1277 result = kernel_setsockopt(sctp_con->sock, SOL_SCTP,
1278 SCTP_SOCKOPT_BINDX_ADD,
1279 (char *)addr, addr_len);
1280
1281 if (result < 0)
1282 log_print("Can't bind to port %d addr number %d",
1283 dlm_config.ci_tcp_port, num);
1284
1285 return result;
1286}
1287
1288/* Initialise SCTP socket and bind to all interfaces */
1289static int sctp_listen_for_all(void)
1290{
1291 struct socket *sock = NULL;
1292 struct sockaddr_storage localaddr;
1293 struct sctp_event_subscribe subscribe;
1294 int result = -EINVAL, num = 1, i, addr_len;
David Teigland573c24c2009-11-30 16:34:43 -06001295 struct connection *con = nodeid2con(0, GFP_NOFS);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001296 int bufsize = NEEDED_RMEM;
1297
1298 if (!con)
1299 return -ENOMEM;
1300
1301 log_print("Using SCTP for communications");
1302
1303 result = sock_create_kern(dlm_local_addr[0]->ss_family, SOCK_SEQPACKET,
1304 IPPROTO_SCTP, &sock);
1305 if (result < 0) {
1306 log_print("Can't create comms socket, check SCTP is loaded");
1307 goto out;
1308 }
1309
1310 /* Listen for events */
1311 memset(&subscribe, 0, sizeof(subscribe));
1312 subscribe.sctp_data_io_event = 1;
1313 subscribe.sctp_association_event = 1;
1314 subscribe.sctp_send_failure_event = 1;
1315 subscribe.sctp_shutdown_event = 1;
1316 subscribe.sctp_partial_delivery_event = 1;
1317
David S. Millerdf61c952007-11-06 23:48:57 -08001318 result = kernel_setsockopt(sock, SOL_SOCKET, SO_RCVBUFFORCE,
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001319 (char *)&bufsize, sizeof(bufsize));
1320 if (result)
David Teigland617e82e2007-04-26 13:46:49 -05001321 log_print("Error increasing buffer space on socket %d", result);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001322
1323 result = kernel_setsockopt(sock, SOL_SCTP, SCTP_EVENTS,
David Teigland617e82e2007-04-26 13:46:49 -05001324 (char *)&subscribe, sizeof(subscribe));
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001325 if (result < 0) {
1326 log_print("Failed to set SCTP_EVENTS on socket: result=%d",
1327 result);
1328 goto create_delsock;
1329 }
1330
1331 /* Init con struct */
1332 sock->sk->sk_user_data = con;
1333 con->sock = sock;
1334 con->sock->sk->sk_data_ready = lowcomms_data_ready;
1335 con->rx_action = receive_from_sock;
1336 con->connect_action = sctp_init_assoc;
1337
1338 /* Bind to all interfaces. */
1339 for (i = 0; i < dlm_local_count; i++) {
1340 memcpy(&localaddr, dlm_local_addr[i], sizeof(localaddr));
1341 make_sockaddr(&localaddr, dlm_config.ci_tcp_port, &addr_len);
1342
1343 result = add_sctp_bind_addr(con, &localaddr, addr_len, num);
1344 if (result)
1345 goto create_delsock;
1346 ++num;
1347 }
1348
1349 result = sock->ops->listen(sock, 5);
1350 if (result < 0) {
1351 log_print("Can't set socket listening");
1352 goto create_delsock;
1353 }
1354
1355 return 0;
1356
1357create_delsock:
1358 sock_release(sock);
1359 con->sock = NULL;
1360out:
1361 return result;
1362}
1363
1364static int tcp_listen_for_all(void)
1365{
1366 struct socket *sock = NULL;
David Teigland573c24c2009-11-30 16:34:43 -06001367 struct connection *con = nodeid2con(0, GFP_NOFS);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001368 int result = -EINVAL;
1369
1370 if (!con)
1371 return -ENOMEM;
1372
1373 /* We don't support multi-homed hosts */
1374 if (dlm_local_addr[1] != NULL) {
David Teigland617e82e2007-04-26 13:46:49 -05001375 log_print("TCP protocol can't handle multi-homed hosts, "
1376 "try SCTP");
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001377 return -EINVAL;
1378 }
1379
1380 log_print("Using TCP for communications");
1381
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001382 sock = tcp_create_listen_sock(con, dlm_local_addr[0]);
1383 if (sock) {
1384 add_sock(sock, con);
1385 result = 0;
1386 }
1387 else {
1388 result = -EADDRINUSE;
1389 }
1390
1391 return result;
1392}
1393
1394
1395
1396static struct writequeue_entry *new_writequeue_entry(struct connection *con,
1397 gfp_t allocation)
1398{
1399 struct writequeue_entry *entry;
1400
1401 entry = kmalloc(sizeof(struct writequeue_entry), allocation);
1402 if (!entry)
1403 return NULL;
1404
1405 entry->page = alloc_page(allocation);
1406 if (!entry->page) {
1407 kfree(entry);
1408 return NULL;
1409 }
1410
1411 entry->offset = 0;
1412 entry->len = 0;
1413 entry->end = 0;
1414 entry->users = 0;
1415 entry->con = con;
1416
1417 return entry;
1418}
1419
David Teigland617e82e2007-04-26 13:46:49 -05001420void *dlm_lowcomms_get_buffer(int nodeid, int len, gfp_t allocation, char **ppc)
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001421{
1422 struct connection *con;
1423 struct writequeue_entry *e;
1424 int offset = 0;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001425
1426 con = nodeid2con(nodeid, allocation);
1427 if (!con)
1428 return NULL;
1429
1430 spin_lock(&con->writequeue_lock);
1431 e = list_entry(con->writequeue.prev, struct writequeue_entry, list);
1432 if ((&e->list == &con->writequeue) ||
1433 (PAGE_CACHE_SIZE - e->end < len)) {
1434 e = NULL;
1435 } else {
1436 offset = e->end;
1437 e->end += len;
Wei Yongjuneeee2b52012-10-18 22:57:19 +08001438 e->users++;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001439 }
1440 spin_unlock(&con->writequeue_lock);
1441
1442 if (e) {
1443 got_one:
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001444 *ppc = page_address(e->page) + offset;
1445 return e;
1446 }
1447
1448 e = new_writequeue_entry(con, allocation);
1449 if (e) {
1450 spin_lock(&con->writequeue_lock);
1451 offset = e->end;
1452 e->end += len;
Wei Yongjuneeee2b52012-10-18 22:57:19 +08001453 e->users++;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001454 list_add_tail(&e->list, &con->writequeue);
1455 spin_unlock(&con->writequeue_lock);
1456 goto got_one;
1457 }
1458 return NULL;
1459}
1460
1461void dlm_lowcomms_commit_buffer(void *mh)
1462{
1463 struct writequeue_entry *e = (struct writequeue_entry *)mh;
1464 struct connection *con = e->con;
1465 int users;
1466
1467 spin_lock(&con->writequeue_lock);
1468 users = --e->users;
1469 if (users)
1470 goto out;
1471 e->len = e->end - e->offset;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001472 spin_unlock(&con->writequeue_lock);
1473
1474 if (!test_and_set_bit(CF_WRITE_PENDING, &con->flags)) {
1475 queue_work(send_workqueue, &con->swork);
1476 }
1477 return;
1478
1479out:
1480 spin_unlock(&con->writequeue_lock);
1481 return;
1482}
1483
1484/* Send a message */
1485static void send_to_sock(struct connection *con)
1486{
1487 int ret = 0;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001488 const int msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL;
1489 struct writequeue_entry *e;
1490 int len, offset;
Bob Petersonf92c8dd2010-11-12 11:15:20 -06001491 int count = 0;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001492
1493 mutex_lock(&con->sock_mutex);
1494 if (con->sock == NULL)
1495 goto out_connect;
1496
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001497 spin_lock(&con->writequeue_lock);
1498 for (;;) {
1499 e = list_entry(con->writequeue.next, struct writequeue_entry,
1500 list);
1501 if ((struct list_head *) e == &con->writequeue)
1502 break;
1503
1504 len = e->len;
1505 offset = e->offset;
1506 BUG_ON(len == 0 && e->users == 0);
1507 spin_unlock(&con->writequeue_lock);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001508
1509 ret = 0;
1510 if (len) {
Paolo Bonzini1329e3f2009-08-24 13:18:04 -05001511 ret = kernel_sendpage(con->sock, e->page, offset, len,
1512 msg_flags);
Patrick Caulfieldd66f8272007-09-14 08:49:21 +01001513 if (ret == -EAGAIN || ret == 0) {
David Millerb36930d2010-11-10 21:56:39 -08001514 if (ret == -EAGAIN &&
1515 test_bit(SOCK_ASYNC_NOSPACE, &con->sock->flags) &&
1516 !test_and_set_bit(CF_APP_LIMITED, &con->flags)) {
1517 /* Notify TCP that we're limited by the
1518 * application window size.
1519 */
1520 set_bit(SOCK_NOSPACE, &con->sock->flags);
1521 con->sock->sk->sk_write_pending++;
1522 }
Patrick Caulfieldd66f8272007-09-14 08:49:21 +01001523 cond_resched();
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001524 goto out;
Ying Xue9c5bef52012-08-13 14:29:55 +08001525 } else if (ret < 0)
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001526 goto send_error;
Patrick Caulfieldd66f8272007-09-14 08:49:21 +01001527 }
Bob Petersonf92c8dd2010-11-12 11:15:20 -06001528
1529 /* Don't starve people filling buffers */
1530 if (++count >= MAX_SEND_MSG_COUNT) {
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001531 cond_resched();
Bob Petersonf92c8dd2010-11-12 11:15:20 -06001532 count = 0;
1533 }
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001534
1535 spin_lock(&con->writequeue_lock);
1536 e->offset += ret;
1537 e->len -= ret;
1538
1539 if (e->len == 0 && e->users == 0) {
1540 list_del(&e->list);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001541 free_entry(e);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001542 }
1543 }
1544 spin_unlock(&con->writequeue_lock);
1545out:
1546 mutex_unlock(&con->sock_mutex);
1547 return;
1548
1549send_error:
1550 mutex_unlock(&con->sock_mutex);
1551 close_connection(con, false);
1552 lowcomms_connect_sock(con);
1553 return;
1554
1555out_connect:
1556 mutex_unlock(&con->sock_mutex);
1557 if (!test_bit(CF_INIT_PENDING, &con->flags))
1558 lowcomms_connect_sock(con);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001559}
1560
1561static void clean_one_writequeue(struct connection *con)
1562{
Christine Caulfield5e9ccc32009-01-28 12:57:40 -06001563 struct writequeue_entry *e, *safe;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001564
1565 spin_lock(&con->writequeue_lock);
Christine Caulfield5e9ccc32009-01-28 12:57:40 -06001566 list_for_each_entry_safe(e, safe, &con->writequeue, list) {
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001567 list_del(&e->list);
1568 free_entry(e);
1569 }
1570 spin_unlock(&con->writequeue_lock);
1571}
1572
1573/* Called from recovery when it knows that a node has
1574 left the cluster */
1575int dlm_lowcomms_close(int nodeid)
1576{
1577 struct connection *con;
David Teigland36b71a82012-07-26 12:44:30 -05001578 struct dlm_node_addr *na;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001579
1580 log_print("closing connection to node %d", nodeid);
1581 con = nodeid2con(nodeid, 0);
1582 if (con) {
Lars Marowsky-Bree063c4c92009-08-11 16:18:23 -05001583 clear_bit(CF_CONNECT_PENDING, &con->flags);
1584 clear_bit(CF_WRITE_PENDING, &con->flags);
1585 set_bit(CF_CLOSE, &con->flags);
1586 if (cancel_work_sync(&con->swork))
1587 log_print("canceled swork for node %d", nodeid);
1588 if (cancel_work_sync(&con->rwork))
1589 log_print("canceled rwork for node %d", nodeid);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001590 clean_one_writequeue(con);
1591 close_connection(con, true);
1592 }
David Teigland36b71a82012-07-26 12:44:30 -05001593
1594 spin_lock(&dlm_node_addrs_spin);
1595 na = find_node_addr(nodeid);
1596 if (na) {
1597 list_del(&na->list);
1598 while (na->addr_count--)
1599 kfree(na->addr[na->addr_count]);
1600 kfree(na);
1601 }
1602 spin_unlock(&dlm_node_addrs_spin);
1603
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001604 return 0;
1605}
1606
1607/* Receive workqueue function */
1608static void process_recv_sockets(struct work_struct *work)
1609{
1610 struct connection *con = container_of(work, struct connection, rwork);
1611 int err;
1612
1613 clear_bit(CF_READ_PENDING, &con->flags);
1614 do {
1615 err = con->rx_action(con);
1616 } while (!err);
1617}
1618
1619/* Send workqueue function */
1620static void process_send_sockets(struct work_struct *work)
1621{
1622 struct connection *con = container_of(work, struct connection, swork);
1623
1624 if (test_and_clear_bit(CF_CONNECT_PENDING, &con->flags)) {
1625 con->connect_action(con);
Lars Marowsky-Bree063c4c92009-08-11 16:18:23 -05001626 set_bit(CF_WRITE_PENDING, &con->flags);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001627 }
Lars Marowsky-Bree063c4c92009-08-11 16:18:23 -05001628 if (test_and_clear_bit(CF_WRITE_PENDING, &con->flags))
1629 send_to_sock(con);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001630}
1631
1632
1633/* Discard all entries on the write queues */
1634static void clean_writequeues(void)
1635{
Christine Caulfield5e9ccc32009-01-28 12:57:40 -06001636 foreach_conn(clean_one_writequeue);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001637}
1638
1639static void work_stop(void)
1640{
1641 destroy_workqueue(recv_workqueue);
1642 destroy_workqueue(send_workqueue);
1643}
1644
1645static int work_start(void)
1646{
David Teiglande43f0552011-03-10 13:22:34 -06001647 recv_workqueue = alloc_workqueue("dlm_recv",
1648 WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
Namhyung Kimb9d41052010-12-13 13:42:24 -06001649 if (!recv_workqueue) {
1650 log_print("can't start dlm_recv");
1651 return -ENOMEM;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001652 }
1653
David Teiglande43f0552011-03-10 13:22:34 -06001654 send_workqueue = alloc_workqueue("dlm_send",
1655 WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
Namhyung Kimb9d41052010-12-13 13:42:24 -06001656 if (!send_workqueue) {
1657 log_print("can't start dlm_send");
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001658 destroy_workqueue(recv_workqueue);
Namhyung Kimb9d41052010-12-13 13:42:24 -06001659 return -ENOMEM;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001660 }
1661
1662 return 0;
1663}
1664
Christine Caulfield5e9ccc32009-01-28 12:57:40 -06001665static void stop_conn(struct connection *con)
1666{
1667 con->flags |= 0x0F;
Christine Caulfield391fbdc2009-05-07 10:54:16 -05001668 if (con->sock && con->sock->sk)
Christine Caulfield5e9ccc32009-01-28 12:57:40 -06001669 con->sock->sk->sk_user_data = NULL;
1670}
1671
1672static void free_conn(struct connection *con)
1673{
1674 close_connection(con, true);
1675 if (con->othercon)
1676 kmem_cache_free(con_cache, con->othercon);
1677 hlist_del(&con->list);
1678 kmem_cache_free(con_cache, con);
1679}
1680
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001681void dlm_lowcomms_stop(void)
1682{
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001683 /* Set all the flags to prevent any
1684 socket activity.
1685 */
Matthias Kaehlcke7a936ce2008-05-12 10:04:51 -05001686 mutex_lock(&connections_lock);
David Teigland513ef592012-03-30 11:46:08 -05001687 dlm_allow_conn = 0;
Christine Caulfield5e9ccc32009-01-28 12:57:40 -06001688 foreach_conn(stop_conn);
Matthias Kaehlcke7a936ce2008-05-12 10:04:51 -05001689 mutex_unlock(&connections_lock);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001690
1691 work_stop();
1692
Matthias Kaehlcke7a936ce2008-05-12 10:04:51 -05001693 mutex_lock(&connections_lock);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001694 clean_writequeues();
1695
Christine Caulfield5e9ccc32009-01-28 12:57:40 -06001696 foreach_conn(free_conn);
1697
Matthias Kaehlcke7a936ce2008-05-12 10:04:51 -05001698 mutex_unlock(&connections_lock);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001699 kmem_cache_destroy(con_cache);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001700}
1701
1702int dlm_lowcomms_start(void)
1703{
1704 int error = -EINVAL;
1705 struct connection *con;
Christine Caulfield5e9ccc32009-01-28 12:57:40 -06001706 int i;
1707
1708 for (i = 0; i < CONN_HASH_SIZE; i++)
1709 INIT_HLIST_HEAD(&connection_hash[i]);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001710
1711 init_local();
1712 if (!dlm_local_count) {
David Teigland617e82e2007-04-26 13:46:49 -05001713 error = -ENOTCONN;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001714 log_print("no local IP address has been set");
David Teigland513ef592012-03-30 11:46:08 -05001715 goto fail;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001716 }
1717
1718 error = -ENOMEM;
1719 con_cache = kmem_cache_create("dlm_conn", sizeof(struct connection),
1720 __alignof__(struct connection), 0,
Paul Mundt20c2df82007-07-20 10:11:58 +09001721 NULL);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001722 if (!con_cache)
David Teigland513ef592012-03-30 11:46:08 -05001723 goto fail;
1724
1725 error = work_start();
1726 if (error)
1727 goto fail_destroy;
1728
1729 dlm_allow_conn = 1;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001730
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001731 /* Start listening */
1732 if (dlm_config.ci_protocol == 0)
1733 error = tcp_listen_for_all();
1734 else
1735 error = sctp_listen_for_all();
1736 if (error)
1737 goto fail_unlisten;
1738
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001739 return 0;
1740
1741fail_unlisten:
David Teigland513ef592012-03-30 11:46:08 -05001742 dlm_allow_conn = 0;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001743 con = nodeid2con(0,0);
1744 if (con) {
1745 close_connection(con, false);
1746 kmem_cache_free(con_cache, con);
1747 }
David Teigland513ef592012-03-30 11:46:08 -05001748fail_destroy:
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001749 kmem_cache_destroy(con_cache);
David Teigland513ef592012-03-30 11:46:08 -05001750fail:
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001751 return error;
1752}
David Teigland36b71a82012-07-26 12:44:30 -05001753
1754void dlm_lowcomms_exit(void)
1755{
1756 struct dlm_node_addr *na, *safe;
1757
1758 spin_lock(&dlm_node_addrs_spin);
1759 list_for_each_entry_safe(na, safe, &dlm_node_addrs, list) {
1760 list_del(&na->list);
1761 while (na->addr_count--)
1762 kfree(na->addr[na->addr_count]);
1763 kfree(na);
1764 }
1765 spin_unlock(&dlm_node_addrs_spin);
1766}