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