xref: /openbmc/linux/net/sunrpc/xprtsock.c (revision d0afde5fc6fb13531e2434fc4b6a65f131671f68)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2a246b010SChuck Lever /*
3a246b010SChuck Lever  * linux/net/sunrpc/xprtsock.c
4a246b010SChuck Lever  *
5a246b010SChuck Lever  * Client-side transport implementation for sockets.
6a246b010SChuck Lever  *
7113aa838SAlan Cox  * TCP callback races fixes (C) 1998 Red Hat
8113aa838SAlan Cox  * TCP send fixes (C) 1998 Red Hat
9a246b010SChuck Lever  * TCP NFS related read + write fixes
10a246b010SChuck Lever  *  (C) 1999 Dave Airlie, University of Limerick, Ireland <airlied@linux.ie>
11a246b010SChuck Lever  *
12a246b010SChuck Lever  * Rewrite of larges part of the code in order to stabilize TCP stuff.
13a246b010SChuck Lever  * Fix behaviour when socket buffer is full.
14a246b010SChuck Lever  *  (C) 1999 Trond Myklebust <trond.myklebust@fys.uio.no>
1555aa4f58SChuck Lever  *
1655aa4f58SChuck Lever  * IP socket transport implementation, (C) 2005 Chuck Lever <cel@netapp.com>
178f9d5b1aSChuck Lever  *
188f9d5b1aSChuck Lever  * IPv6 support contributed by Gilles Quillard, Bull Open Source, 2005.
198f9d5b1aSChuck Lever  *   <gilles.quillard@bull.net>
20a246b010SChuck Lever  */
21a246b010SChuck Lever 
22a246b010SChuck Lever #include <linux/types.h>
23176e21eeSChuck Lever #include <linux/string.h>
24a246b010SChuck Lever #include <linux/slab.h>
25bc25571eS\"Talpey, Thomas\ #include <linux/module.h>
26a246b010SChuck Lever #include <linux/capability.h>
27a246b010SChuck Lever #include <linux/pagemap.h>
28a246b010SChuck Lever #include <linux/errno.h>
29a246b010SChuck Lever #include <linux/socket.h>
30a246b010SChuck Lever #include <linux/in.h>
31a246b010SChuck Lever #include <linux/net.h>
32a246b010SChuck Lever #include <linux/mm.h>
33176e21eeSChuck Lever #include <linux/un.h>
34a246b010SChuck Lever #include <linux/udp.h>
35a246b010SChuck Lever #include <linux/tcp.h>
36a246b010SChuck Lever #include <linux/sunrpc/clnt.h>
375976687aSJeff Layton #include <linux/sunrpc/addr.h>
3802107148SChuck Lever #include <linux/sunrpc/sched.h>
394cfc7e60SRahul Iyer #include <linux/sunrpc/svcsock.h>
4049c36fccS\"Talpey, Thomas\ #include <linux/sunrpc/xprtsock.h>
41a246b010SChuck Lever #include <linux/file.h>
429e00abc3STrond Myklebust #ifdef CONFIG_SUNRPC_BACKCHANNEL
4344b98efdSRicardo Labiaga #include <linux/sunrpc/bc_xprt.h>
4444b98efdSRicardo Labiaga #endif
45a246b010SChuck Lever 
46a246b010SChuck Lever #include <net/sock.h>
47a246b010SChuck Lever #include <net/checksum.h>
48a246b010SChuck Lever #include <net/udp.h>
49a246b010SChuck Lever #include <net/tcp.h>
50277e4ab7STrond Myklebust #include <linux/bvec.h>
516a829eb8STrond Myklebust #include <linux/highmem.h>
52277e4ab7STrond Myklebust #include <linux/uio.h>
53a1231fdaSTrond Myklebust #include <linux/sched/mm.h>
54a246b010SChuck Lever 
5540b5ea0cSTrond Myklebust #include <trace/events/sunrpc.h>
5640b5ea0cSTrond Myklebust 
579e55eef4SChuck Lever #include "socklib.h"
584cfc7e60SRahul Iyer #include "sunrpc.h"
59176e21eeSChuck Lever 
60176e21eeSChuck Lever static void xs_close(struct rpc_xprt *xprt);
613b21f757STrond Myklebust static void xs_set_srcport(struct sock_xprt *transport, struct socket *sock);
627196dbb0STrond Myklebust static void xs_tcp_set_socket_timeouts(struct rpc_xprt *xprt,
637196dbb0STrond Myklebust 		struct socket *sock);
64176e21eeSChuck Lever 
659903cd1cSChuck Lever /*
66c556b754SChuck Lever  * xprtsock tunables
67c556b754SChuck Lever  */
6809acfea5STrond Myklebust static unsigned int xprt_udp_slot_table_entries = RPC_DEF_SLOT_TABLE;
6909acfea5STrond Myklebust static unsigned int xprt_tcp_slot_table_entries = RPC_MIN_SLOT_TABLE;
7009acfea5STrond Myklebust static unsigned int xprt_max_tcp_slot_table_entries = RPC_MAX_SLOT_TABLE;
71c556b754SChuck Lever 
7209acfea5STrond Myklebust static unsigned int xprt_min_resvport = RPC_DEF_MIN_RESVPORT;
7309acfea5STrond Myklebust static unsigned int xprt_max_resvport = RPC_DEF_MAX_RESVPORT;
74c556b754SChuck Lever 
757d1e8255STrond Myklebust #define XS_TCP_LINGER_TO	(15U * HZ)
7625fe6142STrond Myklebust static unsigned int xs_tcp_fin_timeout __read_mostly = XS_TCP_LINGER_TO;
777d1e8255STrond Myklebust 
78c556b754SChuck Lever /*
79fbf76683SChuck Lever  * We can register our own files under /proc/sys/sunrpc by
80fbf76683SChuck Lever  * calling register_sysctl_table() again.  The files in that
81fbf76683SChuck Lever  * directory become the union of all files registered there.
82fbf76683SChuck Lever  *
83fbf76683SChuck Lever  * We simply need to make sure that we don't collide with
84fbf76683SChuck Lever  * someone else's file names!
85fbf76683SChuck Lever  */
86fbf76683SChuck Lever 
87fbf76683SChuck Lever static unsigned int min_slot_table_size = RPC_MIN_SLOT_TABLE;
88fbf76683SChuck Lever static unsigned int max_slot_table_size = RPC_MAX_SLOT_TABLE;
89d9ba131dSTrond Myklebust static unsigned int max_tcp_slot_table_limit = RPC_MAX_SLOT_TABLE_LIMIT;
90fbf76683SChuck Lever static unsigned int xprt_min_resvport_limit = RPC_MIN_RESVPORT;
91fbf76683SChuck Lever static unsigned int xprt_max_resvport_limit = RPC_MAX_RESVPORT;
92fbf76683SChuck Lever 
93fbf76683SChuck Lever static struct ctl_table_header *sunrpc_table_header;
94fbf76683SChuck Lever 
95d3abc739SOlga Kornievskaia static struct xprt_class xs_local_transport;
96d3abc739SOlga Kornievskaia static struct xprt_class xs_udp_transport;
97d3abc739SOlga Kornievskaia static struct xprt_class xs_tcp_transport;
98d3abc739SOlga Kornievskaia static struct xprt_class xs_bc_tcp_transport;
99d3abc739SOlga Kornievskaia 
100fbf76683SChuck Lever /*
101fbf76683SChuck Lever  * FIXME: changing the UDP slot table size should also resize the UDP
102fbf76683SChuck Lever  *        socket buffers for existing UDP transports
103fbf76683SChuck Lever  */
104fe2c6338SJoe Perches static struct ctl_table xs_tunables_table[] = {
105fbf76683SChuck Lever 	{
106fbf76683SChuck Lever 		.procname	= "udp_slot_table_entries",
107fbf76683SChuck Lever 		.data		= &xprt_udp_slot_table_entries,
108fbf76683SChuck Lever 		.maxlen		= sizeof(unsigned int),
109fbf76683SChuck Lever 		.mode		= 0644,
1106d456111SEric W. Biederman 		.proc_handler	= proc_dointvec_minmax,
111fbf76683SChuck Lever 		.extra1		= &min_slot_table_size,
112fbf76683SChuck Lever 		.extra2		= &max_slot_table_size
113fbf76683SChuck Lever 	},
114fbf76683SChuck Lever 	{
115fbf76683SChuck Lever 		.procname	= "tcp_slot_table_entries",
116fbf76683SChuck Lever 		.data		= &xprt_tcp_slot_table_entries,
117fbf76683SChuck Lever 		.maxlen		= sizeof(unsigned int),
118fbf76683SChuck Lever 		.mode		= 0644,
1196d456111SEric W. Biederman 		.proc_handler	= proc_dointvec_minmax,
120fbf76683SChuck Lever 		.extra1		= &min_slot_table_size,
121fbf76683SChuck Lever 		.extra2		= &max_slot_table_size
122fbf76683SChuck Lever 	},
123fbf76683SChuck Lever 	{
124d9ba131dSTrond Myklebust 		.procname	= "tcp_max_slot_table_entries",
125d9ba131dSTrond Myklebust 		.data		= &xprt_max_tcp_slot_table_entries,
126d9ba131dSTrond Myklebust 		.maxlen		= sizeof(unsigned int),
127d9ba131dSTrond Myklebust 		.mode		= 0644,
128d9ba131dSTrond Myklebust 		.proc_handler	= proc_dointvec_minmax,
129d9ba131dSTrond Myklebust 		.extra1		= &min_slot_table_size,
130d9ba131dSTrond Myklebust 		.extra2		= &max_tcp_slot_table_limit
131d9ba131dSTrond Myklebust 	},
132d9ba131dSTrond Myklebust 	{
133fbf76683SChuck Lever 		.procname	= "min_resvport",
134fbf76683SChuck Lever 		.data		= &xprt_min_resvport,
135fbf76683SChuck Lever 		.maxlen		= sizeof(unsigned int),
136fbf76683SChuck Lever 		.mode		= 0644,
1376d456111SEric W. Biederman 		.proc_handler	= proc_dointvec_minmax,
138fbf76683SChuck Lever 		.extra1		= &xprt_min_resvport_limit,
139826799e6SJ. Bruce Fields 		.extra2		= &xprt_max_resvport_limit
140fbf76683SChuck Lever 	},
141fbf76683SChuck Lever 	{
142fbf76683SChuck Lever 		.procname	= "max_resvport",
143fbf76683SChuck Lever 		.data		= &xprt_max_resvport,
144fbf76683SChuck Lever 		.maxlen		= sizeof(unsigned int),
145fbf76683SChuck Lever 		.mode		= 0644,
1466d456111SEric W. Biederman 		.proc_handler	= proc_dointvec_minmax,
147826799e6SJ. Bruce Fields 		.extra1		= &xprt_min_resvport_limit,
148fbf76683SChuck Lever 		.extra2		= &xprt_max_resvport_limit
149fbf76683SChuck Lever 	},
150fbf76683SChuck Lever 	{
15125fe6142STrond Myklebust 		.procname	= "tcp_fin_timeout",
15225fe6142STrond Myklebust 		.data		= &xs_tcp_fin_timeout,
15325fe6142STrond Myklebust 		.maxlen		= sizeof(xs_tcp_fin_timeout),
15425fe6142STrond Myklebust 		.mode		= 0644,
1556d456111SEric W. Biederman 		.proc_handler	= proc_dointvec_jiffies,
15625fe6142STrond Myklebust 	},
157f8572d8fSEric W. Biederman 	{ },
158fbf76683SChuck Lever };
159fbf76683SChuck Lever 
160fe2c6338SJoe Perches static struct ctl_table sunrpc_table[] = {
161fbf76683SChuck Lever 	{
162fbf76683SChuck Lever 		.procname	= "sunrpc",
163fbf76683SChuck Lever 		.mode		= 0555,
164fbf76683SChuck Lever 		.child		= xs_tunables_table
165fbf76683SChuck Lever 	},
166f8572d8fSEric W. Biederman 	{ },
167fbf76683SChuck Lever };
168fbf76683SChuck Lever 
169fbf76683SChuck Lever /*
17003bf4b70SChuck Lever  * Wait duration for a reply from the RPC portmapper.
17103bf4b70SChuck Lever  */
17203bf4b70SChuck Lever #define XS_BIND_TO		(60U * HZ)
17303bf4b70SChuck Lever 
17403bf4b70SChuck Lever /*
17503bf4b70SChuck Lever  * Delay if a UDP socket connect error occurs.  This is most likely some
17603bf4b70SChuck Lever  * kind of resource problem on the local host.
17703bf4b70SChuck Lever  */
17803bf4b70SChuck Lever #define XS_UDP_REEST_TO		(2U * HZ)
17903bf4b70SChuck Lever 
18003bf4b70SChuck Lever /*
18103bf4b70SChuck Lever  * The reestablish timeout allows clients to delay for a bit before attempting
18203bf4b70SChuck Lever  * to reconnect to a server that just dropped our connection.
18303bf4b70SChuck Lever  *
18403bf4b70SChuck Lever  * We implement an exponential backoff when trying to reestablish a TCP
18503bf4b70SChuck Lever  * transport connection with the server.  Some servers like to drop a TCP
18603bf4b70SChuck Lever  * connection when they are overworked, so we start with a short timeout and
18703bf4b70SChuck Lever  * increase over time if the server is down or not responding.
18803bf4b70SChuck Lever  */
18903bf4b70SChuck Lever #define XS_TCP_INIT_REEST_TO	(3U * HZ)
19003bf4b70SChuck Lever 
19103bf4b70SChuck Lever /*
19203bf4b70SChuck Lever  * TCP idle timeout; client drops the transport socket if it is idle
19303bf4b70SChuck Lever  * for this long.  Note that we also timeout UDP sockets to prevent
19403bf4b70SChuck Lever  * holding port numbers when there is no RPC traffic.
19503bf4b70SChuck Lever  */
19603bf4b70SChuck Lever #define XS_IDLE_DISC_TO		(5U * 60 * HZ)
19703bf4b70SChuck Lever 
198f895b252SJeff Layton #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
199a246b010SChuck Lever # undef  RPC_DEBUG_DATA
2009903cd1cSChuck Lever # define RPCDBG_FACILITY	RPCDBG_TRANS
201a246b010SChuck Lever #endif
202a246b010SChuck Lever 
203a246b010SChuck Lever #ifdef RPC_DEBUG_DATA
2049903cd1cSChuck Lever static void xs_pktdump(char *msg, u32 *packet, unsigned int count)
205a246b010SChuck Lever {
206a246b010SChuck Lever 	u8 *buf = (u8 *) packet;
207a246b010SChuck Lever 	int j;
208a246b010SChuck Lever 
209a246b010SChuck Lever 	dprintk("RPC:       %s\n", msg);
210a246b010SChuck Lever 	for (j = 0; j < count && j < 128; j += 4) {
211a246b010SChuck Lever 		if (!(j & 31)) {
212a246b010SChuck Lever 			if (j)
213a246b010SChuck Lever 				dprintk("\n");
214a246b010SChuck Lever 			dprintk("0x%04x ", j);
215a246b010SChuck Lever 		}
216a246b010SChuck Lever 		dprintk("%02x%02x%02x%02x ",
217a246b010SChuck Lever 			buf[j], buf[j+1], buf[j+2], buf[j+3]);
218a246b010SChuck Lever 	}
219a246b010SChuck Lever 	dprintk("\n");
220a246b010SChuck Lever }
221a246b010SChuck Lever #else
2229903cd1cSChuck Lever static inline void xs_pktdump(char *msg, u32 *packet, unsigned int count)
223a246b010SChuck Lever {
224a246b010SChuck Lever 	/* NOP */
225a246b010SChuck Lever }
226a246b010SChuck Lever #endif
227a246b010SChuck Lever 
2282118071dSTrond Myklebust static inline struct rpc_xprt *xprt_from_sock(struct sock *sk)
2292118071dSTrond Myklebust {
2302118071dSTrond Myklebust 	return (struct rpc_xprt *) sk->sk_user_data;
2312118071dSTrond Myklebust }
2322118071dSTrond Myklebust 
23395392c59SChuck Lever static inline struct sockaddr *xs_addr(struct rpc_xprt *xprt)
234edb267a6SChuck Lever {
23595392c59SChuck Lever 	return (struct sockaddr *) &xprt->addr;
23695392c59SChuck Lever }
23795392c59SChuck Lever 
238176e21eeSChuck Lever static inline struct sockaddr_un *xs_addr_un(struct rpc_xprt *xprt)
239176e21eeSChuck Lever {
240176e21eeSChuck Lever 	return (struct sockaddr_un *) &xprt->addr;
241176e21eeSChuck Lever }
242176e21eeSChuck Lever 
24395392c59SChuck Lever static inline struct sockaddr_in *xs_addr_in(struct rpc_xprt *xprt)
24495392c59SChuck Lever {
24595392c59SChuck Lever 	return (struct sockaddr_in *) &xprt->addr;
24695392c59SChuck Lever }
24795392c59SChuck Lever 
24895392c59SChuck Lever static inline struct sockaddr_in6 *xs_addr_in6(struct rpc_xprt *xprt)
24995392c59SChuck Lever {
25095392c59SChuck Lever 	return (struct sockaddr_in6 *) &xprt->addr;
25195392c59SChuck Lever }
25295392c59SChuck Lever 
253c877b849SChuck Lever static void xs_format_common_peer_addresses(struct rpc_xprt *xprt)
254c877b849SChuck Lever {
255c877b849SChuck Lever 	struct sockaddr *sap = xs_addr(xprt);
2569dc3b095SChuck Lever 	struct sockaddr_in6 *sin6;
2579dc3b095SChuck Lever 	struct sockaddr_in *sin;
258176e21eeSChuck Lever 	struct sockaddr_un *sun;
259c877b849SChuck Lever 	char buf[128];
260c877b849SChuck Lever 
2619dc3b095SChuck Lever 	switch (sap->sa_family) {
262176e21eeSChuck Lever 	case AF_LOCAL:
263176e21eeSChuck Lever 		sun = xs_addr_un(xprt);
264176e21eeSChuck Lever 		strlcpy(buf, sun->sun_path, sizeof(buf));
265176e21eeSChuck Lever 		xprt->address_strings[RPC_DISPLAY_ADDR] =
266176e21eeSChuck Lever 						kstrdup(buf, GFP_KERNEL);
267176e21eeSChuck Lever 		break;
2689dc3b095SChuck Lever 	case AF_INET:
269176e21eeSChuck Lever 		(void)rpc_ntop(sap, buf, sizeof(buf));
270176e21eeSChuck Lever 		xprt->address_strings[RPC_DISPLAY_ADDR] =
271176e21eeSChuck Lever 						kstrdup(buf, GFP_KERNEL);
2729dc3b095SChuck Lever 		sin = xs_addr_in(xprt);
273fc0b5791SJoe Perches 		snprintf(buf, sizeof(buf), "%08x", ntohl(sin->sin_addr.s_addr));
2749dc3b095SChuck Lever 		break;
2759dc3b095SChuck Lever 	case AF_INET6:
276176e21eeSChuck Lever 		(void)rpc_ntop(sap, buf, sizeof(buf));
277176e21eeSChuck Lever 		xprt->address_strings[RPC_DISPLAY_ADDR] =
278176e21eeSChuck Lever 						kstrdup(buf, GFP_KERNEL);
2799dc3b095SChuck Lever 		sin6 = xs_addr_in6(xprt);
280fc0b5791SJoe Perches 		snprintf(buf, sizeof(buf), "%pi6", &sin6->sin6_addr);
2819dc3b095SChuck Lever 		break;
2829dc3b095SChuck Lever 	default:
2839dc3b095SChuck Lever 		BUG();
2849dc3b095SChuck Lever 	}
285176e21eeSChuck Lever 
2869dc3b095SChuck Lever 	xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = kstrdup(buf, GFP_KERNEL);
2879dc3b095SChuck Lever }
2889dc3b095SChuck Lever 
2899dc3b095SChuck Lever static void xs_format_common_peer_ports(struct rpc_xprt *xprt)
2909dc3b095SChuck Lever {
2919dc3b095SChuck Lever 	struct sockaddr *sap = xs_addr(xprt);
2929dc3b095SChuck Lever 	char buf[128];
2939dc3b095SChuck Lever 
29481160e66SJoe Perches 	snprintf(buf, sizeof(buf), "%u", rpc_get_port(sap));
295c877b849SChuck Lever 	xprt->address_strings[RPC_DISPLAY_PORT] = kstrdup(buf, GFP_KERNEL);
296c877b849SChuck Lever 
29781160e66SJoe Perches 	snprintf(buf, sizeof(buf), "%4hx", rpc_get_port(sap));
298c877b849SChuck Lever 	xprt->address_strings[RPC_DISPLAY_HEX_PORT] = kstrdup(buf, GFP_KERNEL);
299c877b849SChuck Lever }
300c877b849SChuck Lever 
3019dc3b095SChuck Lever static void xs_format_peer_addresses(struct rpc_xprt *xprt,
302b454ae90SChuck Lever 				     const char *protocol,
303b454ae90SChuck Lever 				     const char *netid)
304edb267a6SChuck Lever {
305b454ae90SChuck Lever 	xprt->address_strings[RPC_DISPLAY_PROTO] = protocol;
306b454ae90SChuck Lever 	xprt->address_strings[RPC_DISPLAY_NETID] = netid;
307c877b849SChuck Lever 	xs_format_common_peer_addresses(xprt);
3089dc3b095SChuck Lever 	xs_format_common_peer_ports(xprt);
309edb267a6SChuck Lever }
310edb267a6SChuck Lever 
3119dc3b095SChuck Lever static void xs_update_peer_port(struct rpc_xprt *xprt)
3124b6473fbSChuck Lever {
3139dc3b095SChuck Lever 	kfree(xprt->address_strings[RPC_DISPLAY_HEX_PORT]);
3149dc3b095SChuck Lever 	kfree(xprt->address_strings[RPC_DISPLAY_PORT]);
3154b6473fbSChuck Lever 
3169dc3b095SChuck Lever 	xs_format_common_peer_ports(xprt);
317edb267a6SChuck Lever }
318edb267a6SChuck Lever 
319edb267a6SChuck Lever static void xs_free_peer_addresses(struct rpc_xprt *xprt)
320edb267a6SChuck Lever {
32133e01dc7SChuck Lever 	unsigned int i;
32233e01dc7SChuck Lever 
32333e01dc7SChuck Lever 	for (i = 0; i < RPC_DISPLAY_MAX; i++)
32433e01dc7SChuck Lever 		switch (i) {
32533e01dc7SChuck Lever 		case RPC_DISPLAY_PROTO:
32633e01dc7SChuck Lever 		case RPC_DISPLAY_NETID:
32733e01dc7SChuck Lever 			continue;
32833e01dc7SChuck Lever 		default:
32933e01dc7SChuck Lever 			kfree(xprt->address_strings[i]);
33033e01dc7SChuck Lever 		}
331edb267a6SChuck Lever }
332edb267a6SChuck Lever 
333277e4ab7STrond Myklebust static size_t
334277e4ab7STrond Myklebust xs_alloc_sparse_pages(struct xdr_buf *buf, size_t want, gfp_t gfp)
335277e4ab7STrond Myklebust {
336277e4ab7STrond Myklebust 	size_t i,n;
337277e4ab7STrond Myklebust 
33816e5e90fSTrond Myklebust 	if (!want || !(buf->flags & XDRBUF_SPARSE_PAGES))
339277e4ab7STrond Myklebust 		return want;
340277e4ab7STrond Myklebust 	n = (buf->page_base + want + PAGE_SIZE - 1) >> PAGE_SHIFT;
341277e4ab7STrond Myklebust 	for (i = 0; i < n; i++) {
342277e4ab7STrond Myklebust 		if (buf->pages[i])
343277e4ab7STrond Myklebust 			continue;
344277e4ab7STrond Myklebust 		buf->bvec[i].bv_page = buf->pages[i] = alloc_page(gfp);
345277e4ab7STrond Myklebust 		if (!buf->pages[i]) {
34616e5e90fSTrond Myklebust 			i *= PAGE_SIZE;
34716e5e90fSTrond Myklebust 			return i > buf->page_base ? i - buf->page_base : 0;
348277e4ab7STrond Myklebust 		}
349277e4ab7STrond Myklebust 	}
350277e4ab7STrond Myklebust 	return want;
351277e4ab7STrond Myklebust }
352277e4ab7STrond Myklebust 
353277e4ab7STrond Myklebust static ssize_t
354277e4ab7STrond Myklebust xs_sock_recvmsg(struct socket *sock, struct msghdr *msg, int flags, size_t seek)
355277e4ab7STrond Myklebust {
356277e4ab7STrond Myklebust 	ssize_t ret;
357277e4ab7STrond Myklebust 	if (seek != 0)
358277e4ab7STrond Myklebust 		iov_iter_advance(&msg->msg_iter, seek);
359277e4ab7STrond Myklebust 	ret = sock_recvmsg(sock, msg, flags);
360277e4ab7STrond Myklebust 	return ret > 0 ? ret + seek : ret;
361277e4ab7STrond Myklebust }
362277e4ab7STrond Myklebust 
363277e4ab7STrond Myklebust static ssize_t
364277e4ab7STrond Myklebust xs_read_kvec(struct socket *sock, struct msghdr *msg, int flags,
365277e4ab7STrond Myklebust 		struct kvec *kvec, size_t count, size_t seek)
366277e4ab7STrond Myklebust {
3670e9b4a82SAl Viro 	iov_iter_kvec(&msg->msg_iter, READ, kvec, 1, count);
368277e4ab7STrond Myklebust 	return xs_sock_recvmsg(sock, msg, flags, seek);
369277e4ab7STrond Myklebust }
370277e4ab7STrond Myklebust 
371277e4ab7STrond Myklebust static ssize_t
372277e4ab7STrond Myklebust xs_read_bvec(struct socket *sock, struct msghdr *msg, int flags,
373277e4ab7STrond Myklebust 		struct bio_vec *bvec, unsigned long nr, size_t count,
374277e4ab7STrond Myklebust 		size_t seek)
375277e4ab7STrond Myklebust {
3760e9b4a82SAl Viro 	iov_iter_bvec(&msg->msg_iter, READ, bvec, nr, count);
377277e4ab7STrond Myklebust 	return xs_sock_recvmsg(sock, msg, flags, seek);
378277e4ab7STrond Myklebust }
379277e4ab7STrond Myklebust 
380277e4ab7STrond Myklebust static ssize_t
381277e4ab7STrond Myklebust xs_read_discard(struct socket *sock, struct msghdr *msg, int flags,
382277e4ab7STrond Myklebust 		size_t count)
383277e4ab7STrond Myklebust {
384b76a5afdSTrond Myklebust 	iov_iter_discard(&msg->msg_iter, READ, count);
385b76a5afdSTrond Myklebust 	return sock_recvmsg(sock, msg, flags);
386277e4ab7STrond Myklebust }
387277e4ab7STrond Myklebust 
3886a829eb8STrond Myklebust #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
3896a829eb8STrond Myklebust static void
3906a829eb8STrond Myklebust xs_flush_bvec(const struct bio_vec *bvec, size_t count, size_t seek)
3916a829eb8STrond Myklebust {
3926a829eb8STrond Myklebust 	struct bvec_iter bi = {
3936a829eb8STrond Myklebust 		.bi_size = count,
3946a829eb8STrond Myklebust 	};
3956a829eb8STrond Myklebust 	struct bio_vec bv;
3966a829eb8STrond Myklebust 
3976a829eb8STrond Myklebust 	bvec_iter_advance(bvec, &bi, seek & PAGE_MASK);
3986a829eb8STrond Myklebust 	for_each_bvec(bv, bvec, bi, bi)
3996a829eb8STrond Myklebust 		flush_dcache_page(bv.bv_page);
4006a829eb8STrond Myklebust }
4016a829eb8STrond Myklebust #else
4026a829eb8STrond Myklebust static inline void
4036a829eb8STrond Myklebust xs_flush_bvec(const struct bio_vec *bvec, size_t count, size_t seek)
4046a829eb8STrond Myklebust {
4056a829eb8STrond Myklebust }
4066a829eb8STrond Myklebust #endif
4076a829eb8STrond Myklebust 
408277e4ab7STrond Myklebust static ssize_t
409277e4ab7STrond Myklebust xs_read_xdr_buf(struct socket *sock, struct msghdr *msg, int flags,
410277e4ab7STrond Myklebust 		struct xdr_buf *buf, size_t count, size_t seek, size_t *read)
411277e4ab7STrond Myklebust {
412277e4ab7STrond Myklebust 	size_t want, seek_init = seek, offset = 0;
413277e4ab7STrond Myklebust 	ssize_t ret;
414277e4ab7STrond Myklebust 
415277e4ab7STrond Myklebust 	want = min_t(size_t, count, buf->head[0].iov_len);
416e92053a5STrond Myklebust 	if (seek < want) {
417277e4ab7STrond Myklebust 		ret = xs_read_kvec(sock, msg, flags, &buf->head[0], want, seek);
418277e4ab7STrond Myklebust 		if (ret <= 0)
419277e4ab7STrond Myklebust 			goto sock_err;
420277e4ab7STrond Myklebust 		offset += ret;
421277e4ab7STrond Myklebust 		if (offset == count || msg->msg_flags & (MSG_EOR|MSG_TRUNC))
422277e4ab7STrond Myklebust 			goto out;
423277e4ab7STrond Myklebust 		if (ret != want)
424c4433055STrond Myklebust 			goto out;
425277e4ab7STrond Myklebust 		seek = 0;
426277e4ab7STrond Myklebust 	} else {
427e92053a5STrond Myklebust 		seek -= want;
428e92053a5STrond Myklebust 		offset += want;
429277e4ab7STrond Myklebust 	}
43016e5e90fSTrond Myklebust 
431277e4ab7STrond Myklebust 	want = xs_alloc_sparse_pages(buf,
432277e4ab7STrond Myklebust 			min_t(size_t, count - offset, buf->page_len),
43312a3ad61STrond Myklebust 			GFP_KERNEL);
43416e5e90fSTrond Myklebust 	if (seek < want) {
435277e4ab7STrond Myklebust 		ret = xs_read_bvec(sock, msg, flags, buf->bvec,
436277e4ab7STrond Myklebust 				xdr_buf_pagecount(buf),
437277e4ab7STrond Myklebust 				want + buf->page_base,
438277e4ab7STrond Myklebust 				seek + buf->page_base);
439277e4ab7STrond Myklebust 		if (ret <= 0)
440277e4ab7STrond Myklebust 			goto sock_err;
4416a829eb8STrond Myklebust 		xs_flush_bvec(buf->bvec, ret, seek + buf->page_base);
442ac9645c8SDan Aloni 		ret -= buf->page_base;
443ac9645c8SDan Aloni 		offset += ret;
444277e4ab7STrond Myklebust 		if (offset == count || msg->msg_flags & (MSG_EOR|MSG_TRUNC))
445277e4ab7STrond Myklebust 			goto out;
446277e4ab7STrond Myklebust 		if (ret != want)
447c4433055STrond Myklebust 			goto out;
448277e4ab7STrond Myklebust 		seek = 0;
449277e4ab7STrond Myklebust 	} else {
45016e5e90fSTrond Myklebust 		seek -= want;
45116e5e90fSTrond Myklebust 		offset += want;
452277e4ab7STrond Myklebust 	}
45316e5e90fSTrond Myklebust 
454277e4ab7STrond Myklebust 	want = min_t(size_t, count - offset, buf->tail[0].iov_len);
455e92053a5STrond Myklebust 	if (seek < want) {
456277e4ab7STrond Myklebust 		ret = xs_read_kvec(sock, msg, flags, &buf->tail[0], want, seek);
457277e4ab7STrond Myklebust 		if (ret <= 0)
458277e4ab7STrond Myklebust 			goto sock_err;
459277e4ab7STrond Myklebust 		offset += ret;
460277e4ab7STrond Myklebust 		if (offset == count || msg->msg_flags & (MSG_EOR|MSG_TRUNC))
461277e4ab7STrond Myklebust 			goto out;
462277e4ab7STrond Myklebust 		if (ret != want)
463c4433055STrond Myklebust 			goto out;
4649734ad57STrond Myklebust 	} else if (offset < seek_init)
465e92053a5STrond Myklebust 		offset = seek_init;
466277e4ab7STrond Myklebust 	ret = -EMSGSIZE;
467277e4ab7STrond Myklebust out:
468277e4ab7STrond Myklebust 	*read = offset - seek_init;
469277e4ab7STrond Myklebust 	return ret;
470277e4ab7STrond Myklebust sock_err:
471277e4ab7STrond Myklebust 	offset += seek;
472277e4ab7STrond Myklebust 	goto out;
473277e4ab7STrond Myklebust }
474277e4ab7STrond Myklebust 
475277e4ab7STrond Myklebust static void
476277e4ab7STrond Myklebust xs_read_header(struct sock_xprt *transport, struct xdr_buf *buf)
477277e4ab7STrond Myklebust {
478277e4ab7STrond Myklebust 	if (!transport->recv.copied) {
479277e4ab7STrond Myklebust 		if (buf->head[0].iov_len >= transport->recv.offset)
480277e4ab7STrond Myklebust 			memcpy(buf->head[0].iov_base,
481277e4ab7STrond Myklebust 					&transport->recv.xid,
482277e4ab7STrond Myklebust 					transport->recv.offset);
483277e4ab7STrond Myklebust 		transport->recv.copied = transport->recv.offset;
484277e4ab7STrond Myklebust 	}
485277e4ab7STrond Myklebust }
486277e4ab7STrond Myklebust 
487277e4ab7STrond Myklebust static bool
488277e4ab7STrond Myklebust xs_read_stream_request_done(struct sock_xprt *transport)
489277e4ab7STrond Myklebust {
490277e4ab7STrond Myklebust 	return transport->recv.fraghdr & cpu_to_be32(RPC_LAST_STREAM_FRAGMENT);
491277e4ab7STrond Myklebust }
492277e4ab7STrond Myklebust 
493e92053a5STrond Myklebust static void
494e92053a5STrond Myklebust xs_read_stream_check_eor(struct sock_xprt *transport,
495e92053a5STrond Myklebust 		struct msghdr *msg)
496e92053a5STrond Myklebust {
497e92053a5STrond Myklebust 	if (xs_read_stream_request_done(transport))
498e92053a5STrond Myklebust 		msg->msg_flags |= MSG_EOR;
499e92053a5STrond Myklebust }
500e92053a5STrond Myklebust 
501277e4ab7STrond Myklebust static ssize_t
502277e4ab7STrond Myklebust xs_read_stream_request(struct sock_xprt *transport, struct msghdr *msg,
503277e4ab7STrond Myklebust 		int flags, struct rpc_rqst *req)
504277e4ab7STrond Myklebust {
505277e4ab7STrond Myklebust 	struct xdr_buf *buf = &req->rq_private_buf;
5063f649ab7SKees Cook 	size_t want, read;
5073f649ab7SKees Cook 	ssize_t ret;
508277e4ab7STrond Myklebust 
509277e4ab7STrond Myklebust 	xs_read_header(transport, buf);
510277e4ab7STrond Myklebust 
511277e4ab7STrond Myklebust 	want = transport->recv.len - transport->recv.offset;
512e92053a5STrond Myklebust 	if (want != 0) {
513277e4ab7STrond Myklebust 		ret = xs_read_xdr_buf(transport->sock, msg, flags, buf,
514e92053a5STrond Myklebust 				transport->recv.copied + want,
515e92053a5STrond Myklebust 				transport->recv.copied,
516277e4ab7STrond Myklebust 				&read);
517277e4ab7STrond Myklebust 		transport->recv.offset += read;
518277e4ab7STrond Myklebust 		transport->recv.copied += read;
519277e4ab7STrond Myklebust 	}
520277e4ab7STrond Myklebust 
521727fcc64STrond Myklebust 	if (transport->recv.offset == transport->recv.len)
522727fcc64STrond Myklebust 		xs_read_stream_check_eor(transport, msg);
523727fcc64STrond Myklebust 
524e92053a5STrond Myklebust 	if (want == 0)
525e92053a5STrond Myklebust 		return 0;
526e92053a5STrond Myklebust 
527277e4ab7STrond Myklebust 	switch (ret) {
528c4433055STrond Myklebust 	default:
529c4433055STrond Myklebust 		break;
53026781eabSTrond Myklebust 	case -EFAULT:
531277e4ab7STrond Myklebust 	case -EMSGSIZE:
53226781eabSTrond Myklebust 		msg->msg_flags |= MSG_TRUNC;
533c4433055STrond Myklebust 		return read;
534277e4ab7STrond Myklebust 	case 0:
535277e4ab7STrond Myklebust 		return -ESHUTDOWN;
536277e4ab7STrond Myklebust 	}
537c4433055STrond Myklebust 	return ret < 0 ? ret : read;
538277e4ab7STrond Myklebust }
539277e4ab7STrond Myklebust 
540277e4ab7STrond Myklebust static size_t
541277e4ab7STrond Myklebust xs_read_stream_headersize(bool isfrag)
542277e4ab7STrond Myklebust {
543277e4ab7STrond Myklebust 	if (isfrag)
544277e4ab7STrond Myklebust 		return sizeof(__be32);
545277e4ab7STrond Myklebust 	return 3 * sizeof(__be32);
546277e4ab7STrond Myklebust }
547277e4ab7STrond Myklebust 
548277e4ab7STrond Myklebust static ssize_t
549277e4ab7STrond Myklebust xs_read_stream_header(struct sock_xprt *transport, struct msghdr *msg,
550277e4ab7STrond Myklebust 		int flags, size_t want, size_t seek)
551277e4ab7STrond Myklebust {
552277e4ab7STrond Myklebust 	struct kvec kvec = {
553277e4ab7STrond Myklebust 		.iov_base = &transport->recv.fraghdr,
554277e4ab7STrond Myklebust 		.iov_len = want,
555277e4ab7STrond Myklebust 	};
556277e4ab7STrond Myklebust 	return xs_read_kvec(transport->sock, msg, flags, &kvec, want, seek);
557277e4ab7STrond Myklebust }
558277e4ab7STrond Myklebust 
559277e4ab7STrond Myklebust #if defined(CONFIG_SUNRPC_BACKCHANNEL)
560277e4ab7STrond Myklebust static ssize_t
561277e4ab7STrond Myklebust xs_read_stream_call(struct sock_xprt *transport, struct msghdr *msg, int flags)
562277e4ab7STrond Myklebust {
563277e4ab7STrond Myklebust 	struct rpc_xprt *xprt = &transport->xprt;
564277e4ab7STrond Myklebust 	struct rpc_rqst *req;
565277e4ab7STrond Myklebust 	ssize_t ret;
566277e4ab7STrond Myklebust 
56798b5cee3SBenjamin Coddington 	/* Is this transport associated with the backchannel? */
56898b5cee3SBenjamin Coddington 	if (!xprt->bc_serv)
56998b5cee3SBenjamin Coddington 		return -ESHUTDOWN;
57098b5cee3SBenjamin Coddington 
571277e4ab7STrond Myklebust 	/* Look up and lock the request corresponding to the given XID */
572277e4ab7STrond Myklebust 	req = xprt_lookup_bc_request(xprt, transport->recv.xid);
573277e4ab7STrond Myklebust 	if (!req) {
574277e4ab7STrond Myklebust 		printk(KERN_WARNING "Callback slot table overflowed\n");
575277e4ab7STrond Myklebust 		return -ESHUTDOWN;
576277e4ab7STrond Myklebust 	}
57745835a63STrond Myklebust 	if (transport->recv.copied && !req->rq_private_buf.len)
57845835a63STrond Myklebust 		return -ESHUTDOWN;
579277e4ab7STrond Myklebust 
580277e4ab7STrond Myklebust 	ret = xs_read_stream_request(transport, msg, flags, req);
581277e4ab7STrond Myklebust 	if (msg->msg_flags & (MSG_EOR|MSG_TRUNC))
582c4433055STrond Myklebust 		xprt_complete_bc_request(req, transport->recv.copied);
58345835a63STrond Myklebust 	else
58445835a63STrond Myklebust 		req->rq_private_buf.len = transport->recv.copied;
585277e4ab7STrond Myklebust 
586277e4ab7STrond Myklebust 	return ret;
587277e4ab7STrond Myklebust }
588277e4ab7STrond Myklebust #else /* CONFIG_SUNRPC_BACKCHANNEL */
589277e4ab7STrond Myklebust static ssize_t
590277e4ab7STrond Myklebust xs_read_stream_call(struct sock_xprt *transport, struct msghdr *msg, int flags)
591277e4ab7STrond Myklebust {
592277e4ab7STrond Myklebust 	return -ESHUTDOWN;
593277e4ab7STrond Myklebust }
594277e4ab7STrond Myklebust #endif /* CONFIG_SUNRPC_BACKCHANNEL */
595277e4ab7STrond Myklebust 
596277e4ab7STrond Myklebust static ssize_t
597277e4ab7STrond Myklebust xs_read_stream_reply(struct sock_xprt *transport, struct msghdr *msg, int flags)
598277e4ab7STrond Myklebust {
599277e4ab7STrond Myklebust 	struct rpc_xprt *xprt = &transport->xprt;
600277e4ab7STrond Myklebust 	struct rpc_rqst *req;
601277e4ab7STrond Myklebust 	ssize_t ret = 0;
602277e4ab7STrond Myklebust 
603277e4ab7STrond Myklebust 	/* Look up and lock the request corresponding to the given XID */
604277e4ab7STrond Myklebust 	spin_lock(&xprt->queue_lock);
605277e4ab7STrond Myklebust 	req = xprt_lookup_rqst(xprt, transport->recv.xid);
60645835a63STrond Myklebust 	if (!req || (transport->recv.copied && !req->rq_private_buf.len)) {
607277e4ab7STrond Myklebust 		msg->msg_flags |= MSG_TRUNC;
608277e4ab7STrond Myklebust 		goto out;
609277e4ab7STrond Myklebust 	}
610277e4ab7STrond Myklebust 	xprt_pin_rqst(req);
611277e4ab7STrond Myklebust 	spin_unlock(&xprt->queue_lock);
612277e4ab7STrond Myklebust 
613277e4ab7STrond Myklebust 	ret = xs_read_stream_request(transport, msg, flags, req);
614277e4ab7STrond Myklebust 
615277e4ab7STrond Myklebust 	spin_lock(&xprt->queue_lock);
616277e4ab7STrond Myklebust 	if (msg->msg_flags & (MSG_EOR|MSG_TRUNC))
617c4433055STrond Myklebust 		xprt_complete_rqst(req->rq_task, transport->recv.copied);
61845835a63STrond Myklebust 	else
61945835a63STrond Myklebust 		req->rq_private_buf.len = transport->recv.copied;
620277e4ab7STrond Myklebust 	xprt_unpin_rqst(req);
621277e4ab7STrond Myklebust out:
622277e4ab7STrond Myklebust 	spin_unlock(&xprt->queue_lock);
623277e4ab7STrond Myklebust 	return ret;
624277e4ab7STrond Myklebust }
625277e4ab7STrond Myklebust 
626277e4ab7STrond Myklebust static ssize_t
627277e4ab7STrond Myklebust xs_read_stream(struct sock_xprt *transport, int flags)
628277e4ab7STrond Myklebust {
629277e4ab7STrond Myklebust 	struct msghdr msg = { 0 };
630277e4ab7STrond Myklebust 	size_t want, read = 0;
631277e4ab7STrond Myklebust 	ssize_t ret = 0;
632277e4ab7STrond Myklebust 
633277e4ab7STrond Myklebust 	if (transport->recv.len == 0) {
634277e4ab7STrond Myklebust 		want = xs_read_stream_headersize(transport->recv.copied != 0);
635277e4ab7STrond Myklebust 		ret = xs_read_stream_header(transport, &msg, flags, want,
636277e4ab7STrond Myklebust 				transport->recv.offset);
637277e4ab7STrond Myklebust 		if (ret <= 0)
638277e4ab7STrond Myklebust 			goto out_err;
639277e4ab7STrond Myklebust 		transport->recv.offset = ret;
640c4433055STrond Myklebust 		if (transport->recv.offset != want)
641c4433055STrond Myklebust 			return transport->recv.offset;
642277e4ab7STrond Myklebust 		transport->recv.len = be32_to_cpu(transport->recv.fraghdr) &
643277e4ab7STrond Myklebust 			RPC_FRAGMENT_SIZE_MASK;
644277e4ab7STrond Myklebust 		transport->recv.offset -= sizeof(transport->recv.fraghdr);
645277e4ab7STrond Myklebust 		read = ret;
646277e4ab7STrond Myklebust 	}
647277e4ab7STrond Myklebust 
648277e4ab7STrond Myklebust 	switch (be32_to_cpu(transport->recv.calldir)) {
649c4433055STrond Myklebust 	default:
650c4433055STrond Myklebust 		msg.msg_flags |= MSG_TRUNC;
651c4433055STrond Myklebust 		break;
652277e4ab7STrond Myklebust 	case RPC_CALL:
653277e4ab7STrond Myklebust 		ret = xs_read_stream_call(transport, &msg, flags);
654277e4ab7STrond Myklebust 		break;
655277e4ab7STrond Myklebust 	case RPC_REPLY:
656277e4ab7STrond Myklebust 		ret = xs_read_stream_reply(transport, &msg, flags);
657277e4ab7STrond Myklebust 	}
658277e4ab7STrond Myklebust 	if (msg.msg_flags & MSG_TRUNC) {
659277e4ab7STrond Myklebust 		transport->recv.calldir = cpu_to_be32(-1);
660277e4ab7STrond Myklebust 		transport->recv.copied = -1;
661277e4ab7STrond Myklebust 	}
662277e4ab7STrond Myklebust 	if (ret < 0)
663277e4ab7STrond Myklebust 		goto out_err;
664277e4ab7STrond Myklebust 	read += ret;
665277e4ab7STrond Myklebust 	if (transport->recv.offset < transport->recv.len) {
666c4433055STrond Myklebust 		if (!(msg.msg_flags & MSG_TRUNC))
667c4433055STrond Myklebust 			return read;
668b76a5afdSTrond Myklebust 		msg.msg_flags = 0;
669277e4ab7STrond Myklebust 		ret = xs_read_discard(transport->sock, &msg, flags,
670277e4ab7STrond Myklebust 				transport->recv.len - transport->recv.offset);
671277e4ab7STrond Myklebust 		if (ret <= 0)
672277e4ab7STrond Myklebust 			goto out_err;
673277e4ab7STrond Myklebust 		transport->recv.offset += ret;
674277e4ab7STrond Myklebust 		read += ret;
675277e4ab7STrond Myklebust 		if (transport->recv.offset != transport->recv.len)
676c4433055STrond Myklebust 			return read;
677277e4ab7STrond Myklebust 	}
678277e4ab7STrond Myklebust 	if (xs_read_stream_request_done(transport)) {
679c50b8ee0STrond Myklebust 		trace_xs_stream_read_request(transport);
680277e4ab7STrond Myklebust 		transport->recv.copied = 0;
681277e4ab7STrond Myklebust 	}
682277e4ab7STrond Myklebust 	transport->recv.offset = 0;
683277e4ab7STrond Myklebust 	transport->recv.len = 0;
684277e4ab7STrond Myklebust 	return read;
685277e4ab7STrond Myklebust out_err:
68679462857STrond Myklebust 	return ret != 0 ? ret : -ESHUTDOWN;
687277e4ab7STrond Myklebust }
688277e4ab7STrond Myklebust 
6890ffe86f4STrond Myklebust static __poll_t xs_poll_socket(struct sock_xprt *transport)
6900ffe86f4STrond Myklebust {
691a73881c9STrond Myklebust 	return transport->sock->ops->poll(transport->file, transport->sock,
692a73881c9STrond Myklebust 			NULL);
6930ffe86f4STrond Myklebust }
6940ffe86f4STrond Myklebust 
6950ffe86f4STrond Myklebust static bool xs_poll_socket_readable(struct sock_xprt *transport)
6960ffe86f4STrond Myklebust {
6970ffe86f4STrond Myklebust 	__poll_t events = xs_poll_socket(transport);
6980ffe86f4STrond Myklebust 
6990ffe86f4STrond Myklebust 	return (events & (EPOLLIN | EPOLLRDNORM)) && !(events & EPOLLRDHUP);
7000ffe86f4STrond Myklebust }
7010ffe86f4STrond Myklebust 
7020ffe86f4STrond Myklebust static void xs_poll_check_readable(struct sock_xprt *transport)
7030ffe86f4STrond Myklebust {
7040ffe86f4STrond Myklebust 
7050ffe86f4STrond Myklebust 	clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state);
7060ffe86f4STrond Myklebust 	if (!xs_poll_socket_readable(transport))
7070ffe86f4STrond Myklebust 		return;
7080ffe86f4STrond Myklebust 	if (!test_and_set_bit(XPRT_SOCK_DATA_READY, &transport->sock_state))
7090ffe86f4STrond Myklebust 		queue_work(xprtiod_workqueue, &transport->recv_worker);
7100ffe86f4STrond Myklebust }
7110ffe86f4STrond Myklebust 
712c50b8ee0STrond Myklebust static void xs_stream_data_receive(struct sock_xprt *transport)
713c50b8ee0STrond Myklebust {
714c50b8ee0STrond Myklebust 	size_t read = 0;
715c50b8ee0STrond Myklebust 	ssize_t ret = 0;
716c50b8ee0STrond Myklebust 
717c50b8ee0STrond Myklebust 	mutex_lock(&transport->recv_mutex);
718c50b8ee0STrond Myklebust 	if (transport->sock == NULL)
719c50b8ee0STrond Myklebust 		goto out;
720c50b8ee0STrond Myklebust 	for (;;) {
721c50b8ee0STrond Myklebust 		ret = xs_read_stream(transport, MSG_DONTWAIT);
722c4433055STrond Myklebust 		if (ret < 0)
723c50b8ee0STrond Myklebust 			break;
724c50b8ee0STrond Myklebust 		read += ret;
725c50b8ee0STrond Myklebust 		cond_resched();
726c50b8ee0STrond Myklebust 	}
7275f52a9d4STrond Myklebust 	if (ret == -ESHUTDOWN)
7285f52a9d4STrond Myklebust 		kernel_sock_shutdown(transport->sock, SHUT_RDWR);
7295f52a9d4STrond Myklebust 	else
7300ffe86f4STrond Myklebust 		xs_poll_check_readable(transport);
731c50b8ee0STrond Myklebust out:
732c50b8ee0STrond Myklebust 	mutex_unlock(&transport->recv_mutex);
733c50b8ee0STrond Myklebust 	trace_xs_stream_read_data(&transport->xprt, ret, read);
734c50b8ee0STrond Myklebust }
735c50b8ee0STrond Myklebust 
736c50b8ee0STrond Myklebust static void xs_stream_data_receive_workfn(struct work_struct *work)
737c50b8ee0STrond Myklebust {
738c50b8ee0STrond Myklebust 	struct sock_xprt *transport =
739c50b8ee0STrond Myklebust 		container_of(work, struct sock_xprt, recv_worker);
740a1231fdaSTrond Myklebust 	unsigned int pflags = memalloc_nofs_save();
741a1231fdaSTrond Myklebust 
742c50b8ee0STrond Myklebust 	xs_stream_data_receive(transport);
743a1231fdaSTrond Myklebust 	memalloc_nofs_restore(pflags);
744c50b8ee0STrond Myklebust }
745c50b8ee0STrond Myklebust 
746550aebfeSTrond Myklebust static void
747550aebfeSTrond Myklebust xs_stream_reset_connect(struct sock_xprt *transport)
748550aebfeSTrond Myklebust {
749550aebfeSTrond Myklebust 	transport->recv.offset = 0;
750550aebfeSTrond Myklebust 	transport->recv.len = 0;
751550aebfeSTrond Myklebust 	transport->recv.copied = 0;
752550aebfeSTrond Myklebust 	transport->xmit.offset = 0;
753ae053551STrond Myklebust }
754ae053551STrond Myklebust 
755ae053551STrond Myklebust static void
756ae053551STrond Myklebust xs_stream_start_connect(struct sock_xprt *transport)
757ae053551STrond Myklebust {
758550aebfeSTrond Myklebust 	transport->xprt.stat.connect_count++;
759550aebfeSTrond Myklebust 	transport->xprt.stat.connect_start = jiffies;
760550aebfeSTrond Myklebust }
761550aebfeSTrond Myklebust 
762b4b5cc85SChuck Lever #define XS_SENDMSG_FLAGS	(MSG_DONTWAIT | MSG_NOSIGNAL)
763b4b5cc85SChuck Lever 
7649903cd1cSChuck Lever /**
765c544577dSTrond Myklebust  * xs_nospace - handle transmit was incomplete
76650f484e2STrond Myklebust  * @req: pointer to RPC request
7677496b59fSTrond Myklebust  * @transport: pointer to struct sock_xprt
7689903cd1cSChuck Lever  *
769a246b010SChuck Lever  */
7707496b59fSTrond Myklebust static int xs_nospace(struct rpc_rqst *req, struct sock_xprt *transport)
771a246b010SChuck Lever {
7727496b59fSTrond Myklebust 	struct rpc_xprt *xprt = &transport->xprt;
77306ea0bfeSTrond Myklebust 	struct sock *sk = transport->inet;
77424ca9a84STrond Myklebust 	int ret = -EAGAIN;
775a246b010SChuck Lever 
776015747d2SChuck Lever 	trace_rpc_socket_nospace(req, transport);
777a246b010SChuck Lever 
778262965f5SChuck Lever 	/* Protect against races with write_space */
779b5e92419STrond Myklebust 	spin_lock(&xprt->transport_lock);
780a246b010SChuck Lever 
781262965f5SChuck Lever 	/* Don't race with disconnect */
782b6ddf64fSTrond Myklebust 	if (xprt_connected(xprt)) {
7837496b59fSTrond Myklebust 		/* wait for more buffer space */
7842790a624STrond Myklebust 		set_bit(XPRT_SOCK_NOSPACE, &transport->sock_state);
7857496b59fSTrond Myklebust 		set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
7867496b59fSTrond Myklebust 		sk->sk_write_pending++;
7877496b59fSTrond Myklebust 		xprt_wait_for_buffer_space(xprt);
7887496b59fSTrond Myklebust 	} else
7897496b59fSTrond Myklebust 		ret = -ENOTCONN;
7907496b59fSTrond Myklebust 
7917496b59fSTrond Myklebust 	spin_unlock(&xprt->transport_lock);
7927496b59fSTrond Myklebust 	return ret;
793d48f9ce7SDavid Vrabel }
7947496b59fSTrond Myklebust 
7957496b59fSTrond Myklebust static int xs_sock_nospace(struct rpc_rqst *req)
7967496b59fSTrond Myklebust {
7977496b59fSTrond Myklebust 	struct sock_xprt *transport =
7987496b59fSTrond Myklebust 		container_of(req->rq_xprt, struct sock_xprt, xprt);
7997496b59fSTrond Myklebust 	struct sock *sk = transport->inet;
8007496b59fSTrond Myklebust 	int ret = -EAGAIN;
8017496b59fSTrond Myklebust 
8027496b59fSTrond Myklebust 	lock_sock(sk);
8037496b59fSTrond Myklebust 	if (!sock_writeable(sk))
8047496b59fSTrond Myklebust 		ret = xs_nospace(req, transport);
8057496b59fSTrond Myklebust 	release_sock(sk);
8067496b59fSTrond Myklebust 	return ret;
8077496b59fSTrond Myklebust }
8087496b59fSTrond Myklebust 
809*d0afde5fSTrond Myklebust static int xs_stream_nospace(struct rpc_rqst *req, bool vm_wait)
8107496b59fSTrond Myklebust {
8117496b59fSTrond Myklebust 	struct sock_xprt *transport =
8127496b59fSTrond Myklebust 		container_of(req->rq_xprt, struct sock_xprt, xprt);
8137496b59fSTrond Myklebust 	struct sock *sk = transport->inet;
8147496b59fSTrond Myklebust 	int ret = -EAGAIN;
8157496b59fSTrond Myklebust 
816*d0afde5fSTrond Myklebust 	if (vm_wait)
817*d0afde5fSTrond Myklebust 		return -ENOBUFS;
8187496b59fSTrond Myklebust 	lock_sock(sk);
8197496b59fSTrond Myklebust 	if (!sk_stream_memory_free(sk))
8207496b59fSTrond Myklebust 		ret = xs_nospace(req, transport);
8217496b59fSTrond Myklebust 	release_sock(sk);
8225e3771ceSTrond Myklebust 	return ret;
823a246b010SChuck Lever }
824a246b010SChuck Lever 
825277e4ab7STrond Myklebust static void
826277e4ab7STrond Myklebust xs_stream_prepare_request(struct rpc_rqst *req)
827277e4ab7STrond Myklebust {
82875369089STrond Myklebust 	xdr_free_bvec(&req->rq_rcv_buf);
82912a3ad61STrond Myklebust 	req->rq_task->tk_status = xdr_alloc_bvec(&req->rq_rcv_buf, GFP_KERNEL);
830277e4ab7STrond Myklebust }
831277e4ab7STrond Myklebust 
83261677eeeSChuck Lever /*
8334cd34e7cSTrond Myklebust  * Determine if the previous message in the stream was aborted before it
8344cd34e7cSTrond Myklebust  * could complete transmission.
8354cd34e7cSTrond Myklebust  */
8364cd34e7cSTrond Myklebust static bool
8374cd34e7cSTrond Myklebust xs_send_request_was_aborted(struct sock_xprt *transport, struct rpc_rqst *req)
8384cd34e7cSTrond Myklebust {
8394cd34e7cSTrond Myklebust 	return transport->xmit.offset != 0 && req->rq_bytes_sent == 0;
8404cd34e7cSTrond Myklebust }
8414cd34e7cSTrond Myklebust 
8424cd34e7cSTrond Myklebust /*
84306b5fc3aSTrond Myklebust  * Return the stream record marker field for a record of length < 2^31-1
84461677eeeSChuck Lever  */
84506b5fc3aSTrond Myklebust static rpc_fraghdr
84606b5fc3aSTrond Myklebust xs_stream_record_marker(struct xdr_buf *xdr)
84761677eeeSChuck Lever {
84806b5fc3aSTrond Myklebust 	if (!xdr->len)
84906b5fc3aSTrond Myklebust 		return 0;
85006b5fc3aSTrond Myklebust 	return cpu_to_be32(RPC_LAST_STREAM_FRAGMENT | (u32)xdr->len);
85161677eeeSChuck Lever }
85261677eeeSChuck Lever 
8539903cd1cSChuck Lever /**
854176e21eeSChuck Lever  * xs_local_send_request - write an RPC request to an AF_LOCAL socket
85550f484e2STrond Myklebust  * @req: pointer to RPC request
856176e21eeSChuck Lever  *
857176e21eeSChuck Lever  * Return values:
858176e21eeSChuck Lever  *        0:	The request has been sent
859176e21eeSChuck Lever  *   EAGAIN:	The socket was blocked, please call again later to
860176e21eeSChuck Lever  *		complete the request
861176e21eeSChuck Lever  * ENOTCONN:	Caller needs to invoke connect logic then call again
86212b20ce3SBhaskar Chowdhury  *    other:	Some other error occurred, the request was not sent
863176e21eeSChuck Lever  */
864adfa7144STrond Myklebust static int xs_local_send_request(struct rpc_rqst *req)
865176e21eeSChuck Lever {
866176e21eeSChuck Lever 	struct rpc_xprt *xprt = req->rq_xprt;
867176e21eeSChuck Lever 	struct sock_xprt *transport =
868176e21eeSChuck Lever 				container_of(xprt, struct sock_xprt, xprt);
869176e21eeSChuck Lever 	struct xdr_buf *xdr = &req->rq_snd_buf;
8707e3d3620STrond Myklebust 	rpc_fraghdr rm = xs_stream_record_marker(xdr);
8717e3d3620STrond Myklebust 	unsigned int msglen = rm ? req->rq_slen + sizeof(rm) : req->rq_slen;
8729e55eef4SChuck Lever 	struct msghdr msg = {
8739e55eef4SChuck Lever 		.msg_flags	= XS_SENDMSG_FLAGS,
8749e55eef4SChuck Lever 	};
875*d0afde5fSTrond Myklebust 	bool vm_wait;
8763f649ab7SKees Cook 	unsigned int sent;
877176e21eeSChuck Lever 	int status;
878176e21eeSChuck Lever 
8794cd34e7cSTrond Myklebust 	/* Close the stream if the previous transmission was incomplete */
8804cd34e7cSTrond Myklebust 	if (xs_send_request_was_aborted(transport, req)) {
8814cd34e7cSTrond Myklebust 		xs_close(xprt);
8824cd34e7cSTrond Myklebust 		return -ENOTCONN;
8834cd34e7cSTrond Myklebust 	}
8844cd34e7cSTrond Myklebust 
885176e21eeSChuck Lever 	xs_pktdump("packet data:",
886176e21eeSChuck Lever 			req->rq_svec->iov_base, req->rq_svec->iov_len);
887176e21eeSChuck Lever 
888*d0afde5fSTrond Myklebust 	vm_wait = sk_stream_is_writeable(transport->inet) ? true : false;
889*d0afde5fSTrond Myklebust 
89078215759SChuck Lever 	req->rq_xtime = ktime_get();
8919e55eef4SChuck Lever 	status = xprt_sock_sendmsg(transport->sock, &msg, xdr,
8927e3d3620STrond Myklebust 				   transport->xmit.offset, rm, &sent);
893176e21eeSChuck Lever 	dprintk("RPC:       %s(%u) = %d\n",
8946c7a64e5STrond Myklebust 			__func__, xdr->len - transport->xmit.offset, status);
895743c69e7SNeilBrown 
896f279cd00SJason Baron 	if (likely(sent > 0) || status == 0) {
8976c7a64e5STrond Myklebust 		transport->xmit.offset += sent;
8986c7a64e5STrond Myklebust 		req->rq_bytes_sent = transport->xmit.offset;
8997e3d3620STrond Myklebust 		if (likely(req->rq_bytes_sent >= msglen)) {
9006c7a64e5STrond Myklebust 			req->rq_xmit_bytes_sent += transport->xmit.offset;
9016c7a64e5STrond Myklebust 			transport->xmit.offset = 0;
902176e21eeSChuck Lever 			return 0;
903176e21eeSChuck Lever 		}
904176e21eeSChuck Lever 		status = -EAGAIN;
905*d0afde5fSTrond Myklebust 		vm_wait = false;
906176e21eeSChuck Lever 	}
907176e21eeSChuck Lever 
908176e21eeSChuck Lever 	switch (status) {
909176e21eeSChuck Lever 	case -EAGAIN:
910*d0afde5fSTrond Myklebust 		status = xs_stream_nospace(req, vm_wait);
911176e21eeSChuck Lever 		break;
912176e21eeSChuck Lever 	default:
913176e21eeSChuck Lever 		dprintk("RPC:       sendmsg returned unrecognized error %d\n",
914176e21eeSChuck Lever 			-status);
915df561f66SGustavo A. R. Silva 		fallthrough;
916176e21eeSChuck Lever 	case -EPIPE:
917176e21eeSChuck Lever 		xs_close(xprt);
918176e21eeSChuck Lever 		status = -ENOTCONN;
919176e21eeSChuck Lever 	}
920176e21eeSChuck Lever 
921176e21eeSChuck Lever 	return status;
922176e21eeSChuck Lever }
923176e21eeSChuck Lever 
924176e21eeSChuck Lever /**
925262965f5SChuck Lever  * xs_udp_send_request - write an RPC request to a UDP socket
92650f484e2STrond Myklebust  * @req: pointer to RPC request
9279903cd1cSChuck Lever  *
9289903cd1cSChuck Lever  * Return values:
9299903cd1cSChuck Lever  *        0:	The request has been sent
9309903cd1cSChuck Lever  *   EAGAIN:	The socket was blocked, please call again later to
9319903cd1cSChuck Lever  *		complete the request
932262965f5SChuck Lever  * ENOTCONN:	Caller needs to invoke connect logic then call again
93325985edcSLucas De Marchi  *    other:	Some other error occurred, the request was not sent
9349903cd1cSChuck Lever  */
935adfa7144STrond Myklebust static int xs_udp_send_request(struct rpc_rqst *req)
936a246b010SChuck Lever {
937a246b010SChuck Lever 	struct rpc_xprt *xprt = req->rq_xprt;
938ee0ac0c2SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
939262965f5SChuck Lever 	struct xdr_buf *xdr = &req->rq_snd_buf;
9409e55eef4SChuck Lever 	struct msghdr msg = {
9419e55eef4SChuck Lever 		.msg_name	= xs_addr(xprt),
9429e55eef4SChuck Lever 		.msg_namelen	= xprt->addrlen,
9439e55eef4SChuck Lever 		.msg_flags	= XS_SENDMSG_FLAGS,
9449e55eef4SChuck Lever 	};
9453f649ab7SKees Cook 	unsigned int sent;
946262965f5SChuck Lever 	int status;
947262965f5SChuck Lever 
948262965f5SChuck Lever 	xs_pktdump("packet data:",
949262965f5SChuck Lever 				req->rq_svec->iov_base,
950262965f5SChuck Lever 				req->rq_svec->iov_len);
951262965f5SChuck Lever 
95201d37c42STrond Myklebust 	if (!xprt_bound(xprt))
95301d37c42STrond Myklebust 		return -ENOTCONN;
95475891f50STrond Myklebust 
95575891f50STrond Myklebust 	if (!xprt_request_get_cong(xprt, req))
95675891f50STrond Myklebust 		return -EBADSLT;
95775891f50STrond Myklebust 
95878215759SChuck Lever 	req->rq_xtime = ktime_get();
9599e55eef4SChuck Lever 	status = xprt_sock_sendmsg(transport->sock, &msg, xdr, 0, 0, &sent);
960262965f5SChuck Lever 
961262965f5SChuck Lever 	dprintk("RPC:       xs_udp_send_request(%u) = %d\n",
9626c7a64e5STrond Myklebust 			xdr->len, status);
963262965f5SChuck Lever 
9643dedbb5cSJason Baron 	/* firewall is blocking us, don't return -EAGAIN or we end up looping */
9653dedbb5cSJason Baron 	if (status == -EPERM)
9663dedbb5cSJason Baron 		goto process_status;
9673dedbb5cSJason Baron 
968743c69e7SNeilBrown 	if (status == -EAGAIN && sock_writeable(transport->inet))
969743c69e7SNeilBrown 		status = -ENOBUFS;
970743c69e7SNeilBrown 
971f279cd00SJason Baron 	if (sent > 0 || status == 0) {
972f279cd00SJason Baron 		req->rq_xmit_bytes_sent += sent;
973f279cd00SJason Baron 		if (sent >= req->rq_slen)
974262965f5SChuck Lever 			return 0;
975262965f5SChuck Lever 		/* Still some bytes left; set up for a retry later. */
976262965f5SChuck Lever 		status = -EAGAIN;
9772199700fSTrond Myklebust 	}
978262965f5SChuck Lever 
9793dedbb5cSJason Baron process_status:
980262965f5SChuck Lever 	switch (status) {
981fba91afbSTrond Myklebust 	case -ENOTSOCK:
982fba91afbSTrond Myklebust 		status = -ENOTCONN;
983fba91afbSTrond Myklebust 		/* Should we call xs_close() here? */
984fba91afbSTrond Myklebust 		break;
985b6ddf64fSTrond Myklebust 	case -EAGAIN:
9867496b59fSTrond Myklebust 		status = xs_sock_nospace(req);
987b6ddf64fSTrond Myklebust 		break;
988262965f5SChuck Lever 	case -ENETUNREACH:
9893601c4a9STrond Myklebust 	case -ENOBUFS:
990262965f5SChuck Lever 	case -EPIPE:
991262965f5SChuck Lever 	case -ECONNREFUSED:
9923dedbb5cSJason Baron 	case -EPERM:
993262965f5SChuck Lever 		/* When the server has died, an ICMP port unreachable message
994262965f5SChuck Lever 		 * prompts ECONNREFUSED. */
99513331a55STrond Myklebust 		break;
99613331a55STrond Myklebust 	default:
99713331a55STrond Myklebust 		dprintk("RPC:       sendmsg returned unrecognized error %d\n",
99813331a55STrond Myklebust 			-status);
999262965f5SChuck Lever 	}
10005fe46e9dSBian Naimeng 
1001262965f5SChuck Lever 	return status;
1002262965f5SChuck Lever }
1003262965f5SChuck Lever 
1004e06799f9STrond Myklebust /**
1005262965f5SChuck Lever  * xs_tcp_send_request - write an RPC request to a TCP socket
100650f484e2STrond Myklebust  * @req: pointer to RPC request
1007262965f5SChuck Lever  *
1008262965f5SChuck Lever  * Return values:
1009262965f5SChuck Lever  *        0:	The request has been sent
1010262965f5SChuck Lever  *   EAGAIN:	The socket was blocked, please call again later to
1011262965f5SChuck Lever  *		complete the request
1012262965f5SChuck Lever  * ENOTCONN:	Caller needs to invoke connect logic then call again
101325985edcSLucas De Marchi  *    other:	Some other error occurred, the request was not sent
1014262965f5SChuck Lever  *
1015262965f5SChuck Lever  * XXX: In the case of soft timeouts, should we eventually give up
1016262965f5SChuck Lever  *	if sendmsg is not able to make progress?
1017262965f5SChuck Lever  */
1018adfa7144STrond Myklebust static int xs_tcp_send_request(struct rpc_rqst *req)
1019262965f5SChuck Lever {
1020262965f5SChuck Lever 	struct rpc_xprt *xprt = req->rq_xprt;
1021ee0ac0c2SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1022262965f5SChuck Lever 	struct xdr_buf *xdr = &req->rq_snd_buf;
10237e3d3620STrond Myklebust 	rpc_fraghdr rm = xs_stream_record_marker(xdr);
10247e3d3620STrond Myklebust 	unsigned int msglen = rm ? req->rq_slen + sizeof(rm) : req->rq_slen;
10259e55eef4SChuck Lever 	struct msghdr msg = {
10269e55eef4SChuck Lever 		.msg_flags	= XS_SENDMSG_FLAGS,
10279e55eef4SChuck Lever 	};
1028*d0afde5fSTrond Myklebust 	bool vm_wait;
10293f649ab7SKees Cook 	unsigned int sent;
1030b595bb15SChuck Lever 	int status;
1031a246b010SChuck Lever 
10324cd34e7cSTrond Myklebust 	/* Close the stream if the previous transmission was incomplete */
10334cd34e7cSTrond Myklebust 	if (xs_send_request_was_aborted(transport, req)) {
10344cd34e7cSTrond Myklebust 		if (transport->sock != NULL)
10354cd34e7cSTrond Myklebust 			kernel_sock_shutdown(transport->sock, SHUT_RDWR);
10364cd34e7cSTrond Myklebust 		return -ENOTCONN;
10374cd34e7cSTrond Myklebust 	}
1038d275880aSTrond Myklebust 	if (!transport->inet)
1039d275880aSTrond Myklebust 		return -ENOTCONN;
10404cd34e7cSTrond Myklebust 
1041262965f5SChuck Lever 	xs_pktdump("packet data:",
1042262965f5SChuck Lever 				req->rq_svec->iov_base,
1043262965f5SChuck Lever 				req->rq_svec->iov_len);
1044a246b010SChuck Lever 
10457196dbb0STrond Myklebust 	if (test_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state))
10467196dbb0STrond Myklebust 		xs_tcp_set_socket_timeouts(xprt, transport->sock);
10477196dbb0STrond Myklebust 
10483b21f757STrond Myklebust 	xs_set_srcport(transport, transport->sock);
10493b21f757STrond Myklebust 
1050a246b010SChuck Lever 	/* Continue transmitting the packet/record. We must be careful
1051a246b010SChuck Lever 	 * to cope with writespace callbacks arriving _after_ we have
1052262965f5SChuck Lever 	 * called sendmsg(). */
105378215759SChuck Lever 	req->rq_xtime = ktime_get();
1054d737e5d4STrond Myklebust 	tcp_sock_set_cork(transport->inet, true);
1055*d0afde5fSTrond Myklebust 
1056*d0afde5fSTrond Myklebust 	vm_wait = sk_stream_is_writeable(transport->inet) ? true : false;
1057*d0afde5fSTrond Myklebust 
1058*d0afde5fSTrond Myklebust 	do {
10599e55eef4SChuck Lever 		status = xprt_sock_sendmsg(transport->sock, &msg, xdr,
10607e3d3620STrond Myklebust 					   transport->xmit.offset, rm, &sent);
1061a246b010SChuck Lever 
1062262965f5SChuck Lever 		dprintk("RPC:       xs_tcp_send_request(%u) = %d\n",
10636c7a64e5STrond Myklebust 				xdr->len - transport->xmit.offset, status);
1064262965f5SChuck Lever 
1065a246b010SChuck Lever 		/* If we've sent the entire packet, immediately
1066a246b010SChuck Lever 		 * reset the count of bytes sent. */
10676c7a64e5STrond Myklebust 		transport->xmit.offset += sent;
10686c7a64e5STrond Myklebust 		req->rq_bytes_sent = transport->xmit.offset;
10697e3d3620STrond Myklebust 		if (likely(req->rq_bytes_sent >= msglen)) {
10706c7a64e5STrond Myklebust 			req->rq_xmit_bytes_sent += transport->xmit.offset;
10716c7a64e5STrond Myklebust 			transport->xmit.offset = 0;
1072d737e5d4STrond Myklebust 			if (atomic_long_read(&xprt->xmit_queuelen) == 1)
1073d737e5d4STrond Myklebust 				tcp_sock_set_cork(transport->inet, false);
1074a246b010SChuck Lever 			return 0;
1075a246b010SChuck Lever 		}
1076262965f5SChuck Lever 
10779ffadfbcSTrond Myklebust 		WARN_ON_ONCE(sent == 0 && status == 0);
10789ffadfbcSTrond Myklebust 
1079*d0afde5fSTrond Myklebust 		if (sent > 0)
10809ffadfbcSTrond Myklebust 			vm_wait = false;
1081*d0afde5fSTrond Myklebust 
1082*d0afde5fSTrond Myklebust 	} while (status == 0);
1083a246b010SChuck Lever 
1084262965f5SChuck Lever 	switch (status) {
1085fba91afbSTrond Myklebust 	case -ENOTSOCK:
1086fba91afbSTrond Myklebust 		status = -ENOTCONN;
1087fba91afbSTrond Myklebust 		/* Should we call xs_close() here? */
1088fba91afbSTrond Myklebust 		break;
1089262965f5SChuck Lever 	case -EAGAIN:
1090*d0afde5fSTrond Myklebust 		status = xs_stream_nospace(req, vm_wait);
1091262965f5SChuck Lever 		break;
1092262965f5SChuck Lever 	case -ECONNRESET:
1093262965f5SChuck Lever 	case -ECONNREFUSED:
1094262965f5SChuck Lever 	case -ENOTCONN:
10953913c78cSTrond Myklebust 	case -EADDRINUSE:
1096b5872f0cSTrond Myklebust 	case -ENOBUFS:
1097b9d2bb2eSTrond Myklebust 	case -EPIPE:
109813331a55STrond Myklebust 		break;
109913331a55STrond Myklebust 	default:
110013331a55STrond Myklebust 		dprintk("RPC:       sendmsg returned unrecognized error %d\n",
110113331a55STrond Myklebust 			-status);
1102a246b010SChuck Lever 	}
11035fe46e9dSBian Naimeng 
1104a246b010SChuck Lever 	return status;
1105a246b010SChuck Lever }
1106a246b010SChuck Lever 
11072a9e1cfaSTrond Myklebust static void xs_save_old_callbacks(struct sock_xprt *transport, struct sock *sk)
11082a9e1cfaSTrond Myklebust {
11092a9e1cfaSTrond Myklebust 	transport->old_data_ready = sk->sk_data_ready;
11102a9e1cfaSTrond Myklebust 	transport->old_state_change = sk->sk_state_change;
11112a9e1cfaSTrond Myklebust 	transport->old_write_space = sk->sk_write_space;
11122118071dSTrond Myklebust 	transport->old_error_report = sk->sk_error_report;
11132a9e1cfaSTrond Myklebust }
11142a9e1cfaSTrond Myklebust 
11152a9e1cfaSTrond Myklebust static void xs_restore_old_callbacks(struct sock_xprt *transport, struct sock *sk)
11162a9e1cfaSTrond Myklebust {
11172a9e1cfaSTrond Myklebust 	sk->sk_data_ready = transport->old_data_ready;
11182a9e1cfaSTrond Myklebust 	sk->sk_state_change = transport->old_state_change;
11192a9e1cfaSTrond Myklebust 	sk->sk_write_space = transport->old_write_space;
11202118071dSTrond Myklebust 	sk->sk_error_report = transport->old_error_report;
11212118071dSTrond Myklebust }
11222118071dSTrond Myklebust 
112342d42a5bSTrond Myklebust static void xs_sock_reset_state_flags(struct rpc_xprt *xprt)
112442d42a5bSTrond Myklebust {
112542d42a5bSTrond Myklebust 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
112642d42a5bSTrond Myklebust 
112742d42a5bSTrond Myklebust 	clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state);
11284f8943f8STrond Myklebust 	clear_bit(XPRT_SOCK_WAKE_ERROR, &transport->sock_state);
11294f8943f8STrond Myklebust 	clear_bit(XPRT_SOCK_WAKE_WRITE, &transport->sock_state);
11304f8943f8STrond Myklebust 	clear_bit(XPRT_SOCK_WAKE_DISCONNECT, &transport->sock_state);
11312790a624STrond Myklebust 	clear_bit(XPRT_SOCK_NOSPACE, &transport->sock_state);
11324f8943f8STrond Myklebust }
11334f8943f8STrond Myklebust 
11344f8943f8STrond Myklebust static void xs_run_error_worker(struct sock_xprt *transport, unsigned int nr)
11354f8943f8STrond Myklebust {
11364f8943f8STrond Myklebust 	set_bit(nr, &transport->sock_state);
11374f8943f8STrond Myklebust 	queue_work(xprtiod_workqueue, &transport->error_worker);
113842d42a5bSTrond Myklebust }
113942d42a5bSTrond Myklebust 
1140b70ae915STrond Myklebust static void xs_sock_reset_connection_flags(struct rpc_xprt *xprt)
1141b70ae915STrond Myklebust {
1142d896ba83STrond Myklebust 	xprt->connect_cookie++;
1143b70ae915STrond Myklebust 	smp_mb__before_atomic();
1144b70ae915STrond Myklebust 	clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
1145b70ae915STrond Myklebust 	clear_bit(XPRT_CLOSING, &xprt->state);
114642d42a5bSTrond Myklebust 	xs_sock_reset_state_flags(xprt);
1147b70ae915STrond Myklebust 	smp_mb__after_atomic();
1148b70ae915STrond Myklebust }
1149b70ae915STrond Myklebust 
11502118071dSTrond Myklebust /**
11512118071dSTrond Myklebust  * xs_error_report - callback to handle TCP socket state errors
11522118071dSTrond Myklebust  * @sk: socket
11532118071dSTrond Myklebust  *
11542118071dSTrond Myklebust  * Note: we don't call sock_error() since there may be a rpc_task
11552118071dSTrond Myklebust  * using the socket, and so we don't want to clear sk->sk_err.
11562118071dSTrond Myklebust  */
11572118071dSTrond Myklebust static void xs_error_report(struct sock *sk)
11582118071dSTrond Myklebust {
11594f8943f8STrond Myklebust 	struct sock_xprt *transport;
11602118071dSTrond Myklebust 	struct rpc_xprt *xprt;
11612118071dSTrond Myklebust 
11622118071dSTrond Myklebust 	if (!(xprt = xprt_from_sock(sk)))
1163ea9afca8STrond Myklebust 		return;
11642118071dSTrond Myklebust 
11654f8943f8STrond Myklebust 	transport = container_of(xprt, struct sock_xprt, xprt);
1166af84537dSBenjamin Coddington 	transport->xprt_err = -sk->sk_err;
1167af84537dSBenjamin Coddington 	if (transport->xprt_err == 0)
1168ea9afca8STrond Myklebust 		return;
11692118071dSTrond Myklebust 	dprintk("RPC:       xs_error_report client %p, error=%d...\n",
1170af84537dSBenjamin Coddington 			xprt, -transport->xprt_err);
1171af84537dSBenjamin Coddington 	trace_rpc_socket_error(xprt, sk->sk_socket, transport->xprt_err);
1172af84537dSBenjamin Coddington 
1173af84537dSBenjamin Coddington 	/* barrier ensures xprt_err is set before XPRT_SOCK_WAKE_ERROR */
1174af84537dSBenjamin Coddington 	smp_mb__before_atomic();
11754f8943f8STrond Myklebust 	xs_run_error_worker(transport, XPRT_SOCK_WAKE_ERROR);
11762a9e1cfaSTrond Myklebust }
11772a9e1cfaSTrond Myklebust 
1178fe315e76SChuck Lever static void xs_reset_transport(struct sock_xprt *transport)
1179a246b010SChuck Lever {
1180ee0ac0c2SChuck Lever 	struct socket *sock = transport->sock;
1181ee0ac0c2SChuck Lever 	struct sock *sk = transport->inet;
11826cc7e908STrond Myklebust 	struct rpc_xprt *xprt = &transport->xprt;
1183a73881c9STrond Myklebust 	struct file *filp = transport->file;
1184a246b010SChuck Lever 
1185fe315e76SChuck Lever 	if (sk == NULL)
1186fe315e76SChuck Lever 		return;
11879903cd1cSChuck Lever 
1188264d1df3SJeff Layton 	if (atomic_read(&transport->xprt.swapper))
1189264d1df3SJeff Layton 		sk_clear_memalloc(sk);
1190264d1df3SJeff Layton 
119109939204STrond Myklebust 	kernel_sock_shutdown(sock, SHUT_RDWR);
119209939204STrond Myklebust 
1193edc1b01cSTrond Myklebust 	mutex_lock(&transport->recv_mutex);
1194ea9afca8STrond Myklebust 	lock_sock(sk);
1195ee0ac0c2SChuck Lever 	transport->inet = NULL;
1196ee0ac0c2SChuck Lever 	transport->sock = NULL;
1197a73881c9STrond Myklebust 	transport->file = NULL;
1198a246b010SChuck Lever 
1199a246b010SChuck Lever 	sk->sk_user_data = NULL;
12002a9e1cfaSTrond Myklebust 
12012a9e1cfaSTrond Myklebust 	xs_restore_old_callbacks(transport, sk);
12020c78789eSTrond Myklebust 	xprt_clear_connected(xprt);
12036cc7e908STrond Myklebust 	xs_sock_reset_connection_flags(xprt);
1204ae053551STrond Myklebust 	/* Reset stream record info */
1205ae053551STrond Myklebust 	xs_stream_reset_connect(transport);
1206ea9afca8STrond Myklebust 	release_sock(sk);
1207edc1b01cSTrond Myklebust 	mutex_unlock(&transport->recv_mutex);
1208a246b010SChuck Lever 
12096cc7e908STrond Myklebust 	trace_rpc_socket_close(xprt, sock);
1210a73881c9STrond Myklebust 	fput(filp);
12110445f92cSTrond Myklebust 
12120445f92cSTrond Myklebust 	xprt_disconnect_done(xprt);
1213fe315e76SChuck Lever }
1214fe315e76SChuck Lever 
1215fe315e76SChuck Lever /**
1216fe315e76SChuck Lever  * xs_close - close a socket
1217fe315e76SChuck Lever  * @xprt: transport
1218fe315e76SChuck Lever  *
1219fe315e76SChuck Lever  * This is used when all requests are complete; ie, no DRC state remains
1220fe315e76SChuck Lever  * on the server we want to save.
1221f75e6745STrond Myklebust  *
1222f75e6745STrond Myklebust  * The caller _must_ be holding XPRT_LOCKED in order to avoid issues with
1223f75e6745STrond Myklebust  * xs_reset_transport() zeroing the socket from underneath a writer.
1224fe315e76SChuck Lever  */
1225fe315e76SChuck Lever static void xs_close(struct rpc_xprt *xprt)
1226fe315e76SChuck Lever {
1227fe315e76SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1228fe315e76SChuck Lever 
1229fe315e76SChuck Lever 	dprintk("RPC:       xs_close xprt %p\n", xprt);
1230fe315e76SChuck Lever 
1231fe315e76SChuck Lever 	xs_reset_transport(transport);
123261d0a8e6SNeil Brown 	xprt->reestablish_timeout = 0;
1233a246b010SChuck Lever }
1234a246b010SChuck Lever 
12354a068258SChuck Lever static void xs_inject_disconnect(struct rpc_xprt *xprt)
12364a068258SChuck Lever {
12374a068258SChuck Lever 	dprintk("RPC:       injecting transport disconnect on xprt=%p\n",
12384a068258SChuck Lever 		xprt);
12394a068258SChuck Lever 	xprt_disconnect_done(xprt);
12404a068258SChuck Lever }
12414a068258SChuck Lever 
1242315f3812SKinglong Mee static void xs_xprt_free(struct rpc_xprt *xprt)
1243315f3812SKinglong Mee {
1244315f3812SKinglong Mee 	xs_free_peer_addresses(xprt);
1245315f3812SKinglong Mee 	xprt_free(xprt);
1246315f3812SKinglong Mee }
1247315f3812SKinglong Mee 
12489903cd1cSChuck Lever /**
12499903cd1cSChuck Lever  * xs_destroy - prepare to shutdown a transport
12509903cd1cSChuck Lever  * @xprt: doomed transport
12519903cd1cSChuck Lever  *
12529903cd1cSChuck Lever  */
12539903cd1cSChuck Lever static void xs_destroy(struct rpc_xprt *xprt)
1254a246b010SChuck Lever {
125503c78827STrond Myklebust 	struct sock_xprt *transport = container_of(xprt,
125603c78827STrond Myklebust 			struct sock_xprt, xprt);
12579903cd1cSChuck Lever 	dprintk("RPC:       xs_destroy xprt %p\n", xprt);
12589903cd1cSChuck Lever 
125903c78827STrond Myklebust 	cancel_delayed_work_sync(&transport->connect_worker);
1260a1311d87STrond Myklebust 	xs_close(xprt);
1261edc1b01cSTrond Myklebust 	cancel_work_sync(&transport->recv_worker);
1262b5e92419STrond Myklebust 	cancel_work_sync(&transport->error_worker);
1263315f3812SKinglong Mee 	xs_xprt_free(xprt);
1264a1311d87STrond Myklebust 	module_put(THIS_MODULE);
1265a246b010SChuck Lever }
1266a246b010SChuck Lever 
12679903cd1cSChuck Lever /**
1268f9b2ee71STrond Myklebust  * xs_udp_data_read_skb - receive callback for UDP sockets
1269f9b2ee71STrond Myklebust  * @xprt: transport
1270f9b2ee71STrond Myklebust  * @sk: socket
1271f9b2ee71STrond Myklebust  * @skb: skbuff
12729903cd1cSChuck Lever  *
1273a246b010SChuck Lever  */
1274f9b2ee71STrond Myklebust static void xs_udp_data_read_skb(struct rpc_xprt *xprt,
1275f9b2ee71STrond Myklebust 		struct sock *sk,
1276f9b2ee71STrond Myklebust 		struct sk_buff *skb)
1277a246b010SChuck Lever {
1278a246b010SChuck Lever 	struct rpc_task *task;
1279a246b010SChuck Lever 	struct rpc_rqst *rovr;
1280f9b2ee71STrond Myklebust 	int repsize, copied;
1281d8ed029dSAlexey Dobriyan 	u32 _xid;
1282d8ed029dSAlexey Dobriyan 	__be32 *xp;
1283a246b010SChuck Lever 
12841da8c681SWillem de Bruijn 	repsize = skb->len;
1285a246b010SChuck Lever 	if (repsize < 4) {
12869903cd1cSChuck Lever 		dprintk("RPC:       impossible RPC reply size %d!\n", repsize);
1287f9b2ee71STrond Myklebust 		return;
1288a246b010SChuck Lever 	}
1289a246b010SChuck Lever 
1290a246b010SChuck Lever 	/* Copy the XID from the skb... */
12911da8c681SWillem de Bruijn 	xp = skb_header_pointer(skb, 0, sizeof(_xid), &_xid);
1292a246b010SChuck Lever 	if (xp == NULL)
1293f9b2ee71STrond Myklebust 		return;
1294a246b010SChuck Lever 
1295a246b010SChuck Lever 	/* Look up and lock the request corresponding to the given XID */
129675c84151STrond Myklebust 	spin_lock(&xprt->queue_lock);
1297a246b010SChuck Lever 	rovr = xprt_lookup_rqst(xprt, *xp);
1298a246b010SChuck Lever 	if (!rovr)
1299a246b010SChuck Lever 		goto out_unlock;
1300729749bbSTrond Myklebust 	xprt_pin_rqst(rovr);
1301ecd465eeSChuck Lever 	xprt_update_rtt(rovr->rq_task);
130275c84151STrond Myklebust 	spin_unlock(&xprt->queue_lock);
1303a246b010SChuck Lever 	task = rovr->rq_task;
1304a246b010SChuck Lever 
1305a246b010SChuck Lever 	if ((copied = rovr->rq_private_buf.buflen) > repsize)
1306a246b010SChuck Lever 		copied = repsize;
1307a246b010SChuck Lever 
1308a246b010SChuck Lever 	/* Suck it into the iovec, verify checksum if not done by hw. */
13091781f7f5SHerbert Xu 	if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) {
131075c84151STrond Myklebust 		spin_lock(&xprt->queue_lock);
13110afa6b44STrond Myklebust 		__UDPX_INC_STATS(sk, UDP_MIB_INERRORS);
1312729749bbSTrond Myklebust 		goto out_unpin;
13131781f7f5SHerbert Xu 	}
13141781f7f5SHerbert Xu 
1315a246b010SChuck Lever 
1316b5e92419STrond Myklebust 	spin_lock(&xprt->transport_lock);
13176a24dfb6STrond Myklebust 	xprt_adjust_cwnd(xprt, task, copied);
1318b5e92419STrond Myklebust 	spin_unlock(&xprt->transport_lock);
131975c84151STrond Myklebust 	spin_lock(&xprt->queue_lock);
13201570c1e4SChuck Lever 	xprt_complete_rqst(task, copied);
13210afa6b44STrond Myklebust 	__UDPX_INC_STATS(sk, UDP_MIB_INDATAGRAMS);
1322729749bbSTrond Myklebust out_unpin:
1323729749bbSTrond Myklebust 	xprt_unpin_rqst(rovr);
1324a246b010SChuck Lever  out_unlock:
132575c84151STrond Myklebust 	spin_unlock(&xprt->queue_lock);
1326f9b2ee71STrond Myklebust }
1327f9b2ee71STrond Myklebust 
1328f9b2ee71STrond Myklebust static void xs_udp_data_receive(struct sock_xprt *transport)
1329f9b2ee71STrond Myklebust {
1330f9b2ee71STrond Myklebust 	struct sk_buff *skb;
1331f9b2ee71STrond Myklebust 	struct sock *sk;
1332f9b2ee71STrond Myklebust 	int err;
1333f9b2ee71STrond Myklebust 
1334f9b2ee71STrond Myklebust 	mutex_lock(&transport->recv_mutex);
1335f9b2ee71STrond Myklebust 	sk = transport->inet;
1336f9b2ee71STrond Myklebust 	if (sk == NULL)
1337f9b2ee71STrond Myklebust 		goto out;
1338f9b2ee71STrond Myklebust 	for (;;) {
13397c13f97fSPaolo Abeni 		skb = skb_recv_udp(sk, 0, 1, &err);
13404f546149STrond Myklebust 		if (skb == NULL)
13414f546149STrond Myklebust 			break;
1342f9b2ee71STrond Myklebust 		xs_udp_data_read_skb(&transport->xprt, sk, skb);
1343850cbaddSPaolo Abeni 		consume_skb(skb);
13440af3442aSTrond Myklebust 		cond_resched();
1345f9b2ee71STrond Myklebust 	}
13460ffe86f4STrond Myklebust 	xs_poll_check_readable(transport);
1347a246b010SChuck Lever out:
1348f9b2ee71STrond Myklebust 	mutex_unlock(&transport->recv_mutex);
1349f9b2ee71STrond Myklebust }
1350f9b2ee71STrond Myklebust 
1351f9b2ee71STrond Myklebust static void xs_udp_data_receive_workfn(struct work_struct *work)
1352f9b2ee71STrond Myklebust {
1353f9b2ee71STrond Myklebust 	struct sock_xprt *transport =
1354f9b2ee71STrond Myklebust 		container_of(work, struct sock_xprt, recv_worker);
1355a1231fdaSTrond Myklebust 	unsigned int pflags = memalloc_nofs_save();
1356a1231fdaSTrond Myklebust 
1357f9b2ee71STrond Myklebust 	xs_udp_data_receive(transport);
1358a1231fdaSTrond Myklebust 	memalloc_nofs_restore(pflags);
1359f9b2ee71STrond Myklebust }
1360f9b2ee71STrond Myklebust 
1361f9b2ee71STrond Myklebust /**
1362f9b2ee71STrond Myklebust  * xs_data_ready - "data ready" callback for UDP sockets
1363f9b2ee71STrond Myklebust  * @sk: socket with data to read
1364f9b2ee71STrond Myklebust  *
1365f9b2ee71STrond Myklebust  */
1366f9b2ee71STrond Myklebust static void xs_data_ready(struct sock *sk)
1367f9b2ee71STrond Myklebust {
1368f9b2ee71STrond Myklebust 	struct rpc_xprt *xprt;
1369f9b2ee71STrond Myklebust 
1370f9b2ee71STrond Myklebust 	dprintk("RPC:       xs_data_ready...\n");
1371f9b2ee71STrond Myklebust 	xprt = xprt_from_sock(sk);
1372f9b2ee71STrond Myklebust 	if (xprt != NULL) {
1373f9b2ee71STrond Myklebust 		struct sock_xprt *transport = container_of(xprt,
1374f9b2ee71STrond Myklebust 				struct sock_xprt, xprt);
13755157b956STrond Myklebust 		transport->old_data_ready(sk);
13765157b956STrond Myklebust 		/* Any data means we had a useful conversation, so
13775157b956STrond Myklebust 		 * then we don't need to delay the next reconnect
13785157b956STrond Myklebust 		 */
13795157b956STrond Myklebust 		if (xprt->reestablish_timeout)
13805157b956STrond Myklebust 			xprt->reestablish_timeout = 0;
138142d42a5bSTrond Myklebust 		if (!test_and_set_bit(XPRT_SOCK_DATA_READY, &transport->sock_state))
138240a5f1b1STrond Myklebust 			queue_work(xprtiod_workqueue, &transport->recv_worker);
1383f9b2ee71STrond Myklebust 	}
1384a246b010SChuck Lever }
1385a246b010SChuck Lever 
1386a519fc7aSTrond Myklebust /*
1387a519fc7aSTrond Myklebust  * Helper function to force a TCP close if the server is sending
1388a519fc7aSTrond Myklebust  * junk and/or it has put us in CLOSE_WAIT
1389a519fc7aSTrond Myklebust  */
1390a519fc7aSTrond Myklebust static void xs_tcp_force_close(struct rpc_xprt *xprt)
1391a519fc7aSTrond Myklebust {
1392a519fc7aSTrond Myklebust 	xprt_force_disconnect(xprt);
1393a519fc7aSTrond Myklebust }
1394a519fc7aSTrond Myklebust 
13959e00abc3STrond Myklebust #if defined(CONFIG_SUNRPC_BACKCHANNEL)
13966b26cc8cSChuck Lever static size_t xs_tcp_bc_maxpayload(struct rpc_xprt *xprt)
13976b26cc8cSChuck Lever {
13986b26cc8cSChuck Lever 	return PAGE_SIZE;
13996b26cc8cSChuck Lever }
14009e00abc3STrond Myklebust #endif /* CONFIG_SUNRPC_BACKCHANNEL */
140144b98efdSRicardo Labiaga 
14029903cd1cSChuck Lever /**
14039903cd1cSChuck Lever  * xs_tcp_state_change - callback to handle TCP socket state changes
14049903cd1cSChuck Lever  * @sk: socket whose state has changed
14059903cd1cSChuck Lever  *
14069903cd1cSChuck Lever  */
14079903cd1cSChuck Lever static void xs_tcp_state_change(struct sock *sk)
1408a246b010SChuck Lever {
1409a246b010SChuck Lever 	struct rpc_xprt *xprt;
14100fdea1e8STrond Myklebust 	struct sock_xprt *transport;
1411a246b010SChuck Lever 
1412a246b010SChuck Lever 	if (!(xprt = xprt_from_sock(sk)))
1413ea9afca8STrond Myklebust 		return;
14149903cd1cSChuck Lever 	dprintk("RPC:       xs_tcp_state_change client %p...\n", xprt);
1415669502ffSAndy Chittenden 	dprintk("RPC:       state %x conn %d dead %d zapped %d sk_shutdown %d\n",
1416a246b010SChuck Lever 			sk->sk_state, xprt_connected(xprt),
1417a246b010SChuck Lever 			sock_flag(sk, SOCK_DEAD),
1418669502ffSAndy Chittenden 			sock_flag(sk, SOCK_ZAPPED),
1419669502ffSAndy Chittenden 			sk->sk_shutdown);
1420a246b010SChuck Lever 
14210fdea1e8STrond Myklebust 	transport = container_of(xprt, struct sock_xprt, xprt);
142240b5ea0cSTrond Myklebust 	trace_rpc_socket_state_change(xprt, sk->sk_socket);
1423a246b010SChuck Lever 	switch (sk->sk_state) {
1424a246b010SChuck Lever 	case TCP_ESTABLISHED:
1425a246b010SChuck Lever 		if (!xprt_test_and_set_connected(xprt)) {
14268b71798cSTrond Myklebust 			xprt->connect_cookie++;
14270fdea1e8STrond Myklebust 			clear_bit(XPRT_SOCK_CONNECTING, &transport->sock_state);
14280fdea1e8STrond Myklebust 			xprt_clear_connecting(xprt);
142951971139SChuck Lever 
14303968a8a5SChuck Lever 			xprt->stat.connect_count++;
14313968a8a5SChuck Lever 			xprt->stat.connect_time += (long)jiffies -
14323968a8a5SChuck Lever 						   xprt->stat.connect_start;
14334f8943f8STrond Myklebust 			xs_run_error_worker(transport, XPRT_SOCK_WAKE_PENDING);
1434a246b010SChuck Lever 		}
1435a246b010SChuck Lever 		break;
14363b948ae5STrond Myklebust 	case TCP_FIN_WAIT1:
14373b948ae5STrond Myklebust 		/* The client initiated a shutdown of the socket */
14387c1d71cfSTrond Myklebust 		xprt->connect_cookie++;
1439663b8858STrond Myklebust 		xprt->reestablish_timeout = 0;
14403b948ae5STrond Myklebust 		set_bit(XPRT_CLOSING, &xprt->state);
14414e857c58SPeter Zijlstra 		smp_mb__before_atomic();
14423b948ae5STrond Myklebust 		clear_bit(XPRT_CONNECTED, &xprt->state);
1443ef803670STrond Myklebust 		clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
14444e857c58SPeter Zijlstra 		smp_mb__after_atomic();
1445a246b010SChuck Lever 		break;
1446632e3bdcSTrond Myklebust 	case TCP_CLOSE_WAIT:
14473b948ae5STrond Myklebust 		/* The server initiated a shutdown of the socket */
14487c1d71cfSTrond Myklebust 		xprt->connect_cookie++;
1449d0bea455STrond Myklebust 		clear_bit(XPRT_CONNECTED, &xprt->state);
14504f8943f8STrond Myklebust 		xs_run_error_worker(transport, XPRT_SOCK_WAKE_DISCONNECT);
1451df561f66SGustavo A. R. Silva 		fallthrough;
1452663b8858STrond Myklebust 	case TCP_CLOSING:
1453663b8858STrond Myklebust 		/*
1454663b8858STrond Myklebust 		 * If the server closed down the connection, make sure that
1455663b8858STrond Myklebust 		 * we back off before reconnecting
1456663b8858STrond Myklebust 		 */
1457663b8858STrond Myklebust 		if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
1458663b8858STrond Myklebust 			xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
14593b948ae5STrond Myklebust 		break;
14603b948ae5STrond Myklebust 	case TCP_LAST_ACK:
1461670f9457STrond Myklebust 		set_bit(XPRT_CLOSING, &xprt->state);
14624e857c58SPeter Zijlstra 		smp_mb__before_atomic();
14633b948ae5STrond Myklebust 		clear_bit(XPRT_CONNECTED, &xprt->state);
14644e857c58SPeter Zijlstra 		smp_mb__after_atomic();
14653b948ae5STrond Myklebust 		break;
14663b948ae5STrond Myklebust 	case TCP_CLOSE:
14670fdea1e8STrond Myklebust 		if (test_and_clear_bit(XPRT_SOCK_CONNECTING,
14680fdea1e8STrond Myklebust 					&transport->sock_state))
14690fdea1e8STrond Myklebust 			xprt_clear_connecting(xprt);
14709b30889cSTrond Myklebust 		clear_bit(XPRT_CLOSING, &xprt->state);
14719b30889cSTrond Myklebust 		/* Trigger the socket release */
14724f8943f8STrond Myklebust 		xs_run_error_worker(transport, XPRT_SOCK_WAKE_DISCONNECT);
1473a246b010SChuck Lever 	}
1474a246b010SChuck Lever }
1475a246b010SChuck Lever 
14761f0fa154SIlpo Järvinen static void xs_write_space(struct sock *sk)
14771f0fa154SIlpo Järvinen {
14784f8943f8STrond Myklebust 	struct sock_xprt *transport;
14791f0fa154SIlpo Järvinen 	struct rpc_xprt *xprt;
14801f0fa154SIlpo Järvinen 
148113331a55STrond Myklebust 	if (!sk->sk_socket)
14821f0fa154SIlpo Järvinen 		return;
148313331a55STrond Myklebust 	clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
14841f0fa154SIlpo Järvinen 
14851f0fa154SIlpo Järvinen 	if (unlikely(!(xprt = xprt_from_sock(sk))))
14861f0fa154SIlpo Järvinen 		return;
14874f8943f8STrond Myklebust 	transport = container_of(xprt, struct sock_xprt, xprt);
14882790a624STrond Myklebust 	if (!test_and_clear_bit(XPRT_SOCK_NOSPACE, &transport->sock_state))
14892790a624STrond Myklebust 		return;
14904f8943f8STrond Myklebust 	xs_run_error_worker(transport, XPRT_SOCK_WAKE_WRITE);
1491c544577dSTrond Myklebust 	sk->sk_write_pending--;
14921f0fa154SIlpo Järvinen }
14931f0fa154SIlpo Järvinen 
14942a9e1cfaSTrond Myklebust /**
1495c7b2cae8SChuck Lever  * xs_udp_write_space - callback invoked when socket buffer space
1496c7b2cae8SChuck Lever  *                             becomes available
14979903cd1cSChuck Lever  * @sk: socket whose state has changed
14989903cd1cSChuck Lever  *
1499a246b010SChuck Lever  * Called when more output buffer space is available for this socket.
1500a246b010SChuck Lever  * We try not to wake our writers until they can make "significant"
1501c7b2cae8SChuck Lever  * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1502a246b010SChuck Lever  * with a bunch of small requests.
1503a246b010SChuck Lever  */
1504c7b2cae8SChuck Lever static void xs_udp_write_space(struct sock *sk)
1505a246b010SChuck Lever {
1506c7b2cae8SChuck Lever 	/* from net/core/sock.c:sock_def_write_space */
15071f0fa154SIlpo Järvinen 	if (sock_writeable(sk))
15081f0fa154SIlpo Järvinen 		xs_write_space(sk);
1509c7b2cae8SChuck Lever }
1510c7b2cae8SChuck Lever 
1511c7b2cae8SChuck Lever /**
1512c7b2cae8SChuck Lever  * xs_tcp_write_space - callback invoked when socket buffer space
1513c7b2cae8SChuck Lever  *                             becomes available
1514c7b2cae8SChuck Lever  * @sk: socket whose state has changed
1515c7b2cae8SChuck Lever  *
1516c7b2cae8SChuck Lever  * Called when more output buffer space is available for this socket.
1517c7b2cae8SChuck Lever  * We try not to wake our writers until they can make "significant"
1518c7b2cae8SChuck Lever  * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1519c7b2cae8SChuck Lever  * with a bunch of small requests.
1520c7b2cae8SChuck Lever  */
1521c7b2cae8SChuck Lever static void xs_tcp_write_space(struct sock *sk)
1522c7b2cae8SChuck Lever {
1523c7b2cae8SChuck Lever 	/* from net/core/stream.c:sk_stream_write_space */
152464dc6130SEric Dumazet 	if (sk_stream_is_writeable(sk))
15251f0fa154SIlpo Järvinen 		xs_write_space(sk);
1526a246b010SChuck Lever }
1527a246b010SChuck Lever 
1528470056c2SChuck Lever static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt)
1529a246b010SChuck Lever {
1530ee0ac0c2SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1531ee0ac0c2SChuck Lever 	struct sock *sk = transport->inet;
1532a246b010SChuck Lever 
15337c6e066eSChuck Lever 	if (transport->rcvsize) {
1534a246b010SChuck Lever 		sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
15357c6e066eSChuck Lever 		sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2;
1536a246b010SChuck Lever 	}
15377c6e066eSChuck Lever 	if (transport->sndsize) {
1538a246b010SChuck Lever 		sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
15397c6e066eSChuck Lever 		sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2;
1540a246b010SChuck Lever 		sk->sk_write_space(sk);
1541a246b010SChuck Lever 	}
1542a246b010SChuck Lever }
1543a246b010SChuck Lever 
154443118c29SChuck Lever /**
1545470056c2SChuck Lever  * xs_udp_set_buffer_size - set send and receive limits
154643118c29SChuck Lever  * @xprt: generic transport
1547470056c2SChuck Lever  * @sndsize: requested size of send buffer, in bytes
1548470056c2SChuck Lever  * @rcvsize: requested size of receive buffer, in bytes
154943118c29SChuck Lever  *
1550470056c2SChuck Lever  * Set socket send and receive buffer size limits.
155143118c29SChuck Lever  */
1552470056c2SChuck Lever static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize)
155343118c29SChuck Lever {
15547c6e066eSChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
15557c6e066eSChuck Lever 
15567c6e066eSChuck Lever 	transport->sndsize = 0;
1557470056c2SChuck Lever 	if (sndsize)
15587c6e066eSChuck Lever 		transport->sndsize = sndsize + 1024;
15597c6e066eSChuck Lever 	transport->rcvsize = 0;
1560470056c2SChuck Lever 	if (rcvsize)
15617c6e066eSChuck Lever 		transport->rcvsize = rcvsize + 1024;
1562470056c2SChuck Lever 
1563470056c2SChuck Lever 	xs_udp_do_set_buffer_size(xprt);
156443118c29SChuck Lever }
156543118c29SChuck Lever 
156646c0ee8bSChuck Lever /**
156746c0ee8bSChuck Lever  * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
1568acf0a39fSChuck Lever  * @xprt: controlling transport
156946c0ee8bSChuck Lever  * @task: task that timed out
157046c0ee8bSChuck Lever  *
157146c0ee8bSChuck Lever  * Adjust the congestion window after a retransmit timeout has occurred.
157246c0ee8bSChuck Lever  */
15736a24dfb6STrond Myklebust static void xs_udp_timer(struct rpc_xprt *xprt, struct rpc_task *task)
157446c0ee8bSChuck Lever {
1575b5e92419STrond Myklebust 	spin_lock(&xprt->transport_lock);
15766a24dfb6STrond Myklebust 	xprt_adjust_cwnd(xprt, task, -ETIMEDOUT);
1577b5e92419STrond Myklebust 	spin_unlock(&xprt->transport_lock);
157846c0ee8bSChuck Lever }
157946c0ee8bSChuck Lever 
1580826799e6SJ. Bruce Fields static int xs_get_random_port(void)
1581b85d8806SChuck Lever {
1582826799e6SJ. Bruce Fields 	unsigned short min = xprt_min_resvport, max = xprt_max_resvport;
1583826799e6SJ. Bruce Fields 	unsigned short range;
1584826799e6SJ. Bruce Fields 	unsigned short rand;
1585826799e6SJ. Bruce Fields 
1586826799e6SJ. Bruce Fields 	if (max < min)
1587826799e6SJ. Bruce Fields 		return -EADDRINUSE;
1588826799e6SJ. Bruce Fields 	range = max - min + 1;
1589826799e6SJ. Bruce Fields 	rand = (unsigned short) prandom_u32() % range;
1590826799e6SJ. Bruce Fields 	return rand + min;
1591b85d8806SChuck Lever }
1592b85d8806SChuck Lever 
15934dda9c8aSTrond Myklebust static unsigned short xs_sock_getport(struct socket *sock)
15944dda9c8aSTrond Myklebust {
15954dda9c8aSTrond Myklebust 	struct sockaddr_storage buf;
15964dda9c8aSTrond Myklebust 	unsigned short port = 0;
15974dda9c8aSTrond Myklebust 
15989b2c45d4SDenys Vlasenko 	if (kernel_getsockname(sock, (struct sockaddr *)&buf) < 0)
15994dda9c8aSTrond Myklebust 		goto out;
16004dda9c8aSTrond Myklebust 	switch (buf.ss_family) {
16014dda9c8aSTrond Myklebust 	case AF_INET6:
16024dda9c8aSTrond Myklebust 		port = ntohs(((struct sockaddr_in6 *)&buf)->sin6_port);
16034dda9c8aSTrond Myklebust 		break;
16044dda9c8aSTrond Myklebust 	case AF_INET:
16054dda9c8aSTrond Myklebust 		port = ntohs(((struct sockaddr_in *)&buf)->sin_port);
16064dda9c8aSTrond Myklebust 	}
16074dda9c8aSTrond Myklebust out:
16084dda9c8aSTrond Myklebust 	return port;
16094dda9c8aSTrond Myklebust }
16104dda9c8aSTrond Myklebust 
16114dda9c8aSTrond Myklebust /**
161292200412SChuck Lever  * xs_set_port - reset the port number in the remote endpoint address
161392200412SChuck Lever  * @xprt: generic transport
161492200412SChuck Lever  * @port: new port number
161592200412SChuck Lever  *
161692200412SChuck Lever  */
161792200412SChuck Lever static void xs_set_port(struct rpc_xprt *xprt, unsigned short port)
161892200412SChuck Lever {
161992200412SChuck Lever 	dprintk("RPC:       setting port for xprt %p to %u\n", xprt, port);
1620c4efcb1dSChuck Lever 
16219dc3b095SChuck Lever 	rpc_set_port(xs_addr(xprt), port);
16229dc3b095SChuck Lever 	xs_update_peer_port(xprt);
162392200412SChuck Lever }
162492200412SChuck Lever 
16254dda9c8aSTrond Myklebust static void xs_set_srcport(struct sock_xprt *transport, struct socket *sock)
16264dda9c8aSTrond Myklebust {
1627e6237b6fSTrond Myklebust 	if (transport->srcport == 0 && transport->xprt.reuseport)
16284dda9c8aSTrond Myklebust 		transport->srcport = xs_sock_getport(sock);
16294dda9c8aSTrond Myklebust }
16304dda9c8aSTrond Myklebust 
1631826799e6SJ. Bruce Fields static int xs_get_srcport(struct sock_xprt *transport)
163267a391d7STrond Myklebust {
1633826799e6SJ. Bruce Fields 	int port = transport->srcport;
163467a391d7STrond Myklebust 
163567a391d7STrond Myklebust 	if (port == 0 && transport->xprt.resvport)
163667a391d7STrond Myklebust 		port = xs_get_random_port();
163767a391d7STrond Myklebust 	return port;
163867a391d7STrond Myklebust }
163967a391d7STrond Myklebust 
1640a8482488SOlga Kornievskaia unsigned short get_srcport(struct rpc_xprt *xprt)
1641a8482488SOlga Kornievskaia {
1642a8482488SOlga Kornievskaia 	struct sock_xprt *sock = container_of(xprt, struct sock_xprt, xprt);
1643b49ea673SNeilBrown 	unsigned short ret = 0;
1644b49ea673SNeilBrown 	mutex_lock(&sock->recv_mutex);
1645b49ea673SNeilBrown 	if (sock->sock)
1646b49ea673SNeilBrown 		ret = xs_sock_getport(sock->sock);
1647b49ea673SNeilBrown 	mutex_unlock(&sock->recv_mutex);
1648b49ea673SNeilBrown 	return ret;
1649a8482488SOlga Kornievskaia }
1650a8482488SOlga Kornievskaia EXPORT_SYMBOL(get_srcport);
1651a8482488SOlga Kornievskaia 
1652baaf4e48SPavel Emelyanov static unsigned short xs_next_srcport(struct sock_xprt *transport, unsigned short port)
165367a391d7STrond Myklebust {
1654fbfffbd5SChuck Lever 	if (transport->srcport != 0)
1655fbfffbd5SChuck Lever 		transport->srcport = 0;
165667a391d7STrond Myklebust 	if (!transport->xprt.resvport)
165767a391d7STrond Myklebust 		return 0;
165867a391d7STrond Myklebust 	if (port <= xprt_min_resvport || port > xprt_max_resvport)
165967a391d7STrond Myklebust 		return xprt_max_resvport;
166067a391d7STrond Myklebust 	return --port;
166167a391d7STrond Myklebust }
1662beb59b68SPavel Emelyanov static int xs_bind(struct sock_xprt *transport, struct socket *sock)
1663a246b010SChuck Lever {
1664beb59b68SPavel Emelyanov 	struct sockaddr_storage myaddr;
166567a391d7STrond Myklebust 	int err, nloop = 0;
1666826799e6SJ. Bruce Fields 	int port = xs_get_srcport(transport);
166767a391d7STrond Myklebust 	unsigned short last;
1668a246b010SChuck Lever 
16690f7a622cSChris Perl 	/*
16700f7a622cSChris Perl 	 * If we are asking for any ephemeral port (i.e. port == 0 &&
16710f7a622cSChris Perl 	 * transport->xprt.resvport == 0), don't bind.  Let the local
16720f7a622cSChris Perl 	 * port selection happen implicitly when the socket is used
16730f7a622cSChris Perl 	 * (for example at connect time).
16740f7a622cSChris Perl 	 *
16750f7a622cSChris Perl 	 * This ensures that we can continue to establish TCP
16760f7a622cSChris Perl 	 * connections even when all local ephemeral ports are already
16770f7a622cSChris Perl 	 * a part of some TCP connection.  This makes no difference
167812b20ce3SBhaskar Chowdhury 	 * for UDP sockets, but also doesn't harm them.
16790f7a622cSChris Perl 	 *
16800f7a622cSChris Perl 	 * If we're asking for any reserved port (i.e. port == 0 &&
16810f7a622cSChris Perl 	 * transport->xprt.resvport == 1) xs_get_srcport above will
16820f7a622cSChris Perl 	 * ensure that port is non-zero and we will bind as needed.
16830f7a622cSChris Perl 	 */
1684826799e6SJ. Bruce Fields 	if (port <= 0)
1685826799e6SJ. Bruce Fields 		return port;
16860f7a622cSChris Perl 
1687beb59b68SPavel Emelyanov 	memcpy(&myaddr, &transport->srcaddr, transport->xprt.addrlen);
1688a246b010SChuck Lever 	do {
1689beb59b68SPavel Emelyanov 		rpc_set_port((struct sockaddr *)&myaddr, port);
1690e6242e92SSridhar Samudrala 		err = kernel_bind(sock, (struct sockaddr *)&myaddr,
1691beb59b68SPavel Emelyanov 				transport->xprt.addrlen);
1692a246b010SChuck Lever 		if (err == 0) {
1693bc1c56e9SNeilBrown 			if (transport->xprt.reuseport)
1694fbfffbd5SChuck Lever 				transport->srcport = port;
1695d3bc9a1dSFrank van Maarseveen 			break;
1696a246b010SChuck Lever 		}
169767a391d7STrond Myklebust 		last = port;
1698baaf4e48SPavel Emelyanov 		port = xs_next_srcport(transport, port);
169967a391d7STrond Myklebust 		if (port > last)
170067a391d7STrond Myklebust 			nloop++;
170167a391d7STrond Myklebust 	} while (err == -EADDRINUSE && nloop != 2);
1702beb59b68SPavel Emelyanov 
17034232e863SChuck Lever 	if (myaddr.ss_family == AF_INET)
1704beb59b68SPavel Emelyanov 		dprintk("RPC:       %s %pI4:%u: %s (%d)\n", __func__,
1705beb59b68SPavel Emelyanov 				&((struct sockaddr_in *)&myaddr)->sin_addr,
1706beb59b68SPavel Emelyanov 				port, err ? "failed" : "ok", err);
1707beb59b68SPavel Emelyanov 	else
1708beb59b68SPavel Emelyanov 		dprintk("RPC:       %s %pI6:%u: %s (%d)\n", __func__,
1709beb59b68SPavel Emelyanov 				&((struct sockaddr_in6 *)&myaddr)->sin6_addr,
17107dc753f0SChuck Lever 				port, err ? "failed" : "ok", err);
1711a246b010SChuck Lever 	return err;
1712a246b010SChuck Lever }
1713a246b010SChuck Lever 
1714176e21eeSChuck Lever /*
1715176e21eeSChuck Lever  * We don't support autobind on AF_LOCAL sockets
1716176e21eeSChuck Lever  */
1717176e21eeSChuck Lever static void xs_local_rpcbind(struct rpc_task *task)
1718176e21eeSChuck Lever {
1719fb43d172STrond Myklebust 	xprt_set_bound(task->tk_xprt);
1720176e21eeSChuck Lever }
1721176e21eeSChuck Lever 
1722176e21eeSChuck Lever static void xs_local_set_port(struct rpc_xprt *xprt, unsigned short port)
1723176e21eeSChuck Lever {
1724176e21eeSChuck Lever }
1725a246b010SChuck Lever 
1726ed07536eSPeter Zijlstra #ifdef CONFIG_DEBUG_LOCK_ALLOC
1727064a9177SNeilBrown static struct lock_class_key xs_key[3];
1728064a9177SNeilBrown static struct lock_class_key xs_slock_key[3];
1729ed07536eSPeter Zijlstra 
1730176e21eeSChuck Lever static inline void xs_reclassify_socketu(struct socket *sock)
1731176e21eeSChuck Lever {
1732176e21eeSChuck Lever 	struct sock *sk = sock->sk;
1733176e21eeSChuck Lever 
1734176e21eeSChuck Lever 	sock_lock_init_class_and_name(sk, "slock-AF_LOCAL-RPC",
1735064a9177SNeilBrown 		&xs_slock_key[0], "sk_lock-AF_LOCAL-RPC", &xs_key[0]);
1736176e21eeSChuck Lever }
1737176e21eeSChuck Lever 
17388945ee5eSChuck Lever static inline void xs_reclassify_socket4(struct socket *sock)
1739ed07536eSPeter Zijlstra {
1740ed07536eSPeter Zijlstra 	struct sock *sk = sock->sk;
17418945ee5eSChuck Lever 
17428945ee5eSChuck Lever 	sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC",
1743064a9177SNeilBrown 		&xs_slock_key[1], "sk_lock-AF_INET-RPC", &xs_key[1]);
1744ed07536eSPeter Zijlstra }
17458945ee5eSChuck Lever 
17468945ee5eSChuck Lever static inline void xs_reclassify_socket6(struct socket *sock)
17478945ee5eSChuck Lever {
17488945ee5eSChuck Lever 	struct sock *sk = sock->sk;
17498945ee5eSChuck Lever 
17508945ee5eSChuck Lever 	sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC",
1751064a9177SNeilBrown 		&xs_slock_key[2], "sk_lock-AF_INET6-RPC", &xs_key[2]);
1752ed07536eSPeter Zijlstra }
17536bc9638aSPavel Emelyanov 
17546bc9638aSPavel Emelyanov static inline void xs_reclassify_socket(int family, struct socket *sock)
17556bc9638aSPavel Emelyanov {
1756fafc4e1eSHannes Frederic Sowa 	if (WARN_ON_ONCE(!sock_allow_reclassification(sock->sk)))
17571b7a1819SWeston Andros Adamson 		return;
17581b7a1819SWeston Andros Adamson 
17594232e863SChuck Lever 	switch (family) {
1760176e21eeSChuck Lever 	case AF_LOCAL:
1761176e21eeSChuck Lever 		xs_reclassify_socketu(sock);
1762176e21eeSChuck Lever 		break;
17634232e863SChuck Lever 	case AF_INET:
17646bc9638aSPavel Emelyanov 		xs_reclassify_socket4(sock);
17654232e863SChuck Lever 		break;
17664232e863SChuck Lever 	case AF_INET6:
17676bc9638aSPavel Emelyanov 		xs_reclassify_socket6(sock);
17684232e863SChuck Lever 		break;
17694232e863SChuck Lever 	}
17706bc9638aSPavel Emelyanov }
1771ed07536eSPeter Zijlstra #else
17726bc9638aSPavel Emelyanov static inline void xs_reclassify_socket(int family, struct socket *sock)
17736bc9638aSPavel Emelyanov {
17746bc9638aSPavel Emelyanov }
1775ed07536eSPeter Zijlstra #endif
1776ed07536eSPeter Zijlstra 
177793dc41bdSNeilBrown static void xs_dummy_setup_socket(struct work_struct *work)
177893dc41bdSNeilBrown {
177993dc41bdSNeilBrown }
178093dc41bdSNeilBrown 
17816bc9638aSPavel Emelyanov static struct socket *xs_create_sock(struct rpc_xprt *xprt,
17824dda9c8aSTrond Myklebust 		struct sock_xprt *transport, int family, int type,
17834dda9c8aSTrond Myklebust 		int protocol, bool reuseport)
178422f79326SPavel Emelyanov {
1785a73881c9STrond Myklebust 	struct file *filp;
178622f79326SPavel Emelyanov 	struct socket *sock;
178722f79326SPavel Emelyanov 	int err;
178822f79326SPavel Emelyanov 
17896bc9638aSPavel Emelyanov 	err = __sock_create(xprt->xprt_net, family, type, protocol, &sock, 1);
179022f79326SPavel Emelyanov 	if (err < 0) {
179122f79326SPavel Emelyanov 		dprintk("RPC:       can't create %d transport socket (%d).\n",
179222f79326SPavel Emelyanov 				protocol, -err);
179322f79326SPavel Emelyanov 		goto out;
179422f79326SPavel Emelyanov 	}
17956bc9638aSPavel Emelyanov 	xs_reclassify_socket(family, sock);
179622f79326SPavel Emelyanov 
17974dda9c8aSTrond Myklebust 	if (reuseport)
1798fe31a326SChristoph Hellwig 		sock_set_reuseport(sock->sk);
17994dda9c8aSTrond Myklebust 
18004cea288aSBen Hutchings 	err = xs_bind(transport, sock);
18014cea288aSBen Hutchings 	if (err) {
180222f79326SPavel Emelyanov 		sock_release(sock);
180322f79326SPavel Emelyanov 		goto out;
180422f79326SPavel Emelyanov 	}
180522f79326SPavel Emelyanov 
1806a73881c9STrond Myklebust 	filp = sock_alloc_file(sock, O_NONBLOCK, NULL);
1807a73881c9STrond Myklebust 	if (IS_ERR(filp))
1808a73881c9STrond Myklebust 		return ERR_CAST(filp);
1809a73881c9STrond Myklebust 	transport->file = filp;
1810a73881c9STrond Myklebust 
181122f79326SPavel Emelyanov 	return sock;
181222f79326SPavel Emelyanov out:
181322f79326SPavel Emelyanov 	return ERR_PTR(err);
181422f79326SPavel Emelyanov }
181522f79326SPavel Emelyanov 
1816176e21eeSChuck Lever static int xs_local_finish_connecting(struct rpc_xprt *xprt,
1817176e21eeSChuck Lever 				      struct socket *sock)
1818176e21eeSChuck Lever {
1819176e21eeSChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
1820176e21eeSChuck Lever 									xprt);
1821176e21eeSChuck Lever 
1822176e21eeSChuck Lever 	if (!transport->inet) {
1823176e21eeSChuck Lever 		struct sock *sk = sock->sk;
1824176e21eeSChuck Lever 
1825ea9afca8STrond Myklebust 		lock_sock(sk);
1826176e21eeSChuck Lever 
1827176e21eeSChuck Lever 		xs_save_old_callbacks(transport, sk);
1828176e21eeSChuck Lever 
1829176e21eeSChuck Lever 		sk->sk_user_data = xprt;
1830a2648094STrond Myklebust 		sk->sk_data_ready = xs_data_ready;
1831176e21eeSChuck Lever 		sk->sk_write_space = xs_udp_write_space;
18322118071dSTrond Myklebust 		sk->sk_error_report = xs_error_report;
1833176e21eeSChuck Lever 
1834176e21eeSChuck Lever 		xprt_clear_connected(xprt);
1835176e21eeSChuck Lever 
1836176e21eeSChuck Lever 		/* Reset to new socket */
1837176e21eeSChuck Lever 		transport->sock = sock;
1838176e21eeSChuck Lever 		transport->inet = sk;
1839176e21eeSChuck Lever 
1840ea9afca8STrond Myklebust 		release_sock(sk);
1841176e21eeSChuck Lever 	}
1842176e21eeSChuck Lever 
1843ae053551STrond Myklebust 	xs_stream_start_connect(transport);
18446c7a64e5STrond Myklebust 
1845176e21eeSChuck Lever 	return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, 0);
1846176e21eeSChuck Lever }
1847176e21eeSChuck Lever 
1848176e21eeSChuck Lever /**
1849176e21eeSChuck Lever  * xs_local_setup_socket - create AF_LOCAL socket, connect to a local endpoint
1850176e21eeSChuck Lever  * @transport: socket transport to connect
1851176e21eeSChuck Lever  */
1852dc107402SJ. Bruce Fields static int xs_local_setup_socket(struct sock_xprt *transport)
1853176e21eeSChuck Lever {
1854176e21eeSChuck Lever 	struct rpc_xprt *xprt = &transport->xprt;
1855a73881c9STrond Myklebust 	struct file *filp;
1856176e21eeSChuck Lever 	struct socket *sock;
185768e9a246SColin Ian King 	int status;
1858176e21eeSChuck Lever 
1859176e21eeSChuck Lever 	status = __sock_create(xprt->xprt_net, AF_LOCAL,
1860176e21eeSChuck Lever 					SOCK_STREAM, 0, &sock, 1);
1861176e21eeSChuck Lever 	if (status < 0) {
1862176e21eeSChuck Lever 		dprintk("RPC:       can't create AF_LOCAL "
1863176e21eeSChuck Lever 			"transport socket (%d).\n", -status);
1864176e21eeSChuck Lever 		goto out;
1865176e21eeSChuck Lever 	}
1866d1358917SStefan Hajnoczi 	xs_reclassify_socket(AF_LOCAL, sock);
1867176e21eeSChuck Lever 
1868a73881c9STrond Myklebust 	filp = sock_alloc_file(sock, O_NONBLOCK, NULL);
1869a73881c9STrond Myklebust 	if (IS_ERR(filp)) {
1870a73881c9STrond Myklebust 		status = PTR_ERR(filp);
1871a73881c9STrond Myklebust 		goto out;
1872a73881c9STrond Myklebust 	}
1873a73881c9STrond Myklebust 	transport->file = filp;
1874a73881c9STrond Myklebust 
1875176e21eeSChuck Lever 	dprintk("RPC:       worker connecting xprt %p via AF_LOCAL to %s\n",
1876176e21eeSChuck Lever 			xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1877176e21eeSChuck Lever 
1878176e21eeSChuck Lever 	status = xs_local_finish_connecting(xprt, sock);
187940b5ea0cSTrond Myklebust 	trace_rpc_socket_connect(xprt, sock, status);
1880176e21eeSChuck Lever 	switch (status) {
1881176e21eeSChuck Lever 	case 0:
1882176e21eeSChuck Lever 		dprintk("RPC:       xprt %p connected to %s\n",
1883176e21eeSChuck Lever 				xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
18843968a8a5SChuck Lever 		xprt->stat.connect_count++;
18853968a8a5SChuck Lever 		xprt->stat.connect_time += (long)jiffies -
18863968a8a5SChuck Lever 					   xprt->stat.connect_start;
1887176e21eeSChuck Lever 		xprt_set_connected(xprt);
188893f479d3SGustavo A. R. Silva 		break;
18893601c4a9STrond Myklebust 	case -ENOBUFS:
1890176e21eeSChuck Lever 		break;
1891176e21eeSChuck Lever 	case -ENOENT:
1892176e21eeSChuck Lever 		dprintk("RPC:       xprt %p: socket %s does not exist\n",
1893176e21eeSChuck Lever 				xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1894176e21eeSChuck Lever 		break;
18954a20a988STrond Myklebust 	case -ECONNREFUSED:
18964a20a988STrond Myklebust 		dprintk("RPC:       xprt %p: connection refused for %s\n",
18974a20a988STrond Myklebust 				xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
18984a20a988STrond Myklebust 		break;
1899176e21eeSChuck Lever 	default:
1900176e21eeSChuck Lever 		printk(KERN_ERR "%s: unhandled error (%d) connecting to %s\n",
1901176e21eeSChuck Lever 				__func__, -status,
1902176e21eeSChuck Lever 				xprt->address_strings[RPC_DISPLAY_ADDR]);
1903176e21eeSChuck Lever 	}
1904176e21eeSChuck Lever 
1905176e21eeSChuck Lever out:
1906176e21eeSChuck Lever 	xprt_clear_connecting(xprt);
1907176e21eeSChuck Lever 	xprt_wake_pending_tasks(xprt, status);
1908dc107402SJ. Bruce Fields 	return status;
1909dc107402SJ. Bruce Fields }
1910dc107402SJ. Bruce Fields 
1911b6669737SLinus Torvalds static void xs_local_connect(struct rpc_xprt *xprt, struct rpc_task *task)
1912dc107402SJ. Bruce Fields {
1913dc107402SJ. Bruce Fields 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1914dc107402SJ. Bruce Fields 	int ret;
1915dc107402SJ. Bruce Fields 
1916dc107402SJ. Bruce Fields 	if (RPC_IS_ASYNC(task)) {
1917dc107402SJ. Bruce Fields 		/*
1918dc107402SJ. Bruce Fields 		 * We want the AF_LOCAL connect to be resolved in the
1919dc107402SJ. Bruce Fields 		 * filesystem namespace of the process making the rpc
1920dc107402SJ. Bruce Fields 		 * call.  Thus we connect synchronously.
1921dc107402SJ. Bruce Fields 		 *
1922dc107402SJ. Bruce Fields 		 * If we want to support asynchronous AF_LOCAL calls,
1923dc107402SJ. Bruce Fields 		 * we'll need to figure out how to pass a namespace to
1924dc107402SJ. Bruce Fields 		 * connect.
1925dc107402SJ. Bruce Fields 		 */
19265ad64b36STrond Myklebust 		task->tk_rpc_status = -ENOTCONN;
1927dc107402SJ. Bruce Fields 		rpc_exit(task, -ENOTCONN);
1928dc107402SJ. Bruce Fields 		return;
1929dc107402SJ. Bruce Fields 	}
1930dc107402SJ. Bruce Fields 	ret = xs_local_setup_socket(transport);
1931dc107402SJ. Bruce Fields 	if (ret && !RPC_IS_SOFTCONN(task))
1932dc107402SJ. Bruce Fields 		msleep_interruptible(15000);
1933176e21eeSChuck Lever }
1934176e21eeSChuck Lever 
19353c87ef6eSJeff Layton #if IS_ENABLED(CONFIG_SUNRPC_SWAP)
1936d6e971d8SJeff Layton /*
1937693486d5SNeilBrown  * Note that this should be called with XPRT_LOCKED held, or recv_mutex
1938693486d5SNeilBrown  * held, or when we otherwise know that we have exclusive access to the
1939693486d5SNeilBrown  * socket, to guard against races with xs_reset_transport.
1940d6e971d8SJeff Layton  */
1941a564b8f0SMel Gorman static void xs_set_memalloc(struct rpc_xprt *xprt)
1942a564b8f0SMel Gorman {
1943a564b8f0SMel Gorman 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
1944a564b8f0SMel Gorman 			xprt);
1945a564b8f0SMel Gorman 
1946d6e971d8SJeff Layton 	/*
1947d6e971d8SJeff Layton 	 * If there's no sock, then we have nothing to set. The
1948d6e971d8SJeff Layton 	 * reconnecting process will get it for us.
1949d6e971d8SJeff Layton 	 */
1950d6e971d8SJeff Layton 	if (!transport->inet)
1951d6e971d8SJeff Layton 		return;
19528e228133SJeff Layton 	if (atomic_read(&xprt->swapper))
1953a564b8f0SMel Gorman 		sk_set_memalloc(transport->inet);
1954a564b8f0SMel Gorman }
1955a564b8f0SMel Gorman 
1956a564b8f0SMel Gorman /**
1957d67fa4d8SJeff Layton  * xs_enable_swap - Tag this transport as being used for swap.
1958a564b8f0SMel Gorman  * @xprt: transport to tag
1959a564b8f0SMel Gorman  *
19608e228133SJeff Layton  * Take a reference to this transport on behalf of the rpc_clnt, and
19618e228133SJeff Layton  * optionally mark it for swapping if it wasn't already.
1962a564b8f0SMel Gorman  */
1963d67fa4d8SJeff Layton static int
1964d67fa4d8SJeff Layton xs_enable_swap(struct rpc_xprt *xprt)
1965a564b8f0SMel Gorman {
1966d6e971d8SJeff Layton 	struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt);
1967a564b8f0SMel Gorman 
1968693486d5SNeilBrown 	mutex_lock(&xs->recv_mutex);
1969693486d5SNeilBrown 	if (atomic_inc_return(&xprt->swapper) == 1 &&
1970693486d5SNeilBrown 	    xs->inet)
1971d6e971d8SJeff Layton 		sk_set_memalloc(xs->inet);
1972693486d5SNeilBrown 	mutex_unlock(&xs->recv_mutex);
19738e228133SJeff Layton 	return 0;
1974a564b8f0SMel Gorman }
1975a564b8f0SMel Gorman 
19768e228133SJeff Layton /**
1977d67fa4d8SJeff Layton  * xs_disable_swap - Untag this transport as being used for swap.
19788e228133SJeff Layton  * @xprt: transport to tag
19798e228133SJeff Layton  *
19808e228133SJeff Layton  * Drop a "swapper" reference to this xprt on behalf of the rpc_clnt. If the
19818e228133SJeff Layton  * swapper refcount goes to 0, untag the socket as a memalloc socket.
19828e228133SJeff Layton  */
1983d67fa4d8SJeff Layton static void
1984d67fa4d8SJeff Layton xs_disable_swap(struct rpc_xprt *xprt)
19858e228133SJeff Layton {
1986d6e971d8SJeff Layton 	struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt);
19878e228133SJeff Layton 
1988693486d5SNeilBrown 	mutex_lock(&xs->recv_mutex);
1989693486d5SNeilBrown 	if (atomic_dec_and_test(&xprt->swapper) &&
1990693486d5SNeilBrown 	    xs->inet)
1991d6e971d8SJeff Layton 		sk_clear_memalloc(xs->inet);
1992693486d5SNeilBrown 	mutex_unlock(&xs->recv_mutex);
1993a564b8f0SMel Gorman }
1994a564b8f0SMel Gorman #else
1995a564b8f0SMel Gorman static void xs_set_memalloc(struct rpc_xprt *xprt)
1996a564b8f0SMel Gorman {
1997a564b8f0SMel Gorman }
1998d67fa4d8SJeff Layton 
1999d67fa4d8SJeff Layton static int
2000d67fa4d8SJeff Layton xs_enable_swap(struct rpc_xprt *xprt)
2001d67fa4d8SJeff Layton {
2002d67fa4d8SJeff Layton 	return -EINVAL;
2003d67fa4d8SJeff Layton }
2004d67fa4d8SJeff Layton 
2005d67fa4d8SJeff Layton static void
2006d67fa4d8SJeff Layton xs_disable_swap(struct rpc_xprt *xprt)
2007d67fa4d8SJeff Layton {
2008d67fa4d8SJeff Layton }
2009a564b8f0SMel Gorman #endif
2010a564b8f0SMel Gorman 
201116be2d20SChuck Lever static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
2012a246b010SChuck Lever {
201316be2d20SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2014edb267a6SChuck Lever 
2015ee0ac0c2SChuck Lever 	if (!transport->inet) {
2016b0d93ad5SChuck Lever 		struct sock *sk = sock->sk;
2017b0d93ad5SChuck Lever 
2018ea9afca8STrond Myklebust 		lock_sock(sk);
2019b0d93ad5SChuck Lever 
20202a9e1cfaSTrond Myklebust 		xs_save_old_callbacks(transport, sk);
20212a9e1cfaSTrond Myklebust 
2022b0d93ad5SChuck Lever 		sk->sk_user_data = xprt;
2023f9b2ee71STrond Myklebust 		sk->sk_data_ready = xs_data_ready;
2024b0d93ad5SChuck Lever 		sk->sk_write_space = xs_udp_write_space;
2025b0d93ad5SChuck Lever 
2026b0d93ad5SChuck Lever 		xprt_set_connected(xprt);
2027b0d93ad5SChuck Lever 
2028b0d93ad5SChuck Lever 		/* Reset to new socket */
2029ee0ac0c2SChuck Lever 		transport->sock = sock;
2030ee0ac0c2SChuck Lever 		transport->inet = sk;
2031b0d93ad5SChuck Lever 
2032a564b8f0SMel Gorman 		xs_set_memalloc(xprt);
2033a564b8f0SMel Gorman 
2034ea9afca8STrond Myklebust 		release_sock(sk);
2035b0d93ad5SChuck Lever 	}
2036470056c2SChuck Lever 	xs_udp_do_set_buffer_size(xprt);
203702910177STrond Myklebust 
203802910177STrond Myklebust 	xprt->stat.connect_start = jiffies;
203916be2d20SChuck Lever }
204016be2d20SChuck Lever 
20418c14ff2aSPavel Emelyanov static void xs_udp_setup_socket(struct work_struct *work)
2042a246b010SChuck Lever {
2043a246b010SChuck Lever 	struct sock_xprt *transport =
2044a246b010SChuck Lever 		container_of(work, struct sock_xprt, connect_worker.work);
2045a246b010SChuck Lever 	struct rpc_xprt *xprt = &transport->xprt;
2046d099b8afSColin Ian King 	struct socket *sock;
2047b65c0310SPavel Emelyanov 	int status = -EIO;
20488db55a03SNeilBrown 	unsigned int pflags = current->flags;
2049a246b010SChuck Lever 
20508db55a03SNeilBrown 	if (atomic_read(&xprt->swapper))
20518db55a03SNeilBrown 		current->flags |= PF_MEMALLOC;
20528c14ff2aSPavel Emelyanov 	sock = xs_create_sock(xprt, transport,
20534dda9c8aSTrond Myklebust 			xs_addr(xprt)->sa_family, SOCK_DGRAM,
20544dda9c8aSTrond Myklebust 			IPPROTO_UDP, false);
2055b65c0310SPavel Emelyanov 	if (IS_ERR(sock))
2056a246b010SChuck Lever 		goto out;
205768e220bdSChuck Lever 
2058c740eff8SChuck Lever 	dprintk("RPC:       worker connecting xprt %p via %s to "
2059c740eff8SChuck Lever 				"%s (port %s)\n", xprt,
2060c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_PROTO],
2061c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_ADDR],
2062c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_PORT]);
206368e220bdSChuck Lever 
206468e220bdSChuck Lever 	xs_udp_finish_connecting(xprt, sock);
206540b5ea0cSTrond Myklebust 	trace_rpc_socket_connect(xprt, sock, 0);
2066a246b010SChuck Lever 	status = 0;
2067b0d93ad5SChuck Lever out:
2068b0d93ad5SChuck Lever 	xprt_clear_connecting(xprt);
2069cf76785dSTrond Myklebust 	xprt_unlock_connect(xprt, transport);
20707d1e8255STrond Myklebust 	xprt_wake_pending_tasks(xprt, status);
20718db55a03SNeilBrown 	current_restore_flags(pflags, PF_MEMALLOC);
2072b0d93ad5SChuck Lever }
2073b0d93ad5SChuck Lever 
20744876cc77STrond Myklebust /**
20754876cc77STrond Myklebust  * xs_tcp_shutdown - gracefully shut down a TCP socket
20764876cc77STrond Myklebust  * @xprt: transport
20774876cc77STrond Myklebust  *
20784876cc77STrond Myklebust  * Initiates a graceful shutdown of the TCP socket by calling the
20794876cc77STrond Myklebust  * equivalent of shutdown(SHUT_RDWR);
20804876cc77STrond Myklebust  */
20814876cc77STrond Myklebust static void xs_tcp_shutdown(struct rpc_xprt *xprt)
20824876cc77STrond Myklebust {
20834876cc77STrond Myklebust 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
20844876cc77STrond Myklebust 	struct socket *sock = transport->sock;
20859b30889cSTrond Myklebust 	int skst = transport->inet ? transport->inet->sk_state : TCP_CLOSE;
20864876cc77STrond Myklebust 
20874876cc77STrond Myklebust 	if (sock == NULL)
20884876cc77STrond Myklebust 		return;
20890a6ff58eSTrond Myklebust 	if (!xprt->reuseport) {
20900a6ff58eSTrond Myklebust 		xs_close(xprt);
20910a6ff58eSTrond Myklebust 		return;
20920a6ff58eSTrond Myklebust 	}
20939b30889cSTrond Myklebust 	switch (skst) {
20947c81e6a9STrond Myklebust 	case TCP_FIN_WAIT1:
20957c81e6a9STrond Myklebust 	case TCP_FIN_WAIT2:
20967c81e6a9STrond Myklebust 		break;
20977c81e6a9STrond Myklebust 	case TCP_ESTABLISHED:
20987c81e6a9STrond Myklebust 	case TCP_CLOSE_WAIT:
20994876cc77STrond Myklebust 		kernel_sock_shutdown(sock, SHUT_RDWR);
21004876cc77STrond Myklebust 		trace_rpc_socket_shutdown(xprt, sock);
21019b30889cSTrond Myklebust 		break;
21027c81e6a9STrond Myklebust 	default:
21034876cc77STrond Myklebust 		xs_reset_transport(transport);
21044876cc77STrond Myklebust 	}
21059b30889cSTrond Myklebust }
21064876cc77STrond Myklebust 
21078d1b8c62STrond Myklebust static void xs_tcp_set_socket_timeouts(struct rpc_xprt *xprt,
21088d1b8c62STrond Myklebust 		struct socket *sock)
2109b0d93ad5SChuck Lever {
211016be2d20SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
21117196dbb0STrond Myklebust 	unsigned int keepidle;
21127196dbb0STrond Myklebust 	unsigned int keepcnt;
2113775f06abSTrond Myklebust 	unsigned int timeo;
21147f260e85STrond Myklebust 
2115b5e92419STrond Myklebust 	spin_lock(&xprt->transport_lock);
21167196dbb0STrond Myklebust 	keepidle = DIV_ROUND_UP(xprt->timeout->to_initval, HZ);
21177196dbb0STrond Myklebust 	keepcnt = xprt->timeout->to_retries + 1;
21187196dbb0STrond Myklebust 	timeo = jiffies_to_msecs(xprt->timeout->to_initval) *
21197196dbb0STrond Myklebust 		(xprt->timeout->to_retries + 1);
21207196dbb0STrond Myklebust 	clear_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state);
2121b5e92419STrond Myklebust 	spin_unlock(&xprt->transport_lock);
21227f260e85STrond Myklebust 
21237f260e85STrond Myklebust 	/* TCP Keepalive options */
2124ce3d9544SChristoph Hellwig 	sock_set_keepalive(sock->sk);
212571c48eb8SChristoph Hellwig 	tcp_sock_set_keepidle(sock->sk, keepidle);
2126d41ecaacSChristoph Hellwig 	tcp_sock_set_keepintvl(sock->sk, keepidle);
2127480aeb96SChristoph Hellwig 	tcp_sock_set_keepcnt(sock->sk, keepcnt);
2128b0d93ad5SChuck Lever 
21298d1b8c62STrond Myklebust 	/* TCP user timeout (see RFC5482) */
2130c488aeadSChristoph Hellwig 	tcp_sock_set_user_timeout(sock->sk, timeo);
21318d1b8c62STrond Myklebust }
21328d1b8c62STrond Myklebust 
21337196dbb0STrond Myklebust static void xs_tcp_set_connect_timeout(struct rpc_xprt *xprt,
21347196dbb0STrond Myklebust 		unsigned long connect_timeout,
21357196dbb0STrond Myklebust 		unsigned long reconnect_timeout)
21367196dbb0STrond Myklebust {
21377196dbb0STrond Myklebust 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
21387196dbb0STrond Myklebust 	struct rpc_timeout to;
21397196dbb0STrond Myklebust 	unsigned long initval;
21407196dbb0STrond Myklebust 
2141b5e92419STrond Myklebust 	spin_lock(&xprt->transport_lock);
21427196dbb0STrond Myklebust 	if (reconnect_timeout < xprt->max_reconnect_timeout)
21437196dbb0STrond Myklebust 		xprt->max_reconnect_timeout = reconnect_timeout;
21447196dbb0STrond Myklebust 	if (connect_timeout < xprt->connect_timeout) {
21457196dbb0STrond Myklebust 		memcpy(&to, xprt->timeout, sizeof(to));
21467196dbb0STrond Myklebust 		initval = DIV_ROUND_UP(connect_timeout, to.to_retries + 1);
21477196dbb0STrond Myklebust 		/* Arbitrary lower limit */
21487196dbb0STrond Myklebust 		if (initval <  XS_TCP_INIT_REEST_TO << 1)
21497196dbb0STrond Myklebust 			initval = XS_TCP_INIT_REEST_TO << 1;
21507196dbb0STrond Myklebust 		to.to_initval = initval;
21517196dbb0STrond Myklebust 		to.to_maxval = initval;
21527196dbb0STrond Myklebust 		memcpy(&transport->tcp_timeout, &to,
21537196dbb0STrond Myklebust 				sizeof(transport->tcp_timeout));
21547196dbb0STrond Myklebust 		xprt->timeout = &transport->tcp_timeout;
21557196dbb0STrond Myklebust 		xprt->connect_timeout = connect_timeout;
21567196dbb0STrond Myklebust 	}
21577196dbb0STrond Myklebust 	set_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state);
2158b5e92419STrond Myklebust 	spin_unlock(&xprt->transport_lock);
21597196dbb0STrond Myklebust }
21607196dbb0STrond Myklebust 
21618d1b8c62STrond Myklebust static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
21628d1b8c62STrond Myklebust {
21638d1b8c62STrond Myklebust 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
21648d1b8c62STrond Myklebust 
21658d1b8c62STrond Myklebust 	if (!transport->inet) {
21668d1b8c62STrond Myklebust 		struct sock *sk = sock->sk;
21678d1b8c62STrond Myklebust 
2168d88e4d82SNeilBrown 		/* Avoid temporary address, they are bad for long-lived
2169d88e4d82SNeilBrown 		 * connections such as NFS mounts.
2170d88e4d82SNeilBrown 		 * RFC4941, section 3.6 suggests that:
2171d88e4d82SNeilBrown 		 *    Individual applications, which have specific
2172d88e4d82SNeilBrown 		 *    knowledge about the normal duration of connections,
2173d88e4d82SNeilBrown 		 *    MAY override this as appropriate.
2174d88e4d82SNeilBrown 		 */
217518d5ad62SChristoph Hellwig 		if (xs_addr(xprt)->sa_family == PF_INET6) {
217618d5ad62SChristoph Hellwig 			ip6_sock_set_addr_preferences(sk,
217718d5ad62SChristoph Hellwig 				IPV6_PREFER_SRC_PUBLIC);
217818d5ad62SChristoph Hellwig 		}
2179d88e4d82SNeilBrown 
21808d1b8c62STrond Myklebust 		xs_tcp_set_socket_timeouts(xprt, sock);
2181d737e5d4STrond Myklebust 		tcp_sock_set_nodelay(sk);
2182775f06abSTrond Myklebust 
2183ea9afca8STrond Myklebust 		lock_sock(sk);
2184b0d93ad5SChuck Lever 
21852a9e1cfaSTrond Myklebust 		xs_save_old_callbacks(transport, sk);
21862a9e1cfaSTrond Myklebust 
2187b0d93ad5SChuck Lever 		sk->sk_user_data = xprt;
21885157b956STrond Myklebust 		sk->sk_data_ready = xs_data_ready;
2189b0d93ad5SChuck Lever 		sk->sk_state_change = xs_tcp_state_change;
2190b0d93ad5SChuck Lever 		sk->sk_write_space = xs_tcp_write_space;
21912118071dSTrond Myklebust 		sk->sk_error_report = xs_error_report;
21923167e12cSChuck Lever 
21933167e12cSChuck Lever 		/* socket options */
21943167e12cSChuck Lever 		sock_reset_flag(sk, SOCK_LINGER);
2195b0d93ad5SChuck Lever 
2196b0d93ad5SChuck Lever 		xprt_clear_connected(xprt);
2197b0d93ad5SChuck Lever 
2198b0d93ad5SChuck Lever 		/* Reset to new socket */
2199ee0ac0c2SChuck Lever 		transport->sock = sock;
2200ee0ac0c2SChuck Lever 		transport->inet = sk;
2201b0d93ad5SChuck Lever 
2202ea9afca8STrond Myklebust 		release_sock(sk);
2203b0d93ad5SChuck Lever 	}
2204b0d93ad5SChuck Lever 
220501d37c42STrond Myklebust 	if (!xprt_bound(xprt))
2206280254b6STrond Myklebust 		return -ENOTCONN;
220701d37c42STrond Myklebust 
2208a564b8f0SMel Gorman 	xs_set_memalloc(xprt);
2209a564b8f0SMel Gorman 
2210ae053551STrond Myklebust 	xs_stream_start_connect(transport);
2211e1806c7bSTrond Myklebust 
2212b0d93ad5SChuck Lever 	/* Tell the socket layer to start connecting... */
22130fdea1e8STrond Myklebust 	set_bit(XPRT_SOCK_CONNECTING, &transport->sock_state);
2214280254b6STrond Myklebust 	return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK);
221516be2d20SChuck Lever }
221616be2d20SChuck Lever 
221716be2d20SChuck Lever /**
2218b61d59ffSTrond Myklebust  * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint
2219acf0a39fSChuck Lever  * @work: queued work item
222016be2d20SChuck Lever  *
222116be2d20SChuck Lever  * Invoked by a work queue tasklet.
222216be2d20SChuck Lever  */
2223cdd518d5SPavel Emelyanov static void xs_tcp_setup_socket(struct work_struct *work)
222416be2d20SChuck Lever {
2225cdd518d5SPavel Emelyanov 	struct sock_xprt *transport =
2226cdd518d5SPavel Emelyanov 		container_of(work, struct sock_xprt, connect_worker.work);
222716be2d20SChuck Lever 	struct socket *sock = transport->sock;
2228a9f5f0f7SPavel Emelyanov 	struct rpc_xprt *xprt = &transport->xprt;
2229280254b6STrond Myklebust 	int status;
22308db55a03SNeilBrown 	unsigned int pflags = current->flags;
223116be2d20SChuck Lever 
22328db55a03SNeilBrown 	if (atomic_read(&xprt->swapper))
22338db55a03SNeilBrown 		current->flags |= PF_MEMALLOC;
223489f42494STrond Myklebust 
223589f42494STrond Myklebust 	if (xprt_connected(xprt))
223689f42494STrond Myklebust 		goto out;
223789f42494STrond Myklebust 	if (test_and_clear_bit(XPRT_SOCK_CONNECT_SENT,
223889f42494STrond Myklebust 			       &transport->sock_state) ||
223989f42494STrond Myklebust 	    !sock) {
224089f42494STrond Myklebust 		xs_reset_transport(transport);
224189f42494STrond Myklebust 		sock = xs_create_sock(xprt, transport, xs_addr(xprt)->sa_family,
224289f42494STrond Myklebust 				      SOCK_STREAM, IPPROTO_TCP, true);
2243b61d59ffSTrond Myklebust 		if (IS_ERR(sock)) {
2244280254b6STrond Myklebust 			xprt_wake_pending_tasks(xprt, PTR_ERR(sock));
224516be2d20SChuck Lever 			goto out;
224616be2d20SChuck Lever 		}
22477d1e8255STrond Myklebust 	}
22487d1e8255STrond Myklebust 
2249c740eff8SChuck Lever 	dprintk("RPC:       worker connecting xprt %p via %s to "
2250c740eff8SChuck Lever 				"%s (port %s)\n", xprt,
2251c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_PROTO],
2252c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_ADDR],
2253c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_PORT]);
225416be2d20SChuck Lever 
225516be2d20SChuck Lever 	status = xs_tcp_finish_connecting(xprt, sock);
225640b5ea0cSTrond Myklebust 	trace_rpc_socket_connect(xprt, sock, status);
2257a246b010SChuck Lever 	dprintk("RPC:       %p connect status %d connected %d sock state %d\n",
225846121cf7SChuck Lever 			xprt, -status, xprt_connected(xprt),
225946121cf7SChuck Lever 			sock->sk->sk_state);
2260a246b010SChuck Lever 	switch (status) {
22612a491991STrond Myklebust 	case 0:
2262a246b010SChuck Lever 	case -EINPROGRESS:
2263280254b6STrond Myklebust 		/* SYN_SENT! */
226489f42494STrond Myklebust 		set_bit(XPRT_SOCK_CONNECT_SENT, &transport->sock_state);
2265280254b6STrond Myklebust 		if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
2266280254b6STrond Myklebust 			xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2267280254b6STrond Myklebust 		fallthrough;
2268a246b010SChuck Lever 	case -EALREADY:
2269280254b6STrond Myklebust 		goto out_unlock;
2270280254b6STrond Myklebust 	case -EADDRNOTAVAIL:
2271280254b6STrond Myklebust 		/* Source port number is unavailable. Try a new one! */
2272280254b6STrond Myklebust 		transport->srcport = 0;
2273280254b6STrond Myklebust 		status = -EAGAIN;
2274280254b6STrond Myklebust 		break;
22759fcfe0c8STrond Myklebust 	case -EINVAL:
22769fcfe0c8STrond Myklebust 		/* Happens, for instance, if the user specified a link
22779fcfe0c8STrond Myklebust 		 * local IPv6 address without a scope-id.
22789fcfe0c8STrond Myklebust 		 */
22793ed5e2a2STrond Myklebust 	case -ECONNREFUSED:
22803ed5e2a2STrond Myklebust 	case -ECONNRESET:
2281eb5b46faSTrond Myklebust 	case -ENETDOWN:
22823ed5e2a2STrond Myklebust 	case -ENETUNREACH:
22834ba161a7STrond Myklebust 	case -EHOSTUNREACH:
22843913c78cSTrond Myklebust 	case -EADDRINUSE:
22853601c4a9STrond Myklebust 	case -ENOBUFS:
2286280254b6STrond Myklebust 		break;
2287280254b6STrond Myklebust 	default:
2288280254b6STrond Myklebust 		printk("%s: connect returned unhandled error %d\n",
2289280254b6STrond Myklebust 			__func__, status);
2290280254b6STrond Myklebust 		status = -EAGAIN;
2291280254b6STrond Myklebust 	}
2292280254b6STrond Myklebust 
22938c71139dSCalum Mackay 	/* xs_tcp_force_close() wakes tasks with a fixed error code.
22948c71139dSCalum Mackay 	 * We need to wake them first to ensure the correct error code.
22956ea44adcSNeilBrown 	 */
22966ea44adcSNeilBrown 	xprt_wake_pending_tasks(xprt, status);
22974efdd92cSTrond Myklebust 	xs_tcp_force_close(xprt);
2298a246b010SChuck Lever out:
22992226feb6SChuck Lever 	xprt_clear_connecting(xprt);
2300280254b6STrond Myklebust out_unlock:
2301cf76785dSTrond Myklebust 	xprt_unlock_connect(xprt, transport);
23028db55a03SNeilBrown 	current_restore_flags(pflags, PF_MEMALLOC);
2303a246b010SChuck Lever }
2304a246b010SChuck Lever 
230568e220bdSChuck Lever /**
23069903cd1cSChuck Lever  * xs_connect - connect a socket to a remote endpoint
23071b092092STrond Myklebust  * @xprt: pointer to transport structure
23089903cd1cSChuck Lever  * @task: address of RPC task that manages state of connect request
23099903cd1cSChuck Lever  *
23109903cd1cSChuck Lever  * TCP: If the remote end dropped the connection, delay reconnecting.
231103bf4b70SChuck Lever  *
231203bf4b70SChuck Lever  * UDP socket connects are synchronous, but we use a work queue anyway
231303bf4b70SChuck Lever  * to guarantee that even unprivileged user processes can set up a
231403bf4b70SChuck Lever  * socket on a privileged port.
231503bf4b70SChuck Lever  *
231603bf4b70SChuck Lever  * If a UDP socket connect fails, the delay behavior here prevents
231703bf4b70SChuck Lever  * retry floods (hard mounts).
23189903cd1cSChuck Lever  */
23191b092092STrond Myklebust static void xs_connect(struct rpc_xprt *xprt, struct rpc_task *task)
2320a246b010SChuck Lever {
2321ee0ac0c2SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
232202910177STrond Myklebust 	unsigned long delay = 0;
2323a246b010SChuck Lever 
2324718ba5b8STrond Myklebust 	WARN_ON_ONCE(!xprt_lock_connect(xprt, task, transport));
2325718ba5b8STrond Myklebust 
232689f42494STrond Myklebust 	if (transport->sock != NULL) {
232746121cf7SChuck Lever 		dprintk("RPC:       xs_connect delayed xprt %p for %lu "
232889f42494STrond Myklebust 			"seconds\n", xprt, xprt->reestablish_timeout / HZ);
232999b1a4c3STrond Myklebust 
2330675dd90aSChuck Lever 		delay = xprt_reconnect_delay(xprt);
2331675dd90aSChuck Lever 		xprt_reconnect_backoff(xprt, XS_TCP_INIT_REEST_TO);
233202910177STrond Myklebust 
233302910177STrond Myklebust 	} else
23349903cd1cSChuck Lever 		dprintk("RPC:       xs_connect scheduled xprt %p\n", xprt);
233502910177STrond Myklebust 
233640a5f1b1STrond Myklebust 	queue_delayed_work(xprtiod_workqueue,
233702910177STrond Myklebust 			&transport->connect_worker,
233802910177STrond Myklebust 			delay);
2339a246b010SChuck Lever }
2340a246b010SChuck Lever 
23414f8943f8STrond Myklebust static void xs_wake_disconnect(struct sock_xprt *transport)
23424f8943f8STrond Myklebust {
23434f8943f8STrond Myklebust 	if (test_and_clear_bit(XPRT_SOCK_WAKE_DISCONNECT, &transport->sock_state))
23444f8943f8STrond Myklebust 		xs_tcp_force_close(&transport->xprt);
23454f8943f8STrond Myklebust }
23464f8943f8STrond Myklebust 
23474f8943f8STrond Myklebust static void xs_wake_write(struct sock_xprt *transport)
23484f8943f8STrond Myklebust {
23494f8943f8STrond Myklebust 	if (test_and_clear_bit(XPRT_SOCK_WAKE_WRITE, &transport->sock_state))
23504f8943f8STrond Myklebust 		xprt_write_space(&transport->xprt);
23514f8943f8STrond Myklebust }
23524f8943f8STrond Myklebust 
23534f8943f8STrond Myklebust static void xs_wake_error(struct sock_xprt *transport)
23544f8943f8STrond Myklebust {
23554f8943f8STrond Myklebust 	int sockerr;
23564f8943f8STrond Myklebust 
23574f8943f8STrond Myklebust 	if (!test_bit(XPRT_SOCK_WAKE_ERROR, &transport->sock_state))
23584f8943f8STrond Myklebust 		return;
23594f8943f8STrond Myklebust 	mutex_lock(&transport->recv_mutex);
23604f8943f8STrond Myklebust 	if (transport->sock == NULL)
23614f8943f8STrond Myklebust 		goto out;
23624f8943f8STrond Myklebust 	if (!test_and_clear_bit(XPRT_SOCK_WAKE_ERROR, &transport->sock_state))
23634f8943f8STrond Myklebust 		goto out;
2364af84537dSBenjamin Coddington 	sockerr = xchg(&transport->xprt_err, 0);
23654f8943f8STrond Myklebust 	if (sockerr < 0)
23664f8943f8STrond Myklebust 		xprt_wake_pending_tasks(&transport->xprt, sockerr);
23674f8943f8STrond Myklebust out:
23684f8943f8STrond Myklebust 	mutex_unlock(&transport->recv_mutex);
23694f8943f8STrond Myklebust }
23704f8943f8STrond Myklebust 
23714f8943f8STrond Myklebust static void xs_wake_pending(struct sock_xprt *transport)
23724f8943f8STrond Myklebust {
23734f8943f8STrond Myklebust 	if (test_and_clear_bit(XPRT_SOCK_WAKE_PENDING, &transport->sock_state))
23744f8943f8STrond Myklebust 		xprt_wake_pending_tasks(&transport->xprt, -EAGAIN);
23754f8943f8STrond Myklebust }
23764f8943f8STrond Myklebust 
23774f8943f8STrond Myklebust static void xs_error_handle(struct work_struct *work)
23784f8943f8STrond Myklebust {
23794f8943f8STrond Myklebust 	struct sock_xprt *transport = container_of(work,
23804f8943f8STrond Myklebust 			struct sock_xprt, error_worker);
23814f8943f8STrond Myklebust 
23824f8943f8STrond Myklebust 	xs_wake_disconnect(transport);
23834f8943f8STrond Myklebust 	xs_wake_write(transport);
23844f8943f8STrond Myklebust 	xs_wake_error(transport);
23854f8943f8STrond Myklebust 	xs_wake_pending(transport);
23864f8943f8STrond Myklebust }
23874f8943f8STrond Myklebust 
2388262ca07dSChuck Lever /**
238912b20ce3SBhaskar Chowdhury  * xs_local_print_stats - display AF_LOCAL socket-specific stats
2390176e21eeSChuck Lever  * @xprt: rpc_xprt struct containing statistics
2391176e21eeSChuck Lever  * @seq: output file
2392176e21eeSChuck Lever  *
2393176e21eeSChuck Lever  */
2394176e21eeSChuck Lever static void xs_local_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2395176e21eeSChuck Lever {
2396176e21eeSChuck Lever 	long idle_time = 0;
2397176e21eeSChuck Lever 
2398176e21eeSChuck Lever 	if (xprt_connected(xprt))
2399176e21eeSChuck Lever 		idle_time = (long)(jiffies - xprt->last_used) / HZ;
2400176e21eeSChuck Lever 
2401176e21eeSChuck Lever 	seq_printf(seq, "\txprt:\tlocal %lu %lu %lu %ld %lu %lu %lu "
240215a45206SAndy Adamson 			"%llu %llu %lu %llu %llu\n",
2403176e21eeSChuck Lever 			xprt->stat.bind_count,
2404176e21eeSChuck Lever 			xprt->stat.connect_count,
24058440a886SChuck Lever 			xprt->stat.connect_time / HZ,
2406176e21eeSChuck Lever 			idle_time,
2407176e21eeSChuck Lever 			xprt->stat.sends,
2408176e21eeSChuck Lever 			xprt->stat.recvs,
2409176e21eeSChuck Lever 			xprt->stat.bad_xids,
2410176e21eeSChuck Lever 			xprt->stat.req_u,
241115a45206SAndy Adamson 			xprt->stat.bklog_u,
241215a45206SAndy Adamson 			xprt->stat.max_slots,
241315a45206SAndy Adamson 			xprt->stat.sending_u,
241415a45206SAndy Adamson 			xprt->stat.pending_u);
2415176e21eeSChuck Lever }
2416176e21eeSChuck Lever 
2417176e21eeSChuck Lever /**
241812b20ce3SBhaskar Chowdhury  * xs_udp_print_stats - display UDP socket-specific stats
2419262ca07dSChuck Lever  * @xprt: rpc_xprt struct containing statistics
2420262ca07dSChuck Lever  * @seq: output file
2421262ca07dSChuck Lever  *
2422262ca07dSChuck Lever  */
2423262ca07dSChuck Lever static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2424262ca07dSChuck Lever {
2425c8475461SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2426c8475461SChuck Lever 
242715a45206SAndy Adamson 	seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %llu %llu "
242815a45206SAndy Adamson 			"%lu %llu %llu\n",
2429fbfffbd5SChuck Lever 			transport->srcport,
2430262ca07dSChuck Lever 			xprt->stat.bind_count,
2431262ca07dSChuck Lever 			xprt->stat.sends,
2432262ca07dSChuck Lever 			xprt->stat.recvs,
2433262ca07dSChuck Lever 			xprt->stat.bad_xids,
2434262ca07dSChuck Lever 			xprt->stat.req_u,
243515a45206SAndy Adamson 			xprt->stat.bklog_u,
243615a45206SAndy Adamson 			xprt->stat.max_slots,
243715a45206SAndy Adamson 			xprt->stat.sending_u,
243815a45206SAndy Adamson 			xprt->stat.pending_u);
2439262ca07dSChuck Lever }
2440262ca07dSChuck Lever 
2441262ca07dSChuck Lever /**
244212b20ce3SBhaskar Chowdhury  * xs_tcp_print_stats - display TCP socket-specific stats
2443262ca07dSChuck Lever  * @xprt: rpc_xprt struct containing statistics
2444262ca07dSChuck Lever  * @seq: output file
2445262ca07dSChuck Lever  *
2446262ca07dSChuck Lever  */
2447262ca07dSChuck Lever static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2448262ca07dSChuck Lever {
2449c8475461SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2450262ca07dSChuck Lever 	long idle_time = 0;
2451262ca07dSChuck Lever 
2452262ca07dSChuck Lever 	if (xprt_connected(xprt))
2453262ca07dSChuck Lever 		idle_time = (long)(jiffies - xprt->last_used) / HZ;
2454262ca07dSChuck Lever 
245515a45206SAndy Adamson 	seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu "
245615a45206SAndy Adamson 			"%llu %llu %lu %llu %llu\n",
2457fbfffbd5SChuck Lever 			transport->srcport,
2458262ca07dSChuck Lever 			xprt->stat.bind_count,
2459262ca07dSChuck Lever 			xprt->stat.connect_count,
24608440a886SChuck Lever 			xprt->stat.connect_time / HZ,
2461262ca07dSChuck Lever 			idle_time,
2462262ca07dSChuck Lever 			xprt->stat.sends,
2463262ca07dSChuck Lever 			xprt->stat.recvs,
2464262ca07dSChuck Lever 			xprt->stat.bad_xids,
2465262ca07dSChuck Lever 			xprt->stat.req_u,
246615a45206SAndy Adamson 			xprt->stat.bklog_u,
246715a45206SAndy Adamson 			xprt->stat.max_slots,
246815a45206SAndy Adamson 			xprt->stat.sending_u,
246915a45206SAndy Adamson 			xprt->stat.pending_u);
2470262ca07dSChuck Lever }
2471262ca07dSChuck Lever 
24724cfc7e60SRahul Iyer /*
24734cfc7e60SRahul Iyer  * Allocate a bunch of pages for a scratch buffer for the rpc code. The reason
24744cfc7e60SRahul Iyer  * we allocate pages instead doing a kmalloc like rpc_malloc is because we want
24754cfc7e60SRahul Iyer  * to use the server side send routines.
24764cfc7e60SRahul Iyer  */
24775fe6eaa1SChuck Lever static int bc_malloc(struct rpc_task *task)
24784cfc7e60SRahul Iyer {
24795fe6eaa1SChuck Lever 	struct rpc_rqst *rqst = task->tk_rqstp;
24805fe6eaa1SChuck Lever 	size_t size = rqst->rq_callsize;
24814cfc7e60SRahul Iyer 	struct page *page;
24824cfc7e60SRahul Iyer 	struct rpc_buffer *buf;
24834cfc7e60SRahul Iyer 
24845fe6eaa1SChuck Lever 	if (size > PAGE_SIZE - sizeof(struct rpc_buffer)) {
24855fe6eaa1SChuck Lever 		WARN_ONCE(1, "xprtsock: large bc buffer request (size %zu)\n",
24865fe6eaa1SChuck Lever 			  size);
24875fe6eaa1SChuck Lever 		return -EINVAL;
24885fe6eaa1SChuck Lever 	}
24894cfc7e60SRahul Iyer 
2490b8a13d03SWeston Andros Adamson 	page = alloc_page(GFP_KERNEL);
24914cfc7e60SRahul Iyer 	if (!page)
24925fe6eaa1SChuck Lever 		return -ENOMEM;
24934cfc7e60SRahul Iyer 
24944cfc7e60SRahul Iyer 	buf = page_address(page);
24954cfc7e60SRahul Iyer 	buf->len = PAGE_SIZE;
24964cfc7e60SRahul Iyer 
24975fe6eaa1SChuck Lever 	rqst->rq_buffer = buf->data;
249818e601d6SJeff Layton 	rqst->rq_rbuffer = (char *)rqst->rq_buffer + rqst->rq_callsize;
24995fe6eaa1SChuck Lever 	return 0;
25004cfc7e60SRahul Iyer }
25014cfc7e60SRahul Iyer 
25024cfc7e60SRahul Iyer /*
25034cfc7e60SRahul Iyer  * Free the space allocated in the bc_alloc routine
25044cfc7e60SRahul Iyer  */
25053435c74aSChuck Lever static void bc_free(struct rpc_task *task)
25064cfc7e60SRahul Iyer {
25073435c74aSChuck Lever 	void *buffer = task->tk_rqstp->rq_buffer;
25084cfc7e60SRahul Iyer 	struct rpc_buffer *buf;
25094cfc7e60SRahul Iyer 
25104cfc7e60SRahul Iyer 	buf = container_of(buffer, struct rpc_buffer, data);
25114cfc7e60SRahul Iyer 	free_page((unsigned long)buf);
25124cfc7e60SRahul Iyer }
25134cfc7e60SRahul Iyer 
25144cfc7e60SRahul Iyer static int bc_sendto(struct rpc_rqst *req)
25154cfc7e60SRahul Iyer {
2516da1661b9SChuck Lever 	struct xdr_buf *xdr = &req->rq_snd_buf;
25174cfc7e60SRahul Iyer 	struct sock_xprt *transport =
2518067fb11bSChuck Lever 			container_of(req->rq_xprt, struct sock_xprt, xprt);
2519067fb11bSChuck Lever 	struct msghdr msg = {
2520da1661b9SChuck Lever 		.msg_flags	= 0,
2521067fb11bSChuck Lever 	};
2522067fb11bSChuck Lever 	rpc_fraghdr marker = cpu_to_be32(RPC_LAST_STREAM_FRAGMENT |
2523da1661b9SChuck Lever 					 (u32)xdr->len);
2524da1661b9SChuck Lever 	unsigned int sent = 0;
2525da1661b9SChuck Lever 	int err;
25264cfc7e60SRahul Iyer 
25278729aabaSChuck Lever 	req->rq_xtime = ktime_get();
2528da1661b9SChuck Lever 	err = xprt_sock_sendmsg(transport->sock, &msg, xdr, 0, marker, &sent);
2529da1661b9SChuck Lever 	xdr_free_bvec(xdr);
2530da1661b9SChuck Lever 	if (err < 0 || sent != (xdr->len + sizeof(marker)))
2531067fb11bSChuck Lever 		return -EAGAIN;
2532da1661b9SChuck Lever 	return sent;
25334cfc7e60SRahul Iyer }
25344cfc7e60SRahul Iyer 
2535ca4faf54SChuck Lever /**
2536ca4faf54SChuck Lever  * bc_send_request - Send a backchannel Call on a TCP socket
2537ca4faf54SChuck Lever  * @req: rpc_rqst containing Call message to be sent
2538ca4faf54SChuck Lever  *
2539ca4faf54SChuck Lever  * xpt_mutex ensures @rqstp's whole message is written to the socket
2540ca4faf54SChuck Lever  * without interruption.
2541ca4faf54SChuck Lever  *
2542ca4faf54SChuck Lever  * Return values:
2543ca4faf54SChuck Lever  *   %0 if the message was sent successfully
2544ca4faf54SChuck Lever  *   %ENOTCONN if the message was not sent
25454cfc7e60SRahul Iyer  */
2546adfa7144STrond Myklebust static int bc_send_request(struct rpc_rqst *req)
25474cfc7e60SRahul Iyer {
25484cfc7e60SRahul Iyer 	struct svc_xprt	*xprt;
25497fc56136SAndrzej Hajda 	int len;
25504cfc7e60SRahul Iyer 
25514cfc7e60SRahul Iyer 	/*
25524cfc7e60SRahul Iyer 	 * Get the server socket associated with this callback xprt
25534cfc7e60SRahul Iyer 	 */
25544cfc7e60SRahul Iyer 	xprt = req->rq_xprt->bc_xprt;
25554cfc7e60SRahul Iyer 
25564cfc7e60SRahul Iyer 	/*
25574cfc7e60SRahul Iyer 	 * Grab the mutex to serialize data as the connection is shared
25584cfc7e60SRahul Iyer 	 * with the fore channel
25594cfc7e60SRahul Iyer 	 */
2560c544577dSTrond Myklebust 	mutex_lock(&xprt->xpt_mutex);
25614cfc7e60SRahul Iyer 	if (test_bit(XPT_DEAD, &xprt->xpt_flags))
25624cfc7e60SRahul Iyer 		len = -ENOTCONN;
25634cfc7e60SRahul Iyer 	else
25644cfc7e60SRahul Iyer 		len = bc_sendto(req);
25654cfc7e60SRahul Iyer 	mutex_unlock(&xprt->xpt_mutex);
25664cfc7e60SRahul Iyer 
25674cfc7e60SRahul Iyer 	if (len > 0)
25684cfc7e60SRahul Iyer 		len = 0;
25694cfc7e60SRahul Iyer 
25704cfc7e60SRahul Iyer 	return len;
25714cfc7e60SRahul Iyer }
25724cfc7e60SRahul Iyer 
25734cfc7e60SRahul Iyer /*
25744cfc7e60SRahul Iyer  * The close routine. Since this is client initiated, we do nothing
25754cfc7e60SRahul Iyer  */
25764cfc7e60SRahul Iyer 
25774cfc7e60SRahul Iyer static void bc_close(struct rpc_xprt *xprt)
25784cfc7e60SRahul Iyer {
25796221f1d9SChuck Lever 	xprt_disconnect_done(xprt);
25804cfc7e60SRahul Iyer }
25814cfc7e60SRahul Iyer 
25824cfc7e60SRahul Iyer /*
25834cfc7e60SRahul Iyer  * The xprt destroy routine. Again, because this connection is client
25844cfc7e60SRahul Iyer  * initiated, we do nothing
25854cfc7e60SRahul Iyer  */
25864cfc7e60SRahul Iyer 
25874cfc7e60SRahul Iyer static void bc_destroy(struct rpc_xprt *xprt)
25884cfc7e60SRahul Iyer {
258947f72efaSKinglong Mee 	dprintk("RPC:       bc_destroy xprt %p\n", xprt);
259047f72efaSKinglong Mee 
259147f72efaSKinglong Mee 	xs_xprt_free(xprt);
259247f72efaSKinglong Mee 	module_put(THIS_MODULE);
25934cfc7e60SRahul Iyer }
25944cfc7e60SRahul Iyer 
2595d31ae254SChuck Lever static const struct rpc_xprt_ops xs_local_ops = {
2596176e21eeSChuck Lever 	.reserve_xprt		= xprt_reserve_xprt,
25974cd34e7cSTrond Myklebust 	.release_xprt		= xprt_release_xprt,
2598f39c1bfbSTrond Myklebust 	.alloc_slot		= xprt_alloc_slot,
2599a9cde23aSChuck Lever 	.free_slot		= xprt_free_slot,
2600176e21eeSChuck Lever 	.rpcbind		= xs_local_rpcbind,
2601176e21eeSChuck Lever 	.set_port		= xs_local_set_port,
2602dc107402SJ. Bruce Fields 	.connect		= xs_local_connect,
2603176e21eeSChuck Lever 	.buf_alloc		= rpc_malloc,
2604176e21eeSChuck Lever 	.buf_free		= rpc_free,
2605550aebfeSTrond Myklebust 	.prepare_request	= xs_stream_prepare_request,
2606176e21eeSChuck Lever 	.send_request		= xs_local_send_request,
26078ba6a92dSTrond Myklebust 	.wait_for_reply_request	= xprt_wait_for_reply_request_def,
2608176e21eeSChuck Lever 	.close			= xs_close,
2609a1311d87STrond Myklebust 	.destroy		= xs_destroy,
2610176e21eeSChuck Lever 	.print_stats		= xs_local_print_stats,
2611d67fa4d8SJeff Layton 	.enable_swap		= xs_enable_swap,
2612d67fa4d8SJeff Layton 	.disable_swap		= xs_disable_swap,
2613176e21eeSChuck Lever };
2614176e21eeSChuck Lever 
2615d31ae254SChuck Lever static const struct rpc_xprt_ops xs_udp_ops = {
261643118c29SChuck Lever 	.set_buffer_size	= xs_udp_set_buffer_size,
261712a80469SChuck Lever 	.reserve_xprt		= xprt_reserve_xprt_cong,
261849e9a890SChuck Lever 	.release_xprt		= xprt_release_xprt_cong,
2619f39c1bfbSTrond Myklebust 	.alloc_slot		= xprt_alloc_slot,
2620a9cde23aSChuck Lever 	.free_slot		= xprt_free_slot,
262145160d62SChuck Lever 	.rpcbind		= rpcb_getport_async,
262292200412SChuck Lever 	.set_port		= xs_set_port,
26239903cd1cSChuck Lever 	.connect		= xs_connect,
262402107148SChuck Lever 	.buf_alloc		= rpc_malloc,
262502107148SChuck Lever 	.buf_free		= rpc_free,
2626262965f5SChuck Lever 	.send_request		= xs_udp_send_request,
26278ba6a92dSTrond Myklebust 	.wait_for_reply_request	= xprt_wait_for_reply_request_rtt,
262846c0ee8bSChuck Lever 	.timer			= xs_udp_timer,
2629a58dd398SChuck Lever 	.release_request	= xprt_release_rqst_cong,
2630262965f5SChuck Lever 	.close			= xs_close,
2631262965f5SChuck Lever 	.destroy		= xs_destroy,
2632262ca07dSChuck Lever 	.print_stats		= xs_udp_print_stats,
2633d67fa4d8SJeff Layton 	.enable_swap		= xs_enable_swap,
2634d67fa4d8SJeff Layton 	.disable_swap		= xs_disable_swap,
26354a068258SChuck Lever 	.inject_disconnect	= xs_inject_disconnect,
2636262965f5SChuck Lever };
2637262965f5SChuck Lever 
2638d31ae254SChuck Lever static const struct rpc_xprt_ops xs_tcp_ops = {
263912a80469SChuck Lever 	.reserve_xprt		= xprt_reserve_xprt,
26404cd34e7cSTrond Myklebust 	.release_xprt		= xprt_release_xprt,
264136bd7de9STrond Myklebust 	.alloc_slot		= xprt_alloc_slot,
2642a9cde23aSChuck Lever 	.free_slot		= xprt_free_slot,
264345160d62SChuck Lever 	.rpcbind		= rpcb_getport_async,
264492200412SChuck Lever 	.set_port		= xs_set_port,
26450b9e7943STrond Myklebust 	.connect		= xs_connect,
264602107148SChuck Lever 	.buf_alloc		= rpc_malloc,
264702107148SChuck Lever 	.buf_free		= rpc_free,
2648277e4ab7STrond Myklebust 	.prepare_request	= xs_stream_prepare_request,
2649262965f5SChuck Lever 	.send_request		= xs_tcp_send_request,
26508ba6a92dSTrond Myklebust 	.wait_for_reply_request	= xprt_wait_for_reply_request_def,
2651c627d31bSTrond Myklebust 	.close			= xs_tcp_shutdown,
26529903cd1cSChuck Lever 	.destroy		= xs_destroy,
26537196dbb0STrond Myklebust 	.set_connect_timeout	= xs_tcp_set_connect_timeout,
2654262ca07dSChuck Lever 	.print_stats		= xs_tcp_print_stats,
2655d67fa4d8SJeff Layton 	.enable_swap		= xs_enable_swap,
2656d67fa4d8SJeff Layton 	.disable_swap		= xs_disable_swap,
26574a068258SChuck Lever 	.inject_disconnect	= xs_inject_disconnect,
265842e5c3e2SChuck Lever #ifdef CONFIG_SUNRPC_BACKCHANNEL
265942e5c3e2SChuck Lever 	.bc_setup		= xprt_setup_bc,
26606b26cc8cSChuck Lever 	.bc_maxpayload		= xs_tcp_bc_maxpayload,
26617402a4feSTrond Myklebust 	.bc_num_slots		= xprt_bc_max_slots,
266242e5c3e2SChuck Lever 	.bc_free_rqst		= xprt_free_bc_rqst,
266342e5c3e2SChuck Lever 	.bc_destroy		= xprt_destroy_bc,
266442e5c3e2SChuck Lever #endif
2665a246b010SChuck Lever };
2666a246b010SChuck Lever 
26674cfc7e60SRahul Iyer /*
26684cfc7e60SRahul Iyer  * The rpc_xprt_ops for the server backchannel
26694cfc7e60SRahul Iyer  */
26704cfc7e60SRahul Iyer 
2671d31ae254SChuck Lever static const struct rpc_xprt_ops bc_tcp_ops = {
26724cfc7e60SRahul Iyer 	.reserve_xprt		= xprt_reserve_xprt,
26734cfc7e60SRahul Iyer 	.release_xprt		= xprt_release_xprt,
267484e28a30SBryan Schumaker 	.alloc_slot		= xprt_alloc_slot,
2675a9cde23aSChuck Lever 	.free_slot		= xprt_free_slot,
26764cfc7e60SRahul Iyer 	.buf_alloc		= bc_malloc,
26774cfc7e60SRahul Iyer 	.buf_free		= bc_free,
26784cfc7e60SRahul Iyer 	.send_request		= bc_send_request,
26798ba6a92dSTrond Myklebust 	.wait_for_reply_request	= xprt_wait_for_reply_request_def,
26804cfc7e60SRahul Iyer 	.close			= bc_close,
26814cfc7e60SRahul Iyer 	.destroy		= bc_destroy,
26824cfc7e60SRahul Iyer 	.print_stats		= xs_tcp_print_stats,
2683d67fa4d8SJeff Layton 	.enable_swap		= xs_enable_swap,
2684d67fa4d8SJeff Layton 	.disable_swap		= xs_disable_swap,
26854a068258SChuck Lever 	.inject_disconnect	= xs_inject_disconnect,
26864cfc7e60SRahul Iyer };
26874cfc7e60SRahul Iyer 
268892476850SChuck Lever static int xs_init_anyaddr(const int family, struct sockaddr *sap)
268992476850SChuck Lever {
269092476850SChuck Lever 	static const struct sockaddr_in sin = {
269192476850SChuck Lever 		.sin_family		= AF_INET,
269292476850SChuck Lever 		.sin_addr.s_addr	= htonl(INADDR_ANY),
269392476850SChuck Lever 	};
269492476850SChuck Lever 	static const struct sockaddr_in6 sin6 = {
269592476850SChuck Lever 		.sin6_family		= AF_INET6,
269692476850SChuck Lever 		.sin6_addr		= IN6ADDR_ANY_INIT,
269792476850SChuck Lever 	};
269892476850SChuck Lever 
269992476850SChuck Lever 	switch (family) {
2700176e21eeSChuck Lever 	case AF_LOCAL:
2701176e21eeSChuck Lever 		break;
270292476850SChuck Lever 	case AF_INET:
270392476850SChuck Lever 		memcpy(sap, &sin, sizeof(sin));
270492476850SChuck Lever 		break;
270592476850SChuck Lever 	case AF_INET6:
270692476850SChuck Lever 		memcpy(sap, &sin6, sizeof(sin6));
270792476850SChuck Lever 		break;
270892476850SChuck Lever 	default:
270992476850SChuck Lever 		dprintk("RPC:       %s: Bad address family\n", __func__);
271092476850SChuck Lever 		return -EAFNOSUPPORT;
271192476850SChuck Lever 	}
271292476850SChuck Lever 	return 0;
271392476850SChuck Lever }
271492476850SChuck Lever 
27153c341b0bS\"Talpey, Thomas\ static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args,
2716d9ba131dSTrond Myklebust 				      unsigned int slot_table_size,
2717d9ba131dSTrond Myklebust 				      unsigned int max_slot_table_size)
2718c8541ecdSChuck Lever {
2719c8541ecdSChuck Lever 	struct rpc_xprt *xprt;
2720ffc2e518SChuck Lever 	struct sock_xprt *new;
2721c8541ecdSChuck Lever 
272296802a09SFrank van Maarseveen 	if (args->addrlen > sizeof(xprt->addr)) {
2723c8541ecdSChuck Lever 		dprintk("RPC:       xs_setup_xprt: address too large\n");
2724c8541ecdSChuck Lever 		return ERR_PTR(-EBADF);
2725c8541ecdSChuck Lever 	}
2726c8541ecdSChuck Lever 
2727d9ba131dSTrond Myklebust 	xprt = xprt_alloc(args->net, sizeof(*new), slot_table_size,
2728d9ba131dSTrond Myklebust 			max_slot_table_size);
2729bd1722d4SPavel Emelyanov 	if (xprt == NULL) {
273046121cf7SChuck Lever 		dprintk("RPC:       xs_setup_xprt: couldn't allocate "
273146121cf7SChuck Lever 				"rpc_xprt\n");
2732c8541ecdSChuck Lever 		return ERR_PTR(-ENOMEM);
2733c8541ecdSChuck Lever 	}
2734c8541ecdSChuck Lever 
2735bd1722d4SPavel Emelyanov 	new = container_of(xprt, struct sock_xprt, xprt);
2736edc1b01cSTrond Myklebust 	mutex_init(&new->recv_mutex);
273796802a09SFrank van Maarseveen 	memcpy(&xprt->addr, args->dstaddr, args->addrlen);
273896802a09SFrank van Maarseveen 	xprt->addrlen = args->addrlen;
2739d3bc9a1dSFrank van Maarseveen 	if (args->srcaddr)
2740fbfffbd5SChuck Lever 		memcpy(&new->srcaddr, args->srcaddr, args->addrlen);
274192476850SChuck Lever 	else {
274292476850SChuck Lever 		int err;
274392476850SChuck Lever 		err = xs_init_anyaddr(args->dstaddr->sa_family,
274492476850SChuck Lever 					(struct sockaddr *)&new->srcaddr);
27452aa13531SStanislav Kinsbursky 		if (err != 0) {
27462aa13531SStanislav Kinsbursky 			xprt_free(xprt);
274792476850SChuck Lever 			return ERR_PTR(err);
274892476850SChuck Lever 		}
27492aa13531SStanislav Kinsbursky 	}
2750c8541ecdSChuck Lever 
2751c8541ecdSChuck Lever 	return xprt;
2752c8541ecdSChuck Lever }
2753c8541ecdSChuck Lever 
2754176e21eeSChuck Lever static const struct rpc_timeout xs_local_default_timeout = {
2755176e21eeSChuck Lever 	.to_initval = 10 * HZ,
2756176e21eeSChuck Lever 	.to_maxval = 10 * HZ,
2757176e21eeSChuck Lever 	.to_retries = 2,
2758176e21eeSChuck Lever };
2759176e21eeSChuck Lever 
2760176e21eeSChuck Lever /**
2761176e21eeSChuck Lever  * xs_setup_local - Set up transport to use an AF_LOCAL socket
2762176e21eeSChuck Lever  * @args: rpc transport creation arguments
2763176e21eeSChuck Lever  *
2764176e21eeSChuck Lever  * AF_LOCAL is a "tpi_cots_ord" transport, just like TCP
2765176e21eeSChuck Lever  */
2766176e21eeSChuck Lever static struct rpc_xprt *xs_setup_local(struct xprt_create *args)
2767176e21eeSChuck Lever {
2768176e21eeSChuck Lever 	struct sockaddr_un *sun = (struct sockaddr_un *)args->dstaddr;
2769176e21eeSChuck Lever 	struct sock_xprt *transport;
2770176e21eeSChuck Lever 	struct rpc_xprt *xprt;
2771176e21eeSChuck Lever 	struct rpc_xprt *ret;
2772176e21eeSChuck Lever 
2773d9ba131dSTrond Myklebust 	xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
2774d9ba131dSTrond Myklebust 			xprt_max_tcp_slot_table_entries);
2775176e21eeSChuck Lever 	if (IS_ERR(xprt))
2776176e21eeSChuck Lever 		return xprt;
2777176e21eeSChuck Lever 	transport = container_of(xprt, struct sock_xprt, xprt);
2778176e21eeSChuck Lever 
2779176e21eeSChuck Lever 	xprt->prot = 0;
2780d3abc739SOlga Kornievskaia 	xprt->xprt_class = &xs_local_transport;
2781176e21eeSChuck Lever 	xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2782176e21eeSChuck Lever 
2783176e21eeSChuck Lever 	xprt->bind_timeout = XS_BIND_TO;
2784176e21eeSChuck Lever 	xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2785176e21eeSChuck Lever 	xprt->idle_timeout = XS_IDLE_DISC_TO;
2786176e21eeSChuck Lever 
2787176e21eeSChuck Lever 	xprt->ops = &xs_local_ops;
2788176e21eeSChuck Lever 	xprt->timeout = &xs_local_default_timeout;
2789176e21eeSChuck Lever 
2790550aebfeSTrond Myklebust 	INIT_WORK(&transport->recv_worker, xs_stream_data_receive_workfn);
27914f8943f8STrond Myklebust 	INIT_WORK(&transport->error_worker, xs_error_handle);
2792550aebfeSTrond Myklebust 	INIT_DELAYED_WORK(&transport->connect_worker, xs_dummy_setup_socket);
279393dc41bdSNeilBrown 
2794176e21eeSChuck Lever 	switch (sun->sun_family) {
2795176e21eeSChuck Lever 	case AF_LOCAL:
2796176e21eeSChuck Lever 		if (sun->sun_path[0] != '/') {
2797176e21eeSChuck Lever 			dprintk("RPC:       bad AF_LOCAL address: %s\n",
2798176e21eeSChuck Lever 					sun->sun_path);
2799176e21eeSChuck Lever 			ret = ERR_PTR(-EINVAL);
2800176e21eeSChuck Lever 			goto out_err;
2801176e21eeSChuck Lever 		}
2802176e21eeSChuck Lever 		xprt_set_bound(xprt);
2803176e21eeSChuck Lever 		xs_format_peer_addresses(xprt, "local", RPCBIND_NETID_LOCAL);
28047073ea87SJ. Bruce Fields 		ret = ERR_PTR(xs_local_setup_socket(transport));
28057073ea87SJ. Bruce Fields 		if (ret)
28067073ea87SJ. Bruce Fields 			goto out_err;
2807176e21eeSChuck Lever 		break;
2808176e21eeSChuck Lever 	default:
2809176e21eeSChuck Lever 		ret = ERR_PTR(-EAFNOSUPPORT);
2810176e21eeSChuck Lever 		goto out_err;
2811176e21eeSChuck Lever 	}
2812176e21eeSChuck Lever 
2813176e21eeSChuck Lever 	dprintk("RPC:       set up xprt to %s via AF_LOCAL\n",
2814176e21eeSChuck Lever 			xprt->address_strings[RPC_DISPLAY_ADDR]);
2815176e21eeSChuck Lever 
2816176e21eeSChuck Lever 	if (try_module_get(THIS_MODULE))
2817176e21eeSChuck Lever 		return xprt;
2818176e21eeSChuck Lever 	ret = ERR_PTR(-EINVAL);
2819176e21eeSChuck Lever out_err:
2820315f3812SKinglong Mee 	xs_xprt_free(xprt);
2821176e21eeSChuck Lever 	return ret;
2822176e21eeSChuck Lever }
2823176e21eeSChuck Lever 
28242881ae74STrond Myklebust static const struct rpc_timeout xs_udp_default_timeout = {
28252881ae74STrond Myklebust 	.to_initval = 5 * HZ,
28262881ae74STrond Myklebust 	.to_maxval = 30 * HZ,
28272881ae74STrond Myklebust 	.to_increment = 5 * HZ,
28282881ae74STrond Myklebust 	.to_retries = 5,
28292881ae74STrond Myklebust };
28302881ae74STrond Myklebust 
28319903cd1cSChuck Lever /**
28329903cd1cSChuck Lever  * xs_setup_udp - Set up transport to use a UDP socket
283396802a09SFrank van Maarseveen  * @args: rpc transport creation arguments
28349903cd1cSChuck Lever  *
28359903cd1cSChuck Lever  */
2836483066d6SAdrian Bunk static struct rpc_xprt *xs_setup_udp(struct xprt_create *args)
2837a246b010SChuck Lever {
28388f9d5b1aSChuck Lever 	struct sockaddr *addr = args->dstaddr;
2839c8541ecdSChuck Lever 	struct rpc_xprt *xprt;
2840c8475461SChuck Lever 	struct sock_xprt *transport;
28410a68b0beSJ. Bruce Fields 	struct rpc_xprt *ret;
2842a246b010SChuck Lever 
2843d9ba131dSTrond Myklebust 	xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries,
2844d9ba131dSTrond Myklebust 			xprt_udp_slot_table_entries);
2845c8541ecdSChuck Lever 	if (IS_ERR(xprt))
2846c8541ecdSChuck Lever 		return xprt;
2847c8475461SChuck Lever 	transport = container_of(xprt, struct sock_xprt, xprt);
2848a246b010SChuck Lever 
2849ec739ef0SChuck Lever 	xprt->prot = IPPROTO_UDP;
2850d3abc739SOlga Kornievskaia 	xprt->xprt_class = &xs_udp_transport;
2851a246b010SChuck Lever 	/* XXX: header size can vary due to auth type, IPv6, etc. */
2852a246b010SChuck Lever 	xprt->max_payload = (1U << 16) - (MAX_HEADER << 3);
2853a246b010SChuck Lever 
285403bf4b70SChuck Lever 	xprt->bind_timeout = XS_BIND_TO;
285503bf4b70SChuck Lever 	xprt->reestablish_timeout = XS_UDP_REEST_TO;
285603bf4b70SChuck Lever 	xprt->idle_timeout = XS_IDLE_DISC_TO;
2857a246b010SChuck Lever 
2858262965f5SChuck Lever 	xprt->ops = &xs_udp_ops;
2859a246b010SChuck Lever 
2860ba7392bbSTrond Myklebust 	xprt->timeout = &xs_udp_default_timeout;
2861a246b010SChuck Lever 
2862f9b2ee71STrond Myklebust 	INIT_WORK(&transport->recv_worker, xs_udp_data_receive_workfn);
28634f8943f8STrond Myklebust 	INIT_WORK(&transport->error_worker, xs_error_handle);
2864edc1b01cSTrond Myklebust 	INIT_DELAYED_WORK(&transport->connect_worker, xs_udp_setup_socket);
2865edc1b01cSTrond Myklebust 
28668f9d5b1aSChuck Lever 	switch (addr->sa_family) {
28678f9d5b1aSChuck Lever 	case AF_INET:
28688f9d5b1aSChuck Lever 		if (((struct sockaddr_in *)addr)->sin_port != htons(0))
28698f9d5b1aSChuck Lever 			xprt_set_bound(xprt);
28708f9d5b1aSChuck Lever 
28719dc3b095SChuck Lever 		xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP);
28728f9d5b1aSChuck Lever 		break;
28738f9d5b1aSChuck Lever 	case AF_INET6:
28748f9d5b1aSChuck Lever 		if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
28758f9d5b1aSChuck Lever 			xprt_set_bound(xprt);
28768f9d5b1aSChuck Lever 
28779dc3b095SChuck Lever 		xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6);
28788f9d5b1aSChuck Lever 		break;
28798f9d5b1aSChuck Lever 	default:
28800a68b0beSJ. Bruce Fields 		ret = ERR_PTR(-EAFNOSUPPORT);
28810a68b0beSJ. Bruce Fields 		goto out_err;
28828f9d5b1aSChuck Lever 	}
28838f9d5b1aSChuck Lever 
2884c740eff8SChuck Lever 	if (xprt_bound(xprt))
2885c740eff8SChuck Lever 		dprintk("RPC:       set up xprt to %s (port %s) via %s\n",
2886c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_ADDR],
2887c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_PORT],
2888c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_PROTO]);
2889c740eff8SChuck Lever 	else
2890c740eff8SChuck Lever 		dprintk("RPC:       set up xprt to %s (autobind) via %s\n",
2891c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_ADDR],
2892c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_PROTO]);
2893edb267a6SChuck Lever 
2894bc25571eS\"Talpey, Thomas\ 	if (try_module_get(THIS_MODULE))
2895c8541ecdSChuck Lever 		return xprt;
28960a68b0beSJ. Bruce Fields 	ret = ERR_PTR(-EINVAL);
28970a68b0beSJ. Bruce Fields out_err:
2898315f3812SKinglong Mee 	xs_xprt_free(xprt);
28990a68b0beSJ. Bruce Fields 	return ret;
2900a246b010SChuck Lever }
2901a246b010SChuck Lever 
29022881ae74STrond Myklebust static const struct rpc_timeout xs_tcp_default_timeout = {
29032881ae74STrond Myklebust 	.to_initval = 60 * HZ,
29042881ae74STrond Myklebust 	.to_maxval = 60 * HZ,
29052881ae74STrond Myklebust 	.to_retries = 2,
29062881ae74STrond Myklebust };
29072881ae74STrond Myklebust 
29089903cd1cSChuck Lever /**
29099903cd1cSChuck Lever  * xs_setup_tcp - Set up transport to use a TCP socket
291096802a09SFrank van Maarseveen  * @args: rpc transport creation arguments
29119903cd1cSChuck Lever  *
29129903cd1cSChuck Lever  */
2913483066d6SAdrian Bunk static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args)
2914a246b010SChuck Lever {
29158f9d5b1aSChuck Lever 	struct sockaddr *addr = args->dstaddr;
2916c8541ecdSChuck Lever 	struct rpc_xprt *xprt;
2917c8475461SChuck Lever 	struct sock_xprt *transport;
29180a68b0beSJ. Bruce Fields 	struct rpc_xprt *ret;
2919b7993cebSTrond Myklebust 	unsigned int max_slot_table_size = xprt_max_tcp_slot_table_entries;
2920b7993cebSTrond Myklebust 
2921b7993cebSTrond Myklebust 	if (args->flags & XPRT_CREATE_INFINITE_SLOTS)
2922b7993cebSTrond Myklebust 		max_slot_table_size = RPC_MAX_SLOT_TABLE_LIMIT;
2923a246b010SChuck Lever 
2924d9ba131dSTrond Myklebust 	xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
2925b7993cebSTrond Myklebust 			max_slot_table_size);
2926c8541ecdSChuck Lever 	if (IS_ERR(xprt))
2927c8541ecdSChuck Lever 		return xprt;
2928c8475461SChuck Lever 	transport = container_of(xprt, struct sock_xprt, xprt);
2929a246b010SChuck Lever 
2930ec739ef0SChuck Lever 	xprt->prot = IPPROTO_TCP;
2931d3abc739SOlga Kornievskaia 	xprt->xprt_class = &xs_tcp_transport;
2932808012fbSChuck Lever 	xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2933a246b010SChuck Lever 
293403bf4b70SChuck Lever 	xprt->bind_timeout = XS_BIND_TO;
293503bf4b70SChuck Lever 	xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
293603bf4b70SChuck Lever 	xprt->idle_timeout = XS_IDLE_DISC_TO;
2937a246b010SChuck Lever 
2938262965f5SChuck Lever 	xprt->ops = &xs_tcp_ops;
2939ba7392bbSTrond Myklebust 	xprt->timeout = &xs_tcp_default_timeout;
2940a246b010SChuck Lever 
29413851f1cdSTrond Myklebust 	xprt->max_reconnect_timeout = xprt->timeout->to_maxval;
29427196dbb0STrond Myklebust 	xprt->connect_timeout = xprt->timeout->to_initval *
29437196dbb0STrond Myklebust 		(xprt->timeout->to_retries + 1);
29443851f1cdSTrond Myklebust 
2945c50b8ee0STrond Myklebust 	INIT_WORK(&transport->recv_worker, xs_stream_data_receive_workfn);
29464f8943f8STrond Myklebust 	INIT_WORK(&transport->error_worker, xs_error_handle);
2947edc1b01cSTrond Myklebust 	INIT_DELAYED_WORK(&transport->connect_worker, xs_tcp_setup_socket);
2948edc1b01cSTrond Myklebust 
29498f9d5b1aSChuck Lever 	switch (addr->sa_family) {
29508f9d5b1aSChuck Lever 	case AF_INET:
29518f9d5b1aSChuck Lever 		if (((struct sockaddr_in *)addr)->sin_port != htons(0))
29528f9d5b1aSChuck Lever 			xprt_set_bound(xprt);
29538f9d5b1aSChuck Lever 
29549dc3b095SChuck Lever 		xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP);
29558f9d5b1aSChuck Lever 		break;
29568f9d5b1aSChuck Lever 	case AF_INET6:
29578f9d5b1aSChuck Lever 		if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
29588f9d5b1aSChuck Lever 			xprt_set_bound(xprt);
29598f9d5b1aSChuck Lever 
29609dc3b095SChuck Lever 		xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6);
29618f9d5b1aSChuck Lever 		break;
29628f9d5b1aSChuck Lever 	default:
29630a68b0beSJ. Bruce Fields 		ret = ERR_PTR(-EAFNOSUPPORT);
29640a68b0beSJ. Bruce Fields 		goto out_err;
29658f9d5b1aSChuck Lever 	}
29668f9d5b1aSChuck Lever 
2967c740eff8SChuck Lever 	if (xprt_bound(xprt))
2968c740eff8SChuck Lever 		dprintk("RPC:       set up xprt to %s (port %s) via %s\n",
2969c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_ADDR],
2970c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_PORT],
2971c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_PROTO]);
2972c740eff8SChuck Lever 	else
2973c740eff8SChuck Lever 		dprintk("RPC:       set up xprt to %s (autobind) via %s\n",
2974c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_ADDR],
2975c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_PROTO]);
2976c740eff8SChuck Lever 
2977bc25571eS\"Talpey, Thomas\ 	if (try_module_get(THIS_MODULE))
2978c8541ecdSChuck Lever 		return xprt;
29790a68b0beSJ. Bruce Fields 	ret = ERR_PTR(-EINVAL);
29800a68b0beSJ. Bruce Fields out_err:
2981315f3812SKinglong Mee 	xs_xprt_free(xprt);
29820a68b0beSJ. Bruce Fields 	return ret;
2983a246b010SChuck Lever }
2984282b32e1SChuck Lever 
2985f300babaSAlexandros Batsakis /**
2986f300babaSAlexandros Batsakis  * xs_setup_bc_tcp - Set up transport to use a TCP backchannel socket
2987f300babaSAlexandros Batsakis  * @args: rpc transport creation arguments
2988f300babaSAlexandros Batsakis  *
2989f300babaSAlexandros Batsakis  */
2990f300babaSAlexandros Batsakis static struct rpc_xprt *xs_setup_bc_tcp(struct xprt_create *args)
2991f300babaSAlexandros Batsakis {
2992f300babaSAlexandros Batsakis 	struct sockaddr *addr = args->dstaddr;
2993f300babaSAlexandros Batsakis 	struct rpc_xprt *xprt;
2994f300babaSAlexandros Batsakis 	struct sock_xprt *transport;
2995f300babaSAlexandros Batsakis 	struct svc_sock *bc_sock;
29960a68b0beSJ. Bruce Fields 	struct rpc_xprt *ret;
2997f300babaSAlexandros Batsakis 
2998d9ba131dSTrond Myklebust 	xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
2999d9ba131dSTrond Myklebust 			xprt_tcp_slot_table_entries);
3000f300babaSAlexandros Batsakis 	if (IS_ERR(xprt))
3001f300babaSAlexandros Batsakis 		return xprt;
3002f300babaSAlexandros Batsakis 	transport = container_of(xprt, struct sock_xprt, xprt);
3003f300babaSAlexandros Batsakis 
3004f300babaSAlexandros Batsakis 	xprt->prot = IPPROTO_TCP;
3005d3abc739SOlga Kornievskaia 	xprt->xprt_class = &xs_bc_tcp_transport;
3006f300babaSAlexandros Batsakis 	xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
3007f300babaSAlexandros Batsakis 	xprt->timeout = &xs_tcp_default_timeout;
3008f300babaSAlexandros Batsakis 
3009f300babaSAlexandros Batsakis 	/* backchannel */
3010f300babaSAlexandros Batsakis 	xprt_set_bound(xprt);
3011f300babaSAlexandros Batsakis 	xprt->bind_timeout = 0;
3012f300babaSAlexandros Batsakis 	xprt->reestablish_timeout = 0;
3013f300babaSAlexandros Batsakis 	xprt->idle_timeout = 0;
3014f300babaSAlexandros Batsakis 
3015f300babaSAlexandros Batsakis 	xprt->ops = &bc_tcp_ops;
3016f300babaSAlexandros Batsakis 
3017f300babaSAlexandros Batsakis 	switch (addr->sa_family) {
3018f300babaSAlexandros Batsakis 	case AF_INET:
3019f300babaSAlexandros Batsakis 		xs_format_peer_addresses(xprt, "tcp",
3020f300babaSAlexandros Batsakis 					 RPCBIND_NETID_TCP);
3021f300babaSAlexandros Batsakis 		break;
3022f300babaSAlexandros Batsakis 	case AF_INET6:
3023f300babaSAlexandros Batsakis 		xs_format_peer_addresses(xprt, "tcp",
3024f300babaSAlexandros Batsakis 				   RPCBIND_NETID_TCP6);
3025f300babaSAlexandros Batsakis 		break;
3026f300babaSAlexandros Batsakis 	default:
30270a68b0beSJ. Bruce Fields 		ret = ERR_PTR(-EAFNOSUPPORT);
30280a68b0beSJ. Bruce Fields 		goto out_err;
3029f300babaSAlexandros Batsakis 	}
3030f300babaSAlexandros Batsakis 
3031f300babaSAlexandros Batsakis 	dprintk("RPC:       set up xprt to %s (port %s) via %s\n",
3032f300babaSAlexandros Batsakis 			xprt->address_strings[RPC_DISPLAY_ADDR],
3033f300babaSAlexandros Batsakis 			xprt->address_strings[RPC_DISPLAY_PORT],
3034f300babaSAlexandros Batsakis 			xprt->address_strings[RPC_DISPLAY_PROTO]);
3035f300babaSAlexandros Batsakis 
3036f300babaSAlexandros Batsakis 	/*
303799de8ea9SJ. Bruce Fields 	 * Once we've associated a backchannel xprt with a connection,
303828303ca3SWeng Meiling 	 * we want to keep it around as long as the connection lasts,
303928303ca3SWeng Meiling 	 * in case we need to start using it for a backchannel again;
304028303ca3SWeng Meiling 	 * this reference won't be dropped until bc_xprt is destroyed.
304199de8ea9SJ. Bruce Fields 	 */
304299de8ea9SJ. Bruce Fields 	xprt_get(xprt);
304399de8ea9SJ. Bruce Fields 	args->bc_xprt->xpt_bc_xprt = xprt;
304499de8ea9SJ. Bruce Fields 	xprt->bc_xprt = args->bc_xprt;
304599de8ea9SJ. Bruce Fields 	bc_sock = container_of(args->bc_xprt, struct svc_sock, sk_xprt);
304699de8ea9SJ. Bruce Fields 	transport->sock = bc_sock->sk_sock;
304799de8ea9SJ. Bruce Fields 	transport->inet = bc_sock->sk_sk;
304899de8ea9SJ. Bruce Fields 
304999de8ea9SJ. Bruce Fields 	/*
3050f300babaSAlexandros Batsakis 	 * Since we don't want connections for the backchannel, we set
3051f300babaSAlexandros Batsakis 	 * the xprt status to connected
3052f300babaSAlexandros Batsakis 	 */
3053f300babaSAlexandros Batsakis 	xprt_set_connected(xprt);
3054f300babaSAlexandros Batsakis 
3055f300babaSAlexandros Batsakis 	if (try_module_get(THIS_MODULE))
3056f300babaSAlexandros Batsakis 		return xprt;
3057642aab58SKinglong Mee 
3058642aab58SKinglong Mee 	args->bc_xprt->xpt_bc_xprt = NULL;
305939a9beabSJ. Bruce Fields 	args->bc_xprt->xpt_bc_xps = NULL;
306099de8ea9SJ. Bruce Fields 	xprt_put(xprt);
30610a68b0beSJ. Bruce Fields 	ret = ERR_PTR(-EINVAL);
30620a68b0beSJ. Bruce Fields out_err:
3063315f3812SKinglong Mee 	xs_xprt_free(xprt);
30640a68b0beSJ. Bruce Fields 	return ret;
3065f300babaSAlexandros Batsakis }
3066f300babaSAlexandros Batsakis 
3067176e21eeSChuck Lever static struct xprt_class	xs_local_transport = {
3068176e21eeSChuck Lever 	.list		= LIST_HEAD_INIT(xs_local_transport.list),
3069176e21eeSChuck Lever 	.name		= "named UNIX socket",
3070176e21eeSChuck Lever 	.owner		= THIS_MODULE,
3071176e21eeSChuck Lever 	.ident		= XPRT_TRANSPORT_LOCAL,
3072176e21eeSChuck Lever 	.setup		= xs_setup_local,
3073d5aa6b22STrond Myklebust 	.netid		= { "" },
3074176e21eeSChuck Lever };
3075176e21eeSChuck Lever 
3076bc25571eS\"Talpey, Thomas\ static struct xprt_class	xs_udp_transport = {
3077bc25571eS\"Talpey, Thomas\ 	.list		= LIST_HEAD_INIT(xs_udp_transport.list),
3078bc25571eS\"Talpey, Thomas\ 	.name		= "udp",
3079bc25571eS\"Talpey, Thomas\ 	.owner		= THIS_MODULE,
3080f300babaSAlexandros Batsakis 	.ident		= XPRT_TRANSPORT_UDP,
3081bc25571eS\"Talpey, Thomas\ 	.setup		= xs_setup_udp,
3082d5aa6b22STrond Myklebust 	.netid		= { "udp", "udp6", "" },
3083bc25571eS\"Talpey, Thomas\ };
3084bc25571eS\"Talpey, Thomas\ 
3085bc25571eS\"Talpey, Thomas\ static struct xprt_class	xs_tcp_transport = {
3086bc25571eS\"Talpey, Thomas\ 	.list		= LIST_HEAD_INIT(xs_tcp_transport.list),
3087bc25571eS\"Talpey, Thomas\ 	.name		= "tcp",
3088bc25571eS\"Talpey, Thomas\ 	.owner		= THIS_MODULE,
3089f300babaSAlexandros Batsakis 	.ident		= XPRT_TRANSPORT_TCP,
3090bc25571eS\"Talpey, Thomas\ 	.setup		= xs_setup_tcp,
3091d5aa6b22STrond Myklebust 	.netid		= { "tcp", "tcp6", "" },
3092bc25571eS\"Talpey, Thomas\ };
3093bc25571eS\"Talpey, Thomas\ 
3094f300babaSAlexandros Batsakis static struct xprt_class	xs_bc_tcp_transport = {
3095f300babaSAlexandros Batsakis 	.list		= LIST_HEAD_INIT(xs_bc_tcp_transport.list),
3096f300babaSAlexandros Batsakis 	.name		= "tcp NFSv4.1 backchannel",
3097f300babaSAlexandros Batsakis 	.owner		= THIS_MODULE,
3098f300babaSAlexandros Batsakis 	.ident		= XPRT_TRANSPORT_BC_TCP,
3099f300babaSAlexandros Batsakis 	.setup		= xs_setup_bc_tcp,
3100d5aa6b22STrond Myklebust 	.netid		= { "" },
3101f300babaSAlexandros Batsakis };
3102f300babaSAlexandros Batsakis 
3103282b32e1SChuck Lever /**
3104bc25571eS\"Talpey, Thomas\  * init_socket_xprt - set up xprtsock's sysctls, register with RPC client
3105282b32e1SChuck Lever  *
3106282b32e1SChuck Lever  */
3107282b32e1SChuck Lever int init_socket_xprt(void)
3108282b32e1SChuck Lever {
31092b1bec5fSEric W. Biederman 	if (!sunrpc_table_header)
31100b4d4147SEric W. Biederman 		sunrpc_table_header = register_sysctl_table(sunrpc_table);
3111fbf76683SChuck Lever 
3112176e21eeSChuck Lever 	xprt_register_transport(&xs_local_transport);
3113bc25571eS\"Talpey, Thomas\ 	xprt_register_transport(&xs_udp_transport);
3114bc25571eS\"Talpey, Thomas\ 	xprt_register_transport(&xs_tcp_transport);
3115f300babaSAlexandros Batsakis 	xprt_register_transport(&xs_bc_tcp_transport);
3116bc25571eS\"Talpey, Thomas\ 
3117282b32e1SChuck Lever 	return 0;
3118282b32e1SChuck Lever }
3119282b32e1SChuck Lever 
3120282b32e1SChuck Lever /**
3121bc25571eS\"Talpey, Thomas\  * cleanup_socket_xprt - remove xprtsock's sysctls, unregister
3122282b32e1SChuck Lever  *
3123282b32e1SChuck Lever  */
3124282b32e1SChuck Lever void cleanup_socket_xprt(void)
3125282b32e1SChuck Lever {
3126fbf76683SChuck Lever 	if (sunrpc_table_header) {
3127fbf76683SChuck Lever 		unregister_sysctl_table(sunrpc_table_header);
3128fbf76683SChuck Lever 		sunrpc_table_header = NULL;
3129fbf76683SChuck Lever 	}
3130bc25571eS\"Talpey, Thomas\ 
3131176e21eeSChuck Lever 	xprt_unregister_transport(&xs_local_transport);
3132bc25571eS\"Talpey, Thomas\ 	xprt_unregister_transport(&xs_udp_transport);
3133bc25571eS\"Talpey, Thomas\ 	xprt_unregister_transport(&xs_tcp_transport);
3134f300babaSAlexandros Batsakis 	xprt_unregister_transport(&xs_bc_tcp_transport);
3135282b32e1SChuck Lever }
3136cbf11071STrond Myklebust 
31379bbb9e5aSRusty Russell static int param_set_portnr(const char *val, const struct kernel_param *kp)
3138cbf11071STrond Myklebust {
3139cbf11071STrond Myklebust 	return param_set_uint_minmax(val, kp,
3140cbf11071STrond Myklebust 			RPC_MIN_RESVPORT,
3141cbf11071STrond Myklebust 			RPC_MAX_RESVPORT);
3142cbf11071STrond Myklebust }
3143cbf11071STrond Myklebust 
31449c27847dSLuis R. Rodriguez static const struct kernel_param_ops param_ops_portnr = {
31459bbb9e5aSRusty Russell 	.set = param_set_portnr,
31469bbb9e5aSRusty Russell 	.get = param_get_uint,
31479bbb9e5aSRusty Russell };
31489bbb9e5aSRusty Russell 
3149cbf11071STrond Myklebust #define param_check_portnr(name, p) \
3150cbf11071STrond Myklebust 	__param_check(name, p, unsigned int);
3151cbf11071STrond Myklebust 
3152cbf11071STrond Myklebust module_param_named(min_resvport, xprt_min_resvport, portnr, 0644);
3153cbf11071STrond Myklebust module_param_named(max_resvport, xprt_max_resvport, portnr, 0644);
3154cbf11071STrond Myklebust 
31559bbb9e5aSRusty Russell static int param_set_slot_table_size(const char *val,
31569bbb9e5aSRusty Russell 				     const struct kernel_param *kp)
3157cbf11071STrond Myklebust {
3158cbf11071STrond Myklebust 	return param_set_uint_minmax(val, kp,
3159cbf11071STrond Myklebust 			RPC_MIN_SLOT_TABLE,
3160cbf11071STrond Myklebust 			RPC_MAX_SLOT_TABLE);
3161cbf11071STrond Myklebust }
3162cbf11071STrond Myklebust 
31639c27847dSLuis R. Rodriguez static const struct kernel_param_ops param_ops_slot_table_size = {
31649bbb9e5aSRusty Russell 	.set = param_set_slot_table_size,
31659bbb9e5aSRusty Russell 	.get = param_get_uint,
31669bbb9e5aSRusty Russell };
31679bbb9e5aSRusty Russell 
3168cbf11071STrond Myklebust #define param_check_slot_table_size(name, p) \
3169cbf11071STrond Myklebust 	__param_check(name, p, unsigned int);
3170cbf11071STrond Myklebust 
3171d9ba131dSTrond Myklebust static int param_set_max_slot_table_size(const char *val,
3172d9ba131dSTrond Myklebust 				     const struct kernel_param *kp)
3173d9ba131dSTrond Myklebust {
3174d9ba131dSTrond Myklebust 	return param_set_uint_minmax(val, kp,
3175d9ba131dSTrond Myklebust 			RPC_MIN_SLOT_TABLE,
3176d9ba131dSTrond Myklebust 			RPC_MAX_SLOT_TABLE_LIMIT);
3177d9ba131dSTrond Myklebust }
3178d9ba131dSTrond Myklebust 
31799c27847dSLuis R. Rodriguez static const struct kernel_param_ops param_ops_max_slot_table_size = {
3180d9ba131dSTrond Myklebust 	.set = param_set_max_slot_table_size,
3181d9ba131dSTrond Myklebust 	.get = param_get_uint,
3182d9ba131dSTrond Myklebust };
3183d9ba131dSTrond Myklebust 
3184d9ba131dSTrond Myklebust #define param_check_max_slot_table_size(name, p) \
3185d9ba131dSTrond Myklebust 	__param_check(name, p, unsigned int);
3186d9ba131dSTrond Myklebust 
3187cbf11071STrond Myklebust module_param_named(tcp_slot_table_entries, xprt_tcp_slot_table_entries,
3188cbf11071STrond Myklebust 		   slot_table_size, 0644);
3189d9ba131dSTrond Myklebust module_param_named(tcp_max_slot_table_entries, xprt_max_tcp_slot_table_entries,
3190d9ba131dSTrond Myklebust 		   max_slot_table_size, 0644);
3191cbf11071STrond Myklebust module_param_named(udp_slot_table_entries, xprt_udp_slot_table_entries,
3192cbf11071STrond Myklebust 		   slot_table_size, 0644);
3193