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