xref: /openbmc/linux/net/sunrpc/xprtsock.c (revision dea034b963c8901bdcc3d3880c04f0d75c95112f)
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>
50*dea034b9SChuck Lever #include <net/tls.h>
51*dea034b9SChuck Lever 
52277e4ab7STrond Myklebust #include <linux/bvec.h>
536a829eb8STrond Myklebust #include <linux/highmem.h>
54277e4ab7STrond Myklebust #include <linux/uio.h>
55a1231fdaSTrond Myklebust #include <linux/sched/mm.h>
56a246b010SChuck Lever 
5740e0b090SPeilin Ye #include <trace/events/sock.h>
5840b5ea0cSTrond Myklebust #include <trace/events/sunrpc.h>
5940b5ea0cSTrond Myklebust 
609e55eef4SChuck Lever #include "socklib.h"
614cfc7e60SRahul Iyer #include "sunrpc.h"
62176e21eeSChuck Lever 
63176e21eeSChuck Lever static void xs_close(struct rpc_xprt *xprt);
643b21f757STrond Myklebust static void xs_set_srcport(struct sock_xprt *transport, struct socket *sock);
657196dbb0STrond Myklebust static void xs_tcp_set_socket_timeouts(struct rpc_xprt *xprt,
667196dbb0STrond Myklebust 		struct socket *sock);
67176e21eeSChuck Lever 
689903cd1cSChuck Lever /*
69c556b754SChuck Lever  * xprtsock tunables
70c556b754SChuck Lever  */
7109acfea5STrond Myklebust static unsigned int xprt_udp_slot_table_entries = RPC_DEF_SLOT_TABLE;
7209acfea5STrond Myklebust static unsigned int xprt_tcp_slot_table_entries = RPC_MIN_SLOT_TABLE;
7309acfea5STrond Myklebust static unsigned int xprt_max_tcp_slot_table_entries = RPC_MAX_SLOT_TABLE;
74c556b754SChuck Lever 
7509acfea5STrond Myklebust static unsigned int xprt_min_resvport = RPC_DEF_MIN_RESVPORT;
7609acfea5STrond Myklebust static unsigned int xprt_max_resvport = RPC_DEF_MAX_RESVPORT;
77c556b754SChuck Lever 
787d1e8255STrond Myklebust #define XS_TCP_LINGER_TO	(15U * HZ)
7925fe6142STrond Myklebust static unsigned int xs_tcp_fin_timeout __read_mostly = XS_TCP_LINGER_TO;
807d1e8255STrond Myklebust 
81c556b754SChuck Lever /*
82fbf76683SChuck Lever  * We can register our own files under /proc/sys/sunrpc by
83c946cb69SLuis Chamberlain  * calling register_sysctl() again.  The files in that
84fbf76683SChuck Lever  * directory become the union of all files registered there.
85fbf76683SChuck Lever  *
86fbf76683SChuck Lever  * We simply need to make sure that we don't collide with
87fbf76683SChuck Lever  * someone else's file names!
88fbf76683SChuck Lever  */
89fbf76683SChuck Lever 
90fbf76683SChuck Lever static unsigned int min_slot_table_size = RPC_MIN_SLOT_TABLE;
91fbf76683SChuck Lever static unsigned int max_slot_table_size = RPC_MAX_SLOT_TABLE;
92d9ba131dSTrond Myklebust static unsigned int max_tcp_slot_table_limit = RPC_MAX_SLOT_TABLE_LIMIT;
93fbf76683SChuck Lever static unsigned int xprt_min_resvport_limit = RPC_MIN_RESVPORT;
94fbf76683SChuck Lever static unsigned int xprt_max_resvport_limit = RPC_MAX_RESVPORT;
95fbf76683SChuck Lever 
96fbf76683SChuck Lever static struct ctl_table_header *sunrpc_table_header;
97fbf76683SChuck Lever 
98d3abc739SOlga Kornievskaia static struct xprt_class xs_local_transport;
99d3abc739SOlga Kornievskaia static struct xprt_class xs_udp_transport;
100d3abc739SOlga Kornievskaia static struct xprt_class xs_tcp_transport;
101d3abc739SOlga Kornievskaia static struct xprt_class xs_bc_tcp_transport;
102d3abc739SOlga Kornievskaia 
103fbf76683SChuck Lever /*
104fbf76683SChuck Lever  * FIXME: changing the UDP slot table size should also resize the UDP
105fbf76683SChuck Lever  *        socket buffers for existing UDP transports
106fbf76683SChuck Lever  */
107fe2c6338SJoe Perches static struct ctl_table xs_tunables_table[] = {
108fbf76683SChuck Lever 	{
109fbf76683SChuck Lever 		.procname	= "udp_slot_table_entries",
110fbf76683SChuck Lever 		.data		= &xprt_udp_slot_table_entries,
111fbf76683SChuck Lever 		.maxlen		= sizeof(unsigned int),
112fbf76683SChuck Lever 		.mode		= 0644,
1136d456111SEric W. Biederman 		.proc_handler	= proc_dointvec_minmax,
114fbf76683SChuck Lever 		.extra1		= &min_slot_table_size,
115fbf76683SChuck Lever 		.extra2		= &max_slot_table_size
116fbf76683SChuck Lever 	},
117fbf76683SChuck Lever 	{
118fbf76683SChuck Lever 		.procname	= "tcp_slot_table_entries",
119fbf76683SChuck Lever 		.data		= &xprt_tcp_slot_table_entries,
120fbf76683SChuck Lever 		.maxlen		= sizeof(unsigned int),
121fbf76683SChuck Lever 		.mode		= 0644,
1226d456111SEric W. Biederman 		.proc_handler	= proc_dointvec_minmax,
123fbf76683SChuck Lever 		.extra1		= &min_slot_table_size,
124fbf76683SChuck Lever 		.extra2		= &max_slot_table_size
125fbf76683SChuck Lever 	},
126fbf76683SChuck Lever 	{
127d9ba131dSTrond Myklebust 		.procname	= "tcp_max_slot_table_entries",
128d9ba131dSTrond Myklebust 		.data		= &xprt_max_tcp_slot_table_entries,
129d9ba131dSTrond Myklebust 		.maxlen		= sizeof(unsigned int),
130d9ba131dSTrond Myklebust 		.mode		= 0644,
131d9ba131dSTrond Myklebust 		.proc_handler	= proc_dointvec_minmax,
132d9ba131dSTrond Myklebust 		.extra1		= &min_slot_table_size,
133d9ba131dSTrond Myklebust 		.extra2		= &max_tcp_slot_table_limit
134d9ba131dSTrond Myklebust 	},
135d9ba131dSTrond Myklebust 	{
136fbf76683SChuck Lever 		.procname	= "min_resvport",
137fbf76683SChuck Lever 		.data		= &xprt_min_resvport,
138fbf76683SChuck Lever 		.maxlen		= sizeof(unsigned int),
139fbf76683SChuck Lever 		.mode		= 0644,
1406d456111SEric W. Biederman 		.proc_handler	= proc_dointvec_minmax,
141fbf76683SChuck Lever 		.extra1		= &xprt_min_resvport_limit,
142826799e6SJ. Bruce Fields 		.extra2		= &xprt_max_resvport_limit
143fbf76683SChuck Lever 	},
144fbf76683SChuck Lever 	{
145fbf76683SChuck Lever 		.procname	= "max_resvport",
146fbf76683SChuck Lever 		.data		= &xprt_max_resvport,
147fbf76683SChuck Lever 		.maxlen		= sizeof(unsigned int),
148fbf76683SChuck Lever 		.mode		= 0644,
1496d456111SEric W. Biederman 		.proc_handler	= proc_dointvec_minmax,
150826799e6SJ. Bruce Fields 		.extra1		= &xprt_min_resvport_limit,
151fbf76683SChuck Lever 		.extra2		= &xprt_max_resvport_limit
152fbf76683SChuck Lever 	},
153fbf76683SChuck Lever 	{
15425fe6142STrond Myklebust 		.procname	= "tcp_fin_timeout",
15525fe6142STrond Myklebust 		.data		= &xs_tcp_fin_timeout,
15625fe6142STrond Myklebust 		.maxlen		= sizeof(xs_tcp_fin_timeout),
15725fe6142STrond Myklebust 		.mode		= 0644,
1586d456111SEric W. Biederman 		.proc_handler	= proc_dointvec_jiffies,
15925fe6142STrond Myklebust 	},
160f8572d8fSEric W. Biederman 	{ },
161fbf76683SChuck Lever };
162fbf76683SChuck Lever 
163fbf76683SChuck Lever /*
16403bf4b70SChuck Lever  * Wait duration for a reply from the RPC portmapper.
16503bf4b70SChuck Lever  */
16603bf4b70SChuck Lever #define XS_BIND_TO		(60U * HZ)
16703bf4b70SChuck Lever 
16803bf4b70SChuck Lever /*
16903bf4b70SChuck Lever  * Delay if a UDP socket connect error occurs.  This is most likely some
17003bf4b70SChuck Lever  * kind of resource problem on the local host.
17103bf4b70SChuck Lever  */
17203bf4b70SChuck Lever #define XS_UDP_REEST_TO		(2U * HZ)
17303bf4b70SChuck Lever 
17403bf4b70SChuck Lever /*
17503bf4b70SChuck Lever  * The reestablish timeout allows clients to delay for a bit before attempting
17603bf4b70SChuck Lever  * to reconnect to a server that just dropped our connection.
17703bf4b70SChuck Lever  *
17803bf4b70SChuck Lever  * We implement an exponential backoff when trying to reestablish a TCP
17903bf4b70SChuck Lever  * transport connection with the server.  Some servers like to drop a TCP
18003bf4b70SChuck Lever  * connection when they are overworked, so we start with a short timeout and
18103bf4b70SChuck Lever  * increase over time if the server is down or not responding.
18203bf4b70SChuck Lever  */
18303bf4b70SChuck Lever #define XS_TCP_INIT_REEST_TO	(3U * HZ)
18403bf4b70SChuck Lever 
18503bf4b70SChuck Lever /*
18603bf4b70SChuck Lever  * TCP idle timeout; client drops the transport socket if it is idle
18703bf4b70SChuck Lever  * for this long.  Note that we also timeout UDP sockets to prevent
18803bf4b70SChuck Lever  * holding port numbers when there is no RPC traffic.
18903bf4b70SChuck Lever  */
19003bf4b70SChuck Lever #define XS_IDLE_DISC_TO		(5U * 60 * HZ)
19103bf4b70SChuck Lever 
192f895b252SJeff Layton #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
193a246b010SChuck Lever # undef  RPC_DEBUG_DATA
1949903cd1cSChuck Lever # define RPCDBG_FACILITY	RPCDBG_TRANS
195a246b010SChuck Lever #endif
196a246b010SChuck Lever 
197a246b010SChuck Lever #ifdef RPC_DEBUG_DATA
1989903cd1cSChuck Lever static void xs_pktdump(char *msg, u32 *packet, unsigned int count)
199a246b010SChuck Lever {
200a246b010SChuck Lever 	u8 *buf = (u8 *) packet;
201a246b010SChuck Lever 	int j;
202a246b010SChuck Lever 
203a246b010SChuck Lever 	dprintk("RPC:       %s\n", msg);
204a246b010SChuck Lever 	for (j = 0; j < count && j < 128; j += 4) {
205a246b010SChuck Lever 		if (!(j & 31)) {
206a246b010SChuck Lever 			if (j)
207a246b010SChuck Lever 				dprintk("\n");
208a246b010SChuck Lever 			dprintk("0x%04x ", j);
209a246b010SChuck Lever 		}
210a246b010SChuck Lever 		dprintk("%02x%02x%02x%02x ",
211a246b010SChuck Lever 			buf[j], buf[j+1], buf[j+2], buf[j+3]);
212a246b010SChuck Lever 	}
213a246b010SChuck Lever 	dprintk("\n");
214a246b010SChuck Lever }
215a246b010SChuck Lever #else
2169903cd1cSChuck Lever static inline void xs_pktdump(char *msg, u32 *packet, unsigned int count)
217a246b010SChuck Lever {
218a246b010SChuck Lever 	/* NOP */
219a246b010SChuck Lever }
220a246b010SChuck Lever #endif
221a246b010SChuck Lever 
2222118071dSTrond Myklebust static inline struct rpc_xprt *xprt_from_sock(struct sock *sk)
2232118071dSTrond Myklebust {
2242118071dSTrond Myklebust 	return (struct rpc_xprt *) sk->sk_user_data;
2252118071dSTrond Myklebust }
2262118071dSTrond Myklebust 
22795392c59SChuck Lever static inline struct sockaddr *xs_addr(struct rpc_xprt *xprt)
228edb267a6SChuck Lever {
22995392c59SChuck Lever 	return (struct sockaddr *) &xprt->addr;
23095392c59SChuck Lever }
23195392c59SChuck Lever 
232176e21eeSChuck Lever static inline struct sockaddr_un *xs_addr_un(struct rpc_xprt *xprt)
233176e21eeSChuck Lever {
234176e21eeSChuck Lever 	return (struct sockaddr_un *) &xprt->addr;
235176e21eeSChuck Lever }
236176e21eeSChuck Lever 
23795392c59SChuck Lever static inline struct sockaddr_in *xs_addr_in(struct rpc_xprt *xprt)
23895392c59SChuck Lever {
23995392c59SChuck Lever 	return (struct sockaddr_in *) &xprt->addr;
24095392c59SChuck Lever }
24195392c59SChuck Lever 
24295392c59SChuck Lever static inline struct sockaddr_in6 *xs_addr_in6(struct rpc_xprt *xprt)
24395392c59SChuck Lever {
24495392c59SChuck Lever 	return (struct sockaddr_in6 *) &xprt->addr;
24595392c59SChuck Lever }
24695392c59SChuck Lever 
247c877b849SChuck Lever static void xs_format_common_peer_addresses(struct rpc_xprt *xprt)
248c877b849SChuck Lever {
249c877b849SChuck Lever 	struct sockaddr *sap = xs_addr(xprt);
2509dc3b095SChuck Lever 	struct sockaddr_in6 *sin6;
2519dc3b095SChuck Lever 	struct sockaddr_in *sin;
252176e21eeSChuck Lever 	struct sockaddr_un *sun;
253c877b849SChuck Lever 	char buf[128];
254c877b849SChuck Lever 
2559dc3b095SChuck Lever 	switch (sap->sa_family) {
256176e21eeSChuck Lever 	case AF_LOCAL:
257176e21eeSChuck Lever 		sun = xs_addr_un(xprt);
2584388ce05SNeilBrown 		if (sun->sun_path[0]) {
25915bcdc92SWolfram Sang 			strscpy(buf, sun->sun_path, sizeof(buf));
2604388ce05SNeilBrown 		} else {
2614388ce05SNeilBrown 			buf[0] = '@';
2624388ce05SNeilBrown 			strscpy(buf+1, sun->sun_path+1, sizeof(buf)-1);
2634388ce05SNeilBrown 		}
264176e21eeSChuck Lever 		xprt->address_strings[RPC_DISPLAY_ADDR] =
265176e21eeSChuck Lever 						kstrdup(buf, GFP_KERNEL);
266176e21eeSChuck Lever 		break;
2679dc3b095SChuck Lever 	case AF_INET:
268176e21eeSChuck Lever 		(void)rpc_ntop(sap, buf, sizeof(buf));
269176e21eeSChuck Lever 		xprt->address_strings[RPC_DISPLAY_ADDR] =
270176e21eeSChuck Lever 						kstrdup(buf, GFP_KERNEL);
2719dc3b095SChuck Lever 		sin = xs_addr_in(xprt);
272fc0b5791SJoe Perches 		snprintf(buf, sizeof(buf), "%08x", ntohl(sin->sin_addr.s_addr));
2739dc3b095SChuck Lever 		break;
2749dc3b095SChuck Lever 	case AF_INET6:
275176e21eeSChuck Lever 		(void)rpc_ntop(sap, buf, sizeof(buf));
276176e21eeSChuck Lever 		xprt->address_strings[RPC_DISPLAY_ADDR] =
277176e21eeSChuck Lever 						kstrdup(buf, GFP_KERNEL);
2789dc3b095SChuck Lever 		sin6 = xs_addr_in6(xprt);
279fc0b5791SJoe Perches 		snprintf(buf, sizeof(buf), "%pi6", &sin6->sin6_addr);
2809dc3b095SChuck Lever 		break;
2819dc3b095SChuck Lever 	default:
2829dc3b095SChuck Lever 		BUG();
2839dc3b095SChuck Lever 	}
284176e21eeSChuck Lever 
2859dc3b095SChuck Lever 	xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = kstrdup(buf, GFP_KERNEL);
2869dc3b095SChuck Lever }
2879dc3b095SChuck Lever 
2889dc3b095SChuck Lever static void xs_format_common_peer_ports(struct rpc_xprt *xprt)
2899dc3b095SChuck Lever {
2909dc3b095SChuck Lever 	struct sockaddr *sap = xs_addr(xprt);
2919dc3b095SChuck Lever 	char buf[128];
2929dc3b095SChuck Lever 
29381160e66SJoe Perches 	snprintf(buf, sizeof(buf), "%u", rpc_get_port(sap));
294c877b849SChuck Lever 	xprt->address_strings[RPC_DISPLAY_PORT] = kstrdup(buf, GFP_KERNEL);
295c877b849SChuck Lever 
29681160e66SJoe Perches 	snprintf(buf, sizeof(buf), "%4hx", rpc_get_port(sap));
297c877b849SChuck Lever 	xprt->address_strings[RPC_DISPLAY_HEX_PORT] = kstrdup(buf, GFP_KERNEL);
298c877b849SChuck Lever }
299c877b849SChuck Lever 
3009dc3b095SChuck Lever static void xs_format_peer_addresses(struct rpc_xprt *xprt,
301b454ae90SChuck Lever 				     const char *protocol,
302b454ae90SChuck Lever 				     const char *netid)
303edb267a6SChuck Lever {
304b454ae90SChuck Lever 	xprt->address_strings[RPC_DISPLAY_PROTO] = protocol;
305b454ae90SChuck Lever 	xprt->address_strings[RPC_DISPLAY_NETID] = netid;
306c877b849SChuck Lever 	xs_format_common_peer_addresses(xprt);
3079dc3b095SChuck Lever 	xs_format_common_peer_ports(xprt);
308edb267a6SChuck Lever }
309edb267a6SChuck Lever 
3109dc3b095SChuck Lever static void xs_update_peer_port(struct rpc_xprt *xprt)
3114b6473fbSChuck Lever {
3129dc3b095SChuck Lever 	kfree(xprt->address_strings[RPC_DISPLAY_HEX_PORT]);
3139dc3b095SChuck Lever 	kfree(xprt->address_strings[RPC_DISPLAY_PORT]);
3144b6473fbSChuck Lever 
3159dc3b095SChuck Lever 	xs_format_common_peer_ports(xprt);
316edb267a6SChuck Lever }
317edb267a6SChuck Lever 
318edb267a6SChuck Lever static void xs_free_peer_addresses(struct rpc_xprt *xprt)
319edb267a6SChuck Lever {
32033e01dc7SChuck Lever 	unsigned int i;
32133e01dc7SChuck Lever 
32233e01dc7SChuck Lever 	for (i = 0; i < RPC_DISPLAY_MAX; i++)
32333e01dc7SChuck Lever 		switch (i) {
32433e01dc7SChuck Lever 		case RPC_DISPLAY_PROTO:
32533e01dc7SChuck Lever 		case RPC_DISPLAY_NETID:
32633e01dc7SChuck Lever 			continue;
32733e01dc7SChuck Lever 		default:
32833e01dc7SChuck Lever 			kfree(xprt->address_strings[i]);
32933e01dc7SChuck Lever 		}
330edb267a6SChuck Lever }
331edb267a6SChuck Lever 
332277e4ab7STrond Myklebust static size_t
333277e4ab7STrond Myklebust xs_alloc_sparse_pages(struct xdr_buf *buf, size_t want, gfp_t gfp)
334277e4ab7STrond Myklebust {
335277e4ab7STrond Myklebust 	size_t i,n;
336277e4ab7STrond Myklebust 
33716e5e90fSTrond Myklebust 	if (!want || !(buf->flags & XDRBUF_SPARSE_PAGES))
338277e4ab7STrond Myklebust 		return want;
339277e4ab7STrond Myklebust 	n = (buf->page_base + want + PAGE_SIZE - 1) >> PAGE_SHIFT;
340277e4ab7STrond Myklebust 	for (i = 0; i < n; i++) {
341277e4ab7STrond Myklebust 		if (buf->pages[i])
342277e4ab7STrond Myklebust 			continue;
343277e4ab7STrond Myklebust 		buf->bvec[i].bv_page = buf->pages[i] = alloc_page(gfp);
344277e4ab7STrond Myklebust 		if (!buf->pages[i]) {
34516e5e90fSTrond Myklebust 			i *= PAGE_SIZE;
34616e5e90fSTrond Myklebust 			return i > buf->page_base ? i - buf->page_base : 0;
347277e4ab7STrond Myklebust 		}
348277e4ab7STrond Myklebust 	}
349277e4ab7STrond Myklebust 	return want;
350277e4ab7STrond Myklebust }
351277e4ab7STrond Myklebust 
352*dea034b9SChuck Lever static int
353*dea034b9SChuck Lever xs_sock_process_cmsg(struct socket *sock, struct msghdr *msg,
354*dea034b9SChuck Lever 		     struct cmsghdr *cmsg, int ret)
355*dea034b9SChuck Lever {
356*dea034b9SChuck Lever 	if (cmsg->cmsg_level == SOL_TLS &&
357*dea034b9SChuck Lever 	    cmsg->cmsg_type == TLS_GET_RECORD_TYPE) {
358*dea034b9SChuck Lever 		u8 content_type = *((u8 *)CMSG_DATA(cmsg));
359*dea034b9SChuck Lever 
360*dea034b9SChuck Lever 		switch (content_type) {
361*dea034b9SChuck Lever 		case TLS_RECORD_TYPE_DATA:
362*dea034b9SChuck Lever 			/* TLS sets EOR at the end of each application data
363*dea034b9SChuck Lever 			 * record, even though there might be more frames
364*dea034b9SChuck Lever 			 * waiting to be decrypted.
365*dea034b9SChuck Lever 			 */
366*dea034b9SChuck Lever 			msg->msg_flags &= ~MSG_EOR;
367*dea034b9SChuck Lever 			break;
368*dea034b9SChuck Lever 		case TLS_RECORD_TYPE_ALERT:
369*dea034b9SChuck Lever 			ret = -ENOTCONN;
370*dea034b9SChuck Lever 			break;
371*dea034b9SChuck Lever 		default:
372*dea034b9SChuck Lever 			ret = -EAGAIN;
373*dea034b9SChuck Lever 		}
374*dea034b9SChuck Lever 	}
375*dea034b9SChuck Lever 	return ret;
376*dea034b9SChuck Lever }
377*dea034b9SChuck Lever 
378*dea034b9SChuck Lever static int
379*dea034b9SChuck Lever xs_sock_recv_cmsg(struct socket *sock, struct msghdr *msg, int flags)
380*dea034b9SChuck Lever {
381*dea034b9SChuck Lever 	union {
382*dea034b9SChuck Lever 		struct cmsghdr	cmsg;
383*dea034b9SChuck Lever 		u8		buf[CMSG_SPACE(sizeof(u8))];
384*dea034b9SChuck Lever 	} u;
385*dea034b9SChuck Lever 	int ret;
386*dea034b9SChuck Lever 
387*dea034b9SChuck Lever 	msg->msg_control = &u;
388*dea034b9SChuck Lever 	msg->msg_controllen = sizeof(u);
389*dea034b9SChuck Lever 	ret = sock_recvmsg(sock, msg, flags);
390*dea034b9SChuck Lever 	if (msg->msg_controllen != sizeof(u))
391*dea034b9SChuck Lever 		ret = xs_sock_process_cmsg(sock, msg, &u.cmsg, ret);
392*dea034b9SChuck Lever 	return ret;
393*dea034b9SChuck Lever }
394*dea034b9SChuck Lever 
395277e4ab7STrond Myklebust static ssize_t
396277e4ab7STrond Myklebust xs_sock_recvmsg(struct socket *sock, struct msghdr *msg, int flags, size_t seek)
397277e4ab7STrond Myklebust {
398277e4ab7STrond Myklebust 	ssize_t ret;
399277e4ab7STrond Myklebust 	if (seek != 0)
400277e4ab7STrond Myklebust 		iov_iter_advance(&msg->msg_iter, seek);
401*dea034b9SChuck Lever 	ret = xs_sock_recv_cmsg(sock, msg, flags);
402277e4ab7STrond Myklebust 	return ret > 0 ? ret + seek : ret;
403277e4ab7STrond Myklebust }
404277e4ab7STrond Myklebust 
405277e4ab7STrond Myklebust static ssize_t
406277e4ab7STrond Myklebust xs_read_kvec(struct socket *sock, struct msghdr *msg, int flags,
407277e4ab7STrond Myklebust 		struct kvec *kvec, size_t count, size_t seek)
408277e4ab7STrond Myklebust {
409de4eda9dSAl Viro 	iov_iter_kvec(&msg->msg_iter, ITER_DEST, kvec, 1, count);
410277e4ab7STrond Myklebust 	return xs_sock_recvmsg(sock, msg, flags, seek);
411277e4ab7STrond Myklebust }
412277e4ab7STrond Myklebust 
413277e4ab7STrond Myklebust static ssize_t
414277e4ab7STrond Myklebust xs_read_bvec(struct socket *sock, struct msghdr *msg, int flags,
415277e4ab7STrond Myklebust 		struct bio_vec *bvec, unsigned long nr, size_t count,
416277e4ab7STrond Myklebust 		size_t seek)
417277e4ab7STrond Myklebust {
418de4eda9dSAl Viro 	iov_iter_bvec(&msg->msg_iter, ITER_DEST, bvec, nr, count);
419277e4ab7STrond Myklebust 	return xs_sock_recvmsg(sock, msg, flags, seek);
420277e4ab7STrond Myklebust }
421277e4ab7STrond Myklebust 
422277e4ab7STrond Myklebust static ssize_t
423277e4ab7STrond Myklebust xs_read_discard(struct socket *sock, struct msghdr *msg, int flags,
424277e4ab7STrond Myklebust 		size_t count)
425277e4ab7STrond Myklebust {
426de4eda9dSAl Viro 	iov_iter_discard(&msg->msg_iter, ITER_DEST, count);
427*dea034b9SChuck Lever 	return xs_sock_recv_cmsg(sock, msg, flags);
428277e4ab7STrond Myklebust }
429277e4ab7STrond Myklebust 
4306a829eb8STrond Myklebust #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
4316a829eb8STrond Myklebust static void
4326a829eb8STrond Myklebust xs_flush_bvec(const struct bio_vec *bvec, size_t count, size_t seek)
4336a829eb8STrond Myklebust {
4346a829eb8STrond Myklebust 	struct bvec_iter bi = {
4356a829eb8STrond Myklebust 		.bi_size = count,
4366a829eb8STrond Myklebust 	};
4376a829eb8STrond Myklebust 	struct bio_vec bv;
4386a829eb8STrond Myklebust 
4396a829eb8STrond Myklebust 	bvec_iter_advance(bvec, &bi, seek & PAGE_MASK);
4406a829eb8STrond Myklebust 	for_each_bvec(bv, bvec, bi, bi)
4416a829eb8STrond Myklebust 		flush_dcache_page(bv.bv_page);
4426a829eb8STrond Myklebust }
4436a829eb8STrond Myklebust #else
4446a829eb8STrond Myklebust static inline void
4456a829eb8STrond Myklebust xs_flush_bvec(const struct bio_vec *bvec, size_t count, size_t seek)
4466a829eb8STrond Myklebust {
4476a829eb8STrond Myklebust }
4486a829eb8STrond Myklebust #endif
4496a829eb8STrond Myklebust 
450277e4ab7STrond Myklebust static ssize_t
451277e4ab7STrond Myklebust xs_read_xdr_buf(struct socket *sock, struct msghdr *msg, int flags,
452277e4ab7STrond Myklebust 		struct xdr_buf *buf, size_t count, size_t seek, size_t *read)
453277e4ab7STrond Myklebust {
454277e4ab7STrond Myklebust 	size_t want, seek_init = seek, offset = 0;
455277e4ab7STrond Myklebust 	ssize_t ret;
456277e4ab7STrond Myklebust 
457277e4ab7STrond Myklebust 	want = min_t(size_t, count, buf->head[0].iov_len);
458e92053a5STrond Myklebust 	if (seek < want) {
459277e4ab7STrond Myklebust 		ret = xs_read_kvec(sock, msg, flags, &buf->head[0], want, seek);
460277e4ab7STrond Myklebust 		if (ret <= 0)
461277e4ab7STrond Myklebust 			goto sock_err;
462277e4ab7STrond Myklebust 		offset += ret;
463277e4ab7STrond Myklebust 		if (offset == count || msg->msg_flags & (MSG_EOR|MSG_TRUNC))
464277e4ab7STrond Myklebust 			goto out;
465277e4ab7STrond Myklebust 		if (ret != want)
466c4433055STrond Myklebust 			goto out;
467277e4ab7STrond Myklebust 		seek = 0;
468277e4ab7STrond Myklebust 	} else {
469e92053a5STrond Myklebust 		seek -= want;
470e92053a5STrond Myklebust 		offset += want;
471277e4ab7STrond Myklebust 	}
47216e5e90fSTrond Myklebust 
473b2648015STrond Myklebust 	want = xs_alloc_sparse_pages(
474b2648015STrond Myklebust 		buf, min_t(size_t, count - offset, buf->page_len),
475b2648015STrond Myklebust 		GFP_KERNEL | __GFP_NORETRY | __GFP_NOWARN);
47616e5e90fSTrond Myklebust 	if (seek < want) {
477277e4ab7STrond Myklebust 		ret = xs_read_bvec(sock, msg, flags, buf->bvec,
478277e4ab7STrond Myklebust 				xdr_buf_pagecount(buf),
479277e4ab7STrond Myklebust 				want + buf->page_base,
480277e4ab7STrond Myklebust 				seek + buf->page_base);
481277e4ab7STrond Myklebust 		if (ret <= 0)
482277e4ab7STrond Myklebust 			goto sock_err;
4836a829eb8STrond Myklebust 		xs_flush_bvec(buf->bvec, ret, seek + buf->page_base);
484ac9645c8SDan Aloni 		ret -= buf->page_base;
485ac9645c8SDan Aloni 		offset += ret;
486277e4ab7STrond Myklebust 		if (offset == count || msg->msg_flags & (MSG_EOR|MSG_TRUNC))
487277e4ab7STrond Myklebust 			goto out;
488277e4ab7STrond Myklebust 		if (ret != want)
489c4433055STrond Myklebust 			goto out;
490277e4ab7STrond Myklebust 		seek = 0;
491277e4ab7STrond Myklebust 	} else {
49216e5e90fSTrond Myklebust 		seek -= want;
49316e5e90fSTrond Myklebust 		offset += want;
494277e4ab7STrond Myklebust 	}
49516e5e90fSTrond Myklebust 
496277e4ab7STrond Myklebust 	want = min_t(size_t, count - offset, buf->tail[0].iov_len);
497e92053a5STrond Myklebust 	if (seek < want) {
498277e4ab7STrond Myklebust 		ret = xs_read_kvec(sock, msg, flags, &buf->tail[0], want, seek);
499277e4ab7STrond Myklebust 		if (ret <= 0)
500277e4ab7STrond Myklebust 			goto sock_err;
501277e4ab7STrond Myklebust 		offset += ret;
502277e4ab7STrond Myklebust 		if (offset == count || msg->msg_flags & (MSG_EOR|MSG_TRUNC))
503277e4ab7STrond Myklebust 			goto out;
504277e4ab7STrond Myklebust 		if (ret != want)
505c4433055STrond Myklebust 			goto out;
5069734ad57STrond Myklebust 	} else if (offset < seek_init)
507e92053a5STrond Myklebust 		offset = seek_init;
508277e4ab7STrond Myklebust 	ret = -EMSGSIZE;
509277e4ab7STrond Myklebust out:
510277e4ab7STrond Myklebust 	*read = offset - seek_init;
511277e4ab7STrond Myklebust 	return ret;
512277e4ab7STrond Myklebust sock_err:
513277e4ab7STrond Myklebust 	offset += seek;
514277e4ab7STrond Myklebust 	goto out;
515277e4ab7STrond Myklebust }
516277e4ab7STrond Myklebust 
517277e4ab7STrond Myklebust static void
518277e4ab7STrond Myklebust xs_read_header(struct sock_xprt *transport, struct xdr_buf *buf)
519277e4ab7STrond Myklebust {
520277e4ab7STrond Myklebust 	if (!transport->recv.copied) {
521277e4ab7STrond Myklebust 		if (buf->head[0].iov_len >= transport->recv.offset)
522277e4ab7STrond Myklebust 			memcpy(buf->head[0].iov_base,
523277e4ab7STrond Myklebust 					&transport->recv.xid,
524277e4ab7STrond Myklebust 					transport->recv.offset);
525277e4ab7STrond Myklebust 		transport->recv.copied = transport->recv.offset;
526277e4ab7STrond Myklebust 	}
527277e4ab7STrond Myklebust }
528277e4ab7STrond Myklebust 
529277e4ab7STrond Myklebust static bool
530277e4ab7STrond Myklebust xs_read_stream_request_done(struct sock_xprt *transport)
531277e4ab7STrond Myklebust {
532277e4ab7STrond Myklebust 	return transport->recv.fraghdr & cpu_to_be32(RPC_LAST_STREAM_FRAGMENT);
533277e4ab7STrond Myklebust }
534277e4ab7STrond Myklebust 
535e92053a5STrond Myklebust static void
536e92053a5STrond Myklebust xs_read_stream_check_eor(struct sock_xprt *transport,
537e92053a5STrond Myklebust 		struct msghdr *msg)
538e92053a5STrond Myklebust {
539e92053a5STrond Myklebust 	if (xs_read_stream_request_done(transport))
540e92053a5STrond Myklebust 		msg->msg_flags |= MSG_EOR;
541e92053a5STrond Myklebust }
542e92053a5STrond Myklebust 
543277e4ab7STrond Myklebust static ssize_t
544277e4ab7STrond Myklebust xs_read_stream_request(struct sock_xprt *transport, struct msghdr *msg,
545277e4ab7STrond Myklebust 		int flags, struct rpc_rqst *req)
546277e4ab7STrond Myklebust {
547277e4ab7STrond Myklebust 	struct xdr_buf *buf = &req->rq_private_buf;
5483f649ab7SKees Cook 	size_t want, read;
5493f649ab7SKees Cook 	ssize_t ret;
550277e4ab7STrond Myklebust 
551277e4ab7STrond Myklebust 	xs_read_header(transport, buf);
552277e4ab7STrond Myklebust 
553277e4ab7STrond Myklebust 	want = transport->recv.len - transport->recv.offset;
554e92053a5STrond Myklebust 	if (want != 0) {
555277e4ab7STrond Myklebust 		ret = xs_read_xdr_buf(transport->sock, msg, flags, buf,
556e92053a5STrond Myklebust 				transport->recv.copied + want,
557e92053a5STrond Myklebust 				transport->recv.copied,
558277e4ab7STrond Myklebust 				&read);
559277e4ab7STrond Myklebust 		transport->recv.offset += read;
560277e4ab7STrond Myklebust 		transport->recv.copied += read;
561277e4ab7STrond Myklebust 	}
562277e4ab7STrond Myklebust 
563727fcc64STrond Myklebust 	if (transport->recv.offset == transport->recv.len)
564727fcc64STrond Myklebust 		xs_read_stream_check_eor(transport, msg);
565727fcc64STrond Myklebust 
566e92053a5STrond Myklebust 	if (want == 0)
567e92053a5STrond Myklebust 		return 0;
568e92053a5STrond Myklebust 
569277e4ab7STrond Myklebust 	switch (ret) {
570c4433055STrond Myklebust 	default:
571c4433055STrond Myklebust 		break;
57226781eabSTrond Myklebust 	case -EFAULT:
573277e4ab7STrond Myklebust 	case -EMSGSIZE:
57426781eabSTrond Myklebust 		msg->msg_flags |= MSG_TRUNC;
575c4433055STrond Myklebust 		return read;
576277e4ab7STrond Myklebust 	case 0:
577277e4ab7STrond Myklebust 		return -ESHUTDOWN;
578277e4ab7STrond Myklebust 	}
579c4433055STrond Myklebust 	return ret < 0 ? ret : read;
580277e4ab7STrond Myklebust }
581277e4ab7STrond Myklebust 
582277e4ab7STrond Myklebust static size_t
583277e4ab7STrond Myklebust xs_read_stream_headersize(bool isfrag)
584277e4ab7STrond Myklebust {
585277e4ab7STrond Myklebust 	if (isfrag)
586277e4ab7STrond Myklebust 		return sizeof(__be32);
587277e4ab7STrond Myklebust 	return 3 * sizeof(__be32);
588277e4ab7STrond Myklebust }
589277e4ab7STrond Myklebust 
590277e4ab7STrond Myklebust static ssize_t
591277e4ab7STrond Myklebust xs_read_stream_header(struct sock_xprt *transport, struct msghdr *msg,
592277e4ab7STrond Myklebust 		int flags, size_t want, size_t seek)
593277e4ab7STrond Myklebust {
594277e4ab7STrond Myklebust 	struct kvec kvec = {
595277e4ab7STrond Myklebust 		.iov_base = &transport->recv.fraghdr,
596277e4ab7STrond Myklebust 		.iov_len = want,
597277e4ab7STrond Myklebust 	};
598277e4ab7STrond Myklebust 	return xs_read_kvec(transport->sock, msg, flags, &kvec, want, seek);
599277e4ab7STrond Myklebust }
600277e4ab7STrond Myklebust 
601277e4ab7STrond Myklebust #if defined(CONFIG_SUNRPC_BACKCHANNEL)
602277e4ab7STrond Myklebust static ssize_t
603277e4ab7STrond Myklebust xs_read_stream_call(struct sock_xprt *transport, struct msghdr *msg, int flags)
604277e4ab7STrond Myklebust {
605277e4ab7STrond Myklebust 	struct rpc_xprt *xprt = &transport->xprt;
606277e4ab7STrond Myklebust 	struct rpc_rqst *req;
607277e4ab7STrond Myklebust 	ssize_t ret;
608277e4ab7STrond Myklebust 
60998b5cee3SBenjamin Coddington 	/* Is this transport associated with the backchannel? */
61098b5cee3SBenjamin Coddington 	if (!xprt->bc_serv)
61198b5cee3SBenjamin Coddington 		return -ESHUTDOWN;
61298b5cee3SBenjamin Coddington 
613277e4ab7STrond Myklebust 	/* Look up and lock the request corresponding to the given XID */
614277e4ab7STrond Myklebust 	req = xprt_lookup_bc_request(xprt, transport->recv.xid);
615277e4ab7STrond Myklebust 	if (!req) {
616277e4ab7STrond Myklebust 		printk(KERN_WARNING "Callback slot table overflowed\n");
617277e4ab7STrond Myklebust 		return -ESHUTDOWN;
618277e4ab7STrond Myklebust 	}
61945835a63STrond Myklebust 	if (transport->recv.copied && !req->rq_private_buf.len)
62045835a63STrond Myklebust 		return -ESHUTDOWN;
621277e4ab7STrond Myklebust 
622277e4ab7STrond Myklebust 	ret = xs_read_stream_request(transport, msg, flags, req);
623277e4ab7STrond Myklebust 	if (msg->msg_flags & (MSG_EOR|MSG_TRUNC))
624c4433055STrond Myklebust 		xprt_complete_bc_request(req, transport->recv.copied);
62545835a63STrond Myklebust 	else
62645835a63STrond Myklebust 		req->rq_private_buf.len = transport->recv.copied;
627277e4ab7STrond Myklebust 
628277e4ab7STrond Myklebust 	return ret;
629277e4ab7STrond Myklebust }
630277e4ab7STrond Myklebust #else /* CONFIG_SUNRPC_BACKCHANNEL */
631277e4ab7STrond Myklebust static ssize_t
632277e4ab7STrond Myklebust xs_read_stream_call(struct sock_xprt *transport, struct msghdr *msg, int flags)
633277e4ab7STrond Myklebust {
634277e4ab7STrond Myklebust 	return -ESHUTDOWN;
635277e4ab7STrond Myklebust }
636277e4ab7STrond Myklebust #endif /* CONFIG_SUNRPC_BACKCHANNEL */
637277e4ab7STrond Myklebust 
638277e4ab7STrond Myklebust static ssize_t
639277e4ab7STrond Myklebust xs_read_stream_reply(struct sock_xprt *transport, struct msghdr *msg, int flags)
640277e4ab7STrond Myklebust {
641277e4ab7STrond Myklebust 	struct rpc_xprt *xprt = &transport->xprt;
642277e4ab7STrond Myklebust 	struct rpc_rqst *req;
643277e4ab7STrond Myklebust 	ssize_t ret = 0;
644277e4ab7STrond Myklebust 
645277e4ab7STrond Myklebust 	/* Look up and lock the request corresponding to the given XID */
646277e4ab7STrond Myklebust 	spin_lock(&xprt->queue_lock);
647277e4ab7STrond Myklebust 	req = xprt_lookup_rqst(xprt, transport->recv.xid);
64845835a63STrond Myklebust 	if (!req || (transport->recv.copied && !req->rq_private_buf.len)) {
649277e4ab7STrond Myklebust 		msg->msg_flags |= MSG_TRUNC;
650277e4ab7STrond Myklebust 		goto out;
651277e4ab7STrond Myklebust 	}
652277e4ab7STrond Myklebust 	xprt_pin_rqst(req);
653277e4ab7STrond Myklebust 	spin_unlock(&xprt->queue_lock);
654277e4ab7STrond Myklebust 
655277e4ab7STrond Myklebust 	ret = xs_read_stream_request(transport, msg, flags, req);
656277e4ab7STrond Myklebust 
657277e4ab7STrond Myklebust 	spin_lock(&xprt->queue_lock);
658277e4ab7STrond Myklebust 	if (msg->msg_flags & (MSG_EOR|MSG_TRUNC))
659c4433055STrond Myklebust 		xprt_complete_rqst(req->rq_task, transport->recv.copied);
66045835a63STrond Myklebust 	else
66145835a63STrond Myklebust 		req->rq_private_buf.len = transport->recv.copied;
662277e4ab7STrond Myklebust 	xprt_unpin_rqst(req);
663277e4ab7STrond Myklebust out:
664277e4ab7STrond Myklebust 	spin_unlock(&xprt->queue_lock);
665277e4ab7STrond Myklebust 	return ret;
666277e4ab7STrond Myklebust }
667277e4ab7STrond Myklebust 
668277e4ab7STrond Myklebust static ssize_t
669277e4ab7STrond Myklebust xs_read_stream(struct sock_xprt *transport, int flags)
670277e4ab7STrond Myklebust {
671277e4ab7STrond Myklebust 	struct msghdr msg = { 0 };
672277e4ab7STrond Myklebust 	size_t want, read = 0;
673277e4ab7STrond Myklebust 	ssize_t ret = 0;
674277e4ab7STrond Myklebust 
675277e4ab7STrond Myklebust 	if (transport->recv.len == 0) {
676277e4ab7STrond Myklebust 		want = xs_read_stream_headersize(transport->recv.copied != 0);
677277e4ab7STrond Myklebust 		ret = xs_read_stream_header(transport, &msg, flags, want,
678277e4ab7STrond Myklebust 				transport->recv.offset);
679277e4ab7STrond Myklebust 		if (ret <= 0)
680277e4ab7STrond Myklebust 			goto out_err;
681277e4ab7STrond Myklebust 		transport->recv.offset = ret;
682c4433055STrond Myklebust 		if (transport->recv.offset != want)
683c4433055STrond Myklebust 			return transport->recv.offset;
684277e4ab7STrond Myklebust 		transport->recv.len = be32_to_cpu(transport->recv.fraghdr) &
685277e4ab7STrond Myklebust 			RPC_FRAGMENT_SIZE_MASK;
686277e4ab7STrond Myklebust 		transport->recv.offset -= sizeof(transport->recv.fraghdr);
687277e4ab7STrond Myklebust 		read = ret;
688277e4ab7STrond Myklebust 	}
689277e4ab7STrond Myklebust 
690277e4ab7STrond Myklebust 	switch (be32_to_cpu(transport->recv.calldir)) {
691c4433055STrond Myklebust 	default:
692c4433055STrond Myklebust 		msg.msg_flags |= MSG_TRUNC;
693c4433055STrond Myklebust 		break;
694277e4ab7STrond Myklebust 	case RPC_CALL:
695277e4ab7STrond Myklebust 		ret = xs_read_stream_call(transport, &msg, flags);
696277e4ab7STrond Myklebust 		break;
697277e4ab7STrond Myklebust 	case RPC_REPLY:
698277e4ab7STrond Myklebust 		ret = xs_read_stream_reply(transport, &msg, flags);
699277e4ab7STrond Myklebust 	}
700277e4ab7STrond Myklebust 	if (msg.msg_flags & MSG_TRUNC) {
701277e4ab7STrond Myklebust 		transport->recv.calldir = cpu_to_be32(-1);
702277e4ab7STrond Myklebust 		transport->recv.copied = -1;
703277e4ab7STrond Myklebust 	}
704277e4ab7STrond Myklebust 	if (ret < 0)
705277e4ab7STrond Myklebust 		goto out_err;
706277e4ab7STrond Myklebust 	read += ret;
707277e4ab7STrond Myklebust 	if (transport->recv.offset < transport->recv.len) {
708c4433055STrond Myklebust 		if (!(msg.msg_flags & MSG_TRUNC))
709c4433055STrond Myklebust 			return read;
710b76a5afdSTrond Myklebust 		msg.msg_flags = 0;
711277e4ab7STrond Myklebust 		ret = xs_read_discard(transport->sock, &msg, flags,
712277e4ab7STrond Myklebust 				transport->recv.len - transport->recv.offset);
713277e4ab7STrond Myklebust 		if (ret <= 0)
714277e4ab7STrond Myklebust 			goto out_err;
715277e4ab7STrond Myklebust 		transport->recv.offset += ret;
716277e4ab7STrond Myklebust 		read += ret;
717277e4ab7STrond Myklebust 		if (transport->recv.offset != transport->recv.len)
718c4433055STrond Myklebust 			return read;
719277e4ab7STrond Myklebust 	}
720277e4ab7STrond Myklebust 	if (xs_read_stream_request_done(transport)) {
721c50b8ee0STrond Myklebust 		trace_xs_stream_read_request(transport);
722277e4ab7STrond Myklebust 		transport->recv.copied = 0;
723277e4ab7STrond Myklebust 	}
724277e4ab7STrond Myklebust 	transport->recv.offset = 0;
725277e4ab7STrond Myklebust 	transport->recv.len = 0;
726277e4ab7STrond Myklebust 	return read;
727277e4ab7STrond Myklebust out_err:
72879462857STrond Myklebust 	return ret != 0 ? ret : -ESHUTDOWN;
729277e4ab7STrond Myklebust }
730277e4ab7STrond Myklebust 
7310ffe86f4STrond Myklebust static __poll_t xs_poll_socket(struct sock_xprt *transport)
7320ffe86f4STrond Myklebust {
733a73881c9STrond Myklebust 	return transport->sock->ops->poll(transport->file, transport->sock,
734a73881c9STrond Myklebust 			NULL);
7350ffe86f4STrond Myklebust }
7360ffe86f4STrond Myklebust 
7370ffe86f4STrond Myklebust static bool xs_poll_socket_readable(struct sock_xprt *transport)
7380ffe86f4STrond Myklebust {
7390ffe86f4STrond Myklebust 	__poll_t events = xs_poll_socket(transport);
7400ffe86f4STrond Myklebust 
7410ffe86f4STrond Myklebust 	return (events & (EPOLLIN | EPOLLRDNORM)) && !(events & EPOLLRDHUP);
7420ffe86f4STrond Myklebust }
7430ffe86f4STrond Myklebust 
7440ffe86f4STrond Myklebust static void xs_poll_check_readable(struct sock_xprt *transport)
7450ffe86f4STrond Myklebust {
7460ffe86f4STrond Myklebust 
7470ffe86f4STrond Myklebust 	clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state);
7480d3ca07fSChuck Lever 	if (test_bit(XPRT_SOCK_IGNORE_RECV, &transport->sock_state))
7490d3ca07fSChuck Lever 		return;
7500ffe86f4STrond Myklebust 	if (!xs_poll_socket_readable(transport))
7510ffe86f4STrond Myklebust 		return;
7520ffe86f4STrond Myklebust 	if (!test_and_set_bit(XPRT_SOCK_DATA_READY, &transport->sock_state))
7530ffe86f4STrond Myklebust 		queue_work(xprtiod_workqueue, &transport->recv_worker);
7540ffe86f4STrond Myklebust }
7550ffe86f4STrond Myklebust 
756c50b8ee0STrond Myklebust static void xs_stream_data_receive(struct sock_xprt *transport)
757c50b8ee0STrond Myklebust {
758c50b8ee0STrond Myklebust 	size_t read = 0;
759c50b8ee0STrond Myklebust 	ssize_t ret = 0;
760c50b8ee0STrond Myklebust 
761c50b8ee0STrond Myklebust 	mutex_lock(&transport->recv_mutex);
762c50b8ee0STrond Myklebust 	if (transport->sock == NULL)
763c50b8ee0STrond Myklebust 		goto out;
764c50b8ee0STrond Myklebust 	for (;;) {
765c50b8ee0STrond Myklebust 		ret = xs_read_stream(transport, MSG_DONTWAIT);
766c4433055STrond Myklebust 		if (ret < 0)
767c50b8ee0STrond Myklebust 			break;
768c50b8ee0STrond Myklebust 		read += ret;
769c50b8ee0STrond Myklebust 		cond_resched();
770c50b8ee0STrond Myklebust 	}
7715f52a9d4STrond Myklebust 	if (ret == -ESHUTDOWN)
7725f52a9d4STrond Myklebust 		kernel_sock_shutdown(transport->sock, SHUT_RDWR);
7735f52a9d4STrond Myklebust 	else
7740ffe86f4STrond Myklebust 		xs_poll_check_readable(transport);
775c50b8ee0STrond Myklebust out:
776c50b8ee0STrond Myklebust 	mutex_unlock(&transport->recv_mutex);
777c50b8ee0STrond Myklebust 	trace_xs_stream_read_data(&transport->xprt, ret, read);
778c50b8ee0STrond Myklebust }
779c50b8ee0STrond Myklebust 
780c50b8ee0STrond Myklebust static void xs_stream_data_receive_workfn(struct work_struct *work)
781c50b8ee0STrond Myklebust {
782c50b8ee0STrond Myklebust 	struct sock_xprt *transport =
783c50b8ee0STrond Myklebust 		container_of(work, struct sock_xprt, recv_worker);
784a1231fdaSTrond Myklebust 	unsigned int pflags = memalloc_nofs_save();
785a1231fdaSTrond Myklebust 
786c50b8ee0STrond Myklebust 	xs_stream_data_receive(transport);
787a1231fdaSTrond Myklebust 	memalloc_nofs_restore(pflags);
788c50b8ee0STrond Myklebust }
789c50b8ee0STrond Myklebust 
790550aebfeSTrond Myklebust static void
791550aebfeSTrond Myklebust xs_stream_reset_connect(struct sock_xprt *transport)
792550aebfeSTrond Myklebust {
793550aebfeSTrond Myklebust 	transport->recv.offset = 0;
794550aebfeSTrond Myklebust 	transport->recv.len = 0;
795550aebfeSTrond Myklebust 	transport->recv.copied = 0;
796550aebfeSTrond Myklebust 	transport->xmit.offset = 0;
797ae053551STrond Myklebust }
798ae053551STrond Myklebust 
799ae053551STrond Myklebust static void
800ae053551STrond Myklebust xs_stream_start_connect(struct sock_xprt *transport)
801ae053551STrond Myklebust {
802550aebfeSTrond Myklebust 	transport->xprt.stat.connect_count++;
803550aebfeSTrond Myklebust 	transport->xprt.stat.connect_start = jiffies;
804550aebfeSTrond Myklebust }
805550aebfeSTrond Myklebust 
806b4b5cc85SChuck Lever #define XS_SENDMSG_FLAGS	(MSG_DONTWAIT | MSG_NOSIGNAL)
807b4b5cc85SChuck Lever 
8089903cd1cSChuck Lever /**
809c544577dSTrond Myklebust  * xs_nospace - handle transmit was incomplete
81050f484e2STrond Myklebust  * @req: pointer to RPC request
8117496b59fSTrond Myklebust  * @transport: pointer to struct sock_xprt
8129903cd1cSChuck Lever  *
813a246b010SChuck Lever  */
8147496b59fSTrond Myklebust static int xs_nospace(struct rpc_rqst *req, struct sock_xprt *transport)
815a246b010SChuck Lever {
8167496b59fSTrond Myklebust 	struct rpc_xprt *xprt = &transport->xprt;
81706ea0bfeSTrond Myklebust 	struct sock *sk = transport->inet;
81824ca9a84STrond Myklebust 	int ret = -EAGAIN;
819a246b010SChuck Lever 
820015747d2SChuck Lever 	trace_rpc_socket_nospace(req, transport);
821a246b010SChuck Lever 
822262965f5SChuck Lever 	/* Protect against races with write_space */
823b5e92419STrond Myklebust 	spin_lock(&xprt->transport_lock);
824a246b010SChuck Lever 
825262965f5SChuck Lever 	/* Don't race with disconnect */
826b6ddf64fSTrond Myklebust 	if (xprt_connected(xprt)) {
8277496b59fSTrond Myklebust 		/* wait for more buffer space */
8282790a624STrond Myklebust 		set_bit(XPRT_SOCK_NOSPACE, &transport->sock_state);
8297496b59fSTrond Myklebust 		set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
8307496b59fSTrond Myklebust 		sk->sk_write_pending++;
8317496b59fSTrond Myklebust 		xprt_wait_for_buffer_space(xprt);
8327496b59fSTrond Myklebust 	} else
8337496b59fSTrond Myklebust 		ret = -ENOTCONN;
8347496b59fSTrond Myklebust 
8357496b59fSTrond Myklebust 	spin_unlock(&xprt->transport_lock);
8367496b59fSTrond Myklebust 	return ret;
837d48f9ce7SDavid Vrabel }
8387496b59fSTrond Myklebust 
8397496b59fSTrond Myklebust static int xs_sock_nospace(struct rpc_rqst *req)
8407496b59fSTrond Myklebust {
8417496b59fSTrond Myklebust 	struct sock_xprt *transport =
8427496b59fSTrond Myklebust 		container_of(req->rq_xprt, struct sock_xprt, xprt);
8437496b59fSTrond Myklebust 	struct sock *sk = transport->inet;
8447496b59fSTrond Myklebust 	int ret = -EAGAIN;
8457496b59fSTrond Myklebust 
8467496b59fSTrond Myklebust 	lock_sock(sk);
8477496b59fSTrond Myklebust 	if (!sock_writeable(sk))
8487496b59fSTrond Myklebust 		ret = xs_nospace(req, transport);
8497496b59fSTrond Myklebust 	release_sock(sk);
8507496b59fSTrond Myklebust 	return ret;
8517496b59fSTrond Myklebust }
8527496b59fSTrond Myklebust 
853d0afde5fSTrond Myklebust static int xs_stream_nospace(struct rpc_rqst *req, bool vm_wait)
8547496b59fSTrond Myklebust {
8557496b59fSTrond Myklebust 	struct sock_xprt *transport =
8567496b59fSTrond Myklebust 		container_of(req->rq_xprt, struct sock_xprt, xprt);
8577496b59fSTrond Myklebust 	struct sock *sk = transport->inet;
8587496b59fSTrond Myklebust 	int ret = -EAGAIN;
8597496b59fSTrond Myklebust 
860d0afde5fSTrond Myklebust 	if (vm_wait)
861d0afde5fSTrond Myklebust 		return -ENOBUFS;
8627496b59fSTrond Myklebust 	lock_sock(sk);
8637496b59fSTrond Myklebust 	if (!sk_stream_memory_free(sk))
8647496b59fSTrond Myklebust 		ret = xs_nospace(req, transport);
8657496b59fSTrond Myklebust 	release_sock(sk);
8665e3771ceSTrond Myklebust 	return ret;
867a246b010SChuck Lever }
868a246b010SChuck Lever 
86972691a26STrond Myklebust static int xs_stream_prepare_request(struct rpc_rqst *req, struct xdr_buf *buf)
870277e4ab7STrond Myklebust {
87172691a26STrond Myklebust 	return xdr_alloc_bvec(buf, rpc_task_gfp_mask());
872277e4ab7STrond Myklebust }
873277e4ab7STrond Myklebust 
87461677eeeSChuck Lever /*
8754cd34e7cSTrond Myklebust  * Determine if the previous message in the stream was aborted before it
8764cd34e7cSTrond Myklebust  * could complete transmission.
8774cd34e7cSTrond Myklebust  */
8784cd34e7cSTrond Myklebust static bool
8794cd34e7cSTrond Myklebust xs_send_request_was_aborted(struct sock_xprt *transport, struct rpc_rqst *req)
8804cd34e7cSTrond Myklebust {
8814cd34e7cSTrond Myklebust 	return transport->xmit.offset != 0 && req->rq_bytes_sent == 0;
8824cd34e7cSTrond Myklebust }
8834cd34e7cSTrond Myklebust 
8844cd34e7cSTrond Myklebust /*
88506b5fc3aSTrond Myklebust  * Return the stream record marker field for a record of length < 2^31-1
88661677eeeSChuck Lever  */
88706b5fc3aSTrond Myklebust static rpc_fraghdr
88806b5fc3aSTrond Myklebust xs_stream_record_marker(struct xdr_buf *xdr)
88961677eeeSChuck Lever {
89006b5fc3aSTrond Myklebust 	if (!xdr->len)
89106b5fc3aSTrond Myklebust 		return 0;
89206b5fc3aSTrond Myklebust 	return cpu_to_be32(RPC_LAST_STREAM_FRAGMENT | (u32)xdr->len);
89361677eeeSChuck Lever }
89461677eeeSChuck Lever 
8959903cd1cSChuck Lever /**
896176e21eeSChuck Lever  * xs_local_send_request - write an RPC request to an AF_LOCAL socket
89750f484e2STrond Myklebust  * @req: pointer to RPC request
898176e21eeSChuck Lever  *
899176e21eeSChuck Lever  * Return values:
900176e21eeSChuck Lever  *        0:	The request has been sent
901176e21eeSChuck Lever  *   EAGAIN:	The socket was blocked, please call again later to
902176e21eeSChuck Lever  *		complete the request
903176e21eeSChuck Lever  * ENOTCONN:	Caller needs to invoke connect logic then call again
90412b20ce3SBhaskar Chowdhury  *    other:	Some other error occurred, the request was not sent
905176e21eeSChuck Lever  */
906adfa7144STrond Myklebust static int xs_local_send_request(struct rpc_rqst *req)
907176e21eeSChuck Lever {
908176e21eeSChuck Lever 	struct rpc_xprt *xprt = req->rq_xprt;
909176e21eeSChuck Lever 	struct sock_xprt *transport =
910176e21eeSChuck Lever 				container_of(xprt, struct sock_xprt, xprt);
911176e21eeSChuck Lever 	struct xdr_buf *xdr = &req->rq_snd_buf;
9127e3d3620STrond Myklebust 	rpc_fraghdr rm = xs_stream_record_marker(xdr);
9137e3d3620STrond Myklebust 	unsigned int msglen = rm ? req->rq_slen + sizeof(rm) : req->rq_slen;
9149e55eef4SChuck Lever 	struct msghdr msg = {
9159e55eef4SChuck Lever 		.msg_flags	= XS_SENDMSG_FLAGS,
9169e55eef4SChuck Lever 	};
917d0afde5fSTrond Myklebust 	bool vm_wait;
9183f649ab7SKees Cook 	unsigned int sent;
919176e21eeSChuck Lever 	int status;
920176e21eeSChuck Lever 
9214cd34e7cSTrond Myklebust 	/* Close the stream if the previous transmission was incomplete */
9224cd34e7cSTrond Myklebust 	if (xs_send_request_was_aborted(transport, req)) {
923f0043206STrond Myklebust 		xprt_force_disconnect(xprt);
9244cd34e7cSTrond Myklebust 		return -ENOTCONN;
9254cd34e7cSTrond Myklebust 	}
9264cd34e7cSTrond Myklebust 
927176e21eeSChuck Lever 	xs_pktdump("packet data:",
928176e21eeSChuck Lever 			req->rq_svec->iov_base, req->rq_svec->iov_len);
929176e21eeSChuck Lever 
930d0afde5fSTrond Myklebust 	vm_wait = sk_stream_is_writeable(transport->inet) ? true : false;
931d0afde5fSTrond Myklebust 
93278215759SChuck Lever 	req->rq_xtime = ktime_get();
9339e55eef4SChuck Lever 	status = xprt_sock_sendmsg(transport->sock, &msg, xdr,
9347e3d3620STrond Myklebust 				   transport->xmit.offset, rm, &sent);
935176e21eeSChuck Lever 	dprintk("RPC:       %s(%u) = %d\n",
9366c7a64e5STrond Myklebust 			__func__, xdr->len - transport->xmit.offset, status);
937743c69e7SNeilBrown 
938f279cd00SJason Baron 	if (likely(sent > 0) || status == 0) {
9396c7a64e5STrond Myklebust 		transport->xmit.offset += sent;
9406c7a64e5STrond Myklebust 		req->rq_bytes_sent = transport->xmit.offset;
9417e3d3620STrond Myklebust 		if (likely(req->rq_bytes_sent >= msglen)) {
9426c7a64e5STrond Myklebust 			req->rq_xmit_bytes_sent += transport->xmit.offset;
9436c7a64e5STrond Myklebust 			transport->xmit.offset = 0;
944176e21eeSChuck Lever 			return 0;
945176e21eeSChuck Lever 		}
946176e21eeSChuck Lever 		status = -EAGAIN;
947d0afde5fSTrond Myklebust 		vm_wait = false;
948176e21eeSChuck Lever 	}
949176e21eeSChuck Lever 
950176e21eeSChuck Lever 	switch (status) {
951176e21eeSChuck Lever 	case -EAGAIN:
952d0afde5fSTrond Myklebust 		status = xs_stream_nospace(req, vm_wait);
953176e21eeSChuck Lever 		break;
954176e21eeSChuck Lever 	default:
955176e21eeSChuck Lever 		dprintk("RPC:       sendmsg returned unrecognized error %d\n",
956176e21eeSChuck Lever 			-status);
957df561f66SGustavo A. R. Silva 		fallthrough;
958176e21eeSChuck Lever 	case -EPIPE:
959f0043206STrond Myklebust 		xprt_force_disconnect(xprt);
960176e21eeSChuck Lever 		status = -ENOTCONN;
961176e21eeSChuck Lever 	}
962176e21eeSChuck Lever 
963176e21eeSChuck Lever 	return status;
964176e21eeSChuck Lever }
965176e21eeSChuck Lever 
966176e21eeSChuck Lever /**
967262965f5SChuck Lever  * xs_udp_send_request - write an RPC request to a UDP socket
96850f484e2STrond Myklebust  * @req: pointer to RPC request
9699903cd1cSChuck Lever  *
9709903cd1cSChuck Lever  * Return values:
9719903cd1cSChuck Lever  *        0:	The request has been sent
9729903cd1cSChuck Lever  *   EAGAIN:	The socket was blocked, please call again later to
9739903cd1cSChuck Lever  *		complete the request
974262965f5SChuck Lever  * ENOTCONN:	Caller needs to invoke connect logic then call again
97525985edcSLucas De Marchi  *    other:	Some other error occurred, the request was not sent
9769903cd1cSChuck Lever  */
977adfa7144STrond Myklebust static int xs_udp_send_request(struct rpc_rqst *req)
978a246b010SChuck Lever {
979a246b010SChuck Lever 	struct rpc_xprt *xprt = req->rq_xprt;
980ee0ac0c2SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
981262965f5SChuck Lever 	struct xdr_buf *xdr = &req->rq_snd_buf;
9829e55eef4SChuck Lever 	struct msghdr msg = {
9839e55eef4SChuck Lever 		.msg_name	= xs_addr(xprt),
9849e55eef4SChuck Lever 		.msg_namelen	= xprt->addrlen,
9859e55eef4SChuck Lever 		.msg_flags	= XS_SENDMSG_FLAGS,
9869e55eef4SChuck Lever 	};
9873f649ab7SKees Cook 	unsigned int sent;
988262965f5SChuck Lever 	int status;
989262965f5SChuck Lever 
990262965f5SChuck Lever 	xs_pktdump("packet data:",
991262965f5SChuck Lever 				req->rq_svec->iov_base,
992262965f5SChuck Lever 				req->rq_svec->iov_len);
993262965f5SChuck Lever 
99401d37c42STrond Myklebust 	if (!xprt_bound(xprt))
99501d37c42STrond Myklebust 		return -ENOTCONN;
99675891f50STrond Myklebust 
99775891f50STrond Myklebust 	if (!xprt_request_get_cong(xprt, req))
99875891f50STrond Myklebust 		return -EBADSLT;
99975891f50STrond Myklebust 
1000ff053dbbSTrond Myklebust 	status = xdr_alloc_bvec(xdr, rpc_task_gfp_mask());
1001ff053dbbSTrond Myklebust 	if (status < 0)
1002ff053dbbSTrond Myklebust 		return status;
100378215759SChuck Lever 	req->rq_xtime = ktime_get();
10049e55eef4SChuck Lever 	status = xprt_sock_sendmsg(transport->sock, &msg, xdr, 0, 0, &sent);
1005262965f5SChuck Lever 
1006262965f5SChuck Lever 	dprintk("RPC:       xs_udp_send_request(%u) = %d\n",
10076c7a64e5STrond Myklebust 			xdr->len, status);
1008262965f5SChuck Lever 
10093dedbb5cSJason Baron 	/* firewall is blocking us, don't return -EAGAIN or we end up looping */
10103dedbb5cSJason Baron 	if (status == -EPERM)
10113dedbb5cSJason Baron 		goto process_status;
10123dedbb5cSJason Baron 
1013743c69e7SNeilBrown 	if (status == -EAGAIN && sock_writeable(transport->inet))
1014743c69e7SNeilBrown 		status = -ENOBUFS;
1015743c69e7SNeilBrown 
1016f279cd00SJason Baron 	if (sent > 0 || status == 0) {
1017f279cd00SJason Baron 		req->rq_xmit_bytes_sent += sent;
1018f279cd00SJason Baron 		if (sent >= req->rq_slen)
1019262965f5SChuck Lever 			return 0;
1020262965f5SChuck Lever 		/* Still some bytes left; set up for a retry later. */
1021262965f5SChuck Lever 		status = -EAGAIN;
10222199700fSTrond Myklebust 	}
1023262965f5SChuck Lever 
10243dedbb5cSJason Baron process_status:
1025262965f5SChuck Lever 	switch (status) {
1026fba91afbSTrond Myklebust 	case -ENOTSOCK:
1027fba91afbSTrond Myklebust 		status = -ENOTCONN;
1028fba91afbSTrond Myklebust 		/* Should we call xs_close() here? */
1029fba91afbSTrond Myklebust 		break;
1030b6ddf64fSTrond Myklebust 	case -EAGAIN:
10317496b59fSTrond Myklebust 		status = xs_sock_nospace(req);
1032b6ddf64fSTrond Myklebust 		break;
1033262965f5SChuck Lever 	case -ENETUNREACH:
10343601c4a9STrond Myklebust 	case -ENOBUFS:
1035262965f5SChuck Lever 	case -EPIPE:
1036262965f5SChuck Lever 	case -ECONNREFUSED:
10373dedbb5cSJason Baron 	case -EPERM:
1038262965f5SChuck Lever 		/* When the server has died, an ICMP port unreachable message
1039262965f5SChuck Lever 		 * prompts ECONNREFUSED. */
104013331a55STrond Myklebust 		break;
104113331a55STrond Myklebust 	default:
104213331a55STrond Myklebust 		dprintk("RPC:       sendmsg returned unrecognized error %d\n",
104313331a55STrond Myklebust 			-status);
1044262965f5SChuck Lever 	}
10455fe46e9dSBian Naimeng 
1046262965f5SChuck Lever 	return status;
1047262965f5SChuck Lever }
1048262965f5SChuck Lever 
1049e06799f9STrond Myklebust /**
1050262965f5SChuck Lever  * xs_tcp_send_request - write an RPC request to a TCP socket
105150f484e2STrond Myklebust  * @req: pointer to RPC request
1052262965f5SChuck Lever  *
1053262965f5SChuck Lever  * Return values:
1054262965f5SChuck Lever  *        0:	The request has been sent
1055262965f5SChuck Lever  *   EAGAIN:	The socket was blocked, please call again later to
1056262965f5SChuck Lever  *		complete the request
1057262965f5SChuck Lever  * ENOTCONN:	Caller needs to invoke connect logic then call again
105825985edcSLucas De Marchi  *    other:	Some other error occurred, the request was not sent
1059262965f5SChuck Lever  *
1060262965f5SChuck Lever  * XXX: In the case of soft timeouts, should we eventually give up
1061262965f5SChuck Lever  *	if sendmsg is not able to make progress?
1062262965f5SChuck Lever  */
1063adfa7144STrond Myklebust static int xs_tcp_send_request(struct rpc_rqst *req)
1064262965f5SChuck Lever {
1065262965f5SChuck Lever 	struct rpc_xprt *xprt = req->rq_xprt;
1066ee0ac0c2SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1067262965f5SChuck Lever 	struct xdr_buf *xdr = &req->rq_snd_buf;
10687e3d3620STrond Myklebust 	rpc_fraghdr rm = xs_stream_record_marker(xdr);
10697e3d3620STrond Myklebust 	unsigned int msglen = rm ? req->rq_slen + sizeof(rm) : req->rq_slen;
10709e55eef4SChuck Lever 	struct msghdr msg = {
10719e55eef4SChuck Lever 		.msg_flags	= XS_SENDMSG_FLAGS,
10729e55eef4SChuck Lever 	};
1073d0afde5fSTrond Myklebust 	bool vm_wait;
10743f649ab7SKees Cook 	unsigned int sent;
1075b595bb15SChuck Lever 	int status;
1076a246b010SChuck Lever 
10774cd34e7cSTrond Myklebust 	/* Close the stream if the previous transmission was incomplete */
10784cd34e7cSTrond Myklebust 	if (xs_send_request_was_aborted(transport, req)) {
10794cd34e7cSTrond Myklebust 		if (transport->sock != NULL)
10804cd34e7cSTrond Myklebust 			kernel_sock_shutdown(transport->sock, SHUT_RDWR);
10814cd34e7cSTrond Myklebust 		return -ENOTCONN;
10824cd34e7cSTrond Myklebust 	}
1083d275880aSTrond Myklebust 	if (!transport->inet)
1084d275880aSTrond Myklebust 		return -ENOTCONN;
10854cd34e7cSTrond Myklebust 
1086262965f5SChuck Lever 	xs_pktdump("packet data:",
1087262965f5SChuck Lever 				req->rq_svec->iov_base,
1088262965f5SChuck Lever 				req->rq_svec->iov_len);
1089a246b010SChuck Lever 
10907196dbb0STrond Myklebust 	if (test_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state))
10917196dbb0STrond Myklebust 		xs_tcp_set_socket_timeouts(xprt, transport->sock);
10927196dbb0STrond Myklebust 
10933b21f757STrond Myklebust 	xs_set_srcport(transport, transport->sock);
10943b21f757STrond Myklebust 
1095a246b010SChuck Lever 	/* Continue transmitting the packet/record. We must be careful
1096a246b010SChuck Lever 	 * to cope with writespace callbacks arriving _after_ we have
1097262965f5SChuck Lever 	 * called sendmsg(). */
109878215759SChuck Lever 	req->rq_xtime = ktime_get();
1099d737e5d4STrond Myklebust 	tcp_sock_set_cork(transport->inet, true);
1100d0afde5fSTrond Myklebust 
1101d0afde5fSTrond Myklebust 	vm_wait = sk_stream_is_writeable(transport->inet) ? true : false;
1102d0afde5fSTrond Myklebust 
1103d0afde5fSTrond Myklebust 	do {
11049e55eef4SChuck Lever 		status = xprt_sock_sendmsg(transport->sock, &msg, xdr,
11057e3d3620STrond Myklebust 					   transport->xmit.offset, rm, &sent);
1106a246b010SChuck Lever 
1107262965f5SChuck Lever 		dprintk("RPC:       xs_tcp_send_request(%u) = %d\n",
11086c7a64e5STrond Myklebust 				xdr->len - transport->xmit.offset, status);
1109262965f5SChuck Lever 
1110a246b010SChuck Lever 		/* If we've sent the entire packet, immediately
1111a246b010SChuck Lever 		 * reset the count of bytes sent. */
11126c7a64e5STrond Myklebust 		transport->xmit.offset += sent;
11136c7a64e5STrond Myklebust 		req->rq_bytes_sent = transport->xmit.offset;
11147e3d3620STrond Myklebust 		if (likely(req->rq_bytes_sent >= msglen)) {
11156c7a64e5STrond Myklebust 			req->rq_xmit_bytes_sent += transport->xmit.offset;
11166c7a64e5STrond Myklebust 			transport->xmit.offset = 0;
1117d737e5d4STrond Myklebust 			if (atomic_long_read(&xprt->xmit_queuelen) == 1)
1118d737e5d4STrond Myklebust 				tcp_sock_set_cork(transport->inet, false);
1119a246b010SChuck Lever 			return 0;
1120a246b010SChuck Lever 		}
1121262965f5SChuck Lever 
11229ffadfbcSTrond Myklebust 		WARN_ON_ONCE(sent == 0 && status == 0);
11239ffadfbcSTrond Myklebust 
1124d0afde5fSTrond Myklebust 		if (sent > 0)
11259ffadfbcSTrond Myklebust 			vm_wait = false;
1126d0afde5fSTrond Myklebust 
1127d0afde5fSTrond Myklebust 	} while (status == 0);
1128a246b010SChuck Lever 
1129262965f5SChuck Lever 	switch (status) {
1130fba91afbSTrond Myklebust 	case -ENOTSOCK:
1131fba91afbSTrond Myklebust 		status = -ENOTCONN;
1132fba91afbSTrond Myklebust 		/* Should we call xs_close() here? */
1133fba91afbSTrond Myklebust 		break;
1134262965f5SChuck Lever 	case -EAGAIN:
1135d0afde5fSTrond Myklebust 		status = xs_stream_nospace(req, vm_wait);
1136262965f5SChuck Lever 		break;
1137262965f5SChuck Lever 	case -ECONNRESET:
1138262965f5SChuck Lever 	case -ECONNREFUSED:
1139262965f5SChuck Lever 	case -ENOTCONN:
11403913c78cSTrond Myklebust 	case -EADDRINUSE:
1141b5872f0cSTrond Myklebust 	case -ENOBUFS:
1142b9d2bb2eSTrond Myklebust 	case -EPIPE:
114313331a55STrond Myklebust 		break;
114413331a55STrond Myklebust 	default:
114513331a55STrond Myklebust 		dprintk("RPC:       sendmsg returned unrecognized error %d\n",
114613331a55STrond Myklebust 			-status);
1147a246b010SChuck Lever 	}
11485fe46e9dSBian Naimeng 
1149a246b010SChuck Lever 	return status;
1150a246b010SChuck Lever }
1151a246b010SChuck Lever 
11522a9e1cfaSTrond Myklebust static void xs_save_old_callbacks(struct sock_xprt *transport, struct sock *sk)
11532a9e1cfaSTrond Myklebust {
11542a9e1cfaSTrond Myklebust 	transport->old_data_ready = sk->sk_data_ready;
11552a9e1cfaSTrond Myklebust 	transport->old_state_change = sk->sk_state_change;
11562a9e1cfaSTrond Myklebust 	transport->old_write_space = sk->sk_write_space;
11572118071dSTrond Myklebust 	transport->old_error_report = sk->sk_error_report;
11582a9e1cfaSTrond Myklebust }
11592a9e1cfaSTrond Myklebust 
11602a9e1cfaSTrond Myklebust static void xs_restore_old_callbacks(struct sock_xprt *transport, struct sock *sk)
11612a9e1cfaSTrond Myklebust {
11622a9e1cfaSTrond Myklebust 	sk->sk_data_ready = transport->old_data_ready;
11632a9e1cfaSTrond Myklebust 	sk->sk_state_change = transport->old_state_change;
11642a9e1cfaSTrond Myklebust 	sk->sk_write_space = transport->old_write_space;
11652118071dSTrond Myklebust 	sk->sk_error_report = transport->old_error_report;
11662118071dSTrond Myklebust }
11672118071dSTrond Myklebust 
116842d42a5bSTrond Myklebust static void xs_sock_reset_state_flags(struct rpc_xprt *xprt)
116942d42a5bSTrond Myklebust {
117042d42a5bSTrond Myklebust 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
117142d42a5bSTrond Myklebust 
117242d42a5bSTrond Myklebust 	clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state);
11734f8943f8STrond Myklebust 	clear_bit(XPRT_SOCK_WAKE_ERROR, &transport->sock_state);
11744f8943f8STrond Myklebust 	clear_bit(XPRT_SOCK_WAKE_WRITE, &transport->sock_state);
11754f8943f8STrond Myklebust 	clear_bit(XPRT_SOCK_WAKE_DISCONNECT, &transport->sock_state);
11762790a624STrond Myklebust 	clear_bit(XPRT_SOCK_NOSPACE, &transport->sock_state);
11774f8943f8STrond Myklebust }
11784f8943f8STrond Myklebust 
11794f8943f8STrond Myklebust static void xs_run_error_worker(struct sock_xprt *transport, unsigned int nr)
11804f8943f8STrond Myklebust {
11814f8943f8STrond Myklebust 	set_bit(nr, &transport->sock_state);
11824f8943f8STrond Myklebust 	queue_work(xprtiod_workqueue, &transport->error_worker);
118342d42a5bSTrond Myklebust }
118442d42a5bSTrond Myklebust 
1185b70ae915STrond Myklebust static void xs_sock_reset_connection_flags(struct rpc_xprt *xprt)
1186b70ae915STrond Myklebust {
1187d896ba83STrond Myklebust 	xprt->connect_cookie++;
1188b70ae915STrond Myklebust 	smp_mb__before_atomic();
1189b70ae915STrond Myklebust 	clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
1190b70ae915STrond Myklebust 	clear_bit(XPRT_CLOSING, &xprt->state);
119142d42a5bSTrond Myklebust 	xs_sock_reset_state_flags(xprt);
1192b70ae915STrond Myklebust 	smp_mb__after_atomic();
1193b70ae915STrond Myklebust }
1194b70ae915STrond Myklebust 
11952118071dSTrond Myklebust /**
11962118071dSTrond Myklebust  * xs_error_report - callback to handle TCP socket state errors
11972118071dSTrond Myklebust  * @sk: socket
11982118071dSTrond Myklebust  *
11992118071dSTrond Myklebust  * Note: we don't call sock_error() since there may be a rpc_task
12002118071dSTrond Myklebust  * using the socket, and so we don't want to clear sk->sk_err.
12012118071dSTrond Myklebust  */
12022118071dSTrond Myklebust static void xs_error_report(struct sock *sk)
12032118071dSTrond Myklebust {
12044f8943f8STrond Myklebust 	struct sock_xprt *transport;
12052118071dSTrond Myklebust 	struct rpc_xprt *xprt;
12062118071dSTrond Myklebust 
12072118071dSTrond Myklebust 	if (!(xprt = xprt_from_sock(sk)))
1208ea9afca8STrond Myklebust 		return;
12092118071dSTrond Myklebust 
12104f8943f8STrond Myklebust 	transport = container_of(xprt, struct sock_xprt, xprt);
1211af84537dSBenjamin Coddington 	transport->xprt_err = -sk->sk_err;
1212af84537dSBenjamin Coddington 	if (transport->xprt_err == 0)
1213ea9afca8STrond Myklebust 		return;
12142118071dSTrond Myklebust 	dprintk("RPC:       xs_error_report client %p, error=%d...\n",
1215af84537dSBenjamin Coddington 			xprt, -transport->xprt_err);
1216af84537dSBenjamin Coddington 	trace_rpc_socket_error(xprt, sk->sk_socket, transport->xprt_err);
1217af84537dSBenjamin Coddington 
1218af84537dSBenjamin Coddington 	/* barrier ensures xprt_err is set before XPRT_SOCK_WAKE_ERROR */
1219af84537dSBenjamin Coddington 	smp_mb__before_atomic();
12204f8943f8STrond Myklebust 	xs_run_error_worker(transport, XPRT_SOCK_WAKE_ERROR);
12212a9e1cfaSTrond Myklebust }
12222a9e1cfaSTrond Myklebust 
1223fe315e76SChuck Lever static void xs_reset_transport(struct sock_xprt *transport)
1224a246b010SChuck Lever {
1225ee0ac0c2SChuck Lever 	struct socket *sock = transport->sock;
1226ee0ac0c2SChuck Lever 	struct sock *sk = transport->inet;
12276cc7e908STrond Myklebust 	struct rpc_xprt *xprt = &transport->xprt;
1228a73881c9STrond Myklebust 	struct file *filp = transport->file;
1229a246b010SChuck Lever 
1230fe315e76SChuck Lever 	if (sk == NULL)
1231fe315e76SChuck Lever 		return;
1232f0043206STrond Myklebust 	/*
1233f0043206STrond Myklebust 	 * Make sure we're calling this in a context from which it is safe
1234f0043206STrond Myklebust 	 * to call __fput_sync(). In practice that means rpciod and the
1235f0043206STrond Myklebust 	 * system workqueue.
1236f0043206STrond Myklebust 	 */
1237f0043206STrond Myklebust 	if (!(current->flags & PF_WQ_WORKER)) {
1238f0043206STrond Myklebust 		WARN_ON_ONCE(1);
1239f0043206STrond Myklebust 		set_bit(XPRT_CLOSE_WAIT, &xprt->state);
1240f0043206STrond Myklebust 		return;
1241f0043206STrond Myklebust 	}
12429903cd1cSChuck Lever 
1243264d1df3SJeff Layton 	if (atomic_read(&transport->xprt.swapper))
1244264d1df3SJeff Layton 		sk_clear_memalloc(sk);
1245264d1df3SJeff Layton 
124609939204STrond Myklebust 	kernel_sock_shutdown(sock, SHUT_RDWR);
124709939204STrond Myklebust 
1248edc1b01cSTrond Myklebust 	mutex_lock(&transport->recv_mutex);
1249ea9afca8STrond Myklebust 	lock_sock(sk);
1250ee0ac0c2SChuck Lever 	transport->inet = NULL;
1251ee0ac0c2SChuck Lever 	transport->sock = NULL;
1252a73881c9STrond Myklebust 	transport->file = NULL;
1253a246b010SChuck Lever 
1254a246b010SChuck Lever 	sk->sk_user_data = NULL;
12552a9e1cfaSTrond Myklebust 
12562a9e1cfaSTrond Myklebust 	xs_restore_old_callbacks(transport, sk);
12570c78789eSTrond Myklebust 	xprt_clear_connected(xprt);
12586cc7e908STrond Myklebust 	xs_sock_reset_connection_flags(xprt);
1259ae053551STrond Myklebust 	/* Reset stream record info */
1260ae053551STrond Myklebust 	xs_stream_reset_connect(transport);
1261ea9afca8STrond Myklebust 	release_sock(sk);
1262edc1b01cSTrond Myklebust 	mutex_unlock(&transport->recv_mutex);
1263a246b010SChuck Lever 
12646cc7e908STrond Myklebust 	trace_rpc_socket_close(xprt, sock);
1265f0043206STrond Myklebust 	__fput_sync(filp);
12660445f92cSTrond Myklebust 
12670445f92cSTrond Myklebust 	xprt_disconnect_done(xprt);
1268fe315e76SChuck Lever }
1269fe315e76SChuck Lever 
1270fe315e76SChuck Lever /**
1271fe315e76SChuck Lever  * xs_close - close a socket
1272fe315e76SChuck Lever  * @xprt: transport
1273fe315e76SChuck Lever  *
1274fe315e76SChuck Lever  * This is used when all requests are complete; ie, no DRC state remains
1275fe315e76SChuck Lever  * on the server we want to save.
1276f75e6745STrond Myklebust  *
1277f75e6745STrond Myklebust  * The caller _must_ be holding XPRT_LOCKED in order to avoid issues with
1278f75e6745STrond Myklebust  * xs_reset_transport() zeroing the socket from underneath a writer.
1279fe315e76SChuck Lever  */
1280fe315e76SChuck Lever static void xs_close(struct rpc_xprt *xprt)
1281fe315e76SChuck Lever {
1282fe315e76SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1283fe315e76SChuck Lever 
1284fe315e76SChuck Lever 	dprintk("RPC:       xs_close xprt %p\n", xprt);
1285fe315e76SChuck Lever 
1286fe315e76SChuck Lever 	xs_reset_transport(transport);
128761d0a8e6SNeil Brown 	xprt->reestablish_timeout = 0;
1288a246b010SChuck Lever }
1289a246b010SChuck Lever 
12904a068258SChuck Lever static void xs_inject_disconnect(struct rpc_xprt *xprt)
12914a068258SChuck Lever {
12924a068258SChuck Lever 	dprintk("RPC:       injecting transport disconnect on xprt=%p\n",
12934a068258SChuck Lever 		xprt);
12944a068258SChuck Lever 	xprt_disconnect_done(xprt);
12954a068258SChuck Lever }
12964a068258SChuck Lever 
1297315f3812SKinglong Mee static void xs_xprt_free(struct rpc_xprt *xprt)
1298315f3812SKinglong Mee {
1299315f3812SKinglong Mee 	xs_free_peer_addresses(xprt);
1300315f3812SKinglong Mee 	xprt_free(xprt);
1301315f3812SKinglong Mee }
1302315f3812SKinglong Mee 
13039903cd1cSChuck Lever /**
13049903cd1cSChuck Lever  * xs_destroy - prepare to shutdown a transport
13059903cd1cSChuck Lever  * @xprt: doomed transport
13069903cd1cSChuck Lever  *
13079903cd1cSChuck Lever  */
13089903cd1cSChuck Lever static void xs_destroy(struct rpc_xprt *xprt)
1309a246b010SChuck Lever {
131003c78827STrond Myklebust 	struct sock_xprt *transport = container_of(xprt,
131103c78827STrond Myklebust 			struct sock_xprt, xprt);
13129903cd1cSChuck Lever 	dprintk("RPC:       xs_destroy xprt %p\n", xprt);
13139903cd1cSChuck Lever 
131403c78827STrond Myklebust 	cancel_delayed_work_sync(&transport->connect_worker);
1315a1311d87STrond Myklebust 	xs_close(xprt);
1316edc1b01cSTrond Myklebust 	cancel_work_sync(&transport->recv_worker);
1317b5e92419STrond Myklebust 	cancel_work_sync(&transport->error_worker);
1318315f3812SKinglong Mee 	xs_xprt_free(xprt);
1319a1311d87STrond Myklebust 	module_put(THIS_MODULE);
1320a246b010SChuck Lever }
1321a246b010SChuck Lever 
13229903cd1cSChuck Lever /**
1323f9b2ee71STrond Myklebust  * xs_udp_data_read_skb - receive callback for UDP sockets
1324f9b2ee71STrond Myklebust  * @xprt: transport
1325f9b2ee71STrond Myklebust  * @sk: socket
1326f9b2ee71STrond Myklebust  * @skb: skbuff
13279903cd1cSChuck Lever  *
1328a246b010SChuck Lever  */
1329f9b2ee71STrond Myklebust static void xs_udp_data_read_skb(struct rpc_xprt *xprt,
1330f9b2ee71STrond Myklebust 		struct sock *sk,
1331f9b2ee71STrond Myklebust 		struct sk_buff *skb)
1332a246b010SChuck Lever {
1333a246b010SChuck Lever 	struct rpc_task *task;
1334a246b010SChuck Lever 	struct rpc_rqst *rovr;
1335f9b2ee71STrond Myklebust 	int repsize, copied;
1336d8ed029dSAlexey Dobriyan 	u32 _xid;
1337d8ed029dSAlexey Dobriyan 	__be32 *xp;
1338a246b010SChuck Lever 
13391da8c681SWillem de Bruijn 	repsize = skb->len;
1340a246b010SChuck Lever 	if (repsize < 4) {
13419903cd1cSChuck Lever 		dprintk("RPC:       impossible RPC reply size %d!\n", repsize);
1342f9b2ee71STrond Myklebust 		return;
1343a246b010SChuck Lever 	}
1344a246b010SChuck Lever 
1345a246b010SChuck Lever 	/* Copy the XID from the skb... */
13461da8c681SWillem de Bruijn 	xp = skb_header_pointer(skb, 0, sizeof(_xid), &_xid);
1347a246b010SChuck Lever 	if (xp == NULL)
1348f9b2ee71STrond Myklebust 		return;
1349a246b010SChuck Lever 
1350a246b010SChuck Lever 	/* Look up and lock the request corresponding to the given XID */
135175c84151STrond Myklebust 	spin_lock(&xprt->queue_lock);
1352a246b010SChuck Lever 	rovr = xprt_lookup_rqst(xprt, *xp);
1353a246b010SChuck Lever 	if (!rovr)
1354a246b010SChuck Lever 		goto out_unlock;
1355729749bbSTrond Myklebust 	xprt_pin_rqst(rovr);
1356ecd465eeSChuck Lever 	xprt_update_rtt(rovr->rq_task);
135775c84151STrond Myklebust 	spin_unlock(&xprt->queue_lock);
1358a246b010SChuck Lever 	task = rovr->rq_task;
1359a246b010SChuck Lever 
1360a246b010SChuck Lever 	if ((copied = rovr->rq_private_buf.buflen) > repsize)
1361a246b010SChuck Lever 		copied = repsize;
1362a246b010SChuck Lever 
1363a246b010SChuck Lever 	/* Suck it into the iovec, verify checksum if not done by hw. */
13641781f7f5SHerbert Xu 	if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) {
136575c84151STrond Myklebust 		spin_lock(&xprt->queue_lock);
13660afa6b44STrond Myklebust 		__UDPX_INC_STATS(sk, UDP_MIB_INERRORS);
1367729749bbSTrond Myklebust 		goto out_unpin;
13681781f7f5SHerbert Xu 	}
13691781f7f5SHerbert Xu 
1370a246b010SChuck Lever 
1371b5e92419STrond Myklebust 	spin_lock(&xprt->transport_lock);
13726a24dfb6STrond Myklebust 	xprt_adjust_cwnd(xprt, task, copied);
1373b5e92419STrond Myklebust 	spin_unlock(&xprt->transport_lock);
137475c84151STrond Myklebust 	spin_lock(&xprt->queue_lock);
13751570c1e4SChuck Lever 	xprt_complete_rqst(task, copied);
13760afa6b44STrond Myklebust 	__UDPX_INC_STATS(sk, UDP_MIB_INDATAGRAMS);
1377729749bbSTrond Myklebust out_unpin:
1378729749bbSTrond Myklebust 	xprt_unpin_rqst(rovr);
1379a246b010SChuck Lever  out_unlock:
138075c84151STrond Myklebust 	spin_unlock(&xprt->queue_lock);
1381f9b2ee71STrond Myklebust }
1382f9b2ee71STrond Myklebust 
1383f9b2ee71STrond Myklebust static void xs_udp_data_receive(struct sock_xprt *transport)
1384f9b2ee71STrond Myklebust {
1385f9b2ee71STrond Myklebust 	struct sk_buff *skb;
1386f9b2ee71STrond Myklebust 	struct sock *sk;
1387f9b2ee71STrond Myklebust 	int err;
1388f9b2ee71STrond Myklebust 
1389f9b2ee71STrond Myklebust 	mutex_lock(&transport->recv_mutex);
1390f9b2ee71STrond Myklebust 	sk = transport->inet;
1391f9b2ee71STrond Myklebust 	if (sk == NULL)
1392f9b2ee71STrond Myklebust 		goto out;
1393f9b2ee71STrond Myklebust 	for (;;) {
1394ec095263SOliver Hartkopp 		skb = skb_recv_udp(sk, MSG_DONTWAIT, &err);
13954f546149STrond Myklebust 		if (skb == NULL)
13964f546149STrond Myklebust 			break;
1397f9b2ee71STrond Myklebust 		xs_udp_data_read_skb(&transport->xprt, sk, skb);
1398850cbaddSPaolo Abeni 		consume_skb(skb);
13990af3442aSTrond Myklebust 		cond_resched();
1400f9b2ee71STrond Myklebust 	}
14010ffe86f4STrond Myklebust 	xs_poll_check_readable(transport);
1402a246b010SChuck Lever out:
1403f9b2ee71STrond Myklebust 	mutex_unlock(&transport->recv_mutex);
1404f9b2ee71STrond Myklebust }
1405f9b2ee71STrond Myklebust 
1406f9b2ee71STrond Myklebust static void xs_udp_data_receive_workfn(struct work_struct *work)
1407f9b2ee71STrond Myklebust {
1408f9b2ee71STrond Myklebust 	struct sock_xprt *transport =
1409f9b2ee71STrond Myklebust 		container_of(work, struct sock_xprt, recv_worker);
1410a1231fdaSTrond Myklebust 	unsigned int pflags = memalloc_nofs_save();
1411a1231fdaSTrond Myklebust 
1412f9b2ee71STrond Myklebust 	xs_udp_data_receive(transport);
1413a1231fdaSTrond Myklebust 	memalloc_nofs_restore(pflags);
1414f9b2ee71STrond Myklebust }
1415f9b2ee71STrond Myklebust 
1416f9b2ee71STrond Myklebust /**
1417f67939e4SChuck Lever  * xs_data_ready - "data ready" callback for sockets
1418f9b2ee71STrond Myklebust  * @sk: socket with data to read
1419f9b2ee71STrond Myklebust  *
1420f9b2ee71STrond Myklebust  */
1421f9b2ee71STrond Myklebust static void xs_data_ready(struct sock *sk)
1422f9b2ee71STrond Myklebust {
1423f9b2ee71STrond Myklebust 	struct rpc_xprt *xprt;
1424f9b2ee71STrond Myklebust 
142540e0b090SPeilin Ye 	trace_sk_data_ready(sk);
142640e0b090SPeilin Ye 
1427f9b2ee71STrond Myklebust 	xprt = xprt_from_sock(sk);
1428f9b2ee71STrond Myklebust 	if (xprt != NULL) {
1429f9b2ee71STrond Myklebust 		struct sock_xprt *transport = container_of(xprt,
1430f9b2ee71STrond Myklebust 				struct sock_xprt, xprt);
1431f67939e4SChuck Lever 
1432f67939e4SChuck Lever 		trace_xs_data_ready(xprt);
1433f67939e4SChuck Lever 
14345157b956STrond Myklebust 		transport->old_data_ready(sk);
14350d3ca07fSChuck Lever 
14360d3ca07fSChuck Lever 		if (test_bit(XPRT_SOCK_IGNORE_RECV, &transport->sock_state))
14370d3ca07fSChuck Lever 			return;
14380d3ca07fSChuck Lever 
14395157b956STrond Myklebust 		/* Any data means we had a useful conversation, so
14405157b956STrond Myklebust 		 * then we don't need to delay the next reconnect
14415157b956STrond Myklebust 		 */
14425157b956STrond Myklebust 		if (xprt->reestablish_timeout)
14435157b956STrond Myklebust 			xprt->reestablish_timeout = 0;
144442d42a5bSTrond Myklebust 		if (!test_and_set_bit(XPRT_SOCK_DATA_READY, &transport->sock_state))
144540a5f1b1STrond Myklebust 			queue_work(xprtiod_workqueue, &transport->recv_worker);
1446f9b2ee71STrond Myklebust 	}
1447a246b010SChuck Lever }
1448a246b010SChuck Lever 
1449a519fc7aSTrond Myklebust /*
1450a519fc7aSTrond Myklebust  * Helper function to force a TCP close if the server is sending
1451a519fc7aSTrond Myklebust  * junk and/or it has put us in CLOSE_WAIT
1452a519fc7aSTrond Myklebust  */
1453a519fc7aSTrond Myklebust static void xs_tcp_force_close(struct rpc_xprt *xprt)
1454a519fc7aSTrond Myklebust {
1455a519fc7aSTrond Myklebust 	xprt_force_disconnect(xprt);
1456a519fc7aSTrond Myklebust }
1457a519fc7aSTrond Myklebust 
14589e00abc3STrond Myklebust #if defined(CONFIG_SUNRPC_BACKCHANNEL)
14596b26cc8cSChuck Lever static size_t xs_tcp_bc_maxpayload(struct rpc_xprt *xprt)
14606b26cc8cSChuck Lever {
14616b26cc8cSChuck Lever 	return PAGE_SIZE;
14626b26cc8cSChuck Lever }
14639e00abc3STrond Myklebust #endif /* CONFIG_SUNRPC_BACKCHANNEL */
146444b98efdSRicardo Labiaga 
14659903cd1cSChuck Lever /**
1466efce2d0bSTrond Myklebust  * xs_local_state_change - callback to handle AF_LOCAL socket state changes
1467efce2d0bSTrond Myklebust  * @sk: socket whose state has changed
1468efce2d0bSTrond Myklebust  *
1469efce2d0bSTrond Myklebust  */
1470efce2d0bSTrond Myklebust static void xs_local_state_change(struct sock *sk)
1471efce2d0bSTrond Myklebust {
1472efce2d0bSTrond Myklebust 	struct rpc_xprt *xprt;
1473efce2d0bSTrond Myklebust 	struct sock_xprt *transport;
1474efce2d0bSTrond Myklebust 
1475efce2d0bSTrond Myklebust 	if (!(xprt = xprt_from_sock(sk)))
1476efce2d0bSTrond Myklebust 		return;
1477efce2d0bSTrond Myklebust 	transport = container_of(xprt, struct sock_xprt, xprt);
1478efce2d0bSTrond Myklebust 	if (sk->sk_shutdown & SHUTDOWN_MASK) {
1479efce2d0bSTrond Myklebust 		clear_bit(XPRT_CONNECTED, &xprt->state);
1480efce2d0bSTrond Myklebust 		/* Trigger the socket release */
1481efce2d0bSTrond Myklebust 		xs_run_error_worker(transport, XPRT_SOCK_WAKE_DISCONNECT);
1482efce2d0bSTrond Myklebust 	}
1483efce2d0bSTrond Myklebust }
1484efce2d0bSTrond Myklebust 
1485efce2d0bSTrond Myklebust /**
14869903cd1cSChuck Lever  * xs_tcp_state_change - callback to handle TCP socket state changes
14879903cd1cSChuck Lever  * @sk: socket whose state has changed
14889903cd1cSChuck Lever  *
14899903cd1cSChuck Lever  */
14909903cd1cSChuck Lever static void xs_tcp_state_change(struct sock *sk)
1491a246b010SChuck Lever {
1492a246b010SChuck Lever 	struct rpc_xprt *xprt;
14930fdea1e8STrond Myklebust 	struct sock_xprt *transport;
1494a246b010SChuck Lever 
1495a246b010SChuck Lever 	if (!(xprt = xprt_from_sock(sk)))
1496ea9afca8STrond Myklebust 		return;
14979903cd1cSChuck Lever 	dprintk("RPC:       xs_tcp_state_change client %p...\n", xprt);
1498669502ffSAndy Chittenden 	dprintk("RPC:       state %x conn %d dead %d zapped %d sk_shutdown %d\n",
1499a246b010SChuck Lever 			sk->sk_state, xprt_connected(xprt),
1500a246b010SChuck Lever 			sock_flag(sk, SOCK_DEAD),
1501669502ffSAndy Chittenden 			sock_flag(sk, SOCK_ZAPPED),
1502669502ffSAndy Chittenden 			sk->sk_shutdown);
1503a246b010SChuck Lever 
15040fdea1e8STrond Myklebust 	transport = container_of(xprt, struct sock_xprt, xprt);
150540b5ea0cSTrond Myklebust 	trace_rpc_socket_state_change(xprt, sk->sk_socket);
1506a246b010SChuck Lever 	switch (sk->sk_state) {
1507a246b010SChuck Lever 	case TCP_ESTABLISHED:
1508a246b010SChuck Lever 		if (!xprt_test_and_set_connected(xprt)) {
15098b71798cSTrond Myklebust 			xprt->connect_cookie++;
15100fdea1e8STrond Myklebust 			clear_bit(XPRT_SOCK_CONNECTING, &transport->sock_state);
15110fdea1e8STrond Myklebust 			xprt_clear_connecting(xprt);
151251971139SChuck Lever 
15133968a8a5SChuck Lever 			xprt->stat.connect_count++;
15143968a8a5SChuck Lever 			xprt->stat.connect_time += (long)jiffies -
15153968a8a5SChuck Lever 						   xprt->stat.connect_start;
15164f8943f8STrond Myklebust 			xs_run_error_worker(transport, XPRT_SOCK_WAKE_PENDING);
1517a246b010SChuck Lever 		}
1518a246b010SChuck Lever 		break;
15193b948ae5STrond Myklebust 	case TCP_FIN_WAIT1:
15203b948ae5STrond Myklebust 		/* The client initiated a shutdown of the socket */
15217c1d71cfSTrond Myklebust 		xprt->connect_cookie++;
1522663b8858STrond Myklebust 		xprt->reestablish_timeout = 0;
15233b948ae5STrond Myklebust 		set_bit(XPRT_CLOSING, &xprt->state);
15244e857c58SPeter Zijlstra 		smp_mb__before_atomic();
15253b948ae5STrond Myklebust 		clear_bit(XPRT_CONNECTED, &xprt->state);
1526ef803670STrond Myklebust 		clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
15274e857c58SPeter Zijlstra 		smp_mb__after_atomic();
1528a246b010SChuck Lever 		break;
1529632e3bdcSTrond Myklebust 	case TCP_CLOSE_WAIT:
15303b948ae5STrond Myklebust 		/* The server initiated a shutdown of the socket */
15317c1d71cfSTrond Myklebust 		xprt->connect_cookie++;
1532d0bea455STrond Myklebust 		clear_bit(XPRT_CONNECTED, &xprt->state);
15334f8943f8STrond Myklebust 		xs_run_error_worker(transport, XPRT_SOCK_WAKE_DISCONNECT);
1534df561f66SGustavo A. R. Silva 		fallthrough;
1535663b8858STrond Myklebust 	case TCP_CLOSING:
1536663b8858STrond Myklebust 		/*
1537663b8858STrond Myklebust 		 * If the server closed down the connection, make sure that
1538663b8858STrond Myklebust 		 * we back off before reconnecting
1539663b8858STrond Myklebust 		 */
1540663b8858STrond Myklebust 		if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
1541663b8858STrond Myklebust 			xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
15423b948ae5STrond Myklebust 		break;
15433b948ae5STrond Myklebust 	case TCP_LAST_ACK:
1544670f9457STrond Myklebust 		set_bit(XPRT_CLOSING, &xprt->state);
15454e857c58SPeter Zijlstra 		smp_mb__before_atomic();
15463b948ae5STrond Myklebust 		clear_bit(XPRT_CONNECTED, &xprt->state);
15474e857c58SPeter Zijlstra 		smp_mb__after_atomic();
15483b948ae5STrond Myklebust 		break;
15493b948ae5STrond Myklebust 	case TCP_CLOSE:
15500fdea1e8STrond Myklebust 		if (test_and_clear_bit(XPRT_SOCK_CONNECTING,
15510fdea1e8STrond Myklebust 					&transport->sock_state))
15520fdea1e8STrond Myklebust 			xprt_clear_connecting(xprt);
15539b30889cSTrond Myklebust 		clear_bit(XPRT_CLOSING, &xprt->state);
15549b30889cSTrond Myklebust 		/* Trigger the socket release */
15554f8943f8STrond Myklebust 		xs_run_error_worker(transport, XPRT_SOCK_WAKE_DISCONNECT);
1556a246b010SChuck Lever 	}
1557a246b010SChuck Lever }
1558a246b010SChuck Lever 
15591f0fa154SIlpo Järvinen static void xs_write_space(struct sock *sk)
15601f0fa154SIlpo Järvinen {
15614f8943f8STrond Myklebust 	struct sock_xprt *transport;
15621f0fa154SIlpo Järvinen 	struct rpc_xprt *xprt;
15631f0fa154SIlpo Järvinen 
156413331a55STrond Myklebust 	if (!sk->sk_socket)
15651f0fa154SIlpo Järvinen 		return;
156613331a55STrond Myklebust 	clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
15671f0fa154SIlpo Järvinen 
15681f0fa154SIlpo Järvinen 	if (unlikely(!(xprt = xprt_from_sock(sk))))
15691f0fa154SIlpo Järvinen 		return;
15704f8943f8STrond Myklebust 	transport = container_of(xprt, struct sock_xprt, xprt);
15712790a624STrond Myklebust 	if (!test_and_clear_bit(XPRT_SOCK_NOSPACE, &transport->sock_state))
15722790a624STrond Myklebust 		return;
15734f8943f8STrond Myklebust 	xs_run_error_worker(transport, XPRT_SOCK_WAKE_WRITE);
1574c544577dSTrond Myklebust 	sk->sk_write_pending--;
15751f0fa154SIlpo Järvinen }
15761f0fa154SIlpo Järvinen 
15772a9e1cfaSTrond Myklebust /**
1578c7b2cae8SChuck Lever  * xs_udp_write_space - callback invoked when socket buffer space
1579c7b2cae8SChuck Lever  *                             becomes available
15809903cd1cSChuck Lever  * @sk: socket whose state has changed
15819903cd1cSChuck Lever  *
1582a246b010SChuck Lever  * Called when more output buffer space is available for this socket.
1583a246b010SChuck Lever  * We try not to wake our writers until they can make "significant"
1584c7b2cae8SChuck Lever  * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1585a246b010SChuck Lever  * with a bunch of small requests.
1586a246b010SChuck Lever  */
1587c7b2cae8SChuck Lever static void xs_udp_write_space(struct sock *sk)
1588a246b010SChuck Lever {
1589c7b2cae8SChuck Lever 	/* from net/core/sock.c:sock_def_write_space */
15901f0fa154SIlpo Järvinen 	if (sock_writeable(sk))
15911f0fa154SIlpo Järvinen 		xs_write_space(sk);
1592c7b2cae8SChuck Lever }
1593c7b2cae8SChuck Lever 
1594c7b2cae8SChuck Lever /**
1595c7b2cae8SChuck Lever  * xs_tcp_write_space - callback invoked when socket buffer space
1596c7b2cae8SChuck Lever  *                             becomes available
1597c7b2cae8SChuck Lever  * @sk: socket whose state has changed
1598c7b2cae8SChuck Lever  *
1599c7b2cae8SChuck Lever  * Called when more output buffer space is available for this socket.
1600c7b2cae8SChuck Lever  * We try not to wake our writers until they can make "significant"
1601c7b2cae8SChuck Lever  * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1602c7b2cae8SChuck Lever  * with a bunch of small requests.
1603c7b2cae8SChuck Lever  */
1604c7b2cae8SChuck Lever static void xs_tcp_write_space(struct sock *sk)
1605c7b2cae8SChuck Lever {
1606c7b2cae8SChuck Lever 	/* from net/core/stream.c:sk_stream_write_space */
160764dc6130SEric Dumazet 	if (sk_stream_is_writeable(sk))
16081f0fa154SIlpo Järvinen 		xs_write_space(sk);
1609a246b010SChuck Lever }
1610a246b010SChuck Lever 
1611470056c2SChuck Lever static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt)
1612a246b010SChuck Lever {
1613ee0ac0c2SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1614ee0ac0c2SChuck Lever 	struct sock *sk = transport->inet;
1615a246b010SChuck Lever 
16167c6e066eSChuck Lever 	if (transport->rcvsize) {
1617a246b010SChuck Lever 		sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
16187c6e066eSChuck Lever 		sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2;
1619a246b010SChuck Lever 	}
16207c6e066eSChuck Lever 	if (transport->sndsize) {
1621a246b010SChuck Lever 		sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
16227c6e066eSChuck Lever 		sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2;
1623a246b010SChuck Lever 		sk->sk_write_space(sk);
1624a246b010SChuck Lever 	}
1625a246b010SChuck Lever }
1626a246b010SChuck Lever 
162743118c29SChuck Lever /**
1628470056c2SChuck Lever  * xs_udp_set_buffer_size - set send and receive limits
162943118c29SChuck Lever  * @xprt: generic transport
1630470056c2SChuck Lever  * @sndsize: requested size of send buffer, in bytes
1631470056c2SChuck Lever  * @rcvsize: requested size of receive buffer, in bytes
163243118c29SChuck Lever  *
1633470056c2SChuck Lever  * Set socket send and receive buffer size limits.
163443118c29SChuck Lever  */
1635470056c2SChuck Lever static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize)
163643118c29SChuck Lever {
16377c6e066eSChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
16387c6e066eSChuck Lever 
16397c6e066eSChuck Lever 	transport->sndsize = 0;
1640470056c2SChuck Lever 	if (sndsize)
16417c6e066eSChuck Lever 		transport->sndsize = sndsize + 1024;
16427c6e066eSChuck Lever 	transport->rcvsize = 0;
1643470056c2SChuck Lever 	if (rcvsize)
16447c6e066eSChuck Lever 		transport->rcvsize = rcvsize + 1024;
1645470056c2SChuck Lever 
1646470056c2SChuck Lever 	xs_udp_do_set_buffer_size(xprt);
164743118c29SChuck Lever }
164843118c29SChuck Lever 
164946c0ee8bSChuck Lever /**
165046c0ee8bSChuck Lever  * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
1651acf0a39fSChuck Lever  * @xprt: controlling transport
165246c0ee8bSChuck Lever  * @task: task that timed out
165346c0ee8bSChuck Lever  *
165446c0ee8bSChuck Lever  * Adjust the congestion window after a retransmit timeout has occurred.
165546c0ee8bSChuck Lever  */
16566a24dfb6STrond Myklebust static void xs_udp_timer(struct rpc_xprt *xprt, struct rpc_task *task)
165746c0ee8bSChuck Lever {
1658b5e92419STrond Myklebust 	spin_lock(&xprt->transport_lock);
16596a24dfb6STrond Myklebust 	xprt_adjust_cwnd(xprt, task, -ETIMEDOUT);
1660b5e92419STrond Myklebust 	spin_unlock(&xprt->transport_lock);
166146c0ee8bSChuck Lever }
166246c0ee8bSChuck Lever 
1663826799e6SJ. Bruce Fields static int xs_get_random_port(void)
1664b85d8806SChuck Lever {
1665826799e6SJ. Bruce Fields 	unsigned short min = xprt_min_resvport, max = xprt_max_resvport;
1666826799e6SJ. Bruce Fields 	unsigned short range;
1667826799e6SJ. Bruce Fields 	unsigned short rand;
1668826799e6SJ. Bruce Fields 
1669826799e6SJ. Bruce Fields 	if (max < min)
1670826799e6SJ. Bruce Fields 		return -EADDRINUSE;
1671826799e6SJ. Bruce Fields 	range = max - min + 1;
16728032bf12SJason A. Donenfeld 	rand = get_random_u32_below(range);
1673826799e6SJ. Bruce Fields 	return rand + min;
1674b85d8806SChuck Lever }
1675b85d8806SChuck Lever 
16764dda9c8aSTrond Myklebust static unsigned short xs_sock_getport(struct socket *sock)
16774dda9c8aSTrond Myklebust {
16784dda9c8aSTrond Myklebust 	struct sockaddr_storage buf;
16794dda9c8aSTrond Myklebust 	unsigned short port = 0;
16804dda9c8aSTrond Myklebust 
16819b2c45d4SDenys Vlasenko 	if (kernel_getsockname(sock, (struct sockaddr *)&buf) < 0)
16824dda9c8aSTrond Myklebust 		goto out;
16834dda9c8aSTrond Myklebust 	switch (buf.ss_family) {
16844dda9c8aSTrond Myklebust 	case AF_INET6:
16854dda9c8aSTrond Myklebust 		port = ntohs(((struct sockaddr_in6 *)&buf)->sin6_port);
16864dda9c8aSTrond Myklebust 		break;
16874dda9c8aSTrond Myklebust 	case AF_INET:
16884dda9c8aSTrond Myklebust 		port = ntohs(((struct sockaddr_in *)&buf)->sin_port);
16894dda9c8aSTrond Myklebust 	}
16904dda9c8aSTrond Myklebust out:
16914dda9c8aSTrond Myklebust 	return port;
16924dda9c8aSTrond Myklebust }
16934dda9c8aSTrond Myklebust 
16944dda9c8aSTrond Myklebust /**
169592200412SChuck Lever  * xs_set_port - reset the port number in the remote endpoint address
169692200412SChuck Lever  * @xprt: generic transport
169792200412SChuck Lever  * @port: new port number
169892200412SChuck Lever  *
169992200412SChuck Lever  */
170092200412SChuck Lever static void xs_set_port(struct rpc_xprt *xprt, unsigned short port)
170192200412SChuck Lever {
170292200412SChuck Lever 	dprintk("RPC:       setting port for xprt %p to %u\n", xprt, port);
1703c4efcb1dSChuck Lever 
17049dc3b095SChuck Lever 	rpc_set_port(xs_addr(xprt), port);
17059dc3b095SChuck Lever 	xs_update_peer_port(xprt);
170692200412SChuck Lever }
170792200412SChuck Lever 
17084dda9c8aSTrond Myklebust static void xs_set_srcport(struct sock_xprt *transport, struct socket *sock)
17094dda9c8aSTrond Myklebust {
1710e6237b6fSTrond Myklebust 	if (transport->srcport == 0 && transport->xprt.reuseport)
17114dda9c8aSTrond Myklebust 		transport->srcport = xs_sock_getport(sock);
17124dda9c8aSTrond Myklebust }
17134dda9c8aSTrond Myklebust 
1714826799e6SJ. Bruce Fields static int xs_get_srcport(struct sock_xprt *transport)
171567a391d7STrond Myklebust {
1716826799e6SJ. Bruce Fields 	int port = transport->srcport;
171767a391d7STrond Myklebust 
171867a391d7STrond Myklebust 	if (port == 0 && transport->xprt.resvport)
171967a391d7STrond Myklebust 		port = xs_get_random_port();
172067a391d7STrond Myklebust 	return port;
172167a391d7STrond Myklebust }
172267a391d7STrond Myklebust 
1723421ab1beSTrond Myklebust static unsigned short xs_sock_srcport(struct rpc_xprt *xprt)
1724a8482488SOlga Kornievskaia {
1725a8482488SOlga Kornievskaia 	struct sock_xprt *sock = container_of(xprt, struct sock_xprt, xprt);
1726b49ea673SNeilBrown 	unsigned short ret = 0;
1727b49ea673SNeilBrown 	mutex_lock(&sock->recv_mutex);
1728b49ea673SNeilBrown 	if (sock->sock)
1729b49ea673SNeilBrown 		ret = xs_sock_getport(sock->sock);
1730b49ea673SNeilBrown 	mutex_unlock(&sock->recv_mutex);
1731b49ea673SNeilBrown 	return ret;
1732a8482488SOlga Kornievskaia }
1733421ab1beSTrond Myklebust 
1734421ab1beSTrond Myklebust static int xs_sock_srcaddr(struct rpc_xprt *xprt, char *buf, size_t buflen)
1735421ab1beSTrond Myklebust {
1736421ab1beSTrond Myklebust 	struct sock_xprt *sock = container_of(xprt, struct sock_xprt, xprt);
1737421ab1beSTrond Myklebust 	union {
1738421ab1beSTrond Myklebust 		struct sockaddr sa;
1739421ab1beSTrond Myklebust 		struct sockaddr_storage st;
1740421ab1beSTrond Myklebust 	} saddr;
1741421ab1beSTrond Myklebust 	int ret = -ENOTCONN;
1742421ab1beSTrond Myklebust 
1743421ab1beSTrond Myklebust 	mutex_lock(&sock->recv_mutex);
1744421ab1beSTrond Myklebust 	if (sock->sock) {
1745421ab1beSTrond Myklebust 		ret = kernel_getsockname(sock->sock, &saddr.sa);
1746421ab1beSTrond Myklebust 		if (ret >= 0)
1747421ab1beSTrond Myklebust 			ret = snprintf(buf, buflen, "%pISc", &saddr.sa);
1748421ab1beSTrond Myklebust 	}
1749421ab1beSTrond Myklebust 	mutex_unlock(&sock->recv_mutex);
1750421ab1beSTrond Myklebust 	return ret;
1751421ab1beSTrond Myklebust }
1752a8482488SOlga Kornievskaia 
1753baaf4e48SPavel Emelyanov static unsigned short xs_next_srcport(struct sock_xprt *transport, unsigned short port)
175467a391d7STrond Myklebust {
1755fbfffbd5SChuck Lever 	if (transport->srcport != 0)
1756fbfffbd5SChuck Lever 		transport->srcport = 0;
175767a391d7STrond Myklebust 	if (!transport->xprt.resvport)
175867a391d7STrond Myklebust 		return 0;
175967a391d7STrond Myklebust 	if (port <= xprt_min_resvport || port > xprt_max_resvport)
176067a391d7STrond Myklebust 		return xprt_max_resvport;
176167a391d7STrond Myklebust 	return --port;
176267a391d7STrond Myklebust }
1763beb59b68SPavel Emelyanov static int xs_bind(struct sock_xprt *transport, struct socket *sock)
1764a246b010SChuck Lever {
1765beb59b68SPavel Emelyanov 	struct sockaddr_storage myaddr;
176667a391d7STrond Myklebust 	int err, nloop = 0;
1767826799e6SJ. Bruce Fields 	int port = xs_get_srcport(transport);
176867a391d7STrond Myklebust 	unsigned short last;
1769a246b010SChuck Lever 
17700f7a622cSChris Perl 	/*
17710f7a622cSChris Perl 	 * If we are asking for any ephemeral port (i.e. port == 0 &&
17720f7a622cSChris Perl 	 * transport->xprt.resvport == 0), don't bind.  Let the local
17730f7a622cSChris Perl 	 * port selection happen implicitly when the socket is used
17740f7a622cSChris Perl 	 * (for example at connect time).
17750f7a622cSChris Perl 	 *
17760f7a622cSChris Perl 	 * This ensures that we can continue to establish TCP
17770f7a622cSChris Perl 	 * connections even when all local ephemeral ports are already
17780f7a622cSChris Perl 	 * a part of some TCP connection.  This makes no difference
177912b20ce3SBhaskar Chowdhury 	 * for UDP sockets, but also doesn't harm them.
17800f7a622cSChris Perl 	 *
17810f7a622cSChris Perl 	 * If we're asking for any reserved port (i.e. port == 0 &&
17820f7a622cSChris Perl 	 * transport->xprt.resvport == 1) xs_get_srcport above will
17830f7a622cSChris Perl 	 * ensure that port is non-zero and we will bind as needed.
17840f7a622cSChris Perl 	 */
1785826799e6SJ. Bruce Fields 	if (port <= 0)
1786826799e6SJ. Bruce Fields 		return port;
17870f7a622cSChris Perl 
1788beb59b68SPavel Emelyanov 	memcpy(&myaddr, &transport->srcaddr, transport->xprt.addrlen);
1789a246b010SChuck Lever 	do {
1790beb59b68SPavel Emelyanov 		rpc_set_port((struct sockaddr *)&myaddr, port);
1791e6242e92SSridhar Samudrala 		err = kernel_bind(sock, (struct sockaddr *)&myaddr,
1792beb59b68SPavel Emelyanov 				transport->xprt.addrlen);
1793a246b010SChuck Lever 		if (err == 0) {
1794bc1c56e9SNeilBrown 			if (transport->xprt.reuseport)
1795fbfffbd5SChuck Lever 				transport->srcport = port;
1796d3bc9a1dSFrank van Maarseveen 			break;
1797a246b010SChuck Lever 		}
179867a391d7STrond Myklebust 		last = port;
1799baaf4e48SPavel Emelyanov 		port = xs_next_srcport(transport, port);
180067a391d7STrond Myklebust 		if (port > last)
180167a391d7STrond Myklebust 			nloop++;
180267a391d7STrond Myklebust 	} while (err == -EADDRINUSE && nloop != 2);
1803beb59b68SPavel Emelyanov 
18044232e863SChuck Lever 	if (myaddr.ss_family == AF_INET)
1805beb59b68SPavel Emelyanov 		dprintk("RPC:       %s %pI4:%u: %s (%d)\n", __func__,
1806beb59b68SPavel Emelyanov 				&((struct sockaddr_in *)&myaddr)->sin_addr,
1807beb59b68SPavel Emelyanov 				port, err ? "failed" : "ok", err);
1808beb59b68SPavel Emelyanov 	else
1809beb59b68SPavel Emelyanov 		dprintk("RPC:       %s %pI6:%u: %s (%d)\n", __func__,
1810beb59b68SPavel Emelyanov 				&((struct sockaddr_in6 *)&myaddr)->sin6_addr,
18117dc753f0SChuck Lever 				port, err ? "failed" : "ok", err);
1812a246b010SChuck Lever 	return err;
1813a246b010SChuck Lever }
1814a246b010SChuck Lever 
1815176e21eeSChuck Lever /*
1816176e21eeSChuck Lever  * We don't support autobind on AF_LOCAL sockets
1817176e21eeSChuck Lever  */
1818176e21eeSChuck Lever static void xs_local_rpcbind(struct rpc_task *task)
1819176e21eeSChuck Lever {
1820fb43d172STrond Myklebust 	xprt_set_bound(task->tk_xprt);
1821176e21eeSChuck Lever }
1822176e21eeSChuck Lever 
1823176e21eeSChuck Lever static void xs_local_set_port(struct rpc_xprt *xprt, unsigned short port)
1824176e21eeSChuck Lever {
1825176e21eeSChuck Lever }
1826a246b010SChuck Lever 
1827ed07536eSPeter Zijlstra #ifdef CONFIG_DEBUG_LOCK_ALLOC
1828064a9177SNeilBrown static struct lock_class_key xs_key[3];
1829064a9177SNeilBrown static struct lock_class_key xs_slock_key[3];
1830ed07536eSPeter Zijlstra 
1831176e21eeSChuck Lever static inline void xs_reclassify_socketu(struct socket *sock)
1832176e21eeSChuck Lever {
1833176e21eeSChuck Lever 	struct sock *sk = sock->sk;
1834176e21eeSChuck Lever 
1835176e21eeSChuck Lever 	sock_lock_init_class_and_name(sk, "slock-AF_LOCAL-RPC",
1836064a9177SNeilBrown 		&xs_slock_key[0], "sk_lock-AF_LOCAL-RPC", &xs_key[0]);
1837176e21eeSChuck Lever }
1838176e21eeSChuck Lever 
18398945ee5eSChuck Lever static inline void xs_reclassify_socket4(struct socket *sock)
1840ed07536eSPeter Zijlstra {
1841ed07536eSPeter Zijlstra 	struct sock *sk = sock->sk;
18428945ee5eSChuck Lever 
18438945ee5eSChuck Lever 	sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC",
1844064a9177SNeilBrown 		&xs_slock_key[1], "sk_lock-AF_INET-RPC", &xs_key[1]);
1845ed07536eSPeter Zijlstra }
18468945ee5eSChuck Lever 
18478945ee5eSChuck Lever static inline void xs_reclassify_socket6(struct socket *sock)
18488945ee5eSChuck Lever {
18498945ee5eSChuck Lever 	struct sock *sk = sock->sk;
18508945ee5eSChuck Lever 
18518945ee5eSChuck Lever 	sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC",
1852064a9177SNeilBrown 		&xs_slock_key[2], "sk_lock-AF_INET6-RPC", &xs_key[2]);
1853ed07536eSPeter Zijlstra }
18546bc9638aSPavel Emelyanov 
18556bc9638aSPavel Emelyanov static inline void xs_reclassify_socket(int family, struct socket *sock)
18566bc9638aSPavel Emelyanov {
1857fafc4e1eSHannes Frederic Sowa 	if (WARN_ON_ONCE(!sock_allow_reclassification(sock->sk)))
18581b7a1819SWeston Andros Adamson 		return;
18591b7a1819SWeston Andros Adamson 
18604232e863SChuck Lever 	switch (family) {
1861176e21eeSChuck Lever 	case AF_LOCAL:
1862176e21eeSChuck Lever 		xs_reclassify_socketu(sock);
1863176e21eeSChuck Lever 		break;
18644232e863SChuck Lever 	case AF_INET:
18656bc9638aSPavel Emelyanov 		xs_reclassify_socket4(sock);
18664232e863SChuck Lever 		break;
18674232e863SChuck Lever 	case AF_INET6:
18686bc9638aSPavel Emelyanov 		xs_reclassify_socket6(sock);
18694232e863SChuck Lever 		break;
18704232e863SChuck Lever 	}
18716bc9638aSPavel Emelyanov }
1872ed07536eSPeter Zijlstra #else
18736bc9638aSPavel Emelyanov static inline void xs_reclassify_socket(int family, struct socket *sock)
18746bc9638aSPavel Emelyanov {
18756bc9638aSPavel Emelyanov }
1876ed07536eSPeter Zijlstra #endif
1877ed07536eSPeter Zijlstra 
187893dc41bdSNeilBrown static void xs_dummy_setup_socket(struct work_struct *work)
187993dc41bdSNeilBrown {
188093dc41bdSNeilBrown }
188193dc41bdSNeilBrown 
18826bc9638aSPavel Emelyanov static struct socket *xs_create_sock(struct rpc_xprt *xprt,
18834dda9c8aSTrond Myklebust 		struct sock_xprt *transport, int family, int type,
18844dda9c8aSTrond Myklebust 		int protocol, bool reuseport)
188522f79326SPavel Emelyanov {
1886a73881c9STrond Myklebust 	struct file *filp;
188722f79326SPavel Emelyanov 	struct socket *sock;
188822f79326SPavel Emelyanov 	int err;
188922f79326SPavel Emelyanov 
18906bc9638aSPavel Emelyanov 	err = __sock_create(xprt->xprt_net, family, type, protocol, &sock, 1);
189122f79326SPavel Emelyanov 	if (err < 0) {
189222f79326SPavel Emelyanov 		dprintk("RPC:       can't create %d transport socket (%d).\n",
189322f79326SPavel Emelyanov 				protocol, -err);
189422f79326SPavel Emelyanov 		goto out;
189522f79326SPavel Emelyanov 	}
18966bc9638aSPavel Emelyanov 	xs_reclassify_socket(family, sock);
189722f79326SPavel Emelyanov 
18984dda9c8aSTrond Myklebust 	if (reuseport)
1899fe31a326SChristoph Hellwig 		sock_set_reuseport(sock->sk);
19004dda9c8aSTrond Myklebust 
19014cea288aSBen Hutchings 	err = xs_bind(transport, sock);
19024cea288aSBen Hutchings 	if (err) {
190322f79326SPavel Emelyanov 		sock_release(sock);
190422f79326SPavel Emelyanov 		goto out;
190522f79326SPavel Emelyanov 	}
190622f79326SPavel Emelyanov 
1907a73881c9STrond Myklebust 	filp = sock_alloc_file(sock, O_NONBLOCK, NULL);
1908a73881c9STrond Myklebust 	if (IS_ERR(filp))
1909a73881c9STrond Myklebust 		return ERR_CAST(filp);
1910a73881c9STrond Myklebust 	transport->file = filp;
1911a73881c9STrond Myklebust 
191222f79326SPavel Emelyanov 	return sock;
191322f79326SPavel Emelyanov out:
191422f79326SPavel Emelyanov 	return ERR_PTR(err);
191522f79326SPavel Emelyanov }
191622f79326SPavel Emelyanov 
1917176e21eeSChuck Lever static int xs_local_finish_connecting(struct rpc_xprt *xprt,
1918176e21eeSChuck Lever 				      struct socket *sock)
1919176e21eeSChuck Lever {
1920176e21eeSChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
1921176e21eeSChuck Lever 									xprt);
1922176e21eeSChuck Lever 
1923176e21eeSChuck Lever 	if (!transport->inet) {
1924176e21eeSChuck Lever 		struct sock *sk = sock->sk;
1925176e21eeSChuck Lever 
1926ea9afca8STrond Myklebust 		lock_sock(sk);
1927176e21eeSChuck Lever 
1928176e21eeSChuck Lever 		xs_save_old_callbacks(transport, sk);
1929176e21eeSChuck Lever 
1930176e21eeSChuck Lever 		sk->sk_user_data = xprt;
1931a2648094STrond Myklebust 		sk->sk_data_ready = xs_data_ready;
1932176e21eeSChuck Lever 		sk->sk_write_space = xs_udp_write_space;
1933efce2d0bSTrond Myklebust 		sk->sk_state_change = xs_local_state_change;
19342118071dSTrond Myklebust 		sk->sk_error_report = xs_error_report;
193598123866SBenjamin Coddington 		sk->sk_use_task_frag = false;
1936176e21eeSChuck Lever 
1937176e21eeSChuck Lever 		xprt_clear_connected(xprt);
1938176e21eeSChuck Lever 
1939176e21eeSChuck Lever 		/* Reset to new socket */
1940176e21eeSChuck Lever 		transport->sock = sock;
1941176e21eeSChuck Lever 		transport->inet = sk;
1942176e21eeSChuck Lever 
1943ea9afca8STrond Myklebust 		release_sock(sk);
1944176e21eeSChuck Lever 	}
1945176e21eeSChuck Lever 
1946ae053551STrond Myklebust 	xs_stream_start_connect(transport);
19476c7a64e5STrond Myklebust 
1948176e21eeSChuck Lever 	return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, 0);
1949176e21eeSChuck Lever }
1950176e21eeSChuck Lever 
1951176e21eeSChuck Lever /**
1952176e21eeSChuck Lever  * xs_local_setup_socket - create AF_LOCAL socket, connect to a local endpoint
1953176e21eeSChuck Lever  * @transport: socket transport to connect
1954176e21eeSChuck Lever  */
1955dc107402SJ. Bruce Fields static int xs_local_setup_socket(struct sock_xprt *transport)
1956176e21eeSChuck Lever {
1957176e21eeSChuck Lever 	struct rpc_xprt *xprt = &transport->xprt;
1958a73881c9STrond Myklebust 	struct file *filp;
1959176e21eeSChuck Lever 	struct socket *sock;
196068e9a246SColin Ian King 	int status;
1961176e21eeSChuck Lever 
1962176e21eeSChuck Lever 	status = __sock_create(xprt->xprt_net, AF_LOCAL,
1963176e21eeSChuck Lever 					SOCK_STREAM, 0, &sock, 1);
1964176e21eeSChuck Lever 	if (status < 0) {
1965176e21eeSChuck Lever 		dprintk("RPC:       can't create AF_LOCAL "
1966176e21eeSChuck Lever 			"transport socket (%d).\n", -status);
1967176e21eeSChuck Lever 		goto out;
1968176e21eeSChuck Lever 	}
1969d1358917SStefan Hajnoczi 	xs_reclassify_socket(AF_LOCAL, sock);
1970176e21eeSChuck Lever 
1971a73881c9STrond Myklebust 	filp = sock_alloc_file(sock, O_NONBLOCK, NULL);
1972a73881c9STrond Myklebust 	if (IS_ERR(filp)) {
1973a73881c9STrond Myklebust 		status = PTR_ERR(filp);
1974a73881c9STrond Myklebust 		goto out;
1975a73881c9STrond Myklebust 	}
1976a73881c9STrond Myklebust 	transport->file = filp;
1977a73881c9STrond Myklebust 
1978176e21eeSChuck Lever 	dprintk("RPC:       worker connecting xprt %p via AF_LOCAL to %s\n",
1979176e21eeSChuck Lever 			xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1980176e21eeSChuck Lever 
1981176e21eeSChuck Lever 	status = xs_local_finish_connecting(xprt, sock);
198240b5ea0cSTrond Myklebust 	trace_rpc_socket_connect(xprt, sock, status);
1983176e21eeSChuck Lever 	switch (status) {
1984176e21eeSChuck Lever 	case 0:
1985176e21eeSChuck Lever 		dprintk("RPC:       xprt %p connected to %s\n",
1986176e21eeSChuck Lever 				xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
19873968a8a5SChuck Lever 		xprt->stat.connect_count++;
19883968a8a5SChuck Lever 		xprt->stat.connect_time += (long)jiffies -
19893968a8a5SChuck Lever 					   xprt->stat.connect_start;
1990176e21eeSChuck Lever 		xprt_set_connected(xprt);
199193f479d3SGustavo A. R. Silva 		break;
19923601c4a9STrond Myklebust 	case -ENOBUFS:
1993176e21eeSChuck Lever 		break;
1994176e21eeSChuck Lever 	case -ENOENT:
1995176e21eeSChuck Lever 		dprintk("RPC:       xprt %p: socket %s does not exist\n",
1996176e21eeSChuck Lever 				xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1997176e21eeSChuck Lever 		break;
19984a20a988STrond Myklebust 	case -ECONNREFUSED:
19994a20a988STrond Myklebust 		dprintk("RPC:       xprt %p: connection refused for %s\n",
20004a20a988STrond Myklebust 				xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
20014a20a988STrond Myklebust 		break;
2002176e21eeSChuck Lever 	default:
2003176e21eeSChuck Lever 		printk(KERN_ERR "%s: unhandled error (%d) connecting to %s\n",
2004176e21eeSChuck Lever 				__func__, -status,
2005176e21eeSChuck Lever 				xprt->address_strings[RPC_DISPLAY_ADDR]);
2006176e21eeSChuck Lever 	}
2007176e21eeSChuck Lever 
2008176e21eeSChuck Lever out:
2009176e21eeSChuck Lever 	xprt_clear_connecting(xprt);
2010176e21eeSChuck Lever 	xprt_wake_pending_tasks(xprt, status);
2011dc107402SJ. Bruce Fields 	return status;
2012dc107402SJ. Bruce Fields }
2013dc107402SJ. Bruce Fields 
2014b6669737SLinus Torvalds static void xs_local_connect(struct rpc_xprt *xprt, struct rpc_task *task)
2015dc107402SJ. Bruce Fields {
2016dc107402SJ. Bruce Fields 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2017dc107402SJ. Bruce Fields 	int ret;
2018dc107402SJ. Bruce Fields 
2019aad41a7dSTrond Myklebust 	if (transport->file)
2020aad41a7dSTrond Myklebust 		goto force_disconnect;
2021aad41a7dSTrond Myklebust 
2022dc107402SJ. Bruce Fields 	if (RPC_IS_ASYNC(task)) {
2023dc107402SJ. Bruce Fields 		/*
2024dc107402SJ. Bruce Fields 		 * We want the AF_LOCAL connect to be resolved in the
2025dc107402SJ. Bruce Fields 		 * filesystem namespace of the process making the rpc
2026dc107402SJ. Bruce Fields 		 * call.  Thus we connect synchronously.
2027dc107402SJ. Bruce Fields 		 *
2028dc107402SJ. Bruce Fields 		 * If we want to support asynchronous AF_LOCAL calls,
2029dc107402SJ. Bruce Fields 		 * we'll need to figure out how to pass a namespace to
2030dc107402SJ. Bruce Fields 		 * connect.
2031dc107402SJ. Bruce Fields 		 */
203239494194STrond Myklebust 		rpc_task_set_rpc_status(task, -ENOTCONN);
2033aad41a7dSTrond Myklebust 		goto out_wake;
2034dc107402SJ. Bruce Fields 	}
2035dc107402SJ. Bruce Fields 	ret = xs_local_setup_socket(transport);
2036dc107402SJ. Bruce Fields 	if (ret && !RPC_IS_SOFTCONN(task))
2037dc107402SJ. Bruce Fields 		msleep_interruptible(15000);
2038aad41a7dSTrond Myklebust 	return;
2039aad41a7dSTrond Myklebust force_disconnect:
2040aad41a7dSTrond Myklebust 	xprt_force_disconnect(xprt);
2041aad41a7dSTrond Myklebust out_wake:
2042aad41a7dSTrond Myklebust 	xprt_clear_connecting(xprt);
2043aad41a7dSTrond Myklebust 	xprt_wake_pending_tasks(xprt, -ENOTCONN);
2044176e21eeSChuck Lever }
2045176e21eeSChuck Lever 
20463c87ef6eSJeff Layton #if IS_ENABLED(CONFIG_SUNRPC_SWAP)
2047d6e971d8SJeff Layton /*
2048693486d5SNeilBrown  * Note that this should be called with XPRT_LOCKED held, or recv_mutex
2049693486d5SNeilBrown  * held, or when we otherwise know that we have exclusive access to the
2050693486d5SNeilBrown  * socket, to guard against races with xs_reset_transport.
2051d6e971d8SJeff Layton  */
2052a564b8f0SMel Gorman static void xs_set_memalloc(struct rpc_xprt *xprt)
2053a564b8f0SMel Gorman {
2054a564b8f0SMel Gorman 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
2055a564b8f0SMel Gorman 			xprt);
2056a564b8f0SMel Gorman 
2057d6e971d8SJeff Layton 	/*
2058d6e971d8SJeff Layton 	 * If there's no sock, then we have nothing to set. The
2059d6e971d8SJeff Layton 	 * reconnecting process will get it for us.
2060d6e971d8SJeff Layton 	 */
2061d6e971d8SJeff Layton 	if (!transport->inet)
2062d6e971d8SJeff Layton 		return;
20638e228133SJeff Layton 	if (atomic_read(&xprt->swapper))
2064a564b8f0SMel Gorman 		sk_set_memalloc(transport->inet);
2065a564b8f0SMel Gorman }
2066a564b8f0SMel Gorman 
2067a564b8f0SMel Gorman /**
2068d67fa4d8SJeff Layton  * xs_enable_swap - Tag this transport as being used for swap.
2069a564b8f0SMel Gorman  * @xprt: transport to tag
2070a564b8f0SMel Gorman  *
20718e228133SJeff Layton  * Take a reference to this transport on behalf of the rpc_clnt, and
20728e228133SJeff Layton  * optionally mark it for swapping if it wasn't already.
2073a564b8f0SMel Gorman  */
2074d67fa4d8SJeff Layton static int
2075d67fa4d8SJeff Layton xs_enable_swap(struct rpc_xprt *xprt)
2076a564b8f0SMel Gorman {
2077d6e971d8SJeff Layton 	struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt);
2078a564b8f0SMel Gorman 
2079693486d5SNeilBrown 	mutex_lock(&xs->recv_mutex);
2080693486d5SNeilBrown 	if (atomic_inc_return(&xprt->swapper) == 1 &&
2081693486d5SNeilBrown 	    xs->inet)
2082d6e971d8SJeff Layton 		sk_set_memalloc(xs->inet);
2083693486d5SNeilBrown 	mutex_unlock(&xs->recv_mutex);
20848e228133SJeff Layton 	return 0;
2085a564b8f0SMel Gorman }
2086a564b8f0SMel Gorman 
20878e228133SJeff Layton /**
2088d67fa4d8SJeff Layton  * xs_disable_swap - Untag this transport as being used for swap.
20898e228133SJeff Layton  * @xprt: transport to tag
20908e228133SJeff Layton  *
20918e228133SJeff Layton  * Drop a "swapper" reference to this xprt on behalf of the rpc_clnt. If the
20928e228133SJeff Layton  * swapper refcount goes to 0, untag the socket as a memalloc socket.
20938e228133SJeff Layton  */
2094d67fa4d8SJeff Layton static void
2095d67fa4d8SJeff Layton xs_disable_swap(struct rpc_xprt *xprt)
20968e228133SJeff Layton {
2097d6e971d8SJeff Layton 	struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt);
20988e228133SJeff Layton 
2099693486d5SNeilBrown 	mutex_lock(&xs->recv_mutex);
2100693486d5SNeilBrown 	if (atomic_dec_and_test(&xprt->swapper) &&
2101693486d5SNeilBrown 	    xs->inet)
2102d6e971d8SJeff Layton 		sk_clear_memalloc(xs->inet);
2103693486d5SNeilBrown 	mutex_unlock(&xs->recv_mutex);
2104a564b8f0SMel Gorman }
2105a564b8f0SMel Gorman #else
2106a564b8f0SMel Gorman static void xs_set_memalloc(struct rpc_xprt *xprt)
2107a564b8f0SMel Gorman {
2108a564b8f0SMel Gorman }
2109d67fa4d8SJeff Layton 
2110d67fa4d8SJeff Layton static int
2111d67fa4d8SJeff Layton xs_enable_swap(struct rpc_xprt *xprt)
2112d67fa4d8SJeff Layton {
2113d67fa4d8SJeff Layton 	return -EINVAL;
2114d67fa4d8SJeff Layton }
2115d67fa4d8SJeff Layton 
2116d67fa4d8SJeff Layton static void
2117d67fa4d8SJeff Layton xs_disable_swap(struct rpc_xprt *xprt)
2118d67fa4d8SJeff Layton {
2119d67fa4d8SJeff Layton }
2120a564b8f0SMel Gorman #endif
2121a564b8f0SMel Gorman 
212216be2d20SChuck Lever static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
2123a246b010SChuck Lever {
212416be2d20SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2125edb267a6SChuck Lever 
2126ee0ac0c2SChuck Lever 	if (!transport->inet) {
2127b0d93ad5SChuck Lever 		struct sock *sk = sock->sk;
2128b0d93ad5SChuck Lever 
2129ea9afca8STrond Myklebust 		lock_sock(sk);
2130b0d93ad5SChuck Lever 
21312a9e1cfaSTrond Myklebust 		xs_save_old_callbacks(transport, sk);
21322a9e1cfaSTrond Myklebust 
2133b0d93ad5SChuck Lever 		sk->sk_user_data = xprt;
2134f9b2ee71STrond Myklebust 		sk->sk_data_ready = xs_data_ready;
2135b0d93ad5SChuck Lever 		sk->sk_write_space = xs_udp_write_space;
213698123866SBenjamin Coddington 		sk->sk_use_task_frag = false;
2137b0d93ad5SChuck Lever 
2138b0d93ad5SChuck Lever 		xprt_set_connected(xprt);
2139b0d93ad5SChuck Lever 
2140b0d93ad5SChuck Lever 		/* Reset to new socket */
2141ee0ac0c2SChuck Lever 		transport->sock = sock;
2142ee0ac0c2SChuck Lever 		transport->inet = sk;
2143b0d93ad5SChuck Lever 
2144a564b8f0SMel Gorman 		xs_set_memalloc(xprt);
2145a564b8f0SMel Gorman 
2146ea9afca8STrond Myklebust 		release_sock(sk);
2147b0d93ad5SChuck Lever 	}
2148470056c2SChuck Lever 	xs_udp_do_set_buffer_size(xprt);
214902910177STrond Myklebust 
215002910177STrond Myklebust 	xprt->stat.connect_start = jiffies;
215116be2d20SChuck Lever }
215216be2d20SChuck Lever 
21538c14ff2aSPavel Emelyanov static void xs_udp_setup_socket(struct work_struct *work)
2154a246b010SChuck Lever {
2155a246b010SChuck Lever 	struct sock_xprt *transport =
2156a246b010SChuck Lever 		container_of(work, struct sock_xprt, connect_worker.work);
2157a246b010SChuck Lever 	struct rpc_xprt *xprt = &transport->xprt;
2158d099b8afSColin Ian King 	struct socket *sock;
2159b65c0310SPavel Emelyanov 	int status = -EIO;
21608db55a03SNeilBrown 	unsigned int pflags = current->flags;
2161a246b010SChuck Lever 
21628db55a03SNeilBrown 	if (atomic_read(&xprt->swapper))
21638db55a03SNeilBrown 		current->flags |= PF_MEMALLOC;
21648c14ff2aSPavel Emelyanov 	sock = xs_create_sock(xprt, transport,
21654dda9c8aSTrond Myklebust 			xs_addr(xprt)->sa_family, SOCK_DGRAM,
21664dda9c8aSTrond Myklebust 			IPPROTO_UDP, false);
2167b65c0310SPavel Emelyanov 	if (IS_ERR(sock))
2168a246b010SChuck Lever 		goto out;
216968e220bdSChuck Lever 
2170c740eff8SChuck Lever 	dprintk("RPC:       worker connecting xprt %p via %s to "
2171c740eff8SChuck Lever 				"%s (port %s)\n", xprt,
2172c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_PROTO],
2173c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_ADDR],
2174c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_PORT]);
217568e220bdSChuck Lever 
217668e220bdSChuck Lever 	xs_udp_finish_connecting(xprt, sock);
217740b5ea0cSTrond Myklebust 	trace_rpc_socket_connect(xprt, sock, 0);
2178a246b010SChuck Lever 	status = 0;
2179b0d93ad5SChuck Lever out:
2180b0d93ad5SChuck Lever 	xprt_clear_connecting(xprt);
2181cf76785dSTrond Myklebust 	xprt_unlock_connect(xprt, transport);
21827d1e8255STrond Myklebust 	xprt_wake_pending_tasks(xprt, status);
21838db55a03SNeilBrown 	current_restore_flags(pflags, PF_MEMALLOC);
2184b0d93ad5SChuck Lever }
2185b0d93ad5SChuck Lever 
21864876cc77STrond Myklebust /**
21874876cc77STrond Myklebust  * xs_tcp_shutdown - gracefully shut down a TCP socket
21884876cc77STrond Myklebust  * @xprt: transport
21894876cc77STrond Myklebust  *
21904876cc77STrond Myklebust  * Initiates a graceful shutdown of the TCP socket by calling the
21914876cc77STrond Myklebust  * equivalent of shutdown(SHUT_RDWR);
21924876cc77STrond Myklebust  */
21934876cc77STrond Myklebust static void xs_tcp_shutdown(struct rpc_xprt *xprt)
21944876cc77STrond Myklebust {
21954876cc77STrond Myklebust 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
21964876cc77STrond Myklebust 	struct socket *sock = transport->sock;
21979b30889cSTrond Myklebust 	int skst = transport->inet ? transport->inet->sk_state : TCP_CLOSE;
21984876cc77STrond Myklebust 
21994876cc77STrond Myklebust 	if (sock == NULL)
22004876cc77STrond Myklebust 		return;
22010a6ff58eSTrond Myklebust 	if (!xprt->reuseport) {
22020a6ff58eSTrond Myklebust 		xs_close(xprt);
22030a6ff58eSTrond Myklebust 		return;
22040a6ff58eSTrond Myklebust 	}
22059b30889cSTrond Myklebust 	switch (skst) {
22067c81e6a9STrond Myklebust 	case TCP_FIN_WAIT1:
22077c81e6a9STrond Myklebust 	case TCP_FIN_WAIT2:
2208943d045aSSiddharth Kawar 	case TCP_LAST_ACK:
22097c81e6a9STrond Myklebust 		break;
22107c81e6a9STrond Myklebust 	case TCP_ESTABLISHED:
22117c81e6a9STrond Myklebust 	case TCP_CLOSE_WAIT:
22124876cc77STrond Myklebust 		kernel_sock_shutdown(sock, SHUT_RDWR);
22134876cc77STrond Myklebust 		trace_rpc_socket_shutdown(xprt, sock);
22149b30889cSTrond Myklebust 		break;
22157c81e6a9STrond Myklebust 	default:
22164876cc77STrond Myklebust 		xs_reset_transport(transport);
22174876cc77STrond Myklebust 	}
22189b30889cSTrond Myklebust }
22194876cc77STrond Myklebust 
22208d1b8c62STrond Myklebust static void xs_tcp_set_socket_timeouts(struct rpc_xprt *xprt,
22218d1b8c62STrond Myklebust 		struct socket *sock)
2222b0d93ad5SChuck Lever {
222316be2d20SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
22247196dbb0STrond Myklebust 	unsigned int keepidle;
22257196dbb0STrond Myklebust 	unsigned int keepcnt;
2226775f06abSTrond Myklebust 	unsigned int timeo;
22277f260e85STrond Myklebust 
2228b5e92419STrond Myklebust 	spin_lock(&xprt->transport_lock);
22297196dbb0STrond Myklebust 	keepidle = DIV_ROUND_UP(xprt->timeout->to_initval, HZ);
22307196dbb0STrond Myklebust 	keepcnt = xprt->timeout->to_retries + 1;
22317196dbb0STrond Myklebust 	timeo = jiffies_to_msecs(xprt->timeout->to_initval) *
22327196dbb0STrond Myklebust 		(xprt->timeout->to_retries + 1);
22337196dbb0STrond Myklebust 	clear_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state);
2234b5e92419STrond Myklebust 	spin_unlock(&xprt->transport_lock);
22357f260e85STrond Myklebust 
22367f260e85STrond Myklebust 	/* TCP Keepalive options */
2237ce3d9544SChristoph Hellwig 	sock_set_keepalive(sock->sk);
223871c48eb8SChristoph Hellwig 	tcp_sock_set_keepidle(sock->sk, keepidle);
2239d41ecaacSChristoph Hellwig 	tcp_sock_set_keepintvl(sock->sk, keepidle);
2240480aeb96SChristoph Hellwig 	tcp_sock_set_keepcnt(sock->sk, keepcnt);
2241b0d93ad5SChuck Lever 
22428d1b8c62STrond Myklebust 	/* TCP user timeout (see RFC5482) */
2243c488aeadSChristoph Hellwig 	tcp_sock_set_user_timeout(sock->sk, timeo);
22448d1b8c62STrond Myklebust }
22458d1b8c62STrond Myklebust 
22467196dbb0STrond Myklebust static void xs_tcp_set_connect_timeout(struct rpc_xprt *xprt,
22477196dbb0STrond Myklebust 		unsigned long connect_timeout,
22487196dbb0STrond Myklebust 		unsigned long reconnect_timeout)
22497196dbb0STrond Myklebust {
22507196dbb0STrond Myklebust 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
22517196dbb0STrond Myklebust 	struct rpc_timeout to;
22527196dbb0STrond Myklebust 	unsigned long initval;
22537196dbb0STrond Myklebust 
2254b5e92419STrond Myklebust 	spin_lock(&xprt->transport_lock);
22557196dbb0STrond Myklebust 	if (reconnect_timeout < xprt->max_reconnect_timeout)
22567196dbb0STrond Myklebust 		xprt->max_reconnect_timeout = reconnect_timeout;
22577196dbb0STrond Myklebust 	if (connect_timeout < xprt->connect_timeout) {
22587196dbb0STrond Myklebust 		memcpy(&to, xprt->timeout, sizeof(to));
22597196dbb0STrond Myklebust 		initval = DIV_ROUND_UP(connect_timeout, to.to_retries + 1);
22607196dbb0STrond Myklebust 		/* Arbitrary lower limit */
22617196dbb0STrond Myklebust 		if (initval <  XS_TCP_INIT_REEST_TO << 1)
22627196dbb0STrond Myklebust 			initval = XS_TCP_INIT_REEST_TO << 1;
22637196dbb0STrond Myklebust 		to.to_initval = initval;
22647196dbb0STrond Myklebust 		to.to_maxval = initval;
22657196dbb0STrond Myklebust 		memcpy(&transport->tcp_timeout, &to,
22667196dbb0STrond Myklebust 				sizeof(transport->tcp_timeout));
22677196dbb0STrond Myklebust 		xprt->timeout = &transport->tcp_timeout;
22687196dbb0STrond Myklebust 		xprt->connect_timeout = connect_timeout;
22697196dbb0STrond Myklebust 	}
22707196dbb0STrond Myklebust 	set_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state);
2271b5e92419STrond Myklebust 	spin_unlock(&xprt->transport_lock);
22727196dbb0STrond Myklebust }
22737196dbb0STrond Myklebust 
22748d1b8c62STrond Myklebust static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
22758d1b8c62STrond Myklebust {
22768d1b8c62STrond Myklebust 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
22778d1b8c62STrond Myklebust 
22788d1b8c62STrond Myklebust 	if (!transport->inet) {
22798d1b8c62STrond Myklebust 		struct sock *sk = sock->sk;
22808d1b8c62STrond Myklebust 
2281d88e4d82SNeilBrown 		/* Avoid temporary address, they are bad for long-lived
2282d88e4d82SNeilBrown 		 * connections such as NFS mounts.
2283d88e4d82SNeilBrown 		 * RFC4941, section 3.6 suggests that:
2284d88e4d82SNeilBrown 		 *    Individual applications, which have specific
2285d88e4d82SNeilBrown 		 *    knowledge about the normal duration of connections,
2286d88e4d82SNeilBrown 		 *    MAY override this as appropriate.
2287d88e4d82SNeilBrown 		 */
228818d5ad62SChristoph Hellwig 		if (xs_addr(xprt)->sa_family == PF_INET6) {
228918d5ad62SChristoph Hellwig 			ip6_sock_set_addr_preferences(sk,
229018d5ad62SChristoph Hellwig 				IPV6_PREFER_SRC_PUBLIC);
229118d5ad62SChristoph Hellwig 		}
2292d88e4d82SNeilBrown 
22938d1b8c62STrond Myklebust 		xs_tcp_set_socket_timeouts(xprt, sock);
2294d737e5d4STrond Myklebust 		tcp_sock_set_nodelay(sk);
2295775f06abSTrond Myklebust 
2296ea9afca8STrond Myklebust 		lock_sock(sk);
2297b0d93ad5SChuck Lever 
22982a9e1cfaSTrond Myklebust 		xs_save_old_callbacks(transport, sk);
22992a9e1cfaSTrond Myklebust 
2300b0d93ad5SChuck Lever 		sk->sk_user_data = xprt;
23015157b956STrond Myklebust 		sk->sk_data_ready = xs_data_ready;
2302b0d93ad5SChuck Lever 		sk->sk_state_change = xs_tcp_state_change;
2303b0d93ad5SChuck Lever 		sk->sk_write_space = xs_tcp_write_space;
23042118071dSTrond Myklebust 		sk->sk_error_report = xs_error_report;
230598123866SBenjamin Coddington 		sk->sk_use_task_frag = false;
23063167e12cSChuck Lever 
23073167e12cSChuck Lever 		/* socket options */
23083167e12cSChuck Lever 		sock_reset_flag(sk, SOCK_LINGER);
2309b0d93ad5SChuck Lever 
2310b0d93ad5SChuck Lever 		xprt_clear_connected(xprt);
2311b0d93ad5SChuck Lever 
2312b0d93ad5SChuck Lever 		/* Reset to new socket */
2313ee0ac0c2SChuck Lever 		transport->sock = sock;
2314ee0ac0c2SChuck Lever 		transport->inet = sk;
2315b0d93ad5SChuck Lever 
2316ea9afca8STrond Myklebust 		release_sock(sk);
2317b0d93ad5SChuck Lever 	}
2318b0d93ad5SChuck Lever 
231901d37c42STrond Myklebust 	if (!xprt_bound(xprt))
2320280254b6STrond Myklebust 		return -ENOTCONN;
232101d37c42STrond Myklebust 
2322a564b8f0SMel Gorman 	xs_set_memalloc(xprt);
2323a564b8f0SMel Gorman 
2324ae053551STrond Myklebust 	xs_stream_start_connect(transport);
2325e1806c7bSTrond Myklebust 
2326b0d93ad5SChuck Lever 	/* Tell the socket layer to start connecting... */
23270fdea1e8STrond Myklebust 	set_bit(XPRT_SOCK_CONNECTING, &transport->sock_state);
2328280254b6STrond Myklebust 	return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK);
232916be2d20SChuck Lever }
233016be2d20SChuck Lever 
233116be2d20SChuck Lever /**
2332b61d59ffSTrond Myklebust  * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint
2333acf0a39fSChuck Lever  * @work: queued work item
233416be2d20SChuck Lever  *
233516be2d20SChuck Lever  * Invoked by a work queue tasklet.
233616be2d20SChuck Lever  */
2337cdd518d5SPavel Emelyanov static void xs_tcp_setup_socket(struct work_struct *work)
233816be2d20SChuck Lever {
2339cdd518d5SPavel Emelyanov 	struct sock_xprt *transport =
2340cdd518d5SPavel Emelyanov 		container_of(work, struct sock_xprt, connect_worker.work);
234116be2d20SChuck Lever 	struct socket *sock = transport->sock;
2342a9f5f0f7SPavel Emelyanov 	struct rpc_xprt *xprt = &transport->xprt;
2343280254b6STrond Myklebust 	int status;
23448db55a03SNeilBrown 	unsigned int pflags = current->flags;
234516be2d20SChuck Lever 
23468db55a03SNeilBrown 	if (atomic_read(&xprt->swapper))
23478db55a03SNeilBrown 		current->flags |= PF_MEMALLOC;
234889f42494STrond Myklebust 
234989f42494STrond Myklebust 	if (xprt_connected(xprt))
235089f42494STrond Myklebust 		goto out;
235189f42494STrond Myklebust 	if (test_and_clear_bit(XPRT_SOCK_CONNECT_SENT,
235289f42494STrond Myklebust 			       &transport->sock_state) ||
235389f42494STrond Myklebust 	    !sock) {
235489f42494STrond Myklebust 		xs_reset_transport(transport);
235589f42494STrond Myklebust 		sock = xs_create_sock(xprt, transport, xs_addr(xprt)->sa_family,
235689f42494STrond Myklebust 				      SOCK_STREAM, IPPROTO_TCP, true);
2357b61d59ffSTrond Myklebust 		if (IS_ERR(sock)) {
2358280254b6STrond Myklebust 			xprt_wake_pending_tasks(xprt, PTR_ERR(sock));
235916be2d20SChuck Lever 			goto out;
236016be2d20SChuck Lever 		}
23617d1e8255STrond Myklebust 	}
23627d1e8255STrond Myklebust 
2363c740eff8SChuck Lever 	dprintk("RPC:       worker connecting xprt %p via %s to "
2364c740eff8SChuck Lever 				"%s (port %s)\n", xprt,
2365c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_PROTO],
2366c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_ADDR],
2367c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_PORT]);
236816be2d20SChuck Lever 
236916be2d20SChuck Lever 	status = xs_tcp_finish_connecting(xprt, sock);
237040b5ea0cSTrond Myklebust 	trace_rpc_socket_connect(xprt, sock, status);
2371a246b010SChuck Lever 	dprintk("RPC:       %p connect status %d connected %d sock state %d\n",
237246121cf7SChuck Lever 			xprt, -status, xprt_connected(xprt),
237346121cf7SChuck Lever 			sock->sk->sk_state);
2374a246b010SChuck Lever 	switch (status) {
23752a491991STrond Myklebust 	case 0:
2376a246b010SChuck Lever 	case -EINPROGRESS:
2377280254b6STrond Myklebust 		/* SYN_SENT! */
237889f42494STrond Myklebust 		set_bit(XPRT_SOCK_CONNECT_SENT, &transport->sock_state);
2379280254b6STrond Myklebust 		if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
2380280254b6STrond Myklebust 			xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2381280254b6STrond Myklebust 		fallthrough;
2382a246b010SChuck Lever 	case -EALREADY:
2383280254b6STrond Myklebust 		goto out_unlock;
2384280254b6STrond Myklebust 	case -EADDRNOTAVAIL:
2385280254b6STrond Myklebust 		/* Source port number is unavailable. Try a new one! */
2386280254b6STrond Myklebust 		transport->srcport = 0;
2387280254b6STrond Myklebust 		status = -EAGAIN;
2388280254b6STrond Myklebust 		break;
23899fcfe0c8STrond Myklebust 	case -EINVAL:
23909fcfe0c8STrond Myklebust 		/* Happens, for instance, if the user specified a link
23919fcfe0c8STrond Myklebust 		 * local IPv6 address without a scope-id.
23929fcfe0c8STrond Myklebust 		 */
23933ed5e2a2STrond Myklebust 	case -ECONNREFUSED:
23943ed5e2a2STrond Myklebust 	case -ECONNRESET:
2395eb5b46faSTrond Myklebust 	case -ENETDOWN:
23963ed5e2a2STrond Myklebust 	case -ENETUNREACH:
23974ba161a7STrond Myklebust 	case -EHOSTUNREACH:
23983913c78cSTrond Myklebust 	case -EADDRINUSE:
23993601c4a9STrond Myklebust 	case -ENOBUFS:
2400280254b6STrond Myklebust 		break;
2401280254b6STrond Myklebust 	default:
2402280254b6STrond Myklebust 		printk("%s: connect returned unhandled error %d\n",
2403280254b6STrond Myklebust 			__func__, status);
2404280254b6STrond Myklebust 		status = -EAGAIN;
2405280254b6STrond Myklebust 	}
2406280254b6STrond Myklebust 
24078c71139dSCalum Mackay 	/* xs_tcp_force_close() wakes tasks with a fixed error code.
24088c71139dSCalum Mackay 	 * We need to wake them first to ensure the correct error code.
24096ea44adcSNeilBrown 	 */
24106ea44adcSNeilBrown 	xprt_wake_pending_tasks(xprt, status);
24114efdd92cSTrond Myklebust 	xs_tcp_force_close(xprt);
2412a246b010SChuck Lever out:
24132226feb6SChuck Lever 	xprt_clear_connecting(xprt);
2414280254b6STrond Myklebust out_unlock:
2415cf76785dSTrond Myklebust 	xprt_unlock_connect(xprt, transport);
24168db55a03SNeilBrown 	current_restore_flags(pflags, PF_MEMALLOC);
2417a246b010SChuck Lever }
2418a246b010SChuck Lever 
241968e220bdSChuck Lever /**
24209903cd1cSChuck Lever  * xs_connect - connect a socket to a remote endpoint
24211b092092STrond Myklebust  * @xprt: pointer to transport structure
24229903cd1cSChuck Lever  * @task: address of RPC task that manages state of connect request
24239903cd1cSChuck Lever  *
24249903cd1cSChuck Lever  * TCP: If the remote end dropped the connection, delay reconnecting.
242503bf4b70SChuck Lever  *
242603bf4b70SChuck Lever  * UDP socket connects are synchronous, but we use a work queue anyway
242703bf4b70SChuck Lever  * to guarantee that even unprivileged user processes can set up a
242803bf4b70SChuck Lever  * socket on a privileged port.
242903bf4b70SChuck Lever  *
243003bf4b70SChuck Lever  * If a UDP socket connect fails, the delay behavior here prevents
243103bf4b70SChuck Lever  * retry floods (hard mounts).
24329903cd1cSChuck Lever  */
24331b092092STrond Myklebust static void xs_connect(struct rpc_xprt *xprt, struct rpc_task *task)
2434a246b010SChuck Lever {
2435ee0ac0c2SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
243602910177STrond Myklebust 	unsigned long delay = 0;
2437a246b010SChuck Lever 
2438718ba5b8STrond Myklebust 	WARN_ON_ONCE(!xprt_lock_connect(xprt, task, transport));
2439718ba5b8STrond Myklebust 
244089f42494STrond Myklebust 	if (transport->sock != NULL) {
244146121cf7SChuck Lever 		dprintk("RPC:       xs_connect delayed xprt %p for %lu "
244289f42494STrond Myklebust 			"seconds\n", xprt, xprt->reestablish_timeout / HZ);
244399b1a4c3STrond Myklebust 
2444675dd90aSChuck Lever 		delay = xprt_reconnect_delay(xprt);
2445675dd90aSChuck Lever 		xprt_reconnect_backoff(xprt, XS_TCP_INIT_REEST_TO);
244602910177STrond Myklebust 
244702910177STrond Myklebust 	} else
24489903cd1cSChuck Lever 		dprintk("RPC:       xs_connect scheduled xprt %p\n", xprt);
244902910177STrond Myklebust 
245040a5f1b1STrond Myklebust 	queue_delayed_work(xprtiod_workqueue,
245102910177STrond Myklebust 			&transport->connect_worker,
245202910177STrond Myklebust 			delay);
2453a246b010SChuck Lever }
2454a246b010SChuck Lever 
24554f8943f8STrond Myklebust static void xs_wake_disconnect(struct sock_xprt *transport)
24564f8943f8STrond Myklebust {
24574f8943f8STrond Myklebust 	if (test_and_clear_bit(XPRT_SOCK_WAKE_DISCONNECT, &transport->sock_state))
24584f8943f8STrond Myklebust 		xs_tcp_force_close(&transport->xprt);
24594f8943f8STrond Myklebust }
24604f8943f8STrond Myklebust 
24614f8943f8STrond Myklebust static void xs_wake_write(struct sock_xprt *transport)
24624f8943f8STrond Myklebust {
24634f8943f8STrond Myklebust 	if (test_and_clear_bit(XPRT_SOCK_WAKE_WRITE, &transport->sock_state))
24644f8943f8STrond Myklebust 		xprt_write_space(&transport->xprt);
24654f8943f8STrond Myklebust }
24664f8943f8STrond Myklebust 
24674f8943f8STrond Myklebust static void xs_wake_error(struct sock_xprt *transport)
24684f8943f8STrond Myklebust {
24694f8943f8STrond Myklebust 	int sockerr;
24704f8943f8STrond Myklebust 
24714f8943f8STrond Myklebust 	if (!test_bit(XPRT_SOCK_WAKE_ERROR, &transport->sock_state))
24724f8943f8STrond Myklebust 		return;
24734f8943f8STrond Myklebust 	mutex_lock(&transport->recv_mutex);
24744f8943f8STrond Myklebust 	if (transport->sock == NULL)
24754f8943f8STrond Myklebust 		goto out;
24764f8943f8STrond Myklebust 	if (!test_and_clear_bit(XPRT_SOCK_WAKE_ERROR, &transport->sock_state))
24774f8943f8STrond Myklebust 		goto out;
2478af84537dSBenjamin Coddington 	sockerr = xchg(&transport->xprt_err, 0);
24794f8943f8STrond Myklebust 	if (sockerr < 0)
24804f8943f8STrond Myklebust 		xprt_wake_pending_tasks(&transport->xprt, sockerr);
24814f8943f8STrond Myklebust out:
24824f8943f8STrond Myklebust 	mutex_unlock(&transport->recv_mutex);
24834f8943f8STrond Myklebust }
24844f8943f8STrond Myklebust 
24854f8943f8STrond Myklebust static void xs_wake_pending(struct sock_xprt *transport)
24864f8943f8STrond Myklebust {
24874f8943f8STrond Myklebust 	if (test_and_clear_bit(XPRT_SOCK_WAKE_PENDING, &transport->sock_state))
24884f8943f8STrond Myklebust 		xprt_wake_pending_tasks(&transport->xprt, -EAGAIN);
24894f8943f8STrond Myklebust }
24904f8943f8STrond Myklebust 
24914f8943f8STrond Myklebust static void xs_error_handle(struct work_struct *work)
24924f8943f8STrond Myklebust {
24934f8943f8STrond Myklebust 	struct sock_xprt *transport = container_of(work,
24944f8943f8STrond Myklebust 			struct sock_xprt, error_worker);
24954f8943f8STrond Myklebust 
24964f8943f8STrond Myklebust 	xs_wake_disconnect(transport);
24974f8943f8STrond Myklebust 	xs_wake_write(transport);
24984f8943f8STrond Myklebust 	xs_wake_error(transport);
24994f8943f8STrond Myklebust 	xs_wake_pending(transport);
25004f8943f8STrond Myklebust }
25014f8943f8STrond Myklebust 
2502262ca07dSChuck Lever /**
250312b20ce3SBhaskar Chowdhury  * xs_local_print_stats - display AF_LOCAL socket-specific stats
2504176e21eeSChuck Lever  * @xprt: rpc_xprt struct containing statistics
2505176e21eeSChuck Lever  * @seq: output file
2506176e21eeSChuck Lever  *
2507176e21eeSChuck Lever  */
2508176e21eeSChuck Lever static void xs_local_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2509176e21eeSChuck Lever {
2510176e21eeSChuck Lever 	long idle_time = 0;
2511176e21eeSChuck Lever 
2512176e21eeSChuck Lever 	if (xprt_connected(xprt))
2513176e21eeSChuck Lever 		idle_time = (long)(jiffies - xprt->last_used) / HZ;
2514176e21eeSChuck Lever 
2515176e21eeSChuck Lever 	seq_printf(seq, "\txprt:\tlocal %lu %lu %lu %ld %lu %lu %lu "
251615a45206SAndy Adamson 			"%llu %llu %lu %llu %llu\n",
2517176e21eeSChuck Lever 			xprt->stat.bind_count,
2518176e21eeSChuck Lever 			xprt->stat.connect_count,
25198440a886SChuck Lever 			xprt->stat.connect_time / HZ,
2520176e21eeSChuck Lever 			idle_time,
2521176e21eeSChuck Lever 			xprt->stat.sends,
2522176e21eeSChuck Lever 			xprt->stat.recvs,
2523176e21eeSChuck Lever 			xprt->stat.bad_xids,
2524176e21eeSChuck Lever 			xprt->stat.req_u,
252515a45206SAndy Adamson 			xprt->stat.bklog_u,
252615a45206SAndy Adamson 			xprt->stat.max_slots,
252715a45206SAndy Adamson 			xprt->stat.sending_u,
252815a45206SAndy Adamson 			xprt->stat.pending_u);
2529176e21eeSChuck Lever }
2530176e21eeSChuck Lever 
2531176e21eeSChuck Lever /**
253212b20ce3SBhaskar Chowdhury  * xs_udp_print_stats - display UDP socket-specific stats
2533262ca07dSChuck Lever  * @xprt: rpc_xprt struct containing statistics
2534262ca07dSChuck Lever  * @seq: output file
2535262ca07dSChuck Lever  *
2536262ca07dSChuck Lever  */
2537262ca07dSChuck Lever static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2538262ca07dSChuck Lever {
2539c8475461SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2540c8475461SChuck Lever 
254115a45206SAndy Adamson 	seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %llu %llu "
254215a45206SAndy Adamson 			"%lu %llu %llu\n",
2543fbfffbd5SChuck Lever 			transport->srcport,
2544262ca07dSChuck Lever 			xprt->stat.bind_count,
2545262ca07dSChuck Lever 			xprt->stat.sends,
2546262ca07dSChuck Lever 			xprt->stat.recvs,
2547262ca07dSChuck Lever 			xprt->stat.bad_xids,
2548262ca07dSChuck Lever 			xprt->stat.req_u,
254915a45206SAndy Adamson 			xprt->stat.bklog_u,
255015a45206SAndy Adamson 			xprt->stat.max_slots,
255115a45206SAndy Adamson 			xprt->stat.sending_u,
255215a45206SAndy Adamson 			xprt->stat.pending_u);
2553262ca07dSChuck Lever }
2554262ca07dSChuck Lever 
2555262ca07dSChuck Lever /**
255612b20ce3SBhaskar Chowdhury  * xs_tcp_print_stats - display TCP socket-specific stats
2557262ca07dSChuck Lever  * @xprt: rpc_xprt struct containing statistics
2558262ca07dSChuck Lever  * @seq: output file
2559262ca07dSChuck Lever  *
2560262ca07dSChuck Lever  */
2561262ca07dSChuck Lever static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2562262ca07dSChuck Lever {
2563c8475461SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2564262ca07dSChuck Lever 	long idle_time = 0;
2565262ca07dSChuck Lever 
2566262ca07dSChuck Lever 	if (xprt_connected(xprt))
2567262ca07dSChuck Lever 		idle_time = (long)(jiffies - xprt->last_used) / HZ;
2568262ca07dSChuck Lever 
256915a45206SAndy Adamson 	seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu "
257015a45206SAndy Adamson 			"%llu %llu %lu %llu %llu\n",
2571fbfffbd5SChuck Lever 			transport->srcport,
2572262ca07dSChuck Lever 			xprt->stat.bind_count,
2573262ca07dSChuck Lever 			xprt->stat.connect_count,
25748440a886SChuck Lever 			xprt->stat.connect_time / HZ,
2575262ca07dSChuck Lever 			idle_time,
2576262ca07dSChuck Lever 			xprt->stat.sends,
2577262ca07dSChuck Lever 			xprt->stat.recvs,
2578262ca07dSChuck Lever 			xprt->stat.bad_xids,
2579262ca07dSChuck Lever 			xprt->stat.req_u,
258015a45206SAndy Adamson 			xprt->stat.bklog_u,
258115a45206SAndy Adamson 			xprt->stat.max_slots,
258215a45206SAndy Adamson 			xprt->stat.sending_u,
258315a45206SAndy Adamson 			xprt->stat.pending_u);
2584262ca07dSChuck Lever }
2585262ca07dSChuck Lever 
25864cfc7e60SRahul Iyer /*
25874cfc7e60SRahul Iyer  * Allocate a bunch of pages for a scratch buffer for the rpc code. The reason
25884cfc7e60SRahul Iyer  * we allocate pages instead doing a kmalloc like rpc_malloc is because we want
25894cfc7e60SRahul Iyer  * to use the server side send routines.
25904cfc7e60SRahul Iyer  */
25915fe6eaa1SChuck Lever static int bc_malloc(struct rpc_task *task)
25924cfc7e60SRahul Iyer {
25935fe6eaa1SChuck Lever 	struct rpc_rqst *rqst = task->tk_rqstp;
25945fe6eaa1SChuck Lever 	size_t size = rqst->rq_callsize;
25954cfc7e60SRahul Iyer 	struct page *page;
25964cfc7e60SRahul Iyer 	struct rpc_buffer *buf;
25974cfc7e60SRahul Iyer 
25985fe6eaa1SChuck Lever 	if (size > PAGE_SIZE - sizeof(struct rpc_buffer)) {
25995fe6eaa1SChuck Lever 		WARN_ONCE(1, "xprtsock: large bc buffer request (size %zu)\n",
26005fe6eaa1SChuck Lever 			  size);
26015fe6eaa1SChuck Lever 		return -EINVAL;
26025fe6eaa1SChuck Lever 	}
26034cfc7e60SRahul Iyer 
2604b2648015STrond Myklebust 	page = alloc_page(GFP_KERNEL | __GFP_NORETRY | __GFP_NOWARN);
26054cfc7e60SRahul Iyer 	if (!page)
26065fe6eaa1SChuck Lever 		return -ENOMEM;
26074cfc7e60SRahul Iyer 
26084cfc7e60SRahul Iyer 	buf = page_address(page);
26094cfc7e60SRahul Iyer 	buf->len = PAGE_SIZE;
26104cfc7e60SRahul Iyer 
26115fe6eaa1SChuck Lever 	rqst->rq_buffer = buf->data;
261218e601d6SJeff Layton 	rqst->rq_rbuffer = (char *)rqst->rq_buffer + rqst->rq_callsize;
26135fe6eaa1SChuck Lever 	return 0;
26144cfc7e60SRahul Iyer }
26154cfc7e60SRahul Iyer 
26164cfc7e60SRahul Iyer /*
26174cfc7e60SRahul Iyer  * Free the space allocated in the bc_alloc routine
26184cfc7e60SRahul Iyer  */
26193435c74aSChuck Lever static void bc_free(struct rpc_task *task)
26204cfc7e60SRahul Iyer {
26213435c74aSChuck Lever 	void *buffer = task->tk_rqstp->rq_buffer;
26224cfc7e60SRahul Iyer 	struct rpc_buffer *buf;
26234cfc7e60SRahul Iyer 
26244cfc7e60SRahul Iyer 	buf = container_of(buffer, struct rpc_buffer, data);
26254cfc7e60SRahul Iyer 	free_page((unsigned long)buf);
26264cfc7e60SRahul Iyer }
26274cfc7e60SRahul Iyer 
26284cfc7e60SRahul Iyer static int bc_sendto(struct rpc_rqst *req)
26294cfc7e60SRahul Iyer {
2630da1661b9SChuck Lever 	struct xdr_buf *xdr = &req->rq_snd_buf;
26314cfc7e60SRahul Iyer 	struct sock_xprt *transport =
2632067fb11bSChuck Lever 			container_of(req->rq_xprt, struct sock_xprt, xprt);
2633067fb11bSChuck Lever 	struct msghdr msg = {
2634da1661b9SChuck Lever 		.msg_flags	= 0,
2635067fb11bSChuck Lever 	};
2636067fb11bSChuck Lever 	rpc_fraghdr marker = cpu_to_be32(RPC_LAST_STREAM_FRAGMENT |
2637da1661b9SChuck Lever 					 (u32)xdr->len);
2638da1661b9SChuck Lever 	unsigned int sent = 0;
2639da1661b9SChuck Lever 	int err;
26404cfc7e60SRahul Iyer 
26418729aabaSChuck Lever 	req->rq_xtime = ktime_get();
2642ff053dbbSTrond Myklebust 	err = xdr_alloc_bvec(xdr, rpc_task_gfp_mask());
2643ff053dbbSTrond Myklebust 	if (err < 0)
2644ff053dbbSTrond Myklebust 		return err;
2645da1661b9SChuck Lever 	err = xprt_sock_sendmsg(transport->sock, &msg, xdr, 0, marker, &sent);
2646da1661b9SChuck Lever 	xdr_free_bvec(xdr);
2647da1661b9SChuck Lever 	if (err < 0 || sent != (xdr->len + sizeof(marker)))
2648067fb11bSChuck Lever 		return -EAGAIN;
2649da1661b9SChuck Lever 	return sent;
26504cfc7e60SRahul Iyer }
26514cfc7e60SRahul Iyer 
2652ca4faf54SChuck Lever /**
2653ca4faf54SChuck Lever  * bc_send_request - Send a backchannel Call on a TCP socket
2654ca4faf54SChuck Lever  * @req: rpc_rqst containing Call message to be sent
2655ca4faf54SChuck Lever  *
2656ca4faf54SChuck Lever  * xpt_mutex ensures @rqstp's whole message is written to the socket
2657ca4faf54SChuck Lever  * without interruption.
2658ca4faf54SChuck Lever  *
2659ca4faf54SChuck Lever  * Return values:
2660ca4faf54SChuck Lever  *   %0 if the message was sent successfully
2661ca4faf54SChuck Lever  *   %ENOTCONN if the message was not sent
26624cfc7e60SRahul Iyer  */
2663adfa7144STrond Myklebust static int bc_send_request(struct rpc_rqst *req)
26644cfc7e60SRahul Iyer {
26654cfc7e60SRahul Iyer 	struct svc_xprt	*xprt;
26667fc56136SAndrzej Hajda 	int len;
26674cfc7e60SRahul Iyer 
26684cfc7e60SRahul Iyer 	/*
26694cfc7e60SRahul Iyer 	 * Get the server socket associated with this callback xprt
26704cfc7e60SRahul Iyer 	 */
26714cfc7e60SRahul Iyer 	xprt = req->rq_xprt->bc_xprt;
26724cfc7e60SRahul Iyer 
26734cfc7e60SRahul Iyer 	/*
26744cfc7e60SRahul Iyer 	 * Grab the mutex to serialize data as the connection is shared
26754cfc7e60SRahul Iyer 	 * with the fore channel
26764cfc7e60SRahul Iyer 	 */
2677c544577dSTrond Myklebust 	mutex_lock(&xprt->xpt_mutex);
26784cfc7e60SRahul Iyer 	if (test_bit(XPT_DEAD, &xprt->xpt_flags))
26794cfc7e60SRahul Iyer 		len = -ENOTCONN;
26804cfc7e60SRahul Iyer 	else
26814cfc7e60SRahul Iyer 		len = bc_sendto(req);
26824cfc7e60SRahul Iyer 	mutex_unlock(&xprt->xpt_mutex);
26834cfc7e60SRahul Iyer 
26844cfc7e60SRahul Iyer 	if (len > 0)
26854cfc7e60SRahul Iyer 		len = 0;
26864cfc7e60SRahul Iyer 
26874cfc7e60SRahul Iyer 	return len;
26884cfc7e60SRahul Iyer }
26894cfc7e60SRahul Iyer 
26904cfc7e60SRahul Iyer /*
26914cfc7e60SRahul Iyer  * The close routine. Since this is client initiated, we do nothing
26924cfc7e60SRahul Iyer  */
26934cfc7e60SRahul Iyer 
26944cfc7e60SRahul Iyer static void bc_close(struct rpc_xprt *xprt)
26954cfc7e60SRahul Iyer {
26966221f1d9SChuck Lever 	xprt_disconnect_done(xprt);
26974cfc7e60SRahul Iyer }
26984cfc7e60SRahul Iyer 
26994cfc7e60SRahul Iyer /*
27004cfc7e60SRahul Iyer  * The xprt destroy routine. Again, because this connection is client
27014cfc7e60SRahul Iyer  * initiated, we do nothing
27024cfc7e60SRahul Iyer  */
27034cfc7e60SRahul Iyer 
27044cfc7e60SRahul Iyer static void bc_destroy(struct rpc_xprt *xprt)
27054cfc7e60SRahul Iyer {
270647f72efaSKinglong Mee 	dprintk("RPC:       bc_destroy xprt %p\n", xprt);
270747f72efaSKinglong Mee 
270847f72efaSKinglong Mee 	xs_xprt_free(xprt);
270947f72efaSKinglong Mee 	module_put(THIS_MODULE);
27104cfc7e60SRahul Iyer }
27114cfc7e60SRahul Iyer 
2712d31ae254SChuck Lever static const struct rpc_xprt_ops xs_local_ops = {
2713176e21eeSChuck Lever 	.reserve_xprt		= xprt_reserve_xprt,
27144cd34e7cSTrond Myklebust 	.release_xprt		= xprt_release_xprt,
2715f39c1bfbSTrond Myklebust 	.alloc_slot		= xprt_alloc_slot,
2716a9cde23aSChuck Lever 	.free_slot		= xprt_free_slot,
2717176e21eeSChuck Lever 	.rpcbind		= xs_local_rpcbind,
2718176e21eeSChuck Lever 	.set_port		= xs_local_set_port,
2719dc107402SJ. Bruce Fields 	.connect		= xs_local_connect,
2720176e21eeSChuck Lever 	.buf_alloc		= rpc_malloc,
2721176e21eeSChuck Lever 	.buf_free		= rpc_free,
2722550aebfeSTrond Myklebust 	.prepare_request	= xs_stream_prepare_request,
2723176e21eeSChuck Lever 	.send_request		= xs_local_send_request,
27248ba6a92dSTrond Myklebust 	.wait_for_reply_request	= xprt_wait_for_reply_request_def,
2725176e21eeSChuck Lever 	.close			= xs_close,
2726a1311d87STrond Myklebust 	.destroy		= xs_destroy,
2727176e21eeSChuck Lever 	.print_stats		= xs_local_print_stats,
2728d67fa4d8SJeff Layton 	.enable_swap		= xs_enable_swap,
2729d67fa4d8SJeff Layton 	.disable_swap		= xs_disable_swap,
2730176e21eeSChuck Lever };
2731176e21eeSChuck Lever 
2732d31ae254SChuck Lever static const struct rpc_xprt_ops xs_udp_ops = {
273343118c29SChuck Lever 	.set_buffer_size	= xs_udp_set_buffer_size,
273412a80469SChuck Lever 	.reserve_xprt		= xprt_reserve_xprt_cong,
273549e9a890SChuck Lever 	.release_xprt		= xprt_release_xprt_cong,
2736f39c1bfbSTrond Myklebust 	.alloc_slot		= xprt_alloc_slot,
2737a9cde23aSChuck Lever 	.free_slot		= xprt_free_slot,
273845160d62SChuck Lever 	.rpcbind		= rpcb_getport_async,
273992200412SChuck Lever 	.set_port		= xs_set_port,
27409903cd1cSChuck Lever 	.connect		= xs_connect,
2741421ab1beSTrond Myklebust 	.get_srcaddr		= xs_sock_srcaddr,
2742421ab1beSTrond Myklebust 	.get_srcport		= xs_sock_srcport,
274302107148SChuck Lever 	.buf_alloc		= rpc_malloc,
274402107148SChuck Lever 	.buf_free		= rpc_free,
2745262965f5SChuck Lever 	.send_request		= xs_udp_send_request,
27468ba6a92dSTrond Myklebust 	.wait_for_reply_request	= xprt_wait_for_reply_request_rtt,
274746c0ee8bSChuck Lever 	.timer			= xs_udp_timer,
2748a58dd398SChuck Lever 	.release_request	= xprt_release_rqst_cong,
2749262965f5SChuck Lever 	.close			= xs_close,
2750262965f5SChuck Lever 	.destroy		= xs_destroy,
2751262ca07dSChuck Lever 	.print_stats		= xs_udp_print_stats,
2752d67fa4d8SJeff Layton 	.enable_swap		= xs_enable_swap,
2753d67fa4d8SJeff Layton 	.disable_swap		= xs_disable_swap,
27544a068258SChuck Lever 	.inject_disconnect	= xs_inject_disconnect,
2755262965f5SChuck Lever };
2756262965f5SChuck Lever 
2757d31ae254SChuck Lever static const struct rpc_xprt_ops xs_tcp_ops = {
275812a80469SChuck Lever 	.reserve_xprt		= xprt_reserve_xprt,
27594cd34e7cSTrond Myklebust 	.release_xprt		= xprt_release_xprt,
276036bd7de9STrond Myklebust 	.alloc_slot		= xprt_alloc_slot,
2761a9cde23aSChuck Lever 	.free_slot		= xprt_free_slot,
276245160d62SChuck Lever 	.rpcbind		= rpcb_getport_async,
276392200412SChuck Lever 	.set_port		= xs_set_port,
27640b9e7943STrond Myklebust 	.connect		= xs_connect,
2765421ab1beSTrond Myklebust 	.get_srcaddr		= xs_sock_srcaddr,
2766421ab1beSTrond Myklebust 	.get_srcport		= xs_sock_srcport,
276702107148SChuck Lever 	.buf_alloc		= rpc_malloc,
276802107148SChuck Lever 	.buf_free		= rpc_free,
2769277e4ab7STrond Myklebust 	.prepare_request	= xs_stream_prepare_request,
2770262965f5SChuck Lever 	.send_request		= xs_tcp_send_request,
27718ba6a92dSTrond Myklebust 	.wait_for_reply_request	= xprt_wait_for_reply_request_def,
2772c627d31bSTrond Myklebust 	.close			= xs_tcp_shutdown,
27739903cd1cSChuck Lever 	.destroy		= xs_destroy,
27747196dbb0STrond Myklebust 	.set_connect_timeout	= xs_tcp_set_connect_timeout,
2775262ca07dSChuck Lever 	.print_stats		= xs_tcp_print_stats,
2776d67fa4d8SJeff Layton 	.enable_swap		= xs_enable_swap,
2777d67fa4d8SJeff Layton 	.disable_swap		= xs_disable_swap,
27784a068258SChuck Lever 	.inject_disconnect	= xs_inject_disconnect,
277942e5c3e2SChuck Lever #ifdef CONFIG_SUNRPC_BACKCHANNEL
278042e5c3e2SChuck Lever 	.bc_setup		= xprt_setup_bc,
27816b26cc8cSChuck Lever 	.bc_maxpayload		= xs_tcp_bc_maxpayload,
27827402a4feSTrond Myklebust 	.bc_num_slots		= xprt_bc_max_slots,
278342e5c3e2SChuck Lever 	.bc_free_rqst		= xprt_free_bc_rqst,
278442e5c3e2SChuck Lever 	.bc_destroy		= xprt_destroy_bc,
278542e5c3e2SChuck Lever #endif
2786a246b010SChuck Lever };
2787a246b010SChuck Lever 
27884cfc7e60SRahul Iyer /*
27894cfc7e60SRahul Iyer  * The rpc_xprt_ops for the server backchannel
27904cfc7e60SRahul Iyer  */
27914cfc7e60SRahul Iyer 
2792d31ae254SChuck Lever static const struct rpc_xprt_ops bc_tcp_ops = {
27934cfc7e60SRahul Iyer 	.reserve_xprt		= xprt_reserve_xprt,
27944cfc7e60SRahul Iyer 	.release_xprt		= xprt_release_xprt,
279584e28a30SBryan Schumaker 	.alloc_slot		= xprt_alloc_slot,
2796a9cde23aSChuck Lever 	.free_slot		= xprt_free_slot,
27974cfc7e60SRahul Iyer 	.buf_alloc		= bc_malloc,
27984cfc7e60SRahul Iyer 	.buf_free		= bc_free,
27994cfc7e60SRahul Iyer 	.send_request		= bc_send_request,
28008ba6a92dSTrond Myklebust 	.wait_for_reply_request	= xprt_wait_for_reply_request_def,
28014cfc7e60SRahul Iyer 	.close			= bc_close,
28024cfc7e60SRahul Iyer 	.destroy		= bc_destroy,
28034cfc7e60SRahul Iyer 	.print_stats		= xs_tcp_print_stats,
2804d67fa4d8SJeff Layton 	.enable_swap		= xs_enable_swap,
2805d67fa4d8SJeff Layton 	.disable_swap		= xs_disable_swap,
28064a068258SChuck Lever 	.inject_disconnect	= xs_inject_disconnect,
28074cfc7e60SRahul Iyer };
28084cfc7e60SRahul Iyer 
280992476850SChuck Lever static int xs_init_anyaddr(const int family, struct sockaddr *sap)
281092476850SChuck Lever {
281192476850SChuck Lever 	static const struct sockaddr_in sin = {
281292476850SChuck Lever 		.sin_family		= AF_INET,
281392476850SChuck Lever 		.sin_addr.s_addr	= htonl(INADDR_ANY),
281492476850SChuck Lever 	};
281592476850SChuck Lever 	static const struct sockaddr_in6 sin6 = {
281692476850SChuck Lever 		.sin6_family		= AF_INET6,
281792476850SChuck Lever 		.sin6_addr		= IN6ADDR_ANY_INIT,
281892476850SChuck Lever 	};
281992476850SChuck Lever 
282092476850SChuck Lever 	switch (family) {
2821176e21eeSChuck Lever 	case AF_LOCAL:
2822176e21eeSChuck Lever 		break;
282392476850SChuck Lever 	case AF_INET:
282492476850SChuck Lever 		memcpy(sap, &sin, sizeof(sin));
282592476850SChuck Lever 		break;
282692476850SChuck Lever 	case AF_INET6:
282792476850SChuck Lever 		memcpy(sap, &sin6, sizeof(sin6));
282892476850SChuck Lever 		break;
282992476850SChuck Lever 	default:
283092476850SChuck Lever 		dprintk("RPC:       %s: Bad address family\n", __func__);
283192476850SChuck Lever 		return -EAFNOSUPPORT;
283292476850SChuck Lever 	}
283392476850SChuck Lever 	return 0;
283492476850SChuck Lever }
283592476850SChuck Lever 
28363c341b0bS\"Talpey, Thomas\ static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args,
2837d9ba131dSTrond Myklebust 				      unsigned int slot_table_size,
2838d9ba131dSTrond Myklebust 				      unsigned int max_slot_table_size)
2839c8541ecdSChuck Lever {
2840c8541ecdSChuck Lever 	struct rpc_xprt *xprt;
2841ffc2e518SChuck Lever 	struct sock_xprt *new;
2842c8541ecdSChuck Lever 
284396802a09SFrank van Maarseveen 	if (args->addrlen > sizeof(xprt->addr)) {
2844c8541ecdSChuck Lever 		dprintk("RPC:       xs_setup_xprt: address too large\n");
2845c8541ecdSChuck Lever 		return ERR_PTR(-EBADF);
2846c8541ecdSChuck Lever 	}
2847c8541ecdSChuck Lever 
2848d9ba131dSTrond Myklebust 	xprt = xprt_alloc(args->net, sizeof(*new), slot_table_size,
2849d9ba131dSTrond Myklebust 			max_slot_table_size);
2850bd1722d4SPavel Emelyanov 	if (xprt == NULL) {
285146121cf7SChuck Lever 		dprintk("RPC:       xs_setup_xprt: couldn't allocate "
285246121cf7SChuck Lever 				"rpc_xprt\n");
2853c8541ecdSChuck Lever 		return ERR_PTR(-ENOMEM);
2854c8541ecdSChuck Lever 	}
2855c8541ecdSChuck Lever 
2856bd1722d4SPavel Emelyanov 	new = container_of(xprt, struct sock_xprt, xprt);
2857edc1b01cSTrond Myklebust 	mutex_init(&new->recv_mutex);
285896802a09SFrank van Maarseveen 	memcpy(&xprt->addr, args->dstaddr, args->addrlen);
285996802a09SFrank van Maarseveen 	xprt->addrlen = args->addrlen;
2860d3bc9a1dSFrank van Maarseveen 	if (args->srcaddr)
2861fbfffbd5SChuck Lever 		memcpy(&new->srcaddr, args->srcaddr, args->addrlen);
286292476850SChuck Lever 	else {
286392476850SChuck Lever 		int err;
286492476850SChuck Lever 		err = xs_init_anyaddr(args->dstaddr->sa_family,
286592476850SChuck Lever 					(struct sockaddr *)&new->srcaddr);
28662aa13531SStanislav Kinsbursky 		if (err != 0) {
28672aa13531SStanislav Kinsbursky 			xprt_free(xprt);
286892476850SChuck Lever 			return ERR_PTR(err);
286992476850SChuck Lever 		}
28702aa13531SStanislav Kinsbursky 	}
2871c8541ecdSChuck Lever 
2872c8541ecdSChuck Lever 	return xprt;
2873c8541ecdSChuck Lever }
2874c8541ecdSChuck Lever 
2875176e21eeSChuck Lever static const struct rpc_timeout xs_local_default_timeout = {
2876176e21eeSChuck Lever 	.to_initval = 10 * HZ,
2877176e21eeSChuck Lever 	.to_maxval = 10 * HZ,
2878176e21eeSChuck Lever 	.to_retries = 2,
2879176e21eeSChuck Lever };
2880176e21eeSChuck Lever 
2881176e21eeSChuck Lever /**
2882176e21eeSChuck Lever  * xs_setup_local - Set up transport to use an AF_LOCAL socket
2883176e21eeSChuck Lever  * @args: rpc transport creation arguments
2884176e21eeSChuck Lever  *
2885176e21eeSChuck Lever  * AF_LOCAL is a "tpi_cots_ord" transport, just like TCP
2886176e21eeSChuck Lever  */
2887176e21eeSChuck Lever static struct rpc_xprt *xs_setup_local(struct xprt_create *args)
2888176e21eeSChuck Lever {
2889176e21eeSChuck Lever 	struct sockaddr_un *sun = (struct sockaddr_un *)args->dstaddr;
2890176e21eeSChuck Lever 	struct sock_xprt *transport;
2891176e21eeSChuck Lever 	struct rpc_xprt *xprt;
2892176e21eeSChuck Lever 	struct rpc_xprt *ret;
2893176e21eeSChuck Lever 
2894d9ba131dSTrond Myklebust 	xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
2895d9ba131dSTrond Myklebust 			xprt_max_tcp_slot_table_entries);
2896176e21eeSChuck Lever 	if (IS_ERR(xprt))
2897176e21eeSChuck Lever 		return xprt;
2898176e21eeSChuck Lever 	transport = container_of(xprt, struct sock_xprt, xprt);
2899176e21eeSChuck Lever 
2900176e21eeSChuck Lever 	xprt->prot = 0;
2901d3abc739SOlga Kornievskaia 	xprt->xprt_class = &xs_local_transport;
2902176e21eeSChuck Lever 	xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2903176e21eeSChuck Lever 
2904176e21eeSChuck Lever 	xprt->bind_timeout = XS_BIND_TO;
2905176e21eeSChuck Lever 	xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2906176e21eeSChuck Lever 	xprt->idle_timeout = XS_IDLE_DISC_TO;
2907176e21eeSChuck Lever 
2908176e21eeSChuck Lever 	xprt->ops = &xs_local_ops;
2909176e21eeSChuck Lever 	xprt->timeout = &xs_local_default_timeout;
2910176e21eeSChuck Lever 
2911550aebfeSTrond Myklebust 	INIT_WORK(&transport->recv_worker, xs_stream_data_receive_workfn);
29124f8943f8STrond Myklebust 	INIT_WORK(&transport->error_worker, xs_error_handle);
2913550aebfeSTrond Myklebust 	INIT_DELAYED_WORK(&transport->connect_worker, xs_dummy_setup_socket);
291493dc41bdSNeilBrown 
2915176e21eeSChuck Lever 	switch (sun->sun_family) {
2916176e21eeSChuck Lever 	case AF_LOCAL:
29174388ce05SNeilBrown 		if (sun->sun_path[0] != '/' && sun->sun_path[0] != '\0') {
2918176e21eeSChuck Lever 			dprintk("RPC:       bad AF_LOCAL address: %s\n",
2919176e21eeSChuck Lever 					sun->sun_path);
2920176e21eeSChuck Lever 			ret = ERR_PTR(-EINVAL);
2921176e21eeSChuck Lever 			goto out_err;
2922176e21eeSChuck Lever 		}
2923176e21eeSChuck Lever 		xprt_set_bound(xprt);
2924176e21eeSChuck Lever 		xs_format_peer_addresses(xprt, "local", RPCBIND_NETID_LOCAL);
2925176e21eeSChuck Lever 		break;
2926176e21eeSChuck Lever 	default:
2927176e21eeSChuck Lever 		ret = ERR_PTR(-EAFNOSUPPORT);
2928176e21eeSChuck Lever 		goto out_err;
2929176e21eeSChuck Lever 	}
2930176e21eeSChuck Lever 
2931176e21eeSChuck Lever 	dprintk("RPC:       set up xprt to %s via AF_LOCAL\n",
2932176e21eeSChuck Lever 			xprt->address_strings[RPC_DISPLAY_ADDR]);
2933176e21eeSChuck Lever 
2934176e21eeSChuck Lever 	if (try_module_get(THIS_MODULE))
2935176e21eeSChuck Lever 		return xprt;
2936176e21eeSChuck Lever 	ret = ERR_PTR(-EINVAL);
2937176e21eeSChuck Lever out_err:
2938315f3812SKinglong Mee 	xs_xprt_free(xprt);
2939176e21eeSChuck Lever 	return ret;
2940176e21eeSChuck Lever }
2941176e21eeSChuck Lever 
29422881ae74STrond Myklebust static const struct rpc_timeout xs_udp_default_timeout = {
29432881ae74STrond Myklebust 	.to_initval = 5 * HZ,
29442881ae74STrond Myklebust 	.to_maxval = 30 * HZ,
29452881ae74STrond Myklebust 	.to_increment = 5 * HZ,
29462881ae74STrond Myklebust 	.to_retries = 5,
29472881ae74STrond Myklebust };
29482881ae74STrond Myklebust 
29499903cd1cSChuck Lever /**
29509903cd1cSChuck Lever  * xs_setup_udp - Set up transport to use a UDP socket
295196802a09SFrank van Maarseveen  * @args: rpc transport creation arguments
29529903cd1cSChuck Lever  *
29539903cd1cSChuck Lever  */
2954483066d6SAdrian Bunk static struct rpc_xprt *xs_setup_udp(struct xprt_create *args)
2955a246b010SChuck Lever {
29568f9d5b1aSChuck Lever 	struct sockaddr *addr = args->dstaddr;
2957c8541ecdSChuck Lever 	struct rpc_xprt *xprt;
2958c8475461SChuck Lever 	struct sock_xprt *transport;
29590a68b0beSJ. Bruce Fields 	struct rpc_xprt *ret;
2960a246b010SChuck Lever 
2961d9ba131dSTrond Myklebust 	xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries,
2962d9ba131dSTrond Myklebust 			xprt_udp_slot_table_entries);
2963c8541ecdSChuck Lever 	if (IS_ERR(xprt))
2964c8541ecdSChuck Lever 		return xprt;
2965c8475461SChuck Lever 	transport = container_of(xprt, struct sock_xprt, xprt);
2966a246b010SChuck Lever 
2967ec739ef0SChuck Lever 	xprt->prot = IPPROTO_UDP;
2968d3abc739SOlga Kornievskaia 	xprt->xprt_class = &xs_udp_transport;
2969a246b010SChuck Lever 	/* XXX: header size can vary due to auth type, IPv6, etc. */
2970a246b010SChuck Lever 	xprt->max_payload = (1U << 16) - (MAX_HEADER << 3);
2971a246b010SChuck Lever 
297203bf4b70SChuck Lever 	xprt->bind_timeout = XS_BIND_TO;
297303bf4b70SChuck Lever 	xprt->reestablish_timeout = XS_UDP_REEST_TO;
297403bf4b70SChuck Lever 	xprt->idle_timeout = XS_IDLE_DISC_TO;
2975a246b010SChuck Lever 
2976262965f5SChuck Lever 	xprt->ops = &xs_udp_ops;
2977a246b010SChuck Lever 
2978ba7392bbSTrond Myklebust 	xprt->timeout = &xs_udp_default_timeout;
2979a246b010SChuck Lever 
2980f9b2ee71STrond Myklebust 	INIT_WORK(&transport->recv_worker, xs_udp_data_receive_workfn);
29814f8943f8STrond Myklebust 	INIT_WORK(&transport->error_worker, xs_error_handle);
2982edc1b01cSTrond Myklebust 	INIT_DELAYED_WORK(&transport->connect_worker, xs_udp_setup_socket);
2983edc1b01cSTrond Myklebust 
29848f9d5b1aSChuck Lever 	switch (addr->sa_family) {
29858f9d5b1aSChuck Lever 	case AF_INET:
29868f9d5b1aSChuck Lever 		if (((struct sockaddr_in *)addr)->sin_port != htons(0))
29878f9d5b1aSChuck Lever 			xprt_set_bound(xprt);
29888f9d5b1aSChuck Lever 
29899dc3b095SChuck Lever 		xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP);
29908f9d5b1aSChuck Lever 		break;
29918f9d5b1aSChuck Lever 	case AF_INET6:
29928f9d5b1aSChuck Lever 		if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
29938f9d5b1aSChuck Lever 			xprt_set_bound(xprt);
29948f9d5b1aSChuck Lever 
29959dc3b095SChuck Lever 		xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6);
29968f9d5b1aSChuck Lever 		break;
29978f9d5b1aSChuck Lever 	default:
29980a68b0beSJ. Bruce Fields 		ret = ERR_PTR(-EAFNOSUPPORT);
29990a68b0beSJ. Bruce Fields 		goto out_err;
30008f9d5b1aSChuck Lever 	}
30018f9d5b1aSChuck Lever 
3002c740eff8SChuck Lever 	if (xprt_bound(xprt))
3003c740eff8SChuck Lever 		dprintk("RPC:       set up xprt to %s (port %s) via %s\n",
3004c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_ADDR],
3005c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_PORT],
3006c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_PROTO]);
3007c740eff8SChuck Lever 	else
3008c740eff8SChuck Lever 		dprintk("RPC:       set up xprt to %s (autobind) via %s\n",
3009c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_ADDR],
3010c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_PROTO]);
3011edb267a6SChuck Lever 
3012bc25571eS\"Talpey, Thomas\ 	if (try_module_get(THIS_MODULE))
3013c8541ecdSChuck Lever 		return xprt;
30140a68b0beSJ. Bruce Fields 	ret = ERR_PTR(-EINVAL);
30150a68b0beSJ. Bruce Fields out_err:
3016315f3812SKinglong Mee 	xs_xprt_free(xprt);
30170a68b0beSJ. Bruce Fields 	return ret;
3018a246b010SChuck Lever }
3019a246b010SChuck Lever 
30202881ae74STrond Myklebust static const struct rpc_timeout xs_tcp_default_timeout = {
30212881ae74STrond Myklebust 	.to_initval = 60 * HZ,
30222881ae74STrond Myklebust 	.to_maxval = 60 * HZ,
30232881ae74STrond Myklebust 	.to_retries = 2,
30242881ae74STrond Myklebust };
30252881ae74STrond Myklebust 
30269903cd1cSChuck Lever /**
30279903cd1cSChuck Lever  * xs_setup_tcp - Set up transport to use a TCP socket
302896802a09SFrank van Maarseveen  * @args: rpc transport creation arguments
30299903cd1cSChuck Lever  *
30309903cd1cSChuck Lever  */
3031483066d6SAdrian Bunk static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args)
3032a246b010SChuck Lever {
30338f9d5b1aSChuck Lever 	struct sockaddr *addr = args->dstaddr;
3034c8541ecdSChuck Lever 	struct rpc_xprt *xprt;
3035c8475461SChuck Lever 	struct sock_xprt *transport;
30360a68b0beSJ. Bruce Fields 	struct rpc_xprt *ret;
3037b7993cebSTrond Myklebust 	unsigned int max_slot_table_size = xprt_max_tcp_slot_table_entries;
3038b7993cebSTrond Myklebust 
3039b7993cebSTrond Myklebust 	if (args->flags & XPRT_CREATE_INFINITE_SLOTS)
3040b7993cebSTrond Myklebust 		max_slot_table_size = RPC_MAX_SLOT_TABLE_LIMIT;
3041a246b010SChuck Lever 
3042d9ba131dSTrond Myklebust 	xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
3043b7993cebSTrond Myklebust 			max_slot_table_size);
3044c8541ecdSChuck Lever 	if (IS_ERR(xprt))
3045c8541ecdSChuck Lever 		return xprt;
3046c8475461SChuck Lever 	transport = container_of(xprt, struct sock_xprt, xprt);
3047a246b010SChuck Lever 
3048ec739ef0SChuck Lever 	xprt->prot = IPPROTO_TCP;
3049d3abc739SOlga Kornievskaia 	xprt->xprt_class = &xs_tcp_transport;
3050808012fbSChuck Lever 	xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
3051a246b010SChuck Lever 
305203bf4b70SChuck Lever 	xprt->bind_timeout = XS_BIND_TO;
305303bf4b70SChuck Lever 	xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
305403bf4b70SChuck Lever 	xprt->idle_timeout = XS_IDLE_DISC_TO;
3055a246b010SChuck Lever 
3056262965f5SChuck Lever 	xprt->ops = &xs_tcp_ops;
3057ba7392bbSTrond Myklebust 	xprt->timeout = &xs_tcp_default_timeout;
3058a246b010SChuck Lever 
30593851f1cdSTrond Myklebust 	xprt->max_reconnect_timeout = xprt->timeout->to_maxval;
30607196dbb0STrond Myklebust 	xprt->connect_timeout = xprt->timeout->to_initval *
30617196dbb0STrond Myklebust 		(xprt->timeout->to_retries + 1);
30623851f1cdSTrond Myklebust 
3063c50b8ee0STrond Myklebust 	INIT_WORK(&transport->recv_worker, xs_stream_data_receive_workfn);
30644f8943f8STrond Myklebust 	INIT_WORK(&transport->error_worker, xs_error_handle);
3065edc1b01cSTrond Myklebust 	INIT_DELAYED_WORK(&transport->connect_worker, xs_tcp_setup_socket);
3066edc1b01cSTrond Myklebust 
30678f9d5b1aSChuck Lever 	switch (addr->sa_family) {
30688f9d5b1aSChuck Lever 	case AF_INET:
30698f9d5b1aSChuck Lever 		if (((struct sockaddr_in *)addr)->sin_port != htons(0))
30708f9d5b1aSChuck Lever 			xprt_set_bound(xprt);
30718f9d5b1aSChuck Lever 
30729dc3b095SChuck Lever 		xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP);
30738f9d5b1aSChuck Lever 		break;
30748f9d5b1aSChuck Lever 	case AF_INET6:
30758f9d5b1aSChuck Lever 		if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
30768f9d5b1aSChuck Lever 			xprt_set_bound(xprt);
30778f9d5b1aSChuck Lever 
30789dc3b095SChuck Lever 		xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6);
30798f9d5b1aSChuck Lever 		break;
30808f9d5b1aSChuck Lever 	default:
30810a68b0beSJ. Bruce Fields 		ret = ERR_PTR(-EAFNOSUPPORT);
30820a68b0beSJ. Bruce Fields 		goto out_err;
30838f9d5b1aSChuck Lever 	}
30848f9d5b1aSChuck Lever 
3085c740eff8SChuck Lever 	if (xprt_bound(xprt))
3086c740eff8SChuck Lever 		dprintk("RPC:       set up xprt to %s (port %s) via %s\n",
3087c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_ADDR],
3088c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_PORT],
3089c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_PROTO]);
3090c740eff8SChuck Lever 	else
3091c740eff8SChuck Lever 		dprintk("RPC:       set up xprt to %s (autobind) via %s\n",
3092c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_ADDR],
3093c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_PROTO]);
3094c740eff8SChuck Lever 
3095bc25571eS\"Talpey, Thomas\ 	if (try_module_get(THIS_MODULE))
3096c8541ecdSChuck Lever 		return xprt;
30970a68b0beSJ. Bruce Fields 	ret = ERR_PTR(-EINVAL);
30980a68b0beSJ. Bruce Fields out_err:
3099315f3812SKinglong Mee 	xs_xprt_free(xprt);
31000a68b0beSJ. Bruce Fields 	return ret;
3101a246b010SChuck Lever }
3102282b32e1SChuck Lever 
3103f300babaSAlexandros Batsakis /**
3104f300babaSAlexandros Batsakis  * xs_setup_bc_tcp - Set up transport to use a TCP backchannel socket
3105f300babaSAlexandros Batsakis  * @args: rpc transport creation arguments
3106f300babaSAlexandros Batsakis  *
3107f300babaSAlexandros Batsakis  */
3108f300babaSAlexandros Batsakis static struct rpc_xprt *xs_setup_bc_tcp(struct xprt_create *args)
3109f300babaSAlexandros Batsakis {
3110f300babaSAlexandros Batsakis 	struct sockaddr *addr = args->dstaddr;
3111f300babaSAlexandros Batsakis 	struct rpc_xprt *xprt;
3112f300babaSAlexandros Batsakis 	struct sock_xprt *transport;
3113f300babaSAlexandros Batsakis 	struct svc_sock *bc_sock;
31140a68b0beSJ. Bruce Fields 	struct rpc_xprt *ret;
3115f300babaSAlexandros Batsakis 
3116d9ba131dSTrond Myklebust 	xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
3117d9ba131dSTrond Myklebust 			xprt_tcp_slot_table_entries);
3118f300babaSAlexandros Batsakis 	if (IS_ERR(xprt))
3119f300babaSAlexandros Batsakis 		return xprt;
3120f300babaSAlexandros Batsakis 	transport = container_of(xprt, struct sock_xprt, xprt);
3121f300babaSAlexandros Batsakis 
3122f300babaSAlexandros Batsakis 	xprt->prot = IPPROTO_TCP;
3123d3abc739SOlga Kornievskaia 	xprt->xprt_class = &xs_bc_tcp_transport;
3124f300babaSAlexandros Batsakis 	xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
3125f300babaSAlexandros Batsakis 	xprt->timeout = &xs_tcp_default_timeout;
3126f300babaSAlexandros Batsakis 
3127f300babaSAlexandros Batsakis 	/* backchannel */
3128f300babaSAlexandros Batsakis 	xprt_set_bound(xprt);
3129f300babaSAlexandros Batsakis 	xprt->bind_timeout = 0;
3130f300babaSAlexandros Batsakis 	xprt->reestablish_timeout = 0;
3131f300babaSAlexandros Batsakis 	xprt->idle_timeout = 0;
3132f300babaSAlexandros Batsakis 
3133f300babaSAlexandros Batsakis 	xprt->ops = &bc_tcp_ops;
3134f300babaSAlexandros Batsakis 
3135f300babaSAlexandros Batsakis 	switch (addr->sa_family) {
3136f300babaSAlexandros Batsakis 	case AF_INET:
3137f300babaSAlexandros Batsakis 		xs_format_peer_addresses(xprt, "tcp",
3138f300babaSAlexandros Batsakis 					 RPCBIND_NETID_TCP);
3139f300babaSAlexandros Batsakis 		break;
3140f300babaSAlexandros Batsakis 	case AF_INET6:
3141f300babaSAlexandros Batsakis 		xs_format_peer_addresses(xprt, "tcp",
3142f300babaSAlexandros Batsakis 				   RPCBIND_NETID_TCP6);
3143f300babaSAlexandros Batsakis 		break;
3144f300babaSAlexandros Batsakis 	default:
31450a68b0beSJ. Bruce Fields 		ret = ERR_PTR(-EAFNOSUPPORT);
31460a68b0beSJ. Bruce Fields 		goto out_err;
3147f300babaSAlexandros Batsakis 	}
3148f300babaSAlexandros Batsakis 
3149f300babaSAlexandros Batsakis 	dprintk("RPC:       set up xprt to %s (port %s) via %s\n",
3150f300babaSAlexandros Batsakis 			xprt->address_strings[RPC_DISPLAY_ADDR],
3151f300babaSAlexandros Batsakis 			xprt->address_strings[RPC_DISPLAY_PORT],
3152f300babaSAlexandros Batsakis 			xprt->address_strings[RPC_DISPLAY_PROTO]);
3153f300babaSAlexandros Batsakis 
3154f300babaSAlexandros Batsakis 	/*
315599de8ea9SJ. Bruce Fields 	 * Once we've associated a backchannel xprt with a connection,
315628303ca3SWeng Meiling 	 * we want to keep it around as long as the connection lasts,
315728303ca3SWeng Meiling 	 * in case we need to start using it for a backchannel again;
315828303ca3SWeng Meiling 	 * this reference won't be dropped until bc_xprt is destroyed.
315999de8ea9SJ. Bruce Fields 	 */
316099de8ea9SJ. Bruce Fields 	xprt_get(xprt);
316199de8ea9SJ. Bruce Fields 	args->bc_xprt->xpt_bc_xprt = xprt;
316299de8ea9SJ. Bruce Fields 	xprt->bc_xprt = args->bc_xprt;
316399de8ea9SJ. Bruce Fields 	bc_sock = container_of(args->bc_xprt, struct svc_sock, sk_xprt);
316499de8ea9SJ. Bruce Fields 	transport->sock = bc_sock->sk_sock;
316599de8ea9SJ. Bruce Fields 	transport->inet = bc_sock->sk_sk;
316699de8ea9SJ. Bruce Fields 
316799de8ea9SJ. Bruce Fields 	/*
3168f300babaSAlexandros Batsakis 	 * Since we don't want connections for the backchannel, we set
3169f300babaSAlexandros Batsakis 	 * the xprt status to connected
3170f300babaSAlexandros Batsakis 	 */
3171f300babaSAlexandros Batsakis 	xprt_set_connected(xprt);
3172f300babaSAlexandros Batsakis 
3173f300babaSAlexandros Batsakis 	if (try_module_get(THIS_MODULE))
3174f300babaSAlexandros Batsakis 		return xprt;
3175642aab58SKinglong Mee 
3176642aab58SKinglong Mee 	args->bc_xprt->xpt_bc_xprt = NULL;
317739a9beabSJ. Bruce Fields 	args->bc_xprt->xpt_bc_xps = NULL;
317899de8ea9SJ. Bruce Fields 	xprt_put(xprt);
31790a68b0beSJ. Bruce Fields 	ret = ERR_PTR(-EINVAL);
31800a68b0beSJ. Bruce Fields out_err:
3181315f3812SKinglong Mee 	xs_xprt_free(xprt);
31820a68b0beSJ. Bruce Fields 	return ret;
3183f300babaSAlexandros Batsakis }
3184f300babaSAlexandros Batsakis 
3185176e21eeSChuck Lever static struct xprt_class	xs_local_transport = {
3186176e21eeSChuck Lever 	.list		= LIST_HEAD_INIT(xs_local_transport.list),
3187176e21eeSChuck Lever 	.name		= "named UNIX socket",
3188176e21eeSChuck Lever 	.owner		= THIS_MODULE,
3189176e21eeSChuck Lever 	.ident		= XPRT_TRANSPORT_LOCAL,
3190176e21eeSChuck Lever 	.setup		= xs_setup_local,
3191d5aa6b22STrond Myklebust 	.netid		= { "" },
3192176e21eeSChuck Lever };
3193176e21eeSChuck Lever 
3194bc25571eS\"Talpey, Thomas\ static struct xprt_class	xs_udp_transport = {
3195bc25571eS\"Talpey, Thomas\ 	.list		= LIST_HEAD_INIT(xs_udp_transport.list),
3196bc25571eS\"Talpey, Thomas\ 	.name		= "udp",
3197bc25571eS\"Talpey, Thomas\ 	.owner		= THIS_MODULE,
3198f300babaSAlexandros Batsakis 	.ident		= XPRT_TRANSPORT_UDP,
3199bc25571eS\"Talpey, Thomas\ 	.setup		= xs_setup_udp,
3200d5aa6b22STrond Myklebust 	.netid		= { "udp", "udp6", "" },
3201bc25571eS\"Talpey, Thomas\ };
3202bc25571eS\"Talpey, Thomas\ 
3203bc25571eS\"Talpey, Thomas\ static struct xprt_class	xs_tcp_transport = {
3204bc25571eS\"Talpey, Thomas\ 	.list		= LIST_HEAD_INIT(xs_tcp_transport.list),
3205bc25571eS\"Talpey, Thomas\ 	.name		= "tcp",
3206bc25571eS\"Talpey, Thomas\ 	.owner		= THIS_MODULE,
3207f300babaSAlexandros Batsakis 	.ident		= XPRT_TRANSPORT_TCP,
3208bc25571eS\"Talpey, Thomas\ 	.setup		= xs_setup_tcp,
3209d5aa6b22STrond Myklebust 	.netid		= { "tcp", "tcp6", "" },
3210bc25571eS\"Talpey, Thomas\ };
3211bc25571eS\"Talpey, Thomas\ 
3212f300babaSAlexandros Batsakis static struct xprt_class	xs_bc_tcp_transport = {
3213f300babaSAlexandros Batsakis 	.list		= LIST_HEAD_INIT(xs_bc_tcp_transport.list),
3214f300babaSAlexandros Batsakis 	.name		= "tcp NFSv4.1 backchannel",
3215f300babaSAlexandros Batsakis 	.owner		= THIS_MODULE,
3216f300babaSAlexandros Batsakis 	.ident		= XPRT_TRANSPORT_BC_TCP,
3217f300babaSAlexandros Batsakis 	.setup		= xs_setup_bc_tcp,
3218d5aa6b22STrond Myklebust 	.netid		= { "" },
3219f300babaSAlexandros Batsakis };
3220f300babaSAlexandros Batsakis 
3221282b32e1SChuck Lever /**
3222bc25571eS\"Talpey, Thomas\  * init_socket_xprt - set up xprtsock's sysctls, register with RPC client
3223282b32e1SChuck Lever  *
3224282b32e1SChuck Lever  */
3225282b32e1SChuck Lever int init_socket_xprt(void)
3226282b32e1SChuck Lever {
32272b1bec5fSEric W. Biederman 	if (!sunrpc_table_header)
3228c946cb69SLuis Chamberlain 		sunrpc_table_header = register_sysctl("sunrpc", xs_tunables_table);
3229fbf76683SChuck Lever 
3230176e21eeSChuck Lever 	xprt_register_transport(&xs_local_transport);
3231bc25571eS\"Talpey, Thomas\ 	xprt_register_transport(&xs_udp_transport);
3232bc25571eS\"Talpey, Thomas\ 	xprt_register_transport(&xs_tcp_transport);
3233f300babaSAlexandros Batsakis 	xprt_register_transport(&xs_bc_tcp_transport);
3234bc25571eS\"Talpey, Thomas\ 
3235282b32e1SChuck Lever 	return 0;
3236282b32e1SChuck Lever }
3237282b32e1SChuck Lever 
3238282b32e1SChuck Lever /**
3239bc25571eS\"Talpey, Thomas\  * cleanup_socket_xprt - remove xprtsock's sysctls, unregister
3240282b32e1SChuck Lever  *
3241282b32e1SChuck Lever  */
3242282b32e1SChuck Lever void cleanup_socket_xprt(void)
3243282b32e1SChuck Lever {
3244fbf76683SChuck Lever 	if (sunrpc_table_header) {
3245fbf76683SChuck Lever 		unregister_sysctl_table(sunrpc_table_header);
3246fbf76683SChuck Lever 		sunrpc_table_header = NULL;
3247fbf76683SChuck Lever 	}
3248bc25571eS\"Talpey, Thomas\ 
3249176e21eeSChuck Lever 	xprt_unregister_transport(&xs_local_transport);
3250bc25571eS\"Talpey, Thomas\ 	xprt_unregister_transport(&xs_udp_transport);
3251bc25571eS\"Talpey, Thomas\ 	xprt_unregister_transport(&xs_tcp_transport);
3252f300babaSAlexandros Batsakis 	xprt_unregister_transport(&xs_bc_tcp_transport);
3253282b32e1SChuck Lever }
3254cbf11071STrond Myklebust 
32559bbb9e5aSRusty Russell static int param_set_portnr(const char *val, const struct kernel_param *kp)
3256cbf11071STrond Myklebust {
3257cbf11071STrond Myklebust 	return param_set_uint_minmax(val, kp,
3258cbf11071STrond Myklebust 			RPC_MIN_RESVPORT,
3259cbf11071STrond Myklebust 			RPC_MAX_RESVPORT);
3260cbf11071STrond Myklebust }
3261cbf11071STrond Myklebust 
32629c27847dSLuis R. Rodriguez static const struct kernel_param_ops param_ops_portnr = {
32639bbb9e5aSRusty Russell 	.set = param_set_portnr,
32649bbb9e5aSRusty Russell 	.get = param_get_uint,
32659bbb9e5aSRusty Russell };
32669bbb9e5aSRusty Russell 
3267cbf11071STrond Myklebust #define param_check_portnr(name, p) \
3268cbf11071STrond Myklebust 	__param_check(name, p, unsigned int);
3269cbf11071STrond Myklebust 
3270cbf11071STrond Myklebust module_param_named(min_resvport, xprt_min_resvport, portnr, 0644);
3271cbf11071STrond Myklebust module_param_named(max_resvport, xprt_max_resvport, portnr, 0644);
3272cbf11071STrond Myklebust 
32739bbb9e5aSRusty Russell static int param_set_slot_table_size(const char *val,
32749bbb9e5aSRusty Russell 				     const struct kernel_param *kp)
3275cbf11071STrond Myklebust {
3276cbf11071STrond Myklebust 	return param_set_uint_minmax(val, kp,
3277cbf11071STrond Myklebust 			RPC_MIN_SLOT_TABLE,
3278cbf11071STrond Myklebust 			RPC_MAX_SLOT_TABLE);
3279cbf11071STrond Myklebust }
3280cbf11071STrond Myklebust 
32819c27847dSLuis R. Rodriguez static const struct kernel_param_ops param_ops_slot_table_size = {
32829bbb9e5aSRusty Russell 	.set = param_set_slot_table_size,
32839bbb9e5aSRusty Russell 	.get = param_get_uint,
32849bbb9e5aSRusty Russell };
32859bbb9e5aSRusty Russell 
3286cbf11071STrond Myklebust #define param_check_slot_table_size(name, p) \
3287cbf11071STrond Myklebust 	__param_check(name, p, unsigned int);
3288cbf11071STrond Myklebust 
3289d9ba131dSTrond Myklebust static int param_set_max_slot_table_size(const char *val,
3290d9ba131dSTrond Myklebust 				     const struct kernel_param *kp)
3291d9ba131dSTrond Myklebust {
3292d9ba131dSTrond Myklebust 	return param_set_uint_minmax(val, kp,
3293d9ba131dSTrond Myklebust 			RPC_MIN_SLOT_TABLE,
3294d9ba131dSTrond Myklebust 			RPC_MAX_SLOT_TABLE_LIMIT);
3295d9ba131dSTrond Myklebust }
3296d9ba131dSTrond Myklebust 
32979c27847dSLuis R. Rodriguez static const struct kernel_param_ops param_ops_max_slot_table_size = {
3298d9ba131dSTrond Myklebust 	.set = param_set_max_slot_table_size,
3299d9ba131dSTrond Myklebust 	.get = param_get_uint,
3300d9ba131dSTrond Myklebust };
3301d9ba131dSTrond Myklebust 
3302d9ba131dSTrond Myklebust #define param_check_max_slot_table_size(name, p) \
3303d9ba131dSTrond Myklebust 	__param_check(name, p, unsigned int);
3304d9ba131dSTrond Myklebust 
3305cbf11071STrond Myklebust module_param_named(tcp_slot_table_entries, xprt_tcp_slot_table_entries,
3306cbf11071STrond Myklebust 		   slot_table_size, 0644);
3307d9ba131dSTrond Myklebust module_param_named(tcp_max_slot_table_entries, xprt_max_tcp_slot_table_entries,
3308d9ba131dSTrond Myklebust 		   max_slot_table_size, 0644);
3309cbf11071STrond Myklebust module_param_named(udp_slot_table_entries, xprt_udp_slot_table_entries,
3310cbf11071STrond Myklebust 		   slot_table_size, 0644);
3311