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