xref: /openbmc/linux/net/sunrpc/xprtsock.c (revision d3abc73987fd2a5992a9bdae9f44fa43d1b4db70)
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);
617196dbb0STrond Myklebust static void xs_tcp_set_socket_timeouts(struct rpc_xprt *xprt,
627196dbb0STrond Myklebust 		struct socket *sock);
63176e21eeSChuck Lever 
649903cd1cSChuck Lever /*
65c556b754SChuck Lever  * xprtsock tunables
66c556b754SChuck Lever  */
6709acfea5STrond Myklebust static unsigned int xprt_udp_slot_table_entries = RPC_DEF_SLOT_TABLE;
6809acfea5STrond Myklebust static unsigned int xprt_tcp_slot_table_entries = RPC_MIN_SLOT_TABLE;
6909acfea5STrond Myklebust static unsigned int xprt_max_tcp_slot_table_entries = RPC_MAX_SLOT_TABLE;
70c556b754SChuck Lever 
7109acfea5STrond Myklebust static unsigned int xprt_min_resvport = RPC_DEF_MIN_RESVPORT;
7209acfea5STrond Myklebust static unsigned int xprt_max_resvport = RPC_DEF_MAX_RESVPORT;
73c556b754SChuck Lever 
747d1e8255STrond Myklebust #define XS_TCP_LINGER_TO	(15U * HZ)
7525fe6142STrond Myklebust static unsigned int xs_tcp_fin_timeout __read_mostly = XS_TCP_LINGER_TO;
767d1e8255STrond Myklebust 
77c556b754SChuck Lever /*
78fbf76683SChuck Lever  * We can register our own files under /proc/sys/sunrpc by
79fbf76683SChuck Lever  * calling register_sysctl_table() again.  The files in that
80fbf76683SChuck Lever  * directory become the union of all files registered there.
81fbf76683SChuck Lever  *
82fbf76683SChuck Lever  * We simply need to make sure that we don't collide with
83fbf76683SChuck Lever  * someone else's file names!
84fbf76683SChuck Lever  */
85fbf76683SChuck Lever 
86fbf76683SChuck Lever static unsigned int min_slot_table_size = RPC_MIN_SLOT_TABLE;
87fbf76683SChuck Lever static unsigned int max_slot_table_size = RPC_MAX_SLOT_TABLE;
88d9ba131dSTrond Myklebust static unsigned int max_tcp_slot_table_limit = RPC_MAX_SLOT_TABLE_LIMIT;
89fbf76683SChuck Lever static unsigned int xprt_min_resvport_limit = RPC_MIN_RESVPORT;
90fbf76683SChuck Lever static unsigned int xprt_max_resvport_limit = RPC_MAX_RESVPORT;
91fbf76683SChuck Lever 
92fbf76683SChuck Lever static struct ctl_table_header *sunrpc_table_header;
93fbf76683SChuck Lever 
94*d3abc739SOlga Kornievskaia static struct xprt_class xs_local_transport;
95*d3abc739SOlga Kornievskaia static struct xprt_class xs_udp_transport;
96*d3abc739SOlga Kornievskaia static struct xprt_class xs_tcp_transport;
97*d3abc739SOlga Kornievskaia static struct xprt_class xs_bc_tcp_transport;
98*d3abc739SOlga Kornievskaia 
99fbf76683SChuck Lever /*
100fbf76683SChuck Lever  * FIXME: changing the UDP slot table size should also resize the UDP
101fbf76683SChuck Lever  *        socket buffers for existing UDP transports
102fbf76683SChuck Lever  */
103fe2c6338SJoe Perches static struct ctl_table xs_tunables_table[] = {
104fbf76683SChuck Lever 	{
105fbf76683SChuck Lever 		.procname	= "udp_slot_table_entries",
106fbf76683SChuck Lever 		.data		= &xprt_udp_slot_table_entries,
107fbf76683SChuck Lever 		.maxlen		= sizeof(unsigned int),
108fbf76683SChuck Lever 		.mode		= 0644,
1096d456111SEric W. Biederman 		.proc_handler	= proc_dointvec_minmax,
110fbf76683SChuck Lever 		.extra1		= &min_slot_table_size,
111fbf76683SChuck Lever 		.extra2		= &max_slot_table_size
112fbf76683SChuck Lever 	},
113fbf76683SChuck Lever 	{
114fbf76683SChuck Lever 		.procname	= "tcp_slot_table_entries",
115fbf76683SChuck Lever 		.data		= &xprt_tcp_slot_table_entries,
116fbf76683SChuck Lever 		.maxlen		= sizeof(unsigned int),
117fbf76683SChuck Lever 		.mode		= 0644,
1186d456111SEric W. Biederman 		.proc_handler	= proc_dointvec_minmax,
119fbf76683SChuck Lever 		.extra1		= &min_slot_table_size,
120fbf76683SChuck Lever 		.extra2		= &max_slot_table_size
121fbf76683SChuck Lever 	},
122fbf76683SChuck Lever 	{
123d9ba131dSTrond Myklebust 		.procname	= "tcp_max_slot_table_entries",
124d9ba131dSTrond Myklebust 		.data		= &xprt_max_tcp_slot_table_entries,
125d9ba131dSTrond Myklebust 		.maxlen		= sizeof(unsigned int),
126d9ba131dSTrond Myklebust 		.mode		= 0644,
127d9ba131dSTrond Myklebust 		.proc_handler	= proc_dointvec_minmax,
128d9ba131dSTrond Myklebust 		.extra1		= &min_slot_table_size,
129d9ba131dSTrond Myklebust 		.extra2		= &max_tcp_slot_table_limit
130d9ba131dSTrond Myklebust 	},
131d9ba131dSTrond Myklebust 	{
132fbf76683SChuck Lever 		.procname	= "min_resvport",
133fbf76683SChuck Lever 		.data		= &xprt_min_resvport,
134fbf76683SChuck Lever 		.maxlen		= sizeof(unsigned int),
135fbf76683SChuck Lever 		.mode		= 0644,
1366d456111SEric W. Biederman 		.proc_handler	= proc_dointvec_minmax,
137fbf76683SChuck Lever 		.extra1		= &xprt_min_resvport_limit,
138826799e6SJ. Bruce Fields 		.extra2		= &xprt_max_resvport_limit
139fbf76683SChuck Lever 	},
140fbf76683SChuck Lever 	{
141fbf76683SChuck Lever 		.procname	= "max_resvport",
142fbf76683SChuck Lever 		.data		= &xprt_max_resvport,
143fbf76683SChuck Lever 		.maxlen		= sizeof(unsigned int),
144fbf76683SChuck Lever 		.mode		= 0644,
1456d456111SEric W. Biederman 		.proc_handler	= proc_dointvec_minmax,
146826799e6SJ. Bruce Fields 		.extra1		= &xprt_min_resvport_limit,
147fbf76683SChuck Lever 		.extra2		= &xprt_max_resvport_limit
148fbf76683SChuck Lever 	},
149fbf76683SChuck Lever 	{
15025fe6142STrond Myklebust 		.procname	= "tcp_fin_timeout",
15125fe6142STrond Myklebust 		.data		= &xs_tcp_fin_timeout,
15225fe6142STrond Myklebust 		.maxlen		= sizeof(xs_tcp_fin_timeout),
15325fe6142STrond Myklebust 		.mode		= 0644,
1546d456111SEric W. Biederman 		.proc_handler	= proc_dointvec_jiffies,
15525fe6142STrond Myklebust 	},
156f8572d8fSEric W. Biederman 	{ },
157fbf76683SChuck Lever };
158fbf76683SChuck Lever 
159fe2c6338SJoe Perches static struct ctl_table sunrpc_table[] = {
160fbf76683SChuck Lever 	{
161fbf76683SChuck Lever 		.procname	= "sunrpc",
162fbf76683SChuck Lever 		.mode		= 0555,
163fbf76683SChuck Lever 		.child		= xs_tunables_table
164fbf76683SChuck Lever 	},
165f8572d8fSEric W. Biederman 	{ },
166fbf76683SChuck Lever };
167fbf76683SChuck Lever 
168fbf76683SChuck Lever /*
16903bf4b70SChuck Lever  * Wait duration for a reply from the RPC portmapper.
17003bf4b70SChuck Lever  */
17103bf4b70SChuck Lever #define XS_BIND_TO		(60U * HZ)
17203bf4b70SChuck Lever 
17303bf4b70SChuck Lever /*
17403bf4b70SChuck Lever  * Delay if a UDP socket connect error occurs.  This is most likely some
17503bf4b70SChuck Lever  * kind of resource problem on the local host.
17603bf4b70SChuck Lever  */
17703bf4b70SChuck Lever #define XS_UDP_REEST_TO		(2U * HZ)
17803bf4b70SChuck Lever 
17903bf4b70SChuck Lever /*
18003bf4b70SChuck Lever  * The reestablish timeout allows clients to delay for a bit before attempting
18103bf4b70SChuck Lever  * to reconnect to a server that just dropped our connection.
18203bf4b70SChuck Lever  *
18303bf4b70SChuck Lever  * We implement an exponential backoff when trying to reestablish a TCP
18403bf4b70SChuck Lever  * transport connection with the server.  Some servers like to drop a TCP
18503bf4b70SChuck Lever  * connection when they are overworked, so we start with a short timeout and
18603bf4b70SChuck Lever  * increase over time if the server is down or not responding.
18703bf4b70SChuck Lever  */
18803bf4b70SChuck Lever #define XS_TCP_INIT_REEST_TO	(3U * HZ)
18903bf4b70SChuck Lever 
19003bf4b70SChuck Lever /*
19103bf4b70SChuck Lever  * TCP idle timeout; client drops the transport socket if it is idle
19203bf4b70SChuck Lever  * for this long.  Note that we also timeout UDP sockets to prevent
19303bf4b70SChuck Lever  * holding port numbers when there is no RPC traffic.
19403bf4b70SChuck Lever  */
19503bf4b70SChuck Lever #define XS_IDLE_DISC_TO		(5U * 60 * HZ)
19603bf4b70SChuck Lever 
197f895b252SJeff Layton #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
198a246b010SChuck Lever # undef  RPC_DEBUG_DATA
1999903cd1cSChuck Lever # define RPCDBG_FACILITY	RPCDBG_TRANS
200a246b010SChuck Lever #endif
201a246b010SChuck Lever 
202a246b010SChuck Lever #ifdef RPC_DEBUG_DATA
2039903cd1cSChuck Lever static void xs_pktdump(char *msg, u32 *packet, unsigned int count)
204a246b010SChuck Lever {
205a246b010SChuck Lever 	u8 *buf = (u8 *) packet;
206a246b010SChuck Lever 	int j;
207a246b010SChuck Lever 
208a246b010SChuck Lever 	dprintk("RPC:       %s\n", msg);
209a246b010SChuck Lever 	for (j = 0; j < count && j < 128; j += 4) {
210a246b010SChuck Lever 		if (!(j & 31)) {
211a246b010SChuck Lever 			if (j)
212a246b010SChuck Lever 				dprintk("\n");
213a246b010SChuck Lever 			dprintk("0x%04x ", j);
214a246b010SChuck Lever 		}
215a246b010SChuck Lever 		dprintk("%02x%02x%02x%02x ",
216a246b010SChuck Lever 			buf[j], buf[j+1], buf[j+2], buf[j+3]);
217a246b010SChuck Lever 	}
218a246b010SChuck Lever 	dprintk("\n");
219a246b010SChuck Lever }
220a246b010SChuck Lever #else
2219903cd1cSChuck Lever static inline void xs_pktdump(char *msg, u32 *packet, unsigned int count)
222a246b010SChuck Lever {
223a246b010SChuck Lever 	/* NOP */
224a246b010SChuck Lever }
225a246b010SChuck Lever #endif
226a246b010SChuck Lever 
2272118071dSTrond Myklebust static inline struct rpc_xprt *xprt_from_sock(struct sock *sk)
2282118071dSTrond Myklebust {
2292118071dSTrond Myklebust 	return (struct rpc_xprt *) sk->sk_user_data;
2302118071dSTrond Myklebust }
2312118071dSTrond Myklebust 
23295392c59SChuck Lever static inline struct sockaddr *xs_addr(struct rpc_xprt *xprt)
233edb267a6SChuck Lever {
23495392c59SChuck Lever 	return (struct sockaddr *) &xprt->addr;
23595392c59SChuck Lever }
23695392c59SChuck Lever 
237176e21eeSChuck Lever static inline struct sockaddr_un *xs_addr_un(struct rpc_xprt *xprt)
238176e21eeSChuck Lever {
239176e21eeSChuck Lever 	return (struct sockaddr_un *) &xprt->addr;
240176e21eeSChuck Lever }
241176e21eeSChuck Lever 
24295392c59SChuck Lever static inline struct sockaddr_in *xs_addr_in(struct rpc_xprt *xprt)
24395392c59SChuck Lever {
24495392c59SChuck Lever 	return (struct sockaddr_in *) &xprt->addr;
24595392c59SChuck Lever }
24695392c59SChuck Lever 
24795392c59SChuck Lever static inline struct sockaddr_in6 *xs_addr_in6(struct rpc_xprt *xprt)
24895392c59SChuck Lever {
24995392c59SChuck Lever 	return (struct sockaddr_in6 *) &xprt->addr;
25095392c59SChuck Lever }
25195392c59SChuck Lever 
252c877b849SChuck Lever static void xs_format_common_peer_addresses(struct rpc_xprt *xprt)
253c877b849SChuck Lever {
254c877b849SChuck Lever 	struct sockaddr *sap = xs_addr(xprt);
2559dc3b095SChuck Lever 	struct sockaddr_in6 *sin6;
2569dc3b095SChuck Lever 	struct sockaddr_in *sin;
257176e21eeSChuck Lever 	struct sockaddr_un *sun;
258c877b849SChuck Lever 	char buf[128];
259c877b849SChuck Lever 
2609dc3b095SChuck Lever 	switch (sap->sa_family) {
261176e21eeSChuck Lever 	case AF_LOCAL:
262176e21eeSChuck Lever 		sun = xs_addr_un(xprt);
263176e21eeSChuck Lever 		strlcpy(buf, sun->sun_path, sizeof(buf));
264176e21eeSChuck Lever 		xprt->address_strings[RPC_DISPLAY_ADDR] =
265176e21eeSChuck Lever 						kstrdup(buf, GFP_KERNEL);
266176e21eeSChuck Lever 		break;
2679dc3b095SChuck Lever 	case AF_INET:
268176e21eeSChuck Lever 		(void)rpc_ntop(sap, buf, sizeof(buf));
269176e21eeSChuck Lever 		xprt->address_strings[RPC_DISPLAY_ADDR] =
270176e21eeSChuck Lever 						kstrdup(buf, GFP_KERNEL);
2719dc3b095SChuck Lever 		sin = xs_addr_in(xprt);
272fc0b5791SJoe Perches 		snprintf(buf, sizeof(buf), "%08x", ntohl(sin->sin_addr.s_addr));
2739dc3b095SChuck Lever 		break;
2749dc3b095SChuck Lever 	case AF_INET6:
275176e21eeSChuck Lever 		(void)rpc_ntop(sap, buf, sizeof(buf));
276176e21eeSChuck Lever 		xprt->address_strings[RPC_DISPLAY_ADDR] =
277176e21eeSChuck Lever 						kstrdup(buf, GFP_KERNEL);
2789dc3b095SChuck Lever 		sin6 = xs_addr_in6(xprt);
279fc0b5791SJoe Perches 		snprintf(buf, sizeof(buf), "%pi6", &sin6->sin6_addr);
2809dc3b095SChuck Lever 		break;
2819dc3b095SChuck Lever 	default:
2829dc3b095SChuck Lever 		BUG();
2839dc3b095SChuck Lever 	}
284176e21eeSChuck Lever 
2859dc3b095SChuck Lever 	xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = kstrdup(buf, GFP_KERNEL);
2869dc3b095SChuck Lever }
2879dc3b095SChuck Lever 
2889dc3b095SChuck Lever static void xs_format_common_peer_ports(struct rpc_xprt *xprt)
2899dc3b095SChuck Lever {
2909dc3b095SChuck Lever 	struct sockaddr *sap = xs_addr(xprt);
2919dc3b095SChuck Lever 	char buf[128];
2929dc3b095SChuck Lever 
29381160e66SJoe Perches 	snprintf(buf, sizeof(buf), "%u", rpc_get_port(sap));
294c877b849SChuck Lever 	xprt->address_strings[RPC_DISPLAY_PORT] = kstrdup(buf, GFP_KERNEL);
295c877b849SChuck Lever 
29681160e66SJoe Perches 	snprintf(buf, sizeof(buf), "%4hx", rpc_get_port(sap));
297c877b849SChuck Lever 	xprt->address_strings[RPC_DISPLAY_HEX_PORT] = kstrdup(buf, GFP_KERNEL);
298c877b849SChuck Lever }
299c877b849SChuck Lever 
3009dc3b095SChuck Lever static void xs_format_peer_addresses(struct rpc_xprt *xprt,
301b454ae90SChuck Lever 				     const char *protocol,
302b454ae90SChuck Lever 				     const char *netid)
303edb267a6SChuck Lever {
304b454ae90SChuck Lever 	xprt->address_strings[RPC_DISPLAY_PROTO] = protocol;
305b454ae90SChuck Lever 	xprt->address_strings[RPC_DISPLAY_NETID] = netid;
306c877b849SChuck Lever 	xs_format_common_peer_addresses(xprt);
3079dc3b095SChuck Lever 	xs_format_common_peer_ports(xprt);
308edb267a6SChuck Lever }
309edb267a6SChuck Lever 
3109dc3b095SChuck Lever static void xs_update_peer_port(struct rpc_xprt *xprt)
3114b6473fbSChuck Lever {
3129dc3b095SChuck Lever 	kfree(xprt->address_strings[RPC_DISPLAY_HEX_PORT]);
3139dc3b095SChuck Lever 	kfree(xprt->address_strings[RPC_DISPLAY_PORT]);
3144b6473fbSChuck Lever 
3159dc3b095SChuck Lever 	xs_format_common_peer_ports(xprt);
316edb267a6SChuck Lever }
317edb267a6SChuck Lever 
318edb267a6SChuck Lever static void xs_free_peer_addresses(struct rpc_xprt *xprt)
319edb267a6SChuck Lever {
32033e01dc7SChuck Lever 	unsigned int i;
32133e01dc7SChuck Lever 
32233e01dc7SChuck Lever 	for (i = 0; i < RPC_DISPLAY_MAX; i++)
32333e01dc7SChuck Lever 		switch (i) {
32433e01dc7SChuck Lever 		case RPC_DISPLAY_PROTO:
32533e01dc7SChuck Lever 		case RPC_DISPLAY_NETID:
32633e01dc7SChuck Lever 			continue;
32733e01dc7SChuck Lever 		default:
32833e01dc7SChuck Lever 			kfree(xprt->address_strings[i]);
32933e01dc7SChuck Lever 		}
330edb267a6SChuck Lever }
331edb267a6SChuck Lever 
332277e4ab7STrond Myklebust static size_t
333277e4ab7STrond Myklebust xs_alloc_sparse_pages(struct xdr_buf *buf, size_t want, gfp_t gfp)
334277e4ab7STrond Myklebust {
335277e4ab7STrond Myklebust 	size_t i,n;
336277e4ab7STrond Myklebust 
33716e5e90fSTrond Myklebust 	if (!want || !(buf->flags & XDRBUF_SPARSE_PAGES))
338277e4ab7STrond Myklebust 		return want;
339277e4ab7STrond Myklebust 	n = (buf->page_base + want + PAGE_SIZE - 1) >> PAGE_SHIFT;
340277e4ab7STrond Myklebust 	for (i = 0; i < n; i++) {
341277e4ab7STrond Myklebust 		if (buf->pages[i])
342277e4ab7STrond Myklebust 			continue;
343277e4ab7STrond Myklebust 		buf->bvec[i].bv_page = buf->pages[i] = alloc_page(gfp);
344277e4ab7STrond Myklebust 		if (!buf->pages[i]) {
34516e5e90fSTrond Myklebust 			i *= PAGE_SIZE;
34616e5e90fSTrond Myklebust 			return i > buf->page_base ? i - buf->page_base : 0;
347277e4ab7STrond Myklebust 		}
348277e4ab7STrond Myklebust 	}
349277e4ab7STrond Myklebust 	return want;
350277e4ab7STrond Myklebust }
351277e4ab7STrond Myklebust 
352277e4ab7STrond Myklebust static ssize_t
353277e4ab7STrond Myklebust xs_sock_recvmsg(struct socket *sock, struct msghdr *msg, int flags, size_t seek)
354277e4ab7STrond Myklebust {
355277e4ab7STrond Myklebust 	ssize_t ret;
356277e4ab7STrond Myklebust 	if (seek != 0)
357277e4ab7STrond Myklebust 		iov_iter_advance(&msg->msg_iter, seek);
358277e4ab7STrond Myklebust 	ret = sock_recvmsg(sock, msg, flags);
359277e4ab7STrond Myklebust 	return ret > 0 ? ret + seek : ret;
360277e4ab7STrond Myklebust }
361277e4ab7STrond Myklebust 
362277e4ab7STrond Myklebust static ssize_t
363277e4ab7STrond Myklebust xs_read_kvec(struct socket *sock, struct msghdr *msg, int flags,
364277e4ab7STrond Myklebust 		struct kvec *kvec, size_t count, size_t seek)
365277e4ab7STrond Myklebust {
3660e9b4a82SAl Viro 	iov_iter_kvec(&msg->msg_iter, READ, kvec, 1, count);
367277e4ab7STrond Myklebust 	return xs_sock_recvmsg(sock, msg, flags, seek);
368277e4ab7STrond Myklebust }
369277e4ab7STrond Myklebust 
370277e4ab7STrond Myklebust static ssize_t
371277e4ab7STrond Myklebust xs_read_bvec(struct socket *sock, struct msghdr *msg, int flags,
372277e4ab7STrond Myklebust 		struct bio_vec *bvec, unsigned long nr, size_t count,
373277e4ab7STrond Myklebust 		size_t seek)
374277e4ab7STrond Myklebust {
3750e9b4a82SAl Viro 	iov_iter_bvec(&msg->msg_iter, READ, bvec, nr, count);
376277e4ab7STrond Myklebust 	return xs_sock_recvmsg(sock, msg, flags, seek);
377277e4ab7STrond Myklebust }
378277e4ab7STrond Myklebust 
379277e4ab7STrond Myklebust static ssize_t
380277e4ab7STrond Myklebust xs_read_discard(struct socket *sock, struct msghdr *msg, int flags,
381277e4ab7STrond Myklebust 		size_t count)
382277e4ab7STrond Myklebust {
383b76a5afdSTrond Myklebust 	iov_iter_discard(&msg->msg_iter, READ, count);
384b76a5afdSTrond Myklebust 	return sock_recvmsg(sock, msg, flags);
385277e4ab7STrond Myklebust }
386277e4ab7STrond Myklebust 
3876a829eb8STrond Myklebust #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
3886a829eb8STrond Myklebust static void
3896a829eb8STrond Myklebust xs_flush_bvec(const struct bio_vec *bvec, size_t count, size_t seek)
3906a829eb8STrond Myklebust {
3916a829eb8STrond Myklebust 	struct bvec_iter bi = {
3926a829eb8STrond Myklebust 		.bi_size = count,
3936a829eb8STrond Myklebust 	};
3946a829eb8STrond Myklebust 	struct bio_vec bv;
3956a829eb8STrond Myklebust 
3966a829eb8STrond Myklebust 	bvec_iter_advance(bvec, &bi, seek & PAGE_MASK);
3976a829eb8STrond Myklebust 	for_each_bvec(bv, bvec, bi, bi)
3986a829eb8STrond Myklebust 		flush_dcache_page(bv.bv_page);
3996a829eb8STrond Myklebust }
4006a829eb8STrond Myklebust #else
4016a829eb8STrond Myklebust static inline void
4026a829eb8STrond Myklebust xs_flush_bvec(const struct bio_vec *bvec, size_t count, size_t seek)
4036a829eb8STrond Myklebust {
4046a829eb8STrond Myklebust }
4056a829eb8STrond Myklebust #endif
4066a829eb8STrond Myklebust 
407277e4ab7STrond Myklebust static ssize_t
408277e4ab7STrond Myklebust xs_read_xdr_buf(struct socket *sock, struct msghdr *msg, int flags,
409277e4ab7STrond Myklebust 		struct xdr_buf *buf, size_t count, size_t seek, size_t *read)
410277e4ab7STrond Myklebust {
411277e4ab7STrond Myklebust 	size_t want, seek_init = seek, offset = 0;
412277e4ab7STrond Myklebust 	ssize_t ret;
413277e4ab7STrond Myklebust 
414277e4ab7STrond Myklebust 	want = min_t(size_t, count, buf->head[0].iov_len);
415e92053a5STrond Myklebust 	if (seek < want) {
416277e4ab7STrond Myklebust 		ret = xs_read_kvec(sock, msg, flags, &buf->head[0], want, seek);
417277e4ab7STrond Myklebust 		if (ret <= 0)
418277e4ab7STrond Myklebust 			goto sock_err;
419277e4ab7STrond Myklebust 		offset += ret;
420277e4ab7STrond Myklebust 		if (offset == count || msg->msg_flags & (MSG_EOR|MSG_TRUNC))
421277e4ab7STrond Myklebust 			goto out;
422277e4ab7STrond Myklebust 		if (ret != want)
423c4433055STrond Myklebust 			goto out;
424277e4ab7STrond Myklebust 		seek = 0;
425277e4ab7STrond Myklebust 	} else {
426e92053a5STrond Myklebust 		seek -= want;
427e92053a5STrond Myklebust 		offset += want;
428277e4ab7STrond Myklebust 	}
42916e5e90fSTrond Myklebust 
430277e4ab7STrond Myklebust 	want = xs_alloc_sparse_pages(buf,
431277e4ab7STrond Myklebust 			min_t(size_t, count - offset, buf->page_len),
43212a3ad61STrond Myklebust 			GFP_KERNEL);
43316e5e90fSTrond Myklebust 	if (seek < want) {
434277e4ab7STrond Myklebust 		ret = xs_read_bvec(sock, msg, flags, buf->bvec,
435277e4ab7STrond Myklebust 				xdr_buf_pagecount(buf),
436277e4ab7STrond Myklebust 				want + buf->page_base,
437277e4ab7STrond Myklebust 				seek + buf->page_base);
438277e4ab7STrond Myklebust 		if (ret <= 0)
439277e4ab7STrond Myklebust 			goto sock_err;
4406a829eb8STrond Myklebust 		xs_flush_bvec(buf->bvec, ret, seek + buf->page_base);
441ac9645c8SDan Aloni 		ret -= buf->page_base;
442ac9645c8SDan Aloni 		offset += ret;
443277e4ab7STrond Myklebust 		if (offset == count || msg->msg_flags & (MSG_EOR|MSG_TRUNC))
444277e4ab7STrond Myklebust 			goto out;
445277e4ab7STrond Myklebust 		if (ret != want)
446c4433055STrond Myklebust 			goto out;
447277e4ab7STrond Myklebust 		seek = 0;
448277e4ab7STrond Myklebust 	} else {
44916e5e90fSTrond Myklebust 		seek -= want;
45016e5e90fSTrond Myklebust 		offset += want;
451277e4ab7STrond Myklebust 	}
45216e5e90fSTrond Myklebust 
453277e4ab7STrond Myklebust 	want = min_t(size_t, count - offset, buf->tail[0].iov_len);
454e92053a5STrond Myklebust 	if (seek < want) {
455277e4ab7STrond Myklebust 		ret = xs_read_kvec(sock, msg, flags, &buf->tail[0], want, seek);
456277e4ab7STrond Myklebust 		if (ret <= 0)
457277e4ab7STrond Myklebust 			goto sock_err;
458277e4ab7STrond Myklebust 		offset += ret;
459277e4ab7STrond Myklebust 		if (offset == count || msg->msg_flags & (MSG_EOR|MSG_TRUNC))
460277e4ab7STrond Myklebust 			goto out;
461277e4ab7STrond Myklebust 		if (ret != want)
462c4433055STrond Myklebust 			goto out;
4639734ad57STrond Myklebust 	} else if (offset < seek_init)
464e92053a5STrond Myklebust 		offset = seek_init;
465277e4ab7STrond Myklebust 	ret = -EMSGSIZE;
466277e4ab7STrond Myklebust out:
467277e4ab7STrond Myklebust 	*read = offset - seek_init;
468277e4ab7STrond Myklebust 	return ret;
469277e4ab7STrond Myklebust sock_err:
470277e4ab7STrond Myklebust 	offset += seek;
471277e4ab7STrond Myklebust 	goto out;
472277e4ab7STrond Myklebust }
473277e4ab7STrond Myklebust 
474277e4ab7STrond Myklebust static void
475277e4ab7STrond Myklebust xs_read_header(struct sock_xprt *transport, struct xdr_buf *buf)
476277e4ab7STrond Myklebust {
477277e4ab7STrond Myklebust 	if (!transport->recv.copied) {
478277e4ab7STrond Myklebust 		if (buf->head[0].iov_len >= transport->recv.offset)
479277e4ab7STrond Myklebust 			memcpy(buf->head[0].iov_base,
480277e4ab7STrond Myklebust 					&transport->recv.xid,
481277e4ab7STrond Myklebust 					transport->recv.offset);
482277e4ab7STrond Myklebust 		transport->recv.copied = transport->recv.offset;
483277e4ab7STrond Myklebust 	}
484277e4ab7STrond Myklebust }
485277e4ab7STrond Myklebust 
486277e4ab7STrond Myklebust static bool
487277e4ab7STrond Myklebust xs_read_stream_request_done(struct sock_xprt *transport)
488277e4ab7STrond Myklebust {
489277e4ab7STrond Myklebust 	return transport->recv.fraghdr & cpu_to_be32(RPC_LAST_STREAM_FRAGMENT);
490277e4ab7STrond Myklebust }
491277e4ab7STrond Myklebust 
492e92053a5STrond Myklebust static void
493e92053a5STrond Myklebust xs_read_stream_check_eor(struct sock_xprt *transport,
494e92053a5STrond Myklebust 		struct msghdr *msg)
495e92053a5STrond Myklebust {
496e92053a5STrond Myklebust 	if (xs_read_stream_request_done(transport))
497e92053a5STrond Myklebust 		msg->msg_flags |= MSG_EOR;
498e92053a5STrond Myklebust }
499e92053a5STrond Myklebust 
500277e4ab7STrond Myklebust static ssize_t
501277e4ab7STrond Myklebust xs_read_stream_request(struct sock_xprt *transport, struct msghdr *msg,
502277e4ab7STrond Myklebust 		int flags, struct rpc_rqst *req)
503277e4ab7STrond Myklebust {
504277e4ab7STrond Myklebust 	struct xdr_buf *buf = &req->rq_private_buf;
5053f649ab7SKees Cook 	size_t want, read;
5063f649ab7SKees Cook 	ssize_t ret;
507277e4ab7STrond Myklebust 
508277e4ab7STrond Myklebust 	xs_read_header(transport, buf);
509277e4ab7STrond Myklebust 
510277e4ab7STrond Myklebust 	want = transport->recv.len - transport->recv.offset;
511e92053a5STrond Myklebust 	if (want != 0) {
512277e4ab7STrond Myklebust 		ret = xs_read_xdr_buf(transport->sock, msg, flags, buf,
513e92053a5STrond Myklebust 				transport->recv.copied + want,
514e92053a5STrond Myklebust 				transport->recv.copied,
515277e4ab7STrond Myklebust 				&read);
516277e4ab7STrond Myklebust 		transport->recv.offset += read;
517277e4ab7STrond Myklebust 		transport->recv.copied += read;
518277e4ab7STrond Myklebust 	}
519277e4ab7STrond Myklebust 
520727fcc64STrond Myklebust 	if (transport->recv.offset == transport->recv.len)
521727fcc64STrond Myklebust 		xs_read_stream_check_eor(transport, msg);
522727fcc64STrond Myklebust 
523e92053a5STrond Myklebust 	if (want == 0)
524e92053a5STrond Myklebust 		return 0;
525e92053a5STrond Myklebust 
526277e4ab7STrond Myklebust 	switch (ret) {
527c4433055STrond Myklebust 	default:
528c4433055STrond Myklebust 		break;
52926781eabSTrond Myklebust 	case -EFAULT:
530277e4ab7STrond Myklebust 	case -EMSGSIZE:
53126781eabSTrond Myklebust 		msg->msg_flags |= MSG_TRUNC;
532c4433055STrond Myklebust 		return read;
533277e4ab7STrond Myklebust 	case 0:
534277e4ab7STrond Myklebust 		return -ESHUTDOWN;
535277e4ab7STrond Myklebust 	}
536c4433055STrond Myklebust 	return ret < 0 ? ret : read;
537277e4ab7STrond Myklebust }
538277e4ab7STrond Myklebust 
539277e4ab7STrond Myklebust static size_t
540277e4ab7STrond Myklebust xs_read_stream_headersize(bool isfrag)
541277e4ab7STrond Myklebust {
542277e4ab7STrond Myklebust 	if (isfrag)
543277e4ab7STrond Myklebust 		return sizeof(__be32);
544277e4ab7STrond Myklebust 	return 3 * sizeof(__be32);
545277e4ab7STrond Myklebust }
546277e4ab7STrond Myklebust 
547277e4ab7STrond Myklebust static ssize_t
548277e4ab7STrond Myklebust xs_read_stream_header(struct sock_xprt *transport, struct msghdr *msg,
549277e4ab7STrond Myklebust 		int flags, size_t want, size_t seek)
550277e4ab7STrond Myklebust {
551277e4ab7STrond Myklebust 	struct kvec kvec = {
552277e4ab7STrond Myklebust 		.iov_base = &transport->recv.fraghdr,
553277e4ab7STrond Myklebust 		.iov_len = want,
554277e4ab7STrond Myklebust 	};
555277e4ab7STrond Myklebust 	return xs_read_kvec(transport->sock, msg, flags, &kvec, want, seek);
556277e4ab7STrond Myklebust }
557277e4ab7STrond Myklebust 
558277e4ab7STrond Myklebust #if defined(CONFIG_SUNRPC_BACKCHANNEL)
559277e4ab7STrond Myklebust static ssize_t
560277e4ab7STrond Myklebust xs_read_stream_call(struct sock_xprt *transport, struct msghdr *msg, int flags)
561277e4ab7STrond Myklebust {
562277e4ab7STrond Myklebust 	struct rpc_xprt *xprt = &transport->xprt;
563277e4ab7STrond Myklebust 	struct rpc_rqst *req;
564277e4ab7STrond Myklebust 	ssize_t ret;
565277e4ab7STrond Myklebust 
56698b5cee3SBenjamin Coddington 	/* Is this transport associated with the backchannel? */
56798b5cee3SBenjamin Coddington 	if (!xprt->bc_serv)
56898b5cee3SBenjamin Coddington 		return -ESHUTDOWN;
56998b5cee3SBenjamin Coddington 
570277e4ab7STrond Myklebust 	/* Look up and lock the request corresponding to the given XID */
571277e4ab7STrond Myklebust 	req = xprt_lookup_bc_request(xprt, transport->recv.xid);
572277e4ab7STrond Myklebust 	if (!req) {
573277e4ab7STrond Myklebust 		printk(KERN_WARNING "Callback slot table overflowed\n");
574277e4ab7STrond Myklebust 		return -ESHUTDOWN;
575277e4ab7STrond Myklebust 	}
57645835a63STrond Myklebust 	if (transport->recv.copied && !req->rq_private_buf.len)
57745835a63STrond Myklebust 		return -ESHUTDOWN;
578277e4ab7STrond Myklebust 
579277e4ab7STrond Myklebust 	ret = xs_read_stream_request(transport, msg, flags, req);
580277e4ab7STrond Myklebust 	if (msg->msg_flags & (MSG_EOR|MSG_TRUNC))
581c4433055STrond Myklebust 		xprt_complete_bc_request(req, transport->recv.copied);
58245835a63STrond Myklebust 	else
58345835a63STrond Myklebust 		req->rq_private_buf.len = transport->recv.copied;
584277e4ab7STrond Myklebust 
585277e4ab7STrond Myklebust 	return ret;
586277e4ab7STrond Myklebust }
587277e4ab7STrond Myklebust #else /* CONFIG_SUNRPC_BACKCHANNEL */
588277e4ab7STrond Myklebust static ssize_t
589277e4ab7STrond Myklebust xs_read_stream_call(struct sock_xprt *transport, struct msghdr *msg, int flags)
590277e4ab7STrond Myklebust {
591277e4ab7STrond Myklebust 	return -ESHUTDOWN;
592277e4ab7STrond Myklebust }
593277e4ab7STrond Myklebust #endif /* CONFIG_SUNRPC_BACKCHANNEL */
594277e4ab7STrond Myklebust 
595277e4ab7STrond Myklebust static ssize_t
596277e4ab7STrond Myklebust xs_read_stream_reply(struct sock_xprt *transport, struct msghdr *msg, int flags)
597277e4ab7STrond Myklebust {
598277e4ab7STrond Myklebust 	struct rpc_xprt *xprt = &transport->xprt;
599277e4ab7STrond Myklebust 	struct rpc_rqst *req;
600277e4ab7STrond Myklebust 	ssize_t ret = 0;
601277e4ab7STrond Myklebust 
602277e4ab7STrond Myklebust 	/* Look up and lock the request corresponding to the given XID */
603277e4ab7STrond Myklebust 	spin_lock(&xprt->queue_lock);
604277e4ab7STrond Myklebust 	req = xprt_lookup_rqst(xprt, transport->recv.xid);
60545835a63STrond Myklebust 	if (!req || (transport->recv.copied && !req->rq_private_buf.len)) {
606277e4ab7STrond Myklebust 		msg->msg_flags |= MSG_TRUNC;
607277e4ab7STrond Myklebust 		goto out;
608277e4ab7STrond Myklebust 	}
609277e4ab7STrond Myklebust 	xprt_pin_rqst(req);
610277e4ab7STrond Myklebust 	spin_unlock(&xprt->queue_lock);
611277e4ab7STrond Myklebust 
612277e4ab7STrond Myklebust 	ret = xs_read_stream_request(transport, msg, flags, req);
613277e4ab7STrond Myklebust 
614277e4ab7STrond Myklebust 	spin_lock(&xprt->queue_lock);
615277e4ab7STrond Myklebust 	if (msg->msg_flags & (MSG_EOR|MSG_TRUNC))
616c4433055STrond Myklebust 		xprt_complete_rqst(req->rq_task, transport->recv.copied);
61745835a63STrond Myklebust 	else
61845835a63STrond Myklebust 		req->rq_private_buf.len = transport->recv.copied;
619277e4ab7STrond Myklebust 	xprt_unpin_rqst(req);
620277e4ab7STrond Myklebust out:
621277e4ab7STrond Myklebust 	spin_unlock(&xprt->queue_lock);
622277e4ab7STrond Myklebust 	return ret;
623277e4ab7STrond Myklebust }
624277e4ab7STrond Myklebust 
625277e4ab7STrond Myklebust static ssize_t
626277e4ab7STrond Myklebust xs_read_stream(struct sock_xprt *transport, int flags)
627277e4ab7STrond Myklebust {
628277e4ab7STrond Myklebust 	struct msghdr msg = { 0 };
629277e4ab7STrond Myklebust 	size_t want, read = 0;
630277e4ab7STrond Myklebust 	ssize_t ret = 0;
631277e4ab7STrond Myklebust 
632277e4ab7STrond Myklebust 	if (transport->recv.len == 0) {
633277e4ab7STrond Myklebust 		want = xs_read_stream_headersize(transport->recv.copied != 0);
634277e4ab7STrond Myklebust 		ret = xs_read_stream_header(transport, &msg, flags, want,
635277e4ab7STrond Myklebust 				transport->recv.offset);
636277e4ab7STrond Myklebust 		if (ret <= 0)
637277e4ab7STrond Myklebust 			goto out_err;
638277e4ab7STrond Myklebust 		transport->recv.offset = ret;
639c4433055STrond Myklebust 		if (transport->recv.offset != want)
640c4433055STrond Myklebust 			return transport->recv.offset;
641277e4ab7STrond Myklebust 		transport->recv.len = be32_to_cpu(transport->recv.fraghdr) &
642277e4ab7STrond Myklebust 			RPC_FRAGMENT_SIZE_MASK;
643277e4ab7STrond Myklebust 		transport->recv.offset -= sizeof(transport->recv.fraghdr);
644277e4ab7STrond Myklebust 		read = ret;
645277e4ab7STrond Myklebust 	}
646277e4ab7STrond Myklebust 
647277e4ab7STrond Myklebust 	switch (be32_to_cpu(transport->recv.calldir)) {
648c4433055STrond Myklebust 	default:
649c4433055STrond Myklebust 		msg.msg_flags |= MSG_TRUNC;
650c4433055STrond Myklebust 		break;
651277e4ab7STrond Myklebust 	case RPC_CALL:
652277e4ab7STrond Myklebust 		ret = xs_read_stream_call(transport, &msg, flags);
653277e4ab7STrond Myklebust 		break;
654277e4ab7STrond Myklebust 	case RPC_REPLY:
655277e4ab7STrond Myklebust 		ret = xs_read_stream_reply(transport, &msg, flags);
656277e4ab7STrond Myklebust 	}
657277e4ab7STrond Myklebust 	if (msg.msg_flags & MSG_TRUNC) {
658277e4ab7STrond Myklebust 		transport->recv.calldir = cpu_to_be32(-1);
659277e4ab7STrond Myklebust 		transport->recv.copied = -1;
660277e4ab7STrond Myklebust 	}
661277e4ab7STrond Myklebust 	if (ret < 0)
662277e4ab7STrond Myklebust 		goto out_err;
663277e4ab7STrond Myklebust 	read += ret;
664277e4ab7STrond Myklebust 	if (transport->recv.offset < transport->recv.len) {
665c4433055STrond Myklebust 		if (!(msg.msg_flags & MSG_TRUNC))
666c4433055STrond Myklebust 			return read;
667b76a5afdSTrond Myklebust 		msg.msg_flags = 0;
668277e4ab7STrond Myklebust 		ret = xs_read_discard(transport->sock, &msg, flags,
669277e4ab7STrond Myklebust 				transport->recv.len - transport->recv.offset);
670277e4ab7STrond Myklebust 		if (ret <= 0)
671277e4ab7STrond Myklebust 			goto out_err;
672277e4ab7STrond Myklebust 		transport->recv.offset += ret;
673277e4ab7STrond Myklebust 		read += ret;
674277e4ab7STrond Myklebust 		if (transport->recv.offset != transport->recv.len)
675c4433055STrond Myklebust 			return read;
676277e4ab7STrond Myklebust 	}
677277e4ab7STrond Myklebust 	if (xs_read_stream_request_done(transport)) {
678c50b8ee0STrond Myklebust 		trace_xs_stream_read_request(transport);
679277e4ab7STrond Myklebust 		transport->recv.copied = 0;
680277e4ab7STrond Myklebust 	}
681277e4ab7STrond Myklebust 	transport->recv.offset = 0;
682277e4ab7STrond Myklebust 	transport->recv.len = 0;
683277e4ab7STrond Myklebust 	return read;
684277e4ab7STrond Myklebust out_err:
68579462857STrond Myklebust 	return ret != 0 ? ret : -ESHUTDOWN;
686277e4ab7STrond Myklebust }
687277e4ab7STrond Myklebust 
6880ffe86f4STrond Myklebust static __poll_t xs_poll_socket(struct sock_xprt *transport)
6890ffe86f4STrond Myklebust {
690a73881c9STrond Myklebust 	return transport->sock->ops->poll(transport->file, transport->sock,
691a73881c9STrond Myklebust 			NULL);
6920ffe86f4STrond Myklebust }
6930ffe86f4STrond Myklebust 
6940ffe86f4STrond Myklebust static bool xs_poll_socket_readable(struct sock_xprt *transport)
6950ffe86f4STrond Myklebust {
6960ffe86f4STrond Myklebust 	__poll_t events = xs_poll_socket(transport);
6970ffe86f4STrond Myklebust 
6980ffe86f4STrond Myklebust 	return (events & (EPOLLIN | EPOLLRDNORM)) && !(events & EPOLLRDHUP);
6990ffe86f4STrond Myklebust }
7000ffe86f4STrond Myklebust 
7010ffe86f4STrond Myklebust static void xs_poll_check_readable(struct sock_xprt *transport)
7020ffe86f4STrond Myklebust {
7030ffe86f4STrond Myklebust 
7040ffe86f4STrond Myklebust 	clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state);
7050ffe86f4STrond Myklebust 	if (!xs_poll_socket_readable(transport))
7060ffe86f4STrond Myklebust 		return;
7070ffe86f4STrond Myklebust 	if (!test_and_set_bit(XPRT_SOCK_DATA_READY, &transport->sock_state))
7080ffe86f4STrond Myklebust 		queue_work(xprtiod_workqueue, &transport->recv_worker);
7090ffe86f4STrond Myklebust }
7100ffe86f4STrond Myklebust 
711c50b8ee0STrond Myklebust static void xs_stream_data_receive(struct sock_xprt *transport)
712c50b8ee0STrond Myklebust {
713c50b8ee0STrond Myklebust 	size_t read = 0;
714c50b8ee0STrond Myklebust 	ssize_t ret = 0;
715c50b8ee0STrond Myklebust 
716c50b8ee0STrond Myklebust 	mutex_lock(&transport->recv_mutex);
717c50b8ee0STrond Myklebust 	if (transport->sock == NULL)
718c50b8ee0STrond Myklebust 		goto out;
719c50b8ee0STrond Myklebust 	for (;;) {
720c50b8ee0STrond Myklebust 		ret = xs_read_stream(transport, MSG_DONTWAIT);
721c4433055STrond Myklebust 		if (ret < 0)
722c50b8ee0STrond Myklebust 			break;
723c50b8ee0STrond Myklebust 		read += ret;
724c50b8ee0STrond Myklebust 		cond_resched();
725c50b8ee0STrond Myklebust 	}
7265f52a9d4STrond Myklebust 	if (ret == -ESHUTDOWN)
7275f52a9d4STrond Myklebust 		kernel_sock_shutdown(transport->sock, SHUT_RDWR);
7285f52a9d4STrond Myklebust 	else
7290ffe86f4STrond Myklebust 		xs_poll_check_readable(transport);
730c50b8ee0STrond Myklebust out:
731c50b8ee0STrond Myklebust 	mutex_unlock(&transport->recv_mutex);
732c50b8ee0STrond Myklebust 	trace_xs_stream_read_data(&transport->xprt, ret, read);
733c50b8ee0STrond Myklebust }
734c50b8ee0STrond Myklebust 
735c50b8ee0STrond Myklebust static void xs_stream_data_receive_workfn(struct work_struct *work)
736c50b8ee0STrond Myklebust {
737c50b8ee0STrond Myklebust 	struct sock_xprt *transport =
738c50b8ee0STrond Myklebust 		container_of(work, struct sock_xprt, recv_worker);
739a1231fdaSTrond Myklebust 	unsigned int pflags = memalloc_nofs_save();
740a1231fdaSTrond Myklebust 
741c50b8ee0STrond Myklebust 	xs_stream_data_receive(transport);
742a1231fdaSTrond Myklebust 	memalloc_nofs_restore(pflags);
743c50b8ee0STrond Myklebust }
744c50b8ee0STrond Myklebust 
745550aebfeSTrond Myklebust static void
746550aebfeSTrond Myklebust xs_stream_reset_connect(struct sock_xprt *transport)
747550aebfeSTrond Myklebust {
748550aebfeSTrond Myklebust 	transport->recv.offset = 0;
749550aebfeSTrond Myklebust 	transport->recv.len = 0;
750550aebfeSTrond Myklebust 	transport->recv.copied = 0;
751550aebfeSTrond Myklebust 	transport->xmit.offset = 0;
752ae053551STrond Myklebust }
753ae053551STrond Myklebust 
754ae053551STrond Myklebust static void
755ae053551STrond Myklebust xs_stream_start_connect(struct sock_xprt *transport)
756ae053551STrond Myklebust {
757550aebfeSTrond Myklebust 	transport->xprt.stat.connect_count++;
758550aebfeSTrond Myklebust 	transport->xprt.stat.connect_start = jiffies;
759550aebfeSTrond Myklebust }
760550aebfeSTrond Myklebust 
761b4b5cc85SChuck Lever #define XS_SENDMSG_FLAGS	(MSG_DONTWAIT | MSG_NOSIGNAL)
762b4b5cc85SChuck Lever 
7639903cd1cSChuck Lever /**
764c544577dSTrond Myklebust  * xs_nospace - handle transmit was incomplete
76550f484e2STrond Myklebust  * @req: pointer to RPC request
7669903cd1cSChuck Lever  *
767a246b010SChuck Lever  */
768c544577dSTrond Myklebust static int xs_nospace(struct rpc_rqst *req)
769a246b010SChuck Lever {
770262965f5SChuck Lever 	struct rpc_xprt *xprt = req->rq_xprt;
771ee0ac0c2SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
77206ea0bfeSTrond Myklebust 	struct sock *sk = transport->inet;
77324ca9a84STrond Myklebust 	int ret = -EAGAIN;
774a246b010SChuck Lever 
775015747d2SChuck Lever 	trace_rpc_socket_nospace(req, transport);
776a246b010SChuck Lever 
777262965f5SChuck Lever 	/* Protect against races with write_space */
778b5e92419STrond Myklebust 	spin_lock(&xprt->transport_lock);
779a246b010SChuck Lever 
780262965f5SChuck Lever 	/* Don't race with disconnect */
781b6ddf64fSTrond Myklebust 	if (xprt_connected(xprt)) {
78213331a55STrond Myklebust 		/* wait for more buffer space */
78306ea0bfeSTrond Myklebust 		sk->sk_write_pending++;
784c544577dSTrond Myklebust 		xprt_wait_for_buffer_space(xprt);
78513331a55STrond Myklebust 	} else
7865e3771ceSTrond Myklebust 		ret = -ENOTCONN;
787a246b010SChuck Lever 
788b5e92419STrond Myklebust 	spin_unlock(&xprt->transport_lock);
78906ea0bfeSTrond Myklebust 
79006ea0bfeSTrond Myklebust 	/* Race breaker in case memory is freed before above code is called */
791d48f9ce7SDavid Vrabel 	if (ret == -EAGAIN) {
792d48f9ce7SDavid Vrabel 		struct socket_wq *wq;
793d48f9ce7SDavid Vrabel 
794d48f9ce7SDavid Vrabel 		rcu_read_lock();
795d48f9ce7SDavid Vrabel 		wq = rcu_dereference(sk->sk_wq);
796d48f9ce7SDavid Vrabel 		set_bit(SOCKWQ_ASYNC_NOSPACE, &wq->flags);
797d48f9ce7SDavid Vrabel 		rcu_read_unlock();
798d48f9ce7SDavid Vrabel 
79906ea0bfeSTrond Myklebust 		sk->sk_write_space(sk);
800d48f9ce7SDavid Vrabel 	}
8015e3771ceSTrond Myklebust 	return ret;
802a246b010SChuck Lever }
803a246b010SChuck Lever 
804277e4ab7STrond Myklebust static void
805277e4ab7STrond Myklebust xs_stream_prepare_request(struct rpc_rqst *req)
806277e4ab7STrond Myklebust {
80775369089STrond Myklebust 	xdr_free_bvec(&req->rq_rcv_buf);
80812a3ad61STrond Myklebust 	req->rq_task->tk_status = xdr_alloc_bvec(&req->rq_rcv_buf, GFP_KERNEL);
809277e4ab7STrond Myklebust }
810277e4ab7STrond Myklebust 
81161677eeeSChuck Lever /*
8124cd34e7cSTrond Myklebust  * Determine if the previous message in the stream was aborted before it
8134cd34e7cSTrond Myklebust  * could complete transmission.
8144cd34e7cSTrond Myklebust  */
8154cd34e7cSTrond Myklebust static bool
8164cd34e7cSTrond Myklebust xs_send_request_was_aborted(struct sock_xprt *transport, struct rpc_rqst *req)
8174cd34e7cSTrond Myklebust {
8184cd34e7cSTrond Myklebust 	return transport->xmit.offset != 0 && req->rq_bytes_sent == 0;
8194cd34e7cSTrond Myklebust }
8204cd34e7cSTrond Myklebust 
8214cd34e7cSTrond Myklebust /*
82206b5fc3aSTrond Myklebust  * Return the stream record marker field for a record of length < 2^31-1
82361677eeeSChuck Lever  */
82406b5fc3aSTrond Myklebust static rpc_fraghdr
82506b5fc3aSTrond Myklebust xs_stream_record_marker(struct xdr_buf *xdr)
82661677eeeSChuck Lever {
82706b5fc3aSTrond Myklebust 	if (!xdr->len)
82806b5fc3aSTrond Myklebust 		return 0;
82906b5fc3aSTrond Myklebust 	return cpu_to_be32(RPC_LAST_STREAM_FRAGMENT | (u32)xdr->len);
83061677eeeSChuck Lever }
83161677eeeSChuck Lever 
8329903cd1cSChuck Lever /**
833176e21eeSChuck Lever  * xs_local_send_request - write an RPC request to an AF_LOCAL socket
83450f484e2STrond Myklebust  * @req: pointer to RPC request
835176e21eeSChuck Lever  *
836176e21eeSChuck Lever  * Return values:
837176e21eeSChuck Lever  *        0:	The request has been sent
838176e21eeSChuck Lever  *   EAGAIN:	The socket was blocked, please call again later to
839176e21eeSChuck Lever  *		complete the request
840176e21eeSChuck Lever  * ENOTCONN:	Caller needs to invoke connect logic then call again
84112b20ce3SBhaskar Chowdhury  *    other:	Some other error occurred, the request was not sent
842176e21eeSChuck Lever  */
843adfa7144STrond Myklebust static int xs_local_send_request(struct rpc_rqst *req)
844176e21eeSChuck Lever {
845176e21eeSChuck Lever 	struct rpc_xprt *xprt = req->rq_xprt;
846176e21eeSChuck Lever 	struct sock_xprt *transport =
847176e21eeSChuck Lever 				container_of(xprt, struct sock_xprt, xprt);
848176e21eeSChuck Lever 	struct xdr_buf *xdr = &req->rq_snd_buf;
8497e3d3620STrond Myklebust 	rpc_fraghdr rm = xs_stream_record_marker(xdr);
8507e3d3620STrond Myklebust 	unsigned int msglen = rm ? req->rq_slen + sizeof(rm) : req->rq_slen;
8519e55eef4SChuck Lever 	struct msghdr msg = {
8529e55eef4SChuck Lever 		.msg_flags	= XS_SENDMSG_FLAGS,
8539e55eef4SChuck Lever 	};
8543f649ab7SKees Cook 	unsigned int sent;
855176e21eeSChuck Lever 	int status;
856176e21eeSChuck Lever 
8574cd34e7cSTrond Myklebust 	/* Close the stream if the previous transmission was incomplete */
8584cd34e7cSTrond Myklebust 	if (xs_send_request_was_aborted(transport, req)) {
8594cd34e7cSTrond Myklebust 		xs_close(xprt);
8604cd34e7cSTrond Myklebust 		return -ENOTCONN;
8614cd34e7cSTrond Myklebust 	}
8624cd34e7cSTrond Myklebust 
863176e21eeSChuck Lever 	xs_pktdump("packet data:",
864176e21eeSChuck Lever 			req->rq_svec->iov_base, req->rq_svec->iov_len);
865176e21eeSChuck Lever 
86678215759SChuck Lever 	req->rq_xtime = ktime_get();
8679e55eef4SChuck Lever 	status = xprt_sock_sendmsg(transport->sock, &msg, xdr,
8687e3d3620STrond Myklebust 				   transport->xmit.offset, rm, &sent);
869176e21eeSChuck Lever 	dprintk("RPC:       %s(%u) = %d\n",
8706c7a64e5STrond Myklebust 			__func__, xdr->len - transport->xmit.offset, status);
871743c69e7SNeilBrown 
872743c69e7SNeilBrown 	if (status == -EAGAIN && sock_writeable(transport->inet))
873743c69e7SNeilBrown 		status = -ENOBUFS;
874743c69e7SNeilBrown 
875f279cd00SJason Baron 	if (likely(sent > 0) || status == 0) {
8766c7a64e5STrond Myklebust 		transport->xmit.offset += sent;
8776c7a64e5STrond Myklebust 		req->rq_bytes_sent = transport->xmit.offset;
8787e3d3620STrond Myklebust 		if (likely(req->rq_bytes_sent >= msglen)) {
8796c7a64e5STrond Myklebust 			req->rq_xmit_bytes_sent += transport->xmit.offset;
8806c7a64e5STrond Myklebust 			transport->xmit.offset = 0;
881176e21eeSChuck Lever 			return 0;
882176e21eeSChuck Lever 		}
883176e21eeSChuck Lever 		status = -EAGAIN;
884176e21eeSChuck Lever 	}
885176e21eeSChuck Lever 
886176e21eeSChuck Lever 	switch (status) {
8873601c4a9STrond Myklebust 	case -ENOBUFS:
888b5872f0cSTrond Myklebust 		break;
889176e21eeSChuck Lever 	case -EAGAIN:
890c544577dSTrond Myklebust 		status = xs_nospace(req);
891176e21eeSChuck Lever 		break;
892176e21eeSChuck Lever 	default:
893176e21eeSChuck Lever 		dprintk("RPC:       sendmsg returned unrecognized error %d\n",
894176e21eeSChuck Lever 			-status);
895df561f66SGustavo A. R. Silva 		fallthrough;
896176e21eeSChuck Lever 	case -EPIPE:
897176e21eeSChuck Lever 		xs_close(xprt);
898176e21eeSChuck Lever 		status = -ENOTCONN;
899176e21eeSChuck Lever 	}
900176e21eeSChuck Lever 
901176e21eeSChuck Lever 	return status;
902176e21eeSChuck Lever }
903176e21eeSChuck Lever 
904176e21eeSChuck Lever /**
905262965f5SChuck Lever  * xs_udp_send_request - write an RPC request to a UDP socket
90650f484e2STrond Myklebust  * @req: pointer to RPC request
9079903cd1cSChuck Lever  *
9089903cd1cSChuck Lever  * Return values:
9099903cd1cSChuck Lever  *        0:	The request has been sent
9109903cd1cSChuck Lever  *   EAGAIN:	The socket was blocked, please call again later to
9119903cd1cSChuck Lever  *		complete the request
912262965f5SChuck Lever  * ENOTCONN:	Caller needs to invoke connect logic then call again
91325985edcSLucas De Marchi  *    other:	Some other error occurred, the request was not sent
9149903cd1cSChuck Lever  */
915adfa7144STrond Myklebust static int xs_udp_send_request(struct rpc_rqst *req)
916a246b010SChuck Lever {
917a246b010SChuck Lever 	struct rpc_xprt *xprt = req->rq_xprt;
918ee0ac0c2SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
919262965f5SChuck Lever 	struct xdr_buf *xdr = &req->rq_snd_buf;
9209e55eef4SChuck Lever 	struct msghdr msg = {
9219e55eef4SChuck Lever 		.msg_name	= xs_addr(xprt),
9229e55eef4SChuck Lever 		.msg_namelen	= xprt->addrlen,
9239e55eef4SChuck Lever 		.msg_flags	= XS_SENDMSG_FLAGS,
9249e55eef4SChuck Lever 	};
9253f649ab7SKees Cook 	unsigned int sent;
926262965f5SChuck Lever 	int status;
927262965f5SChuck Lever 
928262965f5SChuck Lever 	xs_pktdump("packet data:",
929262965f5SChuck Lever 				req->rq_svec->iov_base,
930262965f5SChuck Lever 				req->rq_svec->iov_len);
931262965f5SChuck Lever 
93201d37c42STrond Myklebust 	if (!xprt_bound(xprt))
93301d37c42STrond Myklebust 		return -ENOTCONN;
93475891f50STrond Myklebust 
93575891f50STrond Myklebust 	if (!xprt_request_get_cong(xprt, req))
93675891f50STrond Myklebust 		return -EBADSLT;
93775891f50STrond Myklebust 
93878215759SChuck Lever 	req->rq_xtime = ktime_get();
9399e55eef4SChuck Lever 	status = xprt_sock_sendmsg(transport->sock, &msg, xdr, 0, 0, &sent);
940262965f5SChuck Lever 
941262965f5SChuck Lever 	dprintk("RPC:       xs_udp_send_request(%u) = %d\n",
9426c7a64e5STrond Myklebust 			xdr->len, status);
943262965f5SChuck Lever 
9443dedbb5cSJason Baron 	/* firewall is blocking us, don't return -EAGAIN or we end up looping */
9453dedbb5cSJason Baron 	if (status == -EPERM)
9463dedbb5cSJason Baron 		goto process_status;
9473dedbb5cSJason Baron 
948743c69e7SNeilBrown 	if (status == -EAGAIN && sock_writeable(transport->inet))
949743c69e7SNeilBrown 		status = -ENOBUFS;
950743c69e7SNeilBrown 
951f279cd00SJason Baron 	if (sent > 0 || status == 0) {
952f279cd00SJason Baron 		req->rq_xmit_bytes_sent += sent;
953f279cd00SJason Baron 		if (sent >= req->rq_slen)
954262965f5SChuck Lever 			return 0;
955262965f5SChuck Lever 		/* Still some bytes left; set up for a retry later. */
956262965f5SChuck Lever 		status = -EAGAIN;
9572199700fSTrond Myklebust 	}
958262965f5SChuck Lever 
9593dedbb5cSJason Baron process_status:
960262965f5SChuck Lever 	switch (status) {
961fba91afbSTrond Myklebust 	case -ENOTSOCK:
962fba91afbSTrond Myklebust 		status = -ENOTCONN;
963fba91afbSTrond Myklebust 		/* Should we call xs_close() here? */
964fba91afbSTrond Myklebust 		break;
965b6ddf64fSTrond Myklebust 	case -EAGAIN:
966c544577dSTrond Myklebust 		status = xs_nospace(req);
967b6ddf64fSTrond Myklebust 		break;
968262965f5SChuck Lever 	case -ENETUNREACH:
9693601c4a9STrond Myklebust 	case -ENOBUFS:
970262965f5SChuck Lever 	case -EPIPE:
971262965f5SChuck Lever 	case -ECONNREFUSED:
9723dedbb5cSJason Baron 	case -EPERM:
973262965f5SChuck Lever 		/* When the server has died, an ICMP port unreachable message
974262965f5SChuck Lever 		 * prompts ECONNREFUSED. */
97513331a55STrond Myklebust 		break;
97613331a55STrond Myklebust 	default:
97713331a55STrond Myklebust 		dprintk("RPC:       sendmsg returned unrecognized error %d\n",
97813331a55STrond Myklebust 			-status);
979262965f5SChuck Lever 	}
9805fe46e9dSBian Naimeng 
981262965f5SChuck Lever 	return status;
982262965f5SChuck Lever }
983262965f5SChuck Lever 
984e06799f9STrond Myklebust /**
985262965f5SChuck Lever  * xs_tcp_send_request - write an RPC request to a TCP socket
98650f484e2STrond Myklebust  * @req: pointer to RPC request
987262965f5SChuck Lever  *
988262965f5SChuck Lever  * Return values:
989262965f5SChuck Lever  *        0:	The request has been sent
990262965f5SChuck Lever  *   EAGAIN:	The socket was blocked, please call again later to
991262965f5SChuck Lever  *		complete the request
992262965f5SChuck Lever  * ENOTCONN:	Caller needs to invoke connect logic then call again
99325985edcSLucas De Marchi  *    other:	Some other error occurred, the request was not sent
994262965f5SChuck Lever  *
995262965f5SChuck Lever  * XXX: In the case of soft timeouts, should we eventually give up
996262965f5SChuck Lever  *	if sendmsg is not able to make progress?
997262965f5SChuck Lever  */
998adfa7144STrond Myklebust static int xs_tcp_send_request(struct rpc_rqst *req)
999262965f5SChuck Lever {
1000262965f5SChuck Lever 	struct rpc_xprt *xprt = req->rq_xprt;
1001ee0ac0c2SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1002262965f5SChuck Lever 	struct xdr_buf *xdr = &req->rq_snd_buf;
10037e3d3620STrond Myklebust 	rpc_fraghdr rm = xs_stream_record_marker(xdr);
10047e3d3620STrond Myklebust 	unsigned int msglen = rm ? req->rq_slen + sizeof(rm) : req->rq_slen;
10059e55eef4SChuck Lever 	struct msghdr msg = {
10069e55eef4SChuck Lever 		.msg_flags	= XS_SENDMSG_FLAGS,
10079e55eef4SChuck Lever 	};
10089ffadfbcSTrond Myklebust 	bool vm_wait = false;
10093f649ab7SKees Cook 	unsigned int sent;
1010b595bb15SChuck Lever 	int status;
1011a246b010SChuck Lever 
10124cd34e7cSTrond Myklebust 	/* Close the stream if the previous transmission was incomplete */
10134cd34e7cSTrond Myklebust 	if (xs_send_request_was_aborted(transport, req)) {
10144cd34e7cSTrond Myklebust 		if (transport->sock != NULL)
10154cd34e7cSTrond Myklebust 			kernel_sock_shutdown(transport->sock, SHUT_RDWR);
10164cd34e7cSTrond Myklebust 		return -ENOTCONN;
10174cd34e7cSTrond Myklebust 	}
1018d275880aSTrond Myklebust 	if (!transport->inet)
1019d275880aSTrond Myklebust 		return -ENOTCONN;
10204cd34e7cSTrond Myklebust 
1021262965f5SChuck Lever 	xs_pktdump("packet data:",
1022262965f5SChuck Lever 				req->rq_svec->iov_base,
1023262965f5SChuck Lever 				req->rq_svec->iov_len);
1024a246b010SChuck Lever 
10257196dbb0STrond Myklebust 	if (test_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state))
10267196dbb0STrond Myklebust 		xs_tcp_set_socket_timeouts(xprt, transport->sock);
10277196dbb0STrond Myklebust 
1028a246b010SChuck Lever 	/* Continue transmitting the packet/record. We must be careful
1029a246b010SChuck Lever 	 * to cope with writespace callbacks arriving _after_ we have
1030262965f5SChuck Lever 	 * called sendmsg(). */
103178215759SChuck Lever 	req->rq_xtime = ktime_get();
1032d737e5d4STrond Myklebust 	tcp_sock_set_cork(transport->inet, true);
1033a246b010SChuck Lever 	while (1) {
10349e55eef4SChuck Lever 		status = xprt_sock_sendmsg(transport->sock, &msg, xdr,
10357e3d3620STrond Myklebust 					   transport->xmit.offset, rm, &sent);
1036a246b010SChuck Lever 
1037262965f5SChuck Lever 		dprintk("RPC:       xs_tcp_send_request(%u) = %d\n",
10386c7a64e5STrond Myklebust 				xdr->len - transport->xmit.offset, status);
1039262965f5SChuck Lever 
1040a246b010SChuck Lever 		/* If we've sent the entire packet, immediately
1041a246b010SChuck Lever 		 * reset the count of bytes sent. */
10426c7a64e5STrond Myklebust 		transport->xmit.offset += sent;
10436c7a64e5STrond Myklebust 		req->rq_bytes_sent = transport->xmit.offset;
10447e3d3620STrond Myklebust 		if (likely(req->rq_bytes_sent >= msglen)) {
10456c7a64e5STrond Myklebust 			req->rq_xmit_bytes_sent += transport->xmit.offset;
10466c7a64e5STrond Myklebust 			transport->xmit.offset = 0;
1047d737e5d4STrond Myklebust 			if (atomic_long_read(&xprt->xmit_queuelen) == 1)
1048d737e5d4STrond Myklebust 				tcp_sock_set_cork(transport->inet, false);
1049a246b010SChuck Lever 			return 0;
1050a246b010SChuck Lever 		}
1051262965f5SChuck Lever 
10529ffadfbcSTrond Myklebust 		WARN_ON_ONCE(sent == 0 && status == 0);
10539ffadfbcSTrond Myklebust 
10549ffadfbcSTrond Myklebust 		if (status == -EAGAIN ) {
10559ffadfbcSTrond Myklebust 			/*
10569ffadfbcSTrond Myklebust 			 * Return EAGAIN if we're sure we're hitting the
10579ffadfbcSTrond Myklebust 			 * socket send buffer limits.
10589ffadfbcSTrond Myklebust 			 */
10599ffadfbcSTrond Myklebust 			if (test_bit(SOCK_NOSPACE, &transport->sock->flags))
10609ffadfbcSTrond Myklebust 				break;
10619ffadfbcSTrond Myklebust 			/*
10629ffadfbcSTrond Myklebust 			 * Did we hit a memory allocation failure?
10639ffadfbcSTrond Myklebust 			 */
10649ffadfbcSTrond Myklebust 			if (sent == 0) {
10659ffadfbcSTrond Myklebust 				status = -ENOBUFS;
10669ffadfbcSTrond Myklebust 				if (vm_wait)
10679ffadfbcSTrond Myklebust 					break;
10689ffadfbcSTrond Myklebust 				/* Retry, knowing now that we're below the
10699ffadfbcSTrond Myklebust 				 * socket send buffer limit
10709ffadfbcSTrond Myklebust 				 */
10719ffadfbcSTrond Myklebust 				vm_wait = true;
10729ffadfbcSTrond Myklebust 			}
10739ffadfbcSTrond Myklebust 			continue;
10749ffadfbcSTrond Myklebust 		}
1075f580dd04STrond Myklebust 		if (status < 0)
1076f580dd04STrond Myklebust 			break;
10779ffadfbcSTrond Myklebust 		vm_wait = false;
1078a246b010SChuck Lever 	}
1079a246b010SChuck Lever 
1080262965f5SChuck Lever 	switch (status) {
1081fba91afbSTrond Myklebust 	case -ENOTSOCK:
1082fba91afbSTrond Myklebust 		status = -ENOTCONN;
1083fba91afbSTrond Myklebust 		/* Should we call xs_close() here? */
1084fba91afbSTrond Myklebust 		break;
1085262965f5SChuck Lever 	case -EAGAIN:
1086c544577dSTrond Myklebust 		status = xs_nospace(req);
1087262965f5SChuck Lever 		break;
1088262965f5SChuck Lever 	case -ECONNRESET:
1089262965f5SChuck Lever 	case -ECONNREFUSED:
1090262965f5SChuck Lever 	case -ENOTCONN:
10913913c78cSTrond Myklebust 	case -EADDRINUSE:
1092b5872f0cSTrond Myklebust 	case -ENOBUFS:
1093b9d2bb2eSTrond Myklebust 	case -EPIPE:
109413331a55STrond Myklebust 		break;
109513331a55STrond Myklebust 	default:
109613331a55STrond Myklebust 		dprintk("RPC:       sendmsg returned unrecognized error %d\n",
109713331a55STrond Myklebust 			-status);
1098a246b010SChuck Lever 	}
10995fe46e9dSBian Naimeng 
1100a246b010SChuck Lever 	return status;
1101a246b010SChuck Lever }
1102a246b010SChuck Lever 
11032a9e1cfaSTrond Myklebust static void xs_save_old_callbacks(struct sock_xprt *transport, struct sock *sk)
11042a9e1cfaSTrond Myklebust {
11052a9e1cfaSTrond Myklebust 	transport->old_data_ready = sk->sk_data_ready;
11062a9e1cfaSTrond Myklebust 	transport->old_state_change = sk->sk_state_change;
11072a9e1cfaSTrond Myklebust 	transport->old_write_space = sk->sk_write_space;
11082118071dSTrond Myklebust 	transport->old_error_report = sk->sk_error_report;
11092a9e1cfaSTrond Myklebust }
11102a9e1cfaSTrond Myklebust 
11112a9e1cfaSTrond Myklebust static void xs_restore_old_callbacks(struct sock_xprt *transport, struct sock *sk)
11122a9e1cfaSTrond Myklebust {
11132a9e1cfaSTrond Myklebust 	sk->sk_data_ready = transport->old_data_ready;
11142a9e1cfaSTrond Myklebust 	sk->sk_state_change = transport->old_state_change;
11152a9e1cfaSTrond Myklebust 	sk->sk_write_space = transport->old_write_space;
11162118071dSTrond Myklebust 	sk->sk_error_report = transport->old_error_report;
11172118071dSTrond Myklebust }
11182118071dSTrond Myklebust 
111942d42a5bSTrond Myklebust static void xs_sock_reset_state_flags(struct rpc_xprt *xprt)
112042d42a5bSTrond Myklebust {
112142d42a5bSTrond Myklebust 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
112242d42a5bSTrond Myklebust 
112342d42a5bSTrond Myklebust 	clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state);
11244f8943f8STrond Myklebust 	clear_bit(XPRT_SOCK_WAKE_ERROR, &transport->sock_state);
11254f8943f8STrond Myklebust 	clear_bit(XPRT_SOCK_WAKE_WRITE, &transport->sock_state);
11264f8943f8STrond Myklebust 	clear_bit(XPRT_SOCK_WAKE_DISCONNECT, &transport->sock_state);
11274f8943f8STrond Myklebust }
11284f8943f8STrond Myklebust 
11294f8943f8STrond Myklebust static void xs_run_error_worker(struct sock_xprt *transport, unsigned int nr)
11304f8943f8STrond Myklebust {
11314f8943f8STrond Myklebust 	set_bit(nr, &transport->sock_state);
11324f8943f8STrond Myklebust 	queue_work(xprtiod_workqueue, &transport->error_worker);
113342d42a5bSTrond Myklebust }
113442d42a5bSTrond Myklebust 
1135b70ae915STrond Myklebust static void xs_sock_reset_connection_flags(struct rpc_xprt *xprt)
1136b70ae915STrond Myklebust {
1137b70ae915STrond Myklebust 	smp_mb__before_atomic();
1138b70ae915STrond Myklebust 	clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
1139b70ae915STrond Myklebust 	clear_bit(XPRT_CLOSING, &xprt->state);
114042d42a5bSTrond Myklebust 	xs_sock_reset_state_flags(xprt);
1141b70ae915STrond Myklebust 	smp_mb__after_atomic();
1142b70ae915STrond Myklebust }
1143b70ae915STrond Myklebust 
11442118071dSTrond Myklebust /**
11452118071dSTrond Myklebust  * xs_error_report - callback to handle TCP socket state errors
11462118071dSTrond Myklebust  * @sk: socket
11472118071dSTrond Myklebust  *
11482118071dSTrond Myklebust  * Note: we don't call sock_error() since there may be a rpc_task
11492118071dSTrond Myklebust  * using the socket, and so we don't want to clear sk->sk_err.
11502118071dSTrond Myklebust  */
11512118071dSTrond Myklebust static void xs_error_report(struct sock *sk)
11522118071dSTrond Myklebust {
11534f8943f8STrond Myklebust 	struct sock_xprt *transport;
11542118071dSTrond Myklebust 	struct rpc_xprt *xprt;
11552118071dSTrond Myklebust 
11562118071dSTrond Myklebust 	read_lock_bh(&sk->sk_callback_lock);
11572118071dSTrond Myklebust 	if (!(xprt = xprt_from_sock(sk)))
11582118071dSTrond Myklebust 		goto out;
11592118071dSTrond Myklebust 
11604f8943f8STrond Myklebust 	transport = container_of(xprt, struct sock_xprt, xprt);
1161af84537dSBenjamin Coddington 	transport->xprt_err = -sk->sk_err;
1162af84537dSBenjamin Coddington 	if (transport->xprt_err == 0)
11632118071dSTrond Myklebust 		goto out;
11642118071dSTrond Myklebust 	dprintk("RPC:       xs_error_report client %p, error=%d...\n",
1165af84537dSBenjamin Coddington 			xprt, -transport->xprt_err);
1166af84537dSBenjamin Coddington 	trace_rpc_socket_error(xprt, sk->sk_socket, transport->xprt_err);
1167af84537dSBenjamin Coddington 
1168af84537dSBenjamin Coddington 	/* barrier ensures xprt_err is set before XPRT_SOCK_WAKE_ERROR */
1169af84537dSBenjamin Coddington 	smp_mb__before_atomic();
11704f8943f8STrond Myklebust 	xs_run_error_worker(transport, XPRT_SOCK_WAKE_ERROR);
11712118071dSTrond Myklebust  out:
11722118071dSTrond Myklebust 	read_unlock_bh(&sk->sk_callback_lock);
11732a9e1cfaSTrond Myklebust }
11742a9e1cfaSTrond Myklebust 
1175fe315e76SChuck Lever static void xs_reset_transport(struct sock_xprt *transport)
1176a246b010SChuck Lever {
1177ee0ac0c2SChuck Lever 	struct socket *sock = transport->sock;
1178ee0ac0c2SChuck Lever 	struct sock *sk = transport->inet;
11796cc7e908STrond Myklebust 	struct rpc_xprt *xprt = &transport->xprt;
1180a73881c9STrond Myklebust 	struct file *filp = transport->file;
1181a246b010SChuck Lever 
1182fe315e76SChuck Lever 	if (sk == NULL)
1183fe315e76SChuck Lever 		return;
11849903cd1cSChuck Lever 
1185264d1df3SJeff Layton 	if (atomic_read(&transport->xprt.swapper))
1186264d1df3SJeff Layton 		sk_clear_memalloc(sk);
1187264d1df3SJeff Layton 
118809939204STrond Myklebust 	kernel_sock_shutdown(sock, SHUT_RDWR);
118909939204STrond Myklebust 
1190edc1b01cSTrond Myklebust 	mutex_lock(&transport->recv_mutex);
1191a246b010SChuck Lever 	write_lock_bh(&sk->sk_callback_lock);
1192ee0ac0c2SChuck Lever 	transport->inet = NULL;
1193ee0ac0c2SChuck Lever 	transport->sock = NULL;
1194a73881c9STrond Myklebust 	transport->file = NULL;
1195a246b010SChuck Lever 
1196a246b010SChuck Lever 	sk->sk_user_data = NULL;
11972a9e1cfaSTrond Myklebust 
11982a9e1cfaSTrond Myklebust 	xs_restore_old_callbacks(transport, sk);
11990c78789eSTrond Myklebust 	xprt_clear_connected(xprt);
1200a246b010SChuck Lever 	write_unlock_bh(&sk->sk_callback_lock);
12016cc7e908STrond Myklebust 	xs_sock_reset_connection_flags(xprt);
1202ae053551STrond Myklebust 	/* Reset stream record info */
1203ae053551STrond Myklebust 	xs_stream_reset_connect(transport);
1204edc1b01cSTrond Myklebust 	mutex_unlock(&transport->recv_mutex);
1205a246b010SChuck Lever 
12066cc7e908STrond Myklebust 	trace_rpc_socket_close(xprt, sock);
1207a73881c9STrond Myklebust 	fput(filp);
12080445f92cSTrond Myklebust 
12090445f92cSTrond Myklebust 	xprt_disconnect_done(xprt);
1210fe315e76SChuck Lever }
1211fe315e76SChuck Lever 
1212fe315e76SChuck Lever /**
1213fe315e76SChuck Lever  * xs_close - close a socket
1214fe315e76SChuck Lever  * @xprt: transport
1215fe315e76SChuck Lever  *
1216fe315e76SChuck Lever  * This is used when all requests are complete; ie, no DRC state remains
1217fe315e76SChuck Lever  * on the server we want to save.
1218f75e6745STrond Myklebust  *
1219f75e6745STrond Myklebust  * The caller _must_ be holding XPRT_LOCKED in order to avoid issues with
1220f75e6745STrond Myklebust  * xs_reset_transport() zeroing the socket from underneath a writer.
1221fe315e76SChuck Lever  */
1222fe315e76SChuck Lever static void xs_close(struct rpc_xprt *xprt)
1223fe315e76SChuck Lever {
1224fe315e76SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1225fe315e76SChuck Lever 
1226fe315e76SChuck Lever 	dprintk("RPC:       xs_close xprt %p\n", xprt);
1227fe315e76SChuck Lever 
1228fe315e76SChuck Lever 	xs_reset_transport(transport);
122961d0a8e6SNeil Brown 	xprt->reestablish_timeout = 0;
1230a246b010SChuck Lever }
1231a246b010SChuck Lever 
12324a068258SChuck Lever static void xs_inject_disconnect(struct rpc_xprt *xprt)
12334a068258SChuck Lever {
12344a068258SChuck Lever 	dprintk("RPC:       injecting transport disconnect on xprt=%p\n",
12354a068258SChuck Lever 		xprt);
12364a068258SChuck Lever 	xprt_disconnect_done(xprt);
12374a068258SChuck Lever }
12384a068258SChuck Lever 
1239315f3812SKinglong Mee static void xs_xprt_free(struct rpc_xprt *xprt)
1240315f3812SKinglong Mee {
1241315f3812SKinglong Mee 	xs_free_peer_addresses(xprt);
1242315f3812SKinglong Mee 	xprt_free(xprt);
1243315f3812SKinglong Mee }
1244315f3812SKinglong Mee 
12459903cd1cSChuck Lever /**
12469903cd1cSChuck Lever  * xs_destroy - prepare to shutdown a transport
12479903cd1cSChuck Lever  * @xprt: doomed transport
12489903cd1cSChuck Lever  *
12499903cd1cSChuck Lever  */
12509903cd1cSChuck Lever static void xs_destroy(struct rpc_xprt *xprt)
1251a246b010SChuck Lever {
125203c78827STrond Myklebust 	struct sock_xprt *transport = container_of(xprt,
125303c78827STrond Myklebust 			struct sock_xprt, xprt);
12549903cd1cSChuck Lever 	dprintk("RPC:       xs_destroy xprt %p\n", xprt);
12559903cd1cSChuck Lever 
125603c78827STrond Myklebust 	cancel_delayed_work_sync(&transport->connect_worker);
1257a1311d87STrond Myklebust 	xs_close(xprt);
1258edc1b01cSTrond Myklebust 	cancel_work_sync(&transport->recv_worker);
1259b5e92419STrond Myklebust 	cancel_work_sync(&transport->error_worker);
1260315f3812SKinglong Mee 	xs_xprt_free(xprt);
1261a1311d87STrond Myklebust 	module_put(THIS_MODULE);
1262a246b010SChuck Lever }
1263a246b010SChuck Lever 
12649903cd1cSChuck Lever /**
1265f9b2ee71STrond Myklebust  * xs_udp_data_read_skb - receive callback for UDP sockets
1266f9b2ee71STrond Myklebust  * @xprt: transport
1267f9b2ee71STrond Myklebust  * @sk: socket
1268f9b2ee71STrond Myklebust  * @skb: skbuff
12699903cd1cSChuck Lever  *
1270a246b010SChuck Lever  */
1271f9b2ee71STrond Myklebust static void xs_udp_data_read_skb(struct rpc_xprt *xprt,
1272f9b2ee71STrond Myklebust 		struct sock *sk,
1273f9b2ee71STrond Myklebust 		struct sk_buff *skb)
1274a246b010SChuck Lever {
1275a246b010SChuck Lever 	struct rpc_task *task;
1276a246b010SChuck Lever 	struct rpc_rqst *rovr;
1277f9b2ee71STrond Myklebust 	int repsize, copied;
1278d8ed029dSAlexey Dobriyan 	u32 _xid;
1279d8ed029dSAlexey Dobriyan 	__be32 *xp;
1280a246b010SChuck Lever 
12811da8c681SWillem de Bruijn 	repsize = skb->len;
1282a246b010SChuck Lever 	if (repsize < 4) {
12839903cd1cSChuck Lever 		dprintk("RPC:       impossible RPC reply size %d!\n", repsize);
1284f9b2ee71STrond Myklebust 		return;
1285a246b010SChuck Lever 	}
1286a246b010SChuck Lever 
1287a246b010SChuck Lever 	/* Copy the XID from the skb... */
12881da8c681SWillem de Bruijn 	xp = skb_header_pointer(skb, 0, sizeof(_xid), &_xid);
1289a246b010SChuck Lever 	if (xp == NULL)
1290f9b2ee71STrond Myklebust 		return;
1291a246b010SChuck Lever 
1292a246b010SChuck Lever 	/* Look up and lock the request corresponding to the given XID */
129375c84151STrond Myklebust 	spin_lock(&xprt->queue_lock);
1294a246b010SChuck Lever 	rovr = xprt_lookup_rqst(xprt, *xp);
1295a246b010SChuck Lever 	if (!rovr)
1296a246b010SChuck Lever 		goto out_unlock;
1297729749bbSTrond Myklebust 	xprt_pin_rqst(rovr);
1298ecd465eeSChuck Lever 	xprt_update_rtt(rovr->rq_task);
129975c84151STrond Myklebust 	spin_unlock(&xprt->queue_lock);
1300a246b010SChuck Lever 	task = rovr->rq_task;
1301a246b010SChuck Lever 
1302a246b010SChuck Lever 	if ((copied = rovr->rq_private_buf.buflen) > repsize)
1303a246b010SChuck Lever 		copied = repsize;
1304a246b010SChuck Lever 
1305a246b010SChuck Lever 	/* Suck it into the iovec, verify checksum if not done by hw. */
13061781f7f5SHerbert Xu 	if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) {
130775c84151STrond Myklebust 		spin_lock(&xprt->queue_lock);
13080afa6b44STrond Myklebust 		__UDPX_INC_STATS(sk, UDP_MIB_INERRORS);
1309729749bbSTrond Myklebust 		goto out_unpin;
13101781f7f5SHerbert Xu 	}
13111781f7f5SHerbert Xu 
1312a246b010SChuck Lever 
1313b5e92419STrond Myklebust 	spin_lock(&xprt->transport_lock);
13146a24dfb6STrond Myklebust 	xprt_adjust_cwnd(xprt, task, copied);
1315b5e92419STrond Myklebust 	spin_unlock(&xprt->transport_lock);
131675c84151STrond Myklebust 	spin_lock(&xprt->queue_lock);
13171570c1e4SChuck Lever 	xprt_complete_rqst(task, copied);
13180afa6b44STrond Myklebust 	__UDPX_INC_STATS(sk, UDP_MIB_INDATAGRAMS);
1319729749bbSTrond Myklebust out_unpin:
1320729749bbSTrond Myklebust 	xprt_unpin_rqst(rovr);
1321a246b010SChuck Lever  out_unlock:
132275c84151STrond Myklebust 	spin_unlock(&xprt->queue_lock);
1323f9b2ee71STrond Myklebust }
1324f9b2ee71STrond Myklebust 
1325f9b2ee71STrond Myklebust static void xs_udp_data_receive(struct sock_xprt *transport)
1326f9b2ee71STrond Myklebust {
1327f9b2ee71STrond Myklebust 	struct sk_buff *skb;
1328f9b2ee71STrond Myklebust 	struct sock *sk;
1329f9b2ee71STrond Myklebust 	int err;
1330f9b2ee71STrond Myklebust 
1331f9b2ee71STrond Myklebust 	mutex_lock(&transport->recv_mutex);
1332f9b2ee71STrond Myklebust 	sk = transport->inet;
1333f9b2ee71STrond Myklebust 	if (sk == NULL)
1334f9b2ee71STrond Myklebust 		goto out;
1335f9b2ee71STrond Myklebust 	for (;;) {
13367c13f97fSPaolo Abeni 		skb = skb_recv_udp(sk, 0, 1, &err);
13374f546149STrond Myklebust 		if (skb == NULL)
13384f546149STrond Myklebust 			break;
1339f9b2ee71STrond Myklebust 		xs_udp_data_read_skb(&transport->xprt, sk, skb);
1340850cbaddSPaolo Abeni 		consume_skb(skb);
13410af3442aSTrond Myklebust 		cond_resched();
1342f9b2ee71STrond Myklebust 	}
13430ffe86f4STrond Myklebust 	xs_poll_check_readable(transport);
1344a246b010SChuck Lever out:
1345f9b2ee71STrond Myklebust 	mutex_unlock(&transport->recv_mutex);
1346f9b2ee71STrond Myklebust }
1347f9b2ee71STrond Myklebust 
1348f9b2ee71STrond Myklebust static void xs_udp_data_receive_workfn(struct work_struct *work)
1349f9b2ee71STrond Myklebust {
1350f9b2ee71STrond Myklebust 	struct sock_xprt *transport =
1351f9b2ee71STrond Myklebust 		container_of(work, struct sock_xprt, recv_worker);
1352a1231fdaSTrond Myklebust 	unsigned int pflags = memalloc_nofs_save();
1353a1231fdaSTrond Myklebust 
1354f9b2ee71STrond Myklebust 	xs_udp_data_receive(transport);
1355a1231fdaSTrond Myklebust 	memalloc_nofs_restore(pflags);
1356f9b2ee71STrond Myklebust }
1357f9b2ee71STrond Myklebust 
1358f9b2ee71STrond Myklebust /**
1359f9b2ee71STrond Myklebust  * xs_data_ready - "data ready" callback for UDP sockets
1360f9b2ee71STrond Myklebust  * @sk: socket with data to read
1361f9b2ee71STrond Myklebust  *
1362f9b2ee71STrond Myklebust  */
1363f9b2ee71STrond Myklebust static void xs_data_ready(struct sock *sk)
1364f9b2ee71STrond Myklebust {
1365f9b2ee71STrond Myklebust 	struct rpc_xprt *xprt;
1366f9b2ee71STrond Myklebust 
1367f9b2ee71STrond Myklebust 	read_lock_bh(&sk->sk_callback_lock);
1368f9b2ee71STrond Myklebust 	dprintk("RPC:       xs_data_ready...\n");
1369f9b2ee71STrond Myklebust 	xprt = xprt_from_sock(sk);
1370f9b2ee71STrond Myklebust 	if (xprt != NULL) {
1371f9b2ee71STrond Myklebust 		struct sock_xprt *transport = container_of(xprt,
1372f9b2ee71STrond Myklebust 				struct sock_xprt, xprt);
13735157b956STrond Myklebust 		transport->old_data_ready(sk);
13745157b956STrond Myklebust 		/* Any data means we had a useful conversation, so
13755157b956STrond Myklebust 		 * then we don't need to delay the next reconnect
13765157b956STrond Myklebust 		 */
13775157b956STrond Myklebust 		if (xprt->reestablish_timeout)
13785157b956STrond Myklebust 			xprt->reestablish_timeout = 0;
137942d42a5bSTrond Myklebust 		if (!test_and_set_bit(XPRT_SOCK_DATA_READY, &transport->sock_state))
138040a5f1b1STrond Myklebust 			queue_work(xprtiod_workqueue, &transport->recv_worker);
1381f9b2ee71STrond Myklebust 	}
1382f064af1eSEric Dumazet 	read_unlock_bh(&sk->sk_callback_lock);
1383a246b010SChuck Lever }
1384a246b010SChuck Lever 
1385a519fc7aSTrond Myklebust /*
1386a519fc7aSTrond Myklebust  * Helper function to force a TCP close if the server is sending
1387a519fc7aSTrond Myklebust  * junk and/or it has put us in CLOSE_WAIT
1388a519fc7aSTrond Myklebust  */
1389a519fc7aSTrond Myklebust static void xs_tcp_force_close(struct rpc_xprt *xprt)
1390a519fc7aSTrond Myklebust {
1391a519fc7aSTrond Myklebust 	xprt_force_disconnect(xprt);
1392a519fc7aSTrond Myklebust }
1393a519fc7aSTrond Myklebust 
13949e00abc3STrond Myklebust #if defined(CONFIG_SUNRPC_BACKCHANNEL)
13956b26cc8cSChuck Lever static size_t xs_tcp_bc_maxpayload(struct rpc_xprt *xprt)
13966b26cc8cSChuck Lever {
13976b26cc8cSChuck Lever 	return PAGE_SIZE;
13986b26cc8cSChuck Lever }
13999e00abc3STrond Myklebust #endif /* CONFIG_SUNRPC_BACKCHANNEL */
140044b98efdSRicardo Labiaga 
14019903cd1cSChuck Lever /**
14029903cd1cSChuck Lever  * xs_tcp_state_change - callback to handle TCP socket state changes
14039903cd1cSChuck Lever  * @sk: socket whose state has changed
14049903cd1cSChuck Lever  *
14059903cd1cSChuck Lever  */
14069903cd1cSChuck Lever static void xs_tcp_state_change(struct sock *sk)
1407a246b010SChuck Lever {
1408a246b010SChuck Lever 	struct rpc_xprt *xprt;
14090fdea1e8STrond Myklebust 	struct sock_xprt *transport;
1410a246b010SChuck Lever 
1411f064af1eSEric Dumazet 	read_lock_bh(&sk->sk_callback_lock);
1412a246b010SChuck Lever 	if (!(xprt = xprt_from_sock(sk)))
1413a246b010SChuck Lever 		goto out;
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  out:
1475f064af1eSEric Dumazet 	read_unlock_bh(&sk->sk_callback_lock);
1476a246b010SChuck Lever }
1477a246b010SChuck Lever 
14781f0fa154SIlpo Järvinen static void xs_write_space(struct sock *sk)
14791f0fa154SIlpo Järvinen {
148013331a55STrond Myklebust 	struct socket_wq *wq;
14814f8943f8STrond Myklebust 	struct sock_xprt *transport;
14821f0fa154SIlpo Järvinen 	struct rpc_xprt *xprt;
14831f0fa154SIlpo Järvinen 
148413331a55STrond Myklebust 	if (!sk->sk_socket)
14851f0fa154SIlpo Järvinen 		return;
148613331a55STrond Myklebust 	clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
14871f0fa154SIlpo Järvinen 
14881f0fa154SIlpo Järvinen 	if (unlikely(!(xprt = xprt_from_sock(sk))))
14891f0fa154SIlpo Järvinen 		return;
14904f8943f8STrond Myklebust 	transport = container_of(xprt, struct sock_xprt, xprt);
149113331a55STrond Myklebust 	rcu_read_lock();
149213331a55STrond Myklebust 	wq = rcu_dereference(sk->sk_wq);
149313331a55STrond Myklebust 	if (!wq || test_and_clear_bit(SOCKWQ_ASYNC_NOSPACE, &wq->flags) == 0)
149413331a55STrond Myklebust 		goto out;
14951f0fa154SIlpo Järvinen 
14964f8943f8STrond Myklebust 	xs_run_error_worker(transport, XPRT_SOCK_WAKE_WRITE);
1497c544577dSTrond Myklebust 	sk->sk_write_pending--;
149813331a55STrond Myklebust out:
149913331a55STrond Myklebust 	rcu_read_unlock();
15001f0fa154SIlpo Järvinen }
15011f0fa154SIlpo Järvinen 
15022a9e1cfaSTrond Myklebust /**
1503c7b2cae8SChuck Lever  * xs_udp_write_space - callback invoked when socket buffer space
1504c7b2cae8SChuck Lever  *                             becomes available
15059903cd1cSChuck Lever  * @sk: socket whose state has changed
15069903cd1cSChuck Lever  *
1507a246b010SChuck Lever  * Called when more output buffer space is available for this socket.
1508a246b010SChuck Lever  * We try not to wake our writers until they can make "significant"
1509c7b2cae8SChuck Lever  * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1510a246b010SChuck Lever  * with a bunch of small requests.
1511a246b010SChuck Lever  */
1512c7b2cae8SChuck Lever static void xs_udp_write_space(struct sock *sk)
1513a246b010SChuck Lever {
1514f064af1eSEric Dumazet 	read_lock_bh(&sk->sk_callback_lock);
1515c7b2cae8SChuck Lever 
1516c7b2cae8SChuck Lever 	/* from net/core/sock.c:sock_def_write_space */
15171f0fa154SIlpo Järvinen 	if (sock_writeable(sk))
15181f0fa154SIlpo Järvinen 		xs_write_space(sk);
1519c7b2cae8SChuck Lever 
1520f064af1eSEric Dumazet 	read_unlock_bh(&sk->sk_callback_lock);
1521c7b2cae8SChuck Lever }
1522c7b2cae8SChuck Lever 
1523c7b2cae8SChuck Lever /**
1524c7b2cae8SChuck Lever  * xs_tcp_write_space - callback invoked when socket buffer space
1525c7b2cae8SChuck Lever  *                             becomes available
1526c7b2cae8SChuck Lever  * @sk: socket whose state has changed
1527c7b2cae8SChuck Lever  *
1528c7b2cae8SChuck Lever  * Called when more output buffer space is available for this socket.
1529c7b2cae8SChuck Lever  * We try not to wake our writers until they can make "significant"
1530c7b2cae8SChuck Lever  * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1531c7b2cae8SChuck Lever  * with a bunch of small requests.
1532c7b2cae8SChuck Lever  */
1533c7b2cae8SChuck Lever static void xs_tcp_write_space(struct sock *sk)
1534c7b2cae8SChuck Lever {
1535f064af1eSEric Dumazet 	read_lock_bh(&sk->sk_callback_lock);
1536c7b2cae8SChuck Lever 
1537c7b2cae8SChuck Lever 	/* from net/core/stream.c:sk_stream_write_space */
153864dc6130SEric Dumazet 	if (sk_stream_is_writeable(sk))
15391f0fa154SIlpo Järvinen 		xs_write_space(sk);
1540c7b2cae8SChuck Lever 
1541f064af1eSEric Dumazet 	read_unlock_bh(&sk->sk_callback_lock);
1542a246b010SChuck Lever }
1543a246b010SChuck Lever 
1544470056c2SChuck Lever static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt)
1545a246b010SChuck Lever {
1546ee0ac0c2SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1547ee0ac0c2SChuck Lever 	struct sock *sk = transport->inet;
1548a246b010SChuck Lever 
15497c6e066eSChuck Lever 	if (transport->rcvsize) {
1550a246b010SChuck Lever 		sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
15517c6e066eSChuck Lever 		sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2;
1552a246b010SChuck Lever 	}
15537c6e066eSChuck Lever 	if (transport->sndsize) {
1554a246b010SChuck Lever 		sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
15557c6e066eSChuck Lever 		sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2;
1556a246b010SChuck Lever 		sk->sk_write_space(sk);
1557a246b010SChuck Lever 	}
1558a246b010SChuck Lever }
1559a246b010SChuck Lever 
156043118c29SChuck Lever /**
1561470056c2SChuck Lever  * xs_udp_set_buffer_size - set send and receive limits
156243118c29SChuck Lever  * @xprt: generic transport
1563470056c2SChuck Lever  * @sndsize: requested size of send buffer, in bytes
1564470056c2SChuck Lever  * @rcvsize: requested size of receive buffer, in bytes
156543118c29SChuck Lever  *
1566470056c2SChuck Lever  * Set socket send and receive buffer size limits.
156743118c29SChuck Lever  */
1568470056c2SChuck Lever static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize)
156943118c29SChuck Lever {
15707c6e066eSChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
15717c6e066eSChuck Lever 
15727c6e066eSChuck Lever 	transport->sndsize = 0;
1573470056c2SChuck Lever 	if (sndsize)
15747c6e066eSChuck Lever 		transport->sndsize = sndsize + 1024;
15757c6e066eSChuck Lever 	transport->rcvsize = 0;
1576470056c2SChuck Lever 	if (rcvsize)
15777c6e066eSChuck Lever 		transport->rcvsize = rcvsize + 1024;
1578470056c2SChuck Lever 
1579470056c2SChuck Lever 	xs_udp_do_set_buffer_size(xprt);
158043118c29SChuck Lever }
158143118c29SChuck Lever 
158246c0ee8bSChuck Lever /**
158346c0ee8bSChuck Lever  * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
1584acf0a39fSChuck Lever  * @xprt: controlling transport
158546c0ee8bSChuck Lever  * @task: task that timed out
158646c0ee8bSChuck Lever  *
158746c0ee8bSChuck Lever  * Adjust the congestion window after a retransmit timeout has occurred.
158846c0ee8bSChuck Lever  */
15896a24dfb6STrond Myklebust static void xs_udp_timer(struct rpc_xprt *xprt, struct rpc_task *task)
159046c0ee8bSChuck Lever {
1591b5e92419STrond Myklebust 	spin_lock(&xprt->transport_lock);
15926a24dfb6STrond Myklebust 	xprt_adjust_cwnd(xprt, task, -ETIMEDOUT);
1593b5e92419STrond Myklebust 	spin_unlock(&xprt->transport_lock);
159446c0ee8bSChuck Lever }
159546c0ee8bSChuck Lever 
1596826799e6SJ. Bruce Fields static int xs_get_random_port(void)
1597b85d8806SChuck Lever {
1598826799e6SJ. Bruce Fields 	unsigned short min = xprt_min_resvport, max = xprt_max_resvport;
1599826799e6SJ. Bruce Fields 	unsigned short range;
1600826799e6SJ. Bruce Fields 	unsigned short rand;
1601826799e6SJ. Bruce Fields 
1602826799e6SJ. Bruce Fields 	if (max < min)
1603826799e6SJ. Bruce Fields 		return -EADDRINUSE;
1604826799e6SJ. Bruce Fields 	range = max - min + 1;
1605826799e6SJ. Bruce Fields 	rand = (unsigned short) prandom_u32() % range;
1606826799e6SJ. Bruce Fields 	return rand + min;
1607b85d8806SChuck Lever }
1608b85d8806SChuck Lever 
16094dda9c8aSTrond Myklebust static unsigned short xs_sock_getport(struct socket *sock)
16104dda9c8aSTrond Myklebust {
16114dda9c8aSTrond Myklebust 	struct sockaddr_storage buf;
16124dda9c8aSTrond Myklebust 	unsigned short port = 0;
16134dda9c8aSTrond Myklebust 
16149b2c45d4SDenys Vlasenko 	if (kernel_getsockname(sock, (struct sockaddr *)&buf) < 0)
16154dda9c8aSTrond Myklebust 		goto out;
16164dda9c8aSTrond Myklebust 	switch (buf.ss_family) {
16174dda9c8aSTrond Myklebust 	case AF_INET6:
16184dda9c8aSTrond Myklebust 		port = ntohs(((struct sockaddr_in6 *)&buf)->sin6_port);
16194dda9c8aSTrond Myklebust 		break;
16204dda9c8aSTrond Myklebust 	case AF_INET:
16214dda9c8aSTrond Myklebust 		port = ntohs(((struct sockaddr_in *)&buf)->sin_port);
16224dda9c8aSTrond Myklebust 	}
16234dda9c8aSTrond Myklebust out:
16244dda9c8aSTrond Myklebust 	return port;
16254dda9c8aSTrond Myklebust }
16264dda9c8aSTrond Myklebust 
16274dda9c8aSTrond Myklebust /**
162892200412SChuck Lever  * xs_set_port - reset the port number in the remote endpoint address
162992200412SChuck Lever  * @xprt: generic transport
163092200412SChuck Lever  * @port: new port number
163192200412SChuck Lever  *
163292200412SChuck Lever  */
163392200412SChuck Lever static void xs_set_port(struct rpc_xprt *xprt, unsigned short port)
163492200412SChuck Lever {
163592200412SChuck Lever 	dprintk("RPC:       setting port for xprt %p to %u\n", xprt, port);
1636c4efcb1dSChuck Lever 
16379dc3b095SChuck Lever 	rpc_set_port(xs_addr(xprt), port);
16389dc3b095SChuck Lever 	xs_update_peer_port(xprt);
163992200412SChuck Lever }
164092200412SChuck Lever 
16414dda9c8aSTrond Myklebust static void xs_set_srcport(struct sock_xprt *transport, struct socket *sock)
16424dda9c8aSTrond Myklebust {
1643e6237b6fSTrond Myklebust 	if (transport->srcport == 0 && transport->xprt.reuseport)
16444dda9c8aSTrond Myklebust 		transport->srcport = xs_sock_getport(sock);
16454dda9c8aSTrond Myklebust }
16464dda9c8aSTrond Myklebust 
1647826799e6SJ. Bruce Fields static int xs_get_srcport(struct sock_xprt *transport)
164867a391d7STrond Myklebust {
1649826799e6SJ. Bruce Fields 	int port = transport->srcport;
165067a391d7STrond Myklebust 
165167a391d7STrond Myklebust 	if (port == 0 && transport->xprt.resvport)
165267a391d7STrond Myklebust 		port = xs_get_random_port();
165367a391d7STrond Myklebust 	return port;
165467a391d7STrond Myklebust }
165567a391d7STrond Myklebust 
1656baaf4e48SPavel Emelyanov static unsigned short xs_next_srcport(struct sock_xprt *transport, unsigned short port)
165767a391d7STrond Myklebust {
1658fbfffbd5SChuck Lever 	if (transport->srcport != 0)
1659fbfffbd5SChuck Lever 		transport->srcport = 0;
166067a391d7STrond Myklebust 	if (!transport->xprt.resvport)
166167a391d7STrond Myklebust 		return 0;
166267a391d7STrond Myklebust 	if (port <= xprt_min_resvport || port > xprt_max_resvport)
166367a391d7STrond Myklebust 		return xprt_max_resvport;
166467a391d7STrond Myklebust 	return --port;
166567a391d7STrond Myklebust }
1666beb59b68SPavel Emelyanov static int xs_bind(struct sock_xprt *transport, struct socket *sock)
1667a246b010SChuck Lever {
1668beb59b68SPavel Emelyanov 	struct sockaddr_storage myaddr;
166967a391d7STrond Myklebust 	int err, nloop = 0;
1670826799e6SJ. Bruce Fields 	int port = xs_get_srcport(transport);
167167a391d7STrond Myklebust 	unsigned short last;
1672a246b010SChuck Lever 
16730f7a622cSChris Perl 	/*
16740f7a622cSChris Perl 	 * If we are asking for any ephemeral port (i.e. port == 0 &&
16750f7a622cSChris Perl 	 * transport->xprt.resvport == 0), don't bind.  Let the local
16760f7a622cSChris Perl 	 * port selection happen implicitly when the socket is used
16770f7a622cSChris Perl 	 * (for example at connect time).
16780f7a622cSChris Perl 	 *
16790f7a622cSChris Perl 	 * This ensures that we can continue to establish TCP
16800f7a622cSChris Perl 	 * connections even when all local ephemeral ports are already
16810f7a622cSChris Perl 	 * a part of some TCP connection.  This makes no difference
168212b20ce3SBhaskar Chowdhury 	 * for UDP sockets, but also doesn't harm them.
16830f7a622cSChris Perl 	 *
16840f7a622cSChris Perl 	 * If we're asking for any reserved port (i.e. port == 0 &&
16850f7a622cSChris Perl 	 * transport->xprt.resvport == 1) xs_get_srcport above will
16860f7a622cSChris Perl 	 * ensure that port is non-zero and we will bind as needed.
16870f7a622cSChris Perl 	 */
1688826799e6SJ. Bruce Fields 	if (port <= 0)
1689826799e6SJ. Bruce Fields 		return port;
16900f7a622cSChris Perl 
1691beb59b68SPavel Emelyanov 	memcpy(&myaddr, &transport->srcaddr, transport->xprt.addrlen);
1692a246b010SChuck Lever 	do {
1693beb59b68SPavel Emelyanov 		rpc_set_port((struct sockaddr *)&myaddr, port);
1694e6242e92SSridhar Samudrala 		err = kernel_bind(sock, (struct sockaddr *)&myaddr,
1695beb59b68SPavel Emelyanov 				transport->xprt.addrlen);
1696a246b010SChuck Lever 		if (err == 0) {
1697fbfffbd5SChuck Lever 			transport->srcport = port;
1698d3bc9a1dSFrank van Maarseveen 			break;
1699a246b010SChuck Lever 		}
170067a391d7STrond Myklebust 		last = port;
1701baaf4e48SPavel Emelyanov 		port = xs_next_srcport(transport, port);
170267a391d7STrond Myklebust 		if (port > last)
170367a391d7STrond Myklebust 			nloop++;
170467a391d7STrond Myklebust 	} while (err == -EADDRINUSE && nloop != 2);
1705beb59b68SPavel Emelyanov 
17064232e863SChuck Lever 	if (myaddr.ss_family == AF_INET)
1707beb59b68SPavel Emelyanov 		dprintk("RPC:       %s %pI4:%u: %s (%d)\n", __func__,
1708beb59b68SPavel Emelyanov 				&((struct sockaddr_in *)&myaddr)->sin_addr,
1709beb59b68SPavel Emelyanov 				port, err ? "failed" : "ok", err);
1710beb59b68SPavel Emelyanov 	else
1711beb59b68SPavel Emelyanov 		dprintk("RPC:       %s %pI6:%u: %s (%d)\n", __func__,
1712beb59b68SPavel Emelyanov 				&((struct sockaddr_in6 *)&myaddr)->sin6_addr,
17137dc753f0SChuck Lever 				port, err ? "failed" : "ok", err);
1714a246b010SChuck Lever 	return err;
1715a246b010SChuck Lever }
1716a246b010SChuck Lever 
1717176e21eeSChuck Lever /*
1718176e21eeSChuck Lever  * We don't support autobind on AF_LOCAL sockets
1719176e21eeSChuck Lever  */
1720176e21eeSChuck Lever static void xs_local_rpcbind(struct rpc_task *task)
1721176e21eeSChuck Lever {
1722fb43d172STrond Myklebust 	xprt_set_bound(task->tk_xprt);
1723176e21eeSChuck Lever }
1724176e21eeSChuck Lever 
1725176e21eeSChuck Lever static void xs_local_set_port(struct rpc_xprt *xprt, unsigned short port)
1726176e21eeSChuck Lever {
1727176e21eeSChuck Lever }
1728a246b010SChuck Lever 
1729ed07536eSPeter Zijlstra #ifdef CONFIG_DEBUG_LOCK_ALLOC
1730ed07536eSPeter Zijlstra static struct lock_class_key xs_key[2];
1731ed07536eSPeter Zijlstra static struct lock_class_key xs_slock_key[2];
1732ed07536eSPeter Zijlstra 
1733176e21eeSChuck Lever static inline void xs_reclassify_socketu(struct socket *sock)
1734176e21eeSChuck Lever {
1735176e21eeSChuck Lever 	struct sock *sk = sock->sk;
1736176e21eeSChuck Lever 
1737176e21eeSChuck Lever 	sock_lock_init_class_and_name(sk, "slock-AF_LOCAL-RPC",
1738176e21eeSChuck Lever 		&xs_slock_key[1], "sk_lock-AF_LOCAL-RPC", &xs_key[1]);
1739176e21eeSChuck Lever }
1740176e21eeSChuck Lever 
17418945ee5eSChuck Lever static inline void xs_reclassify_socket4(struct socket *sock)
1742ed07536eSPeter Zijlstra {
1743ed07536eSPeter Zijlstra 	struct sock *sk = sock->sk;
17448945ee5eSChuck Lever 
17458945ee5eSChuck Lever 	sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC",
17468945ee5eSChuck Lever 		&xs_slock_key[0], "sk_lock-AF_INET-RPC", &xs_key[0]);
1747ed07536eSPeter Zijlstra }
17488945ee5eSChuck Lever 
17498945ee5eSChuck Lever static inline void xs_reclassify_socket6(struct socket *sock)
17508945ee5eSChuck Lever {
17518945ee5eSChuck Lever 	struct sock *sk = sock->sk;
17528945ee5eSChuck Lever 
17538945ee5eSChuck Lever 	sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC",
17548945ee5eSChuck Lever 		&xs_slock_key[1], "sk_lock-AF_INET6-RPC", &xs_key[1]);
1755ed07536eSPeter Zijlstra }
17566bc9638aSPavel Emelyanov 
17576bc9638aSPavel Emelyanov static inline void xs_reclassify_socket(int family, struct socket *sock)
17586bc9638aSPavel Emelyanov {
1759fafc4e1eSHannes Frederic Sowa 	if (WARN_ON_ONCE(!sock_allow_reclassification(sock->sk)))
17601b7a1819SWeston Andros Adamson 		return;
17611b7a1819SWeston Andros Adamson 
17624232e863SChuck Lever 	switch (family) {
1763176e21eeSChuck Lever 	case AF_LOCAL:
1764176e21eeSChuck Lever 		xs_reclassify_socketu(sock);
1765176e21eeSChuck Lever 		break;
17664232e863SChuck Lever 	case AF_INET:
17676bc9638aSPavel Emelyanov 		xs_reclassify_socket4(sock);
17684232e863SChuck Lever 		break;
17694232e863SChuck Lever 	case AF_INET6:
17706bc9638aSPavel Emelyanov 		xs_reclassify_socket6(sock);
17714232e863SChuck Lever 		break;
17724232e863SChuck Lever 	}
17736bc9638aSPavel Emelyanov }
1774ed07536eSPeter Zijlstra #else
17756bc9638aSPavel Emelyanov static inline void xs_reclassify_socket(int family, struct socket *sock)
17766bc9638aSPavel Emelyanov {
17776bc9638aSPavel Emelyanov }
1778ed07536eSPeter Zijlstra #endif
1779ed07536eSPeter Zijlstra 
178093dc41bdSNeilBrown static void xs_dummy_setup_socket(struct work_struct *work)
178193dc41bdSNeilBrown {
178293dc41bdSNeilBrown }
178393dc41bdSNeilBrown 
17846bc9638aSPavel Emelyanov static struct socket *xs_create_sock(struct rpc_xprt *xprt,
17854dda9c8aSTrond Myklebust 		struct sock_xprt *transport, int family, int type,
17864dda9c8aSTrond Myklebust 		int protocol, bool reuseport)
178722f79326SPavel Emelyanov {
1788a73881c9STrond Myklebust 	struct file *filp;
178922f79326SPavel Emelyanov 	struct socket *sock;
179022f79326SPavel Emelyanov 	int err;
179122f79326SPavel Emelyanov 
17926bc9638aSPavel Emelyanov 	err = __sock_create(xprt->xprt_net, family, type, protocol, &sock, 1);
179322f79326SPavel Emelyanov 	if (err < 0) {
179422f79326SPavel Emelyanov 		dprintk("RPC:       can't create %d transport socket (%d).\n",
179522f79326SPavel Emelyanov 				protocol, -err);
179622f79326SPavel Emelyanov 		goto out;
179722f79326SPavel Emelyanov 	}
17986bc9638aSPavel Emelyanov 	xs_reclassify_socket(family, sock);
179922f79326SPavel Emelyanov 
18004dda9c8aSTrond Myklebust 	if (reuseport)
1801fe31a326SChristoph Hellwig 		sock_set_reuseport(sock->sk);
18024dda9c8aSTrond Myklebust 
18034cea288aSBen Hutchings 	err = xs_bind(transport, sock);
18044cea288aSBen Hutchings 	if (err) {
180522f79326SPavel Emelyanov 		sock_release(sock);
180622f79326SPavel Emelyanov 		goto out;
180722f79326SPavel Emelyanov 	}
180822f79326SPavel Emelyanov 
1809a73881c9STrond Myklebust 	filp = sock_alloc_file(sock, O_NONBLOCK, NULL);
1810a73881c9STrond Myklebust 	if (IS_ERR(filp))
1811a73881c9STrond Myklebust 		return ERR_CAST(filp);
1812a73881c9STrond Myklebust 	transport->file = filp;
1813a73881c9STrond Myklebust 
181422f79326SPavel Emelyanov 	return sock;
181522f79326SPavel Emelyanov out:
181622f79326SPavel Emelyanov 	return ERR_PTR(err);
181722f79326SPavel Emelyanov }
181822f79326SPavel Emelyanov 
1819176e21eeSChuck Lever static int xs_local_finish_connecting(struct rpc_xprt *xprt,
1820176e21eeSChuck Lever 				      struct socket *sock)
1821176e21eeSChuck Lever {
1822176e21eeSChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
1823176e21eeSChuck Lever 									xprt);
1824176e21eeSChuck Lever 
1825176e21eeSChuck Lever 	if (!transport->inet) {
1826176e21eeSChuck Lever 		struct sock *sk = sock->sk;
1827176e21eeSChuck Lever 
1828176e21eeSChuck Lever 		write_lock_bh(&sk->sk_callback_lock);
1829176e21eeSChuck Lever 
1830176e21eeSChuck Lever 		xs_save_old_callbacks(transport, sk);
1831176e21eeSChuck Lever 
1832176e21eeSChuck Lever 		sk->sk_user_data = xprt;
1833a2648094STrond Myklebust 		sk->sk_data_ready = xs_data_ready;
1834176e21eeSChuck Lever 		sk->sk_write_space = xs_udp_write_space;
1835b4411457SEric Dumazet 		sock_set_flag(sk, SOCK_FASYNC);
18362118071dSTrond Myklebust 		sk->sk_error_report = xs_error_report;
1837176e21eeSChuck Lever 
1838176e21eeSChuck Lever 		xprt_clear_connected(xprt);
1839176e21eeSChuck Lever 
1840176e21eeSChuck Lever 		/* Reset to new socket */
1841176e21eeSChuck Lever 		transport->sock = sock;
1842176e21eeSChuck Lever 		transport->inet = sk;
1843176e21eeSChuck Lever 
1844176e21eeSChuck Lever 		write_unlock_bh(&sk->sk_callback_lock);
1845176e21eeSChuck Lever 	}
1846176e21eeSChuck Lever 
1847ae053551STrond Myklebust 	xs_stream_start_connect(transport);
18486c7a64e5STrond Myklebust 
1849176e21eeSChuck Lever 	return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, 0);
1850176e21eeSChuck Lever }
1851176e21eeSChuck Lever 
1852176e21eeSChuck Lever /**
1853176e21eeSChuck Lever  * xs_local_setup_socket - create AF_LOCAL socket, connect to a local endpoint
1854176e21eeSChuck Lever  * @transport: socket transport to connect
1855176e21eeSChuck Lever  */
1856dc107402SJ. Bruce Fields static int xs_local_setup_socket(struct sock_xprt *transport)
1857176e21eeSChuck Lever {
1858176e21eeSChuck Lever 	struct rpc_xprt *xprt = &transport->xprt;
1859a73881c9STrond Myklebust 	struct file *filp;
1860176e21eeSChuck Lever 	struct socket *sock;
186168e9a246SColin Ian King 	int status;
1862176e21eeSChuck Lever 
1863176e21eeSChuck Lever 	status = __sock_create(xprt->xprt_net, AF_LOCAL,
1864176e21eeSChuck Lever 					SOCK_STREAM, 0, &sock, 1);
1865176e21eeSChuck Lever 	if (status < 0) {
1866176e21eeSChuck Lever 		dprintk("RPC:       can't create AF_LOCAL "
1867176e21eeSChuck Lever 			"transport socket (%d).\n", -status);
1868176e21eeSChuck Lever 		goto out;
1869176e21eeSChuck Lever 	}
1870d1358917SStefan Hajnoczi 	xs_reclassify_socket(AF_LOCAL, sock);
1871176e21eeSChuck Lever 
1872a73881c9STrond Myklebust 	filp = sock_alloc_file(sock, O_NONBLOCK, NULL);
1873a73881c9STrond Myklebust 	if (IS_ERR(filp)) {
1874a73881c9STrond Myklebust 		status = PTR_ERR(filp);
1875a73881c9STrond Myklebust 		goto out;
1876a73881c9STrond Myklebust 	}
1877a73881c9STrond Myklebust 	transport->file = filp;
1878a73881c9STrond Myklebust 
1879176e21eeSChuck Lever 	dprintk("RPC:       worker connecting xprt %p via AF_LOCAL to %s\n",
1880176e21eeSChuck Lever 			xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1881176e21eeSChuck Lever 
1882176e21eeSChuck Lever 	status = xs_local_finish_connecting(xprt, sock);
188340b5ea0cSTrond Myklebust 	trace_rpc_socket_connect(xprt, sock, status);
1884176e21eeSChuck Lever 	switch (status) {
1885176e21eeSChuck Lever 	case 0:
1886176e21eeSChuck Lever 		dprintk("RPC:       xprt %p connected to %s\n",
1887176e21eeSChuck Lever 				xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
18883968a8a5SChuck Lever 		xprt->stat.connect_count++;
18893968a8a5SChuck Lever 		xprt->stat.connect_time += (long)jiffies -
18903968a8a5SChuck Lever 					   xprt->stat.connect_start;
1891176e21eeSChuck Lever 		xprt_set_connected(xprt);
189293f479d3SGustavo A. R. Silva 		break;
18933601c4a9STrond Myklebust 	case -ENOBUFS:
1894176e21eeSChuck Lever 		break;
1895176e21eeSChuck Lever 	case -ENOENT:
1896176e21eeSChuck Lever 		dprintk("RPC:       xprt %p: socket %s does not exist\n",
1897176e21eeSChuck Lever 				xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1898176e21eeSChuck Lever 		break;
18994a20a988STrond Myklebust 	case -ECONNREFUSED:
19004a20a988STrond Myklebust 		dprintk("RPC:       xprt %p: connection refused for %s\n",
19014a20a988STrond Myklebust 				xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
19024a20a988STrond Myklebust 		break;
1903176e21eeSChuck Lever 	default:
1904176e21eeSChuck Lever 		printk(KERN_ERR "%s: unhandled error (%d) connecting to %s\n",
1905176e21eeSChuck Lever 				__func__, -status,
1906176e21eeSChuck Lever 				xprt->address_strings[RPC_DISPLAY_ADDR]);
1907176e21eeSChuck Lever 	}
1908176e21eeSChuck Lever 
1909176e21eeSChuck Lever out:
1910176e21eeSChuck Lever 	xprt_clear_connecting(xprt);
1911176e21eeSChuck Lever 	xprt_wake_pending_tasks(xprt, status);
1912dc107402SJ. Bruce Fields 	return status;
1913dc107402SJ. Bruce Fields }
1914dc107402SJ. Bruce Fields 
1915b6669737SLinus Torvalds static void xs_local_connect(struct rpc_xprt *xprt, struct rpc_task *task)
1916dc107402SJ. Bruce Fields {
1917dc107402SJ. Bruce Fields 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1918dc107402SJ. Bruce Fields 	int ret;
1919dc107402SJ. Bruce Fields 
1920dc107402SJ. Bruce Fields 	 if (RPC_IS_ASYNC(task)) {
1921dc107402SJ. Bruce Fields 		/*
1922dc107402SJ. Bruce Fields 		 * We want the AF_LOCAL connect to be resolved in the
1923dc107402SJ. Bruce Fields 		 * filesystem namespace of the process making the rpc
1924dc107402SJ. Bruce Fields 		 * call.  Thus we connect synchronously.
1925dc107402SJ. Bruce Fields 		 *
1926dc107402SJ. Bruce Fields 		 * If we want to support asynchronous AF_LOCAL calls,
1927dc107402SJ. Bruce Fields 		 * we'll need to figure out how to pass a namespace to
1928dc107402SJ. Bruce Fields 		 * connect.
1929dc107402SJ. Bruce Fields 		 */
19305ad64b36STrond Myklebust 		task->tk_rpc_status = -ENOTCONN;
1931dc107402SJ. Bruce Fields 		rpc_exit(task, -ENOTCONN);
1932dc107402SJ. Bruce Fields 		return;
1933dc107402SJ. Bruce Fields 	}
1934dc107402SJ. Bruce Fields 	ret = xs_local_setup_socket(transport);
1935dc107402SJ. Bruce Fields 	if (ret && !RPC_IS_SOFTCONN(task))
1936dc107402SJ. Bruce Fields 		msleep_interruptible(15000);
1937176e21eeSChuck Lever }
1938176e21eeSChuck Lever 
19393c87ef6eSJeff Layton #if IS_ENABLED(CONFIG_SUNRPC_SWAP)
1940d6e971d8SJeff Layton /*
1941d6e971d8SJeff Layton  * Note that this should be called with XPRT_LOCKED held (or when we otherwise
1942d6e971d8SJeff Layton  * know that we have exclusive access to the socket), to guard against
1943d6e971d8SJeff Layton  * races with xs_reset_transport.
1944d6e971d8SJeff Layton  */
1945a564b8f0SMel Gorman static void xs_set_memalloc(struct rpc_xprt *xprt)
1946a564b8f0SMel Gorman {
1947a564b8f0SMel Gorman 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
1948a564b8f0SMel Gorman 			xprt);
1949a564b8f0SMel Gorman 
1950d6e971d8SJeff Layton 	/*
1951d6e971d8SJeff Layton 	 * If there's no sock, then we have nothing to set. The
1952d6e971d8SJeff Layton 	 * reconnecting process will get it for us.
1953d6e971d8SJeff Layton 	 */
1954d6e971d8SJeff Layton 	if (!transport->inet)
1955d6e971d8SJeff Layton 		return;
19568e228133SJeff Layton 	if (atomic_read(&xprt->swapper))
1957a564b8f0SMel Gorman 		sk_set_memalloc(transport->inet);
1958a564b8f0SMel Gorman }
1959a564b8f0SMel Gorman 
1960a564b8f0SMel Gorman /**
1961d67fa4d8SJeff Layton  * xs_enable_swap - Tag this transport as being used for swap.
1962a564b8f0SMel Gorman  * @xprt: transport to tag
1963a564b8f0SMel Gorman  *
19648e228133SJeff Layton  * Take a reference to this transport on behalf of the rpc_clnt, and
19658e228133SJeff Layton  * optionally mark it for swapping if it wasn't already.
1966a564b8f0SMel Gorman  */
1967d67fa4d8SJeff Layton static int
1968d67fa4d8SJeff Layton xs_enable_swap(struct rpc_xprt *xprt)
1969a564b8f0SMel Gorman {
1970d6e971d8SJeff Layton 	struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt);
1971a564b8f0SMel Gorman 
1972d6e971d8SJeff Layton 	if (atomic_inc_return(&xprt->swapper) != 1)
1973d6e971d8SJeff Layton 		return 0;
1974d6e971d8SJeff Layton 	if (wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_KILLABLE))
1975d6e971d8SJeff Layton 		return -ERESTARTSYS;
1976d6e971d8SJeff Layton 	if (xs->inet)
1977d6e971d8SJeff Layton 		sk_set_memalloc(xs->inet);
1978d6e971d8SJeff Layton 	xprt_release_xprt(xprt, NULL);
19798e228133SJeff Layton 	return 0;
1980a564b8f0SMel Gorman }
1981a564b8f0SMel Gorman 
19828e228133SJeff Layton /**
1983d67fa4d8SJeff Layton  * xs_disable_swap - Untag this transport as being used for swap.
19848e228133SJeff Layton  * @xprt: transport to tag
19858e228133SJeff Layton  *
19868e228133SJeff Layton  * Drop a "swapper" reference to this xprt on behalf of the rpc_clnt. If the
19878e228133SJeff Layton  * swapper refcount goes to 0, untag the socket as a memalloc socket.
19888e228133SJeff Layton  */
1989d67fa4d8SJeff Layton static void
1990d67fa4d8SJeff Layton xs_disable_swap(struct rpc_xprt *xprt)
19918e228133SJeff Layton {
1992d6e971d8SJeff Layton 	struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt);
19938e228133SJeff Layton 
1994d6e971d8SJeff Layton 	if (!atomic_dec_and_test(&xprt->swapper))
1995d6e971d8SJeff Layton 		return;
1996d6e971d8SJeff Layton 	if (wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_KILLABLE))
1997d6e971d8SJeff Layton 		return;
1998d6e971d8SJeff Layton 	if (xs->inet)
1999d6e971d8SJeff Layton 		sk_clear_memalloc(xs->inet);
2000d6e971d8SJeff Layton 	xprt_release_xprt(xprt, NULL);
2001a564b8f0SMel Gorman }
2002a564b8f0SMel Gorman #else
2003a564b8f0SMel Gorman static void xs_set_memalloc(struct rpc_xprt *xprt)
2004a564b8f0SMel Gorman {
2005a564b8f0SMel Gorman }
2006d67fa4d8SJeff Layton 
2007d67fa4d8SJeff Layton static int
2008d67fa4d8SJeff Layton xs_enable_swap(struct rpc_xprt *xprt)
2009d67fa4d8SJeff Layton {
2010d67fa4d8SJeff Layton 	return -EINVAL;
2011d67fa4d8SJeff Layton }
2012d67fa4d8SJeff Layton 
2013d67fa4d8SJeff Layton static void
2014d67fa4d8SJeff Layton xs_disable_swap(struct rpc_xprt *xprt)
2015d67fa4d8SJeff Layton {
2016d67fa4d8SJeff Layton }
2017a564b8f0SMel Gorman #endif
2018a564b8f0SMel Gorman 
201916be2d20SChuck Lever static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
2020a246b010SChuck Lever {
202116be2d20SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2022edb267a6SChuck Lever 
2023ee0ac0c2SChuck Lever 	if (!transport->inet) {
2024b0d93ad5SChuck Lever 		struct sock *sk = sock->sk;
2025b0d93ad5SChuck Lever 
2026b0d93ad5SChuck Lever 		write_lock_bh(&sk->sk_callback_lock);
2027b0d93ad5SChuck Lever 
20282a9e1cfaSTrond Myklebust 		xs_save_old_callbacks(transport, sk);
20292a9e1cfaSTrond Myklebust 
2030b0d93ad5SChuck Lever 		sk->sk_user_data = xprt;
2031f9b2ee71STrond Myklebust 		sk->sk_data_ready = xs_data_ready;
2032b0d93ad5SChuck Lever 		sk->sk_write_space = xs_udp_write_space;
2033b4411457SEric Dumazet 		sock_set_flag(sk, SOCK_FASYNC);
2034b0d93ad5SChuck Lever 
2035b0d93ad5SChuck Lever 		xprt_set_connected(xprt);
2036b0d93ad5SChuck Lever 
2037b0d93ad5SChuck Lever 		/* Reset to new socket */
2038ee0ac0c2SChuck Lever 		transport->sock = sock;
2039ee0ac0c2SChuck Lever 		transport->inet = sk;
2040b0d93ad5SChuck Lever 
2041a564b8f0SMel Gorman 		xs_set_memalloc(xprt);
2042a564b8f0SMel Gorman 
2043b0d93ad5SChuck Lever 		write_unlock_bh(&sk->sk_callback_lock);
2044b0d93ad5SChuck Lever 	}
2045470056c2SChuck Lever 	xs_udp_do_set_buffer_size(xprt);
204602910177STrond Myklebust 
204702910177STrond Myklebust 	xprt->stat.connect_start = jiffies;
204816be2d20SChuck Lever }
204916be2d20SChuck Lever 
20508c14ff2aSPavel Emelyanov static void xs_udp_setup_socket(struct work_struct *work)
2051a246b010SChuck Lever {
2052a246b010SChuck Lever 	struct sock_xprt *transport =
2053a246b010SChuck Lever 		container_of(work, struct sock_xprt, connect_worker.work);
2054a246b010SChuck Lever 	struct rpc_xprt *xprt = &transport->xprt;
2055d099b8afSColin Ian King 	struct socket *sock;
2056b65c0310SPavel Emelyanov 	int status = -EIO;
2057a246b010SChuck Lever 
20588c14ff2aSPavel Emelyanov 	sock = xs_create_sock(xprt, transport,
20594dda9c8aSTrond Myklebust 			xs_addr(xprt)->sa_family, SOCK_DGRAM,
20604dda9c8aSTrond Myklebust 			IPPROTO_UDP, false);
2061b65c0310SPavel Emelyanov 	if (IS_ERR(sock))
2062a246b010SChuck Lever 		goto out;
206368e220bdSChuck Lever 
2064c740eff8SChuck Lever 	dprintk("RPC:       worker connecting xprt %p via %s to "
2065c740eff8SChuck Lever 				"%s (port %s)\n", xprt,
2066c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_PROTO],
2067c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_ADDR],
2068c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_PORT]);
206968e220bdSChuck Lever 
207068e220bdSChuck Lever 	xs_udp_finish_connecting(xprt, sock);
207140b5ea0cSTrond Myklebust 	trace_rpc_socket_connect(xprt, sock, 0);
2072a246b010SChuck Lever 	status = 0;
2073b0d93ad5SChuck Lever out:
2074b0d93ad5SChuck Lever 	xprt_clear_connecting(xprt);
2075cf76785dSTrond Myklebust 	xprt_unlock_connect(xprt, transport);
20767d1e8255STrond Myklebust 	xprt_wake_pending_tasks(xprt, status);
2077b0d93ad5SChuck Lever }
2078b0d93ad5SChuck Lever 
20794876cc77STrond Myklebust /**
20804876cc77STrond Myklebust  * xs_tcp_shutdown - gracefully shut down a TCP socket
20814876cc77STrond Myklebust  * @xprt: transport
20824876cc77STrond Myklebust  *
20834876cc77STrond Myklebust  * Initiates a graceful shutdown of the TCP socket by calling the
20844876cc77STrond Myklebust  * equivalent of shutdown(SHUT_RDWR);
20854876cc77STrond Myklebust  */
20864876cc77STrond Myklebust static void xs_tcp_shutdown(struct rpc_xprt *xprt)
20874876cc77STrond Myklebust {
20884876cc77STrond Myklebust 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
20894876cc77STrond Myklebust 	struct socket *sock = transport->sock;
20909b30889cSTrond Myklebust 	int skst = transport->inet ? transport->inet->sk_state : TCP_CLOSE;
20914876cc77STrond Myklebust 
20924876cc77STrond Myklebust 	if (sock == NULL)
20934876cc77STrond Myklebust 		return;
20949b30889cSTrond Myklebust 	switch (skst) {
20959b30889cSTrond Myklebust 	default:
20964876cc77STrond Myklebust 		kernel_sock_shutdown(sock, SHUT_RDWR);
20974876cc77STrond Myklebust 		trace_rpc_socket_shutdown(xprt, sock);
20989b30889cSTrond Myklebust 		break;
20999b30889cSTrond Myklebust 	case TCP_CLOSE:
21009b30889cSTrond Myklebust 	case TCP_TIME_WAIT:
21014876cc77STrond Myklebust 		xs_reset_transport(transport);
21024876cc77STrond Myklebust 	}
21039b30889cSTrond Myklebust }
21044876cc77STrond Myklebust 
21058d1b8c62STrond Myklebust static void xs_tcp_set_socket_timeouts(struct rpc_xprt *xprt,
21068d1b8c62STrond Myklebust 		struct socket *sock)
2107b0d93ad5SChuck Lever {
210816be2d20SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
21097196dbb0STrond Myklebust 	unsigned int keepidle;
21107196dbb0STrond Myklebust 	unsigned int keepcnt;
2111775f06abSTrond Myklebust 	unsigned int timeo;
21127f260e85STrond Myklebust 
2113b5e92419STrond Myklebust 	spin_lock(&xprt->transport_lock);
21147196dbb0STrond Myklebust 	keepidle = DIV_ROUND_UP(xprt->timeout->to_initval, HZ);
21157196dbb0STrond Myklebust 	keepcnt = xprt->timeout->to_retries + 1;
21167196dbb0STrond Myklebust 	timeo = jiffies_to_msecs(xprt->timeout->to_initval) *
21177196dbb0STrond Myklebust 		(xprt->timeout->to_retries + 1);
21187196dbb0STrond Myklebust 	clear_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state);
2119b5e92419STrond Myklebust 	spin_unlock(&xprt->transport_lock);
21207f260e85STrond Myklebust 
21217f260e85STrond Myklebust 	/* TCP Keepalive options */
2122ce3d9544SChristoph Hellwig 	sock_set_keepalive(sock->sk);
212371c48eb8SChristoph Hellwig 	tcp_sock_set_keepidle(sock->sk, keepidle);
2124d41ecaacSChristoph Hellwig 	tcp_sock_set_keepintvl(sock->sk, keepidle);
2125480aeb96SChristoph Hellwig 	tcp_sock_set_keepcnt(sock->sk, keepcnt);
2126b0d93ad5SChuck Lever 
21278d1b8c62STrond Myklebust 	/* TCP user timeout (see RFC5482) */
2128c488aeadSChristoph Hellwig 	tcp_sock_set_user_timeout(sock->sk, timeo);
21298d1b8c62STrond Myklebust }
21308d1b8c62STrond Myklebust 
21317196dbb0STrond Myklebust static void xs_tcp_set_connect_timeout(struct rpc_xprt *xprt,
21327196dbb0STrond Myklebust 		unsigned long connect_timeout,
21337196dbb0STrond Myklebust 		unsigned long reconnect_timeout)
21347196dbb0STrond Myklebust {
21357196dbb0STrond Myklebust 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
21367196dbb0STrond Myklebust 	struct rpc_timeout to;
21377196dbb0STrond Myklebust 	unsigned long initval;
21387196dbb0STrond Myklebust 
2139b5e92419STrond Myklebust 	spin_lock(&xprt->transport_lock);
21407196dbb0STrond Myklebust 	if (reconnect_timeout < xprt->max_reconnect_timeout)
21417196dbb0STrond Myklebust 		xprt->max_reconnect_timeout = reconnect_timeout;
21427196dbb0STrond Myklebust 	if (connect_timeout < xprt->connect_timeout) {
21437196dbb0STrond Myklebust 		memcpy(&to, xprt->timeout, sizeof(to));
21447196dbb0STrond Myklebust 		initval = DIV_ROUND_UP(connect_timeout, to.to_retries + 1);
21457196dbb0STrond Myklebust 		/* Arbitrary lower limit */
21467196dbb0STrond Myklebust 		if (initval <  XS_TCP_INIT_REEST_TO << 1)
21477196dbb0STrond Myklebust 			initval = XS_TCP_INIT_REEST_TO << 1;
21487196dbb0STrond Myklebust 		to.to_initval = initval;
21497196dbb0STrond Myklebust 		to.to_maxval = initval;
21507196dbb0STrond Myklebust 		memcpy(&transport->tcp_timeout, &to,
21517196dbb0STrond Myklebust 				sizeof(transport->tcp_timeout));
21527196dbb0STrond Myklebust 		xprt->timeout = &transport->tcp_timeout;
21537196dbb0STrond Myklebust 		xprt->connect_timeout = connect_timeout;
21547196dbb0STrond Myklebust 	}
21557196dbb0STrond Myklebust 	set_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state);
2156b5e92419STrond Myklebust 	spin_unlock(&xprt->transport_lock);
21577196dbb0STrond Myklebust }
21587196dbb0STrond Myklebust 
21598d1b8c62STrond Myklebust static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
21608d1b8c62STrond Myklebust {
21618d1b8c62STrond Myklebust 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
21628d1b8c62STrond Myklebust 	int ret = -ENOTCONN;
21638d1b8c62STrond Myklebust 
21648d1b8c62STrond Myklebust 	if (!transport->inet) {
21658d1b8c62STrond Myklebust 		struct sock *sk = sock->sk;
21668d1b8c62STrond Myklebust 
2167d88e4d82SNeilBrown 		/* Avoid temporary address, they are bad for long-lived
2168d88e4d82SNeilBrown 		 * connections such as NFS mounts.
2169d88e4d82SNeilBrown 		 * RFC4941, section 3.6 suggests that:
2170d88e4d82SNeilBrown 		 *    Individual applications, which have specific
2171d88e4d82SNeilBrown 		 *    knowledge about the normal duration of connections,
2172d88e4d82SNeilBrown 		 *    MAY override this as appropriate.
2173d88e4d82SNeilBrown 		 */
217418d5ad62SChristoph Hellwig 		if (xs_addr(xprt)->sa_family == PF_INET6) {
217518d5ad62SChristoph Hellwig 			ip6_sock_set_addr_preferences(sk,
217618d5ad62SChristoph Hellwig 				IPV6_PREFER_SRC_PUBLIC);
217718d5ad62SChristoph Hellwig 		}
2178d88e4d82SNeilBrown 
21798d1b8c62STrond Myklebust 		xs_tcp_set_socket_timeouts(xprt, sock);
2180d737e5d4STrond Myklebust 		tcp_sock_set_nodelay(sk);
2181775f06abSTrond Myklebust 
2182b0d93ad5SChuck Lever 		write_lock_bh(&sk->sk_callback_lock);
2183b0d93ad5SChuck Lever 
21842a9e1cfaSTrond Myklebust 		xs_save_old_callbacks(transport, sk);
21852a9e1cfaSTrond Myklebust 
2186b0d93ad5SChuck Lever 		sk->sk_user_data = xprt;
21875157b956STrond Myklebust 		sk->sk_data_ready = xs_data_ready;
2188b0d93ad5SChuck Lever 		sk->sk_state_change = xs_tcp_state_change;
2189b0d93ad5SChuck Lever 		sk->sk_write_space = xs_tcp_write_space;
2190b4411457SEric Dumazet 		sock_set_flag(sk, SOCK_FASYNC);
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 
2202b0d93ad5SChuck Lever 		write_unlock_bh(&sk->sk_callback_lock);
2203b0d93ad5SChuck Lever 	}
2204b0d93ad5SChuck Lever 
220501d37c42STrond Myklebust 	if (!xprt_bound(xprt))
2206fe19a96bSTrond Myklebust 		goto out;
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);
2214fe19a96bSTrond Myklebust 	ret = kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK);
2215fe19a96bSTrond Myklebust 	switch (ret) {
2216fe19a96bSTrond Myklebust 	case 0:
22174dda9c8aSTrond Myklebust 		xs_set_srcport(transport, sock);
2218df561f66SGustavo A. R. Silva 		fallthrough;
2219fe19a96bSTrond Myklebust 	case -EINPROGRESS:
2220fe19a96bSTrond Myklebust 		/* SYN_SENT! */
2221fe19a96bSTrond Myklebust 		if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
2222fe19a96bSTrond Myklebust 			xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
22231f4c17a0STrond Myklebust 		break;
22241f4c17a0STrond Myklebust 	case -EADDRNOTAVAIL:
22251f4c17a0STrond Myklebust 		/* Source port number is unavailable. Try a new one! */
22261f4c17a0STrond Myklebust 		transport->srcport = 0;
2227fe19a96bSTrond Myklebust 	}
2228fe19a96bSTrond Myklebust out:
2229fe19a96bSTrond Myklebust 	return ret;
223016be2d20SChuck Lever }
223116be2d20SChuck Lever 
223216be2d20SChuck Lever /**
2233b61d59ffSTrond Myklebust  * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint
2234acf0a39fSChuck Lever  * @work: queued work item
223516be2d20SChuck Lever  *
223616be2d20SChuck Lever  * Invoked by a work queue tasklet.
223716be2d20SChuck Lever  */
2238cdd518d5SPavel Emelyanov static void xs_tcp_setup_socket(struct work_struct *work)
223916be2d20SChuck Lever {
2240cdd518d5SPavel Emelyanov 	struct sock_xprt *transport =
2241cdd518d5SPavel Emelyanov 		container_of(work, struct sock_xprt, connect_worker.work);
224216be2d20SChuck Lever 	struct socket *sock = transport->sock;
2243a9f5f0f7SPavel Emelyanov 	struct rpc_xprt *xprt = &transport->xprt;
2244b61d59ffSTrond Myklebust 	int status = -EIO;
224516be2d20SChuck Lever 
224616be2d20SChuck Lever 	if (!sock) {
2247cdd518d5SPavel Emelyanov 		sock = xs_create_sock(xprt, transport,
22484dda9c8aSTrond Myklebust 				xs_addr(xprt)->sa_family, SOCK_STREAM,
22494dda9c8aSTrond Myklebust 				IPPROTO_TCP, true);
2250b61d59ffSTrond Myklebust 		if (IS_ERR(sock)) {
2251b61d59ffSTrond Myklebust 			status = PTR_ERR(sock);
225216be2d20SChuck Lever 			goto out;
225316be2d20SChuck Lever 		}
22547d1e8255STrond Myklebust 	}
22557d1e8255STrond Myklebust 
2256c740eff8SChuck Lever 	dprintk("RPC:       worker connecting xprt %p via %s to "
2257c740eff8SChuck Lever 				"%s (port %s)\n", xprt,
2258c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_PROTO],
2259c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_ADDR],
2260c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_PORT]);
226116be2d20SChuck Lever 
226216be2d20SChuck Lever 	status = xs_tcp_finish_connecting(xprt, sock);
226340b5ea0cSTrond Myklebust 	trace_rpc_socket_connect(xprt, sock, status);
2264a246b010SChuck Lever 	dprintk("RPC:       %p connect status %d connected %d sock state %d\n",
226546121cf7SChuck Lever 			xprt, -status, xprt_connected(xprt),
226646121cf7SChuck Lever 			sock->sk->sk_state);
2267a246b010SChuck Lever 	switch (status) {
2268f75e6745STrond Myklebust 	default:
2269f75e6745STrond Myklebust 		printk("%s: connect returned unhandled error %d\n",
2270f75e6745STrond Myklebust 			__func__, status);
2271df561f66SGustavo A. R. Silva 		fallthrough;
2272f75e6745STrond Myklebust 	case -EADDRNOTAVAIL:
2273f75e6745STrond Myklebust 		/* We're probably in TIME_WAIT. Get rid of existing socket,
2274f75e6745STrond Myklebust 		 * and retry
2275f75e6745STrond Myklebust 		 */
2276a519fc7aSTrond Myklebust 		xs_tcp_force_close(xprt);
227788b5ed73STrond Myklebust 		break;
22782a491991STrond Myklebust 	case 0:
2279a246b010SChuck Lever 	case -EINPROGRESS:
2280a246b010SChuck Lever 	case -EALREADY:
2281718ba5b8STrond Myklebust 		xprt_unlock_connect(xprt, transport);
22827d1e8255STrond Myklebust 		return;
22839fcfe0c8STrond Myklebust 	case -EINVAL:
22849fcfe0c8STrond Myklebust 		/* Happens, for instance, if the user specified a link
22859fcfe0c8STrond Myklebust 		 * local IPv6 address without a scope-id.
22869fcfe0c8STrond Myklebust 		 */
22873ed5e2a2STrond Myklebust 	case -ECONNREFUSED:
22883ed5e2a2STrond Myklebust 	case -ECONNRESET:
2289eb5b46faSTrond Myklebust 	case -ENETDOWN:
22903ed5e2a2STrond Myklebust 	case -ENETUNREACH:
22914ba161a7STrond Myklebust 	case -EHOSTUNREACH:
22923913c78cSTrond Myklebust 	case -EADDRINUSE:
22933601c4a9STrond Myklebust 	case -ENOBUFS:
22948c71139dSCalum Mackay 		/* xs_tcp_force_close() wakes tasks with a fixed error code.
22958c71139dSCalum Mackay 		 * We need to wake them first to ensure the correct error code.
22966ea44adcSNeilBrown 		 */
22976ea44adcSNeilBrown 		xprt_wake_pending_tasks(xprt, status);
22984efdd92cSTrond Myklebust 		xs_tcp_force_close(xprt);
22999fcfe0c8STrond Myklebust 		goto out;
23008a2cec29STrond Myklebust 	}
23012a491991STrond Myklebust 	status = -EAGAIN;
2302a246b010SChuck Lever out:
23032226feb6SChuck Lever 	xprt_clear_connecting(xprt);
2304cf76785dSTrond Myklebust 	xprt_unlock_connect(xprt, transport);
23057d1e8255STrond Myklebust 	xprt_wake_pending_tasks(xprt, status);
2306a246b010SChuck Lever }
2307a246b010SChuck Lever 
230868e220bdSChuck Lever /**
23099903cd1cSChuck Lever  * xs_connect - connect a socket to a remote endpoint
23101b092092STrond Myklebust  * @xprt: pointer to transport structure
23119903cd1cSChuck Lever  * @task: address of RPC task that manages state of connect request
23129903cd1cSChuck Lever  *
23139903cd1cSChuck Lever  * TCP: If the remote end dropped the connection, delay reconnecting.
231403bf4b70SChuck Lever  *
231503bf4b70SChuck Lever  * UDP socket connects are synchronous, but we use a work queue anyway
231603bf4b70SChuck Lever  * to guarantee that even unprivileged user processes can set up a
231703bf4b70SChuck Lever  * socket on a privileged port.
231803bf4b70SChuck Lever  *
231903bf4b70SChuck Lever  * If a UDP socket connect fails, the delay behavior here prevents
232003bf4b70SChuck Lever  * retry floods (hard mounts).
23219903cd1cSChuck Lever  */
23221b092092STrond Myklebust static void xs_connect(struct rpc_xprt *xprt, struct rpc_task *task)
2323a246b010SChuck Lever {
2324ee0ac0c2SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
232502910177STrond Myklebust 	unsigned long delay = 0;
2326a246b010SChuck Lever 
2327718ba5b8STrond Myklebust 	WARN_ON_ONCE(!xprt_lock_connect(xprt, task, transport));
2328718ba5b8STrond Myklebust 
232999b1a4c3STrond Myklebust 	if (transport->sock != NULL) {
233046121cf7SChuck Lever 		dprintk("RPC:       xs_connect delayed xprt %p for %lu "
233146121cf7SChuck Lever 				"seconds\n",
233203bf4b70SChuck Lever 				xprt, xprt->reestablish_timeout / HZ);
233399b1a4c3STrond Myklebust 
233499b1a4c3STrond Myklebust 		/* Start by resetting any existing state */
233599b1a4c3STrond Myklebust 		xs_reset_transport(transport);
233699b1a4c3STrond Myklebust 
2337675dd90aSChuck Lever 		delay = xprt_reconnect_delay(xprt);
2338675dd90aSChuck Lever 		xprt_reconnect_backoff(xprt, XS_TCP_INIT_REEST_TO);
233902910177STrond Myklebust 
234002910177STrond Myklebust 	} else
23419903cd1cSChuck Lever 		dprintk("RPC:       xs_connect scheduled xprt %p\n", xprt);
234202910177STrond Myklebust 
234340a5f1b1STrond Myklebust 	queue_delayed_work(xprtiod_workqueue,
234402910177STrond Myklebust 			&transport->connect_worker,
234502910177STrond Myklebust 			delay);
2346a246b010SChuck Lever }
2347a246b010SChuck Lever 
23484f8943f8STrond Myklebust static void xs_wake_disconnect(struct sock_xprt *transport)
23494f8943f8STrond Myklebust {
23504f8943f8STrond Myklebust 	if (test_and_clear_bit(XPRT_SOCK_WAKE_DISCONNECT, &transport->sock_state))
23514f8943f8STrond Myklebust 		xs_tcp_force_close(&transport->xprt);
23524f8943f8STrond Myklebust }
23534f8943f8STrond Myklebust 
23544f8943f8STrond Myklebust static void xs_wake_write(struct sock_xprt *transport)
23554f8943f8STrond Myklebust {
23564f8943f8STrond Myklebust 	if (test_and_clear_bit(XPRT_SOCK_WAKE_WRITE, &transport->sock_state))
23574f8943f8STrond Myklebust 		xprt_write_space(&transport->xprt);
23584f8943f8STrond Myklebust }
23594f8943f8STrond Myklebust 
23604f8943f8STrond Myklebust static void xs_wake_error(struct sock_xprt *transport)
23614f8943f8STrond Myklebust {
23624f8943f8STrond Myklebust 	int sockerr;
23634f8943f8STrond Myklebust 
23644f8943f8STrond Myklebust 	if (!test_bit(XPRT_SOCK_WAKE_ERROR, &transport->sock_state))
23654f8943f8STrond Myklebust 		return;
23664f8943f8STrond Myklebust 	mutex_lock(&transport->recv_mutex);
23674f8943f8STrond Myklebust 	if (transport->sock == NULL)
23684f8943f8STrond Myklebust 		goto out;
23694f8943f8STrond Myklebust 	if (!test_and_clear_bit(XPRT_SOCK_WAKE_ERROR, &transport->sock_state))
23704f8943f8STrond Myklebust 		goto out;
2371af84537dSBenjamin Coddington 	sockerr = xchg(&transport->xprt_err, 0);
23724f8943f8STrond Myklebust 	if (sockerr < 0)
23734f8943f8STrond Myklebust 		xprt_wake_pending_tasks(&transport->xprt, sockerr);
23744f8943f8STrond Myklebust out:
23754f8943f8STrond Myklebust 	mutex_unlock(&transport->recv_mutex);
23764f8943f8STrond Myklebust }
23774f8943f8STrond Myklebust 
23784f8943f8STrond Myklebust static void xs_wake_pending(struct sock_xprt *transport)
23794f8943f8STrond Myklebust {
23804f8943f8STrond Myklebust 	if (test_and_clear_bit(XPRT_SOCK_WAKE_PENDING, &transport->sock_state))
23814f8943f8STrond Myklebust 		xprt_wake_pending_tasks(&transport->xprt, -EAGAIN);
23824f8943f8STrond Myklebust }
23834f8943f8STrond Myklebust 
23844f8943f8STrond Myklebust static void xs_error_handle(struct work_struct *work)
23854f8943f8STrond Myklebust {
23864f8943f8STrond Myklebust 	struct sock_xprt *transport = container_of(work,
23874f8943f8STrond Myklebust 			struct sock_xprt, error_worker);
23884f8943f8STrond Myklebust 
23894f8943f8STrond Myklebust 	xs_wake_disconnect(transport);
23904f8943f8STrond Myklebust 	xs_wake_write(transport);
23914f8943f8STrond Myklebust 	xs_wake_error(transport);
23924f8943f8STrond Myklebust 	xs_wake_pending(transport);
23934f8943f8STrond Myklebust }
23944f8943f8STrond Myklebust 
2395262ca07dSChuck Lever /**
239612b20ce3SBhaskar Chowdhury  * xs_local_print_stats - display AF_LOCAL socket-specific stats
2397176e21eeSChuck Lever  * @xprt: rpc_xprt struct containing statistics
2398176e21eeSChuck Lever  * @seq: output file
2399176e21eeSChuck Lever  *
2400176e21eeSChuck Lever  */
2401176e21eeSChuck Lever static void xs_local_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2402176e21eeSChuck Lever {
2403176e21eeSChuck Lever 	long idle_time = 0;
2404176e21eeSChuck Lever 
2405176e21eeSChuck Lever 	if (xprt_connected(xprt))
2406176e21eeSChuck Lever 		idle_time = (long)(jiffies - xprt->last_used) / HZ;
2407176e21eeSChuck Lever 
2408176e21eeSChuck Lever 	seq_printf(seq, "\txprt:\tlocal %lu %lu %lu %ld %lu %lu %lu "
240915a45206SAndy Adamson 			"%llu %llu %lu %llu %llu\n",
2410176e21eeSChuck Lever 			xprt->stat.bind_count,
2411176e21eeSChuck Lever 			xprt->stat.connect_count,
24128440a886SChuck Lever 			xprt->stat.connect_time / HZ,
2413176e21eeSChuck Lever 			idle_time,
2414176e21eeSChuck Lever 			xprt->stat.sends,
2415176e21eeSChuck Lever 			xprt->stat.recvs,
2416176e21eeSChuck Lever 			xprt->stat.bad_xids,
2417176e21eeSChuck Lever 			xprt->stat.req_u,
241815a45206SAndy Adamson 			xprt->stat.bklog_u,
241915a45206SAndy Adamson 			xprt->stat.max_slots,
242015a45206SAndy Adamson 			xprt->stat.sending_u,
242115a45206SAndy Adamson 			xprt->stat.pending_u);
2422176e21eeSChuck Lever }
2423176e21eeSChuck Lever 
2424176e21eeSChuck Lever /**
242512b20ce3SBhaskar Chowdhury  * xs_udp_print_stats - display UDP socket-specific stats
2426262ca07dSChuck Lever  * @xprt: rpc_xprt struct containing statistics
2427262ca07dSChuck Lever  * @seq: output file
2428262ca07dSChuck Lever  *
2429262ca07dSChuck Lever  */
2430262ca07dSChuck Lever static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2431262ca07dSChuck Lever {
2432c8475461SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2433c8475461SChuck Lever 
243415a45206SAndy Adamson 	seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %llu %llu "
243515a45206SAndy Adamson 			"%lu %llu %llu\n",
2436fbfffbd5SChuck Lever 			transport->srcport,
2437262ca07dSChuck Lever 			xprt->stat.bind_count,
2438262ca07dSChuck Lever 			xprt->stat.sends,
2439262ca07dSChuck Lever 			xprt->stat.recvs,
2440262ca07dSChuck Lever 			xprt->stat.bad_xids,
2441262ca07dSChuck Lever 			xprt->stat.req_u,
244215a45206SAndy Adamson 			xprt->stat.bklog_u,
244315a45206SAndy Adamson 			xprt->stat.max_slots,
244415a45206SAndy Adamson 			xprt->stat.sending_u,
244515a45206SAndy Adamson 			xprt->stat.pending_u);
2446262ca07dSChuck Lever }
2447262ca07dSChuck Lever 
2448262ca07dSChuck Lever /**
244912b20ce3SBhaskar Chowdhury  * xs_tcp_print_stats - display TCP socket-specific stats
2450262ca07dSChuck Lever  * @xprt: rpc_xprt struct containing statistics
2451262ca07dSChuck Lever  * @seq: output file
2452262ca07dSChuck Lever  *
2453262ca07dSChuck Lever  */
2454262ca07dSChuck Lever static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2455262ca07dSChuck Lever {
2456c8475461SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2457262ca07dSChuck Lever 	long idle_time = 0;
2458262ca07dSChuck Lever 
2459262ca07dSChuck Lever 	if (xprt_connected(xprt))
2460262ca07dSChuck Lever 		idle_time = (long)(jiffies - xprt->last_used) / HZ;
2461262ca07dSChuck Lever 
246215a45206SAndy Adamson 	seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu "
246315a45206SAndy Adamson 			"%llu %llu %lu %llu %llu\n",
2464fbfffbd5SChuck Lever 			transport->srcport,
2465262ca07dSChuck Lever 			xprt->stat.bind_count,
2466262ca07dSChuck Lever 			xprt->stat.connect_count,
24678440a886SChuck Lever 			xprt->stat.connect_time / HZ,
2468262ca07dSChuck Lever 			idle_time,
2469262ca07dSChuck Lever 			xprt->stat.sends,
2470262ca07dSChuck Lever 			xprt->stat.recvs,
2471262ca07dSChuck Lever 			xprt->stat.bad_xids,
2472262ca07dSChuck Lever 			xprt->stat.req_u,
247315a45206SAndy Adamson 			xprt->stat.bklog_u,
247415a45206SAndy Adamson 			xprt->stat.max_slots,
247515a45206SAndy Adamson 			xprt->stat.sending_u,
247615a45206SAndy Adamson 			xprt->stat.pending_u);
2477262ca07dSChuck Lever }
2478262ca07dSChuck Lever 
24794cfc7e60SRahul Iyer /*
24804cfc7e60SRahul Iyer  * Allocate a bunch of pages for a scratch buffer for the rpc code. The reason
24814cfc7e60SRahul Iyer  * we allocate pages instead doing a kmalloc like rpc_malloc is because we want
24824cfc7e60SRahul Iyer  * to use the server side send routines.
24834cfc7e60SRahul Iyer  */
24845fe6eaa1SChuck Lever static int bc_malloc(struct rpc_task *task)
24854cfc7e60SRahul Iyer {
24865fe6eaa1SChuck Lever 	struct rpc_rqst *rqst = task->tk_rqstp;
24875fe6eaa1SChuck Lever 	size_t size = rqst->rq_callsize;
24884cfc7e60SRahul Iyer 	struct page *page;
24894cfc7e60SRahul Iyer 	struct rpc_buffer *buf;
24904cfc7e60SRahul Iyer 
24915fe6eaa1SChuck Lever 	if (size > PAGE_SIZE - sizeof(struct rpc_buffer)) {
24925fe6eaa1SChuck Lever 		WARN_ONCE(1, "xprtsock: large bc buffer request (size %zu)\n",
24935fe6eaa1SChuck Lever 			  size);
24945fe6eaa1SChuck Lever 		return -EINVAL;
24955fe6eaa1SChuck Lever 	}
24964cfc7e60SRahul Iyer 
2497b8a13d03SWeston Andros Adamson 	page = alloc_page(GFP_KERNEL);
24984cfc7e60SRahul Iyer 	if (!page)
24995fe6eaa1SChuck Lever 		return -ENOMEM;
25004cfc7e60SRahul Iyer 
25014cfc7e60SRahul Iyer 	buf = page_address(page);
25024cfc7e60SRahul Iyer 	buf->len = PAGE_SIZE;
25034cfc7e60SRahul Iyer 
25045fe6eaa1SChuck Lever 	rqst->rq_buffer = buf->data;
250518e601d6SJeff Layton 	rqst->rq_rbuffer = (char *)rqst->rq_buffer + rqst->rq_callsize;
25065fe6eaa1SChuck Lever 	return 0;
25074cfc7e60SRahul Iyer }
25084cfc7e60SRahul Iyer 
25094cfc7e60SRahul Iyer /*
25104cfc7e60SRahul Iyer  * Free the space allocated in the bc_alloc routine
25114cfc7e60SRahul Iyer  */
25123435c74aSChuck Lever static void bc_free(struct rpc_task *task)
25134cfc7e60SRahul Iyer {
25143435c74aSChuck Lever 	void *buffer = task->tk_rqstp->rq_buffer;
25154cfc7e60SRahul Iyer 	struct rpc_buffer *buf;
25164cfc7e60SRahul Iyer 
25174cfc7e60SRahul Iyer 	buf = container_of(buffer, struct rpc_buffer, data);
25184cfc7e60SRahul Iyer 	free_page((unsigned long)buf);
25194cfc7e60SRahul Iyer }
25204cfc7e60SRahul Iyer 
25214cfc7e60SRahul Iyer static int bc_sendto(struct rpc_rqst *req)
25224cfc7e60SRahul Iyer {
2523da1661b9SChuck Lever 	struct xdr_buf *xdr = &req->rq_snd_buf;
25244cfc7e60SRahul Iyer 	struct sock_xprt *transport =
2525067fb11bSChuck Lever 			container_of(req->rq_xprt, struct sock_xprt, xprt);
2526067fb11bSChuck Lever 	struct msghdr msg = {
2527da1661b9SChuck Lever 		.msg_flags	= 0,
2528067fb11bSChuck Lever 	};
2529067fb11bSChuck Lever 	rpc_fraghdr marker = cpu_to_be32(RPC_LAST_STREAM_FRAGMENT |
2530da1661b9SChuck Lever 					 (u32)xdr->len);
2531da1661b9SChuck Lever 	unsigned int sent = 0;
2532da1661b9SChuck Lever 	int err;
25334cfc7e60SRahul Iyer 
25348729aabaSChuck Lever 	req->rq_xtime = ktime_get();
2535da1661b9SChuck Lever 	err = xprt_sock_sendmsg(transport->sock, &msg, xdr, 0, marker, &sent);
2536da1661b9SChuck Lever 	xdr_free_bvec(xdr);
2537da1661b9SChuck Lever 	if (err < 0 || sent != (xdr->len + sizeof(marker)))
2538067fb11bSChuck Lever 		return -EAGAIN;
2539da1661b9SChuck Lever 	return sent;
25404cfc7e60SRahul Iyer }
25414cfc7e60SRahul Iyer 
2542ca4faf54SChuck Lever /**
2543ca4faf54SChuck Lever  * bc_send_request - Send a backchannel Call on a TCP socket
2544ca4faf54SChuck Lever  * @req: rpc_rqst containing Call message to be sent
2545ca4faf54SChuck Lever  *
2546ca4faf54SChuck Lever  * xpt_mutex ensures @rqstp's whole message is written to the socket
2547ca4faf54SChuck Lever  * without interruption.
2548ca4faf54SChuck Lever  *
2549ca4faf54SChuck Lever  * Return values:
2550ca4faf54SChuck Lever  *   %0 if the message was sent successfully
2551ca4faf54SChuck Lever  *   %ENOTCONN if the message was not sent
25524cfc7e60SRahul Iyer  */
2553adfa7144STrond Myklebust static int bc_send_request(struct rpc_rqst *req)
25544cfc7e60SRahul Iyer {
25554cfc7e60SRahul Iyer 	struct svc_xprt	*xprt;
25567fc56136SAndrzej Hajda 	int len;
25574cfc7e60SRahul Iyer 
25584cfc7e60SRahul Iyer 	/*
25594cfc7e60SRahul Iyer 	 * Get the server socket associated with this callback xprt
25604cfc7e60SRahul Iyer 	 */
25614cfc7e60SRahul Iyer 	xprt = req->rq_xprt->bc_xprt;
25624cfc7e60SRahul Iyer 
25634cfc7e60SRahul Iyer 	/*
25644cfc7e60SRahul Iyer 	 * Grab the mutex to serialize data as the connection is shared
25654cfc7e60SRahul Iyer 	 * with the fore channel
25664cfc7e60SRahul Iyer 	 */
2567c544577dSTrond Myklebust 	mutex_lock(&xprt->xpt_mutex);
25684cfc7e60SRahul Iyer 	if (test_bit(XPT_DEAD, &xprt->xpt_flags))
25694cfc7e60SRahul Iyer 		len = -ENOTCONN;
25704cfc7e60SRahul Iyer 	else
25714cfc7e60SRahul Iyer 		len = bc_sendto(req);
25724cfc7e60SRahul Iyer 	mutex_unlock(&xprt->xpt_mutex);
25734cfc7e60SRahul Iyer 
25744cfc7e60SRahul Iyer 	if (len > 0)
25754cfc7e60SRahul Iyer 		len = 0;
25764cfc7e60SRahul Iyer 
25774cfc7e60SRahul Iyer 	return len;
25784cfc7e60SRahul Iyer }
25794cfc7e60SRahul Iyer 
25804cfc7e60SRahul Iyer /*
25814cfc7e60SRahul Iyer  * The close routine. Since this is client initiated, we do nothing
25824cfc7e60SRahul Iyer  */
25834cfc7e60SRahul Iyer 
25844cfc7e60SRahul Iyer static void bc_close(struct rpc_xprt *xprt)
25854cfc7e60SRahul Iyer {
25866221f1d9SChuck Lever 	xprt_disconnect_done(xprt);
25874cfc7e60SRahul Iyer }
25884cfc7e60SRahul Iyer 
25894cfc7e60SRahul Iyer /*
25904cfc7e60SRahul Iyer  * The xprt destroy routine. Again, because this connection is client
25914cfc7e60SRahul Iyer  * initiated, we do nothing
25924cfc7e60SRahul Iyer  */
25934cfc7e60SRahul Iyer 
25944cfc7e60SRahul Iyer static void bc_destroy(struct rpc_xprt *xprt)
25954cfc7e60SRahul Iyer {
259647f72efaSKinglong Mee 	dprintk("RPC:       bc_destroy xprt %p\n", xprt);
259747f72efaSKinglong Mee 
259847f72efaSKinglong Mee 	xs_xprt_free(xprt);
259947f72efaSKinglong Mee 	module_put(THIS_MODULE);
26004cfc7e60SRahul Iyer }
26014cfc7e60SRahul Iyer 
2602d31ae254SChuck Lever static const struct rpc_xprt_ops xs_local_ops = {
2603176e21eeSChuck Lever 	.reserve_xprt		= xprt_reserve_xprt,
26044cd34e7cSTrond Myklebust 	.release_xprt		= xprt_release_xprt,
2605f39c1bfbSTrond Myklebust 	.alloc_slot		= xprt_alloc_slot,
2606a9cde23aSChuck Lever 	.free_slot		= xprt_free_slot,
2607176e21eeSChuck Lever 	.rpcbind		= xs_local_rpcbind,
2608176e21eeSChuck Lever 	.set_port		= xs_local_set_port,
2609dc107402SJ. Bruce Fields 	.connect		= xs_local_connect,
2610176e21eeSChuck Lever 	.buf_alloc		= rpc_malloc,
2611176e21eeSChuck Lever 	.buf_free		= rpc_free,
2612550aebfeSTrond Myklebust 	.prepare_request	= xs_stream_prepare_request,
2613176e21eeSChuck Lever 	.send_request		= xs_local_send_request,
26148ba6a92dSTrond Myklebust 	.wait_for_reply_request	= xprt_wait_for_reply_request_def,
2615176e21eeSChuck Lever 	.close			= xs_close,
2616a1311d87STrond Myklebust 	.destroy		= xs_destroy,
2617176e21eeSChuck Lever 	.print_stats		= xs_local_print_stats,
2618d67fa4d8SJeff Layton 	.enable_swap		= xs_enable_swap,
2619d67fa4d8SJeff Layton 	.disable_swap		= xs_disable_swap,
2620176e21eeSChuck Lever };
2621176e21eeSChuck Lever 
2622d31ae254SChuck Lever static const struct rpc_xprt_ops xs_udp_ops = {
262343118c29SChuck Lever 	.set_buffer_size	= xs_udp_set_buffer_size,
262412a80469SChuck Lever 	.reserve_xprt		= xprt_reserve_xprt_cong,
262549e9a890SChuck Lever 	.release_xprt		= xprt_release_xprt_cong,
2626f39c1bfbSTrond Myklebust 	.alloc_slot		= xprt_alloc_slot,
2627a9cde23aSChuck Lever 	.free_slot		= xprt_free_slot,
262845160d62SChuck Lever 	.rpcbind		= rpcb_getport_async,
262992200412SChuck Lever 	.set_port		= xs_set_port,
26309903cd1cSChuck Lever 	.connect		= xs_connect,
263102107148SChuck Lever 	.buf_alloc		= rpc_malloc,
263202107148SChuck Lever 	.buf_free		= rpc_free,
2633262965f5SChuck Lever 	.send_request		= xs_udp_send_request,
26348ba6a92dSTrond Myklebust 	.wait_for_reply_request	= xprt_wait_for_reply_request_rtt,
263546c0ee8bSChuck Lever 	.timer			= xs_udp_timer,
2636a58dd398SChuck Lever 	.release_request	= xprt_release_rqst_cong,
2637262965f5SChuck Lever 	.close			= xs_close,
2638262965f5SChuck Lever 	.destroy		= xs_destroy,
2639262ca07dSChuck Lever 	.print_stats		= xs_udp_print_stats,
2640d67fa4d8SJeff Layton 	.enable_swap		= xs_enable_swap,
2641d67fa4d8SJeff Layton 	.disable_swap		= xs_disable_swap,
26424a068258SChuck Lever 	.inject_disconnect	= xs_inject_disconnect,
2643262965f5SChuck Lever };
2644262965f5SChuck Lever 
2645d31ae254SChuck Lever static const struct rpc_xprt_ops xs_tcp_ops = {
264612a80469SChuck Lever 	.reserve_xprt		= xprt_reserve_xprt,
26474cd34e7cSTrond Myklebust 	.release_xprt		= xprt_release_xprt,
264836bd7de9STrond Myklebust 	.alloc_slot		= xprt_alloc_slot,
2649a9cde23aSChuck Lever 	.free_slot		= xprt_free_slot,
265045160d62SChuck Lever 	.rpcbind		= rpcb_getport_async,
265192200412SChuck Lever 	.set_port		= xs_set_port,
26520b9e7943STrond Myklebust 	.connect		= xs_connect,
265302107148SChuck Lever 	.buf_alloc		= rpc_malloc,
265402107148SChuck Lever 	.buf_free		= rpc_free,
2655277e4ab7STrond Myklebust 	.prepare_request	= xs_stream_prepare_request,
2656262965f5SChuck Lever 	.send_request		= xs_tcp_send_request,
26578ba6a92dSTrond Myklebust 	.wait_for_reply_request	= xprt_wait_for_reply_request_def,
2658c627d31bSTrond Myklebust 	.close			= xs_tcp_shutdown,
26599903cd1cSChuck Lever 	.destroy		= xs_destroy,
26607196dbb0STrond Myklebust 	.set_connect_timeout	= xs_tcp_set_connect_timeout,
2661262ca07dSChuck Lever 	.print_stats		= xs_tcp_print_stats,
2662d67fa4d8SJeff Layton 	.enable_swap		= xs_enable_swap,
2663d67fa4d8SJeff Layton 	.disable_swap		= xs_disable_swap,
26644a068258SChuck Lever 	.inject_disconnect	= xs_inject_disconnect,
266542e5c3e2SChuck Lever #ifdef CONFIG_SUNRPC_BACKCHANNEL
266642e5c3e2SChuck Lever 	.bc_setup		= xprt_setup_bc,
26676b26cc8cSChuck Lever 	.bc_maxpayload		= xs_tcp_bc_maxpayload,
26687402a4feSTrond Myklebust 	.bc_num_slots		= xprt_bc_max_slots,
266942e5c3e2SChuck Lever 	.bc_free_rqst		= xprt_free_bc_rqst,
267042e5c3e2SChuck Lever 	.bc_destroy		= xprt_destroy_bc,
267142e5c3e2SChuck Lever #endif
2672a246b010SChuck Lever };
2673a246b010SChuck Lever 
26744cfc7e60SRahul Iyer /*
26754cfc7e60SRahul Iyer  * The rpc_xprt_ops for the server backchannel
26764cfc7e60SRahul Iyer  */
26774cfc7e60SRahul Iyer 
2678d31ae254SChuck Lever static const struct rpc_xprt_ops bc_tcp_ops = {
26794cfc7e60SRahul Iyer 	.reserve_xprt		= xprt_reserve_xprt,
26804cfc7e60SRahul Iyer 	.release_xprt		= xprt_release_xprt,
268184e28a30SBryan Schumaker 	.alloc_slot		= xprt_alloc_slot,
2682a9cde23aSChuck Lever 	.free_slot		= xprt_free_slot,
26834cfc7e60SRahul Iyer 	.buf_alloc		= bc_malloc,
26844cfc7e60SRahul Iyer 	.buf_free		= bc_free,
26854cfc7e60SRahul Iyer 	.send_request		= bc_send_request,
26868ba6a92dSTrond Myklebust 	.wait_for_reply_request	= xprt_wait_for_reply_request_def,
26874cfc7e60SRahul Iyer 	.close			= bc_close,
26884cfc7e60SRahul Iyer 	.destroy		= bc_destroy,
26894cfc7e60SRahul Iyer 	.print_stats		= xs_tcp_print_stats,
2690d67fa4d8SJeff Layton 	.enable_swap		= xs_enable_swap,
2691d67fa4d8SJeff Layton 	.disable_swap		= xs_disable_swap,
26924a068258SChuck Lever 	.inject_disconnect	= xs_inject_disconnect,
26934cfc7e60SRahul Iyer };
26944cfc7e60SRahul Iyer 
269592476850SChuck Lever static int xs_init_anyaddr(const int family, struct sockaddr *sap)
269692476850SChuck Lever {
269792476850SChuck Lever 	static const struct sockaddr_in sin = {
269892476850SChuck Lever 		.sin_family		= AF_INET,
269992476850SChuck Lever 		.sin_addr.s_addr	= htonl(INADDR_ANY),
270092476850SChuck Lever 	};
270192476850SChuck Lever 	static const struct sockaddr_in6 sin6 = {
270292476850SChuck Lever 		.sin6_family		= AF_INET6,
270392476850SChuck Lever 		.sin6_addr		= IN6ADDR_ANY_INIT,
270492476850SChuck Lever 	};
270592476850SChuck Lever 
270692476850SChuck Lever 	switch (family) {
2707176e21eeSChuck Lever 	case AF_LOCAL:
2708176e21eeSChuck Lever 		break;
270992476850SChuck Lever 	case AF_INET:
271092476850SChuck Lever 		memcpy(sap, &sin, sizeof(sin));
271192476850SChuck Lever 		break;
271292476850SChuck Lever 	case AF_INET6:
271392476850SChuck Lever 		memcpy(sap, &sin6, sizeof(sin6));
271492476850SChuck Lever 		break;
271592476850SChuck Lever 	default:
271692476850SChuck Lever 		dprintk("RPC:       %s: Bad address family\n", __func__);
271792476850SChuck Lever 		return -EAFNOSUPPORT;
271892476850SChuck Lever 	}
271992476850SChuck Lever 	return 0;
272092476850SChuck Lever }
272192476850SChuck Lever 
27223c341b0bS\"Talpey, Thomas\ static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args,
2723d9ba131dSTrond Myklebust 				      unsigned int slot_table_size,
2724d9ba131dSTrond Myklebust 				      unsigned int max_slot_table_size)
2725c8541ecdSChuck Lever {
2726c8541ecdSChuck Lever 	struct rpc_xprt *xprt;
2727ffc2e518SChuck Lever 	struct sock_xprt *new;
2728c8541ecdSChuck Lever 
272996802a09SFrank van Maarseveen 	if (args->addrlen > sizeof(xprt->addr)) {
2730c8541ecdSChuck Lever 		dprintk("RPC:       xs_setup_xprt: address too large\n");
2731c8541ecdSChuck Lever 		return ERR_PTR(-EBADF);
2732c8541ecdSChuck Lever 	}
2733c8541ecdSChuck Lever 
2734d9ba131dSTrond Myklebust 	xprt = xprt_alloc(args->net, sizeof(*new), slot_table_size,
2735d9ba131dSTrond Myklebust 			max_slot_table_size);
2736bd1722d4SPavel Emelyanov 	if (xprt == NULL) {
273746121cf7SChuck Lever 		dprintk("RPC:       xs_setup_xprt: couldn't allocate "
273846121cf7SChuck Lever 				"rpc_xprt\n");
2739c8541ecdSChuck Lever 		return ERR_PTR(-ENOMEM);
2740c8541ecdSChuck Lever 	}
2741c8541ecdSChuck Lever 
2742bd1722d4SPavel Emelyanov 	new = container_of(xprt, struct sock_xprt, xprt);
2743edc1b01cSTrond Myklebust 	mutex_init(&new->recv_mutex);
274496802a09SFrank van Maarseveen 	memcpy(&xprt->addr, args->dstaddr, args->addrlen);
274596802a09SFrank van Maarseveen 	xprt->addrlen = args->addrlen;
2746d3bc9a1dSFrank van Maarseveen 	if (args->srcaddr)
2747fbfffbd5SChuck Lever 		memcpy(&new->srcaddr, args->srcaddr, args->addrlen);
274892476850SChuck Lever 	else {
274992476850SChuck Lever 		int err;
275092476850SChuck Lever 		err = xs_init_anyaddr(args->dstaddr->sa_family,
275192476850SChuck Lever 					(struct sockaddr *)&new->srcaddr);
27522aa13531SStanislav Kinsbursky 		if (err != 0) {
27532aa13531SStanislav Kinsbursky 			xprt_free(xprt);
275492476850SChuck Lever 			return ERR_PTR(err);
275592476850SChuck Lever 		}
27562aa13531SStanislav Kinsbursky 	}
2757c8541ecdSChuck Lever 
2758c8541ecdSChuck Lever 	return xprt;
2759c8541ecdSChuck Lever }
2760c8541ecdSChuck Lever 
2761176e21eeSChuck Lever static const struct rpc_timeout xs_local_default_timeout = {
2762176e21eeSChuck Lever 	.to_initval = 10 * HZ,
2763176e21eeSChuck Lever 	.to_maxval = 10 * HZ,
2764176e21eeSChuck Lever 	.to_retries = 2,
2765176e21eeSChuck Lever };
2766176e21eeSChuck Lever 
2767176e21eeSChuck Lever /**
2768176e21eeSChuck Lever  * xs_setup_local - Set up transport to use an AF_LOCAL socket
2769176e21eeSChuck Lever  * @args: rpc transport creation arguments
2770176e21eeSChuck Lever  *
2771176e21eeSChuck Lever  * AF_LOCAL is a "tpi_cots_ord" transport, just like TCP
2772176e21eeSChuck Lever  */
2773176e21eeSChuck Lever static struct rpc_xprt *xs_setup_local(struct xprt_create *args)
2774176e21eeSChuck Lever {
2775176e21eeSChuck Lever 	struct sockaddr_un *sun = (struct sockaddr_un *)args->dstaddr;
2776176e21eeSChuck Lever 	struct sock_xprt *transport;
2777176e21eeSChuck Lever 	struct rpc_xprt *xprt;
2778176e21eeSChuck Lever 	struct rpc_xprt *ret;
2779176e21eeSChuck Lever 
2780d9ba131dSTrond Myklebust 	xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
2781d9ba131dSTrond Myklebust 			xprt_max_tcp_slot_table_entries);
2782176e21eeSChuck Lever 	if (IS_ERR(xprt))
2783176e21eeSChuck Lever 		return xprt;
2784176e21eeSChuck Lever 	transport = container_of(xprt, struct sock_xprt, xprt);
2785176e21eeSChuck Lever 
2786176e21eeSChuck Lever 	xprt->prot = 0;
2787*d3abc739SOlga Kornievskaia 	xprt->xprt_class = &xs_local_transport;
2788176e21eeSChuck Lever 	xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2789176e21eeSChuck Lever 
2790176e21eeSChuck Lever 	xprt->bind_timeout = XS_BIND_TO;
2791176e21eeSChuck Lever 	xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2792176e21eeSChuck Lever 	xprt->idle_timeout = XS_IDLE_DISC_TO;
2793176e21eeSChuck Lever 
2794176e21eeSChuck Lever 	xprt->ops = &xs_local_ops;
2795176e21eeSChuck Lever 	xprt->timeout = &xs_local_default_timeout;
2796176e21eeSChuck Lever 
2797550aebfeSTrond Myklebust 	INIT_WORK(&transport->recv_worker, xs_stream_data_receive_workfn);
27984f8943f8STrond Myklebust 	INIT_WORK(&transport->error_worker, xs_error_handle);
2799550aebfeSTrond Myklebust 	INIT_DELAYED_WORK(&transport->connect_worker, xs_dummy_setup_socket);
280093dc41bdSNeilBrown 
2801176e21eeSChuck Lever 	switch (sun->sun_family) {
2802176e21eeSChuck Lever 	case AF_LOCAL:
2803176e21eeSChuck Lever 		if (sun->sun_path[0] != '/') {
2804176e21eeSChuck Lever 			dprintk("RPC:       bad AF_LOCAL address: %s\n",
2805176e21eeSChuck Lever 					sun->sun_path);
2806176e21eeSChuck Lever 			ret = ERR_PTR(-EINVAL);
2807176e21eeSChuck Lever 			goto out_err;
2808176e21eeSChuck Lever 		}
2809176e21eeSChuck Lever 		xprt_set_bound(xprt);
2810176e21eeSChuck Lever 		xs_format_peer_addresses(xprt, "local", RPCBIND_NETID_LOCAL);
28117073ea87SJ. Bruce Fields 		ret = ERR_PTR(xs_local_setup_socket(transport));
28127073ea87SJ. Bruce Fields 		if (ret)
28137073ea87SJ. Bruce Fields 			goto out_err;
2814176e21eeSChuck Lever 		break;
2815176e21eeSChuck Lever 	default:
2816176e21eeSChuck Lever 		ret = ERR_PTR(-EAFNOSUPPORT);
2817176e21eeSChuck Lever 		goto out_err;
2818176e21eeSChuck Lever 	}
2819176e21eeSChuck Lever 
2820176e21eeSChuck Lever 	dprintk("RPC:       set up xprt to %s via AF_LOCAL\n",
2821176e21eeSChuck Lever 			xprt->address_strings[RPC_DISPLAY_ADDR]);
2822176e21eeSChuck Lever 
2823176e21eeSChuck Lever 	if (try_module_get(THIS_MODULE))
2824176e21eeSChuck Lever 		return xprt;
2825176e21eeSChuck Lever 	ret = ERR_PTR(-EINVAL);
2826176e21eeSChuck Lever out_err:
2827315f3812SKinglong Mee 	xs_xprt_free(xprt);
2828176e21eeSChuck Lever 	return ret;
2829176e21eeSChuck Lever }
2830176e21eeSChuck Lever 
28312881ae74STrond Myklebust static const struct rpc_timeout xs_udp_default_timeout = {
28322881ae74STrond Myklebust 	.to_initval = 5 * HZ,
28332881ae74STrond Myklebust 	.to_maxval = 30 * HZ,
28342881ae74STrond Myklebust 	.to_increment = 5 * HZ,
28352881ae74STrond Myklebust 	.to_retries = 5,
28362881ae74STrond Myklebust };
28372881ae74STrond Myklebust 
28389903cd1cSChuck Lever /**
28399903cd1cSChuck Lever  * xs_setup_udp - Set up transport to use a UDP socket
284096802a09SFrank van Maarseveen  * @args: rpc transport creation arguments
28419903cd1cSChuck Lever  *
28429903cd1cSChuck Lever  */
2843483066d6SAdrian Bunk static struct rpc_xprt *xs_setup_udp(struct xprt_create *args)
2844a246b010SChuck Lever {
28458f9d5b1aSChuck Lever 	struct sockaddr *addr = args->dstaddr;
2846c8541ecdSChuck Lever 	struct rpc_xprt *xprt;
2847c8475461SChuck Lever 	struct sock_xprt *transport;
28480a68b0beSJ. Bruce Fields 	struct rpc_xprt *ret;
2849a246b010SChuck Lever 
2850d9ba131dSTrond Myklebust 	xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries,
2851d9ba131dSTrond Myklebust 			xprt_udp_slot_table_entries);
2852c8541ecdSChuck Lever 	if (IS_ERR(xprt))
2853c8541ecdSChuck Lever 		return xprt;
2854c8475461SChuck Lever 	transport = container_of(xprt, struct sock_xprt, xprt);
2855a246b010SChuck Lever 
2856ec739ef0SChuck Lever 	xprt->prot = IPPROTO_UDP;
2857*d3abc739SOlga Kornievskaia 	xprt->xprt_class = &xs_udp_transport;
2858a246b010SChuck Lever 	/* XXX: header size can vary due to auth type, IPv6, etc. */
2859a246b010SChuck Lever 	xprt->max_payload = (1U << 16) - (MAX_HEADER << 3);
2860a246b010SChuck Lever 
286103bf4b70SChuck Lever 	xprt->bind_timeout = XS_BIND_TO;
286203bf4b70SChuck Lever 	xprt->reestablish_timeout = XS_UDP_REEST_TO;
286303bf4b70SChuck Lever 	xprt->idle_timeout = XS_IDLE_DISC_TO;
2864a246b010SChuck Lever 
2865262965f5SChuck Lever 	xprt->ops = &xs_udp_ops;
2866a246b010SChuck Lever 
2867ba7392bbSTrond Myklebust 	xprt->timeout = &xs_udp_default_timeout;
2868a246b010SChuck Lever 
2869f9b2ee71STrond Myklebust 	INIT_WORK(&transport->recv_worker, xs_udp_data_receive_workfn);
28704f8943f8STrond Myklebust 	INIT_WORK(&transport->error_worker, xs_error_handle);
2871edc1b01cSTrond Myklebust 	INIT_DELAYED_WORK(&transport->connect_worker, xs_udp_setup_socket);
2872edc1b01cSTrond Myklebust 
28738f9d5b1aSChuck Lever 	switch (addr->sa_family) {
28748f9d5b1aSChuck Lever 	case AF_INET:
28758f9d5b1aSChuck Lever 		if (((struct sockaddr_in *)addr)->sin_port != htons(0))
28768f9d5b1aSChuck Lever 			xprt_set_bound(xprt);
28778f9d5b1aSChuck Lever 
28789dc3b095SChuck Lever 		xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP);
28798f9d5b1aSChuck Lever 		break;
28808f9d5b1aSChuck Lever 	case AF_INET6:
28818f9d5b1aSChuck Lever 		if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
28828f9d5b1aSChuck Lever 			xprt_set_bound(xprt);
28838f9d5b1aSChuck Lever 
28849dc3b095SChuck Lever 		xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6);
28858f9d5b1aSChuck Lever 		break;
28868f9d5b1aSChuck Lever 	default:
28870a68b0beSJ. Bruce Fields 		ret = ERR_PTR(-EAFNOSUPPORT);
28880a68b0beSJ. Bruce Fields 		goto out_err;
28898f9d5b1aSChuck Lever 	}
28908f9d5b1aSChuck Lever 
2891c740eff8SChuck Lever 	if (xprt_bound(xprt))
2892c740eff8SChuck Lever 		dprintk("RPC:       set up xprt to %s (port %s) via %s\n",
2893c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_ADDR],
2894c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_PORT],
2895c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_PROTO]);
2896c740eff8SChuck Lever 	else
2897c740eff8SChuck Lever 		dprintk("RPC:       set up xprt to %s (autobind) via %s\n",
2898c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_ADDR],
2899c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_PROTO]);
2900edb267a6SChuck Lever 
2901bc25571eS\"Talpey, Thomas\ 	if (try_module_get(THIS_MODULE))
2902c8541ecdSChuck Lever 		return xprt;
29030a68b0beSJ. Bruce Fields 	ret = ERR_PTR(-EINVAL);
29040a68b0beSJ. Bruce Fields out_err:
2905315f3812SKinglong Mee 	xs_xprt_free(xprt);
29060a68b0beSJ. Bruce Fields 	return ret;
2907a246b010SChuck Lever }
2908a246b010SChuck Lever 
29092881ae74STrond Myklebust static const struct rpc_timeout xs_tcp_default_timeout = {
29102881ae74STrond Myklebust 	.to_initval = 60 * HZ,
29112881ae74STrond Myklebust 	.to_maxval = 60 * HZ,
29122881ae74STrond Myklebust 	.to_retries = 2,
29132881ae74STrond Myklebust };
29142881ae74STrond Myklebust 
29159903cd1cSChuck Lever /**
29169903cd1cSChuck Lever  * xs_setup_tcp - Set up transport to use a TCP socket
291796802a09SFrank van Maarseveen  * @args: rpc transport creation arguments
29189903cd1cSChuck Lever  *
29199903cd1cSChuck Lever  */
2920483066d6SAdrian Bunk static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args)
2921a246b010SChuck Lever {
29228f9d5b1aSChuck Lever 	struct sockaddr *addr = args->dstaddr;
2923c8541ecdSChuck Lever 	struct rpc_xprt *xprt;
2924c8475461SChuck Lever 	struct sock_xprt *transport;
29250a68b0beSJ. Bruce Fields 	struct rpc_xprt *ret;
2926b7993cebSTrond Myklebust 	unsigned int max_slot_table_size = xprt_max_tcp_slot_table_entries;
2927b7993cebSTrond Myklebust 
2928b7993cebSTrond Myklebust 	if (args->flags & XPRT_CREATE_INFINITE_SLOTS)
2929b7993cebSTrond Myklebust 		max_slot_table_size = RPC_MAX_SLOT_TABLE_LIMIT;
2930a246b010SChuck Lever 
2931d9ba131dSTrond Myklebust 	xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
2932b7993cebSTrond Myklebust 			max_slot_table_size);
2933c8541ecdSChuck Lever 	if (IS_ERR(xprt))
2934c8541ecdSChuck Lever 		return xprt;
2935c8475461SChuck Lever 	transport = container_of(xprt, struct sock_xprt, xprt);
2936a246b010SChuck Lever 
2937ec739ef0SChuck Lever 	xprt->prot = IPPROTO_TCP;
2938*d3abc739SOlga Kornievskaia 	xprt->xprt_class = &xs_tcp_transport;
2939808012fbSChuck Lever 	xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2940a246b010SChuck Lever 
294103bf4b70SChuck Lever 	xprt->bind_timeout = XS_BIND_TO;
294203bf4b70SChuck Lever 	xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
294303bf4b70SChuck Lever 	xprt->idle_timeout = XS_IDLE_DISC_TO;
2944a246b010SChuck Lever 
2945262965f5SChuck Lever 	xprt->ops = &xs_tcp_ops;
2946ba7392bbSTrond Myklebust 	xprt->timeout = &xs_tcp_default_timeout;
2947a246b010SChuck Lever 
29483851f1cdSTrond Myklebust 	xprt->max_reconnect_timeout = xprt->timeout->to_maxval;
29497196dbb0STrond Myklebust 	xprt->connect_timeout = xprt->timeout->to_initval *
29507196dbb0STrond Myklebust 		(xprt->timeout->to_retries + 1);
29513851f1cdSTrond Myklebust 
2952c50b8ee0STrond Myklebust 	INIT_WORK(&transport->recv_worker, xs_stream_data_receive_workfn);
29534f8943f8STrond Myklebust 	INIT_WORK(&transport->error_worker, xs_error_handle);
2954edc1b01cSTrond Myklebust 	INIT_DELAYED_WORK(&transport->connect_worker, xs_tcp_setup_socket);
2955edc1b01cSTrond Myklebust 
29568f9d5b1aSChuck Lever 	switch (addr->sa_family) {
29578f9d5b1aSChuck Lever 	case AF_INET:
29588f9d5b1aSChuck Lever 		if (((struct sockaddr_in *)addr)->sin_port != htons(0))
29598f9d5b1aSChuck Lever 			xprt_set_bound(xprt);
29608f9d5b1aSChuck Lever 
29619dc3b095SChuck Lever 		xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP);
29628f9d5b1aSChuck Lever 		break;
29638f9d5b1aSChuck Lever 	case AF_INET6:
29648f9d5b1aSChuck Lever 		if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
29658f9d5b1aSChuck Lever 			xprt_set_bound(xprt);
29668f9d5b1aSChuck Lever 
29679dc3b095SChuck Lever 		xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6);
29688f9d5b1aSChuck Lever 		break;
29698f9d5b1aSChuck Lever 	default:
29700a68b0beSJ. Bruce Fields 		ret = ERR_PTR(-EAFNOSUPPORT);
29710a68b0beSJ. Bruce Fields 		goto out_err;
29728f9d5b1aSChuck Lever 	}
29738f9d5b1aSChuck Lever 
2974c740eff8SChuck Lever 	if (xprt_bound(xprt))
2975c740eff8SChuck Lever 		dprintk("RPC:       set up xprt to %s (port %s) via %s\n",
2976c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_ADDR],
2977c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_PORT],
2978c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_PROTO]);
2979c740eff8SChuck Lever 	else
2980c740eff8SChuck Lever 		dprintk("RPC:       set up xprt to %s (autobind) via %s\n",
2981c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_ADDR],
2982c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_PROTO]);
2983c740eff8SChuck Lever 
2984bc25571eS\"Talpey, Thomas\ 	if (try_module_get(THIS_MODULE))
2985c8541ecdSChuck Lever 		return xprt;
29860a68b0beSJ. Bruce Fields 	ret = ERR_PTR(-EINVAL);
29870a68b0beSJ. Bruce Fields out_err:
2988315f3812SKinglong Mee 	xs_xprt_free(xprt);
29890a68b0beSJ. Bruce Fields 	return ret;
2990a246b010SChuck Lever }
2991282b32e1SChuck Lever 
2992f300babaSAlexandros Batsakis /**
2993f300babaSAlexandros Batsakis  * xs_setup_bc_tcp - Set up transport to use a TCP backchannel socket
2994f300babaSAlexandros Batsakis  * @args: rpc transport creation arguments
2995f300babaSAlexandros Batsakis  *
2996f300babaSAlexandros Batsakis  */
2997f300babaSAlexandros Batsakis static struct rpc_xprt *xs_setup_bc_tcp(struct xprt_create *args)
2998f300babaSAlexandros Batsakis {
2999f300babaSAlexandros Batsakis 	struct sockaddr *addr = args->dstaddr;
3000f300babaSAlexandros Batsakis 	struct rpc_xprt *xprt;
3001f300babaSAlexandros Batsakis 	struct sock_xprt *transport;
3002f300babaSAlexandros Batsakis 	struct svc_sock *bc_sock;
30030a68b0beSJ. Bruce Fields 	struct rpc_xprt *ret;
3004f300babaSAlexandros Batsakis 
3005d9ba131dSTrond Myklebust 	xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
3006d9ba131dSTrond Myklebust 			xprt_tcp_slot_table_entries);
3007f300babaSAlexandros Batsakis 	if (IS_ERR(xprt))
3008f300babaSAlexandros Batsakis 		return xprt;
3009f300babaSAlexandros Batsakis 	transport = container_of(xprt, struct sock_xprt, xprt);
3010f300babaSAlexandros Batsakis 
3011f300babaSAlexandros Batsakis 	xprt->prot = IPPROTO_TCP;
3012*d3abc739SOlga Kornievskaia 	xprt->xprt_class = &xs_bc_tcp_transport;
3013f300babaSAlexandros Batsakis 	xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
3014f300babaSAlexandros Batsakis 	xprt->timeout = &xs_tcp_default_timeout;
3015f300babaSAlexandros Batsakis 
3016f300babaSAlexandros Batsakis 	/* backchannel */
3017f300babaSAlexandros Batsakis 	xprt_set_bound(xprt);
3018f300babaSAlexandros Batsakis 	xprt->bind_timeout = 0;
3019f300babaSAlexandros Batsakis 	xprt->reestablish_timeout = 0;
3020f300babaSAlexandros Batsakis 	xprt->idle_timeout = 0;
3021f300babaSAlexandros Batsakis 
3022f300babaSAlexandros Batsakis 	xprt->ops = &bc_tcp_ops;
3023f300babaSAlexandros Batsakis 
3024f300babaSAlexandros Batsakis 	switch (addr->sa_family) {
3025f300babaSAlexandros Batsakis 	case AF_INET:
3026f300babaSAlexandros Batsakis 		xs_format_peer_addresses(xprt, "tcp",
3027f300babaSAlexandros Batsakis 					 RPCBIND_NETID_TCP);
3028f300babaSAlexandros Batsakis 		break;
3029f300babaSAlexandros Batsakis 	case AF_INET6:
3030f300babaSAlexandros Batsakis 		xs_format_peer_addresses(xprt, "tcp",
3031f300babaSAlexandros Batsakis 				   RPCBIND_NETID_TCP6);
3032f300babaSAlexandros Batsakis 		break;
3033f300babaSAlexandros Batsakis 	default:
30340a68b0beSJ. Bruce Fields 		ret = ERR_PTR(-EAFNOSUPPORT);
30350a68b0beSJ. Bruce Fields 		goto out_err;
3036f300babaSAlexandros Batsakis 	}
3037f300babaSAlexandros Batsakis 
3038f300babaSAlexandros Batsakis 	dprintk("RPC:       set up xprt to %s (port %s) via %s\n",
3039f300babaSAlexandros Batsakis 			xprt->address_strings[RPC_DISPLAY_ADDR],
3040f300babaSAlexandros Batsakis 			xprt->address_strings[RPC_DISPLAY_PORT],
3041f300babaSAlexandros Batsakis 			xprt->address_strings[RPC_DISPLAY_PROTO]);
3042f300babaSAlexandros Batsakis 
3043f300babaSAlexandros Batsakis 	/*
304499de8ea9SJ. Bruce Fields 	 * Once we've associated a backchannel xprt with a connection,
304528303ca3SWeng Meiling 	 * we want to keep it around as long as the connection lasts,
304628303ca3SWeng Meiling 	 * in case we need to start using it for a backchannel again;
304728303ca3SWeng Meiling 	 * this reference won't be dropped until bc_xprt is destroyed.
304899de8ea9SJ. Bruce Fields 	 */
304999de8ea9SJ. Bruce Fields 	xprt_get(xprt);
305099de8ea9SJ. Bruce Fields 	args->bc_xprt->xpt_bc_xprt = xprt;
305199de8ea9SJ. Bruce Fields 	xprt->bc_xprt = args->bc_xprt;
305299de8ea9SJ. Bruce Fields 	bc_sock = container_of(args->bc_xprt, struct svc_sock, sk_xprt);
305399de8ea9SJ. Bruce Fields 	transport->sock = bc_sock->sk_sock;
305499de8ea9SJ. Bruce Fields 	transport->inet = bc_sock->sk_sk;
305599de8ea9SJ. Bruce Fields 
305699de8ea9SJ. Bruce Fields 	/*
3057f300babaSAlexandros Batsakis 	 * Since we don't want connections for the backchannel, we set
3058f300babaSAlexandros Batsakis 	 * the xprt status to connected
3059f300babaSAlexandros Batsakis 	 */
3060f300babaSAlexandros Batsakis 	xprt_set_connected(xprt);
3061f300babaSAlexandros Batsakis 
3062f300babaSAlexandros Batsakis 	if (try_module_get(THIS_MODULE))
3063f300babaSAlexandros Batsakis 		return xprt;
3064642aab58SKinglong Mee 
3065642aab58SKinglong Mee 	args->bc_xprt->xpt_bc_xprt = NULL;
306639a9beabSJ. Bruce Fields 	args->bc_xprt->xpt_bc_xps = NULL;
306799de8ea9SJ. Bruce Fields 	xprt_put(xprt);
30680a68b0beSJ. Bruce Fields 	ret = ERR_PTR(-EINVAL);
30690a68b0beSJ. Bruce Fields out_err:
3070315f3812SKinglong Mee 	xs_xprt_free(xprt);
30710a68b0beSJ. Bruce Fields 	return ret;
3072f300babaSAlexandros Batsakis }
3073f300babaSAlexandros Batsakis 
3074176e21eeSChuck Lever static struct xprt_class	xs_local_transport = {
3075176e21eeSChuck Lever 	.list		= LIST_HEAD_INIT(xs_local_transport.list),
3076176e21eeSChuck Lever 	.name		= "named UNIX socket",
3077176e21eeSChuck Lever 	.owner		= THIS_MODULE,
3078176e21eeSChuck Lever 	.ident		= XPRT_TRANSPORT_LOCAL,
3079176e21eeSChuck Lever 	.setup		= xs_setup_local,
3080d5aa6b22STrond Myklebust 	.netid		= { "" },
3081176e21eeSChuck Lever };
3082176e21eeSChuck Lever 
3083bc25571eS\"Talpey, Thomas\ static struct xprt_class	xs_udp_transport = {
3084bc25571eS\"Talpey, Thomas\ 	.list		= LIST_HEAD_INIT(xs_udp_transport.list),
3085bc25571eS\"Talpey, Thomas\ 	.name		= "udp",
3086bc25571eS\"Talpey, Thomas\ 	.owner		= THIS_MODULE,
3087f300babaSAlexandros Batsakis 	.ident		= XPRT_TRANSPORT_UDP,
3088bc25571eS\"Talpey, Thomas\ 	.setup		= xs_setup_udp,
3089d5aa6b22STrond Myklebust 	.netid		= { "udp", "udp6", "" },
3090bc25571eS\"Talpey, Thomas\ };
3091bc25571eS\"Talpey, Thomas\ 
3092bc25571eS\"Talpey, Thomas\ static struct xprt_class	xs_tcp_transport = {
3093bc25571eS\"Talpey, Thomas\ 	.list		= LIST_HEAD_INIT(xs_tcp_transport.list),
3094bc25571eS\"Talpey, Thomas\ 	.name		= "tcp",
3095bc25571eS\"Talpey, Thomas\ 	.owner		= THIS_MODULE,
3096f300babaSAlexandros Batsakis 	.ident		= XPRT_TRANSPORT_TCP,
3097bc25571eS\"Talpey, Thomas\ 	.setup		= xs_setup_tcp,
3098d5aa6b22STrond Myklebust 	.netid		= { "tcp", "tcp6", "" },
3099bc25571eS\"Talpey, Thomas\ };
3100bc25571eS\"Talpey, Thomas\ 
3101f300babaSAlexandros Batsakis static struct xprt_class	xs_bc_tcp_transport = {
3102f300babaSAlexandros Batsakis 	.list		= LIST_HEAD_INIT(xs_bc_tcp_transport.list),
3103f300babaSAlexandros Batsakis 	.name		= "tcp NFSv4.1 backchannel",
3104f300babaSAlexandros Batsakis 	.owner		= THIS_MODULE,
3105f300babaSAlexandros Batsakis 	.ident		= XPRT_TRANSPORT_BC_TCP,
3106f300babaSAlexandros Batsakis 	.setup		= xs_setup_bc_tcp,
3107d5aa6b22STrond Myklebust 	.netid		= { "" },
3108f300babaSAlexandros Batsakis };
3109f300babaSAlexandros Batsakis 
3110282b32e1SChuck Lever /**
3111bc25571eS\"Talpey, Thomas\  * init_socket_xprt - set up xprtsock's sysctls, register with RPC client
3112282b32e1SChuck Lever  *
3113282b32e1SChuck Lever  */
3114282b32e1SChuck Lever int init_socket_xprt(void)
3115282b32e1SChuck Lever {
31162b1bec5fSEric W. Biederman 	if (!sunrpc_table_header)
31170b4d4147SEric W. Biederman 		sunrpc_table_header = register_sysctl_table(sunrpc_table);
3118fbf76683SChuck Lever 
3119176e21eeSChuck Lever 	xprt_register_transport(&xs_local_transport);
3120bc25571eS\"Talpey, Thomas\ 	xprt_register_transport(&xs_udp_transport);
3121bc25571eS\"Talpey, Thomas\ 	xprt_register_transport(&xs_tcp_transport);
3122f300babaSAlexandros Batsakis 	xprt_register_transport(&xs_bc_tcp_transport);
3123bc25571eS\"Talpey, Thomas\ 
3124282b32e1SChuck Lever 	return 0;
3125282b32e1SChuck Lever }
3126282b32e1SChuck Lever 
3127282b32e1SChuck Lever /**
3128bc25571eS\"Talpey, Thomas\  * cleanup_socket_xprt - remove xprtsock's sysctls, unregister
3129282b32e1SChuck Lever  *
3130282b32e1SChuck Lever  */
3131282b32e1SChuck Lever void cleanup_socket_xprt(void)
3132282b32e1SChuck Lever {
3133fbf76683SChuck Lever 	if (sunrpc_table_header) {
3134fbf76683SChuck Lever 		unregister_sysctl_table(sunrpc_table_header);
3135fbf76683SChuck Lever 		sunrpc_table_header = NULL;
3136fbf76683SChuck Lever 	}
3137bc25571eS\"Talpey, Thomas\ 
3138176e21eeSChuck Lever 	xprt_unregister_transport(&xs_local_transport);
3139bc25571eS\"Talpey, Thomas\ 	xprt_unregister_transport(&xs_udp_transport);
3140bc25571eS\"Talpey, Thomas\ 	xprt_unregister_transport(&xs_tcp_transport);
3141f300babaSAlexandros Batsakis 	xprt_unregister_transport(&xs_bc_tcp_transport);
3142282b32e1SChuck Lever }
3143cbf11071STrond Myklebust 
31449bbb9e5aSRusty Russell static int param_set_uint_minmax(const char *val,
31459bbb9e5aSRusty Russell 		const struct kernel_param *kp,
3146cbf11071STrond Myklebust 		unsigned int min, unsigned int max)
3147cbf11071STrond Myklebust {
314800cfaa94SDaniel Walter 	unsigned int num;
3149cbf11071STrond Myklebust 	int ret;
3150cbf11071STrond Myklebust 
3151cbf11071STrond Myklebust 	if (!val)
3152cbf11071STrond Myklebust 		return -EINVAL;
315300cfaa94SDaniel Walter 	ret = kstrtouint(val, 0, &num);
31549761a246SDan Carpenter 	if (ret)
31559761a246SDan Carpenter 		return ret;
31569761a246SDan Carpenter 	if (num < min || num > max)
3157cbf11071STrond Myklebust 		return -EINVAL;
3158cbf11071STrond Myklebust 	*((unsigned int *)kp->arg) = num;
3159cbf11071STrond Myklebust 	return 0;
3160cbf11071STrond Myklebust }
3161cbf11071STrond Myklebust 
31629bbb9e5aSRusty Russell static int param_set_portnr(const char *val, const struct kernel_param *kp)
3163cbf11071STrond Myklebust {
3164cbf11071STrond Myklebust 	return param_set_uint_minmax(val, kp,
3165cbf11071STrond Myklebust 			RPC_MIN_RESVPORT,
3166cbf11071STrond Myklebust 			RPC_MAX_RESVPORT);
3167cbf11071STrond Myklebust }
3168cbf11071STrond Myklebust 
31699c27847dSLuis R. Rodriguez static const struct kernel_param_ops param_ops_portnr = {
31709bbb9e5aSRusty Russell 	.set = param_set_portnr,
31719bbb9e5aSRusty Russell 	.get = param_get_uint,
31729bbb9e5aSRusty Russell };
31739bbb9e5aSRusty Russell 
3174cbf11071STrond Myklebust #define param_check_portnr(name, p) \
3175cbf11071STrond Myklebust 	__param_check(name, p, unsigned int);
3176cbf11071STrond Myklebust 
3177cbf11071STrond Myklebust module_param_named(min_resvport, xprt_min_resvport, portnr, 0644);
3178cbf11071STrond Myklebust module_param_named(max_resvport, xprt_max_resvport, portnr, 0644);
3179cbf11071STrond Myklebust 
31809bbb9e5aSRusty Russell static int param_set_slot_table_size(const char *val,
31819bbb9e5aSRusty Russell 				     const struct kernel_param *kp)
3182cbf11071STrond Myklebust {
3183cbf11071STrond Myklebust 	return param_set_uint_minmax(val, kp,
3184cbf11071STrond Myklebust 			RPC_MIN_SLOT_TABLE,
3185cbf11071STrond Myklebust 			RPC_MAX_SLOT_TABLE);
3186cbf11071STrond Myklebust }
3187cbf11071STrond Myklebust 
31889c27847dSLuis R. Rodriguez static const struct kernel_param_ops param_ops_slot_table_size = {
31899bbb9e5aSRusty Russell 	.set = param_set_slot_table_size,
31909bbb9e5aSRusty Russell 	.get = param_get_uint,
31919bbb9e5aSRusty Russell };
31929bbb9e5aSRusty Russell 
3193cbf11071STrond Myklebust #define param_check_slot_table_size(name, p) \
3194cbf11071STrond Myklebust 	__param_check(name, p, unsigned int);
3195cbf11071STrond Myklebust 
3196d9ba131dSTrond Myklebust static int param_set_max_slot_table_size(const char *val,
3197d9ba131dSTrond Myklebust 				     const struct kernel_param *kp)
3198d9ba131dSTrond Myklebust {
3199d9ba131dSTrond Myklebust 	return param_set_uint_minmax(val, kp,
3200d9ba131dSTrond Myklebust 			RPC_MIN_SLOT_TABLE,
3201d9ba131dSTrond Myklebust 			RPC_MAX_SLOT_TABLE_LIMIT);
3202d9ba131dSTrond Myklebust }
3203d9ba131dSTrond Myklebust 
32049c27847dSLuis R. Rodriguez static const struct kernel_param_ops param_ops_max_slot_table_size = {
3205d9ba131dSTrond Myklebust 	.set = param_set_max_slot_table_size,
3206d9ba131dSTrond Myklebust 	.get = param_get_uint,
3207d9ba131dSTrond Myklebust };
3208d9ba131dSTrond Myklebust 
3209d9ba131dSTrond Myklebust #define param_check_max_slot_table_size(name, p) \
3210d9ba131dSTrond Myklebust 	__param_check(name, p, unsigned int);
3211d9ba131dSTrond Myklebust 
3212cbf11071STrond Myklebust module_param_named(tcp_slot_table_entries, xprt_tcp_slot_table_entries,
3213cbf11071STrond Myklebust 		   slot_table_size, 0644);
3214d9ba131dSTrond Myklebust module_param_named(tcp_max_slot_table_entries, xprt_max_tcp_slot_table_entries,
3215d9ba131dSTrond Myklebust 		   max_slot_table_size, 0644);
3216cbf11071STrond Myklebust module_param_named(udp_slot_table_entries, xprt_udp_slot_table_entries,
3217cbf11071STrond Myklebust 		   slot_table_size, 0644);
3218