xref: /openbmc/linux/net/sunrpc/xprtsock.c (revision 3e6ff89d)
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>
50dea034b9SChuck Lever #include <net/tls.h>
5175eb6af7SChuck Lever #include <net/handshake.h>
52dea034b9SChuck Lever 
53277e4ab7STrond Myklebust #include <linux/bvec.h>
546a829eb8STrond Myklebust #include <linux/highmem.h>
55277e4ab7STrond Myklebust #include <linux/uio.h>
56a1231fdaSTrond Myklebust #include <linux/sched/mm.h>
57a246b010SChuck Lever 
5840e0b090SPeilin Ye #include <trace/events/sock.h>
5940b5ea0cSTrond Myklebust #include <trace/events/sunrpc.h>
6040b5ea0cSTrond Myklebust 
619e55eef4SChuck Lever #include "socklib.h"
624cfc7e60SRahul Iyer #include "sunrpc.h"
63176e21eeSChuck Lever 
64176e21eeSChuck Lever static void xs_close(struct rpc_xprt *xprt);
653b21f757STrond Myklebust static void xs_set_srcport(struct sock_xprt *transport, struct socket *sock);
667196dbb0STrond Myklebust static void xs_tcp_set_socket_timeouts(struct rpc_xprt *xprt,
677196dbb0STrond Myklebust 		struct socket *sock);
68176e21eeSChuck Lever 
699903cd1cSChuck Lever /*
70c556b754SChuck Lever  * xprtsock tunables
71c556b754SChuck Lever  */
7209acfea5STrond Myklebust static unsigned int xprt_udp_slot_table_entries = RPC_DEF_SLOT_TABLE;
7309acfea5STrond Myklebust static unsigned int xprt_tcp_slot_table_entries = RPC_MIN_SLOT_TABLE;
7409acfea5STrond Myklebust static unsigned int xprt_max_tcp_slot_table_entries = RPC_MAX_SLOT_TABLE;
75c556b754SChuck Lever 
7609acfea5STrond Myklebust static unsigned int xprt_min_resvport = RPC_DEF_MIN_RESVPORT;
7709acfea5STrond Myklebust static unsigned int xprt_max_resvport = RPC_DEF_MAX_RESVPORT;
78c556b754SChuck Lever 
797d1e8255STrond Myklebust #define XS_TCP_LINGER_TO	(15U * HZ)
8025fe6142STrond Myklebust static unsigned int xs_tcp_fin_timeout __read_mostly = XS_TCP_LINGER_TO;
817d1e8255STrond Myklebust 
82c556b754SChuck Lever /*
83fbf76683SChuck Lever  * We can register our own files under /proc/sys/sunrpc by
84c946cb69SLuis Chamberlain  * calling register_sysctl() again.  The files in that
85fbf76683SChuck Lever  * directory become the union of all files registered there.
86fbf76683SChuck Lever  *
87fbf76683SChuck Lever  * We simply need to make sure that we don't collide with
88fbf76683SChuck Lever  * someone else's file names!
89fbf76683SChuck Lever  */
90fbf76683SChuck Lever 
91fbf76683SChuck Lever static unsigned int min_slot_table_size = RPC_MIN_SLOT_TABLE;
92fbf76683SChuck Lever static unsigned int max_slot_table_size = RPC_MAX_SLOT_TABLE;
93d9ba131dSTrond Myklebust static unsigned int max_tcp_slot_table_limit = RPC_MAX_SLOT_TABLE_LIMIT;
94fbf76683SChuck Lever static unsigned int xprt_min_resvport_limit = RPC_MIN_RESVPORT;
95fbf76683SChuck Lever static unsigned int xprt_max_resvport_limit = RPC_MAX_RESVPORT;
96fbf76683SChuck Lever 
97fbf76683SChuck Lever static struct ctl_table_header *sunrpc_table_header;
98fbf76683SChuck Lever 
99d3abc739SOlga Kornievskaia static struct xprt_class xs_local_transport;
100d3abc739SOlga Kornievskaia static struct xprt_class xs_udp_transport;
101d3abc739SOlga Kornievskaia static struct xprt_class xs_tcp_transport;
10275eb6af7SChuck Lever static struct xprt_class xs_tcp_tls_transport;
103d3abc739SOlga Kornievskaia static struct xprt_class xs_bc_tcp_transport;
104d3abc739SOlga Kornievskaia 
105fbf76683SChuck Lever /*
106fbf76683SChuck Lever  * FIXME: changing the UDP slot table size should also resize the UDP
107fbf76683SChuck Lever  *        socket buffers for existing UDP transports
108fbf76683SChuck Lever  */
109fe2c6338SJoe Perches static struct ctl_table xs_tunables_table[] = {
110fbf76683SChuck Lever 	{
111fbf76683SChuck Lever 		.procname	= "udp_slot_table_entries",
112fbf76683SChuck Lever 		.data		= &xprt_udp_slot_table_entries,
113fbf76683SChuck Lever 		.maxlen		= sizeof(unsigned int),
114fbf76683SChuck Lever 		.mode		= 0644,
1156d456111SEric W. Biederman 		.proc_handler	= proc_dointvec_minmax,
116fbf76683SChuck Lever 		.extra1		= &min_slot_table_size,
117fbf76683SChuck Lever 		.extra2		= &max_slot_table_size
118fbf76683SChuck Lever 	},
119fbf76683SChuck Lever 	{
120fbf76683SChuck Lever 		.procname	= "tcp_slot_table_entries",
121fbf76683SChuck Lever 		.data		= &xprt_tcp_slot_table_entries,
122fbf76683SChuck Lever 		.maxlen		= sizeof(unsigned int),
123fbf76683SChuck Lever 		.mode		= 0644,
1246d456111SEric W. Biederman 		.proc_handler	= proc_dointvec_minmax,
125fbf76683SChuck Lever 		.extra1		= &min_slot_table_size,
126fbf76683SChuck Lever 		.extra2		= &max_slot_table_size
127fbf76683SChuck Lever 	},
128fbf76683SChuck Lever 	{
129d9ba131dSTrond Myklebust 		.procname	= "tcp_max_slot_table_entries",
130d9ba131dSTrond Myklebust 		.data		= &xprt_max_tcp_slot_table_entries,
131d9ba131dSTrond Myklebust 		.maxlen		= sizeof(unsigned int),
132d9ba131dSTrond Myklebust 		.mode		= 0644,
133d9ba131dSTrond Myklebust 		.proc_handler	= proc_dointvec_minmax,
134d9ba131dSTrond Myklebust 		.extra1		= &min_slot_table_size,
135d9ba131dSTrond Myklebust 		.extra2		= &max_tcp_slot_table_limit
136d9ba131dSTrond Myklebust 	},
137d9ba131dSTrond Myklebust 	{
138fbf76683SChuck Lever 		.procname	= "min_resvport",
139fbf76683SChuck Lever 		.data		= &xprt_min_resvport,
140fbf76683SChuck Lever 		.maxlen		= sizeof(unsigned int),
141fbf76683SChuck Lever 		.mode		= 0644,
1426d456111SEric W. Biederman 		.proc_handler	= proc_dointvec_minmax,
143fbf76683SChuck Lever 		.extra1		= &xprt_min_resvport_limit,
144826799e6SJ. Bruce Fields 		.extra2		= &xprt_max_resvport_limit
145fbf76683SChuck Lever 	},
146fbf76683SChuck Lever 	{
147fbf76683SChuck Lever 		.procname	= "max_resvport",
148fbf76683SChuck Lever 		.data		= &xprt_max_resvport,
149fbf76683SChuck Lever 		.maxlen		= sizeof(unsigned int),
150fbf76683SChuck Lever 		.mode		= 0644,
1516d456111SEric W. Biederman 		.proc_handler	= proc_dointvec_minmax,
152826799e6SJ. Bruce Fields 		.extra1		= &xprt_min_resvport_limit,
153fbf76683SChuck Lever 		.extra2		= &xprt_max_resvport_limit
154fbf76683SChuck Lever 	},
155fbf76683SChuck Lever 	{
15625fe6142STrond Myklebust 		.procname	= "tcp_fin_timeout",
15725fe6142STrond Myklebust 		.data		= &xs_tcp_fin_timeout,
15825fe6142STrond Myklebust 		.maxlen		= sizeof(xs_tcp_fin_timeout),
15925fe6142STrond Myklebust 		.mode		= 0644,
1606d456111SEric W. Biederman 		.proc_handler	= proc_dointvec_jiffies,
16125fe6142STrond Myklebust 	},
162f8572d8fSEric W. Biederman 	{ },
163fbf76683SChuck Lever };
164fbf76683SChuck Lever 
165fbf76683SChuck Lever /*
16603bf4b70SChuck Lever  * Wait duration for a reply from the RPC portmapper.
16703bf4b70SChuck Lever  */
16803bf4b70SChuck Lever #define XS_BIND_TO		(60U * HZ)
16903bf4b70SChuck Lever 
17003bf4b70SChuck Lever /*
17103bf4b70SChuck Lever  * Delay if a UDP socket connect error occurs.  This is most likely some
17203bf4b70SChuck Lever  * kind of resource problem on the local host.
17303bf4b70SChuck Lever  */
17403bf4b70SChuck Lever #define XS_UDP_REEST_TO		(2U * HZ)
17503bf4b70SChuck Lever 
17603bf4b70SChuck Lever /*
17703bf4b70SChuck Lever  * The reestablish timeout allows clients to delay for a bit before attempting
17803bf4b70SChuck Lever  * to reconnect to a server that just dropped our connection.
17903bf4b70SChuck Lever  *
18003bf4b70SChuck Lever  * We implement an exponential backoff when trying to reestablish a TCP
18103bf4b70SChuck Lever  * transport connection with the server.  Some servers like to drop a TCP
18203bf4b70SChuck Lever  * connection when they are overworked, so we start with a short timeout and
18303bf4b70SChuck Lever  * increase over time if the server is down or not responding.
18403bf4b70SChuck Lever  */
18503bf4b70SChuck Lever #define XS_TCP_INIT_REEST_TO	(3U * HZ)
18603bf4b70SChuck Lever 
18703bf4b70SChuck Lever /*
18803bf4b70SChuck Lever  * TCP idle timeout; client drops the transport socket if it is idle
18903bf4b70SChuck Lever  * for this long.  Note that we also timeout UDP sockets to prevent
19003bf4b70SChuck Lever  * holding port numbers when there is no RPC traffic.
19103bf4b70SChuck Lever  */
19203bf4b70SChuck Lever #define XS_IDLE_DISC_TO		(5U * 60 * HZ)
19303bf4b70SChuck Lever 
19475eb6af7SChuck Lever /*
19575eb6af7SChuck Lever  * TLS handshake timeout.
19675eb6af7SChuck Lever  */
19775eb6af7SChuck Lever #define XS_TLS_HANDSHAKE_TO	(10U * HZ)
19875eb6af7SChuck Lever 
199f895b252SJeff Layton #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
200a246b010SChuck Lever # undef  RPC_DEBUG_DATA
2019903cd1cSChuck Lever # define RPCDBG_FACILITY	RPCDBG_TRANS
202a246b010SChuck Lever #endif
203a246b010SChuck Lever 
204a246b010SChuck Lever #ifdef RPC_DEBUG_DATA
2059903cd1cSChuck Lever static void xs_pktdump(char *msg, u32 *packet, unsigned int count)
206a246b010SChuck Lever {
207a246b010SChuck Lever 	u8 *buf = (u8 *) packet;
208a246b010SChuck Lever 	int j;
209a246b010SChuck Lever 
210a246b010SChuck Lever 	dprintk("RPC:       %s\n", msg);
211a246b010SChuck Lever 	for (j = 0; j < count && j < 128; j += 4) {
212a246b010SChuck Lever 		if (!(j & 31)) {
213a246b010SChuck Lever 			if (j)
214a246b010SChuck Lever 				dprintk("\n");
215a246b010SChuck Lever 			dprintk("0x%04x ", j);
216a246b010SChuck Lever 		}
217a246b010SChuck Lever 		dprintk("%02x%02x%02x%02x ",
218a246b010SChuck Lever 			buf[j], buf[j+1], buf[j+2], buf[j+3]);
219a246b010SChuck Lever 	}
220a246b010SChuck Lever 	dprintk("\n");
221a246b010SChuck Lever }
222a246b010SChuck Lever #else
2239903cd1cSChuck Lever static inline void xs_pktdump(char *msg, u32 *packet, unsigned int count)
224a246b010SChuck Lever {
225a246b010SChuck Lever 	/* NOP */
226a246b010SChuck Lever }
227a246b010SChuck Lever #endif
228a246b010SChuck Lever 
2292118071dSTrond Myklebust static inline struct rpc_xprt *xprt_from_sock(struct sock *sk)
2302118071dSTrond Myklebust {
2312118071dSTrond Myklebust 	return (struct rpc_xprt *) sk->sk_user_data;
2322118071dSTrond Myklebust }
2332118071dSTrond Myklebust 
23495392c59SChuck Lever static inline struct sockaddr *xs_addr(struct rpc_xprt *xprt)
235edb267a6SChuck Lever {
23695392c59SChuck Lever 	return (struct sockaddr *) &xprt->addr;
23795392c59SChuck Lever }
23895392c59SChuck Lever 
239176e21eeSChuck Lever static inline struct sockaddr_un *xs_addr_un(struct rpc_xprt *xprt)
240176e21eeSChuck Lever {
241176e21eeSChuck Lever 	return (struct sockaddr_un *) &xprt->addr;
242176e21eeSChuck Lever }
243176e21eeSChuck Lever 
24495392c59SChuck Lever static inline struct sockaddr_in *xs_addr_in(struct rpc_xprt *xprt)
24595392c59SChuck Lever {
24695392c59SChuck Lever 	return (struct sockaddr_in *) &xprt->addr;
24795392c59SChuck Lever }
24895392c59SChuck Lever 
24995392c59SChuck Lever static inline struct sockaddr_in6 *xs_addr_in6(struct rpc_xprt *xprt)
25095392c59SChuck Lever {
25195392c59SChuck Lever 	return (struct sockaddr_in6 *) &xprt->addr;
25295392c59SChuck Lever }
25395392c59SChuck Lever 
254c877b849SChuck Lever static void xs_format_common_peer_addresses(struct rpc_xprt *xprt)
255c877b849SChuck Lever {
256c877b849SChuck Lever 	struct sockaddr *sap = xs_addr(xprt);
2579dc3b095SChuck Lever 	struct sockaddr_in6 *sin6;
2589dc3b095SChuck Lever 	struct sockaddr_in *sin;
259176e21eeSChuck Lever 	struct sockaddr_un *sun;
260c877b849SChuck Lever 	char buf[128];
261c877b849SChuck Lever 
2629dc3b095SChuck Lever 	switch (sap->sa_family) {
263176e21eeSChuck Lever 	case AF_LOCAL:
264176e21eeSChuck Lever 		sun = xs_addr_un(xprt);
2654388ce05SNeilBrown 		if (sun->sun_path[0]) {
26615bcdc92SWolfram Sang 			strscpy(buf, sun->sun_path, sizeof(buf));
2674388ce05SNeilBrown 		} else {
2684388ce05SNeilBrown 			buf[0] = '@';
2694388ce05SNeilBrown 			strscpy(buf+1, sun->sun_path+1, sizeof(buf)-1);
2704388ce05SNeilBrown 		}
271176e21eeSChuck Lever 		xprt->address_strings[RPC_DISPLAY_ADDR] =
272176e21eeSChuck Lever 						kstrdup(buf, GFP_KERNEL);
273176e21eeSChuck Lever 		break;
2749dc3b095SChuck Lever 	case AF_INET:
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 		sin = xs_addr_in(xprt);
279fc0b5791SJoe Perches 		snprintf(buf, sizeof(buf), "%08x", ntohl(sin->sin_addr.s_addr));
2809dc3b095SChuck Lever 		break;
2819dc3b095SChuck Lever 	case AF_INET6:
282176e21eeSChuck Lever 		(void)rpc_ntop(sap, buf, sizeof(buf));
283176e21eeSChuck Lever 		xprt->address_strings[RPC_DISPLAY_ADDR] =
284176e21eeSChuck Lever 						kstrdup(buf, GFP_KERNEL);
2859dc3b095SChuck Lever 		sin6 = xs_addr_in6(xprt);
286fc0b5791SJoe Perches 		snprintf(buf, sizeof(buf), "%pi6", &sin6->sin6_addr);
2879dc3b095SChuck Lever 		break;
2889dc3b095SChuck Lever 	default:
2899dc3b095SChuck Lever 		BUG();
2909dc3b095SChuck Lever 	}
291176e21eeSChuck Lever 
2929dc3b095SChuck Lever 	xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = kstrdup(buf, GFP_KERNEL);
2939dc3b095SChuck Lever }
2949dc3b095SChuck Lever 
2959dc3b095SChuck Lever static void xs_format_common_peer_ports(struct rpc_xprt *xprt)
2969dc3b095SChuck Lever {
2979dc3b095SChuck Lever 	struct sockaddr *sap = xs_addr(xprt);
2989dc3b095SChuck Lever 	char buf[128];
2999dc3b095SChuck Lever 
30081160e66SJoe Perches 	snprintf(buf, sizeof(buf), "%u", rpc_get_port(sap));
301c877b849SChuck Lever 	xprt->address_strings[RPC_DISPLAY_PORT] = kstrdup(buf, GFP_KERNEL);
302c877b849SChuck Lever 
30381160e66SJoe Perches 	snprintf(buf, sizeof(buf), "%4hx", rpc_get_port(sap));
304c877b849SChuck Lever 	xprt->address_strings[RPC_DISPLAY_HEX_PORT] = kstrdup(buf, GFP_KERNEL);
305c877b849SChuck Lever }
306c877b849SChuck Lever 
3079dc3b095SChuck Lever static void xs_format_peer_addresses(struct rpc_xprt *xprt,
308b454ae90SChuck Lever 				     const char *protocol,
309b454ae90SChuck Lever 				     const char *netid)
310edb267a6SChuck Lever {
311b454ae90SChuck Lever 	xprt->address_strings[RPC_DISPLAY_PROTO] = protocol;
312b454ae90SChuck Lever 	xprt->address_strings[RPC_DISPLAY_NETID] = netid;
313c877b849SChuck Lever 	xs_format_common_peer_addresses(xprt);
3149dc3b095SChuck Lever 	xs_format_common_peer_ports(xprt);
315edb267a6SChuck Lever }
316edb267a6SChuck Lever 
3179dc3b095SChuck Lever static void xs_update_peer_port(struct rpc_xprt *xprt)
3184b6473fbSChuck Lever {
3199dc3b095SChuck Lever 	kfree(xprt->address_strings[RPC_DISPLAY_HEX_PORT]);
3209dc3b095SChuck Lever 	kfree(xprt->address_strings[RPC_DISPLAY_PORT]);
3214b6473fbSChuck Lever 
3229dc3b095SChuck Lever 	xs_format_common_peer_ports(xprt);
323edb267a6SChuck Lever }
324edb267a6SChuck Lever 
325edb267a6SChuck Lever static void xs_free_peer_addresses(struct rpc_xprt *xprt)
326edb267a6SChuck Lever {
32733e01dc7SChuck Lever 	unsigned int i;
32833e01dc7SChuck Lever 
32933e01dc7SChuck Lever 	for (i = 0; i < RPC_DISPLAY_MAX; i++)
33033e01dc7SChuck Lever 		switch (i) {
33133e01dc7SChuck Lever 		case RPC_DISPLAY_PROTO:
33233e01dc7SChuck Lever 		case RPC_DISPLAY_NETID:
33333e01dc7SChuck Lever 			continue;
33433e01dc7SChuck Lever 		default:
33533e01dc7SChuck Lever 			kfree(xprt->address_strings[i]);
33633e01dc7SChuck Lever 		}
337edb267a6SChuck Lever }
338edb267a6SChuck Lever 
339277e4ab7STrond Myklebust static size_t
340277e4ab7STrond Myklebust xs_alloc_sparse_pages(struct xdr_buf *buf, size_t want, gfp_t gfp)
341277e4ab7STrond Myklebust {
342277e4ab7STrond Myklebust 	size_t i,n;
343277e4ab7STrond Myklebust 
34416e5e90fSTrond Myklebust 	if (!want || !(buf->flags & XDRBUF_SPARSE_PAGES))
345277e4ab7STrond Myklebust 		return want;
346277e4ab7STrond Myklebust 	n = (buf->page_base + want + PAGE_SIZE - 1) >> PAGE_SHIFT;
347277e4ab7STrond Myklebust 	for (i = 0; i < n; i++) {
348277e4ab7STrond Myklebust 		if (buf->pages[i])
349277e4ab7STrond Myklebust 			continue;
350277e4ab7STrond Myklebust 		buf->bvec[i].bv_page = buf->pages[i] = alloc_page(gfp);
351277e4ab7STrond Myklebust 		if (!buf->pages[i]) {
35216e5e90fSTrond Myklebust 			i *= PAGE_SIZE;
35316e5e90fSTrond Myklebust 			return i > buf->page_base ? i - buf->page_base : 0;
354277e4ab7STrond Myklebust 		}
355277e4ab7STrond Myklebust 	}
356277e4ab7STrond Myklebust 	return want;
357277e4ab7STrond Myklebust }
358277e4ab7STrond Myklebust 
359dea034b9SChuck Lever static int
360dea034b9SChuck Lever xs_sock_process_cmsg(struct socket *sock, struct msghdr *msg,
361dea034b9SChuck Lever 		     struct cmsghdr *cmsg, int ret)
362dea034b9SChuck Lever {
363dea034b9SChuck Lever 	if (cmsg->cmsg_level == SOL_TLS &&
364dea034b9SChuck Lever 	    cmsg->cmsg_type == TLS_GET_RECORD_TYPE) {
365dea034b9SChuck Lever 		u8 content_type = *((u8 *)CMSG_DATA(cmsg));
366dea034b9SChuck Lever 
367dea034b9SChuck Lever 		switch (content_type) {
368dea034b9SChuck Lever 		case TLS_RECORD_TYPE_DATA:
369dea034b9SChuck Lever 			/* TLS sets EOR at the end of each application data
370dea034b9SChuck Lever 			 * record, even though there might be more frames
371dea034b9SChuck Lever 			 * waiting to be decrypted.
372dea034b9SChuck Lever 			 */
373dea034b9SChuck Lever 			msg->msg_flags &= ~MSG_EOR;
374dea034b9SChuck Lever 			break;
375dea034b9SChuck Lever 		case TLS_RECORD_TYPE_ALERT:
376dea034b9SChuck Lever 			ret = -ENOTCONN;
377dea034b9SChuck Lever 			break;
378dea034b9SChuck Lever 		default:
379dea034b9SChuck Lever 			ret = -EAGAIN;
380dea034b9SChuck Lever 		}
381dea034b9SChuck Lever 	}
382dea034b9SChuck Lever 	return ret;
383dea034b9SChuck Lever }
384dea034b9SChuck Lever 
385dea034b9SChuck Lever static int
386dea034b9SChuck Lever xs_sock_recv_cmsg(struct socket *sock, struct msghdr *msg, int flags)
387dea034b9SChuck Lever {
388dea034b9SChuck Lever 	union {
389dea034b9SChuck Lever 		struct cmsghdr	cmsg;
390dea034b9SChuck Lever 		u8		buf[CMSG_SPACE(sizeof(u8))];
391dea034b9SChuck Lever 	} u;
392dea034b9SChuck Lever 	int ret;
393dea034b9SChuck Lever 
394dea034b9SChuck Lever 	msg->msg_control = &u;
395dea034b9SChuck Lever 	msg->msg_controllen = sizeof(u);
396dea034b9SChuck Lever 	ret = sock_recvmsg(sock, msg, flags);
397dea034b9SChuck Lever 	if (msg->msg_controllen != sizeof(u))
398dea034b9SChuck Lever 		ret = xs_sock_process_cmsg(sock, msg, &u.cmsg, ret);
399dea034b9SChuck Lever 	return ret;
400dea034b9SChuck Lever }
401dea034b9SChuck Lever 
402277e4ab7STrond Myklebust static ssize_t
403277e4ab7STrond Myklebust xs_sock_recvmsg(struct socket *sock, struct msghdr *msg, int flags, size_t seek)
404277e4ab7STrond Myklebust {
405277e4ab7STrond Myklebust 	ssize_t ret;
406277e4ab7STrond Myklebust 	if (seek != 0)
407277e4ab7STrond Myklebust 		iov_iter_advance(&msg->msg_iter, seek);
408dea034b9SChuck Lever 	ret = xs_sock_recv_cmsg(sock, msg, flags);
409277e4ab7STrond Myklebust 	return ret > 0 ? ret + seek : ret;
410277e4ab7STrond Myklebust }
411277e4ab7STrond Myklebust 
412277e4ab7STrond Myklebust static ssize_t
413277e4ab7STrond Myklebust xs_read_kvec(struct socket *sock, struct msghdr *msg, int flags,
414277e4ab7STrond Myklebust 		struct kvec *kvec, size_t count, size_t seek)
415277e4ab7STrond Myklebust {
416de4eda9dSAl Viro 	iov_iter_kvec(&msg->msg_iter, ITER_DEST, kvec, 1, count);
417277e4ab7STrond Myklebust 	return xs_sock_recvmsg(sock, msg, flags, seek);
418277e4ab7STrond Myklebust }
419277e4ab7STrond Myklebust 
420277e4ab7STrond Myklebust static ssize_t
421277e4ab7STrond Myklebust xs_read_bvec(struct socket *sock, struct msghdr *msg, int flags,
422277e4ab7STrond Myklebust 		struct bio_vec *bvec, unsigned long nr, size_t count,
423277e4ab7STrond Myklebust 		size_t seek)
424277e4ab7STrond Myklebust {
425de4eda9dSAl Viro 	iov_iter_bvec(&msg->msg_iter, ITER_DEST, bvec, nr, count);
426277e4ab7STrond Myklebust 	return xs_sock_recvmsg(sock, msg, flags, seek);
427277e4ab7STrond Myklebust }
428277e4ab7STrond Myklebust 
429277e4ab7STrond Myklebust static ssize_t
430277e4ab7STrond Myklebust xs_read_discard(struct socket *sock, struct msghdr *msg, int flags,
431277e4ab7STrond Myklebust 		size_t count)
432277e4ab7STrond Myklebust {
433de4eda9dSAl Viro 	iov_iter_discard(&msg->msg_iter, ITER_DEST, count);
434dea034b9SChuck Lever 	return xs_sock_recv_cmsg(sock, msg, flags);
435277e4ab7STrond Myklebust }
436277e4ab7STrond Myklebust 
4376a829eb8STrond Myklebust #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
4386a829eb8STrond Myklebust static void
4396a829eb8STrond Myklebust xs_flush_bvec(const struct bio_vec *bvec, size_t count, size_t seek)
4406a829eb8STrond Myklebust {
4416a829eb8STrond Myklebust 	struct bvec_iter bi = {
4426a829eb8STrond Myklebust 		.bi_size = count,
4436a829eb8STrond Myklebust 	};
4446a829eb8STrond Myklebust 	struct bio_vec bv;
4456a829eb8STrond Myklebust 
4466a829eb8STrond Myklebust 	bvec_iter_advance(bvec, &bi, seek & PAGE_MASK);
4476a829eb8STrond Myklebust 	for_each_bvec(bv, bvec, bi, bi)
4486a829eb8STrond Myklebust 		flush_dcache_page(bv.bv_page);
4496a829eb8STrond Myklebust }
4506a829eb8STrond Myklebust #else
4516a829eb8STrond Myklebust static inline void
4526a829eb8STrond Myklebust xs_flush_bvec(const struct bio_vec *bvec, size_t count, size_t seek)
4536a829eb8STrond Myklebust {
4546a829eb8STrond Myklebust }
4556a829eb8STrond Myklebust #endif
4566a829eb8STrond Myklebust 
457277e4ab7STrond Myklebust static ssize_t
458277e4ab7STrond Myklebust xs_read_xdr_buf(struct socket *sock, struct msghdr *msg, int flags,
459277e4ab7STrond Myklebust 		struct xdr_buf *buf, size_t count, size_t seek, size_t *read)
460277e4ab7STrond Myklebust {
461277e4ab7STrond Myklebust 	size_t want, seek_init = seek, offset = 0;
462277e4ab7STrond Myklebust 	ssize_t ret;
463277e4ab7STrond Myklebust 
464277e4ab7STrond Myklebust 	want = min_t(size_t, count, buf->head[0].iov_len);
465e92053a5STrond Myklebust 	if (seek < want) {
466277e4ab7STrond Myklebust 		ret = xs_read_kvec(sock, msg, flags, &buf->head[0], want, seek);
467277e4ab7STrond Myklebust 		if (ret <= 0)
468277e4ab7STrond Myklebust 			goto sock_err;
469277e4ab7STrond Myklebust 		offset += ret;
470277e4ab7STrond Myklebust 		if (offset == count || msg->msg_flags & (MSG_EOR|MSG_TRUNC))
471277e4ab7STrond Myklebust 			goto out;
472277e4ab7STrond Myklebust 		if (ret != want)
473c4433055STrond Myklebust 			goto out;
474277e4ab7STrond Myklebust 		seek = 0;
475277e4ab7STrond Myklebust 	} else {
476e92053a5STrond Myklebust 		seek -= want;
477e92053a5STrond Myklebust 		offset += want;
478277e4ab7STrond Myklebust 	}
47916e5e90fSTrond Myklebust 
480b2648015STrond Myklebust 	want = xs_alloc_sparse_pages(
481b2648015STrond Myklebust 		buf, min_t(size_t, count - offset, buf->page_len),
482b2648015STrond Myklebust 		GFP_KERNEL | __GFP_NORETRY | __GFP_NOWARN);
48316e5e90fSTrond Myklebust 	if (seek < want) {
484277e4ab7STrond Myklebust 		ret = xs_read_bvec(sock, msg, flags, buf->bvec,
485277e4ab7STrond Myklebust 				xdr_buf_pagecount(buf),
486277e4ab7STrond Myklebust 				want + buf->page_base,
487277e4ab7STrond Myklebust 				seek + buf->page_base);
488277e4ab7STrond Myklebust 		if (ret <= 0)
489277e4ab7STrond Myklebust 			goto sock_err;
4906a829eb8STrond Myklebust 		xs_flush_bvec(buf->bvec, ret, seek + buf->page_base);
491ac9645c8SDan Aloni 		ret -= buf->page_base;
492ac9645c8SDan Aloni 		offset += ret;
493277e4ab7STrond Myklebust 		if (offset == count || msg->msg_flags & (MSG_EOR|MSG_TRUNC))
494277e4ab7STrond Myklebust 			goto out;
495277e4ab7STrond Myklebust 		if (ret != want)
496c4433055STrond Myklebust 			goto out;
497277e4ab7STrond Myklebust 		seek = 0;
498277e4ab7STrond Myklebust 	} else {
49916e5e90fSTrond Myklebust 		seek -= want;
50016e5e90fSTrond Myklebust 		offset += want;
501277e4ab7STrond Myklebust 	}
50216e5e90fSTrond Myklebust 
503277e4ab7STrond Myklebust 	want = min_t(size_t, count - offset, buf->tail[0].iov_len);
504e92053a5STrond Myklebust 	if (seek < want) {
505277e4ab7STrond Myklebust 		ret = xs_read_kvec(sock, msg, flags, &buf->tail[0], want, seek);
506277e4ab7STrond Myklebust 		if (ret <= 0)
507277e4ab7STrond Myklebust 			goto sock_err;
508277e4ab7STrond Myklebust 		offset += ret;
509277e4ab7STrond Myklebust 		if (offset == count || msg->msg_flags & (MSG_EOR|MSG_TRUNC))
510277e4ab7STrond Myklebust 			goto out;
511277e4ab7STrond Myklebust 		if (ret != want)
512c4433055STrond Myklebust 			goto out;
5139734ad57STrond Myklebust 	} else if (offset < seek_init)
514e92053a5STrond Myklebust 		offset = seek_init;
515277e4ab7STrond Myklebust 	ret = -EMSGSIZE;
516277e4ab7STrond Myklebust out:
517277e4ab7STrond Myklebust 	*read = offset - seek_init;
518277e4ab7STrond Myklebust 	return ret;
519277e4ab7STrond Myklebust sock_err:
520277e4ab7STrond Myklebust 	offset += seek;
521277e4ab7STrond Myklebust 	goto out;
522277e4ab7STrond Myklebust }
523277e4ab7STrond Myklebust 
524277e4ab7STrond Myklebust static void
525277e4ab7STrond Myklebust xs_read_header(struct sock_xprt *transport, struct xdr_buf *buf)
526277e4ab7STrond Myklebust {
527277e4ab7STrond Myklebust 	if (!transport->recv.copied) {
528277e4ab7STrond Myklebust 		if (buf->head[0].iov_len >= transport->recv.offset)
529277e4ab7STrond Myklebust 			memcpy(buf->head[0].iov_base,
530277e4ab7STrond Myklebust 					&transport->recv.xid,
531277e4ab7STrond Myklebust 					transport->recv.offset);
532277e4ab7STrond Myklebust 		transport->recv.copied = transport->recv.offset;
533277e4ab7STrond Myklebust 	}
534277e4ab7STrond Myklebust }
535277e4ab7STrond Myklebust 
536277e4ab7STrond Myklebust static bool
537277e4ab7STrond Myklebust xs_read_stream_request_done(struct sock_xprt *transport)
538277e4ab7STrond Myklebust {
539277e4ab7STrond Myklebust 	return transport->recv.fraghdr & cpu_to_be32(RPC_LAST_STREAM_FRAGMENT);
540277e4ab7STrond Myklebust }
541277e4ab7STrond Myklebust 
542e92053a5STrond Myklebust static void
543e92053a5STrond Myklebust xs_read_stream_check_eor(struct sock_xprt *transport,
544e92053a5STrond Myklebust 		struct msghdr *msg)
545e92053a5STrond Myklebust {
546e92053a5STrond Myklebust 	if (xs_read_stream_request_done(transport))
547e92053a5STrond Myklebust 		msg->msg_flags |= MSG_EOR;
548e92053a5STrond Myklebust }
549e92053a5STrond Myklebust 
550277e4ab7STrond Myklebust static ssize_t
551277e4ab7STrond Myklebust xs_read_stream_request(struct sock_xprt *transport, struct msghdr *msg,
552277e4ab7STrond Myklebust 		int flags, struct rpc_rqst *req)
553277e4ab7STrond Myklebust {
554277e4ab7STrond Myklebust 	struct xdr_buf *buf = &req->rq_private_buf;
5553f649ab7SKees Cook 	size_t want, read;
5563f649ab7SKees Cook 	ssize_t ret;
557277e4ab7STrond Myklebust 
558277e4ab7STrond Myklebust 	xs_read_header(transport, buf);
559277e4ab7STrond Myklebust 
560277e4ab7STrond Myklebust 	want = transport->recv.len - transport->recv.offset;
561e92053a5STrond Myklebust 	if (want != 0) {
562277e4ab7STrond Myklebust 		ret = xs_read_xdr_buf(transport->sock, msg, flags, buf,
563e92053a5STrond Myklebust 				transport->recv.copied + want,
564e92053a5STrond Myklebust 				transport->recv.copied,
565277e4ab7STrond Myklebust 				&read);
566277e4ab7STrond Myklebust 		transport->recv.offset += read;
567277e4ab7STrond Myklebust 		transport->recv.copied += read;
568277e4ab7STrond Myklebust 	}
569277e4ab7STrond Myklebust 
570727fcc64STrond Myklebust 	if (transport->recv.offset == transport->recv.len)
571727fcc64STrond Myklebust 		xs_read_stream_check_eor(transport, msg);
572727fcc64STrond Myklebust 
573e92053a5STrond Myklebust 	if (want == 0)
574e92053a5STrond Myklebust 		return 0;
575e92053a5STrond Myklebust 
576277e4ab7STrond Myklebust 	switch (ret) {
577c4433055STrond Myklebust 	default:
578c4433055STrond Myklebust 		break;
57926781eabSTrond Myklebust 	case -EFAULT:
580277e4ab7STrond Myklebust 	case -EMSGSIZE:
58126781eabSTrond Myklebust 		msg->msg_flags |= MSG_TRUNC;
582c4433055STrond Myklebust 		return read;
583277e4ab7STrond Myklebust 	case 0:
584277e4ab7STrond Myklebust 		return -ESHUTDOWN;
585277e4ab7STrond Myklebust 	}
586c4433055STrond Myklebust 	return ret < 0 ? ret : read;
587277e4ab7STrond Myklebust }
588277e4ab7STrond Myklebust 
589277e4ab7STrond Myklebust static size_t
590277e4ab7STrond Myklebust xs_read_stream_headersize(bool isfrag)
591277e4ab7STrond Myklebust {
592277e4ab7STrond Myklebust 	if (isfrag)
593277e4ab7STrond Myklebust 		return sizeof(__be32);
594277e4ab7STrond Myklebust 	return 3 * sizeof(__be32);
595277e4ab7STrond Myklebust }
596277e4ab7STrond Myklebust 
597277e4ab7STrond Myklebust static ssize_t
598277e4ab7STrond Myklebust xs_read_stream_header(struct sock_xprt *transport, struct msghdr *msg,
599277e4ab7STrond Myklebust 		int flags, size_t want, size_t seek)
600277e4ab7STrond Myklebust {
601277e4ab7STrond Myklebust 	struct kvec kvec = {
602277e4ab7STrond Myklebust 		.iov_base = &transport->recv.fraghdr,
603277e4ab7STrond Myklebust 		.iov_len = want,
604277e4ab7STrond Myklebust 	};
605277e4ab7STrond Myklebust 	return xs_read_kvec(transport->sock, msg, flags, &kvec, want, seek);
606277e4ab7STrond Myklebust }
607277e4ab7STrond Myklebust 
608277e4ab7STrond Myklebust #if defined(CONFIG_SUNRPC_BACKCHANNEL)
609277e4ab7STrond Myklebust static ssize_t
610277e4ab7STrond Myklebust xs_read_stream_call(struct sock_xprt *transport, struct msghdr *msg, int flags)
611277e4ab7STrond Myklebust {
612277e4ab7STrond Myklebust 	struct rpc_xprt *xprt = &transport->xprt;
613277e4ab7STrond Myklebust 	struct rpc_rqst *req;
614277e4ab7STrond Myklebust 	ssize_t ret;
615277e4ab7STrond Myklebust 
61698b5cee3SBenjamin Coddington 	/* Is this transport associated with the backchannel? */
61798b5cee3SBenjamin Coddington 	if (!xprt->bc_serv)
61898b5cee3SBenjamin Coddington 		return -ESHUTDOWN;
61998b5cee3SBenjamin Coddington 
620277e4ab7STrond Myklebust 	/* Look up and lock the request corresponding to the given XID */
621277e4ab7STrond Myklebust 	req = xprt_lookup_bc_request(xprt, transport->recv.xid);
622277e4ab7STrond Myklebust 	if (!req) {
623277e4ab7STrond Myklebust 		printk(KERN_WARNING "Callback slot table overflowed\n");
624277e4ab7STrond Myklebust 		return -ESHUTDOWN;
625277e4ab7STrond Myklebust 	}
62645835a63STrond Myklebust 	if (transport->recv.copied && !req->rq_private_buf.len)
62745835a63STrond Myklebust 		return -ESHUTDOWN;
628277e4ab7STrond Myklebust 
629277e4ab7STrond Myklebust 	ret = xs_read_stream_request(transport, msg, flags, req);
630277e4ab7STrond Myklebust 	if (msg->msg_flags & (MSG_EOR|MSG_TRUNC))
631c4433055STrond Myklebust 		xprt_complete_bc_request(req, transport->recv.copied);
63245835a63STrond Myklebust 	else
63345835a63STrond Myklebust 		req->rq_private_buf.len = transport->recv.copied;
634277e4ab7STrond Myklebust 
635277e4ab7STrond Myklebust 	return ret;
636277e4ab7STrond Myklebust }
637277e4ab7STrond Myklebust #else /* CONFIG_SUNRPC_BACKCHANNEL */
638277e4ab7STrond Myklebust static ssize_t
639277e4ab7STrond Myklebust xs_read_stream_call(struct sock_xprt *transport, struct msghdr *msg, int flags)
640277e4ab7STrond Myklebust {
641277e4ab7STrond Myklebust 	return -ESHUTDOWN;
642277e4ab7STrond Myklebust }
643277e4ab7STrond Myklebust #endif /* CONFIG_SUNRPC_BACKCHANNEL */
644277e4ab7STrond Myklebust 
645277e4ab7STrond Myklebust static ssize_t
646277e4ab7STrond Myklebust xs_read_stream_reply(struct sock_xprt *transport, struct msghdr *msg, int flags)
647277e4ab7STrond Myklebust {
648277e4ab7STrond Myklebust 	struct rpc_xprt *xprt = &transport->xprt;
649277e4ab7STrond Myklebust 	struct rpc_rqst *req;
650277e4ab7STrond Myklebust 	ssize_t ret = 0;
651277e4ab7STrond Myklebust 
652277e4ab7STrond Myklebust 	/* Look up and lock the request corresponding to the given XID */
653277e4ab7STrond Myklebust 	spin_lock(&xprt->queue_lock);
654277e4ab7STrond Myklebust 	req = xprt_lookup_rqst(xprt, transport->recv.xid);
65545835a63STrond Myklebust 	if (!req || (transport->recv.copied && !req->rq_private_buf.len)) {
656277e4ab7STrond Myklebust 		msg->msg_flags |= MSG_TRUNC;
657277e4ab7STrond Myklebust 		goto out;
658277e4ab7STrond Myklebust 	}
659277e4ab7STrond Myklebust 	xprt_pin_rqst(req);
660277e4ab7STrond Myklebust 	spin_unlock(&xprt->queue_lock);
661277e4ab7STrond Myklebust 
662277e4ab7STrond Myklebust 	ret = xs_read_stream_request(transport, msg, flags, req);
663277e4ab7STrond Myklebust 
664277e4ab7STrond Myklebust 	spin_lock(&xprt->queue_lock);
665277e4ab7STrond Myklebust 	if (msg->msg_flags & (MSG_EOR|MSG_TRUNC))
666c4433055STrond Myklebust 		xprt_complete_rqst(req->rq_task, transport->recv.copied);
66745835a63STrond Myklebust 	else
66845835a63STrond Myklebust 		req->rq_private_buf.len = transport->recv.copied;
669277e4ab7STrond Myklebust 	xprt_unpin_rqst(req);
670277e4ab7STrond Myklebust out:
671277e4ab7STrond Myklebust 	spin_unlock(&xprt->queue_lock);
672277e4ab7STrond Myklebust 	return ret;
673277e4ab7STrond Myklebust }
674277e4ab7STrond Myklebust 
675277e4ab7STrond Myklebust static ssize_t
676277e4ab7STrond Myklebust xs_read_stream(struct sock_xprt *transport, int flags)
677277e4ab7STrond Myklebust {
678277e4ab7STrond Myklebust 	struct msghdr msg = { 0 };
679277e4ab7STrond Myklebust 	size_t want, read = 0;
680277e4ab7STrond Myklebust 	ssize_t ret = 0;
681277e4ab7STrond Myklebust 
682277e4ab7STrond Myklebust 	if (transport->recv.len == 0) {
683277e4ab7STrond Myklebust 		want = xs_read_stream_headersize(transport->recv.copied != 0);
684277e4ab7STrond Myklebust 		ret = xs_read_stream_header(transport, &msg, flags, want,
685277e4ab7STrond Myklebust 				transport->recv.offset);
686277e4ab7STrond Myklebust 		if (ret <= 0)
687277e4ab7STrond Myklebust 			goto out_err;
688277e4ab7STrond Myklebust 		transport->recv.offset = ret;
689c4433055STrond Myklebust 		if (transport->recv.offset != want)
690c4433055STrond Myklebust 			return transport->recv.offset;
691277e4ab7STrond Myklebust 		transport->recv.len = be32_to_cpu(transport->recv.fraghdr) &
692277e4ab7STrond Myklebust 			RPC_FRAGMENT_SIZE_MASK;
693277e4ab7STrond Myklebust 		transport->recv.offset -= sizeof(transport->recv.fraghdr);
694277e4ab7STrond Myklebust 		read = ret;
695277e4ab7STrond Myklebust 	}
696277e4ab7STrond Myklebust 
697277e4ab7STrond Myklebust 	switch (be32_to_cpu(transport->recv.calldir)) {
698c4433055STrond Myklebust 	default:
699c4433055STrond Myklebust 		msg.msg_flags |= MSG_TRUNC;
700c4433055STrond Myklebust 		break;
701277e4ab7STrond Myklebust 	case RPC_CALL:
702277e4ab7STrond Myklebust 		ret = xs_read_stream_call(transport, &msg, flags);
703277e4ab7STrond Myklebust 		break;
704277e4ab7STrond Myklebust 	case RPC_REPLY:
705277e4ab7STrond Myklebust 		ret = xs_read_stream_reply(transport, &msg, flags);
706277e4ab7STrond Myklebust 	}
707277e4ab7STrond Myklebust 	if (msg.msg_flags & MSG_TRUNC) {
708277e4ab7STrond Myklebust 		transport->recv.calldir = cpu_to_be32(-1);
709277e4ab7STrond Myklebust 		transport->recv.copied = -1;
710277e4ab7STrond Myklebust 	}
711277e4ab7STrond Myklebust 	if (ret < 0)
712277e4ab7STrond Myklebust 		goto out_err;
713277e4ab7STrond Myklebust 	read += ret;
714277e4ab7STrond Myklebust 	if (transport->recv.offset < transport->recv.len) {
715c4433055STrond Myklebust 		if (!(msg.msg_flags & MSG_TRUNC))
716c4433055STrond Myklebust 			return read;
717b76a5afdSTrond Myklebust 		msg.msg_flags = 0;
718277e4ab7STrond Myklebust 		ret = xs_read_discard(transport->sock, &msg, flags,
719277e4ab7STrond Myklebust 				transport->recv.len - transport->recv.offset);
720277e4ab7STrond Myklebust 		if (ret <= 0)
721277e4ab7STrond Myklebust 			goto out_err;
722277e4ab7STrond Myklebust 		transport->recv.offset += ret;
723277e4ab7STrond Myklebust 		read += ret;
724277e4ab7STrond Myklebust 		if (transport->recv.offset != transport->recv.len)
725c4433055STrond Myklebust 			return read;
726277e4ab7STrond Myklebust 	}
727277e4ab7STrond Myklebust 	if (xs_read_stream_request_done(transport)) {
728c50b8ee0STrond Myklebust 		trace_xs_stream_read_request(transport);
729277e4ab7STrond Myklebust 		transport->recv.copied = 0;
730277e4ab7STrond Myklebust 	}
731277e4ab7STrond Myklebust 	transport->recv.offset = 0;
732277e4ab7STrond Myklebust 	transport->recv.len = 0;
733277e4ab7STrond Myklebust 	return read;
734277e4ab7STrond Myklebust out_err:
73579462857STrond Myklebust 	return ret != 0 ? ret : -ESHUTDOWN;
736277e4ab7STrond Myklebust }
737277e4ab7STrond Myklebust 
7380ffe86f4STrond Myklebust static __poll_t xs_poll_socket(struct sock_xprt *transport)
7390ffe86f4STrond Myklebust {
740a73881c9STrond Myklebust 	return transport->sock->ops->poll(transport->file, transport->sock,
741a73881c9STrond Myklebust 			NULL);
7420ffe86f4STrond Myklebust }
7430ffe86f4STrond Myklebust 
7440ffe86f4STrond Myklebust static bool xs_poll_socket_readable(struct sock_xprt *transport)
7450ffe86f4STrond Myklebust {
7460ffe86f4STrond Myklebust 	__poll_t events = xs_poll_socket(transport);
7470ffe86f4STrond Myklebust 
7480ffe86f4STrond Myklebust 	return (events & (EPOLLIN | EPOLLRDNORM)) && !(events & EPOLLRDHUP);
7490ffe86f4STrond Myklebust }
7500ffe86f4STrond Myklebust 
7510ffe86f4STrond Myklebust static void xs_poll_check_readable(struct sock_xprt *transport)
7520ffe86f4STrond Myklebust {
7530ffe86f4STrond Myklebust 
7540ffe86f4STrond Myklebust 	clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state);
7550d3ca07fSChuck Lever 	if (test_bit(XPRT_SOCK_IGNORE_RECV, &transport->sock_state))
7560d3ca07fSChuck Lever 		return;
7570ffe86f4STrond Myklebust 	if (!xs_poll_socket_readable(transport))
7580ffe86f4STrond Myklebust 		return;
7590ffe86f4STrond Myklebust 	if (!test_and_set_bit(XPRT_SOCK_DATA_READY, &transport->sock_state))
7600ffe86f4STrond Myklebust 		queue_work(xprtiod_workqueue, &transport->recv_worker);
7610ffe86f4STrond Myklebust }
7620ffe86f4STrond Myklebust 
763c50b8ee0STrond Myklebust static void xs_stream_data_receive(struct sock_xprt *transport)
764c50b8ee0STrond Myklebust {
765c50b8ee0STrond Myklebust 	size_t read = 0;
766c50b8ee0STrond Myklebust 	ssize_t ret = 0;
767c50b8ee0STrond Myklebust 
768c50b8ee0STrond Myklebust 	mutex_lock(&transport->recv_mutex);
769c50b8ee0STrond Myklebust 	if (transport->sock == NULL)
770c50b8ee0STrond Myklebust 		goto out;
771c50b8ee0STrond Myklebust 	for (;;) {
772c50b8ee0STrond Myklebust 		ret = xs_read_stream(transport, MSG_DONTWAIT);
773c4433055STrond Myklebust 		if (ret < 0)
774c50b8ee0STrond Myklebust 			break;
775c50b8ee0STrond Myklebust 		read += ret;
776c50b8ee0STrond Myklebust 		cond_resched();
777c50b8ee0STrond Myklebust 	}
7785f52a9d4STrond Myklebust 	if (ret == -ESHUTDOWN)
7795f52a9d4STrond Myklebust 		kernel_sock_shutdown(transport->sock, SHUT_RDWR);
7805f52a9d4STrond Myklebust 	else
7810ffe86f4STrond Myklebust 		xs_poll_check_readable(transport);
782c50b8ee0STrond Myklebust out:
783c50b8ee0STrond Myklebust 	mutex_unlock(&transport->recv_mutex);
784c50b8ee0STrond Myklebust 	trace_xs_stream_read_data(&transport->xprt, ret, read);
785c50b8ee0STrond Myklebust }
786c50b8ee0STrond Myklebust 
787c50b8ee0STrond Myklebust static void xs_stream_data_receive_workfn(struct work_struct *work)
788c50b8ee0STrond Myklebust {
789c50b8ee0STrond Myklebust 	struct sock_xprt *transport =
790c50b8ee0STrond Myklebust 		container_of(work, struct sock_xprt, recv_worker);
791a1231fdaSTrond Myklebust 	unsigned int pflags = memalloc_nofs_save();
792a1231fdaSTrond Myklebust 
793c50b8ee0STrond Myklebust 	xs_stream_data_receive(transport);
794a1231fdaSTrond Myklebust 	memalloc_nofs_restore(pflags);
795c50b8ee0STrond Myklebust }
796c50b8ee0STrond Myklebust 
797550aebfeSTrond Myklebust static void
798550aebfeSTrond Myklebust xs_stream_reset_connect(struct sock_xprt *transport)
799550aebfeSTrond Myklebust {
800550aebfeSTrond Myklebust 	transport->recv.offset = 0;
801550aebfeSTrond Myklebust 	transport->recv.len = 0;
802550aebfeSTrond Myklebust 	transport->recv.copied = 0;
803550aebfeSTrond Myklebust 	transport->xmit.offset = 0;
804ae053551STrond Myklebust }
805ae053551STrond Myklebust 
806ae053551STrond Myklebust static void
807ae053551STrond Myklebust xs_stream_start_connect(struct sock_xprt *transport)
808ae053551STrond Myklebust {
809550aebfeSTrond Myklebust 	transport->xprt.stat.connect_count++;
810550aebfeSTrond Myklebust 	transport->xprt.stat.connect_start = jiffies;
811550aebfeSTrond Myklebust }
812550aebfeSTrond Myklebust 
813b4b5cc85SChuck Lever #define XS_SENDMSG_FLAGS	(MSG_DONTWAIT | MSG_NOSIGNAL)
814b4b5cc85SChuck Lever 
8159903cd1cSChuck Lever /**
816c544577dSTrond Myklebust  * xs_nospace - handle transmit was incomplete
81750f484e2STrond Myklebust  * @req: pointer to RPC request
8187496b59fSTrond Myklebust  * @transport: pointer to struct sock_xprt
8199903cd1cSChuck Lever  *
820a246b010SChuck Lever  */
8217496b59fSTrond Myklebust static int xs_nospace(struct rpc_rqst *req, struct sock_xprt *transport)
822a246b010SChuck Lever {
8237496b59fSTrond Myklebust 	struct rpc_xprt *xprt = &transport->xprt;
82406ea0bfeSTrond Myklebust 	struct sock *sk = transport->inet;
82524ca9a84STrond Myklebust 	int ret = -EAGAIN;
826a246b010SChuck Lever 
827015747d2SChuck Lever 	trace_rpc_socket_nospace(req, transport);
828a246b010SChuck Lever 
829262965f5SChuck Lever 	/* Protect against races with write_space */
830b5e92419STrond Myklebust 	spin_lock(&xprt->transport_lock);
831a246b010SChuck Lever 
832262965f5SChuck Lever 	/* Don't race with disconnect */
833b6ddf64fSTrond Myklebust 	if (xprt_connected(xprt)) {
8347496b59fSTrond Myklebust 		/* wait for more buffer space */
8352790a624STrond Myklebust 		set_bit(XPRT_SOCK_NOSPACE, &transport->sock_state);
8367496b59fSTrond Myklebust 		set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
8377496b59fSTrond Myklebust 		sk->sk_write_pending++;
8387496b59fSTrond Myklebust 		xprt_wait_for_buffer_space(xprt);
8397496b59fSTrond Myklebust 	} else
8407496b59fSTrond Myklebust 		ret = -ENOTCONN;
8417496b59fSTrond Myklebust 
8427496b59fSTrond Myklebust 	spin_unlock(&xprt->transport_lock);
8437496b59fSTrond Myklebust 	return ret;
844d48f9ce7SDavid Vrabel }
8457496b59fSTrond Myklebust 
8467496b59fSTrond Myklebust static int xs_sock_nospace(struct rpc_rqst *req)
8477496b59fSTrond Myklebust {
8487496b59fSTrond Myklebust 	struct sock_xprt *transport =
8497496b59fSTrond Myklebust 		container_of(req->rq_xprt, struct sock_xprt, xprt);
8507496b59fSTrond Myklebust 	struct sock *sk = transport->inet;
8517496b59fSTrond Myklebust 	int ret = -EAGAIN;
8527496b59fSTrond Myklebust 
8537496b59fSTrond Myklebust 	lock_sock(sk);
8547496b59fSTrond Myklebust 	if (!sock_writeable(sk))
8557496b59fSTrond Myklebust 		ret = xs_nospace(req, transport);
8567496b59fSTrond Myklebust 	release_sock(sk);
8577496b59fSTrond Myklebust 	return ret;
8587496b59fSTrond Myklebust }
8597496b59fSTrond Myklebust 
860d0afde5fSTrond Myklebust static int xs_stream_nospace(struct rpc_rqst *req, bool vm_wait)
8617496b59fSTrond Myklebust {
8627496b59fSTrond Myklebust 	struct sock_xprt *transport =
8637496b59fSTrond Myklebust 		container_of(req->rq_xprt, struct sock_xprt, xprt);
8647496b59fSTrond Myklebust 	struct sock *sk = transport->inet;
8657496b59fSTrond Myklebust 	int ret = -EAGAIN;
8667496b59fSTrond Myklebust 
867d0afde5fSTrond Myklebust 	if (vm_wait)
868d0afde5fSTrond Myklebust 		return -ENOBUFS;
8697496b59fSTrond Myklebust 	lock_sock(sk);
8707496b59fSTrond Myklebust 	if (!sk_stream_memory_free(sk))
8717496b59fSTrond Myklebust 		ret = xs_nospace(req, transport);
8727496b59fSTrond Myklebust 	release_sock(sk);
8735e3771ceSTrond Myklebust 	return ret;
874a246b010SChuck Lever }
875a246b010SChuck Lever 
87672691a26STrond Myklebust static int xs_stream_prepare_request(struct rpc_rqst *req, struct xdr_buf *buf)
877277e4ab7STrond Myklebust {
87872691a26STrond Myklebust 	return xdr_alloc_bvec(buf, rpc_task_gfp_mask());
879277e4ab7STrond Myklebust }
880277e4ab7STrond Myklebust 
88161677eeeSChuck Lever /*
8824cd34e7cSTrond Myklebust  * Determine if the previous message in the stream was aborted before it
8834cd34e7cSTrond Myklebust  * could complete transmission.
8844cd34e7cSTrond Myklebust  */
8854cd34e7cSTrond Myklebust static bool
8864cd34e7cSTrond Myklebust xs_send_request_was_aborted(struct sock_xprt *transport, struct rpc_rqst *req)
8874cd34e7cSTrond Myklebust {
8884cd34e7cSTrond Myklebust 	return transport->xmit.offset != 0 && req->rq_bytes_sent == 0;
8894cd34e7cSTrond Myklebust }
8904cd34e7cSTrond Myklebust 
8914cd34e7cSTrond Myklebust /*
89206b5fc3aSTrond Myklebust  * Return the stream record marker field for a record of length < 2^31-1
89361677eeeSChuck Lever  */
89406b5fc3aSTrond Myklebust static rpc_fraghdr
89506b5fc3aSTrond Myklebust xs_stream_record_marker(struct xdr_buf *xdr)
89661677eeeSChuck Lever {
89706b5fc3aSTrond Myklebust 	if (!xdr->len)
89806b5fc3aSTrond Myklebust 		return 0;
89906b5fc3aSTrond Myklebust 	return cpu_to_be32(RPC_LAST_STREAM_FRAGMENT | (u32)xdr->len);
90061677eeeSChuck Lever }
90161677eeeSChuck Lever 
9029903cd1cSChuck Lever /**
903176e21eeSChuck Lever  * xs_local_send_request - write an RPC request to an AF_LOCAL socket
90450f484e2STrond Myklebust  * @req: pointer to RPC request
905176e21eeSChuck Lever  *
906176e21eeSChuck Lever  * Return values:
907176e21eeSChuck Lever  *        0:	The request has been sent
908176e21eeSChuck Lever  *   EAGAIN:	The socket was blocked, please call again later to
909176e21eeSChuck Lever  *		complete the request
910176e21eeSChuck Lever  * ENOTCONN:	Caller needs to invoke connect logic then call again
91112b20ce3SBhaskar Chowdhury  *    other:	Some other error occurred, the request was not sent
912176e21eeSChuck Lever  */
913adfa7144STrond Myklebust static int xs_local_send_request(struct rpc_rqst *req)
914176e21eeSChuck Lever {
915176e21eeSChuck Lever 	struct rpc_xprt *xprt = req->rq_xprt;
916176e21eeSChuck Lever 	struct sock_xprt *transport =
917176e21eeSChuck Lever 				container_of(xprt, struct sock_xprt, xprt);
918176e21eeSChuck Lever 	struct xdr_buf *xdr = &req->rq_snd_buf;
9197e3d3620STrond Myklebust 	rpc_fraghdr rm = xs_stream_record_marker(xdr);
9207e3d3620STrond Myklebust 	unsigned int msglen = rm ? req->rq_slen + sizeof(rm) : req->rq_slen;
9219e55eef4SChuck Lever 	struct msghdr msg = {
9229e55eef4SChuck Lever 		.msg_flags	= XS_SENDMSG_FLAGS,
9239e55eef4SChuck Lever 	};
924d0afde5fSTrond Myklebust 	bool vm_wait;
9253f649ab7SKees Cook 	unsigned int sent;
926176e21eeSChuck Lever 	int status;
927176e21eeSChuck Lever 
9284cd34e7cSTrond Myklebust 	/* Close the stream if the previous transmission was incomplete */
9294cd34e7cSTrond Myklebust 	if (xs_send_request_was_aborted(transport, req)) {
930f0043206STrond Myklebust 		xprt_force_disconnect(xprt);
9314cd34e7cSTrond Myklebust 		return -ENOTCONN;
9324cd34e7cSTrond Myklebust 	}
9334cd34e7cSTrond Myklebust 
934176e21eeSChuck Lever 	xs_pktdump("packet data:",
935176e21eeSChuck Lever 			req->rq_svec->iov_base, req->rq_svec->iov_len);
936176e21eeSChuck Lever 
937d0afde5fSTrond Myklebust 	vm_wait = sk_stream_is_writeable(transport->inet) ? true : false;
938d0afde5fSTrond Myklebust 
93978215759SChuck Lever 	req->rq_xtime = ktime_get();
9409e55eef4SChuck Lever 	status = xprt_sock_sendmsg(transport->sock, &msg, xdr,
9417e3d3620STrond Myklebust 				   transport->xmit.offset, rm, &sent);
942176e21eeSChuck Lever 	dprintk("RPC:       %s(%u) = %d\n",
9436c7a64e5STrond Myklebust 			__func__, xdr->len - transport->xmit.offset, status);
944743c69e7SNeilBrown 
945f279cd00SJason Baron 	if (likely(sent > 0) || status == 0) {
9466c7a64e5STrond Myklebust 		transport->xmit.offset += sent;
9476c7a64e5STrond Myklebust 		req->rq_bytes_sent = transport->xmit.offset;
9487e3d3620STrond Myklebust 		if (likely(req->rq_bytes_sent >= msglen)) {
9496c7a64e5STrond Myklebust 			req->rq_xmit_bytes_sent += transport->xmit.offset;
9506c7a64e5STrond Myklebust 			transport->xmit.offset = 0;
951176e21eeSChuck Lever 			return 0;
952176e21eeSChuck Lever 		}
953176e21eeSChuck Lever 		status = -EAGAIN;
954d0afde5fSTrond Myklebust 		vm_wait = false;
955176e21eeSChuck Lever 	}
956176e21eeSChuck Lever 
957176e21eeSChuck Lever 	switch (status) {
958176e21eeSChuck Lever 	case -EAGAIN:
959d0afde5fSTrond Myklebust 		status = xs_stream_nospace(req, vm_wait);
960176e21eeSChuck Lever 		break;
961176e21eeSChuck Lever 	default:
962176e21eeSChuck Lever 		dprintk("RPC:       sendmsg returned unrecognized error %d\n",
963176e21eeSChuck Lever 			-status);
964df561f66SGustavo A. R. Silva 		fallthrough;
965176e21eeSChuck Lever 	case -EPIPE:
966f0043206STrond Myklebust 		xprt_force_disconnect(xprt);
967176e21eeSChuck Lever 		status = -ENOTCONN;
968176e21eeSChuck Lever 	}
969176e21eeSChuck Lever 
970176e21eeSChuck Lever 	return status;
971176e21eeSChuck Lever }
972176e21eeSChuck Lever 
973176e21eeSChuck Lever /**
974262965f5SChuck Lever  * xs_udp_send_request - write an RPC request to a UDP socket
97550f484e2STrond Myklebust  * @req: pointer to RPC request
9769903cd1cSChuck Lever  *
9779903cd1cSChuck Lever  * Return values:
9789903cd1cSChuck Lever  *        0:	The request has been sent
9799903cd1cSChuck Lever  *   EAGAIN:	The socket was blocked, please call again later to
9809903cd1cSChuck Lever  *		complete the request
981262965f5SChuck Lever  * ENOTCONN:	Caller needs to invoke connect logic then call again
98225985edcSLucas De Marchi  *    other:	Some other error occurred, the request was not sent
9839903cd1cSChuck Lever  */
984adfa7144STrond Myklebust static int xs_udp_send_request(struct rpc_rqst *req)
985a246b010SChuck Lever {
986a246b010SChuck Lever 	struct rpc_xprt *xprt = req->rq_xprt;
987ee0ac0c2SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
988262965f5SChuck Lever 	struct xdr_buf *xdr = &req->rq_snd_buf;
9899e55eef4SChuck Lever 	struct msghdr msg = {
9909e55eef4SChuck Lever 		.msg_name	= xs_addr(xprt),
9919e55eef4SChuck Lever 		.msg_namelen	= xprt->addrlen,
9929e55eef4SChuck Lever 		.msg_flags	= XS_SENDMSG_FLAGS,
9939e55eef4SChuck Lever 	};
9943f649ab7SKees Cook 	unsigned int sent;
995262965f5SChuck Lever 	int status;
996262965f5SChuck Lever 
997262965f5SChuck Lever 	xs_pktdump("packet data:",
998262965f5SChuck Lever 				req->rq_svec->iov_base,
999262965f5SChuck Lever 				req->rq_svec->iov_len);
1000262965f5SChuck Lever 
100101d37c42STrond Myklebust 	if (!xprt_bound(xprt))
100201d37c42STrond Myklebust 		return -ENOTCONN;
100375891f50STrond Myklebust 
100475891f50STrond Myklebust 	if (!xprt_request_get_cong(xprt, req))
100575891f50STrond Myklebust 		return -EBADSLT;
100675891f50STrond Myklebust 
1007ff053dbbSTrond Myklebust 	status = xdr_alloc_bvec(xdr, rpc_task_gfp_mask());
1008ff053dbbSTrond Myklebust 	if (status < 0)
1009ff053dbbSTrond Myklebust 		return status;
101078215759SChuck Lever 	req->rq_xtime = ktime_get();
10119e55eef4SChuck Lever 	status = xprt_sock_sendmsg(transport->sock, &msg, xdr, 0, 0, &sent);
1012262965f5SChuck Lever 
1013262965f5SChuck Lever 	dprintk("RPC:       xs_udp_send_request(%u) = %d\n",
10146c7a64e5STrond Myklebust 			xdr->len, status);
1015262965f5SChuck Lever 
10163dedbb5cSJason Baron 	/* firewall is blocking us, don't return -EAGAIN or we end up looping */
10173dedbb5cSJason Baron 	if (status == -EPERM)
10183dedbb5cSJason Baron 		goto process_status;
10193dedbb5cSJason Baron 
1020743c69e7SNeilBrown 	if (status == -EAGAIN && sock_writeable(transport->inet))
1021743c69e7SNeilBrown 		status = -ENOBUFS;
1022743c69e7SNeilBrown 
1023f279cd00SJason Baron 	if (sent > 0 || status == 0) {
1024f279cd00SJason Baron 		req->rq_xmit_bytes_sent += sent;
1025f279cd00SJason Baron 		if (sent >= req->rq_slen)
1026262965f5SChuck Lever 			return 0;
1027262965f5SChuck Lever 		/* Still some bytes left; set up for a retry later. */
1028262965f5SChuck Lever 		status = -EAGAIN;
10292199700fSTrond Myklebust 	}
1030262965f5SChuck Lever 
10313dedbb5cSJason Baron process_status:
1032262965f5SChuck Lever 	switch (status) {
1033fba91afbSTrond Myklebust 	case -ENOTSOCK:
1034fba91afbSTrond Myklebust 		status = -ENOTCONN;
1035fba91afbSTrond Myklebust 		/* Should we call xs_close() here? */
1036fba91afbSTrond Myklebust 		break;
1037b6ddf64fSTrond Myklebust 	case -EAGAIN:
10387496b59fSTrond Myklebust 		status = xs_sock_nospace(req);
1039b6ddf64fSTrond Myklebust 		break;
1040262965f5SChuck Lever 	case -ENETUNREACH:
10413601c4a9STrond Myklebust 	case -ENOBUFS:
1042262965f5SChuck Lever 	case -EPIPE:
1043262965f5SChuck Lever 	case -ECONNREFUSED:
10443dedbb5cSJason Baron 	case -EPERM:
1045262965f5SChuck Lever 		/* When the server has died, an ICMP port unreachable message
1046262965f5SChuck Lever 		 * prompts ECONNREFUSED. */
104713331a55STrond Myklebust 		break;
104813331a55STrond Myklebust 	default:
104913331a55STrond Myklebust 		dprintk("RPC:       sendmsg returned unrecognized error %d\n",
105013331a55STrond Myklebust 			-status);
1051262965f5SChuck Lever 	}
10525fe46e9dSBian Naimeng 
1053262965f5SChuck Lever 	return status;
1054262965f5SChuck Lever }
1055262965f5SChuck Lever 
1056e06799f9STrond Myklebust /**
1057262965f5SChuck Lever  * xs_tcp_send_request - write an RPC request to a TCP socket
105850f484e2STrond Myklebust  * @req: pointer to RPC request
1059262965f5SChuck Lever  *
1060262965f5SChuck Lever  * Return values:
1061262965f5SChuck Lever  *        0:	The request has been sent
1062262965f5SChuck Lever  *   EAGAIN:	The socket was blocked, please call again later to
1063262965f5SChuck Lever  *		complete the request
1064262965f5SChuck Lever  * ENOTCONN:	Caller needs to invoke connect logic then call again
106525985edcSLucas De Marchi  *    other:	Some other error occurred, the request was not sent
1066262965f5SChuck Lever  *
1067262965f5SChuck Lever  * XXX: In the case of soft timeouts, should we eventually give up
1068262965f5SChuck Lever  *	if sendmsg is not able to make progress?
1069262965f5SChuck Lever  */
1070adfa7144STrond Myklebust static int xs_tcp_send_request(struct rpc_rqst *req)
1071262965f5SChuck Lever {
1072262965f5SChuck Lever 	struct rpc_xprt *xprt = req->rq_xprt;
1073ee0ac0c2SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1074262965f5SChuck Lever 	struct xdr_buf *xdr = &req->rq_snd_buf;
10757e3d3620STrond Myklebust 	rpc_fraghdr rm = xs_stream_record_marker(xdr);
10767e3d3620STrond Myklebust 	unsigned int msglen = rm ? req->rq_slen + sizeof(rm) : req->rq_slen;
10779e55eef4SChuck Lever 	struct msghdr msg = {
10789e55eef4SChuck Lever 		.msg_flags	= XS_SENDMSG_FLAGS,
10799e55eef4SChuck Lever 	};
1080d0afde5fSTrond Myklebust 	bool vm_wait;
10813f649ab7SKees Cook 	unsigned int sent;
1082b595bb15SChuck Lever 	int status;
1083a246b010SChuck Lever 
10844cd34e7cSTrond Myklebust 	/* Close the stream if the previous transmission was incomplete */
10854cd34e7cSTrond Myklebust 	if (xs_send_request_was_aborted(transport, req)) {
10864cd34e7cSTrond Myklebust 		if (transport->sock != NULL)
10874cd34e7cSTrond Myklebust 			kernel_sock_shutdown(transport->sock, SHUT_RDWR);
10884cd34e7cSTrond Myklebust 		return -ENOTCONN;
10894cd34e7cSTrond Myklebust 	}
1090d275880aSTrond Myklebust 	if (!transport->inet)
1091d275880aSTrond Myklebust 		return -ENOTCONN;
10924cd34e7cSTrond Myklebust 
1093262965f5SChuck Lever 	xs_pktdump("packet data:",
1094262965f5SChuck Lever 				req->rq_svec->iov_base,
1095262965f5SChuck Lever 				req->rq_svec->iov_len);
1096a246b010SChuck Lever 
10977196dbb0STrond Myklebust 	if (test_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state))
10987196dbb0STrond Myklebust 		xs_tcp_set_socket_timeouts(xprt, transport->sock);
10997196dbb0STrond Myklebust 
11003b21f757STrond Myklebust 	xs_set_srcport(transport, transport->sock);
11013b21f757STrond Myklebust 
1102a246b010SChuck Lever 	/* Continue transmitting the packet/record. We must be careful
1103a246b010SChuck Lever 	 * to cope with writespace callbacks arriving _after_ we have
1104262965f5SChuck Lever 	 * called sendmsg(). */
110578215759SChuck Lever 	req->rq_xtime = ktime_get();
1106d737e5d4STrond Myklebust 	tcp_sock_set_cork(transport->inet, true);
1107d0afde5fSTrond Myklebust 
1108d0afde5fSTrond Myklebust 	vm_wait = sk_stream_is_writeable(transport->inet) ? true : false;
1109d0afde5fSTrond Myklebust 
1110d0afde5fSTrond Myklebust 	do {
11119e55eef4SChuck Lever 		status = xprt_sock_sendmsg(transport->sock, &msg, xdr,
11127e3d3620STrond Myklebust 					   transport->xmit.offset, rm, &sent);
1113a246b010SChuck Lever 
1114262965f5SChuck Lever 		dprintk("RPC:       xs_tcp_send_request(%u) = %d\n",
11156c7a64e5STrond Myklebust 				xdr->len - transport->xmit.offset, status);
1116262965f5SChuck Lever 
1117a246b010SChuck Lever 		/* If we've sent the entire packet, immediately
1118a246b010SChuck Lever 		 * reset the count of bytes sent. */
11196c7a64e5STrond Myklebust 		transport->xmit.offset += sent;
11206c7a64e5STrond Myklebust 		req->rq_bytes_sent = transport->xmit.offset;
11217e3d3620STrond Myklebust 		if (likely(req->rq_bytes_sent >= msglen)) {
11226c7a64e5STrond Myklebust 			req->rq_xmit_bytes_sent += transport->xmit.offset;
11236c7a64e5STrond Myklebust 			transport->xmit.offset = 0;
1124d737e5d4STrond Myklebust 			if (atomic_long_read(&xprt->xmit_queuelen) == 1)
1125d737e5d4STrond Myklebust 				tcp_sock_set_cork(transport->inet, false);
1126a246b010SChuck Lever 			return 0;
1127a246b010SChuck Lever 		}
1128262965f5SChuck Lever 
11299ffadfbcSTrond Myklebust 		WARN_ON_ONCE(sent == 0 && status == 0);
11309ffadfbcSTrond Myklebust 
1131d0afde5fSTrond Myklebust 		if (sent > 0)
11329ffadfbcSTrond Myklebust 			vm_wait = false;
1133d0afde5fSTrond Myklebust 
1134d0afde5fSTrond Myklebust 	} while (status == 0);
1135a246b010SChuck Lever 
1136262965f5SChuck Lever 	switch (status) {
1137fba91afbSTrond Myklebust 	case -ENOTSOCK:
1138fba91afbSTrond Myklebust 		status = -ENOTCONN;
1139fba91afbSTrond Myklebust 		/* Should we call xs_close() here? */
1140fba91afbSTrond Myklebust 		break;
1141262965f5SChuck Lever 	case -EAGAIN:
1142d0afde5fSTrond Myklebust 		status = xs_stream_nospace(req, vm_wait);
1143262965f5SChuck Lever 		break;
1144262965f5SChuck Lever 	case -ECONNRESET:
1145262965f5SChuck Lever 	case -ECONNREFUSED:
1146262965f5SChuck Lever 	case -ENOTCONN:
11473913c78cSTrond Myklebust 	case -EADDRINUSE:
1148b5872f0cSTrond Myklebust 	case -ENOBUFS:
1149b9d2bb2eSTrond Myklebust 	case -EPIPE:
115013331a55STrond Myklebust 		break;
115113331a55STrond Myklebust 	default:
115213331a55STrond Myklebust 		dprintk("RPC:       sendmsg returned unrecognized error %d\n",
115313331a55STrond Myklebust 			-status);
1154a246b010SChuck Lever 	}
11555fe46e9dSBian Naimeng 
1156a246b010SChuck Lever 	return status;
1157a246b010SChuck Lever }
1158a246b010SChuck Lever 
11592a9e1cfaSTrond Myklebust static void xs_save_old_callbacks(struct sock_xprt *transport, struct sock *sk)
11602a9e1cfaSTrond Myklebust {
11612a9e1cfaSTrond Myklebust 	transport->old_data_ready = sk->sk_data_ready;
11622a9e1cfaSTrond Myklebust 	transport->old_state_change = sk->sk_state_change;
11632a9e1cfaSTrond Myklebust 	transport->old_write_space = sk->sk_write_space;
11642118071dSTrond Myklebust 	transport->old_error_report = sk->sk_error_report;
11652a9e1cfaSTrond Myklebust }
11662a9e1cfaSTrond Myklebust 
11672a9e1cfaSTrond Myklebust static void xs_restore_old_callbacks(struct sock_xprt *transport, struct sock *sk)
11682a9e1cfaSTrond Myklebust {
11692a9e1cfaSTrond Myklebust 	sk->sk_data_ready = transport->old_data_ready;
11702a9e1cfaSTrond Myklebust 	sk->sk_state_change = transport->old_state_change;
11712a9e1cfaSTrond Myklebust 	sk->sk_write_space = transport->old_write_space;
11722118071dSTrond Myklebust 	sk->sk_error_report = transport->old_error_report;
11732118071dSTrond Myklebust }
11742118071dSTrond Myklebust 
117542d42a5bSTrond Myklebust static void xs_sock_reset_state_flags(struct rpc_xprt *xprt)
117642d42a5bSTrond Myklebust {
117742d42a5bSTrond Myklebust 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
117842d42a5bSTrond Myklebust 
117942d42a5bSTrond Myklebust 	clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state);
11804f8943f8STrond Myklebust 	clear_bit(XPRT_SOCK_WAKE_ERROR, &transport->sock_state);
11814f8943f8STrond Myklebust 	clear_bit(XPRT_SOCK_WAKE_WRITE, &transport->sock_state);
11824f8943f8STrond Myklebust 	clear_bit(XPRT_SOCK_WAKE_DISCONNECT, &transport->sock_state);
11832790a624STrond Myklebust 	clear_bit(XPRT_SOCK_NOSPACE, &transport->sock_state);
11844f8943f8STrond Myklebust }
11854f8943f8STrond Myklebust 
11864f8943f8STrond Myklebust static void xs_run_error_worker(struct sock_xprt *transport, unsigned int nr)
11874f8943f8STrond Myklebust {
11884f8943f8STrond Myklebust 	set_bit(nr, &transport->sock_state);
11894f8943f8STrond Myklebust 	queue_work(xprtiod_workqueue, &transport->error_worker);
119042d42a5bSTrond Myklebust }
119142d42a5bSTrond Myklebust 
1192b70ae915STrond Myklebust static void xs_sock_reset_connection_flags(struct rpc_xprt *xprt)
1193b70ae915STrond Myklebust {
1194d896ba83STrond Myklebust 	xprt->connect_cookie++;
1195b70ae915STrond Myklebust 	smp_mb__before_atomic();
1196b70ae915STrond Myklebust 	clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
1197b70ae915STrond Myklebust 	clear_bit(XPRT_CLOSING, &xprt->state);
119842d42a5bSTrond Myklebust 	xs_sock_reset_state_flags(xprt);
1199b70ae915STrond Myklebust 	smp_mb__after_atomic();
1200b70ae915STrond Myklebust }
1201b70ae915STrond Myklebust 
12022118071dSTrond Myklebust /**
12032118071dSTrond Myklebust  * xs_error_report - callback to handle TCP socket state errors
12042118071dSTrond Myklebust  * @sk: socket
12052118071dSTrond Myklebust  *
12062118071dSTrond Myklebust  * Note: we don't call sock_error() since there may be a rpc_task
12072118071dSTrond Myklebust  * using the socket, and so we don't want to clear sk->sk_err.
12082118071dSTrond Myklebust  */
12092118071dSTrond Myklebust static void xs_error_report(struct sock *sk)
12102118071dSTrond Myklebust {
12114f8943f8STrond Myklebust 	struct sock_xprt *transport;
12122118071dSTrond Myklebust 	struct rpc_xprt *xprt;
12132118071dSTrond Myklebust 
12142118071dSTrond Myklebust 	if (!(xprt = xprt_from_sock(sk)))
1215ea9afca8STrond Myklebust 		return;
12162118071dSTrond Myklebust 
12174f8943f8STrond Myklebust 	transport = container_of(xprt, struct sock_xprt, xprt);
1218af84537dSBenjamin Coddington 	transport->xprt_err = -sk->sk_err;
1219af84537dSBenjamin Coddington 	if (transport->xprt_err == 0)
1220ea9afca8STrond Myklebust 		return;
12212118071dSTrond Myklebust 	dprintk("RPC:       xs_error_report client %p, error=%d...\n",
1222af84537dSBenjamin Coddington 			xprt, -transport->xprt_err);
1223af84537dSBenjamin Coddington 	trace_rpc_socket_error(xprt, sk->sk_socket, transport->xprt_err);
1224af84537dSBenjamin Coddington 
1225af84537dSBenjamin Coddington 	/* barrier ensures xprt_err is set before XPRT_SOCK_WAKE_ERROR */
1226af84537dSBenjamin Coddington 	smp_mb__before_atomic();
12274f8943f8STrond Myklebust 	xs_run_error_worker(transport, XPRT_SOCK_WAKE_ERROR);
12282a9e1cfaSTrond Myklebust }
12292a9e1cfaSTrond Myklebust 
1230fe315e76SChuck Lever static void xs_reset_transport(struct sock_xprt *transport)
1231a246b010SChuck Lever {
1232ee0ac0c2SChuck Lever 	struct socket *sock = transport->sock;
1233ee0ac0c2SChuck Lever 	struct sock *sk = transport->inet;
12346cc7e908STrond Myklebust 	struct rpc_xprt *xprt = &transport->xprt;
1235a73881c9STrond Myklebust 	struct file *filp = transport->file;
1236a246b010SChuck Lever 
1237fe315e76SChuck Lever 	if (sk == NULL)
1238fe315e76SChuck Lever 		return;
1239f0043206STrond Myklebust 	/*
1240f0043206STrond Myklebust 	 * Make sure we're calling this in a context from which it is safe
1241f0043206STrond Myklebust 	 * to call __fput_sync(). In practice that means rpciod and the
1242f0043206STrond Myklebust 	 * system workqueue.
1243f0043206STrond Myklebust 	 */
1244f0043206STrond Myklebust 	if (!(current->flags & PF_WQ_WORKER)) {
1245f0043206STrond Myklebust 		WARN_ON_ONCE(1);
1246f0043206STrond Myklebust 		set_bit(XPRT_CLOSE_WAIT, &xprt->state);
1247f0043206STrond Myklebust 		return;
1248f0043206STrond Myklebust 	}
12499903cd1cSChuck Lever 
1250264d1df3SJeff Layton 	if (atomic_read(&transport->xprt.swapper))
1251264d1df3SJeff Layton 		sk_clear_memalloc(sk);
1252264d1df3SJeff Layton 
125375eb6af7SChuck Lever 	tls_handshake_cancel(sk);
125475eb6af7SChuck Lever 
125509939204STrond Myklebust 	kernel_sock_shutdown(sock, SHUT_RDWR);
125609939204STrond Myklebust 
1257edc1b01cSTrond Myklebust 	mutex_lock(&transport->recv_mutex);
1258ea9afca8STrond Myklebust 	lock_sock(sk);
1259ee0ac0c2SChuck Lever 	transport->inet = NULL;
1260ee0ac0c2SChuck Lever 	transport->sock = NULL;
1261a73881c9STrond Myklebust 	transport->file = NULL;
1262a246b010SChuck Lever 
1263a246b010SChuck Lever 	sk->sk_user_data = NULL;
12642a9e1cfaSTrond Myklebust 
12652a9e1cfaSTrond Myklebust 	xs_restore_old_callbacks(transport, sk);
12660c78789eSTrond Myklebust 	xprt_clear_connected(xprt);
12676cc7e908STrond Myklebust 	xs_sock_reset_connection_flags(xprt);
1268ae053551STrond Myklebust 	/* Reset stream record info */
1269ae053551STrond Myklebust 	xs_stream_reset_connect(transport);
1270ea9afca8STrond Myklebust 	release_sock(sk);
1271edc1b01cSTrond Myklebust 	mutex_unlock(&transport->recv_mutex);
1272a246b010SChuck Lever 
12736cc7e908STrond Myklebust 	trace_rpc_socket_close(xprt, sock);
1274f0043206STrond Myklebust 	__fput_sync(filp);
12750445f92cSTrond Myklebust 
12760445f92cSTrond Myklebust 	xprt_disconnect_done(xprt);
1277fe315e76SChuck Lever }
1278fe315e76SChuck Lever 
1279fe315e76SChuck Lever /**
1280fe315e76SChuck Lever  * xs_close - close a socket
1281fe315e76SChuck Lever  * @xprt: transport
1282fe315e76SChuck Lever  *
1283fe315e76SChuck Lever  * This is used when all requests are complete; ie, no DRC state remains
1284fe315e76SChuck Lever  * on the server we want to save.
1285f75e6745STrond Myklebust  *
1286f75e6745STrond Myklebust  * The caller _must_ be holding XPRT_LOCKED in order to avoid issues with
1287f75e6745STrond Myklebust  * xs_reset_transport() zeroing the socket from underneath a writer.
1288fe315e76SChuck Lever  */
1289fe315e76SChuck Lever static void xs_close(struct rpc_xprt *xprt)
1290fe315e76SChuck Lever {
1291fe315e76SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1292fe315e76SChuck Lever 
1293fe315e76SChuck Lever 	dprintk("RPC:       xs_close xprt %p\n", xprt);
1294fe315e76SChuck Lever 
1295fe315e76SChuck Lever 	xs_reset_transport(transport);
129661d0a8e6SNeil Brown 	xprt->reestablish_timeout = 0;
1297a246b010SChuck Lever }
1298a246b010SChuck Lever 
12994a068258SChuck Lever static void xs_inject_disconnect(struct rpc_xprt *xprt)
13004a068258SChuck Lever {
13014a068258SChuck Lever 	dprintk("RPC:       injecting transport disconnect on xprt=%p\n",
13024a068258SChuck Lever 		xprt);
13034a068258SChuck Lever 	xprt_disconnect_done(xprt);
13044a068258SChuck Lever }
13054a068258SChuck Lever 
1306315f3812SKinglong Mee static void xs_xprt_free(struct rpc_xprt *xprt)
1307315f3812SKinglong Mee {
1308315f3812SKinglong Mee 	xs_free_peer_addresses(xprt);
1309315f3812SKinglong Mee 	xprt_free(xprt);
1310315f3812SKinglong Mee }
1311315f3812SKinglong Mee 
13129903cd1cSChuck Lever /**
13139903cd1cSChuck Lever  * xs_destroy - prepare to shutdown a transport
13149903cd1cSChuck Lever  * @xprt: doomed transport
13159903cd1cSChuck Lever  *
13169903cd1cSChuck Lever  */
13179903cd1cSChuck Lever static void xs_destroy(struct rpc_xprt *xprt)
1318a246b010SChuck Lever {
131903c78827STrond Myklebust 	struct sock_xprt *transport = container_of(xprt,
132003c78827STrond Myklebust 			struct sock_xprt, xprt);
13219903cd1cSChuck Lever 	dprintk("RPC:       xs_destroy xprt %p\n", xprt);
13229903cd1cSChuck Lever 
132303c78827STrond Myklebust 	cancel_delayed_work_sync(&transport->connect_worker);
1324a1311d87STrond Myklebust 	xs_close(xprt);
1325edc1b01cSTrond Myklebust 	cancel_work_sync(&transport->recv_worker);
1326b5e92419STrond Myklebust 	cancel_work_sync(&transport->error_worker);
1327315f3812SKinglong Mee 	xs_xprt_free(xprt);
1328a1311d87STrond Myklebust 	module_put(THIS_MODULE);
1329a246b010SChuck Lever }
1330a246b010SChuck Lever 
13319903cd1cSChuck Lever /**
1332f9b2ee71STrond Myklebust  * xs_udp_data_read_skb - receive callback for UDP sockets
1333f9b2ee71STrond Myklebust  * @xprt: transport
1334f9b2ee71STrond Myklebust  * @sk: socket
1335f9b2ee71STrond Myklebust  * @skb: skbuff
13369903cd1cSChuck Lever  *
1337a246b010SChuck Lever  */
1338f9b2ee71STrond Myklebust static void xs_udp_data_read_skb(struct rpc_xprt *xprt,
1339f9b2ee71STrond Myklebust 		struct sock *sk,
1340f9b2ee71STrond Myklebust 		struct sk_buff *skb)
1341a246b010SChuck Lever {
1342a246b010SChuck Lever 	struct rpc_task *task;
1343a246b010SChuck Lever 	struct rpc_rqst *rovr;
1344f9b2ee71STrond Myklebust 	int repsize, copied;
1345d8ed029dSAlexey Dobriyan 	u32 _xid;
1346d8ed029dSAlexey Dobriyan 	__be32 *xp;
1347a246b010SChuck Lever 
13481da8c681SWillem de Bruijn 	repsize = skb->len;
1349a246b010SChuck Lever 	if (repsize < 4) {
13509903cd1cSChuck Lever 		dprintk("RPC:       impossible RPC reply size %d!\n", repsize);
1351f9b2ee71STrond Myklebust 		return;
1352a246b010SChuck Lever 	}
1353a246b010SChuck Lever 
1354a246b010SChuck Lever 	/* Copy the XID from the skb... */
13551da8c681SWillem de Bruijn 	xp = skb_header_pointer(skb, 0, sizeof(_xid), &_xid);
1356a246b010SChuck Lever 	if (xp == NULL)
1357f9b2ee71STrond Myklebust 		return;
1358a246b010SChuck Lever 
1359a246b010SChuck Lever 	/* Look up and lock the request corresponding to the given XID */
136075c84151STrond Myklebust 	spin_lock(&xprt->queue_lock);
1361a246b010SChuck Lever 	rovr = xprt_lookup_rqst(xprt, *xp);
1362a246b010SChuck Lever 	if (!rovr)
1363a246b010SChuck Lever 		goto out_unlock;
1364729749bbSTrond Myklebust 	xprt_pin_rqst(rovr);
1365ecd465eeSChuck Lever 	xprt_update_rtt(rovr->rq_task);
136675c84151STrond Myklebust 	spin_unlock(&xprt->queue_lock);
1367a246b010SChuck Lever 	task = rovr->rq_task;
1368a246b010SChuck Lever 
1369a246b010SChuck Lever 	if ((copied = rovr->rq_private_buf.buflen) > repsize)
1370a246b010SChuck Lever 		copied = repsize;
1371a246b010SChuck Lever 
1372a246b010SChuck Lever 	/* Suck it into the iovec, verify checksum if not done by hw. */
13731781f7f5SHerbert Xu 	if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) {
137475c84151STrond Myklebust 		spin_lock(&xprt->queue_lock);
13750afa6b44STrond Myklebust 		__UDPX_INC_STATS(sk, UDP_MIB_INERRORS);
1376729749bbSTrond Myklebust 		goto out_unpin;
13771781f7f5SHerbert Xu 	}
13781781f7f5SHerbert Xu 
1379a246b010SChuck Lever 
1380b5e92419STrond Myklebust 	spin_lock(&xprt->transport_lock);
13816a24dfb6STrond Myklebust 	xprt_adjust_cwnd(xprt, task, copied);
1382b5e92419STrond Myklebust 	spin_unlock(&xprt->transport_lock);
138375c84151STrond Myklebust 	spin_lock(&xprt->queue_lock);
13841570c1e4SChuck Lever 	xprt_complete_rqst(task, copied);
13850afa6b44STrond Myklebust 	__UDPX_INC_STATS(sk, UDP_MIB_INDATAGRAMS);
1386729749bbSTrond Myklebust out_unpin:
1387729749bbSTrond Myklebust 	xprt_unpin_rqst(rovr);
1388a246b010SChuck Lever  out_unlock:
138975c84151STrond Myklebust 	spin_unlock(&xprt->queue_lock);
1390f9b2ee71STrond Myklebust }
1391f9b2ee71STrond Myklebust 
1392f9b2ee71STrond Myklebust static void xs_udp_data_receive(struct sock_xprt *transport)
1393f9b2ee71STrond Myklebust {
1394f9b2ee71STrond Myklebust 	struct sk_buff *skb;
1395f9b2ee71STrond Myklebust 	struct sock *sk;
1396f9b2ee71STrond Myklebust 	int err;
1397f9b2ee71STrond Myklebust 
1398f9b2ee71STrond Myklebust 	mutex_lock(&transport->recv_mutex);
1399f9b2ee71STrond Myklebust 	sk = transport->inet;
1400f9b2ee71STrond Myklebust 	if (sk == NULL)
1401f9b2ee71STrond Myklebust 		goto out;
1402f9b2ee71STrond Myklebust 	for (;;) {
1403ec095263SOliver Hartkopp 		skb = skb_recv_udp(sk, MSG_DONTWAIT, &err);
14044f546149STrond Myklebust 		if (skb == NULL)
14054f546149STrond Myklebust 			break;
1406f9b2ee71STrond Myklebust 		xs_udp_data_read_skb(&transport->xprt, sk, skb);
1407850cbaddSPaolo Abeni 		consume_skb(skb);
14080af3442aSTrond Myklebust 		cond_resched();
1409f9b2ee71STrond Myklebust 	}
14100ffe86f4STrond Myklebust 	xs_poll_check_readable(transport);
1411a246b010SChuck Lever out:
1412f9b2ee71STrond Myklebust 	mutex_unlock(&transport->recv_mutex);
1413f9b2ee71STrond Myklebust }
1414f9b2ee71STrond Myklebust 
1415f9b2ee71STrond Myklebust static void xs_udp_data_receive_workfn(struct work_struct *work)
1416f9b2ee71STrond Myklebust {
1417f9b2ee71STrond Myklebust 	struct sock_xprt *transport =
1418f9b2ee71STrond Myklebust 		container_of(work, struct sock_xprt, recv_worker);
1419a1231fdaSTrond Myklebust 	unsigned int pflags = memalloc_nofs_save();
1420a1231fdaSTrond Myklebust 
1421f9b2ee71STrond Myklebust 	xs_udp_data_receive(transport);
1422a1231fdaSTrond Myklebust 	memalloc_nofs_restore(pflags);
1423f9b2ee71STrond Myklebust }
1424f9b2ee71STrond Myklebust 
1425f9b2ee71STrond Myklebust /**
1426f67939e4SChuck Lever  * xs_data_ready - "data ready" callback for sockets
1427f9b2ee71STrond Myklebust  * @sk: socket with data to read
1428f9b2ee71STrond Myklebust  *
1429f9b2ee71STrond Myklebust  */
1430f9b2ee71STrond Myklebust static void xs_data_ready(struct sock *sk)
1431f9b2ee71STrond Myklebust {
1432f9b2ee71STrond Myklebust 	struct rpc_xprt *xprt;
1433f9b2ee71STrond Myklebust 
143440e0b090SPeilin Ye 	trace_sk_data_ready(sk);
143540e0b090SPeilin Ye 
1436f9b2ee71STrond Myklebust 	xprt = xprt_from_sock(sk);
1437f9b2ee71STrond Myklebust 	if (xprt != NULL) {
1438f9b2ee71STrond Myklebust 		struct sock_xprt *transport = container_of(xprt,
1439f9b2ee71STrond Myklebust 				struct sock_xprt, xprt);
1440f67939e4SChuck Lever 
1441f67939e4SChuck Lever 		trace_xs_data_ready(xprt);
1442f67939e4SChuck Lever 
14435157b956STrond Myklebust 		transport->old_data_ready(sk);
14440d3ca07fSChuck Lever 
14450d3ca07fSChuck Lever 		if (test_bit(XPRT_SOCK_IGNORE_RECV, &transport->sock_state))
14460d3ca07fSChuck Lever 			return;
14470d3ca07fSChuck Lever 
14485157b956STrond Myklebust 		/* Any data means we had a useful conversation, so
14495157b956STrond Myklebust 		 * then we don't need to delay the next reconnect
14505157b956STrond Myklebust 		 */
14515157b956STrond Myklebust 		if (xprt->reestablish_timeout)
14525157b956STrond Myklebust 			xprt->reestablish_timeout = 0;
145342d42a5bSTrond Myklebust 		if (!test_and_set_bit(XPRT_SOCK_DATA_READY, &transport->sock_state))
145440a5f1b1STrond Myklebust 			queue_work(xprtiod_workqueue, &transport->recv_worker);
1455f9b2ee71STrond Myklebust 	}
1456a246b010SChuck Lever }
1457a246b010SChuck Lever 
1458a519fc7aSTrond Myklebust /*
1459a519fc7aSTrond Myklebust  * Helper function to force a TCP close if the server is sending
1460a519fc7aSTrond Myklebust  * junk and/or it has put us in CLOSE_WAIT
1461a519fc7aSTrond Myklebust  */
1462a519fc7aSTrond Myklebust static void xs_tcp_force_close(struct rpc_xprt *xprt)
1463a519fc7aSTrond Myklebust {
1464a519fc7aSTrond Myklebust 	xprt_force_disconnect(xprt);
1465a519fc7aSTrond Myklebust }
1466a519fc7aSTrond Myklebust 
14679e00abc3STrond Myklebust #if defined(CONFIG_SUNRPC_BACKCHANNEL)
14686b26cc8cSChuck Lever static size_t xs_tcp_bc_maxpayload(struct rpc_xprt *xprt)
14696b26cc8cSChuck Lever {
14706b26cc8cSChuck Lever 	return PAGE_SIZE;
14716b26cc8cSChuck Lever }
14729e00abc3STrond Myklebust #endif /* CONFIG_SUNRPC_BACKCHANNEL */
147344b98efdSRicardo Labiaga 
14749903cd1cSChuck Lever /**
1475efce2d0bSTrond Myklebust  * xs_local_state_change - callback to handle AF_LOCAL socket state changes
1476efce2d0bSTrond Myklebust  * @sk: socket whose state has changed
1477efce2d0bSTrond Myklebust  *
1478efce2d0bSTrond Myklebust  */
1479efce2d0bSTrond Myklebust static void xs_local_state_change(struct sock *sk)
1480efce2d0bSTrond Myklebust {
1481efce2d0bSTrond Myklebust 	struct rpc_xprt *xprt;
1482efce2d0bSTrond Myklebust 	struct sock_xprt *transport;
1483efce2d0bSTrond Myklebust 
1484efce2d0bSTrond Myklebust 	if (!(xprt = xprt_from_sock(sk)))
1485efce2d0bSTrond Myklebust 		return;
1486efce2d0bSTrond Myklebust 	transport = container_of(xprt, struct sock_xprt, xprt);
1487efce2d0bSTrond Myklebust 	if (sk->sk_shutdown & SHUTDOWN_MASK) {
1488efce2d0bSTrond Myklebust 		clear_bit(XPRT_CONNECTED, &xprt->state);
1489efce2d0bSTrond Myklebust 		/* Trigger the socket release */
1490efce2d0bSTrond Myklebust 		xs_run_error_worker(transport, XPRT_SOCK_WAKE_DISCONNECT);
1491efce2d0bSTrond Myklebust 	}
1492efce2d0bSTrond Myklebust }
1493efce2d0bSTrond Myklebust 
1494efce2d0bSTrond Myklebust /**
14959903cd1cSChuck Lever  * xs_tcp_state_change - callback to handle TCP socket state changes
14969903cd1cSChuck Lever  * @sk: socket whose state has changed
14979903cd1cSChuck Lever  *
14989903cd1cSChuck Lever  */
14999903cd1cSChuck Lever static void xs_tcp_state_change(struct sock *sk)
1500a246b010SChuck Lever {
1501a246b010SChuck Lever 	struct rpc_xprt *xprt;
15020fdea1e8STrond Myklebust 	struct sock_xprt *transport;
1503a246b010SChuck Lever 
1504a246b010SChuck Lever 	if (!(xprt = xprt_from_sock(sk)))
1505ea9afca8STrond Myklebust 		return;
15069903cd1cSChuck Lever 	dprintk("RPC:       xs_tcp_state_change client %p...\n", xprt);
1507669502ffSAndy Chittenden 	dprintk("RPC:       state %x conn %d dead %d zapped %d sk_shutdown %d\n",
1508a246b010SChuck Lever 			sk->sk_state, xprt_connected(xprt),
1509a246b010SChuck Lever 			sock_flag(sk, SOCK_DEAD),
1510669502ffSAndy Chittenden 			sock_flag(sk, SOCK_ZAPPED),
1511669502ffSAndy Chittenden 			sk->sk_shutdown);
1512a246b010SChuck Lever 
15130fdea1e8STrond Myklebust 	transport = container_of(xprt, struct sock_xprt, xprt);
151440b5ea0cSTrond Myklebust 	trace_rpc_socket_state_change(xprt, sk->sk_socket);
1515a246b010SChuck Lever 	switch (sk->sk_state) {
1516a246b010SChuck Lever 	case TCP_ESTABLISHED:
1517a246b010SChuck Lever 		if (!xprt_test_and_set_connected(xprt)) {
15188b71798cSTrond Myklebust 			xprt->connect_cookie++;
15190fdea1e8STrond Myklebust 			clear_bit(XPRT_SOCK_CONNECTING, &transport->sock_state);
15200fdea1e8STrond Myklebust 			xprt_clear_connecting(xprt);
152151971139SChuck Lever 
15223968a8a5SChuck Lever 			xprt->stat.connect_count++;
15233968a8a5SChuck Lever 			xprt->stat.connect_time += (long)jiffies -
15243968a8a5SChuck Lever 						   xprt->stat.connect_start;
15254f8943f8STrond Myklebust 			xs_run_error_worker(transport, XPRT_SOCK_WAKE_PENDING);
1526a246b010SChuck Lever 		}
1527a246b010SChuck Lever 		break;
15283b948ae5STrond Myklebust 	case TCP_FIN_WAIT1:
15293b948ae5STrond Myklebust 		/* The client initiated a shutdown of the socket */
15307c1d71cfSTrond Myklebust 		xprt->connect_cookie++;
1531663b8858STrond Myklebust 		xprt->reestablish_timeout = 0;
15323b948ae5STrond Myklebust 		set_bit(XPRT_CLOSING, &xprt->state);
15334e857c58SPeter Zijlstra 		smp_mb__before_atomic();
15343b948ae5STrond Myklebust 		clear_bit(XPRT_CONNECTED, &xprt->state);
1535ef803670STrond Myklebust 		clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
15364e857c58SPeter Zijlstra 		smp_mb__after_atomic();
1537a246b010SChuck Lever 		break;
1538632e3bdcSTrond Myklebust 	case TCP_CLOSE_WAIT:
15393b948ae5STrond Myklebust 		/* The server initiated a shutdown of the socket */
15407c1d71cfSTrond Myklebust 		xprt->connect_cookie++;
1541d0bea455STrond Myklebust 		clear_bit(XPRT_CONNECTED, &xprt->state);
15424f8943f8STrond Myklebust 		xs_run_error_worker(transport, XPRT_SOCK_WAKE_DISCONNECT);
1543df561f66SGustavo A. R. Silva 		fallthrough;
1544663b8858STrond Myklebust 	case TCP_CLOSING:
1545663b8858STrond Myklebust 		/*
1546663b8858STrond Myklebust 		 * If the server closed down the connection, make sure that
1547663b8858STrond Myklebust 		 * we back off before reconnecting
1548663b8858STrond Myklebust 		 */
1549663b8858STrond Myklebust 		if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
1550663b8858STrond Myklebust 			xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
15513b948ae5STrond Myklebust 		break;
15523b948ae5STrond Myklebust 	case TCP_LAST_ACK:
1553670f9457STrond Myklebust 		set_bit(XPRT_CLOSING, &xprt->state);
15544e857c58SPeter Zijlstra 		smp_mb__before_atomic();
15553b948ae5STrond Myklebust 		clear_bit(XPRT_CONNECTED, &xprt->state);
15564e857c58SPeter Zijlstra 		smp_mb__after_atomic();
15573b948ae5STrond Myklebust 		break;
15583b948ae5STrond Myklebust 	case TCP_CLOSE:
15590fdea1e8STrond Myklebust 		if (test_and_clear_bit(XPRT_SOCK_CONNECTING,
15600fdea1e8STrond Myklebust 					&transport->sock_state))
15610fdea1e8STrond Myklebust 			xprt_clear_connecting(xprt);
15629b30889cSTrond Myklebust 		clear_bit(XPRT_CLOSING, &xprt->state);
15639b30889cSTrond Myklebust 		/* Trigger the socket release */
15644f8943f8STrond Myklebust 		xs_run_error_worker(transport, XPRT_SOCK_WAKE_DISCONNECT);
1565a246b010SChuck Lever 	}
1566a246b010SChuck Lever }
1567a246b010SChuck Lever 
15681f0fa154SIlpo Järvinen static void xs_write_space(struct sock *sk)
15691f0fa154SIlpo Järvinen {
15704f8943f8STrond Myklebust 	struct sock_xprt *transport;
15711f0fa154SIlpo Järvinen 	struct rpc_xprt *xprt;
15721f0fa154SIlpo Järvinen 
157313331a55STrond Myklebust 	if (!sk->sk_socket)
15741f0fa154SIlpo Järvinen 		return;
157513331a55STrond Myklebust 	clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
15761f0fa154SIlpo Järvinen 
15771f0fa154SIlpo Järvinen 	if (unlikely(!(xprt = xprt_from_sock(sk))))
15781f0fa154SIlpo Järvinen 		return;
15794f8943f8STrond Myklebust 	transport = container_of(xprt, struct sock_xprt, xprt);
15802790a624STrond Myklebust 	if (!test_and_clear_bit(XPRT_SOCK_NOSPACE, &transport->sock_state))
15812790a624STrond Myklebust 		return;
15824f8943f8STrond Myklebust 	xs_run_error_worker(transport, XPRT_SOCK_WAKE_WRITE);
1583c544577dSTrond Myklebust 	sk->sk_write_pending--;
15841f0fa154SIlpo Järvinen }
15851f0fa154SIlpo Järvinen 
15862a9e1cfaSTrond Myklebust /**
1587c7b2cae8SChuck Lever  * xs_udp_write_space - callback invoked when socket buffer space
1588c7b2cae8SChuck Lever  *                             becomes available
15899903cd1cSChuck Lever  * @sk: socket whose state has changed
15909903cd1cSChuck Lever  *
1591a246b010SChuck Lever  * Called when more output buffer space is available for this socket.
1592a246b010SChuck Lever  * We try not to wake our writers until they can make "significant"
1593c7b2cae8SChuck Lever  * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1594a246b010SChuck Lever  * with a bunch of small requests.
1595a246b010SChuck Lever  */
1596c7b2cae8SChuck Lever static void xs_udp_write_space(struct sock *sk)
1597a246b010SChuck Lever {
1598c7b2cae8SChuck Lever 	/* from net/core/sock.c:sock_def_write_space */
15991f0fa154SIlpo Järvinen 	if (sock_writeable(sk))
16001f0fa154SIlpo Järvinen 		xs_write_space(sk);
1601c7b2cae8SChuck Lever }
1602c7b2cae8SChuck Lever 
1603c7b2cae8SChuck Lever /**
1604c7b2cae8SChuck Lever  * xs_tcp_write_space - callback invoked when socket buffer space
1605c7b2cae8SChuck Lever  *                             becomes available
1606c7b2cae8SChuck Lever  * @sk: socket whose state has changed
1607c7b2cae8SChuck Lever  *
1608c7b2cae8SChuck Lever  * Called when more output buffer space is available for this socket.
1609c7b2cae8SChuck Lever  * We try not to wake our writers until they can make "significant"
1610c7b2cae8SChuck Lever  * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1611c7b2cae8SChuck Lever  * with a bunch of small requests.
1612c7b2cae8SChuck Lever  */
1613c7b2cae8SChuck Lever static void xs_tcp_write_space(struct sock *sk)
1614c7b2cae8SChuck Lever {
1615c7b2cae8SChuck Lever 	/* from net/core/stream.c:sk_stream_write_space */
161664dc6130SEric Dumazet 	if (sk_stream_is_writeable(sk))
16171f0fa154SIlpo Järvinen 		xs_write_space(sk);
1618a246b010SChuck Lever }
1619a246b010SChuck Lever 
1620470056c2SChuck Lever static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt)
1621a246b010SChuck Lever {
1622ee0ac0c2SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1623ee0ac0c2SChuck Lever 	struct sock *sk = transport->inet;
1624a246b010SChuck Lever 
16257c6e066eSChuck Lever 	if (transport->rcvsize) {
1626a246b010SChuck Lever 		sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
16277c6e066eSChuck Lever 		sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2;
1628a246b010SChuck Lever 	}
16297c6e066eSChuck Lever 	if (transport->sndsize) {
1630a246b010SChuck Lever 		sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
16317c6e066eSChuck Lever 		sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2;
1632a246b010SChuck Lever 		sk->sk_write_space(sk);
1633a246b010SChuck Lever 	}
1634a246b010SChuck Lever }
1635a246b010SChuck Lever 
163643118c29SChuck Lever /**
1637470056c2SChuck Lever  * xs_udp_set_buffer_size - set send and receive limits
163843118c29SChuck Lever  * @xprt: generic transport
1639470056c2SChuck Lever  * @sndsize: requested size of send buffer, in bytes
1640470056c2SChuck Lever  * @rcvsize: requested size of receive buffer, in bytes
164143118c29SChuck Lever  *
1642470056c2SChuck Lever  * Set socket send and receive buffer size limits.
164343118c29SChuck Lever  */
1644470056c2SChuck Lever static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize)
164543118c29SChuck Lever {
16467c6e066eSChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
16477c6e066eSChuck Lever 
16487c6e066eSChuck Lever 	transport->sndsize = 0;
1649470056c2SChuck Lever 	if (sndsize)
16507c6e066eSChuck Lever 		transport->sndsize = sndsize + 1024;
16517c6e066eSChuck Lever 	transport->rcvsize = 0;
1652470056c2SChuck Lever 	if (rcvsize)
16537c6e066eSChuck Lever 		transport->rcvsize = rcvsize + 1024;
1654470056c2SChuck Lever 
1655470056c2SChuck Lever 	xs_udp_do_set_buffer_size(xprt);
165643118c29SChuck Lever }
165743118c29SChuck Lever 
165846c0ee8bSChuck Lever /**
165946c0ee8bSChuck Lever  * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
1660acf0a39fSChuck Lever  * @xprt: controlling transport
166146c0ee8bSChuck Lever  * @task: task that timed out
166246c0ee8bSChuck Lever  *
166346c0ee8bSChuck Lever  * Adjust the congestion window after a retransmit timeout has occurred.
166446c0ee8bSChuck Lever  */
16656a24dfb6STrond Myklebust static void xs_udp_timer(struct rpc_xprt *xprt, struct rpc_task *task)
166646c0ee8bSChuck Lever {
1667b5e92419STrond Myklebust 	spin_lock(&xprt->transport_lock);
16686a24dfb6STrond Myklebust 	xprt_adjust_cwnd(xprt, task, -ETIMEDOUT);
1669b5e92419STrond Myklebust 	spin_unlock(&xprt->transport_lock);
167046c0ee8bSChuck Lever }
167146c0ee8bSChuck Lever 
1672826799e6SJ. Bruce Fields static int xs_get_random_port(void)
1673b85d8806SChuck Lever {
1674826799e6SJ. Bruce Fields 	unsigned short min = xprt_min_resvport, max = xprt_max_resvport;
1675826799e6SJ. Bruce Fields 	unsigned short range;
1676826799e6SJ. Bruce Fields 	unsigned short rand;
1677826799e6SJ. Bruce Fields 
1678826799e6SJ. Bruce Fields 	if (max < min)
1679826799e6SJ. Bruce Fields 		return -EADDRINUSE;
1680826799e6SJ. Bruce Fields 	range = max - min + 1;
16818032bf12SJason A. Donenfeld 	rand = get_random_u32_below(range);
1682826799e6SJ. Bruce Fields 	return rand + min;
1683b85d8806SChuck Lever }
1684b85d8806SChuck Lever 
16854dda9c8aSTrond Myklebust static unsigned short xs_sock_getport(struct socket *sock)
16864dda9c8aSTrond Myklebust {
16874dda9c8aSTrond Myklebust 	struct sockaddr_storage buf;
16884dda9c8aSTrond Myklebust 	unsigned short port = 0;
16894dda9c8aSTrond Myklebust 
16909b2c45d4SDenys Vlasenko 	if (kernel_getsockname(sock, (struct sockaddr *)&buf) < 0)
16914dda9c8aSTrond Myklebust 		goto out;
16924dda9c8aSTrond Myklebust 	switch (buf.ss_family) {
16934dda9c8aSTrond Myklebust 	case AF_INET6:
16944dda9c8aSTrond Myklebust 		port = ntohs(((struct sockaddr_in6 *)&buf)->sin6_port);
16954dda9c8aSTrond Myklebust 		break;
16964dda9c8aSTrond Myklebust 	case AF_INET:
16974dda9c8aSTrond Myklebust 		port = ntohs(((struct sockaddr_in *)&buf)->sin_port);
16984dda9c8aSTrond Myklebust 	}
16994dda9c8aSTrond Myklebust out:
17004dda9c8aSTrond Myklebust 	return port;
17014dda9c8aSTrond Myklebust }
17024dda9c8aSTrond Myklebust 
17034dda9c8aSTrond Myklebust /**
170492200412SChuck Lever  * xs_set_port - reset the port number in the remote endpoint address
170592200412SChuck Lever  * @xprt: generic transport
170692200412SChuck Lever  * @port: new port number
170792200412SChuck Lever  *
170892200412SChuck Lever  */
170992200412SChuck Lever static void xs_set_port(struct rpc_xprt *xprt, unsigned short port)
171092200412SChuck Lever {
171192200412SChuck Lever 	dprintk("RPC:       setting port for xprt %p to %u\n", xprt, port);
1712c4efcb1dSChuck Lever 
17139dc3b095SChuck Lever 	rpc_set_port(xs_addr(xprt), port);
17149dc3b095SChuck Lever 	xs_update_peer_port(xprt);
171592200412SChuck Lever }
171692200412SChuck Lever 
17174dda9c8aSTrond Myklebust static void xs_set_srcport(struct sock_xprt *transport, struct socket *sock)
17184dda9c8aSTrond Myklebust {
1719e6237b6fSTrond Myklebust 	if (transport->srcport == 0 && transport->xprt.reuseport)
17204dda9c8aSTrond Myklebust 		transport->srcport = xs_sock_getport(sock);
17214dda9c8aSTrond Myklebust }
17224dda9c8aSTrond Myklebust 
1723826799e6SJ. Bruce Fields static int xs_get_srcport(struct sock_xprt *transport)
172467a391d7STrond Myklebust {
1725826799e6SJ. Bruce Fields 	int port = transport->srcport;
172667a391d7STrond Myklebust 
172767a391d7STrond Myklebust 	if (port == 0 && transport->xprt.resvport)
172867a391d7STrond Myklebust 		port = xs_get_random_port();
172967a391d7STrond Myklebust 	return port;
173067a391d7STrond Myklebust }
173167a391d7STrond Myklebust 
1732421ab1beSTrond Myklebust static unsigned short xs_sock_srcport(struct rpc_xprt *xprt)
1733a8482488SOlga Kornievskaia {
1734a8482488SOlga Kornievskaia 	struct sock_xprt *sock = container_of(xprt, struct sock_xprt, xprt);
1735b49ea673SNeilBrown 	unsigned short ret = 0;
1736b49ea673SNeilBrown 	mutex_lock(&sock->recv_mutex);
1737b49ea673SNeilBrown 	if (sock->sock)
1738b49ea673SNeilBrown 		ret = xs_sock_getport(sock->sock);
1739b49ea673SNeilBrown 	mutex_unlock(&sock->recv_mutex);
1740b49ea673SNeilBrown 	return ret;
1741a8482488SOlga Kornievskaia }
1742421ab1beSTrond Myklebust 
1743421ab1beSTrond Myklebust static int xs_sock_srcaddr(struct rpc_xprt *xprt, char *buf, size_t buflen)
1744421ab1beSTrond Myklebust {
1745421ab1beSTrond Myklebust 	struct sock_xprt *sock = container_of(xprt, struct sock_xprt, xprt);
1746421ab1beSTrond Myklebust 	union {
1747421ab1beSTrond Myklebust 		struct sockaddr sa;
1748421ab1beSTrond Myklebust 		struct sockaddr_storage st;
1749421ab1beSTrond Myklebust 	} saddr;
1750421ab1beSTrond Myklebust 	int ret = -ENOTCONN;
1751421ab1beSTrond Myklebust 
1752421ab1beSTrond Myklebust 	mutex_lock(&sock->recv_mutex);
1753421ab1beSTrond Myklebust 	if (sock->sock) {
1754421ab1beSTrond Myklebust 		ret = kernel_getsockname(sock->sock, &saddr.sa);
1755421ab1beSTrond Myklebust 		if (ret >= 0)
1756421ab1beSTrond Myklebust 			ret = snprintf(buf, buflen, "%pISc", &saddr.sa);
1757421ab1beSTrond Myklebust 	}
1758421ab1beSTrond Myklebust 	mutex_unlock(&sock->recv_mutex);
1759421ab1beSTrond Myklebust 	return ret;
1760421ab1beSTrond Myklebust }
1761a8482488SOlga Kornievskaia 
1762baaf4e48SPavel Emelyanov static unsigned short xs_next_srcport(struct sock_xprt *transport, unsigned short port)
176367a391d7STrond Myklebust {
1764fbfffbd5SChuck Lever 	if (transport->srcport != 0)
1765fbfffbd5SChuck Lever 		transport->srcport = 0;
176667a391d7STrond Myklebust 	if (!transport->xprt.resvport)
176767a391d7STrond Myklebust 		return 0;
176867a391d7STrond Myklebust 	if (port <= xprt_min_resvport || port > xprt_max_resvport)
176967a391d7STrond Myklebust 		return xprt_max_resvport;
177067a391d7STrond Myklebust 	return --port;
177167a391d7STrond Myklebust }
1772beb59b68SPavel Emelyanov static int xs_bind(struct sock_xprt *transport, struct socket *sock)
1773a246b010SChuck Lever {
1774beb59b68SPavel Emelyanov 	struct sockaddr_storage myaddr;
177567a391d7STrond Myklebust 	int err, nloop = 0;
1776826799e6SJ. Bruce Fields 	int port = xs_get_srcport(transport);
177767a391d7STrond Myklebust 	unsigned short last;
1778a246b010SChuck Lever 
17790f7a622cSChris Perl 	/*
17800f7a622cSChris Perl 	 * If we are asking for any ephemeral port (i.e. port == 0 &&
17810f7a622cSChris Perl 	 * transport->xprt.resvport == 0), don't bind.  Let the local
17820f7a622cSChris Perl 	 * port selection happen implicitly when the socket is used
17830f7a622cSChris Perl 	 * (for example at connect time).
17840f7a622cSChris Perl 	 *
17850f7a622cSChris Perl 	 * This ensures that we can continue to establish TCP
17860f7a622cSChris Perl 	 * connections even when all local ephemeral ports are already
17870f7a622cSChris Perl 	 * a part of some TCP connection.  This makes no difference
178812b20ce3SBhaskar Chowdhury 	 * for UDP sockets, but also doesn't harm them.
17890f7a622cSChris Perl 	 *
17900f7a622cSChris Perl 	 * If we're asking for any reserved port (i.e. port == 0 &&
17910f7a622cSChris Perl 	 * transport->xprt.resvport == 1) xs_get_srcport above will
17920f7a622cSChris Perl 	 * ensure that port is non-zero and we will bind as needed.
17930f7a622cSChris Perl 	 */
1794826799e6SJ. Bruce Fields 	if (port <= 0)
1795826799e6SJ. Bruce Fields 		return port;
17960f7a622cSChris Perl 
1797beb59b68SPavel Emelyanov 	memcpy(&myaddr, &transport->srcaddr, transport->xprt.addrlen);
1798a246b010SChuck Lever 	do {
1799beb59b68SPavel Emelyanov 		rpc_set_port((struct sockaddr *)&myaddr, port);
1800e6242e92SSridhar Samudrala 		err = kernel_bind(sock, (struct sockaddr *)&myaddr,
1801beb59b68SPavel Emelyanov 				transport->xprt.addrlen);
1802a246b010SChuck Lever 		if (err == 0) {
1803bc1c56e9SNeilBrown 			if (transport->xprt.reuseport)
1804fbfffbd5SChuck Lever 				transport->srcport = port;
1805d3bc9a1dSFrank van Maarseveen 			break;
1806a246b010SChuck Lever 		}
180767a391d7STrond Myklebust 		last = port;
1808baaf4e48SPavel Emelyanov 		port = xs_next_srcport(transport, port);
180967a391d7STrond Myklebust 		if (port > last)
181067a391d7STrond Myklebust 			nloop++;
181167a391d7STrond Myklebust 	} while (err == -EADDRINUSE && nloop != 2);
1812beb59b68SPavel Emelyanov 
18134232e863SChuck Lever 	if (myaddr.ss_family == AF_INET)
1814beb59b68SPavel Emelyanov 		dprintk("RPC:       %s %pI4:%u: %s (%d)\n", __func__,
1815beb59b68SPavel Emelyanov 				&((struct sockaddr_in *)&myaddr)->sin_addr,
1816beb59b68SPavel Emelyanov 				port, err ? "failed" : "ok", err);
1817beb59b68SPavel Emelyanov 	else
1818beb59b68SPavel Emelyanov 		dprintk("RPC:       %s %pI6:%u: %s (%d)\n", __func__,
1819beb59b68SPavel Emelyanov 				&((struct sockaddr_in6 *)&myaddr)->sin6_addr,
18207dc753f0SChuck Lever 				port, err ? "failed" : "ok", err);
1821a246b010SChuck Lever 	return err;
1822a246b010SChuck Lever }
1823a246b010SChuck Lever 
1824176e21eeSChuck Lever /*
1825176e21eeSChuck Lever  * We don't support autobind on AF_LOCAL sockets
1826176e21eeSChuck Lever  */
1827176e21eeSChuck Lever static void xs_local_rpcbind(struct rpc_task *task)
1828176e21eeSChuck Lever {
1829fb43d172STrond Myklebust 	xprt_set_bound(task->tk_xprt);
1830176e21eeSChuck Lever }
1831176e21eeSChuck Lever 
1832176e21eeSChuck Lever static void xs_local_set_port(struct rpc_xprt *xprt, unsigned short port)
1833176e21eeSChuck Lever {
1834176e21eeSChuck Lever }
1835a246b010SChuck Lever 
1836ed07536eSPeter Zijlstra #ifdef CONFIG_DEBUG_LOCK_ALLOC
1837064a9177SNeilBrown static struct lock_class_key xs_key[3];
1838064a9177SNeilBrown static struct lock_class_key xs_slock_key[3];
1839ed07536eSPeter Zijlstra 
1840176e21eeSChuck Lever static inline void xs_reclassify_socketu(struct socket *sock)
1841176e21eeSChuck Lever {
1842176e21eeSChuck Lever 	struct sock *sk = sock->sk;
1843176e21eeSChuck Lever 
1844176e21eeSChuck Lever 	sock_lock_init_class_and_name(sk, "slock-AF_LOCAL-RPC",
1845064a9177SNeilBrown 		&xs_slock_key[0], "sk_lock-AF_LOCAL-RPC", &xs_key[0]);
1846176e21eeSChuck Lever }
1847176e21eeSChuck Lever 
18488945ee5eSChuck Lever static inline void xs_reclassify_socket4(struct socket *sock)
1849ed07536eSPeter Zijlstra {
1850ed07536eSPeter Zijlstra 	struct sock *sk = sock->sk;
18518945ee5eSChuck Lever 
18528945ee5eSChuck Lever 	sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC",
1853064a9177SNeilBrown 		&xs_slock_key[1], "sk_lock-AF_INET-RPC", &xs_key[1]);
1854ed07536eSPeter Zijlstra }
18558945ee5eSChuck Lever 
18568945ee5eSChuck Lever static inline void xs_reclassify_socket6(struct socket *sock)
18578945ee5eSChuck Lever {
18588945ee5eSChuck Lever 	struct sock *sk = sock->sk;
18598945ee5eSChuck Lever 
18608945ee5eSChuck Lever 	sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC",
1861064a9177SNeilBrown 		&xs_slock_key[2], "sk_lock-AF_INET6-RPC", &xs_key[2]);
1862ed07536eSPeter Zijlstra }
18636bc9638aSPavel Emelyanov 
18646bc9638aSPavel Emelyanov static inline void xs_reclassify_socket(int family, struct socket *sock)
18656bc9638aSPavel Emelyanov {
1866fafc4e1eSHannes Frederic Sowa 	if (WARN_ON_ONCE(!sock_allow_reclassification(sock->sk)))
18671b7a1819SWeston Andros Adamson 		return;
18681b7a1819SWeston Andros Adamson 
18694232e863SChuck Lever 	switch (family) {
1870176e21eeSChuck Lever 	case AF_LOCAL:
1871176e21eeSChuck Lever 		xs_reclassify_socketu(sock);
1872176e21eeSChuck Lever 		break;
18734232e863SChuck Lever 	case AF_INET:
18746bc9638aSPavel Emelyanov 		xs_reclassify_socket4(sock);
18754232e863SChuck Lever 		break;
18764232e863SChuck Lever 	case AF_INET6:
18776bc9638aSPavel Emelyanov 		xs_reclassify_socket6(sock);
18784232e863SChuck Lever 		break;
18794232e863SChuck Lever 	}
18806bc9638aSPavel Emelyanov }
1881ed07536eSPeter Zijlstra #else
18826bc9638aSPavel Emelyanov static inline void xs_reclassify_socket(int family, struct socket *sock)
18836bc9638aSPavel Emelyanov {
18846bc9638aSPavel Emelyanov }
1885ed07536eSPeter Zijlstra #endif
1886ed07536eSPeter Zijlstra 
188793dc41bdSNeilBrown static void xs_dummy_setup_socket(struct work_struct *work)
188893dc41bdSNeilBrown {
188993dc41bdSNeilBrown }
189093dc41bdSNeilBrown 
18916bc9638aSPavel Emelyanov static struct socket *xs_create_sock(struct rpc_xprt *xprt,
18924dda9c8aSTrond Myklebust 		struct sock_xprt *transport, int family, int type,
18934dda9c8aSTrond Myklebust 		int protocol, bool reuseport)
189422f79326SPavel Emelyanov {
1895a73881c9STrond Myklebust 	struct file *filp;
189622f79326SPavel Emelyanov 	struct socket *sock;
189722f79326SPavel Emelyanov 	int err;
189822f79326SPavel Emelyanov 
18996bc9638aSPavel Emelyanov 	err = __sock_create(xprt->xprt_net, family, type, protocol, &sock, 1);
190022f79326SPavel Emelyanov 	if (err < 0) {
190122f79326SPavel Emelyanov 		dprintk("RPC:       can't create %d transport socket (%d).\n",
190222f79326SPavel Emelyanov 				protocol, -err);
190322f79326SPavel Emelyanov 		goto out;
190422f79326SPavel Emelyanov 	}
19056bc9638aSPavel Emelyanov 	xs_reclassify_socket(family, sock);
190622f79326SPavel Emelyanov 
19074dda9c8aSTrond Myklebust 	if (reuseport)
1908fe31a326SChristoph Hellwig 		sock_set_reuseport(sock->sk);
19094dda9c8aSTrond Myklebust 
19104cea288aSBen Hutchings 	err = xs_bind(transport, sock);
19114cea288aSBen Hutchings 	if (err) {
191222f79326SPavel Emelyanov 		sock_release(sock);
191322f79326SPavel Emelyanov 		goto out;
191422f79326SPavel Emelyanov 	}
191522f79326SPavel Emelyanov 
1916a73881c9STrond Myklebust 	filp = sock_alloc_file(sock, O_NONBLOCK, NULL);
1917a73881c9STrond Myklebust 	if (IS_ERR(filp))
1918a73881c9STrond Myklebust 		return ERR_CAST(filp);
1919a73881c9STrond Myklebust 	transport->file = filp;
1920a73881c9STrond Myklebust 
192122f79326SPavel Emelyanov 	return sock;
192222f79326SPavel Emelyanov out:
192322f79326SPavel Emelyanov 	return ERR_PTR(err);
192422f79326SPavel Emelyanov }
192522f79326SPavel Emelyanov 
1926176e21eeSChuck Lever static int xs_local_finish_connecting(struct rpc_xprt *xprt,
1927176e21eeSChuck Lever 				      struct socket *sock)
1928176e21eeSChuck Lever {
1929176e21eeSChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
1930176e21eeSChuck Lever 									xprt);
1931176e21eeSChuck Lever 
1932176e21eeSChuck Lever 	if (!transport->inet) {
1933176e21eeSChuck Lever 		struct sock *sk = sock->sk;
1934176e21eeSChuck Lever 
1935ea9afca8STrond Myklebust 		lock_sock(sk);
1936176e21eeSChuck Lever 
1937176e21eeSChuck Lever 		xs_save_old_callbacks(transport, sk);
1938176e21eeSChuck Lever 
1939176e21eeSChuck Lever 		sk->sk_user_data = xprt;
1940a2648094STrond Myklebust 		sk->sk_data_ready = xs_data_ready;
1941176e21eeSChuck Lever 		sk->sk_write_space = xs_udp_write_space;
1942efce2d0bSTrond Myklebust 		sk->sk_state_change = xs_local_state_change;
19432118071dSTrond Myklebust 		sk->sk_error_report = xs_error_report;
194498123866SBenjamin Coddington 		sk->sk_use_task_frag = false;
1945176e21eeSChuck Lever 
1946176e21eeSChuck Lever 		xprt_clear_connected(xprt);
1947176e21eeSChuck Lever 
1948176e21eeSChuck Lever 		/* Reset to new socket */
1949176e21eeSChuck Lever 		transport->sock = sock;
1950176e21eeSChuck Lever 		transport->inet = sk;
1951176e21eeSChuck Lever 
1952ea9afca8STrond Myklebust 		release_sock(sk);
1953176e21eeSChuck Lever 	}
1954176e21eeSChuck Lever 
1955ae053551STrond Myklebust 	xs_stream_start_connect(transport);
19566c7a64e5STrond Myklebust 
1957176e21eeSChuck Lever 	return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, 0);
1958176e21eeSChuck Lever }
1959176e21eeSChuck Lever 
1960176e21eeSChuck Lever /**
1961176e21eeSChuck Lever  * xs_local_setup_socket - create AF_LOCAL socket, connect to a local endpoint
1962176e21eeSChuck Lever  * @transport: socket transport to connect
1963176e21eeSChuck Lever  */
1964dc107402SJ. Bruce Fields static int xs_local_setup_socket(struct sock_xprt *transport)
1965176e21eeSChuck Lever {
1966176e21eeSChuck Lever 	struct rpc_xprt *xprt = &transport->xprt;
1967a73881c9STrond Myklebust 	struct file *filp;
1968176e21eeSChuck Lever 	struct socket *sock;
196968e9a246SColin Ian King 	int status;
1970176e21eeSChuck Lever 
1971176e21eeSChuck Lever 	status = __sock_create(xprt->xprt_net, AF_LOCAL,
1972176e21eeSChuck Lever 					SOCK_STREAM, 0, &sock, 1);
1973176e21eeSChuck Lever 	if (status < 0) {
1974176e21eeSChuck Lever 		dprintk("RPC:       can't create AF_LOCAL "
1975176e21eeSChuck Lever 			"transport socket (%d).\n", -status);
1976176e21eeSChuck Lever 		goto out;
1977176e21eeSChuck Lever 	}
1978d1358917SStefan Hajnoczi 	xs_reclassify_socket(AF_LOCAL, sock);
1979176e21eeSChuck Lever 
1980a73881c9STrond Myklebust 	filp = sock_alloc_file(sock, O_NONBLOCK, NULL);
1981a73881c9STrond Myklebust 	if (IS_ERR(filp)) {
1982a73881c9STrond Myklebust 		status = PTR_ERR(filp);
1983a73881c9STrond Myklebust 		goto out;
1984a73881c9STrond Myklebust 	}
1985a73881c9STrond Myklebust 	transport->file = filp;
1986a73881c9STrond Myklebust 
1987176e21eeSChuck Lever 	dprintk("RPC:       worker connecting xprt %p via AF_LOCAL to %s\n",
1988176e21eeSChuck Lever 			xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1989176e21eeSChuck Lever 
1990176e21eeSChuck Lever 	status = xs_local_finish_connecting(xprt, sock);
199140b5ea0cSTrond Myklebust 	trace_rpc_socket_connect(xprt, sock, status);
1992176e21eeSChuck Lever 	switch (status) {
1993176e21eeSChuck Lever 	case 0:
1994176e21eeSChuck Lever 		dprintk("RPC:       xprt %p connected to %s\n",
1995176e21eeSChuck Lever 				xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
19963968a8a5SChuck Lever 		xprt->stat.connect_count++;
19973968a8a5SChuck Lever 		xprt->stat.connect_time += (long)jiffies -
19983968a8a5SChuck Lever 					   xprt->stat.connect_start;
1999176e21eeSChuck Lever 		xprt_set_connected(xprt);
200093f479d3SGustavo A. R. Silva 		break;
20013601c4a9STrond Myklebust 	case -ENOBUFS:
2002176e21eeSChuck Lever 		break;
2003176e21eeSChuck Lever 	case -ENOENT:
2004176e21eeSChuck Lever 		dprintk("RPC:       xprt %p: socket %s does not exist\n",
2005176e21eeSChuck Lever 				xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
2006176e21eeSChuck Lever 		break;
20074a20a988STrond Myklebust 	case -ECONNREFUSED:
20084a20a988STrond Myklebust 		dprintk("RPC:       xprt %p: connection refused for %s\n",
20094a20a988STrond Myklebust 				xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
20104a20a988STrond Myklebust 		break;
2011176e21eeSChuck Lever 	default:
2012176e21eeSChuck Lever 		printk(KERN_ERR "%s: unhandled error (%d) connecting to %s\n",
2013176e21eeSChuck Lever 				__func__, -status,
2014176e21eeSChuck Lever 				xprt->address_strings[RPC_DISPLAY_ADDR]);
2015176e21eeSChuck Lever 	}
2016176e21eeSChuck Lever 
2017176e21eeSChuck Lever out:
2018176e21eeSChuck Lever 	xprt_clear_connecting(xprt);
2019176e21eeSChuck Lever 	xprt_wake_pending_tasks(xprt, status);
2020dc107402SJ. Bruce Fields 	return status;
2021dc107402SJ. Bruce Fields }
2022dc107402SJ. Bruce Fields 
2023b6669737SLinus Torvalds static void xs_local_connect(struct rpc_xprt *xprt, struct rpc_task *task)
2024dc107402SJ. Bruce Fields {
2025dc107402SJ. Bruce Fields 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2026dc107402SJ. Bruce Fields 	int ret;
2027dc107402SJ. Bruce Fields 
2028aad41a7dSTrond Myklebust 	if (transport->file)
2029aad41a7dSTrond Myklebust 		goto force_disconnect;
2030aad41a7dSTrond Myklebust 
2031dc107402SJ. Bruce Fields 	if (RPC_IS_ASYNC(task)) {
2032dc107402SJ. Bruce Fields 		/*
2033dc107402SJ. Bruce Fields 		 * We want the AF_LOCAL connect to be resolved in the
2034dc107402SJ. Bruce Fields 		 * filesystem namespace of the process making the rpc
2035dc107402SJ. Bruce Fields 		 * call.  Thus we connect synchronously.
2036dc107402SJ. Bruce Fields 		 *
2037dc107402SJ. Bruce Fields 		 * If we want to support asynchronous AF_LOCAL calls,
2038dc107402SJ. Bruce Fields 		 * we'll need to figure out how to pass a namespace to
2039dc107402SJ. Bruce Fields 		 * connect.
2040dc107402SJ. Bruce Fields 		 */
204139494194STrond Myklebust 		rpc_task_set_rpc_status(task, -ENOTCONN);
2042aad41a7dSTrond Myklebust 		goto out_wake;
2043dc107402SJ. Bruce Fields 	}
2044dc107402SJ. Bruce Fields 	ret = xs_local_setup_socket(transport);
2045dc107402SJ. Bruce Fields 	if (ret && !RPC_IS_SOFTCONN(task))
2046dc107402SJ. Bruce Fields 		msleep_interruptible(15000);
2047aad41a7dSTrond Myklebust 	return;
2048aad41a7dSTrond Myklebust force_disconnect:
2049aad41a7dSTrond Myklebust 	xprt_force_disconnect(xprt);
2050aad41a7dSTrond Myklebust out_wake:
2051aad41a7dSTrond Myklebust 	xprt_clear_connecting(xprt);
2052aad41a7dSTrond Myklebust 	xprt_wake_pending_tasks(xprt, -ENOTCONN);
2053176e21eeSChuck Lever }
2054176e21eeSChuck Lever 
20553c87ef6eSJeff Layton #if IS_ENABLED(CONFIG_SUNRPC_SWAP)
2056d6e971d8SJeff Layton /*
2057693486d5SNeilBrown  * Note that this should be called with XPRT_LOCKED held, or recv_mutex
2058693486d5SNeilBrown  * held, or when we otherwise know that we have exclusive access to the
2059693486d5SNeilBrown  * socket, to guard against races with xs_reset_transport.
2060d6e971d8SJeff Layton  */
2061a564b8f0SMel Gorman static void xs_set_memalloc(struct rpc_xprt *xprt)
2062a564b8f0SMel Gorman {
2063a564b8f0SMel Gorman 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
2064a564b8f0SMel Gorman 			xprt);
2065a564b8f0SMel Gorman 
2066d6e971d8SJeff Layton 	/*
2067d6e971d8SJeff Layton 	 * If there's no sock, then we have nothing to set. The
2068d6e971d8SJeff Layton 	 * reconnecting process will get it for us.
2069d6e971d8SJeff Layton 	 */
2070d6e971d8SJeff Layton 	if (!transport->inet)
2071d6e971d8SJeff Layton 		return;
20728e228133SJeff Layton 	if (atomic_read(&xprt->swapper))
2073a564b8f0SMel Gorman 		sk_set_memalloc(transport->inet);
2074a564b8f0SMel Gorman }
2075a564b8f0SMel Gorman 
2076a564b8f0SMel Gorman /**
2077d67fa4d8SJeff Layton  * xs_enable_swap - Tag this transport as being used for swap.
2078a564b8f0SMel Gorman  * @xprt: transport to tag
2079a564b8f0SMel Gorman  *
20808e228133SJeff Layton  * Take a reference to this transport on behalf of the rpc_clnt, and
20818e228133SJeff Layton  * optionally mark it for swapping if it wasn't already.
2082a564b8f0SMel Gorman  */
2083d67fa4d8SJeff Layton static int
2084d67fa4d8SJeff Layton xs_enable_swap(struct rpc_xprt *xprt)
2085a564b8f0SMel Gorman {
2086d6e971d8SJeff Layton 	struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt);
2087a564b8f0SMel Gorman 
2088693486d5SNeilBrown 	mutex_lock(&xs->recv_mutex);
2089693486d5SNeilBrown 	if (atomic_inc_return(&xprt->swapper) == 1 &&
2090693486d5SNeilBrown 	    xs->inet)
2091d6e971d8SJeff Layton 		sk_set_memalloc(xs->inet);
2092693486d5SNeilBrown 	mutex_unlock(&xs->recv_mutex);
20938e228133SJeff Layton 	return 0;
2094a564b8f0SMel Gorman }
2095a564b8f0SMel Gorman 
20968e228133SJeff Layton /**
2097d67fa4d8SJeff Layton  * xs_disable_swap - Untag this transport as being used for swap.
20988e228133SJeff Layton  * @xprt: transport to tag
20998e228133SJeff Layton  *
21008e228133SJeff Layton  * Drop a "swapper" reference to this xprt on behalf of the rpc_clnt. If the
21018e228133SJeff Layton  * swapper refcount goes to 0, untag the socket as a memalloc socket.
21028e228133SJeff Layton  */
2103d67fa4d8SJeff Layton static void
2104d67fa4d8SJeff Layton xs_disable_swap(struct rpc_xprt *xprt)
21058e228133SJeff Layton {
2106d6e971d8SJeff Layton 	struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt);
21078e228133SJeff Layton 
2108693486d5SNeilBrown 	mutex_lock(&xs->recv_mutex);
2109693486d5SNeilBrown 	if (atomic_dec_and_test(&xprt->swapper) &&
2110693486d5SNeilBrown 	    xs->inet)
2111d6e971d8SJeff Layton 		sk_clear_memalloc(xs->inet);
2112693486d5SNeilBrown 	mutex_unlock(&xs->recv_mutex);
2113a564b8f0SMel Gorman }
2114a564b8f0SMel Gorman #else
2115a564b8f0SMel Gorman static void xs_set_memalloc(struct rpc_xprt *xprt)
2116a564b8f0SMel Gorman {
2117a564b8f0SMel Gorman }
2118d67fa4d8SJeff Layton 
2119d67fa4d8SJeff Layton static int
2120d67fa4d8SJeff Layton xs_enable_swap(struct rpc_xprt *xprt)
2121d67fa4d8SJeff Layton {
2122d67fa4d8SJeff Layton 	return -EINVAL;
2123d67fa4d8SJeff Layton }
2124d67fa4d8SJeff Layton 
2125d67fa4d8SJeff Layton static void
2126d67fa4d8SJeff Layton xs_disable_swap(struct rpc_xprt *xprt)
2127d67fa4d8SJeff Layton {
2128d67fa4d8SJeff Layton }
2129a564b8f0SMel Gorman #endif
2130a564b8f0SMel Gorman 
213116be2d20SChuck Lever static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
2132a246b010SChuck Lever {
213316be2d20SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2134edb267a6SChuck Lever 
2135ee0ac0c2SChuck Lever 	if (!transport->inet) {
2136b0d93ad5SChuck Lever 		struct sock *sk = sock->sk;
2137b0d93ad5SChuck Lever 
2138ea9afca8STrond Myklebust 		lock_sock(sk);
2139b0d93ad5SChuck Lever 
21402a9e1cfaSTrond Myklebust 		xs_save_old_callbacks(transport, sk);
21412a9e1cfaSTrond Myklebust 
2142b0d93ad5SChuck Lever 		sk->sk_user_data = xprt;
2143f9b2ee71STrond Myklebust 		sk->sk_data_ready = xs_data_ready;
2144b0d93ad5SChuck Lever 		sk->sk_write_space = xs_udp_write_space;
214598123866SBenjamin Coddington 		sk->sk_use_task_frag = false;
2146b0d93ad5SChuck Lever 
2147b0d93ad5SChuck Lever 		xprt_set_connected(xprt);
2148b0d93ad5SChuck Lever 
2149b0d93ad5SChuck Lever 		/* Reset to new socket */
2150ee0ac0c2SChuck Lever 		transport->sock = sock;
2151ee0ac0c2SChuck Lever 		transport->inet = sk;
2152b0d93ad5SChuck Lever 
2153a564b8f0SMel Gorman 		xs_set_memalloc(xprt);
2154a564b8f0SMel Gorman 
2155ea9afca8STrond Myklebust 		release_sock(sk);
2156b0d93ad5SChuck Lever 	}
2157470056c2SChuck Lever 	xs_udp_do_set_buffer_size(xprt);
215802910177STrond Myklebust 
215902910177STrond Myklebust 	xprt->stat.connect_start = jiffies;
216016be2d20SChuck Lever }
216116be2d20SChuck Lever 
21628c14ff2aSPavel Emelyanov static void xs_udp_setup_socket(struct work_struct *work)
2163a246b010SChuck Lever {
2164a246b010SChuck Lever 	struct sock_xprt *transport =
2165a246b010SChuck Lever 		container_of(work, struct sock_xprt, connect_worker.work);
2166a246b010SChuck Lever 	struct rpc_xprt *xprt = &transport->xprt;
2167d099b8afSColin Ian King 	struct socket *sock;
2168b65c0310SPavel Emelyanov 	int status = -EIO;
21698db55a03SNeilBrown 	unsigned int pflags = current->flags;
2170a246b010SChuck Lever 
21718db55a03SNeilBrown 	if (atomic_read(&xprt->swapper))
21728db55a03SNeilBrown 		current->flags |= PF_MEMALLOC;
21738c14ff2aSPavel Emelyanov 	sock = xs_create_sock(xprt, transport,
21744dda9c8aSTrond Myklebust 			xs_addr(xprt)->sa_family, SOCK_DGRAM,
21754dda9c8aSTrond Myklebust 			IPPROTO_UDP, false);
2176b65c0310SPavel Emelyanov 	if (IS_ERR(sock))
2177a246b010SChuck Lever 		goto out;
217868e220bdSChuck Lever 
2179c740eff8SChuck Lever 	dprintk("RPC:       worker connecting xprt %p via %s to "
2180c740eff8SChuck Lever 				"%s (port %s)\n", xprt,
2181c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_PROTO],
2182c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_ADDR],
2183c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_PORT]);
218468e220bdSChuck Lever 
218568e220bdSChuck Lever 	xs_udp_finish_connecting(xprt, sock);
218640b5ea0cSTrond Myklebust 	trace_rpc_socket_connect(xprt, sock, 0);
2187a246b010SChuck Lever 	status = 0;
2188b0d93ad5SChuck Lever out:
2189b0d93ad5SChuck Lever 	xprt_clear_connecting(xprt);
2190cf76785dSTrond Myklebust 	xprt_unlock_connect(xprt, transport);
21917d1e8255STrond Myklebust 	xprt_wake_pending_tasks(xprt, status);
21928db55a03SNeilBrown 	current_restore_flags(pflags, PF_MEMALLOC);
2193b0d93ad5SChuck Lever }
2194b0d93ad5SChuck Lever 
21954876cc77STrond Myklebust /**
21964876cc77STrond Myklebust  * xs_tcp_shutdown - gracefully shut down a TCP socket
21974876cc77STrond Myklebust  * @xprt: transport
21984876cc77STrond Myklebust  *
21994876cc77STrond Myklebust  * Initiates a graceful shutdown of the TCP socket by calling the
22004876cc77STrond Myklebust  * equivalent of shutdown(SHUT_RDWR);
22014876cc77STrond Myklebust  */
22024876cc77STrond Myklebust static void xs_tcp_shutdown(struct rpc_xprt *xprt)
22034876cc77STrond Myklebust {
22044876cc77STrond Myklebust 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
22054876cc77STrond Myklebust 	struct socket *sock = transport->sock;
22069b30889cSTrond Myklebust 	int skst = transport->inet ? transport->inet->sk_state : TCP_CLOSE;
22074876cc77STrond Myklebust 
22084876cc77STrond Myklebust 	if (sock == NULL)
22094876cc77STrond Myklebust 		return;
22100a6ff58eSTrond Myklebust 	if (!xprt->reuseport) {
22110a6ff58eSTrond Myklebust 		xs_close(xprt);
22120a6ff58eSTrond Myklebust 		return;
22130a6ff58eSTrond Myklebust 	}
22149b30889cSTrond Myklebust 	switch (skst) {
22157c81e6a9STrond Myklebust 	case TCP_FIN_WAIT1:
22167c81e6a9STrond Myklebust 	case TCP_FIN_WAIT2:
2217943d045aSSiddharth Kawar 	case TCP_LAST_ACK:
22187c81e6a9STrond Myklebust 		break;
22197c81e6a9STrond Myklebust 	case TCP_ESTABLISHED:
22207c81e6a9STrond Myklebust 	case TCP_CLOSE_WAIT:
22214876cc77STrond Myklebust 		kernel_sock_shutdown(sock, SHUT_RDWR);
22224876cc77STrond Myklebust 		trace_rpc_socket_shutdown(xprt, sock);
22239b30889cSTrond Myklebust 		break;
22247c81e6a9STrond Myklebust 	default:
22254876cc77STrond Myklebust 		xs_reset_transport(transport);
22264876cc77STrond Myklebust 	}
22279b30889cSTrond Myklebust }
22284876cc77STrond Myklebust 
22298d1b8c62STrond Myklebust static void xs_tcp_set_socket_timeouts(struct rpc_xprt *xprt,
22308d1b8c62STrond Myklebust 		struct socket *sock)
2231b0d93ad5SChuck Lever {
223216be2d20SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
22333a107f07STrond Myklebust 	struct net *net = sock_net(sock->sk);
22343a107f07STrond Myklebust 	unsigned long connect_timeout;
22353a107f07STrond Myklebust 	unsigned long syn_retries;
22367196dbb0STrond Myklebust 	unsigned int keepidle;
22377196dbb0STrond Myklebust 	unsigned int keepcnt;
2238775f06abSTrond Myklebust 	unsigned int timeo;
22393a107f07STrond Myklebust 	unsigned long t;
22407f260e85STrond Myklebust 
2241b5e92419STrond Myklebust 	spin_lock(&xprt->transport_lock);
22427196dbb0STrond Myklebust 	keepidle = DIV_ROUND_UP(xprt->timeout->to_initval, HZ);
22437196dbb0STrond Myklebust 	keepcnt = xprt->timeout->to_retries + 1;
22447196dbb0STrond Myklebust 	timeo = jiffies_to_msecs(xprt->timeout->to_initval) *
22457196dbb0STrond Myklebust 		(xprt->timeout->to_retries + 1);
22467196dbb0STrond Myklebust 	clear_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state);
2247b5e92419STrond Myklebust 	spin_unlock(&xprt->transport_lock);
22487f260e85STrond Myklebust 
22497f260e85STrond Myklebust 	/* TCP Keepalive options */
2250ce3d9544SChristoph Hellwig 	sock_set_keepalive(sock->sk);
225171c48eb8SChristoph Hellwig 	tcp_sock_set_keepidle(sock->sk, keepidle);
2252d41ecaacSChristoph Hellwig 	tcp_sock_set_keepintvl(sock->sk, keepidle);
2253480aeb96SChristoph Hellwig 	tcp_sock_set_keepcnt(sock->sk, keepcnt);
2254b0d93ad5SChuck Lever 
22558d1b8c62STrond Myklebust 	/* TCP user timeout (see RFC5482) */
2256c488aeadSChristoph Hellwig 	tcp_sock_set_user_timeout(sock->sk, timeo);
22573a107f07STrond Myklebust 
22583a107f07STrond Myklebust 	/* Connect timeout */
22593a107f07STrond Myklebust 	connect_timeout = max_t(unsigned long,
22603a107f07STrond Myklebust 				DIV_ROUND_UP(xprt->connect_timeout, HZ), 1);
22613a107f07STrond Myklebust 	syn_retries = max_t(unsigned long,
22623a107f07STrond Myklebust 			    READ_ONCE(net->ipv4.sysctl_tcp_syn_retries), 1);
22633a107f07STrond Myklebust 	for (t = 0; t <= syn_retries && (1UL << t) < connect_timeout; t++)
22643a107f07STrond Myklebust 		;
22653a107f07STrond Myklebust 	if (t <= syn_retries)
22663a107f07STrond Myklebust 		tcp_sock_set_syncnt(sock->sk, t - 1);
22678d1b8c62STrond Myklebust }
22688d1b8c62STrond Myklebust 
2269*3e6ff89dSTrond Myklebust static void xs_tcp_do_set_connect_timeout(struct rpc_xprt *xprt,
2270*3e6ff89dSTrond Myklebust 					  unsigned long connect_timeout)
2271*3e6ff89dSTrond Myklebust {
2272*3e6ff89dSTrond Myklebust 	struct sock_xprt *transport =
2273*3e6ff89dSTrond Myklebust 		container_of(xprt, struct sock_xprt, xprt);
2274*3e6ff89dSTrond Myklebust 	struct rpc_timeout to;
2275*3e6ff89dSTrond Myklebust 	unsigned long initval;
2276*3e6ff89dSTrond Myklebust 
2277*3e6ff89dSTrond Myklebust 	memcpy(&to, xprt->timeout, sizeof(to));
2278*3e6ff89dSTrond Myklebust 	/* Arbitrary lower limit */
2279*3e6ff89dSTrond Myklebust 	initval = max_t(unsigned long, connect_timeout, XS_TCP_INIT_REEST_TO);
2280*3e6ff89dSTrond Myklebust 	to.to_initval = initval;
2281*3e6ff89dSTrond Myklebust 	to.to_maxval = initval;
2282*3e6ff89dSTrond Myklebust 	to.to_retries = 0;
2283*3e6ff89dSTrond Myklebust 	memcpy(&transport->tcp_timeout, &to, sizeof(transport->tcp_timeout));
2284*3e6ff89dSTrond Myklebust 	xprt->timeout = &transport->tcp_timeout;
2285*3e6ff89dSTrond Myklebust 	xprt->connect_timeout = connect_timeout;
2286*3e6ff89dSTrond Myklebust }
2287*3e6ff89dSTrond Myklebust 
22887196dbb0STrond Myklebust static void xs_tcp_set_connect_timeout(struct rpc_xprt *xprt,
22897196dbb0STrond Myklebust 		unsigned long connect_timeout,
22907196dbb0STrond Myklebust 		unsigned long reconnect_timeout)
22917196dbb0STrond Myklebust {
22927196dbb0STrond Myklebust 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
22937196dbb0STrond Myklebust 	struct rpc_timeout to;
22947196dbb0STrond Myklebust 	unsigned long initval;
22957196dbb0STrond Myklebust 
2296b5e92419STrond Myklebust 	spin_lock(&xprt->transport_lock);
22977196dbb0STrond Myklebust 	if (reconnect_timeout < xprt->max_reconnect_timeout)
22987196dbb0STrond Myklebust 		xprt->max_reconnect_timeout = reconnect_timeout;
2299*3e6ff89dSTrond Myklebust 	if (connect_timeout < xprt->connect_timeout)
2300*3e6ff89dSTrond Myklebust 		xs_tcp_do_set_connect_timeout(xprt, connect_timeout);
23017196dbb0STrond Myklebust 	set_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state);
2302b5e92419STrond Myklebust 	spin_unlock(&xprt->transport_lock);
23037196dbb0STrond Myklebust }
23047196dbb0STrond Myklebust 
23058d1b8c62STrond Myklebust static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
23068d1b8c62STrond Myklebust {
23078d1b8c62STrond Myklebust 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
23088d1b8c62STrond Myklebust 
23098d1b8c62STrond Myklebust 	if (!transport->inet) {
23108d1b8c62STrond Myklebust 		struct sock *sk = sock->sk;
23118d1b8c62STrond Myklebust 
2312d88e4d82SNeilBrown 		/* Avoid temporary address, they are bad for long-lived
2313d88e4d82SNeilBrown 		 * connections such as NFS mounts.
2314d88e4d82SNeilBrown 		 * RFC4941, section 3.6 suggests that:
2315d88e4d82SNeilBrown 		 *    Individual applications, which have specific
2316d88e4d82SNeilBrown 		 *    knowledge about the normal duration of connections,
2317d88e4d82SNeilBrown 		 *    MAY override this as appropriate.
2318d88e4d82SNeilBrown 		 */
231918d5ad62SChristoph Hellwig 		if (xs_addr(xprt)->sa_family == PF_INET6) {
232018d5ad62SChristoph Hellwig 			ip6_sock_set_addr_preferences(sk,
232118d5ad62SChristoph Hellwig 				IPV6_PREFER_SRC_PUBLIC);
232218d5ad62SChristoph Hellwig 		}
2323d88e4d82SNeilBrown 
23248d1b8c62STrond Myklebust 		xs_tcp_set_socket_timeouts(xprt, sock);
2325d737e5d4STrond Myklebust 		tcp_sock_set_nodelay(sk);
2326775f06abSTrond Myklebust 
2327ea9afca8STrond Myklebust 		lock_sock(sk);
2328b0d93ad5SChuck Lever 
23292a9e1cfaSTrond Myklebust 		xs_save_old_callbacks(transport, sk);
23302a9e1cfaSTrond Myklebust 
2331b0d93ad5SChuck Lever 		sk->sk_user_data = xprt;
23325157b956STrond Myklebust 		sk->sk_data_ready = xs_data_ready;
2333b0d93ad5SChuck Lever 		sk->sk_state_change = xs_tcp_state_change;
2334b0d93ad5SChuck Lever 		sk->sk_write_space = xs_tcp_write_space;
23352118071dSTrond Myklebust 		sk->sk_error_report = xs_error_report;
233698123866SBenjamin Coddington 		sk->sk_use_task_frag = false;
23373167e12cSChuck Lever 
23383167e12cSChuck Lever 		/* socket options */
23393167e12cSChuck Lever 		sock_reset_flag(sk, SOCK_LINGER);
2340b0d93ad5SChuck Lever 
2341b0d93ad5SChuck Lever 		xprt_clear_connected(xprt);
2342b0d93ad5SChuck Lever 
2343b0d93ad5SChuck Lever 		/* Reset to new socket */
2344ee0ac0c2SChuck Lever 		transport->sock = sock;
2345ee0ac0c2SChuck Lever 		transport->inet = sk;
2346b0d93ad5SChuck Lever 
2347ea9afca8STrond Myklebust 		release_sock(sk);
2348b0d93ad5SChuck Lever 	}
2349b0d93ad5SChuck Lever 
235001d37c42STrond Myklebust 	if (!xprt_bound(xprt))
2351280254b6STrond Myklebust 		return -ENOTCONN;
235201d37c42STrond Myklebust 
2353a564b8f0SMel Gorman 	xs_set_memalloc(xprt);
2354a564b8f0SMel Gorman 
2355ae053551STrond Myklebust 	xs_stream_start_connect(transport);
2356e1806c7bSTrond Myklebust 
2357b0d93ad5SChuck Lever 	/* Tell the socket layer to start connecting... */
23580fdea1e8STrond Myklebust 	set_bit(XPRT_SOCK_CONNECTING, &transport->sock_state);
2359280254b6STrond Myklebust 	return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK);
236016be2d20SChuck Lever }
236116be2d20SChuck Lever 
236216be2d20SChuck Lever /**
2363b61d59ffSTrond Myklebust  * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint
2364acf0a39fSChuck Lever  * @work: queued work item
236516be2d20SChuck Lever  *
236616be2d20SChuck Lever  * Invoked by a work queue tasklet.
236716be2d20SChuck Lever  */
2368cdd518d5SPavel Emelyanov static void xs_tcp_setup_socket(struct work_struct *work)
236916be2d20SChuck Lever {
2370cdd518d5SPavel Emelyanov 	struct sock_xprt *transport =
2371cdd518d5SPavel Emelyanov 		container_of(work, struct sock_xprt, connect_worker.work);
237216be2d20SChuck Lever 	struct socket *sock = transport->sock;
2373a9f5f0f7SPavel Emelyanov 	struct rpc_xprt *xprt = &transport->xprt;
2374280254b6STrond Myklebust 	int status;
23758db55a03SNeilBrown 	unsigned int pflags = current->flags;
237616be2d20SChuck Lever 
23778db55a03SNeilBrown 	if (atomic_read(&xprt->swapper))
23788db55a03SNeilBrown 		current->flags |= PF_MEMALLOC;
237989f42494STrond Myklebust 
238089f42494STrond Myklebust 	if (xprt_connected(xprt))
238189f42494STrond Myklebust 		goto out;
238289f42494STrond Myklebust 	if (test_and_clear_bit(XPRT_SOCK_CONNECT_SENT,
238389f42494STrond Myklebust 			       &transport->sock_state) ||
238489f42494STrond Myklebust 	    !sock) {
238589f42494STrond Myklebust 		xs_reset_transport(transport);
238689f42494STrond Myklebust 		sock = xs_create_sock(xprt, transport, xs_addr(xprt)->sa_family,
238789f42494STrond Myklebust 				      SOCK_STREAM, IPPROTO_TCP, true);
2388b61d59ffSTrond Myklebust 		if (IS_ERR(sock)) {
2389280254b6STrond Myklebust 			xprt_wake_pending_tasks(xprt, PTR_ERR(sock));
239016be2d20SChuck Lever 			goto out;
239116be2d20SChuck Lever 		}
23927d1e8255STrond Myklebust 	}
23937d1e8255STrond Myklebust 
2394c740eff8SChuck Lever 	dprintk("RPC:       worker connecting xprt %p via %s to "
2395c740eff8SChuck Lever 				"%s (port %s)\n", xprt,
2396c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_PROTO],
2397c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_ADDR],
2398c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_PORT]);
239916be2d20SChuck Lever 
240016be2d20SChuck Lever 	status = xs_tcp_finish_connecting(xprt, sock);
240140b5ea0cSTrond Myklebust 	trace_rpc_socket_connect(xprt, sock, status);
2402a246b010SChuck Lever 	dprintk("RPC:       %p connect status %d connected %d sock state %d\n",
240346121cf7SChuck Lever 			xprt, -status, xprt_connected(xprt),
240446121cf7SChuck Lever 			sock->sk->sk_state);
2405a246b010SChuck Lever 	switch (status) {
24062a491991STrond Myklebust 	case 0:
2407a246b010SChuck Lever 	case -EINPROGRESS:
2408280254b6STrond Myklebust 		/* SYN_SENT! */
240989f42494STrond Myklebust 		set_bit(XPRT_SOCK_CONNECT_SENT, &transport->sock_state);
2410280254b6STrond Myklebust 		if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
2411280254b6STrond Myklebust 			xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2412280254b6STrond Myklebust 		fallthrough;
2413a246b010SChuck Lever 	case -EALREADY:
2414280254b6STrond Myklebust 		goto out_unlock;
2415280254b6STrond Myklebust 	case -EADDRNOTAVAIL:
2416280254b6STrond Myklebust 		/* Source port number is unavailable. Try a new one! */
2417280254b6STrond Myklebust 		transport->srcport = 0;
2418280254b6STrond Myklebust 		status = -EAGAIN;
2419280254b6STrond Myklebust 		break;
24209fcfe0c8STrond Myklebust 	case -EINVAL:
24219fcfe0c8STrond Myklebust 		/* Happens, for instance, if the user specified a link
24229fcfe0c8STrond Myklebust 		 * local IPv6 address without a scope-id.
24239fcfe0c8STrond Myklebust 		 */
24243ed5e2a2STrond Myklebust 	case -ECONNREFUSED:
24253ed5e2a2STrond Myklebust 	case -ECONNRESET:
2426eb5b46faSTrond Myklebust 	case -ENETDOWN:
24273ed5e2a2STrond Myklebust 	case -ENETUNREACH:
24284ba161a7STrond Myklebust 	case -EHOSTUNREACH:
24293913c78cSTrond Myklebust 	case -EADDRINUSE:
24303601c4a9STrond Myklebust 	case -ENOBUFS:
2431280254b6STrond Myklebust 		break;
2432280254b6STrond Myklebust 	default:
2433280254b6STrond Myklebust 		printk("%s: connect returned unhandled error %d\n",
2434280254b6STrond Myklebust 			__func__, status);
2435280254b6STrond Myklebust 		status = -EAGAIN;
2436280254b6STrond Myklebust 	}
2437280254b6STrond Myklebust 
24388c71139dSCalum Mackay 	/* xs_tcp_force_close() wakes tasks with a fixed error code.
24398c71139dSCalum Mackay 	 * We need to wake them first to ensure the correct error code.
24406ea44adcSNeilBrown 	 */
24416ea44adcSNeilBrown 	xprt_wake_pending_tasks(xprt, status);
24424efdd92cSTrond Myklebust 	xs_tcp_force_close(xprt);
2443a246b010SChuck Lever out:
24442226feb6SChuck Lever 	xprt_clear_connecting(xprt);
2445280254b6STrond Myklebust out_unlock:
2446cf76785dSTrond Myklebust 	xprt_unlock_connect(xprt, transport);
24478db55a03SNeilBrown 	current_restore_flags(pflags, PF_MEMALLOC);
2448a246b010SChuck Lever }
2449a246b010SChuck Lever 
245075eb6af7SChuck Lever /*
245175eb6af7SChuck Lever  * Transfer the connected socket to @upper_transport, then mark that
245275eb6af7SChuck Lever  * xprt CONNECTED.
245375eb6af7SChuck Lever  */
245475eb6af7SChuck Lever static int xs_tcp_tls_finish_connecting(struct rpc_xprt *lower_xprt,
245575eb6af7SChuck Lever 					struct sock_xprt *upper_transport)
245675eb6af7SChuck Lever {
245775eb6af7SChuck Lever 	struct sock_xprt *lower_transport =
245875eb6af7SChuck Lever 			container_of(lower_xprt, struct sock_xprt, xprt);
245975eb6af7SChuck Lever 	struct rpc_xprt *upper_xprt = &upper_transport->xprt;
246075eb6af7SChuck Lever 
246175eb6af7SChuck Lever 	if (!upper_transport->inet) {
246275eb6af7SChuck Lever 		struct socket *sock = lower_transport->sock;
246375eb6af7SChuck Lever 		struct sock *sk = sock->sk;
246475eb6af7SChuck Lever 
246575eb6af7SChuck Lever 		/* Avoid temporary address, they are bad for long-lived
246675eb6af7SChuck Lever 		 * connections such as NFS mounts.
246775eb6af7SChuck Lever 		 * RFC4941, section 3.6 suggests that:
246875eb6af7SChuck Lever 		 *    Individual applications, which have specific
246975eb6af7SChuck Lever 		 *    knowledge about the normal duration of connections,
247075eb6af7SChuck Lever 		 *    MAY override this as appropriate.
247175eb6af7SChuck Lever 		 */
247275eb6af7SChuck Lever 		if (xs_addr(upper_xprt)->sa_family == PF_INET6)
247375eb6af7SChuck Lever 			ip6_sock_set_addr_preferences(sk, IPV6_PREFER_SRC_PUBLIC);
247475eb6af7SChuck Lever 
247575eb6af7SChuck Lever 		xs_tcp_set_socket_timeouts(upper_xprt, sock);
247675eb6af7SChuck Lever 		tcp_sock_set_nodelay(sk);
247775eb6af7SChuck Lever 
247875eb6af7SChuck Lever 		lock_sock(sk);
247975eb6af7SChuck Lever 
248075eb6af7SChuck Lever 		/* @sk is already connected, so it now has the RPC callbacks.
248175eb6af7SChuck Lever 		 * Reach into @lower_transport to save the original ones.
248275eb6af7SChuck Lever 		 */
248375eb6af7SChuck Lever 		upper_transport->old_data_ready = lower_transport->old_data_ready;
248475eb6af7SChuck Lever 		upper_transport->old_state_change = lower_transport->old_state_change;
248575eb6af7SChuck Lever 		upper_transport->old_write_space = lower_transport->old_write_space;
248675eb6af7SChuck Lever 		upper_transport->old_error_report = lower_transport->old_error_report;
248775eb6af7SChuck Lever 		sk->sk_user_data = upper_xprt;
248875eb6af7SChuck Lever 
248975eb6af7SChuck Lever 		/* socket options */
249075eb6af7SChuck Lever 		sock_reset_flag(sk, SOCK_LINGER);
249175eb6af7SChuck Lever 
249275eb6af7SChuck Lever 		xprt_clear_connected(upper_xprt);
249375eb6af7SChuck Lever 
249475eb6af7SChuck Lever 		upper_transport->sock = sock;
249575eb6af7SChuck Lever 		upper_transport->inet = sk;
249675eb6af7SChuck Lever 		upper_transport->file = lower_transport->file;
249775eb6af7SChuck Lever 
249875eb6af7SChuck Lever 		release_sock(sk);
249975eb6af7SChuck Lever 
250075eb6af7SChuck Lever 		/* Reset lower_transport before shutting down its clnt */
250175eb6af7SChuck Lever 		mutex_lock(&lower_transport->recv_mutex);
250275eb6af7SChuck Lever 		lower_transport->inet = NULL;
250375eb6af7SChuck Lever 		lower_transport->sock = NULL;
250475eb6af7SChuck Lever 		lower_transport->file = NULL;
250575eb6af7SChuck Lever 
250675eb6af7SChuck Lever 		xprt_clear_connected(lower_xprt);
250775eb6af7SChuck Lever 		xs_sock_reset_connection_flags(lower_xprt);
250875eb6af7SChuck Lever 		xs_stream_reset_connect(lower_transport);
250975eb6af7SChuck Lever 		mutex_unlock(&lower_transport->recv_mutex);
251075eb6af7SChuck Lever 	}
251175eb6af7SChuck Lever 
251275eb6af7SChuck Lever 	if (!xprt_bound(upper_xprt))
251375eb6af7SChuck Lever 		return -ENOTCONN;
251475eb6af7SChuck Lever 
251575eb6af7SChuck Lever 	xs_set_memalloc(upper_xprt);
251675eb6af7SChuck Lever 
251775eb6af7SChuck Lever 	if (!xprt_test_and_set_connected(upper_xprt)) {
251875eb6af7SChuck Lever 		upper_xprt->connect_cookie++;
251975eb6af7SChuck Lever 		clear_bit(XPRT_SOCK_CONNECTING, &upper_transport->sock_state);
252075eb6af7SChuck Lever 		xprt_clear_connecting(upper_xprt);
252175eb6af7SChuck Lever 
252275eb6af7SChuck Lever 		upper_xprt->stat.connect_count++;
252375eb6af7SChuck Lever 		upper_xprt->stat.connect_time += (long)jiffies -
252475eb6af7SChuck Lever 					   upper_xprt->stat.connect_start;
252575eb6af7SChuck Lever 		xs_run_error_worker(upper_transport, XPRT_SOCK_WAKE_PENDING);
252675eb6af7SChuck Lever 	}
252775eb6af7SChuck Lever 	return 0;
252875eb6af7SChuck Lever }
252975eb6af7SChuck Lever 
253075eb6af7SChuck Lever /**
253175eb6af7SChuck Lever  * xs_tls_handshake_done - TLS handshake completion handler
253275eb6af7SChuck Lever  * @data: address of xprt to wake
253375eb6af7SChuck Lever  * @status: status of handshake
253475eb6af7SChuck Lever  * @peerid: serial number of key containing the remote's identity
253575eb6af7SChuck Lever  *
253675eb6af7SChuck Lever  */
253775eb6af7SChuck Lever static void xs_tls_handshake_done(void *data, int status, key_serial_t peerid)
253875eb6af7SChuck Lever {
253975eb6af7SChuck Lever 	struct rpc_xprt *lower_xprt = data;
254075eb6af7SChuck Lever 	struct sock_xprt *lower_transport =
254175eb6af7SChuck Lever 				container_of(lower_xprt, struct sock_xprt, xprt);
254275eb6af7SChuck Lever 
254375eb6af7SChuck Lever 	lower_transport->xprt_err = status ? -EACCES : 0;
254475eb6af7SChuck Lever 	complete(&lower_transport->handshake_done);
254575eb6af7SChuck Lever 	xprt_put(lower_xprt);
254675eb6af7SChuck Lever }
254775eb6af7SChuck Lever 
254875eb6af7SChuck Lever static int xs_tls_handshake_sync(struct rpc_xprt *lower_xprt, struct xprtsec_parms *xprtsec)
254975eb6af7SChuck Lever {
255075eb6af7SChuck Lever 	struct sock_xprt *lower_transport =
255175eb6af7SChuck Lever 				container_of(lower_xprt, struct sock_xprt, xprt);
255275eb6af7SChuck Lever 	struct tls_handshake_args args = {
255375eb6af7SChuck Lever 		.ta_sock	= lower_transport->sock,
255475eb6af7SChuck Lever 		.ta_done	= xs_tls_handshake_done,
255575eb6af7SChuck Lever 		.ta_data	= xprt_get(lower_xprt),
255675eb6af7SChuck Lever 		.ta_peername	= lower_xprt->servername,
255775eb6af7SChuck Lever 	};
255875eb6af7SChuck Lever 	struct sock *sk = lower_transport->inet;
255975eb6af7SChuck Lever 	int rc;
256075eb6af7SChuck Lever 
256175eb6af7SChuck Lever 	init_completion(&lower_transport->handshake_done);
256275eb6af7SChuck Lever 	set_bit(XPRT_SOCK_IGNORE_RECV, &lower_transport->sock_state);
256375eb6af7SChuck Lever 	lower_transport->xprt_err = -ETIMEDOUT;
256475eb6af7SChuck Lever 	switch (xprtsec->policy) {
256575eb6af7SChuck Lever 	case RPC_XPRTSEC_TLS_ANON:
256675eb6af7SChuck Lever 		rc = tls_client_hello_anon(&args, GFP_KERNEL);
256775eb6af7SChuck Lever 		if (rc)
256875eb6af7SChuck Lever 			goto out_put_xprt;
256975eb6af7SChuck Lever 		break;
257075eb6af7SChuck Lever 	case RPC_XPRTSEC_TLS_X509:
257175eb6af7SChuck Lever 		args.ta_my_cert = xprtsec->cert_serial;
257275eb6af7SChuck Lever 		args.ta_my_privkey = xprtsec->privkey_serial;
257375eb6af7SChuck Lever 		rc = tls_client_hello_x509(&args, GFP_KERNEL);
257475eb6af7SChuck Lever 		if (rc)
257575eb6af7SChuck Lever 			goto out_put_xprt;
257675eb6af7SChuck Lever 		break;
257775eb6af7SChuck Lever 	default:
257875eb6af7SChuck Lever 		rc = -EACCES;
257975eb6af7SChuck Lever 		goto out_put_xprt;
258075eb6af7SChuck Lever 	}
258175eb6af7SChuck Lever 
258275eb6af7SChuck Lever 	rc = wait_for_completion_interruptible_timeout(&lower_transport->handshake_done,
258375eb6af7SChuck Lever 						       XS_TLS_HANDSHAKE_TO);
258475eb6af7SChuck Lever 	if (rc <= 0) {
258575eb6af7SChuck Lever 		if (!tls_handshake_cancel(sk)) {
258675eb6af7SChuck Lever 			if (rc == 0)
258775eb6af7SChuck Lever 				rc = -ETIMEDOUT;
258875eb6af7SChuck Lever 			goto out_put_xprt;
258975eb6af7SChuck Lever 		}
259075eb6af7SChuck Lever 	}
259175eb6af7SChuck Lever 
259275eb6af7SChuck Lever 	rc = lower_transport->xprt_err;
259375eb6af7SChuck Lever 
259475eb6af7SChuck Lever out:
259575eb6af7SChuck Lever 	xs_stream_reset_connect(lower_transport);
259675eb6af7SChuck Lever 	clear_bit(XPRT_SOCK_IGNORE_RECV, &lower_transport->sock_state);
259775eb6af7SChuck Lever 	return rc;
259875eb6af7SChuck Lever 
259975eb6af7SChuck Lever out_put_xprt:
260075eb6af7SChuck Lever 	xprt_put(lower_xprt);
260175eb6af7SChuck Lever 	goto out;
260275eb6af7SChuck Lever }
260375eb6af7SChuck Lever 
260475eb6af7SChuck Lever /**
260575eb6af7SChuck Lever  * xs_tcp_tls_setup_socket - establish a TLS session on a TCP socket
260675eb6af7SChuck Lever  * @work: queued work item
260775eb6af7SChuck Lever  *
260875eb6af7SChuck Lever  * Invoked by a work queue tasklet.
260975eb6af7SChuck Lever  *
261075eb6af7SChuck Lever  * For RPC-with-TLS, there is a two-stage connection process.
261175eb6af7SChuck Lever  *
261275eb6af7SChuck Lever  * The "upper-layer xprt" is visible to the RPC consumer. Once it has
261375eb6af7SChuck Lever  * been marked connected, the consumer knows that a TCP connection and
261475eb6af7SChuck Lever  * a TLS session have been established.
261575eb6af7SChuck Lever  *
261675eb6af7SChuck Lever  * A "lower-layer xprt", created in this function, handles the mechanics
261775eb6af7SChuck Lever  * of connecting the TCP socket, performing the RPC_AUTH_TLS probe, and
261875eb6af7SChuck Lever  * then driving the TLS handshake. Once all that is complete, the upper
261975eb6af7SChuck Lever  * layer xprt is marked connected.
262075eb6af7SChuck Lever  */
262175eb6af7SChuck Lever static void xs_tcp_tls_setup_socket(struct work_struct *work)
262275eb6af7SChuck Lever {
262375eb6af7SChuck Lever 	struct sock_xprt *upper_transport =
262475eb6af7SChuck Lever 		container_of(work, struct sock_xprt, connect_worker.work);
262575eb6af7SChuck Lever 	struct rpc_clnt *upper_clnt = upper_transport->clnt;
262675eb6af7SChuck Lever 	struct rpc_xprt *upper_xprt = &upper_transport->xprt;
262775eb6af7SChuck Lever 	struct rpc_create_args args = {
262875eb6af7SChuck Lever 		.net		= upper_xprt->xprt_net,
262975eb6af7SChuck Lever 		.protocol	= upper_xprt->prot,
263075eb6af7SChuck Lever 		.address	= (struct sockaddr *)&upper_xprt->addr,
263175eb6af7SChuck Lever 		.addrsize	= upper_xprt->addrlen,
263275eb6af7SChuck Lever 		.timeout	= upper_clnt->cl_timeout,
263375eb6af7SChuck Lever 		.servername	= upper_xprt->servername,
263475eb6af7SChuck Lever 		.program	= upper_clnt->cl_program,
263575eb6af7SChuck Lever 		.prognumber	= upper_clnt->cl_prog,
263675eb6af7SChuck Lever 		.version	= upper_clnt->cl_vers,
263775eb6af7SChuck Lever 		.authflavor	= RPC_AUTH_TLS,
263875eb6af7SChuck Lever 		.cred		= upper_clnt->cl_cred,
263975eb6af7SChuck Lever 		.xprtsec	= {
264075eb6af7SChuck Lever 			.policy		= RPC_XPRTSEC_NONE,
264175eb6af7SChuck Lever 		},
264275eb6af7SChuck Lever 	};
264375eb6af7SChuck Lever 	unsigned int pflags = current->flags;
264475eb6af7SChuck Lever 	struct rpc_clnt *lower_clnt;
264575eb6af7SChuck Lever 	struct rpc_xprt *lower_xprt;
264675eb6af7SChuck Lever 	int status;
264775eb6af7SChuck Lever 
264875eb6af7SChuck Lever 	if (atomic_read(&upper_xprt->swapper))
264975eb6af7SChuck Lever 		current->flags |= PF_MEMALLOC;
265075eb6af7SChuck Lever 
265175eb6af7SChuck Lever 	xs_stream_start_connect(upper_transport);
265275eb6af7SChuck Lever 
265375eb6af7SChuck Lever 	/* This implicitly sends an RPC_AUTH_TLS probe */
265475eb6af7SChuck Lever 	lower_clnt = rpc_create(&args);
265575eb6af7SChuck Lever 	if (IS_ERR(lower_clnt)) {
265675eb6af7SChuck Lever 		trace_rpc_tls_unavailable(upper_clnt, upper_xprt);
265775eb6af7SChuck Lever 		clear_bit(XPRT_SOCK_CONNECTING, &upper_transport->sock_state);
265875eb6af7SChuck Lever 		xprt_clear_connecting(upper_xprt);
265975eb6af7SChuck Lever 		xprt_wake_pending_tasks(upper_xprt, PTR_ERR(lower_clnt));
266075eb6af7SChuck Lever 		xs_run_error_worker(upper_transport, XPRT_SOCK_WAKE_PENDING);
266175eb6af7SChuck Lever 		goto out_unlock;
266275eb6af7SChuck Lever 	}
266375eb6af7SChuck Lever 
266475eb6af7SChuck Lever 	/* RPC_AUTH_TLS probe was successful. Try a TLS handshake on
266575eb6af7SChuck Lever 	 * the lower xprt.
266675eb6af7SChuck Lever 	 */
266775eb6af7SChuck Lever 	rcu_read_lock();
266875eb6af7SChuck Lever 	lower_xprt = rcu_dereference(lower_clnt->cl_xprt);
266975eb6af7SChuck Lever 	rcu_read_unlock();
267075eb6af7SChuck Lever 	status = xs_tls_handshake_sync(lower_xprt, &upper_xprt->xprtsec);
267175eb6af7SChuck Lever 	if (status) {
267275eb6af7SChuck Lever 		trace_rpc_tls_not_started(upper_clnt, upper_xprt);
267375eb6af7SChuck Lever 		goto out_close;
267475eb6af7SChuck Lever 	}
267575eb6af7SChuck Lever 
267675eb6af7SChuck Lever 	status = xs_tcp_tls_finish_connecting(lower_xprt, upper_transport);
267775eb6af7SChuck Lever 	if (status)
267875eb6af7SChuck Lever 		goto out_close;
267975eb6af7SChuck Lever 
268075eb6af7SChuck Lever 	trace_rpc_socket_connect(upper_xprt, upper_transport->sock, 0);
268175eb6af7SChuck Lever 	if (!xprt_test_and_set_connected(upper_xprt)) {
268275eb6af7SChuck Lever 		upper_xprt->connect_cookie++;
268375eb6af7SChuck Lever 		clear_bit(XPRT_SOCK_CONNECTING, &upper_transport->sock_state);
268475eb6af7SChuck Lever 		xprt_clear_connecting(upper_xprt);
268575eb6af7SChuck Lever 
268675eb6af7SChuck Lever 		upper_xprt->stat.connect_count++;
268775eb6af7SChuck Lever 		upper_xprt->stat.connect_time += (long)jiffies -
268875eb6af7SChuck Lever 					   upper_xprt->stat.connect_start;
268975eb6af7SChuck Lever 		xs_run_error_worker(upper_transport, XPRT_SOCK_WAKE_PENDING);
269075eb6af7SChuck Lever 	}
269175eb6af7SChuck Lever 	rpc_shutdown_client(lower_clnt);
269275eb6af7SChuck Lever 
269375eb6af7SChuck Lever out_unlock:
269475eb6af7SChuck Lever 	current_restore_flags(pflags, PF_MEMALLOC);
269575eb6af7SChuck Lever 	upper_transport->clnt = NULL;
269675eb6af7SChuck Lever 	xprt_unlock_connect(upper_xprt, upper_transport);
269775eb6af7SChuck Lever 	return;
269875eb6af7SChuck Lever 
269975eb6af7SChuck Lever out_close:
270075eb6af7SChuck Lever 	rpc_shutdown_client(lower_clnt);
270175eb6af7SChuck Lever 
270275eb6af7SChuck Lever 	/* xprt_force_disconnect() wakes tasks with a fixed tk_status code.
270375eb6af7SChuck Lever 	 * Wake them first here to ensure they get our tk_status code.
270475eb6af7SChuck Lever 	 */
270575eb6af7SChuck Lever 	xprt_wake_pending_tasks(upper_xprt, status);
270675eb6af7SChuck Lever 	xs_tcp_force_close(upper_xprt);
270775eb6af7SChuck Lever 	xprt_clear_connecting(upper_xprt);
270875eb6af7SChuck Lever 	goto out_unlock;
270975eb6af7SChuck Lever }
271075eb6af7SChuck Lever 
271168e220bdSChuck Lever /**
27129903cd1cSChuck Lever  * xs_connect - connect a socket to a remote endpoint
27131b092092STrond Myklebust  * @xprt: pointer to transport structure
27149903cd1cSChuck Lever  * @task: address of RPC task that manages state of connect request
27159903cd1cSChuck Lever  *
27169903cd1cSChuck Lever  * TCP: If the remote end dropped the connection, delay reconnecting.
271703bf4b70SChuck Lever  *
271803bf4b70SChuck Lever  * UDP socket connects are synchronous, but we use a work queue anyway
271903bf4b70SChuck Lever  * to guarantee that even unprivileged user processes can set up a
272003bf4b70SChuck Lever  * socket on a privileged port.
272103bf4b70SChuck Lever  *
272203bf4b70SChuck Lever  * If a UDP socket connect fails, the delay behavior here prevents
272303bf4b70SChuck Lever  * retry floods (hard mounts).
27249903cd1cSChuck Lever  */
27251b092092STrond Myklebust static void xs_connect(struct rpc_xprt *xprt, struct rpc_task *task)
2726a246b010SChuck Lever {
2727ee0ac0c2SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
272802910177STrond Myklebust 	unsigned long delay = 0;
2729a246b010SChuck Lever 
2730718ba5b8STrond Myklebust 	WARN_ON_ONCE(!xprt_lock_connect(xprt, task, transport));
2731718ba5b8STrond Myklebust 
273289f42494STrond Myklebust 	if (transport->sock != NULL) {
273346121cf7SChuck Lever 		dprintk("RPC:       xs_connect delayed xprt %p for %lu "
273489f42494STrond Myklebust 			"seconds\n", xprt, xprt->reestablish_timeout / HZ);
273599b1a4c3STrond Myklebust 
2736675dd90aSChuck Lever 		delay = xprt_reconnect_delay(xprt);
2737675dd90aSChuck Lever 		xprt_reconnect_backoff(xprt, XS_TCP_INIT_REEST_TO);
273802910177STrond Myklebust 
273902910177STrond Myklebust 	} else
27409903cd1cSChuck Lever 		dprintk("RPC:       xs_connect scheduled xprt %p\n", xprt);
274102910177STrond Myklebust 
274275eb6af7SChuck Lever 	transport->clnt = task->tk_client;
274340a5f1b1STrond Myklebust 	queue_delayed_work(xprtiod_workqueue,
274402910177STrond Myklebust 			&transport->connect_worker,
274502910177STrond Myklebust 			delay);
2746a246b010SChuck Lever }
2747a246b010SChuck Lever 
27484f8943f8STrond Myklebust static void xs_wake_disconnect(struct sock_xprt *transport)
27494f8943f8STrond Myklebust {
27504f8943f8STrond Myklebust 	if (test_and_clear_bit(XPRT_SOCK_WAKE_DISCONNECT, &transport->sock_state))
27514f8943f8STrond Myklebust 		xs_tcp_force_close(&transport->xprt);
27524f8943f8STrond Myklebust }
27534f8943f8STrond Myklebust 
27544f8943f8STrond Myklebust static void xs_wake_write(struct sock_xprt *transport)
27554f8943f8STrond Myklebust {
27564f8943f8STrond Myklebust 	if (test_and_clear_bit(XPRT_SOCK_WAKE_WRITE, &transport->sock_state))
27574f8943f8STrond Myklebust 		xprt_write_space(&transport->xprt);
27584f8943f8STrond Myklebust }
27594f8943f8STrond Myklebust 
27604f8943f8STrond Myklebust static void xs_wake_error(struct sock_xprt *transport)
27614f8943f8STrond Myklebust {
27624f8943f8STrond Myklebust 	int sockerr;
27634f8943f8STrond Myklebust 
27644f8943f8STrond Myklebust 	if (!test_bit(XPRT_SOCK_WAKE_ERROR, &transport->sock_state))
27654f8943f8STrond Myklebust 		return;
27664f8943f8STrond Myklebust 	mutex_lock(&transport->recv_mutex);
27674f8943f8STrond Myklebust 	if (transport->sock == NULL)
27684f8943f8STrond Myklebust 		goto out;
27694f8943f8STrond Myklebust 	if (!test_and_clear_bit(XPRT_SOCK_WAKE_ERROR, &transport->sock_state))
27704f8943f8STrond Myklebust 		goto out;
2771af84537dSBenjamin Coddington 	sockerr = xchg(&transport->xprt_err, 0);
27724f8943f8STrond Myklebust 	if (sockerr < 0)
27734f8943f8STrond Myklebust 		xprt_wake_pending_tasks(&transport->xprt, sockerr);
27744f8943f8STrond Myklebust out:
27754f8943f8STrond Myklebust 	mutex_unlock(&transport->recv_mutex);
27764f8943f8STrond Myklebust }
27774f8943f8STrond Myklebust 
27784f8943f8STrond Myklebust static void xs_wake_pending(struct sock_xprt *transport)
27794f8943f8STrond Myklebust {
27804f8943f8STrond Myklebust 	if (test_and_clear_bit(XPRT_SOCK_WAKE_PENDING, &transport->sock_state))
27814f8943f8STrond Myklebust 		xprt_wake_pending_tasks(&transport->xprt, -EAGAIN);
27824f8943f8STrond Myklebust }
27834f8943f8STrond Myklebust 
27844f8943f8STrond Myklebust static void xs_error_handle(struct work_struct *work)
27854f8943f8STrond Myklebust {
27864f8943f8STrond Myklebust 	struct sock_xprt *transport = container_of(work,
27874f8943f8STrond Myklebust 			struct sock_xprt, error_worker);
27884f8943f8STrond Myklebust 
27894f8943f8STrond Myklebust 	xs_wake_disconnect(transport);
27904f8943f8STrond Myklebust 	xs_wake_write(transport);
27914f8943f8STrond Myklebust 	xs_wake_error(transport);
27924f8943f8STrond Myklebust 	xs_wake_pending(transport);
27934f8943f8STrond Myklebust }
27944f8943f8STrond Myklebust 
2795262ca07dSChuck Lever /**
279612b20ce3SBhaskar Chowdhury  * xs_local_print_stats - display AF_LOCAL socket-specific stats
2797176e21eeSChuck Lever  * @xprt: rpc_xprt struct containing statistics
2798176e21eeSChuck Lever  * @seq: output file
2799176e21eeSChuck Lever  *
2800176e21eeSChuck Lever  */
2801176e21eeSChuck Lever static void xs_local_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2802176e21eeSChuck Lever {
2803176e21eeSChuck Lever 	long idle_time = 0;
2804176e21eeSChuck Lever 
2805176e21eeSChuck Lever 	if (xprt_connected(xprt))
2806176e21eeSChuck Lever 		idle_time = (long)(jiffies - xprt->last_used) / HZ;
2807176e21eeSChuck Lever 
2808176e21eeSChuck Lever 	seq_printf(seq, "\txprt:\tlocal %lu %lu %lu %ld %lu %lu %lu "
280915a45206SAndy Adamson 			"%llu %llu %lu %llu %llu\n",
2810176e21eeSChuck Lever 			xprt->stat.bind_count,
2811176e21eeSChuck Lever 			xprt->stat.connect_count,
28128440a886SChuck Lever 			xprt->stat.connect_time / HZ,
2813176e21eeSChuck Lever 			idle_time,
2814176e21eeSChuck Lever 			xprt->stat.sends,
2815176e21eeSChuck Lever 			xprt->stat.recvs,
2816176e21eeSChuck Lever 			xprt->stat.bad_xids,
2817176e21eeSChuck Lever 			xprt->stat.req_u,
281815a45206SAndy Adamson 			xprt->stat.bklog_u,
281915a45206SAndy Adamson 			xprt->stat.max_slots,
282015a45206SAndy Adamson 			xprt->stat.sending_u,
282115a45206SAndy Adamson 			xprt->stat.pending_u);
2822176e21eeSChuck Lever }
2823176e21eeSChuck Lever 
2824176e21eeSChuck Lever /**
282512b20ce3SBhaskar Chowdhury  * xs_udp_print_stats - display UDP socket-specific stats
2826262ca07dSChuck Lever  * @xprt: rpc_xprt struct containing statistics
2827262ca07dSChuck Lever  * @seq: output file
2828262ca07dSChuck Lever  *
2829262ca07dSChuck Lever  */
2830262ca07dSChuck Lever static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2831262ca07dSChuck Lever {
2832c8475461SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2833c8475461SChuck Lever 
283415a45206SAndy Adamson 	seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %llu %llu "
283515a45206SAndy Adamson 			"%lu %llu %llu\n",
2836fbfffbd5SChuck Lever 			transport->srcport,
2837262ca07dSChuck Lever 			xprt->stat.bind_count,
2838262ca07dSChuck Lever 			xprt->stat.sends,
2839262ca07dSChuck Lever 			xprt->stat.recvs,
2840262ca07dSChuck Lever 			xprt->stat.bad_xids,
2841262ca07dSChuck Lever 			xprt->stat.req_u,
284215a45206SAndy Adamson 			xprt->stat.bklog_u,
284315a45206SAndy Adamson 			xprt->stat.max_slots,
284415a45206SAndy Adamson 			xprt->stat.sending_u,
284515a45206SAndy Adamson 			xprt->stat.pending_u);
2846262ca07dSChuck Lever }
2847262ca07dSChuck Lever 
2848262ca07dSChuck Lever /**
284912b20ce3SBhaskar Chowdhury  * xs_tcp_print_stats - display TCP socket-specific stats
2850262ca07dSChuck Lever  * @xprt: rpc_xprt struct containing statistics
2851262ca07dSChuck Lever  * @seq: output file
2852262ca07dSChuck Lever  *
2853262ca07dSChuck Lever  */
2854262ca07dSChuck Lever static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2855262ca07dSChuck Lever {
2856c8475461SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2857262ca07dSChuck Lever 	long idle_time = 0;
2858262ca07dSChuck Lever 
2859262ca07dSChuck Lever 	if (xprt_connected(xprt))
2860262ca07dSChuck Lever 		idle_time = (long)(jiffies - xprt->last_used) / HZ;
2861262ca07dSChuck Lever 
286215a45206SAndy Adamson 	seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu "
286315a45206SAndy Adamson 			"%llu %llu %lu %llu %llu\n",
2864fbfffbd5SChuck Lever 			transport->srcport,
2865262ca07dSChuck Lever 			xprt->stat.bind_count,
2866262ca07dSChuck Lever 			xprt->stat.connect_count,
28678440a886SChuck Lever 			xprt->stat.connect_time / HZ,
2868262ca07dSChuck Lever 			idle_time,
2869262ca07dSChuck Lever 			xprt->stat.sends,
2870262ca07dSChuck Lever 			xprt->stat.recvs,
2871262ca07dSChuck Lever 			xprt->stat.bad_xids,
2872262ca07dSChuck Lever 			xprt->stat.req_u,
287315a45206SAndy Adamson 			xprt->stat.bklog_u,
287415a45206SAndy Adamson 			xprt->stat.max_slots,
287515a45206SAndy Adamson 			xprt->stat.sending_u,
287615a45206SAndy Adamson 			xprt->stat.pending_u);
2877262ca07dSChuck Lever }
2878262ca07dSChuck Lever 
28794cfc7e60SRahul Iyer /*
28804cfc7e60SRahul Iyer  * Allocate a bunch of pages for a scratch buffer for the rpc code. The reason
28814cfc7e60SRahul Iyer  * we allocate pages instead doing a kmalloc like rpc_malloc is because we want
28824cfc7e60SRahul Iyer  * to use the server side send routines.
28834cfc7e60SRahul Iyer  */
28845fe6eaa1SChuck Lever static int bc_malloc(struct rpc_task *task)
28854cfc7e60SRahul Iyer {
28865fe6eaa1SChuck Lever 	struct rpc_rqst *rqst = task->tk_rqstp;
28875fe6eaa1SChuck Lever 	size_t size = rqst->rq_callsize;
28884cfc7e60SRahul Iyer 	struct page *page;
28894cfc7e60SRahul Iyer 	struct rpc_buffer *buf;
28904cfc7e60SRahul Iyer 
28915fe6eaa1SChuck Lever 	if (size > PAGE_SIZE - sizeof(struct rpc_buffer)) {
28925fe6eaa1SChuck Lever 		WARN_ONCE(1, "xprtsock: large bc buffer request (size %zu)\n",
28935fe6eaa1SChuck Lever 			  size);
28945fe6eaa1SChuck Lever 		return -EINVAL;
28955fe6eaa1SChuck Lever 	}
28964cfc7e60SRahul Iyer 
2897b2648015STrond Myklebust 	page = alloc_page(GFP_KERNEL | __GFP_NORETRY | __GFP_NOWARN);
28984cfc7e60SRahul Iyer 	if (!page)
28995fe6eaa1SChuck Lever 		return -ENOMEM;
29004cfc7e60SRahul Iyer 
29014cfc7e60SRahul Iyer 	buf = page_address(page);
29024cfc7e60SRahul Iyer 	buf->len = PAGE_SIZE;
29034cfc7e60SRahul Iyer 
29045fe6eaa1SChuck Lever 	rqst->rq_buffer = buf->data;
290518e601d6SJeff Layton 	rqst->rq_rbuffer = (char *)rqst->rq_buffer + rqst->rq_callsize;
29065fe6eaa1SChuck Lever 	return 0;
29074cfc7e60SRahul Iyer }
29084cfc7e60SRahul Iyer 
29094cfc7e60SRahul Iyer /*
29104cfc7e60SRahul Iyer  * Free the space allocated in the bc_alloc routine
29114cfc7e60SRahul Iyer  */
29123435c74aSChuck Lever static void bc_free(struct rpc_task *task)
29134cfc7e60SRahul Iyer {
29143435c74aSChuck Lever 	void *buffer = task->tk_rqstp->rq_buffer;
29154cfc7e60SRahul Iyer 	struct rpc_buffer *buf;
29164cfc7e60SRahul Iyer 
29174cfc7e60SRahul Iyer 	buf = container_of(buffer, struct rpc_buffer, data);
29184cfc7e60SRahul Iyer 	free_page((unsigned long)buf);
29194cfc7e60SRahul Iyer }
29204cfc7e60SRahul Iyer 
29214cfc7e60SRahul Iyer static int bc_sendto(struct rpc_rqst *req)
29224cfc7e60SRahul Iyer {
2923da1661b9SChuck Lever 	struct xdr_buf *xdr = &req->rq_snd_buf;
29244cfc7e60SRahul Iyer 	struct sock_xprt *transport =
2925067fb11bSChuck Lever 			container_of(req->rq_xprt, struct sock_xprt, xprt);
2926067fb11bSChuck Lever 	struct msghdr msg = {
2927da1661b9SChuck Lever 		.msg_flags	= 0,
2928067fb11bSChuck Lever 	};
2929067fb11bSChuck Lever 	rpc_fraghdr marker = cpu_to_be32(RPC_LAST_STREAM_FRAGMENT |
2930da1661b9SChuck Lever 					 (u32)xdr->len);
2931da1661b9SChuck Lever 	unsigned int sent = 0;
2932da1661b9SChuck Lever 	int err;
29334cfc7e60SRahul Iyer 
29348729aabaSChuck Lever 	req->rq_xtime = ktime_get();
2935ff053dbbSTrond Myklebust 	err = xdr_alloc_bvec(xdr, rpc_task_gfp_mask());
2936ff053dbbSTrond Myklebust 	if (err < 0)
2937ff053dbbSTrond Myklebust 		return err;
2938da1661b9SChuck Lever 	err = xprt_sock_sendmsg(transport->sock, &msg, xdr, 0, marker, &sent);
2939da1661b9SChuck Lever 	xdr_free_bvec(xdr);
2940da1661b9SChuck Lever 	if (err < 0 || sent != (xdr->len + sizeof(marker)))
2941067fb11bSChuck Lever 		return -EAGAIN;
2942da1661b9SChuck Lever 	return sent;
29434cfc7e60SRahul Iyer }
29444cfc7e60SRahul Iyer 
2945ca4faf54SChuck Lever /**
2946ca4faf54SChuck Lever  * bc_send_request - Send a backchannel Call on a TCP socket
2947ca4faf54SChuck Lever  * @req: rpc_rqst containing Call message to be sent
2948ca4faf54SChuck Lever  *
2949ca4faf54SChuck Lever  * xpt_mutex ensures @rqstp's whole message is written to the socket
2950ca4faf54SChuck Lever  * without interruption.
2951ca4faf54SChuck Lever  *
2952ca4faf54SChuck Lever  * Return values:
2953ca4faf54SChuck Lever  *   %0 if the message was sent successfully
2954ca4faf54SChuck Lever  *   %ENOTCONN if the message was not sent
29554cfc7e60SRahul Iyer  */
2956adfa7144STrond Myklebust static int bc_send_request(struct rpc_rqst *req)
29574cfc7e60SRahul Iyer {
29584cfc7e60SRahul Iyer 	struct svc_xprt	*xprt;
29597fc56136SAndrzej Hajda 	int len;
29604cfc7e60SRahul Iyer 
29614cfc7e60SRahul Iyer 	/*
29624cfc7e60SRahul Iyer 	 * Get the server socket associated with this callback xprt
29634cfc7e60SRahul Iyer 	 */
29644cfc7e60SRahul Iyer 	xprt = req->rq_xprt->bc_xprt;
29654cfc7e60SRahul Iyer 
29664cfc7e60SRahul Iyer 	/*
29674cfc7e60SRahul Iyer 	 * Grab the mutex to serialize data as the connection is shared
29684cfc7e60SRahul Iyer 	 * with the fore channel
29694cfc7e60SRahul Iyer 	 */
2970c544577dSTrond Myklebust 	mutex_lock(&xprt->xpt_mutex);
29714cfc7e60SRahul Iyer 	if (test_bit(XPT_DEAD, &xprt->xpt_flags))
29724cfc7e60SRahul Iyer 		len = -ENOTCONN;
29734cfc7e60SRahul Iyer 	else
29744cfc7e60SRahul Iyer 		len = bc_sendto(req);
29754cfc7e60SRahul Iyer 	mutex_unlock(&xprt->xpt_mutex);
29764cfc7e60SRahul Iyer 
29774cfc7e60SRahul Iyer 	if (len > 0)
29784cfc7e60SRahul Iyer 		len = 0;
29794cfc7e60SRahul Iyer 
29804cfc7e60SRahul Iyer 	return len;
29814cfc7e60SRahul Iyer }
29824cfc7e60SRahul Iyer 
29834cfc7e60SRahul Iyer /*
29844cfc7e60SRahul Iyer  * The close routine. Since this is client initiated, we do nothing
29854cfc7e60SRahul Iyer  */
29864cfc7e60SRahul Iyer 
29874cfc7e60SRahul Iyer static void bc_close(struct rpc_xprt *xprt)
29884cfc7e60SRahul Iyer {
29896221f1d9SChuck Lever 	xprt_disconnect_done(xprt);
29904cfc7e60SRahul Iyer }
29914cfc7e60SRahul Iyer 
29924cfc7e60SRahul Iyer /*
29934cfc7e60SRahul Iyer  * The xprt destroy routine. Again, because this connection is client
29944cfc7e60SRahul Iyer  * initiated, we do nothing
29954cfc7e60SRahul Iyer  */
29964cfc7e60SRahul Iyer 
29974cfc7e60SRahul Iyer static void bc_destroy(struct rpc_xprt *xprt)
29984cfc7e60SRahul Iyer {
299947f72efaSKinglong Mee 	dprintk("RPC:       bc_destroy xprt %p\n", xprt);
300047f72efaSKinglong Mee 
300147f72efaSKinglong Mee 	xs_xprt_free(xprt);
300247f72efaSKinglong Mee 	module_put(THIS_MODULE);
30034cfc7e60SRahul Iyer }
30044cfc7e60SRahul Iyer 
3005d31ae254SChuck Lever static const struct rpc_xprt_ops xs_local_ops = {
3006176e21eeSChuck Lever 	.reserve_xprt		= xprt_reserve_xprt,
30074cd34e7cSTrond Myklebust 	.release_xprt		= xprt_release_xprt,
3008f39c1bfbSTrond Myklebust 	.alloc_slot		= xprt_alloc_slot,
3009a9cde23aSChuck Lever 	.free_slot		= xprt_free_slot,
3010176e21eeSChuck Lever 	.rpcbind		= xs_local_rpcbind,
3011176e21eeSChuck Lever 	.set_port		= xs_local_set_port,
3012dc107402SJ. Bruce Fields 	.connect		= xs_local_connect,
3013176e21eeSChuck Lever 	.buf_alloc		= rpc_malloc,
3014176e21eeSChuck Lever 	.buf_free		= rpc_free,
3015550aebfeSTrond Myklebust 	.prepare_request	= xs_stream_prepare_request,
3016176e21eeSChuck Lever 	.send_request		= xs_local_send_request,
30178ba6a92dSTrond Myklebust 	.wait_for_reply_request	= xprt_wait_for_reply_request_def,
3018176e21eeSChuck Lever 	.close			= xs_close,
3019a1311d87STrond Myklebust 	.destroy		= xs_destroy,
3020176e21eeSChuck Lever 	.print_stats		= xs_local_print_stats,
3021d67fa4d8SJeff Layton 	.enable_swap		= xs_enable_swap,
3022d67fa4d8SJeff Layton 	.disable_swap		= xs_disable_swap,
3023176e21eeSChuck Lever };
3024176e21eeSChuck Lever 
3025d31ae254SChuck Lever static const struct rpc_xprt_ops xs_udp_ops = {
302643118c29SChuck Lever 	.set_buffer_size	= xs_udp_set_buffer_size,
302712a80469SChuck Lever 	.reserve_xprt		= xprt_reserve_xprt_cong,
302849e9a890SChuck Lever 	.release_xprt		= xprt_release_xprt_cong,
3029f39c1bfbSTrond Myklebust 	.alloc_slot		= xprt_alloc_slot,
3030a9cde23aSChuck Lever 	.free_slot		= xprt_free_slot,
303145160d62SChuck Lever 	.rpcbind		= rpcb_getport_async,
303292200412SChuck Lever 	.set_port		= xs_set_port,
30339903cd1cSChuck Lever 	.connect		= xs_connect,
3034421ab1beSTrond Myklebust 	.get_srcaddr		= xs_sock_srcaddr,
3035421ab1beSTrond Myklebust 	.get_srcport		= xs_sock_srcport,
303602107148SChuck Lever 	.buf_alloc		= rpc_malloc,
303702107148SChuck Lever 	.buf_free		= rpc_free,
3038262965f5SChuck Lever 	.send_request		= xs_udp_send_request,
30398ba6a92dSTrond Myklebust 	.wait_for_reply_request	= xprt_wait_for_reply_request_rtt,
304046c0ee8bSChuck Lever 	.timer			= xs_udp_timer,
3041a58dd398SChuck Lever 	.release_request	= xprt_release_rqst_cong,
3042262965f5SChuck Lever 	.close			= xs_close,
3043262965f5SChuck Lever 	.destroy		= xs_destroy,
3044262ca07dSChuck Lever 	.print_stats		= xs_udp_print_stats,
3045d67fa4d8SJeff Layton 	.enable_swap		= xs_enable_swap,
3046d67fa4d8SJeff Layton 	.disable_swap		= xs_disable_swap,
30474a068258SChuck Lever 	.inject_disconnect	= xs_inject_disconnect,
3048262965f5SChuck Lever };
3049262965f5SChuck Lever 
3050d31ae254SChuck Lever static const struct rpc_xprt_ops xs_tcp_ops = {
305112a80469SChuck Lever 	.reserve_xprt		= xprt_reserve_xprt,
30524cd34e7cSTrond Myklebust 	.release_xprt		= xprt_release_xprt,
305336bd7de9STrond Myklebust 	.alloc_slot		= xprt_alloc_slot,
3054a9cde23aSChuck Lever 	.free_slot		= xprt_free_slot,
305545160d62SChuck Lever 	.rpcbind		= rpcb_getport_async,
305692200412SChuck Lever 	.set_port		= xs_set_port,
30570b9e7943STrond Myklebust 	.connect		= xs_connect,
3058421ab1beSTrond Myklebust 	.get_srcaddr		= xs_sock_srcaddr,
3059421ab1beSTrond Myklebust 	.get_srcport		= xs_sock_srcport,
306002107148SChuck Lever 	.buf_alloc		= rpc_malloc,
306102107148SChuck Lever 	.buf_free		= rpc_free,
3062277e4ab7STrond Myklebust 	.prepare_request	= xs_stream_prepare_request,
3063262965f5SChuck Lever 	.send_request		= xs_tcp_send_request,
30648ba6a92dSTrond Myklebust 	.wait_for_reply_request	= xprt_wait_for_reply_request_def,
3065c627d31bSTrond Myklebust 	.close			= xs_tcp_shutdown,
30669903cd1cSChuck Lever 	.destroy		= xs_destroy,
30677196dbb0STrond Myklebust 	.set_connect_timeout	= xs_tcp_set_connect_timeout,
3068262ca07dSChuck Lever 	.print_stats		= xs_tcp_print_stats,
3069d67fa4d8SJeff Layton 	.enable_swap		= xs_enable_swap,
3070d67fa4d8SJeff Layton 	.disable_swap		= xs_disable_swap,
30714a068258SChuck Lever 	.inject_disconnect	= xs_inject_disconnect,
307242e5c3e2SChuck Lever #ifdef CONFIG_SUNRPC_BACKCHANNEL
307342e5c3e2SChuck Lever 	.bc_setup		= xprt_setup_bc,
30746b26cc8cSChuck Lever 	.bc_maxpayload		= xs_tcp_bc_maxpayload,
30757402a4feSTrond Myklebust 	.bc_num_slots		= xprt_bc_max_slots,
307642e5c3e2SChuck Lever 	.bc_free_rqst		= xprt_free_bc_rqst,
307742e5c3e2SChuck Lever 	.bc_destroy		= xprt_destroy_bc,
307842e5c3e2SChuck Lever #endif
3079a246b010SChuck Lever };
3080a246b010SChuck Lever 
30814cfc7e60SRahul Iyer /*
30824cfc7e60SRahul Iyer  * The rpc_xprt_ops for the server backchannel
30834cfc7e60SRahul Iyer  */
30844cfc7e60SRahul Iyer 
3085d31ae254SChuck Lever static const struct rpc_xprt_ops bc_tcp_ops = {
30864cfc7e60SRahul Iyer 	.reserve_xprt		= xprt_reserve_xprt,
30874cfc7e60SRahul Iyer 	.release_xprt		= xprt_release_xprt,
308884e28a30SBryan Schumaker 	.alloc_slot		= xprt_alloc_slot,
3089a9cde23aSChuck Lever 	.free_slot		= xprt_free_slot,
30904cfc7e60SRahul Iyer 	.buf_alloc		= bc_malloc,
30914cfc7e60SRahul Iyer 	.buf_free		= bc_free,
30924cfc7e60SRahul Iyer 	.send_request		= bc_send_request,
30938ba6a92dSTrond Myklebust 	.wait_for_reply_request	= xprt_wait_for_reply_request_def,
30944cfc7e60SRahul Iyer 	.close			= bc_close,
30954cfc7e60SRahul Iyer 	.destroy		= bc_destroy,
30964cfc7e60SRahul Iyer 	.print_stats		= xs_tcp_print_stats,
3097d67fa4d8SJeff Layton 	.enable_swap		= xs_enable_swap,
3098d67fa4d8SJeff Layton 	.disable_swap		= xs_disable_swap,
30994a068258SChuck Lever 	.inject_disconnect	= xs_inject_disconnect,
31004cfc7e60SRahul Iyer };
31014cfc7e60SRahul Iyer 
310292476850SChuck Lever static int xs_init_anyaddr(const int family, struct sockaddr *sap)
310392476850SChuck Lever {
310492476850SChuck Lever 	static const struct sockaddr_in sin = {
310592476850SChuck Lever 		.sin_family		= AF_INET,
310692476850SChuck Lever 		.sin_addr.s_addr	= htonl(INADDR_ANY),
310792476850SChuck Lever 	};
310892476850SChuck Lever 	static const struct sockaddr_in6 sin6 = {
310992476850SChuck Lever 		.sin6_family		= AF_INET6,
311092476850SChuck Lever 		.sin6_addr		= IN6ADDR_ANY_INIT,
311192476850SChuck Lever 	};
311292476850SChuck Lever 
311392476850SChuck Lever 	switch (family) {
3114176e21eeSChuck Lever 	case AF_LOCAL:
3115176e21eeSChuck Lever 		break;
311692476850SChuck Lever 	case AF_INET:
311792476850SChuck Lever 		memcpy(sap, &sin, sizeof(sin));
311892476850SChuck Lever 		break;
311992476850SChuck Lever 	case AF_INET6:
312092476850SChuck Lever 		memcpy(sap, &sin6, sizeof(sin6));
312192476850SChuck Lever 		break;
312292476850SChuck Lever 	default:
312392476850SChuck Lever 		dprintk("RPC:       %s: Bad address family\n", __func__);
312492476850SChuck Lever 		return -EAFNOSUPPORT;
312592476850SChuck Lever 	}
312692476850SChuck Lever 	return 0;
312792476850SChuck Lever }
312892476850SChuck Lever 
31293c341b0bS\"Talpey, Thomas\ static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args,
3130d9ba131dSTrond Myklebust 				      unsigned int slot_table_size,
3131d9ba131dSTrond Myklebust 				      unsigned int max_slot_table_size)
3132c8541ecdSChuck Lever {
3133c8541ecdSChuck Lever 	struct rpc_xprt *xprt;
3134ffc2e518SChuck Lever 	struct sock_xprt *new;
3135c8541ecdSChuck Lever 
313696802a09SFrank van Maarseveen 	if (args->addrlen > sizeof(xprt->addr)) {
3137c8541ecdSChuck Lever 		dprintk("RPC:       xs_setup_xprt: address too large\n");
3138c8541ecdSChuck Lever 		return ERR_PTR(-EBADF);
3139c8541ecdSChuck Lever 	}
3140c8541ecdSChuck Lever 
3141d9ba131dSTrond Myklebust 	xprt = xprt_alloc(args->net, sizeof(*new), slot_table_size,
3142d9ba131dSTrond Myklebust 			max_slot_table_size);
3143bd1722d4SPavel Emelyanov 	if (xprt == NULL) {
314446121cf7SChuck Lever 		dprintk("RPC:       xs_setup_xprt: couldn't allocate "
314546121cf7SChuck Lever 				"rpc_xprt\n");
3146c8541ecdSChuck Lever 		return ERR_PTR(-ENOMEM);
3147c8541ecdSChuck Lever 	}
3148c8541ecdSChuck Lever 
3149bd1722d4SPavel Emelyanov 	new = container_of(xprt, struct sock_xprt, xprt);
3150edc1b01cSTrond Myklebust 	mutex_init(&new->recv_mutex);
315196802a09SFrank van Maarseveen 	memcpy(&xprt->addr, args->dstaddr, args->addrlen);
315296802a09SFrank van Maarseveen 	xprt->addrlen = args->addrlen;
3153d3bc9a1dSFrank van Maarseveen 	if (args->srcaddr)
3154fbfffbd5SChuck Lever 		memcpy(&new->srcaddr, args->srcaddr, args->addrlen);
315592476850SChuck Lever 	else {
315692476850SChuck Lever 		int err;
315792476850SChuck Lever 		err = xs_init_anyaddr(args->dstaddr->sa_family,
315892476850SChuck Lever 					(struct sockaddr *)&new->srcaddr);
31592aa13531SStanislav Kinsbursky 		if (err != 0) {
31602aa13531SStanislav Kinsbursky 			xprt_free(xprt);
316192476850SChuck Lever 			return ERR_PTR(err);
316292476850SChuck Lever 		}
31632aa13531SStanislav Kinsbursky 	}
3164c8541ecdSChuck Lever 
3165c8541ecdSChuck Lever 	return xprt;
3166c8541ecdSChuck Lever }
3167c8541ecdSChuck Lever 
3168176e21eeSChuck Lever static const struct rpc_timeout xs_local_default_timeout = {
3169176e21eeSChuck Lever 	.to_initval = 10 * HZ,
3170176e21eeSChuck Lever 	.to_maxval = 10 * HZ,
3171176e21eeSChuck Lever 	.to_retries = 2,
3172176e21eeSChuck Lever };
3173176e21eeSChuck Lever 
3174176e21eeSChuck Lever /**
3175176e21eeSChuck Lever  * xs_setup_local - Set up transport to use an AF_LOCAL socket
3176176e21eeSChuck Lever  * @args: rpc transport creation arguments
3177176e21eeSChuck Lever  *
3178176e21eeSChuck Lever  * AF_LOCAL is a "tpi_cots_ord" transport, just like TCP
3179176e21eeSChuck Lever  */
3180176e21eeSChuck Lever static struct rpc_xprt *xs_setup_local(struct xprt_create *args)
3181176e21eeSChuck Lever {
3182176e21eeSChuck Lever 	struct sockaddr_un *sun = (struct sockaddr_un *)args->dstaddr;
3183176e21eeSChuck Lever 	struct sock_xprt *transport;
3184176e21eeSChuck Lever 	struct rpc_xprt *xprt;
3185176e21eeSChuck Lever 	struct rpc_xprt *ret;
3186176e21eeSChuck Lever 
3187d9ba131dSTrond Myklebust 	xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
3188d9ba131dSTrond Myklebust 			xprt_max_tcp_slot_table_entries);
3189176e21eeSChuck Lever 	if (IS_ERR(xprt))
3190176e21eeSChuck Lever 		return xprt;
3191176e21eeSChuck Lever 	transport = container_of(xprt, struct sock_xprt, xprt);
3192176e21eeSChuck Lever 
3193176e21eeSChuck Lever 	xprt->prot = 0;
3194d3abc739SOlga Kornievskaia 	xprt->xprt_class = &xs_local_transport;
3195176e21eeSChuck Lever 	xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
3196176e21eeSChuck Lever 
3197176e21eeSChuck Lever 	xprt->bind_timeout = XS_BIND_TO;
3198176e21eeSChuck Lever 	xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
3199176e21eeSChuck Lever 	xprt->idle_timeout = XS_IDLE_DISC_TO;
3200176e21eeSChuck Lever 
3201176e21eeSChuck Lever 	xprt->ops = &xs_local_ops;
3202176e21eeSChuck Lever 	xprt->timeout = &xs_local_default_timeout;
3203176e21eeSChuck Lever 
3204550aebfeSTrond Myklebust 	INIT_WORK(&transport->recv_worker, xs_stream_data_receive_workfn);
32054f8943f8STrond Myklebust 	INIT_WORK(&transport->error_worker, xs_error_handle);
3206550aebfeSTrond Myklebust 	INIT_DELAYED_WORK(&transport->connect_worker, xs_dummy_setup_socket);
320793dc41bdSNeilBrown 
3208176e21eeSChuck Lever 	switch (sun->sun_family) {
3209176e21eeSChuck Lever 	case AF_LOCAL:
32104388ce05SNeilBrown 		if (sun->sun_path[0] != '/' && sun->sun_path[0] != '\0') {
3211176e21eeSChuck Lever 			dprintk("RPC:       bad AF_LOCAL address: %s\n",
3212176e21eeSChuck Lever 					sun->sun_path);
3213176e21eeSChuck Lever 			ret = ERR_PTR(-EINVAL);
3214176e21eeSChuck Lever 			goto out_err;
3215176e21eeSChuck Lever 		}
3216176e21eeSChuck Lever 		xprt_set_bound(xprt);
3217176e21eeSChuck Lever 		xs_format_peer_addresses(xprt, "local", RPCBIND_NETID_LOCAL);
3218176e21eeSChuck Lever 		break;
3219176e21eeSChuck Lever 	default:
3220176e21eeSChuck Lever 		ret = ERR_PTR(-EAFNOSUPPORT);
3221176e21eeSChuck Lever 		goto out_err;
3222176e21eeSChuck Lever 	}
3223176e21eeSChuck Lever 
3224176e21eeSChuck Lever 	dprintk("RPC:       set up xprt to %s via AF_LOCAL\n",
3225176e21eeSChuck Lever 			xprt->address_strings[RPC_DISPLAY_ADDR]);
3226176e21eeSChuck Lever 
3227176e21eeSChuck Lever 	if (try_module_get(THIS_MODULE))
3228176e21eeSChuck Lever 		return xprt;
3229176e21eeSChuck Lever 	ret = ERR_PTR(-EINVAL);
3230176e21eeSChuck Lever out_err:
3231315f3812SKinglong Mee 	xs_xprt_free(xprt);
3232176e21eeSChuck Lever 	return ret;
3233176e21eeSChuck Lever }
3234176e21eeSChuck Lever 
32352881ae74STrond Myklebust static const struct rpc_timeout xs_udp_default_timeout = {
32362881ae74STrond Myklebust 	.to_initval = 5 * HZ,
32372881ae74STrond Myklebust 	.to_maxval = 30 * HZ,
32382881ae74STrond Myklebust 	.to_increment = 5 * HZ,
32392881ae74STrond Myklebust 	.to_retries = 5,
32402881ae74STrond Myklebust };
32412881ae74STrond Myklebust 
32429903cd1cSChuck Lever /**
32439903cd1cSChuck Lever  * xs_setup_udp - Set up transport to use a UDP socket
324496802a09SFrank van Maarseveen  * @args: rpc transport creation arguments
32459903cd1cSChuck Lever  *
32469903cd1cSChuck Lever  */
3247483066d6SAdrian Bunk static struct rpc_xprt *xs_setup_udp(struct xprt_create *args)
3248a246b010SChuck Lever {
32498f9d5b1aSChuck Lever 	struct sockaddr *addr = args->dstaddr;
3250c8541ecdSChuck Lever 	struct rpc_xprt *xprt;
3251c8475461SChuck Lever 	struct sock_xprt *transport;
32520a68b0beSJ. Bruce Fields 	struct rpc_xprt *ret;
3253a246b010SChuck Lever 
3254d9ba131dSTrond Myklebust 	xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries,
3255d9ba131dSTrond Myklebust 			xprt_udp_slot_table_entries);
3256c8541ecdSChuck Lever 	if (IS_ERR(xprt))
3257c8541ecdSChuck Lever 		return xprt;
3258c8475461SChuck Lever 	transport = container_of(xprt, struct sock_xprt, xprt);
3259a246b010SChuck Lever 
3260ec739ef0SChuck Lever 	xprt->prot = IPPROTO_UDP;
3261d3abc739SOlga Kornievskaia 	xprt->xprt_class = &xs_udp_transport;
3262a246b010SChuck Lever 	/* XXX: header size can vary due to auth type, IPv6, etc. */
3263a246b010SChuck Lever 	xprt->max_payload = (1U << 16) - (MAX_HEADER << 3);
3264a246b010SChuck Lever 
326503bf4b70SChuck Lever 	xprt->bind_timeout = XS_BIND_TO;
326603bf4b70SChuck Lever 	xprt->reestablish_timeout = XS_UDP_REEST_TO;
326703bf4b70SChuck Lever 	xprt->idle_timeout = XS_IDLE_DISC_TO;
3268a246b010SChuck Lever 
3269262965f5SChuck Lever 	xprt->ops = &xs_udp_ops;
3270a246b010SChuck Lever 
3271ba7392bbSTrond Myklebust 	xprt->timeout = &xs_udp_default_timeout;
3272a246b010SChuck Lever 
3273f9b2ee71STrond Myklebust 	INIT_WORK(&transport->recv_worker, xs_udp_data_receive_workfn);
32744f8943f8STrond Myklebust 	INIT_WORK(&transport->error_worker, xs_error_handle);
3275edc1b01cSTrond Myklebust 	INIT_DELAYED_WORK(&transport->connect_worker, xs_udp_setup_socket);
3276edc1b01cSTrond Myklebust 
32778f9d5b1aSChuck Lever 	switch (addr->sa_family) {
32788f9d5b1aSChuck Lever 	case AF_INET:
32798f9d5b1aSChuck Lever 		if (((struct sockaddr_in *)addr)->sin_port != htons(0))
32808f9d5b1aSChuck Lever 			xprt_set_bound(xprt);
32818f9d5b1aSChuck Lever 
32829dc3b095SChuck Lever 		xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP);
32838f9d5b1aSChuck Lever 		break;
32848f9d5b1aSChuck Lever 	case AF_INET6:
32858f9d5b1aSChuck Lever 		if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
32868f9d5b1aSChuck Lever 			xprt_set_bound(xprt);
32878f9d5b1aSChuck Lever 
32889dc3b095SChuck Lever 		xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6);
32898f9d5b1aSChuck Lever 		break;
32908f9d5b1aSChuck Lever 	default:
32910a68b0beSJ. Bruce Fields 		ret = ERR_PTR(-EAFNOSUPPORT);
32920a68b0beSJ. Bruce Fields 		goto out_err;
32938f9d5b1aSChuck Lever 	}
32948f9d5b1aSChuck Lever 
3295c740eff8SChuck Lever 	if (xprt_bound(xprt))
3296c740eff8SChuck Lever 		dprintk("RPC:       set up xprt to %s (port %s) via %s\n",
3297c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_ADDR],
3298c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_PORT],
3299c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_PROTO]);
3300c740eff8SChuck Lever 	else
3301c740eff8SChuck Lever 		dprintk("RPC:       set up xprt to %s (autobind) via %s\n",
3302c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_ADDR],
3303c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_PROTO]);
3304edb267a6SChuck Lever 
3305bc25571eS\"Talpey, Thomas\ 	if (try_module_get(THIS_MODULE))
3306c8541ecdSChuck Lever 		return xprt;
33070a68b0beSJ. Bruce Fields 	ret = ERR_PTR(-EINVAL);
33080a68b0beSJ. Bruce Fields out_err:
3309315f3812SKinglong Mee 	xs_xprt_free(xprt);
33100a68b0beSJ. Bruce Fields 	return ret;
3311a246b010SChuck Lever }
3312a246b010SChuck Lever 
33132881ae74STrond Myklebust static const struct rpc_timeout xs_tcp_default_timeout = {
33142881ae74STrond Myklebust 	.to_initval = 60 * HZ,
33152881ae74STrond Myklebust 	.to_maxval = 60 * HZ,
33162881ae74STrond Myklebust 	.to_retries = 2,
33172881ae74STrond Myklebust };
33182881ae74STrond Myklebust 
33199903cd1cSChuck Lever /**
33209903cd1cSChuck Lever  * xs_setup_tcp - Set up transport to use a TCP socket
332196802a09SFrank van Maarseveen  * @args: rpc transport creation arguments
33229903cd1cSChuck Lever  *
33239903cd1cSChuck Lever  */
3324483066d6SAdrian Bunk static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args)
3325a246b010SChuck Lever {
33268f9d5b1aSChuck Lever 	struct sockaddr *addr = args->dstaddr;
3327c8541ecdSChuck Lever 	struct rpc_xprt *xprt;
3328c8475461SChuck Lever 	struct sock_xprt *transport;
33290a68b0beSJ. Bruce Fields 	struct rpc_xprt *ret;
3330b7993cebSTrond Myklebust 	unsigned int max_slot_table_size = xprt_max_tcp_slot_table_entries;
3331b7993cebSTrond Myklebust 
3332b7993cebSTrond Myklebust 	if (args->flags & XPRT_CREATE_INFINITE_SLOTS)
3333b7993cebSTrond Myklebust 		max_slot_table_size = RPC_MAX_SLOT_TABLE_LIMIT;
3334a246b010SChuck Lever 
3335d9ba131dSTrond Myklebust 	xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
3336b7993cebSTrond Myklebust 			max_slot_table_size);
3337c8541ecdSChuck Lever 	if (IS_ERR(xprt))
3338c8541ecdSChuck Lever 		return xprt;
3339c8475461SChuck Lever 	transport = container_of(xprt, struct sock_xprt, xprt);
3340a246b010SChuck Lever 
3341ec739ef0SChuck Lever 	xprt->prot = IPPROTO_TCP;
3342d3abc739SOlga Kornievskaia 	xprt->xprt_class = &xs_tcp_transport;
3343808012fbSChuck Lever 	xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
3344a246b010SChuck Lever 
334503bf4b70SChuck Lever 	xprt->bind_timeout = XS_BIND_TO;
334603bf4b70SChuck Lever 	xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
334703bf4b70SChuck Lever 	xprt->idle_timeout = XS_IDLE_DISC_TO;
3348a246b010SChuck Lever 
3349262965f5SChuck Lever 	xprt->ops = &xs_tcp_ops;
3350ba7392bbSTrond Myklebust 	xprt->timeout = &xs_tcp_default_timeout;
3351a246b010SChuck Lever 
33523851f1cdSTrond Myklebust 	xprt->max_reconnect_timeout = xprt->timeout->to_maxval;
33537196dbb0STrond Myklebust 	xprt->connect_timeout = xprt->timeout->to_initval *
33547196dbb0STrond Myklebust 		(xprt->timeout->to_retries + 1);
33553851f1cdSTrond Myklebust 
3356c50b8ee0STrond Myklebust 	INIT_WORK(&transport->recv_worker, xs_stream_data_receive_workfn);
33574f8943f8STrond Myklebust 	INIT_WORK(&transport->error_worker, xs_error_handle);
3358edc1b01cSTrond Myklebust 	INIT_DELAYED_WORK(&transport->connect_worker, xs_tcp_setup_socket);
3359edc1b01cSTrond Myklebust 
33608f9d5b1aSChuck Lever 	switch (addr->sa_family) {
33618f9d5b1aSChuck Lever 	case AF_INET:
33628f9d5b1aSChuck Lever 		if (((struct sockaddr_in *)addr)->sin_port != htons(0))
33638f9d5b1aSChuck Lever 			xprt_set_bound(xprt);
33648f9d5b1aSChuck Lever 
33659dc3b095SChuck Lever 		xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP);
33668f9d5b1aSChuck Lever 		break;
33678f9d5b1aSChuck Lever 	case AF_INET6:
33688f9d5b1aSChuck Lever 		if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
33698f9d5b1aSChuck Lever 			xprt_set_bound(xprt);
33708f9d5b1aSChuck Lever 
33719dc3b095SChuck Lever 		xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6);
33728f9d5b1aSChuck Lever 		break;
33738f9d5b1aSChuck Lever 	default:
33740a68b0beSJ. Bruce Fields 		ret = ERR_PTR(-EAFNOSUPPORT);
33750a68b0beSJ. Bruce Fields 		goto out_err;
33768f9d5b1aSChuck Lever 	}
33778f9d5b1aSChuck Lever 
3378c740eff8SChuck Lever 	if (xprt_bound(xprt))
3379c740eff8SChuck Lever 		dprintk("RPC:       set up xprt to %s (port %s) via %s\n",
3380c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_ADDR],
3381c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_PORT],
3382c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_PROTO]);
3383c740eff8SChuck Lever 	else
3384c740eff8SChuck Lever 		dprintk("RPC:       set up xprt to %s (autobind) via %s\n",
3385c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_ADDR],
3386c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_PROTO]);
3387c740eff8SChuck Lever 
3388bc25571eS\"Talpey, Thomas\ 	if (try_module_get(THIS_MODULE))
3389c8541ecdSChuck Lever 		return xprt;
33900a68b0beSJ. Bruce Fields 	ret = ERR_PTR(-EINVAL);
33910a68b0beSJ. Bruce Fields out_err:
3392315f3812SKinglong Mee 	xs_xprt_free(xprt);
33930a68b0beSJ. Bruce Fields 	return ret;
3394a246b010SChuck Lever }
3395282b32e1SChuck Lever 
3396f300babaSAlexandros Batsakis /**
339775eb6af7SChuck Lever  * xs_setup_tcp_tls - Set up transport to use a TCP with TLS
339875eb6af7SChuck Lever  * @args: rpc transport creation arguments
339975eb6af7SChuck Lever  *
340075eb6af7SChuck Lever  */
340175eb6af7SChuck Lever static struct rpc_xprt *xs_setup_tcp_tls(struct xprt_create *args)
340275eb6af7SChuck Lever {
340375eb6af7SChuck Lever 	struct sockaddr *addr = args->dstaddr;
340475eb6af7SChuck Lever 	struct rpc_xprt *xprt;
340575eb6af7SChuck Lever 	struct sock_xprt *transport;
340675eb6af7SChuck Lever 	struct rpc_xprt *ret;
340775eb6af7SChuck Lever 	unsigned int max_slot_table_size = xprt_max_tcp_slot_table_entries;
340875eb6af7SChuck Lever 
340975eb6af7SChuck Lever 	if (args->flags & XPRT_CREATE_INFINITE_SLOTS)
341075eb6af7SChuck Lever 		max_slot_table_size = RPC_MAX_SLOT_TABLE_LIMIT;
341175eb6af7SChuck Lever 
341275eb6af7SChuck Lever 	xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
341375eb6af7SChuck Lever 			     max_slot_table_size);
341475eb6af7SChuck Lever 	if (IS_ERR(xprt))
341575eb6af7SChuck Lever 		return xprt;
341675eb6af7SChuck Lever 	transport = container_of(xprt, struct sock_xprt, xprt);
341775eb6af7SChuck Lever 
341875eb6af7SChuck Lever 	xprt->prot = IPPROTO_TCP;
341975eb6af7SChuck Lever 	xprt->xprt_class = &xs_tcp_transport;
342075eb6af7SChuck Lever 	xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
342175eb6af7SChuck Lever 
342275eb6af7SChuck Lever 	xprt->bind_timeout = XS_BIND_TO;
342375eb6af7SChuck Lever 	xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
342475eb6af7SChuck Lever 	xprt->idle_timeout = XS_IDLE_DISC_TO;
342575eb6af7SChuck Lever 
342675eb6af7SChuck Lever 	xprt->ops = &xs_tcp_ops;
342775eb6af7SChuck Lever 	xprt->timeout = &xs_tcp_default_timeout;
342875eb6af7SChuck Lever 
342975eb6af7SChuck Lever 	xprt->max_reconnect_timeout = xprt->timeout->to_maxval;
343075eb6af7SChuck Lever 	xprt->connect_timeout = xprt->timeout->to_initval *
343175eb6af7SChuck Lever 		(xprt->timeout->to_retries + 1);
343275eb6af7SChuck Lever 
343375eb6af7SChuck Lever 	INIT_WORK(&transport->recv_worker, xs_stream_data_receive_workfn);
343475eb6af7SChuck Lever 	INIT_WORK(&transport->error_worker, xs_error_handle);
343575eb6af7SChuck Lever 
343675eb6af7SChuck Lever 	switch (args->xprtsec.policy) {
343775eb6af7SChuck Lever 	case RPC_XPRTSEC_TLS_ANON:
343875eb6af7SChuck Lever 	case RPC_XPRTSEC_TLS_X509:
343975eb6af7SChuck Lever 		xprt->xprtsec = args->xprtsec;
344075eb6af7SChuck Lever 		INIT_DELAYED_WORK(&transport->connect_worker,
344175eb6af7SChuck Lever 				  xs_tcp_tls_setup_socket);
344275eb6af7SChuck Lever 		break;
344375eb6af7SChuck Lever 	default:
344475eb6af7SChuck Lever 		ret = ERR_PTR(-EACCES);
344575eb6af7SChuck Lever 		goto out_err;
344675eb6af7SChuck Lever 	}
344775eb6af7SChuck Lever 
344875eb6af7SChuck Lever 	switch (addr->sa_family) {
344975eb6af7SChuck Lever 	case AF_INET:
345075eb6af7SChuck Lever 		if (((struct sockaddr_in *)addr)->sin_port != htons(0))
345175eb6af7SChuck Lever 			xprt_set_bound(xprt);
345275eb6af7SChuck Lever 
345375eb6af7SChuck Lever 		xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP);
345475eb6af7SChuck Lever 		break;
345575eb6af7SChuck Lever 	case AF_INET6:
345675eb6af7SChuck Lever 		if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
345775eb6af7SChuck Lever 			xprt_set_bound(xprt);
345875eb6af7SChuck Lever 
345975eb6af7SChuck Lever 		xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6);
346075eb6af7SChuck Lever 		break;
346175eb6af7SChuck Lever 	default:
346275eb6af7SChuck Lever 		ret = ERR_PTR(-EAFNOSUPPORT);
346375eb6af7SChuck Lever 		goto out_err;
346475eb6af7SChuck Lever 	}
346575eb6af7SChuck Lever 
346675eb6af7SChuck Lever 	if (xprt_bound(xprt))
346775eb6af7SChuck Lever 		dprintk("RPC:       set up xprt to %s (port %s) via %s\n",
346875eb6af7SChuck Lever 			xprt->address_strings[RPC_DISPLAY_ADDR],
346975eb6af7SChuck Lever 			xprt->address_strings[RPC_DISPLAY_PORT],
347075eb6af7SChuck Lever 			xprt->address_strings[RPC_DISPLAY_PROTO]);
347175eb6af7SChuck Lever 	else
347275eb6af7SChuck Lever 		dprintk("RPC:       set up xprt to %s (autobind) via %s\n",
347375eb6af7SChuck Lever 			xprt->address_strings[RPC_DISPLAY_ADDR],
347475eb6af7SChuck Lever 			xprt->address_strings[RPC_DISPLAY_PROTO]);
347575eb6af7SChuck Lever 
347675eb6af7SChuck Lever 	if (try_module_get(THIS_MODULE))
347775eb6af7SChuck Lever 		return xprt;
347875eb6af7SChuck Lever 	ret = ERR_PTR(-EINVAL);
347975eb6af7SChuck Lever out_err:
348075eb6af7SChuck Lever 	xs_xprt_free(xprt);
348175eb6af7SChuck Lever 	return ret;
348275eb6af7SChuck Lever }
348375eb6af7SChuck Lever 
348475eb6af7SChuck Lever /**
3485f300babaSAlexandros Batsakis  * xs_setup_bc_tcp - Set up transport to use a TCP backchannel socket
3486f300babaSAlexandros Batsakis  * @args: rpc transport creation arguments
3487f300babaSAlexandros Batsakis  *
3488f300babaSAlexandros Batsakis  */
3489f300babaSAlexandros Batsakis static struct rpc_xprt *xs_setup_bc_tcp(struct xprt_create *args)
3490f300babaSAlexandros Batsakis {
3491f300babaSAlexandros Batsakis 	struct sockaddr *addr = args->dstaddr;
3492f300babaSAlexandros Batsakis 	struct rpc_xprt *xprt;
3493f300babaSAlexandros Batsakis 	struct sock_xprt *transport;
3494f300babaSAlexandros Batsakis 	struct svc_sock *bc_sock;
34950a68b0beSJ. Bruce Fields 	struct rpc_xprt *ret;
3496f300babaSAlexandros Batsakis 
3497d9ba131dSTrond Myklebust 	xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
3498d9ba131dSTrond Myklebust 			xprt_tcp_slot_table_entries);
3499f300babaSAlexandros Batsakis 	if (IS_ERR(xprt))
3500f300babaSAlexandros Batsakis 		return xprt;
3501f300babaSAlexandros Batsakis 	transport = container_of(xprt, struct sock_xprt, xprt);
3502f300babaSAlexandros Batsakis 
3503f300babaSAlexandros Batsakis 	xprt->prot = IPPROTO_TCP;
3504d3abc739SOlga Kornievskaia 	xprt->xprt_class = &xs_bc_tcp_transport;
3505f300babaSAlexandros Batsakis 	xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
3506f300babaSAlexandros Batsakis 	xprt->timeout = &xs_tcp_default_timeout;
3507f300babaSAlexandros Batsakis 
3508f300babaSAlexandros Batsakis 	/* backchannel */
3509f300babaSAlexandros Batsakis 	xprt_set_bound(xprt);
3510f300babaSAlexandros Batsakis 	xprt->bind_timeout = 0;
3511f300babaSAlexandros Batsakis 	xprt->reestablish_timeout = 0;
3512f300babaSAlexandros Batsakis 	xprt->idle_timeout = 0;
3513f300babaSAlexandros Batsakis 
3514f300babaSAlexandros Batsakis 	xprt->ops = &bc_tcp_ops;
3515f300babaSAlexandros Batsakis 
3516f300babaSAlexandros Batsakis 	switch (addr->sa_family) {
3517f300babaSAlexandros Batsakis 	case AF_INET:
3518f300babaSAlexandros Batsakis 		xs_format_peer_addresses(xprt, "tcp",
3519f300babaSAlexandros Batsakis 					 RPCBIND_NETID_TCP);
3520f300babaSAlexandros Batsakis 		break;
3521f300babaSAlexandros Batsakis 	case AF_INET6:
3522f300babaSAlexandros Batsakis 		xs_format_peer_addresses(xprt, "tcp",
3523f300babaSAlexandros Batsakis 				   RPCBIND_NETID_TCP6);
3524f300babaSAlexandros Batsakis 		break;
3525f300babaSAlexandros Batsakis 	default:
35260a68b0beSJ. Bruce Fields 		ret = ERR_PTR(-EAFNOSUPPORT);
35270a68b0beSJ. Bruce Fields 		goto out_err;
3528f300babaSAlexandros Batsakis 	}
3529f300babaSAlexandros Batsakis 
3530f300babaSAlexandros Batsakis 	dprintk("RPC:       set up xprt to %s (port %s) via %s\n",
3531f300babaSAlexandros Batsakis 			xprt->address_strings[RPC_DISPLAY_ADDR],
3532f300babaSAlexandros Batsakis 			xprt->address_strings[RPC_DISPLAY_PORT],
3533f300babaSAlexandros Batsakis 			xprt->address_strings[RPC_DISPLAY_PROTO]);
3534f300babaSAlexandros Batsakis 
3535f300babaSAlexandros Batsakis 	/*
353699de8ea9SJ. Bruce Fields 	 * Once we've associated a backchannel xprt with a connection,
353728303ca3SWeng Meiling 	 * we want to keep it around as long as the connection lasts,
353828303ca3SWeng Meiling 	 * in case we need to start using it for a backchannel again;
353928303ca3SWeng Meiling 	 * this reference won't be dropped until bc_xprt is destroyed.
354099de8ea9SJ. Bruce Fields 	 */
354199de8ea9SJ. Bruce Fields 	xprt_get(xprt);
354299de8ea9SJ. Bruce Fields 	args->bc_xprt->xpt_bc_xprt = xprt;
354399de8ea9SJ. Bruce Fields 	xprt->bc_xprt = args->bc_xprt;
354499de8ea9SJ. Bruce Fields 	bc_sock = container_of(args->bc_xprt, struct svc_sock, sk_xprt);
354599de8ea9SJ. Bruce Fields 	transport->sock = bc_sock->sk_sock;
354699de8ea9SJ. Bruce Fields 	transport->inet = bc_sock->sk_sk;
354799de8ea9SJ. Bruce Fields 
354899de8ea9SJ. Bruce Fields 	/*
3549f300babaSAlexandros Batsakis 	 * Since we don't want connections for the backchannel, we set
3550f300babaSAlexandros Batsakis 	 * the xprt status to connected
3551f300babaSAlexandros Batsakis 	 */
3552f300babaSAlexandros Batsakis 	xprt_set_connected(xprt);
3553f300babaSAlexandros Batsakis 
3554f300babaSAlexandros Batsakis 	if (try_module_get(THIS_MODULE))
3555f300babaSAlexandros Batsakis 		return xprt;
3556642aab58SKinglong Mee 
3557642aab58SKinglong Mee 	args->bc_xprt->xpt_bc_xprt = NULL;
355839a9beabSJ. Bruce Fields 	args->bc_xprt->xpt_bc_xps = NULL;
355999de8ea9SJ. Bruce Fields 	xprt_put(xprt);
35600a68b0beSJ. Bruce Fields 	ret = ERR_PTR(-EINVAL);
35610a68b0beSJ. Bruce Fields out_err:
3562315f3812SKinglong Mee 	xs_xprt_free(xprt);
35630a68b0beSJ. Bruce Fields 	return ret;
3564f300babaSAlexandros Batsakis }
3565f300babaSAlexandros Batsakis 
3566176e21eeSChuck Lever static struct xprt_class	xs_local_transport = {
3567176e21eeSChuck Lever 	.list		= LIST_HEAD_INIT(xs_local_transport.list),
3568176e21eeSChuck Lever 	.name		= "named UNIX socket",
3569176e21eeSChuck Lever 	.owner		= THIS_MODULE,
3570176e21eeSChuck Lever 	.ident		= XPRT_TRANSPORT_LOCAL,
3571176e21eeSChuck Lever 	.setup		= xs_setup_local,
3572d5aa6b22STrond Myklebust 	.netid		= { "" },
3573176e21eeSChuck Lever };
3574176e21eeSChuck Lever 
3575bc25571eS\"Talpey, Thomas\ static struct xprt_class	xs_udp_transport = {
3576bc25571eS\"Talpey, Thomas\ 	.list		= LIST_HEAD_INIT(xs_udp_transport.list),
3577bc25571eS\"Talpey, Thomas\ 	.name		= "udp",
3578bc25571eS\"Talpey, Thomas\ 	.owner		= THIS_MODULE,
3579f300babaSAlexandros Batsakis 	.ident		= XPRT_TRANSPORT_UDP,
3580bc25571eS\"Talpey, Thomas\ 	.setup		= xs_setup_udp,
3581d5aa6b22STrond Myklebust 	.netid		= { "udp", "udp6", "" },
3582bc25571eS\"Talpey, Thomas\ };
3583bc25571eS\"Talpey, Thomas\ 
3584bc25571eS\"Talpey, Thomas\ static struct xprt_class	xs_tcp_transport = {
3585bc25571eS\"Talpey, Thomas\ 	.list		= LIST_HEAD_INIT(xs_tcp_transport.list),
3586bc25571eS\"Talpey, Thomas\ 	.name		= "tcp",
3587bc25571eS\"Talpey, Thomas\ 	.owner		= THIS_MODULE,
3588f300babaSAlexandros Batsakis 	.ident		= XPRT_TRANSPORT_TCP,
3589bc25571eS\"Talpey, Thomas\ 	.setup		= xs_setup_tcp,
3590d5aa6b22STrond Myklebust 	.netid		= { "tcp", "tcp6", "" },
3591bc25571eS\"Talpey, Thomas\ };
3592bc25571eS\"Talpey, Thomas\ 
359375eb6af7SChuck Lever static struct xprt_class	xs_tcp_tls_transport = {
359475eb6af7SChuck Lever 	.list		= LIST_HEAD_INIT(xs_tcp_tls_transport.list),
359575eb6af7SChuck Lever 	.name		= "tcp-with-tls",
359675eb6af7SChuck Lever 	.owner		= THIS_MODULE,
359775eb6af7SChuck Lever 	.ident		= XPRT_TRANSPORT_TCP_TLS,
359875eb6af7SChuck Lever 	.setup		= xs_setup_tcp_tls,
359975eb6af7SChuck Lever 	.netid		= { "tcp", "tcp6", "" },
360075eb6af7SChuck Lever };
360175eb6af7SChuck Lever 
3602f300babaSAlexandros Batsakis static struct xprt_class	xs_bc_tcp_transport = {
3603f300babaSAlexandros Batsakis 	.list		= LIST_HEAD_INIT(xs_bc_tcp_transport.list),
3604f300babaSAlexandros Batsakis 	.name		= "tcp NFSv4.1 backchannel",
3605f300babaSAlexandros Batsakis 	.owner		= THIS_MODULE,
3606f300babaSAlexandros Batsakis 	.ident		= XPRT_TRANSPORT_BC_TCP,
3607f300babaSAlexandros Batsakis 	.setup		= xs_setup_bc_tcp,
3608d5aa6b22STrond Myklebust 	.netid		= { "" },
3609f300babaSAlexandros Batsakis };
3610f300babaSAlexandros Batsakis 
3611282b32e1SChuck Lever /**
3612bc25571eS\"Talpey, Thomas\  * init_socket_xprt - set up xprtsock's sysctls, register with RPC client
3613282b32e1SChuck Lever  *
3614282b32e1SChuck Lever  */
3615282b32e1SChuck Lever int init_socket_xprt(void)
3616282b32e1SChuck Lever {
36172b1bec5fSEric W. Biederman 	if (!sunrpc_table_header)
3618c946cb69SLuis Chamberlain 		sunrpc_table_header = register_sysctl("sunrpc", xs_tunables_table);
3619fbf76683SChuck Lever 
3620176e21eeSChuck Lever 	xprt_register_transport(&xs_local_transport);
3621bc25571eS\"Talpey, Thomas\ 	xprt_register_transport(&xs_udp_transport);
3622bc25571eS\"Talpey, Thomas\ 	xprt_register_transport(&xs_tcp_transport);
362375eb6af7SChuck Lever 	xprt_register_transport(&xs_tcp_tls_transport);
3624f300babaSAlexandros Batsakis 	xprt_register_transport(&xs_bc_tcp_transport);
3625bc25571eS\"Talpey, Thomas\ 
3626282b32e1SChuck Lever 	return 0;
3627282b32e1SChuck Lever }
3628282b32e1SChuck Lever 
3629282b32e1SChuck Lever /**
3630bc25571eS\"Talpey, Thomas\  * cleanup_socket_xprt - remove xprtsock's sysctls, unregister
3631282b32e1SChuck Lever  *
3632282b32e1SChuck Lever  */
3633282b32e1SChuck Lever void cleanup_socket_xprt(void)
3634282b32e1SChuck Lever {
3635fbf76683SChuck Lever 	if (sunrpc_table_header) {
3636fbf76683SChuck Lever 		unregister_sysctl_table(sunrpc_table_header);
3637fbf76683SChuck Lever 		sunrpc_table_header = NULL;
3638fbf76683SChuck Lever 	}
3639bc25571eS\"Talpey, Thomas\ 
3640176e21eeSChuck Lever 	xprt_unregister_transport(&xs_local_transport);
3641bc25571eS\"Talpey, Thomas\ 	xprt_unregister_transport(&xs_udp_transport);
3642bc25571eS\"Talpey, Thomas\ 	xprt_unregister_transport(&xs_tcp_transport);
364375eb6af7SChuck Lever 	xprt_unregister_transport(&xs_tcp_tls_transport);
3644f300babaSAlexandros Batsakis 	xprt_unregister_transport(&xs_bc_tcp_transport);
3645282b32e1SChuck Lever }
3646cbf11071STrond Myklebust 
36479bbb9e5aSRusty Russell static int param_set_portnr(const char *val, const struct kernel_param *kp)
3648cbf11071STrond Myklebust {
3649cbf11071STrond Myklebust 	return param_set_uint_minmax(val, kp,
3650cbf11071STrond Myklebust 			RPC_MIN_RESVPORT,
3651cbf11071STrond Myklebust 			RPC_MAX_RESVPORT);
3652cbf11071STrond Myklebust }
3653cbf11071STrond Myklebust 
36549c27847dSLuis R. Rodriguez static const struct kernel_param_ops param_ops_portnr = {
36559bbb9e5aSRusty Russell 	.set = param_set_portnr,
36569bbb9e5aSRusty Russell 	.get = param_get_uint,
36579bbb9e5aSRusty Russell };
36589bbb9e5aSRusty Russell 
3659cbf11071STrond Myklebust #define param_check_portnr(name, p) \
3660cbf11071STrond Myklebust 	__param_check(name, p, unsigned int);
3661cbf11071STrond Myklebust 
3662cbf11071STrond Myklebust module_param_named(min_resvport, xprt_min_resvport, portnr, 0644);
3663cbf11071STrond Myklebust module_param_named(max_resvport, xprt_max_resvport, portnr, 0644);
3664cbf11071STrond Myklebust 
36659bbb9e5aSRusty Russell static int param_set_slot_table_size(const char *val,
36669bbb9e5aSRusty Russell 				     const struct kernel_param *kp)
3667cbf11071STrond Myklebust {
3668cbf11071STrond Myklebust 	return param_set_uint_minmax(val, kp,
3669cbf11071STrond Myklebust 			RPC_MIN_SLOT_TABLE,
3670cbf11071STrond Myklebust 			RPC_MAX_SLOT_TABLE);
3671cbf11071STrond Myklebust }
3672cbf11071STrond Myklebust 
36739c27847dSLuis R. Rodriguez static const struct kernel_param_ops param_ops_slot_table_size = {
36749bbb9e5aSRusty Russell 	.set = param_set_slot_table_size,
36759bbb9e5aSRusty Russell 	.get = param_get_uint,
36769bbb9e5aSRusty Russell };
36779bbb9e5aSRusty Russell 
3678cbf11071STrond Myklebust #define param_check_slot_table_size(name, p) \
3679cbf11071STrond Myklebust 	__param_check(name, p, unsigned int);
3680cbf11071STrond Myklebust 
3681d9ba131dSTrond Myklebust static int param_set_max_slot_table_size(const char *val,
3682d9ba131dSTrond Myklebust 				     const struct kernel_param *kp)
3683d9ba131dSTrond Myklebust {
3684d9ba131dSTrond Myklebust 	return param_set_uint_minmax(val, kp,
3685d9ba131dSTrond Myklebust 			RPC_MIN_SLOT_TABLE,
3686d9ba131dSTrond Myklebust 			RPC_MAX_SLOT_TABLE_LIMIT);
3687d9ba131dSTrond Myklebust }
3688d9ba131dSTrond Myklebust 
36899c27847dSLuis R. Rodriguez static const struct kernel_param_ops param_ops_max_slot_table_size = {
3690d9ba131dSTrond Myklebust 	.set = param_set_max_slot_table_size,
3691d9ba131dSTrond Myklebust 	.get = param_get_uint,
3692d9ba131dSTrond Myklebust };
3693d9ba131dSTrond Myklebust 
3694d9ba131dSTrond Myklebust #define param_check_max_slot_table_size(name, p) \
3695d9ba131dSTrond Myklebust 	__param_check(name, p, unsigned int);
3696d9ba131dSTrond Myklebust 
3697cbf11071STrond Myklebust module_param_named(tcp_slot_table_entries, xprt_tcp_slot_table_entries,
3698cbf11071STrond Myklebust 		   slot_table_size, 0644);
3699d9ba131dSTrond Myklebust module_param_named(tcp_max_slot_table_entries, xprt_max_tcp_slot_table_entries,
3700d9ba131dSTrond Myklebust 		   max_slot_table_size, 0644);
3701cbf11071STrond Myklebust module_param_named(udp_slot_table_entries, xprt_udp_slot_table_entries,
3702cbf11071STrond Myklebust 		   slot_table_size, 0644);
3703