[GFS2] Make the log reserved blocks depend on block size
[deliverable/linux.git] / fs / dlm / lowcomms.c
CommitLineData
fdda387f
PC
1/******************************************************************************
2*******************************************************************************
3**
4** Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
a34fbc63 5** Copyright (C) 2004-2007 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
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 *
6ed7257b
PC
39 * lowcomms will choose to use wither TCP or SCTP as its transport layer
40 * depending on the configuration variable 'protocol'. This should be set
41 * to 0 (default) for TCP or 1 for SCTP. It shouldbe configured using a
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
PC
51#include <linux/idr.h>
52#include <linux/file.h>
53#include <linux/sctp.h>
54#include <net/sctp/user.h>
fdda387f
PC
55
56#include "dlm_internal.h"
57#include "lowcomms.h"
58#include "midcomms.h"
59#include "config.h"
60
6ed7257b
PC
61#define NEEDED_RMEM (4*1024*1024)
62
fdda387f 63struct cbuf {
ac33d071
PC
64 unsigned int base;
65 unsigned int len;
66 unsigned int mask;
fdda387f
PC
67};
68
ac33d071
PC
69static void cbuf_add(struct cbuf *cb, int n)
70{
71 cb->len += n;
72}
fdda387f 73
ac33d071
PC
74static int cbuf_data(struct cbuf *cb)
75{
76 return ((cb->base + cb->len) & cb->mask);
77}
78
79static void cbuf_init(struct cbuf *cb, int size)
80{
81 cb->base = cb->len = 0;
82 cb->mask = size-1;
83}
84
85static void cbuf_eat(struct cbuf *cb, int n)
86{
87 cb->len -= n;
88 cb->base += n;
89 cb->base &= cb->mask;
90}
91
92static bool cbuf_empty(struct cbuf *cb)
93{
94 return cb->len == 0;
95}
fdda387f 96
fdda387f
PC
97struct connection {
98 struct socket *sock; /* NULL if not connected */
99 uint32_t nodeid; /* So we know who we are in the list */
f1f1c1cc 100 struct mutex sock_mutex;
6ed7257b 101 unsigned long flags;
fdda387f
PC
102#define CF_READ_PENDING 1
103#define CF_WRITE_PENDING 2
104#define CF_CONNECT_PENDING 3
6ed7257b
PC
105#define CF_INIT_PENDING 4
106#define CF_IS_OTHERCON 5
ac33d071 107 struct list_head writequeue; /* List of outgoing writequeue_entries */
fdda387f
PC
108 spinlock_t writequeue_lock;
109 int (*rx_action) (struct connection *); /* What to do when active */
6ed7257b 110 void (*connect_action) (struct connection *); /* What to do to connect */
fdda387f
PC
111 struct page *rx_page;
112 struct cbuf cb;
113 int retries;
fdda387f 114#define MAX_CONNECT_RETRIES 3
6ed7257b 115 int sctp_assoc;
fdda387f 116 struct connection *othercon;
1d6e8131
PC
117 struct work_struct rwork; /* Receive workqueue */
118 struct work_struct swork; /* Send workqueue */
fdda387f
PC
119};
120#define sock2con(x) ((struct connection *)(x)->sk_user_data)
121
122/* An entry waiting to be sent */
123struct writequeue_entry {
124 struct list_head list;
125 struct page *page;
126 int offset;
127 int len;
128 int end;
129 int users;
130 struct connection *con;
131};
132
6ed7257b
PC
133static struct sockaddr_storage *dlm_local_addr[DLM_MAX_ADDR_COUNT];
134static int dlm_local_count;
fdda387f 135
1d6e8131
PC
136/* Work queues */
137static struct workqueue_struct *recv_workqueue;
138static struct workqueue_struct *send_workqueue;
fdda387f 139
6ed7257b 140static DEFINE_IDR(connections_idr);
ac33d071 141static DECLARE_MUTEX(connections_lock);
6ed7257b 142static int max_nodeid;
c80e7c83 143static struct kmem_cache *con_cache;
fdda387f 144
1d6e8131
PC
145static void process_recv_sockets(struct work_struct *work);
146static void process_send_sockets(struct work_struct *work);
fdda387f 147
6ed7257b
PC
148/*
149 * If 'allocation' is zero then we don't attempt to create a new
150 * connection structure for this node.
151 */
152static struct connection *__nodeid2con(int nodeid, gfp_t alloc)
fdda387f
PC
153{
154 struct connection *con = NULL;
6ed7257b
PC
155 int r;
156 int n;
fdda387f 157
6ed7257b
PC
158 con = idr_find(&connections_idr, nodeid);
159 if (con || !alloc)
160 return con;
fdda387f 161
6ed7257b
PC
162 r = idr_pre_get(&connections_idr, alloc);
163 if (!r)
164 return NULL;
165
166 con = kmem_cache_zalloc(con_cache, alloc);
167 if (!con)
168 return NULL;
fdda387f 169
6ed7257b
PC
170 r = idr_get_new_above(&connections_idr, con, nodeid, &n);
171 if (r) {
172 kmem_cache_free(con_cache, con);
173 return NULL;
174 }
fdda387f 175
6ed7257b
PC
176 if (n != nodeid) {
177 idr_remove(&connections_idr, n);
178 kmem_cache_free(con_cache, con);
179 return NULL;
fdda387f
PC
180 }
181
6ed7257b
PC
182 con->nodeid = nodeid;
183 mutex_init(&con->sock_mutex);
184 INIT_LIST_HEAD(&con->writequeue);
185 spin_lock_init(&con->writequeue_lock);
186 INIT_WORK(&con->swork, process_send_sockets);
187 INIT_WORK(&con->rwork, process_recv_sockets);
fdda387f 188
6ed7257b
PC
189 /* Setup action pointers for child sockets */
190 if (con->nodeid) {
191 struct connection *zerocon = idr_find(&connections_idr, 0);
fdda387f 192
6ed7257b
PC
193 con->connect_action = zerocon->connect_action;
194 if (!con->rx_action)
195 con->rx_action = zerocon->rx_action;
fdda387f
PC
196 }
197
6ed7257b
PC
198 if (nodeid > max_nodeid)
199 max_nodeid = nodeid;
200
201 return con;
202}
203
204static struct connection *nodeid2con(int nodeid, gfp_t allocation)
205{
206 struct connection *con;
207
208 down(&connections_lock);
209 con = __nodeid2con(nodeid, allocation);
fdda387f 210 up(&connections_lock);
6ed7257b 211
fdda387f
PC
212 return con;
213}
214
6ed7257b
PC
215/* This is a bit drastic, but only called when things go wrong */
216static struct connection *assoc2con(int assoc_id)
217{
218 int i;
219 struct connection *con;
220
221 down(&connections_lock);
30d3a237 222 for (i=0; i<=max_nodeid; i++) {
6ed7257b
PC
223 con = __nodeid2con(i, 0);
224 if (con && con->sctp_assoc == assoc_id) {
225 up(&connections_lock);
226 return con;
227 }
228 }
229 up(&connections_lock);
230 return NULL;
231}
232
233static int nodeid_to_addr(int nodeid, struct sockaddr *retaddr)
234{
235 struct sockaddr_storage addr;
236 int error;
237
238 if (!dlm_local_count)
239 return -1;
240
241 error = dlm_nodeid_to_addr(nodeid, &addr);
242 if (error)
243 return error;
244
245 if (dlm_local_addr[0]->ss_family == AF_INET) {
246 struct sockaddr_in *in4 = (struct sockaddr_in *) &addr;
247 struct sockaddr_in *ret4 = (struct sockaddr_in *) retaddr;
248 ret4->sin_addr.s_addr = in4->sin_addr.s_addr;
249 } else {
250 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) &addr;
251 struct sockaddr_in6 *ret6 = (struct sockaddr_in6 *) retaddr;
252 memcpy(&ret6->sin6_addr, &in6->sin6_addr,
253 sizeof(in6->sin6_addr));
254 }
255
256 return 0;
257}
258
fdda387f
PC
259/* Data available on socket or listen socket received a connect */
260static void lowcomms_data_ready(struct sock *sk, int count_unused)
261{
262 struct connection *con = sock2con(sk);
1d6e8131
PC
263 if (!test_and_set_bit(CF_READ_PENDING, &con->flags))
264 queue_work(recv_workqueue, &con->rwork);
fdda387f
PC
265}
266
267static void lowcomms_write_space(struct sock *sk)
268{
269 struct connection *con = sock2con(sk);
270
1d6e8131
PC
271 if (!test_and_set_bit(CF_WRITE_PENDING, &con->flags))
272 queue_work(send_workqueue, &con->swork);
fdda387f
PC
273}
274
275static inline void lowcomms_connect_sock(struct connection *con)
276{
1d6e8131
PC
277 if (!test_and_set_bit(CF_CONNECT_PENDING, &con->flags))
278 queue_work(send_workqueue, &con->swork);
fdda387f
PC
279}
280
281static void lowcomms_state_change(struct sock *sk)
282{
ac33d071 283 if (sk->sk_state == TCP_ESTABLISHED)
fdda387f 284 lowcomms_write_space(sk);
fdda387f
PC
285}
286
287/* Make a socket active */
288static int add_sock(struct socket *sock, struct connection *con)
289{
290 con->sock = sock;
291
292 /* Install a data_ready callback */
293 con->sock->sk->sk_data_ready = lowcomms_data_ready;
294 con->sock->sk->sk_write_space = lowcomms_write_space;
295 con->sock->sk->sk_state_change = lowcomms_state_change;
6ed7257b 296 con->sock->sk->sk_user_data = con;
fdda387f
PC
297 return 0;
298}
299
6ed7257b 300/* Add the port number to an IPv6 or 4 sockaddr and return the address
fdda387f
PC
301 length */
302static void make_sockaddr(struct sockaddr_storage *saddr, uint16_t port,
303 int *addr_len)
304{
6ed7257b 305 saddr->ss_family = dlm_local_addr[0]->ss_family;
ac33d071 306 if (saddr->ss_family == AF_INET) {
fdda387f
PC
307 struct sockaddr_in *in4_addr = (struct sockaddr_in *)saddr;
308 in4_addr->sin_port = cpu_to_be16(port);
309 *addr_len = sizeof(struct sockaddr_in);
6ed7257b 310 memset(&in4_addr->sin_zero, 0, sizeof(in4_addr->sin_zero));
ac33d071 311 } else {
fdda387f
PC
312 struct sockaddr_in6 *in6_addr = (struct sockaddr_in6 *)saddr;
313 in6_addr->sin6_port = cpu_to_be16(port);
314 *addr_len = sizeof(struct sockaddr_in6);
315 }
316}
317
318/* Close a remote connection and tidy up */
ac33d071 319static void close_connection(struct connection *con, bool and_other)
fdda387f 320{
f1f1c1cc 321 mutex_lock(&con->sock_mutex);
fdda387f
PC
322
323 if (con->sock) {
324 sock_release(con->sock);
325 con->sock = NULL;
326 }
327 if (con->othercon && and_other) {
ac33d071
PC
328 /* Will only re-enter once. */
329 close_connection(con->othercon, false);
fdda387f
PC
330 }
331 if (con->rx_page) {
332 __free_page(con->rx_page);
333 con->rx_page = NULL;
334 }
335 con->retries = 0;
f1f1c1cc 336 mutex_unlock(&con->sock_mutex);
fdda387f
PC
337}
338
6ed7257b
PC
339/* We only send shutdown messages to nodes that are not part of the cluster */
340static void sctp_send_shutdown(sctp_assoc_t associd)
341{
342 static char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
343 struct msghdr outmessage;
344 struct cmsghdr *cmsg;
345 struct sctp_sndrcvinfo *sinfo;
346 int ret;
347 struct connection *con;
348
349 con = nodeid2con(0,0);
350 BUG_ON(con == NULL);
351
352 outmessage.msg_name = NULL;
353 outmessage.msg_namelen = 0;
354 outmessage.msg_control = outcmsg;
355 outmessage.msg_controllen = sizeof(outcmsg);
356 outmessage.msg_flags = MSG_EOR;
357
358 cmsg = CMSG_FIRSTHDR(&outmessage);
359 cmsg->cmsg_level = IPPROTO_SCTP;
360 cmsg->cmsg_type = SCTP_SNDRCV;
361 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
362 outmessage.msg_controllen = cmsg->cmsg_len;
363 sinfo = CMSG_DATA(cmsg);
364 memset(sinfo, 0x00, sizeof(struct sctp_sndrcvinfo));
365
366 sinfo->sinfo_flags |= MSG_EOF;
367 sinfo->sinfo_assoc_id = associd;
368
369 ret = kernel_sendmsg(con->sock, &outmessage, NULL, 0, 0);
370
371 if (ret != 0)
372 log_print("send EOF to node failed: %d", ret);
373}
374
375/* INIT failed but we don't know which node...
376 restart INIT on all pending nodes */
377static void sctp_init_failed(void)
378{
379 int i;
380 struct connection *con;
381
382 down(&connections_lock);
383 for (i=1; i<=max_nodeid; i++) {
384 con = __nodeid2con(i, 0);
385 if (!con)
386 continue;
387 con->sctp_assoc = 0;
388 if (test_and_clear_bit(CF_CONNECT_PENDING, &con->flags)) {
389 if (!test_and_set_bit(CF_WRITE_PENDING, &con->flags)) {
390 queue_work(send_workqueue, &con->swork);
391 }
392 }
393 }
394 up(&connections_lock);
395}
396
397/* Something happened to an association */
617e82e1
DT
398static void process_sctp_notification(struct connection *con,
399 struct msghdr *msg, char *buf)
6ed7257b
PC
400{
401 union sctp_notification *sn = (union sctp_notification *)buf;
402
403 if (sn->sn_header.sn_type == SCTP_ASSOC_CHANGE) {
404 switch (sn->sn_assoc_change.sac_state) {
405
406 case SCTP_COMM_UP:
407 case SCTP_RESTART:
408 {
409 /* Check that the new node is in the lockspace */
410 struct sctp_prim prim;
411 int nodeid;
412 int prim_len, ret;
413 int addr_len;
414 struct connection *new_con;
415 struct file *file;
416 sctp_peeloff_arg_t parg;
417 int parglen = sizeof(parg);
418
419 /*
420 * We get this before any data for an association.
421 * We verify that the node is in the cluster and
422 * then peel off a socket for it.
423 */
424 if ((int)sn->sn_assoc_change.sac_assoc_id <= 0) {
425 log_print("COMM_UP for invalid assoc ID %d",
617e82e1 426 (int)sn->sn_assoc_change.sac_assoc_id);
6ed7257b
PC
427 sctp_init_failed();
428 return;
429 }
430 memset(&prim, 0, sizeof(struct sctp_prim));
431 prim_len = sizeof(struct sctp_prim);
432 prim.ssp_assoc_id = sn->sn_assoc_change.sac_assoc_id;
433
434 ret = kernel_getsockopt(con->sock,
435 IPPROTO_SCTP,
436 SCTP_PRIMARY_ADDR,
437 (char*)&prim,
438 &prim_len);
439 if (ret < 0) {
440 log_print("getsockopt/sctp_primary_addr on "
441 "new assoc %d failed : %d",
442 (int)sn->sn_assoc_change.sac_assoc_id,
443 ret);
444
445 /* Retry INIT later */
446 new_con = assoc2con(sn->sn_assoc_change.sac_assoc_id);
447 if (new_con)
448 clear_bit(CF_CONNECT_PENDING, &con->flags);
449 return;
450 }
451 make_sockaddr(&prim.ssp_addr, 0, &addr_len);
452 if (dlm_addr_to_nodeid(&prim.ssp_addr, &nodeid)) {
453 int i;
454 unsigned char *b=(unsigned char *)&prim.ssp_addr;
455 log_print("reject connect from unknown addr");
456 for (i=0; i<sizeof(struct sockaddr_storage);i++)
457 printk("%02x ", b[i]);
458 printk("\n");
459 sctp_send_shutdown(prim.ssp_assoc_id);
460 return;
461 }
462
463 new_con = nodeid2con(nodeid, GFP_KERNEL);
464 if (!new_con)
465 return;
466
467 /* Peel off a new sock */
468 parg.associd = sn->sn_assoc_change.sac_assoc_id;
617e82e1
DT
469 ret = kernel_getsockopt(con->sock, IPPROTO_SCTP,
470 SCTP_SOCKOPT_PEELOFF,
6ed7257b 471 (void *)&parg, &parglen);
30d3a237 472 if (ret) {
617e82e1
DT
473 log_print("Can't peel off a socket for "
474 "connection %d to node %d: err=%d\n",
6ed7257b 475 parg.associd, nodeid, ret);
6ed7257b 476 }
6ed7257b
PC
477 file = fget(parg.sd);
478 new_con->sock = SOCKET_I(file->f_dentry->d_inode);
479 add_sock(new_con->sock, new_con);
480 fput(file);
481 put_unused_fd(parg.sd);
482
483 log_print("got new/restarted association %d nodeid %d",
617e82e1 484 (int)sn->sn_assoc_change.sac_assoc_id, nodeid);
6ed7257b
PC
485
486 /* Send any pending writes */
487 clear_bit(CF_CONNECT_PENDING, &new_con->flags);
488 clear_bit(CF_INIT_PENDING, &con->flags);
489 if (!test_and_set_bit(CF_WRITE_PENDING, &new_con->flags)) {
490 queue_work(send_workqueue, &new_con->swork);
491 }
492 if (!test_and_set_bit(CF_READ_PENDING, &new_con->flags))
493 queue_work(recv_workqueue, &new_con->rwork);
494 }
495 break;
496
497 case SCTP_COMM_LOST:
498 case SCTP_SHUTDOWN_COMP:
499 {
500 con = assoc2con(sn->sn_assoc_change.sac_assoc_id);
501 if (con) {
502 con->sctp_assoc = 0;
503 }
504 }
505 break;
506
507 /* We don't know which INIT failed, so clear the PENDING flags
508 * on them all. if assoc_id is zero then it will then try
509 * again */
510
511 case SCTP_CANT_STR_ASSOC:
512 {
513 log_print("Can't start SCTP association - retrying");
514 sctp_init_failed();
515 }
516 break;
517
518 default:
519 log_print("unexpected SCTP assoc change id=%d state=%d",
520 (int)sn->sn_assoc_change.sac_assoc_id,
521 sn->sn_assoc_change.sac_state);
522 }
523 }
524}
525
fdda387f
PC
526/* Data received from remote end */
527static int receive_from_sock(struct connection *con)
528{
529 int ret = 0;
58addbff
AV
530 struct msghdr msg = {};
531 struct kvec iov[2];
fdda387f
PC
532 unsigned len;
533 int r;
534 int call_again_soon = 0;
58addbff 535 int nvec;
6ed7257b 536 char incmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
fdda387f 537
f1f1c1cc 538 mutex_lock(&con->sock_mutex);
fdda387f 539
a34fbc63
PC
540 if (con->sock == NULL) {
541 ret = -EAGAIN;
542 goto out_close;
543 }
544
fdda387f
PC
545 if (con->rx_page == NULL) {
546 /*
547 * This doesn't need to be atomic, but I think it should
548 * improve performance if it is.
549 */
550 con->rx_page = alloc_page(GFP_ATOMIC);
551 if (con->rx_page == NULL)
552 goto out_resched;
ac33d071 553 cbuf_init(&con->cb, PAGE_CACHE_SIZE);
fdda387f
PC
554 }
555
6ed7257b
PC
556 /* Only SCTP needs these really */
557 memset(&incmsg, 0, sizeof(incmsg));
558 msg.msg_control = incmsg;
559 msg.msg_controllen = sizeof(incmsg);
560
fdda387f
PC
561 /*
562 * iov[0] is the bit of the circular buffer between the current end
563 * point (cb.base + cb.len) and the end of the buffer.
564 */
ac33d071
PC
565 iov[0].iov_len = con->cb.base - cbuf_data(&con->cb);
566 iov[0].iov_base = page_address(con->rx_page) + cbuf_data(&con->cb);
89adc934 567 iov[1].iov_len = 0;
58addbff 568 nvec = 1;
fdda387f
PC
569
570 /*
571 * iov[1] is the bit of the circular buffer between the start of the
572 * buffer and the start of the currently used section (cb.base)
573 */
ac33d071
PC
574 if (cbuf_data(&con->cb) >= con->cb.base) {
575 iov[0].iov_len = PAGE_CACHE_SIZE - cbuf_data(&con->cb);
fdda387f
PC
576 iov[1].iov_len = con->cb.base;
577 iov[1].iov_base = page_address(con->rx_page);
58addbff 578 nvec = 2;
fdda387f
PC
579 }
580 len = iov[0].iov_len + iov[1].iov_len;
581
58addbff 582 r = ret = kernel_recvmsg(con->sock, &msg, iov, nvec, len,
fdda387f 583 MSG_DONTWAIT | MSG_NOSIGNAL);
fdda387f
PC
584 if (ret <= 0)
585 goto out_close;
bd44e2b0 586
6ed7257b
PC
587 /* Process SCTP notifications */
588 if (msg.msg_flags & MSG_NOTIFICATION) {
6ed7257b
PC
589 msg.msg_control = incmsg;
590 msg.msg_controllen = sizeof(incmsg);
591
592 process_sctp_notification(con, &msg,
617e82e1 593 page_address(con->rx_page) + con->cb.base);
6ed7257b
PC
594 mutex_unlock(&con->sock_mutex);
595 return 0;
596 }
597 BUG_ON(con->nodeid == 0);
598
fdda387f
PC
599 if (ret == len)
600 call_again_soon = 1;
ac33d071 601 cbuf_add(&con->cb, ret);
fdda387f
PC
602 ret = dlm_process_incoming_buffer(con->nodeid,
603 page_address(con->rx_page),
604 con->cb.base, con->cb.len,
605 PAGE_CACHE_SIZE);
606 if (ret == -EBADMSG) {
617e82e1
DT
607 log_print("lowcomms: addr=%p, base=%u, len=%u, "
608 "iov_len=%u, iov_base[0]=%p, read=%d",
609 page_address(con->rx_page), con->cb.base, con->cb.len,
610 len, iov[0].iov_base, r);
fdda387f
PC
611 }
612 if (ret < 0)
613 goto out_close;
ac33d071 614 cbuf_eat(&con->cb, ret);
fdda387f 615
ac33d071 616 if (cbuf_empty(&con->cb) && !call_again_soon) {
fdda387f
PC
617 __free_page(con->rx_page);
618 con->rx_page = NULL;
619 }
620
fdda387f
PC
621 if (call_again_soon)
622 goto out_resched;
f1f1c1cc 623 mutex_unlock(&con->sock_mutex);
ac33d071 624 return 0;
fdda387f 625
ac33d071 626out_resched:
1d6e8131
PC
627 if (!test_and_set_bit(CF_READ_PENDING, &con->flags))
628 queue_work(recv_workqueue, &con->rwork);
f1f1c1cc 629 mutex_unlock(&con->sock_mutex);
bd44e2b0 630 return -EAGAIN;
fdda387f 631
ac33d071 632out_close:
f1f1c1cc 633 mutex_unlock(&con->sock_mutex);
fdda387f 634 if (ret != -EAGAIN && !test_bit(CF_IS_OTHERCON, &con->flags)) {
ac33d071 635 close_connection(con, false);
fdda387f
PC
636 /* Reconnect when there is something to send */
637 }
a34fbc63
PC
638 /* Don't return success if we really got EOF */
639 if (ret == 0)
640 ret = -EAGAIN;
fdda387f 641
fdda387f
PC
642 return ret;
643}
644
645/* Listening socket is busy, accept a connection */
6ed7257b 646static int tcp_accept_from_sock(struct connection *con)
fdda387f
PC
647{
648 int result;
649 struct sockaddr_storage peeraddr;
650 struct socket *newsock;
651 int len;
652 int nodeid;
653 struct connection *newcon;
bd44e2b0 654 struct connection *addcon;
fdda387f
PC
655
656 memset(&peeraddr, 0, sizeof(peeraddr));
6ed7257b 657 result = sock_create_kern(dlm_local_addr[0]->ss_family, SOCK_STREAM,
ac33d071 658 IPPROTO_TCP, &newsock);
fdda387f
PC
659 if (result < 0)
660 return -ENOMEM;
661
f1f1c1cc 662 mutex_lock_nested(&con->sock_mutex, 0);
fdda387f
PC
663
664 result = -ENOTCONN;
665 if (con->sock == NULL)
666 goto accept_err;
667
668 newsock->type = con->sock->type;
669 newsock->ops = con->sock->ops;
670
671 result = con->sock->ops->accept(con->sock, newsock, O_NONBLOCK);
672 if (result < 0)
673 goto accept_err;
674
675 /* Get the connected socket's peer */
676 memset(&peeraddr, 0, sizeof(peeraddr));
677 if (newsock->ops->getname(newsock, (struct sockaddr *)&peeraddr,
678 &len, 2)) {
679 result = -ECONNABORTED;
680 goto accept_err;
681 }
682
683 /* Get the new node's NODEID */
684 make_sockaddr(&peeraddr, 0, &len);
685 if (dlm_addr_to_nodeid(&peeraddr, &nodeid)) {
617e82e1 686 log_print("connect from non cluster node");
fdda387f 687 sock_release(newsock);
f1f1c1cc 688 mutex_unlock(&con->sock_mutex);
fdda387f
PC
689 return -1;
690 }
691
692 log_print("got connection from %d", nodeid);
693
694 /* Check to see if we already have a connection to this node. This
695 * could happen if the two nodes initiate a connection at roughly
696 * the same time and the connections cross on the wire.
fdda387f
PC
697 * In this case we store the incoming one in "othercon"
698 */
699 newcon = nodeid2con(nodeid, GFP_KERNEL);
700 if (!newcon) {
701 result = -ENOMEM;
702 goto accept_err;
703 }
f1f1c1cc 704 mutex_lock_nested(&newcon->sock_mutex, 1);
fdda387f 705 if (newcon->sock) {
ac33d071 706 struct connection *othercon = newcon->othercon;
fdda387f
PC
707
708 if (!othercon) {
ac33d071 709 othercon = kmem_cache_zalloc(con_cache, GFP_KERNEL);
fdda387f 710 if (!othercon) {
617e82e1 711 log_print("failed to allocate incoming socket");
f1f1c1cc 712 mutex_unlock(&newcon->sock_mutex);
fdda387f
PC
713 result = -ENOMEM;
714 goto accept_err;
715 }
fdda387f
PC
716 othercon->nodeid = nodeid;
717 othercon->rx_action = receive_from_sock;
f1f1c1cc 718 mutex_init(&othercon->sock_mutex);
1d6e8131
PC
719 INIT_WORK(&othercon->swork, process_send_sockets);
720 INIT_WORK(&othercon->rwork, process_recv_sockets);
fdda387f
PC
721 set_bit(CF_IS_OTHERCON, &othercon->flags);
722 newcon->othercon = othercon;
723 }
724 othercon->sock = newsock;
725 newsock->sk->sk_user_data = othercon;
726 add_sock(newsock, othercon);
bd44e2b0 727 addcon = othercon;
fdda387f
PC
728 }
729 else {
730 newsock->sk->sk_user_data = newcon;
731 newcon->rx_action = receive_from_sock;
732 add_sock(newsock, newcon);
bd44e2b0 733 addcon = newcon;
fdda387f
PC
734 }
735
f1f1c1cc 736 mutex_unlock(&newcon->sock_mutex);
fdda387f
PC
737
738 /*
739 * Add it to the active queue in case we got data
740 * beween processing the accept adding the socket
741 * to the read_sockets list
742 */
bd44e2b0
PC
743 if (!test_and_set_bit(CF_READ_PENDING, &addcon->flags))
744 queue_work(recv_workqueue, &addcon->rwork);
f1f1c1cc 745 mutex_unlock(&con->sock_mutex);
fdda387f
PC
746
747 return 0;
748
ac33d071 749accept_err:
f1f1c1cc 750 mutex_unlock(&con->sock_mutex);
fdda387f
PC
751 sock_release(newsock);
752
753 if (result != -EAGAIN)
617e82e1 754 log_print("error accepting connection from node: %d", result);
fdda387f
PC
755 return result;
756}
757
6ed7257b
PC
758static void free_entry(struct writequeue_entry *e)
759{
760 __free_page(e->page);
761 kfree(e);
762}
763
764/* Initiate an SCTP association.
765 This is a special case of send_to_sock() in that we don't yet have a
766 peeled-off socket for this association, so we use the listening socket
767 and add the primary IP address of the remote node.
768 */
769static void sctp_init_assoc(struct connection *con)
770{
771 struct sockaddr_storage rem_addr;
772 char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
773 struct msghdr outmessage;
774 struct cmsghdr *cmsg;
775 struct sctp_sndrcvinfo *sinfo;
776 struct connection *base_con;
777 struct writequeue_entry *e;
778 int len, offset;
779 int ret;
780 int addrlen;
781 struct kvec iov[1];
782
783 if (test_and_set_bit(CF_INIT_PENDING, &con->flags))
784 return;
785
786 if (con->retries++ > MAX_CONNECT_RETRIES)
787 return;
788
789 log_print("Initiating association with node %d", con->nodeid);
790
791 if (nodeid_to_addr(con->nodeid, (struct sockaddr *)&rem_addr)) {
792 log_print("no address for nodeid %d", con->nodeid);
793 return;
794 }
795 base_con = nodeid2con(0, 0);
796 BUG_ON(base_con == NULL);
797
798 make_sockaddr(&rem_addr, dlm_config.ci_tcp_port, &addrlen);
799
800 outmessage.msg_name = &rem_addr;
801 outmessage.msg_namelen = addrlen;
802 outmessage.msg_control = outcmsg;
803 outmessage.msg_controllen = sizeof(outcmsg);
804 outmessage.msg_flags = MSG_EOR;
805
806 spin_lock(&con->writequeue_lock);
807 e = list_entry(con->writequeue.next, struct writequeue_entry,
808 list);
809
810 BUG_ON((struct list_head *) e == &con->writequeue);
811
812 len = e->len;
813 offset = e->offset;
814 spin_unlock(&con->writequeue_lock);
815 kmap(e->page);
816
817 /* Send the first block off the write queue */
818 iov[0].iov_base = page_address(e->page)+offset;
819 iov[0].iov_len = len;
820
821 cmsg = CMSG_FIRSTHDR(&outmessage);
822 cmsg->cmsg_level = IPPROTO_SCTP;
823 cmsg->cmsg_type = SCTP_SNDRCV;
824 cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
825 sinfo = CMSG_DATA(cmsg);
826 memset(sinfo, 0x00, sizeof(struct sctp_sndrcvinfo));
827 sinfo->sinfo_ppid = cpu_to_le32(dlm_our_nodeid());
828 outmessage.msg_controllen = cmsg->cmsg_len;
829
830 ret = kernel_sendmsg(base_con->sock, &outmessage, iov, 1, len);
831 if (ret < 0) {
617e82e1
DT
832 log_print("Send first packet to node %d failed: %d",
833 con->nodeid, ret);
6ed7257b
PC
834
835 /* Try again later */
836 clear_bit(CF_CONNECT_PENDING, &con->flags);
837 clear_bit(CF_INIT_PENDING, &con->flags);
838 }
839 else {
840 spin_lock(&con->writequeue_lock);
841 e->offset += ret;
842 e->len -= ret;
843
844 if (e->len == 0 && e->users == 0) {
845 list_del(&e->list);
846 kunmap(e->page);
847 free_entry(e);
848 }
849 spin_unlock(&con->writequeue_lock);
850 }
851}
852
fdda387f 853/* Connect a new socket to its peer */
6ed7257b 854static void tcp_connect_to_sock(struct connection *con)
fdda387f
PC
855{
856 int result = -EHOSTUNREACH;
857 struct sockaddr_storage saddr;
858 int addr_len;
859 struct socket *sock;
860
861 if (con->nodeid == 0) {
862 log_print("attempt to connect sock 0 foiled");
ac33d071 863 return;
fdda387f
PC
864 }
865
f1f1c1cc 866 mutex_lock(&con->sock_mutex);
fdda387f
PC
867 if (con->retries++ > MAX_CONNECT_RETRIES)
868 goto out;
869
870 /* Some odd races can cause double-connects, ignore them */
871 if (con->sock) {
872 result = 0;
873 goto out;
874 }
875
876 /* Create a socket to communicate with */
6ed7257b 877 result = sock_create_kern(dlm_local_addr[0]->ss_family, SOCK_STREAM,
ac33d071 878 IPPROTO_TCP, &sock);
fdda387f
PC
879 if (result < 0)
880 goto out_err;
881
882 memset(&saddr, 0, sizeof(saddr));
883 if (dlm_nodeid_to_addr(con->nodeid, &saddr))
ac33d071 884 goto out_err;
fdda387f
PC
885
886 sock->sk->sk_user_data = con;
887 con->rx_action = receive_from_sock;
6ed7257b
PC
888 con->connect_action = tcp_connect_to_sock;
889 add_sock(sock, con);
fdda387f 890
68c817a1 891 make_sockaddr(&saddr, dlm_config.ci_tcp_port, &addr_len);
fdda387f 892
fdda387f
PC
893 log_print("connecting to %d", con->nodeid);
894 result =
895 sock->ops->connect(sock, (struct sockaddr *)&saddr, addr_len,
ac33d071 896 O_NONBLOCK);
fdda387f
PC
897 if (result == -EINPROGRESS)
898 result = 0;
ac33d071
PC
899 if (result == 0)
900 goto out;
fdda387f 901
ac33d071 902out_err:
fdda387f
PC
903 if (con->sock) {
904 sock_release(con->sock);
905 con->sock = NULL;
906 }
907 /*
908 * Some errors are fatal and this list might need adjusting. For other
909 * errors we try again until the max number of retries is reached.
910 */
911 if (result != -EHOSTUNREACH && result != -ENETUNREACH &&
912 result != -ENETDOWN && result != EINVAL
913 && result != -EPROTONOSUPPORT) {
914 lowcomms_connect_sock(con);
915 result = 0;
916 }
ac33d071 917out:
f1f1c1cc 918 mutex_unlock(&con->sock_mutex);
ac33d071 919 return;
fdda387f
PC
920}
921
6ed7257b
PC
922static struct socket *tcp_create_listen_sock(struct connection *con,
923 struct sockaddr_storage *saddr)
fdda387f 924{
ac33d071 925 struct socket *sock = NULL;
fdda387f
PC
926 int result = 0;
927 int one = 1;
928 int addr_len;
929
6ed7257b 930 if (dlm_local_addr[0]->ss_family == AF_INET)
fdda387f
PC
931 addr_len = sizeof(struct sockaddr_in);
932 else
933 addr_len = sizeof(struct sockaddr_in6);
934
935 /* Create a socket to communicate with */
617e82e1
DT
936 result = sock_create_kern(dlm_local_addr[0]->ss_family, SOCK_STREAM,
937 IPPROTO_TCP, &sock);
fdda387f 938 if (result < 0) {
617e82e1 939 log_print("Can't create listening comms socket");
fdda387f
PC
940 goto create_out;
941 }
942
6ed7257b
PC
943 result = kernel_setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
944 (char *)&one, sizeof(one));
945
fdda387f 946 if (result < 0) {
617e82e1 947 log_print("Failed to set SO_REUSEADDR on socket: %d", result);
fdda387f
PC
948 }
949 sock->sk->sk_user_data = con;
6ed7257b
PC
950 con->rx_action = tcp_accept_from_sock;
951 con->connect_action = tcp_connect_to_sock;
fdda387f
PC
952 con->sock = sock;
953
954 /* Bind to our port */
68c817a1 955 make_sockaddr(saddr, dlm_config.ci_tcp_port, &addr_len);
fdda387f
PC
956 result = sock->ops->bind(sock, (struct sockaddr *) saddr, addr_len);
957 if (result < 0) {
617e82e1 958 log_print("Can't bind to port %d", dlm_config.ci_tcp_port);
fdda387f
PC
959 sock_release(sock);
960 sock = NULL;
961 con->sock = NULL;
962 goto create_out;
963 }
6ed7257b 964 result = kernel_setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE,
ac33d071 965 (char *)&one, sizeof(one));
fdda387f 966 if (result < 0) {
617e82e1 967 log_print("Set keepalive failed: %d", result);
fdda387f
PC
968 }
969
970 result = sock->ops->listen(sock, 5);
971 if (result < 0) {
617e82e1 972 log_print("Can't listen on port %d", dlm_config.ci_tcp_port);
fdda387f
PC
973 sock_release(sock);
974 sock = NULL;
975 goto create_out;
976 }
977
ac33d071 978create_out:
fdda387f
PC
979 return sock;
980}
981
6ed7257b
PC
982/* Get local addresses */
983static void init_local(void)
984{
985 struct sockaddr_storage sas, *addr;
986 int i;
987
30d3a237 988 dlm_local_count = 0;
6ed7257b
PC
989 for (i = 0; i < DLM_MAX_ADDR_COUNT - 1; i++) {
990 if (dlm_our_addr(&sas, i))
991 break;
992
993 addr = kmalloc(sizeof(*addr), GFP_KERNEL);
994 if (!addr)
995 break;
996 memcpy(addr, &sas, sizeof(*addr));
997 dlm_local_addr[dlm_local_count++] = addr;
998 }
999}
1000
617e82e1
DT
1001/* Bind to an IP address. SCTP allows multiple address so it can do
1002 multi-homing */
1003static int add_sctp_bind_addr(struct connection *sctp_con,
1004 struct sockaddr_storage *addr,
1005 int addr_len, int num)
6ed7257b
PC
1006{
1007 int result = 0;
1008
1009 if (num == 1)
1010 result = kernel_bind(sctp_con->sock,
1011 (struct sockaddr *) addr,
1012 addr_len);
1013 else
1014 result = kernel_setsockopt(sctp_con->sock, SOL_SCTP,
1015 SCTP_SOCKOPT_BINDX_ADD,
1016 (char *)addr, addr_len);
1017
1018 if (result < 0)
1019 log_print("Can't bind to port %d addr number %d",
1020 dlm_config.ci_tcp_port, num);
1021
1022 return result;
1023}
fdda387f 1024
6ed7257b
PC
1025/* Initialise SCTP socket and bind to all interfaces */
1026static int sctp_listen_for_all(void)
1027{
1028 struct socket *sock = NULL;
1029 struct sockaddr_storage localaddr;
1030 struct sctp_event_subscribe subscribe;
1031 int result = -EINVAL, num = 1, i, addr_len;
1032 struct connection *con = nodeid2con(0, GFP_KERNEL);
1033 int bufsize = NEEDED_RMEM;
1034
1035 if (!con)
1036 return -ENOMEM;
1037
1038 log_print("Using SCTP for communications");
1039
1040 result = sock_create_kern(dlm_local_addr[0]->ss_family, SOCK_SEQPACKET,
1041 IPPROTO_SCTP, &sock);
1042 if (result < 0) {
1043 log_print("Can't create comms socket, check SCTP is loaded");
1044 goto out;
1045 }
1046
1047 /* Listen for events */
1048 memset(&subscribe, 0, sizeof(subscribe));
1049 subscribe.sctp_data_io_event = 1;
1050 subscribe.sctp_association_event = 1;
1051 subscribe.sctp_send_failure_event = 1;
1052 subscribe.sctp_shutdown_event = 1;
1053 subscribe.sctp_partial_delivery_event = 1;
1054
1055 result = kernel_setsockopt(sock, SOL_SOCKET, SO_RCVBUF,
1056 (char *)&bufsize, sizeof(bufsize));
1057 if (result)
617e82e1 1058 log_print("Error increasing buffer space on socket %d", result);
6ed7257b
PC
1059
1060 result = kernel_setsockopt(sock, SOL_SCTP, SCTP_EVENTS,
617e82e1 1061 (char *)&subscribe, sizeof(subscribe));
6ed7257b
PC
1062 if (result < 0) {
1063 log_print("Failed to set SCTP_EVENTS on socket: result=%d",
1064 result);
1065 goto create_delsock;
1066 }
1067
1068 /* Init con struct */
1069 sock->sk->sk_user_data = con;
1070 con->sock = sock;
1071 con->sock->sk->sk_data_ready = lowcomms_data_ready;
1072 con->rx_action = receive_from_sock;
1073 con->connect_action = sctp_init_assoc;
1074
1075 /* Bind to all interfaces. */
1076 for (i = 0; i < dlm_local_count; i++) {
1077 memcpy(&localaddr, dlm_local_addr[i], sizeof(localaddr));
1078 make_sockaddr(&localaddr, dlm_config.ci_tcp_port, &addr_len);
1079
1080 result = add_sctp_bind_addr(con, &localaddr, addr_len, num);
1081 if (result)
1082 goto create_delsock;
1083 ++num;
1084 }
1085
1086 result = sock->ops->listen(sock, 5);
1087 if (result < 0) {
1088 log_print("Can't set socket listening");
1089 goto create_delsock;
1090 }
1091
1092 return 0;
1093
1094create_delsock:
1095 sock_release(sock);
1096 con->sock = NULL;
1097out:
1098 return result;
1099}
1100
1101static int tcp_listen_for_all(void)
fdda387f
PC
1102{
1103 struct socket *sock = NULL;
1104 struct connection *con = nodeid2con(0, GFP_KERNEL);
1105 int result = -EINVAL;
1106
6ed7257b
PC
1107 if (!con)
1108 return -ENOMEM;
1109
fdda387f 1110 /* We don't support multi-homed hosts */
6ed7257b 1111 if (dlm_local_addr[1] != NULL) {
617e82e1
DT
1112 log_print("TCP protocol can't handle multi-homed hosts, "
1113 "try SCTP");
6ed7257b
PC
1114 return -EINVAL;
1115 }
1116
1117 log_print("Using TCP for communications");
1118
fdda387f
PC
1119 set_bit(CF_IS_OTHERCON, &con->flags);
1120
6ed7257b 1121 sock = tcp_create_listen_sock(con, dlm_local_addr[0]);
fdda387f
PC
1122 if (sock) {
1123 add_sock(sock, con);
1124 result = 0;
1125 }
1126 else {
1127 result = -EADDRINUSE;
1128 }
1129
1130 return result;
1131}
1132
1133
1134
1135static struct writequeue_entry *new_writequeue_entry(struct connection *con,
1136 gfp_t allocation)
1137{
1138 struct writequeue_entry *entry;
1139
1140 entry = kmalloc(sizeof(struct writequeue_entry), allocation);
1141 if (!entry)
1142 return NULL;
1143
1144 entry->page = alloc_page(allocation);
1145 if (!entry->page) {
1146 kfree(entry);
1147 return NULL;
1148 }
1149
1150 entry->offset = 0;
1151 entry->len = 0;
1152 entry->end = 0;
1153 entry->users = 0;
1154 entry->con = con;
1155
1156 return entry;
1157}
1158
617e82e1 1159void *dlm_lowcomms_get_buffer(int nodeid, int len, gfp_t allocation, char **ppc)
fdda387f
PC
1160{
1161 struct connection *con;
1162 struct writequeue_entry *e;
1163 int offset = 0;
1164 int users = 0;
1165
fdda387f
PC
1166 con = nodeid2con(nodeid, allocation);
1167 if (!con)
1168 return NULL;
1169
4edde74e 1170 spin_lock(&con->writequeue_lock);
fdda387f 1171 e = list_entry(con->writequeue.prev, struct writequeue_entry, list);
ac33d071 1172 if ((&e->list == &con->writequeue) ||
fdda387f
PC
1173 (PAGE_CACHE_SIZE - e->end < len)) {
1174 e = NULL;
1175 } else {
1176 offset = e->end;
1177 e->end += len;
1178 users = e->users++;
1179 }
1180 spin_unlock(&con->writequeue_lock);
1181
1182 if (e) {
ac33d071 1183 got_one:
fdda387f
PC
1184 if (users == 0)
1185 kmap(e->page);
1186 *ppc = page_address(e->page) + offset;
1187 return e;
1188 }
1189
1190 e = new_writequeue_entry(con, allocation);
1191 if (e) {
1192 spin_lock(&con->writequeue_lock);
1193 offset = e->end;
1194 e->end += len;
1195 users = e->users++;
1196 list_add_tail(&e->list, &con->writequeue);
1197 spin_unlock(&con->writequeue_lock);
1198 goto got_one;
1199 }
1200 return NULL;
1201}
1202
1203void dlm_lowcomms_commit_buffer(void *mh)
1204{
1205 struct writequeue_entry *e = (struct writequeue_entry *)mh;
1206 struct connection *con = e->con;
1207 int users;
1208
4edde74e 1209 spin_lock(&con->writequeue_lock);
fdda387f
PC
1210 users = --e->users;
1211 if (users)
1212 goto out;
1213 e->len = e->end - e->offset;
1214 kunmap(e->page);
1215 spin_unlock(&con->writequeue_lock);
1216
1d6e8131
PC
1217 if (!test_and_set_bit(CF_WRITE_PENDING, &con->flags)) {
1218 queue_work(send_workqueue, &con->swork);
fdda387f
PC
1219 }
1220 return;
1221
ac33d071 1222out:
fdda387f
PC
1223 spin_unlock(&con->writequeue_lock);
1224 return;
1225}
1226
fdda387f 1227/* Send a message */
ac33d071 1228static void send_to_sock(struct connection *con)
fdda387f
PC
1229{
1230 int ret = 0;
1231 ssize_t(*sendpage) (struct socket *, struct page *, int, size_t, int);
1232 const int msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL;
1233 struct writequeue_entry *e;
1234 int len, offset;
1235
f1f1c1cc 1236 mutex_lock(&con->sock_mutex);
fdda387f
PC
1237 if (con->sock == NULL)
1238 goto out_connect;
1239
1240 sendpage = con->sock->ops->sendpage;
1241
1242 spin_lock(&con->writequeue_lock);
1243 for (;;) {
1244 e = list_entry(con->writequeue.next, struct writequeue_entry,
1245 list);
1246 if ((struct list_head *) e == &con->writequeue)
1247 break;
1248
1249 len = e->len;
1250 offset = e->offset;
1251 BUG_ON(len == 0 && e->users == 0);
1252 spin_unlock(&con->writequeue_lock);
1d6e8131 1253 kmap(e->page);
fdda387f
PC
1254
1255 ret = 0;
1256 if (len) {
1257 ret = sendpage(con->sock, e->page, offset, len,
1258 msg_flags);
1259 if (ret == -EAGAIN || ret == 0)
1260 goto out;
1261 if (ret <= 0)
1262 goto send_error;
617e82e1 1263 } else {
fdda387f 1264 /* Don't starve people filling buffers */
ac33d071 1265 cond_resched();
fdda387f
PC
1266 }
1267
1268 spin_lock(&con->writequeue_lock);
1269 e->offset += ret;
1270 e->len -= ret;
1271
1272 if (e->len == 0 && e->users == 0) {
1273 list_del(&e->list);
ac33d071 1274 kunmap(e->page);
fdda387f
PC
1275 free_entry(e);
1276 continue;
1277 }
1278 }
1279 spin_unlock(&con->writequeue_lock);
ac33d071 1280out:
f1f1c1cc 1281 mutex_unlock(&con->sock_mutex);
ac33d071 1282 return;
fdda387f 1283
ac33d071 1284send_error:
f1f1c1cc 1285 mutex_unlock(&con->sock_mutex);
ac33d071 1286 close_connection(con, false);
fdda387f 1287 lowcomms_connect_sock(con);
ac33d071 1288 return;
fdda387f 1289
ac33d071 1290out_connect:
f1f1c1cc 1291 mutex_unlock(&con->sock_mutex);
6ed7257b
PC
1292 if (!test_bit(CF_INIT_PENDING, &con->flags))
1293 lowcomms_connect_sock(con);
ac33d071 1294 return;
fdda387f
PC
1295}
1296
1297static void clean_one_writequeue(struct connection *con)
1298{
1299 struct list_head *list;
1300 struct list_head *temp;
1301
1302 spin_lock(&con->writequeue_lock);
1303 list_for_each_safe(list, temp, &con->writequeue) {
1304 struct writequeue_entry *e =
1305 list_entry(list, struct writequeue_entry, list);
1306 list_del(&e->list);
1307 free_entry(e);
1308 }
1309 spin_unlock(&con->writequeue_lock);
1310}
1311
1312/* Called from recovery when it knows that a node has
1313 left the cluster */
1314int dlm_lowcomms_close(int nodeid)
1315{
1316 struct connection *con;
1317
fdda387f
PC
1318 log_print("closing connection to node %d", nodeid);
1319 con = nodeid2con(nodeid, 0);
1320 if (con) {
1321 clean_one_writequeue(con);
ac33d071 1322 close_connection(con, true);
fdda387f
PC
1323 }
1324 return 0;
fdda387f
PC
1325}
1326
6ed7257b 1327/* Receive workqueue function */
1d6e8131 1328static void process_recv_sockets(struct work_struct *work)
fdda387f 1329{
1d6e8131
PC
1330 struct connection *con = container_of(work, struct connection, rwork);
1331 int err;
fdda387f 1332
1d6e8131
PC
1333 clear_bit(CF_READ_PENDING, &con->flags);
1334 do {
1335 err = con->rx_action(con);
1336 } while (!err);
fdda387f
PC
1337}
1338
6ed7257b 1339/* Send workqueue function */
1d6e8131 1340static void process_send_sockets(struct work_struct *work)
fdda387f 1341{
1d6e8131 1342 struct connection *con = container_of(work, struct connection, swork);
fdda387f 1343
1d6e8131 1344 if (test_and_clear_bit(CF_CONNECT_PENDING, &con->flags)) {
6ed7257b 1345 con->connect_action(con);
fdda387f 1346 }
bd44e2b0
PC
1347 clear_bit(CF_WRITE_PENDING, &con->flags);
1348 send_to_sock(con);
fdda387f
PC
1349}
1350
1351
1352/* Discard all entries on the write queues */
1353static void clean_writequeues(void)
1354{
1355 int nodeid;
1356
30d3a237
PC
1357 for (nodeid = 1; nodeid <= max_nodeid; nodeid++) {
1358 struct connection *con = __nodeid2con(nodeid, 0);
fdda387f
PC
1359
1360 if (con)
1361 clean_one_writequeue(con);
1362 }
1363}
1364
1d6e8131 1365static void work_stop(void)
fdda387f 1366{
1d6e8131
PC
1367 destroy_workqueue(recv_workqueue);
1368 destroy_workqueue(send_workqueue);
fdda387f
PC
1369}
1370
1d6e8131 1371static int work_start(void)
fdda387f 1372{
fdda387f 1373 int error;
1d6e8131
PC
1374 recv_workqueue = create_workqueue("dlm_recv");
1375 error = IS_ERR(recv_workqueue);
ac33d071 1376 if (error) {
1d6e8131 1377 log_print("can't start dlm_recv %d", error);
fdda387f
PC
1378 return error;
1379 }
fdda387f 1380
1d6e8131
PC
1381 send_workqueue = create_singlethread_workqueue("dlm_send");
1382 error = IS_ERR(send_workqueue);
ac33d071 1383 if (error) {
1d6e8131
PC
1384 log_print("can't start dlm_send %d", error);
1385 destroy_workqueue(recv_workqueue);
fdda387f
PC
1386 return error;
1387 }
fdda387f
PC
1388
1389 return 0;
1390}
1391
fdda387f
PC
1392void dlm_lowcomms_stop(void)
1393{
1394 int i;
6ed7257b 1395 struct connection *con;
fdda387f 1396
ac33d071 1397 /* Set all the flags to prevent any
fdda387f
PC
1398 socket activity.
1399 */
6ed7257b 1400 down(&connections_lock);
30d3a237 1401 for (i = 0; i <= max_nodeid; i++) {
6ed7257b
PC
1402 con = __nodeid2con(i, 0);
1403 if (con)
1404 con->flags |= 0xFF;
fdda387f 1405 }
6ed7257b 1406 up(&connections_lock);
ac33d071 1407
1d6e8131 1408 work_stop();
6ed7257b
PC
1409
1410 down(&connections_lock);
fdda387f
PC
1411 clean_writequeues();
1412
30d3a237 1413 for (i = 0; i <= max_nodeid; i++) {
2439fe50 1414 con = __nodeid2con(i, 0);
6ed7257b
PC
1415 if (con) {
1416 close_connection(con, true);
1417 if (con->othercon)
1418 kmem_cache_free(con_cache, con->othercon);
1419 kmem_cache_free(con_cache, con);
fdda387f
PC
1420 }
1421 }
30d3a237 1422 max_nodeid = 0;
6ed7257b 1423 up(&connections_lock);
fdda387f 1424 kmem_cache_destroy(con_cache);
30d3a237 1425 idr_init(&connections_idr);
fdda387f
PC
1426}
1427
fdda387f
PC
1428int dlm_lowcomms_start(void)
1429{
6ed7257b
PC
1430 int error = -EINVAL;
1431 struct connection *con;
fdda387f 1432
6ed7257b
PC
1433 init_local();
1434 if (!dlm_local_count) {
617e82e1 1435 error = -ENOTCONN;
fdda387f 1436 log_print("no local IP address has been set");
6ed7257b 1437 goto out;
fdda387f
PC
1438 }
1439
6ed7257b 1440 error = -ENOMEM;
fdda387f 1441 con_cache = kmem_cache_create("dlm_conn", sizeof(struct connection),
ac33d071
PC
1442 __alignof__(struct connection), 0,
1443 NULL, NULL);
fdda387f 1444 if (!con_cache)
6ed7257b 1445 goto out;
fdda387f 1446
6ed7257b
PC
1447 /* Set some sysctl minima */
1448 if (sysctl_rmem_max < NEEDED_RMEM)
1449 sysctl_rmem_max = NEEDED_RMEM;
fdda387f
PC
1450
1451 /* Start listening */
6ed7257b
PC
1452 if (dlm_config.ci_protocol == 0)
1453 error = tcp_listen_for_all();
1454 else
1455 error = sctp_listen_for_all();
fdda387f
PC
1456 if (error)
1457 goto fail_unlisten;
1458
1d6e8131 1459 error = work_start();
fdda387f
PC
1460 if (error)
1461 goto fail_unlisten;
1462
fdda387f
PC
1463 return 0;
1464
ac33d071 1465fail_unlisten:
6ed7257b
PC
1466 con = nodeid2con(0,0);
1467 if (con) {
1468 close_connection(con, false);
1469 kmem_cache_free(con_cache, con);
1470 }
fdda387f
PC
1471 kmem_cache_destroy(con_cache);
1472
ac33d071 1473out:
fdda387f
PC
1474 return error;
1475}
This page took 0.17728 seconds and 5 git commands to generate.