xref: /openbmc/linux/net/sunrpc/xprtsock.c (revision 7402a4fedc2bc448100c2d086406c708451b16dc)
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 
574cfc7e60SRahul Iyer #include "sunrpc.h"
58176e21eeSChuck Lever 
59176e21eeSChuck Lever static void xs_close(struct rpc_xprt *xprt);
607196dbb0STrond Myklebust static void xs_tcp_set_socket_timeouts(struct rpc_xprt *xprt,
617196dbb0STrond Myklebust 		struct socket *sock);
62176e21eeSChuck Lever 
639903cd1cSChuck Lever /*
64c556b754SChuck Lever  * xprtsock tunables
65c556b754SChuck Lever  */
6609acfea5STrond Myklebust static unsigned int xprt_udp_slot_table_entries = RPC_DEF_SLOT_TABLE;
6709acfea5STrond Myklebust static unsigned int xprt_tcp_slot_table_entries = RPC_MIN_SLOT_TABLE;
6809acfea5STrond Myklebust static unsigned int xprt_max_tcp_slot_table_entries = RPC_MAX_SLOT_TABLE;
69c556b754SChuck Lever 
7009acfea5STrond Myklebust static unsigned int xprt_min_resvport = RPC_DEF_MIN_RESVPORT;
7109acfea5STrond Myklebust static unsigned int xprt_max_resvport = RPC_DEF_MAX_RESVPORT;
72c556b754SChuck Lever 
737d1e8255STrond Myklebust #define XS_TCP_LINGER_TO	(15U * HZ)
7425fe6142STrond Myklebust static unsigned int xs_tcp_fin_timeout __read_mostly = XS_TCP_LINGER_TO;
757d1e8255STrond Myklebust 
76c556b754SChuck Lever /*
77fbf76683SChuck Lever  * We can register our own files under /proc/sys/sunrpc by
78fbf76683SChuck Lever  * calling register_sysctl_table() again.  The files in that
79fbf76683SChuck Lever  * directory become the union of all files registered there.
80fbf76683SChuck Lever  *
81fbf76683SChuck Lever  * We simply need to make sure that we don't collide with
82fbf76683SChuck Lever  * someone else's file names!
83fbf76683SChuck Lever  */
84fbf76683SChuck Lever 
85fbf76683SChuck Lever static unsigned int min_slot_table_size = RPC_MIN_SLOT_TABLE;
86fbf76683SChuck Lever static unsigned int max_slot_table_size = RPC_MAX_SLOT_TABLE;
87d9ba131dSTrond Myklebust static unsigned int max_tcp_slot_table_limit = RPC_MAX_SLOT_TABLE_LIMIT;
88fbf76683SChuck Lever static unsigned int xprt_min_resvport_limit = RPC_MIN_RESVPORT;
89fbf76683SChuck Lever static unsigned int xprt_max_resvport_limit = RPC_MAX_RESVPORT;
90fbf76683SChuck Lever 
91fbf76683SChuck Lever static struct ctl_table_header *sunrpc_table_header;
92fbf76683SChuck Lever 
93fbf76683SChuck Lever /*
94fbf76683SChuck Lever  * FIXME: changing the UDP slot table size should also resize the UDP
95fbf76683SChuck Lever  *        socket buffers for existing UDP transports
96fbf76683SChuck Lever  */
97fe2c6338SJoe Perches static struct ctl_table xs_tunables_table[] = {
98fbf76683SChuck Lever 	{
99fbf76683SChuck Lever 		.procname	= "udp_slot_table_entries",
100fbf76683SChuck Lever 		.data		= &xprt_udp_slot_table_entries,
101fbf76683SChuck Lever 		.maxlen		= sizeof(unsigned int),
102fbf76683SChuck Lever 		.mode		= 0644,
1036d456111SEric W. Biederman 		.proc_handler	= proc_dointvec_minmax,
104fbf76683SChuck Lever 		.extra1		= &min_slot_table_size,
105fbf76683SChuck Lever 		.extra2		= &max_slot_table_size
106fbf76683SChuck Lever 	},
107fbf76683SChuck Lever 	{
108fbf76683SChuck Lever 		.procname	= "tcp_slot_table_entries",
109fbf76683SChuck Lever 		.data		= &xprt_tcp_slot_table_entries,
110fbf76683SChuck Lever 		.maxlen		= sizeof(unsigned int),
111fbf76683SChuck Lever 		.mode		= 0644,
1126d456111SEric W. Biederman 		.proc_handler	= proc_dointvec_minmax,
113fbf76683SChuck Lever 		.extra1		= &min_slot_table_size,
114fbf76683SChuck Lever 		.extra2		= &max_slot_table_size
115fbf76683SChuck Lever 	},
116fbf76683SChuck Lever 	{
117d9ba131dSTrond Myklebust 		.procname	= "tcp_max_slot_table_entries",
118d9ba131dSTrond Myklebust 		.data		= &xprt_max_tcp_slot_table_entries,
119d9ba131dSTrond Myklebust 		.maxlen		= sizeof(unsigned int),
120d9ba131dSTrond Myklebust 		.mode		= 0644,
121d9ba131dSTrond Myklebust 		.proc_handler	= proc_dointvec_minmax,
122d9ba131dSTrond Myklebust 		.extra1		= &min_slot_table_size,
123d9ba131dSTrond Myklebust 		.extra2		= &max_tcp_slot_table_limit
124d9ba131dSTrond Myklebust 	},
125d9ba131dSTrond Myklebust 	{
126fbf76683SChuck Lever 		.procname	= "min_resvport",
127fbf76683SChuck Lever 		.data		= &xprt_min_resvport,
128fbf76683SChuck Lever 		.maxlen		= sizeof(unsigned int),
129fbf76683SChuck Lever 		.mode		= 0644,
1306d456111SEric W. Biederman 		.proc_handler	= proc_dointvec_minmax,
131fbf76683SChuck Lever 		.extra1		= &xprt_min_resvport_limit,
132826799e6SJ. Bruce Fields 		.extra2		= &xprt_max_resvport_limit
133fbf76683SChuck Lever 	},
134fbf76683SChuck Lever 	{
135fbf76683SChuck Lever 		.procname	= "max_resvport",
136fbf76683SChuck Lever 		.data		= &xprt_max_resvport,
137fbf76683SChuck Lever 		.maxlen		= sizeof(unsigned int),
138fbf76683SChuck Lever 		.mode		= 0644,
1396d456111SEric W. Biederman 		.proc_handler	= proc_dointvec_minmax,
140826799e6SJ. Bruce Fields 		.extra1		= &xprt_min_resvport_limit,
141fbf76683SChuck Lever 		.extra2		= &xprt_max_resvport_limit
142fbf76683SChuck Lever 	},
143fbf76683SChuck Lever 	{
14425fe6142STrond Myklebust 		.procname	= "tcp_fin_timeout",
14525fe6142STrond Myklebust 		.data		= &xs_tcp_fin_timeout,
14625fe6142STrond Myklebust 		.maxlen		= sizeof(xs_tcp_fin_timeout),
14725fe6142STrond Myklebust 		.mode		= 0644,
1486d456111SEric W. Biederman 		.proc_handler	= proc_dointvec_jiffies,
14925fe6142STrond Myklebust 	},
150f8572d8fSEric W. Biederman 	{ },
151fbf76683SChuck Lever };
152fbf76683SChuck Lever 
153fe2c6338SJoe Perches static struct ctl_table sunrpc_table[] = {
154fbf76683SChuck Lever 	{
155fbf76683SChuck Lever 		.procname	= "sunrpc",
156fbf76683SChuck Lever 		.mode		= 0555,
157fbf76683SChuck Lever 		.child		= xs_tunables_table
158fbf76683SChuck Lever 	},
159f8572d8fSEric W. Biederman 	{ },
160fbf76683SChuck Lever };
161fbf76683SChuck Lever 
162fbf76683SChuck Lever /*
16303bf4b70SChuck Lever  * Wait duration for a reply from the RPC portmapper.
16403bf4b70SChuck Lever  */
16503bf4b70SChuck Lever #define XS_BIND_TO		(60U * HZ)
16603bf4b70SChuck Lever 
16703bf4b70SChuck Lever /*
16803bf4b70SChuck Lever  * Delay if a UDP socket connect error occurs.  This is most likely some
16903bf4b70SChuck Lever  * kind of resource problem on the local host.
17003bf4b70SChuck Lever  */
17103bf4b70SChuck Lever #define XS_UDP_REEST_TO		(2U * HZ)
17203bf4b70SChuck Lever 
17303bf4b70SChuck Lever /*
17403bf4b70SChuck Lever  * The reestablish timeout allows clients to delay for a bit before attempting
17503bf4b70SChuck Lever  * to reconnect to a server that just dropped our connection.
17603bf4b70SChuck Lever  *
17703bf4b70SChuck Lever  * We implement an exponential backoff when trying to reestablish a TCP
17803bf4b70SChuck Lever  * transport connection with the server.  Some servers like to drop a TCP
17903bf4b70SChuck Lever  * connection when they are overworked, so we start with a short timeout and
18003bf4b70SChuck Lever  * increase over time if the server is down or not responding.
18103bf4b70SChuck Lever  */
18203bf4b70SChuck Lever #define XS_TCP_INIT_REEST_TO	(3U * HZ)
18303bf4b70SChuck Lever 
18403bf4b70SChuck Lever /*
18503bf4b70SChuck Lever  * TCP idle timeout; client drops the transport socket if it is idle
18603bf4b70SChuck Lever  * for this long.  Note that we also timeout UDP sockets to prevent
18703bf4b70SChuck Lever  * holding port numbers when there is no RPC traffic.
18803bf4b70SChuck Lever  */
18903bf4b70SChuck Lever #define XS_IDLE_DISC_TO		(5U * 60 * HZ)
19003bf4b70SChuck Lever 
191f895b252SJeff Layton #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
192a246b010SChuck Lever # undef  RPC_DEBUG_DATA
1939903cd1cSChuck Lever # define RPCDBG_FACILITY	RPCDBG_TRANS
194a246b010SChuck Lever #endif
195a246b010SChuck Lever 
196a246b010SChuck Lever #ifdef RPC_DEBUG_DATA
1979903cd1cSChuck Lever static void xs_pktdump(char *msg, u32 *packet, unsigned int count)
198a246b010SChuck Lever {
199a246b010SChuck Lever 	u8 *buf = (u8 *) packet;
200a246b010SChuck Lever 	int j;
201a246b010SChuck Lever 
202a246b010SChuck Lever 	dprintk("RPC:       %s\n", msg);
203a246b010SChuck Lever 	for (j = 0; j < count && j < 128; j += 4) {
204a246b010SChuck Lever 		if (!(j & 31)) {
205a246b010SChuck Lever 			if (j)
206a246b010SChuck Lever 				dprintk("\n");
207a246b010SChuck Lever 			dprintk("0x%04x ", j);
208a246b010SChuck Lever 		}
209a246b010SChuck Lever 		dprintk("%02x%02x%02x%02x ",
210a246b010SChuck Lever 			buf[j], buf[j+1], buf[j+2], buf[j+3]);
211a246b010SChuck Lever 	}
212a246b010SChuck Lever 	dprintk("\n");
213a246b010SChuck Lever }
214a246b010SChuck Lever #else
2159903cd1cSChuck Lever static inline void xs_pktdump(char *msg, u32 *packet, unsigned int count)
216a246b010SChuck Lever {
217a246b010SChuck Lever 	/* NOP */
218a246b010SChuck Lever }
219a246b010SChuck Lever #endif
220a246b010SChuck Lever 
2212118071dSTrond Myklebust static inline struct rpc_xprt *xprt_from_sock(struct sock *sk)
2222118071dSTrond Myklebust {
2232118071dSTrond Myklebust 	return (struct rpc_xprt *) sk->sk_user_data;
2242118071dSTrond Myklebust }
2252118071dSTrond Myklebust 
22695392c59SChuck Lever static inline struct sockaddr *xs_addr(struct rpc_xprt *xprt)
227edb267a6SChuck Lever {
22895392c59SChuck Lever 	return (struct sockaddr *) &xprt->addr;
22995392c59SChuck Lever }
23095392c59SChuck Lever 
231176e21eeSChuck Lever static inline struct sockaddr_un *xs_addr_un(struct rpc_xprt *xprt)
232176e21eeSChuck Lever {
233176e21eeSChuck Lever 	return (struct sockaddr_un *) &xprt->addr;
234176e21eeSChuck Lever }
235176e21eeSChuck Lever 
23695392c59SChuck Lever static inline struct sockaddr_in *xs_addr_in(struct rpc_xprt *xprt)
23795392c59SChuck Lever {
23895392c59SChuck Lever 	return (struct sockaddr_in *) &xprt->addr;
23995392c59SChuck Lever }
24095392c59SChuck Lever 
24195392c59SChuck Lever static inline struct sockaddr_in6 *xs_addr_in6(struct rpc_xprt *xprt)
24295392c59SChuck Lever {
24395392c59SChuck Lever 	return (struct sockaddr_in6 *) &xprt->addr;
24495392c59SChuck Lever }
24595392c59SChuck Lever 
246c877b849SChuck Lever static void xs_format_common_peer_addresses(struct rpc_xprt *xprt)
247c877b849SChuck Lever {
248c877b849SChuck Lever 	struct sockaddr *sap = xs_addr(xprt);
2499dc3b095SChuck Lever 	struct sockaddr_in6 *sin6;
2509dc3b095SChuck Lever 	struct sockaddr_in *sin;
251176e21eeSChuck Lever 	struct sockaddr_un *sun;
252c877b849SChuck Lever 	char buf[128];
253c877b849SChuck Lever 
2549dc3b095SChuck Lever 	switch (sap->sa_family) {
255176e21eeSChuck Lever 	case AF_LOCAL:
256176e21eeSChuck Lever 		sun = xs_addr_un(xprt);
257176e21eeSChuck Lever 		strlcpy(buf, sun->sun_path, sizeof(buf));
258176e21eeSChuck Lever 		xprt->address_strings[RPC_DISPLAY_ADDR] =
259176e21eeSChuck Lever 						kstrdup(buf, GFP_KERNEL);
260176e21eeSChuck Lever 		break;
2619dc3b095SChuck Lever 	case AF_INET:
262176e21eeSChuck Lever 		(void)rpc_ntop(sap, buf, sizeof(buf));
263176e21eeSChuck Lever 		xprt->address_strings[RPC_DISPLAY_ADDR] =
264176e21eeSChuck Lever 						kstrdup(buf, GFP_KERNEL);
2659dc3b095SChuck Lever 		sin = xs_addr_in(xprt);
266fc0b5791SJoe Perches 		snprintf(buf, sizeof(buf), "%08x", ntohl(sin->sin_addr.s_addr));
2679dc3b095SChuck Lever 		break;
2689dc3b095SChuck Lever 	case AF_INET6:
269176e21eeSChuck Lever 		(void)rpc_ntop(sap, buf, sizeof(buf));
270176e21eeSChuck Lever 		xprt->address_strings[RPC_DISPLAY_ADDR] =
271176e21eeSChuck Lever 						kstrdup(buf, GFP_KERNEL);
2729dc3b095SChuck Lever 		sin6 = xs_addr_in6(xprt);
273fc0b5791SJoe Perches 		snprintf(buf, sizeof(buf), "%pi6", &sin6->sin6_addr);
2749dc3b095SChuck Lever 		break;
2759dc3b095SChuck Lever 	default:
2769dc3b095SChuck Lever 		BUG();
2779dc3b095SChuck Lever 	}
278176e21eeSChuck Lever 
2799dc3b095SChuck Lever 	xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = kstrdup(buf, GFP_KERNEL);
2809dc3b095SChuck Lever }
2819dc3b095SChuck Lever 
2829dc3b095SChuck Lever static void xs_format_common_peer_ports(struct rpc_xprt *xprt)
2839dc3b095SChuck Lever {
2849dc3b095SChuck Lever 	struct sockaddr *sap = xs_addr(xprt);
2859dc3b095SChuck Lever 	char buf[128];
2869dc3b095SChuck Lever 
28781160e66SJoe Perches 	snprintf(buf, sizeof(buf), "%u", rpc_get_port(sap));
288c877b849SChuck Lever 	xprt->address_strings[RPC_DISPLAY_PORT] = kstrdup(buf, GFP_KERNEL);
289c877b849SChuck Lever 
29081160e66SJoe Perches 	snprintf(buf, sizeof(buf), "%4hx", rpc_get_port(sap));
291c877b849SChuck Lever 	xprt->address_strings[RPC_DISPLAY_HEX_PORT] = kstrdup(buf, GFP_KERNEL);
292c877b849SChuck Lever }
293c877b849SChuck Lever 
2949dc3b095SChuck Lever static void xs_format_peer_addresses(struct rpc_xprt *xprt,
295b454ae90SChuck Lever 				     const char *protocol,
296b454ae90SChuck Lever 				     const char *netid)
297edb267a6SChuck Lever {
298b454ae90SChuck Lever 	xprt->address_strings[RPC_DISPLAY_PROTO] = protocol;
299b454ae90SChuck Lever 	xprt->address_strings[RPC_DISPLAY_NETID] = netid;
300c877b849SChuck Lever 	xs_format_common_peer_addresses(xprt);
3019dc3b095SChuck Lever 	xs_format_common_peer_ports(xprt);
302edb267a6SChuck Lever }
303edb267a6SChuck Lever 
3049dc3b095SChuck Lever static void xs_update_peer_port(struct rpc_xprt *xprt)
3054b6473fbSChuck Lever {
3069dc3b095SChuck Lever 	kfree(xprt->address_strings[RPC_DISPLAY_HEX_PORT]);
3079dc3b095SChuck Lever 	kfree(xprt->address_strings[RPC_DISPLAY_PORT]);
3084b6473fbSChuck Lever 
3099dc3b095SChuck Lever 	xs_format_common_peer_ports(xprt);
310edb267a6SChuck Lever }
311edb267a6SChuck Lever 
312edb267a6SChuck Lever static void xs_free_peer_addresses(struct rpc_xprt *xprt)
313edb267a6SChuck Lever {
31433e01dc7SChuck Lever 	unsigned int i;
31533e01dc7SChuck Lever 
31633e01dc7SChuck Lever 	for (i = 0; i < RPC_DISPLAY_MAX; i++)
31733e01dc7SChuck Lever 		switch (i) {
31833e01dc7SChuck Lever 		case RPC_DISPLAY_PROTO:
31933e01dc7SChuck Lever 		case RPC_DISPLAY_NETID:
32033e01dc7SChuck Lever 			continue;
32133e01dc7SChuck Lever 		default:
32233e01dc7SChuck Lever 			kfree(xprt->address_strings[i]);
32333e01dc7SChuck Lever 		}
324edb267a6SChuck Lever }
325edb267a6SChuck Lever 
326277e4ab7STrond Myklebust static size_t
327277e4ab7STrond Myklebust xs_alloc_sparse_pages(struct xdr_buf *buf, size_t want, gfp_t gfp)
328277e4ab7STrond Myklebust {
329277e4ab7STrond Myklebust 	size_t i,n;
330277e4ab7STrond Myklebust 
33116e5e90fSTrond Myklebust 	if (!want || !(buf->flags & XDRBUF_SPARSE_PAGES))
332277e4ab7STrond Myklebust 		return want;
333277e4ab7STrond Myklebust 	n = (buf->page_base + want + PAGE_SIZE - 1) >> PAGE_SHIFT;
334277e4ab7STrond Myklebust 	for (i = 0; i < n; i++) {
335277e4ab7STrond Myklebust 		if (buf->pages[i])
336277e4ab7STrond Myklebust 			continue;
337277e4ab7STrond Myklebust 		buf->bvec[i].bv_page = buf->pages[i] = alloc_page(gfp);
338277e4ab7STrond Myklebust 		if (!buf->pages[i]) {
33916e5e90fSTrond Myklebust 			i *= PAGE_SIZE;
34016e5e90fSTrond Myklebust 			return i > buf->page_base ? i - buf->page_base : 0;
341277e4ab7STrond Myklebust 		}
342277e4ab7STrond Myklebust 	}
343277e4ab7STrond Myklebust 	return want;
344277e4ab7STrond Myklebust }
345277e4ab7STrond Myklebust 
346277e4ab7STrond Myklebust static ssize_t
347277e4ab7STrond Myklebust xs_sock_recvmsg(struct socket *sock, struct msghdr *msg, int flags, size_t seek)
348277e4ab7STrond Myklebust {
349277e4ab7STrond Myklebust 	ssize_t ret;
350277e4ab7STrond Myklebust 	if (seek != 0)
351277e4ab7STrond Myklebust 		iov_iter_advance(&msg->msg_iter, seek);
352277e4ab7STrond Myklebust 	ret = sock_recvmsg(sock, msg, flags);
353277e4ab7STrond Myklebust 	return ret > 0 ? ret + seek : ret;
354277e4ab7STrond Myklebust }
355277e4ab7STrond Myklebust 
356277e4ab7STrond Myklebust static ssize_t
357277e4ab7STrond Myklebust xs_read_kvec(struct socket *sock, struct msghdr *msg, int flags,
358277e4ab7STrond Myklebust 		struct kvec *kvec, size_t count, size_t seek)
359277e4ab7STrond Myklebust {
3600e9b4a82SAl Viro 	iov_iter_kvec(&msg->msg_iter, READ, kvec, 1, count);
361277e4ab7STrond Myklebust 	return xs_sock_recvmsg(sock, msg, flags, seek);
362277e4ab7STrond Myklebust }
363277e4ab7STrond Myklebust 
364277e4ab7STrond Myklebust static ssize_t
365277e4ab7STrond Myklebust xs_read_bvec(struct socket *sock, struct msghdr *msg, int flags,
366277e4ab7STrond Myklebust 		struct bio_vec *bvec, unsigned long nr, size_t count,
367277e4ab7STrond Myklebust 		size_t seek)
368277e4ab7STrond Myklebust {
3690e9b4a82SAl Viro 	iov_iter_bvec(&msg->msg_iter, READ, bvec, nr, count);
370277e4ab7STrond Myklebust 	return xs_sock_recvmsg(sock, msg, flags, seek);
371277e4ab7STrond Myklebust }
372277e4ab7STrond Myklebust 
373277e4ab7STrond Myklebust static ssize_t
374277e4ab7STrond Myklebust xs_read_discard(struct socket *sock, struct msghdr *msg, int flags,
375277e4ab7STrond Myklebust 		size_t count)
376277e4ab7STrond Myklebust {
377b76a5afdSTrond Myklebust 	iov_iter_discard(&msg->msg_iter, READ, count);
378b76a5afdSTrond Myklebust 	return sock_recvmsg(sock, msg, flags);
379277e4ab7STrond Myklebust }
380277e4ab7STrond Myklebust 
3816a829eb8STrond Myklebust #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE
3826a829eb8STrond Myklebust static void
3836a829eb8STrond Myklebust xs_flush_bvec(const struct bio_vec *bvec, size_t count, size_t seek)
3846a829eb8STrond Myklebust {
3856a829eb8STrond Myklebust 	struct bvec_iter bi = {
3866a829eb8STrond Myklebust 		.bi_size = count,
3876a829eb8STrond Myklebust 	};
3886a829eb8STrond Myklebust 	struct bio_vec bv;
3896a829eb8STrond Myklebust 
3906a829eb8STrond Myklebust 	bvec_iter_advance(bvec, &bi, seek & PAGE_MASK);
3916a829eb8STrond Myklebust 	for_each_bvec(bv, bvec, bi, bi)
3926a829eb8STrond Myklebust 		flush_dcache_page(bv.bv_page);
3936a829eb8STrond Myklebust }
3946a829eb8STrond Myklebust #else
3956a829eb8STrond Myklebust static inline void
3966a829eb8STrond Myklebust xs_flush_bvec(const struct bio_vec *bvec, size_t count, size_t seek)
3976a829eb8STrond Myklebust {
3986a829eb8STrond Myklebust }
3996a829eb8STrond Myklebust #endif
4006a829eb8STrond Myklebust 
401277e4ab7STrond Myklebust static ssize_t
402277e4ab7STrond Myklebust xs_read_xdr_buf(struct socket *sock, struct msghdr *msg, int flags,
403277e4ab7STrond Myklebust 		struct xdr_buf *buf, size_t count, size_t seek, size_t *read)
404277e4ab7STrond Myklebust {
405277e4ab7STrond Myklebust 	size_t want, seek_init = seek, offset = 0;
406277e4ab7STrond Myklebust 	ssize_t ret;
407277e4ab7STrond Myklebust 
408277e4ab7STrond Myklebust 	want = min_t(size_t, count, buf->head[0].iov_len);
409e92053a5STrond Myklebust 	if (seek < want) {
410277e4ab7STrond Myklebust 		ret = xs_read_kvec(sock, msg, flags, &buf->head[0], want, seek);
411277e4ab7STrond Myklebust 		if (ret <= 0)
412277e4ab7STrond Myklebust 			goto sock_err;
413277e4ab7STrond Myklebust 		offset += ret;
414277e4ab7STrond Myklebust 		if (offset == count || msg->msg_flags & (MSG_EOR|MSG_TRUNC))
415277e4ab7STrond Myklebust 			goto out;
416277e4ab7STrond Myklebust 		if (ret != want)
417c4433055STrond Myklebust 			goto out;
418277e4ab7STrond Myklebust 		seek = 0;
419277e4ab7STrond Myklebust 	} else {
420e92053a5STrond Myklebust 		seek -= want;
421e92053a5STrond Myklebust 		offset += want;
422277e4ab7STrond Myklebust 	}
42316e5e90fSTrond Myklebust 
424277e4ab7STrond Myklebust 	want = xs_alloc_sparse_pages(buf,
425277e4ab7STrond Myklebust 			min_t(size_t, count - offset, buf->page_len),
42612a3ad61STrond Myklebust 			GFP_KERNEL);
42716e5e90fSTrond Myklebust 	if (seek < want) {
428277e4ab7STrond Myklebust 		ret = xs_read_bvec(sock, msg, flags, buf->bvec,
429277e4ab7STrond Myklebust 				xdr_buf_pagecount(buf),
430277e4ab7STrond Myklebust 				want + buf->page_base,
431277e4ab7STrond Myklebust 				seek + buf->page_base);
432277e4ab7STrond Myklebust 		if (ret <= 0)
433277e4ab7STrond Myklebust 			goto sock_err;
4346a829eb8STrond Myklebust 		xs_flush_bvec(buf->bvec, ret, seek + buf->page_base);
435277e4ab7STrond Myklebust 		offset += ret - buf->page_base;
436277e4ab7STrond Myklebust 		if (offset == count || msg->msg_flags & (MSG_EOR|MSG_TRUNC))
437277e4ab7STrond Myklebust 			goto out;
438277e4ab7STrond Myklebust 		if (ret != want)
439c4433055STrond Myklebust 			goto out;
440277e4ab7STrond Myklebust 		seek = 0;
441277e4ab7STrond Myklebust 	} else {
44216e5e90fSTrond Myklebust 		seek -= want;
44316e5e90fSTrond Myklebust 		offset += want;
444277e4ab7STrond Myklebust 	}
44516e5e90fSTrond Myklebust 
446277e4ab7STrond Myklebust 	want = min_t(size_t, count - offset, buf->tail[0].iov_len);
447e92053a5STrond Myklebust 	if (seek < want) {
448277e4ab7STrond Myklebust 		ret = xs_read_kvec(sock, msg, flags, &buf->tail[0], want, seek);
449277e4ab7STrond Myklebust 		if (ret <= 0)
450277e4ab7STrond Myklebust 			goto sock_err;
451277e4ab7STrond Myklebust 		offset += ret;
452277e4ab7STrond Myklebust 		if (offset == count || msg->msg_flags & (MSG_EOR|MSG_TRUNC))
453277e4ab7STrond Myklebust 			goto out;
454277e4ab7STrond Myklebust 		if (ret != want)
455c4433055STrond Myklebust 			goto out;
4569734ad57STrond Myklebust 	} else if (offset < seek_init)
457e92053a5STrond Myklebust 		offset = seek_init;
458277e4ab7STrond Myklebust 	ret = -EMSGSIZE;
459277e4ab7STrond Myklebust out:
460277e4ab7STrond Myklebust 	*read = offset - seek_init;
461277e4ab7STrond Myklebust 	return ret;
462277e4ab7STrond Myklebust sock_err:
463277e4ab7STrond Myklebust 	offset += seek;
464277e4ab7STrond Myklebust 	goto out;
465277e4ab7STrond Myklebust }
466277e4ab7STrond Myklebust 
467277e4ab7STrond Myklebust static void
468277e4ab7STrond Myklebust xs_read_header(struct sock_xprt *transport, struct xdr_buf *buf)
469277e4ab7STrond Myklebust {
470277e4ab7STrond Myklebust 	if (!transport->recv.copied) {
471277e4ab7STrond Myklebust 		if (buf->head[0].iov_len >= transport->recv.offset)
472277e4ab7STrond Myklebust 			memcpy(buf->head[0].iov_base,
473277e4ab7STrond Myklebust 					&transport->recv.xid,
474277e4ab7STrond Myklebust 					transport->recv.offset);
475277e4ab7STrond Myklebust 		transport->recv.copied = transport->recv.offset;
476277e4ab7STrond Myklebust 	}
477277e4ab7STrond Myklebust }
478277e4ab7STrond Myklebust 
479277e4ab7STrond Myklebust static bool
480277e4ab7STrond Myklebust xs_read_stream_request_done(struct sock_xprt *transport)
481277e4ab7STrond Myklebust {
482277e4ab7STrond Myklebust 	return transport->recv.fraghdr & cpu_to_be32(RPC_LAST_STREAM_FRAGMENT);
483277e4ab7STrond Myklebust }
484277e4ab7STrond Myklebust 
485e92053a5STrond Myklebust static void
486e92053a5STrond Myklebust xs_read_stream_check_eor(struct sock_xprt *transport,
487e92053a5STrond Myklebust 		struct msghdr *msg)
488e92053a5STrond Myklebust {
489e92053a5STrond Myklebust 	if (xs_read_stream_request_done(transport))
490e92053a5STrond Myklebust 		msg->msg_flags |= MSG_EOR;
491e92053a5STrond Myklebust }
492e92053a5STrond Myklebust 
493277e4ab7STrond Myklebust static ssize_t
494277e4ab7STrond Myklebust xs_read_stream_request(struct sock_xprt *transport, struct msghdr *msg,
495277e4ab7STrond Myklebust 		int flags, struct rpc_rqst *req)
496277e4ab7STrond Myklebust {
497277e4ab7STrond Myklebust 	struct xdr_buf *buf = &req->rq_private_buf;
49801f2f5b8SAlakesh Haloi 	size_t want, uninitialized_var(read);
49901f2f5b8SAlakesh Haloi 	ssize_t uninitialized_var(ret);
500277e4ab7STrond Myklebust 
501277e4ab7STrond Myklebust 	xs_read_header(transport, buf);
502277e4ab7STrond Myklebust 
503277e4ab7STrond Myklebust 	want = transport->recv.len - transport->recv.offset;
504e92053a5STrond Myklebust 	if (want != 0) {
505277e4ab7STrond Myklebust 		ret = xs_read_xdr_buf(transport->sock, msg, flags, buf,
506e92053a5STrond Myklebust 				transport->recv.copied + want,
507e92053a5STrond Myklebust 				transport->recv.copied,
508277e4ab7STrond Myklebust 				&read);
509277e4ab7STrond Myklebust 		transport->recv.offset += read;
510277e4ab7STrond Myklebust 		transport->recv.copied += read;
511277e4ab7STrond Myklebust 	}
512277e4ab7STrond Myklebust 
513727fcc64STrond Myklebust 	if (transport->recv.offset == transport->recv.len)
514727fcc64STrond Myklebust 		xs_read_stream_check_eor(transport, msg);
515727fcc64STrond Myklebust 
516e92053a5STrond Myklebust 	if (want == 0)
517e92053a5STrond Myklebust 		return 0;
518e92053a5STrond Myklebust 
519277e4ab7STrond Myklebust 	switch (ret) {
520c4433055STrond Myklebust 	default:
521c4433055STrond Myklebust 		break;
52226781eabSTrond Myklebust 	case -EFAULT:
523277e4ab7STrond Myklebust 	case -EMSGSIZE:
52426781eabSTrond Myklebust 		msg->msg_flags |= MSG_TRUNC;
525c4433055STrond Myklebust 		return read;
526277e4ab7STrond Myklebust 	case 0:
527277e4ab7STrond Myklebust 		return -ESHUTDOWN;
528277e4ab7STrond Myklebust 	}
529c4433055STrond Myklebust 	return ret < 0 ? ret : read;
530277e4ab7STrond Myklebust }
531277e4ab7STrond Myklebust 
532277e4ab7STrond Myklebust static size_t
533277e4ab7STrond Myklebust xs_read_stream_headersize(bool isfrag)
534277e4ab7STrond Myklebust {
535277e4ab7STrond Myklebust 	if (isfrag)
536277e4ab7STrond Myklebust 		return sizeof(__be32);
537277e4ab7STrond Myklebust 	return 3 * sizeof(__be32);
538277e4ab7STrond Myklebust }
539277e4ab7STrond Myklebust 
540277e4ab7STrond Myklebust static ssize_t
541277e4ab7STrond Myklebust xs_read_stream_header(struct sock_xprt *transport, struct msghdr *msg,
542277e4ab7STrond Myklebust 		int flags, size_t want, size_t seek)
543277e4ab7STrond Myklebust {
544277e4ab7STrond Myklebust 	struct kvec kvec = {
545277e4ab7STrond Myklebust 		.iov_base = &transport->recv.fraghdr,
546277e4ab7STrond Myklebust 		.iov_len = want,
547277e4ab7STrond Myklebust 	};
548277e4ab7STrond Myklebust 	return xs_read_kvec(transport->sock, msg, flags, &kvec, want, seek);
549277e4ab7STrond Myklebust }
550277e4ab7STrond Myklebust 
551277e4ab7STrond Myklebust #if defined(CONFIG_SUNRPC_BACKCHANNEL)
552277e4ab7STrond Myklebust static ssize_t
553277e4ab7STrond Myklebust xs_read_stream_call(struct sock_xprt *transport, struct msghdr *msg, int flags)
554277e4ab7STrond Myklebust {
555277e4ab7STrond Myklebust 	struct rpc_xprt *xprt = &transport->xprt;
556277e4ab7STrond Myklebust 	struct rpc_rqst *req;
557277e4ab7STrond Myklebust 	ssize_t ret;
558277e4ab7STrond Myklebust 
559277e4ab7STrond Myklebust 	/* Look up and lock the request corresponding to the given XID */
560277e4ab7STrond Myklebust 	req = xprt_lookup_bc_request(xprt, transport->recv.xid);
561277e4ab7STrond Myklebust 	if (!req) {
562277e4ab7STrond Myklebust 		printk(KERN_WARNING "Callback slot table overflowed\n");
563277e4ab7STrond Myklebust 		return -ESHUTDOWN;
564277e4ab7STrond Myklebust 	}
565277e4ab7STrond Myklebust 
566277e4ab7STrond Myklebust 	ret = xs_read_stream_request(transport, msg, flags, req);
567277e4ab7STrond Myklebust 	if (msg->msg_flags & (MSG_EOR|MSG_TRUNC))
568c4433055STrond Myklebust 		xprt_complete_bc_request(req, transport->recv.copied);
569277e4ab7STrond Myklebust 
570277e4ab7STrond Myklebust 	return ret;
571277e4ab7STrond Myklebust }
572277e4ab7STrond Myklebust #else /* CONFIG_SUNRPC_BACKCHANNEL */
573277e4ab7STrond Myklebust static ssize_t
574277e4ab7STrond Myklebust xs_read_stream_call(struct sock_xprt *transport, struct msghdr *msg, int flags)
575277e4ab7STrond Myklebust {
576277e4ab7STrond Myklebust 	return -ESHUTDOWN;
577277e4ab7STrond Myklebust }
578277e4ab7STrond Myklebust #endif /* CONFIG_SUNRPC_BACKCHANNEL */
579277e4ab7STrond Myklebust 
580277e4ab7STrond Myklebust static ssize_t
581277e4ab7STrond Myklebust xs_read_stream_reply(struct sock_xprt *transport, struct msghdr *msg, int flags)
582277e4ab7STrond Myklebust {
583277e4ab7STrond Myklebust 	struct rpc_xprt *xprt = &transport->xprt;
584277e4ab7STrond Myklebust 	struct rpc_rqst *req;
585277e4ab7STrond Myklebust 	ssize_t ret = 0;
586277e4ab7STrond Myklebust 
587277e4ab7STrond Myklebust 	/* Look up and lock the request corresponding to the given XID */
588277e4ab7STrond Myklebust 	spin_lock(&xprt->queue_lock);
589277e4ab7STrond Myklebust 	req = xprt_lookup_rqst(xprt, transport->recv.xid);
590277e4ab7STrond Myklebust 	if (!req) {
591277e4ab7STrond Myklebust 		msg->msg_flags |= MSG_TRUNC;
592277e4ab7STrond Myklebust 		goto out;
593277e4ab7STrond Myklebust 	}
594277e4ab7STrond Myklebust 	xprt_pin_rqst(req);
595277e4ab7STrond Myklebust 	spin_unlock(&xprt->queue_lock);
596277e4ab7STrond Myklebust 
597277e4ab7STrond Myklebust 	ret = xs_read_stream_request(transport, msg, flags, req);
598277e4ab7STrond Myklebust 
599277e4ab7STrond Myklebust 	spin_lock(&xprt->queue_lock);
600277e4ab7STrond Myklebust 	if (msg->msg_flags & (MSG_EOR|MSG_TRUNC))
601c4433055STrond Myklebust 		xprt_complete_rqst(req->rq_task, transport->recv.copied);
602277e4ab7STrond Myklebust 	xprt_unpin_rqst(req);
603277e4ab7STrond Myklebust out:
604277e4ab7STrond Myklebust 	spin_unlock(&xprt->queue_lock);
605277e4ab7STrond Myklebust 	return ret;
606277e4ab7STrond Myklebust }
607277e4ab7STrond Myklebust 
608277e4ab7STrond Myklebust static ssize_t
609277e4ab7STrond Myklebust xs_read_stream(struct sock_xprt *transport, int flags)
610277e4ab7STrond Myklebust {
611277e4ab7STrond Myklebust 	struct msghdr msg = { 0 };
612277e4ab7STrond Myklebust 	size_t want, read = 0;
613277e4ab7STrond Myklebust 	ssize_t ret = 0;
614277e4ab7STrond Myklebust 
615277e4ab7STrond Myklebust 	if (transport->recv.len == 0) {
616277e4ab7STrond Myklebust 		want = xs_read_stream_headersize(transport->recv.copied != 0);
617277e4ab7STrond Myklebust 		ret = xs_read_stream_header(transport, &msg, flags, want,
618277e4ab7STrond Myklebust 				transport->recv.offset);
619277e4ab7STrond Myklebust 		if (ret <= 0)
620277e4ab7STrond Myklebust 			goto out_err;
621277e4ab7STrond Myklebust 		transport->recv.offset = ret;
622c4433055STrond Myklebust 		if (transport->recv.offset != want)
623c4433055STrond Myklebust 			return transport->recv.offset;
624277e4ab7STrond Myklebust 		transport->recv.len = be32_to_cpu(transport->recv.fraghdr) &
625277e4ab7STrond Myklebust 			RPC_FRAGMENT_SIZE_MASK;
626277e4ab7STrond Myklebust 		transport->recv.offset -= sizeof(transport->recv.fraghdr);
627277e4ab7STrond Myklebust 		read = ret;
628277e4ab7STrond Myklebust 	}
629277e4ab7STrond Myklebust 
630277e4ab7STrond Myklebust 	switch (be32_to_cpu(transport->recv.calldir)) {
631c4433055STrond Myklebust 	default:
632c4433055STrond Myklebust 		msg.msg_flags |= MSG_TRUNC;
633c4433055STrond Myklebust 		break;
634277e4ab7STrond Myklebust 	case RPC_CALL:
635277e4ab7STrond Myklebust 		ret = xs_read_stream_call(transport, &msg, flags);
636277e4ab7STrond Myklebust 		break;
637277e4ab7STrond Myklebust 	case RPC_REPLY:
638277e4ab7STrond Myklebust 		ret = xs_read_stream_reply(transport, &msg, flags);
639277e4ab7STrond Myklebust 	}
640277e4ab7STrond Myklebust 	if (msg.msg_flags & MSG_TRUNC) {
641277e4ab7STrond Myklebust 		transport->recv.calldir = cpu_to_be32(-1);
642277e4ab7STrond Myklebust 		transport->recv.copied = -1;
643277e4ab7STrond Myklebust 	}
644277e4ab7STrond Myklebust 	if (ret < 0)
645277e4ab7STrond Myklebust 		goto out_err;
646277e4ab7STrond Myklebust 	read += ret;
647277e4ab7STrond Myklebust 	if (transport->recv.offset < transport->recv.len) {
648c4433055STrond Myklebust 		if (!(msg.msg_flags & MSG_TRUNC))
649c4433055STrond Myklebust 			return read;
650b76a5afdSTrond Myklebust 		msg.msg_flags = 0;
651277e4ab7STrond Myklebust 		ret = xs_read_discard(transport->sock, &msg, flags,
652277e4ab7STrond Myklebust 				transport->recv.len - transport->recv.offset);
653277e4ab7STrond Myklebust 		if (ret <= 0)
654277e4ab7STrond Myklebust 			goto out_err;
655277e4ab7STrond Myklebust 		transport->recv.offset += ret;
656277e4ab7STrond Myklebust 		read += ret;
657277e4ab7STrond Myklebust 		if (transport->recv.offset != transport->recv.len)
658c4433055STrond Myklebust 			return read;
659277e4ab7STrond Myklebust 	}
660277e4ab7STrond Myklebust 	if (xs_read_stream_request_done(transport)) {
661c50b8ee0STrond Myklebust 		trace_xs_stream_read_request(transport);
662277e4ab7STrond Myklebust 		transport->recv.copied = 0;
663277e4ab7STrond Myklebust 	}
664277e4ab7STrond Myklebust 	transport->recv.offset = 0;
665277e4ab7STrond Myklebust 	transport->recv.len = 0;
666277e4ab7STrond Myklebust 	return read;
667277e4ab7STrond Myklebust out_err:
66879462857STrond Myklebust 	return ret != 0 ? ret : -ESHUTDOWN;
669277e4ab7STrond Myklebust }
670277e4ab7STrond Myklebust 
6710ffe86f4STrond Myklebust static __poll_t xs_poll_socket(struct sock_xprt *transport)
6720ffe86f4STrond Myklebust {
673a73881c9STrond Myklebust 	return transport->sock->ops->poll(transport->file, transport->sock,
674a73881c9STrond Myklebust 			NULL);
6750ffe86f4STrond Myklebust }
6760ffe86f4STrond Myklebust 
6770ffe86f4STrond Myklebust static bool xs_poll_socket_readable(struct sock_xprt *transport)
6780ffe86f4STrond Myklebust {
6790ffe86f4STrond Myklebust 	__poll_t events = xs_poll_socket(transport);
6800ffe86f4STrond Myklebust 
6810ffe86f4STrond Myklebust 	return (events & (EPOLLIN | EPOLLRDNORM)) && !(events & EPOLLRDHUP);
6820ffe86f4STrond Myklebust }
6830ffe86f4STrond Myklebust 
6840ffe86f4STrond Myklebust static void xs_poll_check_readable(struct sock_xprt *transport)
6850ffe86f4STrond Myklebust {
6860ffe86f4STrond Myklebust 
6870ffe86f4STrond Myklebust 	clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state);
6880ffe86f4STrond Myklebust 	if (!xs_poll_socket_readable(transport))
6890ffe86f4STrond Myklebust 		return;
6900ffe86f4STrond Myklebust 	if (!test_and_set_bit(XPRT_SOCK_DATA_READY, &transport->sock_state))
6910ffe86f4STrond Myklebust 		queue_work(xprtiod_workqueue, &transport->recv_worker);
6920ffe86f4STrond Myklebust }
6930ffe86f4STrond Myklebust 
694c50b8ee0STrond Myklebust static void xs_stream_data_receive(struct sock_xprt *transport)
695c50b8ee0STrond Myklebust {
696c50b8ee0STrond Myklebust 	size_t read = 0;
697c50b8ee0STrond Myklebust 	ssize_t ret = 0;
698c50b8ee0STrond Myklebust 
699c50b8ee0STrond Myklebust 	mutex_lock(&transport->recv_mutex);
700c50b8ee0STrond Myklebust 	if (transport->sock == NULL)
701c50b8ee0STrond Myklebust 		goto out;
702c50b8ee0STrond Myklebust 	for (;;) {
703c50b8ee0STrond Myklebust 		ret = xs_read_stream(transport, MSG_DONTWAIT);
704c4433055STrond Myklebust 		if (ret < 0)
705c50b8ee0STrond Myklebust 			break;
706c50b8ee0STrond Myklebust 		read += ret;
707c50b8ee0STrond Myklebust 		cond_resched();
708c50b8ee0STrond Myklebust 	}
7095f52a9d4STrond Myklebust 	if (ret == -ESHUTDOWN)
7105f52a9d4STrond Myklebust 		kernel_sock_shutdown(transport->sock, SHUT_RDWR);
7115f52a9d4STrond Myklebust 	else
7120ffe86f4STrond Myklebust 		xs_poll_check_readable(transport);
713c50b8ee0STrond Myklebust out:
714c50b8ee0STrond Myklebust 	mutex_unlock(&transport->recv_mutex);
715c50b8ee0STrond Myklebust 	trace_xs_stream_read_data(&transport->xprt, ret, read);
716c50b8ee0STrond Myklebust }
717c50b8ee0STrond Myklebust 
718c50b8ee0STrond Myklebust static void xs_stream_data_receive_workfn(struct work_struct *work)
719c50b8ee0STrond Myklebust {
720c50b8ee0STrond Myklebust 	struct sock_xprt *transport =
721c50b8ee0STrond Myklebust 		container_of(work, struct sock_xprt, recv_worker);
722a1231fdaSTrond Myklebust 	unsigned int pflags = memalloc_nofs_save();
723a1231fdaSTrond Myklebust 
724c50b8ee0STrond Myklebust 	xs_stream_data_receive(transport);
725a1231fdaSTrond Myklebust 	memalloc_nofs_restore(pflags);
726c50b8ee0STrond Myklebust }
727c50b8ee0STrond Myklebust 
728550aebfeSTrond Myklebust static void
729550aebfeSTrond Myklebust xs_stream_reset_connect(struct sock_xprt *transport)
730550aebfeSTrond Myklebust {
731550aebfeSTrond Myklebust 	transport->recv.offset = 0;
732550aebfeSTrond Myklebust 	transport->recv.len = 0;
733550aebfeSTrond Myklebust 	transport->recv.copied = 0;
734550aebfeSTrond Myklebust 	transport->xmit.offset = 0;
735ae053551STrond Myklebust }
736ae053551STrond Myklebust 
737ae053551STrond Myklebust static void
738ae053551STrond Myklebust xs_stream_start_connect(struct sock_xprt *transport)
739ae053551STrond Myklebust {
740550aebfeSTrond Myklebust 	transport->xprt.stat.connect_count++;
741550aebfeSTrond Myklebust 	transport->xprt.stat.connect_start = jiffies;
742550aebfeSTrond Myklebust }
743550aebfeSTrond Myklebust 
744b4b5cc85SChuck Lever #define XS_SENDMSG_FLAGS	(MSG_DONTWAIT | MSG_NOSIGNAL)
745b4b5cc85SChuck Lever 
746e791f8e9STrond Myklebust static int xs_sendmsg(struct socket *sock, struct msghdr *msg, size_t seek)
747b4b5cc85SChuck Lever {
748e791f8e9STrond Myklebust 	if (seek)
749e791f8e9STrond Myklebust 		iov_iter_advance(&msg->msg_iter, seek);
750e791f8e9STrond Myklebust 	return sock_sendmsg(sock, msg);
751e791f8e9STrond Myklebust }
752b4b5cc85SChuck Lever 
753e791f8e9STrond Myklebust static int xs_send_kvec(struct socket *sock, struct msghdr *msg, struct kvec *vec, size_t seek)
754e791f8e9STrond Myklebust {
755e791f8e9STrond Myklebust 	iov_iter_kvec(&msg->msg_iter, WRITE, vec, 1, vec->iov_len);
756e791f8e9STrond Myklebust 	return xs_sendmsg(sock, msg, seek);
757b4b5cc85SChuck Lever }
758b4b5cc85SChuck Lever 
7590472e476STrond Myklebust static int xs_send_pagedata(struct socket *sock, struct msghdr *msg, struct xdr_buf *xdr, size_t base)
760b4b5cc85SChuck Lever {
761f279cd00SJason Baron 	int err;
762b4b5cc85SChuck Lever 
7630472e476STrond Myklebust 	err = xdr_alloc_bvec(xdr, GFP_KERNEL);
7640472e476STrond Myklebust 	if (err < 0)
76524c5684bSTrond Myklebust 		return err;
7660472e476STrond Myklebust 
7670472e476STrond Myklebust 	iov_iter_bvec(&msg->msg_iter, WRITE, xdr->bvec,
7680472e476STrond Myklebust 			xdr_buf_pagecount(xdr),
7690472e476STrond Myklebust 			xdr->page_len + xdr->page_base);
7700472e476STrond Myklebust 	return xs_sendmsg(sock, msg, base + xdr->page_base);
771b4b5cc85SChuck Lever }
772b4b5cc85SChuck Lever 
77306b5fc3aSTrond Myklebust #define xs_record_marker_len() sizeof(rpc_fraghdr)
77406b5fc3aSTrond Myklebust 
775067fb11bSChuck Lever /* Common case:
776067fb11bSChuck Lever  *  - stream transport
777067fb11bSChuck Lever  *  - sending from byte 0 of the message
778067fb11bSChuck Lever  *  - the message is wholly contained in @xdr's head iovec
779067fb11bSChuck Lever  */
78006b5fc3aSTrond Myklebust static int xs_send_rm_and_kvec(struct socket *sock, struct msghdr *msg,
78106b5fc3aSTrond Myklebust 		rpc_fraghdr marker, struct kvec *vec, size_t base)
782067fb11bSChuck Lever {
783067fb11bSChuck Lever 	struct kvec iov[2] = {
78406b5fc3aSTrond Myklebust 		[0] = {
785067fb11bSChuck Lever 			.iov_base	= &marker,
786067fb11bSChuck Lever 			.iov_len	= sizeof(marker)
787067fb11bSChuck Lever 		},
78806b5fc3aSTrond Myklebust 		[1] = *vec,
789067fb11bSChuck Lever 	};
79006b5fc3aSTrond Myklebust 	size_t len = iov[0].iov_len + iov[1].iov_len;
791067fb11bSChuck Lever 
79206b5fc3aSTrond Myklebust 	iov_iter_kvec(&msg->msg_iter, WRITE, iov, 2, len);
79306b5fc3aSTrond Myklebust 	return xs_sendmsg(sock, msg, base);
794a246b010SChuck Lever }
795a246b010SChuck Lever 
7969903cd1cSChuck Lever /**
7979903cd1cSChuck Lever  * xs_sendpages - write pages directly to a socket
7989903cd1cSChuck Lever  * @sock: socket to send on
7999903cd1cSChuck Lever  * @addr: UDP only -- address of destination
8009903cd1cSChuck Lever  * @addrlen: UDP only -- length of destination address
8019903cd1cSChuck Lever  * @xdr: buffer containing this request
8029903cd1cSChuck Lever  * @base: starting position in the buffer
80306b5fc3aSTrond Myklebust  * @rm: stream record marker field
804f279cd00SJason Baron  * @sent_p: return the total number of bytes successfully queued for sending
8059903cd1cSChuck Lever  *
806a246b010SChuck Lever  */
80706b5fc3aSTrond Myklebust static int xs_sendpages(struct socket *sock, struct sockaddr *addr, int addrlen, struct xdr_buf *xdr, unsigned int base, rpc_fraghdr rm, int *sent_p)
808a246b010SChuck Lever {
809e791f8e9STrond Myklebust 	struct msghdr msg = {
810e791f8e9STrond Myklebust 		.msg_name = addr,
811e791f8e9STrond Myklebust 		.msg_namelen = addrlen,
812e791f8e9STrond Myklebust 		.msg_flags = XS_SENDMSG_FLAGS | MSG_MORE,
813e791f8e9STrond Myklebust 	};
81406b5fc3aSTrond Myklebust 	unsigned int rmsize = rm ? sizeof(rm) : 0;
81506b5fc3aSTrond Myklebust 	unsigned int remainder = rmsize + xdr->len - base;
81606b5fc3aSTrond Myklebust 	unsigned int want;
817f279cd00SJason Baron 	int err = 0;
818a246b010SChuck Lever 
819262965f5SChuck Lever 	if (unlikely(!sock))
820fba91afbSTrond Myklebust 		return -ENOTSOCK;
821262965f5SChuck Lever 
82206b5fc3aSTrond Myklebust 	want = xdr->head[0].iov_len + rmsize;
82306b5fc3aSTrond Myklebust 	if (base < want) {
82406b5fc3aSTrond Myklebust 		unsigned int len = want - base;
82524c5684bSTrond Myklebust 		remainder -= len;
826e791f8e9STrond Myklebust 		if (remainder == 0)
827e791f8e9STrond Myklebust 			msg.msg_flags &= ~MSG_MORE;
82806b5fc3aSTrond Myklebust 		if (rmsize)
82906b5fc3aSTrond Myklebust 			err = xs_send_rm_and_kvec(sock, &msg, rm,
83006b5fc3aSTrond Myklebust 					&xdr->head[0], base);
831067fb11bSChuck Lever 		else
832e791f8e9STrond Myklebust 			err = xs_send_kvec(sock, &msg, &xdr->head[0], base);
83324c5684bSTrond Myklebust 		if (remainder == 0 || err != len)
834a246b010SChuck Lever 			goto out;
835f279cd00SJason Baron 		*sent_p += err;
836a246b010SChuck Lever 		base = 0;
837a246b010SChuck Lever 	} else
83806b5fc3aSTrond Myklebust 		base -= want;
839a246b010SChuck Lever 
84024c5684bSTrond Myklebust 	if (base < xdr->page_len) {
84124c5684bSTrond Myklebust 		unsigned int len = xdr->page_len - base;
84224c5684bSTrond Myklebust 		remainder -= len;
8430472e476STrond Myklebust 		if (remainder == 0)
8440472e476STrond Myklebust 			msg.msg_flags &= ~MSG_MORE;
8450472e476STrond Myklebust 		err = xs_send_pagedata(sock, &msg, xdr, base);
8460472e476STrond Myklebust 		if (remainder == 0 || err != len)
847a246b010SChuck Lever 			goto out;
8480472e476STrond Myklebust 		*sent_p += err;
849a246b010SChuck Lever 		base = 0;
85024c5684bSTrond Myklebust 	} else
85124c5684bSTrond Myklebust 		base -= xdr->page_len;
85224c5684bSTrond Myklebust 
85324c5684bSTrond Myklebust 	if (base >= xdr->tail[0].iov_len)
854f279cd00SJason Baron 		return 0;
855e791f8e9STrond Myklebust 	msg.msg_flags &= ~MSG_MORE;
856e791f8e9STrond Myklebust 	err = xs_send_kvec(sock, &msg, &xdr->tail[0], base);
857a246b010SChuck Lever out:
858f279cd00SJason Baron 	if (err > 0) {
859f279cd00SJason Baron 		*sent_p += err;
860f279cd00SJason Baron 		err = 0;
861f279cd00SJason Baron 	}
86224c5684bSTrond Myklebust 	return err;
863a246b010SChuck Lever }
864a246b010SChuck Lever 
8659903cd1cSChuck Lever /**
866c544577dSTrond Myklebust  * xs_nospace - handle transmit was incomplete
86750f484e2STrond Myklebust  * @req: pointer to RPC request
8689903cd1cSChuck Lever  *
869a246b010SChuck Lever  */
870c544577dSTrond Myklebust static int xs_nospace(struct rpc_rqst *req)
871a246b010SChuck Lever {
872262965f5SChuck Lever 	struct rpc_xprt *xprt = req->rq_xprt;
873ee0ac0c2SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
87406ea0bfeSTrond Myklebust 	struct sock *sk = transport->inet;
87524ca9a84STrond Myklebust 	int ret = -EAGAIN;
876a246b010SChuck Lever 
87746121cf7SChuck Lever 	dprintk("RPC: %5u xmit incomplete (%u left of %u)\n",
878c544577dSTrond Myklebust 			req->rq_task->tk_pid,
879c544577dSTrond Myklebust 			req->rq_slen - transport->xmit.offset,
880262965f5SChuck Lever 			req->rq_slen);
881a246b010SChuck Lever 
882262965f5SChuck Lever 	/* Protect against races with write_space */
883b5e92419STrond Myklebust 	spin_lock(&xprt->transport_lock);
884a246b010SChuck Lever 
885262965f5SChuck Lever 	/* Don't race with disconnect */
886b6ddf64fSTrond Myklebust 	if (xprt_connected(xprt)) {
88713331a55STrond Myklebust 		/* wait for more buffer space */
88806ea0bfeSTrond Myklebust 		sk->sk_write_pending++;
889c544577dSTrond Myklebust 		xprt_wait_for_buffer_space(xprt);
89013331a55STrond Myklebust 	} else
8915e3771ceSTrond Myklebust 		ret = -ENOTCONN;
892a246b010SChuck Lever 
893b5e92419STrond Myklebust 	spin_unlock(&xprt->transport_lock);
89406ea0bfeSTrond Myklebust 
89506ea0bfeSTrond Myklebust 	/* Race breaker in case memory is freed before above code is called */
896d48f9ce7SDavid Vrabel 	if (ret == -EAGAIN) {
897d48f9ce7SDavid Vrabel 		struct socket_wq *wq;
898d48f9ce7SDavid Vrabel 
899d48f9ce7SDavid Vrabel 		rcu_read_lock();
900d48f9ce7SDavid Vrabel 		wq = rcu_dereference(sk->sk_wq);
901d48f9ce7SDavid Vrabel 		set_bit(SOCKWQ_ASYNC_NOSPACE, &wq->flags);
902d48f9ce7SDavid Vrabel 		rcu_read_unlock();
903d48f9ce7SDavid Vrabel 
90406ea0bfeSTrond Myklebust 		sk->sk_write_space(sk);
905d48f9ce7SDavid Vrabel 	}
9065e3771ceSTrond Myklebust 	return ret;
907a246b010SChuck Lever }
908a246b010SChuck Lever 
909277e4ab7STrond Myklebust static void
910277e4ab7STrond Myklebust xs_stream_prepare_request(struct rpc_rqst *req)
911277e4ab7STrond Myklebust {
91212a3ad61STrond Myklebust 	req->rq_task->tk_status = xdr_alloc_bvec(&req->rq_rcv_buf, GFP_KERNEL);
913277e4ab7STrond Myklebust }
914277e4ab7STrond Myklebust 
91561677eeeSChuck Lever /*
9164cd34e7cSTrond Myklebust  * Determine if the previous message in the stream was aborted before it
9174cd34e7cSTrond Myklebust  * could complete transmission.
9184cd34e7cSTrond Myklebust  */
9194cd34e7cSTrond Myklebust static bool
9204cd34e7cSTrond Myklebust xs_send_request_was_aborted(struct sock_xprt *transport, struct rpc_rqst *req)
9214cd34e7cSTrond Myklebust {
9224cd34e7cSTrond Myklebust 	return transport->xmit.offset != 0 && req->rq_bytes_sent == 0;
9234cd34e7cSTrond Myklebust }
9244cd34e7cSTrond Myklebust 
9254cd34e7cSTrond Myklebust /*
92606b5fc3aSTrond Myklebust  * Return the stream record marker field for a record of length < 2^31-1
92761677eeeSChuck Lever  */
92806b5fc3aSTrond Myklebust static rpc_fraghdr
92906b5fc3aSTrond Myklebust xs_stream_record_marker(struct xdr_buf *xdr)
93061677eeeSChuck Lever {
93106b5fc3aSTrond Myklebust 	if (!xdr->len)
93206b5fc3aSTrond Myklebust 		return 0;
93306b5fc3aSTrond Myklebust 	return cpu_to_be32(RPC_LAST_STREAM_FRAGMENT | (u32)xdr->len);
93461677eeeSChuck Lever }
93561677eeeSChuck Lever 
9369903cd1cSChuck Lever /**
937176e21eeSChuck Lever  * xs_local_send_request - write an RPC request to an AF_LOCAL socket
93850f484e2STrond Myklebust  * @req: pointer to RPC request
939176e21eeSChuck Lever  *
940176e21eeSChuck Lever  * Return values:
941176e21eeSChuck Lever  *        0:	The request has been sent
942176e21eeSChuck Lever  *   EAGAIN:	The socket was blocked, please call again later to
943176e21eeSChuck Lever  *		complete the request
944176e21eeSChuck Lever  * ENOTCONN:	Caller needs to invoke connect logic then call again
945176e21eeSChuck Lever  *    other:	Some other error occured, the request was not sent
946176e21eeSChuck Lever  */
947adfa7144STrond Myklebust static int xs_local_send_request(struct rpc_rqst *req)
948176e21eeSChuck Lever {
949176e21eeSChuck Lever 	struct rpc_xprt *xprt = req->rq_xprt;
950176e21eeSChuck Lever 	struct sock_xprt *transport =
951176e21eeSChuck Lever 				container_of(xprt, struct sock_xprt, xprt);
952176e21eeSChuck Lever 	struct xdr_buf *xdr = &req->rq_snd_buf;
9537e3d3620STrond Myklebust 	rpc_fraghdr rm = xs_stream_record_marker(xdr);
9547e3d3620STrond Myklebust 	unsigned int msglen = rm ? req->rq_slen + sizeof(rm) : req->rq_slen;
955176e21eeSChuck Lever 	int status;
956f279cd00SJason Baron 	int sent = 0;
957176e21eeSChuck Lever 
9584cd34e7cSTrond Myklebust 	/* Close the stream if the previous transmission was incomplete */
9594cd34e7cSTrond Myklebust 	if (xs_send_request_was_aborted(transport, req)) {
9604cd34e7cSTrond Myklebust 		xs_close(xprt);
9614cd34e7cSTrond Myklebust 		return -ENOTCONN;
9624cd34e7cSTrond Myklebust 	}
9634cd34e7cSTrond Myklebust 
964176e21eeSChuck Lever 	xs_pktdump("packet data:",
965176e21eeSChuck Lever 			req->rq_svec->iov_base, req->rq_svec->iov_len);
966176e21eeSChuck Lever 
96778215759SChuck Lever 	req->rq_xtime = ktime_get();
9686c7a64e5STrond Myklebust 	status = xs_sendpages(transport->sock, NULL, 0, xdr,
9697e3d3620STrond Myklebust 			      transport->xmit.offset, rm, &sent);
970176e21eeSChuck Lever 	dprintk("RPC:       %s(%u) = %d\n",
9716c7a64e5STrond Myklebust 			__func__, xdr->len - transport->xmit.offset, status);
972743c69e7SNeilBrown 
973743c69e7SNeilBrown 	if (status == -EAGAIN && sock_writeable(transport->inet))
974743c69e7SNeilBrown 		status = -ENOBUFS;
975743c69e7SNeilBrown 
976f279cd00SJason Baron 	if (likely(sent > 0) || status == 0) {
9776c7a64e5STrond Myklebust 		transport->xmit.offset += sent;
9786c7a64e5STrond Myklebust 		req->rq_bytes_sent = transport->xmit.offset;
9797e3d3620STrond Myklebust 		if (likely(req->rq_bytes_sent >= msglen)) {
9806c7a64e5STrond Myklebust 			req->rq_xmit_bytes_sent += transport->xmit.offset;
9816c7a64e5STrond Myklebust 			transport->xmit.offset = 0;
982176e21eeSChuck Lever 			return 0;
983176e21eeSChuck Lever 		}
984176e21eeSChuck Lever 		status = -EAGAIN;
985176e21eeSChuck Lever 	}
986176e21eeSChuck Lever 
987176e21eeSChuck Lever 	switch (status) {
9883601c4a9STrond Myklebust 	case -ENOBUFS:
989b5872f0cSTrond Myklebust 		break;
990176e21eeSChuck Lever 	case -EAGAIN:
991c544577dSTrond Myklebust 		status = xs_nospace(req);
992176e21eeSChuck Lever 		break;
993176e21eeSChuck Lever 	default:
994176e21eeSChuck Lever 		dprintk("RPC:       sendmsg returned unrecognized error %d\n",
995176e21eeSChuck Lever 			-status);
996e9d47639SGustavo A. R. Silva 		/* fall through */
997176e21eeSChuck Lever 	case -EPIPE:
998176e21eeSChuck Lever 		xs_close(xprt);
999176e21eeSChuck Lever 		status = -ENOTCONN;
1000176e21eeSChuck Lever 	}
1001176e21eeSChuck Lever 
1002176e21eeSChuck Lever 	return status;
1003176e21eeSChuck Lever }
1004176e21eeSChuck Lever 
1005176e21eeSChuck Lever /**
1006262965f5SChuck Lever  * xs_udp_send_request - write an RPC request to a UDP socket
100750f484e2STrond Myklebust  * @req: pointer to RPC request
10089903cd1cSChuck Lever  *
10099903cd1cSChuck Lever  * Return values:
10109903cd1cSChuck Lever  *        0:	The request has been sent
10119903cd1cSChuck Lever  *   EAGAIN:	The socket was blocked, please call again later to
10129903cd1cSChuck Lever  *		complete the request
1013262965f5SChuck Lever  * ENOTCONN:	Caller needs to invoke connect logic then call again
101425985edcSLucas De Marchi  *    other:	Some other error occurred, the request was not sent
10159903cd1cSChuck Lever  */
1016adfa7144STrond Myklebust static int xs_udp_send_request(struct rpc_rqst *req)
1017a246b010SChuck Lever {
1018a246b010SChuck Lever 	struct rpc_xprt *xprt = req->rq_xprt;
1019ee0ac0c2SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1020262965f5SChuck Lever 	struct xdr_buf *xdr = &req->rq_snd_buf;
1021f279cd00SJason Baron 	int sent = 0;
1022262965f5SChuck Lever 	int status;
1023262965f5SChuck Lever 
1024262965f5SChuck Lever 	xs_pktdump("packet data:",
1025262965f5SChuck Lever 				req->rq_svec->iov_base,
1026262965f5SChuck Lever 				req->rq_svec->iov_len);
1027262965f5SChuck Lever 
102801d37c42STrond Myklebust 	if (!xprt_bound(xprt))
102901d37c42STrond Myklebust 		return -ENOTCONN;
103075891f50STrond Myklebust 
103175891f50STrond Myklebust 	if (!xprt_request_get_cong(xprt, req))
103275891f50STrond Myklebust 		return -EBADSLT;
103375891f50STrond Myklebust 
103478215759SChuck Lever 	req->rq_xtime = ktime_get();
1035f279cd00SJason Baron 	status = xs_sendpages(transport->sock, xs_addr(xprt), xprt->addrlen,
103606b5fc3aSTrond Myklebust 			      xdr, 0, 0, &sent);
1037262965f5SChuck Lever 
1038262965f5SChuck Lever 	dprintk("RPC:       xs_udp_send_request(%u) = %d\n",
10396c7a64e5STrond Myklebust 			xdr->len, status);
1040262965f5SChuck Lever 
10413dedbb5cSJason Baron 	/* firewall is blocking us, don't return -EAGAIN or we end up looping */
10423dedbb5cSJason Baron 	if (status == -EPERM)
10433dedbb5cSJason Baron 		goto process_status;
10443dedbb5cSJason Baron 
1045743c69e7SNeilBrown 	if (status == -EAGAIN && sock_writeable(transport->inet))
1046743c69e7SNeilBrown 		status = -ENOBUFS;
1047743c69e7SNeilBrown 
1048f279cd00SJason Baron 	if (sent > 0 || status == 0) {
1049f279cd00SJason Baron 		req->rq_xmit_bytes_sent += sent;
1050f279cd00SJason Baron 		if (sent >= req->rq_slen)
1051262965f5SChuck Lever 			return 0;
1052262965f5SChuck Lever 		/* Still some bytes left; set up for a retry later. */
1053262965f5SChuck Lever 		status = -EAGAIN;
10542199700fSTrond Myklebust 	}
1055262965f5SChuck Lever 
10563dedbb5cSJason Baron process_status:
1057262965f5SChuck Lever 	switch (status) {
1058fba91afbSTrond Myklebust 	case -ENOTSOCK:
1059fba91afbSTrond Myklebust 		status = -ENOTCONN;
1060fba91afbSTrond Myklebust 		/* Should we call xs_close() here? */
1061fba91afbSTrond Myklebust 		break;
1062b6ddf64fSTrond Myklebust 	case -EAGAIN:
1063c544577dSTrond Myklebust 		status = xs_nospace(req);
1064b6ddf64fSTrond Myklebust 		break;
1065262965f5SChuck Lever 	case -ENETUNREACH:
10663601c4a9STrond Myklebust 	case -ENOBUFS:
1067262965f5SChuck Lever 	case -EPIPE:
1068262965f5SChuck Lever 	case -ECONNREFUSED:
10693dedbb5cSJason Baron 	case -EPERM:
1070262965f5SChuck Lever 		/* When the server has died, an ICMP port unreachable message
1071262965f5SChuck Lever 		 * prompts ECONNREFUSED. */
107213331a55STrond Myklebust 		break;
107313331a55STrond Myklebust 	default:
107413331a55STrond Myklebust 		dprintk("RPC:       sendmsg returned unrecognized error %d\n",
107513331a55STrond Myklebust 			-status);
1076262965f5SChuck Lever 	}
10775fe46e9dSBian Naimeng 
1078262965f5SChuck Lever 	return status;
1079262965f5SChuck Lever }
1080262965f5SChuck Lever 
1081e06799f9STrond Myklebust /**
1082262965f5SChuck Lever  * xs_tcp_send_request - write an RPC request to a TCP socket
108350f484e2STrond Myklebust  * @req: pointer to RPC request
1084262965f5SChuck Lever  *
1085262965f5SChuck Lever  * Return values:
1086262965f5SChuck Lever  *        0:	The request has been sent
1087262965f5SChuck Lever  *   EAGAIN:	The socket was blocked, please call again later to
1088262965f5SChuck Lever  *		complete the request
1089262965f5SChuck Lever  * ENOTCONN:	Caller needs to invoke connect logic then call again
109025985edcSLucas De Marchi  *    other:	Some other error occurred, the request was not sent
1091262965f5SChuck Lever  *
1092262965f5SChuck Lever  * XXX: In the case of soft timeouts, should we eventually give up
1093262965f5SChuck Lever  *	if sendmsg is not able to make progress?
1094262965f5SChuck Lever  */
1095adfa7144STrond Myklebust static int xs_tcp_send_request(struct rpc_rqst *req)
1096262965f5SChuck Lever {
1097262965f5SChuck Lever 	struct rpc_xprt *xprt = req->rq_xprt;
1098ee0ac0c2SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1099262965f5SChuck Lever 	struct xdr_buf *xdr = &req->rq_snd_buf;
11007e3d3620STrond Myklebust 	rpc_fraghdr rm = xs_stream_record_marker(xdr);
11017e3d3620STrond Myklebust 	unsigned int msglen = rm ? req->rq_slen + sizeof(rm) : req->rq_slen;
11029ffadfbcSTrond Myklebust 	bool vm_wait = false;
1103b595bb15SChuck Lever 	int status;
1104f279cd00SJason Baron 	int sent;
1105a246b010SChuck Lever 
11064cd34e7cSTrond Myklebust 	/* Close the stream if the previous transmission was incomplete */
11074cd34e7cSTrond Myklebust 	if (xs_send_request_was_aborted(transport, req)) {
11084cd34e7cSTrond Myklebust 		if (transport->sock != NULL)
11094cd34e7cSTrond Myklebust 			kernel_sock_shutdown(transport->sock, SHUT_RDWR);
11104cd34e7cSTrond Myklebust 		return -ENOTCONN;
11114cd34e7cSTrond Myklebust 	}
11124cd34e7cSTrond Myklebust 
1113262965f5SChuck Lever 	xs_pktdump("packet data:",
1114262965f5SChuck Lever 				req->rq_svec->iov_base,
1115262965f5SChuck Lever 				req->rq_svec->iov_len);
1116a246b010SChuck Lever 
11177196dbb0STrond Myklebust 	if (test_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state))
11187196dbb0STrond Myklebust 		xs_tcp_set_socket_timeouts(xprt, transport->sock);
11197196dbb0STrond Myklebust 
1120a246b010SChuck Lever 	/* Continue transmitting the packet/record. We must be careful
1121a246b010SChuck Lever 	 * to cope with writespace callbacks arriving _after_ we have
1122262965f5SChuck Lever 	 * called sendmsg(). */
112378215759SChuck Lever 	req->rq_xtime = ktime_get();
1124a246b010SChuck Lever 	while (1) {
1125f279cd00SJason Baron 		sent = 0;
1126f279cd00SJason Baron 		status = xs_sendpages(transport->sock, NULL, 0, xdr,
11277e3d3620STrond Myklebust 				      transport->xmit.offset, rm, &sent);
1128a246b010SChuck Lever 
1129262965f5SChuck Lever 		dprintk("RPC:       xs_tcp_send_request(%u) = %d\n",
11306c7a64e5STrond Myklebust 				xdr->len - transport->xmit.offset, status);
1131262965f5SChuck Lever 
1132a246b010SChuck Lever 		/* If we've sent the entire packet, immediately
1133a246b010SChuck Lever 		 * reset the count of bytes sent. */
11346c7a64e5STrond Myklebust 		transport->xmit.offset += sent;
11356c7a64e5STrond Myklebust 		req->rq_bytes_sent = transport->xmit.offset;
11367e3d3620STrond Myklebust 		if (likely(req->rq_bytes_sent >= msglen)) {
11376c7a64e5STrond Myklebust 			req->rq_xmit_bytes_sent += transport->xmit.offset;
11386c7a64e5STrond Myklebust 			transport->xmit.offset = 0;
1139a246b010SChuck Lever 			return 0;
1140a246b010SChuck Lever 		}
1141262965f5SChuck Lever 
11429ffadfbcSTrond Myklebust 		WARN_ON_ONCE(sent == 0 && status == 0);
11439ffadfbcSTrond Myklebust 
11449ffadfbcSTrond Myklebust 		if (status == -EAGAIN ) {
11459ffadfbcSTrond Myklebust 			/*
11469ffadfbcSTrond Myklebust 			 * Return EAGAIN if we're sure we're hitting the
11479ffadfbcSTrond Myklebust 			 * socket send buffer limits.
11489ffadfbcSTrond Myklebust 			 */
11499ffadfbcSTrond Myklebust 			if (test_bit(SOCK_NOSPACE, &transport->sock->flags))
11509ffadfbcSTrond Myklebust 				break;
11519ffadfbcSTrond Myklebust 			/*
11529ffadfbcSTrond Myklebust 			 * Did we hit a memory allocation failure?
11539ffadfbcSTrond Myklebust 			 */
11549ffadfbcSTrond Myklebust 			if (sent == 0) {
11559ffadfbcSTrond Myklebust 				status = -ENOBUFS;
11569ffadfbcSTrond Myklebust 				if (vm_wait)
11579ffadfbcSTrond Myklebust 					break;
11589ffadfbcSTrond Myklebust 				/* Retry, knowing now that we're below the
11599ffadfbcSTrond Myklebust 				 * socket send buffer limit
11609ffadfbcSTrond Myklebust 				 */
11619ffadfbcSTrond Myklebust 				vm_wait = true;
11629ffadfbcSTrond Myklebust 			}
11639ffadfbcSTrond Myklebust 			continue;
11649ffadfbcSTrond Myklebust 		}
1165f580dd04STrond Myklebust 		if (status < 0)
1166f580dd04STrond Myklebust 			break;
11679ffadfbcSTrond Myklebust 		vm_wait = false;
1168a246b010SChuck Lever 	}
1169a246b010SChuck Lever 
1170262965f5SChuck Lever 	switch (status) {
1171fba91afbSTrond Myklebust 	case -ENOTSOCK:
1172fba91afbSTrond Myklebust 		status = -ENOTCONN;
1173fba91afbSTrond Myklebust 		/* Should we call xs_close() here? */
1174fba91afbSTrond Myklebust 		break;
1175262965f5SChuck Lever 	case -EAGAIN:
1176c544577dSTrond Myklebust 		status = xs_nospace(req);
1177262965f5SChuck Lever 		break;
1178262965f5SChuck Lever 	case -ECONNRESET:
1179262965f5SChuck Lever 	case -ECONNREFUSED:
1180262965f5SChuck Lever 	case -ENOTCONN:
11813913c78cSTrond Myklebust 	case -EADDRINUSE:
1182b5872f0cSTrond Myklebust 	case -ENOBUFS:
1183b9d2bb2eSTrond Myklebust 	case -EPIPE:
118413331a55STrond Myklebust 		break;
118513331a55STrond Myklebust 	default:
118613331a55STrond Myklebust 		dprintk("RPC:       sendmsg returned unrecognized error %d\n",
118713331a55STrond Myklebust 			-status);
1188a246b010SChuck Lever 	}
11895fe46e9dSBian Naimeng 
1190a246b010SChuck Lever 	return status;
1191a246b010SChuck Lever }
1192a246b010SChuck Lever 
11932a9e1cfaSTrond Myklebust static void xs_save_old_callbacks(struct sock_xprt *transport, struct sock *sk)
11942a9e1cfaSTrond Myklebust {
11952a9e1cfaSTrond Myklebust 	transport->old_data_ready = sk->sk_data_ready;
11962a9e1cfaSTrond Myklebust 	transport->old_state_change = sk->sk_state_change;
11972a9e1cfaSTrond Myklebust 	transport->old_write_space = sk->sk_write_space;
11982118071dSTrond Myklebust 	transport->old_error_report = sk->sk_error_report;
11992a9e1cfaSTrond Myklebust }
12002a9e1cfaSTrond Myklebust 
12012a9e1cfaSTrond Myklebust static void xs_restore_old_callbacks(struct sock_xprt *transport, struct sock *sk)
12022a9e1cfaSTrond Myklebust {
12032a9e1cfaSTrond Myklebust 	sk->sk_data_ready = transport->old_data_ready;
12042a9e1cfaSTrond Myklebust 	sk->sk_state_change = transport->old_state_change;
12052a9e1cfaSTrond Myklebust 	sk->sk_write_space = transport->old_write_space;
12062118071dSTrond Myklebust 	sk->sk_error_report = transport->old_error_report;
12072118071dSTrond Myklebust }
12082118071dSTrond Myklebust 
120942d42a5bSTrond Myklebust static void xs_sock_reset_state_flags(struct rpc_xprt *xprt)
121042d42a5bSTrond Myklebust {
121142d42a5bSTrond Myklebust 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
121242d42a5bSTrond Myklebust 
121342d42a5bSTrond Myklebust 	clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state);
12144f8943f8STrond Myklebust 	clear_bit(XPRT_SOCK_WAKE_ERROR, &transport->sock_state);
12154f8943f8STrond Myklebust 	clear_bit(XPRT_SOCK_WAKE_WRITE, &transport->sock_state);
12164f8943f8STrond Myklebust 	clear_bit(XPRT_SOCK_WAKE_DISCONNECT, &transport->sock_state);
12174f8943f8STrond Myklebust }
12184f8943f8STrond Myklebust 
12194f8943f8STrond Myklebust static void xs_run_error_worker(struct sock_xprt *transport, unsigned int nr)
12204f8943f8STrond Myklebust {
12214f8943f8STrond Myklebust 	set_bit(nr, &transport->sock_state);
12224f8943f8STrond Myklebust 	queue_work(xprtiod_workqueue, &transport->error_worker);
122342d42a5bSTrond Myklebust }
122442d42a5bSTrond Myklebust 
1225b70ae915STrond Myklebust static void xs_sock_reset_connection_flags(struct rpc_xprt *xprt)
1226b70ae915STrond Myklebust {
1227b70ae915STrond Myklebust 	smp_mb__before_atomic();
1228b70ae915STrond Myklebust 	clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
1229b70ae915STrond Myklebust 	clear_bit(XPRT_CLOSING, &xprt->state);
123042d42a5bSTrond Myklebust 	xs_sock_reset_state_flags(xprt);
1231b70ae915STrond Myklebust 	smp_mb__after_atomic();
1232b70ae915STrond Myklebust }
1233b70ae915STrond Myklebust 
12342118071dSTrond Myklebust /**
12352118071dSTrond Myklebust  * xs_error_report - callback to handle TCP socket state errors
12362118071dSTrond Myklebust  * @sk: socket
12372118071dSTrond Myklebust  *
12382118071dSTrond Myklebust  * Note: we don't call sock_error() since there may be a rpc_task
12392118071dSTrond Myklebust  * using the socket, and so we don't want to clear sk->sk_err.
12402118071dSTrond Myklebust  */
12412118071dSTrond Myklebust static void xs_error_report(struct sock *sk)
12422118071dSTrond Myklebust {
12434f8943f8STrond Myklebust 	struct sock_xprt *transport;
12442118071dSTrond Myklebust 	struct rpc_xprt *xprt;
12452118071dSTrond Myklebust 	int err;
12462118071dSTrond Myklebust 
12472118071dSTrond Myklebust 	read_lock_bh(&sk->sk_callback_lock);
12482118071dSTrond Myklebust 	if (!(xprt = xprt_from_sock(sk)))
12492118071dSTrond Myklebust 		goto out;
12502118071dSTrond Myklebust 
12514f8943f8STrond Myklebust 	transport = container_of(xprt, struct sock_xprt, xprt);
12522118071dSTrond Myklebust 	err = -sk->sk_err;
12532118071dSTrond Myklebust 	if (err == 0)
12542118071dSTrond Myklebust 		goto out;
12552118071dSTrond Myklebust 	dprintk("RPC:       xs_error_report client %p, error=%d...\n",
12562118071dSTrond Myklebust 			xprt, -err);
1257e8353c76STrond Myklebust 	trace_rpc_socket_error(xprt, sk->sk_socket, err);
12584f8943f8STrond Myklebust 	xs_run_error_worker(transport, XPRT_SOCK_WAKE_ERROR);
12592118071dSTrond Myklebust  out:
12602118071dSTrond Myklebust 	read_unlock_bh(&sk->sk_callback_lock);
12612a9e1cfaSTrond Myklebust }
12622a9e1cfaSTrond Myklebust 
1263fe315e76SChuck Lever static void xs_reset_transport(struct sock_xprt *transport)
1264a246b010SChuck Lever {
1265ee0ac0c2SChuck Lever 	struct socket *sock = transport->sock;
1266ee0ac0c2SChuck Lever 	struct sock *sk = transport->inet;
12676cc7e908STrond Myklebust 	struct rpc_xprt *xprt = &transport->xprt;
1268a73881c9STrond Myklebust 	struct file *filp = transport->file;
1269a246b010SChuck Lever 
1270fe315e76SChuck Lever 	if (sk == NULL)
1271fe315e76SChuck Lever 		return;
12729903cd1cSChuck Lever 
1273264d1df3SJeff Layton 	if (atomic_read(&transport->xprt.swapper))
1274264d1df3SJeff Layton 		sk_clear_memalloc(sk);
1275264d1df3SJeff Layton 
127609939204STrond Myklebust 	kernel_sock_shutdown(sock, SHUT_RDWR);
127709939204STrond Myklebust 
1278edc1b01cSTrond Myklebust 	mutex_lock(&transport->recv_mutex);
1279a246b010SChuck Lever 	write_lock_bh(&sk->sk_callback_lock);
1280ee0ac0c2SChuck Lever 	transport->inet = NULL;
1281ee0ac0c2SChuck Lever 	transport->sock = NULL;
1282a73881c9STrond Myklebust 	transport->file = NULL;
1283a246b010SChuck Lever 
1284a246b010SChuck Lever 	sk->sk_user_data = NULL;
12852a9e1cfaSTrond Myklebust 
12862a9e1cfaSTrond Myklebust 	xs_restore_old_callbacks(transport, sk);
12870c78789eSTrond Myklebust 	xprt_clear_connected(xprt);
1288a246b010SChuck Lever 	write_unlock_bh(&sk->sk_callback_lock);
12896cc7e908STrond Myklebust 	xs_sock_reset_connection_flags(xprt);
1290ae053551STrond Myklebust 	/* Reset stream record info */
1291ae053551STrond Myklebust 	xs_stream_reset_connect(transport);
1292edc1b01cSTrond Myklebust 	mutex_unlock(&transport->recv_mutex);
1293a246b010SChuck Lever 
12946cc7e908STrond Myklebust 	trace_rpc_socket_close(xprt, sock);
1295a73881c9STrond Myklebust 	fput(filp);
12960445f92cSTrond Myklebust 
12970445f92cSTrond Myklebust 	xprt_disconnect_done(xprt);
1298fe315e76SChuck Lever }
1299fe315e76SChuck Lever 
1300fe315e76SChuck Lever /**
1301fe315e76SChuck Lever  * xs_close - close a socket
1302fe315e76SChuck Lever  * @xprt: transport
1303fe315e76SChuck Lever  *
1304fe315e76SChuck Lever  * This is used when all requests are complete; ie, no DRC state remains
1305fe315e76SChuck Lever  * on the server we want to save.
1306f75e6745STrond Myklebust  *
1307f75e6745STrond Myklebust  * The caller _must_ be holding XPRT_LOCKED in order to avoid issues with
1308f75e6745STrond Myklebust  * xs_reset_transport() zeroing the socket from underneath a writer.
1309fe315e76SChuck Lever  */
1310fe315e76SChuck Lever static void xs_close(struct rpc_xprt *xprt)
1311fe315e76SChuck Lever {
1312fe315e76SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1313fe315e76SChuck Lever 
1314fe315e76SChuck Lever 	dprintk("RPC:       xs_close xprt %p\n", xprt);
1315fe315e76SChuck Lever 
1316fe315e76SChuck Lever 	xs_reset_transport(transport);
131761d0a8e6SNeil Brown 	xprt->reestablish_timeout = 0;
1318a246b010SChuck Lever }
1319a246b010SChuck Lever 
13204a068258SChuck Lever static void xs_inject_disconnect(struct rpc_xprt *xprt)
13214a068258SChuck Lever {
13224a068258SChuck Lever 	dprintk("RPC:       injecting transport disconnect on xprt=%p\n",
13234a068258SChuck Lever 		xprt);
13244a068258SChuck Lever 	xprt_disconnect_done(xprt);
13254a068258SChuck Lever }
13264a068258SChuck Lever 
1327315f3812SKinglong Mee static void xs_xprt_free(struct rpc_xprt *xprt)
1328315f3812SKinglong Mee {
1329315f3812SKinglong Mee 	xs_free_peer_addresses(xprt);
1330315f3812SKinglong Mee 	xprt_free(xprt);
1331315f3812SKinglong Mee }
1332315f3812SKinglong Mee 
13339903cd1cSChuck Lever /**
13349903cd1cSChuck Lever  * xs_destroy - prepare to shutdown a transport
13359903cd1cSChuck Lever  * @xprt: doomed transport
13369903cd1cSChuck Lever  *
13379903cd1cSChuck Lever  */
13389903cd1cSChuck Lever static void xs_destroy(struct rpc_xprt *xprt)
1339a246b010SChuck Lever {
134003c78827STrond Myklebust 	struct sock_xprt *transport = container_of(xprt,
134103c78827STrond Myklebust 			struct sock_xprt, xprt);
13429903cd1cSChuck Lever 	dprintk("RPC:       xs_destroy xprt %p\n", xprt);
13439903cd1cSChuck Lever 
134403c78827STrond Myklebust 	cancel_delayed_work_sync(&transport->connect_worker);
1345a1311d87STrond Myklebust 	xs_close(xprt);
1346edc1b01cSTrond Myklebust 	cancel_work_sync(&transport->recv_worker);
1347b5e92419STrond Myklebust 	cancel_work_sync(&transport->error_worker);
1348315f3812SKinglong Mee 	xs_xprt_free(xprt);
1349a1311d87STrond Myklebust 	module_put(THIS_MODULE);
1350a246b010SChuck Lever }
1351a246b010SChuck Lever 
13529903cd1cSChuck Lever /**
1353f9b2ee71STrond Myklebust  * xs_udp_data_read_skb - receive callback for UDP sockets
1354f9b2ee71STrond Myklebust  * @xprt: transport
1355f9b2ee71STrond Myklebust  * @sk: socket
1356f9b2ee71STrond Myklebust  * @skb: skbuff
13579903cd1cSChuck Lever  *
1358a246b010SChuck Lever  */
1359f9b2ee71STrond Myklebust static void xs_udp_data_read_skb(struct rpc_xprt *xprt,
1360f9b2ee71STrond Myklebust 		struct sock *sk,
1361f9b2ee71STrond Myklebust 		struct sk_buff *skb)
1362a246b010SChuck Lever {
1363a246b010SChuck Lever 	struct rpc_task *task;
1364a246b010SChuck Lever 	struct rpc_rqst *rovr;
1365f9b2ee71STrond Myklebust 	int repsize, copied;
1366d8ed029dSAlexey Dobriyan 	u32 _xid;
1367d8ed029dSAlexey Dobriyan 	__be32 *xp;
1368a246b010SChuck Lever 
13691da8c681SWillem de Bruijn 	repsize = skb->len;
1370a246b010SChuck Lever 	if (repsize < 4) {
13719903cd1cSChuck Lever 		dprintk("RPC:       impossible RPC reply size %d!\n", repsize);
1372f9b2ee71STrond Myklebust 		return;
1373a246b010SChuck Lever 	}
1374a246b010SChuck Lever 
1375a246b010SChuck Lever 	/* Copy the XID from the skb... */
13761da8c681SWillem de Bruijn 	xp = skb_header_pointer(skb, 0, sizeof(_xid), &_xid);
1377a246b010SChuck Lever 	if (xp == NULL)
1378f9b2ee71STrond Myklebust 		return;
1379a246b010SChuck Lever 
1380a246b010SChuck Lever 	/* Look up and lock the request corresponding to the given XID */
138175c84151STrond Myklebust 	spin_lock(&xprt->queue_lock);
1382a246b010SChuck Lever 	rovr = xprt_lookup_rqst(xprt, *xp);
1383a246b010SChuck Lever 	if (!rovr)
1384a246b010SChuck Lever 		goto out_unlock;
1385729749bbSTrond Myklebust 	xprt_pin_rqst(rovr);
1386ecd465eeSChuck Lever 	xprt_update_rtt(rovr->rq_task);
138775c84151STrond Myklebust 	spin_unlock(&xprt->queue_lock);
1388a246b010SChuck Lever 	task = rovr->rq_task;
1389a246b010SChuck Lever 
1390a246b010SChuck Lever 	if ((copied = rovr->rq_private_buf.buflen) > repsize)
1391a246b010SChuck Lever 		copied = repsize;
1392a246b010SChuck Lever 
1393a246b010SChuck Lever 	/* Suck it into the iovec, verify checksum if not done by hw. */
13941781f7f5SHerbert Xu 	if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) {
139575c84151STrond Myklebust 		spin_lock(&xprt->queue_lock);
13960afa6b44STrond Myklebust 		__UDPX_INC_STATS(sk, UDP_MIB_INERRORS);
1397729749bbSTrond Myklebust 		goto out_unpin;
13981781f7f5SHerbert Xu 	}
13991781f7f5SHerbert Xu 
1400a246b010SChuck Lever 
1401b5e92419STrond Myklebust 	spin_lock(&xprt->transport_lock);
14026a24dfb6STrond Myklebust 	xprt_adjust_cwnd(xprt, task, copied);
1403b5e92419STrond Myklebust 	spin_unlock(&xprt->transport_lock);
140475c84151STrond Myklebust 	spin_lock(&xprt->queue_lock);
14051570c1e4SChuck Lever 	xprt_complete_rqst(task, copied);
14060afa6b44STrond Myklebust 	__UDPX_INC_STATS(sk, UDP_MIB_INDATAGRAMS);
1407729749bbSTrond Myklebust out_unpin:
1408729749bbSTrond Myklebust 	xprt_unpin_rqst(rovr);
1409a246b010SChuck Lever  out_unlock:
141075c84151STrond Myklebust 	spin_unlock(&xprt->queue_lock);
1411f9b2ee71STrond Myklebust }
1412f9b2ee71STrond Myklebust 
1413f9b2ee71STrond Myklebust static void xs_udp_data_receive(struct sock_xprt *transport)
1414f9b2ee71STrond Myklebust {
1415f9b2ee71STrond Myklebust 	struct sk_buff *skb;
1416f9b2ee71STrond Myklebust 	struct sock *sk;
1417f9b2ee71STrond Myklebust 	int err;
1418f9b2ee71STrond Myklebust 
1419f9b2ee71STrond Myklebust 	mutex_lock(&transport->recv_mutex);
1420f9b2ee71STrond Myklebust 	sk = transport->inet;
1421f9b2ee71STrond Myklebust 	if (sk == NULL)
1422f9b2ee71STrond Myklebust 		goto out;
1423f9b2ee71STrond Myklebust 	for (;;) {
14247c13f97fSPaolo Abeni 		skb = skb_recv_udp(sk, 0, 1, &err);
14254f546149STrond Myklebust 		if (skb == NULL)
14264f546149STrond Myklebust 			break;
1427f9b2ee71STrond Myklebust 		xs_udp_data_read_skb(&transport->xprt, sk, skb);
1428850cbaddSPaolo Abeni 		consume_skb(skb);
14290af3442aSTrond Myklebust 		cond_resched();
1430f9b2ee71STrond Myklebust 	}
14310ffe86f4STrond Myklebust 	xs_poll_check_readable(transport);
1432a246b010SChuck Lever out:
1433f9b2ee71STrond Myklebust 	mutex_unlock(&transport->recv_mutex);
1434f9b2ee71STrond Myklebust }
1435f9b2ee71STrond Myklebust 
1436f9b2ee71STrond Myklebust static void xs_udp_data_receive_workfn(struct work_struct *work)
1437f9b2ee71STrond Myklebust {
1438f9b2ee71STrond Myklebust 	struct sock_xprt *transport =
1439f9b2ee71STrond Myklebust 		container_of(work, struct sock_xprt, recv_worker);
1440a1231fdaSTrond Myklebust 	unsigned int pflags = memalloc_nofs_save();
1441a1231fdaSTrond Myklebust 
1442f9b2ee71STrond Myklebust 	xs_udp_data_receive(transport);
1443a1231fdaSTrond Myklebust 	memalloc_nofs_restore(pflags);
1444f9b2ee71STrond Myklebust }
1445f9b2ee71STrond Myklebust 
1446f9b2ee71STrond Myklebust /**
1447f9b2ee71STrond Myklebust  * xs_data_ready - "data ready" callback for UDP sockets
1448f9b2ee71STrond Myklebust  * @sk: socket with data to read
1449f9b2ee71STrond Myklebust  *
1450f9b2ee71STrond Myklebust  */
1451f9b2ee71STrond Myklebust static void xs_data_ready(struct sock *sk)
1452f9b2ee71STrond Myklebust {
1453f9b2ee71STrond Myklebust 	struct rpc_xprt *xprt;
1454f9b2ee71STrond Myklebust 
1455f9b2ee71STrond Myklebust 	read_lock_bh(&sk->sk_callback_lock);
1456f9b2ee71STrond Myklebust 	dprintk("RPC:       xs_data_ready...\n");
1457f9b2ee71STrond Myklebust 	xprt = xprt_from_sock(sk);
1458f9b2ee71STrond Myklebust 	if (xprt != NULL) {
1459f9b2ee71STrond Myklebust 		struct sock_xprt *transport = container_of(xprt,
1460f9b2ee71STrond Myklebust 				struct sock_xprt, xprt);
14615157b956STrond Myklebust 		transport->old_data_ready(sk);
14625157b956STrond Myklebust 		/* Any data means we had a useful conversation, so
14635157b956STrond Myklebust 		 * then we don't need to delay the next reconnect
14645157b956STrond Myklebust 		 */
14655157b956STrond Myklebust 		if (xprt->reestablish_timeout)
14665157b956STrond Myklebust 			xprt->reestablish_timeout = 0;
146742d42a5bSTrond Myklebust 		if (!test_and_set_bit(XPRT_SOCK_DATA_READY, &transport->sock_state))
146840a5f1b1STrond Myklebust 			queue_work(xprtiod_workqueue, &transport->recv_worker);
1469f9b2ee71STrond Myklebust 	}
1470f064af1eSEric Dumazet 	read_unlock_bh(&sk->sk_callback_lock);
1471a246b010SChuck Lever }
1472a246b010SChuck Lever 
1473a519fc7aSTrond Myklebust /*
1474a519fc7aSTrond Myklebust  * Helper function to force a TCP close if the server is sending
1475a519fc7aSTrond Myklebust  * junk and/or it has put us in CLOSE_WAIT
1476a519fc7aSTrond Myklebust  */
1477a519fc7aSTrond Myklebust static void xs_tcp_force_close(struct rpc_xprt *xprt)
1478a519fc7aSTrond Myklebust {
1479a519fc7aSTrond Myklebust 	xprt_force_disconnect(xprt);
1480a519fc7aSTrond Myklebust }
1481a519fc7aSTrond Myklebust 
14829e00abc3STrond Myklebust #if defined(CONFIG_SUNRPC_BACKCHANNEL)
14836b26cc8cSChuck Lever static size_t xs_tcp_bc_maxpayload(struct rpc_xprt *xprt)
14846b26cc8cSChuck Lever {
14856b26cc8cSChuck Lever 	return PAGE_SIZE;
14866b26cc8cSChuck Lever }
14879e00abc3STrond Myklebust #endif /* CONFIG_SUNRPC_BACKCHANNEL */
148844b98efdSRicardo Labiaga 
14899903cd1cSChuck Lever /**
14909903cd1cSChuck Lever  * xs_tcp_state_change - callback to handle TCP socket state changes
14919903cd1cSChuck Lever  * @sk: socket whose state has changed
14929903cd1cSChuck Lever  *
14939903cd1cSChuck Lever  */
14949903cd1cSChuck Lever static void xs_tcp_state_change(struct sock *sk)
1495a246b010SChuck Lever {
1496a246b010SChuck Lever 	struct rpc_xprt *xprt;
14970fdea1e8STrond Myklebust 	struct sock_xprt *transport;
1498a246b010SChuck Lever 
1499f064af1eSEric Dumazet 	read_lock_bh(&sk->sk_callback_lock);
1500a246b010SChuck Lever 	if (!(xprt = xprt_from_sock(sk)))
1501a246b010SChuck Lever 		goto out;
15029903cd1cSChuck Lever 	dprintk("RPC:       xs_tcp_state_change client %p...\n", xprt);
1503669502ffSAndy Chittenden 	dprintk("RPC:       state %x conn %d dead %d zapped %d sk_shutdown %d\n",
1504a246b010SChuck Lever 			sk->sk_state, xprt_connected(xprt),
1505a246b010SChuck Lever 			sock_flag(sk, SOCK_DEAD),
1506669502ffSAndy Chittenden 			sock_flag(sk, SOCK_ZAPPED),
1507669502ffSAndy Chittenden 			sk->sk_shutdown);
1508a246b010SChuck Lever 
15090fdea1e8STrond Myklebust 	transport = container_of(xprt, struct sock_xprt, xprt);
151040b5ea0cSTrond Myklebust 	trace_rpc_socket_state_change(xprt, sk->sk_socket);
1511a246b010SChuck Lever 	switch (sk->sk_state) {
1512a246b010SChuck Lever 	case TCP_ESTABLISHED:
1513a246b010SChuck Lever 		if (!xprt_test_and_set_connected(xprt)) {
15148b71798cSTrond Myklebust 			xprt->connect_cookie++;
15150fdea1e8STrond Myklebust 			clear_bit(XPRT_SOCK_CONNECTING, &transport->sock_state);
15160fdea1e8STrond Myklebust 			xprt_clear_connecting(xprt);
151751971139SChuck Lever 
15183968a8a5SChuck Lever 			xprt->stat.connect_count++;
15193968a8a5SChuck Lever 			xprt->stat.connect_time += (long)jiffies -
15203968a8a5SChuck Lever 						   xprt->stat.connect_start;
15214f8943f8STrond Myklebust 			xs_run_error_worker(transport, XPRT_SOCK_WAKE_PENDING);
1522a246b010SChuck Lever 		}
1523a246b010SChuck Lever 		break;
15243b948ae5STrond Myklebust 	case TCP_FIN_WAIT1:
15253b948ae5STrond Myklebust 		/* The client initiated a shutdown of the socket */
15267c1d71cfSTrond Myklebust 		xprt->connect_cookie++;
1527663b8858STrond Myklebust 		xprt->reestablish_timeout = 0;
15283b948ae5STrond Myklebust 		set_bit(XPRT_CLOSING, &xprt->state);
15294e857c58SPeter Zijlstra 		smp_mb__before_atomic();
15303b948ae5STrond Myklebust 		clear_bit(XPRT_CONNECTED, &xprt->state);
1531ef803670STrond Myklebust 		clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
15324e857c58SPeter Zijlstra 		smp_mb__after_atomic();
1533a246b010SChuck Lever 		break;
1534632e3bdcSTrond Myklebust 	case TCP_CLOSE_WAIT:
15353b948ae5STrond Myklebust 		/* The server initiated a shutdown of the socket */
15367c1d71cfSTrond Myklebust 		xprt->connect_cookie++;
1537d0bea455STrond Myklebust 		clear_bit(XPRT_CONNECTED, &xprt->state);
15384f8943f8STrond Myklebust 		xs_run_error_worker(transport, XPRT_SOCK_WAKE_DISCONNECT);
1539e9d47639SGustavo A. R. Silva 		/* fall through */
1540663b8858STrond Myklebust 	case TCP_CLOSING:
1541663b8858STrond Myklebust 		/*
1542663b8858STrond Myklebust 		 * If the server closed down the connection, make sure that
1543663b8858STrond Myklebust 		 * we back off before reconnecting
1544663b8858STrond Myklebust 		 */
1545663b8858STrond Myklebust 		if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
1546663b8858STrond Myklebust 			xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
15473b948ae5STrond Myklebust 		break;
15483b948ae5STrond Myklebust 	case TCP_LAST_ACK:
1549670f9457STrond Myklebust 		set_bit(XPRT_CLOSING, &xprt->state);
15504e857c58SPeter Zijlstra 		smp_mb__before_atomic();
15513b948ae5STrond Myklebust 		clear_bit(XPRT_CONNECTED, &xprt->state);
15524e857c58SPeter Zijlstra 		smp_mb__after_atomic();
15533b948ae5STrond Myklebust 		break;
15543b948ae5STrond Myklebust 	case TCP_CLOSE:
15550fdea1e8STrond Myklebust 		if (test_and_clear_bit(XPRT_SOCK_CONNECTING,
15560fdea1e8STrond Myklebust 					&transport->sock_state))
15570fdea1e8STrond Myklebust 			xprt_clear_connecting(xprt);
15589b30889cSTrond Myklebust 		clear_bit(XPRT_CLOSING, &xprt->state);
15599b30889cSTrond Myklebust 		/* Trigger the socket release */
15604f8943f8STrond Myklebust 		xs_run_error_worker(transport, XPRT_SOCK_WAKE_DISCONNECT);
1561a246b010SChuck Lever 	}
1562a246b010SChuck Lever  out:
1563f064af1eSEric Dumazet 	read_unlock_bh(&sk->sk_callback_lock);
1564a246b010SChuck Lever }
1565a246b010SChuck Lever 
15661f0fa154SIlpo Järvinen static void xs_write_space(struct sock *sk)
15671f0fa154SIlpo Järvinen {
156813331a55STrond Myklebust 	struct socket_wq *wq;
15694f8943f8STrond Myklebust 	struct sock_xprt *transport;
15701f0fa154SIlpo Järvinen 	struct rpc_xprt *xprt;
15711f0fa154SIlpo Järvinen 
157213331a55STrond Myklebust 	if (!sk->sk_socket)
15731f0fa154SIlpo Järvinen 		return;
157413331a55STrond Myklebust 	clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
15751f0fa154SIlpo Järvinen 
15761f0fa154SIlpo Järvinen 	if (unlikely(!(xprt = xprt_from_sock(sk))))
15771f0fa154SIlpo Järvinen 		return;
15784f8943f8STrond Myklebust 	transport = container_of(xprt, struct sock_xprt, xprt);
157913331a55STrond Myklebust 	rcu_read_lock();
158013331a55STrond Myklebust 	wq = rcu_dereference(sk->sk_wq);
158113331a55STrond Myklebust 	if (!wq || test_and_clear_bit(SOCKWQ_ASYNC_NOSPACE, &wq->flags) == 0)
158213331a55STrond Myklebust 		goto out;
15831f0fa154SIlpo Järvinen 
15844f8943f8STrond Myklebust 	xs_run_error_worker(transport, XPRT_SOCK_WAKE_WRITE);
1585c544577dSTrond Myklebust 	sk->sk_write_pending--;
158613331a55STrond Myklebust out:
158713331a55STrond Myklebust 	rcu_read_unlock();
15881f0fa154SIlpo Järvinen }
15891f0fa154SIlpo Järvinen 
15902a9e1cfaSTrond Myklebust /**
1591c7b2cae8SChuck Lever  * xs_udp_write_space - callback invoked when socket buffer space
1592c7b2cae8SChuck Lever  *                             becomes available
15939903cd1cSChuck Lever  * @sk: socket whose state has changed
15949903cd1cSChuck Lever  *
1595a246b010SChuck Lever  * Called when more output buffer space is available for this socket.
1596a246b010SChuck Lever  * We try not to wake our writers until they can make "significant"
1597c7b2cae8SChuck Lever  * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1598a246b010SChuck Lever  * with a bunch of small requests.
1599a246b010SChuck Lever  */
1600c7b2cae8SChuck Lever static void xs_udp_write_space(struct sock *sk)
1601a246b010SChuck Lever {
1602f064af1eSEric Dumazet 	read_lock_bh(&sk->sk_callback_lock);
1603c7b2cae8SChuck Lever 
1604c7b2cae8SChuck Lever 	/* from net/core/sock.c:sock_def_write_space */
16051f0fa154SIlpo Järvinen 	if (sock_writeable(sk))
16061f0fa154SIlpo Järvinen 		xs_write_space(sk);
1607c7b2cae8SChuck Lever 
1608f064af1eSEric Dumazet 	read_unlock_bh(&sk->sk_callback_lock);
1609c7b2cae8SChuck Lever }
1610c7b2cae8SChuck Lever 
1611c7b2cae8SChuck Lever /**
1612c7b2cae8SChuck Lever  * xs_tcp_write_space - callback invoked when socket buffer space
1613c7b2cae8SChuck Lever  *                             becomes available
1614c7b2cae8SChuck Lever  * @sk: socket whose state has changed
1615c7b2cae8SChuck Lever  *
1616c7b2cae8SChuck Lever  * Called when more output buffer space is available for this socket.
1617c7b2cae8SChuck Lever  * We try not to wake our writers until they can make "significant"
1618c7b2cae8SChuck Lever  * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1619c7b2cae8SChuck Lever  * with a bunch of small requests.
1620c7b2cae8SChuck Lever  */
1621c7b2cae8SChuck Lever static void xs_tcp_write_space(struct sock *sk)
1622c7b2cae8SChuck Lever {
1623f064af1eSEric Dumazet 	read_lock_bh(&sk->sk_callback_lock);
1624c7b2cae8SChuck Lever 
1625c7b2cae8SChuck Lever 	/* from net/core/stream.c:sk_stream_write_space */
162664dc6130SEric Dumazet 	if (sk_stream_is_writeable(sk))
16271f0fa154SIlpo Järvinen 		xs_write_space(sk);
1628c7b2cae8SChuck Lever 
1629f064af1eSEric Dumazet 	read_unlock_bh(&sk->sk_callback_lock);
1630a246b010SChuck Lever }
1631a246b010SChuck Lever 
1632470056c2SChuck Lever static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt)
1633a246b010SChuck Lever {
1634ee0ac0c2SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1635ee0ac0c2SChuck Lever 	struct sock *sk = transport->inet;
1636a246b010SChuck Lever 
16377c6e066eSChuck Lever 	if (transport->rcvsize) {
1638a246b010SChuck Lever 		sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
16397c6e066eSChuck Lever 		sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2;
1640a246b010SChuck Lever 	}
16417c6e066eSChuck Lever 	if (transport->sndsize) {
1642a246b010SChuck Lever 		sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
16437c6e066eSChuck Lever 		sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2;
1644a246b010SChuck Lever 		sk->sk_write_space(sk);
1645a246b010SChuck Lever 	}
1646a246b010SChuck Lever }
1647a246b010SChuck Lever 
164843118c29SChuck Lever /**
1649470056c2SChuck Lever  * xs_udp_set_buffer_size - set send and receive limits
165043118c29SChuck Lever  * @xprt: generic transport
1651470056c2SChuck Lever  * @sndsize: requested size of send buffer, in bytes
1652470056c2SChuck Lever  * @rcvsize: requested size of receive buffer, in bytes
165343118c29SChuck Lever  *
1654470056c2SChuck Lever  * Set socket send and receive buffer size limits.
165543118c29SChuck Lever  */
1656470056c2SChuck Lever static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize)
165743118c29SChuck Lever {
16587c6e066eSChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
16597c6e066eSChuck Lever 
16607c6e066eSChuck Lever 	transport->sndsize = 0;
1661470056c2SChuck Lever 	if (sndsize)
16627c6e066eSChuck Lever 		transport->sndsize = sndsize + 1024;
16637c6e066eSChuck Lever 	transport->rcvsize = 0;
1664470056c2SChuck Lever 	if (rcvsize)
16657c6e066eSChuck Lever 		transport->rcvsize = rcvsize + 1024;
1666470056c2SChuck Lever 
1667470056c2SChuck Lever 	xs_udp_do_set_buffer_size(xprt);
166843118c29SChuck Lever }
166943118c29SChuck Lever 
167046c0ee8bSChuck Lever /**
167146c0ee8bSChuck Lever  * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
1672acf0a39fSChuck Lever  * @xprt: controlling transport
167346c0ee8bSChuck Lever  * @task: task that timed out
167446c0ee8bSChuck Lever  *
167546c0ee8bSChuck Lever  * Adjust the congestion window after a retransmit timeout has occurred.
167646c0ee8bSChuck Lever  */
16776a24dfb6STrond Myklebust static void xs_udp_timer(struct rpc_xprt *xprt, struct rpc_task *task)
167846c0ee8bSChuck Lever {
1679b5e92419STrond Myklebust 	spin_lock(&xprt->transport_lock);
16806a24dfb6STrond Myklebust 	xprt_adjust_cwnd(xprt, task, -ETIMEDOUT);
1681b5e92419STrond Myklebust 	spin_unlock(&xprt->transport_lock);
168246c0ee8bSChuck Lever }
168346c0ee8bSChuck Lever 
1684826799e6SJ. Bruce Fields static int xs_get_random_port(void)
1685b85d8806SChuck Lever {
1686826799e6SJ. Bruce Fields 	unsigned short min = xprt_min_resvport, max = xprt_max_resvport;
1687826799e6SJ. Bruce Fields 	unsigned short range;
1688826799e6SJ. Bruce Fields 	unsigned short rand;
1689826799e6SJ. Bruce Fields 
1690826799e6SJ. Bruce Fields 	if (max < min)
1691826799e6SJ. Bruce Fields 		return -EADDRINUSE;
1692826799e6SJ. Bruce Fields 	range = max - min + 1;
1693826799e6SJ. Bruce Fields 	rand = (unsigned short) prandom_u32() % range;
1694826799e6SJ. Bruce Fields 	return rand + min;
1695b85d8806SChuck Lever }
1696b85d8806SChuck Lever 
169792200412SChuck Lever /**
16984dda9c8aSTrond Myklebust  * xs_set_reuseaddr_port - set the socket's port and address reuse options
16994dda9c8aSTrond Myklebust  * @sock: socket
17004dda9c8aSTrond Myklebust  *
17014dda9c8aSTrond Myklebust  * Note that this function has to be called on all sockets that share the
17024dda9c8aSTrond Myklebust  * same port, and it must be called before binding.
17034dda9c8aSTrond Myklebust  */
17044dda9c8aSTrond Myklebust static void xs_sock_set_reuseport(struct socket *sock)
17054dda9c8aSTrond Myklebust {
1706402e23b4STrond Myklebust 	int opt = 1;
17074dda9c8aSTrond Myklebust 
1708402e23b4STrond Myklebust 	kernel_setsockopt(sock, SOL_SOCKET, SO_REUSEPORT,
1709402e23b4STrond Myklebust 			(char *)&opt, sizeof(opt));
17104dda9c8aSTrond Myklebust }
17114dda9c8aSTrond Myklebust 
17124dda9c8aSTrond Myklebust static unsigned short xs_sock_getport(struct socket *sock)
17134dda9c8aSTrond Myklebust {
17144dda9c8aSTrond Myklebust 	struct sockaddr_storage buf;
17154dda9c8aSTrond Myklebust 	unsigned short port = 0;
17164dda9c8aSTrond Myklebust 
17179b2c45d4SDenys Vlasenko 	if (kernel_getsockname(sock, (struct sockaddr *)&buf) < 0)
17184dda9c8aSTrond Myklebust 		goto out;
17194dda9c8aSTrond Myklebust 	switch (buf.ss_family) {
17204dda9c8aSTrond Myklebust 	case AF_INET6:
17214dda9c8aSTrond Myklebust 		port = ntohs(((struct sockaddr_in6 *)&buf)->sin6_port);
17224dda9c8aSTrond Myklebust 		break;
17234dda9c8aSTrond Myklebust 	case AF_INET:
17244dda9c8aSTrond Myklebust 		port = ntohs(((struct sockaddr_in *)&buf)->sin_port);
17254dda9c8aSTrond Myklebust 	}
17264dda9c8aSTrond Myklebust out:
17274dda9c8aSTrond Myklebust 	return port;
17284dda9c8aSTrond Myklebust }
17294dda9c8aSTrond Myklebust 
17304dda9c8aSTrond Myklebust /**
173192200412SChuck Lever  * xs_set_port - reset the port number in the remote endpoint address
173292200412SChuck Lever  * @xprt: generic transport
173392200412SChuck Lever  * @port: new port number
173492200412SChuck Lever  *
173592200412SChuck Lever  */
173692200412SChuck Lever static void xs_set_port(struct rpc_xprt *xprt, unsigned short port)
173792200412SChuck Lever {
173892200412SChuck Lever 	dprintk("RPC:       setting port for xprt %p to %u\n", xprt, port);
1739c4efcb1dSChuck Lever 
17409dc3b095SChuck Lever 	rpc_set_port(xs_addr(xprt), port);
17419dc3b095SChuck Lever 	xs_update_peer_port(xprt);
174292200412SChuck Lever }
174392200412SChuck Lever 
17444dda9c8aSTrond Myklebust static void xs_set_srcport(struct sock_xprt *transport, struct socket *sock)
17454dda9c8aSTrond Myklebust {
17464dda9c8aSTrond Myklebust 	if (transport->srcport == 0)
17474dda9c8aSTrond Myklebust 		transport->srcport = xs_sock_getport(sock);
17484dda9c8aSTrond Myklebust }
17494dda9c8aSTrond Myklebust 
1750826799e6SJ. Bruce Fields static int xs_get_srcport(struct sock_xprt *transport)
175167a391d7STrond Myklebust {
1752826799e6SJ. Bruce Fields 	int port = transport->srcport;
175367a391d7STrond Myklebust 
175467a391d7STrond Myklebust 	if (port == 0 && transport->xprt.resvport)
175567a391d7STrond Myklebust 		port = xs_get_random_port();
175667a391d7STrond Myklebust 	return port;
175767a391d7STrond Myklebust }
175867a391d7STrond Myklebust 
1759baaf4e48SPavel Emelyanov static unsigned short xs_next_srcport(struct sock_xprt *transport, unsigned short port)
176067a391d7STrond Myklebust {
1761fbfffbd5SChuck Lever 	if (transport->srcport != 0)
1762fbfffbd5SChuck Lever 		transport->srcport = 0;
176367a391d7STrond Myklebust 	if (!transport->xprt.resvport)
176467a391d7STrond Myklebust 		return 0;
176567a391d7STrond Myklebust 	if (port <= xprt_min_resvport || port > xprt_max_resvport)
176667a391d7STrond Myklebust 		return xprt_max_resvport;
176767a391d7STrond Myklebust 	return --port;
176867a391d7STrond Myklebust }
1769beb59b68SPavel Emelyanov static int xs_bind(struct sock_xprt *transport, struct socket *sock)
1770a246b010SChuck Lever {
1771beb59b68SPavel Emelyanov 	struct sockaddr_storage myaddr;
177267a391d7STrond Myklebust 	int err, nloop = 0;
1773826799e6SJ. Bruce Fields 	int port = xs_get_srcport(transport);
177467a391d7STrond Myklebust 	unsigned short last;
1775a246b010SChuck Lever 
17760f7a622cSChris Perl 	/*
17770f7a622cSChris Perl 	 * If we are asking for any ephemeral port (i.e. port == 0 &&
17780f7a622cSChris Perl 	 * transport->xprt.resvport == 0), don't bind.  Let the local
17790f7a622cSChris Perl 	 * port selection happen implicitly when the socket is used
17800f7a622cSChris Perl 	 * (for example at connect time).
17810f7a622cSChris Perl 	 *
17820f7a622cSChris Perl 	 * This ensures that we can continue to establish TCP
17830f7a622cSChris Perl 	 * connections even when all local ephemeral ports are already
17840f7a622cSChris Perl 	 * a part of some TCP connection.  This makes no difference
17850f7a622cSChris Perl 	 * for UDP sockets, but also doens't harm them.
17860f7a622cSChris Perl 	 *
17870f7a622cSChris Perl 	 * If we're asking for any reserved port (i.e. port == 0 &&
17880f7a622cSChris Perl 	 * transport->xprt.resvport == 1) xs_get_srcport above will
17890f7a622cSChris Perl 	 * ensure that port is non-zero and we will bind as needed.
17900f7a622cSChris Perl 	 */
1791826799e6SJ. Bruce Fields 	if (port <= 0)
1792826799e6SJ. Bruce Fields 		return port;
17930f7a622cSChris Perl 
1794beb59b68SPavel Emelyanov 	memcpy(&myaddr, &transport->srcaddr, transport->xprt.addrlen);
1795a246b010SChuck Lever 	do {
1796beb59b68SPavel Emelyanov 		rpc_set_port((struct sockaddr *)&myaddr, port);
1797e6242e92SSridhar Samudrala 		err = kernel_bind(sock, (struct sockaddr *)&myaddr,
1798beb59b68SPavel Emelyanov 				transport->xprt.addrlen);
1799a246b010SChuck Lever 		if (err == 0) {
1800fbfffbd5SChuck Lever 			transport->srcport = port;
1801d3bc9a1dSFrank van Maarseveen 			break;
1802a246b010SChuck Lever 		}
180367a391d7STrond Myklebust 		last = port;
1804baaf4e48SPavel Emelyanov 		port = xs_next_srcport(transport, port);
180567a391d7STrond Myklebust 		if (port > last)
180667a391d7STrond Myklebust 			nloop++;
180767a391d7STrond Myklebust 	} while (err == -EADDRINUSE && nloop != 2);
1808beb59b68SPavel Emelyanov 
18094232e863SChuck Lever 	if (myaddr.ss_family == AF_INET)
1810beb59b68SPavel Emelyanov 		dprintk("RPC:       %s %pI4:%u: %s (%d)\n", __func__,
1811beb59b68SPavel Emelyanov 				&((struct sockaddr_in *)&myaddr)->sin_addr,
1812beb59b68SPavel Emelyanov 				port, err ? "failed" : "ok", err);
1813beb59b68SPavel Emelyanov 	else
1814beb59b68SPavel Emelyanov 		dprintk("RPC:       %s %pI6:%u: %s (%d)\n", __func__,
1815beb59b68SPavel Emelyanov 				&((struct sockaddr_in6 *)&myaddr)->sin6_addr,
18167dc753f0SChuck Lever 				port, err ? "failed" : "ok", err);
1817a246b010SChuck Lever 	return err;
1818a246b010SChuck Lever }
1819a246b010SChuck Lever 
1820176e21eeSChuck Lever /*
1821176e21eeSChuck Lever  * We don't support autobind on AF_LOCAL sockets
1822176e21eeSChuck Lever  */
1823176e21eeSChuck Lever static void xs_local_rpcbind(struct rpc_task *task)
1824176e21eeSChuck Lever {
1825fb43d172STrond Myklebust 	xprt_set_bound(task->tk_xprt);
1826176e21eeSChuck Lever }
1827176e21eeSChuck Lever 
1828176e21eeSChuck Lever static void xs_local_set_port(struct rpc_xprt *xprt, unsigned short port)
1829176e21eeSChuck Lever {
1830176e21eeSChuck Lever }
1831a246b010SChuck Lever 
1832ed07536eSPeter Zijlstra #ifdef CONFIG_DEBUG_LOCK_ALLOC
1833ed07536eSPeter Zijlstra static struct lock_class_key xs_key[2];
1834ed07536eSPeter Zijlstra static struct lock_class_key xs_slock_key[2];
1835ed07536eSPeter Zijlstra 
1836176e21eeSChuck Lever static inline void xs_reclassify_socketu(struct socket *sock)
1837176e21eeSChuck Lever {
1838176e21eeSChuck Lever 	struct sock *sk = sock->sk;
1839176e21eeSChuck Lever 
1840176e21eeSChuck Lever 	sock_lock_init_class_and_name(sk, "slock-AF_LOCAL-RPC",
1841176e21eeSChuck Lever 		&xs_slock_key[1], "sk_lock-AF_LOCAL-RPC", &xs_key[1]);
1842176e21eeSChuck Lever }
1843176e21eeSChuck Lever 
18448945ee5eSChuck Lever static inline void xs_reclassify_socket4(struct socket *sock)
1845ed07536eSPeter Zijlstra {
1846ed07536eSPeter Zijlstra 	struct sock *sk = sock->sk;
18478945ee5eSChuck Lever 
18488945ee5eSChuck Lever 	sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC",
18498945ee5eSChuck Lever 		&xs_slock_key[0], "sk_lock-AF_INET-RPC", &xs_key[0]);
1850ed07536eSPeter Zijlstra }
18518945ee5eSChuck Lever 
18528945ee5eSChuck Lever static inline void xs_reclassify_socket6(struct socket *sock)
18538945ee5eSChuck Lever {
18548945ee5eSChuck Lever 	struct sock *sk = sock->sk;
18558945ee5eSChuck Lever 
18568945ee5eSChuck Lever 	sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC",
18578945ee5eSChuck Lever 		&xs_slock_key[1], "sk_lock-AF_INET6-RPC", &xs_key[1]);
1858ed07536eSPeter Zijlstra }
18596bc9638aSPavel Emelyanov 
18606bc9638aSPavel Emelyanov static inline void xs_reclassify_socket(int family, struct socket *sock)
18616bc9638aSPavel Emelyanov {
1862fafc4e1eSHannes Frederic Sowa 	if (WARN_ON_ONCE(!sock_allow_reclassification(sock->sk)))
18631b7a1819SWeston Andros Adamson 		return;
18641b7a1819SWeston Andros Adamson 
18654232e863SChuck Lever 	switch (family) {
1866176e21eeSChuck Lever 	case AF_LOCAL:
1867176e21eeSChuck Lever 		xs_reclassify_socketu(sock);
1868176e21eeSChuck Lever 		break;
18694232e863SChuck Lever 	case AF_INET:
18706bc9638aSPavel Emelyanov 		xs_reclassify_socket4(sock);
18714232e863SChuck Lever 		break;
18724232e863SChuck Lever 	case AF_INET6:
18736bc9638aSPavel Emelyanov 		xs_reclassify_socket6(sock);
18744232e863SChuck Lever 		break;
18754232e863SChuck Lever 	}
18766bc9638aSPavel Emelyanov }
1877ed07536eSPeter Zijlstra #else
18786bc9638aSPavel Emelyanov static inline void xs_reclassify_socket(int family, struct socket *sock)
18796bc9638aSPavel Emelyanov {
18806bc9638aSPavel Emelyanov }
1881ed07536eSPeter Zijlstra #endif
1882ed07536eSPeter Zijlstra 
188393dc41bdSNeilBrown static void xs_dummy_setup_socket(struct work_struct *work)
188493dc41bdSNeilBrown {
188593dc41bdSNeilBrown }
188693dc41bdSNeilBrown 
18876bc9638aSPavel Emelyanov static struct socket *xs_create_sock(struct rpc_xprt *xprt,
18884dda9c8aSTrond Myklebust 		struct sock_xprt *transport, int family, int type,
18894dda9c8aSTrond Myklebust 		int protocol, bool reuseport)
189022f79326SPavel Emelyanov {
1891a73881c9STrond Myklebust 	struct file *filp;
189222f79326SPavel Emelyanov 	struct socket *sock;
189322f79326SPavel Emelyanov 	int err;
189422f79326SPavel Emelyanov 
18956bc9638aSPavel Emelyanov 	err = __sock_create(xprt->xprt_net, family, type, protocol, &sock, 1);
189622f79326SPavel Emelyanov 	if (err < 0) {
189722f79326SPavel Emelyanov 		dprintk("RPC:       can't create %d transport socket (%d).\n",
189822f79326SPavel Emelyanov 				protocol, -err);
189922f79326SPavel Emelyanov 		goto out;
190022f79326SPavel Emelyanov 	}
19016bc9638aSPavel Emelyanov 	xs_reclassify_socket(family, sock);
190222f79326SPavel Emelyanov 
19034dda9c8aSTrond Myklebust 	if (reuseport)
19044dda9c8aSTrond Myklebust 		xs_sock_set_reuseport(sock);
19054dda9c8aSTrond Myklebust 
19064cea288aSBen Hutchings 	err = xs_bind(transport, sock);
19074cea288aSBen Hutchings 	if (err) {
190822f79326SPavel Emelyanov 		sock_release(sock);
190922f79326SPavel Emelyanov 		goto out;
191022f79326SPavel Emelyanov 	}
191122f79326SPavel Emelyanov 
1912a73881c9STrond Myklebust 	filp = sock_alloc_file(sock, O_NONBLOCK, NULL);
1913a73881c9STrond Myklebust 	if (IS_ERR(filp))
1914a73881c9STrond Myklebust 		return ERR_CAST(filp);
1915a73881c9STrond Myklebust 	transport->file = filp;
1916a73881c9STrond Myklebust 
191722f79326SPavel Emelyanov 	return sock;
191822f79326SPavel Emelyanov out:
191922f79326SPavel Emelyanov 	return ERR_PTR(err);
192022f79326SPavel Emelyanov }
192122f79326SPavel Emelyanov 
1922176e21eeSChuck Lever static int xs_local_finish_connecting(struct rpc_xprt *xprt,
1923176e21eeSChuck Lever 				      struct socket *sock)
1924176e21eeSChuck Lever {
1925176e21eeSChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
1926176e21eeSChuck Lever 									xprt);
1927176e21eeSChuck Lever 
1928176e21eeSChuck Lever 	if (!transport->inet) {
1929176e21eeSChuck Lever 		struct sock *sk = sock->sk;
1930176e21eeSChuck Lever 
1931176e21eeSChuck Lever 		write_lock_bh(&sk->sk_callback_lock);
1932176e21eeSChuck Lever 
1933176e21eeSChuck Lever 		xs_save_old_callbacks(transport, sk);
1934176e21eeSChuck Lever 
1935176e21eeSChuck Lever 		sk->sk_user_data = xprt;
1936a2648094STrond Myklebust 		sk->sk_data_ready = xs_data_ready;
1937176e21eeSChuck Lever 		sk->sk_write_space = xs_udp_write_space;
1938b4411457SEric Dumazet 		sock_set_flag(sk, SOCK_FASYNC);
19392118071dSTrond Myklebust 		sk->sk_error_report = xs_error_report;
1940176e21eeSChuck Lever 
1941176e21eeSChuck Lever 		xprt_clear_connected(xprt);
1942176e21eeSChuck Lever 
1943176e21eeSChuck Lever 		/* Reset to new socket */
1944176e21eeSChuck Lever 		transport->sock = sock;
1945176e21eeSChuck Lever 		transport->inet = sk;
1946176e21eeSChuck Lever 
1947176e21eeSChuck Lever 		write_unlock_bh(&sk->sk_callback_lock);
1948176e21eeSChuck Lever 	}
1949176e21eeSChuck Lever 
1950ae053551STrond Myklebust 	xs_stream_start_connect(transport);
19516c7a64e5STrond Myklebust 
1952176e21eeSChuck Lever 	return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, 0);
1953176e21eeSChuck Lever }
1954176e21eeSChuck Lever 
1955176e21eeSChuck Lever /**
1956176e21eeSChuck Lever  * xs_local_setup_socket - create AF_LOCAL socket, connect to a local endpoint
1957176e21eeSChuck Lever  * @transport: socket transport to connect
1958176e21eeSChuck Lever  */
1959dc107402SJ. Bruce Fields static int xs_local_setup_socket(struct sock_xprt *transport)
1960176e21eeSChuck Lever {
1961176e21eeSChuck Lever 	struct rpc_xprt *xprt = &transport->xprt;
1962a73881c9STrond Myklebust 	struct file *filp;
1963176e21eeSChuck Lever 	struct socket *sock;
1964176e21eeSChuck Lever 	int status = -EIO;
1965176e21eeSChuck Lever 
1966176e21eeSChuck Lever 	status = __sock_create(xprt->xprt_net, AF_LOCAL,
1967176e21eeSChuck Lever 					SOCK_STREAM, 0, &sock, 1);
1968176e21eeSChuck Lever 	if (status < 0) {
1969176e21eeSChuck Lever 		dprintk("RPC:       can't create AF_LOCAL "
1970176e21eeSChuck Lever 			"transport socket (%d).\n", -status);
1971176e21eeSChuck Lever 		goto out;
1972176e21eeSChuck Lever 	}
1973d1358917SStefan Hajnoczi 	xs_reclassify_socket(AF_LOCAL, sock);
1974176e21eeSChuck Lever 
1975a73881c9STrond Myklebust 	filp = sock_alloc_file(sock, O_NONBLOCK, NULL);
1976a73881c9STrond Myklebust 	if (IS_ERR(filp)) {
1977a73881c9STrond Myklebust 		status = PTR_ERR(filp);
1978a73881c9STrond Myklebust 		goto out;
1979a73881c9STrond Myklebust 	}
1980a73881c9STrond Myklebust 	transport->file = filp;
1981a73881c9STrond Myklebust 
1982176e21eeSChuck Lever 	dprintk("RPC:       worker connecting xprt %p via AF_LOCAL to %s\n",
1983176e21eeSChuck Lever 			xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1984176e21eeSChuck Lever 
1985176e21eeSChuck Lever 	status = xs_local_finish_connecting(xprt, sock);
198640b5ea0cSTrond Myklebust 	trace_rpc_socket_connect(xprt, sock, status);
1987176e21eeSChuck Lever 	switch (status) {
1988176e21eeSChuck Lever 	case 0:
1989176e21eeSChuck Lever 		dprintk("RPC:       xprt %p connected to %s\n",
1990176e21eeSChuck Lever 				xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
19913968a8a5SChuck Lever 		xprt->stat.connect_count++;
19923968a8a5SChuck Lever 		xprt->stat.connect_time += (long)jiffies -
19933968a8a5SChuck Lever 					   xprt->stat.connect_start;
1994176e21eeSChuck Lever 		xprt_set_connected(xprt);
19953601c4a9STrond Myklebust 	case -ENOBUFS:
1996176e21eeSChuck Lever 		break;
1997176e21eeSChuck Lever 	case -ENOENT:
1998176e21eeSChuck Lever 		dprintk("RPC:       xprt %p: socket %s does not exist\n",
1999176e21eeSChuck Lever 				xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
2000176e21eeSChuck Lever 		break;
20014a20a988STrond Myklebust 	case -ECONNREFUSED:
20024a20a988STrond Myklebust 		dprintk("RPC:       xprt %p: connection refused for %s\n",
20034a20a988STrond Myklebust 				xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
20044a20a988STrond Myklebust 		break;
2005176e21eeSChuck Lever 	default:
2006176e21eeSChuck Lever 		printk(KERN_ERR "%s: unhandled error (%d) connecting to %s\n",
2007176e21eeSChuck Lever 				__func__, -status,
2008176e21eeSChuck Lever 				xprt->address_strings[RPC_DISPLAY_ADDR]);
2009176e21eeSChuck Lever 	}
2010176e21eeSChuck Lever 
2011176e21eeSChuck Lever out:
2012176e21eeSChuck Lever 	xprt_clear_connecting(xprt);
2013176e21eeSChuck Lever 	xprt_wake_pending_tasks(xprt, status);
2014dc107402SJ. Bruce Fields 	return status;
2015dc107402SJ. Bruce Fields }
2016dc107402SJ. Bruce Fields 
2017b6669737SLinus Torvalds static void xs_local_connect(struct rpc_xprt *xprt, struct rpc_task *task)
2018dc107402SJ. Bruce Fields {
2019dc107402SJ. Bruce Fields 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2020dc107402SJ. Bruce Fields 	int ret;
2021dc107402SJ. Bruce Fields 
2022dc107402SJ. Bruce Fields 	 if (RPC_IS_ASYNC(task)) {
2023dc107402SJ. Bruce Fields 		/*
2024dc107402SJ. Bruce Fields 		 * We want the AF_LOCAL connect to be resolved in the
2025dc107402SJ. Bruce Fields 		 * filesystem namespace of the process making the rpc
2026dc107402SJ. Bruce Fields 		 * call.  Thus we connect synchronously.
2027dc107402SJ. Bruce Fields 		 *
2028dc107402SJ. Bruce Fields 		 * If we want to support asynchronous AF_LOCAL calls,
2029dc107402SJ. Bruce Fields 		 * we'll need to figure out how to pass a namespace to
2030dc107402SJ. Bruce Fields 		 * connect.
2031dc107402SJ. Bruce Fields 		 */
20325ad64b36STrond Myklebust 		task->tk_rpc_status = -ENOTCONN;
2033dc107402SJ. Bruce Fields 		rpc_exit(task, -ENOTCONN);
2034dc107402SJ. Bruce Fields 		return;
2035dc107402SJ. Bruce Fields 	}
2036dc107402SJ. Bruce Fields 	ret = xs_local_setup_socket(transport);
2037dc107402SJ. Bruce Fields 	if (ret && !RPC_IS_SOFTCONN(task))
2038dc107402SJ. Bruce Fields 		msleep_interruptible(15000);
2039176e21eeSChuck Lever }
2040176e21eeSChuck Lever 
20413c87ef6eSJeff Layton #if IS_ENABLED(CONFIG_SUNRPC_SWAP)
2042d6e971d8SJeff Layton /*
2043d6e971d8SJeff Layton  * Note that this should be called with XPRT_LOCKED held (or when we otherwise
2044d6e971d8SJeff Layton  * know that we have exclusive access to the socket), to guard against
2045d6e971d8SJeff Layton  * races with xs_reset_transport.
2046d6e971d8SJeff Layton  */
2047a564b8f0SMel Gorman static void xs_set_memalloc(struct rpc_xprt *xprt)
2048a564b8f0SMel Gorman {
2049a564b8f0SMel Gorman 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
2050a564b8f0SMel Gorman 			xprt);
2051a564b8f0SMel Gorman 
2052d6e971d8SJeff Layton 	/*
2053d6e971d8SJeff Layton 	 * If there's no sock, then we have nothing to set. The
2054d6e971d8SJeff Layton 	 * reconnecting process will get it for us.
2055d6e971d8SJeff Layton 	 */
2056d6e971d8SJeff Layton 	if (!transport->inet)
2057d6e971d8SJeff Layton 		return;
20588e228133SJeff Layton 	if (atomic_read(&xprt->swapper))
2059a564b8f0SMel Gorman 		sk_set_memalloc(transport->inet);
2060a564b8f0SMel Gorman }
2061a564b8f0SMel Gorman 
2062a564b8f0SMel Gorman /**
2063d67fa4d8SJeff Layton  * xs_enable_swap - Tag this transport as being used for swap.
2064a564b8f0SMel Gorman  * @xprt: transport to tag
2065a564b8f0SMel Gorman  *
20668e228133SJeff Layton  * Take a reference to this transport on behalf of the rpc_clnt, and
20678e228133SJeff Layton  * optionally mark it for swapping if it wasn't already.
2068a564b8f0SMel Gorman  */
2069d67fa4d8SJeff Layton static int
2070d67fa4d8SJeff Layton xs_enable_swap(struct rpc_xprt *xprt)
2071a564b8f0SMel Gorman {
2072d6e971d8SJeff Layton 	struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt);
2073a564b8f0SMel Gorman 
2074d6e971d8SJeff Layton 	if (atomic_inc_return(&xprt->swapper) != 1)
2075d6e971d8SJeff Layton 		return 0;
2076d6e971d8SJeff Layton 	if (wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_KILLABLE))
2077d6e971d8SJeff Layton 		return -ERESTARTSYS;
2078d6e971d8SJeff Layton 	if (xs->inet)
2079d6e971d8SJeff Layton 		sk_set_memalloc(xs->inet);
2080d6e971d8SJeff Layton 	xprt_release_xprt(xprt, NULL);
20818e228133SJeff Layton 	return 0;
2082a564b8f0SMel Gorman }
2083a564b8f0SMel Gorman 
20848e228133SJeff Layton /**
2085d67fa4d8SJeff Layton  * xs_disable_swap - Untag this transport as being used for swap.
20868e228133SJeff Layton  * @xprt: transport to tag
20878e228133SJeff Layton  *
20888e228133SJeff Layton  * Drop a "swapper" reference to this xprt on behalf of the rpc_clnt. If the
20898e228133SJeff Layton  * swapper refcount goes to 0, untag the socket as a memalloc socket.
20908e228133SJeff Layton  */
2091d67fa4d8SJeff Layton static void
2092d67fa4d8SJeff Layton xs_disable_swap(struct rpc_xprt *xprt)
20938e228133SJeff Layton {
2094d6e971d8SJeff Layton 	struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt);
20958e228133SJeff Layton 
2096d6e971d8SJeff Layton 	if (!atomic_dec_and_test(&xprt->swapper))
2097d6e971d8SJeff Layton 		return;
2098d6e971d8SJeff Layton 	if (wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_KILLABLE))
2099d6e971d8SJeff Layton 		return;
2100d6e971d8SJeff Layton 	if (xs->inet)
2101d6e971d8SJeff Layton 		sk_clear_memalloc(xs->inet);
2102d6e971d8SJeff Layton 	xprt_release_xprt(xprt, NULL);
2103a564b8f0SMel Gorman }
2104a564b8f0SMel Gorman #else
2105a564b8f0SMel Gorman static void xs_set_memalloc(struct rpc_xprt *xprt)
2106a564b8f0SMel Gorman {
2107a564b8f0SMel Gorman }
2108d67fa4d8SJeff Layton 
2109d67fa4d8SJeff Layton static int
2110d67fa4d8SJeff Layton xs_enable_swap(struct rpc_xprt *xprt)
2111d67fa4d8SJeff Layton {
2112d67fa4d8SJeff Layton 	return -EINVAL;
2113d67fa4d8SJeff Layton }
2114d67fa4d8SJeff Layton 
2115d67fa4d8SJeff Layton static void
2116d67fa4d8SJeff Layton xs_disable_swap(struct rpc_xprt *xprt)
2117d67fa4d8SJeff Layton {
2118d67fa4d8SJeff Layton }
2119a564b8f0SMel Gorman #endif
2120a564b8f0SMel Gorman 
212116be2d20SChuck Lever static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
2122a246b010SChuck Lever {
212316be2d20SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2124edb267a6SChuck Lever 
2125ee0ac0c2SChuck Lever 	if (!transport->inet) {
2126b0d93ad5SChuck Lever 		struct sock *sk = sock->sk;
2127b0d93ad5SChuck Lever 
2128b0d93ad5SChuck Lever 		write_lock_bh(&sk->sk_callback_lock);
2129b0d93ad5SChuck Lever 
21302a9e1cfaSTrond Myklebust 		xs_save_old_callbacks(transport, sk);
21312a9e1cfaSTrond Myklebust 
2132b0d93ad5SChuck Lever 		sk->sk_user_data = xprt;
2133f9b2ee71STrond Myklebust 		sk->sk_data_ready = xs_data_ready;
2134b0d93ad5SChuck Lever 		sk->sk_write_space = xs_udp_write_space;
2135b4411457SEric Dumazet 		sock_set_flag(sk, SOCK_FASYNC);
2136b0d93ad5SChuck Lever 
2137b0d93ad5SChuck Lever 		xprt_set_connected(xprt);
2138b0d93ad5SChuck Lever 
2139b0d93ad5SChuck Lever 		/* Reset to new socket */
2140ee0ac0c2SChuck Lever 		transport->sock = sock;
2141ee0ac0c2SChuck Lever 		transport->inet = sk;
2142b0d93ad5SChuck Lever 
2143a564b8f0SMel Gorman 		xs_set_memalloc(xprt);
2144a564b8f0SMel Gorman 
2145b0d93ad5SChuck Lever 		write_unlock_bh(&sk->sk_callback_lock);
2146b0d93ad5SChuck Lever 	}
2147470056c2SChuck Lever 	xs_udp_do_set_buffer_size(xprt);
214802910177STrond Myklebust 
214902910177STrond Myklebust 	xprt->stat.connect_start = jiffies;
215016be2d20SChuck Lever }
215116be2d20SChuck Lever 
21528c14ff2aSPavel Emelyanov static void xs_udp_setup_socket(struct work_struct *work)
2153a246b010SChuck Lever {
2154a246b010SChuck Lever 	struct sock_xprt *transport =
2155a246b010SChuck Lever 		container_of(work, struct sock_xprt, connect_worker.work);
2156a246b010SChuck Lever 	struct rpc_xprt *xprt = &transport->xprt;
2157d099b8afSColin Ian King 	struct socket *sock;
2158b65c0310SPavel Emelyanov 	int status = -EIO;
2159a246b010SChuck Lever 
21608c14ff2aSPavel Emelyanov 	sock = xs_create_sock(xprt, transport,
21614dda9c8aSTrond Myklebust 			xs_addr(xprt)->sa_family, SOCK_DGRAM,
21624dda9c8aSTrond Myklebust 			IPPROTO_UDP, false);
2163b65c0310SPavel Emelyanov 	if (IS_ERR(sock))
2164a246b010SChuck Lever 		goto out;
216568e220bdSChuck Lever 
2166c740eff8SChuck Lever 	dprintk("RPC:       worker connecting xprt %p via %s to "
2167c740eff8SChuck Lever 				"%s (port %s)\n", xprt,
2168c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_PROTO],
2169c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_ADDR],
2170c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_PORT]);
217168e220bdSChuck Lever 
217268e220bdSChuck Lever 	xs_udp_finish_connecting(xprt, sock);
217340b5ea0cSTrond Myklebust 	trace_rpc_socket_connect(xprt, sock, 0);
2174a246b010SChuck Lever 	status = 0;
2175b0d93ad5SChuck Lever out:
2176b0d93ad5SChuck Lever 	xprt_clear_connecting(xprt);
2177cf76785dSTrond Myklebust 	xprt_unlock_connect(xprt, transport);
21787d1e8255STrond Myklebust 	xprt_wake_pending_tasks(xprt, status);
2179b0d93ad5SChuck Lever }
2180b0d93ad5SChuck Lever 
21814876cc77STrond Myklebust /**
21824876cc77STrond Myklebust  * xs_tcp_shutdown - gracefully shut down a TCP socket
21834876cc77STrond Myklebust  * @xprt: transport
21844876cc77STrond Myklebust  *
21854876cc77STrond Myklebust  * Initiates a graceful shutdown of the TCP socket by calling the
21864876cc77STrond Myklebust  * equivalent of shutdown(SHUT_RDWR);
21874876cc77STrond Myklebust  */
21884876cc77STrond Myklebust static void xs_tcp_shutdown(struct rpc_xprt *xprt)
21894876cc77STrond Myklebust {
21904876cc77STrond Myklebust 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
21914876cc77STrond Myklebust 	struct socket *sock = transport->sock;
21929b30889cSTrond Myklebust 	int skst = transport->inet ? transport->inet->sk_state : TCP_CLOSE;
21934876cc77STrond Myklebust 
21944876cc77STrond Myklebust 	if (sock == NULL)
21954876cc77STrond Myklebust 		return;
21969b30889cSTrond Myklebust 	switch (skst) {
21979b30889cSTrond Myklebust 	default:
21984876cc77STrond Myklebust 		kernel_sock_shutdown(sock, SHUT_RDWR);
21994876cc77STrond Myklebust 		trace_rpc_socket_shutdown(xprt, sock);
22009b30889cSTrond Myklebust 		break;
22019b30889cSTrond Myklebust 	case TCP_CLOSE:
22029b30889cSTrond Myklebust 	case TCP_TIME_WAIT:
22034876cc77STrond Myklebust 		xs_reset_transport(transport);
22044876cc77STrond Myklebust 	}
22059b30889cSTrond Myklebust }
22064876cc77STrond Myklebust 
22078d1b8c62STrond Myklebust static void xs_tcp_set_socket_timeouts(struct rpc_xprt *xprt,
22088d1b8c62STrond Myklebust 		struct socket *sock)
2209b0d93ad5SChuck Lever {
221016be2d20SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
22117196dbb0STrond Myklebust 	unsigned int keepidle;
22127196dbb0STrond Myklebust 	unsigned int keepcnt;
22137f260e85STrond Myklebust 	unsigned int opt_on = 1;
2214775f06abSTrond Myklebust 	unsigned int timeo;
22157f260e85STrond Myklebust 
2216b5e92419STrond Myklebust 	spin_lock(&xprt->transport_lock);
22177196dbb0STrond Myklebust 	keepidle = DIV_ROUND_UP(xprt->timeout->to_initval, HZ);
22187196dbb0STrond Myklebust 	keepcnt = xprt->timeout->to_retries + 1;
22197196dbb0STrond Myklebust 	timeo = jiffies_to_msecs(xprt->timeout->to_initval) *
22207196dbb0STrond Myklebust 		(xprt->timeout->to_retries + 1);
22217196dbb0STrond Myklebust 	clear_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state);
2222b5e92419STrond Myklebust 	spin_unlock(&xprt->transport_lock);
22237f260e85STrond Myklebust 
22247f260e85STrond Myklebust 	/* TCP Keepalive options */
22257f260e85STrond Myklebust 	kernel_setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE,
22267f260e85STrond Myklebust 			(char *)&opt_on, sizeof(opt_on));
22277f260e85STrond Myklebust 	kernel_setsockopt(sock, SOL_TCP, TCP_KEEPIDLE,
22287f260e85STrond Myklebust 			(char *)&keepidle, sizeof(keepidle));
22297f260e85STrond Myklebust 	kernel_setsockopt(sock, SOL_TCP, TCP_KEEPINTVL,
22307f260e85STrond Myklebust 			(char *)&keepidle, sizeof(keepidle));
22317f260e85STrond Myklebust 	kernel_setsockopt(sock, SOL_TCP, TCP_KEEPCNT,
22327f260e85STrond Myklebust 			(char *)&keepcnt, sizeof(keepcnt));
2233b0d93ad5SChuck Lever 
22348d1b8c62STrond Myklebust 	/* TCP user timeout (see RFC5482) */
22358d1b8c62STrond Myklebust 	kernel_setsockopt(sock, SOL_TCP, TCP_USER_TIMEOUT,
22368d1b8c62STrond Myklebust 			(char *)&timeo, sizeof(timeo));
22378d1b8c62STrond Myklebust }
22388d1b8c62STrond Myklebust 
22397196dbb0STrond Myklebust static void xs_tcp_set_connect_timeout(struct rpc_xprt *xprt,
22407196dbb0STrond Myklebust 		unsigned long connect_timeout,
22417196dbb0STrond Myklebust 		unsigned long reconnect_timeout)
22427196dbb0STrond Myklebust {
22437196dbb0STrond Myklebust 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
22447196dbb0STrond Myklebust 	struct rpc_timeout to;
22457196dbb0STrond Myklebust 	unsigned long initval;
22467196dbb0STrond Myklebust 
2247b5e92419STrond Myklebust 	spin_lock(&xprt->transport_lock);
22487196dbb0STrond Myklebust 	if (reconnect_timeout < xprt->max_reconnect_timeout)
22497196dbb0STrond Myklebust 		xprt->max_reconnect_timeout = reconnect_timeout;
22507196dbb0STrond Myklebust 	if (connect_timeout < xprt->connect_timeout) {
22517196dbb0STrond Myklebust 		memcpy(&to, xprt->timeout, sizeof(to));
22527196dbb0STrond Myklebust 		initval = DIV_ROUND_UP(connect_timeout, to.to_retries + 1);
22537196dbb0STrond Myklebust 		/* Arbitrary lower limit */
22547196dbb0STrond Myklebust 		if (initval <  XS_TCP_INIT_REEST_TO << 1)
22557196dbb0STrond Myklebust 			initval = XS_TCP_INIT_REEST_TO << 1;
22567196dbb0STrond Myklebust 		to.to_initval = initval;
22577196dbb0STrond Myklebust 		to.to_maxval = initval;
22587196dbb0STrond Myklebust 		memcpy(&transport->tcp_timeout, &to,
22597196dbb0STrond Myklebust 				sizeof(transport->tcp_timeout));
22607196dbb0STrond Myklebust 		xprt->timeout = &transport->tcp_timeout;
22617196dbb0STrond Myklebust 		xprt->connect_timeout = connect_timeout;
22627196dbb0STrond Myklebust 	}
22637196dbb0STrond Myklebust 	set_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state);
2264b5e92419STrond Myklebust 	spin_unlock(&xprt->transport_lock);
22657196dbb0STrond Myklebust }
22667196dbb0STrond Myklebust 
22678d1b8c62STrond Myklebust static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
22688d1b8c62STrond Myklebust {
22698d1b8c62STrond Myklebust 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
22708d1b8c62STrond Myklebust 	int ret = -ENOTCONN;
22718d1b8c62STrond Myklebust 
22728d1b8c62STrond Myklebust 	if (!transport->inet) {
22738d1b8c62STrond Myklebust 		struct sock *sk = sock->sk;
22748d1b8c62STrond Myklebust 		unsigned int addr_pref = IPV6_PREFER_SRC_PUBLIC;
22758d1b8c62STrond Myklebust 
2276d88e4d82SNeilBrown 		/* Avoid temporary address, they are bad for long-lived
2277d88e4d82SNeilBrown 		 * connections such as NFS mounts.
2278d88e4d82SNeilBrown 		 * RFC4941, section 3.6 suggests that:
2279d88e4d82SNeilBrown 		 *    Individual applications, which have specific
2280d88e4d82SNeilBrown 		 *    knowledge about the normal duration of connections,
2281d88e4d82SNeilBrown 		 *    MAY override this as appropriate.
2282d88e4d82SNeilBrown 		 */
2283d88e4d82SNeilBrown 		kernel_setsockopt(sock, SOL_IPV6, IPV6_ADDR_PREFERENCES,
2284d88e4d82SNeilBrown 				(char *)&addr_pref, sizeof(addr_pref));
2285d88e4d82SNeilBrown 
22868d1b8c62STrond Myklebust 		xs_tcp_set_socket_timeouts(xprt, sock);
2287775f06abSTrond Myklebust 
2288b0d93ad5SChuck Lever 		write_lock_bh(&sk->sk_callback_lock);
2289b0d93ad5SChuck Lever 
22902a9e1cfaSTrond Myklebust 		xs_save_old_callbacks(transport, sk);
22912a9e1cfaSTrond Myklebust 
2292b0d93ad5SChuck Lever 		sk->sk_user_data = xprt;
22935157b956STrond Myklebust 		sk->sk_data_ready = xs_data_ready;
2294b0d93ad5SChuck Lever 		sk->sk_state_change = xs_tcp_state_change;
2295b0d93ad5SChuck Lever 		sk->sk_write_space = xs_tcp_write_space;
2296b4411457SEric Dumazet 		sock_set_flag(sk, SOCK_FASYNC);
22972118071dSTrond Myklebust 		sk->sk_error_report = xs_error_report;
22983167e12cSChuck Lever 
22993167e12cSChuck Lever 		/* socket options */
23003167e12cSChuck Lever 		sock_reset_flag(sk, SOCK_LINGER);
23013167e12cSChuck Lever 		tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF;
2302b0d93ad5SChuck Lever 
2303b0d93ad5SChuck Lever 		xprt_clear_connected(xprt);
2304b0d93ad5SChuck Lever 
2305b0d93ad5SChuck Lever 		/* Reset to new socket */
2306ee0ac0c2SChuck Lever 		transport->sock = sock;
2307ee0ac0c2SChuck Lever 		transport->inet = sk;
2308b0d93ad5SChuck Lever 
2309b0d93ad5SChuck Lever 		write_unlock_bh(&sk->sk_callback_lock);
2310b0d93ad5SChuck Lever 	}
2311b0d93ad5SChuck Lever 
231201d37c42STrond Myklebust 	if (!xprt_bound(xprt))
2313fe19a96bSTrond Myklebust 		goto out;
231401d37c42STrond Myklebust 
2315a564b8f0SMel Gorman 	xs_set_memalloc(xprt);
2316a564b8f0SMel Gorman 
2317ae053551STrond Myklebust 	xs_stream_start_connect(transport);
2318e1806c7bSTrond Myklebust 
2319b0d93ad5SChuck Lever 	/* Tell the socket layer to start connecting... */
23200fdea1e8STrond Myklebust 	set_bit(XPRT_SOCK_CONNECTING, &transport->sock_state);
2321fe19a96bSTrond Myklebust 	ret = kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK);
2322fe19a96bSTrond Myklebust 	switch (ret) {
2323fe19a96bSTrond Myklebust 	case 0:
23244dda9c8aSTrond Myklebust 		xs_set_srcport(transport, sock);
2325e9d47639SGustavo A. R. Silva 		/* fall through */
2326fe19a96bSTrond Myklebust 	case -EINPROGRESS:
2327fe19a96bSTrond Myklebust 		/* SYN_SENT! */
2328fe19a96bSTrond Myklebust 		if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
2329fe19a96bSTrond Myklebust 			xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
23301f4c17a0STrond Myklebust 		break;
23311f4c17a0STrond Myklebust 	case -EADDRNOTAVAIL:
23321f4c17a0STrond Myklebust 		/* Source port number is unavailable. Try a new one! */
23331f4c17a0STrond Myklebust 		transport->srcport = 0;
2334fe19a96bSTrond Myklebust 	}
2335fe19a96bSTrond Myklebust out:
2336fe19a96bSTrond Myklebust 	return ret;
233716be2d20SChuck Lever }
233816be2d20SChuck Lever 
233916be2d20SChuck Lever /**
2340b61d59ffSTrond Myklebust  * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint
2341acf0a39fSChuck Lever  * @work: queued work item
234216be2d20SChuck Lever  *
234316be2d20SChuck Lever  * Invoked by a work queue tasklet.
234416be2d20SChuck Lever  */
2345cdd518d5SPavel Emelyanov static void xs_tcp_setup_socket(struct work_struct *work)
234616be2d20SChuck Lever {
2347cdd518d5SPavel Emelyanov 	struct sock_xprt *transport =
2348cdd518d5SPavel Emelyanov 		container_of(work, struct sock_xprt, connect_worker.work);
234916be2d20SChuck Lever 	struct socket *sock = transport->sock;
2350a9f5f0f7SPavel Emelyanov 	struct rpc_xprt *xprt = &transport->xprt;
2351b61d59ffSTrond Myklebust 	int status = -EIO;
235216be2d20SChuck Lever 
235316be2d20SChuck Lever 	if (!sock) {
2354cdd518d5SPavel Emelyanov 		sock = xs_create_sock(xprt, transport,
23554dda9c8aSTrond Myklebust 				xs_addr(xprt)->sa_family, SOCK_STREAM,
23564dda9c8aSTrond Myklebust 				IPPROTO_TCP, true);
2357b61d59ffSTrond Myklebust 		if (IS_ERR(sock)) {
2358b61d59ffSTrond Myklebust 			status = PTR_ERR(sock);
235916be2d20SChuck Lever 			goto out;
236016be2d20SChuck Lever 		}
23617d1e8255STrond Myklebust 	}
23627d1e8255STrond Myklebust 
2363c740eff8SChuck Lever 	dprintk("RPC:       worker connecting xprt %p via %s to "
2364c740eff8SChuck Lever 				"%s (port %s)\n", xprt,
2365c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_PROTO],
2366c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_ADDR],
2367c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_PORT]);
236816be2d20SChuck Lever 
236916be2d20SChuck Lever 	status = xs_tcp_finish_connecting(xprt, sock);
237040b5ea0cSTrond Myklebust 	trace_rpc_socket_connect(xprt, sock, status);
2371a246b010SChuck Lever 	dprintk("RPC:       %p connect status %d connected %d sock state %d\n",
237246121cf7SChuck Lever 			xprt, -status, xprt_connected(xprt),
237346121cf7SChuck Lever 			sock->sk->sk_state);
2374a246b010SChuck Lever 	switch (status) {
2375f75e6745STrond Myklebust 	default:
2376f75e6745STrond Myklebust 		printk("%s: connect returned unhandled error %d\n",
2377f75e6745STrond Myklebust 			__func__, status);
2378e9d47639SGustavo A. R. Silva 		/* fall through */
2379f75e6745STrond Myklebust 	case -EADDRNOTAVAIL:
2380f75e6745STrond Myklebust 		/* We're probably in TIME_WAIT. Get rid of existing socket,
2381f75e6745STrond Myklebust 		 * and retry
2382f75e6745STrond Myklebust 		 */
2383a519fc7aSTrond Myklebust 		xs_tcp_force_close(xprt);
238488b5ed73STrond Myklebust 		break;
23852a491991STrond Myklebust 	case 0:
2386a246b010SChuck Lever 	case -EINPROGRESS:
2387a246b010SChuck Lever 	case -EALREADY:
2388718ba5b8STrond Myklebust 		xprt_unlock_connect(xprt, transport);
23897d1e8255STrond Myklebust 		return;
23909fcfe0c8STrond Myklebust 	case -EINVAL:
23919fcfe0c8STrond Myklebust 		/* Happens, for instance, if the user specified a link
23929fcfe0c8STrond Myklebust 		 * local IPv6 address without a scope-id.
23939fcfe0c8STrond Myklebust 		 */
23943ed5e2a2STrond Myklebust 	case -ECONNREFUSED:
23953ed5e2a2STrond Myklebust 	case -ECONNRESET:
2396eb5b46faSTrond Myklebust 	case -ENETDOWN:
23973ed5e2a2STrond Myklebust 	case -ENETUNREACH:
23984ba161a7STrond Myklebust 	case -EHOSTUNREACH:
23993913c78cSTrond Myklebust 	case -EADDRINUSE:
24003601c4a9STrond Myklebust 	case -ENOBUFS:
24016ea44adcSNeilBrown 		/*
24026ea44adcSNeilBrown 		 * xs_tcp_force_close() wakes tasks with -EIO.
24036ea44adcSNeilBrown 		 * We need to wake them first to ensure the
24046ea44adcSNeilBrown 		 * correct error code.
24056ea44adcSNeilBrown 		 */
24066ea44adcSNeilBrown 		xprt_wake_pending_tasks(xprt, status);
24074efdd92cSTrond Myklebust 		xs_tcp_force_close(xprt);
24089fcfe0c8STrond Myklebust 		goto out;
24098a2cec29STrond Myklebust 	}
24102a491991STrond Myklebust 	status = -EAGAIN;
2411a246b010SChuck Lever out:
24122226feb6SChuck Lever 	xprt_clear_connecting(xprt);
2413cf76785dSTrond Myklebust 	xprt_unlock_connect(xprt, transport);
24147d1e8255STrond Myklebust 	xprt_wake_pending_tasks(xprt, status);
2415a246b010SChuck Lever }
2416a246b010SChuck Lever 
241768e220bdSChuck Lever /**
24189903cd1cSChuck Lever  * xs_connect - connect a socket to a remote endpoint
24191b092092STrond Myklebust  * @xprt: pointer to transport structure
24209903cd1cSChuck Lever  * @task: address of RPC task that manages state of connect request
24219903cd1cSChuck Lever  *
24229903cd1cSChuck Lever  * TCP: If the remote end dropped the connection, delay reconnecting.
242303bf4b70SChuck Lever  *
242403bf4b70SChuck Lever  * UDP socket connects are synchronous, but we use a work queue anyway
242503bf4b70SChuck Lever  * to guarantee that even unprivileged user processes can set up a
242603bf4b70SChuck Lever  * socket on a privileged port.
242703bf4b70SChuck Lever  *
242803bf4b70SChuck Lever  * If a UDP socket connect fails, the delay behavior here prevents
242903bf4b70SChuck Lever  * retry floods (hard mounts).
24309903cd1cSChuck Lever  */
24311b092092STrond Myklebust static void xs_connect(struct rpc_xprt *xprt, struct rpc_task *task)
2432a246b010SChuck Lever {
2433ee0ac0c2SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
243402910177STrond Myklebust 	unsigned long delay = 0;
2435a246b010SChuck Lever 
2436718ba5b8STrond Myklebust 	WARN_ON_ONCE(!xprt_lock_connect(xprt, task, transport));
2437718ba5b8STrond Myklebust 
243899b1a4c3STrond Myklebust 	if (transport->sock != NULL) {
243946121cf7SChuck Lever 		dprintk("RPC:       xs_connect delayed xprt %p for %lu "
244046121cf7SChuck Lever 				"seconds\n",
244103bf4b70SChuck Lever 				xprt, xprt->reestablish_timeout / HZ);
244299b1a4c3STrond Myklebust 
244399b1a4c3STrond Myklebust 		/* Start by resetting any existing state */
244499b1a4c3STrond Myklebust 		xs_reset_transport(transport);
244599b1a4c3STrond Myklebust 
2446675dd90aSChuck Lever 		delay = xprt_reconnect_delay(xprt);
2447675dd90aSChuck Lever 		xprt_reconnect_backoff(xprt, XS_TCP_INIT_REEST_TO);
244802910177STrond Myklebust 
244902910177STrond Myklebust 	} else
24509903cd1cSChuck Lever 		dprintk("RPC:       xs_connect scheduled xprt %p\n", xprt);
245102910177STrond Myklebust 
245240a5f1b1STrond Myklebust 	queue_delayed_work(xprtiod_workqueue,
245302910177STrond Myklebust 			&transport->connect_worker,
245402910177STrond Myklebust 			delay);
2455a246b010SChuck Lever }
2456a246b010SChuck Lever 
24574f8943f8STrond Myklebust static void xs_wake_disconnect(struct sock_xprt *transport)
24584f8943f8STrond Myklebust {
24594f8943f8STrond Myklebust 	if (test_and_clear_bit(XPRT_SOCK_WAKE_DISCONNECT, &transport->sock_state))
24604f8943f8STrond Myklebust 		xs_tcp_force_close(&transport->xprt);
24614f8943f8STrond Myklebust }
24624f8943f8STrond Myklebust 
24634f8943f8STrond Myklebust static void xs_wake_write(struct sock_xprt *transport)
24644f8943f8STrond Myklebust {
24654f8943f8STrond Myklebust 	if (test_and_clear_bit(XPRT_SOCK_WAKE_WRITE, &transport->sock_state))
24664f8943f8STrond Myklebust 		xprt_write_space(&transport->xprt);
24674f8943f8STrond Myklebust }
24684f8943f8STrond Myklebust 
24694f8943f8STrond Myklebust static void xs_wake_error(struct sock_xprt *transport)
24704f8943f8STrond Myklebust {
24714f8943f8STrond Myklebust 	int sockerr;
24724f8943f8STrond Myklebust 	int sockerr_len = sizeof(sockerr);
24734f8943f8STrond Myklebust 
24744f8943f8STrond Myklebust 	if (!test_bit(XPRT_SOCK_WAKE_ERROR, &transport->sock_state))
24754f8943f8STrond Myklebust 		return;
24764f8943f8STrond Myklebust 	mutex_lock(&transport->recv_mutex);
24774f8943f8STrond Myklebust 	if (transport->sock == NULL)
24784f8943f8STrond Myklebust 		goto out;
24794f8943f8STrond Myklebust 	if (!test_and_clear_bit(XPRT_SOCK_WAKE_ERROR, &transport->sock_state))
24804f8943f8STrond Myklebust 		goto out;
24814f8943f8STrond Myklebust 	if (kernel_getsockopt(transport->sock, SOL_SOCKET, SO_ERROR,
24824f8943f8STrond Myklebust 				(char *)&sockerr, &sockerr_len) != 0)
24834f8943f8STrond Myklebust 		goto out;
24844f8943f8STrond Myklebust 	if (sockerr < 0)
24854f8943f8STrond Myklebust 		xprt_wake_pending_tasks(&transport->xprt, sockerr);
24864f8943f8STrond Myklebust out:
24874f8943f8STrond Myklebust 	mutex_unlock(&transport->recv_mutex);
24884f8943f8STrond Myklebust }
24894f8943f8STrond Myklebust 
24904f8943f8STrond Myklebust static void xs_wake_pending(struct sock_xprt *transport)
24914f8943f8STrond Myklebust {
24924f8943f8STrond Myklebust 	if (test_and_clear_bit(XPRT_SOCK_WAKE_PENDING, &transport->sock_state))
24934f8943f8STrond Myklebust 		xprt_wake_pending_tasks(&transport->xprt, -EAGAIN);
24944f8943f8STrond Myklebust }
24954f8943f8STrond Myklebust 
24964f8943f8STrond Myklebust static void xs_error_handle(struct work_struct *work)
24974f8943f8STrond Myklebust {
24984f8943f8STrond Myklebust 	struct sock_xprt *transport = container_of(work,
24994f8943f8STrond Myklebust 			struct sock_xprt, error_worker);
25004f8943f8STrond Myklebust 
25014f8943f8STrond Myklebust 	xs_wake_disconnect(transport);
25024f8943f8STrond Myklebust 	xs_wake_write(transport);
25034f8943f8STrond Myklebust 	xs_wake_error(transport);
25044f8943f8STrond Myklebust 	xs_wake_pending(transport);
25054f8943f8STrond Myklebust }
25064f8943f8STrond Myklebust 
2507262ca07dSChuck Lever /**
2508176e21eeSChuck Lever  * xs_local_print_stats - display AF_LOCAL socket-specifc stats
2509176e21eeSChuck Lever  * @xprt: rpc_xprt struct containing statistics
2510176e21eeSChuck Lever  * @seq: output file
2511176e21eeSChuck Lever  *
2512176e21eeSChuck Lever  */
2513176e21eeSChuck Lever static void xs_local_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2514176e21eeSChuck Lever {
2515176e21eeSChuck Lever 	long idle_time = 0;
2516176e21eeSChuck Lever 
2517176e21eeSChuck Lever 	if (xprt_connected(xprt))
2518176e21eeSChuck Lever 		idle_time = (long)(jiffies - xprt->last_used) / HZ;
2519176e21eeSChuck Lever 
2520176e21eeSChuck Lever 	seq_printf(seq, "\txprt:\tlocal %lu %lu %lu %ld %lu %lu %lu "
252115a45206SAndy Adamson 			"%llu %llu %lu %llu %llu\n",
2522176e21eeSChuck Lever 			xprt->stat.bind_count,
2523176e21eeSChuck Lever 			xprt->stat.connect_count,
25248440a886SChuck Lever 			xprt->stat.connect_time / HZ,
2525176e21eeSChuck Lever 			idle_time,
2526176e21eeSChuck Lever 			xprt->stat.sends,
2527176e21eeSChuck Lever 			xprt->stat.recvs,
2528176e21eeSChuck Lever 			xprt->stat.bad_xids,
2529176e21eeSChuck Lever 			xprt->stat.req_u,
253015a45206SAndy Adamson 			xprt->stat.bklog_u,
253115a45206SAndy Adamson 			xprt->stat.max_slots,
253215a45206SAndy Adamson 			xprt->stat.sending_u,
253315a45206SAndy Adamson 			xprt->stat.pending_u);
2534176e21eeSChuck Lever }
2535176e21eeSChuck Lever 
2536176e21eeSChuck Lever /**
2537262ca07dSChuck Lever  * xs_udp_print_stats - display UDP socket-specifc stats
2538262ca07dSChuck Lever  * @xprt: rpc_xprt struct containing statistics
2539262ca07dSChuck Lever  * @seq: output file
2540262ca07dSChuck Lever  *
2541262ca07dSChuck Lever  */
2542262ca07dSChuck Lever static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2543262ca07dSChuck Lever {
2544c8475461SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2545c8475461SChuck Lever 
254615a45206SAndy Adamson 	seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %llu %llu "
254715a45206SAndy Adamson 			"%lu %llu %llu\n",
2548fbfffbd5SChuck Lever 			transport->srcport,
2549262ca07dSChuck Lever 			xprt->stat.bind_count,
2550262ca07dSChuck Lever 			xprt->stat.sends,
2551262ca07dSChuck Lever 			xprt->stat.recvs,
2552262ca07dSChuck Lever 			xprt->stat.bad_xids,
2553262ca07dSChuck Lever 			xprt->stat.req_u,
255415a45206SAndy Adamson 			xprt->stat.bklog_u,
255515a45206SAndy Adamson 			xprt->stat.max_slots,
255615a45206SAndy Adamson 			xprt->stat.sending_u,
255715a45206SAndy Adamson 			xprt->stat.pending_u);
2558262ca07dSChuck Lever }
2559262ca07dSChuck Lever 
2560262ca07dSChuck Lever /**
2561262ca07dSChuck Lever  * xs_tcp_print_stats - display TCP socket-specifc stats
2562262ca07dSChuck Lever  * @xprt: rpc_xprt struct containing statistics
2563262ca07dSChuck Lever  * @seq: output file
2564262ca07dSChuck Lever  *
2565262ca07dSChuck Lever  */
2566262ca07dSChuck Lever static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2567262ca07dSChuck Lever {
2568c8475461SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2569262ca07dSChuck Lever 	long idle_time = 0;
2570262ca07dSChuck Lever 
2571262ca07dSChuck Lever 	if (xprt_connected(xprt))
2572262ca07dSChuck Lever 		idle_time = (long)(jiffies - xprt->last_used) / HZ;
2573262ca07dSChuck Lever 
257415a45206SAndy Adamson 	seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu "
257515a45206SAndy Adamson 			"%llu %llu %lu %llu %llu\n",
2576fbfffbd5SChuck Lever 			transport->srcport,
2577262ca07dSChuck Lever 			xprt->stat.bind_count,
2578262ca07dSChuck Lever 			xprt->stat.connect_count,
25798440a886SChuck Lever 			xprt->stat.connect_time / HZ,
2580262ca07dSChuck Lever 			idle_time,
2581262ca07dSChuck Lever 			xprt->stat.sends,
2582262ca07dSChuck Lever 			xprt->stat.recvs,
2583262ca07dSChuck Lever 			xprt->stat.bad_xids,
2584262ca07dSChuck Lever 			xprt->stat.req_u,
258515a45206SAndy Adamson 			xprt->stat.bklog_u,
258615a45206SAndy Adamson 			xprt->stat.max_slots,
258715a45206SAndy Adamson 			xprt->stat.sending_u,
258815a45206SAndy Adamson 			xprt->stat.pending_u);
2589262ca07dSChuck Lever }
2590262ca07dSChuck Lever 
25914cfc7e60SRahul Iyer /*
25924cfc7e60SRahul Iyer  * Allocate a bunch of pages for a scratch buffer for the rpc code. The reason
25934cfc7e60SRahul Iyer  * we allocate pages instead doing a kmalloc like rpc_malloc is because we want
25944cfc7e60SRahul Iyer  * to use the server side send routines.
25954cfc7e60SRahul Iyer  */
25965fe6eaa1SChuck Lever static int bc_malloc(struct rpc_task *task)
25974cfc7e60SRahul Iyer {
25985fe6eaa1SChuck Lever 	struct rpc_rqst *rqst = task->tk_rqstp;
25995fe6eaa1SChuck Lever 	size_t size = rqst->rq_callsize;
26004cfc7e60SRahul Iyer 	struct page *page;
26014cfc7e60SRahul Iyer 	struct rpc_buffer *buf;
26024cfc7e60SRahul Iyer 
26035fe6eaa1SChuck Lever 	if (size > PAGE_SIZE - sizeof(struct rpc_buffer)) {
26045fe6eaa1SChuck Lever 		WARN_ONCE(1, "xprtsock: large bc buffer request (size %zu)\n",
26055fe6eaa1SChuck Lever 			  size);
26065fe6eaa1SChuck Lever 		return -EINVAL;
26075fe6eaa1SChuck Lever 	}
26084cfc7e60SRahul Iyer 
2609b8a13d03SWeston Andros Adamson 	page = alloc_page(GFP_KERNEL);
26104cfc7e60SRahul Iyer 	if (!page)
26115fe6eaa1SChuck Lever 		return -ENOMEM;
26124cfc7e60SRahul Iyer 
26134cfc7e60SRahul Iyer 	buf = page_address(page);
26144cfc7e60SRahul Iyer 	buf->len = PAGE_SIZE;
26154cfc7e60SRahul Iyer 
26165fe6eaa1SChuck Lever 	rqst->rq_buffer = buf->data;
261718e601d6SJeff Layton 	rqst->rq_rbuffer = (char *)rqst->rq_buffer + rqst->rq_callsize;
26185fe6eaa1SChuck Lever 	return 0;
26194cfc7e60SRahul Iyer }
26204cfc7e60SRahul Iyer 
26214cfc7e60SRahul Iyer /*
26224cfc7e60SRahul Iyer  * Free the space allocated in the bc_alloc routine
26234cfc7e60SRahul Iyer  */
26243435c74aSChuck Lever static void bc_free(struct rpc_task *task)
26254cfc7e60SRahul Iyer {
26263435c74aSChuck Lever 	void *buffer = task->tk_rqstp->rq_buffer;
26274cfc7e60SRahul Iyer 	struct rpc_buffer *buf;
26284cfc7e60SRahul Iyer 
26294cfc7e60SRahul Iyer 	buf = container_of(buffer, struct rpc_buffer, data);
26304cfc7e60SRahul Iyer 	free_page((unsigned long)buf);
26314cfc7e60SRahul Iyer }
26324cfc7e60SRahul Iyer 
26334cfc7e60SRahul Iyer /*
26344cfc7e60SRahul Iyer  * Use the svc_sock to send the callback. Must be called with svsk->sk_mutex
26354cfc7e60SRahul Iyer  * held. Borrows heavily from svc_tcp_sendto and xs_tcp_send_request.
26364cfc7e60SRahul Iyer  */
26374cfc7e60SRahul Iyer static int bc_sendto(struct rpc_rqst *req)
26384cfc7e60SRahul Iyer {
26394cfc7e60SRahul Iyer 	int len;
26404cfc7e60SRahul Iyer 	struct xdr_buf *xbufp = &req->rq_snd_buf;
26414cfc7e60SRahul Iyer 	struct sock_xprt *transport =
2642067fb11bSChuck Lever 			container_of(req->rq_xprt, struct sock_xprt, xprt);
26434cfc7e60SRahul Iyer 	unsigned long headoff;
26444cfc7e60SRahul Iyer 	unsigned long tailoff;
2645067fb11bSChuck Lever 	struct page *tailpage;
2646067fb11bSChuck Lever 	struct msghdr msg = {
2647067fb11bSChuck Lever 		.msg_flags	= MSG_MORE
2648067fb11bSChuck Lever 	};
2649067fb11bSChuck Lever 	rpc_fraghdr marker = cpu_to_be32(RPC_LAST_STREAM_FRAGMENT |
2650067fb11bSChuck Lever 					 (u32)xbufp->len);
2651067fb11bSChuck Lever 	struct kvec iov = {
2652067fb11bSChuck Lever 		.iov_base	= &marker,
2653067fb11bSChuck Lever 		.iov_len	= sizeof(marker),
2654067fb11bSChuck Lever 	};
26554cfc7e60SRahul Iyer 
2656067fb11bSChuck Lever 	len = kernel_sendmsg(transport->sock, &msg, &iov, 1, iov.iov_len);
2657067fb11bSChuck Lever 	if (len != iov.iov_len)
2658067fb11bSChuck Lever 		return -EAGAIN;
26594cfc7e60SRahul Iyer 
2660067fb11bSChuck Lever 	tailpage = NULL;
2661067fb11bSChuck Lever 	if (xbufp->tail[0].iov_len)
2662067fb11bSChuck Lever 		tailpage = virt_to_page(xbufp->tail[0].iov_base);
26634cfc7e60SRahul Iyer 	tailoff = (unsigned long)xbufp->tail[0].iov_base & ~PAGE_MASK;
26644cfc7e60SRahul Iyer 	headoff = (unsigned long)xbufp->head[0].iov_base & ~PAGE_MASK;
2665067fb11bSChuck Lever 	len = svc_send_common(transport->sock, xbufp,
26664cfc7e60SRahul Iyer 			      virt_to_page(xbufp->head[0].iov_base), headoff,
2667067fb11bSChuck Lever 			      tailpage, tailoff);
2668067fb11bSChuck Lever 	if (len != xbufp->len)
2669067fb11bSChuck Lever 		return -EAGAIN;
26704cfc7e60SRahul Iyer 	return len;
26714cfc7e60SRahul Iyer }
26724cfc7e60SRahul Iyer 
26734cfc7e60SRahul Iyer /*
26744cfc7e60SRahul Iyer  * The send routine. Borrows from svc_send
26754cfc7e60SRahul Iyer  */
2676adfa7144STrond Myklebust static int bc_send_request(struct rpc_rqst *req)
26774cfc7e60SRahul Iyer {
26784cfc7e60SRahul Iyer 	struct svc_xprt	*xprt;
26797fc56136SAndrzej Hajda 	int len;
26804cfc7e60SRahul Iyer 
26814cfc7e60SRahul Iyer 	dprintk("sending request with xid: %08x\n", ntohl(req->rq_xid));
26824cfc7e60SRahul Iyer 	/*
26834cfc7e60SRahul Iyer 	 * Get the server socket associated with this callback xprt
26844cfc7e60SRahul Iyer 	 */
26854cfc7e60SRahul Iyer 	xprt = req->rq_xprt->bc_xprt;
26864cfc7e60SRahul Iyer 
26874cfc7e60SRahul Iyer 	/*
26884cfc7e60SRahul Iyer 	 * Grab the mutex to serialize data as the connection is shared
26894cfc7e60SRahul Iyer 	 * with the fore channel
26904cfc7e60SRahul Iyer 	 */
2691c544577dSTrond Myklebust 	mutex_lock(&xprt->xpt_mutex);
26924cfc7e60SRahul Iyer 	if (test_bit(XPT_DEAD, &xprt->xpt_flags))
26934cfc7e60SRahul Iyer 		len = -ENOTCONN;
26944cfc7e60SRahul Iyer 	else
26954cfc7e60SRahul Iyer 		len = bc_sendto(req);
26964cfc7e60SRahul Iyer 	mutex_unlock(&xprt->xpt_mutex);
26974cfc7e60SRahul Iyer 
26984cfc7e60SRahul Iyer 	if (len > 0)
26994cfc7e60SRahul Iyer 		len = 0;
27004cfc7e60SRahul Iyer 
27014cfc7e60SRahul Iyer 	return len;
27024cfc7e60SRahul Iyer }
27034cfc7e60SRahul Iyer 
27044cfc7e60SRahul Iyer /*
27054cfc7e60SRahul Iyer  * The close routine. Since this is client initiated, we do nothing
27064cfc7e60SRahul Iyer  */
27074cfc7e60SRahul Iyer 
27084cfc7e60SRahul Iyer static void bc_close(struct rpc_xprt *xprt)
27094cfc7e60SRahul Iyer {
27104cfc7e60SRahul Iyer }
27114cfc7e60SRahul Iyer 
27124cfc7e60SRahul Iyer /*
27134cfc7e60SRahul Iyer  * The xprt destroy routine. Again, because this connection is client
27144cfc7e60SRahul Iyer  * initiated, we do nothing
27154cfc7e60SRahul Iyer  */
27164cfc7e60SRahul Iyer 
27174cfc7e60SRahul Iyer static void bc_destroy(struct rpc_xprt *xprt)
27184cfc7e60SRahul Iyer {
271947f72efaSKinglong Mee 	dprintk("RPC:       bc_destroy xprt %p\n", xprt);
272047f72efaSKinglong Mee 
272147f72efaSKinglong Mee 	xs_xprt_free(xprt);
272247f72efaSKinglong Mee 	module_put(THIS_MODULE);
27234cfc7e60SRahul Iyer }
27244cfc7e60SRahul Iyer 
2725d31ae254SChuck Lever static const struct rpc_xprt_ops xs_local_ops = {
2726176e21eeSChuck Lever 	.reserve_xprt		= xprt_reserve_xprt,
27274cd34e7cSTrond Myklebust 	.release_xprt		= xprt_release_xprt,
2728f39c1bfbSTrond Myklebust 	.alloc_slot		= xprt_alloc_slot,
2729a9cde23aSChuck Lever 	.free_slot		= xprt_free_slot,
2730176e21eeSChuck Lever 	.rpcbind		= xs_local_rpcbind,
2731176e21eeSChuck Lever 	.set_port		= xs_local_set_port,
2732dc107402SJ. Bruce Fields 	.connect		= xs_local_connect,
2733176e21eeSChuck Lever 	.buf_alloc		= rpc_malloc,
2734176e21eeSChuck Lever 	.buf_free		= rpc_free,
2735550aebfeSTrond Myklebust 	.prepare_request	= xs_stream_prepare_request,
2736176e21eeSChuck Lever 	.send_request		= xs_local_send_request,
27378ba6a92dSTrond Myklebust 	.wait_for_reply_request	= xprt_wait_for_reply_request_def,
2738176e21eeSChuck Lever 	.close			= xs_close,
2739a1311d87STrond Myklebust 	.destroy		= xs_destroy,
2740176e21eeSChuck Lever 	.print_stats		= xs_local_print_stats,
2741d67fa4d8SJeff Layton 	.enable_swap		= xs_enable_swap,
2742d67fa4d8SJeff Layton 	.disable_swap		= xs_disable_swap,
2743176e21eeSChuck Lever };
2744176e21eeSChuck Lever 
2745d31ae254SChuck Lever static const struct rpc_xprt_ops xs_udp_ops = {
274643118c29SChuck Lever 	.set_buffer_size	= xs_udp_set_buffer_size,
274712a80469SChuck Lever 	.reserve_xprt		= xprt_reserve_xprt_cong,
274849e9a890SChuck Lever 	.release_xprt		= xprt_release_xprt_cong,
2749f39c1bfbSTrond Myklebust 	.alloc_slot		= xprt_alloc_slot,
2750a9cde23aSChuck Lever 	.free_slot		= xprt_free_slot,
275145160d62SChuck Lever 	.rpcbind		= rpcb_getport_async,
275292200412SChuck Lever 	.set_port		= xs_set_port,
27539903cd1cSChuck Lever 	.connect		= xs_connect,
275402107148SChuck Lever 	.buf_alloc		= rpc_malloc,
275502107148SChuck Lever 	.buf_free		= rpc_free,
2756262965f5SChuck Lever 	.send_request		= xs_udp_send_request,
27578ba6a92dSTrond Myklebust 	.wait_for_reply_request	= xprt_wait_for_reply_request_rtt,
275846c0ee8bSChuck Lever 	.timer			= xs_udp_timer,
2759a58dd398SChuck Lever 	.release_request	= xprt_release_rqst_cong,
2760262965f5SChuck Lever 	.close			= xs_close,
2761262965f5SChuck Lever 	.destroy		= xs_destroy,
2762262ca07dSChuck Lever 	.print_stats		= xs_udp_print_stats,
2763d67fa4d8SJeff Layton 	.enable_swap		= xs_enable_swap,
2764d67fa4d8SJeff Layton 	.disable_swap		= xs_disable_swap,
27654a068258SChuck Lever 	.inject_disconnect	= xs_inject_disconnect,
2766262965f5SChuck Lever };
2767262965f5SChuck Lever 
2768d31ae254SChuck Lever static const struct rpc_xprt_ops xs_tcp_ops = {
276912a80469SChuck Lever 	.reserve_xprt		= xprt_reserve_xprt,
27704cd34e7cSTrond Myklebust 	.release_xprt		= xprt_release_xprt,
277136bd7de9STrond Myklebust 	.alloc_slot		= xprt_alloc_slot,
2772a9cde23aSChuck Lever 	.free_slot		= xprt_free_slot,
277345160d62SChuck Lever 	.rpcbind		= rpcb_getport_async,
277492200412SChuck Lever 	.set_port		= xs_set_port,
27750b9e7943STrond Myklebust 	.connect		= xs_connect,
277602107148SChuck Lever 	.buf_alloc		= rpc_malloc,
277702107148SChuck Lever 	.buf_free		= rpc_free,
2778277e4ab7STrond Myklebust 	.prepare_request	= xs_stream_prepare_request,
2779262965f5SChuck Lever 	.send_request		= xs_tcp_send_request,
27808ba6a92dSTrond Myklebust 	.wait_for_reply_request	= xprt_wait_for_reply_request_def,
2781c627d31bSTrond Myklebust 	.close			= xs_tcp_shutdown,
27829903cd1cSChuck Lever 	.destroy		= xs_destroy,
27837196dbb0STrond Myklebust 	.set_connect_timeout	= xs_tcp_set_connect_timeout,
2784262ca07dSChuck Lever 	.print_stats		= xs_tcp_print_stats,
2785d67fa4d8SJeff Layton 	.enable_swap		= xs_enable_swap,
2786d67fa4d8SJeff Layton 	.disable_swap		= xs_disable_swap,
27874a068258SChuck Lever 	.inject_disconnect	= xs_inject_disconnect,
278842e5c3e2SChuck Lever #ifdef CONFIG_SUNRPC_BACKCHANNEL
278942e5c3e2SChuck Lever 	.bc_setup		= xprt_setup_bc,
27906b26cc8cSChuck Lever 	.bc_maxpayload		= xs_tcp_bc_maxpayload,
2791*7402a4feSTrond Myklebust 	.bc_num_slots		= xprt_bc_max_slots,
279242e5c3e2SChuck Lever 	.bc_free_rqst		= xprt_free_bc_rqst,
279342e5c3e2SChuck Lever 	.bc_destroy		= xprt_destroy_bc,
279442e5c3e2SChuck Lever #endif
2795a246b010SChuck Lever };
2796a246b010SChuck Lever 
27974cfc7e60SRahul Iyer /*
27984cfc7e60SRahul Iyer  * The rpc_xprt_ops for the server backchannel
27994cfc7e60SRahul Iyer  */
28004cfc7e60SRahul Iyer 
2801d31ae254SChuck Lever static const struct rpc_xprt_ops bc_tcp_ops = {
28024cfc7e60SRahul Iyer 	.reserve_xprt		= xprt_reserve_xprt,
28034cfc7e60SRahul Iyer 	.release_xprt		= xprt_release_xprt,
280484e28a30SBryan Schumaker 	.alloc_slot		= xprt_alloc_slot,
2805a9cde23aSChuck Lever 	.free_slot		= xprt_free_slot,
28064cfc7e60SRahul Iyer 	.buf_alloc		= bc_malloc,
28074cfc7e60SRahul Iyer 	.buf_free		= bc_free,
28084cfc7e60SRahul Iyer 	.send_request		= bc_send_request,
28098ba6a92dSTrond Myklebust 	.wait_for_reply_request	= xprt_wait_for_reply_request_def,
28104cfc7e60SRahul Iyer 	.close			= bc_close,
28114cfc7e60SRahul Iyer 	.destroy		= bc_destroy,
28124cfc7e60SRahul Iyer 	.print_stats		= xs_tcp_print_stats,
2813d67fa4d8SJeff Layton 	.enable_swap		= xs_enable_swap,
2814d67fa4d8SJeff Layton 	.disable_swap		= xs_disable_swap,
28154a068258SChuck Lever 	.inject_disconnect	= xs_inject_disconnect,
28164cfc7e60SRahul Iyer };
28174cfc7e60SRahul Iyer 
281892476850SChuck Lever static int xs_init_anyaddr(const int family, struct sockaddr *sap)
281992476850SChuck Lever {
282092476850SChuck Lever 	static const struct sockaddr_in sin = {
282192476850SChuck Lever 		.sin_family		= AF_INET,
282292476850SChuck Lever 		.sin_addr.s_addr	= htonl(INADDR_ANY),
282392476850SChuck Lever 	};
282492476850SChuck Lever 	static const struct sockaddr_in6 sin6 = {
282592476850SChuck Lever 		.sin6_family		= AF_INET6,
282692476850SChuck Lever 		.sin6_addr		= IN6ADDR_ANY_INIT,
282792476850SChuck Lever 	};
282892476850SChuck Lever 
282992476850SChuck Lever 	switch (family) {
2830176e21eeSChuck Lever 	case AF_LOCAL:
2831176e21eeSChuck Lever 		break;
283292476850SChuck Lever 	case AF_INET:
283392476850SChuck Lever 		memcpy(sap, &sin, sizeof(sin));
283492476850SChuck Lever 		break;
283592476850SChuck Lever 	case AF_INET6:
283692476850SChuck Lever 		memcpy(sap, &sin6, sizeof(sin6));
283792476850SChuck Lever 		break;
283892476850SChuck Lever 	default:
283992476850SChuck Lever 		dprintk("RPC:       %s: Bad address family\n", __func__);
284092476850SChuck Lever 		return -EAFNOSUPPORT;
284192476850SChuck Lever 	}
284292476850SChuck Lever 	return 0;
284392476850SChuck Lever }
284492476850SChuck Lever 
28453c341b0bS\"Talpey, Thomas\ static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args,
2846d9ba131dSTrond Myklebust 				      unsigned int slot_table_size,
2847d9ba131dSTrond Myklebust 				      unsigned int max_slot_table_size)
2848c8541ecdSChuck Lever {
2849c8541ecdSChuck Lever 	struct rpc_xprt *xprt;
2850ffc2e518SChuck Lever 	struct sock_xprt *new;
2851c8541ecdSChuck Lever 
285296802a09SFrank van Maarseveen 	if (args->addrlen > sizeof(xprt->addr)) {
2853c8541ecdSChuck Lever 		dprintk("RPC:       xs_setup_xprt: address too large\n");
2854c8541ecdSChuck Lever 		return ERR_PTR(-EBADF);
2855c8541ecdSChuck Lever 	}
2856c8541ecdSChuck Lever 
2857d9ba131dSTrond Myklebust 	xprt = xprt_alloc(args->net, sizeof(*new), slot_table_size,
2858d9ba131dSTrond Myklebust 			max_slot_table_size);
2859bd1722d4SPavel Emelyanov 	if (xprt == NULL) {
286046121cf7SChuck Lever 		dprintk("RPC:       xs_setup_xprt: couldn't allocate "
286146121cf7SChuck Lever 				"rpc_xprt\n");
2862c8541ecdSChuck Lever 		return ERR_PTR(-ENOMEM);
2863c8541ecdSChuck Lever 	}
2864c8541ecdSChuck Lever 
2865bd1722d4SPavel Emelyanov 	new = container_of(xprt, struct sock_xprt, xprt);
2866edc1b01cSTrond Myklebust 	mutex_init(&new->recv_mutex);
286796802a09SFrank van Maarseveen 	memcpy(&xprt->addr, args->dstaddr, args->addrlen);
286896802a09SFrank van Maarseveen 	xprt->addrlen = args->addrlen;
2869d3bc9a1dSFrank van Maarseveen 	if (args->srcaddr)
2870fbfffbd5SChuck Lever 		memcpy(&new->srcaddr, args->srcaddr, args->addrlen);
287192476850SChuck Lever 	else {
287292476850SChuck Lever 		int err;
287392476850SChuck Lever 		err = xs_init_anyaddr(args->dstaddr->sa_family,
287492476850SChuck Lever 					(struct sockaddr *)&new->srcaddr);
28752aa13531SStanislav Kinsbursky 		if (err != 0) {
28762aa13531SStanislav Kinsbursky 			xprt_free(xprt);
287792476850SChuck Lever 			return ERR_PTR(err);
287892476850SChuck Lever 		}
28792aa13531SStanislav Kinsbursky 	}
2880c8541ecdSChuck Lever 
2881c8541ecdSChuck Lever 	return xprt;
2882c8541ecdSChuck Lever }
2883c8541ecdSChuck Lever 
2884176e21eeSChuck Lever static const struct rpc_timeout xs_local_default_timeout = {
2885176e21eeSChuck Lever 	.to_initval = 10 * HZ,
2886176e21eeSChuck Lever 	.to_maxval = 10 * HZ,
2887176e21eeSChuck Lever 	.to_retries = 2,
2888176e21eeSChuck Lever };
2889176e21eeSChuck Lever 
2890176e21eeSChuck Lever /**
2891176e21eeSChuck Lever  * xs_setup_local - Set up transport to use an AF_LOCAL socket
2892176e21eeSChuck Lever  * @args: rpc transport creation arguments
2893176e21eeSChuck Lever  *
2894176e21eeSChuck Lever  * AF_LOCAL is a "tpi_cots_ord" transport, just like TCP
2895176e21eeSChuck Lever  */
2896176e21eeSChuck Lever static struct rpc_xprt *xs_setup_local(struct xprt_create *args)
2897176e21eeSChuck Lever {
2898176e21eeSChuck Lever 	struct sockaddr_un *sun = (struct sockaddr_un *)args->dstaddr;
2899176e21eeSChuck Lever 	struct sock_xprt *transport;
2900176e21eeSChuck Lever 	struct rpc_xprt *xprt;
2901176e21eeSChuck Lever 	struct rpc_xprt *ret;
2902176e21eeSChuck Lever 
2903d9ba131dSTrond Myklebust 	xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
2904d9ba131dSTrond Myklebust 			xprt_max_tcp_slot_table_entries);
2905176e21eeSChuck Lever 	if (IS_ERR(xprt))
2906176e21eeSChuck Lever 		return xprt;
2907176e21eeSChuck Lever 	transport = container_of(xprt, struct sock_xprt, xprt);
2908176e21eeSChuck Lever 
2909176e21eeSChuck Lever 	xprt->prot = 0;
2910176e21eeSChuck Lever 	xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2911176e21eeSChuck Lever 
2912176e21eeSChuck Lever 	xprt->bind_timeout = XS_BIND_TO;
2913176e21eeSChuck Lever 	xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2914176e21eeSChuck Lever 	xprt->idle_timeout = XS_IDLE_DISC_TO;
2915176e21eeSChuck Lever 
2916176e21eeSChuck Lever 	xprt->ops = &xs_local_ops;
2917176e21eeSChuck Lever 	xprt->timeout = &xs_local_default_timeout;
2918176e21eeSChuck Lever 
2919550aebfeSTrond Myklebust 	INIT_WORK(&transport->recv_worker, xs_stream_data_receive_workfn);
29204f8943f8STrond Myklebust 	INIT_WORK(&transport->error_worker, xs_error_handle);
2921550aebfeSTrond Myklebust 	INIT_DELAYED_WORK(&transport->connect_worker, xs_dummy_setup_socket);
292293dc41bdSNeilBrown 
2923176e21eeSChuck Lever 	switch (sun->sun_family) {
2924176e21eeSChuck Lever 	case AF_LOCAL:
2925176e21eeSChuck Lever 		if (sun->sun_path[0] != '/') {
2926176e21eeSChuck Lever 			dprintk("RPC:       bad AF_LOCAL address: %s\n",
2927176e21eeSChuck Lever 					sun->sun_path);
2928176e21eeSChuck Lever 			ret = ERR_PTR(-EINVAL);
2929176e21eeSChuck Lever 			goto out_err;
2930176e21eeSChuck Lever 		}
2931176e21eeSChuck Lever 		xprt_set_bound(xprt);
2932176e21eeSChuck Lever 		xs_format_peer_addresses(xprt, "local", RPCBIND_NETID_LOCAL);
29337073ea87SJ. Bruce Fields 		ret = ERR_PTR(xs_local_setup_socket(transport));
29347073ea87SJ. Bruce Fields 		if (ret)
29357073ea87SJ. Bruce Fields 			goto out_err;
2936176e21eeSChuck Lever 		break;
2937176e21eeSChuck Lever 	default:
2938176e21eeSChuck Lever 		ret = ERR_PTR(-EAFNOSUPPORT);
2939176e21eeSChuck Lever 		goto out_err;
2940176e21eeSChuck Lever 	}
2941176e21eeSChuck Lever 
2942176e21eeSChuck Lever 	dprintk("RPC:       set up xprt to %s via AF_LOCAL\n",
2943176e21eeSChuck Lever 			xprt->address_strings[RPC_DISPLAY_ADDR]);
2944176e21eeSChuck Lever 
2945176e21eeSChuck Lever 	if (try_module_get(THIS_MODULE))
2946176e21eeSChuck Lever 		return xprt;
2947176e21eeSChuck Lever 	ret = ERR_PTR(-EINVAL);
2948176e21eeSChuck Lever out_err:
2949315f3812SKinglong Mee 	xs_xprt_free(xprt);
2950176e21eeSChuck Lever 	return ret;
2951176e21eeSChuck Lever }
2952176e21eeSChuck Lever 
29532881ae74STrond Myklebust static const struct rpc_timeout xs_udp_default_timeout = {
29542881ae74STrond Myklebust 	.to_initval = 5 * HZ,
29552881ae74STrond Myklebust 	.to_maxval = 30 * HZ,
29562881ae74STrond Myklebust 	.to_increment = 5 * HZ,
29572881ae74STrond Myklebust 	.to_retries = 5,
29582881ae74STrond Myklebust };
29592881ae74STrond Myklebust 
29609903cd1cSChuck Lever /**
29619903cd1cSChuck Lever  * xs_setup_udp - Set up transport to use a UDP socket
296296802a09SFrank van Maarseveen  * @args: rpc transport creation arguments
29639903cd1cSChuck Lever  *
29649903cd1cSChuck Lever  */
2965483066d6SAdrian Bunk static struct rpc_xprt *xs_setup_udp(struct xprt_create *args)
2966a246b010SChuck Lever {
29678f9d5b1aSChuck Lever 	struct sockaddr *addr = args->dstaddr;
2968c8541ecdSChuck Lever 	struct rpc_xprt *xprt;
2969c8475461SChuck Lever 	struct sock_xprt *transport;
29700a68b0beSJ. Bruce Fields 	struct rpc_xprt *ret;
2971a246b010SChuck Lever 
2972d9ba131dSTrond Myklebust 	xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries,
2973d9ba131dSTrond Myklebust 			xprt_udp_slot_table_entries);
2974c8541ecdSChuck Lever 	if (IS_ERR(xprt))
2975c8541ecdSChuck Lever 		return xprt;
2976c8475461SChuck Lever 	transport = container_of(xprt, struct sock_xprt, xprt);
2977a246b010SChuck Lever 
2978ec739ef0SChuck Lever 	xprt->prot = IPPROTO_UDP;
2979a246b010SChuck Lever 	/* XXX: header size can vary due to auth type, IPv6, etc. */
2980a246b010SChuck Lever 	xprt->max_payload = (1U << 16) - (MAX_HEADER << 3);
2981a246b010SChuck Lever 
298203bf4b70SChuck Lever 	xprt->bind_timeout = XS_BIND_TO;
298303bf4b70SChuck Lever 	xprt->reestablish_timeout = XS_UDP_REEST_TO;
298403bf4b70SChuck Lever 	xprt->idle_timeout = XS_IDLE_DISC_TO;
2985a246b010SChuck Lever 
2986262965f5SChuck Lever 	xprt->ops = &xs_udp_ops;
2987a246b010SChuck Lever 
2988ba7392bbSTrond Myklebust 	xprt->timeout = &xs_udp_default_timeout;
2989a246b010SChuck Lever 
2990f9b2ee71STrond Myklebust 	INIT_WORK(&transport->recv_worker, xs_udp_data_receive_workfn);
29914f8943f8STrond Myklebust 	INIT_WORK(&transport->error_worker, xs_error_handle);
2992edc1b01cSTrond Myklebust 	INIT_DELAYED_WORK(&transport->connect_worker, xs_udp_setup_socket);
2993edc1b01cSTrond Myklebust 
29948f9d5b1aSChuck Lever 	switch (addr->sa_family) {
29958f9d5b1aSChuck Lever 	case AF_INET:
29968f9d5b1aSChuck Lever 		if (((struct sockaddr_in *)addr)->sin_port != htons(0))
29978f9d5b1aSChuck Lever 			xprt_set_bound(xprt);
29988f9d5b1aSChuck Lever 
29999dc3b095SChuck Lever 		xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP);
30008f9d5b1aSChuck Lever 		break;
30018f9d5b1aSChuck Lever 	case AF_INET6:
30028f9d5b1aSChuck Lever 		if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
30038f9d5b1aSChuck Lever 			xprt_set_bound(xprt);
30048f9d5b1aSChuck Lever 
30059dc3b095SChuck Lever 		xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6);
30068f9d5b1aSChuck Lever 		break;
30078f9d5b1aSChuck Lever 	default:
30080a68b0beSJ. Bruce Fields 		ret = ERR_PTR(-EAFNOSUPPORT);
30090a68b0beSJ. Bruce Fields 		goto out_err;
30108f9d5b1aSChuck Lever 	}
30118f9d5b1aSChuck Lever 
3012c740eff8SChuck Lever 	if (xprt_bound(xprt))
3013c740eff8SChuck Lever 		dprintk("RPC:       set up xprt to %s (port %s) via %s\n",
3014c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_ADDR],
3015c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_PORT],
3016c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_PROTO]);
3017c740eff8SChuck Lever 	else
3018c740eff8SChuck Lever 		dprintk("RPC:       set up xprt to %s (autobind) via %s\n",
3019c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_ADDR],
3020c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_PROTO]);
3021edb267a6SChuck Lever 
3022bc25571eS\"Talpey, Thomas\ 	if (try_module_get(THIS_MODULE))
3023c8541ecdSChuck Lever 		return xprt;
30240a68b0beSJ. Bruce Fields 	ret = ERR_PTR(-EINVAL);
30250a68b0beSJ. Bruce Fields out_err:
3026315f3812SKinglong Mee 	xs_xprt_free(xprt);
30270a68b0beSJ. Bruce Fields 	return ret;
3028a246b010SChuck Lever }
3029a246b010SChuck Lever 
30302881ae74STrond Myklebust static const struct rpc_timeout xs_tcp_default_timeout = {
30312881ae74STrond Myklebust 	.to_initval = 60 * HZ,
30322881ae74STrond Myklebust 	.to_maxval = 60 * HZ,
30332881ae74STrond Myklebust 	.to_retries = 2,
30342881ae74STrond Myklebust };
30352881ae74STrond Myklebust 
30369903cd1cSChuck Lever /**
30379903cd1cSChuck Lever  * xs_setup_tcp - Set up transport to use a TCP socket
303896802a09SFrank van Maarseveen  * @args: rpc transport creation arguments
30399903cd1cSChuck Lever  *
30409903cd1cSChuck Lever  */
3041483066d6SAdrian Bunk static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args)
3042a246b010SChuck Lever {
30438f9d5b1aSChuck Lever 	struct sockaddr *addr = args->dstaddr;
3044c8541ecdSChuck Lever 	struct rpc_xprt *xprt;
3045c8475461SChuck Lever 	struct sock_xprt *transport;
30460a68b0beSJ. Bruce Fields 	struct rpc_xprt *ret;
3047b7993cebSTrond Myklebust 	unsigned int max_slot_table_size = xprt_max_tcp_slot_table_entries;
3048b7993cebSTrond Myklebust 
3049b7993cebSTrond Myklebust 	if (args->flags & XPRT_CREATE_INFINITE_SLOTS)
3050b7993cebSTrond Myklebust 		max_slot_table_size = RPC_MAX_SLOT_TABLE_LIMIT;
3051a246b010SChuck Lever 
3052d9ba131dSTrond Myklebust 	xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
3053b7993cebSTrond Myklebust 			max_slot_table_size);
3054c8541ecdSChuck Lever 	if (IS_ERR(xprt))
3055c8541ecdSChuck Lever 		return xprt;
3056c8475461SChuck Lever 	transport = container_of(xprt, struct sock_xprt, xprt);
3057a246b010SChuck Lever 
3058ec739ef0SChuck Lever 	xprt->prot = IPPROTO_TCP;
3059808012fbSChuck Lever 	xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
3060a246b010SChuck Lever 
306103bf4b70SChuck Lever 	xprt->bind_timeout = XS_BIND_TO;
306203bf4b70SChuck Lever 	xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
306303bf4b70SChuck Lever 	xprt->idle_timeout = XS_IDLE_DISC_TO;
3064a246b010SChuck Lever 
3065262965f5SChuck Lever 	xprt->ops = &xs_tcp_ops;
3066ba7392bbSTrond Myklebust 	xprt->timeout = &xs_tcp_default_timeout;
3067a246b010SChuck Lever 
30683851f1cdSTrond Myklebust 	xprt->max_reconnect_timeout = xprt->timeout->to_maxval;
30697196dbb0STrond Myklebust 	xprt->connect_timeout = xprt->timeout->to_initval *
30707196dbb0STrond Myklebust 		(xprt->timeout->to_retries + 1);
30713851f1cdSTrond Myklebust 
3072c50b8ee0STrond Myklebust 	INIT_WORK(&transport->recv_worker, xs_stream_data_receive_workfn);
30734f8943f8STrond Myklebust 	INIT_WORK(&transport->error_worker, xs_error_handle);
3074edc1b01cSTrond Myklebust 	INIT_DELAYED_WORK(&transport->connect_worker, xs_tcp_setup_socket);
3075edc1b01cSTrond Myklebust 
30768f9d5b1aSChuck Lever 	switch (addr->sa_family) {
30778f9d5b1aSChuck Lever 	case AF_INET:
30788f9d5b1aSChuck Lever 		if (((struct sockaddr_in *)addr)->sin_port != htons(0))
30798f9d5b1aSChuck Lever 			xprt_set_bound(xprt);
30808f9d5b1aSChuck Lever 
30819dc3b095SChuck Lever 		xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP);
30828f9d5b1aSChuck Lever 		break;
30838f9d5b1aSChuck Lever 	case AF_INET6:
30848f9d5b1aSChuck Lever 		if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
30858f9d5b1aSChuck Lever 			xprt_set_bound(xprt);
30868f9d5b1aSChuck Lever 
30879dc3b095SChuck Lever 		xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6);
30888f9d5b1aSChuck Lever 		break;
30898f9d5b1aSChuck Lever 	default:
30900a68b0beSJ. Bruce Fields 		ret = ERR_PTR(-EAFNOSUPPORT);
30910a68b0beSJ. Bruce Fields 		goto out_err;
30928f9d5b1aSChuck Lever 	}
30938f9d5b1aSChuck Lever 
3094c740eff8SChuck Lever 	if (xprt_bound(xprt))
3095c740eff8SChuck Lever 		dprintk("RPC:       set up xprt to %s (port %s) via %s\n",
3096c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_ADDR],
3097c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_PORT],
3098c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_PROTO]);
3099c740eff8SChuck Lever 	else
3100c740eff8SChuck Lever 		dprintk("RPC:       set up xprt to %s (autobind) via %s\n",
3101c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_ADDR],
3102c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_PROTO]);
3103c740eff8SChuck Lever 
3104bc25571eS\"Talpey, Thomas\ 	if (try_module_get(THIS_MODULE))
3105c8541ecdSChuck Lever 		return xprt;
31060a68b0beSJ. Bruce Fields 	ret = ERR_PTR(-EINVAL);
31070a68b0beSJ. Bruce Fields out_err:
3108315f3812SKinglong Mee 	xs_xprt_free(xprt);
31090a68b0beSJ. Bruce Fields 	return ret;
3110a246b010SChuck Lever }
3111282b32e1SChuck Lever 
3112f300babaSAlexandros Batsakis /**
3113f300babaSAlexandros Batsakis  * xs_setup_bc_tcp - Set up transport to use a TCP backchannel socket
3114f300babaSAlexandros Batsakis  * @args: rpc transport creation arguments
3115f300babaSAlexandros Batsakis  *
3116f300babaSAlexandros Batsakis  */
3117f300babaSAlexandros Batsakis static struct rpc_xprt *xs_setup_bc_tcp(struct xprt_create *args)
3118f300babaSAlexandros Batsakis {
3119f300babaSAlexandros Batsakis 	struct sockaddr *addr = args->dstaddr;
3120f300babaSAlexandros Batsakis 	struct rpc_xprt *xprt;
3121f300babaSAlexandros Batsakis 	struct sock_xprt *transport;
3122f300babaSAlexandros Batsakis 	struct svc_sock *bc_sock;
31230a68b0beSJ. Bruce Fields 	struct rpc_xprt *ret;
3124f300babaSAlexandros Batsakis 
3125d9ba131dSTrond Myklebust 	xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
3126d9ba131dSTrond Myklebust 			xprt_tcp_slot_table_entries);
3127f300babaSAlexandros Batsakis 	if (IS_ERR(xprt))
3128f300babaSAlexandros Batsakis 		return xprt;
3129f300babaSAlexandros Batsakis 	transport = container_of(xprt, struct sock_xprt, xprt);
3130f300babaSAlexandros Batsakis 
3131f300babaSAlexandros Batsakis 	xprt->prot = IPPROTO_TCP;
3132f300babaSAlexandros Batsakis 	xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
3133f300babaSAlexandros Batsakis 	xprt->timeout = &xs_tcp_default_timeout;
3134f300babaSAlexandros Batsakis 
3135f300babaSAlexandros Batsakis 	/* backchannel */
3136f300babaSAlexandros Batsakis 	xprt_set_bound(xprt);
3137f300babaSAlexandros Batsakis 	xprt->bind_timeout = 0;
3138f300babaSAlexandros Batsakis 	xprt->reestablish_timeout = 0;
3139f300babaSAlexandros Batsakis 	xprt->idle_timeout = 0;
3140f300babaSAlexandros Batsakis 
3141f300babaSAlexandros Batsakis 	xprt->ops = &bc_tcp_ops;
3142f300babaSAlexandros Batsakis 
3143f300babaSAlexandros Batsakis 	switch (addr->sa_family) {
3144f300babaSAlexandros Batsakis 	case AF_INET:
3145f300babaSAlexandros Batsakis 		xs_format_peer_addresses(xprt, "tcp",
3146f300babaSAlexandros Batsakis 					 RPCBIND_NETID_TCP);
3147f300babaSAlexandros Batsakis 		break;
3148f300babaSAlexandros Batsakis 	case AF_INET6:
3149f300babaSAlexandros Batsakis 		xs_format_peer_addresses(xprt, "tcp",
3150f300babaSAlexandros Batsakis 				   RPCBIND_NETID_TCP6);
3151f300babaSAlexandros Batsakis 		break;
3152f300babaSAlexandros Batsakis 	default:
31530a68b0beSJ. Bruce Fields 		ret = ERR_PTR(-EAFNOSUPPORT);
31540a68b0beSJ. Bruce Fields 		goto out_err;
3155f300babaSAlexandros Batsakis 	}
3156f300babaSAlexandros Batsakis 
3157f300babaSAlexandros Batsakis 	dprintk("RPC:       set up xprt to %s (port %s) via %s\n",
3158f300babaSAlexandros Batsakis 			xprt->address_strings[RPC_DISPLAY_ADDR],
3159f300babaSAlexandros Batsakis 			xprt->address_strings[RPC_DISPLAY_PORT],
3160f300babaSAlexandros Batsakis 			xprt->address_strings[RPC_DISPLAY_PROTO]);
3161f300babaSAlexandros Batsakis 
3162f300babaSAlexandros Batsakis 	/*
316399de8ea9SJ. Bruce Fields 	 * Once we've associated a backchannel xprt with a connection,
316428303ca3SWeng Meiling 	 * we want to keep it around as long as the connection lasts,
316528303ca3SWeng Meiling 	 * in case we need to start using it for a backchannel again;
316628303ca3SWeng Meiling 	 * this reference won't be dropped until bc_xprt is destroyed.
316799de8ea9SJ. Bruce Fields 	 */
316899de8ea9SJ. Bruce Fields 	xprt_get(xprt);
316999de8ea9SJ. Bruce Fields 	args->bc_xprt->xpt_bc_xprt = xprt;
317099de8ea9SJ. Bruce Fields 	xprt->bc_xprt = args->bc_xprt;
317199de8ea9SJ. Bruce Fields 	bc_sock = container_of(args->bc_xprt, struct svc_sock, sk_xprt);
317299de8ea9SJ. Bruce Fields 	transport->sock = bc_sock->sk_sock;
317399de8ea9SJ. Bruce Fields 	transport->inet = bc_sock->sk_sk;
317499de8ea9SJ. Bruce Fields 
317599de8ea9SJ. Bruce Fields 	/*
3176f300babaSAlexandros Batsakis 	 * Since we don't want connections for the backchannel, we set
3177f300babaSAlexandros Batsakis 	 * the xprt status to connected
3178f300babaSAlexandros Batsakis 	 */
3179f300babaSAlexandros Batsakis 	xprt_set_connected(xprt);
3180f300babaSAlexandros Batsakis 
3181f300babaSAlexandros Batsakis 	if (try_module_get(THIS_MODULE))
3182f300babaSAlexandros Batsakis 		return xprt;
3183642aab58SKinglong Mee 
3184642aab58SKinglong Mee 	args->bc_xprt->xpt_bc_xprt = NULL;
318539a9beabSJ. Bruce Fields 	args->bc_xprt->xpt_bc_xps = NULL;
318699de8ea9SJ. Bruce Fields 	xprt_put(xprt);
31870a68b0beSJ. Bruce Fields 	ret = ERR_PTR(-EINVAL);
31880a68b0beSJ. Bruce Fields out_err:
3189315f3812SKinglong Mee 	xs_xprt_free(xprt);
31900a68b0beSJ. Bruce Fields 	return ret;
3191f300babaSAlexandros Batsakis }
3192f300babaSAlexandros Batsakis 
3193176e21eeSChuck Lever static struct xprt_class	xs_local_transport = {
3194176e21eeSChuck Lever 	.list		= LIST_HEAD_INIT(xs_local_transport.list),
3195176e21eeSChuck Lever 	.name		= "named UNIX socket",
3196176e21eeSChuck Lever 	.owner		= THIS_MODULE,
3197176e21eeSChuck Lever 	.ident		= XPRT_TRANSPORT_LOCAL,
3198176e21eeSChuck Lever 	.setup		= xs_setup_local,
3199176e21eeSChuck Lever };
3200176e21eeSChuck Lever 
3201bc25571eS\"Talpey, Thomas\ static struct xprt_class	xs_udp_transport = {
3202bc25571eS\"Talpey, Thomas\ 	.list		= LIST_HEAD_INIT(xs_udp_transport.list),
3203bc25571eS\"Talpey, Thomas\ 	.name		= "udp",
3204bc25571eS\"Talpey, Thomas\ 	.owner		= THIS_MODULE,
3205f300babaSAlexandros Batsakis 	.ident		= XPRT_TRANSPORT_UDP,
3206bc25571eS\"Talpey, Thomas\ 	.setup		= xs_setup_udp,
3207bc25571eS\"Talpey, Thomas\ };
3208bc25571eS\"Talpey, Thomas\ 
3209bc25571eS\"Talpey, Thomas\ static struct xprt_class	xs_tcp_transport = {
3210bc25571eS\"Talpey, Thomas\ 	.list		= LIST_HEAD_INIT(xs_tcp_transport.list),
3211bc25571eS\"Talpey, Thomas\ 	.name		= "tcp",
3212bc25571eS\"Talpey, Thomas\ 	.owner		= THIS_MODULE,
3213f300babaSAlexandros Batsakis 	.ident		= XPRT_TRANSPORT_TCP,
3214bc25571eS\"Talpey, Thomas\ 	.setup		= xs_setup_tcp,
3215bc25571eS\"Talpey, Thomas\ };
3216bc25571eS\"Talpey, Thomas\ 
3217f300babaSAlexandros Batsakis static struct xprt_class	xs_bc_tcp_transport = {
3218f300babaSAlexandros Batsakis 	.list		= LIST_HEAD_INIT(xs_bc_tcp_transport.list),
3219f300babaSAlexandros Batsakis 	.name		= "tcp NFSv4.1 backchannel",
3220f300babaSAlexandros Batsakis 	.owner		= THIS_MODULE,
3221f300babaSAlexandros Batsakis 	.ident		= XPRT_TRANSPORT_BC_TCP,
3222f300babaSAlexandros Batsakis 	.setup		= xs_setup_bc_tcp,
3223f300babaSAlexandros Batsakis };
3224f300babaSAlexandros Batsakis 
3225282b32e1SChuck Lever /**
3226bc25571eS\"Talpey, Thomas\  * init_socket_xprt - set up xprtsock's sysctls, register with RPC client
3227282b32e1SChuck Lever  *
3228282b32e1SChuck Lever  */
3229282b32e1SChuck Lever int init_socket_xprt(void)
3230282b32e1SChuck Lever {
32312b1bec5fSEric W. Biederman 	if (!sunrpc_table_header)
32320b4d4147SEric W. Biederman 		sunrpc_table_header = register_sysctl_table(sunrpc_table);
3233fbf76683SChuck Lever 
3234176e21eeSChuck Lever 	xprt_register_transport(&xs_local_transport);
3235bc25571eS\"Talpey, Thomas\ 	xprt_register_transport(&xs_udp_transport);
3236bc25571eS\"Talpey, Thomas\ 	xprt_register_transport(&xs_tcp_transport);
3237f300babaSAlexandros Batsakis 	xprt_register_transport(&xs_bc_tcp_transport);
3238bc25571eS\"Talpey, Thomas\ 
3239282b32e1SChuck Lever 	return 0;
3240282b32e1SChuck Lever }
3241282b32e1SChuck Lever 
3242282b32e1SChuck Lever /**
3243bc25571eS\"Talpey, Thomas\  * cleanup_socket_xprt - remove xprtsock's sysctls, unregister
3244282b32e1SChuck Lever  *
3245282b32e1SChuck Lever  */
3246282b32e1SChuck Lever void cleanup_socket_xprt(void)
3247282b32e1SChuck Lever {
3248fbf76683SChuck Lever 	if (sunrpc_table_header) {
3249fbf76683SChuck Lever 		unregister_sysctl_table(sunrpc_table_header);
3250fbf76683SChuck Lever 		sunrpc_table_header = NULL;
3251fbf76683SChuck Lever 	}
3252bc25571eS\"Talpey, Thomas\ 
3253176e21eeSChuck Lever 	xprt_unregister_transport(&xs_local_transport);
3254bc25571eS\"Talpey, Thomas\ 	xprt_unregister_transport(&xs_udp_transport);
3255bc25571eS\"Talpey, Thomas\ 	xprt_unregister_transport(&xs_tcp_transport);
3256f300babaSAlexandros Batsakis 	xprt_unregister_transport(&xs_bc_tcp_transport);
3257282b32e1SChuck Lever }
3258cbf11071STrond Myklebust 
32599bbb9e5aSRusty Russell static int param_set_uint_minmax(const char *val,
32609bbb9e5aSRusty Russell 		const struct kernel_param *kp,
3261cbf11071STrond Myklebust 		unsigned int min, unsigned int max)
3262cbf11071STrond Myklebust {
326300cfaa94SDaniel Walter 	unsigned int num;
3264cbf11071STrond Myklebust 	int ret;
3265cbf11071STrond Myklebust 
3266cbf11071STrond Myklebust 	if (!val)
3267cbf11071STrond Myklebust 		return -EINVAL;
326800cfaa94SDaniel Walter 	ret = kstrtouint(val, 0, &num);
32699761a246SDan Carpenter 	if (ret)
32709761a246SDan Carpenter 		return ret;
32719761a246SDan Carpenter 	if (num < min || num > max)
3272cbf11071STrond Myklebust 		return -EINVAL;
3273cbf11071STrond Myklebust 	*((unsigned int *)kp->arg) = num;
3274cbf11071STrond Myklebust 	return 0;
3275cbf11071STrond Myklebust }
3276cbf11071STrond Myklebust 
32779bbb9e5aSRusty Russell static int param_set_portnr(const char *val, const struct kernel_param *kp)
3278cbf11071STrond Myklebust {
3279cbf11071STrond Myklebust 	return param_set_uint_minmax(val, kp,
3280cbf11071STrond Myklebust 			RPC_MIN_RESVPORT,
3281cbf11071STrond Myklebust 			RPC_MAX_RESVPORT);
3282cbf11071STrond Myklebust }
3283cbf11071STrond Myklebust 
32849c27847dSLuis R. Rodriguez static const struct kernel_param_ops param_ops_portnr = {
32859bbb9e5aSRusty Russell 	.set = param_set_portnr,
32869bbb9e5aSRusty Russell 	.get = param_get_uint,
32879bbb9e5aSRusty Russell };
32889bbb9e5aSRusty Russell 
3289cbf11071STrond Myklebust #define param_check_portnr(name, p) \
3290cbf11071STrond Myklebust 	__param_check(name, p, unsigned int);
3291cbf11071STrond Myklebust 
3292cbf11071STrond Myklebust module_param_named(min_resvport, xprt_min_resvport, portnr, 0644);
3293cbf11071STrond Myklebust module_param_named(max_resvport, xprt_max_resvport, portnr, 0644);
3294cbf11071STrond Myklebust 
32959bbb9e5aSRusty Russell static int param_set_slot_table_size(const char *val,
32969bbb9e5aSRusty Russell 				     const struct kernel_param *kp)
3297cbf11071STrond Myklebust {
3298cbf11071STrond Myklebust 	return param_set_uint_minmax(val, kp,
3299cbf11071STrond Myklebust 			RPC_MIN_SLOT_TABLE,
3300cbf11071STrond Myklebust 			RPC_MAX_SLOT_TABLE);
3301cbf11071STrond Myklebust }
3302cbf11071STrond Myklebust 
33039c27847dSLuis R. Rodriguez static const struct kernel_param_ops param_ops_slot_table_size = {
33049bbb9e5aSRusty Russell 	.set = param_set_slot_table_size,
33059bbb9e5aSRusty Russell 	.get = param_get_uint,
33069bbb9e5aSRusty Russell };
33079bbb9e5aSRusty Russell 
3308cbf11071STrond Myklebust #define param_check_slot_table_size(name, p) \
3309cbf11071STrond Myklebust 	__param_check(name, p, unsigned int);
3310cbf11071STrond Myklebust 
3311d9ba131dSTrond Myklebust static int param_set_max_slot_table_size(const char *val,
3312d9ba131dSTrond Myklebust 				     const struct kernel_param *kp)
3313d9ba131dSTrond Myklebust {
3314d9ba131dSTrond Myklebust 	return param_set_uint_minmax(val, kp,
3315d9ba131dSTrond Myklebust 			RPC_MIN_SLOT_TABLE,
3316d9ba131dSTrond Myklebust 			RPC_MAX_SLOT_TABLE_LIMIT);
3317d9ba131dSTrond Myklebust }
3318d9ba131dSTrond Myklebust 
33199c27847dSLuis R. Rodriguez static const struct kernel_param_ops param_ops_max_slot_table_size = {
3320d9ba131dSTrond Myklebust 	.set = param_set_max_slot_table_size,
3321d9ba131dSTrond Myklebust 	.get = param_get_uint,
3322d9ba131dSTrond Myklebust };
3323d9ba131dSTrond Myklebust 
3324d9ba131dSTrond Myklebust #define param_check_max_slot_table_size(name, p) \
3325d9ba131dSTrond Myklebust 	__param_check(name, p, unsigned int);
3326d9ba131dSTrond Myklebust 
3327cbf11071STrond Myklebust module_param_named(tcp_slot_table_entries, xprt_tcp_slot_table_entries,
3328cbf11071STrond Myklebust 		   slot_table_size, 0644);
3329d9ba131dSTrond Myklebust module_param_named(tcp_max_slot_table_entries, xprt_max_tcp_slot_table_entries,
3330d9ba131dSTrond Myklebust 		   max_slot_table_size, 0644);
3331cbf11071STrond Myklebust module_param_named(udp_slot_table_entries, xprt_udp_slot_table_entries,
3332cbf11071STrond Myklebust 		   slot_table_size, 0644);
3333