blob: d0ccd2fd79eb0430ac08db074edfb811859e4b90 [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 */
129};
130#define sock2con(x) ((struct connection *)(x)->sk_user_data)
131
132/* An entry waiting to be sent */
133struct writequeue_entry {
134 struct list_head list;
135 struct page *page;
136 int offset;
137 int len;
138 int end;
139 int users;
140 struct connection *con;
141};
142
David Teigland36b71a82012-07-26 12:44:30 -0500143struct dlm_node_addr {
144 struct list_head list;
145 int nodeid;
146 int addr_count;
147 struct sockaddr_storage *addr[DLM_MAX_ADDR_COUNT];
148};
149
150static LIST_HEAD(dlm_node_addrs);
151static DEFINE_SPINLOCK(dlm_node_addrs_spin);
152
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100153static struct sockaddr_storage *dlm_local_addr[DLM_MAX_ADDR_COUNT];
154static int dlm_local_count;
David Teigland513ef592012-03-30 11:46:08 -0500155static int dlm_allow_conn;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100156
157/* Work queues */
158static struct workqueue_struct *recv_workqueue;
159static struct workqueue_struct *send_workqueue;
160
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600161static struct hlist_head connection_hash[CONN_HASH_SIZE];
Matthias Kaehlcke7a936ce2008-05-12 10:04:51 -0500162static DEFINE_MUTEX(connections_lock);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100163static struct kmem_cache *con_cache;
164
165static void process_recv_sockets(struct work_struct *work);
166static void process_send_sockets(struct work_struct *work);
167
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600168
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;
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600180 struct connection *con;
181
182 r = nodeid_hash(nodeid);
183
Sasha Levinb67bfe02013-02-27 17:06:00 -0800184 hlist_for_each_entry(con, &connection_hash[r], list) {
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600185 if (con->nodeid == nodeid)
186 return con;
187 }
188 return NULL;
189}
190
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100191/*
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)
196{
197 struct connection *con = NULL;
198 int r;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100199
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600200 con = __find_con(nodeid);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100201 if (con || !alloc)
202 return con;
203
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100204 con = kmem_cache_zalloc(con_cache, alloc);
205 if (!con)
206 return NULL;
207
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600208 r = nodeid_hash(nodeid);
209 hlist_add_head(&con->list, &connection_hash[r]);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100210
211 con->nodeid = nodeid;
212 mutex_init(&con->sock_mutex);
213 INIT_LIST_HEAD(&con->writequeue);
214 spin_lock_init(&con->writequeue_lock);
215 INIT_WORK(&con->swork, process_send_sockets);
216 INIT_WORK(&con->rwork, process_recv_sockets);
217
218 /* Setup action pointers for child sockets */
219 if (con->nodeid) {
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600220 struct connection *zerocon = __find_con(0);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100221
222 con->connect_action = zerocon->connect_action;
223 if (!con->rx_action)
224 con->rx_action = zerocon->rx_action;
225 }
226
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100227 return con;
228}
229
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600230/* Loop round all connections */
231static void foreach_conn(void (*conn_func)(struct connection *c))
232{
233 int i;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800234 struct hlist_node *n;
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600235 struct connection *con;
236
237 for (i = 0; i < CONN_HASH_SIZE; i++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800238 hlist_for_each_entry_safe(con, n, &connection_hash[i], list)
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600239 conn_func(con);
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600240 }
241}
242
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100243static struct connection *nodeid2con(int nodeid, gfp_t allocation)
244{
245 struct connection *con;
246
Matthias Kaehlcke7a936ce2008-05-12 10:04:51 -0500247 mutex_lock(&connections_lock);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100248 con = __nodeid2con(nodeid, allocation);
Matthias Kaehlcke7a936ce2008-05-12 10:04:51 -0500249 mutex_unlock(&connections_lock);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100250
251 return con;
252}
253
254/* This is a bit drastic, but only called when things go wrong */
255static struct connection *assoc2con(int assoc_id)
256{
257 int i;
258 struct connection *con;
259
Matthias Kaehlcke7a936ce2008-05-12 10:04:51 -0500260 mutex_lock(&connections_lock);
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600261
262 for (i = 0 ; i < CONN_HASH_SIZE; i++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800263 hlist_for_each_entry(con, &connection_hash[i], list) {
Julia Lawallf70cb332010-08-03 23:34:16 +0200264 if (con->sctp_assoc == assoc_id) {
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600265 mutex_unlock(&connections_lock);
266 return con;
267 }
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100268 }
269 }
Matthias Kaehlcke7a936ce2008-05-12 10:04:51 -0500270 mutex_unlock(&connections_lock);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100271 return NULL;
272}
273
David Teigland36b71a82012-07-26 12:44:30 -0500274static struct dlm_node_addr *find_node_addr(int nodeid)
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100275{
David Teigland36b71a82012-07-26 12:44:30 -0500276 struct dlm_node_addr *na;
277
278 list_for_each_entry(na, &dlm_node_addrs, list) {
279 if (na->nodeid == nodeid)
280 return na;
281 }
282 return NULL;
283}
284
285static int addr_compare(struct sockaddr_storage *x, struct sockaddr_storage *y)
286{
287 switch (x->ss_family) {
288 case AF_INET: {
289 struct sockaddr_in *sinx = (struct sockaddr_in *)x;
290 struct sockaddr_in *siny = (struct sockaddr_in *)y;
291 if (sinx->sin_addr.s_addr != siny->sin_addr.s_addr)
292 return 0;
293 if (sinx->sin_port != siny->sin_port)
294 return 0;
295 break;
296 }
297 case AF_INET6: {
298 struct sockaddr_in6 *sinx = (struct sockaddr_in6 *)x;
299 struct sockaddr_in6 *siny = (struct sockaddr_in6 *)y;
300 if (!ipv6_addr_equal(&sinx->sin6_addr, &siny->sin6_addr))
301 return 0;
302 if (sinx->sin6_port != siny->sin6_port)
303 return 0;
304 break;
305 }
306 default:
307 return 0;
308 }
309 return 1;
310}
311
312static int nodeid_to_addr(int nodeid, struct sockaddr_storage *sas_out,
313 struct sockaddr *sa_out)
314{
315 struct sockaddr_storage sas;
316 struct dlm_node_addr *na;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100317
318 if (!dlm_local_count)
319 return -1;
320
David Teigland36b71a82012-07-26 12:44:30 -0500321 spin_lock(&dlm_node_addrs_spin);
322 na = find_node_addr(nodeid);
323 if (na && na->addr_count)
324 memcpy(&sas, na->addr[0], sizeof(struct sockaddr_storage));
325 spin_unlock(&dlm_node_addrs_spin);
326
327 if (!na)
328 return -EEXIST;
329
330 if (!na->addr_count)
331 return -ENOENT;
332
333 if (sas_out)
334 memcpy(sas_out, &sas, sizeof(struct sockaddr_storage));
335
336 if (!sa_out)
337 return 0;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100338
339 if (dlm_local_addr[0]->ss_family == AF_INET) {
David Teigland36b71a82012-07-26 12:44:30 -0500340 struct sockaddr_in *in4 = (struct sockaddr_in *) &sas;
341 struct sockaddr_in *ret4 = (struct sockaddr_in *) sa_out;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100342 ret4->sin_addr.s_addr = in4->sin_addr.s_addr;
343 } else {
David Teigland36b71a82012-07-26 12:44:30 -0500344 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) &sas;
345 struct sockaddr_in6 *ret6 = (struct sockaddr_in6 *) sa_out;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000346 ret6->sin6_addr = in6->sin6_addr;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100347 }
348
349 return 0;
350}
351
David Teigland36b71a82012-07-26 12:44:30 -0500352static int addr_to_nodeid(struct sockaddr_storage *addr, int *nodeid)
353{
354 struct dlm_node_addr *na;
355 int rv = -EEXIST;
356
357 spin_lock(&dlm_node_addrs_spin);
358 list_for_each_entry(na, &dlm_node_addrs, list) {
359 if (!na->addr_count)
360 continue;
361
362 if (!addr_compare(na->addr[0], addr))
363 continue;
364
365 *nodeid = na->nodeid;
366 rv = 0;
367 break;
368 }
369 spin_unlock(&dlm_node_addrs_spin);
370 return rv;
371}
372
373int dlm_lowcomms_addr(int nodeid, struct sockaddr_storage *addr, int len)
374{
375 struct sockaddr_storage *new_addr;
376 struct dlm_node_addr *new_node, *na;
377
378 new_node = kzalloc(sizeof(struct dlm_node_addr), GFP_NOFS);
379 if (!new_node)
380 return -ENOMEM;
381
382 new_addr = kzalloc(sizeof(struct sockaddr_storage), GFP_NOFS);
383 if (!new_addr) {
384 kfree(new_node);
385 return -ENOMEM;
386 }
387
388 memcpy(new_addr, addr, len);
389
390 spin_lock(&dlm_node_addrs_spin);
391 na = find_node_addr(nodeid);
392 if (!na) {
393 new_node->nodeid = nodeid;
394 new_node->addr[0] = new_addr;
395 new_node->addr_count = 1;
396 list_add(&new_node->list, &dlm_node_addrs);
397 spin_unlock(&dlm_node_addrs_spin);
398 return 0;
399 }
400
401 if (na->addr_count >= DLM_MAX_ADDR_COUNT) {
402 spin_unlock(&dlm_node_addrs_spin);
403 kfree(new_addr);
404 kfree(new_node);
405 return -ENOSPC;
406 }
407
408 na->addr[na->addr_count++] = new_addr;
409 spin_unlock(&dlm_node_addrs_spin);
410 kfree(new_node);
411 return 0;
412}
413
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100414/* Data available on socket or listen socket received a connect */
415static void lowcomms_data_ready(struct sock *sk, int count_unused)
416{
417 struct connection *con = sock2con(sk);
Patrick Caulfieldafb853f2007-06-01 10:07:26 -0500418 if (con && !test_and_set_bit(CF_READ_PENDING, &con->flags))
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100419 queue_work(recv_workqueue, &con->rwork);
420}
421
422static void lowcomms_write_space(struct sock *sk)
423{
424 struct connection *con = sock2con(sk);
425
David Millerb36930d2010-11-10 21:56:39 -0800426 if (!con)
427 return;
428
429 clear_bit(SOCK_NOSPACE, &con->sock->flags);
430
431 if (test_and_clear_bit(CF_APP_LIMITED, &con->flags)) {
432 con->sock->sk->sk_write_pending--;
433 clear_bit(SOCK_ASYNC_NOSPACE, &con->sock->flags);
434 }
435
436 if (!test_and_set_bit(CF_WRITE_PENDING, &con->flags))
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100437 queue_work(send_workqueue, &con->swork);
438}
439
440static inline void lowcomms_connect_sock(struct connection *con)
441{
Lars Marowsky-Bree063c4c92009-08-11 16:18:23 -0500442 if (test_bit(CF_CLOSE, &con->flags))
443 return;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100444 if (!test_and_set_bit(CF_CONNECT_PENDING, &con->flags))
445 queue_work(send_workqueue, &con->swork);
446}
447
448static void lowcomms_state_change(struct sock *sk)
449{
450 if (sk->sk_state == TCP_ESTABLISHED)
451 lowcomms_write_space(sk);
452}
453
Christine Caulfield391fbdc2009-05-07 10:54:16 -0500454int dlm_lowcomms_connect_node(int nodeid)
455{
456 struct connection *con;
457
David Teigland04bedd72009-09-18 14:31:47 -0500458 /* with sctp there's no connecting without sending */
459 if (dlm_config.ci_protocol != 0)
460 return 0;
461
Christine Caulfield391fbdc2009-05-07 10:54:16 -0500462 if (nodeid == dlm_our_nodeid())
463 return 0;
464
465 con = nodeid2con(nodeid, GFP_NOFS);
466 if (!con)
467 return -ENOMEM;
468 lowcomms_connect_sock(con);
469 return 0;
470}
471
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100472/* Make a socket active */
Ying Xue4dd40f02012-08-10 14:58:42 +0800473static void add_sock(struct socket *sock, struct connection *con)
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100474{
475 con->sock = sock;
476
477 /* Install a data_ready callback */
478 con->sock->sk->sk_data_ready = lowcomms_data_ready;
479 con->sock->sk->sk_write_space = lowcomms_write_space;
480 con->sock->sk->sk_state_change = lowcomms_state_change;
481 con->sock->sk->sk_user_data = con;
Steven Whitehoused6d7b702008-11-12 16:49:48 -0600482 con->sock->sk->sk_allocation = GFP_NOFS;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100483}
484
485/* Add the port number to an IPv6 or 4 sockaddr and return the address
486 length */
487static void make_sockaddr(struct sockaddr_storage *saddr, uint16_t port,
488 int *addr_len)
489{
490 saddr->ss_family = dlm_local_addr[0]->ss_family;
491 if (saddr->ss_family == AF_INET) {
492 struct sockaddr_in *in4_addr = (struct sockaddr_in *)saddr;
493 in4_addr->sin_port = cpu_to_be16(port);
494 *addr_len = sizeof(struct sockaddr_in);
495 memset(&in4_addr->sin_zero, 0, sizeof(in4_addr->sin_zero));
496 } else {
497 struct sockaddr_in6 *in6_addr = (struct sockaddr_in6 *)saddr;
498 in6_addr->sin6_port = cpu_to_be16(port);
499 *addr_len = sizeof(struct sockaddr_in6);
500 }
Patrick Caulfield01c8cab2007-07-17 16:53:15 +0100501 memset((char *)saddr + *addr_len, 0, sizeof(struct sockaddr_storage) - *addr_len);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100502}
503
504/* Close a remote connection and tidy up */
505static void close_connection(struct connection *con, bool and_other)
506{
507 mutex_lock(&con->sock_mutex);
508
509 if (con->sock) {
510 sock_release(con->sock);
511 con->sock = NULL;
512 }
513 if (con->othercon && and_other) {
514 /* Will only re-enter once. */
515 close_connection(con->othercon, false);
516 }
517 if (con->rx_page) {
518 __free_page(con->rx_page);
519 con->rx_page = NULL;
520 }
Patrick Caulfield9e5f2822007-08-02 14:58:14 +0100521
Patrick Caulfield61d96be2007-08-20 15:13:38 +0100522 con->retries = 0;
523 mutex_unlock(&con->sock_mutex);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100524}
525
526/* We only send shutdown messages to nodes that are not part of the cluster */
527static void sctp_send_shutdown(sctp_assoc_t associd)
528{
529 static char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
530 struct msghdr outmessage;
531 struct cmsghdr *cmsg;
532 struct sctp_sndrcvinfo *sinfo;
533 int ret;
534 struct connection *con;
535
536 con = nodeid2con(0,0);
537 BUG_ON(con == NULL);
538
539 outmessage.msg_name = NULL;
540 outmessage.msg_namelen = 0;
541 outmessage.msg_control = outcmsg;
542 outmessage.msg_controllen = sizeof(outcmsg);
543 outmessage.msg_flags = MSG_EOR;
544
545 cmsg = CMSG_FIRSTHDR(&outmessage);
546 cmsg->cmsg_level = IPPROTO_SCTP;
547 cmsg->cmsg_type = SCTP_SNDRCV;
548 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
549 outmessage.msg_controllen = cmsg->cmsg_len;
550 sinfo = CMSG_DATA(cmsg);
551 memset(sinfo, 0x00, sizeof(struct sctp_sndrcvinfo));
552
553 sinfo->sinfo_flags |= MSG_EOF;
554 sinfo->sinfo_assoc_id = associd;
555
556 ret = kernel_sendmsg(con->sock, &outmessage, NULL, 0, 0);
557
558 if (ret != 0)
559 log_print("send EOF to node failed: %d", ret);
560}
561
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600562static void sctp_init_failed_foreach(struct connection *con)
563{
564 con->sctp_assoc = 0;
565 if (test_and_clear_bit(CF_CONNECT_PENDING, &con->flags)) {
566 if (!test_and_set_bit(CF_WRITE_PENDING, &con->flags))
567 queue_work(send_workqueue, &con->swork);
568 }
569}
570
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100571/* INIT failed but we don't know which node...
572 restart INIT on all pending nodes */
573static void sctp_init_failed(void)
574{
Matthias Kaehlcke7a936ce2008-05-12 10:04:51 -0500575 mutex_lock(&connections_lock);
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600576
577 foreach_conn(sctp_init_failed_foreach);
578
Matthias Kaehlcke7a936ce2008-05-12 10:04:51 -0500579 mutex_unlock(&connections_lock);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100580}
581
582/* Something happened to an association */
David Teigland617e82e2007-04-26 13:46:49 -0500583static void process_sctp_notification(struct connection *con,
584 struct msghdr *msg, char *buf)
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100585{
586 union sctp_notification *sn = (union sctp_notification *)buf;
587
588 if (sn->sn_header.sn_type == SCTP_ASSOC_CHANGE) {
589 switch (sn->sn_assoc_change.sac_state) {
590
591 case SCTP_COMM_UP:
592 case SCTP_RESTART:
593 {
594 /* Check that the new node is in the lockspace */
595 struct sctp_prim prim;
596 int nodeid;
597 int prim_len, ret;
598 int addr_len;
599 struct connection *new_con;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100600
601 /*
602 * We get this before any data for an association.
603 * We verify that the node is in the cluster and
604 * then peel off a socket for it.
605 */
606 if ((int)sn->sn_assoc_change.sac_assoc_id <= 0) {
607 log_print("COMM_UP for invalid assoc ID %d",
David Teigland617e82e2007-04-26 13:46:49 -0500608 (int)sn->sn_assoc_change.sac_assoc_id);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100609 sctp_init_failed();
610 return;
611 }
612 memset(&prim, 0, sizeof(struct sctp_prim));
613 prim_len = sizeof(struct sctp_prim);
614 prim.ssp_assoc_id = sn->sn_assoc_change.sac_assoc_id;
615
616 ret = kernel_getsockopt(con->sock,
617 IPPROTO_SCTP,
618 SCTP_PRIMARY_ADDR,
619 (char*)&prim,
620 &prim_len);
621 if (ret < 0) {
622 log_print("getsockopt/sctp_primary_addr on "
623 "new assoc %d failed : %d",
624 (int)sn->sn_assoc_change.sac_assoc_id,
625 ret);
626
627 /* Retry INIT later */
628 new_con = assoc2con(sn->sn_assoc_change.sac_assoc_id);
629 if (new_con)
630 clear_bit(CF_CONNECT_PENDING, &con->flags);
631 return;
632 }
633 make_sockaddr(&prim.ssp_addr, 0, &addr_len);
David Teigland36b71a82012-07-26 12:44:30 -0500634 if (addr_to_nodeid(&prim.ssp_addr, &nodeid)) {
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100635 unsigned char *b=(unsigned char *)&prim.ssp_addr;
636 log_print("reject connect from unknown addr");
Masatake YAMATObcaadf52011-07-04 12:25:51 +0900637 print_hex_dump_bytes("ss: ", DUMP_PREFIX_NONE,
638 b, sizeof(struct sockaddr_storage));
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100639 sctp_send_shutdown(prim.ssp_assoc_id);
640 return;
641 }
642
David Teigland748285c2009-05-15 10:50:57 -0500643 new_con = nodeid2con(nodeid, GFP_NOFS);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100644 if (!new_con)
645 return;
646
647 /* Peel off a new sock */
Benjamin Poirier2f2d76c2012-03-08 05:55:59 +0000648 sctp_lock_sock(con->sock->sk);
649 ret = sctp_do_peeloff(con->sock->sk,
650 sn->sn_assoc_change.sac_assoc_id,
651 &new_con->sock);
652 sctp_release_sock(con->sock->sk);
David Teigland6861f352009-09-24 15:58:23 -0500653 if (ret < 0) {
David Teigland617e82e2007-04-26 13:46:49 -0500654 log_print("Can't peel off a socket for "
David Teigland6861f352009-09-24 15:58:23 -0500655 "connection %d to node %d: err=%d",
Benjamin Poirier2f2d76c2012-03-08 05:55:59 +0000656 (int)sn->sn_assoc_change.sac_assoc_id,
657 nodeid, ret);
David Teigland6861f352009-09-24 15:58:23 -0500658 return;
659 }
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100660 add_sock(new_con->sock, new_con);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100661
David Teigland6861f352009-09-24 15:58:23 -0500662 log_print("connecting to %d sctp association %d",
663 nodeid, (int)sn->sn_assoc_change.sac_assoc_id);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100664
665 /* Send any pending writes */
666 clear_bit(CF_CONNECT_PENDING, &new_con->flags);
667 clear_bit(CF_INIT_PENDING, &con->flags);
668 if (!test_and_set_bit(CF_WRITE_PENDING, &new_con->flags)) {
669 queue_work(send_workqueue, &new_con->swork);
670 }
671 if (!test_and_set_bit(CF_READ_PENDING, &new_con->flags))
672 queue_work(recv_workqueue, &new_con->rwork);
673 }
674 break;
675
676 case SCTP_COMM_LOST:
677 case SCTP_SHUTDOWN_COMP:
678 {
679 con = assoc2con(sn->sn_assoc_change.sac_assoc_id);
680 if (con) {
681 con->sctp_assoc = 0;
682 }
683 }
684 break;
685
686 /* We don't know which INIT failed, so clear the PENDING flags
687 * on them all. if assoc_id is zero then it will then try
688 * again */
689
690 case SCTP_CANT_STR_ASSOC:
691 {
692 log_print("Can't start SCTP association - retrying");
693 sctp_init_failed();
694 }
695 break;
696
697 default:
698 log_print("unexpected SCTP assoc change id=%d state=%d",
699 (int)sn->sn_assoc_change.sac_assoc_id,
700 sn->sn_assoc_change.sac_state);
701 }
702 }
703}
704
705/* Data received from remote end */
706static int receive_from_sock(struct connection *con)
707{
708 int ret = 0;
709 struct msghdr msg = {};
710 struct kvec iov[2];
711 unsigned len;
712 int r;
713 int call_again_soon = 0;
714 int nvec;
715 char incmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
716
717 mutex_lock(&con->sock_mutex);
718
719 if (con->sock == NULL) {
720 ret = -EAGAIN;
721 goto out_close;
722 }
723
724 if (con->rx_page == NULL) {
725 /*
726 * This doesn't need to be atomic, but I think it should
727 * improve performance if it is.
728 */
729 con->rx_page = alloc_page(GFP_ATOMIC);
730 if (con->rx_page == NULL)
731 goto out_resched;
732 cbuf_init(&con->cb, PAGE_CACHE_SIZE);
733 }
734
735 /* Only SCTP needs these really */
736 memset(&incmsg, 0, sizeof(incmsg));
737 msg.msg_control = incmsg;
738 msg.msg_controllen = sizeof(incmsg);
739
740 /*
741 * iov[0] is the bit of the circular buffer between the current end
742 * point (cb.base + cb.len) and the end of the buffer.
743 */
744 iov[0].iov_len = con->cb.base - cbuf_data(&con->cb);
745 iov[0].iov_base = page_address(con->rx_page) + cbuf_data(&con->cb);
746 iov[1].iov_len = 0;
747 nvec = 1;
748
749 /*
750 * iov[1] is the bit of the circular buffer between the start of the
751 * buffer and the start of the currently used section (cb.base)
752 */
753 if (cbuf_data(&con->cb) >= con->cb.base) {
754 iov[0].iov_len = PAGE_CACHE_SIZE - cbuf_data(&con->cb);
755 iov[1].iov_len = con->cb.base;
756 iov[1].iov_base = page_address(con->rx_page);
757 nvec = 2;
758 }
759 len = iov[0].iov_len + iov[1].iov_len;
760
761 r = ret = kernel_recvmsg(con->sock, &msg, iov, nvec, len,
762 MSG_DONTWAIT | MSG_NOSIGNAL);
763 if (ret <= 0)
764 goto out_close;
765
766 /* Process SCTP notifications */
767 if (msg.msg_flags & MSG_NOTIFICATION) {
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100768 msg.msg_control = incmsg;
769 msg.msg_controllen = sizeof(incmsg);
770
771 process_sctp_notification(con, &msg,
David Teigland617e82e2007-04-26 13:46:49 -0500772 page_address(con->rx_page) + con->cb.base);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100773 mutex_unlock(&con->sock_mutex);
774 return 0;
775 }
776 BUG_ON(con->nodeid == 0);
777
778 if (ret == len)
779 call_again_soon = 1;
780 cbuf_add(&con->cb, ret);
781 ret = dlm_process_incoming_buffer(con->nodeid,
782 page_address(con->rx_page),
783 con->cb.base, con->cb.len,
784 PAGE_CACHE_SIZE);
785 if (ret == -EBADMSG) {
David Teigland617e82e2007-04-26 13:46:49 -0500786 log_print("lowcomms: addr=%p, base=%u, len=%u, "
787 "iov_len=%u, iov_base[0]=%p, read=%d",
788 page_address(con->rx_page), con->cb.base, con->cb.len,
789 len, iov[0].iov_base, r);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100790 }
791 if (ret < 0)
792 goto out_close;
793 cbuf_eat(&con->cb, ret);
794
795 if (cbuf_empty(&con->cb) && !call_again_soon) {
796 __free_page(con->rx_page);
797 con->rx_page = NULL;
798 }
799
800 if (call_again_soon)
801 goto out_resched;
802 mutex_unlock(&con->sock_mutex);
803 return 0;
804
805out_resched:
806 if (!test_and_set_bit(CF_READ_PENDING, &con->flags))
807 queue_work(recv_workqueue, &con->rwork);
808 mutex_unlock(&con->sock_mutex);
809 return -EAGAIN;
810
811out_close:
812 mutex_unlock(&con->sock_mutex);
Patrick Caulfield9e5f2822007-08-02 14:58:14 +0100813 if (ret != -EAGAIN) {
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100814 close_connection(con, false);
815 /* Reconnect when there is something to send */
816 }
817 /* Don't return success if we really got EOF */
818 if (ret == 0)
819 ret = -EAGAIN;
820
821 return ret;
822}
823
824/* Listening socket is busy, accept a connection */
825static int tcp_accept_from_sock(struct connection *con)
826{
827 int result;
828 struct sockaddr_storage peeraddr;
829 struct socket *newsock;
830 int len;
831 int nodeid;
832 struct connection *newcon;
833 struct connection *addcon;
834
David Teigland513ef592012-03-30 11:46:08 -0500835 mutex_lock(&connections_lock);
836 if (!dlm_allow_conn) {
837 mutex_unlock(&connections_lock);
838 return -1;
839 }
840 mutex_unlock(&connections_lock);
841
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100842 memset(&peeraddr, 0, sizeof(peeraddr));
843 result = sock_create_kern(dlm_local_addr[0]->ss_family, SOCK_STREAM,
844 IPPROTO_TCP, &newsock);
845 if (result < 0)
846 return -ENOMEM;
847
848 mutex_lock_nested(&con->sock_mutex, 0);
849
850 result = -ENOTCONN;
851 if (con->sock == NULL)
852 goto accept_err;
853
854 newsock->type = con->sock->type;
855 newsock->ops = con->sock->ops;
856
857 result = con->sock->ops->accept(con->sock, newsock, O_NONBLOCK);
858 if (result < 0)
859 goto accept_err;
860
861 /* Get the connected socket's peer */
862 memset(&peeraddr, 0, sizeof(peeraddr));
863 if (newsock->ops->getname(newsock, (struct sockaddr *)&peeraddr,
864 &len, 2)) {
865 result = -ECONNABORTED;
866 goto accept_err;
867 }
868
869 /* Get the new node's NODEID */
870 make_sockaddr(&peeraddr, 0, &len);
David Teigland36b71a82012-07-26 12:44:30 -0500871 if (addr_to_nodeid(&peeraddr, &nodeid)) {
Masatake YAMATObcaadf52011-07-04 12:25:51 +0900872 unsigned char *b=(unsigned char *)&peeraddr;
David Teigland617e82e2007-04-26 13:46:49 -0500873 log_print("connect from non cluster node");
Masatake YAMATObcaadf52011-07-04 12:25:51 +0900874 print_hex_dump_bytes("ss: ", DUMP_PREFIX_NONE,
875 b, sizeof(struct sockaddr_storage));
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100876 sock_release(newsock);
877 mutex_unlock(&con->sock_mutex);
878 return -1;
879 }
880
881 log_print("got connection from %d", nodeid);
882
883 /* Check to see if we already have a connection to this node. This
884 * could happen if the two nodes initiate a connection at roughly
885 * the same time and the connections cross on the wire.
886 * In this case we store the incoming one in "othercon"
887 */
David Teigland748285c2009-05-15 10:50:57 -0500888 newcon = nodeid2con(nodeid, GFP_NOFS);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100889 if (!newcon) {
890 result = -ENOMEM;
891 goto accept_err;
892 }
893 mutex_lock_nested(&newcon->sock_mutex, 1);
894 if (newcon->sock) {
895 struct connection *othercon = newcon->othercon;
896
897 if (!othercon) {
David Teigland748285c2009-05-15 10:50:57 -0500898 othercon = kmem_cache_zalloc(con_cache, GFP_NOFS);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100899 if (!othercon) {
David Teigland617e82e2007-04-26 13:46:49 -0500900 log_print("failed to allocate incoming socket");
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100901 mutex_unlock(&newcon->sock_mutex);
902 result = -ENOMEM;
903 goto accept_err;
904 }
905 othercon->nodeid = nodeid;
906 othercon->rx_action = receive_from_sock;
907 mutex_init(&othercon->sock_mutex);
908 INIT_WORK(&othercon->swork, process_send_sockets);
909 INIT_WORK(&othercon->rwork, process_recv_sockets);
910 set_bit(CF_IS_OTHERCON, &othercon->flags);
Patrick Caulfield61d96be2007-08-20 15:13:38 +0100911 }
912 if (!othercon->sock) {
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100913 newcon->othercon = othercon;
Patrick Caulfield97d84832007-06-27 11:36:23 +0100914 othercon->sock = newsock;
915 newsock->sk->sk_user_data = othercon;
916 add_sock(newsock, othercon);
917 addcon = othercon;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100918 }
Patrick Caulfield97d84832007-06-27 11:36:23 +0100919 else {
920 printk("Extra connection from node %d attempted\n", nodeid);
921 result = -EAGAIN;
akpm@linux-foundation.orgf4fadb23c2007-06-27 14:43:37 -0700922 mutex_unlock(&newcon->sock_mutex);
Patrick Caulfield97d84832007-06-27 11:36:23 +0100923 goto accept_err;
924 }
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100925 }
926 else {
927 newsock->sk->sk_user_data = newcon;
928 newcon->rx_action = receive_from_sock;
929 add_sock(newsock, newcon);
930 addcon = newcon;
931 }
932
933 mutex_unlock(&newcon->sock_mutex);
934
935 /*
936 * Add it to the active queue in case we got data
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300937 * between processing the accept adding the socket
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100938 * to the read_sockets list
939 */
940 if (!test_and_set_bit(CF_READ_PENDING, &addcon->flags))
941 queue_work(recv_workqueue, &addcon->rwork);
942 mutex_unlock(&con->sock_mutex);
943
944 return 0;
945
946accept_err:
947 mutex_unlock(&con->sock_mutex);
948 sock_release(newsock);
949
950 if (result != -EAGAIN)
David Teigland617e82e2007-04-26 13:46:49 -0500951 log_print("error accepting connection from node: %d", result);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100952 return result;
953}
954
955static void free_entry(struct writequeue_entry *e)
956{
957 __free_page(e->page);
958 kfree(e);
959}
960
961/* Initiate an SCTP association.
962 This is a special case of send_to_sock() in that we don't yet have a
963 peeled-off socket for this association, so we use the listening socket
964 and add the primary IP address of the remote node.
965 */
966static void sctp_init_assoc(struct connection *con)
967{
968 struct sockaddr_storage rem_addr;
969 char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
970 struct msghdr outmessage;
971 struct cmsghdr *cmsg;
972 struct sctp_sndrcvinfo *sinfo;
973 struct connection *base_con;
974 struct writequeue_entry *e;
975 int len, offset;
976 int ret;
977 int addrlen;
978 struct kvec iov[1];
979
980 if (test_and_set_bit(CF_INIT_PENDING, &con->flags))
981 return;
982
983 if (con->retries++ > MAX_CONNECT_RETRIES)
984 return;
985
David Teigland36b71a82012-07-26 12:44:30 -0500986 if (nodeid_to_addr(con->nodeid, NULL, (struct sockaddr *)&rem_addr)) {
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +0100987 log_print("no address for nodeid %d", con->nodeid);
988 return;
989 }
990 base_con = nodeid2con(0, 0);
991 BUG_ON(base_con == NULL);
992
993 make_sockaddr(&rem_addr, dlm_config.ci_tcp_port, &addrlen);
994
995 outmessage.msg_name = &rem_addr;
996 outmessage.msg_namelen = addrlen;
997 outmessage.msg_control = outcmsg;
998 outmessage.msg_controllen = sizeof(outcmsg);
999 outmessage.msg_flags = MSG_EOR;
1000
1001 spin_lock(&con->writequeue_lock);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001002
David Teigland04bedd72009-09-18 14:31:47 -05001003 if (list_empty(&con->writequeue)) {
1004 spin_unlock(&con->writequeue_lock);
1005 log_print("writequeue empty for nodeid %d", con->nodeid);
1006 return;
1007 }
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001008
David Teigland04bedd72009-09-18 14:31:47 -05001009 e = list_first_entry(&con->writequeue, struct writequeue_entry, list);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001010 len = e->len;
1011 offset = e->offset;
1012 spin_unlock(&con->writequeue_lock);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001013
1014 /* Send the first block off the write queue */
1015 iov[0].iov_base = page_address(e->page)+offset;
1016 iov[0].iov_len = len;
1017
1018 cmsg = CMSG_FIRSTHDR(&outmessage);
1019 cmsg->cmsg_level = IPPROTO_SCTP;
1020 cmsg->cmsg_type = SCTP_SNDRCV;
1021 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
1022 sinfo = CMSG_DATA(cmsg);
1023 memset(sinfo, 0x00, sizeof(struct sctp_sndrcvinfo));
1024 sinfo->sinfo_ppid = cpu_to_le32(dlm_our_nodeid());
1025 outmessage.msg_controllen = cmsg->cmsg_len;
1026
1027 ret = kernel_sendmsg(base_con->sock, &outmessage, iov, 1, len);
1028 if (ret < 0) {
David Teigland617e82e2007-04-26 13:46:49 -05001029 log_print("Send first packet to node %d failed: %d",
1030 con->nodeid, ret);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001031
1032 /* Try again later */
1033 clear_bit(CF_CONNECT_PENDING, &con->flags);
1034 clear_bit(CF_INIT_PENDING, &con->flags);
1035 }
1036 else {
1037 spin_lock(&con->writequeue_lock);
1038 e->offset += ret;
1039 e->len -= ret;
1040
1041 if (e->len == 0 && e->users == 0) {
1042 list_del(&e->list);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001043 free_entry(e);
1044 }
1045 spin_unlock(&con->writequeue_lock);
1046 }
1047}
1048
1049/* Connect a new socket to its peer */
1050static void tcp_connect_to_sock(struct connection *con)
1051{
Lon Hohberger6bd8fed2007-10-25 18:51:54 -04001052 struct sockaddr_storage saddr, src_addr;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001053 int addr_len;
Casey Dahlina89d63a2009-07-14 12:17:51 -05001054 struct socket *sock = NULL;
David Teiglandcb2d45d2010-11-12 11:12:55 -06001055 int one = 1;
David Teigland36b71a82012-07-26 12:44:30 -05001056 int result;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001057
1058 if (con->nodeid == 0) {
1059 log_print("attempt to connect sock 0 foiled");
1060 return;
1061 }
1062
1063 mutex_lock(&con->sock_mutex);
1064 if (con->retries++ > MAX_CONNECT_RETRIES)
1065 goto out;
1066
1067 /* Some odd races can cause double-connects, ignore them */
David Teigland36b71a82012-07-26 12:44:30 -05001068 if (con->sock)
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001069 goto out;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001070
1071 /* Create a socket to communicate with */
1072 result = sock_create_kern(dlm_local_addr[0]->ss_family, SOCK_STREAM,
1073 IPPROTO_TCP, &sock);
1074 if (result < 0)
1075 goto out_err;
1076
1077 memset(&saddr, 0, sizeof(saddr));
David Teigland36b71a82012-07-26 12:44:30 -05001078 result = nodeid_to_addr(con->nodeid, &saddr, NULL);
1079 if (result < 0) {
1080 log_print("no address for nodeid %d", con->nodeid);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001081 goto out_err;
David Teigland36b71a82012-07-26 12:44:30 -05001082 }
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001083
1084 sock->sk->sk_user_data = con;
1085 con->rx_action = receive_from_sock;
1086 con->connect_action = tcp_connect_to_sock;
1087 add_sock(sock, con);
1088
Lon Hohberger6bd8fed2007-10-25 18:51:54 -04001089 /* Bind to our cluster-known address connecting to avoid
1090 routing problems */
1091 memcpy(&src_addr, dlm_local_addr[0], sizeof(src_addr));
1092 make_sockaddr(&src_addr, 0, &addr_len);
1093 result = sock->ops->bind(sock, (struct sockaddr *) &src_addr,
1094 addr_len);
1095 if (result < 0) {
1096 log_print("could not bind for connect: %d", result);
1097 /* This *may* not indicate a critical error */
1098 }
1099
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001100 make_sockaddr(&saddr, dlm_config.ci_tcp_port, &addr_len);
1101
1102 log_print("connecting to %d", con->nodeid);
David Teiglandcb2d45d2010-11-12 11:12:55 -06001103
1104 /* Turn off Nagle's algorithm */
1105 kernel_setsockopt(sock, SOL_TCP, TCP_NODELAY, (char *)&one,
1106 sizeof(one));
1107
David Teigland36b71a82012-07-26 12:44:30 -05001108 result = sock->ops->connect(sock, (struct sockaddr *)&saddr, addr_len,
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001109 O_NONBLOCK);
1110 if (result == -EINPROGRESS)
1111 result = 0;
1112 if (result == 0)
1113 goto out;
1114
1115out_err:
1116 if (con->sock) {
1117 sock_release(con->sock);
1118 con->sock = NULL;
Casey Dahlina89d63a2009-07-14 12:17:51 -05001119 } else if (sock) {
1120 sock_release(sock);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001121 }
1122 /*
1123 * Some errors are fatal and this list might need adjusting. For other
1124 * errors we try again until the max number of retries is reached.
1125 */
David Teigland36b71a82012-07-26 12:44:30 -05001126 if (result != -EHOSTUNREACH &&
1127 result != -ENETUNREACH &&
1128 result != -ENETDOWN &&
1129 result != -EINVAL &&
1130 result != -EPROTONOSUPPORT) {
1131 log_print("connect %d try %d error %d", con->nodeid,
1132 con->retries, result);
1133 mutex_unlock(&con->sock_mutex);
1134 msleep(1000);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001135 lowcomms_connect_sock(con);
David Teigland36b71a82012-07-26 12:44:30 -05001136 return;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001137 }
1138out:
1139 mutex_unlock(&con->sock_mutex);
1140 return;
1141}
1142
1143static struct socket *tcp_create_listen_sock(struct connection *con,
1144 struct sockaddr_storage *saddr)
1145{
1146 struct socket *sock = NULL;
1147 int result = 0;
1148 int one = 1;
1149 int addr_len;
1150
1151 if (dlm_local_addr[0]->ss_family == AF_INET)
1152 addr_len = sizeof(struct sockaddr_in);
1153 else
1154 addr_len = sizeof(struct sockaddr_in6);
1155
1156 /* Create a socket to communicate with */
David Teigland617e82e2007-04-26 13:46:49 -05001157 result = sock_create_kern(dlm_local_addr[0]->ss_family, SOCK_STREAM,
1158 IPPROTO_TCP, &sock);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001159 if (result < 0) {
David Teigland617e82e2007-04-26 13:46:49 -05001160 log_print("Can't create listening comms socket");
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001161 goto create_out;
1162 }
1163
David Teiglandcb2d45d2010-11-12 11:12:55 -06001164 /* Turn off Nagle's algorithm */
1165 kernel_setsockopt(sock, SOL_TCP, TCP_NODELAY, (char *)&one,
1166 sizeof(one));
1167
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001168 result = kernel_setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
1169 (char *)&one, sizeof(one));
1170
1171 if (result < 0) {
David Teigland617e82e2007-04-26 13:46:49 -05001172 log_print("Failed to set SO_REUSEADDR on socket: %d", result);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001173 }
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001174 con->rx_action = tcp_accept_from_sock;
1175 con->connect_action = tcp_connect_to_sock;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001176
1177 /* Bind to our port */
1178 make_sockaddr(saddr, dlm_config.ci_tcp_port, &addr_len);
1179 result = sock->ops->bind(sock, (struct sockaddr *) saddr, addr_len);
1180 if (result < 0) {
David Teigland617e82e2007-04-26 13:46:49 -05001181 log_print("Can't bind to port %d", dlm_config.ci_tcp_port);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001182 sock_release(sock);
1183 sock = NULL;
1184 con->sock = NULL;
1185 goto create_out;
1186 }
1187 result = kernel_setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE,
1188 (char *)&one, sizeof(one));
1189 if (result < 0) {
David Teigland617e82e2007-04-26 13:46:49 -05001190 log_print("Set keepalive failed: %d", result);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001191 }
1192
1193 result = sock->ops->listen(sock, 5);
1194 if (result < 0) {
David Teigland617e82e2007-04-26 13:46:49 -05001195 log_print("Can't listen on port %d", dlm_config.ci_tcp_port);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001196 sock_release(sock);
1197 sock = NULL;
1198 goto create_out;
1199 }
1200
1201create_out:
1202 return sock;
1203}
1204
1205/* Get local addresses */
1206static void init_local(void)
1207{
1208 struct sockaddr_storage sas, *addr;
1209 int i;
1210
Patrick Caulfield30d3a232007-04-23 16:26:21 +01001211 dlm_local_count = 0;
David Teigland1b189b82012-03-21 09:18:34 -05001212 for (i = 0; i < DLM_MAX_ADDR_COUNT; i++) {
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001213 if (dlm_our_addr(&sas, i))
1214 break;
1215
David Teigland573c24c2009-11-30 16:34:43 -06001216 addr = kmalloc(sizeof(*addr), GFP_NOFS);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001217 if (!addr)
1218 break;
1219 memcpy(addr, &sas, sizeof(*addr));
1220 dlm_local_addr[dlm_local_count++] = addr;
1221 }
1222}
1223
David Teigland617e82e2007-04-26 13:46:49 -05001224/* Bind to an IP address. SCTP allows multiple address so it can do
1225 multi-homing */
1226static int add_sctp_bind_addr(struct connection *sctp_con,
1227 struct sockaddr_storage *addr,
1228 int addr_len, int num)
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001229{
1230 int result = 0;
1231
1232 if (num == 1)
1233 result = kernel_bind(sctp_con->sock,
1234 (struct sockaddr *) addr,
1235 addr_len);
1236 else
1237 result = kernel_setsockopt(sctp_con->sock, SOL_SCTP,
1238 SCTP_SOCKOPT_BINDX_ADD,
1239 (char *)addr, addr_len);
1240
1241 if (result < 0)
1242 log_print("Can't bind to port %d addr number %d",
1243 dlm_config.ci_tcp_port, num);
1244
1245 return result;
1246}
1247
1248/* Initialise SCTP socket and bind to all interfaces */
1249static int sctp_listen_for_all(void)
1250{
1251 struct socket *sock = NULL;
1252 struct sockaddr_storage localaddr;
1253 struct sctp_event_subscribe subscribe;
1254 int result = -EINVAL, num = 1, i, addr_len;
David Teigland573c24c2009-11-30 16:34:43 -06001255 struct connection *con = nodeid2con(0, GFP_NOFS);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001256 int bufsize = NEEDED_RMEM;
1257
1258 if (!con)
1259 return -ENOMEM;
1260
1261 log_print("Using SCTP for communications");
1262
1263 result = sock_create_kern(dlm_local_addr[0]->ss_family, SOCK_SEQPACKET,
1264 IPPROTO_SCTP, &sock);
1265 if (result < 0) {
1266 log_print("Can't create comms socket, check SCTP is loaded");
1267 goto out;
1268 }
1269
1270 /* Listen for events */
1271 memset(&subscribe, 0, sizeof(subscribe));
1272 subscribe.sctp_data_io_event = 1;
1273 subscribe.sctp_association_event = 1;
1274 subscribe.sctp_send_failure_event = 1;
1275 subscribe.sctp_shutdown_event = 1;
1276 subscribe.sctp_partial_delivery_event = 1;
1277
David S. Millerdf61c952007-11-06 23:48:57 -08001278 result = kernel_setsockopt(sock, SOL_SOCKET, SO_RCVBUFFORCE,
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001279 (char *)&bufsize, sizeof(bufsize));
1280 if (result)
David Teigland617e82e2007-04-26 13:46:49 -05001281 log_print("Error increasing buffer space on socket %d", result);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001282
1283 result = kernel_setsockopt(sock, SOL_SCTP, SCTP_EVENTS,
David Teigland617e82e2007-04-26 13:46:49 -05001284 (char *)&subscribe, sizeof(subscribe));
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001285 if (result < 0) {
1286 log_print("Failed to set SCTP_EVENTS on socket: result=%d",
1287 result);
1288 goto create_delsock;
1289 }
1290
1291 /* Init con struct */
1292 sock->sk->sk_user_data = con;
1293 con->sock = sock;
1294 con->sock->sk->sk_data_ready = lowcomms_data_ready;
1295 con->rx_action = receive_from_sock;
1296 con->connect_action = sctp_init_assoc;
1297
1298 /* Bind to all interfaces. */
1299 for (i = 0; i < dlm_local_count; i++) {
1300 memcpy(&localaddr, dlm_local_addr[i], sizeof(localaddr));
1301 make_sockaddr(&localaddr, dlm_config.ci_tcp_port, &addr_len);
1302
1303 result = add_sctp_bind_addr(con, &localaddr, addr_len, num);
1304 if (result)
1305 goto create_delsock;
1306 ++num;
1307 }
1308
1309 result = sock->ops->listen(sock, 5);
1310 if (result < 0) {
1311 log_print("Can't set socket listening");
1312 goto create_delsock;
1313 }
1314
1315 return 0;
1316
1317create_delsock:
1318 sock_release(sock);
1319 con->sock = NULL;
1320out:
1321 return result;
1322}
1323
1324static int tcp_listen_for_all(void)
1325{
1326 struct socket *sock = NULL;
David Teigland573c24c2009-11-30 16:34:43 -06001327 struct connection *con = nodeid2con(0, GFP_NOFS);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001328 int result = -EINVAL;
1329
1330 if (!con)
1331 return -ENOMEM;
1332
1333 /* We don't support multi-homed hosts */
1334 if (dlm_local_addr[1] != NULL) {
David Teigland617e82e2007-04-26 13:46:49 -05001335 log_print("TCP protocol can't handle multi-homed hosts, "
1336 "try SCTP");
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001337 return -EINVAL;
1338 }
1339
1340 log_print("Using TCP for communications");
1341
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001342 sock = tcp_create_listen_sock(con, dlm_local_addr[0]);
1343 if (sock) {
1344 add_sock(sock, con);
1345 result = 0;
1346 }
1347 else {
1348 result = -EADDRINUSE;
1349 }
1350
1351 return result;
1352}
1353
1354
1355
1356static struct writequeue_entry *new_writequeue_entry(struct connection *con,
1357 gfp_t allocation)
1358{
1359 struct writequeue_entry *entry;
1360
1361 entry = kmalloc(sizeof(struct writequeue_entry), allocation);
1362 if (!entry)
1363 return NULL;
1364
1365 entry->page = alloc_page(allocation);
1366 if (!entry->page) {
1367 kfree(entry);
1368 return NULL;
1369 }
1370
1371 entry->offset = 0;
1372 entry->len = 0;
1373 entry->end = 0;
1374 entry->users = 0;
1375 entry->con = con;
1376
1377 return entry;
1378}
1379
David Teigland617e82e2007-04-26 13:46:49 -05001380void *dlm_lowcomms_get_buffer(int nodeid, int len, gfp_t allocation, char **ppc)
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001381{
1382 struct connection *con;
1383 struct writequeue_entry *e;
1384 int offset = 0;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001385
1386 con = nodeid2con(nodeid, allocation);
1387 if (!con)
1388 return NULL;
1389
1390 spin_lock(&con->writequeue_lock);
1391 e = list_entry(con->writequeue.prev, struct writequeue_entry, list);
1392 if ((&e->list == &con->writequeue) ||
1393 (PAGE_CACHE_SIZE - e->end < len)) {
1394 e = NULL;
1395 } else {
1396 offset = e->end;
1397 e->end += len;
Wei Yongjuneeee2b52012-10-18 22:57:19 +08001398 e->users++;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001399 }
1400 spin_unlock(&con->writequeue_lock);
1401
1402 if (e) {
1403 got_one:
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001404 *ppc = page_address(e->page) + offset;
1405 return e;
1406 }
1407
1408 e = new_writequeue_entry(con, allocation);
1409 if (e) {
1410 spin_lock(&con->writequeue_lock);
1411 offset = e->end;
1412 e->end += len;
Wei Yongjuneeee2b52012-10-18 22:57:19 +08001413 e->users++;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001414 list_add_tail(&e->list, &con->writequeue);
1415 spin_unlock(&con->writequeue_lock);
1416 goto got_one;
1417 }
1418 return NULL;
1419}
1420
1421void dlm_lowcomms_commit_buffer(void *mh)
1422{
1423 struct writequeue_entry *e = (struct writequeue_entry *)mh;
1424 struct connection *con = e->con;
1425 int users;
1426
1427 spin_lock(&con->writequeue_lock);
1428 users = --e->users;
1429 if (users)
1430 goto out;
1431 e->len = e->end - e->offset;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001432 spin_unlock(&con->writequeue_lock);
1433
1434 if (!test_and_set_bit(CF_WRITE_PENDING, &con->flags)) {
1435 queue_work(send_workqueue, &con->swork);
1436 }
1437 return;
1438
1439out:
1440 spin_unlock(&con->writequeue_lock);
1441 return;
1442}
1443
1444/* Send a message */
1445static void send_to_sock(struct connection *con)
1446{
1447 int ret = 0;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001448 const int msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL;
1449 struct writequeue_entry *e;
1450 int len, offset;
Bob Petersonf92c8dd2010-11-12 11:15:20 -06001451 int count = 0;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001452
1453 mutex_lock(&con->sock_mutex);
1454 if (con->sock == NULL)
1455 goto out_connect;
1456
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001457 spin_lock(&con->writequeue_lock);
1458 for (;;) {
1459 e = list_entry(con->writequeue.next, struct writequeue_entry,
1460 list);
1461 if ((struct list_head *) e == &con->writequeue)
1462 break;
1463
1464 len = e->len;
1465 offset = e->offset;
1466 BUG_ON(len == 0 && e->users == 0);
1467 spin_unlock(&con->writequeue_lock);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001468
1469 ret = 0;
1470 if (len) {
Paolo Bonzini1329e3f2009-08-24 13:18:04 -05001471 ret = kernel_sendpage(con->sock, e->page, offset, len,
1472 msg_flags);
Patrick Caulfieldd66f8272007-09-14 08:49:21 +01001473 if (ret == -EAGAIN || ret == 0) {
David Millerb36930d2010-11-10 21:56:39 -08001474 if (ret == -EAGAIN &&
1475 test_bit(SOCK_ASYNC_NOSPACE, &con->sock->flags) &&
1476 !test_and_set_bit(CF_APP_LIMITED, &con->flags)) {
1477 /* Notify TCP that we're limited by the
1478 * application window size.
1479 */
1480 set_bit(SOCK_NOSPACE, &con->sock->flags);
1481 con->sock->sk->sk_write_pending++;
1482 }
Patrick Caulfieldd66f8272007-09-14 08:49:21 +01001483 cond_resched();
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001484 goto out;
Ying Xue9c5bef52012-08-13 14:29:55 +08001485 } else if (ret < 0)
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001486 goto send_error;
Patrick Caulfieldd66f8272007-09-14 08:49:21 +01001487 }
Bob Petersonf92c8dd2010-11-12 11:15:20 -06001488
1489 /* Don't starve people filling buffers */
1490 if (++count >= MAX_SEND_MSG_COUNT) {
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001491 cond_resched();
Bob Petersonf92c8dd2010-11-12 11:15:20 -06001492 count = 0;
1493 }
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001494
1495 spin_lock(&con->writequeue_lock);
1496 e->offset += ret;
1497 e->len -= ret;
1498
1499 if (e->len == 0 && e->users == 0) {
1500 list_del(&e->list);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001501 free_entry(e);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001502 }
1503 }
1504 spin_unlock(&con->writequeue_lock);
1505out:
1506 mutex_unlock(&con->sock_mutex);
1507 return;
1508
1509send_error:
1510 mutex_unlock(&con->sock_mutex);
1511 close_connection(con, false);
1512 lowcomms_connect_sock(con);
1513 return;
1514
1515out_connect:
1516 mutex_unlock(&con->sock_mutex);
1517 if (!test_bit(CF_INIT_PENDING, &con->flags))
1518 lowcomms_connect_sock(con);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001519}
1520
1521static void clean_one_writequeue(struct connection *con)
1522{
Christine Caulfield5e9ccc32009-01-28 12:57:40 -06001523 struct writequeue_entry *e, *safe;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001524
1525 spin_lock(&con->writequeue_lock);
Christine Caulfield5e9ccc32009-01-28 12:57:40 -06001526 list_for_each_entry_safe(e, safe, &con->writequeue, list) {
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001527 list_del(&e->list);
1528 free_entry(e);
1529 }
1530 spin_unlock(&con->writequeue_lock);
1531}
1532
1533/* Called from recovery when it knows that a node has
1534 left the cluster */
1535int dlm_lowcomms_close(int nodeid)
1536{
1537 struct connection *con;
David Teigland36b71a82012-07-26 12:44:30 -05001538 struct dlm_node_addr *na;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001539
1540 log_print("closing connection to node %d", nodeid);
1541 con = nodeid2con(nodeid, 0);
1542 if (con) {
Lars Marowsky-Bree063c4c92009-08-11 16:18:23 -05001543 clear_bit(CF_CONNECT_PENDING, &con->flags);
1544 clear_bit(CF_WRITE_PENDING, &con->flags);
1545 set_bit(CF_CLOSE, &con->flags);
1546 if (cancel_work_sync(&con->swork))
1547 log_print("canceled swork for node %d", nodeid);
1548 if (cancel_work_sync(&con->rwork))
1549 log_print("canceled rwork for node %d", nodeid);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001550 clean_one_writequeue(con);
1551 close_connection(con, true);
1552 }
David Teigland36b71a82012-07-26 12:44:30 -05001553
1554 spin_lock(&dlm_node_addrs_spin);
1555 na = find_node_addr(nodeid);
1556 if (na) {
1557 list_del(&na->list);
1558 while (na->addr_count--)
1559 kfree(na->addr[na->addr_count]);
1560 kfree(na);
1561 }
1562 spin_unlock(&dlm_node_addrs_spin);
1563
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001564 return 0;
1565}
1566
1567/* Receive workqueue function */
1568static void process_recv_sockets(struct work_struct *work)
1569{
1570 struct connection *con = container_of(work, struct connection, rwork);
1571 int err;
1572
1573 clear_bit(CF_READ_PENDING, &con->flags);
1574 do {
1575 err = con->rx_action(con);
1576 } while (!err);
1577}
1578
1579/* Send workqueue function */
1580static void process_send_sockets(struct work_struct *work)
1581{
1582 struct connection *con = container_of(work, struct connection, swork);
1583
1584 if (test_and_clear_bit(CF_CONNECT_PENDING, &con->flags)) {
1585 con->connect_action(con);
Lars Marowsky-Bree063c4c92009-08-11 16:18:23 -05001586 set_bit(CF_WRITE_PENDING, &con->flags);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001587 }
Lars Marowsky-Bree063c4c92009-08-11 16:18:23 -05001588 if (test_and_clear_bit(CF_WRITE_PENDING, &con->flags))
1589 send_to_sock(con);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001590}
1591
1592
1593/* Discard all entries on the write queues */
1594static void clean_writequeues(void)
1595{
Christine Caulfield5e9ccc32009-01-28 12:57:40 -06001596 foreach_conn(clean_one_writequeue);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001597}
1598
1599static void work_stop(void)
1600{
1601 destroy_workqueue(recv_workqueue);
1602 destroy_workqueue(send_workqueue);
1603}
1604
1605static int work_start(void)
1606{
David Teiglande43f0552011-03-10 13:22:34 -06001607 recv_workqueue = alloc_workqueue("dlm_recv",
1608 WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
Namhyung Kimb9d41052010-12-13 13:42:24 -06001609 if (!recv_workqueue) {
1610 log_print("can't start dlm_recv");
1611 return -ENOMEM;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001612 }
1613
David Teiglande43f0552011-03-10 13:22:34 -06001614 send_workqueue = alloc_workqueue("dlm_send",
1615 WQ_UNBOUND | WQ_MEM_RECLAIM, 1);
Namhyung Kimb9d41052010-12-13 13:42:24 -06001616 if (!send_workqueue) {
1617 log_print("can't start dlm_send");
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001618 destroy_workqueue(recv_workqueue);
Namhyung Kimb9d41052010-12-13 13:42:24 -06001619 return -ENOMEM;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001620 }
1621
1622 return 0;
1623}
1624
Christine Caulfield5e9ccc32009-01-28 12:57:40 -06001625static void stop_conn(struct connection *con)
1626{
1627 con->flags |= 0x0F;
Christine Caulfield391fbdc2009-05-07 10:54:16 -05001628 if (con->sock && con->sock->sk)
Christine Caulfield5e9ccc32009-01-28 12:57:40 -06001629 con->sock->sk->sk_user_data = NULL;
1630}
1631
1632static void free_conn(struct connection *con)
1633{
1634 close_connection(con, true);
1635 if (con->othercon)
1636 kmem_cache_free(con_cache, con->othercon);
1637 hlist_del(&con->list);
1638 kmem_cache_free(con_cache, con);
1639}
1640
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001641void dlm_lowcomms_stop(void)
1642{
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001643 /* Set all the flags to prevent any
1644 socket activity.
1645 */
Matthias Kaehlcke7a936ce2008-05-12 10:04:51 -05001646 mutex_lock(&connections_lock);
David Teigland513ef592012-03-30 11:46:08 -05001647 dlm_allow_conn = 0;
Christine Caulfield5e9ccc32009-01-28 12:57:40 -06001648 foreach_conn(stop_conn);
Matthias Kaehlcke7a936ce2008-05-12 10:04:51 -05001649 mutex_unlock(&connections_lock);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001650
1651 work_stop();
1652
Matthias Kaehlcke7a936ce2008-05-12 10:04:51 -05001653 mutex_lock(&connections_lock);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001654 clean_writequeues();
1655
Christine Caulfield5e9ccc32009-01-28 12:57:40 -06001656 foreach_conn(free_conn);
1657
Matthias Kaehlcke7a936ce2008-05-12 10:04:51 -05001658 mutex_unlock(&connections_lock);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001659 kmem_cache_destroy(con_cache);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001660}
1661
1662int dlm_lowcomms_start(void)
1663{
1664 int error = -EINVAL;
1665 struct connection *con;
Christine Caulfield5e9ccc32009-01-28 12:57:40 -06001666 int i;
1667
1668 for (i = 0; i < CONN_HASH_SIZE; i++)
1669 INIT_HLIST_HEAD(&connection_hash[i]);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001670
1671 init_local();
1672 if (!dlm_local_count) {
David Teigland617e82e2007-04-26 13:46:49 -05001673 error = -ENOTCONN;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001674 log_print("no local IP address has been set");
David Teigland513ef592012-03-30 11:46:08 -05001675 goto fail;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001676 }
1677
1678 error = -ENOMEM;
1679 con_cache = kmem_cache_create("dlm_conn", sizeof(struct connection),
1680 __alignof__(struct connection), 0,
Paul Mundt20c2df82007-07-20 10:11:58 +09001681 NULL);
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001682 if (!con_cache)
David Teigland513ef592012-03-30 11:46:08 -05001683 goto fail;
1684
1685 error = work_start();
1686 if (error)
1687 goto fail_destroy;
1688
1689 dlm_allow_conn = 1;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001690
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001691 /* Start listening */
1692 if (dlm_config.ci_protocol == 0)
1693 error = tcp_listen_for_all();
1694 else
1695 error = sctp_listen_for_all();
1696 if (error)
1697 goto fail_unlisten;
1698
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001699 return 0;
1700
1701fail_unlisten:
David Teigland513ef592012-03-30 11:46:08 -05001702 dlm_allow_conn = 0;
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001703 con = nodeid2con(0,0);
1704 if (con) {
1705 close_connection(con, false);
1706 kmem_cache_free(con_cache, con);
1707 }
David Teigland513ef592012-03-30 11:46:08 -05001708fail_destroy:
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001709 kmem_cache_destroy(con_cache);
David Teigland513ef592012-03-30 11:46:08 -05001710fail:
Patrick Caulfield6ed7257b2007-04-17 15:39:57 +01001711 return error;
1712}
David Teigland36b71a82012-07-26 12:44:30 -05001713
1714void dlm_lowcomms_exit(void)
1715{
1716 struct dlm_node_addr *na, *safe;
1717
1718 spin_lock(&dlm_node_addrs_spin);
1719 list_for_each_entry_safe(na, safe, &dlm_node_addrs, list) {
1720 list_del(&na->list);
1721 while (na->addr_count--)
1722 kfree(na->addr[na->addr_count]);
1723 kfree(na);
1724 }
1725 spin_unlock(&dlm_node_addrs_spin);
1726}