xref: /openbmc/linux/net/sunrpc/xprtsock.c (revision 5fe46e9d733f19a880ef7e516002bd4c2b833e14)
1a246b010SChuck Lever /*
2a246b010SChuck Lever  * linux/net/sunrpc/xprtsock.c
3a246b010SChuck Lever  *
4a246b010SChuck Lever  * Client-side transport implementation for sockets.
5a246b010SChuck Lever  *
6113aa838SAlan Cox  * TCP callback races fixes (C) 1998 Red Hat
7113aa838SAlan Cox  * TCP send fixes (C) 1998 Red Hat
8a246b010SChuck Lever  * TCP NFS related read + write fixes
9a246b010SChuck Lever  *  (C) 1999 Dave Airlie, University of Limerick, Ireland <airlied@linux.ie>
10a246b010SChuck Lever  *
11a246b010SChuck Lever  * Rewrite of larges part of the code in order to stabilize TCP stuff.
12a246b010SChuck Lever  * Fix behaviour when socket buffer is full.
13a246b010SChuck Lever  *  (C) 1999 Trond Myklebust <trond.myklebust@fys.uio.no>
1455aa4f58SChuck Lever  *
1555aa4f58SChuck Lever  * IP socket transport implementation, (C) 2005 Chuck Lever <cel@netapp.com>
168f9d5b1aSChuck Lever  *
178f9d5b1aSChuck Lever  * IPv6 support contributed by Gilles Quillard, Bull Open Source, 2005.
188f9d5b1aSChuck Lever  *   <gilles.quillard@bull.net>
19a246b010SChuck Lever  */
20a246b010SChuck Lever 
21a246b010SChuck Lever #include <linux/types.h>
22a246b010SChuck Lever #include <linux/slab.h>
23bc25571eS\"Talpey, Thomas\ #include <linux/module.h>
24a246b010SChuck Lever #include <linux/capability.h>
25a246b010SChuck Lever #include <linux/pagemap.h>
26a246b010SChuck Lever #include <linux/errno.h>
27a246b010SChuck Lever #include <linux/socket.h>
28a246b010SChuck Lever #include <linux/in.h>
29a246b010SChuck Lever #include <linux/net.h>
30a246b010SChuck Lever #include <linux/mm.h>
31a246b010SChuck Lever #include <linux/udp.h>
32a246b010SChuck Lever #include <linux/tcp.h>
33a246b010SChuck Lever #include <linux/sunrpc/clnt.h>
3402107148SChuck Lever #include <linux/sunrpc/sched.h>
354cfc7e60SRahul Iyer #include <linux/sunrpc/svcsock.h>
3649c36fccS\"Talpey, Thomas\ #include <linux/sunrpc/xprtsock.h>
37a246b010SChuck Lever #include <linux/file.h>
3844b98efdSRicardo Labiaga #ifdef CONFIG_NFS_V4_1
3944b98efdSRicardo Labiaga #include <linux/sunrpc/bc_xprt.h>
4044b98efdSRicardo Labiaga #endif
41a246b010SChuck Lever 
42a246b010SChuck Lever #include <net/sock.h>
43a246b010SChuck Lever #include <net/checksum.h>
44a246b010SChuck Lever #include <net/udp.h>
45a246b010SChuck Lever #include <net/tcp.h>
46a246b010SChuck Lever 
474cfc7e60SRahul Iyer #include "sunrpc.h"
489903cd1cSChuck Lever /*
49c556b754SChuck Lever  * xprtsock tunables
50c556b754SChuck Lever  */
51c556b754SChuck Lever unsigned int xprt_udp_slot_table_entries = RPC_DEF_SLOT_TABLE;
52c556b754SChuck Lever unsigned int xprt_tcp_slot_table_entries = RPC_DEF_SLOT_TABLE;
53c556b754SChuck Lever 
54c556b754SChuck Lever unsigned int xprt_min_resvport = RPC_DEF_MIN_RESVPORT;
55c556b754SChuck Lever unsigned int xprt_max_resvport = RPC_DEF_MAX_RESVPORT;
56c556b754SChuck Lever 
577d1e8255STrond Myklebust #define XS_TCP_LINGER_TO	(15U * HZ)
5825fe6142STrond Myklebust static unsigned int xs_tcp_fin_timeout __read_mostly = XS_TCP_LINGER_TO;
597d1e8255STrond Myklebust 
60c556b754SChuck Lever /*
61fbf76683SChuck Lever  * We can register our own files under /proc/sys/sunrpc by
62fbf76683SChuck Lever  * calling register_sysctl_table() again.  The files in that
63fbf76683SChuck Lever  * directory become the union of all files registered there.
64fbf76683SChuck Lever  *
65fbf76683SChuck Lever  * We simply need to make sure that we don't collide with
66fbf76683SChuck Lever  * someone else's file names!
67fbf76683SChuck Lever  */
68fbf76683SChuck Lever 
69fbf76683SChuck Lever #ifdef RPC_DEBUG
70fbf76683SChuck Lever 
71fbf76683SChuck Lever static unsigned int min_slot_table_size = RPC_MIN_SLOT_TABLE;
72fbf76683SChuck Lever static unsigned int max_slot_table_size = RPC_MAX_SLOT_TABLE;
73fbf76683SChuck Lever static unsigned int xprt_min_resvport_limit = RPC_MIN_RESVPORT;
74fbf76683SChuck Lever static unsigned int xprt_max_resvport_limit = RPC_MAX_RESVPORT;
75fbf76683SChuck Lever 
76fbf76683SChuck Lever static struct ctl_table_header *sunrpc_table_header;
77fbf76683SChuck Lever 
78fbf76683SChuck Lever /*
79fbf76683SChuck Lever  * FIXME: changing the UDP slot table size should also resize the UDP
80fbf76683SChuck Lever  *        socket buffers for existing UDP transports
81fbf76683SChuck Lever  */
82fbf76683SChuck Lever static ctl_table xs_tunables_table[] = {
83fbf76683SChuck Lever 	{
84fbf76683SChuck Lever 		.procname	= "udp_slot_table_entries",
85fbf76683SChuck Lever 		.data		= &xprt_udp_slot_table_entries,
86fbf76683SChuck Lever 		.maxlen		= sizeof(unsigned int),
87fbf76683SChuck Lever 		.mode		= 0644,
886d456111SEric W. Biederman 		.proc_handler	= proc_dointvec_minmax,
89fbf76683SChuck Lever 		.extra1		= &min_slot_table_size,
90fbf76683SChuck Lever 		.extra2		= &max_slot_table_size
91fbf76683SChuck Lever 	},
92fbf76683SChuck Lever 	{
93fbf76683SChuck Lever 		.procname	= "tcp_slot_table_entries",
94fbf76683SChuck Lever 		.data		= &xprt_tcp_slot_table_entries,
95fbf76683SChuck Lever 		.maxlen		= sizeof(unsigned int),
96fbf76683SChuck Lever 		.mode		= 0644,
976d456111SEric W. Biederman 		.proc_handler	= proc_dointvec_minmax,
98fbf76683SChuck Lever 		.extra1		= &min_slot_table_size,
99fbf76683SChuck Lever 		.extra2		= &max_slot_table_size
100fbf76683SChuck Lever 	},
101fbf76683SChuck Lever 	{
102fbf76683SChuck Lever 		.procname	= "min_resvport",
103fbf76683SChuck Lever 		.data		= &xprt_min_resvport,
104fbf76683SChuck Lever 		.maxlen		= sizeof(unsigned int),
105fbf76683SChuck Lever 		.mode		= 0644,
1066d456111SEric W. Biederman 		.proc_handler	= proc_dointvec_minmax,
107fbf76683SChuck Lever 		.extra1		= &xprt_min_resvport_limit,
108fbf76683SChuck Lever 		.extra2		= &xprt_max_resvport_limit
109fbf76683SChuck Lever 	},
110fbf76683SChuck Lever 	{
111fbf76683SChuck Lever 		.procname	= "max_resvport",
112fbf76683SChuck Lever 		.data		= &xprt_max_resvport,
113fbf76683SChuck Lever 		.maxlen		= sizeof(unsigned int),
114fbf76683SChuck Lever 		.mode		= 0644,
1156d456111SEric W. Biederman 		.proc_handler	= proc_dointvec_minmax,
116fbf76683SChuck Lever 		.extra1		= &xprt_min_resvport_limit,
117fbf76683SChuck Lever 		.extra2		= &xprt_max_resvport_limit
118fbf76683SChuck Lever 	},
119fbf76683SChuck Lever 	{
12025fe6142STrond Myklebust 		.procname	= "tcp_fin_timeout",
12125fe6142STrond Myklebust 		.data		= &xs_tcp_fin_timeout,
12225fe6142STrond Myklebust 		.maxlen		= sizeof(xs_tcp_fin_timeout),
12325fe6142STrond Myklebust 		.mode		= 0644,
1246d456111SEric W. Biederman 		.proc_handler	= proc_dointvec_jiffies,
12525fe6142STrond Myklebust 	},
126f8572d8fSEric W. Biederman 	{ },
127fbf76683SChuck Lever };
128fbf76683SChuck Lever 
129fbf76683SChuck Lever static ctl_table sunrpc_table[] = {
130fbf76683SChuck Lever 	{
131fbf76683SChuck Lever 		.procname	= "sunrpc",
132fbf76683SChuck Lever 		.mode		= 0555,
133fbf76683SChuck Lever 		.child		= xs_tunables_table
134fbf76683SChuck Lever 	},
135f8572d8fSEric W. Biederman 	{ },
136fbf76683SChuck Lever };
137fbf76683SChuck Lever 
138fbf76683SChuck Lever #endif
139fbf76683SChuck Lever 
140fbf76683SChuck Lever /*
14103bf4b70SChuck Lever  * Time out for an RPC UDP socket connect.  UDP socket connects are
14203bf4b70SChuck Lever  * synchronous, but we set a timeout anyway in case of resource
14303bf4b70SChuck Lever  * exhaustion on the local host.
14403bf4b70SChuck Lever  */
14503bf4b70SChuck Lever #define XS_UDP_CONN_TO		(5U * HZ)
14603bf4b70SChuck Lever 
14703bf4b70SChuck Lever /*
14803bf4b70SChuck Lever  * Wait duration for an RPC TCP connection to be established.  Solaris
14903bf4b70SChuck Lever  * NFS over TCP uses 60 seconds, for example, which is in line with how
15003bf4b70SChuck Lever  * long a server takes to reboot.
15103bf4b70SChuck Lever  */
15203bf4b70SChuck Lever #define XS_TCP_CONN_TO		(60U * HZ)
15303bf4b70SChuck Lever 
15403bf4b70SChuck Lever /*
15503bf4b70SChuck Lever  * Wait duration for a reply from the RPC portmapper.
15603bf4b70SChuck Lever  */
15703bf4b70SChuck Lever #define XS_BIND_TO		(60U * HZ)
15803bf4b70SChuck Lever 
15903bf4b70SChuck Lever /*
16003bf4b70SChuck Lever  * Delay if a UDP socket connect error occurs.  This is most likely some
16103bf4b70SChuck Lever  * kind of resource problem on the local host.
16203bf4b70SChuck Lever  */
16303bf4b70SChuck Lever #define XS_UDP_REEST_TO		(2U * HZ)
16403bf4b70SChuck Lever 
16503bf4b70SChuck Lever /*
16603bf4b70SChuck Lever  * The reestablish timeout allows clients to delay for a bit before attempting
16703bf4b70SChuck Lever  * to reconnect to a server that just dropped our connection.
16803bf4b70SChuck Lever  *
16903bf4b70SChuck Lever  * We implement an exponential backoff when trying to reestablish a TCP
17003bf4b70SChuck Lever  * transport connection with the server.  Some servers like to drop a TCP
17103bf4b70SChuck Lever  * connection when they are overworked, so we start with a short timeout and
17203bf4b70SChuck Lever  * increase over time if the server is down or not responding.
17303bf4b70SChuck Lever  */
17403bf4b70SChuck Lever #define XS_TCP_INIT_REEST_TO	(3U * HZ)
17503bf4b70SChuck Lever #define XS_TCP_MAX_REEST_TO	(5U * 60 * HZ)
17603bf4b70SChuck Lever 
17703bf4b70SChuck Lever /*
17803bf4b70SChuck Lever  * TCP idle timeout; client drops the transport socket if it is idle
17903bf4b70SChuck Lever  * for this long.  Note that we also timeout UDP sockets to prevent
18003bf4b70SChuck Lever  * holding port numbers when there is no RPC traffic.
18103bf4b70SChuck Lever  */
18203bf4b70SChuck Lever #define XS_IDLE_DISC_TO		(5U * 60 * HZ)
18303bf4b70SChuck Lever 
184a246b010SChuck Lever #ifdef RPC_DEBUG
185a246b010SChuck Lever # undef  RPC_DEBUG_DATA
1869903cd1cSChuck Lever # define RPCDBG_FACILITY	RPCDBG_TRANS
187a246b010SChuck Lever #endif
188a246b010SChuck Lever 
189a246b010SChuck Lever #ifdef RPC_DEBUG_DATA
1909903cd1cSChuck Lever static void xs_pktdump(char *msg, u32 *packet, unsigned int count)
191a246b010SChuck Lever {
192a246b010SChuck Lever 	u8 *buf = (u8 *) packet;
193a246b010SChuck Lever 	int j;
194a246b010SChuck Lever 
195a246b010SChuck Lever 	dprintk("RPC:       %s\n", msg);
196a246b010SChuck Lever 	for (j = 0; j < count && j < 128; j += 4) {
197a246b010SChuck Lever 		if (!(j & 31)) {
198a246b010SChuck Lever 			if (j)
199a246b010SChuck Lever 				dprintk("\n");
200a246b010SChuck Lever 			dprintk("0x%04x ", j);
201a246b010SChuck Lever 		}
202a246b010SChuck Lever 		dprintk("%02x%02x%02x%02x ",
203a246b010SChuck Lever 			buf[j], buf[j+1], buf[j+2], buf[j+3]);
204a246b010SChuck Lever 	}
205a246b010SChuck Lever 	dprintk("\n");
206a246b010SChuck Lever }
207a246b010SChuck Lever #else
2089903cd1cSChuck Lever static inline void xs_pktdump(char *msg, u32 *packet, unsigned int count)
209a246b010SChuck Lever {
210a246b010SChuck Lever 	/* NOP */
211a246b010SChuck Lever }
212a246b010SChuck Lever #endif
213a246b010SChuck Lever 
214ffc2e518SChuck Lever struct sock_xprt {
215ffc2e518SChuck Lever 	struct rpc_xprt		xprt;
216ee0ac0c2SChuck Lever 
217ee0ac0c2SChuck Lever 	/*
218ee0ac0c2SChuck Lever 	 * Network layer
219ee0ac0c2SChuck Lever 	 */
220ee0ac0c2SChuck Lever 	struct socket *		sock;
221ee0ac0c2SChuck Lever 	struct sock *		inet;
22251971139SChuck Lever 
22351971139SChuck Lever 	/*
22451971139SChuck Lever 	 * State of TCP reply receive
22551971139SChuck Lever 	 */
22651971139SChuck Lever 	__be32			tcp_fraghdr,
22751971139SChuck Lever 				tcp_xid;
22851971139SChuck Lever 
22951971139SChuck Lever 	u32			tcp_offset,
23051971139SChuck Lever 				tcp_reclen;
23151971139SChuck Lever 
23251971139SChuck Lever 	unsigned long		tcp_copied,
23351971139SChuck Lever 				tcp_flags;
234c8475461SChuck Lever 
235c8475461SChuck Lever 	/*
236c8475461SChuck Lever 	 * Connection of transports
237c8475461SChuck Lever 	 */
23834161db6STrond Myklebust 	struct delayed_work	connect_worker;
239fbfffbd5SChuck Lever 	struct sockaddr_storage	srcaddr;
240fbfffbd5SChuck Lever 	unsigned short		srcport;
2417c6e066eSChuck Lever 
2427c6e066eSChuck Lever 	/*
2437c6e066eSChuck Lever 	 * UDP socket buffer size parameters
2447c6e066eSChuck Lever 	 */
2457c6e066eSChuck Lever 	size_t			rcvsize,
2467c6e066eSChuck Lever 				sndsize;
247314dfd79SChuck Lever 
248314dfd79SChuck Lever 	/*
249314dfd79SChuck Lever 	 * Saved socket callback addresses
250314dfd79SChuck Lever 	 */
251314dfd79SChuck Lever 	void			(*old_data_ready)(struct sock *, int);
252314dfd79SChuck Lever 	void			(*old_state_change)(struct sock *);
253314dfd79SChuck Lever 	void			(*old_write_space)(struct sock *);
2542a9e1cfaSTrond Myklebust 	void			(*old_error_report)(struct sock *);
255ffc2e518SChuck Lever };
256ffc2e518SChuck Lever 
257e136d092SChuck Lever /*
258e136d092SChuck Lever  * TCP receive state flags
259e136d092SChuck Lever  */
260e136d092SChuck Lever #define TCP_RCV_LAST_FRAG	(1UL << 0)
261e136d092SChuck Lever #define TCP_RCV_COPY_FRAGHDR	(1UL << 1)
262e136d092SChuck Lever #define TCP_RCV_COPY_XID	(1UL << 2)
263e136d092SChuck Lever #define TCP_RCV_COPY_DATA	(1UL << 3)
264f4a2e418SRicardo Labiaga #define TCP_RCV_READ_CALLDIR	(1UL << 4)
265f4a2e418SRicardo Labiaga #define TCP_RCV_COPY_CALLDIR	(1UL << 5)
26618dca02aSRicardo Labiaga 
26718dca02aSRicardo Labiaga /*
26818dca02aSRicardo Labiaga  * TCP RPC flags
26918dca02aSRicardo Labiaga  */
270f4a2e418SRicardo Labiaga #define TCP_RPC_REPLY		(1UL << 6)
271e136d092SChuck Lever 
27295392c59SChuck Lever static inline struct sockaddr *xs_addr(struct rpc_xprt *xprt)
273edb267a6SChuck Lever {
27495392c59SChuck Lever 	return (struct sockaddr *) &xprt->addr;
27595392c59SChuck Lever }
27695392c59SChuck Lever 
27795392c59SChuck Lever static inline struct sockaddr_in *xs_addr_in(struct rpc_xprt *xprt)
27895392c59SChuck Lever {
27995392c59SChuck Lever 	return (struct sockaddr_in *) &xprt->addr;
28095392c59SChuck Lever }
28195392c59SChuck Lever 
28295392c59SChuck Lever static inline struct sockaddr_in6 *xs_addr_in6(struct rpc_xprt *xprt)
28395392c59SChuck Lever {
28495392c59SChuck Lever 	return (struct sockaddr_in6 *) &xprt->addr;
28595392c59SChuck Lever }
28695392c59SChuck Lever 
287c877b849SChuck Lever static void xs_format_common_peer_addresses(struct rpc_xprt *xprt)
288c877b849SChuck Lever {
289c877b849SChuck Lever 	struct sockaddr *sap = xs_addr(xprt);
2909dc3b095SChuck Lever 	struct sockaddr_in6 *sin6;
2919dc3b095SChuck Lever 	struct sockaddr_in *sin;
292c877b849SChuck Lever 	char buf[128];
293c877b849SChuck Lever 
294c877b849SChuck Lever 	(void)rpc_ntop(sap, buf, sizeof(buf));
295c877b849SChuck Lever 	xprt->address_strings[RPC_DISPLAY_ADDR] = kstrdup(buf, GFP_KERNEL);
296c877b849SChuck Lever 
2979dc3b095SChuck Lever 	switch (sap->sa_family) {
2989dc3b095SChuck Lever 	case AF_INET:
2999dc3b095SChuck Lever 		sin = xs_addr_in(xprt);
3009dc3b095SChuck Lever 		(void)snprintf(buf, sizeof(buf), "%02x%02x%02x%02x",
3019dc3b095SChuck Lever 					NIPQUAD(sin->sin_addr.s_addr));
3029dc3b095SChuck Lever 		break;
3039dc3b095SChuck Lever 	case AF_INET6:
3049dc3b095SChuck Lever 		sin6 = xs_addr_in6(xprt);
3059dc3b095SChuck Lever 		(void)snprintf(buf, sizeof(buf), "%pi6", &sin6->sin6_addr);
3069dc3b095SChuck Lever 		break;
3079dc3b095SChuck Lever 	default:
3089dc3b095SChuck Lever 		BUG();
3099dc3b095SChuck Lever 	}
3109dc3b095SChuck Lever 	xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = kstrdup(buf, GFP_KERNEL);
3119dc3b095SChuck Lever }
3129dc3b095SChuck Lever 
3139dc3b095SChuck Lever static void xs_format_common_peer_ports(struct rpc_xprt *xprt)
3149dc3b095SChuck Lever {
3159dc3b095SChuck Lever 	struct sockaddr *sap = xs_addr(xprt);
3169dc3b095SChuck Lever 	char buf[128];
3179dc3b095SChuck Lever 
318c877b849SChuck Lever 	(void)snprintf(buf, sizeof(buf), "%u", rpc_get_port(sap));
319c877b849SChuck Lever 	xprt->address_strings[RPC_DISPLAY_PORT] = kstrdup(buf, GFP_KERNEL);
320c877b849SChuck Lever 
321c877b849SChuck Lever 	(void)snprintf(buf, sizeof(buf), "%4hx", rpc_get_port(sap));
322c877b849SChuck Lever 	xprt->address_strings[RPC_DISPLAY_HEX_PORT] = kstrdup(buf, GFP_KERNEL);
323c877b849SChuck Lever }
324c877b849SChuck Lever 
3259dc3b095SChuck Lever static void xs_format_peer_addresses(struct rpc_xprt *xprt,
326b454ae90SChuck Lever 				     const char *protocol,
327b454ae90SChuck Lever 				     const char *netid)
328edb267a6SChuck Lever {
329b454ae90SChuck Lever 	xprt->address_strings[RPC_DISPLAY_PROTO] = protocol;
330b454ae90SChuck Lever 	xprt->address_strings[RPC_DISPLAY_NETID] = netid;
331c877b849SChuck Lever 	xs_format_common_peer_addresses(xprt);
3329dc3b095SChuck Lever 	xs_format_common_peer_ports(xprt);
333edb267a6SChuck Lever }
334edb267a6SChuck Lever 
3359dc3b095SChuck Lever static void xs_update_peer_port(struct rpc_xprt *xprt)
3364b6473fbSChuck Lever {
3379dc3b095SChuck Lever 	kfree(xprt->address_strings[RPC_DISPLAY_HEX_PORT]);
3389dc3b095SChuck Lever 	kfree(xprt->address_strings[RPC_DISPLAY_PORT]);
3394b6473fbSChuck Lever 
3409dc3b095SChuck Lever 	xs_format_common_peer_ports(xprt);
341edb267a6SChuck Lever }
342edb267a6SChuck Lever 
343edb267a6SChuck Lever static void xs_free_peer_addresses(struct rpc_xprt *xprt)
344edb267a6SChuck Lever {
34533e01dc7SChuck Lever 	unsigned int i;
34633e01dc7SChuck Lever 
34733e01dc7SChuck Lever 	for (i = 0; i < RPC_DISPLAY_MAX; i++)
34833e01dc7SChuck Lever 		switch (i) {
34933e01dc7SChuck Lever 		case RPC_DISPLAY_PROTO:
35033e01dc7SChuck Lever 		case RPC_DISPLAY_NETID:
35133e01dc7SChuck Lever 			continue;
35233e01dc7SChuck Lever 		default:
35333e01dc7SChuck Lever 			kfree(xprt->address_strings[i]);
35433e01dc7SChuck Lever 		}
355edb267a6SChuck Lever }
356edb267a6SChuck Lever 
357b4b5cc85SChuck Lever #define XS_SENDMSG_FLAGS	(MSG_DONTWAIT | MSG_NOSIGNAL)
358b4b5cc85SChuck Lever 
35924c5684bSTrond Myklebust static int xs_send_kvec(struct socket *sock, struct sockaddr *addr, int addrlen, struct kvec *vec, unsigned int base, int more)
360b4b5cc85SChuck Lever {
361b4b5cc85SChuck Lever 	struct msghdr msg = {
362b4b5cc85SChuck Lever 		.msg_name	= addr,
363b4b5cc85SChuck Lever 		.msg_namelen	= addrlen,
36424c5684bSTrond Myklebust 		.msg_flags	= XS_SENDMSG_FLAGS | (more ? MSG_MORE : 0),
36524c5684bSTrond Myklebust 	};
36624c5684bSTrond Myklebust 	struct kvec iov = {
36724c5684bSTrond Myklebust 		.iov_base	= vec->iov_base + base,
36824c5684bSTrond Myklebust 		.iov_len	= vec->iov_len - base,
369b4b5cc85SChuck Lever 	};
370b4b5cc85SChuck Lever 
37124c5684bSTrond Myklebust 	if (iov.iov_len != 0)
372b4b5cc85SChuck Lever 		return kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len);
373b4b5cc85SChuck Lever 	return kernel_sendmsg(sock, &msg, NULL, 0, 0);
374b4b5cc85SChuck Lever }
375b4b5cc85SChuck Lever 
37624c5684bSTrond Myklebust static int xs_send_pagedata(struct socket *sock, struct xdr_buf *xdr, unsigned int base, int more)
377b4b5cc85SChuck Lever {
37824c5684bSTrond Myklebust 	struct page **ppage;
37924c5684bSTrond Myklebust 	unsigned int remainder;
38024c5684bSTrond Myklebust 	int err, sent = 0;
381b4b5cc85SChuck Lever 
38224c5684bSTrond Myklebust 	remainder = xdr->page_len - base;
38324c5684bSTrond Myklebust 	base += xdr->page_base;
38424c5684bSTrond Myklebust 	ppage = xdr->pages + (base >> PAGE_SHIFT);
38524c5684bSTrond Myklebust 	base &= ~PAGE_MASK;
38624c5684bSTrond Myklebust 	for(;;) {
38724c5684bSTrond Myklebust 		unsigned int len = min_t(unsigned int, PAGE_SIZE - base, remainder);
38824c5684bSTrond Myklebust 		int flags = XS_SENDMSG_FLAGS;
38924c5684bSTrond Myklebust 
39024c5684bSTrond Myklebust 		remainder -= len;
39124c5684bSTrond Myklebust 		if (remainder != 0 || more)
39224c5684bSTrond Myklebust 			flags |= MSG_MORE;
39324c5684bSTrond Myklebust 		err = sock->ops->sendpage(sock, *ppage, base, len, flags);
39424c5684bSTrond Myklebust 		if (remainder == 0 || err != len)
39524c5684bSTrond Myklebust 			break;
39624c5684bSTrond Myklebust 		sent += err;
39724c5684bSTrond Myklebust 		ppage++;
39824c5684bSTrond Myklebust 		base = 0;
39924c5684bSTrond Myklebust 	}
40024c5684bSTrond Myklebust 	if (sent == 0)
40124c5684bSTrond Myklebust 		return err;
40224c5684bSTrond Myklebust 	if (err > 0)
40324c5684bSTrond Myklebust 		sent += err;
40424c5684bSTrond Myklebust 	return sent;
405b4b5cc85SChuck Lever }
406b4b5cc85SChuck Lever 
4079903cd1cSChuck Lever /**
4089903cd1cSChuck Lever  * xs_sendpages - write pages directly to a socket
4099903cd1cSChuck Lever  * @sock: socket to send on
4109903cd1cSChuck Lever  * @addr: UDP only -- address of destination
4119903cd1cSChuck Lever  * @addrlen: UDP only -- length of destination address
4129903cd1cSChuck Lever  * @xdr: buffer containing this request
4139903cd1cSChuck Lever  * @base: starting position in the buffer
4149903cd1cSChuck Lever  *
415a246b010SChuck Lever  */
41624c5684bSTrond Myklebust static int xs_sendpages(struct socket *sock, struct sockaddr *addr, int addrlen, struct xdr_buf *xdr, unsigned int base)
417a246b010SChuck Lever {
41824c5684bSTrond Myklebust 	unsigned int remainder = xdr->len - base;
41924c5684bSTrond Myklebust 	int err, sent = 0;
420a246b010SChuck Lever 
421262965f5SChuck Lever 	if (unlikely(!sock))
422fba91afbSTrond Myklebust 		return -ENOTSOCK;
423262965f5SChuck Lever 
424262965f5SChuck Lever 	clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags);
42524c5684bSTrond Myklebust 	if (base != 0) {
42624c5684bSTrond Myklebust 		addr = NULL;
42724c5684bSTrond Myklebust 		addrlen = 0;
42824c5684bSTrond Myklebust 	}
429262965f5SChuck Lever 
43024c5684bSTrond Myklebust 	if (base < xdr->head[0].iov_len || addr != NULL) {
43124c5684bSTrond Myklebust 		unsigned int len = xdr->head[0].iov_len - base;
43224c5684bSTrond Myklebust 		remainder -= len;
43324c5684bSTrond Myklebust 		err = xs_send_kvec(sock, addr, addrlen, &xdr->head[0], base, remainder != 0);
43424c5684bSTrond Myklebust 		if (remainder == 0 || err != len)
435a246b010SChuck Lever 			goto out;
43624c5684bSTrond Myklebust 		sent += err;
437a246b010SChuck Lever 		base = 0;
438a246b010SChuck Lever 	} else
43924c5684bSTrond Myklebust 		base -= xdr->head[0].iov_len;
440a246b010SChuck Lever 
44124c5684bSTrond Myklebust 	if (base < xdr->page_len) {
44224c5684bSTrond Myklebust 		unsigned int len = xdr->page_len - base;
44324c5684bSTrond Myklebust 		remainder -= len;
44424c5684bSTrond Myklebust 		err = xs_send_pagedata(sock, xdr, base, remainder != 0);
44524c5684bSTrond Myklebust 		if (remainder == 0 || err != len)
446a246b010SChuck Lever 			goto out;
44724c5684bSTrond Myklebust 		sent += err;
448a246b010SChuck Lever 		base = 0;
44924c5684bSTrond Myklebust 	} else
45024c5684bSTrond Myklebust 		base -= xdr->page_len;
45124c5684bSTrond Myklebust 
45224c5684bSTrond Myklebust 	if (base >= xdr->tail[0].iov_len)
45324c5684bSTrond Myklebust 		return sent;
45424c5684bSTrond Myklebust 	err = xs_send_kvec(sock, NULL, 0, &xdr->tail[0], base, 0);
455a246b010SChuck Lever out:
45624c5684bSTrond Myklebust 	if (sent == 0)
45724c5684bSTrond Myklebust 		return err;
45824c5684bSTrond Myklebust 	if (err > 0)
45924c5684bSTrond Myklebust 		sent += err;
46024c5684bSTrond Myklebust 	return sent;
461a246b010SChuck Lever }
462a246b010SChuck Lever 
463b6ddf64fSTrond Myklebust static void xs_nospace_callback(struct rpc_task *task)
464b6ddf64fSTrond Myklebust {
465b6ddf64fSTrond Myklebust 	struct sock_xprt *transport = container_of(task->tk_rqstp->rq_xprt, struct sock_xprt, xprt);
466b6ddf64fSTrond Myklebust 
467b6ddf64fSTrond Myklebust 	transport->inet->sk_write_pending--;
468b6ddf64fSTrond Myklebust 	clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
469b6ddf64fSTrond Myklebust }
470b6ddf64fSTrond Myklebust 
4719903cd1cSChuck Lever /**
472262965f5SChuck Lever  * xs_nospace - place task on wait queue if transmit was incomplete
473262965f5SChuck Lever  * @task: task to put to sleep
4749903cd1cSChuck Lever  *
475a246b010SChuck Lever  */
4765e3771ceSTrond Myklebust static int xs_nospace(struct rpc_task *task)
477a246b010SChuck Lever {
478262965f5SChuck Lever 	struct rpc_rqst *req = task->tk_rqstp;
479262965f5SChuck Lever 	struct rpc_xprt *xprt = req->rq_xprt;
480ee0ac0c2SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
4815e3771ceSTrond Myklebust 	int ret = 0;
482a246b010SChuck Lever 
48346121cf7SChuck Lever 	dprintk("RPC: %5u xmit incomplete (%u left of %u)\n",
484262965f5SChuck Lever 			task->tk_pid, req->rq_slen - req->rq_bytes_sent,
485262965f5SChuck Lever 			req->rq_slen);
486a246b010SChuck Lever 
487262965f5SChuck Lever 	/* Protect against races with write_space */
488262965f5SChuck Lever 	spin_lock_bh(&xprt->transport_lock);
489a246b010SChuck Lever 
490262965f5SChuck Lever 	/* Don't race with disconnect */
491b6ddf64fSTrond Myklebust 	if (xprt_connected(xprt)) {
492b6ddf64fSTrond Myklebust 		if (test_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags)) {
4935e3771ceSTrond Myklebust 			ret = -EAGAIN;
494b6ddf64fSTrond Myklebust 			/*
495b6ddf64fSTrond Myklebust 			 * Notify TCP that we're limited by the application
496b6ddf64fSTrond Myklebust 			 * window size
497b6ddf64fSTrond Myklebust 			 */
498b6ddf64fSTrond Myklebust 			set_bit(SOCK_NOSPACE, &transport->sock->flags);
499b6ddf64fSTrond Myklebust 			transport->inet->sk_write_pending++;
500b6ddf64fSTrond Myklebust 			/* ...and wait for more buffer space */
501b6ddf64fSTrond Myklebust 			xprt_wait_for_buffer_space(task, xs_nospace_callback);
502b6ddf64fSTrond Myklebust 		}
503b6ddf64fSTrond Myklebust 	} else {
504b6ddf64fSTrond Myklebust 		clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
5055e3771ceSTrond Myklebust 		ret = -ENOTCONN;
506b6ddf64fSTrond Myklebust 	}
507a246b010SChuck Lever 
508262965f5SChuck Lever 	spin_unlock_bh(&xprt->transport_lock);
5095e3771ceSTrond Myklebust 	return ret;
510a246b010SChuck Lever }
511a246b010SChuck Lever 
5129903cd1cSChuck Lever /**
513262965f5SChuck Lever  * xs_udp_send_request - write an RPC request to a UDP socket
5149903cd1cSChuck Lever  * @task: address of RPC task that manages the state of an RPC request
5159903cd1cSChuck Lever  *
5169903cd1cSChuck Lever  * Return values:
5179903cd1cSChuck Lever  *        0:	The request has been sent
5189903cd1cSChuck Lever  *   EAGAIN:	The socket was blocked, please call again later to
5199903cd1cSChuck Lever  *		complete the request
520262965f5SChuck Lever  * ENOTCONN:	Caller needs to invoke connect logic then call again
5219903cd1cSChuck Lever  *    other:	Some other error occured, the request was not sent
5229903cd1cSChuck Lever  */
523262965f5SChuck Lever static int xs_udp_send_request(struct rpc_task *task)
524a246b010SChuck Lever {
525a246b010SChuck Lever 	struct rpc_rqst *req = task->tk_rqstp;
526a246b010SChuck Lever 	struct rpc_xprt *xprt = req->rq_xprt;
527ee0ac0c2SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
528262965f5SChuck Lever 	struct xdr_buf *xdr = &req->rq_snd_buf;
529262965f5SChuck Lever 	int status;
530262965f5SChuck Lever 
531262965f5SChuck Lever 	xs_pktdump("packet data:",
532262965f5SChuck Lever 				req->rq_svec->iov_base,
533262965f5SChuck Lever 				req->rq_svec->iov_len);
534262965f5SChuck Lever 
53501d37c42STrond Myklebust 	if (!xprt_bound(xprt))
53601d37c42STrond Myklebust 		return -ENOTCONN;
537ee0ac0c2SChuck Lever 	status = xs_sendpages(transport->sock,
53895392c59SChuck Lever 			      xs_addr(xprt),
539ee0ac0c2SChuck Lever 			      xprt->addrlen, xdr,
540ee0ac0c2SChuck Lever 			      req->rq_bytes_sent);
541262965f5SChuck Lever 
542262965f5SChuck Lever 	dprintk("RPC:       xs_udp_send_request(%u) = %d\n",
543262965f5SChuck Lever 			xdr->len - req->rq_bytes_sent, status);
544262965f5SChuck Lever 
5452199700fSTrond Myklebust 	if (status >= 0) {
5461321d8d9SChuck Lever 		task->tk_bytes_sent += status;
5472199700fSTrond Myklebust 		if (status >= req->rq_slen)
548262965f5SChuck Lever 			return 0;
549262965f5SChuck Lever 		/* Still some bytes left; set up for a retry later. */
550262965f5SChuck Lever 		status = -EAGAIN;
5512199700fSTrond Myklebust 	}
552262965f5SChuck Lever 
553262965f5SChuck Lever 	switch (status) {
554fba91afbSTrond Myklebust 	case -ENOTSOCK:
555fba91afbSTrond Myklebust 		status = -ENOTCONN;
556fba91afbSTrond Myklebust 		/* Should we call xs_close() here? */
557fba91afbSTrond Myklebust 		break;
558b6ddf64fSTrond Myklebust 	case -EAGAIN:
5595e3771ceSTrond Myklebust 		status = xs_nospace(task);
560b6ddf64fSTrond Myklebust 		break;
561c8485e4dSTrond Myklebust 	default:
562c8485e4dSTrond Myklebust 		dprintk("RPC:       sendmsg returned unrecognized error %d\n",
563c8485e4dSTrond Myklebust 			-status);
564262965f5SChuck Lever 	case -ENETUNREACH:
565262965f5SChuck Lever 	case -EPIPE:
566262965f5SChuck Lever 	case -ECONNREFUSED:
567262965f5SChuck Lever 		/* When the server has died, an ICMP port unreachable message
568262965f5SChuck Lever 		 * prompts ECONNREFUSED. */
569b6ddf64fSTrond Myklebust 		clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
570262965f5SChuck Lever 	}
571*5fe46e9dSBian Naimeng 
572262965f5SChuck Lever 	return status;
573262965f5SChuck Lever }
574262965f5SChuck Lever 
575e06799f9STrond Myklebust /**
576e06799f9STrond Myklebust  * xs_tcp_shutdown - gracefully shut down a TCP socket
577e06799f9STrond Myklebust  * @xprt: transport
578e06799f9STrond Myklebust  *
579e06799f9STrond Myklebust  * Initiates a graceful shutdown of the TCP socket by calling the
580e06799f9STrond Myklebust  * equivalent of shutdown(SHUT_WR);
581e06799f9STrond Myklebust  */
582e06799f9STrond Myklebust static void xs_tcp_shutdown(struct rpc_xprt *xprt)
583e06799f9STrond Myklebust {
584e06799f9STrond Myklebust 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
585e06799f9STrond Myklebust 	struct socket *sock = transport->sock;
586e06799f9STrond Myklebust 
587e06799f9STrond Myklebust 	if (sock != NULL)
588e06799f9STrond Myklebust 		kernel_sock_shutdown(sock, SHUT_WR);
589e06799f9STrond Myklebust }
590e06799f9STrond Myklebust 
591808012fbSChuck Lever static inline void xs_encode_tcp_record_marker(struct xdr_buf *buf)
592808012fbSChuck Lever {
593808012fbSChuck Lever 	u32 reclen = buf->len - sizeof(rpc_fraghdr);
594808012fbSChuck Lever 	rpc_fraghdr *base = buf->head[0].iov_base;
595808012fbSChuck Lever 	*base = htonl(RPC_LAST_STREAM_FRAGMENT | reclen);
596808012fbSChuck Lever }
597808012fbSChuck Lever 
598262965f5SChuck Lever /**
599262965f5SChuck Lever  * xs_tcp_send_request - write an RPC request to a TCP socket
600262965f5SChuck Lever  * @task: address of RPC task that manages the state of an RPC request
601262965f5SChuck Lever  *
602262965f5SChuck Lever  * Return values:
603262965f5SChuck Lever  *        0:	The request has been sent
604262965f5SChuck Lever  *   EAGAIN:	The socket was blocked, please call again later to
605262965f5SChuck Lever  *		complete the request
606262965f5SChuck Lever  * ENOTCONN:	Caller needs to invoke connect logic then call again
607262965f5SChuck Lever  *    other:	Some other error occured, the request was not sent
608262965f5SChuck Lever  *
609262965f5SChuck Lever  * XXX: In the case of soft timeouts, should we eventually give up
610262965f5SChuck Lever  *	if sendmsg is not able to make progress?
611262965f5SChuck Lever  */
612262965f5SChuck Lever static int xs_tcp_send_request(struct rpc_task *task)
613262965f5SChuck Lever {
614262965f5SChuck Lever 	struct rpc_rqst *req = task->tk_rqstp;
615262965f5SChuck Lever 	struct rpc_xprt *xprt = req->rq_xprt;
616ee0ac0c2SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
617262965f5SChuck Lever 	struct xdr_buf *xdr = &req->rq_snd_buf;
618b595bb15SChuck Lever 	int status;
619a246b010SChuck Lever 
620808012fbSChuck Lever 	xs_encode_tcp_record_marker(&req->rq_snd_buf);
621262965f5SChuck Lever 
622262965f5SChuck Lever 	xs_pktdump("packet data:",
623262965f5SChuck Lever 				req->rq_svec->iov_base,
624262965f5SChuck Lever 				req->rq_svec->iov_len);
625a246b010SChuck Lever 
626a246b010SChuck Lever 	/* Continue transmitting the packet/record. We must be careful
627a246b010SChuck Lever 	 * to cope with writespace callbacks arriving _after_ we have
628262965f5SChuck Lever 	 * called sendmsg(). */
629a246b010SChuck Lever 	while (1) {
630ee0ac0c2SChuck Lever 		status = xs_sendpages(transport->sock,
631ee0ac0c2SChuck Lever 					NULL, 0, xdr, req->rq_bytes_sent);
632a246b010SChuck Lever 
633262965f5SChuck Lever 		dprintk("RPC:       xs_tcp_send_request(%u) = %d\n",
634262965f5SChuck Lever 				xdr->len - req->rq_bytes_sent, status);
635262965f5SChuck Lever 
636262965f5SChuck Lever 		if (unlikely(status < 0))
637a246b010SChuck Lever 			break;
638a246b010SChuck Lever 
639a246b010SChuck Lever 		/* If we've sent the entire packet, immediately
640a246b010SChuck Lever 		 * reset the count of bytes sent. */
641262965f5SChuck Lever 		req->rq_bytes_sent += status;
642ef759a2eSChuck Lever 		task->tk_bytes_sent += status;
643262965f5SChuck Lever 		if (likely(req->rq_bytes_sent >= req->rq_slen)) {
644a246b010SChuck Lever 			req->rq_bytes_sent = 0;
645a246b010SChuck Lever 			return 0;
646a246b010SChuck Lever 		}
647262965f5SChuck Lever 
64806b4b681STrond Myklebust 		if (status != 0)
64906b4b681STrond Myklebust 			continue;
650a246b010SChuck Lever 		status = -EAGAIN;
651a246b010SChuck Lever 		break;
652a246b010SChuck Lever 	}
653a246b010SChuck Lever 
654262965f5SChuck Lever 	switch (status) {
655fba91afbSTrond Myklebust 	case -ENOTSOCK:
656fba91afbSTrond Myklebust 		status = -ENOTCONN;
657fba91afbSTrond Myklebust 		/* Should we call xs_close() here? */
658fba91afbSTrond Myklebust 		break;
659262965f5SChuck Lever 	case -EAGAIN:
6605e3771ceSTrond Myklebust 		status = xs_nospace(task);
661262965f5SChuck Lever 		break;
662262965f5SChuck Lever 	default:
663262965f5SChuck Lever 		dprintk("RPC:       sendmsg returned unrecognized error %d\n",
664262965f5SChuck Lever 			-status);
665a246b010SChuck Lever 	case -ECONNRESET:
66655420c24STrond Myklebust 	case -EPIPE:
667e06799f9STrond Myklebust 		xs_tcp_shutdown(xprt);
668a246b010SChuck Lever 	case -ECONNREFUSED:
669a246b010SChuck Lever 	case -ENOTCONN:
670a246b010SChuck Lever 		clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
671a246b010SChuck Lever 	}
672*5fe46e9dSBian Naimeng 
673a246b010SChuck Lever 	return status;
674a246b010SChuck Lever }
675a246b010SChuck Lever 
6769903cd1cSChuck Lever /**
677e0ab53deSTrond Myklebust  * xs_tcp_release_xprt - clean up after a tcp transmission
678e0ab53deSTrond Myklebust  * @xprt: transport
679e0ab53deSTrond Myklebust  * @task: rpc task
680e0ab53deSTrond Myklebust  *
681e0ab53deSTrond Myklebust  * This cleans up if an error causes us to abort the transmission of a request.
682e0ab53deSTrond Myklebust  * In this case, the socket may need to be reset in order to avoid confusing
683e0ab53deSTrond Myklebust  * the server.
684e0ab53deSTrond Myklebust  */
685e0ab53deSTrond Myklebust static void xs_tcp_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
686e0ab53deSTrond Myklebust {
687e0ab53deSTrond Myklebust 	struct rpc_rqst *req;
688e0ab53deSTrond Myklebust 
689e0ab53deSTrond Myklebust 	if (task != xprt->snd_task)
690e0ab53deSTrond Myklebust 		return;
691e0ab53deSTrond Myklebust 	if (task == NULL)
692e0ab53deSTrond Myklebust 		goto out_release;
693e0ab53deSTrond Myklebust 	req = task->tk_rqstp;
694e0ab53deSTrond Myklebust 	if (req->rq_bytes_sent == 0)
695e0ab53deSTrond Myklebust 		goto out_release;
696e0ab53deSTrond Myklebust 	if (req->rq_bytes_sent == req->rq_snd_buf.len)
697e0ab53deSTrond Myklebust 		goto out_release;
698e0ab53deSTrond Myklebust 	set_bit(XPRT_CLOSE_WAIT, &task->tk_xprt->state);
699e0ab53deSTrond Myklebust out_release:
700e0ab53deSTrond Myklebust 	xprt_release_xprt(xprt, task);
701e0ab53deSTrond Myklebust }
702e0ab53deSTrond Myklebust 
7032a9e1cfaSTrond Myklebust static void xs_save_old_callbacks(struct sock_xprt *transport, struct sock *sk)
7042a9e1cfaSTrond Myklebust {
7052a9e1cfaSTrond Myklebust 	transport->old_data_ready = sk->sk_data_ready;
7062a9e1cfaSTrond Myklebust 	transport->old_state_change = sk->sk_state_change;
7072a9e1cfaSTrond Myklebust 	transport->old_write_space = sk->sk_write_space;
7082a9e1cfaSTrond Myklebust 	transport->old_error_report = sk->sk_error_report;
7092a9e1cfaSTrond Myklebust }
7102a9e1cfaSTrond Myklebust 
7112a9e1cfaSTrond Myklebust static void xs_restore_old_callbacks(struct sock_xprt *transport, struct sock *sk)
7122a9e1cfaSTrond Myklebust {
7132a9e1cfaSTrond Myklebust 	sk->sk_data_ready = transport->old_data_ready;
7142a9e1cfaSTrond Myklebust 	sk->sk_state_change = transport->old_state_change;
7152a9e1cfaSTrond Myklebust 	sk->sk_write_space = transport->old_write_space;
7162a9e1cfaSTrond Myklebust 	sk->sk_error_report = transport->old_error_report;
7172a9e1cfaSTrond Myklebust }
7182a9e1cfaSTrond Myklebust 
719fe315e76SChuck Lever static void xs_reset_transport(struct sock_xprt *transport)
720a246b010SChuck Lever {
721ee0ac0c2SChuck Lever 	struct socket *sock = transport->sock;
722ee0ac0c2SChuck Lever 	struct sock *sk = transport->inet;
723a246b010SChuck Lever 
724fe315e76SChuck Lever 	if (sk == NULL)
725fe315e76SChuck Lever 		return;
7269903cd1cSChuck Lever 
727a246b010SChuck Lever 	write_lock_bh(&sk->sk_callback_lock);
728ee0ac0c2SChuck Lever 	transport->inet = NULL;
729ee0ac0c2SChuck Lever 	transport->sock = NULL;
730a246b010SChuck Lever 
731a246b010SChuck Lever 	sk->sk_user_data = NULL;
7322a9e1cfaSTrond Myklebust 
7332a9e1cfaSTrond Myklebust 	xs_restore_old_callbacks(transport, sk);
734a246b010SChuck Lever 	write_unlock_bh(&sk->sk_callback_lock);
735a246b010SChuck Lever 
736a246b010SChuck Lever 	sk->sk_no_check = 0;
737a246b010SChuck Lever 
738a246b010SChuck Lever 	sock_release(sock);
739fe315e76SChuck Lever }
740fe315e76SChuck Lever 
741fe315e76SChuck Lever /**
742fe315e76SChuck Lever  * xs_close - close a socket
743fe315e76SChuck Lever  * @xprt: transport
744fe315e76SChuck Lever  *
745fe315e76SChuck Lever  * This is used when all requests are complete; ie, no DRC state remains
746fe315e76SChuck Lever  * on the server we want to save.
747f75e6745STrond Myklebust  *
748f75e6745STrond Myklebust  * The caller _must_ be holding XPRT_LOCKED in order to avoid issues with
749f75e6745STrond Myklebust  * xs_reset_transport() zeroing the socket from underneath a writer.
750fe315e76SChuck Lever  */
751fe315e76SChuck Lever static void xs_close(struct rpc_xprt *xprt)
752fe315e76SChuck Lever {
753fe315e76SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
754fe315e76SChuck Lever 
755fe315e76SChuck Lever 	dprintk("RPC:       xs_close xprt %p\n", xprt);
756fe315e76SChuck Lever 
757fe315e76SChuck Lever 	xs_reset_transport(transport);
75861d0a8e6SNeil Brown 	xprt->reestablish_timeout = 0;
759fe315e76SChuck Lever 
760632e3bdcSTrond Myklebust 	smp_mb__before_clear_bit();
7617d1e8255STrond Myklebust 	clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
762632e3bdcSTrond Myklebust 	clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
7633b948ae5STrond Myklebust 	clear_bit(XPRT_CLOSING, &xprt->state);
764632e3bdcSTrond Myklebust 	smp_mb__after_clear_bit();
76562da3b24STrond Myklebust 	xprt_disconnect_done(xprt);
766a246b010SChuck Lever }
767a246b010SChuck Lever 
768f75e6745STrond Myklebust static void xs_tcp_close(struct rpc_xprt *xprt)
769f75e6745STrond Myklebust {
770f75e6745STrond Myklebust 	if (test_and_clear_bit(XPRT_CONNECTION_CLOSE, &xprt->state))
771f75e6745STrond Myklebust 		xs_close(xprt);
772f75e6745STrond Myklebust 	else
773f75e6745STrond Myklebust 		xs_tcp_shutdown(xprt);
774f75e6745STrond Myklebust }
775f75e6745STrond Myklebust 
7769903cd1cSChuck Lever /**
7779903cd1cSChuck Lever  * xs_destroy - prepare to shutdown a transport
7789903cd1cSChuck Lever  * @xprt: doomed transport
7799903cd1cSChuck Lever  *
7809903cd1cSChuck Lever  */
7819903cd1cSChuck Lever static void xs_destroy(struct rpc_xprt *xprt)
782a246b010SChuck Lever {
783c8475461SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
784c8475461SChuck Lever 
7859903cd1cSChuck Lever 	dprintk("RPC:       xs_destroy xprt %p\n", xprt);
7869903cd1cSChuck Lever 
787c1384c9cSTrond Myklebust 	cancel_rearming_delayed_work(&transport->connect_worker);
788a246b010SChuck Lever 
7899903cd1cSChuck Lever 	xs_close(xprt);
790edb267a6SChuck Lever 	xs_free_peer_addresses(xprt);
791a246b010SChuck Lever 	kfree(xprt->slot);
792c8541ecdSChuck Lever 	kfree(xprt);
793bc25571eS\"Talpey, Thomas\ 	module_put(THIS_MODULE);
794a246b010SChuck Lever }
795a246b010SChuck Lever 
7969903cd1cSChuck Lever static inline struct rpc_xprt *xprt_from_sock(struct sock *sk)
7979903cd1cSChuck Lever {
7989903cd1cSChuck Lever 	return (struct rpc_xprt *) sk->sk_user_data;
7999903cd1cSChuck Lever }
8009903cd1cSChuck Lever 
8019903cd1cSChuck Lever /**
8029903cd1cSChuck Lever  * xs_udp_data_ready - "data ready" callback for UDP sockets
8039903cd1cSChuck Lever  * @sk: socket with data to read
8049903cd1cSChuck Lever  * @len: how much data to read
8059903cd1cSChuck Lever  *
806a246b010SChuck Lever  */
8079903cd1cSChuck Lever static void xs_udp_data_ready(struct sock *sk, int len)
808a246b010SChuck Lever {
809a246b010SChuck Lever 	struct rpc_task *task;
810a246b010SChuck Lever 	struct rpc_xprt *xprt;
811a246b010SChuck Lever 	struct rpc_rqst *rovr;
812a246b010SChuck Lever 	struct sk_buff *skb;
813a246b010SChuck Lever 	int err, repsize, copied;
814d8ed029dSAlexey Dobriyan 	u32 _xid;
815d8ed029dSAlexey Dobriyan 	__be32 *xp;
816a246b010SChuck Lever 
817a246b010SChuck Lever 	read_lock(&sk->sk_callback_lock);
8189903cd1cSChuck Lever 	dprintk("RPC:       xs_udp_data_ready...\n");
8199903cd1cSChuck Lever 	if (!(xprt = xprt_from_sock(sk)))
820a246b010SChuck Lever 		goto out;
821a246b010SChuck Lever 
822a246b010SChuck Lever 	if ((skb = skb_recv_datagram(sk, 0, 1, &err)) == NULL)
823a246b010SChuck Lever 		goto out;
824a246b010SChuck Lever 
825a246b010SChuck Lever 	if (xprt->shutdown)
826a246b010SChuck Lever 		goto dropit;
827a246b010SChuck Lever 
828a246b010SChuck Lever 	repsize = skb->len - sizeof(struct udphdr);
829a246b010SChuck Lever 	if (repsize < 4) {
8309903cd1cSChuck Lever 		dprintk("RPC:       impossible RPC reply size %d!\n", repsize);
831a246b010SChuck Lever 		goto dropit;
832a246b010SChuck Lever 	}
833a246b010SChuck Lever 
834a246b010SChuck Lever 	/* Copy the XID from the skb... */
835a246b010SChuck Lever 	xp = skb_header_pointer(skb, sizeof(struct udphdr),
836a246b010SChuck Lever 				sizeof(_xid), &_xid);
837a246b010SChuck Lever 	if (xp == NULL)
838a246b010SChuck Lever 		goto dropit;
839a246b010SChuck Lever 
840a246b010SChuck Lever 	/* Look up and lock the request corresponding to the given XID */
8414a0f8c04SChuck Lever 	spin_lock(&xprt->transport_lock);
842a246b010SChuck Lever 	rovr = xprt_lookup_rqst(xprt, *xp);
843a246b010SChuck Lever 	if (!rovr)
844a246b010SChuck Lever 		goto out_unlock;
845a246b010SChuck Lever 	task = rovr->rq_task;
846a246b010SChuck Lever 
847a246b010SChuck Lever 	if ((copied = rovr->rq_private_buf.buflen) > repsize)
848a246b010SChuck Lever 		copied = repsize;
849a246b010SChuck Lever 
850a246b010SChuck Lever 	/* Suck it into the iovec, verify checksum if not done by hw. */
8511781f7f5SHerbert Xu 	if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) {
8521781f7f5SHerbert Xu 		UDPX_INC_STATS_BH(sk, UDP_MIB_INERRORS);
853a246b010SChuck Lever 		goto out_unlock;
8541781f7f5SHerbert Xu 	}
8551781f7f5SHerbert Xu 
8561781f7f5SHerbert Xu 	UDPX_INC_STATS_BH(sk, UDP_MIB_INDATAGRAMS);
857a246b010SChuck Lever 
858a246b010SChuck Lever 	/* Something worked... */
859adf30907SEric Dumazet 	dst_confirm(skb_dst(skb));
860a246b010SChuck Lever 
8611570c1e4SChuck Lever 	xprt_adjust_cwnd(task, copied);
8621570c1e4SChuck Lever 	xprt_update_rtt(task);
8631570c1e4SChuck Lever 	xprt_complete_rqst(task, copied);
864a246b010SChuck Lever 
865a246b010SChuck Lever  out_unlock:
8664a0f8c04SChuck Lever 	spin_unlock(&xprt->transport_lock);
867a246b010SChuck Lever  dropit:
868a246b010SChuck Lever 	skb_free_datagram(sk, skb);
869a246b010SChuck Lever  out:
870a246b010SChuck Lever 	read_unlock(&sk->sk_callback_lock);
871a246b010SChuck Lever }
872a246b010SChuck Lever 
873dd456471SChuck Lever static inline void xs_tcp_read_fraghdr(struct rpc_xprt *xprt, struct xdr_skb_reader *desc)
874a246b010SChuck Lever {
87551971139SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
876a246b010SChuck Lever 	size_t len, used;
877a246b010SChuck Lever 	char *p;
878a246b010SChuck Lever 
87951971139SChuck Lever 	p = ((char *) &transport->tcp_fraghdr) + transport->tcp_offset;
88051971139SChuck Lever 	len = sizeof(transport->tcp_fraghdr) - transport->tcp_offset;
8819d292316SChuck Lever 	used = xdr_skb_read_bits(desc, p, len);
88251971139SChuck Lever 	transport->tcp_offset += used;
883a246b010SChuck Lever 	if (used != len)
884a246b010SChuck Lever 		return;
885808012fbSChuck Lever 
88651971139SChuck Lever 	transport->tcp_reclen = ntohl(transport->tcp_fraghdr);
88751971139SChuck Lever 	if (transport->tcp_reclen & RPC_LAST_STREAM_FRAGMENT)
888e136d092SChuck Lever 		transport->tcp_flags |= TCP_RCV_LAST_FRAG;
889a246b010SChuck Lever 	else
890e136d092SChuck Lever 		transport->tcp_flags &= ~TCP_RCV_LAST_FRAG;
89151971139SChuck Lever 	transport->tcp_reclen &= RPC_FRAGMENT_SIZE_MASK;
892808012fbSChuck Lever 
893e136d092SChuck Lever 	transport->tcp_flags &= ~TCP_RCV_COPY_FRAGHDR;
89451971139SChuck Lever 	transport->tcp_offset = 0;
895808012fbSChuck Lever 
896a246b010SChuck Lever 	/* Sanity check of the record length */
89718dca02aSRicardo Labiaga 	if (unlikely(transport->tcp_reclen < 8)) {
8989903cd1cSChuck Lever 		dprintk("RPC:       invalid TCP record fragment length\n");
8993ebb067dSTrond Myklebust 		xprt_force_disconnect(xprt);
9009903cd1cSChuck Lever 		return;
901a246b010SChuck Lever 	}
902a246b010SChuck Lever 	dprintk("RPC:       reading TCP record fragment of length %d\n",
90351971139SChuck Lever 			transport->tcp_reclen);
904a246b010SChuck Lever }
905a246b010SChuck Lever 
90651971139SChuck Lever static void xs_tcp_check_fraghdr(struct sock_xprt *transport)
907a246b010SChuck Lever {
90851971139SChuck Lever 	if (transport->tcp_offset == transport->tcp_reclen) {
909e136d092SChuck Lever 		transport->tcp_flags |= TCP_RCV_COPY_FRAGHDR;
91051971139SChuck Lever 		transport->tcp_offset = 0;
911e136d092SChuck Lever 		if (transport->tcp_flags & TCP_RCV_LAST_FRAG) {
912e136d092SChuck Lever 			transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
913e136d092SChuck Lever 			transport->tcp_flags |= TCP_RCV_COPY_XID;
91451971139SChuck Lever 			transport->tcp_copied = 0;
915a246b010SChuck Lever 		}
916a246b010SChuck Lever 	}
917a246b010SChuck Lever }
918a246b010SChuck Lever 
919dd456471SChuck Lever static inline void xs_tcp_read_xid(struct sock_xprt *transport, struct xdr_skb_reader *desc)
920a246b010SChuck Lever {
921a246b010SChuck Lever 	size_t len, used;
922a246b010SChuck Lever 	char *p;
923a246b010SChuck Lever 
92451971139SChuck Lever 	len = sizeof(transport->tcp_xid) - transport->tcp_offset;
925a246b010SChuck Lever 	dprintk("RPC:       reading XID (%Zu bytes)\n", len);
92651971139SChuck Lever 	p = ((char *) &transport->tcp_xid) + transport->tcp_offset;
9279d292316SChuck Lever 	used = xdr_skb_read_bits(desc, p, len);
92851971139SChuck Lever 	transport->tcp_offset += used;
929a246b010SChuck Lever 	if (used != len)
930a246b010SChuck Lever 		return;
931e136d092SChuck Lever 	transport->tcp_flags &= ~TCP_RCV_COPY_XID;
932f4a2e418SRicardo Labiaga 	transport->tcp_flags |= TCP_RCV_READ_CALLDIR;
93351971139SChuck Lever 	transport->tcp_copied = 4;
93418dca02aSRicardo Labiaga 	dprintk("RPC:       reading %s XID %08x\n",
93518dca02aSRicardo Labiaga 			(transport->tcp_flags & TCP_RPC_REPLY) ? "reply for"
93618dca02aSRicardo Labiaga 							      : "request with",
93751971139SChuck Lever 			ntohl(transport->tcp_xid));
93851971139SChuck Lever 	xs_tcp_check_fraghdr(transport);
939a246b010SChuck Lever }
940a246b010SChuck Lever 
94118dca02aSRicardo Labiaga static inline void xs_tcp_read_calldir(struct sock_xprt *transport,
94218dca02aSRicardo Labiaga 				       struct xdr_skb_reader *desc)
943a246b010SChuck Lever {
94418dca02aSRicardo Labiaga 	size_t len, used;
94518dca02aSRicardo Labiaga 	u32 offset;
94618dca02aSRicardo Labiaga 	__be32	calldir;
94718dca02aSRicardo Labiaga 
94818dca02aSRicardo Labiaga 	/*
94918dca02aSRicardo Labiaga 	 * We want transport->tcp_offset to be 8 at the end of this routine
95018dca02aSRicardo Labiaga 	 * (4 bytes for the xid and 4 bytes for the call/reply flag).
95118dca02aSRicardo Labiaga 	 * When this function is called for the first time,
95218dca02aSRicardo Labiaga 	 * transport->tcp_offset is 4 (after having already read the xid).
95318dca02aSRicardo Labiaga 	 */
95418dca02aSRicardo Labiaga 	offset = transport->tcp_offset - sizeof(transport->tcp_xid);
95518dca02aSRicardo Labiaga 	len = sizeof(calldir) - offset;
95618dca02aSRicardo Labiaga 	dprintk("RPC:       reading CALL/REPLY flag (%Zu bytes)\n", len);
95718dca02aSRicardo Labiaga 	used = xdr_skb_read_bits(desc, &calldir, len);
95818dca02aSRicardo Labiaga 	transport->tcp_offset += used;
95918dca02aSRicardo Labiaga 	if (used != len)
96018dca02aSRicardo Labiaga 		return;
961f4a2e418SRicardo Labiaga 	transport->tcp_flags &= ~TCP_RCV_READ_CALLDIR;
962f4a2e418SRicardo Labiaga 	transport->tcp_flags |= TCP_RCV_COPY_CALLDIR;
96318dca02aSRicardo Labiaga 	transport->tcp_flags |= TCP_RCV_COPY_DATA;
964f4a2e418SRicardo Labiaga 	/*
965f4a2e418SRicardo Labiaga 	 * We don't yet have the XDR buffer, so we will write the calldir
966f4a2e418SRicardo Labiaga 	 * out after we get the buffer from the 'struct rpc_rqst'
967f4a2e418SRicardo Labiaga 	 */
96818dca02aSRicardo Labiaga 	if (ntohl(calldir) == RPC_REPLY)
96918dca02aSRicardo Labiaga 		transport->tcp_flags |= TCP_RPC_REPLY;
97018dca02aSRicardo Labiaga 	else
97118dca02aSRicardo Labiaga 		transport->tcp_flags &= ~TCP_RPC_REPLY;
97218dca02aSRicardo Labiaga 	dprintk("RPC:       reading %s CALL/REPLY flag %08x\n",
97318dca02aSRicardo Labiaga 			(transport->tcp_flags & TCP_RPC_REPLY) ?
97418dca02aSRicardo Labiaga 				"reply for" : "request with", calldir);
97518dca02aSRicardo Labiaga 	xs_tcp_check_fraghdr(transport);
97618dca02aSRicardo Labiaga }
97718dca02aSRicardo Labiaga 
97844b98efdSRicardo Labiaga static inline void xs_tcp_read_common(struct rpc_xprt *xprt,
97944b98efdSRicardo Labiaga 				     struct xdr_skb_reader *desc,
98044b98efdSRicardo Labiaga 				     struct rpc_rqst *req)
981a246b010SChuck Lever {
98244b98efdSRicardo Labiaga 	struct sock_xprt *transport =
98344b98efdSRicardo Labiaga 				container_of(xprt, struct sock_xprt, xprt);
984a246b010SChuck Lever 	struct xdr_buf *rcvbuf;
985a246b010SChuck Lever 	size_t len;
986a246b010SChuck Lever 	ssize_t r;
987a246b010SChuck Lever 
988a246b010SChuck Lever 	rcvbuf = &req->rq_private_buf;
989f4a2e418SRicardo Labiaga 
990f4a2e418SRicardo Labiaga 	if (transport->tcp_flags & TCP_RCV_COPY_CALLDIR) {
991f4a2e418SRicardo Labiaga 		/*
992f4a2e418SRicardo Labiaga 		 * Save the RPC direction in the XDR buffer
993f4a2e418SRicardo Labiaga 		 */
994f4a2e418SRicardo Labiaga 		__be32	calldir = transport->tcp_flags & TCP_RPC_REPLY ?
995f4a2e418SRicardo Labiaga 					htonl(RPC_REPLY) : 0;
996f4a2e418SRicardo Labiaga 
997f4a2e418SRicardo Labiaga 		memcpy(rcvbuf->head[0].iov_base + transport->tcp_copied,
998f4a2e418SRicardo Labiaga 			&calldir, sizeof(calldir));
999f4a2e418SRicardo Labiaga 		transport->tcp_copied += sizeof(calldir);
1000f4a2e418SRicardo Labiaga 		transport->tcp_flags &= ~TCP_RCV_COPY_CALLDIR;
1001a246b010SChuck Lever 	}
1002a246b010SChuck Lever 
1003a246b010SChuck Lever 	len = desc->count;
100451971139SChuck Lever 	if (len > transport->tcp_reclen - transport->tcp_offset) {
1005dd456471SChuck Lever 		struct xdr_skb_reader my_desc;
1006a246b010SChuck Lever 
100751971139SChuck Lever 		len = transport->tcp_reclen - transport->tcp_offset;
1008a246b010SChuck Lever 		memcpy(&my_desc, desc, sizeof(my_desc));
1009a246b010SChuck Lever 		my_desc.count = len;
101051971139SChuck Lever 		r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
10119d292316SChuck Lever 					  &my_desc, xdr_skb_read_bits);
1012a246b010SChuck Lever 		desc->count -= r;
1013a246b010SChuck Lever 		desc->offset += r;
1014a246b010SChuck Lever 	} else
101551971139SChuck Lever 		r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
10169d292316SChuck Lever 					  desc, xdr_skb_read_bits);
1017a246b010SChuck Lever 
1018a246b010SChuck Lever 	if (r > 0) {
101951971139SChuck Lever 		transport->tcp_copied += r;
102051971139SChuck Lever 		transport->tcp_offset += r;
1021a246b010SChuck Lever 	}
1022a246b010SChuck Lever 	if (r != len) {
1023a246b010SChuck Lever 		/* Error when copying to the receive buffer,
1024a246b010SChuck Lever 		 * usually because we weren't able to allocate
1025a246b010SChuck Lever 		 * additional buffer pages. All we can do now
1026e136d092SChuck Lever 		 * is turn off TCP_RCV_COPY_DATA, so the request
1027a246b010SChuck Lever 		 * will not receive any additional updates,
1028a246b010SChuck Lever 		 * and time out.
1029a246b010SChuck Lever 		 * Any remaining data from this record will
1030a246b010SChuck Lever 		 * be discarded.
1031a246b010SChuck Lever 		 */
1032e136d092SChuck Lever 		transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1033a246b010SChuck Lever 		dprintk("RPC:       XID %08x truncated request\n",
103451971139SChuck Lever 				ntohl(transport->tcp_xid));
103546121cf7SChuck Lever 		dprintk("RPC:       xprt = %p, tcp_copied = %lu, "
103646121cf7SChuck Lever 				"tcp_offset = %u, tcp_reclen = %u\n",
103746121cf7SChuck Lever 				xprt, transport->tcp_copied,
103846121cf7SChuck Lever 				transport->tcp_offset, transport->tcp_reclen);
103944b98efdSRicardo Labiaga 		return;
1040a246b010SChuck Lever 	}
1041a246b010SChuck Lever 
1042a246b010SChuck Lever 	dprintk("RPC:       XID %08x read %Zd bytes\n",
104351971139SChuck Lever 			ntohl(transport->tcp_xid), r);
104446121cf7SChuck Lever 	dprintk("RPC:       xprt = %p, tcp_copied = %lu, tcp_offset = %u, "
104546121cf7SChuck Lever 			"tcp_reclen = %u\n", xprt, transport->tcp_copied,
104646121cf7SChuck Lever 			transport->tcp_offset, transport->tcp_reclen);
1047a246b010SChuck Lever 
104851971139SChuck Lever 	if (transport->tcp_copied == req->rq_private_buf.buflen)
1049e136d092SChuck Lever 		transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
105051971139SChuck Lever 	else if (transport->tcp_offset == transport->tcp_reclen) {
1051e136d092SChuck Lever 		if (transport->tcp_flags & TCP_RCV_LAST_FRAG)
1052e136d092SChuck Lever 			transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1053a246b010SChuck Lever 	}
1054a246b010SChuck Lever 
105544b98efdSRicardo Labiaga 	return;
105644b98efdSRicardo Labiaga }
105744b98efdSRicardo Labiaga 
105844b98efdSRicardo Labiaga /*
105944b98efdSRicardo Labiaga  * Finds the request corresponding to the RPC xid and invokes the common
106044b98efdSRicardo Labiaga  * tcp read code to read the data.
106144b98efdSRicardo Labiaga  */
106244b98efdSRicardo Labiaga static inline int xs_tcp_read_reply(struct rpc_xprt *xprt,
106344b98efdSRicardo Labiaga 				    struct xdr_skb_reader *desc)
106444b98efdSRicardo Labiaga {
106544b98efdSRicardo Labiaga 	struct sock_xprt *transport =
106644b98efdSRicardo Labiaga 				container_of(xprt, struct sock_xprt, xprt);
106744b98efdSRicardo Labiaga 	struct rpc_rqst *req;
106844b98efdSRicardo Labiaga 
106944b98efdSRicardo Labiaga 	dprintk("RPC:       read reply XID %08x\n", ntohl(transport->tcp_xid));
107044b98efdSRicardo Labiaga 
107144b98efdSRicardo Labiaga 	/* Find and lock the request corresponding to this xid */
107244b98efdSRicardo Labiaga 	spin_lock(&xprt->transport_lock);
107344b98efdSRicardo Labiaga 	req = xprt_lookup_rqst(xprt, transport->tcp_xid);
107444b98efdSRicardo Labiaga 	if (!req) {
107544b98efdSRicardo Labiaga 		dprintk("RPC:       XID %08x request not found!\n",
107644b98efdSRicardo Labiaga 				ntohl(transport->tcp_xid));
107744b98efdSRicardo Labiaga 		spin_unlock(&xprt->transport_lock);
107844b98efdSRicardo Labiaga 		return -1;
107944b98efdSRicardo Labiaga 	}
108044b98efdSRicardo Labiaga 
108144b98efdSRicardo Labiaga 	xs_tcp_read_common(xprt, desc, req);
108244b98efdSRicardo Labiaga 
1083e136d092SChuck Lever 	if (!(transport->tcp_flags & TCP_RCV_COPY_DATA))
108451971139SChuck Lever 		xprt_complete_rqst(req->rq_task, transport->tcp_copied);
108544b98efdSRicardo Labiaga 
10864a0f8c04SChuck Lever 	spin_unlock(&xprt->transport_lock);
108744b98efdSRicardo Labiaga 	return 0;
108844b98efdSRicardo Labiaga }
108944b98efdSRicardo Labiaga 
109044b98efdSRicardo Labiaga #if defined(CONFIG_NFS_V4_1)
109144b98efdSRicardo Labiaga /*
109244b98efdSRicardo Labiaga  * Obtains an rpc_rqst previously allocated and invokes the common
109344b98efdSRicardo Labiaga  * tcp read code to read the data.  The result is placed in the callback
109444b98efdSRicardo Labiaga  * queue.
109544b98efdSRicardo Labiaga  * If we're unable to obtain the rpc_rqst we schedule the closing of the
109644b98efdSRicardo Labiaga  * connection and return -1.
109744b98efdSRicardo Labiaga  */
109844b98efdSRicardo Labiaga static inline int xs_tcp_read_callback(struct rpc_xprt *xprt,
109944b98efdSRicardo Labiaga 				       struct xdr_skb_reader *desc)
110044b98efdSRicardo Labiaga {
110144b98efdSRicardo Labiaga 	struct sock_xprt *transport =
110244b98efdSRicardo Labiaga 				container_of(xprt, struct sock_xprt, xprt);
110344b98efdSRicardo Labiaga 	struct rpc_rqst *req;
110444b98efdSRicardo Labiaga 
110544b98efdSRicardo Labiaga 	req = xprt_alloc_bc_request(xprt);
110644b98efdSRicardo Labiaga 	if (req == NULL) {
110744b98efdSRicardo Labiaga 		printk(KERN_WARNING "Callback slot table overflowed\n");
110844b98efdSRicardo Labiaga 		xprt_force_disconnect(xprt);
110944b98efdSRicardo Labiaga 		return -1;
111044b98efdSRicardo Labiaga 	}
111144b98efdSRicardo Labiaga 
111244b98efdSRicardo Labiaga 	req->rq_xid = transport->tcp_xid;
111344b98efdSRicardo Labiaga 	dprintk("RPC:       read callback  XID %08x\n", ntohl(req->rq_xid));
111444b98efdSRicardo Labiaga 	xs_tcp_read_common(xprt, desc, req);
111544b98efdSRicardo Labiaga 
111644b98efdSRicardo Labiaga 	if (!(transport->tcp_flags & TCP_RCV_COPY_DATA)) {
111744b98efdSRicardo Labiaga 		struct svc_serv *bc_serv = xprt->bc_serv;
111844b98efdSRicardo Labiaga 
111944b98efdSRicardo Labiaga 		/*
112044b98efdSRicardo Labiaga 		 * Add callback request to callback list.  The callback
112144b98efdSRicardo Labiaga 		 * service sleeps on the sv_cb_waitq waiting for new
112244b98efdSRicardo Labiaga 		 * requests.  Wake it up after adding enqueing the
112344b98efdSRicardo Labiaga 		 * request.
112444b98efdSRicardo Labiaga 		 */
112544b98efdSRicardo Labiaga 		dprintk("RPC:       add callback request to list\n");
112644b98efdSRicardo Labiaga 		spin_lock(&bc_serv->sv_cb_lock);
112744b98efdSRicardo Labiaga 		list_add(&req->rq_bc_list, &bc_serv->sv_cb_list);
112844b98efdSRicardo Labiaga 		spin_unlock(&bc_serv->sv_cb_lock);
112944b98efdSRicardo Labiaga 		wake_up(&bc_serv->sv_cb_waitq);
113044b98efdSRicardo Labiaga 	}
113144b98efdSRicardo Labiaga 
113244b98efdSRicardo Labiaga 	req->rq_private_buf.len = transport->tcp_copied;
113344b98efdSRicardo Labiaga 
113444b98efdSRicardo Labiaga 	return 0;
113544b98efdSRicardo Labiaga }
113644b98efdSRicardo Labiaga 
113744b98efdSRicardo Labiaga static inline int _xs_tcp_read_data(struct rpc_xprt *xprt,
113844b98efdSRicardo Labiaga 					struct xdr_skb_reader *desc)
113944b98efdSRicardo Labiaga {
114044b98efdSRicardo Labiaga 	struct sock_xprt *transport =
114144b98efdSRicardo Labiaga 				container_of(xprt, struct sock_xprt, xprt);
114244b98efdSRicardo Labiaga 
114344b98efdSRicardo Labiaga 	return (transport->tcp_flags & TCP_RPC_REPLY) ?
114444b98efdSRicardo Labiaga 		xs_tcp_read_reply(xprt, desc) :
114544b98efdSRicardo Labiaga 		xs_tcp_read_callback(xprt, desc);
114644b98efdSRicardo Labiaga }
114744b98efdSRicardo Labiaga #else
114844b98efdSRicardo Labiaga static inline int _xs_tcp_read_data(struct rpc_xprt *xprt,
114944b98efdSRicardo Labiaga 					struct xdr_skb_reader *desc)
115044b98efdSRicardo Labiaga {
115144b98efdSRicardo Labiaga 	return xs_tcp_read_reply(xprt, desc);
115244b98efdSRicardo Labiaga }
115344b98efdSRicardo Labiaga #endif /* CONFIG_NFS_V4_1 */
115444b98efdSRicardo Labiaga 
115544b98efdSRicardo Labiaga /*
115644b98efdSRicardo Labiaga  * Read data off the transport.  This can be either an RPC_CALL or an
115744b98efdSRicardo Labiaga  * RPC_REPLY.  Relay the processing to helper functions.
115844b98efdSRicardo Labiaga  */
115944b98efdSRicardo Labiaga static void xs_tcp_read_data(struct rpc_xprt *xprt,
116044b98efdSRicardo Labiaga 				    struct xdr_skb_reader *desc)
116144b98efdSRicardo Labiaga {
116244b98efdSRicardo Labiaga 	struct sock_xprt *transport =
116344b98efdSRicardo Labiaga 				container_of(xprt, struct sock_xprt, xprt);
116444b98efdSRicardo Labiaga 
116544b98efdSRicardo Labiaga 	if (_xs_tcp_read_data(xprt, desc) == 0)
116651971139SChuck Lever 		xs_tcp_check_fraghdr(transport);
116744b98efdSRicardo Labiaga 	else {
116844b98efdSRicardo Labiaga 		/*
116944b98efdSRicardo Labiaga 		 * The transport_lock protects the request handling.
117044b98efdSRicardo Labiaga 		 * There's no need to hold it to update the tcp_flags.
117144b98efdSRicardo Labiaga 		 */
117244b98efdSRicardo Labiaga 		transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
117344b98efdSRicardo Labiaga 	}
1174a246b010SChuck Lever }
1175a246b010SChuck Lever 
1176dd456471SChuck Lever static inline void xs_tcp_read_discard(struct sock_xprt *transport, struct xdr_skb_reader *desc)
1177a246b010SChuck Lever {
1178a246b010SChuck Lever 	size_t len;
1179a246b010SChuck Lever 
118051971139SChuck Lever 	len = transport->tcp_reclen - transport->tcp_offset;
1181a246b010SChuck Lever 	if (len > desc->count)
1182a246b010SChuck Lever 		len = desc->count;
1183a246b010SChuck Lever 	desc->count -= len;
1184a246b010SChuck Lever 	desc->offset += len;
118551971139SChuck Lever 	transport->tcp_offset += len;
1186a246b010SChuck Lever 	dprintk("RPC:       discarded %Zu bytes\n", len);
118751971139SChuck Lever 	xs_tcp_check_fraghdr(transport);
1188a246b010SChuck Lever }
1189a246b010SChuck Lever 
11909903cd1cSChuck Lever static int xs_tcp_data_recv(read_descriptor_t *rd_desc, struct sk_buff *skb, unsigned int offset, size_t len)
1191a246b010SChuck Lever {
1192a246b010SChuck Lever 	struct rpc_xprt *xprt = rd_desc->arg.data;
119351971139SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1194dd456471SChuck Lever 	struct xdr_skb_reader desc = {
1195a246b010SChuck Lever 		.skb	= skb,
1196a246b010SChuck Lever 		.offset	= offset,
1197a246b010SChuck Lever 		.count	= len,
1198a246b010SChuck Lever 	};
1199a246b010SChuck Lever 
12009903cd1cSChuck Lever 	dprintk("RPC:       xs_tcp_data_recv started\n");
1201a246b010SChuck Lever 	do {
1202a246b010SChuck Lever 		/* Read in a new fragment marker if necessary */
1203a246b010SChuck Lever 		/* Can we ever really expect to get completely empty fragments? */
1204e136d092SChuck Lever 		if (transport->tcp_flags & TCP_RCV_COPY_FRAGHDR) {
12059903cd1cSChuck Lever 			xs_tcp_read_fraghdr(xprt, &desc);
1206a246b010SChuck Lever 			continue;
1207a246b010SChuck Lever 		}
1208a246b010SChuck Lever 		/* Read in the xid if necessary */
1209e136d092SChuck Lever 		if (transport->tcp_flags & TCP_RCV_COPY_XID) {
121051971139SChuck Lever 			xs_tcp_read_xid(transport, &desc);
1211a246b010SChuck Lever 			continue;
1212a246b010SChuck Lever 		}
121318dca02aSRicardo Labiaga 		/* Read in the call/reply flag */
1214f4a2e418SRicardo Labiaga 		if (transport->tcp_flags & TCP_RCV_READ_CALLDIR) {
121518dca02aSRicardo Labiaga 			xs_tcp_read_calldir(transport, &desc);
121618dca02aSRicardo Labiaga 			continue;
121718dca02aSRicardo Labiaga 		}
1218a246b010SChuck Lever 		/* Read in the request data */
1219e136d092SChuck Lever 		if (transport->tcp_flags & TCP_RCV_COPY_DATA) {
122044b98efdSRicardo Labiaga 			xs_tcp_read_data(xprt, &desc);
1221a246b010SChuck Lever 			continue;
1222a246b010SChuck Lever 		}
1223a246b010SChuck Lever 		/* Skip over any trailing bytes on short reads */
122451971139SChuck Lever 		xs_tcp_read_discard(transport, &desc);
1225a246b010SChuck Lever 	} while (desc.count);
12269903cd1cSChuck Lever 	dprintk("RPC:       xs_tcp_data_recv done\n");
1227a246b010SChuck Lever 	return len - desc.count;
1228a246b010SChuck Lever }
1229a246b010SChuck Lever 
12309903cd1cSChuck Lever /**
12319903cd1cSChuck Lever  * xs_tcp_data_ready - "data ready" callback for TCP sockets
12329903cd1cSChuck Lever  * @sk: socket with data to read
12339903cd1cSChuck Lever  * @bytes: how much data to read
12349903cd1cSChuck Lever  *
12359903cd1cSChuck Lever  */
12369903cd1cSChuck Lever static void xs_tcp_data_ready(struct sock *sk, int bytes)
1237a246b010SChuck Lever {
1238a246b010SChuck Lever 	struct rpc_xprt *xprt;
1239a246b010SChuck Lever 	read_descriptor_t rd_desc;
1240ff2d7db8STrond Myklebust 	int read;
1241a246b010SChuck Lever 
12429903cd1cSChuck Lever 	dprintk("RPC:       xs_tcp_data_ready...\n");
124346121cf7SChuck Lever 
124446121cf7SChuck Lever 	read_lock(&sk->sk_callback_lock);
12459903cd1cSChuck Lever 	if (!(xprt = xprt_from_sock(sk)))
1246a246b010SChuck Lever 		goto out;
1247a246b010SChuck Lever 	if (xprt->shutdown)
1248a246b010SChuck Lever 		goto out;
1249a246b010SChuck Lever 
125061d0a8e6SNeil Brown 	/* Any data means we had a useful conversation, so
125161d0a8e6SNeil Brown 	 * the we don't need to delay the next reconnect
125261d0a8e6SNeil Brown 	 */
125361d0a8e6SNeil Brown 	if (xprt->reestablish_timeout)
125461d0a8e6SNeil Brown 		xprt->reestablish_timeout = 0;
125561d0a8e6SNeil Brown 
12569903cd1cSChuck Lever 	/* We use rd_desc to pass struct xprt to xs_tcp_data_recv */
1257a246b010SChuck Lever 	rd_desc.arg.data = xprt;
1258ff2d7db8STrond Myklebust 	do {
1259a246b010SChuck Lever 		rd_desc.count = 65536;
1260ff2d7db8STrond Myklebust 		read = tcp_read_sock(sk, &rd_desc, xs_tcp_data_recv);
1261ff2d7db8STrond Myklebust 	} while (read > 0);
1262a246b010SChuck Lever out:
1263a246b010SChuck Lever 	read_unlock(&sk->sk_callback_lock);
1264a246b010SChuck Lever }
1265a246b010SChuck Lever 
12667d1e8255STrond Myklebust /*
12677d1e8255STrond Myklebust  * Do the equivalent of linger/linger2 handling for dealing with
12687d1e8255STrond Myklebust  * broken servers that don't close the socket in a timely
12697d1e8255STrond Myklebust  * fashion
12707d1e8255STrond Myklebust  */
12717d1e8255STrond Myklebust static void xs_tcp_schedule_linger_timeout(struct rpc_xprt *xprt,
12727d1e8255STrond Myklebust 		unsigned long timeout)
12737d1e8255STrond Myklebust {
12747d1e8255STrond Myklebust 	struct sock_xprt *transport;
12757d1e8255STrond Myklebust 
12767d1e8255STrond Myklebust 	if (xprt_test_and_set_connecting(xprt))
12777d1e8255STrond Myklebust 		return;
12787d1e8255STrond Myklebust 	set_bit(XPRT_CONNECTION_ABORT, &xprt->state);
12797d1e8255STrond Myklebust 	transport = container_of(xprt, struct sock_xprt, xprt);
12807d1e8255STrond Myklebust 	queue_delayed_work(rpciod_workqueue, &transport->connect_worker,
12817d1e8255STrond Myklebust 			   timeout);
12827d1e8255STrond Myklebust }
12837d1e8255STrond Myklebust 
12847d1e8255STrond Myklebust static void xs_tcp_cancel_linger_timeout(struct rpc_xprt *xprt)
12857d1e8255STrond Myklebust {
12867d1e8255STrond Myklebust 	struct sock_xprt *transport;
12877d1e8255STrond Myklebust 
12887d1e8255STrond Myklebust 	transport = container_of(xprt, struct sock_xprt, xprt);
12897d1e8255STrond Myklebust 
12907d1e8255STrond Myklebust 	if (!test_bit(XPRT_CONNECTION_ABORT, &xprt->state) ||
12917d1e8255STrond Myklebust 	    !cancel_delayed_work(&transport->connect_worker))
12927d1e8255STrond Myklebust 		return;
12937d1e8255STrond Myklebust 	clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
12947d1e8255STrond Myklebust 	xprt_clear_connecting(xprt);
12957d1e8255STrond Myklebust }
12967d1e8255STrond Myklebust 
12977d1e8255STrond Myklebust static void xs_sock_mark_closed(struct rpc_xprt *xprt)
12987d1e8255STrond Myklebust {
12997d1e8255STrond Myklebust 	smp_mb__before_clear_bit();
13007d1e8255STrond Myklebust 	clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
13017d1e8255STrond Myklebust 	clear_bit(XPRT_CLOSING, &xprt->state);
13027d1e8255STrond Myklebust 	smp_mb__after_clear_bit();
13037d1e8255STrond Myklebust 	/* Mark transport as closed and wake up all pending tasks */
13047d1e8255STrond Myklebust 	xprt_disconnect_done(xprt);
13057d1e8255STrond Myklebust }
13067d1e8255STrond Myklebust 
13079903cd1cSChuck Lever /**
13089903cd1cSChuck Lever  * xs_tcp_state_change - callback to handle TCP socket state changes
13099903cd1cSChuck Lever  * @sk: socket whose state has changed
13109903cd1cSChuck Lever  *
13119903cd1cSChuck Lever  */
13129903cd1cSChuck Lever static void xs_tcp_state_change(struct sock *sk)
1313a246b010SChuck Lever {
1314a246b010SChuck Lever 	struct rpc_xprt *xprt;
1315a246b010SChuck Lever 
1316a246b010SChuck Lever 	read_lock(&sk->sk_callback_lock);
1317a246b010SChuck Lever 	if (!(xprt = xprt_from_sock(sk)))
1318a246b010SChuck Lever 		goto out;
13199903cd1cSChuck Lever 	dprintk("RPC:       xs_tcp_state_change client %p...\n", xprt);
1320a246b010SChuck Lever 	dprintk("RPC:       state %x conn %d dead %d zapped %d\n",
1321a246b010SChuck Lever 			sk->sk_state, xprt_connected(xprt),
1322a246b010SChuck Lever 			sock_flag(sk, SOCK_DEAD),
1323a246b010SChuck Lever 			sock_flag(sk, SOCK_ZAPPED));
1324a246b010SChuck Lever 
1325a246b010SChuck Lever 	switch (sk->sk_state) {
1326a246b010SChuck Lever 	case TCP_ESTABLISHED:
13274a0f8c04SChuck Lever 		spin_lock_bh(&xprt->transport_lock);
1328a246b010SChuck Lever 		if (!xprt_test_and_set_connected(xprt)) {
132951971139SChuck Lever 			struct sock_xprt *transport = container_of(xprt,
133051971139SChuck Lever 					struct sock_xprt, xprt);
133151971139SChuck Lever 
1332a246b010SChuck Lever 			/* Reset TCP record info */
133351971139SChuck Lever 			transport->tcp_offset = 0;
133451971139SChuck Lever 			transport->tcp_reclen = 0;
133551971139SChuck Lever 			transport->tcp_copied = 0;
1336e136d092SChuck Lever 			transport->tcp_flags =
1337e136d092SChuck Lever 				TCP_RCV_COPY_FRAGHDR | TCP_RCV_COPY_XID;
133851971139SChuck Lever 
13392a491991STrond Myklebust 			xprt_wake_pending_tasks(xprt, -EAGAIN);
1340a246b010SChuck Lever 		}
13414a0f8c04SChuck Lever 		spin_unlock_bh(&xprt->transport_lock);
1342a246b010SChuck Lever 		break;
13433b948ae5STrond Myklebust 	case TCP_FIN_WAIT1:
13443b948ae5STrond Myklebust 		/* The client initiated a shutdown of the socket */
13457c1d71cfSTrond Myklebust 		xprt->connect_cookie++;
1346663b8858STrond Myklebust 		xprt->reestablish_timeout = 0;
13473b948ae5STrond Myklebust 		set_bit(XPRT_CLOSING, &xprt->state);
13483b948ae5STrond Myklebust 		smp_mb__before_clear_bit();
13493b948ae5STrond Myklebust 		clear_bit(XPRT_CONNECTED, &xprt->state);
1350ef803670STrond Myklebust 		clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
13513b948ae5STrond Myklebust 		smp_mb__after_clear_bit();
135225fe6142STrond Myklebust 		xs_tcp_schedule_linger_timeout(xprt, xs_tcp_fin_timeout);
1353a246b010SChuck Lever 		break;
1354632e3bdcSTrond Myklebust 	case TCP_CLOSE_WAIT:
13553b948ae5STrond Myklebust 		/* The server initiated a shutdown of the socket */
135666af1e55STrond Myklebust 		xprt_force_disconnect(xprt);
1357663b8858STrond Myklebust 	case TCP_SYN_SENT:
13587c1d71cfSTrond Myklebust 		xprt->connect_cookie++;
1359663b8858STrond Myklebust 	case TCP_CLOSING:
1360663b8858STrond Myklebust 		/*
1361663b8858STrond Myklebust 		 * If the server closed down the connection, make sure that
1362663b8858STrond Myklebust 		 * we back off before reconnecting
1363663b8858STrond Myklebust 		 */
1364663b8858STrond Myklebust 		if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
1365663b8858STrond Myklebust 			xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
13663b948ae5STrond Myklebust 		break;
13673b948ae5STrond Myklebust 	case TCP_LAST_ACK:
1368670f9457STrond Myklebust 		set_bit(XPRT_CLOSING, &xprt->state);
136925fe6142STrond Myklebust 		xs_tcp_schedule_linger_timeout(xprt, xs_tcp_fin_timeout);
13703b948ae5STrond Myklebust 		smp_mb__before_clear_bit();
13713b948ae5STrond Myklebust 		clear_bit(XPRT_CONNECTED, &xprt->state);
13723b948ae5STrond Myklebust 		smp_mb__after_clear_bit();
13733b948ae5STrond Myklebust 		break;
13743b948ae5STrond Myklebust 	case TCP_CLOSE:
13757d1e8255STrond Myklebust 		xs_tcp_cancel_linger_timeout(xprt);
13767d1e8255STrond Myklebust 		xs_sock_mark_closed(xprt);
1377a246b010SChuck Lever 	}
1378a246b010SChuck Lever  out:
1379a246b010SChuck Lever 	read_unlock(&sk->sk_callback_lock);
1380a246b010SChuck Lever }
1381a246b010SChuck Lever 
13829903cd1cSChuck Lever /**
1383482f32e6STrond Myklebust  * xs_error_report - callback mainly for catching socket errors
13842a9e1cfaSTrond Myklebust  * @sk: socket
13852a9e1cfaSTrond Myklebust  */
1386482f32e6STrond Myklebust static void xs_error_report(struct sock *sk)
13872a9e1cfaSTrond Myklebust {
13882a9e1cfaSTrond Myklebust 	struct rpc_xprt *xprt;
13892a9e1cfaSTrond Myklebust 
13902a9e1cfaSTrond Myklebust 	read_lock(&sk->sk_callback_lock);
13912a9e1cfaSTrond Myklebust 	if (!(xprt = xprt_from_sock(sk)))
13922a9e1cfaSTrond Myklebust 		goto out;
13932a9e1cfaSTrond Myklebust 	dprintk("RPC:       %s client %p...\n"
13942a9e1cfaSTrond Myklebust 			"RPC:       error %d\n",
13952a9e1cfaSTrond Myklebust 			__func__, xprt, sk->sk_err);
1396482f32e6STrond Myklebust 	xprt_wake_pending_tasks(xprt, -EAGAIN);
13972a9e1cfaSTrond Myklebust out:
13982a9e1cfaSTrond Myklebust 	read_unlock(&sk->sk_callback_lock);
13992a9e1cfaSTrond Myklebust }
14002a9e1cfaSTrond Myklebust 
14011f0fa154SIlpo Järvinen static void xs_write_space(struct sock *sk)
14021f0fa154SIlpo Järvinen {
14031f0fa154SIlpo Järvinen 	struct socket *sock;
14041f0fa154SIlpo Järvinen 	struct rpc_xprt *xprt;
14051f0fa154SIlpo Järvinen 
14061f0fa154SIlpo Järvinen 	if (unlikely(!(sock = sk->sk_socket)))
14071f0fa154SIlpo Järvinen 		return;
14081f0fa154SIlpo Järvinen 	clear_bit(SOCK_NOSPACE, &sock->flags);
14091f0fa154SIlpo Järvinen 
14101f0fa154SIlpo Järvinen 	if (unlikely(!(xprt = xprt_from_sock(sk))))
14111f0fa154SIlpo Järvinen 		return;
14121f0fa154SIlpo Järvinen 	if (test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags) == 0)
14131f0fa154SIlpo Järvinen 		return;
14141f0fa154SIlpo Järvinen 
14151f0fa154SIlpo Järvinen 	xprt_write_space(xprt);
14161f0fa154SIlpo Järvinen }
14171f0fa154SIlpo Järvinen 
14182a9e1cfaSTrond Myklebust /**
1419c7b2cae8SChuck Lever  * xs_udp_write_space - callback invoked when socket buffer space
1420c7b2cae8SChuck Lever  *                             becomes available
14219903cd1cSChuck Lever  * @sk: socket whose state has changed
14229903cd1cSChuck Lever  *
1423a246b010SChuck Lever  * Called when more output buffer space is available for this socket.
1424a246b010SChuck Lever  * We try not to wake our writers until they can make "significant"
1425c7b2cae8SChuck Lever  * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1426a246b010SChuck Lever  * with a bunch of small requests.
1427a246b010SChuck Lever  */
1428c7b2cae8SChuck Lever static void xs_udp_write_space(struct sock *sk)
1429a246b010SChuck Lever {
1430a246b010SChuck Lever 	read_lock(&sk->sk_callback_lock);
1431c7b2cae8SChuck Lever 
1432c7b2cae8SChuck Lever 	/* from net/core/sock.c:sock_def_write_space */
14331f0fa154SIlpo Järvinen 	if (sock_writeable(sk))
14341f0fa154SIlpo Järvinen 		xs_write_space(sk);
1435c7b2cae8SChuck Lever 
1436c7b2cae8SChuck Lever 	read_unlock(&sk->sk_callback_lock);
1437c7b2cae8SChuck Lever }
1438c7b2cae8SChuck Lever 
1439c7b2cae8SChuck Lever /**
1440c7b2cae8SChuck Lever  * xs_tcp_write_space - callback invoked when socket buffer space
1441c7b2cae8SChuck Lever  *                             becomes available
1442c7b2cae8SChuck Lever  * @sk: socket whose state has changed
1443c7b2cae8SChuck Lever  *
1444c7b2cae8SChuck Lever  * Called when more output buffer space is available for this socket.
1445c7b2cae8SChuck Lever  * We try not to wake our writers until they can make "significant"
1446c7b2cae8SChuck Lever  * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1447c7b2cae8SChuck Lever  * with a bunch of small requests.
1448c7b2cae8SChuck Lever  */
1449c7b2cae8SChuck Lever static void xs_tcp_write_space(struct sock *sk)
1450c7b2cae8SChuck Lever {
1451c7b2cae8SChuck Lever 	read_lock(&sk->sk_callback_lock);
1452c7b2cae8SChuck Lever 
1453c7b2cae8SChuck Lever 	/* from net/core/stream.c:sk_stream_write_space */
14541f0fa154SIlpo Järvinen 	if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk))
14551f0fa154SIlpo Järvinen 		xs_write_space(sk);
1456c7b2cae8SChuck Lever 
1457a246b010SChuck Lever 	read_unlock(&sk->sk_callback_lock);
1458a246b010SChuck Lever }
1459a246b010SChuck Lever 
1460470056c2SChuck Lever static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt)
1461a246b010SChuck Lever {
1462ee0ac0c2SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1463ee0ac0c2SChuck Lever 	struct sock *sk = transport->inet;
1464a246b010SChuck Lever 
14657c6e066eSChuck Lever 	if (transport->rcvsize) {
1466a246b010SChuck Lever 		sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
14677c6e066eSChuck Lever 		sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2;
1468a246b010SChuck Lever 	}
14697c6e066eSChuck Lever 	if (transport->sndsize) {
1470a246b010SChuck Lever 		sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
14717c6e066eSChuck Lever 		sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2;
1472a246b010SChuck Lever 		sk->sk_write_space(sk);
1473a246b010SChuck Lever 	}
1474a246b010SChuck Lever }
1475a246b010SChuck Lever 
147643118c29SChuck Lever /**
1477470056c2SChuck Lever  * xs_udp_set_buffer_size - set send and receive limits
147843118c29SChuck Lever  * @xprt: generic transport
1479470056c2SChuck Lever  * @sndsize: requested size of send buffer, in bytes
1480470056c2SChuck Lever  * @rcvsize: requested size of receive buffer, in bytes
148143118c29SChuck Lever  *
1482470056c2SChuck Lever  * Set socket send and receive buffer size limits.
148343118c29SChuck Lever  */
1484470056c2SChuck Lever static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize)
148543118c29SChuck Lever {
14867c6e066eSChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
14877c6e066eSChuck Lever 
14887c6e066eSChuck Lever 	transport->sndsize = 0;
1489470056c2SChuck Lever 	if (sndsize)
14907c6e066eSChuck Lever 		transport->sndsize = sndsize + 1024;
14917c6e066eSChuck Lever 	transport->rcvsize = 0;
1492470056c2SChuck Lever 	if (rcvsize)
14937c6e066eSChuck Lever 		transport->rcvsize = rcvsize + 1024;
1494470056c2SChuck Lever 
1495470056c2SChuck Lever 	xs_udp_do_set_buffer_size(xprt);
149643118c29SChuck Lever }
149743118c29SChuck Lever 
149846c0ee8bSChuck Lever /**
149946c0ee8bSChuck Lever  * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
150046c0ee8bSChuck Lever  * @task: task that timed out
150146c0ee8bSChuck Lever  *
150246c0ee8bSChuck Lever  * Adjust the congestion window after a retransmit timeout has occurred.
150346c0ee8bSChuck Lever  */
150446c0ee8bSChuck Lever static void xs_udp_timer(struct rpc_task *task)
150546c0ee8bSChuck Lever {
150646c0ee8bSChuck Lever 	xprt_adjust_cwnd(task, -ETIMEDOUT);
150746c0ee8bSChuck Lever }
150846c0ee8bSChuck Lever 
1509b85d8806SChuck Lever static unsigned short xs_get_random_port(void)
1510b85d8806SChuck Lever {
1511b85d8806SChuck Lever 	unsigned short range = xprt_max_resvport - xprt_min_resvport;
1512b85d8806SChuck Lever 	unsigned short rand = (unsigned short) net_random() % range;
1513b85d8806SChuck Lever 	return rand + xprt_min_resvport;
1514b85d8806SChuck Lever }
1515b85d8806SChuck Lever 
151692200412SChuck Lever /**
151792200412SChuck Lever  * xs_set_port - reset the port number in the remote endpoint address
151892200412SChuck Lever  * @xprt: generic transport
151992200412SChuck Lever  * @port: new port number
152092200412SChuck Lever  *
152192200412SChuck Lever  */
152292200412SChuck Lever static void xs_set_port(struct rpc_xprt *xprt, unsigned short port)
152392200412SChuck Lever {
152492200412SChuck Lever 	dprintk("RPC:       setting port for xprt %p to %u\n", xprt, port);
1525c4efcb1dSChuck Lever 
15269dc3b095SChuck Lever 	rpc_set_port(xs_addr(xprt), port);
15279dc3b095SChuck Lever 	xs_update_peer_port(xprt);
152892200412SChuck Lever }
152992200412SChuck Lever 
153067a391d7STrond Myklebust static unsigned short xs_get_srcport(struct sock_xprt *transport, struct socket *sock)
153167a391d7STrond Myklebust {
1532fbfffbd5SChuck Lever 	unsigned short port = transport->srcport;
153367a391d7STrond Myklebust 
153467a391d7STrond Myklebust 	if (port == 0 && transport->xprt.resvport)
153567a391d7STrond Myklebust 		port = xs_get_random_port();
153667a391d7STrond Myklebust 	return port;
153767a391d7STrond Myklebust }
153867a391d7STrond Myklebust 
153967a391d7STrond Myklebust static unsigned short xs_next_srcport(struct sock_xprt *transport, struct socket *sock, unsigned short port)
154067a391d7STrond Myklebust {
1541fbfffbd5SChuck Lever 	if (transport->srcport != 0)
1542fbfffbd5SChuck Lever 		transport->srcport = 0;
154367a391d7STrond Myklebust 	if (!transport->xprt.resvport)
154467a391d7STrond Myklebust 		return 0;
154567a391d7STrond Myklebust 	if (port <= xprt_min_resvport || port > xprt_max_resvport)
154667a391d7STrond Myklebust 		return xprt_max_resvport;
154767a391d7STrond Myklebust 	return --port;
154867a391d7STrond Myklebust }
154967a391d7STrond Myklebust 
15507dc753f0SChuck Lever static int xs_bind4(struct sock_xprt *transport, struct socket *sock)
1551a246b010SChuck Lever {
1552a246b010SChuck Lever 	struct sockaddr_in myaddr = {
1553a246b010SChuck Lever 		.sin_family = AF_INET,
1554a246b010SChuck Lever 	};
1555d3bc9a1dSFrank van Maarseveen 	struct sockaddr_in *sa;
155667a391d7STrond Myklebust 	int err, nloop = 0;
155767a391d7STrond Myklebust 	unsigned short port = xs_get_srcport(transport, sock);
155867a391d7STrond Myklebust 	unsigned short last;
1559a246b010SChuck Lever 
1560fbfffbd5SChuck Lever 	sa = (struct sockaddr_in *)&transport->srcaddr;
1561d3bc9a1dSFrank van Maarseveen 	myaddr.sin_addr = sa->sin_addr;
1562a246b010SChuck Lever 	do {
1563a246b010SChuck Lever 		myaddr.sin_port = htons(port);
1564e6242e92SSridhar Samudrala 		err = kernel_bind(sock, (struct sockaddr *) &myaddr,
1565a246b010SChuck Lever 						sizeof(myaddr));
156667a391d7STrond Myklebust 		if (port == 0)
1567d3bc9a1dSFrank van Maarseveen 			break;
1568a246b010SChuck Lever 		if (err == 0) {
1569fbfffbd5SChuck Lever 			transport->srcport = port;
1570d3bc9a1dSFrank van Maarseveen 			break;
1571a246b010SChuck Lever 		}
157267a391d7STrond Myklebust 		last = port;
157367a391d7STrond Myklebust 		port = xs_next_srcport(transport, sock, port);
157467a391d7STrond Myklebust 		if (port > last)
157567a391d7STrond Myklebust 			nloop++;
157667a391d7STrond Myklebust 	} while (err == -EADDRINUSE && nloop != 2);
157721454aaaSHarvey Harrison 	dprintk("RPC:       %s %pI4:%u: %s (%d)\n",
157821454aaaSHarvey Harrison 			__func__, &myaddr.sin_addr,
15797dc753f0SChuck Lever 			port, err ? "failed" : "ok", err);
1580a246b010SChuck Lever 	return err;
1581a246b010SChuck Lever }
1582a246b010SChuck Lever 
158390058d37SChuck Lever static int xs_bind6(struct sock_xprt *transport, struct socket *sock)
158490058d37SChuck Lever {
158590058d37SChuck Lever 	struct sockaddr_in6 myaddr = {
158690058d37SChuck Lever 		.sin6_family = AF_INET6,
158790058d37SChuck Lever 	};
158890058d37SChuck Lever 	struct sockaddr_in6 *sa;
158967a391d7STrond Myklebust 	int err, nloop = 0;
159067a391d7STrond Myklebust 	unsigned short port = xs_get_srcport(transport, sock);
159167a391d7STrond Myklebust 	unsigned short last;
159290058d37SChuck Lever 
1593fbfffbd5SChuck Lever 	sa = (struct sockaddr_in6 *)&transport->srcaddr;
159490058d37SChuck Lever 	myaddr.sin6_addr = sa->sin6_addr;
159590058d37SChuck Lever 	do {
159690058d37SChuck Lever 		myaddr.sin6_port = htons(port);
159790058d37SChuck Lever 		err = kernel_bind(sock, (struct sockaddr *) &myaddr,
159890058d37SChuck Lever 						sizeof(myaddr));
159967a391d7STrond Myklebust 		if (port == 0)
160090058d37SChuck Lever 			break;
160190058d37SChuck Lever 		if (err == 0) {
1602fbfffbd5SChuck Lever 			transport->srcport = port;
160390058d37SChuck Lever 			break;
160490058d37SChuck Lever 		}
160567a391d7STrond Myklebust 		last = port;
160667a391d7STrond Myklebust 		port = xs_next_srcport(transport, sock, port);
160767a391d7STrond Myklebust 		if (port > last)
160867a391d7STrond Myklebust 			nloop++;
160967a391d7STrond Myklebust 	} while (err == -EADDRINUSE && nloop != 2);
16105b095d98SHarvey Harrison 	dprintk("RPC:       xs_bind6 %pI6:%u: %s (%d)\n",
1611fdb46ee7SHarvey Harrison 		&myaddr.sin6_addr, port, err ? "failed" : "ok", err);
1612a246b010SChuck Lever 	return err;
1613a246b010SChuck Lever }
1614a246b010SChuck Lever 
1615ed07536eSPeter Zijlstra #ifdef CONFIG_DEBUG_LOCK_ALLOC
1616ed07536eSPeter Zijlstra static struct lock_class_key xs_key[2];
1617ed07536eSPeter Zijlstra static struct lock_class_key xs_slock_key[2];
1618ed07536eSPeter Zijlstra 
16198945ee5eSChuck Lever static inline void xs_reclassify_socket4(struct socket *sock)
1620ed07536eSPeter Zijlstra {
1621ed07536eSPeter Zijlstra 	struct sock *sk = sock->sk;
16228945ee5eSChuck Lever 
162302b3d346SJohn Heffner 	BUG_ON(sock_owned_by_user(sk));
16248945ee5eSChuck Lever 	sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC",
16258945ee5eSChuck Lever 		&xs_slock_key[0], "sk_lock-AF_INET-RPC", &xs_key[0]);
1626ed07536eSPeter Zijlstra }
16278945ee5eSChuck Lever 
16288945ee5eSChuck Lever static inline void xs_reclassify_socket6(struct socket *sock)
16298945ee5eSChuck Lever {
16308945ee5eSChuck Lever 	struct sock *sk = sock->sk;
16318945ee5eSChuck Lever 
1632f4921affSLinus Torvalds 	BUG_ON(sock_owned_by_user(sk));
16338945ee5eSChuck Lever 	sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC",
16348945ee5eSChuck Lever 		&xs_slock_key[1], "sk_lock-AF_INET6-RPC", &xs_key[1]);
1635ed07536eSPeter Zijlstra }
1636ed07536eSPeter Zijlstra #else
16378945ee5eSChuck Lever static inline void xs_reclassify_socket4(struct socket *sock)
16388945ee5eSChuck Lever {
16398945ee5eSChuck Lever }
16408945ee5eSChuck Lever 
16418945ee5eSChuck Lever static inline void xs_reclassify_socket6(struct socket *sock)
1642ed07536eSPeter Zijlstra {
1643ed07536eSPeter Zijlstra }
1644ed07536eSPeter Zijlstra #endif
1645ed07536eSPeter Zijlstra 
164616be2d20SChuck Lever static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
1647a246b010SChuck Lever {
164816be2d20SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1649edb267a6SChuck Lever 
1650ee0ac0c2SChuck Lever 	if (!transport->inet) {
1651b0d93ad5SChuck Lever 		struct sock *sk = sock->sk;
1652b0d93ad5SChuck Lever 
1653b0d93ad5SChuck Lever 		write_lock_bh(&sk->sk_callback_lock);
1654b0d93ad5SChuck Lever 
16552a9e1cfaSTrond Myklebust 		xs_save_old_callbacks(transport, sk);
16562a9e1cfaSTrond Myklebust 
1657b0d93ad5SChuck Lever 		sk->sk_user_data = xprt;
1658b0d93ad5SChuck Lever 		sk->sk_data_ready = xs_udp_data_ready;
1659b0d93ad5SChuck Lever 		sk->sk_write_space = xs_udp_write_space;
1660482f32e6STrond Myklebust 		sk->sk_error_report = xs_error_report;
1661b0d93ad5SChuck Lever 		sk->sk_no_check = UDP_CSUM_NORCV;
1662b079fa7bSTrond Myklebust 		sk->sk_allocation = GFP_ATOMIC;
1663b0d93ad5SChuck Lever 
1664b0d93ad5SChuck Lever 		xprt_set_connected(xprt);
1665b0d93ad5SChuck Lever 
1666b0d93ad5SChuck Lever 		/* Reset to new socket */
1667ee0ac0c2SChuck Lever 		transport->sock = sock;
1668ee0ac0c2SChuck Lever 		transport->inet = sk;
1669b0d93ad5SChuck Lever 
1670b0d93ad5SChuck Lever 		write_unlock_bh(&sk->sk_callback_lock);
1671b0d93ad5SChuck Lever 	}
1672470056c2SChuck Lever 	xs_udp_do_set_buffer_size(xprt);
167316be2d20SChuck Lever }
167416be2d20SChuck Lever 
1675a246b010SChuck Lever /**
16769c3d72deSChuck Lever  * xs_udp_connect_worker4 - set up a UDP socket
1677a246b010SChuck Lever  * @work: RPC transport to connect
1678a246b010SChuck Lever  *
1679a246b010SChuck Lever  * Invoked by a work queue tasklet.
1680a246b010SChuck Lever  */
16819c3d72deSChuck Lever static void xs_udp_connect_worker4(struct work_struct *work)
1682a246b010SChuck Lever {
1683a246b010SChuck Lever 	struct sock_xprt *transport =
1684a246b010SChuck Lever 		container_of(work, struct sock_xprt, connect_worker.work);
1685a246b010SChuck Lever 	struct rpc_xprt *xprt = &transport->xprt;
1686a246b010SChuck Lever 	struct socket *sock = transport->sock;
1687a246b010SChuck Lever 	int err, status = -EIO;
1688a246b010SChuck Lever 
168901d37c42STrond Myklebust 	if (xprt->shutdown)
16909903cd1cSChuck Lever 		goto out;
16919903cd1cSChuck Lever 
1692a246b010SChuck Lever 	/* Start by resetting any existing state */
1693fe315e76SChuck Lever 	xs_reset_transport(transport);
1694a246b010SChuck Lever 
1695fe315e76SChuck Lever 	err = sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock);
1696fe315e76SChuck Lever 	if (err < 0) {
16979903cd1cSChuck Lever 		dprintk("RPC:       can't create UDP transport socket (%d).\n", -err);
1698a246b010SChuck Lever 		goto out;
1699a246b010SChuck Lever 	}
17008945ee5eSChuck Lever 	xs_reclassify_socket4(sock);
1701a246b010SChuck Lever 
17027dc753f0SChuck Lever 	if (xs_bind4(transport, sock)) {
17039903cd1cSChuck Lever 		sock_release(sock);
17049903cd1cSChuck Lever 		goto out;
1705a246b010SChuck Lever 	}
1706b0d93ad5SChuck Lever 
1707c740eff8SChuck Lever 	dprintk("RPC:       worker connecting xprt %p via %s to "
1708c740eff8SChuck Lever 				"%s (port %s)\n", xprt,
1709c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_PROTO],
1710c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_ADDR],
1711c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_PORT]);
1712b0d93ad5SChuck Lever 
171316be2d20SChuck Lever 	xs_udp_finish_connecting(xprt, sock);
1714a246b010SChuck Lever 	status = 0;
1715b0d93ad5SChuck Lever out:
1716b0d93ad5SChuck Lever 	xprt_clear_connecting(xprt);
17177d1e8255STrond Myklebust 	xprt_wake_pending_tasks(xprt, status);
1718b0d93ad5SChuck Lever }
1719b0d93ad5SChuck Lever 
172068e220bdSChuck Lever /**
172168e220bdSChuck Lever  * xs_udp_connect_worker6 - set up a UDP socket
172268e220bdSChuck Lever  * @work: RPC transport to connect
172368e220bdSChuck Lever  *
172468e220bdSChuck Lever  * Invoked by a work queue tasklet.
172568e220bdSChuck Lever  */
172668e220bdSChuck Lever static void xs_udp_connect_worker6(struct work_struct *work)
172768e220bdSChuck Lever {
172868e220bdSChuck Lever 	struct sock_xprt *transport =
172968e220bdSChuck Lever 		container_of(work, struct sock_xprt, connect_worker.work);
173068e220bdSChuck Lever 	struct rpc_xprt *xprt = &transport->xprt;
173168e220bdSChuck Lever 	struct socket *sock = transport->sock;
173268e220bdSChuck Lever 	int err, status = -EIO;
173368e220bdSChuck Lever 
173401d37c42STrond Myklebust 	if (xprt->shutdown)
173568e220bdSChuck Lever 		goto out;
173668e220bdSChuck Lever 
173768e220bdSChuck Lever 	/* Start by resetting any existing state */
1738fe315e76SChuck Lever 	xs_reset_transport(transport);
173968e220bdSChuck Lever 
1740fe315e76SChuck Lever 	err = sock_create_kern(PF_INET6, SOCK_DGRAM, IPPROTO_UDP, &sock);
1741fe315e76SChuck Lever 	if (err < 0) {
174268e220bdSChuck Lever 		dprintk("RPC:       can't create UDP transport socket (%d).\n", -err);
174368e220bdSChuck Lever 		goto out;
174468e220bdSChuck Lever 	}
17458945ee5eSChuck Lever 	xs_reclassify_socket6(sock);
174668e220bdSChuck Lever 
174768e220bdSChuck Lever 	if (xs_bind6(transport, sock) < 0) {
174868e220bdSChuck Lever 		sock_release(sock);
174968e220bdSChuck Lever 		goto out;
175068e220bdSChuck Lever 	}
175168e220bdSChuck Lever 
1752c740eff8SChuck Lever 	dprintk("RPC:       worker connecting xprt %p via %s to "
1753c740eff8SChuck Lever 				"%s (port %s)\n", xprt,
1754c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_PROTO],
1755c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_ADDR],
1756c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_PORT]);
175768e220bdSChuck Lever 
175868e220bdSChuck Lever 	xs_udp_finish_connecting(xprt, sock);
1759a246b010SChuck Lever 	status = 0;
1760b0d93ad5SChuck Lever out:
1761b0d93ad5SChuck Lever 	xprt_clear_connecting(xprt);
17627d1e8255STrond Myklebust 	xprt_wake_pending_tasks(xprt, status);
1763b0d93ad5SChuck Lever }
1764b0d93ad5SChuck Lever 
17653167e12cSChuck Lever /*
17663167e12cSChuck Lever  * We need to preserve the port number so the reply cache on the server can
17673167e12cSChuck Lever  * find our cached RPC replies when we get around to reconnecting.
17683167e12cSChuck Lever  */
176940d2549dSTrond Myklebust static void xs_abort_connection(struct rpc_xprt *xprt, struct sock_xprt *transport)
17703167e12cSChuck Lever {
17713167e12cSChuck Lever 	int result;
17723167e12cSChuck Lever 	struct sockaddr any;
17733167e12cSChuck Lever 
17743167e12cSChuck Lever 	dprintk("RPC:       disconnecting xprt %p to reuse port\n", xprt);
17753167e12cSChuck Lever 
17763167e12cSChuck Lever 	/*
17773167e12cSChuck Lever 	 * Disconnect the transport socket by doing a connect operation
17783167e12cSChuck Lever 	 * with AF_UNSPEC.  This should return immediately...
17793167e12cSChuck Lever 	 */
17803167e12cSChuck Lever 	memset(&any, 0, sizeof(any));
17813167e12cSChuck Lever 	any.sa_family = AF_UNSPEC;
1782ee0ac0c2SChuck Lever 	result = kernel_connect(transport->sock, &any, sizeof(any), 0);
17837d1e8255STrond Myklebust 	if (!result)
17847d1e8255STrond Myklebust 		xs_sock_mark_closed(xprt);
17857d1e8255STrond Myklebust 	else
17863167e12cSChuck Lever 		dprintk("RPC:       AF_UNSPEC connect return code %d\n",
17873167e12cSChuck Lever 				result);
17883167e12cSChuck Lever }
17893167e12cSChuck Lever 
179040d2549dSTrond Myklebust static void xs_tcp_reuse_connection(struct rpc_xprt *xprt, struct sock_xprt *transport)
179140d2549dSTrond Myklebust {
179240d2549dSTrond Myklebust 	unsigned int state = transport->inet->sk_state;
179340d2549dSTrond Myklebust 
179440d2549dSTrond Myklebust 	if (state == TCP_CLOSE && transport->sock->state == SS_UNCONNECTED)
179540d2549dSTrond Myklebust 		return;
179640d2549dSTrond Myklebust 	if ((1 << state) & (TCPF_ESTABLISHED|TCPF_SYN_SENT))
179740d2549dSTrond Myklebust 		return;
179840d2549dSTrond Myklebust 	xs_abort_connection(xprt, transport);
179940d2549dSTrond Myklebust }
180040d2549dSTrond Myklebust 
180116be2d20SChuck Lever static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
1802b0d93ad5SChuck Lever {
180316be2d20SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1804edb267a6SChuck Lever 
1805ee0ac0c2SChuck Lever 	if (!transport->inet) {
1806b0d93ad5SChuck Lever 		struct sock *sk = sock->sk;
1807b0d93ad5SChuck Lever 
1808b0d93ad5SChuck Lever 		write_lock_bh(&sk->sk_callback_lock);
1809b0d93ad5SChuck Lever 
18102a9e1cfaSTrond Myklebust 		xs_save_old_callbacks(transport, sk);
18112a9e1cfaSTrond Myklebust 
1812b0d93ad5SChuck Lever 		sk->sk_user_data = xprt;
1813b0d93ad5SChuck Lever 		sk->sk_data_ready = xs_tcp_data_ready;
1814b0d93ad5SChuck Lever 		sk->sk_state_change = xs_tcp_state_change;
1815b0d93ad5SChuck Lever 		sk->sk_write_space = xs_tcp_write_space;
1816482f32e6STrond Myklebust 		sk->sk_error_report = xs_error_report;
1817b079fa7bSTrond Myklebust 		sk->sk_allocation = GFP_ATOMIC;
18183167e12cSChuck Lever 
18193167e12cSChuck Lever 		/* socket options */
18203167e12cSChuck Lever 		sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
18213167e12cSChuck Lever 		sock_reset_flag(sk, SOCK_LINGER);
18223167e12cSChuck Lever 		tcp_sk(sk)->linger2 = 0;
18233167e12cSChuck Lever 		tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF;
1824b0d93ad5SChuck Lever 
1825b0d93ad5SChuck Lever 		xprt_clear_connected(xprt);
1826b0d93ad5SChuck Lever 
1827b0d93ad5SChuck Lever 		/* Reset to new socket */
1828ee0ac0c2SChuck Lever 		transport->sock = sock;
1829ee0ac0c2SChuck Lever 		transport->inet = sk;
1830b0d93ad5SChuck Lever 
1831b0d93ad5SChuck Lever 		write_unlock_bh(&sk->sk_callback_lock);
1832b0d93ad5SChuck Lever 	}
1833b0d93ad5SChuck Lever 
183401d37c42STrond Myklebust 	if (!xprt_bound(xprt))
183501d37c42STrond Myklebust 		return -ENOTCONN;
183601d37c42STrond Myklebust 
1837b0d93ad5SChuck Lever 	/* Tell the socket layer to start connecting... */
1838262ca07dSChuck Lever 	xprt->stat.connect_count++;
1839262ca07dSChuck Lever 	xprt->stat.connect_start = jiffies;
184095392c59SChuck Lever 	return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK);
184116be2d20SChuck Lever }
184216be2d20SChuck Lever 
184316be2d20SChuck Lever /**
1844b61d59ffSTrond Myklebust  * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint
1845b61d59ffSTrond Myklebust  * @xprt: RPC transport to connect
1846b61d59ffSTrond Myklebust  * @transport: socket transport to connect
1847b61d59ffSTrond Myklebust  * @create_sock: function to create a socket of the correct type
184816be2d20SChuck Lever  *
184916be2d20SChuck Lever  * Invoked by a work queue tasklet.
185016be2d20SChuck Lever  */
1851b61d59ffSTrond Myklebust static void xs_tcp_setup_socket(struct rpc_xprt *xprt,
1852b61d59ffSTrond Myklebust 		struct sock_xprt *transport,
1853b61d59ffSTrond Myklebust 		struct socket *(*create_sock)(struct rpc_xprt *,
1854b61d59ffSTrond Myklebust 			struct sock_xprt *))
185516be2d20SChuck Lever {
185616be2d20SChuck Lever 	struct socket *sock = transport->sock;
1857b61d59ffSTrond Myklebust 	int status = -EIO;
185816be2d20SChuck Lever 
185901d37c42STrond Myklebust 	if (xprt->shutdown)
186016be2d20SChuck Lever 		goto out;
186116be2d20SChuck Lever 
186216be2d20SChuck Lever 	if (!sock) {
18637d1e8255STrond Myklebust 		clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1864b61d59ffSTrond Myklebust 		sock = create_sock(xprt, transport);
1865b61d59ffSTrond Myklebust 		if (IS_ERR(sock)) {
1866b61d59ffSTrond Myklebust 			status = PTR_ERR(sock);
186716be2d20SChuck Lever 			goto out;
186816be2d20SChuck Lever 		}
18697d1e8255STrond Myklebust 	} else {
18707d1e8255STrond Myklebust 		int abort_and_exit;
18717d1e8255STrond Myklebust 
18727d1e8255STrond Myklebust 		abort_and_exit = test_and_clear_bit(XPRT_CONNECTION_ABORT,
18737d1e8255STrond Myklebust 				&xprt->state);
187416be2d20SChuck Lever 		/* "close" the socket, preserving the local port */
187540d2549dSTrond Myklebust 		xs_tcp_reuse_connection(xprt, transport);
187616be2d20SChuck Lever 
18777d1e8255STrond Myklebust 		if (abort_and_exit)
18787d1e8255STrond Myklebust 			goto out_eagain;
18797d1e8255STrond Myklebust 	}
18807d1e8255STrond Myklebust 
1881c740eff8SChuck Lever 	dprintk("RPC:       worker connecting xprt %p via %s to "
1882c740eff8SChuck Lever 				"%s (port %s)\n", xprt,
1883c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_PROTO],
1884c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_ADDR],
1885c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_PORT]);
188616be2d20SChuck Lever 
188716be2d20SChuck Lever 	status = xs_tcp_finish_connecting(xprt, sock);
1888a246b010SChuck Lever 	dprintk("RPC:       %p connect status %d connected %d sock state %d\n",
188946121cf7SChuck Lever 			xprt, -status, xprt_connected(xprt),
189046121cf7SChuck Lever 			sock->sk->sk_state);
1891a246b010SChuck Lever 	switch (status) {
1892f75e6745STrond Myklebust 	default:
1893f75e6745STrond Myklebust 		printk("%s: connect returned unhandled error %d\n",
1894f75e6745STrond Myklebust 			__func__, status);
1895f75e6745STrond Myklebust 	case -EADDRNOTAVAIL:
1896f75e6745STrond Myklebust 		/* We're probably in TIME_WAIT. Get rid of existing socket,
1897f75e6745STrond Myklebust 		 * and retry
1898f75e6745STrond Myklebust 		 */
1899f75e6745STrond Myklebust 		set_bit(XPRT_CONNECTION_CLOSE, &xprt->state);
1900f75e6745STrond Myklebust 		xprt_force_disconnect(xprt);
190188b5ed73STrond Myklebust 		break;
19028a2cec29STrond Myklebust 	case -ECONNREFUSED:
19038a2cec29STrond Myklebust 	case -ECONNRESET:
19048a2cec29STrond Myklebust 	case -ENETUNREACH:
19058a2cec29STrond Myklebust 		/* retry with existing socket, after a delay */
19062a491991STrond Myklebust 	case 0:
1907a246b010SChuck Lever 	case -EINPROGRESS:
1908a246b010SChuck Lever 	case -EALREADY:
19097d1e8255STrond Myklebust 		xprt_clear_connecting(xprt);
19107d1e8255STrond Myklebust 		return;
19119fcfe0c8STrond Myklebust 	case -EINVAL:
19129fcfe0c8STrond Myklebust 		/* Happens, for instance, if the user specified a link
19139fcfe0c8STrond Myklebust 		 * local IPv6 address without a scope-id.
19149fcfe0c8STrond Myklebust 		 */
19159fcfe0c8STrond Myklebust 		goto out;
19168a2cec29STrond Myklebust 	}
19177d1e8255STrond Myklebust out_eagain:
19182a491991STrond Myklebust 	status = -EAGAIN;
1919a246b010SChuck Lever out:
19202226feb6SChuck Lever 	xprt_clear_connecting(xprt);
19217d1e8255STrond Myklebust 	xprt_wake_pending_tasks(xprt, status);
1922a246b010SChuck Lever }
1923a246b010SChuck Lever 
1924b61d59ffSTrond Myklebust static struct socket *xs_create_tcp_sock4(struct rpc_xprt *xprt,
1925b61d59ffSTrond Myklebust 		struct sock_xprt *transport)
1926b61d59ffSTrond Myklebust {
1927b61d59ffSTrond Myklebust 	struct socket *sock;
1928b61d59ffSTrond Myklebust 	int err;
1929b61d59ffSTrond Myklebust 
1930b61d59ffSTrond Myklebust 	/* start from scratch */
1931b61d59ffSTrond Myklebust 	err = sock_create_kern(PF_INET, SOCK_STREAM, IPPROTO_TCP, &sock);
1932b61d59ffSTrond Myklebust 	if (err < 0) {
1933b61d59ffSTrond Myklebust 		dprintk("RPC:       can't create TCP transport socket (%d).\n",
1934b61d59ffSTrond Myklebust 				-err);
1935b61d59ffSTrond Myklebust 		goto out_err;
1936b61d59ffSTrond Myklebust 	}
1937b61d59ffSTrond Myklebust 	xs_reclassify_socket4(sock);
1938b61d59ffSTrond Myklebust 
1939b61d59ffSTrond Myklebust 	if (xs_bind4(transport, sock) < 0) {
1940b61d59ffSTrond Myklebust 		sock_release(sock);
1941b61d59ffSTrond Myklebust 		goto out_err;
1942b61d59ffSTrond Myklebust 	}
1943b61d59ffSTrond Myklebust 	return sock;
1944b61d59ffSTrond Myklebust out_err:
1945b61d59ffSTrond Myklebust 	return ERR_PTR(-EIO);
1946b61d59ffSTrond Myklebust }
1947b61d59ffSTrond Myklebust 
1948b61d59ffSTrond Myklebust /**
1949a246b010SChuck Lever  * xs_tcp_connect_worker4 - connect a TCP socket to a remote endpoint
1950a246b010SChuck Lever  * @work: RPC transport to connect
1951a246b010SChuck Lever  *
1952a246b010SChuck Lever  * Invoked by a work queue tasklet.
1953a246b010SChuck Lever  */
1954a246b010SChuck Lever static void xs_tcp_connect_worker4(struct work_struct *work)
1955a246b010SChuck Lever {
1956a246b010SChuck Lever 	struct sock_xprt *transport =
1957a246b010SChuck Lever 		container_of(work, struct sock_xprt, connect_worker.work);
1958a246b010SChuck Lever 	struct rpc_xprt *xprt = &transport->xprt;
1959a246b010SChuck Lever 
1960b61d59ffSTrond Myklebust 	xs_tcp_setup_socket(xprt, transport, xs_create_tcp_sock4);
1961b61d59ffSTrond Myklebust }
1962a246b010SChuck Lever 
1963b61d59ffSTrond Myklebust static struct socket *xs_create_tcp_sock6(struct rpc_xprt *xprt,
1964b61d59ffSTrond Myklebust 		struct sock_xprt *transport)
1965b61d59ffSTrond Myklebust {
1966b61d59ffSTrond Myklebust 	struct socket *sock;
1967b61d59ffSTrond Myklebust 	int err;
1968b61d59ffSTrond Myklebust 
1969a246b010SChuck Lever 	/* start from scratch */
1970b61d59ffSTrond Myklebust 	err = sock_create_kern(PF_INET6, SOCK_STREAM, IPPROTO_TCP, &sock);
1971b61d59ffSTrond Myklebust 	if (err < 0) {
1972b61d59ffSTrond Myklebust 		dprintk("RPC:       can't create TCP transport socket (%d).\n",
1973b61d59ffSTrond Myklebust 				-err);
1974b61d59ffSTrond Myklebust 		goto out_err;
19759903cd1cSChuck Lever 	}
1976b61d59ffSTrond Myklebust 	xs_reclassify_socket6(sock);
19779903cd1cSChuck Lever 
1978b61d59ffSTrond Myklebust 	if (xs_bind6(transport, sock) < 0) {
1979a246b010SChuck Lever 		sock_release(sock);
1980b61d59ffSTrond Myklebust 		goto out_err;
1981a246b010SChuck Lever 	}
1982b61d59ffSTrond Myklebust 	return sock;
1983b61d59ffSTrond Myklebust out_err:
1984b61d59ffSTrond Myklebust 	return ERR_PTR(-EIO);
1985a246b010SChuck Lever }
1986a246b010SChuck Lever 
1987a246b010SChuck Lever /**
198868e220bdSChuck Lever  * xs_tcp_connect_worker6 - connect a TCP socket to a remote endpoint
198968e220bdSChuck Lever  * @work: RPC transport to connect
199068e220bdSChuck Lever  *
199168e220bdSChuck Lever  * Invoked by a work queue tasklet.
199268e220bdSChuck Lever  */
199368e220bdSChuck Lever static void xs_tcp_connect_worker6(struct work_struct *work)
199468e220bdSChuck Lever {
199568e220bdSChuck Lever 	struct sock_xprt *transport =
199668e220bdSChuck Lever 		container_of(work, struct sock_xprt, connect_worker.work);
199768e220bdSChuck Lever 	struct rpc_xprt *xprt = &transport->xprt;
199868e220bdSChuck Lever 
1999b61d59ffSTrond Myklebust 	xs_tcp_setup_socket(xprt, transport, xs_create_tcp_sock6);
200068e220bdSChuck Lever }
200168e220bdSChuck Lever 
200268e220bdSChuck Lever /**
2003a246b010SChuck Lever  * xs_connect - connect a socket to a remote endpoint
2004a246b010SChuck Lever  * @task: address of RPC task that manages state of connect request
2005a246b010SChuck Lever  *
2006a246b010SChuck Lever  * TCP: If the remote end dropped the connection, delay reconnecting.
200703bf4b70SChuck Lever  *
200803bf4b70SChuck Lever  * UDP socket connects are synchronous, but we use a work queue anyway
200903bf4b70SChuck Lever  * to guarantee that even unprivileged user processes can set up a
201003bf4b70SChuck Lever  * socket on a privileged port.
201103bf4b70SChuck Lever  *
201203bf4b70SChuck Lever  * If a UDP socket connect fails, the delay behavior here prevents
201303bf4b70SChuck Lever  * retry floods (hard mounts).
2014a246b010SChuck Lever  */
2015a246b010SChuck Lever static void xs_connect(struct rpc_task *task)
2016a246b010SChuck Lever {
2017a246b010SChuck Lever 	struct rpc_xprt *xprt = task->tk_xprt;
2018ee0ac0c2SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2019a246b010SChuck Lever 
2020b0d93ad5SChuck Lever 	if (xprt_test_and_set_connecting(xprt))
2021b0d93ad5SChuck Lever 		return;
2022b0d93ad5SChuck Lever 
202309a21c41SChuck Lever 	if (transport->sock != NULL && !RPC_IS_SOFTCONN(task)) {
202446121cf7SChuck Lever 		dprintk("RPC:       xs_connect delayed xprt %p for %lu "
202546121cf7SChuck Lever 				"seconds\n",
202603bf4b70SChuck Lever 				xprt, xprt->reestablish_timeout / HZ);
2027c1384c9cSTrond Myklebust 		queue_delayed_work(rpciod_workqueue,
2028c1384c9cSTrond Myklebust 				   &transport->connect_worker,
202903bf4b70SChuck Lever 				   xprt->reestablish_timeout);
203003bf4b70SChuck Lever 		xprt->reestablish_timeout <<= 1;
203161d0a8e6SNeil Brown 		if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
203261d0a8e6SNeil Brown 			xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
203303bf4b70SChuck Lever 		if (xprt->reestablish_timeout > XS_TCP_MAX_REEST_TO)
203403bf4b70SChuck Lever 			xprt->reestablish_timeout = XS_TCP_MAX_REEST_TO;
20359903cd1cSChuck Lever 	} else {
20369903cd1cSChuck Lever 		dprintk("RPC:       xs_connect scheduled xprt %p\n", xprt);
2037c1384c9cSTrond Myklebust 		queue_delayed_work(rpciod_workqueue,
2038c1384c9cSTrond Myklebust 				   &transport->connect_worker, 0);
2039a246b010SChuck Lever 	}
2040a246b010SChuck Lever }
2041a246b010SChuck Lever 
2042e06799f9STrond Myklebust static void xs_tcp_connect(struct rpc_task *task)
2043e06799f9STrond Myklebust {
2044e06799f9STrond Myklebust 	struct rpc_xprt *xprt = task->tk_xprt;
2045e06799f9STrond Myklebust 
2046e06799f9STrond Myklebust 	/* Exit if we need to wait for socket shutdown to complete */
2047e06799f9STrond Myklebust 	if (test_bit(XPRT_CLOSING, &xprt->state))
2048e06799f9STrond Myklebust 		return;
2049e06799f9STrond Myklebust 	xs_connect(task);
2050e06799f9STrond Myklebust }
2051e06799f9STrond Myklebust 
2052262ca07dSChuck Lever /**
2053262ca07dSChuck Lever  * xs_udp_print_stats - display UDP socket-specifc stats
2054262ca07dSChuck Lever  * @xprt: rpc_xprt struct containing statistics
2055262ca07dSChuck Lever  * @seq: output file
2056262ca07dSChuck Lever  *
2057262ca07dSChuck Lever  */
2058262ca07dSChuck Lever static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2059262ca07dSChuck Lever {
2060c8475461SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2061c8475461SChuck Lever 
2062262ca07dSChuck Lever 	seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %Lu %Lu\n",
2063fbfffbd5SChuck Lever 			transport->srcport,
2064262ca07dSChuck Lever 			xprt->stat.bind_count,
2065262ca07dSChuck Lever 			xprt->stat.sends,
2066262ca07dSChuck Lever 			xprt->stat.recvs,
2067262ca07dSChuck Lever 			xprt->stat.bad_xids,
2068262ca07dSChuck Lever 			xprt->stat.req_u,
2069262ca07dSChuck Lever 			xprt->stat.bklog_u);
2070262ca07dSChuck Lever }
2071262ca07dSChuck Lever 
2072262ca07dSChuck Lever /**
2073262ca07dSChuck Lever  * xs_tcp_print_stats - display TCP socket-specifc stats
2074262ca07dSChuck Lever  * @xprt: rpc_xprt struct containing statistics
2075262ca07dSChuck Lever  * @seq: output file
2076262ca07dSChuck Lever  *
2077262ca07dSChuck Lever  */
2078262ca07dSChuck Lever static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2079262ca07dSChuck Lever {
2080c8475461SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2081262ca07dSChuck Lever 	long idle_time = 0;
2082262ca07dSChuck Lever 
2083262ca07dSChuck Lever 	if (xprt_connected(xprt))
2084262ca07dSChuck Lever 		idle_time = (long)(jiffies - xprt->last_used) / HZ;
2085262ca07dSChuck Lever 
2086262ca07dSChuck Lever 	seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu %Lu %Lu\n",
2087fbfffbd5SChuck Lever 			transport->srcport,
2088262ca07dSChuck Lever 			xprt->stat.bind_count,
2089262ca07dSChuck Lever 			xprt->stat.connect_count,
2090262ca07dSChuck Lever 			xprt->stat.connect_time,
2091262ca07dSChuck Lever 			idle_time,
2092262ca07dSChuck Lever 			xprt->stat.sends,
2093262ca07dSChuck Lever 			xprt->stat.recvs,
2094262ca07dSChuck Lever 			xprt->stat.bad_xids,
2095262ca07dSChuck Lever 			xprt->stat.req_u,
2096262ca07dSChuck Lever 			xprt->stat.bklog_u);
2097262ca07dSChuck Lever }
2098262ca07dSChuck Lever 
20994cfc7e60SRahul Iyer /*
21004cfc7e60SRahul Iyer  * Allocate a bunch of pages for a scratch buffer for the rpc code. The reason
21014cfc7e60SRahul Iyer  * we allocate pages instead doing a kmalloc like rpc_malloc is because we want
21024cfc7e60SRahul Iyer  * to use the server side send routines.
21034cfc7e60SRahul Iyer  */
21045a51f13aSH Hartley Sweeten static void *bc_malloc(struct rpc_task *task, size_t size)
21054cfc7e60SRahul Iyer {
21064cfc7e60SRahul Iyer 	struct page *page;
21074cfc7e60SRahul Iyer 	struct rpc_buffer *buf;
21084cfc7e60SRahul Iyer 
21094cfc7e60SRahul Iyer 	BUG_ON(size > PAGE_SIZE - sizeof(struct rpc_buffer));
21104cfc7e60SRahul Iyer 	page = alloc_page(GFP_KERNEL);
21114cfc7e60SRahul Iyer 
21124cfc7e60SRahul Iyer 	if (!page)
21134cfc7e60SRahul Iyer 		return NULL;
21144cfc7e60SRahul Iyer 
21154cfc7e60SRahul Iyer 	buf = page_address(page);
21164cfc7e60SRahul Iyer 	buf->len = PAGE_SIZE;
21174cfc7e60SRahul Iyer 
21184cfc7e60SRahul Iyer 	return buf->data;
21194cfc7e60SRahul Iyer }
21204cfc7e60SRahul Iyer 
21214cfc7e60SRahul Iyer /*
21224cfc7e60SRahul Iyer  * Free the space allocated in the bc_alloc routine
21234cfc7e60SRahul Iyer  */
21245a51f13aSH Hartley Sweeten static void bc_free(void *buffer)
21254cfc7e60SRahul Iyer {
21264cfc7e60SRahul Iyer 	struct rpc_buffer *buf;
21274cfc7e60SRahul Iyer 
21284cfc7e60SRahul Iyer 	if (!buffer)
21294cfc7e60SRahul Iyer 		return;
21304cfc7e60SRahul Iyer 
21314cfc7e60SRahul Iyer 	buf = container_of(buffer, struct rpc_buffer, data);
21324cfc7e60SRahul Iyer 	free_page((unsigned long)buf);
21334cfc7e60SRahul Iyer }
21344cfc7e60SRahul Iyer 
21354cfc7e60SRahul Iyer /*
21364cfc7e60SRahul Iyer  * Use the svc_sock to send the callback. Must be called with svsk->sk_mutex
21374cfc7e60SRahul Iyer  * held. Borrows heavily from svc_tcp_sendto and xs_tcp_send_request.
21384cfc7e60SRahul Iyer  */
21394cfc7e60SRahul Iyer static int bc_sendto(struct rpc_rqst *req)
21404cfc7e60SRahul Iyer {
21414cfc7e60SRahul Iyer 	int len;
21424cfc7e60SRahul Iyer 	struct xdr_buf *xbufp = &req->rq_snd_buf;
21434cfc7e60SRahul Iyer 	struct rpc_xprt *xprt = req->rq_xprt;
21444cfc7e60SRahul Iyer 	struct sock_xprt *transport =
21454cfc7e60SRahul Iyer 				container_of(xprt, struct sock_xprt, xprt);
21464cfc7e60SRahul Iyer 	struct socket *sock = transport->sock;
21474cfc7e60SRahul Iyer 	unsigned long headoff;
21484cfc7e60SRahul Iyer 	unsigned long tailoff;
21494cfc7e60SRahul Iyer 
21504cfc7e60SRahul Iyer 	/*
21514cfc7e60SRahul Iyer 	 * Set up the rpc header and record marker stuff
21524cfc7e60SRahul Iyer 	 */
21534cfc7e60SRahul Iyer 	xs_encode_tcp_record_marker(xbufp);
21544cfc7e60SRahul Iyer 
21554cfc7e60SRahul Iyer 	tailoff = (unsigned long)xbufp->tail[0].iov_base & ~PAGE_MASK;
21564cfc7e60SRahul Iyer 	headoff = (unsigned long)xbufp->head[0].iov_base & ~PAGE_MASK;
21574cfc7e60SRahul Iyer 	len = svc_send_common(sock, xbufp,
21584cfc7e60SRahul Iyer 			      virt_to_page(xbufp->head[0].iov_base), headoff,
21594cfc7e60SRahul Iyer 			      xbufp->tail[0].iov_base, tailoff);
21604cfc7e60SRahul Iyer 
21614cfc7e60SRahul Iyer 	if (len != xbufp->len) {
21624cfc7e60SRahul Iyer 		printk(KERN_NOTICE "Error sending entire callback!\n");
21634cfc7e60SRahul Iyer 		len = -EAGAIN;
21644cfc7e60SRahul Iyer 	}
21654cfc7e60SRahul Iyer 
21664cfc7e60SRahul Iyer 	return len;
21674cfc7e60SRahul Iyer }
21684cfc7e60SRahul Iyer 
21694cfc7e60SRahul Iyer /*
21704cfc7e60SRahul Iyer  * The send routine. Borrows from svc_send
21714cfc7e60SRahul Iyer  */
21724cfc7e60SRahul Iyer static int bc_send_request(struct rpc_task *task)
21734cfc7e60SRahul Iyer {
21744cfc7e60SRahul Iyer 	struct rpc_rqst *req = task->tk_rqstp;
21754cfc7e60SRahul Iyer 	struct svc_xprt	*xprt;
21764cfc7e60SRahul Iyer 	struct svc_sock         *svsk;
21774cfc7e60SRahul Iyer 	u32                     len;
21784cfc7e60SRahul Iyer 
21794cfc7e60SRahul Iyer 	dprintk("sending request with xid: %08x\n", ntohl(req->rq_xid));
21804cfc7e60SRahul Iyer 	/*
21814cfc7e60SRahul Iyer 	 * Get the server socket associated with this callback xprt
21824cfc7e60SRahul Iyer 	 */
21834cfc7e60SRahul Iyer 	xprt = req->rq_xprt->bc_xprt;
21844cfc7e60SRahul Iyer 	svsk = container_of(xprt, struct svc_sock, sk_xprt);
21854cfc7e60SRahul Iyer 
21864cfc7e60SRahul Iyer 	/*
21874cfc7e60SRahul Iyer 	 * Grab the mutex to serialize data as the connection is shared
21884cfc7e60SRahul Iyer 	 * with the fore channel
21894cfc7e60SRahul Iyer 	 */
21904cfc7e60SRahul Iyer 	if (!mutex_trylock(&xprt->xpt_mutex)) {
21914cfc7e60SRahul Iyer 		rpc_sleep_on(&xprt->xpt_bc_pending, task, NULL);
21924cfc7e60SRahul Iyer 		if (!mutex_trylock(&xprt->xpt_mutex))
21934cfc7e60SRahul Iyer 			return -EAGAIN;
21944cfc7e60SRahul Iyer 		rpc_wake_up_queued_task(&xprt->xpt_bc_pending, task);
21954cfc7e60SRahul Iyer 	}
21964cfc7e60SRahul Iyer 	if (test_bit(XPT_DEAD, &xprt->xpt_flags))
21974cfc7e60SRahul Iyer 		len = -ENOTCONN;
21984cfc7e60SRahul Iyer 	else
21994cfc7e60SRahul Iyer 		len = bc_sendto(req);
22004cfc7e60SRahul Iyer 	mutex_unlock(&xprt->xpt_mutex);
22014cfc7e60SRahul Iyer 
22024cfc7e60SRahul Iyer 	if (len > 0)
22034cfc7e60SRahul Iyer 		len = 0;
22044cfc7e60SRahul Iyer 
22054cfc7e60SRahul Iyer 	return len;
22064cfc7e60SRahul Iyer }
22074cfc7e60SRahul Iyer 
22084cfc7e60SRahul Iyer /*
22094cfc7e60SRahul Iyer  * The close routine. Since this is client initiated, we do nothing
22104cfc7e60SRahul Iyer  */
22114cfc7e60SRahul Iyer 
22124cfc7e60SRahul Iyer static void bc_close(struct rpc_xprt *xprt)
22134cfc7e60SRahul Iyer {
22144cfc7e60SRahul Iyer 	return;
22154cfc7e60SRahul Iyer }
22164cfc7e60SRahul Iyer 
22174cfc7e60SRahul Iyer /*
22184cfc7e60SRahul Iyer  * The xprt destroy routine. Again, because this connection is client
22194cfc7e60SRahul Iyer  * initiated, we do nothing
22204cfc7e60SRahul Iyer  */
22214cfc7e60SRahul Iyer 
22224cfc7e60SRahul Iyer static void bc_destroy(struct rpc_xprt *xprt)
22234cfc7e60SRahul Iyer {
22244cfc7e60SRahul Iyer 	return;
22254cfc7e60SRahul Iyer }
22264cfc7e60SRahul Iyer 
2227262965f5SChuck Lever static struct rpc_xprt_ops xs_udp_ops = {
222843118c29SChuck Lever 	.set_buffer_size	= xs_udp_set_buffer_size,
222912a80469SChuck Lever 	.reserve_xprt		= xprt_reserve_xprt_cong,
223049e9a890SChuck Lever 	.release_xprt		= xprt_release_xprt_cong,
223145160d62SChuck Lever 	.rpcbind		= rpcb_getport_async,
223292200412SChuck Lever 	.set_port		= xs_set_port,
22339903cd1cSChuck Lever 	.connect		= xs_connect,
223402107148SChuck Lever 	.buf_alloc		= rpc_malloc,
223502107148SChuck Lever 	.buf_free		= rpc_free,
2236262965f5SChuck Lever 	.send_request		= xs_udp_send_request,
2237fe3aca29SChuck Lever 	.set_retrans_timeout	= xprt_set_retrans_timeout_rtt,
223846c0ee8bSChuck Lever 	.timer			= xs_udp_timer,
2239a58dd398SChuck Lever 	.release_request	= xprt_release_rqst_cong,
2240262965f5SChuck Lever 	.close			= xs_close,
2241262965f5SChuck Lever 	.destroy		= xs_destroy,
2242262ca07dSChuck Lever 	.print_stats		= xs_udp_print_stats,
2243262965f5SChuck Lever };
2244262965f5SChuck Lever 
2245262965f5SChuck Lever static struct rpc_xprt_ops xs_tcp_ops = {
224612a80469SChuck Lever 	.reserve_xprt		= xprt_reserve_xprt,
2247e0ab53deSTrond Myklebust 	.release_xprt		= xs_tcp_release_xprt,
224845160d62SChuck Lever 	.rpcbind		= rpcb_getport_async,
224992200412SChuck Lever 	.set_port		= xs_set_port,
2250e06799f9STrond Myklebust 	.connect		= xs_tcp_connect,
225102107148SChuck Lever 	.buf_alloc		= rpc_malloc,
225202107148SChuck Lever 	.buf_free		= rpc_free,
2253262965f5SChuck Lever 	.send_request		= xs_tcp_send_request,
2254fe3aca29SChuck Lever 	.set_retrans_timeout	= xprt_set_retrans_timeout_def,
22550d90ba1cSRicardo Labiaga #if defined(CONFIG_NFS_V4_1)
22560d90ba1cSRicardo Labiaga 	.release_request	= bc_release_request,
22570d90ba1cSRicardo Labiaga #endif /* CONFIG_NFS_V4_1 */
2258f75e6745STrond Myklebust 	.close			= xs_tcp_close,
22599903cd1cSChuck Lever 	.destroy		= xs_destroy,
2260262ca07dSChuck Lever 	.print_stats		= xs_tcp_print_stats,
2261a246b010SChuck Lever };
2262a246b010SChuck Lever 
22634cfc7e60SRahul Iyer /*
22644cfc7e60SRahul Iyer  * The rpc_xprt_ops for the server backchannel
22654cfc7e60SRahul Iyer  */
22664cfc7e60SRahul Iyer 
22674cfc7e60SRahul Iyer static struct rpc_xprt_ops bc_tcp_ops = {
22684cfc7e60SRahul Iyer 	.reserve_xprt		= xprt_reserve_xprt,
22694cfc7e60SRahul Iyer 	.release_xprt		= xprt_release_xprt,
22704cfc7e60SRahul Iyer 	.buf_alloc		= bc_malloc,
22714cfc7e60SRahul Iyer 	.buf_free		= bc_free,
22724cfc7e60SRahul Iyer 	.send_request		= bc_send_request,
22734cfc7e60SRahul Iyer 	.set_retrans_timeout	= xprt_set_retrans_timeout_def,
22744cfc7e60SRahul Iyer 	.close			= bc_close,
22754cfc7e60SRahul Iyer 	.destroy		= bc_destroy,
22764cfc7e60SRahul Iyer 	.print_stats		= xs_tcp_print_stats,
22774cfc7e60SRahul Iyer };
22784cfc7e60SRahul Iyer 
22793c341b0bS\"Talpey, Thomas\ static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args,
2280bc25571eS\"Talpey, Thomas\ 				      unsigned int slot_table_size)
2281c8541ecdSChuck Lever {
2282c8541ecdSChuck Lever 	struct rpc_xprt *xprt;
2283ffc2e518SChuck Lever 	struct sock_xprt *new;
2284c8541ecdSChuck Lever 
228596802a09SFrank van Maarseveen 	if (args->addrlen > sizeof(xprt->addr)) {
2286c8541ecdSChuck Lever 		dprintk("RPC:       xs_setup_xprt: address too large\n");
2287c8541ecdSChuck Lever 		return ERR_PTR(-EBADF);
2288c8541ecdSChuck Lever 	}
2289c8541ecdSChuck Lever 
2290ffc2e518SChuck Lever 	new = kzalloc(sizeof(*new), GFP_KERNEL);
2291ffc2e518SChuck Lever 	if (new == NULL) {
229246121cf7SChuck Lever 		dprintk("RPC:       xs_setup_xprt: couldn't allocate "
229346121cf7SChuck Lever 				"rpc_xprt\n");
2294c8541ecdSChuck Lever 		return ERR_PTR(-ENOMEM);
2295c8541ecdSChuck Lever 	}
2296ffc2e518SChuck Lever 	xprt = &new->xprt;
2297c8541ecdSChuck Lever 
2298c8541ecdSChuck Lever 	xprt->max_reqs = slot_table_size;
2299c8541ecdSChuck Lever 	xprt->slot = kcalloc(xprt->max_reqs, sizeof(struct rpc_rqst), GFP_KERNEL);
2300c8541ecdSChuck Lever 	if (xprt->slot == NULL) {
2301c8541ecdSChuck Lever 		kfree(xprt);
230246121cf7SChuck Lever 		dprintk("RPC:       xs_setup_xprt: couldn't allocate slot "
230346121cf7SChuck Lever 				"table\n");
2304c8541ecdSChuck Lever 		return ERR_PTR(-ENOMEM);
2305c8541ecdSChuck Lever 	}
2306c8541ecdSChuck Lever 
230796802a09SFrank van Maarseveen 	memcpy(&xprt->addr, args->dstaddr, args->addrlen);
230896802a09SFrank van Maarseveen 	xprt->addrlen = args->addrlen;
2309d3bc9a1dSFrank van Maarseveen 	if (args->srcaddr)
2310fbfffbd5SChuck Lever 		memcpy(&new->srcaddr, args->srcaddr, args->addrlen);
2311c8541ecdSChuck Lever 
2312c8541ecdSChuck Lever 	return xprt;
2313c8541ecdSChuck Lever }
2314c8541ecdSChuck Lever 
23152881ae74STrond Myklebust static const struct rpc_timeout xs_udp_default_timeout = {
23162881ae74STrond Myklebust 	.to_initval = 5 * HZ,
23172881ae74STrond Myklebust 	.to_maxval = 30 * HZ,
23182881ae74STrond Myklebust 	.to_increment = 5 * HZ,
23192881ae74STrond Myklebust 	.to_retries = 5,
23202881ae74STrond Myklebust };
23212881ae74STrond Myklebust 
23229903cd1cSChuck Lever /**
23239903cd1cSChuck Lever  * xs_setup_udp - Set up transport to use a UDP socket
232496802a09SFrank van Maarseveen  * @args: rpc transport creation arguments
23259903cd1cSChuck Lever  *
23269903cd1cSChuck Lever  */
2327483066d6SAdrian Bunk static struct rpc_xprt *xs_setup_udp(struct xprt_create *args)
2328a246b010SChuck Lever {
23298f9d5b1aSChuck Lever 	struct sockaddr *addr = args->dstaddr;
2330c8541ecdSChuck Lever 	struct rpc_xprt *xprt;
2331c8475461SChuck Lever 	struct sock_xprt *transport;
2332a246b010SChuck Lever 
233396802a09SFrank van Maarseveen 	xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries);
2334c8541ecdSChuck Lever 	if (IS_ERR(xprt))
2335c8541ecdSChuck Lever 		return xprt;
2336c8475461SChuck Lever 	transport = container_of(xprt, struct sock_xprt, xprt);
2337a246b010SChuck Lever 
2338ec739ef0SChuck Lever 	xprt->prot = IPPROTO_UDP;
2339808012fbSChuck Lever 	xprt->tsh_size = 0;
2340a246b010SChuck Lever 	/* XXX: header size can vary due to auth type, IPv6, etc. */
2341a246b010SChuck Lever 	xprt->max_payload = (1U << 16) - (MAX_HEADER << 3);
2342a246b010SChuck Lever 
234303bf4b70SChuck Lever 	xprt->bind_timeout = XS_BIND_TO;
234403bf4b70SChuck Lever 	xprt->connect_timeout = XS_UDP_CONN_TO;
234503bf4b70SChuck Lever 	xprt->reestablish_timeout = XS_UDP_REEST_TO;
234603bf4b70SChuck Lever 	xprt->idle_timeout = XS_IDLE_DISC_TO;
2347a246b010SChuck Lever 
2348262965f5SChuck Lever 	xprt->ops = &xs_udp_ops;
2349a246b010SChuck Lever 
2350ba7392bbSTrond Myklebust 	xprt->timeout = &xs_udp_default_timeout;
2351a246b010SChuck Lever 
23528f9d5b1aSChuck Lever 	switch (addr->sa_family) {
23538f9d5b1aSChuck Lever 	case AF_INET:
23548f9d5b1aSChuck Lever 		if (((struct sockaddr_in *)addr)->sin_port != htons(0))
23558f9d5b1aSChuck Lever 			xprt_set_bound(xprt);
23568f9d5b1aSChuck Lever 
23578f9d5b1aSChuck Lever 		INIT_DELAYED_WORK(&transport->connect_worker,
23588f9d5b1aSChuck Lever 					xs_udp_connect_worker4);
23599dc3b095SChuck Lever 		xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP);
23608f9d5b1aSChuck Lever 		break;
23618f9d5b1aSChuck Lever 	case AF_INET6:
23628f9d5b1aSChuck Lever 		if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
23638f9d5b1aSChuck Lever 			xprt_set_bound(xprt);
23648f9d5b1aSChuck Lever 
23658f9d5b1aSChuck Lever 		INIT_DELAYED_WORK(&transport->connect_worker,
23668f9d5b1aSChuck Lever 					xs_udp_connect_worker6);
23679dc3b095SChuck Lever 		xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6);
23688f9d5b1aSChuck Lever 		break;
23698f9d5b1aSChuck Lever 	default:
23708f9d5b1aSChuck Lever 		kfree(xprt);
23718f9d5b1aSChuck Lever 		return ERR_PTR(-EAFNOSUPPORT);
23728f9d5b1aSChuck Lever 	}
23738f9d5b1aSChuck Lever 
2374c740eff8SChuck Lever 	if (xprt_bound(xprt))
2375c740eff8SChuck Lever 		dprintk("RPC:       set up xprt to %s (port %s) via %s\n",
2376c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_ADDR],
2377c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_PORT],
2378c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_PROTO]);
2379c740eff8SChuck Lever 	else
2380c740eff8SChuck Lever 		dprintk("RPC:       set up xprt to %s (autobind) via %s\n",
2381c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_ADDR],
2382c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_PROTO]);
2383edb267a6SChuck Lever 
2384bc25571eS\"Talpey, Thomas\ 	if (try_module_get(THIS_MODULE))
2385c8541ecdSChuck Lever 		return xprt;
2386bc25571eS\"Talpey, Thomas\ 
2387bc25571eS\"Talpey, Thomas\ 	kfree(xprt->slot);
2388bc25571eS\"Talpey, Thomas\ 	kfree(xprt);
2389bc25571eS\"Talpey, Thomas\ 	return ERR_PTR(-EINVAL);
2390a246b010SChuck Lever }
2391a246b010SChuck Lever 
23922881ae74STrond Myklebust static const struct rpc_timeout xs_tcp_default_timeout = {
23932881ae74STrond Myklebust 	.to_initval = 60 * HZ,
23942881ae74STrond Myklebust 	.to_maxval = 60 * HZ,
23952881ae74STrond Myklebust 	.to_retries = 2,
23962881ae74STrond Myklebust };
23972881ae74STrond Myklebust 
23989903cd1cSChuck Lever /**
23999903cd1cSChuck Lever  * xs_setup_tcp - Set up transport to use a TCP socket
240096802a09SFrank van Maarseveen  * @args: rpc transport creation arguments
24019903cd1cSChuck Lever  *
24029903cd1cSChuck Lever  */
2403483066d6SAdrian Bunk static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args)
2404a246b010SChuck Lever {
24058f9d5b1aSChuck Lever 	struct sockaddr *addr = args->dstaddr;
2406c8541ecdSChuck Lever 	struct rpc_xprt *xprt;
2407c8475461SChuck Lever 	struct sock_xprt *transport;
2408a246b010SChuck Lever 
240996802a09SFrank van Maarseveen 	xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries);
2410c8541ecdSChuck Lever 	if (IS_ERR(xprt))
2411c8541ecdSChuck Lever 		return xprt;
2412c8475461SChuck Lever 	transport = container_of(xprt, struct sock_xprt, xprt);
2413a246b010SChuck Lever 
2414ec739ef0SChuck Lever 	xprt->prot = IPPROTO_TCP;
2415808012fbSChuck Lever 	xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
2416808012fbSChuck Lever 	xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2417a246b010SChuck Lever 
241803bf4b70SChuck Lever 	xprt->bind_timeout = XS_BIND_TO;
241903bf4b70SChuck Lever 	xprt->connect_timeout = XS_TCP_CONN_TO;
242003bf4b70SChuck Lever 	xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
242103bf4b70SChuck Lever 	xprt->idle_timeout = XS_IDLE_DISC_TO;
2422a246b010SChuck Lever 
2423262965f5SChuck Lever 	xprt->ops = &xs_tcp_ops;
2424ba7392bbSTrond Myklebust 	xprt->timeout = &xs_tcp_default_timeout;
2425a246b010SChuck Lever 
24268f9d5b1aSChuck Lever 	switch (addr->sa_family) {
24278f9d5b1aSChuck Lever 	case AF_INET:
24288f9d5b1aSChuck Lever 		if (((struct sockaddr_in *)addr)->sin_port != htons(0))
24298f9d5b1aSChuck Lever 			xprt_set_bound(xprt);
24308f9d5b1aSChuck Lever 
24319dc3b095SChuck Lever 		INIT_DELAYED_WORK(&transport->connect_worker,
24329dc3b095SChuck Lever 					xs_tcp_connect_worker4);
24339dc3b095SChuck Lever 		xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP);
24348f9d5b1aSChuck Lever 		break;
24358f9d5b1aSChuck Lever 	case AF_INET6:
24368f9d5b1aSChuck Lever 		if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
24378f9d5b1aSChuck Lever 			xprt_set_bound(xprt);
24388f9d5b1aSChuck Lever 
24399dc3b095SChuck Lever 		INIT_DELAYED_WORK(&transport->connect_worker,
24409dc3b095SChuck Lever 					xs_tcp_connect_worker6);
24419dc3b095SChuck Lever 		xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6);
24428f9d5b1aSChuck Lever 		break;
24438f9d5b1aSChuck Lever 	default:
24448f9d5b1aSChuck Lever 		kfree(xprt);
24458f9d5b1aSChuck Lever 		return ERR_PTR(-EAFNOSUPPORT);
24468f9d5b1aSChuck Lever 	}
24478f9d5b1aSChuck Lever 
2448c740eff8SChuck Lever 	if (xprt_bound(xprt))
2449c740eff8SChuck Lever 		dprintk("RPC:       set up xprt to %s (port %s) via %s\n",
2450c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_ADDR],
2451c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_PORT],
2452c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_PROTO]);
2453c740eff8SChuck Lever 	else
2454c740eff8SChuck Lever 		dprintk("RPC:       set up xprt to %s (autobind) via %s\n",
2455c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_ADDR],
2456c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_PROTO]);
2457c740eff8SChuck Lever 
2458edb267a6SChuck Lever 
2459bc25571eS\"Talpey, Thomas\ 	if (try_module_get(THIS_MODULE))
2460c8541ecdSChuck Lever 		return xprt;
2461bc25571eS\"Talpey, Thomas\ 
2462bc25571eS\"Talpey, Thomas\ 	kfree(xprt->slot);
2463bc25571eS\"Talpey, Thomas\ 	kfree(xprt);
2464bc25571eS\"Talpey, Thomas\ 	return ERR_PTR(-EINVAL);
2465a246b010SChuck Lever }
2466282b32e1SChuck Lever 
2467f300babaSAlexandros Batsakis /**
2468f300babaSAlexandros Batsakis  * xs_setup_bc_tcp - Set up transport to use a TCP backchannel socket
2469f300babaSAlexandros Batsakis  * @args: rpc transport creation arguments
2470f300babaSAlexandros Batsakis  *
2471f300babaSAlexandros Batsakis  */
2472f300babaSAlexandros Batsakis static struct rpc_xprt *xs_setup_bc_tcp(struct xprt_create *args)
2473f300babaSAlexandros Batsakis {
2474f300babaSAlexandros Batsakis 	struct sockaddr *addr = args->dstaddr;
2475f300babaSAlexandros Batsakis 	struct rpc_xprt *xprt;
2476f300babaSAlexandros Batsakis 	struct sock_xprt *transport;
2477f300babaSAlexandros Batsakis 	struct svc_sock *bc_sock;
2478f300babaSAlexandros Batsakis 
2479f300babaSAlexandros Batsakis 	if (!args->bc_xprt)
2480f300babaSAlexandros Batsakis 		ERR_PTR(-EINVAL);
2481f300babaSAlexandros Batsakis 
2482f300babaSAlexandros Batsakis 	xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries);
2483f300babaSAlexandros Batsakis 	if (IS_ERR(xprt))
2484f300babaSAlexandros Batsakis 		return xprt;
2485f300babaSAlexandros Batsakis 	transport = container_of(xprt, struct sock_xprt, xprt);
2486f300babaSAlexandros Batsakis 
2487f300babaSAlexandros Batsakis 	xprt->prot = IPPROTO_TCP;
2488f300babaSAlexandros Batsakis 	xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
2489f300babaSAlexandros Batsakis 	xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2490f300babaSAlexandros Batsakis 	xprt->timeout = &xs_tcp_default_timeout;
2491f300babaSAlexandros Batsakis 
2492f300babaSAlexandros Batsakis 	/* backchannel */
2493f300babaSAlexandros Batsakis 	xprt_set_bound(xprt);
2494f300babaSAlexandros Batsakis 	xprt->bind_timeout = 0;
2495f300babaSAlexandros Batsakis 	xprt->connect_timeout = 0;
2496f300babaSAlexandros Batsakis 	xprt->reestablish_timeout = 0;
2497f300babaSAlexandros Batsakis 	xprt->idle_timeout = 0;
2498f300babaSAlexandros Batsakis 
2499f300babaSAlexandros Batsakis 	/*
2500f300babaSAlexandros Batsakis 	 * The backchannel uses the same socket connection as the
2501f300babaSAlexandros Batsakis 	 * forechannel
2502f300babaSAlexandros Batsakis 	 */
2503f300babaSAlexandros Batsakis 	xprt->bc_xprt = args->bc_xprt;
2504f300babaSAlexandros Batsakis 	bc_sock = container_of(args->bc_xprt, struct svc_sock, sk_xprt);
2505f300babaSAlexandros Batsakis 	bc_sock->sk_bc_xprt = xprt;
2506f300babaSAlexandros Batsakis 	transport->sock = bc_sock->sk_sock;
2507f300babaSAlexandros Batsakis 	transport->inet = bc_sock->sk_sk;
2508f300babaSAlexandros Batsakis 
2509f300babaSAlexandros Batsakis 	xprt->ops = &bc_tcp_ops;
2510f300babaSAlexandros Batsakis 
2511f300babaSAlexandros Batsakis 	switch (addr->sa_family) {
2512f300babaSAlexandros Batsakis 	case AF_INET:
2513f300babaSAlexandros Batsakis 		xs_format_peer_addresses(xprt, "tcp",
2514f300babaSAlexandros Batsakis 					 RPCBIND_NETID_TCP);
2515f300babaSAlexandros Batsakis 		break;
2516f300babaSAlexandros Batsakis 	case AF_INET6:
2517f300babaSAlexandros Batsakis 		xs_format_peer_addresses(xprt, "tcp",
2518f300babaSAlexandros Batsakis 				   RPCBIND_NETID_TCP6);
2519f300babaSAlexandros Batsakis 		break;
2520f300babaSAlexandros Batsakis 	default:
2521f300babaSAlexandros Batsakis 		kfree(xprt);
2522f300babaSAlexandros Batsakis 		return ERR_PTR(-EAFNOSUPPORT);
2523f300babaSAlexandros Batsakis 	}
2524f300babaSAlexandros Batsakis 
2525f300babaSAlexandros Batsakis 	if (xprt_bound(xprt))
2526f300babaSAlexandros Batsakis 		dprintk("RPC:       set up xprt to %s (port %s) via %s\n",
2527f300babaSAlexandros Batsakis 				xprt->address_strings[RPC_DISPLAY_ADDR],
2528f300babaSAlexandros Batsakis 				xprt->address_strings[RPC_DISPLAY_PORT],
2529f300babaSAlexandros Batsakis 				xprt->address_strings[RPC_DISPLAY_PROTO]);
2530f300babaSAlexandros Batsakis 	else
2531f300babaSAlexandros Batsakis 		dprintk("RPC:       set up xprt to %s (autobind) via %s\n",
2532f300babaSAlexandros Batsakis 				xprt->address_strings[RPC_DISPLAY_ADDR],
2533f300babaSAlexandros Batsakis 				xprt->address_strings[RPC_DISPLAY_PROTO]);
2534f300babaSAlexandros Batsakis 
2535f300babaSAlexandros Batsakis 	/*
2536f300babaSAlexandros Batsakis 	 * Since we don't want connections for the backchannel, we set
2537f300babaSAlexandros Batsakis 	 * the xprt status to connected
2538f300babaSAlexandros Batsakis 	 */
2539f300babaSAlexandros Batsakis 	xprt_set_connected(xprt);
2540f300babaSAlexandros Batsakis 
2541f300babaSAlexandros Batsakis 
2542f300babaSAlexandros Batsakis 	if (try_module_get(THIS_MODULE))
2543f300babaSAlexandros Batsakis 		return xprt;
2544f300babaSAlexandros Batsakis 	kfree(xprt->slot);
2545f300babaSAlexandros Batsakis 	kfree(xprt);
2546f300babaSAlexandros Batsakis 	return ERR_PTR(-EINVAL);
2547f300babaSAlexandros Batsakis }
2548f300babaSAlexandros Batsakis 
2549bc25571eS\"Talpey, Thomas\ static struct xprt_class	xs_udp_transport = {
2550bc25571eS\"Talpey, Thomas\ 	.list		= LIST_HEAD_INIT(xs_udp_transport.list),
2551bc25571eS\"Talpey, Thomas\ 	.name		= "udp",
2552bc25571eS\"Talpey, Thomas\ 	.owner		= THIS_MODULE,
2553f300babaSAlexandros Batsakis 	.ident		= XPRT_TRANSPORT_UDP,
2554bc25571eS\"Talpey, Thomas\ 	.setup		= xs_setup_udp,
2555bc25571eS\"Talpey, Thomas\ };
2556bc25571eS\"Talpey, Thomas\ 
2557bc25571eS\"Talpey, Thomas\ static struct xprt_class	xs_tcp_transport = {
2558bc25571eS\"Talpey, Thomas\ 	.list		= LIST_HEAD_INIT(xs_tcp_transport.list),
2559bc25571eS\"Talpey, Thomas\ 	.name		= "tcp",
2560bc25571eS\"Talpey, Thomas\ 	.owner		= THIS_MODULE,
2561f300babaSAlexandros Batsakis 	.ident		= XPRT_TRANSPORT_TCP,
2562bc25571eS\"Talpey, Thomas\ 	.setup		= xs_setup_tcp,
2563bc25571eS\"Talpey, Thomas\ };
2564bc25571eS\"Talpey, Thomas\ 
2565f300babaSAlexandros Batsakis static struct xprt_class	xs_bc_tcp_transport = {
2566f300babaSAlexandros Batsakis 	.list		= LIST_HEAD_INIT(xs_bc_tcp_transport.list),
2567f300babaSAlexandros Batsakis 	.name		= "tcp NFSv4.1 backchannel",
2568f300babaSAlexandros Batsakis 	.owner		= THIS_MODULE,
2569f300babaSAlexandros Batsakis 	.ident		= XPRT_TRANSPORT_BC_TCP,
2570f300babaSAlexandros Batsakis 	.setup		= xs_setup_bc_tcp,
2571f300babaSAlexandros Batsakis };
2572f300babaSAlexandros Batsakis 
2573282b32e1SChuck Lever /**
2574bc25571eS\"Talpey, Thomas\  * init_socket_xprt - set up xprtsock's sysctls, register with RPC client
2575282b32e1SChuck Lever  *
2576282b32e1SChuck Lever  */
2577282b32e1SChuck Lever int init_socket_xprt(void)
2578282b32e1SChuck Lever {
2579fbf76683SChuck Lever #ifdef RPC_DEBUG
25802b1bec5fSEric W. Biederman 	if (!sunrpc_table_header)
25810b4d4147SEric W. Biederman 		sunrpc_table_header = register_sysctl_table(sunrpc_table);
2582fbf76683SChuck Lever #endif
2583fbf76683SChuck Lever 
2584bc25571eS\"Talpey, Thomas\ 	xprt_register_transport(&xs_udp_transport);
2585bc25571eS\"Talpey, Thomas\ 	xprt_register_transport(&xs_tcp_transport);
2586f300babaSAlexandros Batsakis 	xprt_register_transport(&xs_bc_tcp_transport);
2587bc25571eS\"Talpey, Thomas\ 
2588282b32e1SChuck Lever 	return 0;
2589282b32e1SChuck Lever }
2590282b32e1SChuck Lever 
2591282b32e1SChuck Lever /**
2592bc25571eS\"Talpey, Thomas\  * cleanup_socket_xprt - remove xprtsock's sysctls, unregister
2593282b32e1SChuck Lever  *
2594282b32e1SChuck Lever  */
2595282b32e1SChuck Lever void cleanup_socket_xprt(void)
2596282b32e1SChuck Lever {
2597fbf76683SChuck Lever #ifdef RPC_DEBUG
2598fbf76683SChuck Lever 	if (sunrpc_table_header) {
2599fbf76683SChuck Lever 		unregister_sysctl_table(sunrpc_table_header);
2600fbf76683SChuck Lever 		sunrpc_table_header = NULL;
2601fbf76683SChuck Lever 	}
2602fbf76683SChuck Lever #endif
2603bc25571eS\"Talpey, Thomas\ 
2604bc25571eS\"Talpey, Thomas\ 	xprt_unregister_transport(&xs_udp_transport);
2605bc25571eS\"Talpey, Thomas\ 	xprt_unregister_transport(&xs_tcp_transport);
2606f300babaSAlexandros Batsakis 	xprt_unregister_transport(&xs_bc_tcp_transport);
2607282b32e1SChuck Lever }
2608cbf11071STrond Myklebust 
2609cbf11071STrond Myklebust static int param_set_uint_minmax(const char *val, struct kernel_param *kp,
2610cbf11071STrond Myklebust 		unsigned int min, unsigned int max)
2611cbf11071STrond Myklebust {
2612cbf11071STrond Myklebust 	unsigned long num;
2613cbf11071STrond Myklebust 	int ret;
2614cbf11071STrond Myklebust 
2615cbf11071STrond Myklebust 	if (!val)
2616cbf11071STrond Myklebust 		return -EINVAL;
2617cbf11071STrond Myklebust 	ret = strict_strtoul(val, 0, &num);
2618cbf11071STrond Myklebust 	if (ret == -EINVAL || num < min || num > max)
2619cbf11071STrond Myklebust 		return -EINVAL;
2620cbf11071STrond Myklebust 	*((unsigned int *)kp->arg) = num;
2621cbf11071STrond Myklebust 	return 0;
2622cbf11071STrond Myklebust }
2623cbf11071STrond Myklebust 
2624cbf11071STrond Myklebust static int param_set_portnr(const char *val, struct kernel_param *kp)
2625cbf11071STrond Myklebust {
2626cbf11071STrond Myklebust 	return param_set_uint_minmax(val, kp,
2627cbf11071STrond Myklebust 			RPC_MIN_RESVPORT,
2628cbf11071STrond Myklebust 			RPC_MAX_RESVPORT);
2629cbf11071STrond Myklebust }
2630cbf11071STrond Myklebust 
2631cbf11071STrond Myklebust static int param_get_portnr(char *buffer, struct kernel_param *kp)
2632cbf11071STrond Myklebust {
2633cbf11071STrond Myklebust 	return param_get_uint(buffer, kp);
2634cbf11071STrond Myklebust }
2635cbf11071STrond Myklebust #define param_check_portnr(name, p) \
2636cbf11071STrond Myklebust 	__param_check(name, p, unsigned int);
2637cbf11071STrond Myklebust 
2638cbf11071STrond Myklebust module_param_named(min_resvport, xprt_min_resvport, portnr, 0644);
2639cbf11071STrond Myklebust module_param_named(max_resvport, xprt_max_resvport, portnr, 0644);
2640cbf11071STrond Myklebust 
2641cbf11071STrond Myklebust static int param_set_slot_table_size(const char *val, struct kernel_param *kp)
2642cbf11071STrond Myklebust {
2643cbf11071STrond Myklebust 	return param_set_uint_minmax(val, kp,
2644cbf11071STrond Myklebust 			RPC_MIN_SLOT_TABLE,
2645cbf11071STrond Myklebust 			RPC_MAX_SLOT_TABLE);
2646cbf11071STrond Myklebust }
2647cbf11071STrond Myklebust 
2648cbf11071STrond Myklebust static int param_get_slot_table_size(char *buffer, struct kernel_param *kp)
2649cbf11071STrond Myklebust {
2650cbf11071STrond Myklebust 	return param_get_uint(buffer, kp);
2651cbf11071STrond Myklebust }
2652cbf11071STrond Myklebust #define param_check_slot_table_size(name, p) \
2653cbf11071STrond Myklebust 	__param_check(name, p, unsigned int);
2654cbf11071STrond Myklebust 
2655cbf11071STrond Myklebust module_param_named(tcp_slot_table_entries, xprt_tcp_slot_table_entries,
2656cbf11071STrond Myklebust 		   slot_table_size, 0644);
2657cbf11071STrond Myklebust module_param_named(udp_slot_table_entries, xprt_udp_slot_table_entries,
2658cbf11071STrond Myklebust 		   slot_table_size, 0644);
2659cbf11071STrond Myklebust 
2660