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