keyctl_session_to_parent(): use thread_group_empty() to check singlethreadness
[linux-2.6.git] / net / sunrpc / xprtsock.c
blobb7cd8cccbe72c3dc12320e65af18f22104d5df5d
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/slab.h>
23 #include <linux/module.h>
24 #include <linux/capability.h>
25 #include <linux/pagemap.h>
26 #include <linux/errno.h>
27 #include <linux/socket.h>
28 #include <linux/in.h>
29 #include <linux/net.h>
30 #include <linux/mm.h>
31 #include <linux/udp.h>
32 #include <linux/tcp.h>
33 #include <linux/sunrpc/clnt.h>
34 #include <linux/sunrpc/sched.h>
35 #include <linux/sunrpc/svcsock.h>
36 #include <linux/sunrpc/xprtsock.h>
37 #include <linux/file.h>
38 #ifdef CONFIG_NFS_V4_1
39 #include <linux/sunrpc/bc_xprt.h>
40 #endif
42 #include <net/sock.h>
43 #include <net/checksum.h>
44 #include <net/udp.h>
45 #include <net/tcp.h>
47 #include "sunrpc.h"
49 * xprtsock tunables
51 unsigned int xprt_udp_slot_table_entries = RPC_DEF_SLOT_TABLE;
52 unsigned int xprt_tcp_slot_table_entries = RPC_DEF_SLOT_TABLE;
54 unsigned int xprt_min_resvport = RPC_DEF_MIN_RESVPORT;
55 unsigned int xprt_max_resvport = RPC_DEF_MAX_RESVPORT;
57 #define XS_TCP_LINGER_TO (15U * HZ)
58 static unsigned int xs_tcp_fin_timeout __read_mostly = XS_TCP_LINGER_TO;
61 * We can register our own files under /proc/sys/sunrpc by
62 * calling register_sysctl_table() again. The files in that
63 * directory become the union of all files registered there.
65 * We simply need to make sure that we don't collide with
66 * someone else's file names!
69 #ifdef RPC_DEBUG
71 static unsigned int min_slot_table_size = RPC_MIN_SLOT_TABLE;
72 static unsigned int max_slot_table_size = RPC_MAX_SLOT_TABLE;
73 static unsigned int xprt_min_resvport_limit = RPC_MIN_RESVPORT;
74 static unsigned int xprt_max_resvport_limit = RPC_MAX_RESVPORT;
76 static struct ctl_table_header *sunrpc_table_header;
79 * FIXME: changing the UDP slot table size should also resize the UDP
80 * socket buffers for existing UDP transports
82 static ctl_table xs_tunables_table[] = {
84 .procname = "udp_slot_table_entries",
85 .data = &xprt_udp_slot_table_entries,
86 .maxlen = sizeof(unsigned int),
87 .mode = 0644,
88 .proc_handler = proc_dointvec_minmax,
89 .extra1 = &min_slot_table_size,
90 .extra2 = &max_slot_table_size
93 .procname = "tcp_slot_table_entries",
94 .data = &xprt_tcp_slot_table_entries,
95 .maxlen = sizeof(unsigned int),
96 .mode = 0644,
97 .proc_handler = proc_dointvec_minmax,
98 .extra1 = &min_slot_table_size,
99 .extra2 = &max_slot_table_size
102 .procname = "min_resvport",
103 .data = &xprt_min_resvport,
104 .maxlen = sizeof(unsigned int),
105 .mode = 0644,
106 .proc_handler = proc_dointvec_minmax,
107 .extra1 = &xprt_min_resvport_limit,
108 .extra2 = &xprt_max_resvport_limit
111 .procname = "max_resvport",
112 .data = &xprt_max_resvport,
113 .maxlen = sizeof(unsigned int),
114 .mode = 0644,
115 .proc_handler = proc_dointvec_minmax,
116 .extra1 = &xprt_min_resvport_limit,
117 .extra2 = &xprt_max_resvport_limit
120 .procname = "tcp_fin_timeout",
121 .data = &xs_tcp_fin_timeout,
122 .maxlen = sizeof(xs_tcp_fin_timeout),
123 .mode = 0644,
124 .proc_handler = proc_dointvec_jiffies,
126 { },
129 static ctl_table sunrpc_table[] = {
131 .procname = "sunrpc",
132 .mode = 0555,
133 .child = xs_tunables_table
135 { },
138 #endif
141 * Wait duration for a reply from the RPC portmapper.
143 #define XS_BIND_TO (60U * HZ)
146 * Delay if a UDP socket connect error occurs. This is most likely some
147 * kind of resource problem on the local host.
149 #define XS_UDP_REEST_TO (2U * HZ)
152 * The reestablish timeout allows clients to delay for a bit before attempting
153 * to reconnect to a server that just dropped our connection.
155 * We implement an exponential backoff when trying to reestablish a TCP
156 * transport connection with the server. Some servers like to drop a TCP
157 * connection when they are overworked, so we start with a short timeout and
158 * increase over time if the server is down or not responding.
160 #define XS_TCP_INIT_REEST_TO (3U * HZ)
161 #define XS_TCP_MAX_REEST_TO (5U * 60 * HZ)
164 * TCP idle timeout; client drops the transport socket if it is idle
165 * for this long. Note that we also timeout UDP sockets to prevent
166 * holding port numbers when there is no RPC traffic.
168 #define XS_IDLE_DISC_TO (5U * 60 * HZ)
170 #ifdef RPC_DEBUG
171 # undef RPC_DEBUG_DATA
172 # define RPCDBG_FACILITY RPCDBG_TRANS
173 #endif
175 #ifdef RPC_DEBUG_DATA
176 static void xs_pktdump(char *msg, u32 *packet, unsigned int count)
178 u8 *buf = (u8 *) packet;
179 int j;
181 dprintk("RPC: %s\n", msg);
182 for (j = 0; j < count && j < 128; j += 4) {
183 if (!(j & 31)) {
184 if (j)
185 dprintk("\n");
186 dprintk("0x%04x ", j);
188 dprintk("%02x%02x%02x%02x ",
189 buf[j], buf[j+1], buf[j+2], buf[j+3]);
191 dprintk("\n");
193 #else
194 static inline void xs_pktdump(char *msg, u32 *packet, unsigned int count)
196 /* NOP */
198 #endif
200 struct sock_xprt {
201 struct rpc_xprt xprt;
204 * Network layer
206 struct socket * sock;
207 struct sock * inet;
210 * State of TCP reply receive
212 __be32 tcp_fraghdr,
213 tcp_xid;
215 u32 tcp_offset,
216 tcp_reclen;
218 unsigned long tcp_copied,
219 tcp_flags;
222 * Connection of transports
224 struct delayed_work connect_worker;
225 struct sockaddr_storage srcaddr;
226 unsigned short srcport;
229 * UDP socket buffer size parameters
231 size_t rcvsize,
232 sndsize;
235 * Saved socket callback addresses
237 void (*old_data_ready)(struct sock *, int);
238 void (*old_state_change)(struct sock *);
239 void (*old_write_space)(struct sock *);
240 void (*old_error_report)(struct sock *);
244 * TCP receive state flags
246 #define TCP_RCV_LAST_FRAG (1UL << 0)
247 #define TCP_RCV_COPY_FRAGHDR (1UL << 1)
248 #define TCP_RCV_COPY_XID (1UL << 2)
249 #define TCP_RCV_COPY_DATA (1UL << 3)
250 #define TCP_RCV_READ_CALLDIR (1UL << 4)
251 #define TCP_RCV_COPY_CALLDIR (1UL << 5)
254 * TCP RPC flags
256 #define TCP_RPC_REPLY (1UL << 6)
258 static inline struct sockaddr *xs_addr(struct rpc_xprt *xprt)
260 return (struct sockaddr *) &xprt->addr;
263 static inline struct sockaddr_in *xs_addr_in(struct rpc_xprt *xprt)
265 return (struct sockaddr_in *) &xprt->addr;
268 static inline struct sockaddr_in6 *xs_addr_in6(struct rpc_xprt *xprt)
270 return (struct sockaddr_in6 *) &xprt->addr;
273 static void xs_format_common_peer_addresses(struct rpc_xprt *xprt)
275 struct sockaddr *sap = xs_addr(xprt);
276 struct sockaddr_in6 *sin6;
277 struct sockaddr_in *sin;
278 char buf[128];
280 (void)rpc_ntop(sap, buf, sizeof(buf));
281 xprt->address_strings[RPC_DISPLAY_ADDR] = kstrdup(buf, GFP_KERNEL);
283 switch (sap->sa_family) {
284 case AF_INET:
285 sin = xs_addr_in(xprt);
286 snprintf(buf, sizeof(buf), "%08x", ntohl(sin->sin_addr.s_addr));
287 break;
288 case AF_INET6:
289 sin6 = xs_addr_in6(xprt);
290 snprintf(buf, sizeof(buf), "%pi6", &sin6->sin6_addr);
291 break;
292 default:
293 BUG();
295 xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = kstrdup(buf, GFP_KERNEL);
298 static void xs_format_common_peer_ports(struct rpc_xprt *xprt)
300 struct sockaddr *sap = xs_addr(xprt);
301 char buf[128];
303 snprintf(buf, sizeof(buf), "%u", rpc_get_port(sap));
304 xprt->address_strings[RPC_DISPLAY_PORT] = kstrdup(buf, GFP_KERNEL);
306 snprintf(buf, sizeof(buf), "%4hx", rpc_get_port(sap));
307 xprt->address_strings[RPC_DISPLAY_HEX_PORT] = kstrdup(buf, GFP_KERNEL);
310 static void xs_format_peer_addresses(struct rpc_xprt *xprt,
311 const char *protocol,
312 const char *netid)
314 xprt->address_strings[RPC_DISPLAY_PROTO] = protocol;
315 xprt->address_strings[RPC_DISPLAY_NETID] = netid;
316 xs_format_common_peer_addresses(xprt);
317 xs_format_common_peer_ports(xprt);
320 static void xs_update_peer_port(struct rpc_xprt *xprt)
322 kfree(xprt->address_strings[RPC_DISPLAY_HEX_PORT]);
323 kfree(xprt->address_strings[RPC_DISPLAY_PORT]);
325 xs_format_common_peer_ports(xprt);
328 static void xs_free_peer_addresses(struct rpc_xprt *xprt)
330 unsigned int i;
332 for (i = 0; i < RPC_DISPLAY_MAX; i++)
333 switch (i) {
334 case RPC_DISPLAY_PROTO:
335 case RPC_DISPLAY_NETID:
336 continue;
337 default:
338 kfree(xprt->address_strings[i]);
342 #define XS_SENDMSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL)
344 static int xs_send_kvec(struct socket *sock, struct sockaddr *addr, int addrlen, struct kvec *vec, unsigned int base, int more)
346 struct msghdr msg = {
347 .msg_name = addr,
348 .msg_namelen = addrlen,
349 .msg_flags = XS_SENDMSG_FLAGS | (more ? MSG_MORE : 0),
351 struct kvec iov = {
352 .iov_base = vec->iov_base + base,
353 .iov_len = vec->iov_len - base,
356 if (iov.iov_len != 0)
357 return kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len);
358 return kernel_sendmsg(sock, &msg, NULL, 0, 0);
361 static int xs_send_pagedata(struct socket *sock, struct xdr_buf *xdr, unsigned int base, int more)
363 struct page **ppage;
364 unsigned int remainder;
365 int err, sent = 0;
367 remainder = xdr->page_len - base;
368 base += xdr->page_base;
369 ppage = xdr->pages + (base >> PAGE_SHIFT);
370 base &= ~PAGE_MASK;
371 for(;;) {
372 unsigned int len = min_t(unsigned int, PAGE_SIZE - base, remainder);
373 int flags = XS_SENDMSG_FLAGS;
375 remainder -= len;
376 if (remainder != 0 || more)
377 flags |= MSG_MORE;
378 err = sock->ops->sendpage(sock, *ppage, base, len, flags);
379 if (remainder == 0 || err != len)
380 break;
381 sent += err;
382 ppage++;
383 base = 0;
385 if (sent == 0)
386 return err;
387 if (err > 0)
388 sent += err;
389 return sent;
393 * xs_sendpages - write pages directly to a socket
394 * @sock: socket to send on
395 * @addr: UDP only -- address of destination
396 * @addrlen: UDP only -- length of destination address
397 * @xdr: buffer containing this request
398 * @base: starting position in the buffer
401 static int xs_sendpages(struct socket *sock, struct sockaddr *addr, int addrlen, struct xdr_buf *xdr, unsigned int base)
403 unsigned int remainder = xdr->len - base;
404 int err, sent = 0;
406 if (unlikely(!sock))
407 return -ENOTSOCK;
409 clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags);
410 if (base != 0) {
411 addr = NULL;
412 addrlen = 0;
415 if (base < xdr->head[0].iov_len || addr != NULL) {
416 unsigned int len = xdr->head[0].iov_len - base;
417 remainder -= len;
418 err = xs_send_kvec(sock, addr, addrlen, &xdr->head[0], base, remainder != 0);
419 if (remainder == 0 || err != len)
420 goto out;
421 sent += err;
422 base = 0;
423 } else
424 base -= xdr->head[0].iov_len;
426 if (base < xdr->page_len) {
427 unsigned int len = xdr->page_len - base;
428 remainder -= len;
429 err = xs_send_pagedata(sock, xdr, base, remainder != 0);
430 if (remainder == 0 || err != len)
431 goto out;
432 sent += err;
433 base = 0;
434 } else
435 base -= xdr->page_len;
437 if (base >= xdr->tail[0].iov_len)
438 return sent;
439 err = xs_send_kvec(sock, NULL, 0, &xdr->tail[0], base, 0);
440 out:
441 if (sent == 0)
442 return err;
443 if (err > 0)
444 sent += err;
445 return sent;
448 static void xs_nospace_callback(struct rpc_task *task)
450 struct sock_xprt *transport = container_of(task->tk_rqstp->rq_xprt, struct sock_xprt, xprt);
452 transport->inet->sk_write_pending--;
453 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
457 * xs_nospace - place task on wait queue if transmit was incomplete
458 * @task: task to put to sleep
461 static int xs_nospace(struct rpc_task *task)
463 struct rpc_rqst *req = task->tk_rqstp;
464 struct rpc_xprt *xprt = req->rq_xprt;
465 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
466 int ret = 0;
468 dprintk("RPC: %5u xmit incomplete (%u left of %u)\n",
469 task->tk_pid, req->rq_slen - req->rq_bytes_sent,
470 req->rq_slen);
472 /* Protect against races with write_space */
473 spin_lock_bh(&xprt->transport_lock);
475 /* Don't race with disconnect */
476 if (xprt_connected(xprt)) {
477 if (test_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags)) {
478 ret = -EAGAIN;
480 * Notify TCP that we're limited by the application
481 * window size
483 set_bit(SOCK_NOSPACE, &transport->sock->flags);
484 transport->inet->sk_write_pending++;
485 /* ...and wait for more buffer space */
486 xprt_wait_for_buffer_space(task, xs_nospace_callback);
488 } else {
489 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
490 ret = -ENOTCONN;
493 spin_unlock_bh(&xprt->transport_lock);
494 return ret;
498 * xs_udp_send_request - write an RPC request to a UDP socket
499 * @task: address of RPC task that manages the state of an RPC request
501 * Return values:
502 * 0: The request has been sent
503 * EAGAIN: The socket was blocked, please call again later to
504 * complete the request
505 * ENOTCONN: Caller needs to invoke connect logic then call again
506 * other: Some other error occured, the request was not sent
508 static int xs_udp_send_request(struct rpc_task *task)
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 xdr_buf *xdr = &req->rq_snd_buf;
514 int status;
516 xs_pktdump("packet data:",
517 req->rq_svec->iov_base,
518 req->rq_svec->iov_len);
520 if (!xprt_bound(xprt))
521 return -ENOTCONN;
522 status = xs_sendpages(transport->sock,
523 xs_addr(xprt),
524 xprt->addrlen, xdr,
525 req->rq_bytes_sent);
527 dprintk("RPC: xs_udp_send_request(%u) = %d\n",
528 xdr->len - req->rq_bytes_sent, status);
530 if (status >= 0) {
531 req->rq_xmit_bytes_sent += status;
532 if (status >= req->rq_slen)
533 return 0;
534 /* Still some bytes left; set up for a retry later. */
535 status = -EAGAIN;
538 switch (status) {
539 case -ENOTSOCK:
540 status = -ENOTCONN;
541 /* Should we call xs_close() here? */
542 break;
543 case -EAGAIN:
544 status = xs_nospace(task);
545 break;
546 default:
547 dprintk("RPC: sendmsg returned unrecognized error %d\n",
548 -status);
549 case -ENETUNREACH:
550 case -EPIPE:
551 case -ECONNREFUSED:
552 /* When the server has died, an ICMP port unreachable message
553 * prompts ECONNREFUSED. */
554 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
557 return status;
561 * xs_tcp_shutdown - gracefully shut down a TCP socket
562 * @xprt: transport
564 * Initiates a graceful shutdown of the TCP socket by calling the
565 * equivalent of shutdown(SHUT_WR);
567 static void xs_tcp_shutdown(struct rpc_xprt *xprt)
569 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
570 struct socket *sock = transport->sock;
572 if (sock != NULL)
573 kernel_sock_shutdown(sock, SHUT_WR);
576 static inline void xs_encode_tcp_record_marker(struct xdr_buf *buf)
578 u32 reclen = buf->len - sizeof(rpc_fraghdr);
579 rpc_fraghdr *base = buf->head[0].iov_base;
580 *base = htonl(RPC_LAST_STREAM_FRAGMENT | reclen);
584 * xs_tcp_send_request - write an RPC request to a TCP socket
585 * @task: address of RPC task that manages the state of an RPC request
587 * Return values:
588 * 0: The request has been sent
589 * EAGAIN: The socket was blocked, please call again later to
590 * complete the request
591 * ENOTCONN: Caller needs to invoke connect logic then call again
592 * other: Some other error occured, the request was not sent
594 * XXX: In the case of soft timeouts, should we eventually give up
595 * if sendmsg is not able to make progress?
597 static int xs_tcp_send_request(struct rpc_task *task)
599 struct rpc_rqst *req = task->tk_rqstp;
600 struct rpc_xprt *xprt = req->rq_xprt;
601 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
602 struct xdr_buf *xdr = &req->rq_snd_buf;
603 int status;
605 xs_encode_tcp_record_marker(&req->rq_snd_buf);
607 xs_pktdump("packet data:",
608 req->rq_svec->iov_base,
609 req->rq_svec->iov_len);
611 /* Continue transmitting the packet/record. We must be careful
612 * to cope with writespace callbacks arriving _after_ we have
613 * called sendmsg(). */
614 while (1) {
615 status = xs_sendpages(transport->sock,
616 NULL, 0, xdr, req->rq_bytes_sent);
618 dprintk("RPC: xs_tcp_send_request(%u) = %d\n",
619 xdr->len - req->rq_bytes_sent, status);
621 if (unlikely(status < 0))
622 break;
624 /* If we've sent the entire packet, immediately
625 * reset the count of bytes sent. */
626 req->rq_bytes_sent += status;
627 req->rq_xmit_bytes_sent += status;
628 if (likely(req->rq_bytes_sent >= req->rq_slen)) {
629 req->rq_bytes_sent = 0;
630 return 0;
633 if (status != 0)
634 continue;
635 status = -EAGAIN;
636 break;
639 switch (status) {
640 case -ENOTSOCK:
641 status = -ENOTCONN;
642 /* Should we call xs_close() here? */
643 break;
644 case -EAGAIN:
645 status = xs_nospace(task);
646 break;
647 default:
648 dprintk("RPC: sendmsg returned unrecognized error %d\n",
649 -status);
650 case -ECONNRESET:
651 case -EPIPE:
652 xs_tcp_shutdown(xprt);
653 case -ECONNREFUSED:
654 case -ENOTCONN:
655 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
658 return status;
662 * xs_tcp_release_xprt - clean up after a tcp transmission
663 * @xprt: transport
664 * @task: rpc task
666 * This cleans up if an error causes us to abort the transmission of a request.
667 * In this case, the socket may need to be reset in order to avoid confusing
668 * the server.
670 static void xs_tcp_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
672 struct rpc_rqst *req;
674 if (task != xprt->snd_task)
675 return;
676 if (task == NULL)
677 goto out_release;
678 req = task->tk_rqstp;
679 if (req->rq_bytes_sent == 0)
680 goto out_release;
681 if (req->rq_bytes_sent == req->rq_snd_buf.len)
682 goto out_release;
683 set_bit(XPRT_CLOSE_WAIT, &task->tk_xprt->state);
684 out_release:
685 xprt_release_xprt(xprt, task);
688 static void xs_save_old_callbacks(struct sock_xprt *transport, struct sock *sk)
690 transport->old_data_ready = sk->sk_data_ready;
691 transport->old_state_change = sk->sk_state_change;
692 transport->old_write_space = sk->sk_write_space;
693 transport->old_error_report = sk->sk_error_report;
696 static void xs_restore_old_callbacks(struct sock_xprt *transport, struct sock *sk)
698 sk->sk_data_ready = transport->old_data_ready;
699 sk->sk_state_change = transport->old_state_change;
700 sk->sk_write_space = transport->old_write_space;
701 sk->sk_error_report = transport->old_error_report;
704 static void xs_reset_transport(struct sock_xprt *transport)
706 struct socket *sock = transport->sock;
707 struct sock *sk = transport->inet;
709 if (sk == NULL)
710 return;
712 write_lock_bh(&sk->sk_callback_lock);
713 transport->inet = NULL;
714 transport->sock = NULL;
716 sk->sk_user_data = NULL;
718 xs_restore_old_callbacks(transport, sk);
719 write_unlock_bh(&sk->sk_callback_lock);
721 sk->sk_no_check = 0;
723 sock_release(sock);
727 * xs_close - close a socket
728 * @xprt: transport
730 * This is used when all requests are complete; ie, no DRC state remains
731 * on the server we want to save.
733 * The caller _must_ be holding XPRT_LOCKED in order to avoid issues with
734 * xs_reset_transport() zeroing the socket from underneath a writer.
736 static void xs_close(struct rpc_xprt *xprt)
738 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
740 dprintk("RPC: xs_close xprt %p\n", xprt);
742 xs_reset_transport(transport);
743 xprt->reestablish_timeout = 0;
745 smp_mb__before_clear_bit();
746 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
747 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
748 clear_bit(XPRT_CLOSING, &xprt->state);
749 smp_mb__after_clear_bit();
750 xprt_disconnect_done(xprt);
753 static void xs_tcp_close(struct rpc_xprt *xprt)
755 if (test_and_clear_bit(XPRT_CONNECTION_CLOSE, &xprt->state))
756 xs_close(xprt);
757 else
758 xs_tcp_shutdown(xprt);
762 * xs_destroy - prepare to shutdown a transport
763 * @xprt: doomed transport
766 static void xs_destroy(struct rpc_xprt *xprt)
768 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
770 dprintk("RPC: xs_destroy xprt %p\n", xprt);
772 cancel_rearming_delayed_work(&transport->connect_worker);
774 xs_close(xprt);
775 xs_free_peer_addresses(xprt);
776 kfree(xprt->slot);
777 kfree(xprt);
778 module_put(THIS_MODULE);
781 static inline struct rpc_xprt *xprt_from_sock(struct sock *sk)
783 return (struct rpc_xprt *) sk->sk_user_data;
787 * xs_udp_data_ready - "data ready" callback for UDP sockets
788 * @sk: socket with data to read
789 * @len: how much data to read
792 static void xs_udp_data_ready(struct sock *sk, int len)
794 struct rpc_task *task;
795 struct rpc_xprt *xprt;
796 struct rpc_rqst *rovr;
797 struct sk_buff *skb;
798 int err, repsize, copied;
799 u32 _xid;
800 __be32 *xp;
802 read_lock(&sk->sk_callback_lock);
803 dprintk("RPC: xs_udp_data_ready...\n");
804 if (!(xprt = xprt_from_sock(sk)))
805 goto out;
807 if ((skb = skb_recv_datagram(sk, 0, 1, &err)) == NULL)
808 goto out;
810 if (xprt->shutdown)
811 goto dropit;
813 repsize = skb->len - sizeof(struct udphdr);
814 if (repsize < 4) {
815 dprintk("RPC: impossible RPC reply size %d!\n", repsize);
816 goto dropit;
819 /* Copy the XID from the skb... */
820 xp = skb_header_pointer(skb, sizeof(struct udphdr),
821 sizeof(_xid), &_xid);
822 if (xp == NULL)
823 goto dropit;
825 /* Look up and lock the request corresponding to the given XID */
826 spin_lock(&xprt->transport_lock);
827 rovr = xprt_lookup_rqst(xprt, *xp);
828 if (!rovr)
829 goto out_unlock;
830 task = rovr->rq_task;
832 if ((copied = rovr->rq_private_buf.buflen) > repsize)
833 copied = repsize;
835 /* Suck it into the iovec, verify checksum if not done by hw. */
836 if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) {
837 UDPX_INC_STATS_BH(sk, UDP_MIB_INERRORS);
838 goto out_unlock;
841 UDPX_INC_STATS_BH(sk, UDP_MIB_INDATAGRAMS);
843 /* Something worked... */
844 dst_confirm(skb_dst(skb));
846 xprt_adjust_cwnd(task, copied);
847 xprt_complete_rqst(task, copied);
849 out_unlock:
850 spin_unlock(&xprt->transport_lock);
851 dropit:
852 skb_free_datagram(sk, skb);
853 out:
854 read_unlock(&sk->sk_callback_lock);
857 static inline void xs_tcp_read_fraghdr(struct rpc_xprt *xprt, struct xdr_skb_reader *desc)
859 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
860 size_t len, used;
861 char *p;
863 p = ((char *) &transport->tcp_fraghdr) + transport->tcp_offset;
864 len = sizeof(transport->tcp_fraghdr) - transport->tcp_offset;
865 used = xdr_skb_read_bits(desc, p, len);
866 transport->tcp_offset += used;
867 if (used != len)
868 return;
870 transport->tcp_reclen = ntohl(transport->tcp_fraghdr);
871 if (transport->tcp_reclen & RPC_LAST_STREAM_FRAGMENT)
872 transport->tcp_flags |= TCP_RCV_LAST_FRAG;
873 else
874 transport->tcp_flags &= ~TCP_RCV_LAST_FRAG;
875 transport->tcp_reclen &= RPC_FRAGMENT_SIZE_MASK;
877 transport->tcp_flags &= ~TCP_RCV_COPY_FRAGHDR;
878 transport->tcp_offset = 0;
880 /* Sanity check of the record length */
881 if (unlikely(transport->tcp_reclen < 8)) {
882 dprintk("RPC: invalid TCP record fragment length\n");
883 xprt_force_disconnect(xprt);
884 return;
886 dprintk("RPC: reading TCP record fragment of length %d\n",
887 transport->tcp_reclen);
890 static void xs_tcp_check_fraghdr(struct sock_xprt *transport)
892 if (transport->tcp_offset == transport->tcp_reclen) {
893 transport->tcp_flags |= TCP_RCV_COPY_FRAGHDR;
894 transport->tcp_offset = 0;
895 if (transport->tcp_flags & TCP_RCV_LAST_FRAG) {
896 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
897 transport->tcp_flags |= TCP_RCV_COPY_XID;
898 transport->tcp_copied = 0;
903 static inline void xs_tcp_read_xid(struct sock_xprt *transport, struct xdr_skb_reader *desc)
905 size_t len, used;
906 char *p;
908 len = sizeof(transport->tcp_xid) - transport->tcp_offset;
909 dprintk("RPC: reading XID (%Zu bytes)\n", len);
910 p = ((char *) &transport->tcp_xid) + transport->tcp_offset;
911 used = xdr_skb_read_bits(desc, p, len);
912 transport->tcp_offset += used;
913 if (used != len)
914 return;
915 transport->tcp_flags &= ~TCP_RCV_COPY_XID;
916 transport->tcp_flags |= TCP_RCV_READ_CALLDIR;
917 transport->tcp_copied = 4;
918 dprintk("RPC: reading %s XID %08x\n",
919 (transport->tcp_flags & TCP_RPC_REPLY) ? "reply for"
920 : "request with",
921 ntohl(transport->tcp_xid));
922 xs_tcp_check_fraghdr(transport);
925 static inline void xs_tcp_read_calldir(struct sock_xprt *transport,
926 struct xdr_skb_reader *desc)
928 size_t len, used;
929 u32 offset;
930 __be32 calldir;
933 * We want transport->tcp_offset to be 8 at the end of this routine
934 * (4 bytes for the xid and 4 bytes for the call/reply flag).
935 * When this function is called for the first time,
936 * transport->tcp_offset is 4 (after having already read the xid).
938 offset = transport->tcp_offset - sizeof(transport->tcp_xid);
939 len = sizeof(calldir) - offset;
940 dprintk("RPC: reading CALL/REPLY flag (%Zu bytes)\n", len);
941 used = xdr_skb_read_bits(desc, &calldir, len);
942 transport->tcp_offset += used;
943 if (used != len)
944 return;
945 transport->tcp_flags &= ~TCP_RCV_READ_CALLDIR;
946 transport->tcp_flags |= TCP_RCV_COPY_CALLDIR;
947 transport->tcp_flags |= TCP_RCV_COPY_DATA;
949 * We don't yet have the XDR buffer, so we will write the calldir
950 * out after we get the buffer from the 'struct rpc_rqst'
952 if (ntohl(calldir) == RPC_REPLY)
953 transport->tcp_flags |= TCP_RPC_REPLY;
954 else
955 transport->tcp_flags &= ~TCP_RPC_REPLY;
956 dprintk("RPC: reading %s CALL/REPLY flag %08x\n",
957 (transport->tcp_flags & TCP_RPC_REPLY) ?
958 "reply for" : "request with", calldir);
959 xs_tcp_check_fraghdr(transport);
962 static inline void xs_tcp_read_common(struct rpc_xprt *xprt,
963 struct xdr_skb_reader *desc,
964 struct rpc_rqst *req)
966 struct sock_xprt *transport =
967 container_of(xprt, struct sock_xprt, xprt);
968 struct xdr_buf *rcvbuf;
969 size_t len;
970 ssize_t r;
972 rcvbuf = &req->rq_private_buf;
974 if (transport->tcp_flags & TCP_RCV_COPY_CALLDIR) {
976 * Save the RPC direction in the XDR buffer
978 __be32 calldir = transport->tcp_flags & TCP_RPC_REPLY ?
979 htonl(RPC_REPLY) : 0;
981 memcpy(rcvbuf->head[0].iov_base + transport->tcp_copied,
982 &calldir, sizeof(calldir));
983 transport->tcp_copied += sizeof(calldir);
984 transport->tcp_flags &= ~TCP_RCV_COPY_CALLDIR;
987 len = desc->count;
988 if (len > transport->tcp_reclen - transport->tcp_offset) {
989 struct xdr_skb_reader my_desc;
991 len = transport->tcp_reclen - transport->tcp_offset;
992 memcpy(&my_desc, desc, sizeof(my_desc));
993 my_desc.count = len;
994 r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
995 &my_desc, xdr_skb_read_bits);
996 desc->count -= r;
997 desc->offset += r;
998 } else
999 r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
1000 desc, xdr_skb_read_bits);
1002 if (r > 0) {
1003 transport->tcp_copied += r;
1004 transport->tcp_offset += r;
1006 if (r != len) {
1007 /* Error when copying to the receive buffer,
1008 * usually because we weren't able to allocate
1009 * additional buffer pages. All we can do now
1010 * is turn off TCP_RCV_COPY_DATA, so the request
1011 * will not receive any additional updates,
1012 * and time out.
1013 * Any remaining data from this record will
1014 * be discarded.
1016 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1017 dprintk("RPC: XID %08x truncated request\n",
1018 ntohl(transport->tcp_xid));
1019 dprintk("RPC: xprt = %p, tcp_copied = %lu, "
1020 "tcp_offset = %u, tcp_reclen = %u\n",
1021 xprt, transport->tcp_copied,
1022 transport->tcp_offset, transport->tcp_reclen);
1023 return;
1026 dprintk("RPC: XID %08x read %Zd bytes\n",
1027 ntohl(transport->tcp_xid), r);
1028 dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, "
1029 "tcp_reclen = %u\n", xprt, transport->tcp_copied,
1030 transport->tcp_offset, transport->tcp_reclen);
1032 if (transport->tcp_copied == req->rq_private_buf.buflen)
1033 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1034 else if (transport->tcp_offset == transport->tcp_reclen) {
1035 if (transport->tcp_flags & TCP_RCV_LAST_FRAG)
1036 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1041 * Finds the request corresponding to the RPC xid and invokes the common
1042 * tcp read code to read the data.
1044 static inline int xs_tcp_read_reply(struct rpc_xprt *xprt,
1045 struct xdr_skb_reader *desc)
1047 struct sock_xprt *transport =
1048 container_of(xprt, struct sock_xprt, xprt);
1049 struct rpc_rqst *req;
1051 dprintk("RPC: read reply XID %08x\n", ntohl(transport->tcp_xid));
1053 /* Find and lock the request corresponding to this xid */
1054 spin_lock(&xprt->transport_lock);
1055 req = xprt_lookup_rqst(xprt, transport->tcp_xid);
1056 if (!req) {
1057 dprintk("RPC: XID %08x request not found!\n",
1058 ntohl(transport->tcp_xid));
1059 spin_unlock(&xprt->transport_lock);
1060 return -1;
1063 xs_tcp_read_common(xprt, desc, req);
1065 if (!(transport->tcp_flags & TCP_RCV_COPY_DATA))
1066 xprt_complete_rqst(req->rq_task, transport->tcp_copied);
1068 spin_unlock(&xprt->transport_lock);
1069 return 0;
1072 #if defined(CONFIG_NFS_V4_1)
1074 * Obtains an rpc_rqst previously allocated and invokes the common
1075 * tcp read code to read the data. The result is placed in the callback
1076 * queue.
1077 * If we're unable to obtain the rpc_rqst we schedule the closing of the
1078 * connection and return -1.
1080 static inline int xs_tcp_read_callback(struct rpc_xprt *xprt,
1081 struct xdr_skb_reader *desc)
1083 struct sock_xprt *transport =
1084 container_of(xprt, struct sock_xprt, xprt);
1085 struct rpc_rqst *req;
1087 req = xprt_alloc_bc_request(xprt);
1088 if (req == NULL) {
1089 printk(KERN_WARNING "Callback slot table overflowed\n");
1090 xprt_force_disconnect(xprt);
1091 return -1;
1094 req->rq_xid = transport->tcp_xid;
1095 dprintk("RPC: read callback XID %08x\n", ntohl(req->rq_xid));
1096 xs_tcp_read_common(xprt, desc, req);
1098 if (!(transport->tcp_flags & TCP_RCV_COPY_DATA)) {
1099 struct svc_serv *bc_serv = xprt->bc_serv;
1102 * Add callback request to callback list. The callback
1103 * service sleeps on the sv_cb_waitq waiting for new
1104 * requests. Wake it up after adding enqueing the
1105 * request.
1107 dprintk("RPC: add callback request to list\n");
1108 spin_lock(&bc_serv->sv_cb_lock);
1109 list_add(&req->rq_bc_list, &bc_serv->sv_cb_list);
1110 spin_unlock(&bc_serv->sv_cb_lock);
1111 wake_up(&bc_serv->sv_cb_waitq);
1114 req->rq_private_buf.len = transport->tcp_copied;
1116 return 0;
1119 static inline int _xs_tcp_read_data(struct rpc_xprt *xprt,
1120 struct xdr_skb_reader *desc)
1122 struct sock_xprt *transport =
1123 container_of(xprt, struct sock_xprt, xprt);
1125 return (transport->tcp_flags & TCP_RPC_REPLY) ?
1126 xs_tcp_read_reply(xprt, desc) :
1127 xs_tcp_read_callback(xprt, desc);
1129 #else
1130 static inline int _xs_tcp_read_data(struct rpc_xprt *xprt,
1131 struct xdr_skb_reader *desc)
1133 return xs_tcp_read_reply(xprt, desc);
1135 #endif /* CONFIG_NFS_V4_1 */
1138 * Read data off the transport. This can be either an RPC_CALL or an
1139 * RPC_REPLY. Relay the processing to helper functions.
1141 static void xs_tcp_read_data(struct rpc_xprt *xprt,
1142 struct xdr_skb_reader *desc)
1144 struct sock_xprt *transport =
1145 container_of(xprt, struct sock_xprt, xprt);
1147 if (_xs_tcp_read_data(xprt, desc) == 0)
1148 xs_tcp_check_fraghdr(transport);
1149 else {
1151 * The transport_lock protects the request handling.
1152 * There's no need to hold it to update the tcp_flags.
1154 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1158 static inline void xs_tcp_read_discard(struct sock_xprt *transport, struct xdr_skb_reader *desc)
1160 size_t len;
1162 len = transport->tcp_reclen - transport->tcp_offset;
1163 if (len > desc->count)
1164 len = desc->count;
1165 desc->count -= len;
1166 desc->offset += len;
1167 transport->tcp_offset += len;
1168 dprintk("RPC: discarded %Zu bytes\n", len);
1169 xs_tcp_check_fraghdr(transport);
1172 static int xs_tcp_data_recv(read_descriptor_t *rd_desc, struct sk_buff *skb, unsigned int offset, size_t len)
1174 struct rpc_xprt *xprt = rd_desc->arg.data;
1175 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1176 struct xdr_skb_reader desc = {
1177 .skb = skb,
1178 .offset = offset,
1179 .count = len,
1182 dprintk("RPC: xs_tcp_data_recv started\n");
1183 do {
1184 /* Read in a new fragment marker if necessary */
1185 /* Can we ever really expect to get completely empty fragments? */
1186 if (transport->tcp_flags & TCP_RCV_COPY_FRAGHDR) {
1187 xs_tcp_read_fraghdr(xprt, &desc);
1188 continue;
1190 /* Read in the xid if necessary */
1191 if (transport->tcp_flags & TCP_RCV_COPY_XID) {
1192 xs_tcp_read_xid(transport, &desc);
1193 continue;
1195 /* Read in the call/reply flag */
1196 if (transport->tcp_flags & TCP_RCV_READ_CALLDIR) {
1197 xs_tcp_read_calldir(transport, &desc);
1198 continue;
1200 /* Read in the request data */
1201 if (transport->tcp_flags & TCP_RCV_COPY_DATA) {
1202 xs_tcp_read_data(xprt, &desc);
1203 continue;
1205 /* Skip over any trailing bytes on short reads */
1206 xs_tcp_read_discard(transport, &desc);
1207 } while (desc.count);
1208 dprintk("RPC: xs_tcp_data_recv done\n");
1209 return len - desc.count;
1213 * xs_tcp_data_ready - "data ready" callback for TCP sockets
1214 * @sk: socket with data to read
1215 * @bytes: how much data to read
1218 static void xs_tcp_data_ready(struct sock *sk, int bytes)
1220 struct rpc_xprt *xprt;
1221 read_descriptor_t rd_desc;
1222 int read;
1224 dprintk("RPC: xs_tcp_data_ready...\n");
1226 read_lock(&sk->sk_callback_lock);
1227 if (!(xprt = xprt_from_sock(sk)))
1228 goto out;
1229 if (xprt->shutdown)
1230 goto out;
1232 /* Any data means we had a useful conversation, so
1233 * the we don't need to delay the next reconnect
1235 if (xprt->reestablish_timeout)
1236 xprt->reestablish_timeout = 0;
1238 /* We use rd_desc to pass struct xprt to xs_tcp_data_recv */
1239 rd_desc.arg.data = xprt;
1240 do {
1241 rd_desc.count = 65536;
1242 read = tcp_read_sock(sk, &rd_desc, xs_tcp_data_recv);
1243 } while (read > 0);
1244 out:
1245 read_unlock(&sk->sk_callback_lock);
1249 * Do the equivalent of linger/linger2 handling for dealing with
1250 * broken servers that don't close the socket in a timely
1251 * fashion
1253 static void xs_tcp_schedule_linger_timeout(struct rpc_xprt *xprt,
1254 unsigned long timeout)
1256 struct sock_xprt *transport;
1258 if (xprt_test_and_set_connecting(xprt))
1259 return;
1260 set_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1261 transport = container_of(xprt, struct sock_xprt, xprt);
1262 queue_delayed_work(rpciod_workqueue, &transport->connect_worker,
1263 timeout);
1266 static void xs_tcp_cancel_linger_timeout(struct rpc_xprt *xprt)
1268 struct sock_xprt *transport;
1270 transport = container_of(xprt, struct sock_xprt, xprt);
1272 if (!test_bit(XPRT_CONNECTION_ABORT, &xprt->state) ||
1273 !cancel_delayed_work(&transport->connect_worker))
1274 return;
1275 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1276 xprt_clear_connecting(xprt);
1279 static void xs_sock_mark_closed(struct rpc_xprt *xprt)
1281 smp_mb__before_clear_bit();
1282 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
1283 clear_bit(XPRT_CLOSING, &xprt->state);
1284 smp_mb__after_clear_bit();
1285 /* Mark transport as closed and wake up all pending tasks */
1286 xprt_disconnect_done(xprt);
1290 * xs_tcp_state_change - callback to handle TCP socket state changes
1291 * @sk: socket whose state has changed
1294 static void xs_tcp_state_change(struct sock *sk)
1296 struct rpc_xprt *xprt;
1298 read_lock(&sk->sk_callback_lock);
1299 if (!(xprt = xprt_from_sock(sk)))
1300 goto out;
1301 dprintk("RPC: xs_tcp_state_change client %p...\n", xprt);
1302 dprintk("RPC: state %x conn %d dead %d zapped %d\n",
1303 sk->sk_state, xprt_connected(xprt),
1304 sock_flag(sk, SOCK_DEAD),
1305 sock_flag(sk, SOCK_ZAPPED));
1307 switch (sk->sk_state) {
1308 case TCP_ESTABLISHED:
1309 spin_lock_bh(&xprt->transport_lock);
1310 if (!xprt_test_and_set_connected(xprt)) {
1311 struct sock_xprt *transport = container_of(xprt,
1312 struct sock_xprt, xprt);
1314 /* Reset TCP record info */
1315 transport->tcp_offset = 0;
1316 transport->tcp_reclen = 0;
1317 transport->tcp_copied = 0;
1318 transport->tcp_flags =
1319 TCP_RCV_COPY_FRAGHDR | TCP_RCV_COPY_XID;
1321 xprt_wake_pending_tasks(xprt, -EAGAIN);
1323 spin_unlock_bh(&xprt->transport_lock);
1324 break;
1325 case TCP_FIN_WAIT1:
1326 /* The client initiated a shutdown of the socket */
1327 xprt->connect_cookie++;
1328 xprt->reestablish_timeout = 0;
1329 set_bit(XPRT_CLOSING, &xprt->state);
1330 smp_mb__before_clear_bit();
1331 clear_bit(XPRT_CONNECTED, &xprt->state);
1332 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
1333 smp_mb__after_clear_bit();
1334 xs_tcp_schedule_linger_timeout(xprt, xs_tcp_fin_timeout);
1335 break;
1336 case TCP_CLOSE_WAIT:
1337 /* The server initiated a shutdown of the socket */
1338 xprt_force_disconnect(xprt);
1339 case TCP_SYN_SENT:
1340 xprt->connect_cookie++;
1341 case TCP_CLOSING:
1343 * If the server closed down the connection, make sure that
1344 * we back off before reconnecting
1346 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
1347 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
1348 break;
1349 case TCP_LAST_ACK:
1350 set_bit(XPRT_CLOSING, &xprt->state);
1351 xs_tcp_schedule_linger_timeout(xprt, xs_tcp_fin_timeout);
1352 smp_mb__before_clear_bit();
1353 clear_bit(XPRT_CONNECTED, &xprt->state);
1354 smp_mb__after_clear_bit();
1355 break;
1356 case TCP_CLOSE:
1357 xs_tcp_cancel_linger_timeout(xprt);
1358 xs_sock_mark_closed(xprt);
1360 out:
1361 read_unlock(&sk->sk_callback_lock);
1365 * xs_error_report - callback mainly for catching socket errors
1366 * @sk: socket
1368 static void xs_error_report(struct sock *sk)
1370 struct rpc_xprt *xprt;
1372 read_lock(&sk->sk_callback_lock);
1373 if (!(xprt = xprt_from_sock(sk)))
1374 goto out;
1375 dprintk("RPC: %s client %p...\n"
1376 "RPC: error %d\n",
1377 __func__, xprt, sk->sk_err);
1378 xprt_wake_pending_tasks(xprt, -EAGAIN);
1379 out:
1380 read_unlock(&sk->sk_callback_lock);
1383 static void xs_write_space(struct sock *sk)
1385 struct socket *sock;
1386 struct rpc_xprt *xprt;
1388 if (unlikely(!(sock = sk->sk_socket)))
1389 return;
1390 clear_bit(SOCK_NOSPACE, &sock->flags);
1392 if (unlikely(!(xprt = xprt_from_sock(sk))))
1393 return;
1394 if (test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags) == 0)
1395 return;
1397 xprt_write_space(xprt);
1401 * xs_udp_write_space - callback invoked when socket buffer space
1402 * becomes available
1403 * @sk: socket whose state has changed
1405 * Called when more output buffer space is available for this socket.
1406 * We try not to wake our writers until they can make "significant"
1407 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1408 * with a bunch of small requests.
1410 static void xs_udp_write_space(struct sock *sk)
1412 read_lock(&sk->sk_callback_lock);
1414 /* from net/core/sock.c:sock_def_write_space */
1415 if (sock_writeable(sk))
1416 xs_write_space(sk);
1418 read_unlock(&sk->sk_callback_lock);
1422 * xs_tcp_write_space - callback invoked when socket buffer space
1423 * becomes available
1424 * @sk: socket whose state has changed
1426 * Called when more output buffer space is available for this socket.
1427 * We try not to wake our writers until they can make "significant"
1428 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1429 * with a bunch of small requests.
1431 static void xs_tcp_write_space(struct sock *sk)
1433 read_lock(&sk->sk_callback_lock);
1435 /* from net/core/stream.c:sk_stream_write_space */
1436 if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk))
1437 xs_write_space(sk);
1439 read_unlock(&sk->sk_callback_lock);
1442 static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt)
1444 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1445 struct sock *sk = transport->inet;
1447 if (transport->rcvsize) {
1448 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
1449 sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2;
1451 if (transport->sndsize) {
1452 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
1453 sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2;
1454 sk->sk_write_space(sk);
1459 * xs_udp_set_buffer_size - set send and receive limits
1460 * @xprt: generic transport
1461 * @sndsize: requested size of send buffer, in bytes
1462 * @rcvsize: requested size of receive buffer, in bytes
1464 * Set socket send and receive buffer size limits.
1466 static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize)
1468 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1470 transport->sndsize = 0;
1471 if (sndsize)
1472 transport->sndsize = sndsize + 1024;
1473 transport->rcvsize = 0;
1474 if (rcvsize)
1475 transport->rcvsize = rcvsize + 1024;
1477 xs_udp_do_set_buffer_size(xprt);
1481 * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
1482 * @task: task that timed out
1484 * Adjust the congestion window after a retransmit timeout has occurred.
1486 static void xs_udp_timer(struct rpc_task *task)
1488 xprt_adjust_cwnd(task, -ETIMEDOUT);
1491 static unsigned short xs_get_random_port(void)
1493 unsigned short range = xprt_max_resvport - xprt_min_resvport;
1494 unsigned short rand = (unsigned short) net_random() % range;
1495 return rand + xprt_min_resvport;
1499 * xs_set_port - reset the port number in the remote endpoint address
1500 * @xprt: generic transport
1501 * @port: new port number
1504 static void xs_set_port(struct rpc_xprt *xprt, unsigned short port)
1506 dprintk("RPC: setting port for xprt %p to %u\n", xprt, port);
1508 rpc_set_port(xs_addr(xprt), port);
1509 xs_update_peer_port(xprt);
1512 static unsigned short xs_get_srcport(struct sock_xprt *transport, struct socket *sock)
1514 unsigned short port = transport->srcport;
1516 if (port == 0 && transport->xprt.resvport)
1517 port = xs_get_random_port();
1518 return port;
1521 static unsigned short xs_next_srcport(struct sock_xprt *transport, struct socket *sock, unsigned short port)
1523 if (transport->srcport != 0)
1524 transport->srcport = 0;
1525 if (!transport->xprt.resvport)
1526 return 0;
1527 if (port <= xprt_min_resvport || port > xprt_max_resvport)
1528 return xprt_max_resvport;
1529 return --port;
1532 static int xs_bind4(struct sock_xprt *transport, struct socket *sock)
1534 struct sockaddr_in myaddr = {
1535 .sin_family = AF_INET,
1537 struct sockaddr_in *sa;
1538 int err, nloop = 0;
1539 unsigned short port = xs_get_srcport(transport, sock);
1540 unsigned short last;
1542 sa = (struct sockaddr_in *)&transport->srcaddr;
1543 myaddr.sin_addr = sa->sin_addr;
1544 do {
1545 myaddr.sin_port = htons(port);
1546 err = kernel_bind(sock, (struct sockaddr *) &myaddr,
1547 sizeof(myaddr));
1548 if (port == 0)
1549 break;
1550 if (err == 0) {
1551 transport->srcport = port;
1552 break;
1554 last = port;
1555 port = xs_next_srcport(transport, sock, port);
1556 if (port > last)
1557 nloop++;
1558 } while (err == -EADDRINUSE && nloop != 2);
1559 dprintk("RPC: %s %pI4:%u: %s (%d)\n",
1560 __func__, &myaddr.sin_addr,
1561 port, err ? "failed" : "ok", err);
1562 return err;
1565 static int xs_bind6(struct sock_xprt *transport, struct socket *sock)
1567 struct sockaddr_in6 myaddr = {
1568 .sin6_family = AF_INET6,
1570 struct sockaddr_in6 *sa;
1571 int err, nloop = 0;
1572 unsigned short port = xs_get_srcport(transport, sock);
1573 unsigned short last;
1575 sa = (struct sockaddr_in6 *)&transport->srcaddr;
1576 myaddr.sin6_addr = sa->sin6_addr;
1577 do {
1578 myaddr.sin6_port = htons(port);
1579 err = kernel_bind(sock, (struct sockaddr *) &myaddr,
1580 sizeof(myaddr));
1581 if (port == 0)
1582 break;
1583 if (err == 0) {
1584 transport->srcport = port;
1585 break;
1587 last = port;
1588 port = xs_next_srcport(transport, sock, port);
1589 if (port > last)
1590 nloop++;
1591 } while (err == -EADDRINUSE && nloop != 2);
1592 dprintk("RPC: xs_bind6 %pI6:%u: %s (%d)\n",
1593 &myaddr.sin6_addr, port, err ? "failed" : "ok", err);
1594 return err;
1597 #ifdef CONFIG_DEBUG_LOCK_ALLOC
1598 static struct lock_class_key xs_key[2];
1599 static struct lock_class_key xs_slock_key[2];
1601 static inline void xs_reclassify_socket4(struct socket *sock)
1603 struct sock *sk = sock->sk;
1605 BUG_ON(sock_owned_by_user(sk));
1606 sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC",
1607 &xs_slock_key[0], "sk_lock-AF_INET-RPC", &xs_key[0]);
1610 static inline void xs_reclassify_socket6(struct socket *sock)
1612 struct sock *sk = sock->sk;
1614 BUG_ON(sock_owned_by_user(sk));
1615 sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC",
1616 &xs_slock_key[1], "sk_lock-AF_INET6-RPC", &xs_key[1]);
1618 #else
1619 static inline void xs_reclassify_socket4(struct socket *sock)
1623 static inline void xs_reclassify_socket6(struct socket *sock)
1626 #endif
1628 static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
1630 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1632 if (!transport->inet) {
1633 struct sock *sk = sock->sk;
1635 write_lock_bh(&sk->sk_callback_lock);
1637 xs_save_old_callbacks(transport, sk);
1639 sk->sk_user_data = xprt;
1640 sk->sk_data_ready = xs_udp_data_ready;
1641 sk->sk_write_space = xs_udp_write_space;
1642 sk->sk_error_report = xs_error_report;
1643 sk->sk_no_check = UDP_CSUM_NORCV;
1644 sk->sk_allocation = GFP_ATOMIC;
1646 xprt_set_connected(xprt);
1648 /* Reset to new socket */
1649 transport->sock = sock;
1650 transport->inet = sk;
1652 write_unlock_bh(&sk->sk_callback_lock);
1654 xs_udp_do_set_buffer_size(xprt);
1658 * xs_udp_connect_worker4 - set up a UDP socket
1659 * @work: RPC transport to connect
1661 * Invoked by a work queue tasklet.
1663 static void xs_udp_connect_worker4(struct work_struct *work)
1665 struct sock_xprt *transport =
1666 container_of(work, struct sock_xprt, connect_worker.work);
1667 struct rpc_xprt *xprt = &transport->xprt;
1668 struct socket *sock = transport->sock;
1669 int err, status = -EIO;
1671 if (xprt->shutdown)
1672 goto out;
1674 /* Start by resetting any existing state */
1675 xs_reset_transport(transport);
1677 err = sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock);
1678 if (err < 0) {
1679 dprintk("RPC: can't create UDP transport socket (%d).\n", -err);
1680 goto out;
1682 xs_reclassify_socket4(sock);
1684 if (xs_bind4(transport, sock)) {
1685 sock_release(sock);
1686 goto out;
1689 dprintk("RPC: worker connecting xprt %p via %s to "
1690 "%s (port %s)\n", xprt,
1691 xprt->address_strings[RPC_DISPLAY_PROTO],
1692 xprt->address_strings[RPC_DISPLAY_ADDR],
1693 xprt->address_strings[RPC_DISPLAY_PORT]);
1695 xs_udp_finish_connecting(xprt, sock);
1696 status = 0;
1697 out:
1698 xprt_clear_connecting(xprt);
1699 xprt_wake_pending_tasks(xprt, status);
1703 * xs_udp_connect_worker6 - set up a UDP socket
1704 * @work: RPC transport to connect
1706 * Invoked by a work queue tasklet.
1708 static void xs_udp_connect_worker6(struct work_struct *work)
1710 struct sock_xprt *transport =
1711 container_of(work, struct sock_xprt, connect_worker.work);
1712 struct rpc_xprt *xprt = &transport->xprt;
1713 struct socket *sock = transport->sock;
1714 int err, status = -EIO;
1716 if (xprt->shutdown)
1717 goto out;
1719 /* Start by resetting any existing state */
1720 xs_reset_transport(transport);
1722 err = sock_create_kern(PF_INET6, SOCK_DGRAM, IPPROTO_UDP, &sock);
1723 if (err < 0) {
1724 dprintk("RPC: can't create UDP transport socket (%d).\n", -err);
1725 goto out;
1727 xs_reclassify_socket6(sock);
1729 if (xs_bind6(transport, sock) < 0) {
1730 sock_release(sock);
1731 goto out;
1734 dprintk("RPC: worker connecting xprt %p via %s to "
1735 "%s (port %s)\n", xprt,
1736 xprt->address_strings[RPC_DISPLAY_PROTO],
1737 xprt->address_strings[RPC_DISPLAY_ADDR],
1738 xprt->address_strings[RPC_DISPLAY_PORT]);
1740 xs_udp_finish_connecting(xprt, sock);
1741 status = 0;
1742 out:
1743 xprt_clear_connecting(xprt);
1744 xprt_wake_pending_tasks(xprt, status);
1748 * We need to preserve the port number so the reply cache on the server can
1749 * find our cached RPC replies when we get around to reconnecting.
1751 static void xs_abort_connection(struct rpc_xprt *xprt, struct sock_xprt *transport)
1753 int result;
1754 struct sockaddr any;
1756 dprintk("RPC: disconnecting xprt %p to reuse port\n", xprt);
1759 * Disconnect the transport socket by doing a connect operation
1760 * with AF_UNSPEC. This should return immediately...
1762 memset(&any, 0, sizeof(any));
1763 any.sa_family = AF_UNSPEC;
1764 result = kernel_connect(transport->sock, &any, sizeof(any), 0);
1765 if (!result)
1766 xs_sock_mark_closed(xprt);
1767 else
1768 dprintk("RPC: AF_UNSPEC connect return code %d\n",
1769 result);
1772 static void xs_tcp_reuse_connection(struct rpc_xprt *xprt, struct sock_xprt *transport)
1774 unsigned int state = transport->inet->sk_state;
1776 if (state == TCP_CLOSE && transport->sock->state == SS_UNCONNECTED)
1777 return;
1778 if ((1 << state) & (TCPF_ESTABLISHED|TCPF_SYN_SENT))
1779 return;
1780 xs_abort_connection(xprt, transport);
1783 static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
1785 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1787 if (!transport->inet) {
1788 struct sock *sk = sock->sk;
1790 write_lock_bh(&sk->sk_callback_lock);
1792 xs_save_old_callbacks(transport, sk);
1794 sk->sk_user_data = xprt;
1795 sk->sk_data_ready = xs_tcp_data_ready;
1796 sk->sk_state_change = xs_tcp_state_change;
1797 sk->sk_write_space = xs_tcp_write_space;
1798 sk->sk_error_report = xs_error_report;
1799 sk->sk_allocation = GFP_ATOMIC;
1801 /* socket options */
1802 sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
1803 sock_reset_flag(sk, SOCK_LINGER);
1804 tcp_sk(sk)->linger2 = 0;
1805 tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF;
1807 xprt_clear_connected(xprt);
1809 /* Reset to new socket */
1810 transport->sock = sock;
1811 transport->inet = sk;
1813 write_unlock_bh(&sk->sk_callback_lock);
1816 if (!xprt_bound(xprt))
1817 return -ENOTCONN;
1819 /* Tell the socket layer to start connecting... */
1820 xprt->stat.connect_count++;
1821 xprt->stat.connect_start = jiffies;
1822 return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK);
1826 * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint
1827 * @xprt: RPC transport to connect
1828 * @transport: socket transport to connect
1829 * @create_sock: function to create a socket of the correct type
1831 * Invoked by a work queue tasklet.
1833 static void xs_tcp_setup_socket(struct rpc_xprt *xprt,
1834 struct sock_xprt *transport,
1835 struct socket *(*create_sock)(struct rpc_xprt *,
1836 struct sock_xprt *))
1838 struct socket *sock = transport->sock;
1839 int status = -EIO;
1841 if (xprt->shutdown)
1842 goto out;
1844 if (!sock) {
1845 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1846 sock = create_sock(xprt, transport);
1847 if (IS_ERR(sock)) {
1848 status = PTR_ERR(sock);
1849 goto out;
1851 } else {
1852 int abort_and_exit;
1854 abort_and_exit = test_and_clear_bit(XPRT_CONNECTION_ABORT,
1855 &xprt->state);
1856 /* "close" the socket, preserving the local port */
1857 xs_tcp_reuse_connection(xprt, transport);
1859 if (abort_and_exit)
1860 goto out_eagain;
1863 dprintk("RPC: worker connecting xprt %p via %s to "
1864 "%s (port %s)\n", xprt,
1865 xprt->address_strings[RPC_DISPLAY_PROTO],
1866 xprt->address_strings[RPC_DISPLAY_ADDR],
1867 xprt->address_strings[RPC_DISPLAY_PORT]);
1869 status = xs_tcp_finish_connecting(xprt, sock);
1870 dprintk("RPC: %p connect status %d connected %d sock state %d\n",
1871 xprt, -status, xprt_connected(xprt),
1872 sock->sk->sk_state);
1873 switch (status) {
1874 default:
1875 printk("%s: connect returned unhandled error %d\n",
1876 __func__, status);
1877 case -EADDRNOTAVAIL:
1878 /* We're probably in TIME_WAIT. Get rid of existing socket,
1879 * and retry
1881 set_bit(XPRT_CONNECTION_CLOSE, &xprt->state);
1882 xprt_force_disconnect(xprt);
1883 break;
1884 case -ECONNREFUSED:
1885 case -ECONNRESET:
1886 case -ENETUNREACH:
1887 /* retry with existing socket, after a delay */
1888 case 0:
1889 case -EINPROGRESS:
1890 case -EALREADY:
1891 xprt_clear_connecting(xprt);
1892 return;
1893 case -EINVAL:
1894 /* Happens, for instance, if the user specified a link
1895 * local IPv6 address without a scope-id.
1897 goto out;
1899 out_eagain:
1900 status = -EAGAIN;
1901 out:
1902 xprt_clear_connecting(xprt);
1903 xprt_wake_pending_tasks(xprt, status);
1906 static struct socket *xs_create_tcp_sock4(struct rpc_xprt *xprt,
1907 struct sock_xprt *transport)
1909 struct socket *sock;
1910 int err;
1912 /* start from scratch */
1913 err = sock_create_kern(PF_INET, SOCK_STREAM, IPPROTO_TCP, &sock);
1914 if (err < 0) {
1915 dprintk("RPC: can't create TCP transport socket (%d).\n",
1916 -err);
1917 goto out_err;
1919 xs_reclassify_socket4(sock);
1921 if (xs_bind4(transport, sock) < 0) {
1922 sock_release(sock);
1923 goto out_err;
1925 return sock;
1926 out_err:
1927 return ERR_PTR(-EIO);
1931 * xs_tcp_connect_worker4 - connect a TCP socket to a remote endpoint
1932 * @work: RPC transport to connect
1934 * Invoked by a work queue tasklet.
1936 static void xs_tcp_connect_worker4(struct work_struct *work)
1938 struct sock_xprt *transport =
1939 container_of(work, struct sock_xprt, connect_worker.work);
1940 struct rpc_xprt *xprt = &transport->xprt;
1942 xs_tcp_setup_socket(xprt, transport, xs_create_tcp_sock4);
1945 static struct socket *xs_create_tcp_sock6(struct rpc_xprt *xprt,
1946 struct sock_xprt *transport)
1948 struct socket *sock;
1949 int err;
1951 /* start from scratch */
1952 err = sock_create_kern(PF_INET6, SOCK_STREAM, IPPROTO_TCP, &sock);
1953 if (err < 0) {
1954 dprintk("RPC: can't create TCP transport socket (%d).\n",
1955 -err);
1956 goto out_err;
1958 xs_reclassify_socket6(sock);
1960 if (xs_bind6(transport, sock) < 0) {
1961 sock_release(sock);
1962 goto out_err;
1964 return sock;
1965 out_err:
1966 return ERR_PTR(-EIO);
1970 * xs_tcp_connect_worker6 - connect a TCP socket to a remote endpoint
1971 * @work: RPC transport to connect
1973 * Invoked by a work queue tasklet.
1975 static void xs_tcp_connect_worker6(struct work_struct *work)
1977 struct sock_xprt *transport =
1978 container_of(work, struct sock_xprt, connect_worker.work);
1979 struct rpc_xprt *xprt = &transport->xprt;
1981 xs_tcp_setup_socket(xprt, transport, xs_create_tcp_sock6);
1985 * xs_connect - connect a socket to a remote endpoint
1986 * @task: address of RPC task that manages state of connect request
1988 * TCP: If the remote end dropped the connection, delay reconnecting.
1990 * UDP socket connects are synchronous, but we use a work queue anyway
1991 * to guarantee that even unprivileged user processes can set up a
1992 * socket on a privileged port.
1994 * If a UDP socket connect fails, the delay behavior here prevents
1995 * retry floods (hard mounts).
1997 static void xs_connect(struct rpc_task *task)
1999 struct rpc_xprt *xprt = task->tk_xprt;
2000 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2002 if (transport->sock != NULL && !RPC_IS_SOFTCONN(task)) {
2003 dprintk("RPC: xs_connect delayed xprt %p for %lu "
2004 "seconds\n",
2005 xprt, xprt->reestablish_timeout / HZ);
2006 queue_delayed_work(rpciod_workqueue,
2007 &transport->connect_worker,
2008 xprt->reestablish_timeout);
2009 xprt->reestablish_timeout <<= 1;
2010 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
2011 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2012 if (xprt->reestablish_timeout > XS_TCP_MAX_REEST_TO)
2013 xprt->reestablish_timeout = XS_TCP_MAX_REEST_TO;
2014 } else {
2015 dprintk("RPC: xs_connect scheduled xprt %p\n", xprt);
2016 queue_delayed_work(rpciod_workqueue,
2017 &transport->connect_worker, 0);
2022 * xs_udp_print_stats - display UDP socket-specifc stats
2023 * @xprt: rpc_xprt struct containing statistics
2024 * @seq: output file
2027 static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2029 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2031 seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %Lu %Lu\n",
2032 transport->srcport,
2033 xprt->stat.bind_count,
2034 xprt->stat.sends,
2035 xprt->stat.recvs,
2036 xprt->stat.bad_xids,
2037 xprt->stat.req_u,
2038 xprt->stat.bklog_u);
2042 * xs_tcp_print_stats - display TCP socket-specifc stats
2043 * @xprt: rpc_xprt struct containing statistics
2044 * @seq: output file
2047 static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2049 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2050 long idle_time = 0;
2052 if (xprt_connected(xprt))
2053 idle_time = (long)(jiffies - xprt->last_used) / HZ;
2055 seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu %Lu %Lu\n",
2056 transport->srcport,
2057 xprt->stat.bind_count,
2058 xprt->stat.connect_count,
2059 xprt->stat.connect_time,
2060 idle_time,
2061 xprt->stat.sends,
2062 xprt->stat.recvs,
2063 xprt->stat.bad_xids,
2064 xprt->stat.req_u,
2065 xprt->stat.bklog_u);
2069 * Allocate a bunch of pages for a scratch buffer for the rpc code. The reason
2070 * we allocate pages instead doing a kmalloc like rpc_malloc is because we want
2071 * to use the server side send routines.
2073 static void *bc_malloc(struct rpc_task *task, size_t size)
2075 struct page *page;
2076 struct rpc_buffer *buf;
2078 BUG_ON(size > PAGE_SIZE - sizeof(struct rpc_buffer));
2079 page = alloc_page(GFP_KERNEL);
2081 if (!page)
2082 return NULL;
2084 buf = page_address(page);
2085 buf->len = PAGE_SIZE;
2087 return buf->data;
2091 * Free the space allocated in the bc_alloc routine
2093 static void bc_free(void *buffer)
2095 struct rpc_buffer *buf;
2097 if (!buffer)
2098 return;
2100 buf = container_of(buffer, struct rpc_buffer, data);
2101 free_page((unsigned long)buf);
2105 * Use the svc_sock to send the callback. Must be called with svsk->sk_mutex
2106 * held. Borrows heavily from svc_tcp_sendto and xs_tcp_send_request.
2108 static int bc_sendto(struct rpc_rqst *req)
2110 int len;
2111 struct xdr_buf *xbufp = &req->rq_snd_buf;
2112 struct rpc_xprt *xprt = req->rq_xprt;
2113 struct sock_xprt *transport =
2114 container_of(xprt, struct sock_xprt, xprt);
2115 struct socket *sock = transport->sock;
2116 unsigned long headoff;
2117 unsigned long tailoff;
2120 * Set up the rpc header and record marker stuff
2122 xs_encode_tcp_record_marker(xbufp);
2124 tailoff = (unsigned long)xbufp->tail[0].iov_base & ~PAGE_MASK;
2125 headoff = (unsigned long)xbufp->head[0].iov_base & ~PAGE_MASK;
2126 len = svc_send_common(sock, xbufp,
2127 virt_to_page(xbufp->head[0].iov_base), headoff,
2128 xbufp->tail[0].iov_base, tailoff);
2130 if (len != xbufp->len) {
2131 printk(KERN_NOTICE "Error sending entire callback!\n");
2132 len = -EAGAIN;
2135 return len;
2139 * The send routine. Borrows from svc_send
2141 static int bc_send_request(struct rpc_task *task)
2143 struct rpc_rqst *req = task->tk_rqstp;
2144 struct svc_xprt *xprt;
2145 struct svc_sock *svsk;
2146 u32 len;
2148 dprintk("sending request with xid: %08x\n", ntohl(req->rq_xid));
2150 * Get the server socket associated with this callback xprt
2152 xprt = req->rq_xprt->bc_xprt;
2153 svsk = container_of(xprt, struct svc_sock, sk_xprt);
2156 * Grab the mutex to serialize data as the connection is shared
2157 * with the fore channel
2159 if (!mutex_trylock(&xprt->xpt_mutex)) {
2160 rpc_sleep_on(&xprt->xpt_bc_pending, task, NULL);
2161 if (!mutex_trylock(&xprt->xpt_mutex))
2162 return -EAGAIN;
2163 rpc_wake_up_queued_task(&xprt->xpt_bc_pending, task);
2165 if (test_bit(XPT_DEAD, &xprt->xpt_flags))
2166 len = -ENOTCONN;
2167 else
2168 len = bc_sendto(req);
2169 mutex_unlock(&xprt->xpt_mutex);
2171 if (len > 0)
2172 len = 0;
2174 return len;
2178 * The close routine. Since this is client initiated, we do nothing
2181 static void bc_close(struct rpc_xprt *xprt)
2186 * The xprt destroy routine. Again, because this connection is client
2187 * initiated, we do nothing
2190 static void bc_destroy(struct rpc_xprt *xprt)
2194 static struct rpc_xprt_ops xs_udp_ops = {
2195 .set_buffer_size = xs_udp_set_buffer_size,
2196 .reserve_xprt = xprt_reserve_xprt_cong,
2197 .release_xprt = xprt_release_xprt_cong,
2198 .rpcbind = rpcb_getport_async,
2199 .set_port = xs_set_port,
2200 .connect = xs_connect,
2201 .buf_alloc = rpc_malloc,
2202 .buf_free = rpc_free,
2203 .send_request = xs_udp_send_request,
2204 .set_retrans_timeout = xprt_set_retrans_timeout_rtt,
2205 .timer = xs_udp_timer,
2206 .release_request = xprt_release_rqst_cong,
2207 .close = xs_close,
2208 .destroy = xs_destroy,
2209 .print_stats = xs_udp_print_stats,
2212 static struct rpc_xprt_ops xs_tcp_ops = {
2213 .reserve_xprt = xprt_reserve_xprt,
2214 .release_xprt = xs_tcp_release_xprt,
2215 .rpcbind = rpcb_getport_async,
2216 .set_port = xs_set_port,
2217 .connect = xs_connect,
2218 .buf_alloc = rpc_malloc,
2219 .buf_free = rpc_free,
2220 .send_request = xs_tcp_send_request,
2221 .set_retrans_timeout = xprt_set_retrans_timeout_def,
2222 .close = xs_tcp_close,
2223 .destroy = xs_destroy,
2224 .print_stats = xs_tcp_print_stats,
2228 * The rpc_xprt_ops for the server backchannel
2231 static struct rpc_xprt_ops bc_tcp_ops = {
2232 .reserve_xprt = xprt_reserve_xprt,
2233 .release_xprt = xprt_release_xprt,
2234 .buf_alloc = bc_malloc,
2235 .buf_free = bc_free,
2236 .send_request = bc_send_request,
2237 .set_retrans_timeout = xprt_set_retrans_timeout_def,
2238 .close = bc_close,
2239 .destroy = bc_destroy,
2240 .print_stats = xs_tcp_print_stats,
2243 static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args,
2244 unsigned int slot_table_size)
2246 struct rpc_xprt *xprt;
2247 struct sock_xprt *new;
2249 if (args->addrlen > sizeof(xprt->addr)) {
2250 dprintk("RPC: xs_setup_xprt: address too large\n");
2251 return ERR_PTR(-EBADF);
2254 new = kzalloc(sizeof(*new), GFP_KERNEL);
2255 if (new == NULL) {
2256 dprintk("RPC: xs_setup_xprt: couldn't allocate "
2257 "rpc_xprt\n");
2258 return ERR_PTR(-ENOMEM);
2260 xprt = &new->xprt;
2262 xprt->max_reqs = slot_table_size;
2263 xprt->slot = kcalloc(xprt->max_reqs, sizeof(struct rpc_rqst), GFP_KERNEL);
2264 if (xprt->slot == NULL) {
2265 kfree(xprt);
2266 dprintk("RPC: xs_setup_xprt: couldn't allocate slot "
2267 "table\n");
2268 return ERR_PTR(-ENOMEM);
2271 memcpy(&xprt->addr, args->dstaddr, args->addrlen);
2272 xprt->addrlen = args->addrlen;
2273 if (args->srcaddr)
2274 memcpy(&new->srcaddr, args->srcaddr, args->addrlen);
2276 return xprt;
2279 static const struct rpc_timeout xs_udp_default_timeout = {
2280 .to_initval = 5 * HZ,
2281 .to_maxval = 30 * HZ,
2282 .to_increment = 5 * HZ,
2283 .to_retries = 5,
2287 * xs_setup_udp - Set up transport to use a UDP socket
2288 * @args: rpc transport creation arguments
2291 static struct rpc_xprt *xs_setup_udp(struct xprt_create *args)
2293 struct sockaddr *addr = args->dstaddr;
2294 struct rpc_xprt *xprt;
2295 struct sock_xprt *transport;
2297 xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries);
2298 if (IS_ERR(xprt))
2299 return xprt;
2300 transport = container_of(xprt, struct sock_xprt, xprt);
2302 xprt->prot = IPPROTO_UDP;
2303 xprt->tsh_size = 0;
2304 /* XXX: header size can vary due to auth type, IPv6, etc. */
2305 xprt->max_payload = (1U << 16) - (MAX_HEADER << 3);
2307 xprt->bind_timeout = XS_BIND_TO;
2308 xprt->reestablish_timeout = XS_UDP_REEST_TO;
2309 xprt->idle_timeout = XS_IDLE_DISC_TO;
2311 xprt->ops = &xs_udp_ops;
2313 xprt->timeout = &xs_udp_default_timeout;
2315 switch (addr->sa_family) {
2316 case AF_INET:
2317 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
2318 xprt_set_bound(xprt);
2320 INIT_DELAYED_WORK(&transport->connect_worker,
2321 xs_udp_connect_worker4);
2322 xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP);
2323 break;
2324 case AF_INET6:
2325 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
2326 xprt_set_bound(xprt);
2328 INIT_DELAYED_WORK(&transport->connect_worker,
2329 xs_udp_connect_worker6);
2330 xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6);
2331 break;
2332 default:
2333 kfree(xprt);
2334 return ERR_PTR(-EAFNOSUPPORT);
2337 if (xprt_bound(xprt))
2338 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2339 xprt->address_strings[RPC_DISPLAY_ADDR],
2340 xprt->address_strings[RPC_DISPLAY_PORT],
2341 xprt->address_strings[RPC_DISPLAY_PROTO]);
2342 else
2343 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
2344 xprt->address_strings[RPC_DISPLAY_ADDR],
2345 xprt->address_strings[RPC_DISPLAY_PROTO]);
2347 if (try_module_get(THIS_MODULE))
2348 return xprt;
2350 kfree(xprt->slot);
2351 kfree(xprt);
2352 return ERR_PTR(-EINVAL);
2355 static const struct rpc_timeout xs_tcp_default_timeout = {
2356 .to_initval = 60 * HZ,
2357 .to_maxval = 60 * HZ,
2358 .to_retries = 2,
2362 * xs_setup_tcp - Set up transport to use a TCP socket
2363 * @args: rpc transport creation arguments
2366 static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args)
2368 struct sockaddr *addr = args->dstaddr;
2369 struct rpc_xprt *xprt;
2370 struct sock_xprt *transport;
2372 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries);
2373 if (IS_ERR(xprt))
2374 return xprt;
2375 transport = container_of(xprt, struct sock_xprt, xprt);
2377 xprt->prot = IPPROTO_TCP;
2378 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
2379 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2381 xprt->bind_timeout = XS_BIND_TO;
2382 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2383 xprt->idle_timeout = XS_IDLE_DISC_TO;
2385 xprt->ops = &xs_tcp_ops;
2386 xprt->timeout = &xs_tcp_default_timeout;
2388 switch (addr->sa_family) {
2389 case AF_INET:
2390 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
2391 xprt_set_bound(xprt);
2393 INIT_DELAYED_WORK(&transport->connect_worker,
2394 xs_tcp_connect_worker4);
2395 xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP);
2396 break;
2397 case AF_INET6:
2398 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
2399 xprt_set_bound(xprt);
2401 INIT_DELAYED_WORK(&transport->connect_worker,
2402 xs_tcp_connect_worker6);
2403 xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6);
2404 break;
2405 default:
2406 kfree(xprt);
2407 return ERR_PTR(-EAFNOSUPPORT);
2410 if (xprt_bound(xprt))
2411 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2412 xprt->address_strings[RPC_DISPLAY_ADDR],
2413 xprt->address_strings[RPC_DISPLAY_PORT],
2414 xprt->address_strings[RPC_DISPLAY_PROTO]);
2415 else
2416 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
2417 xprt->address_strings[RPC_DISPLAY_ADDR],
2418 xprt->address_strings[RPC_DISPLAY_PROTO]);
2421 if (try_module_get(THIS_MODULE))
2422 return xprt;
2424 kfree(xprt->slot);
2425 kfree(xprt);
2426 return ERR_PTR(-EINVAL);
2430 * xs_setup_bc_tcp - Set up transport to use a TCP backchannel socket
2431 * @args: rpc transport creation arguments
2434 static struct rpc_xprt *xs_setup_bc_tcp(struct xprt_create *args)
2436 struct sockaddr *addr = args->dstaddr;
2437 struct rpc_xprt *xprt;
2438 struct sock_xprt *transport;
2439 struct svc_sock *bc_sock;
2441 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries);
2442 if (IS_ERR(xprt))
2443 return xprt;
2444 transport = container_of(xprt, struct sock_xprt, xprt);
2446 xprt->prot = IPPROTO_TCP;
2447 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
2448 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2449 xprt->timeout = &xs_tcp_default_timeout;
2451 /* backchannel */
2452 xprt_set_bound(xprt);
2453 xprt->bind_timeout = 0;
2454 xprt->reestablish_timeout = 0;
2455 xprt->idle_timeout = 0;
2458 * The backchannel uses the same socket connection as the
2459 * forechannel
2461 xprt->bc_xprt = args->bc_xprt;
2462 bc_sock = container_of(args->bc_xprt, struct svc_sock, sk_xprt);
2463 bc_sock->sk_bc_xprt = xprt;
2464 transport->sock = bc_sock->sk_sock;
2465 transport->inet = bc_sock->sk_sk;
2467 xprt->ops = &bc_tcp_ops;
2469 switch (addr->sa_family) {
2470 case AF_INET:
2471 xs_format_peer_addresses(xprt, "tcp",
2472 RPCBIND_NETID_TCP);
2473 break;
2474 case AF_INET6:
2475 xs_format_peer_addresses(xprt, "tcp",
2476 RPCBIND_NETID_TCP6);
2477 break;
2478 default:
2479 kfree(xprt);
2480 return ERR_PTR(-EAFNOSUPPORT);
2483 if (xprt_bound(xprt))
2484 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2485 xprt->address_strings[RPC_DISPLAY_ADDR],
2486 xprt->address_strings[RPC_DISPLAY_PORT],
2487 xprt->address_strings[RPC_DISPLAY_PROTO]);
2488 else
2489 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
2490 xprt->address_strings[RPC_DISPLAY_ADDR],
2491 xprt->address_strings[RPC_DISPLAY_PROTO]);
2494 * Since we don't want connections for the backchannel, we set
2495 * the xprt status to connected
2497 xprt_set_connected(xprt);
2500 if (try_module_get(THIS_MODULE))
2501 return xprt;
2502 kfree(xprt->slot);
2503 kfree(xprt);
2504 return ERR_PTR(-EINVAL);
2507 static struct xprt_class xs_udp_transport = {
2508 .list = LIST_HEAD_INIT(xs_udp_transport.list),
2509 .name = "udp",
2510 .owner = THIS_MODULE,
2511 .ident = XPRT_TRANSPORT_UDP,
2512 .setup = xs_setup_udp,
2515 static struct xprt_class xs_tcp_transport = {
2516 .list = LIST_HEAD_INIT(xs_tcp_transport.list),
2517 .name = "tcp",
2518 .owner = THIS_MODULE,
2519 .ident = XPRT_TRANSPORT_TCP,
2520 .setup = xs_setup_tcp,
2523 static struct xprt_class xs_bc_tcp_transport = {
2524 .list = LIST_HEAD_INIT(xs_bc_tcp_transport.list),
2525 .name = "tcp NFSv4.1 backchannel",
2526 .owner = THIS_MODULE,
2527 .ident = XPRT_TRANSPORT_BC_TCP,
2528 .setup = xs_setup_bc_tcp,
2532 * init_socket_xprt - set up xprtsock's sysctls, register with RPC client
2535 int init_socket_xprt(void)
2537 #ifdef RPC_DEBUG
2538 if (!sunrpc_table_header)
2539 sunrpc_table_header = register_sysctl_table(sunrpc_table);
2540 #endif
2542 xprt_register_transport(&xs_udp_transport);
2543 xprt_register_transport(&xs_tcp_transport);
2544 xprt_register_transport(&xs_bc_tcp_transport);
2546 return 0;
2550 * cleanup_socket_xprt - remove xprtsock's sysctls, unregister
2553 void cleanup_socket_xprt(void)
2555 #ifdef RPC_DEBUG
2556 if (sunrpc_table_header) {
2557 unregister_sysctl_table(sunrpc_table_header);
2558 sunrpc_table_header = NULL;
2560 #endif
2562 xprt_unregister_transport(&xs_udp_transport);
2563 xprt_unregister_transport(&xs_tcp_transport);
2564 xprt_unregister_transport(&xs_bc_tcp_transport);
2567 static int param_set_uint_minmax(const char *val, struct kernel_param *kp,
2568 unsigned int min, unsigned int max)
2570 unsigned long num;
2571 int ret;
2573 if (!val)
2574 return -EINVAL;
2575 ret = strict_strtoul(val, 0, &num);
2576 if (ret == -EINVAL || num < min || num > max)
2577 return -EINVAL;
2578 *((unsigned int *)kp->arg) = num;
2579 return 0;
2582 static int param_set_portnr(const char *val, struct kernel_param *kp)
2584 return param_set_uint_minmax(val, kp,
2585 RPC_MIN_RESVPORT,
2586 RPC_MAX_RESVPORT);
2589 static int param_get_portnr(char *buffer, struct kernel_param *kp)
2591 return param_get_uint(buffer, kp);
2593 #define param_check_portnr(name, p) \
2594 __param_check(name, p, unsigned int);
2596 module_param_named(min_resvport, xprt_min_resvport, portnr, 0644);
2597 module_param_named(max_resvport, xprt_max_resvport, portnr, 0644);
2599 static int param_set_slot_table_size(const char *val, struct kernel_param *kp)
2601 return param_set_uint_minmax(val, kp,
2602 RPC_MIN_SLOT_TABLE,
2603 RPC_MAX_SLOT_TABLE);
2606 static int param_get_slot_table_size(char *buffer, struct kernel_param *kp)
2608 return param_get_uint(buffer, kp);
2610 #define param_check_slot_table_size(name, p) \
2611 __param_check(name, p, unsigned int);
2613 module_param_named(tcp_slot_table_entries, xprt_tcp_slot_table_entries,
2614 slot_table_size, 0644);
2615 module_param_named(udp_slot_table_entries, xprt_udp_slot_table_entries,
2616 slot_table_size, 0644);