xref: /openbmc/linux/net/sunrpc/xprtsock.c (revision 5dd5ad68)
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>
516a7eccefSChuck Lever #include <net/tls_prot.h>
5275eb6af7SChuck Lever #include <net/handshake.h>
53dea034b9SChuck Lever 
54277e4ab7STrond Myklebust #include <linux/bvec.h>
556a829eb8STrond Myklebust #include <linux/highmem.h>
56277e4ab7STrond Myklebust #include <linux/uio.h>
57a1231fdaSTrond Myklebust #include <linux/sched/mm.h>
58a246b010SChuck Lever 
5940e0b090SPeilin Ye #include <trace/events/sock.h>
6040b5ea0cSTrond Myklebust #include <trace/events/sunrpc.h>
6140b5ea0cSTrond Myklebust 
629e55eef4SChuck Lever #include "socklib.h"
634cfc7e60SRahul Iyer #include "sunrpc.h"
64176e21eeSChuck Lever 
65176e21eeSChuck Lever static void xs_close(struct rpc_xprt *xprt);
663b21f757STrond Myklebust static void xs_set_srcport(struct sock_xprt *transport, struct socket *sock);
677196dbb0STrond Myklebust static void xs_tcp_set_socket_timeouts(struct rpc_xprt *xprt,
687196dbb0STrond Myklebust 		struct socket *sock);
69176e21eeSChuck Lever 
709903cd1cSChuck Lever /*
71c556b754SChuck Lever  * xprtsock tunables
72c556b754SChuck Lever  */
7309acfea5STrond Myklebust static unsigned int xprt_udp_slot_table_entries = RPC_DEF_SLOT_TABLE;
7409acfea5STrond Myklebust static unsigned int xprt_tcp_slot_table_entries = RPC_MIN_SLOT_TABLE;
7509acfea5STrond Myklebust static unsigned int xprt_max_tcp_slot_table_entries = RPC_MAX_SLOT_TABLE;
76c556b754SChuck Lever 
7709acfea5STrond Myklebust static unsigned int xprt_min_resvport = RPC_DEF_MIN_RESVPORT;
7809acfea5STrond Myklebust static unsigned int xprt_max_resvport = RPC_DEF_MAX_RESVPORT;
79c556b754SChuck Lever 
807d1e8255STrond Myklebust #define XS_TCP_LINGER_TO	(15U * HZ)
8125fe6142STrond Myklebust static unsigned int xs_tcp_fin_timeout __read_mostly = XS_TCP_LINGER_TO;
827d1e8255STrond Myklebust 
83c556b754SChuck Lever /*
84fbf76683SChuck Lever  * We can register our own files under /proc/sys/sunrpc by
85c946cb69SLuis Chamberlain  * calling register_sysctl() again.  The files in that
86fbf76683SChuck Lever  * directory become the union of all files registered there.
87fbf76683SChuck Lever  *
88fbf76683SChuck Lever  * We simply need to make sure that we don't collide with
89fbf76683SChuck Lever  * someone else's file names!
90fbf76683SChuck Lever  */
91fbf76683SChuck Lever 
92fbf76683SChuck Lever static unsigned int min_slot_table_size = RPC_MIN_SLOT_TABLE;
93fbf76683SChuck Lever static unsigned int max_slot_table_size = RPC_MAX_SLOT_TABLE;
94d9ba131dSTrond Myklebust static unsigned int max_tcp_slot_table_limit = RPC_MAX_SLOT_TABLE_LIMIT;
95fbf76683SChuck Lever static unsigned int xprt_min_resvport_limit = RPC_MIN_RESVPORT;
96fbf76683SChuck Lever static unsigned int xprt_max_resvport_limit = RPC_MAX_RESVPORT;
97fbf76683SChuck Lever 
98fbf76683SChuck Lever static struct ctl_table_header *sunrpc_table_header;
99fbf76683SChuck Lever 
100d3abc739SOlga Kornievskaia static struct xprt_class xs_local_transport;
101d3abc739SOlga Kornievskaia static struct xprt_class xs_udp_transport;
102d3abc739SOlga Kornievskaia static struct xprt_class xs_tcp_transport;
10375eb6af7SChuck Lever static struct xprt_class xs_tcp_tls_transport;
104d3abc739SOlga Kornievskaia static struct xprt_class xs_bc_tcp_transport;
105d3abc739SOlga Kornievskaia 
106fbf76683SChuck Lever /*
107fbf76683SChuck Lever  * FIXME: changing the UDP slot table size should also resize the UDP
108fbf76683SChuck Lever  *        socket buffers for existing UDP transports
109fbf76683SChuck Lever  */
110fe2c6338SJoe Perches static struct ctl_table xs_tunables_table[] = {
111fbf76683SChuck Lever 	{
112fbf76683SChuck Lever 		.procname	= "udp_slot_table_entries",
113fbf76683SChuck Lever 		.data		= &xprt_udp_slot_table_entries,
114fbf76683SChuck Lever 		.maxlen		= sizeof(unsigned int),
115fbf76683SChuck Lever 		.mode		= 0644,
1166d456111SEric W. Biederman 		.proc_handler	= proc_dointvec_minmax,
117fbf76683SChuck Lever 		.extra1		= &min_slot_table_size,
118fbf76683SChuck Lever 		.extra2		= &max_slot_table_size
119fbf76683SChuck Lever 	},
120fbf76683SChuck Lever 	{
121fbf76683SChuck Lever 		.procname	= "tcp_slot_table_entries",
122fbf76683SChuck Lever 		.data		= &xprt_tcp_slot_table_entries,
123fbf76683SChuck Lever 		.maxlen		= sizeof(unsigned int),
124fbf76683SChuck Lever 		.mode		= 0644,
1256d456111SEric W. Biederman 		.proc_handler	= proc_dointvec_minmax,
126fbf76683SChuck Lever 		.extra1		= &min_slot_table_size,
127fbf76683SChuck Lever 		.extra2		= &max_slot_table_size
128fbf76683SChuck Lever 	},
129fbf76683SChuck Lever 	{
130d9ba131dSTrond Myklebust 		.procname	= "tcp_max_slot_table_entries",
131d9ba131dSTrond Myklebust 		.data		= &xprt_max_tcp_slot_table_entries,
132d9ba131dSTrond Myklebust 		.maxlen		= sizeof(unsigned int),
133d9ba131dSTrond Myklebust 		.mode		= 0644,
134d9ba131dSTrond Myklebust 		.proc_handler	= proc_dointvec_minmax,
135d9ba131dSTrond Myklebust 		.extra1		= &min_slot_table_size,
136d9ba131dSTrond Myklebust 		.extra2		= &max_tcp_slot_table_limit
137d9ba131dSTrond Myklebust 	},
138d9ba131dSTrond Myklebust 	{
139fbf76683SChuck Lever 		.procname	= "min_resvport",
140fbf76683SChuck Lever 		.data		= &xprt_min_resvport,
141fbf76683SChuck Lever 		.maxlen		= sizeof(unsigned int),
142fbf76683SChuck Lever 		.mode		= 0644,
1436d456111SEric W. Biederman 		.proc_handler	= proc_dointvec_minmax,
144fbf76683SChuck Lever 		.extra1		= &xprt_min_resvport_limit,
145826799e6SJ. Bruce Fields 		.extra2		= &xprt_max_resvport_limit
146fbf76683SChuck Lever 	},
147fbf76683SChuck Lever 	{
148fbf76683SChuck Lever 		.procname	= "max_resvport",
149fbf76683SChuck Lever 		.data		= &xprt_max_resvport,
150fbf76683SChuck Lever 		.maxlen		= sizeof(unsigned int),
151fbf76683SChuck Lever 		.mode		= 0644,
1526d456111SEric W. Biederman 		.proc_handler	= proc_dointvec_minmax,
153826799e6SJ. Bruce Fields 		.extra1		= &xprt_min_resvport_limit,
154fbf76683SChuck Lever 		.extra2		= &xprt_max_resvport_limit
155fbf76683SChuck Lever 	},
156fbf76683SChuck Lever 	{
15725fe6142STrond Myklebust 		.procname	= "tcp_fin_timeout",
15825fe6142STrond Myklebust 		.data		= &xs_tcp_fin_timeout,
15925fe6142STrond Myklebust 		.maxlen		= sizeof(xs_tcp_fin_timeout),
16025fe6142STrond Myklebust 		.mode		= 0644,
1616d456111SEric W. Biederman 		.proc_handler	= proc_dointvec_jiffies,
16225fe6142STrond Myklebust 	},
163f8572d8fSEric W. Biederman 	{ },
164fbf76683SChuck Lever };
165fbf76683SChuck Lever 
166fbf76683SChuck Lever /*
16703bf4b70SChuck Lever  * Wait duration for a reply from the RPC portmapper.
16803bf4b70SChuck Lever  */
16903bf4b70SChuck Lever #define XS_BIND_TO		(60U * HZ)
17003bf4b70SChuck Lever 
17103bf4b70SChuck Lever /*
17203bf4b70SChuck Lever  * Delay if a UDP socket connect error occurs.  This is most likely some
17303bf4b70SChuck Lever  * kind of resource problem on the local host.
17403bf4b70SChuck Lever  */
17503bf4b70SChuck Lever #define XS_UDP_REEST_TO		(2U * HZ)
17603bf4b70SChuck Lever 
17703bf4b70SChuck Lever /*
17803bf4b70SChuck Lever  * The reestablish timeout allows clients to delay for a bit before attempting
17903bf4b70SChuck Lever  * to reconnect to a server that just dropped our connection.
18003bf4b70SChuck Lever  *
18103bf4b70SChuck Lever  * We implement an exponential backoff when trying to reestablish a TCP
18203bf4b70SChuck Lever  * transport connection with the server.  Some servers like to drop a TCP
18303bf4b70SChuck Lever  * connection when they are overworked, so we start with a short timeout and
18403bf4b70SChuck Lever  * increase over time if the server is down or not responding.
18503bf4b70SChuck Lever  */
18603bf4b70SChuck Lever #define XS_TCP_INIT_REEST_TO	(3U * HZ)
18703bf4b70SChuck Lever 
18803bf4b70SChuck Lever /*
18903bf4b70SChuck Lever  * TCP idle timeout; client drops the transport socket if it is idle
19003bf4b70SChuck Lever  * for this long.  Note that we also timeout UDP sockets to prevent
19103bf4b70SChuck Lever  * holding port numbers when there is no RPC traffic.
19203bf4b70SChuck Lever  */
19303bf4b70SChuck Lever #define XS_IDLE_DISC_TO		(5U * 60 * HZ)
19403bf4b70SChuck Lever 
19575eb6af7SChuck Lever /*
19675eb6af7SChuck Lever  * TLS handshake timeout.
19775eb6af7SChuck Lever  */
19875eb6af7SChuck Lever #define XS_TLS_HANDSHAKE_TO	(10U * HZ)
19975eb6af7SChuck Lever 
200f895b252SJeff Layton #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
201a246b010SChuck Lever # undef  RPC_DEBUG_DATA
2029903cd1cSChuck Lever # define RPCDBG_FACILITY	RPCDBG_TRANS
203a246b010SChuck Lever #endif
204a246b010SChuck Lever 
205a246b010SChuck Lever #ifdef RPC_DEBUG_DATA
2069903cd1cSChuck Lever static void xs_pktdump(char *msg, u32 *packet, unsigned int count)
207a246b010SChuck Lever {
208a246b010SChuck Lever 	u8 *buf = (u8 *) packet;
209a246b010SChuck Lever 	int j;
210a246b010SChuck Lever 
211a246b010SChuck Lever 	dprintk("RPC:       %s\n", msg);
212a246b010SChuck Lever 	for (j = 0; j < count && j < 128; j += 4) {
213a246b010SChuck Lever 		if (!(j & 31)) {
214a246b010SChuck Lever 			if (j)
215a246b010SChuck Lever 				dprintk("\n");
216a246b010SChuck Lever 			dprintk("0x%04x ", j);
217a246b010SChuck Lever 		}
218a246b010SChuck Lever 		dprintk("%02x%02x%02x%02x ",
219a246b010SChuck Lever 			buf[j], buf[j+1], buf[j+2], buf[j+3]);
220a246b010SChuck Lever 	}
221a246b010SChuck Lever 	dprintk("\n");
222a246b010SChuck Lever }
223a246b010SChuck Lever #else
2249903cd1cSChuck Lever static inline void xs_pktdump(char *msg, u32 *packet, unsigned int count)
225a246b010SChuck Lever {
226a246b010SChuck Lever 	/* NOP */
227a246b010SChuck Lever }
228a246b010SChuck Lever #endif
229a246b010SChuck Lever 
2302118071dSTrond Myklebust static inline struct rpc_xprt *xprt_from_sock(struct sock *sk)
2312118071dSTrond Myklebust {
2322118071dSTrond Myklebust 	return (struct rpc_xprt *) sk->sk_user_data;
2332118071dSTrond Myklebust }
2342118071dSTrond Myklebust 
23595392c59SChuck Lever static inline struct sockaddr *xs_addr(struct rpc_xprt *xprt)
236edb267a6SChuck Lever {
23795392c59SChuck Lever 	return (struct sockaddr *) &xprt->addr;
23895392c59SChuck Lever }
23995392c59SChuck Lever 
240176e21eeSChuck Lever static inline struct sockaddr_un *xs_addr_un(struct rpc_xprt *xprt)
241176e21eeSChuck Lever {
242176e21eeSChuck Lever 	return (struct sockaddr_un *) &xprt->addr;
243176e21eeSChuck Lever }
244176e21eeSChuck Lever 
24595392c59SChuck Lever static inline struct sockaddr_in *xs_addr_in(struct rpc_xprt *xprt)
24695392c59SChuck Lever {
24795392c59SChuck Lever 	return (struct sockaddr_in *) &xprt->addr;
24895392c59SChuck Lever }
24995392c59SChuck Lever 
25095392c59SChuck Lever static inline struct sockaddr_in6 *xs_addr_in6(struct rpc_xprt *xprt)
25195392c59SChuck Lever {
25295392c59SChuck Lever 	return (struct sockaddr_in6 *) &xprt->addr;
25395392c59SChuck Lever }
25495392c59SChuck Lever 
255c877b849SChuck Lever static void xs_format_common_peer_addresses(struct rpc_xprt *xprt)
256c877b849SChuck Lever {
257c877b849SChuck Lever 	struct sockaddr *sap = xs_addr(xprt);
2589dc3b095SChuck Lever 	struct sockaddr_in6 *sin6;
2599dc3b095SChuck Lever 	struct sockaddr_in *sin;
260176e21eeSChuck Lever 	struct sockaddr_un *sun;
261c877b849SChuck Lever 	char buf[128];
262c877b849SChuck Lever 
2639dc3b095SChuck Lever 	switch (sap->sa_family) {
264176e21eeSChuck Lever 	case AF_LOCAL:
265176e21eeSChuck Lever 		sun = xs_addr_un(xprt);
2664388ce05SNeilBrown 		if (sun->sun_path[0]) {
26715bcdc92SWolfram Sang 			strscpy(buf, sun->sun_path, sizeof(buf));
2684388ce05SNeilBrown 		} else {
2694388ce05SNeilBrown 			buf[0] = '@';
2704388ce05SNeilBrown 			strscpy(buf+1, sun->sun_path+1, sizeof(buf)-1);
2714388ce05SNeilBrown 		}
272176e21eeSChuck Lever 		xprt->address_strings[RPC_DISPLAY_ADDR] =
273176e21eeSChuck Lever 						kstrdup(buf, GFP_KERNEL);
274176e21eeSChuck Lever 		break;
2759dc3b095SChuck Lever 	case AF_INET:
276176e21eeSChuck Lever 		(void)rpc_ntop(sap, buf, sizeof(buf));
277176e21eeSChuck Lever 		xprt->address_strings[RPC_DISPLAY_ADDR] =
278176e21eeSChuck Lever 						kstrdup(buf, GFP_KERNEL);
2799dc3b095SChuck Lever 		sin = xs_addr_in(xprt);
280fc0b5791SJoe Perches 		snprintf(buf, sizeof(buf), "%08x", ntohl(sin->sin_addr.s_addr));
2819dc3b095SChuck Lever 		break;
2829dc3b095SChuck Lever 	case AF_INET6:
283176e21eeSChuck Lever 		(void)rpc_ntop(sap, buf, sizeof(buf));
284176e21eeSChuck Lever 		xprt->address_strings[RPC_DISPLAY_ADDR] =
285176e21eeSChuck Lever 						kstrdup(buf, GFP_KERNEL);
2869dc3b095SChuck Lever 		sin6 = xs_addr_in6(xprt);
287fc0b5791SJoe Perches 		snprintf(buf, sizeof(buf), "%pi6", &sin6->sin6_addr);
2889dc3b095SChuck Lever 		break;
2899dc3b095SChuck Lever 	default:
2909dc3b095SChuck Lever 		BUG();
2919dc3b095SChuck Lever 	}
292176e21eeSChuck Lever 
2939dc3b095SChuck Lever 	xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = kstrdup(buf, GFP_KERNEL);
2949dc3b095SChuck Lever }
2959dc3b095SChuck Lever 
2969dc3b095SChuck Lever static void xs_format_common_peer_ports(struct rpc_xprt *xprt)
2979dc3b095SChuck Lever {
2989dc3b095SChuck Lever 	struct sockaddr *sap = xs_addr(xprt);
2999dc3b095SChuck Lever 	char buf[128];
3009dc3b095SChuck Lever 
30181160e66SJoe Perches 	snprintf(buf, sizeof(buf), "%u", rpc_get_port(sap));
302c877b849SChuck Lever 	xprt->address_strings[RPC_DISPLAY_PORT] = kstrdup(buf, GFP_KERNEL);
303c877b849SChuck Lever 
30481160e66SJoe Perches 	snprintf(buf, sizeof(buf), "%4hx", rpc_get_port(sap));
305c877b849SChuck Lever 	xprt->address_strings[RPC_DISPLAY_HEX_PORT] = kstrdup(buf, GFP_KERNEL);
306c877b849SChuck Lever }
307c877b849SChuck Lever 
3089dc3b095SChuck Lever static void xs_format_peer_addresses(struct rpc_xprt *xprt,
309b454ae90SChuck Lever 				     const char *protocol,
310b454ae90SChuck Lever 				     const char *netid)
311edb267a6SChuck Lever {
312b454ae90SChuck Lever 	xprt->address_strings[RPC_DISPLAY_PROTO] = protocol;
313b454ae90SChuck Lever 	xprt->address_strings[RPC_DISPLAY_NETID] = netid;
314c877b849SChuck Lever 	xs_format_common_peer_addresses(xprt);
3159dc3b095SChuck Lever 	xs_format_common_peer_ports(xprt);
316edb267a6SChuck Lever }
317edb267a6SChuck Lever 
3189dc3b095SChuck Lever static void xs_update_peer_port(struct rpc_xprt *xprt)
3194b6473fbSChuck Lever {
3209dc3b095SChuck Lever 	kfree(xprt->address_strings[RPC_DISPLAY_HEX_PORT]);
3219dc3b095SChuck Lever 	kfree(xprt->address_strings[RPC_DISPLAY_PORT]);
3224b6473fbSChuck Lever 
3239dc3b095SChuck Lever 	xs_format_common_peer_ports(xprt);
324edb267a6SChuck Lever }
325edb267a6SChuck Lever 
326edb267a6SChuck Lever static void xs_free_peer_addresses(struct rpc_xprt *xprt)
327edb267a6SChuck Lever {
32833e01dc7SChuck Lever 	unsigned int i;
32933e01dc7SChuck Lever 
33033e01dc7SChuck Lever 	for (i = 0; i < RPC_DISPLAY_MAX; i++)
33133e01dc7SChuck Lever 		switch (i) {
33233e01dc7SChuck Lever 		case RPC_DISPLAY_PROTO:
33333e01dc7SChuck Lever 		case RPC_DISPLAY_NETID:
33433e01dc7SChuck Lever 			continue;
33533e01dc7SChuck Lever 		default:
33633e01dc7SChuck Lever 			kfree(xprt->address_strings[i]);
33733e01dc7SChuck Lever 		}
338edb267a6SChuck Lever }
339edb267a6SChuck Lever 
340277e4ab7STrond Myklebust static size_t
341277e4ab7STrond Myklebust xs_alloc_sparse_pages(struct xdr_buf *buf, size_t want, gfp_t gfp)
342277e4ab7STrond Myklebust {
343277e4ab7STrond Myklebust 	size_t i,n;
344277e4ab7STrond Myklebust 
34516e5e90fSTrond Myklebust 	if (!want || !(buf->flags & XDRBUF_SPARSE_PAGES))
346277e4ab7STrond Myklebust 		return want;
347277e4ab7STrond Myklebust 	n = (buf->page_base + want + PAGE_SIZE - 1) >> PAGE_SHIFT;
348277e4ab7STrond Myklebust 	for (i = 0; i < n; i++) {
349277e4ab7STrond Myklebust 		if (buf->pages[i])
350277e4ab7STrond Myklebust 			continue;
351277e4ab7STrond Myklebust 		buf->bvec[i].bv_page = buf->pages[i] = alloc_page(gfp);
352277e4ab7STrond Myklebust 		if (!buf->pages[i]) {
35316e5e90fSTrond Myklebust 			i *= PAGE_SIZE;
35416e5e90fSTrond Myklebust 			return i > buf->page_base ? i - buf->page_base : 0;
355277e4ab7STrond Myklebust 		}
356277e4ab7STrond Myklebust 	}
357277e4ab7STrond Myklebust 	return want;
358277e4ab7STrond Myklebust }
359277e4ab7STrond Myklebust 
360dea034b9SChuck Lever static int
361dea034b9SChuck Lever xs_sock_process_cmsg(struct socket *sock, struct msghdr *msg,
362dea034b9SChuck Lever 		     struct cmsghdr *cmsg, int ret)
363dea034b9SChuck Lever {
364dea034b9SChuck Lever 	if (cmsg->cmsg_level == SOL_TLS &&
365dea034b9SChuck Lever 	    cmsg->cmsg_type == TLS_GET_RECORD_TYPE) {
366dea034b9SChuck Lever 		u8 content_type = *((u8 *)CMSG_DATA(cmsg));
367dea034b9SChuck Lever 
368dea034b9SChuck Lever 		switch (content_type) {
369dea034b9SChuck Lever 		case TLS_RECORD_TYPE_DATA:
370dea034b9SChuck Lever 			/* TLS sets EOR at the end of each application data
371dea034b9SChuck Lever 			 * record, even though there might be more frames
372dea034b9SChuck Lever 			 * waiting to be decrypted.
373dea034b9SChuck Lever 			 */
374dea034b9SChuck Lever 			msg->msg_flags &= ~MSG_EOR;
375dea034b9SChuck Lever 			break;
376dea034b9SChuck Lever 		case TLS_RECORD_TYPE_ALERT:
377dea034b9SChuck Lever 			ret = -ENOTCONN;
378dea034b9SChuck Lever 			break;
379dea034b9SChuck Lever 		default:
380dea034b9SChuck Lever 			ret = -EAGAIN;
381dea034b9SChuck Lever 		}
382dea034b9SChuck Lever 	}
383dea034b9SChuck Lever 	return ret;
384dea034b9SChuck Lever }
385dea034b9SChuck Lever 
386dea034b9SChuck Lever static int
387dea034b9SChuck Lever xs_sock_recv_cmsg(struct socket *sock, struct msghdr *msg, int flags)
388dea034b9SChuck Lever {
389dea034b9SChuck Lever 	union {
390dea034b9SChuck Lever 		struct cmsghdr	cmsg;
391dea034b9SChuck Lever 		u8		buf[CMSG_SPACE(sizeof(u8))];
392dea034b9SChuck Lever 	} u;
393dea034b9SChuck Lever 	int ret;
394dea034b9SChuck Lever 
395dea034b9SChuck Lever 	msg->msg_control = &u;
396dea034b9SChuck Lever 	msg->msg_controllen = sizeof(u);
397dea034b9SChuck Lever 	ret = sock_recvmsg(sock, msg, flags);
398dea034b9SChuck Lever 	if (msg->msg_controllen != sizeof(u))
399dea034b9SChuck Lever 		ret = xs_sock_process_cmsg(sock, msg, &u.cmsg, ret);
400dea034b9SChuck Lever 	return ret;
401dea034b9SChuck Lever }
402dea034b9SChuck Lever 
403277e4ab7STrond Myklebust static ssize_t
404277e4ab7STrond Myklebust xs_sock_recvmsg(struct socket *sock, struct msghdr *msg, int flags, size_t seek)
405277e4ab7STrond Myklebust {
406277e4ab7STrond Myklebust 	ssize_t ret;
407277e4ab7STrond Myklebust 	if (seek != 0)
408277e4ab7STrond Myklebust 		iov_iter_advance(&msg->msg_iter, seek);
409dea034b9SChuck Lever 	ret = xs_sock_recv_cmsg(sock, msg, flags);
410277e4ab7STrond Myklebust 	return ret > 0 ? ret + seek : ret;
411277e4ab7STrond Myklebust }
412277e4ab7STrond Myklebust 
413277e4ab7STrond Myklebust static ssize_t
414277e4ab7STrond Myklebust xs_read_kvec(struct socket *sock, struct msghdr *msg, int flags,
415277e4ab7STrond Myklebust 		struct kvec *kvec, size_t count, size_t seek)
416277e4ab7STrond Myklebust {
417de4eda9dSAl Viro 	iov_iter_kvec(&msg->msg_iter, ITER_DEST, kvec, 1, count);
418277e4ab7STrond Myklebust 	return xs_sock_recvmsg(sock, msg, flags, seek);
419277e4ab7STrond Myklebust }
420277e4ab7STrond Myklebust 
421277e4ab7STrond Myklebust static ssize_t
422277e4ab7STrond Myklebust xs_read_bvec(struct socket *sock, struct msghdr *msg, int flags,
423277e4ab7STrond Myklebust 		struct bio_vec *bvec, unsigned long nr, size_t count,
424277e4ab7STrond Myklebust 		size_t seek)
425277e4ab7STrond Myklebust {
426de4eda9dSAl Viro 	iov_iter_bvec(&msg->msg_iter, ITER_DEST, bvec, nr, count);
427277e4ab7STrond Myklebust 	return xs_sock_recvmsg(sock, msg, flags, seek);
428277e4ab7STrond Myklebust }
429277e4ab7STrond Myklebust 
430277e4ab7STrond Myklebust static ssize_t
431277e4ab7STrond Myklebust xs_read_discard(struct socket *sock, struct msghdr *msg, int flags,
432277e4ab7STrond Myklebust 		size_t count)
433277e4ab7STrond Myklebust {
434de4eda9dSAl Viro 	iov_iter_discard(&msg->msg_iter, ITER_DEST, count);
435dea034b9SChuck Lever 	return xs_sock_recv_cmsg(sock, msg, flags);
436277e4ab7STrond Myklebust }
437277e4ab7STrond Myklebust 
4386a829eb8STrond Myklebust #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
4396a829eb8STrond Myklebust static void
4406a829eb8STrond Myklebust xs_flush_bvec(const struct bio_vec *bvec, size_t count, size_t seek)
4416a829eb8STrond Myklebust {
4426a829eb8STrond Myklebust 	struct bvec_iter bi = {
4436a829eb8STrond Myklebust 		.bi_size = count,
4446a829eb8STrond Myklebust 	};
4456a829eb8STrond Myklebust 	struct bio_vec bv;
4466a829eb8STrond Myklebust 
4476a829eb8STrond Myklebust 	bvec_iter_advance(bvec, &bi, seek & PAGE_MASK);
4486a829eb8STrond Myklebust 	for_each_bvec(bv, bvec, bi, bi)
4496a829eb8STrond Myklebust 		flush_dcache_page(bv.bv_page);
4506a829eb8STrond Myklebust }
4516a829eb8STrond Myklebust #else
4526a829eb8STrond Myklebust static inline void
4536a829eb8STrond Myklebust xs_flush_bvec(const struct bio_vec *bvec, size_t count, size_t seek)
4546a829eb8STrond Myklebust {
4556a829eb8STrond Myklebust }
4566a829eb8STrond Myklebust #endif
4576a829eb8STrond Myklebust 
458277e4ab7STrond Myklebust static ssize_t
459277e4ab7STrond Myklebust xs_read_xdr_buf(struct socket *sock, struct msghdr *msg, int flags,
460277e4ab7STrond Myklebust 		struct xdr_buf *buf, size_t count, size_t seek, size_t *read)
461277e4ab7STrond Myklebust {
462277e4ab7STrond Myklebust 	size_t want, seek_init = seek, offset = 0;
463277e4ab7STrond Myklebust 	ssize_t ret;
464277e4ab7STrond Myklebust 
465277e4ab7STrond Myklebust 	want = min_t(size_t, count, buf->head[0].iov_len);
466e92053a5STrond Myklebust 	if (seek < want) {
467277e4ab7STrond Myklebust 		ret = xs_read_kvec(sock, msg, flags, &buf->head[0], want, seek);
468277e4ab7STrond Myklebust 		if (ret <= 0)
469277e4ab7STrond Myklebust 			goto sock_err;
470277e4ab7STrond Myklebust 		offset += ret;
471277e4ab7STrond Myklebust 		if (offset == count || msg->msg_flags & (MSG_EOR|MSG_TRUNC))
472277e4ab7STrond Myklebust 			goto out;
473277e4ab7STrond Myklebust 		if (ret != want)
474c4433055STrond Myklebust 			goto out;
475277e4ab7STrond Myklebust 		seek = 0;
476277e4ab7STrond Myklebust 	} else {
477e92053a5STrond Myklebust 		seek -= want;
478e92053a5STrond Myklebust 		offset += want;
479277e4ab7STrond Myklebust 	}
48016e5e90fSTrond Myklebust 
481b2648015STrond Myklebust 	want = xs_alloc_sparse_pages(
482b2648015STrond Myklebust 		buf, min_t(size_t, count - offset, buf->page_len),
483b2648015STrond Myklebust 		GFP_KERNEL | __GFP_NORETRY | __GFP_NOWARN);
48416e5e90fSTrond Myklebust 	if (seek < want) {
485277e4ab7STrond Myklebust 		ret = xs_read_bvec(sock, msg, flags, buf->bvec,
486277e4ab7STrond Myklebust 				xdr_buf_pagecount(buf),
487277e4ab7STrond Myklebust 				want + buf->page_base,
488277e4ab7STrond Myklebust 				seek + buf->page_base);
489277e4ab7STrond Myklebust 		if (ret <= 0)
490277e4ab7STrond Myklebust 			goto sock_err;
4916a829eb8STrond Myklebust 		xs_flush_bvec(buf->bvec, ret, seek + buf->page_base);
492ac9645c8SDan Aloni 		ret -= buf->page_base;
493ac9645c8SDan Aloni 		offset += ret;
494277e4ab7STrond Myklebust 		if (offset == count || msg->msg_flags & (MSG_EOR|MSG_TRUNC))
495277e4ab7STrond Myklebust 			goto out;
496277e4ab7STrond Myklebust 		if (ret != want)
497c4433055STrond Myklebust 			goto out;
498277e4ab7STrond Myklebust 		seek = 0;
499277e4ab7STrond Myklebust 	} else {
50016e5e90fSTrond Myklebust 		seek -= want;
50116e5e90fSTrond Myklebust 		offset += want;
502277e4ab7STrond Myklebust 	}
50316e5e90fSTrond Myklebust 
504277e4ab7STrond Myklebust 	want = min_t(size_t, count - offset, buf->tail[0].iov_len);
505e92053a5STrond Myklebust 	if (seek < want) {
506277e4ab7STrond Myklebust 		ret = xs_read_kvec(sock, msg, flags, &buf->tail[0], want, seek);
507277e4ab7STrond Myklebust 		if (ret <= 0)
508277e4ab7STrond Myklebust 			goto sock_err;
509277e4ab7STrond Myklebust 		offset += ret;
510277e4ab7STrond Myklebust 		if (offset == count || msg->msg_flags & (MSG_EOR|MSG_TRUNC))
511277e4ab7STrond Myklebust 			goto out;
512277e4ab7STrond Myklebust 		if (ret != want)
513c4433055STrond Myklebust 			goto out;
5149734ad57STrond Myklebust 	} else if (offset < seek_init)
515e92053a5STrond Myklebust 		offset = seek_init;
516277e4ab7STrond Myklebust 	ret = -EMSGSIZE;
517277e4ab7STrond Myklebust out:
518277e4ab7STrond Myklebust 	*read = offset - seek_init;
519277e4ab7STrond Myklebust 	return ret;
520277e4ab7STrond Myklebust sock_err:
521277e4ab7STrond Myklebust 	offset += seek;
522277e4ab7STrond Myklebust 	goto out;
523277e4ab7STrond Myklebust }
524277e4ab7STrond Myklebust 
525277e4ab7STrond Myklebust static void
526277e4ab7STrond Myklebust xs_read_header(struct sock_xprt *transport, struct xdr_buf *buf)
527277e4ab7STrond Myklebust {
528277e4ab7STrond Myklebust 	if (!transport->recv.copied) {
529277e4ab7STrond Myklebust 		if (buf->head[0].iov_len >= transport->recv.offset)
530277e4ab7STrond Myklebust 			memcpy(buf->head[0].iov_base,
531277e4ab7STrond Myklebust 					&transport->recv.xid,
532277e4ab7STrond Myklebust 					transport->recv.offset);
533277e4ab7STrond Myklebust 		transport->recv.copied = transport->recv.offset;
534277e4ab7STrond Myklebust 	}
535277e4ab7STrond Myklebust }
536277e4ab7STrond Myklebust 
537277e4ab7STrond Myklebust static bool
538277e4ab7STrond Myklebust xs_read_stream_request_done(struct sock_xprt *transport)
539277e4ab7STrond Myklebust {
540277e4ab7STrond Myklebust 	return transport->recv.fraghdr & cpu_to_be32(RPC_LAST_STREAM_FRAGMENT);
541277e4ab7STrond Myklebust }
542277e4ab7STrond Myklebust 
543e92053a5STrond Myklebust static void
544e92053a5STrond Myklebust xs_read_stream_check_eor(struct sock_xprt *transport,
545e92053a5STrond Myklebust 		struct msghdr *msg)
546e92053a5STrond Myklebust {
547e92053a5STrond Myklebust 	if (xs_read_stream_request_done(transport))
548e92053a5STrond Myklebust 		msg->msg_flags |= MSG_EOR;
549e92053a5STrond Myklebust }
550e92053a5STrond Myklebust 
551277e4ab7STrond Myklebust static ssize_t
552277e4ab7STrond Myklebust xs_read_stream_request(struct sock_xprt *transport, struct msghdr *msg,
553277e4ab7STrond Myklebust 		int flags, struct rpc_rqst *req)
554277e4ab7STrond Myklebust {
555277e4ab7STrond Myklebust 	struct xdr_buf *buf = &req->rq_private_buf;
5563f649ab7SKees Cook 	size_t want, read;
5573f649ab7SKees Cook 	ssize_t ret;
558277e4ab7STrond Myklebust 
559277e4ab7STrond Myklebust 	xs_read_header(transport, buf);
560277e4ab7STrond Myklebust 
561277e4ab7STrond Myklebust 	want = transport->recv.len - transport->recv.offset;
562e92053a5STrond Myklebust 	if (want != 0) {
563277e4ab7STrond Myklebust 		ret = xs_read_xdr_buf(transport->sock, msg, flags, buf,
564e92053a5STrond Myklebust 				transport->recv.copied + want,
565e92053a5STrond Myklebust 				transport->recv.copied,
566277e4ab7STrond Myklebust 				&read);
567277e4ab7STrond Myklebust 		transport->recv.offset += read;
568277e4ab7STrond Myklebust 		transport->recv.copied += read;
569277e4ab7STrond Myklebust 	}
570277e4ab7STrond Myklebust 
571727fcc64STrond Myklebust 	if (transport->recv.offset == transport->recv.len)
572727fcc64STrond Myklebust 		xs_read_stream_check_eor(transport, msg);
573727fcc64STrond Myklebust 
574e92053a5STrond Myklebust 	if (want == 0)
575e92053a5STrond Myklebust 		return 0;
576e92053a5STrond Myklebust 
577277e4ab7STrond Myklebust 	switch (ret) {
578c4433055STrond Myklebust 	default:
579c4433055STrond Myklebust 		break;
58026781eabSTrond Myklebust 	case -EFAULT:
581277e4ab7STrond Myklebust 	case -EMSGSIZE:
58226781eabSTrond Myklebust 		msg->msg_flags |= MSG_TRUNC;
583c4433055STrond Myklebust 		return read;
584277e4ab7STrond Myklebust 	case 0:
585277e4ab7STrond Myklebust 		return -ESHUTDOWN;
586277e4ab7STrond Myklebust 	}
587c4433055STrond Myklebust 	return ret < 0 ? ret : read;
588277e4ab7STrond Myklebust }
589277e4ab7STrond Myklebust 
590277e4ab7STrond Myklebust static size_t
591277e4ab7STrond Myklebust xs_read_stream_headersize(bool isfrag)
592277e4ab7STrond Myklebust {
593277e4ab7STrond Myklebust 	if (isfrag)
594277e4ab7STrond Myklebust 		return sizeof(__be32);
595277e4ab7STrond Myklebust 	return 3 * sizeof(__be32);
596277e4ab7STrond Myklebust }
597277e4ab7STrond Myklebust 
598277e4ab7STrond Myklebust static ssize_t
599277e4ab7STrond Myklebust xs_read_stream_header(struct sock_xprt *transport, struct msghdr *msg,
600277e4ab7STrond Myklebust 		int flags, size_t want, size_t seek)
601277e4ab7STrond Myklebust {
602277e4ab7STrond Myklebust 	struct kvec kvec = {
603277e4ab7STrond Myklebust 		.iov_base = &transport->recv.fraghdr,
604277e4ab7STrond Myklebust 		.iov_len = want,
605277e4ab7STrond Myklebust 	};
606277e4ab7STrond Myklebust 	return xs_read_kvec(transport->sock, msg, flags, &kvec, want, seek);
607277e4ab7STrond Myklebust }
608277e4ab7STrond Myklebust 
609277e4ab7STrond Myklebust #if defined(CONFIG_SUNRPC_BACKCHANNEL)
610277e4ab7STrond Myklebust static ssize_t
611277e4ab7STrond Myklebust xs_read_stream_call(struct sock_xprt *transport, struct msghdr *msg, int flags)
612277e4ab7STrond Myklebust {
613277e4ab7STrond Myklebust 	struct rpc_xprt *xprt = &transport->xprt;
614277e4ab7STrond Myklebust 	struct rpc_rqst *req;
615277e4ab7STrond Myklebust 	ssize_t ret;
616277e4ab7STrond Myklebust 
61798b5cee3SBenjamin Coddington 	/* Is this transport associated with the backchannel? */
61898b5cee3SBenjamin Coddington 	if (!xprt->bc_serv)
61998b5cee3SBenjamin Coddington 		return -ESHUTDOWN;
62098b5cee3SBenjamin Coddington 
621277e4ab7STrond Myklebust 	/* Look up and lock the request corresponding to the given XID */
622277e4ab7STrond Myklebust 	req = xprt_lookup_bc_request(xprt, transport->recv.xid);
623277e4ab7STrond Myklebust 	if (!req) {
624277e4ab7STrond Myklebust 		printk(KERN_WARNING "Callback slot table overflowed\n");
625277e4ab7STrond Myklebust 		return -ESHUTDOWN;
626277e4ab7STrond Myklebust 	}
62745835a63STrond Myklebust 	if (transport->recv.copied && !req->rq_private_buf.len)
62845835a63STrond Myklebust 		return -ESHUTDOWN;
629277e4ab7STrond Myklebust 
630277e4ab7STrond Myklebust 	ret = xs_read_stream_request(transport, msg, flags, req);
631277e4ab7STrond Myklebust 	if (msg->msg_flags & (MSG_EOR|MSG_TRUNC))
632c4433055STrond Myklebust 		xprt_complete_bc_request(req, transport->recv.copied);
63345835a63STrond Myklebust 	else
63445835a63STrond Myklebust 		req->rq_private_buf.len = transport->recv.copied;
635277e4ab7STrond Myklebust 
636277e4ab7STrond Myklebust 	return ret;
637277e4ab7STrond Myklebust }
638277e4ab7STrond Myklebust #else /* CONFIG_SUNRPC_BACKCHANNEL */
639277e4ab7STrond Myklebust static ssize_t
640277e4ab7STrond Myklebust xs_read_stream_call(struct sock_xprt *transport, struct msghdr *msg, int flags)
641277e4ab7STrond Myklebust {
642277e4ab7STrond Myklebust 	return -ESHUTDOWN;
643277e4ab7STrond Myklebust }
644277e4ab7STrond Myklebust #endif /* CONFIG_SUNRPC_BACKCHANNEL */
645277e4ab7STrond Myklebust 
646277e4ab7STrond Myklebust static ssize_t
647277e4ab7STrond Myklebust xs_read_stream_reply(struct sock_xprt *transport, struct msghdr *msg, int flags)
648277e4ab7STrond Myklebust {
649277e4ab7STrond Myklebust 	struct rpc_xprt *xprt = &transport->xprt;
650277e4ab7STrond Myklebust 	struct rpc_rqst *req;
651277e4ab7STrond Myklebust 	ssize_t ret = 0;
652277e4ab7STrond Myklebust 
653277e4ab7STrond Myklebust 	/* Look up and lock the request corresponding to the given XID */
654277e4ab7STrond Myklebust 	spin_lock(&xprt->queue_lock);
655277e4ab7STrond Myklebust 	req = xprt_lookup_rqst(xprt, transport->recv.xid);
65645835a63STrond Myklebust 	if (!req || (transport->recv.copied && !req->rq_private_buf.len)) {
657277e4ab7STrond Myklebust 		msg->msg_flags |= MSG_TRUNC;
658277e4ab7STrond Myklebust 		goto out;
659277e4ab7STrond Myklebust 	}
660277e4ab7STrond Myklebust 	xprt_pin_rqst(req);
661277e4ab7STrond Myklebust 	spin_unlock(&xprt->queue_lock);
662277e4ab7STrond Myklebust 
663277e4ab7STrond Myklebust 	ret = xs_read_stream_request(transport, msg, flags, req);
664277e4ab7STrond Myklebust 
665277e4ab7STrond Myklebust 	spin_lock(&xprt->queue_lock);
666277e4ab7STrond Myklebust 	if (msg->msg_flags & (MSG_EOR|MSG_TRUNC))
667c4433055STrond Myklebust 		xprt_complete_rqst(req->rq_task, transport->recv.copied);
66845835a63STrond Myklebust 	else
66945835a63STrond Myklebust 		req->rq_private_buf.len = transport->recv.copied;
670277e4ab7STrond Myklebust 	xprt_unpin_rqst(req);
671277e4ab7STrond Myklebust out:
672277e4ab7STrond Myklebust 	spin_unlock(&xprt->queue_lock);
673277e4ab7STrond Myklebust 	return ret;
674277e4ab7STrond Myklebust }
675277e4ab7STrond Myklebust 
676277e4ab7STrond Myklebust static ssize_t
677277e4ab7STrond Myklebust xs_read_stream(struct sock_xprt *transport, int flags)
678277e4ab7STrond Myklebust {
679277e4ab7STrond Myklebust 	struct msghdr msg = { 0 };
680277e4ab7STrond Myklebust 	size_t want, read = 0;
681277e4ab7STrond Myklebust 	ssize_t ret = 0;
682277e4ab7STrond Myklebust 
683277e4ab7STrond Myklebust 	if (transport->recv.len == 0) {
684277e4ab7STrond Myklebust 		want = xs_read_stream_headersize(transport->recv.copied != 0);
685277e4ab7STrond Myklebust 		ret = xs_read_stream_header(transport, &msg, flags, want,
686277e4ab7STrond Myklebust 				transport->recv.offset);
687277e4ab7STrond Myklebust 		if (ret <= 0)
688277e4ab7STrond Myklebust 			goto out_err;
689277e4ab7STrond Myklebust 		transport->recv.offset = ret;
690c4433055STrond Myklebust 		if (transport->recv.offset != want)
691c4433055STrond Myklebust 			return transport->recv.offset;
692277e4ab7STrond Myklebust 		transport->recv.len = be32_to_cpu(transport->recv.fraghdr) &
693277e4ab7STrond Myklebust 			RPC_FRAGMENT_SIZE_MASK;
694277e4ab7STrond Myklebust 		transport->recv.offset -= sizeof(transport->recv.fraghdr);
695277e4ab7STrond Myklebust 		read = ret;
696277e4ab7STrond Myklebust 	}
697277e4ab7STrond Myklebust 
698277e4ab7STrond Myklebust 	switch (be32_to_cpu(transport->recv.calldir)) {
699c4433055STrond Myklebust 	default:
700c4433055STrond Myklebust 		msg.msg_flags |= MSG_TRUNC;
701c4433055STrond Myklebust 		break;
702277e4ab7STrond Myklebust 	case RPC_CALL:
703277e4ab7STrond Myklebust 		ret = xs_read_stream_call(transport, &msg, flags);
704277e4ab7STrond Myklebust 		break;
705277e4ab7STrond Myklebust 	case RPC_REPLY:
706277e4ab7STrond Myklebust 		ret = xs_read_stream_reply(transport, &msg, flags);
707277e4ab7STrond Myklebust 	}
708277e4ab7STrond Myklebust 	if (msg.msg_flags & MSG_TRUNC) {
709277e4ab7STrond Myklebust 		transport->recv.calldir = cpu_to_be32(-1);
710277e4ab7STrond Myklebust 		transport->recv.copied = -1;
711277e4ab7STrond Myklebust 	}
712277e4ab7STrond Myklebust 	if (ret < 0)
713277e4ab7STrond Myklebust 		goto out_err;
714277e4ab7STrond Myklebust 	read += ret;
715277e4ab7STrond Myklebust 	if (transport->recv.offset < transport->recv.len) {
716c4433055STrond Myklebust 		if (!(msg.msg_flags & MSG_TRUNC))
717c4433055STrond Myklebust 			return read;
718b76a5afdSTrond Myklebust 		msg.msg_flags = 0;
719277e4ab7STrond Myklebust 		ret = xs_read_discard(transport->sock, &msg, flags,
720277e4ab7STrond Myklebust 				transport->recv.len - transport->recv.offset);
721277e4ab7STrond Myklebust 		if (ret <= 0)
722277e4ab7STrond Myklebust 			goto out_err;
723277e4ab7STrond Myklebust 		transport->recv.offset += ret;
724277e4ab7STrond Myklebust 		read += ret;
725277e4ab7STrond Myklebust 		if (transport->recv.offset != transport->recv.len)
726c4433055STrond Myklebust 			return read;
727277e4ab7STrond Myklebust 	}
728277e4ab7STrond Myklebust 	if (xs_read_stream_request_done(transport)) {
729c50b8ee0STrond Myklebust 		trace_xs_stream_read_request(transport);
730277e4ab7STrond Myklebust 		transport->recv.copied = 0;
731277e4ab7STrond Myklebust 	}
732277e4ab7STrond Myklebust 	transport->recv.offset = 0;
733277e4ab7STrond Myklebust 	transport->recv.len = 0;
734277e4ab7STrond Myklebust 	return read;
735277e4ab7STrond Myklebust out_err:
73679462857STrond Myklebust 	return ret != 0 ? ret : -ESHUTDOWN;
737277e4ab7STrond Myklebust }
738277e4ab7STrond Myklebust 
7390ffe86f4STrond Myklebust static __poll_t xs_poll_socket(struct sock_xprt *transport)
7400ffe86f4STrond Myklebust {
741a73881c9STrond Myklebust 	return transport->sock->ops->poll(transport->file, transport->sock,
742a73881c9STrond Myklebust 			NULL);
7430ffe86f4STrond Myklebust }
7440ffe86f4STrond Myklebust 
7450ffe86f4STrond Myklebust static bool xs_poll_socket_readable(struct sock_xprt *transport)
7460ffe86f4STrond Myklebust {
7470ffe86f4STrond Myklebust 	__poll_t events = xs_poll_socket(transport);
7480ffe86f4STrond Myklebust 
7490ffe86f4STrond Myklebust 	return (events & (EPOLLIN | EPOLLRDNORM)) && !(events & EPOLLRDHUP);
7500ffe86f4STrond Myklebust }
7510ffe86f4STrond Myklebust 
7520ffe86f4STrond Myklebust static void xs_poll_check_readable(struct sock_xprt *transport)
7530ffe86f4STrond Myklebust {
7540ffe86f4STrond Myklebust 
7550ffe86f4STrond Myklebust 	clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state);
7560d3ca07fSChuck Lever 	if (test_bit(XPRT_SOCK_IGNORE_RECV, &transport->sock_state))
7570d3ca07fSChuck Lever 		return;
7580ffe86f4STrond Myklebust 	if (!xs_poll_socket_readable(transport))
7590ffe86f4STrond Myklebust 		return;
7600ffe86f4STrond Myklebust 	if (!test_and_set_bit(XPRT_SOCK_DATA_READY, &transport->sock_state))
7610ffe86f4STrond Myklebust 		queue_work(xprtiod_workqueue, &transport->recv_worker);
7620ffe86f4STrond Myklebust }
7630ffe86f4STrond Myklebust 
764c50b8ee0STrond Myklebust static void xs_stream_data_receive(struct sock_xprt *transport)
765c50b8ee0STrond Myklebust {
766c50b8ee0STrond Myklebust 	size_t read = 0;
767c50b8ee0STrond Myklebust 	ssize_t ret = 0;
768c50b8ee0STrond Myklebust 
769c50b8ee0STrond Myklebust 	mutex_lock(&transport->recv_mutex);
770c50b8ee0STrond Myklebust 	if (transport->sock == NULL)
771c50b8ee0STrond Myklebust 		goto out;
772c50b8ee0STrond Myklebust 	for (;;) {
773c50b8ee0STrond Myklebust 		ret = xs_read_stream(transport, MSG_DONTWAIT);
774c4433055STrond Myklebust 		if (ret < 0)
775c50b8ee0STrond Myklebust 			break;
776c50b8ee0STrond Myklebust 		read += ret;
777c50b8ee0STrond Myklebust 		cond_resched();
778c50b8ee0STrond Myklebust 	}
7795f52a9d4STrond Myklebust 	if (ret == -ESHUTDOWN)
7805f52a9d4STrond Myklebust 		kernel_sock_shutdown(transport->sock, SHUT_RDWR);
7815f52a9d4STrond Myklebust 	else
7820ffe86f4STrond Myklebust 		xs_poll_check_readable(transport);
783c50b8ee0STrond Myklebust out:
784c50b8ee0STrond Myklebust 	mutex_unlock(&transport->recv_mutex);
785c50b8ee0STrond Myklebust 	trace_xs_stream_read_data(&transport->xprt, ret, read);
786c50b8ee0STrond Myklebust }
787c50b8ee0STrond Myklebust 
788c50b8ee0STrond Myklebust static void xs_stream_data_receive_workfn(struct work_struct *work)
789c50b8ee0STrond Myklebust {
790c50b8ee0STrond Myklebust 	struct sock_xprt *transport =
791c50b8ee0STrond Myklebust 		container_of(work, struct sock_xprt, recv_worker);
792a1231fdaSTrond Myklebust 	unsigned int pflags = memalloc_nofs_save();
793a1231fdaSTrond Myklebust 
794c50b8ee0STrond Myklebust 	xs_stream_data_receive(transport);
795a1231fdaSTrond Myklebust 	memalloc_nofs_restore(pflags);
796c50b8ee0STrond Myklebust }
797c50b8ee0STrond Myklebust 
798550aebfeSTrond Myklebust static void
799550aebfeSTrond Myklebust xs_stream_reset_connect(struct sock_xprt *transport)
800550aebfeSTrond Myklebust {
801550aebfeSTrond Myklebust 	transport->recv.offset = 0;
802550aebfeSTrond Myklebust 	transport->recv.len = 0;
803550aebfeSTrond Myklebust 	transport->recv.copied = 0;
804550aebfeSTrond Myklebust 	transport->xmit.offset = 0;
805ae053551STrond Myklebust }
806ae053551STrond Myklebust 
807ae053551STrond Myklebust static void
808ae053551STrond Myklebust xs_stream_start_connect(struct sock_xprt *transport)
809ae053551STrond Myklebust {
810550aebfeSTrond Myklebust 	transport->xprt.stat.connect_count++;
811550aebfeSTrond Myklebust 	transport->xprt.stat.connect_start = jiffies;
812550aebfeSTrond Myklebust }
813550aebfeSTrond Myklebust 
814b4b5cc85SChuck Lever #define XS_SENDMSG_FLAGS	(MSG_DONTWAIT | MSG_NOSIGNAL)
815b4b5cc85SChuck Lever 
8169903cd1cSChuck Lever /**
817c544577dSTrond Myklebust  * xs_nospace - handle transmit was incomplete
81850f484e2STrond Myklebust  * @req: pointer to RPC request
8197496b59fSTrond Myklebust  * @transport: pointer to struct sock_xprt
8209903cd1cSChuck Lever  *
821a246b010SChuck Lever  */
8227496b59fSTrond Myklebust static int xs_nospace(struct rpc_rqst *req, struct sock_xprt *transport)
823a246b010SChuck Lever {
8247496b59fSTrond Myklebust 	struct rpc_xprt *xprt = &transport->xprt;
82506ea0bfeSTrond Myklebust 	struct sock *sk = transport->inet;
82624ca9a84STrond Myklebust 	int ret = -EAGAIN;
827a246b010SChuck Lever 
828015747d2SChuck Lever 	trace_rpc_socket_nospace(req, transport);
829a246b010SChuck Lever 
830262965f5SChuck Lever 	/* Protect against races with write_space */
831b5e92419STrond Myklebust 	spin_lock(&xprt->transport_lock);
832a246b010SChuck Lever 
833262965f5SChuck Lever 	/* Don't race with disconnect */
834b6ddf64fSTrond Myklebust 	if (xprt_connected(xprt)) {
8357496b59fSTrond Myklebust 		/* wait for more buffer space */
8362790a624STrond Myklebust 		set_bit(XPRT_SOCK_NOSPACE, &transport->sock_state);
8377496b59fSTrond Myklebust 		set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
8387496b59fSTrond Myklebust 		sk->sk_write_pending++;
8397496b59fSTrond Myklebust 		xprt_wait_for_buffer_space(xprt);
8407496b59fSTrond Myklebust 	} else
8417496b59fSTrond Myklebust 		ret = -ENOTCONN;
8427496b59fSTrond Myklebust 
8437496b59fSTrond Myklebust 	spin_unlock(&xprt->transport_lock);
8447496b59fSTrond Myklebust 	return ret;
845d48f9ce7SDavid Vrabel }
8467496b59fSTrond Myklebust 
8477496b59fSTrond Myklebust static int xs_sock_nospace(struct rpc_rqst *req)
8487496b59fSTrond Myklebust {
8497496b59fSTrond Myklebust 	struct sock_xprt *transport =
8507496b59fSTrond Myklebust 		container_of(req->rq_xprt, struct sock_xprt, xprt);
8517496b59fSTrond Myklebust 	struct sock *sk = transport->inet;
8527496b59fSTrond Myklebust 	int ret = -EAGAIN;
8537496b59fSTrond Myklebust 
8547496b59fSTrond Myklebust 	lock_sock(sk);
8557496b59fSTrond Myklebust 	if (!sock_writeable(sk))
8567496b59fSTrond Myklebust 		ret = xs_nospace(req, transport);
8577496b59fSTrond Myklebust 	release_sock(sk);
8587496b59fSTrond Myklebust 	return ret;
8597496b59fSTrond Myklebust }
8607496b59fSTrond Myklebust 
861d0afde5fSTrond Myklebust static int xs_stream_nospace(struct rpc_rqst *req, bool vm_wait)
8627496b59fSTrond Myklebust {
8637496b59fSTrond Myklebust 	struct sock_xprt *transport =
8647496b59fSTrond Myklebust 		container_of(req->rq_xprt, struct sock_xprt, xprt);
8657496b59fSTrond Myklebust 	struct sock *sk = transport->inet;
8667496b59fSTrond Myklebust 	int ret = -EAGAIN;
8677496b59fSTrond Myklebust 
868d0afde5fSTrond Myklebust 	if (vm_wait)
869d0afde5fSTrond Myklebust 		return -ENOBUFS;
8707496b59fSTrond Myklebust 	lock_sock(sk);
8717496b59fSTrond Myklebust 	if (!sk_stream_memory_free(sk))
8727496b59fSTrond Myklebust 		ret = xs_nospace(req, transport);
8737496b59fSTrond Myklebust 	release_sock(sk);
8745e3771ceSTrond Myklebust 	return ret;
875a246b010SChuck Lever }
876a246b010SChuck Lever 
87772691a26STrond Myklebust static int xs_stream_prepare_request(struct rpc_rqst *req, struct xdr_buf *buf)
878277e4ab7STrond Myklebust {
87972691a26STrond Myklebust 	return xdr_alloc_bvec(buf, rpc_task_gfp_mask());
880277e4ab7STrond Myklebust }
881277e4ab7STrond Myklebust 
88261677eeeSChuck Lever /*
8834cd34e7cSTrond Myklebust  * Determine if the previous message in the stream was aborted before it
8844cd34e7cSTrond Myklebust  * could complete transmission.
8854cd34e7cSTrond Myklebust  */
8864cd34e7cSTrond Myklebust static bool
8874cd34e7cSTrond Myklebust xs_send_request_was_aborted(struct sock_xprt *transport, struct rpc_rqst *req)
8884cd34e7cSTrond Myklebust {
8894cd34e7cSTrond Myklebust 	return transport->xmit.offset != 0 && req->rq_bytes_sent == 0;
8904cd34e7cSTrond Myklebust }
8914cd34e7cSTrond Myklebust 
8924cd34e7cSTrond Myklebust /*
89306b5fc3aSTrond Myklebust  * Return the stream record marker field for a record of length < 2^31-1
89461677eeeSChuck Lever  */
89506b5fc3aSTrond Myklebust static rpc_fraghdr
89606b5fc3aSTrond Myklebust xs_stream_record_marker(struct xdr_buf *xdr)
89761677eeeSChuck Lever {
89806b5fc3aSTrond Myklebust 	if (!xdr->len)
89906b5fc3aSTrond Myklebust 		return 0;
90006b5fc3aSTrond Myklebust 	return cpu_to_be32(RPC_LAST_STREAM_FRAGMENT | (u32)xdr->len);
90161677eeeSChuck Lever }
90261677eeeSChuck Lever 
9039903cd1cSChuck Lever /**
904176e21eeSChuck Lever  * xs_local_send_request - write an RPC request to an AF_LOCAL socket
90550f484e2STrond Myklebust  * @req: pointer to RPC request
906176e21eeSChuck Lever  *
907176e21eeSChuck Lever  * Return values:
908176e21eeSChuck Lever  *        0:	The request has been sent
909176e21eeSChuck Lever  *   EAGAIN:	The socket was blocked, please call again later to
910176e21eeSChuck Lever  *		complete the request
911176e21eeSChuck Lever  * ENOTCONN:	Caller needs to invoke connect logic then call again
91212b20ce3SBhaskar Chowdhury  *    other:	Some other error occurred, the request was not sent
913176e21eeSChuck Lever  */
914adfa7144STrond Myklebust static int xs_local_send_request(struct rpc_rqst *req)
915176e21eeSChuck Lever {
916176e21eeSChuck Lever 	struct rpc_xprt *xprt = req->rq_xprt;
917176e21eeSChuck Lever 	struct sock_xprt *transport =
918176e21eeSChuck Lever 				container_of(xprt, struct sock_xprt, xprt);
919176e21eeSChuck Lever 	struct xdr_buf *xdr = &req->rq_snd_buf;
9207e3d3620STrond Myklebust 	rpc_fraghdr rm = xs_stream_record_marker(xdr);
9217e3d3620STrond Myklebust 	unsigned int msglen = rm ? req->rq_slen + sizeof(rm) : req->rq_slen;
9229e55eef4SChuck Lever 	struct msghdr msg = {
9239e55eef4SChuck Lever 		.msg_flags	= XS_SENDMSG_FLAGS,
9249e55eef4SChuck Lever 	};
925d0afde5fSTrond Myklebust 	bool vm_wait;
9263f649ab7SKees Cook 	unsigned int sent;
927176e21eeSChuck Lever 	int status;
928176e21eeSChuck Lever 
9294cd34e7cSTrond Myklebust 	/* Close the stream if the previous transmission was incomplete */
9304cd34e7cSTrond Myklebust 	if (xs_send_request_was_aborted(transport, req)) {
931f0043206STrond Myklebust 		xprt_force_disconnect(xprt);
9324cd34e7cSTrond Myklebust 		return -ENOTCONN;
9334cd34e7cSTrond Myklebust 	}
9344cd34e7cSTrond Myklebust 
935176e21eeSChuck Lever 	xs_pktdump("packet data:",
936176e21eeSChuck Lever 			req->rq_svec->iov_base, req->rq_svec->iov_len);
937176e21eeSChuck Lever 
938d0afde5fSTrond Myklebust 	vm_wait = sk_stream_is_writeable(transport->inet) ? true : false;
939d0afde5fSTrond Myklebust 
94078215759SChuck Lever 	req->rq_xtime = ktime_get();
9419e55eef4SChuck Lever 	status = xprt_sock_sendmsg(transport->sock, &msg, xdr,
9427e3d3620STrond Myklebust 				   transport->xmit.offset, rm, &sent);
943176e21eeSChuck Lever 	dprintk("RPC:       %s(%u) = %d\n",
9446c7a64e5STrond Myklebust 			__func__, xdr->len - transport->xmit.offset, status);
945743c69e7SNeilBrown 
946f279cd00SJason Baron 	if (likely(sent > 0) || status == 0) {
9476c7a64e5STrond Myklebust 		transport->xmit.offset += sent;
9486c7a64e5STrond Myklebust 		req->rq_bytes_sent = transport->xmit.offset;
9497e3d3620STrond Myklebust 		if (likely(req->rq_bytes_sent >= msglen)) {
9506c7a64e5STrond Myklebust 			req->rq_xmit_bytes_sent += transport->xmit.offset;
9516c7a64e5STrond Myklebust 			transport->xmit.offset = 0;
952176e21eeSChuck Lever 			return 0;
953176e21eeSChuck Lever 		}
954176e21eeSChuck Lever 		status = -EAGAIN;
955d0afde5fSTrond Myklebust 		vm_wait = false;
956176e21eeSChuck Lever 	}
957176e21eeSChuck Lever 
958176e21eeSChuck Lever 	switch (status) {
959176e21eeSChuck Lever 	case -EAGAIN:
960d0afde5fSTrond Myklebust 		status = xs_stream_nospace(req, vm_wait);
961176e21eeSChuck Lever 		break;
962176e21eeSChuck Lever 	default:
963176e21eeSChuck Lever 		dprintk("RPC:       sendmsg returned unrecognized error %d\n",
964176e21eeSChuck Lever 			-status);
965df561f66SGustavo A. R. Silva 		fallthrough;
966176e21eeSChuck Lever 	case -EPIPE:
967f0043206STrond Myklebust 		xprt_force_disconnect(xprt);
968176e21eeSChuck Lever 		status = -ENOTCONN;
969176e21eeSChuck Lever 	}
970176e21eeSChuck Lever 
971176e21eeSChuck Lever 	return status;
972176e21eeSChuck Lever }
973176e21eeSChuck Lever 
974176e21eeSChuck Lever /**
975262965f5SChuck Lever  * xs_udp_send_request - write an RPC request to a UDP socket
97650f484e2STrond Myklebust  * @req: pointer to RPC request
9779903cd1cSChuck Lever  *
9789903cd1cSChuck Lever  * Return values:
9799903cd1cSChuck Lever  *        0:	The request has been sent
9809903cd1cSChuck Lever  *   EAGAIN:	The socket was blocked, please call again later to
9819903cd1cSChuck Lever  *		complete the request
982262965f5SChuck Lever  * ENOTCONN:	Caller needs to invoke connect logic then call again
98325985edcSLucas De Marchi  *    other:	Some other error occurred, the request was not sent
9849903cd1cSChuck Lever  */
985adfa7144STrond Myklebust static int xs_udp_send_request(struct rpc_rqst *req)
986a246b010SChuck Lever {
987a246b010SChuck Lever 	struct rpc_xprt *xprt = req->rq_xprt;
988ee0ac0c2SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
989262965f5SChuck Lever 	struct xdr_buf *xdr = &req->rq_snd_buf;
9909e55eef4SChuck Lever 	struct msghdr msg = {
9919e55eef4SChuck Lever 		.msg_name	= xs_addr(xprt),
9929e55eef4SChuck Lever 		.msg_namelen	= xprt->addrlen,
9939e55eef4SChuck Lever 		.msg_flags	= XS_SENDMSG_FLAGS,
9949e55eef4SChuck Lever 	};
9953f649ab7SKees Cook 	unsigned int sent;
996262965f5SChuck Lever 	int status;
997262965f5SChuck Lever 
998262965f5SChuck Lever 	xs_pktdump("packet data:",
999262965f5SChuck Lever 				req->rq_svec->iov_base,
1000262965f5SChuck Lever 				req->rq_svec->iov_len);
1001262965f5SChuck Lever 
100201d37c42STrond Myklebust 	if (!xprt_bound(xprt))
100301d37c42STrond Myklebust 		return -ENOTCONN;
100475891f50STrond Myklebust 
100575891f50STrond Myklebust 	if (!xprt_request_get_cong(xprt, req))
100675891f50STrond Myklebust 		return -EBADSLT;
100775891f50STrond Myklebust 
1008ff053dbbSTrond Myklebust 	status = xdr_alloc_bvec(xdr, rpc_task_gfp_mask());
1009ff053dbbSTrond Myklebust 	if (status < 0)
1010ff053dbbSTrond Myklebust 		return status;
101178215759SChuck Lever 	req->rq_xtime = ktime_get();
10129e55eef4SChuck Lever 	status = xprt_sock_sendmsg(transport->sock, &msg, xdr, 0, 0, &sent);
1013262965f5SChuck Lever 
1014262965f5SChuck Lever 	dprintk("RPC:       xs_udp_send_request(%u) = %d\n",
10156c7a64e5STrond Myklebust 			xdr->len, status);
1016262965f5SChuck Lever 
10173dedbb5cSJason Baron 	/* firewall is blocking us, don't return -EAGAIN or we end up looping */
10183dedbb5cSJason Baron 	if (status == -EPERM)
10193dedbb5cSJason Baron 		goto process_status;
10203dedbb5cSJason Baron 
1021743c69e7SNeilBrown 	if (status == -EAGAIN && sock_writeable(transport->inet))
1022743c69e7SNeilBrown 		status = -ENOBUFS;
1023743c69e7SNeilBrown 
1024f279cd00SJason Baron 	if (sent > 0 || status == 0) {
1025f279cd00SJason Baron 		req->rq_xmit_bytes_sent += sent;
1026f279cd00SJason Baron 		if (sent >= req->rq_slen)
1027262965f5SChuck Lever 			return 0;
1028262965f5SChuck Lever 		/* Still some bytes left; set up for a retry later. */
1029262965f5SChuck Lever 		status = -EAGAIN;
10302199700fSTrond Myklebust 	}
1031262965f5SChuck Lever 
10323dedbb5cSJason Baron process_status:
1033262965f5SChuck Lever 	switch (status) {
1034fba91afbSTrond Myklebust 	case -ENOTSOCK:
1035fba91afbSTrond Myklebust 		status = -ENOTCONN;
1036fba91afbSTrond Myklebust 		/* Should we call xs_close() here? */
1037fba91afbSTrond Myklebust 		break;
1038b6ddf64fSTrond Myklebust 	case -EAGAIN:
10397496b59fSTrond Myklebust 		status = xs_sock_nospace(req);
1040b6ddf64fSTrond Myklebust 		break;
1041262965f5SChuck Lever 	case -ENETUNREACH:
10423601c4a9STrond Myklebust 	case -ENOBUFS:
1043262965f5SChuck Lever 	case -EPIPE:
1044262965f5SChuck Lever 	case -ECONNREFUSED:
10453dedbb5cSJason Baron 	case -EPERM:
1046262965f5SChuck Lever 		/* When the server has died, an ICMP port unreachable message
1047262965f5SChuck Lever 		 * prompts ECONNREFUSED. */
104813331a55STrond Myklebust 		break;
104913331a55STrond Myklebust 	default:
105013331a55STrond Myklebust 		dprintk("RPC:       sendmsg returned unrecognized error %d\n",
105113331a55STrond Myklebust 			-status);
1052262965f5SChuck Lever 	}
10535fe46e9dSBian Naimeng 
1054262965f5SChuck Lever 	return status;
1055262965f5SChuck Lever }
1056262965f5SChuck Lever 
1057e06799f9STrond Myklebust /**
1058262965f5SChuck Lever  * xs_tcp_send_request - write an RPC request to a TCP socket
105950f484e2STrond Myklebust  * @req: pointer to RPC request
1060262965f5SChuck Lever  *
1061262965f5SChuck Lever  * Return values:
1062262965f5SChuck Lever  *        0:	The request has been sent
1063262965f5SChuck Lever  *   EAGAIN:	The socket was blocked, please call again later to
1064262965f5SChuck Lever  *		complete the request
1065262965f5SChuck Lever  * ENOTCONN:	Caller needs to invoke connect logic then call again
106625985edcSLucas De Marchi  *    other:	Some other error occurred, the request was not sent
1067262965f5SChuck Lever  *
1068262965f5SChuck Lever  * XXX: In the case of soft timeouts, should we eventually give up
1069262965f5SChuck Lever  *	if sendmsg is not able to make progress?
1070262965f5SChuck Lever  */
1071adfa7144STrond Myklebust static int xs_tcp_send_request(struct rpc_rqst *req)
1072262965f5SChuck Lever {
1073262965f5SChuck Lever 	struct rpc_xprt *xprt = req->rq_xprt;
1074ee0ac0c2SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1075262965f5SChuck Lever 	struct xdr_buf *xdr = &req->rq_snd_buf;
10767e3d3620STrond Myklebust 	rpc_fraghdr rm = xs_stream_record_marker(xdr);
10777e3d3620STrond Myklebust 	unsigned int msglen = rm ? req->rq_slen + sizeof(rm) : req->rq_slen;
10789e55eef4SChuck Lever 	struct msghdr msg = {
10799e55eef4SChuck Lever 		.msg_flags	= XS_SENDMSG_FLAGS,
10809e55eef4SChuck Lever 	};
1081d0afde5fSTrond Myklebust 	bool vm_wait;
10823f649ab7SKees Cook 	unsigned int sent;
1083b595bb15SChuck Lever 	int status;
1084a246b010SChuck Lever 
10854cd34e7cSTrond Myklebust 	/* Close the stream if the previous transmission was incomplete */
10864cd34e7cSTrond Myklebust 	if (xs_send_request_was_aborted(transport, req)) {
10874cd34e7cSTrond Myklebust 		if (transport->sock != NULL)
10884cd34e7cSTrond Myklebust 			kernel_sock_shutdown(transport->sock, SHUT_RDWR);
10894cd34e7cSTrond Myklebust 		return -ENOTCONN;
10904cd34e7cSTrond Myklebust 	}
1091d275880aSTrond Myklebust 	if (!transport->inet)
1092d275880aSTrond Myklebust 		return -ENOTCONN;
10934cd34e7cSTrond Myklebust 
1094262965f5SChuck Lever 	xs_pktdump("packet data:",
1095262965f5SChuck Lever 				req->rq_svec->iov_base,
1096262965f5SChuck Lever 				req->rq_svec->iov_len);
1097a246b010SChuck Lever 
10987196dbb0STrond Myklebust 	if (test_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state))
10997196dbb0STrond Myklebust 		xs_tcp_set_socket_timeouts(xprt, transport->sock);
11007196dbb0STrond Myklebust 
11013b21f757STrond Myklebust 	xs_set_srcport(transport, transport->sock);
11023b21f757STrond Myklebust 
1103a246b010SChuck Lever 	/* Continue transmitting the packet/record. We must be careful
1104a246b010SChuck Lever 	 * to cope with writespace callbacks arriving _after_ we have
1105262965f5SChuck Lever 	 * called sendmsg(). */
110678215759SChuck Lever 	req->rq_xtime = ktime_get();
1107d737e5d4STrond Myklebust 	tcp_sock_set_cork(transport->inet, true);
1108d0afde5fSTrond Myklebust 
1109d0afde5fSTrond Myklebust 	vm_wait = sk_stream_is_writeable(transport->inet) ? true : false;
1110d0afde5fSTrond Myklebust 
1111d0afde5fSTrond Myklebust 	do {
11129e55eef4SChuck Lever 		status = xprt_sock_sendmsg(transport->sock, &msg, xdr,
11137e3d3620STrond Myklebust 					   transport->xmit.offset, rm, &sent);
1114a246b010SChuck Lever 
1115262965f5SChuck Lever 		dprintk("RPC:       xs_tcp_send_request(%u) = %d\n",
11166c7a64e5STrond Myklebust 				xdr->len - transport->xmit.offset, status);
1117262965f5SChuck Lever 
1118a246b010SChuck Lever 		/* If we've sent the entire packet, immediately
1119a246b010SChuck Lever 		 * reset the count of bytes sent. */
11206c7a64e5STrond Myklebust 		transport->xmit.offset += sent;
11216c7a64e5STrond Myklebust 		req->rq_bytes_sent = transport->xmit.offset;
11227e3d3620STrond Myklebust 		if (likely(req->rq_bytes_sent >= msglen)) {
11236c7a64e5STrond Myklebust 			req->rq_xmit_bytes_sent += transport->xmit.offset;
11246c7a64e5STrond Myklebust 			transport->xmit.offset = 0;
1125d737e5d4STrond Myklebust 			if (atomic_long_read(&xprt->xmit_queuelen) == 1)
1126d737e5d4STrond Myklebust 				tcp_sock_set_cork(transport->inet, false);
1127a246b010SChuck Lever 			return 0;
1128a246b010SChuck Lever 		}
1129262965f5SChuck Lever 
11309ffadfbcSTrond Myklebust 		WARN_ON_ONCE(sent == 0 && status == 0);
11319ffadfbcSTrond Myklebust 
1132d0afde5fSTrond Myklebust 		if (sent > 0)
11339ffadfbcSTrond Myklebust 			vm_wait = false;
1134d0afde5fSTrond Myklebust 
1135d0afde5fSTrond Myklebust 	} while (status == 0);
1136a246b010SChuck Lever 
1137262965f5SChuck Lever 	switch (status) {
1138fba91afbSTrond Myklebust 	case -ENOTSOCK:
1139fba91afbSTrond Myklebust 		status = -ENOTCONN;
1140fba91afbSTrond Myklebust 		/* Should we call xs_close() here? */
1141fba91afbSTrond Myklebust 		break;
1142262965f5SChuck Lever 	case -EAGAIN:
1143d0afde5fSTrond Myklebust 		status = xs_stream_nospace(req, vm_wait);
1144262965f5SChuck Lever 		break;
1145262965f5SChuck Lever 	case -ECONNRESET:
1146262965f5SChuck Lever 	case -ECONNREFUSED:
1147262965f5SChuck Lever 	case -ENOTCONN:
11483913c78cSTrond Myklebust 	case -EADDRINUSE:
1149b5872f0cSTrond Myklebust 	case -ENOBUFS:
1150b9d2bb2eSTrond Myklebust 	case -EPIPE:
115113331a55STrond Myklebust 		break;
115213331a55STrond Myklebust 	default:
115313331a55STrond Myklebust 		dprintk("RPC:       sendmsg returned unrecognized error %d\n",
115413331a55STrond Myklebust 			-status);
1155a246b010SChuck Lever 	}
11565fe46e9dSBian Naimeng 
1157a246b010SChuck Lever 	return status;
1158a246b010SChuck Lever }
1159a246b010SChuck Lever 
11602a9e1cfaSTrond Myklebust static void xs_save_old_callbacks(struct sock_xprt *transport, struct sock *sk)
11612a9e1cfaSTrond Myklebust {
11622a9e1cfaSTrond Myklebust 	transport->old_data_ready = sk->sk_data_ready;
11632a9e1cfaSTrond Myklebust 	transport->old_state_change = sk->sk_state_change;
11642a9e1cfaSTrond Myklebust 	transport->old_write_space = sk->sk_write_space;
11652118071dSTrond Myklebust 	transport->old_error_report = sk->sk_error_report;
11662a9e1cfaSTrond Myklebust }
11672a9e1cfaSTrond Myklebust 
11682a9e1cfaSTrond Myklebust static void xs_restore_old_callbacks(struct sock_xprt *transport, struct sock *sk)
11692a9e1cfaSTrond Myklebust {
11702a9e1cfaSTrond Myklebust 	sk->sk_data_ready = transport->old_data_ready;
11712a9e1cfaSTrond Myklebust 	sk->sk_state_change = transport->old_state_change;
11722a9e1cfaSTrond Myklebust 	sk->sk_write_space = transport->old_write_space;
11732118071dSTrond Myklebust 	sk->sk_error_report = transport->old_error_report;
11742118071dSTrond Myklebust }
11752118071dSTrond Myklebust 
117642d42a5bSTrond Myklebust static void xs_sock_reset_state_flags(struct rpc_xprt *xprt)
117742d42a5bSTrond Myklebust {
117842d42a5bSTrond Myklebust 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
117942d42a5bSTrond Myklebust 
118042d42a5bSTrond Myklebust 	clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state);
11814f8943f8STrond Myklebust 	clear_bit(XPRT_SOCK_WAKE_ERROR, &transport->sock_state);
11824f8943f8STrond Myklebust 	clear_bit(XPRT_SOCK_WAKE_WRITE, &transport->sock_state);
11834f8943f8STrond Myklebust 	clear_bit(XPRT_SOCK_WAKE_DISCONNECT, &transport->sock_state);
11842790a624STrond Myklebust 	clear_bit(XPRT_SOCK_NOSPACE, &transport->sock_state);
11854f8943f8STrond Myklebust }
11864f8943f8STrond Myklebust 
11874f8943f8STrond Myklebust static void xs_run_error_worker(struct sock_xprt *transport, unsigned int nr)
11884f8943f8STrond Myklebust {
11894f8943f8STrond Myklebust 	set_bit(nr, &transport->sock_state);
11904f8943f8STrond Myklebust 	queue_work(xprtiod_workqueue, &transport->error_worker);
119142d42a5bSTrond Myklebust }
119242d42a5bSTrond Myklebust 
1193b70ae915STrond Myklebust static void xs_sock_reset_connection_flags(struct rpc_xprt *xprt)
1194b70ae915STrond Myklebust {
1195d896ba83STrond Myklebust 	xprt->connect_cookie++;
1196b70ae915STrond Myklebust 	smp_mb__before_atomic();
1197b70ae915STrond Myklebust 	clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
1198b70ae915STrond Myklebust 	clear_bit(XPRT_CLOSING, &xprt->state);
119942d42a5bSTrond Myklebust 	xs_sock_reset_state_flags(xprt);
1200b70ae915STrond Myklebust 	smp_mb__after_atomic();
1201b70ae915STrond Myklebust }
1202b70ae915STrond Myklebust 
12032118071dSTrond Myklebust /**
12042118071dSTrond Myklebust  * xs_error_report - callback to handle TCP socket state errors
12052118071dSTrond Myklebust  * @sk: socket
12062118071dSTrond Myklebust  *
12072118071dSTrond Myklebust  * Note: we don't call sock_error() since there may be a rpc_task
12082118071dSTrond Myklebust  * using the socket, and so we don't want to clear sk->sk_err.
12092118071dSTrond Myklebust  */
12102118071dSTrond Myklebust static void xs_error_report(struct sock *sk)
12112118071dSTrond Myklebust {
12124f8943f8STrond Myklebust 	struct sock_xprt *transport;
12132118071dSTrond Myklebust 	struct rpc_xprt *xprt;
12142118071dSTrond Myklebust 
12152118071dSTrond Myklebust 	if (!(xprt = xprt_from_sock(sk)))
1216ea9afca8STrond Myklebust 		return;
12172118071dSTrond Myklebust 
12184f8943f8STrond Myklebust 	transport = container_of(xprt, struct sock_xprt, xprt);
1219af84537dSBenjamin Coddington 	transport->xprt_err = -sk->sk_err;
1220af84537dSBenjamin Coddington 	if (transport->xprt_err == 0)
1221ea9afca8STrond Myklebust 		return;
12222118071dSTrond Myklebust 	dprintk("RPC:       xs_error_report client %p, error=%d...\n",
1223af84537dSBenjamin Coddington 			xprt, -transport->xprt_err);
1224af84537dSBenjamin Coddington 	trace_rpc_socket_error(xprt, sk->sk_socket, transport->xprt_err);
1225af84537dSBenjamin Coddington 
1226af84537dSBenjamin Coddington 	/* barrier ensures xprt_err is set before XPRT_SOCK_WAKE_ERROR */
1227af84537dSBenjamin Coddington 	smp_mb__before_atomic();
12284f8943f8STrond Myklebust 	xs_run_error_worker(transport, XPRT_SOCK_WAKE_ERROR);
12292a9e1cfaSTrond Myklebust }
12302a9e1cfaSTrond Myklebust 
1231fe315e76SChuck Lever static void xs_reset_transport(struct sock_xprt *transport)
1232a246b010SChuck Lever {
1233ee0ac0c2SChuck Lever 	struct socket *sock = transport->sock;
1234ee0ac0c2SChuck Lever 	struct sock *sk = transport->inet;
12356cc7e908STrond Myklebust 	struct rpc_xprt *xprt = &transport->xprt;
1236a73881c9STrond Myklebust 	struct file *filp = transport->file;
1237a246b010SChuck Lever 
1238fe315e76SChuck Lever 	if (sk == NULL)
1239fe315e76SChuck Lever 		return;
1240f0043206STrond Myklebust 	/*
1241f0043206STrond Myklebust 	 * Make sure we're calling this in a context from which it is safe
1242f0043206STrond Myklebust 	 * to call __fput_sync(). In practice that means rpciod and the
1243f0043206STrond Myklebust 	 * system workqueue.
1244f0043206STrond Myklebust 	 */
1245f0043206STrond Myklebust 	if (!(current->flags & PF_WQ_WORKER)) {
1246f0043206STrond Myklebust 		WARN_ON_ONCE(1);
1247f0043206STrond Myklebust 		set_bit(XPRT_CLOSE_WAIT, &xprt->state);
1248f0043206STrond Myklebust 		return;
1249f0043206STrond Myklebust 	}
12509903cd1cSChuck Lever 
1251264d1df3SJeff Layton 	if (atomic_read(&transport->xprt.swapper))
1252264d1df3SJeff Layton 		sk_clear_memalloc(sk);
1253264d1df3SJeff Layton 
125475eb6af7SChuck Lever 	tls_handshake_cancel(sk);
125575eb6af7SChuck Lever 
125609939204STrond Myklebust 	kernel_sock_shutdown(sock, SHUT_RDWR);
125709939204STrond Myklebust 
1258edc1b01cSTrond Myklebust 	mutex_lock(&transport->recv_mutex);
1259ea9afca8STrond Myklebust 	lock_sock(sk);
1260ee0ac0c2SChuck Lever 	transport->inet = NULL;
1261ee0ac0c2SChuck Lever 	transport->sock = NULL;
1262a73881c9STrond Myklebust 	transport->file = NULL;
1263a246b010SChuck Lever 
1264a246b010SChuck Lever 	sk->sk_user_data = NULL;
12652a9e1cfaSTrond Myklebust 
12662a9e1cfaSTrond Myklebust 	xs_restore_old_callbacks(transport, sk);
12670c78789eSTrond Myklebust 	xprt_clear_connected(xprt);
12686cc7e908STrond Myklebust 	xs_sock_reset_connection_flags(xprt);
1269ae053551STrond Myklebust 	/* Reset stream record info */
1270ae053551STrond Myklebust 	xs_stream_reset_connect(transport);
1271ea9afca8STrond Myklebust 	release_sock(sk);
1272edc1b01cSTrond Myklebust 	mutex_unlock(&transport->recv_mutex);
1273a246b010SChuck Lever 
12746cc7e908STrond Myklebust 	trace_rpc_socket_close(xprt, sock);
1275f0043206STrond Myklebust 	__fput_sync(filp);
12760445f92cSTrond Myklebust 
12770445f92cSTrond Myklebust 	xprt_disconnect_done(xprt);
1278fe315e76SChuck Lever }
1279fe315e76SChuck Lever 
1280fe315e76SChuck Lever /**
1281fe315e76SChuck Lever  * xs_close - close a socket
1282fe315e76SChuck Lever  * @xprt: transport
1283fe315e76SChuck Lever  *
1284fe315e76SChuck Lever  * This is used when all requests are complete; ie, no DRC state remains
1285fe315e76SChuck Lever  * on the server we want to save.
1286f75e6745STrond Myklebust  *
1287f75e6745STrond Myklebust  * The caller _must_ be holding XPRT_LOCKED in order to avoid issues with
1288f75e6745STrond Myklebust  * xs_reset_transport() zeroing the socket from underneath a writer.
1289fe315e76SChuck Lever  */
1290fe315e76SChuck Lever static void xs_close(struct rpc_xprt *xprt)
1291fe315e76SChuck Lever {
1292fe315e76SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1293fe315e76SChuck Lever 
1294fe315e76SChuck Lever 	dprintk("RPC:       xs_close xprt %p\n", xprt);
1295fe315e76SChuck Lever 
1296*5dd5ad68SChuck Lever 	if (transport->sock)
1297*5dd5ad68SChuck Lever 		tls_handshake_close(transport->sock);
1298fe315e76SChuck Lever 	xs_reset_transport(transport);
129961d0a8e6SNeil Brown 	xprt->reestablish_timeout = 0;
1300a246b010SChuck Lever }
1301a246b010SChuck Lever 
13024a068258SChuck Lever static void xs_inject_disconnect(struct rpc_xprt *xprt)
13034a068258SChuck Lever {
13044a068258SChuck Lever 	dprintk("RPC:       injecting transport disconnect on xprt=%p\n",
13054a068258SChuck Lever 		xprt);
13064a068258SChuck Lever 	xprt_disconnect_done(xprt);
13074a068258SChuck Lever }
13084a068258SChuck Lever 
1309315f3812SKinglong Mee static void xs_xprt_free(struct rpc_xprt *xprt)
1310315f3812SKinglong Mee {
1311315f3812SKinglong Mee 	xs_free_peer_addresses(xprt);
1312315f3812SKinglong Mee 	xprt_free(xprt);
1313315f3812SKinglong Mee }
1314315f3812SKinglong Mee 
13159903cd1cSChuck Lever /**
13169903cd1cSChuck Lever  * xs_destroy - prepare to shutdown a transport
13179903cd1cSChuck Lever  * @xprt: doomed transport
13189903cd1cSChuck Lever  *
13199903cd1cSChuck Lever  */
13209903cd1cSChuck Lever static void xs_destroy(struct rpc_xprt *xprt)
1321a246b010SChuck Lever {
132203c78827STrond Myklebust 	struct sock_xprt *transport = container_of(xprt,
132303c78827STrond Myklebust 			struct sock_xprt, xprt);
13249903cd1cSChuck Lever 	dprintk("RPC:       xs_destroy xprt %p\n", xprt);
13259903cd1cSChuck Lever 
132603c78827STrond Myklebust 	cancel_delayed_work_sync(&transport->connect_worker);
1327a1311d87STrond Myklebust 	xs_close(xprt);
1328edc1b01cSTrond Myklebust 	cancel_work_sync(&transport->recv_worker);
1329b5e92419STrond Myklebust 	cancel_work_sync(&transport->error_worker);
1330315f3812SKinglong Mee 	xs_xprt_free(xprt);
1331a1311d87STrond Myklebust 	module_put(THIS_MODULE);
1332a246b010SChuck Lever }
1333a246b010SChuck Lever 
13349903cd1cSChuck Lever /**
1335f9b2ee71STrond Myklebust  * xs_udp_data_read_skb - receive callback for UDP sockets
1336f9b2ee71STrond Myklebust  * @xprt: transport
1337f9b2ee71STrond Myklebust  * @sk: socket
1338f9b2ee71STrond Myklebust  * @skb: skbuff
13399903cd1cSChuck Lever  *
1340a246b010SChuck Lever  */
1341f9b2ee71STrond Myklebust static void xs_udp_data_read_skb(struct rpc_xprt *xprt,
1342f9b2ee71STrond Myklebust 		struct sock *sk,
1343f9b2ee71STrond Myklebust 		struct sk_buff *skb)
1344a246b010SChuck Lever {
1345a246b010SChuck Lever 	struct rpc_task *task;
1346a246b010SChuck Lever 	struct rpc_rqst *rovr;
1347f9b2ee71STrond Myklebust 	int repsize, copied;
1348d8ed029dSAlexey Dobriyan 	u32 _xid;
1349d8ed029dSAlexey Dobriyan 	__be32 *xp;
1350a246b010SChuck Lever 
13511da8c681SWillem de Bruijn 	repsize = skb->len;
1352a246b010SChuck Lever 	if (repsize < 4) {
13539903cd1cSChuck Lever 		dprintk("RPC:       impossible RPC reply size %d!\n", repsize);
1354f9b2ee71STrond Myklebust 		return;
1355a246b010SChuck Lever 	}
1356a246b010SChuck Lever 
1357a246b010SChuck Lever 	/* Copy the XID from the skb... */
13581da8c681SWillem de Bruijn 	xp = skb_header_pointer(skb, 0, sizeof(_xid), &_xid);
1359a246b010SChuck Lever 	if (xp == NULL)
1360f9b2ee71STrond Myklebust 		return;
1361a246b010SChuck Lever 
1362a246b010SChuck Lever 	/* Look up and lock the request corresponding to the given XID */
136375c84151STrond Myklebust 	spin_lock(&xprt->queue_lock);
1364a246b010SChuck Lever 	rovr = xprt_lookup_rqst(xprt, *xp);
1365a246b010SChuck Lever 	if (!rovr)
1366a246b010SChuck Lever 		goto out_unlock;
1367729749bbSTrond Myklebust 	xprt_pin_rqst(rovr);
1368ecd465eeSChuck Lever 	xprt_update_rtt(rovr->rq_task);
136975c84151STrond Myklebust 	spin_unlock(&xprt->queue_lock);
1370a246b010SChuck Lever 	task = rovr->rq_task;
1371a246b010SChuck Lever 
1372a246b010SChuck Lever 	if ((copied = rovr->rq_private_buf.buflen) > repsize)
1373a246b010SChuck Lever 		copied = repsize;
1374a246b010SChuck Lever 
1375a246b010SChuck Lever 	/* Suck it into the iovec, verify checksum if not done by hw. */
13761781f7f5SHerbert Xu 	if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) {
137775c84151STrond Myklebust 		spin_lock(&xprt->queue_lock);
13780afa6b44STrond Myklebust 		__UDPX_INC_STATS(sk, UDP_MIB_INERRORS);
1379729749bbSTrond Myklebust 		goto out_unpin;
13801781f7f5SHerbert Xu 	}
13811781f7f5SHerbert Xu 
1382a246b010SChuck Lever 
1383b5e92419STrond Myklebust 	spin_lock(&xprt->transport_lock);
13846a24dfb6STrond Myklebust 	xprt_adjust_cwnd(xprt, task, copied);
1385b5e92419STrond Myklebust 	spin_unlock(&xprt->transport_lock);
138675c84151STrond Myklebust 	spin_lock(&xprt->queue_lock);
13871570c1e4SChuck Lever 	xprt_complete_rqst(task, copied);
13880afa6b44STrond Myklebust 	__UDPX_INC_STATS(sk, UDP_MIB_INDATAGRAMS);
1389729749bbSTrond Myklebust out_unpin:
1390729749bbSTrond Myklebust 	xprt_unpin_rqst(rovr);
1391a246b010SChuck Lever  out_unlock:
139275c84151STrond Myklebust 	spin_unlock(&xprt->queue_lock);
1393f9b2ee71STrond Myklebust }
1394f9b2ee71STrond Myklebust 
1395f9b2ee71STrond Myklebust static void xs_udp_data_receive(struct sock_xprt *transport)
1396f9b2ee71STrond Myklebust {
1397f9b2ee71STrond Myklebust 	struct sk_buff *skb;
1398f9b2ee71STrond Myklebust 	struct sock *sk;
1399f9b2ee71STrond Myklebust 	int err;
1400f9b2ee71STrond Myklebust 
1401f9b2ee71STrond Myklebust 	mutex_lock(&transport->recv_mutex);
1402f9b2ee71STrond Myklebust 	sk = transport->inet;
1403f9b2ee71STrond Myklebust 	if (sk == NULL)
1404f9b2ee71STrond Myklebust 		goto out;
1405f9b2ee71STrond Myklebust 	for (;;) {
1406ec095263SOliver Hartkopp 		skb = skb_recv_udp(sk, MSG_DONTWAIT, &err);
14074f546149STrond Myklebust 		if (skb == NULL)
14084f546149STrond Myklebust 			break;
1409f9b2ee71STrond Myklebust 		xs_udp_data_read_skb(&transport->xprt, sk, skb);
1410850cbaddSPaolo Abeni 		consume_skb(skb);
14110af3442aSTrond Myklebust 		cond_resched();
1412f9b2ee71STrond Myklebust 	}
14130ffe86f4STrond Myklebust 	xs_poll_check_readable(transport);
1414a246b010SChuck Lever out:
1415f9b2ee71STrond Myklebust 	mutex_unlock(&transport->recv_mutex);
1416f9b2ee71STrond Myklebust }
1417f9b2ee71STrond Myklebust 
1418f9b2ee71STrond Myklebust static void xs_udp_data_receive_workfn(struct work_struct *work)
1419f9b2ee71STrond Myklebust {
1420f9b2ee71STrond Myklebust 	struct sock_xprt *transport =
1421f9b2ee71STrond Myklebust 		container_of(work, struct sock_xprt, recv_worker);
1422a1231fdaSTrond Myklebust 	unsigned int pflags = memalloc_nofs_save();
1423a1231fdaSTrond Myklebust 
1424f9b2ee71STrond Myklebust 	xs_udp_data_receive(transport);
1425a1231fdaSTrond Myklebust 	memalloc_nofs_restore(pflags);
1426f9b2ee71STrond Myklebust }
1427f9b2ee71STrond Myklebust 
1428f9b2ee71STrond Myklebust /**
1429f67939e4SChuck Lever  * xs_data_ready - "data ready" callback for sockets
1430f9b2ee71STrond Myklebust  * @sk: socket with data to read
1431f9b2ee71STrond Myklebust  *
1432f9b2ee71STrond Myklebust  */
1433f9b2ee71STrond Myklebust static void xs_data_ready(struct sock *sk)
1434f9b2ee71STrond Myklebust {
1435f9b2ee71STrond Myklebust 	struct rpc_xprt *xprt;
1436f9b2ee71STrond Myklebust 
143740e0b090SPeilin Ye 	trace_sk_data_ready(sk);
143840e0b090SPeilin Ye 
1439f9b2ee71STrond Myklebust 	xprt = xprt_from_sock(sk);
1440f9b2ee71STrond Myklebust 	if (xprt != NULL) {
1441f9b2ee71STrond Myklebust 		struct sock_xprt *transport = container_of(xprt,
1442f9b2ee71STrond Myklebust 				struct sock_xprt, xprt);
1443f67939e4SChuck Lever 
1444f67939e4SChuck Lever 		trace_xs_data_ready(xprt);
1445f67939e4SChuck Lever 
14465157b956STrond Myklebust 		transport->old_data_ready(sk);
14470d3ca07fSChuck Lever 
14480d3ca07fSChuck Lever 		if (test_bit(XPRT_SOCK_IGNORE_RECV, &transport->sock_state))
14490d3ca07fSChuck Lever 			return;
14500d3ca07fSChuck Lever 
14515157b956STrond Myklebust 		/* Any data means we had a useful conversation, so
14525157b956STrond Myklebust 		 * then we don't need to delay the next reconnect
14535157b956STrond Myklebust 		 */
14545157b956STrond Myklebust 		if (xprt->reestablish_timeout)
14555157b956STrond Myklebust 			xprt->reestablish_timeout = 0;
145642d42a5bSTrond Myklebust 		if (!test_and_set_bit(XPRT_SOCK_DATA_READY, &transport->sock_state))
145740a5f1b1STrond Myklebust 			queue_work(xprtiod_workqueue, &transport->recv_worker);
1458f9b2ee71STrond Myklebust 	}
1459a246b010SChuck Lever }
1460a246b010SChuck Lever 
1461a519fc7aSTrond Myklebust /*
1462a519fc7aSTrond Myklebust  * Helper function to force a TCP close if the server is sending
1463a519fc7aSTrond Myklebust  * junk and/or it has put us in CLOSE_WAIT
1464a519fc7aSTrond Myklebust  */
1465a519fc7aSTrond Myklebust static void xs_tcp_force_close(struct rpc_xprt *xprt)
1466a519fc7aSTrond Myklebust {
1467a519fc7aSTrond Myklebust 	xprt_force_disconnect(xprt);
1468a519fc7aSTrond Myklebust }
1469a519fc7aSTrond Myklebust 
14709e00abc3STrond Myklebust #if defined(CONFIG_SUNRPC_BACKCHANNEL)
14716b26cc8cSChuck Lever static size_t xs_tcp_bc_maxpayload(struct rpc_xprt *xprt)
14726b26cc8cSChuck Lever {
14736b26cc8cSChuck Lever 	return PAGE_SIZE;
14746b26cc8cSChuck Lever }
14759e00abc3STrond Myklebust #endif /* CONFIG_SUNRPC_BACKCHANNEL */
147644b98efdSRicardo Labiaga 
14779903cd1cSChuck Lever /**
1478efce2d0bSTrond Myklebust  * xs_local_state_change - callback to handle AF_LOCAL socket state changes
1479efce2d0bSTrond Myklebust  * @sk: socket whose state has changed
1480efce2d0bSTrond Myklebust  *
1481efce2d0bSTrond Myklebust  */
1482efce2d0bSTrond Myklebust static void xs_local_state_change(struct sock *sk)
1483efce2d0bSTrond Myklebust {
1484efce2d0bSTrond Myklebust 	struct rpc_xprt *xprt;
1485efce2d0bSTrond Myklebust 	struct sock_xprt *transport;
1486efce2d0bSTrond Myklebust 
1487efce2d0bSTrond Myklebust 	if (!(xprt = xprt_from_sock(sk)))
1488efce2d0bSTrond Myklebust 		return;
1489efce2d0bSTrond Myklebust 	transport = container_of(xprt, struct sock_xprt, xprt);
1490efce2d0bSTrond Myklebust 	if (sk->sk_shutdown & SHUTDOWN_MASK) {
1491efce2d0bSTrond Myklebust 		clear_bit(XPRT_CONNECTED, &xprt->state);
1492efce2d0bSTrond Myklebust 		/* Trigger the socket release */
1493efce2d0bSTrond Myklebust 		xs_run_error_worker(transport, XPRT_SOCK_WAKE_DISCONNECT);
1494efce2d0bSTrond Myklebust 	}
1495efce2d0bSTrond Myklebust }
1496efce2d0bSTrond Myklebust 
1497efce2d0bSTrond Myklebust /**
14989903cd1cSChuck Lever  * xs_tcp_state_change - callback to handle TCP socket state changes
14999903cd1cSChuck Lever  * @sk: socket whose state has changed
15009903cd1cSChuck Lever  *
15019903cd1cSChuck Lever  */
15029903cd1cSChuck Lever static void xs_tcp_state_change(struct sock *sk)
1503a246b010SChuck Lever {
1504a246b010SChuck Lever 	struct rpc_xprt *xprt;
15050fdea1e8STrond Myklebust 	struct sock_xprt *transport;
1506a246b010SChuck Lever 
1507a246b010SChuck Lever 	if (!(xprt = xprt_from_sock(sk)))
1508ea9afca8STrond Myklebust 		return;
15099903cd1cSChuck Lever 	dprintk("RPC:       xs_tcp_state_change client %p...\n", xprt);
1510669502ffSAndy Chittenden 	dprintk("RPC:       state %x conn %d dead %d zapped %d sk_shutdown %d\n",
1511a246b010SChuck Lever 			sk->sk_state, xprt_connected(xprt),
1512a246b010SChuck Lever 			sock_flag(sk, SOCK_DEAD),
1513669502ffSAndy Chittenden 			sock_flag(sk, SOCK_ZAPPED),
1514669502ffSAndy Chittenden 			sk->sk_shutdown);
1515a246b010SChuck Lever 
15160fdea1e8STrond Myklebust 	transport = container_of(xprt, struct sock_xprt, xprt);
151740b5ea0cSTrond Myklebust 	trace_rpc_socket_state_change(xprt, sk->sk_socket);
1518a246b010SChuck Lever 	switch (sk->sk_state) {
1519a246b010SChuck Lever 	case TCP_ESTABLISHED:
1520a246b010SChuck Lever 		if (!xprt_test_and_set_connected(xprt)) {
15218b71798cSTrond Myklebust 			xprt->connect_cookie++;
15220fdea1e8STrond Myklebust 			clear_bit(XPRT_SOCK_CONNECTING, &transport->sock_state);
15230fdea1e8STrond Myklebust 			xprt_clear_connecting(xprt);
152451971139SChuck Lever 
15253968a8a5SChuck Lever 			xprt->stat.connect_count++;
15263968a8a5SChuck Lever 			xprt->stat.connect_time += (long)jiffies -
15273968a8a5SChuck Lever 						   xprt->stat.connect_start;
15284f8943f8STrond Myklebust 			xs_run_error_worker(transport, XPRT_SOCK_WAKE_PENDING);
1529a246b010SChuck Lever 		}
1530a246b010SChuck Lever 		break;
15313b948ae5STrond Myklebust 	case TCP_FIN_WAIT1:
15323b948ae5STrond Myklebust 		/* The client initiated a shutdown of the socket */
15337c1d71cfSTrond Myklebust 		xprt->connect_cookie++;
1534663b8858STrond Myklebust 		xprt->reestablish_timeout = 0;
15353b948ae5STrond Myklebust 		set_bit(XPRT_CLOSING, &xprt->state);
15364e857c58SPeter Zijlstra 		smp_mb__before_atomic();
15373b948ae5STrond Myklebust 		clear_bit(XPRT_CONNECTED, &xprt->state);
1538ef803670STrond Myklebust 		clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
15394e857c58SPeter Zijlstra 		smp_mb__after_atomic();
1540a246b010SChuck Lever 		break;
1541632e3bdcSTrond Myklebust 	case TCP_CLOSE_WAIT:
15423b948ae5STrond Myklebust 		/* The server initiated a shutdown of the socket */
15437c1d71cfSTrond Myklebust 		xprt->connect_cookie++;
1544d0bea455STrond Myklebust 		clear_bit(XPRT_CONNECTED, &xprt->state);
15454f8943f8STrond Myklebust 		xs_run_error_worker(transport, XPRT_SOCK_WAKE_DISCONNECT);
1546df561f66SGustavo A. R. Silva 		fallthrough;
1547663b8858STrond Myklebust 	case TCP_CLOSING:
1548663b8858STrond Myklebust 		/*
1549663b8858STrond Myklebust 		 * If the server closed down the connection, make sure that
1550663b8858STrond Myklebust 		 * we back off before reconnecting
1551663b8858STrond Myklebust 		 */
1552663b8858STrond Myklebust 		if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
1553663b8858STrond Myklebust 			xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
15543b948ae5STrond Myklebust 		break;
15553b948ae5STrond Myklebust 	case TCP_LAST_ACK:
1556670f9457STrond Myklebust 		set_bit(XPRT_CLOSING, &xprt->state);
15574e857c58SPeter Zijlstra 		smp_mb__before_atomic();
15583b948ae5STrond Myklebust 		clear_bit(XPRT_CONNECTED, &xprt->state);
15594e857c58SPeter Zijlstra 		smp_mb__after_atomic();
15603b948ae5STrond Myklebust 		break;
15613b948ae5STrond Myklebust 	case TCP_CLOSE:
15620fdea1e8STrond Myklebust 		if (test_and_clear_bit(XPRT_SOCK_CONNECTING,
15630fdea1e8STrond Myklebust 					&transport->sock_state))
15640fdea1e8STrond Myklebust 			xprt_clear_connecting(xprt);
15659b30889cSTrond Myklebust 		clear_bit(XPRT_CLOSING, &xprt->state);
15669b30889cSTrond Myklebust 		/* Trigger the socket release */
15674f8943f8STrond Myklebust 		xs_run_error_worker(transport, XPRT_SOCK_WAKE_DISCONNECT);
1568a246b010SChuck Lever 	}
1569a246b010SChuck Lever }
1570a246b010SChuck Lever 
15711f0fa154SIlpo Järvinen static void xs_write_space(struct sock *sk)
15721f0fa154SIlpo Järvinen {
15734f8943f8STrond Myklebust 	struct sock_xprt *transport;
15741f0fa154SIlpo Järvinen 	struct rpc_xprt *xprt;
15751f0fa154SIlpo Järvinen 
157613331a55STrond Myklebust 	if (!sk->sk_socket)
15771f0fa154SIlpo Järvinen 		return;
157813331a55STrond Myklebust 	clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
15791f0fa154SIlpo Järvinen 
15801f0fa154SIlpo Järvinen 	if (unlikely(!(xprt = xprt_from_sock(sk))))
15811f0fa154SIlpo Järvinen 		return;
15824f8943f8STrond Myklebust 	transport = container_of(xprt, struct sock_xprt, xprt);
15832790a624STrond Myklebust 	if (!test_and_clear_bit(XPRT_SOCK_NOSPACE, &transport->sock_state))
15842790a624STrond Myklebust 		return;
15854f8943f8STrond Myklebust 	xs_run_error_worker(transport, XPRT_SOCK_WAKE_WRITE);
1586c544577dSTrond Myklebust 	sk->sk_write_pending--;
15871f0fa154SIlpo Järvinen }
15881f0fa154SIlpo Järvinen 
15892a9e1cfaSTrond Myklebust /**
1590c7b2cae8SChuck Lever  * xs_udp_write_space - callback invoked when socket buffer space
1591c7b2cae8SChuck Lever  *                             becomes available
15929903cd1cSChuck Lever  * @sk: socket whose state has changed
15939903cd1cSChuck Lever  *
1594a246b010SChuck Lever  * Called when more output buffer space is available for this socket.
1595a246b010SChuck Lever  * We try not to wake our writers until they can make "significant"
1596c7b2cae8SChuck Lever  * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1597a246b010SChuck Lever  * with a bunch of small requests.
1598a246b010SChuck Lever  */
1599c7b2cae8SChuck Lever static void xs_udp_write_space(struct sock *sk)
1600a246b010SChuck Lever {
1601c7b2cae8SChuck Lever 	/* from net/core/sock.c:sock_def_write_space */
16021f0fa154SIlpo Järvinen 	if (sock_writeable(sk))
16031f0fa154SIlpo Järvinen 		xs_write_space(sk);
1604c7b2cae8SChuck Lever }
1605c7b2cae8SChuck Lever 
1606c7b2cae8SChuck Lever /**
1607c7b2cae8SChuck Lever  * xs_tcp_write_space - callback invoked when socket buffer space
1608c7b2cae8SChuck Lever  *                             becomes available
1609c7b2cae8SChuck Lever  * @sk: socket whose state has changed
1610c7b2cae8SChuck Lever  *
1611c7b2cae8SChuck Lever  * Called when more output buffer space is available for this socket.
1612c7b2cae8SChuck Lever  * We try not to wake our writers until they can make "significant"
1613c7b2cae8SChuck Lever  * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1614c7b2cae8SChuck Lever  * with a bunch of small requests.
1615c7b2cae8SChuck Lever  */
1616c7b2cae8SChuck Lever static void xs_tcp_write_space(struct sock *sk)
1617c7b2cae8SChuck Lever {
1618c7b2cae8SChuck Lever 	/* from net/core/stream.c:sk_stream_write_space */
161964dc6130SEric Dumazet 	if (sk_stream_is_writeable(sk))
16201f0fa154SIlpo Järvinen 		xs_write_space(sk);
1621a246b010SChuck Lever }
1622a246b010SChuck Lever 
1623470056c2SChuck Lever static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt)
1624a246b010SChuck Lever {
1625ee0ac0c2SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1626ee0ac0c2SChuck Lever 	struct sock *sk = transport->inet;
1627a246b010SChuck Lever 
16287c6e066eSChuck Lever 	if (transport->rcvsize) {
1629a246b010SChuck Lever 		sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
16307c6e066eSChuck Lever 		sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2;
1631a246b010SChuck Lever 	}
16327c6e066eSChuck Lever 	if (transport->sndsize) {
1633a246b010SChuck Lever 		sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
16347c6e066eSChuck Lever 		sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2;
1635a246b010SChuck Lever 		sk->sk_write_space(sk);
1636a246b010SChuck Lever 	}
1637a246b010SChuck Lever }
1638a246b010SChuck Lever 
163943118c29SChuck Lever /**
1640470056c2SChuck Lever  * xs_udp_set_buffer_size - set send and receive limits
164143118c29SChuck Lever  * @xprt: generic transport
1642470056c2SChuck Lever  * @sndsize: requested size of send buffer, in bytes
1643470056c2SChuck Lever  * @rcvsize: requested size of receive buffer, in bytes
164443118c29SChuck Lever  *
1645470056c2SChuck Lever  * Set socket send and receive buffer size limits.
164643118c29SChuck Lever  */
1647470056c2SChuck Lever static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize)
164843118c29SChuck Lever {
16497c6e066eSChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
16507c6e066eSChuck Lever 
16517c6e066eSChuck Lever 	transport->sndsize = 0;
1652470056c2SChuck Lever 	if (sndsize)
16537c6e066eSChuck Lever 		transport->sndsize = sndsize + 1024;
16547c6e066eSChuck Lever 	transport->rcvsize = 0;
1655470056c2SChuck Lever 	if (rcvsize)
16567c6e066eSChuck Lever 		transport->rcvsize = rcvsize + 1024;
1657470056c2SChuck Lever 
1658470056c2SChuck Lever 	xs_udp_do_set_buffer_size(xprt);
165943118c29SChuck Lever }
166043118c29SChuck Lever 
166146c0ee8bSChuck Lever /**
166246c0ee8bSChuck Lever  * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
1663acf0a39fSChuck Lever  * @xprt: controlling transport
166446c0ee8bSChuck Lever  * @task: task that timed out
166546c0ee8bSChuck Lever  *
166646c0ee8bSChuck Lever  * Adjust the congestion window after a retransmit timeout has occurred.
166746c0ee8bSChuck Lever  */
16686a24dfb6STrond Myklebust static void xs_udp_timer(struct rpc_xprt *xprt, struct rpc_task *task)
166946c0ee8bSChuck Lever {
1670b5e92419STrond Myklebust 	spin_lock(&xprt->transport_lock);
16716a24dfb6STrond Myklebust 	xprt_adjust_cwnd(xprt, task, -ETIMEDOUT);
1672b5e92419STrond Myklebust 	spin_unlock(&xprt->transport_lock);
167346c0ee8bSChuck Lever }
167446c0ee8bSChuck Lever 
1675826799e6SJ. Bruce Fields static int xs_get_random_port(void)
1676b85d8806SChuck Lever {
1677826799e6SJ. Bruce Fields 	unsigned short min = xprt_min_resvport, max = xprt_max_resvport;
1678826799e6SJ. Bruce Fields 	unsigned short range;
1679826799e6SJ. Bruce Fields 	unsigned short rand;
1680826799e6SJ. Bruce Fields 
1681826799e6SJ. Bruce Fields 	if (max < min)
1682826799e6SJ. Bruce Fields 		return -EADDRINUSE;
1683826799e6SJ. Bruce Fields 	range = max - min + 1;
16848032bf12SJason A. Donenfeld 	rand = get_random_u32_below(range);
1685826799e6SJ. Bruce Fields 	return rand + min;
1686b85d8806SChuck Lever }
1687b85d8806SChuck Lever 
16884dda9c8aSTrond Myklebust static unsigned short xs_sock_getport(struct socket *sock)
16894dda9c8aSTrond Myklebust {
16904dda9c8aSTrond Myklebust 	struct sockaddr_storage buf;
16914dda9c8aSTrond Myklebust 	unsigned short port = 0;
16924dda9c8aSTrond Myklebust 
16939b2c45d4SDenys Vlasenko 	if (kernel_getsockname(sock, (struct sockaddr *)&buf) < 0)
16944dda9c8aSTrond Myklebust 		goto out;
16954dda9c8aSTrond Myklebust 	switch (buf.ss_family) {
16964dda9c8aSTrond Myklebust 	case AF_INET6:
16974dda9c8aSTrond Myklebust 		port = ntohs(((struct sockaddr_in6 *)&buf)->sin6_port);
16984dda9c8aSTrond Myklebust 		break;
16994dda9c8aSTrond Myklebust 	case AF_INET:
17004dda9c8aSTrond Myklebust 		port = ntohs(((struct sockaddr_in *)&buf)->sin_port);
17014dda9c8aSTrond Myklebust 	}
17024dda9c8aSTrond Myklebust out:
17034dda9c8aSTrond Myklebust 	return port;
17044dda9c8aSTrond Myklebust }
17054dda9c8aSTrond Myklebust 
17064dda9c8aSTrond Myklebust /**
170792200412SChuck Lever  * xs_set_port - reset the port number in the remote endpoint address
170892200412SChuck Lever  * @xprt: generic transport
170992200412SChuck Lever  * @port: new port number
171092200412SChuck Lever  *
171192200412SChuck Lever  */
171292200412SChuck Lever static void xs_set_port(struct rpc_xprt *xprt, unsigned short port)
171392200412SChuck Lever {
171492200412SChuck Lever 	dprintk("RPC:       setting port for xprt %p to %u\n", xprt, port);
1715c4efcb1dSChuck Lever 
17169dc3b095SChuck Lever 	rpc_set_port(xs_addr(xprt), port);
17179dc3b095SChuck Lever 	xs_update_peer_port(xprt);
171892200412SChuck Lever }
171992200412SChuck Lever 
17204dda9c8aSTrond Myklebust static void xs_set_srcport(struct sock_xprt *transport, struct socket *sock)
17214dda9c8aSTrond Myklebust {
1722e6237b6fSTrond Myklebust 	if (transport->srcport == 0 && transport->xprt.reuseport)
17234dda9c8aSTrond Myklebust 		transport->srcport = xs_sock_getport(sock);
17244dda9c8aSTrond Myklebust }
17254dda9c8aSTrond Myklebust 
1726826799e6SJ. Bruce Fields static int xs_get_srcport(struct sock_xprt *transport)
172767a391d7STrond Myklebust {
1728826799e6SJ. Bruce Fields 	int port = transport->srcport;
172967a391d7STrond Myklebust 
173067a391d7STrond Myklebust 	if (port == 0 && transport->xprt.resvport)
173167a391d7STrond Myklebust 		port = xs_get_random_port();
173267a391d7STrond Myklebust 	return port;
173367a391d7STrond Myklebust }
173467a391d7STrond Myklebust 
1735421ab1beSTrond Myklebust static unsigned short xs_sock_srcport(struct rpc_xprt *xprt)
1736a8482488SOlga Kornievskaia {
1737a8482488SOlga Kornievskaia 	struct sock_xprt *sock = container_of(xprt, struct sock_xprt, xprt);
1738b49ea673SNeilBrown 	unsigned short ret = 0;
1739b49ea673SNeilBrown 	mutex_lock(&sock->recv_mutex);
1740b49ea673SNeilBrown 	if (sock->sock)
1741b49ea673SNeilBrown 		ret = xs_sock_getport(sock->sock);
1742b49ea673SNeilBrown 	mutex_unlock(&sock->recv_mutex);
1743b49ea673SNeilBrown 	return ret;
1744a8482488SOlga Kornievskaia }
1745421ab1beSTrond Myklebust 
1746421ab1beSTrond Myklebust static int xs_sock_srcaddr(struct rpc_xprt *xprt, char *buf, size_t buflen)
1747421ab1beSTrond Myklebust {
1748421ab1beSTrond Myklebust 	struct sock_xprt *sock = container_of(xprt, struct sock_xprt, xprt);
1749421ab1beSTrond Myklebust 	union {
1750421ab1beSTrond Myklebust 		struct sockaddr sa;
1751421ab1beSTrond Myklebust 		struct sockaddr_storage st;
1752421ab1beSTrond Myklebust 	} saddr;
1753421ab1beSTrond Myklebust 	int ret = -ENOTCONN;
1754421ab1beSTrond Myklebust 
1755421ab1beSTrond Myklebust 	mutex_lock(&sock->recv_mutex);
1756421ab1beSTrond Myklebust 	if (sock->sock) {
1757421ab1beSTrond Myklebust 		ret = kernel_getsockname(sock->sock, &saddr.sa);
1758421ab1beSTrond Myklebust 		if (ret >= 0)
1759421ab1beSTrond Myklebust 			ret = snprintf(buf, buflen, "%pISc", &saddr.sa);
1760421ab1beSTrond Myklebust 	}
1761421ab1beSTrond Myklebust 	mutex_unlock(&sock->recv_mutex);
1762421ab1beSTrond Myklebust 	return ret;
1763421ab1beSTrond Myklebust }
1764a8482488SOlga Kornievskaia 
1765baaf4e48SPavel Emelyanov static unsigned short xs_next_srcport(struct sock_xprt *transport, unsigned short port)
176667a391d7STrond Myklebust {
1767fbfffbd5SChuck Lever 	if (transport->srcport != 0)
1768fbfffbd5SChuck Lever 		transport->srcport = 0;
176967a391d7STrond Myklebust 	if (!transport->xprt.resvport)
177067a391d7STrond Myklebust 		return 0;
177167a391d7STrond Myklebust 	if (port <= xprt_min_resvport || port > xprt_max_resvport)
177267a391d7STrond Myklebust 		return xprt_max_resvport;
177367a391d7STrond Myklebust 	return --port;
177467a391d7STrond Myklebust }
1775beb59b68SPavel Emelyanov static int xs_bind(struct sock_xprt *transport, struct socket *sock)
1776a246b010SChuck Lever {
1777beb59b68SPavel Emelyanov 	struct sockaddr_storage myaddr;
177867a391d7STrond Myklebust 	int err, nloop = 0;
1779826799e6SJ. Bruce Fields 	int port = xs_get_srcport(transport);
178067a391d7STrond Myklebust 	unsigned short last;
1781a246b010SChuck Lever 
17820f7a622cSChris Perl 	/*
17830f7a622cSChris Perl 	 * If we are asking for any ephemeral port (i.e. port == 0 &&
17840f7a622cSChris Perl 	 * transport->xprt.resvport == 0), don't bind.  Let the local
17850f7a622cSChris Perl 	 * port selection happen implicitly when the socket is used
17860f7a622cSChris Perl 	 * (for example at connect time).
17870f7a622cSChris Perl 	 *
17880f7a622cSChris Perl 	 * This ensures that we can continue to establish TCP
17890f7a622cSChris Perl 	 * connections even when all local ephemeral ports are already
17900f7a622cSChris Perl 	 * a part of some TCP connection.  This makes no difference
179112b20ce3SBhaskar Chowdhury 	 * for UDP sockets, but also doesn't harm them.
17920f7a622cSChris Perl 	 *
17930f7a622cSChris Perl 	 * If we're asking for any reserved port (i.e. port == 0 &&
17940f7a622cSChris Perl 	 * transport->xprt.resvport == 1) xs_get_srcport above will
17950f7a622cSChris Perl 	 * ensure that port is non-zero and we will bind as needed.
17960f7a622cSChris Perl 	 */
1797826799e6SJ. Bruce Fields 	if (port <= 0)
1798826799e6SJ. Bruce Fields 		return port;
17990f7a622cSChris Perl 
1800beb59b68SPavel Emelyanov 	memcpy(&myaddr, &transport->srcaddr, transport->xprt.addrlen);
1801a246b010SChuck Lever 	do {
1802beb59b68SPavel Emelyanov 		rpc_set_port((struct sockaddr *)&myaddr, port);
1803e6242e92SSridhar Samudrala 		err = kernel_bind(sock, (struct sockaddr *)&myaddr,
1804beb59b68SPavel Emelyanov 				transport->xprt.addrlen);
1805a246b010SChuck Lever 		if (err == 0) {
1806bc1c56e9SNeilBrown 			if (transport->xprt.reuseport)
1807fbfffbd5SChuck Lever 				transport->srcport = port;
1808d3bc9a1dSFrank van Maarseveen 			break;
1809a246b010SChuck Lever 		}
181067a391d7STrond Myklebust 		last = port;
1811baaf4e48SPavel Emelyanov 		port = xs_next_srcport(transport, port);
181267a391d7STrond Myklebust 		if (port > last)
181367a391d7STrond Myklebust 			nloop++;
181467a391d7STrond Myklebust 	} while (err == -EADDRINUSE && nloop != 2);
1815beb59b68SPavel Emelyanov 
18164232e863SChuck Lever 	if (myaddr.ss_family == AF_INET)
1817beb59b68SPavel Emelyanov 		dprintk("RPC:       %s %pI4:%u: %s (%d)\n", __func__,
1818beb59b68SPavel Emelyanov 				&((struct sockaddr_in *)&myaddr)->sin_addr,
1819beb59b68SPavel Emelyanov 				port, err ? "failed" : "ok", err);
1820beb59b68SPavel Emelyanov 	else
1821beb59b68SPavel Emelyanov 		dprintk("RPC:       %s %pI6:%u: %s (%d)\n", __func__,
1822beb59b68SPavel Emelyanov 				&((struct sockaddr_in6 *)&myaddr)->sin6_addr,
18237dc753f0SChuck Lever 				port, err ? "failed" : "ok", err);
1824a246b010SChuck Lever 	return err;
1825a246b010SChuck Lever }
1826a246b010SChuck Lever 
1827176e21eeSChuck Lever /*
1828176e21eeSChuck Lever  * We don't support autobind on AF_LOCAL sockets
1829176e21eeSChuck Lever  */
1830176e21eeSChuck Lever static void xs_local_rpcbind(struct rpc_task *task)
1831176e21eeSChuck Lever {
1832fb43d172STrond Myklebust 	xprt_set_bound(task->tk_xprt);
1833176e21eeSChuck Lever }
1834176e21eeSChuck Lever 
1835176e21eeSChuck Lever static void xs_local_set_port(struct rpc_xprt *xprt, unsigned short port)
1836176e21eeSChuck Lever {
1837176e21eeSChuck Lever }
1838a246b010SChuck Lever 
1839ed07536eSPeter Zijlstra #ifdef CONFIG_DEBUG_LOCK_ALLOC
1840064a9177SNeilBrown static struct lock_class_key xs_key[3];
1841064a9177SNeilBrown static struct lock_class_key xs_slock_key[3];
1842ed07536eSPeter Zijlstra 
1843176e21eeSChuck Lever static inline void xs_reclassify_socketu(struct socket *sock)
1844176e21eeSChuck Lever {
1845176e21eeSChuck Lever 	struct sock *sk = sock->sk;
1846176e21eeSChuck Lever 
1847176e21eeSChuck Lever 	sock_lock_init_class_and_name(sk, "slock-AF_LOCAL-RPC",
1848064a9177SNeilBrown 		&xs_slock_key[0], "sk_lock-AF_LOCAL-RPC", &xs_key[0]);
1849176e21eeSChuck Lever }
1850176e21eeSChuck Lever 
18518945ee5eSChuck Lever static inline void xs_reclassify_socket4(struct socket *sock)
1852ed07536eSPeter Zijlstra {
1853ed07536eSPeter Zijlstra 	struct sock *sk = sock->sk;
18548945ee5eSChuck Lever 
18558945ee5eSChuck Lever 	sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC",
1856064a9177SNeilBrown 		&xs_slock_key[1], "sk_lock-AF_INET-RPC", &xs_key[1]);
1857ed07536eSPeter Zijlstra }
18588945ee5eSChuck Lever 
18598945ee5eSChuck Lever static inline void xs_reclassify_socket6(struct socket *sock)
18608945ee5eSChuck Lever {
18618945ee5eSChuck Lever 	struct sock *sk = sock->sk;
18628945ee5eSChuck Lever 
18638945ee5eSChuck Lever 	sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC",
1864064a9177SNeilBrown 		&xs_slock_key[2], "sk_lock-AF_INET6-RPC", &xs_key[2]);
1865ed07536eSPeter Zijlstra }
18666bc9638aSPavel Emelyanov 
18676bc9638aSPavel Emelyanov static inline void xs_reclassify_socket(int family, struct socket *sock)
18686bc9638aSPavel Emelyanov {
1869fafc4e1eSHannes Frederic Sowa 	if (WARN_ON_ONCE(!sock_allow_reclassification(sock->sk)))
18701b7a1819SWeston Andros Adamson 		return;
18711b7a1819SWeston Andros Adamson 
18724232e863SChuck Lever 	switch (family) {
1873176e21eeSChuck Lever 	case AF_LOCAL:
1874176e21eeSChuck Lever 		xs_reclassify_socketu(sock);
1875176e21eeSChuck Lever 		break;
18764232e863SChuck Lever 	case AF_INET:
18776bc9638aSPavel Emelyanov 		xs_reclassify_socket4(sock);
18784232e863SChuck Lever 		break;
18794232e863SChuck Lever 	case AF_INET6:
18806bc9638aSPavel Emelyanov 		xs_reclassify_socket6(sock);
18814232e863SChuck Lever 		break;
18824232e863SChuck Lever 	}
18836bc9638aSPavel Emelyanov }
1884ed07536eSPeter Zijlstra #else
18856bc9638aSPavel Emelyanov static inline void xs_reclassify_socket(int family, struct socket *sock)
18866bc9638aSPavel Emelyanov {
18876bc9638aSPavel Emelyanov }
1888ed07536eSPeter Zijlstra #endif
1889ed07536eSPeter Zijlstra 
189093dc41bdSNeilBrown static void xs_dummy_setup_socket(struct work_struct *work)
189193dc41bdSNeilBrown {
189293dc41bdSNeilBrown }
189393dc41bdSNeilBrown 
18946bc9638aSPavel Emelyanov static struct socket *xs_create_sock(struct rpc_xprt *xprt,
18954dda9c8aSTrond Myklebust 		struct sock_xprt *transport, int family, int type,
18964dda9c8aSTrond Myklebust 		int protocol, bool reuseport)
189722f79326SPavel Emelyanov {
1898a73881c9STrond Myklebust 	struct file *filp;
189922f79326SPavel Emelyanov 	struct socket *sock;
190022f79326SPavel Emelyanov 	int err;
190122f79326SPavel Emelyanov 
19026bc9638aSPavel Emelyanov 	err = __sock_create(xprt->xprt_net, family, type, protocol, &sock, 1);
190322f79326SPavel Emelyanov 	if (err < 0) {
190422f79326SPavel Emelyanov 		dprintk("RPC:       can't create %d transport socket (%d).\n",
190522f79326SPavel Emelyanov 				protocol, -err);
190622f79326SPavel Emelyanov 		goto out;
190722f79326SPavel Emelyanov 	}
19086bc9638aSPavel Emelyanov 	xs_reclassify_socket(family, sock);
190922f79326SPavel Emelyanov 
19104dda9c8aSTrond Myklebust 	if (reuseport)
1911fe31a326SChristoph Hellwig 		sock_set_reuseport(sock->sk);
19124dda9c8aSTrond Myklebust 
19134cea288aSBen Hutchings 	err = xs_bind(transport, sock);
19144cea288aSBen Hutchings 	if (err) {
191522f79326SPavel Emelyanov 		sock_release(sock);
191622f79326SPavel Emelyanov 		goto out;
191722f79326SPavel Emelyanov 	}
191822f79326SPavel Emelyanov 
1919a73881c9STrond Myklebust 	filp = sock_alloc_file(sock, O_NONBLOCK, NULL);
1920a73881c9STrond Myklebust 	if (IS_ERR(filp))
1921a73881c9STrond Myklebust 		return ERR_CAST(filp);
1922a73881c9STrond Myklebust 	transport->file = filp;
1923a73881c9STrond Myklebust 
192422f79326SPavel Emelyanov 	return sock;
192522f79326SPavel Emelyanov out:
192622f79326SPavel Emelyanov 	return ERR_PTR(err);
192722f79326SPavel Emelyanov }
192822f79326SPavel Emelyanov 
1929176e21eeSChuck Lever static int xs_local_finish_connecting(struct rpc_xprt *xprt,
1930176e21eeSChuck Lever 				      struct socket *sock)
1931176e21eeSChuck Lever {
1932176e21eeSChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
1933176e21eeSChuck Lever 									xprt);
1934176e21eeSChuck Lever 
1935176e21eeSChuck Lever 	if (!transport->inet) {
1936176e21eeSChuck Lever 		struct sock *sk = sock->sk;
1937176e21eeSChuck Lever 
1938ea9afca8STrond Myklebust 		lock_sock(sk);
1939176e21eeSChuck Lever 
1940176e21eeSChuck Lever 		xs_save_old_callbacks(transport, sk);
1941176e21eeSChuck Lever 
1942176e21eeSChuck Lever 		sk->sk_user_data = xprt;
1943a2648094STrond Myklebust 		sk->sk_data_ready = xs_data_ready;
1944176e21eeSChuck Lever 		sk->sk_write_space = xs_udp_write_space;
1945efce2d0bSTrond Myklebust 		sk->sk_state_change = xs_local_state_change;
19462118071dSTrond Myklebust 		sk->sk_error_report = xs_error_report;
194798123866SBenjamin Coddington 		sk->sk_use_task_frag = false;
1948176e21eeSChuck Lever 
1949176e21eeSChuck Lever 		xprt_clear_connected(xprt);
1950176e21eeSChuck Lever 
1951176e21eeSChuck Lever 		/* Reset to new socket */
1952176e21eeSChuck Lever 		transport->sock = sock;
1953176e21eeSChuck Lever 		transport->inet = sk;
1954176e21eeSChuck Lever 
1955ea9afca8STrond Myklebust 		release_sock(sk);
1956176e21eeSChuck Lever 	}
1957176e21eeSChuck Lever 
1958ae053551STrond Myklebust 	xs_stream_start_connect(transport);
19596c7a64e5STrond Myklebust 
1960176e21eeSChuck Lever 	return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, 0);
1961176e21eeSChuck Lever }
1962176e21eeSChuck Lever 
1963176e21eeSChuck Lever /**
1964176e21eeSChuck Lever  * xs_local_setup_socket - create AF_LOCAL socket, connect to a local endpoint
1965176e21eeSChuck Lever  * @transport: socket transport to connect
1966176e21eeSChuck Lever  */
1967dc107402SJ. Bruce Fields static int xs_local_setup_socket(struct sock_xprt *transport)
1968176e21eeSChuck Lever {
1969176e21eeSChuck Lever 	struct rpc_xprt *xprt = &transport->xprt;
1970a73881c9STrond Myklebust 	struct file *filp;
1971176e21eeSChuck Lever 	struct socket *sock;
197268e9a246SColin Ian King 	int status;
1973176e21eeSChuck Lever 
1974176e21eeSChuck Lever 	status = __sock_create(xprt->xprt_net, AF_LOCAL,
1975176e21eeSChuck Lever 					SOCK_STREAM, 0, &sock, 1);
1976176e21eeSChuck Lever 	if (status < 0) {
1977176e21eeSChuck Lever 		dprintk("RPC:       can't create AF_LOCAL "
1978176e21eeSChuck Lever 			"transport socket (%d).\n", -status);
1979176e21eeSChuck Lever 		goto out;
1980176e21eeSChuck Lever 	}
1981d1358917SStefan Hajnoczi 	xs_reclassify_socket(AF_LOCAL, sock);
1982176e21eeSChuck Lever 
1983a73881c9STrond Myklebust 	filp = sock_alloc_file(sock, O_NONBLOCK, NULL);
1984a73881c9STrond Myklebust 	if (IS_ERR(filp)) {
1985a73881c9STrond Myklebust 		status = PTR_ERR(filp);
1986a73881c9STrond Myklebust 		goto out;
1987a73881c9STrond Myklebust 	}
1988a73881c9STrond Myklebust 	transport->file = filp;
1989a73881c9STrond Myklebust 
1990176e21eeSChuck Lever 	dprintk("RPC:       worker connecting xprt %p via AF_LOCAL to %s\n",
1991176e21eeSChuck Lever 			xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1992176e21eeSChuck Lever 
1993176e21eeSChuck Lever 	status = xs_local_finish_connecting(xprt, sock);
199440b5ea0cSTrond Myklebust 	trace_rpc_socket_connect(xprt, sock, status);
1995176e21eeSChuck Lever 	switch (status) {
1996176e21eeSChuck Lever 	case 0:
1997176e21eeSChuck Lever 		dprintk("RPC:       xprt %p connected to %s\n",
1998176e21eeSChuck Lever 				xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
19993968a8a5SChuck Lever 		xprt->stat.connect_count++;
20003968a8a5SChuck Lever 		xprt->stat.connect_time += (long)jiffies -
20013968a8a5SChuck Lever 					   xprt->stat.connect_start;
2002176e21eeSChuck Lever 		xprt_set_connected(xprt);
200393f479d3SGustavo A. R. Silva 		break;
20043601c4a9STrond Myklebust 	case -ENOBUFS:
2005176e21eeSChuck Lever 		break;
2006176e21eeSChuck Lever 	case -ENOENT:
2007176e21eeSChuck Lever 		dprintk("RPC:       xprt %p: socket %s does not exist\n",
2008176e21eeSChuck Lever 				xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
2009176e21eeSChuck Lever 		break;
20104a20a988STrond Myklebust 	case -ECONNREFUSED:
20114a20a988STrond Myklebust 		dprintk("RPC:       xprt %p: connection refused for %s\n",
20124a20a988STrond Myklebust 				xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
20134a20a988STrond Myklebust 		break;
2014176e21eeSChuck Lever 	default:
2015176e21eeSChuck Lever 		printk(KERN_ERR "%s: unhandled error (%d) connecting to %s\n",
2016176e21eeSChuck Lever 				__func__, -status,
2017176e21eeSChuck Lever 				xprt->address_strings[RPC_DISPLAY_ADDR]);
2018176e21eeSChuck Lever 	}
2019176e21eeSChuck Lever 
2020176e21eeSChuck Lever out:
2021176e21eeSChuck Lever 	xprt_clear_connecting(xprt);
2022176e21eeSChuck Lever 	xprt_wake_pending_tasks(xprt, status);
2023dc107402SJ. Bruce Fields 	return status;
2024dc107402SJ. Bruce Fields }
2025dc107402SJ. Bruce Fields 
2026b6669737SLinus Torvalds static void xs_local_connect(struct rpc_xprt *xprt, struct rpc_task *task)
2027dc107402SJ. Bruce Fields {
2028dc107402SJ. Bruce Fields 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2029dc107402SJ. Bruce Fields 	int ret;
2030dc107402SJ. Bruce Fields 
2031aad41a7dSTrond Myklebust 	if (transport->file)
2032aad41a7dSTrond Myklebust 		goto force_disconnect;
2033aad41a7dSTrond Myklebust 
2034dc107402SJ. Bruce Fields 	if (RPC_IS_ASYNC(task)) {
2035dc107402SJ. Bruce Fields 		/*
2036dc107402SJ. Bruce Fields 		 * We want the AF_LOCAL connect to be resolved in the
2037dc107402SJ. Bruce Fields 		 * filesystem namespace of the process making the rpc
2038dc107402SJ. Bruce Fields 		 * call.  Thus we connect synchronously.
2039dc107402SJ. Bruce Fields 		 *
2040dc107402SJ. Bruce Fields 		 * If we want to support asynchronous AF_LOCAL calls,
2041dc107402SJ. Bruce Fields 		 * we'll need to figure out how to pass a namespace to
2042dc107402SJ. Bruce Fields 		 * connect.
2043dc107402SJ. Bruce Fields 		 */
204439494194STrond Myklebust 		rpc_task_set_rpc_status(task, -ENOTCONN);
2045aad41a7dSTrond Myklebust 		goto out_wake;
2046dc107402SJ. Bruce Fields 	}
2047dc107402SJ. Bruce Fields 	ret = xs_local_setup_socket(transport);
2048dc107402SJ. Bruce Fields 	if (ret && !RPC_IS_SOFTCONN(task))
2049dc107402SJ. Bruce Fields 		msleep_interruptible(15000);
2050aad41a7dSTrond Myklebust 	return;
2051aad41a7dSTrond Myklebust force_disconnect:
2052aad41a7dSTrond Myklebust 	xprt_force_disconnect(xprt);
2053aad41a7dSTrond Myklebust out_wake:
2054aad41a7dSTrond Myklebust 	xprt_clear_connecting(xprt);
2055aad41a7dSTrond Myklebust 	xprt_wake_pending_tasks(xprt, -ENOTCONN);
2056176e21eeSChuck Lever }
2057176e21eeSChuck Lever 
20583c87ef6eSJeff Layton #if IS_ENABLED(CONFIG_SUNRPC_SWAP)
2059d6e971d8SJeff Layton /*
2060693486d5SNeilBrown  * Note that this should be called with XPRT_LOCKED held, or recv_mutex
2061693486d5SNeilBrown  * held, or when we otherwise know that we have exclusive access to the
2062693486d5SNeilBrown  * socket, to guard against races with xs_reset_transport.
2063d6e971d8SJeff Layton  */
2064a564b8f0SMel Gorman static void xs_set_memalloc(struct rpc_xprt *xprt)
2065a564b8f0SMel Gorman {
2066a564b8f0SMel Gorman 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
2067a564b8f0SMel Gorman 			xprt);
2068a564b8f0SMel Gorman 
2069d6e971d8SJeff Layton 	/*
2070d6e971d8SJeff Layton 	 * If there's no sock, then we have nothing to set. The
2071d6e971d8SJeff Layton 	 * reconnecting process will get it for us.
2072d6e971d8SJeff Layton 	 */
2073d6e971d8SJeff Layton 	if (!transport->inet)
2074d6e971d8SJeff Layton 		return;
20758e228133SJeff Layton 	if (atomic_read(&xprt->swapper))
2076a564b8f0SMel Gorman 		sk_set_memalloc(transport->inet);
2077a564b8f0SMel Gorman }
2078a564b8f0SMel Gorman 
2079a564b8f0SMel Gorman /**
2080d67fa4d8SJeff Layton  * xs_enable_swap - Tag this transport as being used for swap.
2081a564b8f0SMel Gorman  * @xprt: transport to tag
2082a564b8f0SMel Gorman  *
20838e228133SJeff Layton  * Take a reference to this transport on behalf of the rpc_clnt, and
20848e228133SJeff Layton  * optionally mark it for swapping if it wasn't already.
2085a564b8f0SMel Gorman  */
2086d67fa4d8SJeff Layton static int
2087d67fa4d8SJeff Layton xs_enable_swap(struct rpc_xprt *xprt)
2088a564b8f0SMel Gorman {
2089d6e971d8SJeff Layton 	struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt);
2090a564b8f0SMel Gorman 
2091693486d5SNeilBrown 	mutex_lock(&xs->recv_mutex);
2092693486d5SNeilBrown 	if (atomic_inc_return(&xprt->swapper) == 1 &&
2093693486d5SNeilBrown 	    xs->inet)
2094d6e971d8SJeff Layton 		sk_set_memalloc(xs->inet);
2095693486d5SNeilBrown 	mutex_unlock(&xs->recv_mutex);
20968e228133SJeff Layton 	return 0;
2097a564b8f0SMel Gorman }
2098a564b8f0SMel Gorman 
20998e228133SJeff Layton /**
2100d67fa4d8SJeff Layton  * xs_disable_swap - Untag this transport as being used for swap.
21018e228133SJeff Layton  * @xprt: transport to tag
21028e228133SJeff Layton  *
21038e228133SJeff Layton  * Drop a "swapper" reference to this xprt on behalf of the rpc_clnt. If the
21048e228133SJeff Layton  * swapper refcount goes to 0, untag the socket as a memalloc socket.
21058e228133SJeff Layton  */
2106d67fa4d8SJeff Layton static void
2107d67fa4d8SJeff Layton xs_disable_swap(struct rpc_xprt *xprt)
21088e228133SJeff Layton {
2109d6e971d8SJeff Layton 	struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt);
21108e228133SJeff Layton 
2111693486d5SNeilBrown 	mutex_lock(&xs->recv_mutex);
2112693486d5SNeilBrown 	if (atomic_dec_and_test(&xprt->swapper) &&
2113693486d5SNeilBrown 	    xs->inet)
2114d6e971d8SJeff Layton 		sk_clear_memalloc(xs->inet);
2115693486d5SNeilBrown 	mutex_unlock(&xs->recv_mutex);
2116a564b8f0SMel Gorman }
2117a564b8f0SMel Gorman #else
2118a564b8f0SMel Gorman static void xs_set_memalloc(struct rpc_xprt *xprt)
2119a564b8f0SMel Gorman {
2120a564b8f0SMel Gorman }
2121d67fa4d8SJeff Layton 
2122d67fa4d8SJeff Layton static int
2123d67fa4d8SJeff Layton xs_enable_swap(struct rpc_xprt *xprt)
2124d67fa4d8SJeff Layton {
2125d67fa4d8SJeff Layton 	return -EINVAL;
2126d67fa4d8SJeff Layton }
2127d67fa4d8SJeff Layton 
2128d67fa4d8SJeff Layton static void
2129d67fa4d8SJeff Layton xs_disable_swap(struct rpc_xprt *xprt)
2130d67fa4d8SJeff Layton {
2131d67fa4d8SJeff Layton }
2132a564b8f0SMel Gorman #endif
2133a564b8f0SMel Gorman 
213416be2d20SChuck Lever static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
2135a246b010SChuck Lever {
213616be2d20SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2137edb267a6SChuck Lever 
2138ee0ac0c2SChuck Lever 	if (!transport->inet) {
2139b0d93ad5SChuck Lever 		struct sock *sk = sock->sk;
2140b0d93ad5SChuck Lever 
2141ea9afca8STrond Myklebust 		lock_sock(sk);
2142b0d93ad5SChuck Lever 
21432a9e1cfaSTrond Myklebust 		xs_save_old_callbacks(transport, sk);
21442a9e1cfaSTrond Myklebust 
2145b0d93ad5SChuck Lever 		sk->sk_user_data = xprt;
2146f9b2ee71STrond Myklebust 		sk->sk_data_ready = xs_data_ready;
2147b0d93ad5SChuck Lever 		sk->sk_write_space = xs_udp_write_space;
214898123866SBenjamin Coddington 		sk->sk_use_task_frag = false;
2149b0d93ad5SChuck Lever 
2150b0d93ad5SChuck Lever 		xprt_set_connected(xprt);
2151b0d93ad5SChuck Lever 
2152b0d93ad5SChuck Lever 		/* Reset to new socket */
2153ee0ac0c2SChuck Lever 		transport->sock = sock;
2154ee0ac0c2SChuck Lever 		transport->inet = sk;
2155b0d93ad5SChuck Lever 
2156a564b8f0SMel Gorman 		xs_set_memalloc(xprt);
2157a564b8f0SMel Gorman 
2158ea9afca8STrond Myklebust 		release_sock(sk);
2159b0d93ad5SChuck Lever 	}
2160470056c2SChuck Lever 	xs_udp_do_set_buffer_size(xprt);
216102910177STrond Myklebust 
216202910177STrond Myklebust 	xprt->stat.connect_start = jiffies;
216316be2d20SChuck Lever }
216416be2d20SChuck Lever 
21658c14ff2aSPavel Emelyanov static void xs_udp_setup_socket(struct work_struct *work)
2166a246b010SChuck Lever {
2167a246b010SChuck Lever 	struct sock_xprt *transport =
2168a246b010SChuck Lever 		container_of(work, struct sock_xprt, connect_worker.work);
2169a246b010SChuck Lever 	struct rpc_xprt *xprt = &transport->xprt;
2170d099b8afSColin Ian King 	struct socket *sock;
2171b65c0310SPavel Emelyanov 	int status = -EIO;
21728db55a03SNeilBrown 	unsigned int pflags = current->flags;
2173a246b010SChuck Lever 
21748db55a03SNeilBrown 	if (atomic_read(&xprt->swapper))
21758db55a03SNeilBrown 		current->flags |= PF_MEMALLOC;
21768c14ff2aSPavel Emelyanov 	sock = xs_create_sock(xprt, transport,
21774dda9c8aSTrond Myklebust 			xs_addr(xprt)->sa_family, SOCK_DGRAM,
21784dda9c8aSTrond Myklebust 			IPPROTO_UDP, false);
2179b65c0310SPavel Emelyanov 	if (IS_ERR(sock))
2180a246b010SChuck Lever 		goto out;
218168e220bdSChuck Lever 
2182c740eff8SChuck Lever 	dprintk("RPC:       worker connecting xprt %p via %s to "
2183c740eff8SChuck Lever 				"%s (port %s)\n", xprt,
2184c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_PROTO],
2185c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_ADDR],
2186c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_PORT]);
218768e220bdSChuck Lever 
218868e220bdSChuck Lever 	xs_udp_finish_connecting(xprt, sock);
218940b5ea0cSTrond Myklebust 	trace_rpc_socket_connect(xprt, sock, 0);
2190a246b010SChuck Lever 	status = 0;
2191b0d93ad5SChuck Lever out:
2192b0d93ad5SChuck Lever 	xprt_clear_connecting(xprt);
2193cf76785dSTrond Myklebust 	xprt_unlock_connect(xprt, transport);
21947d1e8255STrond Myklebust 	xprt_wake_pending_tasks(xprt, status);
21958db55a03SNeilBrown 	current_restore_flags(pflags, PF_MEMALLOC);
2196b0d93ad5SChuck Lever }
2197b0d93ad5SChuck Lever 
21984876cc77STrond Myklebust /**
21994876cc77STrond Myklebust  * xs_tcp_shutdown - gracefully shut down a TCP socket
22004876cc77STrond Myklebust  * @xprt: transport
22014876cc77STrond Myklebust  *
22024876cc77STrond Myklebust  * Initiates a graceful shutdown of the TCP socket by calling the
22034876cc77STrond Myklebust  * equivalent of shutdown(SHUT_RDWR);
22044876cc77STrond Myklebust  */
22054876cc77STrond Myklebust static void xs_tcp_shutdown(struct rpc_xprt *xprt)
22064876cc77STrond Myklebust {
22074876cc77STrond Myklebust 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
22084876cc77STrond Myklebust 	struct socket *sock = transport->sock;
22099b30889cSTrond Myklebust 	int skst = transport->inet ? transport->inet->sk_state : TCP_CLOSE;
22104876cc77STrond Myklebust 
22114876cc77STrond Myklebust 	if (sock == NULL)
22124876cc77STrond Myklebust 		return;
22130a6ff58eSTrond Myklebust 	if (!xprt->reuseport) {
22140a6ff58eSTrond Myklebust 		xs_close(xprt);
22150a6ff58eSTrond Myklebust 		return;
22160a6ff58eSTrond Myklebust 	}
22179b30889cSTrond Myklebust 	switch (skst) {
22187c81e6a9STrond Myklebust 	case TCP_FIN_WAIT1:
22197c81e6a9STrond Myklebust 	case TCP_FIN_WAIT2:
2220943d045aSSiddharth Kawar 	case TCP_LAST_ACK:
22217c81e6a9STrond Myklebust 		break;
22227c81e6a9STrond Myklebust 	case TCP_ESTABLISHED:
22237c81e6a9STrond Myklebust 	case TCP_CLOSE_WAIT:
22244876cc77STrond Myklebust 		kernel_sock_shutdown(sock, SHUT_RDWR);
22254876cc77STrond Myklebust 		trace_rpc_socket_shutdown(xprt, sock);
22269b30889cSTrond Myklebust 		break;
22277c81e6a9STrond Myklebust 	default:
22284876cc77STrond Myklebust 		xs_reset_transport(transport);
22294876cc77STrond Myklebust 	}
22309b30889cSTrond Myklebust }
22314876cc77STrond Myklebust 
22328d1b8c62STrond Myklebust static void xs_tcp_set_socket_timeouts(struct rpc_xprt *xprt,
22338d1b8c62STrond Myklebust 		struct socket *sock)
2234b0d93ad5SChuck Lever {
223516be2d20SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
22367196dbb0STrond Myklebust 	unsigned int keepidle;
22377196dbb0STrond Myklebust 	unsigned int keepcnt;
2238775f06abSTrond Myklebust 	unsigned int timeo;
22397f260e85STrond Myklebust 
2240b5e92419STrond Myklebust 	spin_lock(&xprt->transport_lock);
22417196dbb0STrond Myklebust 	keepidle = DIV_ROUND_UP(xprt->timeout->to_initval, HZ);
22427196dbb0STrond Myklebust 	keepcnt = xprt->timeout->to_retries + 1;
22437196dbb0STrond Myklebust 	timeo = jiffies_to_msecs(xprt->timeout->to_initval) *
22447196dbb0STrond Myklebust 		(xprt->timeout->to_retries + 1);
22457196dbb0STrond Myklebust 	clear_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state);
2246b5e92419STrond Myklebust 	spin_unlock(&xprt->transport_lock);
22477f260e85STrond Myklebust 
22487f260e85STrond Myklebust 	/* TCP Keepalive options */
2249ce3d9544SChristoph Hellwig 	sock_set_keepalive(sock->sk);
225071c48eb8SChristoph Hellwig 	tcp_sock_set_keepidle(sock->sk, keepidle);
2251d41ecaacSChristoph Hellwig 	tcp_sock_set_keepintvl(sock->sk, keepidle);
2252480aeb96SChristoph Hellwig 	tcp_sock_set_keepcnt(sock->sk, keepcnt);
2253b0d93ad5SChuck Lever 
22548d1b8c62STrond Myklebust 	/* TCP user timeout (see RFC5482) */
2255c488aeadSChristoph Hellwig 	tcp_sock_set_user_timeout(sock->sk, timeo);
22568d1b8c62STrond Myklebust }
22578d1b8c62STrond Myklebust 
22587196dbb0STrond Myklebust static void xs_tcp_set_connect_timeout(struct rpc_xprt *xprt,
22597196dbb0STrond Myklebust 		unsigned long connect_timeout,
22607196dbb0STrond Myklebust 		unsigned long reconnect_timeout)
22617196dbb0STrond Myklebust {
22627196dbb0STrond Myklebust 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
22637196dbb0STrond Myklebust 	struct rpc_timeout to;
22647196dbb0STrond Myklebust 	unsigned long initval;
22657196dbb0STrond Myklebust 
2266b5e92419STrond Myklebust 	spin_lock(&xprt->transport_lock);
22677196dbb0STrond Myklebust 	if (reconnect_timeout < xprt->max_reconnect_timeout)
22687196dbb0STrond Myklebust 		xprt->max_reconnect_timeout = reconnect_timeout;
22697196dbb0STrond Myklebust 	if (connect_timeout < xprt->connect_timeout) {
22707196dbb0STrond Myklebust 		memcpy(&to, xprt->timeout, sizeof(to));
22717196dbb0STrond Myklebust 		initval = DIV_ROUND_UP(connect_timeout, to.to_retries + 1);
22727196dbb0STrond Myklebust 		/* Arbitrary lower limit */
22737196dbb0STrond Myklebust 		if (initval <  XS_TCP_INIT_REEST_TO << 1)
22747196dbb0STrond Myklebust 			initval = XS_TCP_INIT_REEST_TO << 1;
22757196dbb0STrond Myklebust 		to.to_initval = initval;
22767196dbb0STrond Myklebust 		to.to_maxval = initval;
22777196dbb0STrond Myklebust 		memcpy(&transport->tcp_timeout, &to,
22787196dbb0STrond Myklebust 				sizeof(transport->tcp_timeout));
22797196dbb0STrond Myklebust 		xprt->timeout = &transport->tcp_timeout;
22807196dbb0STrond Myklebust 		xprt->connect_timeout = connect_timeout;
22817196dbb0STrond Myklebust 	}
22827196dbb0STrond Myklebust 	set_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state);
2283b5e92419STrond Myklebust 	spin_unlock(&xprt->transport_lock);
22847196dbb0STrond Myklebust }
22857196dbb0STrond Myklebust 
22868d1b8c62STrond Myklebust static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
22878d1b8c62STrond Myklebust {
22888d1b8c62STrond Myklebust 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
22898d1b8c62STrond Myklebust 
22908d1b8c62STrond Myklebust 	if (!transport->inet) {
22918d1b8c62STrond Myklebust 		struct sock *sk = sock->sk;
22928d1b8c62STrond Myklebust 
2293d88e4d82SNeilBrown 		/* Avoid temporary address, they are bad for long-lived
2294d88e4d82SNeilBrown 		 * connections such as NFS mounts.
2295d88e4d82SNeilBrown 		 * RFC4941, section 3.6 suggests that:
2296d88e4d82SNeilBrown 		 *    Individual applications, which have specific
2297d88e4d82SNeilBrown 		 *    knowledge about the normal duration of connections,
2298d88e4d82SNeilBrown 		 *    MAY override this as appropriate.
2299d88e4d82SNeilBrown 		 */
230018d5ad62SChristoph Hellwig 		if (xs_addr(xprt)->sa_family == PF_INET6) {
230118d5ad62SChristoph Hellwig 			ip6_sock_set_addr_preferences(sk,
230218d5ad62SChristoph Hellwig 				IPV6_PREFER_SRC_PUBLIC);
230318d5ad62SChristoph Hellwig 		}
2304d88e4d82SNeilBrown 
23058d1b8c62STrond Myklebust 		xs_tcp_set_socket_timeouts(xprt, sock);
2306d737e5d4STrond Myklebust 		tcp_sock_set_nodelay(sk);
2307775f06abSTrond Myklebust 
2308ea9afca8STrond Myklebust 		lock_sock(sk);
2309b0d93ad5SChuck Lever 
23102a9e1cfaSTrond Myklebust 		xs_save_old_callbacks(transport, sk);
23112a9e1cfaSTrond Myklebust 
2312b0d93ad5SChuck Lever 		sk->sk_user_data = xprt;
23135157b956STrond Myklebust 		sk->sk_data_ready = xs_data_ready;
2314b0d93ad5SChuck Lever 		sk->sk_state_change = xs_tcp_state_change;
2315b0d93ad5SChuck Lever 		sk->sk_write_space = xs_tcp_write_space;
23162118071dSTrond Myklebust 		sk->sk_error_report = xs_error_report;
231798123866SBenjamin Coddington 		sk->sk_use_task_frag = false;
23183167e12cSChuck Lever 
23193167e12cSChuck Lever 		/* socket options */
23203167e12cSChuck Lever 		sock_reset_flag(sk, SOCK_LINGER);
2321b0d93ad5SChuck Lever 
2322b0d93ad5SChuck Lever 		xprt_clear_connected(xprt);
2323b0d93ad5SChuck Lever 
2324b0d93ad5SChuck Lever 		/* Reset to new socket */
2325ee0ac0c2SChuck Lever 		transport->sock = sock;
2326ee0ac0c2SChuck Lever 		transport->inet = sk;
2327b0d93ad5SChuck Lever 
2328ea9afca8STrond Myklebust 		release_sock(sk);
2329b0d93ad5SChuck Lever 	}
2330b0d93ad5SChuck Lever 
233101d37c42STrond Myklebust 	if (!xprt_bound(xprt))
2332280254b6STrond Myklebust 		return -ENOTCONN;
233301d37c42STrond Myklebust 
2334a564b8f0SMel Gorman 	xs_set_memalloc(xprt);
2335a564b8f0SMel Gorman 
2336ae053551STrond Myklebust 	xs_stream_start_connect(transport);
2337e1806c7bSTrond Myklebust 
2338b0d93ad5SChuck Lever 	/* Tell the socket layer to start connecting... */
23390fdea1e8STrond Myklebust 	set_bit(XPRT_SOCK_CONNECTING, &transport->sock_state);
2340280254b6STrond Myklebust 	return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK);
234116be2d20SChuck Lever }
234216be2d20SChuck Lever 
234316be2d20SChuck Lever /**
2344b61d59ffSTrond Myklebust  * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint
2345acf0a39fSChuck Lever  * @work: queued work item
234616be2d20SChuck Lever  *
234716be2d20SChuck Lever  * Invoked by a work queue tasklet.
234816be2d20SChuck Lever  */
2349cdd518d5SPavel Emelyanov static void xs_tcp_setup_socket(struct work_struct *work)
235016be2d20SChuck Lever {
2351cdd518d5SPavel Emelyanov 	struct sock_xprt *transport =
2352cdd518d5SPavel Emelyanov 		container_of(work, struct sock_xprt, connect_worker.work);
235316be2d20SChuck Lever 	struct socket *sock = transport->sock;
2354a9f5f0f7SPavel Emelyanov 	struct rpc_xprt *xprt = &transport->xprt;
2355280254b6STrond Myklebust 	int status;
23568db55a03SNeilBrown 	unsigned int pflags = current->flags;
235716be2d20SChuck Lever 
23588db55a03SNeilBrown 	if (atomic_read(&xprt->swapper))
23598db55a03SNeilBrown 		current->flags |= PF_MEMALLOC;
236089f42494STrond Myklebust 
236189f42494STrond Myklebust 	if (xprt_connected(xprt))
236289f42494STrond Myklebust 		goto out;
236389f42494STrond Myklebust 	if (test_and_clear_bit(XPRT_SOCK_CONNECT_SENT,
236489f42494STrond Myklebust 			       &transport->sock_state) ||
236589f42494STrond Myklebust 	    !sock) {
236689f42494STrond Myklebust 		xs_reset_transport(transport);
236789f42494STrond Myklebust 		sock = xs_create_sock(xprt, transport, xs_addr(xprt)->sa_family,
236889f42494STrond Myklebust 				      SOCK_STREAM, IPPROTO_TCP, true);
2369b61d59ffSTrond Myklebust 		if (IS_ERR(sock)) {
2370280254b6STrond Myklebust 			xprt_wake_pending_tasks(xprt, PTR_ERR(sock));
237116be2d20SChuck Lever 			goto out;
237216be2d20SChuck Lever 		}
23737d1e8255STrond Myklebust 	}
23747d1e8255STrond Myklebust 
2375c740eff8SChuck Lever 	dprintk("RPC:       worker connecting xprt %p via %s to "
2376c740eff8SChuck Lever 				"%s (port %s)\n", xprt,
2377c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_PROTO],
2378c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_ADDR],
2379c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_PORT]);
238016be2d20SChuck Lever 
238116be2d20SChuck Lever 	status = xs_tcp_finish_connecting(xprt, sock);
238240b5ea0cSTrond Myklebust 	trace_rpc_socket_connect(xprt, sock, status);
2383a246b010SChuck Lever 	dprintk("RPC:       %p connect status %d connected %d sock state %d\n",
238446121cf7SChuck Lever 			xprt, -status, xprt_connected(xprt),
238546121cf7SChuck Lever 			sock->sk->sk_state);
2386a246b010SChuck Lever 	switch (status) {
23872a491991STrond Myklebust 	case 0:
2388a246b010SChuck Lever 	case -EINPROGRESS:
2389280254b6STrond Myklebust 		/* SYN_SENT! */
239089f42494STrond Myklebust 		set_bit(XPRT_SOCK_CONNECT_SENT, &transport->sock_state);
2391280254b6STrond Myklebust 		if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
2392280254b6STrond Myklebust 			xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2393280254b6STrond Myklebust 		fallthrough;
2394a246b010SChuck Lever 	case -EALREADY:
2395280254b6STrond Myklebust 		goto out_unlock;
2396280254b6STrond Myklebust 	case -EADDRNOTAVAIL:
2397280254b6STrond Myklebust 		/* Source port number is unavailable. Try a new one! */
2398280254b6STrond Myklebust 		transport->srcport = 0;
2399280254b6STrond Myklebust 		status = -EAGAIN;
2400280254b6STrond Myklebust 		break;
24019fcfe0c8STrond Myklebust 	case -EINVAL:
24029fcfe0c8STrond Myklebust 		/* Happens, for instance, if the user specified a link
24039fcfe0c8STrond Myklebust 		 * local IPv6 address without a scope-id.
24049fcfe0c8STrond Myklebust 		 */
24053ed5e2a2STrond Myklebust 	case -ECONNREFUSED:
24063ed5e2a2STrond Myklebust 	case -ECONNRESET:
2407eb5b46faSTrond Myklebust 	case -ENETDOWN:
24083ed5e2a2STrond Myklebust 	case -ENETUNREACH:
24094ba161a7STrond Myklebust 	case -EHOSTUNREACH:
24103913c78cSTrond Myklebust 	case -EADDRINUSE:
24113601c4a9STrond Myklebust 	case -ENOBUFS:
2412280254b6STrond Myklebust 		break;
2413280254b6STrond Myklebust 	default:
2414280254b6STrond Myklebust 		printk("%s: connect returned unhandled error %d\n",
2415280254b6STrond Myklebust 			__func__, status);
2416280254b6STrond Myklebust 		status = -EAGAIN;
2417280254b6STrond Myklebust 	}
2418280254b6STrond Myklebust 
24198c71139dSCalum Mackay 	/* xs_tcp_force_close() wakes tasks with a fixed error code.
24208c71139dSCalum Mackay 	 * We need to wake them first to ensure the correct error code.
24216ea44adcSNeilBrown 	 */
24226ea44adcSNeilBrown 	xprt_wake_pending_tasks(xprt, status);
24234efdd92cSTrond Myklebust 	xs_tcp_force_close(xprt);
2424a246b010SChuck Lever out:
24252226feb6SChuck Lever 	xprt_clear_connecting(xprt);
2426280254b6STrond Myklebust out_unlock:
2427cf76785dSTrond Myklebust 	xprt_unlock_connect(xprt, transport);
24288db55a03SNeilBrown 	current_restore_flags(pflags, PF_MEMALLOC);
2429a246b010SChuck Lever }
2430a246b010SChuck Lever 
243175eb6af7SChuck Lever /*
243275eb6af7SChuck Lever  * Transfer the connected socket to @upper_transport, then mark that
243375eb6af7SChuck Lever  * xprt CONNECTED.
243475eb6af7SChuck Lever  */
243575eb6af7SChuck Lever static int xs_tcp_tls_finish_connecting(struct rpc_xprt *lower_xprt,
243675eb6af7SChuck Lever 					struct sock_xprt *upper_transport)
243775eb6af7SChuck Lever {
243875eb6af7SChuck Lever 	struct sock_xprt *lower_transport =
243975eb6af7SChuck Lever 			container_of(lower_xprt, struct sock_xprt, xprt);
244075eb6af7SChuck Lever 	struct rpc_xprt *upper_xprt = &upper_transport->xprt;
244175eb6af7SChuck Lever 
244275eb6af7SChuck Lever 	if (!upper_transport->inet) {
244375eb6af7SChuck Lever 		struct socket *sock = lower_transport->sock;
244475eb6af7SChuck Lever 		struct sock *sk = sock->sk;
244575eb6af7SChuck Lever 
244675eb6af7SChuck Lever 		/* Avoid temporary address, they are bad for long-lived
244775eb6af7SChuck Lever 		 * connections such as NFS mounts.
244875eb6af7SChuck Lever 		 * RFC4941, section 3.6 suggests that:
244975eb6af7SChuck Lever 		 *    Individual applications, which have specific
245075eb6af7SChuck Lever 		 *    knowledge about the normal duration of connections,
245175eb6af7SChuck Lever 		 *    MAY override this as appropriate.
245275eb6af7SChuck Lever 		 */
245375eb6af7SChuck Lever 		if (xs_addr(upper_xprt)->sa_family == PF_INET6)
245475eb6af7SChuck Lever 			ip6_sock_set_addr_preferences(sk, IPV6_PREFER_SRC_PUBLIC);
245575eb6af7SChuck Lever 
245675eb6af7SChuck Lever 		xs_tcp_set_socket_timeouts(upper_xprt, sock);
245775eb6af7SChuck Lever 		tcp_sock_set_nodelay(sk);
245875eb6af7SChuck Lever 
245975eb6af7SChuck Lever 		lock_sock(sk);
246075eb6af7SChuck Lever 
246175eb6af7SChuck Lever 		/* @sk is already connected, so it now has the RPC callbacks.
246275eb6af7SChuck Lever 		 * Reach into @lower_transport to save the original ones.
246375eb6af7SChuck Lever 		 */
246475eb6af7SChuck Lever 		upper_transport->old_data_ready = lower_transport->old_data_ready;
246575eb6af7SChuck Lever 		upper_transport->old_state_change = lower_transport->old_state_change;
246675eb6af7SChuck Lever 		upper_transport->old_write_space = lower_transport->old_write_space;
246775eb6af7SChuck Lever 		upper_transport->old_error_report = lower_transport->old_error_report;
246875eb6af7SChuck Lever 		sk->sk_user_data = upper_xprt;
246975eb6af7SChuck Lever 
247075eb6af7SChuck Lever 		/* socket options */
247175eb6af7SChuck Lever 		sock_reset_flag(sk, SOCK_LINGER);
247275eb6af7SChuck Lever 
247375eb6af7SChuck Lever 		xprt_clear_connected(upper_xprt);
247475eb6af7SChuck Lever 
247575eb6af7SChuck Lever 		upper_transport->sock = sock;
247675eb6af7SChuck Lever 		upper_transport->inet = sk;
247775eb6af7SChuck Lever 		upper_transport->file = lower_transport->file;
247875eb6af7SChuck Lever 
247975eb6af7SChuck Lever 		release_sock(sk);
248075eb6af7SChuck Lever 
248175eb6af7SChuck Lever 		/* Reset lower_transport before shutting down its clnt */
248275eb6af7SChuck Lever 		mutex_lock(&lower_transport->recv_mutex);
248375eb6af7SChuck Lever 		lower_transport->inet = NULL;
248475eb6af7SChuck Lever 		lower_transport->sock = NULL;
248575eb6af7SChuck Lever 		lower_transport->file = NULL;
248675eb6af7SChuck Lever 
248775eb6af7SChuck Lever 		xprt_clear_connected(lower_xprt);
248875eb6af7SChuck Lever 		xs_sock_reset_connection_flags(lower_xprt);
248975eb6af7SChuck Lever 		xs_stream_reset_connect(lower_transport);
249075eb6af7SChuck Lever 		mutex_unlock(&lower_transport->recv_mutex);
249175eb6af7SChuck Lever 	}
249275eb6af7SChuck Lever 
249375eb6af7SChuck Lever 	if (!xprt_bound(upper_xprt))
249475eb6af7SChuck Lever 		return -ENOTCONN;
249575eb6af7SChuck Lever 
249675eb6af7SChuck Lever 	xs_set_memalloc(upper_xprt);
249775eb6af7SChuck Lever 
249875eb6af7SChuck Lever 	if (!xprt_test_and_set_connected(upper_xprt)) {
249975eb6af7SChuck Lever 		upper_xprt->connect_cookie++;
250075eb6af7SChuck Lever 		clear_bit(XPRT_SOCK_CONNECTING, &upper_transport->sock_state);
250175eb6af7SChuck Lever 		xprt_clear_connecting(upper_xprt);
250275eb6af7SChuck Lever 
250375eb6af7SChuck Lever 		upper_xprt->stat.connect_count++;
250475eb6af7SChuck Lever 		upper_xprt->stat.connect_time += (long)jiffies -
250575eb6af7SChuck Lever 					   upper_xprt->stat.connect_start;
250675eb6af7SChuck Lever 		xs_run_error_worker(upper_transport, XPRT_SOCK_WAKE_PENDING);
250775eb6af7SChuck Lever 	}
250875eb6af7SChuck Lever 	return 0;
250975eb6af7SChuck Lever }
251075eb6af7SChuck Lever 
251175eb6af7SChuck Lever /**
251275eb6af7SChuck Lever  * xs_tls_handshake_done - TLS handshake completion handler
251375eb6af7SChuck Lever  * @data: address of xprt to wake
251475eb6af7SChuck Lever  * @status: status of handshake
251575eb6af7SChuck Lever  * @peerid: serial number of key containing the remote's identity
251675eb6af7SChuck Lever  *
251775eb6af7SChuck Lever  */
251875eb6af7SChuck Lever static void xs_tls_handshake_done(void *data, int status, key_serial_t peerid)
251975eb6af7SChuck Lever {
252075eb6af7SChuck Lever 	struct rpc_xprt *lower_xprt = data;
252175eb6af7SChuck Lever 	struct sock_xprt *lower_transport =
252275eb6af7SChuck Lever 				container_of(lower_xprt, struct sock_xprt, xprt);
252375eb6af7SChuck Lever 
252475eb6af7SChuck Lever 	lower_transport->xprt_err = status ? -EACCES : 0;
252575eb6af7SChuck Lever 	complete(&lower_transport->handshake_done);
252675eb6af7SChuck Lever 	xprt_put(lower_xprt);
252775eb6af7SChuck Lever }
252875eb6af7SChuck Lever 
252975eb6af7SChuck Lever static int xs_tls_handshake_sync(struct rpc_xprt *lower_xprt, struct xprtsec_parms *xprtsec)
253075eb6af7SChuck Lever {
253175eb6af7SChuck Lever 	struct sock_xprt *lower_transport =
253275eb6af7SChuck Lever 				container_of(lower_xprt, struct sock_xprt, xprt);
253375eb6af7SChuck Lever 	struct tls_handshake_args args = {
253475eb6af7SChuck Lever 		.ta_sock	= lower_transport->sock,
253575eb6af7SChuck Lever 		.ta_done	= xs_tls_handshake_done,
253675eb6af7SChuck Lever 		.ta_data	= xprt_get(lower_xprt),
253775eb6af7SChuck Lever 		.ta_peername	= lower_xprt->servername,
253875eb6af7SChuck Lever 	};
253975eb6af7SChuck Lever 	struct sock *sk = lower_transport->inet;
254075eb6af7SChuck Lever 	int rc;
254175eb6af7SChuck Lever 
254275eb6af7SChuck Lever 	init_completion(&lower_transport->handshake_done);
254375eb6af7SChuck Lever 	set_bit(XPRT_SOCK_IGNORE_RECV, &lower_transport->sock_state);
254475eb6af7SChuck Lever 	lower_transport->xprt_err = -ETIMEDOUT;
254575eb6af7SChuck Lever 	switch (xprtsec->policy) {
254675eb6af7SChuck Lever 	case RPC_XPRTSEC_TLS_ANON:
254775eb6af7SChuck Lever 		rc = tls_client_hello_anon(&args, GFP_KERNEL);
254875eb6af7SChuck Lever 		if (rc)
254975eb6af7SChuck Lever 			goto out_put_xprt;
255075eb6af7SChuck Lever 		break;
255175eb6af7SChuck Lever 	case RPC_XPRTSEC_TLS_X509:
255275eb6af7SChuck Lever 		args.ta_my_cert = xprtsec->cert_serial;
255375eb6af7SChuck Lever 		args.ta_my_privkey = xprtsec->privkey_serial;
255475eb6af7SChuck Lever 		rc = tls_client_hello_x509(&args, GFP_KERNEL);
255575eb6af7SChuck Lever 		if (rc)
255675eb6af7SChuck Lever 			goto out_put_xprt;
255775eb6af7SChuck Lever 		break;
255875eb6af7SChuck Lever 	default:
255975eb6af7SChuck Lever 		rc = -EACCES;
256075eb6af7SChuck Lever 		goto out_put_xprt;
256175eb6af7SChuck Lever 	}
256275eb6af7SChuck Lever 
256375eb6af7SChuck Lever 	rc = wait_for_completion_interruptible_timeout(&lower_transport->handshake_done,
256475eb6af7SChuck Lever 						       XS_TLS_HANDSHAKE_TO);
256575eb6af7SChuck Lever 	if (rc <= 0) {
256675eb6af7SChuck Lever 		if (!tls_handshake_cancel(sk)) {
256775eb6af7SChuck Lever 			if (rc == 0)
256875eb6af7SChuck Lever 				rc = -ETIMEDOUT;
256975eb6af7SChuck Lever 			goto out_put_xprt;
257075eb6af7SChuck Lever 		}
257175eb6af7SChuck Lever 	}
257275eb6af7SChuck Lever 
257375eb6af7SChuck Lever 	rc = lower_transport->xprt_err;
257475eb6af7SChuck Lever 
257575eb6af7SChuck Lever out:
257675eb6af7SChuck Lever 	xs_stream_reset_connect(lower_transport);
257775eb6af7SChuck Lever 	clear_bit(XPRT_SOCK_IGNORE_RECV, &lower_transport->sock_state);
257875eb6af7SChuck Lever 	return rc;
257975eb6af7SChuck Lever 
258075eb6af7SChuck Lever out_put_xprt:
258175eb6af7SChuck Lever 	xprt_put(lower_xprt);
258275eb6af7SChuck Lever 	goto out;
258375eb6af7SChuck Lever }
258475eb6af7SChuck Lever 
258575eb6af7SChuck Lever /**
258675eb6af7SChuck Lever  * xs_tcp_tls_setup_socket - establish a TLS session on a TCP socket
258775eb6af7SChuck Lever  * @work: queued work item
258875eb6af7SChuck Lever  *
258975eb6af7SChuck Lever  * Invoked by a work queue tasklet.
259075eb6af7SChuck Lever  *
259175eb6af7SChuck Lever  * For RPC-with-TLS, there is a two-stage connection process.
259275eb6af7SChuck Lever  *
259375eb6af7SChuck Lever  * The "upper-layer xprt" is visible to the RPC consumer. Once it has
259475eb6af7SChuck Lever  * been marked connected, the consumer knows that a TCP connection and
259575eb6af7SChuck Lever  * a TLS session have been established.
259675eb6af7SChuck Lever  *
259775eb6af7SChuck Lever  * A "lower-layer xprt", created in this function, handles the mechanics
259875eb6af7SChuck Lever  * of connecting the TCP socket, performing the RPC_AUTH_TLS probe, and
259975eb6af7SChuck Lever  * then driving the TLS handshake. Once all that is complete, the upper
260075eb6af7SChuck Lever  * layer xprt is marked connected.
260175eb6af7SChuck Lever  */
260275eb6af7SChuck Lever static void xs_tcp_tls_setup_socket(struct work_struct *work)
260375eb6af7SChuck Lever {
260475eb6af7SChuck Lever 	struct sock_xprt *upper_transport =
260575eb6af7SChuck Lever 		container_of(work, struct sock_xprt, connect_worker.work);
260675eb6af7SChuck Lever 	struct rpc_clnt *upper_clnt = upper_transport->clnt;
260775eb6af7SChuck Lever 	struct rpc_xprt *upper_xprt = &upper_transport->xprt;
260875eb6af7SChuck Lever 	struct rpc_create_args args = {
260975eb6af7SChuck Lever 		.net		= upper_xprt->xprt_net,
261075eb6af7SChuck Lever 		.protocol	= upper_xprt->prot,
261175eb6af7SChuck Lever 		.address	= (struct sockaddr *)&upper_xprt->addr,
261275eb6af7SChuck Lever 		.addrsize	= upper_xprt->addrlen,
261375eb6af7SChuck Lever 		.timeout	= upper_clnt->cl_timeout,
261475eb6af7SChuck Lever 		.servername	= upper_xprt->servername,
261575eb6af7SChuck Lever 		.program	= upper_clnt->cl_program,
261675eb6af7SChuck Lever 		.prognumber	= upper_clnt->cl_prog,
261775eb6af7SChuck Lever 		.version	= upper_clnt->cl_vers,
261875eb6af7SChuck Lever 		.authflavor	= RPC_AUTH_TLS,
261975eb6af7SChuck Lever 		.cred		= upper_clnt->cl_cred,
262075eb6af7SChuck Lever 		.xprtsec	= {
262175eb6af7SChuck Lever 			.policy		= RPC_XPRTSEC_NONE,
262275eb6af7SChuck Lever 		},
262375eb6af7SChuck Lever 	};
262475eb6af7SChuck Lever 	unsigned int pflags = current->flags;
262575eb6af7SChuck Lever 	struct rpc_clnt *lower_clnt;
262675eb6af7SChuck Lever 	struct rpc_xprt *lower_xprt;
262775eb6af7SChuck Lever 	int status;
262875eb6af7SChuck Lever 
262975eb6af7SChuck Lever 	if (atomic_read(&upper_xprt->swapper))
263075eb6af7SChuck Lever 		current->flags |= PF_MEMALLOC;
263175eb6af7SChuck Lever 
263275eb6af7SChuck Lever 	xs_stream_start_connect(upper_transport);
263375eb6af7SChuck Lever 
263475eb6af7SChuck Lever 	/* This implicitly sends an RPC_AUTH_TLS probe */
263575eb6af7SChuck Lever 	lower_clnt = rpc_create(&args);
263675eb6af7SChuck Lever 	if (IS_ERR(lower_clnt)) {
263775eb6af7SChuck Lever 		trace_rpc_tls_unavailable(upper_clnt, upper_xprt);
263875eb6af7SChuck Lever 		clear_bit(XPRT_SOCK_CONNECTING, &upper_transport->sock_state);
263975eb6af7SChuck Lever 		xprt_clear_connecting(upper_xprt);
264075eb6af7SChuck Lever 		xprt_wake_pending_tasks(upper_xprt, PTR_ERR(lower_clnt));
264175eb6af7SChuck Lever 		xs_run_error_worker(upper_transport, XPRT_SOCK_WAKE_PENDING);
264275eb6af7SChuck Lever 		goto out_unlock;
264375eb6af7SChuck Lever 	}
264475eb6af7SChuck Lever 
264575eb6af7SChuck Lever 	/* RPC_AUTH_TLS probe was successful. Try a TLS handshake on
264675eb6af7SChuck Lever 	 * the lower xprt.
264775eb6af7SChuck Lever 	 */
264875eb6af7SChuck Lever 	rcu_read_lock();
264975eb6af7SChuck Lever 	lower_xprt = rcu_dereference(lower_clnt->cl_xprt);
265075eb6af7SChuck Lever 	rcu_read_unlock();
265175eb6af7SChuck Lever 	status = xs_tls_handshake_sync(lower_xprt, &upper_xprt->xprtsec);
265275eb6af7SChuck Lever 	if (status) {
265375eb6af7SChuck Lever 		trace_rpc_tls_not_started(upper_clnt, upper_xprt);
265475eb6af7SChuck Lever 		goto out_close;
265575eb6af7SChuck Lever 	}
265675eb6af7SChuck Lever 
265775eb6af7SChuck Lever 	status = xs_tcp_tls_finish_connecting(lower_xprt, upper_transport);
265875eb6af7SChuck Lever 	if (status)
265975eb6af7SChuck Lever 		goto out_close;
266075eb6af7SChuck Lever 
266175eb6af7SChuck Lever 	trace_rpc_socket_connect(upper_xprt, upper_transport->sock, 0);
266275eb6af7SChuck Lever 	if (!xprt_test_and_set_connected(upper_xprt)) {
266375eb6af7SChuck Lever 		upper_xprt->connect_cookie++;
266475eb6af7SChuck Lever 		clear_bit(XPRT_SOCK_CONNECTING, &upper_transport->sock_state);
266575eb6af7SChuck Lever 		xprt_clear_connecting(upper_xprt);
266675eb6af7SChuck Lever 
266775eb6af7SChuck Lever 		upper_xprt->stat.connect_count++;
266875eb6af7SChuck Lever 		upper_xprt->stat.connect_time += (long)jiffies -
266975eb6af7SChuck Lever 					   upper_xprt->stat.connect_start;
267075eb6af7SChuck Lever 		xs_run_error_worker(upper_transport, XPRT_SOCK_WAKE_PENDING);
267175eb6af7SChuck Lever 	}
267275eb6af7SChuck Lever 	rpc_shutdown_client(lower_clnt);
267375eb6af7SChuck Lever 
267475eb6af7SChuck Lever out_unlock:
267575eb6af7SChuck Lever 	current_restore_flags(pflags, PF_MEMALLOC);
267675eb6af7SChuck Lever 	upper_transport->clnt = NULL;
267775eb6af7SChuck Lever 	xprt_unlock_connect(upper_xprt, upper_transport);
267875eb6af7SChuck Lever 	return;
267975eb6af7SChuck Lever 
268075eb6af7SChuck Lever out_close:
268175eb6af7SChuck Lever 	rpc_shutdown_client(lower_clnt);
268275eb6af7SChuck Lever 
268375eb6af7SChuck Lever 	/* xprt_force_disconnect() wakes tasks with a fixed tk_status code.
268475eb6af7SChuck Lever 	 * Wake them first here to ensure they get our tk_status code.
268575eb6af7SChuck Lever 	 */
268675eb6af7SChuck Lever 	xprt_wake_pending_tasks(upper_xprt, status);
268775eb6af7SChuck Lever 	xs_tcp_force_close(upper_xprt);
268875eb6af7SChuck Lever 	xprt_clear_connecting(upper_xprt);
268975eb6af7SChuck Lever 	goto out_unlock;
269075eb6af7SChuck Lever }
269175eb6af7SChuck Lever 
269268e220bdSChuck Lever /**
26939903cd1cSChuck Lever  * xs_connect - connect a socket to a remote endpoint
26941b092092STrond Myklebust  * @xprt: pointer to transport structure
26959903cd1cSChuck Lever  * @task: address of RPC task that manages state of connect request
26969903cd1cSChuck Lever  *
26979903cd1cSChuck Lever  * TCP: If the remote end dropped the connection, delay reconnecting.
269803bf4b70SChuck Lever  *
269903bf4b70SChuck Lever  * UDP socket connects are synchronous, but we use a work queue anyway
270003bf4b70SChuck Lever  * to guarantee that even unprivileged user processes can set up a
270103bf4b70SChuck Lever  * socket on a privileged port.
270203bf4b70SChuck Lever  *
270303bf4b70SChuck Lever  * If a UDP socket connect fails, the delay behavior here prevents
270403bf4b70SChuck Lever  * retry floods (hard mounts).
27059903cd1cSChuck Lever  */
27061b092092STrond Myklebust static void xs_connect(struct rpc_xprt *xprt, struct rpc_task *task)
2707a246b010SChuck Lever {
2708ee0ac0c2SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
270902910177STrond Myklebust 	unsigned long delay = 0;
2710a246b010SChuck Lever 
2711718ba5b8STrond Myklebust 	WARN_ON_ONCE(!xprt_lock_connect(xprt, task, transport));
2712718ba5b8STrond Myklebust 
271389f42494STrond Myklebust 	if (transport->sock != NULL) {
271446121cf7SChuck Lever 		dprintk("RPC:       xs_connect delayed xprt %p for %lu "
271589f42494STrond Myklebust 			"seconds\n", xprt, xprt->reestablish_timeout / HZ);
271699b1a4c3STrond Myklebust 
2717675dd90aSChuck Lever 		delay = xprt_reconnect_delay(xprt);
2718675dd90aSChuck Lever 		xprt_reconnect_backoff(xprt, XS_TCP_INIT_REEST_TO);
271902910177STrond Myklebust 
272002910177STrond Myklebust 	} else
27219903cd1cSChuck Lever 		dprintk("RPC:       xs_connect scheduled xprt %p\n", xprt);
272202910177STrond Myklebust 
272375eb6af7SChuck Lever 	transport->clnt = task->tk_client;
272440a5f1b1STrond Myklebust 	queue_delayed_work(xprtiod_workqueue,
272502910177STrond Myklebust 			&transport->connect_worker,
272602910177STrond Myklebust 			delay);
2727a246b010SChuck Lever }
2728a246b010SChuck Lever 
27294f8943f8STrond Myklebust static void xs_wake_disconnect(struct sock_xprt *transport)
27304f8943f8STrond Myklebust {
27314f8943f8STrond Myklebust 	if (test_and_clear_bit(XPRT_SOCK_WAKE_DISCONNECT, &transport->sock_state))
27324f8943f8STrond Myklebust 		xs_tcp_force_close(&transport->xprt);
27334f8943f8STrond Myklebust }
27344f8943f8STrond Myklebust 
27354f8943f8STrond Myklebust static void xs_wake_write(struct sock_xprt *transport)
27364f8943f8STrond Myklebust {
27374f8943f8STrond Myklebust 	if (test_and_clear_bit(XPRT_SOCK_WAKE_WRITE, &transport->sock_state))
27384f8943f8STrond Myklebust 		xprt_write_space(&transport->xprt);
27394f8943f8STrond Myklebust }
27404f8943f8STrond Myklebust 
27414f8943f8STrond Myklebust static void xs_wake_error(struct sock_xprt *transport)
27424f8943f8STrond Myklebust {
27434f8943f8STrond Myklebust 	int sockerr;
27444f8943f8STrond Myklebust 
27454f8943f8STrond Myklebust 	if (!test_bit(XPRT_SOCK_WAKE_ERROR, &transport->sock_state))
27464f8943f8STrond Myklebust 		return;
27474f8943f8STrond Myklebust 	mutex_lock(&transport->recv_mutex);
27484f8943f8STrond Myklebust 	if (transport->sock == NULL)
27494f8943f8STrond Myklebust 		goto out;
27504f8943f8STrond Myklebust 	if (!test_and_clear_bit(XPRT_SOCK_WAKE_ERROR, &transport->sock_state))
27514f8943f8STrond Myklebust 		goto out;
2752af84537dSBenjamin Coddington 	sockerr = xchg(&transport->xprt_err, 0);
27534f8943f8STrond Myklebust 	if (sockerr < 0)
27544f8943f8STrond Myklebust 		xprt_wake_pending_tasks(&transport->xprt, sockerr);
27554f8943f8STrond Myklebust out:
27564f8943f8STrond Myklebust 	mutex_unlock(&transport->recv_mutex);
27574f8943f8STrond Myklebust }
27584f8943f8STrond Myklebust 
27594f8943f8STrond Myklebust static void xs_wake_pending(struct sock_xprt *transport)
27604f8943f8STrond Myklebust {
27614f8943f8STrond Myklebust 	if (test_and_clear_bit(XPRT_SOCK_WAKE_PENDING, &transport->sock_state))
27624f8943f8STrond Myklebust 		xprt_wake_pending_tasks(&transport->xprt, -EAGAIN);
27634f8943f8STrond Myklebust }
27644f8943f8STrond Myklebust 
27654f8943f8STrond Myklebust static void xs_error_handle(struct work_struct *work)
27664f8943f8STrond Myklebust {
27674f8943f8STrond Myklebust 	struct sock_xprt *transport = container_of(work,
27684f8943f8STrond Myklebust 			struct sock_xprt, error_worker);
27694f8943f8STrond Myklebust 
27704f8943f8STrond Myklebust 	xs_wake_disconnect(transport);
27714f8943f8STrond Myklebust 	xs_wake_write(transport);
27724f8943f8STrond Myklebust 	xs_wake_error(transport);
27734f8943f8STrond Myklebust 	xs_wake_pending(transport);
27744f8943f8STrond Myklebust }
27754f8943f8STrond Myklebust 
2776262ca07dSChuck Lever /**
277712b20ce3SBhaskar Chowdhury  * xs_local_print_stats - display AF_LOCAL socket-specific stats
2778176e21eeSChuck Lever  * @xprt: rpc_xprt struct containing statistics
2779176e21eeSChuck Lever  * @seq: output file
2780176e21eeSChuck Lever  *
2781176e21eeSChuck Lever  */
2782176e21eeSChuck Lever static void xs_local_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2783176e21eeSChuck Lever {
2784176e21eeSChuck Lever 	long idle_time = 0;
2785176e21eeSChuck Lever 
2786176e21eeSChuck Lever 	if (xprt_connected(xprt))
2787176e21eeSChuck Lever 		idle_time = (long)(jiffies - xprt->last_used) / HZ;
2788176e21eeSChuck Lever 
2789176e21eeSChuck Lever 	seq_printf(seq, "\txprt:\tlocal %lu %lu %lu %ld %lu %lu %lu "
279015a45206SAndy Adamson 			"%llu %llu %lu %llu %llu\n",
2791176e21eeSChuck Lever 			xprt->stat.bind_count,
2792176e21eeSChuck Lever 			xprt->stat.connect_count,
27938440a886SChuck Lever 			xprt->stat.connect_time / HZ,
2794176e21eeSChuck Lever 			idle_time,
2795176e21eeSChuck Lever 			xprt->stat.sends,
2796176e21eeSChuck Lever 			xprt->stat.recvs,
2797176e21eeSChuck Lever 			xprt->stat.bad_xids,
2798176e21eeSChuck Lever 			xprt->stat.req_u,
279915a45206SAndy Adamson 			xprt->stat.bklog_u,
280015a45206SAndy Adamson 			xprt->stat.max_slots,
280115a45206SAndy Adamson 			xprt->stat.sending_u,
280215a45206SAndy Adamson 			xprt->stat.pending_u);
2803176e21eeSChuck Lever }
2804176e21eeSChuck Lever 
2805176e21eeSChuck Lever /**
280612b20ce3SBhaskar Chowdhury  * xs_udp_print_stats - display UDP socket-specific stats
2807262ca07dSChuck Lever  * @xprt: rpc_xprt struct containing statistics
2808262ca07dSChuck Lever  * @seq: output file
2809262ca07dSChuck Lever  *
2810262ca07dSChuck Lever  */
2811262ca07dSChuck Lever static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2812262ca07dSChuck Lever {
2813c8475461SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2814c8475461SChuck Lever 
281515a45206SAndy Adamson 	seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %llu %llu "
281615a45206SAndy Adamson 			"%lu %llu %llu\n",
2817fbfffbd5SChuck Lever 			transport->srcport,
2818262ca07dSChuck Lever 			xprt->stat.bind_count,
2819262ca07dSChuck Lever 			xprt->stat.sends,
2820262ca07dSChuck Lever 			xprt->stat.recvs,
2821262ca07dSChuck Lever 			xprt->stat.bad_xids,
2822262ca07dSChuck Lever 			xprt->stat.req_u,
282315a45206SAndy Adamson 			xprt->stat.bklog_u,
282415a45206SAndy Adamson 			xprt->stat.max_slots,
282515a45206SAndy Adamson 			xprt->stat.sending_u,
282615a45206SAndy Adamson 			xprt->stat.pending_u);
2827262ca07dSChuck Lever }
2828262ca07dSChuck Lever 
2829262ca07dSChuck Lever /**
283012b20ce3SBhaskar Chowdhury  * xs_tcp_print_stats - display TCP socket-specific stats
2831262ca07dSChuck Lever  * @xprt: rpc_xprt struct containing statistics
2832262ca07dSChuck Lever  * @seq: output file
2833262ca07dSChuck Lever  *
2834262ca07dSChuck Lever  */
2835262ca07dSChuck Lever static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2836262ca07dSChuck Lever {
2837c8475461SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2838262ca07dSChuck Lever 	long idle_time = 0;
2839262ca07dSChuck Lever 
2840262ca07dSChuck Lever 	if (xprt_connected(xprt))
2841262ca07dSChuck Lever 		idle_time = (long)(jiffies - xprt->last_used) / HZ;
2842262ca07dSChuck Lever 
284315a45206SAndy Adamson 	seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu "
284415a45206SAndy Adamson 			"%llu %llu %lu %llu %llu\n",
2845fbfffbd5SChuck Lever 			transport->srcport,
2846262ca07dSChuck Lever 			xprt->stat.bind_count,
2847262ca07dSChuck Lever 			xprt->stat.connect_count,
28488440a886SChuck Lever 			xprt->stat.connect_time / HZ,
2849262ca07dSChuck Lever 			idle_time,
2850262ca07dSChuck Lever 			xprt->stat.sends,
2851262ca07dSChuck Lever 			xprt->stat.recvs,
2852262ca07dSChuck Lever 			xprt->stat.bad_xids,
2853262ca07dSChuck Lever 			xprt->stat.req_u,
285415a45206SAndy Adamson 			xprt->stat.bklog_u,
285515a45206SAndy Adamson 			xprt->stat.max_slots,
285615a45206SAndy Adamson 			xprt->stat.sending_u,
285715a45206SAndy Adamson 			xprt->stat.pending_u);
2858262ca07dSChuck Lever }
2859262ca07dSChuck Lever 
28604cfc7e60SRahul Iyer /*
28614cfc7e60SRahul Iyer  * Allocate a bunch of pages for a scratch buffer for the rpc code. The reason
28624cfc7e60SRahul Iyer  * we allocate pages instead doing a kmalloc like rpc_malloc is because we want
28634cfc7e60SRahul Iyer  * to use the server side send routines.
28644cfc7e60SRahul Iyer  */
28655fe6eaa1SChuck Lever static int bc_malloc(struct rpc_task *task)
28664cfc7e60SRahul Iyer {
28675fe6eaa1SChuck Lever 	struct rpc_rqst *rqst = task->tk_rqstp;
28685fe6eaa1SChuck Lever 	size_t size = rqst->rq_callsize;
28694cfc7e60SRahul Iyer 	struct page *page;
28704cfc7e60SRahul Iyer 	struct rpc_buffer *buf;
28714cfc7e60SRahul Iyer 
28725fe6eaa1SChuck Lever 	if (size > PAGE_SIZE - sizeof(struct rpc_buffer)) {
28735fe6eaa1SChuck Lever 		WARN_ONCE(1, "xprtsock: large bc buffer request (size %zu)\n",
28745fe6eaa1SChuck Lever 			  size);
28755fe6eaa1SChuck Lever 		return -EINVAL;
28765fe6eaa1SChuck Lever 	}
28774cfc7e60SRahul Iyer 
2878b2648015STrond Myklebust 	page = alloc_page(GFP_KERNEL | __GFP_NORETRY | __GFP_NOWARN);
28794cfc7e60SRahul Iyer 	if (!page)
28805fe6eaa1SChuck Lever 		return -ENOMEM;
28814cfc7e60SRahul Iyer 
28824cfc7e60SRahul Iyer 	buf = page_address(page);
28834cfc7e60SRahul Iyer 	buf->len = PAGE_SIZE;
28844cfc7e60SRahul Iyer 
28855fe6eaa1SChuck Lever 	rqst->rq_buffer = buf->data;
288618e601d6SJeff Layton 	rqst->rq_rbuffer = (char *)rqst->rq_buffer + rqst->rq_callsize;
28875fe6eaa1SChuck Lever 	return 0;
28884cfc7e60SRahul Iyer }
28894cfc7e60SRahul Iyer 
28904cfc7e60SRahul Iyer /*
28914cfc7e60SRahul Iyer  * Free the space allocated in the bc_alloc routine
28924cfc7e60SRahul Iyer  */
28933435c74aSChuck Lever static void bc_free(struct rpc_task *task)
28944cfc7e60SRahul Iyer {
28953435c74aSChuck Lever 	void *buffer = task->tk_rqstp->rq_buffer;
28964cfc7e60SRahul Iyer 	struct rpc_buffer *buf;
28974cfc7e60SRahul Iyer 
28984cfc7e60SRahul Iyer 	buf = container_of(buffer, struct rpc_buffer, data);
28994cfc7e60SRahul Iyer 	free_page((unsigned long)buf);
29004cfc7e60SRahul Iyer }
29014cfc7e60SRahul Iyer 
29024cfc7e60SRahul Iyer static int bc_sendto(struct rpc_rqst *req)
29034cfc7e60SRahul Iyer {
2904da1661b9SChuck Lever 	struct xdr_buf *xdr = &req->rq_snd_buf;
29054cfc7e60SRahul Iyer 	struct sock_xprt *transport =
2906067fb11bSChuck Lever 			container_of(req->rq_xprt, struct sock_xprt, xprt);
2907067fb11bSChuck Lever 	struct msghdr msg = {
2908da1661b9SChuck Lever 		.msg_flags	= 0,
2909067fb11bSChuck Lever 	};
2910067fb11bSChuck Lever 	rpc_fraghdr marker = cpu_to_be32(RPC_LAST_STREAM_FRAGMENT |
2911da1661b9SChuck Lever 					 (u32)xdr->len);
2912da1661b9SChuck Lever 	unsigned int sent = 0;
2913da1661b9SChuck Lever 	int err;
29144cfc7e60SRahul Iyer 
29158729aabaSChuck Lever 	req->rq_xtime = ktime_get();
2916ff053dbbSTrond Myklebust 	err = xdr_alloc_bvec(xdr, rpc_task_gfp_mask());
2917ff053dbbSTrond Myklebust 	if (err < 0)
2918ff053dbbSTrond Myklebust 		return err;
2919da1661b9SChuck Lever 	err = xprt_sock_sendmsg(transport->sock, &msg, xdr, 0, marker, &sent);
2920da1661b9SChuck Lever 	xdr_free_bvec(xdr);
2921da1661b9SChuck Lever 	if (err < 0 || sent != (xdr->len + sizeof(marker)))
2922067fb11bSChuck Lever 		return -EAGAIN;
2923da1661b9SChuck Lever 	return sent;
29244cfc7e60SRahul Iyer }
29254cfc7e60SRahul Iyer 
2926ca4faf54SChuck Lever /**
2927ca4faf54SChuck Lever  * bc_send_request - Send a backchannel Call on a TCP socket
2928ca4faf54SChuck Lever  * @req: rpc_rqst containing Call message to be sent
2929ca4faf54SChuck Lever  *
2930ca4faf54SChuck Lever  * xpt_mutex ensures @rqstp's whole message is written to the socket
2931ca4faf54SChuck Lever  * without interruption.
2932ca4faf54SChuck Lever  *
2933ca4faf54SChuck Lever  * Return values:
2934ca4faf54SChuck Lever  *   %0 if the message was sent successfully
2935ca4faf54SChuck Lever  *   %ENOTCONN if the message was not sent
29364cfc7e60SRahul Iyer  */
2937adfa7144STrond Myklebust static int bc_send_request(struct rpc_rqst *req)
29384cfc7e60SRahul Iyer {
29394cfc7e60SRahul Iyer 	struct svc_xprt	*xprt;
29407fc56136SAndrzej Hajda 	int len;
29414cfc7e60SRahul Iyer 
29424cfc7e60SRahul Iyer 	/*
29434cfc7e60SRahul Iyer 	 * Get the server socket associated with this callback xprt
29444cfc7e60SRahul Iyer 	 */
29454cfc7e60SRahul Iyer 	xprt = req->rq_xprt->bc_xprt;
29464cfc7e60SRahul Iyer 
29474cfc7e60SRahul Iyer 	/*
29484cfc7e60SRahul Iyer 	 * Grab the mutex to serialize data as the connection is shared
29494cfc7e60SRahul Iyer 	 * with the fore channel
29504cfc7e60SRahul Iyer 	 */
2951c544577dSTrond Myklebust 	mutex_lock(&xprt->xpt_mutex);
29524cfc7e60SRahul Iyer 	if (test_bit(XPT_DEAD, &xprt->xpt_flags))
29534cfc7e60SRahul Iyer 		len = -ENOTCONN;
29544cfc7e60SRahul Iyer 	else
29554cfc7e60SRahul Iyer 		len = bc_sendto(req);
29564cfc7e60SRahul Iyer 	mutex_unlock(&xprt->xpt_mutex);
29574cfc7e60SRahul Iyer 
29584cfc7e60SRahul Iyer 	if (len > 0)
29594cfc7e60SRahul Iyer 		len = 0;
29604cfc7e60SRahul Iyer 
29614cfc7e60SRahul Iyer 	return len;
29624cfc7e60SRahul Iyer }
29634cfc7e60SRahul Iyer 
29644cfc7e60SRahul Iyer /*
29654cfc7e60SRahul Iyer  * The close routine. Since this is client initiated, we do nothing
29664cfc7e60SRahul Iyer  */
29674cfc7e60SRahul Iyer 
29684cfc7e60SRahul Iyer static void bc_close(struct rpc_xprt *xprt)
29694cfc7e60SRahul Iyer {
29706221f1d9SChuck Lever 	xprt_disconnect_done(xprt);
29714cfc7e60SRahul Iyer }
29724cfc7e60SRahul Iyer 
29734cfc7e60SRahul Iyer /*
29744cfc7e60SRahul Iyer  * The xprt destroy routine. Again, because this connection is client
29754cfc7e60SRahul Iyer  * initiated, we do nothing
29764cfc7e60SRahul Iyer  */
29774cfc7e60SRahul Iyer 
29784cfc7e60SRahul Iyer static void bc_destroy(struct rpc_xprt *xprt)
29794cfc7e60SRahul Iyer {
298047f72efaSKinglong Mee 	dprintk("RPC:       bc_destroy xprt %p\n", xprt);
298147f72efaSKinglong Mee 
298247f72efaSKinglong Mee 	xs_xprt_free(xprt);
298347f72efaSKinglong Mee 	module_put(THIS_MODULE);
29844cfc7e60SRahul Iyer }
29854cfc7e60SRahul Iyer 
2986d31ae254SChuck Lever static const struct rpc_xprt_ops xs_local_ops = {
2987176e21eeSChuck Lever 	.reserve_xprt		= xprt_reserve_xprt,
29884cd34e7cSTrond Myklebust 	.release_xprt		= xprt_release_xprt,
2989f39c1bfbSTrond Myklebust 	.alloc_slot		= xprt_alloc_slot,
2990a9cde23aSChuck Lever 	.free_slot		= xprt_free_slot,
2991176e21eeSChuck Lever 	.rpcbind		= xs_local_rpcbind,
2992176e21eeSChuck Lever 	.set_port		= xs_local_set_port,
2993dc107402SJ. Bruce Fields 	.connect		= xs_local_connect,
2994176e21eeSChuck Lever 	.buf_alloc		= rpc_malloc,
2995176e21eeSChuck Lever 	.buf_free		= rpc_free,
2996550aebfeSTrond Myklebust 	.prepare_request	= xs_stream_prepare_request,
2997176e21eeSChuck Lever 	.send_request		= xs_local_send_request,
29988ba6a92dSTrond Myklebust 	.wait_for_reply_request	= xprt_wait_for_reply_request_def,
2999176e21eeSChuck Lever 	.close			= xs_close,
3000a1311d87STrond Myklebust 	.destroy		= xs_destroy,
3001176e21eeSChuck Lever 	.print_stats		= xs_local_print_stats,
3002d67fa4d8SJeff Layton 	.enable_swap		= xs_enable_swap,
3003d67fa4d8SJeff Layton 	.disable_swap		= xs_disable_swap,
3004176e21eeSChuck Lever };
3005176e21eeSChuck Lever 
3006d31ae254SChuck Lever static const struct rpc_xprt_ops xs_udp_ops = {
300743118c29SChuck Lever 	.set_buffer_size	= xs_udp_set_buffer_size,
300812a80469SChuck Lever 	.reserve_xprt		= xprt_reserve_xprt_cong,
300949e9a890SChuck Lever 	.release_xprt		= xprt_release_xprt_cong,
3010f39c1bfbSTrond Myklebust 	.alloc_slot		= xprt_alloc_slot,
3011a9cde23aSChuck Lever 	.free_slot		= xprt_free_slot,
301245160d62SChuck Lever 	.rpcbind		= rpcb_getport_async,
301392200412SChuck Lever 	.set_port		= xs_set_port,
30149903cd1cSChuck Lever 	.connect		= xs_connect,
3015421ab1beSTrond Myklebust 	.get_srcaddr		= xs_sock_srcaddr,
3016421ab1beSTrond Myklebust 	.get_srcport		= xs_sock_srcport,
301702107148SChuck Lever 	.buf_alloc		= rpc_malloc,
301802107148SChuck Lever 	.buf_free		= rpc_free,
3019262965f5SChuck Lever 	.send_request		= xs_udp_send_request,
30208ba6a92dSTrond Myklebust 	.wait_for_reply_request	= xprt_wait_for_reply_request_rtt,
302146c0ee8bSChuck Lever 	.timer			= xs_udp_timer,
3022a58dd398SChuck Lever 	.release_request	= xprt_release_rqst_cong,
3023262965f5SChuck Lever 	.close			= xs_close,
3024262965f5SChuck Lever 	.destroy		= xs_destroy,
3025262ca07dSChuck Lever 	.print_stats		= xs_udp_print_stats,
3026d67fa4d8SJeff Layton 	.enable_swap		= xs_enable_swap,
3027d67fa4d8SJeff Layton 	.disable_swap		= xs_disable_swap,
30284a068258SChuck Lever 	.inject_disconnect	= xs_inject_disconnect,
3029262965f5SChuck Lever };
3030262965f5SChuck Lever 
3031d31ae254SChuck Lever static const struct rpc_xprt_ops xs_tcp_ops = {
303212a80469SChuck Lever 	.reserve_xprt		= xprt_reserve_xprt,
30334cd34e7cSTrond Myklebust 	.release_xprt		= xprt_release_xprt,
303436bd7de9STrond Myklebust 	.alloc_slot		= xprt_alloc_slot,
3035a9cde23aSChuck Lever 	.free_slot		= xprt_free_slot,
303645160d62SChuck Lever 	.rpcbind		= rpcb_getport_async,
303792200412SChuck Lever 	.set_port		= xs_set_port,
30380b9e7943STrond Myklebust 	.connect		= xs_connect,
3039421ab1beSTrond Myklebust 	.get_srcaddr		= xs_sock_srcaddr,
3040421ab1beSTrond Myklebust 	.get_srcport		= xs_sock_srcport,
304102107148SChuck Lever 	.buf_alloc		= rpc_malloc,
304202107148SChuck Lever 	.buf_free		= rpc_free,
3043277e4ab7STrond Myklebust 	.prepare_request	= xs_stream_prepare_request,
3044262965f5SChuck Lever 	.send_request		= xs_tcp_send_request,
30458ba6a92dSTrond Myklebust 	.wait_for_reply_request	= xprt_wait_for_reply_request_def,
3046c627d31bSTrond Myklebust 	.close			= xs_tcp_shutdown,
30479903cd1cSChuck Lever 	.destroy		= xs_destroy,
30487196dbb0STrond Myklebust 	.set_connect_timeout	= xs_tcp_set_connect_timeout,
3049262ca07dSChuck Lever 	.print_stats		= xs_tcp_print_stats,
3050d67fa4d8SJeff Layton 	.enable_swap		= xs_enable_swap,
3051d67fa4d8SJeff Layton 	.disable_swap		= xs_disable_swap,
30524a068258SChuck Lever 	.inject_disconnect	= xs_inject_disconnect,
305342e5c3e2SChuck Lever #ifdef CONFIG_SUNRPC_BACKCHANNEL
305442e5c3e2SChuck Lever 	.bc_setup		= xprt_setup_bc,
30556b26cc8cSChuck Lever 	.bc_maxpayload		= xs_tcp_bc_maxpayload,
30567402a4feSTrond Myklebust 	.bc_num_slots		= xprt_bc_max_slots,
305742e5c3e2SChuck Lever 	.bc_free_rqst		= xprt_free_bc_rqst,
305842e5c3e2SChuck Lever 	.bc_destroy		= xprt_destroy_bc,
305942e5c3e2SChuck Lever #endif
3060a246b010SChuck Lever };
3061a246b010SChuck Lever 
30624cfc7e60SRahul Iyer /*
30634cfc7e60SRahul Iyer  * The rpc_xprt_ops for the server backchannel
30644cfc7e60SRahul Iyer  */
30654cfc7e60SRahul Iyer 
3066d31ae254SChuck Lever static const struct rpc_xprt_ops bc_tcp_ops = {
30674cfc7e60SRahul Iyer 	.reserve_xprt		= xprt_reserve_xprt,
30684cfc7e60SRahul Iyer 	.release_xprt		= xprt_release_xprt,
306984e28a30SBryan Schumaker 	.alloc_slot		= xprt_alloc_slot,
3070a9cde23aSChuck Lever 	.free_slot		= xprt_free_slot,
30714cfc7e60SRahul Iyer 	.buf_alloc		= bc_malloc,
30724cfc7e60SRahul Iyer 	.buf_free		= bc_free,
30734cfc7e60SRahul Iyer 	.send_request		= bc_send_request,
30748ba6a92dSTrond Myklebust 	.wait_for_reply_request	= xprt_wait_for_reply_request_def,
30754cfc7e60SRahul Iyer 	.close			= bc_close,
30764cfc7e60SRahul Iyer 	.destroy		= bc_destroy,
30774cfc7e60SRahul Iyer 	.print_stats		= xs_tcp_print_stats,
3078d67fa4d8SJeff Layton 	.enable_swap		= xs_enable_swap,
3079d67fa4d8SJeff Layton 	.disable_swap		= xs_disable_swap,
30804a068258SChuck Lever 	.inject_disconnect	= xs_inject_disconnect,
30814cfc7e60SRahul Iyer };
30824cfc7e60SRahul Iyer 
308392476850SChuck Lever static int xs_init_anyaddr(const int family, struct sockaddr *sap)
308492476850SChuck Lever {
308592476850SChuck Lever 	static const struct sockaddr_in sin = {
308692476850SChuck Lever 		.sin_family		= AF_INET,
308792476850SChuck Lever 		.sin_addr.s_addr	= htonl(INADDR_ANY),
308892476850SChuck Lever 	};
308992476850SChuck Lever 	static const struct sockaddr_in6 sin6 = {
309092476850SChuck Lever 		.sin6_family		= AF_INET6,
309192476850SChuck Lever 		.sin6_addr		= IN6ADDR_ANY_INIT,
309292476850SChuck Lever 	};
309392476850SChuck Lever 
309492476850SChuck Lever 	switch (family) {
3095176e21eeSChuck Lever 	case AF_LOCAL:
3096176e21eeSChuck Lever 		break;
309792476850SChuck Lever 	case AF_INET:
309892476850SChuck Lever 		memcpy(sap, &sin, sizeof(sin));
309992476850SChuck Lever 		break;
310092476850SChuck Lever 	case AF_INET6:
310192476850SChuck Lever 		memcpy(sap, &sin6, sizeof(sin6));
310292476850SChuck Lever 		break;
310392476850SChuck Lever 	default:
310492476850SChuck Lever 		dprintk("RPC:       %s: Bad address family\n", __func__);
310592476850SChuck Lever 		return -EAFNOSUPPORT;
310692476850SChuck Lever 	}
310792476850SChuck Lever 	return 0;
310892476850SChuck Lever }
310992476850SChuck Lever 
31103c341b0bS\"Talpey, Thomas\ static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args,
3111d9ba131dSTrond Myklebust 				      unsigned int slot_table_size,
3112d9ba131dSTrond Myklebust 				      unsigned int max_slot_table_size)
3113c8541ecdSChuck Lever {
3114c8541ecdSChuck Lever 	struct rpc_xprt *xprt;
3115ffc2e518SChuck Lever 	struct sock_xprt *new;
3116c8541ecdSChuck Lever 
311796802a09SFrank van Maarseveen 	if (args->addrlen > sizeof(xprt->addr)) {
3118c8541ecdSChuck Lever 		dprintk("RPC:       xs_setup_xprt: address too large\n");
3119c8541ecdSChuck Lever 		return ERR_PTR(-EBADF);
3120c8541ecdSChuck Lever 	}
3121c8541ecdSChuck Lever 
3122d9ba131dSTrond Myklebust 	xprt = xprt_alloc(args->net, sizeof(*new), slot_table_size,
3123d9ba131dSTrond Myklebust 			max_slot_table_size);
3124bd1722d4SPavel Emelyanov 	if (xprt == NULL) {
312546121cf7SChuck Lever 		dprintk("RPC:       xs_setup_xprt: couldn't allocate "
312646121cf7SChuck Lever 				"rpc_xprt\n");
3127c8541ecdSChuck Lever 		return ERR_PTR(-ENOMEM);
3128c8541ecdSChuck Lever 	}
3129c8541ecdSChuck Lever 
3130bd1722d4SPavel Emelyanov 	new = container_of(xprt, struct sock_xprt, xprt);
3131edc1b01cSTrond Myklebust 	mutex_init(&new->recv_mutex);
313296802a09SFrank van Maarseveen 	memcpy(&xprt->addr, args->dstaddr, args->addrlen);
313396802a09SFrank van Maarseveen 	xprt->addrlen = args->addrlen;
3134d3bc9a1dSFrank van Maarseveen 	if (args->srcaddr)
3135fbfffbd5SChuck Lever 		memcpy(&new->srcaddr, args->srcaddr, args->addrlen);
313692476850SChuck Lever 	else {
313792476850SChuck Lever 		int err;
313892476850SChuck Lever 		err = xs_init_anyaddr(args->dstaddr->sa_family,
313992476850SChuck Lever 					(struct sockaddr *)&new->srcaddr);
31402aa13531SStanislav Kinsbursky 		if (err != 0) {
31412aa13531SStanislav Kinsbursky 			xprt_free(xprt);
314292476850SChuck Lever 			return ERR_PTR(err);
314392476850SChuck Lever 		}
31442aa13531SStanislav Kinsbursky 	}
3145c8541ecdSChuck Lever 
3146c8541ecdSChuck Lever 	return xprt;
3147c8541ecdSChuck Lever }
3148c8541ecdSChuck Lever 
3149176e21eeSChuck Lever static const struct rpc_timeout xs_local_default_timeout = {
3150176e21eeSChuck Lever 	.to_initval = 10 * HZ,
3151176e21eeSChuck Lever 	.to_maxval = 10 * HZ,
3152176e21eeSChuck Lever 	.to_retries = 2,
3153176e21eeSChuck Lever };
3154176e21eeSChuck Lever 
3155176e21eeSChuck Lever /**
3156176e21eeSChuck Lever  * xs_setup_local - Set up transport to use an AF_LOCAL socket
3157176e21eeSChuck Lever  * @args: rpc transport creation arguments
3158176e21eeSChuck Lever  *
3159176e21eeSChuck Lever  * AF_LOCAL is a "tpi_cots_ord" transport, just like TCP
3160176e21eeSChuck Lever  */
3161176e21eeSChuck Lever static struct rpc_xprt *xs_setup_local(struct xprt_create *args)
3162176e21eeSChuck Lever {
3163176e21eeSChuck Lever 	struct sockaddr_un *sun = (struct sockaddr_un *)args->dstaddr;
3164176e21eeSChuck Lever 	struct sock_xprt *transport;
3165176e21eeSChuck Lever 	struct rpc_xprt *xprt;
3166176e21eeSChuck Lever 	struct rpc_xprt *ret;
3167176e21eeSChuck Lever 
3168d9ba131dSTrond Myklebust 	xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
3169d9ba131dSTrond Myklebust 			xprt_max_tcp_slot_table_entries);
3170176e21eeSChuck Lever 	if (IS_ERR(xprt))
3171176e21eeSChuck Lever 		return xprt;
3172176e21eeSChuck Lever 	transport = container_of(xprt, struct sock_xprt, xprt);
3173176e21eeSChuck Lever 
3174176e21eeSChuck Lever 	xprt->prot = 0;
3175d3abc739SOlga Kornievskaia 	xprt->xprt_class = &xs_local_transport;
3176176e21eeSChuck Lever 	xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
3177176e21eeSChuck Lever 
3178176e21eeSChuck Lever 	xprt->bind_timeout = XS_BIND_TO;
3179176e21eeSChuck Lever 	xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
3180176e21eeSChuck Lever 	xprt->idle_timeout = XS_IDLE_DISC_TO;
3181176e21eeSChuck Lever 
3182176e21eeSChuck Lever 	xprt->ops = &xs_local_ops;
3183176e21eeSChuck Lever 	xprt->timeout = &xs_local_default_timeout;
3184176e21eeSChuck Lever 
3185550aebfeSTrond Myklebust 	INIT_WORK(&transport->recv_worker, xs_stream_data_receive_workfn);
31864f8943f8STrond Myklebust 	INIT_WORK(&transport->error_worker, xs_error_handle);
3187550aebfeSTrond Myklebust 	INIT_DELAYED_WORK(&transport->connect_worker, xs_dummy_setup_socket);
318893dc41bdSNeilBrown 
3189176e21eeSChuck Lever 	switch (sun->sun_family) {
3190176e21eeSChuck Lever 	case AF_LOCAL:
31914388ce05SNeilBrown 		if (sun->sun_path[0] != '/' && sun->sun_path[0] != '\0') {
3192176e21eeSChuck Lever 			dprintk("RPC:       bad AF_LOCAL address: %s\n",
3193176e21eeSChuck Lever 					sun->sun_path);
3194176e21eeSChuck Lever 			ret = ERR_PTR(-EINVAL);
3195176e21eeSChuck Lever 			goto out_err;
3196176e21eeSChuck Lever 		}
3197176e21eeSChuck Lever 		xprt_set_bound(xprt);
3198176e21eeSChuck Lever 		xs_format_peer_addresses(xprt, "local", RPCBIND_NETID_LOCAL);
3199176e21eeSChuck Lever 		break;
3200176e21eeSChuck Lever 	default:
3201176e21eeSChuck Lever 		ret = ERR_PTR(-EAFNOSUPPORT);
3202176e21eeSChuck Lever 		goto out_err;
3203176e21eeSChuck Lever 	}
3204176e21eeSChuck Lever 
3205176e21eeSChuck Lever 	dprintk("RPC:       set up xprt to %s via AF_LOCAL\n",
3206176e21eeSChuck Lever 			xprt->address_strings[RPC_DISPLAY_ADDR]);
3207176e21eeSChuck Lever 
3208176e21eeSChuck Lever 	if (try_module_get(THIS_MODULE))
3209176e21eeSChuck Lever 		return xprt;
3210176e21eeSChuck Lever 	ret = ERR_PTR(-EINVAL);
3211176e21eeSChuck Lever out_err:
3212315f3812SKinglong Mee 	xs_xprt_free(xprt);
3213176e21eeSChuck Lever 	return ret;
3214176e21eeSChuck Lever }
3215176e21eeSChuck Lever 
32162881ae74STrond Myklebust static const struct rpc_timeout xs_udp_default_timeout = {
32172881ae74STrond Myklebust 	.to_initval = 5 * HZ,
32182881ae74STrond Myklebust 	.to_maxval = 30 * HZ,
32192881ae74STrond Myklebust 	.to_increment = 5 * HZ,
32202881ae74STrond Myklebust 	.to_retries = 5,
32212881ae74STrond Myklebust };
32222881ae74STrond Myklebust 
32239903cd1cSChuck Lever /**
32249903cd1cSChuck Lever  * xs_setup_udp - Set up transport to use a UDP socket
322596802a09SFrank van Maarseveen  * @args: rpc transport creation arguments
32269903cd1cSChuck Lever  *
32279903cd1cSChuck Lever  */
3228483066d6SAdrian Bunk static struct rpc_xprt *xs_setup_udp(struct xprt_create *args)
3229a246b010SChuck Lever {
32308f9d5b1aSChuck Lever 	struct sockaddr *addr = args->dstaddr;
3231c8541ecdSChuck Lever 	struct rpc_xprt *xprt;
3232c8475461SChuck Lever 	struct sock_xprt *transport;
32330a68b0beSJ. Bruce Fields 	struct rpc_xprt *ret;
3234a246b010SChuck Lever 
3235d9ba131dSTrond Myklebust 	xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries,
3236d9ba131dSTrond Myklebust 			xprt_udp_slot_table_entries);
3237c8541ecdSChuck Lever 	if (IS_ERR(xprt))
3238c8541ecdSChuck Lever 		return xprt;
3239c8475461SChuck Lever 	transport = container_of(xprt, struct sock_xprt, xprt);
3240a246b010SChuck Lever 
3241ec739ef0SChuck Lever 	xprt->prot = IPPROTO_UDP;
3242d3abc739SOlga Kornievskaia 	xprt->xprt_class = &xs_udp_transport;
3243a246b010SChuck Lever 	/* XXX: header size can vary due to auth type, IPv6, etc. */
3244a246b010SChuck Lever 	xprt->max_payload = (1U << 16) - (MAX_HEADER << 3);
3245a246b010SChuck Lever 
324603bf4b70SChuck Lever 	xprt->bind_timeout = XS_BIND_TO;
324703bf4b70SChuck Lever 	xprt->reestablish_timeout = XS_UDP_REEST_TO;
324803bf4b70SChuck Lever 	xprt->idle_timeout = XS_IDLE_DISC_TO;
3249a246b010SChuck Lever 
3250262965f5SChuck Lever 	xprt->ops = &xs_udp_ops;
3251a246b010SChuck Lever 
3252ba7392bbSTrond Myklebust 	xprt->timeout = &xs_udp_default_timeout;
3253a246b010SChuck Lever 
3254f9b2ee71STrond Myklebust 	INIT_WORK(&transport->recv_worker, xs_udp_data_receive_workfn);
32554f8943f8STrond Myklebust 	INIT_WORK(&transport->error_worker, xs_error_handle);
3256edc1b01cSTrond Myklebust 	INIT_DELAYED_WORK(&transport->connect_worker, xs_udp_setup_socket);
3257edc1b01cSTrond Myklebust 
32588f9d5b1aSChuck Lever 	switch (addr->sa_family) {
32598f9d5b1aSChuck Lever 	case AF_INET:
32608f9d5b1aSChuck Lever 		if (((struct sockaddr_in *)addr)->sin_port != htons(0))
32618f9d5b1aSChuck Lever 			xprt_set_bound(xprt);
32628f9d5b1aSChuck Lever 
32639dc3b095SChuck Lever 		xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP);
32648f9d5b1aSChuck Lever 		break;
32658f9d5b1aSChuck Lever 	case AF_INET6:
32668f9d5b1aSChuck Lever 		if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
32678f9d5b1aSChuck Lever 			xprt_set_bound(xprt);
32688f9d5b1aSChuck Lever 
32699dc3b095SChuck Lever 		xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6);
32708f9d5b1aSChuck Lever 		break;
32718f9d5b1aSChuck Lever 	default:
32720a68b0beSJ. Bruce Fields 		ret = ERR_PTR(-EAFNOSUPPORT);
32730a68b0beSJ. Bruce Fields 		goto out_err;
32748f9d5b1aSChuck Lever 	}
32758f9d5b1aSChuck Lever 
3276c740eff8SChuck Lever 	if (xprt_bound(xprt))
3277c740eff8SChuck Lever 		dprintk("RPC:       set up xprt to %s (port %s) via %s\n",
3278c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_ADDR],
3279c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_PORT],
3280c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_PROTO]);
3281c740eff8SChuck Lever 	else
3282c740eff8SChuck Lever 		dprintk("RPC:       set up xprt to %s (autobind) via %s\n",
3283c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_ADDR],
3284c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_PROTO]);
3285edb267a6SChuck Lever 
3286bc25571eS\"Talpey, Thomas\ 	if (try_module_get(THIS_MODULE))
3287c8541ecdSChuck Lever 		return xprt;
32880a68b0beSJ. Bruce Fields 	ret = ERR_PTR(-EINVAL);
32890a68b0beSJ. Bruce Fields out_err:
3290315f3812SKinglong Mee 	xs_xprt_free(xprt);
32910a68b0beSJ. Bruce Fields 	return ret;
3292a246b010SChuck Lever }
3293a246b010SChuck Lever 
32942881ae74STrond Myklebust static const struct rpc_timeout xs_tcp_default_timeout = {
32952881ae74STrond Myklebust 	.to_initval = 60 * HZ,
32962881ae74STrond Myklebust 	.to_maxval = 60 * HZ,
32972881ae74STrond Myklebust 	.to_retries = 2,
32982881ae74STrond Myklebust };
32992881ae74STrond Myklebust 
33009903cd1cSChuck Lever /**
33019903cd1cSChuck Lever  * xs_setup_tcp - Set up transport to use a TCP socket
330296802a09SFrank van Maarseveen  * @args: rpc transport creation arguments
33039903cd1cSChuck Lever  *
33049903cd1cSChuck Lever  */
3305483066d6SAdrian Bunk static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args)
3306a246b010SChuck Lever {
33078f9d5b1aSChuck Lever 	struct sockaddr *addr = args->dstaddr;
3308c8541ecdSChuck Lever 	struct rpc_xprt *xprt;
3309c8475461SChuck Lever 	struct sock_xprt *transport;
33100a68b0beSJ. Bruce Fields 	struct rpc_xprt *ret;
3311b7993cebSTrond Myklebust 	unsigned int max_slot_table_size = xprt_max_tcp_slot_table_entries;
3312b7993cebSTrond Myklebust 
3313b7993cebSTrond Myklebust 	if (args->flags & XPRT_CREATE_INFINITE_SLOTS)
3314b7993cebSTrond Myklebust 		max_slot_table_size = RPC_MAX_SLOT_TABLE_LIMIT;
3315a246b010SChuck Lever 
3316d9ba131dSTrond Myklebust 	xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
3317b7993cebSTrond Myklebust 			max_slot_table_size);
3318c8541ecdSChuck Lever 	if (IS_ERR(xprt))
3319c8541ecdSChuck Lever 		return xprt;
3320c8475461SChuck Lever 	transport = container_of(xprt, struct sock_xprt, xprt);
3321a246b010SChuck Lever 
3322ec739ef0SChuck Lever 	xprt->prot = IPPROTO_TCP;
3323d3abc739SOlga Kornievskaia 	xprt->xprt_class = &xs_tcp_transport;
3324808012fbSChuck Lever 	xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
3325a246b010SChuck Lever 
332603bf4b70SChuck Lever 	xprt->bind_timeout = XS_BIND_TO;
332703bf4b70SChuck Lever 	xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
332803bf4b70SChuck Lever 	xprt->idle_timeout = XS_IDLE_DISC_TO;
3329a246b010SChuck Lever 
3330262965f5SChuck Lever 	xprt->ops = &xs_tcp_ops;
3331ba7392bbSTrond Myklebust 	xprt->timeout = &xs_tcp_default_timeout;
3332a246b010SChuck Lever 
33333851f1cdSTrond Myklebust 	xprt->max_reconnect_timeout = xprt->timeout->to_maxval;
33347196dbb0STrond Myklebust 	xprt->connect_timeout = xprt->timeout->to_initval *
33357196dbb0STrond Myklebust 		(xprt->timeout->to_retries + 1);
33363851f1cdSTrond Myklebust 
3337c50b8ee0STrond Myklebust 	INIT_WORK(&transport->recv_worker, xs_stream_data_receive_workfn);
33384f8943f8STrond Myklebust 	INIT_WORK(&transport->error_worker, xs_error_handle);
3339edc1b01cSTrond Myklebust 	INIT_DELAYED_WORK(&transport->connect_worker, xs_tcp_setup_socket);
3340edc1b01cSTrond Myklebust 
33418f9d5b1aSChuck Lever 	switch (addr->sa_family) {
33428f9d5b1aSChuck Lever 	case AF_INET:
33438f9d5b1aSChuck Lever 		if (((struct sockaddr_in *)addr)->sin_port != htons(0))
33448f9d5b1aSChuck Lever 			xprt_set_bound(xprt);
33458f9d5b1aSChuck Lever 
33469dc3b095SChuck Lever 		xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP);
33478f9d5b1aSChuck Lever 		break;
33488f9d5b1aSChuck Lever 	case AF_INET6:
33498f9d5b1aSChuck Lever 		if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
33508f9d5b1aSChuck Lever 			xprt_set_bound(xprt);
33518f9d5b1aSChuck Lever 
33529dc3b095SChuck Lever 		xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6);
33538f9d5b1aSChuck Lever 		break;
33548f9d5b1aSChuck Lever 	default:
33550a68b0beSJ. Bruce Fields 		ret = ERR_PTR(-EAFNOSUPPORT);
33560a68b0beSJ. Bruce Fields 		goto out_err;
33578f9d5b1aSChuck Lever 	}
33588f9d5b1aSChuck Lever 
3359c740eff8SChuck Lever 	if (xprt_bound(xprt))
3360c740eff8SChuck Lever 		dprintk("RPC:       set up xprt to %s (port %s) via %s\n",
3361c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_ADDR],
3362c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_PORT],
3363c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_PROTO]);
3364c740eff8SChuck Lever 	else
3365c740eff8SChuck Lever 		dprintk("RPC:       set up xprt to %s (autobind) via %s\n",
3366c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_ADDR],
3367c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_PROTO]);
3368c740eff8SChuck Lever 
3369bc25571eS\"Talpey, Thomas\ 	if (try_module_get(THIS_MODULE))
3370c8541ecdSChuck Lever 		return xprt;
33710a68b0beSJ. Bruce Fields 	ret = ERR_PTR(-EINVAL);
33720a68b0beSJ. Bruce Fields out_err:
3373315f3812SKinglong Mee 	xs_xprt_free(xprt);
33740a68b0beSJ. Bruce Fields 	return ret;
3375a246b010SChuck Lever }
3376282b32e1SChuck Lever 
3377f300babaSAlexandros Batsakis /**
337875eb6af7SChuck Lever  * xs_setup_tcp_tls - Set up transport to use a TCP with TLS
337975eb6af7SChuck Lever  * @args: rpc transport creation arguments
338075eb6af7SChuck Lever  *
338175eb6af7SChuck Lever  */
338275eb6af7SChuck Lever static struct rpc_xprt *xs_setup_tcp_tls(struct xprt_create *args)
338375eb6af7SChuck Lever {
338475eb6af7SChuck Lever 	struct sockaddr *addr = args->dstaddr;
338575eb6af7SChuck Lever 	struct rpc_xprt *xprt;
338675eb6af7SChuck Lever 	struct sock_xprt *transport;
338775eb6af7SChuck Lever 	struct rpc_xprt *ret;
338875eb6af7SChuck Lever 	unsigned int max_slot_table_size = xprt_max_tcp_slot_table_entries;
338975eb6af7SChuck Lever 
339075eb6af7SChuck Lever 	if (args->flags & XPRT_CREATE_INFINITE_SLOTS)
339175eb6af7SChuck Lever 		max_slot_table_size = RPC_MAX_SLOT_TABLE_LIMIT;
339275eb6af7SChuck Lever 
339375eb6af7SChuck Lever 	xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
339475eb6af7SChuck Lever 			     max_slot_table_size);
339575eb6af7SChuck Lever 	if (IS_ERR(xprt))
339675eb6af7SChuck Lever 		return xprt;
339775eb6af7SChuck Lever 	transport = container_of(xprt, struct sock_xprt, xprt);
339875eb6af7SChuck Lever 
339975eb6af7SChuck Lever 	xprt->prot = IPPROTO_TCP;
340075eb6af7SChuck Lever 	xprt->xprt_class = &xs_tcp_transport;
340175eb6af7SChuck Lever 	xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
340275eb6af7SChuck Lever 
340375eb6af7SChuck Lever 	xprt->bind_timeout = XS_BIND_TO;
340475eb6af7SChuck Lever 	xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
340575eb6af7SChuck Lever 	xprt->idle_timeout = XS_IDLE_DISC_TO;
340675eb6af7SChuck Lever 
340775eb6af7SChuck Lever 	xprt->ops = &xs_tcp_ops;
340875eb6af7SChuck Lever 	xprt->timeout = &xs_tcp_default_timeout;
340975eb6af7SChuck Lever 
341075eb6af7SChuck Lever 	xprt->max_reconnect_timeout = xprt->timeout->to_maxval;
341175eb6af7SChuck Lever 	xprt->connect_timeout = xprt->timeout->to_initval *
341275eb6af7SChuck Lever 		(xprt->timeout->to_retries + 1);
341375eb6af7SChuck Lever 
341475eb6af7SChuck Lever 	INIT_WORK(&transport->recv_worker, xs_stream_data_receive_workfn);
341575eb6af7SChuck Lever 	INIT_WORK(&transport->error_worker, xs_error_handle);
341675eb6af7SChuck Lever 
341775eb6af7SChuck Lever 	switch (args->xprtsec.policy) {
341875eb6af7SChuck Lever 	case RPC_XPRTSEC_TLS_ANON:
341975eb6af7SChuck Lever 	case RPC_XPRTSEC_TLS_X509:
342075eb6af7SChuck Lever 		xprt->xprtsec = args->xprtsec;
342175eb6af7SChuck Lever 		INIT_DELAYED_WORK(&transport->connect_worker,
342275eb6af7SChuck Lever 				  xs_tcp_tls_setup_socket);
342375eb6af7SChuck Lever 		break;
342475eb6af7SChuck Lever 	default:
342575eb6af7SChuck Lever 		ret = ERR_PTR(-EACCES);
342675eb6af7SChuck Lever 		goto out_err;
342775eb6af7SChuck Lever 	}
342875eb6af7SChuck Lever 
342975eb6af7SChuck Lever 	switch (addr->sa_family) {
343075eb6af7SChuck Lever 	case AF_INET:
343175eb6af7SChuck Lever 		if (((struct sockaddr_in *)addr)->sin_port != htons(0))
343275eb6af7SChuck Lever 			xprt_set_bound(xprt);
343375eb6af7SChuck Lever 
343475eb6af7SChuck Lever 		xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP);
343575eb6af7SChuck Lever 		break;
343675eb6af7SChuck Lever 	case AF_INET6:
343775eb6af7SChuck Lever 		if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
343875eb6af7SChuck Lever 			xprt_set_bound(xprt);
343975eb6af7SChuck Lever 
344075eb6af7SChuck Lever 		xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6);
344175eb6af7SChuck Lever 		break;
344275eb6af7SChuck Lever 	default:
344375eb6af7SChuck Lever 		ret = ERR_PTR(-EAFNOSUPPORT);
344475eb6af7SChuck Lever 		goto out_err;
344575eb6af7SChuck Lever 	}
344675eb6af7SChuck Lever 
344775eb6af7SChuck Lever 	if (xprt_bound(xprt))
344875eb6af7SChuck Lever 		dprintk("RPC:       set up xprt to %s (port %s) via %s\n",
344975eb6af7SChuck Lever 			xprt->address_strings[RPC_DISPLAY_ADDR],
345075eb6af7SChuck Lever 			xprt->address_strings[RPC_DISPLAY_PORT],
345175eb6af7SChuck Lever 			xprt->address_strings[RPC_DISPLAY_PROTO]);
345275eb6af7SChuck Lever 	else
345375eb6af7SChuck Lever 		dprintk("RPC:       set up xprt to %s (autobind) via %s\n",
345475eb6af7SChuck Lever 			xprt->address_strings[RPC_DISPLAY_ADDR],
345575eb6af7SChuck Lever 			xprt->address_strings[RPC_DISPLAY_PROTO]);
345675eb6af7SChuck Lever 
345775eb6af7SChuck Lever 	if (try_module_get(THIS_MODULE))
345875eb6af7SChuck Lever 		return xprt;
345975eb6af7SChuck Lever 	ret = ERR_PTR(-EINVAL);
346075eb6af7SChuck Lever out_err:
346175eb6af7SChuck Lever 	xs_xprt_free(xprt);
346275eb6af7SChuck Lever 	return ret;
346375eb6af7SChuck Lever }
346475eb6af7SChuck Lever 
346575eb6af7SChuck Lever /**
3466f300babaSAlexandros Batsakis  * xs_setup_bc_tcp - Set up transport to use a TCP backchannel socket
3467f300babaSAlexandros Batsakis  * @args: rpc transport creation arguments
3468f300babaSAlexandros Batsakis  *
3469f300babaSAlexandros Batsakis  */
3470f300babaSAlexandros Batsakis static struct rpc_xprt *xs_setup_bc_tcp(struct xprt_create *args)
3471f300babaSAlexandros Batsakis {
3472f300babaSAlexandros Batsakis 	struct sockaddr *addr = args->dstaddr;
3473f300babaSAlexandros Batsakis 	struct rpc_xprt *xprt;
3474f300babaSAlexandros Batsakis 	struct sock_xprt *transport;
3475f300babaSAlexandros Batsakis 	struct svc_sock *bc_sock;
34760a68b0beSJ. Bruce Fields 	struct rpc_xprt *ret;
3477f300babaSAlexandros Batsakis 
3478d9ba131dSTrond Myklebust 	xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
3479d9ba131dSTrond Myklebust 			xprt_tcp_slot_table_entries);
3480f300babaSAlexandros Batsakis 	if (IS_ERR(xprt))
3481f300babaSAlexandros Batsakis 		return xprt;
3482f300babaSAlexandros Batsakis 	transport = container_of(xprt, struct sock_xprt, xprt);
3483f300babaSAlexandros Batsakis 
3484f300babaSAlexandros Batsakis 	xprt->prot = IPPROTO_TCP;
3485d3abc739SOlga Kornievskaia 	xprt->xprt_class = &xs_bc_tcp_transport;
3486f300babaSAlexandros Batsakis 	xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
3487f300babaSAlexandros Batsakis 	xprt->timeout = &xs_tcp_default_timeout;
3488f300babaSAlexandros Batsakis 
3489f300babaSAlexandros Batsakis 	/* backchannel */
3490f300babaSAlexandros Batsakis 	xprt_set_bound(xprt);
3491f300babaSAlexandros Batsakis 	xprt->bind_timeout = 0;
3492f300babaSAlexandros Batsakis 	xprt->reestablish_timeout = 0;
3493f300babaSAlexandros Batsakis 	xprt->idle_timeout = 0;
3494f300babaSAlexandros Batsakis 
3495f300babaSAlexandros Batsakis 	xprt->ops = &bc_tcp_ops;
3496f300babaSAlexandros Batsakis 
3497f300babaSAlexandros Batsakis 	switch (addr->sa_family) {
3498f300babaSAlexandros Batsakis 	case AF_INET:
3499f300babaSAlexandros Batsakis 		xs_format_peer_addresses(xprt, "tcp",
3500f300babaSAlexandros Batsakis 					 RPCBIND_NETID_TCP);
3501f300babaSAlexandros Batsakis 		break;
3502f300babaSAlexandros Batsakis 	case AF_INET6:
3503f300babaSAlexandros Batsakis 		xs_format_peer_addresses(xprt, "tcp",
3504f300babaSAlexandros Batsakis 				   RPCBIND_NETID_TCP6);
3505f300babaSAlexandros Batsakis 		break;
3506f300babaSAlexandros Batsakis 	default:
35070a68b0beSJ. Bruce Fields 		ret = ERR_PTR(-EAFNOSUPPORT);
35080a68b0beSJ. Bruce Fields 		goto out_err;
3509f300babaSAlexandros Batsakis 	}
3510f300babaSAlexandros Batsakis 
3511f300babaSAlexandros Batsakis 	dprintk("RPC:       set up xprt to %s (port %s) via %s\n",
3512f300babaSAlexandros Batsakis 			xprt->address_strings[RPC_DISPLAY_ADDR],
3513f300babaSAlexandros Batsakis 			xprt->address_strings[RPC_DISPLAY_PORT],
3514f300babaSAlexandros Batsakis 			xprt->address_strings[RPC_DISPLAY_PROTO]);
3515f300babaSAlexandros Batsakis 
3516f300babaSAlexandros Batsakis 	/*
351799de8ea9SJ. Bruce Fields 	 * Once we've associated a backchannel xprt with a connection,
351828303ca3SWeng Meiling 	 * we want to keep it around as long as the connection lasts,
351928303ca3SWeng Meiling 	 * in case we need to start using it for a backchannel again;
352028303ca3SWeng Meiling 	 * this reference won't be dropped until bc_xprt is destroyed.
352199de8ea9SJ. Bruce Fields 	 */
352299de8ea9SJ. Bruce Fields 	xprt_get(xprt);
352399de8ea9SJ. Bruce Fields 	args->bc_xprt->xpt_bc_xprt = xprt;
352499de8ea9SJ. Bruce Fields 	xprt->bc_xprt = args->bc_xprt;
352599de8ea9SJ. Bruce Fields 	bc_sock = container_of(args->bc_xprt, struct svc_sock, sk_xprt);
352699de8ea9SJ. Bruce Fields 	transport->sock = bc_sock->sk_sock;
352799de8ea9SJ. Bruce Fields 	transport->inet = bc_sock->sk_sk;
352899de8ea9SJ. Bruce Fields 
352999de8ea9SJ. Bruce Fields 	/*
3530f300babaSAlexandros Batsakis 	 * Since we don't want connections for the backchannel, we set
3531f300babaSAlexandros Batsakis 	 * the xprt status to connected
3532f300babaSAlexandros Batsakis 	 */
3533f300babaSAlexandros Batsakis 	xprt_set_connected(xprt);
3534f300babaSAlexandros Batsakis 
3535f300babaSAlexandros Batsakis 	if (try_module_get(THIS_MODULE))
3536f300babaSAlexandros Batsakis 		return xprt;
3537642aab58SKinglong Mee 
3538642aab58SKinglong Mee 	args->bc_xprt->xpt_bc_xprt = NULL;
353939a9beabSJ. Bruce Fields 	args->bc_xprt->xpt_bc_xps = NULL;
354099de8ea9SJ. Bruce Fields 	xprt_put(xprt);
35410a68b0beSJ. Bruce Fields 	ret = ERR_PTR(-EINVAL);
35420a68b0beSJ. Bruce Fields out_err:
3543315f3812SKinglong Mee 	xs_xprt_free(xprt);
35440a68b0beSJ. Bruce Fields 	return ret;
3545f300babaSAlexandros Batsakis }
3546f300babaSAlexandros Batsakis 
3547176e21eeSChuck Lever static struct xprt_class	xs_local_transport = {
3548176e21eeSChuck Lever 	.list		= LIST_HEAD_INIT(xs_local_transport.list),
3549176e21eeSChuck Lever 	.name		= "named UNIX socket",
3550176e21eeSChuck Lever 	.owner		= THIS_MODULE,
3551176e21eeSChuck Lever 	.ident		= XPRT_TRANSPORT_LOCAL,
3552176e21eeSChuck Lever 	.setup		= xs_setup_local,
3553d5aa6b22STrond Myklebust 	.netid		= { "" },
3554176e21eeSChuck Lever };
3555176e21eeSChuck Lever 
3556bc25571eS\"Talpey, Thomas\ static struct xprt_class	xs_udp_transport = {
3557bc25571eS\"Talpey, Thomas\ 	.list		= LIST_HEAD_INIT(xs_udp_transport.list),
3558bc25571eS\"Talpey, Thomas\ 	.name		= "udp",
3559bc25571eS\"Talpey, Thomas\ 	.owner		= THIS_MODULE,
3560f300babaSAlexandros Batsakis 	.ident		= XPRT_TRANSPORT_UDP,
3561bc25571eS\"Talpey, Thomas\ 	.setup		= xs_setup_udp,
3562d5aa6b22STrond Myklebust 	.netid		= { "udp", "udp6", "" },
3563bc25571eS\"Talpey, Thomas\ };
3564bc25571eS\"Talpey, Thomas\ 
3565bc25571eS\"Talpey, Thomas\ static struct xprt_class	xs_tcp_transport = {
3566bc25571eS\"Talpey, Thomas\ 	.list		= LIST_HEAD_INIT(xs_tcp_transport.list),
3567bc25571eS\"Talpey, Thomas\ 	.name		= "tcp",
3568bc25571eS\"Talpey, Thomas\ 	.owner		= THIS_MODULE,
3569f300babaSAlexandros Batsakis 	.ident		= XPRT_TRANSPORT_TCP,
3570bc25571eS\"Talpey, Thomas\ 	.setup		= xs_setup_tcp,
3571d5aa6b22STrond Myklebust 	.netid		= { "tcp", "tcp6", "" },
3572bc25571eS\"Talpey, Thomas\ };
3573bc25571eS\"Talpey, Thomas\ 
357475eb6af7SChuck Lever static struct xprt_class	xs_tcp_tls_transport = {
357575eb6af7SChuck Lever 	.list		= LIST_HEAD_INIT(xs_tcp_tls_transport.list),
357675eb6af7SChuck Lever 	.name		= "tcp-with-tls",
357775eb6af7SChuck Lever 	.owner		= THIS_MODULE,
357875eb6af7SChuck Lever 	.ident		= XPRT_TRANSPORT_TCP_TLS,
357975eb6af7SChuck Lever 	.setup		= xs_setup_tcp_tls,
358075eb6af7SChuck Lever 	.netid		= { "tcp", "tcp6", "" },
358175eb6af7SChuck Lever };
358275eb6af7SChuck Lever 
3583f300babaSAlexandros Batsakis static struct xprt_class	xs_bc_tcp_transport = {
3584f300babaSAlexandros Batsakis 	.list		= LIST_HEAD_INIT(xs_bc_tcp_transport.list),
3585f300babaSAlexandros Batsakis 	.name		= "tcp NFSv4.1 backchannel",
3586f300babaSAlexandros Batsakis 	.owner		= THIS_MODULE,
3587f300babaSAlexandros Batsakis 	.ident		= XPRT_TRANSPORT_BC_TCP,
3588f300babaSAlexandros Batsakis 	.setup		= xs_setup_bc_tcp,
3589d5aa6b22STrond Myklebust 	.netid		= { "" },
3590f300babaSAlexandros Batsakis };
3591f300babaSAlexandros Batsakis 
3592282b32e1SChuck Lever /**
3593bc25571eS\"Talpey, Thomas\  * init_socket_xprt - set up xprtsock's sysctls, register with RPC client
3594282b32e1SChuck Lever  *
3595282b32e1SChuck Lever  */
3596282b32e1SChuck Lever int init_socket_xprt(void)
3597282b32e1SChuck Lever {
35982b1bec5fSEric W. Biederman 	if (!sunrpc_table_header)
3599c946cb69SLuis Chamberlain 		sunrpc_table_header = register_sysctl("sunrpc", xs_tunables_table);
3600fbf76683SChuck Lever 
3601176e21eeSChuck Lever 	xprt_register_transport(&xs_local_transport);
3602bc25571eS\"Talpey, Thomas\ 	xprt_register_transport(&xs_udp_transport);
3603bc25571eS\"Talpey, Thomas\ 	xprt_register_transport(&xs_tcp_transport);
360475eb6af7SChuck Lever 	xprt_register_transport(&xs_tcp_tls_transport);
3605f300babaSAlexandros Batsakis 	xprt_register_transport(&xs_bc_tcp_transport);
3606bc25571eS\"Talpey, Thomas\ 
3607282b32e1SChuck Lever 	return 0;
3608282b32e1SChuck Lever }
3609282b32e1SChuck Lever 
3610282b32e1SChuck Lever /**
3611bc25571eS\"Talpey, Thomas\  * cleanup_socket_xprt - remove xprtsock's sysctls, unregister
3612282b32e1SChuck Lever  *
3613282b32e1SChuck Lever  */
3614282b32e1SChuck Lever void cleanup_socket_xprt(void)
3615282b32e1SChuck Lever {
3616fbf76683SChuck Lever 	if (sunrpc_table_header) {
3617fbf76683SChuck Lever 		unregister_sysctl_table(sunrpc_table_header);
3618fbf76683SChuck Lever 		sunrpc_table_header = NULL;
3619fbf76683SChuck Lever 	}
3620bc25571eS\"Talpey, Thomas\ 
3621176e21eeSChuck Lever 	xprt_unregister_transport(&xs_local_transport);
3622bc25571eS\"Talpey, Thomas\ 	xprt_unregister_transport(&xs_udp_transport);
3623bc25571eS\"Talpey, Thomas\ 	xprt_unregister_transport(&xs_tcp_transport);
362475eb6af7SChuck Lever 	xprt_unregister_transport(&xs_tcp_tls_transport);
3625f300babaSAlexandros Batsakis 	xprt_unregister_transport(&xs_bc_tcp_transport);
3626282b32e1SChuck Lever }
3627cbf11071STrond Myklebust 
36289bbb9e5aSRusty Russell static int param_set_portnr(const char *val, const struct kernel_param *kp)
3629cbf11071STrond Myklebust {
3630cbf11071STrond Myklebust 	return param_set_uint_minmax(val, kp,
3631cbf11071STrond Myklebust 			RPC_MIN_RESVPORT,
3632cbf11071STrond Myklebust 			RPC_MAX_RESVPORT);
3633cbf11071STrond Myklebust }
3634cbf11071STrond Myklebust 
36359c27847dSLuis R. Rodriguez static const struct kernel_param_ops param_ops_portnr = {
36369bbb9e5aSRusty Russell 	.set = param_set_portnr,
36379bbb9e5aSRusty Russell 	.get = param_get_uint,
36389bbb9e5aSRusty Russell };
36399bbb9e5aSRusty Russell 
3640cbf11071STrond Myklebust #define param_check_portnr(name, p) \
3641cbf11071STrond Myklebust 	__param_check(name, p, unsigned int);
3642cbf11071STrond Myklebust 
3643cbf11071STrond Myklebust module_param_named(min_resvport, xprt_min_resvport, portnr, 0644);
3644cbf11071STrond Myklebust module_param_named(max_resvport, xprt_max_resvport, portnr, 0644);
3645cbf11071STrond Myklebust 
36469bbb9e5aSRusty Russell static int param_set_slot_table_size(const char *val,
36479bbb9e5aSRusty Russell 				     const struct kernel_param *kp)
3648cbf11071STrond Myklebust {
3649cbf11071STrond Myklebust 	return param_set_uint_minmax(val, kp,
3650cbf11071STrond Myklebust 			RPC_MIN_SLOT_TABLE,
3651cbf11071STrond Myklebust 			RPC_MAX_SLOT_TABLE);
3652cbf11071STrond Myklebust }
3653cbf11071STrond Myklebust 
36549c27847dSLuis R. Rodriguez static const struct kernel_param_ops param_ops_slot_table_size = {
36559bbb9e5aSRusty Russell 	.set = param_set_slot_table_size,
36569bbb9e5aSRusty Russell 	.get = param_get_uint,
36579bbb9e5aSRusty Russell };
36589bbb9e5aSRusty Russell 
3659cbf11071STrond Myklebust #define param_check_slot_table_size(name, p) \
3660cbf11071STrond Myklebust 	__param_check(name, p, unsigned int);
3661cbf11071STrond Myklebust 
3662d9ba131dSTrond Myklebust static int param_set_max_slot_table_size(const char *val,
3663d9ba131dSTrond Myklebust 				     const struct kernel_param *kp)
3664d9ba131dSTrond Myklebust {
3665d9ba131dSTrond Myklebust 	return param_set_uint_minmax(val, kp,
3666d9ba131dSTrond Myklebust 			RPC_MIN_SLOT_TABLE,
3667d9ba131dSTrond Myklebust 			RPC_MAX_SLOT_TABLE_LIMIT);
3668d9ba131dSTrond Myklebust }
3669d9ba131dSTrond Myklebust 
36709c27847dSLuis R. Rodriguez static const struct kernel_param_ops param_ops_max_slot_table_size = {
3671d9ba131dSTrond Myklebust 	.set = param_set_max_slot_table_size,
3672d9ba131dSTrond Myklebust 	.get = param_get_uint,
3673d9ba131dSTrond Myklebust };
3674d9ba131dSTrond Myklebust 
3675d9ba131dSTrond Myklebust #define param_check_max_slot_table_size(name, p) \
3676d9ba131dSTrond Myklebust 	__param_check(name, p, unsigned int);
3677d9ba131dSTrond Myklebust 
3678cbf11071STrond Myklebust module_param_named(tcp_slot_table_entries, xprt_tcp_slot_table_entries,
3679cbf11071STrond Myklebust 		   slot_table_size, 0644);
3680d9ba131dSTrond Myklebust module_param_named(tcp_max_slot_table_entries, xprt_max_tcp_slot_table_entries,
3681d9ba131dSTrond Myklebust 		   max_slot_table_size, 0644);
3682cbf11071STrond Myklebust module_param_named(udp_slot_table_entries, xprt_udp_slot_table_entries,
3683cbf11071STrond Myklebust 		   slot_table_size, 0644);
3684