nfsd4: typo logical vs bitwise negate for want_mask
[linux-btrfs-devel.git] / net / sunrpc / xprtsock.c
blobd7f97ef265904f0ddc0d691360f1e920dc63210b
1 /*
2 * linux/net/sunrpc/xprtsock.c
4 * Client-side transport implementation for sockets.
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>
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>
15 * IP socket transport implementation, (C) 2005 Chuck Lever <cel@netapp.com>
17 * IPv6 support contributed by Gilles Quillard, Bull Open Source, 2005.
18 * <gilles.quillard@bull.net>
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/sched.h>
37 #include <linux/sunrpc/svcsock.h>
38 #include <linux/sunrpc/xprtsock.h>
39 #include <linux/file.h>
40 #ifdef CONFIG_SUNRPC_BACKCHANNEL
41 #include <linux/sunrpc/bc_xprt.h>
42 #endif
44 #include <net/sock.h>
45 #include <net/checksum.h>
46 #include <net/udp.h>
47 #include <net/tcp.h>
49 #include "sunrpc.h"
51 static void xs_close(struct rpc_xprt *xprt);
54 * xprtsock tunables
56 unsigned int xprt_udp_slot_table_entries = RPC_DEF_SLOT_TABLE;
57 unsigned int xprt_tcp_slot_table_entries = RPC_MIN_SLOT_TABLE;
58 unsigned int xprt_max_tcp_slot_table_entries = RPC_MAX_SLOT_TABLE;
60 unsigned int xprt_min_resvport = RPC_DEF_MIN_RESVPORT;
61 unsigned int xprt_max_resvport = RPC_DEF_MAX_RESVPORT;
63 #define XS_TCP_LINGER_TO (15U * HZ)
64 static unsigned int xs_tcp_fin_timeout __read_mostly = XS_TCP_LINGER_TO;
67 * We can register our own files under /proc/sys/sunrpc by
68 * calling register_sysctl_table() again. The files in that
69 * directory become the union of all files registered there.
71 * We simply need to make sure that we don't collide with
72 * someone else's file names!
75 #ifdef RPC_DEBUG
77 static unsigned int min_slot_table_size = RPC_MIN_SLOT_TABLE;
78 static unsigned int max_slot_table_size = RPC_MAX_SLOT_TABLE;
79 static unsigned int max_tcp_slot_table_limit = RPC_MAX_SLOT_TABLE_LIMIT;
80 static unsigned int xprt_min_resvport_limit = RPC_MIN_RESVPORT;
81 static unsigned int xprt_max_resvport_limit = RPC_MAX_RESVPORT;
83 static struct ctl_table_header *sunrpc_table_header;
86 * FIXME: changing the UDP slot table size should also resize the UDP
87 * socket buffers for existing UDP transports
89 static ctl_table xs_tunables_table[] = {
91 .procname = "udp_slot_table_entries",
92 .data = &xprt_udp_slot_table_entries,
93 .maxlen = sizeof(unsigned int),
94 .mode = 0644,
95 .proc_handler = proc_dointvec_minmax,
96 .extra1 = &min_slot_table_size,
97 .extra2 = &max_slot_table_size
100 .procname = "tcp_slot_table_entries",
101 .data = &xprt_tcp_slot_table_entries,
102 .maxlen = sizeof(unsigned int),
103 .mode = 0644,
104 .proc_handler = proc_dointvec_minmax,
105 .extra1 = &min_slot_table_size,
106 .extra2 = &max_slot_table_size
109 .procname = "tcp_max_slot_table_entries",
110 .data = &xprt_max_tcp_slot_table_entries,
111 .maxlen = sizeof(unsigned int),
112 .mode = 0644,
113 .proc_handler = proc_dointvec_minmax,
114 .extra1 = &min_slot_table_size,
115 .extra2 = &max_tcp_slot_table_limit
118 .procname = "min_resvport",
119 .data = &xprt_min_resvport,
120 .maxlen = sizeof(unsigned int),
121 .mode = 0644,
122 .proc_handler = proc_dointvec_minmax,
123 .extra1 = &xprt_min_resvport_limit,
124 .extra2 = &xprt_max_resvport_limit
127 .procname = "max_resvport",
128 .data = &xprt_max_resvport,
129 .maxlen = sizeof(unsigned int),
130 .mode = 0644,
131 .proc_handler = proc_dointvec_minmax,
132 .extra1 = &xprt_min_resvport_limit,
133 .extra2 = &xprt_max_resvport_limit
136 .procname = "tcp_fin_timeout",
137 .data = &xs_tcp_fin_timeout,
138 .maxlen = sizeof(xs_tcp_fin_timeout),
139 .mode = 0644,
140 .proc_handler = proc_dointvec_jiffies,
142 { },
145 static ctl_table sunrpc_table[] = {
147 .procname = "sunrpc",
148 .mode = 0555,
149 .child = xs_tunables_table
151 { },
154 #endif
157 * Wait duration for a reply from the RPC portmapper.
159 #define XS_BIND_TO (60U * HZ)
162 * Delay if a UDP socket connect error occurs. This is most likely some
163 * kind of resource problem on the local host.
165 #define XS_UDP_REEST_TO (2U * HZ)
168 * The reestablish timeout allows clients to delay for a bit before attempting
169 * to reconnect to a server that just dropped our connection.
171 * We implement an exponential backoff when trying to reestablish a TCP
172 * transport connection with the server. Some servers like to drop a TCP
173 * connection when they are overworked, so we start with a short timeout and
174 * increase over time if the server is down or not responding.
176 #define XS_TCP_INIT_REEST_TO (3U * HZ)
177 #define XS_TCP_MAX_REEST_TO (5U * 60 * HZ)
180 * TCP idle timeout; client drops the transport socket if it is idle
181 * for this long. Note that we also timeout UDP sockets to prevent
182 * holding port numbers when there is no RPC traffic.
184 #define XS_IDLE_DISC_TO (5U * 60 * HZ)
186 #ifdef RPC_DEBUG
187 # undef RPC_DEBUG_DATA
188 # define RPCDBG_FACILITY RPCDBG_TRANS
189 #endif
191 #ifdef RPC_DEBUG_DATA
192 static void xs_pktdump(char *msg, u32 *packet, unsigned int count)
194 u8 *buf = (u8 *) packet;
195 int j;
197 dprintk("RPC: %s\n", msg);
198 for (j = 0; j < count && j < 128; j += 4) {
199 if (!(j & 31)) {
200 if (j)
201 dprintk("\n");
202 dprintk("0x%04x ", j);
204 dprintk("%02x%02x%02x%02x ",
205 buf[j], buf[j+1], buf[j+2], buf[j+3]);
207 dprintk("\n");
209 #else
210 static inline void xs_pktdump(char *msg, u32 *packet, unsigned int count)
212 /* NOP */
214 #endif
216 struct sock_xprt {
217 struct rpc_xprt xprt;
220 * Network layer
222 struct socket * sock;
223 struct sock * inet;
226 * State of TCP reply receive
228 __be32 tcp_fraghdr,
229 tcp_xid,
230 tcp_calldir;
232 u32 tcp_offset,
233 tcp_reclen;
235 unsigned long tcp_copied,
236 tcp_flags;
239 * Connection of transports
241 struct delayed_work connect_worker;
242 struct sockaddr_storage srcaddr;
243 unsigned short srcport;
246 * UDP socket buffer size parameters
248 size_t rcvsize,
249 sndsize;
252 * Saved socket callback addresses
254 void (*old_data_ready)(struct sock *, int);
255 void (*old_state_change)(struct sock *);
256 void (*old_write_space)(struct sock *);
257 void (*old_error_report)(struct sock *);
261 * TCP receive state flags
263 #define TCP_RCV_LAST_FRAG (1UL << 0)
264 #define TCP_RCV_COPY_FRAGHDR (1UL << 1)
265 #define TCP_RCV_COPY_XID (1UL << 2)
266 #define TCP_RCV_COPY_DATA (1UL << 3)
267 #define TCP_RCV_READ_CALLDIR (1UL << 4)
268 #define TCP_RCV_COPY_CALLDIR (1UL << 5)
271 * TCP RPC flags
273 #define TCP_RPC_REPLY (1UL << 6)
275 static inline struct sockaddr *xs_addr(struct rpc_xprt *xprt)
277 return (struct sockaddr *) &xprt->addr;
280 static inline struct sockaddr_un *xs_addr_un(struct rpc_xprt *xprt)
282 return (struct sockaddr_un *) &xprt->addr;
285 static inline struct sockaddr_in *xs_addr_in(struct rpc_xprt *xprt)
287 return (struct sockaddr_in *) &xprt->addr;
290 static inline struct sockaddr_in6 *xs_addr_in6(struct rpc_xprt *xprt)
292 return (struct sockaddr_in6 *) &xprt->addr;
295 static void xs_format_common_peer_addresses(struct rpc_xprt *xprt)
297 struct sockaddr *sap = xs_addr(xprt);
298 struct sockaddr_in6 *sin6;
299 struct sockaddr_in *sin;
300 struct sockaddr_un *sun;
301 char buf[128];
303 switch (sap->sa_family) {
304 case AF_LOCAL:
305 sun = xs_addr_un(xprt);
306 strlcpy(buf, sun->sun_path, sizeof(buf));
307 xprt->address_strings[RPC_DISPLAY_ADDR] =
308 kstrdup(buf, GFP_KERNEL);
309 break;
310 case AF_INET:
311 (void)rpc_ntop(sap, buf, sizeof(buf));
312 xprt->address_strings[RPC_DISPLAY_ADDR] =
313 kstrdup(buf, GFP_KERNEL);
314 sin = xs_addr_in(xprt);
315 snprintf(buf, sizeof(buf), "%08x", ntohl(sin->sin_addr.s_addr));
316 break;
317 case AF_INET6:
318 (void)rpc_ntop(sap, buf, sizeof(buf));
319 xprt->address_strings[RPC_DISPLAY_ADDR] =
320 kstrdup(buf, GFP_KERNEL);
321 sin6 = xs_addr_in6(xprt);
322 snprintf(buf, sizeof(buf), "%pi6", &sin6->sin6_addr);
323 break;
324 default:
325 BUG();
328 xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = kstrdup(buf, GFP_KERNEL);
331 static void xs_format_common_peer_ports(struct rpc_xprt *xprt)
333 struct sockaddr *sap = xs_addr(xprt);
334 char buf[128];
336 snprintf(buf, sizeof(buf), "%u", rpc_get_port(sap));
337 xprt->address_strings[RPC_DISPLAY_PORT] = kstrdup(buf, GFP_KERNEL);
339 snprintf(buf, sizeof(buf), "%4hx", rpc_get_port(sap));
340 xprt->address_strings[RPC_DISPLAY_HEX_PORT] = kstrdup(buf, GFP_KERNEL);
343 static void xs_format_peer_addresses(struct rpc_xprt *xprt,
344 const char *protocol,
345 const char *netid)
347 xprt->address_strings[RPC_DISPLAY_PROTO] = protocol;
348 xprt->address_strings[RPC_DISPLAY_NETID] = netid;
349 xs_format_common_peer_addresses(xprt);
350 xs_format_common_peer_ports(xprt);
353 static void xs_update_peer_port(struct rpc_xprt *xprt)
355 kfree(xprt->address_strings[RPC_DISPLAY_HEX_PORT]);
356 kfree(xprt->address_strings[RPC_DISPLAY_PORT]);
358 xs_format_common_peer_ports(xprt);
361 static void xs_free_peer_addresses(struct rpc_xprt *xprt)
363 unsigned int i;
365 for (i = 0; i < RPC_DISPLAY_MAX; i++)
366 switch (i) {
367 case RPC_DISPLAY_PROTO:
368 case RPC_DISPLAY_NETID:
369 continue;
370 default:
371 kfree(xprt->address_strings[i]);
375 #define XS_SENDMSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL)
377 static int xs_send_kvec(struct socket *sock, struct sockaddr *addr, int addrlen, struct kvec *vec, unsigned int base, int more)
379 struct msghdr msg = {
380 .msg_name = addr,
381 .msg_namelen = addrlen,
382 .msg_flags = XS_SENDMSG_FLAGS | (more ? MSG_MORE : 0),
384 struct kvec iov = {
385 .iov_base = vec->iov_base + base,
386 .iov_len = vec->iov_len - base,
389 if (iov.iov_len != 0)
390 return kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len);
391 return kernel_sendmsg(sock, &msg, NULL, 0, 0);
394 static int xs_send_pagedata(struct socket *sock, struct xdr_buf *xdr, unsigned int base, int more)
396 struct page **ppage;
397 unsigned int remainder;
398 int err, sent = 0;
400 remainder = xdr->page_len - base;
401 base += xdr->page_base;
402 ppage = xdr->pages + (base >> PAGE_SHIFT);
403 base &= ~PAGE_MASK;
404 for(;;) {
405 unsigned int len = min_t(unsigned int, PAGE_SIZE - base, remainder);
406 int flags = XS_SENDMSG_FLAGS;
408 remainder -= len;
409 if (remainder != 0 || more)
410 flags |= MSG_MORE;
411 err = sock->ops->sendpage(sock, *ppage, base, len, flags);
412 if (remainder == 0 || err != len)
413 break;
414 sent += err;
415 ppage++;
416 base = 0;
418 if (sent == 0)
419 return err;
420 if (err > 0)
421 sent += err;
422 return sent;
426 * xs_sendpages - write pages directly to a socket
427 * @sock: socket to send on
428 * @addr: UDP only -- address of destination
429 * @addrlen: UDP only -- length of destination address
430 * @xdr: buffer containing this request
431 * @base: starting position in the buffer
434 static int xs_sendpages(struct socket *sock, struct sockaddr *addr, int addrlen, struct xdr_buf *xdr, unsigned int base)
436 unsigned int remainder = xdr->len - base;
437 int err, sent = 0;
439 if (unlikely(!sock))
440 return -ENOTSOCK;
442 clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags);
443 if (base != 0) {
444 addr = NULL;
445 addrlen = 0;
448 if (base < xdr->head[0].iov_len || addr != NULL) {
449 unsigned int len = xdr->head[0].iov_len - base;
450 remainder -= len;
451 err = xs_send_kvec(sock, addr, addrlen, &xdr->head[0], base, remainder != 0);
452 if (remainder == 0 || err != len)
453 goto out;
454 sent += err;
455 base = 0;
456 } else
457 base -= xdr->head[0].iov_len;
459 if (base < xdr->page_len) {
460 unsigned int len = xdr->page_len - base;
461 remainder -= len;
462 err = xs_send_pagedata(sock, xdr, base, remainder != 0);
463 if (remainder == 0 || err != len)
464 goto out;
465 sent += err;
466 base = 0;
467 } else
468 base -= xdr->page_len;
470 if (base >= xdr->tail[0].iov_len)
471 return sent;
472 err = xs_send_kvec(sock, NULL, 0, &xdr->tail[0], base, 0);
473 out:
474 if (sent == 0)
475 return err;
476 if (err > 0)
477 sent += err;
478 return sent;
481 static void xs_nospace_callback(struct rpc_task *task)
483 struct sock_xprt *transport = container_of(task->tk_rqstp->rq_xprt, struct sock_xprt, xprt);
485 transport->inet->sk_write_pending--;
486 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
490 * xs_nospace - place task on wait queue if transmit was incomplete
491 * @task: task to put to sleep
494 static int xs_nospace(struct rpc_task *task)
496 struct rpc_rqst *req = task->tk_rqstp;
497 struct rpc_xprt *xprt = req->rq_xprt;
498 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
499 int ret = 0;
501 dprintk("RPC: %5u xmit incomplete (%u left of %u)\n",
502 task->tk_pid, req->rq_slen - req->rq_bytes_sent,
503 req->rq_slen);
505 /* Protect against races with write_space */
506 spin_lock_bh(&xprt->transport_lock);
508 /* Don't race with disconnect */
509 if (xprt_connected(xprt)) {
510 if (test_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags)) {
511 ret = -EAGAIN;
513 * Notify TCP that we're limited by the application
514 * window size
516 set_bit(SOCK_NOSPACE, &transport->sock->flags);
517 transport->inet->sk_write_pending++;
518 /* ...and wait for more buffer space */
519 xprt_wait_for_buffer_space(task, xs_nospace_callback);
521 } else {
522 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
523 ret = -ENOTCONN;
526 spin_unlock_bh(&xprt->transport_lock);
527 return ret;
531 * Construct a stream transport record marker in @buf.
533 static inline void xs_encode_stream_record_marker(struct xdr_buf *buf)
535 u32 reclen = buf->len - sizeof(rpc_fraghdr);
536 rpc_fraghdr *base = buf->head[0].iov_base;
537 *base = cpu_to_be32(RPC_LAST_STREAM_FRAGMENT | reclen);
541 * xs_local_send_request - write an RPC request to an AF_LOCAL socket
542 * @task: RPC task that manages the state of an RPC request
544 * Return values:
545 * 0: The request has been sent
546 * EAGAIN: The socket was blocked, please call again later to
547 * complete the request
548 * ENOTCONN: Caller needs to invoke connect logic then call again
549 * other: Some other error occured, the request was not sent
551 static int xs_local_send_request(struct rpc_task *task)
553 struct rpc_rqst *req = task->tk_rqstp;
554 struct rpc_xprt *xprt = req->rq_xprt;
555 struct sock_xprt *transport =
556 container_of(xprt, struct sock_xprt, xprt);
557 struct xdr_buf *xdr = &req->rq_snd_buf;
558 int status;
560 xs_encode_stream_record_marker(&req->rq_snd_buf);
562 xs_pktdump("packet data:",
563 req->rq_svec->iov_base, req->rq_svec->iov_len);
565 status = xs_sendpages(transport->sock, NULL, 0,
566 xdr, req->rq_bytes_sent);
567 dprintk("RPC: %s(%u) = %d\n",
568 __func__, xdr->len - req->rq_bytes_sent, status);
569 if (likely(status >= 0)) {
570 req->rq_bytes_sent += status;
571 req->rq_xmit_bytes_sent += status;
572 if (likely(req->rq_bytes_sent >= req->rq_slen)) {
573 req->rq_bytes_sent = 0;
574 return 0;
576 status = -EAGAIN;
579 switch (status) {
580 case -EAGAIN:
581 status = xs_nospace(task);
582 break;
583 default:
584 dprintk("RPC: sendmsg returned unrecognized error %d\n",
585 -status);
586 case -EPIPE:
587 xs_close(xprt);
588 status = -ENOTCONN;
591 return status;
595 * xs_udp_send_request - write an RPC request to a UDP socket
596 * @task: address of RPC task that manages the state of an RPC request
598 * Return values:
599 * 0: The request has been sent
600 * EAGAIN: The socket was blocked, please call again later to
601 * complete the request
602 * ENOTCONN: Caller needs to invoke connect logic then call again
603 * other: Some other error occurred, the request was not sent
605 static int xs_udp_send_request(struct rpc_task *task)
607 struct rpc_rqst *req = task->tk_rqstp;
608 struct rpc_xprt *xprt = req->rq_xprt;
609 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
610 struct xdr_buf *xdr = &req->rq_snd_buf;
611 int status;
613 xs_pktdump("packet data:",
614 req->rq_svec->iov_base,
615 req->rq_svec->iov_len);
617 if (!xprt_bound(xprt))
618 return -ENOTCONN;
619 status = xs_sendpages(transport->sock,
620 xs_addr(xprt),
621 xprt->addrlen, xdr,
622 req->rq_bytes_sent);
624 dprintk("RPC: xs_udp_send_request(%u) = %d\n",
625 xdr->len - req->rq_bytes_sent, status);
627 if (status >= 0) {
628 req->rq_xmit_bytes_sent += status;
629 if (status >= req->rq_slen)
630 return 0;
631 /* Still some bytes left; set up for a retry later. */
632 status = -EAGAIN;
635 switch (status) {
636 case -ENOTSOCK:
637 status = -ENOTCONN;
638 /* Should we call xs_close() here? */
639 break;
640 case -EAGAIN:
641 status = xs_nospace(task);
642 break;
643 default:
644 dprintk("RPC: sendmsg returned unrecognized error %d\n",
645 -status);
646 case -ENETUNREACH:
647 case -EPIPE:
648 case -ECONNREFUSED:
649 /* When the server has died, an ICMP port unreachable message
650 * prompts ECONNREFUSED. */
651 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
654 return status;
658 * xs_tcp_shutdown - gracefully shut down a TCP socket
659 * @xprt: transport
661 * Initiates a graceful shutdown of the TCP socket by calling the
662 * equivalent of shutdown(SHUT_WR);
664 static void xs_tcp_shutdown(struct rpc_xprt *xprt)
666 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
667 struct socket *sock = transport->sock;
669 if (sock != NULL)
670 kernel_sock_shutdown(sock, SHUT_WR);
674 * xs_tcp_send_request - write an RPC request to a TCP socket
675 * @task: address of RPC task that manages the state of an RPC request
677 * Return values:
678 * 0: The request has been sent
679 * EAGAIN: The socket was blocked, please call again later to
680 * complete the request
681 * ENOTCONN: Caller needs to invoke connect logic then call again
682 * other: Some other error occurred, the request was not sent
684 * XXX: In the case of soft timeouts, should we eventually give up
685 * if sendmsg is not able to make progress?
687 static int xs_tcp_send_request(struct rpc_task *task)
689 struct rpc_rqst *req = task->tk_rqstp;
690 struct rpc_xprt *xprt = req->rq_xprt;
691 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
692 struct xdr_buf *xdr = &req->rq_snd_buf;
693 int status;
695 xs_encode_stream_record_marker(&req->rq_snd_buf);
697 xs_pktdump("packet data:",
698 req->rq_svec->iov_base,
699 req->rq_svec->iov_len);
701 /* Continue transmitting the packet/record. We must be careful
702 * to cope with writespace callbacks arriving _after_ we have
703 * called sendmsg(). */
704 while (1) {
705 status = xs_sendpages(transport->sock,
706 NULL, 0, xdr, req->rq_bytes_sent);
708 dprintk("RPC: xs_tcp_send_request(%u) = %d\n",
709 xdr->len - req->rq_bytes_sent, status);
711 if (unlikely(status < 0))
712 break;
714 /* If we've sent the entire packet, immediately
715 * reset the count of bytes sent. */
716 req->rq_bytes_sent += status;
717 req->rq_xmit_bytes_sent += status;
718 if (likely(req->rq_bytes_sent >= req->rq_slen)) {
719 req->rq_bytes_sent = 0;
720 return 0;
723 if (status != 0)
724 continue;
725 status = -EAGAIN;
726 break;
729 switch (status) {
730 case -ENOTSOCK:
731 status = -ENOTCONN;
732 /* Should we call xs_close() here? */
733 break;
734 case -EAGAIN:
735 status = xs_nospace(task);
736 break;
737 default:
738 dprintk("RPC: sendmsg returned unrecognized error %d\n",
739 -status);
740 case -ECONNRESET:
741 case -EPIPE:
742 xs_tcp_shutdown(xprt);
743 case -ECONNREFUSED:
744 case -ENOTCONN:
745 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
748 return status;
752 * xs_tcp_release_xprt - clean up after a tcp transmission
753 * @xprt: transport
754 * @task: rpc task
756 * This cleans up if an error causes us to abort the transmission of a request.
757 * In this case, the socket may need to be reset in order to avoid confusing
758 * the server.
760 static void xs_tcp_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
762 struct rpc_rqst *req;
764 if (task != xprt->snd_task)
765 return;
766 if (task == NULL)
767 goto out_release;
768 req = task->tk_rqstp;
769 if (req == NULL)
770 goto out_release;
771 if (req->rq_bytes_sent == 0)
772 goto out_release;
773 if (req->rq_bytes_sent == req->rq_snd_buf.len)
774 goto out_release;
775 set_bit(XPRT_CLOSE_WAIT, &task->tk_xprt->state);
776 out_release:
777 xprt_release_xprt(xprt, task);
780 static void xs_save_old_callbacks(struct sock_xprt *transport, struct sock *sk)
782 transport->old_data_ready = sk->sk_data_ready;
783 transport->old_state_change = sk->sk_state_change;
784 transport->old_write_space = sk->sk_write_space;
785 transport->old_error_report = sk->sk_error_report;
788 static void xs_restore_old_callbacks(struct sock_xprt *transport, struct sock *sk)
790 sk->sk_data_ready = transport->old_data_ready;
791 sk->sk_state_change = transport->old_state_change;
792 sk->sk_write_space = transport->old_write_space;
793 sk->sk_error_report = transport->old_error_report;
796 static void xs_reset_transport(struct sock_xprt *transport)
798 struct socket *sock = transport->sock;
799 struct sock *sk = transport->inet;
801 if (sk == NULL)
802 return;
804 transport->srcport = 0;
806 write_lock_bh(&sk->sk_callback_lock);
807 transport->inet = NULL;
808 transport->sock = NULL;
810 sk->sk_user_data = NULL;
812 xs_restore_old_callbacks(transport, sk);
813 write_unlock_bh(&sk->sk_callback_lock);
815 sk->sk_no_check = 0;
817 sock_release(sock);
821 * xs_close - close a socket
822 * @xprt: transport
824 * This is used when all requests are complete; ie, no DRC state remains
825 * on the server we want to save.
827 * The caller _must_ be holding XPRT_LOCKED in order to avoid issues with
828 * xs_reset_transport() zeroing the socket from underneath a writer.
830 static void xs_close(struct rpc_xprt *xprt)
832 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
834 dprintk("RPC: xs_close xprt %p\n", xprt);
836 xs_reset_transport(transport);
837 xprt->reestablish_timeout = 0;
839 smp_mb__before_clear_bit();
840 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
841 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
842 clear_bit(XPRT_CLOSING, &xprt->state);
843 smp_mb__after_clear_bit();
844 xprt_disconnect_done(xprt);
847 static void xs_tcp_close(struct rpc_xprt *xprt)
849 if (test_and_clear_bit(XPRT_CONNECTION_CLOSE, &xprt->state))
850 xs_close(xprt);
851 else
852 xs_tcp_shutdown(xprt);
856 * xs_destroy - prepare to shutdown a transport
857 * @xprt: doomed transport
860 static void xs_destroy(struct rpc_xprt *xprt)
862 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
864 dprintk("RPC: xs_destroy xprt %p\n", xprt);
866 cancel_delayed_work_sync(&transport->connect_worker);
868 xs_close(xprt);
869 xs_free_peer_addresses(xprt);
870 xprt_free(xprt);
871 module_put(THIS_MODULE);
874 static inline struct rpc_xprt *xprt_from_sock(struct sock *sk)
876 return (struct rpc_xprt *) sk->sk_user_data;
879 static int xs_local_copy_to_xdr(struct xdr_buf *xdr, struct sk_buff *skb)
881 struct xdr_skb_reader desc = {
882 .skb = skb,
883 .offset = sizeof(rpc_fraghdr),
884 .count = skb->len - sizeof(rpc_fraghdr),
887 if (xdr_partial_copy_from_skb(xdr, 0, &desc, xdr_skb_read_bits) < 0)
888 return -1;
889 if (desc.count)
890 return -1;
891 return 0;
895 * xs_local_data_ready - "data ready" callback for AF_LOCAL sockets
896 * @sk: socket with data to read
897 * @len: how much data to read
899 * Currently this assumes we can read the whole reply in a single gulp.
901 static void xs_local_data_ready(struct sock *sk, int len)
903 struct rpc_task *task;
904 struct rpc_xprt *xprt;
905 struct rpc_rqst *rovr;
906 struct sk_buff *skb;
907 int err, repsize, copied;
908 u32 _xid;
909 __be32 *xp;
911 read_lock_bh(&sk->sk_callback_lock);
912 dprintk("RPC: %s...\n", __func__);
913 xprt = xprt_from_sock(sk);
914 if (xprt == NULL)
915 goto out;
917 skb = skb_recv_datagram(sk, 0, 1, &err);
918 if (skb == NULL)
919 goto out;
921 if (xprt->shutdown)
922 goto dropit;
924 repsize = skb->len - sizeof(rpc_fraghdr);
925 if (repsize < 4) {
926 dprintk("RPC: impossible RPC reply size %d\n", repsize);
927 goto dropit;
930 /* Copy the XID from the skb... */
931 xp = skb_header_pointer(skb, sizeof(rpc_fraghdr), sizeof(_xid), &_xid);
932 if (xp == NULL)
933 goto dropit;
935 /* Look up and lock the request corresponding to the given XID */
936 spin_lock(&xprt->transport_lock);
937 rovr = xprt_lookup_rqst(xprt, *xp);
938 if (!rovr)
939 goto out_unlock;
940 task = rovr->rq_task;
942 copied = rovr->rq_private_buf.buflen;
943 if (copied > repsize)
944 copied = repsize;
946 if (xs_local_copy_to_xdr(&rovr->rq_private_buf, skb)) {
947 dprintk("RPC: sk_buff copy failed\n");
948 goto out_unlock;
951 xprt_complete_rqst(task, copied);
953 out_unlock:
954 spin_unlock(&xprt->transport_lock);
955 dropit:
956 skb_free_datagram(sk, skb);
957 out:
958 read_unlock_bh(&sk->sk_callback_lock);
962 * xs_udp_data_ready - "data ready" callback for UDP sockets
963 * @sk: socket with data to read
964 * @len: how much data to read
967 static void xs_udp_data_ready(struct sock *sk, int len)
969 struct rpc_task *task;
970 struct rpc_xprt *xprt;
971 struct rpc_rqst *rovr;
972 struct sk_buff *skb;
973 int err, repsize, copied;
974 u32 _xid;
975 __be32 *xp;
977 read_lock_bh(&sk->sk_callback_lock);
978 dprintk("RPC: xs_udp_data_ready...\n");
979 if (!(xprt = xprt_from_sock(sk)))
980 goto out;
982 if ((skb = skb_recv_datagram(sk, 0, 1, &err)) == NULL)
983 goto out;
985 if (xprt->shutdown)
986 goto dropit;
988 repsize = skb->len - sizeof(struct udphdr);
989 if (repsize < 4) {
990 dprintk("RPC: impossible RPC reply size %d!\n", repsize);
991 goto dropit;
994 /* Copy the XID from the skb... */
995 xp = skb_header_pointer(skb, sizeof(struct udphdr),
996 sizeof(_xid), &_xid);
997 if (xp == NULL)
998 goto dropit;
1000 /* Look up and lock the request corresponding to the given XID */
1001 spin_lock(&xprt->transport_lock);
1002 rovr = xprt_lookup_rqst(xprt, *xp);
1003 if (!rovr)
1004 goto out_unlock;
1005 task = rovr->rq_task;
1007 if ((copied = rovr->rq_private_buf.buflen) > repsize)
1008 copied = repsize;
1010 /* Suck it into the iovec, verify checksum if not done by hw. */
1011 if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) {
1012 UDPX_INC_STATS_BH(sk, UDP_MIB_INERRORS);
1013 goto out_unlock;
1016 UDPX_INC_STATS_BH(sk, UDP_MIB_INDATAGRAMS);
1018 /* Something worked... */
1019 dst_confirm(skb_dst(skb));
1021 xprt_adjust_cwnd(task, copied);
1022 xprt_complete_rqst(task, copied);
1024 out_unlock:
1025 spin_unlock(&xprt->transport_lock);
1026 dropit:
1027 skb_free_datagram(sk, skb);
1028 out:
1029 read_unlock_bh(&sk->sk_callback_lock);
1032 static inline void xs_tcp_read_fraghdr(struct rpc_xprt *xprt, struct xdr_skb_reader *desc)
1034 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1035 size_t len, used;
1036 char *p;
1038 p = ((char *) &transport->tcp_fraghdr) + transport->tcp_offset;
1039 len = sizeof(transport->tcp_fraghdr) - transport->tcp_offset;
1040 used = xdr_skb_read_bits(desc, p, len);
1041 transport->tcp_offset += used;
1042 if (used != len)
1043 return;
1045 transport->tcp_reclen = ntohl(transport->tcp_fraghdr);
1046 if (transport->tcp_reclen & RPC_LAST_STREAM_FRAGMENT)
1047 transport->tcp_flags |= TCP_RCV_LAST_FRAG;
1048 else
1049 transport->tcp_flags &= ~TCP_RCV_LAST_FRAG;
1050 transport->tcp_reclen &= RPC_FRAGMENT_SIZE_MASK;
1052 transport->tcp_flags &= ~TCP_RCV_COPY_FRAGHDR;
1053 transport->tcp_offset = 0;
1055 /* Sanity check of the record length */
1056 if (unlikely(transport->tcp_reclen < 8)) {
1057 dprintk("RPC: invalid TCP record fragment length\n");
1058 xprt_force_disconnect(xprt);
1059 return;
1061 dprintk("RPC: reading TCP record fragment of length %d\n",
1062 transport->tcp_reclen);
1065 static void xs_tcp_check_fraghdr(struct sock_xprt *transport)
1067 if (transport->tcp_offset == transport->tcp_reclen) {
1068 transport->tcp_flags |= TCP_RCV_COPY_FRAGHDR;
1069 transport->tcp_offset = 0;
1070 if (transport->tcp_flags & TCP_RCV_LAST_FRAG) {
1071 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1072 transport->tcp_flags |= TCP_RCV_COPY_XID;
1073 transport->tcp_copied = 0;
1078 static inline void xs_tcp_read_xid(struct sock_xprt *transport, struct xdr_skb_reader *desc)
1080 size_t len, used;
1081 char *p;
1083 len = sizeof(transport->tcp_xid) - transport->tcp_offset;
1084 dprintk("RPC: reading XID (%Zu bytes)\n", len);
1085 p = ((char *) &transport->tcp_xid) + transport->tcp_offset;
1086 used = xdr_skb_read_bits(desc, p, len);
1087 transport->tcp_offset += used;
1088 if (used != len)
1089 return;
1090 transport->tcp_flags &= ~TCP_RCV_COPY_XID;
1091 transport->tcp_flags |= TCP_RCV_READ_CALLDIR;
1092 transport->tcp_copied = 4;
1093 dprintk("RPC: reading %s XID %08x\n",
1094 (transport->tcp_flags & TCP_RPC_REPLY) ? "reply for"
1095 : "request with",
1096 ntohl(transport->tcp_xid));
1097 xs_tcp_check_fraghdr(transport);
1100 static inline void xs_tcp_read_calldir(struct sock_xprt *transport,
1101 struct xdr_skb_reader *desc)
1103 size_t len, used;
1104 u32 offset;
1105 char *p;
1108 * We want transport->tcp_offset to be 8 at the end of this routine
1109 * (4 bytes for the xid and 4 bytes for the call/reply flag).
1110 * When this function is called for the first time,
1111 * transport->tcp_offset is 4 (after having already read the xid).
1113 offset = transport->tcp_offset - sizeof(transport->tcp_xid);
1114 len = sizeof(transport->tcp_calldir) - offset;
1115 dprintk("RPC: reading CALL/REPLY flag (%Zu bytes)\n", len);
1116 p = ((char *) &transport->tcp_calldir) + offset;
1117 used = xdr_skb_read_bits(desc, p, len);
1118 transport->tcp_offset += used;
1119 if (used != len)
1120 return;
1121 transport->tcp_flags &= ~TCP_RCV_READ_CALLDIR;
1123 * We don't yet have the XDR buffer, so we will write the calldir
1124 * out after we get the buffer from the 'struct rpc_rqst'
1126 switch (ntohl(transport->tcp_calldir)) {
1127 case RPC_REPLY:
1128 transport->tcp_flags |= TCP_RCV_COPY_CALLDIR;
1129 transport->tcp_flags |= TCP_RCV_COPY_DATA;
1130 transport->tcp_flags |= TCP_RPC_REPLY;
1131 break;
1132 case RPC_CALL:
1133 transport->tcp_flags |= TCP_RCV_COPY_CALLDIR;
1134 transport->tcp_flags |= TCP_RCV_COPY_DATA;
1135 transport->tcp_flags &= ~TCP_RPC_REPLY;
1136 break;
1137 default:
1138 dprintk("RPC: invalid request message type\n");
1139 xprt_force_disconnect(&transport->xprt);
1141 xs_tcp_check_fraghdr(transport);
1144 static inline void xs_tcp_read_common(struct rpc_xprt *xprt,
1145 struct xdr_skb_reader *desc,
1146 struct rpc_rqst *req)
1148 struct sock_xprt *transport =
1149 container_of(xprt, struct sock_xprt, xprt);
1150 struct xdr_buf *rcvbuf;
1151 size_t len;
1152 ssize_t r;
1154 rcvbuf = &req->rq_private_buf;
1156 if (transport->tcp_flags & TCP_RCV_COPY_CALLDIR) {
1158 * Save the RPC direction in the XDR buffer
1160 memcpy(rcvbuf->head[0].iov_base + transport->tcp_copied,
1161 &transport->tcp_calldir,
1162 sizeof(transport->tcp_calldir));
1163 transport->tcp_copied += sizeof(transport->tcp_calldir);
1164 transport->tcp_flags &= ~TCP_RCV_COPY_CALLDIR;
1167 len = desc->count;
1168 if (len > transport->tcp_reclen - transport->tcp_offset) {
1169 struct xdr_skb_reader my_desc;
1171 len = transport->tcp_reclen - transport->tcp_offset;
1172 memcpy(&my_desc, desc, sizeof(my_desc));
1173 my_desc.count = len;
1174 r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
1175 &my_desc, xdr_skb_read_bits);
1176 desc->count -= r;
1177 desc->offset += r;
1178 } else
1179 r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
1180 desc, xdr_skb_read_bits);
1182 if (r > 0) {
1183 transport->tcp_copied += r;
1184 transport->tcp_offset += r;
1186 if (r != len) {
1187 /* Error when copying to the receive buffer,
1188 * usually because we weren't able to allocate
1189 * additional buffer pages. All we can do now
1190 * is turn off TCP_RCV_COPY_DATA, so the request
1191 * will not receive any additional updates,
1192 * and time out.
1193 * Any remaining data from this record will
1194 * be discarded.
1196 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1197 dprintk("RPC: XID %08x truncated request\n",
1198 ntohl(transport->tcp_xid));
1199 dprintk("RPC: xprt = %p, tcp_copied = %lu, "
1200 "tcp_offset = %u, tcp_reclen = %u\n",
1201 xprt, transport->tcp_copied,
1202 transport->tcp_offset, transport->tcp_reclen);
1203 return;
1206 dprintk("RPC: XID %08x read %Zd bytes\n",
1207 ntohl(transport->tcp_xid), r);
1208 dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, "
1209 "tcp_reclen = %u\n", xprt, transport->tcp_copied,
1210 transport->tcp_offset, transport->tcp_reclen);
1212 if (transport->tcp_copied == req->rq_private_buf.buflen)
1213 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1214 else if (transport->tcp_offset == transport->tcp_reclen) {
1215 if (transport->tcp_flags & TCP_RCV_LAST_FRAG)
1216 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1221 * Finds the request corresponding to the RPC xid and invokes the common
1222 * tcp read code to read the data.
1224 static inline int xs_tcp_read_reply(struct rpc_xprt *xprt,
1225 struct xdr_skb_reader *desc)
1227 struct sock_xprt *transport =
1228 container_of(xprt, struct sock_xprt, xprt);
1229 struct rpc_rqst *req;
1231 dprintk("RPC: read reply XID %08x\n", ntohl(transport->tcp_xid));
1233 /* Find and lock the request corresponding to this xid */
1234 spin_lock(&xprt->transport_lock);
1235 req = xprt_lookup_rqst(xprt, transport->tcp_xid);
1236 if (!req) {
1237 dprintk("RPC: XID %08x request not found!\n",
1238 ntohl(transport->tcp_xid));
1239 spin_unlock(&xprt->transport_lock);
1240 return -1;
1243 xs_tcp_read_common(xprt, desc, req);
1245 if (!(transport->tcp_flags & TCP_RCV_COPY_DATA))
1246 xprt_complete_rqst(req->rq_task, transport->tcp_copied);
1248 spin_unlock(&xprt->transport_lock);
1249 return 0;
1252 #if defined(CONFIG_SUNRPC_BACKCHANNEL)
1254 * Obtains an rpc_rqst previously allocated and invokes the common
1255 * tcp read code to read the data. The result is placed in the callback
1256 * queue.
1257 * If we're unable to obtain the rpc_rqst we schedule the closing of the
1258 * connection and return -1.
1260 static inline int xs_tcp_read_callback(struct rpc_xprt *xprt,
1261 struct xdr_skb_reader *desc)
1263 struct sock_xprt *transport =
1264 container_of(xprt, struct sock_xprt, xprt);
1265 struct rpc_rqst *req;
1267 req = xprt_alloc_bc_request(xprt);
1268 if (req == NULL) {
1269 printk(KERN_WARNING "Callback slot table overflowed\n");
1270 xprt_force_disconnect(xprt);
1271 return -1;
1274 req->rq_xid = transport->tcp_xid;
1275 dprintk("RPC: read callback XID %08x\n", ntohl(req->rq_xid));
1276 xs_tcp_read_common(xprt, desc, req);
1278 if (!(transport->tcp_flags & TCP_RCV_COPY_DATA)) {
1279 struct svc_serv *bc_serv = xprt->bc_serv;
1282 * Add callback request to callback list. The callback
1283 * service sleeps on the sv_cb_waitq waiting for new
1284 * requests. Wake it up after adding enqueing the
1285 * request.
1287 dprintk("RPC: add callback request to list\n");
1288 spin_lock(&bc_serv->sv_cb_lock);
1289 list_add(&req->rq_bc_list, &bc_serv->sv_cb_list);
1290 spin_unlock(&bc_serv->sv_cb_lock);
1291 wake_up(&bc_serv->sv_cb_waitq);
1294 req->rq_private_buf.len = transport->tcp_copied;
1296 return 0;
1299 static inline int _xs_tcp_read_data(struct rpc_xprt *xprt,
1300 struct xdr_skb_reader *desc)
1302 struct sock_xprt *transport =
1303 container_of(xprt, struct sock_xprt, xprt);
1305 return (transport->tcp_flags & TCP_RPC_REPLY) ?
1306 xs_tcp_read_reply(xprt, desc) :
1307 xs_tcp_read_callback(xprt, desc);
1309 #else
1310 static inline int _xs_tcp_read_data(struct rpc_xprt *xprt,
1311 struct xdr_skb_reader *desc)
1313 return xs_tcp_read_reply(xprt, desc);
1315 #endif /* CONFIG_SUNRPC_BACKCHANNEL */
1318 * Read data off the transport. This can be either an RPC_CALL or an
1319 * RPC_REPLY. Relay the processing to helper functions.
1321 static void xs_tcp_read_data(struct rpc_xprt *xprt,
1322 struct xdr_skb_reader *desc)
1324 struct sock_xprt *transport =
1325 container_of(xprt, struct sock_xprt, xprt);
1327 if (_xs_tcp_read_data(xprt, desc) == 0)
1328 xs_tcp_check_fraghdr(transport);
1329 else {
1331 * The transport_lock protects the request handling.
1332 * There's no need to hold it to update the tcp_flags.
1334 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1338 static inline void xs_tcp_read_discard(struct sock_xprt *transport, struct xdr_skb_reader *desc)
1340 size_t len;
1342 len = transport->tcp_reclen - transport->tcp_offset;
1343 if (len > desc->count)
1344 len = desc->count;
1345 desc->count -= len;
1346 desc->offset += len;
1347 transport->tcp_offset += len;
1348 dprintk("RPC: discarded %Zu bytes\n", len);
1349 xs_tcp_check_fraghdr(transport);
1352 static int xs_tcp_data_recv(read_descriptor_t *rd_desc, struct sk_buff *skb, unsigned int offset, size_t len)
1354 struct rpc_xprt *xprt = rd_desc->arg.data;
1355 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1356 struct xdr_skb_reader desc = {
1357 .skb = skb,
1358 .offset = offset,
1359 .count = len,
1362 dprintk("RPC: xs_tcp_data_recv started\n");
1363 do {
1364 /* Read in a new fragment marker if necessary */
1365 /* Can we ever really expect to get completely empty fragments? */
1366 if (transport->tcp_flags & TCP_RCV_COPY_FRAGHDR) {
1367 xs_tcp_read_fraghdr(xprt, &desc);
1368 continue;
1370 /* Read in the xid if necessary */
1371 if (transport->tcp_flags & TCP_RCV_COPY_XID) {
1372 xs_tcp_read_xid(transport, &desc);
1373 continue;
1375 /* Read in the call/reply flag */
1376 if (transport->tcp_flags & TCP_RCV_READ_CALLDIR) {
1377 xs_tcp_read_calldir(transport, &desc);
1378 continue;
1380 /* Read in the request data */
1381 if (transport->tcp_flags & TCP_RCV_COPY_DATA) {
1382 xs_tcp_read_data(xprt, &desc);
1383 continue;
1385 /* Skip over any trailing bytes on short reads */
1386 xs_tcp_read_discard(transport, &desc);
1387 } while (desc.count);
1388 dprintk("RPC: xs_tcp_data_recv done\n");
1389 return len - desc.count;
1393 * xs_tcp_data_ready - "data ready" callback for TCP sockets
1394 * @sk: socket with data to read
1395 * @bytes: how much data to read
1398 static void xs_tcp_data_ready(struct sock *sk, int bytes)
1400 struct rpc_xprt *xprt;
1401 read_descriptor_t rd_desc;
1402 int read;
1404 dprintk("RPC: xs_tcp_data_ready...\n");
1406 read_lock_bh(&sk->sk_callback_lock);
1407 if (!(xprt = xprt_from_sock(sk)))
1408 goto out;
1409 if (xprt->shutdown)
1410 goto out;
1412 /* Any data means we had a useful conversation, so
1413 * the we don't need to delay the next reconnect
1415 if (xprt->reestablish_timeout)
1416 xprt->reestablish_timeout = 0;
1418 /* We use rd_desc to pass struct xprt to xs_tcp_data_recv */
1419 rd_desc.arg.data = xprt;
1420 do {
1421 rd_desc.count = 65536;
1422 read = tcp_read_sock(sk, &rd_desc, xs_tcp_data_recv);
1423 } while (read > 0);
1424 out:
1425 read_unlock_bh(&sk->sk_callback_lock);
1429 * Do the equivalent of linger/linger2 handling for dealing with
1430 * broken servers that don't close the socket in a timely
1431 * fashion
1433 static void xs_tcp_schedule_linger_timeout(struct rpc_xprt *xprt,
1434 unsigned long timeout)
1436 struct sock_xprt *transport;
1438 if (xprt_test_and_set_connecting(xprt))
1439 return;
1440 set_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1441 transport = container_of(xprt, struct sock_xprt, xprt);
1442 queue_delayed_work(rpciod_workqueue, &transport->connect_worker,
1443 timeout);
1446 static void xs_tcp_cancel_linger_timeout(struct rpc_xprt *xprt)
1448 struct sock_xprt *transport;
1450 transport = container_of(xprt, struct sock_xprt, xprt);
1452 if (!test_bit(XPRT_CONNECTION_ABORT, &xprt->state) ||
1453 !cancel_delayed_work(&transport->connect_worker))
1454 return;
1455 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1456 xprt_clear_connecting(xprt);
1459 static void xs_sock_mark_closed(struct rpc_xprt *xprt)
1461 smp_mb__before_clear_bit();
1462 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
1463 clear_bit(XPRT_CLOSING, &xprt->state);
1464 smp_mb__after_clear_bit();
1465 /* Mark transport as closed and wake up all pending tasks */
1466 xprt_disconnect_done(xprt);
1470 * xs_tcp_state_change - callback to handle TCP socket state changes
1471 * @sk: socket whose state has changed
1474 static void xs_tcp_state_change(struct sock *sk)
1476 struct rpc_xprt *xprt;
1478 read_lock_bh(&sk->sk_callback_lock);
1479 if (!(xprt = xprt_from_sock(sk)))
1480 goto out;
1481 dprintk("RPC: xs_tcp_state_change client %p...\n", xprt);
1482 dprintk("RPC: state %x conn %d dead %d zapped %d sk_shutdown %d\n",
1483 sk->sk_state, xprt_connected(xprt),
1484 sock_flag(sk, SOCK_DEAD),
1485 sock_flag(sk, SOCK_ZAPPED),
1486 sk->sk_shutdown);
1488 switch (sk->sk_state) {
1489 case TCP_ESTABLISHED:
1490 spin_lock(&xprt->transport_lock);
1491 if (!xprt_test_and_set_connected(xprt)) {
1492 struct sock_xprt *transport = container_of(xprt,
1493 struct sock_xprt, xprt);
1495 /* Reset TCP record info */
1496 transport->tcp_offset = 0;
1497 transport->tcp_reclen = 0;
1498 transport->tcp_copied = 0;
1499 transport->tcp_flags =
1500 TCP_RCV_COPY_FRAGHDR | TCP_RCV_COPY_XID;
1502 xprt_wake_pending_tasks(xprt, -EAGAIN);
1504 spin_unlock(&xprt->transport_lock);
1505 break;
1506 case TCP_FIN_WAIT1:
1507 /* The client initiated a shutdown of the socket */
1508 xprt->connect_cookie++;
1509 xprt->reestablish_timeout = 0;
1510 set_bit(XPRT_CLOSING, &xprt->state);
1511 smp_mb__before_clear_bit();
1512 clear_bit(XPRT_CONNECTED, &xprt->state);
1513 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
1514 smp_mb__after_clear_bit();
1515 xs_tcp_schedule_linger_timeout(xprt, xs_tcp_fin_timeout);
1516 break;
1517 case TCP_CLOSE_WAIT:
1518 /* The server initiated a shutdown of the socket */
1519 xprt_force_disconnect(xprt);
1520 xprt->connect_cookie++;
1521 case TCP_CLOSING:
1523 * If the server closed down the connection, make sure that
1524 * we back off before reconnecting
1526 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
1527 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
1528 break;
1529 case TCP_LAST_ACK:
1530 set_bit(XPRT_CLOSING, &xprt->state);
1531 xs_tcp_schedule_linger_timeout(xprt, xs_tcp_fin_timeout);
1532 smp_mb__before_clear_bit();
1533 clear_bit(XPRT_CONNECTED, &xprt->state);
1534 smp_mb__after_clear_bit();
1535 break;
1536 case TCP_CLOSE:
1537 xs_tcp_cancel_linger_timeout(xprt);
1538 xs_sock_mark_closed(xprt);
1540 out:
1541 read_unlock_bh(&sk->sk_callback_lock);
1545 * xs_error_report - callback mainly for catching socket errors
1546 * @sk: socket
1548 static void xs_error_report(struct sock *sk)
1550 struct rpc_xprt *xprt;
1552 read_lock_bh(&sk->sk_callback_lock);
1553 if (!(xprt = xprt_from_sock(sk)))
1554 goto out;
1555 dprintk("RPC: %s client %p...\n"
1556 "RPC: error %d\n",
1557 __func__, xprt, sk->sk_err);
1558 xprt_wake_pending_tasks(xprt, -EAGAIN);
1559 out:
1560 read_unlock_bh(&sk->sk_callback_lock);
1563 static void xs_write_space(struct sock *sk)
1565 struct socket *sock;
1566 struct rpc_xprt *xprt;
1568 if (unlikely(!(sock = sk->sk_socket)))
1569 return;
1570 clear_bit(SOCK_NOSPACE, &sock->flags);
1572 if (unlikely(!(xprt = xprt_from_sock(sk))))
1573 return;
1574 if (test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags) == 0)
1575 return;
1577 xprt_write_space(xprt);
1581 * xs_udp_write_space - callback invoked when socket buffer space
1582 * becomes available
1583 * @sk: socket whose state has changed
1585 * Called when more output buffer space is available for this socket.
1586 * We try not to wake our writers until they can make "significant"
1587 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1588 * with a bunch of small requests.
1590 static void xs_udp_write_space(struct sock *sk)
1592 read_lock_bh(&sk->sk_callback_lock);
1594 /* from net/core/sock.c:sock_def_write_space */
1595 if (sock_writeable(sk))
1596 xs_write_space(sk);
1598 read_unlock_bh(&sk->sk_callback_lock);
1602 * xs_tcp_write_space - callback invoked when socket buffer space
1603 * becomes available
1604 * @sk: socket whose state has changed
1606 * Called when more output buffer space is available for this socket.
1607 * We try not to wake our writers until they can make "significant"
1608 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1609 * with a bunch of small requests.
1611 static void xs_tcp_write_space(struct sock *sk)
1613 read_lock_bh(&sk->sk_callback_lock);
1615 /* from net/core/stream.c:sk_stream_write_space */
1616 if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk))
1617 xs_write_space(sk);
1619 read_unlock_bh(&sk->sk_callback_lock);
1622 static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt)
1624 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1625 struct sock *sk = transport->inet;
1627 if (transport->rcvsize) {
1628 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
1629 sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2;
1631 if (transport->sndsize) {
1632 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
1633 sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2;
1634 sk->sk_write_space(sk);
1639 * xs_udp_set_buffer_size - set send and receive limits
1640 * @xprt: generic transport
1641 * @sndsize: requested size of send buffer, in bytes
1642 * @rcvsize: requested size of receive buffer, in bytes
1644 * Set socket send and receive buffer size limits.
1646 static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize)
1648 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1650 transport->sndsize = 0;
1651 if (sndsize)
1652 transport->sndsize = sndsize + 1024;
1653 transport->rcvsize = 0;
1654 if (rcvsize)
1655 transport->rcvsize = rcvsize + 1024;
1657 xs_udp_do_set_buffer_size(xprt);
1661 * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
1662 * @task: task that timed out
1664 * Adjust the congestion window after a retransmit timeout has occurred.
1666 static void xs_udp_timer(struct rpc_task *task)
1668 xprt_adjust_cwnd(task, -ETIMEDOUT);
1671 static unsigned short xs_get_random_port(void)
1673 unsigned short range = xprt_max_resvport - xprt_min_resvport;
1674 unsigned short rand = (unsigned short) net_random() % range;
1675 return rand + xprt_min_resvport;
1679 * xs_set_port - reset the port number in the remote endpoint address
1680 * @xprt: generic transport
1681 * @port: new port number
1684 static void xs_set_port(struct rpc_xprt *xprt, unsigned short port)
1686 dprintk("RPC: setting port for xprt %p to %u\n", xprt, port);
1688 rpc_set_port(xs_addr(xprt), port);
1689 xs_update_peer_port(xprt);
1692 static unsigned short xs_get_srcport(struct sock_xprt *transport)
1694 unsigned short port = transport->srcport;
1696 if (port == 0 && transport->xprt.resvport)
1697 port = xs_get_random_port();
1698 return port;
1701 static unsigned short xs_next_srcport(struct sock_xprt *transport, unsigned short port)
1703 if (transport->srcport != 0)
1704 transport->srcport = 0;
1705 if (!transport->xprt.resvport)
1706 return 0;
1707 if (port <= xprt_min_resvport || port > xprt_max_resvport)
1708 return xprt_max_resvport;
1709 return --port;
1711 static int xs_bind(struct sock_xprt *transport, struct socket *sock)
1713 struct sockaddr_storage myaddr;
1714 int err, nloop = 0;
1715 unsigned short port = xs_get_srcport(transport);
1716 unsigned short last;
1718 memcpy(&myaddr, &transport->srcaddr, transport->xprt.addrlen);
1719 do {
1720 rpc_set_port((struct sockaddr *)&myaddr, port);
1721 err = kernel_bind(sock, (struct sockaddr *)&myaddr,
1722 transport->xprt.addrlen);
1723 if (port == 0)
1724 break;
1725 if (err == 0) {
1726 transport->srcport = port;
1727 break;
1729 last = port;
1730 port = xs_next_srcport(transport, port);
1731 if (port > last)
1732 nloop++;
1733 } while (err == -EADDRINUSE && nloop != 2);
1735 if (myaddr.ss_family == AF_INET)
1736 dprintk("RPC: %s %pI4:%u: %s (%d)\n", __func__,
1737 &((struct sockaddr_in *)&myaddr)->sin_addr,
1738 port, err ? "failed" : "ok", err);
1739 else
1740 dprintk("RPC: %s %pI6:%u: %s (%d)\n", __func__,
1741 &((struct sockaddr_in6 *)&myaddr)->sin6_addr,
1742 port, err ? "failed" : "ok", err);
1743 return err;
1747 * We don't support autobind on AF_LOCAL sockets
1749 static void xs_local_rpcbind(struct rpc_task *task)
1751 xprt_set_bound(task->tk_xprt);
1754 static void xs_local_set_port(struct rpc_xprt *xprt, unsigned short port)
1758 #ifdef CONFIG_DEBUG_LOCK_ALLOC
1759 static struct lock_class_key xs_key[2];
1760 static struct lock_class_key xs_slock_key[2];
1762 static inline void xs_reclassify_socketu(struct socket *sock)
1764 struct sock *sk = sock->sk;
1766 BUG_ON(sock_owned_by_user(sk));
1767 sock_lock_init_class_and_name(sk, "slock-AF_LOCAL-RPC",
1768 &xs_slock_key[1], "sk_lock-AF_LOCAL-RPC", &xs_key[1]);
1771 static inline void xs_reclassify_socket4(struct socket *sock)
1773 struct sock *sk = sock->sk;
1775 BUG_ON(sock_owned_by_user(sk));
1776 sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC",
1777 &xs_slock_key[0], "sk_lock-AF_INET-RPC", &xs_key[0]);
1780 static inline void xs_reclassify_socket6(struct socket *sock)
1782 struct sock *sk = sock->sk;
1784 BUG_ON(sock_owned_by_user(sk));
1785 sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC",
1786 &xs_slock_key[1], "sk_lock-AF_INET6-RPC", &xs_key[1]);
1789 static inline void xs_reclassify_socket(int family, struct socket *sock)
1791 switch (family) {
1792 case AF_LOCAL:
1793 xs_reclassify_socketu(sock);
1794 break;
1795 case AF_INET:
1796 xs_reclassify_socket4(sock);
1797 break;
1798 case AF_INET6:
1799 xs_reclassify_socket6(sock);
1800 break;
1803 #else
1804 static inline void xs_reclassify_socketu(struct socket *sock)
1808 static inline void xs_reclassify_socket4(struct socket *sock)
1812 static inline void xs_reclassify_socket6(struct socket *sock)
1816 static inline void xs_reclassify_socket(int family, struct socket *sock)
1819 #endif
1821 static struct socket *xs_create_sock(struct rpc_xprt *xprt,
1822 struct sock_xprt *transport, int family, int type, int protocol)
1824 struct socket *sock;
1825 int err;
1827 err = __sock_create(xprt->xprt_net, family, type, protocol, &sock, 1);
1828 if (err < 0) {
1829 dprintk("RPC: can't create %d transport socket (%d).\n",
1830 protocol, -err);
1831 goto out;
1833 xs_reclassify_socket(family, sock);
1835 err = xs_bind(transport, sock);
1836 if (err) {
1837 sock_release(sock);
1838 goto out;
1841 return sock;
1842 out:
1843 return ERR_PTR(err);
1846 static int xs_local_finish_connecting(struct rpc_xprt *xprt,
1847 struct socket *sock)
1849 struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
1850 xprt);
1852 if (!transport->inet) {
1853 struct sock *sk = sock->sk;
1855 write_lock_bh(&sk->sk_callback_lock);
1857 xs_save_old_callbacks(transport, sk);
1859 sk->sk_user_data = xprt;
1860 sk->sk_data_ready = xs_local_data_ready;
1861 sk->sk_write_space = xs_udp_write_space;
1862 sk->sk_error_report = xs_error_report;
1863 sk->sk_allocation = GFP_ATOMIC;
1865 xprt_clear_connected(xprt);
1867 /* Reset to new socket */
1868 transport->sock = sock;
1869 transport->inet = sk;
1871 write_unlock_bh(&sk->sk_callback_lock);
1874 /* Tell the socket layer to start connecting... */
1875 xprt->stat.connect_count++;
1876 xprt->stat.connect_start = jiffies;
1877 return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, 0);
1881 * xs_local_setup_socket - create AF_LOCAL socket, connect to a local endpoint
1882 * @xprt: RPC transport to connect
1883 * @transport: socket transport to connect
1884 * @create_sock: function to create a socket of the correct type
1886 * Invoked by a work queue tasklet.
1888 static void xs_local_setup_socket(struct work_struct *work)
1890 struct sock_xprt *transport =
1891 container_of(work, struct sock_xprt, connect_worker.work);
1892 struct rpc_xprt *xprt = &transport->xprt;
1893 struct socket *sock;
1894 int status = -EIO;
1896 if (xprt->shutdown)
1897 goto out;
1899 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1900 status = __sock_create(xprt->xprt_net, AF_LOCAL,
1901 SOCK_STREAM, 0, &sock, 1);
1902 if (status < 0) {
1903 dprintk("RPC: can't create AF_LOCAL "
1904 "transport socket (%d).\n", -status);
1905 goto out;
1907 xs_reclassify_socketu(sock);
1909 dprintk("RPC: worker connecting xprt %p via AF_LOCAL to %s\n",
1910 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1912 status = xs_local_finish_connecting(xprt, sock);
1913 switch (status) {
1914 case 0:
1915 dprintk("RPC: xprt %p connected to %s\n",
1916 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1917 xprt_set_connected(xprt);
1918 break;
1919 case -ENOENT:
1920 dprintk("RPC: xprt %p: socket %s does not exist\n",
1921 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1922 break;
1923 default:
1924 printk(KERN_ERR "%s: unhandled error (%d) connecting to %s\n",
1925 __func__, -status,
1926 xprt->address_strings[RPC_DISPLAY_ADDR]);
1929 out:
1930 xprt_clear_connecting(xprt);
1931 xprt_wake_pending_tasks(xprt, status);
1934 static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
1936 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1938 if (!transport->inet) {
1939 struct sock *sk = sock->sk;
1941 write_lock_bh(&sk->sk_callback_lock);
1943 xs_save_old_callbacks(transport, sk);
1945 sk->sk_user_data = xprt;
1946 sk->sk_data_ready = xs_udp_data_ready;
1947 sk->sk_write_space = xs_udp_write_space;
1948 sk->sk_error_report = xs_error_report;
1949 sk->sk_no_check = UDP_CSUM_NORCV;
1950 sk->sk_allocation = GFP_ATOMIC;
1952 xprt_set_connected(xprt);
1954 /* Reset to new socket */
1955 transport->sock = sock;
1956 transport->inet = sk;
1958 write_unlock_bh(&sk->sk_callback_lock);
1960 xs_udp_do_set_buffer_size(xprt);
1963 static void xs_udp_setup_socket(struct work_struct *work)
1965 struct sock_xprt *transport =
1966 container_of(work, struct sock_xprt, connect_worker.work);
1967 struct rpc_xprt *xprt = &transport->xprt;
1968 struct socket *sock = transport->sock;
1969 int status = -EIO;
1971 if (xprt->shutdown)
1972 goto out;
1974 /* Start by resetting any existing state */
1975 xs_reset_transport(transport);
1976 sock = xs_create_sock(xprt, transport,
1977 xs_addr(xprt)->sa_family, SOCK_DGRAM, IPPROTO_UDP);
1978 if (IS_ERR(sock))
1979 goto out;
1981 dprintk("RPC: worker connecting xprt %p via %s to "
1982 "%s (port %s)\n", xprt,
1983 xprt->address_strings[RPC_DISPLAY_PROTO],
1984 xprt->address_strings[RPC_DISPLAY_ADDR],
1985 xprt->address_strings[RPC_DISPLAY_PORT]);
1987 xs_udp_finish_connecting(xprt, sock);
1988 status = 0;
1989 out:
1990 xprt_clear_connecting(xprt);
1991 xprt_wake_pending_tasks(xprt, status);
1995 * We need to preserve the port number so the reply cache on the server can
1996 * find our cached RPC replies when we get around to reconnecting.
1998 static void xs_abort_connection(struct sock_xprt *transport)
2000 int result;
2001 struct sockaddr any;
2003 dprintk("RPC: disconnecting xprt %p to reuse port\n", transport);
2006 * Disconnect the transport socket by doing a connect operation
2007 * with AF_UNSPEC. This should return immediately...
2009 memset(&any, 0, sizeof(any));
2010 any.sa_family = AF_UNSPEC;
2011 result = kernel_connect(transport->sock, &any, sizeof(any), 0);
2012 if (!result)
2013 xs_sock_mark_closed(&transport->xprt);
2014 else
2015 dprintk("RPC: AF_UNSPEC connect return code %d\n",
2016 result);
2019 static void xs_tcp_reuse_connection(struct sock_xprt *transport)
2021 unsigned int state = transport->inet->sk_state;
2023 if (state == TCP_CLOSE && transport->sock->state == SS_UNCONNECTED) {
2024 /* we don't need to abort the connection if the socket
2025 * hasn't undergone a shutdown
2027 if (transport->inet->sk_shutdown == 0)
2028 return;
2029 dprintk("RPC: %s: TCP_CLOSEd and sk_shutdown set to %d\n",
2030 __func__, transport->inet->sk_shutdown);
2032 if ((1 << state) & (TCPF_ESTABLISHED|TCPF_SYN_SENT)) {
2033 /* we don't need to abort the connection if the socket
2034 * hasn't undergone a shutdown
2036 if (transport->inet->sk_shutdown == 0)
2037 return;
2038 dprintk("RPC: %s: ESTABLISHED/SYN_SENT "
2039 "sk_shutdown set to %d\n",
2040 __func__, transport->inet->sk_shutdown);
2042 xs_abort_connection(transport);
2045 static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
2047 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2048 int ret = -ENOTCONN;
2050 if (!transport->inet) {
2051 struct sock *sk = sock->sk;
2053 write_lock_bh(&sk->sk_callback_lock);
2055 xs_save_old_callbacks(transport, sk);
2057 sk->sk_user_data = xprt;
2058 sk->sk_data_ready = xs_tcp_data_ready;
2059 sk->sk_state_change = xs_tcp_state_change;
2060 sk->sk_write_space = xs_tcp_write_space;
2061 sk->sk_error_report = xs_error_report;
2062 sk->sk_allocation = GFP_ATOMIC;
2064 /* socket options */
2065 sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
2066 sock_reset_flag(sk, SOCK_LINGER);
2067 tcp_sk(sk)->linger2 = 0;
2068 tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF;
2070 xprt_clear_connected(xprt);
2072 /* Reset to new socket */
2073 transport->sock = sock;
2074 transport->inet = sk;
2076 write_unlock_bh(&sk->sk_callback_lock);
2079 if (!xprt_bound(xprt))
2080 goto out;
2082 /* Tell the socket layer to start connecting... */
2083 xprt->stat.connect_count++;
2084 xprt->stat.connect_start = jiffies;
2085 ret = kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK);
2086 switch (ret) {
2087 case 0:
2088 case -EINPROGRESS:
2089 /* SYN_SENT! */
2090 xprt->connect_cookie++;
2091 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
2092 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2094 out:
2095 return ret;
2099 * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint
2100 * @xprt: RPC transport to connect
2101 * @transport: socket transport to connect
2102 * @create_sock: function to create a socket of the correct type
2104 * Invoked by a work queue tasklet.
2106 static void xs_tcp_setup_socket(struct work_struct *work)
2108 struct sock_xprt *transport =
2109 container_of(work, struct sock_xprt, connect_worker.work);
2110 struct socket *sock = transport->sock;
2111 struct rpc_xprt *xprt = &transport->xprt;
2112 int status = -EIO;
2114 if (xprt->shutdown)
2115 goto out;
2117 if (!sock) {
2118 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
2119 sock = xs_create_sock(xprt, transport,
2120 xs_addr(xprt)->sa_family, SOCK_STREAM, IPPROTO_TCP);
2121 if (IS_ERR(sock)) {
2122 status = PTR_ERR(sock);
2123 goto out;
2125 } else {
2126 int abort_and_exit;
2128 abort_and_exit = test_and_clear_bit(XPRT_CONNECTION_ABORT,
2129 &xprt->state);
2130 /* "close" the socket, preserving the local port */
2131 xs_tcp_reuse_connection(transport);
2133 if (abort_and_exit)
2134 goto out_eagain;
2137 dprintk("RPC: worker connecting xprt %p via %s to "
2138 "%s (port %s)\n", xprt,
2139 xprt->address_strings[RPC_DISPLAY_PROTO],
2140 xprt->address_strings[RPC_DISPLAY_ADDR],
2141 xprt->address_strings[RPC_DISPLAY_PORT]);
2143 status = xs_tcp_finish_connecting(xprt, sock);
2144 dprintk("RPC: %p connect status %d connected %d sock state %d\n",
2145 xprt, -status, xprt_connected(xprt),
2146 sock->sk->sk_state);
2147 switch (status) {
2148 default:
2149 printk("%s: connect returned unhandled error %d\n",
2150 __func__, status);
2151 case -EADDRNOTAVAIL:
2152 /* We're probably in TIME_WAIT. Get rid of existing socket,
2153 * and retry
2155 set_bit(XPRT_CONNECTION_CLOSE, &xprt->state);
2156 xprt_force_disconnect(xprt);
2157 break;
2158 case -ECONNREFUSED:
2159 case -ECONNRESET:
2160 case -ENETUNREACH:
2161 /* retry with existing socket, after a delay */
2162 case 0:
2163 case -EINPROGRESS:
2164 case -EALREADY:
2165 xprt_clear_connecting(xprt);
2166 return;
2167 case -EINVAL:
2168 /* Happens, for instance, if the user specified a link
2169 * local IPv6 address without a scope-id.
2171 goto out;
2173 out_eagain:
2174 status = -EAGAIN;
2175 out:
2176 xprt_clear_connecting(xprt);
2177 xprt_wake_pending_tasks(xprt, status);
2181 * xs_connect - connect a socket to a remote endpoint
2182 * @task: address of RPC task that manages state of connect request
2184 * TCP: If the remote end dropped the connection, delay reconnecting.
2186 * UDP socket connects are synchronous, but we use a work queue anyway
2187 * to guarantee that even unprivileged user processes can set up a
2188 * socket on a privileged port.
2190 * If a UDP socket connect fails, the delay behavior here prevents
2191 * retry floods (hard mounts).
2193 static void xs_connect(struct rpc_task *task)
2195 struct rpc_xprt *xprt = task->tk_xprt;
2196 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2198 if (transport->sock != NULL && !RPC_IS_SOFTCONN(task)) {
2199 dprintk("RPC: xs_connect delayed xprt %p for %lu "
2200 "seconds\n",
2201 xprt, xprt->reestablish_timeout / HZ);
2202 queue_delayed_work(rpciod_workqueue,
2203 &transport->connect_worker,
2204 xprt->reestablish_timeout);
2205 xprt->reestablish_timeout <<= 1;
2206 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
2207 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2208 if (xprt->reestablish_timeout > XS_TCP_MAX_REEST_TO)
2209 xprt->reestablish_timeout = XS_TCP_MAX_REEST_TO;
2210 } else {
2211 dprintk("RPC: xs_connect scheduled xprt %p\n", xprt);
2212 queue_delayed_work(rpciod_workqueue,
2213 &transport->connect_worker, 0);
2218 * xs_local_print_stats - display AF_LOCAL socket-specifc stats
2219 * @xprt: rpc_xprt struct containing statistics
2220 * @seq: output file
2223 static void xs_local_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2225 long idle_time = 0;
2227 if (xprt_connected(xprt))
2228 idle_time = (long)(jiffies - xprt->last_used) / HZ;
2230 seq_printf(seq, "\txprt:\tlocal %lu %lu %lu %ld %lu %lu %lu "
2231 "%llu %llu\n",
2232 xprt->stat.bind_count,
2233 xprt->stat.connect_count,
2234 xprt->stat.connect_time,
2235 idle_time,
2236 xprt->stat.sends,
2237 xprt->stat.recvs,
2238 xprt->stat.bad_xids,
2239 xprt->stat.req_u,
2240 xprt->stat.bklog_u);
2244 * xs_udp_print_stats - display UDP socket-specifc stats
2245 * @xprt: rpc_xprt struct containing statistics
2246 * @seq: output file
2249 static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2251 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2253 seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %Lu %Lu\n",
2254 transport->srcport,
2255 xprt->stat.bind_count,
2256 xprt->stat.sends,
2257 xprt->stat.recvs,
2258 xprt->stat.bad_xids,
2259 xprt->stat.req_u,
2260 xprt->stat.bklog_u);
2264 * xs_tcp_print_stats - display TCP socket-specifc stats
2265 * @xprt: rpc_xprt struct containing statistics
2266 * @seq: output file
2269 static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2271 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2272 long idle_time = 0;
2274 if (xprt_connected(xprt))
2275 idle_time = (long)(jiffies - xprt->last_used) / HZ;
2277 seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu %Lu %Lu\n",
2278 transport->srcport,
2279 xprt->stat.bind_count,
2280 xprt->stat.connect_count,
2281 xprt->stat.connect_time,
2282 idle_time,
2283 xprt->stat.sends,
2284 xprt->stat.recvs,
2285 xprt->stat.bad_xids,
2286 xprt->stat.req_u,
2287 xprt->stat.bklog_u);
2291 * Allocate a bunch of pages for a scratch buffer for the rpc code. The reason
2292 * we allocate pages instead doing a kmalloc like rpc_malloc is because we want
2293 * to use the server side send routines.
2295 static void *bc_malloc(struct rpc_task *task, size_t size)
2297 struct page *page;
2298 struct rpc_buffer *buf;
2300 BUG_ON(size > PAGE_SIZE - sizeof(struct rpc_buffer));
2301 page = alloc_page(GFP_KERNEL);
2303 if (!page)
2304 return NULL;
2306 buf = page_address(page);
2307 buf->len = PAGE_SIZE;
2309 return buf->data;
2313 * Free the space allocated in the bc_alloc routine
2315 static void bc_free(void *buffer)
2317 struct rpc_buffer *buf;
2319 if (!buffer)
2320 return;
2322 buf = container_of(buffer, struct rpc_buffer, data);
2323 free_page((unsigned long)buf);
2327 * Use the svc_sock to send the callback. Must be called with svsk->sk_mutex
2328 * held. Borrows heavily from svc_tcp_sendto and xs_tcp_send_request.
2330 static int bc_sendto(struct rpc_rqst *req)
2332 int len;
2333 struct xdr_buf *xbufp = &req->rq_snd_buf;
2334 struct rpc_xprt *xprt = req->rq_xprt;
2335 struct sock_xprt *transport =
2336 container_of(xprt, struct sock_xprt, xprt);
2337 struct socket *sock = transport->sock;
2338 unsigned long headoff;
2339 unsigned long tailoff;
2341 xs_encode_stream_record_marker(xbufp);
2343 tailoff = (unsigned long)xbufp->tail[0].iov_base & ~PAGE_MASK;
2344 headoff = (unsigned long)xbufp->head[0].iov_base & ~PAGE_MASK;
2345 len = svc_send_common(sock, xbufp,
2346 virt_to_page(xbufp->head[0].iov_base), headoff,
2347 xbufp->tail[0].iov_base, tailoff);
2349 if (len != xbufp->len) {
2350 printk(KERN_NOTICE "Error sending entire callback!\n");
2351 len = -EAGAIN;
2354 return len;
2358 * The send routine. Borrows from svc_send
2360 static int bc_send_request(struct rpc_task *task)
2362 struct rpc_rqst *req = task->tk_rqstp;
2363 struct svc_xprt *xprt;
2364 struct svc_sock *svsk;
2365 u32 len;
2367 dprintk("sending request with xid: %08x\n", ntohl(req->rq_xid));
2369 * Get the server socket associated with this callback xprt
2371 xprt = req->rq_xprt->bc_xprt;
2372 svsk = container_of(xprt, struct svc_sock, sk_xprt);
2375 * Grab the mutex to serialize data as the connection is shared
2376 * with the fore channel
2378 if (!mutex_trylock(&xprt->xpt_mutex)) {
2379 rpc_sleep_on(&xprt->xpt_bc_pending, task, NULL);
2380 if (!mutex_trylock(&xprt->xpt_mutex))
2381 return -EAGAIN;
2382 rpc_wake_up_queued_task(&xprt->xpt_bc_pending, task);
2384 if (test_bit(XPT_DEAD, &xprt->xpt_flags))
2385 len = -ENOTCONN;
2386 else
2387 len = bc_sendto(req);
2388 mutex_unlock(&xprt->xpt_mutex);
2390 if (len > 0)
2391 len = 0;
2393 return len;
2397 * The close routine. Since this is client initiated, we do nothing
2400 static void bc_close(struct rpc_xprt *xprt)
2405 * The xprt destroy routine. Again, because this connection is client
2406 * initiated, we do nothing
2409 static void bc_destroy(struct rpc_xprt *xprt)
2413 static struct rpc_xprt_ops xs_local_ops = {
2414 .reserve_xprt = xprt_reserve_xprt,
2415 .release_xprt = xs_tcp_release_xprt,
2416 .rpcbind = xs_local_rpcbind,
2417 .set_port = xs_local_set_port,
2418 .connect = xs_connect,
2419 .buf_alloc = rpc_malloc,
2420 .buf_free = rpc_free,
2421 .send_request = xs_local_send_request,
2422 .set_retrans_timeout = xprt_set_retrans_timeout_def,
2423 .close = xs_close,
2424 .destroy = xs_destroy,
2425 .print_stats = xs_local_print_stats,
2428 static struct rpc_xprt_ops xs_udp_ops = {
2429 .set_buffer_size = xs_udp_set_buffer_size,
2430 .reserve_xprt = xprt_reserve_xprt_cong,
2431 .release_xprt = xprt_release_xprt_cong,
2432 .rpcbind = rpcb_getport_async,
2433 .set_port = xs_set_port,
2434 .connect = xs_connect,
2435 .buf_alloc = rpc_malloc,
2436 .buf_free = rpc_free,
2437 .send_request = xs_udp_send_request,
2438 .set_retrans_timeout = xprt_set_retrans_timeout_rtt,
2439 .timer = xs_udp_timer,
2440 .release_request = xprt_release_rqst_cong,
2441 .close = xs_close,
2442 .destroy = xs_destroy,
2443 .print_stats = xs_udp_print_stats,
2446 static struct rpc_xprt_ops xs_tcp_ops = {
2447 .reserve_xprt = xprt_reserve_xprt,
2448 .release_xprt = xs_tcp_release_xprt,
2449 .rpcbind = rpcb_getport_async,
2450 .set_port = xs_set_port,
2451 .connect = xs_connect,
2452 .buf_alloc = rpc_malloc,
2453 .buf_free = rpc_free,
2454 .send_request = xs_tcp_send_request,
2455 .set_retrans_timeout = xprt_set_retrans_timeout_def,
2456 .close = xs_tcp_close,
2457 .destroy = xs_destroy,
2458 .print_stats = xs_tcp_print_stats,
2462 * The rpc_xprt_ops for the server backchannel
2465 static struct rpc_xprt_ops bc_tcp_ops = {
2466 .reserve_xprt = xprt_reserve_xprt,
2467 .release_xprt = xprt_release_xprt,
2468 .buf_alloc = bc_malloc,
2469 .buf_free = bc_free,
2470 .send_request = bc_send_request,
2471 .set_retrans_timeout = xprt_set_retrans_timeout_def,
2472 .close = bc_close,
2473 .destroy = bc_destroy,
2474 .print_stats = xs_tcp_print_stats,
2477 static int xs_init_anyaddr(const int family, struct sockaddr *sap)
2479 static const struct sockaddr_in sin = {
2480 .sin_family = AF_INET,
2481 .sin_addr.s_addr = htonl(INADDR_ANY),
2483 static const struct sockaddr_in6 sin6 = {
2484 .sin6_family = AF_INET6,
2485 .sin6_addr = IN6ADDR_ANY_INIT,
2488 switch (family) {
2489 case AF_LOCAL:
2490 break;
2491 case AF_INET:
2492 memcpy(sap, &sin, sizeof(sin));
2493 break;
2494 case AF_INET6:
2495 memcpy(sap, &sin6, sizeof(sin6));
2496 break;
2497 default:
2498 dprintk("RPC: %s: Bad address family\n", __func__);
2499 return -EAFNOSUPPORT;
2501 return 0;
2504 static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args,
2505 unsigned int slot_table_size,
2506 unsigned int max_slot_table_size)
2508 struct rpc_xprt *xprt;
2509 struct sock_xprt *new;
2511 if (args->addrlen > sizeof(xprt->addr)) {
2512 dprintk("RPC: xs_setup_xprt: address too large\n");
2513 return ERR_PTR(-EBADF);
2516 xprt = xprt_alloc(args->net, sizeof(*new), slot_table_size,
2517 max_slot_table_size);
2518 if (xprt == NULL) {
2519 dprintk("RPC: xs_setup_xprt: couldn't allocate "
2520 "rpc_xprt\n");
2521 return ERR_PTR(-ENOMEM);
2524 new = container_of(xprt, struct sock_xprt, xprt);
2525 memcpy(&xprt->addr, args->dstaddr, args->addrlen);
2526 xprt->addrlen = args->addrlen;
2527 if (args->srcaddr)
2528 memcpy(&new->srcaddr, args->srcaddr, args->addrlen);
2529 else {
2530 int err;
2531 err = xs_init_anyaddr(args->dstaddr->sa_family,
2532 (struct sockaddr *)&new->srcaddr);
2533 if (err != 0)
2534 return ERR_PTR(err);
2537 return xprt;
2540 static const struct rpc_timeout xs_local_default_timeout = {
2541 .to_initval = 10 * HZ,
2542 .to_maxval = 10 * HZ,
2543 .to_retries = 2,
2547 * xs_setup_local - Set up transport to use an AF_LOCAL socket
2548 * @args: rpc transport creation arguments
2550 * AF_LOCAL is a "tpi_cots_ord" transport, just like TCP
2552 static struct rpc_xprt *xs_setup_local(struct xprt_create *args)
2554 struct sockaddr_un *sun = (struct sockaddr_un *)args->dstaddr;
2555 struct sock_xprt *transport;
2556 struct rpc_xprt *xprt;
2557 struct rpc_xprt *ret;
2559 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
2560 xprt_max_tcp_slot_table_entries);
2561 if (IS_ERR(xprt))
2562 return xprt;
2563 transport = container_of(xprt, struct sock_xprt, xprt);
2565 xprt->prot = 0;
2566 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
2567 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2569 xprt->bind_timeout = XS_BIND_TO;
2570 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2571 xprt->idle_timeout = XS_IDLE_DISC_TO;
2573 xprt->ops = &xs_local_ops;
2574 xprt->timeout = &xs_local_default_timeout;
2576 switch (sun->sun_family) {
2577 case AF_LOCAL:
2578 if (sun->sun_path[0] != '/') {
2579 dprintk("RPC: bad AF_LOCAL address: %s\n",
2580 sun->sun_path);
2581 ret = ERR_PTR(-EINVAL);
2582 goto out_err;
2584 xprt_set_bound(xprt);
2585 INIT_DELAYED_WORK(&transport->connect_worker,
2586 xs_local_setup_socket);
2587 xs_format_peer_addresses(xprt, "local", RPCBIND_NETID_LOCAL);
2588 break;
2589 default:
2590 ret = ERR_PTR(-EAFNOSUPPORT);
2591 goto out_err;
2594 dprintk("RPC: set up xprt to %s via AF_LOCAL\n",
2595 xprt->address_strings[RPC_DISPLAY_ADDR]);
2597 if (try_module_get(THIS_MODULE))
2598 return xprt;
2599 ret = ERR_PTR(-EINVAL);
2600 out_err:
2601 xprt_free(xprt);
2602 return ret;
2605 static const struct rpc_timeout xs_udp_default_timeout = {
2606 .to_initval = 5 * HZ,
2607 .to_maxval = 30 * HZ,
2608 .to_increment = 5 * HZ,
2609 .to_retries = 5,
2613 * xs_setup_udp - Set up transport to use a UDP socket
2614 * @args: rpc transport creation arguments
2617 static struct rpc_xprt *xs_setup_udp(struct xprt_create *args)
2619 struct sockaddr *addr = args->dstaddr;
2620 struct rpc_xprt *xprt;
2621 struct sock_xprt *transport;
2622 struct rpc_xprt *ret;
2624 xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries,
2625 xprt_udp_slot_table_entries);
2626 if (IS_ERR(xprt))
2627 return xprt;
2628 transport = container_of(xprt, struct sock_xprt, xprt);
2630 xprt->prot = IPPROTO_UDP;
2631 xprt->tsh_size = 0;
2632 /* XXX: header size can vary due to auth type, IPv6, etc. */
2633 xprt->max_payload = (1U << 16) - (MAX_HEADER << 3);
2635 xprt->bind_timeout = XS_BIND_TO;
2636 xprt->reestablish_timeout = XS_UDP_REEST_TO;
2637 xprt->idle_timeout = XS_IDLE_DISC_TO;
2639 xprt->ops = &xs_udp_ops;
2641 xprt->timeout = &xs_udp_default_timeout;
2643 switch (addr->sa_family) {
2644 case AF_INET:
2645 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
2646 xprt_set_bound(xprt);
2648 INIT_DELAYED_WORK(&transport->connect_worker,
2649 xs_udp_setup_socket);
2650 xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP);
2651 break;
2652 case AF_INET6:
2653 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
2654 xprt_set_bound(xprt);
2656 INIT_DELAYED_WORK(&transport->connect_worker,
2657 xs_udp_setup_socket);
2658 xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6);
2659 break;
2660 default:
2661 ret = ERR_PTR(-EAFNOSUPPORT);
2662 goto out_err;
2665 if (xprt_bound(xprt))
2666 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2667 xprt->address_strings[RPC_DISPLAY_ADDR],
2668 xprt->address_strings[RPC_DISPLAY_PORT],
2669 xprt->address_strings[RPC_DISPLAY_PROTO]);
2670 else
2671 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
2672 xprt->address_strings[RPC_DISPLAY_ADDR],
2673 xprt->address_strings[RPC_DISPLAY_PROTO]);
2675 if (try_module_get(THIS_MODULE))
2676 return xprt;
2677 ret = ERR_PTR(-EINVAL);
2678 out_err:
2679 xprt_free(xprt);
2680 return ret;
2683 static const struct rpc_timeout xs_tcp_default_timeout = {
2684 .to_initval = 60 * HZ,
2685 .to_maxval = 60 * HZ,
2686 .to_retries = 2,
2690 * xs_setup_tcp - Set up transport to use a TCP socket
2691 * @args: rpc transport creation arguments
2694 static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args)
2696 struct sockaddr *addr = args->dstaddr;
2697 struct rpc_xprt *xprt;
2698 struct sock_xprt *transport;
2699 struct rpc_xprt *ret;
2701 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
2702 xprt_max_tcp_slot_table_entries);
2703 if (IS_ERR(xprt))
2704 return xprt;
2705 transport = container_of(xprt, struct sock_xprt, xprt);
2707 xprt->prot = IPPROTO_TCP;
2708 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
2709 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2711 xprt->bind_timeout = XS_BIND_TO;
2712 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2713 xprt->idle_timeout = XS_IDLE_DISC_TO;
2715 xprt->ops = &xs_tcp_ops;
2716 xprt->timeout = &xs_tcp_default_timeout;
2718 switch (addr->sa_family) {
2719 case AF_INET:
2720 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
2721 xprt_set_bound(xprt);
2723 INIT_DELAYED_WORK(&transport->connect_worker,
2724 xs_tcp_setup_socket);
2725 xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP);
2726 break;
2727 case AF_INET6:
2728 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
2729 xprt_set_bound(xprt);
2731 INIT_DELAYED_WORK(&transport->connect_worker,
2732 xs_tcp_setup_socket);
2733 xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6);
2734 break;
2735 default:
2736 ret = ERR_PTR(-EAFNOSUPPORT);
2737 goto out_err;
2740 if (xprt_bound(xprt))
2741 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2742 xprt->address_strings[RPC_DISPLAY_ADDR],
2743 xprt->address_strings[RPC_DISPLAY_PORT],
2744 xprt->address_strings[RPC_DISPLAY_PROTO]);
2745 else
2746 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
2747 xprt->address_strings[RPC_DISPLAY_ADDR],
2748 xprt->address_strings[RPC_DISPLAY_PROTO]);
2751 if (try_module_get(THIS_MODULE))
2752 return xprt;
2753 ret = ERR_PTR(-EINVAL);
2754 out_err:
2755 xprt_free(xprt);
2756 return ret;
2760 * xs_setup_bc_tcp - Set up transport to use a TCP backchannel socket
2761 * @args: rpc transport creation arguments
2764 static struct rpc_xprt *xs_setup_bc_tcp(struct xprt_create *args)
2766 struct sockaddr *addr = args->dstaddr;
2767 struct rpc_xprt *xprt;
2768 struct sock_xprt *transport;
2769 struct svc_sock *bc_sock;
2770 struct rpc_xprt *ret;
2772 if (args->bc_xprt->xpt_bc_xprt) {
2774 * This server connection already has a backchannel
2775 * export; we can't create a new one, as we wouldn't be
2776 * able to match replies based on xid any more. So,
2777 * reuse the already-existing one:
2779 return args->bc_xprt->xpt_bc_xprt;
2781 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
2782 xprt_tcp_slot_table_entries);
2783 if (IS_ERR(xprt))
2784 return xprt;
2785 transport = container_of(xprt, struct sock_xprt, xprt);
2787 xprt->prot = IPPROTO_TCP;
2788 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
2789 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2790 xprt->timeout = &xs_tcp_default_timeout;
2792 /* backchannel */
2793 xprt_set_bound(xprt);
2794 xprt->bind_timeout = 0;
2795 xprt->reestablish_timeout = 0;
2796 xprt->idle_timeout = 0;
2798 xprt->ops = &bc_tcp_ops;
2800 switch (addr->sa_family) {
2801 case AF_INET:
2802 xs_format_peer_addresses(xprt, "tcp",
2803 RPCBIND_NETID_TCP);
2804 break;
2805 case AF_INET6:
2806 xs_format_peer_addresses(xprt, "tcp",
2807 RPCBIND_NETID_TCP6);
2808 break;
2809 default:
2810 ret = ERR_PTR(-EAFNOSUPPORT);
2811 goto out_err;
2814 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2815 xprt->address_strings[RPC_DISPLAY_ADDR],
2816 xprt->address_strings[RPC_DISPLAY_PORT],
2817 xprt->address_strings[RPC_DISPLAY_PROTO]);
2820 * Once we've associated a backchannel xprt with a connection,
2821 * we want to keep it around as long as long as the connection
2822 * lasts, in case we need to start using it for a backchannel
2823 * again; this reference won't be dropped until bc_xprt is
2824 * destroyed.
2826 xprt_get(xprt);
2827 args->bc_xprt->xpt_bc_xprt = xprt;
2828 xprt->bc_xprt = args->bc_xprt;
2829 bc_sock = container_of(args->bc_xprt, struct svc_sock, sk_xprt);
2830 transport->sock = bc_sock->sk_sock;
2831 transport->inet = bc_sock->sk_sk;
2834 * Since we don't want connections for the backchannel, we set
2835 * the xprt status to connected
2837 xprt_set_connected(xprt);
2840 if (try_module_get(THIS_MODULE))
2841 return xprt;
2842 xprt_put(xprt);
2843 ret = ERR_PTR(-EINVAL);
2844 out_err:
2845 xprt_free(xprt);
2846 return ret;
2849 static struct xprt_class xs_local_transport = {
2850 .list = LIST_HEAD_INIT(xs_local_transport.list),
2851 .name = "named UNIX socket",
2852 .owner = THIS_MODULE,
2853 .ident = XPRT_TRANSPORT_LOCAL,
2854 .setup = xs_setup_local,
2857 static struct xprt_class xs_udp_transport = {
2858 .list = LIST_HEAD_INIT(xs_udp_transport.list),
2859 .name = "udp",
2860 .owner = THIS_MODULE,
2861 .ident = XPRT_TRANSPORT_UDP,
2862 .setup = xs_setup_udp,
2865 static struct xprt_class xs_tcp_transport = {
2866 .list = LIST_HEAD_INIT(xs_tcp_transport.list),
2867 .name = "tcp",
2868 .owner = THIS_MODULE,
2869 .ident = XPRT_TRANSPORT_TCP,
2870 .setup = xs_setup_tcp,
2873 static struct xprt_class xs_bc_tcp_transport = {
2874 .list = LIST_HEAD_INIT(xs_bc_tcp_transport.list),
2875 .name = "tcp NFSv4.1 backchannel",
2876 .owner = THIS_MODULE,
2877 .ident = XPRT_TRANSPORT_BC_TCP,
2878 .setup = xs_setup_bc_tcp,
2882 * init_socket_xprt - set up xprtsock's sysctls, register with RPC client
2885 int init_socket_xprt(void)
2887 #ifdef RPC_DEBUG
2888 if (!sunrpc_table_header)
2889 sunrpc_table_header = register_sysctl_table(sunrpc_table);
2890 #endif
2892 xprt_register_transport(&xs_local_transport);
2893 xprt_register_transport(&xs_udp_transport);
2894 xprt_register_transport(&xs_tcp_transport);
2895 xprt_register_transport(&xs_bc_tcp_transport);
2897 return 0;
2901 * cleanup_socket_xprt - remove xprtsock's sysctls, unregister
2904 void cleanup_socket_xprt(void)
2906 #ifdef RPC_DEBUG
2907 if (sunrpc_table_header) {
2908 unregister_sysctl_table(sunrpc_table_header);
2909 sunrpc_table_header = NULL;
2911 #endif
2913 xprt_unregister_transport(&xs_local_transport);
2914 xprt_unregister_transport(&xs_udp_transport);
2915 xprt_unregister_transport(&xs_tcp_transport);
2916 xprt_unregister_transport(&xs_bc_tcp_transport);
2919 static int param_set_uint_minmax(const char *val,
2920 const struct kernel_param *kp,
2921 unsigned int min, unsigned int max)
2923 unsigned long num;
2924 int ret;
2926 if (!val)
2927 return -EINVAL;
2928 ret = strict_strtoul(val, 0, &num);
2929 if (ret == -EINVAL || num < min || num > max)
2930 return -EINVAL;
2931 *((unsigned int *)kp->arg) = num;
2932 return 0;
2935 static int param_set_portnr(const char *val, const struct kernel_param *kp)
2937 return param_set_uint_minmax(val, kp,
2938 RPC_MIN_RESVPORT,
2939 RPC_MAX_RESVPORT);
2942 static struct kernel_param_ops param_ops_portnr = {
2943 .set = param_set_portnr,
2944 .get = param_get_uint,
2947 #define param_check_portnr(name, p) \
2948 __param_check(name, p, unsigned int);
2950 module_param_named(min_resvport, xprt_min_resvport, portnr, 0644);
2951 module_param_named(max_resvport, xprt_max_resvport, portnr, 0644);
2953 static int param_set_slot_table_size(const char *val,
2954 const struct kernel_param *kp)
2956 return param_set_uint_minmax(val, kp,
2957 RPC_MIN_SLOT_TABLE,
2958 RPC_MAX_SLOT_TABLE);
2961 static struct kernel_param_ops param_ops_slot_table_size = {
2962 .set = param_set_slot_table_size,
2963 .get = param_get_uint,
2966 #define param_check_slot_table_size(name, p) \
2967 __param_check(name, p, unsigned int);
2969 static int param_set_max_slot_table_size(const char *val,
2970 const struct kernel_param *kp)
2972 return param_set_uint_minmax(val, kp,
2973 RPC_MIN_SLOT_TABLE,
2974 RPC_MAX_SLOT_TABLE_LIMIT);
2977 static struct kernel_param_ops param_ops_max_slot_table_size = {
2978 .set = param_set_max_slot_table_size,
2979 .get = param_get_uint,
2982 #define param_check_max_slot_table_size(name, p) \
2983 __param_check(name, p, unsigned int);
2985 module_param_named(tcp_slot_table_entries, xprt_tcp_slot_table_entries,
2986 slot_table_size, 0644);
2987 module_param_named(tcp_max_slot_table_entries, xprt_max_tcp_slot_table_entries,
2988 max_slot_table_size, 0644);
2989 module_param_named(udp_slot_table_entries, xprt_udp_slot_table_entries,
2990 slot_table_size, 0644);