xref: /openbmc/linux/net/sunrpc/xprtsock.c (revision 277e4ab7d530bf287e02b65cfcd3ea8f489784f6)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2a246b010SChuck Lever /*
3a246b010SChuck Lever  * linux/net/sunrpc/xprtsock.c
4a246b010SChuck Lever  *
5a246b010SChuck Lever  * Client-side transport implementation for sockets.
6a246b010SChuck Lever  *
7113aa838SAlan Cox  * TCP callback races fixes (C) 1998 Red Hat
8113aa838SAlan Cox  * TCP send fixes (C) 1998 Red Hat
9a246b010SChuck Lever  * TCP NFS related read + write fixes
10a246b010SChuck Lever  *  (C) 1999 Dave Airlie, University of Limerick, Ireland <airlied@linux.ie>
11a246b010SChuck Lever  *
12a246b010SChuck Lever  * Rewrite of larges part of the code in order to stabilize TCP stuff.
13a246b010SChuck Lever  * Fix behaviour when socket buffer is full.
14a246b010SChuck Lever  *  (C) 1999 Trond Myklebust <trond.myklebust@fys.uio.no>
1555aa4f58SChuck Lever  *
1655aa4f58SChuck Lever  * IP socket transport implementation, (C) 2005 Chuck Lever <cel@netapp.com>
178f9d5b1aSChuck Lever  *
188f9d5b1aSChuck Lever  * IPv6 support contributed by Gilles Quillard, Bull Open Source, 2005.
198f9d5b1aSChuck Lever  *   <gilles.quillard@bull.net>
20a246b010SChuck Lever  */
21a246b010SChuck Lever 
22a246b010SChuck Lever #include <linux/types.h>
23176e21eeSChuck Lever #include <linux/string.h>
24a246b010SChuck Lever #include <linux/slab.h>
25bc25571eS\"Talpey, Thomas\ #include <linux/module.h>
26a246b010SChuck Lever #include <linux/capability.h>
27a246b010SChuck Lever #include <linux/pagemap.h>
28a246b010SChuck Lever #include <linux/errno.h>
29a246b010SChuck Lever #include <linux/socket.h>
30a246b010SChuck Lever #include <linux/in.h>
31a246b010SChuck Lever #include <linux/net.h>
32a246b010SChuck Lever #include <linux/mm.h>
33176e21eeSChuck Lever #include <linux/un.h>
34a246b010SChuck Lever #include <linux/udp.h>
35a246b010SChuck Lever #include <linux/tcp.h>
36a246b010SChuck Lever #include <linux/sunrpc/clnt.h>
375976687aSJeff Layton #include <linux/sunrpc/addr.h>
3802107148SChuck Lever #include <linux/sunrpc/sched.h>
394cfc7e60SRahul Iyer #include <linux/sunrpc/svcsock.h>
4049c36fccS\"Talpey, Thomas\ #include <linux/sunrpc/xprtsock.h>
41a246b010SChuck Lever #include <linux/file.h>
429e00abc3STrond Myklebust #ifdef CONFIG_SUNRPC_BACKCHANNEL
4344b98efdSRicardo Labiaga #include <linux/sunrpc/bc_xprt.h>
4444b98efdSRicardo Labiaga #endif
45a246b010SChuck Lever 
46a246b010SChuck Lever #include <net/sock.h>
47a246b010SChuck Lever #include <net/checksum.h>
48a246b010SChuck Lever #include <net/udp.h>
49a246b010SChuck Lever #include <net/tcp.h>
50*277e4ab7STrond Myklebust #include <linux/bvec.h>
51*277e4ab7STrond Myklebust #include <linux/uio.h>
52a246b010SChuck Lever 
5340b5ea0cSTrond Myklebust #include <trace/events/sunrpc.h>
5440b5ea0cSTrond Myklebust 
554cfc7e60SRahul Iyer #include "sunrpc.h"
56176e21eeSChuck Lever 
57176e21eeSChuck Lever static void xs_close(struct rpc_xprt *xprt);
587196dbb0STrond Myklebust static void xs_tcp_set_socket_timeouts(struct rpc_xprt *xprt,
597196dbb0STrond Myklebust 		struct socket *sock);
60176e21eeSChuck Lever 
619903cd1cSChuck Lever /*
62c556b754SChuck Lever  * xprtsock tunables
63c556b754SChuck Lever  */
6409acfea5STrond Myklebust static unsigned int xprt_udp_slot_table_entries = RPC_DEF_SLOT_TABLE;
6509acfea5STrond Myklebust static unsigned int xprt_tcp_slot_table_entries = RPC_MIN_SLOT_TABLE;
6609acfea5STrond Myklebust static unsigned int xprt_max_tcp_slot_table_entries = RPC_MAX_SLOT_TABLE;
67c556b754SChuck Lever 
6809acfea5STrond Myklebust static unsigned int xprt_min_resvport = RPC_DEF_MIN_RESVPORT;
6909acfea5STrond Myklebust static unsigned int xprt_max_resvport = RPC_DEF_MAX_RESVPORT;
70c556b754SChuck Lever 
7154c09874STrond Myklebust #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
7254c09874STrond Myklebust 
737d1e8255STrond Myklebust #define XS_TCP_LINGER_TO	(15U * HZ)
7425fe6142STrond Myklebust static unsigned int xs_tcp_fin_timeout __read_mostly = XS_TCP_LINGER_TO;
757d1e8255STrond Myklebust 
76c556b754SChuck Lever /*
77fbf76683SChuck Lever  * We can register our own files under /proc/sys/sunrpc by
78fbf76683SChuck Lever  * calling register_sysctl_table() again.  The files in that
79fbf76683SChuck Lever  * directory become the union of all files registered there.
80fbf76683SChuck Lever  *
81fbf76683SChuck Lever  * We simply need to make sure that we don't collide with
82fbf76683SChuck Lever  * someone else's file names!
83fbf76683SChuck Lever  */
84fbf76683SChuck Lever 
85fbf76683SChuck Lever static unsigned int min_slot_table_size = RPC_MIN_SLOT_TABLE;
86fbf76683SChuck Lever static unsigned int max_slot_table_size = RPC_MAX_SLOT_TABLE;
87d9ba131dSTrond Myklebust static unsigned int max_tcp_slot_table_limit = RPC_MAX_SLOT_TABLE_LIMIT;
88fbf76683SChuck Lever static unsigned int xprt_min_resvport_limit = RPC_MIN_RESVPORT;
89fbf76683SChuck Lever static unsigned int xprt_max_resvport_limit = RPC_MAX_RESVPORT;
90fbf76683SChuck Lever 
91fbf76683SChuck Lever static struct ctl_table_header *sunrpc_table_header;
92fbf76683SChuck Lever 
93fbf76683SChuck Lever /*
94fbf76683SChuck Lever  * FIXME: changing the UDP slot table size should also resize the UDP
95fbf76683SChuck Lever  *        socket buffers for existing UDP transports
96fbf76683SChuck Lever  */
97fe2c6338SJoe Perches static struct ctl_table xs_tunables_table[] = {
98fbf76683SChuck Lever 	{
99fbf76683SChuck Lever 		.procname	= "udp_slot_table_entries",
100fbf76683SChuck Lever 		.data		= &xprt_udp_slot_table_entries,
101fbf76683SChuck Lever 		.maxlen		= sizeof(unsigned int),
102fbf76683SChuck Lever 		.mode		= 0644,
1036d456111SEric W. Biederman 		.proc_handler	= proc_dointvec_minmax,
104fbf76683SChuck Lever 		.extra1		= &min_slot_table_size,
105fbf76683SChuck Lever 		.extra2		= &max_slot_table_size
106fbf76683SChuck Lever 	},
107fbf76683SChuck Lever 	{
108fbf76683SChuck Lever 		.procname	= "tcp_slot_table_entries",
109fbf76683SChuck Lever 		.data		= &xprt_tcp_slot_table_entries,
110fbf76683SChuck Lever 		.maxlen		= sizeof(unsigned int),
111fbf76683SChuck Lever 		.mode		= 0644,
1126d456111SEric W. Biederman 		.proc_handler	= proc_dointvec_minmax,
113fbf76683SChuck Lever 		.extra1		= &min_slot_table_size,
114fbf76683SChuck Lever 		.extra2		= &max_slot_table_size
115fbf76683SChuck Lever 	},
116fbf76683SChuck Lever 	{
117d9ba131dSTrond Myklebust 		.procname	= "tcp_max_slot_table_entries",
118d9ba131dSTrond Myklebust 		.data		= &xprt_max_tcp_slot_table_entries,
119d9ba131dSTrond Myklebust 		.maxlen		= sizeof(unsigned int),
120d9ba131dSTrond Myklebust 		.mode		= 0644,
121d9ba131dSTrond Myklebust 		.proc_handler	= proc_dointvec_minmax,
122d9ba131dSTrond Myklebust 		.extra1		= &min_slot_table_size,
123d9ba131dSTrond Myklebust 		.extra2		= &max_tcp_slot_table_limit
124d9ba131dSTrond Myklebust 	},
125d9ba131dSTrond Myklebust 	{
126fbf76683SChuck Lever 		.procname	= "min_resvport",
127fbf76683SChuck Lever 		.data		= &xprt_min_resvport,
128fbf76683SChuck Lever 		.maxlen		= sizeof(unsigned int),
129fbf76683SChuck Lever 		.mode		= 0644,
1306d456111SEric W. Biederman 		.proc_handler	= proc_dointvec_minmax,
131fbf76683SChuck Lever 		.extra1		= &xprt_min_resvport_limit,
132e08ea3a9SFrank Sorenson 		.extra2		= &xprt_max_resvport
133fbf76683SChuck Lever 	},
134fbf76683SChuck Lever 	{
135fbf76683SChuck Lever 		.procname	= "max_resvport",
136fbf76683SChuck Lever 		.data		= &xprt_max_resvport,
137fbf76683SChuck Lever 		.maxlen		= sizeof(unsigned int),
138fbf76683SChuck Lever 		.mode		= 0644,
1396d456111SEric W. Biederman 		.proc_handler	= proc_dointvec_minmax,
140e08ea3a9SFrank Sorenson 		.extra1		= &xprt_min_resvport,
141fbf76683SChuck Lever 		.extra2		= &xprt_max_resvport_limit
142fbf76683SChuck Lever 	},
143fbf76683SChuck Lever 	{
14425fe6142STrond Myklebust 		.procname	= "tcp_fin_timeout",
14525fe6142STrond Myklebust 		.data		= &xs_tcp_fin_timeout,
14625fe6142STrond Myklebust 		.maxlen		= sizeof(xs_tcp_fin_timeout),
14725fe6142STrond Myklebust 		.mode		= 0644,
1486d456111SEric W. Biederman 		.proc_handler	= proc_dointvec_jiffies,
14925fe6142STrond Myklebust 	},
150f8572d8fSEric W. Biederman 	{ },
151fbf76683SChuck Lever };
152fbf76683SChuck Lever 
153fe2c6338SJoe Perches static struct ctl_table sunrpc_table[] = {
154fbf76683SChuck Lever 	{
155fbf76683SChuck Lever 		.procname	= "sunrpc",
156fbf76683SChuck Lever 		.mode		= 0555,
157fbf76683SChuck Lever 		.child		= xs_tunables_table
158fbf76683SChuck Lever 	},
159f8572d8fSEric W. Biederman 	{ },
160fbf76683SChuck Lever };
161fbf76683SChuck Lever 
162fbf76683SChuck Lever #endif
163fbf76683SChuck Lever 
164fbf76683SChuck Lever /*
16503bf4b70SChuck Lever  * Wait duration for a reply from the RPC portmapper.
16603bf4b70SChuck Lever  */
16703bf4b70SChuck Lever #define XS_BIND_TO		(60U * HZ)
16803bf4b70SChuck Lever 
16903bf4b70SChuck Lever /*
17003bf4b70SChuck Lever  * Delay if a UDP socket connect error occurs.  This is most likely some
17103bf4b70SChuck Lever  * kind of resource problem on the local host.
17203bf4b70SChuck Lever  */
17303bf4b70SChuck Lever #define XS_UDP_REEST_TO		(2U * HZ)
17403bf4b70SChuck Lever 
17503bf4b70SChuck Lever /*
17603bf4b70SChuck Lever  * The reestablish timeout allows clients to delay for a bit before attempting
17703bf4b70SChuck Lever  * to reconnect to a server that just dropped our connection.
17803bf4b70SChuck Lever  *
17903bf4b70SChuck Lever  * We implement an exponential backoff when trying to reestablish a TCP
18003bf4b70SChuck Lever  * transport connection with the server.  Some servers like to drop a TCP
18103bf4b70SChuck Lever  * connection when they are overworked, so we start with a short timeout and
18203bf4b70SChuck Lever  * increase over time if the server is down or not responding.
18303bf4b70SChuck Lever  */
18403bf4b70SChuck Lever #define XS_TCP_INIT_REEST_TO	(3U * HZ)
18503bf4b70SChuck Lever 
18603bf4b70SChuck Lever /*
18703bf4b70SChuck Lever  * TCP idle timeout; client drops the transport socket if it is idle
18803bf4b70SChuck Lever  * for this long.  Note that we also timeout UDP sockets to prevent
18903bf4b70SChuck Lever  * holding port numbers when there is no RPC traffic.
19003bf4b70SChuck Lever  */
19103bf4b70SChuck Lever #define XS_IDLE_DISC_TO		(5U * 60 * HZ)
19203bf4b70SChuck Lever 
193f895b252SJeff Layton #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
194a246b010SChuck Lever # undef  RPC_DEBUG_DATA
1959903cd1cSChuck Lever # define RPCDBG_FACILITY	RPCDBG_TRANS
196a246b010SChuck Lever #endif
197a246b010SChuck Lever 
198a246b010SChuck Lever #ifdef RPC_DEBUG_DATA
1999903cd1cSChuck Lever static void xs_pktdump(char *msg, u32 *packet, unsigned int count)
200a246b010SChuck Lever {
201a246b010SChuck Lever 	u8 *buf = (u8 *) packet;
202a246b010SChuck Lever 	int j;
203a246b010SChuck Lever 
204a246b010SChuck Lever 	dprintk("RPC:       %s\n", msg);
205a246b010SChuck Lever 	for (j = 0; j < count && j < 128; j += 4) {
206a246b010SChuck Lever 		if (!(j & 31)) {
207a246b010SChuck Lever 			if (j)
208a246b010SChuck Lever 				dprintk("\n");
209a246b010SChuck Lever 			dprintk("0x%04x ", j);
210a246b010SChuck Lever 		}
211a246b010SChuck Lever 		dprintk("%02x%02x%02x%02x ",
212a246b010SChuck Lever 			buf[j], buf[j+1], buf[j+2], buf[j+3]);
213a246b010SChuck Lever 	}
214a246b010SChuck Lever 	dprintk("\n");
215a246b010SChuck Lever }
216a246b010SChuck Lever #else
2179903cd1cSChuck Lever static inline void xs_pktdump(char *msg, u32 *packet, unsigned int count)
218a246b010SChuck Lever {
219a246b010SChuck Lever 	/* NOP */
220a246b010SChuck Lever }
221a246b010SChuck Lever #endif
222a246b010SChuck Lever 
2232118071dSTrond Myklebust static inline struct rpc_xprt *xprt_from_sock(struct sock *sk)
2242118071dSTrond Myklebust {
2252118071dSTrond Myklebust 	return (struct rpc_xprt *) sk->sk_user_data;
2262118071dSTrond Myklebust }
2272118071dSTrond Myklebust 
22895392c59SChuck Lever static inline struct sockaddr *xs_addr(struct rpc_xprt *xprt)
229edb267a6SChuck Lever {
23095392c59SChuck Lever 	return (struct sockaddr *) &xprt->addr;
23195392c59SChuck Lever }
23295392c59SChuck Lever 
233176e21eeSChuck Lever static inline struct sockaddr_un *xs_addr_un(struct rpc_xprt *xprt)
234176e21eeSChuck Lever {
235176e21eeSChuck Lever 	return (struct sockaddr_un *) &xprt->addr;
236176e21eeSChuck Lever }
237176e21eeSChuck Lever 
23895392c59SChuck Lever static inline struct sockaddr_in *xs_addr_in(struct rpc_xprt *xprt)
23995392c59SChuck Lever {
24095392c59SChuck Lever 	return (struct sockaddr_in *) &xprt->addr;
24195392c59SChuck Lever }
24295392c59SChuck Lever 
24395392c59SChuck Lever static inline struct sockaddr_in6 *xs_addr_in6(struct rpc_xprt *xprt)
24495392c59SChuck Lever {
24595392c59SChuck Lever 	return (struct sockaddr_in6 *) &xprt->addr;
24695392c59SChuck Lever }
24795392c59SChuck Lever 
248c877b849SChuck Lever static void xs_format_common_peer_addresses(struct rpc_xprt *xprt)
249c877b849SChuck Lever {
250c877b849SChuck Lever 	struct sockaddr *sap = xs_addr(xprt);
2519dc3b095SChuck Lever 	struct sockaddr_in6 *sin6;
2529dc3b095SChuck Lever 	struct sockaddr_in *sin;
253176e21eeSChuck Lever 	struct sockaddr_un *sun;
254c877b849SChuck Lever 	char buf[128];
255c877b849SChuck Lever 
2569dc3b095SChuck Lever 	switch (sap->sa_family) {
257176e21eeSChuck Lever 	case AF_LOCAL:
258176e21eeSChuck Lever 		sun = xs_addr_un(xprt);
259176e21eeSChuck Lever 		strlcpy(buf, sun->sun_path, sizeof(buf));
260176e21eeSChuck Lever 		xprt->address_strings[RPC_DISPLAY_ADDR] =
261176e21eeSChuck Lever 						kstrdup(buf, GFP_KERNEL);
262176e21eeSChuck Lever 		break;
2639dc3b095SChuck Lever 	case AF_INET:
264176e21eeSChuck Lever 		(void)rpc_ntop(sap, buf, sizeof(buf));
265176e21eeSChuck Lever 		xprt->address_strings[RPC_DISPLAY_ADDR] =
266176e21eeSChuck Lever 						kstrdup(buf, GFP_KERNEL);
2679dc3b095SChuck Lever 		sin = xs_addr_in(xprt);
268fc0b5791SJoe Perches 		snprintf(buf, sizeof(buf), "%08x", ntohl(sin->sin_addr.s_addr));
2699dc3b095SChuck Lever 		break;
2709dc3b095SChuck Lever 	case AF_INET6:
271176e21eeSChuck Lever 		(void)rpc_ntop(sap, buf, sizeof(buf));
272176e21eeSChuck Lever 		xprt->address_strings[RPC_DISPLAY_ADDR] =
273176e21eeSChuck Lever 						kstrdup(buf, GFP_KERNEL);
2749dc3b095SChuck Lever 		sin6 = xs_addr_in6(xprt);
275fc0b5791SJoe Perches 		snprintf(buf, sizeof(buf), "%pi6", &sin6->sin6_addr);
2769dc3b095SChuck Lever 		break;
2779dc3b095SChuck Lever 	default:
2789dc3b095SChuck Lever 		BUG();
2799dc3b095SChuck Lever 	}
280176e21eeSChuck Lever 
2819dc3b095SChuck Lever 	xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = kstrdup(buf, GFP_KERNEL);
2829dc3b095SChuck Lever }
2839dc3b095SChuck Lever 
2849dc3b095SChuck Lever static void xs_format_common_peer_ports(struct rpc_xprt *xprt)
2859dc3b095SChuck Lever {
2869dc3b095SChuck Lever 	struct sockaddr *sap = xs_addr(xprt);
2879dc3b095SChuck Lever 	char buf[128];
2889dc3b095SChuck Lever 
28981160e66SJoe Perches 	snprintf(buf, sizeof(buf), "%u", rpc_get_port(sap));
290c877b849SChuck Lever 	xprt->address_strings[RPC_DISPLAY_PORT] = kstrdup(buf, GFP_KERNEL);
291c877b849SChuck Lever 
29281160e66SJoe Perches 	snprintf(buf, sizeof(buf), "%4hx", rpc_get_port(sap));
293c877b849SChuck Lever 	xprt->address_strings[RPC_DISPLAY_HEX_PORT] = kstrdup(buf, GFP_KERNEL);
294c877b849SChuck Lever }
295c877b849SChuck Lever 
2969dc3b095SChuck Lever static void xs_format_peer_addresses(struct rpc_xprt *xprt,
297b454ae90SChuck Lever 				     const char *protocol,
298b454ae90SChuck Lever 				     const char *netid)
299edb267a6SChuck Lever {
300b454ae90SChuck Lever 	xprt->address_strings[RPC_DISPLAY_PROTO] = protocol;
301b454ae90SChuck Lever 	xprt->address_strings[RPC_DISPLAY_NETID] = netid;
302c877b849SChuck Lever 	xs_format_common_peer_addresses(xprt);
3039dc3b095SChuck Lever 	xs_format_common_peer_ports(xprt);
304edb267a6SChuck Lever }
305edb267a6SChuck Lever 
3069dc3b095SChuck Lever static void xs_update_peer_port(struct rpc_xprt *xprt)
3074b6473fbSChuck Lever {
3089dc3b095SChuck Lever 	kfree(xprt->address_strings[RPC_DISPLAY_HEX_PORT]);
3099dc3b095SChuck Lever 	kfree(xprt->address_strings[RPC_DISPLAY_PORT]);
3104b6473fbSChuck Lever 
3119dc3b095SChuck Lever 	xs_format_common_peer_ports(xprt);
312edb267a6SChuck Lever }
313edb267a6SChuck Lever 
314edb267a6SChuck Lever static void xs_free_peer_addresses(struct rpc_xprt *xprt)
315edb267a6SChuck Lever {
31633e01dc7SChuck Lever 	unsigned int i;
31733e01dc7SChuck Lever 
31833e01dc7SChuck Lever 	for (i = 0; i < RPC_DISPLAY_MAX; i++)
31933e01dc7SChuck Lever 		switch (i) {
32033e01dc7SChuck Lever 		case RPC_DISPLAY_PROTO:
32133e01dc7SChuck Lever 		case RPC_DISPLAY_NETID:
32233e01dc7SChuck Lever 			continue;
32333e01dc7SChuck Lever 		default:
32433e01dc7SChuck Lever 			kfree(xprt->address_strings[i]);
32533e01dc7SChuck Lever 		}
326edb267a6SChuck Lever }
327edb267a6SChuck Lever 
328*277e4ab7STrond Myklebust static size_t
329*277e4ab7STrond Myklebust xs_alloc_sparse_pages(struct xdr_buf *buf, size_t want, gfp_t gfp)
330*277e4ab7STrond Myklebust {
331*277e4ab7STrond Myklebust 	size_t i,n;
332*277e4ab7STrond Myklebust 
333*277e4ab7STrond Myklebust 	if (!(buf->flags & XDRBUF_SPARSE_PAGES))
334*277e4ab7STrond Myklebust 		return want;
335*277e4ab7STrond Myklebust 	if (want > buf->page_len)
336*277e4ab7STrond Myklebust 		want = buf->page_len;
337*277e4ab7STrond Myklebust 	n = (buf->page_base + want + PAGE_SIZE - 1) >> PAGE_SHIFT;
338*277e4ab7STrond Myklebust 	for (i = 0; i < n; i++) {
339*277e4ab7STrond Myklebust 		if (buf->pages[i])
340*277e4ab7STrond Myklebust 			continue;
341*277e4ab7STrond Myklebust 		buf->bvec[i].bv_page = buf->pages[i] = alloc_page(gfp);
342*277e4ab7STrond Myklebust 		if (!buf->pages[i]) {
343*277e4ab7STrond Myklebust 			buf->page_len = (i * PAGE_SIZE) - buf->page_base;
344*277e4ab7STrond Myklebust 			return buf->page_len;
345*277e4ab7STrond Myklebust 		}
346*277e4ab7STrond Myklebust 	}
347*277e4ab7STrond Myklebust 	return want;
348*277e4ab7STrond Myklebust }
349*277e4ab7STrond Myklebust 
350*277e4ab7STrond Myklebust static ssize_t
351*277e4ab7STrond Myklebust xs_sock_recvmsg(struct socket *sock, struct msghdr *msg, int flags, size_t seek)
352*277e4ab7STrond Myklebust {
353*277e4ab7STrond Myklebust 	ssize_t ret;
354*277e4ab7STrond Myklebust 	if (seek != 0)
355*277e4ab7STrond Myklebust 		iov_iter_advance(&msg->msg_iter, seek);
356*277e4ab7STrond Myklebust 	ret = sock_recvmsg(sock, msg, flags);
357*277e4ab7STrond Myklebust 	return ret > 0 ? ret + seek : ret;
358*277e4ab7STrond Myklebust }
359*277e4ab7STrond Myklebust 
360*277e4ab7STrond Myklebust static ssize_t
361*277e4ab7STrond Myklebust xs_read_kvec(struct socket *sock, struct msghdr *msg, int flags,
362*277e4ab7STrond Myklebust 		struct kvec *kvec, size_t count, size_t seek)
363*277e4ab7STrond Myklebust {
364*277e4ab7STrond Myklebust 	iov_iter_kvec(&msg->msg_iter, READ | ITER_KVEC, kvec, 1, count);
365*277e4ab7STrond Myklebust 	return xs_sock_recvmsg(sock, msg, flags, seek);
366*277e4ab7STrond Myklebust }
367*277e4ab7STrond Myklebust 
368*277e4ab7STrond Myklebust static ssize_t
369*277e4ab7STrond Myklebust xs_read_bvec(struct socket *sock, struct msghdr *msg, int flags,
370*277e4ab7STrond Myklebust 		struct bio_vec *bvec, unsigned long nr, size_t count,
371*277e4ab7STrond Myklebust 		size_t seek)
372*277e4ab7STrond Myklebust {
373*277e4ab7STrond Myklebust 	iov_iter_bvec(&msg->msg_iter, READ | ITER_BVEC, bvec, nr, count);
374*277e4ab7STrond Myklebust 	return xs_sock_recvmsg(sock, msg, flags, seek);
375*277e4ab7STrond Myklebust }
376*277e4ab7STrond Myklebust 
377*277e4ab7STrond Myklebust static ssize_t
378*277e4ab7STrond Myklebust xs_read_discard(struct socket *sock, struct msghdr *msg, int flags,
379*277e4ab7STrond Myklebust 		size_t count)
380*277e4ab7STrond Myklebust {
381*277e4ab7STrond Myklebust 	struct kvec kvec = { 0 };
382*277e4ab7STrond Myklebust 	return xs_read_kvec(sock, msg, flags | MSG_TRUNC, &kvec, count, 0);
383*277e4ab7STrond Myklebust }
384*277e4ab7STrond Myklebust 
385*277e4ab7STrond Myklebust static ssize_t
386*277e4ab7STrond Myklebust xs_read_xdr_buf(struct socket *sock, struct msghdr *msg, int flags,
387*277e4ab7STrond Myklebust 		struct xdr_buf *buf, size_t count, size_t seek, size_t *read)
388*277e4ab7STrond Myklebust {
389*277e4ab7STrond Myklebust 	size_t want, seek_init = seek, offset = 0;
390*277e4ab7STrond Myklebust 	ssize_t ret;
391*277e4ab7STrond Myklebust 
392*277e4ab7STrond Myklebust 	if (seek < buf->head[0].iov_len) {
393*277e4ab7STrond Myklebust 		want = min_t(size_t, count, buf->head[0].iov_len);
394*277e4ab7STrond Myklebust 		ret = xs_read_kvec(sock, msg, flags, &buf->head[0], want, seek);
395*277e4ab7STrond Myklebust 		if (ret <= 0)
396*277e4ab7STrond Myklebust 			goto sock_err;
397*277e4ab7STrond Myklebust 		offset += ret;
398*277e4ab7STrond Myklebust 		if (offset == count || msg->msg_flags & (MSG_EOR|MSG_TRUNC))
399*277e4ab7STrond Myklebust 			goto out;
400*277e4ab7STrond Myklebust 		if (ret != want)
401*277e4ab7STrond Myklebust 			goto eagain;
402*277e4ab7STrond Myklebust 		seek = 0;
403*277e4ab7STrond Myklebust 	} else {
404*277e4ab7STrond Myklebust 		seek -= buf->head[0].iov_len;
405*277e4ab7STrond Myklebust 		offset += buf->head[0].iov_len;
406*277e4ab7STrond Myklebust 	}
407*277e4ab7STrond Myklebust 	if (seek < buf->page_len) {
408*277e4ab7STrond Myklebust 		want = xs_alloc_sparse_pages(buf,
409*277e4ab7STrond Myklebust 				min_t(size_t, count - offset, buf->page_len),
410*277e4ab7STrond Myklebust 				GFP_NOWAIT);
411*277e4ab7STrond Myklebust 		ret = xs_read_bvec(sock, msg, flags, buf->bvec,
412*277e4ab7STrond Myklebust 				xdr_buf_pagecount(buf),
413*277e4ab7STrond Myklebust 				want + buf->page_base,
414*277e4ab7STrond Myklebust 				seek + buf->page_base);
415*277e4ab7STrond Myklebust 		if (ret <= 0)
416*277e4ab7STrond Myklebust 			goto sock_err;
417*277e4ab7STrond Myklebust 		offset += ret - buf->page_base;
418*277e4ab7STrond Myklebust 		if (offset == count || msg->msg_flags & (MSG_EOR|MSG_TRUNC))
419*277e4ab7STrond Myklebust 			goto out;
420*277e4ab7STrond Myklebust 		if (ret != want)
421*277e4ab7STrond Myklebust 			goto eagain;
422*277e4ab7STrond Myklebust 		seek = 0;
423*277e4ab7STrond Myklebust 	} else {
424*277e4ab7STrond Myklebust 		seek -= buf->page_len;
425*277e4ab7STrond Myklebust 		offset += buf->page_len;
426*277e4ab7STrond Myklebust 	}
427*277e4ab7STrond Myklebust 	if (seek < buf->tail[0].iov_len) {
428*277e4ab7STrond Myklebust 		want = min_t(size_t, count - offset, buf->tail[0].iov_len);
429*277e4ab7STrond Myklebust 		ret = xs_read_kvec(sock, msg, flags, &buf->tail[0], want, seek);
430*277e4ab7STrond Myklebust 		if (ret <= 0)
431*277e4ab7STrond Myklebust 			goto sock_err;
432*277e4ab7STrond Myklebust 		offset += ret;
433*277e4ab7STrond Myklebust 		if (offset == count || msg->msg_flags & (MSG_EOR|MSG_TRUNC))
434*277e4ab7STrond Myklebust 			goto out;
435*277e4ab7STrond Myklebust 		if (ret != want)
436*277e4ab7STrond Myklebust 			goto eagain;
437*277e4ab7STrond Myklebust 	} else
438*277e4ab7STrond Myklebust 		offset += buf->tail[0].iov_len;
439*277e4ab7STrond Myklebust 	ret = -EMSGSIZE;
440*277e4ab7STrond Myklebust 	msg->msg_flags |= MSG_TRUNC;
441*277e4ab7STrond Myklebust out:
442*277e4ab7STrond Myklebust 	*read = offset - seek_init;
443*277e4ab7STrond Myklebust 	return ret;
444*277e4ab7STrond Myklebust eagain:
445*277e4ab7STrond Myklebust 	ret = -EAGAIN;
446*277e4ab7STrond Myklebust 	goto out;
447*277e4ab7STrond Myklebust sock_err:
448*277e4ab7STrond Myklebust 	offset += seek;
449*277e4ab7STrond Myklebust 	goto out;
450*277e4ab7STrond Myklebust }
451*277e4ab7STrond Myklebust 
452*277e4ab7STrond Myklebust static void
453*277e4ab7STrond Myklebust xs_read_header(struct sock_xprt *transport, struct xdr_buf *buf)
454*277e4ab7STrond Myklebust {
455*277e4ab7STrond Myklebust 	if (!transport->recv.copied) {
456*277e4ab7STrond Myklebust 		if (buf->head[0].iov_len >= transport->recv.offset)
457*277e4ab7STrond Myklebust 			memcpy(buf->head[0].iov_base,
458*277e4ab7STrond Myklebust 					&transport->recv.xid,
459*277e4ab7STrond Myklebust 					transport->recv.offset);
460*277e4ab7STrond Myklebust 		transport->recv.copied = transport->recv.offset;
461*277e4ab7STrond Myklebust 	}
462*277e4ab7STrond Myklebust }
463*277e4ab7STrond Myklebust 
464*277e4ab7STrond Myklebust static bool
465*277e4ab7STrond Myklebust xs_read_stream_request_done(struct sock_xprt *transport)
466*277e4ab7STrond Myklebust {
467*277e4ab7STrond Myklebust 	return transport->recv.fraghdr & cpu_to_be32(RPC_LAST_STREAM_FRAGMENT);
468*277e4ab7STrond Myklebust }
469*277e4ab7STrond Myklebust 
470*277e4ab7STrond Myklebust static ssize_t
471*277e4ab7STrond Myklebust xs_read_stream_request(struct sock_xprt *transport, struct msghdr *msg,
472*277e4ab7STrond Myklebust 		int flags, struct rpc_rqst *req)
473*277e4ab7STrond Myklebust {
474*277e4ab7STrond Myklebust 	struct xdr_buf *buf = &req->rq_private_buf;
475*277e4ab7STrond Myklebust 	size_t want, read;
476*277e4ab7STrond Myklebust 	ssize_t ret;
477*277e4ab7STrond Myklebust 
478*277e4ab7STrond Myklebust 	xs_read_header(transport, buf);
479*277e4ab7STrond Myklebust 
480*277e4ab7STrond Myklebust 	want = transport->recv.len - transport->recv.offset;
481*277e4ab7STrond Myklebust 	ret = xs_read_xdr_buf(transport->sock, msg, flags, buf,
482*277e4ab7STrond Myklebust 			transport->recv.copied + want, transport->recv.copied,
483*277e4ab7STrond Myklebust 			&read);
484*277e4ab7STrond Myklebust 	transport->recv.offset += read;
485*277e4ab7STrond Myklebust 	transport->recv.copied += read;
486*277e4ab7STrond Myklebust 	if (transport->recv.offset == transport->recv.len) {
487*277e4ab7STrond Myklebust 		if (xs_read_stream_request_done(transport))
488*277e4ab7STrond Myklebust 			msg->msg_flags |= MSG_EOR;
489*277e4ab7STrond Myklebust 		return transport->recv.copied;
490*277e4ab7STrond Myklebust 	}
491*277e4ab7STrond Myklebust 
492*277e4ab7STrond Myklebust 	switch (ret) {
493*277e4ab7STrond Myklebust 	case -EMSGSIZE:
494*277e4ab7STrond Myklebust 		return transport->recv.copied;
495*277e4ab7STrond Myklebust 	case 0:
496*277e4ab7STrond Myklebust 		return -ESHUTDOWN;
497*277e4ab7STrond Myklebust 	default:
498*277e4ab7STrond Myklebust 		if (ret < 0)
499*277e4ab7STrond Myklebust 			return ret;
500*277e4ab7STrond Myklebust 	}
501*277e4ab7STrond Myklebust 	return -EAGAIN;
502*277e4ab7STrond Myklebust }
503*277e4ab7STrond Myklebust 
504*277e4ab7STrond Myklebust static size_t
505*277e4ab7STrond Myklebust xs_read_stream_headersize(bool isfrag)
506*277e4ab7STrond Myklebust {
507*277e4ab7STrond Myklebust 	if (isfrag)
508*277e4ab7STrond Myklebust 		return sizeof(__be32);
509*277e4ab7STrond Myklebust 	return 3 * sizeof(__be32);
510*277e4ab7STrond Myklebust }
511*277e4ab7STrond Myklebust 
512*277e4ab7STrond Myklebust static ssize_t
513*277e4ab7STrond Myklebust xs_read_stream_header(struct sock_xprt *transport, struct msghdr *msg,
514*277e4ab7STrond Myklebust 		int flags, size_t want, size_t seek)
515*277e4ab7STrond Myklebust {
516*277e4ab7STrond Myklebust 	struct kvec kvec = {
517*277e4ab7STrond Myklebust 		.iov_base = &transport->recv.fraghdr,
518*277e4ab7STrond Myklebust 		.iov_len = want,
519*277e4ab7STrond Myklebust 	};
520*277e4ab7STrond Myklebust 	return xs_read_kvec(transport->sock, msg, flags, &kvec, want, seek);
521*277e4ab7STrond Myklebust }
522*277e4ab7STrond Myklebust 
523*277e4ab7STrond Myklebust #if defined(CONFIG_SUNRPC_BACKCHANNEL)
524*277e4ab7STrond Myklebust static ssize_t
525*277e4ab7STrond Myklebust xs_read_stream_call(struct sock_xprt *transport, struct msghdr *msg, int flags)
526*277e4ab7STrond Myklebust {
527*277e4ab7STrond Myklebust 	struct rpc_xprt *xprt = &transport->xprt;
528*277e4ab7STrond Myklebust 	struct rpc_rqst *req;
529*277e4ab7STrond Myklebust 	ssize_t ret;
530*277e4ab7STrond Myklebust 
531*277e4ab7STrond Myklebust 	/* Look up and lock the request corresponding to the given XID */
532*277e4ab7STrond Myklebust 	req = xprt_lookup_bc_request(xprt, transport->recv.xid);
533*277e4ab7STrond Myklebust 	if (!req) {
534*277e4ab7STrond Myklebust 		printk(KERN_WARNING "Callback slot table overflowed\n");
535*277e4ab7STrond Myklebust 		return -ESHUTDOWN;
536*277e4ab7STrond Myklebust 	}
537*277e4ab7STrond Myklebust 
538*277e4ab7STrond Myklebust 	ret = xs_read_stream_request(transport, msg, flags, req);
539*277e4ab7STrond Myklebust 	if (msg->msg_flags & (MSG_EOR|MSG_TRUNC))
540*277e4ab7STrond Myklebust 		xprt_complete_bc_request(req, ret);
541*277e4ab7STrond Myklebust 
542*277e4ab7STrond Myklebust 	return ret;
543*277e4ab7STrond Myklebust }
544*277e4ab7STrond Myklebust #else /* CONFIG_SUNRPC_BACKCHANNEL */
545*277e4ab7STrond Myklebust static ssize_t
546*277e4ab7STrond Myklebust xs_read_stream_call(struct sock_xprt *transport, struct msghdr *msg, int flags)
547*277e4ab7STrond Myklebust {
548*277e4ab7STrond Myklebust 	return -ESHUTDOWN;
549*277e4ab7STrond Myklebust }
550*277e4ab7STrond Myklebust #endif /* CONFIG_SUNRPC_BACKCHANNEL */
551*277e4ab7STrond Myklebust 
552*277e4ab7STrond Myklebust static ssize_t
553*277e4ab7STrond Myklebust xs_read_stream_reply(struct sock_xprt *transport, struct msghdr *msg, int flags)
554*277e4ab7STrond Myklebust {
555*277e4ab7STrond Myklebust 	struct rpc_xprt *xprt = &transport->xprt;
556*277e4ab7STrond Myklebust 	struct rpc_rqst *req;
557*277e4ab7STrond Myklebust 	ssize_t ret = 0;
558*277e4ab7STrond Myklebust 
559*277e4ab7STrond Myklebust 	/* Look up and lock the request corresponding to the given XID */
560*277e4ab7STrond Myklebust 	spin_lock(&xprt->queue_lock);
561*277e4ab7STrond Myklebust 	req = xprt_lookup_rqst(xprt, transport->recv.xid);
562*277e4ab7STrond Myklebust 	if (!req) {
563*277e4ab7STrond Myklebust 		msg->msg_flags |= MSG_TRUNC;
564*277e4ab7STrond Myklebust 		goto out;
565*277e4ab7STrond Myklebust 	}
566*277e4ab7STrond Myklebust 	xprt_pin_rqst(req);
567*277e4ab7STrond Myklebust 	spin_unlock(&xprt->queue_lock);
568*277e4ab7STrond Myklebust 
569*277e4ab7STrond Myklebust 	ret = xs_read_stream_request(transport, msg, flags, req);
570*277e4ab7STrond Myklebust 
571*277e4ab7STrond Myklebust 	spin_lock(&xprt->queue_lock);
572*277e4ab7STrond Myklebust 	if (msg->msg_flags & (MSG_EOR|MSG_TRUNC))
573*277e4ab7STrond Myklebust 		xprt_complete_rqst(req->rq_task, ret);
574*277e4ab7STrond Myklebust 	xprt_unpin_rqst(req);
575*277e4ab7STrond Myklebust out:
576*277e4ab7STrond Myklebust 	spin_unlock(&xprt->queue_lock);
577*277e4ab7STrond Myklebust 	return ret;
578*277e4ab7STrond Myklebust }
579*277e4ab7STrond Myklebust 
580*277e4ab7STrond Myklebust static ssize_t
581*277e4ab7STrond Myklebust xs_read_stream(struct sock_xprt *transport, int flags)
582*277e4ab7STrond Myklebust {
583*277e4ab7STrond Myklebust 	struct msghdr msg = { 0 };
584*277e4ab7STrond Myklebust 	size_t want, read = 0;
585*277e4ab7STrond Myklebust 	ssize_t ret = 0;
586*277e4ab7STrond Myklebust 
587*277e4ab7STrond Myklebust 	if (transport->recv.len == 0) {
588*277e4ab7STrond Myklebust 		want = xs_read_stream_headersize(transport->recv.copied != 0);
589*277e4ab7STrond Myklebust 		ret = xs_read_stream_header(transport, &msg, flags, want,
590*277e4ab7STrond Myklebust 				transport->recv.offset);
591*277e4ab7STrond Myklebust 		if (ret <= 0)
592*277e4ab7STrond Myklebust 			goto out_err;
593*277e4ab7STrond Myklebust 		transport->recv.offset = ret;
594*277e4ab7STrond Myklebust 		if (ret != want) {
595*277e4ab7STrond Myklebust 			ret = -EAGAIN;
596*277e4ab7STrond Myklebust 			goto out_err;
597*277e4ab7STrond Myklebust 		}
598*277e4ab7STrond Myklebust 		transport->recv.len = be32_to_cpu(transport->recv.fraghdr) &
599*277e4ab7STrond Myklebust 			RPC_FRAGMENT_SIZE_MASK;
600*277e4ab7STrond Myklebust 		transport->recv.offset -= sizeof(transport->recv.fraghdr);
601*277e4ab7STrond Myklebust 		read = ret;
602*277e4ab7STrond Myklebust 	}
603*277e4ab7STrond Myklebust 
604*277e4ab7STrond Myklebust 	switch (be32_to_cpu(transport->recv.calldir)) {
605*277e4ab7STrond Myklebust 	case RPC_CALL:
606*277e4ab7STrond Myklebust 		ret = xs_read_stream_call(transport, &msg, flags);
607*277e4ab7STrond Myklebust 		break;
608*277e4ab7STrond Myklebust 	case RPC_REPLY:
609*277e4ab7STrond Myklebust 		ret = xs_read_stream_reply(transport, &msg, flags);
610*277e4ab7STrond Myklebust 	}
611*277e4ab7STrond Myklebust 	if (msg.msg_flags & MSG_TRUNC) {
612*277e4ab7STrond Myklebust 		transport->recv.calldir = cpu_to_be32(-1);
613*277e4ab7STrond Myklebust 		transport->recv.copied = -1;
614*277e4ab7STrond Myklebust 	}
615*277e4ab7STrond Myklebust 	if (ret < 0)
616*277e4ab7STrond Myklebust 		goto out_err;
617*277e4ab7STrond Myklebust 	read += ret;
618*277e4ab7STrond Myklebust 	if (transport->recv.offset < transport->recv.len) {
619*277e4ab7STrond Myklebust 		ret = xs_read_discard(transport->sock, &msg, flags,
620*277e4ab7STrond Myklebust 				transport->recv.len - transport->recv.offset);
621*277e4ab7STrond Myklebust 		if (ret <= 0)
622*277e4ab7STrond Myklebust 			goto out_err;
623*277e4ab7STrond Myklebust 		transport->recv.offset += ret;
624*277e4ab7STrond Myklebust 		read += ret;
625*277e4ab7STrond Myklebust 		if (transport->recv.offset != transport->recv.len)
626*277e4ab7STrond Myklebust 			return -EAGAIN;
627*277e4ab7STrond Myklebust 	}
628*277e4ab7STrond Myklebust 	if (xs_read_stream_request_done(transport)) {
629*277e4ab7STrond Myklebust 		trace_xs_tcp_data_recv(transport);
630*277e4ab7STrond Myklebust 		transport->recv.copied = 0;
631*277e4ab7STrond Myklebust 	}
632*277e4ab7STrond Myklebust 	transport->recv.offset = 0;
633*277e4ab7STrond Myklebust 	transport->recv.len = 0;
634*277e4ab7STrond Myklebust 	return read;
635*277e4ab7STrond Myklebust out_err:
636*277e4ab7STrond Myklebust 	switch (ret) {
637*277e4ab7STrond Myklebust 	case 0:
638*277e4ab7STrond Myklebust 	case -ESHUTDOWN:
639*277e4ab7STrond Myklebust 		xprt_force_disconnect(&transport->xprt);
640*277e4ab7STrond Myklebust 		return -ESHUTDOWN;
641*277e4ab7STrond Myklebust 	}
642*277e4ab7STrond Myklebust 	return ret;
643*277e4ab7STrond Myklebust }
644*277e4ab7STrond Myklebust 
645b4b5cc85SChuck Lever #define XS_SENDMSG_FLAGS	(MSG_DONTWAIT | MSG_NOSIGNAL)
646b4b5cc85SChuck Lever 
64724c5684bSTrond Myklebust static int xs_send_kvec(struct socket *sock, struct sockaddr *addr, int addrlen, struct kvec *vec, unsigned int base, int more)
648b4b5cc85SChuck Lever {
649b4b5cc85SChuck Lever 	struct msghdr msg = {
650b4b5cc85SChuck Lever 		.msg_name	= addr,
651b4b5cc85SChuck Lever 		.msg_namelen	= addrlen,
65224c5684bSTrond Myklebust 		.msg_flags	= XS_SENDMSG_FLAGS | (more ? MSG_MORE : 0),
65324c5684bSTrond Myklebust 	};
65424c5684bSTrond Myklebust 	struct kvec iov = {
65524c5684bSTrond Myklebust 		.iov_base	= vec->iov_base + base,
65624c5684bSTrond Myklebust 		.iov_len	= vec->iov_len - base,
657b4b5cc85SChuck Lever 	};
658b4b5cc85SChuck Lever 
65924c5684bSTrond Myklebust 	if (iov.iov_len != 0)
660b4b5cc85SChuck Lever 		return kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len);
661b4b5cc85SChuck Lever 	return kernel_sendmsg(sock, &msg, NULL, 0, 0);
662b4b5cc85SChuck Lever }
663b4b5cc85SChuck Lever 
664f279cd00SJason Baron static int xs_send_pagedata(struct socket *sock, struct xdr_buf *xdr, unsigned int base, int more, bool zerocopy, int *sent_p)
665b4b5cc85SChuck Lever {
666a6b31d18STrond Myklebust 	ssize_t (*do_sendpage)(struct socket *sock, struct page *page,
667a6b31d18STrond Myklebust 			int offset, size_t size, int flags);
66824c5684bSTrond Myklebust 	struct page **ppage;
66924c5684bSTrond Myklebust 	unsigned int remainder;
670f279cd00SJason Baron 	int err;
671b4b5cc85SChuck Lever 
67224c5684bSTrond Myklebust 	remainder = xdr->page_len - base;
67324c5684bSTrond Myklebust 	base += xdr->page_base;
67424c5684bSTrond Myklebust 	ppage = xdr->pages + (base >> PAGE_SHIFT);
67524c5684bSTrond Myklebust 	base &= ~PAGE_MASK;
676a6b31d18STrond Myklebust 	do_sendpage = sock->ops->sendpage;
677a6b31d18STrond Myklebust 	if (!zerocopy)
678a6b31d18STrond Myklebust 		do_sendpage = sock_no_sendpage;
67924c5684bSTrond Myklebust 	for(;;) {
68024c5684bSTrond Myklebust 		unsigned int len = min_t(unsigned int, PAGE_SIZE - base, remainder);
68124c5684bSTrond Myklebust 		int flags = XS_SENDMSG_FLAGS;
68224c5684bSTrond Myklebust 
68324c5684bSTrond Myklebust 		remainder -= len;
68431303d6cSTrond Myklebust 		if (more)
68524c5684bSTrond Myklebust 			flags |= MSG_MORE;
68631303d6cSTrond Myklebust 		if (remainder != 0)
68731303d6cSTrond Myklebust 			flags |= MSG_SENDPAGE_NOTLAST | MSG_MORE;
688a6b31d18STrond Myklebust 		err = do_sendpage(sock, *ppage, base, len, flags);
68924c5684bSTrond Myklebust 		if (remainder == 0 || err != len)
69024c5684bSTrond Myklebust 			break;
691f279cd00SJason Baron 		*sent_p += err;
69224c5684bSTrond Myklebust 		ppage++;
69324c5684bSTrond Myklebust 		base = 0;
69424c5684bSTrond Myklebust 	}
695f279cd00SJason Baron 	if (err > 0) {
696f279cd00SJason Baron 		*sent_p += err;
697f279cd00SJason Baron 		err = 0;
698f279cd00SJason Baron 	}
69924c5684bSTrond Myklebust 	return err;
700b4b5cc85SChuck Lever }
701b4b5cc85SChuck Lever 
7029903cd1cSChuck Lever /**
7039903cd1cSChuck Lever  * xs_sendpages - write pages directly to a socket
7049903cd1cSChuck Lever  * @sock: socket to send on
7059903cd1cSChuck Lever  * @addr: UDP only -- address of destination
7069903cd1cSChuck Lever  * @addrlen: UDP only -- length of destination address
7079903cd1cSChuck Lever  * @xdr: buffer containing this request
7089903cd1cSChuck Lever  * @base: starting position in the buffer
709a6b31d18STrond Myklebust  * @zerocopy: true if it is safe to use sendpage()
710f279cd00SJason Baron  * @sent_p: return the total number of bytes successfully queued for sending
7119903cd1cSChuck Lever  *
712a246b010SChuck Lever  */
713f279cd00SJason Baron static int xs_sendpages(struct socket *sock, struct sockaddr *addr, int addrlen, struct xdr_buf *xdr, unsigned int base, bool zerocopy, int *sent_p)
714a246b010SChuck Lever {
71524c5684bSTrond Myklebust 	unsigned int remainder = xdr->len - base;
716f279cd00SJason Baron 	int err = 0;
717f279cd00SJason Baron 	int sent = 0;
718a246b010SChuck Lever 
719262965f5SChuck Lever 	if (unlikely(!sock))
720fba91afbSTrond Myklebust 		return -ENOTSOCK;
721262965f5SChuck Lever 
72224c5684bSTrond Myklebust 	if (base != 0) {
72324c5684bSTrond Myklebust 		addr = NULL;
72424c5684bSTrond Myklebust 		addrlen = 0;
72524c5684bSTrond Myklebust 	}
726262965f5SChuck Lever 
72724c5684bSTrond Myklebust 	if (base < xdr->head[0].iov_len || addr != NULL) {
72824c5684bSTrond Myklebust 		unsigned int len = xdr->head[0].iov_len - base;
72924c5684bSTrond Myklebust 		remainder -= len;
73024c5684bSTrond Myklebust 		err = xs_send_kvec(sock, addr, addrlen, &xdr->head[0], base, remainder != 0);
73124c5684bSTrond Myklebust 		if (remainder == 0 || err != len)
732a246b010SChuck Lever 			goto out;
733f279cd00SJason Baron 		*sent_p += err;
734a246b010SChuck Lever 		base = 0;
735a246b010SChuck Lever 	} else
73624c5684bSTrond Myklebust 		base -= xdr->head[0].iov_len;
737a246b010SChuck Lever 
73824c5684bSTrond Myklebust 	if (base < xdr->page_len) {
73924c5684bSTrond Myklebust 		unsigned int len = xdr->page_len - base;
74024c5684bSTrond Myklebust 		remainder -= len;
741f279cd00SJason Baron 		err = xs_send_pagedata(sock, xdr, base, remainder != 0, zerocopy, &sent);
742f279cd00SJason Baron 		*sent_p += sent;
743f279cd00SJason Baron 		if (remainder == 0 || sent != len)
744a246b010SChuck Lever 			goto out;
745a246b010SChuck Lever 		base = 0;
74624c5684bSTrond Myklebust 	} else
74724c5684bSTrond Myklebust 		base -= xdr->page_len;
74824c5684bSTrond Myklebust 
74924c5684bSTrond Myklebust 	if (base >= xdr->tail[0].iov_len)
750f279cd00SJason Baron 		return 0;
75124c5684bSTrond Myklebust 	err = xs_send_kvec(sock, NULL, 0, &xdr->tail[0], base, 0);
752a246b010SChuck Lever out:
753f279cd00SJason Baron 	if (err > 0) {
754f279cd00SJason Baron 		*sent_p += err;
755f279cd00SJason Baron 		err = 0;
756f279cd00SJason Baron 	}
75724c5684bSTrond Myklebust 	return err;
758a246b010SChuck Lever }
759a246b010SChuck Lever 
7609903cd1cSChuck Lever /**
761c544577dSTrond Myklebust  * xs_nospace - handle transmit was incomplete
76250f484e2STrond Myklebust  * @req: pointer to RPC request
7639903cd1cSChuck Lever  *
764a246b010SChuck Lever  */
765c544577dSTrond Myklebust static int xs_nospace(struct rpc_rqst *req)
766a246b010SChuck Lever {
767262965f5SChuck Lever 	struct rpc_xprt *xprt = req->rq_xprt;
768ee0ac0c2SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
76906ea0bfeSTrond Myklebust 	struct sock *sk = transport->inet;
77024ca9a84STrond Myklebust 	int ret = -EAGAIN;
771a246b010SChuck Lever 
77246121cf7SChuck Lever 	dprintk("RPC: %5u xmit incomplete (%u left of %u)\n",
773c544577dSTrond Myklebust 			req->rq_task->tk_pid,
774c544577dSTrond Myklebust 			req->rq_slen - transport->xmit.offset,
775262965f5SChuck Lever 			req->rq_slen);
776a246b010SChuck Lever 
777262965f5SChuck Lever 	/* Protect against races with write_space */
778262965f5SChuck Lever 	spin_lock_bh(&xprt->transport_lock);
779a246b010SChuck Lever 
780262965f5SChuck Lever 	/* Don't race with disconnect */
781b6ddf64fSTrond Myklebust 	if (xprt_connected(xprt)) {
78213331a55STrond Myklebust 		/* wait for more buffer space */
78306ea0bfeSTrond Myklebust 		sk->sk_write_pending++;
784c544577dSTrond Myklebust 		xprt_wait_for_buffer_space(xprt);
78513331a55STrond Myklebust 	} else
7865e3771ceSTrond Myklebust 		ret = -ENOTCONN;
787a246b010SChuck Lever 
788262965f5SChuck Lever 	spin_unlock_bh(&xprt->transport_lock);
78906ea0bfeSTrond Myklebust 
79006ea0bfeSTrond Myklebust 	/* Race breaker in case memory is freed before above code is called */
791d48f9ce7SDavid Vrabel 	if (ret == -EAGAIN) {
792d48f9ce7SDavid Vrabel 		struct socket_wq *wq;
793d48f9ce7SDavid Vrabel 
794d48f9ce7SDavid Vrabel 		rcu_read_lock();
795d48f9ce7SDavid Vrabel 		wq = rcu_dereference(sk->sk_wq);
796d48f9ce7SDavid Vrabel 		set_bit(SOCKWQ_ASYNC_NOSPACE, &wq->flags);
797d48f9ce7SDavid Vrabel 		rcu_read_unlock();
798d48f9ce7SDavid Vrabel 
79906ea0bfeSTrond Myklebust 		sk->sk_write_space(sk);
800d48f9ce7SDavid Vrabel 	}
8015e3771ceSTrond Myklebust 	return ret;
802a246b010SChuck Lever }
803a246b010SChuck Lever 
804*277e4ab7STrond Myklebust static void
805*277e4ab7STrond Myklebust xs_stream_prepare_request(struct rpc_rqst *req)
806*277e4ab7STrond Myklebust {
807*277e4ab7STrond Myklebust 	req->rq_task->tk_status = xdr_alloc_bvec(&req->rq_rcv_buf, GFP_NOIO);
808*277e4ab7STrond Myklebust }
809*277e4ab7STrond Myklebust 
81061677eeeSChuck Lever /*
8114cd34e7cSTrond Myklebust  * Determine if the previous message in the stream was aborted before it
8124cd34e7cSTrond Myklebust  * could complete transmission.
8134cd34e7cSTrond Myklebust  */
8144cd34e7cSTrond Myklebust static bool
8154cd34e7cSTrond Myklebust xs_send_request_was_aborted(struct sock_xprt *transport, struct rpc_rqst *req)
8164cd34e7cSTrond Myklebust {
8174cd34e7cSTrond Myklebust 	return transport->xmit.offset != 0 && req->rq_bytes_sent == 0;
8184cd34e7cSTrond Myklebust }
8194cd34e7cSTrond Myklebust 
8204cd34e7cSTrond Myklebust /*
82161677eeeSChuck Lever  * Construct a stream transport record marker in @buf.
82261677eeeSChuck Lever  */
82361677eeeSChuck Lever static inline void xs_encode_stream_record_marker(struct xdr_buf *buf)
82461677eeeSChuck Lever {
82561677eeeSChuck Lever 	u32 reclen = buf->len - sizeof(rpc_fraghdr);
82661677eeeSChuck Lever 	rpc_fraghdr *base = buf->head[0].iov_base;
82761677eeeSChuck Lever 	*base = cpu_to_be32(RPC_LAST_STREAM_FRAGMENT | reclen);
82861677eeeSChuck Lever }
82961677eeeSChuck Lever 
8309903cd1cSChuck Lever /**
831176e21eeSChuck Lever  * xs_local_send_request - write an RPC request to an AF_LOCAL socket
83250f484e2STrond Myklebust  * @req: pointer to RPC request
833176e21eeSChuck Lever  *
834176e21eeSChuck Lever  * Return values:
835176e21eeSChuck Lever  *        0:	The request has been sent
836176e21eeSChuck Lever  *   EAGAIN:	The socket was blocked, please call again later to
837176e21eeSChuck Lever  *		complete the request
838176e21eeSChuck Lever  * ENOTCONN:	Caller needs to invoke connect logic then call again
839176e21eeSChuck Lever  *    other:	Some other error occured, the request was not sent
840176e21eeSChuck Lever  */
841adfa7144STrond Myklebust static int xs_local_send_request(struct rpc_rqst *req)
842176e21eeSChuck Lever {
843176e21eeSChuck Lever 	struct rpc_xprt *xprt = req->rq_xprt;
844176e21eeSChuck Lever 	struct sock_xprt *transport =
845176e21eeSChuck Lever 				container_of(xprt, struct sock_xprt, xprt);
846176e21eeSChuck Lever 	struct xdr_buf *xdr = &req->rq_snd_buf;
847176e21eeSChuck Lever 	int status;
848f279cd00SJason Baron 	int sent = 0;
849176e21eeSChuck Lever 
8504cd34e7cSTrond Myklebust 	/* Close the stream if the previous transmission was incomplete */
8514cd34e7cSTrond Myklebust 	if (xs_send_request_was_aborted(transport, req)) {
8524cd34e7cSTrond Myklebust 		xs_close(xprt);
8534cd34e7cSTrond Myklebust 		return -ENOTCONN;
8544cd34e7cSTrond Myklebust 	}
8554cd34e7cSTrond Myklebust 
856176e21eeSChuck Lever 	xs_encode_stream_record_marker(&req->rq_snd_buf);
857176e21eeSChuck Lever 
858176e21eeSChuck Lever 	xs_pktdump("packet data:",
859176e21eeSChuck Lever 			req->rq_svec->iov_base, req->rq_svec->iov_len);
860176e21eeSChuck Lever 
86178215759SChuck Lever 	req->rq_xtime = ktime_get();
8626c7a64e5STrond Myklebust 	status = xs_sendpages(transport->sock, NULL, 0, xdr,
8636c7a64e5STrond Myklebust 			      transport->xmit.offset,
864f279cd00SJason Baron 			      true, &sent);
865176e21eeSChuck Lever 	dprintk("RPC:       %s(%u) = %d\n",
8666c7a64e5STrond Myklebust 			__func__, xdr->len - transport->xmit.offset, status);
867743c69e7SNeilBrown 
868743c69e7SNeilBrown 	if (status == -EAGAIN && sock_writeable(transport->inet))
869743c69e7SNeilBrown 		status = -ENOBUFS;
870743c69e7SNeilBrown 
871f279cd00SJason Baron 	if (likely(sent > 0) || status == 0) {
8726c7a64e5STrond Myklebust 		transport->xmit.offset += sent;
8736c7a64e5STrond Myklebust 		req->rq_bytes_sent = transport->xmit.offset;
874176e21eeSChuck Lever 		if (likely(req->rq_bytes_sent >= req->rq_slen)) {
8756c7a64e5STrond Myklebust 			req->rq_xmit_bytes_sent += transport->xmit.offset;
876176e21eeSChuck Lever 			req->rq_bytes_sent = 0;
8776c7a64e5STrond Myklebust 			transport->xmit.offset = 0;
878176e21eeSChuck Lever 			return 0;
879176e21eeSChuck Lever 		}
880176e21eeSChuck Lever 		status = -EAGAIN;
881176e21eeSChuck Lever 	}
882176e21eeSChuck Lever 
883176e21eeSChuck Lever 	switch (status) {
8843601c4a9STrond Myklebust 	case -ENOBUFS:
885b5872f0cSTrond Myklebust 		break;
886176e21eeSChuck Lever 	case -EAGAIN:
887c544577dSTrond Myklebust 		status = xs_nospace(req);
888176e21eeSChuck Lever 		break;
889176e21eeSChuck Lever 	default:
890176e21eeSChuck Lever 		dprintk("RPC:       sendmsg returned unrecognized error %d\n",
891176e21eeSChuck Lever 			-status);
892e9d47639SGustavo A. R. Silva 		/* fall through */
893176e21eeSChuck Lever 	case -EPIPE:
894176e21eeSChuck Lever 		xs_close(xprt);
895176e21eeSChuck Lever 		status = -ENOTCONN;
896176e21eeSChuck Lever 	}
897176e21eeSChuck Lever 
898176e21eeSChuck Lever 	return status;
899176e21eeSChuck Lever }
900176e21eeSChuck Lever 
901176e21eeSChuck Lever /**
902262965f5SChuck Lever  * xs_udp_send_request - write an RPC request to a UDP socket
90350f484e2STrond Myklebust  * @req: pointer to RPC request
9049903cd1cSChuck Lever  *
9059903cd1cSChuck Lever  * Return values:
9069903cd1cSChuck Lever  *        0:	The request has been sent
9079903cd1cSChuck Lever  *   EAGAIN:	The socket was blocked, please call again later to
9089903cd1cSChuck Lever  *		complete the request
909262965f5SChuck Lever  * ENOTCONN:	Caller needs to invoke connect logic then call again
91025985edcSLucas De Marchi  *    other:	Some other error occurred, the request was not sent
9119903cd1cSChuck Lever  */
912adfa7144STrond Myklebust static int xs_udp_send_request(struct rpc_rqst *req)
913a246b010SChuck Lever {
914a246b010SChuck Lever 	struct rpc_xprt *xprt = req->rq_xprt;
915ee0ac0c2SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
916262965f5SChuck Lever 	struct xdr_buf *xdr = &req->rq_snd_buf;
917f279cd00SJason Baron 	int sent = 0;
918262965f5SChuck Lever 	int status;
919262965f5SChuck Lever 
920262965f5SChuck Lever 	xs_pktdump("packet data:",
921262965f5SChuck Lever 				req->rq_svec->iov_base,
922262965f5SChuck Lever 				req->rq_svec->iov_len);
923262965f5SChuck Lever 
92401d37c42STrond Myklebust 	if (!xprt_bound(xprt))
92501d37c42STrond Myklebust 		return -ENOTCONN;
92675891f50STrond Myklebust 
92775891f50STrond Myklebust 	if (!xprt_request_get_cong(xprt, req))
92875891f50STrond Myklebust 		return -EBADSLT;
92975891f50STrond Myklebust 
93078215759SChuck Lever 	req->rq_xtime = ktime_get();
931f279cd00SJason Baron 	status = xs_sendpages(transport->sock, xs_addr(xprt), xprt->addrlen,
9326c7a64e5STrond Myklebust 			      xdr, 0, true, &sent);
933262965f5SChuck Lever 
934262965f5SChuck Lever 	dprintk("RPC:       xs_udp_send_request(%u) = %d\n",
9356c7a64e5STrond Myklebust 			xdr->len, status);
936262965f5SChuck Lever 
9373dedbb5cSJason Baron 	/* firewall is blocking us, don't return -EAGAIN or we end up looping */
9383dedbb5cSJason Baron 	if (status == -EPERM)
9393dedbb5cSJason Baron 		goto process_status;
9403dedbb5cSJason Baron 
941743c69e7SNeilBrown 	if (status == -EAGAIN && sock_writeable(transport->inet))
942743c69e7SNeilBrown 		status = -ENOBUFS;
943743c69e7SNeilBrown 
944f279cd00SJason Baron 	if (sent > 0 || status == 0) {
945f279cd00SJason Baron 		req->rq_xmit_bytes_sent += sent;
946f279cd00SJason Baron 		if (sent >= req->rq_slen)
947262965f5SChuck Lever 			return 0;
948262965f5SChuck Lever 		/* Still some bytes left; set up for a retry later. */
949262965f5SChuck Lever 		status = -EAGAIN;
9502199700fSTrond Myklebust 	}
951262965f5SChuck Lever 
9523dedbb5cSJason Baron process_status:
953262965f5SChuck Lever 	switch (status) {
954fba91afbSTrond Myklebust 	case -ENOTSOCK:
955fba91afbSTrond Myklebust 		status = -ENOTCONN;
956fba91afbSTrond Myklebust 		/* Should we call xs_close() here? */
957fba91afbSTrond Myklebust 		break;
958b6ddf64fSTrond Myklebust 	case -EAGAIN:
959c544577dSTrond Myklebust 		status = xs_nospace(req);
960b6ddf64fSTrond Myklebust 		break;
961262965f5SChuck Lever 	case -ENETUNREACH:
9623601c4a9STrond Myklebust 	case -ENOBUFS:
963262965f5SChuck Lever 	case -EPIPE:
964262965f5SChuck Lever 	case -ECONNREFUSED:
9653dedbb5cSJason Baron 	case -EPERM:
966262965f5SChuck Lever 		/* When the server has died, an ICMP port unreachable message
967262965f5SChuck Lever 		 * prompts ECONNREFUSED. */
96813331a55STrond Myklebust 		break;
96913331a55STrond Myklebust 	default:
97013331a55STrond Myklebust 		dprintk("RPC:       sendmsg returned unrecognized error %d\n",
97113331a55STrond Myklebust 			-status);
972262965f5SChuck Lever 	}
9735fe46e9dSBian Naimeng 
974262965f5SChuck Lever 	return status;
975262965f5SChuck Lever }
976262965f5SChuck Lever 
977e06799f9STrond Myklebust /**
978262965f5SChuck Lever  * xs_tcp_send_request - write an RPC request to a TCP socket
97950f484e2STrond Myklebust  * @req: pointer to RPC request
980262965f5SChuck Lever  *
981262965f5SChuck Lever  * Return values:
982262965f5SChuck Lever  *        0:	The request has been sent
983262965f5SChuck Lever  *   EAGAIN:	The socket was blocked, please call again later to
984262965f5SChuck Lever  *		complete the request
985262965f5SChuck Lever  * ENOTCONN:	Caller needs to invoke connect logic then call again
98625985edcSLucas De Marchi  *    other:	Some other error occurred, the request was not sent
987262965f5SChuck Lever  *
988262965f5SChuck Lever  * XXX: In the case of soft timeouts, should we eventually give up
989262965f5SChuck Lever  *	if sendmsg is not able to make progress?
990262965f5SChuck Lever  */
991adfa7144STrond Myklebust static int xs_tcp_send_request(struct rpc_rqst *req)
992262965f5SChuck Lever {
993262965f5SChuck Lever 	struct rpc_xprt *xprt = req->rq_xprt;
994ee0ac0c2SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
995262965f5SChuck Lever 	struct xdr_buf *xdr = &req->rq_snd_buf;
996a6b31d18STrond Myklebust 	bool zerocopy = true;
9979ffadfbcSTrond Myklebust 	bool vm_wait = false;
998b595bb15SChuck Lever 	int status;
999f279cd00SJason Baron 	int sent;
1000a246b010SChuck Lever 
10014cd34e7cSTrond Myklebust 	/* Close the stream if the previous transmission was incomplete */
10024cd34e7cSTrond Myklebust 	if (xs_send_request_was_aborted(transport, req)) {
10034cd34e7cSTrond Myklebust 		if (transport->sock != NULL)
10044cd34e7cSTrond Myklebust 			kernel_sock_shutdown(transport->sock, SHUT_RDWR);
10054cd34e7cSTrond Myklebust 		return -ENOTCONN;
10064cd34e7cSTrond Myklebust 	}
10074cd34e7cSTrond Myklebust 
100861677eeeSChuck Lever 	xs_encode_stream_record_marker(&req->rq_snd_buf);
1009262965f5SChuck Lever 
1010262965f5SChuck Lever 	xs_pktdump("packet data:",
1011262965f5SChuck Lever 				req->rq_svec->iov_base,
1012262965f5SChuck Lever 				req->rq_svec->iov_len);
1013a6b31d18STrond Myklebust 	/* Don't use zero copy if this is a resend. If the RPC call
1014a6b31d18STrond Myklebust 	 * completes while the socket holds a reference to the pages,
1015a6b31d18STrond Myklebust 	 * then we may end up resending corrupted data.
1016a6b31d18STrond Myklebust 	 */
101750f484e2STrond Myklebust 	if (req->rq_task->tk_flags & RPC_TASK_SENT)
1018a6b31d18STrond Myklebust 		zerocopy = false;
1019a246b010SChuck Lever 
10207196dbb0STrond Myklebust 	if (test_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state))
10217196dbb0STrond Myklebust 		xs_tcp_set_socket_timeouts(xprt, transport->sock);
10227196dbb0STrond Myklebust 
1023a246b010SChuck Lever 	/* Continue transmitting the packet/record. We must be careful
1024a246b010SChuck Lever 	 * to cope with writespace callbacks arriving _after_ we have
1025262965f5SChuck Lever 	 * called sendmsg(). */
102678215759SChuck Lever 	req->rq_xtime = ktime_get();
1027a246b010SChuck Lever 	while (1) {
1028f279cd00SJason Baron 		sent = 0;
1029f279cd00SJason Baron 		status = xs_sendpages(transport->sock, NULL, 0, xdr,
10306c7a64e5STrond Myklebust 				      transport->xmit.offset,
10316c7a64e5STrond Myklebust 				      zerocopy, &sent);
1032a246b010SChuck Lever 
1033262965f5SChuck Lever 		dprintk("RPC:       xs_tcp_send_request(%u) = %d\n",
10346c7a64e5STrond Myklebust 				xdr->len - transport->xmit.offset, status);
1035262965f5SChuck Lever 
1036a246b010SChuck Lever 		/* If we've sent the entire packet, immediately
1037a246b010SChuck Lever 		 * reset the count of bytes sent. */
10386c7a64e5STrond Myklebust 		transport->xmit.offset += sent;
10396c7a64e5STrond Myklebust 		req->rq_bytes_sent = transport->xmit.offset;
1040262965f5SChuck Lever 		if (likely(req->rq_bytes_sent >= req->rq_slen)) {
10416c7a64e5STrond Myklebust 			req->rq_xmit_bytes_sent += transport->xmit.offset;
1042a246b010SChuck Lever 			req->rq_bytes_sent = 0;
10436c7a64e5STrond Myklebust 			transport->xmit.offset = 0;
1044a246b010SChuck Lever 			return 0;
1045a246b010SChuck Lever 		}
1046262965f5SChuck Lever 
10479ffadfbcSTrond Myklebust 		WARN_ON_ONCE(sent == 0 && status == 0);
10489ffadfbcSTrond Myklebust 
10499ffadfbcSTrond Myklebust 		if (status == -EAGAIN ) {
10509ffadfbcSTrond Myklebust 			/*
10519ffadfbcSTrond Myklebust 			 * Return EAGAIN if we're sure we're hitting the
10529ffadfbcSTrond Myklebust 			 * socket send buffer limits.
10539ffadfbcSTrond Myklebust 			 */
10549ffadfbcSTrond Myklebust 			if (test_bit(SOCK_NOSPACE, &transport->sock->flags))
10559ffadfbcSTrond Myklebust 				break;
10569ffadfbcSTrond Myklebust 			/*
10579ffadfbcSTrond Myklebust 			 * Did we hit a memory allocation failure?
10589ffadfbcSTrond Myklebust 			 */
10599ffadfbcSTrond Myklebust 			if (sent == 0) {
10609ffadfbcSTrond Myklebust 				status = -ENOBUFS;
10619ffadfbcSTrond Myklebust 				if (vm_wait)
10629ffadfbcSTrond Myklebust 					break;
10639ffadfbcSTrond Myklebust 				/* Retry, knowing now that we're below the
10649ffadfbcSTrond Myklebust 				 * socket send buffer limit
10659ffadfbcSTrond Myklebust 				 */
10669ffadfbcSTrond Myklebust 				vm_wait = true;
10679ffadfbcSTrond Myklebust 			}
10689ffadfbcSTrond Myklebust 			continue;
10699ffadfbcSTrond Myklebust 		}
1070f580dd04STrond Myklebust 		if (status < 0)
1071f580dd04STrond Myklebust 			break;
10729ffadfbcSTrond Myklebust 		vm_wait = false;
1073a246b010SChuck Lever 	}
1074a246b010SChuck Lever 
1075262965f5SChuck Lever 	switch (status) {
1076fba91afbSTrond Myklebust 	case -ENOTSOCK:
1077fba91afbSTrond Myklebust 		status = -ENOTCONN;
1078fba91afbSTrond Myklebust 		/* Should we call xs_close() here? */
1079fba91afbSTrond Myklebust 		break;
1080262965f5SChuck Lever 	case -EAGAIN:
1081c544577dSTrond Myklebust 		status = xs_nospace(req);
1082262965f5SChuck Lever 		break;
1083262965f5SChuck Lever 	case -ECONNRESET:
1084262965f5SChuck Lever 	case -ECONNREFUSED:
1085262965f5SChuck Lever 	case -ENOTCONN:
10863913c78cSTrond Myklebust 	case -EADDRINUSE:
1087b5872f0cSTrond Myklebust 	case -ENOBUFS:
1088b9d2bb2eSTrond Myklebust 	case -EPIPE:
108913331a55STrond Myklebust 		break;
109013331a55STrond Myklebust 	default:
109113331a55STrond Myklebust 		dprintk("RPC:       sendmsg returned unrecognized error %d\n",
109213331a55STrond Myklebust 			-status);
1093a246b010SChuck Lever 	}
10945fe46e9dSBian Naimeng 
1095a246b010SChuck Lever 	return status;
1096a246b010SChuck Lever }
1097a246b010SChuck Lever 
10982a9e1cfaSTrond Myklebust static void xs_save_old_callbacks(struct sock_xprt *transport, struct sock *sk)
10992a9e1cfaSTrond Myklebust {
11002a9e1cfaSTrond Myklebust 	transport->old_data_ready = sk->sk_data_ready;
11012a9e1cfaSTrond Myklebust 	transport->old_state_change = sk->sk_state_change;
11022a9e1cfaSTrond Myklebust 	transport->old_write_space = sk->sk_write_space;
11032118071dSTrond Myklebust 	transport->old_error_report = sk->sk_error_report;
11042a9e1cfaSTrond Myklebust }
11052a9e1cfaSTrond Myklebust 
11062a9e1cfaSTrond Myklebust static void xs_restore_old_callbacks(struct sock_xprt *transport, struct sock *sk)
11072a9e1cfaSTrond Myklebust {
11082a9e1cfaSTrond Myklebust 	sk->sk_data_ready = transport->old_data_ready;
11092a9e1cfaSTrond Myklebust 	sk->sk_state_change = transport->old_state_change;
11102a9e1cfaSTrond Myklebust 	sk->sk_write_space = transport->old_write_space;
11112118071dSTrond Myklebust 	sk->sk_error_report = transport->old_error_report;
11122118071dSTrond Myklebust }
11132118071dSTrond Myklebust 
111442d42a5bSTrond Myklebust static void xs_sock_reset_state_flags(struct rpc_xprt *xprt)
111542d42a5bSTrond Myklebust {
111642d42a5bSTrond Myklebust 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
111742d42a5bSTrond Myklebust 
111842d42a5bSTrond Myklebust 	clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state);
111942d42a5bSTrond Myklebust }
112042d42a5bSTrond Myklebust 
1121b70ae915STrond Myklebust static void xs_sock_reset_connection_flags(struct rpc_xprt *xprt)
1122b70ae915STrond Myklebust {
1123b70ae915STrond Myklebust 	smp_mb__before_atomic();
1124b70ae915STrond Myklebust 	clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
1125b70ae915STrond Myklebust 	clear_bit(XPRT_CLOSING, &xprt->state);
112642d42a5bSTrond Myklebust 	xs_sock_reset_state_flags(xprt);
1127b70ae915STrond Myklebust 	smp_mb__after_atomic();
1128b70ae915STrond Myklebust }
1129b70ae915STrond Myklebust 
11302118071dSTrond Myklebust /**
11312118071dSTrond Myklebust  * xs_error_report - callback to handle TCP socket state errors
11322118071dSTrond Myklebust  * @sk: socket
11332118071dSTrond Myklebust  *
11342118071dSTrond Myklebust  * Note: we don't call sock_error() since there may be a rpc_task
11352118071dSTrond Myklebust  * using the socket, and so we don't want to clear sk->sk_err.
11362118071dSTrond Myklebust  */
11372118071dSTrond Myklebust static void xs_error_report(struct sock *sk)
11382118071dSTrond Myklebust {
11392118071dSTrond Myklebust 	struct rpc_xprt *xprt;
11402118071dSTrond Myklebust 	int err;
11412118071dSTrond Myklebust 
11422118071dSTrond Myklebust 	read_lock_bh(&sk->sk_callback_lock);
11432118071dSTrond Myklebust 	if (!(xprt = xprt_from_sock(sk)))
11442118071dSTrond Myklebust 		goto out;
11452118071dSTrond Myklebust 
11462118071dSTrond Myklebust 	err = -sk->sk_err;
11472118071dSTrond Myklebust 	if (err == 0)
11482118071dSTrond Myklebust 		goto out;
11492118071dSTrond Myklebust 	dprintk("RPC:       xs_error_report client %p, error=%d...\n",
11502118071dSTrond Myklebust 			xprt, -err);
1151e8353c76STrond Myklebust 	trace_rpc_socket_error(xprt, sk->sk_socket, err);
11522118071dSTrond Myklebust 	xprt_wake_pending_tasks(xprt, err);
11532118071dSTrond Myklebust  out:
11542118071dSTrond Myklebust 	read_unlock_bh(&sk->sk_callback_lock);
11552a9e1cfaSTrond Myklebust }
11562a9e1cfaSTrond Myklebust 
1157fe315e76SChuck Lever static void xs_reset_transport(struct sock_xprt *transport)
1158a246b010SChuck Lever {
1159ee0ac0c2SChuck Lever 	struct socket *sock = transport->sock;
1160ee0ac0c2SChuck Lever 	struct sock *sk = transport->inet;
11616cc7e908STrond Myklebust 	struct rpc_xprt *xprt = &transport->xprt;
1162a246b010SChuck Lever 
1163fe315e76SChuck Lever 	if (sk == NULL)
1164fe315e76SChuck Lever 		return;
11659903cd1cSChuck Lever 
1166264d1df3SJeff Layton 	if (atomic_read(&transport->xprt.swapper))
1167264d1df3SJeff Layton 		sk_clear_memalloc(sk);
1168264d1df3SJeff Layton 
116909939204STrond Myklebust 	kernel_sock_shutdown(sock, SHUT_RDWR);
117009939204STrond Myklebust 
1171edc1b01cSTrond Myklebust 	mutex_lock(&transport->recv_mutex);
1172a246b010SChuck Lever 	write_lock_bh(&sk->sk_callback_lock);
1173ee0ac0c2SChuck Lever 	transport->inet = NULL;
1174ee0ac0c2SChuck Lever 	transport->sock = NULL;
1175a246b010SChuck Lever 
1176a246b010SChuck Lever 	sk->sk_user_data = NULL;
11772a9e1cfaSTrond Myklebust 
11782a9e1cfaSTrond Myklebust 	xs_restore_old_callbacks(transport, sk);
11790c78789eSTrond Myklebust 	xprt_clear_connected(xprt);
1180a246b010SChuck Lever 	write_unlock_bh(&sk->sk_callback_lock);
11816cc7e908STrond Myklebust 	xs_sock_reset_connection_flags(xprt);
1182edc1b01cSTrond Myklebust 	mutex_unlock(&transport->recv_mutex);
1183a246b010SChuck Lever 
11846cc7e908STrond Myklebust 	trace_rpc_socket_close(xprt, sock);
1185a246b010SChuck Lever 	sock_release(sock);
1186fe315e76SChuck Lever }
1187fe315e76SChuck Lever 
1188fe315e76SChuck Lever /**
1189fe315e76SChuck Lever  * xs_close - close a socket
1190fe315e76SChuck Lever  * @xprt: transport
1191fe315e76SChuck Lever  *
1192fe315e76SChuck Lever  * This is used when all requests are complete; ie, no DRC state remains
1193fe315e76SChuck Lever  * on the server we want to save.
1194f75e6745STrond Myklebust  *
1195f75e6745STrond Myklebust  * The caller _must_ be holding XPRT_LOCKED in order to avoid issues with
1196f75e6745STrond Myklebust  * xs_reset_transport() zeroing the socket from underneath a writer.
1197fe315e76SChuck Lever  */
1198fe315e76SChuck Lever static void xs_close(struct rpc_xprt *xprt)
1199fe315e76SChuck Lever {
1200fe315e76SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1201fe315e76SChuck Lever 
1202fe315e76SChuck Lever 	dprintk("RPC:       xs_close xprt %p\n", xprt);
1203fe315e76SChuck Lever 
1204fe315e76SChuck Lever 	xs_reset_transport(transport);
120561d0a8e6SNeil Brown 	xprt->reestablish_timeout = 0;
1206fe315e76SChuck Lever 
120762da3b24STrond Myklebust 	xprt_disconnect_done(xprt);
1208a246b010SChuck Lever }
1209a246b010SChuck Lever 
12104a068258SChuck Lever static void xs_inject_disconnect(struct rpc_xprt *xprt)
12114a068258SChuck Lever {
12124a068258SChuck Lever 	dprintk("RPC:       injecting transport disconnect on xprt=%p\n",
12134a068258SChuck Lever 		xprt);
12144a068258SChuck Lever 	xprt_disconnect_done(xprt);
12154a068258SChuck Lever }
12164a068258SChuck Lever 
1217315f3812SKinglong Mee static void xs_xprt_free(struct rpc_xprt *xprt)
1218315f3812SKinglong Mee {
1219315f3812SKinglong Mee 	xs_free_peer_addresses(xprt);
1220315f3812SKinglong Mee 	xprt_free(xprt);
1221315f3812SKinglong Mee }
1222315f3812SKinglong Mee 
12239903cd1cSChuck Lever /**
12249903cd1cSChuck Lever  * xs_destroy - prepare to shutdown a transport
12259903cd1cSChuck Lever  * @xprt: doomed transport
12269903cd1cSChuck Lever  *
12279903cd1cSChuck Lever  */
12289903cd1cSChuck Lever static void xs_destroy(struct rpc_xprt *xprt)
1229a246b010SChuck Lever {
123003c78827STrond Myklebust 	struct sock_xprt *transport = container_of(xprt,
123103c78827STrond Myklebust 			struct sock_xprt, xprt);
12329903cd1cSChuck Lever 	dprintk("RPC:       xs_destroy xprt %p\n", xprt);
12339903cd1cSChuck Lever 
123403c78827STrond Myklebust 	cancel_delayed_work_sync(&transport->connect_worker);
1235a1311d87STrond Myklebust 	xs_close(xprt);
1236edc1b01cSTrond Myklebust 	cancel_work_sync(&transport->recv_worker);
1237315f3812SKinglong Mee 	xs_xprt_free(xprt);
1238a1311d87STrond Myklebust 	module_put(THIS_MODULE);
1239a246b010SChuck Lever }
1240a246b010SChuck Lever 
1241176e21eeSChuck Lever static int xs_local_copy_to_xdr(struct xdr_buf *xdr, struct sk_buff *skb)
1242176e21eeSChuck Lever {
1243176e21eeSChuck Lever 	struct xdr_skb_reader desc = {
1244176e21eeSChuck Lever 		.skb		= skb,
1245176e21eeSChuck Lever 		.offset		= sizeof(rpc_fraghdr),
1246176e21eeSChuck Lever 		.count		= skb->len - sizeof(rpc_fraghdr),
1247176e21eeSChuck Lever 	};
1248176e21eeSChuck Lever 
1249176e21eeSChuck Lever 	if (xdr_partial_copy_from_skb(xdr, 0, &desc, xdr_skb_read_bits) < 0)
1250176e21eeSChuck Lever 		return -1;
1251176e21eeSChuck Lever 	if (desc.count)
1252176e21eeSChuck Lever 		return -1;
1253176e21eeSChuck Lever 	return 0;
1254176e21eeSChuck Lever }
1255176e21eeSChuck Lever 
1256176e21eeSChuck Lever /**
1257a2648094STrond Myklebust  * xs_local_data_read_skb
1258a2648094STrond Myklebust  * @xprt: transport
1259a2648094STrond Myklebust  * @sk: socket
1260a2648094STrond Myklebust  * @skb: skbuff
1261176e21eeSChuck Lever  *
1262176e21eeSChuck Lever  * Currently this assumes we can read the whole reply in a single gulp.
1263176e21eeSChuck Lever  */
1264a2648094STrond Myklebust static void xs_local_data_read_skb(struct rpc_xprt *xprt,
1265a2648094STrond Myklebust 		struct sock *sk,
1266a2648094STrond Myklebust 		struct sk_buff *skb)
1267176e21eeSChuck Lever {
1268176e21eeSChuck Lever 	struct rpc_task *task;
1269176e21eeSChuck Lever 	struct rpc_rqst *rovr;
1270a2648094STrond Myklebust 	int repsize, copied;
1271176e21eeSChuck Lever 	u32 _xid;
1272176e21eeSChuck Lever 	__be32 *xp;
1273176e21eeSChuck Lever 
1274176e21eeSChuck Lever 	repsize = skb->len - sizeof(rpc_fraghdr);
1275176e21eeSChuck Lever 	if (repsize < 4) {
1276176e21eeSChuck Lever 		dprintk("RPC:       impossible RPC reply size %d\n", repsize);
1277a2648094STrond Myklebust 		return;
1278176e21eeSChuck Lever 	}
1279176e21eeSChuck Lever 
1280176e21eeSChuck Lever 	/* Copy the XID from the skb... */
1281176e21eeSChuck Lever 	xp = skb_header_pointer(skb, sizeof(rpc_fraghdr), sizeof(_xid), &_xid);
1282176e21eeSChuck Lever 	if (xp == NULL)
1283a2648094STrond Myklebust 		return;
1284176e21eeSChuck Lever 
1285176e21eeSChuck Lever 	/* Look up and lock the request corresponding to the given XID */
128675c84151STrond Myklebust 	spin_lock(&xprt->queue_lock);
1287176e21eeSChuck Lever 	rovr = xprt_lookup_rqst(xprt, *xp);
1288176e21eeSChuck Lever 	if (!rovr)
1289176e21eeSChuck Lever 		goto out_unlock;
1290729749bbSTrond Myklebust 	xprt_pin_rqst(rovr);
129175c84151STrond Myklebust 	spin_unlock(&xprt->queue_lock);
1292176e21eeSChuck Lever 	task = rovr->rq_task;
1293176e21eeSChuck Lever 
1294176e21eeSChuck Lever 	copied = rovr->rq_private_buf.buflen;
1295176e21eeSChuck Lever 	if (copied > repsize)
1296176e21eeSChuck Lever 		copied = repsize;
1297176e21eeSChuck Lever 
1298176e21eeSChuck Lever 	if (xs_local_copy_to_xdr(&rovr->rq_private_buf, skb)) {
1299176e21eeSChuck Lever 		dprintk("RPC:       sk_buff copy failed\n");
130075c84151STrond Myklebust 		spin_lock(&xprt->queue_lock);
1301729749bbSTrond Myklebust 		goto out_unpin;
1302176e21eeSChuck Lever 	}
1303176e21eeSChuck Lever 
130475c84151STrond Myklebust 	spin_lock(&xprt->queue_lock);
1305176e21eeSChuck Lever 	xprt_complete_rqst(task, copied);
1306729749bbSTrond Myklebust out_unpin:
1307729749bbSTrond Myklebust 	xprt_unpin_rqst(rovr);
1308176e21eeSChuck Lever  out_unlock:
130975c84151STrond Myklebust 	spin_unlock(&xprt->queue_lock);
1310a2648094STrond Myklebust }
1311a2648094STrond Myklebust 
1312a2648094STrond Myklebust static void xs_local_data_receive(struct sock_xprt *transport)
1313a2648094STrond Myklebust {
1314a2648094STrond Myklebust 	struct sk_buff *skb;
1315a2648094STrond Myklebust 	struct sock *sk;
1316a2648094STrond Myklebust 	int err;
1317a2648094STrond Myklebust 
13180af3442aSTrond Myklebust restart:
1319a2648094STrond Myklebust 	mutex_lock(&transport->recv_mutex);
1320a2648094STrond Myklebust 	sk = transport->inet;
1321a2648094STrond Myklebust 	if (sk == NULL)
1322a2648094STrond Myklebust 		goto out;
1323a2648094STrond Myklebust 	for (;;) {
1324a2648094STrond Myklebust 		skb = skb_recv_datagram(sk, 0, 1, &err);
132542d42a5bSTrond Myklebust 		if (skb != NULL) {
1326a2648094STrond Myklebust 			xs_local_data_read_skb(&transport->xprt, sk, skb);
1327176e21eeSChuck Lever 			skb_free_datagram(sk, skb);
132842d42a5bSTrond Myklebust 			continue;
132942d42a5bSTrond Myklebust 		}
133042d42a5bSTrond Myklebust 		if (!test_and_clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state))
133142d42a5bSTrond Myklebust 			break;
13320af3442aSTrond Myklebust 		if (need_resched()) {
13330af3442aSTrond Myklebust 			mutex_unlock(&transport->recv_mutex);
13340af3442aSTrond Myklebust 			cond_resched();
13350af3442aSTrond Myklebust 			goto restart;
13360af3442aSTrond Myklebust 		}
1337a2648094STrond Myklebust 	}
1338176e21eeSChuck Lever out:
1339a2648094STrond Myklebust 	mutex_unlock(&transport->recv_mutex);
1340a2648094STrond Myklebust }
1341a2648094STrond Myklebust 
1342a2648094STrond Myklebust static void xs_local_data_receive_workfn(struct work_struct *work)
1343a2648094STrond Myklebust {
1344a2648094STrond Myklebust 	struct sock_xprt *transport =
1345a2648094STrond Myklebust 		container_of(work, struct sock_xprt, recv_worker);
1346a2648094STrond Myklebust 	xs_local_data_receive(transport);
1347176e21eeSChuck Lever }
1348176e21eeSChuck Lever 
13499903cd1cSChuck Lever /**
1350f9b2ee71STrond Myklebust  * xs_udp_data_read_skb - receive callback for UDP sockets
1351f9b2ee71STrond Myklebust  * @xprt: transport
1352f9b2ee71STrond Myklebust  * @sk: socket
1353f9b2ee71STrond Myklebust  * @skb: skbuff
13549903cd1cSChuck Lever  *
1355a246b010SChuck Lever  */
1356f9b2ee71STrond Myklebust static void xs_udp_data_read_skb(struct rpc_xprt *xprt,
1357f9b2ee71STrond Myklebust 		struct sock *sk,
1358f9b2ee71STrond Myklebust 		struct sk_buff *skb)
1359a246b010SChuck Lever {
1360a246b010SChuck Lever 	struct rpc_task *task;
1361a246b010SChuck Lever 	struct rpc_rqst *rovr;
1362f9b2ee71STrond Myklebust 	int repsize, copied;
1363d8ed029dSAlexey Dobriyan 	u32 _xid;
1364d8ed029dSAlexey Dobriyan 	__be32 *xp;
1365a246b010SChuck Lever 
13661da8c681SWillem de Bruijn 	repsize = skb->len;
1367a246b010SChuck Lever 	if (repsize < 4) {
13689903cd1cSChuck Lever 		dprintk("RPC:       impossible RPC reply size %d!\n", repsize);
1369f9b2ee71STrond Myklebust 		return;
1370a246b010SChuck Lever 	}
1371a246b010SChuck Lever 
1372a246b010SChuck Lever 	/* Copy the XID from the skb... */
13731da8c681SWillem de Bruijn 	xp = skb_header_pointer(skb, 0, sizeof(_xid), &_xid);
1374a246b010SChuck Lever 	if (xp == NULL)
1375f9b2ee71STrond Myklebust 		return;
1376a246b010SChuck Lever 
1377a246b010SChuck Lever 	/* Look up and lock the request corresponding to the given XID */
137875c84151STrond Myklebust 	spin_lock(&xprt->queue_lock);
1379a246b010SChuck Lever 	rovr = xprt_lookup_rqst(xprt, *xp);
1380a246b010SChuck Lever 	if (!rovr)
1381a246b010SChuck Lever 		goto out_unlock;
1382729749bbSTrond Myklebust 	xprt_pin_rqst(rovr);
1383ecd465eeSChuck Lever 	xprt_update_rtt(rovr->rq_task);
138475c84151STrond Myklebust 	spin_unlock(&xprt->queue_lock);
1385a246b010SChuck Lever 	task = rovr->rq_task;
1386a246b010SChuck Lever 
1387a246b010SChuck Lever 	if ((copied = rovr->rq_private_buf.buflen) > repsize)
1388a246b010SChuck Lever 		copied = repsize;
1389a246b010SChuck Lever 
1390a246b010SChuck Lever 	/* Suck it into the iovec, verify checksum if not done by hw. */
13911781f7f5SHerbert Xu 	if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) {
139275c84151STrond Myklebust 		spin_lock(&xprt->queue_lock);
13930afa6b44STrond Myklebust 		__UDPX_INC_STATS(sk, UDP_MIB_INERRORS);
1394729749bbSTrond Myklebust 		goto out_unpin;
13951781f7f5SHerbert Xu 	}
13961781f7f5SHerbert Xu 
1397a246b010SChuck Lever 
1398729749bbSTrond Myklebust 	spin_lock_bh(&xprt->transport_lock);
13996a24dfb6STrond Myklebust 	xprt_adjust_cwnd(xprt, task, copied);
1400ce7c252aSTrond Myklebust 	spin_unlock_bh(&xprt->transport_lock);
140175c84151STrond Myklebust 	spin_lock(&xprt->queue_lock);
14021570c1e4SChuck Lever 	xprt_complete_rqst(task, copied);
14030afa6b44STrond Myklebust 	__UDPX_INC_STATS(sk, UDP_MIB_INDATAGRAMS);
1404729749bbSTrond Myklebust out_unpin:
1405729749bbSTrond Myklebust 	xprt_unpin_rqst(rovr);
1406a246b010SChuck Lever  out_unlock:
140775c84151STrond Myklebust 	spin_unlock(&xprt->queue_lock);
1408f9b2ee71STrond Myklebust }
1409f9b2ee71STrond Myklebust 
1410f9b2ee71STrond Myklebust static void xs_udp_data_receive(struct sock_xprt *transport)
1411f9b2ee71STrond Myklebust {
1412f9b2ee71STrond Myklebust 	struct sk_buff *skb;
1413f9b2ee71STrond Myklebust 	struct sock *sk;
1414f9b2ee71STrond Myklebust 	int err;
1415f9b2ee71STrond Myklebust 
14160af3442aSTrond Myklebust restart:
1417f9b2ee71STrond Myklebust 	mutex_lock(&transport->recv_mutex);
1418f9b2ee71STrond Myklebust 	sk = transport->inet;
1419f9b2ee71STrond Myklebust 	if (sk == NULL)
1420f9b2ee71STrond Myklebust 		goto out;
1421f9b2ee71STrond Myklebust 	for (;;) {
14227c13f97fSPaolo Abeni 		skb = skb_recv_udp(sk, 0, 1, &err);
142342d42a5bSTrond Myklebust 		if (skb != NULL) {
1424f9b2ee71STrond Myklebust 			xs_udp_data_read_skb(&transport->xprt, sk, skb);
1425850cbaddSPaolo Abeni 			consume_skb(skb);
142642d42a5bSTrond Myklebust 			continue;
142742d42a5bSTrond Myklebust 		}
142842d42a5bSTrond Myklebust 		if (!test_and_clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state))
142942d42a5bSTrond Myklebust 			break;
14300af3442aSTrond Myklebust 		if (need_resched()) {
14310af3442aSTrond Myklebust 			mutex_unlock(&transport->recv_mutex);
14320af3442aSTrond Myklebust 			cond_resched();
14330af3442aSTrond Myklebust 			goto restart;
14340af3442aSTrond Myklebust 		}
1435f9b2ee71STrond Myklebust 	}
1436a246b010SChuck Lever out:
1437f9b2ee71STrond Myklebust 	mutex_unlock(&transport->recv_mutex);
1438f9b2ee71STrond Myklebust }
1439f9b2ee71STrond Myklebust 
1440f9b2ee71STrond Myklebust static void xs_udp_data_receive_workfn(struct work_struct *work)
1441f9b2ee71STrond Myklebust {
1442f9b2ee71STrond Myklebust 	struct sock_xprt *transport =
1443f9b2ee71STrond Myklebust 		container_of(work, struct sock_xprt, recv_worker);
1444f9b2ee71STrond Myklebust 	xs_udp_data_receive(transport);
1445f9b2ee71STrond Myklebust }
1446f9b2ee71STrond Myklebust 
1447f9b2ee71STrond Myklebust /**
1448f9b2ee71STrond Myklebust  * xs_data_ready - "data ready" callback for UDP sockets
1449f9b2ee71STrond Myklebust  * @sk: socket with data to read
1450f9b2ee71STrond Myklebust  *
1451f9b2ee71STrond Myklebust  */
1452f9b2ee71STrond Myklebust static void xs_data_ready(struct sock *sk)
1453f9b2ee71STrond Myklebust {
1454f9b2ee71STrond Myklebust 	struct rpc_xprt *xprt;
1455f9b2ee71STrond Myklebust 
1456f9b2ee71STrond Myklebust 	read_lock_bh(&sk->sk_callback_lock);
1457f9b2ee71STrond Myklebust 	dprintk("RPC:       xs_data_ready...\n");
1458f9b2ee71STrond Myklebust 	xprt = xprt_from_sock(sk);
1459f9b2ee71STrond Myklebust 	if (xprt != NULL) {
1460f9b2ee71STrond Myklebust 		struct sock_xprt *transport = container_of(xprt,
1461f9b2ee71STrond Myklebust 				struct sock_xprt, xprt);
14625157b956STrond Myklebust 		transport->old_data_ready(sk);
14635157b956STrond Myklebust 		/* Any data means we had a useful conversation, so
14645157b956STrond Myklebust 		 * then we don't need to delay the next reconnect
14655157b956STrond Myklebust 		 */
14665157b956STrond Myklebust 		if (xprt->reestablish_timeout)
14675157b956STrond Myklebust 			xprt->reestablish_timeout = 0;
146842d42a5bSTrond Myklebust 		if (!test_and_set_bit(XPRT_SOCK_DATA_READY, &transport->sock_state))
146940a5f1b1STrond Myklebust 			queue_work(xprtiod_workqueue, &transport->recv_worker);
1470f9b2ee71STrond Myklebust 	}
1471f064af1eSEric Dumazet 	read_unlock_bh(&sk->sk_callback_lock);
1472a246b010SChuck Lever }
1473a246b010SChuck Lever 
1474a519fc7aSTrond Myklebust /*
1475a519fc7aSTrond Myklebust  * Helper function to force a TCP close if the server is sending
1476a519fc7aSTrond Myklebust  * junk and/or it has put us in CLOSE_WAIT
1477a519fc7aSTrond Myklebust  */
1478a519fc7aSTrond Myklebust static void xs_tcp_force_close(struct rpc_xprt *xprt)
1479a519fc7aSTrond Myklebust {
1480a519fc7aSTrond Myklebust 	xprt_force_disconnect(xprt);
1481a519fc7aSTrond Myklebust }
1482a519fc7aSTrond Myklebust 
14839e00abc3STrond Myklebust #if defined(CONFIG_SUNRPC_BACKCHANNEL)
148476566773SChuck Lever static int xs_tcp_bc_up(struct svc_serv *serv, struct net *net)
148576566773SChuck Lever {
148676566773SChuck Lever 	int ret;
148776566773SChuck Lever 
148876566773SChuck Lever 	ret = svc_create_xprt(serv, "tcp-bc", net, PF_INET, 0,
148976566773SChuck Lever 			      SVC_SOCK_ANONYMOUS);
149076566773SChuck Lever 	if (ret < 0)
149176566773SChuck Lever 		return ret;
149276566773SChuck Lever 	return 0;
149376566773SChuck Lever }
14946b26cc8cSChuck Lever 
14956b26cc8cSChuck Lever static size_t xs_tcp_bc_maxpayload(struct rpc_xprt *xprt)
14966b26cc8cSChuck Lever {
14976b26cc8cSChuck Lever 	return PAGE_SIZE;
14986b26cc8cSChuck Lever }
14999e00abc3STrond Myklebust #endif /* CONFIG_SUNRPC_BACKCHANNEL */
150044b98efdSRicardo Labiaga 
150166d7a56aSTrond Myklebust static void xs_tcp_data_receive(struct sock_xprt *transport)
150266d7a56aSTrond Myklebust {
150366d7a56aSTrond Myklebust 	struct rpc_xprt *xprt = &transport->xprt;
150466d7a56aSTrond Myklebust 	struct sock *sk;
1505*277e4ab7STrond Myklebust 	size_t read = 0;
1506*277e4ab7STrond Myklebust 	ssize_t ret = 0;
150766d7a56aSTrond Myklebust 
15080af3442aSTrond Myklebust restart:
1509edc1b01cSTrond Myklebust 	mutex_lock(&transport->recv_mutex);
151066d7a56aSTrond Myklebust 	sk = transport->inet;
1511edc1b01cSTrond Myklebust 	if (sk == NULL)
1512edc1b01cSTrond Myklebust 		goto out;
151366d7a56aSTrond Myklebust 
15140af3442aSTrond Myklebust 	for (;;) {
151542d42a5bSTrond Myklebust 		clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state);
1516*277e4ab7STrond Myklebust 		ret = xs_read_stream(transport, MSG_DONTWAIT | MSG_NOSIGNAL);
1517*277e4ab7STrond Myklebust 		if (ret < 0)
151866d7a56aSTrond Myklebust 			break;
1519*277e4ab7STrond Myklebust 		read += ret;
15200af3442aSTrond Myklebust 		if (need_resched()) {
15210af3442aSTrond Myklebust 			mutex_unlock(&transport->recv_mutex);
15220af3442aSTrond Myklebust 			cond_resched();
15230af3442aSTrond Myklebust 			goto restart;
15240af3442aSTrond Myklebust 		}
152566d7a56aSTrond Myklebust 	}
15268d6f97d6STrond Myklebust 	if (test_bit(XPRT_SOCK_DATA_READY, &transport->sock_state))
15278d6f97d6STrond Myklebust 		queue_work(xprtiod_workqueue, &transport->recv_worker);
1528edc1b01cSTrond Myklebust out:
1529edc1b01cSTrond Myklebust 	mutex_unlock(&transport->recv_mutex);
1530*277e4ab7STrond Myklebust 	trace_xs_tcp_data_ready(xprt, ret, read);
153166d7a56aSTrond Myklebust }
153266d7a56aSTrond Myklebust 
1533edc1b01cSTrond Myklebust static void xs_tcp_data_receive_workfn(struct work_struct *work)
1534edc1b01cSTrond Myklebust {
1535edc1b01cSTrond Myklebust 	struct sock_xprt *transport =
1536edc1b01cSTrond Myklebust 		container_of(work, struct sock_xprt, recv_worker);
1537edc1b01cSTrond Myklebust 	xs_tcp_data_receive(transport);
1538edc1b01cSTrond Myklebust }
1539edc1b01cSTrond Myklebust 
15409903cd1cSChuck Lever /**
15419903cd1cSChuck Lever  * xs_tcp_state_change - callback to handle TCP socket state changes
15429903cd1cSChuck Lever  * @sk: socket whose state has changed
15439903cd1cSChuck Lever  *
15449903cd1cSChuck Lever  */
15459903cd1cSChuck Lever static void xs_tcp_state_change(struct sock *sk)
1546a246b010SChuck Lever {
1547a246b010SChuck Lever 	struct rpc_xprt *xprt;
15480fdea1e8STrond Myklebust 	struct sock_xprt *transport;
1549a246b010SChuck Lever 
1550f064af1eSEric Dumazet 	read_lock_bh(&sk->sk_callback_lock);
1551a246b010SChuck Lever 	if (!(xprt = xprt_from_sock(sk)))
1552a246b010SChuck Lever 		goto out;
15539903cd1cSChuck Lever 	dprintk("RPC:       xs_tcp_state_change client %p...\n", xprt);
1554669502ffSAndy Chittenden 	dprintk("RPC:       state %x conn %d dead %d zapped %d sk_shutdown %d\n",
1555a246b010SChuck Lever 			sk->sk_state, xprt_connected(xprt),
1556a246b010SChuck Lever 			sock_flag(sk, SOCK_DEAD),
1557669502ffSAndy Chittenden 			sock_flag(sk, SOCK_ZAPPED),
1558669502ffSAndy Chittenden 			sk->sk_shutdown);
1559a246b010SChuck Lever 
15600fdea1e8STrond Myklebust 	transport = container_of(xprt, struct sock_xprt, xprt);
156140b5ea0cSTrond Myklebust 	trace_rpc_socket_state_change(xprt, sk->sk_socket);
1562a246b010SChuck Lever 	switch (sk->sk_state) {
1563a246b010SChuck Lever 	case TCP_ESTABLISHED:
1564f064af1eSEric Dumazet 		spin_lock(&xprt->transport_lock);
1565a246b010SChuck Lever 		if (!xprt_test_and_set_connected(xprt)) {
15668b71798cSTrond Myklebust 			xprt->connect_cookie++;
15670fdea1e8STrond Myklebust 			clear_bit(XPRT_SOCK_CONNECTING, &transport->sock_state);
15680fdea1e8STrond Myklebust 			xprt_clear_connecting(xprt);
156951971139SChuck Lever 
15702a491991STrond Myklebust 			xprt_wake_pending_tasks(xprt, -EAGAIN);
1571a246b010SChuck Lever 		}
1572f064af1eSEric Dumazet 		spin_unlock(&xprt->transport_lock);
1573a246b010SChuck Lever 		break;
15743b948ae5STrond Myklebust 	case TCP_FIN_WAIT1:
15753b948ae5STrond Myklebust 		/* The client initiated a shutdown of the socket */
15767c1d71cfSTrond Myklebust 		xprt->connect_cookie++;
1577663b8858STrond Myklebust 		xprt->reestablish_timeout = 0;
15783b948ae5STrond Myklebust 		set_bit(XPRT_CLOSING, &xprt->state);
15794e857c58SPeter Zijlstra 		smp_mb__before_atomic();
15803b948ae5STrond Myklebust 		clear_bit(XPRT_CONNECTED, &xprt->state);
1581ef803670STrond Myklebust 		clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
15824e857c58SPeter Zijlstra 		smp_mb__after_atomic();
1583a246b010SChuck Lever 		break;
1584632e3bdcSTrond Myklebust 	case TCP_CLOSE_WAIT:
15853b948ae5STrond Myklebust 		/* The server initiated a shutdown of the socket */
15867c1d71cfSTrond Myklebust 		xprt->connect_cookie++;
1587d0bea455STrond Myklebust 		clear_bit(XPRT_CONNECTED, &xprt->state);
1588a519fc7aSTrond Myklebust 		xs_tcp_force_close(xprt);
1589e9d47639SGustavo A. R. Silva 		/* fall through */
1590663b8858STrond Myklebust 	case TCP_CLOSING:
1591663b8858STrond Myklebust 		/*
1592663b8858STrond Myklebust 		 * If the server closed down the connection, make sure that
1593663b8858STrond Myklebust 		 * we back off before reconnecting
1594663b8858STrond Myklebust 		 */
1595663b8858STrond Myklebust 		if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
1596663b8858STrond Myklebust 			xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
15973b948ae5STrond Myklebust 		break;
15983b948ae5STrond Myklebust 	case TCP_LAST_ACK:
1599670f9457STrond Myklebust 		set_bit(XPRT_CLOSING, &xprt->state);
16004e857c58SPeter Zijlstra 		smp_mb__before_atomic();
16013b948ae5STrond Myklebust 		clear_bit(XPRT_CONNECTED, &xprt->state);
16024e857c58SPeter Zijlstra 		smp_mb__after_atomic();
16033b948ae5STrond Myklebust 		break;
16043b948ae5STrond Myklebust 	case TCP_CLOSE:
16050fdea1e8STrond Myklebust 		if (test_and_clear_bit(XPRT_SOCK_CONNECTING,
16060fdea1e8STrond Myklebust 					&transport->sock_state))
16070fdea1e8STrond Myklebust 			xprt_clear_connecting(xprt);
16089b30889cSTrond Myklebust 		clear_bit(XPRT_CLOSING, &xprt->state);
16093ffbc1d6SNeilBrown 		if (sk->sk_err)
16103ffbc1d6SNeilBrown 			xprt_wake_pending_tasks(xprt, -sk->sk_err);
16119b30889cSTrond Myklebust 		/* Trigger the socket release */
16129b30889cSTrond Myklebust 		xs_tcp_force_close(xprt);
1613a246b010SChuck Lever 	}
1614a246b010SChuck Lever  out:
1615f064af1eSEric Dumazet 	read_unlock_bh(&sk->sk_callback_lock);
1616a246b010SChuck Lever }
1617a246b010SChuck Lever 
16181f0fa154SIlpo Järvinen static void xs_write_space(struct sock *sk)
16191f0fa154SIlpo Järvinen {
162013331a55STrond Myklebust 	struct socket_wq *wq;
16211f0fa154SIlpo Järvinen 	struct rpc_xprt *xprt;
16221f0fa154SIlpo Järvinen 
162313331a55STrond Myklebust 	if (!sk->sk_socket)
16241f0fa154SIlpo Järvinen 		return;
162513331a55STrond Myklebust 	clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
16261f0fa154SIlpo Järvinen 
16271f0fa154SIlpo Järvinen 	if (unlikely(!(xprt = xprt_from_sock(sk))))
16281f0fa154SIlpo Järvinen 		return;
162913331a55STrond Myklebust 	rcu_read_lock();
163013331a55STrond Myklebust 	wq = rcu_dereference(sk->sk_wq);
163113331a55STrond Myklebust 	if (!wq || test_and_clear_bit(SOCKWQ_ASYNC_NOSPACE, &wq->flags) == 0)
163213331a55STrond Myklebust 		goto out;
16331f0fa154SIlpo Järvinen 
1634c544577dSTrond Myklebust 	if (xprt_write_space(xprt))
1635c544577dSTrond Myklebust 		sk->sk_write_pending--;
163613331a55STrond Myklebust out:
163713331a55STrond Myklebust 	rcu_read_unlock();
16381f0fa154SIlpo Järvinen }
16391f0fa154SIlpo Järvinen 
16402a9e1cfaSTrond Myklebust /**
1641c7b2cae8SChuck Lever  * xs_udp_write_space - callback invoked when socket buffer space
1642c7b2cae8SChuck Lever  *                             becomes available
16439903cd1cSChuck Lever  * @sk: socket whose state has changed
16449903cd1cSChuck Lever  *
1645a246b010SChuck Lever  * Called when more output buffer space is available for this socket.
1646a246b010SChuck Lever  * We try not to wake our writers until they can make "significant"
1647c7b2cae8SChuck Lever  * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1648a246b010SChuck Lever  * with a bunch of small requests.
1649a246b010SChuck Lever  */
1650c7b2cae8SChuck Lever static void xs_udp_write_space(struct sock *sk)
1651a246b010SChuck Lever {
1652f064af1eSEric Dumazet 	read_lock_bh(&sk->sk_callback_lock);
1653c7b2cae8SChuck Lever 
1654c7b2cae8SChuck Lever 	/* from net/core/sock.c:sock_def_write_space */
16551f0fa154SIlpo Järvinen 	if (sock_writeable(sk))
16561f0fa154SIlpo Järvinen 		xs_write_space(sk);
1657c7b2cae8SChuck Lever 
1658f064af1eSEric Dumazet 	read_unlock_bh(&sk->sk_callback_lock);
1659c7b2cae8SChuck Lever }
1660c7b2cae8SChuck Lever 
1661c7b2cae8SChuck Lever /**
1662c7b2cae8SChuck Lever  * xs_tcp_write_space - callback invoked when socket buffer space
1663c7b2cae8SChuck Lever  *                             becomes available
1664c7b2cae8SChuck Lever  * @sk: socket whose state has changed
1665c7b2cae8SChuck Lever  *
1666c7b2cae8SChuck Lever  * Called when more output buffer space is available for this socket.
1667c7b2cae8SChuck Lever  * We try not to wake our writers until they can make "significant"
1668c7b2cae8SChuck Lever  * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1669c7b2cae8SChuck Lever  * with a bunch of small requests.
1670c7b2cae8SChuck Lever  */
1671c7b2cae8SChuck Lever static void xs_tcp_write_space(struct sock *sk)
1672c7b2cae8SChuck Lever {
1673f064af1eSEric Dumazet 	read_lock_bh(&sk->sk_callback_lock);
1674c7b2cae8SChuck Lever 
1675c7b2cae8SChuck Lever 	/* from net/core/stream.c:sk_stream_write_space */
167664dc6130SEric Dumazet 	if (sk_stream_is_writeable(sk))
16771f0fa154SIlpo Järvinen 		xs_write_space(sk);
1678c7b2cae8SChuck Lever 
1679f064af1eSEric Dumazet 	read_unlock_bh(&sk->sk_callback_lock);
1680a246b010SChuck Lever }
1681a246b010SChuck Lever 
1682470056c2SChuck Lever static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt)
1683a246b010SChuck Lever {
1684ee0ac0c2SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1685ee0ac0c2SChuck Lever 	struct sock *sk = transport->inet;
1686a246b010SChuck Lever 
16877c6e066eSChuck Lever 	if (transport->rcvsize) {
1688a246b010SChuck Lever 		sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
16897c6e066eSChuck Lever 		sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2;
1690a246b010SChuck Lever 	}
16917c6e066eSChuck Lever 	if (transport->sndsize) {
1692a246b010SChuck Lever 		sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
16937c6e066eSChuck Lever 		sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2;
1694a246b010SChuck Lever 		sk->sk_write_space(sk);
1695a246b010SChuck Lever 	}
1696a246b010SChuck Lever }
1697a246b010SChuck Lever 
169843118c29SChuck Lever /**
1699470056c2SChuck Lever  * xs_udp_set_buffer_size - set send and receive limits
170043118c29SChuck Lever  * @xprt: generic transport
1701470056c2SChuck Lever  * @sndsize: requested size of send buffer, in bytes
1702470056c2SChuck Lever  * @rcvsize: requested size of receive buffer, in bytes
170343118c29SChuck Lever  *
1704470056c2SChuck Lever  * Set socket send and receive buffer size limits.
170543118c29SChuck Lever  */
1706470056c2SChuck Lever static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize)
170743118c29SChuck Lever {
17087c6e066eSChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
17097c6e066eSChuck Lever 
17107c6e066eSChuck Lever 	transport->sndsize = 0;
1711470056c2SChuck Lever 	if (sndsize)
17127c6e066eSChuck Lever 		transport->sndsize = sndsize + 1024;
17137c6e066eSChuck Lever 	transport->rcvsize = 0;
1714470056c2SChuck Lever 	if (rcvsize)
17157c6e066eSChuck Lever 		transport->rcvsize = rcvsize + 1024;
1716470056c2SChuck Lever 
1717470056c2SChuck Lever 	xs_udp_do_set_buffer_size(xprt);
171843118c29SChuck Lever }
171943118c29SChuck Lever 
172046c0ee8bSChuck Lever /**
172146c0ee8bSChuck Lever  * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
172246c0ee8bSChuck Lever  * @task: task that timed out
172346c0ee8bSChuck Lever  *
172446c0ee8bSChuck Lever  * Adjust the congestion window after a retransmit timeout has occurred.
172546c0ee8bSChuck Lever  */
17266a24dfb6STrond Myklebust static void xs_udp_timer(struct rpc_xprt *xprt, struct rpc_task *task)
172746c0ee8bSChuck Lever {
1728b977b644SChuck Lever 	spin_lock_bh(&xprt->transport_lock);
17296a24dfb6STrond Myklebust 	xprt_adjust_cwnd(xprt, task, -ETIMEDOUT);
1730b977b644SChuck Lever 	spin_unlock_bh(&xprt->transport_lock);
173146c0ee8bSChuck Lever }
173246c0ee8bSChuck Lever 
1733b85d8806SChuck Lever static unsigned short xs_get_random_port(void)
1734b85d8806SChuck Lever {
17355d71899aSFrank Sorenson 	unsigned short range = xprt_max_resvport - xprt_min_resvport + 1;
173663862b5bSAruna-Hewapathirane 	unsigned short rand = (unsigned short) prandom_u32() % range;
1737b85d8806SChuck Lever 	return rand + xprt_min_resvport;
1738b85d8806SChuck Lever }
1739b85d8806SChuck Lever 
174092200412SChuck Lever /**
17414dda9c8aSTrond Myklebust  * xs_set_reuseaddr_port - set the socket's port and address reuse options
17424dda9c8aSTrond Myklebust  * @sock: socket
17434dda9c8aSTrond Myklebust  *
17444dda9c8aSTrond Myklebust  * Note that this function has to be called on all sockets that share the
17454dda9c8aSTrond Myklebust  * same port, and it must be called before binding.
17464dda9c8aSTrond Myklebust  */
17474dda9c8aSTrond Myklebust static void xs_sock_set_reuseport(struct socket *sock)
17484dda9c8aSTrond Myklebust {
1749402e23b4STrond Myklebust 	int opt = 1;
17504dda9c8aSTrond Myklebust 
1751402e23b4STrond Myklebust 	kernel_setsockopt(sock, SOL_SOCKET, SO_REUSEPORT,
1752402e23b4STrond Myklebust 			(char *)&opt, sizeof(opt));
17534dda9c8aSTrond Myklebust }
17544dda9c8aSTrond Myklebust 
17554dda9c8aSTrond Myklebust static unsigned short xs_sock_getport(struct socket *sock)
17564dda9c8aSTrond Myklebust {
17574dda9c8aSTrond Myklebust 	struct sockaddr_storage buf;
17584dda9c8aSTrond Myklebust 	unsigned short port = 0;
17594dda9c8aSTrond Myklebust 
17609b2c45d4SDenys Vlasenko 	if (kernel_getsockname(sock, (struct sockaddr *)&buf) < 0)
17614dda9c8aSTrond Myklebust 		goto out;
17624dda9c8aSTrond Myklebust 	switch (buf.ss_family) {
17634dda9c8aSTrond Myklebust 	case AF_INET6:
17644dda9c8aSTrond Myklebust 		port = ntohs(((struct sockaddr_in6 *)&buf)->sin6_port);
17654dda9c8aSTrond Myklebust 		break;
17664dda9c8aSTrond Myklebust 	case AF_INET:
17674dda9c8aSTrond Myklebust 		port = ntohs(((struct sockaddr_in *)&buf)->sin_port);
17684dda9c8aSTrond Myklebust 	}
17694dda9c8aSTrond Myklebust out:
17704dda9c8aSTrond Myklebust 	return port;
17714dda9c8aSTrond Myklebust }
17724dda9c8aSTrond Myklebust 
17734dda9c8aSTrond Myklebust /**
177492200412SChuck Lever  * xs_set_port - reset the port number in the remote endpoint address
177592200412SChuck Lever  * @xprt: generic transport
177692200412SChuck Lever  * @port: new port number
177792200412SChuck Lever  *
177892200412SChuck Lever  */
177992200412SChuck Lever static void xs_set_port(struct rpc_xprt *xprt, unsigned short port)
178092200412SChuck Lever {
178192200412SChuck Lever 	dprintk("RPC:       setting port for xprt %p to %u\n", xprt, port);
1782c4efcb1dSChuck Lever 
17839dc3b095SChuck Lever 	rpc_set_port(xs_addr(xprt), port);
17849dc3b095SChuck Lever 	xs_update_peer_port(xprt);
178592200412SChuck Lever }
178692200412SChuck Lever 
17874dda9c8aSTrond Myklebust static void xs_set_srcport(struct sock_xprt *transport, struct socket *sock)
17884dda9c8aSTrond Myklebust {
17894dda9c8aSTrond Myklebust 	if (transport->srcport == 0)
17904dda9c8aSTrond Myklebust 		transport->srcport = xs_sock_getport(sock);
17914dda9c8aSTrond Myklebust }
17924dda9c8aSTrond Myklebust 
17935d4ec932SPavel Emelyanov static unsigned short xs_get_srcport(struct sock_xprt *transport)
179467a391d7STrond Myklebust {
1795fbfffbd5SChuck Lever 	unsigned short port = transport->srcport;
179667a391d7STrond Myklebust 
179767a391d7STrond Myklebust 	if (port == 0 && transport->xprt.resvport)
179867a391d7STrond Myklebust 		port = xs_get_random_port();
179967a391d7STrond Myklebust 	return port;
180067a391d7STrond Myklebust }
180167a391d7STrond Myklebust 
1802baaf4e48SPavel Emelyanov static unsigned short xs_next_srcport(struct sock_xprt *transport, unsigned short port)
180367a391d7STrond Myklebust {
1804fbfffbd5SChuck Lever 	if (transport->srcport != 0)
1805fbfffbd5SChuck Lever 		transport->srcport = 0;
180667a391d7STrond Myklebust 	if (!transport->xprt.resvport)
180767a391d7STrond Myklebust 		return 0;
180867a391d7STrond Myklebust 	if (port <= xprt_min_resvport || port > xprt_max_resvport)
180967a391d7STrond Myklebust 		return xprt_max_resvport;
181067a391d7STrond Myklebust 	return --port;
181167a391d7STrond Myklebust }
1812beb59b68SPavel Emelyanov static int xs_bind(struct sock_xprt *transport, struct socket *sock)
1813a246b010SChuck Lever {
1814beb59b68SPavel Emelyanov 	struct sockaddr_storage myaddr;
181567a391d7STrond Myklebust 	int err, nloop = 0;
18165d4ec932SPavel Emelyanov 	unsigned short port = xs_get_srcport(transport);
181767a391d7STrond Myklebust 	unsigned short last;
1818a246b010SChuck Lever 
18190f7a622cSChris Perl 	/*
18200f7a622cSChris Perl 	 * If we are asking for any ephemeral port (i.e. port == 0 &&
18210f7a622cSChris Perl 	 * transport->xprt.resvport == 0), don't bind.  Let the local
18220f7a622cSChris Perl 	 * port selection happen implicitly when the socket is used
18230f7a622cSChris Perl 	 * (for example at connect time).
18240f7a622cSChris Perl 	 *
18250f7a622cSChris Perl 	 * This ensures that we can continue to establish TCP
18260f7a622cSChris Perl 	 * connections even when all local ephemeral ports are already
18270f7a622cSChris Perl 	 * a part of some TCP connection.  This makes no difference
18280f7a622cSChris Perl 	 * for UDP sockets, but also doens't harm them.
18290f7a622cSChris Perl 	 *
18300f7a622cSChris Perl 	 * If we're asking for any reserved port (i.e. port == 0 &&
18310f7a622cSChris Perl 	 * transport->xprt.resvport == 1) xs_get_srcport above will
18320f7a622cSChris Perl 	 * ensure that port is non-zero and we will bind as needed.
18330f7a622cSChris Perl 	 */
18340f7a622cSChris Perl 	if (port == 0)
18350f7a622cSChris Perl 		return 0;
18360f7a622cSChris Perl 
1837beb59b68SPavel Emelyanov 	memcpy(&myaddr, &transport->srcaddr, transport->xprt.addrlen);
1838a246b010SChuck Lever 	do {
1839beb59b68SPavel Emelyanov 		rpc_set_port((struct sockaddr *)&myaddr, port);
1840e6242e92SSridhar Samudrala 		err = kernel_bind(sock, (struct sockaddr *)&myaddr,
1841beb59b68SPavel Emelyanov 				transport->xprt.addrlen);
1842a246b010SChuck Lever 		if (err == 0) {
1843fbfffbd5SChuck Lever 			transport->srcport = port;
1844d3bc9a1dSFrank van Maarseveen 			break;
1845a246b010SChuck Lever 		}
184667a391d7STrond Myklebust 		last = port;
1847baaf4e48SPavel Emelyanov 		port = xs_next_srcport(transport, port);
184867a391d7STrond Myklebust 		if (port > last)
184967a391d7STrond Myklebust 			nloop++;
185067a391d7STrond Myklebust 	} while (err == -EADDRINUSE && nloop != 2);
1851beb59b68SPavel Emelyanov 
18524232e863SChuck Lever 	if (myaddr.ss_family == AF_INET)
1853beb59b68SPavel Emelyanov 		dprintk("RPC:       %s %pI4:%u: %s (%d)\n", __func__,
1854beb59b68SPavel Emelyanov 				&((struct sockaddr_in *)&myaddr)->sin_addr,
1855beb59b68SPavel Emelyanov 				port, err ? "failed" : "ok", err);
1856beb59b68SPavel Emelyanov 	else
1857beb59b68SPavel Emelyanov 		dprintk("RPC:       %s %pI6:%u: %s (%d)\n", __func__,
1858beb59b68SPavel Emelyanov 				&((struct sockaddr_in6 *)&myaddr)->sin6_addr,
18597dc753f0SChuck Lever 				port, err ? "failed" : "ok", err);
1860a246b010SChuck Lever 	return err;
1861a246b010SChuck Lever }
1862a246b010SChuck Lever 
1863176e21eeSChuck Lever /*
1864176e21eeSChuck Lever  * We don't support autobind on AF_LOCAL sockets
1865176e21eeSChuck Lever  */
1866176e21eeSChuck Lever static void xs_local_rpcbind(struct rpc_task *task)
1867176e21eeSChuck Lever {
1868fb43d172STrond Myklebust 	xprt_set_bound(task->tk_xprt);
1869176e21eeSChuck Lever }
1870176e21eeSChuck Lever 
1871176e21eeSChuck Lever static void xs_local_set_port(struct rpc_xprt *xprt, unsigned short port)
1872176e21eeSChuck Lever {
1873176e21eeSChuck Lever }
1874a246b010SChuck Lever 
1875ed07536eSPeter Zijlstra #ifdef CONFIG_DEBUG_LOCK_ALLOC
1876ed07536eSPeter Zijlstra static struct lock_class_key xs_key[2];
1877ed07536eSPeter Zijlstra static struct lock_class_key xs_slock_key[2];
1878ed07536eSPeter Zijlstra 
1879176e21eeSChuck Lever static inline void xs_reclassify_socketu(struct socket *sock)
1880176e21eeSChuck Lever {
1881176e21eeSChuck Lever 	struct sock *sk = sock->sk;
1882176e21eeSChuck Lever 
1883176e21eeSChuck Lever 	sock_lock_init_class_and_name(sk, "slock-AF_LOCAL-RPC",
1884176e21eeSChuck Lever 		&xs_slock_key[1], "sk_lock-AF_LOCAL-RPC", &xs_key[1]);
1885176e21eeSChuck Lever }
1886176e21eeSChuck Lever 
18878945ee5eSChuck Lever static inline void xs_reclassify_socket4(struct socket *sock)
1888ed07536eSPeter Zijlstra {
1889ed07536eSPeter Zijlstra 	struct sock *sk = sock->sk;
18908945ee5eSChuck Lever 
18918945ee5eSChuck Lever 	sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC",
18928945ee5eSChuck Lever 		&xs_slock_key[0], "sk_lock-AF_INET-RPC", &xs_key[0]);
1893ed07536eSPeter Zijlstra }
18948945ee5eSChuck Lever 
18958945ee5eSChuck Lever static inline void xs_reclassify_socket6(struct socket *sock)
18968945ee5eSChuck Lever {
18978945ee5eSChuck Lever 	struct sock *sk = sock->sk;
18988945ee5eSChuck Lever 
18998945ee5eSChuck Lever 	sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC",
19008945ee5eSChuck Lever 		&xs_slock_key[1], "sk_lock-AF_INET6-RPC", &xs_key[1]);
1901ed07536eSPeter Zijlstra }
19026bc9638aSPavel Emelyanov 
19036bc9638aSPavel Emelyanov static inline void xs_reclassify_socket(int family, struct socket *sock)
19046bc9638aSPavel Emelyanov {
1905fafc4e1eSHannes Frederic Sowa 	if (WARN_ON_ONCE(!sock_allow_reclassification(sock->sk)))
19061b7a1819SWeston Andros Adamson 		return;
19071b7a1819SWeston Andros Adamson 
19084232e863SChuck Lever 	switch (family) {
1909176e21eeSChuck Lever 	case AF_LOCAL:
1910176e21eeSChuck Lever 		xs_reclassify_socketu(sock);
1911176e21eeSChuck Lever 		break;
19124232e863SChuck Lever 	case AF_INET:
19136bc9638aSPavel Emelyanov 		xs_reclassify_socket4(sock);
19144232e863SChuck Lever 		break;
19154232e863SChuck Lever 	case AF_INET6:
19166bc9638aSPavel Emelyanov 		xs_reclassify_socket6(sock);
19174232e863SChuck Lever 		break;
19184232e863SChuck Lever 	}
19196bc9638aSPavel Emelyanov }
1920ed07536eSPeter Zijlstra #else
19216bc9638aSPavel Emelyanov static inline void xs_reclassify_socket(int family, struct socket *sock)
19226bc9638aSPavel Emelyanov {
19236bc9638aSPavel Emelyanov }
1924ed07536eSPeter Zijlstra #endif
1925ed07536eSPeter Zijlstra 
192693dc41bdSNeilBrown static void xs_dummy_setup_socket(struct work_struct *work)
192793dc41bdSNeilBrown {
192893dc41bdSNeilBrown }
192993dc41bdSNeilBrown 
19306bc9638aSPavel Emelyanov static struct socket *xs_create_sock(struct rpc_xprt *xprt,
19314dda9c8aSTrond Myklebust 		struct sock_xprt *transport, int family, int type,
19324dda9c8aSTrond Myklebust 		int protocol, bool reuseport)
193322f79326SPavel Emelyanov {
193422f79326SPavel Emelyanov 	struct socket *sock;
193522f79326SPavel Emelyanov 	int err;
193622f79326SPavel Emelyanov 
19376bc9638aSPavel Emelyanov 	err = __sock_create(xprt->xprt_net, family, type, protocol, &sock, 1);
193822f79326SPavel Emelyanov 	if (err < 0) {
193922f79326SPavel Emelyanov 		dprintk("RPC:       can't create %d transport socket (%d).\n",
194022f79326SPavel Emelyanov 				protocol, -err);
194122f79326SPavel Emelyanov 		goto out;
194222f79326SPavel Emelyanov 	}
19436bc9638aSPavel Emelyanov 	xs_reclassify_socket(family, sock);
194422f79326SPavel Emelyanov 
19454dda9c8aSTrond Myklebust 	if (reuseport)
19464dda9c8aSTrond Myklebust 		xs_sock_set_reuseport(sock);
19474dda9c8aSTrond Myklebust 
19484cea288aSBen Hutchings 	err = xs_bind(transport, sock);
19494cea288aSBen Hutchings 	if (err) {
195022f79326SPavel Emelyanov 		sock_release(sock);
195122f79326SPavel Emelyanov 		goto out;
195222f79326SPavel Emelyanov 	}
195322f79326SPavel Emelyanov 
195422f79326SPavel Emelyanov 	return sock;
195522f79326SPavel Emelyanov out:
195622f79326SPavel Emelyanov 	return ERR_PTR(err);
195722f79326SPavel Emelyanov }
195822f79326SPavel Emelyanov 
1959176e21eeSChuck Lever static int xs_local_finish_connecting(struct rpc_xprt *xprt,
1960176e21eeSChuck Lever 				      struct socket *sock)
1961176e21eeSChuck Lever {
1962176e21eeSChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
1963176e21eeSChuck Lever 									xprt);
1964176e21eeSChuck Lever 
1965176e21eeSChuck Lever 	if (!transport->inet) {
1966176e21eeSChuck Lever 		struct sock *sk = sock->sk;
1967176e21eeSChuck Lever 
1968176e21eeSChuck Lever 		write_lock_bh(&sk->sk_callback_lock);
1969176e21eeSChuck Lever 
1970176e21eeSChuck Lever 		xs_save_old_callbacks(transport, sk);
1971176e21eeSChuck Lever 
1972176e21eeSChuck Lever 		sk->sk_user_data = xprt;
1973a2648094STrond Myklebust 		sk->sk_data_ready = xs_data_ready;
1974176e21eeSChuck Lever 		sk->sk_write_space = xs_udp_write_space;
1975b4411457SEric Dumazet 		sock_set_flag(sk, SOCK_FASYNC);
19762118071dSTrond Myklebust 		sk->sk_error_report = xs_error_report;
1977c2126157STrond Myklebust 		sk->sk_allocation = GFP_NOIO;
1978176e21eeSChuck Lever 
1979176e21eeSChuck Lever 		xprt_clear_connected(xprt);
1980176e21eeSChuck Lever 
1981176e21eeSChuck Lever 		/* Reset to new socket */
1982176e21eeSChuck Lever 		transport->sock = sock;
1983176e21eeSChuck Lever 		transport->inet = sk;
1984176e21eeSChuck Lever 
1985176e21eeSChuck Lever 		write_unlock_bh(&sk->sk_callback_lock);
1986176e21eeSChuck Lever 	}
1987176e21eeSChuck Lever 
19886c7a64e5STrond Myklebust 	transport->xmit.offset = 0;
19896c7a64e5STrond Myklebust 
1990176e21eeSChuck Lever 	/* Tell the socket layer to start connecting... */
1991176e21eeSChuck Lever 	xprt->stat.connect_count++;
1992176e21eeSChuck Lever 	xprt->stat.connect_start = jiffies;
1993176e21eeSChuck Lever 	return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, 0);
1994176e21eeSChuck Lever }
1995176e21eeSChuck Lever 
1996176e21eeSChuck Lever /**
1997176e21eeSChuck Lever  * xs_local_setup_socket - create AF_LOCAL socket, connect to a local endpoint
1998176e21eeSChuck Lever  * @transport: socket transport to connect
1999176e21eeSChuck Lever  */
2000dc107402SJ. Bruce Fields static int xs_local_setup_socket(struct sock_xprt *transport)
2001176e21eeSChuck Lever {
2002176e21eeSChuck Lever 	struct rpc_xprt *xprt = &transport->xprt;
2003176e21eeSChuck Lever 	struct socket *sock;
2004176e21eeSChuck Lever 	int status = -EIO;
2005176e21eeSChuck Lever 
2006176e21eeSChuck Lever 	status = __sock_create(xprt->xprt_net, AF_LOCAL,
2007176e21eeSChuck Lever 					SOCK_STREAM, 0, &sock, 1);
2008176e21eeSChuck Lever 	if (status < 0) {
2009176e21eeSChuck Lever 		dprintk("RPC:       can't create AF_LOCAL "
2010176e21eeSChuck Lever 			"transport socket (%d).\n", -status);
2011176e21eeSChuck Lever 		goto out;
2012176e21eeSChuck Lever 	}
2013d1358917SStefan Hajnoczi 	xs_reclassify_socket(AF_LOCAL, sock);
2014176e21eeSChuck Lever 
2015176e21eeSChuck Lever 	dprintk("RPC:       worker connecting xprt %p via AF_LOCAL to %s\n",
2016176e21eeSChuck Lever 			xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
2017176e21eeSChuck Lever 
2018176e21eeSChuck Lever 	status = xs_local_finish_connecting(xprt, sock);
201940b5ea0cSTrond Myklebust 	trace_rpc_socket_connect(xprt, sock, status);
2020176e21eeSChuck Lever 	switch (status) {
2021176e21eeSChuck Lever 	case 0:
2022176e21eeSChuck Lever 		dprintk("RPC:       xprt %p connected to %s\n",
2023176e21eeSChuck Lever 				xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
2024176e21eeSChuck Lever 		xprt_set_connected(xprt);
20253601c4a9STrond Myklebust 	case -ENOBUFS:
2026176e21eeSChuck Lever 		break;
2027176e21eeSChuck Lever 	case -ENOENT:
2028176e21eeSChuck Lever 		dprintk("RPC:       xprt %p: socket %s does not exist\n",
2029176e21eeSChuck Lever 				xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
2030176e21eeSChuck Lever 		break;
20314a20a988STrond Myklebust 	case -ECONNREFUSED:
20324a20a988STrond Myklebust 		dprintk("RPC:       xprt %p: connection refused for %s\n",
20334a20a988STrond Myklebust 				xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
20344a20a988STrond Myklebust 		break;
2035176e21eeSChuck Lever 	default:
2036176e21eeSChuck Lever 		printk(KERN_ERR "%s: unhandled error (%d) connecting to %s\n",
2037176e21eeSChuck Lever 				__func__, -status,
2038176e21eeSChuck Lever 				xprt->address_strings[RPC_DISPLAY_ADDR]);
2039176e21eeSChuck Lever 	}
2040176e21eeSChuck Lever 
2041176e21eeSChuck Lever out:
2042176e21eeSChuck Lever 	xprt_clear_connecting(xprt);
2043176e21eeSChuck Lever 	xprt_wake_pending_tasks(xprt, status);
2044dc107402SJ. Bruce Fields 	return status;
2045dc107402SJ. Bruce Fields }
2046dc107402SJ. Bruce Fields 
2047b6669737SLinus Torvalds static void xs_local_connect(struct rpc_xprt *xprt, struct rpc_task *task)
2048dc107402SJ. Bruce Fields {
2049dc107402SJ. Bruce Fields 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2050dc107402SJ. Bruce Fields 	int ret;
2051dc107402SJ. Bruce Fields 
2052dc107402SJ. Bruce Fields 	 if (RPC_IS_ASYNC(task)) {
2053dc107402SJ. Bruce Fields 		/*
2054dc107402SJ. Bruce Fields 		 * We want the AF_LOCAL connect to be resolved in the
2055dc107402SJ. Bruce Fields 		 * filesystem namespace of the process making the rpc
2056dc107402SJ. Bruce Fields 		 * call.  Thus we connect synchronously.
2057dc107402SJ. Bruce Fields 		 *
2058dc107402SJ. Bruce Fields 		 * If we want to support asynchronous AF_LOCAL calls,
2059dc107402SJ. Bruce Fields 		 * we'll need to figure out how to pass a namespace to
2060dc107402SJ. Bruce Fields 		 * connect.
2061dc107402SJ. Bruce Fields 		 */
2062dc107402SJ. Bruce Fields 		rpc_exit(task, -ENOTCONN);
2063dc107402SJ. Bruce Fields 		return;
2064dc107402SJ. Bruce Fields 	}
2065dc107402SJ. Bruce Fields 	ret = xs_local_setup_socket(transport);
2066dc107402SJ. Bruce Fields 	if (ret && !RPC_IS_SOFTCONN(task))
2067dc107402SJ. Bruce Fields 		msleep_interruptible(15000);
2068176e21eeSChuck Lever }
2069176e21eeSChuck Lever 
20703c87ef6eSJeff Layton #if IS_ENABLED(CONFIG_SUNRPC_SWAP)
2071d6e971d8SJeff Layton /*
2072d6e971d8SJeff Layton  * Note that this should be called with XPRT_LOCKED held (or when we otherwise
2073d6e971d8SJeff Layton  * know that we have exclusive access to the socket), to guard against
2074d6e971d8SJeff Layton  * races with xs_reset_transport.
2075d6e971d8SJeff Layton  */
2076a564b8f0SMel Gorman static void xs_set_memalloc(struct rpc_xprt *xprt)
2077a564b8f0SMel Gorman {
2078a564b8f0SMel Gorman 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
2079a564b8f0SMel Gorman 			xprt);
2080a564b8f0SMel Gorman 
2081d6e971d8SJeff Layton 	/*
2082d6e971d8SJeff Layton 	 * If there's no sock, then we have nothing to set. The
2083d6e971d8SJeff Layton 	 * reconnecting process will get it for us.
2084d6e971d8SJeff Layton 	 */
2085d6e971d8SJeff Layton 	if (!transport->inet)
2086d6e971d8SJeff Layton 		return;
20878e228133SJeff Layton 	if (atomic_read(&xprt->swapper))
2088a564b8f0SMel Gorman 		sk_set_memalloc(transport->inet);
2089a564b8f0SMel Gorman }
2090a564b8f0SMel Gorman 
2091a564b8f0SMel Gorman /**
2092d67fa4d8SJeff Layton  * xs_enable_swap - Tag this transport as being used for swap.
2093a564b8f0SMel Gorman  * @xprt: transport to tag
2094a564b8f0SMel Gorman  *
20958e228133SJeff Layton  * Take a reference to this transport on behalf of the rpc_clnt, and
20968e228133SJeff Layton  * optionally mark it for swapping if it wasn't already.
2097a564b8f0SMel Gorman  */
2098d67fa4d8SJeff Layton static int
2099d67fa4d8SJeff Layton xs_enable_swap(struct rpc_xprt *xprt)
2100a564b8f0SMel Gorman {
2101d6e971d8SJeff Layton 	struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt);
2102a564b8f0SMel Gorman 
2103d6e971d8SJeff Layton 	if (atomic_inc_return(&xprt->swapper) != 1)
2104d6e971d8SJeff Layton 		return 0;
2105d6e971d8SJeff Layton 	if (wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_KILLABLE))
2106d6e971d8SJeff Layton 		return -ERESTARTSYS;
2107d6e971d8SJeff Layton 	if (xs->inet)
2108d6e971d8SJeff Layton 		sk_set_memalloc(xs->inet);
2109d6e971d8SJeff Layton 	xprt_release_xprt(xprt, NULL);
21108e228133SJeff Layton 	return 0;
2111a564b8f0SMel Gorman }
2112a564b8f0SMel Gorman 
21138e228133SJeff Layton /**
2114d67fa4d8SJeff Layton  * xs_disable_swap - Untag this transport as being used for swap.
21158e228133SJeff Layton  * @xprt: transport to tag
21168e228133SJeff Layton  *
21178e228133SJeff Layton  * Drop a "swapper" reference to this xprt on behalf of the rpc_clnt. If the
21188e228133SJeff Layton  * swapper refcount goes to 0, untag the socket as a memalloc socket.
21198e228133SJeff Layton  */
2120d67fa4d8SJeff Layton static void
2121d67fa4d8SJeff Layton xs_disable_swap(struct rpc_xprt *xprt)
21228e228133SJeff Layton {
2123d6e971d8SJeff Layton 	struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt);
21248e228133SJeff Layton 
2125d6e971d8SJeff Layton 	if (!atomic_dec_and_test(&xprt->swapper))
2126d6e971d8SJeff Layton 		return;
2127d6e971d8SJeff Layton 	if (wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_KILLABLE))
2128d6e971d8SJeff Layton 		return;
2129d6e971d8SJeff Layton 	if (xs->inet)
2130d6e971d8SJeff Layton 		sk_clear_memalloc(xs->inet);
2131d6e971d8SJeff Layton 	xprt_release_xprt(xprt, NULL);
2132a564b8f0SMel Gorman }
2133a564b8f0SMel Gorman #else
2134a564b8f0SMel Gorman static void xs_set_memalloc(struct rpc_xprt *xprt)
2135a564b8f0SMel Gorman {
2136a564b8f0SMel Gorman }
2137d67fa4d8SJeff Layton 
2138d67fa4d8SJeff Layton static int
2139d67fa4d8SJeff Layton xs_enable_swap(struct rpc_xprt *xprt)
2140d67fa4d8SJeff Layton {
2141d67fa4d8SJeff Layton 	return -EINVAL;
2142d67fa4d8SJeff Layton }
2143d67fa4d8SJeff Layton 
2144d67fa4d8SJeff Layton static void
2145d67fa4d8SJeff Layton xs_disable_swap(struct rpc_xprt *xprt)
2146d67fa4d8SJeff Layton {
2147d67fa4d8SJeff Layton }
2148a564b8f0SMel Gorman #endif
2149a564b8f0SMel Gorman 
215016be2d20SChuck Lever static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
2151a246b010SChuck Lever {
215216be2d20SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2153edb267a6SChuck Lever 
2154ee0ac0c2SChuck Lever 	if (!transport->inet) {
2155b0d93ad5SChuck Lever 		struct sock *sk = sock->sk;
2156b0d93ad5SChuck Lever 
2157b0d93ad5SChuck Lever 		write_lock_bh(&sk->sk_callback_lock);
2158b0d93ad5SChuck Lever 
21592a9e1cfaSTrond Myklebust 		xs_save_old_callbacks(transport, sk);
21602a9e1cfaSTrond Myklebust 
2161b0d93ad5SChuck Lever 		sk->sk_user_data = xprt;
2162f9b2ee71STrond Myklebust 		sk->sk_data_ready = xs_data_ready;
2163b0d93ad5SChuck Lever 		sk->sk_write_space = xs_udp_write_space;
2164b4411457SEric Dumazet 		sock_set_flag(sk, SOCK_FASYNC);
2165c2126157STrond Myklebust 		sk->sk_allocation = GFP_NOIO;
2166b0d93ad5SChuck Lever 
2167b0d93ad5SChuck Lever 		xprt_set_connected(xprt);
2168b0d93ad5SChuck Lever 
2169b0d93ad5SChuck Lever 		/* Reset to new socket */
2170ee0ac0c2SChuck Lever 		transport->sock = sock;
2171ee0ac0c2SChuck Lever 		transport->inet = sk;
2172b0d93ad5SChuck Lever 
2173a564b8f0SMel Gorman 		xs_set_memalloc(xprt);
2174a564b8f0SMel Gorman 
2175b0d93ad5SChuck Lever 		write_unlock_bh(&sk->sk_callback_lock);
2176b0d93ad5SChuck Lever 	}
2177470056c2SChuck Lever 	xs_udp_do_set_buffer_size(xprt);
217802910177STrond Myklebust 
217902910177STrond Myklebust 	xprt->stat.connect_start = jiffies;
218016be2d20SChuck Lever }
218116be2d20SChuck Lever 
21828c14ff2aSPavel Emelyanov static void xs_udp_setup_socket(struct work_struct *work)
2183a246b010SChuck Lever {
2184a246b010SChuck Lever 	struct sock_xprt *transport =
2185a246b010SChuck Lever 		container_of(work, struct sock_xprt, connect_worker.work);
2186a246b010SChuck Lever 	struct rpc_xprt *xprt = &transport->xprt;
2187d099b8afSColin Ian King 	struct socket *sock;
2188b65c0310SPavel Emelyanov 	int status = -EIO;
2189a246b010SChuck Lever 
21908c14ff2aSPavel Emelyanov 	sock = xs_create_sock(xprt, transport,
21914dda9c8aSTrond Myklebust 			xs_addr(xprt)->sa_family, SOCK_DGRAM,
21924dda9c8aSTrond Myklebust 			IPPROTO_UDP, false);
2193b65c0310SPavel Emelyanov 	if (IS_ERR(sock))
2194a246b010SChuck Lever 		goto out;
219568e220bdSChuck Lever 
2196c740eff8SChuck Lever 	dprintk("RPC:       worker connecting xprt %p via %s to "
2197c740eff8SChuck Lever 				"%s (port %s)\n", xprt,
2198c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_PROTO],
2199c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_ADDR],
2200c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_PORT]);
220168e220bdSChuck Lever 
220268e220bdSChuck Lever 	xs_udp_finish_connecting(xprt, sock);
220340b5ea0cSTrond Myklebust 	trace_rpc_socket_connect(xprt, sock, 0);
2204a246b010SChuck Lever 	status = 0;
2205b0d93ad5SChuck Lever out:
2206718ba5b8STrond Myklebust 	xprt_unlock_connect(xprt, transport);
2207b0d93ad5SChuck Lever 	xprt_clear_connecting(xprt);
22087d1e8255STrond Myklebust 	xprt_wake_pending_tasks(xprt, status);
2209b0d93ad5SChuck Lever }
2210b0d93ad5SChuck Lever 
22114876cc77STrond Myklebust /**
22124876cc77STrond Myklebust  * xs_tcp_shutdown - gracefully shut down a TCP socket
22134876cc77STrond Myklebust  * @xprt: transport
22144876cc77STrond Myklebust  *
22154876cc77STrond Myklebust  * Initiates a graceful shutdown of the TCP socket by calling the
22164876cc77STrond Myklebust  * equivalent of shutdown(SHUT_RDWR);
22174876cc77STrond Myklebust  */
22184876cc77STrond Myklebust static void xs_tcp_shutdown(struct rpc_xprt *xprt)
22194876cc77STrond Myklebust {
22204876cc77STrond Myklebust 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
22214876cc77STrond Myklebust 	struct socket *sock = transport->sock;
22229b30889cSTrond Myklebust 	int skst = transport->inet ? transport->inet->sk_state : TCP_CLOSE;
22234876cc77STrond Myklebust 
22244876cc77STrond Myklebust 	if (sock == NULL)
22254876cc77STrond Myklebust 		return;
22269b30889cSTrond Myklebust 	switch (skst) {
22279b30889cSTrond Myklebust 	default:
22284876cc77STrond Myklebust 		kernel_sock_shutdown(sock, SHUT_RDWR);
22294876cc77STrond Myklebust 		trace_rpc_socket_shutdown(xprt, sock);
22309b30889cSTrond Myklebust 		break;
22319b30889cSTrond Myklebust 	case TCP_CLOSE:
22329b30889cSTrond Myklebust 	case TCP_TIME_WAIT:
22334876cc77STrond Myklebust 		xs_reset_transport(transport);
22344876cc77STrond Myklebust 	}
22359b30889cSTrond Myklebust }
22364876cc77STrond Myklebust 
22378d1b8c62STrond Myklebust static void xs_tcp_set_socket_timeouts(struct rpc_xprt *xprt,
22388d1b8c62STrond Myklebust 		struct socket *sock)
2239b0d93ad5SChuck Lever {
224016be2d20SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
22417196dbb0STrond Myklebust 	unsigned int keepidle;
22427196dbb0STrond Myklebust 	unsigned int keepcnt;
22437f260e85STrond Myklebust 	unsigned int opt_on = 1;
2244775f06abSTrond Myklebust 	unsigned int timeo;
22457f260e85STrond Myklebust 
22467196dbb0STrond Myklebust 	spin_lock_bh(&xprt->transport_lock);
22477196dbb0STrond Myklebust 	keepidle = DIV_ROUND_UP(xprt->timeout->to_initval, HZ);
22487196dbb0STrond Myklebust 	keepcnt = xprt->timeout->to_retries + 1;
22497196dbb0STrond Myklebust 	timeo = jiffies_to_msecs(xprt->timeout->to_initval) *
22507196dbb0STrond Myklebust 		(xprt->timeout->to_retries + 1);
22517196dbb0STrond Myklebust 	clear_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state);
22527196dbb0STrond Myklebust 	spin_unlock_bh(&xprt->transport_lock);
22537f260e85STrond Myklebust 
22547f260e85STrond Myklebust 	/* TCP Keepalive options */
22557f260e85STrond Myklebust 	kernel_setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE,
22567f260e85STrond Myklebust 			(char *)&opt_on, sizeof(opt_on));
22577f260e85STrond Myklebust 	kernel_setsockopt(sock, SOL_TCP, TCP_KEEPIDLE,
22587f260e85STrond Myklebust 			(char *)&keepidle, sizeof(keepidle));
22597f260e85STrond Myklebust 	kernel_setsockopt(sock, SOL_TCP, TCP_KEEPINTVL,
22607f260e85STrond Myklebust 			(char *)&keepidle, sizeof(keepidle));
22617f260e85STrond Myklebust 	kernel_setsockopt(sock, SOL_TCP, TCP_KEEPCNT,
22627f260e85STrond Myklebust 			(char *)&keepcnt, sizeof(keepcnt));
2263b0d93ad5SChuck Lever 
22648d1b8c62STrond Myklebust 	/* TCP user timeout (see RFC5482) */
22658d1b8c62STrond Myklebust 	kernel_setsockopt(sock, SOL_TCP, TCP_USER_TIMEOUT,
22668d1b8c62STrond Myklebust 			(char *)&timeo, sizeof(timeo));
22678d1b8c62STrond Myklebust }
22688d1b8c62STrond Myklebust 
22697196dbb0STrond Myklebust static void xs_tcp_set_connect_timeout(struct rpc_xprt *xprt,
22707196dbb0STrond Myklebust 		unsigned long connect_timeout,
22717196dbb0STrond Myklebust 		unsigned long reconnect_timeout)
22727196dbb0STrond Myklebust {
22737196dbb0STrond Myklebust 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
22747196dbb0STrond Myklebust 	struct rpc_timeout to;
22757196dbb0STrond Myklebust 	unsigned long initval;
22767196dbb0STrond Myklebust 
22777196dbb0STrond Myklebust 	spin_lock_bh(&xprt->transport_lock);
22787196dbb0STrond Myklebust 	if (reconnect_timeout < xprt->max_reconnect_timeout)
22797196dbb0STrond Myklebust 		xprt->max_reconnect_timeout = reconnect_timeout;
22807196dbb0STrond Myklebust 	if (connect_timeout < xprt->connect_timeout) {
22817196dbb0STrond Myklebust 		memcpy(&to, xprt->timeout, sizeof(to));
22827196dbb0STrond Myklebust 		initval = DIV_ROUND_UP(connect_timeout, to.to_retries + 1);
22837196dbb0STrond Myklebust 		/* Arbitrary lower limit */
22847196dbb0STrond Myklebust 		if (initval <  XS_TCP_INIT_REEST_TO << 1)
22857196dbb0STrond Myklebust 			initval = XS_TCP_INIT_REEST_TO << 1;
22867196dbb0STrond Myklebust 		to.to_initval = initval;
22877196dbb0STrond Myklebust 		to.to_maxval = initval;
22887196dbb0STrond Myklebust 		memcpy(&transport->tcp_timeout, &to,
22897196dbb0STrond Myklebust 				sizeof(transport->tcp_timeout));
22907196dbb0STrond Myklebust 		xprt->timeout = &transport->tcp_timeout;
22917196dbb0STrond Myklebust 		xprt->connect_timeout = connect_timeout;
22927196dbb0STrond Myklebust 	}
22937196dbb0STrond Myklebust 	set_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state);
22947196dbb0STrond Myklebust 	spin_unlock_bh(&xprt->transport_lock);
22957196dbb0STrond Myklebust }
22967196dbb0STrond Myklebust 
22978d1b8c62STrond Myklebust static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
22988d1b8c62STrond Myklebust {
22998d1b8c62STrond Myklebust 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
23008d1b8c62STrond Myklebust 	int ret = -ENOTCONN;
23018d1b8c62STrond Myklebust 
23028d1b8c62STrond Myklebust 	if (!transport->inet) {
23038d1b8c62STrond Myklebust 		struct sock *sk = sock->sk;
23048d1b8c62STrond Myklebust 		unsigned int addr_pref = IPV6_PREFER_SRC_PUBLIC;
23058d1b8c62STrond Myklebust 
2306d88e4d82SNeilBrown 		/* Avoid temporary address, they are bad for long-lived
2307d88e4d82SNeilBrown 		 * connections such as NFS mounts.
2308d88e4d82SNeilBrown 		 * RFC4941, section 3.6 suggests that:
2309d88e4d82SNeilBrown 		 *    Individual applications, which have specific
2310d88e4d82SNeilBrown 		 *    knowledge about the normal duration of connections,
2311d88e4d82SNeilBrown 		 *    MAY override this as appropriate.
2312d88e4d82SNeilBrown 		 */
2313d88e4d82SNeilBrown 		kernel_setsockopt(sock, SOL_IPV6, IPV6_ADDR_PREFERENCES,
2314d88e4d82SNeilBrown 				(char *)&addr_pref, sizeof(addr_pref));
2315d88e4d82SNeilBrown 
23168d1b8c62STrond Myklebust 		xs_tcp_set_socket_timeouts(xprt, sock);
2317775f06abSTrond Myklebust 
2318b0d93ad5SChuck Lever 		write_lock_bh(&sk->sk_callback_lock);
2319b0d93ad5SChuck Lever 
23202a9e1cfaSTrond Myklebust 		xs_save_old_callbacks(transport, sk);
23212a9e1cfaSTrond Myklebust 
2322b0d93ad5SChuck Lever 		sk->sk_user_data = xprt;
23235157b956STrond Myklebust 		sk->sk_data_ready = xs_data_ready;
2324b0d93ad5SChuck Lever 		sk->sk_state_change = xs_tcp_state_change;
2325b0d93ad5SChuck Lever 		sk->sk_write_space = xs_tcp_write_space;
2326b4411457SEric Dumazet 		sock_set_flag(sk, SOCK_FASYNC);
23272118071dSTrond Myklebust 		sk->sk_error_report = xs_error_report;
2328c2126157STrond Myklebust 		sk->sk_allocation = GFP_NOIO;
23293167e12cSChuck Lever 
23303167e12cSChuck Lever 		/* socket options */
23313167e12cSChuck Lever 		sock_reset_flag(sk, SOCK_LINGER);
23323167e12cSChuck Lever 		tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF;
2333b0d93ad5SChuck Lever 
2334b0d93ad5SChuck Lever 		xprt_clear_connected(xprt);
2335b0d93ad5SChuck Lever 
2336b0d93ad5SChuck Lever 		/* Reset to new socket */
2337ee0ac0c2SChuck Lever 		transport->sock = sock;
2338ee0ac0c2SChuck Lever 		transport->inet = sk;
2339b0d93ad5SChuck Lever 
2340b0d93ad5SChuck Lever 		write_unlock_bh(&sk->sk_callback_lock);
2341b0d93ad5SChuck Lever 	}
2342b0d93ad5SChuck Lever 
234301d37c42STrond Myklebust 	if (!xprt_bound(xprt))
2344fe19a96bSTrond Myklebust 		goto out;
234501d37c42STrond Myklebust 
2346a564b8f0SMel Gorman 	xs_set_memalloc(xprt);
2347a564b8f0SMel Gorman 
2348e1806c7bSTrond Myklebust 	/* Reset TCP record info */
2349e1806c7bSTrond Myklebust 	transport->recv.offset = 0;
2350e1806c7bSTrond Myklebust 	transport->recv.len = 0;
2351e1806c7bSTrond Myklebust 	transport->recv.copied = 0;
23526c7a64e5STrond Myklebust 	transport->xmit.offset = 0;
2353e1806c7bSTrond Myklebust 
2354b0d93ad5SChuck Lever 	/* Tell the socket layer to start connecting... */
2355262ca07dSChuck Lever 	xprt->stat.connect_count++;
2356262ca07dSChuck Lever 	xprt->stat.connect_start = jiffies;
23570fdea1e8STrond Myklebust 	set_bit(XPRT_SOCK_CONNECTING, &transport->sock_state);
2358fe19a96bSTrond Myklebust 	ret = kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK);
2359fe19a96bSTrond Myklebust 	switch (ret) {
2360fe19a96bSTrond Myklebust 	case 0:
23614dda9c8aSTrond Myklebust 		xs_set_srcport(transport, sock);
2362e9d47639SGustavo A. R. Silva 		/* fall through */
2363fe19a96bSTrond Myklebust 	case -EINPROGRESS:
2364fe19a96bSTrond Myklebust 		/* SYN_SENT! */
2365fe19a96bSTrond Myklebust 		if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
2366fe19a96bSTrond Myklebust 			xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
23671f4c17a0STrond Myklebust 		break;
23681f4c17a0STrond Myklebust 	case -EADDRNOTAVAIL:
23691f4c17a0STrond Myklebust 		/* Source port number is unavailable. Try a new one! */
23701f4c17a0STrond Myklebust 		transport->srcport = 0;
2371fe19a96bSTrond Myklebust 	}
2372fe19a96bSTrond Myklebust out:
2373fe19a96bSTrond Myklebust 	return ret;
237416be2d20SChuck Lever }
237516be2d20SChuck Lever 
237616be2d20SChuck Lever /**
2377b61d59ffSTrond Myklebust  * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint
237816be2d20SChuck Lever  *
237916be2d20SChuck Lever  * Invoked by a work queue tasklet.
238016be2d20SChuck Lever  */
2381cdd518d5SPavel Emelyanov static void xs_tcp_setup_socket(struct work_struct *work)
238216be2d20SChuck Lever {
2383cdd518d5SPavel Emelyanov 	struct sock_xprt *transport =
2384cdd518d5SPavel Emelyanov 		container_of(work, struct sock_xprt, connect_worker.work);
238516be2d20SChuck Lever 	struct socket *sock = transport->sock;
2386a9f5f0f7SPavel Emelyanov 	struct rpc_xprt *xprt = &transport->xprt;
2387b61d59ffSTrond Myklebust 	int status = -EIO;
238816be2d20SChuck Lever 
238916be2d20SChuck Lever 	if (!sock) {
2390cdd518d5SPavel Emelyanov 		sock = xs_create_sock(xprt, transport,
23914dda9c8aSTrond Myklebust 				xs_addr(xprt)->sa_family, SOCK_STREAM,
23924dda9c8aSTrond Myklebust 				IPPROTO_TCP, true);
2393b61d59ffSTrond Myklebust 		if (IS_ERR(sock)) {
2394b61d59ffSTrond Myklebust 			status = PTR_ERR(sock);
239516be2d20SChuck Lever 			goto out;
239616be2d20SChuck Lever 		}
23977d1e8255STrond Myklebust 	}
23987d1e8255STrond Myklebust 
2399c740eff8SChuck Lever 	dprintk("RPC:       worker connecting xprt %p via %s to "
2400c740eff8SChuck Lever 				"%s (port %s)\n", xprt,
2401c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_PROTO],
2402c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_ADDR],
2403c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_PORT]);
240416be2d20SChuck Lever 
240516be2d20SChuck Lever 	status = xs_tcp_finish_connecting(xprt, sock);
240640b5ea0cSTrond Myklebust 	trace_rpc_socket_connect(xprt, sock, status);
2407a246b010SChuck Lever 	dprintk("RPC:       %p connect status %d connected %d sock state %d\n",
240846121cf7SChuck Lever 			xprt, -status, xprt_connected(xprt),
240946121cf7SChuck Lever 			sock->sk->sk_state);
2410a246b010SChuck Lever 	switch (status) {
2411f75e6745STrond Myklebust 	default:
2412f75e6745STrond Myklebust 		printk("%s: connect returned unhandled error %d\n",
2413f75e6745STrond Myklebust 			__func__, status);
2414e9d47639SGustavo A. R. Silva 		/* fall through */
2415f75e6745STrond Myklebust 	case -EADDRNOTAVAIL:
2416f75e6745STrond Myklebust 		/* We're probably in TIME_WAIT. Get rid of existing socket,
2417f75e6745STrond Myklebust 		 * and retry
2418f75e6745STrond Myklebust 		 */
2419a519fc7aSTrond Myklebust 		xs_tcp_force_close(xprt);
242088b5ed73STrond Myklebust 		break;
24212a491991STrond Myklebust 	case 0:
2422a246b010SChuck Lever 	case -EINPROGRESS:
2423a246b010SChuck Lever 	case -EALREADY:
2424718ba5b8STrond Myklebust 		xprt_unlock_connect(xprt, transport);
24257d1e8255STrond Myklebust 		return;
24269fcfe0c8STrond Myklebust 	case -EINVAL:
24279fcfe0c8STrond Myklebust 		/* Happens, for instance, if the user specified a link
24289fcfe0c8STrond Myklebust 		 * local IPv6 address without a scope-id.
24299fcfe0c8STrond Myklebust 		 */
24303ed5e2a2STrond Myklebust 	case -ECONNREFUSED:
24313ed5e2a2STrond Myklebust 	case -ECONNRESET:
2432eb5b46faSTrond Myklebust 	case -ENETDOWN:
24333ed5e2a2STrond Myklebust 	case -ENETUNREACH:
24344ba161a7STrond Myklebust 	case -EHOSTUNREACH:
24353913c78cSTrond Myklebust 	case -EADDRINUSE:
24363601c4a9STrond Myklebust 	case -ENOBUFS:
24376ea44adcSNeilBrown 		/*
24386ea44adcSNeilBrown 		 * xs_tcp_force_close() wakes tasks with -EIO.
24396ea44adcSNeilBrown 		 * We need to wake them first to ensure the
24406ea44adcSNeilBrown 		 * correct error code.
24416ea44adcSNeilBrown 		 */
24426ea44adcSNeilBrown 		xprt_wake_pending_tasks(xprt, status);
24434efdd92cSTrond Myklebust 		xs_tcp_force_close(xprt);
24449fcfe0c8STrond Myklebust 		goto out;
24458a2cec29STrond Myklebust 	}
24462a491991STrond Myklebust 	status = -EAGAIN;
2447a246b010SChuck Lever out:
2448718ba5b8STrond Myklebust 	xprt_unlock_connect(xprt, transport);
24492226feb6SChuck Lever 	xprt_clear_connecting(xprt);
24507d1e8255STrond Myklebust 	xprt_wake_pending_tasks(xprt, status);
2451a246b010SChuck Lever }
2452a246b010SChuck Lever 
245302910177STrond Myklebust static unsigned long xs_reconnect_delay(const struct rpc_xprt *xprt)
245402910177STrond Myklebust {
245502910177STrond Myklebust 	unsigned long start, now = jiffies;
245602910177STrond Myklebust 
245702910177STrond Myklebust 	start = xprt->stat.connect_start + xprt->reestablish_timeout;
245802910177STrond Myklebust 	if (time_after(start, now))
245902910177STrond Myklebust 		return start - now;
246002910177STrond Myklebust 	return 0;
246102910177STrond Myklebust }
246202910177STrond Myklebust 
24633851f1cdSTrond Myklebust static void xs_reconnect_backoff(struct rpc_xprt *xprt)
24643851f1cdSTrond Myklebust {
24653851f1cdSTrond Myklebust 	xprt->reestablish_timeout <<= 1;
24663851f1cdSTrond Myklebust 	if (xprt->reestablish_timeout > xprt->max_reconnect_timeout)
24673851f1cdSTrond Myklebust 		xprt->reestablish_timeout = xprt->max_reconnect_timeout;
24683851f1cdSTrond Myklebust 	if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
24693851f1cdSTrond Myklebust 		xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
24703851f1cdSTrond Myklebust }
24713851f1cdSTrond Myklebust 
247268e220bdSChuck Lever /**
24739903cd1cSChuck Lever  * xs_connect - connect a socket to a remote endpoint
24741b092092STrond Myklebust  * @xprt: pointer to transport structure
24759903cd1cSChuck Lever  * @task: address of RPC task that manages state of connect request
24769903cd1cSChuck Lever  *
24779903cd1cSChuck Lever  * TCP: If the remote end dropped the connection, delay reconnecting.
247803bf4b70SChuck Lever  *
247903bf4b70SChuck Lever  * UDP socket connects are synchronous, but we use a work queue anyway
248003bf4b70SChuck Lever  * to guarantee that even unprivileged user processes can set up a
248103bf4b70SChuck Lever  * socket on a privileged port.
248203bf4b70SChuck Lever  *
248303bf4b70SChuck Lever  * If a UDP socket connect fails, the delay behavior here prevents
248403bf4b70SChuck Lever  * retry floods (hard mounts).
24859903cd1cSChuck Lever  */
24861b092092STrond Myklebust static void xs_connect(struct rpc_xprt *xprt, struct rpc_task *task)
2487a246b010SChuck Lever {
2488ee0ac0c2SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
248902910177STrond Myklebust 	unsigned long delay = 0;
2490a246b010SChuck Lever 
2491718ba5b8STrond Myklebust 	WARN_ON_ONCE(!xprt_lock_connect(xprt, task, transport));
2492718ba5b8STrond Myklebust 
249399b1a4c3STrond Myklebust 	if (transport->sock != NULL) {
249446121cf7SChuck Lever 		dprintk("RPC:       xs_connect delayed xprt %p for %lu "
249546121cf7SChuck Lever 				"seconds\n",
249603bf4b70SChuck Lever 				xprt, xprt->reestablish_timeout / HZ);
249799b1a4c3STrond Myklebust 
249899b1a4c3STrond Myklebust 		/* Start by resetting any existing state */
249999b1a4c3STrond Myklebust 		xs_reset_transport(transport);
250099b1a4c3STrond Myklebust 
250102910177STrond Myklebust 		delay = xs_reconnect_delay(xprt);
25023851f1cdSTrond Myklebust 		xs_reconnect_backoff(xprt);
250302910177STrond Myklebust 
250402910177STrond Myklebust 	} else
25059903cd1cSChuck Lever 		dprintk("RPC:       xs_connect scheduled xprt %p\n", xprt);
250602910177STrond Myklebust 
250740a5f1b1STrond Myklebust 	queue_delayed_work(xprtiod_workqueue,
250802910177STrond Myklebust 			&transport->connect_worker,
250902910177STrond Myklebust 			delay);
2510a246b010SChuck Lever }
2511a246b010SChuck Lever 
2512262ca07dSChuck Lever /**
2513176e21eeSChuck Lever  * xs_local_print_stats - display AF_LOCAL socket-specifc stats
2514176e21eeSChuck Lever  * @xprt: rpc_xprt struct containing statistics
2515176e21eeSChuck Lever  * @seq: output file
2516176e21eeSChuck Lever  *
2517176e21eeSChuck Lever  */
2518176e21eeSChuck Lever static void xs_local_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2519176e21eeSChuck Lever {
2520176e21eeSChuck Lever 	long idle_time = 0;
2521176e21eeSChuck Lever 
2522176e21eeSChuck Lever 	if (xprt_connected(xprt))
2523176e21eeSChuck Lever 		idle_time = (long)(jiffies - xprt->last_used) / HZ;
2524176e21eeSChuck Lever 
2525176e21eeSChuck Lever 	seq_printf(seq, "\txprt:\tlocal %lu %lu %lu %ld %lu %lu %lu "
252615a45206SAndy Adamson 			"%llu %llu %lu %llu %llu\n",
2527176e21eeSChuck Lever 			xprt->stat.bind_count,
2528176e21eeSChuck Lever 			xprt->stat.connect_count,
2529176e21eeSChuck Lever 			xprt->stat.connect_time,
2530176e21eeSChuck Lever 			idle_time,
2531176e21eeSChuck Lever 			xprt->stat.sends,
2532176e21eeSChuck Lever 			xprt->stat.recvs,
2533176e21eeSChuck Lever 			xprt->stat.bad_xids,
2534176e21eeSChuck Lever 			xprt->stat.req_u,
253515a45206SAndy Adamson 			xprt->stat.bklog_u,
253615a45206SAndy Adamson 			xprt->stat.max_slots,
253715a45206SAndy Adamson 			xprt->stat.sending_u,
253815a45206SAndy Adamson 			xprt->stat.pending_u);
2539176e21eeSChuck Lever }
2540176e21eeSChuck Lever 
2541176e21eeSChuck Lever /**
2542262ca07dSChuck Lever  * xs_udp_print_stats - display UDP socket-specifc stats
2543262ca07dSChuck Lever  * @xprt: rpc_xprt struct containing statistics
2544262ca07dSChuck Lever  * @seq: output file
2545262ca07dSChuck Lever  *
2546262ca07dSChuck Lever  */
2547262ca07dSChuck Lever static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2548262ca07dSChuck Lever {
2549c8475461SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2550c8475461SChuck Lever 
255115a45206SAndy Adamson 	seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %llu %llu "
255215a45206SAndy Adamson 			"%lu %llu %llu\n",
2553fbfffbd5SChuck Lever 			transport->srcport,
2554262ca07dSChuck Lever 			xprt->stat.bind_count,
2555262ca07dSChuck Lever 			xprt->stat.sends,
2556262ca07dSChuck Lever 			xprt->stat.recvs,
2557262ca07dSChuck Lever 			xprt->stat.bad_xids,
2558262ca07dSChuck Lever 			xprt->stat.req_u,
255915a45206SAndy Adamson 			xprt->stat.bklog_u,
256015a45206SAndy Adamson 			xprt->stat.max_slots,
256115a45206SAndy Adamson 			xprt->stat.sending_u,
256215a45206SAndy Adamson 			xprt->stat.pending_u);
2563262ca07dSChuck Lever }
2564262ca07dSChuck Lever 
2565262ca07dSChuck Lever /**
2566262ca07dSChuck Lever  * xs_tcp_print_stats - display TCP socket-specifc stats
2567262ca07dSChuck Lever  * @xprt: rpc_xprt struct containing statistics
2568262ca07dSChuck Lever  * @seq: output file
2569262ca07dSChuck Lever  *
2570262ca07dSChuck Lever  */
2571262ca07dSChuck Lever static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2572262ca07dSChuck Lever {
2573c8475461SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2574262ca07dSChuck Lever 	long idle_time = 0;
2575262ca07dSChuck Lever 
2576262ca07dSChuck Lever 	if (xprt_connected(xprt))
2577262ca07dSChuck Lever 		idle_time = (long)(jiffies - xprt->last_used) / HZ;
2578262ca07dSChuck Lever 
257915a45206SAndy Adamson 	seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu "
258015a45206SAndy Adamson 			"%llu %llu %lu %llu %llu\n",
2581fbfffbd5SChuck Lever 			transport->srcport,
2582262ca07dSChuck Lever 			xprt->stat.bind_count,
2583262ca07dSChuck Lever 			xprt->stat.connect_count,
2584262ca07dSChuck Lever 			xprt->stat.connect_time,
2585262ca07dSChuck Lever 			idle_time,
2586262ca07dSChuck Lever 			xprt->stat.sends,
2587262ca07dSChuck Lever 			xprt->stat.recvs,
2588262ca07dSChuck Lever 			xprt->stat.bad_xids,
2589262ca07dSChuck Lever 			xprt->stat.req_u,
259015a45206SAndy Adamson 			xprt->stat.bklog_u,
259115a45206SAndy Adamson 			xprt->stat.max_slots,
259215a45206SAndy Adamson 			xprt->stat.sending_u,
259315a45206SAndy Adamson 			xprt->stat.pending_u);
2594262ca07dSChuck Lever }
2595262ca07dSChuck Lever 
25964cfc7e60SRahul Iyer /*
25974cfc7e60SRahul Iyer  * Allocate a bunch of pages for a scratch buffer for the rpc code. The reason
25984cfc7e60SRahul Iyer  * we allocate pages instead doing a kmalloc like rpc_malloc is because we want
25994cfc7e60SRahul Iyer  * to use the server side send routines.
26004cfc7e60SRahul Iyer  */
26015fe6eaa1SChuck Lever static int bc_malloc(struct rpc_task *task)
26024cfc7e60SRahul Iyer {
26035fe6eaa1SChuck Lever 	struct rpc_rqst *rqst = task->tk_rqstp;
26045fe6eaa1SChuck Lever 	size_t size = rqst->rq_callsize;
26054cfc7e60SRahul Iyer 	struct page *page;
26064cfc7e60SRahul Iyer 	struct rpc_buffer *buf;
26074cfc7e60SRahul Iyer 
26085fe6eaa1SChuck Lever 	if (size > PAGE_SIZE - sizeof(struct rpc_buffer)) {
26095fe6eaa1SChuck Lever 		WARN_ONCE(1, "xprtsock: large bc buffer request (size %zu)\n",
26105fe6eaa1SChuck Lever 			  size);
26115fe6eaa1SChuck Lever 		return -EINVAL;
26125fe6eaa1SChuck Lever 	}
26134cfc7e60SRahul Iyer 
2614b8a13d03SWeston Andros Adamson 	page = alloc_page(GFP_KERNEL);
26154cfc7e60SRahul Iyer 	if (!page)
26165fe6eaa1SChuck Lever 		return -ENOMEM;
26174cfc7e60SRahul Iyer 
26184cfc7e60SRahul Iyer 	buf = page_address(page);
26194cfc7e60SRahul Iyer 	buf->len = PAGE_SIZE;
26204cfc7e60SRahul Iyer 
26215fe6eaa1SChuck Lever 	rqst->rq_buffer = buf->data;
262218e601d6SJeff Layton 	rqst->rq_rbuffer = (char *)rqst->rq_buffer + rqst->rq_callsize;
26235fe6eaa1SChuck Lever 	return 0;
26244cfc7e60SRahul Iyer }
26254cfc7e60SRahul Iyer 
26264cfc7e60SRahul Iyer /*
26274cfc7e60SRahul Iyer  * Free the space allocated in the bc_alloc routine
26284cfc7e60SRahul Iyer  */
26293435c74aSChuck Lever static void bc_free(struct rpc_task *task)
26304cfc7e60SRahul Iyer {
26313435c74aSChuck Lever 	void *buffer = task->tk_rqstp->rq_buffer;
26324cfc7e60SRahul Iyer 	struct rpc_buffer *buf;
26334cfc7e60SRahul Iyer 
26344cfc7e60SRahul Iyer 	buf = container_of(buffer, struct rpc_buffer, data);
26354cfc7e60SRahul Iyer 	free_page((unsigned long)buf);
26364cfc7e60SRahul Iyer }
26374cfc7e60SRahul Iyer 
26384cfc7e60SRahul Iyer /*
26394cfc7e60SRahul Iyer  * Use the svc_sock to send the callback. Must be called with svsk->sk_mutex
26404cfc7e60SRahul Iyer  * held. Borrows heavily from svc_tcp_sendto and xs_tcp_send_request.
26414cfc7e60SRahul Iyer  */
26424cfc7e60SRahul Iyer static int bc_sendto(struct rpc_rqst *req)
26434cfc7e60SRahul Iyer {
26444cfc7e60SRahul Iyer 	int len;
26454cfc7e60SRahul Iyer 	struct xdr_buf *xbufp = &req->rq_snd_buf;
26464cfc7e60SRahul Iyer 	struct rpc_xprt *xprt = req->rq_xprt;
26474cfc7e60SRahul Iyer 	struct sock_xprt *transport =
26484cfc7e60SRahul Iyer 				container_of(xprt, struct sock_xprt, xprt);
26494cfc7e60SRahul Iyer 	struct socket *sock = transport->sock;
26504cfc7e60SRahul Iyer 	unsigned long headoff;
26514cfc7e60SRahul Iyer 	unsigned long tailoff;
26524cfc7e60SRahul Iyer 
265361677eeeSChuck Lever 	xs_encode_stream_record_marker(xbufp);
26544cfc7e60SRahul Iyer 
26554cfc7e60SRahul Iyer 	tailoff = (unsigned long)xbufp->tail[0].iov_base & ~PAGE_MASK;
26564cfc7e60SRahul Iyer 	headoff = (unsigned long)xbufp->head[0].iov_base & ~PAGE_MASK;
26574cfc7e60SRahul Iyer 	len = svc_send_common(sock, xbufp,
26584cfc7e60SRahul Iyer 			      virt_to_page(xbufp->head[0].iov_base), headoff,
26594cfc7e60SRahul Iyer 			      xbufp->tail[0].iov_base, tailoff);
26604cfc7e60SRahul Iyer 
26614cfc7e60SRahul Iyer 	if (len != xbufp->len) {
26624cfc7e60SRahul Iyer 		printk(KERN_NOTICE "Error sending entire callback!\n");
26634cfc7e60SRahul Iyer 		len = -EAGAIN;
26644cfc7e60SRahul Iyer 	}
26654cfc7e60SRahul Iyer 
26664cfc7e60SRahul Iyer 	return len;
26674cfc7e60SRahul Iyer }
26684cfc7e60SRahul Iyer 
26694cfc7e60SRahul Iyer /*
26704cfc7e60SRahul Iyer  * The send routine. Borrows from svc_send
26714cfc7e60SRahul Iyer  */
2672adfa7144STrond Myklebust static int bc_send_request(struct rpc_rqst *req)
26734cfc7e60SRahul Iyer {
26744cfc7e60SRahul Iyer 	struct svc_xprt	*xprt;
26757fc56136SAndrzej Hajda 	int len;
26764cfc7e60SRahul Iyer 
26774cfc7e60SRahul Iyer 	dprintk("sending request with xid: %08x\n", ntohl(req->rq_xid));
26784cfc7e60SRahul Iyer 	/*
26794cfc7e60SRahul Iyer 	 * Get the server socket associated with this callback xprt
26804cfc7e60SRahul Iyer 	 */
26814cfc7e60SRahul Iyer 	xprt = req->rq_xprt->bc_xprt;
26824cfc7e60SRahul Iyer 
26834cfc7e60SRahul Iyer 	/*
26844cfc7e60SRahul Iyer 	 * Grab the mutex to serialize data as the connection is shared
26854cfc7e60SRahul Iyer 	 * with the fore channel
26864cfc7e60SRahul Iyer 	 */
2687c544577dSTrond Myklebust 	mutex_lock(&xprt->xpt_mutex);
26884cfc7e60SRahul Iyer 	if (test_bit(XPT_DEAD, &xprt->xpt_flags))
26894cfc7e60SRahul Iyer 		len = -ENOTCONN;
26904cfc7e60SRahul Iyer 	else
26914cfc7e60SRahul Iyer 		len = bc_sendto(req);
26924cfc7e60SRahul Iyer 	mutex_unlock(&xprt->xpt_mutex);
26934cfc7e60SRahul Iyer 
26944cfc7e60SRahul Iyer 	if (len > 0)
26954cfc7e60SRahul Iyer 		len = 0;
26964cfc7e60SRahul Iyer 
26974cfc7e60SRahul Iyer 	return len;
26984cfc7e60SRahul Iyer }
26994cfc7e60SRahul Iyer 
27004cfc7e60SRahul Iyer /*
27014cfc7e60SRahul Iyer  * The close routine. Since this is client initiated, we do nothing
27024cfc7e60SRahul Iyer  */
27034cfc7e60SRahul Iyer 
27044cfc7e60SRahul Iyer static void bc_close(struct rpc_xprt *xprt)
27054cfc7e60SRahul Iyer {
27064cfc7e60SRahul Iyer }
27074cfc7e60SRahul Iyer 
27084cfc7e60SRahul Iyer /*
27094cfc7e60SRahul Iyer  * The xprt destroy routine. Again, because this connection is client
27104cfc7e60SRahul Iyer  * initiated, we do nothing
27114cfc7e60SRahul Iyer  */
27124cfc7e60SRahul Iyer 
27134cfc7e60SRahul Iyer static void bc_destroy(struct rpc_xprt *xprt)
27144cfc7e60SRahul Iyer {
271547f72efaSKinglong Mee 	dprintk("RPC:       bc_destroy xprt %p\n", xprt);
271647f72efaSKinglong Mee 
271747f72efaSKinglong Mee 	xs_xprt_free(xprt);
271847f72efaSKinglong Mee 	module_put(THIS_MODULE);
27194cfc7e60SRahul Iyer }
27204cfc7e60SRahul Iyer 
2721d31ae254SChuck Lever static const struct rpc_xprt_ops xs_local_ops = {
2722176e21eeSChuck Lever 	.reserve_xprt		= xprt_reserve_xprt,
27234cd34e7cSTrond Myklebust 	.release_xprt		= xprt_release_xprt,
2724f39c1bfbSTrond Myklebust 	.alloc_slot		= xprt_alloc_slot,
2725a9cde23aSChuck Lever 	.free_slot		= xprt_free_slot,
2726176e21eeSChuck Lever 	.rpcbind		= xs_local_rpcbind,
2727176e21eeSChuck Lever 	.set_port		= xs_local_set_port,
2728dc107402SJ. Bruce Fields 	.connect		= xs_local_connect,
2729176e21eeSChuck Lever 	.buf_alloc		= rpc_malloc,
2730176e21eeSChuck Lever 	.buf_free		= rpc_free,
2731176e21eeSChuck Lever 	.send_request		= xs_local_send_request,
2732176e21eeSChuck Lever 	.set_retrans_timeout	= xprt_set_retrans_timeout_def,
2733176e21eeSChuck Lever 	.close			= xs_close,
2734a1311d87STrond Myklebust 	.destroy		= xs_destroy,
2735176e21eeSChuck Lever 	.print_stats		= xs_local_print_stats,
2736d67fa4d8SJeff Layton 	.enable_swap		= xs_enable_swap,
2737d67fa4d8SJeff Layton 	.disable_swap		= xs_disable_swap,
2738176e21eeSChuck Lever };
2739176e21eeSChuck Lever 
2740d31ae254SChuck Lever static const struct rpc_xprt_ops xs_udp_ops = {
274143118c29SChuck Lever 	.set_buffer_size	= xs_udp_set_buffer_size,
274212a80469SChuck Lever 	.reserve_xprt		= xprt_reserve_xprt_cong,
274349e9a890SChuck Lever 	.release_xprt		= xprt_release_xprt_cong,
2744f39c1bfbSTrond Myklebust 	.alloc_slot		= xprt_alloc_slot,
2745a9cde23aSChuck Lever 	.free_slot		= xprt_free_slot,
274645160d62SChuck Lever 	.rpcbind		= rpcb_getport_async,
274792200412SChuck Lever 	.set_port		= xs_set_port,
27489903cd1cSChuck Lever 	.connect		= xs_connect,
274902107148SChuck Lever 	.buf_alloc		= rpc_malloc,
275002107148SChuck Lever 	.buf_free		= rpc_free,
2751262965f5SChuck Lever 	.send_request		= xs_udp_send_request,
2752fe3aca29SChuck Lever 	.set_retrans_timeout	= xprt_set_retrans_timeout_rtt,
275346c0ee8bSChuck Lever 	.timer			= xs_udp_timer,
2754a58dd398SChuck Lever 	.release_request	= xprt_release_rqst_cong,
2755262965f5SChuck Lever 	.close			= xs_close,
2756262965f5SChuck Lever 	.destroy		= xs_destroy,
2757262ca07dSChuck Lever 	.print_stats		= xs_udp_print_stats,
2758d67fa4d8SJeff Layton 	.enable_swap		= xs_enable_swap,
2759d67fa4d8SJeff Layton 	.disable_swap		= xs_disable_swap,
27604a068258SChuck Lever 	.inject_disconnect	= xs_inject_disconnect,
2761262965f5SChuck Lever };
2762262965f5SChuck Lever 
2763d31ae254SChuck Lever static const struct rpc_xprt_ops xs_tcp_ops = {
276412a80469SChuck Lever 	.reserve_xprt		= xprt_reserve_xprt,
27654cd34e7cSTrond Myklebust 	.release_xprt		= xprt_release_xprt,
276636bd7de9STrond Myklebust 	.alloc_slot		= xprt_alloc_slot,
2767a9cde23aSChuck Lever 	.free_slot		= xprt_free_slot,
276845160d62SChuck Lever 	.rpcbind		= rpcb_getport_async,
276992200412SChuck Lever 	.set_port		= xs_set_port,
27700b9e7943STrond Myklebust 	.connect		= xs_connect,
277102107148SChuck Lever 	.buf_alloc		= rpc_malloc,
277202107148SChuck Lever 	.buf_free		= rpc_free,
2773*277e4ab7STrond Myklebust 	.prepare_request	= xs_stream_prepare_request,
2774262965f5SChuck Lever 	.send_request		= xs_tcp_send_request,
2775fe3aca29SChuck Lever 	.set_retrans_timeout	= xprt_set_retrans_timeout_def,
2776c627d31bSTrond Myklebust 	.close			= xs_tcp_shutdown,
27779903cd1cSChuck Lever 	.destroy		= xs_destroy,
27787196dbb0STrond Myklebust 	.set_connect_timeout	= xs_tcp_set_connect_timeout,
2779262ca07dSChuck Lever 	.print_stats		= xs_tcp_print_stats,
2780d67fa4d8SJeff Layton 	.enable_swap		= xs_enable_swap,
2781d67fa4d8SJeff Layton 	.disable_swap		= xs_disable_swap,
27824a068258SChuck Lever 	.inject_disconnect	= xs_inject_disconnect,
278342e5c3e2SChuck Lever #ifdef CONFIG_SUNRPC_BACKCHANNEL
278442e5c3e2SChuck Lever 	.bc_setup		= xprt_setup_bc,
278576566773SChuck Lever 	.bc_up			= xs_tcp_bc_up,
27866b26cc8cSChuck Lever 	.bc_maxpayload		= xs_tcp_bc_maxpayload,
278742e5c3e2SChuck Lever 	.bc_free_rqst		= xprt_free_bc_rqst,
278842e5c3e2SChuck Lever 	.bc_destroy		= xprt_destroy_bc,
278942e5c3e2SChuck Lever #endif
2790a246b010SChuck Lever };
2791a246b010SChuck Lever 
27924cfc7e60SRahul Iyer /*
27934cfc7e60SRahul Iyer  * The rpc_xprt_ops for the server backchannel
27944cfc7e60SRahul Iyer  */
27954cfc7e60SRahul Iyer 
2796d31ae254SChuck Lever static const struct rpc_xprt_ops bc_tcp_ops = {
27974cfc7e60SRahul Iyer 	.reserve_xprt		= xprt_reserve_xprt,
27984cfc7e60SRahul Iyer 	.release_xprt		= xprt_release_xprt,
279984e28a30SBryan Schumaker 	.alloc_slot		= xprt_alloc_slot,
2800a9cde23aSChuck Lever 	.free_slot		= xprt_free_slot,
28014cfc7e60SRahul Iyer 	.buf_alloc		= bc_malloc,
28024cfc7e60SRahul Iyer 	.buf_free		= bc_free,
28034cfc7e60SRahul Iyer 	.send_request		= bc_send_request,
28044cfc7e60SRahul Iyer 	.set_retrans_timeout	= xprt_set_retrans_timeout_def,
28054cfc7e60SRahul Iyer 	.close			= bc_close,
28064cfc7e60SRahul Iyer 	.destroy		= bc_destroy,
28074cfc7e60SRahul Iyer 	.print_stats		= xs_tcp_print_stats,
2808d67fa4d8SJeff Layton 	.enable_swap		= xs_enable_swap,
2809d67fa4d8SJeff Layton 	.disable_swap		= xs_disable_swap,
28104a068258SChuck Lever 	.inject_disconnect	= xs_inject_disconnect,
28114cfc7e60SRahul Iyer };
28124cfc7e60SRahul Iyer 
281392476850SChuck Lever static int xs_init_anyaddr(const int family, struct sockaddr *sap)
281492476850SChuck Lever {
281592476850SChuck Lever 	static const struct sockaddr_in sin = {
281692476850SChuck Lever 		.sin_family		= AF_INET,
281792476850SChuck Lever 		.sin_addr.s_addr	= htonl(INADDR_ANY),
281892476850SChuck Lever 	};
281992476850SChuck Lever 	static const struct sockaddr_in6 sin6 = {
282092476850SChuck Lever 		.sin6_family		= AF_INET6,
282192476850SChuck Lever 		.sin6_addr		= IN6ADDR_ANY_INIT,
282292476850SChuck Lever 	};
282392476850SChuck Lever 
282492476850SChuck Lever 	switch (family) {
2825176e21eeSChuck Lever 	case AF_LOCAL:
2826176e21eeSChuck Lever 		break;
282792476850SChuck Lever 	case AF_INET:
282892476850SChuck Lever 		memcpy(sap, &sin, sizeof(sin));
282992476850SChuck Lever 		break;
283092476850SChuck Lever 	case AF_INET6:
283192476850SChuck Lever 		memcpy(sap, &sin6, sizeof(sin6));
283292476850SChuck Lever 		break;
283392476850SChuck Lever 	default:
283492476850SChuck Lever 		dprintk("RPC:       %s: Bad address family\n", __func__);
283592476850SChuck Lever 		return -EAFNOSUPPORT;
283692476850SChuck Lever 	}
283792476850SChuck Lever 	return 0;
283892476850SChuck Lever }
283992476850SChuck Lever 
28403c341b0bS\"Talpey, Thomas\ static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args,
2841d9ba131dSTrond Myklebust 				      unsigned int slot_table_size,
2842d9ba131dSTrond Myklebust 				      unsigned int max_slot_table_size)
2843c8541ecdSChuck Lever {
2844c8541ecdSChuck Lever 	struct rpc_xprt *xprt;
2845ffc2e518SChuck Lever 	struct sock_xprt *new;
2846c8541ecdSChuck Lever 
284796802a09SFrank van Maarseveen 	if (args->addrlen > sizeof(xprt->addr)) {
2848c8541ecdSChuck Lever 		dprintk("RPC:       xs_setup_xprt: address too large\n");
2849c8541ecdSChuck Lever 		return ERR_PTR(-EBADF);
2850c8541ecdSChuck Lever 	}
2851c8541ecdSChuck Lever 
2852d9ba131dSTrond Myklebust 	xprt = xprt_alloc(args->net, sizeof(*new), slot_table_size,
2853d9ba131dSTrond Myklebust 			max_slot_table_size);
2854bd1722d4SPavel Emelyanov 	if (xprt == NULL) {
285546121cf7SChuck Lever 		dprintk("RPC:       xs_setup_xprt: couldn't allocate "
285646121cf7SChuck Lever 				"rpc_xprt\n");
2857c8541ecdSChuck Lever 		return ERR_PTR(-ENOMEM);
2858c8541ecdSChuck Lever 	}
2859c8541ecdSChuck Lever 
2860bd1722d4SPavel Emelyanov 	new = container_of(xprt, struct sock_xprt, xprt);
2861edc1b01cSTrond Myklebust 	mutex_init(&new->recv_mutex);
286296802a09SFrank van Maarseveen 	memcpy(&xprt->addr, args->dstaddr, args->addrlen);
286396802a09SFrank van Maarseveen 	xprt->addrlen = args->addrlen;
2864d3bc9a1dSFrank van Maarseveen 	if (args->srcaddr)
2865fbfffbd5SChuck Lever 		memcpy(&new->srcaddr, args->srcaddr, args->addrlen);
286692476850SChuck Lever 	else {
286792476850SChuck Lever 		int err;
286892476850SChuck Lever 		err = xs_init_anyaddr(args->dstaddr->sa_family,
286992476850SChuck Lever 					(struct sockaddr *)&new->srcaddr);
28702aa13531SStanislav Kinsbursky 		if (err != 0) {
28712aa13531SStanislav Kinsbursky 			xprt_free(xprt);
287292476850SChuck Lever 			return ERR_PTR(err);
287392476850SChuck Lever 		}
28742aa13531SStanislav Kinsbursky 	}
2875c8541ecdSChuck Lever 
2876c8541ecdSChuck Lever 	return xprt;
2877c8541ecdSChuck Lever }
2878c8541ecdSChuck Lever 
2879176e21eeSChuck Lever static const struct rpc_timeout xs_local_default_timeout = {
2880176e21eeSChuck Lever 	.to_initval = 10 * HZ,
2881176e21eeSChuck Lever 	.to_maxval = 10 * HZ,
2882176e21eeSChuck Lever 	.to_retries = 2,
2883176e21eeSChuck Lever };
2884176e21eeSChuck Lever 
2885176e21eeSChuck Lever /**
2886176e21eeSChuck Lever  * xs_setup_local - Set up transport to use an AF_LOCAL socket
2887176e21eeSChuck Lever  * @args: rpc transport creation arguments
2888176e21eeSChuck Lever  *
2889176e21eeSChuck Lever  * AF_LOCAL is a "tpi_cots_ord" transport, just like TCP
2890176e21eeSChuck Lever  */
2891176e21eeSChuck Lever static struct rpc_xprt *xs_setup_local(struct xprt_create *args)
2892176e21eeSChuck Lever {
2893176e21eeSChuck Lever 	struct sockaddr_un *sun = (struct sockaddr_un *)args->dstaddr;
2894176e21eeSChuck Lever 	struct sock_xprt *transport;
2895176e21eeSChuck Lever 	struct rpc_xprt *xprt;
2896176e21eeSChuck Lever 	struct rpc_xprt *ret;
2897176e21eeSChuck Lever 
2898d9ba131dSTrond Myklebust 	xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
2899d9ba131dSTrond Myklebust 			xprt_max_tcp_slot_table_entries);
2900176e21eeSChuck Lever 	if (IS_ERR(xprt))
2901176e21eeSChuck Lever 		return xprt;
2902176e21eeSChuck Lever 	transport = container_of(xprt, struct sock_xprt, xprt);
2903176e21eeSChuck Lever 
2904176e21eeSChuck Lever 	xprt->prot = 0;
2905176e21eeSChuck Lever 	xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
2906176e21eeSChuck Lever 	xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2907176e21eeSChuck Lever 
2908176e21eeSChuck Lever 	xprt->bind_timeout = XS_BIND_TO;
2909176e21eeSChuck Lever 	xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2910176e21eeSChuck Lever 	xprt->idle_timeout = XS_IDLE_DISC_TO;
2911176e21eeSChuck Lever 
2912176e21eeSChuck Lever 	xprt->ops = &xs_local_ops;
2913176e21eeSChuck Lever 	xprt->timeout = &xs_local_default_timeout;
2914176e21eeSChuck Lever 
2915a2648094STrond Myklebust 	INIT_WORK(&transport->recv_worker, xs_local_data_receive_workfn);
291693dc41bdSNeilBrown 	INIT_DELAYED_WORK(&transport->connect_worker,
291793dc41bdSNeilBrown 			xs_dummy_setup_socket);
291893dc41bdSNeilBrown 
2919176e21eeSChuck Lever 	switch (sun->sun_family) {
2920176e21eeSChuck Lever 	case AF_LOCAL:
2921176e21eeSChuck Lever 		if (sun->sun_path[0] != '/') {
2922176e21eeSChuck Lever 			dprintk("RPC:       bad AF_LOCAL address: %s\n",
2923176e21eeSChuck Lever 					sun->sun_path);
2924176e21eeSChuck Lever 			ret = ERR_PTR(-EINVAL);
2925176e21eeSChuck Lever 			goto out_err;
2926176e21eeSChuck Lever 		}
2927176e21eeSChuck Lever 		xprt_set_bound(xprt);
2928176e21eeSChuck Lever 		xs_format_peer_addresses(xprt, "local", RPCBIND_NETID_LOCAL);
29297073ea87SJ. Bruce Fields 		ret = ERR_PTR(xs_local_setup_socket(transport));
29307073ea87SJ. Bruce Fields 		if (ret)
29317073ea87SJ. Bruce Fields 			goto out_err;
2932176e21eeSChuck Lever 		break;
2933176e21eeSChuck Lever 	default:
2934176e21eeSChuck Lever 		ret = ERR_PTR(-EAFNOSUPPORT);
2935176e21eeSChuck Lever 		goto out_err;
2936176e21eeSChuck Lever 	}
2937176e21eeSChuck Lever 
2938176e21eeSChuck Lever 	dprintk("RPC:       set up xprt to %s via AF_LOCAL\n",
2939176e21eeSChuck Lever 			xprt->address_strings[RPC_DISPLAY_ADDR]);
2940176e21eeSChuck Lever 
2941176e21eeSChuck Lever 	if (try_module_get(THIS_MODULE))
2942176e21eeSChuck Lever 		return xprt;
2943176e21eeSChuck Lever 	ret = ERR_PTR(-EINVAL);
2944176e21eeSChuck Lever out_err:
2945315f3812SKinglong Mee 	xs_xprt_free(xprt);
2946176e21eeSChuck Lever 	return ret;
2947176e21eeSChuck Lever }
2948176e21eeSChuck Lever 
29492881ae74STrond Myklebust static const struct rpc_timeout xs_udp_default_timeout = {
29502881ae74STrond Myklebust 	.to_initval = 5 * HZ,
29512881ae74STrond Myklebust 	.to_maxval = 30 * HZ,
29522881ae74STrond Myklebust 	.to_increment = 5 * HZ,
29532881ae74STrond Myklebust 	.to_retries = 5,
29542881ae74STrond Myklebust };
29552881ae74STrond Myklebust 
29569903cd1cSChuck Lever /**
29579903cd1cSChuck Lever  * xs_setup_udp - Set up transport to use a UDP socket
295896802a09SFrank van Maarseveen  * @args: rpc transport creation arguments
29599903cd1cSChuck Lever  *
29609903cd1cSChuck Lever  */
2961483066d6SAdrian Bunk static struct rpc_xprt *xs_setup_udp(struct xprt_create *args)
2962a246b010SChuck Lever {
29638f9d5b1aSChuck Lever 	struct sockaddr *addr = args->dstaddr;
2964c8541ecdSChuck Lever 	struct rpc_xprt *xprt;
2965c8475461SChuck Lever 	struct sock_xprt *transport;
29660a68b0beSJ. Bruce Fields 	struct rpc_xprt *ret;
2967a246b010SChuck Lever 
2968d9ba131dSTrond Myklebust 	xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries,
2969d9ba131dSTrond Myklebust 			xprt_udp_slot_table_entries);
2970c8541ecdSChuck Lever 	if (IS_ERR(xprt))
2971c8541ecdSChuck Lever 		return xprt;
2972c8475461SChuck Lever 	transport = container_of(xprt, struct sock_xprt, xprt);
2973a246b010SChuck Lever 
2974ec739ef0SChuck Lever 	xprt->prot = IPPROTO_UDP;
2975808012fbSChuck Lever 	xprt->tsh_size = 0;
2976a246b010SChuck Lever 	/* XXX: header size can vary due to auth type, IPv6, etc. */
2977a246b010SChuck Lever 	xprt->max_payload = (1U << 16) - (MAX_HEADER << 3);
2978a246b010SChuck Lever 
297903bf4b70SChuck Lever 	xprt->bind_timeout = XS_BIND_TO;
298003bf4b70SChuck Lever 	xprt->reestablish_timeout = XS_UDP_REEST_TO;
298103bf4b70SChuck Lever 	xprt->idle_timeout = XS_IDLE_DISC_TO;
2982a246b010SChuck Lever 
2983262965f5SChuck Lever 	xprt->ops = &xs_udp_ops;
2984a246b010SChuck Lever 
2985ba7392bbSTrond Myklebust 	xprt->timeout = &xs_udp_default_timeout;
2986a246b010SChuck Lever 
2987f9b2ee71STrond Myklebust 	INIT_WORK(&transport->recv_worker, xs_udp_data_receive_workfn);
2988edc1b01cSTrond Myklebust 	INIT_DELAYED_WORK(&transport->connect_worker, xs_udp_setup_socket);
2989edc1b01cSTrond Myklebust 
29908f9d5b1aSChuck Lever 	switch (addr->sa_family) {
29918f9d5b1aSChuck Lever 	case AF_INET:
29928f9d5b1aSChuck Lever 		if (((struct sockaddr_in *)addr)->sin_port != htons(0))
29938f9d5b1aSChuck Lever 			xprt_set_bound(xprt);
29948f9d5b1aSChuck Lever 
29959dc3b095SChuck Lever 		xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP);
29968f9d5b1aSChuck Lever 		break;
29978f9d5b1aSChuck Lever 	case AF_INET6:
29988f9d5b1aSChuck Lever 		if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
29998f9d5b1aSChuck Lever 			xprt_set_bound(xprt);
30008f9d5b1aSChuck Lever 
30019dc3b095SChuck Lever 		xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6);
30028f9d5b1aSChuck Lever 		break;
30038f9d5b1aSChuck Lever 	default:
30040a68b0beSJ. Bruce Fields 		ret = ERR_PTR(-EAFNOSUPPORT);
30050a68b0beSJ. Bruce Fields 		goto out_err;
30068f9d5b1aSChuck Lever 	}
30078f9d5b1aSChuck Lever 
3008c740eff8SChuck Lever 	if (xprt_bound(xprt))
3009c740eff8SChuck Lever 		dprintk("RPC:       set up xprt to %s (port %s) via %s\n",
3010c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_ADDR],
3011c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_PORT],
3012c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_PROTO]);
3013c740eff8SChuck Lever 	else
3014c740eff8SChuck Lever 		dprintk("RPC:       set up xprt to %s (autobind) via %s\n",
3015c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_ADDR],
3016c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_PROTO]);
3017edb267a6SChuck Lever 
3018bc25571eS\"Talpey, Thomas\ 	if (try_module_get(THIS_MODULE))
3019c8541ecdSChuck Lever 		return xprt;
30200a68b0beSJ. Bruce Fields 	ret = ERR_PTR(-EINVAL);
30210a68b0beSJ. Bruce Fields out_err:
3022315f3812SKinglong Mee 	xs_xprt_free(xprt);
30230a68b0beSJ. Bruce Fields 	return ret;
3024a246b010SChuck Lever }
3025a246b010SChuck Lever 
30262881ae74STrond Myklebust static const struct rpc_timeout xs_tcp_default_timeout = {
30272881ae74STrond Myklebust 	.to_initval = 60 * HZ,
30282881ae74STrond Myklebust 	.to_maxval = 60 * HZ,
30292881ae74STrond Myklebust 	.to_retries = 2,
30302881ae74STrond Myklebust };
30312881ae74STrond Myklebust 
30329903cd1cSChuck Lever /**
30339903cd1cSChuck Lever  * xs_setup_tcp - Set up transport to use a TCP socket
303496802a09SFrank van Maarseveen  * @args: rpc transport creation arguments
30359903cd1cSChuck Lever  *
30369903cd1cSChuck Lever  */
3037483066d6SAdrian Bunk static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args)
3038a246b010SChuck Lever {
30398f9d5b1aSChuck Lever 	struct sockaddr *addr = args->dstaddr;
3040c8541ecdSChuck Lever 	struct rpc_xprt *xprt;
3041c8475461SChuck Lever 	struct sock_xprt *transport;
30420a68b0beSJ. Bruce Fields 	struct rpc_xprt *ret;
3043b7993cebSTrond Myklebust 	unsigned int max_slot_table_size = xprt_max_tcp_slot_table_entries;
3044b7993cebSTrond Myklebust 
3045b7993cebSTrond Myklebust 	if (args->flags & XPRT_CREATE_INFINITE_SLOTS)
3046b7993cebSTrond Myklebust 		max_slot_table_size = RPC_MAX_SLOT_TABLE_LIMIT;
3047a246b010SChuck Lever 
3048d9ba131dSTrond Myklebust 	xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
3049b7993cebSTrond Myklebust 			max_slot_table_size);
3050c8541ecdSChuck Lever 	if (IS_ERR(xprt))
3051c8541ecdSChuck Lever 		return xprt;
3052c8475461SChuck Lever 	transport = container_of(xprt, struct sock_xprt, xprt);
3053a246b010SChuck Lever 
3054ec739ef0SChuck Lever 	xprt->prot = IPPROTO_TCP;
3055808012fbSChuck Lever 	xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
3056808012fbSChuck Lever 	xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
3057a246b010SChuck Lever 
305803bf4b70SChuck Lever 	xprt->bind_timeout = XS_BIND_TO;
305903bf4b70SChuck Lever 	xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
306003bf4b70SChuck Lever 	xprt->idle_timeout = XS_IDLE_DISC_TO;
3061a246b010SChuck Lever 
3062262965f5SChuck Lever 	xprt->ops = &xs_tcp_ops;
3063ba7392bbSTrond Myklebust 	xprt->timeout = &xs_tcp_default_timeout;
3064a246b010SChuck Lever 
30653851f1cdSTrond Myklebust 	xprt->max_reconnect_timeout = xprt->timeout->to_maxval;
30667196dbb0STrond Myklebust 	xprt->connect_timeout = xprt->timeout->to_initval *
30677196dbb0STrond Myklebust 		(xprt->timeout->to_retries + 1);
30683851f1cdSTrond Myklebust 
3069edc1b01cSTrond Myklebust 	INIT_WORK(&transport->recv_worker, xs_tcp_data_receive_workfn);
3070edc1b01cSTrond Myklebust 	INIT_DELAYED_WORK(&transport->connect_worker, xs_tcp_setup_socket);
3071edc1b01cSTrond Myklebust 
30728f9d5b1aSChuck Lever 	switch (addr->sa_family) {
30738f9d5b1aSChuck Lever 	case AF_INET:
30748f9d5b1aSChuck Lever 		if (((struct sockaddr_in *)addr)->sin_port != htons(0))
30758f9d5b1aSChuck Lever 			xprt_set_bound(xprt);
30768f9d5b1aSChuck Lever 
30779dc3b095SChuck Lever 		xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP);
30788f9d5b1aSChuck Lever 		break;
30798f9d5b1aSChuck Lever 	case AF_INET6:
30808f9d5b1aSChuck Lever 		if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
30818f9d5b1aSChuck Lever 			xprt_set_bound(xprt);
30828f9d5b1aSChuck Lever 
30839dc3b095SChuck Lever 		xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6);
30848f9d5b1aSChuck Lever 		break;
30858f9d5b1aSChuck Lever 	default:
30860a68b0beSJ. Bruce Fields 		ret = ERR_PTR(-EAFNOSUPPORT);
30870a68b0beSJ. Bruce Fields 		goto out_err;
30888f9d5b1aSChuck Lever 	}
30898f9d5b1aSChuck Lever 
3090c740eff8SChuck Lever 	if (xprt_bound(xprt))
3091c740eff8SChuck Lever 		dprintk("RPC:       set up xprt to %s (port %s) via %s\n",
3092c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_ADDR],
3093c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_PORT],
3094c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_PROTO]);
3095c740eff8SChuck Lever 	else
3096c740eff8SChuck Lever 		dprintk("RPC:       set up xprt to %s (autobind) via %s\n",
3097c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_ADDR],
3098c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_PROTO]);
3099c740eff8SChuck Lever 
3100bc25571eS\"Talpey, Thomas\ 	if (try_module_get(THIS_MODULE))
3101c8541ecdSChuck Lever 		return xprt;
31020a68b0beSJ. Bruce Fields 	ret = ERR_PTR(-EINVAL);
31030a68b0beSJ. Bruce Fields out_err:
3104315f3812SKinglong Mee 	xs_xprt_free(xprt);
31050a68b0beSJ. Bruce Fields 	return ret;
3106a246b010SChuck Lever }
3107282b32e1SChuck Lever 
3108f300babaSAlexandros Batsakis /**
3109f300babaSAlexandros Batsakis  * xs_setup_bc_tcp - Set up transport to use a TCP backchannel socket
3110f300babaSAlexandros Batsakis  * @args: rpc transport creation arguments
3111f300babaSAlexandros Batsakis  *
3112f300babaSAlexandros Batsakis  */
3113f300babaSAlexandros Batsakis static struct rpc_xprt *xs_setup_bc_tcp(struct xprt_create *args)
3114f300babaSAlexandros Batsakis {
3115f300babaSAlexandros Batsakis 	struct sockaddr *addr = args->dstaddr;
3116f300babaSAlexandros Batsakis 	struct rpc_xprt *xprt;
3117f300babaSAlexandros Batsakis 	struct sock_xprt *transport;
3118f300babaSAlexandros Batsakis 	struct svc_sock *bc_sock;
31190a68b0beSJ. Bruce Fields 	struct rpc_xprt *ret;
3120f300babaSAlexandros Batsakis 
3121d9ba131dSTrond Myklebust 	xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
3122d9ba131dSTrond Myklebust 			xprt_tcp_slot_table_entries);
3123f300babaSAlexandros Batsakis 	if (IS_ERR(xprt))
3124f300babaSAlexandros Batsakis 		return xprt;
3125f300babaSAlexandros Batsakis 	transport = container_of(xprt, struct sock_xprt, xprt);
3126f300babaSAlexandros Batsakis 
3127f300babaSAlexandros Batsakis 	xprt->prot = IPPROTO_TCP;
3128f300babaSAlexandros Batsakis 	xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
3129f300babaSAlexandros Batsakis 	xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
3130f300babaSAlexandros Batsakis 	xprt->timeout = &xs_tcp_default_timeout;
3131f300babaSAlexandros Batsakis 
3132f300babaSAlexandros Batsakis 	/* backchannel */
3133f300babaSAlexandros Batsakis 	xprt_set_bound(xprt);
3134f300babaSAlexandros Batsakis 	xprt->bind_timeout = 0;
3135f300babaSAlexandros Batsakis 	xprt->reestablish_timeout = 0;
3136f300babaSAlexandros Batsakis 	xprt->idle_timeout = 0;
3137f300babaSAlexandros Batsakis 
3138f300babaSAlexandros Batsakis 	xprt->ops = &bc_tcp_ops;
3139f300babaSAlexandros Batsakis 
3140f300babaSAlexandros Batsakis 	switch (addr->sa_family) {
3141f300babaSAlexandros Batsakis 	case AF_INET:
3142f300babaSAlexandros Batsakis 		xs_format_peer_addresses(xprt, "tcp",
3143f300babaSAlexandros Batsakis 					 RPCBIND_NETID_TCP);
3144f300babaSAlexandros Batsakis 		break;
3145f300babaSAlexandros Batsakis 	case AF_INET6:
3146f300babaSAlexandros Batsakis 		xs_format_peer_addresses(xprt, "tcp",
3147f300babaSAlexandros Batsakis 				   RPCBIND_NETID_TCP6);
3148f300babaSAlexandros Batsakis 		break;
3149f300babaSAlexandros Batsakis 	default:
31500a68b0beSJ. Bruce Fields 		ret = ERR_PTR(-EAFNOSUPPORT);
31510a68b0beSJ. Bruce Fields 		goto out_err;
3152f300babaSAlexandros Batsakis 	}
3153f300babaSAlexandros Batsakis 
3154f300babaSAlexandros Batsakis 	dprintk("RPC:       set up xprt to %s (port %s) via %s\n",
3155f300babaSAlexandros Batsakis 			xprt->address_strings[RPC_DISPLAY_ADDR],
3156f300babaSAlexandros Batsakis 			xprt->address_strings[RPC_DISPLAY_PORT],
3157f300babaSAlexandros Batsakis 			xprt->address_strings[RPC_DISPLAY_PROTO]);
3158f300babaSAlexandros Batsakis 
3159f300babaSAlexandros Batsakis 	/*
316099de8ea9SJ. Bruce Fields 	 * Once we've associated a backchannel xprt with a connection,
316128303ca3SWeng Meiling 	 * we want to keep it around as long as the connection lasts,
316228303ca3SWeng Meiling 	 * in case we need to start using it for a backchannel again;
316328303ca3SWeng Meiling 	 * this reference won't be dropped until bc_xprt is destroyed.
316499de8ea9SJ. Bruce Fields 	 */
316599de8ea9SJ. Bruce Fields 	xprt_get(xprt);
316699de8ea9SJ. Bruce Fields 	args->bc_xprt->xpt_bc_xprt = xprt;
316799de8ea9SJ. Bruce Fields 	xprt->bc_xprt = args->bc_xprt;
316899de8ea9SJ. Bruce Fields 	bc_sock = container_of(args->bc_xprt, struct svc_sock, sk_xprt);
316999de8ea9SJ. Bruce Fields 	transport->sock = bc_sock->sk_sock;
317099de8ea9SJ. Bruce Fields 	transport->inet = bc_sock->sk_sk;
317199de8ea9SJ. Bruce Fields 
317299de8ea9SJ. Bruce Fields 	/*
3173f300babaSAlexandros Batsakis 	 * Since we don't want connections for the backchannel, we set
3174f300babaSAlexandros Batsakis 	 * the xprt status to connected
3175f300babaSAlexandros Batsakis 	 */
3176f300babaSAlexandros Batsakis 	xprt_set_connected(xprt);
3177f300babaSAlexandros Batsakis 
3178f300babaSAlexandros Batsakis 	if (try_module_get(THIS_MODULE))
3179f300babaSAlexandros Batsakis 		return xprt;
3180642aab58SKinglong Mee 
3181642aab58SKinglong Mee 	args->bc_xprt->xpt_bc_xprt = NULL;
318239a9beabSJ. Bruce Fields 	args->bc_xprt->xpt_bc_xps = NULL;
318399de8ea9SJ. Bruce Fields 	xprt_put(xprt);
31840a68b0beSJ. Bruce Fields 	ret = ERR_PTR(-EINVAL);
31850a68b0beSJ. Bruce Fields out_err:
3186315f3812SKinglong Mee 	xs_xprt_free(xprt);
31870a68b0beSJ. Bruce Fields 	return ret;
3188f300babaSAlexandros Batsakis }
3189f300babaSAlexandros Batsakis 
3190176e21eeSChuck Lever static struct xprt_class	xs_local_transport = {
3191176e21eeSChuck Lever 	.list		= LIST_HEAD_INIT(xs_local_transport.list),
3192176e21eeSChuck Lever 	.name		= "named UNIX socket",
3193176e21eeSChuck Lever 	.owner		= THIS_MODULE,
3194176e21eeSChuck Lever 	.ident		= XPRT_TRANSPORT_LOCAL,
3195176e21eeSChuck Lever 	.setup		= xs_setup_local,
3196176e21eeSChuck Lever };
3197176e21eeSChuck Lever 
3198bc25571eS\"Talpey, Thomas\ static struct xprt_class	xs_udp_transport = {
3199bc25571eS\"Talpey, Thomas\ 	.list		= LIST_HEAD_INIT(xs_udp_transport.list),
3200bc25571eS\"Talpey, Thomas\ 	.name		= "udp",
3201bc25571eS\"Talpey, Thomas\ 	.owner		= THIS_MODULE,
3202f300babaSAlexandros Batsakis 	.ident		= XPRT_TRANSPORT_UDP,
3203bc25571eS\"Talpey, Thomas\ 	.setup		= xs_setup_udp,
3204bc25571eS\"Talpey, Thomas\ };
3205bc25571eS\"Talpey, Thomas\ 
3206bc25571eS\"Talpey, Thomas\ static struct xprt_class	xs_tcp_transport = {
3207bc25571eS\"Talpey, Thomas\ 	.list		= LIST_HEAD_INIT(xs_tcp_transport.list),
3208bc25571eS\"Talpey, Thomas\ 	.name		= "tcp",
3209bc25571eS\"Talpey, Thomas\ 	.owner		= THIS_MODULE,
3210f300babaSAlexandros Batsakis 	.ident		= XPRT_TRANSPORT_TCP,
3211bc25571eS\"Talpey, Thomas\ 	.setup		= xs_setup_tcp,
3212bc25571eS\"Talpey, Thomas\ };
3213bc25571eS\"Talpey, Thomas\ 
3214f300babaSAlexandros Batsakis static struct xprt_class	xs_bc_tcp_transport = {
3215f300babaSAlexandros Batsakis 	.list		= LIST_HEAD_INIT(xs_bc_tcp_transport.list),
3216f300babaSAlexandros Batsakis 	.name		= "tcp NFSv4.1 backchannel",
3217f300babaSAlexandros Batsakis 	.owner		= THIS_MODULE,
3218f300babaSAlexandros Batsakis 	.ident		= XPRT_TRANSPORT_BC_TCP,
3219f300babaSAlexandros Batsakis 	.setup		= xs_setup_bc_tcp,
3220f300babaSAlexandros Batsakis };
3221f300babaSAlexandros Batsakis 
3222282b32e1SChuck Lever /**
3223bc25571eS\"Talpey, Thomas\  * init_socket_xprt - set up xprtsock's sysctls, register with RPC client
3224282b32e1SChuck Lever  *
3225282b32e1SChuck Lever  */
3226282b32e1SChuck Lever int init_socket_xprt(void)
3227282b32e1SChuck Lever {
3228f895b252SJeff Layton #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
32292b1bec5fSEric W. Biederman 	if (!sunrpc_table_header)
32300b4d4147SEric W. Biederman 		sunrpc_table_header = register_sysctl_table(sunrpc_table);
3231fbf76683SChuck Lever #endif
3232fbf76683SChuck Lever 
3233176e21eeSChuck Lever 	xprt_register_transport(&xs_local_transport);
3234bc25571eS\"Talpey, Thomas\ 	xprt_register_transport(&xs_udp_transport);
3235bc25571eS\"Talpey, Thomas\ 	xprt_register_transport(&xs_tcp_transport);
3236f300babaSAlexandros Batsakis 	xprt_register_transport(&xs_bc_tcp_transport);
3237bc25571eS\"Talpey, Thomas\ 
3238282b32e1SChuck Lever 	return 0;
3239282b32e1SChuck Lever }
3240282b32e1SChuck Lever 
3241282b32e1SChuck Lever /**
3242bc25571eS\"Talpey, Thomas\  * cleanup_socket_xprt - remove xprtsock's sysctls, unregister
3243282b32e1SChuck Lever  *
3244282b32e1SChuck Lever  */
3245282b32e1SChuck Lever void cleanup_socket_xprt(void)
3246282b32e1SChuck Lever {
3247f895b252SJeff Layton #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
3248fbf76683SChuck Lever 	if (sunrpc_table_header) {
3249fbf76683SChuck Lever 		unregister_sysctl_table(sunrpc_table_header);
3250fbf76683SChuck Lever 		sunrpc_table_header = NULL;
3251fbf76683SChuck Lever 	}
3252fbf76683SChuck Lever #endif
3253bc25571eS\"Talpey, Thomas\ 
3254176e21eeSChuck Lever 	xprt_unregister_transport(&xs_local_transport);
3255bc25571eS\"Talpey, Thomas\ 	xprt_unregister_transport(&xs_udp_transport);
3256bc25571eS\"Talpey, Thomas\ 	xprt_unregister_transport(&xs_tcp_transport);
3257f300babaSAlexandros Batsakis 	xprt_unregister_transport(&xs_bc_tcp_transport);
3258282b32e1SChuck Lever }
3259cbf11071STrond Myklebust 
32609bbb9e5aSRusty Russell static int param_set_uint_minmax(const char *val,
32619bbb9e5aSRusty Russell 		const struct kernel_param *kp,
3262cbf11071STrond Myklebust 		unsigned int min, unsigned int max)
3263cbf11071STrond Myklebust {
326400cfaa94SDaniel Walter 	unsigned int num;
3265cbf11071STrond Myklebust 	int ret;
3266cbf11071STrond Myklebust 
3267cbf11071STrond Myklebust 	if (!val)
3268cbf11071STrond Myklebust 		return -EINVAL;
326900cfaa94SDaniel Walter 	ret = kstrtouint(val, 0, &num);
32709761a246SDan Carpenter 	if (ret)
32719761a246SDan Carpenter 		return ret;
32729761a246SDan Carpenter 	if (num < min || num > max)
3273cbf11071STrond Myklebust 		return -EINVAL;
3274cbf11071STrond Myklebust 	*((unsigned int *)kp->arg) = num;
3275cbf11071STrond Myklebust 	return 0;
3276cbf11071STrond Myklebust }
3277cbf11071STrond Myklebust 
32789bbb9e5aSRusty Russell static int param_set_portnr(const char *val, const struct kernel_param *kp)
3279cbf11071STrond Myklebust {
3280ffb6ca33SFrank Sorenson 	if (kp->arg == &xprt_min_resvport)
3281cbf11071STrond Myklebust 		return param_set_uint_minmax(val, kp,
3282cbf11071STrond Myklebust 			RPC_MIN_RESVPORT,
3283ffb6ca33SFrank Sorenson 			xprt_max_resvport);
3284ffb6ca33SFrank Sorenson 	return param_set_uint_minmax(val, kp,
3285ffb6ca33SFrank Sorenson 			xprt_min_resvport,
3286cbf11071STrond Myklebust 			RPC_MAX_RESVPORT);
3287cbf11071STrond Myklebust }
3288cbf11071STrond Myklebust 
32899c27847dSLuis R. Rodriguez static const struct kernel_param_ops param_ops_portnr = {
32909bbb9e5aSRusty Russell 	.set = param_set_portnr,
32919bbb9e5aSRusty Russell 	.get = param_get_uint,
32929bbb9e5aSRusty Russell };
32939bbb9e5aSRusty Russell 
3294cbf11071STrond Myklebust #define param_check_portnr(name, p) \
3295cbf11071STrond Myklebust 	__param_check(name, p, unsigned int);
3296cbf11071STrond Myklebust 
3297cbf11071STrond Myklebust module_param_named(min_resvport, xprt_min_resvport, portnr, 0644);
3298cbf11071STrond Myklebust module_param_named(max_resvport, xprt_max_resvport, portnr, 0644);
3299cbf11071STrond Myklebust 
33009bbb9e5aSRusty Russell static int param_set_slot_table_size(const char *val,
33019bbb9e5aSRusty Russell 				     const struct kernel_param *kp)
3302cbf11071STrond Myklebust {
3303cbf11071STrond Myklebust 	return param_set_uint_minmax(val, kp,
3304cbf11071STrond Myklebust 			RPC_MIN_SLOT_TABLE,
3305cbf11071STrond Myklebust 			RPC_MAX_SLOT_TABLE);
3306cbf11071STrond Myklebust }
3307cbf11071STrond Myklebust 
33089c27847dSLuis R. Rodriguez static const struct kernel_param_ops param_ops_slot_table_size = {
33099bbb9e5aSRusty Russell 	.set = param_set_slot_table_size,
33109bbb9e5aSRusty Russell 	.get = param_get_uint,
33119bbb9e5aSRusty Russell };
33129bbb9e5aSRusty Russell 
3313cbf11071STrond Myklebust #define param_check_slot_table_size(name, p) \
3314cbf11071STrond Myklebust 	__param_check(name, p, unsigned int);
3315cbf11071STrond Myklebust 
3316d9ba131dSTrond Myklebust static int param_set_max_slot_table_size(const char *val,
3317d9ba131dSTrond Myklebust 				     const struct kernel_param *kp)
3318d9ba131dSTrond Myklebust {
3319d9ba131dSTrond Myklebust 	return param_set_uint_minmax(val, kp,
3320d9ba131dSTrond Myklebust 			RPC_MIN_SLOT_TABLE,
3321d9ba131dSTrond Myklebust 			RPC_MAX_SLOT_TABLE_LIMIT);
3322d9ba131dSTrond Myklebust }
3323d9ba131dSTrond Myklebust 
33249c27847dSLuis R. Rodriguez static const struct kernel_param_ops param_ops_max_slot_table_size = {
3325d9ba131dSTrond Myklebust 	.set = param_set_max_slot_table_size,
3326d9ba131dSTrond Myklebust 	.get = param_get_uint,
3327d9ba131dSTrond Myklebust };
3328d9ba131dSTrond Myklebust 
3329d9ba131dSTrond Myklebust #define param_check_max_slot_table_size(name, p) \
3330d9ba131dSTrond Myklebust 	__param_check(name, p, unsigned int);
3331d9ba131dSTrond Myklebust 
3332cbf11071STrond Myklebust module_param_named(tcp_slot_table_entries, xprt_tcp_slot_table_entries,
3333cbf11071STrond Myklebust 		   slot_table_size, 0644);
3334d9ba131dSTrond Myklebust module_param_named(tcp_max_slot_table_entries, xprt_max_tcp_slot_table_entries,
3335d9ba131dSTrond Myklebust 		   max_slot_table_size, 0644);
3336cbf11071STrond Myklebust module_param_named(udp_slot_table_entries, xprt_udp_slot_table_entries,
3337cbf11071STrond Myklebust 		   slot_table_size, 0644);
3338