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