xref: /openbmc/linux/net/sunrpc/xprtsock.c (revision f67939e4b045e1c8e857055463c0b5a88eca4844)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2a246b010SChuck Lever /*
3a246b010SChuck Lever  * linux/net/sunrpc/xprtsock.c
4a246b010SChuck Lever  *
5a246b010SChuck Lever  * Client-side transport implementation for sockets.
6a246b010SChuck Lever  *
7113aa838SAlan Cox  * TCP callback races fixes (C) 1998 Red Hat
8113aa838SAlan Cox  * TCP send fixes (C) 1998 Red Hat
9a246b010SChuck Lever  * TCP NFS related read + write fixes
10a246b010SChuck Lever  *  (C) 1999 Dave Airlie, University of Limerick, Ireland <airlied@linux.ie>
11a246b010SChuck Lever  *
12a246b010SChuck Lever  * Rewrite of larges part of the code in order to stabilize TCP stuff.
13a246b010SChuck Lever  * Fix behaviour when socket buffer is full.
14a246b010SChuck Lever  *  (C) 1999 Trond Myklebust <trond.myklebust@fys.uio.no>
1555aa4f58SChuck Lever  *
1655aa4f58SChuck Lever  * IP socket transport implementation, (C) 2005 Chuck Lever <cel@netapp.com>
178f9d5b1aSChuck Lever  *
188f9d5b1aSChuck Lever  * IPv6 support contributed by Gilles Quillard, Bull Open Source, 2005.
198f9d5b1aSChuck Lever  *   <gilles.quillard@bull.net>
20a246b010SChuck Lever  */
21a246b010SChuck Lever 
22a246b010SChuck Lever #include <linux/types.h>
23176e21eeSChuck Lever #include <linux/string.h>
24a246b010SChuck Lever #include <linux/slab.h>
25bc25571eS\"Talpey, Thomas\ #include <linux/module.h>
26a246b010SChuck Lever #include <linux/capability.h>
27a246b010SChuck Lever #include <linux/pagemap.h>
28a246b010SChuck Lever #include <linux/errno.h>
29a246b010SChuck Lever #include <linux/socket.h>
30a246b010SChuck Lever #include <linux/in.h>
31a246b010SChuck Lever #include <linux/net.h>
32a246b010SChuck Lever #include <linux/mm.h>
33176e21eeSChuck Lever #include <linux/un.h>
34a246b010SChuck Lever #include <linux/udp.h>
35a246b010SChuck Lever #include <linux/tcp.h>
36a246b010SChuck Lever #include <linux/sunrpc/clnt.h>
375976687aSJeff Layton #include <linux/sunrpc/addr.h>
3802107148SChuck Lever #include <linux/sunrpc/sched.h>
394cfc7e60SRahul Iyer #include <linux/sunrpc/svcsock.h>
4049c36fccS\"Talpey, Thomas\ #include <linux/sunrpc/xprtsock.h>
41a246b010SChuck Lever #include <linux/file.h>
429e00abc3STrond Myklebust #ifdef CONFIG_SUNRPC_BACKCHANNEL
4344b98efdSRicardo Labiaga #include <linux/sunrpc/bc_xprt.h>
4444b98efdSRicardo Labiaga #endif
45a246b010SChuck Lever 
46a246b010SChuck Lever #include <net/sock.h>
47a246b010SChuck Lever #include <net/checksum.h>
48a246b010SChuck Lever #include <net/udp.h>
49a246b010SChuck Lever #include <net/tcp.h>
50277e4ab7STrond Myklebust #include <linux/bvec.h>
516a829eb8STrond Myklebust #include <linux/highmem.h>
52277e4ab7STrond Myklebust #include <linux/uio.h>
53a1231fdaSTrond Myklebust #include <linux/sched/mm.h>
54a246b010SChuck Lever 
5540b5ea0cSTrond Myklebust #include <trace/events/sunrpc.h>
5640b5ea0cSTrond Myklebust 
579e55eef4SChuck Lever #include "socklib.h"
584cfc7e60SRahul Iyer #include "sunrpc.h"
59176e21eeSChuck Lever 
60176e21eeSChuck Lever static void xs_close(struct rpc_xprt *xprt);
613b21f757STrond Myklebust static void xs_set_srcport(struct sock_xprt *transport, struct socket *sock);
627196dbb0STrond Myklebust static void xs_tcp_set_socket_timeouts(struct rpc_xprt *xprt,
637196dbb0STrond Myklebust 		struct socket *sock);
64176e21eeSChuck Lever 
659903cd1cSChuck Lever /*
66c556b754SChuck Lever  * xprtsock tunables
67c556b754SChuck Lever  */
6809acfea5STrond Myklebust static unsigned int xprt_udp_slot_table_entries = RPC_DEF_SLOT_TABLE;
6909acfea5STrond Myklebust static unsigned int xprt_tcp_slot_table_entries = RPC_MIN_SLOT_TABLE;
7009acfea5STrond Myklebust static unsigned int xprt_max_tcp_slot_table_entries = RPC_MAX_SLOT_TABLE;
71c556b754SChuck Lever 
7209acfea5STrond Myklebust static unsigned int xprt_min_resvport = RPC_DEF_MIN_RESVPORT;
7309acfea5STrond Myklebust static unsigned int xprt_max_resvport = RPC_DEF_MAX_RESVPORT;
74c556b754SChuck Lever 
757d1e8255STrond Myklebust #define XS_TCP_LINGER_TO	(15U * HZ)
7625fe6142STrond Myklebust static unsigned int xs_tcp_fin_timeout __read_mostly = XS_TCP_LINGER_TO;
777d1e8255STrond Myklebust 
78c556b754SChuck Lever /*
79fbf76683SChuck Lever  * We can register our own files under /proc/sys/sunrpc by
80fbf76683SChuck Lever  * calling register_sysctl_table() again.  The files in that
81fbf76683SChuck Lever  * directory become the union of all files registered there.
82fbf76683SChuck Lever  *
83fbf76683SChuck Lever  * We simply need to make sure that we don't collide with
84fbf76683SChuck Lever  * someone else's file names!
85fbf76683SChuck Lever  */
86fbf76683SChuck Lever 
87fbf76683SChuck Lever static unsigned int min_slot_table_size = RPC_MIN_SLOT_TABLE;
88fbf76683SChuck Lever static unsigned int max_slot_table_size = RPC_MAX_SLOT_TABLE;
89d9ba131dSTrond Myklebust static unsigned int max_tcp_slot_table_limit = RPC_MAX_SLOT_TABLE_LIMIT;
90fbf76683SChuck Lever static unsigned int xprt_min_resvport_limit = RPC_MIN_RESVPORT;
91fbf76683SChuck Lever static unsigned int xprt_max_resvport_limit = RPC_MAX_RESVPORT;
92fbf76683SChuck Lever 
93fbf76683SChuck Lever static struct ctl_table_header *sunrpc_table_header;
94fbf76683SChuck Lever 
95d3abc739SOlga Kornievskaia static struct xprt_class xs_local_transport;
96d3abc739SOlga Kornievskaia static struct xprt_class xs_udp_transport;
97d3abc739SOlga Kornievskaia static struct xprt_class xs_tcp_transport;
98d3abc739SOlga Kornievskaia static struct xprt_class xs_bc_tcp_transport;
99d3abc739SOlga Kornievskaia 
100fbf76683SChuck Lever /*
101fbf76683SChuck Lever  * FIXME: changing the UDP slot table size should also resize the UDP
102fbf76683SChuck Lever  *        socket buffers for existing UDP transports
103fbf76683SChuck Lever  */
104fe2c6338SJoe Perches static struct ctl_table xs_tunables_table[] = {
105fbf76683SChuck Lever 	{
106fbf76683SChuck Lever 		.procname	= "udp_slot_table_entries",
107fbf76683SChuck Lever 		.data		= &xprt_udp_slot_table_entries,
108fbf76683SChuck Lever 		.maxlen		= sizeof(unsigned int),
109fbf76683SChuck Lever 		.mode		= 0644,
1106d456111SEric W. Biederman 		.proc_handler	= proc_dointvec_minmax,
111fbf76683SChuck Lever 		.extra1		= &min_slot_table_size,
112fbf76683SChuck Lever 		.extra2		= &max_slot_table_size
113fbf76683SChuck Lever 	},
114fbf76683SChuck Lever 	{
115fbf76683SChuck Lever 		.procname	= "tcp_slot_table_entries",
116fbf76683SChuck Lever 		.data		= &xprt_tcp_slot_table_entries,
117fbf76683SChuck Lever 		.maxlen		= sizeof(unsigned int),
118fbf76683SChuck Lever 		.mode		= 0644,
1196d456111SEric W. Biederman 		.proc_handler	= proc_dointvec_minmax,
120fbf76683SChuck Lever 		.extra1		= &min_slot_table_size,
121fbf76683SChuck Lever 		.extra2		= &max_slot_table_size
122fbf76683SChuck Lever 	},
123fbf76683SChuck Lever 	{
124d9ba131dSTrond Myklebust 		.procname	= "tcp_max_slot_table_entries",
125d9ba131dSTrond Myklebust 		.data		= &xprt_max_tcp_slot_table_entries,
126d9ba131dSTrond Myklebust 		.maxlen		= sizeof(unsigned int),
127d9ba131dSTrond Myklebust 		.mode		= 0644,
128d9ba131dSTrond Myklebust 		.proc_handler	= proc_dointvec_minmax,
129d9ba131dSTrond Myklebust 		.extra1		= &min_slot_table_size,
130d9ba131dSTrond Myklebust 		.extra2		= &max_tcp_slot_table_limit
131d9ba131dSTrond Myklebust 	},
132d9ba131dSTrond Myklebust 	{
133fbf76683SChuck Lever 		.procname	= "min_resvport",
134fbf76683SChuck Lever 		.data		= &xprt_min_resvport,
135fbf76683SChuck Lever 		.maxlen		= sizeof(unsigned int),
136fbf76683SChuck Lever 		.mode		= 0644,
1376d456111SEric W. Biederman 		.proc_handler	= proc_dointvec_minmax,
138fbf76683SChuck Lever 		.extra1		= &xprt_min_resvport_limit,
139826799e6SJ. Bruce Fields 		.extra2		= &xprt_max_resvport_limit
140fbf76683SChuck Lever 	},
141fbf76683SChuck Lever 	{
142fbf76683SChuck Lever 		.procname	= "max_resvport",
143fbf76683SChuck Lever 		.data		= &xprt_max_resvport,
144fbf76683SChuck Lever 		.maxlen		= sizeof(unsigned int),
145fbf76683SChuck Lever 		.mode		= 0644,
1466d456111SEric W. Biederman 		.proc_handler	= proc_dointvec_minmax,
147826799e6SJ. Bruce Fields 		.extra1		= &xprt_min_resvport_limit,
148fbf76683SChuck Lever 		.extra2		= &xprt_max_resvport_limit
149fbf76683SChuck Lever 	},
150fbf76683SChuck Lever 	{
15125fe6142STrond Myklebust 		.procname	= "tcp_fin_timeout",
15225fe6142STrond Myklebust 		.data		= &xs_tcp_fin_timeout,
15325fe6142STrond Myklebust 		.maxlen		= sizeof(xs_tcp_fin_timeout),
15425fe6142STrond Myklebust 		.mode		= 0644,
1556d456111SEric W. Biederman 		.proc_handler	= proc_dointvec_jiffies,
15625fe6142STrond Myklebust 	},
157f8572d8fSEric W. Biederman 	{ },
158fbf76683SChuck Lever };
159fbf76683SChuck Lever 
160fe2c6338SJoe Perches static struct ctl_table sunrpc_table[] = {
161fbf76683SChuck Lever 	{
162fbf76683SChuck Lever 		.procname	= "sunrpc",
163fbf76683SChuck Lever 		.mode		= 0555,
164fbf76683SChuck Lever 		.child		= xs_tunables_table
165fbf76683SChuck Lever 	},
166f8572d8fSEric W. Biederman 	{ },
167fbf76683SChuck Lever };
168fbf76683SChuck Lever 
169fbf76683SChuck Lever /*
17003bf4b70SChuck Lever  * Wait duration for a reply from the RPC portmapper.
17103bf4b70SChuck Lever  */
17203bf4b70SChuck Lever #define XS_BIND_TO		(60U * HZ)
17303bf4b70SChuck Lever 
17403bf4b70SChuck Lever /*
17503bf4b70SChuck Lever  * Delay if a UDP socket connect error occurs.  This is most likely some
17603bf4b70SChuck Lever  * kind of resource problem on the local host.
17703bf4b70SChuck Lever  */
17803bf4b70SChuck Lever #define XS_UDP_REEST_TO		(2U * HZ)
17903bf4b70SChuck Lever 
18003bf4b70SChuck Lever /*
18103bf4b70SChuck Lever  * The reestablish timeout allows clients to delay for a bit before attempting
18203bf4b70SChuck Lever  * to reconnect to a server that just dropped our connection.
18303bf4b70SChuck Lever  *
18403bf4b70SChuck Lever  * We implement an exponential backoff when trying to reestablish a TCP
18503bf4b70SChuck Lever  * transport connection with the server.  Some servers like to drop a TCP
18603bf4b70SChuck Lever  * connection when they are overworked, so we start with a short timeout and
18703bf4b70SChuck Lever  * increase over time if the server is down or not responding.
18803bf4b70SChuck Lever  */
18903bf4b70SChuck Lever #define XS_TCP_INIT_REEST_TO	(3U * HZ)
19003bf4b70SChuck Lever 
19103bf4b70SChuck Lever /*
19203bf4b70SChuck Lever  * TCP idle timeout; client drops the transport socket if it is idle
19303bf4b70SChuck Lever  * for this long.  Note that we also timeout UDP sockets to prevent
19403bf4b70SChuck Lever  * holding port numbers when there is no RPC traffic.
19503bf4b70SChuck Lever  */
19603bf4b70SChuck Lever #define XS_IDLE_DISC_TO		(5U * 60 * HZ)
19703bf4b70SChuck Lever 
198f895b252SJeff Layton #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
199a246b010SChuck Lever # undef  RPC_DEBUG_DATA
2009903cd1cSChuck Lever # define RPCDBG_FACILITY	RPCDBG_TRANS
201a246b010SChuck Lever #endif
202a246b010SChuck Lever 
203a246b010SChuck Lever #ifdef RPC_DEBUG_DATA
2049903cd1cSChuck Lever static void xs_pktdump(char *msg, u32 *packet, unsigned int count)
205a246b010SChuck Lever {
206a246b010SChuck Lever 	u8 *buf = (u8 *) packet;
207a246b010SChuck Lever 	int j;
208a246b010SChuck Lever 
209a246b010SChuck Lever 	dprintk("RPC:       %s\n", msg);
210a246b010SChuck Lever 	for (j = 0; j < count && j < 128; j += 4) {
211a246b010SChuck Lever 		if (!(j & 31)) {
212a246b010SChuck Lever 			if (j)
213a246b010SChuck Lever 				dprintk("\n");
214a246b010SChuck Lever 			dprintk("0x%04x ", j);
215a246b010SChuck Lever 		}
216a246b010SChuck Lever 		dprintk("%02x%02x%02x%02x ",
217a246b010SChuck Lever 			buf[j], buf[j+1], buf[j+2], buf[j+3]);
218a246b010SChuck Lever 	}
219a246b010SChuck Lever 	dprintk("\n");
220a246b010SChuck Lever }
221a246b010SChuck Lever #else
2229903cd1cSChuck Lever static inline void xs_pktdump(char *msg, u32 *packet, unsigned int count)
223a246b010SChuck Lever {
224a246b010SChuck Lever 	/* NOP */
225a246b010SChuck Lever }
226a246b010SChuck Lever #endif
227a246b010SChuck Lever 
2282118071dSTrond Myklebust static inline struct rpc_xprt *xprt_from_sock(struct sock *sk)
2292118071dSTrond Myklebust {
2302118071dSTrond Myklebust 	return (struct rpc_xprt *) sk->sk_user_data;
2312118071dSTrond Myklebust }
2322118071dSTrond Myklebust 
23395392c59SChuck Lever static inline struct sockaddr *xs_addr(struct rpc_xprt *xprt)
234edb267a6SChuck Lever {
23595392c59SChuck Lever 	return (struct sockaddr *) &xprt->addr;
23695392c59SChuck Lever }
23795392c59SChuck Lever 
238176e21eeSChuck Lever static inline struct sockaddr_un *xs_addr_un(struct rpc_xprt *xprt)
239176e21eeSChuck Lever {
240176e21eeSChuck Lever 	return (struct sockaddr_un *) &xprt->addr;
241176e21eeSChuck Lever }
242176e21eeSChuck Lever 
24395392c59SChuck Lever static inline struct sockaddr_in *xs_addr_in(struct rpc_xprt *xprt)
24495392c59SChuck Lever {
24595392c59SChuck Lever 	return (struct sockaddr_in *) &xprt->addr;
24695392c59SChuck Lever }
24795392c59SChuck Lever 
24895392c59SChuck Lever static inline struct sockaddr_in6 *xs_addr_in6(struct rpc_xprt *xprt)
24995392c59SChuck Lever {
25095392c59SChuck Lever 	return (struct sockaddr_in6 *) &xprt->addr;
25195392c59SChuck Lever }
25295392c59SChuck Lever 
253c877b849SChuck Lever static void xs_format_common_peer_addresses(struct rpc_xprt *xprt)
254c877b849SChuck Lever {
255c877b849SChuck Lever 	struct sockaddr *sap = xs_addr(xprt);
2569dc3b095SChuck Lever 	struct sockaddr_in6 *sin6;
2579dc3b095SChuck Lever 	struct sockaddr_in *sin;
258176e21eeSChuck Lever 	struct sockaddr_un *sun;
259c877b849SChuck Lever 	char buf[128];
260c877b849SChuck Lever 
2619dc3b095SChuck Lever 	switch (sap->sa_family) {
262176e21eeSChuck Lever 	case AF_LOCAL:
263176e21eeSChuck Lever 		sun = xs_addr_un(xprt);
264176e21eeSChuck Lever 		strlcpy(buf, sun->sun_path, sizeof(buf));
265176e21eeSChuck Lever 		xprt->address_strings[RPC_DISPLAY_ADDR] =
266176e21eeSChuck Lever 						kstrdup(buf, GFP_KERNEL);
267176e21eeSChuck Lever 		break;
2689dc3b095SChuck Lever 	case AF_INET:
269176e21eeSChuck Lever 		(void)rpc_ntop(sap, buf, sizeof(buf));
270176e21eeSChuck Lever 		xprt->address_strings[RPC_DISPLAY_ADDR] =
271176e21eeSChuck Lever 						kstrdup(buf, GFP_KERNEL);
2729dc3b095SChuck Lever 		sin = xs_addr_in(xprt);
273fc0b5791SJoe Perches 		snprintf(buf, sizeof(buf), "%08x", ntohl(sin->sin_addr.s_addr));
2749dc3b095SChuck Lever 		break;
2759dc3b095SChuck Lever 	case AF_INET6:
276176e21eeSChuck Lever 		(void)rpc_ntop(sap, buf, sizeof(buf));
277176e21eeSChuck Lever 		xprt->address_strings[RPC_DISPLAY_ADDR] =
278176e21eeSChuck Lever 						kstrdup(buf, GFP_KERNEL);
2799dc3b095SChuck Lever 		sin6 = xs_addr_in6(xprt);
280fc0b5791SJoe Perches 		snprintf(buf, sizeof(buf), "%pi6", &sin6->sin6_addr);
2819dc3b095SChuck Lever 		break;
2829dc3b095SChuck Lever 	default:
2839dc3b095SChuck Lever 		BUG();
2849dc3b095SChuck Lever 	}
285176e21eeSChuck Lever 
2869dc3b095SChuck Lever 	xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = kstrdup(buf, GFP_KERNEL);
2879dc3b095SChuck Lever }
2889dc3b095SChuck Lever 
2899dc3b095SChuck Lever static void xs_format_common_peer_ports(struct rpc_xprt *xprt)
2909dc3b095SChuck Lever {
2919dc3b095SChuck Lever 	struct sockaddr *sap = xs_addr(xprt);
2929dc3b095SChuck Lever 	char buf[128];
2939dc3b095SChuck Lever 
29481160e66SJoe Perches 	snprintf(buf, sizeof(buf), "%u", rpc_get_port(sap));
295c877b849SChuck Lever 	xprt->address_strings[RPC_DISPLAY_PORT] = kstrdup(buf, GFP_KERNEL);
296c877b849SChuck Lever 
29781160e66SJoe Perches 	snprintf(buf, sizeof(buf), "%4hx", rpc_get_port(sap));
298c877b849SChuck Lever 	xprt->address_strings[RPC_DISPLAY_HEX_PORT] = kstrdup(buf, GFP_KERNEL);
299c877b849SChuck Lever }
300c877b849SChuck Lever 
3019dc3b095SChuck Lever static void xs_format_peer_addresses(struct rpc_xprt *xprt,
302b454ae90SChuck Lever 				     const char *protocol,
303b454ae90SChuck Lever 				     const char *netid)
304edb267a6SChuck Lever {
305b454ae90SChuck Lever 	xprt->address_strings[RPC_DISPLAY_PROTO] = protocol;
306b454ae90SChuck Lever 	xprt->address_strings[RPC_DISPLAY_NETID] = netid;
307c877b849SChuck Lever 	xs_format_common_peer_addresses(xprt);
3089dc3b095SChuck Lever 	xs_format_common_peer_ports(xprt);
309edb267a6SChuck Lever }
310edb267a6SChuck Lever 
3119dc3b095SChuck Lever static void xs_update_peer_port(struct rpc_xprt *xprt)
3124b6473fbSChuck Lever {
3139dc3b095SChuck Lever 	kfree(xprt->address_strings[RPC_DISPLAY_HEX_PORT]);
3149dc3b095SChuck Lever 	kfree(xprt->address_strings[RPC_DISPLAY_PORT]);
3154b6473fbSChuck Lever 
3169dc3b095SChuck Lever 	xs_format_common_peer_ports(xprt);
317edb267a6SChuck Lever }
318edb267a6SChuck Lever 
319edb267a6SChuck Lever static void xs_free_peer_addresses(struct rpc_xprt *xprt)
320edb267a6SChuck Lever {
32133e01dc7SChuck Lever 	unsigned int i;
32233e01dc7SChuck Lever 
32333e01dc7SChuck Lever 	for (i = 0; i < RPC_DISPLAY_MAX; i++)
32433e01dc7SChuck Lever 		switch (i) {
32533e01dc7SChuck Lever 		case RPC_DISPLAY_PROTO:
32633e01dc7SChuck Lever 		case RPC_DISPLAY_NETID:
32733e01dc7SChuck Lever 			continue;
32833e01dc7SChuck Lever 		default:
32933e01dc7SChuck Lever 			kfree(xprt->address_strings[i]);
33033e01dc7SChuck Lever 		}
331edb267a6SChuck Lever }
332edb267a6SChuck Lever 
333277e4ab7STrond Myklebust static size_t
334277e4ab7STrond Myklebust xs_alloc_sparse_pages(struct xdr_buf *buf, size_t want, gfp_t gfp)
335277e4ab7STrond Myklebust {
336277e4ab7STrond Myklebust 	size_t i,n;
337277e4ab7STrond Myklebust 
33816e5e90fSTrond Myklebust 	if (!want || !(buf->flags & XDRBUF_SPARSE_PAGES))
339277e4ab7STrond Myklebust 		return want;
340277e4ab7STrond Myklebust 	n = (buf->page_base + want + PAGE_SIZE - 1) >> PAGE_SHIFT;
341277e4ab7STrond Myklebust 	for (i = 0; i < n; i++) {
342277e4ab7STrond Myklebust 		if (buf->pages[i])
343277e4ab7STrond Myklebust 			continue;
344277e4ab7STrond Myklebust 		buf->bvec[i].bv_page = buf->pages[i] = alloc_page(gfp);
345277e4ab7STrond Myklebust 		if (!buf->pages[i]) {
34616e5e90fSTrond Myklebust 			i *= PAGE_SIZE;
34716e5e90fSTrond Myklebust 			return i > buf->page_base ? i - buf->page_base : 0;
348277e4ab7STrond Myklebust 		}
349277e4ab7STrond Myklebust 	}
350277e4ab7STrond Myklebust 	return want;
351277e4ab7STrond Myklebust }
352277e4ab7STrond Myklebust 
353277e4ab7STrond Myklebust static ssize_t
354277e4ab7STrond Myklebust xs_sock_recvmsg(struct socket *sock, struct msghdr *msg, int flags, size_t seek)
355277e4ab7STrond Myklebust {
356277e4ab7STrond Myklebust 	ssize_t ret;
357277e4ab7STrond Myklebust 	if (seek != 0)
358277e4ab7STrond Myklebust 		iov_iter_advance(&msg->msg_iter, seek);
359277e4ab7STrond Myklebust 	ret = sock_recvmsg(sock, msg, flags);
360277e4ab7STrond Myklebust 	return ret > 0 ? ret + seek : ret;
361277e4ab7STrond Myklebust }
362277e4ab7STrond Myklebust 
363277e4ab7STrond Myklebust static ssize_t
364277e4ab7STrond Myklebust xs_read_kvec(struct socket *sock, struct msghdr *msg, int flags,
365277e4ab7STrond Myklebust 		struct kvec *kvec, size_t count, size_t seek)
366277e4ab7STrond Myklebust {
3670e9b4a82SAl Viro 	iov_iter_kvec(&msg->msg_iter, READ, kvec, 1, count);
368277e4ab7STrond Myklebust 	return xs_sock_recvmsg(sock, msg, flags, seek);
369277e4ab7STrond Myklebust }
370277e4ab7STrond Myklebust 
371277e4ab7STrond Myklebust static ssize_t
372277e4ab7STrond Myklebust xs_read_bvec(struct socket *sock, struct msghdr *msg, int flags,
373277e4ab7STrond Myklebust 		struct bio_vec *bvec, unsigned long nr, size_t count,
374277e4ab7STrond Myklebust 		size_t seek)
375277e4ab7STrond Myklebust {
3760e9b4a82SAl Viro 	iov_iter_bvec(&msg->msg_iter, READ, bvec, nr, count);
377277e4ab7STrond Myklebust 	return xs_sock_recvmsg(sock, msg, flags, seek);
378277e4ab7STrond Myklebust }
379277e4ab7STrond Myklebust 
380277e4ab7STrond Myklebust static ssize_t
381277e4ab7STrond Myklebust xs_read_discard(struct socket *sock, struct msghdr *msg, int flags,
382277e4ab7STrond Myklebust 		size_t count)
383277e4ab7STrond Myklebust {
384b76a5afdSTrond Myklebust 	iov_iter_discard(&msg->msg_iter, READ, count);
385b76a5afdSTrond Myklebust 	return sock_recvmsg(sock, msg, flags);
386277e4ab7STrond Myklebust }
387277e4ab7STrond Myklebust 
3886a829eb8STrond Myklebust #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
3896a829eb8STrond Myklebust static void
3906a829eb8STrond Myklebust xs_flush_bvec(const struct bio_vec *bvec, size_t count, size_t seek)
3916a829eb8STrond Myklebust {
3926a829eb8STrond Myklebust 	struct bvec_iter bi = {
3936a829eb8STrond Myklebust 		.bi_size = count,
3946a829eb8STrond Myklebust 	};
3956a829eb8STrond Myklebust 	struct bio_vec bv;
3966a829eb8STrond Myklebust 
3976a829eb8STrond Myklebust 	bvec_iter_advance(bvec, &bi, seek & PAGE_MASK);
3986a829eb8STrond Myklebust 	for_each_bvec(bv, bvec, bi, bi)
3996a829eb8STrond Myklebust 		flush_dcache_page(bv.bv_page);
4006a829eb8STrond Myklebust }
4016a829eb8STrond Myklebust #else
4026a829eb8STrond Myklebust static inline void
4036a829eb8STrond Myklebust xs_flush_bvec(const struct bio_vec *bvec, size_t count, size_t seek)
4046a829eb8STrond Myklebust {
4056a829eb8STrond Myklebust }
4066a829eb8STrond Myklebust #endif
4076a829eb8STrond Myklebust 
408277e4ab7STrond Myklebust static ssize_t
409277e4ab7STrond Myklebust xs_read_xdr_buf(struct socket *sock, struct msghdr *msg, int flags,
410277e4ab7STrond Myklebust 		struct xdr_buf *buf, size_t count, size_t seek, size_t *read)
411277e4ab7STrond Myklebust {
412277e4ab7STrond Myklebust 	size_t want, seek_init = seek, offset = 0;
413277e4ab7STrond Myklebust 	ssize_t ret;
414277e4ab7STrond Myklebust 
415277e4ab7STrond Myklebust 	want = min_t(size_t, count, buf->head[0].iov_len);
416e92053a5STrond Myklebust 	if (seek < want) {
417277e4ab7STrond Myklebust 		ret = xs_read_kvec(sock, msg, flags, &buf->head[0], want, seek);
418277e4ab7STrond Myklebust 		if (ret <= 0)
419277e4ab7STrond Myklebust 			goto sock_err;
420277e4ab7STrond Myklebust 		offset += ret;
421277e4ab7STrond Myklebust 		if (offset == count || msg->msg_flags & (MSG_EOR|MSG_TRUNC))
422277e4ab7STrond Myklebust 			goto out;
423277e4ab7STrond Myklebust 		if (ret != want)
424c4433055STrond Myklebust 			goto out;
425277e4ab7STrond Myklebust 		seek = 0;
426277e4ab7STrond Myklebust 	} else {
427e92053a5STrond Myklebust 		seek -= want;
428e92053a5STrond Myklebust 		offset += want;
429277e4ab7STrond Myklebust 	}
43016e5e90fSTrond Myklebust 
431b2648015STrond Myklebust 	want = xs_alloc_sparse_pages(
432b2648015STrond Myklebust 		buf, min_t(size_t, count - offset, buf->page_len),
433b2648015STrond Myklebust 		GFP_KERNEL | __GFP_NORETRY | __GFP_NOWARN);
43416e5e90fSTrond Myklebust 	if (seek < want) {
435277e4ab7STrond Myklebust 		ret = xs_read_bvec(sock, msg, flags, buf->bvec,
436277e4ab7STrond Myklebust 				xdr_buf_pagecount(buf),
437277e4ab7STrond Myklebust 				want + buf->page_base,
438277e4ab7STrond Myklebust 				seek + buf->page_base);
439277e4ab7STrond Myklebust 		if (ret <= 0)
440277e4ab7STrond Myklebust 			goto sock_err;
4416a829eb8STrond Myklebust 		xs_flush_bvec(buf->bvec, ret, seek + buf->page_base);
442ac9645c8SDan Aloni 		ret -= buf->page_base;
443ac9645c8SDan Aloni 		offset += ret;
444277e4ab7STrond Myklebust 		if (offset == count || msg->msg_flags & (MSG_EOR|MSG_TRUNC))
445277e4ab7STrond Myklebust 			goto out;
446277e4ab7STrond Myklebust 		if (ret != want)
447c4433055STrond Myklebust 			goto out;
448277e4ab7STrond Myklebust 		seek = 0;
449277e4ab7STrond Myklebust 	} else {
45016e5e90fSTrond Myklebust 		seek -= want;
45116e5e90fSTrond Myklebust 		offset += want;
452277e4ab7STrond Myklebust 	}
45316e5e90fSTrond Myklebust 
454277e4ab7STrond Myklebust 	want = min_t(size_t, count - offset, buf->tail[0].iov_len);
455e92053a5STrond Myklebust 	if (seek < want) {
456277e4ab7STrond Myklebust 		ret = xs_read_kvec(sock, msg, flags, &buf->tail[0], want, seek);
457277e4ab7STrond Myklebust 		if (ret <= 0)
458277e4ab7STrond Myklebust 			goto sock_err;
459277e4ab7STrond Myklebust 		offset += ret;
460277e4ab7STrond Myklebust 		if (offset == count || msg->msg_flags & (MSG_EOR|MSG_TRUNC))
461277e4ab7STrond Myklebust 			goto out;
462277e4ab7STrond Myklebust 		if (ret != want)
463c4433055STrond Myklebust 			goto out;
4649734ad57STrond Myklebust 	} else if (offset < seek_init)
465e92053a5STrond Myklebust 		offset = seek_init;
466277e4ab7STrond Myklebust 	ret = -EMSGSIZE;
467277e4ab7STrond Myklebust out:
468277e4ab7STrond Myklebust 	*read = offset - seek_init;
469277e4ab7STrond Myklebust 	return ret;
470277e4ab7STrond Myklebust sock_err:
471277e4ab7STrond Myklebust 	offset += seek;
472277e4ab7STrond Myklebust 	goto out;
473277e4ab7STrond Myklebust }
474277e4ab7STrond Myklebust 
475277e4ab7STrond Myklebust static void
476277e4ab7STrond Myklebust xs_read_header(struct sock_xprt *transport, struct xdr_buf *buf)
477277e4ab7STrond Myklebust {
478277e4ab7STrond Myklebust 	if (!transport->recv.copied) {
479277e4ab7STrond Myklebust 		if (buf->head[0].iov_len >= transport->recv.offset)
480277e4ab7STrond Myklebust 			memcpy(buf->head[0].iov_base,
481277e4ab7STrond Myklebust 					&transport->recv.xid,
482277e4ab7STrond Myklebust 					transport->recv.offset);
483277e4ab7STrond Myklebust 		transport->recv.copied = transport->recv.offset;
484277e4ab7STrond Myklebust 	}
485277e4ab7STrond Myklebust }
486277e4ab7STrond Myklebust 
487277e4ab7STrond Myklebust static bool
488277e4ab7STrond Myklebust xs_read_stream_request_done(struct sock_xprt *transport)
489277e4ab7STrond Myklebust {
490277e4ab7STrond Myklebust 	return transport->recv.fraghdr & cpu_to_be32(RPC_LAST_STREAM_FRAGMENT);
491277e4ab7STrond Myklebust }
492277e4ab7STrond Myklebust 
493e92053a5STrond Myklebust static void
494e92053a5STrond Myklebust xs_read_stream_check_eor(struct sock_xprt *transport,
495e92053a5STrond Myklebust 		struct msghdr *msg)
496e92053a5STrond Myklebust {
497e92053a5STrond Myklebust 	if (xs_read_stream_request_done(transport))
498e92053a5STrond Myklebust 		msg->msg_flags |= MSG_EOR;
499e92053a5STrond Myklebust }
500e92053a5STrond Myklebust 
501277e4ab7STrond Myklebust static ssize_t
502277e4ab7STrond Myklebust xs_read_stream_request(struct sock_xprt *transport, struct msghdr *msg,
503277e4ab7STrond Myklebust 		int flags, struct rpc_rqst *req)
504277e4ab7STrond Myklebust {
505277e4ab7STrond Myklebust 	struct xdr_buf *buf = &req->rq_private_buf;
5063f649ab7SKees Cook 	size_t want, read;
5073f649ab7SKees Cook 	ssize_t ret;
508277e4ab7STrond Myklebust 
509277e4ab7STrond Myklebust 	xs_read_header(transport, buf);
510277e4ab7STrond Myklebust 
511277e4ab7STrond Myklebust 	want = transport->recv.len - transport->recv.offset;
512e92053a5STrond Myklebust 	if (want != 0) {
513277e4ab7STrond Myklebust 		ret = xs_read_xdr_buf(transport->sock, msg, flags, buf,
514e92053a5STrond Myklebust 				transport->recv.copied + want,
515e92053a5STrond Myklebust 				transport->recv.copied,
516277e4ab7STrond Myklebust 				&read);
517277e4ab7STrond Myklebust 		transport->recv.offset += read;
518277e4ab7STrond Myklebust 		transport->recv.copied += read;
519277e4ab7STrond Myklebust 	}
520277e4ab7STrond Myklebust 
521727fcc64STrond Myklebust 	if (transport->recv.offset == transport->recv.len)
522727fcc64STrond Myklebust 		xs_read_stream_check_eor(transport, msg);
523727fcc64STrond Myklebust 
524e92053a5STrond Myklebust 	if (want == 0)
525e92053a5STrond Myklebust 		return 0;
526e92053a5STrond Myklebust 
527277e4ab7STrond Myklebust 	switch (ret) {
528c4433055STrond Myklebust 	default:
529c4433055STrond Myklebust 		break;
53026781eabSTrond Myklebust 	case -EFAULT:
531277e4ab7STrond Myklebust 	case -EMSGSIZE:
53226781eabSTrond Myklebust 		msg->msg_flags |= MSG_TRUNC;
533c4433055STrond Myklebust 		return read;
534277e4ab7STrond Myklebust 	case 0:
535277e4ab7STrond Myklebust 		return -ESHUTDOWN;
536277e4ab7STrond Myklebust 	}
537c4433055STrond Myklebust 	return ret < 0 ? ret : read;
538277e4ab7STrond Myklebust }
539277e4ab7STrond Myklebust 
540277e4ab7STrond Myklebust static size_t
541277e4ab7STrond Myklebust xs_read_stream_headersize(bool isfrag)
542277e4ab7STrond Myklebust {
543277e4ab7STrond Myklebust 	if (isfrag)
544277e4ab7STrond Myklebust 		return sizeof(__be32);
545277e4ab7STrond Myklebust 	return 3 * sizeof(__be32);
546277e4ab7STrond Myklebust }
547277e4ab7STrond Myklebust 
548277e4ab7STrond Myklebust static ssize_t
549277e4ab7STrond Myklebust xs_read_stream_header(struct sock_xprt *transport, struct msghdr *msg,
550277e4ab7STrond Myklebust 		int flags, size_t want, size_t seek)
551277e4ab7STrond Myklebust {
552277e4ab7STrond Myklebust 	struct kvec kvec = {
553277e4ab7STrond Myklebust 		.iov_base = &transport->recv.fraghdr,
554277e4ab7STrond Myklebust 		.iov_len = want,
555277e4ab7STrond Myklebust 	};
556277e4ab7STrond Myklebust 	return xs_read_kvec(transport->sock, msg, flags, &kvec, want, seek);
557277e4ab7STrond Myklebust }
558277e4ab7STrond Myklebust 
559277e4ab7STrond Myklebust #if defined(CONFIG_SUNRPC_BACKCHANNEL)
560277e4ab7STrond Myklebust static ssize_t
561277e4ab7STrond Myklebust xs_read_stream_call(struct sock_xprt *transport, struct msghdr *msg, int flags)
562277e4ab7STrond Myklebust {
563277e4ab7STrond Myklebust 	struct rpc_xprt *xprt = &transport->xprt;
564277e4ab7STrond Myklebust 	struct rpc_rqst *req;
565277e4ab7STrond Myklebust 	ssize_t ret;
566277e4ab7STrond Myklebust 
56798b5cee3SBenjamin Coddington 	/* Is this transport associated with the backchannel? */
56898b5cee3SBenjamin Coddington 	if (!xprt->bc_serv)
56998b5cee3SBenjamin Coddington 		return -ESHUTDOWN;
57098b5cee3SBenjamin Coddington 
571277e4ab7STrond Myklebust 	/* Look up and lock the request corresponding to the given XID */
572277e4ab7STrond Myklebust 	req = xprt_lookup_bc_request(xprt, transport->recv.xid);
573277e4ab7STrond Myklebust 	if (!req) {
574277e4ab7STrond Myklebust 		printk(KERN_WARNING "Callback slot table overflowed\n");
575277e4ab7STrond Myklebust 		return -ESHUTDOWN;
576277e4ab7STrond Myklebust 	}
57745835a63STrond Myklebust 	if (transport->recv.copied && !req->rq_private_buf.len)
57845835a63STrond Myklebust 		return -ESHUTDOWN;
579277e4ab7STrond Myklebust 
580277e4ab7STrond Myklebust 	ret = xs_read_stream_request(transport, msg, flags, req);
581277e4ab7STrond Myklebust 	if (msg->msg_flags & (MSG_EOR|MSG_TRUNC))
582c4433055STrond Myklebust 		xprt_complete_bc_request(req, transport->recv.copied);
58345835a63STrond Myklebust 	else
58445835a63STrond Myklebust 		req->rq_private_buf.len = transport->recv.copied;
585277e4ab7STrond Myklebust 
586277e4ab7STrond Myklebust 	return ret;
587277e4ab7STrond Myklebust }
588277e4ab7STrond Myklebust #else /* CONFIG_SUNRPC_BACKCHANNEL */
589277e4ab7STrond Myklebust static ssize_t
590277e4ab7STrond Myklebust xs_read_stream_call(struct sock_xprt *transport, struct msghdr *msg, int flags)
591277e4ab7STrond Myklebust {
592277e4ab7STrond Myklebust 	return -ESHUTDOWN;
593277e4ab7STrond Myklebust }
594277e4ab7STrond Myklebust #endif /* CONFIG_SUNRPC_BACKCHANNEL */
595277e4ab7STrond Myklebust 
596277e4ab7STrond Myklebust static ssize_t
597277e4ab7STrond Myklebust xs_read_stream_reply(struct sock_xprt *transport, struct msghdr *msg, int flags)
598277e4ab7STrond Myklebust {
599277e4ab7STrond Myklebust 	struct rpc_xprt *xprt = &transport->xprt;
600277e4ab7STrond Myklebust 	struct rpc_rqst *req;
601277e4ab7STrond Myklebust 	ssize_t ret = 0;
602277e4ab7STrond Myklebust 
603277e4ab7STrond Myklebust 	/* Look up and lock the request corresponding to the given XID */
604277e4ab7STrond Myklebust 	spin_lock(&xprt->queue_lock);
605277e4ab7STrond Myklebust 	req = xprt_lookup_rqst(xprt, transport->recv.xid);
60645835a63STrond Myklebust 	if (!req || (transport->recv.copied && !req->rq_private_buf.len)) {
607277e4ab7STrond Myklebust 		msg->msg_flags |= MSG_TRUNC;
608277e4ab7STrond Myklebust 		goto out;
609277e4ab7STrond Myklebust 	}
610277e4ab7STrond Myklebust 	xprt_pin_rqst(req);
611277e4ab7STrond Myklebust 	spin_unlock(&xprt->queue_lock);
612277e4ab7STrond Myklebust 
613277e4ab7STrond Myklebust 	ret = xs_read_stream_request(transport, msg, flags, req);
614277e4ab7STrond Myklebust 
615277e4ab7STrond Myklebust 	spin_lock(&xprt->queue_lock);
616277e4ab7STrond Myklebust 	if (msg->msg_flags & (MSG_EOR|MSG_TRUNC))
617c4433055STrond Myklebust 		xprt_complete_rqst(req->rq_task, transport->recv.copied);
61845835a63STrond Myklebust 	else
61945835a63STrond Myklebust 		req->rq_private_buf.len = transport->recv.copied;
620277e4ab7STrond Myklebust 	xprt_unpin_rqst(req);
621277e4ab7STrond Myklebust out:
622277e4ab7STrond Myklebust 	spin_unlock(&xprt->queue_lock);
623277e4ab7STrond Myklebust 	return ret;
624277e4ab7STrond Myklebust }
625277e4ab7STrond Myklebust 
626277e4ab7STrond Myklebust static ssize_t
627277e4ab7STrond Myklebust xs_read_stream(struct sock_xprt *transport, int flags)
628277e4ab7STrond Myklebust {
629277e4ab7STrond Myklebust 	struct msghdr msg = { 0 };
630277e4ab7STrond Myklebust 	size_t want, read = 0;
631277e4ab7STrond Myklebust 	ssize_t ret = 0;
632277e4ab7STrond Myklebust 
633277e4ab7STrond Myklebust 	if (transport->recv.len == 0) {
634277e4ab7STrond Myklebust 		want = xs_read_stream_headersize(transport->recv.copied != 0);
635277e4ab7STrond Myklebust 		ret = xs_read_stream_header(transport, &msg, flags, want,
636277e4ab7STrond Myklebust 				transport->recv.offset);
637277e4ab7STrond Myklebust 		if (ret <= 0)
638277e4ab7STrond Myklebust 			goto out_err;
639277e4ab7STrond Myklebust 		transport->recv.offset = ret;
640c4433055STrond Myklebust 		if (transport->recv.offset != want)
641c4433055STrond Myklebust 			return transport->recv.offset;
642277e4ab7STrond Myklebust 		transport->recv.len = be32_to_cpu(transport->recv.fraghdr) &
643277e4ab7STrond Myklebust 			RPC_FRAGMENT_SIZE_MASK;
644277e4ab7STrond Myklebust 		transport->recv.offset -= sizeof(transport->recv.fraghdr);
645277e4ab7STrond Myklebust 		read = ret;
646277e4ab7STrond Myklebust 	}
647277e4ab7STrond Myklebust 
648277e4ab7STrond Myklebust 	switch (be32_to_cpu(transport->recv.calldir)) {
649c4433055STrond Myklebust 	default:
650c4433055STrond Myklebust 		msg.msg_flags |= MSG_TRUNC;
651c4433055STrond Myklebust 		break;
652277e4ab7STrond Myklebust 	case RPC_CALL:
653277e4ab7STrond Myklebust 		ret = xs_read_stream_call(transport, &msg, flags);
654277e4ab7STrond Myklebust 		break;
655277e4ab7STrond Myklebust 	case RPC_REPLY:
656277e4ab7STrond Myklebust 		ret = xs_read_stream_reply(transport, &msg, flags);
657277e4ab7STrond Myklebust 	}
658277e4ab7STrond Myklebust 	if (msg.msg_flags & MSG_TRUNC) {
659277e4ab7STrond Myklebust 		transport->recv.calldir = cpu_to_be32(-1);
660277e4ab7STrond Myklebust 		transport->recv.copied = -1;
661277e4ab7STrond Myklebust 	}
662277e4ab7STrond Myklebust 	if (ret < 0)
663277e4ab7STrond Myklebust 		goto out_err;
664277e4ab7STrond Myklebust 	read += ret;
665277e4ab7STrond Myklebust 	if (transport->recv.offset < transport->recv.len) {
666c4433055STrond Myklebust 		if (!(msg.msg_flags & MSG_TRUNC))
667c4433055STrond Myklebust 			return read;
668b76a5afdSTrond Myklebust 		msg.msg_flags = 0;
669277e4ab7STrond Myklebust 		ret = xs_read_discard(transport->sock, &msg, flags,
670277e4ab7STrond Myklebust 				transport->recv.len - transport->recv.offset);
671277e4ab7STrond Myklebust 		if (ret <= 0)
672277e4ab7STrond Myklebust 			goto out_err;
673277e4ab7STrond Myklebust 		transport->recv.offset += ret;
674277e4ab7STrond Myklebust 		read += ret;
675277e4ab7STrond Myklebust 		if (transport->recv.offset != transport->recv.len)
676c4433055STrond Myklebust 			return read;
677277e4ab7STrond Myklebust 	}
678277e4ab7STrond Myklebust 	if (xs_read_stream_request_done(transport)) {
679c50b8ee0STrond Myklebust 		trace_xs_stream_read_request(transport);
680277e4ab7STrond Myklebust 		transport->recv.copied = 0;
681277e4ab7STrond Myklebust 	}
682277e4ab7STrond Myklebust 	transport->recv.offset = 0;
683277e4ab7STrond Myklebust 	transport->recv.len = 0;
684277e4ab7STrond Myklebust 	return read;
685277e4ab7STrond Myklebust out_err:
68679462857STrond Myklebust 	return ret != 0 ? ret : -ESHUTDOWN;
687277e4ab7STrond Myklebust }
688277e4ab7STrond Myklebust 
6890ffe86f4STrond Myklebust static __poll_t xs_poll_socket(struct sock_xprt *transport)
6900ffe86f4STrond Myklebust {
691a73881c9STrond Myklebust 	return transport->sock->ops->poll(transport->file, transport->sock,
692a73881c9STrond Myklebust 			NULL);
6930ffe86f4STrond Myklebust }
6940ffe86f4STrond Myklebust 
6950ffe86f4STrond Myklebust static bool xs_poll_socket_readable(struct sock_xprt *transport)
6960ffe86f4STrond Myklebust {
6970ffe86f4STrond Myklebust 	__poll_t events = xs_poll_socket(transport);
6980ffe86f4STrond Myklebust 
6990ffe86f4STrond Myklebust 	return (events & (EPOLLIN | EPOLLRDNORM)) && !(events & EPOLLRDHUP);
7000ffe86f4STrond Myklebust }
7010ffe86f4STrond Myklebust 
7020ffe86f4STrond Myklebust static void xs_poll_check_readable(struct sock_xprt *transport)
7030ffe86f4STrond Myklebust {
7040ffe86f4STrond Myklebust 
7050ffe86f4STrond Myklebust 	clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state);
7060ffe86f4STrond Myklebust 	if (!xs_poll_socket_readable(transport))
7070ffe86f4STrond Myklebust 		return;
7080ffe86f4STrond Myklebust 	if (!test_and_set_bit(XPRT_SOCK_DATA_READY, &transport->sock_state))
7090ffe86f4STrond Myklebust 		queue_work(xprtiod_workqueue, &transport->recv_worker);
7100ffe86f4STrond Myklebust }
7110ffe86f4STrond Myklebust 
712c50b8ee0STrond Myklebust static void xs_stream_data_receive(struct sock_xprt *transport)
713c50b8ee0STrond Myklebust {
714c50b8ee0STrond Myklebust 	size_t read = 0;
715c50b8ee0STrond Myklebust 	ssize_t ret = 0;
716c50b8ee0STrond Myklebust 
717c50b8ee0STrond Myklebust 	mutex_lock(&transport->recv_mutex);
718c50b8ee0STrond Myklebust 	if (transport->sock == NULL)
719c50b8ee0STrond Myklebust 		goto out;
720c50b8ee0STrond Myklebust 	for (;;) {
721c50b8ee0STrond Myklebust 		ret = xs_read_stream(transport, MSG_DONTWAIT);
722c4433055STrond Myklebust 		if (ret < 0)
723c50b8ee0STrond Myklebust 			break;
724c50b8ee0STrond Myklebust 		read += ret;
725c50b8ee0STrond Myklebust 		cond_resched();
726c50b8ee0STrond Myklebust 	}
7275f52a9d4STrond Myklebust 	if (ret == -ESHUTDOWN)
7285f52a9d4STrond Myklebust 		kernel_sock_shutdown(transport->sock, SHUT_RDWR);
7295f52a9d4STrond Myklebust 	else
7300ffe86f4STrond Myklebust 		xs_poll_check_readable(transport);
731c50b8ee0STrond Myklebust out:
732c50b8ee0STrond Myklebust 	mutex_unlock(&transport->recv_mutex);
733c50b8ee0STrond Myklebust 	trace_xs_stream_read_data(&transport->xprt, ret, read);
734c50b8ee0STrond Myklebust }
735c50b8ee0STrond Myklebust 
736c50b8ee0STrond Myklebust static void xs_stream_data_receive_workfn(struct work_struct *work)
737c50b8ee0STrond Myklebust {
738c50b8ee0STrond Myklebust 	struct sock_xprt *transport =
739c50b8ee0STrond Myklebust 		container_of(work, struct sock_xprt, recv_worker);
740a1231fdaSTrond Myklebust 	unsigned int pflags = memalloc_nofs_save();
741a1231fdaSTrond Myklebust 
742c50b8ee0STrond Myklebust 	xs_stream_data_receive(transport);
743a1231fdaSTrond Myklebust 	memalloc_nofs_restore(pflags);
744c50b8ee0STrond Myklebust }
745c50b8ee0STrond Myklebust 
746550aebfeSTrond Myklebust static void
747550aebfeSTrond Myklebust xs_stream_reset_connect(struct sock_xprt *transport)
748550aebfeSTrond Myklebust {
749550aebfeSTrond Myklebust 	transport->recv.offset = 0;
750550aebfeSTrond Myklebust 	transport->recv.len = 0;
751550aebfeSTrond Myklebust 	transport->recv.copied = 0;
752550aebfeSTrond Myklebust 	transport->xmit.offset = 0;
753ae053551STrond Myklebust }
754ae053551STrond Myklebust 
755ae053551STrond Myklebust static void
756ae053551STrond Myklebust xs_stream_start_connect(struct sock_xprt *transport)
757ae053551STrond Myklebust {
758550aebfeSTrond Myklebust 	transport->xprt.stat.connect_count++;
759550aebfeSTrond Myklebust 	transport->xprt.stat.connect_start = jiffies;
760550aebfeSTrond Myklebust }
761550aebfeSTrond Myklebust 
762b4b5cc85SChuck Lever #define XS_SENDMSG_FLAGS	(MSG_DONTWAIT | MSG_NOSIGNAL)
763b4b5cc85SChuck Lever 
7649903cd1cSChuck Lever /**
765c544577dSTrond Myklebust  * xs_nospace - handle transmit was incomplete
76650f484e2STrond Myklebust  * @req: pointer to RPC request
7677496b59fSTrond Myklebust  * @transport: pointer to struct sock_xprt
7689903cd1cSChuck Lever  *
769a246b010SChuck Lever  */
7707496b59fSTrond Myklebust static int xs_nospace(struct rpc_rqst *req, struct sock_xprt *transport)
771a246b010SChuck Lever {
7727496b59fSTrond Myklebust 	struct rpc_xprt *xprt = &transport->xprt;
77306ea0bfeSTrond Myklebust 	struct sock *sk = transport->inet;
77424ca9a84STrond Myklebust 	int ret = -EAGAIN;
775a246b010SChuck Lever 
776015747d2SChuck Lever 	trace_rpc_socket_nospace(req, transport);
777a246b010SChuck Lever 
778262965f5SChuck Lever 	/* Protect against races with write_space */
779b5e92419STrond Myklebust 	spin_lock(&xprt->transport_lock);
780a246b010SChuck Lever 
781262965f5SChuck Lever 	/* Don't race with disconnect */
782b6ddf64fSTrond Myklebust 	if (xprt_connected(xprt)) {
7837496b59fSTrond Myklebust 		/* wait for more buffer space */
7842790a624STrond Myklebust 		set_bit(XPRT_SOCK_NOSPACE, &transport->sock_state);
7857496b59fSTrond Myklebust 		set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
7867496b59fSTrond Myklebust 		sk->sk_write_pending++;
7877496b59fSTrond Myklebust 		xprt_wait_for_buffer_space(xprt);
7887496b59fSTrond Myklebust 	} else
7897496b59fSTrond Myklebust 		ret = -ENOTCONN;
7907496b59fSTrond Myklebust 
7917496b59fSTrond Myklebust 	spin_unlock(&xprt->transport_lock);
7927496b59fSTrond Myklebust 	return ret;
793d48f9ce7SDavid Vrabel }
7947496b59fSTrond Myklebust 
7957496b59fSTrond Myklebust static int xs_sock_nospace(struct rpc_rqst *req)
7967496b59fSTrond Myklebust {
7977496b59fSTrond Myklebust 	struct sock_xprt *transport =
7987496b59fSTrond Myklebust 		container_of(req->rq_xprt, struct sock_xprt, xprt);
7997496b59fSTrond Myklebust 	struct sock *sk = transport->inet;
8007496b59fSTrond Myklebust 	int ret = -EAGAIN;
8017496b59fSTrond Myklebust 
8027496b59fSTrond Myklebust 	lock_sock(sk);
8037496b59fSTrond Myklebust 	if (!sock_writeable(sk))
8047496b59fSTrond Myklebust 		ret = xs_nospace(req, transport);
8057496b59fSTrond Myklebust 	release_sock(sk);
8067496b59fSTrond Myklebust 	return ret;
8077496b59fSTrond Myklebust }
8087496b59fSTrond Myklebust 
809d0afde5fSTrond Myklebust static int xs_stream_nospace(struct rpc_rqst *req, bool vm_wait)
8107496b59fSTrond Myklebust {
8117496b59fSTrond Myklebust 	struct sock_xprt *transport =
8127496b59fSTrond Myklebust 		container_of(req->rq_xprt, struct sock_xprt, xprt);
8137496b59fSTrond Myklebust 	struct sock *sk = transport->inet;
8147496b59fSTrond Myklebust 	int ret = -EAGAIN;
8157496b59fSTrond Myklebust 
816d0afde5fSTrond Myklebust 	if (vm_wait)
817d0afde5fSTrond Myklebust 		return -ENOBUFS;
8187496b59fSTrond Myklebust 	lock_sock(sk);
8197496b59fSTrond Myklebust 	if (!sk_stream_memory_free(sk))
8207496b59fSTrond Myklebust 		ret = xs_nospace(req, transport);
8217496b59fSTrond Myklebust 	release_sock(sk);
8225e3771ceSTrond Myklebust 	return ret;
823a246b010SChuck Lever }
824a246b010SChuck Lever 
825eb07d5a4SNeilBrown static int
826277e4ab7STrond Myklebust xs_stream_prepare_request(struct rpc_rqst *req)
827277e4ab7STrond Myklebust {
828ff053dbbSTrond Myklebust 	gfp_t gfp = rpc_task_gfp_mask();
829ff053dbbSTrond Myklebust 	int ret;
830ff053dbbSTrond Myklebust 
831ff053dbbSTrond Myklebust 	ret = xdr_alloc_bvec(&req->rq_snd_buf, gfp);
832ff053dbbSTrond Myklebust 	if (ret < 0)
833ff053dbbSTrond Myklebust 		return ret;
83475369089STrond Myklebust 	xdr_free_bvec(&req->rq_rcv_buf);
835ff053dbbSTrond Myklebust 	return xdr_alloc_bvec(&req->rq_rcv_buf, gfp);
836277e4ab7STrond Myklebust }
837277e4ab7STrond Myklebust 
83861677eeeSChuck Lever /*
8394cd34e7cSTrond Myklebust  * Determine if the previous message in the stream was aborted before it
8404cd34e7cSTrond Myklebust  * could complete transmission.
8414cd34e7cSTrond Myklebust  */
8424cd34e7cSTrond Myklebust static bool
8434cd34e7cSTrond Myklebust xs_send_request_was_aborted(struct sock_xprt *transport, struct rpc_rqst *req)
8444cd34e7cSTrond Myklebust {
8454cd34e7cSTrond Myklebust 	return transport->xmit.offset != 0 && req->rq_bytes_sent == 0;
8464cd34e7cSTrond Myklebust }
8474cd34e7cSTrond Myklebust 
8484cd34e7cSTrond Myklebust /*
84906b5fc3aSTrond Myklebust  * Return the stream record marker field for a record of length < 2^31-1
85061677eeeSChuck Lever  */
85106b5fc3aSTrond Myklebust static rpc_fraghdr
85206b5fc3aSTrond Myklebust xs_stream_record_marker(struct xdr_buf *xdr)
85361677eeeSChuck Lever {
85406b5fc3aSTrond Myklebust 	if (!xdr->len)
85506b5fc3aSTrond Myklebust 		return 0;
85606b5fc3aSTrond Myklebust 	return cpu_to_be32(RPC_LAST_STREAM_FRAGMENT | (u32)xdr->len);
85761677eeeSChuck Lever }
85861677eeeSChuck Lever 
8599903cd1cSChuck Lever /**
860176e21eeSChuck Lever  * xs_local_send_request - write an RPC request to an AF_LOCAL socket
86150f484e2STrond Myklebust  * @req: pointer to RPC request
862176e21eeSChuck Lever  *
863176e21eeSChuck Lever  * Return values:
864176e21eeSChuck Lever  *        0:	The request has been sent
865176e21eeSChuck Lever  *   EAGAIN:	The socket was blocked, please call again later to
866176e21eeSChuck Lever  *		complete the request
867176e21eeSChuck Lever  * ENOTCONN:	Caller needs to invoke connect logic then call again
86812b20ce3SBhaskar Chowdhury  *    other:	Some other error occurred, the request was not sent
869176e21eeSChuck Lever  */
870adfa7144STrond Myklebust static int xs_local_send_request(struct rpc_rqst *req)
871176e21eeSChuck Lever {
872176e21eeSChuck Lever 	struct rpc_xprt *xprt = req->rq_xprt;
873176e21eeSChuck Lever 	struct sock_xprt *transport =
874176e21eeSChuck Lever 				container_of(xprt, struct sock_xprt, xprt);
875176e21eeSChuck Lever 	struct xdr_buf *xdr = &req->rq_snd_buf;
8767e3d3620STrond Myklebust 	rpc_fraghdr rm = xs_stream_record_marker(xdr);
8777e3d3620STrond Myklebust 	unsigned int msglen = rm ? req->rq_slen + sizeof(rm) : req->rq_slen;
8789e55eef4SChuck Lever 	struct msghdr msg = {
8799e55eef4SChuck Lever 		.msg_flags	= XS_SENDMSG_FLAGS,
8809e55eef4SChuck Lever 	};
881d0afde5fSTrond Myklebust 	bool vm_wait;
8823f649ab7SKees Cook 	unsigned int sent;
883176e21eeSChuck Lever 	int status;
884176e21eeSChuck Lever 
8854cd34e7cSTrond Myklebust 	/* Close the stream if the previous transmission was incomplete */
8864cd34e7cSTrond Myklebust 	if (xs_send_request_was_aborted(transport, req)) {
887f0043206STrond Myklebust 		xprt_force_disconnect(xprt);
8884cd34e7cSTrond Myklebust 		return -ENOTCONN;
8894cd34e7cSTrond Myklebust 	}
8904cd34e7cSTrond Myklebust 
891176e21eeSChuck Lever 	xs_pktdump("packet data:",
892176e21eeSChuck Lever 			req->rq_svec->iov_base, req->rq_svec->iov_len);
893176e21eeSChuck Lever 
894d0afde5fSTrond Myklebust 	vm_wait = sk_stream_is_writeable(transport->inet) ? true : false;
895d0afde5fSTrond Myklebust 
89678215759SChuck Lever 	req->rq_xtime = ktime_get();
8979e55eef4SChuck Lever 	status = xprt_sock_sendmsg(transport->sock, &msg, xdr,
8987e3d3620STrond Myklebust 				   transport->xmit.offset, rm, &sent);
899176e21eeSChuck Lever 	dprintk("RPC:       %s(%u) = %d\n",
9006c7a64e5STrond Myklebust 			__func__, xdr->len - transport->xmit.offset, status);
901743c69e7SNeilBrown 
902f279cd00SJason Baron 	if (likely(sent > 0) || status == 0) {
9036c7a64e5STrond Myklebust 		transport->xmit.offset += sent;
9046c7a64e5STrond Myklebust 		req->rq_bytes_sent = transport->xmit.offset;
9057e3d3620STrond Myklebust 		if (likely(req->rq_bytes_sent >= msglen)) {
9066c7a64e5STrond Myklebust 			req->rq_xmit_bytes_sent += transport->xmit.offset;
9076c7a64e5STrond Myklebust 			transport->xmit.offset = 0;
908176e21eeSChuck Lever 			return 0;
909176e21eeSChuck Lever 		}
910176e21eeSChuck Lever 		status = -EAGAIN;
911d0afde5fSTrond Myklebust 		vm_wait = false;
912176e21eeSChuck Lever 	}
913176e21eeSChuck Lever 
914176e21eeSChuck Lever 	switch (status) {
915176e21eeSChuck Lever 	case -EAGAIN:
916d0afde5fSTrond Myklebust 		status = xs_stream_nospace(req, vm_wait);
917176e21eeSChuck Lever 		break;
918176e21eeSChuck Lever 	default:
919176e21eeSChuck Lever 		dprintk("RPC:       sendmsg returned unrecognized error %d\n",
920176e21eeSChuck Lever 			-status);
921df561f66SGustavo A. R. Silva 		fallthrough;
922176e21eeSChuck Lever 	case -EPIPE:
923f0043206STrond Myklebust 		xprt_force_disconnect(xprt);
924176e21eeSChuck Lever 		status = -ENOTCONN;
925176e21eeSChuck Lever 	}
926176e21eeSChuck Lever 
927176e21eeSChuck Lever 	return status;
928176e21eeSChuck Lever }
929176e21eeSChuck Lever 
930176e21eeSChuck Lever /**
931262965f5SChuck Lever  * xs_udp_send_request - write an RPC request to a UDP socket
93250f484e2STrond Myklebust  * @req: pointer to RPC request
9339903cd1cSChuck Lever  *
9349903cd1cSChuck Lever  * Return values:
9359903cd1cSChuck Lever  *        0:	The request has been sent
9369903cd1cSChuck Lever  *   EAGAIN:	The socket was blocked, please call again later to
9379903cd1cSChuck Lever  *		complete the request
938262965f5SChuck Lever  * ENOTCONN:	Caller needs to invoke connect logic then call again
93925985edcSLucas De Marchi  *    other:	Some other error occurred, the request was not sent
9409903cd1cSChuck Lever  */
941adfa7144STrond Myklebust static int xs_udp_send_request(struct rpc_rqst *req)
942a246b010SChuck Lever {
943a246b010SChuck Lever 	struct rpc_xprt *xprt = req->rq_xprt;
944ee0ac0c2SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
945262965f5SChuck Lever 	struct xdr_buf *xdr = &req->rq_snd_buf;
9469e55eef4SChuck Lever 	struct msghdr msg = {
9479e55eef4SChuck Lever 		.msg_name	= xs_addr(xprt),
9489e55eef4SChuck Lever 		.msg_namelen	= xprt->addrlen,
9499e55eef4SChuck Lever 		.msg_flags	= XS_SENDMSG_FLAGS,
9509e55eef4SChuck Lever 	};
9513f649ab7SKees Cook 	unsigned int sent;
952262965f5SChuck Lever 	int status;
953262965f5SChuck Lever 
954262965f5SChuck Lever 	xs_pktdump("packet data:",
955262965f5SChuck Lever 				req->rq_svec->iov_base,
956262965f5SChuck Lever 				req->rq_svec->iov_len);
957262965f5SChuck Lever 
95801d37c42STrond Myklebust 	if (!xprt_bound(xprt))
95901d37c42STrond Myklebust 		return -ENOTCONN;
96075891f50STrond Myklebust 
96175891f50STrond Myklebust 	if (!xprt_request_get_cong(xprt, req))
96275891f50STrond Myklebust 		return -EBADSLT;
96375891f50STrond Myklebust 
964ff053dbbSTrond Myklebust 	status = xdr_alloc_bvec(xdr, rpc_task_gfp_mask());
965ff053dbbSTrond Myklebust 	if (status < 0)
966ff053dbbSTrond Myklebust 		return status;
96778215759SChuck Lever 	req->rq_xtime = ktime_get();
9689e55eef4SChuck Lever 	status = xprt_sock_sendmsg(transport->sock, &msg, xdr, 0, 0, &sent);
969262965f5SChuck Lever 
970262965f5SChuck Lever 	dprintk("RPC:       xs_udp_send_request(%u) = %d\n",
9716c7a64e5STrond Myklebust 			xdr->len, status);
972262965f5SChuck Lever 
9733dedbb5cSJason Baron 	/* firewall is blocking us, don't return -EAGAIN or we end up looping */
9743dedbb5cSJason Baron 	if (status == -EPERM)
9753dedbb5cSJason Baron 		goto process_status;
9763dedbb5cSJason Baron 
977743c69e7SNeilBrown 	if (status == -EAGAIN && sock_writeable(transport->inet))
978743c69e7SNeilBrown 		status = -ENOBUFS;
979743c69e7SNeilBrown 
980f279cd00SJason Baron 	if (sent > 0 || status == 0) {
981f279cd00SJason Baron 		req->rq_xmit_bytes_sent += sent;
982f279cd00SJason Baron 		if (sent >= req->rq_slen)
983262965f5SChuck Lever 			return 0;
984262965f5SChuck Lever 		/* Still some bytes left; set up for a retry later. */
985262965f5SChuck Lever 		status = -EAGAIN;
9862199700fSTrond Myklebust 	}
987262965f5SChuck Lever 
9883dedbb5cSJason Baron process_status:
989262965f5SChuck Lever 	switch (status) {
990fba91afbSTrond Myklebust 	case -ENOTSOCK:
991fba91afbSTrond Myklebust 		status = -ENOTCONN;
992fba91afbSTrond Myklebust 		/* Should we call xs_close() here? */
993fba91afbSTrond Myklebust 		break;
994b6ddf64fSTrond Myklebust 	case -EAGAIN:
9957496b59fSTrond Myklebust 		status = xs_sock_nospace(req);
996b6ddf64fSTrond Myklebust 		break;
997262965f5SChuck Lever 	case -ENETUNREACH:
9983601c4a9STrond Myklebust 	case -ENOBUFS:
999262965f5SChuck Lever 	case -EPIPE:
1000262965f5SChuck Lever 	case -ECONNREFUSED:
10013dedbb5cSJason Baron 	case -EPERM:
1002262965f5SChuck Lever 		/* When the server has died, an ICMP port unreachable message
1003262965f5SChuck Lever 		 * prompts ECONNREFUSED. */
100413331a55STrond Myklebust 		break;
100513331a55STrond Myklebust 	default:
100613331a55STrond Myklebust 		dprintk("RPC:       sendmsg returned unrecognized error %d\n",
100713331a55STrond Myklebust 			-status);
1008262965f5SChuck Lever 	}
10095fe46e9dSBian Naimeng 
1010262965f5SChuck Lever 	return status;
1011262965f5SChuck Lever }
1012262965f5SChuck Lever 
1013e06799f9STrond Myklebust /**
1014262965f5SChuck Lever  * xs_tcp_send_request - write an RPC request to a TCP socket
101550f484e2STrond Myklebust  * @req: pointer to RPC request
1016262965f5SChuck Lever  *
1017262965f5SChuck Lever  * Return values:
1018262965f5SChuck Lever  *        0:	The request has been sent
1019262965f5SChuck Lever  *   EAGAIN:	The socket was blocked, please call again later to
1020262965f5SChuck Lever  *		complete the request
1021262965f5SChuck Lever  * ENOTCONN:	Caller needs to invoke connect logic then call again
102225985edcSLucas De Marchi  *    other:	Some other error occurred, the request was not sent
1023262965f5SChuck Lever  *
1024262965f5SChuck Lever  * XXX: In the case of soft timeouts, should we eventually give up
1025262965f5SChuck Lever  *	if sendmsg is not able to make progress?
1026262965f5SChuck Lever  */
1027adfa7144STrond Myklebust static int xs_tcp_send_request(struct rpc_rqst *req)
1028262965f5SChuck Lever {
1029262965f5SChuck Lever 	struct rpc_xprt *xprt = req->rq_xprt;
1030ee0ac0c2SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1031262965f5SChuck Lever 	struct xdr_buf *xdr = &req->rq_snd_buf;
10327e3d3620STrond Myklebust 	rpc_fraghdr rm = xs_stream_record_marker(xdr);
10337e3d3620STrond Myklebust 	unsigned int msglen = rm ? req->rq_slen + sizeof(rm) : req->rq_slen;
10349e55eef4SChuck Lever 	struct msghdr msg = {
10359e55eef4SChuck Lever 		.msg_flags	= XS_SENDMSG_FLAGS,
10369e55eef4SChuck Lever 	};
1037d0afde5fSTrond Myklebust 	bool vm_wait;
10383f649ab7SKees Cook 	unsigned int sent;
1039b595bb15SChuck Lever 	int status;
1040a246b010SChuck Lever 
10414cd34e7cSTrond Myklebust 	/* Close the stream if the previous transmission was incomplete */
10424cd34e7cSTrond Myklebust 	if (xs_send_request_was_aborted(transport, req)) {
10434cd34e7cSTrond Myklebust 		if (transport->sock != NULL)
10444cd34e7cSTrond Myklebust 			kernel_sock_shutdown(transport->sock, SHUT_RDWR);
10454cd34e7cSTrond Myklebust 		return -ENOTCONN;
10464cd34e7cSTrond Myklebust 	}
1047d275880aSTrond Myklebust 	if (!transport->inet)
1048d275880aSTrond Myklebust 		return -ENOTCONN;
10494cd34e7cSTrond Myklebust 
1050262965f5SChuck Lever 	xs_pktdump("packet data:",
1051262965f5SChuck Lever 				req->rq_svec->iov_base,
1052262965f5SChuck Lever 				req->rq_svec->iov_len);
1053a246b010SChuck Lever 
10547196dbb0STrond Myklebust 	if (test_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state))
10557196dbb0STrond Myklebust 		xs_tcp_set_socket_timeouts(xprt, transport->sock);
10567196dbb0STrond Myklebust 
10573b21f757STrond Myklebust 	xs_set_srcport(transport, transport->sock);
10583b21f757STrond Myklebust 
1059a246b010SChuck Lever 	/* Continue transmitting the packet/record. We must be careful
1060a246b010SChuck Lever 	 * to cope with writespace callbacks arriving _after_ we have
1061262965f5SChuck Lever 	 * called sendmsg(). */
106278215759SChuck Lever 	req->rq_xtime = ktime_get();
1063d737e5d4STrond Myklebust 	tcp_sock_set_cork(transport->inet, true);
1064d0afde5fSTrond Myklebust 
1065d0afde5fSTrond Myklebust 	vm_wait = sk_stream_is_writeable(transport->inet) ? true : false;
1066d0afde5fSTrond Myklebust 
1067d0afde5fSTrond Myklebust 	do {
10689e55eef4SChuck Lever 		status = xprt_sock_sendmsg(transport->sock, &msg, xdr,
10697e3d3620STrond Myklebust 					   transport->xmit.offset, rm, &sent);
1070a246b010SChuck Lever 
1071262965f5SChuck Lever 		dprintk("RPC:       xs_tcp_send_request(%u) = %d\n",
10726c7a64e5STrond Myklebust 				xdr->len - transport->xmit.offset, status);
1073262965f5SChuck Lever 
1074a246b010SChuck Lever 		/* If we've sent the entire packet, immediately
1075a246b010SChuck Lever 		 * reset the count of bytes sent. */
10766c7a64e5STrond Myklebust 		transport->xmit.offset += sent;
10776c7a64e5STrond Myklebust 		req->rq_bytes_sent = transport->xmit.offset;
10787e3d3620STrond Myklebust 		if (likely(req->rq_bytes_sent >= msglen)) {
10796c7a64e5STrond Myklebust 			req->rq_xmit_bytes_sent += transport->xmit.offset;
10806c7a64e5STrond Myklebust 			transport->xmit.offset = 0;
1081d737e5d4STrond Myklebust 			if (atomic_long_read(&xprt->xmit_queuelen) == 1)
1082d737e5d4STrond Myklebust 				tcp_sock_set_cork(transport->inet, false);
1083a246b010SChuck Lever 			return 0;
1084a246b010SChuck Lever 		}
1085262965f5SChuck Lever 
10869ffadfbcSTrond Myklebust 		WARN_ON_ONCE(sent == 0 && status == 0);
10879ffadfbcSTrond Myklebust 
1088d0afde5fSTrond Myklebust 		if (sent > 0)
10899ffadfbcSTrond Myklebust 			vm_wait = false;
1090d0afde5fSTrond Myklebust 
1091d0afde5fSTrond Myklebust 	} while (status == 0);
1092a246b010SChuck Lever 
1093262965f5SChuck Lever 	switch (status) {
1094fba91afbSTrond Myklebust 	case -ENOTSOCK:
1095fba91afbSTrond Myklebust 		status = -ENOTCONN;
1096fba91afbSTrond Myklebust 		/* Should we call xs_close() here? */
1097fba91afbSTrond Myklebust 		break;
1098262965f5SChuck Lever 	case -EAGAIN:
1099d0afde5fSTrond Myklebust 		status = xs_stream_nospace(req, vm_wait);
1100262965f5SChuck Lever 		break;
1101262965f5SChuck Lever 	case -ECONNRESET:
1102262965f5SChuck Lever 	case -ECONNREFUSED:
1103262965f5SChuck Lever 	case -ENOTCONN:
11043913c78cSTrond Myklebust 	case -EADDRINUSE:
1105b5872f0cSTrond Myklebust 	case -ENOBUFS:
1106b9d2bb2eSTrond Myklebust 	case -EPIPE:
110713331a55STrond Myklebust 		break;
110813331a55STrond Myklebust 	default:
110913331a55STrond Myklebust 		dprintk("RPC:       sendmsg returned unrecognized error %d\n",
111013331a55STrond Myklebust 			-status);
1111a246b010SChuck Lever 	}
11125fe46e9dSBian Naimeng 
1113a246b010SChuck Lever 	return status;
1114a246b010SChuck Lever }
1115a246b010SChuck Lever 
11162a9e1cfaSTrond Myklebust static void xs_save_old_callbacks(struct sock_xprt *transport, struct sock *sk)
11172a9e1cfaSTrond Myklebust {
11182a9e1cfaSTrond Myklebust 	transport->old_data_ready = sk->sk_data_ready;
11192a9e1cfaSTrond Myklebust 	transport->old_state_change = sk->sk_state_change;
11202a9e1cfaSTrond Myklebust 	transport->old_write_space = sk->sk_write_space;
11212118071dSTrond Myklebust 	transport->old_error_report = sk->sk_error_report;
11222a9e1cfaSTrond Myklebust }
11232a9e1cfaSTrond Myklebust 
11242a9e1cfaSTrond Myklebust static void xs_restore_old_callbacks(struct sock_xprt *transport, struct sock *sk)
11252a9e1cfaSTrond Myklebust {
11262a9e1cfaSTrond Myklebust 	sk->sk_data_ready = transport->old_data_ready;
11272a9e1cfaSTrond Myklebust 	sk->sk_state_change = transport->old_state_change;
11282a9e1cfaSTrond Myklebust 	sk->sk_write_space = transport->old_write_space;
11292118071dSTrond Myklebust 	sk->sk_error_report = transport->old_error_report;
11302118071dSTrond Myklebust }
11312118071dSTrond Myklebust 
113242d42a5bSTrond Myklebust static void xs_sock_reset_state_flags(struct rpc_xprt *xprt)
113342d42a5bSTrond Myklebust {
113442d42a5bSTrond Myklebust 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
113542d42a5bSTrond Myklebust 
113642d42a5bSTrond Myklebust 	clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state);
11374f8943f8STrond Myklebust 	clear_bit(XPRT_SOCK_WAKE_ERROR, &transport->sock_state);
11384f8943f8STrond Myklebust 	clear_bit(XPRT_SOCK_WAKE_WRITE, &transport->sock_state);
11394f8943f8STrond Myklebust 	clear_bit(XPRT_SOCK_WAKE_DISCONNECT, &transport->sock_state);
11402790a624STrond Myklebust 	clear_bit(XPRT_SOCK_NOSPACE, &transport->sock_state);
11414f8943f8STrond Myklebust }
11424f8943f8STrond Myklebust 
11434f8943f8STrond Myklebust static void xs_run_error_worker(struct sock_xprt *transport, unsigned int nr)
11444f8943f8STrond Myklebust {
11454f8943f8STrond Myklebust 	set_bit(nr, &transport->sock_state);
11464f8943f8STrond Myklebust 	queue_work(xprtiod_workqueue, &transport->error_worker);
114742d42a5bSTrond Myklebust }
114842d42a5bSTrond Myklebust 
1149b70ae915STrond Myklebust static void xs_sock_reset_connection_flags(struct rpc_xprt *xprt)
1150b70ae915STrond Myklebust {
1151d896ba83STrond Myklebust 	xprt->connect_cookie++;
1152b70ae915STrond Myklebust 	smp_mb__before_atomic();
1153b70ae915STrond Myklebust 	clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
1154b70ae915STrond Myklebust 	clear_bit(XPRT_CLOSING, &xprt->state);
115542d42a5bSTrond Myklebust 	xs_sock_reset_state_flags(xprt);
1156b70ae915STrond Myklebust 	smp_mb__after_atomic();
1157b70ae915STrond Myklebust }
1158b70ae915STrond Myklebust 
11592118071dSTrond Myklebust /**
11602118071dSTrond Myklebust  * xs_error_report - callback to handle TCP socket state errors
11612118071dSTrond Myklebust  * @sk: socket
11622118071dSTrond Myklebust  *
11632118071dSTrond Myklebust  * Note: we don't call sock_error() since there may be a rpc_task
11642118071dSTrond Myklebust  * using the socket, and so we don't want to clear sk->sk_err.
11652118071dSTrond Myklebust  */
11662118071dSTrond Myklebust static void xs_error_report(struct sock *sk)
11672118071dSTrond Myklebust {
11684f8943f8STrond Myklebust 	struct sock_xprt *transport;
11692118071dSTrond Myklebust 	struct rpc_xprt *xprt;
11702118071dSTrond Myklebust 
11712118071dSTrond Myklebust 	if (!(xprt = xprt_from_sock(sk)))
1172ea9afca8STrond Myklebust 		return;
11732118071dSTrond Myklebust 
11744f8943f8STrond Myklebust 	transport = container_of(xprt, struct sock_xprt, xprt);
1175af84537dSBenjamin Coddington 	transport->xprt_err = -sk->sk_err;
1176af84537dSBenjamin Coddington 	if (transport->xprt_err == 0)
1177ea9afca8STrond Myklebust 		return;
11782118071dSTrond Myklebust 	dprintk("RPC:       xs_error_report client %p, error=%d...\n",
1179af84537dSBenjamin Coddington 			xprt, -transport->xprt_err);
1180af84537dSBenjamin Coddington 	trace_rpc_socket_error(xprt, sk->sk_socket, transport->xprt_err);
1181af84537dSBenjamin Coddington 
1182af84537dSBenjamin Coddington 	/* barrier ensures xprt_err is set before XPRT_SOCK_WAKE_ERROR */
1183af84537dSBenjamin Coddington 	smp_mb__before_atomic();
11844f8943f8STrond Myklebust 	xs_run_error_worker(transport, XPRT_SOCK_WAKE_ERROR);
11852a9e1cfaSTrond Myklebust }
11862a9e1cfaSTrond Myklebust 
1187fe315e76SChuck Lever static void xs_reset_transport(struct sock_xprt *transport)
1188a246b010SChuck Lever {
1189ee0ac0c2SChuck Lever 	struct socket *sock = transport->sock;
1190ee0ac0c2SChuck Lever 	struct sock *sk = transport->inet;
11916cc7e908STrond Myklebust 	struct rpc_xprt *xprt = &transport->xprt;
1192a73881c9STrond Myklebust 	struct file *filp = transport->file;
1193a246b010SChuck Lever 
1194fe315e76SChuck Lever 	if (sk == NULL)
1195fe315e76SChuck Lever 		return;
1196f0043206STrond Myklebust 	/*
1197f0043206STrond Myklebust 	 * Make sure we're calling this in a context from which it is safe
1198f0043206STrond Myklebust 	 * to call __fput_sync(). In practice that means rpciod and the
1199f0043206STrond Myklebust 	 * system workqueue.
1200f0043206STrond Myklebust 	 */
1201f0043206STrond Myklebust 	if (!(current->flags & PF_WQ_WORKER)) {
1202f0043206STrond Myklebust 		WARN_ON_ONCE(1);
1203f0043206STrond Myklebust 		set_bit(XPRT_CLOSE_WAIT, &xprt->state);
1204f0043206STrond Myklebust 		return;
1205f0043206STrond Myklebust 	}
12069903cd1cSChuck Lever 
1207264d1df3SJeff Layton 	if (atomic_read(&transport->xprt.swapper))
1208264d1df3SJeff Layton 		sk_clear_memalloc(sk);
1209264d1df3SJeff Layton 
121009939204STrond Myklebust 	kernel_sock_shutdown(sock, SHUT_RDWR);
121109939204STrond Myklebust 
1212edc1b01cSTrond Myklebust 	mutex_lock(&transport->recv_mutex);
1213ea9afca8STrond Myklebust 	lock_sock(sk);
1214ee0ac0c2SChuck Lever 	transport->inet = NULL;
1215ee0ac0c2SChuck Lever 	transport->sock = NULL;
1216a73881c9STrond Myklebust 	transport->file = NULL;
1217a246b010SChuck Lever 
1218a246b010SChuck Lever 	sk->sk_user_data = NULL;
12192a9e1cfaSTrond Myklebust 
12202a9e1cfaSTrond Myklebust 	xs_restore_old_callbacks(transport, sk);
12210c78789eSTrond Myklebust 	xprt_clear_connected(xprt);
12226cc7e908STrond Myklebust 	xs_sock_reset_connection_flags(xprt);
1223ae053551STrond Myklebust 	/* Reset stream record info */
1224ae053551STrond Myklebust 	xs_stream_reset_connect(transport);
1225ea9afca8STrond Myklebust 	release_sock(sk);
1226edc1b01cSTrond Myklebust 	mutex_unlock(&transport->recv_mutex);
1227a246b010SChuck Lever 
12286cc7e908STrond Myklebust 	trace_rpc_socket_close(xprt, sock);
1229f0043206STrond Myklebust 	__fput_sync(filp);
12300445f92cSTrond Myklebust 
12310445f92cSTrond Myklebust 	xprt_disconnect_done(xprt);
1232fe315e76SChuck Lever }
1233fe315e76SChuck Lever 
1234fe315e76SChuck Lever /**
1235fe315e76SChuck Lever  * xs_close - close a socket
1236fe315e76SChuck Lever  * @xprt: transport
1237fe315e76SChuck Lever  *
1238fe315e76SChuck Lever  * This is used when all requests are complete; ie, no DRC state remains
1239fe315e76SChuck Lever  * on the server we want to save.
1240f75e6745STrond Myklebust  *
1241f75e6745STrond Myklebust  * The caller _must_ be holding XPRT_LOCKED in order to avoid issues with
1242f75e6745STrond Myklebust  * xs_reset_transport() zeroing the socket from underneath a writer.
1243fe315e76SChuck Lever  */
1244fe315e76SChuck Lever static void xs_close(struct rpc_xprt *xprt)
1245fe315e76SChuck Lever {
1246fe315e76SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1247fe315e76SChuck Lever 
1248fe315e76SChuck Lever 	dprintk("RPC:       xs_close xprt %p\n", xprt);
1249fe315e76SChuck Lever 
1250fe315e76SChuck Lever 	xs_reset_transport(transport);
125161d0a8e6SNeil Brown 	xprt->reestablish_timeout = 0;
1252a246b010SChuck Lever }
1253a246b010SChuck Lever 
12544a068258SChuck Lever static void xs_inject_disconnect(struct rpc_xprt *xprt)
12554a068258SChuck Lever {
12564a068258SChuck Lever 	dprintk("RPC:       injecting transport disconnect on xprt=%p\n",
12574a068258SChuck Lever 		xprt);
12584a068258SChuck Lever 	xprt_disconnect_done(xprt);
12594a068258SChuck Lever }
12604a068258SChuck Lever 
1261315f3812SKinglong Mee static void xs_xprt_free(struct rpc_xprt *xprt)
1262315f3812SKinglong Mee {
1263315f3812SKinglong Mee 	xs_free_peer_addresses(xprt);
1264315f3812SKinglong Mee 	xprt_free(xprt);
1265315f3812SKinglong Mee }
1266315f3812SKinglong Mee 
12679903cd1cSChuck Lever /**
12689903cd1cSChuck Lever  * xs_destroy - prepare to shutdown a transport
12699903cd1cSChuck Lever  * @xprt: doomed transport
12709903cd1cSChuck Lever  *
12719903cd1cSChuck Lever  */
12729903cd1cSChuck Lever static void xs_destroy(struct rpc_xprt *xprt)
1273a246b010SChuck Lever {
127403c78827STrond Myklebust 	struct sock_xprt *transport = container_of(xprt,
127503c78827STrond Myklebust 			struct sock_xprt, xprt);
12769903cd1cSChuck Lever 	dprintk("RPC:       xs_destroy xprt %p\n", xprt);
12779903cd1cSChuck Lever 
127803c78827STrond Myklebust 	cancel_delayed_work_sync(&transport->connect_worker);
1279a1311d87STrond Myklebust 	xs_close(xprt);
1280edc1b01cSTrond Myklebust 	cancel_work_sync(&transport->recv_worker);
1281b5e92419STrond Myklebust 	cancel_work_sync(&transport->error_worker);
1282315f3812SKinglong Mee 	xs_xprt_free(xprt);
1283a1311d87STrond Myklebust 	module_put(THIS_MODULE);
1284a246b010SChuck Lever }
1285a246b010SChuck Lever 
12869903cd1cSChuck Lever /**
1287f9b2ee71STrond Myklebust  * xs_udp_data_read_skb - receive callback for UDP sockets
1288f9b2ee71STrond Myklebust  * @xprt: transport
1289f9b2ee71STrond Myklebust  * @sk: socket
1290f9b2ee71STrond Myklebust  * @skb: skbuff
12919903cd1cSChuck Lever  *
1292a246b010SChuck Lever  */
1293f9b2ee71STrond Myklebust static void xs_udp_data_read_skb(struct rpc_xprt *xprt,
1294f9b2ee71STrond Myklebust 		struct sock *sk,
1295f9b2ee71STrond Myklebust 		struct sk_buff *skb)
1296a246b010SChuck Lever {
1297a246b010SChuck Lever 	struct rpc_task *task;
1298a246b010SChuck Lever 	struct rpc_rqst *rovr;
1299f9b2ee71STrond Myklebust 	int repsize, copied;
1300d8ed029dSAlexey Dobriyan 	u32 _xid;
1301d8ed029dSAlexey Dobriyan 	__be32 *xp;
1302a246b010SChuck Lever 
13031da8c681SWillem de Bruijn 	repsize = skb->len;
1304a246b010SChuck Lever 	if (repsize < 4) {
13059903cd1cSChuck Lever 		dprintk("RPC:       impossible RPC reply size %d!\n", repsize);
1306f9b2ee71STrond Myklebust 		return;
1307a246b010SChuck Lever 	}
1308a246b010SChuck Lever 
1309a246b010SChuck Lever 	/* Copy the XID from the skb... */
13101da8c681SWillem de Bruijn 	xp = skb_header_pointer(skb, 0, sizeof(_xid), &_xid);
1311a246b010SChuck Lever 	if (xp == NULL)
1312f9b2ee71STrond Myklebust 		return;
1313a246b010SChuck Lever 
1314a246b010SChuck Lever 	/* Look up and lock the request corresponding to the given XID */
131575c84151STrond Myklebust 	spin_lock(&xprt->queue_lock);
1316a246b010SChuck Lever 	rovr = xprt_lookup_rqst(xprt, *xp);
1317a246b010SChuck Lever 	if (!rovr)
1318a246b010SChuck Lever 		goto out_unlock;
1319729749bbSTrond Myklebust 	xprt_pin_rqst(rovr);
1320ecd465eeSChuck Lever 	xprt_update_rtt(rovr->rq_task);
132175c84151STrond Myklebust 	spin_unlock(&xprt->queue_lock);
1322a246b010SChuck Lever 	task = rovr->rq_task;
1323a246b010SChuck Lever 
1324a246b010SChuck Lever 	if ((copied = rovr->rq_private_buf.buflen) > repsize)
1325a246b010SChuck Lever 		copied = repsize;
1326a246b010SChuck Lever 
1327a246b010SChuck Lever 	/* Suck it into the iovec, verify checksum if not done by hw. */
13281781f7f5SHerbert Xu 	if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) {
132975c84151STrond Myklebust 		spin_lock(&xprt->queue_lock);
13300afa6b44STrond Myklebust 		__UDPX_INC_STATS(sk, UDP_MIB_INERRORS);
1331729749bbSTrond Myklebust 		goto out_unpin;
13321781f7f5SHerbert Xu 	}
13331781f7f5SHerbert Xu 
1334a246b010SChuck Lever 
1335b5e92419STrond Myklebust 	spin_lock(&xprt->transport_lock);
13366a24dfb6STrond Myklebust 	xprt_adjust_cwnd(xprt, task, copied);
1337b5e92419STrond Myklebust 	spin_unlock(&xprt->transport_lock);
133875c84151STrond Myklebust 	spin_lock(&xprt->queue_lock);
13391570c1e4SChuck Lever 	xprt_complete_rqst(task, copied);
13400afa6b44STrond Myklebust 	__UDPX_INC_STATS(sk, UDP_MIB_INDATAGRAMS);
1341729749bbSTrond Myklebust out_unpin:
1342729749bbSTrond Myklebust 	xprt_unpin_rqst(rovr);
1343a246b010SChuck Lever  out_unlock:
134475c84151STrond Myklebust 	spin_unlock(&xprt->queue_lock);
1345f9b2ee71STrond Myklebust }
1346f9b2ee71STrond Myklebust 
1347f9b2ee71STrond Myklebust static void xs_udp_data_receive(struct sock_xprt *transport)
1348f9b2ee71STrond Myklebust {
1349f9b2ee71STrond Myklebust 	struct sk_buff *skb;
1350f9b2ee71STrond Myklebust 	struct sock *sk;
1351f9b2ee71STrond Myklebust 	int err;
1352f9b2ee71STrond Myklebust 
1353f9b2ee71STrond Myklebust 	mutex_lock(&transport->recv_mutex);
1354f9b2ee71STrond Myklebust 	sk = transport->inet;
1355f9b2ee71STrond Myklebust 	if (sk == NULL)
1356f9b2ee71STrond Myklebust 		goto out;
1357f9b2ee71STrond Myklebust 	for (;;) {
1358ec095263SOliver Hartkopp 		skb = skb_recv_udp(sk, MSG_DONTWAIT, &err);
13594f546149STrond Myklebust 		if (skb == NULL)
13604f546149STrond Myklebust 			break;
1361f9b2ee71STrond Myklebust 		xs_udp_data_read_skb(&transport->xprt, sk, skb);
1362850cbaddSPaolo Abeni 		consume_skb(skb);
13630af3442aSTrond Myklebust 		cond_resched();
1364f9b2ee71STrond Myklebust 	}
13650ffe86f4STrond Myklebust 	xs_poll_check_readable(transport);
1366a246b010SChuck Lever out:
1367f9b2ee71STrond Myklebust 	mutex_unlock(&transport->recv_mutex);
1368f9b2ee71STrond Myklebust }
1369f9b2ee71STrond Myklebust 
1370f9b2ee71STrond Myklebust static void xs_udp_data_receive_workfn(struct work_struct *work)
1371f9b2ee71STrond Myklebust {
1372f9b2ee71STrond Myklebust 	struct sock_xprt *transport =
1373f9b2ee71STrond Myklebust 		container_of(work, struct sock_xprt, recv_worker);
1374a1231fdaSTrond Myklebust 	unsigned int pflags = memalloc_nofs_save();
1375a1231fdaSTrond Myklebust 
1376f9b2ee71STrond Myklebust 	xs_udp_data_receive(transport);
1377a1231fdaSTrond Myklebust 	memalloc_nofs_restore(pflags);
1378f9b2ee71STrond Myklebust }
1379f9b2ee71STrond Myklebust 
1380f9b2ee71STrond Myklebust /**
1381*f67939e4SChuck Lever  * xs_data_ready - "data ready" callback for sockets
1382f9b2ee71STrond Myklebust  * @sk: socket with data to read
1383f9b2ee71STrond Myklebust  *
1384f9b2ee71STrond Myklebust  */
1385f9b2ee71STrond Myklebust static void xs_data_ready(struct sock *sk)
1386f9b2ee71STrond Myklebust {
1387f9b2ee71STrond Myklebust 	struct rpc_xprt *xprt;
1388f9b2ee71STrond Myklebust 
1389f9b2ee71STrond Myklebust 	xprt = xprt_from_sock(sk);
1390f9b2ee71STrond Myklebust 	if (xprt != NULL) {
1391f9b2ee71STrond Myklebust 		struct sock_xprt *transport = container_of(xprt,
1392f9b2ee71STrond Myklebust 				struct sock_xprt, xprt);
1393*f67939e4SChuck Lever 
1394*f67939e4SChuck Lever 		trace_xs_data_ready(xprt);
1395*f67939e4SChuck Lever 
13965157b956STrond Myklebust 		transport->old_data_ready(sk);
13975157b956STrond Myklebust 		/* Any data means we had a useful conversation, so
13985157b956STrond Myklebust 		 * then we don't need to delay the next reconnect
13995157b956STrond Myklebust 		 */
14005157b956STrond Myklebust 		if (xprt->reestablish_timeout)
14015157b956STrond Myklebust 			xprt->reestablish_timeout = 0;
140242d42a5bSTrond Myklebust 		if (!test_and_set_bit(XPRT_SOCK_DATA_READY, &transport->sock_state))
140340a5f1b1STrond Myklebust 			queue_work(xprtiod_workqueue, &transport->recv_worker);
1404f9b2ee71STrond Myklebust 	}
1405a246b010SChuck Lever }
1406a246b010SChuck Lever 
1407a519fc7aSTrond Myklebust /*
1408a519fc7aSTrond Myklebust  * Helper function to force a TCP close if the server is sending
1409a519fc7aSTrond Myklebust  * junk and/or it has put us in CLOSE_WAIT
1410a519fc7aSTrond Myklebust  */
1411a519fc7aSTrond Myklebust static void xs_tcp_force_close(struct rpc_xprt *xprt)
1412a519fc7aSTrond Myklebust {
1413a519fc7aSTrond Myklebust 	xprt_force_disconnect(xprt);
1414a519fc7aSTrond Myklebust }
1415a519fc7aSTrond Myklebust 
14169e00abc3STrond Myklebust #if defined(CONFIG_SUNRPC_BACKCHANNEL)
14176b26cc8cSChuck Lever static size_t xs_tcp_bc_maxpayload(struct rpc_xprt *xprt)
14186b26cc8cSChuck Lever {
14196b26cc8cSChuck Lever 	return PAGE_SIZE;
14206b26cc8cSChuck Lever }
14219e00abc3STrond Myklebust #endif /* CONFIG_SUNRPC_BACKCHANNEL */
142244b98efdSRicardo Labiaga 
14239903cd1cSChuck Lever /**
1424efce2d0bSTrond Myklebust  * xs_local_state_change - callback to handle AF_LOCAL socket state changes
1425efce2d0bSTrond Myklebust  * @sk: socket whose state has changed
1426efce2d0bSTrond Myklebust  *
1427efce2d0bSTrond Myklebust  */
1428efce2d0bSTrond Myklebust static void xs_local_state_change(struct sock *sk)
1429efce2d0bSTrond Myklebust {
1430efce2d0bSTrond Myklebust 	struct rpc_xprt *xprt;
1431efce2d0bSTrond Myklebust 	struct sock_xprt *transport;
1432efce2d0bSTrond Myklebust 
1433efce2d0bSTrond Myklebust 	if (!(xprt = xprt_from_sock(sk)))
1434efce2d0bSTrond Myklebust 		return;
1435efce2d0bSTrond Myklebust 	transport = container_of(xprt, struct sock_xprt, xprt);
1436efce2d0bSTrond Myklebust 	if (sk->sk_shutdown & SHUTDOWN_MASK) {
1437efce2d0bSTrond Myklebust 		clear_bit(XPRT_CONNECTED, &xprt->state);
1438efce2d0bSTrond Myklebust 		/* Trigger the socket release */
1439efce2d0bSTrond Myklebust 		xs_run_error_worker(transport, XPRT_SOCK_WAKE_DISCONNECT);
1440efce2d0bSTrond Myklebust 	}
1441efce2d0bSTrond Myklebust }
1442efce2d0bSTrond Myklebust 
1443efce2d0bSTrond Myklebust /**
14449903cd1cSChuck Lever  * xs_tcp_state_change - callback to handle TCP socket state changes
14459903cd1cSChuck Lever  * @sk: socket whose state has changed
14469903cd1cSChuck Lever  *
14479903cd1cSChuck Lever  */
14489903cd1cSChuck Lever static void xs_tcp_state_change(struct sock *sk)
1449a246b010SChuck Lever {
1450a246b010SChuck Lever 	struct rpc_xprt *xprt;
14510fdea1e8STrond Myklebust 	struct sock_xprt *transport;
1452a246b010SChuck Lever 
1453a246b010SChuck Lever 	if (!(xprt = xprt_from_sock(sk)))
1454ea9afca8STrond Myklebust 		return;
14559903cd1cSChuck Lever 	dprintk("RPC:       xs_tcp_state_change client %p...\n", xprt);
1456669502ffSAndy Chittenden 	dprintk("RPC:       state %x conn %d dead %d zapped %d sk_shutdown %d\n",
1457a246b010SChuck Lever 			sk->sk_state, xprt_connected(xprt),
1458a246b010SChuck Lever 			sock_flag(sk, SOCK_DEAD),
1459669502ffSAndy Chittenden 			sock_flag(sk, SOCK_ZAPPED),
1460669502ffSAndy Chittenden 			sk->sk_shutdown);
1461a246b010SChuck Lever 
14620fdea1e8STrond Myklebust 	transport = container_of(xprt, struct sock_xprt, xprt);
146340b5ea0cSTrond Myklebust 	trace_rpc_socket_state_change(xprt, sk->sk_socket);
1464a246b010SChuck Lever 	switch (sk->sk_state) {
1465a246b010SChuck Lever 	case TCP_ESTABLISHED:
1466a246b010SChuck Lever 		if (!xprt_test_and_set_connected(xprt)) {
14678b71798cSTrond Myklebust 			xprt->connect_cookie++;
14680fdea1e8STrond Myklebust 			clear_bit(XPRT_SOCK_CONNECTING, &transport->sock_state);
14690fdea1e8STrond Myklebust 			xprt_clear_connecting(xprt);
147051971139SChuck Lever 
14713968a8a5SChuck Lever 			xprt->stat.connect_count++;
14723968a8a5SChuck Lever 			xprt->stat.connect_time += (long)jiffies -
14733968a8a5SChuck Lever 						   xprt->stat.connect_start;
14744f8943f8STrond Myklebust 			xs_run_error_worker(transport, XPRT_SOCK_WAKE_PENDING);
1475a246b010SChuck Lever 		}
1476a246b010SChuck Lever 		break;
14773b948ae5STrond Myklebust 	case TCP_FIN_WAIT1:
14783b948ae5STrond Myklebust 		/* The client initiated a shutdown of the socket */
14797c1d71cfSTrond Myklebust 		xprt->connect_cookie++;
1480663b8858STrond Myklebust 		xprt->reestablish_timeout = 0;
14813b948ae5STrond Myklebust 		set_bit(XPRT_CLOSING, &xprt->state);
14824e857c58SPeter Zijlstra 		smp_mb__before_atomic();
14833b948ae5STrond Myklebust 		clear_bit(XPRT_CONNECTED, &xprt->state);
1484ef803670STrond Myklebust 		clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
14854e857c58SPeter Zijlstra 		smp_mb__after_atomic();
1486a246b010SChuck Lever 		break;
1487632e3bdcSTrond Myklebust 	case TCP_CLOSE_WAIT:
14883b948ae5STrond Myklebust 		/* The server initiated a shutdown of the socket */
14897c1d71cfSTrond Myklebust 		xprt->connect_cookie++;
1490d0bea455STrond Myklebust 		clear_bit(XPRT_CONNECTED, &xprt->state);
14914f8943f8STrond Myklebust 		xs_run_error_worker(transport, XPRT_SOCK_WAKE_DISCONNECT);
1492df561f66SGustavo A. R. Silva 		fallthrough;
1493663b8858STrond Myklebust 	case TCP_CLOSING:
1494663b8858STrond Myklebust 		/*
1495663b8858STrond Myklebust 		 * If the server closed down the connection, make sure that
1496663b8858STrond Myklebust 		 * we back off before reconnecting
1497663b8858STrond Myklebust 		 */
1498663b8858STrond Myklebust 		if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
1499663b8858STrond Myklebust 			xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
15003b948ae5STrond Myklebust 		break;
15013b948ae5STrond Myklebust 	case TCP_LAST_ACK:
1502670f9457STrond Myklebust 		set_bit(XPRT_CLOSING, &xprt->state);
15034e857c58SPeter Zijlstra 		smp_mb__before_atomic();
15043b948ae5STrond Myklebust 		clear_bit(XPRT_CONNECTED, &xprt->state);
15054e857c58SPeter Zijlstra 		smp_mb__after_atomic();
15063b948ae5STrond Myklebust 		break;
15073b948ae5STrond Myklebust 	case TCP_CLOSE:
15080fdea1e8STrond Myklebust 		if (test_and_clear_bit(XPRT_SOCK_CONNECTING,
15090fdea1e8STrond Myklebust 					&transport->sock_state))
15100fdea1e8STrond Myklebust 			xprt_clear_connecting(xprt);
15119b30889cSTrond Myklebust 		clear_bit(XPRT_CLOSING, &xprt->state);
15129b30889cSTrond Myklebust 		/* Trigger the socket release */
15134f8943f8STrond Myklebust 		xs_run_error_worker(transport, XPRT_SOCK_WAKE_DISCONNECT);
1514a246b010SChuck Lever 	}
1515a246b010SChuck Lever }
1516a246b010SChuck Lever 
15171f0fa154SIlpo Järvinen static void xs_write_space(struct sock *sk)
15181f0fa154SIlpo Järvinen {
15194f8943f8STrond Myklebust 	struct sock_xprt *transport;
15201f0fa154SIlpo Järvinen 	struct rpc_xprt *xprt;
15211f0fa154SIlpo Järvinen 
152213331a55STrond Myklebust 	if (!sk->sk_socket)
15231f0fa154SIlpo Järvinen 		return;
152413331a55STrond Myklebust 	clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
15251f0fa154SIlpo Järvinen 
15261f0fa154SIlpo Järvinen 	if (unlikely(!(xprt = xprt_from_sock(sk))))
15271f0fa154SIlpo Järvinen 		return;
15284f8943f8STrond Myklebust 	transport = container_of(xprt, struct sock_xprt, xprt);
15292790a624STrond Myklebust 	if (!test_and_clear_bit(XPRT_SOCK_NOSPACE, &transport->sock_state))
15302790a624STrond Myklebust 		return;
15314f8943f8STrond Myklebust 	xs_run_error_worker(transport, XPRT_SOCK_WAKE_WRITE);
1532c544577dSTrond Myklebust 	sk->sk_write_pending--;
15331f0fa154SIlpo Järvinen }
15341f0fa154SIlpo Järvinen 
15352a9e1cfaSTrond Myklebust /**
1536c7b2cae8SChuck Lever  * xs_udp_write_space - callback invoked when socket buffer space
1537c7b2cae8SChuck Lever  *                             becomes available
15389903cd1cSChuck Lever  * @sk: socket whose state has changed
15399903cd1cSChuck Lever  *
1540a246b010SChuck Lever  * Called when more output buffer space is available for this socket.
1541a246b010SChuck Lever  * We try not to wake our writers until they can make "significant"
1542c7b2cae8SChuck Lever  * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1543a246b010SChuck Lever  * with a bunch of small requests.
1544a246b010SChuck Lever  */
1545c7b2cae8SChuck Lever static void xs_udp_write_space(struct sock *sk)
1546a246b010SChuck Lever {
1547c7b2cae8SChuck Lever 	/* from net/core/sock.c:sock_def_write_space */
15481f0fa154SIlpo Järvinen 	if (sock_writeable(sk))
15491f0fa154SIlpo Järvinen 		xs_write_space(sk);
1550c7b2cae8SChuck Lever }
1551c7b2cae8SChuck Lever 
1552c7b2cae8SChuck Lever /**
1553c7b2cae8SChuck Lever  * xs_tcp_write_space - callback invoked when socket buffer space
1554c7b2cae8SChuck Lever  *                             becomes available
1555c7b2cae8SChuck Lever  * @sk: socket whose state has changed
1556c7b2cae8SChuck Lever  *
1557c7b2cae8SChuck Lever  * Called when more output buffer space is available for this socket.
1558c7b2cae8SChuck Lever  * We try not to wake our writers until they can make "significant"
1559c7b2cae8SChuck Lever  * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1560c7b2cae8SChuck Lever  * with a bunch of small requests.
1561c7b2cae8SChuck Lever  */
1562c7b2cae8SChuck Lever static void xs_tcp_write_space(struct sock *sk)
1563c7b2cae8SChuck Lever {
1564c7b2cae8SChuck Lever 	/* from net/core/stream.c:sk_stream_write_space */
156564dc6130SEric Dumazet 	if (sk_stream_is_writeable(sk))
15661f0fa154SIlpo Järvinen 		xs_write_space(sk);
1567a246b010SChuck Lever }
1568a246b010SChuck Lever 
1569470056c2SChuck Lever static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt)
1570a246b010SChuck Lever {
1571ee0ac0c2SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1572ee0ac0c2SChuck Lever 	struct sock *sk = transport->inet;
1573a246b010SChuck Lever 
15747c6e066eSChuck Lever 	if (transport->rcvsize) {
1575a246b010SChuck Lever 		sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
15767c6e066eSChuck Lever 		sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2;
1577a246b010SChuck Lever 	}
15787c6e066eSChuck Lever 	if (transport->sndsize) {
1579a246b010SChuck Lever 		sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
15807c6e066eSChuck Lever 		sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2;
1581a246b010SChuck Lever 		sk->sk_write_space(sk);
1582a246b010SChuck Lever 	}
1583a246b010SChuck Lever }
1584a246b010SChuck Lever 
158543118c29SChuck Lever /**
1586470056c2SChuck Lever  * xs_udp_set_buffer_size - set send and receive limits
158743118c29SChuck Lever  * @xprt: generic transport
1588470056c2SChuck Lever  * @sndsize: requested size of send buffer, in bytes
1589470056c2SChuck Lever  * @rcvsize: requested size of receive buffer, in bytes
159043118c29SChuck Lever  *
1591470056c2SChuck Lever  * Set socket send and receive buffer size limits.
159243118c29SChuck Lever  */
1593470056c2SChuck Lever static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize)
159443118c29SChuck Lever {
15957c6e066eSChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
15967c6e066eSChuck Lever 
15977c6e066eSChuck Lever 	transport->sndsize = 0;
1598470056c2SChuck Lever 	if (sndsize)
15997c6e066eSChuck Lever 		transport->sndsize = sndsize + 1024;
16007c6e066eSChuck Lever 	transport->rcvsize = 0;
1601470056c2SChuck Lever 	if (rcvsize)
16027c6e066eSChuck Lever 		transport->rcvsize = rcvsize + 1024;
1603470056c2SChuck Lever 
1604470056c2SChuck Lever 	xs_udp_do_set_buffer_size(xprt);
160543118c29SChuck Lever }
160643118c29SChuck Lever 
160746c0ee8bSChuck Lever /**
160846c0ee8bSChuck Lever  * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
1609acf0a39fSChuck Lever  * @xprt: controlling transport
161046c0ee8bSChuck Lever  * @task: task that timed out
161146c0ee8bSChuck Lever  *
161246c0ee8bSChuck Lever  * Adjust the congestion window after a retransmit timeout has occurred.
161346c0ee8bSChuck Lever  */
16146a24dfb6STrond Myklebust static void xs_udp_timer(struct rpc_xprt *xprt, struct rpc_task *task)
161546c0ee8bSChuck Lever {
1616b5e92419STrond Myklebust 	spin_lock(&xprt->transport_lock);
16176a24dfb6STrond Myklebust 	xprt_adjust_cwnd(xprt, task, -ETIMEDOUT);
1618b5e92419STrond Myklebust 	spin_unlock(&xprt->transport_lock);
161946c0ee8bSChuck Lever }
162046c0ee8bSChuck Lever 
1621826799e6SJ. Bruce Fields static int xs_get_random_port(void)
1622b85d8806SChuck Lever {
1623826799e6SJ. Bruce Fields 	unsigned short min = xprt_min_resvport, max = xprt_max_resvport;
1624826799e6SJ. Bruce Fields 	unsigned short range;
1625826799e6SJ. Bruce Fields 	unsigned short rand;
1626826799e6SJ. Bruce Fields 
1627826799e6SJ. Bruce Fields 	if (max < min)
1628826799e6SJ. Bruce Fields 		return -EADDRINUSE;
1629826799e6SJ. Bruce Fields 	range = max - min + 1;
1630826799e6SJ. Bruce Fields 	rand = (unsigned short) prandom_u32() % range;
1631826799e6SJ. Bruce Fields 	return rand + min;
1632b85d8806SChuck Lever }
1633b85d8806SChuck Lever 
16344dda9c8aSTrond Myklebust static unsigned short xs_sock_getport(struct socket *sock)
16354dda9c8aSTrond Myklebust {
16364dda9c8aSTrond Myklebust 	struct sockaddr_storage buf;
16374dda9c8aSTrond Myklebust 	unsigned short port = 0;
16384dda9c8aSTrond Myklebust 
16399b2c45d4SDenys Vlasenko 	if (kernel_getsockname(sock, (struct sockaddr *)&buf) < 0)
16404dda9c8aSTrond Myklebust 		goto out;
16414dda9c8aSTrond Myklebust 	switch (buf.ss_family) {
16424dda9c8aSTrond Myklebust 	case AF_INET6:
16434dda9c8aSTrond Myklebust 		port = ntohs(((struct sockaddr_in6 *)&buf)->sin6_port);
16444dda9c8aSTrond Myklebust 		break;
16454dda9c8aSTrond Myklebust 	case AF_INET:
16464dda9c8aSTrond Myklebust 		port = ntohs(((struct sockaddr_in *)&buf)->sin_port);
16474dda9c8aSTrond Myklebust 	}
16484dda9c8aSTrond Myklebust out:
16494dda9c8aSTrond Myklebust 	return port;
16504dda9c8aSTrond Myklebust }
16514dda9c8aSTrond Myklebust 
16524dda9c8aSTrond Myklebust /**
165392200412SChuck Lever  * xs_set_port - reset the port number in the remote endpoint address
165492200412SChuck Lever  * @xprt: generic transport
165592200412SChuck Lever  * @port: new port number
165692200412SChuck Lever  *
165792200412SChuck Lever  */
165892200412SChuck Lever static void xs_set_port(struct rpc_xprt *xprt, unsigned short port)
165992200412SChuck Lever {
166092200412SChuck Lever 	dprintk("RPC:       setting port for xprt %p to %u\n", xprt, port);
1661c4efcb1dSChuck Lever 
16629dc3b095SChuck Lever 	rpc_set_port(xs_addr(xprt), port);
16639dc3b095SChuck Lever 	xs_update_peer_port(xprt);
166492200412SChuck Lever }
166592200412SChuck Lever 
16664dda9c8aSTrond Myklebust static void xs_set_srcport(struct sock_xprt *transport, struct socket *sock)
16674dda9c8aSTrond Myklebust {
1668e6237b6fSTrond Myklebust 	if (transport->srcport == 0 && transport->xprt.reuseport)
16694dda9c8aSTrond Myklebust 		transport->srcport = xs_sock_getport(sock);
16704dda9c8aSTrond Myklebust }
16714dda9c8aSTrond Myklebust 
1672826799e6SJ. Bruce Fields static int xs_get_srcport(struct sock_xprt *transport)
167367a391d7STrond Myklebust {
1674826799e6SJ. Bruce Fields 	int port = transport->srcport;
167567a391d7STrond Myklebust 
167667a391d7STrond Myklebust 	if (port == 0 && transport->xprt.resvport)
167767a391d7STrond Myklebust 		port = xs_get_random_port();
167867a391d7STrond Myklebust 	return port;
167967a391d7STrond Myklebust }
168067a391d7STrond Myklebust 
1681421ab1beSTrond Myklebust static unsigned short xs_sock_srcport(struct rpc_xprt *xprt)
1682a8482488SOlga Kornievskaia {
1683a8482488SOlga Kornievskaia 	struct sock_xprt *sock = container_of(xprt, struct sock_xprt, xprt);
1684b49ea673SNeilBrown 	unsigned short ret = 0;
1685b49ea673SNeilBrown 	mutex_lock(&sock->recv_mutex);
1686b49ea673SNeilBrown 	if (sock->sock)
1687b49ea673SNeilBrown 		ret = xs_sock_getport(sock->sock);
1688b49ea673SNeilBrown 	mutex_unlock(&sock->recv_mutex);
1689b49ea673SNeilBrown 	return ret;
1690a8482488SOlga Kornievskaia }
1691421ab1beSTrond Myklebust 
1692421ab1beSTrond Myklebust static int xs_sock_srcaddr(struct rpc_xprt *xprt, char *buf, size_t buflen)
1693421ab1beSTrond Myklebust {
1694421ab1beSTrond Myklebust 	struct sock_xprt *sock = container_of(xprt, struct sock_xprt, xprt);
1695421ab1beSTrond Myklebust 	union {
1696421ab1beSTrond Myklebust 		struct sockaddr sa;
1697421ab1beSTrond Myklebust 		struct sockaddr_storage st;
1698421ab1beSTrond Myklebust 	} saddr;
1699421ab1beSTrond Myklebust 	int ret = -ENOTCONN;
1700421ab1beSTrond Myklebust 
1701421ab1beSTrond Myklebust 	mutex_lock(&sock->recv_mutex);
1702421ab1beSTrond Myklebust 	if (sock->sock) {
1703421ab1beSTrond Myklebust 		ret = kernel_getsockname(sock->sock, &saddr.sa);
1704421ab1beSTrond Myklebust 		if (ret >= 0)
1705421ab1beSTrond Myklebust 			ret = snprintf(buf, buflen, "%pISc", &saddr.sa);
1706421ab1beSTrond Myklebust 	}
1707421ab1beSTrond Myklebust 	mutex_unlock(&sock->recv_mutex);
1708421ab1beSTrond Myklebust 	return ret;
1709421ab1beSTrond Myklebust }
1710a8482488SOlga Kornievskaia 
1711baaf4e48SPavel Emelyanov static unsigned short xs_next_srcport(struct sock_xprt *transport, unsigned short port)
171267a391d7STrond Myklebust {
1713fbfffbd5SChuck Lever 	if (transport->srcport != 0)
1714fbfffbd5SChuck Lever 		transport->srcport = 0;
171567a391d7STrond Myklebust 	if (!transport->xprt.resvport)
171667a391d7STrond Myklebust 		return 0;
171767a391d7STrond Myklebust 	if (port <= xprt_min_resvport || port > xprt_max_resvport)
171867a391d7STrond Myklebust 		return xprt_max_resvport;
171967a391d7STrond Myklebust 	return --port;
172067a391d7STrond Myklebust }
1721beb59b68SPavel Emelyanov static int xs_bind(struct sock_xprt *transport, struct socket *sock)
1722a246b010SChuck Lever {
1723beb59b68SPavel Emelyanov 	struct sockaddr_storage myaddr;
172467a391d7STrond Myklebust 	int err, nloop = 0;
1725826799e6SJ. Bruce Fields 	int port = xs_get_srcport(transport);
172667a391d7STrond Myklebust 	unsigned short last;
1727a246b010SChuck Lever 
17280f7a622cSChris Perl 	/*
17290f7a622cSChris Perl 	 * If we are asking for any ephemeral port (i.e. port == 0 &&
17300f7a622cSChris Perl 	 * transport->xprt.resvport == 0), don't bind.  Let the local
17310f7a622cSChris Perl 	 * port selection happen implicitly when the socket is used
17320f7a622cSChris Perl 	 * (for example at connect time).
17330f7a622cSChris Perl 	 *
17340f7a622cSChris Perl 	 * This ensures that we can continue to establish TCP
17350f7a622cSChris Perl 	 * connections even when all local ephemeral ports are already
17360f7a622cSChris Perl 	 * a part of some TCP connection.  This makes no difference
173712b20ce3SBhaskar Chowdhury 	 * for UDP sockets, but also doesn't harm them.
17380f7a622cSChris Perl 	 *
17390f7a622cSChris Perl 	 * If we're asking for any reserved port (i.e. port == 0 &&
17400f7a622cSChris Perl 	 * transport->xprt.resvport == 1) xs_get_srcport above will
17410f7a622cSChris Perl 	 * ensure that port is non-zero and we will bind as needed.
17420f7a622cSChris Perl 	 */
1743826799e6SJ. Bruce Fields 	if (port <= 0)
1744826799e6SJ. Bruce Fields 		return port;
17450f7a622cSChris Perl 
1746beb59b68SPavel Emelyanov 	memcpy(&myaddr, &transport->srcaddr, transport->xprt.addrlen);
1747a246b010SChuck Lever 	do {
1748beb59b68SPavel Emelyanov 		rpc_set_port((struct sockaddr *)&myaddr, port);
1749e6242e92SSridhar Samudrala 		err = kernel_bind(sock, (struct sockaddr *)&myaddr,
1750beb59b68SPavel Emelyanov 				transport->xprt.addrlen);
1751a246b010SChuck Lever 		if (err == 0) {
1752bc1c56e9SNeilBrown 			if (transport->xprt.reuseport)
1753fbfffbd5SChuck Lever 				transport->srcport = port;
1754d3bc9a1dSFrank van Maarseveen 			break;
1755a246b010SChuck Lever 		}
175667a391d7STrond Myklebust 		last = port;
1757baaf4e48SPavel Emelyanov 		port = xs_next_srcport(transport, port);
175867a391d7STrond Myklebust 		if (port > last)
175967a391d7STrond Myklebust 			nloop++;
176067a391d7STrond Myklebust 	} while (err == -EADDRINUSE && nloop != 2);
1761beb59b68SPavel Emelyanov 
17624232e863SChuck Lever 	if (myaddr.ss_family == AF_INET)
1763beb59b68SPavel Emelyanov 		dprintk("RPC:       %s %pI4:%u: %s (%d)\n", __func__,
1764beb59b68SPavel Emelyanov 				&((struct sockaddr_in *)&myaddr)->sin_addr,
1765beb59b68SPavel Emelyanov 				port, err ? "failed" : "ok", err);
1766beb59b68SPavel Emelyanov 	else
1767beb59b68SPavel Emelyanov 		dprintk("RPC:       %s %pI6:%u: %s (%d)\n", __func__,
1768beb59b68SPavel Emelyanov 				&((struct sockaddr_in6 *)&myaddr)->sin6_addr,
17697dc753f0SChuck Lever 				port, err ? "failed" : "ok", err);
1770a246b010SChuck Lever 	return err;
1771a246b010SChuck Lever }
1772a246b010SChuck Lever 
1773176e21eeSChuck Lever /*
1774176e21eeSChuck Lever  * We don't support autobind on AF_LOCAL sockets
1775176e21eeSChuck Lever  */
1776176e21eeSChuck Lever static void xs_local_rpcbind(struct rpc_task *task)
1777176e21eeSChuck Lever {
1778fb43d172STrond Myklebust 	xprt_set_bound(task->tk_xprt);
1779176e21eeSChuck Lever }
1780176e21eeSChuck Lever 
1781176e21eeSChuck Lever static void xs_local_set_port(struct rpc_xprt *xprt, unsigned short port)
1782176e21eeSChuck Lever {
1783176e21eeSChuck Lever }
1784a246b010SChuck Lever 
1785ed07536eSPeter Zijlstra #ifdef CONFIG_DEBUG_LOCK_ALLOC
1786064a9177SNeilBrown static struct lock_class_key xs_key[3];
1787064a9177SNeilBrown static struct lock_class_key xs_slock_key[3];
1788ed07536eSPeter Zijlstra 
1789176e21eeSChuck Lever static inline void xs_reclassify_socketu(struct socket *sock)
1790176e21eeSChuck Lever {
1791176e21eeSChuck Lever 	struct sock *sk = sock->sk;
1792176e21eeSChuck Lever 
1793176e21eeSChuck Lever 	sock_lock_init_class_and_name(sk, "slock-AF_LOCAL-RPC",
1794064a9177SNeilBrown 		&xs_slock_key[0], "sk_lock-AF_LOCAL-RPC", &xs_key[0]);
1795176e21eeSChuck Lever }
1796176e21eeSChuck Lever 
17978945ee5eSChuck Lever static inline void xs_reclassify_socket4(struct socket *sock)
1798ed07536eSPeter Zijlstra {
1799ed07536eSPeter Zijlstra 	struct sock *sk = sock->sk;
18008945ee5eSChuck Lever 
18018945ee5eSChuck Lever 	sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC",
1802064a9177SNeilBrown 		&xs_slock_key[1], "sk_lock-AF_INET-RPC", &xs_key[1]);
1803ed07536eSPeter Zijlstra }
18048945ee5eSChuck Lever 
18058945ee5eSChuck Lever static inline void xs_reclassify_socket6(struct socket *sock)
18068945ee5eSChuck Lever {
18078945ee5eSChuck Lever 	struct sock *sk = sock->sk;
18088945ee5eSChuck Lever 
18098945ee5eSChuck Lever 	sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC",
1810064a9177SNeilBrown 		&xs_slock_key[2], "sk_lock-AF_INET6-RPC", &xs_key[2]);
1811ed07536eSPeter Zijlstra }
18126bc9638aSPavel Emelyanov 
18136bc9638aSPavel Emelyanov static inline void xs_reclassify_socket(int family, struct socket *sock)
18146bc9638aSPavel Emelyanov {
1815fafc4e1eSHannes Frederic Sowa 	if (WARN_ON_ONCE(!sock_allow_reclassification(sock->sk)))
18161b7a1819SWeston Andros Adamson 		return;
18171b7a1819SWeston Andros Adamson 
18184232e863SChuck Lever 	switch (family) {
1819176e21eeSChuck Lever 	case AF_LOCAL:
1820176e21eeSChuck Lever 		xs_reclassify_socketu(sock);
1821176e21eeSChuck Lever 		break;
18224232e863SChuck Lever 	case AF_INET:
18236bc9638aSPavel Emelyanov 		xs_reclassify_socket4(sock);
18244232e863SChuck Lever 		break;
18254232e863SChuck Lever 	case AF_INET6:
18266bc9638aSPavel Emelyanov 		xs_reclassify_socket6(sock);
18274232e863SChuck Lever 		break;
18284232e863SChuck Lever 	}
18296bc9638aSPavel Emelyanov }
1830ed07536eSPeter Zijlstra #else
18316bc9638aSPavel Emelyanov static inline void xs_reclassify_socket(int family, struct socket *sock)
18326bc9638aSPavel Emelyanov {
18336bc9638aSPavel Emelyanov }
1834ed07536eSPeter Zijlstra #endif
1835ed07536eSPeter Zijlstra 
183693dc41bdSNeilBrown static void xs_dummy_setup_socket(struct work_struct *work)
183793dc41bdSNeilBrown {
183893dc41bdSNeilBrown }
183993dc41bdSNeilBrown 
18406bc9638aSPavel Emelyanov static struct socket *xs_create_sock(struct rpc_xprt *xprt,
18414dda9c8aSTrond Myklebust 		struct sock_xprt *transport, int family, int type,
18424dda9c8aSTrond Myklebust 		int protocol, bool reuseport)
184322f79326SPavel Emelyanov {
1844a73881c9STrond Myklebust 	struct file *filp;
184522f79326SPavel Emelyanov 	struct socket *sock;
184622f79326SPavel Emelyanov 	int err;
184722f79326SPavel Emelyanov 
18486bc9638aSPavel Emelyanov 	err = __sock_create(xprt->xprt_net, family, type, protocol, &sock, 1);
184922f79326SPavel Emelyanov 	if (err < 0) {
185022f79326SPavel Emelyanov 		dprintk("RPC:       can't create %d transport socket (%d).\n",
185122f79326SPavel Emelyanov 				protocol, -err);
185222f79326SPavel Emelyanov 		goto out;
185322f79326SPavel Emelyanov 	}
18546bc9638aSPavel Emelyanov 	xs_reclassify_socket(family, sock);
185522f79326SPavel Emelyanov 
18564dda9c8aSTrond Myklebust 	if (reuseport)
1857fe31a326SChristoph Hellwig 		sock_set_reuseport(sock->sk);
18584dda9c8aSTrond Myklebust 
18594cea288aSBen Hutchings 	err = xs_bind(transport, sock);
18604cea288aSBen Hutchings 	if (err) {
186122f79326SPavel Emelyanov 		sock_release(sock);
186222f79326SPavel Emelyanov 		goto out;
186322f79326SPavel Emelyanov 	}
186422f79326SPavel Emelyanov 
1865a73881c9STrond Myklebust 	filp = sock_alloc_file(sock, O_NONBLOCK, NULL);
1866a73881c9STrond Myklebust 	if (IS_ERR(filp))
1867a73881c9STrond Myklebust 		return ERR_CAST(filp);
1868a73881c9STrond Myklebust 	transport->file = filp;
1869a73881c9STrond Myklebust 
187022f79326SPavel Emelyanov 	return sock;
187122f79326SPavel Emelyanov out:
187222f79326SPavel Emelyanov 	return ERR_PTR(err);
187322f79326SPavel Emelyanov }
187422f79326SPavel Emelyanov 
1875176e21eeSChuck Lever static int xs_local_finish_connecting(struct rpc_xprt *xprt,
1876176e21eeSChuck Lever 				      struct socket *sock)
1877176e21eeSChuck Lever {
1878176e21eeSChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
1879176e21eeSChuck Lever 									xprt);
1880176e21eeSChuck Lever 
1881176e21eeSChuck Lever 	if (!transport->inet) {
1882176e21eeSChuck Lever 		struct sock *sk = sock->sk;
1883176e21eeSChuck Lever 
1884ea9afca8STrond Myklebust 		lock_sock(sk);
1885176e21eeSChuck Lever 
1886176e21eeSChuck Lever 		xs_save_old_callbacks(transport, sk);
1887176e21eeSChuck Lever 
1888176e21eeSChuck Lever 		sk->sk_user_data = xprt;
1889a2648094STrond Myklebust 		sk->sk_data_ready = xs_data_ready;
1890176e21eeSChuck Lever 		sk->sk_write_space = xs_udp_write_space;
1891efce2d0bSTrond Myklebust 		sk->sk_state_change = xs_local_state_change;
18922118071dSTrond Myklebust 		sk->sk_error_report = xs_error_report;
1893176e21eeSChuck Lever 
1894176e21eeSChuck Lever 		xprt_clear_connected(xprt);
1895176e21eeSChuck Lever 
1896176e21eeSChuck Lever 		/* Reset to new socket */
1897176e21eeSChuck Lever 		transport->sock = sock;
1898176e21eeSChuck Lever 		transport->inet = sk;
1899176e21eeSChuck Lever 
1900ea9afca8STrond Myklebust 		release_sock(sk);
1901176e21eeSChuck Lever 	}
1902176e21eeSChuck Lever 
1903ae053551STrond Myklebust 	xs_stream_start_connect(transport);
19046c7a64e5STrond Myklebust 
1905176e21eeSChuck Lever 	return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, 0);
1906176e21eeSChuck Lever }
1907176e21eeSChuck Lever 
1908176e21eeSChuck Lever /**
1909176e21eeSChuck Lever  * xs_local_setup_socket - create AF_LOCAL socket, connect to a local endpoint
1910176e21eeSChuck Lever  * @transport: socket transport to connect
1911176e21eeSChuck Lever  */
1912dc107402SJ. Bruce Fields static int xs_local_setup_socket(struct sock_xprt *transport)
1913176e21eeSChuck Lever {
1914176e21eeSChuck Lever 	struct rpc_xprt *xprt = &transport->xprt;
1915a73881c9STrond Myklebust 	struct file *filp;
1916176e21eeSChuck Lever 	struct socket *sock;
191768e9a246SColin Ian King 	int status;
1918176e21eeSChuck Lever 
1919176e21eeSChuck Lever 	status = __sock_create(xprt->xprt_net, AF_LOCAL,
1920176e21eeSChuck Lever 					SOCK_STREAM, 0, &sock, 1);
1921176e21eeSChuck Lever 	if (status < 0) {
1922176e21eeSChuck Lever 		dprintk("RPC:       can't create AF_LOCAL "
1923176e21eeSChuck Lever 			"transport socket (%d).\n", -status);
1924176e21eeSChuck Lever 		goto out;
1925176e21eeSChuck Lever 	}
1926d1358917SStefan Hajnoczi 	xs_reclassify_socket(AF_LOCAL, sock);
1927176e21eeSChuck Lever 
1928a73881c9STrond Myklebust 	filp = sock_alloc_file(sock, O_NONBLOCK, NULL);
1929a73881c9STrond Myklebust 	if (IS_ERR(filp)) {
1930a73881c9STrond Myklebust 		status = PTR_ERR(filp);
1931a73881c9STrond Myklebust 		goto out;
1932a73881c9STrond Myklebust 	}
1933a73881c9STrond Myklebust 	transport->file = filp;
1934a73881c9STrond Myklebust 
1935176e21eeSChuck Lever 	dprintk("RPC:       worker connecting xprt %p via AF_LOCAL to %s\n",
1936176e21eeSChuck Lever 			xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1937176e21eeSChuck Lever 
1938176e21eeSChuck Lever 	status = xs_local_finish_connecting(xprt, sock);
193940b5ea0cSTrond Myklebust 	trace_rpc_socket_connect(xprt, sock, status);
1940176e21eeSChuck Lever 	switch (status) {
1941176e21eeSChuck Lever 	case 0:
1942176e21eeSChuck Lever 		dprintk("RPC:       xprt %p connected to %s\n",
1943176e21eeSChuck Lever 				xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
19443968a8a5SChuck Lever 		xprt->stat.connect_count++;
19453968a8a5SChuck Lever 		xprt->stat.connect_time += (long)jiffies -
19463968a8a5SChuck Lever 					   xprt->stat.connect_start;
1947176e21eeSChuck Lever 		xprt_set_connected(xprt);
194893f479d3SGustavo A. R. Silva 		break;
19493601c4a9STrond Myklebust 	case -ENOBUFS:
1950176e21eeSChuck Lever 		break;
1951176e21eeSChuck Lever 	case -ENOENT:
1952176e21eeSChuck Lever 		dprintk("RPC:       xprt %p: socket %s does not exist\n",
1953176e21eeSChuck Lever 				xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1954176e21eeSChuck Lever 		break;
19554a20a988STrond Myklebust 	case -ECONNREFUSED:
19564a20a988STrond Myklebust 		dprintk("RPC:       xprt %p: connection refused for %s\n",
19574a20a988STrond Myklebust 				xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
19584a20a988STrond Myklebust 		break;
1959176e21eeSChuck Lever 	default:
1960176e21eeSChuck Lever 		printk(KERN_ERR "%s: unhandled error (%d) connecting to %s\n",
1961176e21eeSChuck Lever 				__func__, -status,
1962176e21eeSChuck Lever 				xprt->address_strings[RPC_DISPLAY_ADDR]);
1963176e21eeSChuck Lever 	}
1964176e21eeSChuck Lever 
1965176e21eeSChuck Lever out:
1966176e21eeSChuck Lever 	xprt_clear_connecting(xprt);
1967176e21eeSChuck Lever 	xprt_wake_pending_tasks(xprt, status);
1968dc107402SJ. Bruce Fields 	return status;
1969dc107402SJ. Bruce Fields }
1970dc107402SJ. Bruce Fields 
1971b6669737SLinus Torvalds static void xs_local_connect(struct rpc_xprt *xprt, struct rpc_task *task)
1972dc107402SJ. Bruce Fields {
1973dc107402SJ. Bruce Fields 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1974dc107402SJ. Bruce Fields 	int ret;
1975dc107402SJ. Bruce Fields 
1976aad41a7dSTrond Myklebust 	if (transport->file)
1977aad41a7dSTrond Myklebust 		goto force_disconnect;
1978aad41a7dSTrond Myklebust 
1979dc107402SJ. Bruce Fields 	if (RPC_IS_ASYNC(task)) {
1980dc107402SJ. Bruce Fields 		/*
1981dc107402SJ. Bruce Fields 		 * We want the AF_LOCAL connect to be resolved in the
1982dc107402SJ. Bruce Fields 		 * filesystem namespace of the process making the rpc
1983dc107402SJ. Bruce Fields 		 * call.  Thus we connect synchronously.
1984dc107402SJ. Bruce Fields 		 *
1985dc107402SJ. Bruce Fields 		 * If we want to support asynchronous AF_LOCAL calls,
1986dc107402SJ. Bruce Fields 		 * we'll need to figure out how to pass a namespace to
1987dc107402SJ. Bruce Fields 		 * connect.
1988dc107402SJ. Bruce Fields 		 */
19895ad64b36STrond Myklebust 		task->tk_rpc_status = -ENOTCONN;
1990dc107402SJ. Bruce Fields 		rpc_exit(task, -ENOTCONN);
1991aad41a7dSTrond Myklebust 		goto out_wake;
1992dc107402SJ. Bruce Fields 	}
1993dc107402SJ. Bruce Fields 	ret = xs_local_setup_socket(transport);
1994dc107402SJ. Bruce Fields 	if (ret && !RPC_IS_SOFTCONN(task))
1995dc107402SJ. Bruce Fields 		msleep_interruptible(15000);
1996aad41a7dSTrond Myklebust 	return;
1997aad41a7dSTrond Myklebust force_disconnect:
1998aad41a7dSTrond Myklebust 	xprt_force_disconnect(xprt);
1999aad41a7dSTrond Myklebust out_wake:
2000aad41a7dSTrond Myklebust 	xprt_clear_connecting(xprt);
2001aad41a7dSTrond Myklebust 	xprt_wake_pending_tasks(xprt, -ENOTCONN);
2002176e21eeSChuck Lever }
2003176e21eeSChuck Lever 
20043c87ef6eSJeff Layton #if IS_ENABLED(CONFIG_SUNRPC_SWAP)
2005d6e971d8SJeff Layton /*
2006693486d5SNeilBrown  * Note that this should be called with XPRT_LOCKED held, or recv_mutex
2007693486d5SNeilBrown  * held, or when we otherwise know that we have exclusive access to the
2008693486d5SNeilBrown  * socket, to guard against races with xs_reset_transport.
2009d6e971d8SJeff Layton  */
2010a564b8f0SMel Gorman static void xs_set_memalloc(struct rpc_xprt *xprt)
2011a564b8f0SMel Gorman {
2012a564b8f0SMel Gorman 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
2013a564b8f0SMel Gorman 			xprt);
2014a564b8f0SMel Gorman 
2015d6e971d8SJeff Layton 	/*
2016d6e971d8SJeff Layton 	 * If there's no sock, then we have nothing to set. The
2017d6e971d8SJeff Layton 	 * reconnecting process will get it for us.
2018d6e971d8SJeff Layton 	 */
2019d6e971d8SJeff Layton 	if (!transport->inet)
2020d6e971d8SJeff Layton 		return;
20218e228133SJeff Layton 	if (atomic_read(&xprt->swapper))
2022a564b8f0SMel Gorman 		sk_set_memalloc(transport->inet);
2023a564b8f0SMel Gorman }
2024a564b8f0SMel Gorman 
2025a564b8f0SMel Gorman /**
2026d67fa4d8SJeff Layton  * xs_enable_swap - Tag this transport as being used for swap.
2027a564b8f0SMel Gorman  * @xprt: transport to tag
2028a564b8f0SMel Gorman  *
20298e228133SJeff Layton  * Take a reference to this transport on behalf of the rpc_clnt, and
20308e228133SJeff Layton  * optionally mark it for swapping if it wasn't already.
2031a564b8f0SMel Gorman  */
2032d67fa4d8SJeff Layton static int
2033d67fa4d8SJeff Layton xs_enable_swap(struct rpc_xprt *xprt)
2034a564b8f0SMel Gorman {
2035d6e971d8SJeff Layton 	struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt);
2036a564b8f0SMel Gorman 
2037693486d5SNeilBrown 	mutex_lock(&xs->recv_mutex);
2038693486d5SNeilBrown 	if (atomic_inc_return(&xprt->swapper) == 1 &&
2039693486d5SNeilBrown 	    xs->inet)
2040d6e971d8SJeff Layton 		sk_set_memalloc(xs->inet);
2041693486d5SNeilBrown 	mutex_unlock(&xs->recv_mutex);
20428e228133SJeff Layton 	return 0;
2043a564b8f0SMel Gorman }
2044a564b8f0SMel Gorman 
20458e228133SJeff Layton /**
2046d67fa4d8SJeff Layton  * xs_disable_swap - Untag this transport as being used for swap.
20478e228133SJeff Layton  * @xprt: transport to tag
20488e228133SJeff Layton  *
20498e228133SJeff Layton  * Drop a "swapper" reference to this xprt on behalf of the rpc_clnt. If the
20508e228133SJeff Layton  * swapper refcount goes to 0, untag the socket as a memalloc socket.
20518e228133SJeff Layton  */
2052d67fa4d8SJeff Layton static void
2053d67fa4d8SJeff Layton xs_disable_swap(struct rpc_xprt *xprt)
20548e228133SJeff Layton {
2055d6e971d8SJeff Layton 	struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt);
20568e228133SJeff Layton 
2057693486d5SNeilBrown 	mutex_lock(&xs->recv_mutex);
2058693486d5SNeilBrown 	if (atomic_dec_and_test(&xprt->swapper) &&
2059693486d5SNeilBrown 	    xs->inet)
2060d6e971d8SJeff Layton 		sk_clear_memalloc(xs->inet);
2061693486d5SNeilBrown 	mutex_unlock(&xs->recv_mutex);
2062a564b8f0SMel Gorman }
2063a564b8f0SMel Gorman #else
2064a564b8f0SMel Gorman static void xs_set_memalloc(struct rpc_xprt *xprt)
2065a564b8f0SMel Gorman {
2066a564b8f0SMel Gorman }
2067d67fa4d8SJeff Layton 
2068d67fa4d8SJeff Layton static int
2069d67fa4d8SJeff Layton xs_enable_swap(struct rpc_xprt *xprt)
2070d67fa4d8SJeff Layton {
2071d67fa4d8SJeff Layton 	return -EINVAL;
2072d67fa4d8SJeff Layton }
2073d67fa4d8SJeff Layton 
2074d67fa4d8SJeff Layton static void
2075d67fa4d8SJeff Layton xs_disable_swap(struct rpc_xprt *xprt)
2076d67fa4d8SJeff Layton {
2077d67fa4d8SJeff Layton }
2078a564b8f0SMel Gorman #endif
2079a564b8f0SMel Gorman 
208016be2d20SChuck Lever static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
2081a246b010SChuck Lever {
208216be2d20SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2083edb267a6SChuck Lever 
2084ee0ac0c2SChuck Lever 	if (!transport->inet) {
2085b0d93ad5SChuck Lever 		struct sock *sk = sock->sk;
2086b0d93ad5SChuck Lever 
2087ea9afca8STrond Myklebust 		lock_sock(sk);
2088b0d93ad5SChuck Lever 
20892a9e1cfaSTrond Myklebust 		xs_save_old_callbacks(transport, sk);
20902a9e1cfaSTrond Myklebust 
2091b0d93ad5SChuck Lever 		sk->sk_user_data = xprt;
2092f9b2ee71STrond Myklebust 		sk->sk_data_ready = xs_data_ready;
2093b0d93ad5SChuck Lever 		sk->sk_write_space = xs_udp_write_space;
2094b0d93ad5SChuck Lever 
2095b0d93ad5SChuck Lever 		xprt_set_connected(xprt);
2096b0d93ad5SChuck Lever 
2097b0d93ad5SChuck Lever 		/* Reset to new socket */
2098ee0ac0c2SChuck Lever 		transport->sock = sock;
2099ee0ac0c2SChuck Lever 		transport->inet = sk;
2100b0d93ad5SChuck Lever 
2101a564b8f0SMel Gorman 		xs_set_memalloc(xprt);
2102a564b8f0SMel Gorman 
2103ea9afca8STrond Myklebust 		release_sock(sk);
2104b0d93ad5SChuck Lever 	}
2105470056c2SChuck Lever 	xs_udp_do_set_buffer_size(xprt);
210602910177STrond Myklebust 
210702910177STrond Myklebust 	xprt->stat.connect_start = jiffies;
210816be2d20SChuck Lever }
210916be2d20SChuck Lever 
21108c14ff2aSPavel Emelyanov static void xs_udp_setup_socket(struct work_struct *work)
2111a246b010SChuck Lever {
2112a246b010SChuck Lever 	struct sock_xprt *transport =
2113a246b010SChuck Lever 		container_of(work, struct sock_xprt, connect_worker.work);
2114a246b010SChuck Lever 	struct rpc_xprt *xprt = &transport->xprt;
2115d099b8afSColin Ian King 	struct socket *sock;
2116b65c0310SPavel Emelyanov 	int status = -EIO;
21178db55a03SNeilBrown 	unsigned int pflags = current->flags;
2118a246b010SChuck Lever 
21198db55a03SNeilBrown 	if (atomic_read(&xprt->swapper))
21208db55a03SNeilBrown 		current->flags |= PF_MEMALLOC;
21218c14ff2aSPavel Emelyanov 	sock = xs_create_sock(xprt, transport,
21224dda9c8aSTrond Myklebust 			xs_addr(xprt)->sa_family, SOCK_DGRAM,
21234dda9c8aSTrond Myklebust 			IPPROTO_UDP, false);
2124b65c0310SPavel Emelyanov 	if (IS_ERR(sock))
2125a246b010SChuck Lever 		goto out;
212668e220bdSChuck Lever 
2127c740eff8SChuck Lever 	dprintk("RPC:       worker connecting xprt %p via %s to "
2128c740eff8SChuck Lever 				"%s (port %s)\n", xprt,
2129c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_PROTO],
2130c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_ADDR],
2131c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_PORT]);
213268e220bdSChuck Lever 
213368e220bdSChuck Lever 	xs_udp_finish_connecting(xprt, sock);
213440b5ea0cSTrond Myklebust 	trace_rpc_socket_connect(xprt, sock, 0);
2135a246b010SChuck Lever 	status = 0;
2136b0d93ad5SChuck Lever out:
2137b0d93ad5SChuck Lever 	xprt_clear_connecting(xprt);
2138cf76785dSTrond Myklebust 	xprt_unlock_connect(xprt, transport);
21397d1e8255STrond Myklebust 	xprt_wake_pending_tasks(xprt, status);
21408db55a03SNeilBrown 	current_restore_flags(pflags, PF_MEMALLOC);
2141b0d93ad5SChuck Lever }
2142b0d93ad5SChuck Lever 
21434876cc77STrond Myklebust /**
21444876cc77STrond Myklebust  * xs_tcp_shutdown - gracefully shut down a TCP socket
21454876cc77STrond Myklebust  * @xprt: transport
21464876cc77STrond Myklebust  *
21474876cc77STrond Myklebust  * Initiates a graceful shutdown of the TCP socket by calling the
21484876cc77STrond Myklebust  * equivalent of shutdown(SHUT_RDWR);
21494876cc77STrond Myklebust  */
21504876cc77STrond Myklebust static void xs_tcp_shutdown(struct rpc_xprt *xprt)
21514876cc77STrond Myklebust {
21524876cc77STrond Myklebust 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
21534876cc77STrond Myklebust 	struct socket *sock = transport->sock;
21549b30889cSTrond Myklebust 	int skst = transport->inet ? transport->inet->sk_state : TCP_CLOSE;
21554876cc77STrond Myklebust 
21564876cc77STrond Myklebust 	if (sock == NULL)
21574876cc77STrond Myklebust 		return;
21580a6ff58eSTrond Myklebust 	if (!xprt->reuseport) {
21590a6ff58eSTrond Myklebust 		xs_close(xprt);
21600a6ff58eSTrond Myklebust 		return;
21610a6ff58eSTrond Myklebust 	}
21629b30889cSTrond Myklebust 	switch (skst) {
21637c81e6a9STrond Myklebust 	case TCP_FIN_WAIT1:
21647c81e6a9STrond Myklebust 	case TCP_FIN_WAIT2:
21657c81e6a9STrond Myklebust 		break;
21667c81e6a9STrond Myklebust 	case TCP_ESTABLISHED:
21677c81e6a9STrond Myklebust 	case TCP_CLOSE_WAIT:
21684876cc77STrond Myklebust 		kernel_sock_shutdown(sock, SHUT_RDWR);
21694876cc77STrond Myklebust 		trace_rpc_socket_shutdown(xprt, sock);
21709b30889cSTrond Myklebust 		break;
21717c81e6a9STrond Myklebust 	default:
21724876cc77STrond Myklebust 		xs_reset_transport(transport);
21734876cc77STrond Myklebust 	}
21749b30889cSTrond Myklebust }
21754876cc77STrond Myklebust 
21768d1b8c62STrond Myklebust static void xs_tcp_set_socket_timeouts(struct rpc_xprt *xprt,
21778d1b8c62STrond Myklebust 		struct socket *sock)
2178b0d93ad5SChuck Lever {
217916be2d20SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
21807196dbb0STrond Myklebust 	unsigned int keepidle;
21817196dbb0STrond Myklebust 	unsigned int keepcnt;
2182775f06abSTrond Myklebust 	unsigned int timeo;
21837f260e85STrond Myklebust 
2184b5e92419STrond Myklebust 	spin_lock(&xprt->transport_lock);
21857196dbb0STrond Myklebust 	keepidle = DIV_ROUND_UP(xprt->timeout->to_initval, HZ);
21867196dbb0STrond Myklebust 	keepcnt = xprt->timeout->to_retries + 1;
21877196dbb0STrond Myklebust 	timeo = jiffies_to_msecs(xprt->timeout->to_initval) *
21887196dbb0STrond Myklebust 		(xprt->timeout->to_retries + 1);
21897196dbb0STrond Myklebust 	clear_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state);
2190b5e92419STrond Myklebust 	spin_unlock(&xprt->transport_lock);
21917f260e85STrond Myklebust 
21927f260e85STrond Myklebust 	/* TCP Keepalive options */
2193ce3d9544SChristoph Hellwig 	sock_set_keepalive(sock->sk);
219471c48eb8SChristoph Hellwig 	tcp_sock_set_keepidle(sock->sk, keepidle);
2195d41ecaacSChristoph Hellwig 	tcp_sock_set_keepintvl(sock->sk, keepidle);
2196480aeb96SChristoph Hellwig 	tcp_sock_set_keepcnt(sock->sk, keepcnt);
2197b0d93ad5SChuck Lever 
21988d1b8c62STrond Myklebust 	/* TCP user timeout (see RFC5482) */
2199c488aeadSChristoph Hellwig 	tcp_sock_set_user_timeout(sock->sk, timeo);
22008d1b8c62STrond Myklebust }
22018d1b8c62STrond Myklebust 
22027196dbb0STrond Myklebust static void xs_tcp_set_connect_timeout(struct rpc_xprt *xprt,
22037196dbb0STrond Myklebust 		unsigned long connect_timeout,
22047196dbb0STrond Myklebust 		unsigned long reconnect_timeout)
22057196dbb0STrond Myklebust {
22067196dbb0STrond Myklebust 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
22077196dbb0STrond Myklebust 	struct rpc_timeout to;
22087196dbb0STrond Myklebust 	unsigned long initval;
22097196dbb0STrond Myklebust 
2210b5e92419STrond Myklebust 	spin_lock(&xprt->transport_lock);
22117196dbb0STrond Myklebust 	if (reconnect_timeout < xprt->max_reconnect_timeout)
22127196dbb0STrond Myklebust 		xprt->max_reconnect_timeout = reconnect_timeout;
22137196dbb0STrond Myklebust 	if (connect_timeout < xprt->connect_timeout) {
22147196dbb0STrond Myklebust 		memcpy(&to, xprt->timeout, sizeof(to));
22157196dbb0STrond Myklebust 		initval = DIV_ROUND_UP(connect_timeout, to.to_retries + 1);
22167196dbb0STrond Myklebust 		/* Arbitrary lower limit */
22177196dbb0STrond Myklebust 		if (initval <  XS_TCP_INIT_REEST_TO << 1)
22187196dbb0STrond Myklebust 			initval = XS_TCP_INIT_REEST_TO << 1;
22197196dbb0STrond Myklebust 		to.to_initval = initval;
22207196dbb0STrond Myklebust 		to.to_maxval = initval;
22217196dbb0STrond Myklebust 		memcpy(&transport->tcp_timeout, &to,
22227196dbb0STrond Myklebust 				sizeof(transport->tcp_timeout));
22237196dbb0STrond Myklebust 		xprt->timeout = &transport->tcp_timeout;
22247196dbb0STrond Myklebust 		xprt->connect_timeout = connect_timeout;
22257196dbb0STrond Myklebust 	}
22267196dbb0STrond Myklebust 	set_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state);
2227b5e92419STrond Myklebust 	spin_unlock(&xprt->transport_lock);
22287196dbb0STrond Myklebust }
22297196dbb0STrond Myklebust 
22308d1b8c62STrond Myklebust static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
22318d1b8c62STrond Myklebust {
22328d1b8c62STrond Myklebust 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
22338d1b8c62STrond Myklebust 
22348d1b8c62STrond Myklebust 	if (!transport->inet) {
22358d1b8c62STrond Myklebust 		struct sock *sk = sock->sk;
22368d1b8c62STrond Myklebust 
2237d88e4d82SNeilBrown 		/* Avoid temporary address, they are bad for long-lived
2238d88e4d82SNeilBrown 		 * connections such as NFS mounts.
2239d88e4d82SNeilBrown 		 * RFC4941, section 3.6 suggests that:
2240d88e4d82SNeilBrown 		 *    Individual applications, which have specific
2241d88e4d82SNeilBrown 		 *    knowledge about the normal duration of connections,
2242d88e4d82SNeilBrown 		 *    MAY override this as appropriate.
2243d88e4d82SNeilBrown 		 */
224418d5ad62SChristoph Hellwig 		if (xs_addr(xprt)->sa_family == PF_INET6) {
224518d5ad62SChristoph Hellwig 			ip6_sock_set_addr_preferences(sk,
224618d5ad62SChristoph Hellwig 				IPV6_PREFER_SRC_PUBLIC);
224718d5ad62SChristoph Hellwig 		}
2248d88e4d82SNeilBrown 
22498d1b8c62STrond Myklebust 		xs_tcp_set_socket_timeouts(xprt, sock);
2250d737e5d4STrond Myklebust 		tcp_sock_set_nodelay(sk);
2251775f06abSTrond Myklebust 
2252ea9afca8STrond Myklebust 		lock_sock(sk);
2253b0d93ad5SChuck Lever 
22542a9e1cfaSTrond Myklebust 		xs_save_old_callbacks(transport, sk);
22552a9e1cfaSTrond Myklebust 
2256b0d93ad5SChuck Lever 		sk->sk_user_data = xprt;
22575157b956STrond Myklebust 		sk->sk_data_ready = xs_data_ready;
2258b0d93ad5SChuck Lever 		sk->sk_state_change = xs_tcp_state_change;
2259b0d93ad5SChuck Lever 		sk->sk_write_space = xs_tcp_write_space;
22602118071dSTrond Myklebust 		sk->sk_error_report = xs_error_report;
22613167e12cSChuck Lever 
22623167e12cSChuck Lever 		/* socket options */
22633167e12cSChuck Lever 		sock_reset_flag(sk, SOCK_LINGER);
2264b0d93ad5SChuck Lever 
2265b0d93ad5SChuck Lever 		xprt_clear_connected(xprt);
2266b0d93ad5SChuck Lever 
2267b0d93ad5SChuck Lever 		/* Reset to new socket */
2268ee0ac0c2SChuck Lever 		transport->sock = sock;
2269ee0ac0c2SChuck Lever 		transport->inet = sk;
2270b0d93ad5SChuck Lever 
2271ea9afca8STrond Myklebust 		release_sock(sk);
2272b0d93ad5SChuck Lever 	}
2273b0d93ad5SChuck Lever 
227401d37c42STrond Myklebust 	if (!xprt_bound(xprt))
2275280254b6STrond Myklebust 		return -ENOTCONN;
227601d37c42STrond Myklebust 
2277a564b8f0SMel Gorman 	xs_set_memalloc(xprt);
2278a564b8f0SMel Gorman 
2279ae053551STrond Myklebust 	xs_stream_start_connect(transport);
2280e1806c7bSTrond Myklebust 
2281b0d93ad5SChuck Lever 	/* Tell the socket layer to start connecting... */
22820fdea1e8STrond Myklebust 	set_bit(XPRT_SOCK_CONNECTING, &transport->sock_state);
2283280254b6STrond Myklebust 	return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK);
228416be2d20SChuck Lever }
228516be2d20SChuck Lever 
228616be2d20SChuck Lever /**
2287b61d59ffSTrond Myklebust  * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint
2288acf0a39fSChuck Lever  * @work: queued work item
228916be2d20SChuck Lever  *
229016be2d20SChuck Lever  * Invoked by a work queue tasklet.
229116be2d20SChuck Lever  */
2292cdd518d5SPavel Emelyanov static void xs_tcp_setup_socket(struct work_struct *work)
229316be2d20SChuck Lever {
2294cdd518d5SPavel Emelyanov 	struct sock_xprt *transport =
2295cdd518d5SPavel Emelyanov 		container_of(work, struct sock_xprt, connect_worker.work);
229616be2d20SChuck Lever 	struct socket *sock = transport->sock;
2297a9f5f0f7SPavel Emelyanov 	struct rpc_xprt *xprt = &transport->xprt;
2298280254b6STrond Myklebust 	int status;
22998db55a03SNeilBrown 	unsigned int pflags = current->flags;
230016be2d20SChuck Lever 
23018db55a03SNeilBrown 	if (atomic_read(&xprt->swapper))
23028db55a03SNeilBrown 		current->flags |= PF_MEMALLOC;
230389f42494STrond Myklebust 
230489f42494STrond Myklebust 	if (xprt_connected(xprt))
230589f42494STrond Myklebust 		goto out;
230689f42494STrond Myklebust 	if (test_and_clear_bit(XPRT_SOCK_CONNECT_SENT,
230789f42494STrond Myklebust 			       &transport->sock_state) ||
230889f42494STrond Myklebust 	    !sock) {
230989f42494STrond Myklebust 		xs_reset_transport(transport);
231089f42494STrond Myklebust 		sock = xs_create_sock(xprt, transport, xs_addr(xprt)->sa_family,
231189f42494STrond Myklebust 				      SOCK_STREAM, IPPROTO_TCP, true);
2312b61d59ffSTrond Myklebust 		if (IS_ERR(sock)) {
2313280254b6STrond Myklebust 			xprt_wake_pending_tasks(xprt, PTR_ERR(sock));
231416be2d20SChuck Lever 			goto out;
231516be2d20SChuck Lever 		}
23167d1e8255STrond Myklebust 	}
23177d1e8255STrond Myklebust 
2318c740eff8SChuck Lever 	dprintk("RPC:       worker connecting xprt %p via %s to "
2319c740eff8SChuck Lever 				"%s (port %s)\n", xprt,
2320c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_PROTO],
2321c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_ADDR],
2322c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_PORT]);
232316be2d20SChuck Lever 
232416be2d20SChuck Lever 	status = xs_tcp_finish_connecting(xprt, sock);
232540b5ea0cSTrond Myklebust 	trace_rpc_socket_connect(xprt, sock, status);
2326a246b010SChuck Lever 	dprintk("RPC:       %p connect status %d connected %d sock state %d\n",
232746121cf7SChuck Lever 			xprt, -status, xprt_connected(xprt),
232846121cf7SChuck Lever 			sock->sk->sk_state);
2329a246b010SChuck Lever 	switch (status) {
23302a491991STrond Myklebust 	case 0:
2331a246b010SChuck Lever 	case -EINPROGRESS:
2332280254b6STrond Myklebust 		/* SYN_SENT! */
233389f42494STrond Myklebust 		set_bit(XPRT_SOCK_CONNECT_SENT, &transport->sock_state);
2334280254b6STrond Myklebust 		if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
2335280254b6STrond Myklebust 			xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2336280254b6STrond Myklebust 		fallthrough;
2337a246b010SChuck Lever 	case -EALREADY:
2338280254b6STrond Myklebust 		goto out_unlock;
2339280254b6STrond Myklebust 	case -EADDRNOTAVAIL:
2340280254b6STrond Myklebust 		/* Source port number is unavailable. Try a new one! */
2341280254b6STrond Myklebust 		transport->srcport = 0;
2342280254b6STrond Myklebust 		status = -EAGAIN;
2343280254b6STrond Myklebust 		break;
23449fcfe0c8STrond Myklebust 	case -EINVAL:
23459fcfe0c8STrond Myklebust 		/* Happens, for instance, if the user specified a link
23469fcfe0c8STrond Myklebust 		 * local IPv6 address without a scope-id.
23479fcfe0c8STrond Myklebust 		 */
23483ed5e2a2STrond Myklebust 	case -ECONNREFUSED:
23493ed5e2a2STrond Myklebust 	case -ECONNRESET:
2350eb5b46faSTrond Myklebust 	case -ENETDOWN:
23513ed5e2a2STrond Myklebust 	case -ENETUNREACH:
23524ba161a7STrond Myklebust 	case -EHOSTUNREACH:
23533913c78cSTrond Myklebust 	case -EADDRINUSE:
23543601c4a9STrond Myklebust 	case -ENOBUFS:
2355280254b6STrond Myklebust 		break;
2356280254b6STrond Myklebust 	default:
2357280254b6STrond Myklebust 		printk("%s: connect returned unhandled error %d\n",
2358280254b6STrond Myklebust 			__func__, status);
2359280254b6STrond Myklebust 		status = -EAGAIN;
2360280254b6STrond Myklebust 	}
2361280254b6STrond Myklebust 
23628c71139dSCalum Mackay 	/* xs_tcp_force_close() wakes tasks with a fixed error code.
23638c71139dSCalum Mackay 	 * We need to wake them first to ensure the correct error code.
23646ea44adcSNeilBrown 	 */
23656ea44adcSNeilBrown 	xprt_wake_pending_tasks(xprt, status);
23664efdd92cSTrond Myklebust 	xs_tcp_force_close(xprt);
2367a246b010SChuck Lever out:
23682226feb6SChuck Lever 	xprt_clear_connecting(xprt);
2369280254b6STrond Myklebust out_unlock:
2370cf76785dSTrond Myklebust 	xprt_unlock_connect(xprt, transport);
23718db55a03SNeilBrown 	current_restore_flags(pflags, PF_MEMALLOC);
2372a246b010SChuck Lever }
2373a246b010SChuck Lever 
237468e220bdSChuck Lever /**
23759903cd1cSChuck Lever  * xs_connect - connect a socket to a remote endpoint
23761b092092STrond Myklebust  * @xprt: pointer to transport structure
23779903cd1cSChuck Lever  * @task: address of RPC task that manages state of connect request
23789903cd1cSChuck Lever  *
23799903cd1cSChuck Lever  * TCP: If the remote end dropped the connection, delay reconnecting.
238003bf4b70SChuck Lever  *
238103bf4b70SChuck Lever  * UDP socket connects are synchronous, but we use a work queue anyway
238203bf4b70SChuck Lever  * to guarantee that even unprivileged user processes can set up a
238303bf4b70SChuck Lever  * socket on a privileged port.
238403bf4b70SChuck Lever  *
238503bf4b70SChuck Lever  * If a UDP socket connect fails, the delay behavior here prevents
238603bf4b70SChuck Lever  * retry floods (hard mounts).
23879903cd1cSChuck Lever  */
23881b092092STrond Myklebust static void xs_connect(struct rpc_xprt *xprt, struct rpc_task *task)
2389a246b010SChuck Lever {
2390ee0ac0c2SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
239102910177STrond Myklebust 	unsigned long delay = 0;
2392a246b010SChuck Lever 
2393718ba5b8STrond Myklebust 	WARN_ON_ONCE(!xprt_lock_connect(xprt, task, transport));
2394718ba5b8STrond Myklebust 
239589f42494STrond Myklebust 	if (transport->sock != NULL) {
239646121cf7SChuck Lever 		dprintk("RPC:       xs_connect delayed xprt %p for %lu "
239789f42494STrond Myklebust 			"seconds\n", xprt, xprt->reestablish_timeout / HZ);
239899b1a4c3STrond Myklebust 
2399675dd90aSChuck Lever 		delay = xprt_reconnect_delay(xprt);
2400675dd90aSChuck Lever 		xprt_reconnect_backoff(xprt, XS_TCP_INIT_REEST_TO);
240102910177STrond Myklebust 
240202910177STrond Myklebust 	} else
24039903cd1cSChuck Lever 		dprintk("RPC:       xs_connect scheduled xprt %p\n", xprt);
240402910177STrond Myklebust 
240540a5f1b1STrond Myklebust 	queue_delayed_work(xprtiod_workqueue,
240602910177STrond Myklebust 			&transport->connect_worker,
240702910177STrond Myklebust 			delay);
2408a246b010SChuck Lever }
2409a246b010SChuck Lever 
24104f8943f8STrond Myklebust static void xs_wake_disconnect(struct sock_xprt *transport)
24114f8943f8STrond Myklebust {
24124f8943f8STrond Myklebust 	if (test_and_clear_bit(XPRT_SOCK_WAKE_DISCONNECT, &transport->sock_state))
24134f8943f8STrond Myklebust 		xs_tcp_force_close(&transport->xprt);
24144f8943f8STrond Myklebust }
24154f8943f8STrond Myklebust 
24164f8943f8STrond Myklebust static void xs_wake_write(struct sock_xprt *transport)
24174f8943f8STrond Myklebust {
24184f8943f8STrond Myklebust 	if (test_and_clear_bit(XPRT_SOCK_WAKE_WRITE, &transport->sock_state))
24194f8943f8STrond Myklebust 		xprt_write_space(&transport->xprt);
24204f8943f8STrond Myklebust }
24214f8943f8STrond Myklebust 
24224f8943f8STrond Myklebust static void xs_wake_error(struct sock_xprt *transport)
24234f8943f8STrond Myklebust {
24244f8943f8STrond Myklebust 	int sockerr;
24254f8943f8STrond Myklebust 
24264f8943f8STrond Myklebust 	if (!test_bit(XPRT_SOCK_WAKE_ERROR, &transport->sock_state))
24274f8943f8STrond Myklebust 		return;
24284f8943f8STrond Myklebust 	mutex_lock(&transport->recv_mutex);
24294f8943f8STrond Myklebust 	if (transport->sock == NULL)
24304f8943f8STrond Myklebust 		goto out;
24314f8943f8STrond Myklebust 	if (!test_and_clear_bit(XPRT_SOCK_WAKE_ERROR, &transport->sock_state))
24324f8943f8STrond Myklebust 		goto out;
2433af84537dSBenjamin Coddington 	sockerr = xchg(&transport->xprt_err, 0);
24344f8943f8STrond Myklebust 	if (sockerr < 0)
24354f8943f8STrond Myklebust 		xprt_wake_pending_tasks(&transport->xprt, sockerr);
24364f8943f8STrond Myklebust out:
24374f8943f8STrond Myklebust 	mutex_unlock(&transport->recv_mutex);
24384f8943f8STrond Myklebust }
24394f8943f8STrond Myklebust 
24404f8943f8STrond Myklebust static void xs_wake_pending(struct sock_xprt *transport)
24414f8943f8STrond Myklebust {
24424f8943f8STrond Myklebust 	if (test_and_clear_bit(XPRT_SOCK_WAKE_PENDING, &transport->sock_state))
24434f8943f8STrond Myklebust 		xprt_wake_pending_tasks(&transport->xprt, -EAGAIN);
24444f8943f8STrond Myklebust }
24454f8943f8STrond Myklebust 
24464f8943f8STrond Myklebust static void xs_error_handle(struct work_struct *work)
24474f8943f8STrond Myklebust {
24484f8943f8STrond Myklebust 	struct sock_xprt *transport = container_of(work,
24494f8943f8STrond Myklebust 			struct sock_xprt, error_worker);
24504f8943f8STrond Myklebust 
24514f8943f8STrond Myklebust 	xs_wake_disconnect(transport);
24524f8943f8STrond Myklebust 	xs_wake_write(transport);
24534f8943f8STrond Myklebust 	xs_wake_error(transport);
24544f8943f8STrond Myklebust 	xs_wake_pending(transport);
24554f8943f8STrond Myklebust }
24564f8943f8STrond Myklebust 
2457262ca07dSChuck Lever /**
245812b20ce3SBhaskar Chowdhury  * xs_local_print_stats - display AF_LOCAL socket-specific stats
2459176e21eeSChuck Lever  * @xprt: rpc_xprt struct containing statistics
2460176e21eeSChuck Lever  * @seq: output file
2461176e21eeSChuck Lever  *
2462176e21eeSChuck Lever  */
2463176e21eeSChuck Lever static void xs_local_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2464176e21eeSChuck Lever {
2465176e21eeSChuck Lever 	long idle_time = 0;
2466176e21eeSChuck Lever 
2467176e21eeSChuck Lever 	if (xprt_connected(xprt))
2468176e21eeSChuck Lever 		idle_time = (long)(jiffies - xprt->last_used) / HZ;
2469176e21eeSChuck Lever 
2470176e21eeSChuck Lever 	seq_printf(seq, "\txprt:\tlocal %lu %lu %lu %ld %lu %lu %lu "
247115a45206SAndy Adamson 			"%llu %llu %lu %llu %llu\n",
2472176e21eeSChuck Lever 			xprt->stat.bind_count,
2473176e21eeSChuck Lever 			xprt->stat.connect_count,
24748440a886SChuck Lever 			xprt->stat.connect_time / HZ,
2475176e21eeSChuck Lever 			idle_time,
2476176e21eeSChuck Lever 			xprt->stat.sends,
2477176e21eeSChuck Lever 			xprt->stat.recvs,
2478176e21eeSChuck Lever 			xprt->stat.bad_xids,
2479176e21eeSChuck Lever 			xprt->stat.req_u,
248015a45206SAndy Adamson 			xprt->stat.bklog_u,
248115a45206SAndy Adamson 			xprt->stat.max_slots,
248215a45206SAndy Adamson 			xprt->stat.sending_u,
248315a45206SAndy Adamson 			xprt->stat.pending_u);
2484176e21eeSChuck Lever }
2485176e21eeSChuck Lever 
2486176e21eeSChuck Lever /**
248712b20ce3SBhaskar Chowdhury  * xs_udp_print_stats - display UDP socket-specific stats
2488262ca07dSChuck Lever  * @xprt: rpc_xprt struct containing statistics
2489262ca07dSChuck Lever  * @seq: output file
2490262ca07dSChuck Lever  *
2491262ca07dSChuck Lever  */
2492262ca07dSChuck Lever static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2493262ca07dSChuck Lever {
2494c8475461SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2495c8475461SChuck Lever 
249615a45206SAndy Adamson 	seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %llu %llu "
249715a45206SAndy Adamson 			"%lu %llu %llu\n",
2498fbfffbd5SChuck Lever 			transport->srcport,
2499262ca07dSChuck Lever 			xprt->stat.bind_count,
2500262ca07dSChuck Lever 			xprt->stat.sends,
2501262ca07dSChuck Lever 			xprt->stat.recvs,
2502262ca07dSChuck Lever 			xprt->stat.bad_xids,
2503262ca07dSChuck Lever 			xprt->stat.req_u,
250415a45206SAndy Adamson 			xprt->stat.bklog_u,
250515a45206SAndy Adamson 			xprt->stat.max_slots,
250615a45206SAndy Adamson 			xprt->stat.sending_u,
250715a45206SAndy Adamson 			xprt->stat.pending_u);
2508262ca07dSChuck Lever }
2509262ca07dSChuck Lever 
2510262ca07dSChuck Lever /**
251112b20ce3SBhaskar Chowdhury  * xs_tcp_print_stats - display TCP socket-specific stats
2512262ca07dSChuck Lever  * @xprt: rpc_xprt struct containing statistics
2513262ca07dSChuck Lever  * @seq: output file
2514262ca07dSChuck Lever  *
2515262ca07dSChuck Lever  */
2516262ca07dSChuck Lever static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2517262ca07dSChuck Lever {
2518c8475461SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2519262ca07dSChuck Lever 	long idle_time = 0;
2520262ca07dSChuck Lever 
2521262ca07dSChuck Lever 	if (xprt_connected(xprt))
2522262ca07dSChuck Lever 		idle_time = (long)(jiffies - xprt->last_used) / HZ;
2523262ca07dSChuck Lever 
252415a45206SAndy Adamson 	seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu "
252515a45206SAndy Adamson 			"%llu %llu %lu %llu %llu\n",
2526fbfffbd5SChuck Lever 			transport->srcport,
2527262ca07dSChuck Lever 			xprt->stat.bind_count,
2528262ca07dSChuck Lever 			xprt->stat.connect_count,
25298440a886SChuck Lever 			xprt->stat.connect_time / HZ,
2530262ca07dSChuck Lever 			idle_time,
2531262ca07dSChuck Lever 			xprt->stat.sends,
2532262ca07dSChuck Lever 			xprt->stat.recvs,
2533262ca07dSChuck Lever 			xprt->stat.bad_xids,
2534262ca07dSChuck Lever 			xprt->stat.req_u,
253515a45206SAndy Adamson 			xprt->stat.bklog_u,
253615a45206SAndy Adamson 			xprt->stat.max_slots,
253715a45206SAndy Adamson 			xprt->stat.sending_u,
253815a45206SAndy Adamson 			xprt->stat.pending_u);
2539262ca07dSChuck Lever }
2540262ca07dSChuck Lever 
25414cfc7e60SRahul Iyer /*
25424cfc7e60SRahul Iyer  * Allocate a bunch of pages for a scratch buffer for the rpc code. The reason
25434cfc7e60SRahul Iyer  * we allocate pages instead doing a kmalloc like rpc_malloc is because we want
25444cfc7e60SRahul Iyer  * to use the server side send routines.
25454cfc7e60SRahul Iyer  */
25465fe6eaa1SChuck Lever static int bc_malloc(struct rpc_task *task)
25474cfc7e60SRahul Iyer {
25485fe6eaa1SChuck Lever 	struct rpc_rqst *rqst = task->tk_rqstp;
25495fe6eaa1SChuck Lever 	size_t size = rqst->rq_callsize;
25504cfc7e60SRahul Iyer 	struct page *page;
25514cfc7e60SRahul Iyer 	struct rpc_buffer *buf;
25524cfc7e60SRahul Iyer 
25535fe6eaa1SChuck Lever 	if (size > PAGE_SIZE - sizeof(struct rpc_buffer)) {
25545fe6eaa1SChuck Lever 		WARN_ONCE(1, "xprtsock: large bc buffer request (size %zu)\n",
25555fe6eaa1SChuck Lever 			  size);
25565fe6eaa1SChuck Lever 		return -EINVAL;
25575fe6eaa1SChuck Lever 	}
25584cfc7e60SRahul Iyer 
2559b2648015STrond Myklebust 	page = alloc_page(GFP_KERNEL | __GFP_NORETRY | __GFP_NOWARN);
25604cfc7e60SRahul Iyer 	if (!page)
25615fe6eaa1SChuck Lever 		return -ENOMEM;
25624cfc7e60SRahul Iyer 
25634cfc7e60SRahul Iyer 	buf = page_address(page);
25644cfc7e60SRahul Iyer 	buf->len = PAGE_SIZE;
25654cfc7e60SRahul Iyer 
25665fe6eaa1SChuck Lever 	rqst->rq_buffer = buf->data;
256718e601d6SJeff Layton 	rqst->rq_rbuffer = (char *)rqst->rq_buffer + rqst->rq_callsize;
25685fe6eaa1SChuck Lever 	return 0;
25694cfc7e60SRahul Iyer }
25704cfc7e60SRahul Iyer 
25714cfc7e60SRahul Iyer /*
25724cfc7e60SRahul Iyer  * Free the space allocated in the bc_alloc routine
25734cfc7e60SRahul Iyer  */
25743435c74aSChuck Lever static void bc_free(struct rpc_task *task)
25754cfc7e60SRahul Iyer {
25763435c74aSChuck Lever 	void *buffer = task->tk_rqstp->rq_buffer;
25774cfc7e60SRahul Iyer 	struct rpc_buffer *buf;
25784cfc7e60SRahul Iyer 
25794cfc7e60SRahul Iyer 	buf = container_of(buffer, struct rpc_buffer, data);
25804cfc7e60SRahul Iyer 	free_page((unsigned long)buf);
25814cfc7e60SRahul Iyer }
25824cfc7e60SRahul Iyer 
25834cfc7e60SRahul Iyer static int bc_sendto(struct rpc_rqst *req)
25844cfc7e60SRahul Iyer {
2585da1661b9SChuck Lever 	struct xdr_buf *xdr = &req->rq_snd_buf;
25864cfc7e60SRahul Iyer 	struct sock_xprt *transport =
2587067fb11bSChuck Lever 			container_of(req->rq_xprt, struct sock_xprt, xprt);
2588067fb11bSChuck Lever 	struct msghdr msg = {
2589da1661b9SChuck Lever 		.msg_flags	= 0,
2590067fb11bSChuck Lever 	};
2591067fb11bSChuck Lever 	rpc_fraghdr marker = cpu_to_be32(RPC_LAST_STREAM_FRAGMENT |
2592da1661b9SChuck Lever 					 (u32)xdr->len);
2593da1661b9SChuck Lever 	unsigned int sent = 0;
2594da1661b9SChuck Lever 	int err;
25954cfc7e60SRahul Iyer 
25968729aabaSChuck Lever 	req->rq_xtime = ktime_get();
2597ff053dbbSTrond Myklebust 	err = xdr_alloc_bvec(xdr, rpc_task_gfp_mask());
2598ff053dbbSTrond Myklebust 	if (err < 0)
2599ff053dbbSTrond Myklebust 		return err;
2600da1661b9SChuck Lever 	err = xprt_sock_sendmsg(transport->sock, &msg, xdr, 0, marker, &sent);
2601da1661b9SChuck Lever 	xdr_free_bvec(xdr);
2602da1661b9SChuck Lever 	if (err < 0 || sent != (xdr->len + sizeof(marker)))
2603067fb11bSChuck Lever 		return -EAGAIN;
2604da1661b9SChuck Lever 	return sent;
26054cfc7e60SRahul Iyer }
26064cfc7e60SRahul Iyer 
2607ca4faf54SChuck Lever /**
2608ca4faf54SChuck Lever  * bc_send_request - Send a backchannel Call on a TCP socket
2609ca4faf54SChuck Lever  * @req: rpc_rqst containing Call message to be sent
2610ca4faf54SChuck Lever  *
2611ca4faf54SChuck Lever  * xpt_mutex ensures @rqstp's whole message is written to the socket
2612ca4faf54SChuck Lever  * without interruption.
2613ca4faf54SChuck Lever  *
2614ca4faf54SChuck Lever  * Return values:
2615ca4faf54SChuck Lever  *   %0 if the message was sent successfully
2616ca4faf54SChuck Lever  *   %ENOTCONN if the message was not sent
26174cfc7e60SRahul Iyer  */
2618adfa7144STrond Myklebust static int bc_send_request(struct rpc_rqst *req)
26194cfc7e60SRahul Iyer {
26204cfc7e60SRahul Iyer 	struct svc_xprt	*xprt;
26217fc56136SAndrzej Hajda 	int len;
26224cfc7e60SRahul Iyer 
26234cfc7e60SRahul Iyer 	/*
26244cfc7e60SRahul Iyer 	 * Get the server socket associated with this callback xprt
26254cfc7e60SRahul Iyer 	 */
26264cfc7e60SRahul Iyer 	xprt = req->rq_xprt->bc_xprt;
26274cfc7e60SRahul Iyer 
26284cfc7e60SRahul Iyer 	/*
26294cfc7e60SRahul Iyer 	 * Grab the mutex to serialize data as the connection is shared
26304cfc7e60SRahul Iyer 	 * with the fore channel
26314cfc7e60SRahul Iyer 	 */
2632c544577dSTrond Myklebust 	mutex_lock(&xprt->xpt_mutex);
26334cfc7e60SRahul Iyer 	if (test_bit(XPT_DEAD, &xprt->xpt_flags))
26344cfc7e60SRahul Iyer 		len = -ENOTCONN;
26354cfc7e60SRahul Iyer 	else
26364cfc7e60SRahul Iyer 		len = bc_sendto(req);
26374cfc7e60SRahul Iyer 	mutex_unlock(&xprt->xpt_mutex);
26384cfc7e60SRahul Iyer 
26394cfc7e60SRahul Iyer 	if (len > 0)
26404cfc7e60SRahul Iyer 		len = 0;
26414cfc7e60SRahul Iyer 
26424cfc7e60SRahul Iyer 	return len;
26434cfc7e60SRahul Iyer }
26444cfc7e60SRahul Iyer 
26454cfc7e60SRahul Iyer /*
26464cfc7e60SRahul Iyer  * The close routine. Since this is client initiated, we do nothing
26474cfc7e60SRahul Iyer  */
26484cfc7e60SRahul Iyer 
26494cfc7e60SRahul Iyer static void bc_close(struct rpc_xprt *xprt)
26504cfc7e60SRahul Iyer {
26516221f1d9SChuck Lever 	xprt_disconnect_done(xprt);
26524cfc7e60SRahul Iyer }
26534cfc7e60SRahul Iyer 
26544cfc7e60SRahul Iyer /*
26554cfc7e60SRahul Iyer  * The xprt destroy routine. Again, because this connection is client
26564cfc7e60SRahul Iyer  * initiated, we do nothing
26574cfc7e60SRahul Iyer  */
26584cfc7e60SRahul Iyer 
26594cfc7e60SRahul Iyer static void bc_destroy(struct rpc_xprt *xprt)
26604cfc7e60SRahul Iyer {
266147f72efaSKinglong Mee 	dprintk("RPC:       bc_destroy xprt %p\n", xprt);
266247f72efaSKinglong Mee 
266347f72efaSKinglong Mee 	xs_xprt_free(xprt);
266447f72efaSKinglong Mee 	module_put(THIS_MODULE);
26654cfc7e60SRahul Iyer }
26664cfc7e60SRahul Iyer 
2667d31ae254SChuck Lever static const struct rpc_xprt_ops xs_local_ops = {
2668176e21eeSChuck Lever 	.reserve_xprt		= xprt_reserve_xprt,
26694cd34e7cSTrond Myklebust 	.release_xprt		= xprt_release_xprt,
2670f39c1bfbSTrond Myklebust 	.alloc_slot		= xprt_alloc_slot,
2671a9cde23aSChuck Lever 	.free_slot		= xprt_free_slot,
2672176e21eeSChuck Lever 	.rpcbind		= xs_local_rpcbind,
2673176e21eeSChuck Lever 	.set_port		= xs_local_set_port,
2674dc107402SJ. Bruce Fields 	.connect		= xs_local_connect,
2675176e21eeSChuck Lever 	.buf_alloc		= rpc_malloc,
2676176e21eeSChuck Lever 	.buf_free		= rpc_free,
2677550aebfeSTrond Myklebust 	.prepare_request	= xs_stream_prepare_request,
2678176e21eeSChuck Lever 	.send_request		= xs_local_send_request,
26798ba6a92dSTrond Myklebust 	.wait_for_reply_request	= xprt_wait_for_reply_request_def,
2680176e21eeSChuck Lever 	.close			= xs_close,
2681a1311d87STrond Myklebust 	.destroy		= xs_destroy,
2682176e21eeSChuck Lever 	.print_stats		= xs_local_print_stats,
2683d67fa4d8SJeff Layton 	.enable_swap		= xs_enable_swap,
2684d67fa4d8SJeff Layton 	.disable_swap		= xs_disable_swap,
2685176e21eeSChuck Lever };
2686176e21eeSChuck Lever 
2687d31ae254SChuck Lever static const struct rpc_xprt_ops xs_udp_ops = {
268843118c29SChuck Lever 	.set_buffer_size	= xs_udp_set_buffer_size,
268912a80469SChuck Lever 	.reserve_xprt		= xprt_reserve_xprt_cong,
269049e9a890SChuck Lever 	.release_xprt		= xprt_release_xprt_cong,
2691f39c1bfbSTrond Myklebust 	.alloc_slot		= xprt_alloc_slot,
2692a9cde23aSChuck Lever 	.free_slot		= xprt_free_slot,
269345160d62SChuck Lever 	.rpcbind		= rpcb_getport_async,
269492200412SChuck Lever 	.set_port		= xs_set_port,
26959903cd1cSChuck Lever 	.connect		= xs_connect,
2696421ab1beSTrond Myklebust 	.get_srcaddr		= xs_sock_srcaddr,
2697421ab1beSTrond Myklebust 	.get_srcport		= xs_sock_srcport,
269802107148SChuck Lever 	.buf_alloc		= rpc_malloc,
269902107148SChuck Lever 	.buf_free		= rpc_free,
2700262965f5SChuck Lever 	.send_request		= xs_udp_send_request,
27018ba6a92dSTrond Myklebust 	.wait_for_reply_request	= xprt_wait_for_reply_request_rtt,
270246c0ee8bSChuck Lever 	.timer			= xs_udp_timer,
2703a58dd398SChuck Lever 	.release_request	= xprt_release_rqst_cong,
2704262965f5SChuck Lever 	.close			= xs_close,
2705262965f5SChuck Lever 	.destroy		= xs_destroy,
2706262ca07dSChuck Lever 	.print_stats		= xs_udp_print_stats,
2707d67fa4d8SJeff Layton 	.enable_swap		= xs_enable_swap,
2708d67fa4d8SJeff Layton 	.disable_swap		= xs_disable_swap,
27094a068258SChuck Lever 	.inject_disconnect	= xs_inject_disconnect,
2710262965f5SChuck Lever };
2711262965f5SChuck Lever 
2712d31ae254SChuck Lever static const struct rpc_xprt_ops xs_tcp_ops = {
271312a80469SChuck Lever 	.reserve_xprt		= xprt_reserve_xprt,
27144cd34e7cSTrond Myklebust 	.release_xprt		= xprt_release_xprt,
271536bd7de9STrond Myklebust 	.alloc_slot		= xprt_alloc_slot,
2716a9cde23aSChuck Lever 	.free_slot		= xprt_free_slot,
271745160d62SChuck Lever 	.rpcbind		= rpcb_getport_async,
271892200412SChuck Lever 	.set_port		= xs_set_port,
27190b9e7943STrond Myklebust 	.connect		= xs_connect,
2720421ab1beSTrond Myklebust 	.get_srcaddr		= xs_sock_srcaddr,
2721421ab1beSTrond Myklebust 	.get_srcport		= xs_sock_srcport,
272202107148SChuck Lever 	.buf_alloc		= rpc_malloc,
272302107148SChuck Lever 	.buf_free		= rpc_free,
2724277e4ab7STrond Myklebust 	.prepare_request	= xs_stream_prepare_request,
2725262965f5SChuck Lever 	.send_request		= xs_tcp_send_request,
27268ba6a92dSTrond Myklebust 	.wait_for_reply_request	= xprt_wait_for_reply_request_def,
2727c627d31bSTrond Myklebust 	.close			= xs_tcp_shutdown,
27289903cd1cSChuck Lever 	.destroy		= xs_destroy,
27297196dbb0STrond Myklebust 	.set_connect_timeout	= xs_tcp_set_connect_timeout,
2730262ca07dSChuck Lever 	.print_stats		= xs_tcp_print_stats,
2731d67fa4d8SJeff Layton 	.enable_swap		= xs_enable_swap,
2732d67fa4d8SJeff Layton 	.disable_swap		= xs_disable_swap,
27334a068258SChuck Lever 	.inject_disconnect	= xs_inject_disconnect,
273442e5c3e2SChuck Lever #ifdef CONFIG_SUNRPC_BACKCHANNEL
273542e5c3e2SChuck Lever 	.bc_setup		= xprt_setup_bc,
27366b26cc8cSChuck Lever 	.bc_maxpayload		= xs_tcp_bc_maxpayload,
27377402a4feSTrond Myklebust 	.bc_num_slots		= xprt_bc_max_slots,
273842e5c3e2SChuck Lever 	.bc_free_rqst		= xprt_free_bc_rqst,
273942e5c3e2SChuck Lever 	.bc_destroy		= xprt_destroy_bc,
274042e5c3e2SChuck Lever #endif
2741a246b010SChuck Lever };
2742a246b010SChuck Lever 
27434cfc7e60SRahul Iyer /*
27444cfc7e60SRahul Iyer  * The rpc_xprt_ops for the server backchannel
27454cfc7e60SRahul Iyer  */
27464cfc7e60SRahul Iyer 
2747d31ae254SChuck Lever static const struct rpc_xprt_ops bc_tcp_ops = {
27484cfc7e60SRahul Iyer 	.reserve_xprt		= xprt_reserve_xprt,
27494cfc7e60SRahul Iyer 	.release_xprt		= xprt_release_xprt,
275084e28a30SBryan Schumaker 	.alloc_slot		= xprt_alloc_slot,
2751a9cde23aSChuck Lever 	.free_slot		= xprt_free_slot,
27524cfc7e60SRahul Iyer 	.buf_alloc		= bc_malloc,
27534cfc7e60SRahul Iyer 	.buf_free		= bc_free,
27544cfc7e60SRahul Iyer 	.send_request		= bc_send_request,
27558ba6a92dSTrond Myklebust 	.wait_for_reply_request	= xprt_wait_for_reply_request_def,
27564cfc7e60SRahul Iyer 	.close			= bc_close,
27574cfc7e60SRahul Iyer 	.destroy		= bc_destroy,
27584cfc7e60SRahul Iyer 	.print_stats		= xs_tcp_print_stats,
2759d67fa4d8SJeff Layton 	.enable_swap		= xs_enable_swap,
2760d67fa4d8SJeff Layton 	.disable_swap		= xs_disable_swap,
27614a068258SChuck Lever 	.inject_disconnect	= xs_inject_disconnect,
27624cfc7e60SRahul Iyer };
27634cfc7e60SRahul Iyer 
276492476850SChuck Lever static int xs_init_anyaddr(const int family, struct sockaddr *sap)
276592476850SChuck Lever {
276692476850SChuck Lever 	static const struct sockaddr_in sin = {
276792476850SChuck Lever 		.sin_family		= AF_INET,
276892476850SChuck Lever 		.sin_addr.s_addr	= htonl(INADDR_ANY),
276992476850SChuck Lever 	};
277092476850SChuck Lever 	static const struct sockaddr_in6 sin6 = {
277192476850SChuck Lever 		.sin6_family		= AF_INET6,
277292476850SChuck Lever 		.sin6_addr		= IN6ADDR_ANY_INIT,
277392476850SChuck Lever 	};
277492476850SChuck Lever 
277592476850SChuck Lever 	switch (family) {
2776176e21eeSChuck Lever 	case AF_LOCAL:
2777176e21eeSChuck Lever 		break;
277892476850SChuck Lever 	case AF_INET:
277992476850SChuck Lever 		memcpy(sap, &sin, sizeof(sin));
278092476850SChuck Lever 		break;
278192476850SChuck Lever 	case AF_INET6:
278292476850SChuck Lever 		memcpy(sap, &sin6, sizeof(sin6));
278392476850SChuck Lever 		break;
278492476850SChuck Lever 	default:
278592476850SChuck Lever 		dprintk("RPC:       %s: Bad address family\n", __func__);
278692476850SChuck Lever 		return -EAFNOSUPPORT;
278792476850SChuck Lever 	}
278892476850SChuck Lever 	return 0;
278992476850SChuck Lever }
279092476850SChuck Lever 
27913c341b0bS\"Talpey, Thomas\ static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args,
2792d9ba131dSTrond Myklebust 				      unsigned int slot_table_size,
2793d9ba131dSTrond Myklebust 				      unsigned int max_slot_table_size)
2794c8541ecdSChuck Lever {
2795c8541ecdSChuck Lever 	struct rpc_xprt *xprt;
2796ffc2e518SChuck Lever 	struct sock_xprt *new;
2797c8541ecdSChuck Lever 
279896802a09SFrank van Maarseveen 	if (args->addrlen > sizeof(xprt->addr)) {
2799c8541ecdSChuck Lever 		dprintk("RPC:       xs_setup_xprt: address too large\n");
2800c8541ecdSChuck Lever 		return ERR_PTR(-EBADF);
2801c8541ecdSChuck Lever 	}
2802c8541ecdSChuck Lever 
2803d9ba131dSTrond Myklebust 	xprt = xprt_alloc(args->net, sizeof(*new), slot_table_size,
2804d9ba131dSTrond Myklebust 			max_slot_table_size);
2805bd1722d4SPavel Emelyanov 	if (xprt == NULL) {
280646121cf7SChuck Lever 		dprintk("RPC:       xs_setup_xprt: couldn't allocate "
280746121cf7SChuck Lever 				"rpc_xprt\n");
2808c8541ecdSChuck Lever 		return ERR_PTR(-ENOMEM);
2809c8541ecdSChuck Lever 	}
2810c8541ecdSChuck Lever 
2811bd1722d4SPavel Emelyanov 	new = container_of(xprt, struct sock_xprt, xprt);
2812edc1b01cSTrond Myklebust 	mutex_init(&new->recv_mutex);
281396802a09SFrank van Maarseveen 	memcpy(&xprt->addr, args->dstaddr, args->addrlen);
281496802a09SFrank van Maarseveen 	xprt->addrlen = args->addrlen;
2815d3bc9a1dSFrank van Maarseveen 	if (args->srcaddr)
2816fbfffbd5SChuck Lever 		memcpy(&new->srcaddr, args->srcaddr, args->addrlen);
281792476850SChuck Lever 	else {
281892476850SChuck Lever 		int err;
281992476850SChuck Lever 		err = xs_init_anyaddr(args->dstaddr->sa_family,
282092476850SChuck Lever 					(struct sockaddr *)&new->srcaddr);
28212aa13531SStanislav Kinsbursky 		if (err != 0) {
28222aa13531SStanislav Kinsbursky 			xprt_free(xprt);
282392476850SChuck Lever 			return ERR_PTR(err);
282492476850SChuck Lever 		}
28252aa13531SStanislav Kinsbursky 	}
2826c8541ecdSChuck Lever 
2827c8541ecdSChuck Lever 	return xprt;
2828c8541ecdSChuck Lever }
2829c8541ecdSChuck Lever 
2830176e21eeSChuck Lever static const struct rpc_timeout xs_local_default_timeout = {
2831176e21eeSChuck Lever 	.to_initval = 10 * HZ,
2832176e21eeSChuck Lever 	.to_maxval = 10 * HZ,
2833176e21eeSChuck Lever 	.to_retries = 2,
2834176e21eeSChuck Lever };
2835176e21eeSChuck Lever 
2836176e21eeSChuck Lever /**
2837176e21eeSChuck Lever  * xs_setup_local - Set up transport to use an AF_LOCAL socket
2838176e21eeSChuck Lever  * @args: rpc transport creation arguments
2839176e21eeSChuck Lever  *
2840176e21eeSChuck Lever  * AF_LOCAL is a "tpi_cots_ord" transport, just like TCP
2841176e21eeSChuck Lever  */
2842176e21eeSChuck Lever static struct rpc_xprt *xs_setup_local(struct xprt_create *args)
2843176e21eeSChuck Lever {
2844176e21eeSChuck Lever 	struct sockaddr_un *sun = (struct sockaddr_un *)args->dstaddr;
2845176e21eeSChuck Lever 	struct sock_xprt *transport;
2846176e21eeSChuck Lever 	struct rpc_xprt *xprt;
2847176e21eeSChuck Lever 	struct rpc_xprt *ret;
2848176e21eeSChuck Lever 
2849d9ba131dSTrond Myklebust 	xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
2850d9ba131dSTrond Myklebust 			xprt_max_tcp_slot_table_entries);
2851176e21eeSChuck Lever 	if (IS_ERR(xprt))
2852176e21eeSChuck Lever 		return xprt;
2853176e21eeSChuck Lever 	transport = container_of(xprt, struct sock_xprt, xprt);
2854176e21eeSChuck Lever 
2855176e21eeSChuck Lever 	xprt->prot = 0;
2856d3abc739SOlga Kornievskaia 	xprt->xprt_class = &xs_local_transport;
2857176e21eeSChuck Lever 	xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2858176e21eeSChuck Lever 
2859176e21eeSChuck Lever 	xprt->bind_timeout = XS_BIND_TO;
2860176e21eeSChuck Lever 	xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2861176e21eeSChuck Lever 	xprt->idle_timeout = XS_IDLE_DISC_TO;
2862176e21eeSChuck Lever 
2863176e21eeSChuck Lever 	xprt->ops = &xs_local_ops;
2864176e21eeSChuck Lever 	xprt->timeout = &xs_local_default_timeout;
2865176e21eeSChuck Lever 
2866550aebfeSTrond Myklebust 	INIT_WORK(&transport->recv_worker, xs_stream_data_receive_workfn);
28674f8943f8STrond Myklebust 	INIT_WORK(&transport->error_worker, xs_error_handle);
2868550aebfeSTrond Myklebust 	INIT_DELAYED_WORK(&transport->connect_worker, xs_dummy_setup_socket);
286993dc41bdSNeilBrown 
2870176e21eeSChuck Lever 	switch (sun->sun_family) {
2871176e21eeSChuck Lever 	case AF_LOCAL:
2872176e21eeSChuck Lever 		if (sun->sun_path[0] != '/') {
2873176e21eeSChuck Lever 			dprintk("RPC:       bad AF_LOCAL address: %s\n",
2874176e21eeSChuck Lever 					sun->sun_path);
2875176e21eeSChuck Lever 			ret = ERR_PTR(-EINVAL);
2876176e21eeSChuck Lever 			goto out_err;
2877176e21eeSChuck Lever 		}
2878176e21eeSChuck Lever 		xprt_set_bound(xprt);
2879176e21eeSChuck Lever 		xs_format_peer_addresses(xprt, "local", RPCBIND_NETID_LOCAL);
2880176e21eeSChuck Lever 		break;
2881176e21eeSChuck Lever 	default:
2882176e21eeSChuck Lever 		ret = ERR_PTR(-EAFNOSUPPORT);
2883176e21eeSChuck Lever 		goto out_err;
2884176e21eeSChuck Lever 	}
2885176e21eeSChuck Lever 
2886176e21eeSChuck Lever 	dprintk("RPC:       set up xprt to %s via AF_LOCAL\n",
2887176e21eeSChuck Lever 			xprt->address_strings[RPC_DISPLAY_ADDR]);
2888176e21eeSChuck Lever 
2889176e21eeSChuck Lever 	if (try_module_get(THIS_MODULE))
2890176e21eeSChuck Lever 		return xprt;
2891176e21eeSChuck Lever 	ret = ERR_PTR(-EINVAL);
2892176e21eeSChuck Lever out_err:
2893315f3812SKinglong Mee 	xs_xprt_free(xprt);
2894176e21eeSChuck Lever 	return ret;
2895176e21eeSChuck Lever }
2896176e21eeSChuck Lever 
28972881ae74STrond Myklebust static const struct rpc_timeout xs_udp_default_timeout = {
28982881ae74STrond Myklebust 	.to_initval = 5 * HZ,
28992881ae74STrond Myklebust 	.to_maxval = 30 * HZ,
29002881ae74STrond Myklebust 	.to_increment = 5 * HZ,
29012881ae74STrond Myklebust 	.to_retries = 5,
29022881ae74STrond Myklebust };
29032881ae74STrond Myklebust 
29049903cd1cSChuck Lever /**
29059903cd1cSChuck Lever  * xs_setup_udp - Set up transport to use a UDP socket
290696802a09SFrank van Maarseveen  * @args: rpc transport creation arguments
29079903cd1cSChuck Lever  *
29089903cd1cSChuck Lever  */
2909483066d6SAdrian Bunk static struct rpc_xprt *xs_setup_udp(struct xprt_create *args)
2910a246b010SChuck Lever {
29118f9d5b1aSChuck Lever 	struct sockaddr *addr = args->dstaddr;
2912c8541ecdSChuck Lever 	struct rpc_xprt *xprt;
2913c8475461SChuck Lever 	struct sock_xprt *transport;
29140a68b0beSJ. Bruce Fields 	struct rpc_xprt *ret;
2915a246b010SChuck Lever 
2916d9ba131dSTrond Myklebust 	xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries,
2917d9ba131dSTrond Myklebust 			xprt_udp_slot_table_entries);
2918c8541ecdSChuck Lever 	if (IS_ERR(xprt))
2919c8541ecdSChuck Lever 		return xprt;
2920c8475461SChuck Lever 	transport = container_of(xprt, struct sock_xprt, xprt);
2921a246b010SChuck Lever 
2922ec739ef0SChuck Lever 	xprt->prot = IPPROTO_UDP;
2923d3abc739SOlga Kornievskaia 	xprt->xprt_class = &xs_udp_transport;
2924a246b010SChuck Lever 	/* XXX: header size can vary due to auth type, IPv6, etc. */
2925a246b010SChuck Lever 	xprt->max_payload = (1U << 16) - (MAX_HEADER << 3);
2926a246b010SChuck Lever 
292703bf4b70SChuck Lever 	xprt->bind_timeout = XS_BIND_TO;
292803bf4b70SChuck Lever 	xprt->reestablish_timeout = XS_UDP_REEST_TO;
292903bf4b70SChuck Lever 	xprt->idle_timeout = XS_IDLE_DISC_TO;
2930a246b010SChuck Lever 
2931262965f5SChuck Lever 	xprt->ops = &xs_udp_ops;
2932a246b010SChuck Lever 
2933ba7392bbSTrond Myklebust 	xprt->timeout = &xs_udp_default_timeout;
2934a246b010SChuck Lever 
2935f9b2ee71STrond Myklebust 	INIT_WORK(&transport->recv_worker, xs_udp_data_receive_workfn);
29364f8943f8STrond Myklebust 	INIT_WORK(&transport->error_worker, xs_error_handle);
2937edc1b01cSTrond Myklebust 	INIT_DELAYED_WORK(&transport->connect_worker, xs_udp_setup_socket);
2938edc1b01cSTrond Myklebust 
29398f9d5b1aSChuck Lever 	switch (addr->sa_family) {
29408f9d5b1aSChuck Lever 	case AF_INET:
29418f9d5b1aSChuck Lever 		if (((struct sockaddr_in *)addr)->sin_port != htons(0))
29428f9d5b1aSChuck Lever 			xprt_set_bound(xprt);
29438f9d5b1aSChuck Lever 
29449dc3b095SChuck Lever 		xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP);
29458f9d5b1aSChuck Lever 		break;
29468f9d5b1aSChuck Lever 	case AF_INET6:
29478f9d5b1aSChuck Lever 		if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
29488f9d5b1aSChuck Lever 			xprt_set_bound(xprt);
29498f9d5b1aSChuck Lever 
29509dc3b095SChuck Lever 		xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6);
29518f9d5b1aSChuck Lever 		break;
29528f9d5b1aSChuck Lever 	default:
29530a68b0beSJ. Bruce Fields 		ret = ERR_PTR(-EAFNOSUPPORT);
29540a68b0beSJ. Bruce Fields 		goto out_err;
29558f9d5b1aSChuck Lever 	}
29568f9d5b1aSChuck Lever 
2957c740eff8SChuck Lever 	if (xprt_bound(xprt))
2958c740eff8SChuck Lever 		dprintk("RPC:       set up xprt to %s (port %s) via %s\n",
2959c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_ADDR],
2960c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_PORT],
2961c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_PROTO]);
2962c740eff8SChuck Lever 	else
2963c740eff8SChuck Lever 		dprintk("RPC:       set up xprt to %s (autobind) via %s\n",
2964c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_ADDR],
2965c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_PROTO]);
2966edb267a6SChuck Lever 
2967bc25571eS\"Talpey, Thomas\ 	if (try_module_get(THIS_MODULE))
2968c8541ecdSChuck Lever 		return xprt;
29690a68b0beSJ. Bruce Fields 	ret = ERR_PTR(-EINVAL);
29700a68b0beSJ. Bruce Fields out_err:
2971315f3812SKinglong Mee 	xs_xprt_free(xprt);
29720a68b0beSJ. Bruce Fields 	return ret;
2973a246b010SChuck Lever }
2974a246b010SChuck Lever 
29752881ae74STrond Myklebust static const struct rpc_timeout xs_tcp_default_timeout = {
29762881ae74STrond Myklebust 	.to_initval = 60 * HZ,
29772881ae74STrond Myklebust 	.to_maxval = 60 * HZ,
29782881ae74STrond Myklebust 	.to_retries = 2,
29792881ae74STrond Myklebust };
29802881ae74STrond Myklebust 
29819903cd1cSChuck Lever /**
29829903cd1cSChuck Lever  * xs_setup_tcp - Set up transport to use a TCP socket
298396802a09SFrank van Maarseveen  * @args: rpc transport creation arguments
29849903cd1cSChuck Lever  *
29859903cd1cSChuck Lever  */
2986483066d6SAdrian Bunk static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args)
2987a246b010SChuck Lever {
29888f9d5b1aSChuck Lever 	struct sockaddr *addr = args->dstaddr;
2989c8541ecdSChuck Lever 	struct rpc_xprt *xprt;
2990c8475461SChuck Lever 	struct sock_xprt *transport;
29910a68b0beSJ. Bruce Fields 	struct rpc_xprt *ret;
2992b7993cebSTrond Myklebust 	unsigned int max_slot_table_size = xprt_max_tcp_slot_table_entries;
2993b7993cebSTrond Myklebust 
2994b7993cebSTrond Myklebust 	if (args->flags & XPRT_CREATE_INFINITE_SLOTS)
2995b7993cebSTrond Myklebust 		max_slot_table_size = RPC_MAX_SLOT_TABLE_LIMIT;
2996a246b010SChuck Lever 
2997d9ba131dSTrond Myklebust 	xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
2998b7993cebSTrond Myklebust 			max_slot_table_size);
2999c8541ecdSChuck Lever 	if (IS_ERR(xprt))
3000c8541ecdSChuck Lever 		return xprt;
3001c8475461SChuck Lever 	transport = container_of(xprt, struct sock_xprt, xprt);
3002a246b010SChuck Lever 
3003ec739ef0SChuck Lever 	xprt->prot = IPPROTO_TCP;
3004d3abc739SOlga Kornievskaia 	xprt->xprt_class = &xs_tcp_transport;
3005808012fbSChuck Lever 	xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
3006a246b010SChuck Lever 
300703bf4b70SChuck Lever 	xprt->bind_timeout = XS_BIND_TO;
300803bf4b70SChuck Lever 	xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
300903bf4b70SChuck Lever 	xprt->idle_timeout = XS_IDLE_DISC_TO;
3010a246b010SChuck Lever 
3011262965f5SChuck Lever 	xprt->ops = &xs_tcp_ops;
3012ba7392bbSTrond Myklebust 	xprt->timeout = &xs_tcp_default_timeout;
3013a246b010SChuck Lever 
30143851f1cdSTrond Myklebust 	xprt->max_reconnect_timeout = xprt->timeout->to_maxval;
30157196dbb0STrond Myklebust 	xprt->connect_timeout = xprt->timeout->to_initval *
30167196dbb0STrond Myklebust 		(xprt->timeout->to_retries + 1);
30173851f1cdSTrond Myklebust 
3018c50b8ee0STrond Myklebust 	INIT_WORK(&transport->recv_worker, xs_stream_data_receive_workfn);
30194f8943f8STrond Myklebust 	INIT_WORK(&transport->error_worker, xs_error_handle);
3020edc1b01cSTrond Myklebust 	INIT_DELAYED_WORK(&transport->connect_worker, xs_tcp_setup_socket);
3021edc1b01cSTrond Myklebust 
30228f9d5b1aSChuck Lever 	switch (addr->sa_family) {
30238f9d5b1aSChuck Lever 	case AF_INET:
30248f9d5b1aSChuck Lever 		if (((struct sockaddr_in *)addr)->sin_port != htons(0))
30258f9d5b1aSChuck Lever 			xprt_set_bound(xprt);
30268f9d5b1aSChuck Lever 
30279dc3b095SChuck Lever 		xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP);
30288f9d5b1aSChuck Lever 		break;
30298f9d5b1aSChuck Lever 	case AF_INET6:
30308f9d5b1aSChuck Lever 		if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
30318f9d5b1aSChuck Lever 			xprt_set_bound(xprt);
30328f9d5b1aSChuck Lever 
30339dc3b095SChuck Lever 		xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6);
30348f9d5b1aSChuck Lever 		break;
30358f9d5b1aSChuck Lever 	default:
30360a68b0beSJ. Bruce Fields 		ret = ERR_PTR(-EAFNOSUPPORT);
30370a68b0beSJ. Bruce Fields 		goto out_err;
30388f9d5b1aSChuck Lever 	}
30398f9d5b1aSChuck Lever 
3040c740eff8SChuck Lever 	if (xprt_bound(xprt))
3041c740eff8SChuck Lever 		dprintk("RPC:       set up xprt to %s (port %s) via %s\n",
3042c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_ADDR],
3043c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_PORT],
3044c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_PROTO]);
3045c740eff8SChuck Lever 	else
3046c740eff8SChuck Lever 		dprintk("RPC:       set up xprt to %s (autobind) via %s\n",
3047c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_ADDR],
3048c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_PROTO]);
3049c740eff8SChuck Lever 
3050bc25571eS\"Talpey, Thomas\ 	if (try_module_get(THIS_MODULE))
3051c8541ecdSChuck Lever 		return xprt;
30520a68b0beSJ. Bruce Fields 	ret = ERR_PTR(-EINVAL);
30530a68b0beSJ. Bruce Fields out_err:
3054315f3812SKinglong Mee 	xs_xprt_free(xprt);
30550a68b0beSJ. Bruce Fields 	return ret;
3056a246b010SChuck Lever }
3057282b32e1SChuck Lever 
3058f300babaSAlexandros Batsakis /**
3059f300babaSAlexandros Batsakis  * xs_setup_bc_tcp - Set up transport to use a TCP backchannel socket
3060f300babaSAlexandros Batsakis  * @args: rpc transport creation arguments
3061f300babaSAlexandros Batsakis  *
3062f300babaSAlexandros Batsakis  */
3063f300babaSAlexandros Batsakis static struct rpc_xprt *xs_setup_bc_tcp(struct xprt_create *args)
3064f300babaSAlexandros Batsakis {
3065f300babaSAlexandros Batsakis 	struct sockaddr *addr = args->dstaddr;
3066f300babaSAlexandros Batsakis 	struct rpc_xprt *xprt;
3067f300babaSAlexandros Batsakis 	struct sock_xprt *transport;
3068f300babaSAlexandros Batsakis 	struct svc_sock *bc_sock;
30690a68b0beSJ. Bruce Fields 	struct rpc_xprt *ret;
3070f300babaSAlexandros Batsakis 
3071d9ba131dSTrond Myklebust 	xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
3072d9ba131dSTrond Myklebust 			xprt_tcp_slot_table_entries);
3073f300babaSAlexandros Batsakis 	if (IS_ERR(xprt))
3074f300babaSAlexandros Batsakis 		return xprt;
3075f300babaSAlexandros Batsakis 	transport = container_of(xprt, struct sock_xprt, xprt);
3076f300babaSAlexandros Batsakis 
3077f300babaSAlexandros Batsakis 	xprt->prot = IPPROTO_TCP;
3078d3abc739SOlga Kornievskaia 	xprt->xprt_class = &xs_bc_tcp_transport;
3079f300babaSAlexandros Batsakis 	xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
3080f300babaSAlexandros Batsakis 	xprt->timeout = &xs_tcp_default_timeout;
3081f300babaSAlexandros Batsakis 
3082f300babaSAlexandros Batsakis 	/* backchannel */
3083f300babaSAlexandros Batsakis 	xprt_set_bound(xprt);
3084f300babaSAlexandros Batsakis 	xprt->bind_timeout = 0;
3085f300babaSAlexandros Batsakis 	xprt->reestablish_timeout = 0;
3086f300babaSAlexandros Batsakis 	xprt->idle_timeout = 0;
3087f300babaSAlexandros Batsakis 
3088f300babaSAlexandros Batsakis 	xprt->ops = &bc_tcp_ops;
3089f300babaSAlexandros Batsakis 
3090f300babaSAlexandros Batsakis 	switch (addr->sa_family) {
3091f300babaSAlexandros Batsakis 	case AF_INET:
3092f300babaSAlexandros Batsakis 		xs_format_peer_addresses(xprt, "tcp",
3093f300babaSAlexandros Batsakis 					 RPCBIND_NETID_TCP);
3094f300babaSAlexandros Batsakis 		break;
3095f300babaSAlexandros Batsakis 	case AF_INET6:
3096f300babaSAlexandros Batsakis 		xs_format_peer_addresses(xprt, "tcp",
3097f300babaSAlexandros Batsakis 				   RPCBIND_NETID_TCP6);
3098f300babaSAlexandros Batsakis 		break;
3099f300babaSAlexandros Batsakis 	default:
31000a68b0beSJ. Bruce Fields 		ret = ERR_PTR(-EAFNOSUPPORT);
31010a68b0beSJ. Bruce Fields 		goto out_err;
3102f300babaSAlexandros Batsakis 	}
3103f300babaSAlexandros Batsakis 
3104f300babaSAlexandros Batsakis 	dprintk("RPC:       set up xprt to %s (port %s) via %s\n",
3105f300babaSAlexandros Batsakis 			xprt->address_strings[RPC_DISPLAY_ADDR],
3106f300babaSAlexandros Batsakis 			xprt->address_strings[RPC_DISPLAY_PORT],
3107f300babaSAlexandros Batsakis 			xprt->address_strings[RPC_DISPLAY_PROTO]);
3108f300babaSAlexandros Batsakis 
3109f300babaSAlexandros Batsakis 	/*
311099de8ea9SJ. Bruce Fields 	 * Once we've associated a backchannel xprt with a connection,
311128303ca3SWeng Meiling 	 * we want to keep it around as long as the connection lasts,
311228303ca3SWeng Meiling 	 * in case we need to start using it for a backchannel again;
311328303ca3SWeng Meiling 	 * this reference won't be dropped until bc_xprt is destroyed.
311499de8ea9SJ. Bruce Fields 	 */
311599de8ea9SJ. Bruce Fields 	xprt_get(xprt);
311699de8ea9SJ. Bruce Fields 	args->bc_xprt->xpt_bc_xprt = xprt;
311799de8ea9SJ. Bruce Fields 	xprt->bc_xprt = args->bc_xprt;
311899de8ea9SJ. Bruce Fields 	bc_sock = container_of(args->bc_xprt, struct svc_sock, sk_xprt);
311999de8ea9SJ. Bruce Fields 	transport->sock = bc_sock->sk_sock;
312099de8ea9SJ. Bruce Fields 	transport->inet = bc_sock->sk_sk;
312199de8ea9SJ. Bruce Fields 
312299de8ea9SJ. Bruce Fields 	/*
3123f300babaSAlexandros Batsakis 	 * Since we don't want connections for the backchannel, we set
3124f300babaSAlexandros Batsakis 	 * the xprt status to connected
3125f300babaSAlexandros Batsakis 	 */
3126f300babaSAlexandros Batsakis 	xprt_set_connected(xprt);
3127f300babaSAlexandros Batsakis 
3128f300babaSAlexandros Batsakis 	if (try_module_get(THIS_MODULE))
3129f300babaSAlexandros Batsakis 		return xprt;
3130642aab58SKinglong Mee 
3131642aab58SKinglong Mee 	args->bc_xprt->xpt_bc_xprt = NULL;
313239a9beabSJ. Bruce Fields 	args->bc_xprt->xpt_bc_xps = NULL;
313399de8ea9SJ. Bruce Fields 	xprt_put(xprt);
31340a68b0beSJ. Bruce Fields 	ret = ERR_PTR(-EINVAL);
31350a68b0beSJ. Bruce Fields out_err:
3136315f3812SKinglong Mee 	xs_xprt_free(xprt);
31370a68b0beSJ. Bruce Fields 	return ret;
3138f300babaSAlexandros Batsakis }
3139f300babaSAlexandros Batsakis 
3140176e21eeSChuck Lever static struct xprt_class	xs_local_transport = {
3141176e21eeSChuck Lever 	.list		= LIST_HEAD_INIT(xs_local_transport.list),
3142176e21eeSChuck Lever 	.name		= "named UNIX socket",
3143176e21eeSChuck Lever 	.owner		= THIS_MODULE,
3144176e21eeSChuck Lever 	.ident		= XPRT_TRANSPORT_LOCAL,
3145176e21eeSChuck Lever 	.setup		= xs_setup_local,
3146d5aa6b22STrond Myklebust 	.netid		= { "" },
3147176e21eeSChuck Lever };
3148176e21eeSChuck Lever 
3149bc25571eS\"Talpey, Thomas\ static struct xprt_class	xs_udp_transport = {
3150bc25571eS\"Talpey, Thomas\ 	.list		= LIST_HEAD_INIT(xs_udp_transport.list),
3151bc25571eS\"Talpey, Thomas\ 	.name		= "udp",
3152bc25571eS\"Talpey, Thomas\ 	.owner		= THIS_MODULE,
3153f300babaSAlexandros Batsakis 	.ident		= XPRT_TRANSPORT_UDP,
3154bc25571eS\"Talpey, Thomas\ 	.setup		= xs_setup_udp,
3155d5aa6b22STrond Myklebust 	.netid		= { "udp", "udp6", "" },
3156bc25571eS\"Talpey, Thomas\ };
3157bc25571eS\"Talpey, Thomas\ 
3158bc25571eS\"Talpey, Thomas\ static struct xprt_class	xs_tcp_transport = {
3159bc25571eS\"Talpey, Thomas\ 	.list		= LIST_HEAD_INIT(xs_tcp_transport.list),
3160bc25571eS\"Talpey, Thomas\ 	.name		= "tcp",
3161bc25571eS\"Talpey, Thomas\ 	.owner		= THIS_MODULE,
3162f300babaSAlexandros Batsakis 	.ident		= XPRT_TRANSPORT_TCP,
3163bc25571eS\"Talpey, Thomas\ 	.setup		= xs_setup_tcp,
3164d5aa6b22STrond Myklebust 	.netid		= { "tcp", "tcp6", "" },
3165bc25571eS\"Talpey, Thomas\ };
3166bc25571eS\"Talpey, Thomas\ 
3167f300babaSAlexandros Batsakis static struct xprt_class	xs_bc_tcp_transport = {
3168f300babaSAlexandros Batsakis 	.list		= LIST_HEAD_INIT(xs_bc_tcp_transport.list),
3169f300babaSAlexandros Batsakis 	.name		= "tcp NFSv4.1 backchannel",
3170f300babaSAlexandros Batsakis 	.owner		= THIS_MODULE,
3171f300babaSAlexandros Batsakis 	.ident		= XPRT_TRANSPORT_BC_TCP,
3172f300babaSAlexandros Batsakis 	.setup		= xs_setup_bc_tcp,
3173d5aa6b22STrond Myklebust 	.netid		= { "" },
3174f300babaSAlexandros Batsakis };
3175f300babaSAlexandros Batsakis 
3176282b32e1SChuck Lever /**
3177bc25571eS\"Talpey, Thomas\  * init_socket_xprt - set up xprtsock's sysctls, register with RPC client
3178282b32e1SChuck Lever  *
3179282b32e1SChuck Lever  */
3180282b32e1SChuck Lever int init_socket_xprt(void)
3181282b32e1SChuck Lever {
31822b1bec5fSEric W. Biederman 	if (!sunrpc_table_header)
31830b4d4147SEric W. Biederman 		sunrpc_table_header = register_sysctl_table(sunrpc_table);
3184fbf76683SChuck Lever 
3185176e21eeSChuck Lever 	xprt_register_transport(&xs_local_transport);
3186bc25571eS\"Talpey, Thomas\ 	xprt_register_transport(&xs_udp_transport);
3187bc25571eS\"Talpey, Thomas\ 	xprt_register_transport(&xs_tcp_transport);
3188f300babaSAlexandros Batsakis 	xprt_register_transport(&xs_bc_tcp_transport);
3189bc25571eS\"Talpey, Thomas\ 
3190282b32e1SChuck Lever 	return 0;
3191282b32e1SChuck Lever }
3192282b32e1SChuck Lever 
3193282b32e1SChuck Lever /**
3194bc25571eS\"Talpey, Thomas\  * cleanup_socket_xprt - remove xprtsock's sysctls, unregister
3195282b32e1SChuck Lever  *
3196282b32e1SChuck Lever  */
3197282b32e1SChuck Lever void cleanup_socket_xprt(void)
3198282b32e1SChuck Lever {
3199fbf76683SChuck Lever 	if (sunrpc_table_header) {
3200fbf76683SChuck Lever 		unregister_sysctl_table(sunrpc_table_header);
3201fbf76683SChuck Lever 		sunrpc_table_header = NULL;
3202fbf76683SChuck Lever 	}
3203bc25571eS\"Talpey, Thomas\ 
3204176e21eeSChuck Lever 	xprt_unregister_transport(&xs_local_transport);
3205bc25571eS\"Talpey, Thomas\ 	xprt_unregister_transport(&xs_udp_transport);
3206bc25571eS\"Talpey, Thomas\ 	xprt_unregister_transport(&xs_tcp_transport);
3207f300babaSAlexandros Batsakis 	xprt_unregister_transport(&xs_bc_tcp_transport);
3208282b32e1SChuck Lever }
3209cbf11071STrond Myklebust 
32109bbb9e5aSRusty Russell static int param_set_portnr(const char *val, const struct kernel_param *kp)
3211cbf11071STrond Myklebust {
3212cbf11071STrond Myklebust 	return param_set_uint_minmax(val, kp,
3213cbf11071STrond Myklebust 			RPC_MIN_RESVPORT,
3214cbf11071STrond Myklebust 			RPC_MAX_RESVPORT);
3215cbf11071STrond Myklebust }
3216cbf11071STrond Myklebust 
32179c27847dSLuis R. Rodriguez static const struct kernel_param_ops param_ops_portnr = {
32189bbb9e5aSRusty Russell 	.set = param_set_portnr,
32199bbb9e5aSRusty Russell 	.get = param_get_uint,
32209bbb9e5aSRusty Russell };
32219bbb9e5aSRusty Russell 
3222cbf11071STrond Myklebust #define param_check_portnr(name, p) \
3223cbf11071STrond Myklebust 	__param_check(name, p, unsigned int);
3224cbf11071STrond Myklebust 
3225cbf11071STrond Myklebust module_param_named(min_resvport, xprt_min_resvport, portnr, 0644);
3226cbf11071STrond Myklebust module_param_named(max_resvport, xprt_max_resvport, portnr, 0644);
3227cbf11071STrond Myklebust 
32289bbb9e5aSRusty Russell static int param_set_slot_table_size(const char *val,
32299bbb9e5aSRusty Russell 				     const struct kernel_param *kp)
3230cbf11071STrond Myklebust {
3231cbf11071STrond Myklebust 	return param_set_uint_minmax(val, kp,
3232cbf11071STrond Myklebust 			RPC_MIN_SLOT_TABLE,
3233cbf11071STrond Myklebust 			RPC_MAX_SLOT_TABLE);
3234cbf11071STrond Myklebust }
3235cbf11071STrond Myklebust 
32369c27847dSLuis R. Rodriguez static const struct kernel_param_ops param_ops_slot_table_size = {
32379bbb9e5aSRusty Russell 	.set = param_set_slot_table_size,
32389bbb9e5aSRusty Russell 	.get = param_get_uint,
32399bbb9e5aSRusty Russell };
32409bbb9e5aSRusty Russell 
3241cbf11071STrond Myklebust #define param_check_slot_table_size(name, p) \
3242cbf11071STrond Myklebust 	__param_check(name, p, unsigned int);
3243cbf11071STrond Myklebust 
3244d9ba131dSTrond Myklebust static int param_set_max_slot_table_size(const char *val,
3245d9ba131dSTrond Myklebust 				     const struct kernel_param *kp)
3246d9ba131dSTrond Myklebust {
3247d9ba131dSTrond Myklebust 	return param_set_uint_minmax(val, kp,
3248d9ba131dSTrond Myklebust 			RPC_MIN_SLOT_TABLE,
3249d9ba131dSTrond Myklebust 			RPC_MAX_SLOT_TABLE_LIMIT);
3250d9ba131dSTrond Myklebust }
3251d9ba131dSTrond Myklebust 
32529c27847dSLuis R. Rodriguez static const struct kernel_param_ops param_ops_max_slot_table_size = {
3253d9ba131dSTrond Myklebust 	.set = param_set_max_slot_table_size,
3254d9ba131dSTrond Myklebust 	.get = param_get_uint,
3255d9ba131dSTrond Myklebust };
3256d9ba131dSTrond Myklebust 
3257d9ba131dSTrond Myklebust #define param_check_max_slot_table_size(name, p) \
3258d9ba131dSTrond Myklebust 	__param_check(name, p, unsigned int);
3259d9ba131dSTrond Myklebust 
3260cbf11071STrond Myklebust module_param_named(tcp_slot_table_entries, xprt_tcp_slot_table_entries,
3261cbf11071STrond Myklebust 		   slot_table_size, 0644);
3262d9ba131dSTrond Myklebust module_param_named(tcp_max_slot_table_entries, xprt_max_tcp_slot_table_entries,
3263d9ba131dSTrond Myklebust 		   max_slot_table_size, 0644);
3264cbf11071STrond Myklebust module_param_named(udp_slot_table_entries, xprt_udp_slot_table_entries,
3265cbf11071STrond Myklebust 		   slot_table_size, 0644);
3266