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