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