xref: /openbmc/linux/net/sunrpc/xprtsock.c (revision fba91afbec2c004e2c8733ae9e0ca6998e962c64)
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>
3549c36fccS\"Talpey, Thomas\ #include <linux/sunrpc/xprtsock.h>
36a246b010SChuck Lever #include <linux/file.h>
37a246b010SChuck Lever 
38a246b010SChuck Lever #include <net/sock.h>
39a246b010SChuck Lever #include <net/checksum.h>
40a246b010SChuck Lever #include <net/udp.h>
41a246b010SChuck Lever #include <net/tcp.h>
42a246b010SChuck Lever 
439903cd1cSChuck Lever /*
44c556b754SChuck Lever  * xprtsock tunables
45c556b754SChuck Lever  */
46c556b754SChuck Lever unsigned int xprt_udp_slot_table_entries = RPC_DEF_SLOT_TABLE;
47c556b754SChuck Lever unsigned int xprt_tcp_slot_table_entries = RPC_DEF_SLOT_TABLE;
48c556b754SChuck Lever 
49c556b754SChuck Lever unsigned int xprt_min_resvport = RPC_DEF_MIN_RESVPORT;
50c556b754SChuck Lever unsigned int xprt_max_resvport = RPC_DEF_MAX_RESVPORT;
51c556b754SChuck Lever 
52c556b754SChuck Lever /*
53fbf76683SChuck Lever  * We can register our own files under /proc/sys/sunrpc by
54fbf76683SChuck Lever  * calling register_sysctl_table() again.  The files in that
55fbf76683SChuck Lever  * directory become the union of all files registered there.
56fbf76683SChuck Lever  *
57fbf76683SChuck Lever  * We simply need to make sure that we don't collide with
58fbf76683SChuck Lever  * someone else's file names!
59fbf76683SChuck Lever  */
60fbf76683SChuck Lever 
61fbf76683SChuck Lever #ifdef RPC_DEBUG
62fbf76683SChuck Lever 
63fbf76683SChuck Lever static unsigned int min_slot_table_size = RPC_MIN_SLOT_TABLE;
64fbf76683SChuck Lever static unsigned int max_slot_table_size = RPC_MAX_SLOT_TABLE;
65fbf76683SChuck Lever static unsigned int xprt_min_resvport_limit = RPC_MIN_RESVPORT;
66fbf76683SChuck Lever static unsigned int xprt_max_resvport_limit = RPC_MAX_RESVPORT;
67fbf76683SChuck Lever 
68fbf76683SChuck Lever static struct ctl_table_header *sunrpc_table_header;
69fbf76683SChuck Lever 
70fbf76683SChuck Lever /*
71fbf76683SChuck Lever  * FIXME: changing the UDP slot table size should also resize the UDP
72fbf76683SChuck Lever  *        socket buffers for existing UDP transports
73fbf76683SChuck Lever  */
74fbf76683SChuck Lever static ctl_table xs_tunables_table[] = {
75fbf76683SChuck Lever 	{
76fbf76683SChuck Lever 		.ctl_name	= CTL_SLOTTABLE_UDP,
77fbf76683SChuck Lever 		.procname	= "udp_slot_table_entries",
78fbf76683SChuck Lever 		.data		= &xprt_udp_slot_table_entries,
79fbf76683SChuck Lever 		.maxlen		= sizeof(unsigned int),
80fbf76683SChuck Lever 		.mode		= 0644,
81fbf76683SChuck Lever 		.proc_handler	= &proc_dointvec_minmax,
82fbf76683SChuck Lever 		.strategy	= &sysctl_intvec,
83fbf76683SChuck Lever 		.extra1		= &min_slot_table_size,
84fbf76683SChuck Lever 		.extra2		= &max_slot_table_size
85fbf76683SChuck Lever 	},
86fbf76683SChuck Lever 	{
87fbf76683SChuck Lever 		.ctl_name	= CTL_SLOTTABLE_TCP,
88fbf76683SChuck Lever 		.procname	= "tcp_slot_table_entries",
89fbf76683SChuck Lever 		.data		= &xprt_tcp_slot_table_entries,
90fbf76683SChuck Lever 		.maxlen		= sizeof(unsigned int),
91fbf76683SChuck Lever 		.mode		= 0644,
92fbf76683SChuck Lever 		.proc_handler	= &proc_dointvec_minmax,
93fbf76683SChuck Lever 		.strategy	= &sysctl_intvec,
94fbf76683SChuck Lever 		.extra1		= &min_slot_table_size,
95fbf76683SChuck Lever 		.extra2		= &max_slot_table_size
96fbf76683SChuck Lever 	},
97fbf76683SChuck Lever 	{
98fbf76683SChuck Lever 		.ctl_name	= CTL_MIN_RESVPORT,
99fbf76683SChuck Lever 		.procname	= "min_resvport",
100fbf76683SChuck Lever 		.data		= &xprt_min_resvport,
101fbf76683SChuck Lever 		.maxlen		= sizeof(unsigned int),
102fbf76683SChuck Lever 		.mode		= 0644,
103fbf76683SChuck Lever 		.proc_handler	= &proc_dointvec_minmax,
104fbf76683SChuck Lever 		.strategy	= &sysctl_intvec,
105fbf76683SChuck Lever 		.extra1		= &xprt_min_resvport_limit,
106fbf76683SChuck Lever 		.extra2		= &xprt_max_resvport_limit
107fbf76683SChuck Lever 	},
108fbf76683SChuck Lever 	{
109fbf76683SChuck Lever 		.ctl_name	= CTL_MAX_RESVPORT,
110fbf76683SChuck Lever 		.procname	= "max_resvport",
111fbf76683SChuck Lever 		.data		= &xprt_max_resvport,
112fbf76683SChuck Lever 		.maxlen		= sizeof(unsigned int),
113fbf76683SChuck Lever 		.mode		= 0644,
114fbf76683SChuck Lever 		.proc_handler	= &proc_dointvec_minmax,
115fbf76683SChuck Lever 		.strategy	= &sysctl_intvec,
116fbf76683SChuck Lever 		.extra1		= &xprt_min_resvport_limit,
117fbf76683SChuck Lever 		.extra2		= &xprt_max_resvport_limit
118fbf76683SChuck Lever 	},
119fbf76683SChuck Lever 	{
120fbf76683SChuck Lever 		.ctl_name = 0,
121fbf76683SChuck Lever 	},
122fbf76683SChuck Lever };
123fbf76683SChuck Lever 
124fbf76683SChuck Lever static ctl_table sunrpc_table[] = {
125fbf76683SChuck Lever 	{
126fbf76683SChuck Lever 		.ctl_name	= CTL_SUNRPC,
127fbf76683SChuck Lever 		.procname	= "sunrpc",
128fbf76683SChuck Lever 		.mode		= 0555,
129fbf76683SChuck Lever 		.child		= xs_tunables_table
130fbf76683SChuck Lever 	},
131fbf76683SChuck Lever 	{
132fbf76683SChuck Lever 		.ctl_name = 0,
133fbf76683SChuck Lever 	},
134fbf76683SChuck Lever };
135fbf76683SChuck Lever 
136fbf76683SChuck Lever #endif
137fbf76683SChuck Lever 
138fbf76683SChuck Lever /*
13903bf4b70SChuck Lever  * Time out for an RPC UDP socket connect.  UDP socket connects are
14003bf4b70SChuck Lever  * synchronous, but we set a timeout anyway in case of resource
14103bf4b70SChuck Lever  * exhaustion on the local host.
14203bf4b70SChuck Lever  */
14303bf4b70SChuck Lever #define XS_UDP_CONN_TO		(5U * HZ)
14403bf4b70SChuck Lever 
14503bf4b70SChuck Lever /*
14603bf4b70SChuck Lever  * Wait duration for an RPC TCP connection to be established.  Solaris
14703bf4b70SChuck Lever  * NFS over TCP uses 60 seconds, for example, which is in line with how
14803bf4b70SChuck Lever  * long a server takes to reboot.
14903bf4b70SChuck Lever  */
15003bf4b70SChuck Lever #define XS_TCP_CONN_TO		(60U * HZ)
15103bf4b70SChuck Lever 
15203bf4b70SChuck Lever /*
15303bf4b70SChuck Lever  * Wait duration for a reply from the RPC portmapper.
15403bf4b70SChuck Lever  */
15503bf4b70SChuck Lever #define XS_BIND_TO		(60U * HZ)
15603bf4b70SChuck Lever 
15703bf4b70SChuck Lever /*
15803bf4b70SChuck Lever  * Delay if a UDP socket connect error occurs.  This is most likely some
15903bf4b70SChuck Lever  * kind of resource problem on the local host.
16003bf4b70SChuck Lever  */
16103bf4b70SChuck Lever #define XS_UDP_REEST_TO		(2U * HZ)
16203bf4b70SChuck Lever 
16303bf4b70SChuck Lever /*
16403bf4b70SChuck Lever  * The reestablish timeout allows clients to delay for a bit before attempting
16503bf4b70SChuck Lever  * to reconnect to a server that just dropped our connection.
16603bf4b70SChuck Lever  *
16703bf4b70SChuck Lever  * We implement an exponential backoff when trying to reestablish a TCP
16803bf4b70SChuck Lever  * transport connection with the server.  Some servers like to drop a TCP
16903bf4b70SChuck Lever  * connection when they are overworked, so we start with a short timeout and
17003bf4b70SChuck Lever  * increase over time if the server is down or not responding.
17103bf4b70SChuck Lever  */
17203bf4b70SChuck Lever #define XS_TCP_INIT_REEST_TO	(3U * HZ)
17303bf4b70SChuck Lever #define XS_TCP_MAX_REEST_TO	(5U * 60 * HZ)
17403bf4b70SChuck Lever 
17503bf4b70SChuck Lever /*
17603bf4b70SChuck Lever  * TCP idle timeout; client drops the transport socket if it is idle
17703bf4b70SChuck Lever  * for this long.  Note that we also timeout UDP sockets to prevent
17803bf4b70SChuck Lever  * holding port numbers when there is no RPC traffic.
17903bf4b70SChuck Lever  */
18003bf4b70SChuck Lever #define XS_IDLE_DISC_TO		(5U * 60 * HZ)
18103bf4b70SChuck Lever 
182a246b010SChuck Lever #ifdef RPC_DEBUG
183a246b010SChuck Lever # undef  RPC_DEBUG_DATA
1849903cd1cSChuck Lever # define RPCDBG_FACILITY	RPCDBG_TRANS
185a246b010SChuck Lever #endif
186a246b010SChuck Lever 
187a246b010SChuck Lever #ifdef RPC_DEBUG_DATA
1889903cd1cSChuck Lever static void xs_pktdump(char *msg, u32 *packet, unsigned int count)
189a246b010SChuck Lever {
190a246b010SChuck Lever 	u8 *buf = (u8 *) packet;
191a246b010SChuck Lever 	int j;
192a246b010SChuck Lever 
193a246b010SChuck Lever 	dprintk("RPC:       %s\n", msg);
194a246b010SChuck Lever 	for (j = 0; j < count && j < 128; j += 4) {
195a246b010SChuck Lever 		if (!(j & 31)) {
196a246b010SChuck Lever 			if (j)
197a246b010SChuck Lever 				dprintk("\n");
198a246b010SChuck Lever 			dprintk("0x%04x ", j);
199a246b010SChuck Lever 		}
200a246b010SChuck Lever 		dprintk("%02x%02x%02x%02x ",
201a246b010SChuck Lever 			buf[j], buf[j+1], buf[j+2], buf[j+3]);
202a246b010SChuck Lever 	}
203a246b010SChuck Lever 	dprintk("\n");
204a246b010SChuck Lever }
205a246b010SChuck Lever #else
2069903cd1cSChuck Lever static inline void xs_pktdump(char *msg, u32 *packet, unsigned int count)
207a246b010SChuck Lever {
208a246b010SChuck Lever 	/* NOP */
209a246b010SChuck Lever }
210a246b010SChuck Lever #endif
211a246b010SChuck Lever 
212ffc2e518SChuck Lever struct sock_xprt {
213ffc2e518SChuck Lever 	struct rpc_xprt		xprt;
214ee0ac0c2SChuck Lever 
215ee0ac0c2SChuck Lever 	/*
216ee0ac0c2SChuck Lever 	 * Network layer
217ee0ac0c2SChuck Lever 	 */
218ee0ac0c2SChuck Lever 	struct socket *		sock;
219ee0ac0c2SChuck Lever 	struct sock *		inet;
22051971139SChuck Lever 
22151971139SChuck Lever 	/*
22251971139SChuck Lever 	 * State of TCP reply receive
22351971139SChuck Lever 	 */
22451971139SChuck Lever 	__be32			tcp_fraghdr,
22551971139SChuck Lever 				tcp_xid;
22651971139SChuck Lever 
22751971139SChuck Lever 	u32			tcp_offset,
22851971139SChuck Lever 				tcp_reclen;
22951971139SChuck Lever 
23051971139SChuck Lever 	unsigned long		tcp_copied,
23151971139SChuck Lever 				tcp_flags;
232c8475461SChuck Lever 
233c8475461SChuck Lever 	/*
234c8475461SChuck Lever 	 * Connection of transports
235c8475461SChuck Lever 	 */
23634161db6STrond Myklebust 	struct delayed_work	connect_worker;
237d3bc9a1dSFrank van Maarseveen 	struct sockaddr_storage	addr;
238c8475461SChuck Lever 	unsigned short		port;
2397c6e066eSChuck Lever 
2407c6e066eSChuck Lever 	/*
2417c6e066eSChuck Lever 	 * UDP socket buffer size parameters
2427c6e066eSChuck Lever 	 */
2437c6e066eSChuck Lever 	size_t			rcvsize,
2447c6e066eSChuck Lever 				sndsize;
245314dfd79SChuck Lever 
246314dfd79SChuck Lever 	/*
247314dfd79SChuck Lever 	 * Saved socket callback addresses
248314dfd79SChuck Lever 	 */
249314dfd79SChuck Lever 	void			(*old_data_ready)(struct sock *, int);
250314dfd79SChuck Lever 	void			(*old_state_change)(struct sock *);
251314dfd79SChuck Lever 	void			(*old_write_space)(struct sock *);
2522a9e1cfaSTrond Myklebust 	void			(*old_error_report)(struct sock *);
253ffc2e518SChuck Lever };
254ffc2e518SChuck Lever 
255e136d092SChuck Lever /*
256e136d092SChuck Lever  * TCP receive state flags
257e136d092SChuck Lever  */
258e136d092SChuck Lever #define TCP_RCV_LAST_FRAG	(1UL << 0)
259e136d092SChuck Lever #define TCP_RCV_COPY_FRAGHDR	(1UL << 1)
260e136d092SChuck Lever #define TCP_RCV_COPY_XID	(1UL << 2)
261e136d092SChuck Lever #define TCP_RCV_COPY_DATA	(1UL << 3)
262e136d092SChuck Lever 
26395392c59SChuck Lever static inline struct sockaddr *xs_addr(struct rpc_xprt *xprt)
264edb267a6SChuck Lever {
26595392c59SChuck Lever 	return (struct sockaddr *) &xprt->addr;
26695392c59SChuck Lever }
26795392c59SChuck Lever 
26895392c59SChuck Lever static inline struct sockaddr_in *xs_addr_in(struct rpc_xprt *xprt)
26995392c59SChuck Lever {
27095392c59SChuck Lever 	return (struct sockaddr_in *) &xprt->addr;
27195392c59SChuck Lever }
27295392c59SChuck Lever 
27395392c59SChuck Lever static inline struct sockaddr_in6 *xs_addr_in6(struct rpc_xprt *xprt)
27495392c59SChuck Lever {
27595392c59SChuck Lever 	return (struct sockaddr_in6 *) &xprt->addr;
27695392c59SChuck Lever }
27795392c59SChuck Lever 
278b454ae90SChuck Lever static void xs_format_ipv4_peer_addresses(struct rpc_xprt *xprt,
279b454ae90SChuck Lever 					  const char *protocol,
280b454ae90SChuck Lever 					  const char *netid)
281edb267a6SChuck Lever {
28295392c59SChuck Lever 	struct sockaddr_in *addr = xs_addr_in(xprt);
283edb267a6SChuck Lever 	char *buf;
284edb267a6SChuck Lever 
285edb267a6SChuck Lever 	buf = kzalloc(20, GFP_KERNEL);
286edb267a6SChuck Lever 	if (buf) {
287e0db4a78SDavid S. Miller 		snprintf(buf, 20, "%pI4", &addr->sin_addr.s_addr);
288edb267a6SChuck Lever 	}
289edb267a6SChuck Lever 	xprt->address_strings[RPC_DISPLAY_ADDR] = buf;
290edb267a6SChuck Lever 
291edb267a6SChuck Lever 	buf = kzalloc(8, GFP_KERNEL);
292edb267a6SChuck Lever 	if (buf) {
293edb267a6SChuck Lever 		snprintf(buf, 8, "%u",
294edb267a6SChuck Lever 				ntohs(addr->sin_port));
295edb267a6SChuck Lever 	}
296edb267a6SChuck Lever 	xprt->address_strings[RPC_DISPLAY_PORT] = buf;
297edb267a6SChuck Lever 
298b454ae90SChuck Lever 	xprt->address_strings[RPC_DISPLAY_PROTO] = protocol;
299edb267a6SChuck Lever 
300edb267a6SChuck Lever 	buf = kzalloc(48, GFP_KERNEL);
301edb267a6SChuck Lever 	if (buf) {
30221454aaaSHarvey Harrison 		snprintf(buf, 48, "addr=%pI4 port=%u proto=%s",
30321454aaaSHarvey Harrison 			&addr->sin_addr.s_addr,
304edb267a6SChuck Lever 			ntohs(addr->sin_port),
305b454ae90SChuck Lever 			protocol);
306edb267a6SChuck Lever 	}
307edb267a6SChuck Lever 	xprt->address_strings[RPC_DISPLAY_ALL] = buf;
308fbfe3cc6SChuck Lever 
309fbfe3cc6SChuck Lever 	buf = kzalloc(10, GFP_KERNEL);
310fbfe3cc6SChuck Lever 	if (buf) {
311fbfe3cc6SChuck Lever 		snprintf(buf, 10, "%02x%02x%02x%02x",
312fbfe3cc6SChuck Lever 				NIPQUAD(addr->sin_addr.s_addr));
313fbfe3cc6SChuck Lever 	}
314fbfe3cc6SChuck Lever 	xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = buf;
315fbfe3cc6SChuck Lever 
316fbfe3cc6SChuck Lever 	buf = kzalloc(8, GFP_KERNEL);
317fbfe3cc6SChuck Lever 	if (buf) {
318fbfe3cc6SChuck Lever 		snprintf(buf, 8, "%4hx",
319fbfe3cc6SChuck Lever 				ntohs(addr->sin_port));
320fbfe3cc6SChuck Lever 	}
321fbfe3cc6SChuck Lever 	xprt->address_strings[RPC_DISPLAY_HEX_PORT] = buf;
322756805e7SChuck Lever 
323756805e7SChuck Lever 	buf = kzalloc(30, GFP_KERNEL);
324756805e7SChuck Lever 	if (buf) {
32521454aaaSHarvey Harrison 		snprintf(buf, 30, "%pI4.%u.%u",
32621454aaaSHarvey Harrison 				&addr->sin_addr.s_addr,
327756805e7SChuck Lever 				ntohs(addr->sin_port) >> 8,
328756805e7SChuck Lever 				ntohs(addr->sin_port) & 0xff);
329756805e7SChuck Lever 	}
330756805e7SChuck Lever 	xprt->address_strings[RPC_DISPLAY_UNIVERSAL_ADDR] = buf;
3314417c8c4S\"Talpey, Thomas\ 
332b454ae90SChuck Lever 	xprt->address_strings[RPC_DISPLAY_NETID] = netid;
333edb267a6SChuck Lever }
334edb267a6SChuck Lever 
335b454ae90SChuck Lever static void xs_format_ipv6_peer_addresses(struct rpc_xprt *xprt,
336b454ae90SChuck Lever 					  const char *protocol,
337b454ae90SChuck Lever 					  const char *netid)
3384b6473fbSChuck Lever {
33995392c59SChuck Lever 	struct sockaddr_in6 *addr = xs_addr_in6(xprt);
3404b6473fbSChuck Lever 	char *buf;
3414b6473fbSChuck Lever 
3424b6473fbSChuck Lever 	buf = kzalloc(40, GFP_KERNEL);
3434b6473fbSChuck Lever 	if (buf) {
3445b095d98SHarvey Harrison 		snprintf(buf, 40, "%pI6",&addr->sin6_addr);
3454b6473fbSChuck Lever 	}
3464b6473fbSChuck Lever 	xprt->address_strings[RPC_DISPLAY_ADDR] = buf;
3474b6473fbSChuck Lever 
3484b6473fbSChuck Lever 	buf = kzalloc(8, GFP_KERNEL);
3494b6473fbSChuck Lever 	if (buf) {
3504b6473fbSChuck Lever 		snprintf(buf, 8, "%u",
3514b6473fbSChuck Lever 				ntohs(addr->sin6_port));
3524b6473fbSChuck Lever 	}
3534b6473fbSChuck Lever 	xprt->address_strings[RPC_DISPLAY_PORT] = buf;
3544b6473fbSChuck Lever 
355b454ae90SChuck Lever 	xprt->address_strings[RPC_DISPLAY_PROTO] = protocol;
3564b6473fbSChuck Lever 
3574b6473fbSChuck Lever 	buf = kzalloc(64, GFP_KERNEL);
3584b6473fbSChuck Lever 	if (buf) {
3595b095d98SHarvey Harrison 		snprintf(buf, 64, "addr=%pI6 port=%u proto=%s",
360fdb46ee7SHarvey Harrison 				&addr->sin6_addr,
3614b6473fbSChuck Lever 				ntohs(addr->sin6_port),
362b454ae90SChuck Lever 				protocol);
3634b6473fbSChuck Lever 	}
3644b6473fbSChuck Lever 	xprt->address_strings[RPC_DISPLAY_ALL] = buf;
3654b6473fbSChuck Lever 
3664b6473fbSChuck Lever 	buf = kzalloc(36, GFP_KERNEL);
367b071195dSHarvey Harrison 	if (buf)
3684b7a4274SHarvey Harrison 		snprintf(buf, 36, "%pi6", &addr->sin6_addr);
369b071195dSHarvey Harrison 
3704b6473fbSChuck Lever 	xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = buf;
3714b6473fbSChuck Lever 
3724b6473fbSChuck Lever 	buf = kzalloc(8, GFP_KERNEL);
3734b6473fbSChuck Lever 	if (buf) {
3744b6473fbSChuck Lever 		snprintf(buf, 8, "%4hx",
3754b6473fbSChuck Lever 				ntohs(addr->sin6_port));
3764b6473fbSChuck Lever 	}
3774b6473fbSChuck Lever 	xprt->address_strings[RPC_DISPLAY_HEX_PORT] = buf;
378756805e7SChuck Lever 
379756805e7SChuck Lever 	buf = kzalloc(50, GFP_KERNEL);
380756805e7SChuck Lever 	if (buf) {
3815b095d98SHarvey Harrison 		snprintf(buf, 50, "%pI6.%u.%u",
382fdb46ee7SHarvey Harrison 			 &addr->sin6_addr,
383756805e7SChuck Lever 			 ntohs(addr->sin6_port) >> 8,
384756805e7SChuck Lever 			 ntohs(addr->sin6_port) & 0xff);
385756805e7SChuck Lever 	}
386756805e7SChuck Lever 	xprt->address_strings[RPC_DISPLAY_UNIVERSAL_ADDR] = buf;
3874417c8c4S\"Talpey, Thomas\ 
388b454ae90SChuck Lever 	xprt->address_strings[RPC_DISPLAY_NETID] = netid;
389edb267a6SChuck Lever }
390edb267a6SChuck Lever 
391edb267a6SChuck Lever static void xs_free_peer_addresses(struct rpc_xprt *xprt)
392edb267a6SChuck Lever {
39333e01dc7SChuck Lever 	unsigned int i;
39433e01dc7SChuck Lever 
39533e01dc7SChuck Lever 	for (i = 0; i < RPC_DISPLAY_MAX; i++)
39633e01dc7SChuck Lever 		switch (i) {
39733e01dc7SChuck Lever 		case RPC_DISPLAY_PROTO:
39833e01dc7SChuck Lever 		case RPC_DISPLAY_NETID:
39933e01dc7SChuck Lever 			continue;
40033e01dc7SChuck Lever 		default:
40133e01dc7SChuck Lever 			kfree(xprt->address_strings[i]);
40233e01dc7SChuck Lever 		}
403edb267a6SChuck Lever }
404edb267a6SChuck Lever 
405b4b5cc85SChuck Lever #define XS_SENDMSG_FLAGS	(MSG_DONTWAIT | MSG_NOSIGNAL)
406b4b5cc85SChuck Lever 
40724c5684bSTrond Myklebust static int xs_send_kvec(struct socket *sock, struct sockaddr *addr, int addrlen, struct kvec *vec, unsigned int base, int more)
408b4b5cc85SChuck Lever {
409b4b5cc85SChuck Lever 	struct msghdr msg = {
410b4b5cc85SChuck Lever 		.msg_name	= addr,
411b4b5cc85SChuck Lever 		.msg_namelen	= addrlen,
41224c5684bSTrond Myklebust 		.msg_flags	= XS_SENDMSG_FLAGS | (more ? MSG_MORE : 0),
41324c5684bSTrond Myklebust 	};
41424c5684bSTrond Myklebust 	struct kvec iov = {
41524c5684bSTrond Myklebust 		.iov_base	= vec->iov_base + base,
41624c5684bSTrond Myklebust 		.iov_len	= vec->iov_len - base,
417b4b5cc85SChuck Lever 	};
418b4b5cc85SChuck Lever 
41924c5684bSTrond Myklebust 	if (iov.iov_len != 0)
420b4b5cc85SChuck Lever 		return kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len);
421b4b5cc85SChuck Lever 	return kernel_sendmsg(sock, &msg, NULL, 0, 0);
422b4b5cc85SChuck Lever }
423b4b5cc85SChuck Lever 
42424c5684bSTrond Myklebust static int xs_send_pagedata(struct socket *sock, struct xdr_buf *xdr, unsigned int base, int more)
425b4b5cc85SChuck Lever {
42624c5684bSTrond Myklebust 	struct page **ppage;
42724c5684bSTrond Myklebust 	unsigned int remainder;
42824c5684bSTrond Myklebust 	int err, sent = 0;
429b4b5cc85SChuck Lever 
43024c5684bSTrond Myklebust 	remainder = xdr->page_len - base;
43124c5684bSTrond Myklebust 	base += xdr->page_base;
43224c5684bSTrond Myklebust 	ppage = xdr->pages + (base >> PAGE_SHIFT);
43324c5684bSTrond Myklebust 	base &= ~PAGE_MASK;
43424c5684bSTrond Myklebust 	for(;;) {
43524c5684bSTrond Myklebust 		unsigned int len = min_t(unsigned int, PAGE_SIZE - base, remainder);
43624c5684bSTrond Myklebust 		int flags = XS_SENDMSG_FLAGS;
43724c5684bSTrond Myklebust 
43824c5684bSTrond Myklebust 		remainder -= len;
43924c5684bSTrond Myklebust 		if (remainder != 0 || more)
44024c5684bSTrond Myklebust 			flags |= MSG_MORE;
44124c5684bSTrond Myklebust 		err = sock->ops->sendpage(sock, *ppage, base, len, flags);
44224c5684bSTrond Myklebust 		if (remainder == 0 || err != len)
44324c5684bSTrond Myklebust 			break;
44424c5684bSTrond Myklebust 		sent += err;
44524c5684bSTrond Myklebust 		ppage++;
44624c5684bSTrond Myklebust 		base = 0;
44724c5684bSTrond Myklebust 	}
44824c5684bSTrond Myklebust 	if (sent == 0)
44924c5684bSTrond Myklebust 		return err;
45024c5684bSTrond Myklebust 	if (err > 0)
45124c5684bSTrond Myklebust 		sent += err;
45224c5684bSTrond Myklebust 	return sent;
453b4b5cc85SChuck Lever }
454b4b5cc85SChuck Lever 
4559903cd1cSChuck Lever /**
4569903cd1cSChuck Lever  * xs_sendpages - write pages directly to a socket
4579903cd1cSChuck Lever  * @sock: socket to send on
4589903cd1cSChuck Lever  * @addr: UDP only -- address of destination
4599903cd1cSChuck Lever  * @addrlen: UDP only -- length of destination address
4609903cd1cSChuck Lever  * @xdr: buffer containing this request
4619903cd1cSChuck Lever  * @base: starting position in the buffer
4629903cd1cSChuck Lever  *
463a246b010SChuck Lever  */
46424c5684bSTrond Myklebust static int xs_sendpages(struct socket *sock, struct sockaddr *addr, int addrlen, struct xdr_buf *xdr, unsigned int base)
465a246b010SChuck Lever {
46624c5684bSTrond Myklebust 	unsigned int remainder = xdr->len - base;
46724c5684bSTrond Myklebust 	int err, sent = 0;
468a246b010SChuck Lever 
469262965f5SChuck Lever 	if (unlikely(!sock))
470*fba91afbSTrond Myklebust 		return -ENOTSOCK;
471262965f5SChuck Lever 
472262965f5SChuck Lever 	clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags);
47324c5684bSTrond Myklebust 	if (base != 0) {
47424c5684bSTrond Myklebust 		addr = NULL;
47524c5684bSTrond Myklebust 		addrlen = 0;
47624c5684bSTrond Myklebust 	}
477262965f5SChuck Lever 
47824c5684bSTrond Myklebust 	if (base < xdr->head[0].iov_len || addr != NULL) {
47924c5684bSTrond Myklebust 		unsigned int len = xdr->head[0].iov_len - base;
48024c5684bSTrond Myklebust 		remainder -= len;
48124c5684bSTrond Myklebust 		err = xs_send_kvec(sock, addr, addrlen, &xdr->head[0], base, remainder != 0);
48224c5684bSTrond Myklebust 		if (remainder == 0 || err != len)
483a246b010SChuck Lever 			goto out;
48424c5684bSTrond Myklebust 		sent += err;
485a246b010SChuck Lever 		base = 0;
486a246b010SChuck Lever 	} else
48724c5684bSTrond Myklebust 		base -= xdr->head[0].iov_len;
488a246b010SChuck Lever 
48924c5684bSTrond Myklebust 	if (base < xdr->page_len) {
49024c5684bSTrond Myklebust 		unsigned int len = xdr->page_len - base;
49124c5684bSTrond Myklebust 		remainder -= len;
49224c5684bSTrond Myklebust 		err = xs_send_pagedata(sock, xdr, base, remainder != 0);
49324c5684bSTrond Myklebust 		if (remainder == 0 || err != len)
494a246b010SChuck Lever 			goto out;
49524c5684bSTrond Myklebust 		sent += err;
496a246b010SChuck Lever 		base = 0;
49724c5684bSTrond Myklebust 	} else
49824c5684bSTrond Myklebust 		base -= xdr->page_len;
49924c5684bSTrond Myklebust 
50024c5684bSTrond Myklebust 	if (base >= xdr->tail[0].iov_len)
50124c5684bSTrond Myklebust 		return sent;
50224c5684bSTrond Myklebust 	err = xs_send_kvec(sock, NULL, 0, &xdr->tail[0], base, 0);
503a246b010SChuck Lever out:
50424c5684bSTrond Myklebust 	if (sent == 0)
50524c5684bSTrond Myklebust 		return err;
50624c5684bSTrond Myklebust 	if (err > 0)
50724c5684bSTrond Myklebust 		sent += err;
50824c5684bSTrond Myklebust 	return sent;
509a246b010SChuck Lever }
510a246b010SChuck Lever 
511b6ddf64fSTrond Myklebust static void xs_nospace_callback(struct rpc_task *task)
512b6ddf64fSTrond Myklebust {
513b6ddf64fSTrond Myklebust 	struct sock_xprt *transport = container_of(task->tk_rqstp->rq_xprt, struct sock_xprt, xprt);
514b6ddf64fSTrond Myklebust 
515b6ddf64fSTrond Myklebust 	transport->inet->sk_write_pending--;
516b6ddf64fSTrond Myklebust 	clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
517b6ddf64fSTrond Myklebust }
518b6ddf64fSTrond Myklebust 
5199903cd1cSChuck Lever /**
520262965f5SChuck Lever  * xs_nospace - place task on wait queue if transmit was incomplete
521262965f5SChuck Lever  * @task: task to put to sleep
5229903cd1cSChuck Lever  *
523a246b010SChuck Lever  */
524262965f5SChuck Lever static void xs_nospace(struct rpc_task *task)
525a246b010SChuck Lever {
526262965f5SChuck Lever 	struct rpc_rqst *req = task->tk_rqstp;
527262965f5SChuck Lever 	struct rpc_xprt *xprt = req->rq_xprt;
528ee0ac0c2SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
529a246b010SChuck Lever 
53046121cf7SChuck Lever 	dprintk("RPC: %5u xmit incomplete (%u left of %u)\n",
531262965f5SChuck Lever 			task->tk_pid, req->rq_slen - req->rq_bytes_sent,
532262965f5SChuck Lever 			req->rq_slen);
533a246b010SChuck Lever 
534262965f5SChuck Lever 	/* Protect against races with write_space */
535262965f5SChuck Lever 	spin_lock_bh(&xprt->transport_lock);
536a246b010SChuck Lever 
537262965f5SChuck Lever 	/* Don't race with disconnect */
538b6ddf64fSTrond Myklebust 	if (xprt_connected(xprt)) {
539b6ddf64fSTrond Myklebust 		if (test_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags)) {
540b6ddf64fSTrond Myklebust 			/*
541b6ddf64fSTrond Myklebust 			 * Notify TCP that we're limited by the application
542b6ddf64fSTrond Myklebust 			 * window size
543b6ddf64fSTrond Myklebust 			 */
544b6ddf64fSTrond Myklebust 			set_bit(SOCK_NOSPACE, &transport->sock->flags);
545b6ddf64fSTrond Myklebust 			transport->inet->sk_write_pending++;
546b6ddf64fSTrond Myklebust 			/* ...and wait for more buffer space */
547b6ddf64fSTrond Myklebust 			xprt_wait_for_buffer_space(task, xs_nospace_callback);
548b6ddf64fSTrond Myklebust 		}
549b6ddf64fSTrond Myklebust 	} else {
550b6ddf64fSTrond Myklebust 		clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
551262965f5SChuck Lever 		task->tk_status = -ENOTCONN;
552b6ddf64fSTrond Myklebust 	}
553a246b010SChuck Lever 
554262965f5SChuck Lever 	spin_unlock_bh(&xprt->transport_lock);
555a246b010SChuck Lever }
556a246b010SChuck Lever 
5579903cd1cSChuck Lever /**
558262965f5SChuck Lever  * xs_udp_send_request - write an RPC request to a UDP socket
5599903cd1cSChuck Lever  * @task: address of RPC task that manages the state of an RPC request
5609903cd1cSChuck Lever  *
5619903cd1cSChuck Lever  * Return values:
5629903cd1cSChuck Lever  *        0:	The request has been sent
5639903cd1cSChuck Lever  *   EAGAIN:	The socket was blocked, please call again later to
5649903cd1cSChuck Lever  *		complete the request
565262965f5SChuck Lever  * ENOTCONN:	Caller needs to invoke connect logic then call again
5669903cd1cSChuck Lever  *    other:	Some other error occured, the request was not sent
5679903cd1cSChuck Lever  */
568262965f5SChuck Lever static int xs_udp_send_request(struct rpc_task *task)
569a246b010SChuck Lever {
570a246b010SChuck Lever 	struct rpc_rqst *req = task->tk_rqstp;
571a246b010SChuck Lever 	struct rpc_xprt *xprt = req->rq_xprt;
572ee0ac0c2SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
573262965f5SChuck Lever 	struct xdr_buf *xdr = &req->rq_snd_buf;
574262965f5SChuck Lever 	int status;
575262965f5SChuck Lever 
576262965f5SChuck Lever 	xs_pktdump("packet data:",
577262965f5SChuck Lever 				req->rq_svec->iov_base,
578262965f5SChuck Lever 				req->rq_svec->iov_len);
579262965f5SChuck Lever 
580ee0ac0c2SChuck Lever 	status = xs_sendpages(transport->sock,
58195392c59SChuck Lever 			      xs_addr(xprt),
582ee0ac0c2SChuck Lever 			      xprt->addrlen, xdr,
583ee0ac0c2SChuck Lever 			      req->rq_bytes_sent);
584262965f5SChuck Lever 
585262965f5SChuck Lever 	dprintk("RPC:       xs_udp_send_request(%u) = %d\n",
586262965f5SChuck Lever 			xdr->len - req->rq_bytes_sent, status);
587262965f5SChuck Lever 
5882199700fSTrond Myklebust 	if (status >= 0) {
5891321d8d9SChuck Lever 		task->tk_bytes_sent += status;
5902199700fSTrond Myklebust 		if (status >= req->rq_slen)
591262965f5SChuck Lever 			return 0;
592262965f5SChuck Lever 		/* Still some bytes left; set up for a retry later. */
593262965f5SChuck Lever 		status = -EAGAIN;
5942199700fSTrond Myklebust 	}
595262965f5SChuck Lever 
596262965f5SChuck Lever 	switch (status) {
597*fba91afbSTrond Myklebust 	case -ENOTSOCK:
598*fba91afbSTrond Myklebust 		status = -ENOTCONN;
599*fba91afbSTrond Myklebust 		/* Should we call xs_close() here? */
600*fba91afbSTrond Myklebust 		break;
601b6ddf64fSTrond Myklebust 	case -EAGAIN:
602b6ddf64fSTrond Myklebust 		xs_nospace(task);
603b6ddf64fSTrond Myklebust 		break;
604262965f5SChuck Lever 	case -ENETUNREACH:
605262965f5SChuck Lever 	case -EPIPE:
606262965f5SChuck Lever 	case -ECONNREFUSED:
607262965f5SChuck Lever 		/* When the server has died, an ICMP port unreachable message
608262965f5SChuck Lever 		 * prompts ECONNREFUSED. */
609b6ddf64fSTrond Myklebust 		clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
610262965f5SChuck Lever 		break;
611262965f5SChuck Lever 	default:
612b6ddf64fSTrond Myklebust 		clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
613262965f5SChuck Lever 		dprintk("RPC:       sendmsg returned unrecognized error %d\n",
614262965f5SChuck Lever 			-status);
615262965f5SChuck Lever 	}
616262965f5SChuck Lever 
617262965f5SChuck Lever 	return status;
618262965f5SChuck Lever }
619262965f5SChuck Lever 
620e06799f9STrond Myklebust /**
621e06799f9STrond Myklebust  * xs_tcp_shutdown - gracefully shut down a TCP socket
622e06799f9STrond Myklebust  * @xprt: transport
623e06799f9STrond Myklebust  *
624e06799f9STrond Myklebust  * Initiates a graceful shutdown of the TCP socket by calling the
625e06799f9STrond Myklebust  * equivalent of shutdown(SHUT_WR);
626e06799f9STrond Myklebust  */
627e06799f9STrond Myklebust static void xs_tcp_shutdown(struct rpc_xprt *xprt)
628e06799f9STrond Myklebust {
629e06799f9STrond Myklebust 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
630e06799f9STrond Myklebust 	struct socket *sock = transport->sock;
631e06799f9STrond Myklebust 
632e06799f9STrond Myklebust 	if (sock != NULL)
633e06799f9STrond Myklebust 		kernel_sock_shutdown(sock, SHUT_WR);
634e06799f9STrond Myklebust }
635e06799f9STrond Myklebust 
636808012fbSChuck Lever static inline void xs_encode_tcp_record_marker(struct xdr_buf *buf)
637808012fbSChuck Lever {
638808012fbSChuck Lever 	u32 reclen = buf->len - sizeof(rpc_fraghdr);
639808012fbSChuck Lever 	rpc_fraghdr *base = buf->head[0].iov_base;
640808012fbSChuck Lever 	*base = htonl(RPC_LAST_STREAM_FRAGMENT | reclen);
641808012fbSChuck Lever }
642808012fbSChuck Lever 
643262965f5SChuck Lever /**
644262965f5SChuck Lever  * xs_tcp_send_request - write an RPC request to a TCP socket
645262965f5SChuck Lever  * @task: address of RPC task that manages the state of an RPC request
646262965f5SChuck Lever  *
647262965f5SChuck Lever  * Return values:
648262965f5SChuck Lever  *        0:	The request has been sent
649262965f5SChuck Lever  *   EAGAIN:	The socket was blocked, please call again later to
650262965f5SChuck Lever  *		complete the request
651262965f5SChuck Lever  * ENOTCONN:	Caller needs to invoke connect logic then call again
652262965f5SChuck Lever  *    other:	Some other error occured, the request was not sent
653262965f5SChuck Lever  *
654262965f5SChuck Lever  * XXX: In the case of soft timeouts, should we eventually give up
655262965f5SChuck Lever  *	if sendmsg is not able to make progress?
656262965f5SChuck Lever  */
657262965f5SChuck Lever static int xs_tcp_send_request(struct rpc_task *task)
658262965f5SChuck Lever {
659262965f5SChuck Lever 	struct rpc_rqst *req = task->tk_rqstp;
660262965f5SChuck Lever 	struct rpc_xprt *xprt = req->rq_xprt;
661ee0ac0c2SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
662262965f5SChuck Lever 	struct xdr_buf *xdr = &req->rq_snd_buf;
663b595bb15SChuck Lever 	int status;
664a246b010SChuck Lever 
665808012fbSChuck Lever 	xs_encode_tcp_record_marker(&req->rq_snd_buf);
666262965f5SChuck Lever 
667262965f5SChuck Lever 	xs_pktdump("packet data:",
668262965f5SChuck Lever 				req->rq_svec->iov_base,
669262965f5SChuck Lever 				req->rq_svec->iov_len);
670a246b010SChuck Lever 
671a246b010SChuck Lever 	/* Continue transmitting the packet/record. We must be careful
672a246b010SChuck Lever 	 * to cope with writespace callbacks arriving _after_ we have
673262965f5SChuck Lever 	 * called sendmsg(). */
674a246b010SChuck Lever 	while (1) {
675ee0ac0c2SChuck Lever 		status = xs_sendpages(transport->sock,
676ee0ac0c2SChuck Lever 					NULL, 0, xdr, req->rq_bytes_sent);
677a246b010SChuck Lever 
678262965f5SChuck Lever 		dprintk("RPC:       xs_tcp_send_request(%u) = %d\n",
679262965f5SChuck Lever 				xdr->len - req->rq_bytes_sent, status);
680262965f5SChuck Lever 
681262965f5SChuck Lever 		if (unlikely(status < 0))
682a246b010SChuck Lever 			break;
683a246b010SChuck Lever 
684a246b010SChuck Lever 		/* If we've sent the entire packet, immediately
685a246b010SChuck Lever 		 * reset the count of bytes sent. */
686262965f5SChuck Lever 		req->rq_bytes_sent += status;
687ef759a2eSChuck Lever 		task->tk_bytes_sent += status;
688262965f5SChuck Lever 		if (likely(req->rq_bytes_sent >= req->rq_slen)) {
689a246b010SChuck Lever 			req->rq_bytes_sent = 0;
690a246b010SChuck Lever 			return 0;
691a246b010SChuck Lever 		}
692262965f5SChuck Lever 
69306b4b681STrond Myklebust 		if (status != 0)
69406b4b681STrond Myklebust 			continue;
695a246b010SChuck Lever 		status = -EAGAIN;
696a246b010SChuck Lever 		break;
697a246b010SChuck Lever 	}
698a246b010SChuck Lever 
699262965f5SChuck Lever 	switch (status) {
700*fba91afbSTrond Myklebust 	case -ENOTSOCK:
701*fba91afbSTrond Myklebust 		status = -ENOTCONN;
702*fba91afbSTrond Myklebust 		/* Should we call xs_close() here? */
703*fba91afbSTrond Myklebust 		break;
704262965f5SChuck Lever 	case -EAGAIN:
705262965f5SChuck Lever 		xs_nospace(task);
706262965f5SChuck Lever 		break;
707262965f5SChuck Lever 	case -ECONNRESET:
7082a9e1cfaSTrond Myklebust 		xs_tcp_shutdown(xprt);
7092a9e1cfaSTrond Myklebust 	case -ECONNREFUSED:
710262965f5SChuck Lever 	case -ENOTCONN:
711262965f5SChuck Lever 	case -EPIPE:
712262965f5SChuck Lever 		status = -ENOTCONN;
713b6ddf64fSTrond Myklebust 		clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
714262965f5SChuck Lever 		break;
715262965f5SChuck Lever 	default:
716262965f5SChuck Lever 		dprintk("RPC:       sendmsg returned unrecognized error %d\n",
717262965f5SChuck Lever 			-status);
718b6ddf64fSTrond Myklebust 		clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
719e06799f9STrond Myklebust 		xs_tcp_shutdown(xprt);
720a246b010SChuck Lever 	}
721a246b010SChuck Lever 
722a246b010SChuck Lever 	return status;
723a246b010SChuck Lever }
724a246b010SChuck Lever 
7259903cd1cSChuck Lever /**
726e0ab53deSTrond Myklebust  * xs_tcp_release_xprt - clean up after a tcp transmission
727e0ab53deSTrond Myklebust  * @xprt: transport
728e0ab53deSTrond Myklebust  * @task: rpc task
729e0ab53deSTrond Myklebust  *
730e0ab53deSTrond Myklebust  * This cleans up if an error causes us to abort the transmission of a request.
731e0ab53deSTrond Myklebust  * In this case, the socket may need to be reset in order to avoid confusing
732e0ab53deSTrond Myklebust  * the server.
733e0ab53deSTrond Myklebust  */
734e0ab53deSTrond Myklebust static void xs_tcp_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
735e0ab53deSTrond Myklebust {
736e0ab53deSTrond Myklebust 	struct rpc_rqst *req;
737e0ab53deSTrond Myklebust 
738e0ab53deSTrond Myklebust 	if (task != xprt->snd_task)
739e0ab53deSTrond Myklebust 		return;
740e0ab53deSTrond Myklebust 	if (task == NULL)
741e0ab53deSTrond Myklebust 		goto out_release;
742e0ab53deSTrond Myklebust 	req = task->tk_rqstp;
743e0ab53deSTrond Myklebust 	if (req->rq_bytes_sent == 0)
744e0ab53deSTrond Myklebust 		goto out_release;
745e0ab53deSTrond Myklebust 	if (req->rq_bytes_sent == req->rq_snd_buf.len)
746e0ab53deSTrond Myklebust 		goto out_release;
747e0ab53deSTrond Myklebust 	set_bit(XPRT_CLOSE_WAIT, &task->tk_xprt->state);
748e0ab53deSTrond Myklebust out_release:
749e0ab53deSTrond Myklebust 	xprt_release_xprt(xprt, task);
750e0ab53deSTrond Myklebust }
751e0ab53deSTrond Myklebust 
7522a9e1cfaSTrond Myklebust static void xs_save_old_callbacks(struct sock_xprt *transport, struct sock *sk)
7532a9e1cfaSTrond Myklebust {
7542a9e1cfaSTrond Myklebust 	transport->old_data_ready = sk->sk_data_ready;
7552a9e1cfaSTrond Myklebust 	transport->old_state_change = sk->sk_state_change;
7562a9e1cfaSTrond Myklebust 	transport->old_write_space = sk->sk_write_space;
7572a9e1cfaSTrond Myklebust 	transport->old_error_report = sk->sk_error_report;
7582a9e1cfaSTrond Myklebust }
7592a9e1cfaSTrond Myklebust 
7602a9e1cfaSTrond Myklebust static void xs_restore_old_callbacks(struct sock_xprt *transport, struct sock *sk)
7612a9e1cfaSTrond Myklebust {
7622a9e1cfaSTrond Myklebust 	sk->sk_data_ready = transport->old_data_ready;
7632a9e1cfaSTrond Myklebust 	sk->sk_state_change = transport->old_state_change;
7642a9e1cfaSTrond Myklebust 	sk->sk_write_space = transport->old_write_space;
7652a9e1cfaSTrond Myklebust 	sk->sk_error_report = transport->old_error_report;
7662a9e1cfaSTrond Myklebust }
7672a9e1cfaSTrond Myklebust 
768e0ab53deSTrond Myklebust /**
7699903cd1cSChuck Lever  * xs_close - close a socket
7709903cd1cSChuck Lever  * @xprt: transport
7719903cd1cSChuck Lever  *
7723167e12cSChuck Lever  * This is used when all requests are complete; ie, no DRC state remains
7733167e12cSChuck Lever  * on the server we want to save.
774a246b010SChuck Lever  */
7759903cd1cSChuck Lever static void xs_close(struct rpc_xprt *xprt)
776a246b010SChuck Lever {
777ee0ac0c2SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
778ee0ac0c2SChuck Lever 	struct socket *sock = transport->sock;
779ee0ac0c2SChuck Lever 	struct sock *sk = transport->inet;
780a246b010SChuck Lever 
781a246b010SChuck Lever 	if (!sk)
782632e3bdcSTrond Myklebust 		goto clear_close_wait;
783a246b010SChuck Lever 
7849903cd1cSChuck Lever 	dprintk("RPC:       xs_close xprt %p\n", xprt);
7859903cd1cSChuck Lever 
786a246b010SChuck Lever 	write_lock_bh(&sk->sk_callback_lock);
787ee0ac0c2SChuck Lever 	transport->inet = NULL;
788ee0ac0c2SChuck Lever 	transport->sock = NULL;
789a246b010SChuck Lever 
790a246b010SChuck Lever 	sk->sk_user_data = NULL;
7912a9e1cfaSTrond Myklebust 
7922a9e1cfaSTrond Myklebust 	xs_restore_old_callbacks(transport, sk);
793a246b010SChuck Lever 	write_unlock_bh(&sk->sk_callback_lock);
794a246b010SChuck Lever 
795a246b010SChuck Lever 	sk->sk_no_check = 0;
796a246b010SChuck Lever 
797a246b010SChuck Lever 	sock_release(sock);
798632e3bdcSTrond Myklebust clear_close_wait:
799632e3bdcSTrond Myklebust 	smp_mb__before_clear_bit();
800632e3bdcSTrond Myklebust 	clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
8013b948ae5STrond Myklebust 	clear_bit(XPRT_CLOSING, &xprt->state);
802632e3bdcSTrond Myklebust 	smp_mb__after_clear_bit();
80362da3b24STrond Myklebust 	xprt_disconnect_done(xprt);
804a246b010SChuck Lever }
805a246b010SChuck Lever 
8069903cd1cSChuck Lever /**
8079903cd1cSChuck Lever  * xs_destroy - prepare to shutdown a transport
8089903cd1cSChuck Lever  * @xprt: doomed transport
8099903cd1cSChuck Lever  *
8109903cd1cSChuck Lever  */
8119903cd1cSChuck Lever static void xs_destroy(struct rpc_xprt *xprt)
812a246b010SChuck Lever {
813c8475461SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
814c8475461SChuck Lever 
8159903cd1cSChuck Lever 	dprintk("RPC:       xs_destroy xprt %p\n", xprt);
8169903cd1cSChuck Lever 
817c1384c9cSTrond Myklebust 	cancel_rearming_delayed_work(&transport->connect_worker);
818a246b010SChuck Lever 
8199903cd1cSChuck Lever 	xs_close(xprt);
820edb267a6SChuck Lever 	xs_free_peer_addresses(xprt);
821a246b010SChuck Lever 	kfree(xprt->slot);
822c8541ecdSChuck Lever 	kfree(xprt);
823bc25571eS\"Talpey, Thomas\ 	module_put(THIS_MODULE);
824a246b010SChuck Lever }
825a246b010SChuck Lever 
8269903cd1cSChuck Lever static inline struct rpc_xprt *xprt_from_sock(struct sock *sk)
8279903cd1cSChuck Lever {
8289903cd1cSChuck Lever 	return (struct rpc_xprt *) sk->sk_user_data;
8299903cd1cSChuck Lever }
8309903cd1cSChuck Lever 
8319903cd1cSChuck Lever /**
8329903cd1cSChuck Lever  * xs_udp_data_ready - "data ready" callback for UDP sockets
8339903cd1cSChuck Lever  * @sk: socket with data to read
8349903cd1cSChuck Lever  * @len: how much data to read
8359903cd1cSChuck Lever  *
836a246b010SChuck Lever  */
8379903cd1cSChuck Lever static void xs_udp_data_ready(struct sock *sk, int len)
838a246b010SChuck Lever {
839a246b010SChuck Lever 	struct rpc_task *task;
840a246b010SChuck Lever 	struct rpc_xprt *xprt;
841a246b010SChuck Lever 	struct rpc_rqst *rovr;
842a246b010SChuck Lever 	struct sk_buff *skb;
843a246b010SChuck Lever 	int err, repsize, copied;
844d8ed029dSAlexey Dobriyan 	u32 _xid;
845d8ed029dSAlexey Dobriyan 	__be32 *xp;
846a246b010SChuck Lever 
847a246b010SChuck Lever 	read_lock(&sk->sk_callback_lock);
8489903cd1cSChuck Lever 	dprintk("RPC:       xs_udp_data_ready...\n");
8499903cd1cSChuck Lever 	if (!(xprt = xprt_from_sock(sk)))
850a246b010SChuck Lever 		goto out;
851a246b010SChuck Lever 
852a246b010SChuck Lever 	if ((skb = skb_recv_datagram(sk, 0, 1, &err)) == NULL)
853a246b010SChuck Lever 		goto out;
854a246b010SChuck Lever 
855a246b010SChuck Lever 	if (xprt->shutdown)
856a246b010SChuck Lever 		goto dropit;
857a246b010SChuck Lever 
858a246b010SChuck Lever 	repsize = skb->len - sizeof(struct udphdr);
859a246b010SChuck Lever 	if (repsize < 4) {
8609903cd1cSChuck Lever 		dprintk("RPC:       impossible RPC reply size %d!\n", repsize);
861a246b010SChuck Lever 		goto dropit;
862a246b010SChuck Lever 	}
863a246b010SChuck Lever 
864a246b010SChuck Lever 	/* Copy the XID from the skb... */
865a246b010SChuck Lever 	xp = skb_header_pointer(skb, sizeof(struct udphdr),
866a246b010SChuck Lever 				sizeof(_xid), &_xid);
867a246b010SChuck Lever 	if (xp == NULL)
868a246b010SChuck Lever 		goto dropit;
869a246b010SChuck Lever 
870a246b010SChuck Lever 	/* Look up and lock the request corresponding to the given XID */
8714a0f8c04SChuck Lever 	spin_lock(&xprt->transport_lock);
872a246b010SChuck Lever 	rovr = xprt_lookup_rqst(xprt, *xp);
873a246b010SChuck Lever 	if (!rovr)
874a246b010SChuck Lever 		goto out_unlock;
875a246b010SChuck Lever 	task = rovr->rq_task;
876a246b010SChuck Lever 
877a246b010SChuck Lever 	if ((copied = rovr->rq_private_buf.buflen) > repsize)
878a246b010SChuck Lever 		copied = repsize;
879a246b010SChuck Lever 
880a246b010SChuck Lever 	/* Suck it into the iovec, verify checksum if not done by hw. */
8811781f7f5SHerbert Xu 	if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) {
8821781f7f5SHerbert Xu 		UDPX_INC_STATS_BH(sk, UDP_MIB_INERRORS);
883a246b010SChuck Lever 		goto out_unlock;
8841781f7f5SHerbert Xu 	}
8851781f7f5SHerbert Xu 
8861781f7f5SHerbert Xu 	UDPX_INC_STATS_BH(sk, UDP_MIB_INDATAGRAMS);
887a246b010SChuck Lever 
888a246b010SChuck Lever 	/* Something worked... */
889a246b010SChuck Lever 	dst_confirm(skb->dst);
890a246b010SChuck Lever 
8911570c1e4SChuck Lever 	xprt_adjust_cwnd(task, copied);
8921570c1e4SChuck Lever 	xprt_update_rtt(task);
8931570c1e4SChuck Lever 	xprt_complete_rqst(task, copied);
894a246b010SChuck Lever 
895a246b010SChuck Lever  out_unlock:
8964a0f8c04SChuck Lever 	spin_unlock(&xprt->transport_lock);
897a246b010SChuck Lever  dropit:
898a246b010SChuck Lever 	skb_free_datagram(sk, skb);
899a246b010SChuck Lever  out:
900a246b010SChuck Lever 	read_unlock(&sk->sk_callback_lock);
901a246b010SChuck Lever }
902a246b010SChuck Lever 
903dd456471SChuck Lever static inline void xs_tcp_read_fraghdr(struct rpc_xprt *xprt, struct xdr_skb_reader *desc)
904a246b010SChuck Lever {
90551971139SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
906a246b010SChuck Lever 	size_t len, used;
907a246b010SChuck Lever 	char *p;
908a246b010SChuck Lever 
90951971139SChuck Lever 	p = ((char *) &transport->tcp_fraghdr) + transport->tcp_offset;
91051971139SChuck Lever 	len = sizeof(transport->tcp_fraghdr) - 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;
915808012fbSChuck Lever 
91651971139SChuck Lever 	transport->tcp_reclen = ntohl(transport->tcp_fraghdr);
91751971139SChuck Lever 	if (transport->tcp_reclen & RPC_LAST_STREAM_FRAGMENT)
918e136d092SChuck Lever 		transport->tcp_flags |= TCP_RCV_LAST_FRAG;
919a246b010SChuck Lever 	else
920e136d092SChuck Lever 		transport->tcp_flags &= ~TCP_RCV_LAST_FRAG;
92151971139SChuck Lever 	transport->tcp_reclen &= RPC_FRAGMENT_SIZE_MASK;
922808012fbSChuck Lever 
923e136d092SChuck Lever 	transport->tcp_flags &= ~TCP_RCV_COPY_FRAGHDR;
92451971139SChuck Lever 	transport->tcp_offset = 0;
925808012fbSChuck Lever 
926a246b010SChuck Lever 	/* Sanity check of the record length */
92751971139SChuck Lever 	if (unlikely(transport->tcp_reclen < 4)) {
9289903cd1cSChuck Lever 		dprintk("RPC:       invalid TCP record fragment length\n");
9293ebb067dSTrond Myklebust 		xprt_force_disconnect(xprt);
9309903cd1cSChuck Lever 		return;
931a246b010SChuck Lever 	}
932a246b010SChuck Lever 	dprintk("RPC:       reading TCP record fragment of length %d\n",
93351971139SChuck Lever 			transport->tcp_reclen);
934a246b010SChuck Lever }
935a246b010SChuck Lever 
93651971139SChuck Lever static void xs_tcp_check_fraghdr(struct sock_xprt *transport)
937a246b010SChuck Lever {
93851971139SChuck Lever 	if (transport->tcp_offset == transport->tcp_reclen) {
939e136d092SChuck Lever 		transport->tcp_flags |= TCP_RCV_COPY_FRAGHDR;
94051971139SChuck Lever 		transport->tcp_offset = 0;
941e136d092SChuck Lever 		if (transport->tcp_flags & TCP_RCV_LAST_FRAG) {
942e136d092SChuck Lever 			transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
943e136d092SChuck Lever 			transport->tcp_flags |= TCP_RCV_COPY_XID;
94451971139SChuck Lever 			transport->tcp_copied = 0;
945a246b010SChuck Lever 		}
946a246b010SChuck Lever 	}
947a246b010SChuck Lever }
948a246b010SChuck Lever 
949dd456471SChuck Lever static inline void xs_tcp_read_xid(struct sock_xprt *transport, struct xdr_skb_reader *desc)
950a246b010SChuck Lever {
951a246b010SChuck Lever 	size_t len, used;
952a246b010SChuck Lever 	char *p;
953a246b010SChuck Lever 
95451971139SChuck Lever 	len = sizeof(transport->tcp_xid) - transport->tcp_offset;
955a246b010SChuck Lever 	dprintk("RPC:       reading XID (%Zu bytes)\n", len);
95651971139SChuck Lever 	p = ((char *) &transport->tcp_xid) + transport->tcp_offset;
9579d292316SChuck Lever 	used = xdr_skb_read_bits(desc, p, len);
95851971139SChuck Lever 	transport->tcp_offset += used;
959a246b010SChuck Lever 	if (used != len)
960a246b010SChuck Lever 		return;
961e136d092SChuck Lever 	transport->tcp_flags &= ~TCP_RCV_COPY_XID;
962e136d092SChuck Lever 	transport->tcp_flags |= TCP_RCV_COPY_DATA;
96351971139SChuck Lever 	transport->tcp_copied = 4;
964a246b010SChuck Lever 	dprintk("RPC:       reading reply for XID %08x\n",
96551971139SChuck Lever 			ntohl(transport->tcp_xid));
96651971139SChuck Lever 	xs_tcp_check_fraghdr(transport);
967a246b010SChuck Lever }
968a246b010SChuck Lever 
969dd456471SChuck Lever static inline void xs_tcp_read_request(struct rpc_xprt *xprt, struct xdr_skb_reader *desc)
970a246b010SChuck Lever {
97151971139SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
972a246b010SChuck Lever 	struct rpc_rqst *req;
973a246b010SChuck Lever 	struct xdr_buf *rcvbuf;
974a246b010SChuck Lever 	size_t len;
975a246b010SChuck Lever 	ssize_t r;
976a246b010SChuck Lever 
977a246b010SChuck Lever 	/* Find and lock the request corresponding to this xid */
9784a0f8c04SChuck Lever 	spin_lock(&xprt->transport_lock);
97951971139SChuck Lever 	req = xprt_lookup_rqst(xprt, transport->tcp_xid);
980a246b010SChuck Lever 	if (!req) {
981e136d092SChuck Lever 		transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
982a246b010SChuck Lever 		dprintk("RPC:       XID %08x request not found!\n",
98351971139SChuck Lever 				ntohl(transport->tcp_xid));
9844a0f8c04SChuck Lever 		spin_unlock(&xprt->transport_lock);
985a246b010SChuck Lever 		return;
986a246b010SChuck Lever 	}
987a246b010SChuck Lever 
988a246b010SChuck Lever 	rcvbuf = &req->rq_private_buf;
989a246b010SChuck Lever 	len = desc->count;
99051971139SChuck Lever 	if (len > transport->tcp_reclen - transport->tcp_offset) {
991dd456471SChuck Lever 		struct xdr_skb_reader my_desc;
992a246b010SChuck Lever 
99351971139SChuck Lever 		len = transport->tcp_reclen - transport->tcp_offset;
994a246b010SChuck Lever 		memcpy(&my_desc, desc, sizeof(my_desc));
995a246b010SChuck Lever 		my_desc.count = len;
99651971139SChuck Lever 		r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
9979d292316SChuck Lever 					  &my_desc, xdr_skb_read_bits);
998a246b010SChuck Lever 		desc->count -= r;
999a246b010SChuck Lever 		desc->offset += r;
1000a246b010SChuck Lever 	} else
100151971139SChuck Lever 		r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
10029d292316SChuck Lever 					  desc, xdr_skb_read_bits);
1003a246b010SChuck Lever 
1004a246b010SChuck Lever 	if (r > 0) {
100551971139SChuck Lever 		transport->tcp_copied += r;
100651971139SChuck Lever 		transport->tcp_offset += r;
1007a246b010SChuck Lever 	}
1008a246b010SChuck Lever 	if (r != len) {
1009a246b010SChuck Lever 		/* Error when copying to the receive buffer,
1010a246b010SChuck Lever 		 * usually because we weren't able to allocate
1011a246b010SChuck Lever 		 * additional buffer pages. All we can do now
1012e136d092SChuck Lever 		 * is turn off TCP_RCV_COPY_DATA, so the request
1013a246b010SChuck Lever 		 * will not receive any additional updates,
1014a246b010SChuck Lever 		 * and time out.
1015a246b010SChuck Lever 		 * Any remaining data from this record will
1016a246b010SChuck Lever 		 * be discarded.
1017a246b010SChuck Lever 		 */
1018e136d092SChuck Lever 		transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1019a246b010SChuck Lever 		dprintk("RPC:       XID %08x truncated request\n",
102051971139SChuck Lever 				ntohl(transport->tcp_xid));
102146121cf7SChuck Lever 		dprintk("RPC:       xprt = %p, tcp_copied = %lu, "
102246121cf7SChuck Lever 				"tcp_offset = %u, tcp_reclen = %u\n",
102346121cf7SChuck Lever 				xprt, transport->tcp_copied,
102446121cf7SChuck Lever 				transport->tcp_offset, transport->tcp_reclen);
1025a246b010SChuck Lever 		goto out;
1026a246b010SChuck Lever 	}
1027a246b010SChuck Lever 
1028a246b010SChuck Lever 	dprintk("RPC:       XID %08x read %Zd bytes\n",
102951971139SChuck Lever 			ntohl(transport->tcp_xid), r);
103046121cf7SChuck Lever 	dprintk("RPC:       xprt = %p, tcp_copied = %lu, tcp_offset = %u, "
103146121cf7SChuck Lever 			"tcp_reclen = %u\n", xprt, transport->tcp_copied,
103246121cf7SChuck Lever 			transport->tcp_offset, transport->tcp_reclen);
1033a246b010SChuck Lever 
103451971139SChuck Lever 	if (transport->tcp_copied == req->rq_private_buf.buflen)
1035e136d092SChuck Lever 		transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
103651971139SChuck Lever 	else if (transport->tcp_offset == transport->tcp_reclen) {
1037e136d092SChuck Lever 		if (transport->tcp_flags & TCP_RCV_LAST_FRAG)
1038e136d092SChuck Lever 			transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1039a246b010SChuck Lever 	}
1040a246b010SChuck Lever 
1041a246b010SChuck Lever out:
1042e136d092SChuck Lever 	if (!(transport->tcp_flags & TCP_RCV_COPY_DATA))
104351971139SChuck Lever 		xprt_complete_rqst(req->rq_task, transport->tcp_copied);
10444a0f8c04SChuck Lever 	spin_unlock(&xprt->transport_lock);
104551971139SChuck Lever 	xs_tcp_check_fraghdr(transport);
1046a246b010SChuck Lever }
1047a246b010SChuck Lever 
1048dd456471SChuck Lever static inline void xs_tcp_read_discard(struct sock_xprt *transport, struct xdr_skb_reader *desc)
1049a246b010SChuck Lever {
1050a246b010SChuck Lever 	size_t len;
1051a246b010SChuck Lever 
105251971139SChuck Lever 	len = transport->tcp_reclen - transport->tcp_offset;
1053a246b010SChuck Lever 	if (len > desc->count)
1054a246b010SChuck Lever 		len = desc->count;
1055a246b010SChuck Lever 	desc->count -= len;
1056a246b010SChuck Lever 	desc->offset += len;
105751971139SChuck Lever 	transport->tcp_offset += len;
1058a246b010SChuck Lever 	dprintk("RPC:       discarded %Zu bytes\n", len);
105951971139SChuck Lever 	xs_tcp_check_fraghdr(transport);
1060a246b010SChuck Lever }
1061a246b010SChuck Lever 
10629903cd1cSChuck Lever static int xs_tcp_data_recv(read_descriptor_t *rd_desc, struct sk_buff *skb, unsigned int offset, size_t len)
1063a246b010SChuck Lever {
1064a246b010SChuck Lever 	struct rpc_xprt *xprt = rd_desc->arg.data;
106551971139SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1066dd456471SChuck Lever 	struct xdr_skb_reader desc = {
1067a246b010SChuck Lever 		.skb	= skb,
1068a246b010SChuck Lever 		.offset	= offset,
1069a246b010SChuck Lever 		.count	= len,
1070a246b010SChuck Lever 	};
1071a246b010SChuck Lever 
10729903cd1cSChuck Lever 	dprintk("RPC:       xs_tcp_data_recv started\n");
1073a246b010SChuck Lever 	do {
1074a246b010SChuck Lever 		/* Read in a new fragment marker if necessary */
1075a246b010SChuck Lever 		/* Can we ever really expect to get completely empty fragments? */
1076e136d092SChuck Lever 		if (transport->tcp_flags & TCP_RCV_COPY_FRAGHDR) {
10779903cd1cSChuck Lever 			xs_tcp_read_fraghdr(xprt, &desc);
1078a246b010SChuck Lever 			continue;
1079a246b010SChuck Lever 		}
1080a246b010SChuck Lever 		/* Read in the xid if necessary */
1081e136d092SChuck Lever 		if (transport->tcp_flags & TCP_RCV_COPY_XID) {
108251971139SChuck Lever 			xs_tcp_read_xid(transport, &desc);
1083a246b010SChuck Lever 			continue;
1084a246b010SChuck Lever 		}
1085a246b010SChuck Lever 		/* Read in the request data */
1086e136d092SChuck Lever 		if (transport->tcp_flags & TCP_RCV_COPY_DATA) {
10879903cd1cSChuck Lever 			xs_tcp_read_request(xprt, &desc);
1088a246b010SChuck Lever 			continue;
1089a246b010SChuck Lever 		}
1090a246b010SChuck Lever 		/* Skip over any trailing bytes on short reads */
109151971139SChuck Lever 		xs_tcp_read_discard(transport, &desc);
1092a246b010SChuck Lever 	} while (desc.count);
10939903cd1cSChuck Lever 	dprintk("RPC:       xs_tcp_data_recv done\n");
1094a246b010SChuck Lever 	return len - desc.count;
1095a246b010SChuck Lever }
1096a246b010SChuck Lever 
10979903cd1cSChuck Lever /**
10989903cd1cSChuck Lever  * xs_tcp_data_ready - "data ready" callback for TCP sockets
10999903cd1cSChuck Lever  * @sk: socket with data to read
11009903cd1cSChuck Lever  * @bytes: how much data to read
11019903cd1cSChuck Lever  *
11029903cd1cSChuck Lever  */
11039903cd1cSChuck Lever static void xs_tcp_data_ready(struct sock *sk, int bytes)
1104a246b010SChuck Lever {
1105a246b010SChuck Lever 	struct rpc_xprt *xprt;
1106a246b010SChuck Lever 	read_descriptor_t rd_desc;
1107ff2d7db8STrond Myklebust 	int read;
1108a246b010SChuck Lever 
11099903cd1cSChuck Lever 	dprintk("RPC:       xs_tcp_data_ready...\n");
111046121cf7SChuck Lever 
111146121cf7SChuck Lever 	read_lock(&sk->sk_callback_lock);
11129903cd1cSChuck Lever 	if (!(xprt = xprt_from_sock(sk)))
1113a246b010SChuck Lever 		goto out;
1114a246b010SChuck Lever 	if (xprt->shutdown)
1115a246b010SChuck Lever 		goto out;
1116a246b010SChuck Lever 
11179903cd1cSChuck Lever 	/* We use rd_desc to pass struct xprt to xs_tcp_data_recv */
1118a246b010SChuck Lever 	rd_desc.arg.data = xprt;
1119ff2d7db8STrond Myklebust 	do {
1120a246b010SChuck Lever 		rd_desc.count = 65536;
1121ff2d7db8STrond Myklebust 		read = tcp_read_sock(sk, &rd_desc, xs_tcp_data_recv);
1122ff2d7db8STrond Myklebust 	} while (read > 0);
1123a246b010SChuck Lever out:
1124a246b010SChuck Lever 	read_unlock(&sk->sk_callback_lock);
1125a246b010SChuck Lever }
1126a246b010SChuck Lever 
11279903cd1cSChuck Lever /**
11289903cd1cSChuck Lever  * xs_tcp_state_change - callback to handle TCP socket state changes
11299903cd1cSChuck Lever  * @sk: socket whose state has changed
11309903cd1cSChuck Lever  *
11319903cd1cSChuck Lever  */
11329903cd1cSChuck Lever static void xs_tcp_state_change(struct sock *sk)
1133a246b010SChuck Lever {
1134a246b010SChuck Lever 	struct rpc_xprt *xprt;
1135a246b010SChuck Lever 
1136a246b010SChuck Lever 	read_lock(&sk->sk_callback_lock);
1137a246b010SChuck Lever 	if (!(xprt = xprt_from_sock(sk)))
1138a246b010SChuck Lever 		goto out;
11399903cd1cSChuck Lever 	dprintk("RPC:       xs_tcp_state_change client %p...\n", xprt);
1140a246b010SChuck Lever 	dprintk("RPC:       state %x conn %d dead %d zapped %d\n",
1141a246b010SChuck Lever 			sk->sk_state, xprt_connected(xprt),
1142a246b010SChuck Lever 			sock_flag(sk, SOCK_DEAD),
1143a246b010SChuck Lever 			sock_flag(sk, SOCK_ZAPPED));
1144a246b010SChuck Lever 
1145a246b010SChuck Lever 	switch (sk->sk_state) {
1146a246b010SChuck Lever 	case TCP_ESTABLISHED:
11474a0f8c04SChuck Lever 		spin_lock_bh(&xprt->transport_lock);
1148a246b010SChuck Lever 		if (!xprt_test_and_set_connected(xprt)) {
114951971139SChuck Lever 			struct sock_xprt *transport = container_of(xprt,
115051971139SChuck Lever 					struct sock_xprt, xprt);
115151971139SChuck Lever 
1152a246b010SChuck Lever 			/* Reset TCP record info */
115351971139SChuck Lever 			transport->tcp_offset = 0;
115451971139SChuck Lever 			transport->tcp_reclen = 0;
115551971139SChuck Lever 			transport->tcp_copied = 0;
1156e136d092SChuck Lever 			transport->tcp_flags =
1157e136d092SChuck Lever 				TCP_RCV_COPY_FRAGHDR | TCP_RCV_COPY_XID;
115851971139SChuck Lever 
115944fbac22SChuck Lever 			xprt_wake_pending_tasks(xprt, 0);
1160a246b010SChuck Lever 		}
11614a0f8c04SChuck Lever 		spin_unlock_bh(&xprt->transport_lock);
1162a246b010SChuck Lever 		break;
11633b948ae5STrond Myklebust 	case TCP_FIN_WAIT1:
11643b948ae5STrond Myklebust 		/* The client initiated a shutdown of the socket */
11657c1d71cfSTrond Myklebust 		xprt->connect_cookie++;
1166663b8858STrond Myklebust 		xprt->reestablish_timeout = 0;
11673b948ae5STrond Myklebust 		set_bit(XPRT_CLOSING, &xprt->state);
11683b948ae5STrond Myklebust 		smp_mb__before_clear_bit();
11693b948ae5STrond Myklebust 		clear_bit(XPRT_CONNECTED, &xprt->state);
1170ef803670STrond Myklebust 		clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
11713b948ae5STrond Myklebust 		smp_mb__after_clear_bit();
1172a246b010SChuck Lever 		break;
1173632e3bdcSTrond Myklebust 	case TCP_CLOSE_WAIT:
11743b948ae5STrond Myklebust 		/* The server initiated a shutdown of the socket */
11753b948ae5STrond Myklebust 		set_bit(XPRT_CLOSING, &xprt->state);
117666af1e55STrond Myklebust 		xprt_force_disconnect(xprt);
1177663b8858STrond Myklebust 	case TCP_SYN_SENT:
11787c1d71cfSTrond Myklebust 		xprt->connect_cookie++;
1179663b8858STrond Myklebust 	case TCP_CLOSING:
1180663b8858STrond Myklebust 		/*
1181663b8858STrond Myklebust 		 * If the server closed down the connection, make sure that
1182663b8858STrond Myklebust 		 * we back off before reconnecting
1183663b8858STrond Myklebust 		 */
1184663b8858STrond Myklebust 		if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
1185663b8858STrond Myklebust 			xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
11863b948ae5STrond Myklebust 		break;
11873b948ae5STrond Myklebust 	case TCP_LAST_ACK:
11883b948ae5STrond Myklebust 		smp_mb__before_clear_bit();
11893b948ae5STrond Myklebust 		clear_bit(XPRT_CONNECTED, &xprt->state);
11903b948ae5STrond Myklebust 		smp_mb__after_clear_bit();
11913b948ae5STrond Myklebust 		break;
11923b948ae5STrond Myklebust 	case TCP_CLOSE:
11933b948ae5STrond Myklebust 		smp_mb__before_clear_bit();
1194ef803670STrond Myklebust 		clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
11953b948ae5STrond Myklebust 		clear_bit(XPRT_CLOSING, &xprt->state);
11963b948ae5STrond Myklebust 		smp_mb__after_clear_bit();
11973b948ae5STrond Myklebust 		/* Mark transport as closed and wake up all pending tasks */
119862da3b24STrond Myklebust 		xprt_disconnect_done(xprt);
1199a246b010SChuck Lever 	}
1200a246b010SChuck Lever  out:
1201a246b010SChuck Lever 	read_unlock(&sk->sk_callback_lock);
1202a246b010SChuck Lever }
1203a246b010SChuck Lever 
12049903cd1cSChuck Lever /**
12052a9e1cfaSTrond Myklebust  * xs_tcp_error_report - callback mainly for catching RST events
12062a9e1cfaSTrond Myklebust  * @sk: socket
12072a9e1cfaSTrond Myklebust  */
12082a9e1cfaSTrond Myklebust static void xs_tcp_error_report(struct sock *sk)
12092a9e1cfaSTrond Myklebust {
12102a9e1cfaSTrond Myklebust 	struct rpc_xprt *xprt;
12112a9e1cfaSTrond Myklebust 
12122a9e1cfaSTrond Myklebust 	read_lock(&sk->sk_callback_lock);
12132a9e1cfaSTrond Myklebust 	if (sk->sk_err != ECONNRESET || sk->sk_state != TCP_ESTABLISHED)
12142a9e1cfaSTrond Myklebust 		goto out;
12152a9e1cfaSTrond Myklebust 	if (!(xprt = xprt_from_sock(sk)))
12162a9e1cfaSTrond Myklebust 		goto out;
12172a9e1cfaSTrond Myklebust 	dprintk("RPC:       %s client %p...\n"
12182a9e1cfaSTrond Myklebust 			"RPC:       error %d\n",
12192a9e1cfaSTrond Myklebust 			__func__, xprt, sk->sk_err);
12202a9e1cfaSTrond Myklebust 
12212a9e1cfaSTrond Myklebust 	xprt_force_disconnect(xprt);
12222a9e1cfaSTrond Myklebust out:
12232a9e1cfaSTrond Myklebust 	read_unlock(&sk->sk_callback_lock);
12242a9e1cfaSTrond Myklebust }
12252a9e1cfaSTrond Myklebust 
12262a9e1cfaSTrond Myklebust /**
1227c7b2cae8SChuck Lever  * xs_udp_write_space - callback invoked when socket buffer space
1228c7b2cae8SChuck Lever  *                             becomes available
12299903cd1cSChuck Lever  * @sk: socket whose state has changed
12309903cd1cSChuck Lever  *
1231a246b010SChuck Lever  * Called when more output buffer space is available for this socket.
1232a246b010SChuck Lever  * We try not to wake our writers until they can make "significant"
1233c7b2cae8SChuck Lever  * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1234a246b010SChuck Lever  * with a bunch of small requests.
1235a246b010SChuck Lever  */
1236c7b2cae8SChuck Lever static void xs_udp_write_space(struct sock *sk)
1237a246b010SChuck Lever {
1238a246b010SChuck Lever 	read_lock(&sk->sk_callback_lock);
1239c7b2cae8SChuck Lever 
1240c7b2cae8SChuck Lever 	/* from net/core/sock.c:sock_def_write_space */
1241c7b2cae8SChuck Lever 	if (sock_writeable(sk)) {
1242c7b2cae8SChuck Lever 		struct socket *sock;
1243c7b2cae8SChuck Lever 		struct rpc_xprt *xprt;
1244c7b2cae8SChuck Lever 
1245c7b2cae8SChuck Lever 		if (unlikely(!(sock = sk->sk_socket)))
1246a246b010SChuck Lever 			goto out;
1247b6ddf64fSTrond Myklebust 		clear_bit(SOCK_NOSPACE, &sock->flags);
1248b6ddf64fSTrond Myklebust 
1249c7b2cae8SChuck Lever 		if (unlikely(!(xprt = xprt_from_sock(sk))))
1250c7b2cae8SChuck Lever 			goto out;
1251b6ddf64fSTrond Myklebust 		if (test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags) == 0)
1252a246b010SChuck Lever 			goto out;
1253a246b010SChuck Lever 
1254c7b2cae8SChuck Lever 		xprt_write_space(xprt);
1255a246b010SChuck Lever 	}
1256a246b010SChuck Lever 
1257c7b2cae8SChuck Lever  out:
1258c7b2cae8SChuck Lever 	read_unlock(&sk->sk_callback_lock);
1259c7b2cae8SChuck Lever }
1260c7b2cae8SChuck Lever 
1261c7b2cae8SChuck Lever /**
1262c7b2cae8SChuck Lever  * xs_tcp_write_space - callback invoked when socket buffer space
1263c7b2cae8SChuck Lever  *                             becomes available
1264c7b2cae8SChuck Lever  * @sk: socket whose state has changed
1265c7b2cae8SChuck Lever  *
1266c7b2cae8SChuck Lever  * Called when more output buffer space is available for this socket.
1267c7b2cae8SChuck Lever  * We try not to wake our writers until they can make "significant"
1268c7b2cae8SChuck Lever  * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1269c7b2cae8SChuck Lever  * with a bunch of small requests.
1270c7b2cae8SChuck Lever  */
1271c7b2cae8SChuck Lever static void xs_tcp_write_space(struct sock *sk)
1272c7b2cae8SChuck Lever {
1273c7b2cae8SChuck Lever 	read_lock(&sk->sk_callback_lock);
1274c7b2cae8SChuck Lever 
1275c7b2cae8SChuck Lever 	/* from net/core/stream.c:sk_stream_write_space */
1276c7b2cae8SChuck Lever 	if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk)) {
1277c7b2cae8SChuck Lever 		struct socket *sock;
1278c7b2cae8SChuck Lever 		struct rpc_xprt *xprt;
1279c7b2cae8SChuck Lever 
1280c7b2cae8SChuck Lever 		if (unlikely(!(sock = sk->sk_socket)))
1281c7b2cae8SChuck Lever 			goto out;
1282b6ddf64fSTrond Myklebust 		clear_bit(SOCK_NOSPACE, &sock->flags);
1283b6ddf64fSTrond Myklebust 
1284c7b2cae8SChuck Lever 		if (unlikely(!(xprt = xprt_from_sock(sk))))
1285c7b2cae8SChuck Lever 			goto out;
1286b6ddf64fSTrond Myklebust 		if (test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags) == 0)
1287a246b010SChuck Lever 			goto out;
1288a246b010SChuck Lever 
1289c7b2cae8SChuck Lever 		xprt_write_space(xprt);
1290c7b2cae8SChuck Lever 	}
1291c7b2cae8SChuck Lever 
1292a246b010SChuck Lever  out:
1293a246b010SChuck Lever 	read_unlock(&sk->sk_callback_lock);
1294a246b010SChuck Lever }
1295a246b010SChuck Lever 
1296470056c2SChuck Lever static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt)
1297a246b010SChuck Lever {
1298ee0ac0c2SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1299ee0ac0c2SChuck Lever 	struct sock *sk = transport->inet;
1300a246b010SChuck Lever 
13017c6e066eSChuck Lever 	if (transport->rcvsize) {
1302a246b010SChuck Lever 		sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
13037c6e066eSChuck Lever 		sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2;
1304a246b010SChuck Lever 	}
13057c6e066eSChuck Lever 	if (transport->sndsize) {
1306a246b010SChuck Lever 		sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
13077c6e066eSChuck Lever 		sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2;
1308a246b010SChuck Lever 		sk->sk_write_space(sk);
1309a246b010SChuck Lever 	}
1310a246b010SChuck Lever }
1311a246b010SChuck Lever 
131243118c29SChuck Lever /**
1313470056c2SChuck Lever  * xs_udp_set_buffer_size - set send and receive limits
131443118c29SChuck Lever  * @xprt: generic transport
1315470056c2SChuck Lever  * @sndsize: requested size of send buffer, in bytes
1316470056c2SChuck Lever  * @rcvsize: requested size of receive buffer, in bytes
131743118c29SChuck Lever  *
1318470056c2SChuck Lever  * Set socket send and receive buffer size limits.
131943118c29SChuck Lever  */
1320470056c2SChuck Lever static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize)
132143118c29SChuck Lever {
13227c6e066eSChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
13237c6e066eSChuck Lever 
13247c6e066eSChuck Lever 	transport->sndsize = 0;
1325470056c2SChuck Lever 	if (sndsize)
13267c6e066eSChuck Lever 		transport->sndsize = sndsize + 1024;
13277c6e066eSChuck Lever 	transport->rcvsize = 0;
1328470056c2SChuck Lever 	if (rcvsize)
13297c6e066eSChuck Lever 		transport->rcvsize = rcvsize + 1024;
1330470056c2SChuck Lever 
1331470056c2SChuck Lever 	xs_udp_do_set_buffer_size(xprt);
133243118c29SChuck Lever }
133343118c29SChuck Lever 
133446c0ee8bSChuck Lever /**
133546c0ee8bSChuck Lever  * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
133646c0ee8bSChuck Lever  * @task: task that timed out
133746c0ee8bSChuck Lever  *
133846c0ee8bSChuck Lever  * Adjust the congestion window after a retransmit timeout has occurred.
133946c0ee8bSChuck Lever  */
134046c0ee8bSChuck Lever static void xs_udp_timer(struct rpc_task *task)
134146c0ee8bSChuck Lever {
134246c0ee8bSChuck Lever 	xprt_adjust_cwnd(task, -ETIMEDOUT);
134346c0ee8bSChuck Lever }
134446c0ee8bSChuck Lever 
1345b85d8806SChuck Lever static unsigned short xs_get_random_port(void)
1346b85d8806SChuck Lever {
1347b85d8806SChuck Lever 	unsigned short range = xprt_max_resvport - xprt_min_resvport;
1348b85d8806SChuck Lever 	unsigned short rand = (unsigned short) net_random() % range;
1349b85d8806SChuck Lever 	return rand + xprt_min_resvport;
1350b85d8806SChuck Lever }
1351b85d8806SChuck Lever 
135292200412SChuck Lever /**
135392200412SChuck Lever  * xs_set_port - reset the port number in the remote endpoint address
135492200412SChuck Lever  * @xprt: generic transport
135592200412SChuck Lever  * @port: new port number
135692200412SChuck Lever  *
135792200412SChuck Lever  */
135892200412SChuck Lever static void xs_set_port(struct rpc_xprt *xprt, unsigned short port)
135992200412SChuck Lever {
136095392c59SChuck Lever 	struct sockaddr *addr = xs_addr(xprt);
1361c4efcb1dSChuck Lever 
136292200412SChuck Lever 	dprintk("RPC:       setting port for xprt %p to %u\n", xprt, port);
1363c4efcb1dSChuck Lever 
136420612005SChuck Lever 	switch (addr->sa_family) {
136520612005SChuck Lever 	case AF_INET:
136620612005SChuck Lever 		((struct sockaddr_in *)addr)->sin_port = htons(port);
136720612005SChuck Lever 		break;
136820612005SChuck Lever 	case AF_INET6:
136920612005SChuck Lever 		((struct sockaddr_in6 *)addr)->sin6_port = htons(port);
137020612005SChuck Lever 		break;
137120612005SChuck Lever 	default:
137220612005SChuck Lever 		BUG();
137320612005SChuck Lever 	}
137492200412SChuck Lever }
137592200412SChuck Lever 
137667a391d7STrond Myklebust static unsigned short xs_get_srcport(struct sock_xprt *transport, struct socket *sock)
137767a391d7STrond Myklebust {
137867a391d7STrond Myklebust 	unsigned short port = transport->port;
137967a391d7STrond Myklebust 
138067a391d7STrond Myklebust 	if (port == 0 && transport->xprt.resvport)
138167a391d7STrond Myklebust 		port = xs_get_random_port();
138267a391d7STrond Myklebust 	return port;
138367a391d7STrond Myklebust }
138467a391d7STrond Myklebust 
138567a391d7STrond Myklebust static unsigned short xs_next_srcport(struct sock_xprt *transport, struct socket *sock, unsigned short port)
138667a391d7STrond Myklebust {
138767a391d7STrond Myklebust 	if (transport->port != 0)
138867a391d7STrond Myklebust 		transport->port = 0;
138967a391d7STrond Myklebust 	if (!transport->xprt.resvport)
139067a391d7STrond Myklebust 		return 0;
139167a391d7STrond Myklebust 	if (port <= xprt_min_resvport || port > xprt_max_resvport)
139267a391d7STrond Myklebust 		return xprt_max_resvport;
139367a391d7STrond Myklebust 	return --port;
139467a391d7STrond Myklebust }
139567a391d7STrond Myklebust 
13967dc753f0SChuck Lever static int xs_bind4(struct sock_xprt *transport, struct socket *sock)
1397a246b010SChuck Lever {
1398a246b010SChuck Lever 	struct sockaddr_in myaddr = {
1399a246b010SChuck Lever 		.sin_family = AF_INET,
1400a246b010SChuck Lever 	};
1401d3bc9a1dSFrank van Maarseveen 	struct sockaddr_in *sa;
140267a391d7STrond Myklebust 	int err, nloop = 0;
140367a391d7STrond Myklebust 	unsigned short port = xs_get_srcport(transport, sock);
140467a391d7STrond Myklebust 	unsigned short last;
1405a246b010SChuck Lever 
1406d3bc9a1dSFrank van Maarseveen 	sa = (struct sockaddr_in *)&transport->addr;
1407d3bc9a1dSFrank van Maarseveen 	myaddr.sin_addr = sa->sin_addr;
1408a246b010SChuck Lever 	do {
1409a246b010SChuck Lever 		myaddr.sin_port = htons(port);
1410e6242e92SSridhar Samudrala 		err = kernel_bind(sock, (struct sockaddr *) &myaddr,
1411a246b010SChuck Lever 						sizeof(myaddr));
141267a391d7STrond Myklebust 		if (port == 0)
1413d3bc9a1dSFrank van Maarseveen 			break;
1414a246b010SChuck Lever 		if (err == 0) {
1415c8475461SChuck Lever 			transport->port = port;
1416d3bc9a1dSFrank van Maarseveen 			break;
1417a246b010SChuck Lever 		}
141867a391d7STrond Myklebust 		last = port;
141967a391d7STrond Myklebust 		port = xs_next_srcport(transport, sock, port);
142067a391d7STrond Myklebust 		if (port > last)
142167a391d7STrond Myklebust 			nloop++;
142267a391d7STrond Myklebust 	} while (err == -EADDRINUSE && nloop != 2);
142321454aaaSHarvey Harrison 	dprintk("RPC:       %s %pI4:%u: %s (%d)\n",
142421454aaaSHarvey Harrison 			__func__, &myaddr.sin_addr,
14257dc753f0SChuck Lever 			port, err ? "failed" : "ok", err);
1426a246b010SChuck Lever 	return err;
1427a246b010SChuck Lever }
1428a246b010SChuck Lever 
142990058d37SChuck Lever static int xs_bind6(struct sock_xprt *transport, struct socket *sock)
143090058d37SChuck Lever {
143190058d37SChuck Lever 	struct sockaddr_in6 myaddr = {
143290058d37SChuck Lever 		.sin6_family = AF_INET6,
143390058d37SChuck Lever 	};
143490058d37SChuck Lever 	struct sockaddr_in6 *sa;
143567a391d7STrond Myklebust 	int err, nloop = 0;
143667a391d7STrond Myklebust 	unsigned short port = xs_get_srcport(transport, sock);
143767a391d7STrond Myklebust 	unsigned short last;
143890058d37SChuck Lever 
143990058d37SChuck Lever 	sa = (struct sockaddr_in6 *)&transport->addr;
144090058d37SChuck Lever 	myaddr.sin6_addr = sa->sin6_addr;
144190058d37SChuck Lever 	do {
144290058d37SChuck Lever 		myaddr.sin6_port = htons(port);
144390058d37SChuck Lever 		err = kernel_bind(sock, (struct sockaddr *) &myaddr,
144490058d37SChuck Lever 						sizeof(myaddr));
144567a391d7STrond Myklebust 		if (port == 0)
144690058d37SChuck Lever 			break;
144790058d37SChuck Lever 		if (err == 0) {
144890058d37SChuck Lever 			transport->port = port;
144990058d37SChuck Lever 			break;
145090058d37SChuck Lever 		}
145167a391d7STrond Myklebust 		last = port;
145267a391d7STrond Myklebust 		port = xs_next_srcport(transport, sock, port);
145367a391d7STrond Myklebust 		if (port > last)
145467a391d7STrond Myklebust 			nloop++;
145567a391d7STrond Myklebust 	} while (err == -EADDRINUSE && nloop != 2);
14565b095d98SHarvey Harrison 	dprintk("RPC:       xs_bind6 %pI6:%u: %s (%d)\n",
1457fdb46ee7SHarvey Harrison 		&myaddr.sin6_addr, port, err ? "failed" : "ok", err);
1458a246b010SChuck Lever 	return err;
1459a246b010SChuck Lever }
1460a246b010SChuck Lever 
1461ed07536eSPeter Zijlstra #ifdef CONFIG_DEBUG_LOCK_ALLOC
1462ed07536eSPeter Zijlstra static struct lock_class_key xs_key[2];
1463ed07536eSPeter Zijlstra static struct lock_class_key xs_slock_key[2];
1464ed07536eSPeter Zijlstra 
14658945ee5eSChuck Lever static inline void xs_reclassify_socket4(struct socket *sock)
1466ed07536eSPeter Zijlstra {
1467ed07536eSPeter Zijlstra 	struct sock *sk = sock->sk;
14688945ee5eSChuck Lever 
146902b3d346SJohn Heffner 	BUG_ON(sock_owned_by_user(sk));
14708945ee5eSChuck Lever 	sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC",
14718945ee5eSChuck Lever 		&xs_slock_key[0], "sk_lock-AF_INET-RPC", &xs_key[0]);
1472ed07536eSPeter Zijlstra }
14738945ee5eSChuck Lever 
14748945ee5eSChuck Lever static inline void xs_reclassify_socket6(struct socket *sock)
14758945ee5eSChuck Lever {
14768945ee5eSChuck Lever 	struct sock *sk = sock->sk;
14778945ee5eSChuck Lever 
1478f4921affSLinus Torvalds 	BUG_ON(sock_owned_by_user(sk));
14798945ee5eSChuck Lever 	sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC",
14808945ee5eSChuck Lever 		&xs_slock_key[1], "sk_lock-AF_INET6-RPC", &xs_key[1]);
1481ed07536eSPeter Zijlstra }
1482ed07536eSPeter Zijlstra #else
14838945ee5eSChuck Lever static inline void xs_reclassify_socket4(struct socket *sock)
14848945ee5eSChuck Lever {
14858945ee5eSChuck Lever }
14868945ee5eSChuck Lever 
14878945ee5eSChuck Lever static inline void xs_reclassify_socket6(struct socket *sock)
1488ed07536eSPeter Zijlstra {
1489ed07536eSPeter Zijlstra }
1490ed07536eSPeter Zijlstra #endif
1491ed07536eSPeter Zijlstra 
149216be2d20SChuck Lever static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
1493a246b010SChuck Lever {
149416be2d20SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1495edb267a6SChuck Lever 
1496ee0ac0c2SChuck Lever 	if (!transport->inet) {
1497b0d93ad5SChuck Lever 		struct sock *sk = sock->sk;
1498b0d93ad5SChuck Lever 
1499b0d93ad5SChuck Lever 		write_lock_bh(&sk->sk_callback_lock);
1500b0d93ad5SChuck Lever 
15012a9e1cfaSTrond Myklebust 		xs_save_old_callbacks(transport, sk);
15022a9e1cfaSTrond Myklebust 
1503b0d93ad5SChuck Lever 		sk->sk_user_data = xprt;
1504b0d93ad5SChuck Lever 		sk->sk_data_ready = xs_udp_data_ready;
1505b0d93ad5SChuck Lever 		sk->sk_write_space = xs_udp_write_space;
1506b0d93ad5SChuck Lever 		sk->sk_no_check = UDP_CSUM_NORCV;
1507b079fa7bSTrond Myklebust 		sk->sk_allocation = GFP_ATOMIC;
1508b0d93ad5SChuck Lever 
1509b0d93ad5SChuck Lever 		xprt_set_connected(xprt);
1510b0d93ad5SChuck Lever 
1511b0d93ad5SChuck Lever 		/* Reset to new socket */
1512ee0ac0c2SChuck Lever 		transport->sock = sock;
1513ee0ac0c2SChuck Lever 		transport->inet = sk;
1514b0d93ad5SChuck Lever 
1515b0d93ad5SChuck Lever 		write_unlock_bh(&sk->sk_callback_lock);
1516b0d93ad5SChuck Lever 	}
1517470056c2SChuck Lever 	xs_udp_do_set_buffer_size(xprt);
151816be2d20SChuck Lever }
151916be2d20SChuck Lever 
1520a246b010SChuck Lever /**
15219c3d72deSChuck Lever  * xs_udp_connect_worker4 - set up a UDP socket
1522a246b010SChuck Lever  * @work: RPC transport to connect
1523a246b010SChuck Lever  *
1524a246b010SChuck Lever  * Invoked by a work queue tasklet.
1525a246b010SChuck Lever  */
15269c3d72deSChuck Lever static void xs_udp_connect_worker4(struct work_struct *work)
1527a246b010SChuck Lever {
1528a246b010SChuck Lever 	struct sock_xprt *transport =
1529a246b010SChuck Lever 		container_of(work, struct sock_xprt, connect_worker.work);
1530a246b010SChuck Lever 	struct rpc_xprt *xprt = &transport->xprt;
1531a246b010SChuck Lever 	struct socket *sock = transport->sock;
1532a246b010SChuck Lever 	int err, status = -EIO;
1533a246b010SChuck Lever 
1534a246b010SChuck Lever 	if (xprt->shutdown || !xprt_bound(xprt))
15359903cd1cSChuck Lever 		goto out;
15369903cd1cSChuck Lever 
1537a246b010SChuck Lever 	/* Start by resetting any existing state */
1538a246b010SChuck Lever 	xs_close(xprt);
1539a246b010SChuck Lever 
15409903cd1cSChuck Lever 	if ((err = sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock)) < 0) {
15419903cd1cSChuck Lever 		dprintk("RPC:       can't create UDP transport socket (%d).\n", -err);
1542a246b010SChuck Lever 		goto out;
1543a246b010SChuck Lever 	}
15448945ee5eSChuck Lever 	xs_reclassify_socket4(sock);
1545a246b010SChuck Lever 
15467dc753f0SChuck Lever 	if (xs_bind4(transport, sock)) {
15479903cd1cSChuck Lever 		sock_release(sock);
15489903cd1cSChuck Lever 		goto out;
1549a246b010SChuck Lever 	}
1550b0d93ad5SChuck Lever 
1551b0d93ad5SChuck Lever 	dprintk("RPC:       worker connecting xprt %p to address: %s\n",
1552b0d93ad5SChuck Lever 			xprt, xprt->address_strings[RPC_DISPLAY_ALL]);
1553b0d93ad5SChuck Lever 
155416be2d20SChuck Lever 	xs_udp_finish_connecting(xprt, sock);
1555a246b010SChuck Lever 	status = 0;
1556b0d93ad5SChuck Lever out:
1557b0d93ad5SChuck Lever 	xprt_wake_pending_tasks(xprt, status);
1558b0d93ad5SChuck Lever 	xprt_clear_connecting(xprt);
1559b0d93ad5SChuck Lever }
1560b0d93ad5SChuck Lever 
156168e220bdSChuck Lever /**
156268e220bdSChuck Lever  * xs_udp_connect_worker6 - set up a UDP socket
156368e220bdSChuck Lever  * @work: RPC transport to connect
156468e220bdSChuck Lever  *
156568e220bdSChuck Lever  * Invoked by a work queue tasklet.
156668e220bdSChuck Lever  */
156768e220bdSChuck Lever static void xs_udp_connect_worker6(struct work_struct *work)
156868e220bdSChuck Lever {
156968e220bdSChuck Lever 	struct sock_xprt *transport =
157068e220bdSChuck Lever 		container_of(work, struct sock_xprt, connect_worker.work);
157168e220bdSChuck Lever 	struct rpc_xprt *xprt = &transport->xprt;
157268e220bdSChuck Lever 	struct socket *sock = transport->sock;
157368e220bdSChuck Lever 	int err, status = -EIO;
157468e220bdSChuck Lever 
157568e220bdSChuck Lever 	if (xprt->shutdown || !xprt_bound(xprt))
157668e220bdSChuck Lever 		goto out;
157768e220bdSChuck Lever 
157868e220bdSChuck Lever 	/* Start by resetting any existing state */
157968e220bdSChuck Lever 	xs_close(xprt);
158068e220bdSChuck Lever 
158168e220bdSChuck Lever 	if ((err = sock_create_kern(PF_INET6, SOCK_DGRAM, IPPROTO_UDP, &sock)) < 0) {
158268e220bdSChuck Lever 		dprintk("RPC:       can't create UDP transport socket (%d).\n", -err);
158368e220bdSChuck Lever 		goto out;
158468e220bdSChuck Lever 	}
15858945ee5eSChuck Lever 	xs_reclassify_socket6(sock);
158668e220bdSChuck Lever 
158768e220bdSChuck Lever 	if (xs_bind6(transport, sock) < 0) {
158868e220bdSChuck Lever 		sock_release(sock);
158968e220bdSChuck Lever 		goto out;
159068e220bdSChuck Lever 	}
159168e220bdSChuck Lever 
159268e220bdSChuck Lever 	dprintk("RPC:       worker connecting xprt %p to address: %s\n",
159368e220bdSChuck Lever 			xprt, xprt->address_strings[RPC_DISPLAY_ALL]);
159468e220bdSChuck Lever 
159568e220bdSChuck Lever 	xs_udp_finish_connecting(xprt, sock);
1596a246b010SChuck Lever 	status = 0;
1597b0d93ad5SChuck Lever out:
1598b0d93ad5SChuck Lever 	xprt_wake_pending_tasks(xprt, status);
1599b0d93ad5SChuck Lever 	xprt_clear_connecting(xprt);
1600b0d93ad5SChuck Lever }
1601b0d93ad5SChuck Lever 
16023167e12cSChuck Lever /*
16033167e12cSChuck Lever  * We need to preserve the port number so the reply cache on the server can
16043167e12cSChuck Lever  * find our cached RPC replies when we get around to reconnecting.
16053167e12cSChuck Lever  */
16063167e12cSChuck Lever static void xs_tcp_reuse_connection(struct rpc_xprt *xprt)
16073167e12cSChuck Lever {
16083167e12cSChuck Lever 	int result;
1609ee0ac0c2SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
16103167e12cSChuck Lever 	struct sockaddr any;
16113167e12cSChuck Lever 
16123167e12cSChuck Lever 	dprintk("RPC:       disconnecting xprt %p to reuse port\n", xprt);
16133167e12cSChuck Lever 
16143167e12cSChuck Lever 	/*
16153167e12cSChuck Lever 	 * Disconnect the transport socket by doing a connect operation
16163167e12cSChuck Lever 	 * with AF_UNSPEC.  This should return immediately...
16173167e12cSChuck Lever 	 */
16183167e12cSChuck Lever 	memset(&any, 0, sizeof(any));
16193167e12cSChuck Lever 	any.sa_family = AF_UNSPEC;
1620ee0ac0c2SChuck Lever 	result = kernel_connect(transport->sock, &any, sizeof(any), 0);
16213167e12cSChuck Lever 	if (result)
16223167e12cSChuck Lever 		dprintk("RPC:       AF_UNSPEC connect return code %d\n",
16233167e12cSChuck Lever 				result);
16243167e12cSChuck Lever }
16253167e12cSChuck Lever 
162616be2d20SChuck Lever static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
1627b0d93ad5SChuck Lever {
162816be2d20SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1629edb267a6SChuck Lever 
1630ee0ac0c2SChuck Lever 	if (!transport->inet) {
1631b0d93ad5SChuck Lever 		struct sock *sk = sock->sk;
1632b0d93ad5SChuck Lever 
1633b0d93ad5SChuck Lever 		write_lock_bh(&sk->sk_callback_lock);
1634b0d93ad5SChuck Lever 
16352a9e1cfaSTrond Myklebust 		xs_save_old_callbacks(transport, sk);
16362a9e1cfaSTrond Myklebust 
1637b0d93ad5SChuck Lever 		sk->sk_user_data = xprt;
1638b0d93ad5SChuck Lever 		sk->sk_data_ready = xs_tcp_data_ready;
1639b0d93ad5SChuck Lever 		sk->sk_state_change = xs_tcp_state_change;
1640b0d93ad5SChuck Lever 		sk->sk_write_space = xs_tcp_write_space;
16412a9e1cfaSTrond Myklebust 		sk->sk_error_report = xs_tcp_error_report;
1642b079fa7bSTrond Myklebust 		sk->sk_allocation = GFP_ATOMIC;
16433167e12cSChuck Lever 
16443167e12cSChuck Lever 		/* socket options */
16453167e12cSChuck Lever 		sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
16463167e12cSChuck Lever 		sock_reset_flag(sk, SOCK_LINGER);
16473167e12cSChuck Lever 		tcp_sk(sk)->linger2 = 0;
16483167e12cSChuck Lever 		tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF;
1649b0d93ad5SChuck Lever 
1650b0d93ad5SChuck Lever 		xprt_clear_connected(xprt);
1651b0d93ad5SChuck Lever 
1652b0d93ad5SChuck Lever 		/* Reset to new socket */
1653ee0ac0c2SChuck Lever 		transport->sock = sock;
1654ee0ac0c2SChuck Lever 		transport->inet = sk;
1655b0d93ad5SChuck Lever 
1656b0d93ad5SChuck Lever 		write_unlock_bh(&sk->sk_callback_lock);
1657b0d93ad5SChuck Lever 	}
1658b0d93ad5SChuck Lever 
1659b0d93ad5SChuck Lever 	/* Tell the socket layer to start connecting... */
1660262ca07dSChuck Lever 	xprt->stat.connect_count++;
1661262ca07dSChuck Lever 	xprt->stat.connect_start = jiffies;
166295392c59SChuck Lever 	return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK);
166316be2d20SChuck Lever }
166416be2d20SChuck Lever 
166516be2d20SChuck Lever /**
16669c3d72deSChuck Lever  * xs_tcp_connect_worker4 - connect a TCP socket to a remote endpoint
166716be2d20SChuck Lever  * @work: RPC transport to connect
166816be2d20SChuck Lever  *
166916be2d20SChuck Lever  * Invoked by a work queue tasklet.
167016be2d20SChuck Lever  */
16719c3d72deSChuck Lever static void xs_tcp_connect_worker4(struct work_struct *work)
167216be2d20SChuck Lever {
167316be2d20SChuck Lever 	struct sock_xprt *transport =
167416be2d20SChuck Lever 		container_of(work, struct sock_xprt, connect_worker.work);
167516be2d20SChuck Lever 	struct rpc_xprt *xprt = &transport->xprt;
167616be2d20SChuck Lever 	struct socket *sock = transport->sock;
167716be2d20SChuck Lever 	int err, status = -EIO;
167816be2d20SChuck Lever 
167916be2d20SChuck Lever 	if (xprt->shutdown || !xprt_bound(xprt))
168016be2d20SChuck Lever 		goto out;
168116be2d20SChuck Lever 
168216be2d20SChuck Lever 	if (!sock) {
168316be2d20SChuck Lever 		/* start from scratch */
168416be2d20SChuck Lever 		if ((err = sock_create_kern(PF_INET, SOCK_STREAM, IPPROTO_TCP, &sock)) < 0) {
168516be2d20SChuck Lever 			dprintk("RPC:       can't create TCP transport socket (%d).\n", -err);
168616be2d20SChuck Lever 			goto out;
168716be2d20SChuck Lever 		}
16888945ee5eSChuck Lever 		xs_reclassify_socket4(sock);
168916be2d20SChuck Lever 
169016be2d20SChuck Lever 		if (xs_bind4(transport, sock) < 0) {
169116be2d20SChuck Lever 			sock_release(sock);
169216be2d20SChuck Lever 			goto out;
169316be2d20SChuck Lever 		}
169416be2d20SChuck Lever 	} else
169516be2d20SChuck Lever 		/* "close" the socket, preserving the local port */
169616be2d20SChuck Lever 		xs_tcp_reuse_connection(xprt);
169716be2d20SChuck Lever 
169816be2d20SChuck Lever 	dprintk("RPC:       worker connecting xprt %p to address: %s\n",
169916be2d20SChuck Lever 			xprt, xprt->address_strings[RPC_DISPLAY_ALL]);
170016be2d20SChuck Lever 
170116be2d20SChuck Lever 	status = xs_tcp_finish_connecting(xprt, sock);
1702a246b010SChuck Lever 	dprintk("RPC:       %p connect status %d connected %d sock state %d\n",
170346121cf7SChuck Lever 			xprt, -status, xprt_connected(xprt),
170446121cf7SChuck Lever 			sock->sk->sk_state);
1705a246b010SChuck Lever 	if (status < 0) {
1706a246b010SChuck Lever 		switch (status) {
1707a246b010SChuck Lever 			case -EINPROGRESS:
1708a246b010SChuck Lever 			case -EALREADY:
1709a246b010SChuck Lever 				goto out_clear;
17103167e12cSChuck Lever 			case -ECONNREFUSED:
17113167e12cSChuck Lever 			case -ECONNRESET:
17123167e12cSChuck Lever 				/* retry with existing socket, after a delay */
17133167e12cSChuck Lever 				break;
17143167e12cSChuck Lever 			default:
17153167e12cSChuck Lever 				/* get rid of existing socket, and retry */
1716e06799f9STrond Myklebust 				xs_tcp_shutdown(xprt);
1717a246b010SChuck Lever 		}
1718a246b010SChuck Lever 	}
1719a246b010SChuck Lever out:
172044fbac22SChuck Lever 	xprt_wake_pending_tasks(xprt, status);
1721a246b010SChuck Lever out_clear:
17222226feb6SChuck Lever 	xprt_clear_connecting(xprt);
1723a246b010SChuck Lever }
1724a246b010SChuck Lever 
17259903cd1cSChuck Lever /**
172668e220bdSChuck Lever  * xs_tcp_connect_worker6 - connect a TCP socket to a remote endpoint
172768e220bdSChuck Lever  * @work: RPC transport to connect
172868e220bdSChuck Lever  *
172968e220bdSChuck Lever  * Invoked by a work queue tasklet.
173068e220bdSChuck Lever  */
173168e220bdSChuck Lever static void xs_tcp_connect_worker6(struct work_struct *work)
173268e220bdSChuck Lever {
173368e220bdSChuck Lever 	struct sock_xprt *transport =
173468e220bdSChuck Lever 		container_of(work, struct sock_xprt, connect_worker.work);
173568e220bdSChuck Lever 	struct rpc_xprt *xprt = &transport->xprt;
173668e220bdSChuck Lever 	struct socket *sock = transport->sock;
173768e220bdSChuck Lever 	int err, status = -EIO;
173868e220bdSChuck Lever 
173968e220bdSChuck Lever 	if (xprt->shutdown || !xprt_bound(xprt))
174068e220bdSChuck Lever 		goto out;
174168e220bdSChuck Lever 
174268e220bdSChuck Lever 	if (!sock) {
174368e220bdSChuck Lever 		/* start from scratch */
174468e220bdSChuck Lever 		if ((err = sock_create_kern(PF_INET6, SOCK_STREAM, IPPROTO_TCP, &sock)) < 0) {
174568e220bdSChuck Lever 			dprintk("RPC:       can't create TCP transport socket (%d).\n", -err);
174668e220bdSChuck Lever 			goto out;
174768e220bdSChuck Lever 		}
17488945ee5eSChuck Lever 		xs_reclassify_socket6(sock);
174968e220bdSChuck Lever 
175068e220bdSChuck Lever 		if (xs_bind6(transport, sock) < 0) {
175168e220bdSChuck Lever 			sock_release(sock);
175268e220bdSChuck Lever 			goto out;
175368e220bdSChuck Lever 		}
175468e220bdSChuck Lever 	} else
175568e220bdSChuck Lever 		/* "close" the socket, preserving the local port */
175668e220bdSChuck Lever 		xs_tcp_reuse_connection(xprt);
175768e220bdSChuck Lever 
175868e220bdSChuck Lever 	dprintk("RPC:       worker connecting xprt %p to address: %s\n",
175968e220bdSChuck Lever 			xprt, xprt->address_strings[RPC_DISPLAY_ALL]);
176068e220bdSChuck Lever 
176168e220bdSChuck Lever 	status = xs_tcp_finish_connecting(xprt, sock);
176268e220bdSChuck Lever 	dprintk("RPC:       %p connect status %d connected %d sock state %d\n",
176368e220bdSChuck Lever 			xprt, -status, xprt_connected(xprt), sock->sk->sk_state);
176468e220bdSChuck Lever 	if (status < 0) {
176568e220bdSChuck Lever 		switch (status) {
176668e220bdSChuck Lever 			case -EINPROGRESS:
176768e220bdSChuck Lever 			case -EALREADY:
176868e220bdSChuck Lever 				goto out_clear;
176968e220bdSChuck Lever 			case -ECONNREFUSED:
177068e220bdSChuck Lever 			case -ECONNRESET:
177168e220bdSChuck Lever 				/* retry with existing socket, after a delay */
177268e220bdSChuck Lever 				break;
177368e220bdSChuck Lever 			default:
177468e220bdSChuck Lever 				/* get rid of existing socket, and retry */
1775e06799f9STrond Myklebust 				xs_tcp_shutdown(xprt);
177668e220bdSChuck Lever 		}
177768e220bdSChuck Lever 	}
177868e220bdSChuck Lever out:
177968e220bdSChuck Lever 	xprt_wake_pending_tasks(xprt, status);
178068e220bdSChuck Lever out_clear:
178168e220bdSChuck Lever 	xprt_clear_connecting(xprt);
178268e220bdSChuck Lever }
178368e220bdSChuck Lever 
178468e220bdSChuck Lever /**
17859903cd1cSChuck Lever  * xs_connect - connect a socket to a remote endpoint
17869903cd1cSChuck Lever  * @task: address of RPC task that manages state of connect request
17879903cd1cSChuck Lever  *
17889903cd1cSChuck Lever  * TCP: If the remote end dropped the connection, delay reconnecting.
178903bf4b70SChuck Lever  *
179003bf4b70SChuck Lever  * UDP socket connects are synchronous, but we use a work queue anyway
179103bf4b70SChuck Lever  * to guarantee that even unprivileged user processes can set up a
179203bf4b70SChuck Lever  * socket on a privileged port.
179303bf4b70SChuck Lever  *
179403bf4b70SChuck Lever  * If a UDP socket connect fails, the delay behavior here prevents
179503bf4b70SChuck Lever  * retry floods (hard mounts).
17969903cd1cSChuck Lever  */
17979903cd1cSChuck Lever static void xs_connect(struct rpc_task *task)
1798a246b010SChuck Lever {
1799a246b010SChuck Lever 	struct rpc_xprt *xprt = task->tk_xprt;
1800ee0ac0c2SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1801a246b010SChuck Lever 
1802b0d93ad5SChuck Lever 	if (xprt_test_and_set_connecting(xprt))
1803b0d93ad5SChuck Lever 		return;
1804b0d93ad5SChuck Lever 
1805ee0ac0c2SChuck Lever 	if (transport->sock != NULL) {
180646121cf7SChuck Lever 		dprintk("RPC:       xs_connect delayed xprt %p for %lu "
180746121cf7SChuck Lever 				"seconds\n",
180803bf4b70SChuck Lever 				xprt, xprt->reestablish_timeout / HZ);
1809c1384c9cSTrond Myklebust 		queue_delayed_work(rpciod_workqueue,
1810c1384c9cSTrond Myklebust 				   &transport->connect_worker,
181103bf4b70SChuck Lever 				   xprt->reestablish_timeout);
181203bf4b70SChuck Lever 		xprt->reestablish_timeout <<= 1;
181303bf4b70SChuck Lever 		if (xprt->reestablish_timeout > XS_TCP_MAX_REEST_TO)
181403bf4b70SChuck Lever 			xprt->reestablish_timeout = XS_TCP_MAX_REEST_TO;
18159903cd1cSChuck Lever 	} else {
18169903cd1cSChuck Lever 		dprintk("RPC:       xs_connect scheduled xprt %p\n", xprt);
1817c1384c9cSTrond Myklebust 		queue_delayed_work(rpciod_workqueue,
1818c1384c9cSTrond Myklebust 				   &transport->connect_worker, 0);
1819a246b010SChuck Lever 	}
1820a246b010SChuck Lever }
1821a246b010SChuck Lever 
1822e06799f9STrond Myklebust static void xs_tcp_connect(struct rpc_task *task)
1823e06799f9STrond Myklebust {
1824e06799f9STrond Myklebust 	struct rpc_xprt *xprt = task->tk_xprt;
1825e06799f9STrond Myklebust 
1826e06799f9STrond Myklebust 	/* Initiate graceful shutdown of the socket if not already done */
1827e06799f9STrond Myklebust 	if (test_bit(XPRT_CONNECTED, &xprt->state))
1828e06799f9STrond Myklebust 		xs_tcp_shutdown(xprt);
1829e06799f9STrond Myklebust 	/* Exit if we need to wait for socket shutdown to complete */
1830e06799f9STrond Myklebust 	if (test_bit(XPRT_CLOSING, &xprt->state))
1831e06799f9STrond Myklebust 		return;
1832e06799f9STrond Myklebust 	xs_connect(task);
1833e06799f9STrond Myklebust }
1834e06799f9STrond Myklebust 
1835262ca07dSChuck Lever /**
1836262ca07dSChuck Lever  * xs_udp_print_stats - display UDP socket-specifc stats
1837262ca07dSChuck Lever  * @xprt: rpc_xprt struct containing statistics
1838262ca07dSChuck Lever  * @seq: output file
1839262ca07dSChuck Lever  *
1840262ca07dSChuck Lever  */
1841262ca07dSChuck Lever static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
1842262ca07dSChuck Lever {
1843c8475461SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1844c8475461SChuck Lever 
1845262ca07dSChuck Lever 	seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %Lu %Lu\n",
1846c8475461SChuck Lever 			transport->port,
1847262ca07dSChuck Lever 			xprt->stat.bind_count,
1848262ca07dSChuck Lever 			xprt->stat.sends,
1849262ca07dSChuck Lever 			xprt->stat.recvs,
1850262ca07dSChuck Lever 			xprt->stat.bad_xids,
1851262ca07dSChuck Lever 			xprt->stat.req_u,
1852262ca07dSChuck Lever 			xprt->stat.bklog_u);
1853262ca07dSChuck Lever }
1854262ca07dSChuck Lever 
1855262ca07dSChuck Lever /**
1856262ca07dSChuck Lever  * xs_tcp_print_stats - display TCP socket-specifc stats
1857262ca07dSChuck Lever  * @xprt: rpc_xprt struct containing statistics
1858262ca07dSChuck Lever  * @seq: output file
1859262ca07dSChuck Lever  *
1860262ca07dSChuck Lever  */
1861262ca07dSChuck Lever static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
1862262ca07dSChuck Lever {
1863c8475461SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1864262ca07dSChuck Lever 	long idle_time = 0;
1865262ca07dSChuck Lever 
1866262ca07dSChuck Lever 	if (xprt_connected(xprt))
1867262ca07dSChuck Lever 		idle_time = (long)(jiffies - xprt->last_used) / HZ;
1868262ca07dSChuck Lever 
1869262ca07dSChuck Lever 	seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu %Lu %Lu\n",
1870c8475461SChuck Lever 			transport->port,
1871262ca07dSChuck Lever 			xprt->stat.bind_count,
1872262ca07dSChuck Lever 			xprt->stat.connect_count,
1873262ca07dSChuck Lever 			xprt->stat.connect_time,
1874262ca07dSChuck Lever 			idle_time,
1875262ca07dSChuck Lever 			xprt->stat.sends,
1876262ca07dSChuck Lever 			xprt->stat.recvs,
1877262ca07dSChuck Lever 			xprt->stat.bad_xids,
1878262ca07dSChuck Lever 			xprt->stat.req_u,
1879262ca07dSChuck Lever 			xprt->stat.bklog_u);
1880262ca07dSChuck Lever }
1881262ca07dSChuck Lever 
1882262965f5SChuck Lever static struct rpc_xprt_ops xs_udp_ops = {
188343118c29SChuck Lever 	.set_buffer_size	= xs_udp_set_buffer_size,
188412a80469SChuck Lever 	.reserve_xprt		= xprt_reserve_xprt_cong,
188549e9a890SChuck Lever 	.release_xprt		= xprt_release_xprt_cong,
188645160d62SChuck Lever 	.rpcbind		= rpcb_getport_async,
188792200412SChuck Lever 	.set_port		= xs_set_port,
18889903cd1cSChuck Lever 	.connect		= xs_connect,
188902107148SChuck Lever 	.buf_alloc		= rpc_malloc,
189002107148SChuck Lever 	.buf_free		= rpc_free,
1891262965f5SChuck Lever 	.send_request		= xs_udp_send_request,
1892fe3aca29SChuck Lever 	.set_retrans_timeout	= xprt_set_retrans_timeout_rtt,
189346c0ee8bSChuck Lever 	.timer			= xs_udp_timer,
1894a58dd398SChuck Lever 	.release_request	= xprt_release_rqst_cong,
1895262965f5SChuck Lever 	.close			= xs_close,
1896262965f5SChuck Lever 	.destroy		= xs_destroy,
1897262ca07dSChuck Lever 	.print_stats		= xs_udp_print_stats,
1898262965f5SChuck Lever };
1899262965f5SChuck Lever 
1900262965f5SChuck Lever static struct rpc_xprt_ops xs_tcp_ops = {
190112a80469SChuck Lever 	.reserve_xprt		= xprt_reserve_xprt,
1902e0ab53deSTrond Myklebust 	.release_xprt		= xs_tcp_release_xprt,
190345160d62SChuck Lever 	.rpcbind		= rpcb_getport_async,
190492200412SChuck Lever 	.set_port		= xs_set_port,
1905e06799f9STrond Myklebust 	.connect		= xs_tcp_connect,
190602107148SChuck Lever 	.buf_alloc		= rpc_malloc,
190702107148SChuck Lever 	.buf_free		= rpc_free,
1908262965f5SChuck Lever 	.send_request		= xs_tcp_send_request,
1909fe3aca29SChuck Lever 	.set_retrans_timeout	= xprt_set_retrans_timeout_def,
1910e06799f9STrond Myklebust 	.close			= xs_tcp_shutdown,
19119903cd1cSChuck Lever 	.destroy		= xs_destroy,
1912262ca07dSChuck Lever 	.print_stats		= xs_tcp_print_stats,
1913a246b010SChuck Lever };
1914a246b010SChuck Lever 
19153c341b0bS\"Talpey, Thomas\ static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args,
1916bc25571eS\"Talpey, Thomas\ 				      unsigned int slot_table_size)
1917c8541ecdSChuck Lever {
1918c8541ecdSChuck Lever 	struct rpc_xprt *xprt;
1919ffc2e518SChuck Lever 	struct sock_xprt *new;
1920c8541ecdSChuck Lever 
192196802a09SFrank van Maarseveen 	if (args->addrlen > sizeof(xprt->addr)) {
1922c8541ecdSChuck Lever 		dprintk("RPC:       xs_setup_xprt: address too large\n");
1923c8541ecdSChuck Lever 		return ERR_PTR(-EBADF);
1924c8541ecdSChuck Lever 	}
1925c8541ecdSChuck Lever 
1926ffc2e518SChuck Lever 	new = kzalloc(sizeof(*new), GFP_KERNEL);
1927ffc2e518SChuck Lever 	if (new == NULL) {
192846121cf7SChuck Lever 		dprintk("RPC:       xs_setup_xprt: couldn't allocate "
192946121cf7SChuck Lever 				"rpc_xprt\n");
1930c8541ecdSChuck Lever 		return ERR_PTR(-ENOMEM);
1931c8541ecdSChuck Lever 	}
1932ffc2e518SChuck Lever 	xprt = &new->xprt;
1933c8541ecdSChuck Lever 
1934c8541ecdSChuck Lever 	xprt->max_reqs = slot_table_size;
1935c8541ecdSChuck Lever 	xprt->slot = kcalloc(xprt->max_reqs, sizeof(struct rpc_rqst), GFP_KERNEL);
1936c8541ecdSChuck Lever 	if (xprt->slot == NULL) {
1937c8541ecdSChuck Lever 		kfree(xprt);
193846121cf7SChuck Lever 		dprintk("RPC:       xs_setup_xprt: couldn't allocate slot "
193946121cf7SChuck Lever 				"table\n");
1940c8541ecdSChuck Lever 		return ERR_PTR(-ENOMEM);
1941c8541ecdSChuck Lever 	}
1942c8541ecdSChuck Lever 
194396802a09SFrank van Maarseveen 	memcpy(&xprt->addr, args->dstaddr, args->addrlen);
194496802a09SFrank van Maarseveen 	xprt->addrlen = args->addrlen;
1945d3bc9a1dSFrank van Maarseveen 	if (args->srcaddr)
1946d3bc9a1dSFrank van Maarseveen 		memcpy(&new->addr, args->srcaddr, args->addrlen);
1947c8541ecdSChuck Lever 
1948c8541ecdSChuck Lever 	return xprt;
1949c8541ecdSChuck Lever }
1950c8541ecdSChuck Lever 
19512881ae74STrond Myklebust static const struct rpc_timeout xs_udp_default_timeout = {
19522881ae74STrond Myklebust 	.to_initval = 5 * HZ,
19532881ae74STrond Myklebust 	.to_maxval = 30 * HZ,
19542881ae74STrond Myklebust 	.to_increment = 5 * HZ,
19552881ae74STrond Myklebust 	.to_retries = 5,
19562881ae74STrond Myklebust };
19572881ae74STrond Myklebust 
19589903cd1cSChuck Lever /**
19599903cd1cSChuck Lever  * xs_setup_udp - Set up transport to use a UDP socket
196096802a09SFrank van Maarseveen  * @args: rpc transport creation arguments
19619903cd1cSChuck Lever  *
19629903cd1cSChuck Lever  */
1963483066d6SAdrian Bunk static struct rpc_xprt *xs_setup_udp(struct xprt_create *args)
1964a246b010SChuck Lever {
19658f9d5b1aSChuck Lever 	struct sockaddr *addr = args->dstaddr;
1966c8541ecdSChuck Lever 	struct rpc_xprt *xprt;
1967c8475461SChuck Lever 	struct sock_xprt *transport;
1968a246b010SChuck Lever 
196996802a09SFrank van Maarseveen 	xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries);
1970c8541ecdSChuck Lever 	if (IS_ERR(xprt))
1971c8541ecdSChuck Lever 		return xprt;
1972c8475461SChuck Lever 	transport = container_of(xprt, struct sock_xprt, xprt);
1973a246b010SChuck Lever 
1974ec739ef0SChuck Lever 	xprt->prot = IPPROTO_UDP;
1975808012fbSChuck Lever 	xprt->tsh_size = 0;
1976a246b010SChuck Lever 	/* XXX: header size can vary due to auth type, IPv6, etc. */
1977a246b010SChuck Lever 	xprt->max_payload = (1U << 16) - (MAX_HEADER << 3);
1978a246b010SChuck Lever 
197903bf4b70SChuck Lever 	xprt->bind_timeout = XS_BIND_TO;
198003bf4b70SChuck Lever 	xprt->connect_timeout = XS_UDP_CONN_TO;
198103bf4b70SChuck Lever 	xprt->reestablish_timeout = XS_UDP_REEST_TO;
198203bf4b70SChuck Lever 	xprt->idle_timeout = XS_IDLE_DISC_TO;
1983a246b010SChuck Lever 
1984262965f5SChuck Lever 	xprt->ops = &xs_udp_ops;
1985a246b010SChuck Lever 
1986ba7392bbSTrond Myklebust 	xprt->timeout = &xs_udp_default_timeout;
1987a246b010SChuck Lever 
19888f9d5b1aSChuck Lever 	switch (addr->sa_family) {
19898f9d5b1aSChuck Lever 	case AF_INET:
19908f9d5b1aSChuck Lever 		if (((struct sockaddr_in *)addr)->sin_port != htons(0))
19918f9d5b1aSChuck Lever 			xprt_set_bound(xprt);
19928f9d5b1aSChuck Lever 
19938f9d5b1aSChuck Lever 		INIT_DELAYED_WORK(&transport->connect_worker,
19948f9d5b1aSChuck Lever 					xs_udp_connect_worker4);
1995b454ae90SChuck Lever 		xs_format_ipv4_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP);
19968f9d5b1aSChuck Lever 		break;
19978f9d5b1aSChuck Lever 	case AF_INET6:
19988f9d5b1aSChuck Lever 		if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
19998f9d5b1aSChuck Lever 			xprt_set_bound(xprt);
20008f9d5b1aSChuck Lever 
20018f9d5b1aSChuck Lever 		INIT_DELAYED_WORK(&transport->connect_worker,
20028f9d5b1aSChuck Lever 					xs_udp_connect_worker6);
2003b454ae90SChuck Lever 		xs_format_ipv6_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6);
20048f9d5b1aSChuck Lever 		break;
20058f9d5b1aSChuck Lever 	default:
20068f9d5b1aSChuck Lever 		kfree(xprt);
20078f9d5b1aSChuck Lever 		return ERR_PTR(-EAFNOSUPPORT);
20088f9d5b1aSChuck Lever 	}
20098f9d5b1aSChuck Lever 
2010edb267a6SChuck Lever 	dprintk("RPC:       set up transport to address %s\n",
20117559c7a2SChuck Lever 			xprt->address_strings[RPC_DISPLAY_ALL]);
2012edb267a6SChuck Lever 
2013bc25571eS\"Talpey, Thomas\ 	if (try_module_get(THIS_MODULE))
2014c8541ecdSChuck Lever 		return xprt;
2015bc25571eS\"Talpey, Thomas\ 
2016bc25571eS\"Talpey, Thomas\ 	kfree(xprt->slot);
2017bc25571eS\"Talpey, Thomas\ 	kfree(xprt);
2018bc25571eS\"Talpey, Thomas\ 	return ERR_PTR(-EINVAL);
2019a246b010SChuck Lever }
2020a246b010SChuck Lever 
20212881ae74STrond Myklebust static const struct rpc_timeout xs_tcp_default_timeout = {
20222881ae74STrond Myklebust 	.to_initval = 60 * HZ,
20232881ae74STrond Myklebust 	.to_maxval = 60 * HZ,
20242881ae74STrond Myklebust 	.to_retries = 2,
20252881ae74STrond Myklebust };
20262881ae74STrond Myklebust 
20279903cd1cSChuck Lever /**
20289903cd1cSChuck Lever  * xs_setup_tcp - Set up transport to use a TCP socket
202996802a09SFrank van Maarseveen  * @args: rpc transport creation arguments
20309903cd1cSChuck Lever  *
20319903cd1cSChuck Lever  */
2032483066d6SAdrian Bunk static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args)
2033a246b010SChuck Lever {
20348f9d5b1aSChuck Lever 	struct sockaddr *addr = args->dstaddr;
2035c8541ecdSChuck Lever 	struct rpc_xprt *xprt;
2036c8475461SChuck Lever 	struct sock_xprt *transport;
2037a246b010SChuck Lever 
203896802a09SFrank van Maarseveen 	xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries);
2039c8541ecdSChuck Lever 	if (IS_ERR(xprt))
2040c8541ecdSChuck Lever 		return xprt;
2041c8475461SChuck Lever 	transport = container_of(xprt, struct sock_xprt, xprt);
2042a246b010SChuck Lever 
2043ec739ef0SChuck Lever 	xprt->prot = IPPROTO_TCP;
2044808012fbSChuck Lever 	xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
2045808012fbSChuck Lever 	xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2046a246b010SChuck Lever 
204703bf4b70SChuck Lever 	xprt->bind_timeout = XS_BIND_TO;
204803bf4b70SChuck Lever 	xprt->connect_timeout = XS_TCP_CONN_TO;
204903bf4b70SChuck Lever 	xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
205003bf4b70SChuck Lever 	xprt->idle_timeout = XS_IDLE_DISC_TO;
2051a246b010SChuck Lever 
2052262965f5SChuck Lever 	xprt->ops = &xs_tcp_ops;
2053ba7392bbSTrond Myklebust 	xprt->timeout = &xs_tcp_default_timeout;
2054a246b010SChuck Lever 
20558f9d5b1aSChuck Lever 	switch (addr->sa_family) {
20568f9d5b1aSChuck Lever 	case AF_INET:
20578f9d5b1aSChuck Lever 		if (((struct sockaddr_in *)addr)->sin_port != htons(0))
20588f9d5b1aSChuck Lever 			xprt_set_bound(xprt);
20598f9d5b1aSChuck Lever 
20608f9d5b1aSChuck Lever 		INIT_DELAYED_WORK(&transport->connect_worker, xs_tcp_connect_worker4);
2061b454ae90SChuck Lever 		xs_format_ipv4_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP);
20628f9d5b1aSChuck Lever 		break;
20638f9d5b1aSChuck Lever 	case AF_INET6:
20648f9d5b1aSChuck Lever 		if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
20658f9d5b1aSChuck Lever 			xprt_set_bound(xprt);
20668f9d5b1aSChuck Lever 
20678f9d5b1aSChuck Lever 		INIT_DELAYED_WORK(&transport->connect_worker, xs_tcp_connect_worker6);
2068b454ae90SChuck Lever 		xs_format_ipv6_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6);
20698f9d5b1aSChuck Lever 		break;
20708f9d5b1aSChuck Lever 	default:
20718f9d5b1aSChuck Lever 		kfree(xprt);
20728f9d5b1aSChuck Lever 		return ERR_PTR(-EAFNOSUPPORT);
20738f9d5b1aSChuck Lever 	}
20748f9d5b1aSChuck Lever 
2075edb267a6SChuck Lever 	dprintk("RPC:       set up transport to address %s\n",
20767559c7a2SChuck Lever 			xprt->address_strings[RPC_DISPLAY_ALL]);
2077edb267a6SChuck Lever 
2078bc25571eS\"Talpey, Thomas\ 	if (try_module_get(THIS_MODULE))
2079c8541ecdSChuck Lever 		return xprt;
2080bc25571eS\"Talpey, Thomas\ 
2081bc25571eS\"Talpey, Thomas\ 	kfree(xprt->slot);
2082bc25571eS\"Talpey, Thomas\ 	kfree(xprt);
2083bc25571eS\"Talpey, Thomas\ 	return ERR_PTR(-EINVAL);
2084a246b010SChuck Lever }
2085282b32e1SChuck Lever 
2086bc25571eS\"Talpey, Thomas\ static struct xprt_class	xs_udp_transport = {
2087bc25571eS\"Talpey, Thomas\ 	.list		= LIST_HEAD_INIT(xs_udp_transport.list),
2088bc25571eS\"Talpey, Thomas\ 	.name		= "udp",
2089bc25571eS\"Talpey, Thomas\ 	.owner		= THIS_MODULE,
20904fa016ebS\"Talpey, Thomas\ 	.ident		= IPPROTO_UDP,
2091bc25571eS\"Talpey, Thomas\ 	.setup		= xs_setup_udp,
2092bc25571eS\"Talpey, Thomas\ };
2093bc25571eS\"Talpey, Thomas\ 
2094bc25571eS\"Talpey, Thomas\ static struct xprt_class	xs_tcp_transport = {
2095bc25571eS\"Talpey, Thomas\ 	.list		= LIST_HEAD_INIT(xs_tcp_transport.list),
2096bc25571eS\"Talpey, Thomas\ 	.name		= "tcp",
2097bc25571eS\"Talpey, Thomas\ 	.owner		= THIS_MODULE,
20984fa016ebS\"Talpey, Thomas\ 	.ident		= IPPROTO_TCP,
2099bc25571eS\"Talpey, Thomas\ 	.setup		= xs_setup_tcp,
2100bc25571eS\"Talpey, Thomas\ };
2101bc25571eS\"Talpey, Thomas\ 
2102282b32e1SChuck Lever /**
2103bc25571eS\"Talpey, Thomas\  * init_socket_xprt - set up xprtsock's sysctls, register with RPC client
2104282b32e1SChuck Lever  *
2105282b32e1SChuck Lever  */
2106282b32e1SChuck Lever int init_socket_xprt(void)
2107282b32e1SChuck Lever {
2108fbf76683SChuck Lever #ifdef RPC_DEBUG
21092b1bec5fSEric W. Biederman 	if (!sunrpc_table_header)
21100b4d4147SEric W. Biederman 		sunrpc_table_header = register_sysctl_table(sunrpc_table);
2111fbf76683SChuck Lever #endif
2112fbf76683SChuck Lever 
2113bc25571eS\"Talpey, Thomas\ 	xprt_register_transport(&xs_udp_transport);
2114bc25571eS\"Talpey, Thomas\ 	xprt_register_transport(&xs_tcp_transport);
2115bc25571eS\"Talpey, Thomas\ 
2116282b32e1SChuck Lever 	return 0;
2117282b32e1SChuck Lever }
2118282b32e1SChuck Lever 
2119282b32e1SChuck Lever /**
2120bc25571eS\"Talpey, Thomas\  * cleanup_socket_xprt - remove xprtsock's sysctls, unregister
2121282b32e1SChuck Lever  *
2122282b32e1SChuck Lever  */
2123282b32e1SChuck Lever void cleanup_socket_xprt(void)
2124282b32e1SChuck Lever {
2125fbf76683SChuck Lever #ifdef RPC_DEBUG
2126fbf76683SChuck Lever 	if (sunrpc_table_header) {
2127fbf76683SChuck Lever 		unregister_sysctl_table(sunrpc_table_header);
2128fbf76683SChuck Lever 		sunrpc_table_header = NULL;
2129fbf76683SChuck Lever 	}
2130fbf76683SChuck Lever #endif
2131bc25571eS\"Talpey, Thomas\ 
2132bc25571eS\"Talpey, Thomas\ 	xprt_unregister_transport(&xs_udp_transport);
2133bc25571eS\"Talpey, Thomas\ 	xprt_unregister_transport(&xs_tcp_transport);
2134282b32e1SChuck Lever }
2135