xref: /openbmc/linux/net/sunrpc/xprtsock.c (revision 4f89e4b8)
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(&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(&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 	xdr_free_bvec(&req->rq_rcv_buf);
913 	req->rq_task->tk_status = xdr_alloc_bvec(&req->rq_rcv_buf, GFP_KERNEL);
914 }
915 
916 /*
917  * Determine if the previous message in the stream was aborted before it
918  * could complete transmission.
919  */
920 static bool
921 xs_send_request_was_aborted(struct sock_xprt *transport, struct rpc_rqst *req)
922 {
923 	return transport->xmit.offset != 0 && req->rq_bytes_sent == 0;
924 }
925 
926 /*
927  * Return the stream record marker field for a record of length < 2^31-1
928  */
929 static rpc_fraghdr
930 xs_stream_record_marker(struct xdr_buf *xdr)
931 {
932 	if (!xdr->len)
933 		return 0;
934 	return cpu_to_be32(RPC_LAST_STREAM_FRAGMENT | (u32)xdr->len);
935 }
936 
937 /**
938  * xs_local_send_request - write an RPC request to an AF_LOCAL socket
939  * @req: pointer to RPC request
940  *
941  * Return values:
942  *        0:	The request has been sent
943  *   EAGAIN:	The socket was blocked, please call again later to
944  *		complete the request
945  * ENOTCONN:	Caller needs to invoke connect logic then call again
946  *    other:	Some other error occured, the request was not sent
947  */
948 static int xs_local_send_request(struct rpc_rqst *req)
949 {
950 	struct rpc_xprt *xprt = req->rq_xprt;
951 	struct sock_xprt *transport =
952 				container_of(xprt, struct sock_xprt, xprt);
953 	struct xdr_buf *xdr = &req->rq_snd_buf;
954 	rpc_fraghdr rm = xs_stream_record_marker(xdr);
955 	unsigned int msglen = rm ? req->rq_slen + sizeof(rm) : req->rq_slen;
956 	int status;
957 	int sent = 0;
958 
959 	/* Close the stream if the previous transmission was incomplete */
960 	if (xs_send_request_was_aborted(transport, req)) {
961 		xs_close(xprt);
962 		return -ENOTCONN;
963 	}
964 
965 	xs_pktdump("packet data:",
966 			req->rq_svec->iov_base, req->rq_svec->iov_len);
967 
968 	req->rq_xtime = ktime_get();
969 	status = xs_sendpages(transport->sock, NULL, 0, xdr,
970 			      transport->xmit.offset, rm, &sent);
971 	dprintk("RPC:       %s(%u) = %d\n",
972 			__func__, xdr->len - transport->xmit.offset, status);
973 
974 	if (status == -EAGAIN && sock_writeable(transport->inet))
975 		status = -ENOBUFS;
976 
977 	if (likely(sent > 0) || status == 0) {
978 		transport->xmit.offset += sent;
979 		req->rq_bytes_sent = transport->xmit.offset;
980 		if (likely(req->rq_bytes_sent >= msglen)) {
981 			req->rq_xmit_bytes_sent += transport->xmit.offset;
982 			transport->xmit.offset = 0;
983 			return 0;
984 		}
985 		status = -EAGAIN;
986 	}
987 
988 	switch (status) {
989 	case -ENOBUFS:
990 		break;
991 	case -EAGAIN:
992 		status = xs_nospace(req);
993 		break;
994 	default:
995 		dprintk("RPC:       sendmsg returned unrecognized error %d\n",
996 			-status);
997 		/* fall through */
998 	case -EPIPE:
999 		xs_close(xprt);
1000 		status = -ENOTCONN;
1001 	}
1002 
1003 	return status;
1004 }
1005 
1006 /**
1007  * xs_udp_send_request - write an RPC request to a UDP socket
1008  * @req: pointer to RPC request
1009  *
1010  * Return values:
1011  *        0:	The request has been sent
1012  *   EAGAIN:	The socket was blocked, please call again later to
1013  *		complete the request
1014  * ENOTCONN:	Caller needs to invoke connect logic then call again
1015  *    other:	Some other error occurred, the request was not sent
1016  */
1017 static int xs_udp_send_request(struct rpc_rqst *req)
1018 {
1019 	struct rpc_xprt *xprt = req->rq_xprt;
1020 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1021 	struct xdr_buf *xdr = &req->rq_snd_buf;
1022 	int sent = 0;
1023 	int status;
1024 
1025 	xs_pktdump("packet data:",
1026 				req->rq_svec->iov_base,
1027 				req->rq_svec->iov_len);
1028 
1029 	if (!xprt_bound(xprt))
1030 		return -ENOTCONN;
1031 
1032 	if (!xprt_request_get_cong(xprt, req))
1033 		return -EBADSLT;
1034 
1035 	req->rq_xtime = ktime_get();
1036 	status = xs_sendpages(transport->sock, xs_addr(xprt), xprt->addrlen,
1037 			      xdr, 0, 0, &sent);
1038 
1039 	dprintk("RPC:       xs_udp_send_request(%u) = %d\n",
1040 			xdr->len, status);
1041 
1042 	/* firewall is blocking us, don't return -EAGAIN or we end up looping */
1043 	if (status == -EPERM)
1044 		goto process_status;
1045 
1046 	if (status == -EAGAIN && sock_writeable(transport->inet))
1047 		status = -ENOBUFS;
1048 
1049 	if (sent > 0 || status == 0) {
1050 		req->rq_xmit_bytes_sent += sent;
1051 		if (sent >= req->rq_slen)
1052 			return 0;
1053 		/* Still some bytes left; set up for a retry later. */
1054 		status = -EAGAIN;
1055 	}
1056 
1057 process_status:
1058 	switch (status) {
1059 	case -ENOTSOCK:
1060 		status = -ENOTCONN;
1061 		/* Should we call xs_close() here? */
1062 		break;
1063 	case -EAGAIN:
1064 		status = xs_nospace(req);
1065 		break;
1066 	case -ENETUNREACH:
1067 	case -ENOBUFS:
1068 	case -EPIPE:
1069 	case -ECONNREFUSED:
1070 	case -EPERM:
1071 		/* When the server has died, an ICMP port unreachable message
1072 		 * prompts ECONNREFUSED. */
1073 		break;
1074 	default:
1075 		dprintk("RPC:       sendmsg returned unrecognized error %d\n",
1076 			-status);
1077 	}
1078 
1079 	return status;
1080 }
1081 
1082 /**
1083  * xs_tcp_send_request - write an RPC request to a TCP socket
1084  * @req: pointer to RPC request
1085  *
1086  * Return values:
1087  *        0:	The request has been sent
1088  *   EAGAIN:	The socket was blocked, please call again later to
1089  *		complete the request
1090  * ENOTCONN:	Caller needs to invoke connect logic then call again
1091  *    other:	Some other error occurred, the request was not sent
1092  *
1093  * XXX: In the case of soft timeouts, should we eventually give up
1094  *	if sendmsg is not able to make progress?
1095  */
1096 static int xs_tcp_send_request(struct rpc_rqst *req)
1097 {
1098 	struct rpc_xprt *xprt = req->rq_xprt;
1099 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1100 	struct xdr_buf *xdr = &req->rq_snd_buf;
1101 	rpc_fraghdr rm = xs_stream_record_marker(xdr);
1102 	unsigned int msglen = rm ? req->rq_slen + sizeof(rm) : req->rq_slen;
1103 	bool vm_wait = false;
1104 	int status;
1105 	int sent;
1106 
1107 	/* Close the stream if the previous transmission was incomplete */
1108 	if (xs_send_request_was_aborted(transport, req)) {
1109 		if (transport->sock != NULL)
1110 			kernel_sock_shutdown(transport->sock, SHUT_RDWR);
1111 		return -ENOTCONN;
1112 	}
1113 
1114 	xs_pktdump("packet data:",
1115 				req->rq_svec->iov_base,
1116 				req->rq_svec->iov_len);
1117 
1118 	if (test_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state))
1119 		xs_tcp_set_socket_timeouts(xprt, transport->sock);
1120 
1121 	/* Continue transmitting the packet/record. We must be careful
1122 	 * to cope with writespace callbacks arriving _after_ we have
1123 	 * called sendmsg(). */
1124 	req->rq_xtime = ktime_get();
1125 	while (1) {
1126 		sent = 0;
1127 		status = xs_sendpages(transport->sock, NULL, 0, xdr,
1128 				      transport->xmit.offset, rm, &sent);
1129 
1130 		dprintk("RPC:       xs_tcp_send_request(%u) = %d\n",
1131 				xdr->len - transport->xmit.offset, status);
1132 
1133 		/* If we've sent the entire packet, immediately
1134 		 * reset the count of bytes sent. */
1135 		transport->xmit.offset += sent;
1136 		req->rq_bytes_sent = transport->xmit.offset;
1137 		if (likely(req->rq_bytes_sent >= msglen)) {
1138 			req->rq_xmit_bytes_sent += transport->xmit.offset;
1139 			transport->xmit.offset = 0;
1140 			return 0;
1141 		}
1142 
1143 		WARN_ON_ONCE(sent == 0 && status == 0);
1144 
1145 		if (status == -EAGAIN ) {
1146 			/*
1147 			 * Return EAGAIN if we're sure we're hitting the
1148 			 * socket send buffer limits.
1149 			 */
1150 			if (test_bit(SOCK_NOSPACE, &transport->sock->flags))
1151 				break;
1152 			/*
1153 			 * Did we hit a memory allocation failure?
1154 			 */
1155 			if (sent == 0) {
1156 				status = -ENOBUFS;
1157 				if (vm_wait)
1158 					break;
1159 				/* Retry, knowing now that we're below the
1160 				 * socket send buffer limit
1161 				 */
1162 				vm_wait = true;
1163 			}
1164 			continue;
1165 		}
1166 		if (status < 0)
1167 			break;
1168 		vm_wait = false;
1169 	}
1170 
1171 	switch (status) {
1172 	case -ENOTSOCK:
1173 		status = -ENOTCONN;
1174 		/* Should we call xs_close() here? */
1175 		break;
1176 	case -EAGAIN:
1177 		status = xs_nospace(req);
1178 		break;
1179 	case -ECONNRESET:
1180 	case -ECONNREFUSED:
1181 	case -ENOTCONN:
1182 	case -EADDRINUSE:
1183 	case -ENOBUFS:
1184 	case -EPIPE:
1185 		break;
1186 	default:
1187 		dprintk("RPC:       sendmsg returned unrecognized error %d\n",
1188 			-status);
1189 	}
1190 
1191 	return status;
1192 }
1193 
1194 static void xs_save_old_callbacks(struct sock_xprt *transport, struct sock *sk)
1195 {
1196 	transport->old_data_ready = sk->sk_data_ready;
1197 	transport->old_state_change = sk->sk_state_change;
1198 	transport->old_write_space = sk->sk_write_space;
1199 	transport->old_error_report = sk->sk_error_report;
1200 }
1201 
1202 static void xs_restore_old_callbacks(struct sock_xprt *transport, struct sock *sk)
1203 {
1204 	sk->sk_data_ready = transport->old_data_ready;
1205 	sk->sk_state_change = transport->old_state_change;
1206 	sk->sk_write_space = transport->old_write_space;
1207 	sk->sk_error_report = transport->old_error_report;
1208 }
1209 
1210 static void xs_sock_reset_state_flags(struct rpc_xprt *xprt)
1211 {
1212 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1213 
1214 	clear_bit(XPRT_SOCK_DATA_READY, &transport->sock_state);
1215 	clear_bit(XPRT_SOCK_WAKE_ERROR, &transport->sock_state);
1216 	clear_bit(XPRT_SOCK_WAKE_WRITE, &transport->sock_state);
1217 	clear_bit(XPRT_SOCK_WAKE_DISCONNECT, &transport->sock_state);
1218 }
1219 
1220 static void xs_run_error_worker(struct sock_xprt *transport, unsigned int nr)
1221 {
1222 	set_bit(nr, &transport->sock_state);
1223 	queue_work(xprtiod_workqueue, &transport->error_worker);
1224 }
1225 
1226 static void xs_sock_reset_connection_flags(struct rpc_xprt *xprt)
1227 {
1228 	smp_mb__before_atomic();
1229 	clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
1230 	clear_bit(XPRT_CLOSING, &xprt->state);
1231 	xs_sock_reset_state_flags(xprt);
1232 	smp_mb__after_atomic();
1233 }
1234 
1235 /**
1236  * xs_error_report - callback to handle TCP socket state errors
1237  * @sk: socket
1238  *
1239  * Note: we don't call sock_error() since there may be a rpc_task
1240  * using the socket, and so we don't want to clear sk->sk_err.
1241  */
1242 static void xs_error_report(struct sock *sk)
1243 {
1244 	struct sock_xprt *transport;
1245 	struct rpc_xprt *xprt;
1246 	int err;
1247 
1248 	read_lock_bh(&sk->sk_callback_lock);
1249 	if (!(xprt = xprt_from_sock(sk)))
1250 		goto out;
1251 
1252 	transport = container_of(xprt, struct sock_xprt, xprt);
1253 	err = -sk->sk_err;
1254 	if (err == 0)
1255 		goto out;
1256 	dprintk("RPC:       xs_error_report client %p, error=%d...\n",
1257 			xprt, -err);
1258 	trace_rpc_socket_error(xprt, sk->sk_socket, err);
1259 	xs_run_error_worker(transport, XPRT_SOCK_WAKE_ERROR);
1260  out:
1261 	read_unlock_bh(&sk->sk_callback_lock);
1262 }
1263 
1264 static void xs_reset_transport(struct sock_xprt *transport)
1265 {
1266 	struct socket *sock = transport->sock;
1267 	struct sock *sk = transport->inet;
1268 	struct rpc_xprt *xprt = &transport->xprt;
1269 	struct file *filp = transport->file;
1270 
1271 	if (sk == NULL)
1272 		return;
1273 
1274 	if (atomic_read(&transport->xprt.swapper))
1275 		sk_clear_memalloc(sk);
1276 
1277 	kernel_sock_shutdown(sock, SHUT_RDWR);
1278 
1279 	mutex_lock(&transport->recv_mutex);
1280 	write_lock_bh(&sk->sk_callback_lock);
1281 	transport->inet = NULL;
1282 	transport->sock = NULL;
1283 	transport->file = NULL;
1284 
1285 	sk->sk_user_data = NULL;
1286 
1287 	xs_restore_old_callbacks(transport, sk);
1288 	xprt_clear_connected(xprt);
1289 	write_unlock_bh(&sk->sk_callback_lock);
1290 	xs_sock_reset_connection_flags(xprt);
1291 	/* Reset stream record info */
1292 	xs_stream_reset_connect(transport);
1293 	mutex_unlock(&transport->recv_mutex);
1294 
1295 	trace_rpc_socket_close(xprt, sock);
1296 	fput(filp);
1297 
1298 	xprt_disconnect_done(xprt);
1299 }
1300 
1301 /**
1302  * xs_close - close a socket
1303  * @xprt: transport
1304  *
1305  * This is used when all requests are complete; ie, no DRC state remains
1306  * on the server we want to save.
1307  *
1308  * The caller _must_ be holding XPRT_LOCKED in order to avoid issues with
1309  * xs_reset_transport() zeroing the socket from underneath a writer.
1310  */
1311 static void xs_close(struct rpc_xprt *xprt)
1312 {
1313 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1314 
1315 	dprintk("RPC:       xs_close xprt %p\n", xprt);
1316 
1317 	xs_reset_transport(transport);
1318 	xprt->reestablish_timeout = 0;
1319 }
1320 
1321 static void xs_inject_disconnect(struct rpc_xprt *xprt)
1322 {
1323 	dprintk("RPC:       injecting transport disconnect on xprt=%p\n",
1324 		xprt);
1325 	xprt_disconnect_done(xprt);
1326 }
1327 
1328 static void xs_xprt_free(struct rpc_xprt *xprt)
1329 {
1330 	xs_free_peer_addresses(xprt);
1331 	xprt_free(xprt);
1332 }
1333 
1334 /**
1335  * xs_destroy - prepare to shutdown a transport
1336  * @xprt: doomed transport
1337  *
1338  */
1339 static void xs_destroy(struct rpc_xprt *xprt)
1340 {
1341 	struct sock_xprt *transport = container_of(xprt,
1342 			struct sock_xprt, xprt);
1343 	dprintk("RPC:       xs_destroy xprt %p\n", xprt);
1344 
1345 	cancel_delayed_work_sync(&transport->connect_worker);
1346 	xs_close(xprt);
1347 	cancel_work_sync(&transport->recv_worker);
1348 	cancel_work_sync(&transport->error_worker);
1349 	xs_xprt_free(xprt);
1350 	module_put(THIS_MODULE);
1351 }
1352 
1353 /**
1354  * xs_udp_data_read_skb - receive callback for UDP sockets
1355  * @xprt: transport
1356  * @sk: socket
1357  * @skb: skbuff
1358  *
1359  */
1360 static void xs_udp_data_read_skb(struct rpc_xprt *xprt,
1361 		struct sock *sk,
1362 		struct sk_buff *skb)
1363 {
1364 	struct rpc_task *task;
1365 	struct rpc_rqst *rovr;
1366 	int repsize, copied;
1367 	u32 _xid;
1368 	__be32 *xp;
1369 
1370 	repsize = skb->len;
1371 	if (repsize < 4) {
1372 		dprintk("RPC:       impossible RPC reply size %d!\n", repsize);
1373 		return;
1374 	}
1375 
1376 	/* Copy the XID from the skb... */
1377 	xp = skb_header_pointer(skb, 0, sizeof(_xid), &_xid);
1378 	if (xp == NULL)
1379 		return;
1380 
1381 	/* Look up and lock the request corresponding to the given XID */
1382 	spin_lock(&xprt->queue_lock);
1383 	rovr = xprt_lookup_rqst(xprt, *xp);
1384 	if (!rovr)
1385 		goto out_unlock;
1386 	xprt_pin_rqst(rovr);
1387 	xprt_update_rtt(rovr->rq_task);
1388 	spin_unlock(&xprt->queue_lock);
1389 	task = rovr->rq_task;
1390 
1391 	if ((copied = rovr->rq_private_buf.buflen) > repsize)
1392 		copied = repsize;
1393 
1394 	/* Suck it into the iovec, verify checksum if not done by hw. */
1395 	if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) {
1396 		spin_lock(&xprt->queue_lock);
1397 		__UDPX_INC_STATS(sk, UDP_MIB_INERRORS);
1398 		goto out_unpin;
1399 	}
1400 
1401 
1402 	spin_lock(&xprt->transport_lock);
1403 	xprt_adjust_cwnd(xprt, task, copied);
1404 	spin_unlock(&xprt->transport_lock);
1405 	spin_lock(&xprt->queue_lock);
1406 	xprt_complete_rqst(task, copied);
1407 	__UDPX_INC_STATS(sk, UDP_MIB_INDATAGRAMS);
1408 out_unpin:
1409 	xprt_unpin_rqst(rovr);
1410  out_unlock:
1411 	spin_unlock(&xprt->queue_lock);
1412 }
1413 
1414 static void xs_udp_data_receive(struct sock_xprt *transport)
1415 {
1416 	struct sk_buff *skb;
1417 	struct sock *sk;
1418 	int err;
1419 
1420 	mutex_lock(&transport->recv_mutex);
1421 	sk = transport->inet;
1422 	if (sk == NULL)
1423 		goto out;
1424 	for (;;) {
1425 		skb = skb_recv_udp(sk, 0, 1, &err);
1426 		if (skb == NULL)
1427 			break;
1428 		xs_udp_data_read_skb(&transport->xprt, sk, skb);
1429 		consume_skb(skb);
1430 		cond_resched();
1431 	}
1432 	xs_poll_check_readable(transport);
1433 out:
1434 	mutex_unlock(&transport->recv_mutex);
1435 }
1436 
1437 static void xs_udp_data_receive_workfn(struct work_struct *work)
1438 {
1439 	struct sock_xprt *transport =
1440 		container_of(work, struct sock_xprt, recv_worker);
1441 	unsigned int pflags = memalloc_nofs_save();
1442 
1443 	xs_udp_data_receive(transport);
1444 	memalloc_nofs_restore(pflags);
1445 }
1446 
1447 /**
1448  * xs_data_ready - "data ready" callback for UDP sockets
1449  * @sk: socket with data to read
1450  *
1451  */
1452 static void xs_data_ready(struct sock *sk)
1453 {
1454 	struct rpc_xprt *xprt;
1455 
1456 	read_lock_bh(&sk->sk_callback_lock);
1457 	dprintk("RPC:       xs_data_ready...\n");
1458 	xprt = xprt_from_sock(sk);
1459 	if (xprt != NULL) {
1460 		struct sock_xprt *transport = container_of(xprt,
1461 				struct sock_xprt, xprt);
1462 		transport->old_data_ready(sk);
1463 		/* Any data means we had a useful conversation, so
1464 		 * then we don't need to delay the next reconnect
1465 		 */
1466 		if (xprt->reestablish_timeout)
1467 			xprt->reestablish_timeout = 0;
1468 		if (!test_and_set_bit(XPRT_SOCK_DATA_READY, &transport->sock_state))
1469 			queue_work(xprtiod_workqueue, &transport->recv_worker);
1470 	}
1471 	read_unlock_bh(&sk->sk_callback_lock);
1472 }
1473 
1474 /*
1475  * Helper function to force a TCP close if the server is sending
1476  * junk and/or it has put us in CLOSE_WAIT
1477  */
1478 static void xs_tcp_force_close(struct rpc_xprt *xprt)
1479 {
1480 	xprt_force_disconnect(xprt);
1481 }
1482 
1483 #if defined(CONFIG_SUNRPC_BACKCHANNEL)
1484 static size_t xs_tcp_bc_maxpayload(struct rpc_xprt *xprt)
1485 {
1486 	return PAGE_SIZE;
1487 }
1488 #endif /* CONFIG_SUNRPC_BACKCHANNEL */
1489 
1490 /**
1491  * xs_tcp_state_change - callback to handle TCP socket state changes
1492  * @sk: socket whose state has changed
1493  *
1494  */
1495 static void xs_tcp_state_change(struct sock *sk)
1496 {
1497 	struct rpc_xprt *xprt;
1498 	struct sock_xprt *transport;
1499 
1500 	read_lock_bh(&sk->sk_callback_lock);
1501 	if (!(xprt = xprt_from_sock(sk)))
1502 		goto out;
1503 	dprintk("RPC:       xs_tcp_state_change client %p...\n", xprt);
1504 	dprintk("RPC:       state %x conn %d dead %d zapped %d sk_shutdown %d\n",
1505 			sk->sk_state, xprt_connected(xprt),
1506 			sock_flag(sk, SOCK_DEAD),
1507 			sock_flag(sk, SOCK_ZAPPED),
1508 			sk->sk_shutdown);
1509 
1510 	transport = container_of(xprt, struct sock_xprt, xprt);
1511 	trace_rpc_socket_state_change(xprt, sk->sk_socket);
1512 	switch (sk->sk_state) {
1513 	case TCP_ESTABLISHED:
1514 		if (!xprt_test_and_set_connected(xprt)) {
1515 			xprt->connect_cookie++;
1516 			clear_bit(XPRT_SOCK_CONNECTING, &transport->sock_state);
1517 			xprt_clear_connecting(xprt);
1518 
1519 			xprt->stat.connect_count++;
1520 			xprt->stat.connect_time += (long)jiffies -
1521 						   xprt->stat.connect_start;
1522 			xs_run_error_worker(transport, XPRT_SOCK_WAKE_PENDING);
1523 		}
1524 		break;
1525 	case TCP_FIN_WAIT1:
1526 		/* The client initiated a shutdown of the socket */
1527 		xprt->connect_cookie++;
1528 		xprt->reestablish_timeout = 0;
1529 		set_bit(XPRT_CLOSING, &xprt->state);
1530 		smp_mb__before_atomic();
1531 		clear_bit(XPRT_CONNECTED, &xprt->state);
1532 		clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
1533 		smp_mb__after_atomic();
1534 		break;
1535 	case TCP_CLOSE_WAIT:
1536 		/* The server initiated a shutdown of the socket */
1537 		xprt->connect_cookie++;
1538 		clear_bit(XPRT_CONNECTED, &xprt->state);
1539 		xs_run_error_worker(transport, XPRT_SOCK_WAKE_DISCONNECT);
1540 		/* fall through */
1541 	case TCP_CLOSING:
1542 		/*
1543 		 * If the server closed down the connection, make sure that
1544 		 * we back off before reconnecting
1545 		 */
1546 		if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
1547 			xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
1548 		break;
1549 	case TCP_LAST_ACK:
1550 		set_bit(XPRT_CLOSING, &xprt->state);
1551 		smp_mb__before_atomic();
1552 		clear_bit(XPRT_CONNECTED, &xprt->state);
1553 		smp_mb__after_atomic();
1554 		break;
1555 	case TCP_CLOSE:
1556 		if (test_and_clear_bit(XPRT_SOCK_CONNECTING,
1557 					&transport->sock_state))
1558 			xprt_clear_connecting(xprt);
1559 		clear_bit(XPRT_CLOSING, &xprt->state);
1560 		/* Trigger the socket release */
1561 		xs_run_error_worker(transport, XPRT_SOCK_WAKE_DISCONNECT);
1562 	}
1563  out:
1564 	read_unlock_bh(&sk->sk_callback_lock);
1565 }
1566 
1567 static void xs_write_space(struct sock *sk)
1568 {
1569 	struct socket_wq *wq;
1570 	struct sock_xprt *transport;
1571 	struct rpc_xprt *xprt;
1572 
1573 	if (!sk->sk_socket)
1574 		return;
1575 	clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
1576 
1577 	if (unlikely(!(xprt = xprt_from_sock(sk))))
1578 		return;
1579 	transport = container_of(xprt, struct sock_xprt, xprt);
1580 	rcu_read_lock();
1581 	wq = rcu_dereference(sk->sk_wq);
1582 	if (!wq || test_and_clear_bit(SOCKWQ_ASYNC_NOSPACE, &wq->flags) == 0)
1583 		goto out;
1584 
1585 	xs_run_error_worker(transport, XPRT_SOCK_WAKE_WRITE);
1586 	sk->sk_write_pending--;
1587 out:
1588 	rcu_read_unlock();
1589 }
1590 
1591 /**
1592  * xs_udp_write_space - callback invoked when socket buffer space
1593  *                             becomes available
1594  * @sk: socket whose state has changed
1595  *
1596  * Called when more output buffer space is available for this socket.
1597  * We try not to wake our writers until they can make "significant"
1598  * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1599  * with a bunch of small requests.
1600  */
1601 static void xs_udp_write_space(struct sock *sk)
1602 {
1603 	read_lock_bh(&sk->sk_callback_lock);
1604 
1605 	/* from net/core/sock.c:sock_def_write_space */
1606 	if (sock_writeable(sk))
1607 		xs_write_space(sk);
1608 
1609 	read_unlock_bh(&sk->sk_callback_lock);
1610 }
1611 
1612 /**
1613  * xs_tcp_write_space - callback invoked when socket buffer space
1614  *                             becomes available
1615  * @sk: socket whose state has changed
1616  *
1617  * Called when more output buffer space is available for this socket.
1618  * We try not to wake our writers until they can make "significant"
1619  * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1620  * with a bunch of small requests.
1621  */
1622 static void xs_tcp_write_space(struct sock *sk)
1623 {
1624 	read_lock_bh(&sk->sk_callback_lock);
1625 
1626 	/* from net/core/stream.c:sk_stream_write_space */
1627 	if (sk_stream_is_writeable(sk))
1628 		xs_write_space(sk);
1629 
1630 	read_unlock_bh(&sk->sk_callback_lock);
1631 }
1632 
1633 static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt)
1634 {
1635 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1636 	struct sock *sk = transport->inet;
1637 
1638 	if (transport->rcvsize) {
1639 		sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
1640 		sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2;
1641 	}
1642 	if (transport->sndsize) {
1643 		sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
1644 		sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2;
1645 		sk->sk_write_space(sk);
1646 	}
1647 }
1648 
1649 /**
1650  * xs_udp_set_buffer_size - set send and receive limits
1651  * @xprt: generic transport
1652  * @sndsize: requested size of send buffer, in bytes
1653  * @rcvsize: requested size of receive buffer, in bytes
1654  *
1655  * Set socket send and receive buffer size limits.
1656  */
1657 static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize)
1658 {
1659 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1660 
1661 	transport->sndsize = 0;
1662 	if (sndsize)
1663 		transport->sndsize = sndsize + 1024;
1664 	transport->rcvsize = 0;
1665 	if (rcvsize)
1666 		transport->rcvsize = rcvsize + 1024;
1667 
1668 	xs_udp_do_set_buffer_size(xprt);
1669 }
1670 
1671 /**
1672  * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
1673  * @xprt: controlling transport
1674  * @task: task that timed out
1675  *
1676  * Adjust the congestion window after a retransmit timeout has occurred.
1677  */
1678 static void xs_udp_timer(struct rpc_xprt *xprt, struct rpc_task *task)
1679 {
1680 	spin_lock(&xprt->transport_lock);
1681 	xprt_adjust_cwnd(xprt, task, -ETIMEDOUT);
1682 	spin_unlock(&xprt->transport_lock);
1683 }
1684 
1685 static int xs_get_random_port(void)
1686 {
1687 	unsigned short min = xprt_min_resvport, max = xprt_max_resvport;
1688 	unsigned short range;
1689 	unsigned short rand;
1690 
1691 	if (max < min)
1692 		return -EADDRINUSE;
1693 	range = max - min + 1;
1694 	rand = (unsigned short) prandom_u32() % range;
1695 	return rand + min;
1696 }
1697 
1698 /**
1699  * xs_set_reuseaddr_port - set the socket's port and address reuse options
1700  * @sock: socket
1701  *
1702  * Note that this function has to be called on all sockets that share the
1703  * same port, and it must be called before binding.
1704  */
1705 static void xs_sock_set_reuseport(struct socket *sock)
1706 {
1707 	int opt = 1;
1708 
1709 	kernel_setsockopt(sock, SOL_SOCKET, SO_REUSEPORT,
1710 			(char *)&opt, sizeof(opt));
1711 }
1712 
1713 static unsigned short xs_sock_getport(struct socket *sock)
1714 {
1715 	struct sockaddr_storage buf;
1716 	unsigned short port = 0;
1717 
1718 	if (kernel_getsockname(sock, (struct sockaddr *)&buf) < 0)
1719 		goto out;
1720 	switch (buf.ss_family) {
1721 	case AF_INET6:
1722 		port = ntohs(((struct sockaddr_in6 *)&buf)->sin6_port);
1723 		break;
1724 	case AF_INET:
1725 		port = ntohs(((struct sockaddr_in *)&buf)->sin_port);
1726 	}
1727 out:
1728 	return port;
1729 }
1730 
1731 /**
1732  * xs_set_port - reset the port number in the remote endpoint address
1733  * @xprt: generic transport
1734  * @port: new port number
1735  *
1736  */
1737 static void xs_set_port(struct rpc_xprt *xprt, unsigned short port)
1738 {
1739 	dprintk("RPC:       setting port for xprt %p to %u\n", xprt, port);
1740 
1741 	rpc_set_port(xs_addr(xprt), port);
1742 	xs_update_peer_port(xprt);
1743 }
1744 
1745 static void xs_set_srcport(struct sock_xprt *transport, struct socket *sock)
1746 {
1747 	if (transport->srcport == 0)
1748 		transport->srcport = xs_sock_getport(sock);
1749 }
1750 
1751 static int xs_get_srcport(struct sock_xprt *transport)
1752 {
1753 	int port = transport->srcport;
1754 
1755 	if (port == 0 && transport->xprt.resvport)
1756 		port = xs_get_random_port();
1757 	return port;
1758 }
1759 
1760 static unsigned short xs_next_srcport(struct sock_xprt *transport, unsigned short port)
1761 {
1762 	if (transport->srcport != 0)
1763 		transport->srcport = 0;
1764 	if (!transport->xprt.resvport)
1765 		return 0;
1766 	if (port <= xprt_min_resvport || port > xprt_max_resvport)
1767 		return xprt_max_resvport;
1768 	return --port;
1769 }
1770 static int xs_bind(struct sock_xprt *transport, struct socket *sock)
1771 {
1772 	struct sockaddr_storage myaddr;
1773 	int err, nloop = 0;
1774 	int port = xs_get_srcport(transport);
1775 	unsigned short last;
1776 
1777 	/*
1778 	 * If we are asking for any ephemeral port (i.e. port == 0 &&
1779 	 * transport->xprt.resvport == 0), don't bind.  Let the local
1780 	 * port selection happen implicitly when the socket is used
1781 	 * (for example at connect time).
1782 	 *
1783 	 * This ensures that we can continue to establish TCP
1784 	 * connections even when all local ephemeral ports are already
1785 	 * a part of some TCP connection.  This makes no difference
1786 	 * for UDP sockets, but also doens't harm them.
1787 	 *
1788 	 * If we're asking for any reserved port (i.e. port == 0 &&
1789 	 * transport->xprt.resvport == 1) xs_get_srcport above will
1790 	 * ensure that port is non-zero and we will bind as needed.
1791 	 */
1792 	if (port <= 0)
1793 		return port;
1794 
1795 	memcpy(&myaddr, &transport->srcaddr, transport->xprt.addrlen);
1796 	do {
1797 		rpc_set_port((struct sockaddr *)&myaddr, port);
1798 		err = kernel_bind(sock, (struct sockaddr *)&myaddr,
1799 				transport->xprt.addrlen);
1800 		if (err == 0) {
1801 			transport->srcport = port;
1802 			break;
1803 		}
1804 		last = port;
1805 		port = xs_next_srcport(transport, port);
1806 		if (port > last)
1807 			nloop++;
1808 	} while (err == -EADDRINUSE && nloop != 2);
1809 
1810 	if (myaddr.ss_family == AF_INET)
1811 		dprintk("RPC:       %s %pI4:%u: %s (%d)\n", __func__,
1812 				&((struct sockaddr_in *)&myaddr)->sin_addr,
1813 				port, err ? "failed" : "ok", err);
1814 	else
1815 		dprintk("RPC:       %s %pI6:%u: %s (%d)\n", __func__,
1816 				&((struct sockaddr_in6 *)&myaddr)->sin6_addr,
1817 				port, err ? "failed" : "ok", err);
1818 	return err;
1819 }
1820 
1821 /*
1822  * We don't support autobind on AF_LOCAL sockets
1823  */
1824 static void xs_local_rpcbind(struct rpc_task *task)
1825 {
1826 	xprt_set_bound(task->tk_xprt);
1827 }
1828 
1829 static void xs_local_set_port(struct rpc_xprt *xprt, unsigned short port)
1830 {
1831 }
1832 
1833 #ifdef CONFIG_DEBUG_LOCK_ALLOC
1834 static struct lock_class_key xs_key[2];
1835 static struct lock_class_key xs_slock_key[2];
1836 
1837 static inline void xs_reclassify_socketu(struct socket *sock)
1838 {
1839 	struct sock *sk = sock->sk;
1840 
1841 	sock_lock_init_class_and_name(sk, "slock-AF_LOCAL-RPC",
1842 		&xs_slock_key[1], "sk_lock-AF_LOCAL-RPC", &xs_key[1]);
1843 }
1844 
1845 static inline void xs_reclassify_socket4(struct socket *sock)
1846 {
1847 	struct sock *sk = sock->sk;
1848 
1849 	sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC",
1850 		&xs_slock_key[0], "sk_lock-AF_INET-RPC", &xs_key[0]);
1851 }
1852 
1853 static inline void xs_reclassify_socket6(struct socket *sock)
1854 {
1855 	struct sock *sk = sock->sk;
1856 
1857 	sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC",
1858 		&xs_slock_key[1], "sk_lock-AF_INET6-RPC", &xs_key[1]);
1859 }
1860 
1861 static inline void xs_reclassify_socket(int family, struct socket *sock)
1862 {
1863 	if (WARN_ON_ONCE(!sock_allow_reclassification(sock->sk)))
1864 		return;
1865 
1866 	switch (family) {
1867 	case AF_LOCAL:
1868 		xs_reclassify_socketu(sock);
1869 		break;
1870 	case AF_INET:
1871 		xs_reclassify_socket4(sock);
1872 		break;
1873 	case AF_INET6:
1874 		xs_reclassify_socket6(sock);
1875 		break;
1876 	}
1877 }
1878 #else
1879 static inline void xs_reclassify_socket(int family, struct socket *sock)
1880 {
1881 }
1882 #endif
1883 
1884 static void xs_dummy_setup_socket(struct work_struct *work)
1885 {
1886 }
1887 
1888 static struct socket *xs_create_sock(struct rpc_xprt *xprt,
1889 		struct sock_xprt *transport, int family, int type,
1890 		int protocol, bool reuseport)
1891 {
1892 	struct file *filp;
1893 	struct socket *sock;
1894 	int err;
1895 
1896 	err = __sock_create(xprt->xprt_net, family, type, protocol, &sock, 1);
1897 	if (err < 0) {
1898 		dprintk("RPC:       can't create %d transport socket (%d).\n",
1899 				protocol, -err);
1900 		goto out;
1901 	}
1902 	xs_reclassify_socket(family, sock);
1903 
1904 	if (reuseport)
1905 		xs_sock_set_reuseport(sock);
1906 
1907 	err = xs_bind(transport, sock);
1908 	if (err) {
1909 		sock_release(sock);
1910 		goto out;
1911 	}
1912 
1913 	filp = sock_alloc_file(sock, O_NONBLOCK, NULL);
1914 	if (IS_ERR(filp))
1915 		return ERR_CAST(filp);
1916 	transport->file = filp;
1917 
1918 	return sock;
1919 out:
1920 	return ERR_PTR(err);
1921 }
1922 
1923 static int xs_local_finish_connecting(struct rpc_xprt *xprt,
1924 				      struct socket *sock)
1925 {
1926 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
1927 									xprt);
1928 
1929 	if (!transport->inet) {
1930 		struct sock *sk = sock->sk;
1931 
1932 		write_lock_bh(&sk->sk_callback_lock);
1933 
1934 		xs_save_old_callbacks(transport, sk);
1935 
1936 		sk->sk_user_data = xprt;
1937 		sk->sk_data_ready = xs_data_ready;
1938 		sk->sk_write_space = xs_udp_write_space;
1939 		sock_set_flag(sk, SOCK_FASYNC);
1940 		sk->sk_error_report = xs_error_report;
1941 
1942 		xprt_clear_connected(xprt);
1943 
1944 		/* Reset to new socket */
1945 		transport->sock = sock;
1946 		transport->inet = sk;
1947 
1948 		write_unlock_bh(&sk->sk_callback_lock);
1949 	}
1950 
1951 	xs_stream_start_connect(transport);
1952 
1953 	return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, 0);
1954 }
1955 
1956 /**
1957  * xs_local_setup_socket - create AF_LOCAL socket, connect to a local endpoint
1958  * @transport: socket transport to connect
1959  */
1960 static int xs_local_setup_socket(struct sock_xprt *transport)
1961 {
1962 	struct rpc_xprt *xprt = &transport->xprt;
1963 	struct file *filp;
1964 	struct socket *sock;
1965 	int status = -EIO;
1966 
1967 	status = __sock_create(xprt->xprt_net, AF_LOCAL,
1968 					SOCK_STREAM, 0, &sock, 1);
1969 	if (status < 0) {
1970 		dprintk("RPC:       can't create AF_LOCAL "
1971 			"transport socket (%d).\n", -status);
1972 		goto out;
1973 	}
1974 	xs_reclassify_socket(AF_LOCAL, sock);
1975 
1976 	filp = sock_alloc_file(sock, O_NONBLOCK, NULL);
1977 	if (IS_ERR(filp)) {
1978 		status = PTR_ERR(filp);
1979 		goto out;
1980 	}
1981 	transport->file = filp;
1982 
1983 	dprintk("RPC:       worker connecting xprt %p via AF_LOCAL to %s\n",
1984 			xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1985 
1986 	status = xs_local_finish_connecting(xprt, sock);
1987 	trace_rpc_socket_connect(xprt, sock, status);
1988 	switch (status) {
1989 	case 0:
1990 		dprintk("RPC:       xprt %p connected to %s\n",
1991 				xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1992 		xprt->stat.connect_count++;
1993 		xprt->stat.connect_time += (long)jiffies -
1994 					   xprt->stat.connect_start;
1995 		xprt_set_connected(xprt);
1996 	case -ENOBUFS:
1997 		break;
1998 	case -ENOENT:
1999 		dprintk("RPC:       xprt %p: socket %s does not exist\n",
2000 				xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
2001 		break;
2002 	case -ECONNREFUSED:
2003 		dprintk("RPC:       xprt %p: connection refused for %s\n",
2004 				xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
2005 		break;
2006 	default:
2007 		printk(KERN_ERR "%s: unhandled error (%d) connecting to %s\n",
2008 				__func__, -status,
2009 				xprt->address_strings[RPC_DISPLAY_ADDR]);
2010 	}
2011 
2012 out:
2013 	xprt_clear_connecting(xprt);
2014 	xprt_wake_pending_tasks(xprt, status);
2015 	return status;
2016 }
2017 
2018 static void xs_local_connect(struct rpc_xprt *xprt, struct rpc_task *task)
2019 {
2020 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2021 	int ret;
2022 
2023 	 if (RPC_IS_ASYNC(task)) {
2024 		/*
2025 		 * We want the AF_LOCAL connect to be resolved in the
2026 		 * filesystem namespace of the process making the rpc
2027 		 * call.  Thus we connect synchronously.
2028 		 *
2029 		 * If we want to support asynchronous AF_LOCAL calls,
2030 		 * we'll need to figure out how to pass a namespace to
2031 		 * connect.
2032 		 */
2033 		task->tk_rpc_status = -ENOTCONN;
2034 		rpc_exit(task, -ENOTCONN);
2035 		return;
2036 	}
2037 	ret = xs_local_setup_socket(transport);
2038 	if (ret && !RPC_IS_SOFTCONN(task))
2039 		msleep_interruptible(15000);
2040 }
2041 
2042 #if IS_ENABLED(CONFIG_SUNRPC_SWAP)
2043 /*
2044  * Note that this should be called with XPRT_LOCKED held (or when we otherwise
2045  * know that we have exclusive access to the socket), to guard against
2046  * races with xs_reset_transport.
2047  */
2048 static void xs_set_memalloc(struct rpc_xprt *xprt)
2049 {
2050 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
2051 			xprt);
2052 
2053 	/*
2054 	 * If there's no sock, then we have nothing to set. The
2055 	 * reconnecting process will get it for us.
2056 	 */
2057 	if (!transport->inet)
2058 		return;
2059 	if (atomic_read(&xprt->swapper))
2060 		sk_set_memalloc(transport->inet);
2061 }
2062 
2063 /**
2064  * xs_enable_swap - Tag this transport as being used for swap.
2065  * @xprt: transport to tag
2066  *
2067  * Take a reference to this transport on behalf of the rpc_clnt, and
2068  * optionally mark it for swapping if it wasn't already.
2069  */
2070 static int
2071 xs_enable_swap(struct rpc_xprt *xprt)
2072 {
2073 	struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt);
2074 
2075 	if (atomic_inc_return(&xprt->swapper) != 1)
2076 		return 0;
2077 	if (wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_KILLABLE))
2078 		return -ERESTARTSYS;
2079 	if (xs->inet)
2080 		sk_set_memalloc(xs->inet);
2081 	xprt_release_xprt(xprt, NULL);
2082 	return 0;
2083 }
2084 
2085 /**
2086  * xs_disable_swap - Untag this transport as being used for swap.
2087  * @xprt: transport to tag
2088  *
2089  * Drop a "swapper" reference to this xprt on behalf of the rpc_clnt. If the
2090  * swapper refcount goes to 0, untag the socket as a memalloc socket.
2091  */
2092 static void
2093 xs_disable_swap(struct rpc_xprt *xprt)
2094 {
2095 	struct sock_xprt *xs = container_of(xprt, struct sock_xprt, xprt);
2096 
2097 	if (!atomic_dec_and_test(&xprt->swapper))
2098 		return;
2099 	if (wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_KILLABLE))
2100 		return;
2101 	if (xs->inet)
2102 		sk_clear_memalloc(xs->inet);
2103 	xprt_release_xprt(xprt, NULL);
2104 }
2105 #else
2106 static void xs_set_memalloc(struct rpc_xprt *xprt)
2107 {
2108 }
2109 
2110 static int
2111 xs_enable_swap(struct rpc_xprt *xprt)
2112 {
2113 	return -EINVAL;
2114 }
2115 
2116 static void
2117 xs_disable_swap(struct rpc_xprt *xprt)
2118 {
2119 }
2120 #endif
2121 
2122 static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
2123 {
2124 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2125 
2126 	if (!transport->inet) {
2127 		struct sock *sk = sock->sk;
2128 
2129 		write_lock_bh(&sk->sk_callback_lock);
2130 
2131 		xs_save_old_callbacks(transport, sk);
2132 
2133 		sk->sk_user_data = xprt;
2134 		sk->sk_data_ready = xs_data_ready;
2135 		sk->sk_write_space = xs_udp_write_space;
2136 		sock_set_flag(sk, SOCK_FASYNC);
2137 
2138 		xprt_set_connected(xprt);
2139 
2140 		/* Reset to new socket */
2141 		transport->sock = sock;
2142 		transport->inet = sk;
2143 
2144 		xs_set_memalloc(xprt);
2145 
2146 		write_unlock_bh(&sk->sk_callback_lock);
2147 	}
2148 	xs_udp_do_set_buffer_size(xprt);
2149 
2150 	xprt->stat.connect_start = jiffies;
2151 }
2152 
2153 static void xs_udp_setup_socket(struct work_struct *work)
2154 {
2155 	struct sock_xprt *transport =
2156 		container_of(work, struct sock_xprt, connect_worker.work);
2157 	struct rpc_xprt *xprt = &transport->xprt;
2158 	struct socket *sock;
2159 	int status = -EIO;
2160 
2161 	sock = xs_create_sock(xprt, transport,
2162 			xs_addr(xprt)->sa_family, SOCK_DGRAM,
2163 			IPPROTO_UDP, false);
2164 	if (IS_ERR(sock))
2165 		goto out;
2166 
2167 	dprintk("RPC:       worker connecting xprt %p via %s to "
2168 				"%s (port %s)\n", xprt,
2169 			xprt->address_strings[RPC_DISPLAY_PROTO],
2170 			xprt->address_strings[RPC_DISPLAY_ADDR],
2171 			xprt->address_strings[RPC_DISPLAY_PORT]);
2172 
2173 	xs_udp_finish_connecting(xprt, sock);
2174 	trace_rpc_socket_connect(xprt, sock, 0);
2175 	status = 0;
2176 out:
2177 	xprt_clear_connecting(xprt);
2178 	xprt_unlock_connect(xprt, transport);
2179 	xprt_wake_pending_tasks(xprt, status);
2180 }
2181 
2182 /**
2183  * xs_tcp_shutdown - gracefully shut down a TCP socket
2184  * @xprt: transport
2185  *
2186  * Initiates a graceful shutdown of the TCP socket by calling the
2187  * equivalent of shutdown(SHUT_RDWR);
2188  */
2189 static void xs_tcp_shutdown(struct rpc_xprt *xprt)
2190 {
2191 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2192 	struct socket *sock = transport->sock;
2193 	int skst = transport->inet ? transport->inet->sk_state : TCP_CLOSE;
2194 
2195 	if (sock == NULL)
2196 		return;
2197 	switch (skst) {
2198 	default:
2199 		kernel_sock_shutdown(sock, SHUT_RDWR);
2200 		trace_rpc_socket_shutdown(xprt, sock);
2201 		break;
2202 	case TCP_CLOSE:
2203 	case TCP_TIME_WAIT:
2204 		xs_reset_transport(transport);
2205 	}
2206 }
2207 
2208 static void xs_tcp_set_socket_timeouts(struct rpc_xprt *xprt,
2209 		struct socket *sock)
2210 {
2211 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2212 	unsigned int keepidle;
2213 	unsigned int keepcnt;
2214 	unsigned int opt_on = 1;
2215 	unsigned int timeo;
2216 
2217 	spin_lock(&xprt->transport_lock);
2218 	keepidle = DIV_ROUND_UP(xprt->timeout->to_initval, HZ);
2219 	keepcnt = xprt->timeout->to_retries + 1;
2220 	timeo = jiffies_to_msecs(xprt->timeout->to_initval) *
2221 		(xprt->timeout->to_retries + 1);
2222 	clear_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state);
2223 	spin_unlock(&xprt->transport_lock);
2224 
2225 	/* TCP Keepalive options */
2226 	kernel_setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE,
2227 			(char *)&opt_on, sizeof(opt_on));
2228 	kernel_setsockopt(sock, SOL_TCP, TCP_KEEPIDLE,
2229 			(char *)&keepidle, sizeof(keepidle));
2230 	kernel_setsockopt(sock, SOL_TCP, TCP_KEEPINTVL,
2231 			(char *)&keepidle, sizeof(keepidle));
2232 	kernel_setsockopt(sock, SOL_TCP, TCP_KEEPCNT,
2233 			(char *)&keepcnt, sizeof(keepcnt));
2234 
2235 	/* TCP user timeout (see RFC5482) */
2236 	kernel_setsockopt(sock, SOL_TCP, TCP_USER_TIMEOUT,
2237 			(char *)&timeo, sizeof(timeo));
2238 }
2239 
2240 static void xs_tcp_set_connect_timeout(struct rpc_xprt *xprt,
2241 		unsigned long connect_timeout,
2242 		unsigned long reconnect_timeout)
2243 {
2244 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2245 	struct rpc_timeout to;
2246 	unsigned long initval;
2247 
2248 	spin_lock(&xprt->transport_lock);
2249 	if (reconnect_timeout < xprt->max_reconnect_timeout)
2250 		xprt->max_reconnect_timeout = reconnect_timeout;
2251 	if (connect_timeout < xprt->connect_timeout) {
2252 		memcpy(&to, xprt->timeout, sizeof(to));
2253 		initval = DIV_ROUND_UP(connect_timeout, to.to_retries + 1);
2254 		/* Arbitrary lower limit */
2255 		if (initval <  XS_TCP_INIT_REEST_TO << 1)
2256 			initval = XS_TCP_INIT_REEST_TO << 1;
2257 		to.to_initval = initval;
2258 		to.to_maxval = initval;
2259 		memcpy(&transport->tcp_timeout, &to,
2260 				sizeof(transport->tcp_timeout));
2261 		xprt->timeout = &transport->tcp_timeout;
2262 		xprt->connect_timeout = connect_timeout;
2263 	}
2264 	set_bit(XPRT_SOCK_UPD_TIMEOUT, &transport->sock_state);
2265 	spin_unlock(&xprt->transport_lock);
2266 }
2267 
2268 static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
2269 {
2270 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2271 	int ret = -ENOTCONN;
2272 
2273 	if (!transport->inet) {
2274 		struct sock *sk = sock->sk;
2275 		unsigned int addr_pref = IPV6_PREFER_SRC_PUBLIC;
2276 
2277 		/* Avoid temporary address, they are bad for long-lived
2278 		 * connections such as NFS mounts.
2279 		 * RFC4941, section 3.6 suggests that:
2280 		 *    Individual applications, which have specific
2281 		 *    knowledge about the normal duration of connections,
2282 		 *    MAY override this as appropriate.
2283 		 */
2284 		kernel_setsockopt(sock, SOL_IPV6, IPV6_ADDR_PREFERENCES,
2285 				(char *)&addr_pref, sizeof(addr_pref));
2286 
2287 		xs_tcp_set_socket_timeouts(xprt, sock);
2288 
2289 		write_lock_bh(&sk->sk_callback_lock);
2290 
2291 		xs_save_old_callbacks(transport, sk);
2292 
2293 		sk->sk_user_data = xprt;
2294 		sk->sk_data_ready = xs_data_ready;
2295 		sk->sk_state_change = xs_tcp_state_change;
2296 		sk->sk_write_space = xs_tcp_write_space;
2297 		sock_set_flag(sk, SOCK_FASYNC);
2298 		sk->sk_error_report = xs_error_report;
2299 
2300 		/* socket options */
2301 		sock_reset_flag(sk, SOCK_LINGER);
2302 		tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF;
2303 
2304 		xprt_clear_connected(xprt);
2305 
2306 		/* Reset to new socket */
2307 		transport->sock = sock;
2308 		transport->inet = sk;
2309 
2310 		write_unlock_bh(&sk->sk_callback_lock);
2311 	}
2312 
2313 	if (!xprt_bound(xprt))
2314 		goto out;
2315 
2316 	xs_set_memalloc(xprt);
2317 
2318 	xs_stream_start_connect(transport);
2319 
2320 	/* Tell the socket layer to start connecting... */
2321 	set_bit(XPRT_SOCK_CONNECTING, &transport->sock_state);
2322 	ret = kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK);
2323 	switch (ret) {
2324 	case 0:
2325 		xs_set_srcport(transport, sock);
2326 		/* fall through */
2327 	case -EINPROGRESS:
2328 		/* SYN_SENT! */
2329 		if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
2330 			xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2331 		break;
2332 	case -EADDRNOTAVAIL:
2333 		/* Source port number is unavailable. Try a new one! */
2334 		transport->srcport = 0;
2335 	}
2336 out:
2337 	return ret;
2338 }
2339 
2340 /**
2341  * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint
2342  * @work: queued work item
2343  *
2344  * Invoked by a work queue tasklet.
2345  */
2346 static void xs_tcp_setup_socket(struct work_struct *work)
2347 {
2348 	struct sock_xprt *transport =
2349 		container_of(work, struct sock_xprt, connect_worker.work);
2350 	struct socket *sock = transport->sock;
2351 	struct rpc_xprt *xprt = &transport->xprt;
2352 	int status = -EIO;
2353 
2354 	if (!sock) {
2355 		sock = xs_create_sock(xprt, transport,
2356 				xs_addr(xprt)->sa_family, SOCK_STREAM,
2357 				IPPROTO_TCP, true);
2358 		if (IS_ERR(sock)) {
2359 			status = PTR_ERR(sock);
2360 			goto out;
2361 		}
2362 	}
2363 
2364 	dprintk("RPC:       worker connecting xprt %p via %s to "
2365 				"%s (port %s)\n", xprt,
2366 			xprt->address_strings[RPC_DISPLAY_PROTO],
2367 			xprt->address_strings[RPC_DISPLAY_ADDR],
2368 			xprt->address_strings[RPC_DISPLAY_PORT]);
2369 
2370 	status = xs_tcp_finish_connecting(xprt, sock);
2371 	trace_rpc_socket_connect(xprt, sock, status);
2372 	dprintk("RPC:       %p connect status %d connected %d sock state %d\n",
2373 			xprt, -status, xprt_connected(xprt),
2374 			sock->sk->sk_state);
2375 	switch (status) {
2376 	default:
2377 		printk("%s: connect returned unhandled error %d\n",
2378 			__func__, status);
2379 		/* fall through */
2380 	case -EADDRNOTAVAIL:
2381 		/* We're probably in TIME_WAIT. Get rid of existing socket,
2382 		 * and retry
2383 		 */
2384 		xs_tcp_force_close(xprt);
2385 		break;
2386 	case 0:
2387 	case -EINPROGRESS:
2388 	case -EALREADY:
2389 		xprt_unlock_connect(xprt, transport);
2390 		return;
2391 	case -EINVAL:
2392 		/* Happens, for instance, if the user specified a link
2393 		 * local IPv6 address without a scope-id.
2394 		 */
2395 	case -ECONNREFUSED:
2396 	case -ECONNRESET:
2397 	case -ENETDOWN:
2398 	case -ENETUNREACH:
2399 	case -EHOSTUNREACH:
2400 	case -EADDRINUSE:
2401 	case -ENOBUFS:
2402 		/*
2403 		 * xs_tcp_force_close() wakes tasks with -EIO.
2404 		 * We need to wake them first to ensure the
2405 		 * correct error code.
2406 		 */
2407 		xprt_wake_pending_tasks(xprt, status);
2408 		xs_tcp_force_close(xprt);
2409 		goto out;
2410 	}
2411 	status = -EAGAIN;
2412 out:
2413 	xprt_clear_connecting(xprt);
2414 	xprt_unlock_connect(xprt, transport);
2415 	xprt_wake_pending_tasks(xprt, status);
2416 }
2417 
2418 /**
2419  * xs_connect - connect a socket to a remote endpoint
2420  * @xprt: pointer to transport structure
2421  * @task: address of RPC task that manages state of connect request
2422  *
2423  * TCP: If the remote end dropped the connection, delay reconnecting.
2424  *
2425  * UDP socket connects are synchronous, but we use a work queue anyway
2426  * to guarantee that even unprivileged user processes can set up a
2427  * socket on a privileged port.
2428  *
2429  * If a UDP socket connect fails, the delay behavior here prevents
2430  * retry floods (hard mounts).
2431  */
2432 static void xs_connect(struct rpc_xprt *xprt, struct rpc_task *task)
2433 {
2434 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2435 	unsigned long delay = 0;
2436 
2437 	WARN_ON_ONCE(!xprt_lock_connect(xprt, task, transport));
2438 
2439 	if (transport->sock != NULL) {
2440 		dprintk("RPC:       xs_connect delayed xprt %p for %lu "
2441 				"seconds\n",
2442 				xprt, xprt->reestablish_timeout / HZ);
2443 
2444 		/* Start by resetting any existing state */
2445 		xs_reset_transport(transport);
2446 
2447 		delay = xprt_reconnect_delay(xprt);
2448 		xprt_reconnect_backoff(xprt, XS_TCP_INIT_REEST_TO);
2449 
2450 	} else
2451 		dprintk("RPC:       xs_connect scheduled xprt %p\n", xprt);
2452 
2453 	queue_delayed_work(xprtiod_workqueue,
2454 			&transport->connect_worker,
2455 			delay);
2456 }
2457 
2458 static void xs_wake_disconnect(struct sock_xprt *transport)
2459 {
2460 	if (test_and_clear_bit(XPRT_SOCK_WAKE_DISCONNECT, &transport->sock_state))
2461 		xs_tcp_force_close(&transport->xprt);
2462 }
2463 
2464 static void xs_wake_write(struct sock_xprt *transport)
2465 {
2466 	if (test_and_clear_bit(XPRT_SOCK_WAKE_WRITE, &transport->sock_state))
2467 		xprt_write_space(&transport->xprt);
2468 }
2469 
2470 static void xs_wake_error(struct sock_xprt *transport)
2471 {
2472 	int sockerr;
2473 	int sockerr_len = sizeof(sockerr);
2474 
2475 	if (!test_bit(XPRT_SOCK_WAKE_ERROR, &transport->sock_state))
2476 		return;
2477 	mutex_lock(&transport->recv_mutex);
2478 	if (transport->sock == NULL)
2479 		goto out;
2480 	if (!test_and_clear_bit(XPRT_SOCK_WAKE_ERROR, &transport->sock_state))
2481 		goto out;
2482 	if (kernel_getsockopt(transport->sock, SOL_SOCKET, SO_ERROR,
2483 				(char *)&sockerr, &sockerr_len) != 0)
2484 		goto out;
2485 	if (sockerr < 0)
2486 		xprt_wake_pending_tasks(&transport->xprt, sockerr);
2487 out:
2488 	mutex_unlock(&transport->recv_mutex);
2489 }
2490 
2491 static void xs_wake_pending(struct sock_xprt *transport)
2492 {
2493 	if (test_and_clear_bit(XPRT_SOCK_WAKE_PENDING, &transport->sock_state))
2494 		xprt_wake_pending_tasks(&transport->xprt, -EAGAIN);
2495 }
2496 
2497 static void xs_error_handle(struct work_struct *work)
2498 {
2499 	struct sock_xprt *transport = container_of(work,
2500 			struct sock_xprt, error_worker);
2501 
2502 	xs_wake_disconnect(transport);
2503 	xs_wake_write(transport);
2504 	xs_wake_error(transport);
2505 	xs_wake_pending(transport);
2506 }
2507 
2508 /**
2509  * xs_local_print_stats - display AF_LOCAL socket-specifc stats
2510  * @xprt: rpc_xprt struct containing statistics
2511  * @seq: output file
2512  *
2513  */
2514 static void xs_local_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2515 {
2516 	long idle_time = 0;
2517 
2518 	if (xprt_connected(xprt))
2519 		idle_time = (long)(jiffies - xprt->last_used) / HZ;
2520 
2521 	seq_printf(seq, "\txprt:\tlocal %lu %lu %lu %ld %lu %lu %lu "
2522 			"%llu %llu %lu %llu %llu\n",
2523 			xprt->stat.bind_count,
2524 			xprt->stat.connect_count,
2525 			xprt->stat.connect_time / HZ,
2526 			idle_time,
2527 			xprt->stat.sends,
2528 			xprt->stat.recvs,
2529 			xprt->stat.bad_xids,
2530 			xprt->stat.req_u,
2531 			xprt->stat.bklog_u,
2532 			xprt->stat.max_slots,
2533 			xprt->stat.sending_u,
2534 			xprt->stat.pending_u);
2535 }
2536 
2537 /**
2538  * xs_udp_print_stats - display UDP socket-specifc stats
2539  * @xprt: rpc_xprt struct containing statistics
2540  * @seq: output file
2541  *
2542  */
2543 static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2544 {
2545 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2546 
2547 	seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %llu %llu "
2548 			"%lu %llu %llu\n",
2549 			transport->srcport,
2550 			xprt->stat.bind_count,
2551 			xprt->stat.sends,
2552 			xprt->stat.recvs,
2553 			xprt->stat.bad_xids,
2554 			xprt->stat.req_u,
2555 			xprt->stat.bklog_u,
2556 			xprt->stat.max_slots,
2557 			xprt->stat.sending_u,
2558 			xprt->stat.pending_u);
2559 }
2560 
2561 /**
2562  * xs_tcp_print_stats - display TCP socket-specifc stats
2563  * @xprt: rpc_xprt struct containing statistics
2564  * @seq: output file
2565  *
2566  */
2567 static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2568 {
2569 	struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2570 	long idle_time = 0;
2571 
2572 	if (xprt_connected(xprt))
2573 		idle_time = (long)(jiffies - xprt->last_used) / HZ;
2574 
2575 	seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu "
2576 			"%llu %llu %lu %llu %llu\n",
2577 			transport->srcport,
2578 			xprt->stat.bind_count,
2579 			xprt->stat.connect_count,
2580 			xprt->stat.connect_time / HZ,
2581 			idle_time,
2582 			xprt->stat.sends,
2583 			xprt->stat.recvs,
2584 			xprt->stat.bad_xids,
2585 			xprt->stat.req_u,
2586 			xprt->stat.bklog_u,
2587 			xprt->stat.max_slots,
2588 			xprt->stat.sending_u,
2589 			xprt->stat.pending_u);
2590 }
2591 
2592 /*
2593  * Allocate a bunch of pages for a scratch buffer for the rpc code. The reason
2594  * we allocate pages instead doing a kmalloc like rpc_malloc is because we want
2595  * to use the server side send routines.
2596  */
2597 static int bc_malloc(struct rpc_task *task)
2598 {
2599 	struct rpc_rqst *rqst = task->tk_rqstp;
2600 	size_t size = rqst->rq_callsize;
2601 	struct page *page;
2602 	struct rpc_buffer *buf;
2603 
2604 	if (size > PAGE_SIZE - sizeof(struct rpc_buffer)) {
2605 		WARN_ONCE(1, "xprtsock: large bc buffer request (size %zu)\n",
2606 			  size);
2607 		return -EINVAL;
2608 	}
2609 
2610 	page = alloc_page(GFP_KERNEL);
2611 	if (!page)
2612 		return -ENOMEM;
2613 
2614 	buf = page_address(page);
2615 	buf->len = PAGE_SIZE;
2616 
2617 	rqst->rq_buffer = buf->data;
2618 	rqst->rq_rbuffer = (char *)rqst->rq_buffer + rqst->rq_callsize;
2619 	return 0;
2620 }
2621 
2622 /*
2623  * Free the space allocated in the bc_alloc routine
2624  */
2625 static void bc_free(struct rpc_task *task)
2626 {
2627 	void *buffer = task->tk_rqstp->rq_buffer;
2628 	struct rpc_buffer *buf;
2629 
2630 	buf = container_of(buffer, struct rpc_buffer, data);
2631 	free_page((unsigned long)buf);
2632 }
2633 
2634 /*
2635  * Use the svc_sock to send the callback. Must be called with svsk->sk_mutex
2636  * held. Borrows heavily from svc_tcp_sendto and xs_tcp_send_request.
2637  */
2638 static int bc_sendto(struct rpc_rqst *req)
2639 {
2640 	int len;
2641 	struct xdr_buf *xbufp = &req->rq_snd_buf;
2642 	struct sock_xprt *transport =
2643 			container_of(req->rq_xprt, struct sock_xprt, xprt);
2644 	unsigned long headoff;
2645 	unsigned long tailoff;
2646 	struct page *tailpage;
2647 	struct msghdr msg = {
2648 		.msg_flags	= MSG_MORE
2649 	};
2650 	rpc_fraghdr marker = cpu_to_be32(RPC_LAST_STREAM_FRAGMENT |
2651 					 (u32)xbufp->len);
2652 	struct kvec iov = {
2653 		.iov_base	= &marker,
2654 		.iov_len	= sizeof(marker),
2655 	};
2656 
2657 	len = kernel_sendmsg(transport->sock, &msg, &iov, 1, iov.iov_len);
2658 	if (len != iov.iov_len)
2659 		return -EAGAIN;
2660 
2661 	tailpage = NULL;
2662 	if (xbufp->tail[0].iov_len)
2663 		tailpage = virt_to_page(xbufp->tail[0].iov_base);
2664 	tailoff = (unsigned long)xbufp->tail[0].iov_base & ~PAGE_MASK;
2665 	headoff = (unsigned long)xbufp->head[0].iov_base & ~PAGE_MASK;
2666 	len = svc_send_common(transport->sock, xbufp,
2667 			      virt_to_page(xbufp->head[0].iov_base), headoff,
2668 			      tailpage, tailoff);
2669 	if (len != xbufp->len)
2670 		return -EAGAIN;
2671 	return len;
2672 }
2673 
2674 /*
2675  * The send routine. Borrows from svc_send
2676  */
2677 static int bc_send_request(struct rpc_rqst *req)
2678 {
2679 	struct svc_xprt	*xprt;
2680 	int len;
2681 
2682 	dprintk("sending request with xid: %08x\n", ntohl(req->rq_xid));
2683 	/*
2684 	 * Get the server socket associated with this callback xprt
2685 	 */
2686 	xprt = req->rq_xprt->bc_xprt;
2687 
2688 	/*
2689 	 * Grab the mutex to serialize data as the connection is shared
2690 	 * with the fore channel
2691 	 */
2692 	mutex_lock(&xprt->xpt_mutex);
2693 	if (test_bit(XPT_DEAD, &xprt->xpt_flags))
2694 		len = -ENOTCONN;
2695 	else
2696 		len = bc_sendto(req);
2697 	mutex_unlock(&xprt->xpt_mutex);
2698 
2699 	if (len > 0)
2700 		len = 0;
2701 
2702 	return len;
2703 }
2704 
2705 /*
2706  * The close routine. Since this is client initiated, we do nothing
2707  */
2708 
2709 static void bc_close(struct rpc_xprt *xprt)
2710 {
2711 }
2712 
2713 /*
2714  * The xprt destroy routine. Again, because this connection is client
2715  * initiated, we do nothing
2716  */
2717 
2718 static void bc_destroy(struct rpc_xprt *xprt)
2719 {
2720 	dprintk("RPC:       bc_destroy xprt %p\n", xprt);
2721 
2722 	xs_xprt_free(xprt);
2723 	module_put(THIS_MODULE);
2724 }
2725 
2726 static const struct rpc_xprt_ops xs_local_ops = {
2727 	.reserve_xprt		= xprt_reserve_xprt,
2728 	.release_xprt		= xprt_release_xprt,
2729 	.alloc_slot		= xprt_alloc_slot,
2730 	.free_slot		= xprt_free_slot,
2731 	.rpcbind		= xs_local_rpcbind,
2732 	.set_port		= xs_local_set_port,
2733 	.connect		= xs_local_connect,
2734 	.buf_alloc		= rpc_malloc,
2735 	.buf_free		= rpc_free,
2736 	.prepare_request	= xs_stream_prepare_request,
2737 	.send_request		= xs_local_send_request,
2738 	.wait_for_reply_request	= xprt_wait_for_reply_request_def,
2739 	.close			= xs_close,
2740 	.destroy		= xs_destroy,
2741 	.print_stats		= xs_local_print_stats,
2742 	.enable_swap		= xs_enable_swap,
2743 	.disable_swap		= xs_disable_swap,
2744 };
2745 
2746 static const struct rpc_xprt_ops xs_udp_ops = {
2747 	.set_buffer_size	= xs_udp_set_buffer_size,
2748 	.reserve_xprt		= xprt_reserve_xprt_cong,
2749 	.release_xprt		= xprt_release_xprt_cong,
2750 	.alloc_slot		= xprt_alloc_slot,
2751 	.free_slot		= xprt_free_slot,
2752 	.rpcbind		= rpcb_getport_async,
2753 	.set_port		= xs_set_port,
2754 	.connect		= xs_connect,
2755 	.buf_alloc		= rpc_malloc,
2756 	.buf_free		= rpc_free,
2757 	.send_request		= xs_udp_send_request,
2758 	.wait_for_reply_request	= xprt_wait_for_reply_request_rtt,
2759 	.timer			= xs_udp_timer,
2760 	.release_request	= xprt_release_rqst_cong,
2761 	.close			= xs_close,
2762 	.destroy		= xs_destroy,
2763 	.print_stats		= xs_udp_print_stats,
2764 	.enable_swap		= xs_enable_swap,
2765 	.disable_swap		= xs_disable_swap,
2766 	.inject_disconnect	= xs_inject_disconnect,
2767 };
2768 
2769 static const struct rpc_xprt_ops xs_tcp_ops = {
2770 	.reserve_xprt		= xprt_reserve_xprt,
2771 	.release_xprt		= xprt_release_xprt,
2772 	.alloc_slot		= xprt_alloc_slot,
2773 	.free_slot		= xprt_free_slot,
2774 	.rpcbind		= rpcb_getport_async,
2775 	.set_port		= xs_set_port,
2776 	.connect		= xs_connect,
2777 	.buf_alloc		= rpc_malloc,
2778 	.buf_free		= rpc_free,
2779 	.prepare_request	= xs_stream_prepare_request,
2780 	.send_request		= xs_tcp_send_request,
2781 	.wait_for_reply_request	= xprt_wait_for_reply_request_def,
2782 	.close			= xs_tcp_shutdown,
2783 	.destroy		= xs_destroy,
2784 	.set_connect_timeout	= xs_tcp_set_connect_timeout,
2785 	.print_stats		= xs_tcp_print_stats,
2786 	.enable_swap		= xs_enable_swap,
2787 	.disable_swap		= xs_disable_swap,
2788 	.inject_disconnect	= xs_inject_disconnect,
2789 #ifdef CONFIG_SUNRPC_BACKCHANNEL
2790 	.bc_setup		= xprt_setup_bc,
2791 	.bc_maxpayload		= xs_tcp_bc_maxpayload,
2792 	.bc_num_slots		= xprt_bc_max_slots,
2793 	.bc_free_rqst		= xprt_free_bc_rqst,
2794 	.bc_destroy		= xprt_destroy_bc,
2795 #endif
2796 };
2797 
2798 /*
2799  * The rpc_xprt_ops for the server backchannel
2800  */
2801 
2802 static const struct rpc_xprt_ops bc_tcp_ops = {
2803 	.reserve_xprt		= xprt_reserve_xprt,
2804 	.release_xprt		= xprt_release_xprt,
2805 	.alloc_slot		= xprt_alloc_slot,
2806 	.free_slot		= xprt_free_slot,
2807 	.buf_alloc		= bc_malloc,
2808 	.buf_free		= bc_free,
2809 	.send_request		= bc_send_request,
2810 	.wait_for_reply_request	= xprt_wait_for_reply_request_def,
2811 	.close			= bc_close,
2812 	.destroy		= bc_destroy,
2813 	.print_stats		= xs_tcp_print_stats,
2814 	.enable_swap		= xs_enable_swap,
2815 	.disable_swap		= xs_disable_swap,
2816 	.inject_disconnect	= xs_inject_disconnect,
2817 };
2818 
2819 static int xs_init_anyaddr(const int family, struct sockaddr *sap)
2820 {
2821 	static const struct sockaddr_in sin = {
2822 		.sin_family		= AF_INET,
2823 		.sin_addr.s_addr	= htonl(INADDR_ANY),
2824 	};
2825 	static const struct sockaddr_in6 sin6 = {
2826 		.sin6_family		= AF_INET6,
2827 		.sin6_addr		= IN6ADDR_ANY_INIT,
2828 	};
2829 
2830 	switch (family) {
2831 	case AF_LOCAL:
2832 		break;
2833 	case AF_INET:
2834 		memcpy(sap, &sin, sizeof(sin));
2835 		break;
2836 	case AF_INET6:
2837 		memcpy(sap, &sin6, sizeof(sin6));
2838 		break;
2839 	default:
2840 		dprintk("RPC:       %s: Bad address family\n", __func__);
2841 		return -EAFNOSUPPORT;
2842 	}
2843 	return 0;
2844 }
2845 
2846 static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args,
2847 				      unsigned int slot_table_size,
2848 				      unsigned int max_slot_table_size)
2849 {
2850 	struct rpc_xprt *xprt;
2851 	struct sock_xprt *new;
2852 
2853 	if (args->addrlen > sizeof(xprt->addr)) {
2854 		dprintk("RPC:       xs_setup_xprt: address too large\n");
2855 		return ERR_PTR(-EBADF);
2856 	}
2857 
2858 	xprt = xprt_alloc(args->net, sizeof(*new), slot_table_size,
2859 			max_slot_table_size);
2860 	if (xprt == NULL) {
2861 		dprintk("RPC:       xs_setup_xprt: couldn't allocate "
2862 				"rpc_xprt\n");
2863 		return ERR_PTR(-ENOMEM);
2864 	}
2865 
2866 	new = container_of(xprt, struct sock_xprt, xprt);
2867 	mutex_init(&new->recv_mutex);
2868 	memcpy(&xprt->addr, args->dstaddr, args->addrlen);
2869 	xprt->addrlen = args->addrlen;
2870 	if (args->srcaddr)
2871 		memcpy(&new->srcaddr, args->srcaddr, args->addrlen);
2872 	else {
2873 		int err;
2874 		err = xs_init_anyaddr(args->dstaddr->sa_family,
2875 					(struct sockaddr *)&new->srcaddr);
2876 		if (err != 0) {
2877 			xprt_free(xprt);
2878 			return ERR_PTR(err);
2879 		}
2880 	}
2881 
2882 	return xprt;
2883 }
2884 
2885 static const struct rpc_timeout xs_local_default_timeout = {
2886 	.to_initval = 10 * HZ,
2887 	.to_maxval = 10 * HZ,
2888 	.to_retries = 2,
2889 };
2890 
2891 /**
2892  * xs_setup_local - Set up transport to use an AF_LOCAL socket
2893  * @args: rpc transport creation arguments
2894  *
2895  * AF_LOCAL is a "tpi_cots_ord" transport, just like TCP
2896  */
2897 static struct rpc_xprt *xs_setup_local(struct xprt_create *args)
2898 {
2899 	struct sockaddr_un *sun = (struct sockaddr_un *)args->dstaddr;
2900 	struct sock_xprt *transport;
2901 	struct rpc_xprt *xprt;
2902 	struct rpc_xprt *ret;
2903 
2904 	xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
2905 			xprt_max_tcp_slot_table_entries);
2906 	if (IS_ERR(xprt))
2907 		return xprt;
2908 	transport = container_of(xprt, struct sock_xprt, xprt);
2909 
2910 	xprt->prot = 0;
2911 	xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2912 
2913 	xprt->bind_timeout = XS_BIND_TO;
2914 	xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2915 	xprt->idle_timeout = XS_IDLE_DISC_TO;
2916 
2917 	xprt->ops = &xs_local_ops;
2918 	xprt->timeout = &xs_local_default_timeout;
2919 
2920 	INIT_WORK(&transport->recv_worker, xs_stream_data_receive_workfn);
2921 	INIT_WORK(&transport->error_worker, xs_error_handle);
2922 	INIT_DELAYED_WORK(&transport->connect_worker, xs_dummy_setup_socket);
2923 
2924 	switch (sun->sun_family) {
2925 	case AF_LOCAL:
2926 		if (sun->sun_path[0] != '/') {
2927 			dprintk("RPC:       bad AF_LOCAL address: %s\n",
2928 					sun->sun_path);
2929 			ret = ERR_PTR(-EINVAL);
2930 			goto out_err;
2931 		}
2932 		xprt_set_bound(xprt);
2933 		xs_format_peer_addresses(xprt, "local", RPCBIND_NETID_LOCAL);
2934 		ret = ERR_PTR(xs_local_setup_socket(transport));
2935 		if (ret)
2936 			goto out_err;
2937 		break;
2938 	default:
2939 		ret = ERR_PTR(-EAFNOSUPPORT);
2940 		goto out_err;
2941 	}
2942 
2943 	dprintk("RPC:       set up xprt to %s via AF_LOCAL\n",
2944 			xprt->address_strings[RPC_DISPLAY_ADDR]);
2945 
2946 	if (try_module_get(THIS_MODULE))
2947 		return xprt;
2948 	ret = ERR_PTR(-EINVAL);
2949 out_err:
2950 	xs_xprt_free(xprt);
2951 	return ret;
2952 }
2953 
2954 static const struct rpc_timeout xs_udp_default_timeout = {
2955 	.to_initval = 5 * HZ,
2956 	.to_maxval = 30 * HZ,
2957 	.to_increment = 5 * HZ,
2958 	.to_retries = 5,
2959 };
2960 
2961 /**
2962  * xs_setup_udp - Set up transport to use a UDP socket
2963  * @args: rpc transport creation arguments
2964  *
2965  */
2966 static struct rpc_xprt *xs_setup_udp(struct xprt_create *args)
2967 {
2968 	struct sockaddr *addr = args->dstaddr;
2969 	struct rpc_xprt *xprt;
2970 	struct sock_xprt *transport;
2971 	struct rpc_xprt *ret;
2972 
2973 	xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries,
2974 			xprt_udp_slot_table_entries);
2975 	if (IS_ERR(xprt))
2976 		return xprt;
2977 	transport = container_of(xprt, struct sock_xprt, xprt);
2978 
2979 	xprt->prot = IPPROTO_UDP;
2980 	/* XXX: header size can vary due to auth type, IPv6, etc. */
2981 	xprt->max_payload = (1U << 16) - (MAX_HEADER << 3);
2982 
2983 	xprt->bind_timeout = XS_BIND_TO;
2984 	xprt->reestablish_timeout = XS_UDP_REEST_TO;
2985 	xprt->idle_timeout = XS_IDLE_DISC_TO;
2986 
2987 	xprt->ops = &xs_udp_ops;
2988 
2989 	xprt->timeout = &xs_udp_default_timeout;
2990 
2991 	INIT_WORK(&transport->recv_worker, xs_udp_data_receive_workfn);
2992 	INIT_WORK(&transport->error_worker, xs_error_handle);
2993 	INIT_DELAYED_WORK(&transport->connect_worker, xs_udp_setup_socket);
2994 
2995 	switch (addr->sa_family) {
2996 	case AF_INET:
2997 		if (((struct sockaddr_in *)addr)->sin_port != htons(0))
2998 			xprt_set_bound(xprt);
2999 
3000 		xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP);
3001 		break;
3002 	case AF_INET6:
3003 		if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
3004 			xprt_set_bound(xprt);
3005 
3006 		xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6);
3007 		break;
3008 	default:
3009 		ret = ERR_PTR(-EAFNOSUPPORT);
3010 		goto out_err;
3011 	}
3012 
3013 	if (xprt_bound(xprt))
3014 		dprintk("RPC:       set up xprt to %s (port %s) via %s\n",
3015 				xprt->address_strings[RPC_DISPLAY_ADDR],
3016 				xprt->address_strings[RPC_DISPLAY_PORT],
3017 				xprt->address_strings[RPC_DISPLAY_PROTO]);
3018 	else
3019 		dprintk("RPC:       set up xprt to %s (autobind) via %s\n",
3020 				xprt->address_strings[RPC_DISPLAY_ADDR],
3021 				xprt->address_strings[RPC_DISPLAY_PROTO]);
3022 
3023 	if (try_module_get(THIS_MODULE))
3024 		return xprt;
3025 	ret = ERR_PTR(-EINVAL);
3026 out_err:
3027 	xs_xprt_free(xprt);
3028 	return ret;
3029 }
3030 
3031 static const struct rpc_timeout xs_tcp_default_timeout = {
3032 	.to_initval = 60 * HZ,
3033 	.to_maxval = 60 * HZ,
3034 	.to_retries = 2,
3035 };
3036 
3037 /**
3038  * xs_setup_tcp - Set up transport to use a TCP socket
3039  * @args: rpc transport creation arguments
3040  *
3041  */
3042 static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args)
3043 {
3044 	struct sockaddr *addr = args->dstaddr;
3045 	struct rpc_xprt *xprt;
3046 	struct sock_xprt *transport;
3047 	struct rpc_xprt *ret;
3048 	unsigned int max_slot_table_size = xprt_max_tcp_slot_table_entries;
3049 
3050 	if (args->flags & XPRT_CREATE_INFINITE_SLOTS)
3051 		max_slot_table_size = RPC_MAX_SLOT_TABLE_LIMIT;
3052 
3053 	xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
3054 			max_slot_table_size);
3055 	if (IS_ERR(xprt))
3056 		return xprt;
3057 	transport = container_of(xprt, struct sock_xprt, xprt);
3058 
3059 	xprt->prot = IPPROTO_TCP;
3060 	xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
3061 
3062 	xprt->bind_timeout = XS_BIND_TO;
3063 	xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
3064 	xprt->idle_timeout = XS_IDLE_DISC_TO;
3065 
3066 	xprt->ops = &xs_tcp_ops;
3067 	xprt->timeout = &xs_tcp_default_timeout;
3068 
3069 	xprt->max_reconnect_timeout = xprt->timeout->to_maxval;
3070 	xprt->connect_timeout = xprt->timeout->to_initval *
3071 		(xprt->timeout->to_retries + 1);
3072 
3073 	INIT_WORK(&transport->recv_worker, xs_stream_data_receive_workfn);
3074 	INIT_WORK(&transport->error_worker, xs_error_handle);
3075 	INIT_DELAYED_WORK(&transport->connect_worker, xs_tcp_setup_socket);
3076 
3077 	switch (addr->sa_family) {
3078 	case AF_INET:
3079 		if (((struct sockaddr_in *)addr)->sin_port != htons(0))
3080 			xprt_set_bound(xprt);
3081 
3082 		xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP);
3083 		break;
3084 	case AF_INET6:
3085 		if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
3086 			xprt_set_bound(xprt);
3087 
3088 		xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6);
3089 		break;
3090 	default:
3091 		ret = ERR_PTR(-EAFNOSUPPORT);
3092 		goto out_err;
3093 	}
3094 
3095 	if (xprt_bound(xprt))
3096 		dprintk("RPC:       set up xprt to %s (port %s) via %s\n",
3097 				xprt->address_strings[RPC_DISPLAY_ADDR],
3098 				xprt->address_strings[RPC_DISPLAY_PORT],
3099 				xprt->address_strings[RPC_DISPLAY_PROTO]);
3100 	else
3101 		dprintk("RPC:       set up xprt to %s (autobind) via %s\n",
3102 				xprt->address_strings[RPC_DISPLAY_ADDR],
3103 				xprt->address_strings[RPC_DISPLAY_PROTO]);
3104 
3105 	if (try_module_get(THIS_MODULE))
3106 		return xprt;
3107 	ret = ERR_PTR(-EINVAL);
3108 out_err:
3109 	xs_xprt_free(xprt);
3110 	return ret;
3111 }
3112 
3113 /**
3114  * xs_setup_bc_tcp - Set up transport to use a TCP backchannel socket
3115  * @args: rpc transport creation arguments
3116  *
3117  */
3118 static struct rpc_xprt *xs_setup_bc_tcp(struct xprt_create *args)
3119 {
3120 	struct sockaddr *addr = args->dstaddr;
3121 	struct rpc_xprt *xprt;
3122 	struct sock_xprt *transport;
3123 	struct svc_sock *bc_sock;
3124 	struct rpc_xprt *ret;
3125 
3126 	xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
3127 			xprt_tcp_slot_table_entries);
3128 	if (IS_ERR(xprt))
3129 		return xprt;
3130 	transport = container_of(xprt, struct sock_xprt, xprt);
3131 
3132 	xprt->prot = IPPROTO_TCP;
3133 	xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
3134 	xprt->timeout = &xs_tcp_default_timeout;
3135 
3136 	/* backchannel */
3137 	xprt_set_bound(xprt);
3138 	xprt->bind_timeout = 0;
3139 	xprt->reestablish_timeout = 0;
3140 	xprt->idle_timeout = 0;
3141 
3142 	xprt->ops = &bc_tcp_ops;
3143 
3144 	switch (addr->sa_family) {
3145 	case AF_INET:
3146 		xs_format_peer_addresses(xprt, "tcp",
3147 					 RPCBIND_NETID_TCP);
3148 		break;
3149 	case AF_INET6:
3150 		xs_format_peer_addresses(xprt, "tcp",
3151 				   RPCBIND_NETID_TCP6);
3152 		break;
3153 	default:
3154 		ret = ERR_PTR(-EAFNOSUPPORT);
3155 		goto out_err;
3156 	}
3157 
3158 	dprintk("RPC:       set up xprt to %s (port %s) via %s\n",
3159 			xprt->address_strings[RPC_DISPLAY_ADDR],
3160 			xprt->address_strings[RPC_DISPLAY_PORT],
3161 			xprt->address_strings[RPC_DISPLAY_PROTO]);
3162 
3163 	/*
3164 	 * Once we've associated a backchannel xprt with a connection,
3165 	 * we want to keep it around as long as the connection lasts,
3166 	 * in case we need to start using it for a backchannel again;
3167 	 * this reference won't be dropped until bc_xprt is destroyed.
3168 	 */
3169 	xprt_get(xprt);
3170 	args->bc_xprt->xpt_bc_xprt = xprt;
3171 	xprt->bc_xprt = args->bc_xprt;
3172 	bc_sock = container_of(args->bc_xprt, struct svc_sock, sk_xprt);
3173 	transport->sock = bc_sock->sk_sock;
3174 	transport->inet = bc_sock->sk_sk;
3175 
3176 	/*
3177 	 * Since we don't want connections for the backchannel, we set
3178 	 * the xprt status to connected
3179 	 */
3180 	xprt_set_connected(xprt);
3181 
3182 	if (try_module_get(THIS_MODULE))
3183 		return xprt;
3184 
3185 	args->bc_xprt->xpt_bc_xprt = NULL;
3186 	args->bc_xprt->xpt_bc_xps = NULL;
3187 	xprt_put(xprt);
3188 	ret = ERR_PTR(-EINVAL);
3189 out_err:
3190 	xs_xprt_free(xprt);
3191 	return ret;
3192 }
3193 
3194 static struct xprt_class	xs_local_transport = {
3195 	.list		= LIST_HEAD_INIT(xs_local_transport.list),
3196 	.name		= "named UNIX socket",
3197 	.owner		= THIS_MODULE,
3198 	.ident		= XPRT_TRANSPORT_LOCAL,
3199 	.setup		= xs_setup_local,
3200 };
3201 
3202 static struct xprt_class	xs_udp_transport = {
3203 	.list		= LIST_HEAD_INIT(xs_udp_transport.list),
3204 	.name		= "udp",
3205 	.owner		= THIS_MODULE,
3206 	.ident		= XPRT_TRANSPORT_UDP,
3207 	.setup		= xs_setup_udp,
3208 };
3209 
3210 static struct xprt_class	xs_tcp_transport = {
3211 	.list		= LIST_HEAD_INIT(xs_tcp_transport.list),
3212 	.name		= "tcp",
3213 	.owner		= THIS_MODULE,
3214 	.ident		= XPRT_TRANSPORT_TCP,
3215 	.setup		= xs_setup_tcp,
3216 };
3217 
3218 static struct xprt_class	xs_bc_tcp_transport = {
3219 	.list		= LIST_HEAD_INIT(xs_bc_tcp_transport.list),
3220 	.name		= "tcp NFSv4.1 backchannel",
3221 	.owner		= THIS_MODULE,
3222 	.ident		= XPRT_TRANSPORT_BC_TCP,
3223 	.setup		= xs_setup_bc_tcp,
3224 };
3225 
3226 /**
3227  * init_socket_xprt - set up xprtsock's sysctls, register with RPC client
3228  *
3229  */
3230 int init_socket_xprt(void)
3231 {
3232 	if (!sunrpc_table_header)
3233 		sunrpc_table_header = register_sysctl_table(sunrpc_table);
3234 
3235 	xprt_register_transport(&xs_local_transport);
3236 	xprt_register_transport(&xs_udp_transport);
3237 	xprt_register_transport(&xs_tcp_transport);
3238 	xprt_register_transport(&xs_bc_tcp_transport);
3239 
3240 	return 0;
3241 }
3242 
3243 /**
3244  * cleanup_socket_xprt - remove xprtsock's sysctls, unregister
3245  *
3246  */
3247 void cleanup_socket_xprt(void)
3248 {
3249 	if (sunrpc_table_header) {
3250 		unregister_sysctl_table(sunrpc_table_header);
3251 		sunrpc_table_header = NULL;
3252 	}
3253 
3254 	xprt_unregister_transport(&xs_local_transport);
3255 	xprt_unregister_transport(&xs_udp_transport);
3256 	xprt_unregister_transport(&xs_tcp_transport);
3257 	xprt_unregister_transport(&xs_bc_tcp_transport);
3258 }
3259 
3260 static int param_set_uint_minmax(const char *val,
3261 		const struct kernel_param *kp,
3262 		unsigned int min, unsigned int max)
3263 {
3264 	unsigned int num;
3265 	int ret;
3266 
3267 	if (!val)
3268 		return -EINVAL;
3269 	ret = kstrtouint(val, 0, &num);
3270 	if (ret)
3271 		return ret;
3272 	if (num < min || num > max)
3273 		return -EINVAL;
3274 	*((unsigned int *)kp->arg) = num;
3275 	return 0;
3276 }
3277 
3278 static int param_set_portnr(const char *val, const struct kernel_param *kp)
3279 {
3280 	return param_set_uint_minmax(val, kp,
3281 			RPC_MIN_RESVPORT,
3282 			RPC_MAX_RESVPORT);
3283 }
3284 
3285 static const struct kernel_param_ops param_ops_portnr = {
3286 	.set = param_set_portnr,
3287 	.get = param_get_uint,
3288 };
3289 
3290 #define param_check_portnr(name, p) \
3291 	__param_check(name, p, unsigned int);
3292 
3293 module_param_named(min_resvport, xprt_min_resvport, portnr, 0644);
3294 module_param_named(max_resvport, xprt_max_resvport, portnr, 0644);
3295 
3296 static int param_set_slot_table_size(const char *val,
3297 				     const struct kernel_param *kp)
3298 {
3299 	return param_set_uint_minmax(val, kp,
3300 			RPC_MIN_SLOT_TABLE,
3301 			RPC_MAX_SLOT_TABLE);
3302 }
3303 
3304 static const struct kernel_param_ops param_ops_slot_table_size = {
3305 	.set = param_set_slot_table_size,
3306 	.get = param_get_uint,
3307 };
3308 
3309 #define param_check_slot_table_size(name, p) \
3310 	__param_check(name, p, unsigned int);
3311 
3312 static int param_set_max_slot_table_size(const char *val,
3313 				     const struct kernel_param *kp)
3314 {
3315 	return param_set_uint_minmax(val, kp,
3316 			RPC_MIN_SLOT_TABLE,
3317 			RPC_MAX_SLOT_TABLE_LIMIT);
3318 }
3319 
3320 static const struct kernel_param_ops param_ops_max_slot_table_size = {
3321 	.set = param_set_max_slot_table_size,
3322 	.get = param_get_uint,
3323 };
3324 
3325 #define param_check_max_slot_table_size(name, p) \
3326 	__param_check(name, p, unsigned int);
3327 
3328 module_param_named(tcp_slot_table_entries, xprt_tcp_slot_table_entries,
3329 		   slot_table_size, 0644);
3330 module_param_named(tcp_max_slot_table_entries, xprt_max_tcp_slot_table_entries,
3331 		   max_slot_table_size, 0644);
3332 module_param_named(udp_slot_table_entries, xprt_udp_slot_table_entries,
3333 		   slot_table_size, 0644);
3334