xref: /openbmc/linux/net/sunrpc/xprtsock.c (revision 337cbeb2)
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 /**
1598  * xs_set_reuseaddr_port - set the socket's port and address reuse options
1599  * @sock: socket
1600  *
1601  * Note that this function has to be called on all sockets that share the
1602  * same port, and it must be called before binding.
1603  */
1604 static void xs_sock_set_reuseport(struct socket *sock)
1605 {
1606 	int opt = 1;
1607 
1608 	kernel_setsockopt(sock, SOL_SOCKET, SO_REUSEPORT,
1609 			(char *)&opt, sizeof(opt));
1610 }
1611 
1612 static unsigned short xs_sock_getport(struct socket *sock)
1613 {
1614 	struct sockaddr_storage buf;
1615 	unsigned short port = 0;
1616 
1617 	if (kernel_getsockname(sock, (struct sockaddr *)&buf) < 0)
1618 		goto out;
1619 	switch (buf.ss_family) {
1620 	case AF_INET6:
1621 		port = ntohs(((struct sockaddr_in6 *)&buf)->sin6_port);
1622 		break;
1623 	case AF_INET:
1624 		port = ntohs(((struct sockaddr_in *)&buf)->sin_port);
1625 	}
1626 out:
1627 	return port;
1628 }
1629 
1630 /**
1631  * xs_set_port - reset the port number in the remote endpoint address
1632  * @xprt: generic transport
1633  * @port: new port number
1634  *
1635  */
1636 static void xs_set_port(struct rpc_xprt *xprt, unsigned short port)
1637 {
1638 	dprintk("RPC:       setting port for xprt %p to %u\n", xprt, port);
1639 
1640 	rpc_set_port(xs_addr(xprt), port);
1641 	xs_update_peer_port(xprt);
1642 }
1643 
1644 static void xs_set_srcport(struct sock_xprt *transport, struct socket *sock)
1645 {
1646 	if (transport->srcport == 0 && transport->xprt.reuseport)
1647 		transport->srcport = xs_sock_getport(sock);
1648 }
1649 
1650 static int xs_get_srcport(struct sock_xprt *transport)
1651 {
1652 	int port = transport->srcport;
1653 
1654 	if (port == 0 && transport->xprt.resvport)
1655 		port = xs_get_random_port();
1656 	return port;
1657 }
1658 
1659 static unsigned short xs_next_srcport(struct sock_xprt *transport, unsigned short port)
1660 {
1661 	if (transport->srcport != 0)
1662 		transport->srcport = 0;
1663 	if (!transport->xprt.resvport)
1664 		return 0;
1665 	if (port <= xprt_min_resvport || port > xprt_max_resvport)
1666 		return xprt_max_resvport;
1667 	return --port;
1668 }
1669 static int xs_bind(struct sock_xprt *transport, struct socket *sock)
1670 {
1671 	struct sockaddr_storage myaddr;
1672 	int err, nloop = 0;
1673 	int port = xs_get_srcport(transport);
1674 	unsigned short last;
1675 
1676 	/*
1677 	 * If we are asking for any ephemeral port (i.e. port == 0 &&
1678 	 * transport->xprt.resvport == 0), don't bind.  Let the local
1679 	 * port selection happen implicitly when the socket is used
1680 	 * (for example at connect time).
1681 	 *
1682 	 * This ensures that we can continue to establish TCP
1683 	 * connections even when all local ephemeral ports are already
1684 	 * a part of some TCP connection.  This makes no difference
1685 	 * for UDP sockets, but also doens't harm them.
1686 	 *
1687 	 * If we're asking for any reserved port (i.e. port == 0 &&
1688 	 * transport->xprt.resvport == 1) xs_get_srcport above will
1689 	 * ensure that port is non-zero and we will bind as needed.
1690 	 */
1691 	if (port <= 0)
1692 		return port;
1693 
1694 	memcpy(&myaddr, &transport->srcaddr, transport->xprt.addrlen);
1695 	do {
1696 		rpc_set_port((struct sockaddr *)&myaddr, port);
1697 		err = kernel_bind(sock, (struct sockaddr *)&myaddr,
1698 				transport->xprt.addrlen);
1699 		if (err == 0) {
1700 			transport->srcport = port;
1701 			break;
1702 		}
1703 		last = port;
1704 		port = xs_next_srcport(transport, port);
1705 		if (port > last)
1706 			nloop++;
1707 	} while (err == -EADDRINUSE && nloop != 2);
1708 
1709 	if (myaddr.ss_family == AF_INET)
1710 		dprintk("RPC:       %s %pI4:%u: %s (%d)\n", __func__,
1711 				&((struct sockaddr_in *)&myaddr)->sin_addr,
1712 				port, err ? "failed" : "ok", err);
1713 	else
1714 		dprintk("RPC:       %s %pI6:%u: %s (%d)\n", __func__,
1715 				&((struct sockaddr_in6 *)&myaddr)->sin6_addr,
1716 				port, err ? "failed" : "ok", err);
1717 	return err;
1718 }
1719 
1720 /*
1721  * We don't support autobind on AF_LOCAL sockets
1722  */
1723 static void xs_local_rpcbind(struct rpc_task *task)
1724 {
1725 	xprt_set_bound(task->tk_xprt);
1726 }
1727 
1728 static void xs_local_set_port(struct rpc_xprt *xprt, unsigned short port)
1729 {
1730 }
1731 
1732 #ifdef CONFIG_DEBUG_LOCK_ALLOC
1733 static struct lock_class_key xs_key[2];
1734 static struct lock_class_key xs_slock_key[2];
1735 
1736 static inline void xs_reclassify_socketu(struct socket *sock)
1737 {
1738 	struct sock *sk = sock->sk;
1739 
1740 	sock_lock_init_class_and_name(sk, "slock-AF_LOCAL-RPC",
1741 		&xs_slock_key[1], "sk_lock-AF_LOCAL-RPC", &xs_key[1]);
1742 }
1743 
1744 static inline void xs_reclassify_socket4(struct socket *sock)
1745 {
1746 	struct sock *sk = sock->sk;
1747 
1748 	sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC",
1749 		&xs_slock_key[0], "sk_lock-AF_INET-RPC", &xs_key[0]);
1750 }
1751 
1752 static inline void xs_reclassify_socket6(struct socket *sock)
1753 {
1754 	struct sock *sk = sock->sk;
1755 
1756 	sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC",
1757 		&xs_slock_key[1], "sk_lock-AF_INET6-RPC", &xs_key[1]);
1758 }
1759 
1760 static inline void xs_reclassify_socket(int family, struct socket *sock)
1761 {
1762 	if (WARN_ON_ONCE(!sock_allow_reclassification(sock->sk)))
1763 		return;
1764 
1765 	switch (family) {
1766 	case AF_LOCAL:
1767 		xs_reclassify_socketu(sock);
1768 		break;
1769 	case AF_INET:
1770 		xs_reclassify_socket4(sock);
1771 		break;
1772 	case AF_INET6:
1773 		xs_reclassify_socket6(sock);
1774 		break;
1775 	}
1776 }
1777 #else
1778 static inline void xs_reclassify_socket(int family, struct socket *sock)
1779 {
1780 }
1781 #endif
1782 
1783 static void xs_dummy_setup_socket(struct work_struct *work)
1784 {
1785 }
1786 
1787 static struct socket *xs_create_sock(struct rpc_xprt *xprt,
1788 		struct sock_xprt *transport, int family, int type,
1789 		int protocol, bool reuseport)
1790 {
1791 	struct file *filp;
1792 	struct socket *sock;
1793 	int err;
1794 
1795 	err = __sock_create(xprt->xprt_net, family, type, protocol, &sock, 1);
1796 	if (err < 0) {
1797 		dprintk("RPC:       can't create %d transport socket (%d).\n",
1798 				protocol, -err);
1799 		goto out;
1800 	}
1801 	xs_reclassify_socket(family, sock);
1802 
1803 	if (reuseport)
1804 		xs_sock_set_reuseport(sock);
1805 
1806 	err = xs_bind(transport, sock);
1807 	if (err) {
1808 		sock_release(sock);
1809 		goto out;
1810 	}
1811 
1812 	filp = sock_alloc_file(sock, O_NONBLOCK, NULL);
1813 	if (IS_ERR(filp))
1814 		return ERR_CAST(filp);
1815 	transport->file = filp;
1816 
1817 	return sock;
1818 out:
1819 	return ERR_PTR(err);
1820 }
1821 
1822 static int xs_local_finish_connecting(struct rpc_xprt *xprt,
1823 				      struct socket *sock)
1824 {
1825 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
1826 									xprt);
1827 
1828 	if (!transport->inet) {
1829 		struct sock *sk = sock->sk;
1830 
1831 		write_lock_bh(&sk->sk_callback_lock);
1832 
1833 		xs_save_old_callbacks(transport, sk);
1834 
1835 		sk->sk_user_data = xprt;
1836 		sk->sk_data_ready = xs_data_ready;
1837 		sk->sk_write_space = xs_udp_write_space;
1838 		sock_set_flag(sk, SOCK_FASYNC);
1839 		sk->sk_error_report = xs_error_report;
1840 
1841 		xprt_clear_connected(xprt);
1842 
1843 		/* Reset to new socket */
1844 		transport->sock = sock;
1845 		transport->inet = sk;
1846 
1847 		write_unlock_bh(&sk->sk_callback_lock);
1848 	}
1849 
1850 	xs_stream_start_connect(transport);
1851 
1852 	return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, 0);
1853 }
1854 
1855 /**
1856  * xs_local_setup_socket - create AF_LOCAL socket, connect to a local endpoint
1857  * @transport: socket transport to connect
1858  */
1859 static int xs_local_setup_socket(struct sock_xprt *transport)
1860 {
1861 	struct rpc_xprt *xprt = &transport->xprt;
1862 	struct file *filp;
1863 	struct socket *sock;
1864 	int status;
1865 
1866 	status = __sock_create(xprt->xprt_net, AF_LOCAL,
1867 					SOCK_STREAM, 0, &sock, 1);
1868 	if (status < 0) {
1869 		dprintk("RPC:       can't create AF_LOCAL "
1870 			"transport socket (%d).\n", -status);
1871 		goto out;
1872 	}
1873 	xs_reclassify_socket(AF_LOCAL, sock);
1874 
1875 	filp = sock_alloc_file(sock, O_NONBLOCK, NULL);
1876 	if (IS_ERR(filp)) {
1877 		status = PTR_ERR(filp);
1878 		goto out;
1879 	}
1880 	transport->file = filp;
1881 
1882 	dprintk("RPC:       worker connecting xprt %p via AF_LOCAL to %s\n",
1883 			xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1884 
1885 	status = xs_local_finish_connecting(xprt, sock);
1886 	trace_rpc_socket_connect(xprt, sock, status);
1887 	switch (status) {
1888 	case 0:
1889 		dprintk("RPC:       xprt %p connected to %s\n",
1890 				xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1891 		xprt->stat.connect_count++;
1892 		xprt->stat.connect_time += (long)jiffies -
1893 					   xprt->stat.connect_start;
1894 		xprt_set_connected(xprt);
1895 	case -ENOBUFS:
1896 		break;
1897 	case -ENOENT:
1898 		dprintk("RPC:       xprt %p: socket %s does not exist\n",
1899 				xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1900 		break;
1901 	case -ECONNREFUSED:
1902 		dprintk("RPC:       xprt %p: connection refused for %s\n",
1903 				xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1904 		break;
1905 	default:
1906 		printk(KERN_ERR "%s: unhandled error (%d) connecting to %s\n",
1907 				__func__, -status,
1908 				xprt->address_strings[RPC_DISPLAY_ADDR]);
1909 	}
1910 
1911 out:
1912 	xprt_clear_connecting(xprt);
1913 	xprt_wake_pending_tasks(xprt, status);
1914 	return status;
1915 }
1916 
1917 static void xs_local_connect(struct rpc_xprt *xprt, struct rpc_task *task)
1918 {
1919 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1920 	int ret;
1921 
1922 	 if (RPC_IS_ASYNC(task)) {
1923 		/*
1924 		 * We want the AF_LOCAL connect to be resolved in the
1925 		 * filesystem namespace of the process making the rpc
1926 		 * call.  Thus we connect synchronously.
1927 		 *
1928 		 * If we want to support asynchronous AF_LOCAL calls,
1929 		 * we'll need to figure out how to pass a namespace to
1930 		 * connect.
1931 		 */
1932 		task->tk_rpc_status = -ENOTCONN;
1933 		rpc_exit(task, -ENOTCONN);
1934 		return;
1935 	}
1936 	ret = xs_local_setup_socket(transport);
1937 	if (ret && !RPC_IS_SOFTCONN(task))
1938 		msleep_interruptible(15000);
1939 }
1940 
1941 #if IS_ENABLED(CONFIG_SUNRPC_SWAP)
1942 /*
1943  * Note that this should be called with XPRT_LOCKED held (or when we otherwise
1944  * know that we have exclusive access to the socket), to guard against
1945  * races with xs_reset_transport.
1946  */
1947 static void xs_set_memalloc(struct rpc_xprt *xprt)
1948 {
1949 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
1950 			xprt);
1951 
1952 	/*
1953 	 * If there's no sock, then we have nothing to set. The
1954 	 * reconnecting process will get it for us.
1955 	 */
1956 	if (!transport->inet)
1957 		return;
1958 	if (atomic_read(&xprt->swapper))
1959 		sk_set_memalloc(transport->inet);
1960 }
1961 
1962 /**
1963  * xs_enable_swap - Tag this transport as being used for swap.
1964  * @xprt: transport to tag
1965  *
1966  * Take a reference to this transport on behalf of the rpc_clnt, and
1967  * optionally mark it for swapping if it wasn't already.
1968  */
1969 static int
1970 xs_enable_swap(struct rpc_xprt *xprt)
1971 {
1972 	struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt);
1973 
1974 	if (atomic_inc_return(&xprt->swapper) != 1)
1975 		return 0;
1976 	if (wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_KILLABLE))
1977 		return -ERESTARTSYS;
1978 	if (xs->inet)
1979 		sk_set_memalloc(xs->inet);
1980 	xprt_release_xprt(xprt, NULL);
1981 	return 0;
1982 }
1983 
1984 /**
1985  * xs_disable_swap - Untag this transport as being used for swap.
1986  * @xprt: transport to tag
1987  *
1988  * Drop a "swapper" reference to this xprt on behalf of the rpc_clnt. If the
1989  * swapper refcount goes to 0, untag the socket as a memalloc socket.
1990  */
1991 static void
1992 xs_disable_swap(struct rpc_xprt *xprt)
1993 {
1994 	struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt);
1995 
1996 	if (!atomic_dec_and_test(&xprt->swapper))
1997 		return;
1998 	if (wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_KILLABLE))
1999 		return;
2000 	if (xs->inet)
2001 		sk_clear_memalloc(xs->inet);
2002 	xprt_release_xprt(xprt, NULL);
2003 }
2004 #else
2005 static void xs_set_memalloc(struct rpc_xprt *xprt)
2006 {
2007 }
2008 
2009 static int
2010 xs_enable_swap(struct rpc_xprt *xprt)
2011 {
2012 	return -EINVAL;
2013 }
2014 
2015 static void
2016 xs_disable_swap(struct rpc_xprt *xprt)
2017 {
2018 }
2019 #endif
2020 
2021 static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
2022 {
2023 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2024 
2025 	if (!transport->inet) {
2026 		struct sock *sk = sock->sk;
2027 
2028 		write_lock_bh(&sk->sk_callback_lock);
2029 
2030 		xs_save_old_callbacks(transport, sk);
2031 
2032 		sk->sk_user_data = xprt;
2033 		sk->sk_data_ready = xs_data_ready;
2034 		sk->sk_write_space = xs_udp_write_space;
2035 		sock_set_flag(sk, SOCK_FASYNC);
2036 
2037 		xprt_set_connected(xprt);
2038 
2039 		/* Reset to new socket */
2040 		transport->sock = sock;
2041 		transport->inet = sk;
2042 
2043 		xs_set_memalloc(xprt);
2044 
2045 		write_unlock_bh(&sk->sk_callback_lock);
2046 	}
2047 	xs_udp_do_set_buffer_size(xprt);
2048 
2049 	xprt->stat.connect_start = jiffies;
2050 }
2051 
2052 static void xs_udp_setup_socket(struct work_struct *work)
2053 {
2054 	struct sock_xprt *transport =
2055 		container_of(work, struct sock_xprt, connect_worker.work);
2056 	struct rpc_xprt *xprt = &transport->xprt;
2057 	struct socket *sock;
2058 	int status = -EIO;
2059 
2060 	sock = xs_create_sock(xprt, transport,
2061 			xs_addr(xprt)->sa_family, SOCK_DGRAM,
2062 			IPPROTO_UDP, false);
2063 	if (IS_ERR(sock))
2064 		goto out;
2065 
2066 	dprintk("RPC:       worker connecting xprt %p via %s to "
2067 				"%s (port %s)\n", xprt,
2068 			xprt->address_strings[RPC_DISPLAY_PROTO],
2069 			xprt->address_strings[RPC_DISPLAY_ADDR],
2070 			xprt->address_strings[RPC_DISPLAY_PORT]);
2071 
2072 	xs_udp_finish_connecting(xprt, sock);
2073 	trace_rpc_socket_connect(xprt, sock, 0);
2074 	status = 0;
2075 out:
2076 	xprt_clear_connecting(xprt);
2077 	xprt_unlock_connect(xprt, transport);
2078 	xprt_wake_pending_tasks(xprt, status);
2079 }
2080 
2081 /**
2082  * xs_tcp_shutdown - gracefully shut down a TCP socket
2083  * @xprt: transport
2084  *
2085  * Initiates a graceful shutdown of the TCP socket by calling the
2086  * equivalent of shutdown(SHUT_RDWR);
2087  */
2088 static void xs_tcp_shutdown(struct rpc_xprt *xprt)
2089 {
2090 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2091 	struct socket *sock = transport->sock;
2092 	int skst = transport->inet ? transport->inet->sk_state : TCP_CLOSE;
2093 
2094 	if (sock == NULL)
2095 		return;
2096 	switch (skst) {
2097 	default:
2098 		kernel_sock_shutdown(sock, SHUT_RDWR);
2099 		trace_rpc_socket_shutdown(xprt, sock);
2100 		break;
2101 	case TCP_CLOSE:
2102 	case TCP_TIME_WAIT:
2103 		xs_reset_transport(transport);
2104 	}
2105 }
2106 
2107 static void xs_tcp_set_socket_timeouts(struct rpc_xprt *xprt,
2108 		struct socket *sock)
2109 {
2110 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2111 	unsigned int keepidle;
2112 	unsigned int keepcnt;
2113 	unsigned int opt_on = 1;
2114 	unsigned int timeo;
2115 
2116 	spin_lock(&xprt->transport_lock);
2117 	keepidle = DIV_ROUND_UP(xprt->timeout->to_initval, HZ);
2118 	keepcnt = xprt->timeout->to_retries + 1;
2119 	timeo = jiffies_to_msecs(xprt->timeout->to_initval) *
2120 		(xprt->timeout->to_retries + 1);
2121 	clear_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state);
2122 	spin_unlock(&xprt->transport_lock);
2123 
2124 	/* TCP Keepalive options */
2125 	kernel_setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE,
2126 			(char *)&opt_on, sizeof(opt_on));
2127 	kernel_setsockopt(sock, SOL_TCP, TCP_KEEPIDLE,
2128 			(char *)&keepidle, sizeof(keepidle));
2129 	kernel_setsockopt(sock, SOL_TCP, TCP_KEEPINTVL,
2130 			(char *)&keepidle, sizeof(keepidle));
2131 	kernel_setsockopt(sock, SOL_TCP, TCP_KEEPCNT,
2132 			(char *)&keepcnt, sizeof(keepcnt));
2133 
2134 	/* TCP user timeout (see RFC5482) */
2135 	kernel_setsockopt(sock, SOL_TCP, TCP_USER_TIMEOUT,
2136 			(char *)&timeo, sizeof(timeo));
2137 }
2138 
2139 static void xs_tcp_set_connect_timeout(struct rpc_xprt *xprt,
2140 		unsigned long connect_timeout,
2141 		unsigned long reconnect_timeout)
2142 {
2143 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2144 	struct rpc_timeout to;
2145 	unsigned long initval;
2146 
2147 	spin_lock(&xprt->transport_lock);
2148 	if (reconnect_timeout < xprt->max_reconnect_timeout)
2149 		xprt->max_reconnect_timeout = reconnect_timeout;
2150 	if (connect_timeout < xprt->connect_timeout) {
2151 		memcpy(&to, xprt->timeout, sizeof(to));
2152 		initval = DIV_ROUND_UP(connect_timeout, to.to_retries + 1);
2153 		/* Arbitrary lower limit */
2154 		if (initval <  XS_TCP_INIT_REEST_TO << 1)
2155 			initval = XS_TCP_INIT_REEST_TO << 1;
2156 		to.to_initval = initval;
2157 		to.to_maxval = initval;
2158 		memcpy(&transport->tcp_timeout, &to,
2159 				sizeof(transport->tcp_timeout));
2160 		xprt->timeout = &transport->tcp_timeout;
2161 		xprt->connect_timeout = connect_timeout;
2162 	}
2163 	set_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state);
2164 	spin_unlock(&xprt->transport_lock);
2165 }
2166 
2167 static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
2168 {
2169 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2170 	int ret = -ENOTCONN;
2171 
2172 	if (!transport->inet) {
2173 		struct sock *sk = sock->sk;
2174 		unsigned int addr_pref = IPV6_PREFER_SRC_PUBLIC;
2175 
2176 		/* Avoid temporary address, they are bad for long-lived
2177 		 * connections such as NFS mounts.
2178 		 * RFC4941, section 3.6 suggests that:
2179 		 *    Individual applications, which have specific
2180 		 *    knowledge about the normal duration of connections,
2181 		 *    MAY override this as appropriate.
2182 		 */
2183 		kernel_setsockopt(sock, SOL_IPV6, IPV6_ADDR_PREFERENCES,
2184 				(char *)&addr_pref, sizeof(addr_pref));
2185 
2186 		xs_tcp_set_socket_timeouts(xprt, sock);
2187 
2188 		write_lock_bh(&sk->sk_callback_lock);
2189 
2190 		xs_save_old_callbacks(transport, sk);
2191 
2192 		sk->sk_user_data = xprt;
2193 		sk->sk_data_ready = xs_data_ready;
2194 		sk->sk_state_change = xs_tcp_state_change;
2195 		sk->sk_write_space = xs_tcp_write_space;
2196 		sock_set_flag(sk, SOCK_FASYNC);
2197 		sk->sk_error_report = xs_error_report;
2198 
2199 		/* socket options */
2200 		sock_reset_flag(sk, SOCK_LINGER);
2201 		tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF;
2202 
2203 		xprt_clear_connected(xprt);
2204 
2205 		/* Reset to new socket */
2206 		transport->sock = sock;
2207 		transport->inet = sk;
2208 
2209 		write_unlock_bh(&sk->sk_callback_lock);
2210 	}
2211 
2212 	if (!xprt_bound(xprt))
2213 		goto out;
2214 
2215 	xs_set_memalloc(xprt);
2216 
2217 	xs_stream_start_connect(transport);
2218 
2219 	/* Tell the socket layer to start connecting... */
2220 	set_bit(XPRT_SOCK_CONNECTING, &transport->sock_state);
2221 	ret = kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK);
2222 	switch (ret) {
2223 	case 0:
2224 		xs_set_srcport(transport, sock);
2225 		/* fall through */
2226 	case -EINPROGRESS:
2227 		/* SYN_SENT! */
2228 		if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
2229 			xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2230 		break;
2231 	case -EADDRNOTAVAIL:
2232 		/* Source port number is unavailable. Try a new one! */
2233 		transport->srcport = 0;
2234 	}
2235 out:
2236 	return ret;
2237 }
2238 
2239 /**
2240  * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint
2241  * @work: queued work item
2242  *
2243  * Invoked by a work queue tasklet.
2244  */
2245 static void xs_tcp_setup_socket(struct work_struct *work)
2246 {
2247 	struct sock_xprt *transport =
2248 		container_of(work, struct sock_xprt, connect_worker.work);
2249 	struct socket *sock = transport->sock;
2250 	struct rpc_xprt *xprt = &transport->xprt;
2251 	int status = -EIO;
2252 
2253 	if (!sock) {
2254 		sock = xs_create_sock(xprt, transport,
2255 				xs_addr(xprt)->sa_family, SOCK_STREAM,
2256 				IPPROTO_TCP, true);
2257 		if (IS_ERR(sock)) {
2258 			status = PTR_ERR(sock);
2259 			goto out;
2260 		}
2261 	}
2262 
2263 	dprintk("RPC:       worker connecting xprt %p via %s to "
2264 				"%s (port %s)\n", xprt,
2265 			xprt->address_strings[RPC_DISPLAY_PROTO],
2266 			xprt->address_strings[RPC_DISPLAY_ADDR],
2267 			xprt->address_strings[RPC_DISPLAY_PORT]);
2268 
2269 	status = xs_tcp_finish_connecting(xprt, sock);
2270 	trace_rpc_socket_connect(xprt, sock, status);
2271 	dprintk("RPC:       %p connect status %d connected %d sock state %d\n",
2272 			xprt, -status, xprt_connected(xprt),
2273 			sock->sk->sk_state);
2274 	switch (status) {
2275 	default:
2276 		printk("%s: connect returned unhandled error %d\n",
2277 			__func__, status);
2278 		/* fall through */
2279 	case -EADDRNOTAVAIL:
2280 		/* We're probably in TIME_WAIT. Get rid of existing socket,
2281 		 * and retry
2282 		 */
2283 		xs_tcp_force_close(xprt);
2284 		break;
2285 	case 0:
2286 	case -EINPROGRESS:
2287 	case -EALREADY:
2288 		xprt_unlock_connect(xprt, transport);
2289 		return;
2290 	case -EINVAL:
2291 		/* Happens, for instance, if the user specified a link
2292 		 * local IPv6 address without a scope-id.
2293 		 */
2294 	case -ECONNREFUSED:
2295 	case -ECONNRESET:
2296 	case -ENETDOWN:
2297 	case -ENETUNREACH:
2298 	case -EHOSTUNREACH:
2299 	case -EADDRINUSE:
2300 	case -ENOBUFS:
2301 		/*
2302 		 * xs_tcp_force_close() wakes tasks with -EIO.
2303 		 * We need to wake them first to ensure the
2304 		 * correct error code.
2305 		 */
2306 		xprt_wake_pending_tasks(xprt, status);
2307 		xs_tcp_force_close(xprt);
2308 		goto out;
2309 	}
2310 	status = -EAGAIN;
2311 out:
2312 	xprt_clear_connecting(xprt);
2313 	xprt_unlock_connect(xprt, transport);
2314 	xprt_wake_pending_tasks(xprt, status);
2315 }
2316 
2317 /**
2318  * xs_connect - connect a socket to a remote endpoint
2319  * @xprt: pointer to transport structure
2320  * @task: address of RPC task that manages state of connect request
2321  *
2322  * TCP: If the remote end dropped the connection, delay reconnecting.
2323  *
2324  * UDP socket connects are synchronous, but we use a work queue anyway
2325  * to guarantee that even unprivileged user processes can set up a
2326  * socket on a privileged port.
2327  *
2328  * If a UDP socket connect fails, the delay behavior here prevents
2329  * retry floods (hard mounts).
2330  */
2331 static void xs_connect(struct rpc_xprt *xprt, struct rpc_task *task)
2332 {
2333 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2334 	unsigned long delay = 0;
2335 
2336 	WARN_ON_ONCE(!xprt_lock_connect(xprt, task, transport));
2337 
2338 	if (transport->sock != NULL) {
2339 		dprintk("RPC:       xs_connect delayed xprt %p for %lu "
2340 				"seconds\n",
2341 				xprt, xprt->reestablish_timeout / HZ);
2342 
2343 		/* Start by resetting any existing state */
2344 		xs_reset_transport(transport);
2345 
2346 		delay = xprt_reconnect_delay(xprt);
2347 		xprt_reconnect_backoff(xprt, XS_TCP_INIT_REEST_TO);
2348 
2349 	} else
2350 		dprintk("RPC:       xs_connect scheduled xprt %p\n", xprt);
2351 
2352 	queue_delayed_work(xprtiod_workqueue,
2353 			&transport->connect_worker,
2354 			delay);
2355 }
2356 
2357 static void xs_wake_disconnect(struct sock_xprt *transport)
2358 {
2359 	if (test_and_clear_bit(XPRT_SOCK_WAKE_DISCONNECT, &transport->sock_state))
2360 		xs_tcp_force_close(&transport->xprt);
2361 }
2362 
2363 static void xs_wake_write(struct sock_xprt *transport)
2364 {
2365 	if (test_and_clear_bit(XPRT_SOCK_WAKE_WRITE, &transport->sock_state))
2366 		xprt_write_space(&transport->xprt);
2367 }
2368 
2369 static void xs_wake_error(struct sock_xprt *transport)
2370 {
2371 	int sockerr;
2372 
2373 	if (!test_bit(XPRT_SOCK_WAKE_ERROR, &transport->sock_state))
2374 		return;
2375 	mutex_lock(&transport->recv_mutex);
2376 	if (transport->sock == NULL)
2377 		goto out;
2378 	if (!test_and_clear_bit(XPRT_SOCK_WAKE_ERROR, &transport->sock_state))
2379 		goto out;
2380 	sockerr = xchg(&transport->xprt_err, 0);
2381 	if (sockerr < 0)
2382 		xprt_wake_pending_tasks(&transport->xprt, sockerr);
2383 out:
2384 	mutex_unlock(&transport->recv_mutex);
2385 }
2386 
2387 static void xs_wake_pending(struct sock_xprt *transport)
2388 {
2389 	if (test_and_clear_bit(XPRT_SOCK_WAKE_PENDING, &transport->sock_state))
2390 		xprt_wake_pending_tasks(&transport->xprt, -EAGAIN);
2391 }
2392 
2393 static void xs_error_handle(struct work_struct *work)
2394 {
2395 	struct sock_xprt *transport = container_of(work,
2396 			struct sock_xprt, error_worker);
2397 
2398 	xs_wake_disconnect(transport);
2399 	xs_wake_write(transport);
2400 	xs_wake_error(transport);
2401 	xs_wake_pending(transport);
2402 }
2403 
2404 /**
2405  * xs_local_print_stats - display AF_LOCAL socket-specifc stats
2406  * @xprt: rpc_xprt struct containing statistics
2407  * @seq: output file
2408  *
2409  */
2410 static void xs_local_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2411 {
2412 	long idle_time = 0;
2413 
2414 	if (xprt_connected(xprt))
2415 		idle_time = (long)(jiffies - xprt->last_used) / HZ;
2416 
2417 	seq_printf(seq, "\txprt:\tlocal %lu %lu %lu %ld %lu %lu %lu "
2418 			"%llu %llu %lu %llu %llu\n",
2419 			xprt->stat.bind_count,
2420 			xprt->stat.connect_count,
2421 			xprt->stat.connect_time / HZ,
2422 			idle_time,
2423 			xprt->stat.sends,
2424 			xprt->stat.recvs,
2425 			xprt->stat.bad_xids,
2426 			xprt->stat.req_u,
2427 			xprt->stat.bklog_u,
2428 			xprt->stat.max_slots,
2429 			xprt->stat.sending_u,
2430 			xprt->stat.pending_u);
2431 }
2432 
2433 /**
2434  * xs_udp_print_stats - display UDP socket-specifc stats
2435  * @xprt: rpc_xprt struct containing statistics
2436  * @seq: output file
2437  *
2438  */
2439 static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2440 {
2441 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2442 
2443 	seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %llu %llu "
2444 			"%lu %llu %llu\n",
2445 			transport->srcport,
2446 			xprt->stat.bind_count,
2447 			xprt->stat.sends,
2448 			xprt->stat.recvs,
2449 			xprt->stat.bad_xids,
2450 			xprt->stat.req_u,
2451 			xprt->stat.bklog_u,
2452 			xprt->stat.max_slots,
2453 			xprt->stat.sending_u,
2454 			xprt->stat.pending_u);
2455 }
2456 
2457 /**
2458  * xs_tcp_print_stats - display TCP socket-specifc stats
2459  * @xprt: rpc_xprt struct containing statistics
2460  * @seq: output file
2461  *
2462  */
2463 static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2464 {
2465 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2466 	long idle_time = 0;
2467 
2468 	if (xprt_connected(xprt))
2469 		idle_time = (long)(jiffies - xprt->last_used) / HZ;
2470 
2471 	seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu "
2472 			"%llu %llu %lu %llu %llu\n",
2473 			transport->srcport,
2474 			xprt->stat.bind_count,
2475 			xprt->stat.connect_count,
2476 			xprt->stat.connect_time / HZ,
2477 			idle_time,
2478 			xprt->stat.sends,
2479 			xprt->stat.recvs,
2480 			xprt->stat.bad_xids,
2481 			xprt->stat.req_u,
2482 			xprt->stat.bklog_u,
2483 			xprt->stat.max_slots,
2484 			xprt->stat.sending_u,
2485 			xprt->stat.pending_u);
2486 }
2487 
2488 /*
2489  * Allocate a bunch of pages for a scratch buffer for the rpc code. The reason
2490  * we allocate pages instead doing a kmalloc like rpc_malloc is because we want
2491  * to use the server side send routines.
2492  */
2493 static int bc_malloc(struct rpc_task *task)
2494 {
2495 	struct rpc_rqst *rqst = task->tk_rqstp;
2496 	size_t size = rqst->rq_callsize;
2497 	struct page *page;
2498 	struct rpc_buffer *buf;
2499 
2500 	if (size > PAGE_SIZE - sizeof(struct rpc_buffer)) {
2501 		WARN_ONCE(1, "xprtsock: large bc buffer request (size %zu)\n",
2502 			  size);
2503 		return -EINVAL;
2504 	}
2505 
2506 	page = alloc_page(GFP_KERNEL);
2507 	if (!page)
2508 		return -ENOMEM;
2509 
2510 	buf = page_address(page);
2511 	buf->len = PAGE_SIZE;
2512 
2513 	rqst->rq_buffer = buf->data;
2514 	rqst->rq_rbuffer = (char *)rqst->rq_buffer + rqst->rq_callsize;
2515 	return 0;
2516 }
2517 
2518 /*
2519  * Free the space allocated in the bc_alloc routine
2520  */
2521 static void bc_free(struct rpc_task *task)
2522 {
2523 	void *buffer = task->tk_rqstp->rq_buffer;
2524 	struct rpc_buffer *buf;
2525 
2526 	buf = container_of(buffer, struct rpc_buffer, data);
2527 	free_page((unsigned long)buf);
2528 }
2529 
2530 static int bc_sendto(struct rpc_rqst *req)
2531 {
2532 	struct xdr_buf *xdr = &req->rq_snd_buf;
2533 	struct sock_xprt *transport =
2534 			container_of(req->rq_xprt, struct sock_xprt, xprt);
2535 	struct msghdr msg = {
2536 		.msg_flags	= 0,
2537 	};
2538 	rpc_fraghdr marker = cpu_to_be32(RPC_LAST_STREAM_FRAGMENT |
2539 					 (u32)xdr->len);
2540 	unsigned int sent = 0;
2541 	int err;
2542 
2543 	req->rq_xtime = ktime_get();
2544 	err = xprt_sock_sendmsg(transport->sock, &msg, xdr, 0, marker, &sent);
2545 	xdr_free_bvec(xdr);
2546 	if (err < 0 || sent != (xdr->len + sizeof(marker)))
2547 		return -EAGAIN;
2548 	return sent;
2549 }
2550 
2551 /*
2552  * The send routine. Borrows from svc_send
2553  */
2554 static int bc_send_request(struct rpc_rqst *req)
2555 {
2556 	struct svc_xprt	*xprt;
2557 	int len;
2558 
2559 	/*
2560 	 * Get the server socket associated with this callback xprt
2561 	 */
2562 	xprt = req->rq_xprt->bc_xprt;
2563 
2564 	/*
2565 	 * Grab the mutex to serialize data as the connection is shared
2566 	 * with the fore channel
2567 	 */
2568 	mutex_lock(&xprt->xpt_mutex);
2569 	if (test_bit(XPT_DEAD, &xprt->xpt_flags))
2570 		len = -ENOTCONN;
2571 	else
2572 		len = bc_sendto(req);
2573 	mutex_unlock(&xprt->xpt_mutex);
2574 
2575 	if (len > 0)
2576 		len = 0;
2577 
2578 	return len;
2579 }
2580 
2581 /*
2582  * The close routine. Since this is client initiated, we do nothing
2583  */
2584 
2585 static void bc_close(struct rpc_xprt *xprt)
2586 {
2587 	xprt_disconnect_done(xprt);
2588 }
2589 
2590 /*
2591  * The xprt destroy routine. Again, because this connection is client
2592  * initiated, we do nothing
2593  */
2594 
2595 static void bc_destroy(struct rpc_xprt *xprt)
2596 {
2597 	dprintk("RPC:       bc_destroy xprt %p\n", xprt);
2598 
2599 	xs_xprt_free(xprt);
2600 	module_put(THIS_MODULE);
2601 }
2602 
2603 static const struct rpc_xprt_ops xs_local_ops = {
2604 	.reserve_xprt		= xprt_reserve_xprt,
2605 	.release_xprt		= xprt_release_xprt,
2606 	.alloc_slot		= xprt_alloc_slot,
2607 	.free_slot		= xprt_free_slot,
2608 	.rpcbind		= xs_local_rpcbind,
2609 	.set_port		= xs_local_set_port,
2610 	.connect		= xs_local_connect,
2611 	.buf_alloc		= rpc_malloc,
2612 	.buf_free		= rpc_free,
2613 	.prepare_request	= xs_stream_prepare_request,
2614 	.send_request		= xs_local_send_request,
2615 	.wait_for_reply_request	= xprt_wait_for_reply_request_def,
2616 	.close			= xs_close,
2617 	.destroy		= xs_destroy,
2618 	.print_stats		= xs_local_print_stats,
2619 	.enable_swap		= xs_enable_swap,
2620 	.disable_swap		= xs_disable_swap,
2621 };
2622 
2623 static const struct rpc_xprt_ops xs_udp_ops = {
2624 	.set_buffer_size	= xs_udp_set_buffer_size,
2625 	.reserve_xprt		= xprt_reserve_xprt_cong,
2626 	.release_xprt		= xprt_release_xprt_cong,
2627 	.alloc_slot		= xprt_alloc_slot,
2628 	.free_slot		= xprt_free_slot,
2629 	.rpcbind		= rpcb_getport_async,
2630 	.set_port		= xs_set_port,
2631 	.connect		= xs_connect,
2632 	.buf_alloc		= rpc_malloc,
2633 	.buf_free		= rpc_free,
2634 	.send_request		= xs_udp_send_request,
2635 	.wait_for_reply_request	= xprt_wait_for_reply_request_rtt,
2636 	.timer			= xs_udp_timer,
2637 	.release_request	= xprt_release_rqst_cong,
2638 	.close			= xs_close,
2639 	.destroy		= xs_destroy,
2640 	.print_stats		= xs_udp_print_stats,
2641 	.enable_swap		= xs_enable_swap,
2642 	.disable_swap		= xs_disable_swap,
2643 	.inject_disconnect	= xs_inject_disconnect,
2644 };
2645 
2646 static const struct rpc_xprt_ops xs_tcp_ops = {
2647 	.reserve_xprt		= xprt_reserve_xprt,
2648 	.release_xprt		= xprt_release_xprt,
2649 	.alloc_slot		= xprt_alloc_slot,
2650 	.free_slot		= xprt_free_slot,
2651 	.rpcbind		= rpcb_getport_async,
2652 	.set_port		= xs_set_port,
2653 	.connect		= xs_connect,
2654 	.buf_alloc		= rpc_malloc,
2655 	.buf_free		= rpc_free,
2656 	.prepare_request	= xs_stream_prepare_request,
2657 	.send_request		= xs_tcp_send_request,
2658 	.wait_for_reply_request	= xprt_wait_for_reply_request_def,
2659 	.close			= xs_tcp_shutdown,
2660 	.destroy		= xs_destroy,
2661 	.set_connect_timeout	= xs_tcp_set_connect_timeout,
2662 	.print_stats		= xs_tcp_print_stats,
2663 	.enable_swap		= xs_enable_swap,
2664 	.disable_swap		= xs_disable_swap,
2665 	.inject_disconnect	= xs_inject_disconnect,
2666 #ifdef CONFIG_SUNRPC_BACKCHANNEL
2667 	.bc_setup		= xprt_setup_bc,
2668 	.bc_maxpayload		= xs_tcp_bc_maxpayload,
2669 	.bc_num_slots		= xprt_bc_max_slots,
2670 	.bc_free_rqst		= xprt_free_bc_rqst,
2671 	.bc_destroy		= xprt_destroy_bc,
2672 #endif
2673 };
2674 
2675 /*
2676  * The rpc_xprt_ops for the server backchannel
2677  */
2678 
2679 static const struct rpc_xprt_ops bc_tcp_ops = {
2680 	.reserve_xprt		= xprt_reserve_xprt,
2681 	.release_xprt		= xprt_release_xprt,
2682 	.alloc_slot		= xprt_alloc_slot,
2683 	.free_slot		= xprt_free_slot,
2684 	.buf_alloc		= bc_malloc,
2685 	.buf_free		= bc_free,
2686 	.send_request		= bc_send_request,
2687 	.wait_for_reply_request	= xprt_wait_for_reply_request_def,
2688 	.close			= bc_close,
2689 	.destroy		= bc_destroy,
2690 	.print_stats		= xs_tcp_print_stats,
2691 	.enable_swap		= xs_enable_swap,
2692 	.disable_swap		= xs_disable_swap,
2693 	.inject_disconnect	= xs_inject_disconnect,
2694 };
2695 
2696 static int xs_init_anyaddr(const int family, struct sockaddr *sap)
2697 {
2698 	static const struct sockaddr_in sin = {
2699 		.sin_family		= AF_INET,
2700 		.sin_addr.s_addr	= htonl(INADDR_ANY),
2701 	};
2702 	static const struct sockaddr_in6 sin6 = {
2703 		.sin6_family		= AF_INET6,
2704 		.sin6_addr		= IN6ADDR_ANY_INIT,
2705 	};
2706 
2707 	switch (family) {
2708 	case AF_LOCAL:
2709 		break;
2710 	case AF_INET:
2711 		memcpy(sap, &sin, sizeof(sin));
2712 		break;
2713 	case AF_INET6:
2714 		memcpy(sap, &sin6, sizeof(sin6));
2715 		break;
2716 	default:
2717 		dprintk("RPC:       %s: Bad address family\n", __func__);
2718 		return -EAFNOSUPPORT;
2719 	}
2720 	return 0;
2721 }
2722 
2723 static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args,
2724 				      unsigned int slot_table_size,
2725 				      unsigned int max_slot_table_size)
2726 {
2727 	struct rpc_xprt *xprt;
2728 	struct sock_xprt *new;
2729 
2730 	if (args->addrlen > sizeof(xprt->addr)) {
2731 		dprintk("RPC:       xs_setup_xprt: address too large\n");
2732 		return ERR_PTR(-EBADF);
2733 	}
2734 
2735 	xprt = xprt_alloc(args->net, sizeof(*new), slot_table_size,
2736 			max_slot_table_size);
2737 	if (xprt == NULL) {
2738 		dprintk("RPC:       xs_setup_xprt: couldn't allocate "
2739 				"rpc_xprt\n");
2740 		return ERR_PTR(-ENOMEM);
2741 	}
2742 
2743 	new = container_of(xprt, struct sock_xprt, xprt);
2744 	mutex_init(&new->recv_mutex);
2745 	memcpy(&xprt->addr, args->dstaddr, args->addrlen);
2746 	xprt->addrlen = args->addrlen;
2747 	if (args->srcaddr)
2748 		memcpy(&new->srcaddr, args->srcaddr, args->addrlen);
2749 	else {
2750 		int err;
2751 		err = xs_init_anyaddr(args->dstaddr->sa_family,
2752 					(struct sockaddr *)&new->srcaddr);
2753 		if (err != 0) {
2754 			xprt_free(xprt);
2755 			return ERR_PTR(err);
2756 		}
2757 	}
2758 
2759 	return xprt;
2760 }
2761 
2762 static const struct rpc_timeout xs_local_default_timeout = {
2763 	.to_initval = 10 * HZ,
2764 	.to_maxval = 10 * HZ,
2765 	.to_retries = 2,
2766 };
2767 
2768 /**
2769  * xs_setup_local - Set up transport to use an AF_LOCAL socket
2770  * @args: rpc transport creation arguments
2771  *
2772  * AF_LOCAL is a "tpi_cots_ord" transport, just like TCP
2773  */
2774 static struct rpc_xprt *xs_setup_local(struct xprt_create *args)
2775 {
2776 	struct sockaddr_un *sun = (struct sockaddr_un *)args->dstaddr;
2777 	struct sock_xprt *transport;
2778 	struct rpc_xprt *xprt;
2779 	struct rpc_xprt *ret;
2780 
2781 	xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
2782 			xprt_max_tcp_slot_table_entries);
2783 	if (IS_ERR(xprt))
2784 		return xprt;
2785 	transport = container_of(xprt, struct sock_xprt, xprt);
2786 
2787 	xprt->prot = 0;
2788 	xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2789 
2790 	xprt->bind_timeout = XS_BIND_TO;
2791 	xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2792 	xprt->idle_timeout = XS_IDLE_DISC_TO;
2793 
2794 	xprt->ops = &xs_local_ops;
2795 	xprt->timeout = &xs_local_default_timeout;
2796 
2797 	INIT_WORK(&transport->recv_worker, xs_stream_data_receive_workfn);
2798 	INIT_WORK(&transport->error_worker, xs_error_handle);
2799 	INIT_DELAYED_WORK(&transport->connect_worker, xs_dummy_setup_socket);
2800 
2801 	switch (sun->sun_family) {
2802 	case AF_LOCAL:
2803 		if (sun->sun_path[0] != '/') {
2804 			dprintk("RPC:       bad AF_LOCAL address: %s\n",
2805 					sun->sun_path);
2806 			ret = ERR_PTR(-EINVAL);
2807 			goto out_err;
2808 		}
2809 		xprt_set_bound(xprt);
2810 		xs_format_peer_addresses(xprt, "local", RPCBIND_NETID_LOCAL);
2811 		ret = ERR_PTR(xs_local_setup_socket(transport));
2812 		if (ret)
2813 			goto out_err;
2814 		break;
2815 	default:
2816 		ret = ERR_PTR(-EAFNOSUPPORT);
2817 		goto out_err;
2818 	}
2819 
2820 	dprintk("RPC:       set up xprt to %s via AF_LOCAL\n",
2821 			xprt->address_strings[RPC_DISPLAY_ADDR]);
2822 
2823 	if (try_module_get(THIS_MODULE))
2824 		return xprt;
2825 	ret = ERR_PTR(-EINVAL);
2826 out_err:
2827 	xs_xprt_free(xprt);
2828 	return ret;
2829 }
2830 
2831 static const struct rpc_timeout xs_udp_default_timeout = {
2832 	.to_initval = 5 * HZ,
2833 	.to_maxval = 30 * HZ,
2834 	.to_increment = 5 * HZ,
2835 	.to_retries = 5,
2836 };
2837 
2838 /**
2839  * xs_setup_udp - Set up transport to use a UDP socket
2840  * @args: rpc transport creation arguments
2841  *
2842  */
2843 static struct rpc_xprt *xs_setup_udp(struct xprt_create *args)
2844 {
2845 	struct sockaddr *addr = args->dstaddr;
2846 	struct rpc_xprt *xprt;
2847 	struct sock_xprt *transport;
2848 	struct rpc_xprt *ret;
2849 
2850 	xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries,
2851 			xprt_udp_slot_table_entries);
2852 	if (IS_ERR(xprt))
2853 		return xprt;
2854 	transport = container_of(xprt, struct sock_xprt, xprt);
2855 
2856 	xprt->prot = IPPROTO_UDP;
2857 	/* XXX: header size can vary due to auth type, IPv6, etc. */
2858 	xprt->max_payload = (1U << 16) - (MAX_HEADER << 3);
2859 
2860 	xprt->bind_timeout = XS_BIND_TO;
2861 	xprt->reestablish_timeout = XS_UDP_REEST_TO;
2862 	xprt->idle_timeout = XS_IDLE_DISC_TO;
2863 
2864 	xprt->ops = &xs_udp_ops;
2865 
2866 	xprt->timeout = &xs_udp_default_timeout;
2867 
2868 	INIT_WORK(&transport->recv_worker, xs_udp_data_receive_workfn);
2869 	INIT_WORK(&transport->error_worker, xs_error_handle);
2870 	INIT_DELAYED_WORK(&transport->connect_worker, xs_udp_setup_socket);
2871 
2872 	switch (addr->sa_family) {
2873 	case AF_INET:
2874 		if (((struct sockaddr_in *)addr)->sin_port != htons(0))
2875 			xprt_set_bound(xprt);
2876 
2877 		xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP);
2878 		break;
2879 	case AF_INET6:
2880 		if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
2881 			xprt_set_bound(xprt);
2882 
2883 		xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6);
2884 		break;
2885 	default:
2886 		ret = ERR_PTR(-EAFNOSUPPORT);
2887 		goto out_err;
2888 	}
2889 
2890 	if (xprt_bound(xprt))
2891 		dprintk("RPC:       set up xprt to %s (port %s) via %s\n",
2892 				xprt->address_strings[RPC_DISPLAY_ADDR],
2893 				xprt->address_strings[RPC_DISPLAY_PORT],
2894 				xprt->address_strings[RPC_DISPLAY_PROTO]);
2895 	else
2896 		dprintk("RPC:       set up xprt to %s (autobind) via %s\n",
2897 				xprt->address_strings[RPC_DISPLAY_ADDR],
2898 				xprt->address_strings[RPC_DISPLAY_PROTO]);
2899 
2900 	if (try_module_get(THIS_MODULE))
2901 		return xprt;
2902 	ret = ERR_PTR(-EINVAL);
2903 out_err:
2904 	xs_xprt_free(xprt);
2905 	return ret;
2906 }
2907 
2908 static const struct rpc_timeout xs_tcp_default_timeout = {
2909 	.to_initval = 60 * HZ,
2910 	.to_maxval = 60 * HZ,
2911 	.to_retries = 2,
2912 };
2913 
2914 /**
2915  * xs_setup_tcp - Set up transport to use a TCP socket
2916  * @args: rpc transport creation arguments
2917  *
2918  */
2919 static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args)
2920 {
2921 	struct sockaddr *addr = args->dstaddr;
2922 	struct rpc_xprt *xprt;
2923 	struct sock_xprt *transport;
2924 	struct rpc_xprt *ret;
2925 	unsigned int max_slot_table_size = xprt_max_tcp_slot_table_entries;
2926 
2927 	if (args->flags & XPRT_CREATE_INFINITE_SLOTS)
2928 		max_slot_table_size = RPC_MAX_SLOT_TABLE_LIMIT;
2929 
2930 	xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
2931 			max_slot_table_size);
2932 	if (IS_ERR(xprt))
2933 		return xprt;
2934 	transport = container_of(xprt, struct sock_xprt, xprt);
2935 
2936 	xprt->prot = IPPROTO_TCP;
2937 	xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2938 
2939 	xprt->bind_timeout = XS_BIND_TO;
2940 	xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2941 	xprt->idle_timeout = XS_IDLE_DISC_TO;
2942 
2943 	xprt->ops = &xs_tcp_ops;
2944 	xprt->timeout = &xs_tcp_default_timeout;
2945 
2946 	xprt->max_reconnect_timeout = xprt->timeout->to_maxval;
2947 	xprt->connect_timeout = xprt->timeout->to_initval *
2948 		(xprt->timeout->to_retries + 1);
2949 
2950 	INIT_WORK(&transport->recv_worker, xs_stream_data_receive_workfn);
2951 	INIT_WORK(&transport->error_worker, xs_error_handle);
2952 	INIT_DELAYED_WORK(&transport->connect_worker, xs_tcp_setup_socket);
2953 
2954 	switch (addr->sa_family) {
2955 	case AF_INET:
2956 		if (((struct sockaddr_in *)addr)->sin_port != htons(0))
2957 			xprt_set_bound(xprt);
2958 
2959 		xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP);
2960 		break;
2961 	case AF_INET6:
2962 		if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
2963 			xprt_set_bound(xprt);
2964 
2965 		xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6);
2966 		break;
2967 	default:
2968 		ret = ERR_PTR(-EAFNOSUPPORT);
2969 		goto out_err;
2970 	}
2971 
2972 	if (xprt_bound(xprt))
2973 		dprintk("RPC:       set up xprt to %s (port %s) via %s\n",
2974 				xprt->address_strings[RPC_DISPLAY_ADDR],
2975 				xprt->address_strings[RPC_DISPLAY_PORT],
2976 				xprt->address_strings[RPC_DISPLAY_PROTO]);
2977 	else
2978 		dprintk("RPC:       set up xprt to %s (autobind) via %s\n",
2979 				xprt->address_strings[RPC_DISPLAY_ADDR],
2980 				xprt->address_strings[RPC_DISPLAY_PROTO]);
2981 
2982 	if (try_module_get(THIS_MODULE))
2983 		return xprt;
2984 	ret = ERR_PTR(-EINVAL);
2985 out_err:
2986 	xs_xprt_free(xprt);
2987 	return ret;
2988 }
2989 
2990 /**
2991  * xs_setup_bc_tcp - Set up transport to use a TCP backchannel socket
2992  * @args: rpc transport creation arguments
2993  *
2994  */
2995 static struct rpc_xprt *xs_setup_bc_tcp(struct xprt_create *args)
2996 {
2997 	struct sockaddr *addr = args->dstaddr;
2998 	struct rpc_xprt *xprt;
2999 	struct sock_xprt *transport;
3000 	struct svc_sock *bc_sock;
3001 	struct rpc_xprt *ret;
3002 
3003 	xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
3004 			xprt_tcp_slot_table_entries);
3005 	if (IS_ERR(xprt))
3006 		return xprt;
3007 	transport = container_of(xprt, struct sock_xprt, xprt);
3008 
3009 	xprt->prot = IPPROTO_TCP;
3010 	xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
3011 	xprt->timeout = &xs_tcp_default_timeout;
3012 
3013 	/* backchannel */
3014 	xprt_set_bound(xprt);
3015 	xprt->bind_timeout = 0;
3016 	xprt->reestablish_timeout = 0;
3017 	xprt->idle_timeout = 0;
3018 
3019 	xprt->ops = &bc_tcp_ops;
3020 
3021 	switch (addr->sa_family) {
3022 	case AF_INET:
3023 		xs_format_peer_addresses(xprt, "tcp",
3024 					 RPCBIND_NETID_TCP);
3025 		break;
3026 	case AF_INET6:
3027 		xs_format_peer_addresses(xprt, "tcp",
3028 				   RPCBIND_NETID_TCP6);
3029 		break;
3030 	default:
3031 		ret = ERR_PTR(-EAFNOSUPPORT);
3032 		goto out_err;
3033 	}
3034 
3035 	dprintk("RPC:       set up xprt to %s (port %s) via %s\n",
3036 			xprt->address_strings[RPC_DISPLAY_ADDR],
3037 			xprt->address_strings[RPC_DISPLAY_PORT],
3038 			xprt->address_strings[RPC_DISPLAY_PROTO]);
3039 
3040 	/*
3041 	 * Once we've associated a backchannel xprt with a connection,
3042 	 * we want to keep it around as long as the connection lasts,
3043 	 * in case we need to start using it for a backchannel again;
3044 	 * this reference won't be dropped until bc_xprt is destroyed.
3045 	 */
3046 	xprt_get(xprt);
3047 	args->bc_xprt->xpt_bc_xprt = xprt;
3048 	xprt->bc_xprt = args->bc_xprt;
3049 	bc_sock = container_of(args->bc_xprt, struct svc_sock, sk_xprt);
3050 	transport->sock = bc_sock->sk_sock;
3051 	transport->inet = bc_sock->sk_sk;
3052 
3053 	/*
3054 	 * Since we don't want connections for the backchannel, we set
3055 	 * the xprt status to connected
3056 	 */
3057 	xprt_set_connected(xprt);
3058 
3059 	if (try_module_get(THIS_MODULE))
3060 		return xprt;
3061 
3062 	args->bc_xprt->xpt_bc_xprt = NULL;
3063 	args->bc_xprt->xpt_bc_xps = NULL;
3064 	xprt_put(xprt);
3065 	ret = ERR_PTR(-EINVAL);
3066 out_err:
3067 	xs_xprt_free(xprt);
3068 	return ret;
3069 }
3070 
3071 static struct xprt_class	xs_local_transport = {
3072 	.list		= LIST_HEAD_INIT(xs_local_transport.list),
3073 	.name		= "named UNIX socket",
3074 	.owner		= THIS_MODULE,
3075 	.ident		= XPRT_TRANSPORT_LOCAL,
3076 	.setup		= xs_setup_local,
3077 };
3078 
3079 static struct xprt_class	xs_udp_transport = {
3080 	.list		= LIST_HEAD_INIT(xs_udp_transport.list),
3081 	.name		= "udp",
3082 	.owner		= THIS_MODULE,
3083 	.ident		= XPRT_TRANSPORT_UDP,
3084 	.setup		= xs_setup_udp,
3085 };
3086 
3087 static struct xprt_class	xs_tcp_transport = {
3088 	.list		= LIST_HEAD_INIT(xs_tcp_transport.list),
3089 	.name		= "tcp",
3090 	.owner		= THIS_MODULE,
3091 	.ident		= XPRT_TRANSPORT_TCP,
3092 	.setup		= xs_setup_tcp,
3093 };
3094 
3095 static struct xprt_class	xs_bc_tcp_transport = {
3096 	.list		= LIST_HEAD_INIT(xs_bc_tcp_transport.list),
3097 	.name		= "tcp NFSv4.1 backchannel",
3098 	.owner		= THIS_MODULE,
3099 	.ident		= XPRT_TRANSPORT_BC_TCP,
3100 	.setup		= xs_setup_bc_tcp,
3101 };
3102 
3103 /**
3104  * init_socket_xprt - set up xprtsock's sysctls, register with RPC client
3105  *
3106  */
3107 int init_socket_xprt(void)
3108 {
3109 	if (!sunrpc_table_header)
3110 		sunrpc_table_header = register_sysctl_table(sunrpc_table);
3111 
3112 	xprt_register_transport(&xs_local_transport);
3113 	xprt_register_transport(&xs_udp_transport);
3114 	xprt_register_transport(&xs_tcp_transport);
3115 	xprt_register_transport(&xs_bc_tcp_transport);
3116 
3117 	return 0;
3118 }
3119 
3120 /**
3121  * cleanup_socket_xprt - remove xprtsock's sysctls, unregister
3122  *
3123  */
3124 void cleanup_socket_xprt(void)
3125 {
3126 	if (sunrpc_table_header) {
3127 		unregister_sysctl_table(sunrpc_table_header);
3128 		sunrpc_table_header = NULL;
3129 	}
3130 
3131 	xprt_unregister_transport(&xs_local_transport);
3132 	xprt_unregister_transport(&xs_udp_transport);
3133 	xprt_unregister_transport(&xs_tcp_transport);
3134 	xprt_unregister_transport(&xs_bc_tcp_transport);
3135 }
3136 
3137 static int param_set_uint_minmax(const char *val,
3138 		const struct kernel_param *kp,
3139 		unsigned int min, unsigned int max)
3140 {
3141 	unsigned int num;
3142 	int ret;
3143 
3144 	if (!val)
3145 		return -EINVAL;
3146 	ret = kstrtouint(val, 0, &num);
3147 	if (ret)
3148 		return ret;
3149 	if (num < min || num > max)
3150 		return -EINVAL;
3151 	*((unsigned int *)kp->arg) = num;
3152 	return 0;
3153 }
3154 
3155 static int param_set_portnr(const char *val, const struct kernel_param *kp)
3156 {
3157 	return param_set_uint_minmax(val, kp,
3158 			RPC_MIN_RESVPORT,
3159 			RPC_MAX_RESVPORT);
3160 }
3161 
3162 static const struct kernel_param_ops param_ops_portnr = {
3163 	.set = param_set_portnr,
3164 	.get = param_get_uint,
3165 };
3166 
3167 #define param_check_portnr(name, p) \
3168 	__param_check(name, p, unsigned int);
3169 
3170 module_param_named(min_resvport, xprt_min_resvport, portnr, 0644);
3171 module_param_named(max_resvport, xprt_max_resvport, portnr, 0644);
3172 
3173 static int param_set_slot_table_size(const char *val,
3174 				     const struct kernel_param *kp)
3175 {
3176 	return param_set_uint_minmax(val, kp,
3177 			RPC_MIN_SLOT_TABLE,
3178 			RPC_MAX_SLOT_TABLE);
3179 }
3180 
3181 static const struct kernel_param_ops param_ops_slot_table_size = {
3182 	.set = param_set_slot_table_size,
3183 	.get = param_get_uint,
3184 };
3185 
3186 #define param_check_slot_table_size(name, p) \
3187 	__param_check(name, p, unsigned int);
3188 
3189 static int param_set_max_slot_table_size(const char *val,
3190 				     const struct kernel_param *kp)
3191 {
3192 	return param_set_uint_minmax(val, kp,
3193 			RPC_MIN_SLOT_TABLE,
3194 			RPC_MAX_SLOT_TABLE_LIMIT);
3195 }
3196 
3197 static const struct kernel_param_ops param_ops_max_slot_table_size = {
3198 	.set = param_set_max_slot_table_size,
3199 	.get = param_get_uint,
3200 };
3201 
3202 #define param_check_max_slot_table_size(name, p) \
3203 	__param_check(name, p, unsigned int);
3204 
3205 module_param_named(tcp_slot_table_entries, xprt_tcp_slot_table_entries,
3206 		   slot_table_size, 0644);
3207 module_param_named(tcp_max_slot_table_entries, xprt_max_tcp_slot_table_entries,
3208 		   max_slot_table_size, 0644);
3209 module_param_named(udp_slot_table_entries, xprt_udp_slot_table_entries,
3210 		   slot_table_size, 0644);
3211