blob: 618a60f03886bf75118cefbe1fa113c1733e6baf [file] [log] [blame]
Patrick Caulfield6ed72572007-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 Caulfield6ed72572007-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 Caulfield6ed72572007-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 Caulfield6ed72572007-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 Caulfield6ed72572007-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 Caulfield6ed72572007-04-17 15:39:57 +010051#include <linux/file.h>
Matthias Kaehlcke7a936ce2008-05-12 10:04:51 -050052#include <linux/mutex.h>
Patrick Caulfield6ed72572007-04-17 15:39:57 +010053#include <linux/sctp.h>
54#include <net/sctp/user.h>
Joe Perches44ad5322009-01-22 13:24:49 -080055#include <net/ipv6.h>
Patrick Caulfield6ed72572007-04-17 15:39:57 +010056
57#include "dlm_internal.h"
58#include "lowcomms.h"
59#include "midcomms.h"
60#include "config.h"
61
62#define NEEDED_RMEM (4*1024*1024)
Christine Caulfield5e9ccc32009-01-28 12:57:40 -060063#define CONN_HASH_SIZE 32
Patrick Caulfield6ed72572007-04-17 15:39:57 +010064
65struct cbuf {
66 unsigned int base;
67 unsigned int len;
68 unsigned int mask;
69};
70
71static void cbuf_add(struct cbuf *cb, int n)
72{
73 cb->len += n;
74}
75
76static int cbuf_data(struct cbuf *cb)
77{
78 return ((cb->base + cb->len) & cb->mask);
79}
80
81static void cbuf_init(struct cbuf *cb, int size)
82{
83 cb->base = cb->len = 0;
84 cb->mask = size-1;
85}
86
87static void cbuf_eat(struct cbuf *cb, int n)
88{
89 cb->len -= n;
90 cb->base += n;
91 cb->base &= cb->mask;
92}
93
94static bool cbuf_empty(struct cbuf *cb)
95{
96 return cb->len == 0;
97}
98
99struct connection {
100 struct socket *sock; /* NULL if not connected */
101 uint32_t nodeid; /* So we know who we are in the list */
102 struct mutex sock_mutex;
103 unsigned long flags;
104#define CF_READ_PENDING 1
105#define CF_WRITE_PENDING 2
106#define CF_CONNECT_PENDING 3
107#define CF_INIT_PENDING 4
108#define CF_IS_OTHERCON 5
109 struct list_head writequeue; /* List of outgoing writequeue_entries */
110 spinlock_t writequeue_lock;
111 int (*rx_action) (struct connection *); /* What to do when active */
112 void (*connect_action) (struct connection *); /* What to do to connect */
113 struct page *rx_page;
114 struct cbuf cb;
115 int retries;
116#define MAX_CONNECT_RETRIES 3
117 int sctp_assoc;
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600118 struct hlist_node list;
Patrick Caulfield6ed72572007-04-17 15:39:57 +0100119 struct connection *othercon;
120 struct work_struct rwork; /* Receive workqueue */
121 struct work_struct swork; /* Send workqueue */
122};
123#define sock2con(x) ((struct connection *)(x)->sk_user_data)
124
125/* An entry waiting to be sent */
126struct writequeue_entry {
127 struct list_head list;
128 struct page *page;
129 int offset;
130 int len;
131 int end;
132 int users;
133 struct connection *con;
134};
135
136static struct sockaddr_storage *dlm_local_addr[DLM_MAX_ADDR_COUNT];
137static int dlm_local_count;
138
139/* Work queues */
140static struct workqueue_struct *recv_workqueue;
141static struct workqueue_struct *send_workqueue;
142
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600143static struct hlist_head connection_hash[CONN_HASH_SIZE];
Matthias Kaehlcke7a936ce2008-05-12 10:04:51 -0500144static DEFINE_MUTEX(connections_lock);
Patrick Caulfield6ed72572007-04-17 15:39:57 +0100145static struct kmem_cache *con_cache;
146
147static void process_recv_sockets(struct work_struct *work);
148static void process_send_sockets(struct work_struct *work);
149
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600150
151/* This is deliberately very simple because most clusters have simple
152 sequential nodeids, so we should be able to go straight to a connection
153 struct in the array */
154static inline int nodeid_hash(int nodeid)
155{
156 return nodeid & (CONN_HASH_SIZE-1);
157}
158
159static struct connection *__find_con(int nodeid)
160{
161 int r;
162 struct hlist_node *h;
163 struct connection *con;
164
165 r = nodeid_hash(nodeid);
166
167 hlist_for_each_entry(con, h, &connection_hash[r], list) {
168 if (con->nodeid == nodeid)
169 return con;
170 }
171 return NULL;
172}
173
Patrick Caulfield6ed72572007-04-17 15:39:57 +0100174/*
175 * If 'allocation' is zero then we don't attempt to create a new
176 * connection structure for this node.
177 */
178static struct connection *__nodeid2con(int nodeid, gfp_t alloc)
179{
180 struct connection *con = NULL;
181 int r;
Patrick Caulfield6ed72572007-04-17 15:39:57 +0100182
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600183 con = __find_con(nodeid);
Patrick Caulfield6ed72572007-04-17 15:39:57 +0100184 if (con || !alloc)
185 return con;
186
Patrick Caulfield6ed72572007-04-17 15:39:57 +0100187 con = kmem_cache_zalloc(con_cache, alloc);
188 if (!con)
189 return NULL;
190
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600191 r = nodeid_hash(nodeid);
192 hlist_add_head(&con->list, &connection_hash[r]);
Patrick Caulfield6ed72572007-04-17 15:39:57 +0100193
194 con->nodeid = nodeid;
195 mutex_init(&con->sock_mutex);
196 INIT_LIST_HEAD(&con->writequeue);
197 spin_lock_init(&con->writequeue_lock);
198 INIT_WORK(&con->swork, process_send_sockets);
199 INIT_WORK(&con->rwork, process_recv_sockets);
200
201 /* Setup action pointers for child sockets */
202 if (con->nodeid) {
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600203 struct connection *zerocon = __find_con(0);
Patrick Caulfield6ed72572007-04-17 15:39:57 +0100204
205 con->connect_action = zerocon->connect_action;
206 if (!con->rx_action)
207 con->rx_action = zerocon->rx_action;
208 }
209
Patrick Caulfield6ed72572007-04-17 15:39:57 +0100210 return con;
211}
212
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600213/* Loop round all connections */
214static void foreach_conn(void (*conn_func)(struct connection *c))
215{
216 int i;
217 struct hlist_node *h, *n;
218 struct connection *con;
219
220 for (i = 0; i < CONN_HASH_SIZE; i++) {
221 hlist_for_each_entry_safe(con, h, n, &connection_hash[i], list){
222 conn_func(con);
223 }
224 }
225}
226
Patrick Caulfield6ed72572007-04-17 15:39:57 +0100227static struct connection *nodeid2con(int nodeid, gfp_t allocation)
228{
229 struct connection *con;
230
Matthias Kaehlcke7a936ce2008-05-12 10:04:51 -0500231 mutex_lock(&connections_lock);
Patrick Caulfield6ed72572007-04-17 15:39:57 +0100232 con = __nodeid2con(nodeid, allocation);
Matthias Kaehlcke7a936ce2008-05-12 10:04:51 -0500233 mutex_unlock(&connections_lock);
Patrick Caulfield6ed72572007-04-17 15:39:57 +0100234
235 return con;
236}
237
238/* This is a bit drastic, but only called when things go wrong */
239static struct connection *assoc2con(int assoc_id)
240{
241 int i;
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600242 struct hlist_node *h;
Patrick Caulfield6ed72572007-04-17 15:39:57 +0100243 struct connection *con;
244
Matthias Kaehlcke7a936ce2008-05-12 10:04:51 -0500245 mutex_lock(&connections_lock);
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600246
247 for (i = 0 ; i < CONN_HASH_SIZE; i++) {
248 hlist_for_each_entry(con, h, &connection_hash[i], list) {
249 if (con && con->sctp_assoc == assoc_id) {
250 mutex_unlock(&connections_lock);
251 return con;
252 }
Patrick Caulfield6ed72572007-04-17 15:39:57 +0100253 }
254 }
Matthias Kaehlcke7a936ce2008-05-12 10:04:51 -0500255 mutex_unlock(&connections_lock);
Patrick Caulfield6ed72572007-04-17 15:39:57 +0100256 return NULL;
257}
258
259static int nodeid_to_addr(int nodeid, struct sockaddr *retaddr)
260{
261 struct sockaddr_storage addr;
262 int error;
263
264 if (!dlm_local_count)
265 return -1;
266
267 error = dlm_nodeid_to_addr(nodeid, &addr);
268 if (error)
269 return error;
270
271 if (dlm_local_addr[0]->ss_family == AF_INET) {
272 struct sockaddr_in *in4 = (struct sockaddr_in *) &addr;
273 struct sockaddr_in *ret4 = (struct sockaddr_in *) retaddr;
274 ret4->sin_addr.s_addr = in4->sin_addr.s_addr;
275 } else {
276 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) &addr;
277 struct sockaddr_in6 *ret6 = (struct sockaddr_in6 *) retaddr;
Joe Perches44ad5322009-01-22 13:24:49 -0800278 ipv6_addr_copy(&ret6->sin6_addr, &in6->sin6_addr);
Patrick Caulfield6ed72572007-04-17 15:39:57 +0100279 }
280
281 return 0;
282}
283
284/* Data available on socket or listen socket received a connect */
285static void lowcomms_data_ready(struct sock *sk, int count_unused)
286{
287 struct connection *con = sock2con(sk);
Patrick Caulfieldafb853f2007-06-01 10:07:26 -0500288 if (con && !test_and_set_bit(CF_READ_PENDING, &con->flags))
Patrick Caulfield6ed72572007-04-17 15:39:57 +0100289 queue_work(recv_workqueue, &con->rwork);
290}
291
292static void lowcomms_write_space(struct sock *sk)
293{
294 struct connection *con = sock2con(sk);
295
Patrick Caulfieldafb853f2007-06-01 10:07:26 -0500296 if (con && !test_and_set_bit(CF_WRITE_PENDING, &con->flags))
Patrick Caulfield6ed72572007-04-17 15:39:57 +0100297 queue_work(send_workqueue, &con->swork);
298}
299
300static inline void lowcomms_connect_sock(struct connection *con)
301{
302 if (!test_and_set_bit(CF_CONNECT_PENDING, &con->flags))
303 queue_work(send_workqueue, &con->swork);
304}
305
306static void lowcomms_state_change(struct sock *sk)
307{
308 if (sk->sk_state == TCP_ESTABLISHED)
309 lowcomms_write_space(sk);
310}
311
Christine Caulfield391fbdc2009-05-07 10:54:16 -0500312int dlm_lowcomms_connect_node(int nodeid)
313{
314 struct connection *con;
315
316 if (nodeid == dlm_our_nodeid())
317 return 0;
318
319 con = nodeid2con(nodeid, GFP_NOFS);
320 if (!con)
321 return -ENOMEM;
322 lowcomms_connect_sock(con);
323 return 0;
324}
325
Patrick Caulfield6ed72572007-04-17 15:39:57 +0100326/* Make a socket active */
327static int add_sock(struct socket *sock, struct connection *con)
328{
329 con->sock = sock;
330
331 /* Install a data_ready callback */
332 con->sock->sk->sk_data_ready = lowcomms_data_ready;
333 con->sock->sk->sk_write_space = lowcomms_write_space;
334 con->sock->sk->sk_state_change = lowcomms_state_change;
335 con->sock->sk->sk_user_data = con;
Steven Whitehoused6d7b702008-11-12 16:49:48 -0600336 con->sock->sk->sk_allocation = GFP_NOFS;
Patrick Caulfield6ed72572007-04-17 15:39:57 +0100337 return 0;
338}
339
340/* Add the port number to an IPv6 or 4 sockaddr and return the address
341 length */
342static void make_sockaddr(struct sockaddr_storage *saddr, uint16_t port,
343 int *addr_len)
344{
345 saddr->ss_family = dlm_local_addr[0]->ss_family;
346 if (saddr->ss_family == AF_INET) {
347 struct sockaddr_in *in4_addr = (struct sockaddr_in *)saddr;
348 in4_addr->sin_port = cpu_to_be16(port);
349 *addr_len = sizeof(struct sockaddr_in);
350 memset(&in4_addr->sin_zero, 0, sizeof(in4_addr->sin_zero));
351 } else {
352 struct sockaddr_in6 *in6_addr = (struct sockaddr_in6 *)saddr;
353 in6_addr->sin6_port = cpu_to_be16(port);
354 *addr_len = sizeof(struct sockaddr_in6);
355 }
Patrick Caulfield01c8cab2007-07-17 16:53:15 +0100356 memset((char *)saddr + *addr_len, 0, sizeof(struct sockaddr_storage) - *addr_len);
Patrick Caulfield6ed72572007-04-17 15:39:57 +0100357}
358
359/* Close a remote connection and tidy up */
360static void close_connection(struct connection *con, bool and_other)
361{
362 mutex_lock(&con->sock_mutex);
363
364 if (con->sock) {
365 sock_release(con->sock);
366 con->sock = NULL;
367 }
368 if (con->othercon && and_other) {
369 /* Will only re-enter once. */
370 close_connection(con->othercon, false);
371 }
372 if (con->rx_page) {
373 __free_page(con->rx_page);
374 con->rx_page = NULL;
375 }
Patrick Caulfield9e5f2822007-08-02 14:58:14 +0100376
Patrick Caulfield61d96be2007-08-20 15:13:38 +0100377 con->retries = 0;
378 mutex_unlock(&con->sock_mutex);
Patrick Caulfield6ed72572007-04-17 15:39:57 +0100379}
380
381/* We only send shutdown messages to nodes that are not part of the cluster */
382static void sctp_send_shutdown(sctp_assoc_t associd)
383{
384 static char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
385 struct msghdr outmessage;
386 struct cmsghdr *cmsg;
387 struct sctp_sndrcvinfo *sinfo;
388 int ret;
389 struct connection *con;
390
391 con = nodeid2con(0,0);
392 BUG_ON(con == NULL);
393
394 outmessage.msg_name = NULL;
395 outmessage.msg_namelen = 0;
396 outmessage.msg_control = outcmsg;
397 outmessage.msg_controllen = sizeof(outcmsg);
398 outmessage.msg_flags = MSG_EOR;
399
400 cmsg = CMSG_FIRSTHDR(&outmessage);
401 cmsg->cmsg_level = IPPROTO_SCTP;
402 cmsg->cmsg_type = SCTP_SNDRCV;
403 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
404 outmessage.msg_controllen = cmsg->cmsg_len;
405 sinfo = CMSG_DATA(cmsg);
406 memset(sinfo, 0x00, sizeof(struct sctp_sndrcvinfo));
407
408 sinfo->sinfo_flags |= MSG_EOF;
409 sinfo->sinfo_assoc_id = associd;
410
411 ret = kernel_sendmsg(con->sock, &outmessage, NULL, 0, 0);
412
413 if (ret != 0)
414 log_print("send EOF to node failed: %d", ret);
415}
416
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600417static void sctp_init_failed_foreach(struct connection *con)
418{
419 con->sctp_assoc = 0;
420 if (test_and_clear_bit(CF_CONNECT_PENDING, &con->flags)) {
421 if (!test_and_set_bit(CF_WRITE_PENDING, &con->flags))
422 queue_work(send_workqueue, &con->swork);
423 }
424}
425
Patrick Caulfield6ed72572007-04-17 15:39:57 +0100426/* INIT failed but we don't know which node...
427 restart INIT on all pending nodes */
428static void sctp_init_failed(void)
429{
Matthias Kaehlcke7a936ce2008-05-12 10:04:51 -0500430 mutex_lock(&connections_lock);
Christine Caulfield5e9ccc32009-01-28 12:57:40 -0600431
432 foreach_conn(sctp_init_failed_foreach);
433
Matthias Kaehlcke7a936ce2008-05-12 10:04:51 -0500434 mutex_unlock(&connections_lock);
Patrick Caulfield6ed72572007-04-17 15:39:57 +0100435}
436
437/* Something happened to an association */
David Teigland617e82e2007-04-26 13:46:49 -0500438static void process_sctp_notification(struct connection *con,
439 struct msghdr *msg, char *buf)
Patrick Caulfield6ed72572007-04-17 15:39:57 +0100440{
441 union sctp_notification *sn = (union sctp_notification *)buf;
442
443 if (sn->sn_header.sn_type == SCTP_ASSOC_CHANGE) {
444 switch (sn->sn_assoc_change.sac_state) {
445
446 case SCTP_COMM_UP:
447 case SCTP_RESTART:
448 {
449 /* Check that the new node is in the lockspace */
450 struct sctp_prim prim;
451 int nodeid;
452 int prim_len, ret;
453 int addr_len;
454 struct connection *new_con;
455 struct file *file;
456 sctp_peeloff_arg_t parg;
457 int parglen = sizeof(parg);
458
459 /*
460 * We get this before any data for an association.
461 * We verify that the node is in the cluster and
462 * then peel off a socket for it.
463 */
464 if ((int)sn->sn_assoc_change.sac_assoc_id <= 0) {
465 log_print("COMM_UP for invalid assoc ID %d",
David Teigland617e82e2007-04-26 13:46:49 -0500466 (int)sn->sn_assoc_change.sac_assoc_id);
Patrick Caulfield6ed72572007-04-17 15:39:57 +0100467 sctp_init_failed();
468 return;
469 }
470 memset(&prim, 0, sizeof(struct sctp_prim));
471 prim_len = sizeof(struct sctp_prim);
472 prim.ssp_assoc_id = sn->sn_assoc_change.sac_assoc_id;
473
474 ret = kernel_getsockopt(con->sock,
475 IPPROTO_SCTP,
476 SCTP_PRIMARY_ADDR,
477 (char*)&prim,
478 &prim_len);
479 if (ret < 0) {
480 log_print("getsockopt/sctp_primary_addr on "
481 "new assoc %d failed : %d",
482 (int)sn->sn_assoc_change.sac_assoc_id,
483 ret);
484
485 /* Retry INIT later */
486 new_con = assoc2con(sn->sn_assoc_change.sac_assoc_id);
487 if (new_con)
488 clear_bit(CF_CONNECT_PENDING, &con->flags);
489 return;
490 }
491 make_sockaddr(&prim.ssp_addr, 0, &addr_len);
492 if (dlm_addr_to_nodeid(&prim.ssp_addr, &nodeid)) {
493 int i;
494 unsigned char *b=(unsigned char *)&prim.ssp_addr;
495 log_print("reject connect from unknown addr");
496 for (i=0; i<sizeof(struct sockaddr_storage);i++)
497 printk("%02x ", b[i]);
498 printk("\n");
499 sctp_send_shutdown(prim.ssp_assoc_id);
500 return;
501 }
502
David Teigland748285c2009-05-15 10:50:57 -0500503 new_con = nodeid2con(nodeid, GFP_NOFS);
Patrick Caulfield6ed72572007-04-17 15:39:57 +0100504 if (!new_con)
505 return;
506
507 /* Peel off a new sock */
508 parg.associd = sn->sn_assoc_change.sac_assoc_id;
David Teigland617e82e2007-04-26 13:46:49 -0500509 ret = kernel_getsockopt(con->sock, IPPROTO_SCTP,
510 SCTP_SOCKOPT_PEELOFF,
Patrick Caulfield6ed72572007-04-17 15:39:57 +0100511 (void *)&parg, &parglen);
Patrick Caulfield30d3a232007-04-23 16:26:21 +0100512 if (ret) {
David Teigland617e82e2007-04-26 13:46:49 -0500513 log_print("Can't peel off a socket for "
514 "connection %d to node %d: err=%d\n",
Patrick Caulfield6ed72572007-04-17 15:39:57 +0100515 parg.associd, nodeid, ret);
Patrick Caulfield6ed72572007-04-17 15:39:57 +0100516 }
Patrick Caulfield6ed72572007-04-17 15:39:57 +0100517 file = fget(parg.sd);
518 new_con->sock = SOCKET_I(file->f_dentry->d_inode);
519 add_sock(new_con->sock, new_con);
520 fput(file);
521 put_unused_fd(parg.sd);
522
523 log_print("got new/restarted association %d nodeid %d",
David Teigland617e82e2007-04-26 13:46:49 -0500524 (int)sn->sn_assoc_change.sac_assoc_id, nodeid);
Patrick Caulfield6ed72572007-04-17 15:39:57 +0100525
526 /* Send any pending writes */
527 clear_bit(CF_CONNECT_PENDING, &new_con->flags);
528 clear_bit(CF_INIT_PENDING, &con->flags);
529 if (!test_and_set_bit(CF_WRITE_PENDING, &new_con->flags)) {
530 queue_work(send_workqueue, &new_con->swork);
531 }
532 if (!test_and_set_bit(CF_READ_PENDING, &new_con->flags))
533 queue_work(recv_workqueue, &new_con->rwork);
534 }
535 break;
536
537 case SCTP_COMM_LOST:
538 case SCTP_SHUTDOWN_COMP:
539 {
540 con = assoc2con(sn->sn_assoc_change.sac_assoc_id);
541 if (con) {
542 con->sctp_assoc = 0;
543 }
544 }
545 break;
546
547 /* We don't know which INIT failed, so clear the PENDING flags
548 * on them all. if assoc_id is zero then it will then try
549 * again */
550
551 case SCTP_CANT_STR_ASSOC:
552 {
553 log_print("Can't start SCTP association - retrying");
554 sctp_init_failed();
555 }
556 break;
557
558 default:
559 log_print("unexpected SCTP assoc change id=%d state=%d",
560 (int)sn->sn_assoc_change.sac_assoc_id,
561 sn->sn_assoc_change.sac_state);
562 }
563 }
564}
565
566/* Data received from remote end */
567static int receive_from_sock(struct connection *con)
568{
569 int ret = 0;
570 struct msghdr msg = {};
571 struct kvec iov[2];
572 unsigned len;
573 int r;
574 int call_again_soon = 0;
575 int nvec;
576 char incmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
577
578 mutex_lock(&con->sock_mutex);
579
580 if (con->sock == NULL) {
581 ret = -EAGAIN;
582 goto out_close;
583 }
584
585 if (con->rx_page == NULL) {
586 /*
587 * This doesn't need to be atomic, but I think it should
588 * improve performance if it is.
589 */
590 con->rx_page = alloc_page(GFP_ATOMIC);
591 if (con->rx_page == NULL)
592 goto out_resched;
593 cbuf_init(&con->cb, PAGE_CACHE_SIZE);
594 }
595
596 /* Only SCTP needs these really */
597 memset(&incmsg, 0, sizeof(incmsg));
598 msg.msg_control = incmsg;
599 msg.msg_controllen = sizeof(incmsg);
600
601 /*
602 * iov[0] is the bit of the circular buffer between the current end
603 * point (cb.base + cb.len) and the end of the buffer.
604 */
605 iov[0].iov_len = con->cb.base - cbuf_data(&con->cb);
606 iov[0].iov_base = page_address(con->rx_page) + cbuf_data(&con->cb);
607 iov[1].iov_len = 0;
608 nvec = 1;
609
610 /*
611 * iov[1] is the bit of the circular buffer between the start of the
612 * buffer and the start of the currently used section (cb.base)
613 */
614 if (cbuf_data(&con->cb) >= con->cb.base) {
615 iov[0].iov_len = PAGE_CACHE_SIZE - cbuf_data(&con->cb);
616 iov[1].iov_len = con->cb.base;
617 iov[1].iov_base = page_address(con->rx_page);
618 nvec = 2;
619 }
620 len = iov[0].iov_len + iov[1].iov_len;
621
622 r = ret = kernel_recvmsg(con->sock, &msg, iov, nvec, len,
623 MSG_DONTWAIT | MSG_NOSIGNAL);
624 if (ret <= 0)
625 goto out_close;
626
627 /* Process SCTP notifications */
628 if (msg.msg_flags & MSG_NOTIFICATION) {
Patrick Caulfield6ed72572007-04-17 15:39:57 +0100629 msg.msg_control = incmsg;
630 msg.msg_controllen = sizeof(incmsg);
631
632 process_sctp_notification(con, &msg,
David Teigland617e82e2007-04-26 13:46:49 -0500633 page_address(con->rx_page) + con->cb.base);
Patrick Caulfield6ed72572007-04-17 15:39:57 +0100634 mutex_unlock(&con->sock_mutex);
635 return 0;
636 }
637 BUG_ON(con->nodeid == 0);
638
639 if (ret == len)
640 call_again_soon = 1;
641 cbuf_add(&con->cb, ret);
642 ret = dlm_process_incoming_buffer(con->nodeid,
643 page_address(con->rx_page),
644 con->cb.base, con->cb.len,
645 PAGE_CACHE_SIZE);
646 if (ret == -EBADMSG) {
David Teigland617e82e2007-04-26 13:46:49 -0500647 log_print("lowcomms: addr=%p, base=%u, len=%u, "
648 "iov_len=%u, iov_base[0]=%p, read=%d",
649 page_address(con->rx_page), con->cb.base, con->cb.len,
650 len, iov[0].iov_base, r);
Patrick Caulfield6ed72572007-04-17 15:39:57 +0100651 }
652 if (ret < 0)
653 goto out_close;
654 cbuf_eat(&con->cb, ret);
655
656 if (cbuf_empty(&con->cb) && !call_again_soon) {
657 __free_page(con->rx_page);
658 con->rx_page = NULL;
659 }
660
661 if (call_again_soon)
662 goto out_resched;
663 mutex_unlock(&con->sock_mutex);
664 return 0;
665
666out_resched:
667 if (!test_and_set_bit(CF_READ_PENDING, &con->flags))
668 queue_work(recv_workqueue, &con->rwork);
669 mutex_unlock(&con->sock_mutex);
670 return -EAGAIN;
671
672out_close:
673 mutex_unlock(&con->sock_mutex);
Patrick Caulfield9e5f2822007-08-02 14:58:14 +0100674 if (ret != -EAGAIN) {
Patrick Caulfield6ed72572007-04-17 15:39:57 +0100675 close_connection(con, false);
676 /* Reconnect when there is something to send */
677 }
678 /* Don't return success if we really got EOF */
679 if (ret == 0)
680 ret = -EAGAIN;
681
682 return ret;
683}
684
685/* Listening socket is busy, accept a connection */
686static int tcp_accept_from_sock(struct connection *con)
687{
688 int result;
689 struct sockaddr_storage peeraddr;
690 struct socket *newsock;
691 int len;
692 int nodeid;
693 struct connection *newcon;
694 struct connection *addcon;
695
696 memset(&peeraddr, 0, sizeof(peeraddr));
697 result = sock_create_kern(dlm_local_addr[0]->ss_family, SOCK_STREAM,
698 IPPROTO_TCP, &newsock);
699 if (result < 0)
700 return -ENOMEM;
701
702 mutex_lock_nested(&con->sock_mutex, 0);
703
704 result = -ENOTCONN;
705 if (con->sock == NULL)
706 goto accept_err;
707
708 newsock->type = con->sock->type;
709 newsock->ops = con->sock->ops;
710
711 result = con->sock->ops->accept(con->sock, newsock, O_NONBLOCK);
712 if (result < 0)
713 goto accept_err;
714
715 /* Get the connected socket's peer */
716 memset(&peeraddr, 0, sizeof(peeraddr));
717 if (newsock->ops->getname(newsock, (struct sockaddr *)&peeraddr,
718 &len, 2)) {
719 result = -ECONNABORTED;
720 goto accept_err;
721 }
722
723 /* Get the new node's NODEID */
724 make_sockaddr(&peeraddr, 0, &len);
725 if (dlm_addr_to_nodeid(&peeraddr, &nodeid)) {
David Teigland617e82e2007-04-26 13:46:49 -0500726 log_print("connect from non cluster node");
Patrick Caulfield6ed72572007-04-17 15:39:57 +0100727 sock_release(newsock);
728 mutex_unlock(&con->sock_mutex);
729 return -1;
730 }
731
732 log_print("got connection from %d", nodeid);
733
734 /* Check to see if we already have a connection to this node. This
735 * could happen if the two nodes initiate a connection at roughly
736 * the same time and the connections cross on the wire.
737 * In this case we store the incoming one in "othercon"
738 */
David Teigland748285c2009-05-15 10:50:57 -0500739 newcon = nodeid2con(nodeid, GFP_NOFS);
Patrick Caulfield6ed72572007-04-17 15:39:57 +0100740 if (!newcon) {
741 result = -ENOMEM;
742 goto accept_err;
743 }
744 mutex_lock_nested(&newcon->sock_mutex, 1);
745 if (newcon->sock) {
746 struct connection *othercon = newcon->othercon;
747
748 if (!othercon) {
David Teigland748285c2009-05-15 10:50:57 -0500749 othercon = kmem_cache_zalloc(con_cache, GFP_NOFS);
Patrick Caulfield6ed72572007-04-17 15:39:57 +0100750 if (!othercon) {
David Teigland617e82e2007-04-26 13:46:49 -0500751 log_print("failed to allocate incoming socket");
Patrick Caulfield6ed72572007-04-17 15:39:57 +0100752 mutex_unlock(&newcon->sock_mutex);
753 result = -ENOMEM;
754 goto accept_err;
755 }
756 othercon->nodeid = nodeid;
757 othercon->rx_action = receive_from_sock;
758 mutex_init(&othercon->sock_mutex);
759 INIT_WORK(&othercon->swork, process_send_sockets);
760 INIT_WORK(&othercon->rwork, process_recv_sockets);
761 set_bit(CF_IS_OTHERCON, &othercon->flags);
Patrick Caulfield61d96be2007-08-20 15:13:38 +0100762 }
763 if (!othercon->sock) {
Patrick Caulfield6ed72572007-04-17 15:39:57 +0100764 newcon->othercon = othercon;
Patrick Caulfield97d84832007-06-27 11:36:23 +0100765 othercon->sock = newsock;
766 newsock->sk->sk_user_data = othercon;
767 add_sock(newsock, othercon);
768 addcon = othercon;
Patrick Caulfield6ed72572007-04-17 15:39:57 +0100769 }
Patrick Caulfield97d84832007-06-27 11:36:23 +0100770 else {
771 printk("Extra connection from node %d attempted\n", nodeid);
772 result = -EAGAIN;
akpm@linux-foundation.orgf4fadb23c2007-06-27 14:43:37 -0700773 mutex_unlock(&newcon->sock_mutex);
Patrick Caulfield97d84832007-06-27 11:36:23 +0100774 goto accept_err;
775 }
Patrick Caulfield6ed72572007-04-17 15:39:57 +0100776 }
777 else {
778 newsock->sk->sk_user_data = newcon;
779 newcon->rx_action = receive_from_sock;
780 add_sock(newsock, newcon);
781 addcon = newcon;
782 }
783
784 mutex_unlock(&newcon->sock_mutex);
785
786 /*
787 * Add it to the active queue in case we got data
788 * beween processing the accept adding the socket
789 * to the read_sockets list
790 */
791 if (!test_and_set_bit(CF_READ_PENDING, &addcon->flags))
792 queue_work(recv_workqueue, &addcon->rwork);
793 mutex_unlock(&con->sock_mutex);
794
795 return 0;
796
797accept_err:
798 mutex_unlock(&con->sock_mutex);
799 sock_release(newsock);
800
801 if (result != -EAGAIN)
David Teigland617e82e2007-04-26 13:46:49 -0500802 log_print("error accepting connection from node: %d", result);
Patrick Caulfield6ed72572007-04-17 15:39:57 +0100803 return result;
804}
805
806static void free_entry(struct writequeue_entry *e)
807{
808 __free_page(e->page);
809 kfree(e);
810}
811
812/* Initiate an SCTP association.
813 This is a special case of send_to_sock() in that we don't yet have a
814 peeled-off socket for this association, so we use the listening socket
815 and add the primary IP address of the remote node.
816 */
817static void sctp_init_assoc(struct connection *con)
818{
819 struct sockaddr_storage rem_addr;
820 char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
821 struct msghdr outmessage;
822 struct cmsghdr *cmsg;
823 struct sctp_sndrcvinfo *sinfo;
824 struct connection *base_con;
825 struct writequeue_entry *e;
826 int len, offset;
827 int ret;
828 int addrlen;
829 struct kvec iov[1];
830
831 if (test_and_set_bit(CF_INIT_PENDING, &con->flags))
832 return;
833
834 if (con->retries++ > MAX_CONNECT_RETRIES)
835 return;
836
837 log_print("Initiating association with node %d", con->nodeid);
838
839 if (nodeid_to_addr(con->nodeid, (struct sockaddr *)&rem_addr)) {
840 log_print("no address for nodeid %d", con->nodeid);
841 return;
842 }
843 base_con = nodeid2con(0, 0);
844 BUG_ON(base_con == NULL);
845
846 make_sockaddr(&rem_addr, dlm_config.ci_tcp_port, &addrlen);
847
848 outmessage.msg_name = &rem_addr;
849 outmessage.msg_namelen = addrlen;
850 outmessage.msg_control = outcmsg;
851 outmessage.msg_controllen = sizeof(outcmsg);
852 outmessage.msg_flags = MSG_EOR;
853
854 spin_lock(&con->writequeue_lock);
855 e = list_entry(con->writequeue.next, struct writequeue_entry,
856 list);
857
858 BUG_ON((struct list_head *) e == &con->writequeue);
859
860 len = e->len;
861 offset = e->offset;
862 spin_unlock(&con->writequeue_lock);
Patrick Caulfield6ed72572007-04-17 15:39:57 +0100863
864 /* Send the first block off the write queue */
865 iov[0].iov_base = page_address(e->page)+offset;
866 iov[0].iov_len = len;
867
868 cmsg = CMSG_FIRSTHDR(&outmessage);
869 cmsg->cmsg_level = IPPROTO_SCTP;
870 cmsg->cmsg_type = SCTP_SNDRCV;
871 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
872 sinfo = CMSG_DATA(cmsg);
873 memset(sinfo, 0x00, sizeof(struct sctp_sndrcvinfo));
874 sinfo->sinfo_ppid = cpu_to_le32(dlm_our_nodeid());
875 outmessage.msg_controllen = cmsg->cmsg_len;
876
877 ret = kernel_sendmsg(base_con->sock, &outmessage, iov, 1, len);
878 if (ret < 0) {
David Teigland617e82e2007-04-26 13:46:49 -0500879 log_print("Send first packet to node %d failed: %d",
880 con->nodeid, ret);
Patrick Caulfield6ed72572007-04-17 15:39:57 +0100881
882 /* Try again later */
883 clear_bit(CF_CONNECT_PENDING, &con->flags);
884 clear_bit(CF_INIT_PENDING, &con->flags);
885 }
886 else {
887 spin_lock(&con->writequeue_lock);
888 e->offset += ret;
889 e->len -= ret;
890
891 if (e->len == 0 && e->users == 0) {
892 list_del(&e->list);
Patrick Caulfield6ed72572007-04-17 15:39:57 +0100893 free_entry(e);
894 }
895 spin_unlock(&con->writequeue_lock);
896 }
897}
898
899/* Connect a new socket to its peer */
900static void tcp_connect_to_sock(struct connection *con)
901{
902 int result = -EHOSTUNREACH;
Lon Hohberger6bd8fed2007-10-25 18:51:54 -0400903 struct sockaddr_storage saddr, src_addr;
Patrick Caulfield6ed72572007-04-17 15:39:57 +0100904 int addr_len;
Casey Dahlina89d63a2009-07-14 12:17:51 -0500905 struct socket *sock = NULL;
Patrick Caulfield6ed72572007-04-17 15:39:57 +0100906
907 if (con->nodeid == 0) {
908 log_print("attempt to connect sock 0 foiled");
909 return;
910 }
911
912 mutex_lock(&con->sock_mutex);
913 if (con->retries++ > MAX_CONNECT_RETRIES)
914 goto out;
915
916 /* Some odd races can cause double-connects, ignore them */
917 if (con->sock) {
918 result = 0;
919 goto out;
920 }
921
922 /* Create a socket to communicate with */
923 result = sock_create_kern(dlm_local_addr[0]->ss_family, SOCK_STREAM,
924 IPPROTO_TCP, &sock);
925 if (result < 0)
926 goto out_err;
927
928 memset(&saddr, 0, sizeof(saddr));
Masatake YAMATO311f6fc2008-06-27 08:35:03 -0500929 if (dlm_nodeid_to_addr(con->nodeid, &saddr)) {
930 sock_release(sock);
Patrick Caulfield6ed72572007-04-17 15:39:57 +0100931 goto out_err;
Masatake YAMATO311f6fc2008-06-27 08:35:03 -0500932 }
Patrick Caulfield6ed72572007-04-17 15:39:57 +0100933
934 sock->sk->sk_user_data = con;
935 con->rx_action = receive_from_sock;
936 con->connect_action = tcp_connect_to_sock;
937 add_sock(sock, con);
938
Lon Hohberger6bd8fed2007-10-25 18:51:54 -0400939 /* Bind to our cluster-known address connecting to avoid
940 routing problems */
941 memcpy(&src_addr, dlm_local_addr[0], sizeof(src_addr));
942 make_sockaddr(&src_addr, 0, &addr_len);
943 result = sock->ops->bind(sock, (struct sockaddr *) &src_addr,
944 addr_len);
945 if (result < 0) {
946 log_print("could not bind for connect: %d", result);
947 /* This *may* not indicate a critical error */
948 }
949
Patrick Caulfield6ed72572007-04-17 15:39:57 +0100950 make_sockaddr(&saddr, dlm_config.ci_tcp_port, &addr_len);
951
952 log_print("connecting to %d", con->nodeid);
953 result =
954 sock->ops->connect(sock, (struct sockaddr *)&saddr, addr_len,
955 O_NONBLOCK);
956 if (result == -EINPROGRESS)
957 result = 0;
958 if (result == 0)
959 goto out;
960
961out_err:
962 if (con->sock) {
963 sock_release(con->sock);
964 con->sock = NULL;
Casey Dahlina89d63a2009-07-14 12:17:51 -0500965 } else if (sock) {
966 sock_release(sock);
Patrick Caulfield6ed72572007-04-17 15:39:57 +0100967 }
968 /*
969 * Some errors are fatal and this list might need adjusting. For other
970 * errors we try again until the max number of retries is reached.
971 */
972 if (result != -EHOSTUNREACH && result != -ENETUNREACH &&
Marcin Slusarz0035a4b2008-05-11 22:01:29 +0200973 result != -ENETDOWN && result != -EINVAL
Patrick Caulfield6ed72572007-04-17 15:39:57 +0100974 && result != -EPROTONOSUPPORT) {
975 lowcomms_connect_sock(con);
976 result = 0;
977 }
978out:
979 mutex_unlock(&con->sock_mutex);
980 return;
981}
982
983static struct socket *tcp_create_listen_sock(struct connection *con,
984 struct sockaddr_storage *saddr)
985{
986 struct socket *sock = NULL;
987 int result = 0;
988 int one = 1;
989 int addr_len;
990
991 if (dlm_local_addr[0]->ss_family == AF_INET)
992 addr_len = sizeof(struct sockaddr_in);
993 else
994 addr_len = sizeof(struct sockaddr_in6);
995
996 /* Create a socket to communicate with */
David Teigland617e82e2007-04-26 13:46:49 -0500997 result = sock_create_kern(dlm_local_addr[0]->ss_family, SOCK_STREAM,
998 IPPROTO_TCP, &sock);
Patrick Caulfield6ed72572007-04-17 15:39:57 +0100999 if (result < 0) {
David Teigland617e82e2007-04-26 13:46:49 -05001000 log_print("Can't create listening comms socket");
Patrick Caulfield6ed72572007-04-17 15:39:57 +01001001 goto create_out;
1002 }
1003
1004 result = kernel_setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
1005 (char *)&one, sizeof(one));
1006
1007 if (result < 0) {
David Teigland617e82e2007-04-26 13:46:49 -05001008 log_print("Failed to set SO_REUSEADDR on socket: %d", result);
Patrick Caulfield6ed72572007-04-17 15:39:57 +01001009 }
1010 sock->sk->sk_user_data = con;
1011 con->rx_action = tcp_accept_from_sock;
1012 con->connect_action = tcp_connect_to_sock;
1013 con->sock = sock;
1014
1015 /* Bind to our port */
1016 make_sockaddr(saddr, dlm_config.ci_tcp_port, &addr_len);
1017 result = sock->ops->bind(sock, (struct sockaddr *) saddr, addr_len);
1018 if (result < 0) {
David Teigland617e82e2007-04-26 13:46:49 -05001019 log_print("Can't bind to port %d", dlm_config.ci_tcp_port);
Patrick Caulfield6ed72572007-04-17 15:39:57 +01001020 sock_release(sock);
1021 sock = NULL;
1022 con->sock = NULL;
1023 goto create_out;
1024 }
1025 result = kernel_setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE,
1026 (char *)&one, sizeof(one));
1027 if (result < 0) {
David Teigland617e82e2007-04-26 13:46:49 -05001028 log_print("Set keepalive failed: %d", result);
Patrick Caulfield6ed72572007-04-17 15:39:57 +01001029 }
1030
1031 result = sock->ops->listen(sock, 5);
1032 if (result < 0) {
David Teigland617e82e2007-04-26 13:46:49 -05001033 log_print("Can't listen on port %d", dlm_config.ci_tcp_port);
Patrick Caulfield6ed72572007-04-17 15:39:57 +01001034 sock_release(sock);
1035 sock = NULL;
1036 goto create_out;
1037 }
1038
1039create_out:
1040 return sock;
1041}
1042
1043/* Get local addresses */
1044static void init_local(void)
1045{
1046 struct sockaddr_storage sas, *addr;
1047 int i;
1048
Patrick Caulfield30d3a232007-04-23 16:26:21 +01001049 dlm_local_count = 0;
Patrick Caulfield6ed72572007-04-17 15:39:57 +01001050 for (i = 0; i < DLM_MAX_ADDR_COUNT - 1; i++) {
1051 if (dlm_our_addr(&sas, i))
1052 break;
1053
1054 addr = kmalloc(sizeof(*addr), GFP_KERNEL);
1055 if (!addr)
1056 break;
1057 memcpy(addr, &sas, sizeof(*addr));
1058 dlm_local_addr[dlm_local_count++] = addr;
1059 }
1060}
1061
David Teigland617e82e2007-04-26 13:46:49 -05001062/* Bind to an IP address. SCTP allows multiple address so it can do
1063 multi-homing */
1064static int add_sctp_bind_addr(struct connection *sctp_con,
1065 struct sockaddr_storage *addr,
1066 int addr_len, int num)
Patrick Caulfield6ed72572007-04-17 15:39:57 +01001067{
1068 int result = 0;
1069
1070 if (num == 1)
1071 result = kernel_bind(sctp_con->sock,
1072 (struct sockaddr *) addr,
1073 addr_len);
1074 else
1075 result = kernel_setsockopt(sctp_con->sock, SOL_SCTP,
1076 SCTP_SOCKOPT_BINDX_ADD,
1077 (char *)addr, addr_len);
1078
1079 if (result < 0)
1080 log_print("Can't bind to port %d addr number %d",
1081 dlm_config.ci_tcp_port, num);
1082
1083 return result;
1084}
1085
1086/* Initialise SCTP socket and bind to all interfaces */
1087static int sctp_listen_for_all(void)
1088{
1089 struct socket *sock = NULL;
1090 struct sockaddr_storage localaddr;
1091 struct sctp_event_subscribe subscribe;
1092 int result = -EINVAL, num = 1, i, addr_len;
1093 struct connection *con = nodeid2con(0, GFP_KERNEL);
1094 int bufsize = NEEDED_RMEM;
1095
1096 if (!con)
1097 return -ENOMEM;
1098
1099 log_print("Using SCTP for communications");
1100
1101 result = sock_create_kern(dlm_local_addr[0]->ss_family, SOCK_SEQPACKET,
1102 IPPROTO_SCTP, &sock);
1103 if (result < 0) {
1104 log_print("Can't create comms socket, check SCTP is loaded");
1105 goto out;
1106 }
1107
1108 /* Listen for events */
1109 memset(&subscribe, 0, sizeof(subscribe));
1110 subscribe.sctp_data_io_event = 1;
1111 subscribe.sctp_association_event = 1;
1112 subscribe.sctp_send_failure_event = 1;
1113 subscribe.sctp_shutdown_event = 1;
1114 subscribe.sctp_partial_delivery_event = 1;
1115
David S. Millerdf61c952007-11-06 23:48:57 -08001116 result = kernel_setsockopt(sock, SOL_SOCKET, SO_RCVBUFFORCE,
Patrick Caulfield6ed72572007-04-17 15:39:57 +01001117 (char *)&bufsize, sizeof(bufsize));
1118 if (result)
David Teigland617e82e2007-04-26 13:46:49 -05001119 log_print("Error increasing buffer space on socket %d", result);
Patrick Caulfield6ed72572007-04-17 15:39:57 +01001120
1121 result = kernel_setsockopt(sock, SOL_SCTP, SCTP_EVENTS,
David Teigland617e82e2007-04-26 13:46:49 -05001122 (char *)&subscribe, sizeof(subscribe));
Patrick Caulfield6ed72572007-04-17 15:39:57 +01001123 if (result < 0) {
1124 log_print("Failed to set SCTP_EVENTS on socket: result=%d",
1125 result);
1126 goto create_delsock;
1127 }
1128
1129 /* Init con struct */
1130 sock->sk->sk_user_data = con;
1131 con->sock = sock;
1132 con->sock->sk->sk_data_ready = lowcomms_data_ready;
1133 con->rx_action = receive_from_sock;
1134 con->connect_action = sctp_init_assoc;
1135
1136 /* Bind to all interfaces. */
1137 for (i = 0; i < dlm_local_count; i++) {
1138 memcpy(&localaddr, dlm_local_addr[i], sizeof(localaddr));
1139 make_sockaddr(&localaddr, dlm_config.ci_tcp_port, &addr_len);
1140
1141 result = add_sctp_bind_addr(con, &localaddr, addr_len, num);
1142 if (result)
1143 goto create_delsock;
1144 ++num;
1145 }
1146
1147 result = sock->ops->listen(sock, 5);
1148 if (result < 0) {
1149 log_print("Can't set socket listening");
1150 goto create_delsock;
1151 }
1152
1153 return 0;
1154
1155create_delsock:
1156 sock_release(sock);
1157 con->sock = NULL;
1158out:
1159 return result;
1160}
1161
1162static int tcp_listen_for_all(void)
1163{
1164 struct socket *sock = NULL;
1165 struct connection *con = nodeid2con(0, GFP_KERNEL);
1166 int result = -EINVAL;
1167
1168 if (!con)
1169 return -ENOMEM;
1170
1171 /* We don't support multi-homed hosts */
1172 if (dlm_local_addr[1] != NULL) {
David Teigland617e82e2007-04-26 13:46:49 -05001173 log_print("TCP protocol can't handle multi-homed hosts, "
1174 "try SCTP");
Patrick Caulfield6ed72572007-04-17 15:39:57 +01001175 return -EINVAL;
1176 }
1177
1178 log_print("Using TCP for communications");
1179
Patrick Caulfield6ed72572007-04-17 15:39:57 +01001180 sock = tcp_create_listen_sock(con, dlm_local_addr[0]);
1181 if (sock) {
1182 add_sock(sock, con);
1183 result = 0;
1184 }
1185 else {
1186 result = -EADDRINUSE;
1187 }
1188
1189 return result;
1190}
1191
1192
1193
1194static struct writequeue_entry *new_writequeue_entry(struct connection *con,
1195 gfp_t allocation)
1196{
1197 struct writequeue_entry *entry;
1198
1199 entry = kmalloc(sizeof(struct writequeue_entry), allocation);
1200 if (!entry)
1201 return NULL;
1202
1203 entry->page = alloc_page(allocation);
1204 if (!entry->page) {
1205 kfree(entry);
1206 return NULL;
1207 }
1208
1209 entry->offset = 0;
1210 entry->len = 0;
1211 entry->end = 0;
1212 entry->users = 0;
1213 entry->con = con;
1214
1215 return entry;
1216}
1217
David Teigland617e82e2007-04-26 13:46:49 -05001218void *dlm_lowcomms_get_buffer(int nodeid, int len, gfp_t allocation, char **ppc)
Patrick Caulfield6ed72572007-04-17 15:39:57 +01001219{
1220 struct connection *con;
1221 struct writequeue_entry *e;
1222 int offset = 0;
1223 int users = 0;
1224
1225 con = nodeid2con(nodeid, allocation);
1226 if (!con)
1227 return NULL;
1228
1229 spin_lock(&con->writequeue_lock);
1230 e = list_entry(con->writequeue.prev, struct writequeue_entry, list);
1231 if ((&e->list == &con->writequeue) ||
1232 (PAGE_CACHE_SIZE - e->end < len)) {
1233 e = NULL;
1234 } else {
1235 offset = e->end;
1236 e->end += len;
1237 users = e->users++;
1238 }
1239 spin_unlock(&con->writequeue_lock);
1240
1241 if (e) {
1242 got_one:
Patrick Caulfield6ed72572007-04-17 15:39:57 +01001243 *ppc = page_address(e->page) + offset;
1244 return e;
1245 }
1246
1247 e = new_writequeue_entry(con, allocation);
1248 if (e) {
1249 spin_lock(&con->writequeue_lock);
1250 offset = e->end;
1251 e->end += len;
1252 users = e->users++;
1253 list_add_tail(&e->list, &con->writequeue);
1254 spin_unlock(&con->writequeue_lock);
1255 goto got_one;
1256 }
1257 return NULL;
1258}
1259
1260void dlm_lowcomms_commit_buffer(void *mh)
1261{
1262 struct writequeue_entry *e = (struct writequeue_entry *)mh;
1263 struct connection *con = e->con;
1264 int users;
1265
1266 spin_lock(&con->writequeue_lock);
1267 users = --e->users;
1268 if (users)
1269 goto out;
1270 e->len = e->end - e->offset;
Patrick Caulfield6ed72572007-04-17 15:39:57 +01001271 spin_unlock(&con->writequeue_lock);
1272
1273 if (!test_and_set_bit(CF_WRITE_PENDING, &con->flags)) {
1274 queue_work(send_workqueue, &con->swork);
1275 }
1276 return;
1277
1278out:
1279 spin_unlock(&con->writequeue_lock);
1280 return;
1281}
1282
1283/* Send a message */
1284static void send_to_sock(struct connection *con)
1285{
1286 int ret = 0;
1287 ssize_t(*sendpage) (struct socket *, struct page *, int, size_t, int);
1288 const int msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL;
1289 struct writequeue_entry *e;
1290 int len, offset;
1291
1292 mutex_lock(&con->sock_mutex);
1293 if (con->sock == NULL)
1294 goto out_connect;
1295
1296 sendpage = con->sock->ops->sendpage;
1297
1298 spin_lock(&con->writequeue_lock);
1299 for (;;) {
1300 e = list_entry(con->writequeue.next, struct writequeue_entry,
1301 list);
1302 if ((struct list_head *) e == &con->writequeue)
1303 break;
1304
1305 len = e->len;
1306 offset = e->offset;
1307 BUG_ON(len == 0 && e->users == 0);
1308 spin_unlock(&con->writequeue_lock);
Patrick Caulfield6ed72572007-04-17 15:39:57 +01001309
1310 ret = 0;
1311 if (len) {
1312 ret = sendpage(con->sock, e->page, offset, len,
1313 msg_flags);
Patrick Caulfieldd66f8272007-09-14 08:49:21 +01001314 if (ret == -EAGAIN || ret == 0) {
1315 cond_resched();
Patrick Caulfield6ed72572007-04-17 15:39:57 +01001316 goto out;
Patrick Caulfieldd66f8272007-09-14 08:49:21 +01001317 }
Patrick Caulfield6ed72572007-04-17 15:39:57 +01001318 if (ret <= 0)
1319 goto send_error;
Patrick Caulfieldd66f8272007-09-14 08:49:21 +01001320 }
Patrick Caulfield6ed72572007-04-17 15:39:57 +01001321 /* Don't starve people filling buffers */
1322 cond_resched();
Patrick Caulfield6ed72572007-04-17 15:39:57 +01001323
1324 spin_lock(&con->writequeue_lock);
1325 e->offset += ret;
1326 e->len -= ret;
1327
1328 if (e->len == 0 && e->users == 0) {
1329 list_del(&e->list);
Patrick Caulfield6ed72572007-04-17 15:39:57 +01001330 free_entry(e);
1331 continue;
1332 }
1333 }
1334 spin_unlock(&con->writequeue_lock);
1335out:
1336 mutex_unlock(&con->sock_mutex);
1337 return;
1338
1339send_error:
1340 mutex_unlock(&con->sock_mutex);
1341 close_connection(con, false);
1342 lowcomms_connect_sock(con);
1343 return;
1344
1345out_connect:
1346 mutex_unlock(&con->sock_mutex);
1347 if (!test_bit(CF_INIT_PENDING, &con->flags))
1348 lowcomms_connect_sock(con);
1349 return;
1350}
1351
1352static void clean_one_writequeue(struct connection *con)
1353{
Christine Caulfield5e9ccc32009-01-28 12:57:40 -06001354 struct writequeue_entry *e, *safe;
Patrick Caulfield6ed72572007-04-17 15:39:57 +01001355
1356 spin_lock(&con->writequeue_lock);
Christine Caulfield5e9ccc32009-01-28 12:57:40 -06001357 list_for_each_entry_safe(e, safe, &con->writequeue, list) {
Patrick Caulfield6ed72572007-04-17 15:39:57 +01001358 list_del(&e->list);
1359 free_entry(e);
1360 }
1361 spin_unlock(&con->writequeue_lock);
1362}
1363
1364/* Called from recovery when it knows that a node has
1365 left the cluster */
1366int dlm_lowcomms_close(int nodeid)
1367{
1368 struct connection *con;
1369
1370 log_print("closing connection to node %d", nodeid);
1371 con = nodeid2con(nodeid, 0);
1372 if (con) {
1373 clean_one_writequeue(con);
1374 close_connection(con, true);
1375 }
1376 return 0;
1377}
1378
1379/* Receive workqueue function */
1380static void process_recv_sockets(struct work_struct *work)
1381{
1382 struct connection *con = container_of(work, struct connection, rwork);
1383 int err;
1384
1385 clear_bit(CF_READ_PENDING, &con->flags);
1386 do {
1387 err = con->rx_action(con);
1388 } while (!err);
1389}
1390
1391/* Send workqueue function */
1392static void process_send_sockets(struct work_struct *work)
1393{
1394 struct connection *con = container_of(work, struct connection, swork);
1395
1396 if (test_and_clear_bit(CF_CONNECT_PENDING, &con->flags)) {
1397 con->connect_action(con);
1398 }
1399 clear_bit(CF_WRITE_PENDING, &con->flags);
1400 send_to_sock(con);
1401}
1402
1403
1404/* Discard all entries on the write queues */
1405static void clean_writequeues(void)
1406{
Christine Caulfield5e9ccc32009-01-28 12:57:40 -06001407 foreach_conn(clean_one_writequeue);
Patrick Caulfield6ed72572007-04-17 15:39:57 +01001408}
1409
1410static void work_stop(void)
1411{
1412 destroy_workqueue(recv_workqueue);
1413 destroy_workqueue(send_workqueue);
1414}
1415
1416static int work_start(void)
1417{
1418 int error;
1419 recv_workqueue = create_workqueue("dlm_recv");
1420 error = IS_ERR(recv_workqueue);
1421 if (error) {
1422 log_print("can't start dlm_recv %d", error);
1423 return error;
1424 }
1425
1426 send_workqueue = create_singlethread_workqueue("dlm_send");
1427 error = IS_ERR(send_workqueue);
1428 if (error) {
1429 log_print("can't start dlm_send %d", error);
1430 destroy_workqueue(recv_workqueue);
1431 return error;
1432 }
1433
1434 return 0;
1435}
1436
Christine Caulfield5e9ccc32009-01-28 12:57:40 -06001437static void stop_conn(struct connection *con)
1438{
1439 con->flags |= 0x0F;
Christine Caulfield391fbdc2009-05-07 10:54:16 -05001440 if (con->sock && con->sock->sk)
Christine Caulfield5e9ccc32009-01-28 12:57:40 -06001441 con->sock->sk->sk_user_data = NULL;
1442}
1443
1444static void free_conn(struct connection *con)
1445{
1446 close_connection(con, true);
1447 if (con->othercon)
1448 kmem_cache_free(con_cache, con->othercon);
1449 hlist_del(&con->list);
1450 kmem_cache_free(con_cache, con);
1451}
1452
Patrick Caulfield6ed72572007-04-17 15:39:57 +01001453void dlm_lowcomms_stop(void)
1454{
Patrick Caulfield6ed72572007-04-17 15:39:57 +01001455 /* Set all the flags to prevent any
1456 socket activity.
1457 */
Matthias Kaehlcke7a936ce2008-05-12 10:04:51 -05001458 mutex_lock(&connections_lock);
Christine Caulfield5e9ccc32009-01-28 12:57:40 -06001459 foreach_conn(stop_conn);
Matthias Kaehlcke7a936ce2008-05-12 10:04:51 -05001460 mutex_unlock(&connections_lock);
Patrick Caulfield6ed72572007-04-17 15:39:57 +01001461
1462 work_stop();
1463
Matthias Kaehlcke7a936ce2008-05-12 10:04:51 -05001464 mutex_lock(&connections_lock);
Patrick Caulfield6ed72572007-04-17 15:39:57 +01001465 clean_writequeues();
1466
Christine Caulfield5e9ccc32009-01-28 12:57:40 -06001467 foreach_conn(free_conn);
1468
Matthias Kaehlcke7a936ce2008-05-12 10:04:51 -05001469 mutex_unlock(&connections_lock);
Patrick Caulfield6ed72572007-04-17 15:39:57 +01001470 kmem_cache_destroy(con_cache);
Patrick Caulfield6ed72572007-04-17 15:39:57 +01001471}
1472
1473int dlm_lowcomms_start(void)
1474{
1475 int error = -EINVAL;
1476 struct connection *con;
Christine Caulfield5e9ccc32009-01-28 12:57:40 -06001477 int i;
1478
1479 for (i = 0; i < CONN_HASH_SIZE; i++)
1480 INIT_HLIST_HEAD(&connection_hash[i]);
Patrick Caulfield6ed72572007-04-17 15:39:57 +01001481
1482 init_local();
1483 if (!dlm_local_count) {
David Teigland617e82e2007-04-26 13:46:49 -05001484 error = -ENOTCONN;
Patrick Caulfield6ed72572007-04-17 15:39:57 +01001485 log_print("no local IP address has been set");
1486 goto out;
1487 }
1488
1489 error = -ENOMEM;
1490 con_cache = kmem_cache_create("dlm_conn", sizeof(struct connection),
1491 __alignof__(struct connection), 0,
Paul Mundt20c2df82007-07-20 10:11:58 +09001492 NULL);
Patrick Caulfield6ed72572007-04-17 15:39:57 +01001493 if (!con_cache)
1494 goto out;
1495
Patrick Caulfield6ed72572007-04-17 15:39:57 +01001496 /* Start listening */
1497 if (dlm_config.ci_protocol == 0)
1498 error = tcp_listen_for_all();
1499 else
1500 error = sctp_listen_for_all();
1501 if (error)
1502 goto fail_unlisten;
1503
1504 error = work_start();
1505 if (error)
1506 goto fail_unlisten;
1507
1508 return 0;
1509
1510fail_unlisten:
1511 con = nodeid2con(0,0);
1512 if (con) {
1513 close_connection(con, false);
1514 kmem_cache_free(con_cache, con);
1515 }
1516 kmem_cache_destroy(con_cache);
1517
1518out:
1519 return error;
1520}