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