xref: /openbmc/linux/net/sunrpc/xprtsock.c (revision c877b849d302d1275452af80b7221a2555dc02e1)
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>
3744b98efdSRicardo Labiaga #ifdef CONFIG_NFS_V4_1
3844b98efdSRicardo Labiaga #include <linux/sunrpc/bc_xprt.h>
3944b98efdSRicardo Labiaga #endif
40a246b010SChuck Lever 
41a246b010SChuck Lever #include <net/sock.h>
42a246b010SChuck Lever #include <net/checksum.h>
43a246b010SChuck Lever #include <net/udp.h>
44a246b010SChuck Lever #include <net/tcp.h>
45a246b010SChuck Lever 
469903cd1cSChuck Lever /*
47c556b754SChuck Lever  * xprtsock tunables
48c556b754SChuck Lever  */
49c556b754SChuck Lever unsigned int xprt_udp_slot_table_entries = RPC_DEF_SLOT_TABLE;
50c556b754SChuck Lever unsigned int xprt_tcp_slot_table_entries = RPC_DEF_SLOT_TABLE;
51c556b754SChuck Lever 
52c556b754SChuck Lever unsigned int xprt_min_resvport = RPC_DEF_MIN_RESVPORT;
53c556b754SChuck Lever unsigned int xprt_max_resvport = RPC_DEF_MAX_RESVPORT;
54c556b754SChuck Lever 
557d1e8255STrond Myklebust #define XS_TCP_LINGER_TO	(15U * HZ)
5625fe6142STrond Myklebust static unsigned int xs_tcp_fin_timeout __read_mostly = XS_TCP_LINGER_TO;
577d1e8255STrond Myklebust 
58c556b754SChuck Lever /*
59fbf76683SChuck Lever  * We can register our own files under /proc/sys/sunrpc by
60fbf76683SChuck Lever  * calling register_sysctl_table() again.  The files in that
61fbf76683SChuck Lever  * directory become the union of all files registered there.
62fbf76683SChuck Lever  *
63fbf76683SChuck Lever  * We simply need to make sure that we don't collide with
64fbf76683SChuck Lever  * someone else's file names!
65fbf76683SChuck Lever  */
66fbf76683SChuck Lever 
67fbf76683SChuck Lever #ifdef RPC_DEBUG
68fbf76683SChuck Lever 
69fbf76683SChuck Lever static unsigned int min_slot_table_size = RPC_MIN_SLOT_TABLE;
70fbf76683SChuck Lever static unsigned int max_slot_table_size = RPC_MAX_SLOT_TABLE;
71fbf76683SChuck Lever static unsigned int xprt_min_resvport_limit = RPC_MIN_RESVPORT;
72fbf76683SChuck Lever static unsigned int xprt_max_resvport_limit = RPC_MAX_RESVPORT;
73fbf76683SChuck Lever 
74fbf76683SChuck Lever static struct ctl_table_header *sunrpc_table_header;
75fbf76683SChuck Lever 
76fbf76683SChuck Lever /*
77fbf76683SChuck Lever  * FIXME: changing the UDP slot table size should also resize the UDP
78fbf76683SChuck Lever  *        socket buffers for existing UDP transports
79fbf76683SChuck Lever  */
80fbf76683SChuck Lever static ctl_table xs_tunables_table[] = {
81fbf76683SChuck Lever 	{
82fbf76683SChuck Lever 		.ctl_name	= CTL_SLOTTABLE_UDP,
83fbf76683SChuck Lever 		.procname	= "udp_slot_table_entries",
84fbf76683SChuck Lever 		.data		= &xprt_udp_slot_table_entries,
85fbf76683SChuck Lever 		.maxlen		= sizeof(unsigned int),
86fbf76683SChuck Lever 		.mode		= 0644,
87fbf76683SChuck Lever 		.proc_handler	= &proc_dointvec_minmax,
88fbf76683SChuck Lever 		.strategy	= &sysctl_intvec,
89fbf76683SChuck Lever 		.extra1		= &min_slot_table_size,
90fbf76683SChuck Lever 		.extra2		= &max_slot_table_size
91fbf76683SChuck Lever 	},
92fbf76683SChuck Lever 	{
93fbf76683SChuck Lever 		.ctl_name	= CTL_SLOTTABLE_TCP,
94fbf76683SChuck Lever 		.procname	= "tcp_slot_table_entries",
95fbf76683SChuck Lever 		.data		= &xprt_tcp_slot_table_entries,
96fbf76683SChuck Lever 		.maxlen		= sizeof(unsigned int),
97fbf76683SChuck Lever 		.mode		= 0644,
98fbf76683SChuck Lever 		.proc_handler	= &proc_dointvec_minmax,
99fbf76683SChuck Lever 		.strategy	= &sysctl_intvec,
100fbf76683SChuck Lever 		.extra1		= &min_slot_table_size,
101fbf76683SChuck Lever 		.extra2		= &max_slot_table_size
102fbf76683SChuck Lever 	},
103fbf76683SChuck Lever 	{
104fbf76683SChuck Lever 		.ctl_name	= CTL_MIN_RESVPORT,
105fbf76683SChuck Lever 		.procname	= "min_resvport",
106fbf76683SChuck Lever 		.data		= &xprt_min_resvport,
107fbf76683SChuck Lever 		.maxlen		= sizeof(unsigned int),
108fbf76683SChuck Lever 		.mode		= 0644,
109fbf76683SChuck Lever 		.proc_handler	= &proc_dointvec_minmax,
110fbf76683SChuck Lever 		.strategy	= &sysctl_intvec,
111fbf76683SChuck Lever 		.extra1		= &xprt_min_resvport_limit,
112fbf76683SChuck Lever 		.extra2		= &xprt_max_resvport_limit
113fbf76683SChuck Lever 	},
114fbf76683SChuck Lever 	{
115fbf76683SChuck Lever 		.ctl_name	= CTL_MAX_RESVPORT,
116fbf76683SChuck Lever 		.procname	= "max_resvport",
117fbf76683SChuck Lever 		.data		= &xprt_max_resvport,
118fbf76683SChuck Lever 		.maxlen		= sizeof(unsigned int),
119fbf76683SChuck Lever 		.mode		= 0644,
120fbf76683SChuck Lever 		.proc_handler	= &proc_dointvec_minmax,
121fbf76683SChuck Lever 		.strategy	= &sysctl_intvec,
122fbf76683SChuck Lever 		.extra1		= &xprt_min_resvport_limit,
123fbf76683SChuck Lever 		.extra2		= &xprt_max_resvport_limit
124fbf76683SChuck Lever 	},
125fbf76683SChuck Lever 	{
12625fe6142STrond Myklebust 		.procname	= "tcp_fin_timeout",
12725fe6142STrond Myklebust 		.data		= &xs_tcp_fin_timeout,
12825fe6142STrond Myklebust 		.maxlen		= sizeof(xs_tcp_fin_timeout),
12925fe6142STrond Myklebust 		.mode		= 0644,
13025fe6142STrond Myklebust 		.proc_handler	= &proc_dointvec_jiffies,
13125fe6142STrond Myklebust 		.strategy	= sysctl_jiffies
13225fe6142STrond Myklebust 	},
13325fe6142STrond Myklebust 	{
134fbf76683SChuck Lever 		.ctl_name = 0,
135fbf76683SChuck Lever 	},
136fbf76683SChuck Lever };
137fbf76683SChuck Lever 
138fbf76683SChuck Lever static ctl_table sunrpc_table[] = {
139fbf76683SChuck Lever 	{
140fbf76683SChuck Lever 		.ctl_name	= CTL_SUNRPC,
141fbf76683SChuck Lever 		.procname	= "sunrpc",
142fbf76683SChuck Lever 		.mode		= 0555,
143fbf76683SChuck Lever 		.child		= xs_tunables_table
144fbf76683SChuck Lever 	},
145fbf76683SChuck Lever 	{
146fbf76683SChuck Lever 		.ctl_name = 0,
147fbf76683SChuck Lever 	},
148fbf76683SChuck Lever };
149fbf76683SChuck Lever 
150fbf76683SChuck Lever #endif
151fbf76683SChuck Lever 
152fbf76683SChuck Lever /*
15303bf4b70SChuck Lever  * Time out for an RPC UDP socket connect.  UDP socket connects are
15403bf4b70SChuck Lever  * synchronous, but we set a timeout anyway in case of resource
15503bf4b70SChuck Lever  * exhaustion on the local host.
15603bf4b70SChuck Lever  */
15703bf4b70SChuck Lever #define XS_UDP_CONN_TO		(5U * HZ)
15803bf4b70SChuck Lever 
15903bf4b70SChuck Lever /*
16003bf4b70SChuck Lever  * Wait duration for an RPC TCP connection to be established.  Solaris
16103bf4b70SChuck Lever  * NFS over TCP uses 60 seconds, for example, which is in line with how
16203bf4b70SChuck Lever  * long a server takes to reboot.
16303bf4b70SChuck Lever  */
16403bf4b70SChuck Lever #define XS_TCP_CONN_TO		(60U * HZ)
16503bf4b70SChuck Lever 
16603bf4b70SChuck Lever /*
16703bf4b70SChuck Lever  * Wait duration for a reply from the RPC portmapper.
16803bf4b70SChuck Lever  */
16903bf4b70SChuck Lever #define XS_BIND_TO		(60U * HZ)
17003bf4b70SChuck Lever 
17103bf4b70SChuck Lever /*
17203bf4b70SChuck Lever  * Delay if a UDP socket connect error occurs.  This is most likely some
17303bf4b70SChuck Lever  * kind of resource problem on the local host.
17403bf4b70SChuck Lever  */
17503bf4b70SChuck Lever #define XS_UDP_REEST_TO		(2U * HZ)
17603bf4b70SChuck Lever 
17703bf4b70SChuck Lever /*
17803bf4b70SChuck Lever  * The reestablish timeout allows clients to delay for a bit before attempting
17903bf4b70SChuck Lever  * to reconnect to a server that just dropped our connection.
18003bf4b70SChuck Lever  *
18103bf4b70SChuck Lever  * We implement an exponential backoff when trying to reestablish a TCP
18203bf4b70SChuck Lever  * transport connection with the server.  Some servers like to drop a TCP
18303bf4b70SChuck Lever  * connection when they are overworked, so we start with a short timeout and
18403bf4b70SChuck Lever  * increase over time if the server is down or not responding.
18503bf4b70SChuck Lever  */
18603bf4b70SChuck Lever #define XS_TCP_INIT_REEST_TO	(3U * HZ)
18703bf4b70SChuck Lever #define XS_TCP_MAX_REEST_TO	(5U * 60 * HZ)
18803bf4b70SChuck Lever 
18903bf4b70SChuck Lever /*
19003bf4b70SChuck Lever  * TCP idle timeout; client drops the transport socket if it is idle
19103bf4b70SChuck Lever  * for this long.  Note that we also timeout UDP sockets to prevent
19203bf4b70SChuck Lever  * holding port numbers when there is no RPC traffic.
19303bf4b70SChuck Lever  */
19403bf4b70SChuck Lever #define XS_IDLE_DISC_TO		(5U * 60 * HZ)
19503bf4b70SChuck Lever 
196a246b010SChuck Lever #ifdef RPC_DEBUG
197a246b010SChuck Lever # undef  RPC_DEBUG_DATA
1989903cd1cSChuck Lever # define RPCDBG_FACILITY	RPCDBG_TRANS
199a246b010SChuck Lever #endif
200a246b010SChuck Lever 
201a246b010SChuck Lever #ifdef RPC_DEBUG_DATA
2029903cd1cSChuck Lever static void xs_pktdump(char *msg, u32 *packet, unsigned int count)
203a246b010SChuck Lever {
204a246b010SChuck Lever 	u8 *buf = (u8 *) packet;
205a246b010SChuck Lever 	int j;
206a246b010SChuck Lever 
207a246b010SChuck Lever 	dprintk("RPC:       %s\n", msg);
208a246b010SChuck Lever 	for (j = 0; j < count && j < 128; j += 4) {
209a246b010SChuck Lever 		if (!(j & 31)) {
210a246b010SChuck Lever 			if (j)
211a246b010SChuck Lever 				dprintk("\n");
212a246b010SChuck Lever 			dprintk("0x%04x ", j);
213a246b010SChuck Lever 		}
214a246b010SChuck Lever 		dprintk("%02x%02x%02x%02x ",
215a246b010SChuck Lever 			buf[j], buf[j+1], buf[j+2], buf[j+3]);
216a246b010SChuck Lever 	}
217a246b010SChuck Lever 	dprintk("\n");
218a246b010SChuck Lever }
219a246b010SChuck Lever #else
2209903cd1cSChuck Lever static inline void xs_pktdump(char *msg, u32 *packet, unsigned int count)
221a246b010SChuck Lever {
222a246b010SChuck Lever 	/* NOP */
223a246b010SChuck Lever }
224a246b010SChuck Lever #endif
225a246b010SChuck Lever 
226ffc2e518SChuck Lever struct sock_xprt {
227ffc2e518SChuck Lever 	struct rpc_xprt		xprt;
228ee0ac0c2SChuck Lever 
229ee0ac0c2SChuck Lever 	/*
230ee0ac0c2SChuck Lever 	 * Network layer
231ee0ac0c2SChuck Lever 	 */
232ee0ac0c2SChuck Lever 	struct socket *		sock;
233ee0ac0c2SChuck Lever 	struct sock *		inet;
23451971139SChuck Lever 
23551971139SChuck Lever 	/*
23651971139SChuck Lever 	 * State of TCP reply receive
23751971139SChuck Lever 	 */
23851971139SChuck Lever 	__be32			tcp_fraghdr,
23951971139SChuck Lever 				tcp_xid;
24051971139SChuck Lever 
24151971139SChuck Lever 	u32			tcp_offset,
24251971139SChuck Lever 				tcp_reclen;
24351971139SChuck Lever 
24451971139SChuck Lever 	unsigned long		tcp_copied,
24551971139SChuck Lever 				tcp_flags;
246c8475461SChuck Lever 
247c8475461SChuck Lever 	/*
248c8475461SChuck Lever 	 * Connection of transports
249c8475461SChuck Lever 	 */
25034161db6STrond Myklebust 	struct delayed_work	connect_worker;
251d3bc9a1dSFrank van Maarseveen 	struct sockaddr_storage	addr;
252c8475461SChuck Lever 	unsigned short		port;
2537c6e066eSChuck Lever 
2547c6e066eSChuck Lever 	/*
2557c6e066eSChuck Lever 	 * UDP socket buffer size parameters
2567c6e066eSChuck Lever 	 */
2577c6e066eSChuck Lever 	size_t			rcvsize,
2587c6e066eSChuck Lever 				sndsize;
259314dfd79SChuck Lever 
260314dfd79SChuck Lever 	/*
261314dfd79SChuck Lever 	 * Saved socket callback addresses
262314dfd79SChuck Lever 	 */
263314dfd79SChuck Lever 	void			(*old_data_ready)(struct sock *, int);
264314dfd79SChuck Lever 	void			(*old_state_change)(struct sock *);
265314dfd79SChuck Lever 	void			(*old_write_space)(struct sock *);
2662a9e1cfaSTrond Myklebust 	void			(*old_error_report)(struct sock *);
267ffc2e518SChuck Lever };
268ffc2e518SChuck Lever 
269e136d092SChuck Lever /*
270e136d092SChuck Lever  * TCP receive state flags
271e136d092SChuck Lever  */
272e136d092SChuck Lever #define TCP_RCV_LAST_FRAG	(1UL << 0)
273e136d092SChuck Lever #define TCP_RCV_COPY_FRAGHDR	(1UL << 1)
274e136d092SChuck Lever #define TCP_RCV_COPY_XID	(1UL << 2)
275e136d092SChuck Lever #define TCP_RCV_COPY_DATA	(1UL << 3)
276f4a2e418SRicardo Labiaga #define TCP_RCV_READ_CALLDIR	(1UL << 4)
277f4a2e418SRicardo Labiaga #define TCP_RCV_COPY_CALLDIR	(1UL << 5)
27818dca02aSRicardo Labiaga 
27918dca02aSRicardo Labiaga /*
28018dca02aSRicardo Labiaga  * TCP RPC flags
28118dca02aSRicardo Labiaga  */
282f4a2e418SRicardo Labiaga #define TCP_RPC_REPLY		(1UL << 6)
283e136d092SChuck Lever 
28495392c59SChuck Lever static inline struct sockaddr *xs_addr(struct rpc_xprt *xprt)
285edb267a6SChuck Lever {
28695392c59SChuck Lever 	return (struct sockaddr *) &xprt->addr;
28795392c59SChuck Lever }
28895392c59SChuck Lever 
28995392c59SChuck Lever static inline struct sockaddr_in *xs_addr_in(struct rpc_xprt *xprt)
29095392c59SChuck Lever {
29195392c59SChuck Lever 	return (struct sockaddr_in *) &xprt->addr;
29295392c59SChuck Lever }
29395392c59SChuck Lever 
29495392c59SChuck Lever static inline struct sockaddr_in6 *xs_addr_in6(struct rpc_xprt *xprt)
29595392c59SChuck Lever {
29695392c59SChuck Lever 	return (struct sockaddr_in6 *) &xprt->addr;
29795392c59SChuck Lever }
29895392c59SChuck Lever 
299*c877b849SChuck Lever static void xs_format_common_peer_addresses(struct rpc_xprt *xprt)
300*c877b849SChuck Lever {
301*c877b849SChuck Lever 	struct sockaddr *sap = xs_addr(xprt);
302*c877b849SChuck Lever 	char buf[128];
303*c877b849SChuck Lever 
304*c877b849SChuck Lever 	(void)rpc_ntop(sap, buf, sizeof(buf));
305*c877b849SChuck Lever 	xprt->address_strings[RPC_DISPLAY_ADDR] = kstrdup(buf, GFP_KERNEL);
306*c877b849SChuck Lever 
307*c877b849SChuck Lever 	(void)snprintf(buf, sizeof(buf), "%u", rpc_get_port(sap));
308*c877b849SChuck Lever 	xprt->address_strings[RPC_DISPLAY_PORT] = kstrdup(buf, GFP_KERNEL);
309*c877b849SChuck Lever 
310*c877b849SChuck Lever 	(void)snprintf(buf, sizeof(buf), "addr=%s port=%s proto=%s",
311*c877b849SChuck Lever 			xprt->address_strings[RPC_DISPLAY_ADDR],
312*c877b849SChuck Lever 			xprt->address_strings[RPC_DISPLAY_PORT],
313*c877b849SChuck Lever 			xprt->address_strings[RPC_DISPLAY_PROTO]);
314*c877b849SChuck Lever 	xprt->address_strings[RPC_DISPLAY_ALL] = kstrdup(buf, GFP_KERNEL);
315*c877b849SChuck Lever 
316*c877b849SChuck Lever 	(void)snprintf(buf, sizeof(buf), "%4hx", rpc_get_port(sap));
317*c877b849SChuck Lever 	xprt->address_strings[RPC_DISPLAY_HEX_PORT] = kstrdup(buf, GFP_KERNEL);
318*c877b849SChuck Lever }
319*c877b849SChuck Lever 
320b454ae90SChuck Lever static void xs_format_ipv4_peer_addresses(struct rpc_xprt *xprt,
321b454ae90SChuck Lever 					  const char *protocol,
322b454ae90SChuck Lever 					  const char *netid)
323edb267a6SChuck Lever {
324*c877b849SChuck Lever 	struct sockaddr_in *sin = xs_addr_in(xprt);
325*c877b849SChuck Lever 	char buf[16];
326edb267a6SChuck Lever 
327b454ae90SChuck Lever 	xprt->address_strings[RPC_DISPLAY_PROTO] = protocol;
328b454ae90SChuck Lever 	xprt->address_strings[RPC_DISPLAY_NETID] = netid;
329*c877b849SChuck Lever 
330*c877b849SChuck Lever 	(void)snprintf(buf, sizeof(buf), "%02x%02x%02x%02x",
331*c877b849SChuck Lever 				NIPQUAD(sin->sin_addr.s_addr));
332*c877b849SChuck Lever 	xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = kstrdup(buf, GFP_KERNEL);
333*c877b849SChuck Lever 
334*c877b849SChuck Lever 	xs_format_common_peer_addresses(xprt);
335edb267a6SChuck Lever }
336edb267a6SChuck Lever 
337b454ae90SChuck Lever static void xs_format_ipv6_peer_addresses(struct rpc_xprt *xprt,
338b454ae90SChuck Lever 					  const char *protocol,
339b454ae90SChuck Lever 					  const char *netid)
3404b6473fbSChuck Lever {
341*c877b849SChuck Lever 	struct sockaddr_in6 *sin6 = xs_addr_in6(xprt);
342*c877b849SChuck Lever 	char buf[48];
3434b6473fbSChuck Lever 
344b454ae90SChuck Lever 	xprt->address_strings[RPC_DISPLAY_PROTO] = protocol;
345b454ae90SChuck Lever 	xprt->address_strings[RPC_DISPLAY_NETID] = netid;
346*c877b849SChuck Lever 
347*c877b849SChuck Lever 	(void)snprintf(buf, sizeof(buf), "%pi6", &sin6->sin6_addr);
348*c877b849SChuck Lever 	xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = kstrdup(buf, GFP_KERNEL);
349*c877b849SChuck Lever 
350*c877b849SChuck Lever 	xs_format_common_peer_addresses(xprt);
351edb267a6SChuck Lever }
352edb267a6SChuck Lever 
353edb267a6SChuck Lever static void xs_free_peer_addresses(struct rpc_xprt *xprt)
354edb267a6SChuck Lever {
35533e01dc7SChuck Lever 	unsigned int i;
35633e01dc7SChuck Lever 
35733e01dc7SChuck Lever 	for (i = 0; i < RPC_DISPLAY_MAX; i++)
35833e01dc7SChuck Lever 		switch (i) {
35933e01dc7SChuck Lever 		case RPC_DISPLAY_PROTO:
36033e01dc7SChuck Lever 		case RPC_DISPLAY_NETID:
36133e01dc7SChuck Lever 			continue;
36233e01dc7SChuck Lever 		default:
36333e01dc7SChuck Lever 			kfree(xprt->address_strings[i]);
36433e01dc7SChuck Lever 		}
365edb267a6SChuck Lever }
366edb267a6SChuck Lever 
367b4b5cc85SChuck Lever #define XS_SENDMSG_FLAGS	(MSG_DONTWAIT | MSG_NOSIGNAL)
368b4b5cc85SChuck Lever 
36924c5684bSTrond Myklebust static int xs_send_kvec(struct socket *sock, struct sockaddr *addr, int addrlen, struct kvec *vec, unsigned int base, int more)
370b4b5cc85SChuck Lever {
371b4b5cc85SChuck Lever 	struct msghdr msg = {
372b4b5cc85SChuck Lever 		.msg_name	= addr,
373b4b5cc85SChuck Lever 		.msg_namelen	= addrlen,
37424c5684bSTrond Myklebust 		.msg_flags	= XS_SENDMSG_FLAGS | (more ? MSG_MORE : 0),
37524c5684bSTrond Myklebust 	};
37624c5684bSTrond Myklebust 	struct kvec iov = {
37724c5684bSTrond Myklebust 		.iov_base	= vec->iov_base + base,
37824c5684bSTrond Myklebust 		.iov_len	= vec->iov_len - base,
379b4b5cc85SChuck Lever 	};
380b4b5cc85SChuck Lever 
38124c5684bSTrond Myklebust 	if (iov.iov_len != 0)
382b4b5cc85SChuck Lever 		return kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len);
383b4b5cc85SChuck Lever 	return kernel_sendmsg(sock, &msg, NULL, 0, 0);
384b4b5cc85SChuck Lever }
385b4b5cc85SChuck Lever 
38624c5684bSTrond Myklebust static int xs_send_pagedata(struct socket *sock, struct xdr_buf *xdr, unsigned int base, int more)
387b4b5cc85SChuck Lever {
38824c5684bSTrond Myklebust 	struct page **ppage;
38924c5684bSTrond Myklebust 	unsigned int remainder;
39024c5684bSTrond Myklebust 	int err, sent = 0;
391b4b5cc85SChuck Lever 
39224c5684bSTrond Myklebust 	remainder = xdr->page_len - base;
39324c5684bSTrond Myklebust 	base += xdr->page_base;
39424c5684bSTrond Myklebust 	ppage = xdr->pages + (base >> PAGE_SHIFT);
39524c5684bSTrond Myklebust 	base &= ~PAGE_MASK;
39624c5684bSTrond Myklebust 	for(;;) {
39724c5684bSTrond Myklebust 		unsigned int len = min_t(unsigned int, PAGE_SIZE - base, remainder);
39824c5684bSTrond Myklebust 		int flags = XS_SENDMSG_FLAGS;
39924c5684bSTrond Myklebust 
40024c5684bSTrond Myklebust 		remainder -= len;
40124c5684bSTrond Myklebust 		if (remainder != 0 || more)
40224c5684bSTrond Myklebust 			flags |= MSG_MORE;
40324c5684bSTrond Myklebust 		err = sock->ops->sendpage(sock, *ppage, base, len, flags);
40424c5684bSTrond Myklebust 		if (remainder == 0 || err != len)
40524c5684bSTrond Myklebust 			break;
40624c5684bSTrond Myklebust 		sent += err;
40724c5684bSTrond Myklebust 		ppage++;
40824c5684bSTrond Myklebust 		base = 0;
40924c5684bSTrond Myklebust 	}
41024c5684bSTrond Myklebust 	if (sent == 0)
41124c5684bSTrond Myklebust 		return err;
41224c5684bSTrond Myklebust 	if (err > 0)
41324c5684bSTrond Myklebust 		sent += err;
41424c5684bSTrond Myklebust 	return sent;
415b4b5cc85SChuck Lever }
416b4b5cc85SChuck Lever 
4179903cd1cSChuck Lever /**
4189903cd1cSChuck Lever  * xs_sendpages - write pages directly to a socket
4199903cd1cSChuck Lever  * @sock: socket to send on
4209903cd1cSChuck Lever  * @addr: UDP only -- address of destination
4219903cd1cSChuck Lever  * @addrlen: UDP only -- length of destination address
4229903cd1cSChuck Lever  * @xdr: buffer containing this request
4239903cd1cSChuck Lever  * @base: starting position in the buffer
4249903cd1cSChuck Lever  *
425a246b010SChuck Lever  */
42624c5684bSTrond Myklebust static int xs_sendpages(struct socket *sock, struct sockaddr *addr, int addrlen, struct xdr_buf *xdr, unsigned int base)
427a246b010SChuck Lever {
42824c5684bSTrond Myklebust 	unsigned int remainder = xdr->len - base;
42924c5684bSTrond Myklebust 	int err, sent = 0;
430a246b010SChuck Lever 
431262965f5SChuck Lever 	if (unlikely(!sock))
432fba91afbSTrond Myklebust 		return -ENOTSOCK;
433262965f5SChuck Lever 
434262965f5SChuck Lever 	clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags);
43524c5684bSTrond Myklebust 	if (base != 0) {
43624c5684bSTrond Myklebust 		addr = NULL;
43724c5684bSTrond Myklebust 		addrlen = 0;
43824c5684bSTrond Myklebust 	}
439262965f5SChuck Lever 
44024c5684bSTrond Myklebust 	if (base < xdr->head[0].iov_len || addr != NULL) {
44124c5684bSTrond Myklebust 		unsigned int len = xdr->head[0].iov_len - base;
44224c5684bSTrond Myklebust 		remainder -= len;
44324c5684bSTrond Myklebust 		err = xs_send_kvec(sock, addr, addrlen, &xdr->head[0], base, remainder != 0);
44424c5684bSTrond Myklebust 		if (remainder == 0 || err != len)
445a246b010SChuck Lever 			goto out;
44624c5684bSTrond Myklebust 		sent += err;
447a246b010SChuck Lever 		base = 0;
448a246b010SChuck Lever 	} else
44924c5684bSTrond Myklebust 		base -= xdr->head[0].iov_len;
450a246b010SChuck Lever 
45124c5684bSTrond Myklebust 	if (base < xdr->page_len) {
45224c5684bSTrond Myklebust 		unsigned int len = xdr->page_len - base;
45324c5684bSTrond Myklebust 		remainder -= len;
45424c5684bSTrond Myklebust 		err = xs_send_pagedata(sock, xdr, base, remainder != 0);
45524c5684bSTrond Myklebust 		if (remainder == 0 || err != len)
456a246b010SChuck Lever 			goto out;
45724c5684bSTrond Myklebust 		sent += err;
458a246b010SChuck Lever 		base = 0;
45924c5684bSTrond Myklebust 	} else
46024c5684bSTrond Myklebust 		base -= xdr->page_len;
46124c5684bSTrond Myklebust 
46224c5684bSTrond Myklebust 	if (base >= xdr->tail[0].iov_len)
46324c5684bSTrond Myklebust 		return sent;
46424c5684bSTrond Myklebust 	err = xs_send_kvec(sock, NULL, 0, &xdr->tail[0], base, 0);
465a246b010SChuck Lever out:
46624c5684bSTrond Myklebust 	if (sent == 0)
46724c5684bSTrond Myklebust 		return err;
46824c5684bSTrond Myklebust 	if (err > 0)
46924c5684bSTrond Myklebust 		sent += err;
47024c5684bSTrond Myklebust 	return sent;
471a246b010SChuck Lever }
472a246b010SChuck Lever 
473b6ddf64fSTrond Myklebust static void xs_nospace_callback(struct rpc_task *task)
474b6ddf64fSTrond Myklebust {
475b6ddf64fSTrond Myklebust 	struct sock_xprt *transport = container_of(task->tk_rqstp->rq_xprt, struct sock_xprt, xprt);
476b6ddf64fSTrond Myklebust 
477b6ddf64fSTrond Myklebust 	transport->inet->sk_write_pending--;
478b6ddf64fSTrond Myklebust 	clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
479b6ddf64fSTrond Myklebust }
480b6ddf64fSTrond Myklebust 
4819903cd1cSChuck Lever /**
482262965f5SChuck Lever  * xs_nospace - place task on wait queue if transmit was incomplete
483262965f5SChuck Lever  * @task: task to put to sleep
4849903cd1cSChuck Lever  *
485a246b010SChuck Lever  */
4865e3771ceSTrond Myklebust static int xs_nospace(struct rpc_task *task)
487a246b010SChuck Lever {
488262965f5SChuck Lever 	struct rpc_rqst *req = task->tk_rqstp;
489262965f5SChuck Lever 	struct rpc_xprt *xprt = req->rq_xprt;
490ee0ac0c2SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
4915e3771ceSTrond Myklebust 	int ret = 0;
492a246b010SChuck Lever 
49346121cf7SChuck Lever 	dprintk("RPC: %5u xmit incomplete (%u left of %u)\n",
494262965f5SChuck Lever 			task->tk_pid, req->rq_slen - req->rq_bytes_sent,
495262965f5SChuck Lever 			req->rq_slen);
496a246b010SChuck Lever 
497262965f5SChuck Lever 	/* Protect against races with write_space */
498262965f5SChuck Lever 	spin_lock_bh(&xprt->transport_lock);
499a246b010SChuck Lever 
500262965f5SChuck Lever 	/* Don't race with disconnect */
501b6ddf64fSTrond Myklebust 	if (xprt_connected(xprt)) {
502b6ddf64fSTrond Myklebust 		if (test_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags)) {
5035e3771ceSTrond Myklebust 			ret = -EAGAIN;
504b6ddf64fSTrond Myklebust 			/*
505b6ddf64fSTrond Myklebust 			 * Notify TCP that we're limited by the application
506b6ddf64fSTrond Myklebust 			 * window size
507b6ddf64fSTrond Myklebust 			 */
508b6ddf64fSTrond Myklebust 			set_bit(SOCK_NOSPACE, &transport->sock->flags);
509b6ddf64fSTrond Myklebust 			transport->inet->sk_write_pending++;
510b6ddf64fSTrond Myklebust 			/* ...and wait for more buffer space */
511b6ddf64fSTrond Myklebust 			xprt_wait_for_buffer_space(task, xs_nospace_callback);
512b6ddf64fSTrond Myklebust 		}
513b6ddf64fSTrond Myklebust 	} else {
514b6ddf64fSTrond Myklebust 		clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
5155e3771ceSTrond Myklebust 		ret = -ENOTCONN;
516b6ddf64fSTrond Myklebust 	}
517a246b010SChuck Lever 
518262965f5SChuck Lever 	spin_unlock_bh(&xprt->transport_lock);
5195e3771ceSTrond Myklebust 	return ret;
520a246b010SChuck Lever }
521a246b010SChuck Lever 
5229903cd1cSChuck Lever /**
523262965f5SChuck Lever  * xs_udp_send_request - write an RPC request to a UDP socket
5249903cd1cSChuck Lever  * @task: address of RPC task that manages the state of an RPC request
5259903cd1cSChuck Lever  *
5269903cd1cSChuck Lever  * Return values:
5279903cd1cSChuck Lever  *        0:	The request has been sent
5289903cd1cSChuck Lever  *   EAGAIN:	The socket was blocked, please call again later to
5299903cd1cSChuck Lever  *		complete the request
530262965f5SChuck Lever  * ENOTCONN:	Caller needs to invoke connect logic then call again
5319903cd1cSChuck Lever  *    other:	Some other error occured, the request was not sent
5329903cd1cSChuck Lever  */
533262965f5SChuck Lever static int xs_udp_send_request(struct rpc_task *task)
534a246b010SChuck Lever {
535a246b010SChuck Lever 	struct rpc_rqst *req = task->tk_rqstp;
536a246b010SChuck Lever 	struct rpc_xprt *xprt = req->rq_xprt;
537ee0ac0c2SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
538262965f5SChuck Lever 	struct xdr_buf *xdr = &req->rq_snd_buf;
539262965f5SChuck Lever 	int status;
540262965f5SChuck Lever 
541262965f5SChuck Lever 	xs_pktdump("packet data:",
542262965f5SChuck Lever 				req->rq_svec->iov_base,
543262965f5SChuck Lever 				req->rq_svec->iov_len);
544262965f5SChuck Lever 
54501d37c42STrond Myklebust 	if (!xprt_bound(xprt))
54601d37c42STrond Myklebust 		return -ENOTCONN;
547ee0ac0c2SChuck Lever 	status = xs_sendpages(transport->sock,
54895392c59SChuck Lever 			      xs_addr(xprt),
549ee0ac0c2SChuck Lever 			      xprt->addrlen, xdr,
550ee0ac0c2SChuck Lever 			      req->rq_bytes_sent);
551262965f5SChuck Lever 
552262965f5SChuck Lever 	dprintk("RPC:       xs_udp_send_request(%u) = %d\n",
553262965f5SChuck Lever 			xdr->len - req->rq_bytes_sent, status);
554262965f5SChuck Lever 
5552199700fSTrond Myklebust 	if (status >= 0) {
5561321d8d9SChuck Lever 		task->tk_bytes_sent += status;
5572199700fSTrond Myklebust 		if (status >= req->rq_slen)
558262965f5SChuck Lever 			return 0;
559262965f5SChuck Lever 		/* Still some bytes left; set up for a retry later. */
560262965f5SChuck Lever 		status = -EAGAIN;
5612199700fSTrond Myklebust 	}
562c8485e4dSTrond Myklebust 	if (!transport->sock)
563c8485e4dSTrond Myklebust 		goto out;
564262965f5SChuck Lever 
565262965f5SChuck Lever 	switch (status) {
566fba91afbSTrond Myklebust 	case -ENOTSOCK:
567fba91afbSTrond Myklebust 		status = -ENOTCONN;
568fba91afbSTrond Myklebust 		/* Should we call xs_close() here? */
569fba91afbSTrond Myklebust 		break;
570b6ddf64fSTrond Myklebust 	case -EAGAIN:
5715e3771ceSTrond Myklebust 		status = xs_nospace(task);
572b6ddf64fSTrond Myklebust 		break;
573c8485e4dSTrond Myklebust 	default:
574c8485e4dSTrond Myklebust 		dprintk("RPC:       sendmsg returned unrecognized error %d\n",
575c8485e4dSTrond Myklebust 			-status);
576262965f5SChuck Lever 	case -ENETUNREACH:
577262965f5SChuck Lever 	case -EPIPE:
578262965f5SChuck Lever 	case -ECONNREFUSED:
579262965f5SChuck Lever 		/* When the server has died, an ICMP port unreachable message
580262965f5SChuck Lever 		 * prompts ECONNREFUSED. */
581b6ddf64fSTrond Myklebust 		clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
582262965f5SChuck Lever 	}
583c8485e4dSTrond Myklebust out:
584262965f5SChuck Lever 	return status;
585262965f5SChuck Lever }
586262965f5SChuck Lever 
587e06799f9STrond Myklebust /**
588e06799f9STrond Myklebust  * xs_tcp_shutdown - gracefully shut down a TCP socket
589e06799f9STrond Myklebust  * @xprt: transport
590e06799f9STrond Myklebust  *
591e06799f9STrond Myklebust  * Initiates a graceful shutdown of the TCP socket by calling the
592e06799f9STrond Myklebust  * equivalent of shutdown(SHUT_WR);
593e06799f9STrond Myklebust  */
594e06799f9STrond Myklebust static void xs_tcp_shutdown(struct rpc_xprt *xprt)
595e06799f9STrond Myklebust {
596e06799f9STrond Myklebust 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
597e06799f9STrond Myklebust 	struct socket *sock = transport->sock;
598e06799f9STrond Myklebust 
599e06799f9STrond Myklebust 	if (sock != NULL)
600e06799f9STrond Myklebust 		kernel_sock_shutdown(sock, SHUT_WR);
601e06799f9STrond Myklebust }
602e06799f9STrond Myklebust 
603808012fbSChuck Lever static inline void xs_encode_tcp_record_marker(struct xdr_buf *buf)
604808012fbSChuck Lever {
605808012fbSChuck Lever 	u32 reclen = buf->len - sizeof(rpc_fraghdr);
606808012fbSChuck Lever 	rpc_fraghdr *base = buf->head[0].iov_base;
607808012fbSChuck Lever 	*base = htonl(RPC_LAST_STREAM_FRAGMENT | reclen);
608808012fbSChuck Lever }
609808012fbSChuck Lever 
610262965f5SChuck Lever /**
611262965f5SChuck Lever  * xs_tcp_send_request - write an RPC request to a TCP socket
612262965f5SChuck Lever  * @task: address of RPC task that manages the state of an RPC request
613262965f5SChuck Lever  *
614262965f5SChuck Lever  * Return values:
615262965f5SChuck Lever  *        0:	The request has been sent
616262965f5SChuck Lever  *   EAGAIN:	The socket was blocked, please call again later to
617262965f5SChuck Lever  *		complete the request
618262965f5SChuck Lever  * ENOTCONN:	Caller needs to invoke connect logic then call again
619262965f5SChuck Lever  *    other:	Some other error occured, the request was not sent
620262965f5SChuck Lever  *
621262965f5SChuck Lever  * XXX: In the case of soft timeouts, should we eventually give up
622262965f5SChuck Lever  *	if sendmsg is not able to make progress?
623262965f5SChuck Lever  */
624262965f5SChuck Lever static int xs_tcp_send_request(struct rpc_task *task)
625262965f5SChuck Lever {
626262965f5SChuck Lever 	struct rpc_rqst *req = task->tk_rqstp;
627262965f5SChuck Lever 	struct rpc_xprt *xprt = req->rq_xprt;
628ee0ac0c2SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
629262965f5SChuck Lever 	struct xdr_buf *xdr = &req->rq_snd_buf;
630b595bb15SChuck Lever 	int status;
631a246b010SChuck Lever 
632808012fbSChuck Lever 	xs_encode_tcp_record_marker(&req->rq_snd_buf);
633262965f5SChuck Lever 
634262965f5SChuck Lever 	xs_pktdump("packet data:",
635262965f5SChuck Lever 				req->rq_svec->iov_base,
636262965f5SChuck Lever 				req->rq_svec->iov_len);
637a246b010SChuck Lever 
638a246b010SChuck Lever 	/* Continue transmitting the packet/record. We must be careful
639a246b010SChuck Lever 	 * to cope with writespace callbacks arriving _after_ we have
640262965f5SChuck Lever 	 * called sendmsg(). */
641a246b010SChuck Lever 	while (1) {
642ee0ac0c2SChuck Lever 		status = xs_sendpages(transport->sock,
643ee0ac0c2SChuck Lever 					NULL, 0, xdr, req->rq_bytes_sent);
644a246b010SChuck Lever 
645262965f5SChuck Lever 		dprintk("RPC:       xs_tcp_send_request(%u) = %d\n",
646262965f5SChuck Lever 				xdr->len - req->rq_bytes_sent, status);
647262965f5SChuck Lever 
648262965f5SChuck Lever 		if (unlikely(status < 0))
649a246b010SChuck Lever 			break;
650a246b010SChuck Lever 
651a246b010SChuck Lever 		/* If we've sent the entire packet, immediately
652a246b010SChuck Lever 		 * reset the count of bytes sent. */
653262965f5SChuck Lever 		req->rq_bytes_sent += status;
654ef759a2eSChuck Lever 		task->tk_bytes_sent += status;
655262965f5SChuck Lever 		if (likely(req->rq_bytes_sent >= req->rq_slen)) {
656a246b010SChuck Lever 			req->rq_bytes_sent = 0;
657a246b010SChuck Lever 			return 0;
658a246b010SChuck Lever 		}
659262965f5SChuck Lever 
66006b4b681STrond Myklebust 		if (status != 0)
66106b4b681STrond Myklebust 			continue;
662a246b010SChuck Lever 		status = -EAGAIN;
663a246b010SChuck Lever 		break;
664a246b010SChuck Lever 	}
665c8485e4dSTrond Myklebust 	if (!transport->sock)
666c8485e4dSTrond Myklebust 		goto out;
667a246b010SChuck Lever 
668262965f5SChuck Lever 	switch (status) {
669fba91afbSTrond Myklebust 	case -ENOTSOCK:
670fba91afbSTrond Myklebust 		status = -ENOTCONN;
671fba91afbSTrond Myklebust 		/* Should we call xs_close() here? */
672fba91afbSTrond Myklebust 		break;
673262965f5SChuck Lever 	case -EAGAIN:
6745e3771ceSTrond Myklebust 		status = xs_nospace(task);
675262965f5SChuck Lever 		break;
676262965f5SChuck Lever 	default:
677262965f5SChuck Lever 		dprintk("RPC:       sendmsg returned unrecognized error %d\n",
678262965f5SChuck Lever 			-status);
679a246b010SChuck Lever 	case -ECONNRESET:
68055420c24STrond Myklebust 	case -EPIPE:
681e06799f9STrond Myklebust 		xs_tcp_shutdown(xprt);
682a246b010SChuck Lever 	case -ECONNREFUSED:
683a246b010SChuck Lever 	case -ENOTCONN:
684a246b010SChuck Lever 		clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
685a246b010SChuck Lever 	}
686c8485e4dSTrond Myklebust out:
687a246b010SChuck Lever 	return status;
688a246b010SChuck Lever }
689a246b010SChuck Lever 
6909903cd1cSChuck Lever /**
691e0ab53deSTrond Myklebust  * xs_tcp_release_xprt - clean up after a tcp transmission
692e0ab53deSTrond Myklebust  * @xprt: transport
693e0ab53deSTrond Myklebust  * @task: rpc task
694e0ab53deSTrond Myklebust  *
695e0ab53deSTrond Myklebust  * This cleans up if an error causes us to abort the transmission of a request.
696e0ab53deSTrond Myklebust  * In this case, the socket may need to be reset in order to avoid confusing
697e0ab53deSTrond Myklebust  * the server.
698e0ab53deSTrond Myklebust  */
699e0ab53deSTrond Myklebust static void xs_tcp_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
700e0ab53deSTrond Myklebust {
701e0ab53deSTrond Myklebust 	struct rpc_rqst *req;
702e0ab53deSTrond Myklebust 
703e0ab53deSTrond Myklebust 	if (task != xprt->snd_task)
704e0ab53deSTrond Myklebust 		return;
705e0ab53deSTrond Myklebust 	if (task == NULL)
706e0ab53deSTrond Myklebust 		goto out_release;
707e0ab53deSTrond Myklebust 	req = task->tk_rqstp;
708e0ab53deSTrond Myklebust 	if (req->rq_bytes_sent == 0)
709e0ab53deSTrond Myklebust 		goto out_release;
710e0ab53deSTrond Myklebust 	if (req->rq_bytes_sent == req->rq_snd_buf.len)
711e0ab53deSTrond Myklebust 		goto out_release;
712e0ab53deSTrond Myklebust 	set_bit(XPRT_CLOSE_WAIT, &task->tk_xprt->state);
713e0ab53deSTrond Myklebust out_release:
714e0ab53deSTrond Myklebust 	xprt_release_xprt(xprt, task);
715e0ab53deSTrond Myklebust }
716e0ab53deSTrond Myklebust 
7172a9e1cfaSTrond Myklebust static void xs_save_old_callbacks(struct sock_xprt *transport, struct sock *sk)
7182a9e1cfaSTrond Myklebust {
7192a9e1cfaSTrond Myklebust 	transport->old_data_ready = sk->sk_data_ready;
7202a9e1cfaSTrond Myklebust 	transport->old_state_change = sk->sk_state_change;
7212a9e1cfaSTrond Myklebust 	transport->old_write_space = sk->sk_write_space;
7222a9e1cfaSTrond Myklebust 	transport->old_error_report = sk->sk_error_report;
7232a9e1cfaSTrond Myklebust }
7242a9e1cfaSTrond Myklebust 
7252a9e1cfaSTrond Myklebust static void xs_restore_old_callbacks(struct sock_xprt *transport, struct sock *sk)
7262a9e1cfaSTrond Myklebust {
7272a9e1cfaSTrond Myklebust 	sk->sk_data_ready = transport->old_data_ready;
7282a9e1cfaSTrond Myklebust 	sk->sk_state_change = transport->old_state_change;
7292a9e1cfaSTrond Myklebust 	sk->sk_write_space = transport->old_write_space;
7302a9e1cfaSTrond Myklebust 	sk->sk_error_report = transport->old_error_report;
7312a9e1cfaSTrond Myklebust }
7322a9e1cfaSTrond Myklebust 
733fe315e76SChuck Lever static void xs_reset_transport(struct sock_xprt *transport)
734a246b010SChuck Lever {
735ee0ac0c2SChuck Lever 	struct socket *sock = transport->sock;
736ee0ac0c2SChuck Lever 	struct sock *sk = transport->inet;
737a246b010SChuck Lever 
738fe315e76SChuck Lever 	if (sk == NULL)
739fe315e76SChuck Lever 		return;
7409903cd1cSChuck Lever 
741a246b010SChuck Lever 	write_lock_bh(&sk->sk_callback_lock);
742ee0ac0c2SChuck Lever 	transport->inet = NULL;
743ee0ac0c2SChuck Lever 	transport->sock = NULL;
744a246b010SChuck Lever 
745a246b010SChuck Lever 	sk->sk_user_data = NULL;
7462a9e1cfaSTrond Myklebust 
7472a9e1cfaSTrond Myklebust 	xs_restore_old_callbacks(transport, sk);
748a246b010SChuck Lever 	write_unlock_bh(&sk->sk_callback_lock);
749a246b010SChuck Lever 
750a246b010SChuck Lever 	sk->sk_no_check = 0;
751a246b010SChuck Lever 
752a246b010SChuck Lever 	sock_release(sock);
753fe315e76SChuck Lever }
754fe315e76SChuck Lever 
755fe315e76SChuck Lever /**
756fe315e76SChuck Lever  * xs_close - close a socket
757fe315e76SChuck Lever  * @xprt: transport
758fe315e76SChuck Lever  *
759fe315e76SChuck Lever  * This is used when all requests are complete; ie, no DRC state remains
760fe315e76SChuck Lever  * on the server we want to save.
761f75e6745STrond Myklebust  *
762f75e6745STrond Myklebust  * The caller _must_ be holding XPRT_LOCKED in order to avoid issues with
763f75e6745STrond Myklebust  * xs_reset_transport() zeroing the socket from underneath a writer.
764fe315e76SChuck Lever  */
765fe315e76SChuck Lever static void xs_close(struct rpc_xprt *xprt)
766fe315e76SChuck Lever {
767fe315e76SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
768fe315e76SChuck Lever 
769fe315e76SChuck Lever 	dprintk("RPC:       xs_close xprt %p\n", xprt);
770fe315e76SChuck Lever 
771fe315e76SChuck Lever 	xs_reset_transport(transport);
772fe315e76SChuck Lever 
773632e3bdcSTrond Myklebust 	smp_mb__before_clear_bit();
7747d1e8255STrond Myklebust 	clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
775632e3bdcSTrond Myklebust 	clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
7763b948ae5STrond Myklebust 	clear_bit(XPRT_CLOSING, &xprt->state);
777632e3bdcSTrond Myklebust 	smp_mb__after_clear_bit();
77862da3b24STrond Myklebust 	xprt_disconnect_done(xprt);
779a246b010SChuck Lever }
780a246b010SChuck Lever 
781f75e6745STrond Myklebust static void xs_tcp_close(struct rpc_xprt *xprt)
782f75e6745STrond Myklebust {
783f75e6745STrond Myklebust 	if (test_and_clear_bit(XPRT_CONNECTION_CLOSE, &xprt->state))
784f75e6745STrond Myklebust 		xs_close(xprt);
785f75e6745STrond Myklebust 	else
786f75e6745STrond Myklebust 		xs_tcp_shutdown(xprt);
787f75e6745STrond Myklebust }
788f75e6745STrond Myklebust 
7899903cd1cSChuck Lever /**
7909903cd1cSChuck Lever  * xs_destroy - prepare to shutdown a transport
7919903cd1cSChuck Lever  * @xprt: doomed transport
7929903cd1cSChuck Lever  *
7939903cd1cSChuck Lever  */
7949903cd1cSChuck Lever static void xs_destroy(struct rpc_xprt *xprt)
795a246b010SChuck Lever {
796c8475461SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
797c8475461SChuck Lever 
7989903cd1cSChuck Lever 	dprintk("RPC:       xs_destroy xprt %p\n", xprt);
7999903cd1cSChuck Lever 
800c1384c9cSTrond Myklebust 	cancel_rearming_delayed_work(&transport->connect_worker);
801a246b010SChuck Lever 
8029903cd1cSChuck Lever 	xs_close(xprt);
803edb267a6SChuck Lever 	xs_free_peer_addresses(xprt);
804a246b010SChuck Lever 	kfree(xprt->slot);
805c8541ecdSChuck Lever 	kfree(xprt);
806bc25571eS\"Talpey, Thomas\ 	module_put(THIS_MODULE);
807a246b010SChuck Lever }
808a246b010SChuck Lever 
8099903cd1cSChuck Lever static inline struct rpc_xprt *xprt_from_sock(struct sock *sk)
8109903cd1cSChuck Lever {
8119903cd1cSChuck Lever 	return (struct rpc_xprt *) sk->sk_user_data;
8129903cd1cSChuck Lever }
8139903cd1cSChuck Lever 
8149903cd1cSChuck Lever /**
8159903cd1cSChuck Lever  * xs_udp_data_ready - "data ready" callback for UDP sockets
8169903cd1cSChuck Lever  * @sk: socket with data to read
8179903cd1cSChuck Lever  * @len: how much data to read
8189903cd1cSChuck Lever  *
819a246b010SChuck Lever  */
8209903cd1cSChuck Lever static void xs_udp_data_ready(struct sock *sk, int len)
821a246b010SChuck Lever {
822a246b010SChuck Lever 	struct rpc_task *task;
823a246b010SChuck Lever 	struct rpc_xprt *xprt;
824a246b010SChuck Lever 	struct rpc_rqst *rovr;
825a246b010SChuck Lever 	struct sk_buff *skb;
826a246b010SChuck Lever 	int err, repsize, copied;
827d8ed029dSAlexey Dobriyan 	u32 _xid;
828d8ed029dSAlexey Dobriyan 	__be32 *xp;
829a246b010SChuck Lever 
830a246b010SChuck Lever 	read_lock(&sk->sk_callback_lock);
8319903cd1cSChuck Lever 	dprintk("RPC:       xs_udp_data_ready...\n");
8329903cd1cSChuck Lever 	if (!(xprt = xprt_from_sock(sk)))
833a246b010SChuck Lever 		goto out;
834a246b010SChuck Lever 
835a246b010SChuck Lever 	if ((skb = skb_recv_datagram(sk, 0, 1, &err)) == NULL)
836a246b010SChuck Lever 		goto out;
837a246b010SChuck Lever 
838a246b010SChuck Lever 	if (xprt->shutdown)
839a246b010SChuck Lever 		goto dropit;
840a246b010SChuck Lever 
841a246b010SChuck Lever 	repsize = skb->len - sizeof(struct udphdr);
842a246b010SChuck Lever 	if (repsize < 4) {
8439903cd1cSChuck Lever 		dprintk("RPC:       impossible RPC reply size %d!\n", repsize);
844a246b010SChuck Lever 		goto dropit;
845a246b010SChuck Lever 	}
846a246b010SChuck Lever 
847a246b010SChuck Lever 	/* Copy the XID from the skb... */
848a246b010SChuck Lever 	xp = skb_header_pointer(skb, sizeof(struct udphdr),
849a246b010SChuck Lever 				sizeof(_xid), &_xid);
850a246b010SChuck Lever 	if (xp == NULL)
851a246b010SChuck Lever 		goto dropit;
852a246b010SChuck Lever 
853a246b010SChuck Lever 	/* Look up and lock the request corresponding to the given XID */
8544a0f8c04SChuck Lever 	spin_lock(&xprt->transport_lock);
855a246b010SChuck Lever 	rovr = xprt_lookup_rqst(xprt, *xp);
856a246b010SChuck Lever 	if (!rovr)
857a246b010SChuck Lever 		goto out_unlock;
858a246b010SChuck Lever 	task = rovr->rq_task;
859a246b010SChuck Lever 
860a246b010SChuck Lever 	if ((copied = rovr->rq_private_buf.buflen) > repsize)
861a246b010SChuck Lever 		copied = repsize;
862a246b010SChuck Lever 
863a246b010SChuck Lever 	/* Suck it into the iovec, verify checksum if not done by hw. */
8641781f7f5SHerbert Xu 	if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) {
8651781f7f5SHerbert Xu 		UDPX_INC_STATS_BH(sk, UDP_MIB_INERRORS);
866a246b010SChuck Lever 		goto out_unlock;
8671781f7f5SHerbert Xu 	}
8681781f7f5SHerbert Xu 
8691781f7f5SHerbert Xu 	UDPX_INC_STATS_BH(sk, UDP_MIB_INDATAGRAMS);
870a246b010SChuck Lever 
871a246b010SChuck Lever 	/* Something worked... */
872adf30907SEric Dumazet 	dst_confirm(skb_dst(skb));
873a246b010SChuck Lever 
8741570c1e4SChuck Lever 	xprt_adjust_cwnd(task, copied);
8751570c1e4SChuck Lever 	xprt_update_rtt(task);
8761570c1e4SChuck Lever 	xprt_complete_rqst(task, copied);
877a246b010SChuck Lever 
878a246b010SChuck Lever  out_unlock:
8794a0f8c04SChuck Lever 	spin_unlock(&xprt->transport_lock);
880a246b010SChuck Lever  dropit:
881a246b010SChuck Lever 	skb_free_datagram(sk, skb);
882a246b010SChuck Lever  out:
883a246b010SChuck Lever 	read_unlock(&sk->sk_callback_lock);
884a246b010SChuck Lever }
885a246b010SChuck Lever 
886dd456471SChuck Lever static inline void xs_tcp_read_fraghdr(struct rpc_xprt *xprt, struct xdr_skb_reader *desc)
887a246b010SChuck Lever {
88851971139SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
889a246b010SChuck Lever 	size_t len, used;
890a246b010SChuck Lever 	char *p;
891a246b010SChuck Lever 
89251971139SChuck Lever 	p = ((char *) &transport->tcp_fraghdr) + transport->tcp_offset;
89351971139SChuck Lever 	len = sizeof(transport->tcp_fraghdr) - transport->tcp_offset;
8949d292316SChuck Lever 	used = xdr_skb_read_bits(desc, p, len);
89551971139SChuck Lever 	transport->tcp_offset += used;
896a246b010SChuck Lever 	if (used != len)
897a246b010SChuck Lever 		return;
898808012fbSChuck Lever 
89951971139SChuck Lever 	transport->tcp_reclen = ntohl(transport->tcp_fraghdr);
90051971139SChuck Lever 	if (transport->tcp_reclen & RPC_LAST_STREAM_FRAGMENT)
901e136d092SChuck Lever 		transport->tcp_flags |= TCP_RCV_LAST_FRAG;
902a246b010SChuck Lever 	else
903e136d092SChuck Lever 		transport->tcp_flags &= ~TCP_RCV_LAST_FRAG;
90451971139SChuck Lever 	transport->tcp_reclen &= RPC_FRAGMENT_SIZE_MASK;
905808012fbSChuck Lever 
906e136d092SChuck Lever 	transport->tcp_flags &= ~TCP_RCV_COPY_FRAGHDR;
90751971139SChuck Lever 	transport->tcp_offset = 0;
908808012fbSChuck Lever 
909a246b010SChuck Lever 	/* Sanity check of the record length */
91018dca02aSRicardo Labiaga 	if (unlikely(transport->tcp_reclen < 8)) {
9119903cd1cSChuck Lever 		dprintk("RPC:       invalid TCP record fragment length\n");
9123ebb067dSTrond Myklebust 		xprt_force_disconnect(xprt);
9139903cd1cSChuck Lever 		return;
914a246b010SChuck Lever 	}
915a246b010SChuck Lever 	dprintk("RPC:       reading TCP record fragment of length %d\n",
91651971139SChuck Lever 			transport->tcp_reclen);
917a246b010SChuck Lever }
918a246b010SChuck Lever 
91951971139SChuck Lever static void xs_tcp_check_fraghdr(struct sock_xprt *transport)
920a246b010SChuck Lever {
92151971139SChuck Lever 	if (transport->tcp_offset == transport->tcp_reclen) {
922e136d092SChuck Lever 		transport->tcp_flags |= TCP_RCV_COPY_FRAGHDR;
92351971139SChuck Lever 		transport->tcp_offset = 0;
924e136d092SChuck Lever 		if (transport->tcp_flags & TCP_RCV_LAST_FRAG) {
925e136d092SChuck Lever 			transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
926e136d092SChuck Lever 			transport->tcp_flags |= TCP_RCV_COPY_XID;
92751971139SChuck Lever 			transport->tcp_copied = 0;
928a246b010SChuck Lever 		}
929a246b010SChuck Lever 	}
930a246b010SChuck Lever }
931a246b010SChuck Lever 
932dd456471SChuck Lever static inline void xs_tcp_read_xid(struct sock_xprt *transport, struct xdr_skb_reader *desc)
933a246b010SChuck Lever {
934a246b010SChuck Lever 	size_t len, used;
935a246b010SChuck Lever 	char *p;
936a246b010SChuck Lever 
93751971139SChuck Lever 	len = sizeof(transport->tcp_xid) - transport->tcp_offset;
938a246b010SChuck Lever 	dprintk("RPC:       reading XID (%Zu bytes)\n", len);
93951971139SChuck Lever 	p = ((char *) &transport->tcp_xid) + transport->tcp_offset;
9409d292316SChuck Lever 	used = xdr_skb_read_bits(desc, p, len);
94151971139SChuck Lever 	transport->tcp_offset += used;
942a246b010SChuck Lever 	if (used != len)
943a246b010SChuck Lever 		return;
944e136d092SChuck Lever 	transport->tcp_flags &= ~TCP_RCV_COPY_XID;
945f4a2e418SRicardo Labiaga 	transport->tcp_flags |= TCP_RCV_READ_CALLDIR;
94651971139SChuck Lever 	transport->tcp_copied = 4;
94718dca02aSRicardo Labiaga 	dprintk("RPC:       reading %s XID %08x\n",
94818dca02aSRicardo Labiaga 			(transport->tcp_flags & TCP_RPC_REPLY) ? "reply for"
94918dca02aSRicardo Labiaga 							      : "request with",
95051971139SChuck Lever 			ntohl(transport->tcp_xid));
95151971139SChuck Lever 	xs_tcp_check_fraghdr(transport);
952a246b010SChuck Lever }
953a246b010SChuck Lever 
95418dca02aSRicardo Labiaga static inline void xs_tcp_read_calldir(struct sock_xprt *transport,
95518dca02aSRicardo Labiaga 				       struct xdr_skb_reader *desc)
956a246b010SChuck Lever {
95718dca02aSRicardo Labiaga 	size_t len, used;
95818dca02aSRicardo Labiaga 	u32 offset;
95918dca02aSRicardo Labiaga 	__be32	calldir;
96018dca02aSRicardo Labiaga 
96118dca02aSRicardo Labiaga 	/*
96218dca02aSRicardo Labiaga 	 * We want transport->tcp_offset to be 8 at the end of this routine
96318dca02aSRicardo Labiaga 	 * (4 bytes for the xid and 4 bytes for the call/reply flag).
96418dca02aSRicardo Labiaga 	 * When this function is called for the first time,
96518dca02aSRicardo Labiaga 	 * transport->tcp_offset is 4 (after having already read the xid).
96618dca02aSRicardo Labiaga 	 */
96718dca02aSRicardo Labiaga 	offset = transport->tcp_offset - sizeof(transport->tcp_xid);
96818dca02aSRicardo Labiaga 	len = sizeof(calldir) - offset;
96918dca02aSRicardo Labiaga 	dprintk("RPC:       reading CALL/REPLY flag (%Zu bytes)\n", len);
97018dca02aSRicardo Labiaga 	used = xdr_skb_read_bits(desc, &calldir, len);
97118dca02aSRicardo Labiaga 	transport->tcp_offset += used;
97218dca02aSRicardo Labiaga 	if (used != len)
97318dca02aSRicardo Labiaga 		return;
974f4a2e418SRicardo Labiaga 	transport->tcp_flags &= ~TCP_RCV_READ_CALLDIR;
975f4a2e418SRicardo Labiaga 	transport->tcp_flags |= TCP_RCV_COPY_CALLDIR;
97618dca02aSRicardo Labiaga 	transport->tcp_flags |= TCP_RCV_COPY_DATA;
977f4a2e418SRicardo Labiaga 	/*
978f4a2e418SRicardo Labiaga 	 * We don't yet have the XDR buffer, so we will write the calldir
979f4a2e418SRicardo Labiaga 	 * out after we get the buffer from the 'struct rpc_rqst'
980f4a2e418SRicardo Labiaga 	 */
98118dca02aSRicardo Labiaga 	if (ntohl(calldir) == RPC_REPLY)
98218dca02aSRicardo Labiaga 		transport->tcp_flags |= TCP_RPC_REPLY;
98318dca02aSRicardo Labiaga 	else
98418dca02aSRicardo Labiaga 		transport->tcp_flags &= ~TCP_RPC_REPLY;
98518dca02aSRicardo Labiaga 	dprintk("RPC:       reading %s CALL/REPLY flag %08x\n",
98618dca02aSRicardo Labiaga 			(transport->tcp_flags & TCP_RPC_REPLY) ?
98718dca02aSRicardo Labiaga 				"reply for" : "request with", calldir);
98818dca02aSRicardo Labiaga 	xs_tcp_check_fraghdr(transport);
98918dca02aSRicardo Labiaga }
99018dca02aSRicardo Labiaga 
99144b98efdSRicardo Labiaga static inline void xs_tcp_read_common(struct rpc_xprt *xprt,
99244b98efdSRicardo Labiaga 				     struct xdr_skb_reader *desc,
99344b98efdSRicardo Labiaga 				     struct rpc_rqst *req)
994a246b010SChuck Lever {
99544b98efdSRicardo Labiaga 	struct sock_xprt *transport =
99644b98efdSRicardo Labiaga 				container_of(xprt, struct sock_xprt, xprt);
997a246b010SChuck Lever 	struct xdr_buf *rcvbuf;
998a246b010SChuck Lever 	size_t len;
999a246b010SChuck Lever 	ssize_t r;
1000a246b010SChuck Lever 
1001a246b010SChuck Lever 	rcvbuf = &req->rq_private_buf;
1002f4a2e418SRicardo Labiaga 
1003f4a2e418SRicardo Labiaga 	if (transport->tcp_flags & TCP_RCV_COPY_CALLDIR) {
1004f4a2e418SRicardo Labiaga 		/*
1005f4a2e418SRicardo Labiaga 		 * Save the RPC direction in the XDR buffer
1006f4a2e418SRicardo Labiaga 		 */
1007f4a2e418SRicardo Labiaga 		__be32	calldir = transport->tcp_flags & TCP_RPC_REPLY ?
1008f4a2e418SRicardo Labiaga 					htonl(RPC_REPLY) : 0;
1009f4a2e418SRicardo Labiaga 
1010f4a2e418SRicardo Labiaga 		memcpy(rcvbuf->head[0].iov_base + transport->tcp_copied,
1011f4a2e418SRicardo Labiaga 			&calldir, sizeof(calldir));
1012f4a2e418SRicardo Labiaga 		transport->tcp_copied += sizeof(calldir);
1013f4a2e418SRicardo Labiaga 		transport->tcp_flags &= ~TCP_RCV_COPY_CALLDIR;
1014a246b010SChuck Lever 	}
1015a246b010SChuck Lever 
1016a246b010SChuck Lever 	len = desc->count;
101751971139SChuck Lever 	if (len > transport->tcp_reclen - transport->tcp_offset) {
1018dd456471SChuck Lever 		struct xdr_skb_reader my_desc;
1019a246b010SChuck Lever 
102051971139SChuck Lever 		len = transport->tcp_reclen - transport->tcp_offset;
1021a246b010SChuck Lever 		memcpy(&my_desc, desc, sizeof(my_desc));
1022a246b010SChuck Lever 		my_desc.count = len;
102351971139SChuck Lever 		r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
10249d292316SChuck Lever 					  &my_desc, xdr_skb_read_bits);
1025a246b010SChuck Lever 		desc->count -= r;
1026a246b010SChuck Lever 		desc->offset += r;
1027a246b010SChuck Lever 	} else
102851971139SChuck Lever 		r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
10299d292316SChuck Lever 					  desc, xdr_skb_read_bits);
1030a246b010SChuck Lever 
1031a246b010SChuck Lever 	if (r > 0) {
103251971139SChuck Lever 		transport->tcp_copied += r;
103351971139SChuck Lever 		transport->tcp_offset += r;
1034a246b010SChuck Lever 	}
1035a246b010SChuck Lever 	if (r != len) {
1036a246b010SChuck Lever 		/* Error when copying to the receive buffer,
1037a246b010SChuck Lever 		 * usually because we weren't able to allocate
1038a246b010SChuck Lever 		 * additional buffer pages. All we can do now
1039e136d092SChuck Lever 		 * is turn off TCP_RCV_COPY_DATA, so the request
1040a246b010SChuck Lever 		 * will not receive any additional updates,
1041a246b010SChuck Lever 		 * and time out.
1042a246b010SChuck Lever 		 * Any remaining data from this record will
1043a246b010SChuck Lever 		 * be discarded.
1044a246b010SChuck Lever 		 */
1045e136d092SChuck Lever 		transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1046a246b010SChuck Lever 		dprintk("RPC:       XID %08x truncated request\n",
104751971139SChuck Lever 				ntohl(transport->tcp_xid));
104846121cf7SChuck Lever 		dprintk("RPC:       xprt = %p, tcp_copied = %lu, "
104946121cf7SChuck Lever 				"tcp_offset = %u, tcp_reclen = %u\n",
105046121cf7SChuck Lever 				xprt, transport->tcp_copied,
105146121cf7SChuck Lever 				transport->tcp_offset, transport->tcp_reclen);
105244b98efdSRicardo Labiaga 		return;
1053a246b010SChuck Lever 	}
1054a246b010SChuck Lever 
1055a246b010SChuck Lever 	dprintk("RPC:       XID %08x read %Zd bytes\n",
105651971139SChuck Lever 			ntohl(transport->tcp_xid), r);
105746121cf7SChuck Lever 	dprintk("RPC:       xprt = %p, tcp_copied = %lu, tcp_offset = %u, "
105846121cf7SChuck Lever 			"tcp_reclen = %u\n", xprt, transport->tcp_copied,
105946121cf7SChuck Lever 			transport->tcp_offset, transport->tcp_reclen);
1060a246b010SChuck Lever 
106151971139SChuck Lever 	if (transport->tcp_copied == req->rq_private_buf.buflen)
1062e136d092SChuck Lever 		transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
106351971139SChuck Lever 	else if (transport->tcp_offset == transport->tcp_reclen) {
1064e136d092SChuck Lever 		if (transport->tcp_flags & TCP_RCV_LAST_FRAG)
1065e136d092SChuck Lever 			transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1066a246b010SChuck Lever 	}
1067a246b010SChuck Lever 
106844b98efdSRicardo Labiaga 	return;
106944b98efdSRicardo Labiaga }
107044b98efdSRicardo Labiaga 
107144b98efdSRicardo Labiaga /*
107244b98efdSRicardo Labiaga  * Finds the request corresponding to the RPC xid and invokes the common
107344b98efdSRicardo Labiaga  * tcp read code to read the data.
107444b98efdSRicardo Labiaga  */
107544b98efdSRicardo Labiaga static inline int xs_tcp_read_reply(struct rpc_xprt *xprt,
107644b98efdSRicardo Labiaga 				    struct xdr_skb_reader *desc)
107744b98efdSRicardo Labiaga {
107844b98efdSRicardo Labiaga 	struct sock_xprt *transport =
107944b98efdSRicardo Labiaga 				container_of(xprt, struct sock_xprt, xprt);
108044b98efdSRicardo Labiaga 	struct rpc_rqst *req;
108144b98efdSRicardo Labiaga 
108244b98efdSRicardo Labiaga 	dprintk("RPC:       read reply XID %08x\n", ntohl(transport->tcp_xid));
108344b98efdSRicardo Labiaga 
108444b98efdSRicardo Labiaga 	/* Find and lock the request corresponding to this xid */
108544b98efdSRicardo Labiaga 	spin_lock(&xprt->transport_lock);
108644b98efdSRicardo Labiaga 	req = xprt_lookup_rqst(xprt, transport->tcp_xid);
108744b98efdSRicardo Labiaga 	if (!req) {
108844b98efdSRicardo Labiaga 		dprintk("RPC:       XID %08x request not found!\n",
108944b98efdSRicardo Labiaga 				ntohl(transport->tcp_xid));
109044b98efdSRicardo Labiaga 		spin_unlock(&xprt->transport_lock);
109144b98efdSRicardo Labiaga 		return -1;
109244b98efdSRicardo Labiaga 	}
109344b98efdSRicardo Labiaga 
109444b98efdSRicardo Labiaga 	xs_tcp_read_common(xprt, desc, req);
109544b98efdSRicardo Labiaga 
1096e136d092SChuck Lever 	if (!(transport->tcp_flags & TCP_RCV_COPY_DATA))
109751971139SChuck Lever 		xprt_complete_rqst(req->rq_task, transport->tcp_copied);
109844b98efdSRicardo Labiaga 
10994a0f8c04SChuck Lever 	spin_unlock(&xprt->transport_lock);
110044b98efdSRicardo Labiaga 	return 0;
110144b98efdSRicardo Labiaga }
110244b98efdSRicardo Labiaga 
110344b98efdSRicardo Labiaga #if defined(CONFIG_NFS_V4_1)
110444b98efdSRicardo Labiaga /*
110544b98efdSRicardo Labiaga  * Obtains an rpc_rqst previously allocated and invokes the common
110644b98efdSRicardo Labiaga  * tcp read code to read the data.  The result is placed in the callback
110744b98efdSRicardo Labiaga  * queue.
110844b98efdSRicardo Labiaga  * If we're unable to obtain the rpc_rqst we schedule the closing of the
110944b98efdSRicardo Labiaga  * connection and return -1.
111044b98efdSRicardo Labiaga  */
111144b98efdSRicardo Labiaga static inline int xs_tcp_read_callback(struct rpc_xprt *xprt,
111244b98efdSRicardo Labiaga 				       struct xdr_skb_reader *desc)
111344b98efdSRicardo Labiaga {
111444b98efdSRicardo Labiaga 	struct sock_xprt *transport =
111544b98efdSRicardo Labiaga 				container_of(xprt, struct sock_xprt, xprt);
111644b98efdSRicardo Labiaga 	struct rpc_rqst *req;
111744b98efdSRicardo Labiaga 
111844b98efdSRicardo Labiaga 	req = xprt_alloc_bc_request(xprt);
111944b98efdSRicardo Labiaga 	if (req == NULL) {
112044b98efdSRicardo Labiaga 		printk(KERN_WARNING "Callback slot table overflowed\n");
112144b98efdSRicardo Labiaga 		xprt_force_disconnect(xprt);
112244b98efdSRicardo Labiaga 		return -1;
112344b98efdSRicardo Labiaga 	}
112444b98efdSRicardo Labiaga 
112544b98efdSRicardo Labiaga 	req->rq_xid = transport->tcp_xid;
112644b98efdSRicardo Labiaga 	dprintk("RPC:       read callback  XID %08x\n", ntohl(req->rq_xid));
112744b98efdSRicardo Labiaga 	xs_tcp_read_common(xprt, desc, req);
112844b98efdSRicardo Labiaga 
112944b98efdSRicardo Labiaga 	if (!(transport->tcp_flags & TCP_RCV_COPY_DATA)) {
113044b98efdSRicardo Labiaga 		struct svc_serv *bc_serv = xprt->bc_serv;
113144b98efdSRicardo Labiaga 
113244b98efdSRicardo Labiaga 		/*
113344b98efdSRicardo Labiaga 		 * Add callback request to callback list.  The callback
113444b98efdSRicardo Labiaga 		 * service sleeps on the sv_cb_waitq waiting for new
113544b98efdSRicardo Labiaga 		 * requests.  Wake it up after adding enqueing the
113644b98efdSRicardo Labiaga 		 * request.
113744b98efdSRicardo Labiaga 		 */
113844b98efdSRicardo Labiaga 		dprintk("RPC:       add callback request to list\n");
113944b98efdSRicardo Labiaga 		spin_lock(&bc_serv->sv_cb_lock);
114044b98efdSRicardo Labiaga 		list_add(&req->rq_bc_list, &bc_serv->sv_cb_list);
114144b98efdSRicardo Labiaga 		spin_unlock(&bc_serv->sv_cb_lock);
114244b98efdSRicardo Labiaga 		wake_up(&bc_serv->sv_cb_waitq);
114344b98efdSRicardo Labiaga 	}
114444b98efdSRicardo Labiaga 
114544b98efdSRicardo Labiaga 	req->rq_private_buf.len = transport->tcp_copied;
114644b98efdSRicardo Labiaga 
114744b98efdSRicardo Labiaga 	return 0;
114844b98efdSRicardo Labiaga }
114944b98efdSRicardo Labiaga 
115044b98efdSRicardo Labiaga static inline int _xs_tcp_read_data(struct rpc_xprt *xprt,
115144b98efdSRicardo Labiaga 					struct xdr_skb_reader *desc)
115244b98efdSRicardo Labiaga {
115344b98efdSRicardo Labiaga 	struct sock_xprt *transport =
115444b98efdSRicardo Labiaga 				container_of(xprt, struct sock_xprt, xprt);
115544b98efdSRicardo Labiaga 
115644b98efdSRicardo Labiaga 	return (transport->tcp_flags & TCP_RPC_REPLY) ?
115744b98efdSRicardo Labiaga 		xs_tcp_read_reply(xprt, desc) :
115844b98efdSRicardo Labiaga 		xs_tcp_read_callback(xprt, desc);
115944b98efdSRicardo Labiaga }
116044b98efdSRicardo Labiaga #else
116144b98efdSRicardo Labiaga static inline int _xs_tcp_read_data(struct rpc_xprt *xprt,
116244b98efdSRicardo Labiaga 					struct xdr_skb_reader *desc)
116344b98efdSRicardo Labiaga {
116444b98efdSRicardo Labiaga 	return xs_tcp_read_reply(xprt, desc);
116544b98efdSRicardo Labiaga }
116644b98efdSRicardo Labiaga #endif /* CONFIG_NFS_V4_1 */
116744b98efdSRicardo Labiaga 
116844b98efdSRicardo Labiaga /*
116944b98efdSRicardo Labiaga  * Read data off the transport.  This can be either an RPC_CALL or an
117044b98efdSRicardo Labiaga  * RPC_REPLY.  Relay the processing to helper functions.
117144b98efdSRicardo Labiaga  */
117244b98efdSRicardo Labiaga static void xs_tcp_read_data(struct rpc_xprt *xprt,
117344b98efdSRicardo Labiaga 				    struct xdr_skb_reader *desc)
117444b98efdSRicardo Labiaga {
117544b98efdSRicardo Labiaga 	struct sock_xprt *transport =
117644b98efdSRicardo Labiaga 				container_of(xprt, struct sock_xprt, xprt);
117744b98efdSRicardo Labiaga 
117844b98efdSRicardo Labiaga 	if (_xs_tcp_read_data(xprt, desc) == 0)
117951971139SChuck Lever 		xs_tcp_check_fraghdr(transport);
118044b98efdSRicardo Labiaga 	else {
118144b98efdSRicardo Labiaga 		/*
118244b98efdSRicardo Labiaga 		 * The transport_lock protects the request handling.
118344b98efdSRicardo Labiaga 		 * There's no need to hold it to update the tcp_flags.
118444b98efdSRicardo Labiaga 		 */
118544b98efdSRicardo Labiaga 		transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
118644b98efdSRicardo Labiaga 	}
1187a246b010SChuck Lever }
1188a246b010SChuck Lever 
1189dd456471SChuck Lever static inline void xs_tcp_read_discard(struct sock_xprt *transport, struct xdr_skb_reader *desc)
1190a246b010SChuck Lever {
1191a246b010SChuck Lever 	size_t len;
1192a246b010SChuck Lever 
119351971139SChuck Lever 	len = transport->tcp_reclen - transport->tcp_offset;
1194a246b010SChuck Lever 	if (len > desc->count)
1195a246b010SChuck Lever 		len = desc->count;
1196a246b010SChuck Lever 	desc->count -= len;
1197a246b010SChuck Lever 	desc->offset += len;
119851971139SChuck Lever 	transport->tcp_offset += len;
1199a246b010SChuck Lever 	dprintk("RPC:       discarded %Zu bytes\n", len);
120051971139SChuck Lever 	xs_tcp_check_fraghdr(transport);
1201a246b010SChuck Lever }
1202a246b010SChuck Lever 
12039903cd1cSChuck Lever static int xs_tcp_data_recv(read_descriptor_t *rd_desc, struct sk_buff *skb, unsigned int offset, size_t len)
1204a246b010SChuck Lever {
1205a246b010SChuck Lever 	struct rpc_xprt *xprt = rd_desc->arg.data;
120651971139SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1207dd456471SChuck Lever 	struct xdr_skb_reader desc = {
1208a246b010SChuck Lever 		.skb	= skb,
1209a246b010SChuck Lever 		.offset	= offset,
1210a246b010SChuck Lever 		.count	= len,
1211a246b010SChuck Lever 	};
1212a246b010SChuck Lever 
12139903cd1cSChuck Lever 	dprintk("RPC:       xs_tcp_data_recv started\n");
1214a246b010SChuck Lever 	do {
1215a246b010SChuck Lever 		/* Read in a new fragment marker if necessary */
1216a246b010SChuck Lever 		/* Can we ever really expect to get completely empty fragments? */
1217e136d092SChuck Lever 		if (transport->tcp_flags & TCP_RCV_COPY_FRAGHDR) {
12189903cd1cSChuck Lever 			xs_tcp_read_fraghdr(xprt, &desc);
1219a246b010SChuck Lever 			continue;
1220a246b010SChuck Lever 		}
1221a246b010SChuck Lever 		/* Read in the xid if necessary */
1222e136d092SChuck Lever 		if (transport->tcp_flags & TCP_RCV_COPY_XID) {
122351971139SChuck Lever 			xs_tcp_read_xid(transport, &desc);
1224a246b010SChuck Lever 			continue;
1225a246b010SChuck Lever 		}
122618dca02aSRicardo Labiaga 		/* Read in the call/reply flag */
1227f4a2e418SRicardo Labiaga 		if (transport->tcp_flags & TCP_RCV_READ_CALLDIR) {
122818dca02aSRicardo Labiaga 			xs_tcp_read_calldir(transport, &desc);
122918dca02aSRicardo Labiaga 			continue;
123018dca02aSRicardo Labiaga 		}
1231a246b010SChuck Lever 		/* Read in the request data */
1232e136d092SChuck Lever 		if (transport->tcp_flags & TCP_RCV_COPY_DATA) {
123344b98efdSRicardo Labiaga 			xs_tcp_read_data(xprt, &desc);
1234a246b010SChuck Lever 			continue;
1235a246b010SChuck Lever 		}
1236a246b010SChuck Lever 		/* Skip over any trailing bytes on short reads */
123751971139SChuck Lever 		xs_tcp_read_discard(transport, &desc);
1238a246b010SChuck Lever 	} while (desc.count);
12399903cd1cSChuck Lever 	dprintk("RPC:       xs_tcp_data_recv done\n");
1240a246b010SChuck Lever 	return len - desc.count;
1241a246b010SChuck Lever }
1242a246b010SChuck Lever 
12439903cd1cSChuck Lever /**
12449903cd1cSChuck Lever  * xs_tcp_data_ready - "data ready" callback for TCP sockets
12459903cd1cSChuck Lever  * @sk: socket with data to read
12469903cd1cSChuck Lever  * @bytes: how much data to read
12479903cd1cSChuck Lever  *
12489903cd1cSChuck Lever  */
12499903cd1cSChuck Lever static void xs_tcp_data_ready(struct sock *sk, int bytes)
1250a246b010SChuck Lever {
1251a246b010SChuck Lever 	struct rpc_xprt *xprt;
1252a246b010SChuck Lever 	read_descriptor_t rd_desc;
1253ff2d7db8STrond Myklebust 	int read;
1254a246b010SChuck Lever 
12559903cd1cSChuck Lever 	dprintk("RPC:       xs_tcp_data_ready...\n");
125646121cf7SChuck Lever 
125746121cf7SChuck Lever 	read_lock(&sk->sk_callback_lock);
12589903cd1cSChuck Lever 	if (!(xprt = xprt_from_sock(sk)))
1259a246b010SChuck Lever 		goto out;
1260a246b010SChuck Lever 	if (xprt->shutdown)
1261a246b010SChuck Lever 		goto out;
1262a246b010SChuck Lever 
12639903cd1cSChuck Lever 	/* We use rd_desc to pass struct xprt to xs_tcp_data_recv */
1264a246b010SChuck Lever 	rd_desc.arg.data = xprt;
1265ff2d7db8STrond Myklebust 	do {
1266a246b010SChuck Lever 		rd_desc.count = 65536;
1267ff2d7db8STrond Myklebust 		read = tcp_read_sock(sk, &rd_desc, xs_tcp_data_recv);
1268ff2d7db8STrond Myklebust 	} while (read > 0);
1269a246b010SChuck Lever out:
1270a246b010SChuck Lever 	read_unlock(&sk->sk_callback_lock);
1271a246b010SChuck Lever }
1272a246b010SChuck Lever 
12737d1e8255STrond Myklebust /*
12747d1e8255STrond Myklebust  * Do the equivalent of linger/linger2 handling for dealing with
12757d1e8255STrond Myklebust  * broken servers that don't close the socket in a timely
12767d1e8255STrond Myklebust  * fashion
12777d1e8255STrond Myklebust  */
12787d1e8255STrond Myklebust static void xs_tcp_schedule_linger_timeout(struct rpc_xprt *xprt,
12797d1e8255STrond Myklebust 		unsigned long timeout)
12807d1e8255STrond Myklebust {
12817d1e8255STrond Myklebust 	struct sock_xprt *transport;
12827d1e8255STrond Myklebust 
12837d1e8255STrond Myklebust 	if (xprt_test_and_set_connecting(xprt))
12847d1e8255STrond Myklebust 		return;
12857d1e8255STrond Myklebust 	set_bit(XPRT_CONNECTION_ABORT, &xprt->state);
12867d1e8255STrond Myklebust 	transport = container_of(xprt, struct sock_xprt, xprt);
12877d1e8255STrond Myklebust 	queue_delayed_work(rpciod_workqueue, &transport->connect_worker,
12887d1e8255STrond Myklebust 			   timeout);
12897d1e8255STrond Myklebust }
12907d1e8255STrond Myklebust 
12917d1e8255STrond Myklebust static void xs_tcp_cancel_linger_timeout(struct rpc_xprt *xprt)
12927d1e8255STrond Myklebust {
12937d1e8255STrond Myklebust 	struct sock_xprt *transport;
12947d1e8255STrond Myklebust 
12957d1e8255STrond Myklebust 	transport = container_of(xprt, struct sock_xprt, xprt);
12967d1e8255STrond Myklebust 
12977d1e8255STrond Myklebust 	if (!test_bit(XPRT_CONNECTION_ABORT, &xprt->state) ||
12987d1e8255STrond Myklebust 	    !cancel_delayed_work(&transport->connect_worker))
12997d1e8255STrond Myklebust 		return;
13007d1e8255STrond Myklebust 	clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
13017d1e8255STrond Myklebust 	xprt_clear_connecting(xprt);
13027d1e8255STrond Myklebust }
13037d1e8255STrond Myklebust 
13047d1e8255STrond Myklebust static void xs_sock_mark_closed(struct rpc_xprt *xprt)
13057d1e8255STrond Myklebust {
13067d1e8255STrond Myklebust 	smp_mb__before_clear_bit();
13077d1e8255STrond Myklebust 	clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
13087d1e8255STrond Myklebust 	clear_bit(XPRT_CLOSING, &xprt->state);
13097d1e8255STrond Myklebust 	smp_mb__after_clear_bit();
13107d1e8255STrond Myklebust 	/* Mark transport as closed and wake up all pending tasks */
13117d1e8255STrond Myklebust 	xprt_disconnect_done(xprt);
13127d1e8255STrond Myklebust }
13137d1e8255STrond Myklebust 
13149903cd1cSChuck Lever /**
13159903cd1cSChuck Lever  * xs_tcp_state_change - callback to handle TCP socket state changes
13169903cd1cSChuck Lever  * @sk: socket whose state has changed
13179903cd1cSChuck Lever  *
13189903cd1cSChuck Lever  */
13199903cd1cSChuck Lever static void xs_tcp_state_change(struct sock *sk)
1320a246b010SChuck Lever {
1321a246b010SChuck Lever 	struct rpc_xprt *xprt;
1322a246b010SChuck Lever 
1323a246b010SChuck Lever 	read_lock(&sk->sk_callback_lock);
1324a246b010SChuck Lever 	if (!(xprt = xprt_from_sock(sk)))
1325a246b010SChuck Lever 		goto out;
13269903cd1cSChuck Lever 	dprintk("RPC:       xs_tcp_state_change client %p...\n", xprt);
1327a246b010SChuck Lever 	dprintk("RPC:       state %x conn %d dead %d zapped %d\n",
1328a246b010SChuck Lever 			sk->sk_state, xprt_connected(xprt),
1329a246b010SChuck Lever 			sock_flag(sk, SOCK_DEAD),
1330a246b010SChuck Lever 			sock_flag(sk, SOCK_ZAPPED));
1331a246b010SChuck Lever 
1332a246b010SChuck Lever 	switch (sk->sk_state) {
1333a246b010SChuck Lever 	case TCP_ESTABLISHED:
13344a0f8c04SChuck Lever 		spin_lock_bh(&xprt->transport_lock);
1335a246b010SChuck Lever 		if (!xprt_test_and_set_connected(xprt)) {
133651971139SChuck Lever 			struct sock_xprt *transport = container_of(xprt,
133751971139SChuck Lever 					struct sock_xprt, xprt);
133851971139SChuck Lever 
1339a246b010SChuck Lever 			/* Reset TCP record info */
134051971139SChuck Lever 			transport->tcp_offset = 0;
134151971139SChuck Lever 			transport->tcp_reclen = 0;
134251971139SChuck Lever 			transport->tcp_copied = 0;
1343e136d092SChuck Lever 			transport->tcp_flags =
1344e136d092SChuck Lever 				TCP_RCV_COPY_FRAGHDR | TCP_RCV_COPY_XID;
134551971139SChuck Lever 
13462a491991STrond Myklebust 			xprt_wake_pending_tasks(xprt, -EAGAIN);
1347a246b010SChuck Lever 		}
13484a0f8c04SChuck Lever 		spin_unlock_bh(&xprt->transport_lock);
1349a246b010SChuck Lever 		break;
13503b948ae5STrond Myklebust 	case TCP_FIN_WAIT1:
13513b948ae5STrond Myklebust 		/* The client initiated a shutdown of the socket */
13527c1d71cfSTrond Myklebust 		xprt->connect_cookie++;
1353663b8858STrond Myklebust 		xprt->reestablish_timeout = 0;
13543b948ae5STrond Myklebust 		set_bit(XPRT_CLOSING, &xprt->state);
13553b948ae5STrond Myklebust 		smp_mb__before_clear_bit();
13563b948ae5STrond Myklebust 		clear_bit(XPRT_CONNECTED, &xprt->state);
1357ef803670STrond Myklebust 		clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
13583b948ae5STrond Myklebust 		smp_mb__after_clear_bit();
135925fe6142STrond Myklebust 		xs_tcp_schedule_linger_timeout(xprt, xs_tcp_fin_timeout);
1360a246b010SChuck Lever 		break;
1361632e3bdcSTrond Myklebust 	case TCP_CLOSE_WAIT:
13623b948ae5STrond Myklebust 		/* The server initiated a shutdown of the socket */
136366af1e55STrond Myklebust 		xprt_force_disconnect(xprt);
1364663b8858STrond Myklebust 	case TCP_SYN_SENT:
13657c1d71cfSTrond Myklebust 		xprt->connect_cookie++;
1366663b8858STrond Myklebust 	case TCP_CLOSING:
1367663b8858STrond Myklebust 		/*
1368663b8858STrond Myklebust 		 * If the server closed down the connection, make sure that
1369663b8858STrond Myklebust 		 * we back off before reconnecting
1370663b8858STrond Myklebust 		 */
1371663b8858STrond Myklebust 		if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
1372663b8858STrond Myklebust 			xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
13733b948ae5STrond Myklebust 		break;
13743b948ae5STrond Myklebust 	case TCP_LAST_ACK:
1375670f9457STrond Myklebust 		set_bit(XPRT_CLOSING, &xprt->state);
137625fe6142STrond Myklebust 		xs_tcp_schedule_linger_timeout(xprt, xs_tcp_fin_timeout);
13773b948ae5STrond Myklebust 		smp_mb__before_clear_bit();
13783b948ae5STrond Myklebust 		clear_bit(XPRT_CONNECTED, &xprt->state);
13793b948ae5STrond Myklebust 		smp_mb__after_clear_bit();
13803b948ae5STrond Myklebust 		break;
13813b948ae5STrond Myklebust 	case TCP_CLOSE:
13827d1e8255STrond Myklebust 		xs_tcp_cancel_linger_timeout(xprt);
13837d1e8255STrond Myklebust 		xs_sock_mark_closed(xprt);
1384a246b010SChuck Lever 	}
1385a246b010SChuck Lever  out:
1386a246b010SChuck Lever 	read_unlock(&sk->sk_callback_lock);
1387a246b010SChuck Lever }
1388a246b010SChuck Lever 
13899903cd1cSChuck Lever /**
1390482f32e6STrond Myklebust  * xs_error_report - callback mainly for catching socket errors
13912a9e1cfaSTrond Myklebust  * @sk: socket
13922a9e1cfaSTrond Myklebust  */
1393482f32e6STrond Myklebust static void xs_error_report(struct sock *sk)
13942a9e1cfaSTrond Myklebust {
13952a9e1cfaSTrond Myklebust 	struct rpc_xprt *xprt;
13962a9e1cfaSTrond Myklebust 
13972a9e1cfaSTrond Myklebust 	read_lock(&sk->sk_callback_lock);
13982a9e1cfaSTrond Myklebust 	if (!(xprt = xprt_from_sock(sk)))
13992a9e1cfaSTrond Myklebust 		goto out;
14002a9e1cfaSTrond Myklebust 	dprintk("RPC:       %s client %p...\n"
14012a9e1cfaSTrond Myklebust 			"RPC:       error %d\n",
14022a9e1cfaSTrond Myklebust 			__func__, xprt, sk->sk_err);
1403482f32e6STrond Myklebust 	xprt_wake_pending_tasks(xprt, -EAGAIN);
14042a9e1cfaSTrond Myklebust out:
14052a9e1cfaSTrond Myklebust 	read_unlock(&sk->sk_callback_lock);
14062a9e1cfaSTrond Myklebust }
14072a9e1cfaSTrond Myklebust 
14081f0fa154SIlpo Järvinen static void xs_write_space(struct sock *sk)
14091f0fa154SIlpo Järvinen {
14101f0fa154SIlpo Järvinen 	struct socket *sock;
14111f0fa154SIlpo Järvinen 	struct rpc_xprt *xprt;
14121f0fa154SIlpo Järvinen 
14131f0fa154SIlpo Järvinen 	if (unlikely(!(sock = sk->sk_socket)))
14141f0fa154SIlpo Järvinen 		return;
14151f0fa154SIlpo Järvinen 	clear_bit(SOCK_NOSPACE, &sock->flags);
14161f0fa154SIlpo Järvinen 
14171f0fa154SIlpo Järvinen 	if (unlikely(!(xprt = xprt_from_sock(sk))))
14181f0fa154SIlpo Järvinen 		return;
14191f0fa154SIlpo Järvinen 	if (test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags) == 0)
14201f0fa154SIlpo Järvinen 		return;
14211f0fa154SIlpo Järvinen 
14221f0fa154SIlpo Järvinen 	xprt_write_space(xprt);
14231f0fa154SIlpo Järvinen }
14241f0fa154SIlpo Järvinen 
14252a9e1cfaSTrond Myklebust /**
1426c7b2cae8SChuck Lever  * xs_udp_write_space - callback invoked when socket buffer space
1427c7b2cae8SChuck Lever  *                             becomes available
14289903cd1cSChuck Lever  * @sk: socket whose state has changed
14299903cd1cSChuck Lever  *
1430a246b010SChuck Lever  * Called when more output buffer space is available for this socket.
1431a246b010SChuck Lever  * We try not to wake our writers until they can make "significant"
1432c7b2cae8SChuck Lever  * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1433a246b010SChuck Lever  * with a bunch of small requests.
1434a246b010SChuck Lever  */
1435c7b2cae8SChuck Lever static void xs_udp_write_space(struct sock *sk)
1436a246b010SChuck Lever {
1437a246b010SChuck Lever 	read_lock(&sk->sk_callback_lock);
1438c7b2cae8SChuck Lever 
1439c7b2cae8SChuck Lever 	/* from net/core/sock.c:sock_def_write_space */
14401f0fa154SIlpo Järvinen 	if (sock_writeable(sk))
14411f0fa154SIlpo Järvinen 		xs_write_space(sk);
1442c7b2cae8SChuck Lever 
1443c7b2cae8SChuck Lever 	read_unlock(&sk->sk_callback_lock);
1444c7b2cae8SChuck Lever }
1445c7b2cae8SChuck Lever 
1446c7b2cae8SChuck Lever /**
1447c7b2cae8SChuck Lever  * xs_tcp_write_space - callback invoked when socket buffer space
1448c7b2cae8SChuck Lever  *                             becomes available
1449c7b2cae8SChuck Lever  * @sk: socket whose state has changed
1450c7b2cae8SChuck Lever  *
1451c7b2cae8SChuck Lever  * Called when more output buffer space is available for this socket.
1452c7b2cae8SChuck Lever  * We try not to wake our writers until they can make "significant"
1453c7b2cae8SChuck Lever  * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1454c7b2cae8SChuck Lever  * with a bunch of small requests.
1455c7b2cae8SChuck Lever  */
1456c7b2cae8SChuck Lever static void xs_tcp_write_space(struct sock *sk)
1457c7b2cae8SChuck Lever {
1458c7b2cae8SChuck Lever 	read_lock(&sk->sk_callback_lock);
1459c7b2cae8SChuck Lever 
1460c7b2cae8SChuck Lever 	/* from net/core/stream.c:sk_stream_write_space */
14611f0fa154SIlpo Järvinen 	if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk))
14621f0fa154SIlpo Järvinen 		xs_write_space(sk);
1463c7b2cae8SChuck Lever 
1464a246b010SChuck Lever 	read_unlock(&sk->sk_callback_lock);
1465a246b010SChuck Lever }
1466a246b010SChuck Lever 
1467470056c2SChuck Lever static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt)
1468a246b010SChuck Lever {
1469ee0ac0c2SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1470ee0ac0c2SChuck Lever 	struct sock *sk = transport->inet;
1471a246b010SChuck Lever 
14727c6e066eSChuck Lever 	if (transport->rcvsize) {
1473a246b010SChuck Lever 		sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
14747c6e066eSChuck Lever 		sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2;
1475a246b010SChuck Lever 	}
14767c6e066eSChuck Lever 	if (transport->sndsize) {
1477a246b010SChuck Lever 		sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
14787c6e066eSChuck Lever 		sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2;
1479a246b010SChuck Lever 		sk->sk_write_space(sk);
1480a246b010SChuck Lever 	}
1481a246b010SChuck Lever }
1482a246b010SChuck Lever 
148343118c29SChuck Lever /**
1484470056c2SChuck Lever  * xs_udp_set_buffer_size - set send and receive limits
148543118c29SChuck Lever  * @xprt: generic transport
1486470056c2SChuck Lever  * @sndsize: requested size of send buffer, in bytes
1487470056c2SChuck Lever  * @rcvsize: requested size of receive buffer, in bytes
148843118c29SChuck Lever  *
1489470056c2SChuck Lever  * Set socket send and receive buffer size limits.
149043118c29SChuck Lever  */
1491470056c2SChuck Lever static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize)
149243118c29SChuck Lever {
14937c6e066eSChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
14947c6e066eSChuck Lever 
14957c6e066eSChuck Lever 	transport->sndsize = 0;
1496470056c2SChuck Lever 	if (sndsize)
14977c6e066eSChuck Lever 		transport->sndsize = sndsize + 1024;
14987c6e066eSChuck Lever 	transport->rcvsize = 0;
1499470056c2SChuck Lever 	if (rcvsize)
15007c6e066eSChuck Lever 		transport->rcvsize = rcvsize + 1024;
1501470056c2SChuck Lever 
1502470056c2SChuck Lever 	xs_udp_do_set_buffer_size(xprt);
150343118c29SChuck Lever }
150443118c29SChuck Lever 
150546c0ee8bSChuck Lever /**
150646c0ee8bSChuck Lever  * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
150746c0ee8bSChuck Lever  * @task: task that timed out
150846c0ee8bSChuck Lever  *
150946c0ee8bSChuck Lever  * Adjust the congestion window after a retransmit timeout has occurred.
151046c0ee8bSChuck Lever  */
151146c0ee8bSChuck Lever static void xs_udp_timer(struct rpc_task *task)
151246c0ee8bSChuck Lever {
151346c0ee8bSChuck Lever 	xprt_adjust_cwnd(task, -ETIMEDOUT);
151446c0ee8bSChuck Lever }
151546c0ee8bSChuck Lever 
1516b85d8806SChuck Lever static unsigned short xs_get_random_port(void)
1517b85d8806SChuck Lever {
1518b85d8806SChuck Lever 	unsigned short range = xprt_max_resvport - xprt_min_resvport;
1519b85d8806SChuck Lever 	unsigned short rand = (unsigned short) net_random() % range;
1520b85d8806SChuck Lever 	return rand + xprt_min_resvport;
1521b85d8806SChuck Lever }
1522b85d8806SChuck Lever 
152392200412SChuck Lever /**
152492200412SChuck Lever  * xs_set_port - reset the port number in the remote endpoint address
152592200412SChuck Lever  * @xprt: generic transport
152692200412SChuck Lever  * @port: new port number
152792200412SChuck Lever  *
152892200412SChuck Lever  */
152992200412SChuck Lever static void xs_set_port(struct rpc_xprt *xprt, unsigned short port)
153092200412SChuck Lever {
153195392c59SChuck Lever 	struct sockaddr *addr = xs_addr(xprt);
1532c4efcb1dSChuck Lever 
153392200412SChuck Lever 	dprintk("RPC:       setting port for xprt %p to %u\n", xprt, port);
1534c4efcb1dSChuck Lever 
153520612005SChuck Lever 	switch (addr->sa_family) {
153620612005SChuck Lever 	case AF_INET:
153720612005SChuck Lever 		((struct sockaddr_in *)addr)->sin_port = htons(port);
153820612005SChuck Lever 		break;
153920612005SChuck Lever 	case AF_INET6:
154020612005SChuck Lever 		((struct sockaddr_in6 *)addr)->sin6_port = htons(port);
154120612005SChuck Lever 		break;
154220612005SChuck Lever 	default:
154320612005SChuck Lever 		BUG();
154420612005SChuck Lever 	}
154592200412SChuck Lever }
154692200412SChuck Lever 
154767a391d7STrond Myklebust static unsigned short xs_get_srcport(struct sock_xprt *transport, struct socket *sock)
154867a391d7STrond Myklebust {
154967a391d7STrond Myklebust 	unsigned short port = transport->port;
155067a391d7STrond Myklebust 
155167a391d7STrond Myklebust 	if (port == 0 && transport->xprt.resvport)
155267a391d7STrond Myklebust 		port = xs_get_random_port();
155367a391d7STrond Myklebust 	return port;
155467a391d7STrond Myklebust }
155567a391d7STrond Myklebust 
155667a391d7STrond Myklebust static unsigned short xs_next_srcport(struct sock_xprt *transport, struct socket *sock, unsigned short port)
155767a391d7STrond Myklebust {
155867a391d7STrond Myklebust 	if (transport->port != 0)
155967a391d7STrond Myklebust 		transport->port = 0;
156067a391d7STrond Myklebust 	if (!transport->xprt.resvport)
156167a391d7STrond Myklebust 		return 0;
156267a391d7STrond Myklebust 	if (port <= xprt_min_resvport || port > xprt_max_resvport)
156367a391d7STrond Myklebust 		return xprt_max_resvport;
156467a391d7STrond Myklebust 	return --port;
156567a391d7STrond Myklebust }
156667a391d7STrond Myklebust 
15677dc753f0SChuck Lever static int xs_bind4(struct sock_xprt *transport, struct socket *sock)
1568a246b010SChuck Lever {
1569a246b010SChuck Lever 	struct sockaddr_in myaddr = {
1570a246b010SChuck Lever 		.sin_family = AF_INET,
1571a246b010SChuck Lever 	};
1572d3bc9a1dSFrank van Maarseveen 	struct sockaddr_in *sa;
157367a391d7STrond Myklebust 	int err, nloop = 0;
157467a391d7STrond Myklebust 	unsigned short port = xs_get_srcport(transport, sock);
157567a391d7STrond Myklebust 	unsigned short last;
1576a246b010SChuck Lever 
1577d3bc9a1dSFrank van Maarseveen 	sa = (struct sockaddr_in *)&transport->addr;
1578d3bc9a1dSFrank van Maarseveen 	myaddr.sin_addr = sa->sin_addr;
1579a246b010SChuck Lever 	do {
1580a246b010SChuck Lever 		myaddr.sin_port = htons(port);
1581e6242e92SSridhar Samudrala 		err = kernel_bind(sock, (struct sockaddr *) &myaddr,
1582a246b010SChuck Lever 						sizeof(myaddr));
158367a391d7STrond Myklebust 		if (port == 0)
1584d3bc9a1dSFrank van Maarseveen 			break;
1585a246b010SChuck Lever 		if (err == 0) {
1586c8475461SChuck Lever 			transport->port = port;
1587d3bc9a1dSFrank van Maarseveen 			break;
1588a246b010SChuck Lever 		}
158967a391d7STrond Myklebust 		last = port;
159067a391d7STrond Myklebust 		port = xs_next_srcport(transport, sock, port);
159167a391d7STrond Myklebust 		if (port > last)
159267a391d7STrond Myklebust 			nloop++;
159367a391d7STrond Myklebust 	} while (err == -EADDRINUSE && nloop != 2);
159421454aaaSHarvey Harrison 	dprintk("RPC:       %s %pI4:%u: %s (%d)\n",
159521454aaaSHarvey Harrison 			__func__, &myaddr.sin_addr,
15967dc753f0SChuck Lever 			port, err ? "failed" : "ok", err);
1597a246b010SChuck Lever 	return err;
1598a246b010SChuck Lever }
1599a246b010SChuck Lever 
160090058d37SChuck Lever static int xs_bind6(struct sock_xprt *transport, struct socket *sock)
160190058d37SChuck Lever {
160290058d37SChuck Lever 	struct sockaddr_in6 myaddr = {
160390058d37SChuck Lever 		.sin6_family = AF_INET6,
160490058d37SChuck Lever 	};
160590058d37SChuck Lever 	struct sockaddr_in6 *sa;
160667a391d7STrond Myklebust 	int err, nloop = 0;
160767a391d7STrond Myklebust 	unsigned short port = xs_get_srcport(transport, sock);
160867a391d7STrond Myklebust 	unsigned short last;
160990058d37SChuck Lever 
161090058d37SChuck Lever 	sa = (struct sockaddr_in6 *)&transport->addr;
161190058d37SChuck Lever 	myaddr.sin6_addr = sa->sin6_addr;
161290058d37SChuck Lever 	do {
161390058d37SChuck Lever 		myaddr.sin6_port = htons(port);
161490058d37SChuck Lever 		err = kernel_bind(sock, (struct sockaddr *) &myaddr,
161590058d37SChuck Lever 						sizeof(myaddr));
161667a391d7STrond Myklebust 		if (port == 0)
161790058d37SChuck Lever 			break;
161890058d37SChuck Lever 		if (err == 0) {
161990058d37SChuck Lever 			transport->port = port;
162090058d37SChuck Lever 			break;
162190058d37SChuck Lever 		}
162267a391d7STrond Myklebust 		last = port;
162367a391d7STrond Myklebust 		port = xs_next_srcport(transport, sock, port);
162467a391d7STrond Myklebust 		if (port > last)
162567a391d7STrond Myklebust 			nloop++;
162667a391d7STrond Myklebust 	} while (err == -EADDRINUSE && nloop != 2);
16275b095d98SHarvey Harrison 	dprintk("RPC:       xs_bind6 %pI6:%u: %s (%d)\n",
1628fdb46ee7SHarvey Harrison 		&myaddr.sin6_addr, port, err ? "failed" : "ok", err);
1629a246b010SChuck Lever 	return err;
1630a246b010SChuck Lever }
1631a246b010SChuck Lever 
1632ed07536eSPeter Zijlstra #ifdef CONFIG_DEBUG_LOCK_ALLOC
1633ed07536eSPeter Zijlstra static struct lock_class_key xs_key[2];
1634ed07536eSPeter Zijlstra static struct lock_class_key xs_slock_key[2];
1635ed07536eSPeter Zijlstra 
16368945ee5eSChuck Lever static inline void xs_reclassify_socket4(struct socket *sock)
1637ed07536eSPeter Zijlstra {
1638ed07536eSPeter Zijlstra 	struct sock *sk = sock->sk;
16398945ee5eSChuck Lever 
164002b3d346SJohn Heffner 	BUG_ON(sock_owned_by_user(sk));
16418945ee5eSChuck Lever 	sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC",
16428945ee5eSChuck Lever 		&xs_slock_key[0], "sk_lock-AF_INET-RPC", &xs_key[0]);
1643ed07536eSPeter Zijlstra }
16448945ee5eSChuck Lever 
16458945ee5eSChuck Lever static inline void xs_reclassify_socket6(struct socket *sock)
16468945ee5eSChuck Lever {
16478945ee5eSChuck Lever 	struct sock *sk = sock->sk;
16488945ee5eSChuck Lever 
1649f4921affSLinus Torvalds 	BUG_ON(sock_owned_by_user(sk));
16508945ee5eSChuck Lever 	sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC",
16518945ee5eSChuck Lever 		&xs_slock_key[1], "sk_lock-AF_INET6-RPC", &xs_key[1]);
1652ed07536eSPeter Zijlstra }
1653ed07536eSPeter Zijlstra #else
16548945ee5eSChuck Lever static inline void xs_reclassify_socket4(struct socket *sock)
16558945ee5eSChuck Lever {
16568945ee5eSChuck Lever }
16578945ee5eSChuck Lever 
16588945ee5eSChuck Lever static inline void xs_reclassify_socket6(struct socket *sock)
1659ed07536eSPeter Zijlstra {
1660ed07536eSPeter Zijlstra }
1661ed07536eSPeter Zijlstra #endif
1662ed07536eSPeter Zijlstra 
166316be2d20SChuck Lever static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
1664a246b010SChuck Lever {
166516be2d20SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1666edb267a6SChuck Lever 
1667ee0ac0c2SChuck Lever 	if (!transport->inet) {
1668b0d93ad5SChuck Lever 		struct sock *sk = sock->sk;
1669b0d93ad5SChuck Lever 
1670b0d93ad5SChuck Lever 		write_lock_bh(&sk->sk_callback_lock);
1671b0d93ad5SChuck Lever 
16722a9e1cfaSTrond Myklebust 		xs_save_old_callbacks(transport, sk);
16732a9e1cfaSTrond Myklebust 
1674b0d93ad5SChuck Lever 		sk->sk_user_data = xprt;
1675b0d93ad5SChuck Lever 		sk->sk_data_ready = xs_udp_data_ready;
1676b0d93ad5SChuck Lever 		sk->sk_write_space = xs_udp_write_space;
1677482f32e6STrond Myklebust 		sk->sk_error_report = xs_error_report;
1678b0d93ad5SChuck Lever 		sk->sk_no_check = UDP_CSUM_NORCV;
1679b079fa7bSTrond Myklebust 		sk->sk_allocation = GFP_ATOMIC;
1680b0d93ad5SChuck Lever 
1681b0d93ad5SChuck Lever 		xprt_set_connected(xprt);
1682b0d93ad5SChuck Lever 
1683b0d93ad5SChuck Lever 		/* Reset to new socket */
1684ee0ac0c2SChuck Lever 		transport->sock = sock;
1685ee0ac0c2SChuck Lever 		transport->inet = sk;
1686b0d93ad5SChuck Lever 
1687b0d93ad5SChuck Lever 		write_unlock_bh(&sk->sk_callback_lock);
1688b0d93ad5SChuck Lever 	}
1689470056c2SChuck Lever 	xs_udp_do_set_buffer_size(xprt);
169016be2d20SChuck Lever }
169116be2d20SChuck Lever 
1692a246b010SChuck Lever /**
16939c3d72deSChuck Lever  * xs_udp_connect_worker4 - set up a UDP socket
1694a246b010SChuck Lever  * @work: RPC transport to connect
1695a246b010SChuck Lever  *
1696a246b010SChuck Lever  * Invoked by a work queue tasklet.
1697a246b010SChuck Lever  */
16989c3d72deSChuck Lever static void xs_udp_connect_worker4(struct work_struct *work)
1699a246b010SChuck Lever {
1700a246b010SChuck Lever 	struct sock_xprt *transport =
1701a246b010SChuck Lever 		container_of(work, struct sock_xprt, connect_worker.work);
1702a246b010SChuck Lever 	struct rpc_xprt *xprt = &transport->xprt;
1703a246b010SChuck Lever 	struct socket *sock = transport->sock;
1704a246b010SChuck Lever 	int err, status = -EIO;
1705a246b010SChuck Lever 
170601d37c42STrond Myklebust 	if (xprt->shutdown)
17079903cd1cSChuck Lever 		goto out;
17089903cd1cSChuck Lever 
1709a246b010SChuck Lever 	/* Start by resetting any existing state */
1710fe315e76SChuck Lever 	xs_reset_transport(transport);
1711a246b010SChuck Lever 
1712fe315e76SChuck Lever 	err = sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock);
1713fe315e76SChuck Lever 	if (err < 0) {
17149903cd1cSChuck Lever 		dprintk("RPC:       can't create UDP transport socket (%d).\n", -err);
1715a246b010SChuck Lever 		goto out;
1716a246b010SChuck Lever 	}
17178945ee5eSChuck Lever 	xs_reclassify_socket4(sock);
1718a246b010SChuck Lever 
17197dc753f0SChuck Lever 	if (xs_bind4(transport, sock)) {
17209903cd1cSChuck Lever 		sock_release(sock);
17219903cd1cSChuck Lever 		goto out;
1722a246b010SChuck Lever 	}
1723b0d93ad5SChuck Lever 
1724b0d93ad5SChuck Lever 	dprintk("RPC:       worker connecting xprt %p to address: %s\n",
1725b0d93ad5SChuck Lever 			xprt, xprt->address_strings[RPC_DISPLAY_ALL]);
1726b0d93ad5SChuck Lever 
172716be2d20SChuck Lever 	xs_udp_finish_connecting(xprt, sock);
1728a246b010SChuck Lever 	status = 0;
1729b0d93ad5SChuck Lever out:
1730b0d93ad5SChuck Lever 	xprt_clear_connecting(xprt);
17317d1e8255STrond Myklebust 	xprt_wake_pending_tasks(xprt, status);
1732b0d93ad5SChuck Lever }
1733b0d93ad5SChuck Lever 
173468e220bdSChuck Lever /**
173568e220bdSChuck Lever  * xs_udp_connect_worker6 - set up a UDP socket
173668e220bdSChuck Lever  * @work: RPC transport to connect
173768e220bdSChuck Lever  *
173868e220bdSChuck Lever  * Invoked by a work queue tasklet.
173968e220bdSChuck Lever  */
174068e220bdSChuck Lever static void xs_udp_connect_worker6(struct work_struct *work)
174168e220bdSChuck Lever {
174268e220bdSChuck Lever 	struct sock_xprt *transport =
174368e220bdSChuck Lever 		container_of(work, struct sock_xprt, connect_worker.work);
174468e220bdSChuck Lever 	struct rpc_xprt *xprt = &transport->xprt;
174568e220bdSChuck Lever 	struct socket *sock = transport->sock;
174668e220bdSChuck Lever 	int err, status = -EIO;
174768e220bdSChuck Lever 
174801d37c42STrond Myklebust 	if (xprt->shutdown)
174968e220bdSChuck Lever 		goto out;
175068e220bdSChuck Lever 
175168e220bdSChuck Lever 	/* Start by resetting any existing state */
1752fe315e76SChuck Lever 	xs_reset_transport(transport);
175368e220bdSChuck Lever 
1754fe315e76SChuck Lever 	err = sock_create_kern(PF_INET6, SOCK_DGRAM, IPPROTO_UDP, &sock);
1755fe315e76SChuck Lever 	if (err < 0) {
175668e220bdSChuck Lever 		dprintk("RPC:       can't create UDP transport socket (%d).\n", -err);
175768e220bdSChuck Lever 		goto out;
175868e220bdSChuck Lever 	}
17598945ee5eSChuck Lever 	xs_reclassify_socket6(sock);
176068e220bdSChuck Lever 
176168e220bdSChuck Lever 	if (xs_bind6(transport, sock) < 0) {
176268e220bdSChuck Lever 		sock_release(sock);
176368e220bdSChuck Lever 		goto out;
176468e220bdSChuck Lever 	}
176568e220bdSChuck Lever 
176668e220bdSChuck Lever 	dprintk("RPC:       worker connecting xprt %p to address: %s\n",
176768e220bdSChuck Lever 			xprt, xprt->address_strings[RPC_DISPLAY_ALL]);
176868e220bdSChuck Lever 
176968e220bdSChuck Lever 	xs_udp_finish_connecting(xprt, sock);
1770a246b010SChuck Lever 	status = 0;
1771b0d93ad5SChuck Lever out:
1772b0d93ad5SChuck Lever 	xprt_clear_connecting(xprt);
17737d1e8255STrond Myklebust 	xprt_wake_pending_tasks(xprt, status);
1774b0d93ad5SChuck Lever }
1775b0d93ad5SChuck Lever 
17763167e12cSChuck Lever /*
17773167e12cSChuck Lever  * We need to preserve the port number so the reply cache on the server can
17783167e12cSChuck Lever  * find our cached RPC replies when we get around to reconnecting.
17793167e12cSChuck Lever  */
178040d2549dSTrond Myklebust static void xs_abort_connection(struct rpc_xprt *xprt, struct sock_xprt *transport)
17813167e12cSChuck Lever {
17823167e12cSChuck Lever 	int result;
17833167e12cSChuck Lever 	struct sockaddr any;
17843167e12cSChuck Lever 
17853167e12cSChuck Lever 	dprintk("RPC:       disconnecting xprt %p to reuse port\n", xprt);
17863167e12cSChuck Lever 
17873167e12cSChuck Lever 	/*
17883167e12cSChuck Lever 	 * Disconnect the transport socket by doing a connect operation
17893167e12cSChuck Lever 	 * with AF_UNSPEC.  This should return immediately...
17903167e12cSChuck Lever 	 */
17913167e12cSChuck Lever 	memset(&any, 0, sizeof(any));
17923167e12cSChuck Lever 	any.sa_family = AF_UNSPEC;
1793ee0ac0c2SChuck Lever 	result = kernel_connect(transport->sock, &any, sizeof(any), 0);
17947d1e8255STrond Myklebust 	if (!result)
17957d1e8255STrond Myklebust 		xs_sock_mark_closed(xprt);
17967d1e8255STrond Myklebust 	else
17973167e12cSChuck Lever 		dprintk("RPC:       AF_UNSPEC connect return code %d\n",
17983167e12cSChuck Lever 				result);
17993167e12cSChuck Lever }
18003167e12cSChuck Lever 
180140d2549dSTrond Myklebust static void xs_tcp_reuse_connection(struct rpc_xprt *xprt, struct sock_xprt *transport)
180240d2549dSTrond Myklebust {
180340d2549dSTrond Myklebust 	unsigned int state = transport->inet->sk_state;
180440d2549dSTrond Myklebust 
180540d2549dSTrond Myklebust 	if (state == TCP_CLOSE && transport->sock->state == SS_UNCONNECTED)
180640d2549dSTrond Myklebust 		return;
180740d2549dSTrond Myklebust 	if ((1 << state) & (TCPF_ESTABLISHED|TCPF_SYN_SENT))
180840d2549dSTrond Myklebust 		return;
180940d2549dSTrond Myklebust 	xs_abort_connection(xprt, transport);
181040d2549dSTrond Myklebust }
181140d2549dSTrond Myklebust 
181216be2d20SChuck Lever static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
1813b0d93ad5SChuck Lever {
181416be2d20SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1815edb267a6SChuck Lever 
1816ee0ac0c2SChuck Lever 	if (!transport->inet) {
1817b0d93ad5SChuck Lever 		struct sock *sk = sock->sk;
1818b0d93ad5SChuck Lever 
1819b0d93ad5SChuck Lever 		write_lock_bh(&sk->sk_callback_lock);
1820b0d93ad5SChuck Lever 
18212a9e1cfaSTrond Myklebust 		xs_save_old_callbacks(transport, sk);
18222a9e1cfaSTrond Myklebust 
1823b0d93ad5SChuck Lever 		sk->sk_user_data = xprt;
1824b0d93ad5SChuck Lever 		sk->sk_data_ready = xs_tcp_data_ready;
1825b0d93ad5SChuck Lever 		sk->sk_state_change = xs_tcp_state_change;
1826b0d93ad5SChuck Lever 		sk->sk_write_space = xs_tcp_write_space;
1827482f32e6STrond Myklebust 		sk->sk_error_report = xs_error_report;
1828b079fa7bSTrond Myklebust 		sk->sk_allocation = GFP_ATOMIC;
18293167e12cSChuck Lever 
18303167e12cSChuck Lever 		/* socket options */
18313167e12cSChuck Lever 		sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
18323167e12cSChuck Lever 		sock_reset_flag(sk, SOCK_LINGER);
18333167e12cSChuck Lever 		tcp_sk(sk)->linger2 = 0;
18343167e12cSChuck Lever 		tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF;
1835b0d93ad5SChuck Lever 
1836b0d93ad5SChuck Lever 		xprt_clear_connected(xprt);
1837b0d93ad5SChuck Lever 
1838b0d93ad5SChuck Lever 		/* Reset to new socket */
1839ee0ac0c2SChuck Lever 		transport->sock = sock;
1840ee0ac0c2SChuck Lever 		transport->inet = sk;
1841b0d93ad5SChuck Lever 
1842b0d93ad5SChuck Lever 		write_unlock_bh(&sk->sk_callback_lock);
1843b0d93ad5SChuck Lever 	}
1844b0d93ad5SChuck Lever 
184501d37c42STrond Myklebust 	if (!xprt_bound(xprt))
184601d37c42STrond Myklebust 		return -ENOTCONN;
184701d37c42STrond Myklebust 
1848b0d93ad5SChuck Lever 	/* Tell the socket layer to start connecting... */
1849262ca07dSChuck Lever 	xprt->stat.connect_count++;
1850262ca07dSChuck Lever 	xprt->stat.connect_start = jiffies;
185195392c59SChuck Lever 	return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK);
185216be2d20SChuck Lever }
185316be2d20SChuck Lever 
185416be2d20SChuck Lever /**
1855b61d59ffSTrond Myklebust  * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint
1856b61d59ffSTrond Myklebust  * @xprt: RPC transport to connect
1857b61d59ffSTrond Myklebust  * @transport: socket transport to connect
1858b61d59ffSTrond Myklebust  * @create_sock: function to create a socket of the correct type
185916be2d20SChuck Lever  *
186016be2d20SChuck Lever  * Invoked by a work queue tasklet.
186116be2d20SChuck Lever  */
1862b61d59ffSTrond Myklebust static void xs_tcp_setup_socket(struct rpc_xprt *xprt,
1863b61d59ffSTrond Myklebust 		struct sock_xprt *transport,
1864b61d59ffSTrond Myklebust 		struct socket *(*create_sock)(struct rpc_xprt *,
1865b61d59ffSTrond Myklebust 			struct sock_xprt *))
186616be2d20SChuck Lever {
186716be2d20SChuck Lever 	struct socket *sock = transport->sock;
1868b61d59ffSTrond Myklebust 	int status = -EIO;
186916be2d20SChuck Lever 
187001d37c42STrond Myklebust 	if (xprt->shutdown)
187116be2d20SChuck Lever 		goto out;
187216be2d20SChuck Lever 
187316be2d20SChuck Lever 	if (!sock) {
18747d1e8255STrond Myklebust 		clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1875b61d59ffSTrond Myklebust 		sock = create_sock(xprt, transport);
1876b61d59ffSTrond Myklebust 		if (IS_ERR(sock)) {
1877b61d59ffSTrond Myklebust 			status = PTR_ERR(sock);
187816be2d20SChuck Lever 			goto out;
187916be2d20SChuck Lever 		}
18807d1e8255STrond Myklebust 	} else {
18817d1e8255STrond Myklebust 		int abort_and_exit;
18827d1e8255STrond Myklebust 
18837d1e8255STrond Myklebust 		abort_and_exit = test_and_clear_bit(XPRT_CONNECTION_ABORT,
18847d1e8255STrond Myklebust 				&xprt->state);
188516be2d20SChuck Lever 		/* "close" the socket, preserving the local port */
188640d2549dSTrond Myklebust 		xs_tcp_reuse_connection(xprt, transport);
188716be2d20SChuck Lever 
18887d1e8255STrond Myklebust 		if (abort_and_exit)
18897d1e8255STrond Myklebust 			goto out_eagain;
18907d1e8255STrond Myklebust 	}
18917d1e8255STrond Myklebust 
189216be2d20SChuck Lever 	dprintk("RPC:       worker connecting xprt %p to address: %s\n",
189316be2d20SChuck Lever 			xprt, xprt->address_strings[RPC_DISPLAY_ALL]);
189416be2d20SChuck Lever 
189516be2d20SChuck Lever 	status = xs_tcp_finish_connecting(xprt, sock);
1896a246b010SChuck Lever 	dprintk("RPC:       %p connect status %d connected %d sock state %d\n",
189746121cf7SChuck Lever 			xprt, -status, xprt_connected(xprt),
189846121cf7SChuck Lever 			sock->sk->sk_state);
1899a246b010SChuck Lever 	switch (status) {
1900f75e6745STrond Myklebust 	default:
1901f75e6745STrond Myklebust 		printk("%s: connect returned unhandled error %d\n",
1902f75e6745STrond Myklebust 			__func__, status);
1903f75e6745STrond Myklebust 	case -EADDRNOTAVAIL:
1904f75e6745STrond Myklebust 		/* We're probably in TIME_WAIT. Get rid of existing socket,
1905f75e6745STrond Myklebust 		 * and retry
1906f75e6745STrond Myklebust 		 */
1907f75e6745STrond Myklebust 		set_bit(XPRT_CONNECTION_CLOSE, &xprt->state);
1908f75e6745STrond Myklebust 		xprt_force_disconnect(xprt);
190988b5ed73STrond Myklebust 		break;
19108a2cec29STrond Myklebust 	case -ECONNREFUSED:
19118a2cec29STrond Myklebust 	case -ECONNRESET:
19128a2cec29STrond Myklebust 	case -ENETUNREACH:
19138a2cec29STrond Myklebust 		/* retry with existing socket, after a delay */
19142a491991STrond Myklebust 	case 0:
1915a246b010SChuck Lever 	case -EINPROGRESS:
1916a246b010SChuck Lever 	case -EALREADY:
19177d1e8255STrond Myklebust 		xprt_clear_connecting(xprt);
19187d1e8255STrond Myklebust 		return;
19198a2cec29STrond Myklebust 	}
19207d1e8255STrond Myklebust out_eagain:
19212a491991STrond Myklebust 	status = -EAGAIN;
1922a246b010SChuck Lever out:
19232226feb6SChuck Lever 	xprt_clear_connecting(xprt);
19247d1e8255STrond Myklebust 	xprt_wake_pending_tasks(xprt, status);
1925a246b010SChuck Lever }
1926a246b010SChuck Lever 
1927b61d59ffSTrond Myklebust static struct socket *xs_create_tcp_sock4(struct rpc_xprt *xprt,
1928b61d59ffSTrond Myklebust 		struct sock_xprt *transport)
1929b61d59ffSTrond Myklebust {
1930b61d59ffSTrond Myklebust 	struct socket *sock;
1931b61d59ffSTrond Myklebust 	int err;
1932b61d59ffSTrond Myklebust 
1933b61d59ffSTrond Myklebust 	/* start from scratch */
1934b61d59ffSTrond Myklebust 	err = sock_create_kern(PF_INET, SOCK_STREAM, IPPROTO_TCP, &sock);
1935b61d59ffSTrond Myklebust 	if (err < 0) {
1936b61d59ffSTrond Myklebust 		dprintk("RPC:       can't create TCP transport socket (%d).\n",
1937b61d59ffSTrond Myklebust 				-err);
1938b61d59ffSTrond Myklebust 		goto out_err;
1939b61d59ffSTrond Myklebust 	}
1940b61d59ffSTrond Myklebust 	xs_reclassify_socket4(sock);
1941b61d59ffSTrond Myklebust 
1942b61d59ffSTrond Myklebust 	if (xs_bind4(transport, sock) < 0) {
1943b61d59ffSTrond Myklebust 		sock_release(sock);
1944b61d59ffSTrond Myklebust 		goto out_err;
1945b61d59ffSTrond Myklebust 	}
1946b61d59ffSTrond Myklebust 	return sock;
1947b61d59ffSTrond Myklebust out_err:
1948b61d59ffSTrond Myklebust 	return ERR_PTR(-EIO);
1949b61d59ffSTrond Myklebust }
1950b61d59ffSTrond Myklebust 
1951b61d59ffSTrond Myklebust /**
1952a246b010SChuck Lever  * xs_tcp_connect_worker4 - connect a TCP socket to a remote endpoint
1953a246b010SChuck Lever  * @work: RPC transport to connect
1954a246b010SChuck Lever  *
1955a246b010SChuck Lever  * Invoked by a work queue tasklet.
1956a246b010SChuck Lever  */
1957a246b010SChuck Lever static void xs_tcp_connect_worker4(struct work_struct *work)
1958a246b010SChuck Lever {
1959a246b010SChuck Lever 	struct sock_xprt *transport =
1960a246b010SChuck Lever 		container_of(work, struct sock_xprt, connect_worker.work);
1961a246b010SChuck Lever 	struct rpc_xprt *xprt = &transport->xprt;
1962a246b010SChuck Lever 
1963b61d59ffSTrond Myklebust 	xs_tcp_setup_socket(xprt, transport, xs_create_tcp_sock4);
1964b61d59ffSTrond Myklebust }
1965a246b010SChuck Lever 
1966b61d59ffSTrond Myklebust static struct socket *xs_create_tcp_sock6(struct rpc_xprt *xprt,
1967b61d59ffSTrond Myklebust 		struct sock_xprt *transport)
1968b61d59ffSTrond Myklebust {
1969b61d59ffSTrond Myklebust 	struct socket *sock;
1970b61d59ffSTrond Myklebust 	int err;
1971b61d59ffSTrond Myklebust 
1972a246b010SChuck Lever 	/* start from scratch */
1973b61d59ffSTrond Myklebust 	err = sock_create_kern(PF_INET6, SOCK_STREAM, IPPROTO_TCP, &sock);
1974b61d59ffSTrond Myklebust 	if (err < 0) {
1975b61d59ffSTrond Myklebust 		dprintk("RPC:       can't create TCP transport socket (%d).\n",
1976b61d59ffSTrond Myklebust 				-err);
1977b61d59ffSTrond Myklebust 		goto out_err;
19789903cd1cSChuck Lever 	}
1979b61d59ffSTrond Myklebust 	xs_reclassify_socket6(sock);
19809903cd1cSChuck Lever 
1981b61d59ffSTrond Myklebust 	if (xs_bind6(transport, sock) < 0) {
1982a246b010SChuck Lever 		sock_release(sock);
1983b61d59ffSTrond Myklebust 		goto out_err;
1984a246b010SChuck Lever 	}
1985b61d59ffSTrond Myklebust 	return sock;
1986b61d59ffSTrond Myklebust out_err:
1987b61d59ffSTrond Myklebust 	return ERR_PTR(-EIO);
1988a246b010SChuck Lever }
1989a246b010SChuck Lever 
1990a246b010SChuck Lever /**
199168e220bdSChuck Lever  * xs_tcp_connect_worker6 - connect a TCP socket to a remote endpoint
199268e220bdSChuck Lever  * @work: RPC transport to connect
199368e220bdSChuck Lever  *
199468e220bdSChuck Lever  * Invoked by a work queue tasklet.
199568e220bdSChuck Lever  */
199668e220bdSChuck Lever static void xs_tcp_connect_worker6(struct work_struct *work)
199768e220bdSChuck Lever {
199868e220bdSChuck Lever 	struct sock_xprt *transport =
199968e220bdSChuck Lever 		container_of(work, struct sock_xprt, connect_worker.work);
200068e220bdSChuck Lever 	struct rpc_xprt *xprt = &transport->xprt;
200168e220bdSChuck Lever 
2002b61d59ffSTrond Myklebust 	xs_tcp_setup_socket(xprt, transport, xs_create_tcp_sock6);
200368e220bdSChuck Lever }
200468e220bdSChuck Lever 
200568e220bdSChuck Lever /**
2006a246b010SChuck Lever  * xs_connect - connect a socket to a remote endpoint
2007a246b010SChuck Lever  * @task: address of RPC task that manages state of connect request
2008a246b010SChuck Lever  *
2009a246b010SChuck Lever  * TCP: If the remote end dropped the connection, delay reconnecting.
201003bf4b70SChuck Lever  *
201103bf4b70SChuck Lever  * UDP socket connects are synchronous, but we use a work queue anyway
201203bf4b70SChuck Lever  * to guarantee that even unprivileged user processes can set up a
201303bf4b70SChuck Lever  * socket on a privileged port.
201403bf4b70SChuck Lever  *
201503bf4b70SChuck Lever  * If a UDP socket connect fails, the delay behavior here prevents
201603bf4b70SChuck Lever  * retry floods (hard mounts).
2017a246b010SChuck Lever  */
2018a246b010SChuck Lever static void xs_connect(struct rpc_task *task)
2019a246b010SChuck Lever {
2020a246b010SChuck Lever 	struct rpc_xprt *xprt = task->tk_xprt;
2021ee0ac0c2SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2022a246b010SChuck Lever 
2023b0d93ad5SChuck Lever 	if (xprt_test_and_set_connecting(xprt))
2024b0d93ad5SChuck Lever 		return;
2025b0d93ad5SChuck Lever 
2026ee0ac0c2SChuck Lever 	if (transport->sock != NULL) {
202746121cf7SChuck Lever 		dprintk("RPC:       xs_connect delayed xprt %p for %lu "
202846121cf7SChuck Lever 				"seconds\n",
202903bf4b70SChuck Lever 				xprt, xprt->reestablish_timeout / HZ);
2030c1384c9cSTrond Myklebust 		queue_delayed_work(rpciod_workqueue,
2031c1384c9cSTrond Myklebust 				   &transport->connect_worker,
203203bf4b70SChuck Lever 				   xprt->reestablish_timeout);
203303bf4b70SChuck Lever 		xprt->reestablish_timeout <<= 1;
203403bf4b70SChuck Lever 		if (xprt->reestablish_timeout > XS_TCP_MAX_REEST_TO)
203503bf4b70SChuck Lever 			xprt->reestablish_timeout = XS_TCP_MAX_REEST_TO;
20369903cd1cSChuck Lever 	} else {
20379903cd1cSChuck Lever 		dprintk("RPC:       xs_connect scheduled xprt %p\n", xprt);
2038c1384c9cSTrond Myklebust 		queue_delayed_work(rpciod_workqueue,
2039c1384c9cSTrond Myklebust 				   &transport->connect_worker, 0);
2040a246b010SChuck Lever 	}
2041a246b010SChuck Lever }
2042a246b010SChuck Lever 
2043e06799f9STrond Myklebust static void xs_tcp_connect(struct rpc_task *task)
2044e06799f9STrond Myklebust {
2045e06799f9STrond Myklebust 	struct rpc_xprt *xprt = task->tk_xprt;
2046e06799f9STrond Myklebust 
2047e06799f9STrond Myklebust 	/* Exit if we need to wait for socket shutdown to complete */
2048e06799f9STrond Myklebust 	if (test_bit(XPRT_CLOSING, &xprt->state))
2049e06799f9STrond Myklebust 		return;
2050e06799f9STrond Myklebust 	xs_connect(task);
2051e06799f9STrond Myklebust }
2052e06799f9STrond Myklebust 
2053262ca07dSChuck Lever /**
2054262ca07dSChuck Lever  * xs_udp_print_stats - display UDP socket-specifc stats
2055262ca07dSChuck Lever  * @xprt: rpc_xprt struct containing statistics
2056262ca07dSChuck Lever  * @seq: output file
2057262ca07dSChuck Lever  *
2058262ca07dSChuck Lever  */
2059262ca07dSChuck Lever static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2060262ca07dSChuck Lever {
2061c8475461SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2062c8475461SChuck Lever 
2063262ca07dSChuck Lever 	seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %Lu %Lu\n",
2064c8475461SChuck Lever 			transport->port,
2065262ca07dSChuck Lever 			xprt->stat.bind_count,
2066262ca07dSChuck Lever 			xprt->stat.sends,
2067262ca07dSChuck Lever 			xprt->stat.recvs,
2068262ca07dSChuck Lever 			xprt->stat.bad_xids,
2069262ca07dSChuck Lever 			xprt->stat.req_u,
2070262ca07dSChuck Lever 			xprt->stat.bklog_u);
2071262ca07dSChuck Lever }
2072262ca07dSChuck Lever 
2073262ca07dSChuck Lever /**
2074262ca07dSChuck Lever  * xs_tcp_print_stats - display TCP socket-specifc stats
2075262ca07dSChuck Lever  * @xprt: rpc_xprt struct containing statistics
2076262ca07dSChuck Lever  * @seq: output file
2077262ca07dSChuck Lever  *
2078262ca07dSChuck Lever  */
2079262ca07dSChuck Lever static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2080262ca07dSChuck Lever {
2081c8475461SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2082262ca07dSChuck Lever 	long idle_time = 0;
2083262ca07dSChuck Lever 
2084262ca07dSChuck Lever 	if (xprt_connected(xprt))
2085262ca07dSChuck Lever 		idle_time = (long)(jiffies - xprt->last_used) / HZ;
2086262ca07dSChuck Lever 
2087262ca07dSChuck Lever 	seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu %Lu %Lu\n",
2088c8475461SChuck Lever 			transport->port,
2089262ca07dSChuck Lever 			xprt->stat.bind_count,
2090262ca07dSChuck Lever 			xprt->stat.connect_count,
2091262ca07dSChuck Lever 			xprt->stat.connect_time,
2092262ca07dSChuck Lever 			idle_time,
2093262ca07dSChuck Lever 			xprt->stat.sends,
2094262ca07dSChuck Lever 			xprt->stat.recvs,
2095262ca07dSChuck Lever 			xprt->stat.bad_xids,
2096262ca07dSChuck Lever 			xprt->stat.req_u,
2097262ca07dSChuck Lever 			xprt->stat.bklog_u);
2098262ca07dSChuck Lever }
2099262ca07dSChuck Lever 
2100262965f5SChuck Lever static struct rpc_xprt_ops xs_udp_ops = {
210143118c29SChuck Lever 	.set_buffer_size	= xs_udp_set_buffer_size,
210212a80469SChuck Lever 	.reserve_xprt		= xprt_reserve_xprt_cong,
210349e9a890SChuck Lever 	.release_xprt		= xprt_release_xprt_cong,
210445160d62SChuck Lever 	.rpcbind		= rpcb_getport_async,
210592200412SChuck Lever 	.set_port		= xs_set_port,
21069903cd1cSChuck Lever 	.connect		= xs_connect,
210702107148SChuck Lever 	.buf_alloc		= rpc_malloc,
210802107148SChuck Lever 	.buf_free		= rpc_free,
2109262965f5SChuck Lever 	.send_request		= xs_udp_send_request,
2110fe3aca29SChuck Lever 	.set_retrans_timeout	= xprt_set_retrans_timeout_rtt,
211146c0ee8bSChuck Lever 	.timer			= xs_udp_timer,
2112a58dd398SChuck Lever 	.release_request	= xprt_release_rqst_cong,
2113262965f5SChuck Lever 	.close			= xs_close,
2114262965f5SChuck Lever 	.destroy		= xs_destroy,
2115262ca07dSChuck Lever 	.print_stats		= xs_udp_print_stats,
2116262965f5SChuck Lever };
2117262965f5SChuck Lever 
2118262965f5SChuck Lever static struct rpc_xprt_ops xs_tcp_ops = {
211912a80469SChuck Lever 	.reserve_xprt		= xprt_reserve_xprt,
2120e0ab53deSTrond Myklebust 	.release_xprt		= xs_tcp_release_xprt,
212145160d62SChuck Lever 	.rpcbind		= rpcb_getport_async,
212292200412SChuck Lever 	.set_port		= xs_set_port,
2123e06799f9STrond Myklebust 	.connect		= xs_tcp_connect,
212402107148SChuck Lever 	.buf_alloc		= rpc_malloc,
212502107148SChuck Lever 	.buf_free		= rpc_free,
2126262965f5SChuck Lever 	.send_request		= xs_tcp_send_request,
2127fe3aca29SChuck Lever 	.set_retrans_timeout	= xprt_set_retrans_timeout_def,
21280d90ba1cSRicardo Labiaga #if defined(CONFIG_NFS_V4_1)
21290d90ba1cSRicardo Labiaga 	.release_request	= bc_release_request,
21300d90ba1cSRicardo Labiaga #endif /* CONFIG_NFS_V4_1 */
2131f75e6745STrond Myklebust 	.close			= xs_tcp_close,
21329903cd1cSChuck Lever 	.destroy		= xs_destroy,
2133262ca07dSChuck Lever 	.print_stats		= xs_tcp_print_stats,
2134a246b010SChuck Lever };
2135a246b010SChuck Lever 
21363c341b0bS\"Talpey, Thomas\ static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args,
2137bc25571eS\"Talpey, Thomas\ 				      unsigned int slot_table_size)
2138c8541ecdSChuck Lever {
2139c8541ecdSChuck Lever 	struct rpc_xprt *xprt;
2140ffc2e518SChuck Lever 	struct sock_xprt *new;
2141c8541ecdSChuck Lever 
214296802a09SFrank van Maarseveen 	if (args->addrlen > sizeof(xprt->addr)) {
2143c8541ecdSChuck Lever 		dprintk("RPC:       xs_setup_xprt: address too large\n");
2144c8541ecdSChuck Lever 		return ERR_PTR(-EBADF);
2145c8541ecdSChuck Lever 	}
2146c8541ecdSChuck Lever 
2147ffc2e518SChuck Lever 	new = kzalloc(sizeof(*new), GFP_KERNEL);
2148ffc2e518SChuck Lever 	if (new == NULL) {
214946121cf7SChuck Lever 		dprintk("RPC:       xs_setup_xprt: couldn't allocate "
215046121cf7SChuck Lever 				"rpc_xprt\n");
2151c8541ecdSChuck Lever 		return ERR_PTR(-ENOMEM);
2152c8541ecdSChuck Lever 	}
2153ffc2e518SChuck Lever 	xprt = &new->xprt;
2154c8541ecdSChuck Lever 
2155c8541ecdSChuck Lever 	xprt->max_reqs = slot_table_size;
2156c8541ecdSChuck Lever 	xprt->slot = kcalloc(xprt->max_reqs, sizeof(struct rpc_rqst), GFP_KERNEL);
2157c8541ecdSChuck Lever 	if (xprt->slot == NULL) {
2158c8541ecdSChuck Lever 		kfree(xprt);
215946121cf7SChuck Lever 		dprintk("RPC:       xs_setup_xprt: couldn't allocate slot "
216046121cf7SChuck Lever 				"table\n");
2161c8541ecdSChuck Lever 		return ERR_PTR(-ENOMEM);
2162c8541ecdSChuck Lever 	}
2163c8541ecdSChuck Lever 
216496802a09SFrank van Maarseveen 	memcpy(&xprt->addr, args->dstaddr, args->addrlen);
216596802a09SFrank van Maarseveen 	xprt->addrlen = args->addrlen;
2166d3bc9a1dSFrank van Maarseveen 	if (args->srcaddr)
2167d3bc9a1dSFrank van Maarseveen 		memcpy(&new->addr, args->srcaddr, args->addrlen);
2168c8541ecdSChuck Lever 
2169c8541ecdSChuck Lever 	return xprt;
2170c8541ecdSChuck Lever }
2171c8541ecdSChuck Lever 
21722881ae74STrond Myklebust static const struct rpc_timeout xs_udp_default_timeout = {
21732881ae74STrond Myklebust 	.to_initval = 5 * HZ,
21742881ae74STrond Myklebust 	.to_maxval = 30 * HZ,
21752881ae74STrond Myklebust 	.to_increment = 5 * HZ,
21762881ae74STrond Myklebust 	.to_retries = 5,
21772881ae74STrond Myklebust };
21782881ae74STrond Myklebust 
21799903cd1cSChuck Lever /**
21809903cd1cSChuck Lever  * xs_setup_udp - Set up transport to use a UDP socket
218196802a09SFrank van Maarseveen  * @args: rpc transport creation arguments
21829903cd1cSChuck Lever  *
21839903cd1cSChuck Lever  */
2184483066d6SAdrian Bunk static struct rpc_xprt *xs_setup_udp(struct xprt_create *args)
2185a246b010SChuck Lever {
21868f9d5b1aSChuck Lever 	struct sockaddr *addr = args->dstaddr;
2187c8541ecdSChuck Lever 	struct rpc_xprt *xprt;
2188c8475461SChuck Lever 	struct sock_xprt *transport;
2189a246b010SChuck Lever 
219096802a09SFrank van Maarseveen 	xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries);
2191c8541ecdSChuck Lever 	if (IS_ERR(xprt))
2192c8541ecdSChuck Lever 		return xprt;
2193c8475461SChuck Lever 	transport = container_of(xprt, struct sock_xprt, xprt);
2194a246b010SChuck Lever 
2195ec739ef0SChuck Lever 	xprt->prot = IPPROTO_UDP;
2196808012fbSChuck Lever 	xprt->tsh_size = 0;
2197a246b010SChuck Lever 	/* XXX: header size can vary due to auth type, IPv6, etc. */
2198a246b010SChuck Lever 	xprt->max_payload = (1U << 16) - (MAX_HEADER << 3);
2199a246b010SChuck Lever 
220003bf4b70SChuck Lever 	xprt->bind_timeout = XS_BIND_TO;
220103bf4b70SChuck Lever 	xprt->connect_timeout = XS_UDP_CONN_TO;
220203bf4b70SChuck Lever 	xprt->reestablish_timeout = XS_UDP_REEST_TO;
220303bf4b70SChuck Lever 	xprt->idle_timeout = XS_IDLE_DISC_TO;
2204a246b010SChuck Lever 
2205262965f5SChuck Lever 	xprt->ops = &xs_udp_ops;
2206a246b010SChuck Lever 
2207ba7392bbSTrond Myklebust 	xprt->timeout = &xs_udp_default_timeout;
2208a246b010SChuck Lever 
22098f9d5b1aSChuck Lever 	switch (addr->sa_family) {
22108f9d5b1aSChuck Lever 	case AF_INET:
22118f9d5b1aSChuck Lever 		if (((struct sockaddr_in *)addr)->sin_port != htons(0))
22128f9d5b1aSChuck Lever 			xprt_set_bound(xprt);
22138f9d5b1aSChuck Lever 
22148f9d5b1aSChuck Lever 		INIT_DELAYED_WORK(&transport->connect_worker,
22158f9d5b1aSChuck Lever 					xs_udp_connect_worker4);
2216b454ae90SChuck Lever 		xs_format_ipv4_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP);
22178f9d5b1aSChuck Lever 		break;
22188f9d5b1aSChuck Lever 	case AF_INET6:
22198f9d5b1aSChuck Lever 		if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
22208f9d5b1aSChuck Lever 			xprt_set_bound(xprt);
22218f9d5b1aSChuck Lever 
22228f9d5b1aSChuck Lever 		INIT_DELAYED_WORK(&transport->connect_worker,
22238f9d5b1aSChuck Lever 					xs_udp_connect_worker6);
2224b454ae90SChuck Lever 		xs_format_ipv6_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6);
22258f9d5b1aSChuck Lever 		break;
22268f9d5b1aSChuck Lever 	default:
22278f9d5b1aSChuck Lever 		kfree(xprt);
22288f9d5b1aSChuck Lever 		return ERR_PTR(-EAFNOSUPPORT);
22298f9d5b1aSChuck Lever 	}
22308f9d5b1aSChuck Lever 
2231edb267a6SChuck Lever 	dprintk("RPC:       set up transport to address %s\n",
22327559c7a2SChuck Lever 			xprt->address_strings[RPC_DISPLAY_ALL]);
2233edb267a6SChuck Lever 
2234bc25571eS\"Talpey, Thomas\ 	if (try_module_get(THIS_MODULE))
2235c8541ecdSChuck Lever 		return xprt;
2236bc25571eS\"Talpey, Thomas\ 
2237bc25571eS\"Talpey, Thomas\ 	kfree(xprt->slot);
2238bc25571eS\"Talpey, Thomas\ 	kfree(xprt);
2239bc25571eS\"Talpey, Thomas\ 	return ERR_PTR(-EINVAL);
2240a246b010SChuck Lever }
2241a246b010SChuck Lever 
22422881ae74STrond Myklebust static const struct rpc_timeout xs_tcp_default_timeout = {
22432881ae74STrond Myklebust 	.to_initval = 60 * HZ,
22442881ae74STrond Myklebust 	.to_maxval = 60 * HZ,
22452881ae74STrond Myklebust 	.to_retries = 2,
22462881ae74STrond Myklebust };
22472881ae74STrond Myklebust 
22489903cd1cSChuck Lever /**
22499903cd1cSChuck Lever  * xs_setup_tcp - Set up transport to use a TCP socket
225096802a09SFrank van Maarseveen  * @args: rpc transport creation arguments
22519903cd1cSChuck Lever  *
22529903cd1cSChuck Lever  */
2253483066d6SAdrian Bunk static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args)
2254a246b010SChuck Lever {
22558f9d5b1aSChuck Lever 	struct sockaddr *addr = args->dstaddr;
2256c8541ecdSChuck Lever 	struct rpc_xprt *xprt;
2257c8475461SChuck Lever 	struct sock_xprt *transport;
2258a246b010SChuck Lever 
225996802a09SFrank van Maarseveen 	xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries);
2260c8541ecdSChuck Lever 	if (IS_ERR(xprt))
2261c8541ecdSChuck Lever 		return xprt;
2262c8475461SChuck Lever 	transport = container_of(xprt, struct sock_xprt, xprt);
2263a246b010SChuck Lever 
2264ec739ef0SChuck Lever 	xprt->prot = IPPROTO_TCP;
2265808012fbSChuck Lever 	xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
2266808012fbSChuck Lever 	xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2267a246b010SChuck Lever 
226803bf4b70SChuck Lever 	xprt->bind_timeout = XS_BIND_TO;
226903bf4b70SChuck Lever 	xprt->connect_timeout = XS_TCP_CONN_TO;
227003bf4b70SChuck Lever 	xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
227103bf4b70SChuck Lever 	xprt->idle_timeout = XS_IDLE_DISC_TO;
2272a246b010SChuck Lever 
2273262965f5SChuck Lever 	xprt->ops = &xs_tcp_ops;
2274ba7392bbSTrond Myklebust 	xprt->timeout = &xs_tcp_default_timeout;
2275a246b010SChuck Lever 
22768f9d5b1aSChuck Lever 	switch (addr->sa_family) {
22778f9d5b1aSChuck Lever 	case AF_INET:
22788f9d5b1aSChuck Lever 		if (((struct sockaddr_in *)addr)->sin_port != htons(0))
22798f9d5b1aSChuck Lever 			xprt_set_bound(xprt);
22808f9d5b1aSChuck Lever 
22818f9d5b1aSChuck Lever 		INIT_DELAYED_WORK(&transport->connect_worker, xs_tcp_connect_worker4);
2282b454ae90SChuck Lever 		xs_format_ipv4_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP);
22838f9d5b1aSChuck Lever 		break;
22848f9d5b1aSChuck Lever 	case AF_INET6:
22858f9d5b1aSChuck Lever 		if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
22868f9d5b1aSChuck Lever 			xprt_set_bound(xprt);
22878f9d5b1aSChuck Lever 
22888f9d5b1aSChuck Lever 		INIT_DELAYED_WORK(&transport->connect_worker, xs_tcp_connect_worker6);
2289b454ae90SChuck Lever 		xs_format_ipv6_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6);
22908f9d5b1aSChuck Lever 		break;
22918f9d5b1aSChuck Lever 	default:
22928f9d5b1aSChuck Lever 		kfree(xprt);
22938f9d5b1aSChuck Lever 		return ERR_PTR(-EAFNOSUPPORT);
22948f9d5b1aSChuck Lever 	}
22958f9d5b1aSChuck Lever 
2296edb267a6SChuck Lever 	dprintk("RPC:       set up transport to address %s\n",
22977559c7a2SChuck Lever 			xprt->address_strings[RPC_DISPLAY_ALL]);
2298edb267a6SChuck Lever 
2299bc25571eS\"Talpey, Thomas\ 	if (try_module_get(THIS_MODULE))
2300c8541ecdSChuck Lever 		return xprt;
2301bc25571eS\"Talpey, Thomas\ 
2302bc25571eS\"Talpey, Thomas\ 	kfree(xprt->slot);
2303bc25571eS\"Talpey, Thomas\ 	kfree(xprt);
2304bc25571eS\"Talpey, Thomas\ 	return ERR_PTR(-EINVAL);
2305a246b010SChuck Lever }
2306282b32e1SChuck Lever 
2307bc25571eS\"Talpey, Thomas\ static struct xprt_class	xs_udp_transport = {
2308bc25571eS\"Talpey, Thomas\ 	.list		= LIST_HEAD_INIT(xs_udp_transport.list),
2309bc25571eS\"Talpey, Thomas\ 	.name		= "udp",
2310bc25571eS\"Talpey, Thomas\ 	.owner		= THIS_MODULE,
23114fa016ebS\"Talpey, Thomas\ 	.ident		= IPPROTO_UDP,
2312bc25571eS\"Talpey, Thomas\ 	.setup		= xs_setup_udp,
2313bc25571eS\"Talpey, Thomas\ };
2314bc25571eS\"Talpey, Thomas\ 
2315bc25571eS\"Talpey, Thomas\ static struct xprt_class	xs_tcp_transport = {
2316bc25571eS\"Talpey, Thomas\ 	.list		= LIST_HEAD_INIT(xs_tcp_transport.list),
2317bc25571eS\"Talpey, Thomas\ 	.name		= "tcp",
2318bc25571eS\"Talpey, Thomas\ 	.owner		= THIS_MODULE,
23194fa016ebS\"Talpey, Thomas\ 	.ident		= IPPROTO_TCP,
2320bc25571eS\"Talpey, Thomas\ 	.setup		= xs_setup_tcp,
2321bc25571eS\"Talpey, Thomas\ };
2322bc25571eS\"Talpey, Thomas\ 
2323282b32e1SChuck Lever /**
2324bc25571eS\"Talpey, Thomas\  * init_socket_xprt - set up xprtsock's sysctls, register with RPC client
2325282b32e1SChuck Lever  *
2326282b32e1SChuck Lever  */
2327282b32e1SChuck Lever int init_socket_xprt(void)
2328282b32e1SChuck Lever {
2329fbf76683SChuck Lever #ifdef RPC_DEBUG
23302b1bec5fSEric W. Biederman 	if (!sunrpc_table_header)
23310b4d4147SEric W. Biederman 		sunrpc_table_header = register_sysctl_table(sunrpc_table);
2332fbf76683SChuck Lever #endif
2333fbf76683SChuck Lever 
2334bc25571eS\"Talpey, Thomas\ 	xprt_register_transport(&xs_udp_transport);
2335bc25571eS\"Talpey, Thomas\ 	xprt_register_transport(&xs_tcp_transport);
2336bc25571eS\"Talpey, Thomas\ 
2337282b32e1SChuck Lever 	return 0;
2338282b32e1SChuck Lever }
2339282b32e1SChuck Lever 
2340282b32e1SChuck Lever /**
2341bc25571eS\"Talpey, Thomas\  * cleanup_socket_xprt - remove xprtsock's sysctls, unregister
2342282b32e1SChuck Lever  *
2343282b32e1SChuck Lever  */
2344282b32e1SChuck Lever void cleanup_socket_xprt(void)
2345282b32e1SChuck Lever {
2346fbf76683SChuck Lever #ifdef RPC_DEBUG
2347fbf76683SChuck Lever 	if (sunrpc_table_header) {
2348fbf76683SChuck Lever 		unregister_sysctl_table(sunrpc_table_header);
2349fbf76683SChuck Lever 		sunrpc_table_header = NULL;
2350fbf76683SChuck Lever 	}
2351fbf76683SChuck Lever #endif
2352bc25571eS\"Talpey, Thomas\ 
2353bc25571eS\"Talpey, Thomas\ 	xprt_unregister_transport(&xs_udp_transport);
2354bc25571eS\"Talpey, Thomas\ 	xprt_unregister_transport(&xs_tcp_transport);
2355282b32e1SChuck Lever }
2356