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