xref: /openbmc/linux/net/sunrpc/xprtsock.c (revision 4388ce05fa38b17e7d9ddabffcb16ed778ee417c)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2a246b010SChuck Lever /*
3a246b010SChuck Lever  * linux/net/sunrpc/xprtsock.c
4a246b010SChuck Lever  *
5a246b010SChuck Lever  * Client-side transport implementation for sockets.
6a246b010SChuck Lever  *
7113aa838SAlan Cox  * TCP callback races fixes (C) 1998 Red Hat
8113aa838SAlan Cox  * TCP send fixes (C) 1998 Red Hat
9a246b010SChuck Lever  * TCP NFS related read + write fixes
10a246b010SChuck Lever  *  (C) 1999 Dave Airlie, University of Limerick, Ireland <airlied@linux.ie>
11a246b010SChuck Lever  *
12a246b010SChuck Lever  * Rewrite of larges part of the code in order to stabilize TCP stuff.
13a246b010SChuck Lever  * Fix behaviour when socket buffer is full.
14a246b010SChuck Lever  *  (C) 1999 Trond Myklebust <trond.myklebust@fys.uio.no>
1555aa4f58SChuck Lever  *
1655aa4f58SChuck Lever  * IP socket transport implementation, (C) 2005 Chuck Lever <cel@netapp.com>
178f9d5b1aSChuck Lever  *
188f9d5b1aSChuck Lever  * IPv6 support contributed by Gilles Quillard, Bull Open Source, 2005.
198f9d5b1aSChuck Lever  *   <gilles.quillard@bull.net>
20a246b010SChuck Lever  */
21a246b010SChuck Lever 
22a246b010SChuck Lever #include <linux/types.h>
23176e21eeSChuck Lever #include <linux/string.h>
24a246b010SChuck Lever #include <linux/slab.h>
25bc25571eS\"Talpey, Thomas\ #include <linux/module.h>
26a246b010SChuck Lever #include <linux/capability.h>
27a246b010SChuck Lever #include <linux/pagemap.h>
28a246b010SChuck Lever #include <linux/errno.h>
29a246b010SChuck Lever #include <linux/socket.h>
30a246b010SChuck Lever #include <linux/in.h>
31a246b010SChuck Lever #include <linux/net.h>
32a246b010SChuck Lever #include <linux/mm.h>
33176e21eeSChuck Lever #include <linux/un.h>
34a246b010SChuck Lever #include <linux/udp.h>
35a246b010SChuck Lever #include <linux/tcp.h>
36a246b010SChuck Lever #include <linux/sunrpc/clnt.h>
375976687aSJeff Layton #include <linux/sunrpc/addr.h>
3802107148SChuck Lever #include <linux/sunrpc/sched.h>
394cfc7e60SRahul Iyer #include <linux/sunrpc/svcsock.h>
4049c36fccS\"Talpey, Thomas\ #include <linux/sunrpc/xprtsock.h>
41a246b010SChuck Lever #include <linux/file.h>
429e00abc3STrond Myklebust #ifdef CONFIG_SUNRPC_BACKCHANNEL
4344b98efdSRicardo Labiaga #include <linux/sunrpc/bc_xprt.h>
4444b98efdSRicardo Labiaga #endif
45a246b010SChuck Lever 
46a246b010SChuck Lever #include <net/sock.h>
47a246b010SChuck Lever #include <net/checksum.h>
48a246b010SChuck Lever #include <net/udp.h>
49a246b010SChuck Lever #include <net/tcp.h>
50277e4ab7STrond Myklebust #include <linux/bvec.h>
516a829eb8STrond Myklebust #include <linux/highmem.h>
52277e4ab7STrond Myklebust #include <linux/uio.h>
53a1231fdaSTrond Myklebust #include <linux/sched/mm.h>
54a246b010SChuck Lever 
5540e0b090SPeilin Ye #include <trace/events/sock.h>
5640b5ea0cSTrond Myklebust #include <trace/events/sunrpc.h>
5740b5ea0cSTrond Myklebust 
589e55eef4SChuck Lever #include "socklib.h"
594cfc7e60SRahul Iyer #include "sunrpc.h"
60176e21eeSChuck Lever 
61176e21eeSChuck Lever static void xs_close(struct rpc_xprt *xprt);
623b21f757STrond Myklebust static void xs_set_srcport(struct sock_xprt *transport, struct socket *sock);
637196dbb0STrond Myklebust static void xs_tcp_set_socket_timeouts(struct rpc_xprt *xprt,
647196dbb0STrond Myklebust 		struct socket *sock);
65176e21eeSChuck Lever 
669903cd1cSChuck Lever /*
67c556b754SChuck Lever  * xprtsock tunables
68c556b754SChuck Lever  */
6909acfea5STrond Myklebust static unsigned int xprt_udp_slot_table_entries = RPC_DEF_SLOT_TABLE;
7009acfea5STrond Myklebust static unsigned int xprt_tcp_slot_table_entries = RPC_MIN_SLOT_TABLE;
7109acfea5STrond Myklebust static unsigned int xprt_max_tcp_slot_table_entries = RPC_MAX_SLOT_TABLE;
72c556b754SChuck Lever 
7309acfea5STrond Myklebust static unsigned int xprt_min_resvport = RPC_DEF_MIN_RESVPORT;
7409acfea5STrond Myklebust static unsigned int xprt_max_resvport = RPC_DEF_MAX_RESVPORT;
75c556b754SChuck Lever 
767d1e8255STrond Myklebust #define XS_TCP_LINGER_TO	(15U * HZ)
7725fe6142STrond Myklebust static unsigned int xs_tcp_fin_timeout __read_mostly = XS_TCP_LINGER_TO;
787d1e8255STrond Myklebust 
79c556b754SChuck Lever /*
80fbf76683SChuck Lever  * We can register our own files under /proc/sys/sunrpc by
81c946cb69SLuis Chamberlain  * calling register_sysctl() again.  The files in that
82fbf76683SChuck Lever  * directory become the union of all files registered there.
83fbf76683SChuck Lever  *
84fbf76683SChuck Lever  * We simply need to make sure that we don't collide with
85fbf76683SChuck Lever  * someone else's file names!
86fbf76683SChuck Lever  */
87fbf76683SChuck Lever 
88fbf76683SChuck Lever static unsigned int min_slot_table_size = RPC_MIN_SLOT_TABLE;
89fbf76683SChuck Lever static unsigned int max_slot_table_size = RPC_MAX_SLOT_TABLE;
90d9ba131dSTrond Myklebust static unsigned int max_tcp_slot_table_limit = RPC_MAX_SLOT_TABLE_LIMIT;
91fbf76683SChuck Lever static unsigned int xprt_min_resvport_limit = RPC_MIN_RESVPORT;
92fbf76683SChuck Lever static unsigned int xprt_max_resvport_limit = RPC_MAX_RESVPORT;
93fbf76683SChuck Lever 
94fbf76683SChuck Lever static struct ctl_table_header *sunrpc_table_header;
95fbf76683SChuck Lever 
96d3abc739SOlga Kornievskaia static struct xprt_class xs_local_transport;
97d3abc739SOlga Kornievskaia static struct xprt_class xs_udp_transport;
98d3abc739SOlga Kornievskaia static struct xprt_class xs_tcp_transport;
99d3abc739SOlga Kornievskaia static struct xprt_class xs_bc_tcp_transport;
100d3abc739SOlga Kornievskaia 
101fbf76683SChuck Lever /*
102fbf76683SChuck Lever  * FIXME: changing the UDP slot table size should also resize the UDP
103fbf76683SChuck Lever  *        socket buffers for existing UDP transports
104fbf76683SChuck Lever  */
105fe2c6338SJoe Perches static struct ctl_table xs_tunables_table[] = {
106fbf76683SChuck Lever 	{
107fbf76683SChuck Lever 		.procname	= "udp_slot_table_entries",
108fbf76683SChuck Lever 		.data		= &xprt_udp_slot_table_entries,
109fbf76683SChuck Lever 		.maxlen		= sizeof(unsigned int),
110fbf76683SChuck Lever 		.mode		= 0644,
1116d456111SEric W. Biederman 		.proc_handler	= proc_dointvec_minmax,
112fbf76683SChuck Lever 		.extra1		= &min_slot_table_size,
113fbf76683SChuck Lever 		.extra2		= &max_slot_table_size
114fbf76683SChuck Lever 	},
115fbf76683SChuck Lever 	{
116fbf76683SChuck Lever 		.procname	= "tcp_slot_table_entries",
117fbf76683SChuck Lever 		.data		= &xprt_tcp_slot_table_entries,
118fbf76683SChuck Lever 		.maxlen		= sizeof(unsigned int),
119fbf76683SChuck Lever 		.mode		= 0644,
1206d456111SEric W. Biederman 		.proc_handler	= proc_dointvec_minmax,
121fbf76683SChuck Lever 		.extra1		= &min_slot_table_size,
122fbf76683SChuck Lever 		.extra2		= &max_slot_table_size
123fbf76683SChuck Lever 	},
124fbf76683SChuck Lever 	{
125d9ba131dSTrond Myklebust 		.procname	= "tcp_max_slot_table_entries",
126d9ba131dSTrond Myklebust 		.data		= &xprt_max_tcp_slot_table_entries,
127d9ba131dSTrond Myklebust 		.maxlen		= sizeof(unsigned int),
128d9ba131dSTrond Myklebust 		.mode		= 0644,
129d9ba131dSTrond Myklebust 		.proc_handler	= proc_dointvec_minmax,
130d9ba131dSTrond Myklebust 		.extra1		= &min_slot_table_size,
131d9ba131dSTrond Myklebust 		.extra2		= &max_tcp_slot_table_limit
132d9ba131dSTrond Myklebust 	},
133d9ba131dSTrond Myklebust 	{
134fbf76683SChuck Lever 		.procname	= "min_resvport",
135fbf76683SChuck Lever 		.data		= &xprt_min_resvport,
136fbf76683SChuck Lever 		.maxlen		= sizeof(unsigned int),
137fbf76683SChuck Lever 		.mode		= 0644,
1386d456111SEric W. Biederman 		.proc_handler	= proc_dointvec_minmax,
139fbf76683SChuck Lever 		.extra1		= &xprt_min_resvport_limit,
140826799e6SJ. Bruce Fields 		.extra2		= &xprt_max_resvport_limit
141fbf76683SChuck Lever 	},
142fbf76683SChuck Lever 	{
143fbf76683SChuck Lever 		.procname	= "max_resvport",
144fbf76683SChuck Lever 		.data		= &xprt_max_resvport,
145fbf76683SChuck Lever 		.maxlen		= sizeof(unsigned int),
146fbf76683SChuck Lever 		.mode		= 0644,
1476d456111SEric W. Biederman 		.proc_handler	= proc_dointvec_minmax,
148826799e6SJ. Bruce Fields 		.extra1		= &xprt_min_resvport_limit,
149fbf76683SChuck Lever 		.extra2		= &xprt_max_resvport_limit
150fbf76683SChuck Lever 	},
151fbf76683SChuck Lever 	{
15225fe6142STrond Myklebust 		.procname	= "tcp_fin_timeout",
15325fe6142STrond Myklebust 		.data		= &xs_tcp_fin_timeout,
15425fe6142STrond Myklebust 		.maxlen		= sizeof(xs_tcp_fin_timeout),
15525fe6142STrond Myklebust 		.mode		= 0644,
1566d456111SEric W. Biederman 		.proc_handler	= proc_dointvec_jiffies,
15725fe6142STrond Myklebust 	},
158f8572d8fSEric W. Biederman 	{ },
159fbf76683SChuck Lever };
160fbf76683SChuck Lever 
161fbf76683SChuck Lever /*
16203bf4b70SChuck Lever  * Wait duration for a reply from the RPC portmapper.
16303bf4b70SChuck Lever  */
16403bf4b70SChuck Lever #define XS_BIND_TO		(60U * HZ)
16503bf4b70SChuck Lever 
16603bf4b70SChuck Lever /*
16703bf4b70SChuck Lever  * Delay if a UDP socket connect error occurs.  This is most likely some
16803bf4b70SChuck Lever  * kind of resource problem on the local host.
16903bf4b70SChuck Lever  */
17003bf4b70SChuck Lever #define XS_UDP_REEST_TO		(2U * HZ)
17103bf4b70SChuck Lever 
17203bf4b70SChuck Lever /*
17303bf4b70SChuck Lever  * The reestablish timeout allows clients to delay for a bit before attempting
17403bf4b70SChuck Lever  * to reconnect to a server that just dropped our connection.
17503bf4b70SChuck Lever  *
17603bf4b70SChuck Lever  * We implement an exponential backoff when trying to reestablish a TCP
17703bf4b70SChuck Lever  * transport connection with the server.  Some servers like to drop a TCP
17803bf4b70SChuck Lever  * connection when they are overworked, so we start with a short timeout and
17903bf4b70SChuck Lever  * increase over time if the server is down or not responding.
18003bf4b70SChuck Lever  */
18103bf4b70SChuck Lever #define XS_TCP_INIT_REEST_TO	(3U * HZ)
18203bf4b70SChuck Lever 
18303bf4b70SChuck Lever /*
18403bf4b70SChuck Lever  * TCP idle timeout; client drops the transport socket if it is idle
18503bf4b70SChuck Lever  * for this long.  Note that we also timeout UDP sockets to prevent
18603bf4b70SChuck Lever  * holding port numbers when there is no RPC traffic.
18703bf4b70SChuck Lever  */
18803bf4b70SChuck Lever #define XS_IDLE_DISC_TO		(5U * 60 * HZ)
18903bf4b70SChuck Lever 
190f895b252SJeff Layton #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
191a246b010SChuck Lever # undef  RPC_DEBUG_DATA
1929903cd1cSChuck Lever # define RPCDBG_FACILITY	RPCDBG_TRANS
193a246b010SChuck Lever #endif
194a246b010SChuck Lever 
195a246b010SChuck Lever #ifdef RPC_DEBUG_DATA
1969903cd1cSChuck Lever static void xs_pktdump(char *msg, u32 *packet, unsigned int count)
197a246b010SChuck Lever {
198a246b010SChuck Lever 	u8 *buf = (u8 *) packet;
199a246b010SChuck Lever 	int j;
200a246b010SChuck Lever 
201a246b010SChuck Lever 	dprintk("RPC:       %s\n", msg);
202a246b010SChuck Lever 	for (j = 0; j < count && j < 128; j += 4) {
203a246b010SChuck Lever 		if (!(j & 31)) {
204a246b010SChuck Lever 			if (j)
205a246b010SChuck Lever 				dprintk("\n");
206a246b010SChuck Lever 			dprintk("0x%04x ", j);
207a246b010SChuck Lever 		}
208a246b010SChuck Lever 		dprintk("%02x%02x%02x%02x ",
209a246b010SChuck Lever 			buf[j], buf[j+1], buf[j+2], buf[j+3]);
210a246b010SChuck Lever 	}
211a246b010SChuck Lever 	dprintk("\n");
212a246b010SChuck Lever }
213a246b010SChuck Lever #else
2149903cd1cSChuck Lever static inline void xs_pktdump(char *msg, u32 *packet, unsigned int count)
215a246b010SChuck Lever {
216a246b010SChuck Lever 	/* NOP */
217a246b010SChuck Lever }
218a246b010SChuck Lever #endif
219a246b010SChuck Lever 
2202118071dSTrond Myklebust static inline struct rpc_xprt *xprt_from_sock(struct sock *sk)
2212118071dSTrond Myklebust {
2222118071dSTrond Myklebust 	return (struct rpc_xprt *) sk->sk_user_data;
2232118071dSTrond Myklebust }
2242118071dSTrond Myklebust 
22595392c59SChuck Lever static inline struct sockaddr *xs_addr(struct rpc_xprt *xprt)
226edb267a6SChuck Lever {
22795392c59SChuck Lever 	return (struct sockaddr *) &xprt->addr;
22895392c59SChuck Lever }
22995392c59SChuck Lever 
230176e21eeSChuck Lever static inline struct sockaddr_un *xs_addr_un(struct rpc_xprt *xprt)
231176e21eeSChuck Lever {
232176e21eeSChuck Lever 	return (struct sockaddr_un *) &xprt->addr;
233176e21eeSChuck Lever }
234176e21eeSChuck Lever 
23595392c59SChuck Lever static inline struct sockaddr_in *xs_addr_in(struct rpc_xprt *xprt)
23695392c59SChuck Lever {
23795392c59SChuck Lever 	return (struct sockaddr_in *) &xprt->addr;
23895392c59SChuck Lever }
23995392c59SChuck Lever 
24095392c59SChuck Lever static inline struct sockaddr_in6 *xs_addr_in6(struct rpc_xprt *xprt)
24195392c59SChuck Lever {
24295392c59SChuck Lever 	return (struct sockaddr_in6 *) &xprt->addr;
24395392c59SChuck Lever }
24495392c59SChuck Lever 
245c877b849SChuck Lever static void xs_format_common_peer_addresses(struct rpc_xprt *xprt)
246c877b849SChuck Lever {
247c877b849SChuck Lever 	struct sockaddr *sap = xs_addr(xprt);
2489dc3b095SChuck Lever 	struct sockaddr_in6 *sin6;
2499dc3b095SChuck Lever 	struct sockaddr_in *sin;
250176e21eeSChuck Lever 	struct sockaddr_un *sun;
251c877b849SChuck Lever 	char buf[128];
252c877b849SChuck Lever 
2539dc3b095SChuck Lever 	switch (sap->sa_family) {
254176e21eeSChuck Lever 	case AF_LOCAL:
255176e21eeSChuck Lever 		sun = xs_addr_un(xprt);
256*4388ce05SNeilBrown 		if (sun->sun_path[0]) {
25715bcdc92SWolfram Sang 			strscpy(buf, sun->sun_path, sizeof(buf));
258*4388ce05SNeilBrown 		} else {
259*4388ce05SNeilBrown 			buf[0] = '@';
260*4388ce05SNeilBrown 			strscpy(buf+1, sun->sun_path+1, sizeof(buf)-1);
261*4388ce05SNeilBrown 		}
262176e21eeSChuck Lever 		xprt->address_strings[RPC_DISPLAY_ADDR] =
263176e21eeSChuck Lever 						kstrdup(buf, GFP_KERNEL);
264176e21eeSChuck Lever 		break;
2659dc3b095SChuck Lever 	case AF_INET:
266176e21eeSChuck Lever 		(void)rpc_ntop(sap, buf, sizeof(buf));
267176e21eeSChuck Lever 		xprt->address_strings[RPC_DISPLAY_ADDR] =
268176e21eeSChuck Lever 						kstrdup(buf, GFP_KERNEL);
2699dc3b095SChuck Lever 		sin = xs_addr_in(xprt);
270fc0b5791SJoe Perches 		snprintf(buf, sizeof(buf), "%08x", ntohl(sin->sin_addr.s_addr));
2719dc3b095SChuck Lever 		break;
2729dc3b095SChuck Lever 	case AF_INET6:
273176e21eeSChuck Lever 		(void)rpc_ntop(sap, buf, sizeof(buf));
274176e21eeSChuck Lever 		xprt->address_strings[RPC_DISPLAY_ADDR] =
275176e21eeSChuck Lever 						kstrdup(buf, GFP_KERNEL);
2769dc3b095SChuck Lever 		sin6 = xs_addr_in6(xprt);
277fc0b5791SJoe Perches 		snprintf(buf, sizeof(buf), "%pi6", &sin6->sin6_addr);
2789dc3b095SChuck Lever 		break;
2799dc3b095SChuck Lever 	default:
2809dc3b095SChuck Lever 		BUG();
2819dc3b095SChuck Lever 	}
282176e21eeSChuck Lever 
2839dc3b095SChuck Lever 	xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = kstrdup(buf, GFP_KERNEL);
2849dc3b095SChuck Lever }
2859dc3b095SChuck Lever 
2869dc3b095SChuck Lever static void xs_format_common_peer_ports(struct rpc_xprt *xprt)
2879dc3b095SChuck Lever {
2889dc3b095SChuck Lever 	struct sockaddr *sap = xs_addr(xprt);
2899dc3b095SChuck Lever 	char buf[128];
2909dc3b095SChuck Lever 
29181160e66SJoe Perches 	snprintf(buf, sizeof(buf), "%u", rpc_get_port(sap));
292c877b849SChuck Lever 	xprt->address_strings[RPC_DISPLAY_PORT] = kstrdup(buf, GFP_KERNEL);
293c877b849SChuck Lever 
29481160e66SJoe Perches 	snprintf(buf, sizeof(buf), "%4hx", rpc_get_port(sap));
295c877b849SChuck Lever 	xprt->address_strings[RPC_DISPLAY_HEX_PORT] = kstrdup(buf, GFP_KERNEL);
296c877b849SChuck Lever }
297c877b849SChuck Lever 
2989dc3b095SChuck Lever static void xs_format_peer_addresses(struct rpc_xprt *xprt,
299b454ae90SChuck Lever 				     const char *protocol,
300b454ae90SChuck Lever 				     const char *netid)
301edb267a6SChuck Lever {
302b454ae90SChuck Lever 	xprt->address_strings[RPC_DISPLAY_PROTO] = protocol;
303b454ae90SChuck Lever 	xprt->address_strings[RPC_DISPLAY_NETID] = netid;
304c877b849SChuck Lever 	xs_format_common_peer_addresses(xprt);
3059dc3b095SChuck Lever 	xs_format_common_peer_ports(xprt);
306edb267a6SChuck Lever }
307edb267a6SChuck Lever 
3089dc3b095SChuck Lever static void xs_update_peer_port(struct rpc_xprt *xprt)
3094b6473fbSChuck Lever {
3109dc3b095SChuck Lever 	kfree(xprt->address_strings[RPC_DISPLAY_HEX_PORT]);
3119dc3b095SChuck Lever 	kfree(xprt->address_strings[RPC_DISPLAY_PORT]);
3124b6473fbSChuck Lever 
3139dc3b095SChuck Lever 	xs_format_common_peer_ports(xprt);
314edb267a6SChuck Lever }
315edb267a6SChuck Lever 
316edb267a6SChuck Lever static void xs_free_peer_addresses(struct rpc_xprt *xprt)
317edb267a6SChuck Lever {
31833e01dc7SChuck Lever 	unsigned int i;
31933e01dc7SChuck Lever 
32033e01dc7SChuck Lever 	for (i = 0; i < RPC_DISPLAY_MAX; i++)
32133e01dc7SChuck Lever 		switch (i) {
32233e01dc7SChuck Lever 		case RPC_DISPLAY_PROTO:
32333e01dc7SChuck Lever 		case RPC_DISPLAY_NETID:
32433e01dc7SChuck Lever 			continue;
32533e01dc7SChuck Lever 		default:
32633e01dc7SChuck Lever 			kfree(xprt->address_strings[i]);
32733e01dc7SChuck Lever 		}
328edb267a6SChuck Lever }
329edb267a6SChuck Lever 
330277e4ab7STrond Myklebust static size_t
331277e4ab7STrond Myklebust xs_alloc_sparse_pages(struct xdr_buf *buf, size_t want, gfp_t gfp)
332277e4ab7STrond Myklebust {
333277e4ab7STrond Myklebust 	size_t i,n;
334277e4ab7STrond Myklebust 
33516e5e90fSTrond Myklebust 	if (!want || !(buf->flags & XDRBUF_SPARSE_PAGES))
336277e4ab7STrond Myklebust 		return want;
337277e4ab7STrond Myklebust 	n = (buf->page_base + want + PAGE_SIZE - 1) >> PAGE_SHIFT;
338277e4ab7STrond Myklebust 	for (i = 0; i < n; i++) {
339277e4ab7STrond Myklebust 		if (buf->pages[i])
340277e4ab7STrond Myklebust 			continue;
341277e4ab7STrond Myklebust 		buf->bvec[i].bv_page = buf->pages[i] = alloc_page(gfp);
342277e4ab7STrond Myklebust 		if (!buf->pages[i]) {
34316e5e90fSTrond Myklebust 			i *= PAGE_SIZE;
34416e5e90fSTrond Myklebust 			return i > buf->page_base ? i - buf->page_base : 0;
345277e4ab7STrond Myklebust 		}
346277e4ab7STrond Myklebust 	}
347277e4ab7STrond Myklebust 	return want;
348277e4ab7STrond Myklebust }
349277e4ab7STrond Myklebust 
350277e4ab7STrond Myklebust static ssize_t
351277e4ab7STrond Myklebust xs_sock_recvmsg(struct socket *sock, struct msghdr *msg, int flags, size_t seek)
352277e4ab7STrond Myklebust {
353277e4ab7STrond Myklebust 	ssize_t ret;
354277e4ab7STrond Myklebust 	if (seek != 0)
355277e4ab7STrond Myklebust 		iov_iter_advance(&msg->msg_iter, seek);
356277e4ab7STrond Myklebust 	ret = sock_recvmsg(sock, msg, flags);
357277e4ab7STrond Myklebust 	return ret > 0 ? ret + seek : ret;
358277e4ab7STrond Myklebust }
359277e4ab7STrond Myklebust 
360277e4ab7STrond Myklebust static ssize_t
361277e4ab7STrond Myklebust xs_read_kvec(struct socket *sock, struct msghdr *msg, int flags,
362277e4ab7STrond Myklebust 		struct kvec *kvec, size_t count, size_t seek)
363277e4ab7STrond Myklebust {
364de4eda9dSAl Viro 	iov_iter_kvec(&msg->msg_iter, ITER_DEST, kvec, 1, count);
365277e4ab7STrond Myklebust 	return xs_sock_recvmsg(sock, msg, flags, seek);
366277e4ab7STrond Myklebust }
367277e4ab7STrond Myklebust 
368277e4ab7STrond Myklebust static ssize_t
369277e4ab7STrond Myklebust xs_read_bvec(struct socket *sock, struct msghdr *msg, int flags,
370277e4ab7STrond Myklebust 		struct bio_vec *bvec, unsigned long nr, size_t count,
371277e4ab7STrond Myklebust 		size_t seek)
372277e4ab7STrond Myklebust {
373de4eda9dSAl Viro 	iov_iter_bvec(&msg->msg_iter, ITER_DEST, bvec, nr, count);
374277e4ab7STrond Myklebust 	return xs_sock_recvmsg(sock, msg, flags, seek);
375277e4ab7STrond Myklebust }
376277e4ab7STrond Myklebust 
377277e4ab7STrond Myklebust static ssize_t
378277e4ab7STrond Myklebust xs_read_discard(struct socket *sock, struct msghdr *msg, int flags,
379277e4ab7STrond Myklebust 		size_t count)
380277e4ab7STrond Myklebust {
381de4eda9dSAl Viro 	iov_iter_discard(&msg->msg_iter, ITER_DEST, count);
382b76a5afdSTrond Myklebust 	return sock_recvmsg(sock, msg, flags);
383277e4ab7STrond Myklebust }
384277e4ab7STrond Myklebust 
3856a829eb8STrond Myklebust #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
3866a829eb8STrond Myklebust static void
3876a829eb8STrond Myklebust xs_flush_bvec(const struct bio_vec *bvec, size_t count, size_t seek)
3886a829eb8STrond Myklebust {
3896a829eb8STrond Myklebust 	struct bvec_iter bi = {
3906a829eb8STrond Myklebust 		.bi_size = count,
3916a829eb8STrond Myklebust 	};
3926a829eb8STrond Myklebust 	struct bio_vec bv;
3936a829eb8STrond Myklebust 
3946a829eb8STrond Myklebust 	bvec_iter_advance(bvec, &bi, seek & PAGE_MASK);
3956a829eb8STrond Myklebust 	for_each_bvec(bv, bvec, bi, bi)
3966a829eb8STrond Myklebust 		flush_dcache_page(bv.bv_page);
3976a829eb8STrond Myklebust }
3986a829eb8STrond Myklebust #else
3996a829eb8STrond Myklebust static inline void
4006a829eb8STrond Myklebust xs_flush_bvec(const struct bio_vec *bvec, size_t count, size_t seek)
4016a829eb8STrond Myklebust {
4026a829eb8STrond Myklebust }
4036a829eb8STrond Myklebust #endif
4046a829eb8STrond Myklebust 
405277e4ab7STrond Myklebust static ssize_t
406277e4ab7STrond Myklebust xs_read_xdr_buf(struct socket *sock, struct msghdr *msg, int flags,
407277e4ab7STrond Myklebust 		struct xdr_buf *buf, size_t count, size_t seek, size_t *read)
408277e4ab7STrond Myklebust {
409277e4ab7STrond Myklebust 	size_t want, seek_init = seek, offset = 0;
410277e4ab7STrond Myklebust 	ssize_t ret;
411277e4ab7STrond Myklebust 
412277e4ab7STrond Myklebust 	want = min_t(size_t, count, buf->head[0].iov_len);
413e92053a5STrond Myklebust 	if (seek < want) {
414277e4ab7STrond Myklebust 		ret = xs_read_kvec(sock, msg, flags, &buf->head[0], want, seek);
415277e4ab7STrond Myklebust 		if (ret <= 0)
416277e4ab7STrond Myklebust 			goto sock_err;
417277e4ab7STrond Myklebust 		offset += ret;
418277e4ab7STrond Myklebust 		if (offset == count || msg->msg_flags & (MSG_EOR|MSG_TRUNC))
419277e4ab7STrond Myklebust 			goto out;
420277e4ab7STrond Myklebust 		if (ret != want)
421c4433055STrond Myklebust 			goto out;
422277e4ab7STrond Myklebust 		seek = 0;
423277e4ab7STrond Myklebust 	} else {
424e92053a5STrond Myklebust 		seek -= want;
425e92053a5STrond Myklebust 		offset += want;
426277e4ab7STrond Myklebust 	}
42716e5e90fSTrond Myklebust 
428b2648015STrond Myklebust 	want = xs_alloc_sparse_pages(
429b2648015STrond Myklebust 		buf, min_t(size_t, count - offset, buf->page_len),
430b2648015STrond Myklebust 		GFP_KERNEL | __GFP_NORETRY | __GFP_NOWARN);
43116e5e90fSTrond Myklebust 	if (seek < want) {
432277e4ab7STrond Myklebust 		ret = xs_read_bvec(sock, msg, flags, buf->bvec,
433277e4ab7STrond Myklebust 				xdr_buf_pagecount(buf),
434277e4ab7STrond Myklebust 				want + buf->page_base,
435277e4ab7STrond Myklebust 				seek + buf->page_base);
436277e4ab7STrond Myklebust 		if (ret <= 0)
437277e4ab7STrond Myklebust 			goto sock_err;
4386a829eb8STrond Myklebust 		xs_flush_bvec(buf->bvec, ret, seek + buf->page_base);
439ac9645c8SDan Aloni 		ret -= buf->page_base;
440ac9645c8SDan Aloni 		offset += ret;
441277e4ab7STrond Myklebust 		if (offset == count || msg->msg_flags & (MSG_EOR|MSG_TRUNC))
442277e4ab7STrond Myklebust 			goto out;
443277e4ab7STrond Myklebust 		if (ret != want)
444c4433055STrond Myklebust 			goto out;
445277e4ab7STrond Myklebust 		seek = 0;
446277e4ab7STrond Myklebust 	} else {
44716e5e90fSTrond Myklebust 		seek -= want;
44816e5e90fSTrond Myklebust 		offset += want;
449277e4ab7STrond Myklebust 	}
45016e5e90fSTrond Myklebust 
451277e4ab7STrond Myklebust 	want = min_t(size_t, count - offset, buf->tail[0].iov_len);
452e92053a5STrond Myklebust 	if (seek < want) {
453277e4ab7STrond Myklebust 		ret = xs_read_kvec(sock, msg, flags, &buf->tail[0], want, seek);
454277e4ab7STrond Myklebust 		if (ret <= 0)
455277e4ab7STrond Myklebust 			goto sock_err;
456277e4ab7STrond Myklebust 		offset += ret;
457277e4ab7STrond Myklebust 		if (offset == count || msg->msg_flags & (MSG_EOR|MSG_TRUNC))
458277e4ab7STrond Myklebust 			goto out;
459277e4ab7STrond Myklebust 		if (ret != want)
460c4433055STrond Myklebust 			goto out;
4619734ad57STrond Myklebust 	} else if (offset < seek_init)
462e92053a5STrond Myklebust 		offset = seek_init;
463277e4ab7STrond Myklebust 	ret = -EMSGSIZE;
464277e4ab7STrond Myklebust out:
465277e4ab7STrond Myklebust 	*read = offset - seek_init;
466277e4ab7STrond Myklebust 	return ret;
467277e4ab7STrond Myklebust sock_err:
468277e4ab7STrond Myklebust 	offset += seek;
469277e4ab7STrond Myklebust 	goto out;
470277e4ab7STrond Myklebust }
471277e4ab7STrond Myklebust 
472277e4ab7STrond Myklebust static void
473277e4ab7STrond Myklebust xs_read_header(struct sock_xprt *transport, struct xdr_buf *buf)
474277e4ab7STrond Myklebust {
475277e4ab7STrond Myklebust 	if (!transport->recv.copied) {
476277e4ab7STrond Myklebust 		if (buf->head[0].iov_len >= transport->recv.offset)
477277e4ab7STrond Myklebust 			memcpy(buf->head[0].iov_base,
478277e4ab7STrond Myklebust 					&transport->recv.xid,
479277e4ab7STrond Myklebust 					transport->recv.offset);
480277e4ab7STrond Myklebust 		transport->recv.copied = transport->recv.offset;
481277e4ab7STrond Myklebust 	}
482277e4ab7STrond Myklebust }
483277e4ab7STrond Myklebust 
484277e4ab7STrond Myklebust static bool
485277e4ab7STrond Myklebust xs_read_stream_request_done(struct sock_xprt *transport)
486277e4ab7STrond Myklebust {
487277e4ab7STrond Myklebust 	return transport->recv.fraghdr & cpu_to_be32(RPC_LAST_STREAM_FRAGMENT);
488277e4ab7STrond Myklebust }
489277e4ab7STrond Myklebust 
490e92053a5STrond Myklebust static void
491e92053a5STrond Myklebust xs_read_stream_check_eor(struct sock_xprt *transport,
492e92053a5STrond Myklebust 		struct msghdr *msg)
493e92053a5STrond Myklebust {
494e92053a5STrond Myklebust 	if (xs_read_stream_request_done(transport))
495e92053a5STrond Myklebust 		msg->msg_flags |= MSG_EOR;
496e92053a5STrond Myklebust }
497e92053a5STrond Myklebust 
498277e4ab7STrond Myklebust static ssize_t
499277e4ab7STrond Myklebust xs_read_stream_request(struct sock_xprt *transport, struct msghdr *msg,
500277e4ab7STrond Myklebust 		int flags, struct rpc_rqst *req)
501277e4ab7STrond Myklebust {
502277e4ab7STrond Myklebust 	struct xdr_buf *buf = &req->rq_private_buf;
5033f649ab7SKees Cook 	size_t want, read;
5043f649ab7SKees Cook 	ssize_t ret;
505277e4ab7STrond Myklebust 
506277e4ab7STrond Myklebust 	xs_read_header(transport, buf);
507277e4ab7STrond Myklebust 
508277e4ab7STrond Myklebust 	want = transport->recv.len - transport->recv.offset;
509e92053a5STrond Myklebust 	if (want != 0) {
510277e4ab7STrond Myklebust 		ret = xs_read_xdr_buf(transport->sock, msg, flags, buf,
511e92053a5STrond Myklebust 				transport->recv.copied + want,
512e92053a5STrond Myklebust 				transport->recv.copied,
513277e4ab7STrond Myklebust 				&read);
514277e4ab7STrond Myklebust 		transport->recv.offset += read;
515277e4ab7STrond Myklebust 		transport->recv.copied += read;
516277e4ab7STrond Myklebust 	}
517277e4ab7STrond Myklebust 
518727fcc64STrond Myklebust 	if (transport->recv.offset == transport->recv.len)
519727fcc64STrond Myklebust 		xs_read_stream_check_eor(transport, msg);
520727fcc64STrond Myklebust 
521e92053a5STrond Myklebust 	if (want == 0)
522e92053a5STrond Myklebust 		return 0;
523e92053a5STrond Myklebust 
524277e4ab7STrond Myklebust 	switch (ret) {
525c4433055STrond Myklebust 	default:
526c4433055STrond Myklebust 		break;
52726781eabSTrond Myklebust 	case -EFAULT:
528277e4ab7STrond Myklebust 	case -EMSGSIZE:
52926781eabSTrond Myklebust 		msg->msg_flags |= MSG_TRUNC;
530c4433055STrond Myklebust 		return read;
531277e4ab7STrond Myklebust 	case 0:
532277e4ab7STrond Myklebust 		return -ESHUTDOWN;
533277e4ab7STrond Myklebust 	}
534c4433055STrond Myklebust 	return ret < 0 ? ret : read;
535277e4ab7STrond Myklebust }
536277e4ab7STrond Myklebust 
537277e4ab7STrond Myklebust static size_t
538277e4ab7STrond Myklebust xs_read_stream_headersize(bool isfrag)
539277e4ab7STrond Myklebust {
540277e4ab7STrond Myklebust 	if (isfrag)
541277e4ab7STrond Myklebust 		return sizeof(__be32);
542277e4ab7STrond Myklebust 	return 3 * sizeof(__be32);
543277e4ab7STrond Myklebust }
544277e4ab7STrond Myklebust 
545277e4ab7STrond Myklebust static ssize_t
546277e4ab7STrond Myklebust xs_read_stream_header(struct sock_xprt *transport, struct msghdr *msg,
547277e4ab7STrond Myklebust 		int flags, size_t want, size_t seek)
548277e4ab7STrond Myklebust {
549277e4ab7STrond Myklebust 	struct kvec kvec = {
550277e4ab7STrond Myklebust 		.iov_base = &transport->recv.fraghdr,
551277e4ab7STrond Myklebust 		.iov_len = want,
552277e4ab7STrond Myklebust 	};
553277e4ab7STrond Myklebust 	return xs_read_kvec(transport->sock, msg, flags, &kvec, want, seek);
554277e4ab7STrond Myklebust }
555277e4ab7STrond Myklebust 
556277e4ab7STrond Myklebust #if defined(CONFIG_SUNRPC_BACKCHANNEL)
557277e4ab7STrond Myklebust static ssize_t
558277e4ab7STrond Myklebust xs_read_stream_call(struct sock_xprt *transport, struct msghdr *msg, int flags)
559277e4ab7STrond Myklebust {
560277e4ab7STrond Myklebust 	struct rpc_xprt *xprt = &transport->xprt;
561277e4ab7STrond Myklebust 	struct rpc_rqst *req;
562277e4ab7STrond Myklebust 	ssize_t ret;
563277e4ab7STrond Myklebust 
56498b5cee3SBenjamin Coddington 	/* Is this transport associated with the backchannel? */
56598b5cee3SBenjamin Coddington 	if (!xprt->bc_serv)
56698b5cee3SBenjamin Coddington 		return -ESHUTDOWN;
56798b5cee3SBenjamin Coddington 
568277e4ab7STrond Myklebust 	/* Look up and lock the request corresponding to the given XID */
569277e4ab7STrond Myklebust 	req = xprt_lookup_bc_request(xprt, transport->recv.xid);
570277e4ab7STrond Myklebust 	if (!req) {
571277e4ab7STrond Myklebust 		printk(KERN_WARNING "Callback slot table overflowed\n");
572277e4ab7STrond Myklebust 		return -ESHUTDOWN;
573277e4ab7STrond Myklebust 	}
57445835a63STrond Myklebust 	if (transport->recv.copied && !req->rq_private_buf.len)
57545835a63STrond Myklebust 		return -ESHUTDOWN;
576277e4ab7STrond Myklebust 
577277e4ab7STrond Myklebust 	ret = xs_read_stream_request(transport, msg, flags, req);
578277e4ab7STrond Myklebust 	if (msg->msg_flags & (MSG_EOR|MSG_TRUNC))
579c4433055STrond Myklebust 		xprt_complete_bc_request(req, transport->recv.copied);
58045835a63STrond Myklebust 	else
58145835a63STrond Myklebust 		req->rq_private_buf.len = transport->recv.copied;
582277e4ab7STrond Myklebust 
583277e4ab7STrond Myklebust 	return ret;
584277e4ab7STrond Myklebust }
585277e4ab7STrond Myklebust #else /* CONFIG_SUNRPC_BACKCHANNEL */
586277e4ab7STrond Myklebust static ssize_t
587277e4ab7STrond Myklebust xs_read_stream_call(struct sock_xprt *transport, struct msghdr *msg, int flags)
588277e4ab7STrond Myklebust {
589277e4ab7STrond Myklebust 	return -ESHUTDOWN;
590277e4ab7STrond Myklebust }
591277e4ab7STrond Myklebust #endif /* CONFIG_SUNRPC_BACKCHANNEL */
592277e4ab7STrond Myklebust 
593277e4ab7STrond Myklebust static ssize_t
594277e4ab7STrond Myklebust xs_read_stream_reply(struct sock_xprt *transport, struct msghdr *msg, int flags)
595277e4ab7STrond Myklebust {
596277e4ab7STrond Myklebust 	struct rpc_xprt *xprt = &transport->xprt;
597277e4ab7STrond Myklebust 	struct rpc_rqst *req;
598277e4ab7STrond Myklebust 	ssize_t ret = 0;
599277e4ab7STrond Myklebust 
600277e4ab7STrond Myklebust 	/* Look up and lock the request corresponding to the given XID */
601277e4ab7STrond Myklebust 	spin_lock(&xprt->queue_lock);
602277e4ab7STrond Myklebust 	req = xprt_lookup_rqst(xprt, transport->recv.xid);
60345835a63STrond Myklebust 	if (!req || (transport->recv.copied && !req->rq_private_buf.len)) {
604277e4ab7STrond Myklebust 		msg->msg_flags |= MSG_TRUNC;
605277e4ab7STrond Myklebust 		goto out;
606277e4ab7STrond Myklebust 	}
607277e4ab7STrond Myklebust 	xprt_pin_rqst(req);
608277e4ab7STrond Myklebust 	spin_unlock(&xprt->queue_lock);
609277e4ab7STrond Myklebust 
610277e4ab7STrond Myklebust 	ret = xs_read_stream_request(transport, msg, flags, req);
611277e4ab7STrond Myklebust 
612277e4ab7STrond Myklebust 	spin_lock(&xprt->queue_lock);
613277e4ab7STrond Myklebust 	if (msg->msg_flags & (MSG_EOR|MSG_TRUNC))
614c4433055STrond Myklebust 		xprt_complete_rqst(req->rq_task, transport->recv.copied);
61545835a63STrond Myklebust 	else
61645835a63STrond Myklebust 		req->rq_private_buf.len = transport->recv.copied;
617277e4ab7STrond Myklebust 	xprt_unpin_rqst(req);
618277e4ab7STrond Myklebust out:
619277e4ab7STrond Myklebust 	spin_unlock(&xprt->queue_lock);
620277e4ab7STrond Myklebust 	return ret;
621277e4ab7STrond Myklebust }
622277e4ab7STrond Myklebust 
623277e4ab7STrond Myklebust static ssize_t
624277e4ab7STrond Myklebust xs_read_stream(struct sock_xprt *transport, int flags)
625277e4ab7STrond Myklebust {
626277e4ab7STrond Myklebust 	struct msghdr msg = { 0 };
627277e4ab7STrond Myklebust 	size_t want, read = 0;
628277e4ab7STrond Myklebust 	ssize_t ret = 0;
629277e4ab7STrond Myklebust 
630277e4ab7STrond Myklebust 	if (transport->recv.len == 0) {
631277e4ab7STrond Myklebust 		want = xs_read_stream_headersize(transport->recv.copied != 0);
632277e4ab7STrond Myklebust 		ret = xs_read_stream_header(transport, &msg, flags, want,
633277e4ab7STrond Myklebust 				transport->recv.offset);
634277e4ab7STrond Myklebust 		if (ret <= 0)
635277e4ab7STrond Myklebust 			goto out_err;
636277e4ab7STrond Myklebust 		transport->recv.offset = ret;
637c4433055STrond Myklebust 		if (transport->recv.offset != want)
638c4433055STrond Myklebust 			return transport->recv.offset;
639277e4ab7STrond Myklebust 		transport->recv.len = be32_to_cpu(transport->recv.fraghdr) &
640277e4ab7STrond Myklebust 			RPC_FRAGMENT_SIZE_MASK;
641277e4ab7STrond Myklebust 		transport->recv.offset -= sizeof(transport->recv.fraghdr);
642277e4ab7STrond Myklebust 		read = ret;
643277e4ab7STrond Myklebust 	}
644277e4ab7STrond Myklebust 
645277e4ab7STrond Myklebust 	switch (be32_to_cpu(transport->recv.calldir)) {
646c4433055STrond Myklebust 	default:
647c4433055STrond Myklebust 		msg.msg_flags |= MSG_TRUNC;
648c4433055STrond Myklebust 		break;
649277e4ab7STrond Myklebust 	case RPC_CALL:
650277e4ab7STrond Myklebust 		ret = xs_read_stream_call(transport, &msg, flags);
651277e4ab7STrond Myklebust 		break;
652277e4ab7STrond Myklebust 	case RPC_REPLY:
653277e4ab7STrond Myklebust 		ret = xs_read_stream_reply(transport, &msg, flags);
654277e4ab7STrond Myklebust 	}
655277e4ab7STrond Myklebust 	if (msg.msg_flags & MSG_TRUNC) {
656277e4ab7STrond Myklebust 		transport->recv.calldir = cpu_to_be32(-1);
657277e4ab7STrond Myklebust 		transport->recv.copied = -1;
658277e4ab7STrond Myklebust 	}
659277e4ab7STrond Myklebust 	if (ret < 0)
660277e4ab7STrond Myklebust 		goto out_err;
661277e4ab7STrond Myklebust 	read += ret;
662277e4ab7STrond Myklebust 	if (transport->recv.offset < transport->recv.len) {
663c4433055STrond Myklebust 		if (!(msg.msg_flags & MSG_TRUNC))
664c4433055STrond Myklebust 			return read;
665b76a5afdSTrond Myklebust 		msg.msg_flags = 0;
666277e4ab7STrond Myklebust 		ret = xs_read_discard(transport->sock, &msg, flags,
667277e4ab7STrond Myklebust 				transport->recv.len - transport->recv.offset);
668277e4ab7STrond Myklebust 		if (ret <= 0)
669277e4ab7STrond Myklebust 			goto out_err;
670277e4ab7STrond Myklebust 		transport->recv.offset += ret;
671277e4ab7STrond Myklebust 		read += ret;
672277e4ab7STrond Myklebust 		if (transport->recv.offset != transport->recv.len)
673c4433055STrond Myklebust 			return read;
674277e4ab7STrond Myklebust 	}
675277e4ab7STrond Myklebust 	if (xs_read_stream_request_done(transport)) {
676c50b8ee0STrond Myklebust 		trace_xs_stream_read_request(transport);
677277e4ab7STrond Myklebust 		transport->recv.copied = 0;
678277e4ab7STrond Myklebust 	}
679277e4ab7STrond Myklebust 	transport->recv.offset = 0;
680277e4ab7STrond Myklebust 	transport->recv.len = 0;
681277e4ab7STrond Myklebust 	return read;
682277e4ab7STrond Myklebust out_err:
68379462857STrond Myklebust 	return ret != 0 ? ret : -ESHUTDOWN;
684277e4ab7STrond Myklebust }
685277e4ab7STrond Myklebust 
6860ffe86f4STrond Myklebust static __poll_t xs_poll_socket(struct sock_xprt *transport)
6870ffe86f4STrond Myklebust {
688a73881c9STrond Myklebust 	return transport->sock->ops->poll(transport->file, transport->sock,
689a73881c9STrond Myklebust 			NULL);
6900ffe86f4STrond Myklebust }
6910ffe86f4STrond Myklebust 
6920ffe86f4STrond Myklebust static bool xs_poll_socket_readable(struct sock_xprt *transport)
6930ffe86f4STrond Myklebust {
6940ffe86f4STrond Myklebust 	__poll_t events = xs_poll_socket(transport);
6950ffe86f4STrond Myklebust 
6960ffe86f4STrond Myklebust 	return (events & (EPOLLIN | EPOLLRDNORM)) && !(events & EPOLLRDHUP);
6970ffe86f4STrond Myklebust }
6980ffe86f4STrond Myklebust 
6990ffe86f4STrond Myklebust static void xs_poll_check_readable(struct sock_xprt *transport)
7000ffe86f4STrond Myklebust {
7010ffe86f4STrond Myklebust 
7020ffe86f4STrond Myklebust 	clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state);
7030ffe86f4STrond Myklebust 	if (!xs_poll_socket_readable(transport))
7040ffe86f4STrond Myklebust 		return;
7050ffe86f4STrond Myklebust 	if (!test_and_set_bit(XPRT_SOCK_DATA_READY, &transport->sock_state))
7060ffe86f4STrond Myklebust 		queue_work(xprtiod_workqueue, &transport->recv_worker);
7070ffe86f4STrond Myklebust }
7080ffe86f4STrond Myklebust 
709c50b8ee0STrond Myklebust static void xs_stream_data_receive(struct sock_xprt *transport)
710c50b8ee0STrond Myklebust {
711c50b8ee0STrond Myklebust 	size_t read = 0;
712c50b8ee0STrond Myklebust 	ssize_t ret = 0;
713c50b8ee0STrond Myklebust 
714c50b8ee0STrond Myklebust 	mutex_lock(&transport->recv_mutex);
715c50b8ee0STrond Myklebust 	if (transport->sock == NULL)
716c50b8ee0STrond Myklebust 		goto out;
717c50b8ee0STrond Myklebust 	for (;;) {
718c50b8ee0STrond Myklebust 		ret = xs_read_stream(transport, MSG_DONTWAIT);
719c4433055STrond Myklebust 		if (ret < 0)
720c50b8ee0STrond Myklebust 			break;
721c50b8ee0STrond Myklebust 		read += ret;
722c50b8ee0STrond Myklebust 		cond_resched();
723c50b8ee0STrond Myklebust 	}
7245f52a9d4STrond Myklebust 	if (ret == -ESHUTDOWN)
7255f52a9d4STrond Myklebust 		kernel_sock_shutdown(transport->sock, SHUT_RDWR);
7265f52a9d4STrond Myklebust 	else
7270ffe86f4STrond Myklebust 		xs_poll_check_readable(transport);
728c50b8ee0STrond Myklebust out:
729c50b8ee0STrond Myklebust 	mutex_unlock(&transport->recv_mutex);
730c50b8ee0STrond Myklebust 	trace_xs_stream_read_data(&transport->xprt, ret, read);
731c50b8ee0STrond Myklebust }
732c50b8ee0STrond Myklebust 
733c50b8ee0STrond Myklebust static void xs_stream_data_receive_workfn(struct work_struct *work)
734c50b8ee0STrond Myklebust {
735c50b8ee0STrond Myklebust 	struct sock_xprt *transport =
736c50b8ee0STrond Myklebust 		container_of(work, struct sock_xprt, recv_worker);
737a1231fdaSTrond Myklebust 	unsigned int pflags = memalloc_nofs_save();
738a1231fdaSTrond Myklebust 
739c50b8ee0STrond Myklebust 	xs_stream_data_receive(transport);
740a1231fdaSTrond Myklebust 	memalloc_nofs_restore(pflags);
741c50b8ee0STrond Myklebust }
742c50b8ee0STrond Myklebust 
743550aebfeSTrond Myklebust static void
744550aebfeSTrond Myklebust xs_stream_reset_connect(struct sock_xprt *transport)
745550aebfeSTrond Myklebust {
746550aebfeSTrond Myklebust 	transport->recv.offset = 0;
747550aebfeSTrond Myklebust 	transport->recv.len = 0;
748550aebfeSTrond Myklebust 	transport->recv.copied = 0;
749550aebfeSTrond Myklebust 	transport->xmit.offset = 0;
750ae053551STrond Myklebust }
751ae053551STrond Myklebust 
752ae053551STrond Myklebust static void
753ae053551STrond Myklebust xs_stream_start_connect(struct sock_xprt *transport)
754ae053551STrond Myklebust {
755550aebfeSTrond Myklebust 	transport->xprt.stat.connect_count++;
756550aebfeSTrond Myklebust 	transport->xprt.stat.connect_start = jiffies;
757550aebfeSTrond Myklebust }
758550aebfeSTrond Myklebust 
759b4b5cc85SChuck Lever #define XS_SENDMSG_FLAGS	(MSG_DONTWAIT | MSG_NOSIGNAL)
760b4b5cc85SChuck Lever 
7619903cd1cSChuck Lever /**
762c544577dSTrond Myklebust  * xs_nospace - handle transmit was incomplete
76350f484e2STrond Myklebust  * @req: pointer to RPC request
7647496b59fSTrond Myklebust  * @transport: pointer to struct sock_xprt
7659903cd1cSChuck Lever  *
766a246b010SChuck Lever  */
7677496b59fSTrond Myklebust static int xs_nospace(struct rpc_rqst *req, struct sock_xprt *transport)
768a246b010SChuck Lever {
7697496b59fSTrond Myklebust 	struct rpc_xprt *xprt = &transport->xprt;
77006ea0bfeSTrond Myklebust 	struct sock *sk = transport->inet;
77124ca9a84STrond Myklebust 	int ret = -EAGAIN;
772a246b010SChuck Lever 
773015747d2SChuck Lever 	trace_rpc_socket_nospace(req, transport);
774a246b010SChuck Lever 
775262965f5SChuck Lever 	/* Protect against races with write_space */
776b5e92419STrond Myklebust 	spin_lock(&xprt->transport_lock);
777a246b010SChuck Lever 
778262965f5SChuck Lever 	/* Don't race with disconnect */
779b6ddf64fSTrond Myklebust 	if (xprt_connected(xprt)) {
7807496b59fSTrond Myklebust 		/* wait for more buffer space */
7812790a624STrond Myklebust 		set_bit(XPRT_SOCK_NOSPACE, &transport->sock_state);
7827496b59fSTrond Myklebust 		set_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
7837496b59fSTrond Myklebust 		sk->sk_write_pending++;
7847496b59fSTrond Myklebust 		xprt_wait_for_buffer_space(xprt);
7857496b59fSTrond Myklebust 	} else
7867496b59fSTrond Myklebust 		ret = -ENOTCONN;
7877496b59fSTrond Myklebust 
7887496b59fSTrond Myklebust 	spin_unlock(&xprt->transport_lock);
7897496b59fSTrond Myklebust 	return ret;
790d48f9ce7SDavid Vrabel }
7917496b59fSTrond Myklebust 
7927496b59fSTrond Myklebust static int xs_sock_nospace(struct rpc_rqst *req)
7937496b59fSTrond Myklebust {
7947496b59fSTrond Myklebust 	struct sock_xprt *transport =
7957496b59fSTrond Myklebust 		container_of(req->rq_xprt, struct sock_xprt, xprt);
7967496b59fSTrond Myklebust 	struct sock *sk = transport->inet;
7977496b59fSTrond Myklebust 	int ret = -EAGAIN;
7987496b59fSTrond Myklebust 
7997496b59fSTrond Myklebust 	lock_sock(sk);
8007496b59fSTrond Myklebust 	if (!sock_writeable(sk))
8017496b59fSTrond Myklebust 		ret = xs_nospace(req, transport);
8027496b59fSTrond Myklebust 	release_sock(sk);
8037496b59fSTrond Myklebust 	return ret;
8047496b59fSTrond Myklebust }
8057496b59fSTrond Myklebust 
806d0afde5fSTrond Myklebust static int xs_stream_nospace(struct rpc_rqst *req, bool vm_wait)
8077496b59fSTrond Myklebust {
8087496b59fSTrond Myklebust 	struct sock_xprt *transport =
8097496b59fSTrond Myklebust 		container_of(req->rq_xprt, struct sock_xprt, xprt);
8107496b59fSTrond Myklebust 	struct sock *sk = transport->inet;
8117496b59fSTrond Myklebust 	int ret = -EAGAIN;
8127496b59fSTrond Myklebust 
813d0afde5fSTrond Myklebust 	if (vm_wait)
814d0afde5fSTrond Myklebust 		return -ENOBUFS;
8157496b59fSTrond Myklebust 	lock_sock(sk);
8167496b59fSTrond Myklebust 	if (!sk_stream_memory_free(sk))
8177496b59fSTrond Myklebust 		ret = xs_nospace(req, transport);
8187496b59fSTrond Myklebust 	release_sock(sk);
8195e3771ceSTrond Myklebust 	return ret;
820a246b010SChuck Lever }
821a246b010SChuck Lever 
82272691a26STrond Myklebust static int xs_stream_prepare_request(struct rpc_rqst *req, struct xdr_buf *buf)
823277e4ab7STrond Myklebust {
82472691a26STrond Myklebust 	return xdr_alloc_bvec(buf, rpc_task_gfp_mask());
825277e4ab7STrond Myklebust }
826277e4ab7STrond Myklebust 
82761677eeeSChuck Lever /*
8284cd34e7cSTrond Myklebust  * Determine if the previous message in the stream was aborted before it
8294cd34e7cSTrond Myklebust  * could complete transmission.
8304cd34e7cSTrond Myklebust  */
8314cd34e7cSTrond Myklebust static bool
8324cd34e7cSTrond Myklebust xs_send_request_was_aborted(struct sock_xprt *transport, struct rpc_rqst *req)
8334cd34e7cSTrond Myklebust {
8344cd34e7cSTrond Myklebust 	return transport->xmit.offset != 0 && req->rq_bytes_sent == 0;
8354cd34e7cSTrond Myklebust }
8364cd34e7cSTrond Myklebust 
8374cd34e7cSTrond Myklebust /*
83806b5fc3aSTrond Myklebust  * Return the stream record marker field for a record of length < 2^31-1
83961677eeeSChuck Lever  */
84006b5fc3aSTrond Myklebust static rpc_fraghdr
84106b5fc3aSTrond Myklebust xs_stream_record_marker(struct xdr_buf *xdr)
84261677eeeSChuck Lever {
84306b5fc3aSTrond Myklebust 	if (!xdr->len)
84406b5fc3aSTrond Myklebust 		return 0;
84506b5fc3aSTrond Myklebust 	return cpu_to_be32(RPC_LAST_STREAM_FRAGMENT | (u32)xdr->len);
84661677eeeSChuck Lever }
84761677eeeSChuck Lever 
8489903cd1cSChuck Lever /**
849176e21eeSChuck Lever  * xs_local_send_request - write an RPC request to an AF_LOCAL socket
85050f484e2STrond Myklebust  * @req: pointer to RPC request
851176e21eeSChuck Lever  *
852176e21eeSChuck Lever  * Return values:
853176e21eeSChuck Lever  *        0:	The request has been sent
854176e21eeSChuck Lever  *   EAGAIN:	The socket was blocked, please call again later to
855176e21eeSChuck Lever  *		complete the request
856176e21eeSChuck Lever  * ENOTCONN:	Caller needs to invoke connect logic then call again
85712b20ce3SBhaskar Chowdhury  *    other:	Some other error occurred, the request was not sent
858176e21eeSChuck Lever  */
859adfa7144STrond Myklebust static int xs_local_send_request(struct rpc_rqst *req)
860176e21eeSChuck Lever {
861176e21eeSChuck Lever 	struct rpc_xprt *xprt = req->rq_xprt;
862176e21eeSChuck Lever 	struct sock_xprt *transport =
863176e21eeSChuck Lever 				container_of(xprt, struct sock_xprt, xprt);
864176e21eeSChuck Lever 	struct xdr_buf *xdr = &req->rq_snd_buf;
8657e3d3620STrond Myklebust 	rpc_fraghdr rm = xs_stream_record_marker(xdr);
8667e3d3620STrond Myklebust 	unsigned int msglen = rm ? req->rq_slen + sizeof(rm) : req->rq_slen;
8679e55eef4SChuck Lever 	struct msghdr msg = {
8689e55eef4SChuck Lever 		.msg_flags	= XS_SENDMSG_FLAGS,
8699e55eef4SChuck Lever 	};
870d0afde5fSTrond Myklebust 	bool vm_wait;
8713f649ab7SKees Cook 	unsigned int sent;
872176e21eeSChuck Lever 	int status;
873176e21eeSChuck Lever 
8744cd34e7cSTrond Myklebust 	/* Close the stream if the previous transmission was incomplete */
8754cd34e7cSTrond Myklebust 	if (xs_send_request_was_aborted(transport, req)) {
876f0043206STrond Myklebust 		xprt_force_disconnect(xprt);
8774cd34e7cSTrond Myklebust 		return -ENOTCONN;
8784cd34e7cSTrond Myklebust 	}
8794cd34e7cSTrond Myklebust 
880176e21eeSChuck Lever 	xs_pktdump("packet data:",
881176e21eeSChuck Lever 			req->rq_svec->iov_base, req->rq_svec->iov_len);
882176e21eeSChuck Lever 
883d0afde5fSTrond Myklebust 	vm_wait = sk_stream_is_writeable(transport->inet) ? true : false;
884d0afde5fSTrond Myklebust 
88578215759SChuck Lever 	req->rq_xtime = ktime_get();
8869e55eef4SChuck Lever 	status = xprt_sock_sendmsg(transport->sock, &msg, xdr,
8877e3d3620STrond Myklebust 				   transport->xmit.offset, rm, &sent);
888176e21eeSChuck Lever 	dprintk("RPC:       %s(%u) = %d\n",
8896c7a64e5STrond Myklebust 			__func__, xdr->len - transport->xmit.offset, status);
890743c69e7SNeilBrown 
891f279cd00SJason Baron 	if (likely(sent > 0) || status == 0) {
8926c7a64e5STrond Myklebust 		transport->xmit.offset += sent;
8936c7a64e5STrond Myklebust 		req->rq_bytes_sent = transport->xmit.offset;
8947e3d3620STrond Myklebust 		if (likely(req->rq_bytes_sent >= msglen)) {
8956c7a64e5STrond Myklebust 			req->rq_xmit_bytes_sent += transport->xmit.offset;
8966c7a64e5STrond Myklebust 			transport->xmit.offset = 0;
897176e21eeSChuck Lever 			return 0;
898176e21eeSChuck Lever 		}
899176e21eeSChuck Lever 		status = -EAGAIN;
900d0afde5fSTrond Myklebust 		vm_wait = false;
901176e21eeSChuck Lever 	}
902176e21eeSChuck Lever 
903176e21eeSChuck Lever 	switch (status) {
904176e21eeSChuck Lever 	case -EAGAIN:
905d0afde5fSTrond Myklebust 		status = xs_stream_nospace(req, vm_wait);
906176e21eeSChuck Lever 		break;
907176e21eeSChuck Lever 	default:
908176e21eeSChuck Lever 		dprintk("RPC:       sendmsg returned unrecognized error %d\n",
909176e21eeSChuck Lever 			-status);
910df561f66SGustavo A. R. Silva 		fallthrough;
911176e21eeSChuck Lever 	case -EPIPE:
912f0043206STrond Myklebust 		xprt_force_disconnect(xprt);
913176e21eeSChuck Lever 		status = -ENOTCONN;
914176e21eeSChuck Lever 	}
915176e21eeSChuck Lever 
916176e21eeSChuck Lever 	return status;
917176e21eeSChuck Lever }
918176e21eeSChuck Lever 
919176e21eeSChuck Lever /**
920262965f5SChuck Lever  * xs_udp_send_request - write an RPC request to a UDP socket
92150f484e2STrond Myklebust  * @req: pointer to RPC request
9229903cd1cSChuck Lever  *
9239903cd1cSChuck Lever  * Return values:
9249903cd1cSChuck Lever  *        0:	The request has been sent
9259903cd1cSChuck Lever  *   EAGAIN:	The socket was blocked, please call again later to
9269903cd1cSChuck Lever  *		complete the request
927262965f5SChuck Lever  * ENOTCONN:	Caller needs to invoke connect logic then call again
92825985edcSLucas De Marchi  *    other:	Some other error occurred, the request was not sent
9299903cd1cSChuck Lever  */
930adfa7144STrond Myklebust static int xs_udp_send_request(struct rpc_rqst *req)
931a246b010SChuck Lever {
932a246b010SChuck Lever 	struct rpc_xprt *xprt = req->rq_xprt;
933ee0ac0c2SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
934262965f5SChuck Lever 	struct xdr_buf *xdr = &req->rq_snd_buf;
9359e55eef4SChuck Lever 	struct msghdr msg = {
9369e55eef4SChuck Lever 		.msg_name	= xs_addr(xprt),
9379e55eef4SChuck Lever 		.msg_namelen	= xprt->addrlen,
9389e55eef4SChuck Lever 		.msg_flags	= XS_SENDMSG_FLAGS,
9399e55eef4SChuck Lever 	};
9403f649ab7SKees Cook 	unsigned int sent;
941262965f5SChuck Lever 	int status;
942262965f5SChuck Lever 
943262965f5SChuck Lever 	xs_pktdump("packet data:",
944262965f5SChuck Lever 				req->rq_svec->iov_base,
945262965f5SChuck Lever 				req->rq_svec->iov_len);
946262965f5SChuck Lever 
94701d37c42STrond Myklebust 	if (!xprt_bound(xprt))
94801d37c42STrond Myklebust 		return -ENOTCONN;
94975891f50STrond Myklebust 
95075891f50STrond Myklebust 	if (!xprt_request_get_cong(xprt, req))
95175891f50STrond Myklebust 		return -EBADSLT;
95275891f50STrond Myklebust 
953ff053dbbSTrond Myklebust 	status = xdr_alloc_bvec(xdr, rpc_task_gfp_mask());
954ff053dbbSTrond Myklebust 	if (status < 0)
955ff053dbbSTrond Myklebust 		return status;
95678215759SChuck Lever 	req->rq_xtime = ktime_get();
9579e55eef4SChuck Lever 	status = xprt_sock_sendmsg(transport->sock, &msg, xdr, 0, 0, &sent);
958262965f5SChuck Lever 
959262965f5SChuck Lever 	dprintk("RPC:       xs_udp_send_request(%u) = %d\n",
9606c7a64e5STrond Myklebust 			xdr->len, status);
961262965f5SChuck Lever 
9623dedbb5cSJason Baron 	/* firewall is blocking us, don't return -EAGAIN or we end up looping */
9633dedbb5cSJason Baron 	if (status == -EPERM)
9643dedbb5cSJason Baron 		goto process_status;
9653dedbb5cSJason Baron 
966743c69e7SNeilBrown 	if (status == -EAGAIN && sock_writeable(transport->inet))
967743c69e7SNeilBrown 		status = -ENOBUFS;
968743c69e7SNeilBrown 
969f279cd00SJason Baron 	if (sent > 0 || status == 0) {
970f279cd00SJason Baron 		req->rq_xmit_bytes_sent += sent;
971f279cd00SJason Baron 		if (sent >= req->rq_slen)
972262965f5SChuck Lever 			return 0;
973262965f5SChuck Lever 		/* Still some bytes left; set up for a retry later. */
974262965f5SChuck Lever 		status = -EAGAIN;
9752199700fSTrond Myklebust 	}
976262965f5SChuck Lever 
9773dedbb5cSJason Baron process_status:
978262965f5SChuck Lever 	switch (status) {
979fba91afbSTrond Myklebust 	case -ENOTSOCK:
980fba91afbSTrond Myklebust 		status = -ENOTCONN;
981fba91afbSTrond Myklebust 		/* Should we call xs_close() here? */
982fba91afbSTrond Myklebust 		break;
983b6ddf64fSTrond Myklebust 	case -EAGAIN:
9847496b59fSTrond Myklebust 		status = xs_sock_nospace(req);
985b6ddf64fSTrond Myklebust 		break;
986262965f5SChuck Lever 	case -ENETUNREACH:
9873601c4a9STrond Myklebust 	case -ENOBUFS:
988262965f5SChuck Lever 	case -EPIPE:
989262965f5SChuck Lever 	case -ECONNREFUSED:
9903dedbb5cSJason Baron 	case -EPERM:
991262965f5SChuck Lever 		/* When the server has died, an ICMP port unreachable message
992262965f5SChuck Lever 		 * prompts ECONNREFUSED. */
99313331a55STrond Myklebust 		break;
99413331a55STrond Myklebust 	default:
99513331a55STrond Myklebust 		dprintk("RPC:       sendmsg returned unrecognized error %d\n",
99613331a55STrond Myklebust 			-status);
997262965f5SChuck Lever 	}
9985fe46e9dSBian Naimeng 
999262965f5SChuck Lever 	return status;
1000262965f5SChuck Lever }
1001262965f5SChuck Lever 
1002e06799f9STrond Myklebust /**
1003262965f5SChuck Lever  * xs_tcp_send_request - write an RPC request to a TCP socket
100450f484e2STrond Myklebust  * @req: pointer to RPC request
1005262965f5SChuck Lever  *
1006262965f5SChuck Lever  * Return values:
1007262965f5SChuck Lever  *        0:	The request has been sent
1008262965f5SChuck Lever  *   EAGAIN:	The socket was blocked, please call again later to
1009262965f5SChuck Lever  *		complete the request
1010262965f5SChuck Lever  * ENOTCONN:	Caller needs to invoke connect logic then call again
101125985edcSLucas De Marchi  *    other:	Some other error occurred, the request was not sent
1012262965f5SChuck Lever  *
1013262965f5SChuck Lever  * XXX: In the case of soft timeouts, should we eventually give up
1014262965f5SChuck Lever  *	if sendmsg is not able to make progress?
1015262965f5SChuck Lever  */
1016adfa7144STrond Myklebust static int xs_tcp_send_request(struct rpc_rqst *req)
1017262965f5SChuck Lever {
1018262965f5SChuck Lever 	struct rpc_xprt *xprt = req->rq_xprt;
1019ee0ac0c2SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1020262965f5SChuck Lever 	struct xdr_buf *xdr = &req->rq_snd_buf;
10217e3d3620STrond Myklebust 	rpc_fraghdr rm = xs_stream_record_marker(xdr);
10227e3d3620STrond Myklebust 	unsigned int msglen = rm ? req->rq_slen + sizeof(rm) : req->rq_slen;
10239e55eef4SChuck Lever 	struct msghdr msg = {
10249e55eef4SChuck Lever 		.msg_flags	= XS_SENDMSG_FLAGS,
10259e55eef4SChuck Lever 	};
1026d0afde5fSTrond Myklebust 	bool vm_wait;
10273f649ab7SKees Cook 	unsigned int sent;
1028b595bb15SChuck Lever 	int status;
1029a246b010SChuck Lever 
10304cd34e7cSTrond Myklebust 	/* Close the stream if the previous transmission was incomplete */
10314cd34e7cSTrond Myklebust 	if (xs_send_request_was_aborted(transport, req)) {
10324cd34e7cSTrond Myklebust 		if (transport->sock != NULL)
10334cd34e7cSTrond Myklebust 			kernel_sock_shutdown(transport->sock, SHUT_RDWR);
10344cd34e7cSTrond Myklebust 		return -ENOTCONN;
10354cd34e7cSTrond Myklebust 	}
1036d275880aSTrond Myklebust 	if (!transport->inet)
1037d275880aSTrond Myklebust 		return -ENOTCONN;
10384cd34e7cSTrond Myklebust 
1039262965f5SChuck Lever 	xs_pktdump("packet data:",
1040262965f5SChuck Lever 				req->rq_svec->iov_base,
1041262965f5SChuck Lever 				req->rq_svec->iov_len);
1042a246b010SChuck Lever 
10437196dbb0STrond Myklebust 	if (test_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state))
10447196dbb0STrond Myklebust 		xs_tcp_set_socket_timeouts(xprt, transport->sock);
10457196dbb0STrond Myklebust 
10463b21f757STrond Myklebust 	xs_set_srcport(transport, transport->sock);
10473b21f757STrond Myklebust 
1048a246b010SChuck Lever 	/* Continue transmitting the packet/record. We must be careful
1049a246b010SChuck Lever 	 * to cope with writespace callbacks arriving _after_ we have
1050262965f5SChuck Lever 	 * called sendmsg(). */
105178215759SChuck Lever 	req->rq_xtime = ktime_get();
1052d737e5d4STrond Myklebust 	tcp_sock_set_cork(transport->inet, true);
1053d0afde5fSTrond Myklebust 
1054d0afde5fSTrond Myklebust 	vm_wait = sk_stream_is_writeable(transport->inet) ? true : false;
1055d0afde5fSTrond Myklebust 
1056d0afde5fSTrond Myklebust 	do {
10579e55eef4SChuck Lever 		status = xprt_sock_sendmsg(transport->sock, &msg, xdr,
10587e3d3620STrond Myklebust 					   transport->xmit.offset, rm, &sent);
1059a246b010SChuck Lever 
1060262965f5SChuck Lever 		dprintk("RPC:       xs_tcp_send_request(%u) = %d\n",
10616c7a64e5STrond Myklebust 				xdr->len - transport->xmit.offset, status);
1062262965f5SChuck Lever 
1063a246b010SChuck Lever 		/* If we've sent the entire packet, immediately
1064a246b010SChuck Lever 		 * reset the count of bytes sent. */
10656c7a64e5STrond Myklebust 		transport->xmit.offset += sent;
10666c7a64e5STrond Myklebust 		req->rq_bytes_sent = transport->xmit.offset;
10677e3d3620STrond Myklebust 		if (likely(req->rq_bytes_sent >= msglen)) {
10686c7a64e5STrond Myklebust 			req->rq_xmit_bytes_sent += transport->xmit.offset;
10696c7a64e5STrond Myklebust 			transport->xmit.offset = 0;
1070d737e5d4STrond Myklebust 			if (atomic_long_read(&xprt->xmit_queuelen) == 1)
1071d737e5d4STrond Myklebust 				tcp_sock_set_cork(transport->inet, false);
1072a246b010SChuck Lever 			return 0;
1073a246b010SChuck Lever 		}
1074262965f5SChuck Lever 
10759ffadfbcSTrond Myklebust 		WARN_ON_ONCE(sent == 0 && status == 0);
10769ffadfbcSTrond Myklebust 
1077d0afde5fSTrond Myklebust 		if (sent > 0)
10789ffadfbcSTrond Myklebust 			vm_wait = false;
1079d0afde5fSTrond Myklebust 
1080d0afde5fSTrond Myklebust 	} while (status == 0);
1081a246b010SChuck Lever 
1082262965f5SChuck Lever 	switch (status) {
1083fba91afbSTrond Myklebust 	case -ENOTSOCK:
1084fba91afbSTrond Myklebust 		status = -ENOTCONN;
1085fba91afbSTrond Myklebust 		/* Should we call xs_close() here? */
1086fba91afbSTrond Myklebust 		break;
1087262965f5SChuck Lever 	case -EAGAIN:
1088d0afde5fSTrond Myklebust 		status = xs_stream_nospace(req, vm_wait);
1089262965f5SChuck Lever 		break;
1090262965f5SChuck Lever 	case -ECONNRESET:
1091262965f5SChuck Lever 	case -ECONNREFUSED:
1092262965f5SChuck Lever 	case -ENOTCONN:
10933913c78cSTrond Myklebust 	case -EADDRINUSE:
1094b5872f0cSTrond Myklebust 	case -ENOBUFS:
1095b9d2bb2eSTrond Myklebust 	case -EPIPE:
109613331a55STrond Myklebust 		break;
109713331a55STrond Myklebust 	default:
109813331a55STrond Myklebust 		dprintk("RPC:       sendmsg returned unrecognized error %d\n",
109913331a55STrond Myklebust 			-status);
1100a246b010SChuck Lever 	}
11015fe46e9dSBian Naimeng 
1102a246b010SChuck Lever 	return status;
1103a246b010SChuck Lever }
1104a246b010SChuck Lever 
11052a9e1cfaSTrond Myklebust static void xs_save_old_callbacks(struct sock_xprt *transport, struct sock *sk)
11062a9e1cfaSTrond Myklebust {
11072a9e1cfaSTrond Myklebust 	transport->old_data_ready = sk->sk_data_ready;
11082a9e1cfaSTrond Myklebust 	transport->old_state_change = sk->sk_state_change;
11092a9e1cfaSTrond Myklebust 	transport->old_write_space = sk->sk_write_space;
11102118071dSTrond Myklebust 	transport->old_error_report = sk->sk_error_report;
11112a9e1cfaSTrond Myklebust }
11122a9e1cfaSTrond Myklebust 
11132a9e1cfaSTrond Myklebust static void xs_restore_old_callbacks(struct sock_xprt *transport, struct sock *sk)
11142a9e1cfaSTrond Myklebust {
11152a9e1cfaSTrond Myklebust 	sk->sk_data_ready = transport->old_data_ready;
11162a9e1cfaSTrond Myklebust 	sk->sk_state_change = transport->old_state_change;
11172a9e1cfaSTrond Myklebust 	sk->sk_write_space = transport->old_write_space;
11182118071dSTrond Myklebust 	sk->sk_error_report = transport->old_error_report;
11192118071dSTrond Myklebust }
11202118071dSTrond Myklebust 
112142d42a5bSTrond Myklebust static void xs_sock_reset_state_flags(struct rpc_xprt *xprt)
112242d42a5bSTrond Myklebust {
112342d42a5bSTrond Myklebust 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
112442d42a5bSTrond Myklebust 
112542d42a5bSTrond Myklebust 	clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state);
11264f8943f8STrond Myklebust 	clear_bit(XPRT_SOCK_WAKE_ERROR, &transport->sock_state);
11274f8943f8STrond Myklebust 	clear_bit(XPRT_SOCK_WAKE_WRITE, &transport->sock_state);
11284f8943f8STrond Myklebust 	clear_bit(XPRT_SOCK_WAKE_DISCONNECT, &transport->sock_state);
11292790a624STrond Myklebust 	clear_bit(XPRT_SOCK_NOSPACE, &transport->sock_state);
11304f8943f8STrond Myklebust }
11314f8943f8STrond Myklebust 
11324f8943f8STrond Myklebust static void xs_run_error_worker(struct sock_xprt *transport, unsigned int nr)
11334f8943f8STrond Myklebust {
11344f8943f8STrond Myklebust 	set_bit(nr, &transport->sock_state);
11354f8943f8STrond Myklebust 	queue_work(xprtiod_workqueue, &transport->error_worker);
113642d42a5bSTrond Myklebust }
113742d42a5bSTrond Myklebust 
1138b70ae915STrond Myklebust static void xs_sock_reset_connection_flags(struct rpc_xprt *xprt)
1139b70ae915STrond Myklebust {
1140d896ba83STrond Myklebust 	xprt->connect_cookie++;
1141b70ae915STrond Myklebust 	smp_mb__before_atomic();
1142b70ae915STrond Myklebust 	clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
1143b70ae915STrond Myklebust 	clear_bit(XPRT_CLOSING, &xprt->state);
114442d42a5bSTrond Myklebust 	xs_sock_reset_state_flags(xprt);
1145b70ae915STrond Myklebust 	smp_mb__after_atomic();
1146b70ae915STrond Myklebust }
1147b70ae915STrond Myklebust 
11482118071dSTrond Myklebust /**
11492118071dSTrond Myklebust  * xs_error_report - callback to handle TCP socket state errors
11502118071dSTrond Myklebust  * @sk: socket
11512118071dSTrond Myklebust  *
11522118071dSTrond Myklebust  * Note: we don't call sock_error() since there may be a rpc_task
11532118071dSTrond Myklebust  * using the socket, and so we don't want to clear sk->sk_err.
11542118071dSTrond Myklebust  */
11552118071dSTrond Myklebust static void xs_error_report(struct sock *sk)
11562118071dSTrond Myklebust {
11574f8943f8STrond Myklebust 	struct sock_xprt *transport;
11582118071dSTrond Myklebust 	struct rpc_xprt *xprt;
11592118071dSTrond Myklebust 
11602118071dSTrond Myklebust 	if (!(xprt = xprt_from_sock(sk)))
1161ea9afca8STrond Myklebust 		return;
11622118071dSTrond Myklebust 
11634f8943f8STrond Myklebust 	transport = container_of(xprt, struct sock_xprt, xprt);
1164af84537dSBenjamin Coddington 	transport->xprt_err = -sk->sk_err;
1165af84537dSBenjamin Coddington 	if (transport->xprt_err == 0)
1166ea9afca8STrond Myklebust 		return;
11672118071dSTrond Myklebust 	dprintk("RPC:       xs_error_report client %p, error=%d...\n",
1168af84537dSBenjamin Coddington 			xprt, -transport->xprt_err);
1169af84537dSBenjamin Coddington 	trace_rpc_socket_error(xprt, sk->sk_socket, transport->xprt_err);
1170af84537dSBenjamin Coddington 
1171af84537dSBenjamin Coddington 	/* barrier ensures xprt_err is set before XPRT_SOCK_WAKE_ERROR */
1172af84537dSBenjamin Coddington 	smp_mb__before_atomic();
11734f8943f8STrond Myklebust 	xs_run_error_worker(transport, XPRT_SOCK_WAKE_ERROR);
11742a9e1cfaSTrond Myklebust }
11752a9e1cfaSTrond Myklebust 
1176fe315e76SChuck Lever static void xs_reset_transport(struct sock_xprt *transport)
1177a246b010SChuck Lever {
1178ee0ac0c2SChuck Lever 	struct socket *sock = transport->sock;
1179ee0ac0c2SChuck Lever 	struct sock *sk = transport->inet;
11806cc7e908STrond Myklebust 	struct rpc_xprt *xprt = &transport->xprt;
1181a73881c9STrond Myklebust 	struct file *filp = transport->file;
1182a246b010SChuck Lever 
1183fe315e76SChuck Lever 	if (sk == NULL)
1184fe315e76SChuck Lever 		return;
1185f0043206STrond Myklebust 	/*
1186f0043206STrond Myklebust 	 * Make sure we're calling this in a context from which it is safe
1187f0043206STrond Myklebust 	 * to call __fput_sync(). In practice that means rpciod and the
1188f0043206STrond Myklebust 	 * system workqueue.
1189f0043206STrond Myklebust 	 */
1190f0043206STrond Myklebust 	if (!(current->flags & PF_WQ_WORKER)) {
1191f0043206STrond Myklebust 		WARN_ON_ONCE(1);
1192f0043206STrond Myklebust 		set_bit(XPRT_CLOSE_WAIT, &xprt->state);
1193f0043206STrond Myklebust 		return;
1194f0043206STrond Myklebust 	}
11959903cd1cSChuck Lever 
1196264d1df3SJeff Layton 	if (atomic_read(&transport->xprt.swapper))
1197264d1df3SJeff Layton 		sk_clear_memalloc(sk);
1198264d1df3SJeff Layton 
119909939204STrond Myklebust 	kernel_sock_shutdown(sock, SHUT_RDWR);
120009939204STrond Myklebust 
1201edc1b01cSTrond Myklebust 	mutex_lock(&transport->recv_mutex);
1202ea9afca8STrond Myklebust 	lock_sock(sk);
1203ee0ac0c2SChuck Lever 	transport->inet = NULL;
1204ee0ac0c2SChuck Lever 	transport->sock = NULL;
1205a73881c9STrond Myklebust 	transport->file = NULL;
1206a246b010SChuck Lever 
1207a246b010SChuck Lever 	sk->sk_user_data = NULL;
12082a9e1cfaSTrond Myklebust 
12092a9e1cfaSTrond Myklebust 	xs_restore_old_callbacks(transport, sk);
12100c78789eSTrond Myklebust 	xprt_clear_connected(xprt);
12116cc7e908STrond Myklebust 	xs_sock_reset_connection_flags(xprt);
1212ae053551STrond Myklebust 	/* Reset stream record info */
1213ae053551STrond Myklebust 	xs_stream_reset_connect(transport);
1214ea9afca8STrond Myklebust 	release_sock(sk);
1215edc1b01cSTrond Myklebust 	mutex_unlock(&transport->recv_mutex);
1216a246b010SChuck Lever 
12176cc7e908STrond Myklebust 	trace_rpc_socket_close(xprt, sock);
1218f0043206STrond Myklebust 	__fput_sync(filp);
12190445f92cSTrond Myklebust 
12200445f92cSTrond Myklebust 	xprt_disconnect_done(xprt);
1221fe315e76SChuck Lever }
1222fe315e76SChuck Lever 
1223fe315e76SChuck Lever /**
1224fe315e76SChuck Lever  * xs_close - close a socket
1225fe315e76SChuck Lever  * @xprt: transport
1226fe315e76SChuck Lever  *
1227fe315e76SChuck Lever  * This is used when all requests are complete; ie, no DRC state remains
1228fe315e76SChuck Lever  * on the server we want to save.
1229f75e6745STrond Myklebust  *
1230f75e6745STrond Myklebust  * The caller _must_ be holding XPRT_LOCKED in order to avoid issues with
1231f75e6745STrond Myklebust  * xs_reset_transport() zeroing the socket from underneath a writer.
1232fe315e76SChuck Lever  */
1233fe315e76SChuck Lever static void xs_close(struct rpc_xprt *xprt)
1234fe315e76SChuck Lever {
1235fe315e76SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1236fe315e76SChuck Lever 
1237fe315e76SChuck Lever 	dprintk("RPC:       xs_close xprt %p\n", xprt);
1238fe315e76SChuck Lever 
1239fe315e76SChuck Lever 	xs_reset_transport(transport);
124061d0a8e6SNeil Brown 	xprt->reestablish_timeout = 0;
1241a246b010SChuck Lever }
1242a246b010SChuck Lever 
12434a068258SChuck Lever static void xs_inject_disconnect(struct rpc_xprt *xprt)
12444a068258SChuck Lever {
12454a068258SChuck Lever 	dprintk("RPC:       injecting transport disconnect on xprt=%p\n",
12464a068258SChuck Lever 		xprt);
12474a068258SChuck Lever 	xprt_disconnect_done(xprt);
12484a068258SChuck Lever }
12494a068258SChuck Lever 
1250315f3812SKinglong Mee static void xs_xprt_free(struct rpc_xprt *xprt)
1251315f3812SKinglong Mee {
1252315f3812SKinglong Mee 	xs_free_peer_addresses(xprt);
1253315f3812SKinglong Mee 	xprt_free(xprt);
1254315f3812SKinglong Mee }
1255315f3812SKinglong Mee 
12569903cd1cSChuck Lever /**
12579903cd1cSChuck Lever  * xs_destroy - prepare to shutdown a transport
12589903cd1cSChuck Lever  * @xprt: doomed transport
12599903cd1cSChuck Lever  *
12609903cd1cSChuck Lever  */
12619903cd1cSChuck Lever static void xs_destroy(struct rpc_xprt *xprt)
1262a246b010SChuck Lever {
126303c78827STrond Myklebust 	struct sock_xprt *transport = container_of(xprt,
126403c78827STrond Myklebust 			struct sock_xprt, xprt);
12659903cd1cSChuck Lever 	dprintk("RPC:       xs_destroy xprt %p\n", xprt);
12669903cd1cSChuck Lever 
126703c78827STrond Myklebust 	cancel_delayed_work_sync(&transport->connect_worker);
1268a1311d87STrond Myklebust 	xs_close(xprt);
1269edc1b01cSTrond Myklebust 	cancel_work_sync(&transport->recv_worker);
1270b5e92419STrond Myklebust 	cancel_work_sync(&transport->error_worker);
1271315f3812SKinglong Mee 	xs_xprt_free(xprt);
1272a1311d87STrond Myklebust 	module_put(THIS_MODULE);
1273a246b010SChuck Lever }
1274a246b010SChuck Lever 
12759903cd1cSChuck Lever /**
1276f9b2ee71STrond Myklebust  * xs_udp_data_read_skb - receive callback for UDP sockets
1277f9b2ee71STrond Myklebust  * @xprt: transport
1278f9b2ee71STrond Myklebust  * @sk: socket
1279f9b2ee71STrond Myklebust  * @skb: skbuff
12809903cd1cSChuck Lever  *
1281a246b010SChuck Lever  */
1282f9b2ee71STrond Myklebust static void xs_udp_data_read_skb(struct rpc_xprt *xprt,
1283f9b2ee71STrond Myklebust 		struct sock *sk,
1284f9b2ee71STrond Myklebust 		struct sk_buff *skb)
1285a246b010SChuck Lever {
1286a246b010SChuck Lever 	struct rpc_task *task;
1287a246b010SChuck Lever 	struct rpc_rqst *rovr;
1288f9b2ee71STrond Myklebust 	int repsize, copied;
1289d8ed029dSAlexey Dobriyan 	u32 _xid;
1290d8ed029dSAlexey Dobriyan 	__be32 *xp;
1291a246b010SChuck Lever 
12921da8c681SWillem de Bruijn 	repsize = skb->len;
1293a246b010SChuck Lever 	if (repsize < 4) {
12949903cd1cSChuck Lever 		dprintk("RPC:       impossible RPC reply size %d!\n", repsize);
1295f9b2ee71STrond Myklebust 		return;
1296a246b010SChuck Lever 	}
1297a246b010SChuck Lever 
1298a246b010SChuck Lever 	/* Copy the XID from the skb... */
12991da8c681SWillem de Bruijn 	xp = skb_header_pointer(skb, 0, sizeof(_xid), &_xid);
1300a246b010SChuck Lever 	if (xp == NULL)
1301f9b2ee71STrond Myklebust 		return;
1302a246b010SChuck Lever 
1303a246b010SChuck Lever 	/* Look up and lock the request corresponding to the given XID */
130475c84151STrond Myklebust 	spin_lock(&xprt->queue_lock);
1305a246b010SChuck Lever 	rovr = xprt_lookup_rqst(xprt, *xp);
1306a246b010SChuck Lever 	if (!rovr)
1307a246b010SChuck Lever 		goto out_unlock;
1308729749bbSTrond Myklebust 	xprt_pin_rqst(rovr);
1309ecd465eeSChuck Lever 	xprt_update_rtt(rovr->rq_task);
131075c84151STrond Myklebust 	spin_unlock(&xprt->queue_lock);
1311a246b010SChuck Lever 	task = rovr->rq_task;
1312a246b010SChuck Lever 
1313a246b010SChuck Lever 	if ((copied = rovr->rq_private_buf.buflen) > repsize)
1314a246b010SChuck Lever 		copied = repsize;
1315a246b010SChuck Lever 
1316a246b010SChuck Lever 	/* Suck it into the iovec, verify checksum if not done by hw. */
13171781f7f5SHerbert Xu 	if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) {
131875c84151STrond Myklebust 		spin_lock(&xprt->queue_lock);
13190afa6b44STrond Myklebust 		__UDPX_INC_STATS(sk, UDP_MIB_INERRORS);
1320729749bbSTrond Myklebust 		goto out_unpin;
13211781f7f5SHerbert Xu 	}
13221781f7f5SHerbert Xu 
1323a246b010SChuck Lever 
1324b5e92419STrond Myklebust 	spin_lock(&xprt->transport_lock);
13256a24dfb6STrond Myklebust 	xprt_adjust_cwnd(xprt, task, copied);
1326b5e92419STrond Myklebust 	spin_unlock(&xprt->transport_lock);
132775c84151STrond Myklebust 	spin_lock(&xprt->queue_lock);
13281570c1e4SChuck Lever 	xprt_complete_rqst(task, copied);
13290afa6b44STrond Myklebust 	__UDPX_INC_STATS(sk, UDP_MIB_INDATAGRAMS);
1330729749bbSTrond Myklebust out_unpin:
1331729749bbSTrond Myklebust 	xprt_unpin_rqst(rovr);
1332a246b010SChuck Lever  out_unlock:
133375c84151STrond Myklebust 	spin_unlock(&xprt->queue_lock);
1334f9b2ee71STrond Myklebust }
1335f9b2ee71STrond Myklebust 
1336f9b2ee71STrond Myklebust static void xs_udp_data_receive(struct sock_xprt *transport)
1337f9b2ee71STrond Myklebust {
1338f9b2ee71STrond Myklebust 	struct sk_buff *skb;
1339f9b2ee71STrond Myklebust 	struct sock *sk;
1340f9b2ee71STrond Myklebust 	int err;
1341f9b2ee71STrond Myklebust 
1342f9b2ee71STrond Myklebust 	mutex_lock(&transport->recv_mutex);
1343f9b2ee71STrond Myklebust 	sk = transport->inet;
1344f9b2ee71STrond Myklebust 	if (sk == NULL)
1345f9b2ee71STrond Myklebust 		goto out;
1346f9b2ee71STrond Myklebust 	for (;;) {
1347ec095263SOliver Hartkopp 		skb = skb_recv_udp(sk, MSG_DONTWAIT, &err);
13484f546149STrond Myklebust 		if (skb == NULL)
13494f546149STrond Myklebust 			break;
1350f9b2ee71STrond Myklebust 		xs_udp_data_read_skb(&transport->xprt, sk, skb);
1351850cbaddSPaolo Abeni 		consume_skb(skb);
13520af3442aSTrond Myklebust 		cond_resched();
1353f9b2ee71STrond Myklebust 	}
13540ffe86f4STrond Myklebust 	xs_poll_check_readable(transport);
1355a246b010SChuck Lever out:
1356f9b2ee71STrond Myklebust 	mutex_unlock(&transport->recv_mutex);
1357f9b2ee71STrond Myklebust }
1358f9b2ee71STrond Myklebust 
1359f9b2ee71STrond Myklebust static void xs_udp_data_receive_workfn(struct work_struct *work)
1360f9b2ee71STrond Myklebust {
1361f9b2ee71STrond Myklebust 	struct sock_xprt *transport =
1362f9b2ee71STrond Myklebust 		container_of(work, struct sock_xprt, recv_worker);
1363a1231fdaSTrond Myklebust 	unsigned int pflags = memalloc_nofs_save();
1364a1231fdaSTrond Myklebust 
1365f9b2ee71STrond Myklebust 	xs_udp_data_receive(transport);
1366a1231fdaSTrond Myklebust 	memalloc_nofs_restore(pflags);
1367f9b2ee71STrond Myklebust }
1368f9b2ee71STrond Myklebust 
1369f9b2ee71STrond Myklebust /**
1370f67939e4SChuck Lever  * xs_data_ready - "data ready" callback for sockets
1371f9b2ee71STrond Myklebust  * @sk: socket with data to read
1372f9b2ee71STrond Myklebust  *
1373f9b2ee71STrond Myklebust  */
1374f9b2ee71STrond Myklebust static void xs_data_ready(struct sock *sk)
1375f9b2ee71STrond Myklebust {
1376f9b2ee71STrond Myklebust 	struct rpc_xprt *xprt;
1377f9b2ee71STrond Myklebust 
137840e0b090SPeilin Ye 	trace_sk_data_ready(sk);
137940e0b090SPeilin Ye 
1380f9b2ee71STrond Myklebust 	xprt = xprt_from_sock(sk);
1381f9b2ee71STrond Myklebust 	if (xprt != NULL) {
1382f9b2ee71STrond Myklebust 		struct sock_xprt *transport = container_of(xprt,
1383f9b2ee71STrond Myklebust 				struct sock_xprt, xprt);
1384f67939e4SChuck Lever 
1385f67939e4SChuck Lever 		trace_xs_data_ready(xprt);
1386f67939e4SChuck Lever 
13875157b956STrond Myklebust 		transport->old_data_ready(sk);
13885157b956STrond Myklebust 		/* Any data means we had a useful conversation, so
13895157b956STrond Myklebust 		 * then we don't need to delay the next reconnect
13905157b956STrond Myklebust 		 */
13915157b956STrond Myklebust 		if (xprt->reestablish_timeout)
13925157b956STrond Myklebust 			xprt->reestablish_timeout = 0;
139342d42a5bSTrond Myklebust 		if (!test_and_set_bit(XPRT_SOCK_DATA_READY, &transport->sock_state))
139440a5f1b1STrond Myklebust 			queue_work(xprtiod_workqueue, &transport->recv_worker);
1395f9b2ee71STrond Myklebust 	}
1396a246b010SChuck Lever }
1397a246b010SChuck Lever 
1398a519fc7aSTrond Myklebust /*
1399a519fc7aSTrond Myklebust  * Helper function to force a TCP close if the server is sending
1400a519fc7aSTrond Myklebust  * junk and/or it has put us in CLOSE_WAIT
1401a519fc7aSTrond Myklebust  */
1402a519fc7aSTrond Myklebust static void xs_tcp_force_close(struct rpc_xprt *xprt)
1403a519fc7aSTrond Myklebust {
1404a519fc7aSTrond Myklebust 	xprt_force_disconnect(xprt);
1405a519fc7aSTrond Myklebust }
1406a519fc7aSTrond Myklebust 
14079e00abc3STrond Myklebust #if defined(CONFIG_SUNRPC_BACKCHANNEL)
14086b26cc8cSChuck Lever static size_t xs_tcp_bc_maxpayload(struct rpc_xprt *xprt)
14096b26cc8cSChuck Lever {
14106b26cc8cSChuck Lever 	return PAGE_SIZE;
14116b26cc8cSChuck Lever }
14129e00abc3STrond Myklebust #endif /* CONFIG_SUNRPC_BACKCHANNEL */
141344b98efdSRicardo Labiaga 
14149903cd1cSChuck Lever /**
1415efce2d0bSTrond Myklebust  * xs_local_state_change - callback to handle AF_LOCAL socket state changes
1416efce2d0bSTrond Myklebust  * @sk: socket whose state has changed
1417efce2d0bSTrond Myklebust  *
1418efce2d0bSTrond Myklebust  */
1419efce2d0bSTrond Myklebust static void xs_local_state_change(struct sock *sk)
1420efce2d0bSTrond Myklebust {
1421efce2d0bSTrond Myklebust 	struct rpc_xprt *xprt;
1422efce2d0bSTrond Myklebust 	struct sock_xprt *transport;
1423efce2d0bSTrond Myklebust 
1424efce2d0bSTrond Myklebust 	if (!(xprt = xprt_from_sock(sk)))
1425efce2d0bSTrond Myklebust 		return;
1426efce2d0bSTrond Myklebust 	transport = container_of(xprt, struct sock_xprt, xprt);
1427efce2d0bSTrond Myklebust 	if (sk->sk_shutdown & SHUTDOWN_MASK) {
1428efce2d0bSTrond Myklebust 		clear_bit(XPRT_CONNECTED, &xprt->state);
1429efce2d0bSTrond Myklebust 		/* Trigger the socket release */
1430efce2d0bSTrond Myklebust 		xs_run_error_worker(transport, XPRT_SOCK_WAKE_DISCONNECT);
1431efce2d0bSTrond Myklebust 	}
1432efce2d0bSTrond Myklebust }
1433efce2d0bSTrond Myklebust 
1434efce2d0bSTrond Myklebust /**
14359903cd1cSChuck Lever  * xs_tcp_state_change - callback to handle TCP socket state changes
14369903cd1cSChuck Lever  * @sk: socket whose state has changed
14379903cd1cSChuck Lever  *
14389903cd1cSChuck Lever  */
14399903cd1cSChuck Lever static void xs_tcp_state_change(struct sock *sk)
1440a246b010SChuck Lever {
1441a246b010SChuck Lever 	struct rpc_xprt *xprt;
14420fdea1e8STrond Myklebust 	struct sock_xprt *transport;
1443a246b010SChuck Lever 
1444a246b010SChuck Lever 	if (!(xprt = xprt_from_sock(sk)))
1445ea9afca8STrond Myklebust 		return;
14469903cd1cSChuck Lever 	dprintk("RPC:       xs_tcp_state_change client %p...\n", xprt);
1447669502ffSAndy Chittenden 	dprintk("RPC:       state %x conn %d dead %d zapped %d sk_shutdown %d\n",
1448a246b010SChuck Lever 			sk->sk_state, xprt_connected(xprt),
1449a246b010SChuck Lever 			sock_flag(sk, SOCK_DEAD),
1450669502ffSAndy Chittenden 			sock_flag(sk, SOCK_ZAPPED),
1451669502ffSAndy Chittenden 			sk->sk_shutdown);
1452a246b010SChuck Lever 
14530fdea1e8STrond Myklebust 	transport = container_of(xprt, struct sock_xprt, xprt);
145440b5ea0cSTrond Myklebust 	trace_rpc_socket_state_change(xprt, sk->sk_socket);
1455a246b010SChuck Lever 	switch (sk->sk_state) {
1456a246b010SChuck Lever 	case TCP_ESTABLISHED:
1457a246b010SChuck Lever 		if (!xprt_test_and_set_connected(xprt)) {
14588b71798cSTrond Myklebust 			xprt->connect_cookie++;
14590fdea1e8STrond Myklebust 			clear_bit(XPRT_SOCK_CONNECTING, &transport->sock_state);
14600fdea1e8STrond Myklebust 			xprt_clear_connecting(xprt);
146151971139SChuck Lever 
14623968a8a5SChuck Lever 			xprt->stat.connect_count++;
14633968a8a5SChuck Lever 			xprt->stat.connect_time += (long)jiffies -
14643968a8a5SChuck Lever 						   xprt->stat.connect_start;
14654f8943f8STrond Myklebust 			xs_run_error_worker(transport, XPRT_SOCK_WAKE_PENDING);
1466a246b010SChuck Lever 		}
1467a246b010SChuck Lever 		break;
14683b948ae5STrond Myklebust 	case TCP_FIN_WAIT1:
14693b948ae5STrond Myklebust 		/* The client initiated a shutdown of the socket */
14707c1d71cfSTrond Myklebust 		xprt->connect_cookie++;
1471663b8858STrond Myklebust 		xprt->reestablish_timeout = 0;
14723b948ae5STrond Myklebust 		set_bit(XPRT_CLOSING, &xprt->state);
14734e857c58SPeter Zijlstra 		smp_mb__before_atomic();
14743b948ae5STrond Myklebust 		clear_bit(XPRT_CONNECTED, &xprt->state);
1475ef803670STrond Myklebust 		clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
14764e857c58SPeter Zijlstra 		smp_mb__after_atomic();
1477a246b010SChuck Lever 		break;
1478632e3bdcSTrond Myklebust 	case TCP_CLOSE_WAIT:
14793b948ae5STrond Myklebust 		/* The server initiated a shutdown of the socket */
14807c1d71cfSTrond Myklebust 		xprt->connect_cookie++;
1481d0bea455STrond Myklebust 		clear_bit(XPRT_CONNECTED, &xprt->state);
14824f8943f8STrond Myklebust 		xs_run_error_worker(transport, XPRT_SOCK_WAKE_DISCONNECT);
1483df561f66SGustavo A. R. Silva 		fallthrough;
1484663b8858STrond Myklebust 	case TCP_CLOSING:
1485663b8858STrond Myklebust 		/*
1486663b8858STrond Myklebust 		 * If the server closed down the connection, make sure that
1487663b8858STrond Myklebust 		 * we back off before reconnecting
1488663b8858STrond Myklebust 		 */
1489663b8858STrond Myklebust 		if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
1490663b8858STrond Myklebust 			xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
14913b948ae5STrond Myklebust 		break;
14923b948ae5STrond Myklebust 	case TCP_LAST_ACK:
1493670f9457STrond Myklebust 		set_bit(XPRT_CLOSING, &xprt->state);
14944e857c58SPeter Zijlstra 		smp_mb__before_atomic();
14953b948ae5STrond Myklebust 		clear_bit(XPRT_CONNECTED, &xprt->state);
14964e857c58SPeter Zijlstra 		smp_mb__after_atomic();
14973b948ae5STrond Myklebust 		break;
14983b948ae5STrond Myklebust 	case TCP_CLOSE:
14990fdea1e8STrond Myklebust 		if (test_and_clear_bit(XPRT_SOCK_CONNECTING,
15000fdea1e8STrond Myklebust 					&transport->sock_state))
15010fdea1e8STrond Myklebust 			xprt_clear_connecting(xprt);
15029b30889cSTrond Myklebust 		clear_bit(XPRT_CLOSING, &xprt->state);
15039b30889cSTrond Myklebust 		/* Trigger the socket release */
15044f8943f8STrond Myklebust 		xs_run_error_worker(transport, XPRT_SOCK_WAKE_DISCONNECT);
1505a246b010SChuck Lever 	}
1506a246b010SChuck Lever }
1507a246b010SChuck Lever 
15081f0fa154SIlpo Järvinen static void xs_write_space(struct sock *sk)
15091f0fa154SIlpo Järvinen {
15104f8943f8STrond Myklebust 	struct sock_xprt *transport;
15111f0fa154SIlpo Järvinen 	struct rpc_xprt *xprt;
15121f0fa154SIlpo Järvinen 
151313331a55STrond Myklebust 	if (!sk->sk_socket)
15141f0fa154SIlpo Järvinen 		return;
151513331a55STrond Myklebust 	clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
15161f0fa154SIlpo Järvinen 
15171f0fa154SIlpo Järvinen 	if (unlikely(!(xprt = xprt_from_sock(sk))))
15181f0fa154SIlpo Järvinen 		return;
15194f8943f8STrond Myklebust 	transport = container_of(xprt, struct sock_xprt, xprt);
15202790a624STrond Myklebust 	if (!test_and_clear_bit(XPRT_SOCK_NOSPACE, &transport->sock_state))
15212790a624STrond Myklebust 		return;
15224f8943f8STrond Myklebust 	xs_run_error_worker(transport, XPRT_SOCK_WAKE_WRITE);
1523c544577dSTrond Myklebust 	sk->sk_write_pending--;
15241f0fa154SIlpo Järvinen }
15251f0fa154SIlpo Järvinen 
15262a9e1cfaSTrond Myklebust /**
1527c7b2cae8SChuck Lever  * xs_udp_write_space - callback invoked when socket buffer space
1528c7b2cae8SChuck Lever  *                             becomes available
15299903cd1cSChuck Lever  * @sk: socket whose state has changed
15309903cd1cSChuck Lever  *
1531a246b010SChuck Lever  * Called when more output buffer space is available for this socket.
1532a246b010SChuck Lever  * We try not to wake our writers until they can make "significant"
1533c7b2cae8SChuck Lever  * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1534a246b010SChuck Lever  * with a bunch of small requests.
1535a246b010SChuck Lever  */
1536c7b2cae8SChuck Lever static void xs_udp_write_space(struct sock *sk)
1537a246b010SChuck Lever {
1538c7b2cae8SChuck Lever 	/* from net/core/sock.c:sock_def_write_space */
15391f0fa154SIlpo Järvinen 	if (sock_writeable(sk))
15401f0fa154SIlpo Järvinen 		xs_write_space(sk);
1541c7b2cae8SChuck Lever }
1542c7b2cae8SChuck Lever 
1543c7b2cae8SChuck Lever /**
1544c7b2cae8SChuck Lever  * xs_tcp_write_space - callback invoked when socket buffer space
1545c7b2cae8SChuck Lever  *                             becomes available
1546c7b2cae8SChuck Lever  * @sk: socket whose state has changed
1547c7b2cae8SChuck Lever  *
1548c7b2cae8SChuck Lever  * Called when more output buffer space is available for this socket.
1549c7b2cae8SChuck Lever  * We try not to wake our writers until they can make "significant"
1550c7b2cae8SChuck Lever  * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1551c7b2cae8SChuck Lever  * with a bunch of small requests.
1552c7b2cae8SChuck Lever  */
1553c7b2cae8SChuck Lever static void xs_tcp_write_space(struct sock *sk)
1554c7b2cae8SChuck Lever {
1555c7b2cae8SChuck Lever 	/* from net/core/stream.c:sk_stream_write_space */
155664dc6130SEric Dumazet 	if (sk_stream_is_writeable(sk))
15571f0fa154SIlpo Järvinen 		xs_write_space(sk);
1558a246b010SChuck Lever }
1559a246b010SChuck Lever 
1560470056c2SChuck Lever static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt)
1561a246b010SChuck Lever {
1562ee0ac0c2SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1563ee0ac0c2SChuck Lever 	struct sock *sk = transport->inet;
1564a246b010SChuck Lever 
15657c6e066eSChuck Lever 	if (transport->rcvsize) {
1566a246b010SChuck Lever 		sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
15677c6e066eSChuck Lever 		sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2;
1568a246b010SChuck Lever 	}
15697c6e066eSChuck Lever 	if (transport->sndsize) {
1570a246b010SChuck Lever 		sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
15717c6e066eSChuck Lever 		sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2;
1572a246b010SChuck Lever 		sk->sk_write_space(sk);
1573a246b010SChuck Lever 	}
1574a246b010SChuck Lever }
1575a246b010SChuck Lever 
157643118c29SChuck Lever /**
1577470056c2SChuck Lever  * xs_udp_set_buffer_size - set send and receive limits
157843118c29SChuck Lever  * @xprt: generic transport
1579470056c2SChuck Lever  * @sndsize: requested size of send buffer, in bytes
1580470056c2SChuck Lever  * @rcvsize: requested size of receive buffer, in bytes
158143118c29SChuck Lever  *
1582470056c2SChuck Lever  * Set socket send and receive buffer size limits.
158343118c29SChuck Lever  */
1584470056c2SChuck Lever static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize)
158543118c29SChuck Lever {
15867c6e066eSChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
15877c6e066eSChuck Lever 
15887c6e066eSChuck Lever 	transport->sndsize = 0;
1589470056c2SChuck Lever 	if (sndsize)
15907c6e066eSChuck Lever 		transport->sndsize = sndsize + 1024;
15917c6e066eSChuck Lever 	transport->rcvsize = 0;
1592470056c2SChuck Lever 	if (rcvsize)
15937c6e066eSChuck Lever 		transport->rcvsize = rcvsize + 1024;
1594470056c2SChuck Lever 
1595470056c2SChuck Lever 	xs_udp_do_set_buffer_size(xprt);
159643118c29SChuck Lever }
159743118c29SChuck Lever 
159846c0ee8bSChuck Lever /**
159946c0ee8bSChuck Lever  * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
1600acf0a39fSChuck Lever  * @xprt: controlling transport
160146c0ee8bSChuck Lever  * @task: task that timed out
160246c0ee8bSChuck Lever  *
160346c0ee8bSChuck Lever  * Adjust the congestion window after a retransmit timeout has occurred.
160446c0ee8bSChuck Lever  */
16056a24dfb6STrond Myklebust static void xs_udp_timer(struct rpc_xprt *xprt, struct rpc_task *task)
160646c0ee8bSChuck Lever {
1607b5e92419STrond Myklebust 	spin_lock(&xprt->transport_lock);
16086a24dfb6STrond Myklebust 	xprt_adjust_cwnd(xprt, task, -ETIMEDOUT);
1609b5e92419STrond Myklebust 	spin_unlock(&xprt->transport_lock);
161046c0ee8bSChuck Lever }
161146c0ee8bSChuck Lever 
1612826799e6SJ. Bruce Fields static int xs_get_random_port(void)
1613b85d8806SChuck Lever {
1614826799e6SJ. Bruce Fields 	unsigned short min = xprt_min_resvport, max = xprt_max_resvport;
1615826799e6SJ. Bruce Fields 	unsigned short range;
1616826799e6SJ. Bruce Fields 	unsigned short rand;
1617826799e6SJ. Bruce Fields 
1618826799e6SJ. Bruce Fields 	if (max < min)
1619826799e6SJ. Bruce Fields 		return -EADDRINUSE;
1620826799e6SJ. Bruce Fields 	range = max - min + 1;
16218032bf12SJason A. Donenfeld 	rand = get_random_u32_below(range);
1622826799e6SJ. Bruce Fields 	return rand + min;
1623b85d8806SChuck Lever }
1624b85d8806SChuck Lever 
16254dda9c8aSTrond Myklebust static unsigned short xs_sock_getport(struct socket *sock)
16264dda9c8aSTrond Myklebust {
16274dda9c8aSTrond Myklebust 	struct sockaddr_storage buf;
16284dda9c8aSTrond Myklebust 	unsigned short port = 0;
16294dda9c8aSTrond Myklebust 
16309b2c45d4SDenys Vlasenko 	if (kernel_getsockname(sock, (struct sockaddr *)&buf) < 0)
16314dda9c8aSTrond Myklebust 		goto out;
16324dda9c8aSTrond Myklebust 	switch (buf.ss_family) {
16334dda9c8aSTrond Myklebust 	case AF_INET6:
16344dda9c8aSTrond Myklebust 		port = ntohs(((struct sockaddr_in6 *)&buf)->sin6_port);
16354dda9c8aSTrond Myklebust 		break;
16364dda9c8aSTrond Myklebust 	case AF_INET:
16374dda9c8aSTrond Myklebust 		port = ntohs(((struct sockaddr_in *)&buf)->sin_port);
16384dda9c8aSTrond Myklebust 	}
16394dda9c8aSTrond Myklebust out:
16404dda9c8aSTrond Myklebust 	return port;
16414dda9c8aSTrond Myklebust }
16424dda9c8aSTrond Myklebust 
16434dda9c8aSTrond Myklebust /**
164492200412SChuck Lever  * xs_set_port - reset the port number in the remote endpoint address
164592200412SChuck Lever  * @xprt: generic transport
164692200412SChuck Lever  * @port: new port number
164792200412SChuck Lever  *
164892200412SChuck Lever  */
164992200412SChuck Lever static void xs_set_port(struct rpc_xprt *xprt, unsigned short port)
165092200412SChuck Lever {
165192200412SChuck Lever 	dprintk("RPC:       setting port for xprt %p to %u\n", xprt, port);
1652c4efcb1dSChuck Lever 
16539dc3b095SChuck Lever 	rpc_set_port(xs_addr(xprt), port);
16549dc3b095SChuck Lever 	xs_update_peer_port(xprt);
165592200412SChuck Lever }
165692200412SChuck Lever 
16574dda9c8aSTrond Myklebust static void xs_set_srcport(struct sock_xprt *transport, struct socket *sock)
16584dda9c8aSTrond Myklebust {
1659e6237b6fSTrond Myklebust 	if (transport->srcport == 0 && transport->xprt.reuseport)
16604dda9c8aSTrond Myklebust 		transport->srcport = xs_sock_getport(sock);
16614dda9c8aSTrond Myklebust }
16624dda9c8aSTrond Myklebust 
1663826799e6SJ. Bruce Fields static int xs_get_srcport(struct sock_xprt *transport)
166467a391d7STrond Myklebust {
1665826799e6SJ. Bruce Fields 	int port = transport->srcport;
166667a391d7STrond Myklebust 
166767a391d7STrond Myklebust 	if (port == 0 && transport->xprt.resvport)
166867a391d7STrond Myklebust 		port = xs_get_random_port();
166967a391d7STrond Myklebust 	return port;
167067a391d7STrond Myklebust }
167167a391d7STrond Myklebust 
1672421ab1beSTrond Myklebust static unsigned short xs_sock_srcport(struct rpc_xprt *xprt)
1673a8482488SOlga Kornievskaia {
1674a8482488SOlga Kornievskaia 	struct sock_xprt *sock = container_of(xprt, struct sock_xprt, xprt);
1675b49ea673SNeilBrown 	unsigned short ret = 0;
1676b49ea673SNeilBrown 	mutex_lock(&sock->recv_mutex);
1677b49ea673SNeilBrown 	if (sock->sock)
1678b49ea673SNeilBrown 		ret = xs_sock_getport(sock->sock);
1679b49ea673SNeilBrown 	mutex_unlock(&sock->recv_mutex);
1680b49ea673SNeilBrown 	return ret;
1681a8482488SOlga Kornievskaia }
1682421ab1beSTrond Myklebust 
1683421ab1beSTrond Myklebust static int xs_sock_srcaddr(struct rpc_xprt *xprt, char *buf, size_t buflen)
1684421ab1beSTrond Myklebust {
1685421ab1beSTrond Myklebust 	struct sock_xprt *sock = container_of(xprt, struct sock_xprt, xprt);
1686421ab1beSTrond Myklebust 	union {
1687421ab1beSTrond Myklebust 		struct sockaddr sa;
1688421ab1beSTrond Myklebust 		struct sockaddr_storage st;
1689421ab1beSTrond Myklebust 	} saddr;
1690421ab1beSTrond Myklebust 	int ret = -ENOTCONN;
1691421ab1beSTrond Myklebust 
1692421ab1beSTrond Myklebust 	mutex_lock(&sock->recv_mutex);
1693421ab1beSTrond Myklebust 	if (sock->sock) {
1694421ab1beSTrond Myklebust 		ret = kernel_getsockname(sock->sock, &saddr.sa);
1695421ab1beSTrond Myklebust 		if (ret >= 0)
1696421ab1beSTrond Myklebust 			ret = snprintf(buf, buflen, "%pISc", &saddr.sa);
1697421ab1beSTrond Myklebust 	}
1698421ab1beSTrond Myklebust 	mutex_unlock(&sock->recv_mutex);
1699421ab1beSTrond Myklebust 	return ret;
1700421ab1beSTrond Myklebust }
1701a8482488SOlga Kornievskaia 
1702baaf4e48SPavel Emelyanov static unsigned short xs_next_srcport(struct sock_xprt *transport, unsigned short port)
170367a391d7STrond Myklebust {
1704fbfffbd5SChuck Lever 	if (transport->srcport != 0)
1705fbfffbd5SChuck Lever 		transport->srcport = 0;
170667a391d7STrond Myklebust 	if (!transport->xprt.resvport)
170767a391d7STrond Myklebust 		return 0;
170867a391d7STrond Myklebust 	if (port <= xprt_min_resvport || port > xprt_max_resvport)
170967a391d7STrond Myklebust 		return xprt_max_resvport;
171067a391d7STrond Myklebust 	return --port;
171167a391d7STrond Myklebust }
1712beb59b68SPavel Emelyanov static int xs_bind(struct sock_xprt *transport, struct socket *sock)
1713a246b010SChuck Lever {
1714beb59b68SPavel Emelyanov 	struct sockaddr_storage myaddr;
171567a391d7STrond Myklebust 	int err, nloop = 0;
1716826799e6SJ. Bruce Fields 	int port = xs_get_srcport(transport);
171767a391d7STrond Myklebust 	unsigned short last;
1718a246b010SChuck Lever 
17190f7a622cSChris Perl 	/*
17200f7a622cSChris Perl 	 * If we are asking for any ephemeral port (i.e. port == 0 &&
17210f7a622cSChris Perl 	 * transport->xprt.resvport == 0), don't bind.  Let the local
17220f7a622cSChris Perl 	 * port selection happen implicitly when the socket is used
17230f7a622cSChris Perl 	 * (for example at connect time).
17240f7a622cSChris Perl 	 *
17250f7a622cSChris Perl 	 * This ensures that we can continue to establish TCP
17260f7a622cSChris Perl 	 * connections even when all local ephemeral ports are already
17270f7a622cSChris Perl 	 * a part of some TCP connection.  This makes no difference
172812b20ce3SBhaskar Chowdhury 	 * for UDP sockets, but also doesn't harm them.
17290f7a622cSChris Perl 	 *
17300f7a622cSChris Perl 	 * If we're asking for any reserved port (i.e. port == 0 &&
17310f7a622cSChris Perl 	 * transport->xprt.resvport == 1) xs_get_srcport above will
17320f7a622cSChris Perl 	 * ensure that port is non-zero and we will bind as needed.
17330f7a622cSChris Perl 	 */
1734826799e6SJ. Bruce Fields 	if (port <= 0)
1735826799e6SJ. Bruce Fields 		return port;
17360f7a622cSChris Perl 
1737beb59b68SPavel Emelyanov 	memcpy(&myaddr, &transport->srcaddr, transport->xprt.addrlen);
1738a246b010SChuck Lever 	do {
1739beb59b68SPavel Emelyanov 		rpc_set_port((struct sockaddr *)&myaddr, port);
1740e6242e92SSridhar Samudrala 		err = kernel_bind(sock, (struct sockaddr *)&myaddr,
1741beb59b68SPavel Emelyanov 				transport->xprt.addrlen);
1742a246b010SChuck Lever 		if (err == 0) {
1743bc1c56e9SNeilBrown 			if (transport->xprt.reuseport)
1744fbfffbd5SChuck Lever 				transport->srcport = port;
1745d3bc9a1dSFrank van Maarseveen 			break;
1746a246b010SChuck Lever 		}
174767a391d7STrond Myklebust 		last = port;
1748baaf4e48SPavel Emelyanov 		port = xs_next_srcport(transport, port);
174967a391d7STrond Myklebust 		if (port > last)
175067a391d7STrond Myklebust 			nloop++;
175167a391d7STrond Myklebust 	} while (err == -EADDRINUSE && nloop != 2);
1752beb59b68SPavel Emelyanov 
17534232e863SChuck Lever 	if (myaddr.ss_family == AF_INET)
1754beb59b68SPavel Emelyanov 		dprintk("RPC:       %s %pI4:%u: %s (%d)\n", __func__,
1755beb59b68SPavel Emelyanov 				&((struct sockaddr_in *)&myaddr)->sin_addr,
1756beb59b68SPavel Emelyanov 				port, err ? "failed" : "ok", err);
1757beb59b68SPavel Emelyanov 	else
1758beb59b68SPavel Emelyanov 		dprintk("RPC:       %s %pI6:%u: %s (%d)\n", __func__,
1759beb59b68SPavel Emelyanov 				&((struct sockaddr_in6 *)&myaddr)->sin6_addr,
17607dc753f0SChuck Lever 				port, err ? "failed" : "ok", err);
1761a246b010SChuck Lever 	return err;
1762a246b010SChuck Lever }
1763a246b010SChuck Lever 
1764176e21eeSChuck Lever /*
1765176e21eeSChuck Lever  * We don't support autobind on AF_LOCAL sockets
1766176e21eeSChuck Lever  */
1767176e21eeSChuck Lever static void xs_local_rpcbind(struct rpc_task *task)
1768176e21eeSChuck Lever {
1769fb43d172STrond Myklebust 	xprt_set_bound(task->tk_xprt);
1770176e21eeSChuck Lever }
1771176e21eeSChuck Lever 
1772176e21eeSChuck Lever static void xs_local_set_port(struct rpc_xprt *xprt, unsigned short port)
1773176e21eeSChuck Lever {
1774176e21eeSChuck Lever }
1775a246b010SChuck Lever 
1776ed07536eSPeter Zijlstra #ifdef CONFIG_DEBUG_LOCK_ALLOC
1777064a9177SNeilBrown static struct lock_class_key xs_key[3];
1778064a9177SNeilBrown static struct lock_class_key xs_slock_key[3];
1779ed07536eSPeter Zijlstra 
1780176e21eeSChuck Lever static inline void xs_reclassify_socketu(struct socket *sock)
1781176e21eeSChuck Lever {
1782176e21eeSChuck Lever 	struct sock *sk = sock->sk;
1783176e21eeSChuck Lever 
1784176e21eeSChuck Lever 	sock_lock_init_class_and_name(sk, "slock-AF_LOCAL-RPC",
1785064a9177SNeilBrown 		&xs_slock_key[0], "sk_lock-AF_LOCAL-RPC", &xs_key[0]);
1786176e21eeSChuck Lever }
1787176e21eeSChuck Lever 
17888945ee5eSChuck Lever static inline void xs_reclassify_socket4(struct socket *sock)
1789ed07536eSPeter Zijlstra {
1790ed07536eSPeter Zijlstra 	struct sock *sk = sock->sk;
17918945ee5eSChuck Lever 
17928945ee5eSChuck Lever 	sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC",
1793064a9177SNeilBrown 		&xs_slock_key[1], "sk_lock-AF_INET-RPC", &xs_key[1]);
1794ed07536eSPeter Zijlstra }
17958945ee5eSChuck Lever 
17968945ee5eSChuck Lever static inline void xs_reclassify_socket6(struct socket *sock)
17978945ee5eSChuck Lever {
17988945ee5eSChuck Lever 	struct sock *sk = sock->sk;
17998945ee5eSChuck Lever 
18008945ee5eSChuck Lever 	sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC",
1801064a9177SNeilBrown 		&xs_slock_key[2], "sk_lock-AF_INET6-RPC", &xs_key[2]);
1802ed07536eSPeter Zijlstra }
18036bc9638aSPavel Emelyanov 
18046bc9638aSPavel Emelyanov static inline void xs_reclassify_socket(int family, struct socket *sock)
18056bc9638aSPavel Emelyanov {
1806fafc4e1eSHannes Frederic Sowa 	if (WARN_ON_ONCE(!sock_allow_reclassification(sock->sk)))
18071b7a1819SWeston Andros Adamson 		return;
18081b7a1819SWeston Andros Adamson 
18094232e863SChuck Lever 	switch (family) {
1810176e21eeSChuck Lever 	case AF_LOCAL:
1811176e21eeSChuck Lever 		xs_reclassify_socketu(sock);
1812176e21eeSChuck Lever 		break;
18134232e863SChuck Lever 	case AF_INET:
18146bc9638aSPavel Emelyanov 		xs_reclassify_socket4(sock);
18154232e863SChuck Lever 		break;
18164232e863SChuck Lever 	case AF_INET6:
18176bc9638aSPavel Emelyanov 		xs_reclassify_socket6(sock);
18184232e863SChuck Lever 		break;
18194232e863SChuck Lever 	}
18206bc9638aSPavel Emelyanov }
1821ed07536eSPeter Zijlstra #else
18226bc9638aSPavel Emelyanov static inline void xs_reclassify_socket(int family, struct socket *sock)
18236bc9638aSPavel Emelyanov {
18246bc9638aSPavel Emelyanov }
1825ed07536eSPeter Zijlstra #endif
1826ed07536eSPeter Zijlstra 
182793dc41bdSNeilBrown static void xs_dummy_setup_socket(struct work_struct *work)
182893dc41bdSNeilBrown {
182993dc41bdSNeilBrown }
183093dc41bdSNeilBrown 
18316bc9638aSPavel Emelyanov static struct socket *xs_create_sock(struct rpc_xprt *xprt,
18324dda9c8aSTrond Myklebust 		struct sock_xprt *transport, int family, int type,
18334dda9c8aSTrond Myklebust 		int protocol, bool reuseport)
183422f79326SPavel Emelyanov {
1835a73881c9STrond Myklebust 	struct file *filp;
183622f79326SPavel Emelyanov 	struct socket *sock;
183722f79326SPavel Emelyanov 	int err;
183822f79326SPavel Emelyanov 
18396bc9638aSPavel Emelyanov 	err = __sock_create(xprt->xprt_net, family, type, protocol, &sock, 1);
184022f79326SPavel Emelyanov 	if (err < 0) {
184122f79326SPavel Emelyanov 		dprintk("RPC:       can't create %d transport socket (%d).\n",
184222f79326SPavel Emelyanov 				protocol, -err);
184322f79326SPavel Emelyanov 		goto out;
184422f79326SPavel Emelyanov 	}
18456bc9638aSPavel Emelyanov 	xs_reclassify_socket(family, sock);
184622f79326SPavel Emelyanov 
18474dda9c8aSTrond Myklebust 	if (reuseport)
1848fe31a326SChristoph Hellwig 		sock_set_reuseport(sock->sk);
18494dda9c8aSTrond Myklebust 
18504cea288aSBen Hutchings 	err = xs_bind(transport, sock);
18514cea288aSBen Hutchings 	if (err) {
185222f79326SPavel Emelyanov 		sock_release(sock);
185322f79326SPavel Emelyanov 		goto out;
185422f79326SPavel Emelyanov 	}
185522f79326SPavel Emelyanov 
1856a73881c9STrond Myklebust 	filp = sock_alloc_file(sock, O_NONBLOCK, NULL);
1857a73881c9STrond Myklebust 	if (IS_ERR(filp))
1858a73881c9STrond Myklebust 		return ERR_CAST(filp);
1859a73881c9STrond Myklebust 	transport->file = filp;
1860a73881c9STrond Myklebust 
186122f79326SPavel Emelyanov 	return sock;
186222f79326SPavel Emelyanov out:
186322f79326SPavel Emelyanov 	return ERR_PTR(err);
186422f79326SPavel Emelyanov }
186522f79326SPavel Emelyanov 
1866176e21eeSChuck Lever static int xs_local_finish_connecting(struct rpc_xprt *xprt,
1867176e21eeSChuck Lever 				      struct socket *sock)
1868176e21eeSChuck Lever {
1869176e21eeSChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
1870176e21eeSChuck Lever 									xprt);
1871176e21eeSChuck Lever 
1872176e21eeSChuck Lever 	if (!transport->inet) {
1873176e21eeSChuck Lever 		struct sock *sk = sock->sk;
1874176e21eeSChuck Lever 
1875ea9afca8STrond Myklebust 		lock_sock(sk);
1876176e21eeSChuck Lever 
1877176e21eeSChuck Lever 		xs_save_old_callbacks(transport, sk);
1878176e21eeSChuck Lever 
1879176e21eeSChuck Lever 		sk->sk_user_data = xprt;
1880a2648094STrond Myklebust 		sk->sk_data_ready = xs_data_ready;
1881176e21eeSChuck Lever 		sk->sk_write_space = xs_udp_write_space;
1882efce2d0bSTrond Myklebust 		sk->sk_state_change = xs_local_state_change;
18832118071dSTrond Myklebust 		sk->sk_error_report = xs_error_report;
188498123866SBenjamin Coddington 		sk->sk_use_task_frag = false;
1885176e21eeSChuck Lever 
1886176e21eeSChuck Lever 		xprt_clear_connected(xprt);
1887176e21eeSChuck Lever 
1888176e21eeSChuck Lever 		/* Reset to new socket */
1889176e21eeSChuck Lever 		transport->sock = sock;
1890176e21eeSChuck Lever 		transport->inet = sk;
1891176e21eeSChuck Lever 
1892ea9afca8STrond Myklebust 		release_sock(sk);
1893176e21eeSChuck Lever 	}
1894176e21eeSChuck Lever 
1895ae053551STrond Myklebust 	xs_stream_start_connect(transport);
18966c7a64e5STrond Myklebust 
1897176e21eeSChuck Lever 	return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, 0);
1898176e21eeSChuck Lever }
1899176e21eeSChuck Lever 
1900176e21eeSChuck Lever /**
1901176e21eeSChuck Lever  * xs_local_setup_socket - create AF_LOCAL socket, connect to a local endpoint
1902176e21eeSChuck Lever  * @transport: socket transport to connect
1903176e21eeSChuck Lever  */
1904dc107402SJ. Bruce Fields static int xs_local_setup_socket(struct sock_xprt *transport)
1905176e21eeSChuck Lever {
1906176e21eeSChuck Lever 	struct rpc_xprt *xprt = &transport->xprt;
1907a73881c9STrond Myklebust 	struct file *filp;
1908176e21eeSChuck Lever 	struct socket *sock;
190968e9a246SColin Ian King 	int status;
1910176e21eeSChuck Lever 
1911176e21eeSChuck Lever 	status = __sock_create(xprt->xprt_net, AF_LOCAL,
1912176e21eeSChuck Lever 					SOCK_STREAM, 0, &sock, 1);
1913176e21eeSChuck Lever 	if (status < 0) {
1914176e21eeSChuck Lever 		dprintk("RPC:       can't create AF_LOCAL "
1915176e21eeSChuck Lever 			"transport socket (%d).\n", -status);
1916176e21eeSChuck Lever 		goto out;
1917176e21eeSChuck Lever 	}
1918d1358917SStefan Hajnoczi 	xs_reclassify_socket(AF_LOCAL, sock);
1919176e21eeSChuck Lever 
1920a73881c9STrond Myklebust 	filp = sock_alloc_file(sock, O_NONBLOCK, NULL);
1921a73881c9STrond Myklebust 	if (IS_ERR(filp)) {
1922a73881c9STrond Myklebust 		status = PTR_ERR(filp);
1923a73881c9STrond Myklebust 		goto out;
1924a73881c9STrond Myklebust 	}
1925a73881c9STrond Myklebust 	transport->file = filp;
1926a73881c9STrond Myklebust 
1927176e21eeSChuck Lever 	dprintk("RPC:       worker connecting xprt %p via AF_LOCAL to %s\n",
1928176e21eeSChuck Lever 			xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1929176e21eeSChuck Lever 
1930176e21eeSChuck Lever 	status = xs_local_finish_connecting(xprt, sock);
193140b5ea0cSTrond Myklebust 	trace_rpc_socket_connect(xprt, sock, status);
1932176e21eeSChuck Lever 	switch (status) {
1933176e21eeSChuck Lever 	case 0:
1934176e21eeSChuck Lever 		dprintk("RPC:       xprt %p connected to %s\n",
1935176e21eeSChuck Lever 				xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
19363968a8a5SChuck Lever 		xprt->stat.connect_count++;
19373968a8a5SChuck Lever 		xprt->stat.connect_time += (long)jiffies -
19383968a8a5SChuck Lever 					   xprt->stat.connect_start;
1939176e21eeSChuck Lever 		xprt_set_connected(xprt);
194093f479d3SGustavo A. R. Silva 		break;
19413601c4a9STrond Myklebust 	case -ENOBUFS:
1942176e21eeSChuck Lever 		break;
1943176e21eeSChuck Lever 	case -ENOENT:
1944176e21eeSChuck Lever 		dprintk("RPC:       xprt %p: socket %s does not exist\n",
1945176e21eeSChuck Lever 				xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1946176e21eeSChuck Lever 		break;
19474a20a988STrond Myklebust 	case -ECONNREFUSED:
19484a20a988STrond Myklebust 		dprintk("RPC:       xprt %p: connection refused for %s\n",
19494a20a988STrond Myklebust 				xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
19504a20a988STrond Myklebust 		break;
1951176e21eeSChuck Lever 	default:
1952176e21eeSChuck Lever 		printk(KERN_ERR "%s: unhandled error (%d) connecting to %s\n",
1953176e21eeSChuck Lever 				__func__, -status,
1954176e21eeSChuck Lever 				xprt->address_strings[RPC_DISPLAY_ADDR]);
1955176e21eeSChuck Lever 	}
1956176e21eeSChuck Lever 
1957176e21eeSChuck Lever out:
1958176e21eeSChuck Lever 	xprt_clear_connecting(xprt);
1959176e21eeSChuck Lever 	xprt_wake_pending_tasks(xprt, status);
1960dc107402SJ. Bruce Fields 	return status;
1961dc107402SJ. Bruce Fields }
1962dc107402SJ. Bruce Fields 
1963b6669737SLinus Torvalds static void xs_local_connect(struct rpc_xprt *xprt, struct rpc_task *task)
1964dc107402SJ. Bruce Fields {
1965dc107402SJ. Bruce Fields 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1966dc107402SJ. Bruce Fields 	int ret;
1967dc107402SJ. Bruce Fields 
1968aad41a7dSTrond Myklebust 	if (transport->file)
1969aad41a7dSTrond Myklebust 		goto force_disconnect;
1970aad41a7dSTrond Myklebust 
1971dc107402SJ. Bruce Fields 	if (RPC_IS_ASYNC(task)) {
1972dc107402SJ. Bruce Fields 		/*
1973dc107402SJ. Bruce Fields 		 * We want the AF_LOCAL connect to be resolved in the
1974dc107402SJ. Bruce Fields 		 * filesystem namespace of the process making the rpc
1975dc107402SJ. Bruce Fields 		 * call.  Thus we connect synchronously.
1976dc107402SJ. Bruce Fields 		 *
1977dc107402SJ. Bruce Fields 		 * If we want to support asynchronous AF_LOCAL calls,
1978dc107402SJ. Bruce Fields 		 * we'll need to figure out how to pass a namespace to
1979dc107402SJ. Bruce Fields 		 * connect.
1980dc107402SJ. Bruce Fields 		 */
198139494194STrond Myklebust 		rpc_task_set_rpc_status(task, -ENOTCONN);
1982aad41a7dSTrond Myklebust 		goto out_wake;
1983dc107402SJ. Bruce Fields 	}
1984dc107402SJ. Bruce Fields 	ret = xs_local_setup_socket(transport);
1985dc107402SJ. Bruce Fields 	if (ret && !RPC_IS_SOFTCONN(task))
1986dc107402SJ. Bruce Fields 		msleep_interruptible(15000);
1987aad41a7dSTrond Myklebust 	return;
1988aad41a7dSTrond Myklebust force_disconnect:
1989aad41a7dSTrond Myklebust 	xprt_force_disconnect(xprt);
1990aad41a7dSTrond Myklebust out_wake:
1991aad41a7dSTrond Myklebust 	xprt_clear_connecting(xprt);
1992aad41a7dSTrond Myklebust 	xprt_wake_pending_tasks(xprt, -ENOTCONN);
1993176e21eeSChuck Lever }
1994176e21eeSChuck Lever 
19953c87ef6eSJeff Layton #if IS_ENABLED(CONFIG_SUNRPC_SWAP)
1996d6e971d8SJeff Layton /*
1997693486d5SNeilBrown  * Note that this should be called with XPRT_LOCKED held, or recv_mutex
1998693486d5SNeilBrown  * held, or when we otherwise know that we have exclusive access to the
1999693486d5SNeilBrown  * socket, to guard against races with xs_reset_transport.
2000d6e971d8SJeff Layton  */
2001a564b8f0SMel Gorman static void xs_set_memalloc(struct rpc_xprt *xprt)
2002a564b8f0SMel Gorman {
2003a564b8f0SMel Gorman 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
2004a564b8f0SMel Gorman 			xprt);
2005a564b8f0SMel Gorman 
2006d6e971d8SJeff Layton 	/*
2007d6e971d8SJeff Layton 	 * If there's no sock, then we have nothing to set. The
2008d6e971d8SJeff Layton 	 * reconnecting process will get it for us.
2009d6e971d8SJeff Layton 	 */
2010d6e971d8SJeff Layton 	if (!transport->inet)
2011d6e971d8SJeff Layton 		return;
20128e228133SJeff Layton 	if (atomic_read(&xprt->swapper))
2013a564b8f0SMel Gorman 		sk_set_memalloc(transport->inet);
2014a564b8f0SMel Gorman }
2015a564b8f0SMel Gorman 
2016a564b8f0SMel Gorman /**
2017d67fa4d8SJeff Layton  * xs_enable_swap - Tag this transport as being used for swap.
2018a564b8f0SMel Gorman  * @xprt: transport to tag
2019a564b8f0SMel Gorman  *
20208e228133SJeff Layton  * Take a reference to this transport on behalf of the rpc_clnt, and
20218e228133SJeff Layton  * optionally mark it for swapping if it wasn't already.
2022a564b8f0SMel Gorman  */
2023d67fa4d8SJeff Layton static int
2024d67fa4d8SJeff Layton xs_enable_swap(struct rpc_xprt *xprt)
2025a564b8f0SMel Gorman {
2026d6e971d8SJeff Layton 	struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt);
2027a564b8f0SMel Gorman 
2028693486d5SNeilBrown 	mutex_lock(&xs->recv_mutex);
2029693486d5SNeilBrown 	if (atomic_inc_return(&xprt->swapper) == 1 &&
2030693486d5SNeilBrown 	    xs->inet)
2031d6e971d8SJeff Layton 		sk_set_memalloc(xs->inet);
2032693486d5SNeilBrown 	mutex_unlock(&xs->recv_mutex);
20338e228133SJeff Layton 	return 0;
2034a564b8f0SMel Gorman }
2035a564b8f0SMel Gorman 
20368e228133SJeff Layton /**
2037d67fa4d8SJeff Layton  * xs_disable_swap - Untag this transport as being used for swap.
20388e228133SJeff Layton  * @xprt: transport to tag
20398e228133SJeff Layton  *
20408e228133SJeff Layton  * Drop a "swapper" reference to this xprt on behalf of the rpc_clnt. If the
20418e228133SJeff Layton  * swapper refcount goes to 0, untag the socket as a memalloc socket.
20428e228133SJeff Layton  */
2043d67fa4d8SJeff Layton static void
2044d67fa4d8SJeff Layton xs_disable_swap(struct rpc_xprt *xprt)
20458e228133SJeff Layton {
2046d6e971d8SJeff Layton 	struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt);
20478e228133SJeff Layton 
2048693486d5SNeilBrown 	mutex_lock(&xs->recv_mutex);
2049693486d5SNeilBrown 	if (atomic_dec_and_test(&xprt->swapper) &&
2050693486d5SNeilBrown 	    xs->inet)
2051d6e971d8SJeff Layton 		sk_clear_memalloc(xs->inet);
2052693486d5SNeilBrown 	mutex_unlock(&xs->recv_mutex);
2053a564b8f0SMel Gorman }
2054a564b8f0SMel Gorman #else
2055a564b8f0SMel Gorman static void xs_set_memalloc(struct rpc_xprt *xprt)
2056a564b8f0SMel Gorman {
2057a564b8f0SMel Gorman }
2058d67fa4d8SJeff Layton 
2059d67fa4d8SJeff Layton static int
2060d67fa4d8SJeff Layton xs_enable_swap(struct rpc_xprt *xprt)
2061d67fa4d8SJeff Layton {
2062d67fa4d8SJeff Layton 	return -EINVAL;
2063d67fa4d8SJeff Layton }
2064d67fa4d8SJeff Layton 
2065d67fa4d8SJeff Layton static void
2066d67fa4d8SJeff Layton xs_disable_swap(struct rpc_xprt *xprt)
2067d67fa4d8SJeff Layton {
2068d67fa4d8SJeff Layton }
2069a564b8f0SMel Gorman #endif
2070a564b8f0SMel Gorman 
207116be2d20SChuck Lever static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
2072a246b010SChuck Lever {
207316be2d20SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2074edb267a6SChuck Lever 
2075ee0ac0c2SChuck Lever 	if (!transport->inet) {
2076b0d93ad5SChuck Lever 		struct sock *sk = sock->sk;
2077b0d93ad5SChuck Lever 
2078ea9afca8STrond Myklebust 		lock_sock(sk);
2079b0d93ad5SChuck Lever 
20802a9e1cfaSTrond Myklebust 		xs_save_old_callbacks(transport, sk);
20812a9e1cfaSTrond Myklebust 
2082b0d93ad5SChuck Lever 		sk->sk_user_data = xprt;
2083f9b2ee71STrond Myklebust 		sk->sk_data_ready = xs_data_ready;
2084b0d93ad5SChuck Lever 		sk->sk_write_space = xs_udp_write_space;
208598123866SBenjamin Coddington 		sk->sk_use_task_frag = false;
2086b0d93ad5SChuck Lever 
2087b0d93ad5SChuck Lever 		xprt_set_connected(xprt);
2088b0d93ad5SChuck Lever 
2089b0d93ad5SChuck Lever 		/* Reset to new socket */
2090ee0ac0c2SChuck Lever 		transport->sock = sock;
2091ee0ac0c2SChuck Lever 		transport->inet = sk;
2092b0d93ad5SChuck Lever 
2093a564b8f0SMel Gorman 		xs_set_memalloc(xprt);
2094a564b8f0SMel Gorman 
2095ea9afca8STrond Myklebust 		release_sock(sk);
2096b0d93ad5SChuck Lever 	}
2097470056c2SChuck Lever 	xs_udp_do_set_buffer_size(xprt);
209802910177STrond Myklebust 
209902910177STrond Myklebust 	xprt->stat.connect_start = jiffies;
210016be2d20SChuck Lever }
210116be2d20SChuck Lever 
21028c14ff2aSPavel Emelyanov static void xs_udp_setup_socket(struct work_struct *work)
2103a246b010SChuck Lever {
2104a246b010SChuck Lever 	struct sock_xprt *transport =
2105a246b010SChuck Lever 		container_of(work, struct sock_xprt, connect_worker.work);
2106a246b010SChuck Lever 	struct rpc_xprt *xprt = &transport->xprt;
2107d099b8afSColin Ian King 	struct socket *sock;
2108b65c0310SPavel Emelyanov 	int status = -EIO;
21098db55a03SNeilBrown 	unsigned int pflags = current->flags;
2110a246b010SChuck Lever 
21118db55a03SNeilBrown 	if (atomic_read(&xprt->swapper))
21128db55a03SNeilBrown 		current->flags |= PF_MEMALLOC;
21138c14ff2aSPavel Emelyanov 	sock = xs_create_sock(xprt, transport,
21144dda9c8aSTrond Myklebust 			xs_addr(xprt)->sa_family, SOCK_DGRAM,
21154dda9c8aSTrond Myklebust 			IPPROTO_UDP, false);
2116b65c0310SPavel Emelyanov 	if (IS_ERR(sock))
2117a246b010SChuck Lever 		goto out;
211868e220bdSChuck Lever 
2119c740eff8SChuck Lever 	dprintk("RPC:       worker connecting xprt %p via %s to "
2120c740eff8SChuck Lever 				"%s (port %s)\n", xprt,
2121c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_PROTO],
2122c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_ADDR],
2123c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_PORT]);
212468e220bdSChuck Lever 
212568e220bdSChuck Lever 	xs_udp_finish_connecting(xprt, sock);
212640b5ea0cSTrond Myklebust 	trace_rpc_socket_connect(xprt, sock, 0);
2127a246b010SChuck Lever 	status = 0;
2128b0d93ad5SChuck Lever out:
2129b0d93ad5SChuck Lever 	xprt_clear_connecting(xprt);
2130cf76785dSTrond Myklebust 	xprt_unlock_connect(xprt, transport);
21317d1e8255STrond Myklebust 	xprt_wake_pending_tasks(xprt, status);
21328db55a03SNeilBrown 	current_restore_flags(pflags, PF_MEMALLOC);
2133b0d93ad5SChuck Lever }
2134b0d93ad5SChuck Lever 
21354876cc77STrond Myklebust /**
21364876cc77STrond Myklebust  * xs_tcp_shutdown - gracefully shut down a TCP socket
21374876cc77STrond Myklebust  * @xprt: transport
21384876cc77STrond Myklebust  *
21394876cc77STrond Myklebust  * Initiates a graceful shutdown of the TCP socket by calling the
21404876cc77STrond Myklebust  * equivalent of shutdown(SHUT_RDWR);
21414876cc77STrond Myklebust  */
21424876cc77STrond Myklebust static void xs_tcp_shutdown(struct rpc_xprt *xprt)
21434876cc77STrond Myklebust {
21444876cc77STrond Myklebust 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
21454876cc77STrond Myklebust 	struct socket *sock = transport->sock;
21469b30889cSTrond Myklebust 	int skst = transport->inet ? transport->inet->sk_state : TCP_CLOSE;
21474876cc77STrond Myklebust 
21484876cc77STrond Myklebust 	if (sock == NULL)
21494876cc77STrond Myklebust 		return;
21500a6ff58eSTrond Myklebust 	if (!xprt->reuseport) {
21510a6ff58eSTrond Myklebust 		xs_close(xprt);
21520a6ff58eSTrond Myklebust 		return;
21530a6ff58eSTrond Myklebust 	}
21549b30889cSTrond Myklebust 	switch (skst) {
21557c81e6a9STrond Myklebust 	case TCP_FIN_WAIT1:
21567c81e6a9STrond Myklebust 	case TCP_FIN_WAIT2:
2157943d045aSSiddharth Kawar 	case TCP_LAST_ACK:
21587c81e6a9STrond Myklebust 		break;
21597c81e6a9STrond Myklebust 	case TCP_ESTABLISHED:
21607c81e6a9STrond Myklebust 	case TCP_CLOSE_WAIT:
21614876cc77STrond Myklebust 		kernel_sock_shutdown(sock, SHUT_RDWR);
21624876cc77STrond Myklebust 		trace_rpc_socket_shutdown(xprt, sock);
21639b30889cSTrond Myklebust 		break;
21647c81e6a9STrond Myklebust 	default:
21654876cc77STrond Myklebust 		xs_reset_transport(transport);
21664876cc77STrond Myklebust 	}
21679b30889cSTrond Myklebust }
21684876cc77STrond Myklebust 
21698d1b8c62STrond Myklebust static void xs_tcp_set_socket_timeouts(struct rpc_xprt *xprt,
21708d1b8c62STrond Myklebust 		struct socket *sock)
2171b0d93ad5SChuck Lever {
217216be2d20SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
21737196dbb0STrond Myklebust 	unsigned int keepidle;
21747196dbb0STrond Myklebust 	unsigned int keepcnt;
2175775f06abSTrond Myklebust 	unsigned int timeo;
21767f260e85STrond Myklebust 
2177b5e92419STrond Myklebust 	spin_lock(&xprt->transport_lock);
21787196dbb0STrond Myklebust 	keepidle = DIV_ROUND_UP(xprt->timeout->to_initval, HZ);
21797196dbb0STrond Myklebust 	keepcnt = xprt->timeout->to_retries + 1;
21807196dbb0STrond Myklebust 	timeo = jiffies_to_msecs(xprt->timeout->to_initval) *
21817196dbb0STrond Myklebust 		(xprt->timeout->to_retries + 1);
21827196dbb0STrond Myklebust 	clear_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state);
2183b5e92419STrond Myklebust 	spin_unlock(&xprt->transport_lock);
21847f260e85STrond Myklebust 
21857f260e85STrond Myklebust 	/* TCP Keepalive options */
2186ce3d9544SChristoph Hellwig 	sock_set_keepalive(sock->sk);
218771c48eb8SChristoph Hellwig 	tcp_sock_set_keepidle(sock->sk, keepidle);
2188d41ecaacSChristoph Hellwig 	tcp_sock_set_keepintvl(sock->sk, keepidle);
2189480aeb96SChristoph Hellwig 	tcp_sock_set_keepcnt(sock->sk, keepcnt);
2190b0d93ad5SChuck Lever 
21918d1b8c62STrond Myklebust 	/* TCP user timeout (see RFC5482) */
2192c488aeadSChristoph Hellwig 	tcp_sock_set_user_timeout(sock->sk, timeo);
21938d1b8c62STrond Myklebust }
21948d1b8c62STrond Myklebust 
21957196dbb0STrond Myklebust static void xs_tcp_set_connect_timeout(struct rpc_xprt *xprt,
21967196dbb0STrond Myklebust 		unsigned long connect_timeout,
21977196dbb0STrond Myklebust 		unsigned long reconnect_timeout)
21987196dbb0STrond Myklebust {
21997196dbb0STrond Myklebust 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
22007196dbb0STrond Myklebust 	struct rpc_timeout to;
22017196dbb0STrond Myklebust 	unsigned long initval;
22027196dbb0STrond Myklebust 
2203b5e92419STrond Myklebust 	spin_lock(&xprt->transport_lock);
22047196dbb0STrond Myklebust 	if (reconnect_timeout < xprt->max_reconnect_timeout)
22057196dbb0STrond Myklebust 		xprt->max_reconnect_timeout = reconnect_timeout;
22067196dbb0STrond Myklebust 	if (connect_timeout < xprt->connect_timeout) {
22077196dbb0STrond Myklebust 		memcpy(&to, xprt->timeout, sizeof(to));
22087196dbb0STrond Myklebust 		initval = DIV_ROUND_UP(connect_timeout, to.to_retries + 1);
22097196dbb0STrond Myklebust 		/* Arbitrary lower limit */
22107196dbb0STrond Myklebust 		if (initval <  XS_TCP_INIT_REEST_TO << 1)
22117196dbb0STrond Myklebust 			initval = XS_TCP_INIT_REEST_TO << 1;
22127196dbb0STrond Myklebust 		to.to_initval = initval;
22137196dbb0STrond Myklebust 		to.to_maxval = initval;
22147196dbb0STrond Myklebust 		memcpy(&transport->tcp_timeout, &to,
22157196dbb0STrond Myklebust 				sizeof(transport->tcp_timeout));
22167196dbb0STrond Myklebust 		xprt->timeout = &transport->tcp_timeout;
22177196dbb0STrond Myklebust 		xprt->connect_timeout = connect_timeout;
22187196dbb0STrond Myklebust 	}
22197196dbb0STrond Myklebust 	set_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state);
2220b5e92419STrond Myklebust 	spin_unlock(&xprt->transport_lock);
22217196dbb0STrond Myklebust }
22227196dbb0STrond Myklebust 
22238d1b8c62STrond Myklebust static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
22248d1b8c62STrond Myklebust {
22258d1b8c62STrond Myklebust 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
22268d1b8c62STrond Myklebust 
22278d1b8c62STrond Myklebust 	if (!transport->inet) {
22288d1b8c62STrond Myklebust 		struct sock *sk = sock->sk;
22298d1b8c62STrond Myklebust 
2230d88e4d82SNeilBrown 		/* Avoid temporary address, they are bad for long-lived
2231d88e4d82SNeilBrown 		 * connections such as NFS mounts.
2232d88e4d82SNeilBrown 		 * RFC4941, section 3.6 suggests that:
2233d88e4d82SNeilBrown 		 *    Individual applications, which have specific
2234d88e4d82SNeilBrown 		 *    knowledge about the normal duration of connections,
2235d88e4d82SNeilBrown 		 *    MAY override this as appropriate.
2236d88e4d82SNeilBrown 		 */
223718d5ad62SChristoph Hellwig 		if (xs_addr(xprt)->sa_family == PF_INET6) {
223818d5ad62SChristoph Hellwig 			ip6_sock_set_addr_preferences(sk,
223918d5ad62SChristoph Hellwig 				IPV6_PREFER_SRC_PUBLIC);
224018d5ad62SChristoph Hellwig 		}
2241d88e4d82SNeilBrown 
22428d1b8c62STrond Myklebust 		xs_tcp_set_socket_timeouts(xprt, sock);
2243d737e5d4STrond Myklebust 		tcp_sock_set_nodelay(sk);
2244775f06abSTrond Myklebust 
2245ea9afca8STrond Myklebust 		lock_sock(sk);
2246b0d93ad5SChuck Lever 
22472a9e1cfaSTrond Myklebust 		xs_save_old_callbacks(transport, sk);
22482a9e1cfaSTrond Myklebust 
2249b0d93ad5SChuck Lever 		sk->sk_user_data = xprt;
22505157b956STrond Myklebust 		sk->sk_data_ready = xs_data_ready;
2251b0d93ad5SChuck Lever 		sk->sk_state_change = xs_tcp_state_change;
2252b0d93ad5SChuck Lever 		sk->sk_write_space = xs_tcp_write_space;
22532118071dSTrond Myklebust 		sk->sk_error_report = xs_error_report;
225498123866SBenjamin Coddington 		sk->sk_use_task_frag = false;
22553167e12cSChuck Lever 
22563167e12cSChuck Lever 		/* socket options */
22573167e12cSChuck Lever 		sock_reset_flag(sk, SOCK_LINGER);
2258b0d93ad5SChuck Lever 
2259b0d93ad5SChuck Lever 		xprt_clear_connected(xprt);
2260b0d93ad5SChuck Lever 
2261b0d93ad5SChuck Lever 		/* Reset to new socket */
2262ee0ac0c2SChuck Lever 		transport->sock = sock;
2263ee0ac0c2SChuck Lever 		transport->inet = sk;
2264b0d93ad5SChuck Lever 
2265ea9afca8STrond Myklebust 		release_sock(sk);
2266b0d93ad5SChuck Lever 	}
2267b0d93ad5SChuck Lever 
226801d37c42STrond Myklebust 	if (!xprt_bound(xprt))
2269280254b6STrond Myklebust 		return -ENOTCONN;
227001d37c42STrond Myklebust 
2271a564b8f0SMel Gorman 	xs_set_memalloc(xprt);
2272a564b8f0SMel Gorman 
2273ae053551STrond Myklebust 	xs_stream_start_connect(transport);
2274e1806c7bSTrond Myklebust 
2275b0d93ad5SChuck Lever 	/* Tell the socket layer to start connecting... */
22760fdea1e8STrond Myklebust 	set_bit(XPRT_SOCK_CONNECTING, &transport->sock_state);
2277280254b6STrond Myklebust 	return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK);
227816be2d20SChuck Lever }
227916be2d20SChuck Lever 
228016be2d20SChuck Lever /**
2281b61d59ffSTrond Myklebust  * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint
2282acf0a39fSChuck Lever  * @work: queued work item
228316be2d20SChuck Lever  *
228416be2d20SChuck Lever  * Invoked by a work queue tasklet.
228516be2d20SChuck Lever  */
2286cdd518d5SPavel Emelyanov static void xs_tcp_setup_socket(struct work_struct *work)
228716be2d20SChuck Lever {
2288cdd518d5SPavel Emelyanov 	struct sock_xprt *transport =
2289cdd518d5SPavel Emelyanov 		container_of(work, struct sock_xprt, connect_worker.work);
229016be2d20SChuck Lever 	struct socket *sock = transport->sock;
2291a9f5f0f7SPavel Emelyanov 	struct rpc_xprt *xprt = &transport->xprt;
2292280254b6STrond Myklebust 	int status;
22938db55a03SNeilBrown 	unsigned int pflags = current->flags;
229416be2d20SChuck Lever 
22958db55a03SNeilBrown 	if (atomic_read(&xprt->swapper))
22968db55a03SNeilBrown 		current->flags |= PF_MEMALLOC;
229789f42494STrond Myklebust 
229889f42494STrond Myklebust 	if (xprt_connected(xprt))
229989f42494STrond Myklebust 		goto out;
230089f42494STrond Myklebust 	if (test_and_clear_bit(XPRT_SOCK_CONNECT_SENT,
230189f42494STrond Myklebust 			       &transport->sock_state) ||
230289f42494STrond Myklebust 	    !sock) {
230389f42494STrond Myklebust 		xs_reset_transport(transport);
230489f42494STrond Myklebust 		sock = xs_create_sock(xprt, transport, xs_addr(xprt)->sa_family,
230589f42494STrond Myklebust 				      SOCK_STREAM, IPPROTO_TCP, true);
2306b61d59ffSTrond Myklebust 		if (IS_ERR(sock)) {
2307280254b6STrond Myklebust 			xprt_wake_pending_tasks(xprt, PTR_ERR(sock));
230816be2d20SChuck Lever 			goto out;
230916be2d20SChuck Lever 		}
23107d1e8255STrond Myklebust 	}
23117d1e8255STrond Myklebust 
2312c740eff8SChuck Lever 	dprintk("RPC:       worker connecting xprt %p via %s to "
2313c740eff8SChuck Lever 				"%s (port %s)\n", xprt,
2314c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_PROTO],
2315c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_ADDR],
2316c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_PORT]);
231716be2d20SChuck Lever 
231816be2d20SChuck Lever 	status = xs_tcp_finish_connecting(xprt, sock);
231940b5ea0cSTrond Myklebust 	trace_rpc_socket_connect(xprt, sock, status);
2320a246b010SChuck Lever 	dprintk("RPC:       %p connect status %d connected %d sock state %d\n",
232146121cf7SChuck Lever 			xprt, -status, xprt_connected(xprt),
232246121cf7SChuck Lever 			sock->sk->sk_state);
2323a246b010SChuck Lever 	switch (status) {
23242a491991STrond Myklebust 	case 0:
2325a246b010SChuck Lever 	case -EINPROGRESS:
2326280254b6STrond Myklebust 		/* SYN_SENT! */
232789f42494STrond Myklebust 		set_bit(XPRT_SOCK_CONNECT_SENT, &transport->sock_state);
2328280254b6STrond Myklebust 		if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
2329280254b6STrond Myklebust 			xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2330280254b6STrond Myklebust 		fallthrough;
2331a246b010SChuck Lever 	case -EALREADY:
2332280254b6STrond Myklebust 		goto out_unlock;
2333280254b6STrond Myklebust 	case -EADDRNOTAVAIL:
2334280254b6STrond Myklebust 		/* Source port number is unavailable. Try a new one! */
2335280254b6STrond Myklebust 		transport->srcport = 0;
2336280254b6STrond Myklebust 		status = -EAGAIN;
2337280254b6STrond Myklebust 		break;
23389fcfe0c8STrond Myklebust 	case -EINVAL:
23399fcfe0c8STrond Myklebust 		/* Happens, for instance, if the user specified a link
23409fcfe0c8STrond Myklebust 		 * local IPv6 address without a scope-id.
23419fcfe0c8STrond Myklebust 		 */
23423ed5e2a2STrond Myklebust 	case -ECONNREFUSED:
23433ed5e2a2STrond Myklebust 	case -ECONNRESET:
2344eb5b46faSTrond Myklebust 	case -ENETDOWN:
23453ed5e2a2STrond Myklebust 	case -ENETUNREACH:
23464ba161a7STrond Myklebust 	case -EHOSTUNREACH:
23473913c78cSTrond Myklebust 	case -EADDRINUSE:
23483601c4a9STrond Myklebust 	case -ENOBUFS:
2349280254b6STrond Myklebust 		break;
2350280254b6STrond Myklebust 	default:
2351280254b6STrond Myklebust 		printk("%s: connect returned unhandled error %d\n",
2352280254b6STrond Myklebust 			__func__, status);
2353280254b6STrond Myklebust 		status = -EAGAIN;
2354280254b6STrond Myklebust 	}
2355280254b6STrond Myklebust 
23568c71139dSCalum Mackay 	/* xs_tcp_force_close() wakes tasks with a fixed error code.
23578c71139dSCalum Mackay 	 * We need to wake them first to ensure the correct error code.
23586ea44adcSNeilBrown 	 */
23596ea44adcSNeilBrown 	xprt_wake_pending_tasks(xprt, status);
23604efdd92cSTrond Myklebust 	xs_tcp_force_close(xprt);
2361a246b010SChuck Lever out:
23622226feb6SChuck Lever 	xprt_clear_connecting(xprt);
2363280254b6STrond Myklebust out_unlock:
2364cf76785dSTrond Myklebust 	xprt_unlock_connect(xprt, transport);
23658db55a03SNeilBrown 	current_restore_flags(pflags, PF_MEMALLOC);
2366a246b010SChuck Lever }
2367a246b010SChuck Lever 
236868e220bdSChuck Lever /**
23699903cd1cSChuck Lever  * xs_connect - connect a socket to a remote endpoint
23701b092092STrond Myklebust  * @xprt: pointer to transport structure
23719903cd1cSChuck Lever  * @task: address of RPC task that manages state of connect request
23729903cd1cSChuck Lever  *
23739903cd1cSChuck Lever  * TCP: If the remote end dropped the connection, delay reconnecting.
237403bf4b70SChuck Lever  *
237503bf4b70SChuck Lever  * UDP socket connects are synchronous, but we use a work queue anyway
237603bf4b70SChuck Lever  * to guarantee that even unprivileged user processes can set up a
237703bf4b70SChuck Lever  * socket on a privileged port.
237803bf4b70SChuck Lever  *
237903bf4b70SChuck Lever  * If a UDP socket connect fails, the delay behavior here prevents
238003bf4b70SChuck Lever  * retry floods (hard mounts).
23819903cd1cSChuck Lever  */
23821b092092STrond Myklebust static void xs_connect(struct rpc_xprt *xprt, struct rpc_task *task)
2383a246b010SChuck Lever {
2384ee0ac0c2SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
238502910177STrond Myklebust 	unsigned long delay = 0;
2386a246b010SChuck Lever 
2387718ba5b8STrond Myklebust 	WARN_ON_ONCE(!xprt_lock_connect(xprt, task, transport));
2388718ba5b8STrond Myklebust 
238989f42494STrond Myklebust 	if (transport->sock != NULL) {
239046121cf7SChuck Lever 		dprintk("RPC:       xs_connect delayed xprt %p for %lu "
239189f42494STrond Myklebust 			"seconds\n", xprt, xprt->reestablish_timeout / HZ);
239299b1a4c3STrond Myklebust 
2393675dd90aSChuck Lever 		delay = xprt_reconnect_delay(xprt);
2394675dd90aSChuck Lever 		xprt_reconnect_backoff(xprt, XS_TCP_INIT_REEST_TO);
239502910177STrond Myklebust 
239602910177STrond Myklebust 	} else
23979903cd1cSChuck Lever 		dprintk("RPC:       xs_connect scheduled xprt %p\n", xprt);
239802910177STrond Myklebust 
239940a5f1b1STrond Myklebust 	queue_delayed_work(xprtiod_workqueue,
240002910177STrond Myklebust 			&transport->connect_worker,
240102910177STrond Myklebust 			delay);
2402a246b010SChuck Lever }
2403a246b010SChuck Lever 
24044f8943f8STrond Myklebust static void xs_wake_disconnect(struct sock_xprt *transport)
24054f8943f8STrond Myklebust {
24064f8943f8STrond Myklebust 	if (test_and_clear_bit(XPRT_SOCK_WAKE_DISCONNECT, &transport->sock_state))
24074f8943f8STrond Myklebust 		xs_tcp_force_close(&transport->xprt);
24084f8943f8STrond Myklebust }
24094f8943f8STrond Myklebust 
24104f8943f8STrond Myklebust static void xs_wake_write(struct sock_xprt *transport)
24114f8943f8STrond Myklebust {
24124f8943f8STrond Myklebust 	if (test_and_clear_bit(XPRT_SOCK_WAKE_WRITE, &transport->sock_state))
24134f8943f8STrond Myklebust 		xprt_write_space(&transport->xprt);
24144f8943f8STrond Myklebust }
24154f8943f8STrond Myklebust 
24164f8943f8STrond Myklebust static void xs_wake_error(struct sock_xprt *transport)
24174f8943f8STrond Myklebust {
24184f8943f8STrond Myklebust 	int sockerr;
24194f8943f8STrond Myklebust 
24204f8943f8STrond Myklebust 	if (!test_bit(XPRT_SOCK_WAKE_ERROR, &transport->sock_state))
24214f8943f8STrond Myklebust 		return;
24224f8943f8STrond Myklebust 	mutex_lock(&transport->recv_mutex);
24234f8943f8STrond Myklebust 	if (transport->sock == NULL)
24244f8943f8STrond Myklebust 		goto out;
24254f8943f8STrond Myklebust 	if (!test_and_clear_bit(XPRT_SOCK_WAKE_ERROR, &transport->sock_state))
24264f8943f8STrond Myklebust 		goto out;
2427af84537dSBenjamin Coddington 	sockerr = xchg(&transport->xprt_err, 0);
24284f8943f8STrond Myklebust 	if (sockerr < 0)
24294f8943f8STrond Myklebust 		xprt_wake_pending_tasks(&transport->xprt, sockerr);
24304f8943f8STrond Myklebust out:
24314f8943f8STrond Myklebust 	mutex_unlock(&transport->recv_mutex);
24324f8943f8STrond Myklebust }
24334f8943f8STrond Myklebust 
24344f8943f8STrond Myklebust static void xs_wake_pending(struct sock_xprt *transport)
24354f8943f8STrond Myklebust {
24364f8943f8STrond Myklebust 	if (test_and_clear_bit(XPRT_SOCK_WAKE_PENDING, &transport->sock_state))
24374f8943f8STrond Myklebust 		xprt_wake_pending_tasks(&transport->xprt, -EAGAIN);
24384f8943f8STrond Myklebust }
24394f8943f8STrond Myklebust 
24404f8943f8STrond Myklebust static void xs_error_handle(struct work_struct *work)
24414f8943f8STrond Myklebust {
24424f8943f8STrond Myklebust 	struct sock_xprt *transport = container_of(work,
24434f8943f8STrond Myklebust 			struct sock_xprt, error_worker);
24444f8943f8STrond Myklebust 
24454f8943f8STrond Myklebust 	xs_wake_disconnect(transport);
24464f8943f8STrond Myklebust 	xs_wake_write(transport);
24474f8943f8STrond Myklebust 	xs_wake_error(transport);
24484f8943f8STrond Myklebust 	xs_wake_pending(transport);
24494f8943f8STrond Myklebust }
24504f8943f8STrond Myklebust 
2451262ca07dSChuck Lever /**
245212b20ce3SBhaskar Chowdhury  * xs_local_print_stats - display AF_LOCAL socket-specific stats
2453176e21eeSChuck Lever  * @xprt: rpc_xprt struct containing statistics
2454176e21eeSChuck Lever  * @seq: output file
2455176e21eeSChuck Lever  *
2456176e21eeSChuck Lever  */
2457176e21eeSChuck Lever static void xs_local_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2458176e21eeSChuck Lever {
2459176e21eeSChuck Lever 	long idle_time = 0;
2460176e21eeSChuck Lever 
2461176e21eeSChuck Lever 	if (xprt_connected(xprt))
2462176e21eeSChuck Lever 		idle_time = (long)(jiffies - xprt->last_used) / HZ;
2463176e21eeSChuck Lever 
2464176e21eeSChuck Lever 	seq_printf(seq, "\txprt:\tlocal %lu %lu %lu %ld %lu %lu %lu "
246515a45206SAndy Adamson 			"%llu %llu %lu %llu %llu\n",
2466176e21eeSChuck Lever 			xprt->stat.bind_count,
2467176e21eeSChuck Lever 			xprt->stat.connect_count,
24688440a886SChuck Lever 			xprt->stat.connect_time / HZ,
2469176e21eeSChuck Lever 			idle_time,
2470176e21eeSChuck Lever 			xprt->stat.sends,
2471176e21eeSChuck Lever 			xprt->stat.recvs,
2472176e21eeSChuck Lever 			xprt->stat.bad_xids,
2473176e21eeSChuck Lever 			xprt->stat.req_u,
247415a45206SAndy Adamson 			xprt->stat.bklog_u,
247515a45206SAndy Adamson 			xprt->stat.max_slots,
247615a45206SAndy Adamson 			xprt->stat.sending_u,
247715a45206SAndy Adamson 			xprt->stat.pending_u);
2478176e21eeSChuck Lever }
2479176e21eeSChuck Lever 
2480176e21eeSChuck Lever /**
248112b20ce3SBhaskar Chowdhury  * xs_udp_print_stats - display UDP socket-specific stats
2482262ca07dSChuck Lever  * @xprt: rpc_xprt struct containing statistics
2483262ca07dSChuck Lever  * @seq: output file
2484262ca07dSChuck Lever  *
2485262ca07dSChuck Lever  */
2486262ca07dSChuck Lever static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2487262ca07dSChuck Lever {
2488c8475461SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2489c8475461SChuck Lever 
249015a45206SAndy Adamson 	seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %llu %llu "
249115a45206SAndy Adamson 			"%lu %llu %llu\n",
2492fbfffbd5SChuck Lever 			transport->srcport,
2493262ca07dSChuck Lever 			xprt->stat.bind_count,
2494262ca07dSChuck Lever 			xprt->stat.sends,
2495262ca07dSChuck Lever 			xprt->stat.recvs,
2496262ca07dSChuck Lever 			xprt->stat.bad_xids,
2497262ca07dSChuck Lever 			xprt->stat.req_u,
249815a45206SAndy Adamson 			xprt->stat.bklog_u,
249915a45206SAndy Adamson 			xprt->stat.max_slots,
250015a45206SAndy Adamson 			xprt->stat.sending_u,
250115a45206SAndy Adamson 			xprt->stat.pending_u);
2502262ca07dSChuck Lever }
2503262ca07dSChuck Lever 
2504262ca07dSChuck Lever /**
250512b20ce3SBhaskar Chowdhury  * xs_tcp_print_stats - display TCP socket-specific stats
2506262ca07dSChuck Lever  * @xprt: rpc_xprt struct containing statistics
2507262ca07dSChuck Lever  * @seq: output file
2508262ca07dSChuck Lever  *
2509262ca07dSChuck Lever  */
2510262ca07dSChuck Lever static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2511262ca07dSChuck Lever {
2512c8475461SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2513262ca07dSChuck Lever 	long idle_time = 0;
2514262ca07dSChuck Lever 
2515262ca07dSChuck Lever 	if (xprt_connected(xprt))
2516262ca07dSChuck Lever 		idle_time = (long)(jiffies - xprt->last_used) / HZ;
2517262ca07dSChuck Lever 
251815a45206SAndy Adamson 	seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu "
251915a45206SAndy Adamson 			"%llu %llu %lu %llu %llu\n",
2520fbfffbd5SChuck Lever 			transport->srcport,
2521262ca07dSChuck Lever 			xprt->stat.bind_count,
2522262ca07dSChuck Lever 			xprt->stat.connect_count,
25238440a886SChuck Lever 			xprt->stat.connect_time / HZ,
2524262ca07dSChuck Lever 			idle_time,
2525262ca07dSChuck Lever 			xprt->stat.sends,
2526262ca07dSChuck Lever 			xprt->stat.recvs,
2527262ca07dSChuck Lever 			xprt->stat.bad_xids,
2528262ca07dSChuck Lever 			xprt->stat.req_u,
252915a45206SAndy Adamson 			xprt->stat.bklog_u,
253015a45206SAndy Adamson 			xprt->stat.max_slots,
253115a45206SAndy Adamson 			xprt->stat.sending_u,
253215a45206SAndy Adamson 			xprt->stat.pending_u);
2533262ca07dSChuck Lever }
2534262ca07dSChuck Lever 
25354cfc7e60SRahul Iyer /*
25364cfc7e60SRahul Iyer  * Allocate a bunch of pages for a scratch buffer for the rpc code. The reason
25374cfc7e60SRahul Iyer  * we allocate pages instead doing a kmalloc like rpc_malloc is because we want
25384cfc7e60SRahul Iyer  * to use the server side send routines.
25394cfc7e60SRahul Iyer  */
25405fe6eaa1SChuck Lever static int bc_malloc(struct rpc_task *task)
25414cfc7e60SRahul Iyer {
25425fe6eaa1SChuck Lever 	struct rpc_rqst *rqst = task->tk_rqstp;
25435fe6eaa1SChuck Lever 	size_t size = rqst->rq_callsize;
25444cfc7e60SRahul Iyer 	struct page *page;
25454cfc7e60SRahul Iyer 	struct rpc_buffer *buf;
25464cfc7e60SRahul Iyer 
25475fe6eaa1SChuck Lever 	if (size > PAGE_SIZE - sizeof(struct rpc_buffer)) {
25485fe6eaa1SChuck Lever 		WARN_ONCE(1, "xprtsock: large bc buffer request (size %zu)\n",
25495fe6eaa1SChuck Lever 			  size);
25505fe6eaa1SChuck Lever 		return -EINVAL;
25515fe6eaa1SChuck Lever 	}
25524cfc7e60SRahul Iyer 
2553b2648015STrond Myklebust 	page = alloc_page(GFP_KERNEL | __GFP_NORETRY | __GFP_NOWARN);
25544cfc7e60SRahul Iyer 	if (!page)
25555fe6eaa1SChuck Lever 		return -ENOMEM;
25564cfc7e60SRahul Iyer 
25574cfc7e60SRahul Iyer 	buf = page_address(page);
25584cfc7e60SRahul Iyer 	buf->len = PAGE_SIZE;
25594cfc7e60SRahul Iyer 
25605fe6eaa1SChuck Lever 	rqst->rq_buffer = buf->data;
256118e601d6SJeff Layton 	rqst->rq_rbuffer = (char *)rqst->rq_buffer + rqst->rq_callsize;
25625fe6eaa1SChuck Lever 	return 0;
25634cfc7e60SRahul Iyer }
25644cfc7e60SRahul Iyer 
25654cfc7e60SRahul Iyer /*
25664cfc7e60SRahul Iyer  * Free the space allocated in the bc_alloc routine
25674cfc7e60SRahul Iyer  */
25683435c74aSChuck Lever static void bc_free(struct rpc_task *task)
25694cfc7e60SRahul Iyer {
25703435c74aSChuck Lever 	void *buffer = task->tk_rqstp->rq_buffer;
25714cfc7e60SRahul Iyer 	struct rpc_buffer *buf;
25724cfc7e60SRahul Iyer 
25734cfc7e60SRahul Iyer 	buf = container_of(buffer, struct rpc_buffer, data);
25744cfc7e60SRahul Iyer 	free_page((unsigned long)buf);
25754cfc7e60SRahul Iyer }
25764cfc7e60SRahul Iyer 
25774cfc7e60SRahul Iyer static int bc_sendto(struct rpc_rqst *req)
25784cfc7e60SRahul Iyer {
2579da1661b9SChuck Lever 	struct xdr_buf *xdr = &req->rq_snd_buf;
25804cfc7e60SRahul Iyer 	struct sock_xprt *transport =
2581067fb11bSChuck Lever 			container_of(req->rq_xprt, struct sock_xprt, xprt);
2582067fb11bSChuck Lever 	struct msghdr msg = {
2583da1661b9SChuck Lever 		.msg_flags	= 0,
2584067fb11bSChuck Lever 	};
2585067fb11bSChuck Lever 	rpc_fraghdr marker = cpu_to_be32(RPC_LAST_STREAM_FRAGMENT |
2586da1661b9SChuck Lever 					 (u32)xdr->len);
2587da1661b9SChuck Lever 	unsigned int sent = 0;
2588da1661b9SChuck Lever 	int err;
25894cfc7e60SRahul Iyer 
25908729aabaSChuck Lever 	req->rq_xtime = ktime_get();
2591ff053dbbSTrond Myklebust 	err = xdr_alloc_bvec(xdr, rpc_task_gfp_mask());
2592ff053dbbSTrond Myklebust 	if (err < 0)
2593ff053dbbSTrond Myklebust 		return err;
2594da1661b9SChuck Lever 	err = xprt_sock_sendmsg(transport->sock, &msg, xdr, 0, marker, &sent);
2595da1661b9SChuck Lever 	xdr_free_bvec(xdr);
2596da1661b9SChuck Lever 	if (err < 0 || sent != (xdr->len + sizeof(marker)))
2597067fb11bSChuck Lever 		return -EAGAIN;
2598da1661b9SChuck Lever 	return sent;
25994cfc7e60SRahul Iyer }
26004cfc7e60SRahul Iyer 
2601ca4faf54SChuck Lever /**
2602ca4faf54SChuck Lever  * bc_send_request - Send a backchannel Call on a TCP socket
2603ca4faf54SChuck Lever  * @req: rpc_rqst containing Call message to be sent
2604ca4faf54SChuck Lever  *
2605ca4faf54SChuck Lever  * xpt_mutex ensures @rqstp's whole message is written to the socket
2606ca4faf54SChuck Lever  * without interruption.
2607ca4faf54SChuck Lever  *
2608ca4faf54SChuck Lever  * Return values:
2609ca4faf54SChuck Lever  *   %0 if the message was sent successfully
2610ca4faf54SChuck Lever  *   %ENOTCONN if the message was not sent
26114cfc7e60SRahul Iyer  */
2612adfa7144STrond Myklebust static int bc_send_request(struct rpc_rqst *req)
26134cfc7e60SRahul Iyer {
26144cfc7e60SRahul Iyer 	struct svc_xprt	*xprt;
26157fc56136SAndrzej Hajda 	int len;
26164cfc7e60SRahul Iyer 
26174cfc7e60SRahul Iyer 	/*
26184cfc7e60SRahul Iyer 	 * Get the server socket associated with this callback xprt
26194cfc7e60SRahul Iyer 	 */
26204cfc7e60SRahul Iyer 	xprt = req->rq_xprt->bc_xprt;
26214cfc7e60SRahul Iyer 
26224cfc7e60SRahul Iyer 	/*
26234cfc7e60SRahul Iyer 	 * Grab the mutex to serialize data as the connection is shared
26244cfc7e60SRahul Iyer 	 * with the fore channel
26254cfc7e60SRahul Iyer 	 */
2626c544577dSTrond Myklebust 	mutex_lock(&xprt->xpt_mutex);
26274cfc7e60SRahul Iyer 	if (test_bit(XPT_DEAD, &xprt->xpt_flags))
26284cfc7e60SRahul Iyer 		len = -ENOTCONN;
26294cfc7e60SRahul Iyer 	else
26304cfc7e60SRahul Iyer 		len = bc_sendto(req);
26314cfc7e60SRahul Iyer 	mutex_unlock(&xprt->xpt_mutex);
26324cfc7e60SRahul Iyer 
26334cfc7e60SRahul Iyer 	if (len > 0)
26344cfc7e60SRahul Iyer 		len = 0;
26354cfc7e60SRahul Iyer 
26364cfc7e60SRahul Iyer 	return len;
26374cfc7e60SRahul Iyer }
26384cfc7e60SRahul Iyer 
26394cfc7e60SRahul Iyer /*
26404cfc7e60SRahul Iyer  * The close routine. Since this is client initiated, we do nothing
26414cfc7e60SRahul Iyer  */
26424cfc7e60SRahul Iyer 
26434cfc7e60SRahul Iyer static void bc_close(struct rpc_xprt *xprt)
26444cfc7e60SRahul Iyer {
26456221f1d9SChuck Lever 	xprt_disconnect_done(xprt);
26464cfc7e60SRahul Iyer }
26474cfc7e60SRahul Iyer 
26484cfc7e60SRahul Iyer /*
26494cfc7e60SRahul Iyer  * The xprt destroy routine. Again, because this connection is client
26504cfc7e60SRahul Iyer  * initiated, we do nothing
26514cfc7e60SRahul Iyer  */
26524cfc7e60SRahul Iyer 
26534cfc7e60SRahul Iyer static void bc_destroy(struct rpc_xprt *xprt)
26544cfc7e60SRahul Iyer {
265547f72efaSKinglong Mee 	dprintk("RPC:       bc_destroy xprt %p\n", xprt);
265647f72efaSKinglong Mee 
265747f72efaSKinglong Mee 	xs_xprt_free(xprt);
265847f72efaSKinglong Mee 	module_put(THIS_MODULE);
26594cfc7e60SRahul Iyer }
26604cfc7e60SRahul Iyer 
2661d31ae254SChuck Lever static const struct rpc_xprt_ops xs_local_ops = {
2662176e21eeSChuck Lever 	.reserve_xprt		= xprt_reserve_xprt,
26634cd34e7cSTrond Myklebust 	.release_xprt		= xprt_release_xprt,
2664f39c1bfbSTrond Myklebust 	.alloc_slot		= xprt_alloc_slot,
2665a9cde23aSChuck Lever 	.free_slot		= xprt_free_slot,
2666176e21eeSChuck Lever 	.rpcbind		= xs_local_rpcbind,
2667176e21eeSChuck Lever 	.set_port		= xs_local_set_port,
2668dc107402SJ. Bruce Fields 	.connect		= xs_local_connect,
2669176e21eeSChuck Lever 	.buf_alloc		= rpc_malloc,
2670176e21eeSChuck Lever 	.buf_free		= rpc_free,
2671550aebfeSTrond Myklebust 	.prepare_request	= xs_stream_prepare_request,
2672176e21eeSChuck Lever 	.send_request		= xs_local_send_request,
26738ba6a92dSTrond Myklebust 	.wait_for_reply_request	= xprt_wait_for_reply_request_def,
2674176e21eeSChuck Lever 	.close			= xs_close,
2675a1311d87STrond Myklebust 	.destroy		= xs_destroy,
2676176e21eeSChuck Lever 	.print_stats		= xs_local_print_stats,
2677d67fa4d8SJeff Layton 	.enable_swap		= xs_enable_swap,
2678d67fa4d8SJeff Layton 	.disable_swap		= xs_disable_swap,
2679176e21eeSChuck Lever };
2680176e21eeSChuck Lever 
2681d31ae254SChuck Lever static const struct rpc_xprt_ops xs_udp_ops = {
268243118c29SChuck Lever 	.set_buffer_size	= xs_udp_set_buffer_size,
268312a80469SChuck Lever 	.reserve_xprt		= xprt_reserve_xprt_cong,
268449e9a890SChuck Lever 	.release_xprt		= xprt_release_xprt_cong,
2685f39c1bfbSTrond Myklebust 	.alloc_slot		= xprt_alloc_slot,
2686a9cde23aSChuck Lever 	.free_slot		= xprt_free_slot,
268745160d62SChuck Lever 	.rpcbind		= rpcb_getport_async,
268892200412SChuck Lever 	.set_port		= xs_set_port,
26899903cd1cSChuck Lever 	.connect		= xs_connect,
2690421ab1beSTrond Myklebust 	.get_srcaddr		= xs_sock_srcaddr,
2691421ab1beSTrond Myklebust 	.get_srcport		= xs_sock_srcport,
269202107148SChuck Lever 	.buf_alloc		= rpc_malloc,
269302107148SChuck Lever 	.buf_free		= rpc_free,
2694262965f5SChuck Lever 	.send_request		= xs_udp_send_request,
26958ba6a92dSTrond Myklebust 	.wait_for_reply_request	= xprt_wait_for_reply_request_rtt,
269646c0ee8bSChuck Lever 	.timer			= xs_udp_timer,
2697a58dd398SChuck Lever 	.release_request	= xprt_release_rqst_cong,
2698262965f5SChuck Lever 	.close			= xs_close,
2699262965f5SChuck Lever 	.destroy		= xs_destroy,
2700262ca07dSChuck Lever 	.print_stats		= xs_udp_print_stats,
2701d67fa4d8SJeff Layton 	.enable_swap		= xs_enable_swap,
2702d67fa4d8SJeff Layton 	.disable_swap		= xs_disable_swap,
27034a068258SChuck Lever 	.inject_disconnect	= xs_inject_disconnect,
2704262965f5SChuck Lever };
2705262965f5SChuck Lever 
2706d31ae254SChuck Lever static const struct rpc_xprt_ops xs_tcp_ops = {
270712a80469SChuck Lever 	.reserve_xprt		= xprt_reserve_xprt,
27084cd34e7cSTrond Myklebust 	.release_xprt		= xprt_release_xprt,
270936bd7de9STrond Myklebust 	.alloc_slot		= xprt_alloc_slot,
2710a9cde23aSChuck Lever 	.free_slot		= xprt_free_slot,
271145160d62SChuck Lever 	.rpcbind		= rpcb_getport_async,
271292200412SChuck Lever 	.set_port		= xs_set_port,
27130b9e7943STrond Myklebust 	.connect		= xs_connect,
2714421ab1beSTrond Myklebust 	.get_srcaddr		= xs_sock_srcaddr,
2715421ab1beSTrond Myklebust 	.get_srcport		= xs_sock_srcport,
271602107148SChuck Lever 	.buf_alloc		= rpc_malloc,
271702107148SChuck Lever 	.buf_free		= rpc_free,
2718277e4ab7STrond Myklebust 	.prepare_request	= xs_stream_prepare_request,
2719262965f5SChuck Lever 	.send_request		= xs_tcp_send_request,
27208ba6a92dSTrond Myklebust 	.wait_for_reply_request	= xprt_wait_for_reply_request_def,
2721c627d31bSTrond Myklebust 	.close			= xs_tcp_shutdown,
27229903cd1cSChuck Lever 	.destroy		= xs_destroy,
27237196dbb0STrond Myklebust 	.set_connect_timeout	= xs_tcp_set_connect_timeout,
2724262ca07dSChuck Lever 	.print_stats		= xs_tcp_print_stats,
2725d67fa4d8SJeff Layton 	.enable_swap		= xs_enable_swap,
2726d67fa4d8SJeff Layton 	.disable_swap		= xs_disable_swap,
27274a068258SChuck Lever 	.inject_disconnect	= xs_inject_disconnect,
272842e5c3e2SChuck Lever #ifdef CONFIG_SUNRPC_BACKCHANNEL
272942e5c3e2SChuck Lever 	.bc_setup		= xprt_setup_bc,
27306b26cc8cSChuck Lever 	.bc_maxpayload		= xs_tcp_bc_maxpayload,
27317402a4feSTrond Myklebust 	.bc_num_slots		= xprt_bc_max_slots,
273242e5c3e2SChuck Lever 	.bc_free_rqst		= xprt_free_bc_rqst,
273342e5c3e2SChuck Lever 	.bc_destroy		= xprt_destroy_bc,
273442e5c3e2SChuck Lever #endif
2735a246b010SChuck Lever };
2736a246b010SChuck Lever 
27374cfc7e60SRahul Iyer /*
27384cfc7e60SRahul Iyer  * The rpc_xprt_ops for the server backchannel
27394cfc7e60SRahul Iyer  */
27404cfc7e60SRahul Iyer 
2741d31ae254SChuck Lever static const struct rpc_xprt_ops bc_tcp_ops = {
27424cfc7e60SRahul Iyer 	.reserve_xprt		= xprt_reserve_xprt,
27434cfc7e60SRahul Iyer 	.release_xprt		= xprt_release_xprt,
274484e28a30SBryan Schumaker 	.alloc_slot		= xprt_alloc_slot,
2745a9cde23aSChuck Lever 	.free_slot		= xprt_free_slot,
27464cfc7e60SRahul Iyer 	.buf_alloc		= bc_malloc,
27474cfc7e60SRahul Iyer 	.buf_free		= bc_free,
27484cfc7e60SRahul Iyer 	.send_request		= bc_send_request,
27498ba6a92dSTrond Myklebust 	.wait_for_reply_request	= xprt_wait_for_reply_request_def,
27504cfc7e60SRahul Iyer 	.close			= bc_close,
27514cfc7e60SRahul Iyer 	.destroy		= bc_destroy,
27524cfc7e60SRahul Iyer 	.print_stats		= xs_tcp_print_stats,
2753d67fa4d8SJeff Layton 	.enable_swap		= xs_enable_swap,
2754d67fa4d8SJeff Layton 	.disable_swap		= xs_disable_swap,
27554a068258SChuck Lever 	.inject_disconnect	= xs_inject_disconnect,
27564cfc7e60SRahul Iyer };
27574cfc7e60SRahul Iyer 
275892476850SChuck Lever static int xs_init_anyaddr(const int family, struct sockaddr *sap)
275992476850SChuck Lever {
276092476850SChuck Lever 	static const struct sockaddr_in sin = {
276192476850SChuck Lever 		.sin_family		= AF_INET,
276292476850SChuck Lever 		.sin_addr.s_addr	= htonl(INADDR_ANY),
276392476850SChuck Lever 	};
276492476850SChuck Lever 	static const struct sockaddr_in6 sin6 = {
276592476850SChuck Lever 		.sin6_family		= AF_INET6,
276692476850SChuck Lever 		.sin6_addr		= IN6ADDR_ANY_INIT,
276792476850SChuck Lever 	};
276892476850SChuck Lever 
276992476850SChuck Lever 	switch (family) {
2770176e21eeSChuck Lever 	case AF_LOCAL:
2771176e21eeSChuck Lever 		break;
277292476850SChuck Lever 	case AF_INET:
277392476850SChuck Lever 		memcpy(sap, &sin, sizeof(sin));
277492476850SChuck Lever 		break;
277592476850SChuck Lever 	case AF_INET6:
277692476850SChuck Lever 		memcpy(sap, &sin6, sizeof(sin6));
277792476850SChuck Lever 		break;
277892476850SChuck Lever 	default:
277992476850SChuck Lever 		dprintk("RPC:       %s: Bad address family\n", __func__);
278092476850SChuck Lever 		return -EAFNOSUPPORT;
278192476850SChuck Lever 	}
278292476850SChuck Lever 	return 0;
278392476850SChuck Lever }
278492476850SChuck Lever 
27853c341b0bS\"Talpey, Thomas\ static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args,
2786d9ba131dSTrond Myklebust 				      unsigned int slot_table_size,
2787d9ba131dSTrond Myklebust 				      unsigned int max_slot_table_size)
2788c8541ecdSChuck Lever {
2789c8541ecdSChuck Lever 	struct rpc_xprt *xprt;
2790ffc2e518SChuck Lever 	struct sock_xprt *new;
2791c8541ecdSChuck Lever 
279296802a09SFrank van Maarseveen 	if (args->addrlen > sizeof(xprt->addr)) {
2793c8541ecdSChuck Lever 		dprintk("RPC:       xs_setup_xprt: address too large\n");
2794c8541ecdSChuck Lever 		return ERR_PTR(-EBADF);
2795c8541ecdSChuck Lever 	}
2796c8541ecdSChuck Lever 
2797d9ba131dSTrond Myklebust 	xprt = xprt_alloc(args->net, sizeof(*new), slot_table_size,
2798d9ba131dSTrond Myklebust 			max_slot_table_size);
2799bd1722d4SPavel Emelyanov 	if (xprt == NULL) {
280046121cf7SChuck Lever 		dprintk("RPC:       xs_setup_xprt: couldn't allocate "
280146121cf7SChuck Lever 				"rpc_xprt\n");
2802c8541ecdSChuck Lever 		return ERR_PTR(-ENOMEM);
2803c8541ecdSChuck Lever 	}
2804c8541ecdSChuck Lever 
2805bd1722d4SPavel Emelyanov 	new = container_of(xprt, struct sock_xprt, xprt);
2806edc1b01cSTrond Myklebust 	mutex_init(&new->recv_mutex);
280796802a09SFrank van Maarseveen 	memcpy(&xprt->addr, args->dstaddr, args->addrlen);
280896802a09SFrank van Maarseveen 	xprt->addrlen = args->addrlen;
2809d3bc9a1dSFrank van Maarseveen 	if (args->srcaddr)
2810fbfffbd5SChuck Lever 		memcpy(&new->srcaddr, args->srcaddr, args->addrlen);
281192476850SChuck Lever 	else {
281292476850SChuck Lever 		int err;
281392476850SChuck Lever 		err = xs_init_anyaddr(args->dstaddr->sa_family,
281492476850SChuck Lever 					(struct sockaddr *)&new->srcaddr);
28152aa13531SStanislav Kinsbursky 		if (err != 0) {
28162aa13531SStanislav Kinsbursky 			xprt_free(xprt);
281792476850SChuck Lever 			return ERR_PTR(err);
281892476850SChuck Lever 		}
28192aa13531SStanislav Kinsbursky 	}
2820c8541ecdSChuck Lever 
2821c8541ecdSChuck Lever 	return xprt;
2822c8541ecdSChuck Lever }
2823c8541ecdSChuck Lever 
2824176e21eeSChuck Lever static const struct rpc_timeout xs_local_default_timeout = {
2825176e21eeSChuck Lever 	.to_initval = 10 * HZ,
2826176e21eeSChuck Lever 	.to_maxval = 10 * HZ,
2827176e21eeSChuck Lever 	.to_retries = 2,
2828176e21eeSChuck Lever };
2829176e21eeSChuck Lever 
2830176e21eeSChuck Lever /**
2831176e21eeSChuck Lever  * xs_setup_local - Set up transport to use an AF_LOCAL socket
2832176e21eeSChuck Lever  * @args: rpc transport creation arguments
2833176e21eeSChuck Lever  *
2834176e21eeSChuck Lever  * AF_LOCAL is a "tpi_cots_ord" transport, just like TCP
2835176e21eeSChuck Lever  */
2836176e21eeSChuck Lever static struct rpc_xprt *xs_setup_local(struct xprt_create *args)
2837176e21eeSChuck Lever {
2838176e21eeSChuck Lever 	struct sockaddr_un *sun = (struct sockaddr_un *)args->dstaddr;
2839176e21eeSChuck Lever 	struct sock_xprt *transport;
2840176e21eeSChuck Lever 	struct rpc_xprt *xprt;
2841176e21eeSChuck Lever 	struct rpc_xprt *ret;
2842176e21eeSChuck Lever 
2843d9ba131dSTrond Myklebust 	xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
2844d9ba131dSTrond Myklebust 			xprt_max_tcp_slot_table_entries);
2845176e21eeSChuck Lever 	if (IS_ERR(xprt))
2846176e21eeSChuck Lever 		return xprt;
2847176e21eeSChuck Lever 	transport = container_of(xprt, struct sock_xprt, xprt);
2848176e21eeSChuck Lever 
2849176e21eeSChuck Lever 	xprt->prot = 0;
2850d3abc739SOlga Kornievskaia 	xprt->xprt_class = &xs_local_transport;
2851176e21eeSChuck Lever 	xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2852176e21eeSChuck Lever 
2853176e21eeSChuck Lever 	xprt->bind_timeout = XS_BIND_TO;
2854176e21eeSChuck Lever 	xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2855176e21eeSChuck Lever 	xprt->idle_timeout = XS_IDLE_DISC_TO;
2856176e21eeSChuck Lever 
2857176e21eeSChuck Lever 	xprt->ops = &xs_local_ops;
2858176e21eeSChuck Lever 	xprt->timeout = &xs_local_default_timeout;
2859176e21eeSChuck Lever 
2860550aebfeSTrond Myklebust 	INIT_WORK(&transport->recv_worker, xs_stream_data_receive_workfn);
28614f8943f8STrond Myklebust 	INIT_WORK(&transport->error_worker, xs_error_handle);
2862550aebfeSTrond Myklebust 	INIT_DELAYED_WORK(&transport->connect_worker, xs_dummy_setup_socket);
286393dc41bdSNeilBrown 
2864176e21eeSChuck Lever 	switch (sun->sun_family) {
2865176e21eeSChuck Lever 	case AF_LOCAL:
2866*4388ce05SNeilBrown 		if (sun->sun_path[0] != '/' && sun->sun_path[0] != '\0') {
2867176e21eeSChuck Lever 			dprintk("RPC:       bad AF_LOCAL address: %s\n",
2868176e21eeSChuck Lever 					sun->sun_path);
2869176e21eeSChuck Lever 			ret = ERR_PTR(-EINVAL);
2870176e21eeSChuck Lever 			goto out_err;
2871176e21eeSChuck Lever 		}
2872176e21eeSChuck Lever 		xprt_set_bound(xprt);
2873176e21eeSChuck Lever 		xs_format_peer_addresses(xprt, "local", RPCBIND_NETID_LOCAL);
2874176e21eeSChuck Lever 		break;
2875176e21eeSChuck Lever 	default:
2876176e21eeSChuck Lever 		ret = ERR_PTR(-EAFNOSUPPORT);
2877176e21eeSChuck Lever 		goto out_err;
2878176e21eeSChuck Lever 	}
2879176e21eeSChuck Lever 
2880176e21eeSChuck Lever 	dprintk("RPC:       set up xprt to %s via AF_LOCAL\n",
2881176e21eeSChuck Lever 			xprt->address_strings[RPC_DISPLAY_ADDR]);
2882176e21eeSChuck Lever 
2883176e21eeSChuck Lever 	if (try_module_get(THIS_MODULE))
2884176e21eeSChuck Lever 		return xprt;
2885176e21eeSChuck Lever 	ret = ERR_PTR(-EINVAL);
2886176e21eeSChuck Lever out_err:
2887315f3812SKinglong Mee 	xs_xprt_free(xprt);
2888176e21eeSChuck Lever 	return ret;
2889176e21eeSChuck Lever }
2890176e21eeSChuck Lever 
28912881ae74STrond Myklebust static const struct rpc_timeout xs_udp_default_timeout = {
28922881ae74STrond Myklebust 	.to_initval = 5 * HZ,
28932881ae74STrond Myklebust 	.to_maxval = 30 * HZ,
28942881ae74STrond Myklebust 	.to_increment = 5 * HZ,
28952881ae74STrond Myklebust 	.to_retries = 5,
28962881ae74STrond Myklebust };
28972881ae74STrond Myklebust 
28989903cd1cSChuck Lever /**
28999903cd1cSChuck Lever  * xs_setup_udp - Set up transport to use a UDP socket
290096802a09SFrank van Maarseveen  * @args: rpc transport creation arguments
29019903cd1cSChuck Lever  *
29029903cd1cSChuck Lever  */
2903483066d6SAdrian Bunk static struct rpc_xprt *xs_setup_udp(struct xprt_create *args)
2904a246b010SChuck Lever {
29058f9d5b1aSChuck Lever 	struct sockaddr *addr = args->dstaddr;
2906c8541ecdSChuck Lever 	struct rpc_xprt *xprt;
2907c8475461SChuck Lever 	struct sock_xprt *transport;
29080a68b0beSJ. Bruce Fields 	struct rpc_xprt *ret;
2909a246b010SChuck Lever 
2910d9ba131dSTrond Myklebust 	xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries,
2911d9ba131dSTrond Myklebust 			xprt_udp_slot_table_entries);
2912c8541ecdSChuck Lever 	if (IS_ERR(xprt))
2913c8541ecdSChuck Lever 		return xprt;
2914c8475461SChuck Lever 	transport = container_of(xprt, struct sock_xprt, xprt);
2915a246b010SChuck Lever 
2916ec739ef0SChuck Lever 	xprt->prot = IPPROTO_UDP;
2917d3abc739SOlga Kornievskaia 	xprt->xprt_class = &xs_udp_transport;
2918a246b010SChuck Lever 	/* XXX: header size can vary due to auth type, IPv6, etc. */
2919a246b010SChuck Lever 	xprt->max_payload = (1U << 16) - (MAX_HEADER << 3);
2920a246b010SChuck Lever 
292103bf4b70SChuck Lever 	xprt->bind_timeout = XS_BIND_TO;
292203bf4b70SChuck Lever 	xprt->reestablish_timeout = XS_UDP_REEST_TO;
292303bf4b70SChuck Lever 	xprt->idle_timeout = XS_IDLE_DISC_TO;
2924a246b010SChuck Lever 
2925262965f5SChuck Lever 	xprt->ops = &xs_udp_ops;
2926a246b010SChuck Lever 
2927ba7392bbSTrond Myklebust 	xprt->timeout = &xs_udp_default_timeout;
2928a246b010SChuck Lever 
2929f9b2ee71STrond Myklebust 	INIT_WORK(&transport->recv_worker, xs_udp_data_receive_workfn);
29304f8943f8STrond Myklebust 	INIT_WORK(&transport->error_worker, xs_error_handle);
2931edc1b01cSTrond Myklebust 	INIT_DELAYED_WORK(&transport->connect_worker, xs_udp_setup_socket);
2932edc1b01cSTrond Myklebust 
29338f9d5b1aSChuck Lever 	switch (addr->sa_family) {
29348f9d5b1aSChuck Lever 	case AF_INET:
29358f9d5b1aSChuck Lever 		if (((struct sockaddr_in *)addr)->sin_port != htons(0))
29368f9d5b1aSChuck Lever 			xprt_set_bound(xprt);
29378f9d5b1aSChuck Lever 
29389dc3b095SChuck Lever 		xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP);
29398f9d5b1aSChuck Lever 		break;
29408f9d5b1aSChuck Lever 	case AF_INET6:
29418f9d5b1aSChuck Lever 		if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
29428f9d5b1aSChuck Lever 			xprt_set_bound(xprt);
29438f9d5b1aSChuck Lever 
29449dc3b095SChuck Lever 		xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6);
29458f9d5b1aSChuck Lever 		break;
29468f9d5b1aSChuck Lever 	default:
29470a68b0beSJ. Bruce Fields 		ret = ERR_PTR(-EAFNOSUPPORT);
29480a68b0beSJ. Bruce Fields 		goto out_err;
29498f9d5b1aSChuck Lever 	}
29508f9d5b1aSChuck Lever 
2951c740eff8SChuck Lever 	if (xprt_bound(xprt))
2952c740eff8SChuck Lever 		dprintk("RPC:       set up xprt to %s (port %s) via %s\n",
2953c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_ADDR],
2954c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_PORT],
2955c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_PROTO]);
2956c740eff8SChuck Lever 	else
2957c740eff8SChuck Lever 		dprintk("RPC:       set up xprt to %s (autobind) via %s\n",
2958c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_ADDR],
2959c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_PROTO]);
2960edb267a6SChuck Lever 
2961bc25571eS\"Talpey, Thomas\ 	if (try_module_get(THIS_MODULE))
2962c8541ecdSChuck Lever 		return xprt;
29630a68b0beSJ. Bruce Fields 	ret = ERR_PTR(-EINVAL);
29640a68b0beSJ. Bruce Fields out_err:
2965315f3812SKinglong Mee 	xs_xprt_free(xprt);
29660a68b0beSJ. Bruce Fields 	return ret;
2967a246b010SChuck Lever }
2968a246b010SChuck Lever 
29692881ae74STrond Myklebust static const struct rpc_timeout xs_tcp_default_timeout = {
29702881ae74STrond Myklebust 	.to_initval = 60 * HZ,
29712881ae74STrond Myklebust 	.to_maxval = 60 * HZ,
29722881ae74STrond Myklebust 	.to_retries = 2,
29732881ae74STrond Myklebust };
29742881ae74STrond Myklebust 
29759903cd1cSChuck Lever /**
29769903cd1cSChuck Lever  * xs_setup_tcp - Set up transport to use a TCP socket
297796802a09SFrank van Maarseveen  * @args: rpc transport creation arguments
29789903cd1cSChuck Lever  *
29799903cd1cSChuck Lever  */
2980483066d6SAdrian Bunk static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args)
2981a246b010SChuck Lever {
29828f9d5b1aSChuck Lever 	struct sockaddr *addr = args->dstaddr;
2983c8541ecdSChuck Lever 	struct rpc_xprt *xprt;
2984c8475461SChuck Lever 	struct sock_xprt *transport;
29850a68b0beSJ. Bruce Fields 	struct rpc_xprt *ret;
2986b7993cebSTrond Myklebust 	unsigned int max_slot_table_size = xprt_max_tcp_slot_table_entries;
2987b7993cebSTrond Myklebust 
2988b7993cebSTrond Myklebust 	if (args->flags & XPRT_CREATE_INFINITE_SLOTS)
2989b7993cebSTrond Myklebust 		max_slot_table_size = RPC_MAX_SLOT_TABLE_LIMIT;
2990a246b010SChuck Lever 
2991d9ba131dSTrond Myklebust 	xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
2992b7993cebSTrond Myklebust 			max_slot_table_size);
2993c8541ecdSChuck Lever 	if (IS_ERR(xprt))
2994c8541ecdSChuck Lever 		return xprt;
2995c8475461SChuck Lever 	transport = container_of(xprt, struct sock_xprt, xprt);
2996a246b010SChuck Lever 
2997ec739ef0SChuck Lever 	xprt->prot = IPPROTO_TCP;
2998d3abc739SOlga Kornievskaia 	xprt->xprt_class = &xs_tcp_transport;
2999808012fbSChuck Lever 	xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
3000a246b010SChuck Lever 
300103bf4b70SChuck Lever 	xprt->bind_timeout = XS_BIND_TO;
300203bf4b70SChuck Lever 	xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
300303bf4b70SChuck Lever 	xprt->idle_timeout = XS_IDLE_DISC_TO;
3004a246b010SChuck Lever 
3005262965f5SChuck Lever 	xprt->ops = &xs_tcp_ops;
3006ba7392bbSTrond Myklebust 	xprt->timeout = &xs_tcp_default_timeout;
3007a246b010SChuck Lever 
30083851f1cdSTrond Myklebust 	xprt->max_reconnect_timeout = xprt->timeout->to_maxval;
30097196dbb0STrond Myklebust 	xprt->connect_timeout = xprt->timeout->to_initval *
30107196dbb0STrond Myklebust 		(xprt->timeout->to_retries + 1);
30113851f1cdSTrond Myklebust 
3012c50b8ee0STrond Myklebust 	INIT_WORK(&transport->recv_worker, xs_stream_data_receive_workfn);
30134f8943f8STrond Myklebust 	INIT_WORK(&transport->error_worker, xs_error_handle);
3014edc1b01cSTrond Myklebust 	INIT_DELAYED_WORK(&transport->connect_worker, xs_tcp_setup_socket);
3015edc1b01cSTrond Myklebust 
30168f9d5b1aSChuck Lever 	switch (addr->sa_family) {
30178f9d5b1aSChuck Lever 	case AF_INET:
30188f9d5b1aSChuck Lever 		if (((struct sockaddr_in *)addr)->sin_port != htons(0))
30198f9d5b1aSChuck Lever 			xprt_set_bound(xprt);
30208f9d5b1aSChuck Lever 
30219dc3b095SChuck Lever 		xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP);
30228f9d5b1aSChuck Lever 		break;
30238f9d5b1aSChuck Lever 	case AF_INET6:
30248f9d5b1aSChuck Lever 		if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
30258f9d5b1aSChuck Lever 			xprt_set_bound(xprt);
30268f9d5b1aSChuck Lever 
30279dc3b095SChuck Lever 		xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6);
30288f9d5b1aSChuck Lever 		break;
30298f9d5b1aSChuck Lever 	default:
30300a68b0beSJ. Bruce Fields 		ret = ERR_PTR(-EAFNOSUPPORT);
30310a68b0beSJ. Bruce Fields 		goto out_err;
30328f9d5b1aSChuck Lever 	}
30338f9d5b1aSChuck Lever 
3034c740eff8SChuck Lever 	if (xprt_bound(xprt))
3035c740eff8SChuck Lever 		dprintk("RPC:       set up xprt to %s (port %s) via %s\n",
3036c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_ADDR],
3037c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_PORT],
3038c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_PROTO]);
3039c740eff8SChuck Lever 	else
3040c740eff8SChuck Lever 		dprintk("RPC:       set up xprt to %s (autobind) via %s\n",
3041c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_ADDR],
3042c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_PROTO]);
3043c740eff8SChuck Lever 
3044bc25571eS\"Talpey, Thomas\ 	if (try_module_get(THIS_MODULE))
3045c8541ecdSChuck Lever 		return xprt;
30460a68b0beSJ. Bruce Fields 	ret = ERR_PTR(-EINVAL);
30470a68b0beSJ. Bruce Fields out_err:
3048315f3812SKinglong Mee 	xs_xprt_free(xprt);
30490a68b0beSJ. Bruce Fields 	return ret;
3050a246b010SChuck Lever }
3051282b32e1SChuck Lever 
3052f300babaSAlexandros Batsakis /**
3053f300babaSAlexandros Batsakis  * xs_setup_bc_tcp - Set up transport to use a TCP backchannel socket
3054f300babaSAlexandros Batsakis  * @args: rpc transport creation arguments
3055f300babaSAlexandros Batsakis  *
3056f300babaSAlexandros Batsakis  */
3057f300babaSAlexandros Batsakis static struct rpc_xprt *xs_setup_bc_tcp(struct xprt_create *args)
3058f300babaSAlexandros Batsakis {
3059f300babaSAlexandros Batsakis 	struct sockaddr *addr = args->dstaddr;
3060f300babaSAlexandros Batsakis 	struct rpc_xprt *xprt;
3061f300babaSAlexandros Batsakis 	struct sock_xprt *transport;
3062f300babaSAlexandros Batsakis 	struct svc_sock *bc_sock;
30630a68b0beSJ. Bruce Fields 	struct rpc_xprt *ret;
3064f300babaSAlexandros Batsakis 
3065d9ba131dSTrond Myklebust 	xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
3066d9ba131dSTrond Myklebust 			xprt_tcp_slot_table_entries);
3067f300babaSAlexandros Batsakis 	if (IS_ERR(xprt))
3068f300babaSAlexandros Batsakis 		return xprt;
3069f300babaSAlexandros Batsakis 	transport = container_of(xprt, struct sock_xprt, xprt);
3070f300babaSAlexandros Batsakis 
3071f300babaSAlexandros Batsakis 	xprt->prot = IPPROTO_TCP;
3072d3abc739SOlga Kornievskaia 	xprt->xprt_class = &xs_bc_tcp_transport;
3073f300babaSAlexandros Batsakis 	xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
3074f300babaSAlexandros Batsakis 	xprt->timeout = &xs_tcp_default_timeout;
3075f300babaSAlexandros Batsakis 
3076f300babaSAlexandros Batsakis 	/* backchannel */
3077f300babaSAlexandros Batsakis 	xprt_set_bound(xprt);
3078f300babaSAlexandros Batsakis 	xprt->bind_timeout = 0;
3079f300babaSAlexandros Batsakis 	xprt->reestablish_timeout = 0;
3080f300babaSAlexandros Batsakis 	xprt->idle_timeout = 0;
3081f300babaSAlexandros Batsakis 
3082f300babaSAlexandros Batsakis 	xprt->ops = &bc_tcp_ops;
3083f300babaSAlexandros Batsakis 
3084f300babaSAlexandros Batsakis 	switch (addr->sa_family) {
3085f300babaSAlexandros Batsakis 	case AF_INET:
3086f300babaSAlexandros Batsakis 		xs_format_peer_addresses(xprt, "tcp",
3087f300babaSAlexandros Batsakis 					 RPCBIND_NETID_TCP);
3088f300babaSAlexandros Batsakis 		break;
3089f300babaSAlexandros Batsakis 	case AF_INET6:
3090f300babaSAlexandros Batsakis 		xs_format_peer_addresses(xprt, "tcp",
3091f300babaSAlexandros Batsakis 				   RPCBIND_NETID_TCP6);
3092f300babaSAlexandros Batsakis 		break;
3093f300babaSAlexandros Batsakis 	default:
30940a68b0beSJ. Bruce Fields 		ret = ERR_PTR(-EAFNOSUPPORT);
30950a68b0beSJ. Bruce Fields 		goto out_err;
3096f300babaSAlexandros Batsakis 	}
3097f300babaSAlexandros Batsakis 
3098f300babaSAlexandros Batsakis 	dprintk("RPC:       set up xprt to %s (port %s) via %s\n",
3099f300babaSAlexandros Batsakis 			xprt->address_strings[RPC_DISPLAY_ADDR],
3100f300babaSAlexandros Batsakis 			xprt->address_strings[RPC_DISPLAY_PORT],
3101f300babaSAlexandros Batsakis 			xprt->address_strings[RPC_DISPLAY_PROTO]);
3102f300babaSAlexandros Batsakis 
3103f300babaSAlexandros Batsakis 	/*
310499de8ea9SJ. Bruce Fields 	 * Once we've associated a backchannel xprt with a connection,
310528303ca3SWeng Meiling 	 * we want to keep it around as long as the connection lasts,
310628303ca3SWeng Meiling 	 * in case we need to start using it for a backchannel again;
310728303ca3SWeng Meiling 	 * this reference won't be dropped until bc_xprt is destroyed.
310899de8ea9SJ. Bruce Fields 	 */
310999de8ea9SJ. Bruce Fields 	xprt_get(xprt);
311099de8ea9SJ. Bruce Fields 	args->bc_xprt->xpt_bc_xprt = xprt;
311199de8ea9SJ. Bruce Fields 	xprt->bc_xprt = args->bc_xprt;
311299de8ea9SJ. Bruce Fields 	bc_sock = container_of(args->bc_xprt, struct svc_sock, sk_xprt);
311399de8ea9SJ. Bruce Fields 	transport->sock = bc_sock->sk_sock;
311499de8ea9SJ. Bruce Fields 	transport->inet = bc_sock->sk_sk;
311599de8ea9SJ. Bruce Fields 
311699de8ea9SJ. Bruce Fields 	/*
3117f300babaSAlexandros Batsakis 	 * Since we don't want connections for the backchannel, we set
3118f300babaSAlexandros Batsakis 	 * the xprt status to connected
3119f300babaSAlexandros Batsakis 	 */
3120f300babaSAlexandros Batsakis 	xprt_set_connected(xprt);
3121f300babaSAlexandros Batsakis 
3122f300babaSAlexandros Batsakis 	if (try_module_get(THIS_MODULE))
3123f300babaSAlexandros Batsakis 		return xprt;
3124642aab58SKinglong Mee 
3125642aab58SKinglong Mee 	args->bc_xprt->xpt_bc_xprt = NULL;
312639a9beabSJ. Bruce Fields 	args->bc_xprt->xpt_bc_xps = NULL;
312799de8ea9SJ. Bruce Fields 	xprt_put(xprt);
31280a68b0beSJ. Bruce Fields 	ret = ERR_PTR(-EINVAL);
31290a68b0beSJ. Bruce Fields out_err:
3130315f3812SKinglong Mee 	xs_xprt_free(xprt);
31310a68b0beSJ. Bruce Fields 	return ret;
3132f300babaSAlexandros Batsakis }
3133f300babaSAlexandros Batsakis 
3134176e21eeSChuck Lever static struct xprt_class	xs_local_transport = {
3135176e21eeSChuck Lever 	.list		= LIST_HEAD_INIT(xs_local_transport.list),
3136176e21eeSChuck Lever 	.name		= "named UNIX socket",
3137176e21eeSChuck Lever 	.owner		= THIS_MODULE,
3138176e21eeSChuck Lever 	.ident		= XPRT_TRANSPORT_LOCAL,
3139176e21eeSChuck Lever 	.setup		= xs_setup_local,
3140d5aa6b22STrond Myklebust 	.netid		= { "" },
3141176e21eeSChuck Lever };
3142176e21eeSChuck Lever 
3143bc25571eS\"Talpey, Thomas\ static struct xprt_class	xs_udp_transport = {
3144bc25571eS\"Talpey, Thomas\ 	.list		= LIST_HEAD_INIT(xs_udp_transport.list),
3145bc25571eS\"Talpey, Thomas\ 	.name		= "udp",
3146bc25571eS\"Talpey, Thomas\ 	.owner		= THIS_MODULE,
3147f300babaSAlexandros Batsakis 	.ident		= XPRT_TRANSPORT_UDP,
3148bc25571eS\"Talpey, Thomas\ 	.setup		= xs_setup_udp,
3149d5aa6b22STrond Myklebust 	.netid		= { "udp", "udp6", "" },
3150bc25571eS\"Talpey, Thomas\ };
3151bc25571eS\"Talpey, Thomas\ 
3152bc25571eS\"Talpey, Thomas\ static struct xprt_class	xs_tcp_transport = {
3153bc25571eS\"Talpey, Thomas\ 	.list		= LIST_HEAD_INIT(xs_tcp_transport.list),
3154bc25571eS\"Talpey, Thomas\ 	.name		= "tcp",
3155bc25571eS\"Talpey, Thomas\ 	.owner		= THIS_MODULE,
3156f300babaSAlexandros Batsakis 	.ident		= XPRT_TRANSPORT_TCP,
3157bc25571eS\"Talpey, Thomas\ 	.setup		= xs_setup_tcp,
3158d5aa6b22STrond Myklebust 	.netid		= { "tcp", "tcp6", "" },
3159bc25571eS\"Talpey, Thomas\ };
3160bc25571eS\"Talpey, Thomas\ 
3161f300babaSAlexandros Batsakis static struct xprt_class	xs_bc_tcp_transport = {
3162f300babaSAlexandros Batsakis 	.list		= LIST_HEAD_INIT(xs_bc_tcp_transport.list),
3163f300babaSAlexandros Batsakis 	.name		= "tcp NFSv4.1 backchannel",
3164f300babaSAlexandros Batsakis 	.owner		= THIS_MODULE,
3165f300babaSAlexandros Batsakis 	.ident		= XPRT_TRANSPORT_BC_TCP,
3166f300babaSAlexandros Batsakis 	.setup		= xs_setup_bc_tcp,
3167d5aa6b22STrond Myklebust 	.netid		= { "" },
3168f300babaSAlexandros Batsakis };
3169f300babaSAlexandros Batsakis 
3170282b32e1SChuck Lever /**
3171bc25571eS\"Talpey, Thomas\  * init_socket_xprt - set up xprtsock's sysctls, register with RPC client
3172282b32e1SChuck Lever  *
3173282b32e1SChuck Lever  */
3174282b32e1SChuck Lever int init_socket_xprt(void)
3175282b32e1SChuck Lever {
31762b1bec5fSEric W. Biederman 	if (!sunrpc_table_header)
3177c946cb69SLuis Chamberlain 		sunrpc_table_header = register_sysctl("sunrpc", xs_tunables_table);
3178fbf76683SChuck Lever 
3179176e21eeSChuck Lever 	xprt_register_transport(&xs_local_transport);
3180bc25571eS\"Talpey, Thomas\ 	xprt_register_transport(&xs_udp_transport);
3181bc25571eS\"Talpey, Thomas\ 	xprt_register_transport(&xs_tcp_transport);
3182f300babaSAlexandros Batsakis 	xprt_register_transport(&xs_bc_tcp_transport);
3183bc25571eS\"Talpey, Thomas\ 
3184282b32e1SChuck Lever 	return 0;
3185282b32e1SChuck Lever }
3186282b32e1SChuck Lever 
3187282b32e1SChuck Lever /**
3188bc25571eS\"Talpey, Thomas\  * cleanup_socket_xprt - remove xprtsock's sysctls, unregister
3189282b32e1SChuck Lever  *
3190282b32e1SChuck Lever  */
3191282b32e1SChuck Lever void cleanup_socket_xprt(void)
3192282b32e1SChuck Lever {
3193fbf76683SChuck Lever 	if (sunrpc_table_header) {
3194fbf76683SChuck Lever 		unregister_sysctl_table(sunrpc_table_header);
3195fbf76683SChuck Lever 		sunrpc_table_header = NULL;
3196fbf76683SChuck Lever 	}
3197bc25571eS\"Talpey, Thomas\ 
3198176e21eeSChuck Lever 	xprt_unregister_transport(&xs_local_transport);
3199bc25571eS\"Talpey, Thomas\ 	xprt_unregister_transport(&xs_udp_transport);
3200bc25571eS\"Talpey, Thomas\ 	xprt_unregister_transport(&xs_tcp_transport);
3201f300babaSAlexandros Batsakis 	xprt_unregister_transport(&xs_bc_tcp_transport);
3202282b32e1SChuck Lever }
3203cbf11071STrond Myklebust 
32049bbb9e5aSRusty Russell static int param_set_portnr(const char *val, const struct kernel_param *kp)
3205cbf11071STrond Myklebust {
3206cbf11071STrond Myklebust 	return param_set_uint_minmax(val, kp,
3207cbf11071STrond Myklebust 			RPC_MIN_RESVPORT,
3208cbf11071STrond Myklebust 			RPC_MAX_RESVPORT);
3209cbf11071STrond Myklebust }
3210cbf11071STrond Myklebust 
32119c27847dSLuis R. Rodriguez static const struct kernel_param_ops param_ops_portnr = {
32129bbb9e5aSRusty Russell 	.set = param_set_portnr,
32139bbb9e5aSRusty Russell 	.get = param_get_uint,
32149bbb9e5aSRusty Russell };
32159bbb9e5aSRusty Russell 
3216cbf11071STrond Myklebust #define param_check_portnr(name, p) \
3217cbf11071STrond Myklebust 	__param_check(name, p, unsigned int);
3218cbf11071STrond Myklebust 
3219cbf11071STrond Myklebust module_param_named(min_resvport, xprt_min_resvport, portnr, 0644);
3220cbf11071STrond Myklebust module_param_named(max_resvport, xprt_max_resvport, portnr, 0644);
3221cbf11071STrond Myklebust 
32229bbb9e5aSRusty Russell static int param_set_slot_table_size(const char *val,
32239bbb9e5aSRusty Russell 				     const struct kernel_param *kp)
3224cbf11071STrond Myklebust {
3225cbf11071STrond Myklebust 	return param_set_uint_minmax(val, kp,
3226cbf11071STrond Myklebust 			RPC_MIN_SLOT_TABLE,
3227cbf11071STrond Myklebust 			RPC_MAX_SLOT_TABLE);
3228cbf11071STrond Myklebust }
3229cbf11071STrond Myklebust 
32309c27847dSLuis R. Rodriguez static const struct kernel_param_ops param_ops_slot_table_size = {
32319bbb9e5aSRusty Russell 	.set = param_set_slot_table_size,
32329bbb9e5aSRusty Russell 	.get = param_get_uint,
32339bbb9e5aSRusty Russell };
32349bbb9e5aSRusty Russell 
3235cbf11071STrond Myklebust #define param_check_slot_table_size(name, p) \
3236cbf11071STrond Myklebust 	__param_check(name, p, unsigned int);
3237cbf11071STrond Myklebust 
3238d9ba131dSTrond Myklebust static int param_set_max_slot_table_size(const char *val,
3239d9ba131dSTrond Myklebust 				     const struct kernel_param *kp)
3240d9ba131dSTrond Myklebust {
3241d9ba131dSTrond Myklebust 	return param_set_uint_minmax(val, kp,
3242d9ba131dSTrond Myklebust 			RPC_MIN_SLOT_TABLE,
3243d9ba131dSTrond Myklebust 			RPC_MAX_SLOT_TABLE_LIMIT);
3244d9ba131dSTrond Myklebust }
3245d9ba131dSTrond Myklebust 
32469c27847dSLuis R. Rodriguez static const struct kernel_param_ops param_ops_max_slot_table_size = {
3247d9ba131dSTrond Myklebust 	.set = param_set_max_slot_table_size,
3248d9ba131dSTrond Myklebust 	.get = param_get_uint,
3249d9ba131dSTrond Myklebust };
3250d9ba131dSTrond Myklebust 
3251d9ba131dSTrond Myklebust #define param_check_max_slot_table_size(name, p) \
3252d9ba131dSTrond Myklebust 	__param_check(name, p, unsigned int);
3253d9ba131dSTrond Myklebust 
3254cbf11071STrond Myklebust module_param_named(tcp_slot_table_entries, xprt_tcp_slot_table_entries,
3255cbf11071STrond Myklebust 		   slot_table_size, 0644);
3256d9ba131dSTrond Myklebust module_param_named(tcp_max_slot_table_entries, xprt_max_tcp_slot_table_entries,
3257d9ba131dSTrond Myklebust 		   max_slot_table_size, 0644);
3258cbf11071STrond Myklebust module_param_named(udp_slot_table_entries, xprt_udp_slot_table_entries,
3259cbf11071STrond Myklebust 		   slot_table_size, 0644);
3260