xref: /openbmc/linux/net/sunrpc/xprtsock.c (revision 71c48eb8)
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 
5540b5ea0cSTrond Myklebust #include <trace/events/sunrpc.h>
5640b5ea0cSTrond Myklebust 
579e55eef4SChuck Lever #include "socklib.h"
584cfc7e60SRahul Iyer #include "sunrpc.h"
59176e21eeSChuck Lever 
60176e21eeSChuck Lever static void xs_close(struct rpc_xprt *xprt);
617196dbb0STrond Myklebust static void xs_tcp_set_socket_timeouts(struct rpc_xprt *xprt,
627196dbb0STrond Myklebust 		struct socket *sock);
63176e21eeSChuck Lever 
649903cd1cSChuck Lever /*
65c556b754SChuck Lever  * xprtsock tunables
66c556b754SChuck Lever  */
6709acfea5STrond Myklebust static unsigned int xprt_udp_slot_table_entries = RPC_DEF_SLOT_TABLE;
6809acfea5STrond Myklebust static unsigned int xprt_tcp_slot_table_entries = RPC_MIN_SLOT_TABLE;
6909acfea5STrond Myklebust static unsigned int xprt_max_tcp_slot_table_entries = RPC_MAX_SLOT_TABLE;
70c556b754SChuck Lever 
7109acfea5STrond Myklebust static unsigned int xprt_min_resvport = RPC_DEF_MIN_RESVPORT;
7209acfea5STrond Myklebust static unsigned int xprt_max_resvport = RPC_DEF_MAX_RESVPORT;
73c556b754SChuck Lever 
747d1e8255STrond Myklebust #define XS_TCP_LINGER_TO	(15U * HZ)
7525fe6142STrond Myklebust static unsigned int xs_tcp_fin_timeout __read_mostly = XS_TCP_LINGER_TO;
767d1e8255STrond Myklebust 
77c556b754SChuck Lever /*
78fbf76683SChuck Lever  * We can register our own files under /proc/sys/sunrpc by
79fbf76683SChuck Lever  * calling register_sysctl_table() again.  The files in that
80fbf76683SChuck Lever  * directory become the union of all files registered there.
81fbf76683SChuck Lever  *
82fbf76683SChuck Lever  * We simply need to make sure that we don't collide with
83fbf76683SChuck Lever  * someone else's file names!
84fbf76683SChuck Lever  */
85fbf76683SChuck Lever 
86fbf76683SChuck Lever static unsigned int min_slot_table_size = RPC_MIN_SLOT_TABLE;
87fbf76683SChuck Lever static unsigned int max_slot_table_size = RPC_MAX_SLOT_TABLE;
88d9ba131dSTrond Myklebust static unsigned int max_tcp_slot_table_limit = RPC_MAX_SLOT_TABLE_LIMIT;
89fbf76683SChuck Lever static unsigned int xprt_min_resvport_limit = RPC_MIN_RESVPORT;
90fbf76683SChuck Lever static unsigned int xprt_max_resvport_limit = RPC_MAX_RESVPORT;
91fbf76683SChuck Lever 
92fbf76683SChuck Lever static struct ctl_table_header *sunrpc_table_header;
93fbf76683SChuck Lever 
94fbf76683SChuck Lever /*
95fbf76683SChuck Lever  * FIXME: changing the UDP slot table size should also resize the UDP
96fbf76683SChuck Lever  *        socket buffers for existing UDP transports
97fbf76683SChuck Lever  */
98fe2c6338SJoe Perches static struct ctl_table xs_tunables_table[] = {
99fbf76683SChuck Lever 	{
100fbf76683SChuck Lever 		.procname	= "udp_slot_table_entries",
101fbf76683SChuck Lever 		.data		= &xprt_udp_slot_table_entries,
102fbf76683SChuck Lever 		.maxlen		= sizeof(unsigned int),
103fbf76683SChuck Lever 		.mode		= 0644,
1046d456111SEric W. Biederman 		.proc_handler	= proc_dointvec_minmax,
105fbf76683SChuck Lever 		.extra1		= &min_slot_table_size,
106fbf76683SChuck Lever 		.extra2		= &max_slot_table_size
107fbf76683SChuck Lever 	},
108fbf76683SChuck Lever 	{
109fbf76683SChuck Lever 		.procname	= "tcp_slot_table_entries",
110fbf76683SChuck Lever 		.data		= &xprt_tcp_slot_table_entries,
111fbf76683SChuck Lever 		.maxlen		= sizeof(unsigned int),
112fbf76683SChuck Lever 		.mode		= 0644,
1136d456111SEric W. Biederman 		.proc_handler	= proc_dointvec_minmax,
114fbf76683SChuck Lever 		.extra1		= &min_slot_table_size,
115fbf76683SChuck Lever 		.extra2		= &max_slot_table_size
116fbf76683SChuck Lever 	},
117fbf76683SChuck Lever 	{
118d9ba131dSTrond Myklebust 		.procname	= "tcp_max_slot_table_entries",
119d9ba131dSTrond Myklebust 		.data		= &xprt_max_tcp_slot_table_entries,
120d9ba131dSTrond Myklebust 		.maxlen		= sizeof(unsigned int),
121d9ba131dSTrond Myklebust 		.mode		= 0644,
122d9ba131dSTrond Myklebust 		.proc_handler	= proc_dointvec_minmax,
123d9ba131dSTrond Myklebust 		.extra1		= &min_slot_table_size,
124d9ba131dSTrond Myklebust 		.extra2		= &max_tcp_slot_table_limit
125d9ba131dSTrond Myklebust 	},
126d9ba131dSTrond Myklebust 	{
127fbf76683SChuck Lever 		.procname	= "min_resvport",
128fbf76683SChuck Lever 		.data		= &xprt_min_resvport,
129fbf76683SChuck Lever 		.maxlen		= sizeof(unsigned int),
130fbf76683SChuck Lever 		.mode		= 0644,
1316d456111SEric W. Biederman 		.proc_handler	= proc_dointvec_minmax,
132fbf76683SChuck Lever 		.extra1		= &xprt_min_resvport_limit,
133826799e6SJ. Bruce Fields 		.extra2		= &xprt_max_resvport_limit
134fbf76683SChuck Lever 	},
135fbf76683SChuck Lever 	{
136fbf76683SChuck Lever 		.procname	= "max_resvport",
137fbf76683SChuck Lever 		.data		= &xprt_max_resvport,
138fbf76683SChuck Lever 		.maxlen		= sizeof(unsigned int),
139fbf76683SChuck Lever 		.mode		= 0644,
1406d456111SEric W. Biederman 		.proc_handler	= proc_dointvec_minmax,
141826799e6SJ. Bruce Fields 		.extra1		= &xprt_min_resvport_limit,
142fbf76683SChuck Lever 		.extra2		= &xprt_max_resvport_limit
143fbf76683SChuck Lever 	},
144fbf76683SChuck Lever 	{
14525fe6142STrond Myklebust 		.procname	= "tcp_fin_timeout",
14625fe6142STrond Myklebust 		.data		= &xs_tcp_fin_timeout,
14725fe6142STrond Myklebust 		.maxlen		= sizeof(xs_tcp_fin_timeout),
14825fe6142STrond Myklebust 		.mode		= 0644,
1496d456111SEric W. Biederman 		.proc_handler	= proc_dointvec_jiffies,
15025fe6142STrond Myklebust 	},
151f8572d8fSEric W. Biederman 	{ },
152fbf76683SChuck Lever };
153fbf76683SChuck Lever 
154fe2c6338SJoe Perches static struct ctl_table sunrpc_table[] = {
155fbf76683SChuck Lever 	{
156fbf76683SChuck Lever 		.procname	= "sunrpc",
157fbf76683SChuck Lever 		.mode		= 0555,
158fbf76683SChuck Lever 		.child		= xs_tunables_table
159fbf76683SChuck Lever 	},
160f8572d8fSEric W. Biederman 	{ },
161fbf76683SChuck Lever };
162fbf76683SChuck Lever 
163fbf76683SChuck Lever /*
16403bf4b70SChuck Lever  * Wait duration for a reply from the RPC portmapper.
16503bf4b70SChuck Lever  */
16603bf4b70SChuck Lever #define XS_BIND_TO		(60U * HZ)
16703bf4b70SChuck Lever 
16803bf4b70SChuck Lever /*
16903bf4b70SChuck Lever  * Delay if a UDP socket connect error occurs.  This is most likely some
17003bf4b70SChuck Lever  * kind of resource problem on the local host.
17103bf4b70SChuck Lever  */
17203bf4b70SChuck Lever #define XS_UDP_REEST_TO		(2U * HZ)
17303bf4b70SChuck Lever 
17403bf4b70SChuck Lever /*
17503bf4b70SChuck Lever  * The reestablish timeout allows clients to delay for a bit before attempting
17603bf4b70SChuck Lever  * to reconnect to a server that just dropped our connection.
17703bf4b70SChuck Lever  *
17803bf4b70SChuck Lever  * We implement an exponential backoff when trying to reestablish a TCP
17903bf4b70SChuck Lever  * transport connection with the server.  Some servers like to drop a TCP
18003bf4b70SChuck Lever  * connection when they are overworked, so we start with a short timeout and
18103bf4b70SChuck Lever  * increase over time if the server is down or not responding.
18203bf4b70SChuck Lever  */
18303bf4b70SChuck Lever #define XS_TCP_INIT_REEST_TO	(3U * HZ)
18403bf4b70SChuck Lever 
18503bf4b70SChuck Lever /*
18603bf4b70SChuck Lever  * TCP idle timeout; client drops the transport socket if it is idle
18703bf4b70SChuck Lever  * for this long.  Note that we also timeout UDP sockets to prevent
18803bf4b70SChuck Lever  * holding port numbers when there is no RPC traffic.
18903bf4b70SChuck Lever  */
19003bf4b70SChuck Lever #define XS_IDLE_DISC_TO		(5U * 60 * HZ)
19103bf4b70SChuck Lever 
192f895b252SJeff Layton #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
193a246b010SChuck Lever # undef  RPC_DEBUG_DATA
1949903cd1cSChuck Lever # define RPCDBG_FACILITY	RPCDBG_TRANS
195a246b010SChuck Lever #endif
196a246b010SChuck Lever 
197a246b010SChuck Lever #ifdef RPC_DEBUG_DATA
1989903cd1cSChuck Lever static void xs_pktdump(char *msg, u32 *packet, unsigned int count)
199a246b010SChuck Lever {
200a246b010SChuck Lever 	u8 *buf = (u8 *) packet;
201a246b010SChuck Lever 	int j;
202a246b010SChuck Lever 
203a246b010SChuck Lever 	dprintk("RPC:       %s\n", msg);
204a246b010SChuck Lever 	for (j = 0; j < count && j < 128; j += 4) {
205a246b010SChuck Lever 		if (!(j & 31)) {
206a246b010SChuck Lever 			if (j)
207a246b010SChuck Lever 				dprintk("\n");
208a246b010SChuck Lever 			dprintk("0x%04x ", j);
209a246b010SChuck Lever 		}
210a246b010SChuck Lever 		dprintk("%02x%02x%02x%02x ",
211a246b010SChuck Lever 			buf[j], buf[j+1], buf[j+2], buf[j+3]);
212a246b010SChuck Lever 	}
213a246b010SChuck Lever 	dprintk("\n");
214a246b010SChuck Lever }
215a246b010SChuck Lever #else
2169903cd1cSChuck Lever static inline void xs_pktdump(char *msg, u32 *packet, unsigned int count)
217a246b010SChuck Lever {
218a246b010SChuck Lever 	/* NOP */
219a246b010SChuck Lever }
220a246b010SChuck Lever #endif
221a246b010SChuck Lever 
2222118071dSTrond Myklebust static inline struct rpc_xprt *xprt_from_sock(struct sock *sk)
2232118071dSTrond Myklebust {
2242118071dSTrond Myklebust 	return (struct rpc_xprt *) sk->sk_user_data;
2252118071dSTrond Myklebust }
2262118071dSTrond Myklebust 
22795392c59SChuck Lever static inline struct sockaddr *xs_addr(struct rpc_xprt *xprt)
228edb267a6SChuck Lever {
22995392c59SChuck Lever 	return (struct sockaddr *) &xprt->addr;
23095392c59SChuck Lever }
23195392c59SChuck Lever 
232176e21eeSChuck Lever static inline struct sockaddr_un *xs_addr_un(struct rpc_xprt *xprt)
233176e21eeSChuck Lever {
234176e21eeSChuck Lever 	return (struct sockaddr_un *) &xprt->addr;
235176e21eeSChuck Lever }
236176e21eeSChuck Lever 
23795392c59SChuck Lever static inline struct sockaddr_in *xs_addr_in(struct rpc_xprt *xprt)
23895392c59SChuck Lever {
23995392c59SChuck Lever 	return (struct sockaddr_in *) &xprt->addr;
24095392c59SChuck Lever }
24195392c59SChuck Lever 
24295392c59SChuck Lever static inline struct sockaddr_in6 *xs_addr_in6(struct rpc_xprt *xprt)
24395392c59SChuck Lever {
24495392c59SChuck Lever 	return (struct sockaddr_in6 *) &xprt->addr;
24595392c59SChuck Lever }
24695392c59SChuck Lever 
247c877b849SChuck Lever static void xs_format_common_peer_addresses(struct rpc_xprt *xprt)
248c877b849SChuck Lever {
249c877b849SChuck Lever 	struct sockaddr *sap = xs_addr(xprt);
2509dc3b095SChuck Lever 	struct sockaddr_in6 *sin6;
2519dc3b095SChuck Lever 	struct sockaddr_in *sin;
252176e21eeSChuck Lever 	struct sockaddr_un *sun;
253c877b849SChuck Lever 	char buf[128];
254c877b849SChuck Lever 
2559dc3b095SChuck Lever 	switch (sap->sa_family) {
256176e21eeSChuck Lever 	case AF_LOCAL:
257176e21eeSChuck Lever 		sun = xs_addr_un(xprt);
258176e21eeSChuck Lever 		strlcpy(buf, sun->sun_path, sizeof(buf));
259176e21eeSChuck Lever 		xprt->address_strings[RPC_DISPLAY_ADDR] =
260176e21eeSChuck Lever 						kstrdup(buf, GFP_KERNEL);
261176e21eeSChuck Lever 		break;
2629dc3b095SChuck Lever 	case AF_INET:
263176e21eeSChuck Lever 		(void)rpc_ntop(sap, buf, sizeof(buf));
264176e21eeSChuck Lever 		xprt->address_strings[RPC_DISPLAY_ADDR] =
265176e21eeSChuck Lever 						kstrdup(buf, GFP_KERNEL);
2669dc3b095SChuck Lever 		sin = xs_addr_in(xprt);
267fc0b5791SJoe Perches 		snprintf(buf, sizeof(buf), "%08x", ntohl(sin->sin_addr.s_addr));
2689dc3b095SChuck Lever 		break;
2699dc3b095SChuck Lever 	case AF_INET6:
270176e21eeSChuck Lever 		(void)rpc_ntop(sap, buf, sizeof(buf));
271176e21eeSChuck Lever 		xprt->address_strings[RPC_DISPLAY_ADDR] =
272176e21eeSChuck Lever 						kstrdup(buf, GFP_KERNEL);
2739dc3b095SChuck Lever 		sin6 = xs_addr_in6(xprt);
274fc0b5791SJoe Perches 		snprintf(buf, sizeof(buf), "%pi6", &sin6->sin6_addr);
2759dc3b095SChuck Lever 		break;
2769dc3b095SChuck Lever 	default:
2779dc3b095SChuck Lever 		BUG();
2789dc3b095SChuck Lever 	}
279176e21eeSChuck Lever 
2809dc3b095SChuck Lever 	xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = kstrdup(buf, GFP_KERNEL);
2819dc3b095SChuck Lever }
2829dc3b095SChuck Lever 
2839dc3b095SChuck Lever static void xs_format_common_peer_ports(struct rpc_xprt *xprt)
2849dc3b095SChuck Lever {
2859dc3b095SChuck Lever 	struct sockaddr *sap = xs_addr(xprt);
2869dc3b095SChuck Lever 	char buf[128];
2879dc3b095SChuck Lever 
28881160e66SJoe Perches 	snprintf(buf, sizeof(buf), "%u", rpc_get_port(sap));
289c877b849SChuck Lever 	xprt->address_strings[RPC_DISPLAY_PORT] = kstrdup(buf, GFP_KERNEL);
290c877b849SChuck Lever 
29181160e66SJoe Perches 	snprintf(buf, sizeof(buf), "%4hx", rpc_get_port(sap));
292c877b849SChuck Lever 	xprt->address_strings[RPC_DISPLAY_HEX_PORT] = kstrdup(buf, GFP_KERNEL);
293c877b849SChuck Lever }
294c877b849SChuck Lever 
2959dc3b095SChuck Lever static void xs_format_peer_addresses(struct rpc_xprt *xprt,
296b454ae90SChuck Lever 				     const char *protocol,
297b454ae90SChuck Lever 				     const char *netid)
298edb267a6SChuck Lever {
299b454ae90SChuck Lever 	xprt->address_strings[RPC_DISPLAY_PROTO] = protocol;
300b454ae90SChuck Lever 	xprt->address_strings[RPC_DISPLAY_NETID] = netid;
301c877b849SChuck Lever 	xs_format_common_peer_addresses(xprt);
3029dc3b095SChuck Lever 	xs_format_common_peer_ports(xprt);
303edb267a6SChuck Lever }
304edb267a6SChuck Lever 
3059dc3b095SChuck Lever static void xs_update_peer_port(struct rpc_xprt *xprt)
3064b6473fbSChuck Lever {
3079dc3b095SChuck Lever 	kfree(xprt->address_strings[RPC_DISPLAY_HEX_PORT]);
3089dc3b095SChuck Lever 	kfree(xprt->address_strings[RPC_DISPLAY_PORT]);
3094b6473fbSChuck Lever 
3109dc3b095SChuck Lever 	xs_format_common_peer_ports(xprt);
311edb267a6SChuck Lever }
312edb267a6SChuck Lever 
313edb267a6SChuck Lever static void xs_free_peer_addresses(struct rpc_xprt *xprt)
314edb267a6SChuck Lever {
31533e01dc7SChuck Lever 	unsigned int i;
31633e01dc7SChuck Lever 
31733e01dc7SChuck Lever 	for (i = 0; i < RPC_DISPLAY_MAX; i++)
31833e01dc7SChuck Lever 		switch (i) {
31933e01dc7SChuck Lever 		case RPC_DISPLAY_PROTO:
32033e01dc7SChuck Lever 		case RPC_DISPLAY_NETID:
32133e01dc7SChuck Lever 			continue;
32233e01dc7SChuck Lever 		default:
32333e01dc7SChuck Lever 			kfree(xprt->address_strings[i]);
32433e01dc7SChuck Lever 		}
325edb267a6SChuck Lever }
326edb267a6SChuck Lever 
327277e4ab7STrond Myklebust static size_t
328277e4ab7STrond Myklebust xs_alloc_sparse_pages(struct xdr_buf *buf, size_t want, gfp_t gfp)
329277e4ab7STrond Myklebust {
330277e4ab7STrond Myklebust 	size_t i,n;
331277e4ab7STrond Myklebust 
33216e5e90fSTrond Myklebust 	if (!want || !(buf->flags & XDRBUF_SPARSE_PAGES))
333277e4ab7STrond Myklebust 		return want;
334277e4ab7STrond Myklebust 	n = (buf->page_base + want + PAGE_SIZE - 1) >> PAGE_SHIFT;
335277e4ab7STrond Myklebust 	for (i = 0; i < n; i++) {
336277e4ab7STrond Myklebust 		if (buf->pages[i])
337277e4ab7STrond Myklebust 			continue;
338277e4ab7STrond Myklebust 		buf->bvec[i].bv_page = buf->pages[i] = alloc_page(gfp);
339277e4ab7STrond Myklebust 		if (!buf->pages[i]) {
34016e5e90fSTrond Myklebust 			i *= PAGE_SIZE;
34116e5e90fSTrond Myklebust 			return i > buf->page_base ? i - buf->page_base : 0;
342277e4ab7STrond Myklebust 		}
343277e4ab7STrond Myklebust 	}
344277e4ab7STrond Myklebust 	return want;
345277e4ab7STrond Myklebust }
346277e4ab7STrond Myklebust 
347277e4ab7STrond Myklebust static ssize_t
348277e4ab7STrond Myklebust xs_sock_recvmsg(struct socket *sock, struct msghdr *msg, int flags, size_t seek)
349277e4ab7STrond Myklebust {
350277e4ab7STrond Myklebust 	ssize_t ret;
351277e4ab7STrond Myklebust 	if (seek != 0)
352277e4ab7STrond Myklebust 		iov_iter_advance(&msg->msg_iter, seek);
353277e4ab7STrond Myklebust 	ret = sock_recvmsg(sock, msg, flags);
354277e4ab7STrond Myklebust 	return ret > 0 ? ret + seek : ret;
355277e4ab7STrond Myklebust }
356277e4ab7STrond Myklebust 
357277e4ab7STrond Myklebust static ssize_t
358277e4ab7STrond Myklebust xs_read_kvec(struct socket *sock, struct msghdr *msg, int flags,
359277e4ab7STrond Myklebust 		struct kvec *kvec, size_t count, size_t seek)
360277e4ab7STrond Myklebust {
3610e9b4a82SAl Viro 	iov_iter_kvec(&msg->msg_iter, READ, kvec, 1, count);
362277e4ab7STrond Myklebust 	return xs_sock_recvmsg(sock, msg, flags, seek);
363277e4ab7STrond Myklebust }
364277e4ab7STrond Myklebust 
365277e4ab7STrond Myklebust static ssize_t
366277e4ab7STrond Myklebust xs_read_bvec(struct socket *sock, struct msghdr *msg, int flags,
367277e4ab7STrond Myklebust 		struct bio_vec *bvec, unsigned long nr, size_t count,
368277e4ab7STrond Myklebust 		size_t seek)
369277e4ab7STrond Myklebust {
3700e9b4a82SAl Viro 	iov_iter_bvec(&msg->msg_iter, READ, bvec, nr, count);
371277e4ab7STrond Myklebust 	return xs_sock_recvmsg(sock, msg, flags, seek);
372277e4ab7STrond Myklebust }
373277e4ab7STrond Myklebust 
374277e4ab7STrond Myklebust static ssize_t
375277e4ab7STrond Myklebust xs_read_discard(struct socket *sock, struct msghdr *msg, int flags,
376277e4ab7STrond Myklebust 		size_t count)
377277e4ab7STrond Myklebust {
378b76a5afdSTrond Myklebust 	iov_iter_discard(&msg->msg_iter, READ, count);
379b76a5afdSTrond Myklebust 	return sock_recvmsg(sock, msg, flags);
380277e4ab7STrond Myklebust }
381277e4ab7STrond Myklebust 
3826a829eb8STrond Myklebust #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
3836a829eb8STrond Myklebust static void
3846a829eb8STrond Myklebust xs_flush_bvec(const struct bio_vec *bvec, size_t count, size_t seek)
3856a829eb8STrond Myklebust {
3866a829eb8STrond Myklebust 	struct bvec_iter bi = {
3876a829eb8STrond Myklebust 		.bi_size = count,
3886a829eb8STrond Myklebust 	};
3896a829eb8STrond Myklebust 	struct bio_vec bv;
3906a829eb8STrond Myklebust 
3916a829eb8STrond Myklebust 	bvec_iter_advance(bvec, &bi, seek & PAGE_MASK);
3926a829eb8STrond Myklebust 	for_each_bvec(bv, bvec, bi, bi)
3936a829eb8STrond Myklebust 		flush_dcache_page(bv.bv_page);
3946a829eb8STrond Myklebust }
3956a829eb8STrond Myklebust #else
3966a829eb8STrond Myklebust static inline void
3976a829eb8STrond Myklebust xs_flush_bvec(const struct bio_vec *bvec, size_t count, size_t seek)
3986a829eb8STrond Myklebust {
3996a829eb8STrond Myklebust }
4006a829eb8STrond Myklebust #endif
4016a829eb8STrond Myklebust 
402277e4ab7STrond Myklebust static ssize_t
403277e4ab7STrond Myklebust xs_read_xdr_buf(struct socket *sock, struct msghdr *msg, int flags,
404277e4ab7STrond Myklebust 		struct xdr_buf *buf, size_t count, size_t seek, size_t *read)
405277e4ab7STrond Myklebust {
406277e4ab7STrond Myklebust 	size_t want, seek_init = seek, offset = 0;
407277e4ab7STrond Myklebust 	ssize_t ret;
408277e4ab7STrond Myklebust 
409277e4ab7STrond Myklebust 	want = min_t(size_t, count, buf->head[0].iov_len);
410e92053a5STrond Myklebust 	if (seek < want) {
411277e4ab7STrond Myklebust 		ret = xs_read_kvec(sock, msg, flags, &buf->head[0], want, seek);
412277e4ab7STrond Myklebust 		if (ret <= 0)
413277e4ab7STrond Myklebust 			goto sock_err;
414277e4ab7STrond Myklebust 		offset += ret;
415277e4ab7STrond Myklebust 		if (offset == count || msg->msg_flags & (MSG_EOR|MSG_TRUNC))
416277e4ab7STrond Myklebust 			goto out;
417277e4ab7STrond Myklebust 		if (ret != want)
418c4433055STrond Myklebust 			goto out;
419277e4ab7STrond Myklebust 		seek = 0;
420277e4ab7STrond Myklebust 	} else {
421e92053a5STrond Myklebust 		seek -= want;
422e92053a5STrond Myklebust 		offset += want;
423277e4ab7STrond Myklebust 	}
42416e5e90fSTrond Myklebust 
425277e4ab7STrond Myklebust 	want = xs_alloc_sparse_pages(buf,
426277e4ab7STrond Myklebust 			min_t(size_t, count - offset, buf->page_len),
42712a3ad61STrond Myklebust 			GFP_KERNEL);
42816e5e90fSTrond Myklebust 	if (seek < want) {
429277e4ab7STrond Myklebust 		ret = xs_read_bvec(sock, msg, flags, buf->bvec,
430277e4ab7STrond Myklebust 				xdr_buf_pagecount(buf),
431277e4ab7STrond Myklebust 				want + buf->page_base,
432277e4ab7STrond Myklebust 				seek + buf->page_base);
433277e4ab7STrond Myklebust 		if (ret <= 0)
434277e4ab7STrond Myklebust 			goto sock_err;
4356a829eb8STrond Myklebust 		xs_flush_bvec(buf->bvec, ret, seek + buf->page_base);
436277e4ab7STrond Myklebust 		offset += ret - buf->page_base;
437277e4ab7STrond Myklebust 		if (offset == count || msg->msg_flags & (MSG_EOR|MSG_TRUNC))
438277e4ab7STrond Myklebust 			goto out;
439277e4ab7STrond Myklebust 		if (ret != want)
440c4433055STrond Myklebust 			goto out;
441277e4ab7STrond Myklebust 		seek = 0;
442277e4ab7STrond Myklebust 	} else {
44316e5e90fSTrond Myklebust 		seek -= want;
44416e5e90fSTrond Myklebust 		offset += want;
445277e4ab7STrond Myklebust 	}
44616e5e90fSTrond Myklebust 
447277e4ab7STrond Myklebust 	want = min_t(size_t, count - offset, buf->tail[0].iov_len);
448e92053a5STrond Myklebust 	if (seek < want) {
449277e4ab7STrond Myklebust 		ret = xs_read_kvec(sock, msg, flags, &buf->tail[0], want, seek);
450277e4ab7STrond Myklebust 		if (ret <= 0)
451277e4ab7STrond Myklebust 			goto sock_err;
452277e4ab7STrond Myklebust 		offset += ret;
453277e4ab7STrond Myklebust 		if (offset == count || msg->msg_flags & (MSG_EOR|MSG_TRUNC))
454277e4ab7STrond Myklebust 			goto out;
455277e4ab7STrond Myklebust 		if (ret != want)
456c4433055STrond Myklebust 			goto out;
4579734ad57STrond Myklebust 	} else if (offset < seek_init)
458e92053a5STrond Myklebust 		offset = seek_init;
459277e4ab7STrond Myklebust 	ret = -EMSGSIZE;
460277e4ab7STrond Myklebust out:
461277e4ab7STrond Myklebust 	*read = offset - seek_init;
462277e4ab7STrond Myklebust 	return ret;
463277e4ab7STrond Myklebust sock_err:
464277e4ab7STrond Myklebust 	offset += seek;
465277e4ab7STrond Myklebust 	goto out;
466277e4ab7STrond Myklebust }
467277e4ab7STrond Myklebust 
468277e4ab7STrond Myklebust static void
469277e4ab7STrond Myklebust xs_read_header(struct sock_xprt *transport, struct xdr_buf *buf)
470277e4ab7STrond Myklebust {
471277e4ab7STrond Myklebust 	if (!transport->recv.copied) {
472277e4ab7STrond Myklebust 		if (buf->head[0].iov_len >= transport->recv.offset)
473277e4ab7STrond Myklebust 			memcpy(buf->head[0].iov_base,
474277e4ab7STrond Myklebust 					&transport->recv.xid,
475277e4ab7STrond Myklebust 					transport->recv.offset);
476277e4ab7STrond Myklebust 		transport->recv.copied = transport->recv.offset;
477277e4ab7STrond Myklebust 	}
478277e4ab7STrond Myklebust }
479277e4ab7STrond Myklebust 
480277e4ab7STrond Myklebust static bool
481277e4ab7STrond Myklebust xs_read_stream_request_done(struct sock_xprt *transport)
482277e4ab7STrond Myklebust {
483277e4ab7STrond Myklebust 	return transport->recv.fraghdr & cpu_to_be32(RPC_LAST_STREAM_FRAGMENT);
484277e4ab7STrond Myklebust }
485277e4ab7STrond Myklebust 
486e92053a5STrond Myklebust static void
487e92053a5STrond Myklebust xs_read_stream_check_eor(struct sock_xprt *transport,
488e92053a5STrond Myklebust 		struct msghdr *msg)
489e92053a5STrond Myklebust {
490e92053a5STrond Myklebust 	if (xs_read_stream_request_done(transport))
491e92053a5STrond Myklebust 		msg->msg_flags |= MSG_EOR;
492e92053a5STrond Myklebust }
493e92053a5STrond Myklebust 
494277e4ab7STrond Myklebust static ssize_t
495277e4ab7STrond Myklebust xs_read_stream_request(struct sock_xprt *transport, struct msghdr *msg,
496277e4ab7STrond Myklebust 		int flags, struct rpc_rqst *req)
497277e4ab7STrond Myklebust {
498277e4ab7STrond Myklebust 	struct xdr_buf *buf = &req->rq_private_buf;
49901f2f5b8SAlakesh Haloi 	size_t want, uninitialized_var(read);
50001f2f5b8SAlakesh Haloi 	ssize_t uninitialized_var(ret);
501277e4ab7STrond Myklebust 
502277e4ab7STrond Myklebust 	xs_read_header(transport, buf);
503277e4ab7STrond Myklebust 
504277e4ab7STrond Myklebust 	want = transport->recv.len - transport->recv.offset;
505e92053a5STrond Myklebust 	if (want != 0) {
506277e4ab7STrond Myklebust 		ret = xs_read_xdr_buf(transport->sock, msg, flags, buf,
507e92053a5STrond Myklebust 				transport->recv.copied + want,
508e92053a5STrond Myklebust 				transport->recv.copied,
509277e4ab7STrond Myklebust 				&read);
510277e4ab7STrond Myklebust 		transport->recv.offset += read;
511277e4ab7STrond Myklebust 		transport->recv.copied += read;
512277e4ab7STrond Myklebust 	}
513277e4ab7STrond Myklebust 
514727fcc64STrond Myklebust 	if (transport->recv.offset == transport->recv.len)
515727fcc64STrond Myklebust 		xs_read_stream_check_eor(transport, msg);
516727fcc64STrond Myklebust 
517e92053a5STrond Myklebust 	if (want == 0)
518e92053a5STrond Myklebust 		return 0;
519e92053a5STrond Myklebust 
520277e4ab7STrond Myklebust 	switch (ret) {
521c4433055STrond Myklebust 	default:
522c4433055STrond Myklebust 		break;
52326781eabSTrond Myklebust 	case -EFAULT:
524277e4ab7STrond Myklebust 	case -EMSGSIZE:
52526781eabSTrond Myklebust 		msg->msg_flags |= MSG_TRUNC;
526c4433055STrond Myklebust 		return read;
527277e4ab7STrond Myklebust 	case 0:
528277e4ab7STrond Myklebust 		return -ESHUTDOWN;
529277e4ab7STrond Myklebust 	}
530c4433055STrond Myklebust 	return ret < 0 ? ret : read;
531277e4ab7STrond Myklebust }
532277e4ab7STrond Myklebust 
533277e4ab7STrond Myklebust static size_t
534277e4ab7STrond Myklebust xs_read_stream_headersize(bool isfrag)
535277e4ab7STrond Myklebust {
536277e4ab7STrond Myklebust 	if (isfrag)
537277e4ab7STrond Myklebust 		return sizeof(__be32);
538277e4ab7STrond Myklebust 	return 3 * sizeof(__be32);
539277e4ab7STrond Myklebust }
540277e4ab7STrond Myklebust 
541277e4ab7STrond Myklebust static ssize_t
542277e4ab7STrond Myklebust xs_read_stream_header(struct sock_xprt *transport, struct msghdr *msg,
543277e4ab7STrond Myklebust 		int flags, size_t want, size_t seek)
544277e4ab7STrond Myklebust {
545277e4ab7STrond Myklebust 	struct kvec kvec = {
546277e4ab7STrond Myklebust 		.iov_base = &transport->recv.fraghdr,
547277e4ab7STrond Myklebust 		.iov_len = want,
548277e4ab7STrond Myklebust 	};
549277e4ab7STrond Myklebust 	return xs_read_kvec(transport->sock, msg, flags, &kvec, want, seek);
550277e4ab7STrond Myklebust }
551277e4ab7STrond Myklebust 
552277e4ab7STrond Myklebust #if defined(CONFIG_SUNRPC_BACKCHANNEL)
553277e4ab7STrond Myklebust static ssize_t
554277e4ab7STrond Myklebust xs_read_stream_call(struct sock_xprt *transport, struct msghdr *msg, int flags)
555277e4ab7STrond Myklebust {
556277e4ab7STrond Myklebust 	struct rpc_xprt *xprt = &transport->xprt;
557277e4ab7STrond Myklebust 	struct rpc_rqst *req;
558277e4ab7STrond Myklebust 	ssize_t ret;
559277e4ab7STrond Myklebust 
560277e4ab7STrond Myklebust 	/* Look up and lock the request corresponding to the given XID */
561277e4ab7STrond Myklebust 	req = xprt_lookup_bc_request(xprt, transport->recv.xid);
562277e4ab7STrond Myklebust 	if (!req) {
563277e4ab7STrond Myklebust 		printk(KERN_WARNING "Callback slot table overflowed\n");
564277e4ab7STrond Myklebust 		return -ESHUTDOWN;
565277e4ab7STrond Myklebust 	}
56645835a63STrond Myklebust 	if (transport->recv.copied && !req->rq_private_buf.len)
56745835a63STrond Myklebust 		return -ESHUTDOWN;
568277e4ab7STrond Myklebust 
569277e4ab7STrond Myklebust 	ret = xs_read_stream_request(transport, msg, flags, req);
570277e4ab7STrond Myklebust 	if (msg->msg_flags & (MSG_EOR|MSG_TRUNC))
571c4433055STrond Myklebust 		xprt_complete_bc_request(req, transport->recv.copied);
57245835a63STrond Myklebust 	else
57345835a63STrond Myklebust 		req->rq_private_buf.len = transport->recv.copied;
574277e4ab7STrond Myklebust 
575277e4ab7STrond Myklebust 	return ret;
576277e4ab7STrond Myklebust }
577277e4ab7STrond Myklebust #else /* CONFIG_SUNRPC_BACKCHANNEL */
578277e4ab7STrond Myklebust static ssize_t
579277e4ab7STrond Myklebust xs_read_stream_call(struct sock_xprt *transport, struct msghdr *msg, int flags)
580277e4ab7STrond Myklebust {
581277e4ab7STrond Myklebust 	return -ESHUTDOWN;
582277e4ab7STrond Myklebust }
583277e4ab7STrond Myklebust #endif /* CONFIG_SUNRPC_BACKCHANNEL */
584277e4ab7STrond Myklebust 
585277e4ab7STrond Myklebust static ssize_t
586277e4ab7STrond Myklebust xs_read_stream_reply(struct sock_xprt *transport, struct msghdr *msg, int flags)
587277e4ab7STrond Myklebust {
588277e4ab7STrond Myklebust 	struct rpc_xprt *xprt = &transport->xprt;
589277e4ab7STrond Myklebust 	struct rpc_rqst *req;
590277e4ab7STrond Myklebust 	ssize_t ret = 0;
591277e4ab7STrond Myklebust 
592277e4ab7STrond Myklebust 	/* Look up and lock the request corresponding to the given XID */
593277e4ab7STrond Myklebust 	spin_lock(&xprt->queue_lock);
594277e4ab7STrond Myklebust 	req = xprt_lookup_rqst(xprt, transport->recv.xid);
59545835a63STrond Myklebust 	if (!req || (transport->recv.copied && !req->rq_private_buf.len)) {
596277e4ab7STrond Myklebust 		msg->msg_flags |= MSG_TRUNC;
597277e4ab7STrond Myklebust 		goto out;
598277e4ab7STrond Myklebust 	}
599277e4ab7STrond Myklebust 	xprt_pin_rqst(req);
600277e4ab7STrond Myklebust 	spin_unlock(&xprt->queue_lock);
601277e4ab7STrond Myklebust 
602277e4ab7STrond Myklebust 	ret = xs_read_stream_request(transport, msg, flags, req);
603277e4ab7STrond Myklebust 
604277e4ab7STrond Myklebust 	spin_lock(&xprt->queue_lock);
605277e4ab7STrond Myklebust 	if (msg->msg_flags & (MSG_EOR|MSG_TRUNC))
606c4433055STrond Myklebust 		xprt_complete_rqst(req->rq_task, transport->recv.copied);
60745835a63STrond Myklebust 	else
60845835a63STrond Myklebust 		req->rq_private_buf.len = transport->recv.copied;
609277e4ab7STrond Myklebust 	xprt_unpin_rqst(req);
610277e4ab7STrond Myklebust out:
611277e4ab7STrond Myklebust 	spin_unlock(&xprt->queue_lock);
612277e4ab7STrond Myklebust 	return ret;
613277e4ab7STrond Myklebust }
614277e4ab7STrond Myklebust 
615277e4ab7STrond Myklebust static ssize_t
616277e4ab7STrond Myklebust xs_read_stream(struct sock_xprt *transport, int flags)
617277e4ab7STrond Myklebust {
618277e4ab7STrond Myklebust 	struct msghdr msg = { 0 };
619277e4ab7STrond Myklebust 	size_t want, read = 0;
620277e4ab7STrond Myklebust 	ssize_t ret = 0;
621277e4ab7STrond Myklebust 
622277e4ab7STrond Myklebust 	if (transport->recv.len == 0) {
623277e4ab7STrond Myklebust 		want = xs_read_stream_headersize(transport->recv.copied != 0);
624277e4ab7STrond Myklebust 		ret = xs_read_stream_header(transport, &msg, flags, want,
625277e4ab7STrond Myklebust 				transport->recv.offset);
626277e4ab7STrond Myklebust 		if (ret <= 0)
627277e4ab7STrond Myklebust 			goto out_err;
628277e4ab7STrond Myklebust 		transport->recv.offset = ret;
629c4433055STrond Myklebust 		if (transport->recv.offset != want)
630c4433055STrond Myklebust 			return transport->recv.offset;
631277e4ab7STrond Myklebust 		transport->recv.len = be32_to_cpu(transport->recv.fraghdr) &
632277e4ab7STrond Myklebust 			RPC_FRAGMENT_SIZE_MASK;
633277e4ab7STrond Myklebust 		transport->recv.offset -= sizeof(transport->recv.fraghdr);
634277e4ab7STrond Myklebust 		read = ret;
635277e4ab7STrond Myklebust 	}
636277e4ab7STrond Myklebust 
637277e4ab7STrond Myklebust 	switch (be32_to_cpu(transport->recv.calldir)) {
638c4433055STrond Myklebust 	default:
639c4433055STrond Myklebust 		msg.msg_flags |= MSG_TRUNC;
640c4433055STrond Myklebust 		break;
641277e4ab7STrond Myklebust 	case RPC_CALL:
642277e4ab7STrond Myklebust 		ret = xs_read_stream_call(transport, &msg, flags);
643277e4ab7STrond Myklebust 		break;
644277e4ab7STrond Myklebust 	case RPC_REPLY:
645277e4ab7STrond Myklebust 		ret = xs_read_stream_reply(transport, &msg, flags);
646277e4ab7STrond Myklebust 	}
647277e4ab7STrond Myklebust 	if (msg.msg_flags & MSG_TRUNC) {
648277e4ab7STrond Myklebust 		transport->recv.calldir = cpu_to_be32(-1);
649277e4ab7STrond Myklebust 		transport->recv.copied = -1;
650277e4ab7STrond Myklebust 	}
651277e4ab7STrond Myklebust 	if (ret < 0)
652277e4ab7STrond Myklebust 		goto out_err;
653277e4ab7STrond Myklebust 	read += ret;
654277e4ab7STrond Myklebust 	if (transport->recv.offset < transport->recv.len) {
655c4433055STrond Myklebust 		if (!(msg.msg_flags & MSG_TRUNC))
656c4433055STrond Myklebust 			return read;
657b76a5afdSTrond Myklebust 		msg.msg_flags = 0;
658277e4ab7STrond Myklebust 		ret = xs_read_discard(transport->sock, &msg, flags,
659277e4ab7STrond Myklebust 				transport->recv.len - transport->recv.offset);
660277e4ab7STrond Myklebust 		if (ret <= 0)
661277e4ab7STrond Myklebust 			goto out_err;
662277e4ab7STrond Myklebust 		transport->recv.offset += ret;
663277e4ab7STrond Myklebust 		read += ret;
664277e4ab7STrond Myklebust 		if (transport->recv.offset != transport->recv.len)
665c4433055STrond Myklebust 			return read;
666277e4ab7STrond Myklebust 	}
667277e4ab7STrond Myklebust 	if (xs_read_stream_request_done(transport)) {
668c50b8ee0STrond Myklebust 		trace_xs_stream_read_request(transport);
669277e4ab7STrond Myklebust 		transport->recv.copied = 0;
670277e4ab7STrond Myklebust 	}
671277e4ab7STrond Myklebust 	transport->recv.offset = 0;
672277e4ab7STrond Myklebust 	transport->recv.len = 0;
673277e4ab7STrond Myklebust 	return read;
674277e4ab7STrond Myklebust out_err:
67579462857STrond Myklebust 	return ret != 0 ? ret : -ESHUTDOWN;
676277e4ab7STrond Myklebust }
677277e4ab7STrond Myklebust 
6780ffe86f4STrond Myklebust static __poll_t xs_poll_socket(struct sock_xprt *transport)
6790ffe86f4STrond Myklebust {
680a73881c9STrond Myklebust 	return transport->sock->ops->poll(transport->file, transport->sock,
681a73881c9STrond Myklebust 			NULL);
6820ffe86f4STrond Myklebust }
6830ffe86f4STrond Myklebust 
6840ffe86f4STrond Myklebust static bool xs_poll_socket_readable(struct sock_xprt *transport)
6850ffe86f4STrond Myklebust {
6860ffe86f4STrond Myklebust 	__poll_t events = xs_poll_socket(transport);
6870ffe86f4STrond Myklebust 
6880ffe86f4STrond Myklebust 	return (events & (EPOLLIN | EPOLLRDNORM)) && !(events & EPOLLRDHUP);
6890ffe86f4STrond Myklebust }
6900ffe86f4STrond Myklebust 
6910ffe86f4STrond Myklebust static void xs_poll_check_readable(struct sock_xprt *transport)
6920ffe86f4STrond Myklebust {
6930ffe86f4STrond Myklebust 
6940ffe86f4STrond Myklebust 	clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state);
6950ffe86f4STrond Myklebust 	if (!xs_poll_socket_readable(transport))
6960ffe86f4STrond Myklebust 		return;
6970ffe86f4STrond Myklebust 	if (!test_and_set_bit(XPRT_SOCK_DATA_READY, &transport->sock_state))
6980ffe86f4STrond Myklebust 		queue_work(xprtiod_workqueue, &transport->recv_worker);
6990ffe86f4STrond Myklebust }
7000ffe86f4STrond Myklebust 
701c50b8ee0STrond Myklebust static void xs_stream_data_receive(struct sock_xprt *transport)
702c50b8ee0STrond Myklebust {
703c50b8ee0STrond Myklebust 	size_t read = 0;
704c50b8ee0STrond Myklebust 	ssize_t ret = 0;
705c50b8ee0STrond Myklebust 
706c50b8ee0STrond Myklebust 	mutex_lock(&transport->recv_mutex);
707c50b8ee0STrond Myklebust 	if (transport->sock == NULL)
708c50b8ee0STrond Myklebust 		goto out;
709c50b8ee0STrond Myklebust 	for (;;) {
710c50b8ee0STrond Myklebust 		ret = xs_read_stream(transport, MSG_DONTWAIT);
711c4433055STrond Myklebust 		if (ret < 0)
712c50b8ee0STrond Myklebust 			break;
713c50b8ee0STrond Myklebust 		read += ret;
714c50b8ee0STrond Myklebust 		cond_resched();
715c50b8ee0STrond Myklebust 	}
7165f52a9d4STrond Myklebust 	if (ret == -ESHUTDOWN)
7175f52a9d4STrond Myklebust 		kernel_sock_shutdown(transport->sock, SHUT_RDWR);
7185f52a9d4STrond Myklebust 	else
7190ffe86f4STrond Myklebust 		xs_poll_check_readable(transport);
720c50b8ee0STrond Myklebust out:
721c50b8ee0STrond Myklebust 	mutex_unlock(&transport->recv_mutex);
722c50b8ee0STrond Myklebust 	trace_xs_stream_read_data(&transport->xprt, ret, read);
723c50b8ee0STrond Myklebust }
724c50b8ee0STrond Myklebust 
725c50b8ee0STrond Myklebust static void xs_stream_data_receive_workfn(struct work_struct *work)
726c50b8ee0STrond Myklebust {
727c50b8ee0STrond Myklebust 	struct sock_xprt *transport =
728c50b8ee0STrond Myklebust 		container_of(work, struct sock_xprt, recv_worker);
729a1231fdaSTrond Myklebust 	unsigned int pflags = memalloc_nofs_save();
730a1231fdaSTrond Myklebust 
731c50b8ee0STrond Myklebust 	xs_stream_data_receive(transport);
732a1231fdaSTrond Myklebust 	memalloc_nofs_restore(pflags);
733c50b8ee0STrond Myklebust }
734c50b8ee0STrond Myklebust 
735550aebfeSTrond Myklebust static void
736550aebfeSTrond Myklebust xs_stream_reset_connect(struct sock_xprt *transport)
737550aebfeSTrond Myklebust {
738550aebfeSTrond Myklebust 	transport->recv.offset = 0;
739550aebfeSTrond Myklebust 	transport->recv.len = 0;
740550aebfeSTrond Myklebust 	transport->recv.copied = 0;
741550aebfeSTrond Myklebust 	transport->xmit.offset = 0;
742ae053551STrond Myklebust }
743ae053551STrond Myklebust 
744ae053551STrond Myklebust static void
745ae053551STrond Myklebust xs_stream_start_connect(struct sock_xprt *transport)
746ae053551STrond Myklebust {
747550aebfeSTrond Myklebust 	transport->xprt.stat.connect_count++;
748550aebfeSTrond Myklebust 	transport->xprt.stat.connect_start = jiffies;
749550aebfeSTrond Myklebust }
750550aebfeSTrond Myklebust 
751b4b5cc85SChuck Lever #define XS_SENDMSG_FLAGS	(MSG_DONTWAIT | MSG_NOSIGNAL)
752b4b5cc85SChuck Lever 
7539903cd1cSChuck Lever /**
754c544577dSTrond Myklebust  * xs_nospace - handle transmit was incomplete
75550f484e2STrond Myklebust  * @req: pointer to RPC request
7569903cd1cSChuck Lever  *
757a246b010SChuck Lever  */
758c544577dSTrond Myklebust static int xs_nospace(struct rpc_rqst *req)
759a246b010SChuck Lever {
760262965f5SChuck Lever 	struct rpc_xprt *xprt = req->rq_xprt;
761ee0ac0c2SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
76206ea0bfeSTrond Myklebust 	struct sock *sk = transport->inet;
76324ca9a84STrond Myklebust 	int ret = -EAGAIN;
764a246b010SChuck Lever 
76546121cf7SChuck Lever 	dprintk("RPC: %5u xmit incomplete (%u left of %u)\n",
766c544577dSTrond Myklebust 			req->rq_task->tk_pid,
767c544577dSTrond Myklebust 			req->rq_slen - transport->xmit.offset,
768262965f5SChuck Lever 			req->rq_slen);
769a246b010SChuck Lever 
770262965f5SChuck Lever 	/* Protect against races with write_space */
771b5e92419STrond Myklebust 	spin_lock(&xprt->transport_lock);
772a246b010SChuck Lever 
773262965f5SChuck Lever 	/* Don't race with disconnect */
774b6ddf64fSTrond Myklebust 	if (xprt_connected(xprt)) {
77513331a55STrond Myklebust 		/* wait for more buffer space */
77606ea0bfeSTrond Myklebust 		sk->sk_write_pending++;
777c544577dSTrond Myklebust 		xprt_wait_for_buffer_space(xprt);
77813331a55STrond Myklebust 	} else
7795e3771ceSTrond Myklebust 		ret = -ENOTCONN;
780a246b010SChuck Lever 
781b5e92419STrond Myklebust 	spin_unlock(&xprt->transport_lock);
78206ea0bfeSTrond Myklebust 
78306ea0bfeSTrond Myklebust 	/* Race breaker in case memory is freed before above code is called */
784d48f9ce7SDavid Vrabel 	if (ret == -EAGAIN) {
785d48f9ce7SDavid Vrabel 		struct socket_wq *wq;
786d48f9ce7SDavid Vrabel 
787d48f9ce7SDavid Vrabel 		rcu_read_lock();
788d48f9ce7SDavid Vrabel 		wq = rcu_dereference(sk->sk_wq);
789d48f9ce7SDavid Vrabel 		set_bit(SOCKWQ_ASYNC_NOSPACE, &wq->flags);
790d48f9ce7SDavid Vrabel 		rcu_read_unlock();
791d48f9ce7SDavid Vrabel 
79206ea0bfeSTrond Myklebust 		sk->sk_write_space(sk);
793d48f9ce7SDavid Vrabel 	}
7945e3771ceSTrond Myklebust 	return ret;
795a246b010SChuck Lever }
796a246b010SChuck Lever 
797277e4ab7STrond Myklebust static void
798277e4ab7STrond Myklebust xs_stream_prepare_request(struct rpc_rqst *req)
799277e4ab7STrond Myklebust {
80075369089STrond Myklebust 	xdr_free_bvec(&req->rq_rcv_buf);
80112a3ad61STrond Myklebust 	req->rq_task->tk_status = xdr_alloc_bvec(&req->rq_rcv_buf, GFP_KERNEL);
802277e4ab7STrond Myklebust }
803277e4ab7STrond Myklebust 
80461677eeeSChuck Lever /*
8054cd34e7cSTrond Myklebust  * Determine if the previous message in the stream was aborted before it
8064cd34e7cSTrond Myklebust  * could complete transmission.
8074cd34e7cSTrond Myklebust  */
8084cd34e7cSTrond Myklebust static bool
8094cd34e7cSTrond Myklebust xs_send_request_was_aborted(struct sock_xprt *transport, struct rpc_rqst *req)
8104cd34e7cSTrond Myklebust {
8114cd34e7cSTrond Myklebust 	return transport->xmit.offset != 0 && req->rq_bytes_sent == 0;
8124cd34e7cSTrond Myklebust }
8134cd34e7cSTrond Myklebust 
8144cd34e7cSTrond Myklebust /*
81506b5fc3aSTrond Myklebust  * Return the stream record marker field for a record of length < 2^31-1
81661677eeeSChuck Lever  */
81706b5fc3aSTrond Myklebust static rpc_fraghdr
81806b5fc3aSTrond Myklebust xs_stream_record_marker(struct xdr_buf *xdr)
81961677eeeSChuck Lever {
82006b5fc3aSTrond Myklebust 	if (!xdr->len)
82106b5fc3aSTrond Myklebust 		return 0;
82206b5fc3aSTrond Myklebust 	return cpu_to_be32(RPC_LAST_STREAM_FRAGMENT | (u32)xdr->len);
82361677eeeSChuck Lever }
82461677eeeSChuck Lever 
8259903cd1cSChuck Lever /**
826176e21eeSChuck Lever  * xs_local_send_request - write an RPC request to an AF_LOCAL socket
82750f484e2STrond Myklebust  * @req: pointer to RPC request
828176e21eeSChuck Lever  *
829176e21eeSChuck Lever  * Return values:
830176e21eeSChuck Lever  *        0:	The request has been sent
831176e21eeSChuck Lever  *   EAGAIN:	The socket was blocked, please call again later to
832176e21eeSChuck Lever  *		complete the request
833176e21eeSChuck Lever  * ENOTCONN:	Caller needs to invoke connect logic then call again
834176e21eeSChuck Lever  *    other:	Some other error occured, the request was not sent
835176e21eeSChuck Lever  */
836adfa7144STrond Myklebust static int xs_local_send_request(struct rpc_rqst *req)
837176e21eeSChuck Lever {
838176e21eeSChuck Lever 	struct rpc_xprt *xprt = req->rq_xprt;
839176e21eeSChuck Lever 	struct sock_xprt *transport =
840176e21eeSChuck Lever 				container_of(xprt, struct sock_xprt, xprt);
841176e21eeSChuck Lever 	struct xdr_buf *xdr = &req->rq_snd_buf;
8427e3d3620STrond Myklebust 	rpc_fraghdr rm = xs_stream_record_marker(xdr);
8437e3d3620STrond Myklebust 	unsigned int msglen = rm ? req->rq_slen + sizeof(rm) : req->rq_slen;
8449e55eef4SChuck Lever 	struct msghdr msg = {
8459e55eef4SChuck Lever 		.msg_flags	= XS_SENDMSG_FLAGS,
8469e55eef4SChuck Lever 	};
8479e55eef4SChuck Lever 	unsigned int uninitialized_var(sent);
848176e21eeSChuck Lever 	int status;
849176e21eeSChuck Lever 
8504cd34e7cSTrond Myklebust 	/* Close the stream if the previous transmission was incomplete */
8514cd34e7cSTrond Myklebust 	if (xs_send_request_was_aborted(transport, req)) {
8524cd34e7cSTrond Myklebust 		xs_close(xprt);
8534cd34e7cSTrond Myklebust 		return -ENOTCONN;
8544cd34e7cSTrond Myklebust 	}
8554cd34e7cSTrond Myklebust 
856176e21eeSChuck Lever 	xs_pktdump("packet data:",
857176e21eeSChuck Lever 			req->rq_svec->iov_base, req->rq_svec->iov_len);
858176e21eeSChuck Lever 
85978215759SChuck Lever 	req->rq_xtime = ktime_get();
8609e55eef4SChuck Lever 	status = xprt_sock_sendmsg(transport->sock, &msg, xdr,
8617e3d3620STrond Myklebust 				   transport->xmit.offset, rm, &sent);
862176e21eeSChuck Lever 	dprintk("RPC:       %s(%u) = %d\n",
8636c7a64e5STrond Myklebust 			__func__, xdr->len - transport->xmit.offset, status);
864743c69e7SNeilBrown 
865743c69e7SNeilBrown 	if (status == -EAGAIN && sock_writeable(transport->inet))
866743c69e7SNeilBrown 		status = -ENOBUFS;
867743c69e7SNeilBrown 
868f279cd00SJason Baron 	if (likely(sent > 0) || status == 0) {
8696c7a64e5STrond Myklebust 		transport->xmit.offset += sent;
8706c7a64e5STrond Myklebust 		req->rq_bytes_sent = transport->xmit.offset;
8717e3d3620STrond Myklebust 		if (likely(req->rq_bytes_sent >= msglen)) {
8726c7a64e5STrond Myklebust 			req->rq_xmit_bytes_sent += transport->xmit.offset;
8736c7a64e5STrond Myklebust 			transport->xmit.offset = 0;
874176e21eeSChuck Lever 			return 0;
875176e21eeSChuck Lever 		}
876176e21eeSChuck Lever 		status = -EAGAIN;
877176e21eeSChuck Lever 	}
878176e21eeSChuck Lever 
879176e21eeSChuck Lever 	switch (status) {
8803601c4a9STrond Myklebust 	case -ENOBUFS:
881b5872f0cSTrond Myklebust 		break;
882176e21eeSChuck Lever 	case -EAGAIN:
883c544577dSTrond Myklebust 		status = xs_nospace(req);
884176e21eeSChuck Lever 		break;
885176e21eeSChuck Lever 	default:
886176e21eeSChuck Lever 		dprintk("RPC:       sendmsg returned unrecognized error %d\n",
887176e21eeSChuck Lever 			-status);
888e9d47639SGustavo A. R. Silva 		/* fall through */
889176e21eeSChuck Lever 	case -EPIPE:
890176e21eeSChuck Lever 		xs_close(xprt);
891176e21eeSChuck Lever 		status = -ENOTCONN;
892176e21eeSChuck Lever 	}
893176e21eeSChuck Lever 
894176e21eeSChuck Lever 	return status;
895176e21eeSChuck Lever }
896176e21eeSChuck Lever 
897176e21eeSChuck Lever /**
898262965f5SChuck Lever  * xs_udp_send_request - write an RPC request to a UDP socket
89950f484e2STrond Myklebust  * @req: pointer to RPC request
9009903cd1cSChuck Lever  *
9019903cd1cSChuck Lever  * Return values:
9029903cd1cSChuck Lever  *        0:	The request has been sent
9039903cd1cSChuck Lever  *   EAGAIN:	The socket was blocked, please call again later to
9049903cd1cSChuck Lever  *		complete the request
905262965f5SChuck Lever  * ENOTCONN:	Caller needs to invoke connect logic then call again
90625985edcSLucas De Marchi  *    other:	Some other error occurred, the request was not sent
9079903cd1cSChuck Lever  */
908adfa7144STrond Myklebust static int xs_udp_send_request(struct rpc_rqst *req)
909a246b010SChuck Lever {
910a246b010SChuck Lever 	struct rpc_xprt *xprt = req->rq_xprt;
911ee0ac0c2SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
912262965f5SChuck Lever 	struct xdr_buf *xdr = &req->rq_snd_buf;
9139e55eef4SChuck Lever 	struct msghdr msg = {
9149e55eef4SChuck Lever 		.msg_name	= xs_addr(xprt),
9159e55eef4SChuck Lever 		.msg_namelen	= xprt->addrlen,
9169e55eef4SChuck Lever 		.msg_flags	= XS_SENDMSG_FLAGS,
9179e55eef4SChuck Lever 	};
9189e55eef4SChuck Lever 	unsigned int uninitialized_var(sent);
919262965f5SChuck Lever 	int status;
920262965f5SChuck Lever 
921262965f5SChuck Lever 	xs_pktdump("packet data:",
922262965f5SChuck Lever 				req->rq_svec->iov_base,
923262965f5SChuck Lever 				req->rq_svec->iov_len);
924262965f5SChuck Lever 
92501d37c42STrond Myklebust 	if (!xprt_bound(xprt))
92601d37c42STrond Myklebust 		return -ENOTCONN;
92775891f50STrond Myklebust 
92875891f50STrond Myklebust 	if (!xprt_request_get_cong(xprt, req))
92975891f50STrond Myklebust 		return -EBADSLT;
93075891f50STrond Myklebust 
93178215759SChuck Lever 	req->rq_xtime = ktime_get();
9329e55eef4SChuck Lever 	status = xprt_sock_sendmsg(transport->sock, &msg, xdr, 0, 0, &sent);
933262965f5SChuck Lever 
934262965f5SChuck Lever 	dprintk("RPC:       xs_udp_send_request(%u) = %d\n",
9356c7a64e5STrond Myklebust 			xdr->len, status);
936262965f5SChuck Lever 
9373dedbb5cSJason Baron 	/* firewall is blocking us, don't return -EAGAIN or we end up looping */
9383dedbb5cSJason Baron 	if (status == -EPERM)
9393dedbb5cSJason Baron 		goto process_status;
9403dedbb5cSJason Baron 
941743c69e7SNeilBrown 	if (status == -EAGAIN && sock_writeable(transport->inet))
942743c69e7SNeilBrown 		status = -ENOBUFS;
943743c69e7SNeilBrown 
944f279cd00SJason Baron 	if (sent > 0 || status == 0) {
945f279cd00SJason Baron 		req->rq_xmit_bytes_sent += sent;
946f279cd00SJason Baron 		if (sent >= req->rq_slen)
947262965f5SChuck Lever 			return 0;
948262965f5SChuck Lever 		/* Still some bytes left; set up for a retry later. */
949262965f5SChuck Lever 		status = -EAGAIN;
9502199700fSTrond Myklebust 	}
951262965f5SChuck Lever 
9523dedbb5cSJason Baron process_status:
953262965f5SChuck Lever 	switch (status) {
954fba91afbSTrond Myklebust 	case -ENOTSOCK:
955fba91afbSTrond Myklebust 		status = -ENOTCONN;
956fba91afbSTrond Myklebust 		/* Should we call xs_close() here? */
957fba91afbSTrond Myklebust 		break;
958b6ddf64fSTrond Myklebust 	case -EAGAIN:
959c544577dSTrond Myklebust 		status = xs_nospace(req);
960b6ddf64fSTrond Myklebust 		break;
961262965f5SChuck Lever 	case -ENETUNREACH:
9623601c4a9STrond Myklebust 	case -ENOBUFS:
963262965f5SChuck Lever 	case -EPIPE:
964262965f5SChuck Lever 	case -ECONNREFUSED:
9653dedbb5cSJason Baron 	case -EPERM:
966262965f5SChuck Lever 		/* When the server has died, an ICMP port unreachable message
967262965f5SChuck Lever 		 * prompts ECONNREFUSED. */
96813331a55STrond Myklebust 		break;
96913331a55STrond Myklebust 	default:
97013331a55STrond Myklebust 		dprintk("RPC:       sendmsg returned unrecognized error %d\n",
97113331a55STrond Myklebust 			-status);
972262965f5SChuck Lever 	}
9735fe46e9dSBian Naimeng 
974262965f5SChuck Lever 	return status;
975262965f5SChuck Lever }
976262965f5SChuck Lever 
977e06799f9STrond Myklebust /**
978262965f5SChuck Lever  * xs_tcp_send_request - write an RPC request to a TCP socket
97950f484e2STrond Myklebust  * @req: pointer to RPC request
980262965f5SChuck Lever  *
981262965f5SChuck Lever  * Return values:
982262965f5SChuck Lever  *        0:	The request has been sent
983262965f5SChuck Lever  *   EAGAIN:	The socket was blocked, please call again later to
984262965f5SChuck Lever  *		complete the request
985262965f5SChuck Lever  * ENOTCONN:	Caller needs to invoke connect logic then call again
98625985edcSLucas De Marchi  *    other:	Some other error occurred, the request was not sent
987262965f5SChuck Lever  *
988262965f5SChuck Lever  * XXX: In the case of soft timeouts, should we eventually give up
989262965f5SChuck Lever  *	if sendmsg is not able to make progress?
990262965f5SChuck Lever  */
991adfa7144STrond Myklebust static int xs_tcp_send_request(struct rpc_rqst *req)
992262965f5SChuck Lever {
993262965f5SChuck Lever 	struct rpc_xprt *xprt = req->rq_xprt;
994ee0ac0c2SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
995262965f5SChuck Lever 	struct xdr_buf *xdr = &req->rq_snd_buf;
9967e3d3620STrond Myklebust 	rpc_fraghdr rm = xs_stream_record_marker(xdr);
9977e3d3620STrond Myklebust 	unsigned int msglen = rm ? req->rq_slen + sizeof(rm) : req->rq_slen;
9989e55eef4SChuck Lever 	struct msghdr msg = {
9999e55eef4SChuck Lever 		.msg_flags	= XS_SENDMSG_FLAGS,
10009e55eef4SChuck Lever 	};
10019ffadfbcSTrond Myklebust 	bool vm_wait = false;
10029e55eef4SChuck Lever 	unsigned int uninitialized_var(sent);
1003b595bb15SChuck Lever 	int status;
1004a246b010SChuck Lever 
10054cd34e7cSTrond Myklebust 	/* Close the stream if the previous transmission was incomplete */
10064cd34e7cSTrond Myklebust 	if (xs_send_request_was_aborted(transport, req)) {
10074cd34e7cSTrond Myklebust 		if (transport->sock != NULL)
10084cd34e7cSTrond Myklebust 			kernel_sock_shutdown(transport->sock, SHUT_RDWR);
10094cd34e7cSTrond Myklebust 		return -ENOTCONN;
10104cd34e7cSTrond Myklebust 	}
10114cd34e7cSTrond Myklebust 
1012262965f5SChuck Lever 	xs_pktdump("packet data:",
1013262965f5SChuck Lever 				req->rq_svec->iov_base,
1014262965f5SChuck Lever 				req->rq_svec->iov_len);
1015a246b010SChuck Lever 
10167196dbb0STrond Myklebust 	if (test_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state))
10177196dbb0STrond Myklebust 		xs_tcp_set_socket_timeouts(xprt, transport->sock);
10187196dbb0STrond Myklebust 
1019a246b010SChuck Lever 	/* Continue transmitting the packet/record. We must be careful
1020a246b010SChuck Lever 	 * to cope with writespace callbacks arriving _after_ we have
1021262965f5SChuck Lever 	 * called sendmsg(). */
102278215759SChuck Lever 	req->rq_xtime = ktime_get();
1023a246b010SChuck Lever 	while (1) {
10249e55eef4SChuck Lever 		status = xprt_sock_sendmsg(transport->sock, &msg, xdr,
10257e3d3620STrond Myklebust 					   transport->xmit.offset, rm, &sent);
1026a246b010SChuck Lever 
1027262965f5SChuck Lever 		dprintk("RPC:       xs_tcp_send_request(%u) = %d\n",
10286c7a64e5STrond Myklebust 				xdr->len - transport->xmit.offset, status);
1029262965f5SChuck Lever 
1030a246b010SChuck Lever 		/* If we've sent the entire packet, immediately
1031a246b010SChuck Lever 		 * reset the count of bytes sent. */
10326c7a64e5STrond Myklebust 		transport->xmit.offset += sent;
10336c7a64e5STrond Myklebust 		req->rq_bytes_sent = transport->xmit.offset;
10347e3d3620STrond Myklebust 		if (likely(req->rq_bytes_sent >= msglen)) {
10356c7a64e5STrond Myklebust 			req->rq_xmit_bytes_sent += transport->xmit.offset;
10366c7a64e5STrond Myklebust 			transport->xmit.offset = 0;
1037a246b010SChuck Lever 			return 0;
1038a246b010SChuck Lever 		}
1039262965f5SChuck Lever 
10409ffadfbcSTrond Myklebust 		WARN_ON_ONCE(sent == 0 && status == 0);
10419ffadfbcSTrond Myklebust 
10429ffadfbcSTrond Myklebust 		if (status == -EAGAIN ) {
10439ffadfbcSTrond Myklebust 			/*
10449ffadfbcSTrond Myklebust 			 * Return EAGAIN if we're sure we're hitting the
10459ffadfbcSTrond Myklebust 			 * socket send buffer limits.
10469ffadfbcSTrond Myklebust 			 */
10479ffadfbcSTrond Myklebust 			if (test_bit(SOCK_NOSPACE, &transport->sock->flags))
10489ffadfbcSTrond Myklebust 				break;
10499ffadfbcSTrond Myklebust 			/*
10509ffadfbcSTrond Myklebust 			 * Did we hit a memory allocation failure?
10519ffadfbcSTrond Myklebust 			 */
10529ffadfbcSTrond Myklebust 			if (sent == 0) {
10539ffadfbcSTrond Myklebust 				status = -ENOBUFS;
10549ffadfbcSTrond Myklebust 				if (vm_wait)
10559ffadfbcSTrond Myklebust 					break;
10569ffadfbcSTrond Myklebust 				/* Retry, knowing now that we're below the
10579ffadfbcSTrond Myklebust 				 * socket send buffer limit
10589ffadfbcSTrond Myklebust 				 */
10599ffadfbcSTrond Myklebust 				vm_wait = true;
10609ffadfbcSTrond Myklebust 			}
10619ffadfbcSTrond Myklebust 			continue;
10629ffadfbcSTrond Myklebust 		}
1063f580dd04STrond Myklebust 		if (status < 0)
1064f580dd04STrond Myklebust 			break;
10659ffadfbcSTrond Myklebust 		vm_wait = false;
1066a246b010SChuck Lever 	}
1067a246b010SChuck Lever 
1068262965f5SChuck Lever 	switch (status) {
1069fba91afbSTrond Myklebust 	case -ENOTSOCK:
1070fba91afbSTrond Myklebust 		status = -ENOTCONN;
1071fba91afbSTrond Myklebust 		/* Should we call xs_close() here? */
1072fba91afbSTrond Myklebust 		break;
1073262965f5SChuck Lever 	case -EAGAIN:
1074c544577dSTrond Myklebust 		status = xs_nospace(req);
1075262965f5SChuck Lever 		break;
1076262965f5SChuck Lever 	case -ECONNRESET:
1077262965f5SChuck Lever 	case -ECONNREFUSED:
1078262965f5SChuck Lever 	case -ENOTCONN:
10793913c78cSTrond Myklebust 	case -EADDRINUSE:
1080b5872f0cSTrond Myklebust 	case -ENOBUFS:
1081b9d2bb2eSTrond Myklebust 	case -EPIPE:
108213331a55STrond Myklebust 		break;
108313331a55STrond Myklebust 	default:
108413331a55STrond Myklebust 		dprintk("RPC:       sendmsg returned unrecognized error %d\n",
108513331a55STrond Myklebust 			-status);
1086a246b010SChuck Lever 	}
10875fe46e9dSBian Naimeng 
1088a246b010SChuck Lever 	return status;
1089a246b010SChuck Lever }
1090a246b010SChuck Lever 
10912a9e1cfaSTrond Myklebust static void xs_save_old_callbacks(struct sock_xprt *transport, struct sock *sk)
10922a9e1cfaSTrond Myklebust {
10932a9e1cfaSTrond Myklebust 	transport->old_data_ready = sk->sk_data_ready;
10942a9e1cfaSTrond Myklebust 	transport->old_state_change = sk->sk_state_change;
10952a9e1cfaSTrond Myklebust 	transport->old_write_space = sk->sk_write_space;
10962118071dSTrond Myklebust 	transport->old_error_report = sk->sk_error_report;
10972a9e1cfaSTrond Myklebust }
10982a9e1cfaSTrond Myklebust 
10992a9e1cfaSTrond Myklebust static void xs_restore_old_callbacks(struct sock_xprt *transport, struct sock *sk)
11002a9e1cfaSTrond Myklebust {
11012a9e1cfaSTrond Myklebust 	sk->sk_data_ready = transport->old_data_ready;
11022a9e1cfaSTrond Myklebust 	sk->sk_state_change = transport->old_state_change;
11032a9e1cfaSTrond Myklebust 	sk->sk_write_space = transport->old_write_space;
11042118071dSTrond Myklebust 	sk->sk_error_report = transport->old_error_report;
11052118071dSTrond Myklebust }
11062118071dSTrond Myklebust 
110742d42a5bSTrond Myklebust static void xs_sock_reset_state_flags(struct rpc_xprt *xprt)
110842d42a5bSTrond Myklebust {
110942d42a5bSTrond Myklebust 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
111042d42a5bSTrond Myklebust 
111142d42a5bSTrond Myklebust 	clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state);
11124f8943f8STrond Myklebust 	clear_bit(XPRT_SOCK_WAKE_ERROR, &transport->sock_state);
11134f8943f8STrond Myklebust 	clear_bit(XPRT_SOCK_WAKE_WRITE, &transport->sock_state);
11144f8943f8STrond Myklebust 	clear_bit(XPRT_SOCK_WAKE_DISCONNECT, &transport->sock_state);
11154f8943f8STrond Myklebust }
11164f8943f8STrond Myklebust 
11174f8943f8STrond Myklebust static void xs_run_error_worker(struct sock_xprt *transport, unsigned int nr)
11184f8943f8STrond Myklebust {
11194f8943f8STrond Myklebust 	set_bit(nr, &transport->sock_state);
11204f8943f8STrond Myklebust 	queue_work(xprtiod_workqueue, &transport->error_worker);
112142d42a5bSTrond Myklebust }
112242d42a5bSTrond Myklebust 
1123b70ae915STrond Myklebust static void xs_sock_reset_connection_flags(struct rpc_xprt *xprt)
1124b70ae915STrond Myklebust {
1125b70ae915STrond Myklebust 	smp_mb__before_atomic();
1126b70ae915STrond Myklebust 	clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
1127b70ae915STrond Myklebust 	clear_bit(XPRT_CLOSING, &xprt->state);
112842d42a5bSTrond Myklebust 	xs_sock_reset_state_flags(xprt);
1129b70ae915STrond Myklebust 	smp_mb__after_atomic();
1130b70ae915STrond Myklebust }
1131b70ae915STrond Myklebust 
11322118071dSTrond Myklebust /**
11332118071dSTrond Myklebust  * xs_error_report - callback to handle TCP socket state errors
11342118071dSTrond Myklebust  * @sk: socket
11352118071dSTrond Myklebust  *
11362118071dSTrond Myklebust  * Note: we don't call sock_error() since there may be a rpc_task
11372118071dSTrond Myklebust  * using the socket, and so we don't want to clear sk->sk_err.
11382118071dSTrond Myklebust  */
11392118071dSTrond Myklebust static void xs_error_report(struct sock *sk)
11402118071dSTrond Myklebust {
11414f8943f8STrond Myklebust 	struct sock_xprt *transport;
11422118071dSTrond Myklebust 	struct rpc_xprt *xprt;
11432118071dSTrond Myklebust 
11442118071dSTrond Myklebust 	read_lock_bh(&sk->sk_callback_lock);
11452118071dSTrond Myklebust 	if (!(xprt = xprt_from_sock(sk)))
11462118071dSTrond Myklebust 		goto out;
11472118071dSTrond Myklebust 
11484f8943f8STrond Myklebust 	transport = container_of(xprt, struct sock_xprt, xprt);
1149af84537dSBenjamin Coddington 	transport->xprt_err = -sk->sk_err;
1150af84537dSBenjamin Coddington 	if (transport->xprt_err == 0)
11512118071dSTrond Myklebust 		goto out;
11522118071dSTrond Myklebust 	dprintk("RPC:       xs_error_report client %p, error=%d...\n",
1153af84537dSBenjamin Coddington 			xprt, -transport->xprt_err);
1154af84537dSBenjamin Coddington 	trace_rpc_socket_error(xprt, sk->sk_socket, transport->xprt_err);
1155af84537dSBenjamin Coddington 
1156af84537dSBenjamin Coddington 	/* barrier ensures xprt_err is set before XPRT_SOCK_WAKE_ERROR */
1157af84537dSBenjamin Coddington 	smp_mb__before_atomic();
11584f8943f8STrond Myklebust 	xs_run_error_worker(transport, XPRT_SOCK_WAKE_ERROR);
11592118071dSTrond Myklebust  out:
11602118071dSTrond Myklebust 	read_unlock_bh(&sk->sk_callback_lock);
11612a9e1cfaSTrond Myklebust }
11622a9e1cfaSTrond Myklebust 
1163fe315e76SChuck Lever static void xs_reset_transport(struct sock_xprt *transport)
1164a246b010SChuck Lever {
1165ee0ac0c2SChuck Lever 	struct socket *sock = transport->sock;
1166ee0ac0c2SChuck Lever 	struct sock *sk = transport->inet;
11676cc7e908STrond Myklebust 	struct rpc_xprt *xprt = &transport->xprt;
1168a73881c9STrond Myklebust 	struct file *filp = transport->file;
1169a246b010SChuck Lever 
1170fe315e76SChuck Lever 	if (sk == NULL)
1171fe315e76SChuck Lever 		return;
11729903cd1cSChuck Lever 
1173264d1df3SJeff Layton 	if (atomic_read(&transport->xprt.swapper))
1174264d1df3SJeff Layton 		sk_clear_memalloc(sk);
1175264d1df3SJeff Layton 
117609939204STrond Myklebust 	kernel_sock_shutdown(sock, SHUT_RDWR);
117709939204STrond Myklebust 
1178edc1b01cSTrond Myklebust 	mutex_lock(&transport->recv_mutex);
1179a246b010SChuck Lever 	write_lock_bh(&sk->sk_callback_lock);
1180ee0ac0c2SChuck Lever 	transport->inet = NULL;
1181ee0ac0c2SChuck Lever 	transport->sock = NULL;
1182a73881c9STrond Myklebust 	transport->file = NULL;
1183a246b010SChuck Lever 
1184a246b010SChuck Lever 	sk->sk_user_data = NULL;
11852a9e1cfaSTrond Myklebust 
11862a9e1cfaSTrond Myklebust 	xs_restore_old_callbacks(transport, sk);
11870c78789eSTrond Myklebust 	xprt_clear_connected(xprt);
1188a246b010SChuck Lever 	write_unlock_bh(&sk->sk_callback_lock);
11896cc7e908STrond Myklebust 	xs_sock_reset_connection_flags(xprt);
1190ae053551STrond Myklebust 	/* Reset stream record info */
1191ae053551STrond Myklebust 	xs_stream_reset_connect(transport);
1192edc1b01cSTrond Myklebust 	mutex_unlock(&transport->recv_mutex);
1193a246b010SChuck Lever 
11946cc7e908STrond Myklebust 	trace_rpc_socket_close(xprt, sock);
1195a73881c9STrond Myklebust 	fput(filp);
11960445f92cSTrond Myklebust 
11970445f92cSTrond Myklebust 	xprt_disconnect_done(xprt);
1198fe315e76SChuck Lever }
1199fe315e76SChuck Lever 
1200fe315e76SChuck Lever /**
1201fe315e76SChuck Lever  * xs_close - close a socket
1202fe315e76SChuck Lever  * @xprt: transport
1203fe315e76SChuck Lever  *
1204fe315e76SChuck Lever  * This is used when all requests are complete; ie, no DRC state remains
1205fe315e76SChuck Lever  * on the server we want to save.
1206f75e6745STrond Myklebust  *
1207f75e6745STrond Myklebust  * The caller _must_ be holding XPRT_LOCKED in order to avoid issues with
1208f75e6745STrond Myklebust  * xs_reset_transport() zeroing the socket from underneath a writer.
1209fe315e76SChuck Lever  */
1210fe315e76SChuck Lever static void xs_close(struct rpc_xprt *xprt)
1211fe315e76SChuck Lever {
1212fe315e76SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1213fe315e76SChuck Lever 
1214fe315e76SChuck Lever 	dprintk("RPC:       xs_close xprt %p\n", xprt);
1215fe315e76SChuck Lever 
1216fe315e76SChuck Lever 	xs_reset_transport(transport);
121761d0a8e6SNeil Brown 	xprt->reestablish_timeout = 0;
1218a246b010SChuck Lever }
1219a246b010SChuck Lever 
12204a068258SChuck Lever static void xs_inject_disconnect(struct rpc_xprt *xprt)
12214a068258SChuck Lever {
12224a068258SChuck Lever 	dprintk("RPC:       injecting transport disconnect on xprt=%p\n",
12234a068258SChuck Lever 		xprt);
12244a068258SChuck Lever 	xprt_disconnect_done(xprt);
12254a068258SChuck Lever }
12264a068258SChuck Lever 
1227315f3812SKinglong Mee static void xs_xprt_free(struct rpc_xprt *xprt)
1228315f3812SKinglong Mee {
1229315f3812SKinglong Mee 	xs_free_peer_addresses(xprt);
1230315f3812SKinglong Mee 	xprt_free(xprt);
1231315f3812SKinglong Mee }
1232315f3812SKinglong Mee 
12339903cd1cSChuck Lever /**
12349903cd1cSChuck Lever  * xs_destroy - prepare to shutdown a transport
12359903cd1cSChuck Lever  * @xprt: doomed transport
12369903cd1cSChuck Lever  *
12379903cd1cSChuck Lever  */
12389903cd1cSChuck Lever static void xs_destroy(struct rpc_xprt *xprt)
1239a246b010SChuck Lever {
124003c78827STrond Myklebust 	struct sock_xprt *transport = container_of(xprt,
124103c78827STrond Myklebust 			struct sock_xprt, xprt);
12429903cd1cSChuck Lever 	dprintk("RPC:       xs_destroy xprt %p\n", xprt);
12439903cd1cSChuck Lever 
124403c78827STrond Myklebust 	cancel_delayed_work_sync(&transport->connect_worker);
1245a1311d87STrond Myklebust 	xs_close(xprt);
1246edc1b01cSTrond Myklebust 	cancel_work_sync(&transport->recv_worker);
1247b5e92419STrond Myklebust 	cancel_work_sync(&transport->error_worker);
1248315f3812SKinglong Mee 	xs_xprt_free(xprt);
1249a1311d87STrond Myklebust 	module_put(THIS_MODULE);
1250a246b010SChuck Lever }
1251a246b010SChuck Lever 
12529903cd1cSChuck Lever /**
1253f9b2ee71STrond Myklebust  * xs_udp_data_read_skb - receive callback for UDP sockets
1254f9b2ee71STrond Myklebust  * @xprt: transport
1255f9b2ee71STrond Myklebust  * @sk: socket
1256f9b2ee71STrond Myklebust  * @skb: skbuff
12579903cd1cSChuck Lever  *
1258a246b010SChuck Lever  */
1259f9b2ee71STrond Myklebust static void xs_udp_data_read_skb(struct rpc_xprt *xprt,
1260f9b2ee71STrond Myklebust 		struct sock *sk,
1261f9b2ee71STrond Myklebust 		struct sk_buff *skb)
1262a246b010SChuck Lever {
1263a246b010SChuck Lever 	struct rpc_task *task;
1264a246b010SChuck Lever 	struct rpc_rqst *rovr;
1265f9b2ee71STrond Myklebust 	int repsize, copied;
1266d8ed029dSAlexey Dobriyan 	u32 _xid;
1267d8ed029dSAlexey Dobriyan 	__be32 *xp;
1268a246b010SChuck Lever 
12691da8c681SWillem de Bruijn 	repsize = skb->len;
1270a246b010SChuck Lever 	if (repsize < 4) {
12719903cd1cSChuck Lever 		dprintk("RPC:       impossible RPC reply size %d!\n", repsize);
1272f9b2ee71STrond Myklebust 		return;
1273a246b010SChuck Lever 	}
1274a246b010SChuck Lever 
1275a246b010SChuck Lever 	/* Copy the XID from the skb... */
12761da8c681SWillem de Bruijn 	xp = skb_header_pointer(skb, 0, sizeof(_xid), &_xid);
1277a246b010SChuck Lever 	if (xp == NULL)
1278f9b2ee71STrond Myklebust 		return;
1279a246b010SChuck Lever 
1280a246b010SChuck Lever 	/* Look up and lock the request corresponding to the given XID */
128175c84151STrond Myklebust 	spin_lock(&xprt->queue_lock);
1282a246b010SChuck Lever 	rovr = xprt_lookup_rqst(xprt, *xp);
1283a246b010SChuck Lever 	if (!rovr)
1284a246b010SChuck Lever 		goto out_unlock;
1285729749bbSTrond Myklebust 	xprt_pin_rqst(rovr);
1286ecd465eeSChuck Lever 	xprt_update_rtt(rovr->rq_task);
128775c84151STrond Myklebust 	spin_unlock(&xprt->queue_lock);
1288a246b010SChuck Lever 	task = rovr->rq_task;
1289a246b010SChuck Lever 
1290a246b010SChuck Lever 	if ((copied = rovr->rq_private_buf.buflen) > repsize)
1291a246b010SChuck Lever 		copied = repsize;
1292a246b010SChuck Lever 
1293a246b010SChuck Lever 	/* Suck it into the iovec, verify checksum if not done by hw. */
12941781f7f5SHerbert Xu 	if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) {
129575c84151STrond Myklebust 		spin_lock(&xprt->queue_lock);
12960afa6b44STrond Myklebust 		__UDPX_INC_STATS(sk, UDP_MIB_INERRORS);
1297729749bbSTrond Myklebust 		goto out_unpin;
12981781f7f5SHerbert Xu 	}
12991781f7f5SHerbert Xu 
1300a246b010SChuck Lever 
1301b5e92419STrond Myklebust 	spin_lock(&xprt->transport_lock);
13026a24dfb6STrond Myklebust 	xprt_adjust_cwnd(xprt, task, copied);
1303b5e92419STrond Myklebust 	spin_unlock(&xprt->transport_lock);
130475c84151STrond Myklebust 	spin_lock(&xprt->queue_lock);
13051570c1e4SChuck Lever 	xprt_complete_rqst(task, copied);
13060afa6b44STrond Myklebust 	__UDPX_INC_STATS(sk, UDP_MIB_INDATAGRAMS);
1307729749bbSTrond Myklebust out_unpin:
1308729749bbSTrond Myklebust 	xprt_unpin_rqst(rovr);
1309a246b010SChuck Lever  out_unlock:
131075c84151STrond Myklebust 	spin_unlock(&xprt->queue_lock);
1311f9b2ee71STrond Myklebust }
1312f9b2ee71STrond Myklebust 
1313f9b2ee71STrond Myklebust static void xs_udp_data_receive(struct sock_xprt *transport)
1314f9b2ee71STrond Myklebust {
1315f9b2ee71STrond Myklebust 	struct sk_buff *skb;
1316f9b2ee71STrond Myklebust 	struct sock *sk;
1317f9b2ee71STrond Myklebust 	int err;
1318f9b2ee71STrond Myklebust 
1319f9b2ee71STrond Myklebust 	mutex_lock(&transport->recv_mutex);
1320f9b2ee71STrond Myklebust 	sk = transport->inet;
1321f9b2ee71STrond Myklebust 	if (sk == NULL)
1322f9b2ee71STrond Myklebust 		goto out;
1323f9b2ee71STrond Myklebust 	for (;;) {
13247c13f97fSPaolo Abeni 		skb = skb_recv_udp(sk, 0, 1, &err);
13254f546149STrond Myklebust 		if (skb == NULL)
13264f546149STrond Myklebust 			break;
1327f9b2ee71STrond Myklebust 		xs_udp_data_read_skb(&transport->xprt, sk, skb);
1328850cbaddSPaolo Abeni 		consume_skb(skb);
13290af3442aSTrond Myklebust 		cond_resched();
1330f9b2ee71STrond Myklebust 	}
13310ffe86f4STrond Myklebust 	xs_poll_check_readable(transport);
1332a246b010SChuck Lever out:
1333f9b2ee71STrond Myklebust 	mutex_unlock(&transport->recv_mutex);
1334f9b2ee71STrond Myklebust }
1335f9b2ee71STrond Myklebust 
1336f9b2ee71STrond Myklebust static void xs_udp_data_receive_workfn(struct work_struct *work)
1337f9b2ee71STrond Myklebust {
1338f9b2ee71STrond Myklebust 	struct sock_xprt *transport =
1339f9b2ee71STrond Myklebust 		container_of(work, struct sock_xprt, recv_worker);
1340a1231fdaSTrond Myklebust 	unsigned int pflags = memalloc_nofs_save();
1341a1231fdaSTrond Myklebust 
1342f9b2ee71STrond Myklebust 	xs_udp_data_receive(transport);
1343a1231fdaSTrond Myklebust 	memalloc_nofs_restore(pflags);
1344f9b2ee71STrond Myklebust }
1345f9b2ee71STrond Myklebust 
1346f9b2ee71STrond Myklebust /**
1347f9b2ee71STrond Myklebust  * xs_data_ready - "data ready" callback for UDP sockets
1348f9b2ee71STrond Myklebust  * @sk: socket with data to read
1349f9b2ee71STrond Myklebust  *
1350f9b2ee71STrond Myklebust  */
1351f9b2ee71STrond Myklebust static void xs_data_ready(struct sock *sk)
1352f9b2ee71STrond Myklebust {
1353f9b2ee71STrond Myklebust 	struct rpc_xprt *xprt;
1354f9b2ee71STrond Myklebust 
1355f9b2ee71STrond Myklebust 	read_lock_bh(&sk->sk_callback_lock);
1356f9b2ee71STrond Myklebust 	dprintk("RPC:       xs_data_ready...\n");
1357f9b2ee71STrond Myklebust 	xprt = xprt_from_sock(sk);
1358f9b2ee71STrond Myklebust 	if (xprt != NULL) {
1359f9b2ee71STrond Myklebust 		struct sock_xprt *transport = container_of(xprt,
1360f9b2ee71STrond Myklebust 				struct sock_xprt, xprt);
13615157b956STrond Myklebust 		transport->old_data_ready(sk);
13625157b956STrond Myklebust 		/* Any data means we had a useful conversation, so
13635157b956STrond Myklebust 		 * then we don't need to delay the next reconnect
13645157b956STrond Myklebust 		 */
13655157b956STrond Myklebust 		if (xprt->reestablish_timeout)
13665157b956STrond Myklebust 			xprt->reestablish_timeout = 0;
136742d42a5bSTrond Myklebust 		if (!test_and_set_bit(XPRT_SOCK_DATA_READY, &transport->sock_state))
136840a5f1b1STrond Myklebust 			queue_work(xprtiod_workqueue, &transport->recv_worker);
1369f9b2ee71STrond Myklebust 	}
1370f064af1eSEric Dumazet 	read_unlock_bh(&sk->sk_callback_lock);
1371a246b010SChuck Lever }
1372a246b010SChuck Lever 
1373a519fc7aSTrond Myklebust /*
1374a519fc7aSTrond Myklebust  * Helper function to force a TCP close if the server is sending
1375a519fc7aSTrond Myklebust  * junk and/or it has put us in CLOSE_WAIT
1376a519fc7aSTrond Myklebust  */
1377a519fc7aSTrond Myklebust static void xs_tcp_force_close(struct rpc_xprt *xprt)
1378a519fc7aSTrond Myklebust {
1379a519fc7aSTrond Myklebust 	xprt_force_disconnect(xprt);
1380a519fc7aSTrond Myklebust }
1381a519fc7aSTrond Myklebust 
13829e00abc3STrond Myklebust #if defined(CONFIG_SUNRPC_BACKCHANNEL)
13836b26cc8cSChuck Lever static size_t xs_tcp_bc_maxpayload(struct rpc_xprt *xprt)
13846b26cc8cSChuck Lever {
13856b26cc8cSChuck Lever 	return PAGE_SIZE;
13866b26cc8cSChuck Lever }
13879e00abc3STrond Myklebust #endif /* CONFIG_SUNRPC_BACKCHANNEL */
138844b98efdSRicardo Labiaga 
13899903cd1cSChuck Lever /**
13909903cd1cSChuck Lever  * xs_tcp_state_change - callback to handle TCP socket state changes
13919903cd1cSChuck Lever  * @sk: socket whose state has changed
13929903cd1cSChuck Lever  *
13939903cd1cSChuck Lever  */
13949903cd1cSChuck Lever static void xs_tcp_state_change(struct sock *sk)
1395a246b010SChuck Lever {
1396a246b010SChuck Lever 	struct rpc_xprt *xprt;
13970fdea1e8STrond Myklebust 	struct sock_xprt *transport;
1398a246b010SChuck Lever 
1399f064af1eSEric Dumazet 	read_lock_bh(&sk->sk_callback_lock);
1400a246b010SChuck Lever 	if (!(xprt = xprt_from_sock(sk)))
1401a246b010SChuck Lever 		goto out;
14029903cd1cSChuck Lever 	dprintk("RPC:       xs_tcp_state_change client %p...\n", xprt);
1403669502ffSAndy Chittenden 	dprintk("RPC:       state %x conn %d dead %d zapped %d sk_shutdown %d\n",
1404a246b010SChuck Lever 			sk->sk_state, xprt_connected(xprt),
1405a246b010SChuck Lever 			sock_flag(sk, SOCK_DEAD),
1406669502ffSAndy Chittenden 			sock_flag(sk, SOCK_ZAPPED),
1407669502ffSAndy Chittenden 			sk->sk_shutdown);
1408a246b010SChuck Lever 
14090fdea1e8STrond Myklebust 	transport = container_of(xprt, struct sock_xprt, xprt);
141040b5ea0cSTrond Myklebust 	trace_rpc_socket_state_change(xprt, sk->sk_socket);
1411a246b010SChuck Lever 	switch (sk->sk_state) {
1412a246b010SChuck Lever 	case TCP_ESTABLISHED:
1413a246b010SChuck Lever 		if (!xprt_test_and_set_connected(xprt)) {
14148b71798cSTrond Myklebust 			xprt->connect_cookie++;
14150fdea1e8STrond Myklebust 			clear_bit(XPRT_SOCK_CONNECTING, &transport->sock_state);
14160fdea1e8STrond Myklebust 			xprt_clear_connecting(xprt);
141751971139SChuck Lever 
14183968a8a5SChuck Lever 			xprt->stat.connect_count++;
14193968a8a5SChuck Lever 			xprt->stat.connect_time += (long)jiffies -
14203968a8a5SChuck Lever 						   xprt->stat.connect_start;
14214f8943f8STrond Myklebust 			xs_run_error_worker(transport, XPRT_SOCK_WAKE_PENDING);
1422a246b010SChuck Lever 		}
1423a246b010SChuck Lever 		break;
14243b948ae5STrond Myklebust 	case TCP_FIN_WAIT1:
14253b948ae5STrond Myklebust 		/* The client initiated a shutdown of the socket */
14267c1d71cfSTrond Myklebust 		xprt->connect_cookie++;
1427663b8858STrond Myklebust 		xprt->reestablish_timeout = 0;
14283b948ae5STrond Myklebust 		set_bit(XPRT_CLOSING, &xprt->state);
14294e857c58SPeter Zijlstra 		smp_mb__before_atomic();
14303b948ae5STrond Myklebust 		clear_bit(XPRT_CONNECTED, &xprt->state);
1431ef803670STrond Myklebust 		clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
14324e857c58SPeter Zijlstra 		smp_mb__after_atomic();
1433a246b010SChuck Lever 		break;
1434632e3bdcSTrond Myklebust 	case TCP_CLOSE_WAIT:
14353b948ae5STrond Myklebust 		/* The server initiated a shutdown of the socket */
14367c1d71cfSTrond Myklebust 		xprt->connect_cookie++;
1437d0bea455STrond Myklebust 		clear_bit(XPRT_CONNECTED, &xprt->state);
14384f8943f8STrond Myklebust 		xs_run_error_worker(transport, XPRT_SOCK_WAKE_DISCONNECT);
1439e9d47639SGustavo A. R. Silva 		/* fall through */
1440663b8858STrond Myklebust 	case TCP_CLOSING:
1441663b8858STrond Myklebust 		/*
1442663b8858STrond Myklebust 		 * If the server closed down the connection, make sure that
1443663b8858STrond Myklebust 		 * we back off before reconnecting
1444663b8858STrond Myklebust 		 */
1445663b8858STrond Myklebust 		if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
1446663b8858STrond Myklebust 			xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
14473b948ae5STrond Myklebust 		break;
14483b948ae5STrond Myklebust 	case TCP_LAST_ACK:
1449670f9457STrond Myklebust 		set_bit(XPRT_CLOSING, &xprt->state);
14504e857c58SPeter Zijlstra 		smp_mb__before_atomic();
14513b948ae5STrond Myklebust 		clear_bit(XPRT_CONNECTED, &xprt->state);
14524e857c58SPeter Zijlstra 		smp_mb__after_atomic();
14533b948ae5STrond Myklebust 		break;
14543b948ae5STrond Myklebust 	case TCP_CLOSE:
14550fdea1e8STrond Myklebust 		if (test_and_clear_bit(XPRT_SOCK_CONNECTING,
14560fdea1e8STrond Myklebust 					&transport->sock_state))
14570fdea1e8STrond Myklebust 			xprt_clear_connecting(xprt);
14589b30889cSTrond Myklebust 		clear_bit(XPRT_CLOSING, &xprt->state);
14599b30889cSTrond Myklebust 		/* Trigger the socket release */
14604f8943f8STrond Myklebust 		xs_run_error_worker(transport, XPRT_SOCK_WAKE_DISCONNECT);
1461a246b010SChuck Lever 	}
1462a246b010SChuck Lever  out:
1463f064af1eSEric Dumazet 	read_unlock_bh(&sk->sk_callback_lock);
1464a246b010SChuck Lever }
1465a246b010SChuck Lever 
14661f0fa154SIlpo Järvinen static void xs_write_space(struct sock *sk)
14671f0fa154SIlpo Järvinen {
146813331a55STrond Myklebust 	struct socket_wq *wq;
14694f8943f8STrond Myklebust 	struct sock_xprt *transport;
14701f0fa154SIlpo Järvinen 	struct rpc_xprt *xprt;
14711f0fa154SIlpo Järvinen 
147213331a55STrond Myklebust 	if (!sk->sk_socket)
14731f0fa154SIlpo Järvinen 		return;
147413331a55STrond Myklebust 	clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
14751f0fa154SIlpo Järvinen 
14761f0fa154SIlpo Järvinen 	if (unlikely(!(xprt = xprt_from_sock(sk))))
14771f0fa154SIlpo Järvinen 		return;
14784f8943f8STrond Myklebust 	transport = container_of(xprt, struct sock_xprt, xprt);
147913331a55STrond Myklebust 	rcu_read_lock();
148013331a55STrond Myklebust 	wq = rcu_dereference(sk->sk_wq);
148113331a55STrond Myklebust 	if (!wq || test_and_clear_bit(SOCKWQ_ASYNC_NOSPACE, &wq->flags) == 0)
148213331a55STrond Myklebust 		goto out;
14831f0fa154SIlpo Järvinen 
14844f8943f8STrond Myklebust 	xs_run_error_worker(transport, XPRT_SOCK_WAKE_WRITE);
1485c544577dSTrond Myklebust 	sk->sk_write_pending--;
148613331a55STrond Myklebust out:
148713331a55STrond Myklebust 	rcu_read_unlock();
14881f0fa154SIlpo Järvinen }
14891f0fa154SIlpo Järvinen 
14902a9e1cfaSTrond Myklebust /**
1491c7b2cae8SChuck Lever  * xs_udp_write_space - callback invoked when socket buffer space
1492c7b2cae8SChuck Lever  *                             becomes available
14939903cd1cSChuck Lever  * @sk: socket whose state has changed
14949903cd1cSChuck Lever  *
1495a246b010SChuck Lever  * Called when more output buffer space is available for this socket.
1496a246b010SChuck Lever  * We try not to wake our writers until they can make "significant"
1497c7b2cae8SChuck Lever  * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1498a246b010SChuck Lever  * with a bunch of small requests.
1499a246b010SChuck Lever  */
1500c7b2cae8SChuck Lever static void xs_udp_write_space(struct sock *sk)
1501a246b010SChuck Lever {
1502f064af1eSEric Dumazet 	read_lock_bh(&sk->sk_callback_lock);
1503c7b2cae8SChuck Lever 
1504c7b2cae8SChuck Lever 	/* from net/core/sock.c:sock_def_write_space */
15051f0fa154SIlpo Järvinen 	if (sock_writeable(sk))
15061f0fa154SIlpo Järvinen 		xs_write_space(sk);
1507c7b2cae8SChuck Lever 
1508f064af1eSEric Dumazet 	read_unlock_bh(&sk->sk_callback_lock);
1509c7b2cae8SChuck Lever }
1510c7b2cae8SChuck Lever 
1511c7b2cae8SChuck Lever /**
1512c7b2cae8SChuck Lever  * xs_tcp_write_space - callback invoked when socket buffer space
1513c7b2cae8SChuck Lever  *                             becomes available
1514c7b2cae8SChuck Lever  * @sk: socket whose state has changed
1515c7b2cae8SChuck Lever  *
1516c7b2cae8SChuck Lever  * Called when more output buffer space is available for this socket.
1517c7b2cae8SChuck Lever  * We try not to wake our writers until they can make "significant"
1518c7b2cae8SChuck Lever  * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1519c7b2cae8SChuck Lever  * with a bunch of small requests.
1520c7b2cae8SChuck Lever  */
1521c7b2cae8SChuck Lever static void xs_tcp_write_space(struct sock *sk)
1522c7b2cae8SChuck Lever {
1523f064af1eSEric Dumazet 	read_lock_bh(&sk->sk_callback_lock);
1524c7b2cae8SChuck Lever 
1525c7b2cae8SChuck Lever 	/* from net/core/stream.c:sk_stream_write_space */
152664dc6130SEric Dumazet 	if (sk_stream_is_writeable(sk))
15271f0fa154SIlpo Järvinen 		xs_write_space(sk);
1528c7b2cae8SChuck Lever 
1529f064af1eSEric Dumazet 	read_unlock_bh(&sk->sk_callback_lock);
1530a246b010SChuck Lever }
1531a246b010SChuck Lever 
1532470056c2SChuck Lever static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt)
1533a246b010SChuck Lever {
1534ee0ac0c2SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1535ee0ac0c2SChuck Lever 	struct sock *sk = transport->inet;
1536a246b010SChuck Lever 
15377c6e066eSChuck Lever 	if (transport->rcvsize) {
1538a246b010SChuck Lever 		sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
15397c6e066eSChuck Lever 		sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2;
1540a246b010SChuck Lever 	}
15417c6e066eSChuck Lever 	if (transport->sndsize) {
1542a246b010SChuck Lever 		sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
15437c6e066eSChuck Lever 		sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2;
1544a246b010SChuck Lever 		sk->sk_write_space(sk);
1545a246b010SChuck Lever 	}
1546a246b010SChuck Lever }
1547a246b010SChuck Lever 
154843118c29SChuck Lever /**
1549470056c2SChuck Lever  * xs_udp_set_buffer_size - set send and receive limits
155043118c29SChuck Lever  * @xprt: generic transport
1551470056c2SChuck Lever  * @sndsize: requested size of send buffer, in bytes
1552470056c2SChuck Lever  * @rcvsize: requested size of receive buffer, in bytes
155343118c29SChuck Lever  *
1554470056c2SChuck Lever  * Set socket send and receive buffer size limits.
155543118c29SChuck Lever  */
1556470056c2SChuck Lever static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize)
155743118c29SChuck Lever {
15587c6e066eSChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
15597c6e066eSChuck Lever 
15607c6e066eSChuck Lever 	transport->sndsize = 0;
1561470056c2SChuck Lever 	if (sndsize)
15627c6e066eSChuck Lever 		transport->sndsize = sndsize + 1024;
15637c6e066eSChuck Lever 	transport->rcvsize = 0;
1564470056c2SChuck Lever 	if (rcvsize)
15657c6e066eSChuck Lever 		transport->rcvsize = rcvsize + 1024;
1566470056c2SChuck Lever 
1567470056c2SChuck Lever 	xs_udp_do_set_buffer_size(xprt);
156843118c29SChuck Lever }
156943118c29SChuck Lever 
157046c0ee8bSChuck Lever /**
157146c0ee8bSChuck Lever  * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
1572acf0a39fSChuck Lever  * @xprt: controlling transport
157346c0ee8bSChuck Lever  * @task: task that timed out
157446c0ee8bSChuck Lever  *
157546c0ee8bSChuck Lever  * Adjust the congestion window after a retransmit timeout has occurred.
157646c0ee8bSChuck Lever  */
15776a24dfb6STrond Myklebust static void xs_udp_timer(struct rpc_xprt *xprt, struct rpc_task *task)
157846c0ee8bSChuck Lever {
1579b5e92419STrond Myklebust 	spin_lock(&xprt->transport_lock);
15806a24dfb6STrond Myklebust 	xprt_adjust_cwnd(xprt, task, -ETIMEDOUT);
1581b5e92419STrond Myklebust 	spin_unlock(&xprt->transport_lock);
158246c0ee8bSChuck Lever }
158346c0ee8bSChuck Lever 
1584826799e6SJ. Bruce Fields static int xs_get_random_port(void)
1585b85d8806SChuck Lever {
1586826799e6SJ. Bruce Fields 	unsigned short min = xprt_min_resvport, max = xprt_max_resvport;
1587826799e6SJ. Bruce Fields 	unsigned short range;
1588826799e6SJ. Bruce Fields 	unsigned short rand;
1589826799e6SJ. Bruce Fields 
1590826799e6SJ. Bruce Fields 	if (max < min)
1591826799e6SJ. Bruce Fields 		return -EADDRINUSE;
1592826799e6SJ. Bruce Fields 	range = max - min + 1;
1593826799e6SJ. Bruce Fields 	rand = (unsigned short) prandom_u32() % range;
1594826799e6SJ. Bruce Fields 	return rand + min;
1595b85d8806SChuck Lever }
1596b85d8806SChuck Lever 
15974dda9c8aSTrond Myklebust static unsigned short xs_sock_getport(struct socket *sock)
15984dda9c8aSTrond Myklebust {
15994dda9c8aSTrond Myklebust 	struct sockaddr_storage buf;
16004dda9c8aSTrond Myklebust 	unsigned short port = 0;
16014dda9c8aSTrond Myklebust 
16029b2c45d4SDenys Vlasenko 	if (kernel_getsockname(sock, (struct sockaddr *)&buf) < 0)
16034dda9c8aSTrond Myklebust 		goto out;
16044dda9c8aSTrond Myklebust 	switch (buf.ss_family) {
16054dda9c8aSTrond Myklebust 	case AF_INET6:
16064dda9c8aSTrond Myklebust 		port = ntohs(((struct sockaddr_in6 *)&buf)->sin6_port);
16074dda9c8aSTrond Myklebust 		break;
16084dda9c8aSTrond Myklebust 	case AF_INET:
16094dda9c8aSTrond Myklebust 		port = ntohs(((struct sockaddr_in *)&buf)->sin_port);
16104dda9c8aSTrond Myklebust 	}
16114dda9c8aSTrond Myklebust out:
16124dda9c8aSTrond Myklebust 	return port;
16134dda9c8aSTrond Myklebust }
16144dda9c8aSTrond Myklebust 
16154dda9c8aSTrond Myklebust /**
161692200412SChuck Lever  * xs_set_port - reset the port number in the remote endpoint address
161792200412SChuck Lever  * @xprt: generic transport
161892200412SChuck Lever  * @port: new port number
161992200412SChuck Lever  *
162092200412SChuck Lever  */
162192200412SChuck Lever static void xs_set_port(struct rpc_xprt *xprt, unsigned short port)
162292200412SChuck Lever {
162392200412SChuck Lever 	dprintk("RPC:       setting port for xprt %p to %u\n", xprt, port);
1624c4efcb1dSChuck Lever 
16259dc3b095SChuck Lever 	rpc_set_port(xs_addr(xprt), port);
16269dc3b095SChuck Lever 	xs_update_peer_port(xprt);
162792200412SChuck Lever }
162892200412SChuck Lever 
16294dda9c8aSTrond Myklebust static void xs_set_srcport(struct sock_xprt *transport, struct socket *sock)
16304dda9c8aSTrond Myklebust {
1631e6237b6fSTrond Myklebust 	if (transport->srcport == 0 && transport->xprt.reuseport)
16324dda9c8aSTrond Myklebust 		transport->srcport = xs_sock_getport(sock);
16334dda9c8aSTrond Myklebust }
16344dda9c8aSTrond Myklebust 
1635826799e6SJ. Bruce Fields static int xs_get_srcport(struct sock_xprt *transport)
163667a391d7STrond Myklebust {
1637826799e6SJ. Bruce Fields 	int port = transport->srcport;
163867a391d7STrond Myklebust 
163967a391d7STrond Myklebust 	if (port == 0 && transport->xprt.resvport)
164067a391d7STrond Myklebust 		port = xs_get_random_port();
164167a391d7STrond Myklebust 	return port;
164267a391d7STrond Myklebust }
164367a391d7STrond Myklebust 
1644baaf4e48SPavel Emelyanov static unsigned short xs_next_srcport(struct sock_xprt *transport, unsigned short port)
164567a391d7STrond Myklebust {
1646fbfffbd5SChuck Lever 	if (transport->srcport != 0)
1647fbfffbd5SChuck Lever 		transport->srcport = 0;
164867a391d7STrond Myklebust 	if (!transport->xprt.resvport)
164967a391d7STrond Myklebust 		return 0;
165067a391d7STrond Myklebust 	if (port <= xprt_min_resvport || port > xprt_max_resvport)
165167a391d7STrond Myklebust 		return xprt_max_resvport;
165267a391d7STrond Myklebust 	return --port;
165367a391d7STrond Myklebust }
1654beb59b68SPavel Emelyanov static int xs_bind(struct sock_xprt *transport, struct socket *sock)
1655a246b010SChuck Lever {
1656beb59b68SPavel Emelyanov 	struct sockaddr_storage myaddr;
165767a391d7STrond Myklebust 	int err, nloop = 0;
1658826799e6SJ. Bruce Fields 	int port = xs_get_srcport(transport);
165967a391d7STrond Myklebust 	unsigned short last;
1660a246b010SChuck Lever 
16610f7a622cSChris Perl 	/*
16620f7a622cSChris Perl 	 * If we are asking for any ephemeral port (i.e. port == 0 &&
16630f7a622cSChris Perl 	 * transport->xprt.resvport == 0), don't bind.  Let the local
16640f7a622cSChris Perl 	 * port selection happen implicitly when the socket is used
16650f7a622cSChris Perl 	 * (for example at connect time).
16660f7a622cSChris Perl 	 *
16670f7a622cSChris Perl 	 * This ensures that we can continue to establish TCP
16680f7a622cSChris Perl 	 * connections even when all local ephemeral ports are already
16690f7a622cSChris Perl 	 * a part of some TCP connection.  This makes no difference
16700f7a622cSChris Perl 	 * for UDP sockets, but also doens't harm them.
16710f7a622cSChris Perl 	 *
16720f7a622cSChris Perl 	 * If we're asking for any reserved port (i.e. port == 0 &&
16730f7a622cSChris Perl 	 * transport->xprt.resvport == 1) xs_get_srcport above will
16740f7a622cSChris Perl 	 * ensure that port is non-zero and we will bind as needed.
16750f7a622cSChris Perl 	 */
1676826799e6SJ. Bruce Fields 	if (port <= 0)
1677826799e6SJ. Bruce Fields 		return port;
16780f7a622cSChris Perl 
1679beb59b68SPavel Emelyanov 	memcpy(&myaddr, &transport->srcaddr, transport->xprt.addrlen);
1680a246b010SChuck Lever 	do {
1681beb59b68SPavel Emelyanov 		rpc_set_port((struct sockaddr *)&myaddr, port);
1682e6242e92SSridhar Samudrala 		err = kernel_bind(sock, (struct sockaddr *)&myaddr,
1683beb59b68SPavel Emelyanov 				transport->xprt.addrlen);
1684a246b010SChuck Lever 		if (err == 0) {
1685fbfffbd5SChuck Lever 			transport->srcport = port;
1686d3bc9a1dSFrank van Maarseveen 			break;
1687a246b010SChuck Lever 		}
168867a391d7STrond Myklebust 		last = port;
1689baaf4e48SPavel Emelyanov 		port = xs_next_srcport(transport, port);
169067a391d7STrond Myklebust 		if (port > last)
169167a391d7STrond Myklebust 			nloop++;
169267a391d7STrond Myklebust 	} while (err == -EADDRINUSE && nloop != 2);
1693beb59b68SPavel Emelyanov 
16944232e863SChuck Lever 	if (myaddr.ss_family == AF_INET)
1695beb59b68SPavel Emelyanov 		dprintk("RPC:       %s %pI4:%u: %s (%d)\n", __func__,
1696beb59b68SPavel Emelyanov 				&((struct sockaddr_in *)&myaddr)->sin_addr,
1697beb59b68SPavel Emelyanov 				port, err ? "failed" : "ok", err);
1698beb59b68SPavel Emelyanov 	else
1699beb59b68SPavel Emelyanov 		dprintk("RPC:       %s %pI6:%u: %s (%d)\n", __func__,
1700beb59b68SPavel Emelyanov 				&((struct sockaddr_in6 *)&myaddr)->sin6_addr,
17017dc753f0SChuck Lever 				port, err ? "failed" : "ok", err);
1702a246b010SChuck Lever 	return err;
1703a246b010SChuck Lever }
1704a246b010SChuck Lever 
1705176e21eeSChuck Lever /*
1706176e21eeSChuck Lever  * We don't support autobind on AF_LOCAL sockets
1707176e21eeSChuck Lever  */
1708176e21eeSChuck Lever static void xs_local_rpcbind(struct rpc_task *task)
1709176e21eeSChuck Lever {
1710fb43d172STrond Myklebust 	xprt_set_bound(task->tk_xprt);
1711176e21eeSChuck Lever }
1712176e21eeSChuck Lever 
1713176e21eeSChuck Lever static void xs_local_set_port(struct rpc_xprt *xprt, unsigned short port)
1714176e21eeSChuck Lever {
1715176e21eeSChuck Lever }
1716a246b010SChuck Lever 
1717ed07536eSPeter Zijlstra #ifdef CONFIG_DEBUG_LOCK_ALLOC
1718ed07536eSPeter Zijlstra static struct lock_class_key xs_key[2];
1719ed07536eSPeter Zijlstra static struct lock_class_key xs_slock_key[2];
1720ed07536eSPeter Zijlstra 
1721176e21eeSChuck Lever static inline void xs_reclassify_socketu(struct socket *sock)
1722176e21eeSChuck Lever {
1723176e21eeSChuck Lever 	struct sock *sk = sock->sk;
1724176e21eeSChuck Lever 
1725176e21eeSChuck Lever 	sock_lock_init_class_and_name(sk, "slock-AF_LOCAL-RPC",
1726176e21eeSChuck Lever 		&xs_slock_key[1], "sk_lock-AF_LOCAL-RPC", &xs_key[1]);
1727176e21eeSChuck Lever }
1728176e21eeSChuck Lever 
17298945ee5eSChuck Lever static inline void xs_reclassify_socket4(struct socket *sock)
1730ed07536eSPeter Zijlstra {
1731ed07536eSPeter Zijlstra 	struct sock *sk = sock->sk;
17328945ee5eSChuck Lever 
17338945ee5eSChuck Lever 	sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC",
17348945ee5eSChuck Lever 		&xs_slock_key[0], "sk_lock-AF_INET-RPC", &xs_key[0]);
1735ed07536eSPeter Zijlstra }
17368945ee5eSChuck Lever 
17378945ee5eSChuck Lever static inline void xs_reclassify_socket6(struct socket *sock)
17388945ee5eSChuck Lever {
17398945ee5eSChuck Lever 	struct sock *sk = sock->sk;
17408945ee5eSChuck Lever 
17418945ee5eSChuck Lever 	sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC",
17428945ee5eSChuck Lever 		&xs_slock_key[1], "sk_lock-AF_INET6-RPC", &xs_key[1]);
1743ed07536eSPeter Zijlstra }
17446bc9638aSPavel Emelyanov 
17456bc9638aSPavel Emelyanov static inline void xs_reclassify_socket(int family, struct socket *sock)
17466bc9638aSPavel Emelyanov {
1747fafc4e1eSHannes Frederic Sowa 	if (WARN_ON_ONCE(!sock_allow_reclassification(sock->sk)))
17481b7a1819SWeston Andros Adamson 		return;
17491b7a1819SWeston Andros Adamson 
17504232e863SChuck Lever 	switch (family) {
1751176e21eeSChuck Lever 	case AF_LOCAL:
1752176e21eeSChuck Lever 		xs_reclassify_socketu(sock);
1753176e21eeSChuck Lever 		break;
17544232e863SChuck Lever 	case AF_INET:
17556bc9638aSPavel Emelyanov 		xs_reclassify_socket4(sock);
17564232e863SChuck Lever 		break;
17574232e863SChuck Lever 	case AF_INET6:
17586bc9638aSPavel Emelyanov 		xs_reclassify_socket6(sock);
17594232e863SChuck Lever 		break;
17604232e863SChuck Lever 	}
17616bc9638aSPavel Emelyanov }
1762ed07536eSPeter Zijlstra #else
17636bc9638aSPavel Emelyanov static inline void xs_reclassify_socket(int family, struct socket *sock)
17646bc9638aSPavel Emelyanov {
17656bc9638aSPavel Emelyanov }
1766ed07536eSPeter Zijlstra #endif
1767ed07536eSPeter Zijlstra 
176893dc41bdSNeilBrown static void xs_dummy_setup_socket(struct work_struct *work)
176993dc41bdSNeilBrown {
177093dc41bdSNeilBrown }
177193dc41bdSNeilBrown 
17726bc9638aSPavel Emelyanov static struct socket *xs_create_sock(struct rpc_xprt *xprt,
17734dda9c8aSTrond Myklebust 		struct sock_xprt *transport, int family, int type,
17744dda9c8aSTrond Myklebust 		int protocol, bool reuseport)
177522f79326SPavel Emelyanov {
1776a73881c9STrond Myklebust 	struct file *filp;
177722f79326SPavel Emelyanov 	struct socket *sock;
177822f79326SPavel Emelyanov 	int err;
177922f79326SPavel Emelyanov 
17806bc9638aSPavel Emelyanov 	err = __sock_create(xprt->xprt_net, family, type, protocol, &sock, 1);
178122f79326SPavel Emelyanov 	if (err < 0) {
178222f79326SPavel Emelyanov 		dprintk("RPC:       can't create %d transport socket (%d).\n",
178322f79326SPavel Emelyanov 				protocol, -err);
178422f79326SPavel Emelyanov 		goto out;
178522f79326SPavel Emelyanov 	}
17866bc9638aSPavel Emelyanov 	xs_reclassify_socket(family, sock);
178722f79326SPavel Emelyanov 
17884dda9c8aSTrond Myklebust 	if (reuseport)
1789fe31a326SChristoph Hellwig 		sock_set_reuseport(sock->sk);
17904dda9c8aSTrond Myklebust 
17914cea288aSBen Hutchings 	err = xs_bind(transport, sock);
17924cea288aSBen Hutchings 	if (err) {
179322f79326SPavel Emelyanov 		sock_release(sock);
179422f79326SPavel Emelyanov 		goto out;
179522f79326SPavel Emelyanov 	}
179622f79326SPavel Emelyanov 
1797a73881c9STrond Myklebust 	filp = sock_alloc_file(sock, O_NONBLOCK, NULL);
1798a73881c9STrond Myklebust 	if (IS_ERR(filp))
1799a73881c9STrond Myklebust 		return ERR_CAST(filp);
1800a73881c9STrond Myklebust 	transport->file = filp;
1801a73881c9STrond Myklebust 
180222f79326SPavel Emelyanov 	return sock;
180322f79326SPavel Emelyanov out:
180422f79326SPavel Emelyanov 	return ERR_PTR(err);
180522f79326SPavel Emelyanov }
180622f79326SPavel Emelyanov 
1807176e21eeSChuck Lever static int xs_local_finish_connecting(struct rpc_xprt *xprt,
1808176e21eeSChuck Lever 				      struct socket *sock)
1809176e21eeSChuck Lever {
1810176e21eeSChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
1811176e21eeSChuck Lever 									xprt);
1812176e21eeSChuck Lever 
1813176e21eeSChuck Lever 	if (!transport->inet) {
1814176e21eeSChuck Lever 		struct sock *sk = sock->sk;
1815176e21eeSChuck Lever 
1816176e21eeSChuck Lever 		write_lock_bh(&sk->sk_callback_lock);
1817176e21eeSChuck Lever 
1818176e21eeSChuck Lever 		xs_save_old_callbacks(transport, sk);
1819176e21eeSChuck Lever 
1820176e21eeSChuck Lever 		sk->sk_user_data = xprt;
1821a2648094STrond Myklebust 		sk->sk_data_ready = xs_data_ready;
1822176e21eeSChuck Lever 		sk->sk_write_space = xs_udp_write_space;
1823b4411457SEric Dumazet 		sock_set_flag(sk, SOCK_FASYNC);
18242118071dSTrond Myklebust 		sk->sk_error_report = xs_error_report;
1825176e21eeSChuck Lever 
1826176e21eeSChuck Lever 		xprt_clear_connected(xprt);
1827176e21eeSChuck Lever 
1828176e21eeSChuck Lever 		/* Reset to new socket */
1829176e21eeSChuck Lever 		transport->sock = sock;
1830176e21eeSChuck Lever 		transport->inet = sk;
1831176e21eeSChuck Lever 
1832176e21eeSChuck Lever 		write_unlock_bh(&sk->sk_callback_lock);
1833176e21eeSChuck Lever 	}
1834176e21eeSChuck Lever 
1835ae053551STrond Myklebust 	xs_stream_start_connect(transport);
18366c7a64e5STrond Myklebust 
1837176e21eeSChuck Lever 	return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, 0);
1838176e21eeSChuck Lever }
1839176e21eeSChuck Lever 
1840176e21eeSChuck Lever /**
1841176e21eeSChuck Lever  * xs_local_setup_socket - create AF_LOCAL socket, connect to a local endpoint
1842176e21eeSChuck Lever  * @transport: socket transport to connect
1843176e21eeSChuck Lever  */
1844dc107402SJ. Bruce Fields static int xs_local_setup_socket(struct sock_xprt *transport)
1845176e21eeSChuck Lever {
1846176e21eeSChuck Lever 	struct rpc_xprt *xprt = &transport->xprt;
1847a73881c9STrond Myklebust 	struct file *filp;
1848176e21eeSChuck Lever 	struct socket *sock;
184968e9a246SColin Ian King 	int status;
1850176e21eeSChuck Lever 
1851176e21eeSChuck Lever 	status = __sock_create(xprt->xprt_net, AF_LOCAL,
1852176e21eeSChuck Lever 					SOCK_STREAM, 0, &sock, 1);
1853176e21eeSChuck Lever 	if (status < 0) {
1854176e21eeSChuck Lever 		dprintk("RPC:       can't create AF_LOCAL "
1855176e21eeSChuck Lever 			"transport socket (%d).\n", -status);
1856176e21eeSChuck Lever 		goto out;
1857176e21eeSChuck Lever 	}
1858d1358917SStefan Hajnoczi 	xs_reclassify_socket(AF_LOCAL, sock);
1859176e21eeSChuck Lever 
1860a73881c9STrond Myklebust 	filp = sock_alloc_file(sock, O_NONBLOCK, NULL);
1861a73881c9STrond Myklebust 	if (IS_ERR(filp)) {
1862a73881c9STrond Myklebust 		status = PTR_ERR(filp);
1863a73881c9STrond Myklebust 		goto out;
1864a73881c9STrond Myklebust 	}
1865a73881c9STrond Myklebust 	transport->file = filp;
1866a73881c9STrond Myklebust 
1867176e21eeSChuck Lever 	dprintk("RPC:       worker connecting xprt %p via AF_LOCAL to %s\n",
1868176e21eeSChuck Lever 			xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1869176e21eeSChuck Lever 
1870176e21eeSChuck Lever 	status = xs_local_finish_connecting(xprt, sock);
187140b5ea0cSTrond Myklebust 	trace_rpc_socket_connect(xprt, sock, status);
1872176e21eeSChuck Lever 	switch (status) {
1873176e21eeSChuck Lever 	case 0:
1874176e21eeSChuck Lever 		dprintk("RPC:       xprt %p connected to %s\n",
1875176e21eeSChuck Lever 				xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
18763968a8a5SChuck Lever 		xprt->stat.connect_count++;
18773968a8a5SChuck Lever 		xprt->stat.connect_time += (long)jiffies -
18783968a8a5SChuck Lever 					   xprt->stat.connect_start;
1879176e21eeSChuck Lever 		xprt_set_connected(xprt);
18803601c4a9STrond Myklebust 	case -ENOBUFS:
1881176e21eeSChuck Lever 		break;
1882176e21eeSChuck Lever 	case -ENOENT:
1883176e21eeSChuck Lever 		dprintk("RPC:       xprt %p: socket %s does not exist\n",
1884176e21eeSChuck Lever 				xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1885176e21eeSChuck Lever 		break;
18864a20a988STrond Myklebust 	case -ECONNREFUSED:
18874a20a988STrond Myklebust 		dprintk("RPC:       xprt %p: connection refused for %s\n",
18884a20a988STrond Myklebust 				xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
18894a20a988STrond Myklebust 		break;
1890176e21eeSChuck Lever 	default:
1891176e21eeSChuck Lever 		printk(KERN_ERR "%s: unhandled error (%d) connecting to %s\n",
1892176e21eeSChuck Lever 				__func__, -status,
1893176e21eeSChuck Lever 				xprt->address_strings[RPC_DISPLAY_ADDR]);
1894176e21eeSChuck Lever 	}
1895176e21eeSChuck Lever 
1896176e21eeSChuck Lever out:
1897176e21eeSChuck Lever 	xprt_clear_connecting(xprt);
1898176e21eeSChuck Lever 	xprt_wake_pending_tasks(xprt, status);
1899dc107402SJ. Bruce Fields 	return status;
1900dc107402SJ. Bruce Fields }
1901dc107402SJ. Bruce Fields 
1902b6669737SLinus Torvalds static void xs_local_connect(struct rpc_xprt *xprt, struct rpc_task *task)
1903dc107402SJ. Bruce Fields {
1904dc107402SJ. Bruce Fields 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1905dc107402SJ. Bruce Fields 	int ret;
1906dc107402SJ. Bruce Fields 
1907dc107402SJ. Bruce Fields 	 if (RPC_IS_ASYNC(task)) {
1908dc107402SJ. Bruce Fields 		/*
1909dc107402SJ. Bruce Fields 		 * We want the AF_LOCAL connect to be resolved in the
1910dc107402SJ. Bruce Fields 		 * filesystem namespace of the process making the rpc
1911dc107402SJ. Bruce Fields 		 * call.  Thus we connect synchronously.
1912dc107402SJ. Bruce Fields 		 *
1913dc107402SJ. Bruce Fields 		 * If we want to support asynchronous AF_LOCAL calls,
1914dc107402SJ. Bruce Fields 		 * we'll need to figure out how to pass a namespace to
1915dc107402SJ. Bruce Fields 		 * connect.
1916dc107402SJ. Bruce Fields 		 */
19175ad64b36STrond Myklebust 		task->tk_rpc_status = -ENOTCONN;
1918dc107402SJ. Bruce Fields 		rpc_exit(task, -ENOTCONN);
1919dc107402SJ. Bruce Fields 		return;
1920dc107402SJ. Bruce Fields 	}
1921dc107402SJ. Bruce Fields 	ret = xs_local_setup_socket(transport);
1922dc107402SJ. Bruce Fields 	if (ret && !RPC_IS_SOFTCONN(task))
1923dc107402SJ. Bruce Fields 		msleep_interruptible(15000);
1924176e21eeSChuck Lever }
1925176e21eeSChuck Lever 
19263c87ef6eSJeff Layton #if IS_ENABLED(CONFIG_SUNRPC_SWAP)
1927d6e971d8SJeff Layton /*
1928d6e971d8SJeff Layton  * Note that this should be called with XPRT_LOCKED held (or when we otherwise
1929d6e971d8SJeff Layton  * know that we have exclusive access to the socket), to guard against
1930d6e971d8SJeff Layton  * races with xs_reset_transport.
1931d6e971d8SJeff Layton  */
1932a564b8f0SMel Gorman static void xs_set_memalloc(struct rpc_xprt *xprt)
1933a564b8f0SMel Gorman {
1934a564b8f0SMel Gorman 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
1935a564b8f0SMel Gorman 			xprt);
1936a564b8f0SMel Gorman 
1937d6e971d8SJeff Layton 	/*
1938d6e971d8SJeff Layton 	 * If there's no sock, then we have nothing to set. The
1939d6e971d8SJeff Layton 	 * reconnecting process will get it for us.
1940d6e971d8SJeff Layton 	 */
1941d6e971d8SJeff Layton 	if (!transport->inet)
1942d6e971d8SJeff Layton 		return;
19438e228133SJeff Layton 	if (atomic_read(&xprt->swapper))
1944a564b8f0SMel Gorman 		sk_set_memalloc(transport->inet);
1945a564b8f0SMel Gorman }
1946a564b8f0SMel Gorman 
1947a564b8f0SMel Gorman /**
1948d67fa4d8SJeff Layton  * xs_enable_swap - Tag this transport as being used for swap.
1949a564b8f0SMel Gorman  * @xprt: transport to tag
1950a564b8f0SMel Gorman  *
19518e228133SJeff Layton  * Take a reference to this transport on behalf of the rpc_clnt, and
19528e228133SJeff Layton  * optionally mark it for swapping if it wasn't already.
1953a564b8f0SMel Gorman  */
1954d67fa4d8SJeff Layton static int
1955d67fa4d8SJeff Layton xs_enable_swap(struct rpc_xprt *xprt)
1956a564b8f0SMel Gorman {
1957d6e971d8SJeff Layton 	struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt);
1958a564b8f0SMel Gorman 
1959d6e971d8SJeff Layton 	if (atomic_inc_return(&xprt->swapper) != 1)
1960d6e971d8SJeff Layton 		return 0;
1961d6e971d8SJeff Layton 	if (wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_KILLABLE))
1962d6e971d8SJeff Layton 		return -ERESTARTSYS;
1963d6e971d8SJeff Layton 	if (xs->inet)
1964d6e971d8SJeff Layton 		sk_set_memalloc(xs->inet);
1965d6e971d8SJeff Layton 	xprt_release_xprt(xprt, NULL);
19668e228133SJeff Layton 	return 0;
1967a564b8f0SMel Gorman }
1968a564b8f0SMel Gorman 
19698e228133SJeff Layton /**
1970d67fa4d8SJeff Layton  * xs_disable_swap - Untag this transport as being used for swap.
19718e228133SJeff Layton  * @xprt: transport to tag
19728e228133SJeff Layton  *
19738e228133SJeff Layton  * Drop a "swapper" reference to this xprt on behalf of the rpc_clnt. If the
19748e228133SJeff Layton  * swapper refcount goes to 0, untag the socket as a memalloc socket.
19758e228133SJeff Layton  */
1976d67fa4d8SJeff Layton static void
1977d67fa4d8SJeff Layton xs_disable_swap(struct rpc_xprt *xprt)
19788e228133SJeff Layton {
1979d6e971d8SJeff Layton 	struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt);
19808e228133SJeff Layton 
1981d6e971d8SJeff Layton 	if (!atomic_dec_and_test(&xprt->swapper))
1982d6e971d8SJeff Layton 		return;
1983d6e971d8SJeff Layton 	if (wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_KILLABLE))
1984d6e971d8SJeff Layton 		return;
1985d6e971d8SJeff Layton 	if (xs->inet)
1986d6e971d8SJeff Layton 		sk_clear_memalloc(xs->inet);
1987d6e971d8SJeff Layton 	xprt_release_xprt(xprt, NULL);
1988a564b8f0SMel Gorman }
1989a564b8f0SMel Gorman #else
1990a564b8f0SMel Gorman static void xs_set_memalloc(struct rpc_xprt *xprt)
1991a564b8f0SMel Gorman {
1992a564b8f0SMel Gorman }
1993d67fa4d8SJeff Layton 
1994d67fa4d8SJeff Layton static int
1995d67fa4d8SJeff Layton xs_enable_swap(struct rpc_xprt *xprt)
1996d67fa4d8SJeff Layton {
1997d67fa4d8SJeff Layton 	return -EINVAL;
1998d67fa4d8SJeff Layton }
1999d67fa4d8SJeff Layton 
2000d67fa4d8SJeff Layton static void
2001d67fa4d8SJeff Layton xs_disable_swap(struct rpc_xprt *xprt)
2002d67fa4d8SJeff Layton {
2003d67fa4d8SJeff Layton }
2004a564b8f0SMel Gorman #endif
2005a564b8f0SMel Gorman 
200616be2d20SChuck Lever static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
2007a246b010SChuck Lever {
200816be2d20SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2009edb267a6SChuck Lever 
2010ee0ac0c2SChuck Lever 	if (!transport->inet) {
2011b0d93ad5SChuck Lever 		struct sock *sk = sock->sk;
2012b0d93ad5SChuck Lever 
2013b0d93ad5SChuck Lever 		write_lock_bh(&sk->sk_callback_lock);
2014b0d93ad5SChuck Lever 
20152a9e1cfaSTrond Myklebust 		xs_save_old_callbacks(transport, sk);
20162a9e1cfaSTrond Myklebust 
2017b0d93ad5SChuck Lever 		sk->sk_user_data = xprt;
2018f9b2ee71STrond Myklebust 		sk->sk_data_ready = xs_data_ready;
2019b0d93ad5SChuck Lever 		sk->sk_write_space = xs_udp_write_space;
2020b4411457SEric Dumazet 		sock_set_flag(sk, SOCK_FASYNC);
2021b0d93ad5SChuck Lever 
2022b0d93ad5SChuck Lever 		xprt_set_connected(xprt);
2023b0d93ad5SChuck Lever 
2024b0d93ad5SChuck Lever 		/* Reset to new socket */
2025ee0ac0c2SChuck Lever 		transport->sock = sock;
2026ee0ac0c2SChuck Lever 		transport->inet = sk;
2027b0d93ad5SChuck Lever 
2028a564b8f0SMel Gorman 		xs_set_memalloc(xprt);
2029a564b8f0SMel Gorman 
2030b0d93ad5SChuck Lever 		write_unlock_bh(&sk->sk_callback_lock);
2031b0d93ad5SChuck Lever 	}
2032470056c2SChuck Lever 	xs_udp_do_set_buffer_size(xprt);
203302910177STrond Myklebust 
203402910177STrond Myklebust 	xprt->stat.connect_start = jiffies;
203516be2d20SChuck Lever }
203616be2d20SChuck Lever 
20378c14ff2aSPavel Emelyanov static void xs_udp_setup_socket(struct work_struct *work)
2038a246b010SChuck Lever {
2039a246b010SChuck Lever 	struct sock_xprt *transport =
2040a246b010SChuck Lever 		container_of(work, struct sock_xprt, connect_worker.work);
2041a246b010SChuck Lever 	struct rpc_xprt *xprt = &transport->xprt;
2042d099b8afSColin Ian King 	struct socket *sock;
2043b65c0310SPavel Emelyanov 	int status = -EIO;
2044a246b010SChuck Lever 
20458c14ff2aSPavel Emelyanov 	sock = xs_create_sock(xprt, transport,
20464dda9c8aSTrond Myklebust 			xs_addr(xprt)->sa_family, SOCK_DGRAM,
20474dda9c8aSTrond Myklebust 			IPPROTO_UDP, false);
2048b65c0310SPavel Emelyanov 	if (IS_ERR(sock))
2049a246b010SChuck Lever 		goto out;
205068e220bdSChuck Lever 
2051c740eff8SChuck Lever 	dprintk("RPC:       worker connecting xprt %p via %s to "
2052c740eff8SChuck Lever 				"%s (port %s)\n", xprt,
2053c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_PROTO],
2054c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_ADDR],
2055c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_PORT]);
205668e220bdSChuck Lever 
205768e220bdSChuck Lever 	xs_udp_finish_connecting(xprt, sock);
205840b5ea0cSTrond Myklebust 	trace_rpc_socket_connect(xprt, sock, 0);
2059a246b010SChuck Lever 	status = 0;
2060b0d93ad5SChuck Lever out:
2061b0d93ad5SChuck Lever 	xprt_clear_connecting(xprt);
2062cf76785dSTrond Myklebust 	xprt_unlock_connect(xprt, transport);
20637d1e8255STrond Myklebust 	xprt_wake_pending_tasks(xprt, status);
2064b0d93ad5SChuck Lever }
2065b0d93ad5SChuck Lever 
20664876cc77STrond Myklebust /**
20674876cc77STrond Myklebust  * xs_tcp_shutdown - gracefully shut down a TCP socket
20684876cc77STrond Myklebust  * @xprt: transport
20694876cc77STrond Myklebust  *
20704876cc77STrond Myklebust  * Initiates a graceful shutdown of the TCP socket by calling the
20714876cc77STrond Myklebust  * equivalent of shutdown(SHUT_RDWR);
20724876cc77STrond Myklebust  */
20734876cc77STrond Myklebust static void xs_tcp_shutdown(struct rpc_xprt *xprt)
20744876cc77STrond Myklebust {
20754876cc77STrond Myklebust 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
20764876cc77STrond Myklebust 	struct socket *sock = transport->sock;
20779b30889cSTrond Myklebust 	int skst = transport->inet ? transport->inet->sk_state : TCP_CLOSE;
20784876cc77STrond Myklebust 
20794876cc77STrond Myklebust 	if (sock == NULL)
20804876cc77STrond Myklebust 		return;
20819b30889cSTrond Myklebust 	switch (skst) {
20829b30889cSTrond Myklebust 	default:
20834876cc77STrond Myklebust 		kernel_sock_shutdown(sock, SHUT_RDWR);
20844876cc77STrond Myklebust 		trace_rpc_socket_shutdown(xprt, sock);
20859b30889cSTrond Myklebust 		break;
20869b30889cSTrond Myklebust 	case TCP_CLOSE:
20879b30889cSTrond Myklebust 	case TCP_TIME_WAIT:
20884876cc77STrond Myklebust 		xs_reset_transport(transport);
20894876cc77STrond Myklebust 	}
20909b30889cSTrond Myklebust }
20914876cc77STrond Myklebust 
20928d1b8c62STrond Myklebust static void xs_tcp_set_socket_timeouts(struct rpc_xprt *xprt,
20938d1b8c62STrond Myklebust 		struct socket *sock)
2094b0d93ad5SChuck Lever {
209516be2d20SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
20967196dbb0STrond Myklebust 	unsigned int keepidle;
20977196dbb0STrond Myklebust 	unsigned int keepcnt;
2098775f06abSTrond Myklebust 	unsigned int timeo;
20997f260e85STrond Myklebust 
2100b5e92419STrond Myklebust 	spin_lock(&xprt->transport_lock);
21017196dbb0STrond Myklebust 	keepidle = DIV_ROUND_UP(xprt->timeout->to_initval, HZ);
21027196dbb0STrond Myklebust 	keepcnt = xprt->timeout->to_retries + 1;
21037196dbb0STrond Myklebust 	timeo = jiffies_to_msecs(xprt->timeout->to_initval) *
21047196dbb0STrond Myklebust 		(xprt->timeout->to_retries + 1);
21057196dbb0STrond Myklebust 	clear_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state);
2106b5e92419STrond Myklebust 	spin_unlock(&xprt->transport_lock);
21077f260e85STrond Myklebust 
21087f260e85STrond Myklebust 	/* TCP Keepalive options */
2109ce3d9544SChristoph Hellwig 	sock_set_keepalive(sock->sk);
211071c48eb8SChristoph Hellwig 	tcp_sock_set_keepidle(sock->sk, keepidle);
21117f260e85STrond Myklebust 	kernel_setsockopt(sock, SOL_TCP, TCP_KEEPINTVL,
21127f260e85STrond Myklebust 			(char *)&keepidle, sizeof(keepidle));
21137f260e85STrond Myklebust 	kernel_setsockopt(sock, SOL_TCP, TCP_KEEPCNT,
21147f260e85STrond Myklebust 			(char *)&keepcnt, sizeof(keepcnt));
2115b0d93ad5SChuck Lever 
21168d1b8c62STrond Myklebust 	/* TCP user timeout (see RFC5482) */
2117c488aeadSChristoph Hellwig 	tcp_sock_set_user_timeout(sock->sk, timeo);
21188d1b8c62STrond Myklebust }
21198d1b8c62STrond Myklebust 
21207196dbb0STrond Myklebust static void xs_tcp_set_connect_timeout(struct rpc_xprt *xprt,
21217196dbb0STrond Myklebust 		unsigned long connect_timeout,
21227196dbb0STrond Myklebust 		unsigned long reconnect_timeout)
21237196dbb0STrond Myklebust {
21247196dbb0STrond Myklebust 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
21257196dbb0STrond Myklebust 	struct rpc_timeout to;
21267196dbb0STrond Myklebust 	unsigned long initval;
21277196dbb0STrond Myklebust 
2128b5e92419STrond Myklebust 	spin_lock(&xprt->transport_lock);
21297196dbb0STrond Myklebust 	if (reconnect_timeout < xprt->max_reconnect_timeout)
21307196dbb0STrond Myklebust 		xprt->max_reconnect_timeout = reconnect_timeout;
21317196dbb0STrond Myklebust 	if (connect_timeout < xprt->connect_timeout) {
21327196dbb0STrond Myklebust 		memcpy(&to, xprt->timeout, sizeof(to));
21337196dbb0STrond Myklebust 		initval = DIV_ROUND_UP(connect_timeout, to.to_retries + 1);
21347196dbb0STrond Myklebust 		/* Arbitrary lower limit */
21357196dbb0STrond Myklebust 		if (initval <  XS_TCP_INIT_REEST_TO << 1)
21367196dbb0STrond Myklebust 			initval = XS_TCP_INIT_REEST_TO << 1;
21377196dbb0STrond Myklebust 		to.to_initval = initval;
21387196dbb0STrond Myklebust 		to.to_maxval = initval;
21397196dbb0STrond Myklebust 		memcpy(&transport->tcp_timeout, &to,
21407196dbb0STrond Myklebust 				sizeof(transport->tcp_timeout));
21417196dbb0STrond Myklebust 		xprt->timeout = &transport->tcp_timeout;
21427196dbb0STrond Myklebust 		xprt->connect_timeout = connect_timeout;
21437196dbb0STrond Myklebust 	}
21447196dbb0STrond Myklebust 	set_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state);
2145b5e92419STrond Myklebust 	spin_unlock(&xprt->transport_lock);
21467196dbb0STrond Myklebust }
21477196dbb0STrond Myklebust 
21488d1b8c62STrond Myklebust static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
21498d1b8c62STrond Myklebust {
21508d1b8c62STrond Myklebust 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
21518d1b8c62STrond Myklebust 	int ret = -ENOTCONN;
21528d1b8c62STrond Myklebust 
21538d1b8c62STrond Myklebust 	if (!transport->inet) {
21548d1b8c62STrond Myklebust 		struct sock *sk = sock->sk;
21558d1b8c62STrond Myklebust 		unsigned int addr_pref = IPV6_PREFER_SRC_PUBLIC;
21568d1b8c62STrond Myklebust 
2157d88e4d82SNeilBrown 		/* Avoid temporary address, they are bad for long-lived
2158d88e4d82SNeilBrown 		 * connections such as NFS mounts.
2159d88e4d82SNeilBrown 		 * RFC4941, section 3.6 suggests that:
2160d88e4d82SNeilBrown 		 *    Individual applications, which have specific
2161d88e4d82SNeilBrown 		 *    knowledge about the normal duration of connections,
2162d88e4d82SNeilBrown 		 *    MAY override this as appropriate.
2163d88e4d82SNeilBrown 		 */
2164d88e4d82SNeilBrown 		kernel_setsockopt(sock, SOL_IPV6, IPV6_ADDR_PREFERENCES,
2165d88e4d82SNeilBrown 				(char *)&addr_pref, sizeof(addr_pref));
2166d88e4d82SNeilBrown 
21678d1b8c62STrond Myklebust 		xs_tcp_set_socket_timeouts(xprt, sock);
2168775f06abSTrond Myklebust 
2169b0d93ad5SChuck Lever 		write_lock_bh(&sk->sk_callback_lock);
2170b0d93ad5SChuck Lever 
21712a9e1cfaSTrond Myklebust 		xs_save_old_callbacks(transport, sk);
21722a9e1cfaSTrond Myklebust 
2173b0d93ad5SChuck Lever 		sk->sk_user_data = xprt;
21745157b956STrond Myklebust 		sk->sk_data_ready = xs_data_ready;
2175b0d93ad5SChuck Lever 		sk->sk_state_change = xs_tcp_state_change;
2176b0d93ad5SChuck Lever 		sk->sk_write_space = xs_tcp_write_space;
2177b4411457SEric Dumazet 		sock_set_flag(sk, SOCK_FASYNC);
21782118071dSTrond Myklebust 		sk->sk_error_report = xs_error_report;
21793167e12cSChuck Lever 
21803167e12cSChuck Lever 		/* socket options */
21813167e12cSChuck Lever 		sock_reset_flag(sk, SOCK_LINGER);
21823167e12cSChuck Lever 		tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF;
2183b0d93ad5SChuck Lever 
2184b0d93ad5SChuck Lever 		xprt_clear_connected(xprt);
2185b0d93ad5SChuck Lever 
2186b0d93ad5SChuck Lever 		/* Reset to new socket */
2187ee0ac0c2SChuck Lever 		transport->sock = sock;
2188ee0ac0c2SChuck Lever 		transport->inet = sk;
2189b0d93ad5SChuck Lever 
2190b0d93ad5SChuck Lever 		write_unlock_bh(&sk->sk_callback_lock);
2191b0d93ad5SChuck Lever 	}
2192b0d93ad5SChuck Lever 
219301d37c42STrond Myklebust 	if (!xprt_bound(xprt))
2194fe19a96bSTrond Myklebust 		goto out;
219501d37c42STrond Myklebust 
2196a564b8f0SMel Gorman 	xs_set_memalloc(xprt);
2197a564b8f0SMel Gorman 
2198ae053551STrond Myklebust 	xs_stream_start_connect(transport);
2199e1806c7bSTrond Myklebust 
2200b0d93ad5SChuck Lever 	/* Tell the socket layer to start connecting... */
22010fdea1e8STrond Myklebust 	set_bit(XPRT_SOCK_CONNECTING, &transport->sock_state);
2202fe19a96bSTrond Myklebust 	ret = kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK);
2203fe19a96bSTrond Myklebust 	switch (ret) {
2204fe19a96bSTrond Myklebust 	case 0:
22054dda9c8aSTrond Myklebust 		xs_set_srcport(transport, sock);
2206e9d47639SGustavo A. R. Silva 		/* fall through */
2207fe19a96bSTrond Myklebust 	case -EINPROGRESS:
2208fe19a96bSTrond Myklebust 		/* SYN_SENT! */
2209fe19a96bSTrond Myklebust 		if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
2210fe19a96bSTrond Myklebust 			xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
22111f4c17a0STrond Myklebust 		break;
22121f4c17a0STrond Myklebust 	case -EADDRNOTAVAIL:
22131f4c17a0STrond Myklebust 		/* Source port number is unavailable. Try a new one! */
22141f4c17a0STrond Myklebust 		transport->srcport = 0;
2215fe19a96bSTrond Myklebust 	}
2216fe19a96bSTrond Myklebust out:
2217fe19a96bSTrond Myklebust 	return ret;
221816be2d20SChuck Lever }
221916be2d20SChuck Lever 
222016be2d20SChuck Lever /**
2221b61d59ffSTrond Myklebust  * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint
2222acf0a39fSChuck Lever  * @work: queued work item
222316be2d20SChuck Lever  *
222416be2d20SChuck Lever  * Invoked by a work queue tasklet.
222516be2d20SChuck Lever  */
2226cdd518d5SPavel Emelyanov static void xs_tcp_setup_socket(struct work_struct *work)
222716be2d20SChuck Lever {
2228cdd518d5SPavel Emelyanov 	struct sock_xprt *transport =
2229cdd518d5SPavel Emelyanov 		container_of(work, struct sock_xprt, connect_worker.work);
223016be2d20SChuck Lever 	struct socket *sock = transport->sock;
2231a9f5f0f7SPavel Emelyanov 	struct rpc_xprt *xprt = &transport->xprt;
2232b61d59ffSTrond Myklebust 	int status = -EIO;
223316be2d20SChuck Lever 
223416be2d20SChuck Lever 	if (!sock) {
2235cdd518d5SPavel Emelyanov 		sock = xs_create_sock(xprt, transport,
22364dda9c8aSTrond Myklebust 				xs_addr(xprt)->sa_family, SOCK_STREAM,
22374dda9c8aSTrond Myklebust 				IPPROTO_TCP, true);
2238b61d59ffSTrond Myklebust 		if (IS_ERR(sock)) {
2239b61d59ffSTrond Myklebust 			status = PTR_ERR(sock);
224016be2d20SChuck Lever 			goto out;
224116be2d20SChuck Lever 		}
22427d1e8255STrond Myklebust 	}
22437d1e8255STrond Myklebust 
2244c740eff8SChuck Lever 	dprintk("RPC:       worker connecting xprt %p via %s to "
2245c740eff8SChuck Lever 				"%s (port %s)\n", xprt,
2246c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_PROTO],
2247c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_ADDR],
2248c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_PORT]);
224916be2d20SChuck Lever 
225016be2d20SChuck Lever 	status = xs_tcp_finish_connecting(xprt, sock);
225140b5ea0cSTrond Myklebust 	trace_rpc_socket_connect(xprt, sock, status);
2252a246b010SChuck Lever 	dprintk("RPC:       %p connect status %d connected %d sock state %d\n",
225346121cf7SChuck Lever 			xprt, -status, xprt_connected(xprt),
225446121cf7SChuck Lever 			sock->sk->sk_state);
2255a246b010SChuck Lever 	switch (status) {
2256f75e6745STrond Myklebust 	default:
2257f75e6745STrond Myklebust 		printk("%s: connect returned unhandled error %d\n",
2258f75e6745STrond Myklebust 			__func__, status);
2259e9d47639SGustavo A. R. Silva 		/* fall through */
2260f75e6745STrond Myklebust 	case -EADDRNOTAVAIL:
2261f75e6745STrond Myklebust 		/* We're probably in TIME_WAIT. Get rid of existing socket,
2262f75e6745STrond Myklebust 		 * and retry
2263f75e6745STrond Myklebust 		 */
2264a519fc7aSTrond Myklebust 		xs_tcp_force_close(xprt);
226588b5ed73STrond Myklebust 		break;
22662a491991STrond Myklebust 	case 0:
2267a246b010SChuck Lever 	case -EINPROGRESS:
2268a246b010SChuck Lever 	case -EALREADY:
2269718ba5b8STrond Myklebust 		xprt_unlock_connect(xprt, transport);
22707d1e8255STrond Myklebust 		return;
22719fcfe0c8STrond Myklebust 	case -EINVAL:
22729fcfe0c8STrond Myklebust 		/* Happens, for instance, if the user specified a link
22739fcfe0c8STrond Myklebust 		 * local IPv6 address without a scope-id.
22749fcfe0c8STrond Myklebust 		 */
22753ed5e2a2STrond Myklebust 	case -ECONNREFUSED:
22763ed5e2a2STrond Myklebust 	case -ECONNRESET:
2277eb5b46faSTrond Myklebust 	case -ENETDOWN:
22783ed5e2a2STrond Myklebust 	case -ENETUNREACH:
22794ba161a7STrond Myklebust 	case -EHOSTUNREACH:
22803913c78cSTrond Myklebust 	case -EADDRINUSE:
22813601c4a9STrond Myklebust 	case -ENOBUFS:
22826ea44adcSNeilBrown 		/*
22836ea44adcSNeilBrown 		 * xs_tcp_force_close() wakes tasks with -EIO.
22846ea44adcSNeilBrown 		 * We need to wake them first to ensure the
22856ea44adcSNeilBrown 		 * correct error code.
22866ea44adcSNeilBrown 		 */
22876ea44adcSNeilBrown 		xprt_wake_pending_tasks(xprt, status);
22884efdd92cSTrond Myklebust 		xs_tcp_force_close(xprt);
22899fcfe0c8STrond Myklebust 		goto out;
22908a2cec29STrond Myklebust 	}
22912a491991STrond Myklebust 	status = -EAGAIN;
2292a246b010SChuck Lever out:
22932226feb6SChuck Lever 	xprt_clear_connecting(xprt);
2294cf76785dSTrond Myklebust 	xprt_unlock_connect(xprt, transport);
22957d1e8255STrond Myklebust 	xprt_wake_pending_tasks(xprt, status);
2296a246b010SChuck Lever }
2297a246b010SChuck Lever 
229868e220bdSChuck Lever /**
22999903cd1cSChuck Lever  * xs_connect - connect a socket to a remote endpoint
23001b092092STrond Myklebust  * @xprt: pointer to transport structure
23019903cd1cSChuck Lever  * @task: address of RPC task that manages state of connect request
23029903cd1cSChuck Lever  *
23039903cd1cSChuck Lever  * TCP: If the remote end dropped the connection, delay reconnecting.
230403bf4b70SChuck Lever  *
230503bf4b70SChuck Lever  * UDP socket connects are synchronous, but we use a work queue anyway
230603bf4b70SChuck Lever  * to guarantee that even unprivileged user processes can set up a
230703bf4b70SChuck Lever  * socket on a privileged port.
230803bf4b70SChuck Lever  *
230903bf4b70SChuck Lever  * If a UDP socket connect fails, the delay behavior here prevents
231003bf4b70SChuck Lever  * retry floods (hard mounts).
23119903cd1cSChuck Lever  */
23121b092092STrond Myklebust static void xs_connect(struct rpc_xprt *xprt, struct rpc_task *task)
2313a246b010SChuck Lever {
2314ee0ac0c2SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
231502910177STrond Myklebust 	unsigned long delay = 0;
2316a246b010SChuck Lever 
2317718ba5b8STrond Myklebust 	WARN_ON_ONCE(!xprt_lock_connect(xprt, task, transport));
2318718ba5b8STrond Myklebust 
231999b1a4c3STrond Myklebust 	if (transport->sock != NULL) {
232046121cf7SChuck Lever 		dprintk("RPC:       xs_connect delayed xprt %p for %lu "
232146121cf7SChuck Lever 				"seconds\n",
232203bf4b70SChuck Lever 				xprt, xprt->reestablish_timeout / HZ);
232399b1a4c3STrond Myklebust 
232499b1a4c3STrond Myklebust 		/* Start by resetting any existing state */
232599b1a4c3STrond Myklebust 		xs_reset_transport(transport);
232699b1a4c3STrond Myklebust 
2327675dd90aSChuck Lever 		delay = xprt_reconnect_delay(xprt);
2328675dd90aSChuck Lever 		xprt_reconnect_backoff(xprt, XS_TCP_INIT_REEST_TO);
232902910177STrond Myklebust 
233002910177STrond Myklebust 	} else
23319903cd1cSChuck Lever 		dprintk("RPC:       xs_connect scheduled xprt %p\n", xprt);
233202910177STrond Myklebust 
233340a5f1b1STrond Myklebust 	queue_delayed_work(xprtiod_workqueue,
233402910177STrond Myklebust 			&transport->connect_worker,
233502910177STrond Myklebust 			delay);
2336a246b010SChuck Lever }
2337a246b010SChuck Lever 
23384f8943f8STrond Myklebust static void xs_wake_disconnect(struct sock_xprt *transport)
23394f8943f8STrond Myklebust {
23404f8943f8STrond Myklebust 	if (test_and_clear_bit(XPRT_SOCK_WAKE_DISCONNECT, &transport->sock_state))
23414f8943f8STrond Myklebust 		xs_tcp_force_close(&transport->xprt);
23424f8943f8STrond Myklebust }
23434f8943f8STrond Myklebust 
23444f8943f8STrond Myklebust static void xs_wake_write(struct sock_xprt *transport)
23454f8943f8STrond Myklebust {
23464f8943f8STrond Myklebust 	if (test_and_clear_bit(XPRT_SOCK_WAKE_WRITE, &transport->sock_state))
23474f8943f8STrond Myklebust 		xprt_write_space(&transport->xprt);
23484f8943f8STrond Myklebust }
23494f8943f8STrond Myklebust 
23504f8943f8STrond Myklebust static void xs_wake_error(struct sock_xprt *transport)
23514f8943f8STrond Myklebust {
23524f8943f8STrond Myklebust 	int sockerr;
23534f8943f8STrond Myklebust 
23544f8943f8STrond Myklebust 	if (!test_bit(XPRT_SOCK_WAKE_ERROR, &transport->sock_state))
23554f8943f8STrond Myklebust 		return;
23564f8943f8STrond Myklebust 	mutex_lock(&transport->recv_mutex);
23574f8943f8STrond Myklebust 	if (transport->sock == NULL)
23584f8943f8STrond Myklebust 		goto out;
23594f8943f8STrond Myklebust 	if (!test_and_clear_bit(XPRT_SOCK_WAKE_ERROR, &transport->sock_state))
23604f8943f8STrond Myklebust 		goto out;
2361af84537dSBenjamin Coddington 	sockerr = xchg(&transport->xprt_err, 0);
23624f8943f8STrond Myklebust 	if (sockerr < 0)
23634f8943f8STrond Myklebust 		xprt_wake_pending_tasks(&transport->xprt, sockerr);
23644f8943f8STrond Myklebust out:
23654f8943f8STrond Myklebust 	mutex_unlock(&transport->recv_mutex);
23664f8943f8STrond Myklebust }
23674f8943f8STrond Myklebust 
23684f8943f8STrond Myklebust static void xs_wake_pending(struct sock_xprt *transport)
23694f8943f8STrond Myklebust {
23704f8943f8STrond Myklebust 	if (test_and_clear_bit(XPRT_SOCK_WAKE_PENDING, &transport->sock_state))
23714f8943f8STrond Myklebust 		xprt_wake_pending_tasks(&transport->xprt, -EAGAIN);
23724f8943f8STrond Myklebust }
23734f8943f8STrond Myklebust 
23744f8943f8STrond Myklebust static void xs_error_handle(struct work_struct *work)
23754f8943f8STrond Myklebust {
23764f8943f8STrond Myklebust 	struct sock_xprt *transport = container_of(work,
23774f8943f8STrond Myklebust 			struct sock_xprt, error_worker);
23784f8943f8STrond Myklebust 
23794f8943f8STrond Myklebust 	xs_wake_disconnect(transport);
23804f8943f8STrond Myklebust 	xs_wake_write(transport);
23814f8943f8STrond Myklebust 	xs_wake_error(transport);
23824f8943f8STrond Myklebust 	xs_wake_pending(transport);
23834f8943f8STrond Myklebust }
23844f8943f8STrond Myklebust 
2385262ca07dSChuck Lever /**
2386176e21eeSChuck Lever  * xs_local_print_stats - display AF_LOCAL socket-specifc stats
2387176e21eeSChuck Lever  * @xprt: rpc_xprt struct containing statistics
2388176e21eeSChuck Lever  * @seq: output file
2389176e21eeSChuck Lever  *
2390176e21eeSChuck Lever  */
2391176e21eeSChuck Lever static void xs_local_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2392176e21eeSChuck Lever {
2393176e21eeSChuck Lever 	long idle_time = 0;
2394176e21eeSChuck Lever 
2395176e21eeSChuck Lever 	if (xprt_connected(xprt))
2396176e21eeSChuck Lever 		idle_time = (long)(jiffies - xprt->last_used) / HZ;
2397176e21eeSChuck Lever 
2398176e21eeSChuck Lever 	seq_printf(seq, "\txprt:\tlocal %lu %lu %lu %ld %lu %lu %lu "
239915a45206SAndy Adamson 			"%llu %llu %lu %llu %llu\n",
2400176e21eeSChuck Lever 			xprt->stat.bind_count,
2401176e21eeSChuck Lever 			xprt->stat.connect_count,
24028440a886SChuck Lever 			xprt->stat.connect_time / HZ,
2403176e21eeSChuck Lever 			idle_time,
2404176e21eeSChuck Lever 			xprt->stat.sends,
2405176e21eeSChuck Lever 			xprt->stat.recvs,
2406176e21eeSChuck Lever 			xprt->stat.bad_xids,
2407176e21eeSChuck Lever 			xprt->stat.req_u,
240815a45206SAndy Adamson 			xprt->stat.bklog_u,
240915a45206SAndy Adamson 			xprt->stat.max_slots,
241015a45206SAndy Adamson 			xprt->stat.sending_u,
241115a45206SAndy Adamson 			xprt->stat.pending_u);
2412176e21eeSChuck Lever }
2413176e21eeSChuck Lever 
2414176e21eeSChuck Lever /**
2415262ca07dSChuck Lever  * xs_udp_print_stats - display UDP socket-specifc stats
2416262ca07dSChuck Lever  * @xprt: rpc_xprt struct containing statistics
2417262ca07dSChuck Lever  * @seq: output file
2418262ca07dSChuck Lever  *
2419262ca07dSChuck Lever  */
2420262ca07dSChuck Lever static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2421262ca07dSChuck Lever {
2422c8475461SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2423c8475461SChuck Lever 
242415a45206SAndy Adamson 	seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %llu %llu "
242515a45206SAndy Adamson 			"%lu %llu %llu\n",
2426fbfffbd5SChuck Lever 			transport->srcport,
2427262ca07dSChuck Lever 			xprt->stat.bind_count,
2428262ca07dSChuck Lever 			xprt->stat.sends,
2429262ca07dSChuck Lever 			xprt->stat.recvs,
2430262ca07dSChuck Lever 			xprt->stat.bad_xids,
2431262ca07dSChuck Lever 			xprt->stat.req_u,
243215a45206SAndy Adamson 			xprt->stat.bklog_u,
243315a45206SAndy Adamson 			xprt->stat.max_slots,
243415a45206SAndy Adamson 			xprt->stat.sending_u,
243515a45206SAndy Adamson 			xprt->stat.pending_u);
2436262ca07dSChuck Lever }
2437262ca07dSChuck Lever 
2438262ca07dSChuck Lever /**
2439262ca07dSChuck Lever  * xs_tcp_print_stats - display TCP socket-specifc stats
2440262ca07dSChuck Lever  * @xprt: rpc_xprt struct containing statistics
2441262ca07dSChuck Lever  * @seq: output file
2442262ca07dSChuck Lever  *
2443262ca07dSChuck Lever  */
2444262ca07dSChuck Lever static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2445262ca07dSChuck Lever {
2446c8475461SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2447262ca07dSChuck Lever 	long idle_time = 0;
2448262ca07dSChuck Lever 
2449262ca07dSChuck Lever 	if (xprt_connected(xprt))
2450262ca07dSChuck Lever 		idle_time = (long)(jiffies - xprt->last_used) / HZ;
2451262ca07dSChuck Lever 
245215a45206SAndy Adamson 	seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu "
245315a45206SAndy Adamson 			"%llu %llu %lu %llu %llu\n",
2454fbfffbd5SChuck Lever 			transport->srcport,
2455262ca07dSChuck Lever 			xprt->stat.bind_count,
2456262ca07dSChuck Lever 			xprt->stat.connect_count,
24578440a886SChuck Lever 			xprt->stat.connect_time / HZ,
2458262ca07dSChuck Lever 			idle_time,
2459262ca07dSChuck Lever 			xprt->stat.sends,
2460262ca07dSChuck Lever 			xprt->stat.recvs,
2461262ca07dSChuck Lever 			xprt->stat.bad_xids,
2462262ca07dSChuck Lever 			xprt->stat.req_u,
246315a45206SAndy Adamson 			xprt->stat.bklog_u,
246415a45206SAndy Adamson 			xprt->stat.max_slots,
246515a45206SAndy Adamson 			xprt->stat.sending_u,
246615a45206SAndy Adamson 			xprt->stat.pending_u);
2467262ca07dSChuck Lever }
2468262ca07dSChuck Lever 
24694cfc7e60SRahul Iyer /*
24704cfc7e60SRahul Iyer  * Allocate a bunch of pages for a scratch buffer for the rpc code. The reason
24714cfc7e60SRahul Iyer  * we allocate pages instead doing a kmalloc like rpc_malloc is because we want
24724cfc7e60SRahul Iyer  * to use the server side send routines.
24734cfc7e60SRahul Iyer  */
24745fe6eaa1SChuck Lever static int bc_malloc(struct rpc_task *task)
24754cfc7e60SRahul Iyer {
24765fe6eaa1SChuck Lever 	struct rpc_rqst *rqst = task->tk_rqstp;
24775fe6eaa1SChuck Lever 	size_t size = rqst->rq_callsize;
24784cfc7e60SRahul Iyer 	struct page *page;
24794cfc7e60SRahul Iyer 	struct rpc_buffer *buf;
24804cfc7e60SRahul Iyer 
24815fe6eaa1SChuck Lever 	if (size > PAGE_SIZE - sizeof(struct rpc_buffer)) {
24825fe6eaa1SChuck Lever 		WARN_ONCE(1, "xprtsock: large bc buffer request (size %zu)\n",
24835fe6eaa1SChuck Lever 			  size);
24845fe6eaa1SChuck Lever 		return -EINVAL;
24855fe6eaa1SChuck Lever 	}
24864cfc7e60SRahul Iyer 
2487b8a13d03SWeston Andros Adamson 	page = alloc_page(GFP_KERNEL);
24884cfc7e60SRahul Iyer 	if (!page)
24895fe6eaa1SChuck Lever 		return -ENOMEM;
24904cfc7e60SRahul Iyer 
24914cfc7e60SRahul Iyer 	buf = page_address(page);
24924cfc7e60SRahul Iyer 	buf->len = PAGE_SIZE;
24934cfc7e60SRahul Iyer 
24945fe6eaa1SChuck Lever 	rqst->rq_buffer = buf->data;
249518e601d6SJeff Layton 	rqst->rq_rbuffer = (char *)rqst->rq_buffer + rqst->rq_callsize;
24965fe6eaa1SChuck Lever 	return 0;
24974cfc7e60SRahul Iyer }
24984cfc7e60SRahul Iyer 
24994cfc7e60SRahul Iyer /*
25004cfc7e60SRahul Iyer  * Free the space allocated in the bc_alloc routine
25014cfc7e60SRahul Iyer  */
25023435c74aSChuck Lever static void bc_free(struct rpc_task *task)
25034cfc7e60SRahul Iyer {
25043435c74aSChuck Lever 	void *buffer = task->tk_rqstp->rq_buffer;
25054cfc7e60SRahul Iyer 	struct rpc_buffer *buf;
25064cfc7e60SRahul Iyer 
25074cfc7e60SRahul Iyer 	buf = container_of(buffer, struct rpc_buffer, data);
25084cfc7e60SRahul Iyer 	free_page((unsigned long)buf);
25094cfc7e60SRahul Iyer }
25104cfc7e60SRahul Iyer 
25114cfc7e60SRahul Iyer static int bc_sendto(struct rpc_rqst *req)
25124cfc7e60SRahul Iyer {
2513da1661b9SChuck Lever 	struct xdr_buf *xdr = &req->rq_snd_buf;
25144cfc7e60SRahul Iyer 	struct sock_xprt *transport =
2515067fb11bSChuck Lever 			container_of(req->rq_xprt, struct sock_xprt, xprt);
2516067fb11bSChuck Lever 	struct msghdr msg = {
2517da1661b9SChuck Lever 		.msg_flags	= 0,
2518067fb11bSChuck Lever 	};
2519067fb11bSChuck Lever 	rpc_fraghdr marker = cpu_to_be32(RPC_LAST_STREAM_FRAGMENT |
2520da1661b9SChuck Lever 					 (u32)xdr->len);
2521da1661b9SChuck Lever 	unsigned int sent = 0;
2522da1661b9SChuck Lever 	int err;
25234cfc7e60SRahul Iyer 
25248729aabaSChuck Lever 	req->rq_xtime = ktime_get();
2525da1661b9SChuck Lever 	err = xprt_sock_sendmsg(transport->sock, &msg, xdr, 0, marker, &sent);
2526da1661b9SChuck Lever 	xdr_free_bvec(xdr);
2527da1661b9SChuck Lever 	if (err < 0 || sent != (xdr->len + sizeof(marker)))
2528067fb11bSChuck Lever 		return -EAGAIN;
2529da1661b9SChuck Lever 	return sent;
25304cfc7e60SRahul Iyer }
25314cfc7e60SRahul Iyer 
25324cfc7e60SRahul Iyer /*
25334cfc7e60SRahul Iyer  * The send routine. Borrows from svc_send
25344cfc7e60SRahul Iyer  */
2535adfa7144STrond Myklebust static int bc_send_request(struct rpc_rqst *req)
25364cfc7e60SRahul Iyer {
25374cfc7e60SRahul Iyer 	struct svc_xprt	*xprt;
25387fc56136SAndrzej Hajda 	int len;
25394cfc7e60SRahul Iyer 
25404cfc7e60SRahul Iyer 	/*
25414cfc7e60SRahul Iyer 	 * Get the server socket associated with this callback xprt
25424cfc7e60SRahul Iyer 	 */
25434cfc7e60SRahul Iyer 	xprt = req->rq_xprt->bc_xprt;
25444cfc7e60SRahul Iyer 
25454cfc7e60SRahul Iyer 	/*
25464cfc7e60SRahul Iyer 	 * Grab the mutex to serialize data as the connection is shared
25474cfc7e60SRahul Iyer 	 * with the fore channel
25484cfc7e60SRahul Iyer 	 */
2549c544577dSTrond Myklebust 	mutex_lock(&xprt->xpt_mutex);
25504cfc7e60SRahul Iyer 	if (test_bit(XPT_DEAD, &xprt->xpt_flags))
25514cfc7e60SRahul Iyer 		len = -ENOTCONN;
25524cfc7e60SRahul Iyer 	else
25534cfc7e60SRahul Iyer 		len = bc_sendto(req);
25544cfc7e60SRahul Iyer 	mutex_unlock(&xprt->xpt_mutex);
25554cfc7e60SRahul Iyer 
25564cfc7e60SRahul Iyer 	if (len > 0)
25574cfc7e60SRahul Iyer 		len = 0;
25584cfc7e60SRahul Iyer 
25594cfc7e60SRahul Iyer 	return len;
25604cfc7e60SRahul Iyer }
25614cfc7e60SRahul Iyer 
25624cfc7e60SRahul Iyer /*
25634cfc7e60SRahul Iyer  * The close routine. Since this is client initiated, we do nothing
25644cfc7e60SRahul Iyer  */
25654cfc7e60SRahul Iyer 
25664cfc7e60SRahul Iyer static void bc_close(struct rpc_xprt *xprt)
25674cfc7e60SRahul Iyer {
25686221f1d9SChuck Lever 	xprt_disconnect_done(xprt);
25694cfc7e60SRahul Iyer }
25704cfc7e60SRahul Iyer 
25714cfc7e60SRahul Iyer /*
25724cfc7e60SRahul Iyer  * The xprt destroy routine. Again, because this connection is client
25734cfc7e60SRahul Iyer  * initiated, we do nothing
25744cfc7e60SRahul Iyer  */
25754cfc7e60SRahul Iyer 
25764cfc7e60SRahul Iyer static void bc_destroy(struct rpc_xprt *xprt)
25774cfc7e60SRahul Iyer {
257847f72efaSKinglong Mee 	dprintk("RPC:       bc_destroy xprt %p\n", xprt);
257947f72efaSKinglong Mee 
258047f72efaSKinglong Mee 	xs_xprt_free(xprt);
258147f72efaSKinglong Mee 	module_put(THIS_MODULE);
25824cfc7e60SRahul Iyer }
25834cfc7e60SRahul Iyer 
2584d31ae254SChuck Lever static const struct rpc_xprt_ops xs_local_ops = {
2585176e21eeSChuck Lever 	.reserve_xprt		= xprt_reserve_xprt,
25864cd34e7cSTrond Myklebust 	.release_xprt		= xprt_release_xprt,
2587f39c1bfbSTrond Myklebust 	.alloc_slot		= xprt_alloc_slot,
2588a9cde23aSChuck Lever 	.free_slot		= xprt_free_slot,
2589176e21eeSChuck Lever 	.rpcbind		= xs_local_rpcbind,
2590176e21eeSChuck Lever 	.set_port		= xs_local_set_port,
2591dc107402SJ. Bruce Fields 	.connect		= xs_local_connect,
2592176e21eeSChuck Lever 	.buf_alloc		= rpc_malloc,
2593176e21eeSChuck Lever 	.buf_free		= rpc_free,
2594550aebfeSTrond Myklebust 	.prepare_request	= xs_stream_prepare_request,
2595176e21eeSChuck Lever 	.send_request		= xs_local_send_request,
25968ba6a92dSTrond Myklebust 	.wait_for_reply_request	= xprt_wait_for_reply_request_def,
2597176e21eeSChuck Lever 	.close			= xs_close,
2598a1311d87STrond Myklebust 	.destroy		= xs_destroy,
2599176e21eeSChuck Lever 	.print_stats		= xs_local_print_stats,
2600d67fa4d8SJeff Layton 	.enable_swap		= xs_enable_swap,
2601d67fa4d8SJeff Layton 	.disable_swap		= xs_disable_swap,
2602176e21eeSChuck Lever };
2603176e21eeSChuck Lever 
2604d31ae254SChuck Lever static const struct rpc_xprt_ops xs_udp_ops = {
260543118c29SChuck Lever 	.set_buffer_size	= xs_udp_set_buffer_size,
260612a80469SChuck Lever 	.reserve_xprt		= xprt_reserve_xprt_cong,
260749e9a890SChuck Lever 	.release_xprt		= xprt_release_xprt_cong,
2608f39c1bfbSTrond Myklebust 	.alloc_slot		= xprt_alloc_slot,
2609a9cde23aSChuck Lever 	.free_slot		= xprt_free_slot,
261045160d62SChuck Lever 	.rpcbind		= rpcb_getport_async,
261192200412SChuck Lever 	.set_port		= xs_set_port,
26129903cd1cSChuck Lever 	.connect		= xs_connect,
261302107148SChuck Lever 	.buf_alloc		= rpc_malloc,
261402107148SChuck Lever 	.buf_free		= rpc_free,
2615262965f5SChuck Lever 	.send_request		= xs_udp_send_request,
26168ba6a92dSTrond Myklebust 	.wait_for_reply_request	= xprt_wait_for_reply_request_rtt,
261746c0ee8bSChuck Lever 	.timer			= xs_udp_timer,
2618a58dd398SChuck Lever 	.release_request	= xprt_release_rqst_cong,
2619262965f5SChuck Lever 	.close			= xs_close,
2620262965f5SChuck Lever 	.destroy		= xs_destroy,
2621262ca07dSChuck Lever 	.print_stats		= xs_udp_print_stats,
2622d67fa4d8SJeff Layton 	.enable_swap		= xs_enable_swap,
2623d67fa4d8SJeff Layton 	.disable_swap		= xs_disable_swap,
26244a068258SChuck Lever 	.inject_disconnect	= xs_inject_disconnect,
2625262965f5SChuck Lever };
2626262965f5SChuck Lever 
2627d31ae254SChuck Lever static const struct rpc_xprt_ops xs_tcp_ops = {
262812a80469SChuck Lever 	.reserve_xprt		= xprt_reserve_xprt,
26294cd34e7cSTrond Myklebust 	.release_xprt		= xprt_release_xprt,
263036bd7de9STrond Myklebust 	.alloc_slot		= xprt_alloc_slot,
2631a9cde23aSChuck Lever 	.free_slot		= xprt_free_slot,
263245160d62SChuck Lever 	.rpcbind		= rpcb_getport_async,
263392200412SChuck Lever 	.set_port		= xs_set_port,
26340b9e7943STrond Myklebust 	.connect		= xs_connect,
263502107148SChuck Lever 	.buf_alloc		= rpc_malloc,
263602107148SChuck Lever 	.buf_free		= rpc_free,
2637277e4ab7STrond Myklebust 	.prepare_request	= xs_stream_prepare_request,
2638262965f5SChuck Lever 	.send_request		= xs_tcp_send_request,
26398ba6a92dSTrond Myklebust 	.wait_for_reply_request	= xprt_wait_for_reply_request_def,
2640c627d31bSTrond Myklebust 	.close			= xs_tcp_shutdown,
26419903cd1cSChuck Lever 	.destroy		= xs_destroy,
26427196dbb0STrond Myklebust 	.set_connect_timeout	= xs_tcp_set_connect_timeout,
2643262ca07dSChuck Lever 	.print_stats		= xs_tcp_print_stats,
2644d67fa4d8SJeff Layton 	.enable_swap		= xs_enable_swap,
2645d67fa4d8SJeff Layton 	.disable_swap		= xs_disable_swap,
26464a068258SChuck Lever 	.inject_disconnect	= xs_inject_disconnect,
264742e5c3e2SChuck Lever #ifdef CONFIG_SUNRPC_BACKCHANNEL
264842e5c3e2SChuck Lever 	.bc_setup		= xprt_setup_bc,
26496b26cc8cSChuck Lever 	.bc_maxpayload		= xs_tcp_bc_maxpayload,
26507402a4feSTrond Myklebust 	.bc_num_slots		= xprt_bc_max_slots,
265142e5c3e2SChuck Lever 	.bc_free_rqst		= xprt_free_bc_rqst,
265242e5c3e2SChuck Lever 	.bc_destroy		= xprt_destroy_bc,
265342e5c3e2SChuck Lever #endif
2654a246b010SChuck Lever };
2655a246b010SChuck Lever 
26564cfc7e60SRahul Iyer /*
26574cfc7e60SRahul Iyer  * The rpc_xprt_ops for the server backchannel
26584cfc7e60SRahul Iyer  */
26594cfc7e60SRahul Iyer 
2660d31ae254SChuck Lever static const struct rpc_xprt_ops bc_tcp_ops = {
26614cfc7e60SRahul Iyer 	.reserve_xprt		= xprt_reserve_xprt,
26624cfc7e60SRahul Iyer 	.release_xprt		= xprt_release_xprt,
266384e28a30SBryan Schumaker 	.alloc_slot		= xprt_alloc_slot,
2664a9cde23aSChuck Lever 	.free_slot		= xprt_free_slot,
26654cfc7e60SRahul Iyer 	.buf_alloc		= bc_malloc,
26664cfc7e60SRahul Iyer 	.buf_free		= bc_free,
26674cfc7e60SRahul Iyer 	.send_request		= bc_send_request,
26688ba6a92dSTrond Myklebust 	.wait_for_reply_request	= xprt_wait_for_reply_request_def,
26694cfc7e60SRahul Iyer 	.close			= bc_close,
26704cfc7e60SRahul Iyer 	.destroy		= bc_destroy,
26714cfc7e60SRahul Iyer 	.print_stats		= xs_tcp_print_stats,
2672d67fa4d8SJeff Layton 	.enable_swap		= xs_enable_swap,
2673d67fa4d8SJeff Layton 	.disable_swap		= xs_disable_swap,
26744a068258SChuck Lever 	.inject_disconnect	= xs_inject_disconnect,
26754cfc7e60SRahul Iyer };
26764cfc7e60SRahul Iyer 
267792476850SChuck Lever static int xs_init_anyaddr(const int family, struct sockaddr *sap)
267892476850SChuck Lever {
267992476850SChuck Lever 	static const struct sockaddr_in sin = {
268092476850SChuck Lever 		.sin_family		= AF_INET,
268192476850SChuck Lever 		.sin_addr.s_addr	= htonl(INADDR_ANY),
268292476850SChuck Lever 	};
268392476850SChuck Lever 	static const struct sockaddr_in6 sin6 = {
268492476850SChuck Lever 		.sin6_family		= AF_INET6,
268592476850SChuck Lever 		.sin6_addr		= IN6ADDR_ANY_INIT,
268692476850SChuck Lever 	};
268792476850SChuck Lever 
268892476850SChuck Lever 	switch (family) {
2689176e21eeSChuck Lever 	case AF_LOCAL:
2690176e21eeSChuck Lever 		break;
269192476850SChuck Lever 	case AF_INET:
269292476850SChuck Lever 		memcpy(sap, &sin, sizeof(sin));
269392476850SChuck Lever 		break;
269492476850SChuck Lever 	case AF_INET6:
269592476850SChuck Lever 		memcpy(sap, &sin6, sizeof(sin6));
269692476850SChuck Lever 		break;
269792476850SChuck Lever 	default:
269892476850SChuck Lever 		dprintk("RPC:       %s: Bad address family\n", __func__);
269992476850SChuck Lever 		return -EAFNOSUPPORT;
270092476850SChuck Lever 	}
270192476850SChuck Lever 	return 0;
270292476850SChuck Lever }
270392476850SChuck Lever 
27043c341b0bS\"Talpey, Thomas\ static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args,
2705d9ba131dSTrond Myklebust 				      unsigned int slot_table_size,
2706d9ba131dSTrond Myklebust 				      unsigned int max_slot_table_size)
2707c8541ecdSChuck Lever {
2708c8541ecdSChuck Lever 	struct rpc_xprt *xprt;
2709ffc2e518SChuck Lever 	struct sock_xprt *new;
2710c8541ecdSChuck Lever 
271196802a09SFrank van Maarseveen 	if (args->addrlen > sizeof(xprt->addr)) {
2712c8541ecdSChuck Lever 		dprintk("RPC:       xs_setup_xprt: address too large\n");
2713c8541ecdSChuck Lever 		return ERR_PTR(-EBADF);
2714c8541ecdSChuck Lever 	}
2715c8541ecdSChuck Lever 
2716d9ba131dSTrond Myklebust 	xprt = xprt_alloc(args->net, sizeof(*new), slot_table_size,
2717d9ba131dSTrond Myklebust 			max_slot_table_size);
2718bd1722d4SPavel Emelyanov 	if (xprt == NULL) {
271946121cf7SChuck Lever 		dprintk("RPC:       xs_setup_xprt: couldn't allocate "
272046121cf7SChuck Lever 				"rpc_xprt\n");
2721c8541ecdSChuck Lever 		return ERR_PTR(-ENOMEM);
2722c8541ecdSChuck Lever 	}
2723c8541ecdSChuck Lever 
2724bd1722d4SPavel Emelyanov 	new = container_of(xprt, struct sock_xprt, xprt);
2725edc1b01cSTrond Myklebust 	mutex_init(&new->recv_mutex);
272696802a09SFrank van Maarseveen 	memcpy(&xprt->addr, args->dstaddr, args->addrlen);
272796802a09SFrank van Maarseveen 	xprt->addrlen = args->addrlen;
2728d3bc9a1dSFrank van Maarseveen 	if (args->srcaddr)
2729fbfffbd5SChuck Lever 		memcpy(&new->srcaddr, args->srcaddr, args->addrlen);
273092476850SChuck Lever 	else {
273192476850SChuck Lever 		int err;
273292476850SChuck Lever 		err = xs_init_anyaddr(args->dstaddr->sa_family,
273392476850SChuck Lever 					(struct sockaddr *)&new->srcaddr);
27342aa13531SStanislav Kinsbursky 		if (err != 0) {
27352aa13531SStanislav Kinsbursky 			xprt_free(xprt);
273692476850SChuck Lever 			return ERR_PTR(err);
273792476850SChuck Lever 		}
27382aa13531SStanislav Kinsbursky 	}
2739c8541ecdSChuck Lever 
2740c8541ecdSChuck Lever 	return xprt;
2741c8541ecdSChuck Lever }
2742c8541ecdSChuck Lever 
2743176e21eeSChuck Lever static const struct rpc_timeout xs_local_default_timeout = {
2744176e21eeSChuck Lever 	.to_initval = 10 * HZ,
2745176e21eeSChuck Lever 	.to_maxval = 10 * HZ,
2746176e21eeSChuck Lever 	.to_retries = 2,
2747176e21eeSChuck Lever };
2748176e21eeSChuck Lever 
2749176e21eeSChuck Lever /**
2750176e21eeSChuck Lever  * xs_setup_local - Set up transport to use an AF_LOCAL socket
2751176e21eeSChuck Lever  * @args: rpc transport creation arguments
2752176e21eeSChuck Lever  *
2753176e21eeSChuck Lever  * AF_LOCAL is a "tpi_cots_ord" transport, just like TCP
2754176e21eeSChuck Lever  */
2755176e21eeSChuck Lever static struct rpc_xprt *xs_setup_local(struct xprt_create *args)
2756176e21eeSChuck Lever {
2757176e21eeSChuck Lever 	struct sockaddr_un *sun = (struct sockaddr_un *)args->dstaddr;
2758176e21eeSChuck Lever 	struct sock_xprt *transport;
2759176e21eeSChuck Lever 	struct rpc_xprt *xprt;
2760176e21eeSChuck Lever 	struct rpc_xprt *ret;
2761176e21eeSChuck Lever 
2762d9ba131dSTrond Myklebust 	xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
2763d9ba131dSTrond Myklebust 			xprt_max_tcp_slot_table_entries);
2764176e21eeSChuck Lever 	if (IS_ERR(xprt))
2765176e21eeSChuck Lever 		return xprt;
2766176e21eeSChuck Lever 	transport = container_of(xprt, struct sock_xprt, xprt);
2767176e21eeSChuck Lever 
2768176e21eeSChuck Lever 	xprt->prot = 0;
2769176e21eeSChuck Lever 	xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2770176e21eeSChuck Lever 
2771176e21eeSChuck Lever 	xprt->bind_timeout = XS_BIND_TO;
2772176e21eeSChuck Lever 	xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2773176e21eeSChuck Lever 	xprt->idle_timeout = XS_IDLE_DISC_TO;
2774176e21eeSChuck Lever 
2775176e21eeSChuck Lever 	xprt->ops = &xs_local_ops;
2776176e21eeSChuck Lever 	xprt->timeout = &xs_local_default_timeout;
2777176e21eeSChuck Lever 
2778550aebfeSTrond Myklebust 	INIT_WORK(&transport->recv_worker, xs_stream_data_receive_workfn);
27794f8943f8STrond Myklebust 	INIT_WORK(&transport->error_worker, xs_error_handle);
2780550aebfeSTrond Myklebust 	INIT_DELAYED_WORK(&transport->connect_worker, xs_dummy_setup_socket);
278193dc41bdSNeilBrown 
2782176e21eeSChuck Lever 	switch (sun->sun_family) {
2783176e21eeSChuck Lever 	case AF_LOCAL:
2784176e21eeSChuck Lever 		if (sun->sun_path[0] != '/') {
2785176e21eeSChuck Lever 			dprintk("RPC:       bad AF_LOCAL address: %s\n",
2786176e21eeSChuck Lever 					sun->sun_path);
2787176e21eeSChuck Lever 			ret = ERR_PTR(-EINVAL);
2788176e21eeSChuck Lever 			goto out_err;
2789176e21eeSChuck Lever 		}
2790176e21eeSChuck Lever 		xprt_set_bound(xprt);
2791176e21eeSChuck Lever 		xs_format_peer_addresses(xprt, "local", RPCBIND_NETID_LOCAL);
27927073ea87SJ. Bruce Fields 		ret = ERR_PTR(xs_local_setup_socket(transport));
27937073ea87SJ. Bruce Fields 		if (ret)
27947073ea87SJ. Bruce Fields 			goto out_err;
2795176e21eeSChuck Lever 		break;
2796176e21eeSChuck Lever 	default:
2797176e21eeSChuck Lever 		ret = ERR_PTR(-EAFNOSUPPORT);
2798176e21eeSChuck Lever 		goto out_err;
2799176e21eeSChuck Lever 	}
2800176e21eeSChuck Lever 
2801176e21eeSChuck Lever 	dprintk("RPC:       set up xprt to %s via AF_LOCAL\n",
2802176e21eeSChuck Lever 			xprt->address_strings[RPC_DISPLAY_ADDR]);
2803176e21eeSChuck Lever 
2804176e21eeSChuck Lever 	if (try_module_get(THIS_MODULE))
2805176e21eeSChuck Lever 		return xprt;
2806176e21eeSChuck Lever 	ret = ERR_PTR(-EINVAL);
2807176e21eeSChuck Lever out_err:
2808315f3812SKinglong Mee 	xs_xprt_free(xprt);
2809176e21eeSChuck Lever 	return ret;
2810176e21eeSChuck Lever }
2811176e21eeSChuck Lever 
28122881ae74STrond Myklebust static const struct rpc_timeout xs_udp_default_timeout = {
28132881ae74STrond Myklebust 	.to_initval = 5 * HZ,
28142881ae74STrond Myklebust 	.to_maxval = 30 * HZ,
28152881ae74STrond Myklebust 	.to_increment = 5 * HZ,
28162881ae74STrond Myklebust 	.to_retries = 5,
28172881ae74STrond Myklebust };
28182881ae74STrond Myklebust 
28199903cd1cSChuck Lever /**
28209903cd1cSChuck Lever  * xs_setup_udp - Set up transport to use a UDP socket
282196802a09SFrank van Maarseveen  * @args: rpc transport creation arguments
28229903cd1cSChuck Lever  *
28239903cd1cSChuck Lever  */
2824483066d6SAdrian Bunk static struct rpc_xprt *xs_setup_udp(struct xprt_create *args)
2825a246b010SChuck Lever {
28268f9d5b1aSChuck Lever 	struct sockaddr *addr = args->dstaddr;
2827c8541ecdSChuck Lever 	struct rpc_xprt *xprt;
2828c8475461SChuck Lever 	struct sock_xprt *transport;
28290a68b0beSJ. Bruce Fields 	struct rpc_xprt *ret;
2830a246b010SChuck Lever 
2831d9ba131dSTrond Myklebust 	xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries,
2832d9ba131dSTrond Myklebust 			xprt_udp_slot_table_entries);
2833c8541ecdSChuck Lever 	if (IS_ERR(xprt))
2834c8541ecdSChuck Lever 		return xprt;
2835c8475461SChuck Lever 	transport = container_of(xprt, struct sock_xprt, xprt);
2836a246b010SChuck Lever 
2837ec739ef0SChuck Lever 	xprt->prot = IPPROTO_UDP;
2838a246b010SChuck Lever 	/* XXX: header size can vary due to auth type, IPv6, etc. */
2839a246b010SChuck Lever 	xprt->max_payload = (1U << 16) - (MAX_HEADER << 3);
2840a246b010SChuck Lever 
284103bf4b70SChuck Lever 	xprt->bind_timeout = XS_BIND_TO;
284203bf4b70SChuck Lever 	xprt->reestablish_timeout = XS_UDP_REEST_TO;
284303bf4b70SChuck Lever 	xprt->idle_timeout = XS_IDLE_DISC_TO;
2844a246b010SChuck Lever 
2845262965f5SChuck Lever 	xprt->ops = &xs_udp_ops;
2846a246b010SChuck Lever 
2847ba7392bbSTrond Myklebust 	xprt->timeout = &xs_udp_default_timeout;
2848a246b010SChuck Lever 
2849f9b2ee71STrond Myklebust 	INIT_WORK(&transport->recv_worker, xs_udp_data_receive_workfn);
28504f8943f8STrond Myklebust 	INIT_WORK(&transport->error_worker, xs_error_handle);
2851edc1b01cSTrond Myklebust 	INIT_DELAYED_WORK(&transport->connect_worker, xs_udp_setup_socket);
2852edc1b01cSTrond Myklebust 
28538f9d5b1aSChuck Lever 	switch (addr->sa_family) {
28548f9d5b1aSChuck Lever 	case AF_INET:
28558f9d5b1aSChuck Lever 		if (((struct sockaddr_in *)addr)->sin_port != htons(0))
28568f9d5b1aSChuck Lever 			xprt_set_bound(xprt);
28578f9d5b1aSChuck Lever 
28589dc3b095SChuck Lever 		xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP);
28598f9d5b1aSChuck Lever 		break;
28608f9d5b1aSChuck Lever 	case AF_INET6:
28618f9d5b1aSChuck Lever 		if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
28628f9d5b1aSChuck Lever 			xprt_set_bound(xprt);
28638f9d5b1aSChuck Lever 
28649dc3b095SChuck Lever 		xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6);
28658f9d5b1aSChuck Lever 		break;
28668f9d5b1aSChuck Lever 	default:
28670a68b0beSJ. Bruce Fields 		ret = ERR_PTR(-EAFNOSUPPORT);
28680a68b0beSJ. Bruce Fields 		goto out_err;
28698f9d5b1aSChuck Lever 	}
28708f9d5b1aSChuck Lever 
2871c740eff8SChuck Lever 	if (xprt_bound(xprt))
2872c740eff8SChuck Lever 		dprintk("RPC:       set up xprt to %s (port %s) via %s\n",
2873c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_ADDR],
2874c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_PORT],
2875c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_PROTO]);
2876c740eff8SChuck Lever 	else
2877c740eff8SChuck Lever 		dprintk("RPC:       set up xprt to %s (autobind) via %s\n",
2878c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_ADDR],
2879c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_PROTO]);
2880edb267a6SChuck Lever 
2881bc25571eS\"Talpey, Thomas\ 	if (try_module_get(THIS_MODULE))
2882c8541ecdSChuck Lever 		return xprt;
28830a68b0beSJ. Bruce Fields 	ret = ERR_PTR(-EINVAL);
28840a68b0beSJ. Bruce Fields out_err:
2885315f3812SKinglong Mee 	xs_xprt_free(xprt);
28860a68b0beSJ. Bruce Fields 	return ret;
2887a246b010SChuck Lever }
2888a246b010SChuck Lever 
28892881ae74STrond Myklebust static const struct rpc_timeout xs_tcp_default_timeout = {
28902881ae74STrond Myklebust 	.to_initval = 60 * HZ,
28912881ae74STrond Myklebust 	.to_maxval = 60 * HZ,
28922881ae74STrond Myklebust 	.to_retries = 2,
28932881ae74STrond Myklebust };
28942881ae74STrond Myklebust 
28959903cd1cSChuck Lever /**
28969903cd1cSChuck Lever  * xs_setup_tcp - Set up transport to use a TCP socket
289796802a09SFrank van Maarseveen  * @args: rpc transport creation arguments
28989903cd1cSChuck Lever  *
28999903cd1cSChuck Lever  */
2900483066d6SAdrian Bunk static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args)
2901a246b010SChuck Lever {
29028f9d5b1aSChuck Lever 	struct sockaddr *addr = args->dstaddr;
2903c8541ecdSChuck Lever 	struct rpc_xprt *xprt;
2904c8475461SChuck Lever 	struct sock_xprt *transport;
29050a68b0beSJ. Bruce Fields 	struct rpc_xprt *ret;
2906b7993cebSTrond Myklebust 	unsigned int max_slot_table_size = xprt_max_tcp_slot_table_entries;
2907b7993cebSTrond Myklebust 
2908b7993cebSTrond Myklebust 	if (args->flags & XPRT_CREATE_INFINITE_SLOTS)
2909b7993cebSTrond Myklebust 		max_slot_table_size = RPC_MAX_SLOT_TABLE_LIMIT;
2910a246b010SChuck Lever 
2911d9ba131dSTrond Myklebust 	xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
2912b7993cebSTrond Myklebust 			max_slot_table_size);
2913c8541ecdSChuck Lever 	if (IS_ERR(xprt))
2914c8541ecdSChuck Lever 		return xprt;
2915c8475461SChuck Lever 	transport = container_of(xprt, struct sock_xprt, xprt);
2916a246b010SChuck Lever 
2917ec739ef0SChuck Lever 	xprt->prot = IPPROTO_TCP;
2918808012fbSChuck Lever 	xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2919a246b010SChuck Lever 
292003bf4b70SChuck Lever 	xprt->bind_timeout = XS_BIND_TO;
292103bf4b70SChuck Lever 	xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
292203bf4b70SChuck Lever 	xprt->idle_timeout = XS_IDLE_DISC_TO;
2923a246b010SChuck Lever 
2924262965f5SChuck Lever 	xprt->ops = &xs_tcp_ops;
2925ba7392bbSTrond Myklebust 	xprt->timeout = &xs_tcp_default_timeout;
2926a246b010SChuck Lever 
29273851f1cdSTrond Myklebust 	xprt->max_reconnect_timeout = xprt->timeout->to_maxval;
29287196dbb0STrond Myklebust 	xprt->connect_timeout = xprt->timeout->to_initval *
29297196dbb0STrond Myklebust 		(xprt->timeout->to_retries + 1);
29303851f1cdSTrond Myklebust 
2931c50b8ee0STrond Myklebust 	INIT_WORK(&transport->recv_worker, xs_stream_data_receive_workfn);
29324f8943f8STrond Myklebust 	INIT_WORK(&transport->error_worker, xs_error_handle);
2933edc1b01cSTrond Myklebust 	INIT_DELAYED_WORK(&transport->connect_worker, xs_tcp_setup_socket);
2934edc1b01cSTrond Myklebust 
29358f9d5b1aSChuck Lever 	switch (addr->sa_family) {
29368f9d5b1aSChuck Lever 	case AF_INET:
29378f9d5b1aSChuck Lever 		if (((struct sockaddr_in *)addr)->sin_port != htons(0))
29388f9d5b1aSChuck Lever 			xprt_set_bound(xprt);
29398f9d5b1aSChuck Lever 
29409dc3b095SChuck Lever 		xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP);
29418f9d5b1aSChuck Lever 		break;
29428f9d5b1aSChuck Lever 	case AF_INET6:
29438f9d5b1aSChuck Lever 		if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
29448f9d5b1aSChuck Lever 			xprt_set_bound(xprt);
29458f9d5b1aSChuck Lever 
29469dc3b095SChuck Lever 		xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6);
29478f9d5b1aSChuck Lever 		break;
29488f9d5b1aSChuck Lever 	default:
29490a68b0beSJ. Bruce Fields 		ret = ERR_PTR(-EAFNOSUPPORT);
29500a68b0beSJ. Bruce Fields 		goto out_err;
29518f9d5b1aSChuck Lever 	}
29528f9d5b1aSChuck Lever 
2953c740eff8SChuck Lever 	if (xprt_bound(xprt))
2954c740eff8SChuck Lever 		dprintk("RPC:       set up xprt to %s (port %s) via %s\n",
2955c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_ADDR],
2956c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_PORT],
2957c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_PROTO]);
2958c740eff8SChuck Lever 	else
2959c740eff8SChuck Lever 		dprintk("RPC:       set up xprt to %s (autobind) via %s\n",
2960c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_ADDR],
2961c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_PROTO]);
2962c740eff8SChuck Lever 
2963bc25571eS\"Talpey, Thomas\ 	if (try_module_get(THIS_MODULE))
2964c8541ecdSChuck Lever 		return xprt;
29650a68b0beSJ. Bruce Fields 	ret = ERR_PTR(-EINVAL);
29660a68b0beSJ. Bruce Fields out_err:
2967315f3812SKinglong Mee 	xs_xprt_free(xprt);
29680a68b0beSJ. Bruce Fields 	return ret;
2969a246b010SChuck Lever }
2970282b32e1SChuck Lever 
2971f300babaSAlexandros Batsakis /**
2972f300babaSAlexandros Batsakis  * xs_setup_bc_tcp - Set up transport to use a TCP backchannel socket
2973f300babaSAlexandros Batsakis  * @args: rpc transport creation arguments
2974f300babaSAlexandros Batsakis  *
2975f300babaSAlexandros Batsakis  */
2976f300babaSAlexandros Batsakis static struct rpc_xprt *xs_setup_bc_tcp(struct xprt_create *args)
2977f300babaSAlexandros Batsakis {
2978f300babaSAlexandros Batsakis 	struct sockaddr *addr = args->dstaddr;
2979f300babaSAlexandros Batsakis 	struct rpc_xprt *xprt;
2980f300babaSAlexandros Batsakis 	struct sock_xprt *transport;
2981f300babaSAlexandros Batsakis 	struct svc_sock *bc_sock;
29820a68b0beSJ. Bruce Fields 	struct rpc_xprt *ret;
2983f300babaSAlexandros Batsakis 
2984d9ba131dSTrond Myklebust 	xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
2985d9ba131dSTrond Myklebust 			xprt_tcp_slot_table_entries);
2986f300babaSAlexandros Batsakis 	if (IS_ERR(xprt))
2987f300babaSAlexandros Batsakis 		return xprt;
2988f300babaSAlexandros Batsakis 	transport = container_of(xprt, struct sock_xprt, xprt);
2989f300babaSAlexandros Batsakis 
2990f300babaSAlexandros Batsakis 	xprt->prot = IPPROTO_TCP;
2991f300babaSAlexandros Batsakis 	xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2992f300babaSAlexandros Batsakis 	xprt->timeout = &xs_tcp_default_timeout;
2993f300babaSAlexandros Batsakis 
2994f300babaSAlexandros Batsakis 	/* backchannel */
2995f300babaSAlexandros Batsakis 	xprt_set_bound(xprt);
2996f300babaSAlexandros Batsakis 	xprt->bind_timeout = 0;
2997f300babaSAlexandros Batsakis 	xprt->reestablish_timeout = 0;
2998f300babaSAlexandros Batsakis 	xprt->idle_timeout = 0;
2999f300babaSAlexandros Batsakis 
3000f300babaSAlexandros Batsakis 	xprt->ops = &bc_tcp_ops;
3001f300babaSAlexandros Batsakis 
3002f300babaSAlexandros Batsakis 	switch (addr->sa_family) {
3003f300babaSAlexandros Batsakis 	case AF_INET:
3004f300babaSAlexandros Batsakis 		xs_format_peer_addresses(xprt, "tcp",
3005f300babaSAlexandros Batsakis 					 RPCBIND_NETID_TCP);
3006f300babaSAlexandros Batsakis 		break;
3007f300babaSAlexandros Batsakis 	case AF_INET6:
3008f300babaSAlexandros Batsakis 		xs_format_peer_addresses(xprt, "tcp",
3009f300babaSAlexandros Batsakis 				   RPCBIND_NETID_TCP6);
3010f300babaSAlexandros Batsakis 		break;
3011f300babaSAlexandros Batsakis 	default:
30120a68b0beSJ. Bruce Fields 		ret = ERR_PTR(-EAFNOSUPPORT);
30130a68b0beSJ. Bruce Fields 		goto out_err;
3014f300babaSAlexandros Batsakis 	}
3015f300babaSAlexandros Batsakis 
3016f300babaSAlexandros Batsakis 	dprintk("RPC:       set up xprt to %s (port %s) via %s\n",
3017f300babaSAlexandros Batsakis 			xprt->address_strings[RPC_DISPLAY_ADDR],
3018f300babaSAlexandros Batsakis 			xprt->address_strings[RPC_DISPLAY_PORT],
3019f300babaSAlexandros Batsakis 			xprt->address_strings[RPC_DISPLAY_PROTO]);
3020f300babaSAlexandros Batsakis 
3021f300babaSAlexandros Batsakis 	/*
302299de8ea9SJ. Bruce Fields 	 * Once we've associated a backchannel xprt with a connection,
302328303ca3SWeng Meiling 	 * we want to keep it around as long as the connection lasts,
302428303ca3SWeng Meiling 	 * in case we need to start using it for a backchannel again;
302528303ca3SWeng Meiling 	 * this reference won't be dropped until bc_xprt is destroyed.
302699de8ea9SJ. Bruce Fields 	 */
302799de8ea9SJ. Bruce Fields 	xprt_get(xprt);
302899de8ea9SJ. Bruce Fields 	args->bc_xprt->xpt_bc_xprt = xprt;
302999de8ea9SJ. Bruce Fields 	xprt->bc_xprt = args->bc_xprt;
303099de8ea9SJ. Bruce Fields 	bc_sock = container_of(args->bc_xprt, struct svc_sock, sk_xprt);
303199de8ea9SJ. Bruce Fields 	transport->sock = bc_sock->sk_sock;
303299de8ea9SJ. Bruce Fields 	transport->inet = bc_sock->sk_sk;
303399de8ea9SJ. Bruce Fields 
303499de8ea9SJ. Bruce Fields 	/*
3035f300babaSAlexandros Batsakis 	 * Since we don't want connections for the backchannel, we set
3036f300babaSAlexandros Batsakis 	 * the xprt status to connected
3037f300babaSAlexandros Batsakis 	 */
3038f300babaSAlexandros Batsakis 	xprt_set_connected(xprt);
3039f300babaSAlexandros Batsakis 
3040f300babaSAlexandros Batsakis 	if (try_module_get(THIS_MODULE))
3041f300babaSAlexandros Batsakis 		return xprt;
3042642aab58SKinglong Mee 
3043642aab58SKinglong Mee 	args->bc_xprt->xpt_bc_xprt = NULL;
304439a9beabSJ. Bruce Fields 	args->bc_xprt->xpt_bc_xps = NULL;
304599de8ea9SJ. Bruce Fields 	xprt_put(xprt);
30460a68b0beSJ. Bruce Fields 	ret = ERR_PTR(-EINVAL);
30470a68b0beSJ. Bruce Fields out_err:
3048315f3812SKinglong Mee 	xs_xprt_free(xprt);
30490a68b0beSJ. Bruce Fields 	return ret;
3050f300babaSAlexandros Batsakis }
3051f300babaSAlexandros Batsakis 
3052176e21eeSChuck Lever static struct xprt_class	xs_local_transport = {
3053176e21eeSChuck Lever 	.list		= LIST_HEAD_INIT(xs_local_transport.list),
3054176e21eeSChuck Lever 	.name		= "named UNIX socket",
3055176e21eeSChuck Lever 	.owner		= THIS_MODULE,
3056176e21eeSChuck Lever 	.ident		= XPRT_TRANSPORT_LOCAL,
3057176e21eeSChuck Lever 	.setup		= xs_setup_local,
3058176e21eeSChuck Lever };
3059176e21eeSChuck Lever 
3060bc25571eS\"Talpey, Thomas\ static struct xprt_class	xs_udp_transport = {
3061bc25571eS\"Talpey, Thomas\ 	.list		= LIST_HEAD_INIT(xs_udp_transport.list),
3062bc25571eS\"Talpey, Thomas\ 	.name		= "udp",
3063bc25571eS\"Talpey, Thomas\ 	.owner		= THIS_MODULE,
3064f300babaSAlexandros Batsakis 	.ident		= XPRT_TRANSPORT_UDP,
3065bc25571eS\"Talpey, Thomas\ 	.setup		= xs_setup_udp,
3066bc25571eS\"Talpey, Thomas\ };
3067bc25571eS\"Talpey, Thomas\ 
3068bc25571eS\"Talpey, Thomas\ static struct xprt_class	xs_tcp_transport = {
3069bc25571eS\"Talpey, Thomas\ 	.list		= LIST_HEAD_INIT(xs_tcp_transport.list),
3070bc25571eS\"Talpey, Thomas\ 	.name		= "tcp",
3071bc25571eS\"Talpey, Thomas\ 	.owner		= THIS_MODULE,
3072f300babaSAlexandros Batsakis 	.ident		= XPRT_TRANSPORT_TCP,
3073bc25571eS\"Talpey, Thomas\ 	.setup		= xs_setup_tcp,
3074bc25571eS\"Talpey, Thomas\ };
3075bc25571eS\"Talpey, Thomas\ 
3076f300babaSAlexandros Batsakis static struct xprt_class	xs_bc_tcp_transport = {
3077f300babaSAlexandros Batsakis 	.list		= LIST_HEAD_INIT(xs_bc_tcp_transport.list),
3078f300babaSAlexandros Batsakis 	.name		= "tcp NFSv4.1 backchannel",
3079f300babaSAlexandros Batsakis 	.owner		= THIS_MODULE,
3080f300babaSAlexandros Batsakis 	.ident		= XPRT_TRANSPORT_BC_TCP,
3081f300babaSAlexandros Batsakis 	.setup		= xs_setup_bc_tcp,
3082f300babaSAlexandros Batsakis };
3083f300babaSAlexandros Batsakis 
3084282b32e1SChuck Lever /**
3085bc25571eS\"Talpey, Thomas\  * init_socket_xprt - set up xprtsock's sysctls, register with RPC client
3086282b32e1SChuck Lever  *
3087282b32e1SChuck Lever  */
3088282b32e1SChuck Lever int init_socket_xprt(void)
3089282b32e1SChuck Lever {
30902b1bec5fSEric W. Biederman 	if (!sunrpc_table_header)
30910b4d4147SEric W. Biederman 		sunrpc_table_header = register_sysctl_table(sunrpc_table);
3092fbf76683SChuck Lever 
3093176e21eeSChuck Lever 	xprt_register_transport(&xs_local_transport);
3094bc25571eS\"Talpey, Thomas\ 	xprt_register_transport(&xs_udp_transport);
3095bc25571eS\"Talpey, Thomas\ 	xprt_register_transport(&xs_tcp_transport);
3096f300babaSAlexandros Batsakis 	xprt_register_transport(&xs_bc_tcp_transport);
3097bc25571eS\"Talpey, Thomas\ 
3098282b32e1SChuck Lever 	return 0;
3099282b32e1SChuck Lever }
3100282b32e1SChuck Lever 
3101282b32e1SChuck Lever /**
3102bc25571eS\"Talpey, Thomas\  * cleanup_socket_xprt - remove xprtsock's sysctls, unregister
3103282b32e1SChuck Lever  *
3104282b32e1SChuck Lever  */
3105282b32e1SChuck Lever void cleanup_socket_xprt(void)
3106282b32e1SChuck Lever {
3107fbf76683SChuck Lever 	if (sunrpc_table_header) {
3108fbf76683SChuck Lever 		unregister_sysctl_table(sunrpc_table_header);
3109fbf76683SChuck Lever 		sunrpc_table_header = NULL;
3110fbf76683SChuck Lever 	}
3111bc25571eS\"Talpey, Thomas\ 
3112176e21eeSChuck Lever 	xprt_unregister_transport(&xs_local_transport);
3113bc25571eS\"Talpey, Thomas\ 	xprt_unregister_transport(&xs_udp_transport);
3114bc25571eS\"Talpey, Thomas\ 	xprt_unregister_transport(&xs_tcp_transport);
3115f300babaSAlexandros Batsakis 	xprt_unregister_transport(&xs_bc_tcp_transport);
3116282b32e1SChuck Lever }
3117cbf11071STrond Myklebust 
31189bbb9e5aSRusty Russell static int param_set_uint_minmax(const char *val,
31199bbb9e5aSRusty Russell 		const struct kernel_param *kp,
3120cbf11071STrond Myklebust 		unsigned int min, unsigned int max)
3121cbf11071STrond Myklebust {
312200cfaa94SDaniel Walter 	unsigned int num;
3123cbf11071STrond Myklebust 	int ret;
3124cbf11071STrond Myklebust 
3125cbf11071STrond Myklebust 	if (!val)
3126cbf11071STrond Myklebust 		return -EINVAL;
312700cfaa94SDaniel Walter 	ret = kstrtouint(val, 0, &num);
31289761a246SDan Carpenter 	if (ret)
31299761a246SDan Carpenter 		return ret;
31309761a246SDan Carpenter 	if (num < min || num > max)
3131cbf11071STrond Myklebust 		return -EINVAL;
3132cbf11071STrond Myklebust 	*((unsigned int *)kp->arg) = num;
3133cbf11071STrond Myklebust 	return 0;
3134cbf11071STrond Myklebust }
3135cbf11071STrond Myklebust 
31369bbb9e5aSRusty Russell static int param_set_portnr(const char *val, const struct kernel_param *kp)
3137cbf11071STrond Myklebust {
3138cbf11071STrond Myklebust 	return param_set_uint_minmax(val, kp,
3139cbf11071STrond Myklebust 			RPC_MIN_RESVPORT,
3140cbf11071STrond Myklebust 			RPC_MAX_RESVPORT);
3141cbf11071STrond Myklebust }
3142cbf11071STrond Myklebust 
31439c27847dSLuis R. Rodriguez static const struct kernel_param_ops param_ops_portnr = {
31449bbb9e5aSRusty Russell 	.set = param_set_portnr,
31459bbb9e5aSRusty Russell 	.get = param_get_uint,
31469bbb9e5aSRusty Russell };
31479bbb9e5aSRusty Russell 
3148cbf11071STrond Myklebust #define param_check_portnr(name, p) \
3149cbf11071STrond Myklebust 	__param_check(name, p, unsigned int);
3150cbf11071STrond Myklebust 
3151cbf11071STrond Myklebust module_param_named(min_resvport, xprt_min_resvport, portnr, 0644);
3152cbf11071STrond Myklebust module_param_named(max_resvport, xprt_max_resvport, portnr, 0644);
3153cbf11071STrond Myklebust 
31549bbb9e5aSRusty Russell static int param_set_slot_table_size(const char *val,
31559bbb9e5aSRusty Russell 				     const struct kernel_param *kp)
3156cbf11071STrond Myklebust {
3157cbf11071STrond Myklebust 	return param_set_uint_minmax(val, kp,
3158cbf11071STrond Myklebust 			RPC_MIN_SLOT_TABLE,
3159cbf11071STrond Myklebust 			RPC_MAX_SLOT_TABLE);
3160cbf11071STrond Myklebust }
3161cbf11071STrond Myklebust 
31629c27847dSLuis R. Rodriguez static const struct kernel_param_ops param_ops_slot_table_size = {
31639bbb9e5aSRusty Russell 	.set = param_set_slot_table_size,
31649bbb9e5aSRusty Russell 	.get = param_get_uint,
31659bbb9e5aSRusty Russell };
31669bbb9e5aSRusty Russell 
3167cbf11071STrond Myklebust #define param_check_slot_table_size(name, p) \
3168cbf11071STrond Myklebust 	__param_check(name, p, unsigned int);
3169cbf11071STrond Myklebust 
3170d9ba131dSTrond Myklebust static int param_set_max_slot_table_size(const char *val,
3171d9ba131dSTrond Myklebust 				     const struct kernel_param *kp)
3172d9ba131dSTrond Myklebust {
3173d9ba131dSTrond Myklebust 	return param_set_uint_minmax(val, kp,
3174d9ba131dSTrond Myklebust 			RPC_MIN_SLOT_TABLE,
3175d9ba131dSTrond Myklebust 			RPC_MAX_SLOT_TABLE_LIMIT);
3176d9ba131dSTrond Myklebust }
3177d9ba131dSTrond Myklebust 
31789c27847dSLuis R. Rodriguez static const struct kernel_param_ops param_ops_max_slot_table_size = {
3179d9ba131dSTrond Myklebust 	.set = param_set_max_slot_table_size,
3180d9ba131dSTrond Myklebust 	.get = param_get_uint,
3181d9ba131dSTrond Myklebust };
3182d9ba131dSTrond Myklebust 
3183d9ba131dSTrond Myklebust #define param_check_max_slot_table_size(name, p) \
3184d9ba131dSTrond Myklebust 	__param_check(name, p, unsigned int);
3185d9ba131dSTrond Myklebust 
3186cbf11071STrond Myklebust module_param_named(tcp_slot_table_entries, xprt_tcp_slot_table_entries,
3187cbf11071STrond Myklebust 		   slot_table_size, 0644);
3188d9ba131dSTrond Myklebust module_param_named(tcp_max_slot_table_entries, xprt_max_tcp_slot_table_entries,
3189d9ba131dSTrond Myklebust 		   max_slot_table_size, 0644);
3190cbf11071STrond Myklebust module_param_named(udp_slot_table_entries, xprt_udp_slot_table_entries,
3191cbf11071STrond Myklebust 		   slot_table_size, 0644);
3192