xref: /openbmc/linux/net/sunrpc/xprtsock.c (revision f064af1e)
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  * Wait duration for a reply from the RPC portmapper.
14203bf4b70SChuck Lever  */
14303bf4b70SChuck Lever #define XS_BIND_TO		(60U * HZ)
14403bf4b70SChuck Lever 
14503bf4b70SChuck Lever /*
14603bf4b70SChuck Lever  * Delay if a UDP socket connect error occurs.  This is most likely some
14703bf4b70SChuck Lever  * kind of resource problem on the local host.
14803bf4b70SChuck Lever  */
14903bf4b70SChuck Lever #define XS_UDP_REEST_TO		(2U * HZ)
15003bf4b70SChuck Lever 
15103bf4b70SChuck Lever /*
15203bf4b70SChuck Lever  * The reestablish timeout allows clients to delay for a bit before attempting
15303bf4b70SChuck Lever  * to reconnect to a server that just dropped our connection.
15403bf4b70SChuck Lever  *
15503bf4b70SChuck Lever  * We implement an exponential backoff when trying to reestablish a TCP
15603bf4b70SChuck Lever  * transport connection with the server.  Some servers like to drop a TCP
15703bf4b70SChuck Lever  * connection when they are overworked, so we start with a short timeout and
15803bf4b70SChuck Lever  * increase over time if the server is down or not responding.
15903bf4b70SChuck Lever  */
16003bf4b70SChuck Lever #define XS_TCP_INIT_REEST_TO	(3U * HZ)
16103bf4b70SChuck Lever #define XS_TCP_MAX_REEST_TO	(5U * 60 * HZ)
16203bf4b70SChuck Lever 
16303bf4b70SChuck Lever /*
16403bf4b70SChuck Lever  * TCP idle timeout; client drops the transport socket if it is idle
16503bf4b70SChuck Lever  * for this long.  Note that we also timeout UDP sockets to prevent
16603bf4b70SChuck Lever  * holding port numbers when there is no RPC traffic.
16703bf4b70SChuck Lever  */
16803bf4b70SChuck Lever #define XS_IDLE_DISC_TO		(5U * 60 * HZ)
16903bf4b70SChuck Lever 
170a246b010SChuck Lever #ifdef RPC_DEBUG
171a246b010SChuck Lever # undef  RPC_DEBUG_DATA
1729903cd1cSChuck Lever # define RPCDBG_FACILITY	RPCDBG_TRANS
173a246b010SChuck Lever #endif
174a246b010SChuck Lever 
175a246b010SChuck Lever #ifdef RPC_DEBUG_DATA
1769903cd1cSChuck Lever static void xs_pktdump(char *msg, u32 *packet, unsigned int count)
177a246b010SChuck Lever {
178a246b010SChuck Lever 	u8 *buf = (u8 *) packet;
179a246b010SChuck Lever 	int j;
180a246b010SChuck Lever 
181a246b010SChuck Lever 	dprintk("RPC:       %s\n", msg);
182a246b010SChuck Lever 	for (j = 0; j < count && j < 128; j += 4) {
183a246b010SChuck Lever 		if (!(j & 31)) {
184a246b010SChuck Lever 			if (j)
185a246b010SChuck Lever 				dprintk("\n");
186a246b010SChuck Lever 			dprintk("0x%04x ", j);
187a246b010SChuck Lever 		}
188a246b010SChuck Lever 		dprintk("%02x%02x%02x%02x ",
189a246b010SChuck Lever 			buf[j], buf[j+1], buf[j+2], buf[j+3]);
190a246b010SChuck Lever 	}
191a246b010SChuck Lever 	dprintk("\n");
192a246b010SChuck Lever }
193a246b010SChuck Lever #else
1949903cd1cSChuck Lever static inline void xs_pktdump(char *msg, u32 *packet, unsigned int count)
195a246b010SChuck Lever {
196a246b010SChuck Lever 	/* NOP */
197a246b010SChuck Lever }
198a246b010SChuck Lever #endif
199a246b010SChuck Lever 
200ffc2e518SChuck Lever struct sock_xprt {
201ffc2e518SChuck Lever 	struct rpc_xprt		xprt;
202ee0ac0c2SChuck Lever 
203ee0ac0c2SChuck Lever 	/*
204ee0ac0c2SChuck Lever 	 * Network layer
205ee0ac0c2SChuck Lever 	 */
206ee0ac0c2SChuck Lever 	struct socket *		sock;
207ee0ac0c2SChuck Lever 	struct sock *		inet;
20851971139SChuck Lever 
20951971139SChuck Lever 	/*
21051971139SChuck Lever 	 * State of TCP reply receive
21151971139SChuck Lever 	 */
21251971139SChuck Lever 	__be32			tcp_fraghdr,
213b76ce561STrond Myklebust 				tcp_xid,
214b76ce561STrond Myklebust 				tcp_calldir;
21551971139SChuck Lever 
21651971139SChuck Lever 	u32			tcp_offset,
21751971139SChuck Lever 				tcp_reclen;
21851971139SChuck Lever 
21951971139SChuck Lever 	unsigned long		tcp_copied,
22051971139SChuck Lever 				tcp_flags;
221c8475461SChuck Lever 
222c8475461SChuck Lever 	/*
223c8475461SChuck Lever 	 * Connection of transports
224c8475461SChuck Lever 	 */
22534161db6STrond Myklebust 	struct delayed_work	connect_worker;
226fbfffbd5SChuck Lever 	struct sockaddr_storage	srcaddr;
227fbfffbd5SChuck Lever 	unsigned short		srcport;
2287c6e066eSChuck Lever 
2297c6e066eSChuck Lever 	/*
2307c6e066eSChuck Lever 	 * UDP socket buffer size parameters
2317c6e066eSChuck Lever 	 */
2327c6e066eSChuck Lever 	size_t			rcvsize,
2337c6e066eSChuck Lever 				sndsize;
234314dfd79SChuck Lever 
235314dfd79SChuck Lever 	/*
236314dfd79SChuck Lever 	 * Saved socket callback addresses
237314dfd79SChuck Lever 	 */
238314dfd79SChuck Lever 	void			(*old_data_ready)(struct sock *, int);
239314dfd79SChuck Lever 	void			(*old_state_change)(struct sock *);
240314dfd79SChuck Lever 	void			(*old_write_space)(struct sock *);
2412a9e1cfaSTrond Myklebust 	void			(*old_error_report)(struct sock *);
242ffc2e518SChuck Lever };
243ffc2e518SChuck Lever 
244e136d092SChuck Lever /*
245e136d092SChuck Lever  * TCP receive state flags
246e136d092SChuck Lever  */
247e136d092SChuck Lever #define TCP_RCV_LAST_FRAG	(1UL << 0)
248e136d092SChuck Lever #define TCP_RCV_COPY_FRAGHDR	(1UL << 1)
249e136d092SChuck Lever #define TCP_RCV_COPY_XID	(1UL << 2)
250e136d092SChuck Lever #define TCP_RCV_COPY_DATA	(1UL << 3)
251f4a2e418SRicardo Labiaga #define TCP_RCV_READ_CALLDIR	(1UL << 4)
252f4a2e418SRicardo Labiaga #define TCP_RCV_COPY_CALLDIR	(1UL << 5)
25318dca02aSRicardo Labiaga 
25418dca02aSRicardo Labiaga /*
25518dca02aSRicardo Labiaga  * TCP RPC flags
25618dca02aSRicardo Labiaga  */
257f4a2e418SRicardo Labiaga #define TCP_RPC_REPLY		(1UL << 6)
258e136d092SChuck Lever 
25995392c59SChuck Lever static inline struct sockaddr *xs_addr(struct rpc_xprt *xprt)
260edb267a6SChuck Lever {
26195392c59SChuck Lever 	return (struct sockaddr *) &xprt->addr;
26295392c59SChuck Lever }
26395392c59SChuck Lever 
26495392c59SChuck Lever static inline struct sockaddr_in *xs_addr_in(struct rpc_xprt *xprt)
26595392c59SChuck Lever {
26695392c59SChuck Lever 	return (struct sockaddr_in *) &xprt->addr;
26795392c59SChuck Lever }
26895392c59SChuck Lever 
26995392c59SChuck Lever static inline struct sockaddr_in6 *xs_addr_in6(struct rpc_xprt *xprt)
27095392c59SChuck Lever {
27195392c59SChuck Lever 	return (struct sockaddr_in6 *) &xprt->addr;
27295392c59SChuck Lever }
27395392c59SChuck Lever 
274c877b849SChuck Lever static void xs_format_common_peer_addresses(struct rpc_xprt *xprt)
275c877b849SChuck Lever {
276c877b849SChuck Lever 	struct sockaddr *sap = xs_addr(xprt);
2779dc3b095SChuck Lever 	struct sockaddr_in6 *sin6;
2789dc3b095SChuck Lever 	struct sockaddr_in *sin;
279c877b849SChuck Lever 	char buf[128];
280c877b849SChuck Lever 
281c877b849SChuck Lever 	(void)rpc_ntop(sap, buf, sizeof(buf));
282c877b849SChuck Lever 	xprt->address_strings[RPC_DISPLAY_ADDR] = kstrdup(buf, GFP_KERNEL);
283c877b849SChuck Lever 
2849dc3b095SChuck Lever 	switch (sap->sa_family) {
2859dc3b095SChuck Lever 	case AF_INET:
2869dc3b095SChuck Lever 		sin = xs_addr_in(xprt);
287fc0b5791SJoe Perches 		snprintf(buf, sizeof(buf), "%08x", ntohl(sin->sin_addr.s_addr));
2889dc3b095SChuck Lever 		break;
2899dc3b095SChuck Lever 	case AF_INET6:
2909dc3b095SChuck Lever 		sin6 = xs_addr_in6(xprt);
291fc0b5791SJoe Perches 		snprintf(buf, sizeof(buf), "%pi6", &sin6->sin6_addr);
2929dc3b095SChuck Lever 		break;
2939dc3b095SChuck Lever 	default:
2949dc3b095SChuck Lever 		BUG();
2959dc3b095SChuck Lever 	}
2969dc3b095SChuck Lever 	xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = kstrdup(buf, GFP_KERNEL);
2979dc3b095SChuck Lever }
2989dc3b095SChuck Lever 
2999dc3b095SChuck Lever static void xs_format_common_peer_ports(struct rpc_xprt *xprt)
3009dc3b095SChuck Lever {
3019dc3b095SChuck Lever 	struct sockaddr *sap = xs_addr(xprt);
3029dc3b095SChuck Lever 	char buf[128];
3039dc3b095SChuck Lever 
30481160e66SJoe Perches 	snprintf(buf, sizeof(buf), "%u", rpc_get_port(sap));
305c877b849SChuck Lever 	xprt->address_strings[RPC_DISPLAY_PORT] = kstrdup(buf, GFP_KERNEL);
306c877b849SChuck Lever 
30781160e66SJoe Perches 	snprintf(buf, sizeof(buf), "%4hx", rpc_get_port(sap));
308c877b849SChuck Lever 	xprt->address_strings[RPC_DISPLAY_HEX_PORT] = kstrdup(buf, GFP_KERNEL);
309c877b849SChuck Lever }
310c877b849SChuck Lever 
3119dc3b095SChuck Lever static void xs_format_peer_addresses(struct rpc_xprt *xprt,
312b454ae90SChuck Lever 				     const char *protocol,
313b454ae90SChuck Lever 				     const char *netid)
314edb267a6SChuck Lever {
315b454ae90SChuck Lever 	xprt->address_strings[RPC_DISPLAY_PROTO] = protocol;
316b454ae90SChuck Lever 	xprt->address_strings[RPC_DISPLAY_NETID] = netid;
317c877b849SChuck Lever 	xs_format_common_peer_addresses(xprt);
3189dc3b095SChuck Lever 	xs_format_common_peer_ports(xprt);
319edb267a6SChuck Lever }
320edb267a6SChuck Lever 
3219dc3b095SChuck Lever static void xs_update_peer_port(struct rpc_xprt *xprt)
3224b6473fbSChuck Lever {
3239dc3b095SChuck Lever 	kfree(xprt->address_strings[RPC_DISPLAY_HEX_PORT]);
3249dc3b095SChuck Lever 	kfree(xprt->address_strings[RPC_DISPLAY_PORT]);
3254b6473fbSChuck Lever 
3269dc3b095SChuck Lever 	xs_format_common_peer_ports(xprt);
327edb267a6SChuck Lever }
328edb267a6SChuck Lever 
329edb267a6SChuck Lever static void xs_free_peer_addresses(struct rpc_xprt *xprt)
330edb267a6SChuck Lever {
33133e01dc7SChuck Lever 	unsigned int i;
33233e01dc7SChuck Lever 
33333e01dc7SChuck Lever 	for (i = 0; i < RPC_DISPLAY_MAX; i++)
33433e01dc7SChuck Lever 		switch (i) {
33533e01dc7SChuck Lever 		case RPC_DISPLAY_PROTO:
33633e01dc7SChuck Lever 		case RPC_DISPLAY_NETID:
33733e01dc7SChuck Lever 			continue;
33833e01dc7SChuck Lever 		default:
33933e01dc7SChuck Lever 			kfree(xprt->address_strings[i]);
34033e01dc7SChuck Lever 		}
341edb267a6SChuck Lever }
342edb267a6SChuck Lever 
343b4b5cc85SChuck Lever #define XS_SENDMSG_FLAGS	(MSG_DONTWAIT | MSG_NOSIGNAL)
344b4b5cc85SChuck Lever 
34524c5684bSTrond Myklebust static int xs_send_kvec(struct socket *sock, struct sockaddr *addr, int addrlen, struct kvec *vec, unsigned int base, int more)
346b4b5cc85SChuck Lever {
347b4b5cc85SChuck Lever 	struct msghdr msg = {
348b4b5cc85SChuck Lever 		.msg_name	= addr,
349b4b5cc85SChuck Lever 		.msg_namelen	= addrlen,
35024c5684bSTrond Myklebust 		.msg_flags	= XS_SENDMSG_FLAGS | (more ? MSG_MORE : 0),
35124c5684bSTrond Myklebust 	};
35224c5684bSTrond Myklebust 	struct kvec iov = {
35324c5684bSTrond Myklebust 		.iov_base	= vec->iov_base + base,
35424c5684bSTrond Myklebust 		.iov_len	= vec->iov_len - base,
355b4b5cc85SChuck Lever 	};
356b4b5cc85SChuck Lever 
35724c5684bSTrond Myklebust 	if (iov.iov_len != 0)
358b4b5cc85SChuck Lever 		return kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len);
359b4b5cc85SChuck Lever 	return kernel_sendmsg(sock, &msg, NULL, 0, 0);
360b4b5cc85SChuck Lever }
361b4b5cc85SChuck Lever 
36224c5684bSTrond Myklebust static int xs_send_pagedata(struct socket *sock, struct xdr_buf *xdr, unsigned int base, int more)
363b4b5cc85SChuck Lever {
36424c5684bSTrond Myklebust 	struct page **ppage;
36524c5684bSTrond Myklebust 	unsigned int remainder;
36624c5684bSTrond Myklebust 	int err, sent = 0;
367b4b5cc85SChuck Lever 
36824c5684bSTrond Myklebust 	remainder = xdr->page_len - base;
36924c5684bSTrond Myklebust 	base += xdr->page_base;
37024c5684bSTrond Myklebust 	ppage = xdr->pages + (base >> PAGE_SHIFT);
37124c5684bSTrond Myklebust 	base &= ~PAGE_MASK;
37224c5684bSTrond Myklebust 	for(;;) {
37324c5684bSTrond Myklebust 		unsigned int len = min_t(unsigned int, PAGE_SIZE - base, remainder);
37424c5684bSTrond Myklebust 		int flags = XS_SENDMSG_FLAGS;
37524c5684bSTrond Myklebust 
37624c5684bSTrond Myklebust 		remainder -= len;
37724c5684bSTrond Myklebust 		if (remainder != 0 || more)
37824c5684bSTrond Myklebust 			flags |= MSG_MORE;
37924c5684bSTrond Myklebust 		err = sock->ops->sendpage(sock, *ppage, base, len, flags);
38024c5684bSTrond Myklebust 		if (remainder == 0 || err != len)
38124c5684bSTrond Myklebust 			break;
38224c5684bSTrond Myklebust 		sent += err;
38324c5684bSTrond Myklebust 		ppage++;
38424c5684bSTrond Myklebust 		base = 0;
38524c5684bSTrond Myklebust 	}
38624c5684bSTrond Myklebust 	if (sent == 0)
38724c5684bSTrond Myklebust 		return err;
38824c5684bSTrond Myklebust 	if (err > 0)
38924c5684bSTrond Myklebust 		sent += err;
39024c5684bSTrond Myklebust 	return sent;
391b4b5cc85SChuck Lever }
392b4b5cc85SChuck Lever 
3939903cd1cSChuck Lever /**
3949903cd1cSChuck Lever  * xs_sendpages - write pages directly to a socket
3959903cd1cSChuck Lever  * @sock: socket to send on
3969903cd1cSChuck Lever  * @addr: UDP only -- address of destination
3979903cd1cSChuck Lever  * @addrlen: UDP only -- length of destination address
3989903cd1cSChuck Lever  * @xdr: buffer containing this request
3999903cd1cSChuck Lever  * @base: starting position in the buffer
4009903cd1cSChuck Lever  *
401a246b010SChuck Lever  */
40224c5684bSTrond Myklebust static int xs_sendpages(struct socket *sock, struct sockaddr *addr, int addrlen, struct xdr_buf *xdr, unsigned int base)
403a246b010SChuck Lever {
40424c5684bSTrond Myklebust 	unsigned int remainder = xdr->len - base;
40524c5684bSTrond Myklebust 	int err, sent = 0;
406a246b010SChuck Lever 
407262965f5SChuck Lever 	if (unlikely(!sock))
408fba91afbSTrond Myklebust 		return -ENOTSOCK;
409262965f5SChuck Lever 
410262965f5SChuck Lever 	clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags);
41124c5684bSTrond Myklebust 	if (base != 0) {
41224c5684bSTrond Myklebust 		addr = NULL;
41324c5684bSTrond Myklebust 		addrlen = 0;
41424c5684bSTrond Myklebust 	}
415262965f5SChuck Lever 
41624c5684bSTrond Myklebust 	if (base < xdr->head[0].iov_len || addr != NULL) {
41724c5684bSTrond Myklebust 		unsigned int len = xdr->head[0].iov_len - base;
41824c5684bSTrond Myklebust 		remainder -= len;
41924c5684bSTrond Myklebust 		err = xs_send_kvec(sock, addr, addrlen, &xdr->head[0], base, remainder != 0);
42024c5684bSTrond Myklebust 		if (remainder == 0 || err != len)
421a246b010SChuck Lever 			goto out;
42224c5684bSTrond Myklebust 		sent += err;
423a246b010SChuck Lever 		base = 0;
424a246b010SChuck Lever 	} else
42524c5684bSTrond Myklebust 		base -= xdr->head[0].iov_len;
426a246b010SChuck Lever 
42724c5684bSTrond Myklebust 	if (base < xdr->page_len) {
42824c5684bSTrond Myklebust 		unsigned int len = xdr->page_len - base;
42924c5684bSTrond Myklebust 		remainder -= len;
43024c5684bSTrond Myklebust 		err = xs_send_pagedata(sock, xdr, base, remainder != 0);
43124c5684bSTrond Myklebust 		if (remainder == 0 || err != len)
432a246b010SChuck Lever 			goto out;
43324c5684bSTrond Myklebust 		sent += err;
434a246b010SChuck Lever 		base = 0;
43524c5684bSTrond Myklebust 	} else
43624c5684bSTrond Myklebust 		base -= xdr->page_len;
43724c5684bSTrond Myklebust 
43824c5684bSTrond Myklebust 	if (base >= xdr->tail[0].iov_len)
43924c5684bSTrond Myklebust 		return sent;
44024c5684bSTrond Myklebust 	err = xs_send_kvec(sock, NULL, 0, &xdr->tail[0], base, 0);
441a246b010SChuck Lever out:
44224c5684bSTrond Myklebust 	if (sent == 0)
44324c5684bSTrond Myklebust 		return err;
44424c5684bSTrond Myklebust 	if (err > 0)
44524c5684bSTrond Myklebust 		sent += err;
44624c5684bSTrond Myklebust 	return sent;
447a246b010SChuck Lever }
448a246b010SChuck Lever 
449b6ddf64fSTrond Myklebust static void xs_nospace_callback(struct rpc_task *task)
450b6ddf64fSTrond Myklebust {
451b6ddf64fSTrond Myklebust 	struct sock_xprt *transport = container_of(task->tk_rqstp->rq_xprt, struct sock_xprt, xprt);
452b6ddf64fSTrond Myklebust 
453b6ddf64fSTrond Myklebust 	transport->inet->sk_write_pending--;
454b6ddf64fSTrond Myklebust 	clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
455b6ddf64fSTrond Myklebust }
456b6ddf64fSTrond Myklebust 
4579903cd1cSChuck Lever /**
458262965f5SChuck Lever  * xs_nospace - place task on wait queue if transmit was incomplete
459262965f5SChuck Lever  * @task: task to put to sleep
4609903cd1cSChuck Lever  *
461a246b010SChuck Lever  */
4625e3771ceSTrond Myklebust static int xs_nospace(struct rpc_task *task)
463a246b010SChuck Lever {
464262965f5SChuck Lever 	struct rpc_rqst *req = task->tk_rqstp;
465262965f5SChuck Lever 	struct rpc_xprt *xprt = req->rq_xprt;
466ee0ac0c2SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
4675e3771ceSTrond Myklebust 	int ret = 0;
468a246b010SChuck Lever 
46946121cf7SChuck Lever 	dprintk("RPC: %5u xmit incomplete (%u left of %u)\n",
470262965f5SChuck Lever 			task->tk_pid, req->rq_slen - req->rq_bytes_sent,
471262965f5SChuck Lever 			req->rq_slen);
472a246b010SChuck Lever 
473262965f5SChuck Lever 	/* Protect against races with write_space */
474262965f5SChuck Lever 	spin_lock_bh(&xprt->transport_lock);
475a246b010SChuck Lever 
476262965f5SChuck Lever 	/* Don't race with disconnect */
477b6ddf64fSTrond Myklebust 	if (xprt_connected(xprt)) {
478b6ddf64fSTrond Myklebust 		if (test_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags)) {
4795e3771ceSTrond Myklebust 			ret = -EAGAIN;
480b6ddf64fSTrond Myklebust 			/*
481b6ddf64fSTrond Myklebust 			 * Notify TCP that we're limited by the application
482b6ddf64fSTrond Myklebust 			 * window size
483b6ddf64fSTrond Myklebust 			 */
484b6ddf64fSTrond Myklebust 			set_bit(SOCK_NOSPACE, &transport->sock->flags);
485b6ddf64fSTrond Myklebust 			transport->inet->sk_write_pending++;
486b6ddf64fSTrond Myklebust 			/* ...and wait for more buffer space */
487b6ddf64fSTrond Myklebust 			xprt_wait_for_buffer_space(task, xs_nospace_callback);
488b6ddf64fSTrond Myklebust 		}
489b6ddf64fSTrond Myklebust 	} else {
490b6ddf64fSTrond Myklebust 		clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
4915e3771ceSTrond Myklebust 		ret = -ENOTCONN;
492b6ddf64fSTrond Myklebust 	}
493a246b010SChuck Lever 
494262965f5SChuck Lever 	spin_unlock_bh(&xprt->transport_lock);
4955e3771ceSTrond Myklebust 	return ret;
496a246b010SChuck Lever }
497a246b010SChuck Lever 
4989903cd1cSChuck Lever /**
499262965f5SChuck Lever  * xs_udp_send_request - write an RPC request to a UDP socket
5009903cd1cSChuck Lever  * @task: address of RPC task that manages the state of an RPC request
5019903cd1cSChuck Lever  *
5029903cd1cSChuck Lever  * Return values:
5039903cd1cSChuck Lever  *        0:	The request has been sent
5049903cd1cSChuck Lever  *   EAGAIN:	The socket was blocked, please call again later to
5059903cd1cSChuck Lever  *		complete the request
506262965f5SChuck Lever  * ENOTCONN:	Caller needs to invoke connect logic then call again
5079903cd1cSChuck Lever  *    other:	Some other error occured, the request was not sent
5089903cd1cSChuck Lever  */
509262965f5SChuck Lever static int xs_udp_send_request(struct rpc_task *task)
510a246b010SChuck Lever {
511a246b010SChuck Lever 	struct rpc_rqst *req = task->tk_rqstp;
512a246b010SChuck Lever 	struct rpc_xprt *xprt = req->rq_xprt;
513ee0ac0c2SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
514262965f5SChuck Lever 	struct xdr_buf *xdr = &req->rq_snd_buf;
515262965f5SChuck Lever 	int status;
516262965f5SChuck Lever 
517262965f5SChuck Lever 	xs_pktdump("packet data:",
518262965f5SChuck Lever 				req->rq_svec->iov_base,
519262965f5SChuck Lever 				req->rq_svec->iov_len);
520262965f5SChuck Lever 
52101d37c42STrond Myklebust 	if (!xprt_bound(xprt))
52201d37c42STrond Myklebust 		return -ENOTCONN;
523ee0ac0c2SChuck Lever 	status = xs_sendpages(transport->sock,
52495392c59SChuck Lever 			      xs_addr(xprt),
525ee0ac0c2SChuck Lever 			      xprt->addrlen, xdr,
526ee0ac0c2SChuck Lever 			      req->rq_bytes_sent);
527262965f5SChuck Lever 
528262965f5SChuck Lever 	dprintk("RPC:       xs_udp_send_request(%u) = %d\n",
529262965f5SChuck Lever 			xdr->len - req->rq_bytes_sent, status);
530262965f5SChuck Lever 
5312199700fSTrond Myklebust 	if (status >= 0) {
532d60dbb20STrond Myklebust 		req->rq_xmit_bytes_sent += status;
5332199700fSTrond Myklebust 		if (status >= req->rq_slen)
534262965f5SChuck Lever 			return 0;
535262965f5SChuck Lever 		/* Still some bytes left; set up for a retry later. */
536262965f5SChuck Lever 		status = -EAGAIN;
5372199700fSTrond Myklebust 	}
538262965f5SChuck Lever 
539262965f5SChuck Lever 	switch (status) {
540fba91afbSTrond Myklebust 	case -ENOTSOCK:
541fba91afbSTrond Myklebust 		status = -ENOTCONN;
542fba91afbSTrond Myklebust 		/* Should we call xs_close() here? */
543fba91afbSTrond Myklebust 		break;
544b6ddf64fSTrond Myklebust 	case -EAGAIN:
5455e3771ceSTrond Myklebust 		status = xs_nospace(task);
546b6ddf64fSTrond Myklebust 		break;
547c8485e4dSTrond Myklebust 	default:
548c8485e4dSTrond Myklebust 		dprintk("RPC:       sendmsg returned unrecognized error %d\n",
549c8485e4dSTrond Myklebust 			-status);
550262965f5SChuck Lever 	case -ENETUNREACH:
551262965f5SChuck Lever 	case -EPIPE:
552262965f5SChuck Lever 	case -ECONNREFUSED:
553262965f5SChuck Lever 		/* When the server has died, an ICMP port unreachable message
554262965f5SChuck Lever 		 * prompts ECONNREFUSED. */
555b6ddf64fSTrond Myklebust 		clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
556262965f5SChuck Lever 	}
5575fe46e9dSBian Naimeng 
558262965f5SChuck Lever 	return status;
559262965f5SChuck Lever }
560262965f5SChuck Lever 
561e06799f9STrond Myklebust /**
562e06799f9STrond Myklebust  * xs_tcp_shutdown - gracefully shut down a TCP socket
563e06799f9STrond Myklebust  * @xprt: transport
564e06799f9STrond Myklebust  *
565e06799f9STrond Myklebust  * Initiates a graceful shutdown of the TCP socket by calling the
566e06799f9STrond Myklebust  * equivalent of shutdown(SHUT_WR);
567e06799f9STrond Myklebust  */
568e06799f9STrond Myklebust static void xs_tcp_shutdown(struct rpc_xprt *xprt)
569e06799f9STrond Myklebust {
570e06799f9STrond Myklebust 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
571e06799f9STrond Myklebust 	struct socket *sock = transport->sock;
572e06799f9STrond Myklebust 
573e06799f9STrond Myklebust 	if (sock != NULL)
574e06799f9STrond Myklebust 		kernel_sock_shutdown(sock, SHUT_WR);
575e06799f9STrond Myklebust }
576e06799f9STrond Myklebust 
577808012fbSChuck Lever static inline void xs_encode_tcp_record_marker(struct xdr_buf *buf)
578808012fbSChuck Lever {
579808012fbSChuck Lever 	u32 reclen = buf->len - sizeof(rpc_fraghdr);
580808012fbSChuck Lever 	rpc_fraghdr *base = buf->head[0].iov_base;
581808012fbSChuck Lever 	*base = htonl(RPC_LAST_STREAM_FRAGMENT | reclen);
582808012fbSChuck Lever }
583808012fbSChuck Lever 
584262965f5SChuck Lever /**
585262965f5SChuck Lever  * xs_tcp_send_request - write an RPC request to a TCP socket
586262965f5SChuck Lever  * @task: address of RPC task that manages the state of an RPC request
587262965f5SChuck Lever  *
588262965f5SChuck Lever  * Return values:
589262965f5SChuck Lever  *        0:	The request has been sent
590262965f5SChuck Lever  *   EAGAIN:	The socket was blocked, please call again later to
591262965f5SChuck Lever  *		complete the request
592262965f5SChuck Lever  * ENOTCONN:	Caller needs to invoke connect logic then call again
593262965f5SChuck Lever  *    other:	Some other error occured, the request was not sent
594262965f5SChuck Lever  *
595262965f5SChuck Lever  * XXX: In the case of soft timeouts, should we eventually give up
596262965f5SChuck Lever  *	if sendmsg is not able to make progress?
597262965f5SChuck Lever  */
598262965f5SChuck Lever static int xs_tcp_send_request(struct rpc_task *task)
599262965f5SChuck Lever {
600262965f5SChuck Lever 	struct rpc_rqst *req = task->tk_rqstp;
601262965f5SChuck Lever 	struct rpc_xprt *xprt = req->rq_xprt;
602ee0ac0c2SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
603262965f5SChuck Lever 	struct xdr_buf *xdr = &req->rq_snd_buf;
604b595bb15SChuck Lever 	int status;
605a246b010SChuck Lever 
606808012fbSChuck Lever 	xs_encode_tcp_record_marker(&req->rq_snd_buf);
607262965f5SChuck Lever 
608262965f5SChuck Lever 	xs_pktdump("packet data:",
609262965f5SChuck Lever 				req->rq_svec->iov_base,
610262965f5SChuck Lever 				req->rq_svec->iov_len);
611a246b010SChuck Lever 
612a246b010SChuck Lever 	/* Continue transmitting the packet/record. We must be careful
613a246b010SChuck Lever 	 * to cope with writespace callbacks arriving _after_ we have
614262965f5SChuck Lever 	 * called sendmsg(). */
615a246b010SChuck Lever 	while (1) {
616ee0ac0c2SChuck Lever 		status = xs_sendpages(transport->sock,
617ee0ac0c2SChuck Lever 					NULL, 0, xdr, req->rq_bytes_sent);
618a246b010SChuck Lever 
619262965f5SChuck Lever 		dprintk("RPC:       xs_tcp_send_request(%u) = %d\n",
620262965f5SChuck Lever 				xdr->len - req->rq_bytes_sent, status);
621262965f5SChuck Lever 
622262965f5SChuck Lever 		if (unlikely(status < 0))
623a246b010SChuck Lever 			break;
624a246b010SChuck Lever 
625a246b010SChuck Lever 		/* If we've sent the entire packet, immediately
626a246b010SChuck Lever 		 * reset the count of bytes sent. */
627262965f5SChuck Lever 		req->rq_bytes_sent += status;
628d60dbb20STrond Myklebust 		req->rq_xmit_bytes_sent += status;
629262965f5SChuck Lever 		if (likely(req->rq_bytes_sent >= req->rq_slen)) {
630a246b010SChuck Lever 			req->rq_bytes_sent = 0;
631a246b010SChuck Lever 			return 0;
632a246b010SChuck Lever 		}
633262965f5SChuck Lever 
63406b4b681STrond Myklebust 		if (status != 0)
63506b4b681STrond Myklebust 			continue;
636a246b010SChuck Lever 		status = -EAGAIN;
637a246b010SChuck Lever 		break;
638a246b010SChuck Lever 	}
639a246b010SChuck Lever 
640262965f5SChuck Lever 	switch (status) {
641fba91afbSTrond Myklebust 	case -ENOTSOCK:
642fba91afbSTrond Myklebust 		status = -ENOTCONN;
643fba91afbSTrond Myklebust 		/* Should we call xs_close() here? */
644fba91afbSTrond Myklebust 		break;
645262965f5SChuck Lever 	case -EAGAIN:
6465e3771ceSTrond Myklebust 		status = xs_nospace(task);
647262965f5SChuck Lever 		break;
648262965f5SChuck Lever 	default:
649262965f5SChuck Lever 		dprintk("RPC:       sendmsg returned unrecognized error %d\n",
650262965f5SChuck Lever 			-status);
651a246b010SChuck Lever 	case -ECONNRESET:
65255420c24STrond Myklebust 	case -EPIPE:
653e06799f9STrond Myklebust 		xs_tcp_shutdown(xprt);
654a246b010SChuck Lever 	case -ECONNREFUSED:
655a246b010SChuck Lever 	case -ENOTCONN:
656a246b010SChuck Lever 		clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
657a246b010SChuck Lever 	}
6585fe46e9dSBian Naimeng 
659a246b010SChuck Lever 	return status;
660a246b010SChuck Lever }
661a246b010SChuck Lever 
6629903cd1cSChuck Lever /**
663e0ab53deSTrond Myklebust  * xs_tcp_release_xprt - clean up after a tcp transmission
664e0ab53deSTrond Myklebust  * @xprt: transport
665e0ab53deSTrond Myklebust  * @task: rpc task
666e0ab53deSTrond Myklebust  *
667e0ab53deSTrond Myklebust  * This cleans up if an error causes us to abort the transmission of a request.
668e0ab53deSTrond Myklebust  * In this case, the socket may need to be reset in order to avoid confusing
669e0ab53deSTrond Myklebust  * the server.
670e0ab53deSTrond Myklebust  */
671e0ab53deSTrond Myklebust static void xs_tcp_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
672e0ab53deSTrond Myklebust {
673e0ab53deSTrond Myklebust 	struct rpc_rqst *req;
674e0ab53deSTrond Myklebust 
675e0ab53deSTrond Myklebust 	if (task != xprt->snd_task)
676e0ab53deSTrond Myklebust 		return;
677e0ab53deSTrond Myklebust 	if (task == NULL)
678e0ab53deSTrond Myklebust 		goto out_release;
679e0ab53deSTrond Myklebust 	req = task->tk_rqstp;
680e0ab53deSTrond Myklebust 	if (req->rq_bytes_sent == 0)
681e0ab53deSTrond Myklebust 		goto out_release;
682e0ab53deSTrond Myklebust 	if (req->rq_bytes_sent == req->rq_snd_buf.len)
683e0ab53deSTrond Myklebust 		goto out_release;
684e0ab53deSTrond Myklebust 	set_bit(XPRT_CLOSE_WAIT, &task->tk_xprt->state);
685e0ab53deSTrond Myklebust out_release:
686e0ab53deSTrond Myklebust 	xprt_release_xprt(xprt, task);
687e0ab53deSTrond Myklebust }
688e0ab53deSTrond Myklebust 
6892a9e1cfaSTrond Myklebust static void xs_save_old_callbacks(struct sock_xprt *transport, struct sock *sk)
6902a9e1cfaSTrond Myklebust {
6912a9e1cfaSTrond Myklebust 	transport->old_data_ready = sk->sk_data_ready;
6922a9e1cfaSTrond Myklebust 	transport->old_state_change = sk->sk_state_change;
6932a9e1cfaSTrond Myklebust 	transport->old_write_space = sk->sk_write_space;
6942a9e1cfaSTrond Myklebust 	transport->old_error_report = sk->sk_error_report;
6952a9e1cfaSTrond Myklebust }
6962a9e1cfaSTrond Myklebust 
6972a9e1cfaSTrond Myklebust static void xs_restore_old_callbacks(struct sock_xprt *transport, struct sock *sk)
6982a9e1cfaSTrond Myklebust {
6992a9e1cfaSTrond Myklebust 	sk->sk_data_ready = transport->old_data_ready;
7002a9e1cfaSTrond Myklebust 	sk->sk_state_change = transport->old_state_change;
7012a9e1cfaSTrond Myklebust 	sk->sk_write_space = transport->old_write_space;
7022a9e1cfaSTrond Myklebust 	sk->sk_error_report = transport->old_error_report;
7032a9e1cfaSTrond Myklebust }
7042a9e1cfaSTrond Myklebust 
705fe315e76SChuck Lever static void xs_reset_transport(struct sock_xprt *transport)
706a246b010SChuck Lever {
707ee0ac0c2SChuck Lever 	struct socket *sock = transport->sock;
708ee0ac0c2SChuck Lever 	struct sock *sk = transport->inet;
709a246b010SChuck Lever 
710fe315e76SChuck Lever 	if (sk == NULL)
711fe315e76SChuck Lever 		return;
7129903cd1cSChuck Lever 
713a246b010SChuck Lever 	write_lock_bh(&sk->sk_callback_lock);
714ee0ac0c2SChuck Lever 	transport->inet = NULL;
715ee0ac0c2SChuck Lever 	transport->sock = NULL;
716a246b010SChuck Lever 
717a246b010SChuck Lever 	sk->sk_user_data = NULL;
7182a9e1cfaSTrond Myklebust 
7192a9e1cfaSTrond Myklebust 	xs_restore_old_callbacks(transport, sk);
720a246b010SChuck Lever 	write_unlock_bh(&sk->sk_callback_lock);
721a246b010SChuck Lever 
722a246b010SChuck Lever 	sk->sk_no_check = 0;
723a246b010SChuck Lever 
724a246b010SChuck Lever 	sock_release(sock);
725fe315e76SChuck Lever }
726fe315e76SChuck Lever 
727fe315e76SChuck Lever /**
728fe315e76SChuck Lever  * xs_close - close a socket
729fe315e76SChuck Lever  * @xprt: transport
730fe315e76SChuck Lever  *
731fe315e76SChuck Lever  * This is used when all requests are complete; ie, no DRC state remains
732fe315e76SChuck Lever  * on the server we want to save.
733f75e6745STrond Myklebust  *
734f75e6745STrond Myklebust  * The caller _must_ be holding XPRT_LOCKED in order to avoid issues with
735f75e6745STrond Myklebust  * xs_reset_transport() zeroing the socket from underneath a writer.
736fe315e76SChuck Lever  */
737fe315e76SChuck Lever static void xs_close(struct rpc_xprt *xprt)
738fe315e76SChuck Lever {
739fe315e76SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
740fe315e76SChuck Lever 
741fe315e76SChuck Lever 	dprintk("RPC:       xs_close xprt %p\n", xprt);
742fe315e76SChuck Lever 
743fe315e76SChuck Lever 	xs_reset_transport(transport);
74461d0a8e6SNeil Brown 	xprt->reestablish_timeout = 0;
745fe315e76SChuck Lever 
746632e3bdcSTrond Myklebust 	smp_mb__before_clear_bit();
7477d1e8255STrond Myklebust 	clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
748632e3bdcSTrond Myklebust 	clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
7493b948ae5STrond Myklebust 	clear_bit(XPRT_CLOSING, &xprt->state);
750632e3bdcSTrond Myklebust 	smp_mb__after_clear_bit();
75162da3b24STrond Myklebust 	xprt_disconnect_done(xprt);
752a246b010SChuck Lever }
753a246b010SChuck Lever 
754f75e6745STrond Myklebust static void xs_tcp_close(struct rpc_xprt *xprt)
755f75e6745STrond Myklebust {
756f75e6745STrond Myklebust 	if (test_and_clear_bit(XPRT_CONNECTION_CLOSE, &xprt->state))
757f75e6745STrond Myklebust 		xs_close(xprt);
758f75e6745STrond Myklebust 	else
759f75e6745STrond Myklebust 		xs_tcp_shutdown(xprt);
760f75e6745STrond Myklebust }
761f75e6745STrond Myklebust 
7629903cd1cSChuck Lever /**
7639903cd1cSChuck Lever  * xs_destroy - prepare to shutdown a transport
7649903cd1cSChuck Lever  * @xprt: doomed transport
7659903cd1cSChuck Lever  *
7669903cd1cSChuck Lever  */
7679903cd1cSChuck Lever static void xs_destroy(struct rpc_xprt *xprt)
768a246b010SChuck Lever {
769c8475461SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
770c8475461SChuck Lever 
7719903cd1cSChuck Lever 	dprintk("RPC:       xs_destroy xprt %p\n", xprt);
7729903cd1cSChuck Lever 
773c1384c9cSTrond Myklebust 	cancel_rearming_delayed_work(&transport->connect_worker);
774a246b010SChuck Lever 
7759903cd1cSChuck Lever 	xs_close(xprt);
776edb267a6SChuck Lever 	xs_free_peer_addresses(xprt);
777a246b010SChuck Lever 	kfree(xprt->slot);
778c8541ecdSChuck Lever 	kfree(xprt);
779bc25571eS\"Talpey, Thomas\ 	module_put(THIS_MODULE);
780a246b010SChuck Lever }
781a246b010SChuck Lever 
7829903cd1cSChuck Lever static inline struct rpc_xprt *xprt_from_sock(struct sock *sk)
7839903cd1cSChuck Lever {
7849903cd1cSChuck Lever 	return (struct rpc_xprt *) sk->sk_user_data;
7859903cd1cSChuck Lever }
7869903cd1cSChuck Lever 
7879903cd1cSChuck Lever /**
7889903cd1cSChuck Lever  * xs_udp_data_ready - "data ready" callback for UDP sockets
7899903cd1cSChuck Lever  * @sk: socket with data to read
7909903cd1cSChuck Lever  * @len: how much data to read
7919903cd1cSChuck Lever  *
792a246b010SChuck Lever  */
7939903cd1cSChuck Lever static void xs_udp_data_ready(struct sock *sk, int len)
794a246b010SChuck Lever {
795a246b010SChuck Lever 	struct rpc_task *task;
796a246b010SChuck Lever 	struct rpc_xprt *xprt;
797a246b010SChuck Lever 	struct rpc_rqst *rovr;
798a246b010SChuck Lever 	struct sk_buff *skb;
799a246b010SChuck Lever 	int err, repsize, copied;
800d8ed029dSAlexey Dobriyan 	u32 _xid;
801d8ed029dSAlexey Dobriyan 	__be32 *xp;
802a246b010SChuck Lever 
803f064af1eSEric Dumazet 	read_lock_bh(&sk->sk_callback_lock);
8049903cd1cSChuck Lever 	dprintk("RPC:       xs_udp_data_ready...\n");
8059903cd1cSChuck Lever 	if (!(xprt = xprt_from_sock(sk)))
806a246b010SChuck Lever 		goto out;
807a246b010SChuck Lever 
808a246b010SChuck Lever 	if ((skb = skb_recv_datagram(sk, 0, 1, &err)) == NULL)
809a246b010SChuck Lever 		goto out;
810a246b010SChuck Lever 
811a246b010SChuck Lever 	if (xprt->shutdown)
812a246b010SChuck Lever 		goto dropit;
813a246b010SChuck Lever 
814a246b010SChuck Lever 	repsize = skb->len - sizeof(struct udphdr);
815a246b010SChuck Lever 	if (repsize < 4) {
8169903cd1cSChuck Lever 		dprintk("RPC:       impossible RPC reply size %d!\n", repsize);
817a246b010SChuck Lever 		goto dropit;
818a246b010SChuck Lever 	}
819a246b010SChuck Lever 
820a246b010SChuck Lever 	/* Copy the XID from the skb... */
821a246b010SChuck Lever 	xp = skb_header_pointer(skb, sizeof(struct udphdr),
822a246b010SChuck Lever 				sizeof(_xid), &_xid);
823a246b010SChuck Lever 	if (xp == NULL)
824a246b010SChuck Lever 		goto dropit;
825a246b010SChuck Lever 
826a246b010SChuck Lever 	/* Look up and lock the request corresponding to the given XID */
8274a0f8c04SChuck Lever 	spin_lock(&xprt->transport_lock);
828a246b010SChuck Lever 	rovr = xprt_lookup_rqst(xprt, *xp);
829a246b010SChuck Lever 	if (!rovr)
830a246b010SChuck Lever 		goto out_unlock;
831a246b010SChuck Lever 	task = rovr->rq_task;
832a246b010SChuck Lever 
833a246b010SChuck Lever 	if ((copied = rovr->rq_private_buf.buflen) > repsize)
834a246b010SChuck Lever 		copied = repsize;
835a246b010SChuck Lever 
836a246b010SChuck Lever 	/* Suck it into the iovec, verify checksum if not done by hw. */
8371781f7f5SHerbert Xu 	if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) {
8381781f7f5SHerbert Xu 		UDPX_INC_STATS_BH(sk, UDP_MIB_INERRORS);
839a246b010SChuck Lever 		goto out_unlock;
8401781f7f5SHerbert Xu 	}
8411781f7f5SHerbert Xu 
8421781f7f5SHerbert Xu 	UDPX_INC_STATS_BH(sk, UDP_MIB_INDATAGRAMS);
843a246b010SChuck Lever 
844a246b010SChuck Lever 	/* Something worked... */
845adf30907SEric Dumazet 	dst_confirm(skb_dst(skb));
846a246b010SChuck Lever 
8471570c1e4SChuck Lever 	xprt_adjust_cwnd(task, copied);
8481570c1e4SChuck Lever 	xprt_complete_rqst(task, copied);
849a246b010SChuck Lever 
850a246b010SChuck Lever  out_unlock:
8514a0f8c04SChuck Lever 	spin_unlock(&xprt->transport_lock);
852a246b010SChuck Lever  dropit:
853a246b010SChuck Lever 	skb_free_datagram(sk, skb);
854a246b010SChuck Lever  out:
855f064af1eSEric Dumazet 	read_unlock_bh(&sk->sk_callback_lock);
856a246b010SChuck Lever }
857a246b010SChuck Lever 
858dd456471SChuck Lever static inline void xs_tcp_read_fraghdr(struct rpc_xprt *xprt, struct xdr_skb_reader *desc)
859a246b010SChuck Lever {
86051971139SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
861a246b010SChuck Lever 	size_t len, used;
862a246b010SChuck Lever 	char *p;
863a246b010SChuck Lever 
86451971139SChuck Lever 	p = ((char *) &transport->tcp_fraghdr) + transport->tcp_offset;
86551971139SChuck Lever 	len = sizeof(transport->tcp_fraghdr) - transport->tcp_offset;
8669d292316SChuck Lever 	used = xdr_skb_read_bits(desc, p, len);
86751971139SChuck Lever 	transport->tcp_offset += used;
868a246b010SChuck Lever 	if (used != len)
869a246b010SChuck Lever 		return;
870808012fbSChuck Lever 
87151971139SChuck Lever 	transport->tcp_reclen = ntohl(transport->tcp_fraghdr);
87251971139SChuck Lever 	if (transport->tcp_reclen & RPC_LAST_STREAM_FRAGMENT)
873e136d092SChuck Lever 		transport->tcp_flags |= TCP_RCV_LAST_FRAG;
874a246b010SChuck Lever 	else
875e136d092SChuck Lever 		transport->tcp_flags &= ~TCP_RCV_LAST_FRAG;
87651971139SChuck Lever 	transport->tcp_reclen &= RPC_FRAGMENT_SIZE_MASK;
877808012fbSChuck Lever 
878e136d092SChuck Lever 	transport->tcp_flags &= ~TCP_RCV_COPY_FRAGHDR;
87951971139SChuck Lever 	transport->tcp_offset = 0;
880808012fbSChuck Lever 
881a246b010SChuck Lever 	/* Sanity check of the record length */
88218dca02aSRicardo Labiaga 	if (unlikely(transport->tcp_reclen < 8)) {
8839903cd1cSChuck Lever 		dprintk("RPC:       invalid TCP record fragment length\n");
8843ebb067dSTrond Myklebust 		xprt_force_disconnect(xprt);
8859903cd1cSChuck Lever 		return;
886a246b010SChuck Lever 	}
887a246b010SChuck Lever 	dprintk("RPC:       reading TCP record fragment of length %d\n",
88851971139SChuck Lever 			transport->tcp_reclen);
889a246b010SChuck Lever }
890a246b010SChuck Lever 
89151971139SChuck Lever static void xs_tcp_check_fraghdr(struct sock_xprt *transport)
892a246b010SChuck Lever {
89351971139SChuck Lever 	if (transport->tcp_offset == transport->tcp_reclen) {
894e136d092SChuck Lever 		transport->tcp_flags |= TCP_RCV_COPY_FRAGHDR;
89551971139SChuck Lever 		transport->tcp_offset = 0;
896e136d092SChuck Lever 		if (transport->tcp_flags & TCP_RCV_LAST_FRAG) {
897e136d092SChuck Lever 			transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
898e136d092SChuck Lever 			transport->tcp_flags |= TCP_RCV_COPY_XID;
89951971139SChuck Lever 			transport->tcp_copied = 0;
900a246b010SChuck Lever 		}
901a246b010SChuck Lever 	}
902a246b010SChuck Lever }
903a246b010SChuck Lever 
904dd456471SChuck Lever static inline void xs_tcp_read_xid(struct sock_xprt *transport, struct xdr_skb_reader *desc)
905a246b010SChuck Lever {
906a246b010SChuck Lever 	size_t len, used;
907a246b010SChuck Lever 	char *p;
908a246b010SChuck Lever 
90951971139SChuck Lever 	len = sizeof(transport->tcp_xid) - transport->tcp_offset;
910a246b010SChuck Lever 	dprintk("RPC:       reading XID (%Zu bytes)\n", len);
91151971139SChuck Lever 	p = ((char *) &transport->tcp_xid) + transport->tcp_offset;
9129d292316SChuck Lever 	used = xdr_skb_read_bits(desc, p, len);
91351971139SChuck Lever 	transport->tcp_offset += used;
914a246b010SChuck Lever 	if (used != len)
915a246b010SChuck Lever 		return;
916e136d092SChuck Lever 	transport->tcp_flags &= ~TCP_RCV_COPY_XID;
917f4a2e418SRicardo Labiaga 	transport->tcp_flags |= TCP_RCV_READ_CALLDIR;
91851971139SChuck Lever 	transport->tcp_copied = 4;
91918dca02aSRicardo Labiaga 	dprintk("RPC:       reading %s XID %08x\n",
92018dca02aSRicardo Labiaga 			(transport->tcp_flags & TCP_RPC_REPLY) ? "reply for"
92118dca02aSRicardo Labiaga 							      : "request with",
92251971139SChuck Lever 			ntohl(transport->tcp_xid));
92351971139SChuck Lever 	xs_tcp_check_fraghdr(transport);
924a246b010SChuck Lever }
925a246b010SChuck Lever 
92618dca02aSRicardo Labiaga static inline void xs_tcp_read_calldir(struct sock_xprt *transport,
92718dca02aSRicardo Labiaga 				       struct xdr_skb_reader *desc)
928a246b010SChuck Lever {
92918dca02aSRicardo Labiaga 	size_t len, used;
93018dca02aSRicardo Labiaga 	u32 offset;
931b76ce561STrond Myklebust 	char *p;
93218dca02aSRicardo Labiaga 
93318dca02aSRicardo Labiaga 	/*
93418dca02aSRicardo Labiaga 	 * We want transport->tcp_offset to be 8 at the end of this routine
93518dca02aSRicardo Labiaga 	 * (4 bytes for the xid and 4 bytes for the call/reply flag).
93618dca02aSRicardo Labiaga 	 * When this function is called for the first time,
93718dca02aSRicardo Labiaga 	 * transport->tcp_offset is 4 (after having already read the xid).
93818dca02aSRicardo Labiaga 	 */
93918dca02aSRicardo Labiaga 	offset = transport->tcp_offset - sizeof(transport->tcp_xid);
940b76ce561STrond Myklebust 	len = sizeof(transport->tcp_calldir) - offset;
94118dca02aSRicardo Labiaga 	dprintk("RPC:       reading CALL/REPLY flag (%Zu bytes)\n", len);
942b76ce561STrond Myklebust 	p = ((char *) &transport->tcp_calldir) + offset;
943b76ce561STrond Myklebust 	used = xdr_skb_read_bits(desc, p, len);
94418dca02aSRicardo Labiaga 	transport->tcp_offset += used;
94518dca02aSRicardo Labiaga 	if (used != len)
94618dca02aSRicardo Labiaga 		return;
947f4a2e418SRicardo Labiaga 	transport->tcp_flags &= ~TCP_RCV_READ_CALLDIR;
948f4a2e418SRicardo Labiaga 	/*
949f4a2e418SRicardo Labiaga 	 * We don't yet have the XDR buffer, so we will write the calldir
950f4a2e418SRicardo Labiaga 	 * out after we get the buffer from the 'struct rpc_rqst'
951f4a2e418SRicardo Labiaga 	 */
952b76ce561STrond Myklebust 	switch (ntohl(transport->tcp_calldir)) {
953b76ce561STrond Myklebust 	case RPC_REPLY:
954b76ce561STrond Myklebust 		transport->tcp_flags |= TCP_RCV_COPY_CALLDIR;
955b76ce561STrond Myklebust 		transport->tcp_flags |= TCP_RCV_COPY_DATA;
95618dca02aSRicardo Labiaga 		transport->tcp_flags |= TCP_RPC_REPLY;
957b76ce561STrond Myklebust 		break;
958b76ce561STrond Myklebust 	case RPC_CALL:
959b76ce561STrond Myklebust 		transport->tcp_flags |= TCP_RCV_COPY_CALLDIR;
960b76ce561STrond Myklebust 		transport->tcp_flags |= TCP_RCV_COPY_DATA;
96118dca02aSRicardo Labiaga 		transport->tcp_flags &= ~TCP_RPC_REPLY;
962b76ce561STrond Myklebust 		break;
963b76ce561STrond Myklebust 	default:
964b76ce561STrond Myklebust 		dprintk("RPC:       invalid request message type\n");
965b76ce561STrond Myklebust 		xprt_force_disconnect(&transport->xprt);
966b76ce561STrond Myklebust 	}
96718dca02aSRicardo Labiaga 	xs_tcp_check_fraghdr(transport);
96818dca02aSRicardo Labiaga }
96918dca02aSRicardo Labiaga 
97044b98efdSRicardo Labiaga static inline void xs_tcp_read_common(struct rpc_xprt *xprt,
97144b98efdSRicardo Labiaga 				     struct xdr_skb_reader *desc,
97244b98efdSRicardo Labiaga 				     struct rpc_rqst *req)
973a246b010SChuck Lever {
97444b98efdSRicardo Labiaga 	struct sock_xprt *transport =
97544b98efdSRicardo Labiaga 				container_of(xprt, struct sock_xprt, xprt);
976a246b010SChuck Lever 	struct xdr_buf *rcvbuf;
977a246b010SChuck Lever 	size_t len;
978a246b010SChuck Lever 	ssize_t r;
979a246b010SChuck Lever 
980a246b010SChuck Lever 	rcvbuf = &req->rq_private_buf;
981f4a2e418SRicardo Labiaga 
982f4a2e418SRicardo Labiaga 	if (transport->tcp_flags & TCP_RCV_COPY_CALLDIR) {
983f4a2e418SRicardo Labiaga 		/*
984f4a2e418SRicardo Labiaga 		 * Save the RPC direction in the XDR buffer
985f4a2e418SRicardo Labiaga 		 */
986f4a2e418SRicardo Labiaga 		memcpy(rcvbuf->head[0].iov_base + transport->tcp_copied,
987b76ce561STrond Myklebust 			&transport->tcp_calldir,
988b76ce561STrond Myklebust 			sizeof(transport->tcp_calldir));
989b76ce561STrond Myklebust 		transport->tcp_copied += sizeof(transport->tcp_calldir);
990f4a2e418SRicardo Labiaga 		transport->tcp_flags &= ~TCP_RCV_COPY_CALLDIR;
991a246b010SChuck Lever 	}
992a246b010SChuck Lever 
993a246b010SChuck Lever 	len = desc->count;
99451971139SChuck Lever 	if (len > transport->tcp_reclen - transport->tcp_offset) {
995dd456471SChuck Lever 		struct xdr_skb_reader my_desc;
996a246b010SChuck Lever 
99751971139SChuck Lever 		len = transport->tcp_reclen - transport->tcp_offset;
998a246b010SChuck Lever 		memcpy(&my_desc, desc, sizeof(my_desc));
999a246b010SChuck Lever 		my_desc.count = len;
100051971139SChuck Lever 		r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
10019d292316SChuck Lever 					  &my_desc, xdr_skb_read_bits);
1002a246b010SChuck Lever 		desc->count -= r;
1003a246b010SChuck Lever 		desc->offset += r;
1004a246b010SChuck Lever 	} else
100551971139SChuck Lever 		r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
10069d292316SChuck Lever 					  desc, xdr_skb_read_bits);
1007a246b010SChuck Lever 
1008a246b010SChuck Lever 	if (r > 0) {
100951971139SChuck Lever 		transport->tcp_copied += r;
101051971139SChuck Lever 		transport->tcp_offset += r;
1011a246b010SChuck Lever 	}
1012a246b010SChuck Lever 	if (r != len) {
1013a246b010SChuck Lever 		/* Error when copying to the receive buffer,
1014a246b010SChuck Lever 		 * usually because we weren't able to allocate
1015a246b010SChuck Lever 		 * additional buffer pages. All we can do now
1016e136d092SChuck Lever 		 * is turn off TCP_RCV_COPY_DATA, so the request
1017a246b010SChuck Lever 		 * will not receive any additional updates,
1018a246b010SChuck Lever 		 * and time out.
1019a246b010SChuck Lever 		 * Any remaining data from this record will
1020a246b010SChuck Lever 		 * be discarded.
1021a246b010SChuck Lever 		 */
1022e136d092SChuck Lever 		transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1023a246b010SChuck Lever 		dprintk("RPC:       XID %08x truncated request\n",
102451971139SChuck Lever 				ntohl(transport->tcp_xid));
102546121cf7SChuck Lever 		dprintk("RPC:       xprt = %p, tcp_copied = %lu, "
102646121cf7SChuck Lever 				"tcp_offset = %u, tcp_reclen = %u\n",
102746121cf7SChuck Lever 				xprt, transport->tcp_copied,
102846121cf7SChuck Lever 				transport->tcp_offset, transport->tcp_reclen);
102944b98efdSRicardo Labiaga 		return;
1030a246b010SChuck Lever 	}
1031a246b010SChuck Lever 
1032a246b010SChuck Lever 	dprintk("RPC:       XID %08x read %Zd bytes\n",
103351971139SChuck Lever 			ntohl(transport->tcp_xid), r);
103446121cf7SChuck Lever 	dprintk("RPC:       xprt = %p, tcp_copied = %lu, tcp_offset = %u, "
103546121cf7SChuck Lever 			"tcp_reclen = %u\n", xprt, transport->tcp_copied,
103646121cf7SChuck Lever 			transport->tcp_offset, transport->tcp_reclen);
1037a246b010SChuck Lever 
103851971139SChuck Lever 	if (transport->tcp_copied == req->rq_private_buf.buflen)
1039e136d092SChuck Lever 		transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
104051971139SChuck Lever 	else if (transport->tcp_offset == transport->tcp_reclen) {
1041e136d092SChuck Lever 		if (transport->tcp_flags & TCP_RCV_LAST_FRAG)
1042e136d092SChuck Lever 			transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1043a246b010SChuck Lever 	}
104444b98efdSRicardo Labiaga }
104544b98efdSRicardo Labiaga 
104644b98efdSRicardo Labiaga /*
104744b98efdSRicardo Labiaga  * Finds the request corresponding to the RPC xid and invokes the common
104844b98efdSRicardo Labiaga  * tcp read code to read the data.
104944b98efdSRicardo Labiaga  */
105044b98efdSRicardo Labiaga static inline int xs_tcp_read_reply(struct rpc_xprt *xprt,
105144b98efdSRicardo Labiaga 				    struct xdr_skb_reader *desc)
105244b98efdSRicardo Labiaga {
105344b98efdSRicardo Labiaga 	struct sock_xprt *transport =
105444b98efdSRicardo Labiaga 				container_of(xprt, struct sock_xprt, xprt);
105544b98efdSRicardo Labiaga 	struct rpc_rqst *req;
105644b98efdSRicardo Labiaga 
105744b98efdSRicardo Labiaga 	dprintk("RPC:       read reply XID %08x\n", ntohl(transport->tcp_xid));
105844b98efdSRicardo Labiaga 
105944b98efdSRicardo Labiaga 	/* Find and lock the request corresponding to this xid */
106044b98efdSRicardo Labiaga 	spin_lock(&xprt->transport_lock);
106144b98efdSRicardo Labiaga 	req = xprt_lookup_rqst(xprt, transport->tcp_xid);
106244b98efdSRicardo Labiaga 	if (!req) {
106344b98efdSRicardo Labiaga 		dprintk("RPC:       XID %08x request not found!\n",
106444b98efdSRicardo Labiaga 				ntohl(transport->tcp_xid));
106544b98efdSRicardo Labiaga 		spin_unlock(&xprt->transport_lock);
106644b98efdSRicardo Labiaga 		return -1;
106744b98efdSRicardo Labiaga 	}
106844b98efdSRicardo Labiaga 
106944b98efdSRicardo Labiaga 	xs_tcp_read_common(xprt, desc, req);
107044b98efdSRicardo Labiaga 
1071e136d092SChuck Lever 	if (!(transport->tcp_flags & TCP_RCV_COPY_DATA))
107251971139SChuck Lever 		xprt_complete_rqst(req->rq_task, transport->tcp_copied);
107344b98efdSRicardo Labiaga 
10744a0f8c04SChuck Lever 	spin_unlock(&xprt->transport_lock);
107544b98efdSRicardo Labiaga 	return 0;
107644b98efdSRicardo Labiaga }
107744b98efdSRicardo Labiaga 
107844b98efdSRicardo Labiaga #if defined(CONFIG_NFS_V4_1)
107944b98efdSRicardo Labiaga /*
108044b98efdSRicardo Labiaga  * Obtains an rpc_rqst previously allocated and invokes the common
108144b98efdSRicardo Labiaga  * tcp read code to read the data.  The result is placed in the callback
108244b98efdSRicardo Labiaga  * queue.
108344b98efdSRicardo Labiaga  * If we're unable to obtain the rpc_rqst we schedule the closing of the
108444b98efdSRicardo Labiaga  * connection and return -1.
108544b98efdSRicardo Labiaga  */
108644b98efdSRicardo Labiaga static inline int xs_tcp_read_callback(struct rpc_xprt *xprt,
108744b98efdSRicardo Labiaga 				       struct xdr_skb_reader *desc)
108844b98efdSRicardo Labiaga {
108944b98efdSRicardo Labiaga 	struct sock_xprt *transport =
109044b98efdSRicardo Labiaga 				container_of(xprt, struct sock_xprt, xprt);
109144b98efdSRicardo Labiaga 	struct rpc_rqst *req;
109244b98efdSRicardo Labiaga 
109344b98efdSRicardo Labiaga 	req = xprt_alloc_bc_request(xprt);
109444b98efdSRicardo Labiaga 	if (req == NULL) {
109544b98efdSRicardo Labiaga 		printk(KERN_WARNING "Callback slot table overflowed\n");
109644b98efdSRicardo Labiaga 		xprt_force_disconnect(xprt);
109744b98efdSRicardo Labiaga 		return -1;
109844b98efdSRicardo Labiaga 	}
109944b98efdSRicardo Labiaga 
110044b98efdSRicardo Labiaga 	req->rq_xid = transport->tcp_xid;
110144b98efdSRicardo Labiaga 	dprintk("RPC:       read callback  XID %08x\n", ntohl(req->rq_xid));
110244b98efdSRicardo Labiaga 	xs_tcp_read_common(xprt, desc, req);
110344b98efdSRicardo Labiaga 
110444b98efdSRicardo Labiaga 	if (!(transport->tcp_flags & TCP_RCV_COPY_DATA)) {
110544b98efdSRicardo Labiaga 		struct svc_serv *bc_serv = xprt->bc_serv;
110644b98efdSRicardo Labiaga 
110744b98efdSRicardo Labiaga 		/*
110844b98efdSRicardo Labiaga 		 * Add callback request to callback list.  The callback
110944b98efdSRicardo Labiaga 		 * service sleeps on the sv_cb_waitq waiting for new
111044b98efdSRicardo Labiaga 		 * requests.  Wake it up after adding enqueing the
111144b98efdSRicardo Labiaga 		 * request.
111244b98efdSRicardo Labiaga 		 */
111344b98efdSRicardo Labiaga 		dprintk("RPC:       add callback request to list\n");
111444b98efdSRicardo Labiaga 		spin_lock(&bc_serv->sv_cb_lock);
111544b98efdSRicardo Labiaga 		list_add(&req->rq_bc_list, &bc_serv->sv_cb_list);
111644b98efdSRicardo Labiaga 		spin_unlock(&bc_serv->sv_cb_lock);
111744b98efdSRicardo Labiaga 		wake_up(&bc_serv->sv_cb_waitq);
111844b98efdSRicardo Labiaga 	}
111944b98efdSRicardo Labiaga 
112044b98efdSRicardo Labiaga 	req->rq_private_buf.len = transport->tcp_copied;
112144b98efdSRicardo Labiaga 
112244b98efdSRicardo Labiaga 	return 0;
112344b98efdSRicardo Labiaga }
112444b98efdSRicardo Labiaga 
112544b98efdSRicardo Labiaga static inline int _xs_tcp_read_data(struct rpc_xprt *xprt,
112644b98efdSRicardo Labiaga 					struct xdr_skb_reader *desc)
112744b98efdSRicardo Labiaga {
112844b98efdSRicardo Labiaga 	struct sock_xprt *transport =
112944b98efdSRicardo Labiaga 				container_of(xprt, struct sock_xprt, xprt);
113044b98efdSRicardo Labiaga 
113144b98efdSRicardo Labiaga 	return (transport->tcp_flags & TCP_RPC_REPLY) ?
113244b98efdSRicardo Labiaga 		xs_tcp_read_reply(xprt, desc) :
113344b98efdSRicardo Labiaga 		xs_tcp_read_callback(xprt, desc);
113444b98efdSRicardo Labiaga }
113544b98efdSRicardo Labiaga #else
113644b98efdSRicardo Labiaga static inline int _xs_tcp_read_data(struct rpc_xprt *xprt,
113744b98efdSRicardo Labiaga 					struct xdr_skb_reader *desc)
113844b98efdSRicardo Labiaga {
113944b98efdSRicardo Labiaga 	return xs_tcp_read_reply(xprt, desc);
114044b98efdSRicardo Labiaga }
114144b98efdSRicardo Labiaga #endif /* CONFIG_NFS_V4_1 */
114244b98efdSRicardo Labiaga 
114344b98efdSRicardo Labiaga /*
114444b98efdSRicardo Labiaga  * Read data off the transport.  This can be either an RPC_CALL or an
114544b98efdSRicardo Labiaga  * RPC_REPLY.  Relay the processing to helper functions.
114644b98efdSRicardo Labiaga  */
114744b98efdSRicardo Labiaga static void xs_tcp_read_data(struct rpc_xprt *xprt,
114844b98efdSRicardo Labiaga 				    struct xdr_skb_reader *desc)
114944b98efdSRicardo Labiaga {
115044b98efdSRicardo Labiaga 	struct sock_xprt *transport =
115144b98efdSRicardo Labiaga 				container_of(xprt, struct sock_xprt, xprt);
115244b98efdSRicardo Labiaga 
115344b98efdSRicardo Labiaga 	if (_xs_tcp_read_data(xprt, desc) == 0)
115451971139SChuck Lever 		xs_tcp_check_fraghdr(transport);
115544b98efdSRicardo Labiaga 	else {
115644b98efdSRicardo Labiaga 		/*
115744b98efdSRicardo Labiaga 		 * The transport_lock protects the request handling.
115844b98efdSRicardo Labiaga 		 * There's no need to hold it to update the tcp_flags.
115944b98efdSRicardo Labiaga 		 */
116044b98efdSRicardo Labiaga 		transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
116144b98efdSRicardo Labiaga 	}
1162a246b010SChuck Lever }
1163a246b010SChuck Lever 
1164dd456471SChuck Lever static inline void xs_tcp_read_discard(struct sock_xprt *transport, struct xdr_skb_reader *desc)
1165a246b010SChuck Lever {
1166a246b010SChuck Lever 	size_t len;
1167a246b010SChuck Lever 
116851971139SChuck Lever 	len = transport->tcp_reclen - transport->tcp_offset;
1169a246b010SChuck Lever 	if (len > desc->count)
1170a246b010SChuck Lever 		len = desc->count;
1171a246b010SChuck Lever 	desc->count -= len;
1172a246b010SChuck Lever 	desc->offset += len;
117351971139SChuck Lever 	transport->tcp_offset += len;
1174a246b010SChuck Lever 	dprintk("RPC:       discarded %Zu bytes\n", len);
117551971139SChuck Lever 	xs_tcp_check_fraghdr(transport);
1176a246b010SChuck Lever }
1177a246b010SChuck Lever 
11789903cd1cSChuck Lever static int xs_tcp_data_recv(read_descriptor_t *rd_desc, struct sk_buff *skb, unsigned int offset, size_t len)
1179a246b010SChuck Lever {
1180a246b010SChuck Lever 	struct rpc_xprt *xprt = rd_desc->arg.data;
118151971139SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1182dd456471SChuck Lever 	struct xdr_skb_reader desc = {
1183a246b010SChuck Lever 		.skb	= skb,
1184a246b010SChuck Lever 		.offset	= offset,
1185a246b010SChuck Lever 		.count	= len,
1186a246b010SChuck Lever 	};
1187a246b010SChuck Lever 
11889903cd1cSChuck Lever 	dprintk("RPC:       xs_tcp_data_recv started\n");
1189a246b010SChuck Lever 	do {
1190a246b010SChuck Lever 		/* Read in a new fragment marker if necessary */
1191a246b010SChuck Lever 		/* Can we ever really expect to get completely empty fragments? */
1192e136d092SChuck Lever 		if (transport->tcp_flags & TCP_RCV_COPY_FRAGHDR) {
11939903cd1cSChuck Lever 			xs_tcp_read_fraghdr(xprt, &desc);
1194a246b010SChuck Lever 			continue;
1195a246b010SChuck Lever 		}
1196a246b010SChuck Lever 		/* Read in the xid if necessary */
1197e136d092SChuck Lever 		if (transport->tcp_flags & TCP_RCV_COPY_XID) {
119851971139SChuck Lever 			xs_tcp_read_xid(transport, &desc);
1199a246b010SChuck Lever 			continue;
1200a246b010SChuck Lever 		}
120118dca02aSRicardo Labiaga 		/* Read in the call/reply flag */
1202f4a2e418SRicardo Labiaga 		if (transport->tcp_flags & TCP_RCV_READ_CALLDIR) {
120318dca02aSRicardo Labiaga 			xs_tcp_read_calldir(transport, &desc);
120418dca02aSRicardo Labiaga 			continue;
120518dca02aSRicardo Labiaga 		}
1206a246b010SChuck Lever 		/* Read in the request data */
1207e136d092SChuck Lever 		if (transport->tcp_flags & TCP_RCV_COPY_DATA) {
120844b98efdSRicardo Labiaga 			xs_tcp_read_data(xprt, &desc);
1209a246b010SChuck Lever 			continue;
1210a246b010SChuck Lever 		}
1211a246b010SChuck Lever 		/* Skip over any trailing bytes on short reads */
121251971139SChuck Lever 		xs_tcp_read_discard(transport, &desc);
1213a246b010SChuck Lever 	} while (desc.count);
12149903cd1cSChuck Lever 	dprintk("RPC:       xs_tcp_data_recv done\n");
1215a246b010SChuck Lever 	return len - desc.count;
1216a246b010SChuck Lever }
1217a246b010SChuck Lever 
12189903cd1cSChuck Lever /**
12199903cd1cSChuck Lever  * xs_tcp_data_ready - "data ready" callback for TCP sockets
12209903cd1cSChuck Lever  * @sk: socket with data to read
12219903cd1cSChuck Lever  * @bytes: how much data to read
12229903cd1cSChuck Lever  *
12239903cd1cSChuck Lever  */
12249903cd1cSChuck Lever static void xs_tcp_data_ready(struct sock *sk, int bytes)
1225a246b010SChuck Lever {
1226a246b010SChuck Lever 	struct rpc_xprt *xprt;
1227a246b010SChuck Lever 	read_descriptor_t rd_desc;
1228ff2d7db8STrond Myklebust 	int read;
1229a246b010SChuck Lever 
12309903cd1cSChuck Lever 	dprintk("RPC:       xs_tcp_data_ready...\n");
123146121cf7SChuck Lever 
1232f064af1eSEric Dumazet 	read_lock_bh(&sk->sk_callback_lock);
12339903cd1cSChuck Lever 	if (!(xprt = xprt_from_sock(sk)))
1234a246b010SChuck Lever 		goto out;
1235a246b010SChuck Lever 	if (xprt->shutdown)
1236a246b010SChuck Lever 		goto out;
1237a246b010SChuck Lever 
123861d0a8e6SNeil Brown 	/* Any data means we had a useful conversation, so
123961d0a8e6SNeil Brown 	 * the we don't need to delay the next reconnect
124061d0a8e6SNeil Brown 	 */
124161d0a8e6SNeil Brown 	if (xprt->reestablish_timeout)
124261d0a8e6SNeil Brown 		xprt->reestablish_timeout = 0;
124361d0a8e6SNeil Brown 
12449903cd1cSChuck Lever 	/* We use rd_desc to pass struct xprt to xs_tcp_data_recv */
1245a246b010SChuck Lever 	rd_desc.arg.data = xprt;
1246ff2d7db8STrond Myklebust 	do {
1247a246b010SChuck Lever 		rd_desc.count = 65536;
1248ff2d7db8STrond Myklebust 		read = tcp_read_sock(sk, &rd_desc, xs_tcp_data_recv);
1249ff2d7db8STrond Myklebust 	} while (read > 0);
1250a246b010SChuck Lever out:
1251f064af1eSEric Dumazet 	read_unlock_bh(&sk->sk_callback_lock);
1252a246b010SChuck Lever }
1253a246b010SChuck Lever 
12547d1e8255STrond Myklebust /*
12557d1e8255STrond Myklebust  * Do the equivalent of linger/linger2 handling for dealing with
12567d1e8255STrond Myklebust  * broken servers that don't close the socket in a timely
12577d1e8255STrond Myklebust  * fashion
12587d1e8255STrond Myklebust  */
12597d1e8255STrond Myklebust static void xs_tcp_schedule_linger_timeout(struct rpc_xprt *xprt,
12607d1e8255STrond Myklebust 		unsigned long timeout)
12617d1e8255STrond Myklebust {
12627d1e8255STrond Myklebust 	struct sock_xprt *transport;
12637d1e8255STrond Myklebust 
12647d1e8255STrond Myklebust 	if (xprt_test_and_set_connecting(xprt))
12657d1e8255STrond Myklebust 		return;
12667d1e8255STrond Myklebust 	set_bit(XPRT_CONNECTION_ABORT, &xprt->state);
12677d1e8255STrond Myklebust 	transport = container_of(xprt, struct sock_xprt, xprt);
12687d1e8255STrond Myklebust 	queue_delayed_work(rpciod_workqueue, &transport->connect_worker,
12697d1e8255STrond Myklebust 			   timeout);
12707d1e8255STrond Myklebust }
12717d1e8255STrond Myklebust 
12727d1e8255STrond Myklebust static void xs_tcp_cancel_linger_timeout(struct rpc_xprt *xprt)
12737d1e8255STrond Myklebust {
12747d1e8255STrond Myklebust 	struct sock_xprt *transport;
12757d1e8255STrond Myklebust 
12767d1e8255STrond Myklebust 	transport = container_of(xprt, struct sock_xprt, xprt);
12777d1e8255STrond Myklebust 
12787d1e8255STrond Myklebust 	if (!test_bit(XPRT_CONNECTION_ABORT, &xprt->state) ||
12797d1e8255STrond Myklebust 	    !cancel_delayed_work(&transport->connect_worker))
12807d1e8255STrond Myklebust 		return;
12817d1e8255STrond Myklebust 	clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
12827d1e8255STrond Myklebust 	xprt_clear_connecting(xprt);
12837d1e8255STrond Myklebust }
12847d1e8255STrond Myklebust 
12857d1e8255STrond Myklebust static void xs_sock_mark_closed(struct rpc_xprt *xprt)
12867d1e8255STrond Myklebust {
12877d1e8255STrond Myklebust 	smp_mb__before_clear_bit();
12887d1e8255STrond Myklebust 	clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
12897d1e8255STrond Myklebust 	clear_bit(XPRT_CLOSING, &xprt->state);
12907d1e8255STrond Myklebust 	smp_mb__after_clear_bit();
12917d1e8255STrond Myklebust 	/* Mark transport as closed and wake up all pending tasks */
12927d1e8255STrond Myklebust 	xprt_disconnect_done(xprt);
12937d1e8255STrond Myklebust }
12947d1e8255STrond Myklebust 
12959903cd1cSChuck Lever /**
12969903cd1cSChuck Lever  * xs_tcp_state_change - callback to handle TCP socket state changes
12979903cd1cSChuck Lever  * @sk: socket whose state has changed
12989903cd1cSChuck Lever  *
12999903cd1cSChuck Lever  */
13009903cd1cSChuck Lever static void xs_tcp_state_change(struct sock *sk)
1301a246b010SChuck Lever {
1302a246b010SChuck Lever 	struct rpc_xprt *xprt;
1303a246b010SChuck Lever 
1304f064af1eSEric Dumazet 	read_lock_bh(&sk->sk_callback_lock);
1305a246b010SChuck Lever 	if (!(xprt = xprt_from_sock(sk)))
1306a246b010SChuck Lever 		goto out;
13079903cd1cSChuck Lever 	dprintk("RPC:       xs_tcp_state_change client %p...\n", xprt);
1308669502ffSAndy Chittenden 	dprintk("RPC:       state %x conn %d dead %d zapped %d sk_shutdown %d\n",
1309a246b010SChuck Lever 			sk->sk_state, xprt_connected(xprt),
1310a246b010SChuck Lever 			sock_flag(sk, SOCK_DEAD),
1311669502ffSAndy Chittenden 			sock_flag(sk, SOCK_ZAPPED),
1312669502ffSAndy Chittenden 			sk->sk_shutdown);
1313a246b010SChuck Lever 
1314a246b010SChuck Lever 	switch (sk->sk_state) {
1315a246b010SChuck Lever 	case TCP_ESTABLISHED:
1316f064af1eSEric Dumazet 		spin_lock(&xprt->transport_lock);
1317a246b010SChuck Lever 		if (!xprt_test_and_set_connected(xprt)) {
131851971139SChuck Lever 			struct sock_xprt *transport = container_of(xprt,
131951971139SChuck Lever 					struct sock_xprt, xprt);
132051971139SChuck Lever 
1321a246b010SChuck Lever 			/* Reset TCP record info */
132251971139SChuck Lever 			transport->tcp_offset = 0;
132351971139SChuck Lever 			transport->tcp_reclen = 0;
132451971139SChuck Lever 			transport->tcp_copied = 0;
1325e136d092SChuck Lever 			transport->tcp_flags =
1326e136d092SChuck Lever 				TCP_RCV_COPY_FRAGHDR | TCP_RCV_COPY_XID;
132751971139SChuck Lever 
13282a491991STrond Myklebust 			xprt_wake_pending_tasks(xprt, -EAGAIN);
1329a246b010SChuck Lever 		}
1330f064af1eSEric Dumazet 		spin_unlock(&xprt->transport_lock);
1331a246b010SChuck Lever 		break;
13323b948ae5STrond Myklebust 	case TCP_FIN_WAIT1:
13333b948ae5STrond Myklebust 		/* The client initiated a shutdown of the socket */
13347c1d71cfSTrond Myklebust 		xprt->connect_cookie++;
1335663b8858STrond Myklebust 		xprt->reestablish_timeout = 0;
13363b948ae5STrond Myklebust 		set_bit(XPRT_CLOSING, &xprt->state);
13373b948ae5STrond Myklebust 		smp_mb__before_clear_bit();
13383b948ae5STrond Myklebust 		clear_bit(XPRT_CONNECTED, &xprt->state);
1339ef803670STrond Myklebust 		clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
13403b948ae5STrond Myklebust 		smp_mb__after_clear_bit();
134125fe6142STrond Myklebust 		xs_tcp_schedule_linger_timeout(xprt, xs_tcp_fin_timeout);
1342a246b010SChuck Lever 		break;
1343632e3bdcSTrond Myklebust 	case TCP_CLOSE_WAIT:
13443b948ae5STrond Myklebust 		/* The server initiated a shutdown of the socket */
134566af1e55STrond Myklebust 		xprt_force_disconnect(xprt);
1346663b8858STrond Myklebust 	case TCP_SYN_SENT:
13477c1d71cfSTrond Myklebust 		xprt->connect_cookie++;
1348663b8858STrond Myklebust 	case TCP_CLOSING:
1349663b8858STrond Myklebust 		/*
1350663b8858STrond Myklebust 		 * If the server closed down the connection, make sure that
1351663b8858STrond Myklebust 		 * we back off before reconnecting
1352663b8858STrond Myklebust 		 */
1353663b8858STrond Myklebust 		if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
1354663b8858STrond Myklebust 			xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
13553b948ae5STrond Myklebust 		break;
13563b948ae5STrond Myklebust 	case TCP_LAST_ACK:
1357670f9457STrond Myklebust 		set_bit(XPRT_CLOSING, &xprt->state);
135825fe6142STrond Myklebust 		xs_tcp_schedule_linger_timeout(xprt, xs_tcp_fin_timeout);
13593b948ae5STrond Myklebust 		smp_mb__before_clear_bit();
13603b948ae5STrond Myklebust 		clear_bit(XPRT_CONNECTED, &xprt->state);
13613b948ae5STrond Myklebust 		smp_mb__after_clear_bit();
13623b948ae5STrond Myklebust 		break;
13633b948ae5STrond Myklebust 	case TCP_CLOSE:
13647d1e8255STrond Myklebust 		xs_tcp_cancel_linger_timeout(xprt);
13657d1e8255STrond Myklebust 		xs_sock_mark_closed(xprt);
1366a246b010SChuck Lever 	}
1367a246b010SChuck Lever  out:
1368f064af1eSEric Dumazet 	read_unlock_bh(&sk->sk_callback_lock);
1369a246b010SChuck Lever }
1370a246b010SChuck Lever 
13719903cd1cSChuck Lever /**
1372482f32e6STrond Myklebust  * xs_error_report - callback mainly for catching socket errors
13732a9e1cfaSTrond Myklebust  * @sk: socket
13742a9e1cfaSTrond Myklebust  */
1375482f32e6STrond Myklebust static void xs_error_report(struct sock *sk)
13762a9e1cfaSTrond Myklebust {
13772a9e1cfaSTrond Myklebust 	struct rpc_xprt *xprt;
13782a9e1cfaSTrond Myklebust 
1379f064af1eSEric Dumazet 	read_lock_bh(&sk->sk_callback_lock);
13802a9e1cfaSTrond Myklebust 	if (!(xprt = xprt_from_sock(sk)))
13812a9e1cfaSTrond Myklebust 		goto out;
13822a9e1cfaSTrond Myklebust 	dprintk("RPC:       %s client %p...\n"
13832a9e1cfaSTrond Myklebust 			"RPC:       error %d\n",
13842a9e1cfaSTrond Myklebust 			__func__, xprt, sk->sk_err);
1385482f32e6STrond Myklebust 	xprt_wake_pending_tasks(xprt, -EAGAIN);
13862a9e1cfaSTrond Myklebust out:
1387f064af1eSEric Dumazet 	read_unlock_bh(&sk->sk_callback_lock);
13882a9e1cfaSTrond Myklebust }
13892a9e1cfaSTrond Myklebust 
13901f0fa154SIlpo Järvinen static void xs_write_space(struct sock *sk)
13911f0fa154SIlpo Järvinen {
13921f0fa154SIlpo Järvinen 	struct socket *sock;
13931f0fa154SIlpo Järvinen 	struct rpc_xprt *xprt;
13941f0fa154SIlpo Järvinen 
13951f0fa154SIlpo Järvinen 	if (unlikely(!(sock = sk->sk_socket)))
13961f0fa154SIlpo Järvinen 		return;
13971f0fa154SIlpo Järvinen 	clear_bit(SOCK_NOSPACE, &sock->flags);
13981f0fa154SIlpo Järvinen 
13991f0fa154SIlpo Järvinen 	if (unlikely(!(xprt = xprt_from_sock(sk))))
14001f0fa154SIlpo Järvinen 		return;
14011f0fa154SIlpo Järvinen 	if (test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags) == 0)
14021f0fa154SIlpo Järvinen 		return;
14031f0fa154SIlpo Järvinen 
14041f0fa154SIlpo Järvinen 	xprt_write_space(xprt);
14051f0fa154SIlpo Järvinen }
14061f0fa154SIlpo Järvinen 
14072a9e1cfaSTrond Myklebust /**
1408c7b2cae8SChuck Lever  * xs_udp_write_space - callback invoked when socket buffer space
1409c7b2cae8SChuck Lever  *                             becomes available
14109903cd1cSChuck Lever  * @sk: socket whose state has changed
14119903cd1cSChuck Lever  *
1412a246b010SChuck Lever  * Called when more output buffer space is available for this socket.
1413a246b010SChuck Lever  * We try not to wake our writers until they can make "significant"
1414c7b2cae8SChuck Lever  * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1415a246b010SChuck Lever  * with a bunch of small requests.
1416a246b010SChuck Lever  */
1417c7b2cae8SChuck Lever static void xs_udp_write_space(struct sock *sk)
1418a246b010SChuck Lever {
1419f064af1eSEric Dumazet 	read_lock_bh(&sk->sk_callback_lock);
1420c7b2cae8SChuck Lever 
1421c7b2cae8SChuck Lever 	/* from net/core/sock.c:sock_def_write_space */
14221f0fa154SIlpo Järvinen 	if (sock_writeable(sk))
14231f0fa154SIlpo Järvinen 		xs_write_space(sk);
1424c7b2cae8SChuck Lever 
1425f064af1eSEric Dumazet 	read_unlock_bh(&sk->sk_callback_lock);
1426c7b2cae8SChuck Lever }
1427c7b2cae8SChuck Lever 
1428c7b2cae8SChuck Lever /**
1429c7b2cae8SChuck Lever  * xs_tcp_write_space - callback invoked when socket buffer space
1430c7b2cae8SChuck Lever  *                             becomes available
1431c7b2cae8SChuck Lever  * @sk: socket whose state has changed
1432c7b2cae8SChuck Lever  *
1433c7b2cae8SChuck Lever  * Called when more output buffer space is available for this socket.
1434c7b2cae8SChuck Lever  * We try not to wake our writers until they can make "significant"
1435c7b2cae8SChuck Lever  * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1436c7b2cae8SChuck Lever  * with a bunch of small requests.
1437c7b2cae8SChuck Lever  */
1438c7b2cae8SChuck Lever static void xs_tcp_write_space(struct sock *sk)
1439c7b2cae8SChuck Lever {
1440f064af1eSEric Dumazet 	read_lock_bh(&sk->sk_callback_lock);
1441c7b2cae8SChuck Lever 
1442c7b2cae8SChuck Lever 	/* from net/core/stream.c:sk_stream_write_space */
14431f0fa154SIlpo Järvinen 	if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk))
14441f0fa154SIlpo Järvinen 		xs_write_space(sk);
1445c7b2cae8SChuck Lever 
1446f064af1eSEric Dumazet 	read_unlock_bh(&sk->sk_callback_lock);
1447a246b010SChuck Lever }
1448a246b010SChuck Lever 
1449470056c2SChuck Lever static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt)
1450a246b010SChuck Lever {
1451ee0ac0c2SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1452ee0ac0c2SChuck Lever 	struct sock *sk = transport->inet;
1453a246b010SChuck Lever 
14547c6e066eSChuck Lever 	if (transport->rcvsize) {
1455a246b010SChuck Lever 		sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
14567c6e066eSChuck Lever 		sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2;
1457a246b010SChuck Lever 	}
14587c6e066eSChuck Lever 	if (transport->sndsize) {
1459a246b010SChuck Lever 		sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
14607c6e066eSChuck Lever 		sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2;
1461a246b010SChuck Lever 		sk->sk_write_space(sk);
1462a246b010SChuck Lever 	}
1463a246b010SChuck Lever }
1464a246b010SChuck Lever 
146543118c29SChuck Lever /**
1466470056c2SChuck Lever  * xs_udp_set_buffer_size - set send and receive limits
146743118c29SChuck Lever  * @xprt: generic transport
1468470056c2SChuck Lever  * @sndsize: requested size of send buffer, in bytes
1469470056c2SChuck Lever  * @rcvsize: requested size of receive buffer, in bytes
147043118c29SChuck Lever  *
1471470056c2SChuck Lever  * Set socket send and receive buffer size limits.
147243118c29SChuck Lever  */
1473470056c2SChuck Lever static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize)
147443118c29SChuck Lever {
14757c6e066eSChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
14767c6e066eSChuck Lever 
14777c6e066eSChuck Lever 	transport->sndsize = 0;
1478470056c2SChuck Lever 	if (sndsize)
14797c6e066eSChuck Lever 		transport->sndsize = sndsize + 1024;
14807c6e066eSChuck Lever 	transport->rcvsize = 0;
1481470056c2SChuck Lever 	if (rcvsize)
14827c6e066eSChuck Lever 		transport->rcvsize = rcvsize + 1024;
1483470056c2SChuck Lever 
1484470056c2SChuck Lever 	xs_udp_do_set_buffer_size(xprt);
148543118c29SChuck Lever }
148643118c29SChuck Lever 
148746c0ee8bSChuck Lever /**
148846c0ee8bSChuck Lever  * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
148946c0ee8bSChuck Lever  * @task: task that timed out
149046c0ee8bSChuck Lever  *
149146c0ee8bSChuck Lever  * Adjust the congestion window after a retransmit timeout has occurred.
149246c0ee8bSChuck Lever  */
149346c0ee8bSChuck Lever static void xs_udp_timer(struct rpc_task *task)
149446c0ee8bSChuck Lever {
149546c0ee8bSChuck Lever 	xprt_adjust_cwnd(task, -ETIMEDOUT);
149646c0ee8bSChuck Lever }
149746c0ee8bSChuck Lever 
1498b85d8806SChuck Lever static unsigned short xs_get_random_port(void)
1499b85d8806SChuck Lever {
1500b85d8806SChuck Lever 	unsigned short range = xprt_max_resvport - xprt_min_resvport;
1501b85d8806SChuck Lever 	unsigned short rand = (unsigned short) net_random() % range;
1502b85d8806SChuck Lever 	return rand + xprt_min_resvport;
1503b85d8806SChuck Lever }
1504b85d8806SChuck Lever 
150592200412SChuck Lever /**
150692200412SChuck Lever  * xs_set_port - reset the port number in the remote endpoint address
150792200412SChuck Lever  * @xprt: generic transport
150892200412SChuck Lever  * @port: new port number
150992200412SChuck Lever  *
151092200412SChuck Lever  */
151192200412SChuck Lever static void xs_set_port(struct rpc_xprt *xprt, unsigned short port)
151292200412SChuck Lever {
151392200412SChuck Lever 	dprintk("RPC:       setting port for xprt %p to %u\n", xprt, port);
1514c4efcb1dSChuck Lever 
15159dc3b095SChuck Lever 	rpc_set_port(xs_addr(xprt), port);
15169dc3b095SChuck Lever 	xs_update_peer_port(xprt);
151792200412SChuck Lever }
151892200412SChuck Lever 
151967a391d7STrond Myklebust static unsigned short xs_get_srcport(struct sock_xprt *transport, struct socket *sock)
152067a391d7STrond Myklebust {
1521fbfffbd5SChuck Lever 	unsigned short port = transport->srcport;
152267a391d7STrond Myklebust 
152367a391d7STrond Myklebust 	if (port == 0 && transport->xprt.resvport)
152467a391d7STrond Myklebust 		port = xs_get_random_port();
152567a391d7STrond Myklebust 	return port;
152667a391d7STrond Myklebust }
152767a391d7STrond Myklebust 
152867a391d7STrond Myklebust static unsigned short xs_next_srcport(struct sock_xprt *transport, struct socket *sock, unsigned short port)
152967a391d7STrond Myklebust {
1530fbfffbd5SChuck Lever 	if (transport->srcport != 0)
1531fbfffbd5SChuck Lever 		transport->srcport = 0;
153267a391d7STrond Myklebust 	if (!transport->xprt.resvport)
153367a391d7STrond Myklebust 		return 0;
153467a391d7STrond Myklebust 	if (port <= xprt_min_resvport || port > xprt_max_resvport)
153567a391d7STrond Myklebust 		return xprt_max_resvport;
153667a391d7STrond Myklebust 	return --port;
153767a391d7STrond Myklebust }
153867a391d7STrond Myklebust 
15397dc753f0SChuck Lever static int xs_bind4(struct sock_xprt *transport, struct socket *sock)
1540a246b010SChuck Lever {
1541a246b010SChuck Lever 	struct sockaddr_in myaddr = {
1542a246b010SChuck Lever 		.sin_family = AF_INET,
1543a246b010SChuck Lever 	};
1544d3bc9a1dSFrank van Maarseveen 	struct sockaddr_in *sa;
154567a391d7STrond Myklebust 	int err, nloop = 0;
154667a391d7STrond Myklebust 	unsigned short port = xs_get_srcport(transport, sock);
154767a391d7STrond Myklebust 	unsigned short last;
1548a246b010SChuck Lever 
1549fbfffbd5SChuck Lever 	sa = (struct sockaddr_in *)&transport->srcaddr;
1550d3bc9a1dSFrank van Maarseveen 	myaddr.sin_addr = sa->sin_addr;
1551a246b010SChuck Lever 	do {
1552a246b010SChuck Lever 		myaddr.sin_port = htons(port);
1553e6242e92SSridhar Samudrala 		err = kernel_bind(sock, (struct sockaddr *) &myaddr,
1554a246b010SChuck Lever 						sizeof(myaddr));
155567a391d7STrond Myklebust 		if (port == 0)
1556d3bc9a1dSFrank van Maarseveen 			break;
1557a246b010SChuck Lever 		if (err == 0) {
1558fbfffbd5SChuck Lever 			transport->srcport = port;
1559d3bc9a1dSFrank van Maarseveen 			break;
1560a246b010SChuck Lever 		}
156167a391d7STrond Myklebust 		last = port;
156267a391d7STrond Myklebust 		port = xs_next_srcport(transport, sock, port);
156367a391d7STrond Myklebust 		if (port > last)
156467a391d7STrond Myklebust 			nloop++;
156567a391d7STrond Myklebust 	} while (err == -EADDRINUSE && nloop != 2);
156621454aaaSHarvey Harrison 	dprintk("RPC:       %s %pI4:%u: %s (%d)\n",
156721454aaaSHarvey Harrison 			__func__, &myaddr.sin_addr,
15687dc753f0SChuck Lever 			port, err ? "failed" : "ok", err);
1569a246b010SChuck Lever 	return err;
1570a246b010SChuck Lever }
1571a246b010SChuck Lever 
157290058d37SChuck Lever static int xs_bind6(struct sock_xprt *transport, struct socket *sock)
157390058d37SChuck Lever {
157490058d37SChuck Lever 	struct sockaddr_in6 myaddr = {
157590058d37SChuck Lever 		.sin6_family = AF_INET6,
157690058d37SChuck Lever 	};
157790058d37SChuck Lever 	struct sockaddr_in6 *sa;
157867a391d7STrond Myklebust 	int err, nloop = 0;
157967a391d7STrond Myklebust 	unsigned short port = xs_get_srcport(transport, sock);
158067a391d7STrond Myklebust 	unsigned short last;
158190058d37SChuck Lever 
1582fbfffbd5SChuck Lever 	sa = (struct sockaddr_in6 *)&transport->srcaddr;
158390058d37SChuck Lever 	myaddr.sin6_addr = sa->sin6_addr;
158490058d37SChuck Lever 	do {
158590058d37SChuck Lever 		myaddr.sin6_port = htons(port);
158690058d37SChuck Lever 		err = kernel_bind(sock, (struct sockaddr *) &myaddr,
158790058d37SChuck Lever 						sizeof(myaddr));
158867a391d7STrond Myklebust 		if (port == 0)
158990058d37SChuck Lever 			break;
159090058d37SChuck Lever 		if (err == 0) {
1591fbfffbd5SChuck Lever 			transport->srcport = port;
159290058d37SChuck Lever 			break;
159390058d37SChuck Lever 		}
159467a391d7STrond Myklebust 		last = port;
159567a391d7STrond Myklebust 		port = xs_next_srcport(transport, sock, port);
159667a391d7STrond Myklebust 		if (port > last)
159767a391d7STrond Myklebust 			nloop++;
159867a391d7STrond Myklebust 	} while (err == -EADDRINUSE && nloop != 2);
15995b095d98SHarvey Harrison 	dprintk("RPC:       xs_bind6 %pI6:%u: %s (%d)\n",
1600fdb46ee7SHarvey Harrison 		&myaddr.sin6_addr, port, err ? "failed" : "ok", err);
1601a246b010SChuck Lever 	return err;
1602a246b010SChuck Lever }
1603a246b010SChuck Lever 
1604ed07536eSPeter Zijlstra #ifdef CONFIG_DEBUG_LOCK_ALLOC
1605ed07536eSPeter Zijlstra static struct lock_class_key xs_key[2];
1606ed07536eSPeter Zijlstra static struct lock_class_key xs_slock_key[2];
1607ed07536eSPeter Zijlstra 
16088945ee5eSChuck Lever static inline void xs_reclassify_socket4(struct socket *sock)
1609ed07536eSPeter Zijlstra {
1610ed07536eSPeter Zijlstra 	struct sock *sk = sock->sk;
16118945ee5eSChuck Lever 
161202b3d346SJohn Heffner 	BUG_ON(sock_owned_by_user(sk));
16138945ee5eSChuck Lever 	sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC",
16148945ee5eSChuck Lever 		&xs_slock_key[0], "sk_lock-AF_INET-RPC", &xs_key[0]);
1615ed07536eSPeter Zijlstra }
16168945ee5eSChuck Lever 
16178945ee5eSChuck Lever static inline void xs_reclassify_socket6(struct socket *sock)
16188945ee5eSChuck Lever {
16198945ee5eSChuck Lever 	struct sock *sk = sock->sk;
16208945ee5eSChuck Lever 
1621f4921affSLinus Torvalds 	BUG_ON(sock_owned_by_user(sk));
16228945ee5eSChuck Lever 	sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC",
16238945ee5eSChuck Lever 		&xs_slock_key[1], "sk_lock-AF_INET6-RPC", &xs_key[1]);
1624ed07536eSPeter Zijlstra }
1625ed07536eSPeter Zijlstra #else
16268945ee5eSChuck Lever static inline void xs_reclassify_socket4(struct socket *sock)
16278945ee5eSChuck Lever {
16288945ee5eSChuck Lever }
16298945ee5eSChuck Lever 
16308945ee5eSChuck Lever static inline void xs_reclassify_socket6(struct socket *sock)
1631ed07536eSPeter Zijlstra {
1632ed07536eSPeter Zijlstra }
1633ed07536eSPeter Zijlstra #endif
1634ed07536eSPeter Zijlstra 
163516be2d20SChuck Lever static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
1636a246b010SChuck Lever {
163716be2d20SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1638edb267a6SChuck Lever 
1639ee0ac0c2SChuck Lever 	if (!transport->inet) {
1640b0d93ad5SChuck Lever 		struct sock *sk = sock->sk;
1641b0d93ad5SChuck Lever 
1642b0d93ad5SChuck Lever 		write_lock_bh(&sk->sk_callback_lock);
1643b0d93ad5SChuck Lever 
16442a9e1cfaSTrond Myklebust 		xs_save_old_callbacks(transport, sk);
16452a9e1cfaSTrond Myklebust 
1646b0d93ad5SChuck Lever 		sk->sk_user_data = xprt;
1647b0d93ad5SChuck Lever 		sk->sk_data_ready = xs_udp_data_ready;
1648b0d93ad5SChuck Lever 		sk->sk_write_space = xs_udp_write_space;
1649482f32e6STrond Myklebust 		sk->sk_error_report = xs_error_report;
1650b0d93ad5SChuck Lever 		sk->sk_no_check = UDP_CSUM_NORCV;
1651b079fa7bSTrond Myklebust 		sk->sk_allocation = GFP_ATOMIC;
1652b0d93ad5SChuck Lever 
1653b0d93ad5SChuck Lever 		xprt_set_connected(xprt);
1654b0d93ad5SChuck Lever 
1655b0d93ad5SChuck Lever 		/* Reset to new socket */
1656ee0ac0c2SChuck Lever 		transport->sock = sock;
1657ee0ac0c2SChuck Lever 		transport->inet = sk;
1658b0d93ad5SChuck Lever 
1659b0d93ad5SChuck Lever 		write_unlock_bh(&sk->sk_callback_lock);
1660b0d93ad5SChuck Lever 	}
1661470056c2SChuck Lever 	xs_udp_do_set_buffer_size(xprt);
166216be2d20SChuck Lever }
166316be2d20SChuck Lever 
1664a246b010SChuck Lever /**
16659c3d72deSChuck Lever  * xs_udp_connect_worker4 - set up a UDP socket
1666a246b010SChuck Lever  * @work: RPC transport to connect
1667a246b010SChuck Lever  *
1668a246b010SChuck Lever  * Invoked by a work queue tasklet.
1669a246b010SChuck Lever  */
16709c3d72deSChuck Lever static void xs_udp_connect_worker4(struct work_struct *work)
1671a246b010SChuck Lever {
1672a246b010SChuck Lever 	struct sock_xprt *transport =
1673a246b010SChuck Lever 		container_of(work, struct sock_xprt, connect_worker.work);
1674a246b010SChuck Lever 	struct rpc_xprt *xprt = &transport->xprt;
1675a246b010SChuck Lever 	struct socket *sock = transport->sock;
1676a246b010SChuck Lever 	int err, status = -EIO;
1677a246b010SChuck Lever 
167801d37c42STrond Myklebust 	if (xprt->shutdown)
16799903cd1cSChuck Lever 		goto out;
16809903cd1cSChuck Lever 
1681a246b010SChuck Lever 	/* Start by resetting any existing state */
1682fe315e76SChuck Lever 	xs_reset_transport(transport);
1683a246b010SChuck Lever 
1684fe315e76SChuck Lever 	err = sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock);
1685fe315e76SChuck Lever 	if (err < 0) {
16869903cd1cSChuck Lever 		dprintk("RPC:       can't create UDP transport socket (%d).\n", -err);
1687a246b010SChuck Lever 		goto out;
1688a246b010SChuck Lever 	}
16898945ee5eSChuck Lever 	xs_reclassify_socket4(sock);
1690a246b010SChuck Lever 
16917dc753f0SChuck Lever 	if (xs_bind4(transport, sock)) {
16929903cd1cSChuck Lever 		sock_release(sock);
16939903cd1cSChuck Lever 		goto out;
1694a246b010SChuck Lever 	}
1695b0d93ad5SChuck Lever 
1696c740eff8SChuck Lever 	dprintk("RPC:       worker connecting xprt %p via %s to "
1697c740eff8SChuck Lever 				"%s (port %s)\n", xprt,
1698c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_PROTO],
1699c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_ADDR],
1700c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_PORT]);
1701b0d93ad5SChuck Lever 
170216be2d20SChuck Lever 	xs_udp_finish_connecting(xprt, sock);
1703a246b010SChuck Lever 	status = 0;
1704b0d93ad5SChuck Lever out:
1705b0d93ad5SChuck Lever 	xprt_clear_connecting(xprt);
17067d1e8255STrond Myklebust 	xprt_wake_pending_tasks(xprt, status);
1707b0d93ad5SChuck Lever }
1708b0d93ad5SChuck Lever 
170968e220bdSChuck Lever /**
171068e220bdSChuck Lever  * xs_udp_connect_worker6 - set up a UDP socket
171168e220bdSChuck Lever  * @work: RPC transport to connect
171268e220bdSChuck Lever  *
171368e220bdSChuck Lever  * Invoked by a work queue tasklet.
171468e220bdSChuck Lever  */
171568e220bdSChuck Lever static void xs_udp_connect_worker6(struct work_struct *work)
171668e220bdSChuck Lever {
171768e220bdSChuck Lever 	struct sock_xprt *transport =
171868e220bdSChuck Lever 		container_of(work, struct sock_xprt, connect_worker.work);
171968e220bdSChuck Lever 	struct rpc_xprt *xprt = &transport->xprt;
172068e220bdSChuck Lever 	struct socket *sock = transport->sock;
172168e220bdSChuck Lever 	int err, status = -EIO;
172268e220bdSChuck Lever 
172301d37c42STrond Myklebust 	if (xprt->shutdown)
172468e220bdSChuck Lever 		goto out;
172568e220bdSChuck Lever 
172668e220bdSChuck Lever 	/* Start by resetting any existing state */
1727fe315e76SChuck Lever 	xs_reset_transport(transport);
172868e220bdSChuck Lever 
1729fe315e76SChuck Lever 	err = sock_create_kern(PF_INET6, SOCK_DGRAM, IPPROTO_UDP, &sock);
1730fe315e76SChuck Lever 	if (err < 0) {
173168e220bdSChuck Lever 		dprintk("RPC:       can't create UDP transport socket (%d).\n", -err);
173268e220bdSChuck Lever 		goto out;
173368e220bdSChuck Lever 	}
17348945ee5eSChuck Lever 	xs_reclassify_socket6(sock);
173568e220bdSChuck Lever 
173668e220bdSChuck Lever 	if (xs_bind6(transport, sock) < 0) {
173768e220bdSChuck Lever 		sock_release(sock);
173868e220bdSChuck Lever 		goto out;
173968e220bdSChuck Lever 	}
174068e220bdSChuck Lever 
1741c740eff8SChuck Lever 	dprintk("RPC:       worker connecting xprt %p via %s to "
1742c740eff8SChuck Lever 				"%s (port %s)\n", xprt,
1743c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_PROTO],
1744c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_ADDR],
1745c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_PORT]);
174668e220bdSChuck Lever 
174768e220bdSChuck Lever 	xs_udp_finish_connecting(xprt, sock);
1748a246b010SChuck Lever 	status = 0;
1749b0d93ad5SChuck Lever out:
1750b0d93ad5SChuck Lever 	xprt_clear_connecting(xprt);
17517d1e8255STrond Myklebust 	xprt_wake_pending_tasks(xprt, status);
1752b0d93ad5SChuck Lever }
1753b0d93ad5SChuck Lever 
17543167e12cSChuck Lever /*
17553167e12cSChuck Lever  * We need to preserve the port number so the reply cache on the server can
17563167e12cSChuck Lever  * find our cached RPC replies when we get around to reconnecting.
17573167e12cSChuck Lever  */
175840d2549dSTrond Myklebust static void xs_abort_connection(struct rpc_xprt *xprt, struct sock_xprt *transport)
17593167e12cSChuck Lever {
17603167e12cSChuck Lever 	int result;
17613167e12cSChuck Lever 	struct sockaddr any;
17623167e12cSChuck Lever 
17633167e12cSChuck Lever 	dprintk("RPC:       disconnecting xprt %p to reuse port\n", xprt);
17643167e12cSChuck Lever 
17653167e12cSChuck Lever 	/*
17663167e12cSChuck Lever 	 * Disconnect the transport socket by doing a connect operation
17673167e12cSChuck Lever 	 * with AF_UNSPEC.  This should return immediately...
17683167e12cSChuck Lever 	 */
17693167e12cSChuck Lever 	memset(&any, 0, sizeof(any));
17703167e12cSChuck Lever 	any.sa_family = AF_UNSPEC;
1771ee0ac0c2SChuck Lever 	result = kernel_connect(transport->sock, &any, sizeof(any), 0);
17727d1e8255STrond Myklebust 	if (!result)
17737d1e8255STrond Myklebust 		xs_sock_mark_closed(xprt);
17747d1e8255STrond Myklebust 	else
17753167e12cSChuck Lever 		dprintk("RPC:       AF_UNSPEC connect return code %d\n",
17763167e12cSChuck Lever 				result);
17773167e12cSChuck Lever }
17783167e12cSChuck Lever 
177940d2549dSTrond Myklebust static void xs_tcp_reuse_connection(struct rpc_xprt *xprt, struct sock_xprt *transport)
178040d2549dSTrond Myklebust {
178140d2549dSTrond Myklebust 	unsigned int state = transport->inet->sk_state;
178240d2549dSTrond Myklebust 
1783669502ffSAndy Chittenden 	if (state == TCP_CLOSE && transport->sock->state == SS_UNCONNECTED) {
1784669502ffSAndy Chittenden 		/* we don't need to abort the connection if the socket
1785669502ffSAndy Chittenden 		 * hasn't undergone a shutdown
1786669502ffSAndy Chittenden 		 */
1787669502ffSAndy Chittenden 		if (transport->inet->sk_shutdown == 0)
178840d2549dSTrond Myklebust 			return;
1789669502ffSAndy Chittenden 		dprintk("RPC:       %s: TCP_CLOSEd and sk_shutdown set to %d\n",
1790669502ffSAndy Chittenden 				__func__, transport->inet->sk_shutdown);
1791669502ffSAndy Chittenden 	}
1792669502ffSAndy Chittenden 	if ((1 << state) & (TCPF_ESTABLISHED|TCPF_SYN_SENT)) {
1793669502ffSAndy Chittenden 		/* we don't need to abort the connection if the socket
1794669502ffSAndy Chittenden 		 * hasn't undergone a shutdown
1795669502ffSAndy Chittenden 		 */
1796669502ffSAndy Chittenden 		if (transport->inet->sk_shutdown == 0)
179740d2549dSTrond Myklebust 			return;
1798669502ffSAndy Chittenden 		dprintk("RPC:       %s: ESTABLISHED/SYN_SENT "
1799669502ffSAndy Chittenden 				"sk_shutdown set to %d\n",
1800669502ffSAndy Chittenden 				__func__, transport->inet->sk_shutdown);
1801669502ffSAndy Chittenden 	}
180240d2549dSTrond Myklebust 	xs_abort_connection(xprt, transport);
180340d2549dSTrond Myklebust }
180440d2549dSTrond Myklebust 
180516be2d20SChuck Lever static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
1806b0d93ad5SChuck Lever {
180716be2d20SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1808edb267a6SChuck Lever 
1809ee0ac0c2SChuck Lever 	if (!transport->inet) {
1810b0d93ad5SChuck Lever 		struct sock *sk = sock->sk;
1811b0d93ad5SChuck Lever 
1812b0d93ad5SChuck Lever 		write_lock_bh(&sk->sk_callback_lock);
1813b0d93ad5SChuck Lever 
18142a9e1cfaSTrond Myklebust 		xs_save_old_callbacks(transport, sk);
18152a9e1cfaSTrond Myklebust 
1816b0d93ad5SChuck Lever 		sk->sk_user_data = xprt;
1817b0d93ad5SChuck Lever 		sk->sk_data_ready = xs_tcp_data_ready;
1818b0d93ad5SChuck Lever 		sk->sk_state_change = xs_tcp_state_change;
1819b0d93ad5SChuck Lever 		sk->sk_write_space = xs_tcp_write_space;
1820482f32e6STrond Myklebust 		sk->sk_error_report = xs_error_report;
1821b079fa7bSTrond Myklebust 		sk->sk_allocation = GFP_ATOMIC;
18223167e12cSChuck Lever 
18233167e12cSChuck Lever 		/* socket options */
18243167e12cSChuck Lever 		sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
18253167e12cSChuck Lever 		sock_reset_flag(sk, SOCK_LINGER);
18263167e12cSChuck Lever 		tcp_sk(sk)->linger2 = 0;
18273167e12cSChuck Lever 		tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF;
1828b0d93ad5SChuck Lever 
1829b0d93ad5SChuck Lever 		xprt_clear_connected(xprt);
1830b0d93ad5SChuck Lever 
1831b0d93ad5SChuck Lever 		/* Reset to new socket */
1832ee0ac0c2SChuck Lever 		transport->sock = sock;
1833ee0ac0c2SChuck Lever 		transport->inet = sk;
1834b0d93ad5SChuck Lever 
1835b0d93ad5SChuck Lever 		write_unlock_bh(&sk->sk_callback_lock);
1836b0d93ad5SChuck Lever 	}
1837b0d93ad5SChuck Lever 
183801d37c42STrond Myklebust 	if (!xprt_bound(xprt))
183901d37c42STrond Myklebust 		return -ENOTCONN;
184001d37c42STrond Myklebust 
1841b0d93ad5SChuck Lever 	/* Tell the socket layer to start connecting... */
1842262ca07dSChuck Lever 	xprt->stat.connect_count++;
1843262ca07dSChuck Lever 	xprt->stat.connect_start = jiffies;
184495392c59SChuck Lever 	return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK);
184516be2d20SChuck Lever }
184616be2d20SChuck Lever 
184716be2d20SChuck Lever /**
1848b61d59ffSTrond Myklebust  * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint
1849b61d59ffSTrond Myklebust  * @xprt: RPC transport to connect
1850b61d59ffSTrond Myklebust  * @transport: socket transport to connect
1851b61d59ffSTrond Myklebust  * @create_sock: function to create a socket of the correct type
185216be2d20SChuck Lever  *
185316be2d20SChuck Lever  * Invoked by a work queue tasklet.
185416be2d20SChuck Lever  */
1855b61d59ffSTrond Myklebust static void xs_tcp_setup_socket(struct rpc_xprt *xprt,
1856b61d59ffSTrond Myklebust 		struct sock_xprt *transport,
1857b61d59ffSTrond Myklebust 		struct socket *(*create_sock)(struct rpc_xprt *,
1858b61d59ffSTrond Myklebust 			struct sock_xprt *))
185916be2d20SChuck Lever {
186016be2d20SChuck Lever 	struct socket *sock = transport->sock;
1861b61d59ffSTrond Myklebust 	int status = -EIO;
186216be2d20SChuck Lever 
186301d37c42STrond Myklebust 	if (xprt->shutdown)
186416be2d20SChuck Lever 		goto out;
186516be2d20SChuck Lever 
186616be2d20SChuck Lever 	if (!sock) {
18677d1e8255STrond Myklebust 		clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1868b61d59ffSTrond Myklebust 		sock = create_sock(xprt, transport);
1869b61d59ffSTrond Myklebust 		if (IS_ERR(sock)) {
1870b61d59ffSTrond Myklebust 			status = PTR_ERR(sock);
187116be2d20SChuck Lever 			goto out;
187216be2d20SChuck Lever 		}
18737d1e8255STrond Myklebust 	} else {
18747d1e8255STrond Myklebust 		int abort_and_exit;
18757d1e8255STrond Myklebust 
18767d1e8255STrond Myklebust 		abort_and_exit = test_and_clear_bit(XPRT_CONNECTION_ABORT,
18777d1e8255STrond Myklebust 				&xprt->state);
187816be2d20SChuck Lever 		/* "close" the socket, preserving the local port */
187940d2549dSTrond Myklebust 		xs_tcp_reuse_connection(xprt, transport);
188016be2d20SChuck Lever 
18817d1e8255STrond Myklebust 		if (abort_and_exit)
18827d1e8255STrond Myklebust 			goto out_eagain;
18837d1e8255STrond Myklebust 	}
18847d1e8255STrond Myklebust 
1885c740eff8SChuck Lever 	dprintk("RPC:       worker connecting xprt %p via %s to "
1886c740eff8SChuck Lever 				"%s (port %s)\n", xprt,
1887c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_PROTO],
1888c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_ADDR],
1889c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_PORT]);
189016be2d20SChuck Lever 
189116be2d20SChuck Lever 	status = xs_tcp_finish_connecting(xprt, sock);
1892a246b010SChuck Lever 	dprintk("RPC:       %p connect status %d connected %d sock state %d\n",
189346121cf7SChuck Lever 			xprt, -status, xprt_connected(xprt),
189446121cf7SChuck Lever 			sock->sk->sk_state);
1895a246b010SChuck Lever 	switch (status) {
1896f75e6745STrond Myklebust 	default:
1897f75e6745STrond Myklebust 		printk("%s: connect returned unhandled error %d\n",
1898f75e6745STrond Myklebust 			__func__, status);
1899f75e6745STrond Myklebust 	case -EADDRNOTAVAIL:
1900f75e6745STrond Myklebust 		/* We're probably in TIME_WAIT. Get rid of existing socket,
1901f75e6745STrond Myklebust 		 * and retry
1902f75e6745STrond Myklebust 		 */
1903f75e6745STrond Myklebust 		set_bit(XPRT_CONNECTION_CLOSE, &xprt->state);
1904f75e6745STrond Myklebust 		xprt_force_disconnect(xprt);
190588b5ed73STrond Myklebust 		break;
19068a2cec29STrond Myklebust 	case -ECONNREFUSED:
19078a2cec29STrond Myklebust 	case -ECONNRESET:
19088a2cec29STrond Myklebust 	case -ENETUNREACH:
19098a2cec29STrond Myklebust 		/* retry with existing socket, after a delay */
19102a491991STrond Myklebust 	case 0:
1911a246b010SChuck Lever 	case -EINPROGRESS:
1912a246b010SChuck Lever 	case -EALREADY:
19137d1e8255STrond Myklebust 		xprt_clear_connecting(xprt);
19147d1e8255STrond Myklebust 		return;
19159fcfe0c8STrond Myklebust 	case -EINVAL:
19169fcfe0c8STrond Myklebust 		/* Happens, for instance, if the user specified a link
19179fcfe0c8STrond Myklebust 		 * local IPv6 address without a scope-id.
19189fcfe0c8STrond Myklebust 		 */
19199fcfe0c8STrond Myklebust 		goto out;
19208a2cec29STrond Myklebust 	}
19217d1e8255STrond Myklebust out_eagain:
19222a491991STrond Myklebust 	status = -EAGAIN;
1923a246b010SChuck Lever out:
19242226feb6SChuck Lever 	xprt_clear_connecting(xprt);
19257d1e8255STrond Myklebust 	xprt_wake_pending_tasks(xprt, status);
1926a246b010SChuck Lever }
1927a246b010SChuck Lever 
1928b61d59ffSTrond Myklebust static struct socket *xs_create_tcp_sock4(struct rpc_xprt *xprt,
1929b61d59ffSTrond Myklebust 		struct sock_xprt *transport)
1930b61d59ffSTrond Myklebust {
1931b61d59ffSTrond Myklebust 	struct socket *sock;
1932b61d59ffSTrond Myklebust 	int err;
1933b61d59ffSTrond Myklebust 
1934b61d59ffSTrond Myklebust 	/* start from scratch */
1935b61d59ffSTrond Myklebust 	err = sock_create_kern(PF_INET, SOCK_STREAM, IPPROTO_TCP, &sock);
1936b61d59ffSTrond Myklebust 	if (err < 0) {
1937b61d59ffSTrond Myklebust 		dprintk("RPC:       can't create TCP transport socket (%d).\n",
1938b61d59ffSTrond Myklebust 				-err);
1939b61d59ffSTrond Myklebust 		goto out_err;
1940b61d59ffSTrond Myklebust 	}
1941b61d59ffSTrond Myklebust 	xs_reclassify_socket4(sock);
1942b61d59ffSTrond Myklebust 
1943b61d59ffSTrond Myklebust 	if (xs_bind4(transport, sock) < 0) {
1944b61d59ffSTrond Myklebust 		sock_release(sock);
1945b61d59ffSTrond Myklebust 		goto out_err;
1946b61d59ffSTrond Myklebust 	}
1947b61d59ffSTrond Myklebust 	return sock;
1948b61d59ffSTrond Myklebust out_err:
1949b61d59ffSTrond Myklebust 	return ERR_PTR(-EIO);
1950b61d59ffSTrond Myklebust }
1951b61d59ffSTrond Myklebust 
1952b61d59ffSTrond Myklebust /**
1953a246b010SChuck Lever  * xs_tcp_connect_worker4 - connect a TCP socket to a remote endpoint
1954a246b010SChuck Lever  * @work: RPC transport to connect
1955a246b010SChuck Lever  *
1956a246b010SChuck Lever  * Invoked by a work queue tasklet.
1957a246b010SChuck Lever  */
1958a246b010SChuck Lever static void xs_tcp_connect_worker4(struct work_struct *work)
1959a246b010SChuck Lever {
1960a246b010SChuck Lever 	struct sock_xprt *transport =
1961a246b010SChuck Lever 		container_of(work, struct sock_xprt, connect_worker.work);
1962a246b010SChuck Lever 	struct rpc_xprt *xprt = &transport->xprt;
1963a246b010SChuck Lever 
1964b61d59ffSTrond Myklebust 	xs_tcp_setup_socket(xprt, transport, xs_create_tcp_sock4);
1965b61d59ffSTrond Myklebust }
1966a246b010SChuck Lever 
1967b61d59ffSTrond Myklebust static struct socket *xs_create_tcp_sock6(struct rpc_xprt *xprt,
1968b61d59ffSTrond Myklebust 		struct sock_xprt *transport)
1969b61d59ffSTrond Myklebust {
1970b61d59ffSTrond Myklebust 	struct socket *sock;
1971b61d59ffSTrond Myklebust 	int err;
1972b61d59ffSTrond Myklebust 
1973a246b010SChuck Lever 	/* start from scratch */
1974b61d59ffSTrond Myklebust 	err = sock_create_kern(PF_INET6, SOCK_STREAM, IPPROTO_TCP, &sock);
1975b61d59ffSTrond Myklebust 	if (err < 0) {
1976b61d59ffSTrond Myklebust 		dprintk("RPC:       can't create TCP transport socket (%d).\n",
1977b61d59ffSTrond Myklebust 				-err);
1978b61d59ffSTrond Myklebust 		goto out_err;
19799903cd1cSChuck Lever 	}
1980b61d59ffSTrond Myklebust 	xs_reclassify_socket6(sock);
19819903cd1cSChuck Lever 
1982b61d59ffSTrond Myklebust 	if (xs_bind6(transport, sock) < 0) {
1983a246b010SChuck Lever 		sock_release(sock);
1984b61d59ffSTrond Myklebust 		goto out_err;
1985a246b010SChuck Lever 	}
1986b61d59ffSTrond Myklebust 	return sock;
1987b61d59ffSTrond Myklebust out_err:
1988b61d59ffSTrond Myklebust 	return ERR_PTR(-EIO);
1989a246b010SChuck Lever }
1990a246b010SChuck Lever 
1991a246b010SChuck Lever /**
199268e220bdSChuck Lever  * xs_tcp_connect_worker6 - connect a TCP socket to a remote endpoint
199368e220bdSChuck Lever  * @work: RPC transport to connect
199468e220bdSChuck Lever  *
199568e220bdSChuck Lever  * Invoked by a work queue tasklet.
199668e220bdSChuck Lever  */
199768e220bdSChuck Lever static void xs_tcp_connect_worker6(struct work_struct *work)
199868e220bdSChuck Lever {
199968e220bdSChuck Lever 	struct sock_xprt *transport =
200068e220bdSChuck Lever 		container_of(work, struct sock_xprt, connect_worker.work);
200168e220bdSChuck Lever 	struct rpc_xprt *xprt = &transport->xprt;
200268e220bdSChuck Lever 
2003b61d59ffSTrond Myklebust 	xs_tcp_setup_socket(xprt, transport, xs_create_tcp_sock6);
200468e220bdSChuck Lever }
200568e220bdSChuck Lever 
200668e220bdSChuck Lever /**
2007a246b010SChuck Lever  * xs_connect - connect a socket to a remote endpoint
2008a246b010SChuck Lever  * @task: address of RPC task that manages state of connect request
2009a246b010SChuck Lever  *
2010a246b010SChuck Lever  * TCP: If the remote end dropped the connection, delay reconnecting.
201103bf4b70SChuck Lever  *
201203bf4b70SChuck Lever  * UDP socket connects are synchronous, but we use a work queue anyway
201303bf4b70SChuck Lever  * to guarantee that even unprivileged user processes can set up a
201403bf4b70SChuck Lever  * socket on a privileged port.
201503bf4b70SChuck Lever  *
201603bf4b70SChuck Lever  * If a UDP socket connect fails, the delay behavior here prevents
201703bf4b70SChuck Lever  * retry floods (hard mounts).
2018a246b010SChuck Lever  */
2019a246b010SChuck Lever static void xs_connect(struct rpc_task *task)
2020a246b010SChuck Lever {
2021a246b010SChuck Lever 	struct rpc_xprt *xprt = task->tk_xprt;
2022ee0ac0c2SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2023a246b010SChuck Lever 
202409a21c41SChuck Lever 	if (transport->sock != NULL && !RPC_IS_SOFTCONN(task)) {
202546121cf7SChuck Lever 		dprintk("RPC:       xs_connect delayed xprt %p for %lu "
202646121cf7SChuck Lever 				"seconds\n",
202703bf4b70SChuck Lever 				xprt, xprt->reestablish_timeout / HZ);
2028c1384c9cSTrond Myklebust 		queue_delayed_work(rpciod_workqueue,
2029c1384c9cSTrond Myklebust 				   &transport->connect_worker,
203003bf4b70SChuck Lever 				   xprt->reestablish_timeout);
203103bf4b70SChuck Lever 		xprt->reestablish_timeout <<= 1;
203261d0a8e6SNeil Brown 		if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
203361d0a8e6SNeil Brown 			xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
203403bf4b70SChuck Lever 		if (xprt->reestablish_timeout > XS_TCP_MAX_REEST_TO)
203503bf4b70SChuck Lever 			xprt->reestablish_timeout = XS_TCP_MAX_REEST_TO;
20369903cd1cSChuck Lever 	} else {
20379903cd1cSChuck Lever 		dprintk("RPC:       xs_connect scheduled xprt %p\n", xprt);
2038c1384c9cSTrond Myklebust 		queue_delayed_work(rpciod_workqueue,
2039c1384c9cSTrond Myklebust 				   &transport->connect_worker, 0);
2040a246b010SChuck Lever 	}
2041a246b010SChuck Lever }
2042a246b010SChuck Lever 
2043262ca07dSChuck Lever /**
2044262ca07dSChuck Lever  * xs_udp_print_stats - display UDP socket-specifc stats
2045262ca07dSChuck Lever  * @xprt: rpc_xprt struct containing statistics
2046262ca07dSChuck Lever  * @seq: output file
2047262ca07dSChuck Lever  *
2048262ca07dSChuck Lever  */
2049262ca07dSChuck Lever static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2050262ca07dSChuck Lever {
2051c8475461SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2052c8475461SChuck Lever 
2053262ca07dSChuck Lever 	seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %Lu %Lu\n",
2054fbfffbd5SChuck Lever 			transport->srcport,
2055262ca07dSChuck Lever 			xprt->stat.bind_count,
2056262ca07dSChuck Lever 			xprt->stat.sends,
2057262ca07dSChuck Lever 			xprt->stat.recvs,
2058262ca07dSChuck Lever 			xprt->stat.bad_xids,
2059262ca07dSChuck Lever 			xprt->stat.req_u,
2060262ca07dSChuck Lever 			xprt->stat.bklog_u);
2061262ca07dSChuck Lever }
2062262ca07dSChuck Lever 
2063262ca07dSChuck Lever /**
2064262ca07dSChuck Lever  * xs_tcp_print_stats - display TCP socket-specifc stats
2065262ca07dSChuck Lever  * @xprt: rpc_xprt struct containing statistics
2066262ca07dSChuck Lever  * @seq: output file
2067262ca07dSChuck Lever  *
2068262ca07dSChuck Lever  */
2069262ca07dSChuck Lever static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2070262ca07dSChuck Lever {
2071c8475461SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2072262ca07dSChuck Lever 	long idle_time = 0;
2073262ca07dSChuck Lever 
2074262ca07dSChuck Lever 	if (xprt_connected(xprt))
2075262ca07dSChuck Lever 		idle_time = (long)(jiffies - xprt->last_used) / HZ;
2076262ca07dSChuck Lever 
2077262ca07dSChuck Lever 	seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu %Lu %Lu\n",
2078fbfffbd5SChuck Lever 			transport->srcport,
2079262ca07dSChuck Lever 			xprt->stat.bind_count,
2080262ca07dSChuck Lever 			xprt->stat.connect_count,
2081262ca07dSChuck Lever 			xprt->stat.connect_time,
2082262ca07dSChuck Lever 			idle_time,
2083262ca07dSChuck Lever 			xprt->stat.sends,
2084262ca07dSChuck Lever 			xprt->stat.recvs,
2085262ca07dSChuck Lever 			xprt->stat.bad_xids,
2086262ca07dSChuck Lever 			xprt->stat.req_u,
2087262ca07dSChuck Lever 			xprt->stat.bklog_u);
2088262ca07dSChuck Lever }
2089262ca07dSChuck Lever 
20904cfc7e60SRahul Iyer /*
20914cfc7e60SRahul Iyer  * Allocate a bunch of pages for a scratch buffer for the rpc code. The reason
20924cfc7e60SRahul Iyer  * we allocate pages instead doing a kmalloc like rpc_malloc is because we want
20934cfc7e60SRahul Iyer  * to use the server side send routines.
20944cfc7e60SRahul Iyer  */
20955a51f13aSH Hartley Sweeten static void *bc_malloc(struct rpc_task *task, size_t size)
20964cfc7e60SRahul Iyer {
20974cfc7e60SRahul Iyer 	struct page *page;
20984cfc7e60SRahul Iyer 	struct rpc_buffer *buf;
20994cfc7e60SRahul Iyer 
21004cfc7e60SRahul Iyer 	BUG_ON(size > PAGE_SIZE - sizeof(struct rpc_buffer));
21014cfc7e60SRahul Iyer 	page = alloc_page(GFP_KERNEL);
21024cfc7e60SRahul Iyer 
21034cfc7e60SRahul Iyer 	if (!page)
21044cfc7e60SRahul Iyer 		return NULL;
21054cfc7e60SRahul Iyer 
21064cfc7e60SRahul Iyer 	buf = page_address(page);
21074cfc7e60SRahul Iyer 	buf->len = PAGE_SIZE;
21084cfc7e60SRahul Iyer 
21094cfc7e60SRahul Iyer 	return buf->data;
21104cfc7e60SRahul Iyer }
21114cfc7e60SRahul Iyer 
21124cfc7e60SRahul Iyer /*
21134cfc7e60SRahul Iyer  * Free the space allocated in the bc_alloc routine
21144cfc7e60SRahul Iyer  */
21155a51f13aSH Hartley Sweeten static void bc_free(void *buffer)
21164cfc7e60SRahul Iyer {
21174cfc7e60SRahul Iyer 	struct rpc_buffer *buf;
21184cfc7e60SRahul Iyer 
21194cfc7e60SRahul Iyer 	if (!buffer)
21204cfc7e60SRahul Iyer 		return;
21214cfc7e60SRahul Iyer 
21224cfc7e60SRahul Iyer 	buf = container_of(buffer, struct rpc_buffer, data);
21234cfc7e60SRahul Iyer 	free_page((unsigned long)buf);
21244cfc7e60SRahul Iyer }
21254cfc7e60SRahul Iyer 
21264cfc7e60SRahul Iyer /*
21274cfc7e60SRahul Iyer  * Use the svc_sock to send the callback. Must be called with svsk->sk_mutex
21284cfc7e60SRahul Iyer  * held. Borrows heavily from svc_tcp_sendto and xs_tcp_send_request.
21294cfc7e60SRahul Iyer  */
21304cfc7e60SRahul Iyer static int bc_sendto(struct rpc_rqst *req)
21314cfc7e60SRahul Iyer {
21324cfc7e60SRahul Iyer 	int len;
21334cfc7e60SRahul Iyer 	struct xdr_buf *xbufp = &req->rq_snd_buf;
21344cfc7e60SRahul Iyer 	struct rpc_xprt *xprt = req->rq_xprt;
21354cfc7e60SRahul Iyer 	struct sock_xprt *transport =
21364cfc7e60SRahul Iyer 				container_of(xprt, struct sock_xprt, xprt);
21374cfc7e60SRahul Iyer 	struct socket *sock = transport->sock;
21384cfc7e60SRahul Iyer 	unsigned long headoff;
21394cfc7e60SRahul Iyer 	unsigned long tailoff;
21404cfc7e60SRahul Iyer 
21414cfc7e60SRahul Iyer 	/*
21424cfc7e60SRahul Iyer 	 * Set up the rpc header and record marker stuff
21434cfc7e60SRahul Iyer 	 */
21444cfc7e60SRahul Iyer 	xs_encode_tcp_record_marker(xbufp);
21454cfc7e60SRahul Iyer 
21464cfc7e60SRahul Iyer 	tailoff = (unsigned long)xbufp->tail[0].iov_base & ~PAGE_MASK;
21474cfc7e60SRahul Iyer 	headoff = (unsigned long)xbufp->head[0].iov_base & ~PAGE_MASK;
21484cfc7e60SRahul Iyer 	len = svc_send_common(sock, xbufp,
21494cfc7e60SRahul Iyer 			      virt_to_page(xbufp->head[0].iov_base), headoff,
21504cfc7e60SRahul Iyer 			      xbufp->tail[0].iov_base, tailoff);
21514cfc7e60SRahul Iyer 
21524cfc7e60SRahul Iyer 	if (len != xbufp->len) {
21534cfc7e60SRahul Iyer 		printk(KERN_NOTICE "Error sending entire callback!\n");
21544cfc7e60SRahul Iyer 		len = -EAGAIN;
21554cfc7e60SRahul Iyer 	}
21564cfc7e60SRahul Iyer 
21574cfc7e60SRahul Iyer 	return len;
21584cfc7e60SRahul Iyer }
21594cfc7e60SRahul Iyer 
21604cfc7e60SRahul Iyer /*
21614cfc7e60SRahul Iyer  * The send routine. Borrows from svc_send
21624cfc7e60SRahul Iyer  */
21634cfc7e60SRahul Iyer static int bc_send_request(struct rpc_task *task)
21644cfc7e60SRahul Iyer {
21654cfc7e60SRahul Iyer 	struct rpc_rqst *req = task->tk_rqstp;
21664cfc7e60SRahul Iyer 	struct svc_xprt	*xprt;
21674cfc7e60SRahul Iyer 	struct svc_sock         *svsk;
21684cfc7e60SRahul Iyer 	u32                     len;
21694cfc7e60SRahul Iyer 
21704cfc7e60SRahul Iyer 	dprintk("sending request with xid: %08x\n", ntohl(req->rq_xid));
21714cfc7e60SRahul Iyer 	/*
21724cfc7e60SRahul Iyer 	 * Get the server socket associated with this callback xprt
21734cfc7e60SRahul Iyer 	 */
21744cfc7e60SRahul Iyer 	xprt = req->rq_xprt->bc_xprt;
21754cfc7e60SRahul Iyer 	svsk = container_of(xprt, struct svc_sock, sk_xprt);
21764cfc7e60SRahul Iyer 
21774cfc7e60SRahul Iyer 	/*
21784cfc7e60SRahul Iyer 	 * Grab the mutex to serialize data as the connection is shared
21794cfc7e60SRahul Iyer 	 * with the fore channel
21804cfc7e60SRahul Iyer 	 */
21814cfc7e60SRahul Iyer 	if (!mutex_trylock(&xprt->xpt_mutex)) {
21824cfc7e60SRahul Iyer 		rpc_sleep_on(&xprt->xpt_bc_pending, task, NULL);
21834cfc7e60SRahul Iyer 		if (!mutex_trylock(&xprt->xpt_mutex))
21844cfc7e60SRahul Iyer 			return -EAGAIN;
21854cfc7e60SRahul Iyer 		rpc_wake_up_queued_task(&xprt->xpt_bc_pending, task);
21864cfc7e60SRahul Iyer 	}
21874cfc7e60SRahul Iyer 	if (test_bit(XPT_DEAD, &xprt->xpt_flags))
21884cfc7e60SRahul Iyer 		len = -ENOTCONN;
21894cfc7e60SRahul Iyer 	else
21904cfc7e60SRahul Iyer 		len = bc_sendto(req);
21914cfc7e60SRahul Iyer 	mutex_unlock(&xprt->xpt_mutex);
21924cfc7e60SRahul Iyer 
21934cfc7e60SRahul Iyer 	if (len > 0)
21944cfc7e60SRahul Iyer 		len = 0;
21954cfc7e60SRahul Iyer 
21964cfc7e60SRahul Iyer 	return len;
21974cfc7e60SRahul Iyer }
21984cfc7e60SRahul Iyer 
21994cfc7e60SRahul Iyer /*
22004cfc7e60SRahul Iyer  * The close routine. Since this is client initiated, we do nothing
22014cfc7e60SRahul Iyer  */
22024cfc7e60SRahul Iyer 
22034cfc7e60SRahul Iyer static void bc_close(struct rpc_xprt *xprt)
22044cfc7e60SRahul Iyer {
22054cfc7e60SRahul Iyer }
22064cfc7e60SRahul Iyer 
22074cfc7e60SRahul Iyer /*
22084cfc7e60SRahul Iyer  * The xprt destroy routine. Again, because this connection is client
22094cfc7e60SRahul Iyer  * initiated, we do nothing
22104cfc7e60SRahul Iyer  */
22114cfc7e60SRahul Iyer 
22124cfc7e60SRahul Iyer static void bc_destroy(struct rpc_xprt *xprt)
22134cfc7e60SRahul Iyer {
22144cfc7e60SRahul Iyer }
22154cfc7e60SRahul Iyer 
2216262965f5SChuck Lever static struct rpc_xprt_ops xs_udp_ops = {
221743118c29SChuck Lever 	.set_buffer_size	= xs_udp_set_buffer_size,
221812a80469SChuck Lever 	.reserve_xprt		= xprt_reserve_xprt_cong,
221949e9a890SChuck Lever 	.release_xprt		= xprt_release_xprt_cong,
222045160d62SChuck Lever 	.rpcbind		= rpcb_getport_async,
222192200412SChuck Lever 	.set_port		= xs_set_port,
22229903cd1cSChuck Lever 	.connect		= xs_connect,
222302107148SChuck Lever 	.buf_alloc		= rpc_malloc,
222402107148SChuck Lever 	.buf_free		= rpc_free,
2225262965f5SChuck Lever 	.send_request		= xs_udp_send_request,
2226fe3aca29SChuck Lever 	.set_retrans_timeout	= xprt_set_retrans_timeout_rtt,
222746c0ee8bSChuck Lever 	.timer			= xs_udp_timer,
2228a58dd398SChuck Lever 	.release_request	= xprt_release_rqst_cong,
2229262965f5SChuck Lever 	.close			= xs_close,
2230262965f5SChuck Lever 	.destroy		= xs_destroy,
2231262ca07dSChuck Lever 	.print_stats		= xs_udp_print_stats,
2232262965f5SChuck Lever };
2233262965f5SChuck Lever 
2234262965f5SChuck Lever static struct rpc_xprt_ops xs_tcp_ops = {
223512a80469SChuck Lever 	.reserve_xprt		= xprt_reserve_xprt,
2236e0ab53deSTrond Myklebust 	.release_xprt		= xs_tcp_release_xprt,
223745160d62SChuck Lever 	.rpcbind		= rpcb_getport_async,
223892200412SChuck Lever 	.set_port		= xs_set_port,
22390b9e7943STrond Myklebust 	.connect		= xs_connect,
224002107148SChuck Lever 	.buf_alloc		= rpc_malloc,
224102107148SChuck Lever 	.buf_free		= rpc_free,
2242262965f5SChuck Lever 	.send_request		= xs_tcp_send_request,
2243fe3aca29SChuck Lever 	.set_retrans_timeout	= xprt_set_retrans_timeout_def,
2244f75e6745STrond Myklebust 	.close			= xs_tcp_close,
22459903cd1cSChuck Lever 	.destroy		= xs_destroy,
2246262ca07dSChuck Lever 	.print_stats		= xs_tcp_print_stats,
2247a246b010SChuck Lever };
2248a246b010SChuck Lever 
22494cfc7e60SRahul Iyer /*
22504cfc7e60SRahul Iyer  * The rpc_xprt_ops for the server backchannel
22514cfc7e60SRahul Iyer  */
22524cfc7e60SRahul Iyer 
22534cfc7e60SRahul Iyer static struct rpc_xprt_ops bc_tcp_ops = {
22544cfc7e60SRahul Iyer 	.reserve_xprt		= xprt_reserve_xprt,
22554cfc7e60SRahul Iyer 	.release_xprt		= xprt_release_xprt,
22564cfc7e60SRahul Iyer 	.buf_alloc		= bc_malloc,
22574cfc7e60SRahul Iyer 	.buf_free		= bc_free,
22584cfc7e60SRahul Iyer 	.send_request		= bc_send_request,
22594cfc7e60SRahul Iyer 	.set_retrans_timeout	= xprt_set_retrans_timeout_def,
22604cfc7e60SRahul Iyer 	.close			= bc_close,
22614cfc7e60SRahul Iyer 	.destroy		= bc_destroy,
22624cfc7e60SRahul Iyer 	.print_stats		= xs_tcp_print_stats,
22634cfc7e60SRahul Iyer };
22644cfc7e60SRahul Iyer 
22653c341b0bS\"Talpey, Thomas\ static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args,
2266bc25571eS\"Talpey, Thomas\ 				      unsigned int slot_table_size)
2267c8541ecdSChuck Lever {
2268c8541ecdSChuck Lever 	struct rpc_xprt *xprt;
2269ffc2e518SChuck Lever 	struct sock_xprt *new;
2270c8541ecdSChuck Lever 
227196802a09SFrank van Maarseveen 	if (args->addrlen > sizeof(xprt->addr)) {
2272c8541ecdSChuck Lever 		dprintk("RPC:       xs_setup_xprt: address too large\n");
2273c8541ecdSChuck Lever 		return ERR_PTR(-EBADF);
2274c8541ecdSChuck Lever 	}
2275c8541ecdSChuck Lever 
2276ffc2e518SChuck Lever 	new = kzalloc(sizeof(*new), GFP_KERNEL);
2277ffc2e518SChuck Lever 	if (new == NULL) {
227846121cf7SChuck Lever 		dprintk("RPC:       xs_setup_xprt: couldn't allocate "
227946121cf7SChuck Lever 				"rpc_xprt\n");
2280c8541ecdSChuck Lever 		return ERR_PTR(-ENOMEM);
2281c8541ecdSChuck Lever 	}
2282ffc2e518SChuck Lever 	xprt = &new->xprt;
2283c8541ecdSChuck Lever 
2284c8541ecdSChuck Lever 	xprt->max_reqs = slot_table_size;
2285c8541ecdSChuck Lever 	xprt->slot = kcalloc(xprt->max_reqs, sizeof(struct rpc_rqst), GFP_KERNEL);
2286c8541ecdSChuck Lever 	if (xprt->slot == NULL) {
2287c8541ecdSChuck Lever 		kfree(xprt);
228846121cf7SChuck Lever 		dprintk("RPC:       xs_setup_xprt: couldn't allocate slot "
228946121cf7SChuck Lever 				"table\n");
2290c8541ecdSChuck Lever 		return ERR_PTR(-ENOMEM);
2291c8541ecdSChuck Lever 	}
2292c8541ecdSChuck Lever 
229396802a09SFrank van Maarseveen 	memcpy(&xprt->addr, args->dstaddr, args->addrlen);
229496802a09SFrank van Maarseveen 	xprt->addrlen = args->addrlen;
2295d3bc9a1dSFrank van Maarseveen 	if (args->srcaddr)
2296fbfffbd5SChuck Lever 		memcpy(&new->srcaddr, args->srcaddr, args->addrlen);
2297c8541ecdSChuck Lever 
2298c8541ecdSChuck Lever 	return xprt;
2299c8541ecdSChuck Lever }
2300c8541ecdSChuck Lever 
23012881ae74STrond Myklebust static const struct rpc_timeout xs_udp_default_timeout = {
23022881ae74STrond Myklebust 	.to_initval = 5 * HZ,
23032881ae74STrond Myklebust 	.to_maxval = 30 * HZ,
23042881ae74STrond Myklebust 	.to_increment = 5 * HZ,
23052881ae74STrond Myklebust 	.to_retries = 5,
23062881ae74STrond Myklebust };
23072881ae74STrond Myklebust 
23089903cd1cSChuck Lever /**
23099903cd1cSChuck Lever  * xs_setup_udp - Set up transport to use a UDP socket
231096802a09SFrank van Maarseveen  * @args: rpc transport creation arguments
23119903cd1cSChuck Lever  *
23129903cd1cSChuck Lever  */
2313483066d6SAdrian Bunk static struct rpc_xprt *xs_setup_udp(struct xprt_create *args)
2314a246b010SChuck Lever {
23158f9d5b1aSChuck Lever 	struct sockaddr *addr = args->dstaddr;
2316c8541ecdSChuck Lever 	struct rpc_xprt *xprt;
2317c8475461SChuck Lever 	struct sock_xprt *transport;
23180a68b0beSJ. Bruce Fields 	struct rpc_xprt *ret;
2319a246b010SChuck Lever 
232096802a09SFrank van Maarseveen 	xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries);
2321c8541ecdSChuck Lever 	if (IS_ERR(xprt))
2322c8541ecdSChuck Lever 		return xprt;
2323c8475461SChuck Lever 	transport = container_of(xprt, struct sock_xprt, xprt);
2324a246b010SChuck Lever 
2325ec739ef0SChuck Lever 	xprt->prot = IPPROTO_UDP;
2326808012fbSChuck Lever 	xprt->tsh_size = 0;
2327a246b010SChuck Lever 	/* XXX: header size can vary due to auth type, IPv6, etc. */
2328a246b010SChuck Lever 	xprt->max_payload = (1U << 16) - (MAX_HEADER << 3);
2329a246b010SChuck Lever 
233003bf4b70SChuck Lever 	xprt->bind_timeout = XS_BIND_TO;
233103bf4b70SChuck Lever 	xprt->reestablish_timeout = XS_UDP_REEST_TO;
233203bf4b70SChuck Lever 	xprt->idle_timeout = XS_IDLE_DISC_TO;
2333a246b010SChuck Lever 
2334262965f5SChuck Lever 	xprt->ops = &xs_udp_ops;
2335a246b010SChuck Lever 
2336ba7392bbSTrond Myklebust 	xprt->timeout = &xs_udp_default_timeout;
2337a246b010SChuck Lever 
23388f9d5b1aSChuck Lever 	switch (addr->sa_family) {
23398f9d5b1aSChuck Lever 	case AF_INET:
23408f9d5b1aSChuck Lever 		if (((struct sockaddr_in *)addr)->sin_port != htons(0))
23418f9d5b1aSChuck Lever 			xprt_set_bound(xprt);
23428f9d5b1aSChuck Lever 
23438f9d5b1aSChuck Lever 		INIT_DELAYED_WORK(&transport->connect_worker,
23448f9d5b1aSChuck Lever 					xs_udp_connect_worker4);
23459dc3b095SChuck Lever 		xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP);
23468f9d5b1aSChuck Lever 		break;
23478f9d5b1aSChuck Lever 	case AF_INET6:
23488f9d5b1aSChuck Lever 		if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
23498f9d5b1aSChuck Lever 			xprt_set_bound(xprt);
23508f9d5b1aSChuck Lever 
23518f9d5b1aSChuck Lever 		INIT_DELAYED_WORK(&transport->connect_worker,
23528f9d5b1aSChuck Lever 					xs_udp_connect_worker6);
23539dc3b095SChuck Lever 		xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6);
23548f9d5b1aSChuck Lever 		break;
23558f9d5b1aSChuck Lever 	default:
23560a68b0beSJ. Bruce Fields 		ret = ERR_PTR(-EAFNOSUPPORT);
23570a68b0beSJ. Bruce Fields 		goto out_err;
23588f9d5b1aSChuck Lever 	}
23598f9d5b1aSChuck Lever 
2360c740eff8SChuck Lever 	if (xprt_bound(xprt))
2361c740eff8SChuck Lever 		dprintk("RPC:       set up xprt to %s (port %s) via %s\n",
2362c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_ADDR],
2363c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_PORT],
2364c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_PROTO]);
2365c740eff8SChuck Lever 	else
2366c740eff8SChuck Lever 		dprintk("RPC:       set up xprt to %s (autobind) via %s\n",
2367c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_ADDR],
2368c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_PROTO]);
2369edb267a6SChuck Lever 
2370bc25571eS\"Talpey, Thomas\ 	if (try_module_get(THIS_MODULE))
2371c8541ecdSChuck Lever 		return xprt;
23720a68b0beSJ. Bruce Fields 	ret = ERR_PTR(-EINVAL);
23730a68b0beSJ. Bruce Fields out_err:
2374bc25571eS\"Talpey, Thomas\ 	kfree(xprt->slot);
2375bc25571eS\"Talpey, Thomas\ 	kfree(xprt);
23760a68b0beSJ. Bruce Fields 	return ret;
2377a246b010SChuck Lever }
2378a246b010SChuck Lever 
23792881ae74STrond Myklebust static const struct rpc_timeout xs_tcp_default_timeout = {
23802881ae74STrond Myklebust 	.to_initval = 60 * HZ,
23812881ae74STrond Myklebust 	.to_maxval = 60 * HZ,
23822881ae74STrond Myklebust 	.to_retries = 2,
23832881ae74STrond Myklebust };
23842881ae74STrond Myklebust 
23859903cd1cSChuck Lever /**
23869903cd1cSChuck Lever  * xs_setup_tcp - Set up transport to use a TCP socket
238796802a09SFrank van Maarseveen  * @args: rpc transport creation arguments
23889903cd1cSChuck Lever  *
23899903cd1cSChuck Lever  */
2390483066d6SAdrian Bunk static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args)
2391a246b010SChuck Lever {
23928f9d5b1aSChuck Lever 	struct sockaddr *addr = args->dstaddr;
2393c8541ecdSChuck Lever 	struct rpc_xprt *xprt;
2394c8475461SChuck Lever 	struct sock_xprt *transport;
23950a68b0beSJ. Bruce Fields 	struct rpc_xprt *ret;
2396a246b010SChuck Lever 
239796802a09SFrank van Maarseveen 	xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries);
2398c8541ecdSChuck Lever 	if (IS_ERR(xprt))
2399c8541ecdSChuck Lever 		return xprt;
2400c8475461SChuck Lever 	transport = container_of(xprt, struct sock_xprt, xprt);
2401a246b010SChuck Lever 
2402ec739ef0SChuck Lever 	xprt->prot = IPPROTO_TCP;
2403808012fbSChuck Lever 	xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
2404808012fbSChuck Lever 	xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2405a246b010SChuck Lever 
240603bf4b70SChuck Lever 	xprt->bind_timeout = XS_BIND_TO;
240703bf4b70SChuck Lever 	xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
240803bf4b70SChuck Lever 	xprt->idle_timeout = XS_IDLE_DISC_TO;
2409a246b010SChuck Lever 
2410262965f5SChuck Lever 	xprt->ops = &xs_tcp_ops;
2411ba7392bbSTrond Myklebust 	xprt->timeout = &xs_tcp_default_timeout;
2412a246b010SChuck Lever 
24138f9d5b1aSChuck Lever 	switch (addr->sa_family) {
24148f9d5b1aSChuck Lever 	case AF_INET:
24158f9d5b1aSChuck Lever 		if (((struct sockaddr_in *)addr)->sin_port != htons(0))
24168f9d5b1aSChuck Lever 			xprt_set_bound(xprt);
24178f9d5b1aSChuck Lever 
24189dc3b095SChuck Lever 		INIT_DELAYED_WORK(&transport->connect_worker,
24199dc3b095SChuck Lever 					xs_tcp_connect_worker4);
24209dc3b095SChuck Lever 		xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP);
24218f9d5b1aSChuck Lever 		break;
24228f9d5b1aSChuck Lever 	case AF_INET6:
24238f9d5b1aSChuck Lever 		if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
24248f9d5b1aSChuck Lever 			xprt_set_bound(xprt);
24258f9d5b1aSChuck Lever 
24269dc3b095SChuck Lever 		INIT_DELAYED_WORK(&transport->connect_worker,
24279dc3b095SChuck Lever 					xs_tcp_connect_worker6);
24289dc3b095SChuck Lever 		xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6);
24298f9d5b1aSChuck Lever 		break;
24308f9d5b1aSChuck Lever 	default:
24310a68b0beSJ. Bruce Fields 		ret = ERR_PTR(-EAFNOSUPPORT);
24320a68b0beSJ. Bruce Fields 		goto out_err;
24338f9d5b1aSChuck Lever 	}
24348f9d5b1aSChuck Lever 
2435c740eff8SChuck Lever 	if (xprt_bound(xprt))
2436c740eff8SChuck Lever 		dprintk("RPC:       set up xprt to %s (port %s) via %s\n",
2437c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_ADDR],
2438c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_PORT],
2439c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_PROTO]);
2440c740eff8SChuck Lever 	else
2441c740eff8SChuck Lever 		dprintk("RPC:       set up xprt to %s (autobind) via %s\n",
2442c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_ADDR],
2443c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_PROTO]);
2444c740eff8SChuck Lever 
2445edb267a6SChuck Lever 
2446bc25571eS\"Talpey, Thomas\ 	if (try_module_get(THIS_MODULE))
2447c8541ecdSChuck Lever 		return xprt;
24480a68b0beSJ. Bruce Fields 	ret = ERR_PTR(-EINVAL);
24490a68b0beSJ. Bruce Fields out_err:
2450bc25571eS\"Talpey, Thomas\ 	kfree(xprt->slot);
2451bc25571eS\"Talpey, Thomas\ 	kfree(xprt);
24520a68b0beSJ. Bruce Fields 	return ret;
2453a246b010SChuck Lever }
2454282b32e1SChuck Lever 
2455f300babaSAlexandros Batsakis /**
2456f300babaSAlexandros Batsakis  * xs_setup_bc_tcp - Set up transport to use a TCP backchannel socket
2457f300babaSAlexandros Batsakis  * @args: rpc transport creation arguments
2458f300babaSAlexandros Batsakis  *
2459f300babaSAlexandros Batsakis  */
2460f300babaSAlexandros Batsakis static struct rpc_xprt *xs_setup_bc_tcp(struct xprt_create *args)
2461f300babaSAlexandros Batsakis {
2462f300babaSAlexandros Batsakis 	struct sockaddr *addr = args->dstaddr;
2463f300babaSAlexandros Batsakis 	struct rpc_xprt *xprt;
2464f300babaSAlexandros Batsakis 	struct sock_xprt *transport;
2465f300babaSAlexandros Batsakis 	struct svc_sock *bc_sock;
24660a68b0beSJ. Bruce Fields 	struct rpc_xprt *ret;
2467f300babaSAlexandros Batsakis 
2468f300babaSAlexandros Batsakis 	xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries);
2469f300babaSAlexandros Batsakis 	if (IS_ERR(xprt))
2470f300babaSAlexandros Batsakis 		return xprt;
2471f300babaSAlexandros Batsakis 	transport = container_of(xprt, struct sock_xprt, xprt);
2472f300babaSAlexandros Batsakis 
2473f300babaSAlexandros Batsakis 	xprt->prot = IPPROTO_TCP;
2474f300babaSAlexandros Batsakis 	xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
2475f300babaSAlexandros Batsakis 	xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2476f300babaSAlexandros Batsakis 	xprt->timeout = &xs_tcp_default_timeout;
2477f300babaSAlexandros Batsakis 
2478f300babaSAlexandros Batsakis 	/* backchannel */
2479f300babaSAlexandros Batsakis 	xprt_set_bound(xprt);
2480f300babaSAlexandros Batsakis 	xprt->bind_timeout = 0;
2481f300babaSAlexandros Batsakis 	xprt->reestablish_timeout = 0;
2482f300babaSAlexandros Batsakis 	xprt->idle_timeout = 0;
2483f300babaSAlexandros Batsakis 
2484f300babaSAlexandros Batsakis 	/*
2485f300babaSAlexandros Batsakis 	 * The backchannel uses the same socket connection as the
2486f300babaSAlexandros Batsakis 	 * forechannel
2487f300babaSAlexandros Batsakis 	 */
2488f300babaSAlexandros Batsakis 	xprt->bc_xprt = args->bc_xprt;
2489f300babaSAlexandros Batsakis 	bc_sock = container_of(args->bc_xprt, struct svc_sock, sk_xprt);
2490f300babaSAlexandros Batsakis 	bc_sock->sk_bc_xprt = xprt;
2491f300babaSAlexandros Batsakis 	transport->sock = bc_sock->sk_sock;
2492f300babaSAlexandros Batsakis 	transport->inet = bc_sock->sk_sk;
2493f300babaSAlexandros Batsakis 
2494f300babaSAlexandros Batsakis 	xprt->ops = &bc_tcp_ops;
2495f300babaSAlexandros Batsakis 
2496f300babaSAlexandros Batsakis 	switch (addr->sa_family) {
2497f300babaSAlexandros Batsakis 	case AF_INET:
2498f300babaSAlexandros Batsakis 		xs_format_peer_addresses(xprt, "tcp",
2499f300babaSAlexandros Batsakis 					 RPCBIND_NETID_TCP);
2500f300babaSAlexandros Batsakis 		break;
2501f300babaSAlexandros Batsakis 	case AF_INET6:
2502f300babaSAlexandros Batsakis 		xs_format_peer_addresses(xprt, "tcp",
2503f300babaSAlexandros Batsakis 				   RPCBIND_NETID_TCP6);
2504f300babaSAlexandros Batsakis 		break;
2505f300babaSAlexandros Batsakis 	default:
25060a68b0beSJ. Bruce Fields 		ret = ERR_PTR(-EAFNOSUPPORT);
25070a68b0beSJ. Bruce Fields 		goto out_err;
2508f300babaSAlexandros Batsakis 	}
2509f300babaSAlexandros Batsakis 
2510f300babaSAlexandros Batsakis 	if (xprt_bound(xprt))
2511f300babaSAlexandros Batsakis 		dprintk("RPC:       set up xprt to %s (port %s) via %s\n",
2512f300babaSAlexandros Batsakis 				xprt->address_strings[RPC_DISPLAY_ADDR],
2513f300babaSAlexandros Batsakis 				xprt->address_strings[RPC_DISPLAY_PORT],
2514f300babaSAlexandros Batsakis 				xprt->address_strings[RPC_DISPLAY_PROTO]);
2515f300babaSAlexandros Batsakis 	else
2516f300babaSAlexandros Batsakis 		dprintk("RPC:       set up xprt to %s (autobind) via %s\n",
2517f300babaSAlexandros Batsakis 				xprt->address_strings[RPC_DISPLAY_ADDR],
2518f300babaSAlexandros Batsakis 				xprt->address_strings[RPC_DISPLAY_PROTO]);
2519f300babaSAlexandros Batsakis 
2520f300babaSAlexandros Batsakis 	/*
2521f300babaSAlexandros Batsakis 	 * Since we don't want connections for the backchannel, we set
2522f300babaSAlexandros Batsakis 	 * the xprt status to connected
2523f300babaSAlexandros Batsakis 	 */
2524f300babaSAlexandros Batsakis 	xprt_set_connected(xprt);
2525f300babaSAlexandros Batsakis 
2526f300babaSAlexandros Batsakis 
2527f300babaSAlexandros Batsakis 	if (try_module_get(THIS_MODULE))
2528f300babaSAlexandros Batsakis 		return xprt;
25290a68b0beSJ. Bruce Fields 	ret = ERR_PTR(-EINVAL);
25300a68b0beSJ. Bruce Fields out_err:
2531f300babaSAlexandros Batsakis 	kfree(xprt->slot);
2532f300babaSAlexandros Batsakis 	kfree(xprt);
25330a68b0beSJ. Bruce Fields 	return ret;
2534f300babaSAlexandros Batsakis }
2535f300babaSAlexandros Batsakis 
2536bc25571eS\"Talpey, Thomas\ static struct xprt_class	xs_udp_transport = {
2537bc25571eS\"Talpey, Thomas\ 	.list		= LIST_HEAD_INIT(xs_udp_transport.list),
2538bc25571eS\"Talpey, Thomas\ 	.name		= "udp",
2539bc25571eS\"Talpey, Thomas\ 	.owner		= THIS_MODULE,
2540f300babaSAlexandros Batsakis 	.ident		= XPRT_TRANSPORT_UDP,
2541bc25571eS\"Talpey, Thomas\ 	.setup		= xs_setup_udp,
2542bc25571eS\"Talpey, Thomas\ };
2543bc25571eS\"Talpey, Thomas\ 
2544bc25571eS\"Talpey, Thomas\ static struct xprt_class	xs_tcp_transport = {
2545bc25571eS\"Talpey, Thomas\ 	.list		= LIST_HEAD_INIT(xs_tcp_transport.list),
2546bc25571eS\"Talpey, Thomas\ 	.name		= "tcp",
2547bc25571eS\"Talpey, Thomas\ 	.owner		= THIS_MODULE,
2548f300babaSAlexandros Batsakis 	.ident		= XPRT_TRANSPORT_TCP,
2549bc25571eS\"Talpey, Thomas\ 	.setup		= xs_setup_tcp,
2550bc25571eS\"Talpey, Thomas\ };
2551bc25571eS\"Talpey, Thomas\ 
2552f300babaSAlexandros Batsakis static struct xprt_class	xs_bc_tcp_transport = {
2553f300babaSAlexandros Batsakis 	.list		= LIST_HEAD_INIT(xs_bc_tcp_transport.list),
2554f300babaSAlexandros Batsakis 	.name		= "tcp NFSv4.1 backchannel",
2555f300babaSAlexandros Batsakis 	.owner		= THIS_MODULE,
2556f300babaSAlexandros Batsakis 	.ident		= XPRT_TRANSPORT_BC_TCP,
2557f300babaSAlexandros Batsakis 	.setup		= xs_setup_bc_tcp,
2558f300babaSAlexandros Batsakis };
2559f300babaSAlexandros Batsakis 
2560282b32e1SChuck Lever /**
2561bc25571eS\"Talpey, Thomas\  * init_socket_xprt - set up xprtsock's sysctls, register with RPC client
2562282b32e1SChuck Lever  *
2563282b32e1SChuck Lever  */
2564282b32e1SChuck Lever int init_socket_xprt(void)
2565282b32e1SChuck Lever {
2566fbf76683SChuck Lever #ifdef RPC_DEBUG
25672b1bec5fSEric W. Biederman 	if (!sunrpc_table_header)
25680b4d4147SEric W. Biederman 		sunrpc_table_header = register_sysctl_table(sunrpc_table);
2569fbf76683SChuck Lever #endif
2570fbf76683SChuck Lever 
2571bc25571eS\"Talpey, Thomas\ 	xprt_register_transport(&xs_udp_transport);
2572bc25571eS\"Talpey, Thomas\ 	xprt_register_transport(&xs_tcp_transport);
2573f300babaSAlexandros Batsakis 	xprt_register_transport(&xs_bc_tcp_transport);
2574bc25571eS\"Talpey, Thomas\ 
2575282b32e1SChuck Lever 	return 0;
2576282b32e1SChuck Lever }
2577282b32e1SChuck Lever 
2578282b32e1SChuck Lever /**
2579bc25571eS\"Talpey, Thomas\  * cleanup_socket_xprt - remove xprtsock's sysctls, unregister
2580282b32e1SChuck Lever  *
2581282b32e1SChuck Lever  */
2582282b32e1SChuck Lever void cleanup_socket_xprt(void)
2583282b32e1SChuck Lever {
2584fbf76683SChuck Lever #ifdef RPC_DEBUG
2585fbf76683SChuck Lever 	if (sunrpc_table_header) {
2586fbf76683SChuck Lever 		unregister_sysctl_table(sunrpc_table_header);
2587fbf76683SChuck Lever 		sunrpc_table_header = NULL;
2588fbf76683SChuck Lever 	}
2589fbf76683SChuck Lever #endif
2590bc25571eS\"Talpey, Thomas\ 
2591bc25571eS\"Talpey, Thomas\ 	xprt_unregister_transport(&xs_udp_transport);
2592bc25571eS\"Talpey, Thomas\ 	xprt_unregister_transport(&xs_tcp_transport);
2593f300babaSAlexandros Batsakis 	xprt_unregister_transport(&xs_bc_tcp_transport);
2594282b32e1SChuck Lever }
2595cbf11071STrond Myklebust 
25969bbb9e5aSRusty Russell static int param_set_uint_minmax(const char *val,
25979bbb9e5aSRusty Russell 		const struct kernel_param *kp,
2598cbf11071STrond Myklebust 		unsigned int min, unsigned int max)
2599cbf11071STrond Myklebust {
2600cbf11071STrond Myklebust 	unsigned long num;
2601cbf11071STrond Myklebust 	int ret;
2602cbf11071STrond Myklebust 
2603cbf11071STrond Myklebust 	if (!val)
2604cbf11071STrond Myklebust 		return -EINVAL;
2605cbf11071STrond Myklebust 	ret = strict_strtoul(val, 0, &num);
2606cbf11071STrond Myklebust 	if (ret == -EINVAL || num < min || num > max)
2607cbf11071STrond Myklebust 		return -EINVAL;
2608cbf11071STrond Myklebust 	*((unsigned int *)kp->arg) = num;
2609cbf11071STrond Myklebust 	return 0;
2610cbf11071STrond Myklebust }
2611cbf11071STrond Myklebust 
26129bbb9e5aSRusty Russell static int param_set_portnr(const char *val, const struct kernel_param *kp)
2613cbf11071STrond Myklebust {
2614cbf11071STrond Myklebust 	return param_set_uint_minmax(val, kp,
2615cbf11071STrond Myklebust 			RPC_MIN_RESVPORT,
2616cbf11071STrond Myklebust 			RPC_MAX_RESVPORT);
2617cbf11071STrond Myklebust }
2618cbf11071STrond Myklebust 
26199bbb9e5aSRusty Russell static struct kernel_param_ops param_ops_portnr = {
26209bbb9e5aSRusty Russell 	.set = param_set_portnr,
26219bbb9e5aSRusty Russell 	.get = param_get_uint,
26229bbb9e5aSRusty Russell };
26239bbb9e5aSRusty Russell 
2624cbf11071STrond Myklebust #define param_check_portnr(name, p) \
2625cbf11071STrond Myklebust 	__param_check(name, p, unsigned int);
2626cbf11071STrond Myklebust 
2627cbf11071STrond Myklebust module_param_named(min_resvport, xprt_min_resvport, portnr, 0644);
2628cbf11071STrond Myklebust module_param_named(max_resvport, xprt_max_resvport, portnr, 0644);
2629cbf11071STrond Myklebust 
26309bbb9e5aSRusty Russell static int param_set_slot_table_size(const char *val,
26319bbb9e5aSRusty Russell 				     const struct kernel_param *kp)
2632cbf11071STrond Myklebust {
2633cbf11071STrond Myklebust 	return param_set_uint_minmax(val, kp,
2634cbf11071STrond Myklebust 			RPC_MIN_SLOT_TABLE,
2635cbf11071STrond Myklebust 			RPC_MAX_SLOT_TABLE);
2636cbf11071STrond Myklebust }
2637cbf11071STrond Myklebust 
26389bbb9e5aSRusty Russell static struct kernel_param_ops param_ops_slot_table_size = {
26399bbb9e5aSRusty Russell 	.set = param_set_slot_table_size,
26409bbb9e5aSRusty Russell 	.get = param_get_uint,
26419bbb9e5aSRusty Russell };
26429bbb9e5aSRusty Russell 
2643cbf11071STrond Myklebust #define param_check_slot_table_size(name, p) \
2644cbf11071STrond Myklebust 	__param_check(name, p, unsigned int);
2645cbf11071STrond Myklebust 
2646cbf11071STrond Myklebust module_param_named(tcp_slot_table_entries, xprt_tcp_slot_table_entries,
2647cbf11071STrond Myklebust 		   slot_table_size, 0644);
2648cbf11071STrond Myklebust module_param_named(udp_slot_table_entries, xprt_udp_slot_table_entries,
2649cbf11071STrond Myklebust 		   slot_table_size, 0644);
2650cbf11071STrond Myklebust 
2651