xref: /openbmc/linux/net/sunrpc/xprtsock.c (revision 7073ea8799a8cf73db60270986f14e4aae20fa80)
1a246b010SChuck Lever /*
2a246b010SChuck Lever  * linux/net/sunrpc/xprtsock.c
3a246b010SChuck Lever  *
4a246b010SChuck Lever  * Client-side transport implementation for sockets.
5a246b010SChuck Lever  *
6113aa838SAlan Cox  * TCP callback races fixes (C) 1998 Red Hat
7113aa838SAlan Cox  * TCP send fixes (C) 1998 Red Hat
8a246b010SChuck Lever  * TCP NFS related read + write fixes
9a246b010SChuck Lever  *  (C) 1999 Dave Airlie, University of Limerick, Ireland <airlied@linux.ie>
10a246b010SChuck Lever  *
11a246b010SChuck Lever  * Rewrite of larges part of the code in order to stabilize TCP stuff.
12a246b010SChuck Lever  * Fix behaviour when socket buffer is full.
13a246b010SChuck Lever  *  (C) 1999 Trond Myklebust <trond.myklebust@fys.uio.no>
1455aa4f58SChuck Lever  *
1555aa4f58SChuck Lever  * IP socket transport implementation, (C) 2005 Chuck Lever <cel@netapp.com>
168f9d5b1aSChuck Lever  *
178f9d5b1aSChuck Lever  * IPv6 support contributed by Gilles Quillard, Bull Open Source, 2005.
188f9d5b1aSChuck Lever  *   <gilles.quillard@bull.net>
19a246b010SChuck Lever  */
20a246b010SChuck Lever 
21a246b010SChuck Lever #include <linux/types.h>
22176e21eeSChuck Lever #include <linux/string.h>
23a246b010SChuck Lever #include <linux/slab.h>
24bc25571eS\"Talpey, Thomas\ #include <linux/module.h>
25a246b010SChuck Lever #include <linux/capability.h>
26a246b010SChuck Lever #include <linux/pagemap.h>
27a246b010SChuck Lever #include <linux/errno.h>
28a246b010SChuck Lever #include <linux/socket.h>
29a246b010SChuck Lever #include <linux/in.h>
30a246b010SChuck Lever #include <linux/net.h>
31a246b010SChuck Lever #include <linux/mm.h>
32176e21eeSChuck Lever #include <linux/un.h>
33a246b010SChuck Lever #include <linux/udp.h>
34a246b010SChuck Lever #include <linux/tcp.h>
35a246b010SChuck Lever #include <linux/sunrpc/clnt.h>
365976687aSJeff Layton #include <linux/sunrpc/addr.h>
3702107148SChuck Lever #include <linux/sunrpc/sched.h>
384cfc7e60SRahul Iyer #include <linux/sunrpc/svcsock.h>
3949c36fccS\"Talpey, Thomas\ #include <linux/sunrpc/xprtsock.h>
40a246b010SChuck Lever #include <linux/file.h>
419e00abc3STrond Myklebust #ifdef CONFIG_SUNRPC_BACKCHANNEL
4244b98efdSRicardo Labiaga #include <linux/sunrpc/bc_xprt.h>
4344b98efdSRicardo Labiaga #endif
44a246b010SChuck Lever 
45a246b010SChuck Lever #include <net/sock.h>
46a246b010SChuck Lever #include <net/checksum.h>
47a246b010SChuck Lever #include <net/udp.h>
48a246b010SChuck Lever #include <net/tcp.h>
49a246b010SChuck Lever 
504cfc7e60SRahul Iyer #include "sunrpc.h"
51176e21eeSChuck Lever 
52176e21eeSChuck Lever static void xs_close(struct rpc_xprt *xprt);
53176e21eeSChuck Lever 
549903cd1cSChuck Lever /*
55c556b754SChuck Lever  * xprtsock tunables
56c556b754SChuck Lever  */
5709acfea5STrond Myklebust static unsigned int xprt_udp_slot_table_entries = RPC_DEF_SLOT_TABLE;
5809acfea5STrond Myklebust static unsigned int xprt_tcp_slot_table_entries = RPC_MIN_SLOT_TABLE;
5909acfea5STrond Myklebust static unsigned int xprt_max_tcp_slot_table_entries = RPC_MAX_SLOT_TABLE;
60c556b754SChuck Lever 
6109acfea5STrond Myklebust static unsigned int xprt_min_resvport = RPC_DEF_MIN_RESVPORT;
6209acfea5STrond Myklebust static unsigned int xprt_max_resvport = RPC_DEF_MAX_RESVPORT;
63c556b754SChuck Lever 
647d1e8255STrond Myklebust #define XS_TCP_LINGER_TO	(15U * HZ)
6525fe6142STrond Myklebust static unsigned int xs_tcp_fin_timeout __read_mostly = XS_TCP_LINGER_TO;
667d1e8255STrond Myklebust 
67c556b754SChuck Lever /*
68fbf76683SChuck Lever  * We can register our own files under /proc/sys/sunrpc by
69fbf76683SChuck Lever  * calling register_sysctl_table() again.  The files in that
70fbf76683SChuck Lever  * directory become the union of all files registered there.
71fbf76683SChuck Lever  *
72fbf76683SChuck Lever  * We simply need to make sure that we don't collide with
73fbf76683SChuck Lever  * someone else's file names!
74fbf76683SChuck Lever  */
75fbf76683SChuck Lever 
76fbf76683SChuck Lever #ifdef RPC_DEBUG
77fbf76683SChuck Lever 
78fbf76683SChuck Lever static unsigned int min_slot_table_size = RPC_MIN_SLOT_TABLE;
79fbf76683SChuck Lever static unsigned int max_slot_table_size = RPC_MAX_SLOT_TABLE;
80d9ba131dSTrond Myklebust static unsigned int max_tcp_slot_table_limit = RPC_MAX_SLOT_TABLE_LIMIT;
81fbf76683SChuck Lever static unsigned int xprt_min_resvport_limit = RPC_MIN_RESVPORT;
82fbf76683SChuck Lever static unsigned int xprt_max_resvport_limit = RPC_MAX_RESVPORT;
83fbf76683SChuck Lever 
84fbf76683SChuck Lever static struct ctl_table_header *sunrpc_table_header;
85fbf76683SChuck Lever 
86fbf76683SChuck Lever /*
87fbf76683SChuck Lever  * FIXME: changing the UDP slot table size should also resize the UDP
88fbf76683SChuck Lever  *        socket buffers for existing UDP transports
89fbf76683SChuck Lever  */
90fbf76683SChuck Lever static ctl_table xs_tunables_table[] = {
91fbf76683SChuck Lever 	{
92fbf76683SChuck Lever 		.procname	= "udp_slot_table_entries",
93fbf76683SChuck Lever 		.data		= &xprt_udp_slot_table_entries,
94fbf76683SChuck Lever 		.maxlen		= sizeof(unsigned int),
95fbf76683SChuck Lever 		.mode		= 0644,
966d456111SEric W. Biederman 		.proc_handler	= proc_dointvec_minmax,
97fbf76683SChuck Lever 		.extra1		= &min_slot_table_size,
98fbf76683SChuck Lever 		.extra2		= &max_slot_table_size
99fbf76683SChuck Lever 	},
100fbf76683SChuck Lever 	{
101fbf76683SChuck Lever 		.procname	= "tcp_slot_table_entries",
102fbf76683SChuck Lever 		.data		= &xprt_tcp_slot_table_entries,
103fbf76683SChuck Lever 		.maxlen		= sizeof(unsigned int),
104fbf76683SChuck Lever 		.mode		= 0644,
1056d456111SEric W. Biederman 		.proc_handler	= proc_dointvec_minmax,
106fbf76683SChuck Lever 		.extra1		= &min_slot_table_size,
107fbf76683SChuck Lever 		.extra2		= &max_slot_table_size
108fbf76683SChuck Lever 	},
109fbf76683SChuck Lever 	{
110d9ba131dSTrond Myklebust 		.procname	= "tcp_max_slot_table_entries",
111d9ba131dSTrond Myklebust 		.data		= &xprt_max_tcp_slot_table_entries,
112d9ba131dSTrond Myklebust 		.maxlen		= sizeof(unsigned int),
113d9ba131dSTrond Myklebust 		.mode		= 0644,
114d9ba131dSTrond Myklebust 		.proc_handler	= proc_dointvec_minmax,
115d9ba131dSTrond Myklebust 		.extra1		= &min_slot_table_size,
116d9ba131dSTrond Myklebust 		.extra2		= &max_tcp_slot_table_limit
117d9ba131dSTrond Myklebust 	},
118d9ba131dSTrond Myklebust 	{
119fbf76683SChuck Lever 		.procname	= "min_resvport",
120fbf76683SChuck Lever 		.data		= &xprt_min_resvport,
121fbf76683SChuck Lever 		.maxlen		= sizeof(unsigned int),
122fbf76683SChuck Lever 		.mode		= 0644,
1236d456111SEric W. Biederman 		.proc_handler	= proc_dointvec_minmax,
124fbf76683SChuck Lever 		.extra1		= &xprt_min_resvport_limit,
125fbf76683SChuck Lever 		.extra2		= &xprt_max_resvport_limit
126fbf76683SChuck Lever 	},
127fbf76683SChuck Lever 	{
128fbf76683SChuck Lever 		.procname	= "max_resvport",
129fbf76683SChuck Lever 		.data		= &xprt_max_resvport,
130fbf76683SChuck Lever 		.maxlen		= sizeof(unsigned int),
131fbf76683SChuck Lever 		.mode		= 0644,
1326d456111SEric W. Biederman 		.proc_handler	= proc_dointvec_minmax,
133fbf76683SChuck Lever 		.extra1		= &xprt_min_resvport_limit,
134fbf76683SChuck Lever 		.extra2		= &xprt_max_resvport_limit
135fbf76683SChuck Lever 	},
136fbf76683SChuck Lever 	{
13725fe6142STrond Myklebust 		.procname	= "tcp_fin_timeout",
13825fe6142STrond Myklebust 		.data		= &xs_tcp_fin_timeout,
13925fe6142STrond Myklebust 		.maxlen		= sizeof(xs_tcp_fin_timeout),
14025fe6142STrond Myklebust 		.mode		= 0644,
1416d456111SEric W. Biederman 		.proc_handler	= proc_dointvec_jiffies,
14225fe6142STrond Myklebust 	},
143f8572d8fSEric W. Biederman 	{ },
144fbf76683SChuck Lever };
145fbf76683SChuck Lever 
146fbf76683SChuck Lever static ctl_table sunrpc_table[] = {
147fbf76683SChuck Lever 	{
148fbf76683SChuck Lever 		.procname	= "sunrpc",
149fbf76683SChuck Lever 		.mode		= 0555,
150fbf76683SChuck Lever 		.child		= xs_tunables_table
151fbf76683SChuck Lever 	},
152f8572d8fSEric W. Biederman 	{ },
153fbf76683SChuck Lever };
154fbf76683SChuck Lever 
155fbf76683SChuck Lever #endif
156fbf76683SChuck Lever 
157fbf76683SChuck Lever /*
15803bf4b70SChuck Lever  * Wait duration for a reply from the RPC portmapper.
15903bf4b70SChuck Lever  */
16003bf4b70SChuck Lever #define XS_BIND_TO		(60U * HZ)
16103bf4b70SChuck Lever 
16203bf4b70SChuck Lever /*
16303bf4b70SChuck Lever  * Delay if a UDP socket connect error occurs.  This is most likely some
16403bf4b70SChuck Lever  * kind of resource problem on the local host.
16503bf4b70SChuck Lever  */
16603bf4b70SChuck Lever #define XS_UDP_REEST_TO		(2U * HZ)
16703bf4b70SChuck Lever 
16803bf4b70SChuck Lever /*
16903bf4b70SChuck Lever  * The reestablish timeout allows clients to delay for a bit before attempting
17003bf4b70SChuck Lever  * to reconnect to a server that just dropped our connection.
17103bf4b70SChuck Lever  *
17203bf4b70SChuck Lever  * We implement an exponential backoff when trying to reestablish a TCP
17303bf4b70SChuck Lever  * transport connection with the server.  Some servers like to drop a TCP
17403bf4b70SChuck Lever  * connection when they are overworked, so we start with a short timeout and
17503bf4b70SChuck Lever  * increase over time if the server is down or not responding.
17603bf4b70SChuck Lever  */
17703bf4b70SChuck Lever #define XS_TCP_INIT_REEST_TO	(3U * HZ)
17803bf4b70SChuck Lever #define XS_TCP_MAX_REEST_TO	(5U * 60 * HZ)
17903bf4b70SChuck Lever 
18003bf4b70SChuck Lever /*
18103bf4b70SChuck Lever  * TCP idle timeout; client drops the transport socket if it is idle
18203bf4b70SChuck Lever  * for this long.  Note that we also timeout UDP sockets to prevent
18303bf4b70SChuck Lever  * holding port numbers when there is no RPC traffic.
18403bf4b70SChuck Lever  */
18503bf4b70SChuck Lever #define XS_IDLE_DISC_TO		(5U * 60 * HZ)
18603bf4b70SChuck Lever 
187a246b010SChuck Lever #ifdef RPC_DEBUG
188a246b010SChuck Lever # undef  RPC_DEBUG_DATA
1899903cd1cSChuck Lever # define RPCDBG_FACILITY	RPCDBG_TRANS
190a246b010SChuck Lever #endif
191a246b010SChuck Lever 
192a246b010SChuck Lever #ifdef RPC_DEBUG_DATA
1939903cd1cSChuck Lever static void xs_pktdump(char *msg, u32 *packet, unsigned int count)
194a246b010SChuck Lever {
195a246b010SChuck Lever 	u8 *buf = (u8 *) packet;
196a246b010SChuck Lever 	int j;
197a246b010SChuck Lever 
198a246b010SChuck Lever 	dprintk("RPC:       %s\n", msg);
199a246b010SChuck Lever 	for (j = 0; j < count && j < 128; j += 4) {
200a246b010SChuck Lever 		if (!(j & 31)) {
201a246b010SChuck Lever 			if (j)
202a246b010SChuck Lever 				dprintk("\n");
203a246b010SChuck Lever 			dprintk("0x%04x ", j);
204a246b010SChuck Lever 		}
205a246b010SChuck Lever 		dprintk("%02x%02x%02x%02x ",
206a246b010SChuck Lever 			buf[j], buf[j+1], buf[j+2], buf[j+3]);
207a246b010SChuck Lever 	}
208a246b010SChuck Lever 	dprintk("\n");
209a246b010SChuck Lever }
210a246b010SChuck Lever #else
2119903cd1cSChuck Lever static inline void xs_pktdump(char *msg, u32 *packet, unsigned int count)
212a246b010SChuck Lever {
213a246b010SChuck Lever 	/* NOP */
214a246b010SChuck Lever }
215a246b010SChuck Lever #endif
216a246b010SChuck Lever 
217ffc2e518SChuck Lever struct sock_xprt {
218ffc2e518SChuck Lever 	struct rpc_xprt		xprt;
219ee0ac0c2SChuck Lever 
220ee0ac0c2SChuck Lever 	/*
221ee0ac0c2SChuck Lever 	 * Network layer
222ee0ac0c2SChuck Lever 	 */
223ee0ac0c2SChuck Lever 	struct socket *		sock;
224ee0ac0c2SChuck Lever 	struct sock *		inet;
22551971139SChuck Lever 
22651971139SChuck Lever 	/*
22751971139SChuck Lever 	 * State of TCP reply receive
22851971139SChuck Lever 	 */
22951971139SChuck Lever 	__be32			tcp_fraghdr,
230b76ce561STrond Myklebust 				tcp_xid,
231b76ce561STrond Myklebust 				tcp_calldir;
23251971139SChuck Lever 
23351971139SChuck Lever 	u32			tcp_offset,
23451971139SChuck Lever 				tcp_reclen;
23551971139SChuck Lever 
23651971139SChuck Lever 	unsigned long		tcp_copied,
23751971139SChuck Lever 				tcp_flags;
238c8475461SChuck Lever 
239c8475461SChuck Lever 	/*
240c8475461SChuck Lever 	 * Connection of transports
241c8475461SChuck Lever 	 */
24234161db6STrond Myklebust 	struct delayed_work	connect_worker;
243fbfffbd5SChuck Lever 	struct sockaddr_storage	srcaddr;
244fbfffbd5SChuck Lever 	unsigned short		srcport;
2457c6e066eSChuck Lever 
2467c6e066eSChuck Lever 	/*
2477c6e066eSChuck Lever 	 * UDP socket buffer size parameters
2487c6e066eSChuck Lever 	 */
2497c6e066eSChuck Lever 	size_t			rcvsize,
2507c6e066eSChuck Lever 				sndsize;
251314dfd79SChuck Lever 
252314dfd79SChuck Lever 	/*
253314dfd79SChuck Lever 	 * Saved socket callback addresses
254314dfd79SChuck Lever 	 */
255314dfd79SChuck Lever 	void			(*old_data_ready)(struct sock *, int);
256314dfd79SChuck Lever 	void			(*old_state_change)(struct sock *);
257314dfd79SChuck Lever 	void			(*old_write_space)(struct sock *);
258ffc2e518SChuck Lever };
259ffc2e518SChuck Lever 
260e136d092SChuck Lever /*
261e136d092SChuck Lever  * TCP receive state flags
262e136d092SChuck Lever  */
263e136d092SChuck Lever #define TCP_RCV_LAST_FRAG	(1UL << 0)
264e136d092SChuck Lever #define TCP_RCV_COPY_FRAGHDR	(1UL << 1)
265e136d092SChuck Lever #define TCP_RCV_COPY_XID	(1UL << 2)
266e136d092SChuck Lever #define TCP_RCV_COPY_DATA	(1UL << 3)
267f4a2e418SRicardo Labiaga #define TCP_RCV_READ_CALLDIR	(1UL << 4)
268f4a2e418SRicardo Labiaga #define TCP_RCV_COPY_CALLDIR	(1UL << 5)
26918dca02aSRicardo Labiaga 
27018dca02aSRicardo Labiaga /*
27118dca02aSRicardo Labiaga  * TCP RPC flags
27218dca02aSRicardo Labiaga  */
273f4a2e418SRicardo Labiaga #define TCP_RPC_REPLY		(1UL << 6)
274e136d092SChuck Lever 
27595392c59SChuck Lever static inline struct sockaddr *xs_addr(struct rpc_xprt *xprt)
276edb267a6SChuck Lever {
27795392c59SChuck Lever 	return (struct sockaddr *) &xprt->addr;
27895392c59SChuck Lever }
27995392c59SChuck Lever 
280176e21eeSChuck Lever static inline struct sockaddr_un *xs_addr_un(struct rpc_xprt *xprt)
281176e21eeSChuck Lever {
282176e21eeSChuck Lever 	return (struct sockaddr_un *) &xprt->addr;
283176e21eeSChuck Lever }
284176e21eeSChuck Lever 
28595392c59SChuck Lever static inline struct sockaddr_in *xs_addr_in(struct rpc_xprt *xprt)
28695392c59SChuck Lever {
28795392c59SChuck Lever 	return (struct sockaddr_in *) &xprt->addr;
28895392c59SChuck Lever }
28995392c59SChuck Lever 
29095392c59SChuck Lever static inline struct sockaddr_in6 *xs_addr_in6(struct rpc_xprt *xprt)
29195392c59SChuck Lever {
29295392c59SChuck Lever 	return (struct sockaddr_in6 *) &xprt->addr;
29395392c59SChuck Lever }
29495392c59SChuck Lever 
295c877b849SChuck Lever static void xs_format_common_peer_addresses(struct rpc_xprt *xprt)
296c877b849SChuck Lever {
297c877b849SChuck Lever 	struct sockaddr *sap = xs_addr(xprt);
2989dc3b095SChuck Lever 	struct sockaddr_in6 *sin6;
2999dc3b095SChuck Lever 	struct sockaddr_in *sin;
300176e21eeSChuck Lever 	struct sockaddr_un *sun;
301c877b849SChuck Lever 	char buf[128];
302c877b849SChuck Lever 
3039dc3b095SChuck Lever 	switch (sap->sa_family) {
304176e21eeSChuck Lever 	case AF_LOCAL:
305176e21eeSChuck Lever 		sun = xs_addr_un(xprt);
306176e21eeSChuck Lever 		strlcpy(buf, sun->sun_path, sizeof(buf));
307176e21eeSChuck Lever 		xprt->address_strings[RPC_DISPLAY_ADDR] =
308176e21eeSChuck Lever 						kstrdup(buf, GFP_KERNEL);
309176e21eeSChuck Lever 		break;
3109dc3b095SChuck Lever 	case AF_INET:
311176e21eeSChuck Lever 		(void)rpc_ntop(sap, buf, sizeof(buf));
312176e21eeSChuck Lever 		xprt->address_strings[RPC_DISPLAY_ADDR] =
313176e21eeSChuck Lever 						kstrdup(buf, GFP_KERNEL);
3149dc3b095SChuck Lever 		sin = xs_addr_in(xprt);
315fc0b5791SJoe Perches 		snprintf(buf, sizeof(buf), "%08x", ntohl(sin->sin_addr.s_addr));
3169dc3b095SChuck Lever 		break;
3179dc3b095SChuck Lever 	case AF_INET6:
318176e21eeSChuck Lever 		(void)rpc_ntop(sap, buf, sizeof(buf));
319176e21eeSChuck Lever 		xprt->address_strings[RPC_DISPLAY_ADDR] =
320176e21eeSChuck Lever 						kstrdup(buf, GFP_KERNEL);
3219dc3b095SChuck Lever 		sin6 = xs_addr_in6(xprt);
322fc0b5791SJoe Perches 		snprintf(buf, sizeof(buf), "%pi6", &sin6->sin6_addr);
3239dc3b095SChuck Lever 		break;
3249dc3b095SChuck Lever 	default:
3259dc3b095SChuck Lever 		BUG();
3269dc3b095SChuck Lever 	}
327176e21eeSChuck Lever 
3289dc3b095SChuck Lever 	xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = kstrdup(buf, GFP_KERNEL);
3299dc3b095SChuck Lever }
3309dc3b095SChuck Lever 
3319dc3b095SChuck Lever static void xs_format_common_peer_ports(struct rpc_xprt *xprt)
3329dc3b095SChuck Lever {
3339dc3b095SChuck Lever 	struct sockaddr *sap = xs_addr(xprt);
3349dc3b095SChuck Lever 	char buf[128];
3359dc3b095SChuck Lever 
33681160e66SJoe Perches 	snprintf(buf, sizeof(buf), "%u", rpc_get_port(sap));
337c877b849SChuck Lever 	xprt->address_strings[RPC_DISPLAY_PORT] = kstrdup(buf, GFP_KERNEL);
338c877b849SChuck Lever 
33981160e66SJoe Perches 	snprintf(buf, sizeof(buf), "%4hx", rpc_get_port(sap));
340c877b849SChuck Lever 	xprt->address_strings[RPC_DISPLAY_HEX_PORT] = kstrdup(buf, GFP_KERNEL);
341c877b849SChuck Lever }
342c877b849SChuck Lever 
3439dc3b095SChuck Lever static void xs_format_peer_addresses(struct rpc_xprt *xprt,
344b454ae90SChuck Lever 				     const char *protocol,
345b454ae90SChuck Lever 				     const char *netid)
346edb267a6SChuck Lever {
347b454ae90SChuck Lever 	xprt->address_strings[RPC_DISPLAY_PROTO] = protocol;
348b454ae90SChuck Lever 	xprt->address_strings[RPC_DISPLAY_NETID] = netid;
349c877b849SChuck Lever 	xs_format_common_peer_addresses(xprt);
3509dc3b095SChuck Lever 	xs_format_common_peer_ports(xprt);
351edb267a6SChuck Lever }
352edb267a6SChuck Lever 
3539dc3b095SChuck Lever static void xs_update_peer_port(struct rpc_xprt *xprt)
3544b6473fbSChuck Lever {
3559dc3b095SChuck Lever 	kfree(xprt->address_strings[RPC_DISPLAY_HEX_PORT]);
3569dc3b095SChuck Lever 	kfree(xprt->address_strings[RPC_DISPLAY_PORT]);
3574b6473fbSChuck Lever 
3589dc3b095SChuck Lever 	xs_format_common_peer_ports(xprt);
359edb267a6SChuck Lever }
360edb267a6SChuck Lever 
361edb267a6SChuck Lever static void xs_free_peer_addresses(struct rpc_xprt *xprt)
362edb267a6SChuck Lever {
36333e01dc7SChuck Lever 	unsigned int i;
36433e01dc7SChuck Lever 
36533e01dc7SChuck Lever 	for (i = 0; i < RPC_DISPLAY_MAX; i++)
36633e01dc7SChuck Lever 		switch (i) {
36733e01dc7SChuck Lever 		case RPC_DISPLAY_PROTO:
36833e01dc7SChuck Lever 		case RPC_DISPLAY_NETID:
36933e01dc7SChuck Lever 			continue;
37033e01dc7SChuck Lever 		default:
37133e01dc7SChuck Lever 			kfree(xprt->address_strings[i]);
37233e01dc7SChuck Lever 		}
373edb267a6SChuck Lever }
374edb267a6SChuck Lever 
375b4b5cc85SChuck Lever #define XS_SENDMSG_FLAGS	(MSG_DONTWAIT | MSG_NOSIGNAL)
376b4b5cc85SChuck Lever 
37724c5684bSTrond Myklebust static int xs_send_kvec(struct socket *sock, struct sockaddr *addr, int addrlen, struct kvec *vec, unsigned int base, int more)
378b4b5cc85SChuck Lever {
379b4b5cc85SChuck Lever 	struct msghdr msg = {
380b4b5cc85SChuck Lever 		.msg_name	= addr,
381b4b5cc85SChuck Lever 		.msg_namelen	= addrlen,
38224c5684bSTrond Myklebust 		.msg_flags	= XS_SENDMSG_FLAGS | (more ? MSG_MORE : 0),
38324c5684bSTrond Myklebust 	};
38424c5684bSTrond Myklebust 	struct kvec iov = {
38524c5684bSTrond Myklebust 		.iov_base	= vec->iov_base + base,
38624c5684bSTrond Myklebust 		.iov_len	= vec->iov_len - base,
387b4b5cc85SChuck Lever 	};
388b4b5cc85SChuck Lever 
38924c5684bSTrond Myklebust 	if (iov.iov_len != 0)
390b4b5cc85SChuck Lever 		return kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len);
391b4b5cc85SChuck Lever 	return kernel_sendmsg(sock, &msg, NULL, 0, 0);
392b4b5cc85SChuck Lever }
393b4b5cc85SChuck Lever 
39424c5684bSTrond Myklebust static int xs_send_pagedata(struct socket *sock, struct xdr_buf *xdr, unsigned int base, int more)
395b4b5cc85SChuck Lever {
39624c5684bSTrond Myklebust 	struct page **ppage;
39724c5684bSTrond Myklebust 	unsigned int remainder;
39824c5684bSTrond Myklebust 	int err, sent = 0;
399b4b5cc85SChuck Lever 
40024c5684bSTrond Myklebust 	remainder = xdr->page_len - base;
40124c5684bSTrond Myklebust 	base += xdr->page_base;
40224c5684bSTrond Myklebust 	ppage = xdr->pages + (base >> PAGE_SHIFT);
40324c5684bSTrond Myklebust 	base &= ~PAGE_MASK;
40424c5684bSTrond Myklebust 	for(;;) {
40524c5684bSTrond Myklebust 		unsigned int len = min_t(unsigned int, PAGE_SIZE - base, remainder);
40624c5684bSTrond Myklebust 		int flags = XS_SENDMSG_FLAGS;
40724c5684bSTrond Myklebust 
40824c5684bSTrond Myklebust 		remainder -= len;
40924c5684bSTrond Myklebust 		if (remainder != 0 || more)
41024c5684bSTrond Myklebust 			flags |= MSG_MORE;
41124c5684bSTrond Myklebust 		err = sock->ops->sendpage(sock, *ppage, base, len, flags);
41224c5684bSTrond Myklebust 		if (remainder == 0 || err != len)
41324c5684bSTrond Myklebust 			break;
41424c5684bSTrond Myklebust 		sent += err;
41524c5684bSTrond Myklebust 		ppage++;
41624c5684bSTrond Myklebust 		base = 0;
41724c5684bSTrond Myklebust 	}
41824c5684bSTrond Myklebust 	if (sent == 0)
41924c5684bSTrond Myklebust 		return err;
42024c5684bSTrond Myklebust 	if (err > 0)
42124c5684bSTrond Myklebust 		sent += err;
42224c5684bSTrond Myklebust 	return sent;
423b4b5cc85SChuck Lever }
424b4b5cc85SChuck Lever 
4259903cd1cSChuck Lever /**
4269903cd1cSChuck Lever  * xs_sendpages - write pages directly to a socket
4279903cd1cSChuck Lever  * @sock: socket to send on
4289903cd1cSChuck Lever  * @addr: UDP only -- address of destination
4299903cd1cSChuck Lever  * @addrlen: UDP only -- length of destination address
4309903cd1cSChuck Lever  * @xdr: buffer containing this request
4319903cd1cSChuck Lever  * @base: starting position in the buffer
4329903cd1cSChuck Lever  *
433a246b010SChuck Lever  */
43424c5684bSTrond Myklebust static int xs_sendpages(struct socket *sock, struct sockaddr *addr, int addrlen, struct xdr_buf *xdr, unsigned int base)
435a246b010SChuck Lever {
43624c5684bSTrond Myklebust 	unsigned int remainder = xdr->len - base;
43724c5684bSTrond Myklebust 	int err, sent = 0;
438a246b010SChuck Lever 
439262965f5SChuck Lever 	if (unlikely(!sock))
440fba91afbSTrond Myklebust 		return -ENOTSOCK;
441262965f5SChuck Lever 
442262965f5SChuck Lever 	clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags);
44324c5684bSTrond Myklebust 	if (base != 0) {
44424c5684bSTrond Myklebust 		addr = NULL;
44524c5684bSTrond Myklebust 		addrlen = 0;
44624c5684bSTrond Myklebust 	}
447262965f5SChuck Lever 
44824c5684bSTrond Myklebust 	if (base < xdr->head[0].iov_len || addr != NULL) {
44924c5684bSTrond Myklebust 		unsigned int len = xdr->head[0].iov_len - base;
45024c5684bSTrond Myklebust 		remainder -= len;
45124c5684bSTrond Myklebust 		err = xs_send_kvec(sock, addr, addrlen, &xdr->head[0], base, remainder != 0);
45224c5684bSTrond Myklebust 		if (remainder == 0 || err != len)
453a246b010SChuck Lever 			goto out;
45424c5684bSTrond Myklebust 		sent += err;
455a246b010SChuck Lever 		base = 0;
456a246b010SChuck Lever 	} else
45724c5684bSTrond Myklebust 		base -= xdr->head[0].iov_len;
458a246b010SChuck Lever 
45924c5684bSTrond Myklebust 	if (base < xdr->page_len) {
46024c5684bSTrond Myklebust 		unsigned int len = xdr->page_len - base;
46124c5684bSTrond Myklebust 		remainder -= len;
46224c5684bSTrond Myklebust 		err = xs_send_pagedata(sock, xdr, base, remainder != 0);
46324c5684bSTrond Myklebust 		if (remainder == 0 || err != len)
464a246b010SChuck Lever 			goto out;
46524c5684bSTrond Myklebust 		sent += err;
466a246b010SChuck Lever 		base = 0;
46724c5684bSTrond Myklebust 	} else
46824c5684bSTrond Myklebust 		base -= xdr->page_len;
46924c5684bSTrond Myklebust 
47024c5684bSTrond Myklebust 	if (base >= xdr->tail[0].iov_len)
47124c5684bSTrond Myklebust 		return sent;
47224c5684bSTrond Myklebust 	err = xs_send_kvec(sock, NULL, 0, &xdr->tail[0], base, 0);
473a246b010SChuck Lever out:
47424c5684bSTrond Myklebust 	if (sent == 0)
47524c5684bSTrond Myklebust 		return err;
47624c5684bSTrond Myklebust 	if (err > 0)
47724c5684bSTrond Myklebust 		sent += err;
47824c5684bSTrond Myklebust 	return sent;
479a246b010SChuck Lever }
480a246b010SChuck Lever 
481b6ddf64fSTrond Myklebust static void xs_nospace_callback(struct rpc_task *task)
482b6ddf64fSTrond Myklebust {
483b6ddf64fSTrond Myklebust 	struct sock_xprt *transport = container_of(task->tk_rqstp->rq_xprt, struct sock_xprt, xprt);
484b6ddf64fSTrond Myklebust 
485b6ddf64fSTrond Myklebust 	transport->inet->sk_write_pending--;
486b6ddf64fSTrond Myklebust 	clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
487b6ddf64fSTrond Myklebust }
488b6ddf64fSTrond Myklebust 
4899903cd1cSChuck Lever /**
490262965f5SChuck Lever  * xs_nospace - place task on wait queue if transmit was incomplete
491262965f5SChuck Lever  * @task: task to put to sleep
4929903cd1cSChuck Lever  *
493a246b010SChuck Lever  */
4945e3771ceSTrond Myklebust static int xs_nospace(struct rpc_task *task)
495a246b010SChuck Lever {
496262965f5SChuck Lever 	struct rpc_rqst *req = task->tk_rqstp;
497262965f5SChuck Lever 	struct rpc_xprt *xprt = req->rq_xprt;
498ee0ac0c2SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
49924ca9a84STrond Myklebust 	int ret = -EAGAIN;
500a246b010SChuck Lever 
50146121cf7SChuck Lever 	dprintk("RPC: %5u xmit incomplete (%u left of %u)\n",
502262965f5SChuck Lever 			task->tk_pid, req->rq_slen - req->rq_bytes_sent,
503262965f5SChuck Lever 			req->rq_slen);
504a246b010SChuck Lever 
505262965f5SChuck Lever 	/* Protect against races with write_space */
506262965f5SChuck Lever 	spin_lock_bh(&xprt->transport_lock);
507a246b010SChuck Lever 
508262965f5SChuck Lever 	/* Don't race with disconnect */
509b6ddf64fSTrond Myklebust 	if (xprt_connected(xprt)) {
510b6ddf64fSTrond Myklebust 		if (test_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags)) {
511b6ddf64fSTrond Myklebust 			/*
512b6ddf64fSTrond Myklebust 			 * Notify TCP that we're limited by the application
513b6ddf64fSTrond Myklebust 			 * window size
514b6ddf64fSTrond Myklebust 			 */
515b6ddf64fSTrond Myklebust 			set_bit(SOCK_NOSPACE, &transport->sock->flags);
516b6ddf64fSTrond Myklebust 			transport->inet->sk_write_pending++;
517b6ddf64fSTrond Myklebust 			/* ...and wait for more buffer space */
518b6ddf64fSTrond Myklebust 			xprt_wait_for_buffer_space(task, xs_nospace_callback);
519b6ddf64fSTrond Myklebust 		}
520b6ddf64fSTrond Myklebust 	} else {
521b6ddf64fSTrond Myklebust 		clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
5225e3771ceSTrond Myklebust 		ret = -ENOTCONN;
523b6ddf64fSTrond Myklebust 	}
524a246b010SChuck Lever 
525262965f5SChuck Lever 	spin_unlock_bh(&xprt->transport_lock);
5265e3771ceSTrond Myklebust 	return ret;
527a246b010SChuck Lever }
528a246b010SChuck Lever 
52961677eeeSChuck Lever /*
53061677eeeSChuck Lever  * Construct a stream transport record marker in @buf.
53161677eeeSChuck Lever  */
53261677eeeSChuck Lever static inline void xs_encode_stream_record_marker(struct xdr_buf *buf)
53361677eeeSChuck Lever {
53461677eeeSChuck Lever 	u32 reclen = buf->len - sizeof(rpc_fraghdr);
53561677eeeSChuck Lever 	rpc_fraghdr *base = buf->head[0].iov_base;
53661677eeeSChuck Lever 	*base = cpu_to_be32(RPC_LAST_STREAM_FRAGMENT | reclen);
53761677eeeSChuck Lever }
53861677eeeSChuck Lever 
5399903cd1cSChuck Lever /**
540176e21eeSChuck Lever  * xs_local_send_request - write an RPC request to an AF_LOCAL socket
541176e21eeSChuck Lever  * @task: RPC task that manages the state of an RPC request
542176e21eeSChuck Lever  *
543176e21eeSChuck Lever  * Return values:
544176e21eeSChuck Lever  *        0:	The request has been sent
545176e21eeSChuck Lever  *   EAGAIN:	The socket was blocked, please call again later to
546176e21eeSChuck Lever  *		complete the request
547176e21eeSChuck Lever  * ENOTCONN:	Caller needs to invoke connect logic then call again
548176e21eeSChuck Lever  *    other:	Some other error occured, the request was not sent
549176e21eeSChuck Lever  */
550176e21eeSChuck Lever static int xs_local_send_request(struct rpc_task *task)
551176e21eeSChuck Lever {
552176e21eeSChuck Lever 	struct rpc_rqst *req = task->tk_rqstp;
553176e21eeSChuck Lever 	struct rpc_xprt *xprt = req->rq_xprt;
554176e21eeSChuck Lever 	struct sock_xprt *transport =
555176e21eeSChuck Lever 				container_of(xprt, struct sock_xprt, xprt);
556176e21eeSChuck Lever 	struct xdr_buf *xdr = &req->rq_snd_buf;
557176e21eeSChuck Lever 	int status;
558176e21eeSChuck Lever 
559176e21eeSChuck Lever 	xs_encode_stream_record_marker(&req->rq_snd_buf);
560176e21eeSChuck Lever 
561176e21eeSChuck Lever 	xs_pktdump("packet data:",
562176e21eeSChuck Lever 			req->rq_svec->iov_base, req->rq_svec->iov_len);
563176e21eeSChuck Lever 
564176e21eeSChuck Lever 	status = xs_sendpages(transport->sock, NULL, 0,
565176e21eeSChuck Lever 						xdr, req->rq_bytes_sent);
566176e21eeSChuck Lever 	dprintk("RPC:       %s(%u) = %d\n",
567176e21eeSChuck Lever 			__func__, xdr->len - req->rq_bytes_sent, status);
568176e21eeSChuck Lever 	if (likely(status >= 0)) {
569176e21eeSChuck Lever 		req->rq_bytes_sent += status;
570176e21eeSChuck Lever 		req->rq_xmit_bytes_sent += status;
571176e21eeSChuck Lever 		if (likely(req->rq_bytes_sent >= req->rq_slen)) {
572176e21eeSChuck Lever 			req->rq_bytes_sent = 0;
573176e21eeSChuck Lever 			return 0;
574176e21eeSChuck Lever 		}
575176e21eeSChuck Lever 		status = -EAGAIN;
576176e21eeSChuck Lever 	}
577176e21eeSChuck Lever 
578176e21eeSChuck Lever 	switch (status) {
579176e21eeSChuck Lever 	case -EAGAIN:
580176e21eeSChuck Lever 		status = xs_nospace(task);
581176e21eeSChuck Lever 		break;
582176e21eeSChuck Lever 	default:
583176e21eeSChuck Lever 		dprintk("RPC:       sendmsg returned unrecognized error %d\n",
584176e21eeSChuck Lever 			-status);
585176e21eeSChuck Lever 	case -EPIPE:
586176e21eeSChuck Lever 		xs_close(xprt);
587176e21eeSChuck Lever 		status = -ENOTCONN;
588176e21eeSChuck Lever 	}
589176e21eeSChuck Lever 
590176e21eeSChuck Lever 	return status;
591176e21eeSChuck Lever }
592176e21eeSChuck Lever 
593176e21eeSChuck Lever /**
594262965f5SChuck Lever  * xs_udp_send_request - write an RPC request to a UDP socket
5959903cd1cSChuck Lever  * @task: address of RPC task that manages the state of an RPC request
5969903cd1cSChuck Lever  *
5979903cd1cSChuck Lever  * Return values:
5989903cd1cSChuck Lever  *        0:	The request has been sent
5999903cd1cSChuck Lever  *   EAGAIN:	The socket was blocked, please call again later to
6009903cd1cSChuck Lever  *		complete the request
601262965f5SChuck Lever  * ENOTCONN:	Caller needs to invoke connect logic then call again
60225985edcSLucas De Marchi  *    other:	Some other error occurred, the request was not sent
6039903cd1cSChuck Lever  */
604262965f5SChuck Lever static int xs_udp_send_request(struct rpc_task *task)
605a246b010SChuck Lever {
606a246b010SChuck Lever 	struct rpc_rqst *req = task->tk_rqstp;
607a246b010SChuck Lever 	struct rpc_xprt *xprt = req->rq_xprt;
608ee0ac0c2SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
609262965f5SChuck Lever 	struct xdr_buf *xdr = &req->rq_snd_buf;
610262965f5SChuck Lever 	int status;
611262965f5SChuck Lever 
612262965f5SChuck Lever 	xs_pktdump("packet data:",
613262965f5SChuck Lever 				req->rq_svec->iov_base,
614262965f5SChuck Lever 				req->rq_svec->iov_len);
615262965f5SChuck Lever 
61601d37c42STrond Myklebust 	if (!xprt_bound(xprt))
61701d37c42STrond Myklebust 		return -ENOTCONN;
618ee0ac0c2SChuck Lever 	status = xs_sendpages(transport->sock,
61995392c59SChuck Lever 			      xs_addr(xprt),
620ee0ac0c2SChuck Lever 			      xprt->addrlen, xdr,
621ee0ac0c2SChuck Lever 			      req->rq_bytes_sent);
622262965f5SChuck Lever 
623262965f5SChuck Lever 	dprintk("RPC:       xs_udp_send_request(%u) = %d\n",
624262965f5SChuck Lever 			xdr->len - req->rq_bytes_sent, status);
625262965f5SChuck Lever 
6262199700fSTrond Myklebust 	if (status >= 0) {
627d60dbb20STrond Myklebust 		req->rq_xmit_bytes_sent += status;
6282199700fSTrond Myklebust 		if (status >= req->rq_slen)
629262965f5SChuck Lever 			return 0;
630262965f5SChuck Lever 		/* Still some bytes left; set up for a retry later. */
631262965f5SChuck Lever 		status = -EAGAIN;
6322199700fSTrond Myklebust 	}
633262965f5SChuck Lever 
634262965f5SChuck Lever 	switch (status) {
635fba91afbSTrond Myklebust 	case -ENOTSOCK:
636fba91afbSTrond Myklebust 		status = -ENOTCONN;
637fba91afbSTrond Myklebust 		/* Should we call xs_close() here? */
638fba91afbSTrond Myklebust 		break;
639b6ddf64fSTrond Myklebust 	case -EAGAIN:
6405e3771ceSTrond Myklebust 		status = xs_nospace(task);
641b6ddf64fSTrond Myklebust 		break;
642c8485e4dSTrond Myklebust 	default:
643c8485e4dSTrond Myklebust 		dprintk("RPC:       sendmsg returned unrecognized error %d\n",
644c8485e4dSTrond Myklebust 			-status);
645262965f5SChuck Lever 	case -ENETUNREACH:
646262965f5SChuck Lever 	case -EPIPE:
647262965f5SChuck Lever 	case -ECONNREFUSED:
648262965f5SChuck Lever 		/* When the server has died, an ICMP port unreachable message
649262965f5SChuck Lever 		 * prompts ECONNREFUSED. */
650b6ddf64fSTrond Myklebust 		clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
651262965f5SChuck Lever 	}
6525fe46e9dSBian Naimeng 
653262965f5SChuck Lever 	return status;
654262965f5SChuck Lever }
655262965f5SChuck Lever 
656e06799f9STrond Myklebust /**
657e06799f9STrond Myklebust  * xs_tcp_shutdown - gracefully shut down a TCP socket
658e06799f9STrond Myklebust  * @xprt: transport
659e06799f9STrond Myklebust  *
660e06799f9STrond Myklebust  * Initiates a graceful shutdown of the TCP socket by calling the
661e06799f9STrond Myklebust  * equivalent of shutdown(SHUT_WR);
662e06799f9STrond Myklebust  */
663e06799f9STrond Myklebust static void xs_tcp_shutdown(struct rpc_xprt *xprt)
664e06799f9STrond Myklebust {
665e06799f9STrond Myklebust 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
666e06799f9STrond Myklebust 	struct socket *sock = transport->sock;
667e06799f9STrond Myklebust 
668e06799f9STrond Myklebust 	if (sock != NULL)
669e06799f9STrond Myklebust 		kernel_sock_shutdown(sock, SHUT_WR);
670e06799f9STrond Myklebust }
671e06799f9STrond Myklebust 
672262965f5SChuck Lever /**
673262965f5SChuck Lever  * xs_tcp_send_request - write an RPC request to a TCP socket
674262965f5SChuck Lever  * @task: address of RPC task that manages the state of an RPC request
675262965f5SChuck Lever  *
676262965f5SChuck Lever  * Return values:
677262965f5SChuck Lever  *        0:	The request has been sent
678262965f5SChuck Lever  *   EAGAIN:	The socket was blocked, please call again later to
679262965f5SChuck Lever  *		complete the request
680262965f5SChuck Lever  * ENOTCONN:	Caller needs to invoke connect logic then call again
68125985edcSLucas De Marchi  *    other:	Some other error occurred, the request was not sent
682262965f5SChuck Lever  *
683262965f5SChuck Lever  * XXX: In the case of soft timeouts, should we eventually give up
684262965f5SChuck Lever  *	if sendmsg is not able to make progress?
685262965f5SChuck Lever  */
686262965f5SChuck Lever static int xs_tcp_send_request(struct rpc_task *task)
687262965f5SChuck Lever {
688262965f5SChuck Lever 	struct rpc_rqst *req = task->tk_rqstp;
689262965f5SChuck Lever 	struct rpc_xprt *xprt = req->rq_xprt;
690ee0ac0c2SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
691262965f5SChuck Lever 	struct xdr_buf *xdr = &req->rq_snd_buf;
692b595bb15SChuck Lever 	int status;
693a246b010SChuck Lever 
69461677eeeSChuck Lever 	xs_encode_stream_record_marker(&req->rq_snd_buf);
695262965f5SChuck Lever 
696262965f5SChuck Lever 	xs_pktdump("packet data:",
697262965f5SChuck Lever 				req->rq_svec->iov_base,
698262965f5SChuck Lever 				req->rq_svec->iov_len);
699a246b010SChuck Lever 
700a246b010SChuck Lever 	/* Continue transmitting the packet/record. We must be careful
701a246b010SChuck Lever 	 * to cope with writespace callbacks arriving _after_ we have
702262965f5SChuck Lever 	 * called sendmsg(). */
703a246b010SChuck Lever 	while (1) {
704ee0ac0c2SChuck Lever 		status = xs_sendpages(transport->sock,
705ee0ac0c2SChuck Lever 					NULL, 0, xdr, req->rq_bytes_sent);
706a246b010SChuck Lever 
707262965f5SChuck Lever 		dprintk("RPC:       xs_tcp_send_request(%u) = %d\n",
708262965f5SChuck Lever 				xdr->len - req->rq_bytes_sent, status);
709262965f5SChuck Lever 
710262965f5SChuck Lever 		if (unlikely(status < 0))
711a246b010SChuck Lever 			break;
712a246b010SChuck Lever 
713a246b010SChuck Lever 		/* If we've sent the entire packet, immediately
714a246b010SChuck Lever 		 * reset the count of bytes sent. */
715262965f5SChuck Lever 		req->rq_bytes_sent += status;
716d60dbb20STrond Myklebust 		req->rq_xmit_bytes_sent += status;
717262965f5SChuck Lever 		if (likely(req->rq_bytes_sent >= req->rq_slen)) {
718a246b010SChuck Lever 			req->rq_bytes_sent = 0;
719a246b010SChuck Lever 			return 0;
720a246b010SChuck Lever 		}
721262965f5SChuck Lever 
72206b4b681STrond Myklebust 		if (status != 0)
72306b4b681STrond Myklebust 			continue;
724a246b010SChuck Lever 		status = -EAGAIN;
725a246b010SChuck Lever 		break;
726a246b010SChuck Lever 	}
727a246b010SChuck Lever 
728262965f5SChuck Lever 	switch (status) {
729fba91afbSTrond Myklebust 	case -ENOTSOCK:
730fba91afbSTrond Myklebust 		status = -ENOTCONN;
731fba91afbSTrond Myklebust 		/* Should we call xs_close() here? */
732fba91afbSTrond Myklebust 		break;
733262965f5SChuck Lever 	case -EAGAIN:
7345e3771ceSTrond Myklebust 		status = xs_nospace(task);
735262965f5SChuck Lever 		break;
736262965f5SChuck Lever 	default:
737262965f5SChuck Lever 		dprintk("RPC:       sendmsg returned unrecognized error %d\n",
738262965f5SChuck Lever 			-status);
739a246b010SChuck Lever 	case -ECONNRESET:
740e06799f9STrond Myklebust 		xs_tcp_shutdown(xprt);
741a246b010SChuck Lever 	case -ECONNREFUSED:
742a246b010SChuck Lever 	case -ENOTCONN:
743b9d2bb2eSTrond Myklebust 	case -EPIPE:
744a246b010SChuck Lever 		clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
745a246b010SChuck Lever 	}
7465fe46e9dSBian Naimeng 
747a246b010SChuck Lever 	return status;
748a246b010SChuck Lever }
749a246b010SChuck Lever 
7509903cd1cSChuck Lever /**
751e0ab53deSTrond Myklebust  * xs_tcp_release_xprt - clean up after a tcp transmission
752e0ab53deSTrond Myklebust  * @xprt: transport
753e0ab53deSTrond Myklebust  * @task: rpc task
754e0ab53deSTrond Myklebust  *
755e0ab53deSTrond Myklebust  * This cleans up if an error causes us to abort the transmission of a request.
756e0ab53deSTrond Myklebust  * In this case, the socket may need to be reset in order to avoid confusing
757e0ab53deSTrond Myklebust  * the server.
758e0ab53deSTrond Myklebust  */
759e0ab53deSTrond Myklebust static void xs_tcp_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
760e0ab53deSTrond Myklebust {
761e0ab53deSTrond Myklebust 	struct rpc_rqst *req;
762e0ab53deSTrond Myklebust 
763e0ab53deSTrond Myklebust 	if (task != xprt->snd_task)
764e0ab53deSTrond Myklebust 		return;
765e0ab53deSTrond Myklebust 	if (task == NULL)
766e0ab53deSTrond Myklebust 		goto out_release;
767e0ab53deSTrond Myklebust 	req = task->tk_rqstp;
76843cedbf0STrond Myklebust 	if (req == NULL)
76943cedbf0STrond Myklebust 		goto out_release;
770e0ab53deSTrond Myklebust 	if (req->rq_bytes_sent == 0)
771e0ab53deSTrond Myklebust 		goto out_release;
772e0ab53deSTrond Myklebust 	if (req->rq_bytes_sent == req->rq_snd_buf.len)
773e0ab53deSTrond Myklebust 		goto out_release;
774a4f0835cSTrond Myklebust 	set_bit(XPRT_CLOSE_WAIT, &xprt->state);
775e0ab53deSTrond Myklebust out_release:
776e0ab53deSTrond Myklebust 	xprt_release_xprt(xprt, task);
777e0ab53deSTrond Myklebust }
778e0ab53deSTrond Myklebust 
7792a9e1cfaSTrond Myklebust static void xs_save_old_callbacks(struct sock_xprt *transport, struct sock *sk)
7802a9e1cfaSTrond Myklebust {
7812a9e1cfaSTrond Myklebust 	transport->old_data_ready = sk->sk_data_ready;
7822a9e1cfaSTrond Myklebust 	transport->old_state_change = sk->sk_state_change;
7832a9e1cfaSTrond Myklebust 	transport->old_write_space = sk->sk_write_space;
7842a9e1cfaSTrond Myklebust }
7852a9e1cfaSTrond Myklebust 
7862a9e1cfaSTrond Myklebust static void xs_restore_old_callbacks(struct sock_xprt *transport, struct sock *sk)
7872a9e1cfaSTrond Myklebust {
7882a9e1cfaSTrond Myklebust 	sk->sk_data_ready = transport->old_data_ready;
7892a9e1cfaSTrond Myklebust 	sk->sk_state_change = transport->old_state_change;
7902a9e1cfaSTrond Myklebust 	sk->sk_write_space = transport->old_write_space;
7912a9e1cfaSTrond Myklebust }
7922a9e1cfaSTrond Myklebust 
793fe315e76SChuck Lever static void xs_reset_transport(struct sock_xprt *transport)
794a246b010SChuck Lever {
795ee0ac0c2SChuck Lever 	struct socket *sock = transport->sock;
796ee0ac0c2SChuck Lever 	struct sock *sk = transport->inet;
797a246b010SChuck Lever 
798fe315e76SChuck Lever 	if (sk == NULL)
799fe315e76SChuck Lever 		return;
8009903cd1cSChuck Lever 
801246408dcSTrond Myklebust 	transport->srcport = 0;
802246408dcSTrond Myklebust 
803a246b010SChuck Lever 	write_lock_bh(&sk->sk_callback_lock);
804ee0ac0c2SChuck Lever 	transport->inet = NULL;
805ee0ac0c2SChuck Lever 	transport->sock = NULL;
806a246b010SChuck Lever 
807a246b010SChuck Lever 	sk->sk_user_data = NULL;
8082a9e1cfaSTrond Myklebust 
8092a9e1cfaSTrond Myklebust 	xs_restore_old_callbacks(transport, sk);
810a246b010SChuck Lever 	write_unlock_bh(&sk->sk_callback_lock);
811a246b010SChuck Lever 
812a246b010SChuck Lever 	sk->sk_no_check = 0;
813a246b010SChuck Lever 
814a246b010SChuck Lever 	sock_release(sock);
815fe315e76SChuck Lever }
816fe315e76SChuck Lever 
817fe315e76SChuck Lever /**
818fe315e76SChuck Lever  * xs_close - close a socket
819fe315e76SChuck Lever  * @xprt: transport
820fe315e76SChuck Lever  *
821fe315e76SChuck Lever  * This is used when all requests are complete; ie, no DRC state remains
822fe315e76SChuck Lever  * on the server we want to save.
823f75e6745STrond Myklebust  *
824f75e6745STrond Myklebust  * The caller _must_ be holding XPRT_LOCKED in order to avoid issues with
825f75e6745STrond Myklebust  * xs_reset_transport() zeroing the socket from underneath a writer.
826fe315e76SChuck Lever  */
827fe315e76SChuck Lever static void xs_close(struct rpc_xprt *xprt)
828fe315e76SChuck Lever {
829fe315e76SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
830fe315e76SChuck Lever 
831fe315e76SChuck Lever 	dprintk("RPC:       xs_close xprt %p\n", xprt);
832fe315e76SChuck Lever 
833fe315e76SChuck Lever 	xs_reset_transport(transport);
83461d0a8e6SNeil Brown 	xprt->reestablish_timeout = 0;
835fe315e76SChuck Lever 
836632e3bdcSTrond Myklebust 	smp_mb__before_clear_bit();
8377d1e8255STrond Myklebust 	clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
838632e3bdcSTrond Myklebust 	clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
8393b948ae5STrond Myklebust 	clear_bit(XPRT_CLOSING, &xprt->state);
840632e3bdcSTrond Myklebust 	smp_mb__after_clear_bit();
84162da3b24STrond Myklebust 	xprt_disconnect_done(xprt);
842a246b010SChuck Lever }
843a246b010SChuck Lever 
844f75e6745STrond Myklebust static void xs_tcp_close(struct rpc_xprt *xprt)
845f75e6745STrond Myklebust {
846f75e6745STrond Myklebust 	if (test_and_clear_bit(XPRT_CONNECTION_CLOSE, &xprt->state))
847f75e6745STrond Myklebust 		xs_close(xprt);
848f75e6745STrond Myklebust 	else
849f75e6745STrond Myklebust 		xs_tcp_shutdown(xprt);
850f75e6745STrond Myklebust }
851f75e6745STrond Myklebust 
852190b1ecfSJ. Bruce Fields static void xs_local_destroy(struct rpc_xprt *xprt)
853190b1ecfSJ. Bruce Fields {
854190b1ecfSJ. Bruce Fields 	xs_close(xprt);
855190b1ecfSJ. Bruce Fields 	xs_free_peer_addresses(xprt);
856190b1ecfSJ. Bruce Fields 	xprt_free(xprt);
857190b1ecfSJ. Bruce Fields 	module_put(THIS_MODULE);
858190b1ecfSJ. Bruce Fields }
859190b1ecfSJ. Bruce Fields 
8609903cd1cSChuck Lever /**
8619903cd1cSChuck Lever  * xs_destroy - prepare to shutdown a transport
8629903cd1cSChuck Lever  * @xprt: doomed transport
8639903cd1cSChuck Lever  *
8649903cd1cSChuck Lever  */
8659903cd1cSChuck Lever static void xs_destroy(struct rpc_xprt *xprt)
866a246b010SChuck Lever {
867c8475461SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
868c8475461SChuck Lever 
8699903cd1cSChuck Lever 	dprintk("RPC:       xs_destroy xprt %p\n", xprt);
8709903cd1cSChuck Lever 
871afe2c511STejun Heo 	cancel_delayed_work_sync(&transport->connect_worker);
872a246b010SChuck Lever 
873190b1ecfSJ. Bruce Fields 	xs_local_destroy(xprt);
874a246b010SChuck Lever }
875a246b010SChuck Lever 
8769903cd1cSChuck Lever static inline struct rpc_xprt *xprt_from_sock(struct sock *sk)
8779903cd1cSChuck Lever {
8789903cd1cSChuck Lever 	return (struct rpc_xprt *) sk->sk_user_data;
8799903cd1cSChuck Lever }
8809903cd1cSChuck Lever 
881176e21eeSChuck Lever static int xs_local_copy_to_xdr(struct xdr_buf *xdr, struct sk_buff *skb)
882176e21eeSChuck Lever {
883176e21eeSChuck Lever 	struct xdr_skb_reader desc = {
884176e21eeSChuck Lever 		.skb		= skb,
885176e21eeSChuck Lever 		.offset		= sizeof(rpc_fraghdr),
886176e21eeSChuck Lever 		.count		= skb->len - sizeof(rpc_fraghdr),
887176e21eeSChuck Lever 	};
888176e21eeSChuck Lever 
889176e21eeSChuck Lever 	if (xdr_partial_copy_from_skb(xdr, 0, &desc, xdr_skb_read_bits) < 0)
890176e21eeSChuck Lever 		return -1;
891176e21eeSChuck Lever 	if (desc.count)
892176e21eeSChuck Lever 		return -1;
893176e21eeSChuck Lever 	return 0;
894176e21eeSChuck Lever }
895176e21eeSChuck Lever 
896176e21eeSChuck Lever /**
897176e21eeSChuck Lever  * xs_local_data_ready - "data ready" callback for AF_LOCAL sockets
898176e21eeSChuck Lever  * @sk: socket with data to read
899176e21eeSChuck Lever  * @len: how much data to read
900176e21eeSChuck Lever  *
901176e21eeSChuck Lever  * Currently this assumes we can read the whole reply in a single gulp.
902176e21eeSChuck Lever  */
903176e21eeSChuck Lever static void xs_local_data_ready(struct sock *sk, int len)
904176e21eeSChuck Lever {
905176e21eeSChuck Lever 	struct rpc_task *task;
906176e21eeSChuck Lever 	struct rpc_xprt *xprt;
907176e21eeSChuck Lever 	struct rpc_rqst *rovr;
908176e21eeSChuck Lever 	struct sk_buff *skb;
909176e21eeSChuck Lever 	int err, repsize, copied;
910176e21eeSChuck Lever 	u32 _xid;
911176e21eeSChuck Lever 	__be32 *xp;
912176e21eeSChuck Lever 
913176e21eeSChuck Lever 	read_lock_bh(&sk->sk_callback_lock);
914176e21eeSChuck Lever 	dprintk("RPC:       %s...\n", __func__);
915176e21eeSChuck Lever 	xprt = xprt_from_sock(sk);
916176e21eeSChuck Lever 	if (xprt == NULL)
917176e21eeSChuck Lever 		goto out;
918176e21eeSChuck Lever 
919176e21eeSChuck Lever 	skb = skb_recv_datagram(sk, 0, 1, &err);
920176e21eeSChuck Lever 	if (skb == NULL)
921176e21eeSChuck Lever 		goto out;
922176e21eeSChuck Lever 
923176e21eeSChuck Lever 	repsize = skb->len - sizeof(rpc_fraghdr);
924176e21eeSChuck Lever 	if (repsize < 4) {
925176e21eeSChuck Lever 		dprintk("RPC:       impossible RPC reply size %d\n", repsize);
926176e21eeSChuck Lever 		goto dropit;
927176e21eeSChuck Lever 	}
928176e21eeSChuck Lever 
929176e21eeSChuck Lever 	/* Copy the XID from the skb... */
930176e21eeSChuck Lever 	xp = skb_header_pointer(skb, sizeof(rpc_fraghdr), sizeof(_xid), &_xid);
931176e21eeSChuck Lever 	if (xp == NULL)
932176e21eeSChuck Lever 		goto dropit;
933176e21eeSChuck Lever 
934176e21eeSChuck Lever 	/* Look up and lock the request corresponding to the given XID */
935176e21eeSChuck Lever 	spin_lock(&xprt->transport_lock);
936176e21eeSChuck Lever 	rovr = xprt_lookup_rqst(xprt, *xp);
937176e21eeSChuck Lever 	if (!rovr)
938176e21eeSChuck Lever 		goto out_unlock;
939176e21eeSChuck Lever 	task = rovr->rq_task;
940176e21eeSChuck Lever 
941176e21eeSChuck Lever 	copied = rovr->rq_private_buf.buflen;
942176e21eeSChuck Lever 	if (copied > repsize)
943176e21eeSChuck Lever 		copied = repsize;
944176e21eeSChuck Lever 
945176e21eeSChuck Lever 	if (xs_local_copy_to_xdr(&rovr->rq_private_buf, skb)) {
946176e21eeSChuck Lever 		dprintk("RPC:       sk_buff copy failed\n");
947176e21eeSChuck Lever 		goto out_unlock;
948176e21eeSChuck Lever 	}
949176e21eeSChuck Lever 
950176e21eeSChuck Lever 	xprt_complete_rqst(task, copied);
951176e21eeSChuck Lever 
952176e21eeSChuck Lever  out_unlock:
953176e21eeSChuck Lever 	spin_unlock(&xprt->transport_lock);
954176e21eeSChuck Lever  dropit:
955176e21eeSChuck Lever 	skb_free_datagram(sk, skb);
956176e21eeSChuck Lever  out:
957176e21eeSChuck Lever 	read_unlock_bh(&sk->sk_callback_lock);
958176e21eeSChuck Lever }
959176e21eeSChuck Lever 
9609903cd1cSChuck Lever /**
9619903cd1cSChuck Lever  * xs_udp_data_ready - "data ready" callback for UDP sockets
9629903cd1cSChuck Lever  * @sk: socket with data to read
9639903cd1cSChuck Lever  * @len: how much data to read
9649903cd1cSChuck Lever  *
965a246b010SChuck Lever  */
9669903cd1cSChuck Lever static void xs_udp_data_ready(struct sock *sk, int len)
967a246b010SChuck Lever {
968a246b010SChuck Lever 	struct rpc_task *task;
969a246b010SChuck Lever 	struct rpc_xprt *xprt;
970a246b010SChuck Lever 	struct rpc_rqst *rovr;
971a246b010SChuck Lever 	struct sk_buff *skb;
972a246b010SChuck Lever 	int err, repsize, copied;
973d8ed029dSAlexey Dobriyan 	u32 _xid;
974d8ed029dSAlexey Dobriyan 	__be32 *xp;
975a246b010SChuck Lever 
976f064af1eSEric Dumazet 	read_lock_bh(&sk->sk_callback_lock);
9779903cd1cSChuck Lever 	dprintk("RPC:       xs_udp_data_ready...\n");
9789903cd1cSChuck Lever 	if (!(xprt = xprt_from_sock(sk)))
979a246b010SChuck Lever 		goto out;
980a246b010SChuck Lever 
981a246b010SChuck Lever 	if ((skb = skb_recv_datagram(sk, 0, 1, &err)) == NULL)
982a246b010SChuck Lever 		goto out;
983a246b010SChuck Lever 
984a246b010SChuck Lever 	repsize = skb->len - sizeof(struct udphdr);
985a246b010SChuck Lever 	if (repsize < 4) {
9869903cd1cSChuck Lever 		dprintk("RPC:       impossible RPC reply size %d!\n", repsize);
987a246b010SChuck Lever 		goto dropit;
988a246b010SChuck Lever 	}
989a246b010SChuck Lever 
990a246b010SChuck Lever 	/* Copy the XID from the skb... */
991a246b010SChuck Lever 	xp = skb_header_pointer(skb, sizeof(struct udphdr),
992a246b010SChuck Lever 				sizeof(_xid), &_xid);
993a246b010SChuck Lever 	if (xp == NULL)
994a246b010SChuck Lever 		goto dropit;
995a246b010SChuck Lever 
996a246b010SChuck Lever 	/* Look up and lock the request corresponding to the given XID */
9974a0f8c04SChuck Lever 	spin_lock(&xprt->transport_lock);
998a246b010SChuck Lever 	rovr = xprt_lookup_rqst(xprt, *xp);
999a246b010SChuck Lever 	if (!rovr)
1000a246b010SChuck Lever 		goto out_unlock;
1001a246b010SChuck Lever 	task = rovr->rq_task;
1002a246b010SChuck Lever 
1003a246b010SChuck Lever 	if ((copied = rovr->rq_private_buf.buflen) > repsize)
1004a246b010SChuck Lever 		copied = repsize;
1005a246b010SChuck Lever 
1006a246b010SChuck Lever 	/* Suck it into the iovec, verify checksum if not done by hw. */
10071781f7f5SHerbert Xu 	if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) {
10081781f7f5SHerbert Xu 		UDPX_INC_STATS_BH(sk, UDP_MIB_INERRORS);
1009a246b010SChuck Lever 		goto out_unlock;
10101781f7f5SHerbert Xu 	}
10111781f7f5SHerbert Xu 
10121781f7f5SHerbert Xu 	UDPX_INC_STATS_BH(sk, UDP_MIB_INDATAGRAMS);
1013a246b010SChuck Lever 
10146a24dfb6STrond Myklebust 	xprt_adjust_cwnd(xprt, task, copied);
10151570c1e4SChuck Lever 	xprt_complete_rqst(task, copied);
1016a246b010SChuck Lever 
1017a246b010SChuck Lever  out_unlock:
10184a0f8c04SChuck Lever 	spin_unlock(&xprt->transport_lock);
1019a246b010SChuck Lever  dropit:
1020a246b010SChuck Lever 	skb_free_datagram(sk, skb);
1021a246b010SChuck Lever  out:
1022f064af1eSEric Dumazet 	read_unlock_bh(&sk->sk_callback_lock);
1023a246b010SChuck Lever }
1024a246b010SChuck Lever 
1025a519fc7aSTrond Myklebust /*
1026a519fc7aSTrond Myklebust  * Helper function to force a TCP close if the server is sending
1027a519fc7aSTrond Myklebust  * junk and/or it has put us in CLOSE_WAIT
1028a519fc7aSTrond Myklebust  */
1029a519fc7aSTrond Myklebust static void xs_tcp_force_close(struct rpc_xprt *xprt)
1030a519fc7aSTrond Myklebust {
1031a519fc7aSTrond Myklebust 	set_bit(XPRT_CONNECTION_CLOSE, &xprt->state);
1032a519fc7aSTrond Myklebust 	xprt_force_disconnect(xprt);
1033a519fc7aSTrond Myklebust }
1034a519fc7aSTrond Myklebust 
1035dd456471SChuck Lever static inline void xs_tcp_read_fraghdr(struct rpc_xprt *xprt, struct xdr_skb_reader *desc)
1036a246b010SChuck Lever {
103751971139SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1038a246b010SChuck Lever 	size_t len, used;
1039a246b010SChuck Lever 	char *p;
1040a246b010SChuck Lever 
104151971139SChuck Lever 	p = ((char *) &transport->tcp_fraghdr) + transport->tcp_offset;
104251971139SChuck Lever 	len = sizeof(transport->tcp_fraghdr) - transport->tcp_offset;
10439d292316SChuck Lever 	used = xdr_skb_read_bits(desc, p, len);
104451971139SChuck Lever 	transport->tcp_offset += used;
1045a246b010SChuck Lever 	if (used != len)
1046a246b010SChuck Lever 		return;
1047808012fbSChuck Lever 
104851971139SChuck Lever 	transport->tcp_reclen = ntohl(transport->tcp_fraghdr);
104951971139SChuck Lever 	if (transport->tcp_reclen & RPC_LAST_STREAM_FRAGMENT)
1050e136d092SChuck Lever 		transport->tcp_flags |= TCP_RCV_LAST_FRAG;
1051a246b010SChuck Lever 	else
1052e136d092SChuck Lever 		transport->tcp_flags &= ~TCP_RCV_LAST_FRAG;
105351971139SChuck Lever 	transport->tcp_reclen &= RPC_FRAGMENT_SIZE_MASK;
1054808012fbSChuck Lever 
1055e136d092SChuck Lever 	transport->tcp_flags &= ~TCP_RCV_COPY_FRAGHDR;
105651971139SChuck Lever 	transport->tcp_offset = 0;
1057808012fbSChuck Lever 
1058a246b010SChuck Lever 	/* Sanity check of the record length */
105918dca02aSRicardo Labiaga 	if (unlikely(transport->tcp_reclen < 8)) {
10609903cd1cSChuck Lever 		dprintk("RPC:       invalid TCP record fragment length\n");
1061a519fc7aSTrond Myklebust 		xs_tcp_force_close(xprt);
10629903cd1cSChuck Lever 		return;
1063a246b010SChuck Lever 	}
1064a246b010SChuck Lever 	dprintk("RPC:       reading TCP record fragment of length %d\n",
106551971139SChuck Lever 			transport->tcp_reclen);
1066a246b010SChuck Lever }
1067a246b010SChuck Lever 
106851971139SChuck Lever static void xs_tcp_check_fraghdr(struct sock_xprt *transport)
1069a246b010SChuck Lever {
107051971139SChuck Lever 	if (transport->tcp_offset == transport->tcp_reclen) {
1071e136d092SChuck Lever 		transport->tcp_flags |= TCP_RCV_COPY_FRAGHDR;
107251971139SChuck Lever 		transport->tcp_offset = 0;
1073e136d092SChuck Lever 		if (transport->tcp_flags & TCP_RCV_LAST_FRAG) {
1074e136d092SChuck Lever 			transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1075e136d092SChuck Lever 			transport->tcp_flags |= TCP_RCV_COPY_XID;
107651971139SChuck Lever 			transport->tcp_copied = 0;
1077a246b010SChuck Lever 		}
1078a246b010SChuck Lever 	}
1079a246b010SChuck Lever }
1080a246b010SChuck Lever 
1081dd456471SChuck Lever static inline void xs_tcp_read_xid(struct sock_xprt *transport, struct xdr_skb_reader *desc)
1082a246b010SChuck Lever {
1083a246b010SChuck Lever 	size_t len, used;
1084a246b010SChuck Lever 	char *p;
1085a246b010SChuck Lever 
108651971139SChuck Lever 	len = sizeof(transport->tcp_xid) - transport->tcp_offset;
1087a246b010SChuck Lever 	dprintk("RPC:       reading XID (%Zu bytes)\n", len);
108851971139SChuck Lever 	p = ((char *) &transport->tcp_xid) + transport->tcp_offset;
10899d292316SChuck Lever 	used = xdr_skb_read_bits(desc, p, len);
109051971139SChuck Lever 	transport->tcp_offset += used;
1091a246b010SChuck Lever 	if (used != len)
1092a246b010SChuck Lever 		return;
1093e136d092SChuck Lever 	transport->tcp_flags &= ~TCP_RCV_COPY_XID;
1094f4a2e418SRicardo Labiaga 	transport->tcp_flags |= TCP_RCV_READ_CALLDIR;
109551971139SChuck Lever 	transport->tcp_copied = 4;
109618dca02aSRicardo Labiaga 	dprintk("RPC:       reading %s XID %08x\n",
109718dca02aSRicardo Labiaga 			(transport->tcp_flags & TCP_RPC_REPLY) ? "reply for"
109818dca02aSRicardo Labiaga 							      : "request with",
109951971139SChuck Lever 			ntohl(transport->tcp_xid));
110051971139SChuck Lever 	xs_tcp_check_fraghdr(transport);
1101a246b010SChuck Lever }
1102a246b010SChuck Lever 
110318dca02aSRicardo Labiaga static inline void xs_tcp_read_calldir(struct sock_xprt *transport,
110418dca02aSRicardo Labiaga 				       struct xdr_skb_reader *desc)
1105a246b010SChuck Lever {
110618dca02aSRicardo Labiaga 	size_t len, used;
110718dca02aSRicardo Labiaga 	u32 offset;
1108b76ce561STrond Myklebust 	char *p;
110918dca02aSRicardo Labiaga 
111018dca02aSRicardo Labiaga 	/*
111118dca02aSRicardo Labiaga 	 * We want transport->tcp_offset to be 8 at the end of this routine
111218dca02aSRicardo Labiaga 	 * (4 bytes for the xid and 4 bytes for the call/reply flag).
111318dca02aSRicardo Labiaga 	 * When this function is called for the first time,
111418dca02aSRicardo Labiaga 	 * transport->tcp_offset is 4 (after having already read the xid).
111518dca02aSRicardo Labiaga 	 */
111618dca02aSRicardo Labiaga 	offset = transport->tcp_offset - sizeof(transport->tcp_xid);
1117b76ce561STrond Myklebust 	len = sizeof(transport->tcp_calldir) - offset;
111818dca02aSRicardo Labiaga 	dprintk("RPC:       reading CALL/REPLY flag (%Zu bytes)\n", len);
1119b76ce561STrond Myklebust 	p = ((char *) &transport->tcp_calldir) + offset;
1120b76ce561STrond Myklebust 	used = xdr_skb_read_bits(desc, p, len);
112118dca02aSRicardo Labiaga 	transport->tcp_offset += used;
112218dca02aSRicardo Labiaga 	if (used != len)
112318dca02aSRicardo Labiaga 		return;
1124f4a2e418SRicardo Labiaga 	transport->tcp_flags &= ~TCP_RCV_READ_CALLDIR;
1125f4a2e418SRicardo Labiaga 	/*
1126f4a2e418SRicardo Labiaga 	 * We don't yet have the XDR buffer, so we will write the calldir
1127f4a2e418SRicardo Labiaga 	 * out after we get the buffer from the 'struct rpc_rqst'
1128f4a2e418SRicardo Labiaga 	 */
1129b76ce561STrond Myklebust 	switch (ntohl(transport->tcp_calldir)) {
1130b76ce561STrond Myklebust 	case RPC_REPLY:
1131b76ce561STrond Myklebust 		transport->tcp_flags |= TCP_RCV_COPY_CALLDIR;
1132b76ce561STrond Myklebust 		transport->tcp_flags |= TCP_RCV_COPY_DATA;
113318dca02aSRicardo Labiaga 		transport->tcp_flags |= TCP_RPC_REPLY;
1134b76ce561STrond Myklebust 		break;
1135b76ce561STrond Myklebust 	case RPC_CALL:
1136b76ce561STrond Myklebust 		transport->tcp_flags |= TCP_RCV_COPY_CALLDIR;
1137b76ce561STrond Myklebust 		transport->tcp_flags |= TCP_RCV_COPY_DATA;
113818dca02aSRicardo Labiaga 		transport->tcp_flags &= ~TCP_RPC_REPLY;
1139b76ce561STrond Myklebust 		break;
1140b76ce561STrond Myklebust 	default:
1141b76ce561STrond Myklebust 		dprintk("RPC:       invalid request message type\n");
1142a519fc7aSTrond Myklebust 		xs_tcp_force_close(&transport->xprt);
1143b76ce561STrond Myklebust 	}
114418dca02aSRicardo Labiaga 	xs_tcp_check_fraghdr(transport);
114518dca02aSRicardo Labiaga }
114618dca02aSRicardo Labiaga 
114744b98efdSRicardo Labiaga static inline void xs_tcp_read_common(struct rpc_xprt *xprt,
114844b98efdSRicardo Labiaga 				     struct xdr_skb_reader *desc,
114944b98efdSRicardo Labiaga 				     struct rpc_rqst *req)
1150a246b010SChuck Lever {
115144b98efdSRicardo Labiaga 	struct sock_xprt *transport =
115244b98efdSRicardo Labiaga 				container_of(xprt, struct sock_xprt, xprt);
1153a246b010SChuck Lever 	struct xdr_buf *rcvbuf;
1154a246b010SChuck Lever 	size_t len;
1155a246b010SChuck Lever 	ssize_t r;
1156a246b010SChuck Lever 
1157a246b010SChuck Lever 	rcvbuf = &req->rq_private_buf;
1158f4a2e418SRicardo Labiaga 
1159f4a2e418SRicardo Labiaga 	if (transport->tcp_flags & TCP_RCV_COPY_CALLDIR) {
1160f4a2e418SRicardo Labiaga 		/*
1161f4a2e418SRicardo Labiaga 		 * Save the RPC direction in the XDR buffer
1162f4a2e418SRicardo Labiaga 		 */
1163f4a2e418SRicardo Labiaga 		memcpy(rcvbuf->head[0].iov_base + transport->tcp_copied,
1164b76ce561STrond Myklebust 			&transport->tcp_calldir,
1165b76ce561STrond Myklebust 			sizeof(transport->tcp_calldir));
1166b76ce561STrond Myklebust 		transport->tcp_copied += sizeof(transport->tcp_calldir);
1167f4a2e418SRicardo Labiaga 		transport->tcp_flags &= ~TCP_RCV_COPY_CALLDIR;
1168a246b010SChuck Lever 	}
1169a246b010SChuck Lever 
1170a246b010SChuck Lever 	len = desc->count;
117151971139SChuck Lever 	if (len > transport->tcp_reclen - transport->tcp_offset) {
1172dd456471SChuck Lever 		struct xdr_skb_reader my_desc;
1173a246b010SChuck Lever 
117451971139SChuck Lever 		len = transport->tcp_reclen - transport->tcp_offset;
1175a246b010SChuck Lever 		memcpy(&my_desc, desc, sizeof(my_desc));
1176a246b010SChuck Lever 		my_desc.count = len;
117751971139SChuck Lever 		r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
11789d292316SChuck Lever 					  &my_desc, xdr_skb_read_bits);
1179a246b010SChuck Lever 		desc->count -= r;
1180a246b010SChuck Lever 		desc->offset += r;
1181a246b010SChuck Lever 	} else
118251971139SChuck Lever 		r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
11839d292316SChuck Lever 					  desc, xdr_skb_read_bits);
1184a246b010SChuck Lever 
1185a246b010SChuck Lever 	if (r > 0) {
118651971139SChuck Lever 		transport->tcp_copied += r;
118751971139SChuck Lever 		transport->tcp_offset += r;
1188a246b010SChuck Lever 	}
1189a246b010SChuck Lever 	if (r != len) {
1190a246b010SChuck Lever 		/* Error when copying to the receive buffer,
1191a246b010SChuck Lever 		 * usually because we weren't able to allocate
1192a246b010SChuck Lever 		 * additional buffer pages. All we can do now
1193e136d092SChuck Lever 		 * is turn off TCP_RCV_COPY_DATA, so the request
1194a246b010SChuck Lever 		 * will not receive any additional updates,
1195a246b010SChuck Lever 		 * and time out.
1196a246b010SChuck Lever 		 * Any remaining data from this record will
1197a246b010SChuck Lever 		 * be discarded.
1198a246b010SChuck Lever 		 */
1199e136d092SChuck Lever 		transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1200a246b010SChuck Lever 		dprintk("RPC:       XID %08x truncated request\n",
120151971139SChuck Lever 				ntohl(transport->tcp_xid));
120246121cf7SChuck Lever 		dprintk("RPC:       xprt = %p, tcp_copied = %lu, "
120346121cf7SChuck Lever 				"tcp_offset = %u, tcp_reclen = %u\n",
120446121cf7SChuck Lever 				xprt, transport->tcp_copied,
120546121cf7SChuck Lever 				transport->tcp_offset, transport->tcp_reclen);
120644b98efdSRicardo Labiaga 		return;
1207a246b010SChuck Lever 	}
1208a246b010SChuck Lever 
1209a246b010SChuck Lever 	dprintk("RPC:       XID %08x read %Zd bytes\n",
121051971139SChuck Lever 			ntohl(transport->tcp_xid), r);
121146121cf7SChuck Lever 	dprintk("RPC:       xprt = %p, tcp_copied = %lu, tcp_offset = %u, "
121246121cf7SChuck Lever 			"tcp_reclen = %u\n", xprt, transport->tcp_copied,
121346121cf7SChuck Lever 			transport->tcp_offset, transport->tcp_reclen);
1214a246b010SChuck Lever 
121551971139SChuck Lever 	if (transport->tcp_copied == req->rq_private_buf.buflen)
1216e136d092SChuck Lever 		transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
121751971139SChuck Lever 	else if (transport->tcp_offset == transport->tcp_reclen) {
1218e136d092SChuck Lever 		if (transport->tcp_flags & TCP_RCV_LAST_FRAG)
1219e136d092SChuck Lever 			transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1220a246b010SChuck Lever 	}
122144b98efdSRicardo Labiaga }
122244b98efdSRicardo Labiaga 
122344b98efdSRicardo Labiaga /*
122444b98efdSRicardo Labiaga  * Finds the request corresponding to the RPC xid and invokes the common
122544b98efdSRicardo Labiaga  * tcp read code to read the data.
122644b98efdSRicardo Labiaga  */
122744b98efdSRicardo Labiaga static inline int xs_tcp_read_reply(struct rpc_xprt *xprt,
122844b98efdSRicardo Labiaga 				    struct xdr_skb_reader *desc)
122944b98efdSRicardo Labiaga {
123044b98efdSRicardo Labiaga 	struct sock_xprt *transport =
123144b98efdSRicardo Labiaga 				container_of(xprt, struct sock_xprt, xprt);
123244b98efdSRicardo Labiaga 	struct rpc_rqst *req;
123344b98efdSRicardo Labiaga 
123444b98efdSRicardo Labiaga 	dprintk("RPC:       read reply XID %08x\n", ntohl(transport->tcp_xid));
123544b98efdSRicardo Labiaga 
123644b98efdSRicardo Labiaga 	/* Find and lock the request corresponding to this xid */
123744b98efdSRicardo Labiaga 	spin_lock(&xprt->transport_lock);
123844b98efdSRicardo Labiaga 	req = xprt_lookup_rqst(xprt, transport->tcp_xid);
123944b98efdSRicardo Labiaga 	if (!req) {
124044b98efdSRicardo Labiaga 		dprintk("RPC:       XID %08x request not found!\n",
124144b98efdSRicardo Labiaga 				ntohl(transport->tcp_xid));
124244b98efdSRicardo Labiaga 		spin_unlock(&xprt->transport_lock);
124344b98efdSRicardo Labiaga 		return -1;
124444b98efdSRicardo Labiaga 	}
124544b98efdSRicardo Labiaga 
124644b98efdSRicardo Labiaga 	xs_tcp_read_common(xprt, desc, req);
124744b98efdSRicardo Labiaga 
1248e136d092SChuck Lever 	if (!(transport->tcp_flags & TCP_RCV_COPY_DATA))
124951971139SChuck Lever 		xprt_complete_rqst(req->rq_task, transport->tcp_copied);
125044b98efdSRicardo Labiaga 
12514a0f8c04SChuck Lever 	spin_unlock(&xprt->transport_lock);
125244b98efdSRicardo Labiaga 	return 0;
125344b98efdSRicardo Labiaga }
125444b98efdSRicardo Labiaga 
12559e00abc3STrond Myklebust #if defined(CONFIG_SUNRPC_BACKCHANNEL)
125644b98efdSRicardo Labiaga /*
125744b98efdSRicardo Labiaga  * Obtains an rpc_rqst previously allocated and invokes the common
125844b98efdSRicardo Labiaga  * tcp read code to read the data.  The result is placed in the callback
125944b98efdSRicardo Labiaga  * queue.
126044b98efdSRicardo Labiaga  * If we're unable to obtain the rpc_rqst we schedule the closing of the
126144b98efdSRicardo Labiaga  * connection and return -1.
126244b98efdSRicardo Labiaga  */
126344b98efdSRicardo Labiaga static inline int xs_tcp_read_callback(struct rpc_xprt *xprt,
126444b98efdSRicardo Labiaga 				       struct xdr_skb_reader *desc)
126544b98efdSRicardo Labiaga {
126644b98efdSRicardo Labiaga 	struct sock_xprt *transport =
126744b98efdSRicardo Labiaga 				container_of(xprt, struct sock_xprt, xprt);
126844b98efdSRicardo Labiaga 	struct rpc_rqst *req;
126944b98efdSRicardo Labiaga 
127044b98efdSRicardo Labiaga 	req = xprt_alloc_bc_request(xprt);
127144b98efdSRicardo Labiaga 	if (req == NULL) {
127244b98efdSRicardo Labiaga 		printk(KERN_WARNING "Callback slot table overflowed\n");
127344b98efdSRicardo Labiaga 		xprt_force_disconnect(xprt);
127444b98efdSRicardo Labiaga 		return -1;
127544b98efdSRicardo Labiaga 	}
127644b98efdSRicardo Labiaga 
127744b98efdSRicardo Labiaga 	req->rq_xid = transport->tcp_xid;
127844b98efdSRicardo Labiaga 	dprintk("RPC:       read callback  XID %08x\n", ntohl(req->rq_xid));
127944b98efdSRicardo Labiaga 	xs_tcp_read_common(xprt, desc, req);
128044b98efdSRicardo Labiaga 
128144b98efdSRicardo Labiaga 	if (!(transport->tcp_flags & TCP_RCV_COPY_DATA)) {
128244b98efdSRicardo Labiaga 		struct svc_serv *bc_serv = xprt->bc_serv;
128344b98efdSRicardo Labiaga 
128444b98efdSRicardo Labiaga 		/*
128544b98efdSRicardo Labiaga 		 * Add callback request to callback list.  The callback
128644b98efdSRicardo Labiaga 		 * service sleeps on the sv_cb_waitq waiting for new
128744b98efdSRicardo Labiaga 		 * requests.  Wake it up after adding enqueing the
128844b98efdSRicardo Labiaga 		 * request.
128944b98efdSRicardo Labiaga 		 */
129044b98efdSRicardo Labiaga 		dprintk("RPC:       add callback request to list\n");
129144b98efdSRicardo Labiaga 		spin_lock(&bc_serv->sv_cb_lock);
129244b98efdSRicardo Labiaga 		list_add(&req->rq_bc_list, &bc_serv->sv_cb_list);
129344b98efdSRicardo Labiaga 		spin_unlock(&bc_serv->sv_cb_lock);
129444b98efdSRicardo Labiaga 		wake_up(&bc_serv->sv_cb_waitq);
129544b98efdSRicardo Labiaga 	}
129644b98efdSRicardo Labiaga 
129744b98efdSRicardo Labiaga 	req->rq_private_buf.len = transport->tcp_copied;
129844b98efdSRicardo Labiaga 
129944b98efdSRicardo Labiaga 	return 0;
130044b98efdSRicardo Labiaga }
130144b98efdSRicardo Labiaga 
130244b98efdSRicardo Labiaga static inline int _xs_tcp_read_data(struct rpc_xprt *xprt,
130344b98efdSRicardo Labiaga 					struct xdr_skb_reader *desc)
130444b98efdSRicardo Labiaga {
130544b98efdSRicardo Labiaga 	struct sock_xprt *transport =
130644b98efdSRicardo Labiaga 				container_of(xprt, struct sock_xprt, xprt);
130744b98efdSRicardo Labiaga 
130844b98efdSRicardo Labiaga 	return (transport->tcp_flags & TCP_RPC_REPLY) ?
130944b98efdSRicardo Labiaga 		xs_tcp_read_reply(xprt, desc) :
131044b98efdSRicardo Labiaga 		xs_tcp_read_callback(xprt, desc);
131144b98efdSRicardo Labiaga }
131244b98efdSRicardo Labiaga #else
131344b98efdSRicardo Labiaga static inline int _xs_tcp_read_data(struct rpc_xprt *xprt,
131444b98efdSRicardo Labiaga 					struct xdr_skb_reader *desc)
131544b98efdSRicardo Labiaga {
131644b98efdSRicardo Labiaga 	return xs_tcp_read_reply(xprt, desc);
131744b98efdSRicardo Labiaga }
13189e00abc3STrond Myklebust #endif /* CONFIG_SUNRPC_BACKCHANNEL */
131944b98efdSRicardo Labiaga 
132044b98efdSRicardo Labiaga /*
132144b98efdSRicardo Labiaga  * Read data off the transport.  This can be either an RPC_CALL or an
132244b98efdSRicardo Labiaga  * RPC_REPLY.  Relay the processing to helper functions.
132344b98efdSRicardo Labiaga  */
132444b98efdSRicardo Labiaga static void xs_tcp_read_data(struct rpc_xprt *xprt,
132544b98efdSRicardo Labiaga 				    struct xdr_skb_reader *desc)
132644b98efdSRicardo Labiaga {
132744b98efdSRicardo Labiaga 	struct sock_xprt *transport =
132844b98efdSRicardo Labiaga 				container_of(xprt, struct sock_xprt, xprt);
132944b98efdSRicardo Labiaga 
133044b98efdSRicardo Labiaga 	if (_xs_tcp_read_data(xprt, desc) == 0)
133151971139SChuck Lever 		xs_tcp_check_fraghdr(transport);
133244b98efdSRicardo Labiaga 	else {
133344b98efdSRicardo Labiaga 		/*
133444b98efdSRicardo Labiaga 		 * The transport_lock protects the request handling.
133544b98efdSRicardo Labiaga 		 * There's no need to hold it to update the tcp_flags.
133644b98efdSRicardo Labiaga 		 */
133744b98efdSRicardo Labiaga 		transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
133844b98efdSRicardo Labiaga 	}
1339a246b010SChuck Lever }
1340a246b010SChuck Lever 
1341dd456471SChuck Lever static inline void xs_tcp_read_discard(struct sock_xprt *transport, struct xdr_skb_reader *desc)
1342a246b010SChuck Lever {
1343a246b010SChuck Lever 	size_t len;
1344a246b010SChuck Lever 
134551971139SChuck Lever 	len = transport->tcp_reclen - transport->tcp_offset;
1346a246b010SChuck Lever 	if (len > desc->count)
1347a246b010SChuck Lever 		len = desc->count;
1348a246b010SChuck Lever 	desc->count -= len;
1349a246b010SChuck Lever 	desc->offset += len;
135051971139SChuck Lever 	transport->tcp_offset += len;
1351a246b010SChuck Lever 	dprintk("RPC:       discarded %Zu bytes\n", len);
135251971139SChuck Lever 	xs_tcp_check_fraghdr(transport);
1353a246b010SChuck Lever }
1354a246b010SChuck Lever 
13559903cd1cSChuck Lever static int xs_tcp_data_recv(read_descriptor_t *rd_desc, struct sk_buff *skb, unsigned int offset, size_t len)
1356a246b010SChuck Lever {
1357a246b010SChuck Lever 	struct rpc_xprt *xprt = rd_desc->arg.data;
135851971139SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1359dd456471SChuck Lever 	struct xdr_skb_reader desc = {
1360a246b010SChuck Lever 		.skb	= skb,
1361a246b010SChuck Lever 		.offset	= offset,
1362a246b010SChuck Lever 		.count	= len,
1363a246b010SChuck Lever 	};
1364a246b010SChuck Lever 
13659903cd1cSChuck Lever 	dprintk("RPC:       xs_tcp_data_recv started\n");
1366a246b010SChuck Lever 	do {
1367a246b010SChuck Lever 		/* Read in a new fragment marker if necessary */
1368a246b010SChuck Lever 		/* Can we ever really expect to get completely empty fragments? */
1369e136d092SChuck Lever 		if (transport->tcp_flags & TCP_RCV_COPY_FRAGHDR) {
13709903cd1cSChuck Lever 			xs_tcp_read_fraghdr(xprt, &desc);
1371a246b010SChuck Lever 			continue;
1372a246b010SChuck Lever 		}
1373a246b010SChuck Lever 		/* Read in the xid if necessary */
1374e136d092SChuck Lever 		if (transport->tcp_flags & TCP_RCV_COPY_XID) {
137551971139SChuck Lever 			xs_tcp_read_xid(transport, &desc);
1376a246b010SChuck Lever 			continue;
1377a246b010SChuck Lever 		}
137818dca02aSRicardo Labiaga 		/* Read in the call/reply flag */
1379f4a2e418SRicardo Labiaga 		if (transport->tcp_flags & TCP_RCV_READ_CALLDIR) {
138018dca02aSRicardo Labiaga 			xs_tcp_read_calldir(transport, &desc);
138118dca02aSRicardo Labiaga 			continue;
138218dca02aSRicardo Labiaga 		}
1383a246b010SChuck Lever 		/* Read in the request data */
1384e136d092SChuck Lever 		if (transport->tcp_flags & TCP_RCV_COPY_DATA) {
138544b98efdSRicardo Labiaga 			xs_tcp_read_data(xprt, &desc);
1386a246b010SChuck Lever 			continue;
1387a246b010SChuck Lever 		}
1388a246b010SChuck Lever 		/* Skip over any trailing bytes on short reads */
138951971139SChuck Lever 		xs_tcp_read_discard(transport, &desc);
1390a246b010SChuck Lever 	} while (desc.count);
13919903cd1cSChuck Lever 	dprintk("RPC:       xs_tcp_data_recv done\n");
1392a246b010SChuck Lever 	return len - desc.count;
1393a246b010SChuck Lever }
1394a246b010SChuck Lever 
13959903cd1cSChuck Lever /**
13969903cd1cSChuck Lever  * xs_tcp_data_ready - "data ready" callback for TCP sockets
13979903cd1cSChuck Lever  * @sk: socket with data to read
13989903cd1cSChuck Lever  * @bytes: how much data to read
13999903cd1cSChuck Lever  *
14009903cd1cSChuck Lever  */
14019903cd1cSChuck Lever static void xs_tcp_data_ready(struct sock *sk, int bytes)
1402a246b010SChuck Lever {
1403a246b010SChuck Lever 	struct rpc_xprt *xprt;
1404a246b010SChuck Lever 	read_descriptor_t rd_desc;
1405ff2d7db8STrond Myklebust 	int read;
1406a246b010SChuck Lever 
14079903cd1cSChuck Lever 	dprintk("RPC:       xs_tcp_data_ready...\n");
140846121cf7SChuck Lever 
1409f064af1eSEric Dumazet 	read_lock_bh(&sk->sk_callback_lock);
14109903cd1cSChuck Lever 	if (!(xprt = xprt_from_sock(sk)))
1411a246b010SChuck Lever 		goto out;
141261d0a8e6SNeil Brown 	/* Any data means we had a useful conversation, so
141361d0a8e6SNeil Brown 	 * the we don't need to delay the next reconnect
141461d0a8e6SNeil Brown 	 */
141561d0a8e6SNeil Brown 	if (xprt->reestablish_timeout)
141661d0a8e6SNeil Brown 		xprt->reestablish_timeout = 0;
141761d0a8e6SNeil Brown 
14189903cd1cSChuck Lever 	/* We use rd_desc to pass struct xprt to xs_tcp_data_recv */
1419a246b010SChuck Lever 	rd_desc.arg.data = xprt;
1420ff2d7db8STrond Myklebust 	do {
1421a246b010SChuck Lever 		rd_desc.count = 65536;
1422ff2d7db8STrond Myklebust 		read = tcp_read_sock(sk, &rd_desc, xs_tcp_data_recv);
1423ff2d7db8STrond Myklebust 	} while (read > 0);
1424a246b010SChuck Lever out:
1425f064af1eSEric Dumazet 	read_unlock_bh(&sk->sk_callback_lock);
1426a246b010SChuck Lever }
1427a246b010SChuck Lever 
14287d1e8255STrond Myklebust /*
14297d1e8255STrond Myklebust  * Do the equivalent of linger/linger2 handling for dealing with
14307d1e8255STrond Myklebust  * broken servers that don't close the socket in a timely
14317d1e8255STrond Myklebust  * fashion
14327d1e8255STrond Myklebust  */
14337d1e8255STrond Myklebust static void xs_tcp_schedule_linger_timeout(struct rpc_xprt *xprt,
14347d1e8255STrond Myklebust 		unsigned long timeout)
14357d1e8255STrond Myklebust {
14367d1e8255STrond Myklebust 	struct sock_xprt *transport;
14377d1e8255STrond Myklebust 
14387d1e8255STrond Myklebust 	if (xprt_test_and_set_connecting(xprt))
14397d1e8255STrond Myklebust 		return;
14407d1e8255STrond Myklebust 	set_bit(XPRT_CONNECTION_ABORT, &xprt->state);
14417d1e8255STrond Myklebust 	transport = container_of(xprt, struct sock_xprt, xprt);
14427d1e8255STrond Myklebust 	queue_delayed_work(rpciod_workqueue, &transport->connect_worker,
14437d1e8255STrond Myklebust 			   timeout);
14447d1e8255STrond Myklebust }
14457d1e8255STrond Myklebust 
14467d1e8255STrond Myklebust static void xs_tcp_cancel_linger_timeout(struct rpc_xprt *xprt)
14477d1e8255STrond Myklebust {
14487d1e8255STrond Myklebust 	struct sock_xprt *transport;
14497d1e8255STrond Myklebust 
14507d1e8255STrond Myklebust 	transport = container_of(xprt, struct sock_xprt, xprt);
14517d1e8255STrond Myklebust 
14527d1e8255STrond Myklebust 	if (!test_bit(XPRT_CONNECTION_ABORT, &xprt->state) ||
14537d1e8255STrond Myklebust 	    !cancel_delayed_work(&transport->connect_worker))
14547d1e8255STrond Myklebust 		return;
14557d1e8255STrond Myklebust 	clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
14567d1e8255STrond Myklebust 	xprt_clear_connecting(xprt);
14577d1e8255STrond Myklebust }
14587d1e8255STrond Myklebust 
14594bc1e68eSTrond Myklebust static void xs_sock_reset_connection_flags(struct rpc_xprt *xprt)
14607d1e8255STrond Myklebust {
14617d1e8255STrond Myklebust 	smp_mb__before_clear_bit();
1462a519fc7aSTrond Myklebust 	clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1463a519fc7aSTrond Myklebust 	clear_bit(XPRT_CONNECTION_CLOSE, &xprt->state);
14647d1e8255STrond Myklebust 	clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
14657d1e8255STrond Myklebust 	clear_bit(XPRT_CLOSING, &xprt->state);
14667d1e8255STrond Myklebust 	smp_mb__after_clear_bit();
14674bc1e68eSTrond Myklebust }
14684bc1e68eSTrond Myklebust 
14694bc1e68eSTrond Myklebust static void xs_sock_mark_closed(struct rpc_xprt *xprt)
14704bc1e68eSTrond Myklebust {
14714bc1e68eSTrond Myklebust 	xs_sock_reset_connection_flags(xprt);
14727d1e8255STrond Myklebust 	/* Mark transport as closed and wake up all pending tasks */
14737d1e8255STrond Myklebust 	xprt_disconnect_done(xprt);
14747d1e8255STrond Myklebust }
14757d1e8255STrond Myklebust 
14769903cd1cSChuck Lever /**
14779903cd1cSChuck Lever  * xs_tcp_state_change - callback to handle TCP socket state changes
14789903cd1cSChuck Lever  * @sk: socket whose state has changed
14799903cd1cSChuck Lever  *
14809903cd1cSChuck Lever  */
14819903cd1cSChuck Lever static void xs_tcp_state_change(struct sock *sk)
1482a246b010SChuck Lever {
1483a246b010SChuck Lever 	struct rpc_xprt *xprt;
1484a246b010SChuck Lever 
1485f064af1eSEric Dumazet 	read_lock_bh(&sk->sk_callback_lock);
1486a246b010SChuck Lever 	if (!(xprt = xprt_from_sock(sk)))
1487a246b010SChuck Lever 		goto out;
14889903cd1cSChuck Lever 	dprintk("RPC:       xs_tcp_state_change client %p...\n", xprt);
1489669502ffSAndy Chittenden 	dprintk("RPC:       state %x conn %d dead %d zapped %d sk_shutdown %d\n",
1490a246b010SChuck Lever 			sk->sk_state, xprt_connected(xprt),
1491a246b010SChuck Lever 			sock_flag(sk, SOCK_DEAD),
1492669502ffSAndy Chittenden 			sock_flag(sk, SOCK_ZAPPED),
1493669502ffSAndy Chittenden 			sk->sk_shutdown);
1494a246b010SChuck Lever 
1495a246b010SChuck Lever 	switch (sk->sk_state) {
1496a246b010SChuck Lever 	case TCP_ESTABLISHED:
1497f064af1eSEric Dumazet 		spin_lock(&xprt->transport_lock);
1498a246b010SChuck Lever 		if (!xprt_test_and_set_connected(xprt)) {
149951971139SChuck Lever 			struct sock_xprt *transport = container_of(xprt,
150051971139SChuck Lever 					struct sock_xprt, xprt);
150151971139SChuck Lever 
1502a246b010SChuck Lever 			/* Reset TCP record info */
150351971139SChuck Lever 			transport->tcp_offset = 0;
150451971139SChuck Lever 			transport->tcp_reclen = 0;
150551971139SChuck Lever 			transport->tcp_copied = 0;
1506e136d092SChuck Lever 			transport->tcp_flags =
1507e136d092SChuck Lever 				TCP_RCV_COPY_FRAGHDR | TCP_RCV_COPY_XID;
150851971139SChuck Lever 
15092a491991STrond Myklebust 			xprt_wake_pending_tasks(xprt, -EAGAIN);
1510a246b010SChuck Lever 		}
1511f064af1eSEric Dumazet 		spin_unlock(&xprt->transport_lock);
1512a246b010SChuck Lever 		break;
15133b948ae5STrond Myklebust 	case TCP_FIN_WAIT1:
15143b948ae5STrond Myklebust 		/* The client initiated a shutdown of the socket */
15157c1d71cfSTrond Myklebust 		xprt->connect_cookie++;
1516663b8858STrond Myklebust 		xprt->reestablish_timeout = 0;
15173b948ae5STrond Myklebust 		set_bit(XPRT_CLOSING, &xprt->state);
15183b948ae5STrond Myklebust 		smp_mb__before_clear_bit();
15193b948ae5STrond Myklebust 		clear_bit(XPRT_CONNECTED, &xprt->state);
1520ef803670STrond Myklebust 		clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
15213b948ae5STrond Myklebust 		smp_mb__after_clear_bit();
152225fe6142STrond Myklebust 		xs_tcp_schedule_linger_timeout(xprt, xs_tcp_fin_timeout);
1523a246b010SChuck Lever 		break;
1524632e3bdcSTrond Myklebust 	case TCP_CLOSE_WAIT:
15253b948ae5STrond Myklebust 		/* The server initiated a shutdown of the socket */
15267c1d71cfSTrond Myklebust 		xprt->connect_cookie++;
1527d0bea455STrond Myklebust 		clear_bit(XPRT_CONNECTED, &xprt->state);
1528a519fc7aSTrond Myklebust 		xs_tcp_force_close(xprt);
1529663b8858STrond Myklebust 	case TCP_CLOSING:
1530663b8858STrond Myklebust 		/*
1531663b8858STrond Myklebust 		 * If the server closed down the connection, make sure that
1532663b8858STrond Myklebust 		 * we back off before reconnecting
1533663b8858STrond Myklebust 		 */
1534663b8858STrond Myklebust 		if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
1535663b8858STrond Myklebust 			xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
15363b948ae5STrond Myklebust 		break;
15373b948ae5STrond Myklebust 	case TCP_LAST_ACK:
1538670f9457STrond Myklebust 		set_bit(XPRT_CLOSING, &xprt->state);
153925fe6142STrond Myklebust 		xs_tcp_schedule_linger_timeout(xprt, xs_tcp_fin_timeout);
15403b948ae5STrond Myklebust 		smp_mb__before_clear_bit();
15413b948ae5STrond Myklebust 		clear_bit(XPRT_CONNECTED, &xprt->state);
15423b948ae5STrond Myklebust 		smp_mb__after_clear_bit();
15433b948ae5STrond Myklebust 		break;
15443b948ae5STrond Myklebust 	case TCP_CLOSE:
15457d1e8255STrond Myklebust 		xs_tcp_cancel_linger_timeout(xprt);
15467d1e8255STrond Myklebust 		xs_sock_mark_closed(xprt);
1547a246b010SChuck Lever 	}
1548a246b010SChuck Lever  out:
1549f064af1eSEric Dumazet 	read_unlock_bh(&sk->sk_callback_lock);
1550a246b010SChuck Lever }
1551a246b010SChuck Lever 
15521f0fa154SIlpo Järvinen static void xs_write_space(struct sock *sk)
15531f0fa154SIlpo Järvinen {
15541f0fa154SIlpo Järvinen 	struct socket *sock;
15551f0fa154SIlpo Järvinen 	struct rpc_xprt *xprt;
15561f0fa154SIlpo Järvinen 
15571f0fa154SIlpo Järvinen 	if (unlikely(!(sock = sk->sk_socket)))
15581f0fa154SIlpo Järvinen 		return;
15591f0fa154SIlpo Järvinen 	clear_bit(SOCK_NOSPACE, &sock->flags);
15601f0fa154SIlpo Järvinen 
15611f0fa154SIlpo Järvinen 	if (unlikely(!(xprt = xprt_from_sock(sk))))
15621f0fa154SIlpo Järvinen 		return;
15631f0fa154SIlpo Järvinen 	if (test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags) == 0)
15641f0fa154SIlpo Järvinen 		return;
15651f0fa154SIlpo Järvinen 
15661f0fa154SIlpo Järvinen 	xprt_write_space(xprt);
15671f0fa154SIlpo Järvinen }
15681f0fa154SIlpo Järvinen 
15692a9e1cfaSTrond Myklebust /**
1570c7b2cae8SChuck Lever  * xs_udp_write_space - callback invoked when socket buffer space
1571c7b2cae8SChuck Lever  *                             becomes available
15729903cd1cSChuck Lever  * @sk: socket whose state has changed
15739903cd1cSChuck Lever  *
1574a246b010SChuck Lever  * Called when more output buffer space is available for this socket.
1575a246b010SChuck Lever  * We try not to wake our writers until they can make "significant"
1576c7b2cae8SChuck Lever  * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1577a246b010SChuck Lever  * with a bunch of small requests.
1578a246b010SChuck Lever  */
1579c7b2cae8SChuck Lever static void xs_udp_write_space(struct sock *sk)
1580a246b010SChuck Lever {
1581f064af1eSEric Dumazet 	read_lock_bh(&sk->sk_callback_lock);
1582c7b2cae8SChuck Lever 
1583c7b2cae8SChuck Lever 	/* from net/core/sock.c:sock_def_write_space */
15841f0fa154SIlpo Järvinen 	if (sock_writeable(sk))
15851f0fa154SIlpo Järvinen 		xs_write_space(sk);
1586c7b2cae8SChuck Lever 
1587f064af1eSEric Dumazet 	read_unlock_bh(&sk->sk_callback_lock);
1588c7b2cae8SChuck Lever }
1589c7b2cae8SChuck Lever 
1590c7b2cae8SChuck Lever /**
1591c7b2cae8SChuck Lever  * xs_tcp_write_space - callback invoked when socket buffer space
1592c7b2cae8SChuck Lever  *                             becomes available
1593c7b2cae8SChuck Lever  * @sk: socket whose state has changed
1594c7b2cae8SChuck Lever  *
1595c7b2cae8SChuck Lever  * Called when more output buffer space is available for this socket.
1596c7b2cae8SChuck Lever  * We try not to wake our writers until they can make "significant"
1597c7b2cae8SChuck Lever  * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1598c7b2cae8SChuck Lever  * with a bunch of small requests.
1599c7b2cae8SChuck Lever  */
1600c7b2cae8SChuck Lever static void xs_tcp_write_space(struct sock *sk)
1601c7b2cae8SChuck Lever {
1602f064af1eSEric Dumazet 	read_lock_bh(&sk->sk_callback_lock);
1603c7b2cae8SChuck Lever 
1604c7b2cae8SChuck Lever 	/* from net/core/stream.c:sk_stream_write_space */
16051f0fa154SIlpo Järvinen 	if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk))
16061f0fa154SIlpo Järvinen 		xs_write_space(sk);
1607c7b2cae8SChuck Lever 
1608f064af1eSEric Dumazet 	read_unlock_bh(&sk->sk_callback_lock);
1609a246b010SChuck Lever }
1610a246b010SChuck Lever 
1611470056c2SChuck Lever static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt)
1612a246b010SChuck Lever {
1613ee0ac0c2SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1614ee0ac0c2SChuck Lever 	struct sock *sk = transport->inet;
1615a246b010SChuck Lever 
16167c6e066eSChuck Lever 	if (transport->rcvsize) {
1617a246b010SChuck Lever 		sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
16187c6e066eSChuck Lever 		sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2;
1619a246b010SChuck Lever 	}
16207c6e066eSChuck Lever 	if (transport->sndsize) {
1621a246b010SChuck Lever 		sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
16227c6e066eSChuck Lever 		sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2;
1623a246b010SChuck Lever 		sk->sk_write_space(sk);
1624a246b010SChuck Lever 	}
1625a246b010SChuck Lever }
1626a246b010SChuck Lever 
162743118c29SChuck Lever /**
1628470056c2SChuck Lever  * xs_udp_set_buffer_size - set send and receive limits
162943118c29SChuck Lever  * @xprt: generic transport
1630470056c2SChuck Lever  * @sndsize: requested size of send buffer, in bytes
1631470056c2SChuck Lever  * @rcvsize: requested size of receive buffer, in bytes
163243118c29SChuck Lever  *
1633470056c2SChuck Lever  * Set socket send and receive buffer size limits.
163443118c29SChuck Lever  */
1635470056c2SChuck Lever static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize)
163643118c29SChuck Lever {
16377c6e066eSChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
16387c6e066eSChuck Lever 
16397c6e066eSChuck Lever 	transport->sndsize = 0;
1640470056c2SChuck Lever 	if (sndsize)
16417c6e066eSChuck Lever 		transport->sndsize = sndsize + 1024;
16427c6e066eSChuck Lever 	transport->rcvsize = 0;
1643470056c2SChuck Lever 	if (rcvsize)
16447c6e066eSChuck Lever 		transport->rcvsize = rcvsize + 1024;
1645470056c2SChuck Lever 
1646470056c2SChuck Lever 	xs_udp_do_set_buffer_size(xprt);
164743118c29SChuck Lever }
164843118c29SChuck Lever 
164946c0ee8bSChuck Lever /**
165046c0ee8bSChuck Lever  * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
165146c0ee8bSChuck Lever  * @task: task that timed out
165246c0ee8bSChuck Lever  *
165346c0ee8bSChuck Lever  * Adjust the congestion window after a retransmit timeout has occurred.
165446c0ee8bSChuck Lever  */
16556a24dfb6STrond Myklebust static void xs_udp_timer(struct rpc_xprt *xprt, struct rpc_task *task)
165646c0ee8bSChuck Lever {
16576a24dfb6STrond Myklebust 	xprt_adjust_cwnd(xprt, task, -ETIMEDOUT);
165846c0ee8bSChuck Lever }
165946c0ee8bSChuck Lever 
1660b85d8806SChuck Lever static unsigned short xs_get_random_port(void)
1661b85d8806SChuck Lever {
1662b85d8806SChuck Lever 	unsigned short range = xprt_max_resvport - xprt_min_resvport;
1663b85d8806SChuck Lever 	unsigned short rand = (unsigned short) net_random() % range;
1664b85d8806SChuck Lever 	return rand + xprt_min_resvport;
1665b85d8806SChuck Lever }
1666b85d8806SChuck Lever 
166792200412SChuck Lever /**
166892200412SChuck Lever  * xs_set_port - reset the port number in the remote endpoint address
166992200412SChuck Lever  * @xprt: generic transport
167092200412SChuck Lever  * @port: new port number
167192200412SChuck Lever  *
167292200412SChuck Lever  */
167392200412SChuck Lever static void xs_set_port(struct rpc_xprt *xprt, unsigned short port)
167492200412SChuck Lever {
167592200412SChuck Lever 	dprintk("RPC:       setting port for xprt %p to %u\n", xprt, port);
1676c4efcb1dSChuck Lever 
16779dc3b095SChuck Lever 	rpc_set_port(xs_addr(xprt), port);
16789dc3b095SChuck Lever 	xs_update_peer_port(xprt);
167992200412SChuck Lever }
168092200412SChuck Lever 
16815d4ec932SPavel Emelyanov static unsigned short xs_get_srcport(struct sock_xprt *transport)
168267a391d7STrond Myklebust {
1683fbfffbd5SChuck Lever 	unsigned short port = transport->srcport;
168467a391d7STrond Myklebust 
168567a391d7STrond Myklebust 	if (port == 0 && transport->xprt.resvport)
168667a391d7STrond Myklebust 		port = xs_get_random_port();
168767a391d7STrond Myklebust 	return port;
168867a391d7STrond Myklebust }
168967a391d7STrond Myklebust 
1690baaf4e48SPavel Emelyanov static unsigned short xs_next_srcport(struct sock_xprt *transport, unsigned short port)
169167a391d7STrond Myklebust {
1692fbfffbd5SChuck Lever 	if (transport->srcport != 0)
1693fbfffbd5SChuck Lever 		transport->srcport = 0;
169467a391d7STrond Myklebust 	if (!transport->xprt.resvport)
169567a391d7STrond Myklebust 		return 0;
169667a391d7STrond Myklebust 	if (port <= xprt_min_resvport || port > xprt_max_resvport)
169767a391d7STrond Myklebust 		return xprt_max_resvport;
169867a391d7STrond Myklebust 	return --port;
169967a391d7STrond Myklebust }
1700beb59b68SPavel Emelyanov static int xs_bind(struct sock_xprt *transport, struct socket *sock)
1701a246b010SChuck Lever {
1702beb59b68SPavel Emelyanov 	struct sockaddr_storage myaddr;
170367a391d7STrond Myklebust 	int err, nloop = 0;
17045d4ec932SPavel Emelyanov 	unsigned short port = xs_get_srcport(transport);
170567a391d7STrond Myklebust 	unsigned short last;
1706a246b010SChuck Lever 
1707beb59b68SPavel Emelyanov 	memcpy(&myaddr, &transport->srcaddr, transport->xprt.addrlen);
1708a246b010SChuck Lever 	do {
1709beb59b68SPavel Emelyanov 		rpc_set_port((struct sockaddr *)&myaddr, port);
1710e6242e92SSridhar Samudrala 		err = kernel_bind(sock, (struct sockaddr *)&myaddr,
1711beb59b68SPavel Emelyanov 				transport->xprt.addrlen);
171267a391d7STrond Myklebust 		if (port == 0)
1713d3bc9a1dSFrank van Maarseveen 			break;
1714a246b010SChuck Lever 		if (err == 0) {
1715fbfffbd5SChuck Lever 			transport->srcport = port;
1716d3bc9a1dSFrank van Maarseveen 			break;
1717a246b010SChuck Lever 		}
171867a391d7STrond Myklebust 		last = port;
1719baaf4e48SPavel Emelyanov 		port = xs_next_srcport(transport, port);
172067a391d7STrond Myklebust 		if (port > last)
172167a391d7STrond Myklebust 			nloop++;
172267a391d7STrond Myklebust 	} while (err == -EADDRINUSE && nloop != 2);
1723beb59b68SPavel Emelyanov 
17244232e863SChuck Lever 	if (myaddr.ss_family == AF_INET)
1725beb59b68SPavel Emelyanov 		dprintk("RPC:       %s %pI4:%u: %s (%d)\n", __func__,
1726beb59b68SPavel Emelyanov 				&((struct sockaddr_in *)&myaddr)->sin_addr,
1727beb59b68SPavel Emelyanov 				port, err ? "failed" : "ok", err);
1728beb59b68SPavel Emelyanov 	else
1729beb59b68SPavel Emelyanov 		dprintk("RPC:       %s %pI6:%u: %s (%d)\n", __func__,
1730beb59b68SPavel Emelyanov 				&((struct sockaddr_in6 *)&myaddr)->sin6_addr,
17317dc753f0SChuck Lever 				port, err ? "failed" : "ok", err);
1732a246b010SChuck Lever 	return err;
1733a246b010SChuck Lever }
1734a246b010SChuck Lever 
1735176e21eeSChuck Lever /*
1736176e21eeSChuck Lever  * We don't support autobind on AF_LOCAL sockets
1737176e21eeSChuck Lever  */
1738176e21eeSChuck Lever static void xs_local_rpcbind(struct rpc_task *task)
1739176e21eeSChuck Lever {
17403dc0da27STrond Myklebust 	rcu_read_lock();
17413dc0da27STrond Myklebust 	xprt_set_bound(rcu_dereference(task->tk_client->cl_xprt));
17423dc0da27STrond Myklebust 	rcu_read_unlock();
1743176e21eeSChuck Lever }
1744176e21eeSChuck Lever 
1745176e21eeSChuck Lever static void xs_local_set_port(struct rpc_xprt *xprt, unsigned short port)
1746176e21eeSChuck Lever {
1747176e21eeSChuck Lever }
1748a246b010SChuck Lever 
1749ed07536eSPeter Zijlstra #ifdef CONFIG_DEBUG_LOCK_ALLOC
1750ed07536eSPeter Zijlstra static struct lock_class_key xs_key[2];
1751ed07536eSPeter Zijlstra static struct lock_class_key xs_slock_key[2];
1752ed07536eSPeter Zijlstra 
1753176e21eeSChuck Lever static inline void xs_reclassify_socketu(struct socket *sock)
1754176e21eeSChuck Lever {
1755176e21eeSChuck Lever 	struct sock *sk = sock->sk;
1756176e21eeSChuck Lever 
1757176e21eeSChuck Lever 	sock_lock_init_class_and_name(sk, "slock-AF_LOCAL-RPC",
1758176e21eeSChuck Lever 		&xs_slock_key[1], "sk_lock-AF_LOCAL-RPC", &xs_key[1]);
1759176e21eeSChuck Lever }
1760176e21eeSChuck Lever 
17618945ee5eSChuck Lever static inline void xs_reclassify_socket4(struct socket *sock)
1762ed07536eSPeter Zijlstra {
1763ed07536eSPeter Zijlstra 	struct sock *sk = sock->sk;
17648945ee5eSChuck Lever 
17658945ee5eSChuck Lever 	sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC",
17668945ee5eSChuck Lever 		&xs_slock_key[0], "sk_lock-AF_INET-RPC", &xs_key[0]);
1767ed07536eSPeter Zijlstra }
17688945ee5eSChuck Lever 
17698945ee5eSChuck Lever static inline void xs_reclassify_socket6(struct socket *sock)
17708945ee5eSChuck Lever {
17718945ee5eSChuck Lever 	struct sock *sk = sock->sk;
17728945ee5eSChuck Lever 
17738945ee5eSChuck Lever 	sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC",
17748945ee5eSChuck Lever 		&xs_slock_key[1], "sk_lock-AF_INET6-RPC", &xs_key[1]);
1775ed07536eSPeter Zijlstra }
17766bc9638aSPavel Emelyanov 
17776bc9638aSPavel Emelyanov static inline void xs_reclassify_socket(int family, struct socket *sock)
17786bc9638aSPavel Emelyanov {
17791b7a1819SWeston Andros Adamson 	WARN_ON_ONCE(sock_owned_by_user(sock->sk));
17801b7a1819SWeston Andros Adamson 	if (sock_owned_by_user(sock->sk))
17811b7a1819SWeston Andros Adamson 		return;
17821b7a1819SWeston Andros Adamson 
17834232e863SChuck Lever 	switch (family) {
1784176e21eeSChuck Lever 	case AF_LOCAL:
1785176e21eeSChuck Lever 		xs_reclassify_socketu(sock);
1786176e21eeSChuck Lever 		break;
17874232e863SChuck Lever 	case AF_INET:
17886bc9638aSPavel Emelyanov 		xs_reclassify_socket4(sock);
17894232e863SChuck Lever 		break;
17904232e863SChuck Lever 	case AF_INET6:
17916bc9638aSPavel Emelyanov 		xs_reclassify_socket6(sock);
17924232e863SChuck Lever 		break;
17934232e863SChuck Lever 	}
17946bc9638aSPavel Emelyanov }
1795ed07536eSPeter Zijlstra #else
1796176e21eeSChuck Lever static inline void xs_reclassify_socketu(struct socket *sock)
1797176e21eeSChuck Lever {
1798176e21eeSChuck Lever }
1799176e21eeSChuck Lever 
18008945ee5eSChuck Lever static inline void xs_reclassify_socket4(struct socket *sock)
18018945ee5eSChuck Lever {
18028945ee5eSChuck Lever }
18038945ee5eSChuck Lever 
18048945ee5eSChuck Lever static inline void xs_reclassify_socket6(struct socket *sock)
1805ed07536eSPeter Zijlstra {
1806ed07536eSPeter Zijlstra }
18076bc9638aSPavel Emelyanov 
18086bc9638aSPavel Emelyanov static inline void xs_reclassify_socket(int family, struct socket *sock)
18096bc9638aSPavel Emelyanov {
18106bc9638aSPavel Emelyanov }
1811ed07536eSPeter Zijlstra #endif
1812ed07536eSPeter Zijlstra 
18136bc9638aSPavel Emelyanov static struct socket *xs_create_sock(struct rpc_xprt *xprt,
18146bc9638aSPavel Emelyanov 		struct sock_xprt *transport, int family, int type, int protocol)
181522f79326SPavel Emelyanov {
181622f79326SPavel Emelyanov 	struct socket *sock;
181722f79326SPavel Emelyanov 	int err;
181822f79326SPavel Emelyanov 
18196bc9638aSPavel Emelyanov 	err = __sock_create(xprt->xprt_net, family, type, protocol, &sock, 1);
182022f79326SPavel Emelyanov 	if (err < 0) {
182122f79326SPavel Emelyanov 		dprintk("RPC:       can't create %d transport socket (%d).\n",
182222f79326SPavel Emelyanov 				protocol, -err);
182322f79326SPavel Emelyanov 		goto out;
182422f79326SPavel Emelyanov 	}
18256bc9638aSPavel Emelyanov 	xs_reclassify_socket(family, sock);
182622f79326SPavel Emelyanov 
18274cea288aSBen Hutchings 	err = xs_bind(transport, sock);
18284cea288aSBen Hutchings 	if (err) {
182922f79326SPavel Emelyanov 		sock_release(sock);
183022f79326SPavel Emelyanov 		goto out;
183122f79326SPavel Emelyanov 	}
183222f79326SPavel Emelyanov 
183322f79326SPavel Emelyanov 	return sock;
183422f79326SPavel Emelyanov out:
183522f79326SPavel Emelyanov 	return ERR_PTR(err);
183622f79326SPavel Emelyanov }
183722f79326SPavel Emelyanov 
1838176e21eeSChuck Lever static int xs_local_finish_connecting(struct rpc_xprt *xprt,
1839176e21eeSChuck Lever 				      struct socket *sock)
1840176e21eeSChuck Lever {
1841176e21eeSChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
1842176e21eeSChuck Lever 									xprt);
1843176e21eeSChuck Lever 
1844176e21eeSChuck Lever 	if (!transport->inet) {
1845176e21eeSChuck Lever 		struct sock *sk = sock->sk;
1846176e21eeSChuck Lever 
1847176e21eeSChuck Lever 		write_lock_bh(&sk->sk_callback_lock);
1848176e21eeSChuck Lever 
1849176e21eeSChuck Lever 		xs_save_old_callbacks(transport, sk);
1850176e21eeSChuck Lever 
1851176e21eeSChuck Lever 		sk->sk_user_data = xprt;
1852176e21eeSChuck Lever 		sk->sk_data_ready = xs_local_data_ready;
1853176e21eeSChuck Lever 		sk->sk_write_space = xs_udp_write_space;
1854176e21eeSChuck Lever 		sk->sk_allocation = GFP_ATOMIC;
1855176e21eeSChuck Lever 
1856176e21eeSChuck Lever 		xprt_clear_connected(xprt);
1857176e21eeSChuck Lever 
1858176e21eeSChuck Lever 		/* Reset to new socket */
1859176e21eeSChuck Lever 		transport->sock = sock;
1860176e21eeSChuck Lever 		transport->inet = sk;
1861176e21eeSChuck Lever 
1862176e21eeSChuck Lever 		write_unlock_bh(&sk->sk_callback_lock);
1863176e21eeSChuck Lever 	}
1864176e21eeSChuck Lever 
1865176e21eeSChuck Lever 	/* Tell the socket layer to start connecting... */
1866176e21eeSChuck Lever 	xprt->stat.connect_count++;
1867176e21eeSChuck Lever 	xprt->stat.connect_start = jiffies;
1868176e21eeSChuck Lever 	return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, 0);
1869176e21eeSChuck Lever }
1870176e21eeSChuck Lever 
1871176e21eeSChuck Lever /**
1872176e21eeSChuck Lever  * xs_local_setup_socket - create AF_LOCAL socket, connect to a local endpoint
1873176e21eeSChuck Lever  * @xprt: RPC transport to connect
1874176e21eeSChuck Lever  * @transport: socket transport to connect
1875176e21eeSChuck Lever  * @create_sock: function to create a socket of the correct type
1876176e21eeSChuck Lever  */
1877dc107402SJ. Bruce Fields static int xs_local_setup_socket(struct sock_xprt *transport)
1878176e21eeSChuck Lever {
1879176e21eeSChuck Lever 	struct rpc_xprt *xprt = &transport->xprt;
1880176e21eeSChuck Lever 	struct socket *sock;
1881176e21eeSChuck Lever 	int status = -EIO;
1882176e21eeSChuck Lever 
18835cf02d09SJeff Layton 	current->flags |= PF_FSTRANS;
18845cf02d09SJeff Layton 
1885176e21eeSChuck Lever 	clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1886176e21eeSChuck Lever 	status = __sock_create(xprt->xprt_net, AF_LOCAL,
1887176e21eeSChuck Lever 					SOCK_STREAM, 0, &sock, 1);
1888176e21eeSChuck Lever 	if (status < 0) {
1889176e21eeSChuck Lever 		dprintk("RPC:       can't create AF_LOCAL "
1890176e21eeSChuck Lever 			"transport socket (%d).\n", -status);
1891176e21eeSChuck Lever 		goto out;
1892176e21eeSChuck Lever 	}
1893176e21eeSChuck Lever 	xs_reclassify_socketu(sock);
1894176e21eeSChuck Lever 
1895176e21eeSChuck Lever 	dprintk("RPC:       worker connecting xprt %p via AF_LOCAL to %s\n",
1896176e21eeSChuck Lever 			xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1897176e21eeSChuck Lever 
1898176e21eeSChuck Lever 	status = xs_local_finish_connecting(xprt, sock);
1899176e21eeSChuck Lever 	switch (status) {
1900176e21eeSChuck Lever 	case 0:
1901176e21eeSChuck Lever 		dprintk("RPC:       xprt %p connected to %s\n",
1902176e21eeSChuck Lever 				xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1903176e21eeSChuck Lever 		xprt_set_connected(xprt);
1904176e21eeSChuck Lever 		break;
1905176e21eeSChuck Lever 	case -ENOENT:
1906176e21eeSChuck Lever 		dprintk("RPC:       xprt %p: socket %s does not exist\n",
1907176e21eeSChuck Lever 				xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1908176e21eeSChuck Lever 		break;
19094a20a988STrond Myklebust 	case -ECONNREFUSED:
19104a20a988STrond Myklebust 		dprintk("RPC:       xprt %p: connection refused for %s\n",
19114a20a988STrond Myklebust 				xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
19124a20a988STrond Myklebust 		break;
1913176e21eeSChuck Lever 	default:
1914176e21eeSChuck Lever 		printk(KERN_ERR "%s: unhandled error (%d) connecting to %s\n",
1915176e21eeSChuck Lever 				__func__, -status,
1916176e21eeSChuck Lever 				xprt->address_strings[RPC_DISPLAY_ADDR]);
1917176e21eeSChuck Lever 	}
1918176e21eeSChuck Lever 
1919176e21eeSChuck Lever out:
1920176e21eeSChuck Lever 	xprt_clear_connecting(xprt);
1921176e21eeSChuck Lever 	xprt_wake_pending_tasks(xprt, status);
19225cf02d09SJeff Layton 	current->flags &= ~PF_FSTRANS;
1923dc107402SJ. Bruce Fields 	return status;
1924dc107402SJ. Bruce Fields }
1925dc107402SJ. Bruce Fields 
1926b6669737SLinus Torvalds static void xs_local_connect(struct rpc_xprt *xprt, struct rpc_task *task)
1927dc107402SJ. Bruce Fields {
1928dc107402SJ. Bruce Fields 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1929dc107402SJ. Bruce Fields 	int ret;
1930dc107402SJ. Bruce Fields 
1931dc107402SJ. Bruce Fields 	 if (RPC_IS_ASYNC(task)) {
1932dc107402SJ. Bruce Fields 		/*
1933dc107402SJ. Bruce Fields 		 * We want the AF_LOCAL connect to be resolved in the
1934dc107402SJ. Bruce Fields 		 * filesystem namespace of the process making the rpc
1935dc107402SJ. Bruce Fields 		 * call.  Thus we connect synchronously.
1936dc107402SJ. Bruce Fields 		 *
1937dc107402SJ. Bruce Fields 		 * If we want to support asynchronous AF_LOCAL calls,
1938dc107402SJ. Bruce Fields 		 * we'll need to figure out how to pass a namespace to
1939dc107402SJ. Bruce Fields 		 * connect.
1940dc107402SJ. Bruce Fields 		 */
1941dc107402SJ. Bruce Fields 		rpc_exit(task, -ENOTCONN);
1942dc107402SJ. Bruce Fields 		return;
1943dc107402SJ. Bruce Fields 	}
1944dc107402SJ. Bruce Fields 	ret = xs_local_setup_socket(transport);
1945dc107402SJ. Bruce Fields 	if (ret && !RPC_IS_SOFTCONN(task))
1946dc107402SJ. Bruce Fields 		msleep_interruptible(15000);
1947176e21eeSChuck Lever }
1948176e21eeSChuck Lever 
1949a564b8f0SMel Gorman #ifdef CONFIG_SUNRPC_SWAP
1950a564b8f0SMel Gorman static void xs_set_memalloc(struct rpc_xprt *xprt)
1951a564b8f0SMel Gorman {
1952a564b8f0SMel Gorman 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
1953a564b8f0SMel Gorman 			xprt);
1954a564b8f0SMel Gorman 
1955a564b8f0SMel Gorman 	if (xprt->swapper)
1956a564b8f0SMel Gorman 		sk_set_memalloc(transport->inet);
1957a564b8f0SMel Gorman }
1958a564b8f0SMel Gorman 
1959a564b8f0SMel Gorman /**
1960a564b8f0SMel Gorman  * xs_swapper - Tag this transport as being used for swap.
1961a564b8f0SMel Gorman  * @xprt: transport to tag
1962a564b8f0SMel Gorman  * @enable: enable/disable
1963a564b8f0SMel Gorman  *
1964a564b8f0SMel Gorman  */
1965a564b8f0SMel Gorman int xs_swapper(struct rpc_xprt *xprt, int enable)
1966a564b8f0SMel Gorman {
1967a564b8f0SMel Gorman 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
1968a564b8f0SMel Gorman 			xprt);
1969a564b8f0SMel Gorman 	int err = 0;
1970a564b8f0SMel Gorman 
1971a564b8f0SMel Gorman 	if (enable) {
1972a564b8f0SMel Gorman 		xprt->swapper++;
1973a564b8f0SMel Gorman 		xs_set_memalloc(xprt);
1974a564b8f0SMel Gorman 	} else if (xprt->swapper) {
1975a564b8f0SMel Gorman 		xprt->swapper--;
1976a564b8f0SMel Gorman 		sk_clear_memalloc(transport->inet);
1977a564b8f0SMel Gorman 	}
1978a564b8f0SMel Gorman 
1979a564b8f0SMel Gorman 	return err;
1980a564b8f0SMel Gorman }
1981a564b8f0SMel Gorman EXPORT_SYMBOL_GPL(xs_swapper);
1982a564b8f0SMel Gorman #else
1983a564b8f0SMel Gorman static void xs_set_memalloc(struct rpc_xprt *xprt)
1984a564b8f0SMel Gorman {
1985a564b8f0SMel Gorman }
1986a564b8f0SMel Gorman #endif
1987a564b8f0SMel Gorman 
198816be2d20SChuck Lever static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
1989a246b010SChuck Lever {
199016be2d20SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1991edb267a6SChuck Lever 
1992ee0ac0c2SChuck Lever 	if (!transport->inet) {
1993b0d93ad5SChuck Lever 		struct sock *sk = sock->sk;
1994b0d93ad5SChuck Lever 
1995b0d93ad5SChuck Lever 		write_lock_bh(&sk->sk_callback_lock);
1996b0d93ad5SChuck Lever 
19972a9e1cfaSTrond Myklebust 		xs_save_old_callbacks(transport, sk);
19982a9e1cfaSTrond Myklebust 
1999b0d93ad5SChuck Lever 		sk->sk_user_data = xprt;
2000b0d93ad5SChuck Lever 		sk->sk_data_ready = xs_udp_data_ready;
2001b0d93ad5SChuck Lever 		sk->sk_write_space = xs_udp_write_space;
2002b0d93ad5SChuck Lever 		sk->sk_no_check = UDP_CSUM_NORCV;
2003b079fa7bSTrond Myklebust 		sk->sk_allocation = GFP_ATOMIC;
2004b0d93ad5SChuck Lever 
2005b0d93ad5SChuck Lever 		xprt_set_connected(xprt);
2006b0d93ad5SChuck Lever 
2007b0d93ad5SChuck Lever 		/* Reset to new socket */
2008ee0ac0c2SChuck Lever 		transport->sock = sock;
2009ee0ac0c2SChuck Lever 		transport->inet = sk;
2010b0d93ad5SChuck Lever 
2011a564b8f0SMel Gorman 		xs_set_memalloc(xprt);
2012a564b8f0SMel Gorman 
2013b0d93ad5SChuck Lever 		write_unlock_bh(&sk->sk_callback_lock);
2014b0d93ad5SChuck Lever 	}
2015470056c2SChuck Lever 	xs_udp_do_set_buffer_size(xprt);
201616be2d20SChuck Lever }
201716be2d20SChuck Lever 
20188c14ff2aSPavel Emelyanov static void xs_udp_setup_socket(struct work_struct *work)
2019a246b010SChuck Lever {
2020a246b010SChuck Lever 	struct sock_xprt *transport =
2021a246b010SChuck Lever 		container_of(work, struct sock_xprt, connect_worker.work);
2022a246b010SChuck Lever 	struct rpc_xprt *xprt = &transport->xprt;
2023a246b010SChuck Lever 	struct socket *sock = transport->sock;
2024b65c0310SPavel Emelyanov 	int status = -EIO;
2025a246b010SChuck Lever 
20265cf02d09SJeff Layton 	current->flags |= PF_FSTRANS;
20275cf02d09SJeff Layton 
2028a246b010SChuck Lever 	/* Start by resetting any existing state */
2029fe315e76SChuck Lever 	xs_reset_transport(transport);
20308c14ff2aSPavel Emelyanov 	sock = xs_create_sock(xprt, transport,
20318c14ff2aSPavel Emelyanov 			xs_addr(xprt)->sa_family, SOCK_DGRAM, IPPROTO_UDP);
2032b65c0310SPavel Emelyanov 	if (IS_ERR(sock))
2033a246b010SChuck Lever 		goto out;
203468e220bdSChuck Lever 
2035c740eff8SChuck Lever 	dprintk("RPC:       worker connecting xprt %p via %s to "
2036c740eff8SChuck Lever 				"%s (port %s)\n", xprt,
2037c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_PROTO],
2038c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_ADDR],
2039c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_PORT]);
204068e220bdSChuck Lever 
204168e220bdSChuck Lever 	xs_udp_finish_connecting(xprt, sock);
2042a246b010SChuck Lever 	status = 0;
2043b0d93ad5SChuck Lever out:
2044b0d93ad5SChuck Lever 	xprt_clear_connecting(xprt);
20457d1e8255STrond Myklebust 	xprt_wake_pending_tasks(xprt, status);
20465cf02d09SJeff Layton 	current->flags &= ~PF_FSTRANS;
2047b0d93ad5SChuck Lever }
2048b0d93ad5SChuck Lever 
20493167e12cSChuck Lever /*
20503167e12cSChuck Lever  * We need to preserve the port number so the reply cache on the server can
20513167e12cSChuck Lever  * find our cached RPC replies when we get around to reconnecting.
20523167e12cSChuck Lever  */
205358dddac9SPavel Emelyanov static void xs_abort_connection(struct sock_xprt *transport)
20543167e12cSChuck Lever {
20553167e12cSChuck Lever 	int result;
20563167e12cSChuck Lever 	struct sockaddr any;
20573167e12cSChuck Lever 
205858dddac9SPavel Emelyanov 	dprintk("RPC:       disconnecting xprt %p to reuse port\n", transport);
20593167e12cSChuck Lever 
20603167e12cSChuck Lever 	/*
20613167e12cSChuck Lever 	 * Disconnect the transport socket by doing a connect operation
20623167e12cSChuck Lever 	 * with AF_UNSPEC.  This should return immediately...
20633167e12cSChuck Lever 	 */
20643167e12cSChuck Lever 	memset(&any, 0, sizeof(any));
20653167e12cSChuck Lever 	any.sa_family = AF_UNSPEC;
2066ee0ac0c2SChuck Lever 	result = kernel_connect(transport->sock, &any, sizeof(any), 0);
20677d1e8255STrond Myklebust 	if (!result)
20684bc1e68eSTrond Myklebust 		xs_sock_reset_connection_flags(&transport->xprt);
20694bc1e68eSTrond Myklebust 	dprintk("RPC:       AF_UNSPEC connect return code %d\n", result);
20703167e12cSChuck Lever }
20713167e12cSChuck Lever 
207258dddac9SPavel Emelyanov static void xs_tcp_reuse_connection(struct sock_xprt *transport)
207340d2549dSTrond Myklebust {
207440d2549dSTrond Myklebust 	unsigned int state = transport->inet->sk_state;
207540d2549dSTrond Myklebust 
2076669502ffSAndy Chittenden 	if (state == TCP_CLOSE && transport->sock->state == SS_UNCONNECTED) {
2077669502ffSAndy Chittenden 		/* we don't need to abort the connection if the socket
2078669502ffSAndy Chittenden 		 * hasn't undergone a shutdown
2079669502ffSAndy Chittenden 		 */
2080669502ffSAndy Chittenden 		if (transport->inet->sk_shutdown == 0)
208140d2549dSTrond Myklebust 			return;
2082669502ffSAndy Chittenden 		dprintk("RPC:       %s: TCP_CLOSEd and sk_shutdown set to %d\n",
2083669502ffSAndy Chittenden 				__func__, transport->inet->sk_shutdown);
2084669502ffSAndy Chittenden 	}
2085669502ffSAndy Chittenden 	if ((1 << state) & (TCPF_ESTABLISHED|TCPF_SYN_SENT)) {
2086669502ffSAndy Chittenden 		/* we don't need to abort the connection if the socket
2087669502ffSAndy Chittenden 		 * hasn't undergone a shutdown
2088669502ffSAndy Chittenden 		 */
2089669502ffSAndy Chittenden 		if (transport->inet->sk_shutdown == 0)
209040d2549dSTrond Myklebust 			return;
2091669502ffSAndy Chittenden 		dprintk("RPC:       %s: ESTABLISHED/SYN_SENT "
2092669502ffSAndy Chittenden 				"sk_shutdown set to %d\n",
2093669502ffSAndy Chittenden 				__func__, transport->inet->sk_shutdown);
2094669502ffSAndy Chittenden 	}
209558dddac9SPavel Emelyanov 	xs_abort_connection(transport);
209640d2549dSTrond Myklebust }
209740d2549dSTrond Myklebust 
209816be2d20SChuck Lever static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
2099b0d93ad5SChuck Lever {
210016be2d20SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2101fe19a96bSTrond Myklebust 	int ret = -ENOTCONN;
2102edb267a6SChuck Lever 
2103ee0ac0c2SChuck Lever 	if (!transport->inet) {
2104b0d93ad5SChuck Lever 		struct sock *sk = sock->sk;
2105b0d93ad5SChuck Lever 
2106b0d93ad5SChuck Lever 		write_lock_bh(&sk->sk_callback_lock);
2107b0d93ad5SChuck Lever 
21082a9e1cfaSTrond Myklebust 		xs_save_old_callbacks(transport, sk);
21092a9e1cfaSTrond Myklebust 
2110b0d93ad5SChuck Lever 		sk->sk_user_data = xprt;
2111b0d93ad5SChuck Lever 		sk->sk_data_ready = xs_tcp_data_ready;
2112b0d93ad5SChuck Lever 		sk->sk_state_change = xs_tcp_state_change;
2113b0d93ad5SChuck Lever 		sk->sk_write_space = xs_tcp_write_space;
2114b079fa7bSTrond Myklebust 		sk->sk_allocation = GFP_ATOMIC;
21153167e12cSChuck Lever 
21163167e12cSChuck Lever 		/* socket options */
21173167e12cSChuck Lever 		sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
21183167e12cSChuck Lever 		sock_reset_flag(sk, SOCK_LINGER);
21193167e12cSChuck Lever 		tcp_sk(sk)->linger2 = 0;
21203167e12cSChuck Lever 		tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF;
2121b0d93ad5SChuck Lever 
2122b0d93ad5SChuck Lever 		xprt_clear_connected(xprt);
2123b0d93ad5SChuck Lever 
2124b0d93ad5SChuck Lever 		/* Reset to new socket */
2125ee0ac0c2SChuck Lever 		transport->sock = sock;
2126ee0ac0c2SChuck Lever 		transport->inet = sk;
2127b0d93ad5SChuck Lever 
2128b0d93ad5SChuck Lever 		write_unlock_bh(&sk->sk_callback_lock);
2129b0d93ad5SChuck Lever 	}
2130b0d93ad5SChuck Lever 
213101d37c42STrond Myklebust 	if (!xprt_bound(xprt))
2132fe19a96bSTrond Myklebust 		goto out;
213301d37c42STrond Myklebust 
2134a564b8f0SMel Gorman 	xs_set_memalloc(xprt);
2135a564b8f0SMel Gorman 
2136b0d93ad5SChuck Lever 	/* Tell the socket layer to start connecting... */
2137262ca07dSChuck Lever 	xprt->stat.connect_count++;
2138262ca07dSChuck Lever 	xprt->stat.connect_start = jiffies;
2139fe19a96bSTrond Myklebust 	ret = kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK);
2140fe19a96bSTrond Myklebust 	switch (ret) {
2141fe19a96bSTrond Myklebust 	case 0:
2142fe19a96bSTrond Myklebust 	case -EINPROGRESS:
2143fe19a96bSTrond Myklebust 		/* SYN_SENT! */
2144fe19a96bSTrond Myklebust 		xprt->connect_cookie++;
2145fe19a96bSTrond Myklebust 		if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
2146fe19a96bSTrond Myklebust 			xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2147fe19a96bSTrond Myklebust 	}
2148fe19a96bSTrond Myklebust out:
2149fe19a96bSTrond Myklebust 	return ret;
215016be2d20SChuck Lever }
215116be2d20SChuck Lever 
215216be2d20SChuck Lever /**
2153b61d59ffSTrond Myklebust  * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint
2154b61d59ffSTrond Myklebust  * @xprt: RPC transport to connect
2155b61d59ffSTrond Myklebust  * @transport: socket transport to connect
2156b61d59ffSTrond Myklebust  * @create_sock: function to create a socket of the correct type
215716be2d20SChuck Lever  *
215816be2d20SChuck Lever  * Invoked by a work queue tasklet.
215916be2d20SChuck Lever  */
2160cdd518d5SPavel Emelyanov static void xs_tcp_setup_socket(struct work_struct *work)
216116be2d20SChuck Lever {
2162cdd518d5SPavel Emelyanov 	struct sock_xprt *transport =
2163cdd518d5SPavel Emelyanov 		container_of(work, struct sock_xprt, connect_worker.work);
216416be2d20SChuck Lever 	struct socket *sock = transport->sock;
2165a9f5f0f7SPavel Emelyanov 	struct rpc_xprt *xprt = &transport->xprt;
2166b61d59ffSTrond Myklebust 	int status = -EIO;
216716be2d20SChuck Lever 
21685cf02d09SJeff Layton 	current->flags |= PF_FSTRANS;
21695cf02d09SJeff Layton 
217016be2d20SChuck Lever 	if (!sock) {
21717d1e8255STrond Myklebust 		clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
2172cdd518d5SPavel Emelyanov 		sock = xs_create_sock(xprt, transport,
2173cdd518d5SPavel Emelyanov 				xs_addr(xprt)->sa_family, SOCK_STREAM, IPPROTO_TCP);
2174b61d59ffSTrond Myklebust 		if (IS_ERR(sock)) {
2175b61d59ffSTrond Myklebust 			status = PTR_ERR(sock);
217616be2d20SChuck Lever 			goto out;
217716be2d20SChuck Lever 		}
21787d1e8255STrond Myklebust 	} else {
21797d1e8255STrond Myklebust 		int abort_and_exit;
21807d1e8255STrond Myklebust 
21817d1e8255STrond Myklebust 		abort_and_exit = test_and_clear_bit(XPRT_CONNECTION_ABORT,
21827d1e8255STrond Myklebust 				&xprt->state);
218316be2d20SChuck Lever 		/* "close" the socket, preserving the local port */
218458dddac9SPavel Emelyanov 		xs_tcp_reuse_connection(transport);
218516be2d20SChuck Lever 
21867d1e8255STrond Myklebust 		if (abort_and_exit)
21877d1e8255STrond Myklebust 			goto out_eagain;
21887d1e8255STrond Myklebust 	}
21897d1e8255STrond Myklebust 
2190c740eff8SChuck Lever 	dprintk("RPC:       worker connecting xprt %p via %s to "
2191c740eff8SChuck Lever 				"%s (port %s)\n", xprt,
2192c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_PROTO],
2193c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_ADDR],
2194c740eff8SChuck Lever 			xprt->address_strings[RPC_DISPLAY_PORT]);
219516be2d20SChuck Lever 
219616be2d20SChuck Lever 	status = xs_tcp_finish_connecting(xprt, sock);
2197a246b010SChuck Lever 	dprintk("RPC:       %p connect status %d connected %d sock state %d\n",
219846121cf7SChuck Lever 			xprt, -status, xprt_connected(xprt),
219946121cf7SChuck Lever 			sock->sk->sk_state);
2200a246b010SChuck Lever 	switch (status) {
2201f75e6745STrond Myklebust 	default:
2202f75e6745STrond Myklebust 		printk("%s: connect returned unhandled error %d\n",
2203f75e6745STrond Myklebust 			__func__, status);
2204f75e6745STrond Myklebust 	case -EADDRNOTAVAIL:
2205f75e6745STrond Myklebust 		/* We're probably in TIME_WAIT. Get rid of existing socket,
2206f75e6745STrond Myklebust 		 * and retry
2207f75e6745STrond Myklebust 		 */
2208a519fc7aSTrond Myklebust 		xs_tcp_force_close(xprt);
220988b5ed73STrond Myklebust 		break;
22102a491991STrond Myklebust 	case 0:
2211a246b010SChuck Lever 	case -EINPROGRESS:
2212a246b010SChuck Lever 	case -EALREADY:
22137d1e8255STrond Myklebust 		xprt_clear_connecting(xprt);
22145cf02d09SJeff Layton 		current->flags &= ~PF_FSTRANS;
22157d1e8255STrond Myklebust 		return;
22169fcfe0c8STrond Myklebust 	case -EINVAL:
22179fcfe0c8STrond Myklebust 		/* Happens, for instance, if the user specified a link
22189fcfe0c8STrond Myklebust 		 * local IPv6 address without a scope-id.
22199fcfe0c8STrond Myklebust 		 */
22203ed5e2a2STrond Myklebust 	case -ECONNREFUSED:
22213ed5e2a2STrond Myklebust 	case -ECONNRESET:
22223ed5e2a2STrond Myklebust 	case -ENETUNREACH:
22233ed5e2a2STrond Myklebust 		/* retry with existing socket, after a delay */
22249fcfe0c8STrond Myklebust 		goto out;
22258a2cec29STrond Myklebust 	}
22267d1e8255STrond Myklebust out_eagain:
22272a491991STrond Myklebust 	status = -EAGAIN;
2228a246b010SChuck Lever out:
22292226feb6SChuck Lever 	xprt_clear_connecting(xprt);
22307d1e8255STrond Myklebust 	xprt_wake_pending_tasks(xprt, status);
22315cf02d09SJeff Layton 	current->flags &= ~PF_FSTRANS;
2232a246b010SChuck Lever }
2233a246b010SChuck Lever 
223468e220bdSChuck Lever /**
22359903cd1cSChuck Lever  * xs_connect - connect a socket to a remote endpoint
22361b092092STrond Myklebust  * @xprt: pointer to transport structure
22379903cd1cSChuck Lever  * @task: address of RPC task that manages state of connect request
22389903cd1cSChuck Lever  *
22399903cd1cSChuck Lever  * TCP: If the remote end dropped the connection, delay reconnecting.
224003bf4b70SChuck Lever  *
224103bf4b70SChuck Lever  * UDP socket connects are synchronous, but we use a work queue anyway
224203bf4b70SChuck Lever  * to guarantee that even unprivileged user processes can set up a
224303bf4b70SChuck Lever  * socket on a privileged port.
224403bf4b70SChuck Lever  *
224503bf4b70SChuck Lever  * If a UDP socket connect fails, the delay behavior here prevents
224603bf4b70SChuck Lever  * retry floods (hard mounts).
22479903cd1cSChuck Lever  */
22481b092092STrond Myklebust static void xs_connect(struct rpc_xprt *xprt, struct rpc_task *task)
2249a246b010SChuck Lever {
2250ee0ac0c2SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2251a246b010SChuck Lever 
225209a21c41SChuck Lever 	if (transport->sock != NULL && !RPC_IS_SOFTCONN(task)) {
225346121cf7SChuck Lever 		dprintk("RPC:       xs_connect delayed xprt %p for %lu "
225446121cf7SChuck Lever 				"seconds\n",
225503bf4b70SChuck Lever 				xprt, xprt->reestablish_timeout / HZ);
2256c1384c9cSTrond Myklebust 		queue_delayed_work(rpciod_workqueue,
2257c1384c9cSTrond Myklebust 				   &transport->connect_worker,
225803bf4b70SChuck Lever 				   xprt->reestablish_timeout);
225903bf4b70SChuck Lever 		xprt->reestablish_timeout <<= 1;
226061d0a8e6SNeil Brown 		if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
226161d0a8e6SNeil Brown 			xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
226203bf4b70SChuck Lever 		if (xprt->reestablish_timeout > XS_TCP_MAX_REEST_TO)
226303bf4b70SChuck Lever 			xprt->reestablish_timeout = XS_TCP_MAX_REEST_TO;
22649903cd1cSChuck Lever 	} else {
22659903cd1cSChuck Lever 		dprintk("RPC:       xs_connect scheduled xprt %p\n", xprt);
2266c1384c9cSTrond Myklebust 		queue_delayed_work(rpciod_workqueue,
2267c1384c9cSTrond Myklebust 				   &transport->connect_worker, 0);
2268a246b010SChuck Lever 	}
2269a246b010SChuck Lever }
2270a246b010SChuck Lever 
2271262ca07dSChuck Lever /**
2272176e21eeSChuck Lever  * xs_local_print_stats - display AF_LOCAL socket-specifc stats
2273176e21eeSChuck Lever  * @xprt: rpc_xprt struct containing statistics
2274176e21eeSChuck Lever  * @seq: output file
2275176e21eeSChuck Lever  *
2276176e21eeSChuck Lever  */
2277176e21eeSChuck Lever static void xs_local_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2278176e21eeSChuck Lever {
2279176e21eeSChuck Lever 	long idle_time = 0;
2280176e21eeSChuck Lever 
2281176e21eeSChuck Lever 	if (xprt_connected(xprt))
2282176e21eeSChuck Lever 		idle_time = (long)(jiffies - xprt->last_used) / HZ;
2283176e21eeSChuck Lever 
2284176e21eeSChuck Lever 	seq_printf(seq, "\txprt:\tlocal %lu %lu %lu %ld %lu %lu %lu "
228515a45206SAndy Adamson 			"%llu %llu %lu %llu %llu\n",
2286176e21eeSChuck Lever 			xprt->stat.bind_count,
2287176e21eeSChuck Lever 			xprt->stat.connect_count,
2288176e21eeSChuck Lever 			xprt->stat.connect_time,
2289176e21eeSChuck Lever 			idle_time,
2290176e21eeSChuck Lever 			xprt->stat.sends,
2291176e21eeSChuck Lever 			xprt->stat.recvs,
2292176e21eeSChuck Lever 			xprt->stat.bad_xids,
2293176e21eeSChuck Lever 			xprt->stat.req_u,
229415a45206SAndy Adamson 			xprt->stat.bklog_u,
229515a45206SAndy Adamson 			xprt->stat.max_slots,
229615a45206SAndy Adamson 			xprt->stat.sending_u,
229715a45206SAndy Adamson 			xprt->stat.pending_u);
2298176e21eeSChuck Lever }
2299176e21eeSChuck Lever 
2300176e21eeSChuck Lever /**
2301262ca07dSChuck Lever  * xs_udp_print_stats - display UDP socket-specifc stats
2302262ca07dSChuck Lever  * @xprt: rpc_xprt struct containing statistics
2303262ca07dSChuck Lever  * @seq: output file
2304262ca07dSChuck Lever  *
2305262ca07dSChuck Lever  */
2306262ca07dSChuck Lever static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2307262ca07dSChuck Lever {
2308c8475461SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2309c8475461SChuck Lever 
231015a45206SAndy Adamson 	seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %llu %llu "
231115a45206SAndy Adamson 			"%lu %llu %llu\n",
2312fbfffbd5SChuck Lever 			transport->srcport,
2313262ca07dSChuck Lever 			xprt->stat.bind_count,
2314262ca07dSChuck Lever 			xprt->stat.sends,
2315262ca07dSChuck Lever 			xprt->stat.recvs,
2316262ca07dSChuck Lever 			xprt->stat.bad_xids,
2317262ca07dSChuck Lever 			xprt->stat.req_u,
231815a45206SAndy Adamson 			xprt->stat.bklog_u,
231915a45206SAndy Adamson 			xprt->stat.max_slots,
232015a45206SAndy Adamson 			xprt->stat.sending_u,
232115a45206SAndy Adamson 			xprt->stat.pending_u);
2322262ca07dSChuck Lever }
2323262ca07dSChuck Lever 
2324262ca07dSChuck Lever /**
2325262ca07dSChuck Lever  * xs_tcp_print_stats - display TCP socket-specifc stats
2326262ca07dSChuck Lever  * @xprt: rpc_xprt struct containing statistics
2327262ca07dSChuck Lever  * @seq: output file
2328262ca07dSChuck Lever  *
2329262ca07dSChuck Lever  */
2330262ca07dSChuck Lever static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2331262ca07dSChuck Lever {
2332c8475461SChuck Lever 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2333262ca07dSChuck Lever 	long idle_time = 0;
2334262ca07dSChuck Lever 
2335262ca07dSChuck Lever 	if (xprt_connected(xprt))
2336262ca07dSChuck Lever 		idle_time = (long)(jiffies - xprt->last_used) / HZ;
2337262ca07dSChuck Lever 
233815a45206SAndy Adamson 	seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu "
233915a45206SAndy Adamson 			"%llu %llu %lu %llu %llu\n",
2340fbfffbd5SChuck Lever 			transport->srcport,
2341262ca07dSChuck Lever 			xprt->stat.bind_count,
2342262ca07dSChuck Lever 			xprt->stat.connect_count,
2343262ca07dSChuck Lever 			xprt->stat.connect_time,
2344262ca07dSChuck Lever 			idle_time,
2345262ca07dSChuck Lever 			xprt->stat.sends,
2346262ca07dSChuck Lever 			xprt->stat.recvs,
2347262ca07dSChuck Lever 			xprt->stat.bad_xids,
2348262ca07dSChuck Lever 			xprt->stat.req_u,
234915a45206SAndy Adamson 			xprt->stat.bklog_u,
235015a45206SAndy Adamson 			xprt->stat.max_slots,
235115a45206SAndy Adamson 			xprt->stat.sending_u,
235215a45206SAndy Adamson 			xprt->stat.pending_u);
2353262ca07dSChuck Lever }
2354262ca07dSChuck Lever 
23554cfc7e60SRahul Iyer /*
23564cfc7e60SRahul Iyer  * Allocate a bunch of pages for a scratch buffer for the rpc code. The reason
23574cfc7e60SRahul Iyer  * we allocate pages instead doing a kmalloc like rpc_malloc is because we want
23584cfc7e60SRahul Iyer  * to use the server side send routines.
23594cfc7e60SRahul Iyer  */
23605a51f13aSH Hartley Sweeten static void *bc_malloc(struct rpc_task *task, size_t size)
23614cfc7e60SRahul Iyer {
23624cfc7e60SRahul Iyer 	struct page *page;
23634cfc7e60SRahul Iyer 	struct rpc_buffer *buf;
23644cfc7e60SRahul Iyer 
2365b8a13d03SWeston Andros Adamson 	WARN_ON_ONCE(size > PAGE_SIZE - sizeof(struct rpc_buffer));
2366b8a13d03SWeston Andros Adamson 	if (size > PAGE_SIZE - sizeof(struct rpc_buffer))
2367b8a13d03SWeston Andros Adamson 		return NULL;
23684cfc7e60SRahul Iyer 
2369b8a13d03SWeston Andros Adamson 	page = alloc_page(GFP_KERNEL);
23704cfc7e60SRahul Iyer 	if (!page)
23714cfc7e60SRahul Iyer 		return NULL;
23724cfc7e60SRahul Iyer 
23734cfc7e60SRahul Iyer 	buf = page_address(page);
23744cfc7e60SRahul Iyer 	buf->len = PAGE_SIZE;
23754cfc7e60SRahul Iyer 
23764cfc7e60SRahul Iyer 	return buf->data;
23774cfc7e60SRahul Iyer }
23784cfc7e60SRahul Iyer 
23794cfc7e60SRahul Iyer /*
23804cfc7e60SRahul Iyer  * Free the space allocated in the bc_alloc routine
23814cfc7e60SRahul Iyer  */
23825a51f13aSH Hartley Sweeten static void bc_free(void *buffer)
23834cfc7e60SRahul Iyer {
23844cfc7e60SRahul Iyer 	struct rpc_buffer *buf;
23854cfc7e60SRahul Iyer 
23864cfc7e60SRahul Iyer 	if (!buffer)
23874cfc7e60SRahul Iyer 		return;
23884cfc7e60SRahul Iyer 
23894cfc7e60SRahul Iyer 	buf = container_of(buffer, struct rpc_buffer, data);
23904cfc7e60SRahul Iyer 	free_page((unsigned long)buf);
23914cfc7e60SRahul Iyer }
23924cfc7e60SRahul Iyer 
23934cfc7e60SRahul Iyer /*
23944cfc7e60SRahul Iyer  * Use the svc_sock to send the callback. Must be called with svsk->sk_mutex
23954cfc7e60SRahul Iyer  * held. Borrows heavily from svc_tcp_sendto and xs_tcp_send_request.
23964cfc7e60SRahul Iyer  */
23974cfc7e60SRahul Iyer static int bc_sendto(struct rpc_rqst *req)
23984cfc7e60SRahul Iyer {
23994cfc7e60SRahul Iyer 	int len;
24004cfc7e60SRahul Iyer 	struct xdr_buf *xbufp = &req->rq_snd_buf;
24014cfc7e60SRahul Iyer 	struct rpc_xprt *xprt = req->rq_xprt;
24024cfc7e60SRahul Iyer 	struct sock_xprt *transport =
24034cfc7e60SRahul Iyer 				container_of(xprt, struct sock_xprt, xprt);
24044cfc7e60SRahul Iyer 	struct socket *sock = transport->sock;
24054cfc7e60SRahul Iyer 	unsigned long headoff;
24064cfc7e60SRahul Iyer 	unsigned long tailoff;
24074cfc7e60SRahul Iyer 
240861677eeeSChuck Lever 	xs_encode_stream_record_marker(xbufp);
24094cfc7e60SRahul Iyer 
24104cfc7e60SRahul Iyer 	tailoff = (unsigned long)xbufp->tail[0].iov_base & ~PAGE_MASK;
24114cfc7e60SRahul Iyer 	headoff = (unsigned long)xbufp->head[0].iov_base & ~PAGE_MASK;
24124cfc7e60SRahul Iyer 	len = svc_send_common(sock, xbufp,
24134cfc7e60SRahul Iyer 			      virt_to_page(xbufp->head[0].iov_base), headoff,
24144cfc7e60SRahul Iyer 			      xbufp->tail[0].iov_base, tailoff);
24154cfc7e60SRahul Iyer 
24164cfc7e60SRahul Iyer 	if (len != xbufp->len) {
24174cfc7e60SRahul Iyer 		printk(KERN_NOTICE "Error sending entire callback!\n");
24184cfc7e60SRahul Iyer 		len = -EAGAIN;
24194cfc7e60SRahul Iyer 	}
24204cfc7e60SRahul Iyer 
24214cfc7e60SRahul Iyer 	return len;
24224cfc7e60SRahul Iyer }
24234cfc7e60SRahul Iyer 
24244cfc7e60SRahul Iyer /*
24254cfc7e60SRahul Iyer  * The send routine. Borrows from svc_send
24264cfc7e60SRahul Iyer  */
24274cfc7e60SRahul Iyer static int bc_send_request(struct rpc_task *task)
24284cfc7e60SRahul Iyer {
24294cfc7e60SRahul Iyer 	struct rpc_rqst *req = task->tk_rqstp;
24304cfc7e60SRahul Iyer 	struct svc_xprt	*xprt;
24314cfc7e60SRahul Iyer 	u32                     len;
24324cfc7e60SRahul Iyer 
24334cfc7e60SRahul Iyer 	dprintk("sending request with xid: %08x\n", ntohl(req->rq_xid));
24344cfc7e60SRahul Iyer 	/*
24354cfc7e60SRahul Iyer 	 * Get the server socket associated with this callback xprt
24364cfc7e60SRahul Iyer 	 */
24374cfc7e60SRahul Iyer 	xprt = req->rq_xprt->bc_xprt;
24384cfc7e60SRahul Iyer 
24394cfc7e60SRahul Iyer 	/*
24404cfc7e60SRahul Iyer 	 * Grab the mutex to serialize data as the connection is shared
24414cfc7e60SRahul Iyer 	 * with the fore channel
24424cfc7e60SRahul Iyer 	 */
24434cfc7e60SRahul Iyer 	if (!mutex_trylock(&xprt->xpt_mutex)) {
24444cfc7e60SRahul Iyer 		rpc_sleep_on(&xprt->xpt_bc_pending, task, NULL);
24454cfc7e60SRahul Iyer 		if (!mutex_trylock(&xprt->xpt_mutex))
24464cfc7e60SRahul Iyer 			return -EAGAIN;
24474cfc7e60SRahul Iyer 		rpc_wake_up_queued_task(&xprt->xpt_bc_pending, task);
24484cfc7e60SRahul Iyer 	}
24494cfc7e60SRahul Iyer 	if (test_bit(XPT_DEAD, &xprt->xpt_flags))
24504cfc7e60SRahul Iyer 		len = -ENOTCONN;
24514cfc7e60SRahul Iyer 	else
24524cfc7e60SRahul Iyer 		len = bc_sendto(req);
24534cfc7e60SRahul Iyer 	mutex_unlock(&xprt->xpt_mutex);
24544cfc7e60SRahul Iyer 
24554cfc7e60SRahul Iyer 	if (len > 0)
24564cfc7e60SRahul Iyer 		len = 0;
24574cfc7e60SRahul Iyer 
24584cfc7e60SRahul Iyer 	return len;
24594cfc7e60SRahul Iyer }
24604cfc7e60SRahul Iyer 
24614cfc7e60SRahul Iyer /*
24624cfc7e60SRahul Iyer  * The close routine. Since this is client initiated, we do nothing
24634cfc7e60SRahul Iyer  */
24644cfc7e60SRahul Iyer 
24654cfc7e60SRahul Iyer static void bc_close(struct rpc_xprt *xprt)
24664cfc7e60SRahul Iyer {
24674cfc7e60SRahul Iyer }
24684cfc7e60SRahul Iyer 
24694cfc7e60SRahul Iyer /*
24704cfc7e60SRahul Iyer  * The xprt destroy routine. Again, because this connection is client
24714cfc7e60SRahul Iyer  * initiated, we do nothing
24724cfc7e60SRahul Iyer  */
24734cfc7e60SRahul Iyer 
24744cfc7e60SRahul Iyer static void bc_destroy(struct rpc_xprt *xprt)
24754cfc7e60SRahul Iyer {
24764cfc7e60SRahul Iyer }
24774cfc7e60SRahul Iyer 
2478176e21eeSChuck Lever static struct rpc_xprt_ops xs_local_ops = {
2479176e21eeSChuck Lever 	.reserve_xprt		= xprt_reserve_xprt,
2480176e21eeSChuck Lever 	.release_xprt		= xs_tcp_release_xprt,
2481f39c1bfbSTrond Myklebust 	.alloc_slot		= xprt_alloc_slot,
2482176e21eeSChuck Lever 	.rpcbind		= xs_local_rpcbind,
2483176e21eeSChuck Lever 	.set_port		= xs_local_set_port,
2484dc107402SJ. Bruce Fields 	.connect		= xs_local_connect,
2485176e21eeSChuck Lever 	.buf_alloc		= rpc_malloc,
2486176e21eeSChuck Lever 	.buf_free		= rpc_free,
2487176e21eeSChuck Lever 	.send_request		= xs_local_send_request,
2488176e21eeSChuck Lever 	.set_retrans_timeout	= xprt_set_retrans_timeout_def,
2489176e21eeSChuck Lever 	.close			= xs_close,
2490190b1ecfSJ. Bruce Fields 	.destroy		= xs_local_destroy,
2491176e21eeSChuck Lever 	.print_stats		= xs_local_print_stats,
2492176e21eeSChuck Lever };
2493176e21eeSChuck Lever 
2494262965f5SChuck Lever static struct rpc_xprt_ops xs_udp_ops = {
249543118c29SChuck Lever 	.set_buffer_size	= xs_udp_set_buffer_size,
249612a80469SChuck Lever 	.reserve_xprt		= xprt_reserve_xprt_cong,
249749e9a890SChuck Lever 	.release_xprt		= xprt_release_xprt_cong,
2498f39c1bfbSTrond Myklebust 	.alloc_slot		= xprt_alloc_slot,
249945160d62SChuck Lever 	.rpcbind		= rpcb_getport_async,
250092200412SChuck Lever 	.set_port		= xs_set_port,
25019903cd1cSChuck Lever 	.connect		= xs_connect,
250202107148SChuck Lever 	.buf_alloc		= rpc_malloc,
250302107148SChuck Lever 	.buf_free		= rpc_free,
2504262965f5SChuck Lever 	.send_request		= xs_udp_send_request,
2505fe3aca29SChuck Lever 	.set_retrans_timeout	= xprt_set_retrans_timeout_rtt,
250646c0ee8bSChuck Lever 	.timer			= xs_udp_timer,
2507a58dd398SChuck Lever 	.release_request	= xprt_release_rqst_cong,
2508262965f5SChuck Lever 	.close			= xs_close,
2509262965f5SChuck Lever 	.destroy		= xs_destroy,
2510262ca07dSChuck Lever 	.print_stats		= xs_udp_print_stats,
2511262965f5SChuck Lever };
2512262965f5SChuck Lever 
2513262965f5SChuck Lever static struct rpc_xprt_ops xs_tcp_ops = {
251412a80469SChuck Lever 	.reserve_xprt		= xprt_reserve_xprt,
2515e0ab53deSTrond Myklebust 	.release_xprt		= xs_tcp_release_xprt,
2516f39c1bfbSTrond Myklebust 	.alloc_slot		= xprt_lock_and_alloc_slot,
251745160d62SChuck Lever 	.rpcbind		= rpcb_getport_async,
251892200412SChuck Lever 	.set_port		= xs_set_port,
25190b9e7943STrond Myklebust 	.connect		= xs_connect,
252002107148SChuck Lever 	.buf_alloc		= rpc_malloc,
252102107148SChuck Lever 	.buf_free		= rpc_free,
2522262965f5SChuck Lever 	.send_request		= xs_tcp_send_request,
2523fe3aca29SChuck Lever 	.set_retrans_timeout	= xprt_set_retrans_timeout_def,
2524f75e6745STrond Myklebust 	.close			= xs_tcp_close,
25259903cd1cSChuck Lever 	.destroy		= xs_destroy,
2526262ca07dSChuck Lever 	.print_stats		= xs_tcp_print_stats,
2527a246b010SChuck Lever };
2528a246b010SChuck Lever 
25294cfc7e60SRahul Iyer /*
25304cfc7e60SRahul Iyer  * The rpc_xprt_ops for the server backchannel
25314cfc7e60SRahul Iyer  */
25324cfc7e60SRahul Iyer 
25334cfc7e60SRahul Iyer static struct rpc_xprt_ops bc_tcp_ops = {
25344cfc7e60SRahul Iyer 	.reserve_xprt		= xprt_reserve_xprt,
25354cfc7e60SRahul Iyer 	.release_xprt		= xprt_release_xprt,
253684e28a30SBryan Schumaker 	.alloc_slot		= xprt_alloc_slot,
253792769108SJ. Bruce Fields 	.rpcbind		= xs_local_rpcbind,
25384cfc7e60SRahul Iyer 	.buf_alloc		= bc_malloc,
25394cfc7e60SRahul Iyer 	.buf_free		= bc_free,
25404cfc7e60SRahul Iyer 	.send_request		= bc_send_request,
25414cfc7e60SRahul Iyer 	.set_retrans_timeout	= xprt_set_retrans_timeout_def,
25424cfc7e60SRahul Iyer 	.close			= bc_close,
25434cfc7e60SRahul Iyer 	.destroy		= bc_destroy,
25444cfc7e60SRahul Iyer 	.print_stats		= xs_tcp_print_stats,
25454cfc7e60SRahul Iyer };
25464cfc7e60SRahul Iyer 
254792476850SChuck Lever static int xs_init_anyaddr(const int family, struct sockaddr *sap)
254892476850SChuck Lever {
254992476850SChuck Lever 	static const struct sockaddr_in sin = {
255092476850SChuck Lever 		.sin_family		= AF_INET,
255192476850SChuck Lever 		.sin_addr.s_addr	= htonl(INADDR_ANY),
255292476850SChuck Lever 	};
255392476850SChuck Lever 	static const struct sockaddr_in6 sin6 = {
255492476850SChuck Lever 		.sin6_family		= AF_INET6,
255592476850SChuck Lever 		.sin6_addr		= IN6ADDR_ANY_INIT,
255692476850SChuck Lever 	};
255792476850SChuck Lever 
255892476850SChuck Lever 	switch (family) {
2559176e21eeSChuck Lever 	case AF_LOCAL:
2560176e21eeSChuck Lever 		break;
256192476850SChuck Lever 	case AF_INET:
256292476850SChuck Lever 		memcpy(sap, &sin, sizeof(sin));
256392476850SChuck Lever 		break;
256492476850SChuck Lever 	case AF_INET6:
256592476850SChuck Lever 		memcpy(sap, &sin6, sizeof(sin6));
256692476850SChuck Lever 		break;
256792476850SChuck Lever 	default:
256892476850SChuck Lever 		dprintk("RPC:       %s: Bad address family\n", __func__);
256992476850SChuck Lever 		return -EAFNOSUPPORT;
257092476850SChuck Lever 	}
257192476850SChuck Lever 	return 0;
257292476850SChuck Lever }
257392476850SChuck Lever 
25743c341b0bS\"Talpey, Thomas\ static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args,
2575d9ba131dSTrond Myklebust 				      unsigned int slot_table_size,
2576d9ba131dSTrond Myklebust 				      unsigned int max_slot_table_size)
2577c8541ecdSChuck Lever {
2578c8541ecdSChuck Lever 	struct rpc_xprt *xprt;
2579ffc2e518SChuck Lever 	struct sock_xprt *new;
2580c8541ecdSChuck Lever 
258196802a09SFrank van Maarseveen 	if (args->addrlen > sizeof(xprt->addr)) {
2582c8541ecdSChuck Lever 		dprintk("RPC:       xs_setup_xprt: address too large\n");
2583c8541ecdSChuck Lever 		return ERR_PTR(-EBADF);
2584c8541ecdSChuck Lever 	}
2585c8541ecdSChuck Lever 
2586d9ba131dSTrond Myklebust 	xprt = xprt_alloc(args->net, sizeof(*new), slot_table_size,
2587d9ba131dSTrond Myklebust 			max_slot_table_size);
2588bd1722d4SPavel Emelyanov 	if (xprt == NULL) {
258946121cf7SChuck Lever 		dprintk("RPC:       xs_setup_xprt: couldn't allocate "
259046121cf7SChuck Lever 				"rpc_xprt\n");
2591c8541ecdSChuck Lever 		return ERR_PTR(-ENOMEM);
2592c8541ecdSChuck Lever 	}
2593c8541ecdSChuck Lever 
2594bd1722d4SPavel Emelyanov 	new = container_of(xprt, struct sock_xprt, xprt);
259596802a09SFrank van Maarseveen 	memcpy(&xprt->addr, args->dstaddr, args->addrlen);
259696802a09SFrank van Maarseveen 	xprt->addrlen = args->addrlen;
2597d3bc9a1dSFrank van Maarseveen 	if (args->srcaddr)
2598fbfffbd5SChuck Lever 		memcpy(&new->srcaddr, args->srcaddr, args->addrlen);
259992476850SChuck Lever 	else {
260092476850SChuck Lever 		int err;
260192476850SChuck Lever 		err = xs_init_anyaddr(args->dstaddr->sa_family,
260292476850SChuck Lever 					(struct sockaddr *)&new->srcaddr);
26032aa13531SStanislav Kinsbursky 		if (err != 0) {
26042aa13531SStanislav Kinsbursky 			xprt_free(xprt);
260592476850SChuck Lever 			return ERR_PTR(err);
260692476850SChuck Lever 		}
26072aa13531SStanislav Kinsbursky 	}
2608c8541ecdSChuck Lever 
2609c8541ecdSChuck Lever 	return xprt;
2610c8541ecdSChuck Lever }
2611c8541ecdSChuck Lever 
2612176e21eeSChuck Lever static const struct rpc_timeout xs_local_default_timeout = {
2613176e21eeSChuck Lever 	.to_initval = 10 * HZ,
2614176e21eeSChuck Lever 	.to_maxval = 10 * HZ,
2615176e21eeSChuck Lever 	.to_retries = 2,
2616176e21eeSChuck Lever };
2617176e21eeSChuck Lever 
2618176e21eeSChuck Lever /**
2619176e21eeSChuck Lever  * xs_setup_local - Set up transport to use an AF_LOCAL socket
2620176e21eeSChuck Lever  * @args: rpc transport creation arguments
2621176e21eeSChuck Lever  *
2622176e21eeSChuck Lever  * AF_LOCAL is a "tpi_cots_ord" transport, just like TCP
2623176e21eeSChuck Lever  */
2624176e21eeSChuck Lever static struct rpc_xprt *xs_setup_local(struct xprt_create *args)
2625176e21eeSChuck Lever {
2626176e21eeSChuck Lever 	struct sockaddr_un *sun = (struct sockaddr_un *)args->dstaddr;
2627176e21eeSChuck Lever 	struct sock_xprt *transport;
2628176e21eeSChuck Lever 	struct rpc_xprt *xprt;
2629176e21eeSChuck Lever 	struct rpc_xprt *ret;
2630176e21eeSChuck Lever 
2631d9ba131dSTrond Myklebust 	xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
2632d9ba131dSTrond Myklebust 			xprt_max_tcp_slot_table_entries);
2633176e21eeSChuck Lever 	if (IS_ERR(xprt))
2634176e21eeSChuck Lever 		return xprt;
2635176e21eeSChuck Lever 	transport = container_of(xprt, struct sock_xprt, xprt);
2636176e21eeSChuck Lever 
2637176e21eeSChuck Lever 	xprt->prot = 0;
2638176e21eeSChuck Lever 	xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
2639176e21eeSChuck Lever 	xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2640176e21eeSChuck Lever 
2641176e21eeSChuck Lever 	xprt->bind_timeout = XS_BIND_TO;
2642176e21eeSChuck Lever 	xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2643176e21eeSChuck Lever 	xprt->idle_timeout = XS_IDLE_DISC_TO;
2644176e21eeSChuck Lever 
2645176e21eeSChuck Lever 	xprt->ops = &xs_local_ops;
2646176e21eeSChuck Lever 	xprt->timeout = &xs_local_default_timeout;
2647176e21eeSChuck Lever 
2648176e21eeSChuck Lever 	switch (sun->sun_family) {
2649176e21eeSChuck Lever 	case AF_LOCAL:
2650176e21eeSChuck Lever 		if (sun->sun_path[0] != '/') {
2651176e21eeSChuck Lever 			dprintk("RPC:       bad AF_LOCAL address: %s\n",
2652176e21eeSChuck Lever 					sun->sun_path);
2653176e21eeSChuck Lever 			ret = ERR_PTR(-EINVAL);
2654176e21eeSChuck Lever 			goto out_err;
2655176e21eeSChuck Lever 		}
2656176e21eeSChuck Lever 		xprt_set_bound(xprt);
2657176e21eeSChuck Lever 		xs_format_peer_addresses(xprt, "local", RPCBIND_NETID_LOCAL);
2658*7073ea87SJ. Bruce Fields 		ret = ERR_PTR(xs_local_setup_socket(transport));
2659*7073ea87SJ. Bruce Fields 		if (ret)
2660*7073ea87SJ. Bruce Fields 			goto out_err;
2661176e21eeSChuck Lever 		break;
2662176e21eeSChuck Lever 	default:
2663176e21eeSChuck Lever 		ret = ERR_PTR(-EAFNOSUPPORT);
2664176e21eeSChuck Lever 		goto out_err;
2665176e21eeSChuck Lever 	}
2666176e21eeSChuck Lever 
2667176e21eeSChuck Lever 	dprintk("RPC:       set up xprt to %s via AF_LOCAL\n",
2668176e21eeSChuck Lever 			xprt->address_strings[RPC_DISPLAY_ADDR]);
2669176e21eeSChuck Lever 
2670176e21eeSChuck Lever 	if (try_module_get(THIS_MODULE))
2671176e21eeSChuck Lever 		return xprt;
2672176e21eeSChuck Lever 	ret = ERR_PTR(-EINVAL);
2673176e21eeSChuck Lever out_err:
2674176e21eeSChuck Lever 	xprt_free(xprt);
2675176e21eeSChuck Lever 	return ret;
2676176e21eeSChuck Lever }
2677176e21eeSChuck Lever 
26782881ae74STrond Myklebust static const struct rpc_timeout xs_udp_default_timeout = {
26792881ae74STrond Myklebust 	.to_initval = 5 * HZ,
26802881ae74STrond Myklebust 	.to_maxval = 30 * HZ,
26812881ae74STrond Myklebust 	.to_increment = 5 * HZ,
26822881ae74STrond Myklebust 	.to_retries = 5,
26832881ae74STrond Myklebust };
26842881ae74STrond Myklebust 
26859903cd1cSChuck Lever /**
26869903cd1cSChuck Lever  * xs_setup_udp - Set up transport to use a UDP socket
268796802a09SFrank van Maarseveen  * @args: rpc transport creation arguments
26889903cd1cSChuck Lever  *
26899903cd1cSChuck Lever  */
2690483066d6SAdrian Bunk static struct rpc_xprt *xs_setup_udp(struct xprt_create *args)
2691a246b010SChuck Lever {
26928f9d5b1aSChuck Lever 	struct sockaddr *addr = args->dstaddr;
2693c8541ecdSChuck Lever 	struct rpc_xprt *xprt;
2694c8475461SChuck Lever 	struct sock_xprt *transport;
26950a68b0beSJ. Bruce Fields 	struct rpc_xprt *ret;
2696a246b010SChuck Lever 
2697d9ba131dSTrond Myklebust 	xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries,
2698d9ba131dSTrond Myklebust 			xprt_udp_slot_table_entries);
2699c8541ecdSChuck Lever 	if (IS_ERR(xprt))
2700c8541ecdSChuck Lever 		return xprt;
2701c8475461SChuck Lever 	transport = container_of(xprt, struct sock_xprt, xprt);
2702a246b010SChuck Lever 
2703ec739ef0SChuck Lever 	xprt->prot = IPPROTO_UDP;
2704808012fbSChuck Lever 	xprt->tsh_size = 0;
2705a246b010SChuck Lever 	/* XXX: header size can vary due to auth type, IPv6, etc. */
2706a246b010SChuck Lever 	xprt->max_payload = (1U << 16) - (MAX_HEADER << 3);
2707a246b010SChuck Lever 
270803bf4b70SChuck Lever 	xprt->bind_timeout = XS_BIND_TO;
270903bf4b70SChuck Lever 	xprt->reestablish_timeout = XS_UDP_REEST_TO;
271003bf4b70SChuck Lever 	xprt->idle_timeout = XS_IDLE_DISC_TO;
2711a246b010SChuck Lever 
2712262965f5SChuck Lever 	xprt->ops = &xs_udp_ops;
2713a246b010SChuck Lever 
2714ba7392bbSTrond Myklebust 	xprt->timeout = &xs_udp_default_timeout;
2715a246b010SChuck Lever 
27168f9d5b1aSChuck Lever 	switch (addr->sa_family) {
27178f9d5b1aSChuck Lever 	case AF_INET:
27188f9d5b1aSChuck Lever 		if (((struct sockaddr_in *)addr)->sin_port != htons(0))
27198f9d5b1aSChuck Lever 			xprt_set_bound(xprt);
27208f9d5b1aSChuck Lever 
27218f9d5b1aSChuck Lever 		INIT_DELAYED_WORK(&transport->connect_worker,
27228c14ff2aSPavel Emelyanov 					xs_udp_setup_socket);
27239dc3b095SChuck Lever 		xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP);
27248f9d5b1aSChuck Lever 		break;
27258f9d5b1aSChuck Lever 	case AF_INET6:
27268f9d5b1aSChuck Lever 		if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
27278f9d5b1aSChuck Lever 			xprt_set_bound(xprt);
27288f9d5b1aSChuck Lever 
27298f9d5b1aSChuck Lever 		INIT_DELAYED_WORK(&transport->connect_worker,
27308c14ff2aSPavel Emelyanov 					xs_udp_setup_socket);
27319dc3b095SChuck Lever 		xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6);
27328f9d5b1aSChuck Lever 		break;
27338f9d5b1aSChuck Lever 	default:
27340a68b0beSJ. Bruce Fields 		ret = ERR_PTR(-EAFNOSUPPORT);
27350a68b0beSJ. Bruce Fields 		goto out_err;
27368f9d5b1aSChuck Lever 	}
27378f9d5b1aSChuck Lever 
2738c740eff8SChuck Lever 	if (xprt_bound(xprt))
2739c740eff8SChuck Lever 		dprintk("RPC:       set up xprt to %s (port %s) via %s\n",
2740c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_ADDR],
2741c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_PORT],
2742c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_PROTO]);
2743c740eff8SChuck Lever 	else
2744c740eff8SChuck Lever 		dprintk("RPC:       set up xprt to %s (autobind) via %s\n",
2745c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_ADDR],
2746c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_PROTO]);
2747edb267a6SChuck Lever 
2748bc25571eS\"Talpey, Thomas\ 	if (try_module_get(THIS_MODULE))
2749c8541ecdSChuck Lever 		return xprt;
27500a68b0beSJ. Bruce Fields 	ret = ERR_PTR(-EINVAL);
27510a68b0beSJ. Bruce Fields out_err:
2752e204e621SPavel Emelyanov 	xprt_free(xprt);
27530a68b0beSJ. Bruce Fields 	return ret;
2754a246b010SChuck Lever }
2755a246b010SChuck Lever 
27562881ae74STrond Myklebust static const struct rpc_timeout xs_tcp_default_timeout = {
27572881ae74STrond Myklebust 	.to_initval = 60 * HZ,
27582881ae74STrond Myklebust 	.to_maxval = 60 * HZ,
27592881ae74STrond Myklebust 	.to_retries = 2,
27602881ae74STrond Myklebust };
27612881ae74STrond Myklebust 
27629903cd1cSChuck Lever /**
27639903cd1cSChuck Lever  * xs_setup_tcp - Set up transport to use a TCP socket
276496802a09SFrank van Maarseveen  * @args: rpc transport creation arguments
27659903cd1cSChuck Lever  *
27669903cd1cSChuck Lever  */
2767483066d6SAdrian Bunk static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args)
2768a246b010SChuck Lever {
27698f9d5b1aSChuck Lever 	struct sockaddr *addr = args->dstaddr;
2770c8541ecdSChuck Lever 	struct rpc_xprt *xprt;
2771c8475461SChuck Lever 	struct sock_xprt *transport;
27720a68b0beSJ. Bruce Fields 	struct rpc_xprt *ret;
2773b7993cebSTrond Myklebust 	unsigned int max_slot_table_size = xprt_max_tcp_slot_table_entries;
2774b7993cebSTrond Myklebust 
2775b7993cebSTrond Myklebust 	if (args->flags & XPRT_CREATE_INFINITE_SLOTS)
2776b7993cebSTrond Myklebust 		max_slot_table_size = RPC_MAX_SLOT_TABLE_LIMIT;
2777a246b010SChuck Lever 
2778d9ba131dSTrond Myklebust 	xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
2779b7993cebSTrond Myklebust 			max_slot_table_size);
2780c8541ecdSChuck Lever 	if (IS_ERR(xprt))
2781c8541ecdSChuck Lever 		return xprt;
2782c8475461SChuck Lever 	transport = container_of(xprt, struct sock_xprt, xprt);
2783a246b010SChuck Lever 
2784ec739ef0SChuck Lever 	xprt->prot = IPPROTO_TCP;
2785808012fbSChuck Lever 	xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
2786808012fbSChuck Lever 	xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2787a246b010SChuck Lever 
278803bf4b70SChuck Lever 	xprt->bind_timeout = XS_BIND_TO;
278903bf4b70SChuck Lever 	xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
279003bf4b70SChuck Lever 	xprt->idle_timeout = XS_IDLE_DISC_TO;
2791a246b010SChuck Lever 
2792262965f5SChuck Lever 	xprt->ops = &xs_tcp_ops;
2793ba7392bbSTrond Myklebust 	xprt->timeout = &xs_tcp_default_timeout;
2794a246b010SChuck Lever 
27958f9d5b1aSChuck Lever 	switch (addr->sa_family) {
27968f9d5b1aSChuck Lever 	case AF_INET:
27978f9d5b1aSChuck Lever 		if (((struct sockaddr_in *)addr)->sin_port != htons(0))
27988f9d5b1aSChuck Lever 			xprt_set_bound(xprt);
27998f9d5b1aSChuck Lever 
28009dc3b095SChuck Lever 		INIT_DELAYED_WORK(&transport->connect_worker,
2801cdd518d5SPavel Emelyanov 					xs_tcp_setup_socket);
28029dc3b095SChuck Lever 		xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP);
28038f9d5b1aSChuck Lever 		break;
28048f9d5b1aSChuck Lever 	case AF_INET6:
28058f9d5b1aSChuck Lever 		if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
28068f9d5b1aSChuck Lever 			xprt_set_bound(xprt);
28078f9d5b1aSChuck Lever 
28089dc3b095SChuck Lever 		INIT_DELAYED_WORK(&transport->connect_worker,
2809cdd518d5SPavel Emelyanov 					xs_tcp_setup_socket);
28109dc3b095SChuck Lever 		xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6);
28118f9d5b1aSChuck Lever 		break;
28128f9d5b1aSChuck Lever 	default:
28130a68b0beSJ. Bruce Fields 		ret = ERR_PTR(-EAFNOSUPPORT);
28140a68b0beSJ. Bruce Fields 		goto out_err;
28158f9d5b1aSChuck Lever 	}
28168f9d5b1aSChuck Lever 
2817c740eff8SChuck Lever 	if (xprt_bound(xprt))
2818c740eff8SChuck Lever 		dprintk("RPC:       set up xprt to %s (port %s) via %s\n",
2819c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_ADDR],
2820c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_PORT],
2821c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_PROTO]);
2822c740eff8SChuck Lever 	else
2823c740eff8SChuck Lever 		dprintk("RPC:       set up xprt to %s (autobind) via %s\n",
2824c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_ADDR],
2825c740eff8SChuck Lever 				xprt->address_strings[RPC_DISPLAY_PROTO]);
2826c740eff8SChuck Lever 
2827edb267a6SChuck Lever 
2828bc25571eS\"Talpey, Thomas\ 	if (try_module_get(THIS_MODULE))
2829c8541ecdSChuck Lever 		return xprt;
28300a68b0beSJ. Bruce Fields 	ret = ERR_PTR(-EINVAL);
28310a68b0beSJ. Bruce Fields out_err:
2832e204e621SPavel Emelyanov 	xprt_free(xprt);
28330a68b0beSJ. Bruce Fields 	return ret;
2834a246b010SChuck Lever }
2835282b32e1SChuck Lever 
2836f300babaSAlexandros Batsakis /**
2837f300babaSAlexandros Batsakis  * xs_setup_bc_tcp - Set up transport to use a TCP backchannel socket
2838f300babaSAlexandros Batsakis  * @args: rpc transport creation arguments
2839f300babaSAlexandros Batsakis  *
2840f300babaSAlexandros Batsakis  */
2841f300babaSAlexandros Batsakis static struct rpc_xprt *xs_setup_bc_tcp(struct xprt_create *args)
2842f300babaSAlexandros Batsakis {
2843f300babaSAlexandros Batsakis 	struct sockaddr *addr = args->dstaddr;
2844f300babaSAlexandros Batsakis 	struct rpc_xprt *xprt;
2845f300babaSAlexandros Batsakis 	struct sock_xprt *transport;
2846f300babaSAlexandros Batsakis 	struct svc_sock *bc_sock;
28470a68b0beSJ. Bruce Fields 	struct rpc_xprt *ret;
2848f300babaSAlexandros Batsakis 
2849f0418aa4SJ. Bruce Fields 	if (args->bc_xprt->xpt_bc_xprt) {
2850f0418aa4SJ. Bruce Fields 		/*
2851f0418aa4SJ. Bruce Fields 		 * This server connection already has a backchannel
2852f0418aa4SJ. Bruce Fields 		 * export; we can't create a new one, as we wouldn't be
2853f0418aa4SJ. Bruce Fields 		 * able to match replies based on xid any more.  So,
2854f0418aa4SJ. Bruce Fields 		 * reuse the already-existing one:
2855f0418aa4SJ. Bruce Fields 		 */
2856f0418aa4SJ. Bruce Fields 		 return args->bc_xprt->xpt_bc_xprt;
2857f0418aa4SJ. Bruce Fields 	}
2858d9ba131dSTrond Myklebust 	xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
2859d9ba131dSTrond Myklebust 			xprt_tcp_slot_table_entries);
2860f300babaSAlexandros Batsakis 	if (IS_ERR(xprt))
2861f300babaSAlexandros Batsakis 		return xprt;
2862f300babaSAlexandros Batsakis 	transport = container_of(xprt, struct sock_xprt, xprt);
2863f300babaSAlexandros Batsakis 
2864f300babaSAlexandros Batsakis 	xprt->prot = IPPROTO_TCP;
2865f300babaSAlexandros Batsakis 	xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
2866f300babaSAlexandros Batsakis 	xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2867f300babaSAlexandros Batsakis 	xprt->timeout = &xs_tcp_default_timeout;
2868f300babaSAlexandros Batsakis 
2869f300babaSAlexandros Batsakis 	/* backchannel */
2870f300babaSAlexandros Batsakis 	xprt_set_bound(xprt);
2871f300babaSAlexandros Batsakis 	xprt->bind_timeout = 0;
2872f300babaSAlexandros Batsakis 	xprt->reestablish_timeout = 0;
2873f300babaSAlexandros Batsakis 	xprt->idle_timeout = 0;
2874f300babaSAlexandros Batsakis 
2875f300babaSAlexandros Batsakis 	xprt->ops = &bc_tcp_ops;
2876f300babaSAlexandros Batsakis 
2877f300babaSAlexandros Batsakis 	switch (addr->sa_family) {
2878f300babaSAlexandros Batsakis 	case AF_INET:
2879f300babaSAlexandros Batsakis 		xs_format_peer_addresses(xprt, "tcp",
2880f300babaSAlexandros Batsakis 					 RPCBIND_NETID_TCP);
2881f300babaSAlexandros Batsakis 		break;
2882f300babaSAlexandros Batsakis 	case AF_INET6:
2883f300babaSAlexandros Batsakis 		xs_format_peer_addresses(xprt, "tcp",
2884f300babaSAlexandros Batsakis 				   RPCBIND_NETID_TCP6);
2885f300babaSAlexandros Batsakis 		break;
2886f300babaSAlexandros Batsakis 	default:
28870a68b0beSJ. Bruce Fields 		ret = ERR_PTR(-EAFNOSUPPORT);
28880a68b0beSJ. Bruce Fields 		goto out_err;
2889f300babaSAlexandros Batsakis 	}
2890f300babaSAlexandros Batsakis 
2891f300babaSAlexandros Batsakis 	dprintk("RPC:       set up xprt to %s (port %s) via %s\n",
2892f300babaSAlexandros Batsakis 			xprt->address_strings[RPC_DISPLAY_ADDR],
2893f300babaSAlexandros Batsakis 			xprt->address_strings[RPC_DISPLAY_PORT],
2894f300babaSAlexandros Batsakis 			xprt->address_strings[RPC_DISPLAY_PROTO]);
2895f300babaSAlexandros Batsakis 
2896f300babaSAlexandros Batsakis 	/*
289799de8ea9SJ. Bruce Fields 	 * Once we've associated a backchannel xprt with a connection,
289899de8ea9SJ. Bruce Fields 	 * we want to keep it around as long as long as the connection
289999de8ea9SJ. Bruce Fields 	 * lasts, in case we need to start using it for a backchannel
290099de8ea9SJ. Bruce Fields 	 * again; this reference won't be dropped until bc_xprt is
290199de8ea9SJ. Bruce Fields 	 * destroyed.
290299de8ea9SJ. Bruce Fields 	 */
290399de8ea9SJ. Bruce Fields 	xprt_get(xprt);
290499de8ea9SJ. Bruce Fields 	args->bc_xprt->xpt_bc_xprt = xprt;
290599de8ea9SJ. Bruce Fields 	xprt->bc_xprt = args->bc_xprt;
290699de8ea9SJ. Bruce Fields 	bc_sock = container_of(args->bc_xprt, struct svc_sock, sk_xprt);
290799de8ea9SJ. Bruce Fields 	transport->sock = bc_sock->sk_sock;
290899de8ea9SJ. Bruce Fields 	transport->inet = bc_sock->sk_sk;
290999de8ea9SJ. Bruce Fields 
291099de8ea9SJ. Bruce Fields 	/*
2911f300babaSAlexandros Batsakis 	 * Since we don't want connections for the backchannel, we set
2912f300babaSAlexandros Batsakis 	 * the xprt status to connected
2913f300babaSAlexandros Batsakis 	 */
2914f300babaSAlexandros Batsakis 	xprt_set_connected(xprt);
2915f300babaSAlexandros Batsakis 
2916f300babaSAlexandros Batsakis 
2917f300babaSAlexandros Batsakis 	if (try_module_get(THIS_MODULE))
2918f300babaSAlexandros Batsakis 		return xprt;
291999de8ea9SJ. Bruce Fields 	xprt_put(xprt);
29200a68b0beSJ. Bruce Fields 	ret = ERR_PTR(-EINVAL);
29210a68b0beSJ. Bruce Fields out_err:
2922e204e621SPavel Emelyanov 	xprt_free(xprt);
29230a68b0beSJ. Bruce Fields 	return ret;
2924f300babaSAlexandros Batsakis }
2925f300babaSAlexandros Batsakis 
2926176e21eeSChuck Lever static struct xprt_class	xs_local_transport = {
2927176e21eeSChuck Lever 	.list		= LIST_HEAD_INIT(xs_local_transport.list),
2928176e21eeSChuck Lever 	.name		= "named UNIX socket",
2929176e21eeSChuck Lever 	.owner		= THIS_MODULE,
2930176e21eeSChuck Lever 	.ident		= XPRT_TRANSPORT_LOCAL,
2931176e21eeSChuck Lever 	.setup		= xs_setup_local,
2932176e21eeSChuck Lever };
2933176e21eeSChuck Lever 
2934bc25571eS\"Talpey, Thomas\ static struct xprt_class	xs_udp_transport = {
2935bc25571eS\"Talpey, Thomas\ 	.list		= LIST_HEAD_INIT(xs_udp_transport.list),
2936bc25571eS\"Talpey, Thomas\ 	.name		= "udp",
2937bc25571eS\"Talpey, Thomas\ 	.owner		= THIS_MODULE,
2938f300babaSAlexandros Batsakis 	.ident		= XPRT_TRANSPORT_UDP,
2939bc25571eS\"Talpey, Thomas\ 	.setup		= xs_setup_udp,
2940bc25571eS\"Talpey, Thomas\ };
2941bc25571eS\"Talpey, Thomas\ 
2942bc25571eS\"Talpey, Thomas\ static struct xprt_class	xs_tcp_transport = {
2943bc25571eS\"Talpey, Thomas\ 	.list		= LIST_HEAD_INIT(xs_tcp_transport.list),
2944bc25571eS\"Talpey, Thomas\ 	.name		= "tcp",
2945bc25571eS\"Talpey, Thomas\ 	.owner		= THIS_MODULE,
2946f300babaSAlexandros Batsakis 	.ident		= XPRT_TRANSPORT_TCP,
2947bc25571eS\"Talpey, Thomas\ 	.setup		= xs_setup_tcp,
2948bc25571eS\"Talpey, Thomas\ };
2949bc25571eS\"Talpey, Thomas\ 
2950f300babaSAlexandros Batsakis static struct xprt_class	xs_bc_tcp_transport = {
2951f300babaSAlexandros Batsakis 	.list		= LIST_HEAD_INIT(xs_bc_tcp_transport.list),
2952f300babaSAlexandros Batsakis 	.name		= "tcp NFSv4.1 backchannel",
2953f300babaSAlexandros Batsakis 	.owner		= THIS_MODULE,
2954f300babaSAlexandros Batsakis 	.ident		= XPRT_TRANSPORT_BC_TCP,
2955f300babaSAlexandros Batsakis 	.setup		= xs_setup_bc_tcp,
2956f300babaSAlexandros Batsakis };
2957f300babaSAlexandros Batsakis 
2958282b32e1SChuck Lever /**
2959bc25571eS\"Talpey, Thomas\  * init_socket_xprt - set up xprtsock's sysctls, register with RPC client
2960282b32e1SChuck Lever  *
2961282b32e1SChuck Lever  */
2962282b32e1SChuck Lever int init_socket_xprt(void)
2963282b32e1SChuck Lever {
2964fbf76683SChuck Lever #ifdef RPC_DEBUG
29652b1bec5fSEric W. Biederman 	if (!sunrpc_table_header)
29660b4d4147SEric W. Biederman 		sunrpc_table_header = register_sysctl_table(sunrpc_table);
2967fbf76683SChuck Lever #endif
2968fbf76683SChuck Lever 
2969176e21eeSChuck Lever 	xprt_register_transport(&xs_local_transport);
2970bc25571eS\"Talpey, Thomas\ 	xprt_register_transport(&xs_udp_transport);
2971bc25571eS\"Talpey, Thomas\ 	xprt_register_transport(&xs_tcp_transport);
2972f300babaSAlexandros Batsakis 	xprt_register_transport(&xs_bc_tcp_transport);
2973bc25571eS\"Talpey, Thomas\ 
2974282b32e1SChuck Lever 	return 0;
2975282b32e1SChuck Lever }
2976282b32e1SChuck Lever 
2977282b32e1SChuck Lever /**
2978bc25571eS\"Talpey, Thomas\  * cleanup_socket_xprt - remove xprtsock's sysctls, unregister
2979282b32e1SChuck Lever  *
2980282b32e1SChuck Lever  */
2981282b32e1SChuck Lever void cleanup_socket_xprt(void)
2982282b32e1SChuck Lever {
2983fbf76683SChuck Lever #ifdef RPC_DEBUG
2984fbf76683SChuck Lever 	if (sunrpc_table_header) {
2985fbf76683SChuck Lever 		unregister_sysctl_table(sunrpc_table_header);
2986fbf76683SChuck Lever 		sunrpc_table_header = NULL;
2987fbf76683SChuck Lever 	}
2988fbf76683SChuck Lever #endif
2989bc25571eS\"Talpey, Thomas\ 
2990176e21eeSChuck Lever 	xprt_unregister_transport(&xs_local_transport);
2991bc25571eS\"Talpey, Thomas\ 	xprt_unregister_transport(&xs_udp_transport);
2992bc25571eS\"Talpey, Thomas\ 	xprt_unregister_transport(&xs_tcp_transport);
2993f300babaSAlexandros Batsakis 	xprt_unregister_transport(&xs_bc_tcp_transport);
2994282b32e1SChuck Lever }
2995cbf11071STrond Myklebust 
29969bbb9e5aSRusty Russell static int param_set_uint_minmax(const char *val,
29979bbb9e5aSRusty Russell 		const struct kernel_param *kp,
2998cbf11071STrond Myklebust 		unsigned int min, unsigned int max)
2999cbf11071STrond Myklebust {
3000cbf11071STrond Myklebust 	unsigned long num;
3001cbf11071STrond Myklebust 	int ret;
3002cbf11071STrond Myklebust 
3003cbf11071STrond Myklebust 	if (!val)
3004cbf11071STrond Myklebust 		return -EINVAL;
3005cbf11071STrond Myklebust 	ret = strict_strtoul(val, 0, &num);
3006cbf11071STrond Myklebust 	if (ret == -EINVAL || num < min || num > max)
3007cbf11071STrond Myklebust 		return -EINVAL;
3008cbf11071STrond Myklebust 	*((unsigned int *)kp->arg) = num;
3009cbf11071STrond Myklebust 	return 0;
3010cbf11071STrond Myklebust }
3011cbf11071STrond Myklebust 
30129bbb9e5aSRusty Russell static int param_set_portnr(const char *val, const struct kernel_param *kp)
3013cbf11071STrond Myklebust {
3014cbf11071STrond Myklebust 	return param_set_uint_minmax(val, kp,
3015cbf11071STrond Myklebust 			RPC_MIN_RESVPORT,
3016cbf11071STrond Myklebust 			RPC_MAX_RESVPORT);
3017cbf11071STrond Myklebust }
3018cbf11071STrond Myklebust 
30199bbb9e5aSRusty Russell static struct kernel_param_ops param_ops_portnr = {
30209bbb9e5aSRusty Russell 	.set = param_set_portnr,
30219bbb9e5aSRusty Russell 	.get = param_get_uint,
30229bbb9e5aSRusty Russell };
30239bbb9e5aSRusty Russell 
3024cbf11071STrond Myklebust #define param_check_portnr(name, p) \
3025cbf11071STrond Myklebust 	__param_check(name, p, unsigned int);
3026cbf11071STrond Myklebust 
3027cbf11071STrond Myklebust module_param_named(min_resvport, xprt_min_resvport, portnr, 0644);
3028cbf11071STrond Myklebust module_param_named(max_resvport, xprt_max_resvport, portnr, 0644);
3029cbf11071STrond Myklebust 
30309bbb9e5aSRusty Russell static int param_set_slot_table_size(const char *val,
30319bbb9e5aSRusty Russell 				     const struct kernel_param *kp)
3032cbf11071STrond Myklebust {
3033cbf11071STrond Myklebust 	return param_set_uint_minmax(val, kp,
3034cbf11071STrond Myklebust 			RPC_MIN_SLOT_TABLE,
3035cbf11071STrond Myklebust 			RPC_MAX_SLOT_TABLE);
3036cbf11071STrond Myklebust }
3037cbf11071STrond Myklebust 
30389bbb9e5aSRusty Russell static struct kernel_param_ops param_ops_slot_table_size = {
30399bbb9e5aSRusty Russell 	.set = param_set_slot_table_size,
30409bbb9e5aSRusty Russell 	.get = param_get_uint,
30419bbb9e5aSRusty Russell };
30429bbb9e5aSRusty Russell 
3043cbf11071STrond Myklebust #define param_check_slot_table_size(name, p) \
3044cbf11071STrond Myklebust 	__param_check(name, p, unsigned int);
3045cbf11071STrond Myklebust 
3046d9ba131dSTrond Myklebust static int param_set_max_slot_table_size(const char *val,
3047d9ba131dSTrond Myklebust 				     const struct kernel_param *kp)
3048d9ba131dSTrond Myklebust {
3049d9ba131dSTrond Myklebust 	return param_set_uint_minmax(val, kp,
3050d9ba131dSTrond Myklebust 			RPC_MIN_SLOT_TABLE,
3051d9ba131dSTrond Myklebust 			RPC_MAX_SLOT_TABLE_LIMIT);
3052d9ba131dSTrond Myklebust }
3053d9ba131dSTrond Myklebust 
3054d9ba131dSTrond Myklebust static struct kernel_param_ops param_ops_max_slot_table_size = {
3055d9ba131dSTrond Myklebust 	.set = param_set_max_slot_table_size,
3056d9ba131dSTrond Myklebust 	.get = param_get_uint,
3057d9ba131dSTrond Myklebust };
3058d9ba131dSTrond Myklebust 
3059d9ba131dSTrond Myklebust #define param_check_max_slot_table_size(name, p) \
3060d9ba131dSTrond Myklebust 	__param_check(name, p, unsigned int);
3061d9ba131dSTrond Myklebust 
3062cbf11071STrond Myklebust module_param_named(tcp_slot_table_entries, xprt_tcp_slot_table_entries,
3063cbf11071STrond Myklebust 		   slot_table_size, 0644);
3064d9ba131dSTrond Myklebust module_param_named(tcp_max_slot_table_entries, xprt_max_tcp_slot_table_entries,
3065d9ba131dSTrond Myklebust 		   max_slot_table_size, 0644);
3066cbf11071STrond Myklebust module_param_named(udp_slot_table_entries, xprt_udp_slot_table_entries,
3067cbf11071STrond Myklebust 		   slot_table_size, 0644);
3068cbf11071STrond Myklebust 
3069