blob: 6e27201f2f957952a2743f853e0a0ea0a55f269d [file] [log] [blame]
Patrick Caulfieldfdda3872006-11-02 11:19:21 -05001/******************************************************************************
2*******************************************************************************
3**
4** Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
5** Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
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
24 * be expanded for the cluster infrastructure then that is it's
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 *
39 * I don't see any problem with the recv thread executing the locking
40 * code on behalf of remote processes as the locking code is
41 * short, efficient and never waits.
42 *
43 */
44
45
46#include <asm/ioctls.h>
47#include <net/sock.h>
48#include <net/tcp.h>
49#include <linux/pagemap.h>
50
51#include "dlm_internal.h"
52#include "lowcomms.h"
53#include "midcomms.h"
54#include "config.h"
55
56struct cbuf {
Patrick Caulfieldac33d072006-12-06 15:10:37 +000057 unsigned int base;
58 unsigned int len;
59 unsigned int mask;
Patrick Caulfieldfdda3872006-11-02 11:19:21 -050060};
61
Patrick Caulfieldfdda3872006-11-02 11:19:21 -050062#define NODE_INCREMENT 32
Patrick Caulfieldac33d072006-12-06 15:10:37 +000063static void cbuf_add(struct cbuf *cb, int n)
64{
65 cb->len += n;
66}
Patrick Caulfieldfdda3872006-11-02 11:19:21 -050067
Patrick Caulfieldac33d072006-12-06 15:10:37 +000068static int cbuf_data(struct cbuf *cb)
69{
70 return ((cb->base + cb->len) & cb->mask);
71}
72
73static void cbuf_init(struct cbuf *cb, int size)
74{
75 cb->base = cb->len = 0;
76 cb->mask = size-1;
77}
78
79static void cbuf_eat(struct cbuf *cb, int n)
80{
81 cb->len -= n;
82 cb->base += n;
83 cb->base &= cb->mask;
84}
85
86static bool cbuf_empty(struct cbuf *cb)
87{
88 return cb->len == 0;
89}
Patrick Caulfieldfdda3872006-11-02 11:19:21 -050090
91/* Maximum number of incoming messages to process before
Patrick Caulfieldac33d072006-12-06 15:10:37 +000092 doing a cond_resched()
Patrick Caulfieldfdda3872006-11-02 11:19:21 -050093*/
94#define MAX_RX_MSG_COUNT 25
95
96struct connection {
97 struct socket *sock; /* NULL if not connected */
98 uint32_t nodeid; /* So we know who we are in the list */
Patrick Caulfieldac33d072006-12-06 15:10:37 +000099 struct rw_semaphore sock_sem; /* Stop connect races */
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500100 unsigned long flags; /* bit 1,2 = We are on the read/write lists */
101#define CF_READ_PENDING 1
102#define CF_WRITE_PENDING 2
103#define CF_CONNECT_PENDING 3
104#define CF_IS_OTHERCON 4
Patrick Caulfieldac33d072006-12-06 15:10:37 +0000105 struct list_head writequeue; /* List of outgoing writequeue_entries */
106 struct list_head listenlist; /* List of allocated listening sockets */
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500107 spinlock_t writequeue_lock;
108 int (*rx_action) (struct connection *); /* What to do when active */
109 struct page *rx_page;
110 struct cbuf cb;
111 int retries;
112 atomic_t waiting_requests;
113#define MAX_CONNECT_RETRIES 3
114 struct connection *othercon;
Patrick Caulfield1d6e8132007-01-15 14:33:34 +0000115 struct work_struct rwork; /* Receive workqueue */
116 struct work_struct swork; /* Send workqueue */
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500117};
118#define sock2con(x) ((struct connection *)(x)->sk_user_data)
119
120/* An entry waiting to be sent */
121struct writequeue_entry {
122 struct list_head list;
123 struct page *page;
124 int offset;
125 int len;
126 int end;
127 int users;
128 struct connection *con;
129};
130
131static struct sockaddr_storage dlm_local_addr;
132
Patrick Caulfield1d6e8132007-01-15 14:33:34 +0000133/* Work queues */
134static struct workqueue_struct *recv_workqueue;
135static struct workqueue_struct *send_workqueue;
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500136
137/* An array of pointers to connections, indexed by NODEID */
138static struct connection **connections;
Patrick Caulfieldac33d072006-12-06 15:10:37 +0000139static DECLARE_MUTEX(connections_lock);
Patrick Caulfieldc80e7c82006-12-08 14:31:12 -0500140static struct kmem_cache *con_cache;
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500141static int conn_array_size;
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500142
Patrick Caulfield1d6e8132007-01-15 14:33:34 +0000143static void process_recv_sockets(struct work_struct *work);
144static void process_send_sockets(struct work_struct *work);
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500145
146static struct connection *nodeid2con(int nodeid, gfp_t allocation)
147{
148 struct connection *con = NULL;
149
150 down(&connections_lock);
151 if (nodeid >= conn_array_size) {
152 int new_size = nodeid + NODE_INCREMENT;
153 struct connection **new_conns;
154
Patrick Caulfieldac33d072006-12-06 15:10:37 +0000155 new_conns = kzalloc(sizeof(struct connection *) *
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500156 new_size, allocation);
157 if (!new_conns)
158 goto finish;
159
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500160 memcpy(new_conns, connections, sizeof(struct connection *) * conn_array_size);
161 conn_array_size = new_size;
162 kfree(connections);
163 connections = new_conns;
164
165 }
166
167 con = connections[nodeid];
168 if (con == NULL && allocation) {
Patrick Caulfieldac33d072006-12-06 15:10:37 +0000169 con = kmem_cache_zalloc(con_cache, allocation);
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500170 if (!con)
171 goto finish;
172
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500173 con->nodeid = nodeid;
174 init_rwsem(&con->sock_sem);
175 INIT_LIST_HEAD(&con->writequeue);
176 spin_lock_init(&con->writequeue_lock);
Patrick Caulfield1d6e8132007-01-15 14:33:34 +0000177 INIT_WORK(&con->swork, process_send_sockets);
178 INIT_WORK(&con->rwork, process_recv_sockets);
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500179
180 connections[nodeid] = con;
181 }
182
Patrick Caulfieldac33d072006-12-06 15:10:37 +0000183finish:
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500184 up(&connections_lock);
185 return con;
186}
187
188/* Data available on socket or listen socket received a connect */
189static void lowcomms_data_ready(struct sock *sk, int count_unused)
190{
191 struct connection *con = sock2con(sk);
192
Patrick Caulfield1d6e8132007-01-15 14:33:34 +0000193 if (!test_and_set_bit(CF_READ_PENDING, &con->flags))
194 queue_work(recv_workqueue, &con->rwork);
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500195}
196
197static void lowcomms_write_space(struct sock *sk)
198{
199 struct connection *con = sock2con(sk);
200
Patrick Caulfield1d6e8132007-01-15 14:33:34 +0000201 if (!test_and_set_bit(CF_WRITE_PENDING, &con->flags))
202 queue_work(send_workqueue, &con->swork);
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500203}
204
205static inline void lowcomms_connect_sock(struct connection *con)
206{
Patrick Caulfield1d6e8132007-01-15 14:33:34 +0000207 if (!test_and_set_bit(CF_CONNECT_PENDING, &con->flags))
208 queue_work(send_workqueue, &con->swork);
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500209}
210
211static void lowcomms_state_change(struct sock *sk)
212{
Patrick Caulfieldac33d072006-12-06 15:10:37 +0000213 if (sk->sk_state == TCP_ESTABLISHED)
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500214 lowcomms_write_space(sk);
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500215}
216
217/* Make a socket active */
218static int add_sock(struct socket *sock, struct connection *con)
219{
220 con->sock = sock;
221
222 /* Install a data_ready callback */
223 con->sock->sk->sk_data_ready = lowcomms_data_ready;
224 con->sock->sk->sk_write_space = lowcomms_write_space;
225 con->sock->sk->sk_state_change = lowcomms_state_change;
226
227 return 0;
228}
229
230/* Add the port number to an IP6 or 4 sockaddr and return the address
231 length */
232static void make_sockaddr(struct sockaddr_storage *saddr, uint16_t port,
233 int *addr_len)
234{
Patrick Caulfieldac33d072006-12-06 15:10:37 +0000235 saddr->ss_family = dlm_local_addr.ss_family;
236 if (saddr->ss_family == AF_INET) {
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500237 struct sockaddr_in *in4_addr = (struct sockaddr_in *)saddr;
238 in4_addr->sin_port = cpu_to_be16(port);
239 *addr_len = sizeof(struct sockaddr_in);
Patrick Caulfieldac33d072006-12-06 15:10:37 +0000240 } else {
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500241 struct sockaddr_in6 *in6_addr = (struct sockaddr_in6 *)saddr;
242 in6_addr->sin6_port = cpu_to_be16(port);
243 *addr_len = sizeof(struct sockaddr_in6);
244 }
245}
246
247/* Close a remote connection and tidy up */
Patrick Caulfieldac33d072006-12-06 15:10:37 +0000248static void close_connection(struct connection *con, bool and_other)
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500249{
250 down_write(&con->sock_sem);
251
252 if (con->sock) {
253 sock_release(con->sock);
254 con->sock = NULL;
255 }
256 if (con->othercon && and_other) {
Patrick Caulfieldac33d072006-12-06 15:10:37 +0000257 /* Will only re-enter once. */
258 close_connection(con->othercon, false);
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500259 }
260 if (con->rx_page) {
261 __free_page(con->rx_page);
262 con->rx_page = NULL;
263 }
264 con->retries = 0;
265 up_write(&con->sock_sem);
266}
267
268/* Data received from remote end */
269static int receive_from_sock(struct connection *con)
270{
271 int ret = 0;
272 struct msghdr msg;
273 struct iovec iov[2];
274 mm_segment_t fs;
275 unsigned len;
276 int r;
277 int call_again_soon = 0;
278
279 down_read(&con->sock_sem);
280
281 if (con->sock == NULL)
282 goto out;
283 if (con->rx_page == NULL) {
284 /*
285 * This doesn't need to be atomic, but I think it should
286 * improve performance if it is.
287 */
288 con->rx_page = alloc_page(GFP_ATOMIC);
289 if (con->rx_page == NULL)
290 goto out_resched;
Patrick Caulfieldac33d072006-12-06 15:10:37 +0000291 cbuf_init(&con->cb, PAGE_CACHE_SIZE);
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500292 }
293
294 msg.msg_control = NULL;
295 msg.msg_controllen = 0;
296 msg.msg_iovlen = 1;
297 msg.msg_iov = iov;
298 msg.msg_name = NULL;
299 msg.msg_namelen = 0;
300 msg.msg_flags = 0;
301
302 /*
303 * iov[0] is the bit of the circular buffer between the current end
304 * point (cb.base + cb.len) and the end of the buffer.
305 */
Patrick Caulfieldac33d072006-12-06 15:10:37 +0000306 iov[0].iov_len = con->cb.base - cbuf_data(&con->cb);
307 iov[0].iov_base = page_address(con->rx_page) + cbuf_data(&con->cb);
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500308 iov[1].iov_len = 0;
309
310 /*
311 * iov[1] is the bit of the circular buffer between the start of the
312 * buffer and the start of the currently used section (cb.base)
313 */
Patrick Caulfieldac33d072006-12-06 15:10:37 +0000314 if (cbuf_data(&con->cb) >= con->cb.base) {
315 iov[0].iov_len = PAGE_CACHE_SIZE - cbuf_data(&con->cb);
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500316 iov[1].iov_len = con->cb.base;
317 iov[1].iov_base = page_address(con->rx_page);
318 msg.msg_iovlen = 2;
319 }
320 len = iov[0].iov_len + iov[1].iov_len;
321
322 fs = get_fs();
323 set_fs(get_ds());
324 r = ret = sock_recvmsg(con->sock, &msg, len,
325 MSG_DONTWAIT | MSG_NOSIGNAL);
326 set_fs(fs);
327
328 if (ret <= 0)
329 goto out_close;
330 if (ret == len)
331 call_again_soon = 1;
Patrick Caulfieldac33d072006-12-06 15:10:37 +0000332 cbuf_add(&con->cb, ret);
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500333 ret = dlm_process_incoming_buffer(con->nodeid,
334 page_address(con->rx_page),
335 con->cb.base, con->cb.len,
336 PAGE_CACHE_SIZE);
337 if (ret == -EBADMSG) {
338 printk(KERN_INFO "dlm: lowcomms: addr=%p, base=%u, len=%u, "
339 "iov_len=%u, iov_base[0]=%p, read=%d\n",
340 page_address(con->rx_page), con->cb.base, con->cb.len,
341 len, iov[0].iov_base, r);
342 }
343 if (ret < 0)
344 goto out_close;
Patrick Caulfieldac33d072006-12-06 15:10:37 +0000345 cbuf_eat(&con->cb, ret);
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500346
Patrick Caulfieldac33d072006-12-06 15:10:37 +0000347 if (cbuf_empty(&con->cb) && !call_again_soon) {
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500348 __free_page(con->rx_page);
349 con->rx_page = NULL;
350 }
351
Patrick Caulfieldac33d072006-12-06 15:10:37 +0000352out:
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500353 if (call_again_soon)
354 goto out_resched;
355 up_read(&con->sock_sem);
Patrick Caulfieldac33d072006-12-06 15:10:37 +0000356 return 0;
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500357
Patrick Caulfieldac33d072006-12-06 15:10:37 +0000358out_resched:
Patrick Caulfield1d6e8132007-01-15 14:33:34 +0000359 if (!test_and_set_bit(CF_READ_PENDING, &con->flags))
360 queue_work(recv_workqueue, &con->rwork);
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500361 up_read(&con->sock_sem);
Patrick Caulfieldac33d072006-12-06 15:10:37 +0000362 cond_resched();
363 return 0;
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500364
Patrick Caulfieldac33d072006-12-06 15:10:37 +0000365out_close:
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500366 up_read(&con->sock_sem);
367 if (ret != -EAGAIN && !test_bit(CF_IS_OTHERCON, &con->flags)) {
Patrick Caulfieldac33d072006-12-06 15:10:37 +0000368 close_connection(con, false);
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500369 /* Reconnect when there is something to send */
370 }
371
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500372 return ret;
373}
374
375/* Listening socket is busy, accept a connection */
376static int accept_from_sock(struct connection *con)
377{
378 int result;
379 struct sockaddr_storage peeraddr;
380 struct socket *newsock;
381 int len;
382 int nodeid;
383 struct connection *newcon;
384
385 memset(&peeraddr, 0, sizeof(peeraddr));
Patrick Caulfieldac33d072006-12-06 15:10:37 +0000386 result = sock_create_kern(dlm_local_addr.ss_family, SOCK_STREAM,
387 IPPROTO_TCP, &newsock);
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500388 if (result < 0)
389 return -ENOMEM;
390
Patrick Caulfieldf2f50952007-01-22 14:50:10 +0000391 down_read_nested(&con->sock_sem, 0);
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500392
393 result = -ENOTCONN;
394 if (con->sock == NULL)
395 goto accept_err;
396
397 newsock->type = con->sock->type;
398 newsock->ops = con->sock->ops;
399
400 result = con->sock->ops->accept(con->sock, newsock, O_NONBLOCK);
401 if (result < 0)
402 goto accept_err;
403
404 /* Get the connected socket's peer */
405 memset(&peeraddr, 0, sizeof(peeraddr));
406 if (newsock->ops->getname(newsock, (struct sockaddr *)&peeraddr,
407 &len, 2)) {
408 result = -ECONNABORTED;
409 goto accept_err;
410 }
411
412 /* Get the new node's NODEID */
413 make_sockaddr(&peeraddr, 0, &len);
414 if (dlm_addr_to_nodeid(&peeraddr, &nodeid)) {
Patrick Caulfieldac33d072006-12-06 15:10:37 +0000415 printk("dlm: connect from non cluster node\n");
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500416 sock_release(newsock);
417 up_read(&con->sock_sem);
418 return -1;
419 }
420
421 log_print("got connection from %d", nodeid);
422
423 /* Check to see if we already have a connection to this node. This
424 * could happen if the two nodes initiate a connection at roughly
425 * the same time and the connections cross on the wire.
426 * TEMPORARY FIX:
427 * In this case we store the incoming one in "othercon"
428 */
429 newcon = nodeid2con(nodeid, GFP_KERNEL);
430 if (!newcon) {
431 result = -ENOMEM;
432 goto accept_err;
433 }
Patrick Caulfieldf2f50952007-01-22 14:50:10 +0000434 down_write_nested(&newcon->sock_sem, 1);
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500435 if (newcon->sock) {
Patrick Caulfieldac33d072006-12-06 15:10:37 +0000436 struct connection *othercon = newcon->othercon;
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500437
438 if (!othercon) {
Patrick Caulfieldac33d072006-12-06 15:10:37 +0000439 othercon = kmem_cache_zalloc(con_cache, GFP_KERNEL);
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500440 if (!othercon) {
441 printk("dlm: failed to allocate incoming socket\n");
442 up_write(&newcon->sock_sem);
443 result = -ENOMEM;
444 goto accept_err;
445 }
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500446 othercon->nodeid = nodeid;
447 othercon->rx_action = receive_from_sock;
448 init_rwsem(&othercon->sock_sem);
Patrick Caulfield1d6e8132007-01-15 14:33:34 +0000449 INIT_WORK(&othercon->swork, process_send_sockets);
450 INIT_WORK(&othercon->rwork, process_recv_sockets);
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500451 set_bit(CF_IS_OTHERCON, &othercon->flags);
452 newcon->othercon = othercon;
453 }
454 othercon->sock = newsock;
455 newsock->sk->sk_user_data = othercon;
456 add_sock(newsock, othercon);
457 }
458 else {
459 newsock->sk->sk_user_data = newcon;
460 newcon->rx_action = receive_from_sock;
461 add_sock(newsock, newcon);
462
463 }
464
465 up_write(&newcon->sock_sem);
466
467 /*
468 * Add it to the active queue in case we got data
469 * beween processing the accept adding the socket
470 * to the read_sockets list
471 */
Patrick Caulfield1d6e8132007-01-15 14:33:34 +0000472 if (!test_and_set_bit(CF_READ_PENDING, &newcon->flags))
473 queue_work(recv_workqueue, &newcon->rwork);
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500474 up_read(&con->sock_sem);
475
476 return 0;
477
Patrick Caulfieldac33d072006-12-06 15:10:37 +0000478accept_err:
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500479 up_read(&con->sock_sem);
480 sock_release(newsock);
481
482 if (result != -EAGAIN)
483 printk("dlm: error accepting connection from node: %d\n", result);
484 return result;
485}
486
487/* Connect a new socket to its peer */
Patrick Caulfieldac33d072006-12-06 15:10:37 +0000488static void connect_to_sock(struct connection *con)
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500489{
490 int result = -EHOSTUNREACH;
491 struct sockaddr_storage saddr;
492 int addr_len;
493 struct socket *sock;
494
495 if (con->nodeid == 0) {
496 log_print("attempt to connect sock 0 foiled");
Patrick Caulfieldac33d072006-12-06 15:10:37 +0000497 return;
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500498 }
499
500 down_write(&con->sock_sem);
501 if (con->retries++ > MAX_CONNECT_RETRIES)
502 goto out;
503
504 /* Some odd races can cause double-connects, ignore them */
505 if (con->sock) {
506 result = 0;
507 goto out;
508 }
509
510 /* Create a socket to communicate with */
Patrick Caulfieldac33d072006-12-06 15:10:37 +0000511 result = sock_create_kern(dlm_local_addr.ss_family, SOCK_STREAM,
512 IPPROTO_TCP, &sock);
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500513 if (result < 0)
514 goto out_err;
515
516 memset(&saddr, 0, sizeof(saddr));
517 if (dlm_nodeid_to_addr(con->nodeid, &saddr))
Patrick Caulfieldac33d072006-12-06 15:10:37 +0000518 goto out_err;
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500519
520 sock->sk->sk_user_data = con;
521 con->rx_action = receive_from_sock;
522
David Teigland68c817a2007-01-09 09:41:48 -0600523 make_sockaddr(&saddr, dlm_config.ci_tcp_port, &addr_len);
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500524
525 add_sock(sock, con);
526
527 log_print("connecting to %d", con->nodeid);
528 result =
529 sock->ops->connect(sock, (struct sockaddr *)&saddr, addr_len,
Patrick Caulfieldac33d072006-12-06 15:10:37 +0000530 O_NONBLOCK);
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500531 if (result == -EINPROGRESS)
532 result = 0;
Patrick Caulfieldac33d072006-12-06 15:10:37 +0000533 if (result == 0)
534 goto out;
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500535
Patrick Caulfieldac33d072006-12-06 15:10:37 +0000536out_err:
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500537 if (con->sock) {
538 sock_release(con->sock);
539 con->sock = NULL;
540 }
541 /*
542 * Some errors are fatal and this list might need adjusting. For other
543 * errors we try again until the max number of retries is reached.
544 */
545 if (result != -EHOSTUNREACH && result != -ENETUNREACH &&
546 result != -ENETDOWN && result != EINVAL
547 && result != -EPROTONOSUPPORT) {
548 lowcomms_connect_sock(con);
549 result = 0;
550 }
Patrick Caulfieldac33d072006-12-06 15:10:37 +0000551out:
552 up_write(&con->sock_sem);
553 return;
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500554}
555
Patrick Caulfieldac33d072006-12-06 15:10:37 +0000556static struct socket *create_listen_sock(struct connection *con,
557 struct sockaddr_storage *saddr)
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500558{
Patrick Caulfieldac33d072006-12-06 15:10:37 +0000559 struct socket *sock = NULL;
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500560 mm_segment_t fs;
561 int result = 0;
562 int one = 1;
563 int addr_len;
564
565 if (dlm_local_addr.ss_family == AF_INET)
566 addr_len = sizeof(struct sockaddr_in);
567 else
568 addr_len = sizeof(struct sockaddr_in6);
569
570 /* Create a socket to communicate with */
571 result = sock_create_kern(dlm_local_addr.ss_family, SOCK_STREAM, IPPROTO_TCP, &sock);
572 if (result < 0) {
573 printk("dlm: Can't create listening comms socket\n");
574 goto create_out;
575 }
576
577 fs = get_fs();
578 set_fs(get_ds());
Patrick Caulfieldac33d072006-12-06 15:10:37 +0000579 result = sock_setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
580 (char *)&one, sizeof(one));
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500581 set_fs(fs);
582 if (result < 0) {
Patrick Caulfieldac33d072006-12-06 15:10:37 +0000583 printk("dlm: Failed to set SO_REUSEADDR on socket: result=%d\n",
584 result);
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500585 }
586 sock->sk->sk_user_data = con;
587 con->rx_action = accept_from_sock;
588 con->sock = sock;
589
590 /* Bind to our port */
David Teigland68c817a2007-01-09 09:41:48 -0600591 make_sockaddr(saddr, dlm_config.ci_tcp_port, &addr_len);
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500592 result = sock->ops->bind(sock, (struct sockaddr *) saddr, addr_len);
593 if (result < 0) {
David Teigland68c817a2007-01-09 09:41:48 -0600594 printk("dlm: Can't bind to port %d\n", dlm_config.ci_tcp_port);
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500595 sock_release(sock);
596 sock = NULL;
597 con->sock = NULL;
598 goto create_out;
599 }
600
601 fs = get_fs();
602 set_fs(get_ds());
603
Patrick Caulfieldac33d072006-12-06 15:10:37 +0000604 result = sock_setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE,
605 (char *)&one, sizeof(one));
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500606 set_fs(fs);
607 if (result < 0) {
608 printk("dlm: Set keepalive failed: %d\n", result);
609 }
610
611 result = sock->ops->listen(sock, 5);
612 if (result < 0) {
David Teigland68c817a2007-01-09 09:41:48 -0600613 printk("dlm: Can't listen on port %d\n",
614 dlm_config.ci_tcp_port);
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500615 sock_release(sock);
616 sock = NULL;
617 goto create_out;
618 }
619
Patrick Caulfieldac33d072006-12-06 15:10:37 +0000620create_out:
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500621 return sock;
622}
623
624
625/* Listen on all interfaces */
626static int listen_for_all(void)
627{
628 struct socket *sock = NULL;
629 struct connection *con = nodeid2con(0, GFP_KERNEL);
630 int result = -EINVAL;
631
632 /* We don't support multi-homed hosts */
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500633 set_bit(CF_IS_OTHERCON, &con->flags);
634
635 sock = create_listen_sock(con, &dlm_local_addr);
636 if (sock) {
637 add_sock(sock, con);
638 result = 0;
639 }
640 else {
641 result = -EADDRINUSE;
642 }
643
644 return result;
645}
646
647
648
649static struct writequeue_entry *new_writequeue_entry(struct connection *con,
650 gfp_t allocation)
651{
652 struct writequeue_entry *entry;
653
654 entry = kmalloc(sizeof(struct writequeue_entry), allocation);
655 if (!entry)
656 return NULL;
657
658 entry->page = alloc_page(allocation);
659 if (!entry->page) {
660 kfree(entry);
661 return NULL;
662 }
663
664 entry->offset = 0;
665 entry->len = 0;
666 entry->end = 0;
667 entry->users = 0;
668 entry->con = con;
669
670 return entry;
671}
672
673void *dlm_lowcomms_get_buffer(int nodeid, int len,
674 gfp_t allocation, char **ppc)
675{
676 struct connection *con;
677 struct writequeue_entry *e;
678 int offset = 0;
679 int users = 0;
680
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500681 con = nodeid2con(nodeid, allocation);
682 if (!con)
683 return NULL;
684
Patrick Caulfield4edde742007-01-02 17:08:54 +0000685 spin_lock(&con->writequeue_lock);
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500686 e = list_entry(con->writequeue.prev, struct writequeue_entry, list);
Patrick Caulfieldac33d072006-12-06 15:10:37 +0000687 if ((&e->list == &con->writequeue) ||
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500688 (PAGE_CACHE_SIZE - e->end < len)) {
689 e = NULL;
690 } else {
691 offset = e->end;
692 e->end += len;
693 users = e->users++;
694 }
695 spin_unlock(&con->writequeue_lock);
696
697 if (e) {
Patrick Caulfieldac33d072006-12-06 15:10:37 +0000698 got_one:
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500699 if (users == 0)
700 kmap(e->page);
701 *ppc = page_address(e->page) + offset;
702 return e;
703 }
704
705 e = new_writequeue_entry(con, allocation);
706 if (e) {
707 spin_lock(&con->writequeue_lock);
708 offset = e->end;
709 e->end += len;
710 users = e->users++;
711 list_add_tail(&e->list, &con->writequeue);
712 spin_unlock(&con->writequeue_lock);
713 goto got_one;
714 }
715 return NULL;
716}
717
718void dlm_lowcomms_commit_buffer(void *mh)
719{
720 struct writequeue_entry *e = (struct writequeue_entry *)mh;
721 struct connection *con = e->con;
722 int users;
723
Patrick Caulfield4edde742007-01-02 17:08:54 +0000724 spin_lock(&con->writequeue_lock);
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500725 users = --e->users;
726 if (users)
727 goto out;
728 e->len = e->end - e->offset;
729 kunmap(e->page);
730 spin_unlock(&con->writequeue_lock);
731
Patrick Caulfield1d6e8132007-01-15 14:33:34 +0000732 if (!test_and_set_bit(CF_WRITE_PENDING, &con->flags)) {
733 queue_work(send_workqueue, &con->swork);
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500734 }
735 return;
736
Patrick Caulfieldac33d072006-12-06 15:10:37 +0000737out:
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500738 spin_unlock(&con->writequeue_lock);
739 return;
740}
741
742static void free_entry(struct writequeue_entry *e)
743{
744 __free_page(e->page);
745 kfree(e);
746}
747
748/* Send a message */
Patrick Caulfieldac33d072006-12-06 15:10:37 +0000749static void send_to_sock(struct connection *con)
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500750{
751 int ret = 0;
752 ssize_t(*sendpage) (struct socket *, struct page *, int, size_t, int);
753 const int msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL;
754 struct writequeue_entry *e;
755 int len, offset;
756
757 down_read(&con->sock_sem);
758 if (con->sock == NULL)
759 goto out_connect;
760
761 sendpage = con->sock->ops->sendpage;
762
763 spin_lock(&con->writequeue_lock);
764 for (;;) {
765 e = list_entry(con->writequeue.next, struct writequeue_entry,
766 list);
767 if ((struct list_head *) e == &con->writequeue)
768 break;
769
770 len = e->len;
771 offset = e->offset;
772 BUG_ON(len == 0 && e->users == 0);
773 spin_unlock(&con->writequeue_lock);
Patrick Caulfield1d6e8132007-01-15 14:33:34 +0000774 kmap(e->page);
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500775
776 ret = 0;
777 if (len) {
778 ret = sendpage(con->sock, e->page, offset, len,
779 msg_flags);
780 if (ret == -EAGAIN || ret == 0)
781 goto out;
782 if (ret <= 0)
783 goto send_error;
784 }
785 else {
786 /* Don't starve people filling buffers */
Patrick Caulfieldac33d072006-12-06 15:10:37 +0000787 cond_resched();
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500788 }
789
790 spin_lock(&con->writequeue_lock);
791 e->offset += ret;
792 e->len -= ret;
793
794 if (e->len == 0 && e->users == 0) {
795 list_del(&e->list);
Patrick Caulfieldac33d072006-12-06 15:10:37 +0000796 kunmap(e->page);
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500797 free_entry(e);
798 continue;
799 }
800 }
801 spin_unlock(&con->writequeue_lock);
Patrick Caulfieldac33d072006-12-06 15:10:37 +0000802out:
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500803 up_read(&con->sock_sem);
Patrick Caulfieldac33d072006-12-06 15:10:37 +0000804 return;
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500805
Patrick Caulfieldac33d072006-12-06 15:10:37 +0000806send_error:
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500807 up_read(&con->sock_sem);
Patrick Caulfieldac33d072006-12-06 15:10:37 +0000808 close_connection(con, false);
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500809 lowcomms_connect_sock(con);
Patrick Caulfieldac33d072006-12-06 15:10:37 +0000810 return;
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500811
Patrick Caulfieldac33d072006-12-06 15:10:37 +0000812out_connect:
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500813 up_read(&con->sock_sem);
814 lowcomms_connect_sock(con);
Patrick Caulfieldac33d072006-12-06 15:10:37 +0000815 return;
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500816}
817
818static void clean_one_writequeue(struct connection *con)
819{
820 struct list_head *list;
821 struct list_head *temp;
822
823 spin_lock(&con->writequeue_lock);
824 list_for_each_safe(list, temp, &con->writequeue) {
825 struct writequeue_entry *e =
826 list_entry(list, struct writequeue_entry, list);
827 list_del(&e->list);
828 free_entry(e);
829 }
830 spin_unlock(&con->writequeue_lock);
831}
832
833/* Called from recovery when it knows that a node has
834 left the cluster */
835int dlm_lowcomms_close(int nodeid)
836{
837 struct connection *con;
838
839 if (!connections)
840 goto out;
841
842 log_print("closing connection to node %d", nodeid);
843 con = nodeid2con(nodeid, 0);
844 if (con) {
845 clean_one_writequeue(con);
Patrick Caulfieldac33d072006-12-06 15:10:37 +0000846 close_connection(con, true);
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500847 atomic_set(&con->waiting_requests, 0);
848 }
849 return 0;
850
Patrick Caulfieldac33d072006-12-06 15:10:37 +0000851out:
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500852 return -1;
853}
854
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500855/* Look for activity on active sockets */
Patrick Caulfield1d6e8132007-01-15 14:33:34 +0000856static void process_recv_sockets(struct work_struct *work)
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500857{
Patrick Caulfield1d6e8132007-01-15 14:33:34 +0000858 struct connection *con = container_of(work, struct connection, rwork);
859 int err;
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500860
Patrick Caulfield1d6e8132007-01-15 14:33:34 +0000861 clear_bit(CF_READ_PENDING, &con->flags);
862 do {
863 err = con->rx_action(con);
864 } while (!err);
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500865}
866
Patrick Caulfield1d6e8132007-01-15 14:33:34 +0000867
868static void process_send_sockets(struct work_struct *work)
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500869{
Patrick Caulfield1d6e8132007-01-15 14:33:34 +0000870 struct connection *con = container_of(work, struct connection, swork);
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500871
Patrick Caulfield1d6e8132007-01-15 14:33:34 +0000872 if (test_and_clear_bit(CF_CONNECT_PENDING, &con->flags)) {
Patrick Caulfieldac33d072006-12-06 15:10:37 +0000873 connect_to_sock(con);
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500874 }
Patrick Caulfield1d6e8132007-01-15 14:33:34 +0000875
876 if (test_and_clear_bit(CF_WRITE_PENDING, &con->flags)) {
877 send_to_sock(con);
878 }
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500879}
880
881
882/* Discard all entries on the write queues */
883static void clean_writequeues(void)
884{
885 int nodeid;
886
887 for (nodeid = 1; nodeid < conn_array_size; nodeid++) {
888 struct connection *con = nodeid2con(nodeid, 0);
889
890 if (con)
891 clean_one_writequeue(con);
892 }
893}
894
Patrick Caulfield1d6e8132007-01-15 14:33:34 +0000895static void work_stop(void)
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500896{
Patrick Caulfield1d6e8132007-01-15 14:33:34 +0000897 destroy_workqueue(recv_workqueue);
898 destroy_workqueue(send_workqueue);
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500899}
900
Patrick Caulfield1d6e8132007-01-15 14:33:34 +0000901static int work_start(void)
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500902{
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500903 int error;
Patrick Caulfield1d6e8132007-01-15 14:33:34 +0000904 recv_workqueue = create_workqueue("dlm_recv");
905 error = IS_ERR(recv_workqueue);
Patrick Caulfieldac33d072006-12-06 15:10:37 +0000906 if (error) {
Patrick Caulfield1d6e8132007-01-15 14:33:34 +0000907 log_print("can't start dlm_recv %d", error);
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500908 return error;
909 }
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500910
Patrick Caulfield1d6e8132007-01-15 14:33:34 +0000911 send_workqueue = create_singlethread_workqueue("dlm_send");
912 error = IS_ERR(send_workqueue);
Patrick Caulfieldac33d072006-12-06 15:10:37 +0000913 if (error) {
Patrick Caulfield1d6e8132007-01-15 14:33:34 +0000914 log_print("can't start dlm_send %d", error);
915 destroy_workqueue(recv_workqueue);
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500916 return error;
917 }
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500918
919 return 0;
920}
921
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500922void dlm_lowcomms_stop(void)
923{
924 int i;
925
Patrick Caulfieldac33d072006-12-06 15:10:37 +0000926 /* Set all the flags to prevent any
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500927 socket activity.
928 */
929 for (i = 0; i < conn_array_size; i++) {
930 if (connections[i])
Patrick Caulfieldac33d072006-12-06 15:10:37 +0000931 connections[i]->flags |= 0xFF;
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500932 }
Patrick Caulfieldac33d072006-12-06 15:10:37 +0000933
Patrick Caulfield1d6e8132007-01-15 14:33:34 +0000934 work_stop();
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500935 clean_writequeues();
936
937 for (i = 0; i < conn_array_size; i++) {
938 if (connections[i]) {
Patrick Caulfieldac33d072006-12-06 15:10:37 +0000939 close_connection(connections[i], true);
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500940 if (connections[i]->othercon)
941 kmem_cache_free(con_cache, connections[i]->othercon);
942 kmem_cache_free(con_cache, connections[i]);
943 }
944 }
945
946 kfree(connections);
947 connections = NULL;
948
949 kmem_cache_destroy(con_cache);
950}
951
952/* This is quite likely to sleep... */
953int dlm_lowcomms_start(void)
954{
955 int error = 0;
956
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500957 error = -ENOMEM;
Patrick Caulfieldac33d072006-12-06 15:10:37 +0000958 connections = kzalloc(sizeof(struct connection *) *
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500959 NODE_INCREMENT, GFP_KERNEL);
960 if (!connections)
961 goto out;
962
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500963 conn_array_size = NODE_INCREMENT;
964
965 if (dlm_our_addr(&dlm_local_addr, 0)) {
966 log_print("no local IP address has been set");
967 goto fail_free_conn;
968 }
969 if (!dlm_our_addr(&dlm_local_addr, 1)) {
970 log_print("This dlm comms module does not support multi-homed clustering");
971 goto fail_free_conn;
972 }
973
974 con_cache = kmem_cache_create("dlm_conn", sizeof(struct connection),
Patrick Caulfieldac33d072006-12-06 15:10:37 +0000975 __alignof__(struct connection), 0,
976 NULL, NULL);
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500977 if (!con_cache)
978 goto fail_free_conn;
979
980
981 /* Start listening */
982 error = listen_for_all();
983 if (error)
984 goto fail_unlisten;
985
Patrick Caulfield1d6e8132007-01-15 14:33:34 +0000986 error = work_start();
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500987 if (error)
988 goto fail_unlisten;
989
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500990 return 0;
991
Patrick Caulfieldac33d072006-12-06 15:10:37 +0000992fail_unlisten:
993 close_connection(connections[0], false);
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500994 kmem_cache_free(con_cache, connections[0]);
995 kmem_cache_destroy(con_cache);
996
Patrick Caulfieldac33d072006-12-06 15:10:37 +0000997fail_free_conn:
Patrick Caulfieldfdda3872006-11-02 11:19:21 -0500998 kfree(connections);
999
Patrick Caulfieldac33d072006-12-06 15:10:37 +00001000out:
Patrick Caulfieldfdda3872006-11-02 11:19:21 -05001001 return error;
1002}
1003
Patrick Caulfieldfdda3872006-11-02 11:19:21 -05001004/*
1005 * Overrides for Emacs so that we follow Linus's tabbing style.
1006 * Emacs will notice this stuff at the end of the file and automatically
1007 * adjust the settings for this buffer only. This must remain at the end
1008 * of the file.
1009 * ---------------------------------------------------------------------------
1010 * Local variables:
1011 * c-file-style: "linux"
1012 * End:
1013 */