1 // SPDX-License-Identifier: GPL-2.0-only
2 /******************************************************************************
3 *******************************************************************************
5 ** Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
6 ** Copyright (C) 2004-2009 Red Hat, Inc. All rights reserved.
9 *******************************************************************************
10 ******************************************************************************/
15 * This is the "low-level" comms layer.
17 * It is responsible for sending/receiving messages
18 * from other nodes in the cluster.
20 * Cluster nodes are referred to by their nodeids. nodeids are
21 * simply 32 bit numbers to the locking module - if they need to
22 * be expanded for the cluster infrastructure then that is its
23 * responsibility. It is this layer's
24 * responsibility to resolve these into IP address or
25 * whatever it needs for inter-node communication.
27 * The comms level is two kernel threads that deal mainly with
28 * the receiving of messages from other nodes and passing them
29 * up to the mid-level comms layer (which understands the
30 * message format) for execution by the locking core, and
31 * a send thread which does all the setting up of connections
32 * to remote nodes and the sending of data. Threads are not allowed
33 * to send their own data because it may cause them to wait in times
34 * of high load. Also, this way, the sending thread can collect together
35 * messages bound for one node and send them in one block.
37 * lowcomms will choose to use either TCP or SCTP as its transport layer
38 * depending on the configuration variable 'protocol'. This should be set
39 * to 0 (default) for TCP or 1 for SCTP. It should be configured using a
40 * cluster-wide mechanism as it must be the same on all nodes of the cluster
41 * for the DLM to function.
45 #include <asm/ioctls.h>
48 #include <linux/pagemap.h>
49 #include <linux/file.h>
50 #include <linux/mutex.h>
51 #include <linux/sctp.h>
52 #include <linux/slab.h>
53 #include <net/sctp/sctp.h>
56 #include "dlm_internal.h"
61 #define NEEDED_RMEM (4*1024*1024)
63 /* Number of messages to send before rescheduling */
64 #define MAX_SEND_MSG_COUNT 25
65 #define DLM_SHUTDOWN_WAIT_TIMEOUT msecs_to_jiffies(10000)
68 struct socket *sock; /* NULL if not connected */
69 uint32_t nodeid; /* So we know who we are in the list */
70 struct mutex sock_mutex;
72 #define CF_READ_PENDING 1
73 #define CF_WRITE_PENDING 2
74 #define CF_INIT_PENDING 4
75 #define CF_IS_OTHERCON 5
77 #define CF_APP_LIMITED 7
80 #define CF_CONNECTED 10
81 #define CF_RECONNECT 11
82 #define CF_DELAY_CONNECT 12
84 struct list_head writequeue; /* List of outgoing writequeue_entries */
85 spinlock_t writequeue_lock;
86 atomic_t writequeue_cnt;
87 void (*connect_action) (struct connection *); /* What to do to connect */
88 void (*shutdown_action)(struct connection *con); /* What to do to shutdown */
89 bool (*eof_condition)(struct connection *con); /* What to do to eof check */
91 #define MAX_CONNECT_RETRIES 3
92 struct hlist_node list;
93 struct connection *othercon;
94 struct connection *sendcon;
95 struct work_struct rwork; /* Receive workqueue */
96 struct work_struct swork; /* Send workqueue */
97 wait_queue_head_t shutdown_wait; /* wait for graceful shutdown */
98 unsigned char *rx_buf;
103 #define sock2con(x) ((struct connection *)(x)->sk_user_data)
105 struct listen_connection {
107 struct work_struct rwork;
110 #define DLM_WQ_REMAIN_BYTES(e) (PAGE_SIZE - e->end)
111 #define DLM_WQ_LENGTH_BYTES(e) (e->end - e->offset)
113 /* An entry waiting to be sent */
114 struct writequeue_entry {
115 struct list_head list;
122 struct connection *con;
123 struct list_head msgs;
128 struct writequeue_entry *entry;
129 struct dlm_msg *orig_msg;
133 int idx; /* new()/commit() idx exchange */
135 struct list_head list;
139 struct dlm_node_addr {
140 struct list_head list;
145 struct sockaddr_storage *addr[DLM_MAX_ADDR_COUNT];
148 static struct listen_sock_callbacks {
149 void (*sk_error_report)(struct sock *);
150 void (*sk_data_ready)(struct sock *);
151 void (*sk_state_change)(struct sock *);
152 void (*sk_write_space)(struct sock *);
155 static LIST_HEAD(dlm_node_addrs);
156 static DEFINE_SPINLOCK(dlm_node_addrs_spin);
158 static struct listen_connection listen_con;
159 static struct sockaddr_storage *dlm_local_addr[DLM_MAX_ADDR_COUNT];
160 static int dlm_local_count;
164 static struct workqueue_struct *recv_workqueue;
165 static struct workqueue_struct *send_workqueue;
167 static struct hlist_head connection_hash[CONN_HASH_SIZE];
168 static DEFINE_SPINLOCK(connections_lock);
169 DEFINE_STATIC_SRCU(connections_srcu);
171 static void process_recv_sockets(struct work_struct *work);
172 static void process_send_sockets(struct work_struct *work);
174 static void sctp_connect_to_sock(struct connection *con);
175 static void tcp_connect_to_sock(struct connection *con);
176 static void dlm_tcp_shutdown(struct connection *con);
178 /* need to held writequeue_lock */
179 static struct writequeue_entry *con_next_wq(struct connection *con)
181 struct writequeue_entry *e;
183 if (list_empty(&con->writequeue))
186 e = list_first_entry(&con->writequeue, struct writequeue_entry,
194 static struct connection *__find_con(int nodeid, int r)
196 struct connection *con;
198 hlist_for_each_entry_rcu(con, &connection_hash[r], list) {
199 if (con->nodeid == nodeid)
206 static bool tcp_eof_condition(struct connection *con)
208 return atomic_read(&con->writequeue_cnt);
211 static int dlm_con_init(struct connection *con, int nodeid)
213 con->rx_buflen = dlm_config.ci_buffer_size;
214 con->rx_buf = kmalloc(con->rx_buflen, GFP_NOFS);
218 con->nodeid = nodeid;
219 mutex_init(&con->sock_mutex);
220 INIT_LIST_HEAD(&con->writequeue);
221 spin_lock_init(&con->writequeue_lock);
222 atomic_set(&con->writequeue_cnt, 0);
223 INIT_WORK(&con->swork, process_send_sockets);
224 INIT_WORK(&con->rwork, process_recv_sockets);
225 init_waitqueue_head(&con->shutdown_wait);
227 switch (dlm_config.ci_protocol) {
229 con->connect_action = tcp_connect_to_sock;
230 con->shutdown_action = dlm_tcp_shutdown;
231 con->eof_condition = tcp_eof_condition;
234 con->connect_action = sctp_connect_to_sock;
245 * If 'allocation' is zero then we don't attempt to create a new
246 * connection structure for this node.
248 static struct connection *nodeid2con(int nodeid, gfp_t alloc)
250 struct connection *con, *tmp;
253 r = nodeid_hash(nodeid);
254 con = __find_con(nodeid, r);
258 con = kzalloc(sizeof(*con), alloc);
262 ret = dlm_con_init(con, nodeid);
268 spin_lock(&connections_lock);
269 /* Because multiple workqueues/threads calls this function it can
270 * race on multiple cpu's. Instead of locking hot path __find_con()
271 * we just check in rare cases of recently added nodes again
272 * under protection of connections_lock. If this is the case we
273 * abort our connection creation and return the existing connection.
275 tmp = __find_con(nodeid, r);
277 spin_unlock(&connections_lock);
283 hlist_add_head_rcu(&con->list, &connection_hash[r]);
284 spin_unlock(&connections_lock);
289 /* Loop round all connections */
290 static void foreach_conn(void (*conn_func)(struct connection *c))
293 struct connection *con;
295 for (i = 0; i < CONN_HASH_SIZE; i++) {
296 hlist_for_each_entry_rcu(con, &connection_hash[i], list)
301 static struct dlm_node_addr *find_node_addr(int nodeid)
303 struct dlm_node_addr *na;
305 list_for_each_entry(na, &dlm_node_addrs, list) {
306 if (na->nodeid == nodeid)
312 static int addr_compare(const struct sockaddr_storage *x,
313 const struct sockaddr_storage *y)
315 switch (x->ss_family) {
317 struct sockaddr_in *sinx = (struct sockaddr_in *)x;
318 struct sockaddr_in *siny = (struct sockaddr_in *)y;
319 if (sinx->sin_addr.s_addr != siny->sin_addr.s_addr)
321 if (sinx->sin_port != siny->sin_port)
326 struct sockaddr_in6 *sinx = (struct sockaddr_in6 *)x;
327 struct sockaddr_in6 *siny = (struct sockaddr_in6 *)y;
328 if (!ipv6_addr_equal(&sinx->sin6_addr, &siny->sin6_addr))
330 if (sinx->sin6_port != siny->sin6_port)
340 static int nodeid_to_addr(int nodeid, struct sockaddr_storage *sas_out,
341 struct sockaddr *sa_out, bool try_new_addr,
344 struct sockaddr_storage sas;
345 struct dlm_node_addr *na;
347 if (!dlm_local_count)
350 spin_lock(&dlm_node_addrs_spin);
351 na = find_node_addr(nodeid);
352 if (na && na->addr_count) {
353 memcpy(&sas, na->addr[na->curr_addr_index],
354 sizeof(struct sockaddr_storage));
357 na->curr_addr_index++;
358 if (na->curr_addr_index == na->addr_count)
359 na->curr_addr_index = 0;
362 spin_unlock(&dlm_node_addrs_spin);
373 memcpy(sas_out, &sas, sizeof(struct sockaddr_storage));
378 if (dlm_local_addr[0]->ss_family == AF_INET) {
379 struct sockaddr_in *in4 = (struct sockaddr_in *) &sas;
380 struct sockaddr_in *ret4 = (struct sockaddr_in *) sa_out;
381 ret4->sin_addr.s_addr = in4->sin_addr.s_addr;
383 struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) &sas;
384 struct sockaddr_in6 *ret6 = (struct sockaddr_in6 *) sa_out;
385 ret6->sin6_addr = in6->sin6_addr;
391 static int addr_to_nodeid(struct sockaddr_storage *addr, int *nodeid,
394 struct dlm_node_addr *na;
398 spin_lock(&dlm_node_addrs_spin);
399 list_for_each_entry(na, &dlm_node_addrs, list) {
403 for (addr_i = 0; addr_i < na->addr_count; addr_i++) {
404 if (addr_compare(na->addr[addr_i], addr)) {
405 *nodeid = na->nodeid;
413 spin_unlock(&dlm_node_addrs_spin);
417 /* caller need to held dlm_node_addrs_spin lock */
418 static bool dlm_lowcomms_na_has_addr(const struct dlm_node_addr *na,
419 const struct sockaddr_storage *addr)
423 for (i = 0; i < na->addr_count; i++) {
424 if (addr_compare(na->addr[i], addr))
431 int dlm_lowcomms_addr(int nodeid, struct sockaddr_storage *addr, int len)
433 struct sockaddr_storage *new_addr;
434 struct dlm_node_addr *new_node, *na;
437 new_node = kzalloc(sizeof(struct dlm_node_addr), GFP_NOFS);
441 new_addr = kzalloc(sizeof(struct sockaddr_storage), GFP_NOFS);
447 memcpy(new_addr, addr, len);
449 spin_lock(&dlm_node_addrs_spin);
450 na = find_node_addr(nodeid);
452 new_node->nodeid = nodeid;
453 new_node->addr[0] = new_addr;
454 new_node->addr_count = 1;
455 new_node->mark = dlm_config.ci_mark;
456 list_add(&new_node->list, &dlm_node_addrs);
457 spin_unlock(&dlm_node_addrs_spin);
461 ret = dlm_lowcomms_na_has_addr(na, addr);
463 spin_unlock(&dlm_node_addrs_spin);
469 if (na->addr_count >= DLM_MAX_ADDR_COUNT) {
470 spin_unlock(&dlm_node_addrs_spin);
476 na->addr[na->addr_count++] = new_addr;
477 spin_unlock(&dlm_node_addrs_spin);
482 /* Data available on socket or listen socket received a connect */
483 static void lowcomms_data_ready(struct sock *sk)
485 struct connection *con;
487 read_lock_bh(&sk->sk_callback_lock);
489 if (con && !test_and_set_bit(CF_READ_PENDING, &con->flags))
490 queue_work(recv_workqueue, &con->rwork);
491 read_unlock_bh(&sk->sk_callback_lock);
494 static void lowcomms_listen_data_ready(struct sock *sk)
499 queue_work(recv_workqueue, &listen_con.rwork);
502 static void lowcomms_write_space(struct sock *sk)
504 struct connection *con;
506 read_lock_bh(&sk->sk_callback_lock);
511 if (!test_and_set_bit(CF_CONNECTED, &con->flags)) {
512 log_print("successful connected to node %d", con->nodeid);
513 queue_work(send_workqueue, &con->swork);
517 clear_bit(SOCK_NOSPACE, &con->sock->flags);
519 if (test_and_clear_bit(CF_APP_LIMITED, &con->flags)) {
520 con->sock->sk->sk_write_pending--;
521 clear_bit(SOCKWQ_ASYNC_NOSPACE, &con->sock->flags);
524 queue_work(send_workqueue, &con->swork);
526 read_unlock_bh(&sk->sk_callback_lock);
529 static inline void lowcomms_connect_sock(struct connection *con)
531 if (test_bit(CF_CLOSE, &con->flags))
533 queue_work(send_workqueue, &con->swork);
537 static void lowcomms_state_change(struct sock *sk)
539 /* SCTP layer is not calling sk_data_ready when the connection
540 * is done, so we catch the signal through here. Also, it
541 * doesn't switch socket state when entering shutdown, so we
542 * skip the write in that case.
544 if (sk->sk_shutdown) {
545 if (sk->sk_shutdown == RCV_SHUTDOWN)
546 lowcomms_data_ready(sk);
547 } else if (sk->sk_state == TCP_ESTABLISHED) {
548 lowcomms_write_space(sk);
552 int dlm_lowcomms_connect_node(int nodeid)
554 struct connection *con;
557 if (nodeid == dlm_our_nodeid())
560 idx = srcu_read_lock(&connections_srcu);
561 con = nodeid2con(nodeid, GFP_NOFS);
563 srcu_read_unlock(&connections_srcu, idx);
567 lowcomms_connect_sock(con);
568 srcu_read_unlock(&connections_srcu, idx);
573 int dlm_lowcomms_nodes_set_mark(int nodeid, unsigned int mark)
575 struct dlm_node_addr *na;
577 spin_lock(&dlm_node_addrs_spin);
578 na = find_node_addr(nodeid);
580 spin_unlock(&dlm_node_addrs_spin);
585 spin_unlock(&dlm_node_addrs_spin);
590 static void lowcomms_error_report(struct sock *sk)
592 struct connection *con;
593 struct sockaddr_storage saddr;
594 void (*orig_report)(struct sock *) = NULL;
596 read_lock_bh(&sk->sk_callback_lock);
601 orig_report = listen_sock.sk_error_report;
602 if (kernel_getpeername(sk->sk_socket, (struct sockaddr *)&saddr) < 0) {
603 printk_ratelimited(KERN_ERR "dlm: node %d: socket error "
604 "sending to node %d, port %d, "
605 "sk_err=%d/%d\n", dlm_our_nodeid(),
606 con->nodeid, dlm_config.ci_tcp_port,
607 sk->sk_err, sk->sk_err_soft);
608 } else if (saddr.ss_family == AF_INET) {
609 struct sockaddr_in *sin4 = (struct sockaddr_in *)&saddr;
611 printk_ratelimited(KERN_ERR "dlm: node %d: socket error "
612 "sending to node %d at %pI4, port %d, "
613 "sk_err=%d/%d\n", dlm_our_nodeid(),
614 con->nodeid, &sin4->sin_addr.s_addr,
615 dlm_config.ci_tcp_port, sk->sk_err,
618 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&saddr;
620 printk_ratelimited(KERN_ERR "dlm: node %d: socket error "
621 "sending to node %d at %u.%u.%u.%u, "
622 "port %d, sk_err=%d/%d\n", dlm_our_nodeid(),
623 con->nodeid, sin6->sin6_addr.s6_addr32[0],
624 sin6->sin6_addr.s6_addr32[1],
625 sin6->sin6_addr.s6_addr32[2],
626 sin6->sin6_addr.s6_addr32[3],
627 dlm_config.ci_tcp_port, sk->sk_err,
631 /* below sendcon only handling */
632 if (test_bit(CF_IS_OTHERCON, &con->flags))
635 switch (sk->sk_err) {
637 set_bit(CF_DELAY_CONNECT, &con->flags);
643 if (!test_and_set_bit(CF_RECONNECT, &con->flags))
644 queue_work(send_workqueue, &con->swork);
647 read_unlock_bh(&sk->sk_callback_lock);
652 /* Note: sk_callback_lock must be locked before calling this function. */
653 static void save_listen_callbacks(struct socket *sock)
655 struct sock *sk = sock->sk;
657 listen_sock.sk_data_ready = sk->sk_data_ready;
658 listen_sock.sk_state_change = sk->sk_state_change;
659 listen_sock.sk_write_space = sk->sk_write_space;
660 listen_sock.sk_error_report = sk->sk_error_report;
663 static void restore_callbacks(struct socket *sock)
665 struct sock *sk = sock->sk;
667 write_lock_bh(&sk->sk_callback_lock);
668 sk->sk_user_data = NULL;
669 sk->sk_data_ready = listen_sock.sk_data_ready;
670 sk->sk_state_change = listen_sock.sk_state_change;
671 sk->sk_write_space = listen_sock.sk_write_space;
672 sk->sk_error_report = listen_sock.sk_error_report;
673 write_unlock_bh(&sk->sk_callback_lock);
676 static void add_listen_sock(struct socket *sock, struct listen_connection *con)
678 struct sock *sk = sock->sk;
680 write_lock_bh(&sk->sk_callback_lock);
681 save_listen_callbacks(sock);
684 sk->sk_user_data = con;
685 sk->sk_allocation = GFP_NOFS;
686 /* Install a data_ready callback */
687 sk->sk_data_ready = lowcomms_listen_data_ready;
688 write_unlock_bh(&sk->sk_callback_lock);
691 /* Make a socket active */
692 static void add_sock(struct socket *sock, struct connection *con)
694 struct sock *sk = sock->sk;
696 write_lock_bh(&sk->sk_callback_lock);
699 sk->sk_user_data = con;
700 /* Install a data_ready callback */
701 sk->sk_data_ready = lowcomms_data_ready;
702 sk->sk_write_space = lowcomms_write_space;
703 sk->sk_state_change = lowcomms_state_change;
704 sk->sk_allocation = GFP_NOFS;
705 sk->sk_error_report = lowcomms_error_report;
706 write_unlock_bh(&sk->sk_callback_lock);
709 /* Add the port number to an IPv6 or 4 sockaddr and return the address
711 static void make_sockaddr(struct sockaddr_storage *saddr, uint16_t port,
714 saddr->ss_family = dlm_local_addr[0]->ss_family;
715 if (saddr->ss_family == AF_INET) {
716 struct sockaddr_in *in4_addr = (struct sockaddr_in *)saddr;
717 in4_addr->sin_port = cpu_to_be16(port);
718 *addr_len = sizeof(struct sockaddr_in);
719 memset(&in4_addr->sin_zero, 0, sizeof(in4_addr->sin_zero));
721 struct sockaddr_in6 *in6_addr = (struct sockaddr_in6 *)saddr;
722 in6_addr->sin6_port = cpu_to_be16(port);
723 *addr_len = sizeof(struct sockaddr_in6);
725 memset((char *)saddr + *addr_len, 0, sizeof(struct sockaddr_storage) - *addr_len);
728 static void dlm_page_release(struct kref *kref)
730 struct writequeue_entry *e = container_of(kref, struct writequeue_entry,
733 __free_page(e->page);
737 static void dlm_msg_release(struct kref *kref)
739 struct dlm_msg *msg = container_of(kref, struct dlm_msg, ref);
741 kref_put(&msg->entry->ref, dlm_page_release);
745 static void free_entry(struct writequeue_entry *e)
747 struct dlm_msg *msg, *tmp;
749 list_for_each_entry_safe(msg, tmp, &e->msgs, list) {
751 msg->orig_msg->retransmit = false;
752 kref_put(&msg->orig_msg->ref, dlm_msg_release);
755 list_del(&msg->list);
756 kref_put(&msg->ref, dlm_msg_release);
760 atomic_dec(&e->con->writequeue_cnt);
761 kref_put(&e->ref, dlm_page_release);
764 static void dlm_close_sock(struct socket **sock)
767 restore_callbacks(*sock);
773 /* Close a remote connection and tidy up */
774 static void close_connection(struct connection *con, bool and_other,
777 bool closing = test_and_set_bit(CF_CLOSING, &con->flags);
778 struct writequeue_entry *e;
780 if (tx && !closing && cancel_work_sync(&con->swork)) {
781 log_print("canceled swork for node %d", con->nodeid);
782 clear_bit(CF_WRITE_PENDING, &con->flags);
784 if (rx && !closing && cancel_work_sync(&con->rwork)) {
785 log_print("canceled rwork for node %d", con->nodeid);
786 clear_bit(CF_READ_PENDING, &con->flags);
789 mutex_lock(&con->sock_mutex);
790 dlm_close_sock(&con->sock);
792 if (con->othercon && and_other) {
793 /* Will only re-enter once. */
794 close_connection(con->othercon, false, tx, rx);
797 /* if we send a writequeue entry only a half way, we drop the
798 * whole entry because reconnection and that we not start of the
799 * middle of a msg which will confuse the other end.
801 * we can always drop messages because retransmits, but what we
802 * cannot allow is to transmit half messages which may be processed
805 * our policy is to start on a clean state when disconnects, we don't
806 * know what's send/received on transport layer in this case.
808 spin_lock(&con->writequeue_lock);
809 if (!list_empty(&con->writequeue)) {
810 e = list_first_entry(&con->writequeue, struct writequeue_entry,
815 spin_unlock(&con->writequeue_lock);
817 con->rx_leftover = 0;
819 clear_bit(CF_APP_LIMITED, &con->flags);
820 clear_bit(CF_CONNECTED, &con->flags);
821 clear_bit(CF_DELAY_CONNECT, &con->flags);
822 clear_bit(CF_RECONNECT, &con->flags);
823 clear_bit(CF_EOF, &con->flags);
824 mutex_unlock(&con->sock_mutex);
825 clear_bit(CF_CLOSING, &con->flags);
828 static void shutdown_connection(struct connection *con)
832 flush_work(&con->swork);
834 mutex_lock(&con->sock_mutex);
835 /* nothing to shutdown */
837 mutex_unlock(&con->sock_mutex);
841 set_bit(CF_SHUTDOWN, &con->flags);
842 ret = kernel_sock_shutdown(con->sock, SHUT_WR);
843 mutex_unlock(&con->sock_mutex);
845 log_print("Connection %p failed to shutdown: %d will force close",
849 ret = wait_event_timeout(con->shutdown_wait,
850 !test_bit(CF_SHUTDOWN, &con->flags),
851 DLM_SHUTDOWN_WAIT_TIMEOUT);
853 log_print("Connection %p shutdown timed out, will force close",
862 clear_bit(CF_SHUTDOWN, &con->flags);
863 close_connection(con, false, true, true);
866 static void dlm_tcp_shutdown(struct connection *con)
869 shutdown_connection(con->othercon);
870 shutdown_connection(con);
873 static int con_realloc_receive_buf(struct connection *con, int newlen)
875 unsigned char *newbuf;
877 newbuf = kmalloc(newlen, GFP_NOFS);
881 /* copy any leftover from last receive */
882 if (con->rx_leftover)
883 memmove(newbuf, con->rx_buf, con->rx_leftover);
885 /* swap to new buffer space */
887 con->rx_buflen = newlen;
888 con->rx_buf = newbuf;
893 /* Data received from remote end */
894 static int receive_from_sock(struct connection *con)
896 int call_again_soon = 0;
901 mutex_lock(&con->sock_mutex);
903 if (con->sock == NULL) {
908 /* realloc if we get new buffer size to read out */
909 buflen = dlm_config.ci_buffer_size;
910 if (con->rx_buflen != buflen && con->rx_leftover <= buflen) {
911 ret = con_realloc_receive_buf(con, buflen);
916 /* calculate new buffer parameter regarding last receive and
917 * possible leftover bytes
919 iov.iov_base = con->rx_buf + con->rx_leftover;
920 iov.iov_len = con->rx_buflen - con->rx_leftover;
922 memset(&msg, 0, sizeof(msg));
923 msg.msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL;
924 ret = kernel_recvmsg(con->sock, &msg, &iov, 1, iov.iov_len,
928 else if (ret == iov.iov_len)
931 /* new buflen according readed bytes and leftover from last receive */
932 buflen = ret + con->rx_leftover;
933 ret = dlm_process_incoming_buffer(con->nodeid, con->rx_buf, buflen);
937 /* calculate leftover bytes from process and put it into begin of
938 * the receive buffer, so next receive we have the full message
939 * at the start address of the receive buffer.
941 con->rx_leftover = buflen - ret;
942 if (con->rx_leftover) {
943 memmove(con->rx_buf, con->rx_buf + ret,
945 call_again_soon = true;
951 mutex_unlock(&con->sock_mutex);
955 if (!test_and_set_bit(CF_READ_PENDING, &con->flags))
956 queue_work(recv_workqueue, &con->rwork);
957 mutex_unlock(&con->sock_mutex);
962 log_print("connection %p got EOF from %d",
965 if (con->eof_condition && con->eof_condition(con)) {
966 set_bit(CF_EOF, &con->flags);
967 mutex_unlock(&con->sock_mutex);
969 mutex_unlock(&con->sock_mutex);
970 close_connection(con, false, true, false);
972 /* handling for tcp shutdown */
973 clear_bit(CF_SHUTDOWN, &con->flags);
974 wake_up(&con->shutdown_wait);
977 /* signal to breaking receive worker */
980 mutex_unlock(&con->sock_mutex);
985 /* Listening socket is busy, accept a connection */
986 static int accept_from_sock(struct listen_connection *con)
989 struct sockaddr_storage peeraddr;
990 struct socket *newsock;
993 struct connection *newcon;
994 struct connection *addcon;
1000 result = kernel_accept(con->sock, &newsock, O_NONBLOCK);
1004 /* Get the connected socket's peer */
1005 memset(&peeraddr, 0, sizeof(peeraddr));
1006 len = newsock->ops->getname(newsock, (struct sockaddr *)&peeraddr, 2);
1008 result = -ECONNABORTED;
1012 /* Get the new node's NODEID */
1013 make_sockaddr(&peeraddr, 0, &len);
1014 if (addr_to_nodeid(&peeraddr, &nodeid, &mark)) {
1015 unsigned char *b=(unsigned char *)&peeraddr;
1016 log_print("connect from non cluster node");
1017 print_hex_dump_bytes("ss: ", DUMP_PREFIX_NONE,
1018 b, sizeof(struct sockaddr_storage));
1019 sock_release(newsock);
1023 log_print("got connection from %d", nodeid);
1025 /* Check to see if we already have a connection to this node. This
1026 * could happen if the two nodes initiate a connection at roughly
1027 * the same time and the connections cross on the wire.
1028 * In this case we store the incoming one in "othercon"
1030 idx = srcu_read_lock(&connections_srcu);
1031 newcon = nodeid2con(nodeid, GFP_NOFS);
1033 srcu_read_unlock(&connections_srcu, idx);
1038 sock_set_mark(newsock->sk, mark);
1040 mutex_lock(&newcon->sock_mutex);
1042 struct connection *othercon = newcon->othercon;
1045 othercon = kzalloc(sizeof(*othercon), GFP_NOFS);
1047 log_print("failed to allocate incoming socket");
1048 mutex_unlock(&newcon->sock_mutex);
1049 srcu_read_unlock(&connections_srcu, idx);
1054 result = dlm_con_init(othercon, nodeid);
1057 mutex_unlock(&newcon->sock_mutex);
1058 srcu_read_unlock(&connections_srcu, idx);
1062 lockdep_set_subclass(&othercon->sock_mutex, 1);
1063 set_bit(CF_IS_OTHERCON, &othercon->flags);
1064 newcon->othercon = othercon;
1065 othercon->sendcon = newcon;
1067 /* close other sock con if we have something new */
1068 close_connection(othercon, false, true, false);
1071 mutex_lock(&othercon->sock_mutex);
1072 add_sock(newsock, othercon);
1074 mutex_unlock(&othercon->sock_mutex);
1077 /* accept copies the sk after we've saved the callbacks, so we
1078 don't want to save them a second time or comm errors will
1079 result in calling sk_error_report recursively. */
1080 add_sock(newsock, newcon);
1084 set_bit(CF_CONNECTED, &addcon->flags);
1085 mutex_unlock(&newcon->sock_mutex);
1088 * Add it to the active queue in case we got data
1089 * between processing the accept adding the socket
1090 * to the read_sockets list
1092 if (!test_and_set_bit(CF_READ_PENDING, &addcon->flags))
1093 queue_work(recv_workqueue, &addcon->rwork);
1095 srcu_read_unlock(&connections_srcu, idx);
1101 sock_release(newsock);
1103 if (result != -EAGAIN)
1104 log_print("error accepting connection from node: %d", result);
1109 * writequeue_entry_complete - try to delete and free write queue entry
1110 * @e: write queue entry to try to delete
1111 * @completed: bytes completed
1113 * writequeue_lock must be held.
1115 static void writequeue_entry_complete(struct writequeue_entry *e, int completed)
1117 e->offset += completed;
1118 e->len -= completed;
1119 /* signal that page was half way transmitted */
1122 if (e->len == 0 && e->users == 0)
1127 * sctp_bind_addrs - bind a SCTP socket to all our addresses
1129 static int sctp_bind_addrs(struct socket *sock, uint16_t port)
1131 struct sockaddr_storage localaddr;
1132 struct sockaddr *addr = (struct sockaddr *)&localaddr;
1133 int i, addr_len, result = 0;
1135 for (i = 0; i < dlm_local_count; i++) {
1136 memcpy(&localaddr, dlm_local_addr[i], sizeof(localaddr));
1137 make_sockaddr(&localaddr, port, &addr_len);
1140 result = kernel_bind(sock, addr, addr_len);
1142 result = sock_bind_add(sock->sk, addr, addr_len);
1145 log_print("Can't bind to %d addr number %d, %d.\n",
1146 port, i + 1, result);
1153 /* Initiate an SCTP association.
1154 This is a special case of send_to_sock() in that we don't yet have a
1155 peeled-off socket for this association, so we use the listening socket
1156 and add the primary IP address of the remote node.
1158 static void sctp_connect_to_sock(struct connection *con)
1160 struct sockaddr_storage daddr;
1163 struct socket *sock;
1166 mutex_lock(&con->sock_mutex);
1168 /* Some odd races can cause double-connects, ignore them */
1169 if (con->retries++ > MAX_CONNECT_RETRIES)
1173 log_print("node %d already connected.", con->nodeid);
1177 memset(&daddr, 0, sizeof(daddr));
1178 result = nodeid_to_addr(con->nodeid, &daddr, NULL, true, &mark);
1180 log_print("no address for nodeid %d", con->nodeid);
1184 /* Create a socket to communicate with */
1185 result = sock_create_kern(&init_net, dlm_local_addr[0]->ss_family,
1186 SOCK_STREAM, IPPROTO_SCTP, &sock);
1190 sock_set_mark(sock->sk, mark);
1192 add_sock(sock, con);
1194 /* Bind to all addresses. */
1195 if (sctp_bind_addrs(con->sock, 0))
1198 make_sockaddr(&daddr, dlm_config.ci_tcp_port, &addr_len);
1200 log_print_ratelimited("connecting to %d", con->nodeid);
1202 /* Turn off Nagle's algorithm */
1203 sctp_sock_set_nodelay(sock->sk);
1206 * Make sock->ops->connect() function return in specified time,
1207 * since O_NONBLOCK argument in connect() function does not work here,
1208 * then, we should restore the default value of this attribute.
1210 sock_set_sndtimeo(sock->sk, 5);
1211 result = sock->ops->connect(sock, (struct sockaddr *)&daddr, addr_len,
1213 sock_set_sndtimeo(sock->sk, 0);
1215 if (result == -EINPROGRESS)
1218 if (!test_and_set_bit(CF_CONNECTED, &con->flags))
1219 log_print("successful connected to node %d", con->nodeid);
1229 * Some errors are fatal and this list might need adjusting. For other
1230 * errors we try again until the max number of retries is reached.
1232 if (result != -EHOSTUNREACH &&
1233 result != -ENETUNREACH &&
1234 result != -ENETDOWN &&
1235 result != -EINVAL &&
1236 result != -EPROTONOSUPPORT) {
1237 log_print("connect %d try %d error %d", con->nodeid,
1238 con->retries, result);
1239 mutex_unlock(&con->sock_mutex);
1241 lowcomms_connect_sock(con);
1246 mutex_unlock(&con->sock_mutex);
1249 /* Connect a new socket to its peer */
1250 static void tcp_connect_to_sock(struct connection *con)
1252 struct sockaddr_storage saddr, src_addr;
1255 struct socket *sock = NULL;
1258 mutex_lock(&con->sock_mutex);
1259 if (con->retries++ > MAX_CONNECT_RETRIES)
1262 /* Some odd races can cause double-connects, ignore them */
1266 /* Create a socket to communicate with */
1267 result = sock_create_kern(&init_net, dlm_local_addr[0]->ss_family,
1268 SOCK_STREAM, IPPROTO_TCP, &sock);
1272 memset(&saddr, 0, sizeof(saddr));
1273 result = nodeid_to_addr(con->nodeid, &saddr, NULL, false, &mark);
1275 log_print("no address for nodeid %d", con->nodeid);
1279 sock_set_mark(sock->sk, mark);
1281 add_sock(sock, con);
1283 /* Bind to our cluster-known address connecting to avoid
1285 memcpy(&src_addr, dlm_local_addr[0], sizeof(src_addr));
1286 make_sockaddr(&src_addr, 0, &addr_len);
1287 result = sock->ops->bind(sock, (struct sockaddr *) &src_addr,
1290 log_print("could not bind for connect: %d", result);
1291 /* This *may* not indicate a critical error */
1294 make_sockaddr(&saddr, dlm_config.ci_tcp_port, &addr_len);
1296 log_print_ratelimited("connecting to %d", con->nodeid);
1298 /* Turn off Nagle's algorithm */
1299 tcp_sock_set_nodelay(sock->sk);
1301 result = sock->ops->connect(sock, (struct sockaddr *)&saddr, addr_len,
1303 if (result == -EINPROGRESS)
1310 sock_release(con->sock);
1316 * Some errors are fatal and this list might need adjusting. For other
1317 * errors we try again until the max number of retries is reached.
1319 if (result != -EHOSTUNREACH &&
1320 result != -ENETUNREACH &&
1321 result != -ENETDOWN &&
1322 result != -EINVAL &&
1323 result != -EPROTONOSUPPORT) {
1324 log_print("connect %d try %d error %d", con->nodeid,
1325 con->retries, result);
1326 mutex_unlock(&con->sock_mutex);
1328 lowcomms_connect_sock(con);
1332 mutex_unlock(&con->sock_mutex);
1336 /* On error caller must run dlm_close_sock() for the
1337 * listen connection socket.
1339 static int tcp_create_listen_sock(struct listen_connection *con,
1340 struct sockaddr_storage *saddr)
1342 struct socket *sock = NULL;
1346 if (dlm_local_addr[0]->ss_family == AF_INET)
1347 addr_len = sizeof(struct sockaddr_in);
1349 addr_len = sizeof(struct sockaddr_in6);
1351 /* Create a socket to communicate with */
1352 result = sock_create_kern(&init_net, dlm_local_addr[0]->ss_family,
1353 SOCK_STREAM, IPPROTO_TCP, &sock);
1355 log_print("Can't create listening comms socket");
1359 sock_set_mark(sock->sk, dlm_config.ci_mark);
1361 /* Turn off Nagle's algorithm */
1362 tcp_sock_set_nodelay(sock->sk);
1364 sock_set_reuseaddr(sock->sk);
1366 add_listen_sock(sock, con);
1368 /* Bind to our port */
1369 make_sockaddr(saddr, dlm_config.ci_tcp_port, &addr_len);
1370 result = sock->ops->bind(sock, (struct sockaddr *) saddr, addr_len);
1372 log_print("Can't bind to port %d", dlm_config.ci_tcp_port);
1375 sock_set_keepalive(sock->sk);
1377 result = sock->ops->listen(sock, 5);
1379 log_print("Can't listen on port %d", dlm_config.ci_tcp_port);
1389 /* Get local addresses */
1390 static void init_local(void)
1392 struct sockaddr_storage sas, *addr;
1395 dlm_local_count = 0;
1396 for (i = 0; i < DLM_MAX_ADDR_COUNT; i++) {
1397 if (dlm_our_addr(&sas, i))
1400 addr = kmemdup(&sas, sizeof(*addr), GFP_NOFS);
1403 dlm_local_addr[dlm_local_count++] = addr;
1407 static void deinit_local(void)
1411 for (i = 0; i < dlm_local_count; i++)
1412 kfree(dlm_local_addr[i]);
1415 /* Initialise SCTP socket and bind to all interfaces
1416 * On error caller must run dlm_close_sock() for the
1417 * listen connection socket.
1419 static int sctp_listen_for_all(struct listen_connection *con)
1421 struct socket *sock = NULL;
1422 int result = -EINVAL;
1424 log_print("Using SCTP for communications");
1426 result = sock_create_kern(&init_net, dlm_local_addr[0]->ss_family,
1427 SOCK_STREAM, IPPROTO_SCTP, &sock);
1429 log_print("Can't create comms socket, check SCTP is loaded");
1433 sock_set_rcvbuf(sock->sk, NEEDED_RMEM);
1434 sock_set_mark(sock->sk, dlm_config.ci_mark);
1435 sctp_sock_set_nodelay(sock->sk);
1437 add_listen_sock(sock, con);
1439 /* Bind to all addresses. */
1440 result = sctp_bind_addrs(con->sock, dlm_config.ci_tcp_port);
1444 result = sock->ops->listen(sock, 5);
1446 log_print("Can't set socket listening");
1456 static int tcp_listen_for_all(void)
1458 /* We don't support multi-homed hosts */
1459 if (dlm_local_count > 1) {
1460 log_print("TCP protocol can't handle multi-homed hosts, "
1465 log_print("Using TCP for communications");
1467 return tcp_create_listen_sock(&listen_con, dlm_local_addr[0]);
1472 static struct writequeue_entry *new_writequeue_entry(struct connection *con,
1475 struct writequeue_entry *entry;
1477 entry = kzalloc(sizeof(*entry), allocation);
1481 entry->page = alloc_page(allocation | __GFP_ZERO);
1489 kref_init(&entry->ref);
1490 INIT_LIST_HEAD(&entry->msgs);
1495 static struct writequeue_entry *new_wq_entry(struct connection *con, int len,
1496 gfp_t allocation, char **ppc,
1497 void (*cb)(struct dlm_mhandle *mh),
1498 struct dlm_mhandle *mh)
1500 struct writequeue_entry *e;
1502 spin_lock(&con->writequeue_lock);
1503 if (!list_empty(&con->writequeue)) {
1504 e = list_last_entry(&con->writequeue, struct writequeue_entry, list);
1505 if (DLM_WQ_REMAIN_BYTES(e) >= len) {
1508 *ppc = page_address(e->page) + e->end;
1514 spin_unlock(&con->writequeue_lock);
1519 spin_unlock(&con->writequeue_lock);
1521 e = new_writequeue_entry(con, allocation);
1526 *ppc = page_address(e->page);
1528 atomic_inc(&con->writequeue_cnt);
1530 spin_lock(&con->writequeue_lock);
1534 list_add_tail(&e->list, &con->writequeue);
1535 spin_unlock(&con->writequeue_lock);
1540 static struct dlm_msg *dlm_lowcomms_new_msg_con(struct connection *con, int len,
1541 gfp_t allocation, char **ppc,
1542 void (*cb)(struct dlm_mhandle *mh),
1543 struct dlm_mhandle *mh)
1545 struct writequeue_entry *e;
1546 struct dlm_msg *msg;
1548 msg = kzalloc(sizeof(*msg), allocation);
1552 kref_init(&msg->ref);
1554 e = new_wq_entry(con, len, allocation, ppc, cb, mh);
1567 struct dlm_msg *dlm_lowcomms_new_msg(int nodeid, int len, gfp_t allocation,
1568 char **ppc, void (*cb)(struct dlm_mhandle *mh),
1569 struct dlm_mhandle *mh)
1571 struct connection *con;
1572 struct dlm_msg *msg;
1575 if (len > DLM_MAX_SOCKET_BUFSIZE ||
1576 len < sizeof(struct dlm_header)) {
1577 BUILD_BUG_ON(PAGE_SIZE < DLM_MAX_SOCKET_BUFSIZE);
1578 log_print("failed to allocate a buffer of size %d", len);
1583 idx = srcu_read_lock(&connections_srcu);
1584 con = nodeid2con(nodeid, allocation);
1586 srcu_read_unlock(&connections_srcu, idx);
1590 msg = dlm_lowcomms_new_msg_con(con, len, allocation, ppc, cb, mh);
1592 srcu_read_unlock(&connections_srcu, idx);
1596 /* we assume if successful commit must called */
1601 static void _dlm_lowcomms_commit_msg(struct dlm_msg *msg)
1603 struct writequeue_entry *e = msg->entry;
1604 struct connection *con = e->con;
1607 spin_lock(&con->writequeue_lock);
1608 kref_get(&msg->ref);
1609 list_add(&msg->list, &e->msgs);
1615 e->len = DLM_WQ_LENGTH_BYTES(e);
1616 spin_unlock(&con->writequeue_lock);
1618 queue_work(send_workqueue, &con->swork);
1622 spin_unlock(&con->writequeue_lock);
1626 void dlm_lowcomms_commit_msg(struct dlm_msg *msg)
1628 _dlm_lowcomms_commit_msg(msg);
1629 srcu_read_unlock(&connections_srcu, msg->idx);
1632 void dlm_lowcomms_put_msg(struct dlm_msg *msg)
1634 kref_put(&msg->ref, dlm_msg_release);
1637 /* does not held connections_srcu, usage workqueue only */
1638 int dlm_lowcomms_resend_msg(struct dlm_msg *msg)
1640 struct dlm_msg *msg_resend;
1643 if (msg->retransmit)
1646 msg_resend = dlm_lowcomms_new_msg_con(msg->entry->con, msg->len,
1647 GFP_ATOMIC, &ppc, NULL, NULL);
1651 msg->retransmit = true;
1652 kref_get(&msg->ref);
1653 msg_resend->orig_msg = msg;
1655 memcpy(ppc, msg->ppc, msg->len);
1656 _dlm_lowcomms_commit_msg(msg_resend);
1657 dlm_lowcomms_put_msg(msg_resend);
1662 /* Send a message */
1663 static void send_to_sock(struct connection *con)
1665 const int msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL;
1666 struct writequeue_entry *e;
1667 int len, offset, ret;
1670 mutex_lock(&con->sock_mutex);
1671 if (con->sock == NULL)
1674 spin_lock(&con->writequeue_lock);
1676 e = con_next_wq(con);
1680 e = list_first_entry(&con->writequeue, struct writequeue_entry, list);
1683 BUG_ON(len == 0 && e->users == 0);
1684 spin_unlock(&con->writequeue_lock);
1686 ret = kernel_sendpage(con->sock, e->page, offset, len,
1688 if (ret == -EAGAIN || ret == 0) {
1689 if (ret == -EAGAIN &&
1690 test_bit(SOCKWQ_ASYNC_NOSPACE, &con->sock->flags) &&
1691 !test_and_set_bit(CF_APP_LIMITED, &con->flags)) {
1692 /* Notify TCP that we're limited by the
1693 * application window size.
1695 set_bit(SOCK_NOSPACE, &con->sock->flags);
1696 con->sock->sk->sk_write_pending++;
1703 /* Don't starve people filling buffers */
1704 if (++count >= MAX_SEND_MSG_COUNT) {
1709 spin_lock(&con->writequeue_lock);
1710 writequeue_entry_complete(e, ret);
1712 spin_unlock(&con->writequeue_lock);
1714 /* close if we got EOF */
1715 if (test_and_clear_bit(CF_EOF, &con->flags)) {
1716 mutex_unlock(&con->sock_mutex);
1717 close_connection(con, false, false, true);
1719 /* handling for tcp shutdown */
1720 clear_bit(CF_SHUTDOWN, &con->flags);
1721 wake_up(&con->shutdown_wait);
1723 mutex_unlock(&con->sock_mutex);
1729 mutex_unlock(&con->sock_mutex);
1733 mutex_unlock(&con->sock_mutex);
1734 queue_work(send_workqueue, &con->swork);
1738 static void clean_one_writequeue(struct connection *con)
1740 struct writequeue_entry *e, *safe;
1742 spin_lock(&con->writequeue_lock);
1743 list_for_each_entry_safe(e, safe, &con->writequeue, list) {
1746 spin_unlock(&con->writequeue_lock);
1749 /* Called from recovery when it knows that a node has
1751 int dlm_lowcomms_close(int nodeid)
1753 struct connection *con;
1754 struct dlm_node_addr *na;
1757 log_print("closing connection to node %d", nodeid);
1758 idx = srcu_read_lock(&connections_srcu);
1759 con = nodeid2con(nodeid, 0);
1761 set_bit(CF_CLOSE, &con->flags);
1762 close_connection(con, true, true, true);
1763 clean_one_writequeue(con);
1765 clean_one_writequeue(con->othercon);
1767 srcu_read_unlock(&connections_srcu, idx);
1769 spin_lock(&dlm_node_addrs_spin);
1770 na = find_node_addr(nodeid);
1772 list_del(&na->list);
1773 while (na->addr_count--)
1774 kfree(na->addr[na->addr_count]);
1777 spin_unlock(&dlm_node_addrs_spin);
1782 /* Receive workqueue function */
1783 static void process_recv_sockets(struct work_struct *work)
1785 struct connection *con = container_of(work, struct connection, rwork);
1788 clear_bit(CF_READ_PENDING, &con->flags);
1790 err = receive_from_sock(con);
1794 static void process_listen_recv_socket(struct work_struct *work)
1796 accept_from_sock(&listen_con);
1799 /* Send workqueue function */
1800 static void process_send_sockets(struct work_struct *work)
1802 struct connection *con = container_of(work, struct connection, swork);
1804 WARN_ON(test_bit(CF_IS_OTHERCON, &con->flags));
1806 clear_bit(CF_WRITE_PENDING, &con->flags);
1808 if (test_and_clear_bit(CF_RECONNECT, &con->flags)) {
1809 close_connection(con, false, false, true);
1810 dlm_midcomms_unack_msg_resend(con->nodeid);
1813 if (con->sock == NULL) { /* not mutex protected so check it inside too */
1814 if (test_and_clear_bit(CF_DELAY_CONNECT, &con->flags))
1816 con->connect_action(con);
1818 if (!list_empty(&con->writequeue))
1822 static void work_stop(void)
1824 if (recv_workqueue) {
1825 destroy_workqueue(recv_workqueue);
1826 recv_workqueue = NULL;
1829 if (send_workqueue) {
1830 destroy_workqueue(send_workqueue);
1831 send_workqueue = NULL;
1835 static int work_start(void)
1837 recv_workqueue = alloc_ordered_workqueue("dlm_recv", WQ_MEM_RECLAIM);
1838 if (!recv_workqueue) {
1839 log_print("can't start dlm_recv");
1843 send_workqueue = alloc_ordered_workqueue("dlm_send", WQ_MEM_RECLAIM);
1844 if (!send_workqueue) {
1845 log_print("can't start dlm_send");
1846 destroy_workqueue(recv_workqueue);
1847 recv_workqueue = NULL;
1854 static void shutdown_conn(struct connection *con)
1856 if (con->shutdown_action)
1857 con->shutdown_action(con);
1860 void dlm_lowcomms_shutdown(void)
1864 /* Set all the flags to prevent any
1870 flush_workqueue(recv_workqueue);
1872 flush_workqueue(send_workqueue);
1874 dlm_close_sock(&listen_con.sock);
1876 idx = srcu_read_lock(&connections_srcu);
1877 foreach_conn(shutdown_conn);
1878 srcu_read_unlock(&connections_srcu, idx);
1881 static void _stop_conn(struct connection *con, bool and_other)
1883 mutex_lock(&con->sock_mutex);
1884 set_bit(CF_CLOSE, &con->flags);
1885 set_bit(CF_READ_PENDING, &con->flags);
1886 set_bit(CF_WRITE_PENDING, &con->flags);
1887 if (con->sock && con->sock->sk) {
1888 write_lock_bh(&con->sock->sk->sk_callback_lock);
1889 con->sock->sk->sk_user_data = NULL;
1890 write_unlock_bh(&con->sock->sk->sk_callback_lock);
1892 if (con->othercon && and_other)
1893 _stop_conn(con->othercon, false);
1894 mutex_unlock(&con->sock_mutex);
1897 static void stop_conn(struct connection *con)
1899 _stop_conn(con, true);
1902 static void connection_release(struct rcu_head *rcu)
1904 struct connection *con = container_of(rcu, struct connection, rcu);
1910 static void free_conn(struct connection *con)
1912 close_connection(con, true, true, true);
1913 spin_lock(&connections_lock);
1914 hlist_del_rcu(&con->list);
1915 spin_unlock(&connections_lock);
1916 if (con->othercon) {
1917 clean_one_writequeue(con->othercon);
1918 call_srcu(&connections_srcu, &con->othercon->rcu,
1919 connection_release);
1921 clean_one_writequeue(con);
1922 call_srcu(&connections_srcu, &con->rcu, connection_release);
1925 static void work_flush(void)
1929 struct connection *con;
1933 foreach_conn(stop_conn);
1935 flush_workqueue(recv_workqueue);
1937 flush_workqueue(send_workqueue);
1938 for (i = 0; i < CONN_HASH_SIZE && ok; i++) {
1939 hlist_for_each_entry_rcu(con, &connection_hash[i],
1941 ok &= test_bit(CF_READ_PENDING, &con->flags);
1942 ok &= test_bit(CF_WRITE_PENDING, &con->flags);
1943 if (con->othercon) {
1944 ok &= test_bit(CF_READ_PENDING,
1945 &con->othercon->flags);
1946 ok &= test_bit(CF_WRITE_PENDING,
1947 &con->othercon->flags);
1954 void dlm_lowcomms_stop(void)
1958 idx = srcu_read_lock(&connections_srcu);
1960 foreach_conn(free_conn);
1961 srcu_read_unlock(&connections_srcu, idx);
1966 int dlm_lowcomms_start(void)
1968 int error = -EINVAL;
1971 for (i = 0; i < CONN_HASH_SIZE; i++)
1972 INIT_HLIST_HEAD(&connection_hash[i]);
1975 if (!dlm_local_count) {
1977 log_print("no local IP address has been set");
1981 INIT_WORK(&listen_con.rwork, process_listen_recv_socket);
1983 error = work_start();
1989 /* Start listening */
1990 switch (dlm_config.ci_protocol) {
1992 error = tcp_listen_for_all();
1994 case DLM_PROTO_SCTP:
1995 error = sctp_listen_for_all(&listen_con);
1998 log_print("Invalid protocol identifier %d set",
1999 dlm_config.ci_protocol);
2010 dlm_close_sock(&listen_con.sock);
2018 void dlm_lowcomms_exit(void)
2020 struct dlm_node_addr *na, *safe;
2022 spin_lock(&dlm_node_addrs_spin);
2023 list_for_each_entry_safe(na, safe, &dlm_node_addrs, list) {
2024 list_del(&na->list);
2025 while (na->addr_count--)
2026 kfree(na->addr[na->addr_count]);
2029 spin_unlock(&dlm_node_addrs_spin);