xref: /openbmc/linux/net/sunrpc/xprtsock.c (revision 943d045a6d796175e5d08f9973953b1d2c07d797)
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 
5540e0b090SPeilin Ye #include <trace/events/sock.h>
5640b5ea0cSTrond Myklebust #include <trace/events/sunrpc.h>
5740b5ea0cSTrond Myklebust 
589e55eef4SChuck Lever #include "socklib.h"
594cfc7e60SRahul Iyer #include "sunrpc.h"
60176e21eeSChuck Lever 
61176e21eeSChuck Lever static void xs_close(struct rpc_xprt *xprt);
623b21f757STrond Myklebust static void xs_set_srcport(struct sock_xprt *transport, struct socket *sock);
637196dbb0STrond Myklebust static void xs_tcp_set_socket_timeouts(struct rpc_xprt *xprt,
647196dbb0STrond Myklebust 		struct socket *sock);
65176e21eeSChuck Lever 
669903cd1cSChuck Lever /*
67c556b754SChuck Lever  * xprtsock tunables
68c556b754SChuck Lever  */
6909acfea5STrond Myklebust static unsigned int xprt_udp_slot_table_entries = RPC_DEF_SLOT_TABLE;
7009acfea5STrond Myklebust static unsigned int xprt_tcp_slot_table_entries = RPC_MIN_SLOT_TABLE;
7109acfea5STrond Myklebust static unsigned int xprt_max_tcp_slot_table_entries = RPC_MAX_SLOT_TABLE;
72c556b754SChuck Lever 
7309acfea5STrond Myklebust static unsigned int xprt_min_resvport = RPC_DEF_MIN_RESVPORT;
7409acfea5STrond Myklebust static unsigned int xprt_max_resvport = RPC_DEF_MAX_RESVPORT;
75c556b754SChuck Lever 
767d1e8255STrond Myklebust #define XS_TCP_LINGER_TO	(15U * HZ)
7725fe6142STrond Myklebust static unsigned int xs_tcp_fin_timeout __read_mostly = XS_TCP_LINGER_TO;
787d1e8255STrond Myklebust 
79c556b754SChuck Lever /*
80fbf76683SChuck Lever  * We can register our own files under /proc/sys/sunrpc by
81fbf76683SChuck Lever  * calling register_sysctl_table() again.  The files in that
82fbf76683SChuck Lever  * directory become the union of all files registered there.
83fbf76683SChuck Lever  *
84fbf76683SChuck Lever  * We simply need to make sure that we don't collide with
85fbf76683SChuck Lever  * someone else's file names!
86fbf76683SChuck Lever  */
87fbf76683SChuck Lever 
88fbf76683SChuck Lever static unsigned int min_slot_table_size = RPC_MIN_SLOT_TABLE;
89fbf76683SChuck Lever static unsigned int max_slot_table_size = RPC_MAX_SLOT_TABLE;
90d9ba131dSTrond Myklebust static unsigned int max_tcp_slot_table_limit = RPC_MAX_SLOT_TABLE_LIMIT;
91fbf76683SChuck Lever static unsigned int xprt_min_resvport_limit = RPC_MIN_RESVPORT;
92fbf76683SChuck Lever static unsigned int xprt_max_resvport_limit = RPC_MAX_RESVPORT;
93fbf76683SChuck Lever 
94fbf76683SChuck Lever static struct ctl_table_header *sunrpc_table_header;
95fbf76683SChuck Lever 
96d3abc739SOlga Kornievskaia static struct xprt_class xs_local_transport;
97d3abc739SOlga Kornievskaia static struct xprt_class xs_udp_transport;
98d3abc739SOlga Kornievskaia static struct xprt_class xs_tcp_transport;
99d3abc739SOlga Kornievskaia static struct xprt_class xs_bc_tcp_transport;
100d3abc739SOlga Kornievskaia 
101fbf76683SChuck Lever /*
102fbf76683SChuck Lever  * FIXME: changing the UDP slot table size should also resize the UDP
103fbf76683SChuck Lever  *        socket buffers for existing UDP transports
104fbf76683SChuck Lever  */
105fe2c6338SJoe Perches static struct ctl_table xs_tunables_table[] = {
106fbf76683SChuck Lever 	{
107fbf76683SChuck Lever 		.procname	= "udp_slot_table_entries",
108fbf76683SChuck Lever 		.data		= &xprt_udp_slot_table_entries,
109fbf76683SChuck Lever 		.maxlen		= sizeof(unsigned int),
110fbf76683SChuck Lever 		.mode		= 0644,
1116d456111SEric W. Biederman 		.proc_handler	= proc_dointvec_minmax,
112fbf76683SChuck Lever 		.extra1		= &min_slot_table_size,
113fbf76683SChuck Lever 		.extra2		= &max_slot_table_size
114fbf76683SChuck Lever 	},
115fbf76683SChuck Lever 	{
116fbf76683SChuck Lever 		.procname	= "tcp_slot_table_entries",
117fbf76683SChuck Lever 		.data		= &xprt_tcp_slot_table_entries,
118fbf76683SChuck Lever 		.maxlen		= sizeof(unsigned int),
119fbf76683SChuck Lever 		.mode		= 0644,
1206d456111SEric W. Biederman 		.proc_handler	= proc_dointvec_minmax,
121fbf76683SChuck Lever 		.extra1		= &min_slot_table_size,
122fbf76683SChuck Lever 		.extra2		= &max_slot_table_size
123fbf76683SChuck Lever 	},
124fbf76683SChuck Lever 	{
125d9ba131dSTrond Myklebust 		.procname	= "tcp_max_slot_table_entries",
126d9ba131dSTrond Myklebust 		.data		= &xprt_max_tcp_slot_table_entries,
127d9ba131dSTrond Myklebust 		.maxlen		= sizeof(unsigned int),
128d9ba131dSTrond Myklebust 		.mode		= 0644,
129d9ba131dSTrond Myklebust 		.proc_handler	= proc_dointvec_minmax,
130d9ba131dSTrond Myklebust 		.extra1		= &min_slot_table_size,
131d9ba131dSTrond Myklebust 		.extra2		= &max_tcp_slot_table_limit
132d9ba131dSTrond Myklebust 	},
133d9ba131dSTrond Myklebust 	{
134fbf76683SChuck Lever 		.procname	= "min_resvport",
135fbf76683SChuck Lever 		.data		= &xprt_min_resvport,
136fbf76683SChuck Lever 		.maxlen		= sizeof(unsigned int),
137fbf76683SChuck Lever 		.mode		= 0644,
1386d456111SEric W. Biederman 		.proc_handler	= proc_dointvec_minmax,
139fbf76683SChuck Lever 		.extra1		= &xprt_min_resvport_limit,
140826799e6SJ. Bruce Fields 		.extra2		= &xprt_max_resvport_limit
141fbf76683SChuck Lever 	},
142fbf76683SChuck Lever 	{
143fbf76683SChuck Lever 		.procname	= "max_resvport",
144fbf76683SChuck Lever 		.data		= &xprt_max_resvport,
145fbf76683SChuck Lever 		.maxlen		= sizeof(unsigned int),
146fbf76683SChuck Lever 		.mode		= 0644,
1476d456111SEric W. Biederman 		.proc_handler	= proc_dointvec_minmax,
148826799e6SJ. Bruce Fields 		.extra1		= &xprt_min_resvport_limit,
149fbf76683SChuck Lever 		.extra2		= &xprt_max_resvport_limit
150fbf76683SChuck Lever 	},
151fbf76683SChuck Lever 	{
15225fe6142STrond Myklebust 		.procname	= "tcp_fin_timeout",
15325fe6142STrond Myklebust 		.data		= &xs_tcp_fin_timeout,
15425fe6142STrond Myklebust 		.maxlen		= sizeof(xs_tcp_fin_timeout),
15525fe6142STrond Myklebust 		.mode		= 0644,
1566d456111SEric W. Biederman 		.proc_handler	= proc_dointvec_jiffies,
15725fe6142STrond Myklebust 	},
158f8572d8fSEric W. Biederman 	{ },
159fbf76683SChuck Lever };
160fbf76683SChuck Lever 
161fe2c6338SJoe Perches static struct ctl_table sunrpc_table[] = {
162fbf76683SChuck Lever 	{
163fbf76683SChuck Lever 		.procname	= "sunrpc",
164fbf76683SChuck Lever 		.mode		= 0555,
165fbf76683SChuck Lever 		.child		= xs_tunables_table
166fbf76683SChuck Lever 	},
167f8572d8fSEric W. Biederman 	{ },
168fbf76683SChuck Lever };
169fbf76683SChuck Lever 
170fbf76683SChuck Lever /*
17103bf4b70SChuck Lever  * Wait duration for a reply from the RPC portmapper.
17203bf4b70SChuck Lever  */
17303bf4b70SChuck Lever #define XS_BIND_TO		(60U * HZ)
17403bf4b70SChuck Lever 
17503bf4b70SChuck Lever /*
17603bf4b70SChuck Lever  * Delay if a UDP socket connect error occurs.  This is most likely some
17703bf4b70SChuck Lever  * kind of resource problem on the local host.
17803bf4b70SChuck Lever  */
17903bf4b70SChuck Lever #define XS_UDP_REEST_TO		(2U * HZ)
18003bf4b70SChuck Lever 
18103bf4b70SChuck Lever /*
18203bf4b70SChuck Lever  * The reestablish timeout allows clients to delay for a bit before attempting
18303bf4b70SChuck Lever  * to reconnect to a server that just dropped our connection.
18403bf4b70SChuck Lever  *
18503bf4b70SChuck Lever  * We implement an exponential backoff when trying to reestablish a TCP
18603bf4b70SChuck Lever  * transport connection with the server.  Some servers like to drop a TCP
18703bf4b70SChuck Lever  * connection when they are overworked, so we start with a short timeout and
18803bf4b70SChuck Lever  * increase over time if the server is down or not responding.
18903bf4b70SChuck Lever  */
19003bf4b70SChuck Lever #define XS_TCP_INIT_REEST_TO	(3U * HZ)
19103bf4b70SChuck Lever 
19203bf4b70SChuck Lever /*
19303bf4b70SChuck Lever  * TCP idle timeout; client drops the transport socket if it is idle
19403bf4b70SChuck Lever  * for this long.  Note that we also timeout UDP sockets to prevent
19503bf4b70SChuck Lever  * holding port numbers when there is no RPC traffic.
19603bf4b70SChuck Lever  */
19703bf4b70SChuck Lever #define XS_IDLE_DISC_TO		(5U * 60 * HZ)
19803bf4b70SChuck Lever 
199f895b252SJeff Layton #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
200a246b010SChuck Lever # undef  RPC_DEBUG_DATA
2019903cd1cSChuck Lever # define RPCDBG_FACILITY	RPCDBG_TRANS
202a246b010SChuck Lever #endif
203a246b010SChuck Lever 
204a246b010SChuck Lever #ifdef RPC_DEBUG_DATA
2059903cd1cSChuck Lever static void xs_pktdump(char *msg, u32 *packet, unsigned int count)
206a246b010SChuck Lever {
207a246b010SChuck Lever 	u8 *buf = (u8 *) packet;
208a246b010SChuck Lever 	int j;
209a246b010SChuck Lever 
210a246b010SChuck Lever 	dprintk("RPC:       %s\n", msg);
211a246b010SChuck Lever 	for (j = 0; j < count && j < 128; j += 4) {
212a246b010SChuck Lever 		if (!(j & 31)) {
213a246b010SChuck Lever 			if (j)
214a246b010SChuck Lever 				dprintk("\n");
215a246b010SChuck Lever 			dprintk("0x%04x ", j);
216a246b010SChuck Lever 		}
217a246b010SChuck Lever 		dprintk("%02x%02x%02x%02x ",
218a246b010SChuck Lever 			buf[j], buf[j+1], buf[j+2], buf[j+3]);
219a246b010SChuck Lever 	}
220a246b010SChuck Lever 	dprintk("\n");
221a246b010SChuck Lever }
222a246b010SChuck Lever #else
2239903cd1cSChuck Lever static inline void xs_pktdump(char *msg, u32 *packet, unsigned int count)
224a246b010SChuck Lever {
225a246b010SChuck Lever 	/* NOP */
226a246b010SChuck Lever }
227a246b010SChuck Lever #endif
228a246b010SChuck Lever 
2292118071dSTrond Myklebust static inline struct rpc_xprt *xprt_from_sock(struct sock *sk)
2302118071dSTrond Myklebust {
2312118071dSTrond Myklebust 	return (struct rpc_xprt *) sk->sk_user_data;
2322118071dSTrond Myklebust }
2332118071dSTrond Myklebust 
23495392c59SChuck Lever static inline struct sockaddr *xs_addr(struct rpc_xprt *xprt)
235edb267a6SChuck Lever {
23695392c59SChuck Lever 	return (struct sockaddr *) &xprt->addr;
23795392c59SChuck Lever }
23895392c59SChuck Lever 
239176e21eeSChuck Lever static inline struct sockaddr_un *xs_addr_un(struct rpc_xprt *xprt)
240176e21eeSChuck Lever {
241176e21eeSChuck Lever 	return (struct sockaddr_un *) &xprt->addr;
242176e21eeSChuck Lever }
243176e21eeSChuck Lever 
24495392c59SChuck Lever static inline struct sockaddr_in *xs_addr_in(struct rpc_xprt *xprt)
24595392c59SChuck Lever {
24695392c59SChuck Lever 	return (struct sockaddr_in *) &xprt->addr;
24795392c59SChuck Lever }
24895392c59SChuck Lever 
24995392c59SChuck Lever static inline struct sockaddr_in6 *xs_addr_in6(struct rpc_xprt *xprt)
25095392c59SChuck Lever {
25195392c59SChuck Lever 	return (struct sockaddr_in6 *) &xprt->addr;
25295392c59SChuck Lever }
25395392c59SChuck Lever 
254c877b849SChuck Lever static void xs_format_common_peer_addresses(struct rpc_xprt *xprt)
255c877b849SChuck Lever {
256c877b849SChuck Lever 	struct sockaddr *sap = xs_addr(xprt);
2579dc3b095SChuck Lever 	struct sockaddr_in6 *sin6;
2589dc3b095SChuck Lever 	struct sockaddr_in *sin;
259176e21eeSChuck Lever 	struct sockaddr_un *sun;
260c877b849SChuck Lever 	char buf[128];
261c877b849SChuck Lever 
2629dc3b095SChuck Lever 	switch (sap->sa_family) {
263176e21eeSChuck Lever 	case AF_LOCAL:
264176e21eeSChuck Lever 		sun = xs_addr_un(xprt);
26515bcdc92SWolfram Sang 		strscpy(buf, sun->sun_path, sizeof(buf));
266176e21eeSChuck Lever 		xprt->address_strings[RPC_DISPLAY_ADDR] =
267176e21eeSChuck Lever 						kstrdup(buf, GFP_KERNEL);
268176e21eeSChuck Lever 		break;
2699dc3b095SChuck Lever 	case AF_INET:
270176e21eeSChuck Lever 		(void)rpc_ntop(sap, buf, sizeof(buf));
271176e21eeSChuck Lever 		xprt->address_strings[RPC_DISPLAY_ADDR] =
272176e21eeSChuck Lever 						kstrdup(buf, GFP_KERNEL);
2739dc3b095SChuck Lever 		sin = xs_addr_in(xprt);
274fc0b5791SJoe Perches 		snprintf(buf, sizeof(buf), "%08x", ntohl(sin->sin_addr.s_addr));
2759dc3b095SChuck Lever 		break;
2769dc3b095SChuck Lever 	case AF_INET6:
277176e21eeSChuck Lever 		(void)rpc_ntop(sap, buf, sizeof(buf));
278176e21eeSChuck Lever 		xprt->address_strings[RPC_DISPLAY_ADDR] =
279176e21eeSChuck Lever 						kstrdup(buf, GFP_KERNEL);
2809dc3b095SChuck Lever 		sin6 = xs_addr_in6(xprt);
281fc0b5791SJoe Perches 		snprintf(buf, sizeof(buf), "%pi6", &sin6->sin6_addr);
2829dc3b095SChuck Lever 		break;
2839dc3b095SChuck Lever 	default:
2849dc3b095SChuck Lever 		BUG();
2859dc3b095SChuck Lever 	}
286176e21eeSChuck Lever 
2879dc3b095SChuck Lever 	xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = kstrdup(buf, GFP_KERNEL);
2889dc3b095SChuck Lever }
2899dc3b095SChuck Lever 
2909dc3b095SChuck Lever static void xs_format_common_peer_ports(struct rpc_xprt *xprt)
2919dc3b095SChuck Lever {
2929dc3b095SChuck Lever 	struct sockaddr *sap = xs_addr(xprt);
2939dc3b095SChuck Lever 	char buf[128];
2949dc3b095SChuck Lever 
29581160e66SJoe Perches 	snprintf(buf, sizeof(buf), "%u", rpc_get_port(sap));
296c877b849SChuck Lever 	xprt->address_strings[RPC_DISPLAY_PORT] = kstrdup(buf, GFP_KERNEL);
297c877b849SChuck Lever 
29881160e66SJoe Perches 	snprintf(buf, sizeof(buf), "%4hx", rpc_get_port(sap));
299c877b849SChuck Lever 	xprt->address_strings[RPC_DISPLAY_HEX_PORT] = kstrdup(buf, GFP_KERNEL);
300c877b849SChuck Lever }
301c877b849SChuck Lever 
3029dc3b095SChuck Lever static void xs_format_peer_addresses(struct rpc_xprt *xprt,
303b454ae90SChuck Lever 				     const char *protocol,
304b454ae90SChuck Lever 				     const char *netid)
305edb267a6SChuck Lever {
306b454ae90SChuck Lever 	xprt->address_strings[RPC_DISPLAY_PROTO] = protocol;
307b454ae90SChuck Lever 	xprt->address_strings[RPC_DISPLAY_NETID] = netid;
308c877b849SChuck Lever 	xs_format_common_peer_addresses(xprt);
3099dc3b095SChuck Lever 	xs_format_common_peer_ports(xprt);
310edb267a6SChuck Lever }
311edb267a6SChuck Lever 
3129dc3b095SChuck Lever static void xs_update_peer_port(struct rpc_xprt *xprt)
3134b6473fbSChuck Lever {
3149dc3b095SChuck Lever 	kfree(xprt->address_strings[RPC_DISPLAY_HEX_PORT]);
3159dc3b095SChuck Lever 	kfree(xprt->address_strings[RPC_DISPLAY_PORT]);
3164b6473fbSChuck Lever 
3179dc3b095SChuck Lever 	xs_format_common_peer_ports(xprt);
318edb267a6SChuck Lever }
319edb267a6SChuck Lever 
320edb267a6SChuck Lever static void xs_free_peer_addresses(struct rpc_xprt *xprt)
321edb267a6SChuck Lever {
32233e01dc7SChuck Lever 	unsigned int i;
32333e01dc7SChuck Lever 
32433e01dc7SChuck Lever 	for (i = 0; i < RPC_DISPLAY_MAX; i++)
32533e01dc7SChuck Lever 		switch (i) {
32633e01dc7SChuck Lever 		case RPC_DISPLAY_PROTO:
32733e01dc7SChuck Lever 		case RPC_DISPLAY_NETID:
32833e01dc7SChuck Lever 			continue;
32933e01dc7SChuck Lever 		default:
33033e01dc7SChuck Lever 			kfree(xprt->address_strings[i]);
33133e01dc7SChuck Lever 		}
332edb267a6SChuck Lever }
333edb267a6SChuck Lever 
334277e4ab7STrond Myklebust static size_t
335277e4ab7STrond Myklebust xs_alloc_sparse_pages(struct xdr_buf *buf, size_t want, gfp_t gfp)
336277e4ab7STrond Myklebust {
337277e4ab7STrond Myklebust 	size_t i,n;
338277e4ab7STrond Myklebust 
33916e5e90fSTrond Myklebust 	if (!want || !(buf->flags & XDRBUF_SPARSE_PAGES))
340277e4ab7STrond Myklebust 		return want;
341277e4ab7STrond Myklebust 	n = (buf->page_base + want + PAGE_SIZE - 1) >> PAGE_SHIFT;
342277e4ab7STrond Myklebust 	for (i = 0; i < n; i++) {
343277e4ab7STrond Myklebust 		if (buf->pages[i])
344277e4ab7STrond Myklebust 			continue;
345277e4ab7STrond Myklebust 		buf->bvec[i].bv_page = buf->pages[i] = alloc_page(gfp);
346277e4ab7STrond Myklebust 		if (!buf->pages[i]) {
34716e5e90fSTrond Myklebust 			i *= PAGE_SIZE;
34816e5e90fSTrond Myklebust 			return i > buf->page_base ? i - buf->page_base : 0;
349277e4ab7STrond Myklebust 		}
350277e4ab7STrond Myklebust 	}
351277e4ab7STrond Myklebust 	return want;
352277e4ab7STrond Myklebust }
353277e4ab7STrond Myklebust 
354277e4ab7STrond Myklebust static ssize_t
355277e4ab7STrond Myklebust xs_sock_recvmsg(struct socket *sock, struct msghdr *msg, int flags, size_t seek)
356277e4ab7STrond Myklebust {
357277e4ab7STrond Myklebust 	ssize_t ret;
358277e4ab7STrond Myklebust 	if (seek != 0)
359277e4ab7STrond Myklebust 		iov_iter_advance(&msg->msg_iter, seek);
360277e4ab7STrond Myklebust 	ret = sock_recvmsg(sock, msg, flags);
361277e4ab7STrond Myklebust 	return ret > 0 ? ret + seek : ret;
362277e4ab7STrond Myklebust }
363277e4ab7STrond Myklebust 
364277e4ab7STrond Myklebust static ssize_t
365277e4ab7STrond Myklebust xs_read_kvec(struct socket *sock, struct msghdr *msg, int flags,
366277e4ab7STrond Myklebust 		struct kvec *kvec, size_t count, size_t seek)
367277e4ab7STrond Myklebust {
368de4eda9dSAl Viro 	iov_iter_kvec(&msg->msg_iter, ITER_DEST, kvec, 1, count);
369277e4ab7STrond Myklebust 	return xs_sock_recvmsg(sock, msg, flags, seek);
370277e4ab7STrond Myklebust }
371277e4ab7STrond Myklebust 
372277e4ab7STrond Myklebust static ssize_t
373277e4ab7STrond Myklebust xs_read_bvec(struct socket *sock, struct msghdr *msg, int flags,
374277e4ab7STrond Myklebust 		struct bio_vec *bvec, unsigned long nr, size_t count,
375277e4ab7STrond Myklebust 		size_t seek)
376277e4ab7STrond Myklebust {
377de4eda9dSAl Viro 	iov_iter_bvec(&msg->msg_iter, ITER_DEST, bvec, nr, count);
378277e4ab7STrond Myklebust 	return xs_sock_recvmsg(sock, msg, flags, seek);
379277e4ab7STrond Myklebust }
380277e4ab7STrond Myklebust 
381277e4ab7STrond Myklebust static ssize_t
382277e4ab7STrond Myklebust xs_read_discard(struct socket *sock, struct msghdr *msg, int flags,
383277e4ab7STrond Myklebust 		size_t count)
384277e4ab7STrond Myklebust {
385de4eda9dSAl Viro 	iov_iter_discard(&msg->msg_iter, ITER_DEST, count);
386b76a5afdSTrond Myklebust 	return sock_recvmsg(sock, msg, flags);
387277e4ab7STrond Myklebust }
388277e4ab7STrond Myklebust 
3896a829eb8STrond Myklebust #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
3906a829eb8STrond Myklebust static void
3916a829eb8STrond Myklebust xs_flush_bvec(const struct bio_vec *bvec, size_t count, size_t seek)
3926a829eb8STrond Myklebust {
3936a829eb8STrond Myklebust 	struct bvec_iter bi = {
3946a829eb8STrond Myklebust 		.bi_size = count,
3956a829eb8STrond Myklebust 	};
3966a829eb8STrond Myklebust 	struct bio_vec bv;
3976a829eb8STrond Myklebust 
3986a829eb8STrond Myklebust 	bvec_iter_advance(bvec, &bi, seek & PAGE_MASK);
3996a829eb8STrond Myklebust 	for_each_bvec(bv, bvec, bi, bi)
4006a829eb8STrond Myklebust 		flush_dcache_page(bv.bv_page);
4016a829eb8STrond Myklebust }
4026a829eb8STrond Myklebust #else
4036a829eb8STrond Myklebust static inline void
4046a829eb8STrond Myklebust xs_flush_bvec(const struct bio_vec *bvec, size_t count, size_t seek)
4056a829eb8STrond Myklebust {
4066a829eb8STrond Myklebust }
4076a829eb8STrond Myklebust #endif
4086a829eb8STrond Myklebust 
409277e4ab7STrond Myklebust static ssize_t
410277e4ab7STrond Myklebust xs_read_xdr_buf(struct socket *sock, struct msghdr *msg, int flags,
411277e4ab7STrond Myklebust 		struct xdr_buf *buf, size_t count, size_t seek, size_t *read)
412277e4ab7STrond Myklebust {
413277e4ab7STrond Myklebust 	size_t want, seek_init = seek, offset = 0;
414277e4ab7STrond Myklebust 	ssize_t ret;
415277e4ab7STrond Myklebust 
416277e4ab7STrond Myklebust 	want = min_t(size_t, count, buf->head[0].iov_len);
417e92053a5STrond Myklebust 	if (seek < want) {
418277e4ab7STrond Myklebust 		ret = xs_read_kvec(sock, msg, flags, &buf->head[0], want, seek);
419277e4ab7STrond Myklebust 		if (ret <= 0)
420277e4ab7STrond Myklebust 			goto sock_err;
421277e4ab7STrond Myklebust 		offset += ret;
422277e4ab7STrond Myklebust 		if (offset == count || msg->msg_flags & (MSG_EOR|MSG_TRUNC))
423277e4ab7STrond Myklebust 			goto out;
424277e4ab7STrond Myklebust 		if (ret != want)
425c4433055STrond Myklebust 			goto out;
426277e4ab7STrond Myklebust 		seek = 0;
427277e4ab7STrond Myklebust 	} else {
428e92053a5STrond Myklebust 		seek -= want;
429e92053a5STrond Myklebust 		offset += want;
430277e4ab7STrond Myklebust 	}
43116e5e90fSTrond Myklebust 
432b2648015STrond Myklebust 	want = xs_alloc_sparse_pages(
433b2648015STrond Myklebust 		buf, min_t(size_t, count - offset, buf->page_len),
434b2648015STrond Myklebust 		GFP_KERNEL | __GFP_NORETRY | __GFP_NOWARN);
43516e5e90fSTrond Myklebust 	if (seek < want) {
436277e4ab7STrond Myklebust 		ret = xs_read_bvec(sock, msg, flags, buf->bvec,
437277e4ab7STrond Myklebust 				xdr_buf_pagecount(buf),
438277e4ab7STrond Myklebust 				want + buf->page_base,
439277e4ab7STrond Myklebust 				seek + buf->page_base);
440277e4ab7STrond Myklebust 		if (ret <= 0)
441277e4ab7STrond Myklebust 			goto sock_err;
4426a829eb8STrond Myklebust 		xs_flush_bvec(buf->bvec, ret, seek + buf->page_base);
443ac9645c8SDan Aloni 		ret -= buf->page_base;
444ac9645c8SDan Aloni 		offset += ret;
445277e4ab7STrond Myklebust 		if (offset == count || msg->msg_flags & (MSG_EOR|MSG_TRUNC))
446277e4ab7STrond Myklebust 			goto out;
447277e4ab7STrond Myklebust 		if (ret != want)
448c4433055STrond Myklebust 			goto out;
449277e4ab7STrond Myklebust 		seek = 0;
450277e4ab7STrond Myklebust 	} else {
45116e5e90fSTrond Myklebust 		seek -= want;
45216e5e90fSTrond Myklebust 		offset += want;
453277e4ab7STrond Myklebust 	}
45416e5e90fSTrond Myklebust 
455277e4ab7STrond Myklebust 	want = min_t(size_t, count - offset, buf->tail[0].iov_len);
456e92053a5STrond Myklebust 	if (seek < want) {
457277e4ab7STrond Myklebust 		ret = xs_read_kvec(sock, msg, flags, &buf->tail[0], want, seek);
458277e4ab7STrond Myklebust 		if (ret <= 0)
459277e4ab7STrond Myklebust 			goto sock_err;
460277e4ab7STrond Myklebust 		offset += ret;
461277e4ab7STrond Myklebust 		if (offset == count || msg->msg_flags & (MSG_EOR|MSG_TRUNC))
462277e4ab7STrond Myklebust 			goto out;
463277e4ab7STrond Myklebust 		if (ret != want)
464c4433055STrond Myklebust 			goto out;
4659734ad57STrond Myklebust 	} else if (offset < seek_init)
466e92053a5STrond Myklebust 		offset = seek_init;
467277e4ab7STrond Myklebust 	ret = -EMSGSIZE;
468277e4ab7STrond Myklebust out:
469277e4ab7STrond Myklebust 	*read = offset - seek_init;
470277e4ab7STrond Myklebust 	return ret;
471277e4ab7STrond Myklebust sock_err:
472277e4ab7STrond Myklebust 	offset += seek;
473277e4ab7STrond Myklebust 	goto out;
474277e4ab7STrond Myklebust }
475277e4ab7STrond Myklebust 
476277e4ab7STrond Myklebust static void
477277e4ab7STrond Myklebust xs_read_header(struct sock_xprt *transport, struct xdr_buf *buf)
478277e4ab7STrond Myklebust {
479277e4ab7STrond Myklebust 	if (!transport->recv.copied) {
480277e4ab7STrond Myklebust 		if (buf->head[0].iov_len >= transport->recv.offset)
481277e4ab7STrond Myklebust 			memcpy(buf->head[0].iov_base,
482277e4ab7STrond Myklebust 					&transport->recv.xid,
483277e4ab7STrond Myklebust 					transport->recv.offset);
484277e4ab7STrond Myklebust 		transport->recv.copied = transport->recv.offset;
485277e4ab7STrond Myklebust 	}
486277e4ab7STrond Myklebust }
487277e4ab7STrond Myklebust 
488277e4ab7STrond Myklebust static bool
489277e4ab7STrond Myklebust xs_read_stream_request_done(struct sock_xprt *transport)
490277e4ab7STrond Myklebust {
491277e4ab7STrond Myklebust 	return transport->recv.fraghdr & cpu_to_be32(RPC_LAST_STREAM_FRAGMENT);
492277e4ab7STrond Myklebust }
493277e4ab7STrond Myklebust 
494e92053a5STrond Myklebust static void
495e92053a5STrond Myklebust xs_read_stream_check_eor(struct sock_xprt *transport,
496e92053a5STrond Myklebust 		struct msghdr *msg)
497e92053a5STrond Myklebust {
498e92053a5STrond Myklebust 	if (xs_read_stream_request_done(transport))
499e92053a5STrond Myklebust 		msg->msg_flags |= MSG_EOR;
500e92053a5STrond Myklebust }
501e92053a5STrond Myklebust 
502277e4ab7STrond Myklebust static ssize_t
503277e4ab7STrond Myklebust xs_read_stream_request(struct sock_xprt *transport, struct msghdr *msg,
504277e4ab7STrond Myklebust 		int flags, struct rpc_rqst *req)
505277e4ab7STrond Myklebust {
506277e4ab7STrond Myklebust 	struct xdr_buf *buf = &req->rq_private_buf;
5073f649ab7SKees Cook 	size_t want, read;
5083f649ab7SKees Cook 	ssize_t ret;
509277e4ab7STrond Myklebust 
510277e4ab7STrond Myklebust 	xs_read_header(transport, buf);
511277e4ab7STrond Myklebust 
512277e4ab7STrond Myklebust 	want = transport->recv.len - transport->recv.offset;
513e92053a5STrond Myklebust 	if (want != 0) {
514277e4ab7STrond Myklebust 		ret = xs_read_xdr_buf(transport->sock, msg, flags, buf,
515e92053a5STrond Myklebust 				transport->recv.copied + want,
516e92053a5STrond Myklebust 				transport->recv.copied,
517277e4ab7STrond Myklebust 				&read);
518277e4ab7STrond Myklebust 		transport->recv.offset += read;
519277e4ab7STrond Myklebust 		transport->recv.copied += read;
520277e4ab7STrond Myklebust 	}
521277e4ab7STrond Myklebust 
522727fcc64STrond Myklebust 	if (transport->recv.offset == transport->recv.len)
523727fcc64STrond Myklebust 		xs_read_stream_check_eor(transport, msg);
524727fcc64STrond Myklebust 
525e92053a5STrond Myklebust 	if (want == 0)
526e92053a5STrond Myklebust 		return 0;
527e92053a5STrond Myklebust 
528277e4ab7STrond Myklebust 	switch (ret) {
529c4433055STrond Myklebust 	default:
530c4433055STrond Myklebust 		break;
53126781eabSTrond Myklebust 	case -EFAULT:
532277e4ab7STrond Myklebust 	case -EMSGSIZE:
53326781eabSTrond Myklebust 		msg->msg_flags |= MSG_TRUNC;
534c4433055STrond Myklebust 		return read;
535277e4ab7STrond Myklebust 	case 0:
536277e4ab7STrond Myklebust 		return -ESHUTDOWN;
537277e4ab7STrond Myklebust 	}
538c4433055STrond Myklebust 	return ret < 0 ? ret : read;
539277e4ab7STrond Myklebust }
540277e4ab7STrond Myklebust 
541277e4ab7STrond Myklebust static size_t
542277e4ab7STrond Myklebust xs_read_stream_headersize(bool isfrag)
543277e4ab7STrond Myklebust {
544277e4ab7STrond Myklebust 	if (isfrag)
545277e4ab7STrond Myklebust 		return sizeof(__be32);
546277e4ab7STrond Myklebust 	return 3 * sizeof(__be32);
547277e4ab7STrond Myklebust }
548277e4ab7STrond Myklebust 
549277e4ab7STrond Myklebust static ssize_t
550277e4ab7STrond Myklebust xs_read_stream_header(struct sock_xprt *transport, struct msghdr *msg,
551277e4ab7STrond Myklebust 		int flags, size_t want, size_t seek)
552277e4ab7STrond Myklebust {
553277e4ab7STrond Myklebust 	struct kvec kvec = {
554277e4ab7STrond Myklebust 		.iov_base = &transport->recv.fraghdr,
555277e4ab7STrond Myklebust 		.iov_len = want,
556277e4ab7STrond Myklebust 	};
557277e4ab7STrond Myklebust 	return xs_read_kvec(transport->sock, msg, flags, &kvec, want, seek);
558277e4ab7STrond Myklebust }
559277e4ab7STrond Myklebust 
560277e4ab7STrond Myklebust #if defined(CONFIG_SUNRPC_BACKCHANNEL)
561277e4ab7STrond Myklebust static ssize_t
562277e4ab7STrond Myklebust xs_read_stream_call(struct sock_xprt *transport, struct msghdr *msg, int flags)
563277e4ab7STrond Myklebust {
564277e4ab7STrond Myklebust 	struct rpc_xprt *xprt = &transport->xprt;
565277e4ab7STrond Myklebust 	struct rpc_rqst *req;
566277e4ab7STrond Myklebust 	ssize_t ret;
567277e4ab7STrond Myklebust 
56898b5cee3SBenjamin Coddington 	/* Is this transport associated with the backchannel? */
56998b5cee3SBenjamin Coddington 	if (!xprt->bc_serv)
57098b5cee3SBenjamin Coddington 		return -ESHUTDOWN;
57198b5cee3SBenjamin Coddington 
572277e4ab7STrond Myklebust 	/* Look up and lock the request corresponding to the given XID */
573277e4ab7STrond Myklebust 	req = xprt_lookup_bc_request(xprt, transport->recv.xid);
574277e4ab7STrond Myklebust 	if (!req) {
575277e4ab7STrond Myklebust 		printk(KERN_WARNING "Callback slot table overflowed\n");
576277e4ab7STrond Myklebust 		return -ESHUTDOWN;
577277e4ab7STrond Myklebust 	}
57845835a63STrond Myklebust 	if (transport->recv.copied && !req->rq_private_buf.len)
57945835a63STrond Myklebust 		return -ESHUTDOWN;
580277e4ab7STrond Myklebust 
581277e4ab7STrond Myklebust 	ret = xs_read_stream_request(transport, msg, flags, req);
582277e4ab7STrond Myklebust 	if (msg->msg_flags & (MSG_EOR|MSG_TRUNC))
583c4433055STrond Myklebust 		xprt_complete_bc_request(req, transport->recv.copied);
58445835a63STrond Myklebust 	else
58545835a63STrond Myklebust 		req->rq_private_buf.len = transport->recv.copied;
586277e4ab7STrond Myklebust 
587277e4ab7STrond Myklebust 	return ret;
588277e4ab7STrond Myklebust }
589277e4ab7STrond Myklebust #else /* CONFIG_SUNRPC_BACKCHANNEL */
590277e4ab7STrond Myklebust static ssize_t
591277e4ab7STrond Myklebust xs_read_stream_call(struct sock_xprt *transport, struct msghdr *msg, int flags)
592277e4ab7STrond Myklebust {
593277e4ab7STrond Myklebust 	return -ESHUTDOWN;
594277e4ab7STrond Myklebust }
595277e4ab7STrond Myklebust #endif /* CONFIG_SUNRPC_BACKCHANNEL */
596277e4ab7STrond Myklebust 
597277e4ab7STrond Myklebust static ssize_t
598277e4ab7STrond Myklebust xs_read_stream_reply(struct sock_xprt *transport, struct msghdr *msg, int flags)
599277e4ab7STrond Myklebust {
600277e4ab7STrond Myklebust 	struct rpc_xprt *xprt = &transport->xprt;
601277e4ab7STrond Myklebust 	struct rpc_rqst *req;
602277e4ab7STrond Myklebust 	ssize_t ret = 0;
603277e4ab7STrond Myklebust 
604277e4ab7STrond Myklebust 	/* Look up and lock the request corresponding to the given XID */
605277e4ab7STrond Myklebust 	spin_lock(&xprt->queue_lock);
606277e4ab7STrond Myklebust 	req = xprt_lookup_rqst(xprt, transport->recv.xid);
60745835a63STrond Myklebust 	if (!req || (transport->recv.copied && !req->rq_private_buf.len)) {
608277e4ab7STrond Myklebust 		msg->msg_flags |= MSG_TRUNC;
609277e4ab7STrond Myklebust 		goto out;
610277e4ab7STrond Myklebust 	}
611277e4ab7STrond Myklebust 	xprt_pin_rqst(req);
612277e4ab7STrond Myklebust 	spin_unlock(&xprt->queue_lock);
613277e4ab7STrond Myklebust 
614277e4ab7STrond Myklebust 	ret = xs_read_stream_request(transport, msg, flags, req);
615277e4ab7STrond Myklebust 
616277e4ab7STrond Myklebust 	spin_lock(&xprt->queue_lock);
617277e4ab7STrond Myklebust 	if (msg->msg_flags & (MSG_EOR|MSG_TRUNC))
618c4433055STrond Myklebust 		xprt_complete_rqst(req->rq_task, transport->recv.copied);
61945835a63STrond Myklebust 	else
62045835a63STrond Myklebust 		req->rq_private_buf.len = transport->recv.copied;
621277e4ab7STrond Myklebust 	xprt_unpin_rqst(req);
622277e4ab7STrond Myklebust out:
623277e4ab7STrond Myklebust 	spin_unlock(&xprt->queue_lock);
624277e4ab7STrond Myklebust 	return ret;
625277e4ab7STrond Myklebust }
626277e4ab7STrond Myklebust 
627277e4ab7STrond Myklebust static ssize_t
628277e4ab7STrond Myklebust xs_read_stream(struct sock_xprt *transport, int flags)
629277e4ab7STrond Myklebust {
630277e4ab7STrond Myklebust 	struct msghdr msg = { 0 };
631277e4ab7STrond Myklebust 	size_t want, read = 0;
632277e4ab7STrond Myklebust 	ssize_t ret = 0;
633277e4ab7STrond Myklebust 
634277e4ab7STrond Myklebust 	if (transport->recv.len == 0) {
635277e4ab7STrond Myklebust 		want = xs_read_stream_headersize(transport->recv.copied != 0);
636277e4ab7STrond Myklebust 		ret = xs_read_stream_header(transport, &msg, flags, want,
637277e4ab7STrond Myklebust 				transport->recv.offset);
638277e4ab7STrond Myklebust 		if (ret <= 0)
639277e4ab7STrond Myklebust 			goto out_err;
640277e4ab7STrond Myklebust 		transport->recv.offset = ret;
641c4433055STrond Myklebust 		if (transport->recv.offset != want)
642c4433055STrond Myklebust 			return transport->recv.offset;
643277e4ab7STrond Myklebust 		transport->recv.len = be32_to_cpu(transport->recv.fraghdr) &
644277e4ab7STrond Myklebust 			RPC_FRAGMENT_SIZE_MASK;
645277e4ab7STrond Myklebust 		transport->recv.offset -= sizeof(transport->recv.fraghdr);
646277e4ab7STrond Myklebust 		read = ret;
647277e4ab7STrond Myklebust 	}
648277e4ab7STrond Myklebust 
649277e4ab7STrond Myklebust 	switch (be32_to_cpu(transport->recv.calldir)) {
650c4433055STrond Myklebust 	default:
651c4433055STrond Myklebust 		msg.msg_flags |= MSG_TRUNC;
652c4433055STrond Myklebust 		break;
653277e4ab7STrond Myklebust 	case RPC_CALL:
654277e4ab7STrond Myklebust 		ret = xs_read_stream_call(transport, &msg, flags);
655277e4ab7STrond Myklebust 		break;
656277e4ab7STrond Myklebust 	case RPC_REPLY:
657277e4ab7STrond Myklebust 		ret = xs_read_stream_reply(transport, &msg, flags);
658277e4ab7STrond Myklebust 	}
659277e4ab7STrond Myklebust 	if (msg.msg_flags & MSG_TRUNC) {
660277e4ab7STrond Myklebust 		transport->recv.calldir = cpu_to_be32(-1);
661277e4ab7STrond Myklebust 		transport->recv.copied = -1;
662277e4ab7STrond Myklebust 	}
663277e4ab7STrond Myklebust 	if (ret < 0)
664277e4ab7STrond Myklebust 		goto out_err;
665277e4ab7STrond Myklebust 	read += ret;
666277e4ab7STrond Myklebust 	if (transport->recv.offset < transport->recv.len) {
667c4433055STrond Myklebust 		if (!(msg.msg_flags & MSG_TRUNC))
668c4433055STrond Myklebust 			return read;
669b76a5afdSTrond Myklebust 		msg.msg_flags = 0;
670277e4ab7STrond Myklebust 		ret = xs_read_discard(transport->sock, &msg, flags,
671277e4ab7STrond Myklebust 				transport->recv.len - transport->recv.offset);
672277e4ab7STrond Myklebust 		if (ret <= 0)
673277e4ab7STrond Myklebust 			goto out_err;
674277e4ab7STrond Myklebust 		transport->recv.offset += ret;
675277e4ab7STrond Myklebust 		read += ret;
676277e4ab7STrond Myklebust 		if (transport->recv.offset != transport->recv.len)
677c4433055STrond Myklebust 			return read;
678277e4ab7STrond Myklebust 	}
679277e4ab7STrond Myklebust 	if (xs_read_stream_request_done(transport)) {
680c50b8ee0STrond Myklebust 		trace_xs_stream_read_request(transport);
681277e4ab7STrond Myklebust 		transport->recv.copied = 0;
682277e4ab7STrond Myklebust 	}
683277e4ab7STrond Myklebust 	transport->recv.offset = 0;
684277e4ab7STrond Myklebust 	transport->recv.len = 0;
685277e4ab7STrond Myklebust 	return read;
686277e4ab7STrond Myklebust out_err:
68779462857STrond Myklebust 	return ret != 0 ? ret : -ESHUTDOWN;
688277e4ab7STrond Myklebust }
689277e4ab7STrond Myklebust 
6900ffe86f4STrond Myklebust static __poll_t xs_poll_socket(struct sock_xprt *transport)
6910ffe86f4STrond Myklebust {
692a73881c9STrond Myklebust 	return transport->sock->ops->poll(transport->file, transport->sock,
693a73881c9STrond Myklebust 			NULL);
6940ffe86f4STrond Myklebust }
6950ffe86f4STrond Myklebust 
6960ffe86f4STrond Myklebust static bool xs_poll_socket_readable(struct sock_xprt *transport)
6970ffe86f4STrond Myklebust {
6980ffe86f4STrond Myklebust 	__poll_t events = xs_poll_socket(transport);
6990ffe86f4STrond Myklebust 
7000ffe86f4STrond Myklebust 	return (events & (EPOLLIN | EPOLLRDNORM)) && !(events & EPOLLRDHUP);
7010ffe86f4STrond Myklebust }
7020ffe86f4STrond Myklebust 
7030ffe86f4STrond Myklebust static void xs_poll_check_readable(struct sock_xprt *transport)
7040ffe86f4STrond Myklebust {
7050ffe86f4STrond Myklebust 
7060ffe86f4STrond Myklebust 	clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state);
7070ffe86f4STrond Myklebust 	if (!xs_poll_socket_readable(transport))
7080ffe86f4STrond Myklebust 		return;
7090ffe86f4STrond Myklebust 	if (!test_and_set_bit(XPRT_SOCK_DATA_READY, &transport->sock_state))
7100ffe86f4STrond Myklebust 		queue_work(xprtiod_workqueue, &transport->recv_worker);
7110ffe86f4STrond Myklebust }
7120ffe86f4STrond Myklebust 
713c50b8ee0STrond Myklebust static void xs_stream_data_receive(struct sock_xprt *transport)
714c50b8ee0STrond Myklebust {
715c50b8ee0STrond Myklebust 	size_t read = 0;
716c50b8ee0STrond Myklebust 	ssize_t ret = 0;
717c50b8ee0STrond Myklebust 
718c50b8ee0STrond Myklebust 	mutex_lock(&transport->recv_mutex);
719c50b8ee0STrond Myklebust 	if (transport->sock == NULL)
720c50b8ee0STrond Myklebust 		goto out;
721c50b8ee0STrond Myklebust 	for (;;) {
722c50b8ee0STrond Myklebust 		ret = xs_read_stream(transport, MSG_DONTWAIT);
723c4433055STrond Myklebust 		if (ret < 0)
724c50b8ee0STrond Myklebust 			break;
725c50b8ee0STrond Myklebust 		read += ret;
726c50b8ee0STrond Myklebust 		cond_resched();
727c50b8ee0STrond Myklebust 	}
7285f52a9d4STrond Myklebust 	if (ret == -ESHUTDOWN)
7295f52a9d4STrond Myklebust 		kernel_sock_shutdown(transport->sock, SHUT_RDWR);
7305f52a9d4STrond Myklebust 	else
7310ffe86f4STrond Myklebust 		xs_poll_check_readable(transport);
732c50b8ee0STrond Myklebust out:
733c50b8ee0STrond Myklebust 	mutex_unlock(&transport->recv_mutex);
734c50b8ee0STrond Myklebust 	trace_xs_stream_read_data(&transport->xprt, ret, read);
735c50b8ee0STrond Myklebust }
736c50b8ee0STrond Myklebust 
737c50b8ee0STrond Myklebust static void xs_stream_data_receive_workfn(struct work_struct *work)
738c50b8ee0STrond Myklebust {
739c50b8ee0STrond Myklebust 	struct sock_xprt *transport =
740c50b8ee0STrond Myklebust 		container_of(work, struct sock_xprt, recv_worker);
741a1231fdaSTrond Myklebust 	unsigned int pflags = memalloc_nofs_save();
742a1231fdaSTrond Myklebust 
743c50b8ee0STrond Myklebust 	xs_stream_data_receive(transport);
744a1231fdaSTrond Myklebust 	memalloc_nofs_restore(pflags);
745c50b8ee0STrond Myklebust }
746c50b8ee0STrond Myklebust 
747550aebfeSTrond Myklebust static void
748550aebfeSTrond Myklebust xs_stream_reset_connect(struct sock_xprt *transport)
749550aebfeSTrond Myklebust {
750550aebfeSTrond Myklebust 	transport->recv.offset = 0;
751550aebfeSTrond Myklebust 	transport->recv.len = 0;
752550aebfeSTrond Myklebust 	transport->recv.copied = 0;
753550aebfeSTrond Myklebust 	transport->xmit.offset = 0;
754ae053551STrond Myklebust }
755ae053551STrond Myklebust 
756ae053551STrond Myklebust static void
757ae053551STrond Myklebust xs_stream_start_connect(struct sock_xprt *transport)
758ae053551STrond Myklebust {
759550aebfeSTrond Myklebust 	transport->xprt.stat.connect_count++;
760550aebfeSTrond Myklebust 	transport->xprt.stat.connect_start = jiffies;
761550aebfeSTrond Myklebust }
762550aebfeSTrond Myklebust 
763b4b5cc85SChuck Lever #define XS_SENDMSG_FLAGS	(MSG_DONTWAIT | MSG_NOSIGNAL)
764b4b5cc85SChuck Lever 
7659903cd1cSChuck Lever /**
766c544577dSTrond Myklebust  * xs_nospace - handle transmit was incomplete
76750f484e2STrond Myklebust  * @req: pointer to RPC request
7687496b59fSTrond Myklebust  * @transport: pointer to struct sock_xprt
7699903cd1cSChuck Lever  *
770a246b010SChuck Lever  */
7717496b59fSTrond Myklebust static int xs_nospace(struct rpc_rqst *req, struct sock_xprt *transport)
772a246b010SChuck Lever {
7737496b59fSTrond Myklebust 	struct rpc_xprt *xprt = &transport->xprt;
77406ea0bfeSTrond Myklebust 	struct sock *sk = transport->inet;
77524ca9a84STrond Myklebust 	int ret = -EAGAIN;
776a246b010SChuck Lever 
777015747d2SChuck Lever 	trace_rpc_socket_nospace(req, transport);
778a246b010SChuck Lever 
779262965f5SChuck Lever 	/* Protect against races with write_space */
780b5e92419STrond Myklebust 	spin_lock(&xprt->transport_lock);
781a246b010SChuck Lever 
782262965f5SChuck Lever 	/* Don't race with disconnect */
783b6ddf64fSTrond Myklebust 	if (xprt_connected(xprt)) {
7847496b59fSTrond Myklebust 		/* wait for more buffer space */
7852790a624STrond Myklebust 		set_bit(XPRT_SOCK_NOSPACE, &transport->sock_state);
7867496b59fSTrond Myklebust 		set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
7877496b59fSTrond Myklebust 		sk->sk_write_pending++;
7887496b59fSTrond Myklebust 		xprt_wait_for_buffer_space(xprt);
7897496b59fSTrond Myklebust 	} else
7907496b59fSTrond Myklebust 		ret = -ENOTCONN;
7917496b59fSTrond Myklebust 
7927496b59fSTrond Myklebust 	spin_unlock(&xprt->transport_lock);
7937496b59fSTrond Myklebust 	return ret;
794d48f9ce7SDavid Vrabel }
7957496b59fSTrond Myklebust 
7967496b59fSTrond Myklebust static int xs_sock_nospace(struct rpc_rqst *req)
7977496b59fSTrond Myklebust {
7987496b59fSTrond Myklebust 	struct sock_xprt *transport =
7997496b59fSTrond Myklebust 		container_of(req->rq_xprt, struct sock_xprt, xprt);
8007496b59fSTrond Myklebust 	struct sock *sk = transport->inet;
8017496b59fSTrond Myklebust 	int ret = -EAGAIN;
8027496b59fSTrond Myklebust 
8037496b59fSTrond Myklebust 	lock_sock(sk);
8047496b59fSTrond Myklebust 	if (!sock_writeable(sk))
8057496b59fSTrond Myklebust 		ret = xs_nospace(req, transport);
8067496b59fSTrond Myklebust 	release_sock(sk);
8077496b59fSTrond Myklebust 	return ret;
8087496b59fSTrond Myklebust }
8097496b59fSTrond Myklebust 
810d0afde5fSTrond Myklebust static int xs_stream_nospace(struct rpc_rqst *req, bool vm_wait)
8117496b59fSTrond Myklebust {
8127496b59fSTrond Myklebust 	struct sock_xprt *transport =
8137496b59fSTrond Myklebust 		container_of(req->rq_xprt, struct sock_xprt, xprt);
8147496b59fSTrond Myklebust 	struct sock *sk = transport->inet;
8157496b59fSTrond Myklebust 	int ret = -EAGAIN;
8167496b59fSTrond Myklebust 
817d0afde5fSTrond Myklebust 	if (vm_wait)
818d0afde5fSTrond Myklebust 		return -ENOBUFS;
8197496b59fSTrond Myklebust 	lock_sock(sk);
8207496b59fSTrond Myklebust 	if (!sk_stream_memory_free(sk))
8217496b59fSTrond Myklebust 		ret = xs_nospace(req, transport);
8227496b59fSTrond Myklebust 	release_sock(sk);
8235e3771ceSTrond Myklebust 	return ret;
824a246b010SChuck Lever }
825a246b010SChuck Lever 
82672691a26STrond Myklebust static int xs_stream_prepare_request(struct rpc_rqst *req, struct xdr_buf *buf)
827277e4ab7STrond Myklebust {
82872691a26STrond Myklebust 	return xdr_alloc_bvec(buf, rpc_task_gfp_mask());
829277e4ab7STrond Myklebust }
830277e4ab7STrond Myklebust 
83161677eeeSChuck Lever /*
8324cd34e7cSTrond Myklebust  * Determine if the previous message in the stream was aborted before it
8334cd34e7cSTrond Myklebust  * could complete transmission.
8344cd34e7cSTrond Myklebust  */
8354cd34e7cSTrond Myklebust static bool
8364cd34e7cSTrond Myklebust xs_send_request_was_aborted(struct sock_xprt *transport, struct rpc_rqst *req)
8374cd34e7cSTrond Myklebust {
8384cd34e7cSTrond Myklebust 	return transport->xmit.offset != 0 && req->rq_bytes_sent == 0;
8394cd34e7cSTrond Myklebust }
8404cd34e7cSTrond Myklebust 
8414cd34e7cSTrond Myklebust /*
84206b5fc3aSTrond Myklebust  * Return the stream record marker field for a record of length < 2^31-1
84361677eeeSChuck Lever  */
84406b5fc3aSTrond Myklebust static rpc_fraghdr
84506b5fc3aSTrond Myklebust xs_stream_record_marker(struct xdr_buf *xdr)
84661677eeeSChuck Lever {
84706b5fc3aSTrond Myklebust 	if (!xdr->len)
84806b5fc3aSTrond Myklebust 		return 0;
84906b5fc3aSTrond Myklebust 	return cpu_to_be32(RPC_LAST_STREAM_FRAGMENT | (u32)xdr->len);
85061677eeeSChuck Lever }
85161677eeeSChuck Lever 
8529903cd1cSChuck Lever /**
853176e21eeSChuck Lever  * xs_local_send_request - write an RPC request to an AF_LOCAL socket
85450f484e2STrond Myklebust  * @req: pointer to RPC request
855176e21eeSChuck Lever  *
856176e21eeSChuck Lever  * Return values:
857176e21eeSChuck Lever  *        0:	The request has been sent
858176e21eeSChuck Lever  *   EAGAIN:	The socket was blocked, please call again later to
859176e21eeSChuck Lever  *		complete the request
860176e21eeSChuck Lever  * ENOTCONN:	Caller needs to invoke connect logic then call again
86112b20ce3SBhaskar Chowdhury  *    other:	Some other error occurred, the request was not sent
862176e21eeSChuck Lever  */
863adfa7144STrond Myklebust static int xs_local_send_request(struct rpc_rqst *req)
864176e21eeSChuck Lever {
865176e21eeSChuck Lever 	struct rpc_xprt *xprt = req->rq_xprt;
866176e21eeSChuck Lever 	struct sock_xprt *transport =
867176e21eeSChuck Lever 				container_of(xprt, struct sock_xprt, xprt);
868176e21eeSChuck Lever 	struct xdr_buf *xdr = &req->rq_snd_buf;
8697e3d3620STrond Myklebust 	rpc_fraghdr rm = xs_stream_record_marker(xdr);
8707e3d3620STrond Myklebust 	unsigned int msglen = rm ? req->rq_slen + sizeof(rm) : req->rq_slen;
8719e55eef4SChuck Lever 	struct msghdr msg = {
8729e55eef4SChuck Lever 		.msg_flags	= XS_SENDMSG_FLAGS,
8739e55eef4SChuck Lever 	};
874d0afde5fSTrond Myklebust 	bool vm_wait;
8753f649ab7SKees Cook 	unsigned int sent;
876176e21eeSChuck Lever 	int status;
877176e21eeSChuck Lever 
8784cd34e7cSTrond Myklebust 	/* Close the stream if the previous transmission was incomplete */
8794cd34e7cSTrond Myklebust 	if (xs_send_request_was_aborted(transport, req)) {
880f0043206STrond Myklebust 		xprt_force_disconnect(xprt);
8814cd34e7cSTrond Myklebust 		return -ENOTCONN;
8824cd34e7cSTrond Myklebust 	}
8834cd34e7cSTrond Myklebust 
884176e21eeSChuck Lever 	xs_pktdump("packet data:",
885176e21eeSChuck Lever 			req->rq_svec->iov_base, req->rq_svec->iov_len);
886176e21eeSChuck Lever 
887d0afde5fSTrond Myklebust 	vm_wait = sk_stream_is_writeable(transport->inet) ? true : false;
888d0afde5fSTrond Myklebust 
88978215759SChuck Lever 	req->rq_xtime = ktime_get();
8909e55eef4SChuck Lever 	status = xprt_sock_sendmsg(transport->sock, &msg, xdr,
8917e3d3620STrond Myklebust 				   transport->xmit.offset, rm, &sent);
892176e21eeSChuck Lever 	dprintk("RPC:       %s(%u) = %d\n",
8936c7a64e5STrond Myklebust 			__func__, xdr->len - transport->xmit.offset, status);
894743c69e7SNeilBrown 
895f279cd00SJason Baron 	if (likely(sent > 0) || status == 0) {
8966c7a64e5STrond Myklebust 		transport->xmit.offset += sent;
8976c7a64e5STrond Myklebust 		req->rq_bytes_sent = transport->xmit.offset;
8987e3d3620STrond Myklebust 		if (likely(req->rq_bytes_sent >= msglen)) {
8996c7a64e5STrond Myklebust 			req->rq_xmit_bytes_sent += transport->xmit.offset;
9006c7a64e5STrond Myklebust 			transport->xmit.offset = 0;
901176e21eeSChuck Lever 			return 0;
902176e21eeSChuck Lever 		}
903176e21eeSChuck Lever 		status = -EAGAIN;
904d0afde5fSTrond Myklebust 		vm_wait = false;
905176e21eeSChuck Lever 	}
906176e21eeSChuck Lever 
907176e21eeSChuck Lever 	switch (status) {
908176e21eeSChuck Lever 	case -EAGAIN:
909d0afde5fSTrond Myklebust 		status = xs_stream_nospace(req, vm_wait);
910176e21eeSChuck Lever 		break;
911176e21eeSChuck Lever 	default:
912176e21eeSChuck Lever 		dprintk("RPC:       sendmsg returned unrecognized error %d\n",
913176e21eeSChuck Lever 			-status);
914df561f66SGustavo A. R. Silva 		fallthrough;
915176e21eeSChuck Lever 	case -EPIPE:
916f0043206STrond Myklebust 		xprt_force_disconnect(xprt);
917176e21eeSChuck Lever 		status = -ENOTCONN;
918176e21eeSChuck Lever 	}
919176e21eeSChuck Lever 
920176e21eeSChuck Lever 	return status;
921176e21eeSChuck Lever }
922176e21eeSChuck Lever 
923176e21eeSChuck Lever /**
924262965f5SChuck Lever  * xs_udp_send_request - write an RPC request to a UDP socket
92550f484e2STrond Myklebust  * @req: pointer to RPC request
9269903cd1cSChuck Lever  *
9279903cd1cSChuck Lever  * Return values:
9289903cd1cSChuck Lever  *        0:	The request has been sent
9299903cd1cSChuck Lever  *   EAGAIN:	The socket was blocked, please call again later to
9309903cd1cSChuck Lever  *		complete the request
931262965f5SChuck Lever  * ENOTCONN:	Caller needs to invoke connect logic then call again
93225985edcSLucas De Marchi  *    other:	Some other error occurred, the request was not sent
9339903cd1cSChuck Lever  */
934adfa7144STrond Myklebust static int xs_udp_send_request(struct rpc_rqst *req)
935a246b010SChuck Lever {
936a246b010SChuck Lever 	struct rpc_xprt *xprt = req->rq_xprt;
937ee0ac0c2SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
938262965f5SChuck Lever 	struct xdr_buf *xdr = &req->rq_snd_buf;
9399e55eef4SChuck Lever 	struct msghdr msg = {
9409e55eef4SChuck Lever 		.msg_name	= xs_addr(xprt),
9419e55eef4SChuck Lever 		.msg_namelen	= xprt->addrlen,
9429e55eef4SChuck Lever 		.msg_flags	= XS_SENDMSG_FLAGS,
9439e55eef4SChuck Lever 	};
9443f649ab7SKees Cook 	unsigned int sent;
945262965f5SChuck Lever 	int status;
946262965f5SChuck Lever 
947262965f5SChuck Lever 	xs_pktdump("packet data:",
948262965f5SChuck Lever 				req->rq_svec->iov_base,
949262965f5SChuck Lever 				req->rq_svec->iov_len);
950262965f5SChuck Lever 
95101d37c42STrond Myklebust 	if (!xprt_bound(xprt))
95201d37c42STrond Myklebust 		return -ENOTCONN;
95375891f50STrond Myklebust 
95475891f50STrond Myklebust 	if (!xprt_request_get_cong(xprt, req))
95575891f50STrond Myklebust 		return -EBADSLT;
95675891f50STrond Myklebust 
957ff053dbbSTrond Myklebust 	status = xdr_alloc_bvec(xdr, rpc_task_gfp_mask());
958ff053dbbSTrond Myklebust 	if (status < 0)
959ff053dbbSTrond Myklebust 		return status;
96078215759SChuck Lever 	req->rq_xtime = ktime_get();
9619e55eef4SChuck Lever 	status = xprt_sock_sendmsg(transport->sock, &msg, xdr, 0, 0, &sent);
962262965f5SChuck Lever 
963262965f5SChuck Lever 	dprintk("RPC:       xs_udp_send_request(%u) = %d\n",
9646c7a64e5STrond Myklebust 			xdr->len, status);
965262965f5SChuck Lever 
9663dedbb5cSJason Baron 	/* firewall is blocking us, don't return -EAGAIN or we end up looping */
9673dedbb5cSJason Baron 	if (status == -EPERM)
9683dedbb5cSJason Baron 		goto process_status;
9693dedbb5cSJason Baron 
970743c69e7SNeilBrown 	if (status == -EAGAIN && sock_writeable(transport->inet))
971743c69e7SNeilBrown 		status = -ENOBUFS;
972743c69e7SNeilBrown 
973f279cd00SJason Baron 	if (sent > 0 || status == 0) {
974f279cd00SJason Baron 		req->rq_xmit_bytes_sent += sent;
975f279cd00SJason Baron 		if (sent >= req->rq_slen)
976262965f5SChuck Lever 			return 0;
977262965f5SChuck Lever 		/* Still some bytes left; set up for a retry later. */
978262965f5SChuck Lever 		status = -EAGAIN;
9792199700fSTrond Myklebust 	}
980262965f5SChuck Lever 
9813dedbb5cSJason Baron process_status:
982262965f5SChuck Lever 	switch (status) {
983fba91afbSTrond Myklebust 	case -ENOTSOCK:
984fba91afbSTrond Myklebust 		status = -ENOTCONN;
985fba91afbSTrond Myklebust 		/* Should we call xs_close() here? */
986fba91afbSTrond Myklebust 		break;
987b6ddf64fSTrond Myklebust 	case -EAGAIN:
9887496b59fSTrond Myklebust 		status = xs_sock_nospace(req);
989b6ddf64fSTrond Myklebust 		break;
990262965f5SChuck Lever 	case -ENETUNREACH:
9913601c4a9STrond Myklebust 	case -ENOBUFS:
992262965f5SChuck Lever 	case -EPIPE:
993262965f5SChuck Lever 	case -ECONNREFUSED:
9943dedbb5cSJason Baron 	case -EPERM:
995262965f5SChuck Lever 		/* When the server has died, an ICMP port unreachable message
996262965f5SChuck Lever 		 * prompts ECONNREFUSED. */
99713331a55STrond Myklebust 		break;
99813331a55STrond Myklebust 	default:
99913331a55STrond Myklebust 		dprintk("RPC:       sendmsg returned unrecognized error %d\n",
100013331a55STrond Myklebust 			-status);
1001262965f5SChuck Lever 	}
10025fe46e9dSBian Naimeng 
1003262965f5SChuck Lever 	return status;
1004262965f5SChuck Lever }
1005262965f5SChuck Lever 
1006e06799f9STrond Myklebust /**
1007262965f5SChuck Lever  * xs_tcp_send_request - write an RPC request to a TCP socket
100850f484e2STrond Myklebust  * @req: pointer to RPC request
1009262965f5SChuck Lever  *
1010262965f5SChuck Lever  * Return values:
1011262965f5SChuck Lever  *        0:	The request has been sent
1012262965f5SChuck Lever  *   EAGAIN:	The socket was blocked, please call again later to
1013262965f5SChuck Lever  *		complete the request
1014262965f5SChuck Lever  * ENOTCONN:	Caller needs to invoke connect logic then call again
101525985edcSLucas De Marchi  *    other:	Some other error occurred, the request was not sent
1016262965f5SChuck Lever  *
1017262965f5SChuck Lever  * XXX: In the case of soft timeouts, should we eventually give up
1018262965f5SChuck Lever  *	if sendmsg is not able to make progress?
1019262965f5SChuck Lever  */
1020adfa7144STrond Myklebust static int xs_tcp_send_request(struct rpc_rqst *req)
1021262965f5SChuck Lever {
1022262965f5SChuck Lever 	struct rpc_xprt *xprt = req->rq_xprt;
1023ee0ac0c2SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1024262965f5SChuck Lever 	struct xdr_buf *xdr = &req->rq_snd_buf;
10257e3d3620STrond Myklebust 	rpc_fraghdr rm = xs_stream_record_marker(xdr);
10267e3d3620STrond Myklebust 	unsigned int msglen = rm ? req->rq_slen + sizeof(rm) : req->rq_slen;
10279e55eef4SChuck Lever 	struct msghdr msg = {
10289e55eef4SChuck Lever 		.msg_flags	= XS_SENDMSG_FLAGS,
10299e55eef4SChuck Lever 	};
1030d0afde5fSTrond Myklebust 	bool vm_wait;
10313f649ab7SKees Cook 	unsigned int sent;
1032b595bb15SChuck Lever 	int status;
1033a246b010SChuck Lever 
10344cd34e7cSTrond Myklebust 	/* Close the stream if the previous transmission was incomplete */
10354cd34e7cSTrond Myklebust 	if (xs_send_request_was_aborted(transport, req)) {
10364cd34e7cSTrond Myklebust 		if (transport->sock != NULL)
10374cd34e7cSTrond Myklebust 			kernel_sock_shutdown(transport->sock, SHUT_RDWR);
10384cd34e7cSTrond Myklebust 		return -ENOTCONN;
10394cd34e7cSTrond Myklebust 	}
1040d275880aSTrond Myklebust 	if (!transport->inet)
1041d275880aSTrond Myklebust 		return -ENOTCONN;
10424cd34e7cSTrond Myklebust 
1043262965f5SChuck Lever 	xs_pktdump("packet data:",
1044262965f5SChuck Lever 				req->rq_svec->iov_base,
1045262965f5SChuck Lever 				req->rq_svec->iov_len);
1046a246b010SChuck Lever 
10477196dbb0STrond Myklebust 	if (test_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state))
10487196dbb0STrond Myklebust 		xs_tcp_set_socket_timeouts(xprt, transport->sock);
10497196dbb0STrond Myklebust 
10503b21f757STrond Myklebust 	xs_set_srcport(transport, transport->sock);
10513b21f757STrond Myklebust 
1052a246b010SChuck Lever 	/* Continue transmitting the packet/record. We must be careful
1053a246b010SChuck Lever 	 * to cope with writespace callbacks arriving _after_ we have
1054262965f5SChuck Lever 	 * called sendmsg(). */
105578215759SChuck Lever 	req->rq_xtime = ktime_get();
1056d737e5d4STrond Myklebust 	tcp_sock_set_cork(transport->inet, true);
1057d0afde5fSTrond Myklebust 
1058d0afde5fSTrond Myklebust 	vm_wait = sk_stream_is_writeable(transport->inet) ? true : false;
1059d0afde5fSTrond Myklebust 
1060d0afde5fSTrond Myklebust 	do {
10619e55eef4SChuck Lever 		status = xprt_sock_sendmsg(transport->sock, &msg, xdr,
10627e3d3620STrond Myklebust 					   transport->xmit.offset, rm, &sent);
1063a246b010SChuck Lever 
1064262965f5SChuck Lever 		dprintk("RPC:       xs_tcp_send_request(%u) = %d\n",
10656c7a64e5STrond Myklebust 				xdr->len - transport->xmit.offset, status);
1066262965f5SChuck Lever 
1067a246b010SChuck Lever 		/* If we've sent the entire packet, immediately
1068a246b010SChuck Lever 		 * reset the count of bytes sent. */
10696c7a64e5STrond Myklebust 		transport->xmit.offset += sent;
10706c7a64e5STrond Myklebust 		req->rq_bytes_sent = transport->xmit.offset;
10717e3d3620STrond Myklebust 		if (likely(req->rq_bytes_sent >= msglen)) {
10726c7a64e5STrond Myklebust 			req->rq_xmit_bytes_sent += transport->xmit.offset;
10736c7a64e5STrond Myklebust 			transport->xmit.offset = 0;
1074d737e5d4STrond Myklebust 			if (atomic_long_read(&xprt->xmit_queuelen) == 1)
1075d737e5d4STrond Myklebust 				tcp_sock_set_cork(transport->inet, false);
1076a246b010SChuck Lever 			return 0;
1077a246b010SChuck Lever 		}
1078262965f5SChuck Lever 
10799ffadfbcSTrond Myklebust 		WARN_ON_ONCE(sent == 0 && status == 0);
10809ffadfbcSTrond Myklebust 
1081d0afde5fSTrond Myklebust 		if (sent > 0)
10829ffadfbcSTrond Myklebust 			vm_wait = false;
1083d0afde5fSTrond Myklebust 
1084d0afde5fSTrond Myklebust 	} while (status == 0);
1085a246b010SChuck Lever 
1086262965f5SChuck Lever 	switch (status) {
1087fba91afbSTrond Myklebust 	case -ENOTSOCK:
1088fba91afbSTrond Myklebust 		status = -ENOTCONN;
1089fba91afbSTrond Myklebust 		/* Should we call xs_close() here? */
1090fba91afbSTrond Myklebust 		break;
1091262965f5SChuck Lever 	case -EAGAIN:
1092d0afde5fSTrond Myklebust 		status = xs_stream_nospace(req, vm_wait);
1093262965f5SChuck Lever 		break;
1094262965f5SChuck Lever 	case -ECONNRESET:
1095262965f5SChuck Lever 	case -ECONNREFUSED:
1096262965f5SChuck Lever 	case -ENOTCONN:
10973913c78cSTrond Myklebust 	case -EADDRINUSE:
1098b5872f0cSTrond Myklebust 	case -ENOBUFS:
1099b9d2bb2eSTrond Myklebust 	case -EPIPE:
110013331a55STrond Myklebust 		break;
110113331a55STrond Myklebust 	default:
110213331a55STrond Myklebust 		dprintk("RPC:       sendmsg returned unrecognized error %d\n",
110313331a55STrond Myklebust 			-status);
1104a246b010SChuck Lever 	}
11055fe46e9dSBian Naimeng 
1106a246b010SChuck Lever 	return status;
1107a246b010SChuck Lever }
1108a246b010SChuck Lever 
11092a9e1cfaSTrond Myklebust static void xs_save_old_callbacks(struct sock_xprt *transport, struct sock *sk)
11102a9e1cfaSTrond Myklebust {
11112a9e1cfaSTrond Myklebust 	transport->old_data_ready = sk->sk_data_ready;
11122a9e1cfaSTrond Myklebust 	transport->old_state_change = sk->sk_state_change;
11132a9e1cfaSTrond Myklebust 	transport->old_write_space = sk->sk_write_space;
11142118071dSTrond Myklebust 	transport->old_error_report = sk->sk_error_report;
11152a9e1cfaSTrond Myklebust }
11162a9e1cfaSTrond Myklebust 
11172a9e1cfaSTrond Myklebust static void xs_restore_old_callbacks(struct sock_xprt *transport, struct sock *sk)
11182a9e1cfaSTrond Myklebust {
11192a9e1cfaSTrond Myklebust 	sk->sk_data_ready = transport->old_data_ready;
11202a9e1cfaSTrond Myklebust 	sk->sk_state_change = transport->old_state_change;
11212a9e1cfaSTrond Myklebust 	sk->sk_write_space = transport->old_write_space;
11222118071dSTrond Myklebust 	sk->sk_error_report = transport->old_error_report;
11232118071dSTrond Myklebust }
11242118071dSTrond Myklebust 
112542d42a5bSTrond Myklebust static void xs_sock_reset_state_flags(struct rpc_xprt *xprt)
112642d42a5bSTrond Myklebust {
112742d42a5bSTrond Myklebust 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
112842d42a5bSTrond Myklebust 
112942d42a5bSTrond Myklebust 	clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state);
11304f8943f8STrond Myklebust 	clear_bit(XPRT_SOCK_WAKE_ERROR, &transport->sock_state);
11314f8943f8STrond Myklebust 	clear_bit(XPRT_SOCK_WAKE_WRITE, &transport->sock_state);
11324f8943f8STrond Myklebust 	clear_bit(XPRT_SOCK_WAKE_DISCONNECT, &transport->sock_state);
11332790a624STrond Myklebust 	clear_bit(XPRT_SOCK_NOSPACE, &transport->sock_state);
11344f8943f8STrond Myklebust }
11354f8943f8STrond Myklebust 
11364f8943f8STrond Myklebust static void xs_run_error_worker(struct sock_xprt *transport, unsigned int nr)
11374f8943f8STrond Myklebust {
11384f8943f8STrond Myklebust 	set_bit(nr, &transport->sock_state);
11394f8943f8STrond Myklebust 	queue_work(xprtiod_workqueue, &transport->error_worker);
114042d42a5bSTrond Myklebust }
114142d42a5bSTrond Myklebust 
1142b70ae915STrond Myklebust static void xs_sock_reset_connection_flags(struct rpc_xprt *xprt)
1143b70ae915STrond Myklebust {
1144d896ba83STrond Myklebust 	xprt->connect_cookie++;
1145b70ae915STrond Myklebust 	smp_mb__before_atomic();
1146b70ae915STrond Myklebust 	clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
1147b70ae915STrond Myklebust 	clear_bit(XPRT_CLOSING, &xprt->state);
114842d42a5bSTrond Myklebust 	xs_sock_reset_state_flags(xprt);
1149b70ae915STrond Myklebust 	smp_mb__after_atomic();
1150b70ae915STrond Myklebust }
1151b70ae915STrond Myklebust 
11522118071dSTrond Myklebust /**
11532118071dSTrond Myklebust  * xs_error_report - callback to handle TCP socket state errors
11542118071dSTrond Myklebust  * @sk: socket
11552118071dSTrond Myklebust  *
11562118071dSTrond Myklebust  * Note: we don't call sock_error() since there may be a rpc_task
11572118071dSTrond Myklebust  * using the socket, and so we don't want to clear sk->sk_err.
11582118071dSTrond Myklebust  */
11592118071dSTrond Myklebust static void xs_error_report(struct sock *sk)
11602118071dSTrond Myklebust {
11614f8943f8STrond Myklebust 	struct sock_xprt *transport;
11622118071dSTrond Myklebust 	struct rpc_xprt *xprt;
11632118071dSTrond Myklebust 
11642118071dSTrond Myklebust 	if (!(xprt = xprt_from_sock(sk)))
1165ea9afca8STrond Myklebust 		return;
11662118071dSTrond Myklebust 
11674f8943f8STrond Myklebust 	transport = container_of(xprt, struct sock_xprt, xprt);
1168af84537dSBenjamin Coddington 	transport->xprt_err = -sk->sk_err;
1169af84537dSBenjamin Coddington 	if (transport->xprt_err == 0)
1170ea9afca8STrond Myklebust 		return;
11712118071dSTrond Myklebust 	dprintk("RPC:       xs_error_report client %p, error=%d...\n",
1172af84537dSBenjamin Coddington 			xprt, -transport->xprt_err);
1173af84537dSBenjamin Coddington 	trace_rpc_socket_error(xprt, sk->sk_socket, transport->xprt_err);
1174af84537dSBenjamin Coddington 
1175af84537dSBenjamin Coddington 	/* barrier ensures xprt_err is set before XPRT_SOCK_WAKE_ERROR */
1176af84537dSBenjamin Coddington 	smp_mb__before_atomic();
11774f8943f8STrond Myklebust 	xs_run_error_worker(transport, XPRT_SOCK_WAKE_ERROR);
11782a9e1cfaSTrond Myklebust }
11792a9e1cfaSTrond Myklebust 
1180fe315e76SChuck Lever static void xs_reset_transport(struct sock_xprt *transport)
1181a246b010SChuck Lever {
1182ee0ac0c2SChuck Lever 	struct socket *sock = transport->sock;
1183ee0ac0c2SChuck Lever 	struct sock *sk = transport->inet;
11846cc7e908STrond Myklebust 	struct rpc_xprt *xprt = &transport->xprt;
1185a73881c9STrond Myklebust 	struct file *filp = transport->file;
1186a246b010SChuck Lever 
1187fe315e76SChuck Lever 	if (sk == NULL)
1188fe315e76SChuck Lever 		return;
1189f0043206STrond Myklebust 	/*
1190f0043206STrond Myklebust 	 * Make sure we're calling this in a context from which it is safe
1191f0043206STrond Myklebust 	 * to call __fput_sync(). In practice that means rpciod and the
1192f0043206STrond Myklebust 	 * system workqueue.
1193f0043206STrond Myklebust 	 */
1194f0043206STrond Myklebust 	if (!(current->flags & PF_WQ_WORKER)) {
1195f0043206STrond Myklebust 		WARN_ON_ONCE(1);
1196f0043206STrond Myklebust 		set_bit(XPRT_CLOSE_WAIT, &xprt->state);
1197f0043206STrond Myklebust 		return;
1198f0043206STrond Myklebust 	}
11999903cd1cSChuck Lever 
1200264d1df3SJeff Layton 	if (atomic_read(&transport->xprt.swapper))
1201264d1df3SJeff Layton 		sk_clear_memalloc(sk);
1202264d1df3SJeff Layton 
120309939204STrond Myklebust 	kernel_sock_shutdown(sock, SHUT_RDWR);
120409939204STrond Myklebust 
1205edc1b01cSTrond Myklebust 	mutex_lock(&transport->recv_mutex);
1206ea9afca8STrond Myklebust 	lock_sock(sk);
1207ee0ac0c2SChuck Lever 	transport->inet = NULL;
1208ee0ac0c2SChuck Lever 	transport->sock = NULL;
1209a73881c9STrond Myklebust 	transport->file = NULL;
1210a246b010SChuck Lever 
1211a246b010SChuck Lever 	sk->sk_user_data = NULL;
12122a9e1cfaSTrond Myklebust 
12132a9e1cfaSTrond Myklebust 	xs_restore_old_callbacks(transport, sk);
12140c78789eSTrond Myklebust 	xprt_clear_connected(xprt);
12156cc7e908STrond Myklebust 	xs_sock_reset_connection_flags(xprt);
1216ae053551STrond Myklebust 	/* Reset stream record info */
1217ae053551STrond Myklebust 	xs_stream_reset_connect(transport);
1218ea9afca8STrond Myklebust 	release_sock(sk);
1219edc1b01cSTrond Myklebust 	mutex_unlock(&transport->recv_mutex);
1220a246b010SChuck Lever 
12216cc7e908STrond Myklebust 	trace_rpc_socket_close(xprt, sock);
1222f0043206STrond Myklebust 	__fput_sync(filp);
12230445f92cSTrond Myklebust 
12240445f92cSTrond Myklebust 	xprt_disconnect_done(xprt);
1225fe315e76SChuck Lever }
1226fe315e76SChuck Lever 
1227fe315e76SChuck Lever /**
1228fe315e76SChuck Lever  * xs_close - close a socket
1229fe315e76SChuck Lever  * @xprt: transport
1230fe315e76SChuck Lever  *
1231fe315e76SChuck Lever  * This is used when all requests are complete; ie, no DRC state remains
1232fe315e76SChuck Lever  * on the server we want to save.
1233f75e6745STrond Myklebust  *
1234f75e6745STrond Myklebust  * The caller _must_ be holding XPRT_LOCKED in order to avoid issues with
1235f75e6745STrond Myklebust  * xs_reset_transport() zeroing the socket from underneath a writer.
1236fe315e76SChuck Lever  */
1237fe315e76SChuck Lever static void xs_close(struct rpc_xprt *xprt)
1238fe315e76SChuck Lever {
1239fe315e76SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1240fe315e76SChuck Lever 
1241fe315e76SChuck Lever 	dprintk("RPC:       xs_close xprt %p\n", xprt);
1242fe315e76SChuck Lever 
1243fe315e76SChuck Lever 	xs_reset_transport(transport);
124461d0a8e6SNeil Brown 	xprt->reestablish_timeout = 0;
1245a246b010SChuck Lever }
1246a246b010SChuck Lever 
12474a068258SChuck Lever static void xs_inject_disconnect(struct rpc_xprt *xprt)
12484a068258SChuck Lever {
12494a068258SChuck Lever 	dprintk("RPC:       injecting transport disconnect on xprt=%p\n",
12504a068258SChuck Lever 		xprt);
12514a068258SChuck Lever 	xprt_disconnect_done(xprt);
12524a068258SChuck Lever }
12534a068258SChuck Lever 
1254315f3812SKinglong Mee static void xs_xprt_free(struct rpc_xprt *xprt)
1255315f3812SKinglong Mee {
1256315f3812SKinglong Mee 	xs_free_peer_addresses(xprt);
1257315f3812SKinglong Mee 	xprt_free(xprt);
1258315f3812SKinglong Mee }
1259315f3812SKinglong Mee 
12609903cd1cSChuck Lever /**
12619903cd1cSChuck Lever  * xs_destroy - prepare to shutdown a transport
12629903cd1cSChuck Lever  * @xprt: doomed transport
12639903cd1cSChuck Lever  *
12649903cd1cSChuck Lever  */
12659903cd1cSChuck Lever static void xs_destroy(struct rpc_xprt *xprt)
1266a246b010SChuck Lever {
126703c78827STrond Myklebust 	struct sock_xprt *transport = container_of(xprt,
126803c78827STrond Myklebust 			struct sock_xprt, xprt);
12699903cd1cSChuck Lever 	dprintk("RPC:       xs_destroy xprt %p\n", xprt);
12709903cd1cSChuck Lever 
127103c78827STrond Myklebust 	cancel_delayed_work_sync(&transport->connect_worker);
1272a1311d87STrond Myklebust 	xs_close(xprt);
1273edc1b01cSTrond Myklebust 	cancel_work_sync(&transport->recv_worker);
1274b5e92419STrond Myklebust 	cancel_work_sync(&transport->error_worker);
1275315f3812SKinglong Mee 	xs_xprt_free(xprt);
1276a1311d87STrond Myklebust 	module_put(THIS_MODULE);
1277a246b010SChuck Lever }
1278a246b010SChuck Lever 
12799903cd1cSChuck Lever /**
1280f9b2ee71STrond Myklebust  * xs_udp_data_read_skb - receive callback for UDP sockets
1281f9b2ee71STrond Myklebust  * @xprt: transport
1282f9b2ee71STrond Myklebust  * @sk: socket
1283f9b2ee71STrond Myklebust  * @skb: skbuff
12849903cd1cSChuck Lever  *
1285a246b010SChuck Lever  */
1286f9b2ee71STrond Myklebust static void xs_udp_data_read_skb(struct rpc_xprt *xprt,
1287f9b2ee71STrond Myklebust 		struct sock *sk,
1288f9b2ee71STrond Myklebust 		struct sk_buff *skb)
1289a246b010SChuck Lever {
1290a246b010SChuck Lever 	struct rpc_task *task;
1291a246b010SChuck Lever 	struct rpc_rqst *rovr;
1292f9b2ee71STrond Myklebust 	int repsize, copied;
1293d8ed029dSAlexey Dobriyan 	u32 _xid;
1294d8ed029dSAlexey Dobriyan 	__be32 *xp;
1295a246b010SChuck Lever 
12961da8c681SWillem de Bruijn 	repsize = skb->len;
1297a246b010SChuck Lever 	if (repsize < 4) {
12989903cd1cSChuck Lever 		dprintk("RPC:       impossible RPC reply size %d!\n", repsize);
1299f9b2ee71STrond Myklebust 		return;
1300a246b010SChuck Lever 	}
1301a246b010SChuck Lever 
1302a246b010SChuck Lever 	/* Copy the XID from the skb... */
13031da8c681SWillem de Bruijn 	xp = skb_header_pointer(skb, 0, sizeof(_xid), &_xid);
1304a246b010SChuck Lever 	if (xp == NULL)
1305f9b2ee71STrond Myklebust 		return;
1306a246b010SChuck Lever 
1307a246b010SChuck Lever 	/* Look up and lock the request corresponding to the given XID */
130875c84151STrond Myklebust 	spin_lock(&xprt->queue_lock);
1309a246b010SChuck Lever 	rovr = xprt_lookup_rqst(xprt, *xp);
1310a246b010SChuck Lever 	if (!rovr)
1311a246b010SChuck Lever 		goto out_unlock;
1312729749bbSTrond Myklebust 	xprt_pin_rqst(rovr);
1313ecd465eeSChuck Lever 	xprt_update_rtt(rovr->rq_task);
131475c84151STrond Myklebust 	spin_unlock(&xprt->queue_lock);
1315a246b010SChuck Lever 	task = rovr->rq_task;
1316a246b010SChuck Lever 
1317a246b010SChuck Lever 	if ((copied = rovr->rq_private_buf.buflen) > repsize)
1318a246b010SChuck Lever 		copied = repsize;
1319a246b010SChuck Lever 
1320a246b010SChuck Lever 	/* Suck it into the iovec, verify checksum if not done by hw. */
13211781f7f5SHerbert Xu 	if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) {
132275c84151STrond Myklebust 		spin_lock(&xprt->queue_lock);
13230afa6b44STrond Myklebust 		__UDPX_INC_STATS(sk, UDP_MIB_INERRORS);
1324729749bbSTrond Myklebust 		goto out_unpin;
13251781f7f5SHerbert Xu 	}
13261781f7f5SHerbert Xu 
1327a246b010SChuck Lever 
1328b5e92419STrond Myklebust 	spin_lock(&xprt->transport_lock);
13296a24dfb6STrond Myklebust 	xprt_adjust_cwnd(xprt, task, copied);
1330b5e92419STrond Myklebust 	spin_unlock(&xprt->transport_lock);
133175c84151STrond Myklebust 	spin_lock(&xprt->queue_lock);
13321570c1e4SChuck Lever 	xprt_complete_rqst(task, copied);
13330afa6b44STrond Myklebust 	__UDPX_INC_STATS(sk, UDP_MIB_INDATAGRAMS);
1334729749bbSTrond Myklebust out_unpin:
1335729749bbSTrond Myklebust 	xprt_unpin_rqst(rovr);
1336a246b010SChuck Lever  out_unlock:
133775c84151STrond Myklebust 	spin_unlock(&xprt->queue_lock);
1338f9b2ee71STrond Myklebust }
1339f9b2ee71STrond Myklebust 
1340f9b2ee71STrond Myklebust static void xs_udp_data_receive(struct sock_xprt *transport)
1341f9b2ee71STrond Myklebust {
1342f9b2ee71STrond Myklebust 	struct sk_buff *skb;
1343f9b2ee71STrond Myklebust 	struct sock *sk;
1344f9b2ee71STrond Myklebust 	int err;
1345f9b2ee71STrond Myklebust 
1346f9b2ee71STrond Myklebust 	mutex_lock(&transport->recv_mutex);
1347f9b2ee71STrond Myklebust 	sk = transport->inet;
1348f9b2ee71STrond Myklebust 	if (sk == NULL)
1349f9b2ee71STrond Myklebust 		goto out;
1350f9b2ee71STrond Myklebust 	for (;;) {
1351ec095263SOliver Hartkopp 		skb = skb_recv_udp(sk, MSG_DONTWAIT, &err);
13524f546149STrond Myklebust 		if (skb == NULL)
13534f546149STrond Myklebust 			break;
1354f9b2ee71STrond Myklebust 		xs_udp_data_read_skb(&transport->xprt, sk, skb);
1355850cbaddSPaolo Abeni 		consume_skb(skb);
13560af3442aSTrond Myklebust 		cond_resched();
1357f9b2ee71STrond Myklebust 	}
13580ffe86f4STrond Myklebust 	xs_poll_check_readable(transport);
1359a246b010SChuck Lever out:
1360f9b2ee71STrond Myklebust 	mutex_unlock(&transport->recv_mutex);
1361f9b2ee71STrond Myklebust }
1362f9b2ee71STrond Myklebust 
1363f9b2ee71STrond Myklebust static void xs_udp_data_receive_workfn(struct work_struct *work)
1364f9b2ee71STrond Myklebust {
1365f9b2ee71STrond Myklebust 	struct sock_xprt *transport =
1366f9b2ee71STrond Myklebust 		container_of(work, struct sock_xprt, recv_worker);
1367a1231fdaSTrond Myklebust 	unsigned int pflags = memalloc_nofs_save();
1368a1231fdaSTrond Myklebust 
1369f9b2ee71STrond Myklebust 	xs_udp_data_receive(transport);
1370a1231fdaSTrond Myklebust 	memalloc_nofs_restore(pflags);
1371f9b2ee71STrond Myklebust }
1372f9b2ee71STrond Myklebust 
1373f9b2ee71STrond Myklebust /**
1374f67939e4SChuck Lever  * xs_data_ready - "data ready" callback for sockets
1375f9b2ee71STrond Myklebust  * @sk: socket with data to read
1376f9b2ee71STrond Myklebust  *
1377f9b2ee71STrond Myklebust  */
1378f9b2ee71STrond Myklebust static void xs_data_ready(struct sock *sk)
1379f9b2ee71STrond Myklebust {
1380f9b2ee71STrond Myklebust 	struct rpc_xprt *xprt;
1381f9b2ee71STrond Myklebust 
138240e0b090SPeilin Ye 	trace_sk_data_ready(sk);
138340e0b090SPeilin Ye 
1384f9b2ee71STrond Myklebust 	xprt = xprt_from_sock(sk);
1385f9b2ee71STrond Myklebust 	if (xprt != NULL) {
1386f9b2ee71STrond Myklebust 		struct sock_xprt *transport = container_of(xprt,
1387f9b2ee71STrond Myklebust 				struct sock_xprt, xprt);
1388f67939e4SChuck Lever 
1389f67939e4SChuck Lever 		trace_xs_data_ready(xprt);
1390f67939e4SChuck Lever 
13915157b956STrond Myklebust 		transport->old_data_ready(sk);
13925157b956STrond Myklebust 		/* Any data means we had a useful conversation, so
13935157b956STrond Myklebust 		 * then we don't need to delay the next reconnect
13945157b956STrond Myklebust 		 */
13955157b956STrond Myklebust 		if (xprt->reestablish_timeout)
13965157b956STrond Myklebust 			xprt->reestablish_timeout = 0;
139742d42a5bSTrond Myklebust 		if (!test_and_set_bit(XPRT_SOCK_DATA_READY, &transport->sock_state))
139840a5f1b1STrond Myklebust 			queue_work(xprtiod_workqueue, &transport->recv_worker);
1399f9b2ee71STrond Myklebust 	}
1400a246b010SChuck Lever }
1401a246b010SChuck Lever 
1402a519fc7aSTrond Myklebust /*
1403a519fc7aSTrond Myklebust  * Helper function to force a TCP close if the server is sending
1404a519fc7aSTrond Myklebust  * junk and/or it has put us in CLOSE_WAIT
1405a519fc7aSTrond Myklebust  */
1406a519fc7aSTrond Myklebust static void xs_tcp_force_close(struct rpc_xprt *xprt)
1407a519fc7aSTrond Myklebust {
1408a519fc7aSTrond Myklebust 	xprt_force_disconnect(xprt);
1409a519fc7aSTrond Myklebust }
1410a519fc7aSTrond Myklebust 
14119e00abc3STrond Myklebust #if defined(CONFIG_SUNRPC_BACKCHANNEL)
14126b26cc8cSChuck Lever static size_t xs_tcp_bc_maxpayload(struct rpc_xprt *xprt)
14136b26cc8cSChuck Lever {
14146b26cc8cSChuck Lever 	return PAGE_SIZE;
14156b26cc8cSChuck Lever }
14169e00abc3STrond Myklebust #endif /* CONFIG_SUNRPC_BACKCHANNEL */
141744b98efdSRicardo Labiaga 
14189903cd1cSChuck Lever /**
1419efce2d0bSTrond Myklebust  * xs_local_state_change - callback to handle AF_LOCAL socket state changes
1420efce2d0bSTrond Myklebust  * @sk: socket whose state has changed
1421efce2d0bSTrond Myklebust  *
1422efce2d0bSTrond Myklebust  */
1423efce2d0bSTrond Myklebust static void xs_local_state_change(struct sock *sk)
1424efce2d0bSTrond Myklebust {
1425efce2d0bSTrond Myklebust 	struct rpc_xprt *xprt;
1426efce2d0bSTrond Myklebust 	struct sock_xprt *transport;
1427efce2d0bSTrond Myklebust 
1428efce2d0bSTrond Myklebust 	if (!(xprt = xprt_from_sock(sk)))
1429efce2d0bSTrond Myklebust 		return;
1430efce2d0bSTrond Myklebust 	transport = container_of(xprt, struct sock_xprt, xprt);
1431efce2d0bSTrond Myklebust 	if (sk->sk_shutdown & SHUTDOWN_MASK) {
1432efce2d0bSTrond Myklebust 		clear_bit(XPRT_CONNECTED, &xprt->state);
1433efce2d0bSTrond Myklebust 		/* Trigger the socket release */
1434efce2d0bSTrond Myklebust 		xs_run_error_worker(transport, XPRT_SOCK_WAKE_DISCONNECT);
1435efce2d0bSTrond Myklebust 	}
1436efce2d0bSTrond Myklebust }
1437efce2d0bSTrond Myklebust 
1438efce2d0bSTrond Myklebust /**
14399903cd1cSChuck Lever  * xs_tcp_state_change - callback to handle TCP socket state changes
14409903cd1cSChuck Lever  * @sk: socket whose state has changed
14419903cd1cSChuck Lever  *
14429903cd1cSChuck Lever  */
14439903cd1cSChuck Lever static void xs_tcp_state_change(struct sock *sk)
1444a246b010SChuck Lever {
1445a246b010SChuck Lever 	struct rpc_xprt *xprt;
14460fdea1e8STrond Myklebust 	struct sock_xprt *transport;
1447a246b010SChuck Lever 
1448a246b010SChuck Lever 	if (!(xprt = xprt_from_sock(sk)))
1449ea9afca8STrond Myklebust 		return;
14509903cd1cSChuck Lever 	dprintk("RPC:       xs_tcp_state_change client %p...\n", xprt);
1451669502ffSAndy Chittenden 	dprintk("RPC:       state %x conn %d dead %d zapped %d sk_shutdown %d\n",
1452a246b010SChuck Lever 			sk->sk_state, xprt_connected(xprt),
1453a246b010SChuck Lever 			sock_flag(sk, SOCK_DEAD),
1454669502ffSAndy Chittenden 			sock_flag(sk, SOCK_ZAPPED),
1455669502ffSAndy Chittenden 			sk->sk_shutdown);
1456a246b010SChuck Lever 
14570fdea1e8STrond Myklebust 	transport = container_of(xprt, struct sock_xprt, xprt);
145840b5ea0cSTrond Myklebust 	trace_rpc_socket_state_change(xprt, sk->sk_socket);
1459a246b010SChuck Lever 	switch (sk->sk_state) {
1460a246b010SChuck Lever 	case TCP_ESTABLISHED:
1461a246b010SChuck Lever 		if (!xprt_test_and_set_connected(xprt)) {
14628b71798cSTrond Myklebust 			xprt->connect_cookie++;
14630fdea1e8STrond Myklebust 			clear_bit(XPRT_SOCK_CONNECTING, &transport->sock_state);
14640fdea1e8STrond Myklebust 			xprt_clear_connecting(xprt);
146551971139SChuck Lever 
14663968a8a5SChuck Lever 			xprt->stat.connect_count++;
14673968a8a5SChuck Lever 			xprt->stat.connect_time += (long)jiffies -
14683968a8a5SChuck Lever 						   xprt->stat.connect_start;
14694f8943f8STrond Myklebust 			xs_run_error_worker(transport, XPRT_SOCK_WAKE_PENDING);
1470a246b010SChuck Lever 		}
1471a246b010SChuck Lever 		break;
14723b948ae5STrond Myklebust 	case TCP_FIN_WAIT1:
14733b948ae5STrond Myklebust 		/* The client initiated a shutdown of the socket */
14747c1d71cfSTrond Myklebust 		xprt->connect_cookie++;
1475663b8858STrond Myklebust 		xprt->reestablish_timeout = 0;
14763b948ae5STrond Myklebust 		set_bit(XPRT_CLOSING, &xprt->state);
14774e857c58SPeter Zijlstra 		smp_mb__before_atomic();
14783b948ae5STrond Myklebust 		clear_bit(XPRT_CONNECTED, &xprt->state);
1479ef803670STrond Myklebust 		clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
14804e857c58SPeter Zijlstra 		smp_mb__after_atomic();
1481a246b010SChuck Lever 		break;
1482632e3bdcSTrond Myklebust 	case TCP_CLOSE_WAIT:
14833b948ae5STrond Myklebust 		/* The server initiated a shutdown of the socket */
14847c1d71cfSTrond Myklebust 		xprt->connect_cookie++;
1485d0bea455STrond Myklebust 		clear_bit(XPRT_CONNECTED, &xprt->state);
14864f8943f8STrond Myklebust 		xs_run_error_worker(transport, XPRT_SOCK_WAKE_DISCONNECT);
1487df561f66SGustavo A. R. Silva 		fallthrough;
1488663b8858STrond Myklebust 	case TCP_CLOSING:
1489663b8858STrond Myklebust 		/*
1490663b8858STrond Myklebust 		 * If the server closed down the connection, make sure that
1491663b8858STrond Myklebust 		 * we back off before reconnecting
1492663b8858STrond Myklebust 		 */
1493663b8858STrond Myklebust 		if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
1494663b8858STrond Myklebust 			xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
14953b948ae5STrond Myklebust 		break;
14963b948ae5STrond Myklebust 	case TCP_LAST_ACK:
1497670f9457STrond Myklebust 		set_bit(XPRT_CLOSING, &xprt->state);
14984e857c58SPeter Zijlstra 		smp_mb__before_atomic();
14993b948ae5STrond Myklebust 		clear_bit(XPRT_CONNECTED, &xprt->state);
15004e857c58SPeter Zijlstra 		smp_mb__after_atomic();
15013b948ae5STrond Myklebust 		break;
15023b948ae5STrond Myklebust 	case TCP_CLOSE:
15030fdea1e8STrond Myklebust 		if (test_and_clear_bit(XPRT_SOCK_CONNECTING,
15040fdea1e8STrond Myklebust 					&transport->sock_state))
15050fdea1e8STrond Myklebust 			xprt_clear_connecting(xprt);
15069b30889cSTrond Myklebust 		clear_bit(XPRT_CLOSING, &xprt->state);
15079b30889cSTrond Myklebust 		/* Trigger the socket release */
15084f8943f8STrond Myklebust 		xs_run_error_worker(transport, XPRT_SOCK_WAKE_DISCONNECT);
1509a246b010SChuck Lever 	}
1510a246b010SChuck Lever }
1511a246b010SChuck Lever 
15121f0fa154SIlpo Järvinen static void xs_write_space(struct sock *sk)
15131f0fa154SIlpo Järvinen {
15144f8943f8STrond Myklebust 	struct sock_xprt *transport;
15151f0fa154SIlpo Järvinen 	struct rpc_xprt *xprt;
15161f0fa154SIlpo Järvinen 
151713331a55STrond Myklebust 	if (!sk->sk_socket)
15181f0fa154SIlpo Järvinen 		return;
151913331a55STrond Myklebust 	clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
15201f0fa154SIlpo Järvinen 
15211f0fa154SIlpo Järvinen 	if (unlikely(!(xprt = xprt_from_sock(sk))))
15221f0fa154SIlpo Järvinen 		return;
15234f8943f8STrond Myklebust 	transport = container_of(xprt, struct sock_xprt, xprt);
15242790a624STrond Myklebust 	if (!test_and_clear_bit(XPRT_SOCK_NOSPACE, &transport->sock_state))
15252790a624STrond Myklebust 		return;
15264f8943f8STrond Myklebust 	xs_run_error_worker(transport, XPRT_SOCK_WAKE_WRITE);
1527c544577dSTrond Myklebust 	sk->sk_write_pending--;
15281f0fa154SIlpo Järvinen }
15291f0fa154SIlpo Järvinen 
15302a9e1cfaSTrond Myklebust /**
1531c7b2cae8SChuck Lever  * xs_udp_write_space - callback invoked when socket buffer space
1532c7b2cae8SChuck Lever  *                             becomes available
15339903cd1cSChuck Lever  * @sk: socket whose state has changed
15349903cd1cSChuck Lever  *
1535a246b010SChuck Lever  * Called when more output buffer space is available for this socket.
1536a246b010SChuck Lever  * We try not to wake our writers until they can make "significant"
1537c7b2cae8SChuck Lever  * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1538a246b010SChuck Lever  * with a bunch of small requests.
1539a246b010SChuck Lever  */
1540c7b2cae8SChuck Lever static void xs_udp_write_space(struct sock *sk)
1541a246b010SChuck Lever {
1542c7b2cae8SChuck Lever 	/* from net/core/sock.c:sock_def_write_space */
15431f0fa154SIlpo Järvinen 	if (sock_writeable(sk))
15441f0fa154SIlpo Järvinen 		xs_write_space(sk);
1545c7b2cae8SChuck Lever }
1546c7b2cae8SChuck Lever 
1547c7b2cae8SChuck Lever /**
1548c7b2cae8SChuck Lever  * xs_tcp_write_space - callback invoked when socket buffer space
1549c7b2cae8SChuck Lever  *                             becomes available
1550c7b2cae8SChuck Lever  * @sk: socket whose state has changed
1551c7b2cae8SChuck Lever  *
1552c7b2cae8SChuck Lever  * Called when more output buffer space is available for this socket.
1553c7b2cae8SChuck Lever  * We try not to wake our writers until they can make "significant"
1554c7b2cae8SChuck Lever  * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1555c7b2cae8SChuck Lever  * with a bunch of small requests.
1556c7b2cae8SChuck Lever  */
1557c7b2cae8SChuck Lever static void xs_tcp_write_space(struct sock *sk)
1558c7b2cae8SChuck Lever {
1559c7b2cae8SChuck Lever 	/* from net/core/stream.c:sk_stream_write_space */
156064dc6130SEric Dumazet 	if (sk_stream_is_writeable(sk))
15611f0fa154SIlpo Järvinen 		xs_write_space(sk);
1562a246b010SChuck Lever }
1563a246b010SChuck Lever 
1564470056c2SChuck Lever static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt)
1565a246b010SChuck Lever {
1566ee0ac0c2SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1567ee0ac0c2SChuck Lever 	struct sock *sk = transport->inet;
1568a246b010SChuck Lever 
15697c6e066eSChuck Lever 	if (transport->rcvsize) {
1570a246b010SChuck Lever 		sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
15717c6e066eSChuck Lever 		sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2;
1572a246b010SChuck Lever 	}
15737c6e066eSChuck Lever 	if (transport->sndsize) {
1574a246b010SChuck Lever 		sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
15757c6e066eSChuck Lever 		sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2;
1576a246b010SChuck Lever 		sk->sk_write_space(sk);
1577a246b010SChuck Lever 	}
1578a246b010SChuck Lever }
1579a246b010SChuck Lever 
158043118c29SChuck Lever /**
1581470056c2SChuck Lever  * xs_udp_set_buffer_size - set send and receive limits
158243118c29SChuck Lever  * @xprt: generic transport
1583470056c2SChuck Lever  * @sndsize: requested size of send buffer, in bytes
1584470056c2SChuck Lever  * @rcvsize: requested size of receive buffer, in bytes
158543118c29SChuck Lever  *
1586470056c2SChuck Lever  * Set socket send and receive buffer size limits.
158743118c29SChuck Lever  */
1588470056c2SChuck Lever static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize)
158943118c29SChuck Lever {
15907c6e066eSChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
15917c6e066eSChuck Lever 
15927c6e066eSChuck Lever 	transport->sndsize = 0;
1593470056c2SChuck Lever 	if (sndsize)
15947c6e066eSChuck Lever 		transport->sndsize = sndsize + 1024;
15957c6e066eSChuck Lever 	transport->rcvsize = 0;
1596470056c2SChuck Lever 	if (rcvsize)
15977c6e066eSChuck Lever 		transport->rcvsize = rcvsize + 1024;
1598470056c2SChuck Lever 
1599470056c2SChuck Lever 	xs_udp_do_set_buffer_size(xprt);
160043118c29SChuck Lever }
160143118c29SChuck Lever 
160246c0ee8bSChuck Lever /**
160346c0ee8bSChuck Lever  * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
1604acf0a39fSChuck Lever  * @xprt: controlling transport
160546c0ee8bSChuck Lever  * @task: task that timed out
160646c0ee8bSChuck Lever  *
160746c0ee8bSChuck Lever  * Adjust the congestion window after a retransmit timeout has occurred.
160846c0ee8bSChuck Lever  */
16096a24dfb6STrond Myklebust static void xs_udp_timer(struct rpc_xprt *xprt, struct rpc_task *task)
161046c0ee8bSChuck Lever {
1611b5e92419STrond Myklebust 	spin_lock(&xprt->transport_lock);
16126a24dfb6STrond Myklebust 	xprt_adjust_cwnd(xprt, task, -ETIMEDOUT);
1613b5e92419STrond Myklebust 	spin_unlock(&xprt->transport_lock);
161446c0ee8bSChuck Lever }
161546c0ee8bSChuck Lever 
1616826799e6SJ. Bruce Fields static int xs_get_random_port(void)
1617b85d8806SChuck Lever {
1618826799e6SJ. Bruce Fields 	unsigned short min = xprt_min_resvport, max = xprt_max_resvport;
1619826799e6SJ. Bruce Fields 	unsigned short range;
1620826799e6SJ. Bruce Fields 	unsigned short rand;
1621826799e6SJ. Bruce Fields 
1622826799e6SJ. Bruce Fields 	if (max < min)
1623826799e6SJ. Bruce Fields 		return -EADDRINUSE;
1624826799e6SJ. Bruce Fields 	range = max - min + 1;
16258032bf12SJason A. Donenfeld 	rand = get_random_u32_below(range);
1626826799e6SJ. Bruce Fields 	return rand + min;
1627b85d8806SChuck Lever }
1628b85d8806SChuck Lever 
16294dda9c8aSTrond Myklebust static unsigned short xs_sock_getport(struct socket *sock)
16304dda9c8aSTrond Myklebust {
16314dda9c8aSTrond Myklebust 	struct sockaddr_storage buf;
16324dda9c8aSTrond Myklebust 	unsigned short port = 0;
16334dda9c8aSTrond Myklebust 
16349b2c45d4SDenys Vlasenko 	if (kernel_getsockname(sock, (struct sockaddr *)&buf) < 0)
16354dda9c8aSTrond Myklebust 		goto out;
16364dda9c8aSTrond Myklebust 	switch (buf.ss_family) {
16374dda9c8aSTrond Myklebust 	case AF_INET6:
16384dda9c8aSTrond Myklebust 		port = ntohs(((struct sockaddr_in6 *)&buf)->sin6_port);
16394dda9c8aSTrond Myklebust 		break;
16404dda9c8aSTrond Myklebust 	case AF_INET:
16414dda9c8aSTrond Myklebust 		port = ntohs(((struct sockaddr_in *)&buf)->sin_port);
16424dda9c8aSTrond Myklebust 	}
16434dda9c8aSTrond Myklebust out:
16444dda9c8aSTrond Myklebust 	return port;
16454dda9c8aSTrond Myklebust }
16464dda9c8aSTrond Myklebust 
16474dda9c8aSTrond Myklebust /**
164892200412SChuck Lever  * xs_set_port - reset the port number in the remote endpoint address
164992200412SChuck Lever  * @xprt: generic transport
165092200412SChuck Lever  * @port: new port number
165192200412SChuck Lever  *
165292200412SChuck Lever  */
165392200412SChuck Lever static void xs_set_port(struct rpc_xprt *xprt, unsigned short port)
165492200412SChuck Lever {
165592200412SChuck Lever 	dprintk("RPC:       setting port for xprt %p to %u\n", xprt, port);
1656c4efcb1dSChuck Lever 
16579dc3b095SChuck Lever 	rpc_set_port(xs_addr(xprt), port);
16589dc3b095SChuck Lever 	xs_update_peer_port(xprt);
165992200412SChuck Lever }
166092200412SChuck Lever 
16614dda9c8aSTrond Myklebust static void xs_set_srcport(struct sock_xprt *transport, struct socket *sock)
16624dda9c8aSTrond Myklebust {
1663e6237b6fSTrond Myklebust 	if (transport->srcport == 0 && transport->xprt.reuseport)
16644dda9c8aSTrond Myklebust 		transport->srcport = xs_sock_getport(sock);
16654dda9c8aSTrond Myklebust }
16664dda9c8aSTrond Myklebust 
1667826799e6SJ. Bruce Fields static int xs_get_srcport(struct sock_xprt *transport)
166867a391d7STrond Myklebust {
1669826799e6SJ. Bruce Fields 	int port = transport->srcport;
167067a391d7STrond Myklebust 
167167a391d7STrond Myklebust 	if (port == 0 && transport->xprt.resvport)
167267a391d7STrond Myklebust 		port = xs_get_random_port();
167367a391d7STrond Myklebust 	return port;
167467a391d7STrond Myklebust }
167567a391d7STrond Myklebust 
1676421ab1beSTrond Myklebust static unsigned short xs_sock_srcport(struct rpc_xprt *xprt)
1677a8482488SOlga Kornievskaia {
1678a8482488SOlga Kornievskaia 	struct sock_xprt *sock = container_of(xprt, struct sock_xprt, xprt);
1679b49ea673SNeilBrown 	unsigned short ret = 0;
1680b49ea673SNeilBrown 	mutex_lock(&sock->recv_mutex);
1681b49ea673SNeilBrown 	if (sock->sock)
1682b49ea673SNeilBrown 		ret = xs_sock_getport(sock->sock);
1683b49ea673SNeilBrown 	mutex_unlock(&sock->recv_mutex);
1684b49ea673SNeilBrown 	return ret;
1685a8482488SOlga Kornievskaia }
1686421ab1beSTrond Myklebust 
1687421ab1beSTrond Myklebust static int xs_sock_srcaddr(struct rpc_xprt *xprt, char *buf, size_t buflen)
1688421ab1beSTrond Myklebust {
1689421ab1beSTrond Myklebust 	struct sock_xprt *sock = container_of(xprt, struct sock_xprt, xprt);
1690421ab1beSTrond Myklebust 	union {
1691421ab1beSTrond Myklebust 		struct sockaddr sa;
1692421ab1beSTrond Myklebust 		struct sockaddr_storage st;
1693421ab1beSTrond Myklebust 	} saddr;
1694421ab1beSTrond Myklebust 	int ret = -ENOTCONN;
1695421ab1beSTrond Myklebust 
1696421ab1beSTrond Myklebust 	mutex_lock(&sock->recv_mutex);
1697421ab1beSTrond Myklebust 	if (sock->sock) {
1698421ab1beSTrond Myklebust 		ret = kernel_getsockname(sock->sock, &saddr.sa);
1699421ab1beSTrond Myklebust 		if (ret >= 0)
1700421ab1beSTrond Myklebust 			ret = snprintf(buf, buflen, "%pISc", &saddr.sa);
1701421ab1beSTrond Myklebust 	}
1702421ab1beSTrond Myklebust 	mutex_unlock(&sock->recv_mutex);
1703421ab1beSTrond Myklebust 	return ret;
1704421ab1beSTrond Myklebust }
1705a8482488SOlga Kornievskaia 
1706baaf4e48SPavel Emelyanov static unsigned short xs_next_srcport(struct sock_xprt *transport, unsigned short port)
170767a391d7STrond Myklebust {
1708fbfffbd5SChuck Lever 	if (transport->srcport != 0)
1709fbfffbd5SChuck Lever 		transport->srcport = 0;
171067a391d7STrond Myklebust 	if (!transport->xprt.resvport)
171167a391d7STrond Myklebust 		return 0;
171267a391d7STrond Myklebust 	if (port <= xprt_min_resvport || port > xprt_max_resvport)
171367a391d7STrond Myklebust 		return xprt_max_resvport;
171467a391d7STrond Myklebust 	return --port;
171567a391d7STrond Myklebust }
1716beb59b68SPavel Emelyanov static int xs_bind(struct sock_xprt *transport, struct socket *sock)
1717a246b010SChuck Lever {
1718beb59b68SPavel Emelyanov 	struct sockaddr_storage myaddr;
171967a391d7STrond Myklebust 	int err, nloop = 0;
1720826799e6SJ. Bruce Fields 	int port = xs_get_srcport(transport);
172167a391d7STrond Myklebust 	unsigned short last;
1722a246b010SChuck Lever 
17230f7a622cSChris Perl 	/*
17240f7a622cSChris Perl 	 * If we are asking for any ephemeral port (i.e. port == 0 &&
17250f7a622cSChris Perl 	 * transport->xprt.resvport == 0), don't bind.  Let the local
17260f7a622cSChris Perl 	 * port selection happen implicitly when the socket is used
17270f7a622cSChris Perl 	 * (for example at connect time).
17280f7a622cSChris Perl 	 *
17290f7a622cSChris Perl 	 * This ensures that we can continue to establish TCP
17300f7a622cSChris Perl 	 * connections even when all local ephemeral ports are already
17310f7a622cSChris Perl 	 * a part of some TCP connection.  This makes no difference
173212b20ce3SBhaskar Chowdhury 	 * for UDP sockets, but also doesn't harm them.
17330f7a622cSChris Perl 	 *
17340f7a622cSChris Perl 	 * If we're asking for any reserved port (i.e. port == 0 &&
17350f7a622cSChris Perl 	 * transport->xprt.resvport == 1) xs_get_srcport above will
17360f7a622cSChris Perl 	 * ensure that port is non-zero and we will bind as needed.
17370f7a622cSChris Perl 	 */
1738826799e6SJ. Bruce Fields 	if (port <= 0)
1739826799e6SJ. Bruce Fields 		return port;
17400f7a622cSChris Perl 
1741beb59b68SPavel Emelyanov 	memcpy(&myaddr, &transport->srcaddr, transport->xprt.addrlen);
1742a246b010SChuck Lever 	do {
1743beb59b68SPavel Emelyanov 		rpc_set_port((struct sockaddr *)&myaddr, port);
1744e6242e92SSridhar Samudrala 		err = kernel_bind(sock, (struct sockaddr *)&myaddr,
1745beb59b68SPavel Emelyanov 				transport->xprt.addrlen);
1746a246b010SChuck Lever 		if (err == 0) {
1747bc1c56e9SNeilBrown 			if (transport->xprt.reuseport)
1748fbfffbd5SChuck Lever 				transport->srcport = port;
1749d3bc9a1dSFrank van Maarseveen 			break;
1750a246b010SChuck Lever 		}
175167a391d7STrond Myklebust 		last = port;
1752baaf4e48SPavel Emelyanov 		port = xs_next_srcport(transport, port);
175367a391d7STrond Myklebust 		if (port > last)
175467a391d7STrond Myklebust 			nloop++;
175567a391d7STrond Myklebust 	} while (err == -EADDRINUSE && nloop != 2);
1756beb59b68SPavel Emelyanov 
17574232e863SChuck Lever 	if (myaddr.ss_family == AF_INET)
1758beb59b68SPavel Emelyanov 		dprintk("RPC:       %s %pI4:%u: %s (%d)\n", __func__,
1759beb59b68SPavel Emelyanov 				&((struct sockaddr_in *)&myaddr)->sin_addr,
1760beb59b68SPavel Emelyanov 				port, err ? "failed" : "ok", err);
1761beb59b68SPavel Emelyanov 	else
1762beb59b68SPavel Emelyanov 		dprintk("RPC:       %s %pI6:%u: %s (%d)\n", __func__,
1763beb59b68SPavel Emelyanov 				&((struct sockaddr_in6 *)&myaddr)->sin6_addr,
17647dc753f0SChuck Lever 				port, err ? "failed" : "ok", err);
1765a246b010SChuck Lever 	return err;
1766a246b010SChuck Lever }
1767a246b010SChuck Lever 
1768176e21eeSChuck Lever /*
1769176e21eeSChuck Lever  * We don't support autobind on AF_LOCAL sockets
1770176e21eeSChuck Lever  */
1771176e21eeSChuck Lever static void xs_local_rpcbind(struct rpc_task *task)
1772176e21eeSChuck Lever {
1773fb43d172STrond Myklebust 	xprt_set_bound(task->tk_xprt);
1774176e21eeSChuck Lever }
1775176e21eeSChuck Lever 
1776176e21eeSChuck Lever static void xs_local_set_port(struct rpc_xprt *xprt, unsigned short port)
1777176e21eeSChuck Lever {
1778176e21eeSChuck Lever }
1779a246b010SChuck Lever 
1780ed07536eSPeter Zijlstra #ifdef CONFIG_DEBUG_LOCK_ALLOC
1781064a9177SNeilBrown static struct lock_class_key xs_key[3];
1782064a9177SNeilBrown static struct lock_class_key xs_slock_key[3];
1783ed07536eSPeter Zijlstra 
1784176e21eeSChuck Lever static inline void xs_reclassify_socketu(struct socket *sock)
1785176e21eeSChuck Lever {
1786176e21eeSChuck Lever 	struct sock *sk = sock->sk;
1787176e21eeSChuck Lever 
1788176e21eeSChuck Lever 	sock_lock_init_class_and_name(sk, "slock-AF_LOCAL-RPC",
1789064a9177SNeilBrown 		&xs_slock_key[0], "sk_lock-AF_LOCAL-RPC", &xs_key[0]);
1790176e21eeSChuck Lever }
1791176e21eeSChuck Lever 
17928945ee5eSChuck Lever static inline void xs_reclassify_socket4(struct socket *sock)
1793ed07536eSPeter Zijlstra {
1794ed07536eSPeter Zijlstra 	struct sock *sk = sock->sk;
17958945ee5eSChuck Lever 
17968945ee5eSChuck Lever 	sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC",
1797064a9177SNeilBrown 		&xs_slock_key[1], "sk_lock-AF_INET-RPC", &xs_key[1]);
1798ed07536eSPeter Zijlstra }
17998945ee5eSChuck Lever 
18008945ee5eSChuck Lever static inline void xs_reclassify_socket6(struct socket *sock)
18018945ee5eSChuck Lever {
18028945ee5eSChuck Lever 	struct sock *sk = sock->sk;
18038945ee5eSChuck Lever 
18048945ee5eSChuck Lever 	sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC",
1805064a9177SNeilBrown 		&xs_slock_key[2], "sk_lock-AF_INET6-RPC", &xs_key[2]);
1806ed07536eSPeter Zijlstra }
18076bc9638aSPavel Emelyanov 
18086bc9638aSPavel Emelyanov static inline void xs_reclassify_socket(int family, struct socket *sock)
18096bc9638aSPavel Emelyanov {
1810fafc4e1eSHannes Frederic Sowa 	if (WARN_ON_ONCE(!sock_allow_reclassification(sock->sk)))
18111b7a1819SWeston Andros Adamson 		return;
18121b7a1819SWeston Andros Adamson 
18134232e863SChuck Lever 	switch (family) {
1814176e21eeSChuck Lever 	case AF_LOCAL:
1815176e21eeSChuck Lever 		xs_reclassify_socketu(sock);
1816176e21eeSChuck Lever 		break;
18174232e863SChuck Lever 	case AF_INET:
18186bc9638aSPavel Emelyanov 		xs_reclassify_socket4(sock);
18194232e863SChuck Lever 		break;
18204232e863SChuck Lever 	case AF_INET6:
18216bc9638aSPavel Emelyanov 		xs_reclassify_socket6(sock);
18224232e863SChuck Lever 		break;
18234232e863SChuck Lever 	}
18246bc9638aSPavel Emelyanov }
1825ed07536eSPeter Zijlstra #else
18266bc9638aSPavel Emelyanov static inline void xs_reclassify_socket(int family, struct socket *sock)
18276bc9638aSPavel Emelyanov {
18286bc9638aSPavel Emelyanov }
1829ed07536eSPeter Zijlstra #endif
1830ed07536eSPeter Zijlstra 
183193dc41bdSNeilBrown static void xs_dummy_setup_socket(struct work_struct *work)
183293dc41bdSNeilBrown {
183393dc41bdSNeilBrown }
183493dc41bdSNeilBrown 
18356bc9638aSPavel Emelyanov static struct socket *xs_create_sock(struct rpc_xprt *xprt,
18364dda9c8aSTrond Myklebust 		struct sock_xprt *transport, int family, int type,
18374dda9c8aSTrond Myklebust 		int protocol, bool reuseport)
183822f79326SPavel Emelyanov {
1839a73881c9STrond Myklebust 	struct file *filp;
184022f79326SPavel Emelyanov 	struct socket *sock;
184122f79326SPavel Emelyanov 	int err;
184222f79326SPavel Emelyanov 
18436bc9638aSPavel Emelyanov 	err = __sock_create(xprt->xprt_net, family, type, protocol, &sock, 1);
184422f79326SPavel Emelyanov 	if (err < 0) {
184522f79326SPavel Emelyanov 		dprintk("RPC:       can't create %d transport socket (%d).\n",
184622f79326SPavel Emelyanov 				protocol, -err);
184722f79326SPavel Emelyanov 		goto out;
184822f79326SPavel Emelyanov 	}
18496bc9638aSPavel Emelyanov 	xs_reclassify_socket(family, sock);
185022f79326SPavel Emelyanov 
18514dda9c8aSTrond Myklebust 	if (reuseport)
1852fe31a326SChristoph Hellwig 		sock_set_reuseport(sock->sk);
18534dda9c8aSTrond Myklebust 
18544cea288aSBen Hutchings 	err = xs_bind(transport, sock);
18554cea288aSBen Hutchings 	if (err) {
185622f79326SPavel Emelyanov 		sock_release(sock);
185722f79326SPavel Emelyanov 		goto out;
185822f79326SPavel Emelyanov 	}
185922f79326SPavel Emelyanov 
1860a73881c9STrond Myklebust 	filp = sock_alloc_file(sock, O_NONBLOCK, NULL);
1861a73881c9STrond Myklebust 	if (IS_ERR(filp))
1862a73881c9STrond Myklebust 		return ERR_CAST(filp);
1863a73881c9STrond Myklebust 	transport->file = filp;
1864a73881c9STrond Myklebust 
186522f79326SPavel Emelyanov 	return sock;
186622f79326SPavel Emelyanov out:
186722f79326SPavel Emelyanov 	return ERR_PTR(err);
186822f79326SPavel Emelyanov }
186922f79326SPavel Emelyanov 
1870176e21eeSChuck Lever static int xs_local_finish_connecting(struct rpc_xprt *xprt,
1871176e21eeSChuck Lever 				      struct socket *sock)
1872176e21eeSChuck Lever {
1873176e21eeSChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
1874176e21eeSChuck Lever 									xprt);
1875176e21eeSChuck Lever 
1876176e21eeSChuck Lever 	if (!transport->inet) {
1877176e21eeSChuck Lever 		struct sock *sk = sock->sk;
1878176e21eeSChuck Lever 
1879ea9afca8STrond Myklebust 		lock_sock(sk);
1880176e21eeSChuck Lever 
1881176e21eeSChuck Lever 		xs_save_old_callbacks(transport, sk);
1882176e21eeSChuck Lever 
1883176e21eeSChuck Lever 		sk->sk_user_data = xprt;
1884a2648094STrond Myklebust 		sk->sk_data_ready = xs_data_ready;
1885176e21eeSChuck Lever 		sk->sk_write_space = xs_udp_write_space;
1886efce2d0bSTrond Myklebust 		sk->sk_state_change = xs_local_state_change;
18872118071dSTrond Myklebust 		sk->sk_error_report = xs_error_report;
188898123866SBenjamin Coddington 		sk->sk_use_task_frag = false;
1889176e21eeSChuck Lever 
1890176e21eeSChuck Lever 		xprt_clear_connected(xprt);
1891176e21eeSChuck Lever 
1892176e21eeSChuck Lever 		/* Reset to new socket */
1893176e21eeSChuck Lever 		transport->sock = sock;
1894176e21eeSChuck Lever 		transport->inet = sk;
1895176e21eeSChuck Lever 
1896ea9afca8STrond Myklebust 		release_sock(sk);
1897176e21eeSChuck Lever 	}
1898176e21eeSChuck Lever 
1899ae053551STrond Myklebust 	xs_stream_start_connect(transport);
19006c7a64e5STrond Myklebust 
1901176e21eeSChuck Lever 	return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, 0);
1902176e21eeSChuck Lever }
1903176e21eeSChuck Lever 
1904176e21eeSChuck Lever /**
1905176e21eeSChuck Lever  * xs_local_setup_socket - create AF_LOCAL socket, connect to a local endpoint
1906176e21eeSChuck Lever  * @transport: socket transport to connect
1907176e21eeSChuck Lever  */
1908dc107402SJ. Bruce Fields static int xs_local_setup_socket(struct sock_xprt *transport)
1909176e21eeSChuck Lever {
1910176e21eeSChuck Lever 	struct rpc_xprt *xprt = &transport->xprt;
1911a73881c9STrond Myklebust 	struct file *filp;
1912176e21eeSChuck Lever 	struct socket *sock;
191368e9a246SColin Ian King 	int status;
1914176e21eeSChuck Lever 
1915176e21eeSChuck Lever 	status = __sock_create(xprt->xprt_net, AF_LOCAL,
1916176e21eeSChuck Lever 					SOCK_STREAM, 0, &sock, 1);
1917176e21eeSChuck Lever 	if (status < 0) {
1918176e21eeSChuck Lever 		dprintk("RPC:       can't create AF_LOCAL "
1919176e21eeSChuck Lever 			"transport socket (%d).\n", -status);
1920176e21eeSChuck Lever 		goto out;
1921176e21eeSChuck Lever 	}
1922d1358917SStefan Hajnoczi 	xs_reclassify_socket(AF_LOCAL, sock);
1923176e21eeSChuck Lever 
1924a73881c9STrond Myklebust 	filp = sock_alloc_file(sock, O_NONBLOCK, NULL);
1925a73881c9STrond Myklebust 	if (IS_ERR(filp)) {
1926a73881c9STrond Myklebust 		status = PTR_ERR(filp);
1927a73881c9STrond Myklebust 		goto out;
1928a73881c9STrond Myklebust 	}
1929a73881c9STrond Myklebust 	transport->file = filp;
1930a73881c9STrond Myklebust 
1931176e21eeSChuck Lever 	dprintk("RPC:       worker connecting xprt %p via AF_LOCAL to %s\n",
1932176e21eeSChuck Lever 			xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1933176e21eeSChuck Lever 
1934176e21eeSChuck Lever 	status = xs_local_finish_connecting(xprt, sock);
193540b5ea0cSTrond Myklebust 	trace_rpc_socket_connect(xprt, sock, status);
1936176e21eeSChuck Lever 	switch (status) {
1937176e21eeSChuck Lever 	case 0:
1938176e21eeSChuck Lever 		dprintk("RPC:       xprt %p connected to %s\n",
1939176e21eeSChuck Lever 				xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
19403968a8a5SChuck Lever 		xprt->stat.connect_count++;
19413968a8a5SChuck Lever 		xprt->stat.connect_time += (long)jiffies -
19423968a8a5SChuck Lever 					   xprt->stat.connect_start;
1943176e21eeSChuck Lever 		xprt_set_connected(xprt);
194493f479d3SGustavo A. R. Silva 		break;
19453601c4a9STrond Myklebust 	case -ENOBUFS:
1946176e21eeSChuck Lever 		break;
1947176e21eeSChuck Lever 	case -ENOENT:
1948176e21eeSChuck Lever 		dprintk("RPC:       xprt %p: socket %s does not exist\n",
1949176e21eeSChuck Lever 				xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1950176e21eeSChuck Lever 		break;
19514a20a988STrond Myklebust 	case -ECONNREFUSED:
19524a20a988STrond Myklebust 		dprintk("RPC:       xprt %p: connection refused for %s\n",
19534a20a988STrond Myklebust 				xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
19544a20a988STrond Myklebust 		break;
1955176e21eeSChuck Lever 	default:
1956176e21eeSChuck Lever 		printk(KERN_ERR "%s: unhandled error (%d) connecting to %s\n",
1957176e21eeSChuck Lever 				__func__, -status,
1958176e21eeSChuck Lever 				xprt->address_strings[RPC_DISPLAY_ADDR]);
1959176e21eeSChuck Lever 	}
1960176e21eeSChuck Lever 
1961176e21eeSChuck Lever out:
1962176e21eeSChuck Lever 	xprt_clear_connecting(xprt);
1963176e21eeSChuck Lever 	xprt_wake_pending_tasks(xprt, status);
1964dc107402SJ. Bruce Fields 	return status;
1965dc107402SJ. Bruce Fields }
1966dc107402SJ. Bruce Fields 
1967b6669737SLinus Torvalds static void xs_local_connect(struct rpc_xprt *xprt, struct rpc_task *task)
1968dc107402SJ. Bruce Fields {
1969dc107402SJ. Bruce Fields 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1970dc107402SJ. Bruce Fields 	int ret;
1971dc107402SJ. Bruce Fields 
1972aad41a7dSTrond Myklebust 	if (transport->file)
1973aad41a7dSTrond Myklebust 		goto force_disconnect;
1974aad41a7dSTrond Myklebust 
1975dc107402SJ. Bruce Fields 	if (RPC_IS_ASYNC(task)) {
1976dc107402SJ. Bruce Fields 		/*
1977dc107402SJ. Bruce Fields 		 * We want the AF_LOCAL connect to be resolved in the
1978dc107402SJ. Bruce Fields 		 * filesystem namespace of the process making the rpc
1979dc107402SJ. Bruce Fields 		 * call.  Thus we connect synchronously.
1980dc107402SJ. Bruce Fields 		 *
1981dc107402SJ. Bruce Fields 		 * If we want to support asynchronous AF_LOCAL calls,
1982dc107402SJ. Bruce Fields 		 * we'll need to figure out how to pass a namespace to
1983dc107402SJ. Bruce Fields 		 * connect.
1984dc107402SJ. Bruce Fields 		 */
198539494194STrond Myklebust 		rpc_task_set_rpc_status(task, -ENOTCONN);
1986aad41a7dSTrond Myklebust 		goto out_wake;
1987dc107402SJ. Bruce Fields 	}
1988dc107402SJ. Bruce Fields 	ret = xs_local_setup_socket(transport);
1989dc107402SJ. Bruce Fields 	if (ret && !RPC_IS_SOFTCONN(task))
1990dc107402SJ. Bruce Fields 		msleep_interruptible(15000);
1991aad41a7dSTrond Myklebust 	return;
1992aad41a7dSTrond Myklebust force_disconnect:
1993aad41a7dSTrond Myklebust 	xprt_force_disconnect(xprt);
1994aad41a7dSTrond Myklebust out_wake:
1995aad41a7dSTrond Myklebust 	xprt_clear_connecting(xprt);
1996aad41a7dSTrond Myklebust 	xprt_wake_pending_tasks(xprt, -ENOTCONN);
1997176e21eeSChuck Lever }
1998176e21eeSChuck Lever 
19993c87ef6eSJeff Layton #if IS_ENABLED(CONFIG_SUNRPC_SWAP)
2000d6e971d8SJeff Layton /*
2001693486d5SNeilBrown  * Note that this should be called with XPRT_LOCKED held, or recv_mutex
2002693486d5SNeilBrown  * held, or when we otherwise know that we have exclusive access to the
2003693486d5SNeilBrown  * socket, to guard against races with xs_reset_transport.
2004d6e971d8SJeff Layton  */
2005a564b8f0SMel Gorman static void xs_set_memalloc(struct rpc_xprt *xprt)
2006a564b8f0SMel Gorman {
2007a564b8f0SMel Gorman 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
2008a564b8f0SMel Gorman 			xprt);
2009a564b8f0SMel Gorman 
2010d6e971d8SJeff Layton 	/*
2011d6e971d8SJeff Layton 	 * If there's no sock, then we have nothing to set. The
2012d6e971d8SJeff Layton 	 * reconnecting process will get it for us.
2013d6e971d8SJeff Layton 	 */
2014d6e971d8SJeff Layton 	if (!transport->inet)
2015d6e971d8SJeff Layton 		return;
20168e228133SJeff Layton 	if (atomic_read(&xprt->swapper))
2017a564b8f0SMel Gorman 		sk_set_memalloc(transport->inet);
2018a564b8f0SMel Gorman }
2019a564b8f0SMel Gorman 
2020a564b8f0SMel Gorman /**
2021d67fa4d8SJeff Layton  * xs_enable_swap - Tag this transport as being used for swap.
2022a564b8f0SMel Gorman  * @xprt: transport to tag
2023a564b8f0SMel Gorman  *
20248e228133SJeff Layton  * Take a reference to this transport on behalf of the rpc_clnt, and
20258e228133SJeff Layton  * optionally mark it for swapping if it wasn't already.
2026a564b8f0SMel Gorman  */
2027d67fa4d8SJeff Layton static int
2028d67fa4d8SJeff Layton xs_enable_swap(struct rpc_xprt *xprt)
2029a564b8f0SMel Gorman {
2030d6e971d8SJeff Layton 	struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt);
2031a564b8f0SMel Gorman 
2032693486d5SNeilBrown 	mutex_lock(&xs->recv_mutex);
2033693486d5SNeilBrown 	if (atomic_inc_return(&xprt->swapper) == 1 &&
2034693486d5SNeilBrown 	    xs->inet)
2035d6e971d8SJeff Layton 		sk_set_memalloc(xs->inet);
2036693486d5SNeilBrown 	mutex_unlock(&xs->recv_mutex);
20378e228133SJeff Layton 	return 0;
2038a564b8f0SMel Gorman }
2039a564b8f0SMel Gorman 
20408e228133SJeff Layton /**
2041d67fa4d8SJeff Layton  * xs_disable_swap - Untag this transport as being used for swap.
20428e228133SJeff Layton  * @xprt: transport to tag
20438e228133SJeff Layton  *
20448e228133SJeff Layton  * Drop a "swapper" reference to this xprt on behalf of the rpc_clnt. If the
20458e228133SJeff Layton  * swapper refcount goes to 0, untag the socket as a memalloc socket.
20468e228133SJeff Layton  */
2047d67fa4d8SJeff Layton static void
2048d67fa4d8SJeff Layton xs_disable_swap(struct rpc_xprt *xprt)
20498e228133SJeff Layton {
2050d6e971d8SJeff Layton 	struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt);
20518e228133SJeff Layton 
2052693486d5SNeilBrown 	mutex_lock(&xs->recv_mutex);
2053693486d5SNeilBrown 	if (atomic_dec_and_test(&xprt->swapper) &&
2054693486d5SNeilBrown 	    xs->inet)
2055d6e971d8SJeff Layton 		sk_clear_memalloc(xs->inet);
2056693486d5SNeilBrown 	mutex_unlock(&xs->recv_mutex);
2057a564b8f0SMel Gorman }
2058a564b8f0SMel Gorman #else
2059a564b8f0SMel Gorman static void xs_set_memalloc(struct rpc_xprt *xprt)
2060a564b8f0SMel Gorman {
2061a564b8f0SMel Gorman }
2062d67fa4d8SJeff Layton 
2063d67fa4d8SJeff Layton static int
2064d67fa4d8SJeff Layton xs_enable_swap(struct rpc_xprt *xprt)
2065d67fa4d8SJeff Layton {
2066d67fa4d8SJeff Layton 	return -EINVAL;
2067d67fa4d8SJeff Layton }
2068d67fa4d8SJeff Layton 
2069d67fa4d8SJeff Layton static void
2070d67fa4d8SJeff Layton xs_disable_swap(struct rpc_xprt *xprt)
2071d67fa4d8SJeff Layton {
2072d67fa4d8SJeff Layton }
2073a564b8f0SMel Gorman #endif
2074a564b8f0SMel Gorman 
207516be2d20SChuck Lever static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
2076a246b010SChuck Lever {
207716be2d20SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2078edb267a6SChuck Lever 
2079ee0ac0c2SChuck Lever 	if (!transport->inet) {
2080b0d93ad5SChuck Lever 		struct sock *sk = sock->sk;
2081b0d93ad5SChuck Lever 
2082ea9afca8STrond Myklebust 		lock_sock(sk);
2083b0d93ad5SChuck Lever 
20842a9e1cfaSTrond Myklebust 		xs_save_old_callbacks(transport, sk);
20852a9e1cfaSTrond Myklebust 
2086b0d93ad5SChuck Lever 		sk->sk_user_data = xprt;
2087f9b2ee71STrond Myklebust 		sk->sk_data_ready = xs_data_ready;
2088b0d93ad5SChuck Lever 		sk->sk_write_space = xs_udp_write_space;
208998123866SBenjamin Coddington 		sk->sk_use_task_frag = false;
2090b0d93ad5SChuck Lever 
2091b0d93ad5SChuck Lever 		xprt_set_connected(xprt);
2092b0d93ad5SChuck Lever 
2093b0d93ad5SChuck Lever 		/* Reset to new socket */
2094ee0ac0c2SChuck Lever 		transport->sock = sock;
2095ee0ac0c2SChuck Lever 		transport->inet = sk;
2096b0d93ad5SChuck Lever 
2097a564b8f0SMel Gorman 		xs_set_memalloc(xprt);
2098a564b8f0SMel Gorman 
2099ea9afca8STrond Myklebust 		release_sock(sk);
2100b0d93ad5SChuck Lever 	}
2101470056c2SChuck Lever 	xs_udp_do_set_buffer_size(xprt);
210202910177STrond Myklebust 
210302910177STrond Myklebust 	xprt->stat.connect_start = jiffies;
210416be2d20SChuck Lever }
210516be2d20SChuck Lever 
21068c14ff2aSPavel Emelyanov static void xs_udp_setup_socket(struct work_struct *work)
2107a246b010SChuck Lever {
2108a246b010SChuck Lever 	struct sock_xprt *transport =
2109a246b010SChuck Lever 		container_of(work, struct sock_xprt, connect_worker.work);
2110a246b010SChuck Lever 	struct rpc_xprt *xprt = &transport->xprt;
2111d099b8afSColin Ian King 	struct socket *sock;
2112b65c0310SPavel Emelyanov 	int status = -EIO;
21138db55a03SNeilBrown 	unsigned int pflags = current->flags;
2114a246b010SChuck Lever 
21158db55a03SNeilBrown 	if (atomic_read(&xprt->swapper))
21168db55a03SNeilBrown 		current->flags |= PF_MEMALLOC;
21178c14ff2aSPavel Emelyanov 	sock = xs_create_sock(xprt, transport,
21184dda9c8aSTrond Myklebust 			xs_addr(xprt)->sa_family, SOCK_DGRAM,
21194dda9c8aSTrond Myklebust 			IPPROTO_UDP, false);
2120b65c0310SPavel Emelyanov 	if (IS_ERR(sock))
2121a246b010SChuck Lever 		goto out;
212268e220bdSChuck Lever 
2123c740eff8SChuck Lever 	dprintk("RPC:       worker connecting xprt %p via %s to "
2124c740eff8SChuck Lever 				"%s (port %s)\n", xprt,
2125c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_PROTO],
2126c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_ADDR],
2127c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_PORT]);
212868e220bdSChuck Lever 
212968e220bdSChuck Lever 	xs_udp_finish_connecting(xprt, sock);
213040b5ea0cSTrond Myklebust 	trace_rpc_socket_connect(xprt, sock, 0);
2131a246b010SChuck Lever 	status = 0;
2132b0d93ad5SChuck Lever out:
2133b0d93ad5SChuck Lever 	xprt_clear_connecting(xprt);
2134cf76785dSTrond Myklebust 	xprt_unlock_connect(xprt, transport);
21357d1e8255STrond Myklebust 	xprt_wake_pending_tasks(xprt, status);
21368db55a03SNeilBrown 	current_restore_flags(pflags, PF_MEMALLOC);
2137b0d93ad5SChuck Lever }
2138b0d93ad5SChuck Lever 
21394876cc77STrond Myklebust /**
21404876cc77STrond Myklebust  * xs_tcp_shutdown - gracefully shut down a TCP socket
21414876cc77STrond Myklebust  * @xprt: transport
21424876cc77STrond Myklebust  *
21434876cc77STrond Myklebust  * Initiates a graceful shutdown of the TCP socket by calling the
21444876cc77STrond Myklebust  * equivalent of shutdown(SHUT_RDWR);
21454876cc77STrond Myklebust  */
21464876cc77STrond Myklebust static void xs_tcp_shutdown(struct rpc_xprt *xprt)
21474876cc77STrond Myklebust {
21484876cc77STrond Myklebust 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
21494876cc77STrond Myklebust 	struct socket *sock = transport->sock;
21509b30889cSTrond Myklebust 	int skst = transport->inet ? transport->inet->sk_state : TCP_CLOSE;
21514876cc77STrond Myklebust 
21524876cc77STrond Myklebust 	if (sock == NULL)
21534876cc77STrond Myklebust 		return;
21540a6ff58eSTrond Myklebust 	if (!xprt->reuseport) {
21550a6ff58eSTrond Myklebust 		xs_close(xprt);
21560a6ff58eSTrond Myklebust 		return;
21570a6ff58eSTrond Myklebust 	}
21589b30889cSTrond Myklebust 	switch (skst) {
21597c81e6a9STrond Myklebust 	case TCP_FIN_WAIT1:
21607c81e6a9STrond Myklebust 	case TCP_FIN_WAIT2:
2161*943d045aSSiddharth Kawar 	case TCP_LAST_ACK:
21627c81e6a9STrond Myklebust 		break;
21637c81e6a9STrond Myklebust 	case TCP_ESTABLISHED:
21647c81e6a9STrond Myklebust 	case TCP_CLOSE_WAIT:
21654876cc77STrond Myklebust 		kernel_sock_shutdown(sock, SHUT_RDWR);
21664876cc77STrond Myklebust 		trace_rpc_socket_shutdown(xprt, sock);
21679b30889cSTrond Myklebust 		break;
21687c81e6a9STrond Myklebust 	default:
21694876cc77STrond Myklebust 		xs_reset_transport(transport);
21704876cc77STrond Myklebust 	}
21719b30889cSTrond Myklebust }
21724876cc77STrond Myklebust 
21738d1b8c62STrond Myklebust static void xs_tcp_set_socket_timeouts(struct rpc_xprt *xprt,
21748d1b8c62STrond Myklebust 		struct socket *sock)
2175b0d93ad5SChuck Lever {
217616be2d20SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
21777196dbb0STrond Myklebust 	unsigned int keepidle;
21787196dbb0STrond Myklebust 	unsigned int keepcnt;
2179775f06abSTrond Myklebust 	unsigned int timeo;
21807f260e85STrond Myklebust 
2181b5e92419STrond Myklebust 	spin_lock(&xprt->transport_lock);
21827196dbb0STrond Myklebust 	keepidle = DIV_ROUND_UP(xprt->timeout->to_initval, HZ);
21837196dbb0STrond Myklebust 	keepcnt = xprt->timeout->to_retries + 1;
21847196dbb0STrond Myklebust 	timeo = jiffies_to_msecs(xprt->timeout->to_initval) *
21857196dbb0STrond Myklebust 		(xprt->timeout->to_retries + 1);
21867196dbb0STrond Myklebust 	clear_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state);
2187b5e92419STrond Myklebust 	spin_unlock(&xprt->transport_lock);
21887f260e85STrond Myklebust 
21897f260e85STrond Myklebust 	/* TCP Keepalive options */
2190ce3d9544SChristoph Hellwig 	sock_set_keepalive(sock->sk);
219171c48eb8SChristoph Hellwig 	tcp_sock_set_keepidle(sock->sk, keepidle);
2192d41ecaacSChristoph Hellwig 	tcp_sock_set_keepintvl(sock->sk, keepidle);
2193480aeb96SChristoph Hellwig 	tcp_sock_set_keepcnt(sock->sk, keepcnt);
2194b0d93ad5SChuck Lever 
21958d1b8c62STrond Myklebust 	/* TCP user timeout (see RFC5482) */
2196c488aeadSChristoph Hellwig 	tcp_sock_set_user_timeout(sock->sk, timeo);
21978d1b8c62STrond Myklebust }
21988d1b8c62STrond Myklebust 
21997196dbb0STrond Myklebust static void xs_tcp_set_connect_timeout(struct rpc_xprt *xprt,
22007196dbb0STrond Myklebust 		unsigned long connect_timeout,
22017196dbb0STrond Myklebust 		unsigned long reconnect_timeout)
22027196dbb0STrond Myklebust {
22037196dbb0STrond Myklebust 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
22047196dbb0STrond Myklebust 	struct rpc_timeout to;
22057196dbb0STrond Myklebust 	unsigned long initval;
22067196dbb0STrond Myklebust 
2207b5e92419STrond Myklebust 	spin_lock(&xprt->transport_lock);
22087196dbb0STrond Myklebust 	if (reconnect_timeout < xprt->max_reconnect_timeout)
22097196dbb0STrond Myklebust 		xprt->max_reconnect_timeout = reconnect_timeout;
22107196dbb0STrond Myklebust 	if (connect_timeout < xprt->connect_timeout) {
22117196dbb0STrond Myklebust 		memcpy(&to, xprt->timeout, sizeof(to));
22127196dbb0STrond Myklebust 		initval = DIV_ROUND_UP(connect_timeout, to.to_retries + 1);
22137196dbb0STrond Myklebust 		/* Arbitrary lower limit */
22147196dbb0STrond Myklebust 		if (initval <  XS_TCP_INIT_REEST_TO << 1)
22157196dbb0STrond Myklebust 			initval = XS_TCP_INIT_REEST_TO << 1;
22167196dbb0STrond Myklebust 		to.to_initval = initval;
22177196dbb0STrond Myklebust 		to.to_maxval = initval;
22187196dbb0STrond Myklebust 		memcpy(&transport->tcp_timeout, &to,
22197196dbb0STrond Myklebust 				sizeof(transport->tcp_timeout));
22207196dbb0STrond Myklebust 		xprt->timeout = &transport->tcp_timeout;
22217196dbb0STrond Myklebust 		xprt->connect_timeout = connect_timeout;
22227196dbb0STrond Myklebust 	}
22237196dbb0STrond Myklebust 	set_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state);
2224b5e92419STrond Myklebust 	spin_unlock(&xprt->transport_lock);
22257196dbb0STrond Myklebust }
22267196dbb0STrond Myklebust 
22278d1b8c62STrond Myklebust static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
22288d1b8c62STrond Myklebust {
22298d1b8c62STrond Myklebust 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
22308d1b8c62STrond Myklebust 
22318d1b8c62STrond Myklebust 	if (!transport->inet) {
22328d1b8c62STrond Myklebust 		struct sock *sk = sock->sk;
22338d1b8c62STrond Myklebust 
2234d88e4d82SNeilBrown 		/* Avoid temporary address, they are bad for long-lived
2235d88e4d82SNeilBrown 		 * connections such as NFS mounts.
2236d88e4d82SNeilBrown 		 * RFC4941, section 3.6 suggests that:
2237d88e4d82SNeilBrown 		 *    Individual applications, which have specific
2238d88e4d82SNeilBrown 		 *    knowledge about the normal duration of connections,
2239d88e4d82SNeilBrown 		 *    MAY override this as appropriate.
2240d88e4d82SNeilBrown 		 */
224118d5ad62SChristoph Hellwig 		if (xs_addr(xprt)->sa_family == PF_INET6) {
224218d5ad62SChristoph Hellwig 			ip6_sock_set_addr_preferences(sk,
224318d5ad62SChristoph Hellwig 				IPV6_PREFER_SRC_PUBLIC);
224418d5ad62SChristoph Hellwig 		}
2245d88e4d82SNeilBrown 
22468d1b8c62STrond Myklebust 		xs_tcp_set_socket_timeouts(xprt, sock);
2247d737e5d4STrond Myklebust 		tcp_sock_set_nodelay(sk);
2248775f06abSTrond Myklebust 
2249ea9afca8STrond Myklebust 		lock_sock(sk);
2250b0d93ad5SChuck Lever 
22512a9e1cfaSTrond Myklebust 		xs_save_old_callbacks(transport, sk);
22522a9e1cfaSTrond Myklebust 
2253b0d93ad5SChuck Lever 		sk->sk_user_data = xprt;
22545157b956STrond Myklebust 		sk->sk_data_ready = xs_data_ready;
2255b0d93ad5SChuck Lever 		sk->sk_state_change = xs_tcp_state_change;
2256b0d93ad5SChuck Lever 		sk->sk_write_space = xs_tcp_write_space;
22572118071dSTrond Myklebust 		sk->sk_error_report = xs_error_report;
225898123866SBenjamin Coddington 		sk->sk_use_task_frag = false;
22593167e12cSChuck Lever 
22603167e12cSChuck Lever 		/* socket options */
22613167e12cSChuck Lever 		sock_reset_flag(sk, SOCK_LINGER);
2262b0d93ad5SChuck Lever 
2263b0d93ad5SChuck Lever 		xprt_clear_connected(xprt);
2264b0d93ad5SChuck Lever 
2265b0d93ad5SChuck Lever 		/* Reset to new socket */
2266ee0ac0c2SChuck Lever 		transport->sock = sock;
2267ee0ac0c2SChuck Lever 		transport->inet = sk;
2268b0d93ad5SChuck Lever 
2269ea9afca8STrond Myklebust 		release_sock(sk);
2270b0d93ad5SChuck Lever 	}
2271b0d93ad5SChuck Lever 
227201d37c42STrond Myklebust 	if (!xprt_bound(xprt))
2273280254b6STrond Myklebust 		return -ENOTCONN;
227401d37c42STrond Myklebust 
2275a564b8f0SMel Gorman 	xs_set_memalloc(xprt);
2276a564b8f0SMel Gorman 
2277ae053551STrond Myklebust 	xs_stream_start_connect(transport);
2278e1806c7bSTrond Myklebust 
2279b0d93ad5SChuck Lever 	/* Tell the socket layer to start connecting... */
22800fdea1e8STrond Myklebust 	set_bit(XPRT_SOCK_CONNECTING, &transport->sock_state);
2281280254b6STrond Myklebust 	return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK);
228216be2d20SChuck Lever }
228316be2d20SChuck Lever 
228416be2d20SChuck Lever /**
2285b61d59ffSTrond Myklebust  * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint
2286acf0a39fSChuck Lever  * @work: queued work item
228716be2d20SChuck Lever  *
228816be2d20SChuck Lever  * Invoked by a work queue tasklet.
228916be2d20SChuck Lever  */
2290cdd518d5SPavel Emelyanov static void xs_tcp_setup_socket(struct work_struct *work)
229116be2d20SChuck Lever {
2292cdd518d5SPavel Emelyanov 	struct sock_xprt *transport =
2293cdd518d5SPavel Emelyanov 		container_of(work, struct sock_xprt, connect_worker.work);
229416be2d20SChuck Lever 	struct socket *sock = transport->sock;
2295a9f5f0f7SPavel Emelyanov 	struct rpc_xprt *xprt = &transport->xprt;
2296280254b6STrond Myklebust 	int status;
22978db55a03SNeilBrown 	unsigned int pflags = current->flags;
229816be2d20SChuck Lever 
22998db55a03SNeilBrown 	if (atomic_read(&xprt->swapper))
23008db55a03SNeilBrown 		current->flags |= PF_MEMALLOC;
230189f42494STrond Myklebust 
230289f42494STrond Myklebust 	if (xprt_connected(xprt))
230389f42494STrond Myklebust 		goto out;
230489f42494STrond Myklebust 	if (test_and_clear_bit(XPRT_SOCK_CONNECT_SENT,
230589f42494STrond Myklebust 			       &transport->sock_state) ||
230689f42494STrond Myklebust 	    !sock) {
230789f42494STrond Myklebust 		xs_reset_transport(transport);
230889f42494STrond Myklebust 		sock = xs_create_sock(xprt, transport, xs_addr(xprt)->sa_family,
230989f42494STrond Myklebust 				      SOCK_STREAM, IPPROTO_TCP, true);
2310b61d59ffSTrond Myklebust 		if (IS_ERR(sock)) {
2311280254b6STrond Myklebust 			xprt_wake_pending_tasks(xprt, PTR_ERR(sock));
231216be2d20SChuck Lever 			goto out;
231316be2d20SChuck Lever 		}
23147d1e8255STrond Myklebust 	}
23157d1e8255STrond Myklebust 
2316c740eff8SChuck Lever 	dprintk("RPC:       worker connecting xprt %p via %s to "
2317c740eff8SChuck Lever 				"%s (port %s)\n", xprt,
2318c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_PROTO],
2319c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_ADDR],
2320c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_PORT]);
232116be2d20SChuck Lever 
232216be2d20SChuck Lever 	status = xs_tcp_finish_connecting(xprt, sock);
232340b5ea0cSTrond Myklebust 	trace_rpc_socket_connect(xprt, sock, status);
2324a246b010SChuck Lever 	dprintk("RPC:       %p connect status %d connected %d sock state %d\n",
232546121cf7SChuck Lever 			xprt, -status, xprt_connected(xprt),
232646121cf7SChuck Lever 			sock->sk->sk_state);
2327a246b010SChuck Lever 	switch (status) {
23282a491991STrond Myklebust 	case 0:
2329a246b010SChuck Lever 	case -EINPROGRESS:
2330280254b6STrond Myklebust 		/* SYN_SENT! */
233189f42494STrond Myklebust 		set_bit(XPRT_SOCK_CONNECT_SENT, &transport->sock_state);
2332280254b6STrond Myklebust 		if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
2333280254b6STrond Myklebust 			xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2334280254b6STrond Myklebust 		fallthrough;
2335a246b010SChuck Lever 	case -EALREADY:
2336280254b6STrond Myklebust 		goto out_unlock;
2337280254b6STrond Myklebust 	case -EADDRNOTAVAIL:
2338280254b6STrond Myklebust 		/* Source port number is unavailable. Try a new one! */
2339280254b6STrond Myklebust 		transport->srcport = 0;
2340280254b6STrond Myklebust 		status = -EAGAIN;
2341280254b6STrond Myklebust 		break;
23429fcfe0c8STrond Myklebust 	case -EINVAL:
23439fcfe0c8STrond Myklebust 		/* Happens, for instance, if the user specified a link
23449fcfe0c8STrond Myklebust 		 * local IPv6 address without a scope-id.
23459fcfe0c8STrond Myklebust 		 */
23463ed5e2a2STrond Myklebust 	case -ECONNREFUSED:
23473ed5e2a2STrond Myklebust 	case -ECONNRESET:
2348eb5b46faSTrond Myklebust 	case -ENETDOWN:
23493ed5e2a2STrond Myklebust 	case -ENETUNREACH:
23504ba161a7STrond Myklebust 	case -EHOSTUNREACH:
23513913c78cSTrond Myklebust 	case -EADDRINUSE:
23523601c4a9STrond Myklebust 	case -ENOBUFS:
2353280254b6STrond Myklebust 		break;
2354280254b6STrond Myklebust 	default:
2355280254b6STrond Myklebust 		printk("%s: connect returned unhandled error %d\n",
2356280254b6STrond Myklebust 			__func__, status);
2357280254b6STrond Myklebust 		status = -EAGAIN;
2358280254b6STrond Myklebust 	}
2359280254b6STrond Myklebust 
23608c71139dSCalum Mackay 	/* xs_tcp_force_close() wakes tasks with a fixed error code.
23618c71139dSCalum Mackay 	 * We need to wake them first to ensure the correct error code.
23626ea44adcSNeilBrown 	 */
23636ea44adcSNeilBrown 	xprt_wake_pending_tasks(xprt, status);
23644efdd92cSTrond Myklebust 	xs_tcp_force_close(xprt);
2365a246b010SChuck Lever out:
23662226feb6SChuck Lever 	xprt_clear_connecting(xprt);
2367280254b6STrond Myklebust out_unlock:
2368cf76785dSTrond Myklebust 	xprt_unlock_connect(xprt, transport);
23698db55a03SNeilBrown 	current_restore_flags(pflags, PF_MEMALLOC);
2370a246b010SChuck Lever }
2371a246b010SChuck Lever 
237268e220bdSChuck Lever /**
23739903cd1cSChuck Lever  * xs_connect - connect a socket to a remote endpoint
23741b092092STrond Myklebust  * @xprt: pointer to transport structure
23759903cd1cSChuck Lever  * @task: address of RPC task that manages state of connect request
23769903cd1cSChuck Lever  *
23779903cd1cSChuck Lever  * TCP: If the remote end dropped the connection, delay reconnecting.
237803bf4b70SChuck Lever  *
237903bf4b70SChuck Lever  * UDP socket connects are synchronous, but we use a work queue anyway
238003bf4b70SChuck Lever  * to guarantee that even unprivileged user processes can set up a
238103bf4b70SChuck Lever  * socket on a privileged port.
238203bf4b70SChuck Lever  *
238303bf4b70SChuck Lever  * If a UDP socket connect fails, the delay behavior here prevents
238403bf4b70SChuck Lever  * retry floods (hard mounts).
23859903cd1cSChuck Lever  */
23861b092092STrond Myklebust static void xs_connect(struct rpc_xprt *xprt, struct rpc_task *task)
2387a246b010SChuck Lever {
2388ee0ac0c2SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
238902910177STrond Myklebust 	unsigned long delay = 0;
2390a246b010SChuck Lever 
2391718ba5b8STrond Myklebust 	WARN_ON_ONCE(!xprt_lock_connect(xprt, task, transport));
2392718ba5b8STrond Myklebust 
239389f42494STrond Myklebust 	if (transport->sock != NULL) {
239446121cf7SChuck Lever 		dprintk("RPC:       xs_connect delayed xprt %p for %lu "
239589f42494STrond Myklebust 			"seconds\n", xprt, xprt->reestablish_timeout / HZ);
239699b1a4c3STrond Myklebust 
2397675dd90aSChuck Lever 		delay = xprt_reconnect_delay(xprt);
2398675dd90aSChuck Lever 		xprt_reconnect_backoff(xprt, XS_TCP_INIT_REEST_TO);
239902910177STrond Myklebust 
240002910177STrond Myklebust 	} else
24019903cd1cSChuck Lever 		dprintk("RPC:       xs_connect scheduled xprt %p\n", xprt);
240202910177STrond Myklebust 
240340a5f1b1STrond Myklebust 	queue_delayed_work(xprtiod_workqueue,
240402910177STrond Myklebust 			&transport->connect_worker,
240502910177STrond Myklebust 			delay);
2406a246b010SChuck Lever }
2407a246b010SChuck Lever 
24084f8943f8STrond Myklebust static void xs_wake_disconnect(struct sock_xprt *transport)
24094f8943f8STrond Myklebust {
24104f8943f8STrond Myklebust 	if (test_and_clear_bit(XPRT_SOCK_WAKE_DISCONNECT, &transport->sock_state))
24114f8943f8STrond Myklebust 		xs_tcp_force_close(&transport->xprt);
24124f8943f8STrond Myklebust }
24134f8943f8STrond Myklebust 
24144f8943f8STrond Myklebust static void xs_wake_write(struct sock_xprt *transport)
24154f8943f8STrond Myklebust {
24164f8943f8STrond Myklebust 	if (test_and_clear_bit(XPRT_SOCK_WAKE_WRITE, &transport->sock_state))
24174f8943f8STrond Myklebust 		xprt_write_space(&transport->xprt);
24184f8943f8STrond Myklebust }
24194f8943f8STrond Myklebust 
24204f8943f8STrond Myklebust static void xs_wake_error(struct sock_xprt *transport)
24214f8943f8STrond Myklebust {
24224f8943f8STrond Myklebust 	int sockerr;
24234f8943f8STrond Myklebust 
24244f8943f8STrond Myklebust 	if (!test_bit(XPRT_SOCK_WAKE_ERROR, &transport->sock_state))
24254f8943f8STrond Myklebust 		return;
24264f8943f8STrond Myklebust 	mutex_lock(&transport->recv_mutex);
24274f8943f8STrond Myklebust 	if (transport->sock == NULL)
24284f8943f8STrond Myklebust 		goto out;
24294f8943f8STrond Myklebust 	if (!test_and_clear_bit(XPRT_SOCK_WAKE_ERROR, &transport->sock_state))
24304f8943f8STrond Myklebust 		goto out;
2431af84537dSBenjamin Coddington 	sockerr = xchg(&transport->xprt_err, 0);
24324f8943f8STrond Myklebust 	if (sockerr < 0)
24334f8943f8STrond Myklebust 		xprt_wake_pending_tasks(&transport->xprt, sockerr);
24344f8943f8STrond Myklebust out:
24354f8943f8STrond Myklebust 	mutex_unlock(&transport->recv_mutex);
24364f8943f8STrond Myklebust }
24374f8943f8STrond Myklebust 
24384f8943f8STrond Myklebust static void xs_wake_pending(struct sock_xprt *transport)
24394f8943f8STrond Myklebust {
24404f8943f8STrond Myklebust 	if (test_and_clear_bit(XPRT_SOCK_WAKE_PENDING, &transport->sock_state))
24414f8943f8STrond Myklebust 		xprt_wake_pending_tasks(&transport->xprt, -EAGAIN);
24424f8943f8STrond Myklebust }
24434f8943f8STrond Myklebust 
24444f8943f8STrond Myklebust static void xs_error_handle(struct work_struct *work)
24454f8943f8STrond Myklebust {
24464f8943f8STrond Myklebust 	struct sock_xprt *transport = container_of(work,
24474f8943f8STrond Myklebust 			struct sock_xprt, error_worker);
24484f8943f8STrond Myklebust 
24494f8943f8STrond Myklebust 	xs_wake_disconnect(transport);
24504f8943f8STrond Myklebust 	xs_wake_write(transport);
24514f8943f8STrond Myklebust 	xs_wake_error(transport);
24524f8943f8STrond Myklebust 	xs_wake_pending(transport);
24534f8943f8STrond Myklebust }
24544f8943f8STrond Myklebust 
2455262ca07dSChuck Lever /**
245612b20ce3SBhaskar Chowdhury  * xs_local_print_stats - display AF_LOCAL socket-specific stats
2457176e21eeSChuck Lever  * @xprt: rpc_xprt struct containing statistics
2458176e21eeSChuck Lever  * @seq: output file
2459176e21eeSChuck Lever  *
2460176e21eeSChuck Lever  */
2461176e21eeSChuck Lever static void xs_local_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2462176e21eeSChuck Lever {
2463176e21eeSChuck Lever 	long idle_time = 0;
2464176e21eeSChuck Lever 
2465176e21eeSChuck Lever 	if (xprt_connected(xprt))
2466176e21eeSChuck Lever 		idle_time = (long)(jiffies - xprt->last_used) / HZ;
2467176e21eeSChuck Lever 
2468176e21eeSChuck Lever 	seq_printf(seq, "\txprt:\tlocal %lu %lu %lu %ld %lu %lu %lu "
246915a45206SAndy Adamson 			"%llu %llu %lu %llu %llu\n",
2470176e21eeSChuck Lever 			xprt->stat.bind_count,
2471176e21eeSChuck Lever 			xprt->stat.connect_count,
24728440a886SChuck Lever 			xprt->stat.connect_time / HZ,
2473176e21eeSChuck Lever 			idle_time,
2474176e21eeSChuck Lever 			xprt->stat.sends,
2475176e21eeSChuck Lever 			xprt->stat.recvs,
2476176e21eeSChuck Lever 			xprt->stat.bad_xids,
2477176e21eeSChuck Lever 			xprt->stat.req_u,
247815a45206SAndy Adamson 			xprt->stat.bklog_u,
247915a45206SAndy Adamson 			xprt->stat.max_slots,
248015a45206SAndy Adamson 			xprt->stat.sending_u,
248115a45206SAndy Adamson 			xprt->stat.pending_u);
2482176e21eeSChuck Lever }
2483176e21eeSChuck Lever 
2484176e21eeSChuck Lever /**
248512b20ce3SBhaskar Chowdhury  * xs_udp_print_stats - display UDP socket-specific stats
2486262ca07dSChuck Lever  * @xprt: rpc_xprt struct containing statistics
2487262ca07dSChuck Lever  * @seq: output file
2488262ca07dSChuck Lever  *
2489262ca07dSChuck Lever  */
2490262ca07dSChuck Lever static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2491262ca07dSChuck Lever {
2492c8475461SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2493c8475461SChuck Lever 
249415a45206SAndy Adamson 	seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %llu %llu "
249515a45206SAndy Adamson 			"%lu %llu %llu\n",
2496fbfffbd5SChuck Lever 			transport->srcport,
2497262ca07dSChuck Lever 			xprt->stat.bind_count,
2498262ca07dSChuck Lever 			xprt->stat.sends,
2499262ca07dSChuck Lever 			xprt->stat.recvs,
2500262ca07dSChuck Lever 			xprt->stat.bad_xids,
2501262ca07dSChuck Lever 			xprt->stat.req_u,
250215a45206SAndy Adamson 			xprt->stat.bklog_u,
250315a45206SAndy Adamson 			xprt->stat.max_slots,
250415a45206SAndy Adamson 			xprt->stat.sending_u,
250515a45206SAndy Adamson 			xprt->stat.pending_u);
2506262ca07dSChuck Lever }
2507262ca07dSChuck Lever 
2508262ca07dSChuck Lever /**
250912b20ce3SBhaskar Chowdhury  * xs_tcp_print_stats - display TCP socket-specific stats
2510262ca07dSChuck Lever  * @xprt: rpc_xprt struct containing statistics
2511262ca07dSChuck Lever  * @seq: output file
2512262ca07dSChuck Lever  *
2513262ca07dSChuck Lever  */
2514262ca07dSChuck Lever static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2515262ca07dSChuck Lever {
2516c8475461SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2517262ca07dSChuck Lever 	long idle_time = 0;
2518262ca07dSChuck Lever 
2519262ca07dSChuck Lever 	if (xprt_connected(xprt))
2520262ca07dSChuck Lever 		idle_time = (long)(jiffies - xprt->last_used) / HZ;
2521262ca07dSChuck Lever 
252215a45206SAndy Adamson 	seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu "
252315a45206SAndy Adamson 			"%llu %llu %lu %llu %llu\n",
2524fbfffbd5SChuck Lever 			transport->srcport,
2525262ca07dSChuck Lever 			xprt->stat.bind_count,
2526262ca07dSChuck Lever 			xprt->stat.connect_count,
25278440a886SChuck Lever 			xprt->stat.connect_time / HZ,
2528262ca07dSChuck Lever 			idle_time,
2529262ca07dSChuck Lever 			xprt->stat.sends,
2530262ca07dSChuck Lever 			xprt->stat.recvs,
2531262ca07dSChuck Lever 			xprt->stat.bad_xids,
2532262ca07dSChuck Lever 			xprt->stat.req_u,
253315a45206SAndy Adamson 			xprt->stat.bklog_u,
253415a45206SAndy Adamson 			xprt->stat.max_slots,
253515a45206SAndy Adamson 			xprt->stat.sending_u,
253615a45206SAndy Adamson 			xprt->stat.pending_u);
2537262ca07dSChuck Lever }
2538262ca07dSChuck Lever 
25394cfc7e60SRahul Iyer /*
25404cfc7e60SRahul Iyer  * Allocate a bunch of pages for a scratch buffer for the rpc code. The reason
25414cfc7e60SRahul Iyer  * we allocate pages instead doing a kmalloc like rpc_malloc is because we want
25424cfc7e60SRahul Iyer  * to use the server side send routines.
25434cfc7e60SRahul Iyer  */
25445fe6eaa1SChuck Lever static int bc_malloc(struct rpc_task *task)
25454cfc7e60SRahul Iyer {
25465fe6eaa1SChuck Lever 	struct rpc_rqst *rqst = task->tk_rqstp;
25475fe6eaa1SChuck Lever 	size_t size = rqst->rq_callsize;
25484cfc7e60SRahul Iyer 	struct page *page;
25494cfc7e60SRahul Iyer 	struct rpc_buffer *buf;
25504cfc7e60SRahul Iyer 
25515fe6eaa1SChuck Lever 	if (size > PAGE_SIZE - sizeof(struct rpc_buffer)) {
25525fe6eaa1SChuck Lever 		WARN_ONCE(1, "xprtsock: large bc buffer request (size %zu)\n",
25535fe6eaa1SChuck Lever 			  size);
25545fe6eaa1SChuck Lever 		return -EINVAL;
25555fe6eaa1SChuck Lever 	}
25564cfc7e60SRahul Iyer 
2557b2648015STrond Myklebust 	page = alloc_page(GFP_KERNEL | __GFP_NORETRY | __GFP_NOWARN);
25584cfc7e60SRahul Iyer 	if (!page)
25595fe6eaa1SChuck Lever 		return -ENOMEM;
25604cfc7e60SRahul Iyer 
25614cfc7e60SRahul Iyer 	buf = page_address(page);
25624cfc7e60SRahul Iyer 	buf->len = PAGE_SIZE;
25634cfc7e60SRahul Iyer 
25645fe6eaa1SChuck Lever 	rqst->rq_buffer = buf->data;
256518e601d6SJeff Layton 	rqst->rq_rbuffer = (char *)rqst->rq_buffer + rqst->rq_callsize;
25665fe6eaa1SChuck Lever 	return 0;
25674cfc7e60SRahul Iyer }
25684cfc7e60SRahul Iyer 
25694cfc7e60SRahul Iyer /*
25704cfc7e60SRahul Iyer  * Free the space allocated in the bc_alloc routine
25714cfc7e60SRahul Iyer  */
25723435c74aSChuck Lever static void bc_free(struct rpc_task *task)
25734cfc7e60SRahul Iyer {
25743435c74aSChuck Lever 	void *buffer = task->tk_rqstp->rq_buffer;
25754cfc7e60SRahul Iyer 	struct rpc_buffer *buf;
25764cfc7e60SRahul Iyer 
25774cfc7e60SRahul Iyer 	buf = container_of(buffer, struct rpc_buffer, data);
25784cfc7e60SRahul Iyer 	free_page((unsigned long)buf);
25794cfc7e60SRahul Iyer }
25804cfc7e60SRahul Iyer 
25814cfc7e60SRahul Iyer static int bc_sendto(struct rpc_rqst *req)
25824cfc7e60SRahul Iyer {
2583da1661b9SChuck Lever 	struct xdr_buf *xdr = &req->rq_snd_buf;
25844cfc7e60SRahul Iyer 	struct sock_xprt *transport =
2585067fb11bSChuck Lever 			container_of(req->rq_xprt, struct sock_xprt, xprt);
2586067fb11bSChuck Lever 	struct msghdr msg = {
2587da1661b9SChuck Lever 		.msg_flags	= 0,
2588067fb11bSChuck Lever 	};
2589067fb11bSChuck Lever 	rpc_fraghdr marker = cpu_to_be32(RPC_LAST_STREAM_FRAGMENT |
2590da1661b9SChuck Lever 					 (u32)xdr->len);
2591da1661b9SChuck Lever 	unsigned int sent = 0;
2592da1661b9SChuck Lever 	int err;
25934cfc7e60SRahul Iyer 
25948729aabaSChuck Lever 	req->rq_xtime = ktime_get();
2595ff053dbbSTrond Myklebust 	err = xdr_alloc_bvec(xdr, rpc_task_gfp_mask());
2596ff053dbbSTrond Myklebust 	if (err < 0)
2597ff053dbbSTrond Myklebust 		return err;
2598da1661b9SChuck Lever 	err = xprt_sock_sendmsg(transport->sock, &msg, xdr, 0, marker, &sent);
2599da1661b9SChuck Lever 	xdr_free_bvec(xdr);
2600da1661b9SChuck Lever 	if (err < 0 || sent != (xdr->len + sizeof(marker)))
2601067fb11bSChuck Lever 		return -EAGAIN;
2602da1661b9SChuck Lever 	return sent;
26034cfc7e60SRahul Iyer }
26044cfc7e60SRahul Iyer 
2605ca4faf54SChuck Lever /**
2606ca4faf54SChuck Lever  * bc_send_request - Send a backchannel Call on a TCP socket
2607ca4faf54SChuck Lever  * @req: rpc_rqst containing Call message to be sent
2608ca4faf54SChuck Lever  *
2609ca4faf54SChuck Lever  * xpt_mutex ensures @rqstp's whole message is written to the socket
2610ca4faf54SChuck Lever  * without interruption.
2611ca4faf54SChuck Lever  *
2612ca4faf54SChuck Lever  * Return values:
2613ca4faf54SChuck Lever  *   %0 if the message was sent successfully
2614ca4faf54SChuck Lever  *   %ENOTCONN if the message was not sent
26154cfc7e60SRahul Iyer  */
2616adfa7144STrond Myklebust static int bc_send_request(struct rpc_rqst *req)
26174cfc7e60SRahul Iyer {
26184cfc7e60SRahul Iyer 	struct svc_xprt	*xprt;
26197fc56136SAndrzej Hajda 	int len;
26204cfc7e60SRahul Iyer 
26214cfc7e60SRahul Iyer 	/*
26224cfc7e60SRahul Iyer 	 * Get the server socket associated with this callback xprt
26234cfc7e60SRahul Iyer 	 */
26244cfc7e60SRahul Iyer 	xprt = req->rq_xprt->bc_xprt;
26254cfc7e60SRahul Iyer 
26264cfc7e60SRahul Iyer 	/*
26274cfc7e60SRahul Iyer 	 * Grab the mutex to serialize data as the connection is shared
26284cfc7e60SRahul Iyer 	 * with the fore channel
26294cfc7e60SRahul Iyer 	 */
2630c544577dSTrond Myklebust 	mutex_lock(&xprt->xpt_mutex);
26314cfc7e60SRahul Iyer 	if (test_bit(XPT_DEAD, &xprt->xpt_flags))
26324cfc7e60SRahul Iyer 		len = -ENOTCONN;
26334cfc7e60SRahul Iyer 	else
26344cfc7e60SRahul Iyer 		len = bc_sendto(req);
26354cfc7e60SRahul Iyer 	mutex_unlock(&xprt->xpt_mutex);
26364cfc7e60SRahul Iyer 
26374cfc7e60SRahul Iyer 	if (len > 0)
26384cfc7e60SRahul Iyer 		len = 0;
26394cfc7e60SRahul Iyer 
26404cfc7e60SRahul Iyer 	return len;
26414cfc7e60SRahul Iyer }
26424cfc7e60SRahul Iyer 
26434cfc7e60SRahul Iyer /*
26444cfc7e60SRahul Iyer  * The close routine. Since this is client initiated, we do nothing
26454cfc7e60SRahul Iyer  */
26464cfc7e60SRahul Iyer 
26474cfc7e60SRahul Iyer static void bc_close(struct rpc_xprt *xprt)
26484cfc7e60SRahul Iyer {
26496221f1d9SChuck Lever 	xprt_disconnect_done(xprt);
26504cfc7e60SRahul Iyer }
26514cfc7e60SRahul Iyer 
26524cfc7e60SRahul Iyer /*
26534cfc7e60SRahul Iyer  * The xprt destroy routine. Again, because this connection is client
26544cfc7e60SRahul Iyer  * initiated, we do nothing
26554cfc7e60SRahul Iyer  */
26564cfc7e60SRahul Iyer 
26574cfc7e60SRahul Iyer static void bc_destroy(struct rpc_xprt *xprt)
26584cfc7e60SRahul Iyer {
265947f72efaSKinglong Mee 	dprintk("RPC:       bc_destroy xprt %p\n", xprt);
266047f72efaSKinglong Mee 
266147f72efaSKinglong Mee 	xs_xprt_free(xprt);
266247f72efaSKinglong Mee 	module_put(THIS_MODULE);
26634cfc7e60SRahul Iyer }
26644cfc7e60SRahul Iyer 
2665d31ae254SChuck Lever static const struct rpc_xprt_ops xs_local_ops = {
2666176e21eeSChuck Lever 	.reserve_xprt		= xprt_reserve_xprt,
26674cd34e7cSTrond Myklebust 	.release_xprt		= xprt_release_xprt,
2668f39c1bfbSTrond Myklebust 	.alloc_slot		= xprt_alloc_slot,
2669a9cde23aSChuck Lever 	.free_slot		= xprt_free_slot,
2670176e21eeSChuck Lever 	.rpcbind		= xs_local_rpcbind,
2671176e21eeSChuck Lever 	.set_port		= xs_local_set_port,
2672dc107402SJ. Bruce Fields 	.connect		= xs_local_connect,
2673176e21eeSChuck Lever 	.buf_alloc		= rpc_malloc,
2674176e21eeSChuck Lever 	.buf_free		= rpc_free,
2675550aebfeSTrond Myklebust 	.prepare_request	= xs_stream_prepare_request,
2676176e21eeSChuck Lever 	.send_request		= xs_local_send_request,
26778ba6a92dSTrond Myklebust 	.wait_for_reply_request	= xprt_wait_for_reply_request_def,
2678176e21eeSChuck Lever 	.close			= xs_close,
2679a1311d87STrond Myklebust 	.destroy		= xs_destroy,
2680176e21eeSChuck Lever 	.print_stats		= xs_local_print_stats,
2681d67fa4d8SJeff Layton 	.enable_swap		= xs_enable_swap,
2682d67fa4d8SJeff Layton 	.disable_swap		= xs_disable_swap,
2683176e21eeSChuck Lever };
2684176e21eeSChuck Lever 
2685d31ae254SChuck Lever static const struct rpc_xprt_ops xs_udp_ops = {
268643118c29SChuck Lever 	.set_buffer_size	= xs_udp_set_buffer_size,
268712a80469SChuck Lever 	.reserve_xprt		= xprt_reserve_xprt_cong,
268849e9a890SChuck Lever 	.release_xprt		= xprt_release_xprt_cong,
2689f39c1bfbSTrond Myklebust 	.alloc_slot		= xprt_alloc_slot,
2690a9cde23aSChuck Lever 	.free_slot		= xprt_free_slot,
269145160d62SChuck Lever 	.rpcbind		= rpcb_getport_async,
269292200412SChuck Lever 	.set_port		= xs_set_port,
26939903cd1cSChuck Lever 	.connect		= xs_connect,
2694421ab1beSTrond Myklebust 	.get_srcaddr		= xs_sock_srcaddr,
2695421ab1beSTrond Myklebust 	.get_srcport		= xs_sock_srcport,
269602107148SChuck Lever 	.buf_alloc		= rpc_malloc,
269702107148SChuck Lever 	.buf_free		= rpc_free,
2698262965f5SChuck Lever 	.send_request		= xs_udp_send_request,
26998ba6a92dSTrond Myklebust 	.wait_for_reply_request	= xprt_wait_for_reply_request_rtt,
270046c0ee8bSChuck Lever 	.timer			= xs_udp_timer,
2701a58dd398SChuck Lever 	.release_request	= xprt_release_rqst_cong,
2702262965f5SChuck Lever 	.close			= xs_close,
2703262965f5SChuck Lever 	.destroy		= xs_destroy,
2704262ca07dSChuck Lever 	.print_stats		= xs_udp_print_stats,
2705d67fa4d8SJeff Layton 	.enable_swap		= xs_enable_swap,
2706d67fa4d8SJeff Layton 	.disable_swap		= xs_disable_swap,
27074a068258SChuck Lever 	.inject_disconnect	= xs_inject_disconnect,
2708262965f5SChuck Lever };
2709262965f5SChuck Lever 
2710d31ae254SChuck Lever static const struct rpc_xprt_ops xs_tcp_ops = {
271112a80469SChuck Lever 	.reserve_xprt		= xprt_reserve_xprt,
27124cd34e7cSTrond Myklebust 	.release_xprt		= xprt_release_xprt,
271336bd7de9STrond Myklebust 	.alloc_slot		= xprt_alloc_slot,
2714a9cde23aSChuck Lever 	.free_slot		= xprt_free_slot,
271545160d62SChuck Lever 	.rpcbind		= rpcb_getport_async,
271692200412SChuck Lever 	.set_port		= xs_set_port,
27170b9e7943STrond Myklebust 	.connect		= xs_connect,
2718421ab1beSTrond Myklebust 	.get_srcaddr		= xs_sock_srcaddr,
2719421ab1beSTrond Myklebust 	.get_srcport		= xs_sock_srcport,
272002107148SChuck Lever 	.buf_alloc		= rpc_malloc,
272102107148SChuck Lever 	.buf_free		= rpc_free,
2722277e4ab7STrond Myklebust 	.prepare_request	= xs_stream_prepare_request,
2723262965f5SChuck Lever 	.send_request		= xs_tcp_send_request,
27248ba6a92dSTrond Myklebust 	.wait_for_reply_request	= xprt_wait_for_reply_request_def,
2725c627d31bSTrond Myklebust 	.close			= xs_tcp_shutdown,
27269903cd1cSChuck Lever 	.destroy		= xs_destroy,
27277196dbb0STrond Myklebust 	.set_connect_timeout	= xs_tcp_set_connect_timeout,
2728262ca07dSChuck Lever 	.print_stats		= xs_tcp_print_stats,
2729d67fa4d8SJeff Layton 	.enable_swap		= xs_enable_swap,
2730d67fa4d8SJeff Layton 	.disable_swap		= xs_disable_swap,
27314a068258SChuck Lever 	.inject_disconnect	= xs_inject_disconnect,
273242e5c3e2SChuck Lever #ifdef CONFIG_SUNRPC_BACKCHANNEL
273342e5c3e2SChuck Lever 	.bc_setup		= xprt_setup_bc,
27346b26cc8cSChuck Lever 	.bc_maxpayload		= xs_tcp_bc_maxpayload,
27357402a4feSTrond Myklebust 	.bc_num_slots		= xprt_bc_max_slots,
273642e5c3e2SChuck Lever 	.bc_free_rqst		= xprt_free_bc_rqst,
273742e5c3e2SChuck Lever 	.bc_destroy		= xprt_destroy_bc,
273842e5c3e2SChuck Lever #endif
2739a246b010SChuck Lever };
2740a246b010SChuck Lever 
27414cfc7e60SRahul Iyer /*
27424cfc7e60SRahul Iyer  * The rpc_xprt_ops for the server backchannel
27434cfc7e60SRahul Iyer  */
27444cfc7e60SRahul Iyer 
2745d31ae254SChuck Lever static const struct rpc_xprt_ops bc_tcp_ops = {
27464cfc7e60SRahul Iyer 	.reserve_xprt		= xprt_reserve_xprt,
27474cfc7e60SRahul Iyer 	.release_xprt		= xprt_release_xprt,
274884e28a30SBryan Schumaker 	.alloc_slot		= xprt_alloc_slot,
2749a9cde23aSChuck Lever 	.free_slot		= xprt_free_slot,
27504cfc7e60SRahul Iyer 	.buf_alloc		= bc_malloc,
27514cfc7e60SRahul Iyer 	.buf_free		= bc_free,
27524cfc7e60SRahul Iyer 	.send_request		= bc_send_request,
27538ba6a92dSTrond Myklebust 	.wait_for_reply_request	= xprt_wait_for_reply_request_def,
27544cfc7e60SRahul Iyer 	.close			= bc_close,
27554cfc7e60SRahul Iyer 	.destroy		= bc_destroy,
27564cfc7e60SRahul Iyer 	.print_stats		= xs_tcp_print_stats,
2757d67fa4d8SJeff Layton 	.enable_swap		= xs_enable_swap,
2758d67fa4d8SJeff Layton 	.disable_swap		= xs_disable_swap,
27594a068258SChuck Lever 	.inject_disconnect	= xs_inject_disconnect,
27604cfc7e60SRahul Iyer };
27614cfc7e60SRahul Iyer 
276292476850SChuck Lever static int xs_init_anyaddr(const int family, struct sockaddr *sap)
276392476850SChuck Lever {
276492476850SChuck Lever 	static const struct sockaddr_in sin = {
276592476850SChuck Lever 		.sin_family		= AF_INET,
276692476850SChuck Lever 		.sin_addr.s_addr	= htonl(INADDR_ANY),
276792476850SChuck Lever 	};
276892476850SChuck Lever 	static const struct sockaddr_in6 sin6 = {
276992476850SChuck Lever 		.sin6_family		= AF_INET6,
277092476850SChuck Lever 		.sin6_addr		= IN6ADDR_ANY_INIT,
277192476850SChuck Lever 	};
277292476850SChuck Lever 
277392476850SChuck Lever 	switch (family) {
2774176e21eeSChuck Lever 	case AF_LOCAL:
2775176e21eeSChuck Lever 		break;
277692476850SChuck Lever 	case AF_INET:
277792476850SChuck Lever 		memcpy(sap, &sin, sizeof(sin));
277892476850SChuck Lever 		break;
277992476850SChuck Lever 	case AF_INET6:
278092476850SChuck Lever 		memcpy(sap, &sin6, sizeof(sin6));
278192476850SChuck Lever 		break;
278292476850SChuck Lever 	default:
278392476850SChuck Lever 		dprintk("RPC:       %s: Bad address family\n", __func__);
278492476850SChuck Lever 		return -EAFNOSUPPORT;
278592476850SChuck Lever 	}
278692476850SChuck Lever 	return 0;
278792476850SChuck Lever }
278892476850SChuck Lever 
27893c341b0bS\"Talpey, Thomas\ static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args,
2790d9ba131dSTrond Myklebust 				      unsigned int slot_table_size,
2791d9ba131dSTrond Myklebust 				      unsigned int max_slot_table_size)
2792c8541ecdSChuck Lever {
2793c8541ecdSChuck Lever 	struct rpc_xprt *xprt;
2794ffc2e518SChuck Lever 	struct sock_xprt *new;
2795c8541ecdSChuck Lever 
279696802a09SFrank van Maarseveen 	if (args->addrlen > sizeof(xprt->addr)) {
2797c8541ecdSChuck Lever 		dprintk("RPC:       xs_setup_xprt: address too large\n");
2798c8541ecdSChuck Lever 		return ERR_PTR(-EBADF);
2799c8541ecdSChuck Lever 	}
2800c8541ecdSChuck Lever 
2801d9ba131dSTrond Myklebust 	xprt = xprt_alloc(args->net, sizeof(*new), slot_table_size,
2802d9ba131dSTrond Myklebust 			max_slot_table_size);
2803bd1722d4SPavel Emelyanov 	if (xprt == NULL) {
280446121cf7SChuck Lever 		dprintk("RPC:       xs_setup_xprt: couldn't allocate "
280546121cf7SChuck Lever 				"rpc_xprt\n");
2806c8541ecdSChuck Lever 		return ERR_PTR(-ENOMEM);
2807c8541ecdSChuck Lever 	}
2808c8541ecdSChuck Lever 
2809bd1722d4SPavel Emelyanov 	new = container_of(xprt, struct sock_xprt, xprt);
2810edc1b01cSTrond Myklebust 	mutex_init(&new->recv_mutex);
281196802a09SFrank van Maarseveen 	memcpy(&xprt->addr, args->dstaddr, args->addrlen);
281296802a09SFrank van Maarseveen 	xprt->addrlen = args->addrlen;
2813d3bc9a1dSFrank van Maarseveen 	if (args->srcaddr)
2814fbfffbd5SChuck Lever 		memcpy(&new->srcaddr, args->srcaddr, args->addrlen);
281592476850SChuck Lever 	else {
281692476850SChuck Lever 		int err;
281792476850SChuck Lever 		err = xs_init_anyaddr(args->dstaddr->sa_family,
281892476850SChuck Lever 					(struct sockaddr *)&new->srcaddr);
28192aa13531SStanislav Kinsbursky 		if (err != 0) {
28202aa13531SStanislav Kinsbursky 			xprt_free(xprt);
282192476850SChuck Lever 			return ERR_PTR(err);
282292476850SChuck Lever 		}
28232aa13531SStanislav Kinsbursky 	}
2824c8541ecdSChuck Lever 
2825c8541ecdSChuck Lever 	return xprt;
2826c8541ecdSChuck Lever }
2827c8541ecdSChuck Lever 
2828176e21eeSChuck Lever static const struct rpc_timeout xs_local_default_timeout = {
2829176e21eeSChuck Lever 	.to_initval = 10 * HZ,
2830176e21eeSChuck Lever 	.to_maxval = 10 * HZ,
2831176e21eeSChuck Lever 	.to_retries = 2,
2832176e21eeSChuck Lever };
2833176e21eeSChuck Lever 
2834176e21eeSChuck Lever /**
2835176e21eeSChuck Lever  * xs_setup_local - Set up transport to use an AF_LOCAL socket
2836176e21eeSChuck Lever  * @args: rpc transport creation arguments
2837176e21eeSChuck Lever  *
2838176e21eeSChuck Lever  * AF_LOCAL is a "tpi_cots_ord" transport, just like TCP
2839176e21eeSChuck Lever  */
2840176e21eeSChuck Lever static struct rpc_xprt *xs_setup_local(struct xprt_create *args)
2841176e21eeSChuck Lever {
2842176e21eeSChuck Lever 	struct sockaddr_un *sun = (struct sockaddr_un *)args->dstaddr;
2843176e21eeSChuck Lever 	struct sock_xprt *transport;
2844176e21eeSChuck Lever 	struct rpc_xprt *xprt;
2845176e21eeSChuck Lever 	struct rpc_xprt *ret;
2846176e21eeSChuck Lever 
2847d9ba131dSTrond Myklebust 	xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
2848d9ba131dSTrond Myklebust 			xprt_max_tcp_slot_table_entries);
2849176e21eeSChuck Lever 	if (IS_ERR(xprt))
2850176e21eeSChuck Lever 		return xprt;
2851176e21eeSChuck Lever 	transport = container_of(xprt, struct sock_xprt, xprt);
2852176e21eeSChuck Lever 
2853176e21eeSChuck Lever 	xprt->prot = 0;
2854d3abc739SOlga Kornievskaia 	xprt->xprt_class = &xs_local_transport;
2855176e21eeSChuck Lever 	xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2856176e21eeSChuck Lever 
2857176e21eeSChuck Lever 	xprt->bind_timeout = XS_BIND_TO;
2858176e21eeSChuck Lever 	xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2859176e21eeSChuck Lever 	xprt->idle_timeout = XS_IDLE_DISC_TO;
2860176e21eeSChuck Lever 
2861176e21eeSChuck Lever 	xprt->ops = &xs_local_ops;
2862176e21eeSChuck Lever 	xprt->timeout = &xs_local_default_timeout;
2863176e21eeSChuck Lever 
2864550aebfeSTrond Myklebust 	INIT_WORK(&transport->recv_worker, xs_stream_data_receive_workfn);
28654f8943f8STrond Myklebust 	INIT_WORK(&transport->error_worker, xs_error_handle);
2866550aebfeSTrond Myklebust 	INIT_DELAYED_WORK(&transport->connect_worker, xs_dummy_setup_socket);
286793dc41bdSNeilBrown 
2868176e21eeSChuck Lever 	switch (sun->sun_family) {
2869176e21eeSChuck Lever 	case AF_LOCAL:
2870176e21eeSChuck Lever 		if (sun->sun_path[0] != '/') {
2871176e21eeSChuck Lever 			dprintk("RPC:       bad AF_LOCAL address: %s\n",
2872176e21eeSChuck Lever 					sun->sun_path);
2873176e21eeSChuck Lever 			ret = ERR_PTR(-EINVAL);
2874176e21eeSChuck Lever 			goto out_err;
2875176e21eeSChuck Lever 		}
2876176e21eeSChuck Lever 		xprt_set_bound(xprt);
2877176e21eeSChuck Lever 		xs_format_peer_addresses(xprt, "local", RPCBIND_NETID_LOCAL);
2878176e21eeSChuck Lever 		break;
2879176e21eeSChuck Lever 	default:
2880176e21eeSChuck Lever 		ret = ERR_PTR(-EAFNOSUPPORT);
2881176e21eeSChuck Lever 		goto out_err;
2882176e21eeSChuck Lever 	}
2883176e21eeSChuck Lever 
2884176e21eeSChuck Lever 	dprintk("RPC:       set up xprt to %s via AF_LOCAL\n",
2885176e21eeSChuck Lever 			xprt->address_strings[RPC_DISPLAY_ADDR]);
2886176e21eeSChuck Lever 
2887176e21eeSChuck Lever 	if (try_module_get(THIS_MODULE))
2888176e21eeSChuck Lever 		return xprt;
2889176e21eeSChuck Lever 	ret = ERR_PTR(-EINVAL);
2890176e21eeSChuck Lever out_err:
2891315f3812SKinglong Mee 	xs_xprt_free(xprt);
2892176e21eeSChuck Lever 	return ret;
2893176e21eeSChuck Lever }
2894176e21eeSChuck Lever 
28952881ae74STrond Myklebust static const struct rpc_timeout xs_udp_default_timeout = {
28962881ae74STrond Myklebust 	.to_initval = 5 * HZ,
28972881ae74STrond Myklebust 	.to_maxval = 30 * HZ,
28982881ae74STrond Myklebust 	.to_increment = 5 * HZ,
28992881ae74STrond Myklebust 	.to_retries = 5,
29002881ae74STrond Myklebust };
29012881ae74STrond Myklebust 
29029903cd1cSChuck Lever /**
29039903cd1cSChuck Lever  * xs_setup_udp - Set up transport to use a UDP socket
290496802a09SFrank van Maarseveen  * @args: rpc transport creation arguments
29059903cd1cSChuck Lever  *
29069903cd1cSChuck Lever  */
2907483066d6SAdrian Bunk static struct rpc_xprt *xs_setup_udp(struct xprt_create *args)
2908a246b010SChuck Lever {
29098f9d5b1aSChuck Lever 	struct sockaddr *addr = args->dstaddr;
2910c8541ecdSChuck Lever 	struct rpc_xprt *xprt;
2911c8475461SChuck Lever 	struct sock_xprt *transport;
29120a68b0beSJ. Bruce Fields 	struct rpc_xprt *ret;
2913a246b010SChuck Lever 
2914d9ba131dSTrond Myklebust 	xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries,
2915d9ba131dSTrond Myklebust 			xprt_udp_slot_table_entries);
2916c8541ecdSChuck Lever 	if (IS_ERR(xprt))
2917c8541ecdSChuck Lever 		return xprt;
2918c8475461SChuck Lever 	transport = container_of(xprt, struct sock_xprt, xprt);
2919a246b010SChuck Lever 
2920ec739ef0SChuck Lever 	xprt->prot = IPPROTO_UDP;
2921d3abc739SOlga Kornievskaia 	xprt->xprt_class = &xs_udp_transport;
2922a246b010SChuck Lever 	/* XXX: header size can vary due to auth type, IPv6, etc. */
2923a246b010SChuck Lever 	xprt->max_payload = (1U << 16) - (MAX_HEADER << 3);
2924a246b010SChuck Lever 
292503bf4b70SChuck Lever 	xprt->bind_timeout = XS_BIND_TO;
292603bf4b70SChuck Lever 	xprt->reestablish_timeout = XS_UDP_REEST_TO;
292703bf4b70SChuck Lever 	xprt->idle_timeout = XS_IDLE_DISC_TO;
2928a246b010SChuck Lever 
2929262965f5SChuck Lever 	xprt->ops = &xs_udp_ops;
2930a246b010SChuck Lever 
2931ba7392bbSTrond Myklebust 	xprt->timeout = &xs_udp_default_timeout;
2932a246b010SChuck Lever 
2933f9b2ee71STrond Myklebust 	INIT_WORK(&transport->recv_worker, xs_udp_data_receive_workfn);
29344f8943f8STrond Myklebust 	INIT_WORK(&transport->error_worker, xs_error_handle);
2935edc1b01cSTrond Myklebust 	INIT_DELAYED_WORK(&transport->connect_worker, xs_udp_setup_socket);
2936edc1b01cSTrond Myklebust 
29378f9d5b1aSChuck Lever 	switch (addr->sa_family) {
29388f9d5b1aSChuck Lever 	case AF_INET:
29398f9d5b1aSChuck Lever 		if (((struct sockaddr_in *)addr)->sin_port != htons(0))
29408f9d5b1aSChuck Lever 			xprt_set_bound(xprt);
29418f9d5b1aSChuck Lever 
29429dc3b095SChuck Lever 		xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP);
29438f9d5b1aSChuck Lever 		break;
29448f9d5b1aSChuck Lever 	case AF_INET6:
29458f9d5b1aSChuck Lever 		if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
29468f9d5b1aSChuck Lever 			xprt_set_bound(xprt);
29478f9d5b1aSChuck Lever 
29489dc3b095SChuck Lever 		xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6);
29498f9d5b1aSChuck Lever 		break;
29508f9d5b1aSChuck Lever 	default:
29510a68b0beSJ. Bruce Fields 		ret = ERR_PTR(-EAFNOSUPPORT);
29520a68b0beSJ. Bruce Fields 		goto out_err;
29538f9d5b1aSChuck Lever 	}
29548f9d5b1aSChuck Lever 
2955c740eff8SChuck Lever 	if (xprt_bound(xprt))
2956c740eff8SChuck Lever 		dprintk("RPC:       set up xprt to %s (port %s) via %s\n",
2957c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_ADDR],
2958c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_PORT],
2959c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_PROTO]);
2960c740eff8SChuck Lever 	else
2961c740eff8SChuck Lever 		dprintk("RPC:       set up xprt to %s (autobind) via %s\n",
2962c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_ADDR],
2963c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_PROTO]);
2964edb267a6SChuck Lever 
2965bc25571eS\"Talpey, Thomas\ 	if (try_module_get(THIS_MODULE))
2966c8541ecdSChuck Lever 		return xprt;
29670a68b0beSJ. Bruce Fields 	ret = ERR_PTR(-EINVAL);
29680a68b0beSJ. Bruce Fields out_err:
2969315f3812SKinglong Mee 	xs_xprt_free(xprt);
29700a68b0beSJ. Bruce Fields 	return ret;
2971a246b010SChuck Lever }
2972a246b010SChuck Lever 
29732881ae74STrond Myklebust static const struct rpc_timeout xs_tcp_default_timeout = {
29742881ae74STrond Myklebust 	.to_initval = 60 * HZ,
29752881ae74STrond Myklebust 	.to_maxval = 60 * HZ,
29762881ae74STrond Myklebust 	.to_retries = 2,
29772881ae74STrond Myklebust };
29782881ae74STrond Myklebust 
29799903cd1cSChuck Lever /**
29809903cd1cSChuck Lever  * xs_setup_tcp - Set up transport to use a TCP socket
298196802a09SFrank van Maarseveen  * @args: rpc transport creation arguments
29829903cd1cSChuck Lever  *
29839903cd1cSChuck Lever  */
2984483066d6SAdrian Bunk static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args)
2985a246b010SChuck Lever {
29868f9d5b1aSChuck Lever 	struct sockaddr *addr = args->dstaddr;
2987c8541ecdSChuck Lever 	struct rpc_xprt *xprt;
2988c8475461SChuck Lever 	struct sock_xprt *transport;
29890a68b0beSJ. Bruce Fields 	struct rpc_xprt *ret;
2990b7993cebSTrond Myklebust 	unsigned int max_slot_table_size = xprt_max_tcp_slot_table_entries;
2991b7993cebSTrond Myklebust 
2992b7993cebSTrond Myklebust 	if (args->flags & XPRT_CREATE_INFINITE_SLOTS)
2993b7993cebSTrond Myklebust 		max_slot_table_size = RPC_MAX_SLOT_TABLE_LIMIT;
2994a246b010SChuck Lever 
2995d9ba131dSTrond Myklebust 	xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
2996b7993cebSTrond Myklebust 			max_slot_table_size);
2997c8541ecdSChuck Lever 	if (IS_ERR(xprt))
2998c8541ecdSChuck Lever 		return xprt;
2999c8475461SChuck Lever 	transport = container_of(xprt, struct sock_xprt, xprt);
3000a246b010SChuck Lever 
3001ec739ef0SChuck Lever 	xprt->prot = IPPROTO_TCP;
3002d3abc739SOlga Kornievskaia 	xprt->xprt_class = &xs_tcp_transport;
3003808012fbSChuck Lever 	xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
3004a246b010SChuck Lever 
300503bf4b70SChuck Lever 	xprt->bind_timeout = XS_BIND_TO;
300603bf4b70SChuck Lever 	xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
300703bf4b70SChuck Lever 	xprt->idle_timeout = XS_IDLE_DISC_TO;
3008a246b010SChuck Lever 
3009262965f5SChuck Lever 	xprt->ops = &xs_tcp_ops;
3010ba7392bbSTrond Myklebust 	xprt->timeout = &xs_tcp_default_timeout;
3011a246b010SChuck Lever 
30123851f1cdSTrond Myklebust 	xprt->max_reconnect_timeout = xprt->timeout->to_maxval;
30137196dbb0STrond Myklebust 	xprt->connect_timeout = xprt->timeout->to_initval *
30147196dbb0STrond Myklebust 		(xprt->timeout->to_retries + 1);
30153851f1cdSTrond Myklebust 
3016c50b8ee0STrond Myklebust 	INIT_WORK(&transport->recv_worker, xs_stream_data_receive_workfn);
30174f8943f8STrond Myklebust 	INIT_WORK(&transport->error_worker, xs_error_handle);
3018edc1b01cSTrond Myklebust 	INIT_DELAYED_WORK(&transport->connect_worker, xs_tcp_setup_socket);
3019edc1b01cSTrond Myklebust 
30208f9d5b1aSChuck Lever 	switch (addr->sa_family) {
30218f9d5b1aSChuck Lever 	case AF_INET:
30228f9d5b1aSChuck Lever 		if (((struct sockaddr_in *)addr)->sin_port != htons(0))
30238f9d5b1aSChuck Lever 			xprt_set_bound(xprt);
30248f9d5b1aSChuck Lever 
30259dc3b095SChuck Lever 		xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP);
30268f9d5b1aSChuck Lever 		break;
30278f9d5b1aSChuck Lever 	case AF_INET6:
30288f9d5b1aSChuck Lever 		if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
30298f9d5b1aSChuck Lever 			xprt_set_bound(xprt);
30308f9d5b1aSChuck Lever 
30319dc3b095SChuck Lever 		xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6);
30328f9d5b1aSChuck Lever 		break;
30338f9d5b1aSChuck Lever 	default:
30340a68b0beSJ. Bruce Fields 		ret = ERR_PTR(-EAFNOSUPPORT);
30350a68b0beSJ. Bruce Fields 		goto out_err;
30368f9d5b1aSChuck Lever 	}
30378f9d5b1aSChuck Lever 
3038c740eff8SChuck Lever 	if (xprt_bound(xprt))
3039c740eff8SChuck Lever 		dprintk("RPC:       set up xprt to %s (port %s) via %s\n",
3040c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_ADDR],
3041c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_PORT],
3042c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_PROTO]);
3043c740eff8SChuck Lever 	else
3044c740eff8SChuck Lever 		dprintk("RPC:       set up xprt to %s (autobind) via %s\n",
3045c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_ADDR],
3046c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_PROTO]);
3047c740eff8SChuck Lever 
3048bc25571eS\"Talpey, Thomas\ 	if (try_module_get(THIS_MODULE))
3049c8541ecdSChuck Lever 		return xprt;
30500a68b0beSJ. Bruce Fields 	ret = ERR_PTR(-EINVAL);
30510a68b0beSJ. Bruce Fields out_err:
3052315f3812SKinglong Mee 	xs_xprt_free(xprt);
30530a68b0beSJ. Bruce Fields 	return ret;
3054a246b010SChuck Lever }
3055282b32e1SChuck Lever 
3056f300babaSAlexandros Batsakis /**
3057f300babaSAlexandros Batsakis  * xs_setup_bc_tcp - Set up transport to use a TCP backchannel socket
3058f300babaSAlexandros Batsakis  * @args: rpc transport creation arguments
3059f300babaSAlexandros Batsakis  *
3060f300babaSAlexandros Batsakis  */
3061f300babaSAlexandros Batsakis static struct rpc_xprt *xs_setup_bc_tcp(struct xprt_create *args)
3062f300babaSAlexandros Batsakis {
3063f300babaSAlexandros Batsakis 	struct sockaddr *addr = args->dstaddr;
3064f300babaSAlexandros Batsakis 	struct rpc_xprt *xprt;
3065f300babaSAlexandros Batsakis 	struct sock_xprt *transport;
3066f300babaSAlexandros Batsakis 	struct svc_sock *bc_sock;
30670a68b0beSJ. Bruce Fields 	struct rpc_xprt *ret;
3068f300babaSAlexandros Batsakis 
3069d9ba131dSTrond Myklebust 	xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
3070d9ba131dSTrond Myklebust 			xprt_tcp_slot_table_entries);
3071f300babaSAlexandros Batsakis 	if (IS_ERR(xprt))
3072f300babaSAlexandros Batsakis 		return xprt;
3073f300babaSAlexandros Batsakis 	transport = container_of(xprt, struct sock_xprt, xprt);
3074f300babaSAlexandros Batsakis 
3075f300babaSAlexandros Batsakis 	xprt->prot = IPPROTO_TCP;
3076d3abc739SOlga Kornievskaia 	xprt->xprt_class = &xs_bc_tcp_transport;
3077f300babaSAlexandros Batsakis 	xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
3078f300babaSAlexandros Batsakis 	xprt->timeout = &xs_tcp_default_timeout;
3079f300babaSAlexandros Batsakis 
3080f300babaSAlexandros Batsakis 	/* backchannel */
3081f300babaSAlexandros Batsakis 	xprt_set_bound(xprt);
3082f300babaSAlexandros Batsakis 	xprt->bind_timeout = 0;
3083f300babaSAlexandros Batsakis 	xprt->reestablish_timeout = 0;
3084f300babaSAlexandros Batsakis 	xprt->idle_timeout = 0;
3085f300babaSAlexandros Batsakis 
3086f300babaSAlexandros Batsakis 	xprt->ops = &bc_tcp_ops;
3087f300babaSAlexandros Batsakis 
3088f300babaSAlexandros Batsakis 	switch (addr->sa_family) {
3089f300babaSAlexandros Batsakis 	case AF_INET:
3090f300babaSAlexandros Batsakis 		xs_format_peer_addresses(xprt, "tcp",
3091f300babaSAlexandros Batsakis 					 RPCBIND_NETID_TCP);
3092f300babaSAlexandros Batsakis 		break;
3093f300babaSAlexandros Batsakis 	case AF_INET6:
3094f300babaSAlexandros Batsakis 		xs_format_peer_addresses(xprt, "tcp",
3095f300babaSAlexandros Batsakis 				   RPCBIND_NETID_TCP6);
3096f300babaSAlexandros Batsakis 		break;
3097f300babaSAlexandros Batsakis 	default:
30980a68b0beSJ. Bruce Fields 		ret = ERR_PTR(-EAFNOSUPPORT);
30990a68b0beSJ. Bruce Fields 		goto out_err;
3100f300babaSAlexandros Batsakis 	}
3101f300babaSAlexandros Batsakis 
3102f300babaSAlexandros Batsakis 	dprintk("RPC:       set up xprt to %s (port %s) via %s\n",
3103f300babaSAlexandros Batsakis 			xprt->address_strings[RPC_DISPLAY_ADDR],
3104f300babaSAlexandros Batsakis 			xprt->address_strings[RPC_DISPLAY_PORT],
3105f300babaSAlexandros Batsakis 			xprt->address_strings[RPC_DISPLAY_PROTO]);
3106f300babaSAlexandros Batsakis 
3107f300babaSAlexandros Batsakis 	/*
310899de8ea9SJ. Bruce Fields 	 * Once we've associated a backchannel xprt with a connection,
310928303ca3SWeng Meiling 	 * we want to keep it around as long as the connection lasts,
311028303ca3SWeng Meiling 	 * in case we need to start using it for a backchannel again;
311128303ca3SWeng Meiling 	 * this reference won't be dropped until bc_xprt is destroyed.
311299de8ea9SJ. Bruce Fields 	 */
311399de8ea9SJ. Bruce Fields 	xprt_get(xprt);
311499de8ea9SJ. Bruce Fields 	args->bc_xprt->xpt_bc_xprt = xprt;
311599de8ea9SJ. Bruce Fields 	xprt->bc_xprt = args->bc_xprt;
311699de8ea9SJ. Bruce Fields 	bc_sock = container_of(args->bc_xprt, struct svc_sock, sk_xprt);
311799de8ea9SJ. Bruce Fields 	transport->sock = bc_sock->sk_sock;
311899de8ea9SJ. Bruce Fields 	transport->inet = bc_sock->sk_sk;
311999de8ea9SJ. Bruce Fields 
312099de8ea9SJ. Bruce Fields 	/*
3121f300babaSAlexandros Batsakis 	 * Since we don't want connections for the backchannel, we set
3122f300babaSAlexandros Batsakis 	 * the xprt status to connected
3123f300babaSAlexandros Batsakis 	 */
3124f300babaSAlexandros Batsakis 	xprt_set_connected(xprt);
3125f300babaSAlexandros Batsakis 
3126f300babaSAlexandros Batsakis 	if (try_module_get(THIS_MODULE))
3127f300babaSAlexandros Batsakis 		return xprt;
3128642aab58SKinglong Mee 
3129642aab58SKinglong Mee 	args->bc_xprt->xpt_bc_xprt = NULL;
313039a9beabSJ. Bruce Fields 	args->bc_xprt->xpt_bc_xps = NULL;
313199de8ea9SJ. Bruce Fields 	xprt_put(xprt);
31320a68b0beSJ. Bruce Fields 	ret = ERR_PTR(-EINVAL);
31330a68b0beSJ. Bruce Fields out_err:
3134315f3812SKinglong Mee 	xs_xprt_free(xprt);
31350a68b0beSJ. Bruce Fields 	return ret;
3136f300babaSAlexandros Batsakis }
3137f300babaSAlexandros Batsakis 
3138176e21eeSChuck Lever static struct xprt_class	xs_local_transport = {
3139176e21eeSChuck Lever 	.list		= LIST_HEAD_INIT(xs_local_transport.list),
3140176e21eeSChuck Lever 	.name		= "named UNIX socket",
3141176e21eeSChuck Lever 	.owner		= THIS_MODULE,
3142176e21eeSChuck Lever 	.ident		= XPRT_TRANSPORT_LOCAL,
3143176e21eeSChuck Lever 	.setup		= xs_setup_local,
3144d5aa6b22STrond Myklebust 	.netid		= { "" },
3145176e21eeSChuck Lever };
3146176e21eeSChuck Lever 
3147bc25571eS\"Talpey, Thomas\ static struct xprt_class	xs_udp_transport = {
3148bc25571eS\"Talpey, Thomas\ 	.list		= LIST_HEAD_INIT(xs_udp_transport.list),
3149bc25571eS\"Talpey, Thomas\ 	.name		= "udp",
3150bc25571eS\"Talpey, Thomas\ 	.owner		= THIS_MODULE,
3151f300babaSAlexandros Batsakis 	.ident		= XPRT_TRANSPORT_UDP,
3152bc25571eS\"Talpey, Thomas\ 	.setup		= xs_setup_udp,
3153d5aa6b22STrond Myklebust 	.netid		= { "udp", "udp6", "" },
3154bc25571eS\"Talpey, Thomas\ };
3155bc25571eS\"Talpey, Thomas\ 
3156bc25571eS\"Talpey, Thomas\ static struct xprt_class	xs_tcp_transport = {
3157bc25571eS\"Talpey, Thomas\ 	.list		= LIST_HEAD_INIT(xs_tcp_transport.list),
3158bc25571eS\"Talpey, Thomas\ 	.name		= "tcp",
3159bc25571eS\"Talpey, Thomas\ 	.owner		= THIS_MODULE,
3160f300babaSAlexandros Batsakis 	.ident		= XPRT_TRANSPORT_TCP,
3161bc25571eS\"Talpey, Thomas\ 	.setup		= xs_setup_tcp,
3162d5aa6b22STrond Myklebust 	.netid		= { "tcp", "tcp6", "" },
3163bc25571eS\"Talpey, Thomas\ };
3164bc25571eS\"Talpey, Thomas\ 
3165f300babaSAlexandros Batsakis static struct xprt_class	xs_bc_tcp_transport = {
3166f300babaSAlexandros Batsakis 	.list		= LIST_HEAD_INIT(xs_bc_tcp_transport.list),
3167f300babaSAlexandros Batsakis 	.name		= "tcp NFSv4.1 backchannel",
3168f300babaSAlexandros Batsakis 	.owner		= THIS_MODULE,
3169f300babaSAlexandros Batsakis 	.ident		= XPRT_TRANSPORT_BC_TCP,
3170f300babaSAlexandros Batsakis 	.setup		= xs_setup_bc_tcp,
3171d5aa6b22STrond Myklebust 	.netid		= { "" },
3172f300babaSAlexandros Batsakis };
3173f300babaSAlexandros Batsakis 
3174282b32e1SChuck Lever /**
3175bc25571eS\"Talpey, Thomas\  * init_socket_xprt - set up xprtsock's sysctls, register with RPC client
3176282b32e1SChuck Lever  *
3177282b32e1SChuck Lever  */
3178282b32e1SChuck Lever int init_socket_xprt(void)
3179282b32e1SChuck Lever {
31802b1bec5fSEric W. Biederman 	if (!sunrpc_table_header)
31810b4d4147SEric W. Biederman 		sunrpc_table_header = register_sysctl_table(sunrpc_table);
3182fbf76683SChuck Lever 
3183176e21eeSChuck Lever 	xprt_register_transport(&xs_local_transport);
3184bc25571eS\"Talpey, Thomas\ 	xprt_register_transport(&xs_udp_transport);
3185bc25571eS\"Talpey, Thomas\ 	xprt_register_transport(&xs_tcp_transport);
3186f300babaSAlexandros Batsakis 	xprt_register_transport(&xs_bc_tcp_transport);
3187bc25571eS\"Talpey, Thomas\ 
3188282b32e1SChuck Lever 	return 0;
3189282b32e1SChuck Lever }
3190282b32e1SChuck Lever 
3191282b32e1SChuck Lever /**
3192bc25571eS\"Talpey, Thomas\  * cleanup_socket_xprt - remove xprtsock's sysctls, unregister
3193282b32e1SChuck Lever  *
3194282b32e1SChuck Lever  */
3195282b32e1SChuck Lever void cleanup_socket_xprt(void)
3196282b32e1SChuck Lever {
3197fbf76683SChuck Lever 	if (sunrpc_table_header) {
3198fbf76683SChuck Lever 		unregister_sysctl_table(sunrpc_table_header);
3199fbf76683SChuck Lever 		sunrpc_table_header = NULL;
3200fbf76683SChuck Lever 	}
3201bc25571eS\"Talpey, Thomas\ 
3202176e21eeSChuck Lever 	xprt_unregister_transport(&xs_local_transport);
3203bc25571eS\"Talpey, Thomas\ 	xprt_unregister_transport(&xs_udp_transport);
3204bc25571eS\"Talpey, Thomas\ 	xprt_unregister_transport(&xs_tcp_transport);
3205f300babaSAlexandros Batsakis 	xprt_unregister_transport(&xs_bc_tcp_transport);
3206282b32e1SChuck Lever }
3207cbf11071STrond Myklebust 
32089bbb9e5aSRusty Russell static int param_set_portnr(const char *val, const struct kernel_param *kp)
3209cbf11071STrond Myklebust {
3210cbf11071STrond Myklebust 	return param_set_uint_minmax(val, kp,
3211cbf11071STrond Myklebust 			RPC_MIN_RESVPORT,
3212cbf11071STrond Myklebust 			RPC_MAX_RESVPORT);
3213cbf11071STrond Myklebust }
3214cbf11071STrond Myklebust 
32159c27847dSLuis R. Rodriguez static const struct kernel_param_ops param_ops_portnr = {
32169bbb9e5aSRusty Russell 	.set = param_set_portnr,
32179bbb9e5aSRusty Russell 	.get = param_get_uint,
32189bbb9e5aSRusty Russell };
32199bbb9e5aSRusty Russell 
3220cbf11071STrond Myklebust #define param_check_portnr(name, p) \
3221cbf11071STrond Myklebust 	__param_check(name, p, unsigned int);
3222cbf11071STrond Myklebust 
3223cbf11071STrond Myklebust module_param_named(min_resvport, xprt_min_resvport, portnr, 0644);
3224cbf11071STrond Myklebust module_param_named(max_resvport, xprt_max_resvport, portnr, 0644);
3225cbf11071STrond Myklebust 
32269bbb9e5aSRusty Russell static int param_set_slot_table_size(const char *val,
32279bbb9e5aSRusty Russell 				     const struct kernel_param *kp)
3228cbf11071STrond Myklebust {
3229cbf11071STrond Myklebust 	return param_set_uint_minmax(val, kp,
3230cbf11071STrond Myklebust 			RPC_MIN_SLOT_TABLE,
3231cbf11071STrond Myklebust 			RPC_MAX_SLOT_TABLE);
3232cbf11071STrond Myklebust }
3233cbf11071STrond Myklebust 
32349c27847dSLuis R. Rodriguez static const struct kernel_param_ops param_ops_slot_table_size = {
32359bbb9e5aSRusty Russell 	.set = param_set_slot_table_size,
32369bbb9e5aSRusty Russell 	.get = param_get_uint,
32379bbb9e5aSRusty Russell };
32389bbb9e5aSRusty Russell 
3239cbf11071STrond Myklebust #define param_check_slot_table_size(name, p) \
3240cbf11071STrond Myklebust 	__param_check(name, p, unsigned int);
3241cbf11071STrond Myklebust 
3242d9ba131dSTrond Myklebust static int param_set_max_slot_table_size(const char *val,
3243d9ba131dSTrond Myklebust 				     const struct kernel_param *kp)
3244d9ba131dSTrond Myklebust {
3245d9ba131dSTrond Myklebust 	return param_set_uint_minmax(val, kp,
3246d9ba131dSTrond Myklebust 			RPC_MIN_SLOT_TABLE,
3247d9ba131dSTrond Myklebust 			RPC_MAX_SLOT_TABLE_LIMIT);
3248d9ba131dSTrond Myklebust }
3249d9ba131dSTrond Myklebust 
32509c27847dSLuis R. Rodriguez static const struct kernel_param_ops param_ops_max_slot_table_size = {
3251d9ba131dSTrond Myklebust 	.set = param_set_max_slot_table_size,
3252d9ba131dSTrond Myklebust 	.get = param_get_uint,
3253d9ba131dSTrond Myklebust };
3254d9ba131dSTrond Myklebust 
3255d9ba131dSTrond Myklebust #define param_check_max_slot_table_size(name, p) \
3256d9ba131dSTrond Myklebust 	__param_check(name, p, unsigned int);
3257d9ba131dSTrond Myklebust 
3258cbf11071STrond Myklebust module_param_named(tcp_slot_table_entries, xprt_tcp_slot_table_entries,
3259cbf11071STrond Myklebust 		   slot_table_size, 0644);
3260d9ba131dSTrond Myklebust module_param_named(tcp_max_slot_table_entries, xprt_max_tcp_slot_table_entries,
3261d9ba131dSTrond Myklebust 		   max_slot_table_size, 0644);
3262cbf11071STrond Myklebust module_param_named(udp_slot_table_entries, xprt_udp_slot_table_entries,
3263cbf11071STrond Myklebust 		   slot_table_size, 0644);
3264