xref: /openbmc/linux/net/sunrpc/xprtsock.c (revision 0a68b0bed08eeb7ec62e0125f17856b1ccb1ea4b)
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,
21351971139SChuck Lever 				tcp_xid;
21451971139SChuck Lever 
21551971139SChuck Lever 	u32			tcp_offset,
21651971139SChuck Lever 				tcp_reclen;
21751971139SChuck Lever 
21851971139SChuck Lever 	unsigned long		tcp_copied,
21951971139SChuck Lever 				tcp_flags;
220c8475461SChuck Lever 
221c8475461SChuck Lever 	/*
222c8475461SChuck Lever 	 * Connection of transports
223c8475461SChuck Lever 	 */
22434161db6STrond Myklebust 	struct delayed_work	connect_worker;
225fbfffbd5SChuck Lever 	struct sockaddr_storage	srcaddr;
226fbfffbd5SChuck Lever 	unsigned short		srcport;
2277c6e066eSChuck Lever 
2287c6e066eSChuck Lever 	/*
2297c6e066eSChuck Lever 	 * UDP socket buffer size parameters
2307c6e066eSChuck Lever 	 */
2317c6e066eSChuck Lever 	size_t			rcvsize,
2327c6e066eSChuck Lever 				sndsize;
233314dfd79SChuck Lever 
234314dfd79SChuck Lever 	/*
235314dfd79SChuck Lever 	 * Saved socket callback addresses
236314dfd79SChuck Lever 	 */
237314dfd79SChuck Lever 	void			(*old_data_ready)(struct sock *, int);
238314dfd79SChuck Lever 	void			(*old_state_change)(struct sock *);
239314dfd79SChuck Lever 	void			(*old_write_space)(struct sock *);
2402a9e1cfaSTrond Myklebust 	void			(*old_error_report)(struct sock *);
241ffc2e518SChuck Lever };
242ffc2e518SChuck Lever 
243e136d092SChuck Lever /*
244e136d092SChuck Lever  * TCP receive state flags
245e136d092SChuck Lever  */
246e136d092SChuck Lever #define TCP_RCV_LAST_FRAG	(1UL << 0)
247e136d092SChuck Lever #define TCP_RCV_COPY_FRAGHDR	(1UL << 1)
248e136d092SChuck Lever #define TCP_RCV_COPY_XID	(1UL << 2)
249e136d092SChuck Lever #define TCP_RCV_COPY_DATA	(1UL << 3)
250f4a2e418SRicardo Labiaga #define TCP_RCV_READ_CALLDIR	(1UL << 4)
251f4a2e418SRicardo Labiaga #define TCP_RCV_COPY_CALLDIR	(1UL << 5)
25218dca02aSRicardo Labiaga 
25318dca02aSRicardo Labiaga /*
25418dca02aSRicardo Labiaga  * TCP RPC flags
25518dca02aSRicardo Labiaga  */
256f4a2e418SRicardo Labiaga #define TCP_RPC_REPLY		(1UL << 6)
257e136d092SChuck Lever 
25895392c59SChuck Lever static inline struct sockaddr *xs_addr(struct rpc_xprt *xprt)
259edb267a6SChuck Lever {
26095392c59SChuck Lever 	return (struct sockaddr *) &xprt->addr;
26195392c59SChuck Lever }
26295392c59SChuck Lever 
26395392c59SChuck Lever static inline struct sockaddr_in *xs_addr_in(struct rpc_xprt *xprt)
26495392c59SChuck Lever {
26595392c59SChuck Lever 	return (struct sockaddr_in *) &xprt->addr;
26695392c59SChuck Lever }
26795392c59SChuck Lever 
26895392c59SChuck Lever static inline struct sockaddr_in6 *xs_addr_in6(struct rpc_xprt *xprt)
26995392c59SChuck Lever {
27095392c59SChuck Lever 	return (struct sockaddr_in6 *) &xprt->addr;
27195392c59SChuck Lever }
27295392c59SChuck Lever 
273c877b849SChuck Lever static void xs_format_common_peer_addresses(struct rpc_xprt *xprt)
274c877b849SChuck Lever {
275c877b849SChuck Lever 	struct sockaddr *sap = xs_addr(xprt);
2769dc3b095SChuck Lever 	struct sockaddr_in6 *sin6;
2779dc3b095SChuck Lever 	struct sockaddr_in *sin;
278c877b849SChuck Lever 	char buf[128];
279c877b849SChuck Lever 
280c877b849SChuck Lever 	(void)rpc_ntop(sap, buf, sizeof(buf));
281c877b849SChuck Lever 	xprt->address_strings[RPC_DISPLAY_ADDR] = kstrdup(buf, GFP_KERNEL);
282c877b849SChuck Lever 
2839dc3b095SChuck Lever 	switch (sap->sa_family) {
2849dc3b095SChuck Lever 	case AF_INET:
2859dc3b095SChuck Lever 		sin = xs_addr_in(xprt);
286fc0b5791SJoe Perches 		snprintf(buf, sizeof(buf), "%08x", ntohl(sin->sin_addr.s_addr));
2879dc3b095SChuck Lever 		break;
2889dc3b095SChuck Lever 	case AF_INET6:
2899dc3b095SChuck Lever 		sin6 = xs_addr_in6(xprt);
290fc0b5791SJoe Perches 		snprintf(buf, sizeof(buf), "%pi6", &sin6->sin6_addr);
2919dc3b095SChuck Lever 		break;
2929dc3b095SChuck Lever 	default:
2939dc3b095SChuck Lever 		BUG();
2949dc3b095SChuck Lever 	}
2959dc3b095SChuck Lever 	xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = kstrdup(buf, GFP_KERNEL);
2969dc3b095SChuck Lever }
2979dc3b095SChuck Lever 
2989dc3b095SChuck Lever static void xs_format_common_peer_ports(struct rpc_xprt *xprt)
2999dc3b095SChuck Lever {
3009dc3b095SChuck Lever 	struct sockaddr *sap = xs_addr(xprt);
3019dc3b095SChuck Lever 	char buf[128];
3029dc3b095SChuck Lever 
30381160e66SJoe Perches 	snprintf(buf, sizeof(buf), "%u", rpc_get_port(sap));
304c877b849SChuck Lever 	xprt->address_strings[RPC_DISPLAY_PORT] = kstrdup(buf, GFP_KERNEL);
305c877b849SChuck Lever 
30681160e66SJoe Perches 	snprintf(buf, sizeof(buf), "%4hx", rpc_get_port(sap));
307c877b849SChuck Lever 	xprt->address_strings[RPC_DISPLAY_HEX_PORT] = kstrdup(buf, GFP_KERNEL);
308c877b849SChuck Lever }
309c877b849SChuck Lever 
3109dc3b095SChuck Lever static void xs_format_peer_addresses(struct rpc_xprt *xprt,
311b454ae90SChuck Lever 				     const char *protocol,
312b454ae90SChuck Lever 				     const char *netid)
313edb267a6SChuck Lever {
314b454ae90SChuck Lever 	xprt->address_strings[RPC_DISPLAY_PROTO] = protocol;
315b454ae90SChuck Lever 	xprt->address_strings[RPC_DISPLAY_NETID] = netid;
316c877b849SChuck Lever 	xs_format_common_peer_addresses(xprt);
3179dc3b095SChuck Lever 	xs_format_common_peer_ports(xprt);
318edb267a6SChuck Lever }
319edb267a6SChuck Lever 
3209dc3b095SChuck Lever static void xs_update_peer_port(struct rpc_xprt *xprt)
3214b6473fbSChuck Lever {
3229dc3b095SChuck Lever 	kfree(xprt->address_strings[RPC_DISPLAY_HEX_PORT]);
3239dc3b095SChuck Lever 	kfree(xprt->address_strings[RPC_DISPLAY_PORT]);
3244b6473fbSChuck Lever 
3259dc3b095SChuck Lever 	xs_format_common_peer_ports(xprt);
326edb267a6SChuck Lever }
327edb267a6SChuck Lever 
328edb267a6SChuck Lever static void xs_free_peer_addresses(struct rpc_xprt *xprt)
329edb267a6SChuck Lever {
33033e01dc7SChuck Lever 	unsigned int i;
33133e01dc7SChuck Lever 
33233e01dc7SChuck Lever 	for (i = 0; i < RPC_DISPLAY_MAX; i++)
33333e01dc7SChuck Lever 		switch (i) {
33433e01dc7SChuck Lever 		case RPC_DISPLAY_PROTO:
33533e01dc7SChuck Lever 		case RPC_DISPLAY_NETID:
33633e01dc7SChuck Lever 			continue;
33733e01dc7SChuck Lever 		default:
33833e01dc7SChuck Lever 			kfree(xprt->address_strings[i]);
33933e01dc7SChuck Lever 		}
340edb267a6SChuck Lever }
341edb267a6SChuck Lever 
342b4b5cc85SChuck Lever #define XS_SENDMSG_FLAGS	(MSG_DONTWAIT | MSG_NOSIGNAL)
343b4b5cc85SChuck Lever 
34424c5684bSTrond Myklebust static int xs_send_kvec(struct socket *sock, struct sockaddr *addr, int addrlen, struct kvec *vec, unsigned int base, int more)
345b4b5cc85SChuck Lever {
346b4b5cc85SChuck Lever 	struct msghdr msg = {
347b4b5cc85SChuck Lever 		.msg_name	= addr,
348b4b5cc85SChuck Lever 		.msg_namelen	= addrlen,
34924c5684bSTrond Myklebust 		.msg_flags	= XS_SENDMSG_FLAGS | (more ? MSG_MORE : 0),
35024c5684bSTrond Myklebust 	};
35124c5684bSTrond Myklebust 	struct kvec iov = {
35224c5684bSTrond Myklebust 		.iov_base	= vec->iov_base + base,
35324c5684bSTrond Myklebust 		.iov_len	= vec->iov_len - base,
354b4b5cc85SChuck Lever 	};
355b4b5cc85SChuck Lever 
35624c5684bSTrond Myklebust 	if (iov.iov_len != 0)
357b4b5cc85SChuck Lever 		return kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len);
358b4b5cc85SChuck Lever 	return kernel_sendmsg(sock, &msg, NULL, 0, 0);
359b4b5cc85SChuck Lever }
360b4b5cc85SChuck Lever 
36124c5684bSTrond Myklebust static int xs_send_pagedata(struct socket *sock, struct xdr_buf *xdr, unsigned int base, int more)
362b4b5cc85SChuck Lever {
36324c5684bSTrond Myklebust 	struct page **ppage;
36424c5684bSTrond Myklebust 	unsigned int remainder;
36524c5684bSTrond Myklebust 	int err, sent = 0;
366b4b5cc85SChuck Lever 
36724c5684bSTrond Myklebust 	remainder = xdr->page_len - base;
36824c5684bSTrond Myklebust 	base += xdr->page_base;
36924c5684bSTrond Myklebust 	ppage = xdr->pages + (base >> PAGE_SHIFT);
37024c5684bSTrond Myklebust 	base &= ~PAGE_MASK;
37124c5684bSTrond Myklebust 	for(;;) {
37224c5684bSTrond Myklebust 		unsigned int len = min_t(unsigned int, PAGE_SIZE - base, remainder);
37324c5684bSTrond Myklebust 		int flags = XS_SENDMSG_FLAGS;
37424c5684bSTrond Myklebust 
37524c5684bSTrond Myklebust 		remainder -= len;
37624c5684bSTrond Myklebust 		if (remainder != 0 || more)
37724c5684bSTrond Myklebust 			flags |= MSG_MORE;
37824c5684bSTrond Myklebust 		err = sock->ops->sendpage(sock, *ppage, base, len, flags);
37924c5684bSTrond Myklebust 		if (remainder == 0 || err != len)
38024c5684bSTrond Myklebust 			break;
38124c5684bSTrond Myklebust 		sent += err;
38224c5684bSTrond Myklebust 		ppage++;
38324c5684bSTrond Myklebust 		base = 0;
38424c5684bSTrond Myklebust 	}
38524c5684bSTrond Myklebust 	if (sent == 0)
38624c5684bSTrond Myklebust 		return err;
38724c5684bSTrond Myklebust 	if (err > 0)
38824c5684bSTrond Myklebust 		sent += err;
38924c5684bSTrond Myklebust 	return sent;
390b4b5cc85SChuck Lever }
391b4b5cc85SChuck Lever 
3929903cd1cSChuck Lever /**
3939903cd1cSChuck Lever  * xs_sendpages - write pages directly to a socket
3949903cd1cSChuck Lever  * @sock: socket to send on
3959903cd1cSChuck Lever  * @addr: UDP only -- address of destination
3969903cd1cSChuck Lever  * @addrlen: UDP only -- length of destination address
3979903cd1cSChuck Lever  * @xdr: buffer containing this request
3989903cd1cSChuck Lever  * @base: starting position in the buffer
3999903cd1cSChuck Lever  *
400a246b010SChuck Lever  */
40124c5684bSTrond Myklebust static int xs_sendpages(struct socket *sock, struct sockaddr *addr, int addrlen, struct xdr_buf *xdr, unsigned int base)
402a246b010SChuck Lever {
40324c5684bSTrond Myklebust 	unsigned int remainder = xdr->len - base;
40424c5684bSTrond Myklebust 	int err, sent = 0;
405a246b010SChuck Lever 
406262965f5SChuck Lever 	if (unlikely(!sock))
407fba91afbSTrond Myklebust 		return -ENOTSOCK;
408262965f5SChuck Lever 
409262965f5SChuck Lever 	clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags);
41024c5684bSTrond Myklebust 	if (base != 0) {
41124c5684bSTrond Myklebust 		addr = NULL;
41224c5684bSTrond Myklebust 		addrlen = 0;
41324c5684bSTrond Myklebust 	}
414262965f5SChuck Lever 
41524c5684bSTrond Myklebust 	if (base < xdr->head[0].iov_len || addr != NULL) {
41624c5684bSTrond Myklebust 		unsigned int len = xdr->head[0].iov_len - base;
41724c5684bSTrond Myklebust 		remainder -= len;
41824c5684bSTrond Myklebust 		err = xs_send_kvec(sock, addr, addrlen, &xdr->head[0], base, remainder != 0);
41924c5684bSTrond Myklebust 		if (remainder == 0 || err != len)
420a246b010SChuck Lever 			goto out;
42124c5684bSTrond Myklebust 		sent += err;
422a246b010SChuck Lever 		base = 0;
423a246b010SChuck Lever 	} else
42424c5684bSTrond Myklebust 		base -= xdr->head[0].iov_len;
425a246b010SChuck Lever 
42624c5684bSTrond Myklebust 	if (base < xdr->page_len) {
42724c5684bSTrond Myklebust 		unsigned int len = xdr->page_len - base;
42824c5684bSTrond Myklebust 		remainder -= len;
42924c5684bSTrond Myklebust 		err = xs_send_pagedata(sock, xdr, base, remainder != 0);
43024c5684bSTrond Myklebust 		if (remainder == 0 || err != len)
431a246b010SChuck Lever 			goto out;
43224c5684bSTrond Myklebust 		sent += err;
433a246b010SChuck Lever 		base = 0;
43424c5684bSTrond Myklebust 	} else
43524c5684bSTrond Myklebust 		base -= xdr->page_len;
43624c5684bSTrond Myklebust 
43724c5684bSTrond Myklebust 	if (base >= xdr->tail[0].iov_len)
43824c5684bSTrond Myklebust 		return sent;
43924c5684bSTrond Myklebust 	err = xs_send_kvec(sock, NULL, 0, &xdr->tail[0], base, 0);
440a246b010SChuck Lever out:
44124c5684bSTrond Myklebust 	if (sent == 0)
44224c5684bSTrond Myklebust 		return err;
44324c5684bSTrond Myklebust 	if (err > 0)
44424c5684bSTrond Myklebust 		sent += err;
44524c5684bSTrond Myklebust 	return sent;
446a246b010SChuck Lever }
447a246b010SChuck Lever 
448b6ddf64fSTrond Myklebust static void xs_nospace_callback(struct rpc_task *task)
449b6ddf64fSTrond Myklebust {
450b6ddf64fSTrond Myklebust 	struct sock_xprt *transport = container_of(task->tk_rqstp->rq_xprt, struct sock_xprt, xprt);
451b6ddf64fSTrond Myklebust 
452b6ddf64fSTrond Myklebust 	transport->inet->sk_write_pending--;
453b6ddf64fSTrond Myklebust 	clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
454b6ddf64fSTrond Myklebust }
455b6ddf64fSTrond Myklebust 
4569903cd1cSChuck Lever /**
457262965f5SChuck Lever  * xs_nospace - place task on wait queue if transmit was incomplete
458262965f5SChuck Lever  * @task: task to put to sleep
4599903cd1cSChuck Lever  *
460a246b010SChuck Lever  */
4615e3771ceSTrond Myklebust static int xs_nospace(struct rpc_task *task)
462a246b010SChuck Lever {
463262965f5SChuck Lever 	struct rpc_rqst *req = task->tk_rqstp;
464262965f5SChuck Lever 	struct rpc_xprt *xprt = req->rq_xprt;
465ee0ac0c2SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
4665e3771ceSTrond Myklebust 	int ret = 0;
467a246b010SChuck Lever 
46846121cf7SChuck Lever 	dprintk("RPC: %5u xmit incomplete (%u left of %u)\n",
469262965f5SChuck Lever 			task->tk_pid, req->rq_slen - req->rq_bytes_sent,
470262965f5SChuck Lever 			req->rq_slen);
471a246b010SChuck Lever 
472262965f5SChuck Lever 	/* Protect against races with write_space */
473262965f5SChuck Lever 	spin_lock_bh(&xprt->transport_lock);
474a246b010SChuck Lever 
475262965f5SChuck Lever 	/* Don't race with disconnect */
476b6ddf64fSTrond Myklebust 	if (xprt_connected(xprt)) {
477b6ddf64fSTrond Myklebust 		if (test_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags)) {
4785e3771ceSTrond Myklebust 			ret = -EAGAIN;
479b6ddf64fSTrond Myklebust 			/*
480b6ddf64fSTrond Myklebust 			 * Notify TCP that we're limited by the application
481b6ddf64fSTrond Myklebust 			 * window size
482b6ddf64fSTrond Myklebust 			 */
483b6ddf64fSTrond Myklebust 			set_bit(SOCK_NOSPACE, &transport->sock->flags);
484b6ddf64fSTrond Myklebust 			transport->inet->sk_write_pending++;
485b6ddf64fSTrond Myklebust 			/* ...and wait for more buffer space */
486b6ddf64fSTrond Myklebust 			xprt_wait_for_buffer_space(task, xs_nospace_callback);
487b6ddf64fSTrond Myklebust 		}
488b6ddf64fSTrond Myklebust 	} else {
489b6ddf64fSTrond Myklebust 		clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
4905e3771ceSTrond Myklebust 		ret = -ENOTCONN;
491b6ddf64fSTrond Myklebust 	}
492a246b010SChuck Lever 
493262965f5SChuck Lever 	spin_unlock_bh(&xprt->transport_lock);
4945e3771ceSTrond Myklebust 	return ret;
495a246b010SChuck Lever }
496a246b010SChuck Lever 
4979903cd1cSChuck Lever /**
498262965f5SChuck Lever  * xs_udp_send_request - write an RPC request to a UDP socket
4999903cd1cSChuck Lever  * @task: address of RPC task that manages the state of an RPC request
5009903cd1cSChuck Lever  *
5019903cd1cSChuck Lever  * Return values:
5029903cd1cSChuck Lever  *        0:	The request has been sent
5039903cd1cSChuck Lever  *   EAGAIN:	The socket was blocked, please call again later to
5049903cd1cSChuck Lever  *		complete the request
505262965f5SChuck Lever  * ENOTCONN:	Caller needs to invoke connect logic then call again
5069903cd1cSChuck Lever  *    other:	Some other error occured, the request was not sent
5079903cd1cSChuck Lever  */
508262965f5SChuck Lever static int xs_udp_send_request(struct rpc_task *task)
509a246b010SChuck Lever {
510a246b010SChuck Lever 	struct rpc_rqst *req = task->tk_rqstp;
511a246b010SChuck Lever 	struct rpc_xprt *xprt = req->rq_xprt;
512ee0ac0c2SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
513262965f5SChuck Lever 	struct xdr_buf *xdr = &req->rq_snd_buf;
514262965f5SChuck Lever 	int status;
515262965f5SChuck Lever 
516262965f5SChuck Lever 	xs_pktdump("packet data:",
517262965f5SChuck Lever 				req->rq_svec->iov_base,
518262965f5SChuck Lever 				req->rq_svec->iov_len);
519262965f5SChuck Lever 
52001d37c42STrond Myklebust 	if (!xprt_bound(xprt))
52101d37c42STrond Myklebust 		return -ENOTCONN;
522ee0ac0c2SChuck Lever 	status = xs_sendpages(transport->sock,
52395392c59SChuck Lever 			      xs_addr(xprt),
524ee0ac0c2SChuck Lever 			      xprt->addrlen, xdr,
525ee0ac0c2SChuck Lever 			      req->rq_bytes_sent);
526262965f5SChuck Lever 
527262965f5SChuck Lever 	dprintk("RPC:       xs_udp_send_request(%u) = %d\n",
528262965f5SChuck Lever 			xdr->len - req->rq_bytes_sent, status);
529262965f5SChuck Lever 
5302199700fSTrond Myklebust 	if (status >= 0) {
531d60dbb20STrond Myklebust 		req->rq_xmit_bytes_sent += status;
5322199700fSTrond Myklebust 		if (status >= req->rq_slen)
533262965f5SChuck Lever 			return 0;
534262965f5SChuck Lever 		/* Still some bytes left; set up for a retry later. */
535262965f5SChuck Lever 		status = -EAGAIN;
5362199700fSTrond Myklebust 	}
537262965f5SChuck Lever 
538262965f5SChuck Lever 	switch (status) {
539fba91afbSTrond Myklebust 	case -ENOTSOCK:
540fba91afbSTrond Myklebust 		status = -ENOTCONN;
541fba91afbSTrond Myklebust 		/* Should we call xs_close() here? */
542fba91afbSTrond Myklebust 		break;
543b6ddf64fSTrond Myklebust 	case -EAGAIN:
5445e3771ceSTrond Myklebust 		status = xs_nospace(task);
545b6ddf64fSTrond Myklebust 		break;
546c8485e4dSTrond Myklebust 	default:
547c8485e4dSTrond Myklebust 		dprintk("RPC:       sendmsg returned unrecognized error %d\n",
548c8485e4dSTrond Myklebust 			-status);
549262965f5SChuck Lever 	case -ENETUNREACH:
550262965f5SChuck Lever 	case -EPIPE:
551262965f5SChuck Lever 	case -ECONNREFUSED:
552262965f5SChuck Lever 		/* When the server has died, an ICMP port unreachable message
553262965f5SChuck Lever 		 * prompts ECONNREFUSED. */
554b6ddf64fSTrond Myklebust 		clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
555262965f5SChuck Lever 	}
5565fe46e9dSBian Naimeng 
557262965f5SChuck Lever 	return status;
558262965f5SChuck Lever }
559262965f5SChuck Lever 
560e06799f9STrond Myklebust /**
561e06799f9STrond Myklebust  * xs_tcp_shutdown - gracefully shut down a TCP socket
562e06799f9STrond Myklebust  * @xprt: transport
563e06799f9STrond Myklebust  *
564e06799f9STrond Myklebust  * Initiates a graceful shutdown of the TCP socket by calling the
565e06799f9STrond Myklebust  * equivalent of shutdown(SHUT_WR);
566e06799f9STrond Myklebust  */
567e06799f9STrond Myklebust static void xs_tcp_shutdown(struct rpc_xprt *xprt)
568e06799f9STrond Myklebust {
569e06799f9STrond Myklebust 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
570e06799f9STrond Myklebust 	struct socket *sock = transport->sock;
571e06799f9STrond Myklebust 
572e06799f9STrond Myklebust 	if (sock != NULL)
573e06799f9STrond Myklebust 		kernel_sock_shutdown(sock, SHUT_WR);
574e06799f9STrond Myklebust }
575e06799f9STrond Myklebust 
576808012fbSChuck Lever static inline void xs_encode_tcp_record_marker(struct xdr_buf *buf)
577808012fbSChuck Lever {
578808012fbSChuck Lever 	u32 reclen = buf->len - sizeof(rpc_fraghdr);
579808012fbSChuck Lever 	rpc_fraghdr *base = buf->head[0].iov_base;
580808012fbSChuck Lever 	*base = htonl(RPC_LAST_STREAM_FRAGMENT | reclen);
581808012fbSChuck Lever }
582808012fbSChuck Lever 
583262965f5SChuck Lever /**
584262965f5SChuck Lever  * xs_tcp_send_request - write an RPC request to a TCP socket
585262965f5SChuck Lever  * @task: address of RPC task that manages the state of an RPC request
586262965f5SChuck Lever  *
587262965f5SChuck Lever  * Return values:
588262965f5SChuck Lever  *        0:	The request has been sent
589262965f5SChuck Lever  *   EAGAIN:	The socket was blocked, please call again later to
590262965f5SChuck Lever  *		complete the request
591262965f5SChuck Lever  * ENOTCONN:	Caller needs to invoke connect logic then call again
592262965f5SChuck Lever  *    other:	Some other error occured, the request was not sent
593262965f5SChuck Lever  *
594262965f5SChuck Lever  * XXX: In the case of soft timeouts, should we eventually give up
595262965f5SChuck Lever  *	if sendmsg is not able to make progress?
596262965f5SChuck Lever  */
597262965f5SChuck Lever static int xs_tcp_send_request(struct rpc_task *task)
598262965f5SChuck Lever {
599262965f5SChuck Lever 	struct rpc_rqst *req = task->tk_rqstp;
600262965f5SChuck Lever 	struct rpc_xprt *xprt = req->rq_xprt;
601ee0ac0c2SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
602262965f5SChuck Lever 	struct xdr_buf *xdr = &req->rq_snd_buf;
603b595bb15SChuck Lever 	int status;
604a246b010SChuck Lever 
605808012fbSChuck Lever 	xs_encode_tcp_record_marker(&req->rq_snd_buf);
606262965f5SChuck Lever 
607262965f5SChuck Lever 	xs_pktdump("packet data:",
608262965f5SChuck Lever 				req->rq_svec->iov_base,
609262965f5SChuck Lever 				req->rq_svec->iov_len);
610a246b010SChuck Lever 
611a246b010SChuck Lever 	/* Continue transmitting the packet/record. We must be careful
612a246b010SChuck Lever 	 * to cope with writespace callbacks arriving _after_ we have
613262965f5SChuck Lever 	 * called sendmsg(). */
614a246b010SChuck Lever 	while (1) {
615ee0ac0c2SChuck Lever 		status = xs_sendpages(transport->sock,
616ee0ac0c2SChuck Lever 					NULL, 0, xdr, req->rq_bytes_sent);
617a246b010SChuck Lever 
618262965f5SChuck Lever 		dprintk("RPC:       xs_tcp_send_request(%u) = %d\n",
619262965f5SChuck Lever 				xdr->len - req->rq_bytes_sent, status);
620262965f5SChuck Lever 
621262965f5SChuck Lever 		if (unlikely(status < 0))
622a246b010SChuck Lever 			break;
623a246b010SChuck Lever 
624a246b010SChuck Lever 		/* If we've sent the entire packet, immediately
625a246b010SChuck Lever 		 * reset the count of bytes sent. */
626262965f5SChuck Lever 		req->rq_bytes_sent += status;
627d60dbb20STrond Myklebust 		req->rq_xmit_bytes_sent += status;
628262965f5SChuck Lever 		if (likely(req->rq_bytes_sent >= req->rq_slen)) {
629a246b010SChuck Lever 			req->rq_bytes_sent = 0;
630a246b010SChuck Lever 			return 0;
631a246b010SChuck Lever 		}
632262965f5SChuck Lever 
63306b4b681STrond Myklebust 		if (status != 0)
63406b4b681STrond Myklebust 			continue;
635a246b010SChuck Lever 		status = -EAGAIN;
636a246b010SChuck Lever 		break;
637a246b010SChuck Lever 	}
638a246b010SChuck Lever 
639262965f5SChuck Lever 	switch (status) {
640fba91afbSTrond Myklebust 	case -ENOTSOCK:
641fba91afbSTrond Myklebust 		status = -ENOTCONN;
642fba91afbSTrond Myklebust 		/* Should we call xs_close() here? */
643fba91afbSTrond Myklebust 		break;
644262965f5SChuck Lever 	case -EAGAIN:
6455e3771ceSTrond Myklebust 		status = xs_nospace(task);
646262965f5SChuck Lever 		break;
647262965f5SChuck Lever 	default:
648262965f5SChuck Lever 		dprintk("RPC:       sendmsg returned unrecognized error %d\n",
649262965f5SChuck Lever 			-status);
650a246b010SChuck Lever 	case -ECONNRESET:
65155420c24STrond Myklebust 	case -EPIPE:
652e06799f9STrond Myklebust 		xs_tcp_shutdown(xprt);
653a246b010SChuck Lever 	case -ECONNREFUSED:
654a246b010SChuck Lever 	case -ENOTCONN:
655a246b010SChuck Lever 		clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
656a246b010SChuck Lever 	}
6575fe46e9dSBian Naimeng 
658a246b010SChuck Lever 	return status;
659a246b010SChuck Lever }
660a246b010SChuck Lever 
6619903cd1cSChuck Lever /**
662e0ab53deSTrond Myklebust  * xs_tcp_release_xprt - clean up after a tcp transmission
663e0ab53deSTrond Myklebust  * @xprt: transport
664e0ab53deSTrond Myklebust  * @task: rpc task
665e0ab53deSTrond Myklebust  *
666e0ab53deSTrond Myklebust  * This cleans up if an error causes us to abort the transmission of a request.
667e0ab53deSTrond Myklebust  * In this case, the socket may need to be reset in order to avoid confusing
668e0ab53deSTrond Myklebust  * the server.
669e0ab53deSTrond Myklebust  */
670e0ab53deSTrond Myklebust static void xs_tcp_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
671e0ab53deSTrond Myklebust {
672e0ab53deSTrond Myklebust 	struct rpc_rqst *req;
673e0ab53deSTrond Myklebust 
674e0ab53deSTrond Myklebust 	if (task != xprt->snd_task)
675e0ab53deSTrond Myklebust 		return;
676e0ab53deSTrond Myklebust 	if (task == NULL)
677e0ab53deSTrond Myklebust 		goto out_release;
678e0ab53deSTrond Myklebust 	req = task->tk_rqstp;
679e0ab53deSTrond Myklebust 	if (req->rq_bytes_sent == 0)
680e0ab53deSTrond Myklebust 		goto out_release;
681e0ab53deSTrond Myklebust 	if (req->rq_bytes_sent == req->rq_snd_buf.len)
682e0ab53deSTrond Myklebust 		goto out_release;
683e0ab53deSTrond Myklebust 	set_bit(XPRT_CLOSE_WAIT, &task->tk_xprt->state);
684e0ab53deSTrond Myklebust out_release:
685e0ab53deSTrond Myklebust 	xprt_release_xprt(xprt, task);
686e0ab53deSTrond Myklebust }
687e0ab53deSTrond Myklebust 
6882a9e1cfaSTrond Myklebust static void xs_save_old_callbacks(struct sock_xprt *transport, struct sock *sk)
6892a9e1cfaSTrond Myklebust {
6902a9e1cfaSTrond Myklebust 	transport->old_data_ready = sk->sk_data_ready;
6912a9e1cfaSTrond Myklebust 	transport->old_state_change = sk->sk_state_change;
6922a9e1cfaSTrond Myklebust 	transport->old_write_space = sk->sk_write_space;
6932a9e1cfaSTrond Myklebust 	transport->old_error_report = sk->sk_error_report;
6942a9e1cfaSTrond Myklebust }
6952a9e1cfaSTrond Myklebust 
6962a9e1cfaSTrond Myklebust static void xs_restore_old_callbacks(struct sock_xprt *transport, struct sock *sk)
6972a9e1cfaSTrond Myklebust {
6982a9e1cfaSTrond Myklebust 	sk->sk_data_ready = transport->old_data_ready;
6992a9e1cfaSTrond Myklebust 	sk->sk_state_change = transport->old_state_change;
7002a9e1cfaSTrond Myklebust 	sk->sk_write_space = transport->old_write_space;
7012a9e1cfaSTrond Myklebust 	sk->sk_error_report = transport->old_error_report;
7022a9e1cfaSTrond Myklebust }
7032a9e1cfaSTrond Myklebust 
704fe315e76SChuck Lever static void xs_reset_transport(struct sock_xprt *transport)
705a246b010SChuck Lever {
706ee0ac0c2SChuck Lever 	struct socket *sock = transport->sock;
707ee0ac0c2SChuck Lever 	struct sock *sk = transport->inet;
708a246b010SChuck Lever 
709fe315e76SChuck Lever 	if (sk == NULL)
710fe315e76SChuck Lever 		return;
7119903cd1cSChuck Lever 
712a246b010SChuck Lever 	write_lock_bh(&sk->sk_callback_lock);
713ee0ac0c2SChuck Lever 	transport->inet = NULL;
714ee0ac0c2SChuck Lever 	transport->sock = NULL;
715a246b010SChuck Lever 
716a246b010SChuck Lever 	sk->sk_user_data = NULL;
7172a9e1cfaSTrond Myklebust 
7182a9e1cfaSTrond Myklebust 	xs_restore_old_callbacks(transport, sk);
719a246b010SChuck Lever 	write_unlock_bh(&sk->sk_callback_lock);
720a246b010SChuck Lever 
721a246b010SChuck Lever 	sk->sk_no_check = 0;
722a246b010SChuck Lever 
723a246b010SChuck Lever 	sock_release(sock);
724fe315e76SChuck Lever }
725fe315e76SChuck Lever 
726fe315e76SChuck Lever /**
727fe315e76SChuck Lever  * xs_close - close a socket
728fe315e76SChuck Lever  * @xprt: transport
729fe315e76SChuck Lever  *
730fe315e76SChuck Lever  * This is used when all requests are complete; ie, no DRC state remains
731fe315e76SChuck Lever  * on the server we want to save.
732f75e6745STrond Myklebust  *
733f75e6745STrond Myklebust  * The caller _must_ be holding XPRT_LOCKED in order to avoid issues with
734f75e6745STrond Myklebust  * xs_reset_transport() zeroing the socket from underneath a writer.
735fe315e76SChuck Lever  */
736fe315e76SChuck Lever static void xs_close(struct rpc_xprt *xprt)
737fe315e76SChuck Lever {
738fe315e76SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
739fe315e76SChuck Lever 
740fe315e76SChuck Lever 	dprintk("RPC:       xs_close xprt %p\n", xprt);
741fe315e76SChuck Lever 
742fe315e76SChuck Lever 	xs_reset_transport(transport);
74361d0a8e6SNeil Brown 	xprt->reestablish_timeout = 0;
744fe315e76SChuck Lever 
745632e3bdcSTrond Myklebust 	smp_mb__before_clear_bit();
7467d1e8255STrond Myklebust 	clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
747632e3bdcSTrond Myklebust 	clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
7483b948ae5STrond Myklebust 	clear_bit(XPRT_CLOSING, &xprt->state);
749632e3bdcSTrond Myklebust 	smp_mb__after_clear_bit();
75062da3b24STrond Myklebust 	xprt_disconnect_done(xprt);
751a246b010SChuck Lever }
752a246b010SChuck Lever 
753f75e6745STrond Myklebust static void xs_tcp_close(struct rpc_xprt *xprt)
754f75e6745STrond Myklebust {
755f75e6745STrond Myklebust 	if (test_and_clear_bit(XPRT_CONNECTION_CLOSE, &xprt->state))
756f75e6745STrond Myklebust 		xs_close(xprt);
757f75e6745STrond Myklebust 	else
758f75e6745STrond Myklebust 		xs_tcp_shutdown(xprt);
759f75e6745STrond Myklebust }
760f75e6745STrond Myklebust 
7619903cd1cSChuck Lever /**
7629903cd1cSChuck Lever  * xs_destroy - prepare to shutdown a transport
7639903cd1cSChuck Lever  * @xprt: doomed transport
7649903cd1cSChuck Lever  *
7659903cd1cSChuck Lever  */
7669903cd1cSChuck Lever static void xs_destroy(struct rpc_xprt *xprt)
767a246b010SChuck Lever {
768c8475461SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
769c8475461SChuck Lever 
7709903cd1cSChuck Lever 	dprintk("RPC:       xs_destroy xprt %p\n", xprt);
7719903cd1cSChuck Lever 
772c1384c9cSTrond Myklebust 	cancel_rearming_delayed_work(&transport->connect_worker);
773a246b010SChuck Lever 
7749903cd1cSChuck Lever 	xs_close(xprt);
775edb267a6SChuck Lever 	xs_free_peer_addresses(xprt);
776a246b010SChuck Lever 	kfree(xprt->slot);
777c8541ecdSChuck Lever 	kfree(xprt);
778bc25571eS\"Talpey, Thomas\ 	module_put(THIS_MODULE);
779a246b010SChuck Lever }
780a246b010SChuck Lever 
7819903cd1cSChuck Lever static inline struct rpc_xprt *xprt_from_sock(struct sock *sk)
7829903cd1cSChuck Lever {
7839903cd1cSChuck Lever 	return (struct rpc_xprt *) sk->sk_user_data;
7849903cd1cSChuck Lever }
7859903cd1cSChuck Lever 
7869903cd1cSChuck Lever /**
7879903cd1cSChuck Lever  * xs_udp_data_ready - "data ready" callback for UDP sockets
7889903cd1cSChuck Lever  * @sk: socket with data to read
7899903cd1cSChuck Lever  * @len: how much data to read
7909903cd1cSChuck Lever  *
791a246b010SChuck Lever  */
7929903cd1cSChuck Lever static void xs_udp_data_ready(struct sock *sk, int len)
793a246b010SChuck Lever {
794a246b010SChuck Lever 	struct rpc_task *task;
795a246b010SChuck Lever 	struct rpc_xprt *xprt;
796a246b010SChuck Lever 	struct rpc_rqst *rovr;
797a246b010SChuck Lever 	struct sk_buff *skb;
798a246b010SChuck Lever 	int err, repsize, copied;
799d8ed029dSAlexey Dobriyan 	u32 _xid;
800d8ed029dSAlexey Dobriyan 	__be32 *xp;
801a246b010SChuck Lever 
802a246b010SChuck Lever 	read_lock(&sk->sk_callback_lock);
8039903cd1cSChuck Lever 	dprintk("RPC:       xs_udp_data_ready...\n");
8049903cd1cSChuck Lever 	if (!(xprt = xprt_from_sock(sk)))
805a246b010SChuck Lever 		goto out;
806a246b010SChuck Lever 
807a246b010SChuck Lever 	if ((skb = skb_recv_datagram(sk, 0, 1, &err)) == NULL)
808a246b010SChuck Lever 		goto out;
809a246b010SChuck Lever 
810a246b010SChuck Lever 	if (xprt->shutdown)
811a246b010SChuck Lever 		goto dropit;
812a246b010SChuck Lever 
813a246b010SChuck Lever 	repsize = skb->len - sizeof(struct udphdr);
814a246b010SChuck Lever 	if (repsize < 4) {
8159903cd1cSChuck Lever 		dprintk("RPC:       impossible RPC reply size %d!\n", repsize);
816a246b010SChuck Lever 		goto dropit;
817a246b010SChuck Lever 	}
818a246b010SChuck Lever 
819a246b010SChuck Lever 	/* Copy the XID from the skb... */
820a246b010SChuck Lever 	xp = skb_header_pointer(skb, sizeof(struct udphdr),
821a246b010SChuck Lever 				sizeof(_xid), &_xid);
822a246b010SChuck Lever 	if (xp == NULL)
823a246b010SChuck Lever 		goto dropit;
824a246b010SChuck Lever 
825a246b010SChuck Lever 	/* Look up and lock the request corresponding to the given XID */
8264a0f8c04SChuck Lever 	spin_lock(&xprt->transport_lock);
827a246b010SChuck Lever 	rovr = xprt_lookup_rqst(xprt, *xp);
828a246b010SChuck Lever 	if (!rovr)
829a246b010SChuck Lever 		goto out_unlock;
830a246b010SChuck Lever 	task = rovr->rq_task;
831a246b010SChuck Lever 
832a246b010SChuck Lever 	if ((copied = rovr->rq_private_buf.buflen) > repsize)
833a246b010SChuck Lever 		copied = repsize;
834a246b010SChuck Lever 
835a246b010SChuck Lever 	/* Suck it into the iovec, verify checksum if not done by hw. */
8361781f7f5SHerbert Xu 	if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) {
8371781f7f5SHerbert Xu 		UDPX_INC_STATS_BH(sk, UDP_MIB_INERRORS);
838a246b010SChuck Lever 		goto out_unlock;
8391781f7f5SHerbert Xu 	}
8401781f7f5SHerbert Xu 
8411781f7f5SHerbert Xu 	UDPX_INC_STATS_BH(sk, UDP_MIB_INDATAGRAMS);
842a246b010SChuck Lever 
843a246b010SChuck Lever 	/* Something worked... */
844adf30907SEric Dumazet 	dst_confirm(skb_dst(skb));
845a246b010SChuck Lever 
8461570c1e4SChuck Lever 	xprt_adjust_cwnd(task, copied);
8471570c1e4SChuck Lever 	xprt_complete_rqst(task, copied);
848a246b010SChuck Lever 
849a246b010SChuck Lever  out_unlock:
8504a0f8c04SChuck Lever 	spin_unlock(&xprt->transport_lock);
851a246b010SChuck Lever  dropit:
852a246b010SChuck Lever 	skb_free_datagram(sk, skb);
853a246b010SChuck Lever  out:
854a246b010SChuck Lever 	read_unlock(&sk->sk_callback_lock);
855a246b010SChuck Lever }
856a246b010SChuck Lever 
857dd456471SChuck Lever static inline void xs_tcp_read_fraghdr(struct rpc_xprt *xprt, struct xdr_skb_reader *desc)
858a246b010SChuck Lever {
85951971139SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
860a246b010SChuck Lever 	size_t len, used;
861a246b010SChuck Lever 	char *p;
862a246b010SChuck Lever 
86351971139SChuck Lever 	p = ((char *) &transport->tcp_fraghdr) + transport->tcp_offset;
86451971139SChuck Lever 	len = sizeof(transport->tcp_fraghdr) - transport->tcp_offset;
8659d292316SChuck Lever 	used = xdr_skb_read_bits(desc, p, len);
86651971139SChuck Lever 	transport->tcp_offset += used;
867a246b010SChuck Lever 	if (used != len)
868a246b010SChuck Lever 		return;
869808012fbSChuck Lever 
87051971139SChuck Lever 	transport->tcp_reclen = ntohl(transport->tcp_fraghdr);
87151971139SChuck Lever 	if (transport->tcp_reclen & RPC_LAST_STREAM_FRAGMENT)
872e136d092SChuck Lever 		transport->tcp_flags |= TCP_RCV_LAST_FRAG;
873a246b010SChuck Lever 	else
874e136d092SChuck Lever 		transport->tcp_flags &= ~TCP_RCV_LAST_FRAG;
87551971139SChuck Lever 	transport->tcp_reclen &= RPC_FRAGMENT_SIZE_MASK;
876808012fbSChuck Lever 
877e136d092SChuck Lever 	transport->tcp_flags &= ~TCP_RCV_COPY_FRAGHDR;
87851971139SChuck Lever 	transport->tcp_offset = 0;
879808012fbSChuck Lever 
880a246b010SChuck Lever 	/* Sanity check of the record length */
88118dca02aSRicardo Labiaga 	if (unlikely(transport->tcp_reclen < 8)) {
8829903cd1cSChuck Lever 		dprintk("RPC:       invalid TCP record fragment length\n");
8833ebb067dSTrond Myklebust 		xprt_force_disconnect(xprt);
8849903cd1cSChuck Lever 		return;
885a246b010SChuck Lever 	}
886a246b010SChuck Lever 	dprintk("RPC:       reading TCP record fragment of length %d\n",
88751971139SChuck Lever 			transport->tcp_reclen);
888a246b010SChuck Lever }
889a246b010SChuck Lever 
89051971139SChuck Lever static void xs_tcp_check_fraghdr(struct sock_xprt *transport)
891a246b010SChuck Lever {
89251971139SChuck Lever 	if (transport->tcp_offset == transport->tcp_reclen) {
893e136d092SChuck Lever 		transport->tcp_flags |= TCP_RCV_COPY_FRAGHDR;
89451971139SChuck Lever 		transport->tcp_offset = 0;
895e136d092SChuck Lever 		if (transport->tcp_flags & TCP_RCV_LAST_FRAG) {
896e136d092SChuck Lever 			transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
897e136d092SChuck Lever 			transport->tcp_flags |= TCP_RCV_COPY_XID;
89851971139SChuck Lever 			transport->tcp_copied = 0;
899a246b010SChuck Lever 		}
900a246b010SChuck Lever 	}
901a246b010SChuck Lever }
902a246b010SChuck Lever 
903dd456471SChuck Lever static inline void xs_tcp_read_xid(struct sock_xprt *transport, struct xdr_skb_reader *desc)
904a246b010SChuck Lever {
905a246b010SChuck Lever 	size_t len, used;
906a246b010SChuck Lever 	char *p;
907a246b010SChuck Lever 
90851971139SChuck Lever 	len = sizeof(transport->tcp_xid) - transport->tcp_offset;
909a246b010SChuck Lever 	dprintk("RPC:       reading XID (%Zu bytes)\n", len);
91051971139SChuck Lever 	p = ((char *) &transport->tcp_xid) + transport->tcp_offset;
9119d292316SChuck Lever 	used = xdr_skb_read_bits(desc, p, len);
91251971139SChuck Lever 	transport->tcp_offset += used;
913a246b010SChuck Lever 	if (used != len)
914a246b010SChuck Lever 		return;
915e136d092SChuck Lever 	transport->tcp_flags &= ~TCP_RCV_COPY_XID;
916f4a2e418SRicardo Labiaga 	transport->tcp_flags |= TCP_RCV_READ_CALLDIR;
91751971139SChuck Lever 	transport->tcp_copied = 4;
91818dca02aSRicardo Labiaga 	dprintk("RPC:       reading %s XID %08x\n",
91918dca02aSRicardo Labiaga 			(transport->tcp_flags & TCP_RPC_REPLY) ? "reply for"
92018dca02aSRicardo Labiaga 							      : "request with",
92151971139SChuck Lever 			ntohl(transport->tcp_xid));
92251971139SChuck Lever 	xs_tcp_check_fraghdr(transport);
923a246b010SChuck Lever }
924a246b010SChuck Lever 
92518dca02aSRicardo Labiaga static inline void xs_tcp_read_calldir(struct sock_xprt *transport,
92618dca02aSRicardo Labiaga 				       struct xdr_skb_reader *desc)
927a246b010SChuck Lever {
92818dca02aSRicardo Labiaga 	size_t len, used;
92918dca02aSRicardo Labiaga 	u32 offset;
93018dca02aSRicardo Labiaga 	__be32	calldir;
93118dca02aSRicardo Labiaga 
93218dca02aSRicardo Labiaga 	/*
93318dca02aSRicardo Labiaga 	 * We want transport->tcp_offset to be 8 at the end of this routine
93418dca02aSRicardo Labiaga 	 * (4 bytes for the xid and 4 bytes for the call/reply flag).
93518dca02aSRicardo Labiaga 	 * When this function is called for the first time,
93618dca02aSRicardo Labiaga 	 * transport->tcp_offset is 4 (after having already read the xid).
93718dca02aSRicardo Labiaga 	 */
93818dca02aSRicardo Labiaga 	offset = transport->tcp_offset - sizeof(transport->tcp_xid);
93918dca02aSRicardo Labiaga 	len = sizeof(calldir) - offset;
94018dca02aSRicardo Labiaga 	dprintk("RPC:       reading CALL/REPLY flag (%Zu bytes)\n", len);
94118dca02aSRicardo Labiaga 	used = xdr_skb_read_bits(desc, &calldir, len);
94218dca02aSRicardo Labiaga 	transport->tcp_offset += used;
94318dca02aSRicardo Labiaga 	if (used != len)
94418dca02aSRicardo Labiaga 		return;
945f4a2e418SRicardo Labiaga 	transport->tcp_flags &= ~TCP_RCV_READ_CALLDIR;
946f4a2e418SRicardo Labiaga 	transport->tcp_flags |= TCP_RCV_COPY_CALLDIR;
94718dca02aSRicardo Labiaga 	transport->tcp_flags |= TCP_RCV_COPY_DATA;
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 	 */
95218dca02aSRicardo Labiaga 	if (ntohl(calldir) == RPC_REPLY)
95318dca02aSRicardo Labiaga 		transport->tcp_flags |= TCP_RPC_REPLY;
95418dca02aSRicardo Labiaga 	else
95518dca02aSRicardo Labiaga 		transport->tcp_flags &= ~TCP_RPC_REPLY;
95618dca02aSRicardo Labiaga 	dprintk("RPC:       reading %s CALL/REPLY flag %08x\n",
95718dca02aSRicardo Labiaga 			(transport->tcp_flags & TCP_RPC_REPLY) ?
95818dca02aSRicardo Labiaga 				"reply for" : "request with", calldir);
95918dca02aSRicardo Labiaga 	xs_tcp_check_fraghdr(transport);
96018dca02aSRicardo Labiaga }
96118dca02aSRicardo Labiaga 
96244b98efdSRicardo Labiaga static inline void xs_tcp_read_common(struct rpc_xprt *xprt,
96344b98efdSRicardo Labiaga 				     struct xdr_skb_reader *desc,
96444b98efdSRicardo Labiaga 				     struct rpc_rqst *req)
965a246b010SChuck Lever {
96644b98efdSRicardo Labiaga 	struct sock_xprt *transport =
96744b98efdSRicardo Labiaga 				container_of(xprt, struct sock_xprt, xprt);
968a246b010SChuck Lever 	struct xdr_buf *rcvbuf;
969a246b010SChuck Lever 	size_t len;
970a246b010SChuck Lever 	ssize_t r;
971a246b010SChuck Lever 
972a246b010SChuck Lever 	rcvbuf = &req->rq_private_buf;
973f4a2e418SRicardo Labiaga 
974f4a2e418SRicardo Labiaga 	if (transport->tcp_flags & TCP_RCV_COPY_CALLDIR) {
975f4a2e418SRicardo Labiaga 		/*
976f4a2e418SRicardo Labiaga 		 * Save the RPC direction in the XDR buffer
977f4a2e418SRicardo Labiaga 		 */
978f4a2e418SRicardo Labiaga 		__be32	calldir = transport->tcp_flags & TCP_RPC_REPLY ?
979f4a2e418SRicardo Labiaga 					htonl(RPC_REPLY) : 0;
980f4a2e418SRicardo Labiaga 
981f4a2e418SRicardo Labiaga 		memcpy(rcvbuf->head[0].iov_base + transport->tcp_copied,
982f4a2e418SRicardo Labiaga 			&calldir, sizeof(calldir));
983f4a2e418SRicardo Labiaga 		transport->tcp_copied += sizeof(calldir);
984f4a2e418SRicardo Labiaga 		transport->tcp_flags &= ~TCP_RCV_COPY_CALLDIR;
985a246b010SChuck Lever 	}
986a246b010SChuck Lever 
987a246b010SChuck Lever 	len = desc->count;
98851971139SChuck Lever 	if (len > transport->tcp_reclen - transport->tcp_offset) {
989dd456471SChuck Lever 		struct xdr_skb_reader my_desc;
990a246b010SChuck Lever 
99151971139SChuck Lever 		len = transport->tcp_reclen - transport->tcp_offset;
992a246b010SChuck Lever 		memcpy(&my_desc, desc, sizeof(my_desc));
993a246b010SChuck Lever 		my_desc.count = len;
99451971139SChuck Lever 		r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
9959d292316SChuck Lever 					  &my_desc, xdr_skb_read_bits);
996a246b010SChuck Lever 		desc->count -= r;
997a246b010SChuck Lever 		desc->offset += r;
998a246b010SChuck Lever 	} else
99951971139SChuck Lever 		r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
10009d292316SChuck Lever 					  desc, xdr_skb_read_bits);
1001a246b010SChuck Lever 
1002a246b010SChuck Lever 	if (r > 0) {
100351971139SChuck Lever 		transport->tcp_copied += r;
100451971139SChuck Lever 		transport->tcp_offset += r;
1005a246b010SChuck Lever 	}
1006a246b010SChuck Lever 	if (r != len) {
1007a246b010SChuck Lever 		/* Error when copying to the receive buffer,
1008a246b010SChuck Lever 		 * usually because we weren't able to allocate
1009a246b010SChuck Lever 		 * additional buffer pages. All we can do now
1010e136d092SChuck Lever 		 * is turn off TCP_RCV_COPY_DATA, so the request
1011a246b010SChuck Lever 		 * will not receive any additional updates,
1012a246b010SChuck Lever 		 * and time out.
1013a246b010SChuck Lever 		 * Any remaining data from this record will
1014a246b010SChuck Lever 		 * be discarded.
1015a246b010SChuck Lever 		 */
1016e136d092SChuck Lever 		transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1017a246b010SChuck Lever 		dprintk("RPC:       XID %08x truncated request\n",
101851971139SChuck Lever 				ntohl(transport->tcp_xid));
101946121cf7SChuck Lever 		dprintk("RPC:       xprt = %p, tcp_copied = %lu, "
102046121cf7SChuck Lever 				"tcp_offset = %u, tcp_reclen = %u\n",
102146121cf7SChuck Lever 				xprt, transport->tcp_copied,
102246121cf7SChuck Lever 				transport->tcp_offset, transport->tcp_reclen);
102344b98efdSRicardo Labiaga 		return;
1024a246b010SChuck Lever 	}
1025a246b010SChuck Lever 
1026a246b010SChuck Lever 	dprintk("RPC:       XID %08x read %Zd bytes\n",
102751971139SChuck Lever 			ntohl(transport->tcp_xid), r);
102846121cf7SChuck Lever 	dprintk("RPC:       xprt = %p, tcp_copied = %lu, tcp_offset = %u, "
102946121cf7SChuck Lever 			"tcp_reclen = %u\n", xprt, transport->tcp_copied,
103046121cf7SChuck Lever 			transport->tcp_offset, transport->tcp_reclen);
1031a246b010SChuck Lever 
103251971139SChuck Lever 	if (transport->tcp_copied == req->rq_private_buf.buflen)
1033e136d092SChuck Lever 		transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
103451971139SChuck Lever 	else if (transport->tcp_offset == transport->tcp_reclen) {
1035e136d092SChuck Lever 		if (transport->tcp_flags & TCP_RCV_LAST_FRAG)
1036e136d092SChuck Lever 			transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1037a246b010SChuck Lever 	}
103844b98efdSRicardo Labiaga }
103944b98efdSRicardo Labiaga 
104044b98efdSRicardo Labiaga /*
104144b98efdSRicardo Labiaga  * Finds the request corresponding to the RPC xid and invokes the common
104244b98efdSRicardo Labiaga  * tcp read code to read the data.
104344b98efdSRicardo Labiaga  */
104444b98efdSRicardo Labiaga static inline int xs_tcp_read_reply(struct rpc_xprt *xprt,
104544b98efdSRicardo Labiaga 				    struct xdr_skb_reader *desc)
104644b98efdSRicardo Labiaga {
104744b98efdSRicardo Labiaga 	struct sock_xprt *transport =
104844b98efdSRicardo Labiaga 				container_of(xprt, struct sock_xprt, xprt);
104944b98efdSRicardo Labiaga 	struct rpc_rqst *req;
105044b98efdSRicardo Labiaga 
105144b98efdSRicardo Labiaga 	dprintk("RPC:       read reply XID %08x\n", ntohl(transport->tcp_xid));
105244b98efdSRicardo Labiaga 
105344b98efdSRicardo Labiaga 	/* Find and lock the request corresponding to this xid */
105444b98efdSRicardo Labiaga 	spin_lock(&xprt->transport_lock);
105544b98efdSRicardo Labiaga 	req = xprt_lookup_rqst(xprt, transport->tcp_xid);
105644b98efdSRicardo Labiaga 	if (!req) {
105744b98efdSRicardo Labiaga 		dprintk("RPC:       XID %08x request not found!\n",
105844b98efdSRicardo Labiaga 				ntohl(transport->tcp_xid));
105944b98efdSRicardo Labiaga 		spin_unlock(&xprt->transport_lock);
106044b98efdSRicardo Labiaga 		return -1;
106144b98efdSRicardo Labiaga 	}
106244b98efdSRicardo Labiaga 
106344b98efdSRicardo Labiaga 	xs_tcp_read_common(xprt, desc, req);
106444b98efdSRicardo Labiaga 
1065e136d092SChuck Lever 	if (!(transport->tcp_flags & TCP_RCV_COPY_DATA))
106651971139SChuck Lever 		xprt_complete_rqst(req->rq_task, transport->tcp_copied);
106744b98efdSRicardo Labiaga 
10684a0f8c04SChuck Lever 	spin_unlock(&xprt->transport_lock);
106944b98efdSRicardo Labiaga 	return 0;
107044b98efdSRicardo Labiaga }
107144b98efdSRicardo Labiaga 
107244b98efdSRicardo Labiaga #if defined(CONFIG_NFS_V4_1)
107344b98efdSRicardo Labiaga /*
107444b98efdSRicardo Labiaga  * Obtains an rpc_rqst previously allocated and invokes the common
107544b98efdSRicardo Labiaga  * tcp read code to read the data.  The result is placed in the callback
107644b98efdSRicardo Labiaga  * queue.
107744b98efdSRicardo Labiaga  * If we're unable to obtain the rpc_rqst we schedule the closing of the
107844b98efdSRicardo Labiaga  * connection and return -1.
107944b98efdSRicardo Labiaga  */
108044b98efdSRicardo Labiaga static inline int xs_tcp_read_callback(struct rpc_xprt *xprt,
108144b98efdSRicardo Labiaga 				       struct xdr_skb_reader *desc)
108244b98efdSRicardo Labiaga {
108344b98efdSRicardo Labiaga 	struct sock_xprt *transport =
108444b98efdSRicardo Labiaga 				container_of(xprt, struct sock_xprt, xprt);
108544b98efdSRicardo Labiaga 	struct rpc_rqst *req;
108644b98efdSRicardo Labiaga 
108744b98efdSRicardo Labiaga 	req = xprt_alloc_bc_request(xprt);
108844b98efdSRicardo Labiaga 	if (req == NULL) {
108944b98efdSRicardo Labiaga 		printk(KERN_WARNING "Callback slot table overflowed\n");
109044b98efdSRicardo Labiaga 		xprt_force_disconnect(xprt);
109144b98efdSRicardo Labiaga 		return -1;
109244b98efdSRicardo Labiaga 	}
109344b98efdSRicardo Labiaga 
109444b98efdSRicardo Labiaga 	req->rq_xid = transport->tcp_xid;
109544b98efdSRicardo Labiaga 	dprintk("RPC:       read callback  XID %08x\n", ntohl(req->rq_xid));
109644b98efdSRicardo Labiaga 	xs_tcp_read_common(xprt, desc, req);
109744b98efdSRicardo Labiaga 
109844b98efdSRicardo Labiaga 	if (!(transport->tcp_flags & TCP_RCV_COPY_DATA)) {
109944b98efdSRicardo Labiaga 		struct svc_serv *bc_serv = xprt->bc_serv;
110044b98efdSRicardo Labiaga 
110144b98efdSRicardo Labiaga 		/*
110244b98efdSRicardo Labiaga 		 * Add callback request to callback list.  The callback
110344b98efdSRicardo Labiaga 		 * service sleeps on the sv_cb_waitq waiting for new
110444b98efdSRicardo Labiaga 		 * requests.  Wake it up after adding enqueing the
110544b98efdSRicardo Labiaga 		 * request.
110644b98efdSRicardo Labiaga 		 */
110744b98efdSRicardo Labiaga 		dprintk("RPC:       add callback request to list\n");
110844b98efdSRicardo Labiaga 		spin_lock(&bc_serv->sv_cb_lock);
110944b98efdSRicardo Labiaga 		list_add(&req->rq_bc_list, &bc_serv->sv_cb_list);
111044b98efdSRicardo Labiaga 		spin_unlock(&bc_serv->sv_cb_lock);
111144b98efdSRicardo Labiaga 		wake_up(&bc_serv->sv_cb_waitq);
111244b98efdSRicardo Labiaga 	}
111344b98efdSRicardo Labiaga 
111444b98efdSRicardo Labiaga 	req->rq_private_buf.len = transport->tcp_copied;
111544b98efdSRicardo Labiaga 
111644b98efdSRicardo Labiaga 	return 0;
111744b98efdSRicardo Labiaga }
111844b98efdSRicardo Labiaga 
111944b98efdSRicardo Labiaga static inline int _xs_tcp_read_data(struct rpc_xprt *xprt,
112044b98efdSRicardo Labiaga 					struct xdr_skb_reader *desc)
112144b98efdSRicardo Labiaga {
112244b98efdSRicardo Labiaga 	struct sock_xprt *transport =
112344b98efdSRicardo Labiaga 				container_of(xprt, struct sock_xprt, xprt);
112444b98efdSRicardo Labiaga 
112544b98efdSRicardo Labiaga 	return (transport->tcp_flags & TCP_RPC_REPLY) ?
112644b98efdSRicardo Labiaga 		xs_tcp_read_reply(xprt, desc) :
112744b98efdSRicardo Labiaga 		xs_tcp_read_callback(xprt, desc);
112844b98efdSRicardo Labiaga }
112944b98efdSRicardo Labiaga #else
113044b98efdSRicardo Labiaga static inline int _xs_tcp_read_data(struct rpc_xprt *xprt,
113144b98efdSRicardo Labiaga 					struct xdr_skb_reader *desc)
113244b98efdSRicardo Labiaga {
113344b98efdSRicardo Labiaga 	return xs_tcp_read_reply(xprt, desc);
113444b98efdSRicardo Labiaga }
113544b98efdSRicardo Labiaga #endif /* CONFIG_NFS_V4_1 */
113644b98efdSRicardo Labiaga 
113744b98efdSRicardo Labiaga /*
113844b98efdSRicardo Labiaga  * Read data off the transport.  This can be either an RPC_CALL or an
113944b98efdSRicardo Labiaga  * RPC_REPLY.  Relay the processing to helper functions.
114044b98efdSRicardo Labiaga  */
114144b98efdSRicardo Labiaga static void xs_tcp_read_data(struct rpc_xprt *xprt,
114244b98efdSRicardo Labiaga 				    struct xdr_skb_reader *desc)
114344b98efdSRicardo Labiaga {
114444b98efdSRicardo Labiaga 	struct sock_xprt *transport =
114544b98efdSRicardo Labiaga 				container_of(xprt, struct sock_xprt, xprt);
114644b98efdSRicardo Labiaga 
114744b98efdSRicardo Labiaga 	if (_xs_tcp_read_data(xprt, desc) == 0)
114851971139SChuck Lever 		xs_tcp_check_fraghdr(transport);
114944b98efdSRicardo Labiaga 	else {
115044b98efdSRicardo Labiaga 		/*
115144b98efdSRicardo Labiaga 		 * The transport_lock protects the request handling.
115244b98efdSRicardo Labiaga 		 * There's no need to hold it to update the tcp_flags.
115344b98efdSRicardo Labiaga 		 */
115444b98efdSRicardo Labiaga 		transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
115544b98efdSRicardo Labiaga 	}
1156a246b010SChuck Lever }
1157a246b010SChuck Lever 
1158dd456471SChuck Lever static inline void xs_tcp_read_discard(struct sock_xprt *transport, struct xdr_skb_reader *desc)
1159a246b010SChuck Lever {
1160a246b010SChuck Lever 	size_t len;
1161a246b010SChuck Lever 
116251971139SChuck Lever 	len = transport->tcp_reclen - transport->tcp_offset;
1163a246b010SChuck Lever 	if (len > desc->count)
1164a246b010SChuck Lever 		len = desc->count;
1165a246b010SChuck Lever 	desc->count -= len;
1166a246b010SChuck Lever 	desc->offset += len;
116751971139SChuck Lever 	transport->tcp_offset += len;
1168a246b010SChuck Lever 	dprintk("RPC:       discarded %Zu bytes\n", len);
116951971139SChuck Lever 	xs_tcp_check_fraghdr(transport);
1170a246b010SChuck Lever }
1171a246b010SChuck Lever 
11729903cd1cSChuck Lever static int xs_tcp_data_recv(read_descriptor_t *rd_desc, struct sk_buff *skb, unsigned int offset, size_t len)
1173a246b010SChuck Lever {
1174a246b010SChuck Lever 	struct rpc_xprt *xprt = rd_desc->arg.data;
117551971139SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1176dd456471SChuck Lever 	struct xdr_skb_reader desc = {
1177a246b010SChuck Lever 		.skb	= skb,
1178a246b010SChuck Lever 		.offset	= offset,
1179a246b010SChuck Lever 		.count	= len,
1180a246b010SChuck Lever 	};
1181a246b010SChuck Lever 
11829903cd1cSChuck Lever 	dprintk("RPC:       xs_tcp_data_recv started\n");
1183a246b010SChuck Lever 	do {
1184a246b010SChuck Lever 		/* Read in a new fragment marker if necessary */
1185a246b010SChuck Lever 		/* Can we ever really expect to get completely empty fragments? */
1186e136d092SChuck Lever 		if (transport->tcp_flags & TCP_RCV_COPY_FRAGHDR) {
11879903cd1cSChuck Lever 			xs_tcp_read_fraghdr(xprt, &desc);
1188a246b010SChuck Lever 			continue;
1189a246b010SChuck Lever 		}
1190a246b010SChuck Lever 		/* Read in the xid if necessary */
1191e136d092SChuck Lever 		if (transport->tcp_flags & TCP_RCV_COPY_XID) {
119251971139SChuck Lever 			xs_tcp_read_xid(transport, &desc);
1193a246b010SChuck Lever 			continue;
1194a246b010SChuck Lever 		}
119518dca02aSRicardo Labiaga 		/* Read in the call/reply flag */
1196f4a2e418SRicardo Labiaga 		if (transport->tcp_flags & TCP_RCV_READ_CALLDIR) {
119718dca02aSRicardo Labiaga 			xs_tcp_read_calldir(transport, &desc);
119818dca02aSRicardo Labiaga 			continue;
119918dca02aSRicardo Labiaga 		}
1200a246b010SChuck Lever 		/* Read in the request data */
1201e136d092SChuck Lever 		if (transport->tcp_flags & TCP_RCV_COPY_DATA) {
120244b98efdSRicardo Labiaga 			xs_tcp_read_data(xprt, &desc);
1203a246b010SChuck Lever 			continue;
1204a246b010SChuck Lever 		}
1205a246b010SChuck Lever 		/* Skip over any trailing bytes on short reads */
120651971139SChuck Lever 		xs_tcp_read_discard(transport, &desc);
1207a246b010SChuck Lever 	} while (desc.count);
12089903cd1cSChuck Lever 	dprintk("RPC:       xs_tcp_data_recv done\n");
1209a246b010SChuck Lever 	return len - desc.count;
1210a246b010SChuck Lever }
1211a246b010SChuck Lever 
12129903cd1cSChuck Lever /**
12139903cd1cSChuck Lever  * xs_tcp_data_ready - "data ready" callback for TCP sockets
12149903cd1cSChuck Lever  * @sk: socket with data to read
12159903cd1cSChuck Lever  * @bytes: how much data to read
12169903cd1cSChuck Lever  *
12179903cd1cSChuck Lever  */
12189903cd1cSChuck Lever static void xs_tcp_data_ready(struct sock *sk, int bytes)
1219a246b010SChuck Lever {
1220a246b010SChuck Lever 	struct rpc_xprt *xprt;
1221a246b010SChuck Lever 	read_descriptor_t rd_desc;
1222ff2d7db8STrond Myklebust 	int read;
1223a246b010SChuck Lever 
12249903cd1cSChuck Lever 	dprintk("RPC:       xs_tcp_data_ready...\n");
122546121cf7SChuck Lever 
122646121cf7SChuck Lever 	read_lock(&sk->sk_callback_lock);
12279903cd1cSChuck Lever 	if (!(xprt = xprt_from_sock(sk)))
1228a246b010SChuck Lever 		goto out;
1229a246b010SChuck Lever 	if (xprt->shutdown)
1230a246b010SChuck Lever 		goto out;
1231a246b010SChuck Lever 
123261d0a8e6SNeil Brown 	/* Any data means we had a useful conversation, so
123361d0a8e6SNeil Brown 	 * the we don't need to delay the next reconnect
123461d0a8e6SNeil Brown 	 */
123561d0a8e6SNeil Brown 	if (xprt->reestablish_timeout)
123661d0a8e6SNeil Brown 		xprt->reestablish_timeout = 0;
123761d0a8e6SNeil Brown 
12389903cd1cSChuck Lever 	/* We use rd_desc to pass struct xprt to xs_tcp_data_recv */
1239a246b010SChuck Lever 	rd_desc.arg.data = xprt;
1240ff2d7db8STrond Myklebust 	do {
1241a246b010SChuck Lever 		rd_desc.count = 65536;
1242ff2d7db8STrond Myklebust 		read = tcp_read_sock(sk, &rd_desc, xs_tcp_data_recv);
1243ff2d7db8STrond Myklebust 	} while (read > 0);
1244a246b010SChuck Lever out:
1245a246b010SChuck Lever 	read_unlock(&sk->sk_callback_lock);
1246a246b010SChuck Lever }
1247a246b010SChuck Lever 
12487d1e8255STrond Myklebust /*
12497d1e8255STrond Myklebust  * Do the equivalent of linger/linger2 handling for dealing with
12507d1e8255STrond Myklebust  * broken servers that don't close the socket in a timely
12517d1e8255STrond Myklebust  * fashion
12527d1e8255STrond Myklebust  */
12537d1e8255STrond Myklebust static void xs_tcp_schedule_linger_timeout(struct rpc_xprt *xprt,
12547d1e8255STrond Myklebust 		unsigned long timeout)
12557d1e8255STrond Myklebust {
12567d1e8255STrond Myklebust 	struct sock_xprt *transport;
12577d1e8255STrond Myklebust 
12587d1e8255STrond Myklebust 	if (xprt_test_and_set_connecting(xprt))
12597d1e8255STrond Myklebust 		return;
12607d1e8255STrond Myklebust 	set_bit(XPRT_CONNECTION_ABORT, &xprt->state);
12617d1e8255STrond Myklebust 	transport = container_of(xprt, struct sock_xprt, xprt);
12627d1e8255STrond Myklebust 	queue_delayed_work(rpciod_workqueue, &transport->connect_worker,
12637d1e8255STrond Myklebust 			   timeout);
12647d1e8255STrond Myklebust }
12657d1e8255STrond Myklebust 
12667d1e8255STrond Myklebust static void xs_tcp_cancel_linger_timeout(struct rpc_xprt *xprt)
12677d1e8255STrond Myklebust {
12687d1e8255STrond Myklebust 	struct sock_xprt *transport;
12697d1e8255STrond Myklebust 
12707d1e8255STrond Myklebust 	transport = container_of(xprt, struct sock_xprt, xprt);
12717d1e8255STrond Myklebust 
12727d1e8255STrond Myklebust 	if (!test_bit(XPRT_CONNECTION_ABORT, &xprt->state) ||
12737d1e8255STrond Myklebust 	    !cancel_delayed_work(&transport->connect_worker))
12747d1e8255STrond Myklebust 		return;
12757d1e8255STrond Myklebust 	clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
12767d1e8255STrond Myklebust 	xprt_clear_connecting(xprt);
12777d1e8255STrond Myklebust }
12787d1e8255STrond Myklebust 
12797d1e8255STrond Myklebust static void xs_sock_mark_closed(struct rpc_xprt *xprt)
12807d1e8255STrond Myklebust {
12817d1e8255STrond Myklebust 	smp_mb__before_clear_bit();
12827d1e8255STrond Myklebust 	clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
12837d1e8255STrond Myklebust 	clear_bit(XPRT_CLOSING, &xprt->state);
12847d1e8255STrond Myklebust 	smp_mb__after_clear_bit();
12857d1e8255STrond Myklebust 	/* Mark transport as closed and wake up all pending tasks */
12867d1e8255STrond Myklebust 	xprt_disconnect_done(xprt);
12877d1e8255STrond Myklebust }
12887d1e8255STrond Myklebust 
12899903cd1cSChuck Lever /**
12909903cd1cSChuck Lever  * xs_tcp_state_change - callback to handle TCP socket state changes
12919903cd1cSChuck Lever  * @sk: socket whose state has changed
12929903cd1cSChuck Lever  *
12939903cd1cSChuck Lever  */
12949903cd1cSChuck Lever static void xs_tcp_state_change(struct sock *sk)
1295a246b010SChuck Lever {
1296a246b010SChuck Lever 	struct rpc_xprt *xprt;
1297a246b010SChuck Lever 
1298a246b010SChuck Lever 	read_lock(&sk->sk_callback_lock);
1299a246b010SChuck Lever 	if (!(xprt = xprt_from_sock(sk)))
1300a246b010SChuck Lever 		goto out;
13019903cd1cSChuck Lever 	dprintk("RPC:       xs_tcp_state_change client %p...\n", xprt);
1302a246b010SChuck Lever 	dprintk("RPC:       state %x conn %d dead %d zapped %d\n",
1303a246b010SChuck Lever 			sk->sk_state, xprt_connected(xprt),
1304a246b010SChuck Lever 			sock_flag(sk, SOCK_DEAD),
1305a246b010SChuck Lever 			sock_flag(sk, SOCK_ZAPPED));
1306a246b010SChuck Lever 
1307a246b010SChuck Lever 	switch (sk->sk_state) {
1308a246b010SChuck Lever 	case TCP_ESTABLISHED:
13094a0f8c04SChuck Lever 		spin_lock_bh(&xprt->transport_lock);
1310a246b010SChuck Lever 		if (!xprt_test_and_set_connected(xprt)) {
131151971139SChuck Lever 			struct sock_xprt *transport = container_of(xprt,
131251971139SChuck Lever 					struct sock_xprt, xprt);
131351971139SChuck Lever 
1314a246b010SChuck Lever 			/* Reset TCP record info */
131551971139SChuck Lever 			transport->tcp_offset = 0;
131651971139SChuck Lever 			transport->tcp_reclen = 0;
131751971139SChuck Lever 			transport->tcp_copied = 0;
1318e136d092SChuck Lever 			transport->tcp_flags =
1319e136d092SChuck Lever 				TCP_RCV_COPY_FRAGHDR | TCP_RCV_COPY_XID;
132051971139SChuck Lever 
13212a491991STrond Myklebust 			xprt_wake_pending_tasks(xprt, -EAGAIN);
1322a246b010SChuck Lever 		}
13234a0f8c04SChuck Lever 		spin_unlock_bh(&xprt->transport_lock);
1324a246b010SChuck Lever 		break;
13253b948ae5STrond Myklebust 	case TCP_FIN_WAIT1:
13263b948ae5STrond Myklebust 		/* The client initiated a shutdown of the socket */
13277c1d71cfSTrond Myklebust 		xprt->connect_cookie++;
1328663b8858STrond Myklebust 		xprt->reestablish_timeout = 0;
13293b948ae5STrond Myklebust 		set_bit(XPRT_CLOSING, &xprt->state);
13303b948ae5STrond Myklebust 		smp_mb__before_clear_bit();
13313b948ae5STrond Myklebust 		clear_bit(XPRT_CONNECTED, &xprt->state);
1332ef803670STrond Myklebust 		clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
13333b948ae5STrond Myklebust 		smp_mb__after_clear_bit();
133425fe6142STrond Myklebust 		xs_tcp_schedule_linger_timeout(xprt, xs_tcp_fin_timeout);
1335a246b010SChuck Lever 		break;
1336632e3bdcSTrond Myklebust 	case TCP_CLOSE_WAIT:
13373b948ae5STrond Myklebust 		/* The server initiated a shutdown of the socket */
133866af1e55STrond Myklebust 		xprt_force_disconnect(xprt);
1339663b8858STrond Myklebust 	case TCP_SYN_SENT:
13407c1d71cfSTrond Myklebust 		xprt->connect_cookie++;
1341663b8858STrond Myklebust 	case TCP_CLOSING:
1342663b8858STrond Myklebust 		/*
1343663b8858STrond Myklebust 		 * If the server closed down the connection, make sure that
1344663b8858STrond Myklebust 		 * we back off before reconnecting
1345663b8858STrond Myklebust 		 */
1346663b8858STrond Myklebust 		if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
1347663b8858STrond Myklebust 			xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
13483b948ae5STrond Myklebust 		break;
13493b948ae5STrond Myklebust 	case TCP_LAST_ACK:
1350670f9457STrond Myklebust 		set_bit(XPRT_CLOSING, &xprt->state);
135125fe6142STrond Myklebust 		xs_tcp_schedule_linger_timeout(xprt, xs_tcp_fin_timeout);
13523b948ae5STrond Myklebust 		smp_mb__before_clear_bit();
13533b948ae5STrond Myklebust 		clear_bit(XPRT_CONNECTED, &xprt->state);
13543b948ae5STrond Myklebust 		smp_mb__after_clear_bit();
13553b948ae5STrond Myklebust 		break;
13563b948ae5STrond Myklebust 	case TCP_CLOSE:
13577d1e8255STrond Myklebust 		xs_tcp_cancel_linger_timeout(xprt);
13587d1e8255STrond Myklebust 		xs_sock_mark_closed(xprt);
1359a246b010SChuck Lever 	}
1360a246b010SChuck Lever  out:
1361a246b010SChuck Lever 	read_unlock(&sk->sk_callback_lock);
1362a246b010SChuck Lever }
1363a246b010SChuck Lever 
13649903cd1cSChuck Lever /**
1365482f32e6STrond Myklebust  * xs_error_report - callback mainly for catching socket errors
13662a9e1cfaSTrond Myklebust  * @sk: socket
13672a9e1cfaSTrond Myklebust  */
1368482f32e6STrond Myklebust static void xs_error_report(struct sock *sk)
13692a9e1cfaSTrond Myklebust {
13702a9e1cfaSTrond Myklebust 	struct rpc_xprt *xprt;
13712a9e1cfaSTrond Myklebust 
13722a9e1cfaSTrond Myklebust 	read_lock(&sk->sk_callback_lock);
13732a9e1cfaSTrond Myklebust 	if (!(xprt = xprt_from_sock(sk)))
13742a9e1cfaSTrond Myklebust 		goto out;
13752a9e1cfaSTrond Myklebust 	dprintk("RPC:       %s client %p...\n"
13762a9e1cfaSTrond Myklebust 			"RPC:       error %d\n",
13772a9e1cfaSTrond Myklebust 			__func__, xprt, sk->sk_err);
1378482f32e6STrond Myklebust 	xprt_wake_pending_tasks(xprt, -EAGAIN);
13792a9e1cfaSTrond Myklebust out:
13802a9e1cfaSTrond Myklebust 	read_unlock(&sk->sk_callback_lock);
13812a9e1cfaSTrond Myklebust }
13822a9e1cfaSTrond Myklebust 
13831f0fa154SIlpo Järvinen static void xs_write_space(struct sock *sk)
13841f0fa154SIlpo Järvinen {
13851f0fa154SIlpo Järvinen 	struct socket *sock;
13861f0fa154SIlpo Järvinen 	struct rpc_xprt *xprt;
13871f0fa154SIlpo Järvinen 
13881f0fa154SIlpo Järvinen 	if (unlikely(!(sock = sk->sk_socket)))
13891f0fa154SIlpo Järvinen 		return;
13901f0fa154SIlpo Järvinen 	clear_bit(SOCK_NOSPACE, &sock->flags);
13911f0fa154SIlpo Järvinen 
13921f0fa154SIlpo Järvinen 	if (unlikely(!(xprt = xprt_from_sock(sk))))
13931f0fa154SIlpo Järvinen 		return;
13941f0fa154SIlpo Järvinen 	if (test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags) == 0)
13951f0fa154SIlpo Järvinen 		return;
13961f0fa154SIlpo Järvinen 
13971f0fa154SIlpo Järvinen 	xprt_write_space(xprt);
13981f0fa154SIlpo Järvinen }
13991f0fa154SIlpo Järvinen 
14002a9e1cfaSTrond Myklebust /**
1401c7b2cae8SChuck Lever  * xs_udp_write_space - callback invoked when socket buffer space
1402c7b2cae8SChuck Lever  *                             becomes available
14039903cd1cSChuck Lever  * @sk: socket whose state has changed
14049903cd1cSChuck Lever  *
1405a246b010SChuck Lever  * Called when more output buffer space is available for this socket.
1406a246b010SChuck Lever  * We try not to wake our writers until they can make "significant"
1407c7b2cae8SChuck Lever  * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1408a246b010SChuck Lever  * with a bunch of small requests.
1409a246b010SChuck Lever  */
1410c7b2cae8SChuck Lever static void xs_udp_write_space(struct sock *sk)
1411a246b010SChuck Lever {
1412a246b010SChuck Lever 	read_lock(&sk->sk_callback_lock);
1413c7b2cae8SChuck Lever 
1414c7b2cae8SChuck Lever 	/* from net/core/sock.c:sock_def_write_space */
14151f0fa154SIlpo Järvinen 	if (sock_writeable(sk))
14161f0fa154SIlpo Järvinen 		xs_write_space(sk);
1417c7b2cae8SChuck Lever 
1418c7b2cae8SChuck Lever 	read_unlock(&sk->sk_callback_lock);
1419c7b2cae8SChuck Lever }
1420c7b2cae8SChuck Lever 
1421c7b2cae8SChuck Lever /**
1422c7b2cae8SChuck Lever  * xs_tcp_write_space - callback invoked when socket buffer space
1423c7b2cae8SChuck Lever  *                             becomes available
1424c7b2cae8SChuck Lever  * @sk: socket whose state has changed
1425c7b2cae8SChuck Lever  *
1426c7b2cae8SChuck Lever  * Called when more output buffer space is available for this socket.
1427c7b2cae8SChuck Lever  * We try not to wake our writers until they can make "significant"
1428c7b2cae8SChuck Lever  * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1429c7b2cae8SChuck Lever  * with a bunch of small requests.
1430c7b2cae8SChuck Lever  */
1431c7b2cae8SChuck Lever static void xs_tcp_write_space(struct sock *sk)
1432c7b2cae8SChuck Lever {
1433c7b2cae8SChuck Lever 	read_lock(&sk->sk_callback_lock);
1434c7b2cae8SChuck Lever 
1435c7b2cae8SChuck Lever 	/* from net/core/stream.c:sk_stream_write_space */
14361f0fa154SIlpo Järvinen 	if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk))
14371f0fa154SIlpo Järvinen 		xs_write_space(sk);
1438c7b2cae8SChuck Lever 
1439a246b010SChuck Lever 	read_unlock(&sk->sk_callback_lock);
1440a246b010SChuck Lever }
1441a246b010SChuck Lever 
1442470056c2SChuck Lever static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt)
1443a246b010SChuck Lever {
1444ee0ac0c2SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1445ee0ac0c2SChuck Lever 	struct sock *sk = transport->inet;
1446a246b010SChuck Lever 
14477c6e066eSChuck Lever 	if (transport->rcvsize) {
1448a246b010SChuck Lever 		sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
14497c6e066eSChuck Lever 		sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2;
1450a246b010SChuck Lever 	}
14517c6e066eSChuck Lever 	if (transport->sndsize) {
1452a246b010SChuck Lever 		sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
14537c6e066eSChuck Lever 		sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2;
1454a246b010SChuck Lever 		sk->sk_write_space(sk);
1455a246b010SChuck Lever 	}
1456a246b010SChuck Lever }
1457a246b010SChuck Lever 
145843118c29SChuck Lever /**
1459470056c2SChuck Lever  * xs_udp_set_buffer_size - set send and receive limits
146043118c29SChuck Lever  * @xprt: generic transport
1461470056c2SChuck Lever  * @sndsize: requested size of send buffer, in bytes
1462470056c2SChuck Lever  * @rcvsize: requested size of receive buffer, in bytes
146343118c29SChuck Lever  *
1464470056c2SChuck Lever  * Set socket send and receive buffer size limits.
146543118c29SChuck Lever  */
1466470056c2SChuck Lever static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize)
146743118c29SChuck Lever {
14687c6e066eSChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
14697c6e066eSChuck Lever 
14707c6e066eSChuck Lever 	transport->sndsize = 0;
1471470056c2SChuck Lever 	if (sndsize)
14727c6e066eSChuck Lever 		transport->sndsize = sndsize + 1024;
14737c6e066eSChuck Lever 	transport->rcvsize = 0;
1474470056c2SChuck Lever 	if (rcvsize)
14757c6e066eSChuck Lever 		transport->rcvsize = rcvsize + 1024;
1476470056c2SChuck Lever 
1477470056c2SChuck Lever 	xs_udp_do_set_buffer_size(xprt);
147843118c29SChuck Lever }
147943118c29SChuck Lever 
148046c0ee8bSChuck Lever /**
148146c0ee8bSChuck Lever  * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
148246c0ee8bSChuck Lever  * @task: task that timed out
148346c0ee8bSChuck Lever  *
148446c0ee8bSChuck Lever  * Adjust the congestion window after a retransmit timeout has occurred.
148546c0ee8bSChuck Lever  */
148646c0ee8bSChuck Lever static void xs_udp_timer(struct rpc_task *task)
148746c0ee8bSChuck Lever {
148846c0ee8bSChuck Lever 	xprt_adjust_cwnd(task, -ETIMEDOUT);
148946c0ee8bSChuck Lever }
149046c0ee8bSChuck Lever 
1491b85d8806SChuck Lever static unsigned short xs_get_random_port(void)
1492b85d8806SChuck Lever {
1493b85d8806SChuck Lever 	unsigned short range = xprt_max_resvport - xprt_min_resvport;
1494b85d8806SChuck Lever 	unsigned short rand = (unsigned short) net_random() % range;
1495b85d8806SChuck Lever 	return rand + xprt_min_resvport;
1496b85d8806SChuck Lever }
1497b85d8806SChuck Lever 
149892200412SChuck Lever /**
149992200412SChuck Lever  * xs_set_port - reset the port number in the remote endpoint address
150092200412SChuck Lever  * @xprt: generic transport
150192200412SChuck Lever  * @port: new port number
150292200412SChuck Lever  *
150392200412SChuck Lever  */
150492200412SChuck Lever static void xs_set_port(struct rpc_xprt *xprt, unsigned short port)
150592200412SChuck Lever {
150692200412SChuck Lever 	dprintk("RPC:       setting port for xprt %p to %u\n", xprt, port);
1507c4efcb1dSChuck Lever 
15089dc3b095SChuck Lever 	rpc_set_port(xs_addr(xprt), port);
15099dc3b095SChuck Lever 	xs_update_peer_port(xprt);
151092200412SChuck Lever }
151192200412SChuck Lever 
151267a391d7STrond Myklebust static unsigned short xs_get_srcport(struct sock_xprt *transport, struct socket *sock)
151367a391d7STrond Myklebust {
1514fbfffbd5SChuck Lever 	unsigned short port = transport->srcport;
151567a391d7STrond Myklebust 
151667a391d7STrond Myklebust 	if (port == 0 && transport->xprt.resvport)
151767a391d7STrond Myklebust 		port = xs_get_random_port();
151867a391d7STrond Myklebust 	return port;
151967a391d7STrond Myklebust }
152067a391d7STrond Myklebust 
152167a391d7STrond Myklebust static unsigned short xs_next_srcport(struct sock_xprt *transport, struct socket *sock, unsigned short port)
152267a391d7STrond Myklebust {
1523fbfffbd5SChuck Lever 	if (transport->srcport != 0)
1524fbfffbd5SChuck Lever 		transport->srcport = 0;
152567a391d7STrond Myklebust 	if (!transport->xprt.resvport)
152667a391d7STrond Myklebust 		return 0;
152767a391d7STrond Myklebust 	if (port <= xprt_min_resvport || port > xprt_max_resvport)
152867a391d7STrond Myklebust 		return xprt_max_resvport;
152967a391d7STrond Myklebust 	return --port;
153067a391d7STrond Myklebust }
153167a391d7STrond Myklebust 
15327dc753f0SChuck Lever static int xs_bind4(struct sock_xprt *transport, struct socket *sock)
1533a246b010SChuck Lever {
1534a246b010SChuck Lever 	struct sockaddr_in myaddr = {
1535a246b010SChuck Lever 		.sin_family = AF_INET,
1536a246b010SChuck Lever 	};
1537d3bc9a1dSFrank van Maarseveen 	struct sockaddr_in *sa;
153867a391d7STrond Myklebust 	int err, nloop = 0;
153967a391d7STrond Myklebust 	unsigned short port = xs_get_srcport(transport, sock);
154067a391d7STrond Myklebust 	unsigned short last;
1541a246b010SChuck Lever 
1542fbfffbd5SChuck Lever 	sa = (struct sockaddr_in *)&transport->srcaddr;
1543d3bc9a1dSFrank van Maarseveen 	myaddr.sin_addr = sa->sin_addr;
1544a246b010SChuck Lever 	do {
1545a246b010SChuck Lever 		myaddr.sin_port = htons(port);
1546e6242e92SSridhar Samudrala 		err = kernel_bind(sock, (struct sockaddr *) &myaddr,
1547a246b010SChuck Lever 						sizeof(myaddr));
154867a391d7STrond Myklebust 		if (port == 0)
1549d3bc9a1dSFrank van Maarseveen 			break;
1550a246b010SChuck Lever 		if (err == 0) {
1551fbfffbd5SChuck Lever 			transport->srcport = port;
1552d3bc9a1dSFrank van Maarseveen 			break;
1553a246b010SChuck Lever 		}
155467a391d7STrond Myklebust 		last = port;
155567a391d7STrond Myklebust 		port = xs_next_srcport(transport, sock, port);
155667a391d7STrond Myklebust 		if (port > last)
155767a391d7STrond Myklebust 			nloop++;
155867a391d7STrond Myklebust 	} while (err == -EADDRINUSE && nloop != 2);
155921454aaaSHarvey Harrison 	dprintk("RPC:       %s %pI4:%u: %s (%d)\n",
156021454aaaSHarvey Harrison 			__func__, &myaddr.sin_addr,
15617dc753f0SChuck Lever 			port, err ? "failed" : "ok", err);
1562a246b010SChuck Lever 	return err;
1563a246b010SChuck Lever }
1564a246b010SChuck Lever 
156590058d37SChuck Lever static int xs_bind6(struct sock_xprt *transport, struct socket *sock)
156690058d37SChuck Lever {
156790058d37SChuck Lever 	struct sockaddr_in6 myaddr = {
156890058d37SChuck Lever 		.sin6_family = AF_INET6,
156990058d37SChuck Lever 	};
157090058d37SChuck Lever 	struct sockaddr_in6 *sa;
157167a391d7STrond Myklebust 	int err, nloop = 0;
157267a391d7STrond Myklebust 	unsigned short port = xs_get_srcport(transport, sock);
157367a391d7STrond Myklebust 	unsigned short last;
157490058d37SChuck Lever 
1575fbfffbd5SChuck Lever 	sa = (struct sockaddr_in6 *)&transport->srcaddr;
157690058d37SChuck Lever 	myaddr.sin6_addr = sa->sin6_addr;
157790058d37SChuck Lever 	do {
157890058d37SChuck Lever 		myaddr.sin6_port = htons(port);
157990058d37SChuck Lever 		err = kernel_bind(sock, (struct sockaddr *) &myaddr,
158090058d37SChuck Lever 						sizeof(myaddr));
158167a391d7STrond Myklebust 		if (port == 0)
158290058d37SChuck Lever 			break;
158390058d37SChuck Lever 		if (err == 0) {
1584fbfffbd5SChuck Lever 			transport->srcport = port;
158590058d37SChuck Lever 			break;
158690058d37SChuck Lever 		}
158767a391d7STrond Myklebust 		last = port;
158867a391d7STrond Myklebust 		port = xs_next_srcport(transport, sock, port);
158967a391d7STrond Myklebust 		if (port > last)
159067a391d7STrond Myklebust 			nloop++;
159167a391d7STrond Myklebust 	} while (err == -EADDRINUSE && nloop != 2);
15925b095d98SHarvey Harrison 	dprintk("RPC:       xs_bind6 %pI6:%u: %s (%d)\n",
1593fdb46ee7SHarvey Harrison 		&myaddr.sin6_addr, port, err ? "failed" : "ok", err);
1594a246b010SChuck Lever 	return err;
1595a246b010SChuck Lever }
1596a246b010SChuck Lever 
1597ed07536eSPeter Zijlstra #ifdef CONFIG_DEBUG_LOCK_ALLOC
1598ed07536eSPeter Zijlstra static struct lock_class_key xs_key[2];
1599ed07536eSPeter Zijlstra static struct lock_class_key xs_slock_key[2];
1600ed07536eSPeter Zijlstra 
16018945ee5eSChuck Lever static inline void xs_reclassify_socket4(struct socket *sock)
1602ed07536eSPeter Zijlstra {
1603ed07536eSPeter Zijlstra 	struct sock *sk = sock->sk;
16048945ee5eSChuck Lever 
160502b3d346SJohn Heffner 	BUG_ON(sock_owned_by_user(sk));
16068945ee5eSChuck Lever 	sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC",
16078945ee5eSChuck Lever 		&xs_slock_key[0], "sk_lock-AF_INET-RPC", &xs_key[0]);
1608ed07536eSPeter Zijlstra }
16098945ee5eSChuck Lever 
16108945ee5eSChuck Lever static inline void xs_reclassify_socket6(struct socket *sock)
16118945ee5eSChuck Lever {
16128945ee5eSChuck Lever 	struct sock *sk = sock->sk;
16138945ee5eSChuck Lever 
1614f4921affSLinus Torvalds 	BUG_ON(sock_owned_by_user(sk));
16158945ee5eSChuck Lever 	sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC",
16168945ee5eSChuck Lever 		&xs_slock_key[1], "sk_lock-AF_INET6-RPC", &xs_key[1]);
1617ed07536eSPeter Zijlstra }
1618ed07536eSPeter Zijlstra #else
16198945ee5eSChuck Lever static inline void xs_reclassify_socket4(struct socket *sock)
16208945ee5eSChuck Lever {
16218945ee5eSChuck Lever }
16228945ee5eSChuck Lever 
16238945ee5eSChuck Lever static inline void xs_reclassify_socket6(struct socket *sock)
1624ed07536eSPeter Zijlstra {
1625ed07536eSPeter Zijlstra }
1626ed07536eSPeter Zijlstra #endif
1627ed07536eSPeter Zijlstra 
162816be2d20SChuck Lever static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
1629a246b010SChuck Lever {
163016be2d20SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1631edb267a6SChuck Lever 
1632ee0ac0c2SChuck Lever 	if (!transport->inet) {
1633b0d93ad5SChuck Lever 		struct sock *sk = sock->sk;
1634b0d93ad5SChuck Lever 
1635b0d93ad5SChuck Lever 		write_lock_bh(&sk->sk_callback_lock);
1636b0d93ad5SChuck Lever 
16372a9e1cfaSTrond Myklebust 		xs_save_old_callbacks(transport, sk);
16382a9e1cfaSTrond Myklebust 
1639b0d93ad5SChuck Lever 		sk->sk_user_data = xprt;
1640b0d93ad5SChuck Lever 		sk->sk_data_ready = xs_udp_data_ready;
1641b0d93ad5SChuck Lever 		sk->sk_write_space = xs_udp_write_space;
1642482f32e6STrond Myklebust 		sk->sk_error_report = xs_error_report;
1643b0d93ad5SChuck Lever 		sk->sk_no_check = UDP_CSUM_NORCV;
1644b079fa7bSTrond Myklebust 		sk->sk_allocation = GFP_ATOMIC;
1645b0d93ad5SChuck Lever 
1646b0d93ad5SChuck Lever 		xprt_set_connected(xprt);
1647b0d93ad5SChuck Lever 
1648b0d93ad5SChuck Lever 		/* Reset to new socket */
1649ee0ac0c2SChuck Lever 		transport->sock = sock;
1650ee0ac0c2SChuck Lever 		transport->inet = sk;
1651b0d93ad5SChuck Lever 
1652b0d93ad5SChuck Lever 		write_unlock_bh(&sk->sk_callback_lock);
1653b0d93ad5SChuck Lever 	}
1654470056c2SChuck Lever 	xs_udp_do_set_buffer_size(xprt);
165516be2d20SChuck Lever }
165616be2d20SChuck Lever 
1657a246b010SChuck Lever /**
16589c3d72deSChuck Lever  * xs_udp_connect_worker4 - set up a UDP socket
1659a246b010SChuck Lever  * @work: RPC transport to connect
1660a246b010SChuck Lever  *
1661a246b010SChuck Lever  * Invoked by a work queue tasklet.
1662a246b010SChuck Lever  */
16639c3d72deSChuck Lever static void xs_udp_connect_worker4(struct work_struct *work)
1664a246b010SChuck Lever {
1665a246b010SChuck Lever 	struct sock_xprt *transport =
1666a246b010SChuck Lever 		container_of(work, struct sock_xprt, connect_worker.work);
1667a246b010SChuck Lever 	struct rpc_xprt *xprt = &transport->xprt;
1668a246b010SChuck Lever 	struct socket *sock = transport->sock;
1669a246b010SChuck Lever 	int err, status = -EIO;
1670a246b010SChuck Lever 
167101d37c42STrond Myklebust 	if (xprt->shutdown)
16729903cd1cSChuck Lever 		goto out;
16739903cd1cSChuck Lever 
1674a246b010SChuck Lever 	/* Start by resetting any existing state */
1675fe315e76SChuck Lever 	xs_reset_transport(transport);
1676a246b010SChuck Lever 
1677fe315e76SChuck Lever 	err = sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock);
1678fe315e76SChuck Lever 	if (err < 0) {
16799903cd1cSChuck Lever 		dprintk("RPC:       can't create UDP transport socket (%d).\n", -err);
1680a246b010SChuck Lever 		goto out;
1681a246b010SChuck Lever 	}
16828945ee5eSChuck Lever 	xs_reclassify_socket4(sock);
1683a246b010SChuck Lever 
16847dc753f0SChuck Lever 	if (xs_bind4(transport, sock)) {
16859903cd1cSChuck Lever 		sock_release(sock);
16869903cd1cSChuck Lever 		goto out;
1687a246b010SChuck Lever 	}
1688b0d93ad5SChuck Lever 
1689c740eff8SChuck Lever 	dprintk("RPC:       worker connecting xprt %p via %s to "
1690c740eff8SChuck Lever 				"%s (port %s)\n", xprt,
1691c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_PROTO],
1692c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_ADDR],
1693c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_PORT]);
1694b0d93ad5SChuck Lever 
169516be2d20SChuck Lever 	xs_udp_finish_connecting(xprt, sock);
1696a246b010SChuck Lever 	status = 0;
1697b0d93ad5SChuck Lever out:
1698b0d93ad5SChuck Lever 	xprt_clear_connecting(xprt);
16997d1e8255STrond Myklebust 	xprt_wake_pending_tasks(xprt, status);
1700b0d93ad5SChuck Lever }
1701b0d93ad5SChuck Lever 
170268e220bdSChuck Lever /**
170368e220bdSChuck Lever  * xs_udp_connect_worker6 - set up a UDP socket
170468e220bdSChuck Lever  * @work: RPC transport to connect
170568e220bdSChuck Lever  *
170668e220bdSChuck Lever  * Invoked by a work queue tasklet.
170768e220bdSChuck Lever  */
170868e220bdSChuck Lever static void xs_udp_connect_worker6(struct work_struct *work)
170968e220bdSChuck Lever {
171068e220bdSChuck Lever 	struct sock_xprt *transport =
171168e220bdSChuck Lever 		container_of(work, struct sock_xprt, connect_worker.work);
171268e220bdSChuck Lever 	struct rpc_xprt *xprt = &transport->xprt;
171368e220bdSChuck Lever 	struct socket *sock = transport->sock;
171468e220bdSChuck Lever 	int err, status = -EIO;
171568e220bdSChuck Lever 
171601d37c42STrond Myklebust 	if (xprt->shutdown)
171768e220bdSChuck Lever 		goto out;
171868e220bdSChuck Lever 
171968e220bdSChuck Lever 	/* Start by resetting any existing state */
1720fe315e76SChuck Lever 	xs_reset_transport(transport);
172168e220bdSChuck Lever 
1722fe315e76SChuck Lever 	err = sock_create_kern(PF_INET6, SOCK_DGRAM, IPPROTO_UDP, &sock);
1723fe315e76SChuck Lever 	if (err < 0) {
172468e220bdSChuck Lever 		dprintk("RPC:       can't create UDP transport socket (%d).\n", -err);
172568e220bdSChuck Lever 		goto out;
172668e220bdSChuck Lever 	}
17278945ee5eSChuck Lever 	xs_reclassify_socket6(sock);
172868e220bdSChuck Lever 
172968e220bdSChuck Lever 	if (xs_bind6(transport, sock) < 0) {
173068e220bdSChuck Lever 		sock_release(sock);
173168e220bdSChuck Lever 		goto out;
173268e220bdSChuck Lever 	}
173368e220bdSChuck Lever 
1734c740eff8SChuck Lever 	dprintk("RPC:       worker connecting xprt %p via %s to "
1735c740eff8SChuck Lever 				"%s (port %s)\n", xprt,
1736c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_PROTO],
1737c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_ADDR],
1738c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_PORT]);
173968e220bdSChuck Lever 
174068e220bdSChuck Lever 	xs_udp_finish_connecting(xprt, sock);
1741a246b010SChuck Lever 	status = 0;
1742b0d93ad5SChuck Lever out:
1743b0d93ad5SChuck Lever 	xprt_clear_connecting(xprt);
17447d1e8255STrond Myklebust 	xprt_wake_pending_tasks(xprt, status);
1745b0d93ad5SChuck Lever }
1746b0d93ad5SChuck Lever 
17473167e12cSChuck Lever /*
17483167e12cSChuck Lever  * We need to preserve the port number so the reply cache on the server can
17493167e12cSChuck Lever  * find our cached RPC replies when we get around to reconnecting.
17503167e12cSChuck Lever  */
175140d2549dSTrond Myklebust static void xs_abort_connection(struct rpc_xprt *xprt, struct sock_xprt *transport)
17523167e12cSChuck Lever {
17533167e12cSChuck Lever 	int result;
17543167e12cSChuck Lever 	struct sockaddr any;
17553167e12cSChuck Lever 
17563167e12cSChuck Lever 	dprintk("RPC:       disconnecting xprt %p to reuse port\n", xprt);
17573167e12cSChuck Lever 
17583167e12cSChuck Lever 	/*
17593167e12cSChuck Lever 	 * Disconnect the transport socket by doing a connect operation
17603167e12cSChuck Lever 	 * with AF_UNSPEC.  This should return immediately...
17613167e12cSChuck Lever 	 */
17623167e12cSChuck Lever 	memset(&any, 0, sizeof(any));
17633167e12cSChuck Lever 	any.sa_family = AF_UNSPEC;
1764ee0ac0c2SChuck Lever 	result = kernel_connect(transport->sock, &any, sizeof(any), 0);
17657d1e8255STrond Myklebust 	if (!result)
17667d1e8255STrond Myklebust 		xs_sock_mark_closed(xprt);
17677d1e8255STrond Myklebust 	else
17683167e12cSChuck Lever 		dprintk("RPC:       AF_UNSPEC connect return code %d\n",
17693167e12cSChuck Lever 				result);
17703167e12cSChuck Lever }
17713167e12cSChuck Lever 
177240d2549dSTrond Myklebust static void xs_tcp_reuse_connection(struct rpc_xprt *xprt, struct sock_xprt *transport)
177340d2549dSTrond Myklebust {
177440d2549dSTrond Myklebust 	unsigned int state = transport->inet->sk_state;
177540d2549dSTrond Myklebust 
177640d2549dSTrond Myklebust 	if (state == TCP_CLOSE && transport->sock->state == SS_UNCONNECTED)
177740d2549dSTrond Myklebust 		return;
177840d2549dSTrond Myklebust 	if ((1 << state) & (TCPF_ESTABLISHED|TCPF_SYN_SENT))
177940d2549dSTrond Myklebust 		return;
178040d2549dSTrond Myklebust 	xs_abort_connection(xprt, transport);
178140d2549dSTrond Myklebust }
178240d2549dSTrond Myklebust 
178316be2d20SChuck Lever static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
1784b0d93ad5SChuck Lever {
178516be2d20SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1786edb267a6SChuck Lever 
1787ee0ac0c2SChuck Lever 	if (!transport->inet) {
1788b0d93ad5SChuck Lever 		struct sock *sk = sock->sk;
1789b0d93ad5SChuck Lever 
1790b0d93ad5SChuck Lever 		write_lock_bh(&sk->sk_callback_lock);
1791b0d93ad5SChuck Lever 
17922a9e1cfaSTrond Myklebust 		xs_save_old_callbacks(transport, sk);
17932a9e1cfaSTrond Myklebust 
1794b0d93ad5SChuck Lever 		sk->sk_user_data = xprt;
1795b0d93ad5SChuck Lever 		sk->sk_data_ready = xs_tcp_data_ready;
1796b0d93ad5SChuck Lever 		sk->sk_state_change = xs_tcp_state_change;
1797b0d93ad5SChuck Lever 		sk->sk_write_space = xs_tcp_write_space;
1798482f32e6STrond Myklebust 		sk->sk_error_report = xs_error_report;
1799b079fa7bSTrond Myklebust 		sk->sk_allocation = GFP_ATOMIC;
18003167e12cSChuck Lever 
18013167e12cSChuck Lever 		/* socket options */
18023167e12cSChuck Lever 		sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
18033167e12cSChuck Lever 		sock_reset_flag(sk, SOCK_LINGER);
18043167e12cSChuck Lever 		tcp_sk(sk)->linger2 = 0;
18053167e12cSChuck Lever 		tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF;
1806b0d93ad5SChuck Lever 
1807b0d93ad5SChuck Lever 		xprt_clear_connected(xprt);
1808b0d93ad5SChuck Lever 
1809b0d93ad5SChuck Lever 		/* Reset to new socket */
1810ee0ac0c2SChuck Lever 		transport->sock = sock;
1811ee0ac0c2SChuck Lever 		transport->inet = sk;
1812b0d93ad5SChuck Lever 
1813b0d93ad5SChuck Lever 		write_unlock_bh(&sk->sk_callback_lock);
1814b0d93ad5SChuck Lever 	}
1815b0d93ad5SChuck Lever 
181601d37c42STrond Myklebust 	if (!xprt_bound(xprt))
181701d37c42STrond Myklebust 		return -ENOTCONN;
181801d37c42STrond Myklebust 
1819b0d93ad5SChuck Lever 	/* Tell the socket layer to start connecting... */
1820262ca07dSChuck Lever 	xprt->stat.connect_count++;
1821262ca07dSChuck Lever 	xprt->stat.connect_start = jiffies;
182295392c59SChuck Lever 	return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK);
182316be2d20SChuck Lever }
182416be2d20SChuck Lever 
182516be2d20SChuck Lever /**
1826b61d59ffSTrond Myklebust  * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint
1827b61d59ffSTrond Myklebust  * @xprt: RPC transport to connect
1828b61d59ffSTrond Myklebust  * @transport: socket transport to connect
1829b61d59ffSTrond Myklebust  * @create_sock: function to create a socket of the correct type
183016be2d20SChuck Lever  *
183116be2d20SChuck Lever  * Invoked by a work queue tasklet.
183216be2d20SChuck Lever  */
1833b61d59ffSTrond Myklebust static void xs_tcp_setup_socket(struct rpc_xprt *xprt,
1834b61d59ffSTrond Myklebust 		struct sock_xprt *transport,
1835b61d59ffSTrond Myklebust 		struct socket *(*create_sock)(struct rpc_xprt *,
1836b61d59ffSTrond Myklebust 			struct sock_xprt *))
183716be2d20SChuck Lever {
183816be2d20SChuck Lever 	struct socket *sock = transport->sock;
1839b61d59ffSTrond Myklebust 	int status = -EIO;
184016be2d20SChuck Lever 
184101d37c42STrond Myklebust 	if (xprt->shutdown)
184216be2d20SChuck Lever 		goto out;
184316be2d20SChuck Lever 
184416be2d20SChuck Lever 	if (!sock) {
18457d1e8255STrond Myklebust 		clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1846b61d59ffSTrond Myklebust 		sock = create_sock(xprt, transport);
1847b61d59ffSTrond Myklebust 		if (IS_ERR(sock)) {
1848b61d59ffSTrond Myklebust 			status = PTR_ERR(sock);
184916be2d20SChuck Lever 			goto out;
185016be2d20SChuck Lever 		}
18517d1e8255STrond Myklebust 	} else {
18527d1e8255STrond Myklebust 		int abort_and_exit;
18537d1e8255STrond Myklebust 
18547d1e8255STrond Myklebust 		abort_and_exit = test_and_clear_bit(XPRT_CONNECTION_ABORT,
18557d1e8255STrond Myklebust 				&xprt->state);
185616be2d20SChuck Lever 		/* "close" the socket, preserving the local port */
185740d2549dSTrond Myklebust 		xs_tcp_reuse_connection(xprt, transport);
185816be2d20SChuck Lever 
18597d1e8255STrond Myklebust 		if (abort_and_exit)
18607d1e8255STrond Myklebust 			goto out_eagain;
18617d1e8255STrond Myklebust 	}
18627d1e8255STrond Myklebust 
1863c740eff8SChuck Lever 	dprintk("RPC:       worker connecting xprt %p via %s to "
1864c740eff8SChuck Lever 				"%s (port %s)\n", xprt,
1865c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_PROTO],
1866c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_ADDR],
1867c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_PORT]);
186816be2d20SChuck Lever 
186916be2d20SChuck Lever 	status = xs_tcp_finish_connecting(xprt, sock);
1870a246b010SChuck Lever 	dprintk("RPC:       %p connect status %d connected %d sock state %d\n",
187146121cf7SChuck Lever 			xprt, -status, xprt_connected(xprt),
187246121cf7SChuck Lever 			sock->sk->sk_state);
1873a246b010SChuck Lever 	switch (status) {
1874f75e6745STrond Myklebust 	default:
1875f75e6745STrond Myklebust 		printk("%s: connect returned unhandled error %d\n",
1876f75e6745STrond Myklebust 			__func__, status);
1877f75e6745STrond Myklebust 	case -EADDRNOTAVAIL:
1878f75e6745STrond Myklebust 		/* We're probably in TIME_WAIT. Get rid of existing socket,
1879f75e6745STrond Myklebust 		 * and retry
1880f75e6745STrond Myklebust 		 */
1881f75e6745STrond Myklebust 		set_bit(XPRT_CONNECTION_CLOSE, &xprt->state);
1882f75e6745STrond Myklebust 		xprt_force_disconnect(xprt);
188388b5ed73STrond Myklebust 		break;
18848a2cec29STrond Myklebust 	case -ECONNREFUSED:
18858a2cec29STrond Myklebust 	case -ECONNRESET:
18868a2cec29STrond Myklebust 	case -ENETUNREACH:
18878a2cec29STrond Myklebust 		/* retry with existing socket, after a delay */
18882a491991STrond Myklebust 	case 0:
1889a246b010SChuck Lever 	case -EINPROGRESS:
1890a246b010SChuck Lever 	case -EALREADY:
18917d1e8255STrond Myklebust 		xprt_clear_connecting(xprt);
18927d1e8255STrond Myklebust 		return;
18939fcfe0c8STrond Myklebust 	case -EINVAL:
18949fcfe0c8STrond Myklebust 		/* Happens, for instance, if the user specified a link
18959fcfe0c8STrond Myklebust 		 * local IPv6 address without a scope-id.
18969fcfe0c8STrond Myklebust 		 */
18979fcfe0c8STrond Myklebust 		goto out;
18988a2cec29STrond Myklebust 	}
18997d1e8255STrond Myklebust out_eagain:
19002a491991STrond Myklebust 	status = -EAGAIN;
1901a246b010SChuck Lever out:
19022226feb6SChuck Lever 	xprt_clear_connecting(xprt);
19037d1e8255STrond Myklebust 	xprt_wake_pending_tasks(xprt, status);
1904a246b010SChuck Lever }
1905a246b010SChuck Lever 
1906b61d59ffSTrond Myklebust static struct socket *xs_create_tcp_sock4(struct rpc_xprt *xprt,
1907b61d59ffSTrond Myklebust 		struct sock_xprt *transport)
1908b61d59ffSTrond Myklebust {
1909b61d59ffSTrond Myklebust 	struct socket *sock;
1910b61d59ffSTrond Myklebust 	int err;
1911b61d59ffSTrond Myklebust 
1912b61d59ffSTrond Myklebust 	/* start from scratch */
1913b61d59ffSTrond Myklebust 	err = sock_create_kern(PF_INET, SOCK_STREAM, IPPROTO_TCP, &sock);
1914b61d59ffSTrond Myklebust 	if (err < 0) {
1915b61d59ffSTrond Myklebust 		dprintk("RPC:       can't create TCP transport socket (%d).\n",
1916b61d59ffSTrond Myklebust 				-err);
1917b61d59ffSTrond Myklebust 		goto out_err;
1918b61d59ffSTrond Myklebust 	}
1919b61d59ffSTrond Myklebust 	xs_reclassify_socket4(sock);
1920b61d59ffSTrond Myklebust 
1921b61d59ffSTrond Myklebust 	if (xs_bind4(transport, sock) < 0) {
1922b61d59ffSTrond Myklebust 		sock_release(sock);
1923b61d59ffSTrond Myklebust 		goto out_err;
1924b61d59ffSTrond Myklebust 	}
1925b61d59ffSTrond Myklebust 	return sock;
1926b61d59ffSTrond Myklebust out_err:
1927b61d59ffSTrond Myklebust 	return ERR_PTR(-EIO);
1928b61d59ffSTrond Myklebust }
1929b61d59ffSTrond Myklebust 
1930b61d59ffSTrond Myklebust /**
1931a246b010SChuck Lever  * xs_tcp_connect_worker4 - connect a TCP socket to a remote endpoint
1932a246b010SChuck Lever  * @work: RPC transport to connect
1933a246b010SChuck Lever  *
1934a246b010SChuck Lever  * Invoked by a work queue tasklet.
1935a246b010SChuck Lever  */
1936a246b010SChuck Lever static void xs_tcp_connect_worker4(struct work_struct *work)
1937a246b010SChuck Lever {
1938a246b010SChuck Lever 	struct sock_xprt *transport =
1939a246b010SChuck Lever 		container_of(work, struct sock_xprt, connect_worker.work);
1940a246b010SChuck Lever 	struct rpc_xprt *xprt = &transport->xprt;
1941a246b010SChuck Lever 
1942b61d59ffSTrond Myklebust 	xs_tcp_setup_socket(xprt, transport, xs_create_tcp_sock4);
1943b61d59ffSTrond Myklebust }
1944a246b010SChuck Lever 
1945b61d59ffSTrond Myklebust static struct socket *xs_create_tcp_sock6(struct rpc_xprt *xprt,
1946b61d59ffSTrond Myklebust 		struct sock_xprt *transport)
1947b61d59ffSTrond Myklebust {
1948b61d59ffSTrond Myklebust 	struct socket *sock;
1949b61d59ffSTrond Myklebust 	int err;
1950b61d59ffSTrond Myklebust 
1951a246b010SChuck Lever 	/* start from scratch */
1952b61d59ffSTrond Myklebust 	err = sock_create_kern(PF_INET6, SOCK_STREAM, IPPROTO_TCP, &sock);
1953b61d59ffSTrond Myklebust 	if (err < 0) {
1954b61d59ffSTrond Myklebust 		dprintk("RPC:       can't create TCP transport socket (%d).\n",
1955b61d59ffSTrond Myklebust 				-err);
1956b61d59ffSTrond Myklebust 		goto out_err;
19579903cd1cSChuck Lever 	}
1958b61d59ffSTrond Myklebust 	xs_reclassify_socket6(sock);
19599903cd1cSChuck Lever 
1960b61d59ffSTrond Myklebust 	if (xs_bind6(transport, sock) < 0) {
1961a246b010SChuck Lever 		sock_release(sock);
1962b61d59ffSTrond Myklebust 		goto out_err;
1963a246b010SChuck Lever 	}
1964b61d59ffSTrond Myklebust 	return sock;
1965b61d59ffSTrond Myklebust out_err:
1966b61d59ffSTrond Myklebust 	return ERR_PTR(-EIO);
1967a246b010SChuck Lever }
1968a246b010SChuck Lever 
1969a246b010SChuck Lever /**
197068e220bdSChuck Lever  * xs_tcp_connect_worker6 - connect a TCP socket to a remote endpoint
197168e220bdSChuck Lever  * @work: RPC transport to connect
197268e220bdSChuck Lever  *
197368e220bdSChuck Lever  * Invoked by a work queue tasklet.
197468e220bdSChuck Lever  */
197568e220bdSChuck Lever static void xs_tcp_connect_worker6(struct work_struct *work)
197668e220bdSChuck Lever {
197768e220bdSChuck Lever 	struct sock_xprt *transport =
197868e220bdSChuck Lever 		container_of(work, struct sock_xprt, connect_worker.work);
197968e220bdSChuck Lever 	struct rpc_xprt *xprt = &transport->xprt;
198068e220bdSChuck Lever 
1981b61d59ffSTrond Myklebust 	xs_tcp_setup_socket(xprt, transport, xs_create_tcp_sock6);
198268e220bdSChuck Lever }
198368e220bdSChuck Lever 
198468e220bdSChuck Lever /**
1985a246b010SChuck Lever  * xs_connect - connect a socket to a remote endpoint
1986a246b010SChuck Lever  * @task: address of RPC task that manages state of connect request
1987a246b010SChuck Lever  *
1988a246b010SChuck Lever  * TCP: If the remote end dropped the connection, delay reconnecting.
198903bf4b70SChuck Lever  *
199003bf4b70SChuck Lever  * UDP socket connects are synchronous, but we use a work queue anyway
199103bf4b70SChuck Lever  * to guarantee that even unprivileged user processes can set up a
199203bf4b70SChuck Lever  * socket on a privileged port.
199303bf4b70SChuck Lever  *
199403bf4b70SChuck Lever  * If a UDP socket connect fails, the delay behavior here prevents
199503bf4b70SChuck Lever  * retry floods (hard mounts).
1996a246b010SChuck Lever  */
1997a246b010SChuck Lever static void xs_connect(struct rpc_task *task)
1998a246b010SChuck Lever {
1999a246b010SChuck Lever 	struct rpc_xprt *xprt = task->tk_xprt;
2000ee0ac0c2SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2001a246b010SChuck Lever 
200209a21c41SChuck Lever 	if (transport->sock != NULL && !RPC_IS_SOFTCONN(task)) {
200346121cf7SChuck Lever 		dprintk("RPC:       xs_connect delayed xprt %p for %lu "
200446121cf7SChuck Lever 				"seconds\n",
200503bf4b70SChuck Lever 				xprt, xprt->reestablish_timeout / HZ);
2006c1384c9cSTrond Myklebust 		queue_delayed_work(rpciod_workqueue,
2007c1384c9cSTrond Myklebust 				   &transport->connect_worker,
200803bf4b70SChuck Lever 				   xprt->reestablish_timeout);
200903bf4b70SChuck Lever 		xprt->reestablish_timeout <<= 1;
201061d0a8e6SNeil Brown 		if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
201161d0a8e6SNeil Brown 			xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
201203bf4b70SChuck Lever 		if (xprt->reestablish_timeout > XS_TCP_MAX_REEST_TO)
201303bf4b70SChuck Lever 			xprt->reestablish_timeout = XS_TCP_MAX_REEST_TO;
20149903cd1cSChuck Lever 	} else {
20159903cd1cSChuck Lever 		dprintk("RPC:       xs_connect scheduled xprt %p\n", xprt);
2016c1384c9cSTrond Myklebust 		queue_delayed_work(rpciod_workqueue,
2017c1384c9cSTrond Myklebust 				   &transport->connect_worker, 0);
2018a246b010SChuck Lever 	}
2019a246b010SChuck Lever }
2020a246b010SChuck Lever 
2021262ca07dSChuck Lever /**
2022262ca07dSChuck Lever  * xs_udp_print_stats - display UDP socket-specifc stats
2023262ca07dSChuck Lever  * @xprt: rpc_xprt struct containing statistics
2024262ca07dSChuck Lever  * @seq: output file
2025262ca07dSChuck Lever  *
2026262ca07dSChuck Lever  */
2027262ca07dSChuck Lever static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2028262ca07dSChuck Lever {
2029c8475461SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2030c8475461SChuck Lever 
2031262ca07dSChuck Lever 	seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %Lu %Lu\n",
2032fbfffbd5SChuck Lever 			transport->srcport,
2033262ca07dSChuck Lever 			xprt->stat.bind_count,
2034262ca07dSChuck Lever 			xprt->stat.sends,
2035262ca07dSChuck Lever 			xprt->stat.recvs,
2036262ca07dSChuck Lever 			xprt->stat.bad_xids,
2037262ca07dSChuck Lever 			xprt->stat.req_u,
2038262ca07dSChuck Lever 			xprt->stat.bklog_u);
2039262ca07dSChuck Lever }
2040262ca07dSChuck Lever 
2041262ca07dSChuck Lever /**
2042262ca07dSChuck Lever  * xs_tcp_print_stats - display TCP socket-specifc stats
2043262ca07dSChuck Lever  * @xprt: rpc_xprt struct containing statistics
2044262ca07dSChuck Lever  * @seq: output file
2045262ca07dSChuck Lever  *
2046262ca07dSChuck Lever  */
2047262ca07dSChuck Lever static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2048262ca07dSChuck Lever {
2049c8475461SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2050262ca07dSChuck Lever 	long idle_time = 0;
2051262ca07dSChuck Lever 
2052262ca07dSChuck Lever 	if (xprt_connected(xprt))
2053262ca07dSChuck Lever 		idle_time = (long)(jiffies - xprt->last_used) / HZ;
2054262ca07dSChuck Lever 
2055262ca07dSChuck Lever 	seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu %Lu %Lu\n",
2056fbfffbd5SChuck Lever 			transport->srcport,
2057262ca07dSChuck Lever 			xprt->stat.bind_count,
2058262ca07dSChuck Lever 			xprt->stat.connect_count,
2059262ca07dSChuck Lever 			xprt->stat.connect_time,
2060262ca07dSChuck Lever 			idle_time,
2061262ca07dSChuck Lever 			xprt->stat.sends,
2062262ca07dSChuck Lever 			xprt->stat.recvs,
2063262ca07dSChuck Lever 			xprt->stat.bad_xids,
2064262ca07dSChuck Lever 			xprt->stat.req_u,
2065262ca07dSChuck Lever 			xprt->stat.bklog_u);
2066262ca07dSChuck Lever }
2067262ca07dSChuck Lever 
20684cfc7e60SRahul Iyer /*
20694cfc7e60SRahul Iyer  * Allocate a bunch of pages for a scratch buffer for the rpc code. The reason
20704cfc7e60SRahul Iyer  * we allocate pages instead doing a kmalloc like rpc_malloc is because we want
20714cfc7e60SRahul Iyer  * to use the server side send routines.
20724cfc7e60SRahul Iyer  */
20735a51f13aSH Hartley Sweeten static void *bc_malloc(struct rpc_task *task, size_t size)
20744cfc7e60SRahul Iyer {
20754cfc7e60SRahul Iyer 	struct page *page;
20764cfc7e60SRahul Iyer 	struct rpc_buffer *buf;
20774cfc7e60SRahul Iyer 
20784cfc7e60SRahul Iyer 	BUG_ON(size > PAGE_SIZE - sizeof(struct rpc_buffer));
20794cfc7e60SRahul Iyer 	page = alloc_page(GFP_KERNEL);
20804cfc7e60SRahul Iyer 
20814cfc7e60SRahul Iyer 	if (!page)
20824cfc7e60SRahul Iyer 		return NULL;
20834cfc7e60SRahul Iyer 
20844cfc7e60SRahul Iyer 	buf = page_address(page);
20854cfc7e60SRahul Iyer 	buf->len = PAGE_SIZE;
20864cfc7e60SRahul Iyer 
20874cfc7e60SRahul Iyer 	return buf->data;
20884cfc7e60SRahul Iyer }
20894cfc7e60SRahul Iyer 
20904cfc7e60SRahul Iyer /*
20914cfc7e60SRahul Iyer  * Free the space allocated in the bc_alloc routine
20924cfc7e60SRahul Iyer  */
20935a51f13aSH Hartley Sweeten static void bc_free(void *buffer)
20944cfc7e60SRahul Iyer {
20954cfc7e60SRahul Iyer 	struct rpc_buffer *buf;
20964cfc7e60SRahul Iyer 
20974cfc7e60SRahul Iyer 	if (!buffer)
20984cfc7e60SRahul Iyer 		return;
20994cfc7e60SRahul Iyer 
21004cfc7e60SRahul Iyer 	buf = container_of(buffer, struct rpc_buffer, data);
21014cfc7e60SRahul Iyer 	free_page((unsigned long)buf);
21024cfc7e60SRahul Iyer }
21034cfc7e60SRahul Iyer 
21044cfc7e60SRahul Iyer /*
21054cfc7e60SRahul Iyer  * Use the svc_sock to send the callback. Must be called with svsk->sk_mutex
21064cfc7e60SRahul Iyer  * held. Borrows heavily from svc_tcp_sendto and xs_tcp_send_request.
21074cfc7e60SRahul Iyer  */
21084cfc7e60SRahul Iyer static int bc_sendto(struct rpc_rqst *req)
21094cfc7e60SRahul Iyer {
21104cfc7e60SRahul Iyer 	int len;
21114cfc7e60SRahul Iyer 	struct xdr_buf *xbufp = &req->rq_snd_buf;
21124cfc7e60SRahul Iyer 	struct rpc_xprt *xprt = req->rq_xprt;
21134cfc7e60SRahul Iyer 	struct sock_xprt *transport =
21144cfc7e60SRahul Iyer 				container_of(xprt, struct sock_xprt, xprt);
21154cfc7e60SRahul Iyer 	struct socket *sock = transport->sock;
21164cfc7e60SRahul Iyer 	unsigned long headoff;
21174cfc7e60SRahul Iyer 	unsigned long tailoff;
21184cfc7e60SRahul Iyer 
21194cfc7e60SRahul Iyer 	/*
21204cfc7e60SRahul Iyer 	 * Set up the rpc header and record marker stuff
21214cfc7e60SRahul Iyer 	 */
21224cfc7e60SRahul Iyer 	xs_encode_tcp_record_marker(xbufp);
21234cfc7e60SRahul Iyer 
21244cfc7e60SRahul Iyer 	tailoff = (unsigned long)xbufp->tail[0].iov_base & ~PAGE_MASK;
21254cfc7e60SRahul Iyer 	headoff = (unsigned long)xbufp->head[0].iov_base & ~PAGE_MASK;
21264cfc7e60SRahul Iyer 	len = svc_send_common(sock, xbufp,
21274cfc7e60SRahul Iyer 			      virt_to_page(xbufp->head[0].iov_base), headoff,
21284cfc7e60SRahul Iyer 			      xbufp->tail[0].iov_base, tailoff);
21294cfc7e60SRahul Iyer 
21304cfc7e60SRahul Iyer 	if (len != xbufp->len) {
21314cfc7e60SRahul Iyer 		printk(KERN_NOTICE "Error sending entire callback!\n");
21324cfc7e60SRahul Iyer 		len = -EAGAIN;
21334cfc7e60SRahul Iyer 	}
21344cfc7e60SRahul Iyer 
21354cfc7e60SRahul Iyer 	return len;
21364cfc7e60SRahul Iyer }
21374cfc7e60SRahul Iyer 
21384cfc7e60SRahul Iyer /*
21394cfc7e60SRahul Iyer  * The send routine. Borrows from svc_send
21404cfc7e60SRahul Iyer  */
21414cfc7e60SRahul Iyer static int bc_send_request(struct rpc_task *task)
21424cfc7e60SRahul Iyer {
21434cfc7e60SRahul Iyer 	struct rpc_rqst *req = task->tk_rqstp;
21444cfc7e60SRahul Iyer 	struct svc_xprt	*xprt;
21454cfc7e60SRahul Iyer 	struct svc_sock         *svsk;
21464cfc7e60SRahul Iyer 	u32                     len;
21474cfc7e60SRahul Iyer 
21484cfc7e60SRahul Iyer 	dprintk("sending request with xid: %08x\n", ntohl(req->rq_xid));
21494cfc7e60SRahul Iyer 	/*
21504cfc7e60SRahul Iyer 	 * Get the server socket associated with this callback xprt
21514cfc7e60SRahul Iyer 	 */
21524cfc7e60SRahul Iyer 	xprt = req->rq_xprt->bc_xprt;
21534cfc7e60SRahul Iyer 	svsk = container_of(xprt, struct svc_sock, sk_xprt);
21544cfc7e60SRahul Iyer 
21554cfc7e60SRahul Iyer 	/*
21564cfc7e60SRahul Iyer 	 * Grab the mutex to serialize data as the connection is shared
21574cfc7e60SRahul Iyer 	 * with the fore channel
21584cfc7e60SRahul Iyer 	 */
21594cfc7e60SRahul Iyer 	if (!mutex_trylock(&xprt->xpt_mutex)) {
21604cfc7e60SRahul Iyer 		rpc_sleep_on(&xprt->xpt_bc_pending, task, NULL);
21614cfc7e60SRahul Iyer 		if (!mutex_trylock(&xprt->xpt_mutex))
21624cfc7e60SRahul Iyer 			return -EAGAIN;
21634cfc7e60SRahul Iyer 		rpc_wake_up_queued_task(&xprt->xpt_bc_pending, task);
21644cfc7e60SRahul Iyer 	}
21654cfc7e60SRahul Iyer 	if (test_bit(XPT_DEAD, &xprt->xpt_flags))
21664cfc7e60SRahul Iyer 		len = -ENOTCONN;
21674cfc7e60SRahul Iyer 	else
21684cfc7e60SRahul Iyer 		len = bc_sendto(req);
21694cfc7e60SRahul Iyer 	mutex_unlock(&xprt->xpt_mutex);
21704cfc7e60SRahul Iyer 
21714cfc7e60SRahul Iyer 	if (len > 0)
21724cfc7e60SRahul Iyer 		len = 0;
21734cfc7e60SRahul Iyer 
21744cfc7e60SRahul Iyer 	return len;
21754cfc7e60SRahul Iyer }
21764cfc7e60SRahul Iyer 
21774cfc7e60SRahul Iyer /*
21784cfc7e60SRahul Iyer  * The close routine. Since this is client initiated, we do nothing
21794cfc7e60SRahul Iyer  */
21804cfc7e60SRahul Iyer 
21814cfc7e60SRahul Iyer static void bc_close(struct rpc_xprt *xprt)
21824cfc7e60SRahul Iyer {
21834cfc7e60SRahul Iyer }
21844cfc7e60SRahul Iyer 
21854cfc7e60SRahul Iyer /*
21864cfc7e60SRahul Iyer  * The xprt destroy routine. Again, because this connection is client
21874cfc7e60SRahul Iyer  * initiated, we do nothing
21884cfc7e60SRahul Iyer  */
21894cfc7e60SRahul Iyer 
21904cfc7e60SRahul Iyer static void bc_destroy(struct rpc_xprt *xprt)
21914cfc7e60SRahul Iyer {
21924cfc7e60SRahul Iyer }
21934cfc7e60SRahul Iyer 
2194262965f5SChuck Lever static struct rpc_xprt_ops xs_udp_ops = {
219543118c29SChuck Lever 	.set_buffer_size	= xs_udp_set_buffer_size,
219612a80469SChuck Lever 	.reserve_xprt		= xprt_reserve_xprt_cong,
219749e9a890SChuck Lever 	.release_xprt		= xprt_release_xprt_cong,
219845160d62SChuck Lever 	.rpcbind		= rpcb_getport_async,
219992200412SChuck Lever 	.set_port		= xs_set_port,
22009903cd1cSChuck Lever 	.connect		= xs_connect,
220102107148SChuck Lever 	.buf_alloc		= rpc_malloc,
220202107148SChuck Lever 	.buf_free		= rpc_free,
2203262965f5SChuck Lever 	.send_request		= xs_udp_send_request,
2204fe3aca29SChuck Lever 	.set_retrans_timeout	= xprt_set_retrans_timeout_rtt,
220546c0ee8bSChuck Lever 	.timer			= xs_udp_timer,
2206a58dd398SChuck Lever 	.release_request	= xprt_release_rqst_cong,
2207262965f5SChuck Lever 	.close			= xs_close,
2208262965f5SChuck Lever 	.destroy		= xs_destroy,
2209262ca07dSChuck Lever 	.print_stats		= xs_udp_print_stats,
2210262965f5SChuck Lever };
2211262965f5SChuck Lever 
2212262965f5SChuck Lever static struct rpc_xprt_ops xs_tcp_ops = {
221312a80469SChuck Lever 	.reserve_xprt		= xprt_reserve_xprt,
2214e0ab53deSTrond Myklebust 	.release_xprt		= xs_tcp_release_xprt,
221545160d62SChuck Lever 	.rpcbind		= rpcb_getport_async,
221692200412SChuck Lever 	.set_port		= xs_set_port,
22170b9e7943STrond Myklebust 	.connect		= xs_connect,
221802107148SChuck Lever 	.buf_alloc		= rpc_malloc,
221902107148SChuck Lever 	.buf_free		= rpc_free,
2220262965f5SChuck Lever 	.send_request		= xs_tcp_send_request,
2221fe3aca29SChuck Lever 	.set_retrans_timeout	= xprt_set_retrans_timeout_def,
2222f75e6745STrond Myklebust 	.close			= xs_tcp_close,
22239903cd1cSChuck Lever 	.destroy		= xs_destroy,
2224262ca07dSChuck Lever 	.print_stats		= xs_tcp_print_stats,
2225a246b010SChuck Lever };
2226a246b010SChuck Lever 
22274cfc7e60SRahul Iyer /*
22284cfc7e60SRahul Iyer  * The rpc_xprt_ops for the server backchannel
22294cfc7e60SRahul Iyer  */
22304cfc7e60SRahul Iyer 
22314cfc7e60SRahul Iyer static struct rpc_xprt_ops bc_tcp_ops = {
22324cfc7e60SRahul Iyer 	.reserve_xprt		= xprt_reserve_xprt,
22334cfc7e60SRahul Iyer 	.release_xprt		= xprt_release_xprt,
22344cfc7e60SRahul Iyer 	.buf_alloc		= bc_malloc,
22354cfc7e60SRahul Iyer 	.buf_free		= bc_free,
22364cfc7e60SRahul Iyer 	.send_request		= bc_send_request,
22374cfc7e60SRahul Iyer 	.set_retrans_timeout	= xprt_set_retrans_timeout_def,
22384cfc7e60SRahul Iyer 	.close			= bc_close,
22394cfc7e60SRahul Iyer 	.destroy		= bc_destroy,
22404cfc7e60SRahul Iyer 	.print_stats		= xs_tcp_print_stats,
22414cfc7e60SRahul Iyer };
22424cfc7e60SRahul Iyer 
22433c341b0bS\"Talpey, Thomas\ static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args,
2244bc25571eS\"Talpey, Thomas\ 				      unsigned int slot_table_size)
2245c8541ecdSChuck Lever {
2246c8541ecdSChuck Lever 	struct rpc_xprt *xprt;
2247ffc2e518SChuck Lever 	struct sock_xprt *new;
2248c8541ecdSChuck Lever 
224996802a09SFrank van Maarseveen 	if (args->addrlen > sizeof(xprt->addr)) {
2250c8541ecdSChuck Lever 		dprintk("RPC:       xs_setup_xprt: address too large\n");
2251c8541ecdSChuck Lever 		return ERR_PTR(-EBADF);
2252c8541ecdSChuck Lever 	}
2253c8541ecdSChuck Lever 
2254ffc2e518SChuck Lever 	new = kzalloc(sizeof(*new), GFP_KERNEL);
2255ffc2e518SChuck Lever 	if (new == NULL) {
225646121cf7SChuck Lever 		dprintk("RPC:       xs_setup_xprt: couldn't allocate "
225746121cf7SChuck Lever 				"rpc_xprt\n");
2258c8541ecdSChuck Lever 		return ERR_PTR(-ENOMEM);
2259c8541ecdSChuck Lever 	}
2260ffc2e518SChuck Lever 	xprt = &new->xprt;
2261c8541ecdSChuck Lever 
2262c8541ecdSChuck Lever 	xprt->max_reqs = slot_table_size;
2263c8541ecdSChuck Lever 	xprt->slot = kcalloc(xprt->max_reqs, sizeof(struct rpc_rqst), GFP_KERNEL);
2264c8541ecdSChuck Lever 	if (xprt->slot == NULL) {
2265c8541ecdSChuck Lever 		kfree(xprt);
226646121cf7SChuck Lever 		dprintk("RPC:       xs_setup_xprt: couldn't allocate slot "
226746121cf7SChuck Lever 				"table\n");
2268c8541ecdSChuck Lever 		return ERR_PTR(-ENOMEM);
2269c8541ecdSChuck Lever 	}
2270c8541ecdSChuck Lever 
227196802a09SFrank van Maarseveen 	memcpy(&xprt->addr, args->dstaddr, args->addrlen);
227296802a09SFrank van Maarseveen 	xprt->addrlen = args->addrlen;
2273d3bc9a1dSFrank van Maarseveen 	if (args->srcaddr)
2274fbfffbd5SChuck Lever 		memcpy(&new->srcaddr, args->srcaddr, args->addrlen);
2275c8541ecdSChuck Lever 
2276c8541ecdSChuck Lever 	return xprt;
2277c8541ecdSChuck Lever }
2278c8541ecdSChuck Lever 
22792881ae74STrond Myklebust static const struct rpc_timeout xs_udp_default_timeout = {
22802881ae74STrond Myklebust 	.to_initval = 5 * HZ,
22812881ae74STrond Myklebust 	.to_maxval = 30 * HZ,
22822881ae74STrond Myklebust 	.to_increment = 5 * HZ,
22832881ae74STrond Myklebust 	.to_retries = 5,
22842881ae74STrond Myklebust };
22852881ae74STrond Myklebust 
22869903cd1cSChuck Lever /**
22879903cd1cSChuck Lever  * xs_setup_udp - Set up transport to use a UDP socket
228896802a09SFrank van Maarseveen  * @args: rpc transport creation arguments
22899903cd1cSChuck Lever  *
22909903cd1cSChuck Lever  */
2291483066d6SAdrian Bunk static struct rpc_xprt *xs_setup_udp(struct xprt_create *args)
2292a246b010SChuck Lever {
22938f9d5b1aSChuck Lever 	struct sockaddr *addr = args->dstaddr;
2294c8541ecdSChuck Lever 	struct rpc_xprt *xprt;
2295c8475461SChuck Lever 	struct sock_xprt *transport;
2296*0a68b0beSJ. Bruce Fields 	struct rpc_xprt *ret;
2297a246b010SChuck Lever 
229896802a09SFrank van Maarseveen 	xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries);
2299c8541ecdSChuck Lever 	if (IS_ERR(xprt))
2300c8541ecdSChuck Lever 		return xprt;
2301c8475461SChuck Lever 	transport = container_of(xprt, struct sock_xprt, xprt);
2302a246b010SChuck Lever 
2303ec739ef0SChuck Lever 	xprt->prot = IPPROTO_UDP;
2304808012fbSChuck Lever 	xprt->tsh_size = 0;
2305a246b010SChuck Lever 	/* XXX: header size can vary due to auth type, IPv6, etc. */
2306a246b010SChuck Lever 	xprt->max_payload = (1U << 16) - (MAX_HEADER << 3);
2307a246b010SChuck Lever 
230803bf4b70SChuck Lever 	xprt->bind_timeout = XS_BIND_TO;
230903bf4b70SChuck Lever 	xprt->reestablish_timeout = XS_UDP_REEST_TO;
231003bf4b70SChuck Lever 	xprt->idle_timeout = XS_IDLE_DISC_TO;
2311a246b010SChuck Lever 
2312262965f5SChuck Lever 	xprt->ops = &xs_udp_ops;
2313a246b010SChuck Lever 
2314ba7392bbSTrond Myklebust 	xprt->timeout = &xs_udp_default_timeout;
2315a246b010SChuck Lever 
23168f9d5b1aSChuck Lever 	switch (addr->sa_family) {
23178f9d5b1aSChuck Lever 	case AF_INET:
23188f9d5b1aSChuck Lever 		if (((struct sockaddr_in *)addr)->sin_port != htons(0))
23198f9d5b1aSChuck Lever 			xprt_set_bound(xprt);
23208f9d5b1aSChuck Lever 
23218f9d5b1aSChuck Lever 		INIT_DELAYED_WORK(&transport->connect_worker,
23228f9d5b1aSChuck Lever 					xs_udp_connect_worker4);
23239dc3b095SChuck Lever 		xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP);
23248f9d5b1aSChuck Lever 		break;
23258f9d5b1aSChuck Lever 	case AF_INET6:
23268f9d5b1aSChuck Lever 		if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
23278f9d5b1aSChuck Lever 			xprt_set_bound(xprt);
23288f9d5b1aSChuck Lever 
23298f9d5b1aSChuck Lever 		INIT_DELAYED_WORK(&transport->connect_worker,
23308f9d5b1aSChuck Lever 					xs_udp_connect_worker6);
23319dc3b095SChuck Lever 		xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6);
23328f9d5b1aSChuck Lever 		break;
23338f9d5b1aSChuck Lever 	default:
2334*0a68b0beSJ. Bruce Fields 		ret = ERR_PTR(-EAFNOSUPPORT);
2335*0a68b0beSJ. Bruce Fields 		goto out_err;
23368f9d5b1aSChuck Lever 	}
23378f9d5b1aSChuck Lever 
2338c740eff8SChuck Lever 	if (xprt_bound(xprt))
2339c740eff8SChuck Lever 		dprintk("RPC:       set up xprt to %s (port %s) via %s\n",
2340c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_ADDR],
2341c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_PORT],
2342c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_PROTO]);
2343c740eff8SChuck Lever 	else
2344c740eff8SChuck Lever 		dprintk("RPC:       set up xprt to %s (autobind) via %s\n",
2345c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_ADDR],
2346c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_PROTO]);
2347edb267a6SChuck Lever 
2348bc25571eS\"Talpey, Thomas\ 	if (try_module_get(THIS_MODULE))
2349c8541ecdSChuck Lever 		return xprt;
2350*0a68b0beSJ. Bruce Fields 	ret = ERR_PTR(-EINVAL);
2351*0a68b0beSJ. Bruce Fields out_err:
2352bc25571eS\"Talpey, Thomas\ 	kfree(xprt->slot);
2353bc25571eS\"Talpey, Thomas\ 	kfree(xprt);
2354*0a68b0beSJ. Bruce Fields 	return ret;
2355a246b010SChuck Lever }
2356a246b010SChuck Lever 
23572881ae74STrond Myklebust static const struct rpc_timeout xs_tcp_default_timeout = {
23582881ae74STrond Myklebust 	.to_initval = 60 * HZ,
23592881ae74STrond Myklebust 	.to_maxval = 60 * HZ,
23602881ae74STrond Myklebust 	.to_retries = 2,
23612881ae74STrond Myklebust };
23622881ae74STrond Myklebust 
23639903cd1cSChuck Lever /**
23649903cd1cSChuck Lever  * xs_setup_tcp - Set up transport to use a TCP socket
236596802a09SFrank van Maarseveen  * @args: rpc transport creation arguments
23669903cd1cSChuck Lever  *
23679903cd1cSChuck Lever  */
2368483066d6SAdrian Bunk static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args)
2369a246b010SChuck Lever {
23708f9d5b1aSChuck Lever 	struct sockaddr *addr = args->dstaddr;
2371c8541ecdSChuck Lever 	struct rpc_xprt *xprt;
2372c8475461SChuck Lever 	struct sock_xprt *transport;
2373*0a68b0beSJ. Bruce Fields 	struct rpc_xprt *ret;
2374a246b010SChuck Lever 
237596802a09SFrank van Maarseveen 	xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries);
2376c8541ecdSChuck Lever 	if (IS_ERR(xprt))
2377c8541ecdSChuck Lever 		return xprt;
2378c8475461SChuck Lever 	transport = container_of(xprt, struct sock_xprt, xprt);
2379a246b010SChuck Lever 
2380ec739ef0SChuck Lever 	xprt->prot = IPPROTO_TCP;
2381808012fbSChuck Lever 	xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
2382808012fbSChuck Lever 	xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2383a246b010SChuck Lever 
238403bf4b70SChuck Lever 	xprt->bind_timeout = XS_BIND_TO;
238503bf4b70SChuck Lever 	xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
238603bf4b70SChuck Lever 	xprt->idle_timeout = XS_IDLE_DISC_TO;
2387a246b010SChuck Lever 
2388262965f5SChuck Lever 	xprt->ops = &xs_tcp_ops;
2389ba7392bbSTrond Myklebust 	xprt->timeout = &xs_tcp_default_timeout;
2390a246b010SChuck Lever 
23918f9d5b1aSChuck Lever 	switch (addr->sa_family) {
23928f9d5b1aSChuck Lever 	case AF_INET:
23938f9d5b1aSChuck Lever 		if (((struct sockaddr_in *)addr)->sin_port != htons(0))
23948f9d5b1aSChuck Lever 			xprt_set_bound(xprt);
23958f9d5b1aSChuck Lever 
23969dc3b095SChuck Lever 		INIT_DELAYED_WORK(&transport->connect_worker,
23979dc3b095SChuck Lever 					xs_tcp_connect_worker4);
23989dc3b095SChuck Lever 		xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP);
23998f9d5b1aSChuck Lever 		break;
24008f9d5b1aSChuck Lever 	case AF_INET6:
24018f9d5b1aSChuck Lever 		if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
24028f9d5b1aSChuck Lever 			xprt_set_bound(xprt);
24038f9d5b1aSChuck Lever 
24049dc3b095SChuck Lever 		INIT_DELAYED_WORK(&transport->connect_worker,
24059dc3b095SChuck Lever 					xs_tcp_connect_worker6);
24069dc3b095SChuck Lever 		xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6);
24078f9d5b1aSChuck Lever 		break;
24088f9d5b1aSChuck Lever 	default:
2409*0a68b0beSJ. Bruce Fields 		ret = ERR_PTR(-EAFNOSUPPORT);
2410*0a68b0beSJ. Bruce Fields 		goto out_err;
24118f9d5b1aSChuck Lever 	}
24128f9d5b1aSChuck Lever 
2413c740eff8SChuck Lever 	if (xprt_bound(xprt))
2414c740eff8SChuck Lever 		dprintk("RPC:       set up xprt to %s (port %s) via %s\n",
2415c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_ADDR],
2416c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_PORT],
2417c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_PROTO]);
2418c740eff8SChuck Lever 	else
2419c740eff8SChuck Lever 		dprintk("RPC:       set up xprt to %s (autobind) via %s\n",
2420c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_ADDR],
2421c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_PROTO]);
2422c740eff8SChuck Lever 
2423edb267a6SChuck Lever 
2424bc25571eS\"Talpey, Thomas\ 	if (try_module_get(THIS_MODULE))
2425c8541ecdSChuck Lever 		return xprt;
2426*0a68b0beSJ. Bruce Fields 	ret = ERR_PTR(-EINVAL);
2427*0a68b0beSJ. Bruce Fields out_err:
2428bc25571eS\"Talpey, Thomas\ 	kfree(xprt->slot);
2429bc25571eS\"Talpey, Thomas\ 	kfree(xprt);
2430*0a68b0beSJ. Bruce Fields 	return ret;
2431a246b010SChuck Lever }
2432282b32e1SChuck Lever 
2433f300babaSAlexandros Batsakis /**
2434f300babaSAlexandros Batsakis  * xs_setup_bc_tcp - Set up transport to use a TCP backchannel socket
2435f300babaSAlexandros Batsakis  * @args: rpc transport creation arguments
2436f300babaSAlexandros Batsakis  *
2437f300babaSAlexandros Batsakis  */
2438f300babaSAlexandros Batsakis static struct rpc_xprt *xs_setup_bc_tcp(struct xprt_create *args)
2439f300babaSAlexandros Batsakis {
2440f300babaSAlexandros Batsakis 	struct sockaddr *addr = args->dstaddr;
2441f300babaSAlexandros Batsakis 	struct rpc_xprt *xprt;
2442f300babaSAlexandros Batsakis 	struct sock_xprt *transport;
2443f300babaSAlexandros Batsakis 	struct svc_sock *bc_sock;
2444*0a68b0beSJ. Bruce Fields 	struct rpc_xprt *ret;
2445f300babaSAlexandros Batsakis 
2446f300babaSAlexandros Batsakis 	xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries);
2447f300babaSAlexandros Batsakis 	if (IS_ERR(xprt))
2448f300babaSAlexandros Batsakis 		return xprt;
2449f300babaSAlexandros Batsakis 	transport = container_of(xprt, struct sock_xprt, xprt);
2450f300babaSAlexandros Batsakis 
2451f300babaSAlexandros Batsakis 	xprt->prot = IPPROTO_TCP;
2452f300babaSAlexandros Batsakis 	xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
2453f300babaSAlexandros Batsakis 	xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2454f300babaSAlexandros Batsakis 	xprt->timeout = &xs_tcp_default_timeout;
2455f300babaSAlexandros Batsakis 
2456f300babaSAlexandros Batsakis 	/* backchannel */
2457f300babaSAlexandros Batsakis 	xprt_set_bound(xprt);
2458f300babaSAlexandros Batsakis 	xprt->bind_timeout = 0;
2459f300babaSAlexandros Batsakis 	xprt->reestablish_timeout = 0;
2460f300babaSAlexandros Batsakis 	xprt->idle_timeout = 0;
2461f300babaSAlexandros Batsakis 
2462f300babaSAlexandros Batsakis 	/*
2463f300babaSAlexandros Batsakis 	 * The backchannel uses the same socket connection as the
2464f300babaSAlexandros Batsakis 	 * forechannel
2465f300babaSAlexandros Batsakis 	 */
2466f300babaSAlexandros Batsakis 	xprt->bc_xprt = args->bc_xprt;
2467f300babaSAlexandros Batsakis 	bc_sock = container_of(args->bc_xprt, struct svc_sock, sk_xprt);
2468f300babaSAlexandros Batsakis 	bc_sock->sk_bc_xprt = xprt;
2469f300babaSAlexandros Batsakis 	transport->sock = bc_sock->sk_sock;
2470f300babaSAlexandros Batsakis 	transport->inet = bc_sock->sk_sk;
2471f300babaSAlexandros Batsakis 
2472f300babaSAlexandros Batsakis 	xprt->ops = &bc_tcp_ops;
2473f300babaSAlexandros Batsakis 
2474f300babaSAlexandros Batsakis 	switch (addr->sa_family) {
2475f300babaSAlexandros Batsakis 	case AF_INET:
2476f300babaSAlexandros Batsakis 		xs_format_peer_addresses(xprt, "tcp",
2477f300babaSAlexandros Batsakis 					 RPCBIND_NETID_TCP);
2478f300babaSAlexandros Batsakis 		break;
2479f300babaSAlexandros Batsakis 	case AF_INET6:
2480f300babaSAlexandros Batsakis 		xs_format_peer_addresses(xprt, "tcp",
2481f300babaSAlexandros Batsakis 				   RPCBIND_NETID_TCP6);
2482f300babaSAlexandros Batsakis 		break;
2483f300babaSAlexandros Batsakis 	default:
2484*0a68b0beSJ. Bruce Fields 		ret = ERR_PTR(-EAFNOSUPPORT);
2485*0a68b0beSJ. Bruce Fields 		goto out_err;
2486f300babaSAlexandros Batsakis 	}
2487f300babaSAlexandros Batsakis 
2488f300babaSAlexandros Batsakis 	if (xprt_bound(xprt))
2489f300babaSAlexandros Batsakis 		dprintk("RPC:       set up xprt to %s (port %s) via %s\n",
2490f300babaSAlexandros Batsakis 				xprt->address_strings[RPC_DISPLAY_ADDR],
2491f300babaSAlexandros Batsakis 				xprt->address_strings[RPC_DISPLAY_PORT],
2492f300babaSAlexandros Batsakis 				xprt->address_strings[RPC_DISPLAY_PROTO]);
2493f300babaSAlexandros Batsakis 	else
2494f300babaSAlexandros Batsakis 		dprintk("RPC:       set up xprt to %s (autobind) via %s\n",
2495f300babaSAlexandros Batsakis 				xprt->address_strings[RPC_DISPLAY_ADDR],
2496f300babaSAlexandros Batsakis 				xprt->address_strings[RPC_DISPLAY_PROTO]);
2497f300babaSAlexandros Batsakis 
2498f300babaSAlexandros Batsakis 	/*
2499f300babaSAlexandros Batsakis 	 * Since we don't want connections for the backchannel, we set
2500f300babaSAlexandros Batsakis 	 * the xprt status to connected
2501f300babaSAlexandros Batsakis 	 */
2502f300babaSAlexandros Batsakis 	xprt_set_connected(xprt);
2503f300babaSAlexandros Batsakis 
2504f300babaSAlexandros Batsakis 
2505f300babaSAlexandros Batsakis 	if (try_module_get(THIS_MODULE))
2506f300babaSAlexandros Batsakis 		return xprt;
2507*0a68b0beSJ. Bruce Fields 	ret = ERR_PTR(-EINVAL);
2508*0a68b0beSJ. Bruce Fields out_err:
2509f300babaSAlexandros Batsakis 	kfree(xprt->slot);
2510f300babaSAlexandros Batsakis 	kfree(xprt);
2511*0a68b0beSJ. Bruce Fields 	return ret;
2512f300babaSAlexandros Batsakis }
2513f300babaSAlexandros Batsakis 
2514bc25571eS\"Talpey, Thomas\ static struct xprt_class	xs_udp_transport = {
2515bc25571eS\"Talpey, Thomas\ 	.list		= LIST_HEAD_INIT(xs_udp_transport.list),
2516bc25571eS\"Talpey, Thomas\ 	.name		= "udp",
2517bc25571eS\"Talpey, Thomas\ 	.owner		= THIS_MODULE,
2518f300babaSAlexandros Batsakis 	.ident		= XPRT_TRANSPORT_UDP,
2519bc25571eS\"Talpey, Thomas\ 	.setup		= xs_setup_udp,
2520bc25571eS\"Talpey, Thomas\ };
2521bc25571eS\"Talpey, Thomas\ 
2522bc25571eS\"Talpey, Thomas\ static struct xprt_class	xs_tcp_transport = {
2523bc25571eS\"Talpey, Thomas\ 	.list		= LIST_HEAD_INIT(xs_tcp_transport.list),
2524bc25571eS\"Talpey, Thomas\ 	.name		= "tcp",
2525bc25571eS\"Talpey, Thomas\ 	.owner		= THIS_MODULE,
2526f300babaSAlexandros Batsakis 	.ident		= XPRT_TRANSPORT_TCP,
2527bc25571eS\"Talpey, Thomas\ 	.setup		= xs_setup_tcp,
2528bc25571eS\"Talpey, Thomas\ };
2529bc25571eS\"Talpey, Thomas\ 
2530f300babaSAlexandros Batsakis static struct xprt_class	xs_bc_tcp_transport = {
2531f300babaSAlexandros Batsakis 	.list		= LIST_HEAD_INIT(xs_bc_tcp_transport.list),
2532f300babaSAlexandros Batsakis 	.name		= "tcp NFSv4.1 backchannel",
2533f300babaSAlexandros Batsakis 	.owner		= THIS_MODULE,
2534f300babaSAlexandros Batsakis 	.ident		= XPRT_TRANSPORT_BC_TCP,
2535f300babaSAlexandros Batsakis 	.setup		= xs_setup_bc_tcp,
2536f300babaSAlexandros Batsakis };
2537f300babaSAlexandros Batsakis 
2538282b32e1SChuck Lever /**
2539bc25571eS\"Talpey, Thomas\  * init_socket_xprt - set up xprtsock's sysctls, register with RPC client
2540282b32e1SChuck Lever  *
2541282b32e1SChuck Lever  */
2542282b32e1SChuck Lever int init_socket_xprt(void)
2543282b32e1SChuck Lever {
2544fbf76683SChuck Lever #ifdef RPC_DEBUG
25452b1bec5fSEric W. Biederman 	if (!sunrpc_table_header)
25460b4d4147SEric W. Biederman 		sunrpc_table_header = register_sysctl_table(sunrpc_table);
2547fbf76683SChuck Lever #endif
2548fbf76683SChuck Lever 
2549bc25571eS\"Talpey, Thomas\ 	xprt_register_transport(&xs_udp_transport);
2550bc25571eS\"Talpey, Thomas\ 	xprt_register_transport(&xs_tcp_transport);
2551f300babaSAlexandros Batsakis 	xprt_register_transport(&xs_bc_tcp_transport);
2552bc25571eS\"Talpey, Thomas\ 
2553282b32e1SChuck Lever 	return 0;
2554282b32e1SChuck Lever }
2555282b32e1SChuck Lever 
2556282b32e1SChuck Lever /**
2557bc25571eS\"Talpey, Thomas\  * cleanup_socket_xprt - remove xprtsock's sysctls, unregister
2558282b32e1SChuck Lever  *
2559282b32e1SChuck Lever  */
2560282b32e1SChuck Lever void cleanup_socket_xprt(void)
2561282b32e1SChuck Lever {
2562fbf76683SChuck Lever #ifdef RPC_DEBUG
2563fbf76683SChuck Lever 	if (sunrpc_table_header) {
2564fbf76683SChuck Lever 		unregister_sysctl_table(sunrpc_table_header);
2565fbf76683SChuck Lever 		sunrpc_table_header = NULL;
2566fbf76683SChuck Lever 	}
2567fbf76683SChuck Lever #endif
2568bc25571eS\"Talpey, Thomas\ 
2569bc25571eS\"Talpey, Thomas\ 	xprt_unregister_transport(&xs_udp_transport);
2570bc25571eS\"Talpey, Thomas\ 	xprt_unregister_transport(&xs_tcp_transport);
2571f300babaSAlexandros Batsakis 	xprt_unregister_transport(&xs_bc_tcp_transport);
2572282b32e1SChuck Lever }
2573cbf11071STrond Myklebust 
2574cbf11071STrond Myklebust static int param_set_uint_minmax(const char *val, struct kernel_param *kp,
2575cbf11071STrond Myklebust 		unsigned int min, unsigned int max)
2576cbf11071STrond Myklebust {
2577cbf11071STrond Myklebust 	unsigned long num;
2578cbf11071STrond Myklebust 	int ret;
2579cbf11071STrond Myklebust 
2580cbf11071STrond Myklebust 	if (!val)
2581cbf11071STrond Myklebust 		return -EINVAL;
2582cbf11071STrond Myklebust 	ret = strict_strtoul(val, 0, &num);
2583cbf11071STrond Myklebust 	if (ret == -EINVAL || num < min || num > max)
2584cbf11071STrond Myklebust 		return -EINVAL;
2585cbf11071STrond Myklebust 	*((unsigned int *)kp->arg) = num;
2586cbf11071STrond Myklebust 	return 0;
2587cbf11071STrond Myklebust }
2588cbf11071STrond Myklebust 
2589cbf11071STrond Myklebust static int param_set_portnr(const char *val, struct kernel_param *kp)
2590cbf11071STrond Myklebust {
2591cbf11071STrond Myklebust 	return param_set_uint_minmax(val, kp,
2592cbf11071STrond Myklebust 			RPC_MIN_RESVPORT,
2593cbf11071STrond Myklebust 			RPC_MAX_RESVPORT);
2594cbf11071STrond Myklebust }
2595cbf11071STrond Myklebust 
2596cbf11071STrond Myklebust static int param_get_portnr(char *buffer, struct kernel_param *kp)
2597cbf11071STrond Myklebust {
2598cbf11071STrond Myklebust 	return param_get_uint(buffer, kp);
2599cbf11071STrond Myklebust }
2600cbf11071STrond Myklebust #define param_check_portnr(name, p) \
2601cbf11071STrond Myklebust 	__param_check(name, p, unsigned int);
2602cbf11071STrond Myklebust 
2603cbf11071STrond Myklebust module_param_named(min_resvport, xprt_min_resvport, portnr, 0644);
2604cbf11071STrond Myklebust module_param_named(max_resvport, xprt_max_resvport, portnr, 0644);
2605cbf11071STrond Myklebust 
2606cbf11071STrond Myklebust static int param_set_slot_table_size(const char *val, struct kernel_param *kp)
2607cbf11071STrond Myklebust {
2608cbf11071STrond Myklebust 	return param_set_uint_minmax(val, kp,
2609cbf11071STrond Myklebust 			RPC_MIN_SLOT_TABLE,
2610cbf11071STrond Myklebust 			RPC_MAX_SLOT_TABLE);
2611cbf11071STrond Myklebust }
2612cbf11071STrond Myklebust 
2613cbf11071STrond Myklebust static int param_get_slot_table_size(char *buffer, struct kernel_param *kp)
2614cbf11071STrond Myklebust {
2615cbf11071STrond Myklebust 	return param_get_uint(buffer, kp);
2616cbf11071STrond Myklebust }
2617cbf11071STrond Myklebust #define param_check_slot_table_size(name, p) \
2618cbf11071STrond Myklebust 	__param_check(name, p, unsigned int);
2619cbf11071STrond Myklebust 
2620cbf11071STrond Myklebust module_param_named(tcp_slot_table_entries, xprt_tcp_slot_table_entries,
2621cbf11071STrond Myklebust 		   slot_table_size, 0644);
2622cbf11071STrond Myklebust module_param_named(udp_slot_table_entries, xprt_udp_slot_table_entries,
2623cbf11071STrond Myklebust 		   slot_table_size, 0644);
2624cbf11071STrond Myklebust 
2625