xref: /openbmc/linux/fs/dlm/lowcomms.c (revision 39bd4177)
16ed7257bSPatrick Caulfield /******************************************************************************
26ed7257bSPatrick Caulfield *******************************************************************************
36ed7257bSPatrick Caulfield **
46ed7257bSPatrick Caulfield **  Copyright (C) Sistina Software, Inc.  1997-2003  All rights reserved.
56ed7257bSPatrick Caulfield **  Copyright (C) 2004-2007 Red Hat, Inc.  All rights reserved.
66ed7257bSPatrick Caulfield **
76ed7257bSPatrick Caulfield **  This copyrighted material is made available to anyone wishing to use,
86ed7257bSPatrick Caulfield **  modify, copy, or redistribute it subject to the terms and conditions
96ed7257bSPatrick Caulfield **  of the GNU General Public License v.2.
106ed7257bSPatrick Caulfield **
116ed7257bSPatrick Caulfield *******************************************************************************
126ed7257bSPatrick Caulfield ******************************************************************************/
136ed7257bSPatrick Caulfield 
146ed7257bSPatrick Caulfield /*
156ed7257bSPatrick Caulfield  * lowcomms.c
166ed7257bSPatrick Caulfield  *
176ed7257bSPatrick Caulfield  * This is the "low-level" comms layer.
186ed7257bSPatrick Caulfield  *
196ed7257bSPatrick Caulfield  * It is responsible for sending/receiving messages
206ed7257bSPatrick Caulfield  * from other nodes in the cluster.
216ed7257bSPatrick Caulfield  *
226ed7257bSPatrick Caulfield  * Cluster nodes are referred to by their nodeids. nodeids are
236ed7257bSPatrick Caulfield  * simply 32 bit numbers to the locking module - if they need to
246ed7257bSPatrick Caulfield  * be expanded for the cluster infrastructure then that is it's
256ed7257bSPatrick Caulfield  * responsibility. It is this layer's
266ed7257bSPatrick Caulfield  * responsibility to resolve these into IP address or
276ed7257bSPatrick Caulfield  * whatever it needs for inter-node communication.
286ed7257bSPatrick Caulfield  *
296ed7257bSPatrick Caulfield  * The comms level is two kernel threads that deal mainly with
306ed7257bSPatrick Caulfield  * the receiving of messages from other nodes and passing them
316ed7257bSPatrick Caulfield  * up to the mid-level comms layer (which understands the
326ed7257bSPatrick Caulfield  * message format) for execution by the locking core, and
336ed7257bSPatrick Caulfield  * a send thread which does all the setting up of connections
346ed7257bSPatrick Caulfield  * to remote nodes and the sending of data. Threads are not allowed
356ed7257bSPatrick Caulfield  * to send their own data because it may cause them to wait in times
366ed7257bSPatrick Caulfield  * of high load. Also, this way, the sending thread can collect together
376ed7257bSPatrick Caulfield  * messages bound for one node and send them in one block.
386ed7257bSPatrick Caulfield  *
396ed7257bSPatrick Caulfield  * lowcomms will choose to use wither TCP or SCTP as its transport layer
406ed7257bSPatrick Caulfield  * depending on the configuration variable 'protocol'. This should be set
416ed7257bSPatrick Caulfield  * to 0 (default) for TCP or 1 for SCTP. It shouldbe configured using a
426ed7257bSPatrick Caulfield  * cluster-wide mechanism as it must be the same on all nodes of the cluster
436ed7257bSPatrick Caulfield  * for the DLM to function.
446ed7257bSPatrick Caulfield  *
456ed7257bSPatrick Caulfield  */
466ed7257bSPatrick Caulfield 
476ed7257bSPatrick Caulfield #include <asm/ioctls.h>
486ed7257bSPatrick Caulfield #include <net/sock.h>
496ed7257bSPatrick Caulfield #include <net/tcp.h>
506ed7257bSPatrick Caulfield #include <linux/pagemap.h>
516ed7257bSPatrick Caulfield #include <linux/idr.h>
526ed7257bSPatrick Caulfield #include <linux/file.h>
536ed7257bSPatrick Caulfield #include <linux/sctp.h>
546ed7257bSPatrick Caulfield #include <net/sctp/user.h>
556ed7257bSPatrick Caulfield 
566ed7257bSPatrick Caulfield #include "dlm_internal.h"
576ed7257bSPatrick Caulfield #include "lowcomms.h"
586ed7257bSPatrick Caulfield #include "midcomms.h"
596ed7257bSPatrick Caulfield #include "config.h"
606ed7257bSPatrick Caulfield 
616ed7257bSPatrick Caulfield #define NEEDED_RMEM (4*1024*1024)
626ed7257bSPatrick Caulfield 
636ed7257bSPatrick Caulfield struct cbuf {
646ed7257bSPatrick Caulfield 	unsigned int base;
656ed7257bSPatrick Caulfield 	unsigned int len;
666ed7257bSPatrick Caulfield 	unsigned int mask;
676ed7257bSPatrick Caulfield };
686ed7257bSPatrick Caulfield 
696ed7257bSPatrick Caulfield static void cbuf_add(struct cbuf *cb, int n)
706ed7257bSPatrick Caulfield {
716ed7257bSPatrick Caulfield 	cb->len += n;
726ed7257bSPatrick Caulfield }
736ed7257bSPatrick Caulfield 
746ed7257bSPatrick Caulfield static int cbuf_data(struct cbuf *cb)
756ed7257bSPatrick Caulfield {
766ed7257bSPatrick Caulfield 	return ((cb->base + cb->len) & cb->mask);
776ed7257bSPatrick Caulfield }
786ed7257bSPatrick Caulfield 
796ed7257bSPatrick Caulfield static void cbuf_init(struct cbuf *cb, int size)
806ed7257bSPatrick Caulfield {
816ed7257bSPatrick Caulfield 	cb->base = cb->len = 0;
826ed7257bSPatrick Caulfield 	cb->mask = size-1;
836ed7257bSPatrick Caulfield }
846ed7257bSPatrick Caulfield 
856ed7257bSPatrick Caulfield static void cbuf_eat(struct cbuf *cb, int n)
866ed7257bSPatrick Caulfield {
876ed7257bSPatrick Caulfield 	cb->len  -= n;
886ed7257bSPatrick Caulfield 	cb->base += n;
896ed7257bSPatrick Caulfield 	cb->base &= cb->mask;
906ed7257bSPatrick Caulfield }
916ed7257bSPatrick Caulfield 
926ed7257bSPatrick Caulfield static bool cbuf_empty(struct cbuf *cb)
936ed7257bSPatrick Caulfield {
946ed7257bSPatrick Caulfield 	return cb->len == 0;
956ed7257bSPatrick Caulfield }
966ed7257bSPatrick Caulfield 
976ed7257bSPatrick Caulfield struct connection {
986ed7257bSPatrick Caulfield 	struct socket *sock;	/* NULL if not connected */
996ed7257bSPatrick Caulfield 	uint32_t nodeid;	/* So we know who we are in the list */
1006ed7257bSPatrick Caulfield 	struct mutex sock_mutex;
1016ed7257bSPatrick Caulfield 	unsigned long flags;
1026ed7257bSPatrick Caulfield #define CF_READ_PENDING 1
1036ed7257bSPatrick Caulfield #define CF_WRITE_PENDING 2
1046ed7257bSPatrick Caulfield #define CF_CONNECT_PENDING 3
1056ed7257bSPatrick Caulfield #define CF_INIT_PENDING 4
1066ed7257bSPatrick Caulfield #define CF_IS_OTHERCON 5
1076ed7257bSPatrick Caulfield 	struct list_head writequeue;  /* List of outgoing writequeue_entries */
1086ed7257bSPatrick Caulfield 	spinlock_t writequeue_lock;
1096ed7257bSPatrick Caulfield 	int (*rx_action) (struct connection *);	/* What to do when active */
1106ed7257bSPatrick Caulfield 	void (*connect_action) (struct connection *);	/* What to do to connect */
1116ed7257bSPatrick Caulfield 	struct page *rx_page;
1126ed7257bSPatrick Caulfield 	struct cbuf cb;
1136ed7257bSPatrick Caulfield 	int retries;
1146ed7257bSPatrick Caulfield #define MAX_CONNECT_RETRIES 3
1156ed7257bSPatrick Caulfield 	int sctp_assoc;
1166ed7257bSPatrick Caulfield 	struct connection *othercon;
1176ed7257bSPatrick Caulfield 	struct work_struct rwork; /* Receive workqueue */
1186ed7257bSPatrick Caulfield 	struct work_struct swork; /* Send workqueue */
1196ed7257bSPatrick Caulfield };
1206ed7257bSPatrick Caulfield #define sock2con(x) ((struct connection *)(x)->sk_user_data)
1216ed7257bSPatrick Caulfield 
1226ed7257bSPatrick Caulfield /* An entry waiting to be sent */
1236ed7257bSPatrick Caulfield struct writequeue_entry {
1246ed7257bSPatrick Caulfield 	struct list_head list;
1256ed7257bSPatrick Caulfield 	struct page *page;
1266ed7257bSPatrick Caulfield 	int offset;
1276ed7257bSPatrick Caulfield 	int len;
1286ed7257bSPatrick Caulfield 	int end;
1296ed7257bSPatrick Caulfield 	int users;
1306ed7257bSPatrick Caulfield 	struct connection *con;
1316ed7257bSPatrick Caulfield };
1326ed7257bSPatrick Caulfield 
1336ed7257bSPatrick Caulfield static struct sockaddr_storage *dlm_local_addr[DLM_MAX_ADDR_COUNT];
1346ed7257bSPatrick Caulfield static int dlm_local_count;
1356ed7257bSPatrick Caulfield 
1366ed7257bSPatrick Caulfield /* Work queues */
1376ed7257bSPatrick Caulfield static struct workqueue_struct *recv_workqueue;
1386ed7257bSPatrick Caulfield static struct workqueue_struct *send_workqueue;
1396ed7257bSPatrick Caulfield 
1406ed7257bSPatrick Caulfield static DEFINE_IDR(connections_idr);
1416ed7257bSPatrick Caulfield static DECLARE_MUTEX(connections_lock);
1426ed7257bSPatrick Caulfield static int max_nodeid;
1436ed7257bSPatrick Caulfield static struct kmem_cache *con_cache;
1446ed7257bSPatrick Caulfield 
1456ed7257bSPatrick Caulfield static void process_recv_sockets(struct work_struct *work);
1466ed7257bSPatrick Caulfield static void process_send_sockets(struct work_struct *work);
1476ed7257bSPatrick Caulfield 
1486ed7257bSPatrick Caulfield /*
1496ed7257bSPatrick Caulfield  * If 'allocation' is zero then we don't attempt to create a new
1506ed7257bSPatrick Caulfield  * connection structure for this node.
1516ed7257bSPatrick Caulfield  */
1526ed7257bSPatrick Caulfield static struct connection *__nodeid2con(int nodeid, gfp_t alloc)
1536ed7257bSPatrick Caulfield {
1546ed7257bSPatrick Caulfield 	struct connection *con = NULL;
1556ed7257bSPatrick Caulfield 	int r;
1566ed7257bSPatrick Caulfield 	int n;
1576ed7257bSPatrick Caulfield 
1586ed7257bSPatrick Caulfield 	con = idr_find(&connections_idr, nodeid);
1596ed7257bSPatrick Caulfield 	if (con || !alloc)
1606ed7257bSPatrick Caulfield 		return con;
1616ed7257bSPatrick Caulfield 
1626ed7257bSPatrick Caulfield 	r = idr_pre_get(&connections_idr, alloc);
1636ed7257bSPatrick Caulfield 	if (!r)
1646ed7257bSPatrick Caulfield 		return NULL;
1656ed7257bSPatrick Caulfield 
1666ed7257bSPatrick Caulfield 	con = kmem_cache_zalloc(con_cache, alloc);
1676ed7257bSPatrick Caulfield 	if (!con)
1686ed7257bSPatrick Caulfield 		return NULL;
1696ed7257bSPatrick Caulfield 
1706ed7257bSPatrick Caulfield 	r = idr_get_new_above(&connections_idr, con, nodeid, &n);
1716ed7257bSPatrick Caulfield 	if (r) {
1726ed7257bSPatrick Caulfield 		kmem_cache_free(con_cache, con);
1736ed7257bSPatrick Caulfield 		return NULL;
1746ed7257bSPatrick Caulfield 	}
1756ed7257bSPatrick Caulfield 
1766ed7257bSPatrick Caulfield 	if (n != nodeid) {
1776ed7257bSPatrick Caulfield 		idr_remove(&connections_idr, n);
1786ed7257bSPatrick Caulfield 		kmem_cache_free(con_cache, con);
1796ed7257bSPatrick Caulfield 		return NULL;
1806ed7257bSPatrick Caulfield 	}
1816ed7257bSPatrick Caulfield 
1826ed7257bSPatrick Caulfield 	con->nodeid = nodeid;
1836ed7257bSPatrick Caulfield 	mutex_init(&con->sock_mutex);
1846ed7257bSPatrick Caulfield 	INIT_LIST_HEAD(&con->writequeue);
1856ed7257bSPatrick Caulfield 	spin_lock_init(&con->writequeue_lock);
1866ed7257bSPatrick Caulfield 	INIT_WORK(&con->swork, process_send_sockets);
1876ed7257bSPatrick Caulfield 	INIT_WORK(&con->rwork, process_recv_sockets);
1886ed7257bSPatrick Caulfield 
1896ed7257bSPatrick Caulfield 	/* Setup action pointers for child sockets */
1906ed7257bSPatrick Caulfield 	if (con->nodeid) {
1916ed7257bSPatrick Caulfield 		struct connection *zerocon = idr_find(&connections_idr, 0);
1926ed7257bSPatrick Caulfield 
1936ed7257bSPatrick Caulfield 		con->connect_action = zerocon->connect_action;
1946ed7257bSPatrick Caulfield 		if (!con->rx_action)
1956ed7257bSPatrick Caulfield 			con->rx_action = zerocon->rx_action;
1966ed7257bSPatrick Caulfield 	}
1976ed7257bSPatrick Caulfield 
1986ed7257bSPatrick Caulfield 	if (nodeid > max_nodeid)
1996ed7257bSPatrick Caulfield 		max_nodeid = nodeid;
2006ed7257bSPatrick Caulfield 
2016ed7257bSPatrick Caulfield 	return con;
2026ed7257bSPatrick Caulfield }
2036ed7257bSPatrick Caulfield 
2046ed7257bSPatrick Caulfield static struct connection *nodeid2con(int nodeid, gfp_t allocation)
2056ed7257bSPatrick Caulfield {
2066ed7257bSPatrick Caulfield 	struct connection *con;
2076ed7257bSPatrick Caulfield 
2086ed7257bSPatrick Caulfield 	down(&connections_lock);
2096ed7257bSPatrick Caulfield 	con = __nodeid2con(nodeid, allocation);
2106ed7257bSPatrick Caulfield 	up(&connections_lock);
2116ed7257bSPatrick Caulfield 
2126ed7257bSPatrick Caulfield 	return con;
2136ed7257bSPatrick Caulfield }
2146ed7257bSPatrick Caulfield 
2156ed7257bSPatrick Caulfield /* This is a bit drastic, but only called when things go wrong */
2166ed7257bSPatrick Caulfield static struct connection *assoc2con(int assoc_id)
2176ed7257bSPatrick Caulfield {
2186ed7257bSPatrick Caulfield 	int i;
2196ed7257bSPatrick Caulfield 	struct connection *con;
2206ed7257bSPatrick Caulfield 
2216ed7257bSPatrick Caulfield 	down(&connections_lock);
22230d3a237SPatrick Caulfield 	for (i=0; i<=max_nodeid; i++) {
2236ed7257bSPatrick Caulfield 		con = __nodeid2con(i, 0);
2246ed7257bSPatrick Caulfield 		if (con && con->sctp_assoc == assoc_id) {
2256ed7257bSPatrick Caulfield 			up(&connections_lock);
2266ed7257bSPatrick Caulfield 			return con;
2276ed7257bSPatrick Caulfield 		}
2286ed7257bSPatrick Caulfield 	}
2296ed7257bSPatrick Caulfield 	up(&connections_lock);
2306ed7257bSPatrick Caulfield 	return NULL;
2316ed7257bSPatrick Caulfield }
2326ed7257bSPatrick Caulfield 
2336ed7257bSPatrick Caulfield static int nodeid_to_addr(int nodeid, struct sockaddr *retaddr)
2346ed7257bSPatrick Caulfield {
2356ed7257bSPatrick Caulfield 	struct sockaddr_storage addr;
2366ed7257bSPatrick Caulfield 	int error;
2376ed7257bSPatrick Caulfield 
2386ed7257bSPatrick Caulfield 	if (!dlm_local_count)
2396ed7257bSPatrick Caulfield 		return -1;
2406ed7257bSPatrick Caulfield 
2416ed7257bSPatrick Caulfield 	error = dlm_nodeid_to_addr(nodeid, &addr);
2426ed7257bSPatrick Caulfield 	if (error)
2436ed7257bSPatrick Caulfield 		return error;
2446ed7257bSPatrick Caulfield 
2456ed7257bSPatrick Caulfield 	if (dlm_local_addr[0]->ss_family == AF_INET) {
2466ed7257bSPatrick Caulfield 		struct sockaddr_in *in4  = (struct sockaddr_in *) &addr;
2476ed7257bSPatrick Caulfield 		struct sockaddr_in *ret4 = (struct sockaddr_in *) retaddr;
2486ed7257bSPatrick Caulfield 		ret4->sin_addr.s_addr = in4->sin_addr.s_addr;
2496ed7257bSPatrick Caulfield 	} else {
2506ed7257bSPatrick Caulfield 		struct sockaddr_in6 *in6  = (struct sockaddr_in6 *) &addr;
2516ed7257bSPatrick Caulfield 		struct sockaddr_in6 *ret6 = (struct sockaddr_in6 *) retaddr;
2526ed7257bSPatrick Caulfield 		memcpy(&ret6->sin6_addr, &in6->sin6_addr,
2536ed7257bSPatrick Caulfield 		       sizeof(in6->sin6_addr));
2546ed7257bSPatrick Caulfield 	}
2556ed7257bSPatrick Caulfield 
2566ed7257bSPatrick Caulfield 	return 0;
2576ed7257bSPatrick Caulfield }
2586ed7257bSPatrick Caulfield 
2596ed7257bSPatrick Caulfield /* Data available on socket or listen socket received a connect */
2606ed7257bSPatrick Caulfield static void lowcomms_data_ready(struct sock *sk, int count_unused)
2616ed7257bSPatrick Caulfield {
2626ed7257bSPatrick Caulfield 	struct connection *con = sock2con(sk);
263afb853fbSPatrick Caulfield 	if (con && !test_and_set_bit(CF_READ_PENDING, &con->flags))
2646ed7257bSPatrick Caulfield 		queue_work(recv_workqueue, &con->rwork);
2656ed7257bSPatrick Caulfield }
2666ed7257bSPatrick Caulfield 
2676ed7257bSPatrick Caulfield static void lowcomms_write_space(struct sock *sk)
2686ed7257bSPatrick Caulfield {
2696ed7257bSPatrick Caulfield 	struct connection *con = sock2con(sk);
2706ed7257bSPatrick Caulfield 
271afb853fbSPatrick Caulfield 	if (con && !test_and_set_bit(CF_WRITE_PENDING, &con->flags))
2726ed7257bSPatrick Caulfield 		queue_work(send_workqueue, &con->swork);
2736ed7257bSPatrick Caulfield }
2746ed7257bSPatrick Caulfield 
2756ed7257bSPatrick Caulfield static inline void lowcomms_connect_sock(struct connection *con)
2766ed7257bSPatrick Caulfield {
2776ed7257bSPatrick Caulfield 	if (!test_and_set_bit(CF_CONNECT_PENDING, &con->flags))
2786ed7257bSPatrick Caulfield 		queue_work(send_workqueue, &con->swork);
2796ed7257bSPatrick Caulfield }
2806ed7257bSPatrick Caulfield 
2816ed7257bSPatrick Caulfield static void lowcomms_state_change(struct sock *sk)
2826ed7257bSPatrick Caulfield {
2836ed7257bSPatrick Caulfield 	if (sk->sk_state == TCP_ESTABLISHED)
2846ed7257bSPatrick Caulfield 		lowcomms_write_space(sk);
2856ed7257bSPatrick Caulfield }
2866ed7257bSPatrick Caulfield 
2876ed7257bSPatrick Caulfield /* Make a socket active */
2886ed7257bSPatrick Caulfield static int add_sock(struct socket *sock, struct connection *con)
2896ed7257bSPatrick Caulfield {
2906ed7257bSPatrick Caulfield 	con->sock = sock;
2916ed7257bSPatrick Caulfield 
2926ed7257bSPatrick Caulfield 	/* Install a data_ready callback */
2936ed7257bSPatrick Caulfield 	con->sock->sk->sk_data_ready = lowcomms_data_ready;
2946ed7257bSPatrick Caulfield 	con->sock->sk->sk_write_space = lowcomms_write_space;
2956ed7257bSPatrick Caulfield 	con->sock->sk->sk_state_change = lowcomms_state_change;
2966ed7257bSPatrick Caulfield 	con->sock->sk->sk_user_data = con;
2976ed7257bSPatrick Caulfield 	return 0;
2986ed7257bSPatrick Caulfield }
2996ed7257bSPatrick Caulfield 
3006ed7257bSPatrick Caulfield /* Add the port number to an IPv6 or 4 sockaddr and return the address
3016ed7257bSPatrick Caulfield    length */
3026ed7257bSPatrick Caulfield static void make_sockaddr(struct sockaddr_storage *saddr, uint16_t port,
3036ed7257bSPatrick Caulfield 			  int *addr_len)
3046ed7257bSPatrick Caulfield {
3056ed7257bSPatrick Caulfield 	saddr->ss_family =  dlm_local_addr[0]->ss_family;
3066ed7257bSPatrick Caulfield 	if (saddr->ss_family == AF_INET) {
3076ed7257bSPatrick Caulfield 		struct sockaddr_in *in4_addr = (struct sockaddr_in *)saddr;
3086ed7257bSPatrick Caulfield 		in4_addr->sin_port = cpu_to_be16(port);
3096ed7257bSPatrick Caulfield 		*addr_len = sizeof(struct sockaddr_in);
3106ed7257bSPatrick Caulfield 		memset(&in4_addr->sin_zero, 0, sizeof(in4_addr->sin_zero));
3116ed7257bSPatrick Caulfield 	} else {
3126ed7257bSPatrick Caulfield 		struct sockaddr_in6 *in6_addr = (struct sockaddr_in6 *)saddr;
3136ed7257bSPatrick Caulfield 		in6_addr->sin6_port = cpu_to_be16(port);
3146ed7257bSPatrick Caulfield 		*addr_len = sizeof(struct sockaddr_in6);
3156ed7257bSPatrick Caulfield 	}
31601c8cab2SPatrick Caulfield 	memset((char *)saddr + *addr_len, 0, sizeof(struct sockaddr_storage) - *addr_len);
3176ed7257bSPatrick Caulfield }
3186ed7257bSPatrick Caulfield 
3196ed7257bSPatrick Caulfield /* Close a remote connection and tidy up */
3206ed7257bSPatrick Caulfield static void close_connection(struct connection *con, bool and_other)
3216ed7257bSPatrick Caulfield {
3226ed7257bSPatrick Caulfield 	mutex_lock(&con->sock_mutex);
3236ed7257bSPatrick Caulfield 
3246ed7257bSPatrick Caulfield 	if (con->sock) {
3256ed7257bSPatrick Caulfield 		sock_release(con->sock);
3266ed7257bSPatrick Caulfield 		con->sock = NULL;
3276ed7257bSPatrick Caulfield 	}
3286ed7257bSPatrick Caulfield 	if (con->othercon && and_other) {
3296ed7257bSPatrick Caulfield 		/* Will only re-enter once. */
3306ed7257bSPatrick Caulfield 		close_connection(con->othercon, false);
3316ed7257bSPatrick Caulfield 	}
3326ed7257bSPatrick Caulfield 	if (con->rx_page) {
3336ed7257bSPatrick Caulfield 		__free_page(con->rx_page);
3346ed7257bSPatrick Caulfield 		con->rx_page = NULL;
3356ed7257bSPatrick Caulfield 	}
3369e5f2825SPatrick Caulfield 
3376ed7257bSPatrick Caulfield 	con->retries = 0;
3386ed7257bSPatrick Caulfield 	mutex_unlock(&con->sock_mutex);
3396ed7257bSPatrick Caulfield }
3406ed7257bSPatrick Caulfield 
3416ed7257bSPatrick Caulfield /* We only send shutdown messages to nodes that are not part of the cluster */
3426ed7257bSPatrick Caulfield static void sctp_send_shutdown(sctp_assoc_t associd)
3436ed7257bSPatrick Caulfield {
3446ed7257bSPatrick Caulfield 	static char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
3456ed7257bSPatrick Caulfield 	struct msghdr outmessage;
3466ed7257bSPatrick Caulfield 	struct cmsghdr *cmsg;
3476ed7257bSPatrick Caulfield 	struct sctp_sndrcvinfo *sinfo;
3486ed7257bSPatrick Caulfield 	int ret;
3496ed7257bSPatrick Caulfield 	struct connection *con;
3506ed7257bSPatrick Caulfield 
3516ed7257bSPatrick Caulfield 	con = nodeid2con(0,0);
3526ed7257bSPatrick Caulfield 	BUG_ON(con == NULL);
3536ed7257bSPatrick Caulfield 
3546ed7257bSPatrick Caulfield 	outmessage.msg_name = NULL;
3556ed7257bSPatrick Caulfield 	outmessage.msg_namelen = 0;
3566ed7257bSPatrick Caulfield 	outmessage.msg_control = outcmsg;
3576ed7257bSPatrick Caulfield 	outmessage.msg_controllen = sizeof(outcmsg);
3586ed7257bSPatrick Caulfield 	outmessage.msg_flags = MSG_EOR;
3596ed7257bSPatrick Caulfield 
3606ed7257bSPatrick Caulfield 	cmsg = CMSG_FIRSTHDR(&outmessage);
3616ed7257bSPatrick Caulfield 	cmsg->cmsg_level = IPPROTO_SCTP;
3626ed7257bSPatrick Caulfield 	cmsg->cmsg_type = SCTP_SNDRCV;
3636ed7257bSPatrick Caulfield 	cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
3646ed7257bSPatrick Caulfield 	outmessage.msg_controllen = cmsg->cmsg_len;
3656ed7257bSPatrick Caulfield 	sinfo = CMSG_DATA(cmsg);
3666ed7257bSPatrick Caulfield 	memset(sinfo, 0x00, sizeof(struct sctp_sndrcvinfo));
3676ed7257bSPatrick Caulfield 
3686ed7257bSPatrick Caulfield 	sinfo->sinfo_flags |= MSG_EOF;
3696ed7257bSPatrick Caulfield 	sinfo->sinfo_assoc_id = associd;
3706ed7257bSPatrick Caulfield 
3716ed7257bSPatrick Caulfield 	ret = kernel_sendmsg(con->sock, &outmessage, NULL, 0, 0);
3726ed7257bSPatrick Caulfield 
3736ed7257bSPatrick Caulfield 	if (ret != 0)
3746ed7257bSPatrick Caulfield 		log_print("send EOF to node failed: %d", ret);
3756ed7257bSPatrick Caulfield }
3766ed7257bSPatrick Caulfield 
3776ed7257bSPatrick Caulfield /* INIT failed but we don't know which node...
3786ed7257bSPatrick Caulfield    restart INIT on all pending nodes */
3796ed7257bSPatrick Caulfield static void sctp_init_failed(void)
3806ed7257bSPatrick Caulfield {
3816ed7257bSPatrick Caulfield 	int i;
3826ed7257bSPatrick Caulfield 	struct connection *con;
3836ed7257bSPatrick Caulfield 
3846ed7257bSPatrick Caulfield 	down(&connections_lock);
3856ed7257bSPatrick Caulfield 	for (i=1; i<=max_nodeid; i++) {
3866ed7257bSPatrick Caulfield 		con = __nodeid2con(i, 0);
3876ed7257bSPatrick Caulfield 		if (!con)
3886ed7257bSPatrick Caulfield 			continue;
3896ed7257bSPatrick Caulfield 		con->sctp_assoc = 0;
3906ed7257bSPatrick Caulfield 		if (test_and_clear_bit(CF_CONNECT_PENDING, &con->flags)) {
3916ed7257bSPatrick Caulfield 			if (!test_and_set_bit(CF_WRITE_PENDING, &con->flags)) {
3926ed7257bSPatrick Caulfield 				queue_work(send_workqueue, &con->swork);
3936ed7257bSPatrick Caulfield 			}
3946ed7257bSPatrick Caulfield 		}
3956ed7257bSPatrick Caulfield 	}
3966ed7257bSPatrick Caulfield 	up(&connections_lock);
3976ed7257bSPatrick Caulfield }
3986ed7257bSPatrick Caulfield 
3996ed7257bSPatrick Caulfield /* Something happened to an association */
400617e82e1SDavid Teigland static void process_sctp_notification(struct connection *con,
401617e82e1SDavid Teigland 				      struct msghdr *msg, char *buf)
4026ed7257bSPatrick Caulfield {
4036ed7257bSPatrick Caulfield 	union sctp_notification *sn = (union sctp_notification *)buf;
4046ed7257bSPatrick Caulfield 
4056ed7257bSPatrick Caulfield 	if (sn->sn_header.sn_type == SCTP_ASSOC_CHANGE) {
4066ed7257bSPatrick Caulfield 		switch (sn->sn_assoc_change.sac_state) {
4076ed7257bSPatrick Caulfield 
4086ed7257bSPatrick Caulfield 		case SCTP_COMM_UP:
4096ed7257bSPatrick Caulfield 		case SCTP_RESTART:
4106ed7257bSPatrick Caulfield 		{
4116ed7257bSPatrick Caulfield 			/* Check that the new node is in the lockspace */
4126ed7257bSPatrick Caulfield 			struct sctp_prim prim;
4136ed7257bSPatrick Caulfield 			int nodeid;
4146ed7257bSPatrick Caulfield 			int prim_len, ret;
4156ed7257bSPatrick Caulfield 			int addr_len;
4166ed7257bSPatrick Caulfield 			struct connection *new_con;
4176ed7257bSPatrick Caulfield 			struct file *file;
4186ed7257bSPatrick Caulfield 			sctp_peeloff_arg_t parg;
4196ed7257bSPatrick Caulfield 			int parglen = sizeof(parg);
4206ed7257bSPatrick Caulfield 
4216ed7257bSPatrick Caulfield 			/*
4226ed7257bSPatrick Caulfield 			 * We get this before any data for an association.
4236ed7257bSPatrick Caulfield 			 * We verify that the node is in the cluster and
4246ed7257bSPatrick Caulfield 			 * then peel off a socket for it.
4256ed7257bSPatrick Caulfield 			 */
4266ed7257bSPatrick Caulfield 			if ((int)sn->sn_assoc_change.sac_assoc_id <= 0) {
4276ed7257bSPatrick Caulfield 				log_print("COMM_UP for invalid assoc ID %d",
4286ed7257bSPatrick Caulfield 					 (int)sn->sn_assoc_change.sac_assoc_id);
4296ed7257bSPatrick Caulfield 				sctp_init_failed();
4306ed7257bSPatrick Caulfield 				return;
4316ed7257bSPatrick Caulfield 			}
4326ed7257bSPatrick Caulfield 			memset(&prim, 0, sizeof(struct sctp_prim));
4336ed7257bSPatrick Caulfield 			prim_len = sizeof(struct sctp_prim);
4346ed7257bSPatrick Caulfield 			prim.ssp_assoc_id = sn->sn_assoc_change.sac_assoc_id;
4356ed7257bSPatrick Caulfield 
4366ed7257bSPatrick Caulfield 			ret = kernel_getsockopt(con->sock,
4376ed7257bSPatrick Caulfield 						IPPROTO_SCTP,
4386ed7257bSPatrick Caulfield 						SCTP_PRIMARY_ADDR,
4396ed7257bSPatrick Caulfield 						(char*)&prim,
4406ed7257bSPatrick Caulfield 						&prim_len);
4416ed7257bSPatrick Caulfield 			if (ret < 0) {
4426ed7257bSPatrick Caulfield 				log_print("getsockopt/sctp_primary_addr on "
4436ed7257bSPatrick Caulfield 					  "new assoc %d failed : %d",
4446ed7257bSPatrick Caulfield 					  (int)sn->sn_assoc_change.sac_assoc_id,
4456ed7257bSPatrick Caulfield 					  ret);
4466ed7257bSPatrick Caulfield 
4476ed7257bSPatrick Caulfield 				/* Retry INIT later */
4486ed7257bSPatrick Caulfield 				new_con = assoc2con(sn->sn_assoc_change.sac_assoc_id);
4496ed7257bSPatrick Caulfield 				if (new_con)
4506ed7257bSPatrick Caulfield 					clear_bit(CF_CONNECT_PENDING, &con->flags);
4516ed7257bSPatrick Caulfield 				return;
4526ed7257bSPatrick Caulfield 			}
4536ed7257bSPatrick Caulfield 			make_sockaddr(&prim.ssp_addr, 0, &addr_len);
4546ed7257bSPatrick Caulfield 			if (dlm_addr_to_nodeid(&prim.ssp_addr, &nodeid)) {
4556ed7257bSPatrick Caulfield 				int i;
4566ed7257bSPatrick Caulfield 				unsigned char *b=(unsigned char *)&prim.ssp_addr;
4576ed7257bSPatrick Caulfield 				log_print("reject connect from unknown addr");
4586ed7257bSPatrick Caulfield 				for (i=0; i<sizeof(struct sockaddr_storage);i++)
4596ed7257bSPatrick Caulfield 					printk("%02x ", b[i]);
4606ed7257bSPatrick Caulfield 				printk("\n");
4616ed7257bSPatrick Caulfield 				sctp_send_shutdown(prim.ssp_assoc_id);
4626ed7257bSPatrick Caulfield 				return;
4636ed7257bSPatrick Caulfield 			}
4646ed7257bSPatrick Caulfield 
4656ed7257bSPatrick Caulfield 			new_con = nodeid2con(nodeid, GFP_KERNEL);
4666ed7257bSPatrick Caulfield 			if (!new_con)
4676ed7257bSPatrick Caulfield 				return;
4686ed7257bSPatrick Caulfield 
4696ed7257bSPatrick Caulfield 			/* Peel off a new sock */
4706ed7257bSPatrick Caulfield 			parg.associd = sn->sn_assoc_change.sac_assoc_id;
471617e82e1SDavid Teigland 			ret = kernel_getsockopt(con->sock, IPPROTO_SCTP,
472617e82e1SDavid Teigland 						SCTP_SOCKOPT_PEELOFF,
4736ed7257bSPatrick Caulfield 						(void *)&parg, &parglen);
47430d3a237SPatrick Caulfield 			if (ret) {
475617e82e1SDavid Teigland 				log_print("Can't peel off a socket for "
476617e82e1SDavid Teigland 					  "connection %d to node %d: err=%d\n",
4776ed7257bSPatrick Caulfield 					  parg.associd, nodeid, ret);
4786ed7257bSPatrick Caulfield 			}
4796ed7257bSPatrick Caulfield 			file = fget(parg.sd);
4806ed7257bSPatrick Caulfield 			new_con->sock = SOCKET_I(file->f_dentry->d_inode);
4816ed7257bSPatrick Caulfield 			add_sock(new_con->sock, new_con);
4826ed7257bSPatrick Caulfield 			fput(file);
4836ed7257bSPatrick Caulfield 			put_unused_fd(parg.sd);
4846ed7257bSPatrick Caulfield 
4856ed7257bSPatrick Caulfield 			log_print("got new/restarted association %d nodeid %d",
4866ed7257bSPatrick Caulfield 				 (int)sn->sn_assoc_change.sac_assoc_id, nodeid);
4876ed7257bSPatrick Caulfield 
4886ed7257bSPatrick Caulfield 			/* Send any pending writes */
4896ed7257bSPatrick Caulfield 			clear_bit(CF_CONNECT_PENDING, &new_con->flags);
4906ed7257bSPatrick Caulfield 			clear_bit(CF_INIT_PENDING, &con->flags);
4916ed7257bSPatrick Caulfield 			if (!test_and_set_bit(CF_WRITE_PENDING, &new_con->flags)) {
4926ed7257bSPatrick Caulfield 				queue_work(send_workqueue, &new_con->swork);
4936ed7257bSPatrick Caulfield 			}
4946ed7257bSPatrick Caulfield 			if (!test_and_set_bit(CF_READ_PENDING, &new_con->flags))
4956ed7257bSPatrick Caulfield 				queue_work(recv_workqueue, &new_con->rwork);
4966ed7257bSPatrick Caulfield 		}
4976ed7257bSPatrick Caulfield 		break;
4986ed7257bSPatrick Caulfield 
4996ed7257bSPatrick Caulfield 		case SCTP_COMM_LOST:
5006ed7257bSPatrick Caulfield 		case SCTP_SHUTDOWN_COMP:
5016ed7257bSPatrick Caulfield 		{
5026ed7257bSPatrick Caulfield 			con = assoc2con(sn->sn_assoc_change.sac_assoc_id);
5036ed7257bSPatrick Caulfield 			if (con) {
5046ed7257bSPatrick Caulfield 				con->sctp_assoc = 0;
5056ed7257bSPatrick Caulfield 			}
5066ed7257bSPatrick Caulfield 		}
5076ed7257bSPatrick Caulfield 		break;
5086ed7257bSPatrick Caulfield 
5096ed7257bSPatrick Caulfield 		/* We don't know which INIT failed, so clear the PENDING flags
5106ed7257bSPatrick Caulfield 		 * on them all.  if assoc_id is zero then it will then try
5116ed7257bSPatrick Caulfield 		 * again */
5126ed7257bSPatrick Caulfield 
5136ed7257bSPatrick Caulfield 		case SCTP_CANT_STR_ASSOC:
5146ed7257bSPatrick Caulfield 		{
5156ed7257bSPatrick Caulfield 			log_print("Can't start SCTP association - retrying");
5166ed7257bSPatrick Caulfield 			sctp_init_failed();
5176ed7257bSPatrick Caulfield 		}
5186ed7257bSPatrick Caulfield 		break;
5196ed7257bSPatrick Caulfield 
5206ed7257bSPatrick Caulfield 		default:
5216ed7257bSPatrick Caulfield 			log_print("unexpected SCTP assoc change id=%d state=%d",
5226ed7257bSPatrick Caulfield 				  (int)sn->sn_assoc_change.sac_assoc_id,
5236ed7257bSPatrick Caulfield 				  sn->sn_assoc_change.sac_state);
5246ed7257bSPatrick Caulfield 		}
5256ed7257bSPatrick Caulfield 	}
5266ed7257bSPatrick Caulfield }
5276ed7257bSPatrick Caulfield 
5286ed7257bSPatrick Caulfield /* Data received from remote end */
5296ed7257bSPatrick Caulfield static int receive_from_sock(struct connection *con)
5306ed7257bSPatrick Caulfield {
5316ed7257bSPatrick Caulfield 	int ret = 0;
5326ed7257bSPatrick Caulfield 	struct msghdr msg = {};
5336ed7257bSPatrick Caulfield 	struct kvec iov[2];
5346ed7257bSPatrick Caulfield 	unsigned len;
5356ed7257bSPatrick Caulfield 	int r;
5366ed7257bSPatrick Caulfield 	int call_again_soon = 0;
5376ed7257bSPatrick Caulfield 	int nvec;
5386ed7257bSPatrick Caulfield 	char incmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
5396ed7257bSPatrick Caulfield 
5406ed7257bSPatrick Caulfield 	mutex_lock(&con->sock_mutex);
5416ed7257bSPatrick Caulfield 
5426ed7257bSPatrick Caulfield 	if (con->sock == NULL) {
5436ed7257bSPatrick Caulfield 		ret = -EAGAIN;
5446ed7257bSPatrick Caulfield 		goto out_close;
5456ed7257bSPatrick Caulfield 	}
5466ed7257bSPatrick Caulfield 
5476ed7257bSPatrick Caulfield 	if (con->rx_page == NULL) {
5486ed7257bSPatrick Caulfield 		/*
5496ed7257bSPatrick Caulfield 		 * This doesn't need to be atomic, but I think it should
5506ed7257bSPatrick Caulfield 		 * improve performance if it is.
5516ed7257bSPatrick Caulfield 		 */
5526ed7257bSPatrick Caulfield 		con->rx_page = alloc_page(GFP_ATOMIC);
5536ed7257bSPatrick Caulfield 		if (con->rx_page == NULL)
5546ed7257bSPatrick Caulfield 			goto out_resched;
5556ed7257bSPatrick Caulfield 		cbuf_init(&con->cb, PAGE_CACHE_SIZE);
5566ed7257bSPatrick Caulfield 	}
5576ed7257bSPatrick Caulfield 
5586ed7257bSPatrick Caulfield 	/* Only SCTP needs these really */
5596ed7257bSPatrick Caulfield 	memset(&incmsg, 0, sizeof(incmsg));
5606ed7257bSPatrick Caulfield 	msg.msg_control = incmsg;
5616ed7257bSPatrick Caulfield 	msg.msg_controllen = sizeof(incmsg);
5626ed7257bSPatrick Caulfield 
5636ed7257bSPatrick Caulfield 	/*
5646ed7257bSPatrick Caulfield 	 * iov[0] is the bit of the circular buffer between the current end
5656ed7257bSPatrick Caulfield 	 * point (cb.base + cb.len) and the end of the buffer.
5666ed7257bSPatrick Caulfield 	 */
5676ed7257bSPatrick Caulfield 	iov[0].iov_len = con->cb.base - cbuf_data(&con->cb);
5686ed7257bSPatrick Caulfield 	iov[0].iov_base = page_address(con->rx_page) + cbuf_data(&con->cb);
5696ed7257bSPatrick Caulfield 	iov[1].iov_len = 0;
5706ed7257bSPatrick Caulfield 	nvec = 1;
5716ed7257bSPatrick Caulfield 
5726ed7257bSPatrick Caulfield 	/*
5736ed7257bSPatrick Caulfield 	 * iov[1] is the bit of the circular buffer between the start of the
5746ed7257bSPatrick Caulfield 	 * buffer and the start of the currently used section (cb.base)
5756ed7257bSPatrick Caulfield 	 */
5766ed7257bSPatrick Caulfield 	if (cbuf_data(&con->cb) >= con->cb.base) {
5776ed7257bSPatrick Caulfield 		iov[0].iov_len = PAGE_CACHE_SIZE - cbuf_data(&con->cb);
5786ed7257bSPatrick Caulfield 		iov[1].iov_len = con->cb.base;
5796ed7257bSPatrick Caulfield 		iov[1].iov_base = page_address(con->rx_page);
5806ed7257bSPatrick Caulfield 		nvec = 2;
5816ed7257bSPatrick Caulfield 	}
5826ed7257bSPatrick Caulfield 	len = iov[0].iov_len + iov[1].iov_len;
5836ed7257bSPatrick Caulfield 
5846ed7257bSPatrick Caulfield 	r = ret = kernel_recvmsg(con->sock, &msg, iov, nvec, len,
5856ed7257bSPatrick Caulfield 			       MSG_DONTWAIT | MSG_NOSIGNAL);
5866ed7257bSPatrick Caulfield 	if (ret <= 0)
5876ed7257bSPatrick Caulfield 		goto out_close;
5886ed7257bSPatrick Caulfield 
5896ed7257bSPatrick Caulfield 	/* Process SCTP notifications */
5906ed7257bSPatrick Caulfield 	if (msg.msg_flags & MSG_NOTIFICATION) {
5916ed7257bSPatrick Caulfield 		msg.msg_control = incmsg;
5926ed7257bSPatrick Caulfield 		msg.msg_controllen = sizeof(incmsg);
5936ed7257bSPatrick Caulfield 
5946ed7257bSPatrick Caulfield 		process_sctp_notification(con, &msg,
5956ed7257bSPatrick Caulfield 				page_address(con->rx_page) + con->cb.base);
5966ed7257bSPatrick Caulfield 		mutex_unlock(&con->sock_mutex);
5976ed7257bSPatrick Caulfield 		return 0;
5986ed7257bSPatrick Caulfield 	}
5996ed7257bSPatrick Caulfield 	BUG_ON(con->nodeid == 0);
6006ed7257bSPatrick Caulfield 
6016ed7257bSPatrick Caulfield 	if (ret == len)
6026ed7257bSPatrick Caulfield 		call_again_soon = 1;
6036ed7257bSPatrick Caulfield 	cbuf_add(&con->cb, ret);
6046ed7257bSPatrick Caulfield 	ret = dlm_process_incoming_buffer(con->nodeid,
6056ed7257bSPatrick Caulfield 					  page_address(con->rx_page),
6066ed7257bSPatrick Caulfield 					  con->cb.base, con->cb.len,
6076ed7257bSPatrick Caulfield 					  PAGE_CACHE_SIZE);
6086ed7257bSPatrick Caulfield 	if (ret == -EBADMSG) {
609617e82e1SDavid Teigland 		log_print("lowcomms: addr=%p, base=%u, len=%u, "
610617e82e1SDavid Teigland 			  "iov_len=%u, iov_base[0]=%p, read=%d",
6116ed7257bSPatrick Caulfield 			  page_address(con->rx_page), con->cb.base, con->cb.len,
6126ed7257bSPatrick Caulfield 			  len, iov[0].iov_base, r);
6136ed7257bSPatrick Caulfield 	}
6146ed7257bSPatrick Caulfield 	if (ret < 0)
6156ed7257bSPatrick Caulfield 		goto out_close;
6166ed7257bSPatrick Caulfield 	cbuf_eat(&con->cb, ret);
6176ed7257bSPatrick Caulfield 
6186ed7257bSPatrick Caulfield 	if (cbuf_empty(&con->cb) && !call_again_soon) {
6196ed7257bSPatrick Caulfield 		__free_page(con->rx_page);
6206ed7257bSPatrick Caulfield 		con->rx_page = NULL;
6216ed7257bSPatrick Caulfield 	}
6226ed7257bSPatrick Caulfield 
6236ed7257bSPatrick Caulfield 	if (call_again_soon)
6246ed7257bSPatrick Caulfield 		goto out_resched;
6256ed7257bSPatrick Caulfield 	mutex_unlock(&con->sock_mutex);
6266ed7257bSPatrick Caulfield 	return 0;
6276ed7257bSPatrick Caulfield 
6286ed7257bSPatrick Caulfield out_resched:
6296ed7257bSPatrick Caulfield 	if (!test_and_set_bit(CF_READ_PENDING, &con->flags))
6306ed7257bSPatrick Caulfield 		queue_work(recv_workqueue, &con->rwork);
6316ed7257bSPatrick Caulfield 	mutex_unlock(&con->sock_mutex);
6326ed7257bSPatrick Caulfield 	return -EAGAIN;
6336ed7257bSPatrick Caulfield 
6346ed7257bSPatrick Caulfield out_close:
6356ed7257bSPatrick Caulfield 	mutex_unlock(&con->sock_mutex);
6369e5f2825SPatrick Caulfield 	if (ret != -EAGAIN) {
6376ed7257bSPatrick Caulfield 		close_connection(con, false);
6386ed7257bSPatrick Caulfield 		/* Reconnect when there is something to send */
6396ed7257bSPatrick Caulfield 	}
6406ed7257bSPatrick Caulfield 	/* Don't return success if we really got EOF */
6416ed7257bSPatrick Caulfield 	if (ret == 0)
6426ed7257bSPatrick Caulfield 		ret = -EAGAIN;
6436ed7257bSPatrick Caulfield 
6446ed7257bSPatrick Caulfield 	return ret;
6456ed7257bSPatrick Caulfield }
6466ed7257bSPatrick Caulfield 
6476ed7257bSPatrick Caulfield /* Listening socket is busy, accept a connection */
6486ed7257bSPatrick Caulfield static int tcp_accept_from_sock(struct connection *con)
6496ed7257bSPatrick Caulfield {
6506ed7257bSPatrick Caulfield 	int result;
6516ed7257bSPatrick Caulfield 	struct sockaddr_storage peeraddr;
6526ed7257bSPatrick Caulfield 	struct socket *newsock;
6536ed7257bSPatrick Caulfield 	int len;
6546ed7257bSPatrick Caulfield 	int nodeid;
6556ed7257bSPatrick Caulfield 	struct connection *newcon;
6566ed7257bSPatrick Caulfield 	struct connection *addcon;
6576ed7257bSPatrick Caulfield 
6586ed7257bSPatrick Caulfield 	memset(&peeraddr, 0, sizeof(peeraddr));
6596ed7257bSPatrick Caulfield 	result = sock_create_kern(dlm_local_addr[0]->ss_family, SOCK_STREAM,
6606ed7257bSPatrick Caulfield 				  IPPROTO_TCP, &newsock);
6616ed7257bSPatrick Caulfield 	if (result < 0)
6626ed7257bSPatrick Caulfield 		return -ENOMEM;
6636ed7257bSPatrick Caulfield 
6646ed7257bSPatrick Caulfield 	mutex_lock_nested(&con->sock_mutex, 0);
6656ed7257bSPatrick Caulfield 
6666ed7257bSPatrick Caulfield 	result = -ENOTCONN;
6676ed7257bSPatrick Caulfield 	if (con->sock == NULL)
6686ed7257bSPatrick Caulfield 		goto accept_err;
6696ed7257bSPatrick Caulfield 
6706ed7257bSPatrick Caulfield 	newsock->type = con->sock->type;
6716ed7257bSPatrick Caulfield 	newsock->ops = con->sock->ops;
6726ed7257bSPatrick Caulfield 
6736ed7257bSPatrick Caulfield 	result = con->sock->ops->accept(con->sock, newsock, O_NONBLOCK);
6746ed7257bSPatrick Caulfield 	if (result < 0)
6756ed7257bSPatrick Caulfield 		goto accept_err;
6766ed7257bSPatrick Caulfield 
6776ed7257bSPatrick Caulfield 	/* Get the connected socket's peer */
6786ed7257bSPatrick Caulfield 	memset(&peeraddr, 0, sizeof(peeraddr));
6796ed7257bSPatrick Caulfield 	if (newsock->ops->getname(newsock, (struct sockaddr *)&peeraddr,
6806ed7257bSPatrick Caulfield 				  &len, 2)) {
6816ed7257bSPatrick Caulfield 		result = -ECONNABORTED;
6826ed7257bSPatrick Caulfield 		goto accept_err;
6836ed7257bSPatrick Caulfield 	}
6846ed7257bSPatrick Caulfield 
6856ed7257bSPatrick Caulfield 	/* Get the new node's NODEID */
6866ed7257bSPatrick Caulfield 	make_sockaddr(&peeraddr, 0, &len);
6876ed7257bSPatrick Caulfield 	if (dlm_addr_to_nodeid(&peeraddr, &nodeid)) {
688617e82e1SDavid Teigland 		log_print("connect from non cluster node");
6896ed7257bSPatrick Caulfield 		sock_release(newsock);
6906ed7257bSPatrick Caulfield 		mutex_unlock(&con->sock_mutex);
6916ed7257bSPatrick Caulfield 		return -1;
6926ed7257bSPatrick Caulfield 	}
6936ed7257bSPatrick Caulfield 
6946ed7257bSPatrick Caulfield 	log_print("got connection from %d", nodeid);
6956ed7257bSPatrick Caulfield 
6966ed7257bSPatrick Caulfield 	/*  Check to see if we already have a connection to this node. This
6976ed7257bSPatrick Caulfield 	 *  could happen if the two nodes initiate a connection at roughly
6986ed7257bSPatrick Caulfield 	 *  the same time and the connections cross on the wire.
6996ed7257bSPatrick Caulfield 	 *  In this case we store the incoming one in "othercon"
7006ed7257bSPatrick Caulfield 	 */
7016ed7257bSPatrick Caulfield 	newcon = nodeid2con(nodeid, GFP_KERNEL);
7026ed7257bSPatrick Caulfield 	if (!newcon) {
7036ed7257bSPatrick Caulfield 		result = -ENOMEM;
7046ed7257bSPatrick Caulfield 		goto accept_err;
7056ed7257bSPatrick Caulfield 	}
7066ed7257bSPatrick Caulfield 	mutex_lock_nested(&newcon->sock_mutex, 1);
7076ed7257bSPatrick Caulfield 	if (newcon->sock) {
7086ed7257bSPatrick Caulfield 		struct connection *othercon = newcon->othercon;
7096ed7257bSPatrick Caulfield 
7106ed7257bSPatrick Caulfield 		if (!othercon) {
7116ed7257bSPatrick Caulfield 			othercon = kmem_cache_zalloc(con_cache, GFP_KERNEL);
7126ed7257bSPatrick Caulfield 			if (!othercon) {
713617e82e1SDavid Teigland 				log_print("failed to allocate incoming socket");
7146ed7257bSPatrick Caulfield 				mutex_unlock(&newcon->sock_mutex);
7156ed7257bSPatrick Caulfield 				result = -ENOMEM;
7166ed7257bSPatrick Caulfield 				goto accept_err;
7176ed7257bSPatrick Caulfield 			}
7186ed7257bSPatrick Caulfield 			othercon->nodeid = nodeid;
7196ed7257bSPatrick Caulfield 			othercon->rx_action = receive_from_sock;
7206ed7257bSPatrick Caulfield 			mutex_init(&othercon->sock_mutex);
7216ed7257bSPatrick Caulfield 			INIT_WORK(&othercon->swork, process_send_sockets);
7226ed7257bSPatrick Caulfield 			INIT_WORK(&othercon->rwork, process_recv_sockets);
7236ed7257bSPatrick Caulfield 			set_bit(CF_IS_OTHERCON, &othercon->flags);
72461d96be0SPatrick Caulfield 		}
72561d96be0SPatrick Caulfield 		if (!othercon->sock) {
7266ed7257bSPatrick Caulfield 			newcon->othercon = othercon;
7276ed7257bSPatrick Caulfield 			othercon->sock = newsock;
7286ed7257bSPatrick Caulfield 			newsock->sk->sk_user_data = othercon;
7296ed7257bSPatrick Caulfield 			add_sock(newsock, othercon);
7306ed7257bSPatrick Caulfield 			addcon = othercon;
7316ed7257bSPatrick Caulfield 		}
7326ed7257bSPatrick Caulfield 		else {
73397d84836SPatrick Caulfield 			printk("Extra connection from node %d attempted\n", nodeid);
73497d84836SPatrick Caulfield 			result = -EAGAIN;
735f4fadb23Sakpm@linux-foundation.org 			mutex_unlock(&newcon->sock_mutex);
73697d84836SPatrick Caulfield 			goto accept_err;
73797d84836SPatrick Caulfield 		}
73897d84836SPatrick Caulfield 	}
73997d84836SPatrick Caulfield 	else {
7406ed7257bSPatrick Caulfield 		newsock->sk->sk_user_data = newcon;
7416ed7257bSPatrick Caulfield 		newcon->rx_action = receive_from_sock;
7426ed7257bSPatrick Caulfield 		add_sock(newsock, newcon);
7436ed7257bSPatrick Caulfield 		addcon = newcon;
7446ed7257bSPatrick Caulfield 	}
7456ed7257bSPatrick Caulfield 
7466ed7257bSPatrick Caulfield 	mutex_unlock(&newcon->sock_mutex);
7476ed7257bSPatrick Caulfield 
7486ed7257bSPatrick Caulfield 	/*
7496ed7257bSPatrick Caulfield 	 * Add it to the active queue in case we got data
7506ed7257bSPatrick Caulfield 	 * beween processing the accept adding the socket
7516ed7257bSPatrick Caulfield 	 * to the read_sockets list
7526ed7257bSPatrick Caulfield 	 */
7536ed7257bSPatrick Caulfield 	if (!test_and_set_bit(CF_READ_PENDING, &addcon->flags))
7546ed7257bSPatrick Caulfield 		queue_work(recv_workqueue, &addcon->rwork);
7556ed7257bSPatrick Caulfield 	mutex_unlock(&con->sock_mutex);
7566ed7257bSPatrick Caulfield 
7576ed7257bSPatrick Caulfield 	return 0;
7586ed7257bSPatrick Caulfield 
7596ed7257bSPatrick Caulfield accept_err:
7606ed7257bSPatrick Caulfield 	mutex_unlock(&con->sock_mutex);
7616ed7257bSPatrick Caulfield 	sock_release(newsock);
7626ed7257bSPatrick Caulfield 
7636ed7257bSPatrick Caulfield 	if (result != -EAGAIN)
764617e82e1SDavid Teigland 		log_print("error accepting connection from node: %d", result);
7656ed7257bSPatrick Caulfield 	return result;
7666ed7257bSPatrick Caulfield }
7676ed7257bSPatrick Caulfield 
7686ed7257bSPatrick Caulfield static void free_entry(struct writequeue_entry *e)
7696ed7257bSPatrick Caulfield {
7706ed7257bSPatrick Caulfield 	__free_page(e->page);
7716ed7257bSPatrick Caulfield 	kfree(e);
7726ed7257bSPatrick Caulfield }
7736ed7257bSPatrick Caulfield 
7746ed7257bSPatrick Caulfield /* Initiate an SCTP association.
7756ed7257bSPatrick Caulfield    This is a special case of send_to_sock() in that we don't yet have a
7766ed7257bSPatrick Caulfield    peeled-off socket for this association, so we use the listening socket
7776ed7257bSPatrick Caulfield    and add the primary IP address of the remote node.
7786ed7257bSPatrick Caulfield  */
7796ed7257bSPatrick Caulfield static void sctp_init_assoc(struct connection *con)
7806ed7257bSPatrick Caulfield {
7816ed7257bSPatrick Caulfield 	struct sockaddr_storage rem_addr;
7826ed7257bSPatrick Caulfield 	char outcmsg[CMSG_SPACE(sizeof(struct sctp_sndrcvinfo))];
7836ed7257bSPatrick Caulfield 	struct msghdr outmessage;
7846ed7257bSPatrick Caulfield 	struct cmsghdr *cmsg;
7856ed7257bSPatrick Caulfield 	struct sctp_sndrcvinfo *sinfo;
7866ed7257bSPatrick Caulfield 	struct connection *base_con;
7876ed7257bSPatrick Caulfield 	struct writequeue_entry *e;
7886ed7257bSPatrick Caulfield 	int len, offset;
7896ed7257bSPatrick Caulfield 	int ret;
7906ed7257bSPatrick Caulfield 	int addrlen;
7916ed7257bSPatrick Caulfield 	struct kvec iov[1];
7926ed7257bSPatrick Caulfield 
7936ed7257bSPatrick Caulfield 	if (test_and_set_bit(CF_INIT_PENDING, &con->flags))
7946ed7257bSPatrick Caulfield 		return;
7956ed7257bSPatrick Caulfield 
7966ed7257bSPatrick Caulfield 	if (con->retries++ > MAX_CONNECT_RETRIES)
7976ed7257bSPatrick Caulfield 		return;
7986ed7257bSPatrick Caulfield 
7996ed7257bSPatrick Caulfield 	log_print("Initiating association with node %d", con->nodeid);
8006ed7257bSPatrick Caulfield 
8016ed7257bSPatrick Caulfield 	if (nodeid_to_addr(con->nodeid, (struct sockaddr *)&rem_addr)) {
8026ed7257bSPatrick Caulfield 		log_print("no address for nodeid %d", con->nodeid);
8036ed7257bSPatrick Caulfield 		return;
8046ed7257bSPatrick Caulfield 	}
8056ed7257bSPatrick Caulfield 	base_con = nodeid2con(0, 0);
8066ed7257bSPatrick Caulfield 	BUG_ON(base_con == NULL);
8076ed7257bSPatrick Caulfield 
8086ed7257bSPatrick Caulfield 	make_sockaddr(&rem_addr, dlm_config.ci_tcp_port, &addrlen);
8096ed7257bSPatrick Caulfield 
8106ed7257bSPatrick Caulfield 	outmessage.msg_name = &rem_addr;
8116ed7257bSPatrick Caulfield 	outmessage.msg_namelen = addrlen;
8126ed7257bSPatrick Caulfield 	outmessage.msg_control = outcmsg;
8136ed7257bSPatrick Caulfield 	outmessage.msg_controllen = sizeof(outcmsg);
8146ed7257bSPatrick Caulfield 	outmessage.msg_flags = MSG_EOR;
8156ed7257bSPatrick Caulfield 
8166ed7257bSPatrick Caulfield 	spin_lock(&con->writequeue_lock);
8176ed7257bSPatrick Caulfield 	e = list_entry(con->writequeue.next, struct writequeue_entry,
8186ed7257bSPatrick Caulfield 		       list);
8196ed7257bSPatrick Caulfield 
8206ed7257bSPatrick Caulfield 	BUG_ON((struct list_head *) e == &con->writequeue);
8216ed7257bSPatrick Caulfield 
8226ed7257bSPatrick Caulfield 	len = e->len;
8236ed7257bSPatrick Caulfield 	offset = e->offset;
8246ed7257bSPatrick Caulfield 	spin_unlock(&con->writequeue_lock);
8256ed7257bSPatrick Caulfield 	kmap(e->page);
8266ed7257bSPatrick Caulfield 
8276ed7257bSPatrick Caulfield 	/* Send the first block off the write queue */
8286ed7257bSPatrick Caulfield 	iov[0].iov_base = page_address(e->page)+offset;
8296ed7257bSPatrick Caulfield 	iov[0].iov_len = len;
8306ed7257bSPatrick Caulfield 
8316ed7257bSPatrick Caulfield 	cmsg = CMSG_FIRSTHDR(&outmessage);
8326ed7257bSPatrick Caulfield 	cmsg->cmsg_level = IPPROTO_SCTP;
8336ed7257bSPatrick Caulfield 	cmsg->cmsg_type = SCTP_SNDRCV;
8346ed7257bSPatrick Caulfield 	cmsg->cmsg_len = CMSG_LEN(sizeof(struct sctp_sndrcvinfo));
8356ed7257bSPatrick Caulfield 	sinfo = CMSG_DATA(cmsg);
8366ed7257bSPatrick Caulfield 	memset(sinfo, 0x00, sizeof(struct sctp_sndrcvinfo));
8376ed7257bSPatrick Caulfield 	sinfo->sinfo_ppid = cpu_to_le32(dlm_our_nodeid());
8386ed7257bSPatrick Caulfield 	outmessage.msg_controllen = cmsg->cmsg_len;
8396ed7257bSPatrick Caulfield 
8406ed7257bSPatrick Caulfield 	ret = kernel_sendmsg(base_con->sock, &outmessage, iov, 1, len);
8416ed7257bSPatrick Caulfield 	if (ret < 0) {
842617e82e1SDavid Teigland 		log_print("Send first packet to node %d failed: %d",
843617e82e1SDavid Teigland 			  con->nodeid, ret);
8446ed7257bSPatrick Caulfield 
8456ed7257bSPatrick Caulfield 		/* Try again later */
8466ed7257bSPatrick Caulfield 		clear_bit(CF_CONNECT_PENDING, &con->flags);
8476ed7257bSPatrick Caulfield 		clear_bit(CF_INIT_PENDING, &con->flags);
8486ed7257bSPatrick Caulfield 	}
8496ed7257bSPatrick Caulfield 	else {
8506ed7257bSPatrick Caulfield 		spin_lock(&con->writequeue_lock);
8516ed7257bSPatrick Caulfield 		e->offset += ret;
8526ed7257bSPatrick Caulfield 		e->len -= ret;
8536ed7257bSPatrick Caulfield 
8546ed7257bSPatrick Caulfield 		if (e->len == 0 && e->users == 0) {
8556ed7257bSPatrick Caulfield 			list_del(&e->list);
8566ed7257bSPatrick Caulfield 			kunmap(e->page);
8576ed7257bSPatrick Caulfield 			free_entry(e);
8586ed7257bSPatrick Caulfield 		}
8596ed7257bSPatrick Caulfield 		spin_unlock(&con->writequeue_lock);
8606ed7257bSPatrick Caulfield 	}
8616ed7257bSPatrick Caulfield }
8626ed7257bSPatrick Caulfield 
8636ed7257bSPatrick Caulfield /* Connect a new socket to its peer */
8646ed7257bSPatrick Caulfield static void tcp_connect_to_sock(struct connection *con)
8656ed7257bSPatrick Caulfield {
8666ed7257bSPatrick Caulfield 	int result = -EHOSTUNREACH;
8676bd8fedaSLon Hohberger 	struct sockaddr_storage saddr, src_addr;
8686ed7257bSPatrick Caulfield 	int addr_len;
8696ed7257bSPatrick Caulfield 	struct socket *sock;
8706ed7257bSPatrick Caulfield 
8716ed7257bSPatrick Caulfield 	if (con->nodeid == 0) {
8726ed7257bSPatrick Caulfield 		log_print("attempt to connect sock 0 foiled");
8736ed7257bSPatrick Caulfield 		return;
8746ed7257bSPatrick Caulfield 	}
8756ed7257bSPatrick Caulfield 
8766ed7257bSPatrick Caulfield 	mutex_lock(&con->sock_mutex);
8776ed7257bSPatrick Caulfield 	if (con->retries++ > MAX_CONNECT_RETRIES)
8786ed7257bSPatrick Caulfield 		goto out;
8796ed7257bSPatrick Caulfield 
8806ed7257bSPatrick Caulfield 	/* Some odd races can cause double-connects, ignore them */
8816ed7257bSPatrick Caulfield 	if (con->sock) {
8826ed7257bSPatrick Caulfield 		result = 0;
8836ed7257bSPatrick Caulfield 		goto out;
8846ed7257bSPatrick Caulfield 	}
8856ed7257bSPatrick Caulfield 
8866ed7257bSPatrick Caulfield 	/* Create a socket to communicate with */
8876ed7257bSPatrick Caulfield 	result = sock_create_kern(dlm_local_addr[0]->ss_family, SOCK_STREAM,
8886ed7257bSPatrick Caulfield 				  IPPROTO_TCP, &sock);
8896ed7257bSPatrick Caulfield 	if (result < 0)
8906ed7257bSPatrick Caulfield 		goto out_err;
8916ed7257bSPatrick Caulfield 
8926ed7257bSPatrick Caulfield 	memset(&saddr, 0, sizeof(saddr));
8936ed7257bSPatrick Caulfield 	if (dlm_nodeid_to_addr(con->nodeid, &saddr))
8946ed7257bSPatrick Caulfield 		goto out_err;
8956ed7257bSPatrick Caulfield 
8966ed7257bSPatrick Caulfield 	sock->sk->sk_user_data = con;
8976ed7257bSPatrick Caulfield 	con->rx_action = receive_from_sock;
8986ed7257bSPatrick Caulfield 	con->connect_action = tcp_connect_to_sock;
8996ed7257bSPatrick Caulfield 	add_sock(sock, con);
9006ed7257bSPatrick Caulfield 
9016bd8fedaSLon Hohberger 	/* Bind to our cluster-known address connecting to avoid
9026bd8fedaSLon Hohberger 	   routing problems */
9036bd8fedaSLon Hohberger 	memcpy(&src_addr, dlm_local_addr[0], sizeof(src_addr));
9046bd8fedaSLon Hohberger 	make_sockaddr(&src_addr, 0, &addr_len);
9056bd8fedaSLon Hohberger 	result = sock->ops->bind(sock, (struct sockaddr *) &src_addr,
9066bd8fedaSLon Hohberger 				 addr_len);
9076bd8fedaSLon Hohberger 	if (result < 0) {
9086bd8fedaSLon Hohberger 		log_print("could not bind for connect: %d", result);
9096bd8fedaSLon Hohberger 		/* This *may* not indicate a critical error */
9106bd8fedaSLon Hohberger 	}
9116bd8fedaSLon Hohberger 
9126ed7257bSPatrick Caulfield 	make_sockaddr(&saddr, dlm_config.ci_tcp_port, &addr_len);
9136ed7257bSPatrick Caulfield 
9146ed7257bSPatrick Caulfield 	log_print("connecting to %d", con->nodeid);
9156ed7257bSPatrick Caulfield 	result =
9166ed7257bSPatrick Caulfield 		sock->ops->connect(sock, (struct sockaddr *)&saddr, addr_len,
9176ed7257bSPatrick Caulfield 				   O_NONBLOCK);
9186ed7257bSPatrick Caulfield 	if (result == -EINPROGRESS)
9196ed7257bSPatrick Caulfield 		result = 0;
9206ed7257bSPatrick Caulfield 	if (result == 0)
9216ed7257bSPatrick Caulfield 		goto out;
9226ed7257bSPatrick Caulfield 
9236ed7257bSPatrick Caulfield out_err:
9246ed7257bSPatrick Caulfield 	if (con->sock) {
9256ed7257bSPatrick Caulfield 		sock_release(con->sock);
9266ed7257bSPatrick Caulfield 		con->sock = NULL;
9276ed7257bSPatrick Caulfield 	}
9286ed7257bSPatrick Caulfield 	/*
9296ed7257bSPatrick Caulfield 	 * Some errors are fatal and this list might need adjusting. For other
9306ed7257bSPatrick Caulfield 	 * errors we try again until the max number of retries is reached.
9316ed7257bSPatrick Caulfield 	 */
9326ed7257bSPatrick Caulfield 	if (result != -EHOSTUNREACH && result != -ENETUNREACH &&
9336ed7257bSPatrick Caulfield 	    result != -ENETDOWN && result != EINVAL
9346ed7257bSPatrick Caulfield 	    && result != -EPROTONOSUPPORT) {
9356ed7257bSPatrick Caulfield 		lowcomms_connect_sock(con);
9366ed7257bSPatrick Caulfield 		result = 0;
9376ed7257bSPatrick Caulfield 	}
9386ed7257bSPatrick Caulfield out:
9396ed7257bSPatrick Caulfield 	mutex_unlock(&con->sock_mutex);
9406ed7257bSPatrick Caulfield 	return;
9416ed7257bSPatrick Caulfield }
9426ed7257bSPatrick Caulfield 
9436ed7257bSPatrick Caulfield static struct socket *tcp_create_listen_sock(struct connection *con,
9446ed7257bSPatrick Caulfield 					     struct sockaddr_storage *saddr)
9456ed7257bSPatrick Caulfield {
9466ed7257bSPatrick Caulfield 	struct socket *sock = NULL;
9476ed7257bSPatrick Caulfield 	int result = 0;
9486ed7257bSPatrick Caulfield 	int one = 1;
9496ed7257bSPatrick Caulfield 	int addr_len;
9506ed7257bSPatrick Caulfield 
9516ed7257bSPatrick Caulfield 	if (dlm_local_addr[0]->ss_family == AF_INET)
9526ed7257bSPatrick Caulfield 		addr_len = sizeof(struct sockaddr_in);
9536ed7257bSPatrick Caulfield 	else
9546ed7257bSPatrick Caulfield 		addr_len = sizeof(struct sockaddr_in6);
9556ed7257bSPatrick Caulfield 
9566ed7257bSPatrick Caulfield 	/* Create a socket to communicate with */
957617e82e1SDavid Teigland 	result = sock_create_kern(dlm_local_addr[0]->ss_family, SOCK_STREAM,
958617e82e1SDavid Teigland 				  IPPROTO_TCP, &sock);
9596ed7257bSPatrick Caulfield 	if (result < 0) {
960617e82e1SDavid Teigland 		log_print("Can't create listening comms socket");
9616ed7257bSPatrick Caulfield 		goto create_out;
9626ed7257bSPatrick Caulfield 	}
9636ed7257bSPatrick Caulfield 
9646ed7257bSPatrick Caulfield 	result = kernel_setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
9656ed7257bSPatrick Caulfield 				   (char *)&one, sizeof(one));
9666ed7257bSPatrick Caulfield 
9676ed7257bSPatrick Caulfield 	if (result < 0) {
968617e82e1SDavid Teigland 		log_print("Failed to set SO_REUSEADDR on socket: %d", result);
9696ed7257bSPatrick Caulfield 	}
9706ed7257bSPatrick Caulfield 	sock->sk->sk_user_data = con;
9716ed7257bSPatrick Caulfield 	con->rx_action = tcp_accept_from_sock;
9726ed7257bSPatrick Caulfield 	con->connect_action = tcp_connect_to_sock;
9736ed7257bSPatrick Caulfield 	con->sock = sock;
9746ed7257bSPatrick Caulfield 
9756ed7257bSPatrick Caulfield 	/* Bind to our port */
9766ed7257bSPatrick Caulfield 	make_sockaddr(saddr, dlm_config.ci_tcp_port, &addr_len);
9776ed7257bSPatrick Caulfield 	result = sock->ops->bind(sock, (struct sockaddr *) saddr, addr_len);
9786ed7257bSPatrick Caulfield 	if (result < 0) {
979617e82e1SDavid Teigland 		log_print("Can't bind to port %d", dlm_config.ci_tcp_port);
9806ed7257bSPatrick Caulfield 		sock_release(sock);
9816ed7257bSPatrick Caulfield 		sock = NULL;
9826ed7257bSPatrick Caulfield 		con->sock = NULL;
9836ed7257bSPatrick Caulfield 		goto create_out;
9846ed7257bSPatrick Caulfield 	}
9856ed7257bSPatrick Caulfield 	result = kernel_setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE,
9866ed7257bSPatrick Caulfield 				 (char *)&one, sizeof(one));
9876ed7257bSPatrick Caulfield 	if (result < 0) {
988617e82e1SDavid Teigland 		log_print("Set keepalive failed: %d", result);
9896ed7257bSPatrick Caulfield 	}
9906ed7257bSPatrick Caulfield 
9916ed7257bSPatrick Caulfield 	result = sock->ops->listen(sock, 5);
9926ed7257bSPatrick Caulfield 	if (result < 0) {
993617e82e1SDavid Teigland 		log_print("Can't listen on port %d", dlm_config.ci_tcp_port);
9946ed7257bSPatrick Caulfield 		sock_release(sock);
9956ed7257bSPatrick Caulfield 		sock = NULL;
9966ed7257bSPatrick Caulfield 		goto create_out;
9976ed7257bSPatrick Caulfield 	}
9986ed7257bSPatrick Caulfield 
9996ed7257bSPatrick Caulfield create_out:
10006ed7257bSPatrick Caulfield 	return sock;
10016ed7257bSPatrick Caulfield }
10026ed7257bSPatrick Caulfield 
10036ed7257bSPatrick Caulfield /* Get local addresses */
10046ed7257bSPatrick Caulfield static void init_local(void)
10056ed7257bSPatrick Caulfield {
10066ed7257bSPatrick Caulfield 	struct sockaddr_storage sas, *addr;
10076ed7257bSPatrick Caulfield 	int i;
10086ed7257bSPatrick Caulfield 
100930d3a237SPatrick Caulfield 	dlm_local_count = 0;
10106ed7257bSPatrick Caulfield 	for (i = 0; i < DLM_MAX_ADDR_COUNT - 1; i++) {
10116ed7257bSPatrick Caulfield 		if (dlm_our_addr(&sas, i))
10126ed7257bSPatrick Caulfield 			break;
10136ed7257bSPatrick Caulfield 
10146ed7257bSPatrick Caulfield 		addr = kmalloc(sizeof(*addr), GFP_KERNEL);
10156ed7257bSPatrick Caulfield 		if (!addr)
10166ed7257bSPatrick Caulfield 			break;
10176ed7257bSPatrick Caulfield 		memcpy(addr, &sas, sizeof(*addr));
10186ed7257bSPatrick Caulfield 		dlm_local_addr[dlm_local_count++] = addr;
10196ed7257bSPatrick Caulfield 	}
10206ed7257bSPatrick Caulfield }
10216ed7257bSPatrick Caulfield 
1022617e82e1SDavid Teigland /* Bind to an IP address. SCTP allows multiple address so it can do
1023617e82e1SDavid Teigland    multi-homing */
1024617e82e1SDavid Teigland static int add_sctp_bind_addr(struct connection *sctp_con,
1025617e82e1SDavid Teigland 			      struct sockaddr_storage *addr,
1026617e82e1SDavid Teigland 			      int addr_len, int num)
10276ed7257bSPatrick Caulfield {
10286ed7257bSPatrick Caulfield 	int result = 0;
10296ed7257bSPatrick Caulfield 
10306ed7257bSPatrick Caulfield 	if (num == 1)
10316ed7257bSPatrick Caulfield 		result = kernel_bind(sctp_con->sock,
10326ed7257bSPatrick Caulfield 				     (struct sockaddr *) addr,
10336ed7257bSPatrick Caulfield 				     addr_len);
10346ed7257bSPatrick Caulfield 	else
10356ed7257bSPatrick Caulfield 		result = kernel_setsockopt(sctp_con->sock, SOL_SCTP,
10366ed7257bSPatrick Caulfield 					   SCTP_SOCKOPT_BINDX_ADD,
10376ed7257bSPatrick Caulfield 					   (char *)addr, addr_len);
10386ed7257bSPatrick Caulfield 
10396ed7257bSPatrick Caulfield 	if (result < 0)
10406ed7257bSPatrick Caulfield 		log_print("Can't bind to port %d addr number %d",
10416ed7257bSPatrick Caulfield 			  dlm_config.ci_tcp_port, num);
10426ed7257bSPatrick Caulfield 
10436ed7257bSPatrick Caulfield 	return result;
10446ed7257bSPatrick Caulfield }
10456ed7257bSPatrick Caulfield 
10466ed7257bSPatrick Caulfield /* Initialise SCTP socket and bind to all interfaces */
10476ed7257bSPatrick Caulfield static int sctp_listen_for_all(void)
10486ed7257bSPatrick Caulfield {
10496ed7257bSPatrick Caulfield 	struct socket *sock = NULL;
10506ed7257bSPatrick Caulfield 	struct sockaddr_storage localaddr;
10516ed7257bSPatrick Caulfield 	struct sctp_event_subscribe subscribe;
10526ed7257bSPatrick Caulfield 	int result = -EINVAL, num = 1, i, addr_len;
10536ed7257bSPatrick Caulfield 	struct connection *con = nodeid2con(0, GFP_KERNEL);
10546ed7257bSPatrick Caulfield 	int bufsize = NEEDED_RMEM;
10556ed7257bSPatrick Caulfield 
10566ed7257bSPatrick Caulfield 	if (!con)
10576ed7257bSPatrick Caulfield 		return -ENOMEM;
10586ed7257bSPatrick Caulfield 
10596ed7257bSPatrick Caulfield 	log_print("Using SCTP for communications");
10606ed7257bSPatrick Caulfield 
10616ed7257bSPatrick Caulfield 	result = sock_create_kern(dlm_local_addr[0]->ss_family, SOCK_SEQPACKET,
10626ed7257bSPatrick Caulfield 				  IPPROTO_SCTP, &sock);
10636ed7257bSPatrick Caulfield 	if (result < 0) {
10646ed7257bSPatrick Caulfield 		log_print("Can't create comms socket, check SCTP is loaded");
10656ed7257bSPatrick Caulfield 		goto out;
10666ed7257bSPatrick Caulfield 	}
10676ed7257bSPatrick Caulfield 
10686ed7257bSPatrick Caulfield 	/* Listen for events */
10696ed7257bSPatrick Caulfield 	memset(&subscribe, 0, sizeof(subscribe));
10706ed7257bSPatrick Caulfield 	subscribe.sctp_data_io_event = 1;
10716ed7257bSPatrick Caulfield 	subscribe.sctp_association_event = 1;
10726ed7257bSPatrick Caulfield 	subscribe.sctp_send_failure_event = 1;
10736ed7257bSPatrick Caulfield 	subscribe.sctp_shutdown_event = 1;
10746ed7257bSPatrick Caulfield 	subscribe.sctp_partial_delivery_event = 1;
10756ed7257bSPatrick Caulfield 
1076df61c952SDavid S. Miller 	result = kernel_setsockopt(sock, SOL_SOCKET, SO_RCVBUFFORCE,
10776ed7257bSPatrick Caulfield 				 (char *)&bufsize, sizeof(bufsize));
10786ed7257bSPatrick Caulfield 	if (result)
1079617e82e1SDavid Teigland 		log_print("Error increasing buffer space on socket %d", result);
10806ed7257bSPatrick Caulfield 
10816ed7257bSPatrick Caulfield 	result = kernel_setsockopt(sock, SOL_SCTP, SCTP_EVENTS,
10826ed7257bSPatrick Caulfield 				   (char *)&subscribe, sizeof(subscribe));
10836ed7257bSPatrick Caulfield 	if (result < 0) {
10846ed7257bSPatrick Caulfield 		log_print("Failed to set SCTP_EVENTS on socket: result=%d",
10856ed7257bSPatrick Caulfield 			  result);
10866ed7257bSPatrick Caulfield 		goto create_delsock;
10876ed7257bSPatrick Caulfield 	}
10886ed7257bSPatrick Caulfield 
10896ed7257bSPatrick Caulfield 	/* Init con struct */
10906ed7257bSPatrick Caulfield 	sock->sk->sk_user_data = con;
10916ed7257bSPatrick Caulfield 	con->sock = sock;
10926ed7257bSPatrick Caulfield 	con->sock->sk->sk_data_ready = lowcomms_data_ready;
10936ed7257bSPatrick Caulfield 	con->rx_action = receive_from_sock;
10946ed7257bSPatrick Caulfield 	con->connect_action = sctp_init_assoc;
10956ed7257bSPatrick Caulfield 
10966ed7257bSPatrick Caulfield 	/* Bind to all interfaces. */
10976ed7257bSPatrick Caulfield 	for (i = 0; i < dlm_local_count; i++) {
10986ed7257bSPatrick Caulfield 		memcpy(&localaddr, dlm_local_addr[i], sizeof(localaddr));
10996ed7257bSPatrick Caulfield 		make_sockaddr(&localaddr, dlm_config.ci_tcp_port, &addr_len);
11006ed7257bSPatrick Caulfield 
11016ed7257bSPatrick Caulfield 		result = add_sctp_bind_addr(con, &localaddr, addr_len, num);
11026ed7257bSPatrick Caulfield 		if (result)
11036ed7257bSPatrick Caulfield 			goto create_delsock;
11046ed7257bSPatrick Caulfield 		++num;
11056ed7257bSPatrick Caulfield 	}
11066ed7257bSPatrick Caulfield 
11076ed7257bSPatrick Caulfield 	result = sock->ops->listen(sock, 5);
11086ed7257bSPatrick Caulfield 	if (result < 0) {
11096ed7257bSPatrick Caulfield 		log_print("Can't set socket listening");
11106ed7257bSPatrick Caulfield 		goto create_delsock;
11116ed7257bSPatrick Caulfield 	}
11126ed7257bSPatrick Caulfield 
11136ed7257bSPatrick Caulfield 	return 0;
11146ed7257bSPatrick Caulfield 
11156ed7257bSPatrick Caulfield create_delsock:
11166ed7257bSPatrick Caulfield 	sock_release(sock);
11176ed7257bSPatrick Caulfield 	con->sock = NULL;
11186ed7257bSPatrick Caulfield out:
11196ed7257bSPatrick Caulfield 	return result;
11206ed7257bSPatrick Caulfield }
11216ed7257bSPatrick Caulfield 
11226ed7257bSPatrick Caulfield static int tcp_listen_for_all(void)
11236ed7257bSPatrick Caulfield {
11246ed7257bSPatrick Caulfield 	struct socket *sock = NULL;
11256ed7257bSPatrick Caulfield 	struct connection *con = nodeid2con(0, GFP_KERNEL);
11266ed7257bSPatrick Caulfield 	int result = -EINVAL;
11276ed7257bSPatrick Caulfield 
11286ed7257bSPatrick Caulfield 	if (!con)
11296ed7257bSPatrick Caulfield 		return -ENOMEM;
11306ed7257bSPatrick Caulfield 
11316ed7257bSPatrick Caulfield 	/* We don't support multi-homed hosts */
11326ed7257bSPatrick Caulfield 	if (dlm_local_addr[1] != NULL) {
1133617e82e1SDavid Teigland 		log_print("TCP protocol can't handle multi-homed hosts, "
1134617e82e1SDavid Teigland 			  "try SCTP");
11356ed7257bSPatrick Caulfield 		return -EINVAL;
11366ed7257bSPatrick Caulfield 	}
11376ed7257bSPatrick Caulfield 
11386ed7257bSPatrick Caulfield 	log_print("Using TCP for communications");
11396ed7257bSPatrick Caulfield 
11406ed7257bSPatrick Caulfield 	sock = tcp_create_listen_sock(con, dlm_local_addr[0]);
11416ed7257bSPatrick Caulfield 	if (sock) {
11426ed7257bSPatrick Caulfield 		add_sock(sock, con);
11436ed7257bSPatrick Caulfield 		result = 0;
11446ed7257bSPatrick Caulfield 	}
11456ed7257bSPatrick Caulfield 	else {
11466ed7257bSPatrick Caulfield 		result = -EADDRINUSE;
11476ed7257bSPatrick Caulfield 	}
11486ed7257bSPatrick Caulfield 
11496ed7257bSPatrick Caulfield 	return result;
11506ed7257bSPatrick Caulfield }
11516ed7257bSPatrick Caulfield 
11526ed7257bSPatrick Caulfield 
11536ed7257bSPatrick Caulfield 
11546ed7257bSPatrick Caulfield static struct writequeue_entry *new_writequeue_entry(struct connection *con,
11556ed7257bSPatrick Caulfield 						     gfp_t allocation)
11566ed7257bSPatrick Caulfield {
11576ed7257bSPatrick Caulfield 	struct writequeue_entry *entry;
11586ed7257bSPatrick Caulfield 
11596ed7257bSPatrick Caulfield 	entry = kmalloc(sizeof(struct writequeue_entry), allocation);
11606ed7257bSPatrick Caulfield 	if (!entry)
11616ed7257bSPatrick Caulfield 		return NULL;
11626ed7257bSPatrick Caulfield 
11636ed7257bSPatrick Caulfield 	entry->page = alloc_page(allocation);
11646ed7257bSPatrick Caulfield 	if (!entry->page) {
11656ed7257bSPatrick Caulfield 		kfree(entry);
11666ed7257bSPatrick Caulfield 		return NULL;
11676ed7257bSPatrick Caulfield 	}
11686ed7257bSPatrick Caulfield 
11696ed7257bSPatrick Caulfield 	entry->offset = 0;
11706ed7257bSPatrick Caulfield 	entry->len = 0;
11716ed7257bSPatrick Caulfield 	entry->end = 0;
11726ed7257bSPatrick Caulfield 	entry->users = 0;
11736ed7257bSPatrick Caulfield 	entry->con = con;
11746ed7257bSPatrick Caulfield 
11756ed7257bSPatrick Caulfield 	return entry;
11766ed7257bSPatrick Caulfield }
11776ed7257bSPatrick Caulfield 
1178617e82e1SDavid Teigland void *dlm_lowcomms_get_buffer(int nodeid, int len, gfp_t allocation, char **ppc)
11796ed7257bSPatrick Caulfield {
11806ed7257bSPatrick Caulfield 	struct connection *con;
11816ed7257bSPatrick Caulfield 	struct writequeue_entry *e;
11826ed7257bSPatrick Caulfield 	int offset = 0;
11836ed7257bSPatrick Caulfield 	int users = 0;
11846ed7257bSPatrick Caulfield 
11856ed7257bSPatrick Caulfield 	con = nodeid2con(nodeid, allocation);
11866ed7257bSPatrick Caulfield 	if (!con)
11876ed7257bSPatrick Caulfield 		return NULL;
11886ed7257bSPatrick Caulfield 
11896ed7257bSPatrick Caulfield 	spin_lock(&con->writequeue_lock);
11906ed7257bSPatrick Caulfield 	e = list_entry(con->writequeue.prev, struct writequeue_entry, list);
11916ed7257bSPatrick Caulfield 	if ((&e->list == &con->writequeue) ||
11926ed7257bSPatrick Caulfield 	    (PAGE_CACHE_SIZE - e->end < len)) {
11936ed7257bSPatrick Caulfield 		e = NULL;
11946ed7257bSPatrick Caulfield 	} else {
11956ed7257bSPatrick Caulfield 		offset = e->end;
11966ed7257bSPatrick Caulfield 		e->end += len;
11976ed7257bSPatrick Caulfield 		users = e->users++;
11986ed7257bSPatrick Caulfield 	}
11996ed7257bSPatrick Caulfield 	spin_unlock(&con->writequeue_lock);
12006ed7257bSPatrick Caulfield 
12016ed7257bSPatrick Caulfield 	if (e) {
12026ed7257bSPatrick Caulfield 	got_one:
12036ed7257bSPatrick Caulfield 		if (users == 0)
12046ed7257bSPatrick Caulfield 			kmap(e->page);
12056ed7257bSPatrick Caulfield 		*ppc = page_address(e->page) + offset;
12066ed7257bSPatrick Caulfield 		return e;
12076ed7257bSPatrick Caulfield 	}
12086ed7257bSPatrick Caulfield 
12096ed7257bSPatrick Caulfield 	e = new_writequeue_entry(con, allocation);
12106ed7257bSPatrick Caulfield 	if (e) {
12116ed7257bSPatrick Caulfield 		spin_lock(&con->writequeue_lock);
12126ed7257bSPatrick Caulfield 		offset = e->end;
12136ed7257bSPatrick Caulfield 		e->end += len;
12146ed7257bSPatrick Caulfield 		users = e->users++;
12156ed7257bSPatrick Caulfield 		list_add_tail(&e->list, &con->writequeue);
12166ed7257bSPatrick Caulfield 		spin_unlock(&con->writequeue_lock);
12176ed7257bSPatrick Caulfield 		goto got_one;
12186ed7257bSPatrick Caulfield 	}
12196ed7257bSPatrick Caulfield 	return NULL;
12206ed7257bSPatrick Caulfield }
12216ed7257bSPatrick Caulfield 
12226ed7257bSPatrick Caulfield void dlm_lowcomms_commit_buffer(void *mh)
12236ed7257bSPatrick Caulfield {
12246ed7257bSPatrick Caulfield 	struct writequeue_entry *e = (struct writequeue_entry *)mh;
12256ed7257bSPatrick Caulfield 	struct connection *con = e->con;
12266ed7257bSPatrick Caulfield 	int users;
12276ed7257bSPatrick Caulfield 
12286ed7257bSPatrick Caulfield 	spin_lock(&con->writequeue_lock);
12296ed7257bSPatrick Caulfield 	users = --e->users;
12306ed7257bSPatrick Caulfield 	if (users)
12316ed7257bSPatrick Caulfield 		goto out;
12326ed7257bSPatrick Caulfield 	e->len = e->end - e->offset;
12336ed7257bSPatrick Caulfield 	kunmap(e->page);
12346ed7257bSPatrick Caulfield 	spin_unlock(&con->writequeue_lock);
12356ed7257bSPatrick Caulfield 
12366ed7257bSPatrick Caulfield 	if (!test_and_set_bit(CF_WRITE_PENDING, &con->flags)) {
12376ed7257bSPatrick Caulfield 		queue_work(send_workqueue, &con->swork);
12386ed7257bSPatrick Caulfield 	}
12396ed7257bSPatrick Caulfield 	return;
12406ed7257bSPatrick Caulfield 
12416ed7257bSPatrick Caulfield out:
12426ed7257bSPatrick Caulfield 	spin_unlock(&con->writequeue_lock);
12436ed7257bSPatrick Caulfield 	return;
12446ed7257bSPatrick Caulfield }
12456ed7257bSPatrick Caulfield 
12466ed7257bSPatrick Caulfield /* Send a message */
12476ed7257bSPatrick Caulfield static void send_to_sock(struct connection *con)
12486ed7257bSPatrick Caulfield {
12496ed7257bSPatrick Caulfield 	int ret = 0;
12506ed7257bSPatrick Caulfield 	ssize_t(*sendpage) (struct socket *, struct page *, int, size_t, int);
12516ed7257bSPatrick Caulfield 	const int msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL;
12526ed7257bSPatrick Caulfield 	struct writequeue_entry *e;
12536ed7257bSPatrick Caulfield 	int len, offset;
12546ed7257bSPatrick Caulfield 
12556ed7257bSPatrick Caulfield 	mutex_lock(&con->sock_mutex);
12566ed7257bSPatrick Caulfield 	if (con->sock == NULL)
12576ed7257bSPatrick Caulfield 		goto out_connect;
12586ed7257bSPatrick Caulfield 
12596ed7257bSPatrick Caulfield 	sendpage = con->sock->ops->sendpage;
12606ed7257bSPatrick Caulfield 
12616ed7257bSPatrick Caulfield 	spin_lock(&con->writequeue_lock);
12626ed7257bSPatrick Caulfield 	for (;;) {
12636ed7257bSPatrick Caulfield 		e = list_entry(con->writequeue.next, struct writequeue_entry,
12646ed7257bSPatrick Caulfield 			       list);
12656ed7257bSPatrick Caulfield 		if ((struct list_head *) e == &con->writequeue)
12666ed7257bSPatrick Caulfield 			break;
12676ed7257bSPatrick Caulfield 
12686ed7257bSPatrick Caulfield 		len = e->len;
12696ed7257bSPatrick Caulfield 		offset = e->offset;
12706ed7257bSPatrick Caulfield 		BUG_ON(len == 0 && e->users == 0);
12716ed7257bSPatrick Caulfield 		spin_unlock(&con->writequeue_lock);
12726ed7257bSPatrick Caulfield 		kmap(e->page);
12736ed7257bSPatrick Caulfield 
12746ed7257bSPatrick Caulfield 		ret = 0;
12756ed7257bSPatrick Caulfield 		if (len) {
12766ed7257bSPatrick Caulfield 			ret = sendpage(con->sock, e->page, offset, len,
12776ed7257bSPatrick Caulfield 				       msg_flags);
1278d66f8277SPatrick Caulfield 			if (ret == -EAGAIN || ret == 0) {
1279d66f8277SPatrick Caulfield 				cond_resched();
12806ed7257bSPatrick Caulfield 				goto out;
1281d66f8277SPatrick Caulfield 			}
12826ed7257bSPatrick Caulfield 			if (ret <= 0)
12836ed7257bSPatrick Caulfield 				goto send_error;
1284d66f8277SPatrick Caulfield 		}
12856ed7257bSPatrick Caulfield 			/* Don't starve people filling buffers */
12866ed7257bSPatrick Caulfield 			cond_resched();
12876ed7257bSPatrick Caulfield 
12886ed7257bSPatrick Caulfield 		spin_lock(&con->writequeue_lock);
12896ed7257bSPatrick Caulfield 		e->offset += ret;
12906ed7257bSPatrick Caulfield 		e->len -= ret;
12916ed7257bSPatrick Caulfield 
12926ed7257bSPatrick Caulfield 		if (e->len == 0 && e->users == 0) {
12936ed7257bSPatrick Caulfield 			list_del(&e->list);
12946ed7257bSPatrick Caulfield 			kunmap(e->page);
12956ed7257bSPatrick Caulfield 			free_entry(e);
12966ed7257bSPatrick Caulfield 			continue;
12976ed7257bSPatrick Caulfield 		}
12986ed7257bSPatrick Caulfield 	}
12996ed7257bSPatrick Caulfield 	spin_unlock(&con->writequeue_lock);
13006ed7257bSPatrick Caulfield out:
13016ed7257bSPatrick Caulfield 	mutex_unlock(&con->sock_mutex);
13026ed7257bSPatrick Caulfield 	return;
13036ed7257bSPatrick Caulfield 
13046ed7257bSPatrick Caulfield send_error:
13056ed7257bSPatrick Caulfield 	mutex_unlock(&con->sock_mutex);
13066ed7257bSPatrick Caulfield 	close_connection(con, false);
13076ed7257bSPatrick Caulfield 	lowcomms_connect_sock(con);
13086ed7257bSPatrick Caulfield 	return;
13096ed7257bSPatrick Caulfield 
13106ed7257bSPatrick Caulfield out_connect:
13116ed7257bSPatrick Caulfield 	mutex_unlock(&con->sock_mutex);
13126ed7257bSPatrick Caulfield 	if (!test_bit(CF_INIT_PENDING, &con->flags))
13136ed7257bSPatrick Caulfield 		lowcomms_connect_sock(con);
13146ed7257bSPatrick Caulfield 	return;
13156ed7257bSPatrick Caulfield }
13166ed7257bSPatrick Caulfield 
13176ed7257bSPatrick Caulfield static void clean_one_writequeue(struct connection *con)
13186ed7257bSPatrick Caulfield {
13196ed7257bSPatrick Caulfield 	struct list_head *list;
13206ed7257bSPatrick Caulfield 	struct list_head *temp;
13216ed7257bSPatrick Caulfield 
13226ed7257bSPatrick Caulfield 	spin_lock(&con->writequeue_lock);
13236ed7257bSPatrick Caulfield 	list_for_each_safe(list, temp, &con->writequeue) {
13246ed7257bSPatrick Caulfield 		struct writequeue_entry *e =
13256ed7257bSPatrick Caulfield 			list_entry(list, struct writequeue_entry, list);
13266ed7257bSPatrick Caulfield 		list_del(&e->list);
13276ed7257bSPatrick Caulfield 		free_entry(e);
13286ed7257bSPatrick Caulfield 	}
13296ed7257bSPatrick Caulfield 	spin_unlock(&con->writequeue_lock);
13306ed7257bSPatrick Caulfield }
13316ed7257bSPatrick Caulfield 
13326ed7257bSPatrick Caulfield /* Called from recovery when it knows that a node has
13336ed7257bSPatrick Caulfield    left the cluster */
13346ed7257bSPatrick Caulfield int dlm_lowcomms_close(int nodeid)
13356ed7257bSPatrick Caulfield {
13366ed7257bSPatrick Caulfield 	struct connection *con;
13376ed7257bSPatrick Caulfield 
13386ed7257bSPatrick Caulfield 	log_print("closing connection to node %d", nodeid);
13396ed7257bSPatrick Caulfield 	con = nodeid2con(nodeid, 0);
13406ed7257bSPatrick Caulfield 	if (con) {
13416ed7257bSPatrick Caulfield 		clean_one_writequeue(con);
13426ed7257bSPatrick Caulfield 		close_connection(con, true);
13436ed7257bSPatrick Caulfield 	}
13446ed7257bSPatrick Caulfield 	return 0;
13456ed7257bSPatrick Caulfield }
13466ed7257bSPatrick Caulfield 
13476ed7257bSPatrick Caulfield /* Receive workqueue function */
13486ed7257bSPatrick Caulfield static void process_recv_sockets(struct work_struct *work)
13496ed7257bSPatrick Caulfield {
13506ed7257bSPatrick Caulfield 	struct connection *con = container_of(work, struct connection, rwork);
13516ed7257bSPatrick Caulfield 	int err;
13526ed7257bSPatrick Caulfield 
13536ed7257bSPatrick Caulfield 	clear_bit(CF_READ_PENDING, &con->flags);
13546ed7257bSPatrick Caulfield 	do {
13556ed7257bSPatrick Caulfield 		err = con->rx_action(con);
13566ed7257bSPatrick Caulfield 	} while (!err);
13576ed7257bSPatrick Caulfield }
13586ed7257bSPatrick Caulfield 
13596ed7257bSPatrick Caulfield /* Send workqueue function */
13606ed7257bSPatrick Caulfield static void process_send_sockets(struct work_struct *work)
13616ed7257bSPatrick Caulfield {
13626ed7257bSPatrick Caulfield 	struct connection *con = container_of(work, struct connection, swork);
13636ed7257bSPatrick Caulfield 
13646ed7257bSPatrick Caulfield 	if (test_and_clear_bit(CF_CONNECT_PENDING, &con->flags)) {
13656ed7257bSPatrick Caulfield 		con->connect_action(con);
13666ed7257bSPatrick Caulfield 	}
13676ed7257bSPatrick Caulfield 	clear_bit(CF_WRITE_PENDING, &con->flags);
13686ed7257bSPatrick Caulfield 	send_to_sock(con);
13696ed7257bSPatrick Caulfield }
13706ed7257bSPatrick Caulfield 
13716ed7257bSPatrick Caulfield 
13726ed7257bSPatrick Caulfield /* Discard all entries on the write queues */
13736ed7257bSPatrick Caulfield static void clean_writequeues(void)
13746ed7257bSPatrick Caulfield {
13756ed7257bSPatrick Caulfield 	int nodeid;
13766ed7257bSPatrick Caulfield 
137730d3a237SPatrick Caulfield 	for (nodeid = 1; nodeid <= max_nodeid; nodeid++) {
137830d3a237SPatrick Caulfield 		struct connection *con = __nodeid2con(nodeid, 0);
13796ed7257bSPatrick Caulfield 
13806ed7257bSPatrick Caulfield 		if (con)
13816ed7257bSPatrick Caulfield 			clean_one_writequeue(con);
13826ed7257bSPatrick Caulfield 	}
13836ed7257bSPatrick Caulfield }
13846ed7257bSPatrick Caulfield 
13856ed7257bSPatrick Caulfield static void work_stop(void)
13866ed7257bSPatrick Caulfield {
13876ed7257bSPatrick Caulfield 	destroy_workqueue(recv_workqueue);
13886ed7257bSPatrick Caulfield 	destroy_workqueue(send_workqueue);
13896ed7257bSPatrick Caulfield }
13906ed7257bSPatrick Caulfield 
13916ed7257bSPatrick Caulfield static int work_start(void)
13926ed7257bSPatrick Caulfield {
13936ed7257bSPatrick Caulfield 	int error;
13946ed7257bSPatrick Caulfield 	recv_workqueue = create_workqueue("dlm_recv");
13956ed7257bSPatrick Caulfield 	error = IS_ERR(recv_workqueue);
13966ed7257bSPatrick Caulfield 	if (error) {
13976ed7257bSPatrick Caulfield 		log_print("can't start dlm_recv %d", error);
13986ed7257bSPatrick Caulfield 		return error;
13996ed7257bSPatrick Caulfield 	}
14006ed7257bSPatrick Caulfield 
14016ed7257bSPatrick Caulfield 	send_workqueue = create_singlethread_workqueue("dlm_send");
14026ed7257bSPatrick Caulfield 	error = IS_ERR(send_workqueue);
14036ed7257bSPatrick Caulfield 	if (error) {
14046ed7257bSPatrick Caulfield 		log_print("can't start dlm_send %d", error);
14056ed7257bSPatrick Caulfield 		destroy_workqueue(recv_workqueue);
14066ed7257bSPatrick Caulfield 		return error;
14076ed7257bSPatrick Caulfield 	}
14086ed7257bSPatrick Caulfield 
14096ed7257bSPatrick Caulfield 	return 0;
14106ed7257bSPatrick Caulfield }
14116ed7257bSPatrick Caulfield 
14126ed7257bSPatrick Caulfield void dlm_lowcomms_stop(void)
14136ed7257bSPatrick Caulfield {
14146ed7257bSPatrick Caulfield 	int i;
14156ed7257bSPatrick Caulfield 	struct connection *con;
14166ed7257bSPatrick Caulfield 
14176ed7257bSPatrick Caulfield 	/* Set all the flags to prevent any
14186ed7257bSPatrick Caulfield 	   socket activity.
14196ed7257bSPatrick Caulfield 	*/
14206ed7257bSPatrick Caulfield 	down(&connections_lock);
142130d3a237SPatrick Caulfield 	for (i = 0; i <= max_nodeid; i++) {
14226ed7257bSPatrick Caulfield 		con = __nodeid2con(i, 0);
1423afb853fbSPatrick Caulfield 		if (con) {
14249e5f2825SPatrick Caulfield 			con->flags |= 0x0F;
1425afb853fbSPatrick Caulfield 			if (con->sock)
1426afb853fbSPatrick Caulfield 				con->sock->sk->sk_user_data = NULL;
1427afb853fbSPatrick Caulfield 		}
14286ed7257bSPatrick Caulfield 	}
14296ed7257bSPatrick Caulfield 	up(&connections_lock);
14306ed7257bSPatrick Caulfield 
14316ed7257bSPatrick Caulfield 	work_stop();
14326ed7257bSPatrick Caulfield 
14336ed7257bSPatrick Caulfield 	down(&connections_lock);
14346ed7257bSPatrick Caulfield 	clean_writequeues();
14356ed7257bSPatrick Caulfield 
143630d3a237SPatrick Caulfield 	for (i = 0; i <= max_nodeid; i++) {
14372439fe50SJosef Bacik 		con = __nodeid2con(i, 0);
14386ed7257bSPatrick Caulfield 		if (con) {
14396ed7257bSPatrick Caulfield 			close_connection(con, true);
144039bd4177SPatrick Caulfeld 			if (con->othercon)
144139bd4177SPatrick Caulfeld 				kmem_cache_free(con_cache, con->othercon);
14426ed7257bSPatrick Caulfield 			kmem_cache_free(con_cache, con);
14436ed7257bSPatrick Caulfield 		}
14446ed7257bSPatrick Caulfield 	}
144530d3a237SPatrick Caulfield 	max_nodeid = 0;
14466ed7257bSPatrick Caulfield 	up(&connections_lock);
14476ed7257bSPatrick Caulfield 	kmem_cache_destroy(con_cache);
144830d3a237SPatrick Caulfield 	idr_init(&connections_idr);
14496ed7257bSPatrick Caulfield }
14506ed7257bSPatrick Caulfield 
14516ed7257bSPatrick Caulfield int dlm_lowcomms_start(void)
14526ed7257bSPatrick Caulfield {
14536ed7257bSPatrick Caulfield 	int error = -EINVAL;
14546ed7257bSPatrick Caulfield 	struct connection *con;
14556ed7257bSPatrick Caulfield 
14566ed7257bSPatrick Caulfield 	init_local();
14576ed7257bSPatrick Caulfield 	if (!dlm_local_count) {
1458617e82e1SDavid Teigland 		error = -ENOTCONN;
14596ed7257bSPatrick Caulfield 		log_print("no local IP address has been set");
14606ed7257bSPatrick Caulfield 		goto out;
14616ed7257bSPatrick Caulfield 	}
14626ed7257bSPatrick Caulfield 
14636ed7257bSPatrick Caulfield 	error = -ENOMEM;
14646ed7257bSPatrick Caulfield 	con_cache = kmem_cache_create("dlm_conn", sizeof(struct connection),
14656ed7257bSPatrick Caulfield 				      __alignof__(struct connection), 0,
146620c2df83SPaul Mundt 				      NULL);
14676ed7257bSPatrick Caulfield 	if (!con_cache)
14686ed7257bSPatrick Caulfield 		goto out;
14696ed7257bSPatrick Caulfield 
14706ed7257bSPatrick Caulfield 	/* Start listening */
14716ed7257bSPatrick Caulfield 	if (dlm_config.ci_protocol == 0)
14726ed7257bSPatrick Caulfield 		error = tcp_listen_for_all();
14736ed7257bSPatrick Caulfield 	else
14746ed7257bSPatrick Caulfield 		error = sctp_listen_for_all();
14756ed7257bSPatrick Caulfield 	if (error)
14766ed7257bSPatrick Caulfield 		goto fail_unlisten;
14776ed7257bSPatrick Caulfield 
14786ed7257bSPatrick Caulfield 	error = work_start();
14796ed7257bSPatrick Caulfield 	if (error)
14806ed7257bSPatrick Caulfield 		goto fail_unlisten;
14816ed7257bSPatrick Caulfield 
14826ed7257bSPatrick Caulfield 	return 0;
14836ed7257bSPatrick Caulfield 
14846ed7257bSPatrick Caulfield fail_unlisten:
14856ed7257bSPatrick Caulfield 	con = nodeid2con(0,0);
14866ed7257bSPatrick Caulfield 	if (con) {
14876ed7257bSPatrick Caulfield 		close_connection(con, false);
14886ed7257bSPatrick Caulfield 		kmem_cache_free(con_cache, con);
14896ed7257bSPatrick Caulfield 	}
14906ed7257bSPatrick Caulfield 	kmem_cache_destroy(con_cache);
14916ed7257bSPatrick Caulfield 
14926ed7257bSPatrick Caulfield out:
14936ed7257bSPatrick Caulfield 	return error;
14946ed7257bSPatrick Caulfield }
1495