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