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