xref: /openbmc/linux/net/sunrpc/xprtsock.c (revision e23feb16)
1 /*
2  * linux/net/sunrpc/xprtsock.c
3  *
4  * Client-side transport implementation for sockets.
5  *
6  * TCP callback races fixes (C) 1998 Red Hat
7  * TCP send fixes (C) 1998 Red Hat
8  * TCP NFS related read + write fixes
9  *  (C) 1999 Dave Airlie, University of Limerick, Ireland <airlied@linux.ie>
10  *
11  * Rewrite of larges part of the code in order to stabilize TCP stuff.
12  * Fix behaviour when socket buffer is full.
13  *  (C) 1999 Trond Myklebust <trond.myklebust@fys.uio.no>
14  *
15  * IP socket transport implementation, (C) 2005 Chuck Lever <cel@netapp.com>
16  *
17  * IPv6 support contributed by Gilles Quillard, Bull Open Source, 2005.
18  *   <gilles.quillard@bull.net>
19  */
20 
21 #include <linux/types.h>
22 #include <linux/string.h>
23 #include <linux/slab.h>
24 #include <linux/module.h>
25 #include <linux/capability.h>
26 #include <linux/pagemap.h>
27 #include <linux/errno.h>
28 #include <linux/socket.h>
29 #include <linux/in.h>
30 #include <linux/net.h>
31 #include <linux/mm.h>
32 #include <linux/un.h>
33 #include <linux/udp.h>
34 #include <linux/tcp.h>
35 #include <linux/sunrpc/clnt.h>
36 #include <linux/sunrpc/addr.h>
37 #include <linux/sunrpc/sched.h>
38 #include <linux/sunrpc/svcsock.h>
39 #include <linux/sunrpc/xprtsock.h>
40 #include <linux/file.h>
41 #ifdef CONFIG_SUNRPC_BACKCHANNEL
42 #include <linux/sunrpc/bc_xprt.h>
43 #endif
44 
45 #include <net/sock.h>
46 #include <net/checksum.h>
47 #include <net/udp.h>
48 #include <net/tcp.h>
49 
50 #include <trace/events/sunrpc.h>
51 
52 #include "sunrpc.h"
53 
54 static void xs_close(struct rpc_xprt *xprt);
55 
56 /*
57  * xprtsock tunables
58  */
59 static unsigned int xprt_udp_slot_table_entries = RPC_DEF_SLOT_TABLE;
60 static unsigned int xprt_tcp_slot_table_entries = RPC_MIN_SLOT_TABLE;
61 static unsigned int xprt_max_tcp_slot_table_entries = RPC_MAX_SLOT_TABLE;
62 
63 static unsigned int xprt_min_resvport = RPC_DEF_MIN_RESVPORT;
64 static unsigned int xprt_max_resvport = RPC_DEF_MAX_RESVPORT;
65 
66 #define XS_TCP_LINGER_TO	(15U * HZ)
67 static unsigned int xs_tcp_fin_timeout __read_mostly = XS_TCP_LINGER_TO;
68 
69 /*
70  * We can register our own files under /proc/sys/sunrpc by
71  * calling register_sysctl_table() again.  The files in that
72  * directory become the union of all files registered there.
73  *
74  * We simply need to make sure that we don't collide with
75  * someone else's file names!
76  */
77 
78 #ifdef RPC_DEBUG
79 
80 static unsigned int min_slot_table_size = RPC_MIN_SLOT_TABLE;
81 static unsigned int max_slot_table_size = RPC_MAX_SLOT_TABLE;
82 static unsigned int max_tcp_slot_table_limit = RPC_MAX_SLOT_TABLE_LIMIT;
83 static unsigned int xprt_min_resvport_limit = RPC_MIN_RESVPORT;
84 static unsigned int xprt_max_resvport_limit = RPC_MAX_RESVPORT;
85 
86 static struct ctl_table_header *sunrpc_table_header;
87 
88 /*
89  * FIXME: changing the UDP slot table size should also resize the UDP
90  *        socket buffers for existing UDP transports
91  */
92 static struct ctl_table xs_tunables_table[] = {
93 	{
94 		.procname	= "udp_slot_table_entries",
95 		.data		= &xprt_udp_slot_table_entries,
96 		.maxlen		= sizeof(unsigned int),
97 		.mode		= 0644,
98 		.proc_handler	= proc_dointvec_minmax,
99 		.extra1		= &min_slot_table_size,
100 		.extra2		= &max_slot_table_size
101 	},
102 	{
103 		.procname	= "tcp_slot_table_entries",
104 		.data		= &xprt_tcp_slot_table_entries,
105 		.maxlen		= sizeof(unsigned int),
106 		.mode		= 0644,
107 		.proc_handler	= proc_dointvec_minmax,
108 		.extra1		= &min_slot_table_size,
109 		.extra2		= &max_slot_table_size
110 	},
111 	{
112 		.procname	= "tcp_max_slot_table_entries",
113 		.data		= &xprt_max_tcp_slot_table_entries,
114 		.maxlen		= sizeof(unsigned int),
115 		.mode		= 0644,
116 		.proc_handler	= proc_dointvec_minmax,
117 		.extra1		= &min_slot_table_size,
118 		.extra2		= &max_tcp_slot_table_limit
119 	},
120 	{
121 		.procname	= "min_resvport",
122 		.data		= &xprt_min_resvport,
123 		.maxlen		= sizeof(unsigned int),
124 		.mode		= 0644,
125 		.proc_handler	= proc_dointvec_minmax,
126 		.extra1		= &xprt_min_resvport_limit,
127 		.extra2		= &xprt_max_resvport_limit
128 	},
129 	{
130 		.procname	= "max_resvport",
131 		.data		= &xprt_max_resvport,
132 		.maxlen		= sizeof(unsigned int),
133 		.mode		= 0644,
134 		.proc_handler	= proc_dointvec_minmax,
135 		.extra1		= &xprt_min_resvport_limit,
136 		.extra2		= &xprt_max_resvport_limit
137 	},
138 	{
139 		.procname	= "tcp_fin_timeout",
140 		.data		= &xs_tcp_fin_timeout,
141 		.maxlen		= sizeof(xs_tcp_fin_timeout),
142 		.mode		= 0644,
143 		.proc_handler	= proc_dointvec_jiffies,
144 	},
145 	{ },
146 };
147 
148 static struct ctl_table sunrpc_table[] = {
149 	{
150 		.procname	= "sunrpc",
151 		.mode		= 0555,
152 		.child		= xs_tunables_table
153 	},
154 	{ },
155 };
156 
157 #endif
158 
159 /*
160  * Wait duration for a reply from the RPC portmapper.
161  */
162 #define XS_BIND_TO		(60U * HZ)
163 
164 /*
165  * Delay if a UDP socket connect error occurs.  This is most likely some
166  * kind of resource problem on the local host.
167  */
168 #define XS_UDP_REEST_TO		(2U * HZ)
169 
170 /*
171  * The reestablish timeout allows clients to delay for a bit before attempting
172  * to reconnect to a server that just dropped our connection.
173  *
174  * We implement an exponential backoff when trying to reestablish a TCP
175  * transport connection with the server.  Some servers like to drop a TCP
176  * connection when they are overworked, so we start with a short timeout and
177  * increase over time if the server is down or not responding.
178  */
179 #define XS_TCP_INIT_REEST_TO	(3U * HZ)
180 #define XS_TCP_MAX_REEST_TO	(5U * 60 * HZ)
181 
182 /*
183  * TCP idle timeout; client drops the transport socket if it is idle
184  * for this long.  Note that we also timeout UDP sockets to prevent
185  * holding port numbers when there is no RPC traffic.
186  */
187 #define XS_IDLE_DISC_TO		(5U * 60 * HZ)
188 
189 #ifdef RPC_DEBUG
190 # undef  RPC_DEBUG_DATA
191 # define RPCDBG_FACILITY	RPCDBG_TRANS
192 #endif
193 
194 #ifdef RPC_DEBUG_DATA
195 static void xs_pktdump(char *msg, u32 *packet, unsigned int count)
196 {
197 	u8 *buf = (u8 *) packet;
198 	int j;
199 
200 	dprintk("RPC:       %s\n", msg);
201 	for (j = 0; j < count && j < 128; j += 4) {
202 		if (!(j & 31)) {
203 			if (j)
204 				dprintk("\n");
205 			dprintk("0x%04x ", j);
206 		}
207 		dprintk("%02x%02x%02x%02x ",
208 			buf[j], buf[j+1], buf[j+2], buf[j+3]);
209 	}
210 	dprintk("\n");
211 }
212 #else
213 static inline void xs_pktdump(char *msg, u32 *packet, unsigned int count)
214 {
215 	/* NOP */
216 }
217 #endif
218 
219 struct sock_xprt {
220 	struct rpc_xprt		xprt;
221 
222 	/*
223 	 * Network layer
224 	 */
225 	struct socket *		sock;
226 	struct sock *		inet;
227 
228 	/*
229 	 * State of TCP reply receive
230 	 */
231 	__be32			tcp_fraghdr,
232 				tcp_xid,
233 				tcp_calldir;
234 
235 	u32			tcp_offset,
236 				tcp_reclen;
237 
238 	unsigned long		tcp_copied,
239 				tcp_flags;
240 
241 	/*
242 	 * Connection of transports
243 	 */
244 	struct delayed_work	connect_worker;
245 	struct sockaddr_storage	srcaddr;
246 	unsigned short		srcport;
247 
248 	/*
249 	 * UDP socket buffer size parameters
250 	 */
251 	size_t			rcvsize,
252 				sndsize;
253 
254 	/*
255 	 * Saved socket callback addresses
256 	 */
257 	void			(*old_data_ready)(struct sock *, int);
258 	void			(*old_state_change)(struct sock *);
259 	void			(*old_write_space)(struct sock *);
260 };
261 
262 /*
263  * TCP receive state flags
264  */
265 #define TCP_RCV_LAST_FRAG	(1UL << 0)
266 #define TCP_RCV_COPY_FRAGHDR	(1UL << 1)
267 #define TCP_RCV_COPY_XID	(1UL << 2)
268 #define TCP_RCV_COPY_DATA	(1UL << 3)
269 #define TCP_RCV_READ_CALLDIR	(1UL << 4)
270 #define TCP_RCV_COPY_CALLDIR	(1UL << 5)
271 
272 /*
273  * TCP RPC flags
274  */
275 #define TCP_RPC_REPLY		(1UL << 6)
276 
277 static inline struct sockaddr *xs_addr(struct rpc_xprt *xprt)
278 {
279 	return (struct sockaddr *) &xprt->addr;
280 }
281 
282 static inline struct sockaddr_un *xs_addr_un(struct rpc_xprt *xprt)
283 {
284 	return (struct sockaddr_un *) &xprt->addr;
285 }
286 
287 static inline struct sockaddr_in *xs_addr_in(struct rpc_xprt *xprt)
288 {
289 	return (struct sockaddr_in *) &xprt->addr;
290 }
291 
292 static inline struct sockaddr_in6 *xs_addr_in6(struct rpc_xprt *xprt)
293 {
294 	return (struct sockaddr_in6 *) &xprt->addr;
295 }
296 
297 static void xs_format_common_peer_addresses(struct rpc_xprt *xprt)
298 {
299 	struct sockaddr *sap = xs_addr(xprt);
300 	struct sockaddr_in6 *sin6;
301 	struct sockaddr_in *sin;
302 	struct sockaddr_un *sun;
303 	char buf[128];
304 
305 	switch (sap->sa_family) {
306 	case AF_LOCAL:
307 		sun = xs_addr_un(xprt);
308 		strlcpy(buf, sun->sun_path, sizeof(buf));
309 		xprt->address_strings[RPC_DISPLAY_ADDR] =
310 						kstrdup(buf, GFP_KERNEL);
311 		break;
312 	case AF_INET:
313 		(void)rpc_ntop(sap, buf, sizeof(buf));
314 		xprt->address_strings[RPC_DISPLAY_ADDR] =
315 						kstrdup(buf, GFP_KERNEL);
316 		sin = xs_addr_in(xprt);
317 		snprintf(buf, sizeof(buf), "%08x", ntohl(sin->sin_addr.s_addr));
318 		break;
319 	case AF_INET6:
320 		(void)rpc_ntop(sap, buf, sizeof(buf));
321 		xprt->address_strings[RPC_DISPLAY_ADDR] =
322 						kstrdup(buf, GFP_KERNEL);
323 		sin6 = xs_addr_in6(xprt);
324 		snprintf(buf, sizeof(buf), "%pi6", &sin6->sin6_addr);
325 		break;
326 	default:
327 		BUG();
328 	}
329 
330 	xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = kstrdup(buf, GFP_KERNEL);
331 }
332 
333 static void xs_format_common_peer_ports(struct rpc_xprt *xprt)
334 {
335 	struct sockaddr *sap = xs_addr(xprt);
336 	char buf[128];
337 
338 	snprintf(buf, sizeof(buf), "%u", rpc_get_port(sap));
339 	xprt->address_strings[RPC_DISPLAY_PORT] = kstrdup(buf, GFP_KERNEL);
340 
341 	snprintf(buf, sizeof(buf), "%4hx", rpc_get_port(sap));
342 	xprt->address_strings[RPC_DISPLAY_HEX_PORT] = kstrdup(buf, GFP_KERNEL);
343 }
344 
345 static void xs_format_peer_addresses(struct rpc_xprt *xprt,
346 				     const char *protocol,
347 				     const char *netid)
348 {
349 	xprt->address_strings[RPC_DISPLAY_PROTO] = protocol;
350 	xprt->address_strings[RPC_DISPLAY_NETID] = netid;
351 	xs_format_common_peer_addresses(xprt);
352 	xs_format_common_peer_ports(xprt);
353 }
354 
355 static void xs_update_peer_port(struct rpc_xprt *xprt)
356 {
357 	kfree(xprt->address_strings[RPC_DISPLAY_HEX_PORT]);
358 	kfree(xprt->address_strings[RPC_DISPLAY_PORT]);
359 
360 	xs_format_common_peer_ports(xprt);
361 }
362 
363 static void xs_free_peer_addresses(struct rpc_xprt *xprt)
364 {
365 	unsigned int i;
366 
367 	for (i = 0; i < RPC_DISPLAY_MAX; i++)
368 		switch (i) {
369 		case RPC_DISPLAY_PROTO:
370 		case RPC_DISPLAY_NETID:
371 			continue;
372 		default:
373 			kfree(xprt->address_strings[i]);
374 		}
375 }
376 
377 #define XS_SENDMSG_FLAGS	(MSG_DONTWAIT | MSG_NOSIGNAL)
378 
379 static int xs_send_kvec(struct socket *sock, struct sockaddr *addr, int addrlen, struct kvec *vec, unsigned int base, int more)
380 {
381 	struct msghdr msg = {
382 		.msg_name	= addr,
383 		.msg_namelen	= addrlen,
384 		.msg_flags	= XS_SENDMSG_FLAGS | (more ? MSG_MORE : 0),
385 	};
386 	struct kvec iov = {
387 		.iov_base	= vec->iov_base + base,
388 		.iov_len	= vec->iov_len - base,
389 	};
390 
391 	if (iov.iov_len != 0)
392 		return kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len);
393 	return kernel_sendmsg(sock, &msg, NULL, 0, 0);
394 }
395 
396 static int xs_send_pagedata(struct socket *sock, struct xdr_buf *xdr, unsigned int base, int more)
397 {
398 	struct page **ppage;
399 	unsigned int remainder;
400 	int err, sent = 0;
401 
402 	remainder = xdr->page_len - base;
403 	base += xdr->page_base;
404 	ppage = xdr->pages + (base >> PAGE_SHIFT);
405 	base &= ~PAGE_MASK;
406 	for(;;) {
407 		unsigned int len = min_t(unsigned int, PAGE_SIZE - base, remainder);
408 		int flags = XS_SENDMSG_FLAGS;
409 
410 		remainder -= len;
411 		if (remainder != 0 || more)
412 			flags |= MSG_MORE;
413 		err = sock->ops->sendpage(sock, *ppage, base, len, flags);
414 		if (remainder == 0 || err != len)
415 			break;
416 		sent += err;
417 		ppage++;
418 		base = 0;
419 	}
420 	if (sent == 0)
421 		return err;
422 	if (err > 0)
423 		sent += err;
424 	return sent;
425 }
426 
427 /**
428  * xs_sendpages - write pages directly to a socket
429  * @sock: socket to send on
430  * @addr: UDP only -- address of destination
431  * @addrlen: UDP only -- length of destination address
432  * @xdr: buffer containing this request
433  * @base: starting position in the buffer
434  *
435  */
436 static int xs_sendpages(struct socket *sock, struct sockaddr *addr, int addrlen, struct xdr_buf *xdr, unsigned int base)
437 {
438 	unsigned int remainder = xdr->len - base;
439 	int err, sent = 0;
440 
441 	if (unlikely(!sock))
442 		return -ENOTSOCK;
443 
444 	clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags);
445 	if (base != 0) {
446 		addr = NULL;
447 		addrlen = 0;
448 	}
449 
450 	if (base < xdr->head[0].iov_len || addr != NULL) {
451 		unsigned int len = xdr->head[0].iov_len - base;
452 		remainder -= len;
453 		err = xs_send_kvec(sock, addr, addrlen, &xdr->head[0], base, remainder != 0);
454 		if (remainder == 0 || err != len)
455 			goto out;
456 		sent += err;
457 		base = 0;
458 	} else
459 		base -= xdr->head[0].iov_len;
460 
461 	if (base < xdr->page_len) {
462 		unsigned int len = xdr->page_len - base;
463 		remainder -= len;
464 		err = xs_send_pagedata(sock, xdr, base, remainder != 0);
465 		if (remainder == 0 || err != len)
466 			goto out;
467 		sent += err;
468 		base = 0;
469 	} else
470 		base -= xdr->page_len;
471 
472 	if (base >= xdr->tail[0].iov_len)
473 		return sent;
474 	err = xs_send_kvec(sock, NULL, 0, &xdr->tail[0], base, 0);
475 out:
476 	if (sent == 0)
477 		return err;
478 	if (err > 0)
479 		sent += err;
480 	return sent;
481 }
482 
483 static void xs_nospace_callback(struct rpc_task *task)
484 {
485 	struct sock_xprt *transport = container_of(task->tk_rqstp->rq_xprt, struct sock_xprt, xprt);
486 
487 	transport->inet->sk_write_pending--;
488 	clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
489 }
490 
491 /**
492  * xs_nospace - place task on wait queue if transmit was incomplete
493  * @task: task to put to sleep
494  *
495  */
496 static int xs_nospace(struct rpc_task *task)
497 {
498 	struct rpc_rqst *req = task->tk_rqstp;
499 	struct rpc_xprt *xprt = req->rq_xprt;
500 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
501 	int ret = -EAGAIN;
502 
503 	dprintk("RPC: %5u xmit incomplete (%u left of %u)\n",
504 			task->tk_pid, req->rq_slen - req->rq_bytes_sent,
505 			req->rq_slen);
506 
507 	/* Protect against races with write_space */
508 	spin_lock_bh(&xprt->transport_lock);
509 
510 	/* Don't race with disconnect */
511 	if (xprt_connected(xprt)) {
512 		if (test_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags)) {
513 			/*
514 			 * Notify TCP that we're limited by the application
515 			 * window size
516 			 */
517 			set_bit(SOCK_NOSPACE, &transport->sock->flags);
518 			transport->inet->sk_write_pending++;
519 			/* ...and wait for more buffer space */
520 			xprt_wait_for_buffer_space(task, xs_nospace_callback);
521 		}
522 	} else {
523 		clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
524 		ret = -ENOTCONN;
525 	}
526 
527 	spin_unlock_bh(&xprt->transport_lock);
528 	return ret;
529 }
530 
531 /*
532  * Construct a stream transport record marker in @buf.
533  */
534 static inline void xs_encode_stream_record_marker(struct xdr_buf *buf)
535 {
536 	u32 reclen = buf->len - sizeof(rpc_fraghdr);
537 	rpc_fraghdr *base = buf->head[0].iov_base;
538 	*base = cpu_to_be32(RPC_LAST_STREAM_FRAGMENT | reclen);
539 }
540 
541 /**
542  * xs_local_send_request - write an RPC request to an AF_LOCAL socket
543  * @task: RPC task that manages the state of an RPC request
544  *
545  * Return values:
546  *        0:	The request has been sent
547  *   EAGAIN:	The socket was blocked, please call again later to
548  *		complete the request
549  * ENOTCONN:	Caller needs to invoke connect logic then call again
550  *    other:	Some other error occured, the request was not sent
551  */
552 static int xs_local_send_request(struct rpc_task *task)
553 {
554 	struct rpc_rqst *req = task->tk_rqstp;
555 	struct rpc_xprt *xprt = req->rq_xprt;
556 	struct sock_xprt *transport =
557 				container_of(xprt, struct sock_xprt, xprt);
558 	struct xdr_buf *xdr = &req->rq_snd_buf;
559 	int status;
560 
561 	xs_encode_stream_record_marker(&req->rq_snd_buf);
562 
563 	xs_pktdump("packet data:",
564 			req->rq_svec->iov_base, req->rq_svec->iov_len);
565 
566 	status = xs_sendpages(transport->sock, NULL, 0,
567 						xdr, req->rq_bytes_sent);
568 	dprintk("RPC:       %s(%u) = %d\n",
569 			__func__, xdr->len - req->rq_bytes_sent, status);
570 	if (likely(status >= 0)) {
571 		req->rq_bytes_sent += status;
572 		req->rq_xmit_bytes_sent += status;
573 		if (likely(req->rq_bytes_sent >= req->rq_slen)) {
574 			req->rq_bytes_sent = 0;
575 			return 0;
576 		}
577 		status = -EAGAIN;
578 	}
579 
580 	switch (status) {
581 	case -EAGAIN:
582 		status = xs_nospace(task);
583 		break;
584 	default:
585 		dprintk("RPC:       sendmsg returned unrecognized error %d\n",
586 			-status);
587 	case -EPIPE:
588 		xs_close(xprt);
589 		status = -ENOTCONN;
590 	}
591 
592 	return status;
593 }
594 
595 /**
596  * xs_udp_send_request - write an RPC request to a UDP socket
597  * @task: address of RPC task that manages the state of an RPC request
598  *
599  * Return values:
600  *        0:	The request has been sent
601  *   EAGAIN:	The socket was blocked, please call again later to
602  *		complete the request
603  * ENOTCONN:	Caller needs to invoke connect logic then call again
604  *    other:	Some other error occurred, the request was not sent
605  */
606 static int xs_udp_send_request(struct rpc_task *task)
607 {
608 	struct rpc_rqst *req = task->tk_rqstp;
609 	struct rpc_xprt *xprt = req->rq_xprt;
610 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
611 	struct xdr_buf *xdr = &req->rq_snd_buf;
612 	int status;
613 
614 	xs_pktdump("packet data:",
615 				req->rq_svec->iov_base,
616 				req->rq_svec->iov_len);
617 
618 	if (!xprt_bound(xprt))
619 		return -ENOTCONN;
620 	status = xs_sendpages(transport->sock,
621 			      xs_addr(xprt),
622 			      xprt->addrlen, xdr,
623 			      req->rq_bytes_sent);
624 
625 	dprintk("RPC:       xs_udp_send_request(%u) = %d\n",
626 			xdr->len - req->rq_bytes_sent, status);
627 
628 	if (status >= 0) {
629 		req->rq_xmit_bytes_sent += status;
630 		if (status >= req->rq_slen)
631 			return 0;
632 		/* Still some bytes left; set up for a retry later. */
633 		status = -EAGAIN;
634 	}
635 
636 	switch (status) {
637 	case -ENOTSOCK:
638 		status = -ENOTCONN;
639 		/* Should we call xs_close() here? */
640 		break;
641 	case -EAGAIN:
642 		status = xs_nospace(task);
643 		break;
644 	default:
645 		dprintk("RPC:       sendmsg returned unrecognized error %d\n",
646 			-status);
647 	case -ENETUNREACH:
648 	case -EPIPE:
649 	case -ECONNREFUSED:
650 		/* When the server has died, an ICMP port unreachable message
651 		 * prompts ECONNREFUSED. */
652 		clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
653 	}
654 
655 	return status;
656 }
657 
658 /**
659  * xs_tcp_shutdown - gracefully shut down a TCP socket
660  * @xprt: transport
661  *
662  * Initiates a graceful shutdown of the TCP socket by calling the
663  * equivalent of shutdown(SHUT_WR);
664  */
665 static void xs_tcp_shutdown(struct rpc_xprt *xprt)
666 {
667 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
668 	struct socket *sock = transport->sock;
669 
670 	if (sock != NULL) {
671 		kernel_sock_shutdown(sock, SHUT_WR);
672 		trace_rpc_socket_shutdown(xprt, sock);
673 	}
674 }
675 
676 /**
677  * xs_tcp_send_request - write an RPC request to a TCP socket
678  * @task: address of RPC task that manages the state of an RPC request
679  *
680  * Return values:
681  *        0:	The request has been sent
682  *   EAGAIN:	The socket was blocked, please call again later to
683  *		complete the request
684  * ENOTCONN:	Caller needs to invoke connect logic then call again
685  *    other:	Some other error occurred, the request was not sent
686  *
687  * XXX: In the case of soft timeouts, should we eventually give up
688  *	if sendmsg is not able to make progress?
689  */
690 static int xs_tcp_send_request(struct rpc_task *task)
691 {
692 	struct rpc_rqst *req = task->tk_rqstp;
693 	struct rpc_xprt *xprt = req->rq_xprt;
694 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
695 	struct xdr_buf *xdr = &req->rq_snd_buf;
696 	int status;
697 
698 	xs_encode_stream_record_marker(&req->rq_snd_buf);
699 
700 	xs_pktdump("packet data:",
701 				req->rq_svec->iov_base,
702 				req->rq_svec->iov_len);
703 
704 	/* Continue transmitting the packet/record. We must be careful
705 	 * to cope with writespace callbacks arriving _after_ we have
706 	 * called sendmsg(). */
707 	while (1) {
708 		status = xs_sendpages(transport->sock,
709 					NULL, 0, xdr, req->rq_bytes_sent);
710 
711 		dprintk("RPC:       xs_tcp_send_request(%u) = %d\n",
712 				xdr->len - req->rq_bytes_sent, status);
713 
714 		if (unlikely(status < 0))
715 			break;
716 
717 		/* If we've sent the entire packet, immediately
718 		 * reset the count of bytes sent. */
719 		req->rq_bytes_sent += status;
720 		req->rq_xmit_bytes_sent += status;
721 		if (likely(req->rq_bytes_sent >= req->rq_slen)) {
722 			req->rq_bytes_sent = 0;
723 			return 0;
724 		}
725 
726 		if (status != 0)
727 			continue;
728 		status = -EAGAIN;
729 		break;
730 	}
731 
732 	switch (status) {
733 	case -ENOTSOCK:
734 		status = -ENOTCONN;
735 		/* Should we call xs_close() here? */
736 		break;
737 	case -EAGAIN:
738 		status = xs_nospace(task);
739 		break;
740 	default:
741 		dprintk("RPC:       sendmsg returned unrecognized error %d\n",
742 			-status);
743 	case -ECONNRESET:
744 		xs_tcp_shutdown(xprt);
745 	case -ECONNREFUSED:
746 	case -ENOTCONN:
747 	case -EPIPE:
748 		clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
749 	}
750 
751 	return status;
752 }
753 
754 /**
755  * xs_tcp_release_xprt - clean up after a tcp transmission
756  * @xprt: transport
757  * @task: rpc task
758  *
759  * This cleans up if an error causes us to abort the transmission of a request.
760  * In this case, the socket may need to be reset in order to avoid confusing
761  * the server.
762  */
763 static void xs_tcp_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
764 {
765 	struct rpc_rqst *req;
766 
767 	if (task != xprt->snd_task)
768 		return;
769 	if (task == NULL)
770 		goto out_release;
771 	req = task->tk_rqstp;
772 	if (req == NULL)
773 		goto out_release;
774 	if (req->rq_bytes_sent == 0)
775 		goto out_release;
776 	if (req->rq_bytes_sent == req->rq_snd_buf.len)
777 		goto out_release;
778 	set_bit(XPRT_CLOSE_WAIT, &xprt->state);
779 out_release:
780 	xprt_release_xprt(xprt, task);
781 }
782 
783 static void xs_save_old_callbacks(struct sock_xprt *transport, struct sock *sk)
784 {
785 	transport->old_data_ready = sk->sk_data_ready;
786 	transport->old_state_change = sk->sk_state_change;
787 	transport->old_write_space = sk->sk_write_space;
788 }
789 
790 static void xs_restore_old_callbacks(struct sock_xprt *transport, struct sock *sk)
791 {
792 	sk->sk_data_ready = transport->old_data_ready;
793 	sk->sk_state_change = transport->old_state_change;
794 	sk->sk_write_space = transport->old_write_space;
795 }
796 
797 static void xs_reset_transport(struct sock_xprt *transport)
798 {
799 	struct socket *sock = transport->sock;
800 	struct sock *sk = transport->inet;
801 
802 	if (sk == NULL)
803 		return;
804 
805 	transport->srcport = 0;
806 
807 	write_lock_bh(&sk->sk_callback_lock);
808 	transport->inet = NULL;
809 	transport->sock = NULL;
810 
811 	sk->sk_user_data = NULL;
812 
813 	xs_restore_old_callbacks(transport, sk);
814 	write_unlock_bh(&sk->sk_callback_lock);
815 
816 	sk->sk_no_check = 0;
817 
818 	trace_rpc_socket_close(&transport->xprt, sock);
819 	sock_release(sock);
820 }
821 
822 /**
823  * xs_close - close a socket
824  * @xprt: transport
825  *
826  * This is used when all requests are complete; ie, no DRC state remains
827  * on the server we want to save.
828  *
829  * The caller _must_ be holding XPRT_LOCKED in order to avoid issues with
830  * xs_reset_transport() zeroing the socket from underneath a writer.
831  */
832 static void xs_close(struct rpc_xprt *xprt)
833 {
834 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
835 
836 	dprintk("RPC:       xs_close xprt %p\n", xprt);
837 
838 	xs_reset_transport(transport);
839 	xprt->reestablish_timeout = 0;
840 
841 	smp_mb__before_clear_bit();
842 	clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
843 	clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
844 	clear_bit(XPRT_CLOSING, &xprt->state);
845 	smp_mb__after_clear_bit();
846 	xprt_disconnect_done(xprt);
847 }
848 
849 static void xs_tcp_close(struct rpc_xprt *xprt)
850 {
851 	if (test_and_clear_bit(XPRT_CONNECTION_CLOSE, &xprt->state))
852 		xs_close(xprt);
853 	else
854 		xs_tcp_shutdown(xprt);
855 }
856 
857 static void xs_local_destroy(struct rpc_xprt *xprt)
858 {
859 	xs_close(xprt);
860 	xs_free_peer_addresses(xprt);
861 	xprt_free(xprt);
862 	module_put(THIS_MODULE);
863 }
864 
865 /**
866  * xs_destroy - prepare to shutdown a transport
867  * @xprt: doomed transport
868  *
869  */
870 static void xs_destroy(struct rpc_xprt *xprt)
871 {
872 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
873 
874 	dprintk("RPC:       xs_destroy xprt %p\n", xprt);
875 
876 	cancel_delayed_work_sync(&transport->connect_worker);
877 
878 	xs_local_destroy(xprt);
879 }
880 
881 static inline struct rpc_xprt *xprt_from_sock(struct sock *sk)
882 {
883 	return (struct rpc_xprt *) sk->sk_user_data;
884 }
885 
886 static int xs_local_copy_to_xdr(struct xdr_buf *xdr, struct sk_buff *skb)
887 {
888 	struct xdr_skb_reader desc = {
889 		.skb		= skb,
890 		.offset		= sizeof(rpc_fraghdr),
891 		.count		= skb->len - sizeof(rpc_fraghdr),
892 	};
893 
894 	if (xdr_partial_copy_from_skb(xdr, 0, &desc, xdr_skb_read_bits) < 0)
895 		return -1;
896 	if (desc.count)
897 		return -1;
898 	return 0;
899 }
900 
901 /**
902  * xs_local_data_ready - "data ready" callback for AF_LOCAL sockets
903  * @sk: socket with data to read
904  * @len: how much data to read
905  *
906  * Currently this assumes we can read the whole reply in a single gulp.
907  */
908 static void xs_local_data_ready(struct sock *sk, int len)
909 {
910 	struct rpc_task *task;
911 	struct rpc_xprt *xprt;
912 	struct rpc_rqst *rovr;
913 	struct sk_buff *skb;
914 	int err, repsize, copied;
915 	u32 _xid;
916 	__be32 *xp;
917 
918 	read_lock_bh(&sk->sk_callback_lock);
919 	dprintk("RPC:       %s...\n", __func__);
920 	xprt = xprt_from_sock(sk);
921 	if (xprt == NULL)
922 		goto out;
923 
924 	skb = skb_recv_datagram(sk, 0, 1, &err);
925 	if (skb == NULL)
926 		goto out;
927 
928 	repsize = skb->len - sizeof(rpc_fraghdr);
929 	if (repsize < 4) {
930 		dprintk("RPC:       impossible RPC reply size %d\n", repsize);
931 		goto dropit;
932 	}
933 
934 	/* Copy the XID from the skb... */
935 	xp = skb_header_pointer(skb, sizeof(rpc_fraghdr), sizeof(_xid), &_xid);
936 	if (xp == NULL)
937 		goto dropit;
938 
939 	/* Look up and lock the request corresponding to the given XID */
940 	spin_lock(&xprt->transport_lock);
941 	rovr = xprt_lookup_rqst(xprt, *xp);
942 	if (!rovr)
943 		goto out_unlock;
944 	task = rovr->rq_task;
945 
946 	copied = rovr->rq_private_buf.buflen;
947 	if (copied > repsize)
948 		copied = repsize;
949 
950 	if (xs_local_copy_to_xdr(&rovr->rq_private_buf, skb)) {
951 		dprintk("RPC:       sk_buff copy failed\n");
952 		goto out_unlock;
953 	}
954 
955 	xprt_complete_rqst(task, copied);
956 
957  out_unlock:
958 	spin_unlock(&xprt->transport_lock);
959  dropit:
960 	skb_free_datagram(sk, skb);
961  out:
962 	read_unlock_bh(&sk->sk_callback_lock);
963 }
964 
965 /**
966  * xs_udp_data_ready - "data ready" callback for UDP sockets
967  * @sk: socket with data to read
968  * @len: how much data to read
969  *
970  */
971 static void xs_udp_data_ready(struct sock *sk, int len)
972 {
973 	struct rpc_task *task;
974 	struct rpc_xprt *xprt;
975 	struct rpc_rqst *rovr;
976 	struct sk_buff *skb;
977 	int err, repsize, copied;
978 	u32 _xid;
979 	__be32 *xp;
980 
981 	read_lock_bh(&sk->sk_callback_lock);
982 	dprintk("RPC:       xs_udp_data_ready...\n");
983 	if (!(xprt = xprt_from_sock(sk)))
984 		goto out;
985 
986 	if ((skb = skb_recv_datagram(sk, 0, 1, &err)) == NULL)
987 		goto out;
988 
989 	repsize = skb->len - sizeof(struct udphdr);
990 	if (repsize < 4) {
991 		dprintk("RPC:       impossible RPC reply size %d!\n", repsize);
992 		goto dropit;
993 	}
994 
995 	/* Copy the XID from the skb... */
996 	xp = skb_header_pointer(skb, sizeof(struct udphdr),
997 				sizeof(_xid), &_xid);
998 	if (xp == NULL)
999 		goto dropit;
1000 
1001 	/* Look up and lock the request corresponding to the given XID */
1002 	spin_lock(&xprt->transport_lock);
1003 	rovr = xprt_lookup_rqst(xprt, *xp);
1004 	if (!rovr)
1005 		goto out_unlock;
1006 	task = rovr->rq_task;
1007 
1008 	if ((copied = rovr->rq_private_buf.buflen) > repsize)
1009 		copied = repsize;
1010 
1011 	/* Suck it into the iovec, verify checksum if not done by hw. */
1012 	if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) {
1013 		UDPX_INC_STATS_BH(sk, UDP_MIB_INERRORS);
1014 		goto out_unlock;
1015 	}
1016 
1017 	UDPX_INC_STATS_BH(sk, UDP_MIB_INDATAGRAMS);
1018 
1019 	xprt_adjust_cwnd(xprt, task, copied);
1020 	xprt_complete_rqst(task, copied);
1021 
1022  out_unlock:
1023 	spin_unlock(&xprt->transport_lock);
1024  dropit:
1025 	skb_free_datagram(sk, skb);
1026  out:
1027 	read_unlock_bh(&sk->sk_callback_lock);
1028 }
1029 
1030 /*
1031  * Helper function to force a TCP close if the server is sending
1032  * junk and/or it has put us in CLOSE_WAIT
1033  */
1034 static void xs_tcp_force_close(struct rpc_xprt *xprt)
1035 {
1036 	set_bit(XPRT_CONNECTION_CLOSE, &xprt->state);
1037 	xprt_force_disconnect(xprt);
1038 }
1039 
1040 static inline void xs_tcp_read_fraghdr(struct rpc_xprt *xprt, struct xdr_skb_reader *desc)
1041 {
1042 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1043 	size_t len, used;
1044 	char *p;
1045 
1046 	p = ((char *) &transport->tcp_fraghdr) + transport->tcp_offset;
1047 	len = sizeof(transport->tcp_fraghdr) - transport->tcp_offset;
1048 	used = xdr_skb_read_bits(desc, p, len);
1049 	transport->tcp_offset += used;
1050 	if (used != len)
1051 		return;
1052 
1053 	transport->tcp_reclen = ntohl(transport->tcp_fraghdr);
1054 	if (transport->tcp_reclen & RPC_LAST_STREAM_FRAGMENT)
1055 		transport->tcp_flags |= TCP_RCV_LAST_FRAG;
1056 	else
1057 		transport->tcp_flags &= ~TCP_RCV_LAST_FRAG;
1058 	transport->tcp_reclen &= RPC_FRAGMENT_SIZE_MASK;
1059 
1060 	transport->tcp_flags &= ~TCP_RCV_COPY_FRAGHDR;
1061 	transport->tcp_offset = 0;
1062 
1063 	/* Sanity check of the record length */
1064 	if (unlikely(transport->tcp_reclen < 8)) {
1065 		dprintk("RPC:       invalid TCP record fragment length\n");
1066 		xs_tcp_force_close(xprt);
1067 		return;
1068 	}
1069 	dprintk("RPC:       reading TCP record fragment of length %d\n",
1070 			transport->tcp_reclen);
1071 }
1072 
1073 static void xs_tcp_check_fraghdr(struct sock_xprt *transport)
1074 {
1075 	if (transport->tcp_offset == transport->tcp_reclen) {
1076 		transport->tcp_flags |= TCP_RCV_COPY_FRAGHDR;
1077 		transport->tcp_offset = 0;
1078 		if (transport->tcp_flags & TCP_RCV_LAST_FRAG) {
1079 			transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1080 			transport->tcp_flags |= TCP_RCV_COPY_XID;
1081 			transport->tcp_copied = 0;
1082 		}
1083 	}
1084 }
1085 
1086 static inline void xs_tcp_read_xid(struct sock_xprt *transport, struct xdr_skb_reader *desc)
1087 {
1088 	size_t len, used;
1089 	char *p;
1090 
1091 	len = sizeof(transport->tcp_xid) - transport->tcp_offset;
1092 	dprintk("RPC:       reading XID (%Zu bytes)\n", len);
1093 	p = ((char *) &transport->tcp_xid) + transport->tcp_offset;
1094 	used = xdr_skb_read_bits(desc, p, len);
1095 	transport->tcp_offset += used;
1096 	if (used != len)
1097 		return;
1098 	transport->tcp_flags &= ~TCP_RCV_COPY_XID;
1099 	transport->tcp_flags |= TCP_RCV_READ_CALLDIR;
1100 	transport->tcp_copied = 4;
1101 	dprintk("RPC:       reading %s XID %08x\n",
1102 			(transport->tcp_flags & TCP_RPC_REPLY) ? "reply for"
1103 							      : "request with",
1104 			ntohl(transport->tcp_xid));
1105 	xs_tcp_check_fraghdr(transport);
1106 }
1107 
1108 static inline void xs_tcp_read_calldir(struct sock_xprt *transport,
1109 				       struct xdr_skb_reader *desc)
1110 {
1111 	size_t len, used;
1112 	u32 offset;
1113 	char *p;
1114 
1115 	/*
1116 	 * We want transport->tcp_offset to be 8 at the end of this routine
1117 	 * (4 bytes for the xid and 4 bytes for the call/reply flag).
1118 	 * When this function is called for the first time,
1119 	 * transport->tcp_offset is 4 (after having already read the xid).
1120 	 */
1121 	offset = transport->tcp_offset - sizeof(transport->tcp_xid);
1122 	len = sizeof(transport->tcp_calldir) - offset;
1123 	dprintk("RPC:       reading CALL/REPLY flag (%Zu bytes)\n", len);
1124 	p = ((char *) &transport->tcp_calldir) + offset;
1125 	used = xdr_skb_read_bits(desc, p, len);
1126 	transport->tcp_offset += used;
1127 	if (used != len)
1128 		return;
1129 	transport->tcp_flags &= ~TCP_RCV_READ_CALLDIR;
1130 	/*
1131 	 * We don't yet have the XDR buffer, so we will write the calldir
1132 	 * out after we get the buffer from the 'struct rpc_rqst'
1133 	 */
1134 	switch (ntohl(transport->tcp_calldir)) {
1135 	case RPC_REPLY:
1136 		transport->tcp_flags |= TCP_RCV_COPY_CALLDIR;
1137 		transport->tcp_flags |= TCP_RCV_COPY_DATA;
1138 		transport->tcp_flags |= TCP_RPC_REPLY;
1139 		break;
1140 	case RPC_CALL:
1141 		transport->tcp_flags |= TCP_RCV_COPY_CALLDIR;
1142 		transport->tcp_flags |= TCP_RCV_COPY_DATA;
1143 		transport->tcp_flags &= ~TCP_RPC_REPLY;
1144 		break;
1145 	default:
1146 		dprintk("RPC:       invalid request message type\n");
1147 		xs_tcp_force_close(&transport->xprt);
1148 	}
1149 	xs_tcp_check_fraghdr(transport);
1150 }
1151 
1152 static inline void xs_tcp_read_common(struct rpc_xprt *xprt,
1153 				     struct xdr_skb_reader *desc,
1154 				     struct rpc_rqst *req)
1155 {
1156 	struct sock_xprt *transport =
1157 				container_of(xprt, struct sock_xprt, xprt);
1158 	struct xdr_buf *rcvbuf;
1159 	size_t len;
1160 	ssize_t r;
1161 
1162 	rcvbuf = &req->rq_private_buf;
1163 
1164 	if (transport->tcp_flags & TCP_RCV_COPY_CALLDIR) {
1165 		/*
1166 		 * Save the RPC direction in the XDR buffer
1167 		 */
1168 		memcpy(rcvbuf->head[0].iov_base + transport->tcp_copied,
1169 			&transport->tcp_calldir,
1170 			sizeof(transport->tcp_calldir));
1171 		transport->tcp_copied += sizeof(transport->tcp_calldir);
1172 		transport->tcp_flags &= ~TCP_RCV_COPY_CALLDIR;
1173 	}
1174 
1175 	len = desc->count;
1176 	if (len > transport->tcp_reclen - transport->tcp_offset) {
1177 		struct xdr_skb_reader my_desc;
1178 
1179 		len = transport->tcp_reclen - transport->tcp_offset;
1180 		memcpy(&my_desc, desc, sizeof(my_desc));
1181 		my_desc.count = len;
1182 		r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
1183 					  &my_desc, xdr_skb_read_bits);
1184 		desc->count -= r;
1185 		desc->offset += r;
1186 	} else
1187 		r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
1188 					  desc, xdr_skb_read_bits);
1189 
1190 	if (r > 0) {
1191 		transport->tcp_copied += r;
1192 		transport->tcp_offset += r;
1193 	}
1194 	if (r != len) {
1195 		/* Error when copying to the receive buffer,
1196 		 * usually because we weren't able to allocate
1197 		 * additional buffer pages. All we can do now
1198 		 * is turn off TCP_RCV_COPY_DATA, so the request
1199 		 * will not receive any additional updates,
1200 		 * and time out.
1201 		 * Any remaining data from this record will
1202 		 * be discarded.
1203 		 */
1204 		transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1205 		dprintk("RPC:       XID %08x truncated request\n",
1206 				ntohl(transport->tcp_xid));
1207 		dprintk("RPC:       xprt = %p, tcp_copied = %lu, "
1208 				"tcp_offset = %u, tcp_reclen = %u\n",
1209 				xprt, transport->tcp_copied,
1210 				transport->tcp_offset, transport->tcp_reclen);
1211 		return;
1212 	}
1213 
1214 	dprintk("RPC:       XID %08x read %Zd bytes\n",
1215 			ntohl(transport->tcp_xid), r);
1216 	dprintk("RPC:       xprt = %p, tcp_copied = %lu, tcp_offset = %u, "
1217 			"tcp_reclen = %u\n", xprt, transport->tcp_copied,
1218 			transport->tcp_offset, transport->tcp_reclen);
1219 
1220 	if (transport->tcp_copied == req->rq_private_buf.buflen)
1221 		transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1222 	else if (transport->tcp_offset == transport->tcp_reclen) {
1223 		if (transport->tcp_flags & TCP_RCV_LAST_FRAG)
1224 			transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1225 	}
1226 }
1227 
1228 /*
1229  * Finds the request corresponding to the RPC xid and invokes the common
1230  * tcp read code to read the data.
1231  */
1232 static inline int xs_tcp_read_reply(struct rpc_xprt *xprt,
1233 				    struct xdr_skb_reader *desc)
1234 {
1235 	struct sock_xprt *transport =
1236 				container_of(xprt, struct sock_xprt, xprt);
1237 	struct rpc_rqst *req;
1238 
1239 	dprintk("RPC:       read reply XID %08x\n", ntohl(transport->tcp_xid));
1240 
1241 	/* Find and lock the request corresponding to this xid */
1242 	spin_lock(&xprt->transport_lock);
1243 	req = xprt_lookup_rqst(xprt, transport->tcp_xid);
1244 	if (!req) {
1245 		dprintk("RPC:       XID %08x request not found!\n",
1246 				ntohl(transport->tcp_xid));
1247 		spin_unlock(&xprt->transport_lock);
1248 		return -1;
1249 	}
1250 
1251 	xs_tcp_read_common(xprt, desc, req);
1252 
1253 	if (!(transport->tcp_flags & TCP_RCV_COPY_DATA))
1254 		xprt_complete_rqst(req->rq_task, transport->tcp_copied);
1255 
1256 	spin_unlock(&xprt->transport_lock);
1257 	return 0;
1258 }
1259 
1260 #if defined(CONFIG_SUNRPC_BACKCHANNEL)
1261 /*
1262  * Obtains an rpc_rqst previously allocated and invokes the common
1263  * tcp read code to read the data.  The result is placed in the callback
1264  * queue.
1265  * If we're unable to obtain the rpc_rqst we schedule the closing of the
1266  * connection and return -1.
1267  */
1268 static inline int xs_tcp_read_callback(struct rpc_xprt *xprt,
1269 				       struct xdr_skb_reader *desc)
1270 {
1271 	struct sock_xprt *transport =
1272 				container_of(xprt, struct sock_xprt, xprt);
1273 	struct rpc_rqst *req;
1274 
1275 	req = xprt_alloc_bc_request(xprt);
1276 	if (req == NULL) {
1277 		printk(KERN_WARNING "Callback slot table overflowed\n");
1278 		xprt_force_disconnect(xprt);
1279 		return -1;
1280 	}
1281 
1282 	req->rq_xid = transport->tcp_xid;
1283 	dprintk("RPC:       read callback  XID %08x\n", ntohl(req->rq_xid));
1284 	xs_tcp_read_common(xprt, desc, req);
1285 
1286 	if (!(transport->tcp_flags & TCP_RCV_COPY_DATA)) {
1287 		struct svc_serv *bc_serv = xprt->bc_serv;
1288 
1289 		/*
1290 		 * Add callback request to callback list.  The callback
1291 		 * service sleeps on the sv_cb_waitq waiting for new
1292 		 * requests.  Wake it up after adding enqueing the
1293 		 * request.
1294 		 */
1295 		dprintk("RPC:       add callback request to list\n");
1296 		spin_lock(&bc_serv->sv_cb_lock);
1297 		list_add(&req->rq_bc_list, &bc_serv->sv_cb_list);
1298 		spin_unlock(&bc_serv->sv_cb_lock);
1299 		wake_up(&bc_serv->sv_cb_waitq);
1300 	}
1301 
1302 	req->rq_private_buf.len = transport->tcp_copied;
1303 
1304 	return 0;
1305 }
1306 
1307 static inline int _xs_tcp_read_data(struct rpc_xprt *xprt,
1308 					struct xdr_skb_reader *desc)
1309 {
1310 	struct sock_xprt *transport =
1311 				container_of(xprt, struct sock_xprt, xprt);
1312 
1313 	return (transport->tcp_flags & TCP_RPC_REPLY) ?
1314 		xs_tcp_read_reply(xprt, desc) :
1315 		xs_tcp_read_callback(xprt, desc);
1316 }
1317 #else
1318 static inline int _xs_tcp_read_data(struct rpc_xprt *xprt,
1319 					struct xdr_skb_reader *desc)
1320 {
1321 	return xs_tcp_read_reply(xprt, desc);
1322 }
1323 #endif /* CONFIG_SUNRPC_BACKCHANNEL */
1324 
1325 /*
1326  * Read data off the transport.  This can be either an RPC_CALL or an
1327  * RPC_REPLY.  Relay the processing to helper functions.
1328  */
1329 static void xs_tcp_read_data(struct rpc_xprt *xprt,
1330 				    struct xdr_skb_reader *desc)
1331 {
1332 	struct sock_xprt *transport =
1333 				container_of(xprt, struct sock_xprt, xprt);
1334 
1335 	if (_xs_tcp_read_data(xprt, desc) == 0)
1336 		xs_tcp_check_fraghdr(transport);
1337 	else {
1338 		/*
1339 		 * The transport_lock protects the request handling.
1340 		 * There's no need to hold it to update the tcp_flags.
1341 		 */
1342 		transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1343 	}
1344 }
1345 
1346 static inline void xs_tcp_read_discard(struct sock_xprt *transport, struct xdr_skb_reader *desc)
1347 {
1348 	size_t len;
1349 
1350 	len = transport->tcp_reclen - transport->tcp_offset;
1351 	if (len > desc->count)
1352 		len = desc->count;
1353 	desc->count -= len;
1354 	desc->offset += len;
1355 	transport->tcp_offset += len;
1356 	dprintk("RPC:       discarded %Zu bytes\n", len);
1357 	xs_tcp_check_fraghdr(transport);
1358 }
1359 
1360 static int xs_tcp_data_recv(read_descriptor_t *rd_desc, struct sk_buff *skb, unsigned int offset, size_t len)
1361 {
1362 	struct rpc_xprt *xprt = rd_desc->arg.data;
1363 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1364 	struct xdr_skb_reader desc = {
1365 		.skb	= skb,
1366 		.offset	= offset,
1367 		.count	= len,
1368 	};
1369 
1370 	dprintk("RPC:       xs_tcp_data_recv started\n");
1371 	do {
1372 		/* Read in a new fragment marker if necessary */
1373 		/* Can we ever really expect to get completely empty fragments? */
1374 		if (transport->tcp_flags & TCP_RCV_COPY_FRAGHDR) {
1375 			xs_tcp_read_fraghdr(xprt, &desc);
1376 			continue;
1377 		}
1378 		/* Read in the xid if necessary */
1379 		if (transport->tcp_flags & TCP_RCV_COPY_XID) {
1380 			xs_tcp_read_xid(transport, &desc);
1381 			continue;
1382 		}
1383 		/* Read in the call/reply flag */
1384 		if (transport->tcp_flags & TCP_RCV_READ_CALLDIR) {
1385 			xs_tcp_read_calldir(transport, &desc);
1386 			continue;
1387 		}
1388 		/* Read in the request data */
1389 		if (transport->tcp_flags & TCP_RCV_COPY_DATA) {
1390 			xs_tcp_read_data(xprt, &desc);
1391 			continue;
1392 		}
1393 		/* Skip over any trailing bytes on short reads */
1394 		xs_tcp_read_discard(transport, &desc);
1395 	} while (desc.count);
1396 	dprintk("RPC:       xs_tcp_data_recv done\n");
1397 	return len - desc.count;
1398 }
1399 
1400 /**
1401  * xs_tcp_data_ready - "data ready" callback for TCP sockets
1402  * @sk: socket with data to read
1403  * @bytes: how much data to read
1404  *
1405  */
1406 static void xs_tcp_data_ready(struct sock *sk, int bytes)
1407 {
1408 	struct rpc_xprt *xprt;
1409 	read_descriptor_t rd_desc;
1410 	int read;
1411 
1412 	dprintk("RPC:       xs_tcp_data_ready...\n");
1413 
1414 	read_lock_bh(&sk->sk_callback_lock);
1415 	if (!(xprt = xprt_from_sock(sk)))
1416 		goto out;
1417 	/* Any data means we had a useful conversation, so
1418 	 * the we don't need to delay the next reconnect
1419 	 */
1420 	if (xprt->reestablish_timeout)
1421 		xprt->reestablish_timeout = 0;
1422 
1423 	/* We use rd_desc to pass struct xprt to xs_tcp_data_recv */
1424 	rd_desc.arg.data = xprt;
1425 	do {
1426 		rd_desc.count = 65536;
1427 		read = tcp_read_sock(sk, &rd_desc, xs_tcp_data_recv);
1428 	} while (read > 0);
1429 out:
1430 	read_unlock_bh(&sk->sk_callback_lock);
1431 }
1432 
1433 /*
1434  * Do the equivalent of linger/linger2 handling for dealing with
1435  * broken servers that don't close the socket in a timely
1436  * fashion
1437  */
1438 static void xs_tcp_schedule_linger_timeout(struct rpc_xprt *xprt,
1439 		unsigned long timeout)
1440 {
1441 	struct sock_xprt *transport;
1442 
1443 	if (xprt_test_and_set_connecting(xprt))
1444 		return;
1445 	set_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1446 	transport = container_of(xprt, struct sock_xprt, xprt);
1447 	queue_delayed_work(rpciod_workqueue, &transport->connect_worker,
1448 			   timeout);
1449 }
1450 
1451 static void xs_tcp_cancel_linger_timeout(struct rpc_xprt *xprt)
1452 {
1453 	struct sock_xprt *transport;
1454 
1455 	transport = container_of(xprt, struct sock_xprt, xprt);
1456 
1457 	if (!test_bit(XPRT_CONNECTION_ABORT, &xprt->state) ||
1458 	    !cancel_delayed_work(&transport->connect_worker))
1459 		return;
1460 	clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1461 	xprt_clear_connecting(xprt);
1462 }
1463 
1464 static void xs_sock_reset_connection_flags(struct rpc_xprt *xprt)
1465 {
1466 	smp_mb__before_clear_bit();
1467 	clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1468 	clear_bit(XPRT_CONNECTION_CLOSE, &xprt->state);
1469 	clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
1470 	clear_bit(XPRT_CLOSING, &xprt->state);
1471 	smp_mb__after_clear_bit();
1472 }
1473 
1474 static void xs_sock_mark_closed(struct rpc_xprt *xprt)
1475 {
1476 	xs_sock_reset_connection_flags(xprt);
1477 	/* Mark transport as closed and wake up all pending tasks */
1478 	xprt_disconnect_done(xprt);
1479 }
1480 
1481 /**
1482  * xs_tcp_state_change - callback to handle TCP socket state changes
1483  * @sk: socket whose state has changed
1484  *
1485  */
1486 static void xs_tcp_state_change(struct sock *sk)
1487 {
1488 	struct rpc_xprt *xprt;
1489 
1490 	read_lock_bh(&sk->sk_callback_lock);
1491 	if (!(xprt = xprt_from_sock(sk)))
1492 		goto out;
1493 	dprintk("RPC:       xs_tcp_state_change client %p...\n", xprt);
1494 	dprintk("RPC:       state %x conn %d dead %d zapped %d sk_shutdown %d\n",
1495 			sk->sk_state, xprt_connected(xprt),
1496 			sock_flag(sk, SOCK_DEAD),
1497 			sock_flag(sk, SOCK_ZAPPED),
1498 			sk->sk_shutdown);
1499 
1500 	trace_rpc_socket_state_change(xprt, sk->sk_socket);
1501 	switch (sk->sk_state) {
1502 	case TCP_ESTABLISHED:
1503 		spin_lock(&xprt->transport_lock);
1504 		if (!xprt_test_and_set_connected(xprt)) {
1505 			struct sock_xprt *transport = container_of(xprt,
1506 					struct sock_xprt, xprt);
1507 
1508 			/* Reset TCP record info */
1509 			transport->tcp_offset = 0;
1510 			transport->tcp_reclen = 0;
1511 			transport->tcp_copied = 0;
1512 			transport->tcp_flags =
1513 				TCP_RCV_COPY_FRAGHDR | TCP_RCV_COPY_XID;
1514 
1515 			xprt_wake_pending_tasks(xprt, -EAGAIN);
1516 		}
1517 		spin_unlock(&xprt->transport_lock);
1518 		break;
1519 	case TCP_FIN_WAIT1:
1520 		/* The client initiated a shutdown of the socket */
1521 		xprt->connect_cookie++;
1522 		xprt->reestablish_timeout = 0;
1523 		set_bit(XPRT_CLOSING, &xprt->state);
1524 		smp_mb__before_clear_bit();
1525 		clear_bit(XPRT_CONNECTED, &xprt->state);
1526 		clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
1527 		smp_mb__after_clear_bit();
1528 		xs_tcp_schedule_linger_timeout(xprt, xs_tcp_fin_timeout);
1529 		break;
1530 	case TCP_CLOSE_WAIT:
1531 		/* The server initiated a shutdown of the socket */
1532 		xprt->connect_cookie++;
1533 		clear_bit(XPRT_CONNECTED, &xprt->state);
1534 		xs_tcp_force_close(xprt);
1535 	case TCP_CLOSING:
1536 		/*
1537 		 * If the server closed down the connection, make sure that
1538 		 * we back off before reconnecting
1539 		 */
1540 		if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
1541 			xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
1542 		break;
1543 	case TCP_LAST_ACK:
1544 		set_bit(XPRT_CLOSING, &xprt->state);
1545 		xs_tcp_schedule_linger_timeout(xprt, xs_tcp_fin_timeout);
1546 		smp_mb__before_clear_bit();
1547 		clear_bit(XPRT_CONNECTED, &xprt->state);
1548 		smp_mb__after_clear_bit();
1549 		break;
1550 	case TCP_CLOSE:
1551 		xs_tcp_cancel_linger_timeout(xprt);
1552 		xs_sock_mark_closed(xprt);
1553 	}
1554  out:
1555 	read_unlock_bh(&sk->sk_callback_lock);
1556 }
1557 
1558 static void xs_write_space(struct sock *sk)
1559 {
1560 	struct socket *sock;
1561 	struct rpc_xprt *xprt;
1562 
1563 	if (unlikely(!(sock = sk->sk_socket)))
1564 		return;
1565 	clear_bit(SOCK_NOSPACE, &sock->flags);
1566 
1567 	if (unlikely(!(xprt = xprt_from_sock(sk))))
1568 		return;
1569 	if (test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags) == 0)
1570 		return;
1571 
1572 	xprt_write_space(xprt);
1573 }
1574 
1575 /**
1576  * xs_udp_write_space - callback invoked when socket buffer space
1577  *                             becomes available
1578  * @sk: socket whose state has changed
1579  *
1580  * Called when more output buffer space is available for this socket.
1581  * We try not to wake our writers until they can make "significant"
1582  * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1583  * with a bunch of small requests.
1584  */
1585 static void xs_udp_write_space(struct sock *sk)
1586 {
1587 	read_lock_bh(&sk->sk_callback_lock);
1588 
1589 	/* from net/core/sock.c:sock_def_write_space */
1590 	if (sock_writeable(sk))
1591 		xs_write_space(sk);
1592 
1593 	read_unlock_bh(&sk->sk_callback_lock);
1594 }
1595 
1596 /**
1597  * xs_tcp_write_space - callback invoked when socket buffer space
1598  *                             becomes available
1599  * @sk: socket whose state has changed
1600  *
1601  * Called when more output buffer space is available for this socket.
1602  * We try not to wake our writers until they can make "significant"
1603  * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1604  * with a bunch of small requests.
1605  */
1606 static void xs_tcp_write_space(struct sock *sk)
1607 {
1608 	read_lock_bh(&sk->sk_callback_lock);
1609 
1610 	/* from net/core/stream.c:sk_stream_write_space */
1611 	if (sk_stream_is_writeable(sk))
1612 		xs_write_space(sk);
1613 
1614 	read_unlock_bh(&sk->sk_callback_lock);
1615 }
1616 
1617 static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt)
1618 {
1619 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1620 	struct sock *sk = transport->inet;
1621 
1622 	if (transport->rcvsize) {
1623 		sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
1624 		sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2;
1625 	}
1626 	if (transport->sndsize) {
1627 		sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
1628 		sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2;
1629 		sk->sk_write_space(sk);
1630 	}
1631 }
1632 
1633 /**
1634  * xs_udp_set_buffer_size - set send and receive limits
1635  * @xprt: generic transport
1636  * @sndsize: requested size of send buffer, in bytes
1637  * @rcvsize: requested size of receive buffer, in bytes
1638  *
1639  * Set socket send and receive buffer size limits.
1640  */
1641 static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize)
1642 {
1643 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1644 
1645 	transport->sndsize = 0;
1646 	if (sndsize)
1647 		transport->sndsize = sndsize + 1024;
1648 	transport->rcvsize = 0;
1649 	if (rcvsize)
1650 		transport->rcvsize = rcvsize + 1024;
1651 
1652 	xs_udp_do_set_buffer_size(xprt);
1653 }
1654 
1655 /**
1656  * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
1657  * @task: task that timed out
1658  *
1659  * Adjust the congestion window after a retransmit timeout has occurred.
1660  */
1661 static void xs_udp_timer(struct rpc_xprt *xprt, struct rpc_task *task)
1662 {
1663 	xprt_adjust_cwnd(xprt, task, -ETIMEDOUT);
1664 }
1665 
1666 static unsigned short xs_get_random_port(void)
1667 {
1668 	unsigned short range = xprt_max_resvport - xprt_min_resvport;
1669 	unsigned short rand = (unsigned short) net_random() % range;
1670 	return rand + xprt_min_resvport;
1671 }
1672 
1673 /**
1674  * xs_set_port - reset the port number in the remote endpoint address
1675  * @xprt: generic transport
1676  * @port: new port number
1677  *
1678  */
1679 static void xs_set_port(struct rpc_xprt *xprt, unsigned short port)
1680 {
1681 	dprintk("RPC:       setting port for xprt %p to %u\n", xprt, port);
1682 
1683 	rpc_set_port(xs_addr(xprt), port);
1684 	xs_update_peer_port(xprt);
1685 }
1686 
1687 static unsigned short xs_get_srcport(struct sock_xprt *transport)
1688 {
1689 	unsigned short port = transport->srcport;
1690 
1691 	if (port == 0 && transport->xprt.resvport)
1692 		port = xs_get_random_port();
1693 	return port;
1694 }
1695 
1696 static unsigned short xs_next_srcport(struct sock_xprt *transport, unsigned short port)
1697 {
1698 	if (transport->srcport != 0)
1699 		transport->srcport = 0;
1700 	if (!transport->xprt.resvport)
1701 		return 0;
1702 	if (port <= xprt_min_resvport || port > xprt_max_resvport)
1703 		return xprt_max_resvport;
1704 	return --port;
1705 }
1706 static int xs_bind(struct sock_xprt *transport, struct socket *sock)
1707 {
1708 	struct sockaddr_storage myaddr;
1709 	int err, nloop = 0;
1710 	unsigned short port = xs_get_srcport(transport);
1711 	unsigned short last;
1712 
1713 	memcpy(&myaddr, &transport->srcaddr, transport->xprt.addrlen);
1714 	do {
1715 		rpc_set_port((struct sockaddr *)&myaddr, port);
1716 		err = kernel_bind(sock, (struct sockaddr *)&myaddr,
1717 				transport->xprt.addrlen);
1718 		if (port == 0)
1719 			break;
1720 		if (err == 0) {
1721 			transport->srcport = port;
1722 			break;
1723 		}
1724 		last = port;
1725 		port = xs_next_srcport(transport, port);
1726 		if (port > last)
1727 			nloop++;
1728 	} while (err == -EADDRINUSE && nloop != 2);
1729 
1730 	if (myaddr.ss_family == AF_INET)
1731 		dprintk("RPC:       %s %pI4:%u: %s (%d)\n", __func__,
1732 				&((struct sockaddr_in *)&myaddr)->sin_addr,
1733 				port, err ? "failed" : "ok", err);
1734 	else
1735 		dprintk("RPC:       %s %pI6:%u: %s (%d)\n", __func__,
1736 				&((struct sockaddr_in6 *)&myaddr)->sin6_addr,
1737 				port, err ? "failed" : "ok", err);
1738 	return err;
1739 }
1740 
1741 /*
1742  * We don't support autobind on AF_LOCAL sockets
1743  */
1744 static void xs_local_rpcbind(struct rpc_task *task)
1745 {
1746 	rcu_read_lock();
1747 	xprt_set_bound(rcu_dereference(task->tk_client->cl_xprt));
1748 	rcu_read_unlock();
1749 }
1750 
1751 static void xs_local_set_port(struct rpc_xprt *xprt, unsigned short port)
1752 {
1753 }
1754 
1755 #ifdef CONFIG_DEBUG_LOCK_ALLOC
1756 static struct lock_class_key xs_key[2];
1757 static struct lock_class_key xs_slock_key[2];
1758 
1759 static inline void xs_reclassify_socketu(struct socket *sock)
1760 {
1761 	struct sock *sk = sock->sk;
1762 
1763 	sock_lock_init_class_and_name(sk, "slock-AF_LOCAL-RPC",
1764 		&xs_slock_key[1], "sk_lock-AF_LOCAL-RPC", &xs_key[1]);
1765 }
1766 
1767 static inline void xs_reclassify_socket4(struct socket *sock)
1768 {
1769 	struct sock *sk = sock->sk;
1770 
1771 	sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC",
1772 		&xs_slock_key[0], "sk_lock-AF_INET-RPC", &xs_key[0]);
1773 }
1774 
1775 static inline void xs_reclassify_socket6(struct socket *sock)
1776 {
1777 	struct sock *sk = sock->sk;
1778 
1779 	sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC",
1780 		&xs_slock_key[1], "sk_lock-AF_INET6-RPC", &xs_key[1]);
1781 }
1782 
1783 static inline void xs_reclassify_socket(int family, struct socket *sock)
1784 {
1785 	WARN_ON_ONCE(sock_owned_by_user(sock->sk));
1786 	if (sock_owned_by_user(sock->sk))
1787 		return;
1788 
1789 	switch (family) {
1790 	case AF_LOCAL:
1791 		xs_reclassify_socketu(sock);
1792 		break;
1793 	case AF_INET:
1794 		xs_reclassify_socket4(sock);
1795 		break;
1796 	case AF_INET6:
1797 		xs_reclassify_socket6(sock);
1798 		break;
1799 	}
1800 }
1801 #else
1802 static inline void xs_reclassify_socketu(struct socket *sock)
1803 {
1804 }
1805 
1806 static inline void xs_reclassify_socket4(struct socket *sock)
1807 {
1808 }
1809 
1810 static inline void xs_reclassify_socket6(struct socket *sock)
1811 {
1812 }
1813 
1814 static inline void xs_reclassify_socket(int family, struct socket *sock)
1815 {
1816 }
1817 #endif
1818 
1819 static struct socket *xs_create_sock(struct rpc_xprt *xprt,
1820 		struct sock_xprt *transport, int family, int type, int protocol)
1821 {
1822 	struct socket *sock;
1823 	int err;
1824 
1825 	err = __sock_create(xprt->xprt_net, family, type, protocol, &sock, 1);
1826 	if (err < 0) {
1827 		dprintk("RPC:       can't create %d transport socket (%d).\n",
1828 				protocol, -err);
1829 		goto out;
1830 	}
1831 	xs_reclassify_socket(family, sock);
1832 
1833 	err = xs_bind(transport, sock);
1834 	if (err) {
1835 		sock_release(sock);
1836 		goto out;
1837 	}
1838 
1839 	return sock;
1840 out:
1841 	return ERR_PTR(err);
1842 }
1843 
1844 static int xs_local_finish_connecting(struct rpc_xprt *xprt,
1845 				      struct socket *sock)
1846 {
1847 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
1848 									xprt);
1849 
1850 	if (!transport->inet) {
1851 		struct sock *sk = sock->sk;
1852 
1853 		write_lock_bh(&sk->sk_callback_lock);
1854 
1855 		xs_save_old_callbacks(transport, sk);
1856 
1857 		sk->sk_user_data = xprt;
1858 		sk->sk_data_ready = xs_local_data_ready;
1859 		sk->sk_write_space = xs_udp_write_space;
1860 		sk->sk_allocation = GFP_ATOMIC;
1861 
1862 		xprt_clear_connected(xprt);
1863 
1864 		/* Reset to new socket */
1865 		transport->sock = sock;
1866 		transport->inet = sk;
1867 
1868 		write_unlock_bh(&sk->sk_callback_lock);
1869 	}
1870 
1871 	/* Tell the socket layer to start connecting... */
1872 	xprt->stat.connect_count++;
1873 	xprt->stat.connect_start = jiffies;
1874 	return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, 0);
1875 }
1876 
1877 /**
1878  * xs_local_setup_socket - create AF_LOCAL socket, connect to a local endpoint
1879  * @xprt: RPC transport to connect
1880  * @transport: socket transport to connect
1881  * @create_sock: function to create a socket of the correct type
1882  */
1883 static int xs_local_setup_socket(struct sock_xprt *transport)
1884 {
1885 	struct rpc_xprt *xprt = &transport->xprt;
1886 	struct socket *sock;
1887 	int status = -EIO;
1888 
1889 	current->flags |= PF_FSTRANS;
1890 
1891 	clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1892 	status = __sock_create(xprt->xprt_net, AF_LOCAL,
1893 					SOCK_STREAM, 0, &sock, 1);
1894 	if (status < 0) {
1895 		dprintk("RPC:       can't create AF_LOCAL "
1896 			"transport socket (%d).\n", -status);
1897 		goto out;
1898 	}
1899 	xs_reclassify_socketu(sock);
1900 
1901 	dprintk("RPC:       worker connecting xprt %p via AF_LOCAL to %s\n",
1902 			xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1903 
1904 	status = xs_local_finish_connecting(xprt, sock);
1905 	trace_rpc_socket_connect(xprt, sock, status);
1906 	switch (status) {
1907 	case 0:
1908 		dprintk("RPC:       xprt %p connected to %s\n",
1909 				xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1910 		xprt_set_connected(xprt);
1911 		break;
1912 	case -ENOENT:
1913 		dprintk("RPC:       xprt %p: socket %s does not exist\n",
1914 				xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1915 		break;
1916 	case -ECONNREFUSED:
1917 		dprintk("RPC:       xprt %p: connection refused for %s\n",
1918 				xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1919 		break;
1920 	default:
1921 		printk(KERN_ERR "%s: unhandled error (%d) connecting to %s\n",
1922 				__func__, -status,
1923 				xprt->address_strings[RPC_DISPLAY_ADDR]);
1924 	}
1925 
1926 out:
1927 	xprt_clear_connecting(xprt);
1928 	xprt_wake_pending_tasks(xprt, status);
1929 	current->flags &= ~PF_FSTRANS;
1930 	return status;
1931 }
1932 
1933 static void xs_local_connect(struct rpc_xprt *xprt, struct rpc_task *task)
1934 {
1935 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1936 	int ret;
1937 
1938 	 if (RPC_IS_ASYNC(task)) {
1939 		/*
1940 		 * We want the AF_LOCAL connect to be resolved in the
1941 		 * filesystem namespace of the process making the rpc
1942 		 * call.  Thus we connect synchronously.
1943 		 *
1944 		 * If we want to support asynchronous AF_LOCAL calls,
1945 		 * we'll need to figure out how to pass a namespace to
1946 		 * connect.
1947 		 */
1948 		rpc_exit(task, -ENOTCONN);
1949 		return;
1950 	}
1951 	ret = xs_local_setup_socket(transport);
1952 	if (ret && !RPC_IS_SOFTCONN(task))
1953 		msleep_interruptible(15000);
1954 }
1955 
1956 #ifdef CONFIG_SUNRPC_SWAP
1957 static void xs_set_memalloc(struct rpc_xprt *xprt)
1958 {
1959 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
1960 			xprt);
1961 
1962 	if (xprt->swapper)
1963 		sk_set_memalloc(transport->inet);
1964 }
1965 
1966 /**
1967  * xs_swapper - Tag this transport as being used for swap.
1968  * @xprt: transport to tag
1969  * @enable: enable/disable
1970  *
1971  */
1972 int xs_swapper(struct rpc_xprt *xprt, int enable)
1973 {
1974 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
1975 			xprt);
1976 	int err = 0;
1977 
1978 	if (enable) {
1979 		xprt->swapper++;
1980 		xs_set_memalloc(xprt);
1981 	} else if (xprt->swapper) {
1982 		xprt->swapper--;
1983 		sk_clear_memalloc(transport->inet);
1984 	}
1985 
1986 	return err;
1987 }
1988 EXPORT_SYMBOL_GPL(xs_swapper);
1989 #else
1990 static void xs_set_memalloc(struct rpc_xprt *xprt)
1991 {
1992 }
1993 #endif
1994 
1995 static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
1996 {
1997 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1998 
1999 	if (!transport->inet) {
2000 		struct sock *sk = sock->sk;
2001 
2002 		write_lock_bh(&sk->sk_callback_lock);
2003 
2004 		xs_save_old_callbacks(transport, sk);
2005 
2006 		sk->sk_user_data = xprt;
2007 		sk->sk_data_ready = xs_udp_data_ready;
2008 		sk->sk_write_space = xs_udp_write_space;
2009 		sk->sk_no_check = UDP_CSUM_NORCV;
2010 		sk->sk_allocation = GFP_ATOMIC;
2011 
2012 		xprt_set_connected(xprt);
2013 
2014 		/* Reset to new socket */
2015 		transport->sock = sock;
2016 		transport->inet = sk;
2017 
2018 		xs_set_memalloc(xprt);
2019 
2020 		write_unlock_bh(&sk->sk_callback_lock);
2021 	}
2022 	xs_udp_do_set_buffer_size(xprt);
2023 }
2024 
2025 static void xs_udp_setup_socket(struct work_struct *work)
2026 {
2027 	struct sock_xprt *transport =
2028 		container_of(work, struct sock_xprt, connect_worker.work);
2029 	struct rpc_xprt *xprt = &transport->xprt;
2030 	struct socket *sock = transport->sock;
2031 	int status = -EIO;
2032 
2033 	current->flags |= PF_FSTRANS;
2034 
2035 	/* Start by resetting any existing state */
2036 	xs_reset_transport(transport);
2037 	sock = xs_create_sock(xprt, transport,
2038 			xs_addr(xprt)->sa_family, SOCK_DGRAM, IPPROTO_UDP);
2039 	if (IS_ERR(sock))
2040 		goto out;
2041 
2042 	dprintk("RPC:       worker connecting xprt %p via %s to "
2043 				"%s (port %s)\n", xprt,
2044 			xprt->address_strings[RPC_DISPLAY_PROTO],
2045 			xprt->address_strings[RPC_DISPLAY_ADDR],
2046 			xprt->address_strings[RPC_DISPLAY_PORT]);
2047 
2048 	xs_udp_finish_connecting(xprt, sock);
2049 	trace_rpc_socket_connect(xprt, sock, 0);
2050 	status = 0;
2051 out:
2052 	xprt_clear_connecting(xprt);
2053 	xprt_wake_pending_tasks(xprt, status);
2054 	current->flags &= ~PF_FSTRANS;
2055 }
2056 
2057 /*
2058  * We need to preserve the port number so the reply cache on the server can
2059  * find our cached RPC replies when we get around to reconnecting.
2060  */
2061 static void xs_abort_connection(struct sock_xprt *transport)
2062 {
2063 	int result;
2064 	struct sockaddr any;
2065 
2066 	dprintk("RPC:       disconnecting xprt %p to reuse port\n", transport);
2067 
2068 	/*
2069 	 * Disconnect the transport socket by doing a connect operation
2070 	 * with AF_UNSPEC.  This should return immediately...
2071 	 */
2072 	memset(&any, 0, sizeof(any));
2073 	any.sa_family = AF_UNSPEC;
2074 	result = kernel_connect(transport->sock, &any, sizeof(any), 0);
2075 	trace_rpc_socket_reset_connection(&transport->xprt,
2076 			transport->sock, result);
2077 	if (!result)
2078 		xs_sock_reset_connection_flags(&transport->xprt);
2079 	dprintk("RPC:       AF_UNSPEC connect return code %d\n", result);
2080 }
2081 
2082 static void xs_tcp_reuse_connection(struct sock_xprt *transport)
2083 {
2084 	unsigned int state = transport->inet->sk_state;
2085 
2086 	if (state == TCP_CLOSE && transport->sock->state == SS_UNCONNECTED) {
2087 		/* we don't need to abort the connection if the socket
2088 		 * hasn't undergone a shutdown
2089 		 */
2090 		if (transport->inet->sk_shutdown == 0)
2091 			return;
2092 		dprintk("RPC:       %s: TCP_CLOSEd and sk_shutdown set to %d\n",
2093 				__func__, transport->inet->sk_shutdown);
2094 	}
2095 	if ((1 << state) & (TCPF_ESTABLISHED|TCPF_SYN_SENT)) {
2096 		/* we don't need to abort the connection if the socket
2097 		 * hasn't undergone a shutdown
2098 		 */
2099 		if (transport->inet->sk_shutdown == 0)
2100 			return;
2101 		dprintk("RPC:       %s: ESTABLISHED/SYN_SENT "
2102 				"sk_shutdown set to %d\n",
2103 				__func__, transport->inet->sk_shutdown);
2104 	}
2105 	xs_abort_connection(transport);
2106 }
2107 
2108 static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
2109 {
2110 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2111 	int ret = -ENOTCONN;
2112 
2113 	if (!transport->inet) {
2114 		struct sock *sk = sock->sk;
2115 
2116 		write_lock_bh(&sk->sk_callback_lock);
2117 
2118 		xs_save_old_callbacks(transport, sk);
2119 
2120 		sk->sk_user_data = xprt;
2121 		sk->sk_data_ready = xs_tcp_data_ready;
2122 		sk->sk_state_change = xs_tcp_state_change;
2123 		sk->sk_write_space = xs_tcp_write_space;
2124 		sk->sk_allocation = GFP_ATOMIC;
2125 
2126 		/* socket options */
2127 		sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
2128 		sock_reset_flag(sk, SOCK_LINGER);
2129 		tcp_sk(sk)->linger2 = 0;
2130 		tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF;
2131 
2132 		xprt_clear_connected(xprt);
2133 
2134 		/* Reset to new socket */
2135 		transport->sock = sock;
2136 		transport->inet = sk;
2137 
2138 		write_unlock_bh(&sk->sk_callback_lock);
2139 	}
2140 
2141 	if (!xprt_bound(xprt))
2142 		goto out;
2143 
2144 	xs_set_memalloc(xprt);
2145 
2146 	/* Tell the socket layer to start connecting... */
2147 	xprt->stat.connect_count++;
2148 	xprt->stat.connect_start = jiffies;
2149 	ret = kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK);
2150 	switch (ret) {
2151 	case 0:
2152 	case -EINPROGRESS:
2153 		/* SYN_SENT! */
2154 		xprt->connect_cookie++;
2155 		if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
2156 			xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2157 	}
2158 out:
2159 	return ret;
2160 }
2161 
2162 /**
2163  * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint
2164  * @xprt: RPC transport to connect
2165  * @transport: socket transport to connect
2166  * @create_sock: function to create a socket of the correct type
2167  *
2168  * Invoked by a work queue tasklet.
2169  */
2170 static void xs_tcp_setup_socket(struct work_struct *work)
2171 {
2172 	struct sock_xprt *transport =
2173 		container_of(work, struct sock_xprt, connect_worker.work);
2174 	struct socket *sock = transport->sock;
2175 	struct rpc_xprt *xprt = &transport->xprt;
2176 	int status = -EIO;
2177 
2178 	current->flags |= PF_FSTRANS;
2179 
2180 	if (!sock) {
2181 		clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
2182 		sock = xs_create_sock(xprt, transport,
2183 				xs_addr(xprt)->sa_family, SOCK_STREAM, IPPROTO_TCP);
2184 		if (IS_ERR(sock)) {
2185 			status = PTR_ERR(sock);
2186 			goto out;
2187 		}
2188 	} else {
2189 		int abort_and_exit;
2190 
2191 		abort_and_exit = test_and_clear_bit(XPRT_CONNECTION_ABORT,
2192 				&xprt->state);
2193 		/* "close" the socket, preserving the local port */
2194 		xs_tcp_reuse_connection(transport);
2195 
2196 		if (abort_and_exit)
2197 			goto out_eagain;
2198 	}
2199 
2200 	dprintk("RPC:       worker connecting xprt %p via %s to "
2201 				"%s (port %s)\n", xprt,
2202 			xprt->address_strings[RPC_DISPLAY_PROTO],
2203 			xprt->address_strings[RPC_DISPLAY_ADDR],
2204 			xprt->address_strings[RPC_DISPLAY_PORT]);
2205 
2206 	status = xs_tcp_finish_connecting(xprt, sock);
2207 	trace_rpc_socket_connect(xprt, sock, status);
2208 	dprintk("RPC:       %p connect status %d connected %d sock state %d\n",
2209 			xprt, -status, xprt_connected(xprt),
2210 			sock->sk->sk_state);
2211 	switch (status) {
2212 	default:
2213 		printk("%s: connect returned unhandled error %d\n",
2214 			__func__, status);
2215 	case -EADDRNOTAVAIL:
2216 		/* We're probably in TIME_WAIT. Get rid of existing socket,
2217 		 * and retry
2218 		 */
2219 		xs_tcp_force_close(xprt);
2220 		break;
2221 	case 0:
2222 	case -EINPROGRESS:
2223 	case -EALREADY:
2224 		xprt_clear_connecting(xprt);
2225 		current->flags &= ~PF_FSTRANS;
2226 		return;
2227 	case -EINVAL:
2228 		/* Happens, for instance, if the user specified a link
2229 		 * local IPv6 address without a scope-id.
2230 		 */
2231 	case -ECONNREFUSED:
2232 	case -ECONNRESET:
2233 	case -ENETUNREACH:
2234 		/* retry with existing socket, after a delay */
2235 		goto out;
2236 	}
2237 out_eagain:
2238 	status = -EAGAIN;
2239 out:
2240 	xprt_clear_connecting(xprt);
2241 	xprt_wake_pending_tasks(xprt, status);
2242 	current->flags &= ~PF_FSTRANS;
2243 }
2244 
2245 /**
2246  * xs_connect - connect a socket to a remote endpoint
2247  * @xprt: pointer to transport structure
2248  * @task: address of RPC task that manages state of connect request
2249  *
2250  * TCP: If the remote end dropped the connection, delay reconnecting.
2251  *
2252  * UDP socket connects are synchronous, but we use a work queue anyway
2253  * to guarantee that even unprivileged user processes can set up a
2254  * socket on a privileged port.
2255  *
2256  * If a UDP socket connect fails, the delay behavior here prevents
2257  * retry floods (hard mounts).
2258  */
2259 static void xs_connect(struct rpc_xprt *xprt, struct rpc_task *task)
2260 {
2261 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2262 
2263 	if (transport->sock != NULL && !RPC_IS_SOFTCONN(task)) {
2264 		dprintk("RPC:       xs_connect delayed xprt %p for %lu "
2265 				"seconds\n",
2266 				xprt, xprt->reestablish_timeout / HZ);
2267 		queue_delayed_work(rpciod_workqueue,
2268 				   &transport->connect_worker,
2269 				   xprt->reestablish_timeout);
2270 		xprt->reestablish_timeout <<= 1;
2271 		if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
2272 			xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2273 		if (xprt->reestablish_timeout > XS_TCP_MAX_REEST_TO)
2274 			xprt->reestablish_timeout = XS_TCP_MAX_REEST_TO;
2275 	} else {
2276 		dprintk("RPC:       xs_connect scheduled xprt %p\n", xprt);
2277 		queue_delayed_work(rpciod_workqueue,
2278 				   &transport->connect_worker, 0);
2279 	}
2280 }
2281 
2282 /**
2283  * xs_local_print_stats - display AF_LOCAL socket-specifc stats
2284  * @xprt: rpc_xprt struct containing statistics
2285  * @seq: output file
2286  *
2287  */
2288 static void xs_local_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2289 {
2290 	long idle_time = 0;
2291 
2292 	if (xprt_connected(xprt))
2293 		idle_time = (long)(jiffies - xprt->last_used) / HZ;
2294 
2295 	seq_printf(seq, "\txprt:\tlocal %lu %lu %lu %ld %lu %lu %lu "
2296 			"%llu %llu %lu %llu %llu\n",
2297 			xprt->stat.bind_count,
2298 			xprt->stat.connect_count,
2299 			xprt->stat.connect_time,
2300 			idle_time,
2301 			xprt->stat.sends,
2302 			xprt->stat.recvs,
2303 			xprt->stat.bad_xids,
2304 			xprt->stat.req_u,
2305 			xprt->stat.bklog_u,
2306 			xprt->stat.max_slots,
2307 			xprt->stat.sending_u,
2308 			xprt->stat.pending_u);
2309 }
2310 
2311 /**
2312  * xs_udp_print_stats - display UDP socket-specifc stats
2313  * @xprt: rpc_xprt struct containing statistics
2314  * @seq: output file
2315  *
2316  */
2317 static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2318 {
2319 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2320 
2321 	seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %llu %llu "
2322 			"%lu %llu %llu\n",
2323 			transport->srcport,
2324 			xprt->stat.bind_count,
2325 			xprt->stat.sends,
2326 			xprt->stat.recvs,
2327 			xprt->stat.bad_xids,
2328 			xprt->stat.req_u,
2329 			xprt->stat.bklog_u,
2330 			xprt->stat.max_slots,
2331 			xprt->stat.sending_u,
2332 			xprt->stat.pending_u);
2333 }
2334 
2335 /**
2336  * xs_tcp_print_stats - display TCP socket-specifc stats
2337  * @xprt: rpc_xprt struct containing statistics
2338  * @seq: output file
2339  *
2340  */
2341 static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2342 {
2343 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2344 	long idle_time = 0;
2345 
2346 	if (xprt_connected(xprt))
2347 		idle_time = (long)(jiffies - xprt->last_used) / HZ;
2348 
2349 	seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu "
2350 			"%llu %llu %lu %llu %llu\n",
2351 			transport->srcport,
2352 			xprt->stat.bind_count,
2353 			xprt->stat.connect_count,
2354 			xprt->stat.connect_time,
2355 			idle_time,
2356 			xprt->stat.sends,
2357 			xprt->stat.recvs,
2358 			xprt->stat.bad_xids,
2359 			xprt->stat.req_u,
2360 			xprt->stat.bklog_u,
2361 			xprt->stat.max_slots,
2362 			xprt->stat.sending_u,
2363 			xprt->stat.pending_u);
2364 }
2365 
2366 /*
2367  * Allocate a bunch of pages for a scratch buffer for the rpc code. The reason
2368  * we allocate pages instead doing a kmalloc like rpc_malloc is because we want
2369  * to use the server side send routines.
2370  */
2371 static void *bc_malloc(struct rpc_task *task, size_t size)
2372 {
2373 	struct page *page;
2374 	struct rpc_buffer *buf;
2375 
2376 	WARN_ON_ONCE(size > PAGE_SIZE - sizeof(struct rpc_buffer));
2377 	if (size > PAGE_SIZE - sizeof(struct rpc_buffer))
2378 		return NULL;
2379 
2380 	page = alloc_page(GFP_KERNEL);
2381 	if (!page)
2382 		return NULL;
2383 
2384 	buf = page_address(page);
2385 	buf->len = PAGE_SIZE;
2386 
2387 	return buf->data;
2388 }
2389 
2390 /*
2391  * Free the space allocated in the bc_alloc routine
2392  */
2393 static void bc_free(void *buffer)
2394 {
2395 	struct rpc_buffer *buf;
2396 
2397 	if (!buffer)
2398 		return;
2399 
2400 	buf = container_of(buffer, struct rpc_buffer, data);
2401 	free_page((unsigned long)buf);
2402 }
2403 
2404 /*
2405  * Use the svc_sock to send the callback. Must be called with svsk->sk_mutex
2406  * held. Borrows heavily from svc_tcp_sendto and xs_tcp_send_request.
2407  */
2408 static int bc_sendto(struct rpc_rqst *req)
2409 {
2410 	int len;
2411 	struct xdr_buf *xbufp = &req->rq_snd_buf;
2412 	struct rpc_xprt *xprt = req->rq_xprt;
2413 	struct sock_xprt *transport =
2414 				container_of(xprt, struct sock_xprt, xprt);
2415 	struct socket *sock = transport->sock;
2416 	unsigned long headoff;
2417 	unsigned long tailoff;
2418 
2419 	xs_encode_stream_record_marker(xbufp);
2420 
2421 	tailoff = (unsigned long)xbufp->tail[0].iov_base & ~PAGE_MASK;
2422 	headoff = (unsigned long)xbufp->head[0].iov_base & ~PAGE_MASK;
2423 	len = svc_send_common(sock, xbufp,
2424 			      virt_to_page(xbufp->head[0].iov_base), headoff,
2425 			      xbufp->tail[0].iov_base, tailoff);
2426 
2427 	if (len != xbufp->len) {
2428 		printk(KERN_NOTICE "Error sending entire callback!\n");
2429 		len = -EAGAIN;
2430 	}
2431 
2432 	return len;
2433 }
2434 
2435 /*
2436  * The send routine. Borrows from svc_send
2437  */
2438 static int bc_send_request(struct rpc_task *task)
2439 {
2440 	struct rpc_rqst *req = task->tk_rqstp;
2441 	struct svc_xprt	*xprt;
2442 	u32                     len;
2443 
2444 	dprintk("sending request with xid: %08x\n", ntohl(req->rq_xid));
2445 	/*
2446 	 * Get the server socket associated with this callback xprt
2447 	 */
2448 	xprt = req->rq_xprt->bc_xprt;
2449 
2450 	/*
2451 	 * Grab the mutex to serialize data as the connection is shared
2452 	 * with the fore channel
2453 	 */
2454 	if (!mutex_trylock(&xprt->xpt_mutex)) {
2455 		rpc_sleep_on(&xprt->xpt_bc_pending, task, NULL);
2456 		if (!mutex_trylock(&xprt->xpt_mutex))
2457 			return -EAGAIN;
2458 		rpc_wake_up_queued_task(&xprt->xpt_bc_pending, task);
2459 	}
2460 	if (test_bit(XPT_DEAD, &xprt->xpt_flags))
2461 		len = -ENOTCONN;
2462 	else
2463 		len = bc_sendto(req);
2464 	mutex_unlock(&xprt->xpt_mutex);
2465 
2466 	if (len > 0)
2467 		len = 0;
2468 
2469 	return len;
2470 }
2471 
2472 /*
2473  * The close routine. Since this is client initiated, we do nothing
2474  */
2475 
2476 static void bc_close(struct rpc_xprt *xprt)
2477 {
2478 }
2479 
2480 /*
2481  * The xprt destroy routine. Again, because this connection is client
2482  * initiated, we do nothing
2483  */
2484 
2485 static void bc_destroy(struct rpc_xprt *xprt)
2486 {
2487 }
2488 
2489 static struct rpc_xprt_ops xs_local_ops = {
2490 	.reserve_xprt		= xprt_reserve_xprt,
2491 	.release_xprt		= xs_tcp_release_xprt,
2492 	.alloc_slot		= xprt_alloc_slot,
2493 	.rpcbind		= xs_local_rpcbind,
2494 	.set_port		= xs_local_set_port,
2495 	.connect		= xs_local_connect,
2496 	.buf_alloc		= rpc_malloc,
2497 	.buf_free		= rpc_free,
2498 	.send_request		= xs_local_send_request,
2499 	.set_retrans_timeout	= xprt_set_retrans_timeout_def,
2500 	.close			= xs_close,
2501 	.destroy		= xs_local_destroy,
2502 	.print_stats		= xs_local_print_stats,
2503 };
2504 
2505 static struct rpc_xprt_ops xs_udp_ops = {
2506 	.set_buffer_size	= xs_udp_set_buffer_size,
2507 	.reserve_xprt		= xprt_reserve_xprt_cong,
2508 	.release_xprt		= xprt_release_xprt_cong,
2509 	.alloc_slot		= xprt_alloc_slot,
2510 	.rpcbind		= rpcb_getport_async,
2511 	.set_port		= xs_set_port,
2512 	.connect		= xs_connect,
2513 	.buf_alloc		= rpc_malloc,
2514 	.buf_free		= rpc_free,
2515 	.send_request		= xs_udp_send_request,
2516 	.set_retrans_timeout	= xprt_set_retrans_timeout_rtt,
2517 	.timer			= xs_udp_timer,
2518 	.release_request	= xprt_release_rqst_cong,
2519 	.close			= xs_close,
2520 	.destroy		= xs_destroy,
2521 	.print_stats		= xs_udp_print_stats,
2522 };
2523 
2524 static struct rpc_xprt_ops xs_tcp_ops = {
2525 	.reserve_xprt		= xprt_reserve_xprt,
2526 	.release_xprt		= xs_tcp_release_xprt,
2527 	.alloc_slot		= xprt_lock_and_alloc_slot,
2528 	.rpcbind		= rpcb_getport_async,
2529 	.set_port		= xs_set_port,
2530 	.connect		= xs_connect,
2531 	.buf_alloc		= rpc_malloc,
2532 	.buf_free		= rpc_free,
2533 	.send_request		= xs_tcp_send_request,
2534 	.set_retrans_timeout	= xprt_set_retrans_timeout_def,
2535 	.close			= xs_tcp_close,
2536 	.destroy		= xs_destroy,
2537 	.print_stats		= xs_tcp_print_stats,
2538 };
2539 
2540 /*
2541  * The rpc_xprt_ops for the server backchannel
2542  */
2543 
2544 static struct rpc_xprt_ops bc_tcp_ops = {
2545 	.reserve_xprt		= xprt_reserve_xprt,
2546 	.release_xprt		= xprt_release_xprt,
2547 	.alloc_slot		= xprt_alloc_slot,
2548 	.buf_alloc		= bc_malloc,
2549 	.buf_free		= bc_free,
2550 	.send_request		= bc_send_request,
2551 	.set_retrans_timeout	= xprt_set_retrans_timeout_def,
2552 	.close			= bc_close,
2553 	.destroy		= bc_destroy,
2554 	.print_stats		= xs_tcp_print_stats,
2555 };
2556 
2557 static int xs_init_anyaddr(const int family, struct sockaddr *sap)
2558 {
2559 	static const struct sockaddr_in sin = {
2560 		.sin_family		= AF_INET,
2561 		.sin_addr.s_addr	= htonl(INADDR_ANY),
2562 	};
2563 	static const struct sockaddr_in6 sin6 = {
2564 		.sin6_family		= AF_INET6,
2565 		.sin6_addr		= IN6ADDR_ANY_INIT,
2566 	};
2567 
2568 	switch (family) {
2569 	case AF_LOCAL:
2570 		break;
2571 	case AF_INET:
2572 		memcpy(sap, &sin, sizeof(sin));
2573 		break;
2574 	case AF_INET6:
2575 		memcpy(sap, &sin6, sizeof(sin6));
2576 		break;
2577 	default:
2578 		dprintk("RPC:       %s: Bad address family\n", __func__);
2579 		return -EAFNOSUPPORT;
2580 	}
2581 	return 0;
2582 }
2583 
2584 static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args,
2585 				      unsigned int slot_table_size,
2586 				      unsigned int max_slot_table_size)
2587 {
2588 	struct rpc_xprt *xprt;
2589 	struct sock_xprt *new;
2590 
2591 	if (args->addrlen > sizeof(xprt->addr)) {
2592 		dprintk("RPC:       xs_setup_xprt: address too large\n");
2593 		return ERR_PTR(-EBADF);
2594 	}
2595 
2596 	xprt = xprt_alloc(args->net, sizeof(*new), slot_table_size,
2597 			max_slot_table_size);
2598 	if (xprt == NULL) {
2599 		dprintk("RPC:       xs_setup_xprt: couldn't allocate "
2600 				"rpc_xprt\n");
2601 		return ERR_PTR(-ENOMEM);
2602 	}
2603 
2604 	new = container_of(xprt, struct sock_xprt, xprt);
2605 	memcpy(&xprt->addr, args->dstaddr, args->addrlen);
2606 	xprt->addrlen = args->addrlen;
2607 	if (args->srcaddr)
2608 		memcpy(&new->srcaddr, args->srcaddr, args->addrlen);
2609 	else {
2610 		int err;
2611 		err = xs_init_anyaddr(args->dstaddr->sa_family,
2612 					(struct sockaddr *)&new->srcaddr);
2613 		if (err != 0) {
2614 			xprt_free(xprt);
2615 			return ERR_PTR(err);
2616 		}
2617 	}
2618 
2619 	return xprt;
2620 }
2621 
2622 static const struct rpc_timeout xs_local_default_timeout = {
2623 	.to_initval = 10 * HZ,
2624 	.to_maxval = 10 * HZ,
2625 	.to_retries = 2,
2626 };
2627 
2628 /**
2629  * xs_setup_local - Set up transport to use an AF_LOCAL socket
2630  * @args: rpc transport creation arguments
2631  *
2632  * AF_LOCAL is a "tpi_cots_ord" transport, just like TCP
2633  */
2634 static struct rpc_xprt *xs_setup_local(struct xprt_create *args)
2635 {
2636 	struct sockaddr_un *sun = (struct sockaddr_un *)args->dstaddr;
2637 	struct sock_xprt *transport;
2638 	struct rpc_xprt *xprt;
2639 	struct rpc_xprt *ret;
2640 
2641 	xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
2642 			xprt_max_tcp_slot_table_entries);
2643 	if (IS_ERR(xprt))
2644 		return xprt;
2645 	transport = container_of(xprt, struct sock_xprt, xprt);
2646 
2647 	xprt->prot = 0;
2648 	xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
2649 	xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2650 
2651 	xprt->bind_timeout = XS_BIND_TO;
2652 	xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2653 	xprt->idle_timeout = XS_IDLE_DISC_TO;
2654 
2655 	xprt->ops = &xs_local_ops;
2656 	xprt->timeout = &xs_local_default_timeout;
2657 
2658 	switch (sun->sun_family) {
2659 	case AF_LOCAL:
2660 		if (sun->sun_path[0] != '/') {
2661 			dprintk("RPC:       bad AF_LOCAL address: %s\n",
2662 					sun->sun_path);
2663 			ret = ERR_PTR(-EINVAL);
2664 			goto out_err;
2665 		}
2666 		xprt_set_bound(xprt);
2667 		xs_format_peer_addresses(xprt, "local", RPCBIND_NETID_LOCAL);
2668 		ret = ERR_PTR(xs_local_setup_socket(transport));
2669 		if (ret)
2670 			goto out_err;
2671 		break;
2672 	default:
2673 		ret = ERR_PTR(-EAFNOSUPPORT);
2674 		goto out_err;
2675 	}
2676 
2677 	dprintk("RPC:       set up xprt to %s via AF_LOCAL\n",
2678 			xprt->address_strings[RPC_DISPLAY_ADDR]);
2679 
2680 	if (try_module_get(THIS_MODULE))
2681 		return xprt;
2682 	ret = ERR_PTR(-EINVAL);
2683 out_err:
2684 	xprt_free(xprt);
2685 	return ret;
2686 }
2687 
2688 static const struct rpc_timeout xs_udp_default_timeout = {
2689 	.to_initval = 5 * HZ,
2690 	.to_maxval = 30 * HZ,
2691 	.to_increment = 5 * HZ,
2692 	.to_retries = 5,
2693 };
2694 
2695 /**
2696  * xs_setup_udp - Set up transport to use a UDP socket
2697  * @args: rpc transport creation arguments
2698  *
2699  */
2700 static struct rpc_xprt *xs_setup_udp(struct xprt_create *args)
2701 {
2702 	struct sockaddr *addr = args->dstaddr;
2703 	struct rpc_xprt *xprt;
2704 	struct sock_xprt *transport;
2705 	struct rpc_xprt *ret;
2706 
2707 	xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries,
2708 			xprt_udp_slot_table_entries);
2709 	if (IS_ERR(xprt))
2710 		return xprt;
2711 	transport = container_of(xprt, struct sock_xprt, xprt);
2712 
2713 	xprt->prot = IPPROTO_UDP;
2714 	xprt->tsh_size = 0;
2715 	/* XXX: header size can vary due to auth type, IPv6, etc. */
2716 	xprt->max_payload = (1U << 16) - (MAX_HEADER << 3);
2717 
2718 	xprt->bind_timeout = XS_BIND_TO;
2719 	xprt->reestablish_timeout = XS_UDP_REEST_TO;
2720 	xprt->idle_timeout = XS_IDLE_DISC_TO;
2721 
2722 	xprt->ops = &xs_udp_ops;
2723 
2724 	xprt->timeout = &xs_udp_default_timeout;
2725 
2726 	switch (addr->sa_family) {
2727 	case AF_INET:
2728 		if (((struct sockaddr_in *)addr)->sin_port != htons(0))
2729 			xprt_set_bound(xprt);
2730 
2731 		INIT_DELAYED_WORK(&transport->connect_worker,
2732 					xs_udp_setup_socket);
2733 		xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP);
2734 		break;
2735 	case AF_INET6:
2736 		if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
2737 			xprt_set_bound(xprt);
2738 
2739 		INIT_DELAYED_WORK(&transport->connect_worker,
2740 					xs_udp_setup_socket);
2741 		xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6);
2742 		break;
2743 	default:
2744 		ret = ERR_PTR(-EAFNOSUPPORT);
2745 		goto out_err;
2746 	}
2747 
2748 	if (xprt_bound(xprt))
2749 		dprintk("RPC:       set up xprt to %s (port %s) via %s\n",
2750 				xprt->address_strings[RPC_DISPLAY_ADDR],
2751 				xprt->address_strings[RPC_DISPLAY_PORT],
2752 				xprt->address_strings[RPC_DISPLAY_PROTO]);
2753 	else
2754 		dprintk("RPC:       set up xprt to %s (autobind) via %s\n",
2755 				xprt->address_strings[RPC_DISPLAY_ADDR],
2756 				xprt->address_strings[RPC_DISPLAY_PROTO]);
2757 
2758 	if (try_module_get(THIS_MODULE))
2759 		return xprt;
2760 	ret = ERR_PTR(-EINVAL);
2761 out_err:
2762 	xprt_free(xprt);
2763 	return ret;
2764 }
2765 
2766 static const struct rpc_timeout xs_tcp_default_timeout = {
2767 	.to_initval = 60 * HZ,
2768 	.to_maxval = 60 * HZ,
2769 	.to_retries = 2,
2770 };
2771 
2772 /**
2773  * xs_setup_tcp - Set up transport to use a TCP socket
2774  * @args: rpc transport creation arguments
2775  *
2776  */
2777 static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args)
2778 {
2779 	struct sockaddr *addr = args->dstaddr;
2780 	struct rpc_xprt *xprt;
2781 	struct sock_xprt *transport;
2782 	struct rpc_xprt *ret;
2783 	unsigned int max_slot_table_size = xprt_max_tcp_slot_table_entries;
2784 
2785 	if (args->flags & XPRT_CREATE_INFINITE_SLOTS)
2786 		max_slot_table_size = RPC_MAX_SLOT_TABLE_LIMIT;
2787 
2788 	xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
2789 			max_slot_table_size);
2790 	if (IS_ERR(xprt))
2791 		return xprt;
2792 	transport = container_of(xprt, struct sock_xprt, xprt);
2793 
2794 	xprt->prot = IPPROTO_TCP;
2795 	xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
2796 	xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2797 
2798 	xprt->bind_timeout = XS_BIND_TO;
2799 	xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2800 	xprt->idle_timeout = XS_IDLE_DISC_TO;
2801 
2802 	xprt->ops = &xs_tcp_ops;
2803 	xprt->timeout = &xs_tcp_default_timeout;
2804 
2805 	switch (addr->sa_family) {
2806 	case AF_INET:
2807 		if (((struct sockaddr_in *)addr)->sin_port != htons(0))
2808 			xprt_set_bound(xprt);
2809 
2810 		INIT_DELAYED_WORK(&transport->connect_worker,
2811 					xs_tcp_setup_socket);
2812 		xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP);
2813 		break;
2814 	case AF_INET6:
2815 		if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
2816 			xprt_set_bound(xprt);
2817 
2818 		INIT_DELAYED_WORK(&transport->connect_worker,
2819 					xs_tcp_setup_socket);
2820 		xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6);
2821 		break;
2822 	default:
2823 		ret = ERR_PTR(-EAFNOSUPPORT);
2824 		goto out_err;
2825 	}
2826 
2827 	if (xprt_bound(xprt))
2828 		dprintk("RPC:       set up xprt to %s (port %s) via %s\n",
2829 				xprt->address_strings[RPC_DISPLAY_ADDR],
2830 				xprt->address_strings[RPC_DISPLAY_PORT],
2831 				xprt->address_strings[RPC_DISPLAY_PROTO]);
2832 	else
2833 		dprintk("RPC:       set up xprt to %s (autobind) via %s\n",
2834 				xprt->address_strings[RPC_DISPLAY_ADDR],
2835 				xprt->address_strings[RPC_DISPLAY_PROTO]);
2836 
2837 
2838 	if (try_module_get(THIS_MODULE))
2839 		return xprt;
2840 	ret = ERR_PTR(-EINVAL);
2841 out_err:
2842 	xprt_free(xprt);
2843 	return ret;
2844 }
2845 
2846 /**
2847  * xs_setup_bc_tcp - Set up transport to use a TCP backchannel socket
2848  * @args: rpc transport creation arguments
2849  *
2850  */
2851 static struct rpc_xprt *xs_setup_bc_tcp(struct xprt_create *args)
2852 {
2853 	struct sockaddr *addr = args->dstaddr;
2854 	struct rpc_xprt *xprt;
2855 	struct sock_xprt *transport;
2856 	struct svc_sock *bc_sock;
2857 	struct rpc_xprt *ret;
2858 
2859 	if (args->bc_xprt->xpt_bc_xprt) {
2860 		/*
2861 		 * This server connection already has a backchannel
2862 		 * export; we can't create a new one, as we wouldn't be
2863 		 * able to match replies based on xid any more.  So,
2864 		 * reuse the already-existing one:
2865 		 */
2866 		 return args->bc_xprt->xpt_bc_xprt;
2867 	}
2868 	xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
2869 			xprt_tcp_slot_table_entries);
2870 	if (IS_ERR(xprt))
2871 		return xprt;
2872 	transport = container_of(xprt, struct sock_xprt, xprt);
2873 
2874 	xprt->prot = IPPROTO_TCP;
2875 	xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
2876 	xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2877 	xprt->timeout = &xs_tcp_default_timeout;
2878 
2879 	/* backchannel */
2880 	xprt_set_bound(xprt);
2881 	xprt->bind_timeout = 0;
2882 	xprt->reestablish_timeout = 0;
2883 	xprt->idle_timeout = 0;
2884 
2885 	xprt->ops = &bc_tcp_ops;
2886 
2887 	switch (addr->sa_family) {
2888 	case AF_INET:
2889 		xs_format_peer_addresses(xprt, "tcp",
2890 					 RPCBIND_NETID_TCP);
2891 		break;
2892 	case AF_INET6:
2893 		xs_format_peer_addresses(xprt, "tcp",
2894 				   RPCBIND_NETID_TCP6);
2895 		break;
2896 	default:
2897 		ret = ERR_PTR(-EAFNOSUPPORT);
2898 		goto out_err;
2899 	}
2900 
2901 	dprintk("RPC:       set up xprt to %s (port %s) via %s\n",
2902 			xprt->address_strings[RPC_DISPLAY_ADDR],
2903 			xprt->address_strings[RPC_DISPLAY_PORT],
2904 			xprt->address_strings[RPC_DISPLAY_PROTO]);
2905 
2906 	/*
2907 	 * Once we've associated a backchannel xprt with a connection,
2908 	 * we want to keep it around as long as long as the connection
2909 	 * lasts, in case we need to start using it for a backchannel
2910 	 * again; this reference won't be dropped until bc_xprt is
2911 	 * destroyed.
2912 	 */
2913 	xprt_get(xprt);
2914 	args->bc_xprt->xpt_bc_xprt = xprt;
2915 	xprt->bc_xprt = args->bc_xprt;
2916 	bc_sock = container_of(args->bc_xprt, struct svc_sock, sk_xprt);
2917 	transport->sock = bc_sock->sk_sock;
2918 	transport->inet = bc_sock->sk_sk;
2919 
2920 	/*
2921 	 * Since we don't want connections for the backchannel, we set
2922 	 * the xprt status to connected
2923 	 */
2924 	xprt_set_connected(xprt);
2925 
2926 
2927 	if (try_module_get(THIS_MODULE))
2928 		return xprt;
2929 	xprt_put(xprt);
2930 	ret = ERR_PTR(-EINVAL);
2931 out_err:
2932 	xprt_free(xprt);
2933 	return ret;
2934 }
2935 
2936 static struct xprt_class	xs_local_transport = {
2937 	.list		= LIST_HEAD_INIT(xs_local_transport.list),
2938 	.name		= "named UNIX socket",
2939 	.owner		= THIS_MODULE,
2940 	.ident		= XPRT_TRANSPORT_LOCAL,
2941 	.setup		= xs_setup_local,
2942 };
2943 
2944 static struct xprt_class	xs_udp_transport = {
2945 	.list		= LIST_HEAD_INIT(xs_udp_transport.list),
2946 	.name		= "udp",
2947 	.owner		= THIS_MODULE,
2948 	.ident		= XPRT_TRANSPORT_UDP,
2949 	.setup		= xs_setup_udp,
2950 };
2951 
2952 static struct xprt_class	xs_tcp_transport = {
2953 	.list		= LIST_HEAD_INIT(xs_tcp_transport.list),
2954 	.name		= "tcp",
2955 	.owner		= THIS_MODULE,
2956 	.ident		= XPRT_TRANSPORT_TCP,
2957 	.setup		= xs_setup_tcp,
2958 };
2959 
2960 static struct xprt_class	xs_bc_tcp_transport = {
2961 	.list		= LIST_HEAD_INIT(xs_bc_tcp_transport.list),
2962 	.name		= "tcp NFSv4.1 backchannel",
2963 	.owner		= THIS_MODULE,
2964 	.ident		= XPRT_TRANSPORT_BC_TCP,
2965 	.setup		= xs_setup_bc_tcp,
2966 };
2967 
2968 /**
2969  * init_socket_xprt - set up xprtsock's sysctls, register with RPC client
2970  *
2971  */
2972 int init_socket_xprt(void)
2973 {
2974 #ifdef RPC_DEBUG
2975 	if (!sunrpc_table_header)
2976 		sunrpc_table_header = register_sysctl_table(sunrpc_table);
2977 #endif
2978 
2979 	xprt_register_transport(&xs_local_transport);
2980 	xprt_register_transport(&xs_udp_transport);
2981 	xprt_register_transport(&xs_tcp_transport);
2982 	xprt_register_transport(&xs_bc_tcp_transport);
2983 
2984 	return 0;
2985 }
2986 
2987 /**
2988  * cleanup_socket_xprt - remove xprtsock's sysctls, unregister
2989  *
2990  */
2991 void cleanup_socket_xprt(void)
2992 {
2993 #ifdef RPC_DEBUG
2994 	if (sunrpc_table_header) {
2995 		unregister_sysctl_table(sunrpc_table_header);
2996 		sunrpc_table_header = NULL;
2997 	}
2998 #endif
2999 
3000 	xprt_unregister_transport(&xs_local_transport);
3001 	xprt_unregister_transport(&xs_udp_transport);
3002 	xprt_unregister_transport(&xs_tcp_transport);
3003 	xprt_unregister_transport(&xs_bc_tcp_transport);
3004 }
3005 
3006 static int param_set_uint_minmax(const char *val,
3007 		const struct kernel_param *kp,
3008 		unsigned int min, unsigned int max)
3009 {
3010 	unsigned long num;
3011 	int ret;
3012 
3013 	if (!val)
3014 		return -EINVAL;
3015 	ret = strict_strtoul(val, 0, &num);
3016 	if (ret == -EINVAL || num < min || num > max)
3017 		return -EINVAL;
3018 	*((unsigned int *)kp->arg) = num;
3019 	return 0;
3020 }
3021 
3022 static int param_set_portnr(const char *val, const struct kernel_param *kp)
3023 {
3024 	return param_set_uint_minmax(val, kp,
3025 			RPC_MIN_RESVPORT,
3026 			RPC_MAX_RESVPORT);
3027 }
3028 
3029 static struct kernel_param_ops param_ops_portnr = {
3030 	.set = param_set_portnr,
3031 	.get = param_get_uint,
3032 };
3033 
3034 #define param_check_portnr(name, p) \
3035 	__param_check(name, p, unsigned int);
3036 
3037 module_param_named(min_resvport, xprt_min_resvport, portnr, 0644);
3038 module_param_named(max_resvport, xprt_max_resvport, portnr, 0644);
3039 
3040 static int param_set_slot_table_size(const char *val,
3041 				     const struct kernel_param *kp)
3042 {
3043 	return param_set_uint_minmax(val, kp,
3044 			RPC_MIN_SLOT_TABLE,
3045 			RPC_MAX_SLOT_TABLE);
3046 }
3047 
3048 static struct kernel_param_ops param_ops_slot_table_size = {
3049 	.set = param_set_slot_table_size,
3050 	.get = param_get_uint,
3051 };
3052 
3053 #define param_check_slot_table_size(name, p) \
3054 	__param_check(name, p, unsigned int);
3055 
3056 static int param_set_max_slot_table_size(const char *val,
3057 				     const struct kernel_param *kp)
3058 {
3059 	return param_set_uint_minmax(val, kp,
3060 			RPC_MIN_SLOT_TABLE,
3061 			RPC_MAX_SLOT_TABLE_LIMIT);
3062 }
3063 
3064 static struct kernel_param_ops param_ops_max_slot_table_size = {
3065 	.set = param_set_max_slot_table_size,
3066 	.get = param_get_uint,
3067 };
3068 
3069 #define param_check_max_slot_table_size(name, p) \
3070 	__param_check(name, p, unsigned int);
3071 
3072 module_param_named(tcp_slot_table_entries, xprt_tcp_slot_table_entries,
3073 		   slot_table_size, 0644);
3074 module_param_named(tcp_max_slot_table_entries, xprt_max_tcp_slot_table_entries,
3075 		   max_slot_table_size, 0644);
3076 module_param_named(udp_slot_table_entries, xprt_udp_slot_table_entries,
3077 		   slot_table_size, 0644);
3078 
3079