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