proc: protect mm start_code/end_code in /proc/pid/stat
[linux/fpc-iii.git] / net / sunrpc / xprtsock.c
blobb6fcf68dc35cdd3a2fd08942151c5771338f05cb
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 .ctl_name = CTL_SLOTTABLE_UDP,
85 .procname = "udp_slot_table_entries",
86 .data = &xprt_udp_slot_table_entries,
87 .maxlen = sizeof(unsigned int),
88 .mode = 0644,
89 .proc_handler = &proc_dointvec_minmax,
90 .strategy = &sysctl_intvec,
91 .extra1 = &min_slot_table_size,
92 .extra2 = &max_slot_table_size
95 .ctl_name = CTL_SLOTTABLE_TCP,
96 .procname = "tcp_slot_table_entries",
97 .data = &xprt_tcp_slot_table_entries,
98 .maxlen = sizeof(unsigned int),
99 .mode = 0644,
100 .proc_handler = &proc_dointvec_minmax,
101 .strategy = &sysctl_intvec,
102 .extra1 = &min_slot_table_size,
103 .extra2 = &max_slot_table_size
106 .ctl_name = CTL_MIN_RESVPORT,
107 .procname = "min_resvport",
108 .data = &xprt_min_resvport,
109 .maxlen = sizeof(unsigned int),
110 .mode = 0644,
111 .proc_handler = &proc_dointvec_minmax,
112 .strategy = &sysctl_intvec,
113 .extra1 = &xprt_min_resvport_limit,
114 .extra2 = &xprt_max_resvport_limit
117 .ctl_name = CTL_MAX_RESVPORT,
118 .procname = "max_resvport",
119 .data = &xprt_max_resvport,
120 .maxlen = sizeof(unsigned int),
121 .mode = 0644,
122 .proc_handler = &proc_dointvec_minmax,
123 .strategy = &sysctl_intvec,
124 .extra1 = &xprt_min_resvport_limit,
125 .extra2 = &xprt_max_resvport_limit
128 .procname = "tcp_fin_timeout",
129 .data = &xs_tcp_fin_timeout,
130 .maxlen = sizeof(xs_tcp_fin_timeout),
131 .mode = 0644,
132 .proc_handler = &proc_dointvec_jiffies,
133 .strategy = sysctl_jiffies
136 .ctl_name = 0,
140 static ctl_table sunrpc_table[] = {
142 .ctl_name = CTL_SUNRPC,
143 .procname = "sunrpc",
144 .mode = 0555,
145 .child = xs_tunables_table
148 .ctl_name = 0,
152 #endif
155 * Time out for an RPC UDP socket connect. UDP socket connects are
156 * synchronous, but we set a timeout anyway in case of resource
157 * exhaustion on the local host.
159 #define XS_UDP_CONN_TO (5U * HZ)
162 * Wait duration for an RPC TCP connection to be established. Solaris
163 * NFS over TCP uses 60 seconds, for example, which is in line with how
164 * long a server takes to reboot.
166 #define XS_TCP_CONN_TO (60U * HZ)
169 * Wait duration for a reply from the RPC portmapper.
171 #define XS_BIND_TO (60U * HZ)
174 * Delay if a UDP socket connect error occurs. This is most likely some
175 * kind of resource problem on the local host.
177 #define XS_UDP_REEST_TO (2U * HZ)
180 * The reestablish timeout allows clients to delay for a bit before attempting
181 * to reconnect to a server that just dropped our connection.
183 * We implement an exponential backoff when trying to reestablish a TCP
184 * transport connection with the server. Some servers like to drop a TCP
185 * connection when they are overworked, so we start with a short timeout and
186 * increase over time if the server is down or not responding.
188 #define XS_TCP_INIT_REEST_TO (3U * HZ)
189 #define XS_TCP_MAX_REEST_TO (5U * 60 * HZ)
192 * TCP idle timeout; client drops the transport socket if it is idle
193 * for this long. Note that we also timeout UDP sockets to prevent
194 * holding port numbers when there is no RPC traffic.
196 #define XS_IDLE_DISC_TO (5U * 60 * HZ)
198 #ifdef RPC_DEBUG
199 # undef RPC_DEBUG_DATA
200 # define RPCDBG_FACILITY RPCDBG_TRANS
201 #endif
203 #ifdef RPC_DEBUG_DATA
204 static void xs_pktdump(char *msg, u32 *packet, unsigned int count)
206 u8 *buf = (u8 *) packet;
207 int j;
209 dprintk("RPC: %s\n", msg);
210 for (j = 0; j < count && j < 128; j += 4) {
211 if (!(j & 31)) {
212 if (j)
213 dprintk("\n");
214 dprintk("0x%04x ", j);
216 dprintk("%02x%02x%02x%02x ",
217 buf[j], buf[j+1], buf[j+2], buf[j+3]);
219 dprintk("\n");
221 #else
222 static inline void xs_pktdump(char *msg, u32 *packet, unsigned int count)
224 /* NOP */
226 #endif
228 struct sock_xprt {
229 struct rpc_xprt xprt;
232 * Network layer
234 struct socket * sock;
235 struct sock * inet;
238 * State of TCP reply receive
240 __be32 tcp_fraghdr,
241 tcp_xid,
242 tcp_calldir;
244 u32 tcp_offset,
245 tcp_reclen;
247 unsigned long tcp_copied,
248 tcp_flags;
251 * Connection of transports
253 struct delayed_work connect_worker;
254 struct sockaddr_storage srcaddr;
255 unsigned short srcport;
258 * UDP socket buffer size parameters
260 size_t rcvsize,
261 sndsize;
264 * Saved socket callback addresses
266 void (*old_data_ready)(struct sock *, int);
267 void (*old_state_change)(struct sock *);
268 void (*old_write_space)(struct sock *);
269 void (*old_error_report)(struct sock *);
273 * TCP receive state flags
275 #define TCP_RCV_LAST_FRAG (1UL << 0)
276 #define TCP_RCV_COPY_FRAGHDR (1UL << 1)
277 #define TCP_RCV_COPY_XID (1UL << 2)
278 #define TCP_RCV_COPY_DATA (1UL << 3)
279 #define TCP_RCV_READ_CALLDIR (1UL << 4)
280 #define TCP_RCV_COPY_CALLDIR (1UL << 5)
283 * TCP RPC flags
285 #define TCP_RPC_REPLY (1UL << 6)
287 static inline struct sockaddr *xs_addr(struct rpc_xprt *xprt)
289 return (struct sockaddr *) &xprt->addr;
292 static inline struct sockaddr_in *xs_addr_in(struct rpc_xprt *xprt)
294 return (struct sockaddr_in *) &xprt->addr;
297 static inline struct sockaddr_in6 *xs_addr_in6(struct rpc_xprt *xprt)
299 return (struct sockaddr_in6 *) &xprt->addr;
302 static void xs_format_common_peer_addresses(struct rpc_xprt *xprt)
304 struct sockaddr *sap = xs_addr(xprt);
305 struct sockaddr_in6 *sin6;
306 struct sockaddr_in *sin;
307 char buf[128];
309 (void)rpc_ntop(sap, buf, sizeof(buf));
310 xprt->address_strings[RPC_DISPLAY_ADDR] = kstrdup(buf, GFP_KERNEL);
312 switch (sap->sa_family) {
313 case AF_INET:
314 sin = xs_addr_in(xprt);
315 (void)snprintf(buf, sizeof(buf), "%02x%02x%02x%02x",
316 NIPQUAD(sin->sin_addr.s_addr));
317 break;
318 case AF_INET6:
319 sin6 = xs_addr_in6(xprt);
320 (void)snprintf(buf, sizeof(buf), "%pi6", &sin6->sin6_addr);
321 break;
322 default:
323 BUG();
325 xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = kstrdup(buf, GFP_KERNEL);
328 static void xs_format_common_peer_ports(struct rpc_xprt *xprt)
330 struct sockaddr *sap = xs_addr(xprt);
331 char buf[128];
333 (void)snprintf(buf, sizeof(buf), "%u", rpc_get_port(sap));
334 xprt->address_strings[RPC_DISPLAY_PORT] = kstrdup(buf, GFP_KERNEL);
336 (void)snprintf(buf, sizeof(buf), "%4hx", rpc_get_port(sap));
337 xprt->address_strings[RPC_DISPLAY_HEX_PORT] = kstrdup(buf, GFP_KERNEL);
340 static void xs_format_peer_addresses(struct rpc_xprt *xprt,
341 const char *protocol,
342 const char *netid)
344 xprt->address_strings[RPC_DISPLAY_PROTO] = protocol;
345 xprt->address_strings[RPC_DISPLAY_NETID] = netid;
346 xs_format_common_peer_addresses(xprt);
347 xs_format_common_peer_ports(xprt);
350 static void xs_update_peer_port(struct rpc_xprt *xprt)
352 kfree(xprt->address_strings[RPC_DISPLAY_HEX_PORT]);
353 kfree(xprt->address_strings[RPC_DISPLAY_PORT]);
355 xs_format_common_peer_ports(xprt);
358 static void xs_free_peer_addresses(struct rpc_xprt *xprt)
360 unsigned int i;
362 for (i = 0; i < RPC_DISPLAY_MAX; i++)
363 switch (i) {
364 case RPC_DISPLAY_PROTO:
365 case RPC_DISPLAY_NETID:
366 continue;
367 default:
368 kfree(xprt->address_strings[i]);
372 #define XS_SENDMSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL)
374 static int xs_send_kvec(struct socket *sock, struct sockaddr *addr, int addrlen, struct kvec *vec, unsigned int base, int more)
376 struct msghdr msg = {
377 .msg_name = addr,
378 .msg_namelen = addrlen,
379 .msg_flags = XS_SENDMSG_FLAGS | (more ? MSG_MORE : 0),
381 struct kvec iov = {
382 .iov_base = vec->iov_base + base,
383 .iov_len = vec->iov_len - base,
386 if (iov.iov_len != 0)
387 return kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len);
388 return kernel_sendmsg(sock, &msg, NULL, 0, 0);
391 static int xs_send_pagedata(struct socket *sock, struct xdr_buf *xdr, unsigned int base, int more)
393 struct page **ppage;
394 unsigned int remainder;
395 int err, sent = 0;
397 remainder = xdr->page_len - base;
398 base += xdr->page_base;
399 ppage = xdr->pages + (base >> PAGE_SHIFT);
400 base &= ~PAGE_MASK;
401 for(;;) {
402 unsigned int len = min_t(unsigned int, PAGE_SIZE - base, remainder);
403 int flags = XS_SENDMSG_FLAGS;
405 remainder -= len;
406 if (remainder != 0 || more)
407 flags |= MSG_MORE;
408 err = sock->ops->sendpage(sock, *ppage, base, len, flags);
409 if (remainder == 0 || err != len)
410 break;
411 sent += err;
412 ppage++;
413 base = 0;
415 if (sent == 0)
416 return err;
417 if (err > 0)
418 sent += err;
419 return sent;
423 * xs_sendpages - write pages directly to a socket
424 * @sock: socket to send on
425 * @addr: UDP only -- address of destination
426 * @addrlen: UDP only -- length of destination address
427 * @xdr: buffer containing this request
428 * @base: starting position in the buffer
431 static int xs_sendpages(struct socket *sock, struct sockaddr *addr, int addrlen, struct xdr_buf *xdr, unsigned int base)
433 unsigned int remainder = xdr->len - base;
434 int err, sent = 0;
436 if (unlikely(!sock))
437 return -ENOTSOCK;
439 clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags);
440 if (base != 0) {
441 addr = NULL;
442 addrlen = 0;
445 if (base < xdr->head[0].iov_len || addr != NULL) {
446 unsigned int len = xdr->head[0].iov_len - base;
447 remainder -= len;
448 err = xs_send_kvec(sock, addr, addrlen, &xdr->head[0], base, remainder != 0);
449 if (remainder == 0 || err != len)
450 goto out;
451 sent += err;
452 base = 0;
453 } else
454 base -= xdr->head[0].iov_len;
456 if (base < xdr->page_len) {
457 unsigned int len = xdr->page_len - base;
458 remainder -= len;
459 err = xs_send_pagedata(sock, xdr, base, remainder != 0);
460 if (remainder == 0 || err != len)
461 goto out;
462 sent += err;
463 base = 0;
464 } else
465 base -= xdr->page_len;
467 if (base >= xdr->tail[0].iov_len)
468 return sent;
469 err = xs_send_kvec(sock, NULL, 0, &xdr->tail[0], base, 0);
470 out:
471 if (sent == 0)
472 return err;
473 if (err > 0)
474 sent += err;
475 return sent;
478 static void xs_nospace_callback(struct rpc_task *task)
480 struct sock_xprt *transport = container_of(task->tk_rqstp->rq_xprt, struct sock_xprt, xprt);
482 transport->inet->sk_write_pending--;
483 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
487 * xs_nospace - place task on wait queue if transmit was incomplete
488 * @task: task to put to sleep
491 static int xs_nospace(struct rpc_task *task)
493 struct rpc_rqst *req = task->tk_rqstp;
494 struct rpc_xprt *xprt = req->rq_xprt;
495 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
496 int ret = 0;
498 dprintk("RPC: %5u xmit incomplete (%u left of %u)\n",
499 task->tk_pid, req->rq_slen - req->rq_bytes_sent,
500 req->rq_slen);
502 /* Protect against races with write_space */
503 spin_lock_bh(&xprt->transport_lock);
505 /* Don't race with disconnect */
506 if (xprt_connected(xprt)) {
507 if (test_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags)) {
508 ret = -EAGAIN;
510 * Notify TCP that we're limited by the application
511 * window size
513 set_bit(SOCK_NOSPACE, &transport->sock->flags);
514 transport->inet->sk_write_pending++;
515 /* ...and wait for more buffer space */
516 xprt_wait_for_buffer_space(task, xs_nospace_callback);
518 } else {
519 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
520 ret = -ENOTCONN;
523 spin_unlock_bh(&xprt->transport_lock);
524 return ret;
528 * xs_udp_send_request - write an RPC request to a UDP socket
529 * @task: address of RPC task that manages the state of an RPC request
531 * Return values:
532 * 0: The request has been sent
533 * EAGAIN: The socket was blocked, please call again later to
534 * complete the request
535 * ENOTCONN: Caller needs to invoke connect logic then call again
536 * other: Some other error occured, the request was not sent
538 static int xs_udp_send_request(struct rpc_task *task)
540 struct rpc_rqst *req = task->tk_rqstp;
541 struct rpc_xprt *xprt = req->rq_xprt;
542 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
543 struct xdr_buf *xdr = &req->rq_snd_buf;
544 int status;
546 xs_pktdump("packet data:",
547 req->rq_svec->iov_base,
548 req->rq_svec->iov_len);
550 if (!xprt_bound(xprt))
551 return -ENOTCONN;
552 status = xs_sendpages(transport->sock,
553 xs_addr(xprt),
554 xprt->addrlen, xdr,
555 req->rq_bytes_sent);
557 dprintk("RPC: xs_udp_send_request(%u) = %d\n",
558 xdr->len - req->rq_bytes_sent, status);
560 if (status >= 0) {
561 task->tk_bytes_sent += status;
562 if (status >= req->rq_slen)
563 return 0;
564 /* Still some bytes left; set up for a retry later. */
565 status = -EAGAIN;
567 if (!transport->sock)
568 goto out;
570 switch (status) {
571 case -ENOTSOCK:
572 status = -ENOTCONN;
573 /* Should we call xs_close() here? */
574 break;
575 case -EAGAIN:
576 status = xs_nospace(task);
577 break;
578 default:
579 dprintk("RPC: sendmsg returned unrecognized error %d\n",
580 -status);
581 case -ENETUNREACH:
582 case -EPIPE:
583 case -ECONNREFUSED:
584 /* When the server has died, an ICMP port unreachable message
585 * prompts ECONNREFUSED. */
586 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
588 out:
589 return status;
593 * xs_tcp_shutdown - gracefully shut down a TCP socket
594 * @xprt: transport
596 * Initiates a graceful shutdown of the TCP socket by calling the
597 * equivalent of shutdown(SHUT_WR);
599 static void xs_tcp_shutdown(struct rpc_xprt *xprt)
601 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
602 struct socket *sock = transport->sock;
604 if (sock != NULL)
605 kernel_sock_shutdown(sock, SHUT_WR);
608 static inline void xs_encode_tcp_record_marker(struct xdr_buf *buf)
610 u32 reclen = buf->len - sizeof(rpc_fraghdr);
611 rpc_fraghdr *base = buf->head[0].iov_base;
612 *base = htonl(RPC_LAST_STREAM_FRAGMENT | reclen);
616 * xs_tcp_send_request - write an RPC request to a TCP socket
617 * @task: address of RPC task that manages the state of an RPC request
619 * Return values:
620 * 0: The request has been sent
621 * EAGAIN: The socket was blocked, please call again later to
622 * complete the request
623 * ENOTCONN: Caller needs to invoke connect logic then call again
624 * other: Some other error occured, the request was not sent
626 * XXX: In the case of soft timeouts, should we eventually give up
627 * if sendmsg is not able to make progress?
629 static int xs_tcp_send_request(struct rpc_task *task)
631 struct rpc_rqst *req = task->tk_rqstp;
632 struct rpc_xprt *xprt = req->rq_xprt;
633 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
634 struct xdr_buf *xdr = &req->rq_snd_buf;
635 int status;
637 xs_encode_tcp_record_marker(&req->rq_snd_buf);
639 xs_pktdump("packet data:",
640 req->rq_svec->iov_base,
641 req->rq_svec->iov_len);
643 /* Continue transmitting the packet/record. We must be careful
644 * to cope with writespace callbacks arriving _after_ we have
645 * called sendmsg(). */
646 while (1) {
647 status = xs_sendpages(transport->sock,
648 NULL, 0, xdr, req->rq_bytes_sent);
650 dprintk("RPC: xs_tcp_send_request(%u) = %d\n",
651 xdr->len - req->rq_bytes_sent, status);
653 if (unlikely(status < 0))
654 break;
656 /* If we've sent the entire packet, immediately
657 * reset the count of bytes sent. */
658 req->rq_bytes_sent += status;
659 task->tk_bytes_sent += status;
660 if (likely(req->rq_bytes_sent >= req->rq_slen)) {
661 req->rq_bytes_sent = 0;
662 return 0;
665 if (status != 0)
666 continue;
667 status = -EAGAIN;
668 break;
670 if (!transport->sock)
671 goto out;
673 switch (status) {
674 case -ENOTSOCK:
675 status = -ENOTCONN;
676 /* Should we call xs_close() here? */
677 break;
678 case -EAGAIN:
679 status = xs_nospace(task);
680 break;
681 default:
682 dprintk("RPC: sendmsg returned unrecognized error %d\n",
683 -status);
684 case -ECONNRESET:
685 case -EPIPE:
686 xs_tcp_shutdown(xprt);
687 case -ECONNREFUSED:
688 case -ENOTCONN:
689 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
691 out:
692 return status;
696 * xs_tcp_release_xprt - clean up after a tcp transmission
697 * @xprt: transport
698 * @task: rpc task
700 * This cleans up if an error causes us to abort the transmission of a request.
701 * In this case, the socket may need to be reset in order to avoid confusing
702 * the server.
704 static void xs_tcp_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
706 struct rpc_rqst *req;
708 if (task != xprt->snd_task)
709 return;
710 if (task == NULL)
711 goto out_release;
712 req = task->tk_rqstp;
713 if (req->rq_bytes_sent == 0)
714 goto out_release;
715 if (req->rq_bytes_sent == req->rq_snd_buf.len)
716 goto out_release;
717 set_bit(XPRT_CLOSE_WAIT, &task->tk_xprt->state);
718 out_release:
719 xprt_release_xprt(xprt, task);
722 static void xs_save_old_callbacks(struct sock_xprt *transport, struct sock *sk)
724 transport->old_data_ready = sk->sk_data_ready;
725 transport->old_state_change = sk->sk_state_change;
726 transport->old_write_space = sk->sk_write_space;
727 transport->old_error_report = sk->sk_error_report;
730 static void xs_restore_old_callbacks(struct sock_xprt *transport, struct sock *sk)
732 sk->sk_data_ready = transport->old_data_ready;
733 sk->sk_state_change = transport->old_state_change;
734 sk->sk_write_space = transport->old_write_space;
735 sk->sk_error_report = transport->old_error_report;
738 static void xs_reset_transport(struct sock_xprt *transport)
740 struct socket *sock = transport->sock;
741 struct sock *sk = transport->inet;
743 if (sk == NULL)
744 return;
746 write_lock_bh(&sk->sk_callback_lock);
747 transport->inet = NULL;
748 transport->sock = NULL;
750 sk->sk_user_data = NULL;
752 xs_restore_old_callbacks(transport, sk);
753 write_unlock_bh(&sk->sk_callback_lock);
755 sk->sk_no_check = 0;
757 sock_release(sock);
761 * xs_close - close a socket
762 * @xprt: transport
764 * This is used when all requests are complete; ie, no DRC state remains
765 * on the server we want to save.
767 * The caller _must_ be holding XPRT_LOCKED in order to avoid issues with
768 * xs_reset_transport() zeroing the socket from underneath a writer.
770 static void xs_close(struct rpc_xprt *xprt)
772 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
774 dprintk("RPC: xs_close xprt %p\n", xprt);
776 xs_reset_transport(transport);
777 xprt->reestablish_timeout = 0;
779 smp_mb__before_clear_bit();
780 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
781 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
782 clear_bit(XPRT_CLOSING, &xprt->state);
783 smp_mb__after_clear_bit();
784 xprt_disconnect_done(xprt);
787 static void xs_tcp_close(struct rpc_xprt *xprt)
789 if (test_and_clear_bit(XPRT_CONNECTION_CLOSE, &xprt->state))
790 xs_close(xprt);
791 else
792 xs_tcp_shutdown(xprt);
796 * xs_destroy - prepare to shutdown a transport
797 * @xprt: doomed transport
800 static void xs_destroy(struct rpc_xprt *xprt)
802 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
804 dprintk("RPC: xs_destroy xprt %p\n", xprt);
806 cancel_rearming_delayed_work(&transport->connect_worker);
808 xs_close(xprt);
809 xs_free_peer_addresses(xprt);
810 kfree(xprt->slot);
811 kfree(xprt);
812 module_put(THIS_MODULE);
815 static inline struct rpc_xprt *xprt_from_sock(struct sock *sk)
817 return (struct rpc_xprt *) sk->sk_user_data;
821 * xs_udp_data_ready - "data ready" callback for UDP sockets
822 * @sk: socket with data to read
823 * @len: how much data to read
826 static void xs_udp_data_ready(struct sock *sk, int len)
828 struct rpc_task *task;
829 struct rpc_xprt *xprt;
830 struct rpc_rqst *rovr;
831 struct sk_buff *skb;
832 int err, repsize, copied;
833 u32 _xid;
834 __be32 *xp;
836 read_lock(&sk->sk_callback_lock);
837 dprintk("RPC: xs_udp_data_ready...\n");
838 if (!(xprt = xprt_from_sock(sk)))
839 goto out;
841 if ((skb = skb_recv_datagram(sk, 0, 1, &err)) == NULL)
842 goto out;
844 if (xprt->shutdown)
845 goto dropit;
847 repsize = skb->len - sizeof(struct udphdr);
848 if (repsize < 4) {
849 dprintk("RPC: impossible RPC reply size %d!\n", repsize);
850 goto dropit;
853 /* Copy the XID from the skb... */
854 xp = skb_header_pointer(skb, sizeof(struct udphdr),
855 sizeof(_xid), &_xid);
856 if (xp == NULL)
857 goto dropit;
859 /* Look up and lock the request corresponding to the given XID */
860 spin_lock(&xprt->transport_lock);
861 rovr = xprt_lookup_rqst(xprt, *xp);
862 if (!rovr)
863 goto out_unlock;
864 task = rovr->rq_task;
866 if ((copied = rovr->rq_private_buf.buflen) > repsize)
867 copied = repsize;
869 /* Suck it into the iovec, verify checksum if not done by hw. */
870 if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) {
871 UDPX_INC_STATS_BH(sk, UDP_MIB_INERRORS);
872 goto out_unlock;
875 UDPX_INC_STATS_BH(sk, UDP_MIB_INDATAGRAMS);
877 /* Something worked... */
878 dst_confirm(skb_dst(skb));
880 xprt_adjust_cwnd(task, copied);
881 xprt_update_rtt(task);
882 xprt_complete_rqst(task, copied);
884 out_unlock:
885 spin_unlock(&xprt->transport_lock);
886 dropit:
887 skb_free_datagram(sk, skb);
888 out:
889 read_unlock(&sk->sk_callback_lock);
892 static inline void xs_tcp_read_fraghdr(struct rpc_xprt *xprt, struct xdr_skb_reader *desc)
894 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
895 size_t len, used;
896 char *p;
898 p = ((char *) &transport->tcp_fraghdr) + transport->tcp_offset;
899 len = sizeof(transport->tcp_fraghdr) - transport->tcp_offset;
900 used = xdr_skb_read_bits(desc, p, len);
901 transport->tcp_offset += used;
902 if (used != len)
903 return;
905 transport->tcp_reclen = ntohl(transport->tcp_fraghdr);
906 if (transport->tcp_reclen & RPC_LAST_STREAM_FRAGMENT)
907 transport->tcp_flags |= TCP_RCV_LAST_FRAG;
908 else
909 transport->tcp_flags &= ~TCP_RCV_LAST_FRAG;
910 transport->tcp_reclen &= RPC_FRAGMENT_SIZE_MASK;
912 transport->tcp_flags &= ~TCP_RCV_COPY_FRAGHDR;
913 transport->tcp_offset = 0;
915 /* Sanity check of the record length */
916 if (unlikely(transport->tcp_reclen < 8)) {
917 dprintk("RPC: invalid TCP record fragment length\n");
918 xprt_force_disconnect(xprt);
919 return;
921 dprintk("RPC: reading TCP record fragment of length %d\n",
922 transport->tcp_reclen);
925 static void xs_tcp_check_fraghdr(struct sock_xprt *transport)
927 if (transport->tcp_offset == transport->tcp_reclen) {
928 transport->tcp_flags |= TCP_RCV_COPY_FRAGHDR;
929 transport->tcp_offset = 0;
930 if (transport->tcp_flags & TCP_RCV_LAST_FRAG) {
931 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
932 transport->tcp_flags |= TCP_RCV_COPY_XID;
933 transport->tcp_copied = 0;
938 static inline void xs_tcp_read_xid(struct sock_xprt *transport, struct xdr_skb_reader *desc)
940 size_t len, used;
941 char *p;
943 len = sizeof(transport->tcp_xid) - transport->tcp_offset;
944 dprintk("RPC: reading XID (%Zu bytes)\n", len);
945 p = ((char *) &transport->tcp_xid) + transport->tcp_offset;
946 used = xdr_skb_read_bits(desc, p, len);
947 transport->tcp_offset += used;
948 if (used != len)
949 return;
950 transport->tcp_flags &= ~TCP_RCV_COPY_XID;
951 transport->tcp_flags |= TCP_RCV_READ_CALLDIR;
952 transport->tcp_copied = 4;
953 dprintk("RPC: reading %s XID %08x\n",
954 (transport->tcp_flags & TCP_RPC_REPLY) ? "reply for"
955 : "request with",
956 ntohl(transport->tcp_xid));
957 xs_tcp_check_fraghdr(transport);
960 static inline void xs_tcp_read_calldir(struct sock_xprt *transport,
961 struct xdr_skb_reader *desc)
963 size_t len, used;
964 u32 offset;
965 char *p;
968 * We want transport->tcp_offset to be 8 at the end of this routine
969 * (4 bytes for the xid and 4 bytes for the call/reply flag).
970 * When this function is called for the first time,
971 * transport->tcp_offset is 4 (after having already read the xid).
973 offset = transport->tcp_offset - sizeof(transport->tcp_xid);
974 len = sizeof(transport->tcp_calldir) - offset;
975 dprintk("RPC: reading CALL/REPLY flag (%Zu bytes)\n", len);
976 p = ((char *) &transport->tcp_calldir) + offset;
977 used = xdr_skb_read_bits(desc, p, len);
978 transport->tcp_offset += used;
979 if (used != len)
980 return;
981 transport->tcp_flags &= ~TCP_RCV_READ_CALLDIR;
983 * We don't yet have the XDR buffer, so we will write the calldir
984 * out after we get the buffer from the 'struct rpc_rqst'
986 switch (ntohl(transport->tcp_calldir)) {
987 case RPC_REPLY:
988 transport->tcp_flags |= TCP_RCV_COPY_CALLDIR;
989 transport->tcp_flags |= TCP_RCV_COPY_DATA;
990 transport->tcp_flags |= TCP_RPC_REPLY;
991 break;
992 case RPC_CALL:
993 transport->tcp_flags |= TCP_RCV_COPY_CALLDIR;
994 transport->tcp_flags |= TCP_RCV_COPY_DATA;
995 transport->tcp_flags &= ~TCP_RPC_REPLY;
996 break;
997 default:
998 dprintk("RPC: invalid request message type\n");
999 xprt_force_disconnect(&transport->xprt);
1001 xs_tcp_check_fraghdr(transport);
1004 static inline void xs_tcp_read_common(struct rpc_xprt *xprt,
1005 struct xdr_skb_reader *desc,
1006 struct rpc_rqst *req)
1008 struct sock_xprt *transport =
1009 container_of(xprt, struct sock_xprt, xprt);
1010 struct xdr_buf *rcvbuf;
1011 size_t len;
1012 ssize_t r;
1014 rcvbuf = &req->rq_private_buf;
1016 if (transport->tcp_flags & TCP_RCV_COPY_CALLDIR) {
1018 * Save the RPC direction in the XDR buffer
1020 memcpy(rcvbuf->head[0].iov_base + transport->tcp_copied,
1021 &transport->tcp_calldir,
1022 sizeof(transport->tcp_calldir));
1023 transport->tcp_copied += sizeof(transport->tcp_calldir);
1024 transport->tcp_flags &= ~TCP_RCV_COPY_CALLDIR;
1027 len = desc->count;
1028 if (len > transport->tcp_reclen - transport->tcp_offset) {
1029 struct xdr_skb_reader my_desc;
1031 len = transport->tcp_reclen - transport->tcp_offset;
1032 memcpy(&my_desc, desc, sizeof(my_desc));
1033 my_desc.count = len;
1034 r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
1035 &my_desc, xdr_skb_read_bits);
1036 desc->count -= r;
1037 desc->offset += r;
1038 } else
1039 r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
1040 desc, xdr_skb_read_bits);
1042 if (r > 0) {
1043 transport->tcp_copied += r;
1044 transport->tcp_offset += r;
1046 if (r != len) {
1047 /* Error when copying to the receive buffer,
1048 * usually because we weren't able to allocate
1049 * additional buffer pages. All we can do now
1050 * is turn off TCP_RCV_COPY_DATA, so the request
1051 * will not receive any additional updates,
1052 * and time out.
1053 * Any remaining data from this record will
1054 * be discarded.
1056 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1057 dprintk("RPC: XID %08x truncated request\n",
1058 ntohl(transport->tcp_xid));
1059 dprintk("RPC: xprt = %p, tcp_copied = %lu, "
1060 "tcp_offset = %u, tcp_reclen = %u\n",
1061 xprt, transport->tcp_copied,
1062 transport->tcp_offset, transport->tcp_reclen);
1063 return;
1066 dprintk("RPC: XID %08x read %Zd bytes\n",
1067 ntohl(transport->tcp_xid), r);
1068 dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, "
1069 "tcp_reclen = %u\n", xprt, transport->tcp_copied,
1070 transport->tcp_offset, transport->tcp_reclen);
1072 if (transport->tcp_copied == req->rq_private_buf.buflen)
1073 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1074 else if (transport->tcp_offset == transport->tcp_reclen) {
1075 if (transport->tcp_flags & TCP_RCV_LAST_FRAG)
1076 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1079 return;
1083 * Finds the request corresponding to the RPC xid and invokes the common
1084 * tcp read code to read the data.
1086 static inline int xs_tcp_read_reply(struct rpc_xprt *xprt,
1087 struct xdr_skb_reader *desc)
1089 struct sock_xprt *transport =
1090 container_of(xprt, struct sock_xprt, xprt);
1091 struct rpc_rqst *req;
1093 dprintk("RPC: read reply XID %08x\n", ntohl(transport->tcp_xid));
1095 /* Find and lock the request corresponding to this xid */
1096 spin_lock(&xprt->transport_lock);
1097 req = xprt_lookup_rqst(xprt, transport->tcp_xid);
1098 if (!req) {
1099 dprintk("RPC: XID %08x request not found!\n",
1100 ntohl(transport->tcp_xid));
1101 spin_unlock(&xprt->transport_lock);
1102 return -1;
1105 xs_tcp_read_common(xprt, desc, req);
1107 if (!(transport->tcp_flags & TCP_RCV_COPY_DATA))
1108 xprt_complete_rqst(req->rq_task, transport->tcp_copied);
1110 spin_unlock(&xprt->transport_lock);
1111 return 0;
1114 #if defined(CONFIG_NFS_V4_1)
1116 * Obtains an rpc_rqst previously allocated and invokes the common
1117 * tcp read code to read the data. The result is placed in the callback
1118 * queue.
1119 * If we're unable to obtain the rpc_rqst we schedule the closing of the
1120 * connection and return -1.
1122 static inline int xs_tcp_read_callback(struct rpc_xprt *xprt,
1123 struct xdr_skb_reader *desc)
1125 struct sock_xprt *transport =
1126 container_of(xprt, struct sock_xprt, xprt);
1127 struct rpc_rqst *req;
1129 req = xprt_alloc_bc_request(xprt);
1130 if (req == NULL) {
1131 printk(KERN_WARNING "Callback slot table overflowed\n");
1132 xprt_force_disconnect(xprt);
1133 return -1;
1136 req->rq_xid = transport->tcp_xid;
1137 dprintk("RPC: read callback XID %08x\n", ntohl(req->rq_xid));
1138 xs_tcp_read_common(xprt, desc, req);
1140 if (!(transport->tcp_flags & TCP_RCV_COPY_DATA)) {
1141 struct svc_serv *bc_serv = xprt->bc_serv;
1144 * Add callback request to callback list. The callback
1145 * service sleeps on the sv_cb_waitq waiting for new
1146 * requests. Wake it up after adding enqueing the
1147 * request.
1149 dprintk("RPC: add callback request to list\n");
1150 spin_lock(&bc_serv->sv_cb_lock);
1151 list_add(&req->rq_bc_list, &bc_serv->sv_cb_list);
1152 spin_unlock(&bc_serv->sv_cb_lock);
1153 wake_up(&bc_serv->sv_cb_waitq);
1156 req->rq_private_buf.len = transport->tcp_copied;
1158 return 0;
1161 static inline int _xs_tcp_read_data(struct rpc_xprt *xprt,
1162 struct xdr_skb_reader *desc)
1164 struct sock_xprt *transport =
1165 container_of(xprt, struct sock_xprt, xprt);
1167 return (transport->tcp_flags & TCP_RPC_REPLY) ?
1168 xs_tcp_read_reply(xprt, desc) :
1169 xs_tcp_read_callback(xprt, desc);
1171 #else
1172 static inline int _xs_tcp_read_data(struct rpc_xprt *xprt,
1173 struct xdr_skb_reader *desc)
1175 return xs_tcp_read_reply(xprt, desc);
1177 #endif /* CONFIG_NFS_V4_1 */
1180 * Read data off the transport. This can be either an RPC_CALL or an
1181 * RPC_REPLY. Relay the processing to helper functions.
1183 static void xs_tcp_read_data(struct rpc_xprt *xprt,
1184 struct xdr_skb_reader *desc)
1186 struct sock_xprt *transport =
1187 container_of(xprt, struct sock_xprt, xprt);
1189 if (_xs_tcp_read_data(xprt, desc) == 0)
1190 xs_tcp_check_fraghdr(transport);
1191 else {
1193 * The transport_lock protects the request handling.
1194 * There's no need to hold it to update the tcp_flags.
1196 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1200 static inline void xs_tcp_read_discard(struct sock_xprt *transport, struct xdr_skb_reader *desc)
1202 size_t len;
1204 len = transport->tcp_reclen - transport->tcp_offset;
1205 if (len > desc->count)
1206 len = desc->count;
1207 desc->count -= len;
1208 desc->offset += len;
1209 transport->tcp_offset += len;
1210 dprintk("RPC: discarded %Zu bytes\n", len);
1211 xs_tcp_check_fraghdr(transport);
1214 static int xs_tcp_data_recv(read_descriptor_t *rd_desc, struct sk_buff *skb, unsigned int offset, size_t len)
1216 struct rpc_xprt *xprt = rd_desc->arg.data;
1217 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1218 struct xdr_skb_reader desc = {
1219 .skb = skb,
1220 .offset = offset,
1221 .count = len,
1224 dprintk("RPC: xs_tcp_data_recv started\n");
1225 do {
1226 /* Read in a new fragment marker if necessary */
1227 /* Can we ever really expect to get completely empty fragments? */
1228 if (transport->tcp_flags & TCP_RCV_COPY_FRAGHDR) {
1229 xs_tcp_read_fraghdr(xprt, &desc);
1230 continue;
1232 /* Read in the xid if necessary */
1233 if (transport->tcp_flags & TCP_RCV_COPY_XID) {
1234 xs_tcp_read_xid(transport, &desc);
1235 continue;
1237 /* Read in the call/reply flag */
1238 if (transport->tcp_flags & TCP_RCV_READ_CALLDIR) {
1239 xs_tcp_read_calldir(transport, &desc);
1240 continue;
1242 /* Read in the request data */
1243 if (transport->tcp_flags & TCP_RCV_COPY_DATA) {
1244 xs_tcp_read_data(xprt, &desc);
1245 continue;
1247 /* Skip over any trailing bytes on short reads */
1248 xs_tcp_read_discard(transport, &desc);
1249 } while (desc.count);
1250 dprintk("RPC: xs_tcp_data_recv done\n");
1251 return len - desc.count;
1255 * xs_tcp_data_ready - "data ready" callback for TCP sockets
1256 * @sk: socket with data to read
1257 * @bytes: how much data to read
1260 static void xs_tcp_data_ready(struct sock *sk, int bytes)
1262 struct rpc_xprt *xprt;
1263 read_descriptor_t rd_desc;
1264 int read;
1266 dprintk("RPC: xs_tcp_data_ready...\n");
1268 read_lock(&sk->sk_callback_lock);
1269 if (!(xprt = xprt_from_sock(sk)))
1270 goto out;
1271 if (xprt->shutdown)
1272 goto out;
1274 /* Any data means we had a useful conversation, so
1275 * the we don't need to delay the next reconnect
1277 if (xprt->reestablish_timeout)
1278 xprt->reestablish_timeout = 0;
1280 /* We use rd_desc to pass struct xprt to xs_tcp_data_recv */
1281 rd_desc.arg.data = xprt;
1282 do {
1283 rd_desc.count = 65536;
1284 read = tcp_read_sock(sk, &rd_desc, xs_tcp_data_recv);
1285 } while (read > 0);
1286 out:
1287 read_unlock(&sk->sk_callback_lock);
1291 * Do the equivalent of linger/linger2 handling for dealing with
1292 * broken servers that don't close the socket in a timely
1293 * fashion
1295 static void xs_tcp_schedule_linger_timeout(struct rpc_xprt *xprt,
1296 unsigned long timeout)
1298 struct sock_xprt *transport;
1300 if (xprt_test_and_set_connecting(xprt))
1301 return;
1302 set_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1303 transport = container_of(xprt, struct sock_xprt, xprt);
1304 queue_delayed_work(rpciod_workqueue, &transport->connect_worker,
1305 timeout);
1308 static void xs_tcp_cancel_linger_timeout(struct rpc_xprt *xprt)
1310 struct sock_xprt *transport;
1312 transport = container_of(xprt, struct sock_xprt, xprt);
1314 if (!test_bit(XPRT_CONNECTION_ABORT, &xprt->state) ||
1315 !cancel_delayed_work(&transport->connect_worker))
1316 return;
1317 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1318 xprt_clear_connecting(xprt);
1321 static void xs_sock_mark_closed(struct rpc_xprt *xprt)
1323 smp_mb__before_clear_bit();
1324 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
1325 clear_bit(XPRT_CLOSING, &xprt->state);
1326 smp_mb__after_clear_bit();
1327 /* Mark transport as closed and wake up all pending tasks */
1328 xprt_disconnect_done(xprt);
1332 * xs_tcp_state_change - callback to handle TCP socket state changes
1333 * @sk: socket whose state has changed
1336 static void xs_tcp_state_change(struct sock *sk)
1338 struct rpc_xprt *xprt;
1340 read_lock(&sk->sk_callback_lock);
1341 if (!(xprt = xprt_from_sock(sk)))
1342 goto out;
1343 dprintk("RPC: xs_tcp_state_change client %p...\n", xprt);
1344 dprintk("RPC: state %x conn %d dead %d zapped %d\n",
1345 sk->sk_state, xprt_connected(xprt),
1346 sock_flag(sk, SOCK_DEAD),
1347 sock_flag(sk, SOCK_ZAPPED));
1349 switch (sk->sk_state) {
1350 case TCP_ESTABLISHED:
1351 spin_lock_bh(&xprt->transport_lock);
1352 if (!xprt_test_and_set_connected(xprt)) {
1353 struct sock_xprt *transport = container_of(xprt,
1354 struct sock_xprt, xprt);
1356 /* Reset TCP record info */
1357 transport->tcp_offset = 0;
1358 transport->tcp_reclen = 0;
1359 transport->tcp_copied = 0;
1360 transport->tcp_flags =
1361 TCP_RCV_COPY_FRAGHDR | TCP_RCV_COPY_XID;
1363 xprt_wake_pending_tasks(xprt, -EAGAIN);
1365 spin_unlock_bh(&xprt->transport_lock);
1366 break;
1367 case TCP_FIN_WAIT1:
1368 /* The client initiated a shutdown of the socket */
1369 xprt->connect_cookie++;
1370 xprt->reestablish_timeout = 0;
1371 set_bit(XPRT_CLOSING, &xprt->state);
1372 smp_mb__before_clear_bit();
1373 clear_bit(XPRT_CONNECTED, &xprt->state);
1374 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
1375 smp_mb__after_clear_bit();
1376 xs_tcp_schedule_linger_timeout(xprt, xs_tcp_fin_timeout);
1377 break;
1378 case TCP_CLOSE_WAIT:
1379 /* The server initiated a shutdown of the socket */
1380 xprt_force_disconnect(xprt);
1381 case TCP_SYN_SENT:
1382 xprt->connect_cookie++;
1383 case TCP_CLOSING:
1385 * If the server closed down the connection, make sure that
1386 * we back off before reconnecting
1388 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
1389 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
1390 break;
1391 case TCP_LAST_ACK:
1392 set_bit(XPRT_CLOSING, &xprt->state);
1393 xs_tcp_schedule_linger_timeout(xprt, xs_tcp_fin_timeout);
1394 smp_mb__before_clear_bit();
1395 clear_bit(XPRT_CONNECTED, &xprt->state);
1396 smp_mb__after_clear_bit();
1397 break;
1398 case TCP_CLOSE:
1399 xs_tcp_cancel_linger_timeout(xprt);
1400 xs_sock_mark_closed(xprt);
1402 out:
1403 read_unlock(&sk->sk_callback_lock);
1407 * xs_error_report - callback mainly for catching socket errors
1408 * @sk: socket
1410 static void xs_error_report(struct sock *sk)
1412 struct rpc_xprt *xprt;
1414 read_lock(&sk->sk_callback_lock);
1415 if (!(xprt = xprt_from_sock(sk)))
1416 goto out;
1417 dprintk("RPC: %s client %p...\n"
1418 "RPC: error %d\n",
1419 __func__, xprt, sk->sk_err);
1420 xprt_wake_pending_tasks(xprt, -EAGAIN);
1421 out:
1422 read_unlock(&sk->sk_callback_lock);
1425 static void xs_write_space(struct sock *sk)
1427 struct socket *sock;
1428 struct rpc_xprt *xprt;
1430 if (unlikely(!(sock = sk->sk_socket)))
1431 return;
1432 clear_bit(SOCK_NOSPACE, &sock->flags);
1434 if (unlikely(!(xprt = xprt_from_sock(sk))))
1435 return;
1436 if (test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags) == 0)
1437 return;
1439 xprt_write_space(xprt);
1443 * xs_udp_write_space - callback invoked when socket buffer space
1444 * becomes available
1445 * @sk: socket whose state has changed
1447 * Called when more output buffer space is available for this socket.
1448 * We try not to wake our writers until they can make "significant"
1449 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1450 * with a bunch of small requests.
1452 static void xs_udp_write_space(struct sock *sk)
1454 read_lock(&sk->sk_callback_lock);
1456 /* from net/core/sock.c:sock_def_write_space */
1457 if (sock_writeable(sk))
1458 xs_write_space(sk);
1460 read_unlock(&sk->sk_callback_lock);
1464 * xs_tcp_write_space - callback invoked when socket buffer space
1465 * becomes available
1466 * @sk: socket whose state has changed
1468 * Called when more output buffer space is available for this socket.
1469 * We try not to wake our writers until they can make "significant"
1470 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1471 * with a bunch of small requests.
1473 static void xs_tcp_write_space(struct sock *sk)
1475 read_lock(&sk->sk_callback_lock);
1477 /* from net/core/stream.c:sk_stream_write_space */
1478 if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk))
1479 xs_write_space(sk);
1481 read_unlock(&sk->sk_callback_lock);
1484 static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt)
1486 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1487 struct sock *sk = transport->inet;
1489 if (transport->rcvsize) {
1490 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
1491 sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2;
1493 if (transport->sndsize) {
1494 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
1495 sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2;
1496 sk->sk_write_space(sk);
1501 * xs_udp_set_buffer_size - set send and receive limits
1502 * @xprt: generic transport
1503 * @sndsize: requested size of send buffer, in bytes
1504 * @rcvsize: requested size of receive buffer, in bytes
1506 * Set socket send and receive buffer size limits.
1508 static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize)
1510 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1512 transport->sndsize = 0;
1513 if (sndsize)
1514 transport->sndsize = sndsize + 1024;
1515 transport->rcvsize = 0;
1516 if (rcvsize)
1517 transport->rcvsize = rcvsize + 1024;
1519 xs_udp_do_set_buffer_size(xprt);
1523 * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
1524 * @task: task that timed out
1526 * Adjust the congestion window after a retransmit timeout has occurred.
1528 static void xs_udp_timer(struct rpc_task *task)
1530 xprt_adjust_cwnd(task, -ETIMEDOUT);
1533 static unsigned short xs_get_random_port(void)
1535 unsigned short range = xprt_max_resvport - xprt_min_resvport;
1536 unsigned short rand = (unsigned short) net_random() % range;
1537 return rand + xprt_min_resvport;
1541 * xs_set_port - reset the port number in the remote endpoint address
1542 * @xprt: generic transport
1543 * @port: new port number
1546 static void xs_set_port(struct rpc_xprt *xprt, unsigned short port)
1548 dprintk("RPC: setting port for xprt %p to %u\n", xprt, port);
1550 rpc_set_port(xs_addr(xprt), port);
1551 xs_update_peer_port(xprt);
1554 static unsigned short xs_get_srcport(struct sock_xprt *transport, struct socket *sock)
1556 unsigned short port = transport->srcport;
1558 if (port == 0 && transport->xprt.resvport)
1559 port = xs_get_random_port();
1560 return port;
1563 static unsigned short xs_next_srcport(struct sock_xprt *transport, struct socket *sock, unsigned short port)
1565 if (transport->srcport != 0)
1566 transport->srcport = 0;
1567 if (!transport->xprt.resvport)
1568 return 0;
1569 if (port <= xprt_min_resvport || port > xprt_max_resvport)
1570 return xprt_max_resvport;
1571 return --port;
1574 static int xs_bind4(struct sock_xprt *transport, struct socket *sock)
1576 struct sockaddr_in myaddr = {
1577 .sin_family = AF_INET,
1579 struct sockaddr_in *sa;
1580 int err, nloop = 0;
1581 unsigned short port = xs_get_srcport(transport, sock);
1582 unsigned short last;
1584 sa = (struct sockaddr_in *)&transport->srcaddr;
1585 myaddr.sin_addr = sa->sin_addr;
1586 do {
1587 myaddr.sin_port = htons(port);
1588 err = kernel_bind(sock, (struct sockaddr *) &myaddr,
1589 sizeof(myaddr));
1590 if (port == 0)
1591 break;
1592 if (err == 0) {
1593 transport->srcport = port;
1594 break;
1596 last = port;
1597 port = xs_next_srcport(transport, sock, port);
1598 if (port > last)
1599 nloop++;
1600 } while (err == -EADDRINUSE && nloop != 2);
1601 dprintk("RPC: %s %pI4:%u: %s (%d)\n",
1602 __func__, &myaddr.sin_addr,
1603 port, err ? "failed" : "ok", err);
1604 return err;
1607 static int xs_bind6(struct sock_xprt *transport, struct socket *sock)
1609 struct sockaddr_in6 myaddr = {
1610 .sin6_family = AF_INET6,
1612 struct sockaddr_in6 *sa;
1613 int err, nloop = 0;
1614 unsigned short port = xs_get_srcport(transport, sock);
1615 unsigned short last;
1617 sa = (struct sockaddr_in6 *)&transport->srcaddr;
1618 myaddr.sin6_addr = sa->sin6_addr;
1619 do {
1620 myaddr.sin6_port = htons(port);
1621 err = kernel_bind(sock, (struct sockaddr *) &myaddr,
1622 sizeof(myaddr));
1623 if (port == 0)
1624 break;
1625 if (err == 0) {
1626 transport->srcport = port;
1627 break;
1629 last = port;
1630 port = xs_next_srcport(transport, sock, port);
1631 if (port > last)
1632 nloop++;
1633 } while (err == -EADDRINUSE && nloop != 2);
1634 dprintk("RPC: xs_bind6 %pI6:%u: %s (%d)\n",
1635 &myaddr.sin6_addr, port, err ? "failed" : "ok", err);
1636 return err;
1639 #ifdef CONFIG_DEBUG_LOCK_ALLOC
1640 static struct lock_class_key xs_key[2];
1641 static struct lock_class_key xs_slock_key[2];
1643 static inline void xs_reclassify_socket4(struct socket *sock)
1645 struct sock *sk = sock->sk;
1647 BUG_ON(sock_owned_by_user(sk));
1648 sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC",
1649 &xs_slock_key[0], "sk_lock-AF_INET-RPC", &xs_key[0]);
1652 static inline void xs_reclassify_socket6(struct socket *sock)
1654 struct sock *sk = sock->sk;
1656 BUG_ON(sock_owned_by_user(sk));
1657 sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC",
1658 &xs_slock_key[1], "sk_lock-AF_INET6-RPC", &xs_key[1]);
1660 #else
1661 static inline void xs_reclassify_socket4(struct socket *sock)
1665 static inline void xs_reclassify_socket6(struct socket *sock)
1668 #endif
1670 static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
1672 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1674 if (!transport->inet) {
1675 struct sock *sk = sock->sk;
1677 write_lock_bh(&sk->sk_callback_lock);
1679 xs_save_old_callbacks(transport, sk);
1681 sk->sk_user_data = xprt;
1682 sk->sk_data_ready = xs_udp_data_ready;
1683 sk->sk_write_space = xs_udp_write_space;
1684 sk->sk_error_report = xs_error_report;
1685 sk->sk_no_check = UDP_CSUM_NORCV;
1686 sk->sk_allocation = GFP_ATOMIC;
1688 xprt_set_connected(xprt);
1690 /* Reset to new socket */
1691 transport->sock = sock;
1692 transport->inet = sk;
1694 write_unlock_bh(&sk->sk_callback_lock);
1696 xs_udp_do_set_buffer_size(xprt);
1700 * xs_udp_connect_worker4 - set up a UDP socket
1701 * @work: RPC transport to connect
1703 * Invoked by a work queue tasklet.
1705 static void xs_udp_connect_worker4(struct work_struct *work)
1707 struct sock_xprt *transport =
1708 container_of(work, struct sock_xprt, connect_worker.work);
1709 struct rpc_xprt *xprt = &transport->xprt;
1710 struct socket *sock = transport->sock;
1711 int err, status = -EIO;
1713 if (xprt->shutdown)
1714 goto out;
1716 /* Start by resetting any existing state */
1717 xs_reset_transport(transport);
1719 err = sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock);
1720 if (err < 0) {
1721 dprintk("RPC: can't create UDP transport socket (%d).\n", -err);
1722 goto out;
1724 xs_reclassify_socket4(sock);
1726 if (xs_bind4(transport, sock)) {
1727 sock_release(sock);
1728 goto out;
1731 dprintk("RPC: worker connecting xprt %p via %s to "
1732 "%s (port %s)\n", xprt,
1733 xprt->address_strings[RPC_DISPLAY_PROTO],
1734 xprt->address_strings[RPC_DISPLAY_ADDR],
1735 xprt->address_strings[RPC_DISPLAY_PORT]);
1737 xs_udp_finish_connecting(xprt, sock);
1738 status = 0;
1739 out:
1740 xprt_clear_connecting(xprt);
1741 xprt_wake_pending_tasks(xprt, status);
1745 * xs_udp_connect_worker6 - set up a UDP socket
1746 * @work: RPC transport to connect
1748 * Invoked by a work queue tasklet.
1750 static void xs_udp_connect_worker6(struct work_struct *work)
1752 struct sock_xprt *transport =
1753 container_of(work, struct sock_xprt, connect_worker.work);
1754 struct rpc_xprt *xprt = &transport->xprt;
1755 struct socket *sock = transport->sock;
1756 int err, status = -EIO;
1758 if (xprt->shutdown)
1759 goto out;
1761 /* Start by resetting any existing state */
1762 xs_reset_transport(transport);
1764 err = sock_create_kern(PF_INET6, SOCK_DGRAM, IPPROTO_UDP, &sock);
1765 if (err < 0) {
1766 dprintk("RPC: can't create UDP transport socket (%d).\n", -err);
1767 goto out;
1769 xs_reclassify_socket6(sock);
1771 if (xs_bind6(transport, sock) < 0) {
1772 sock_release(sock);
1773 goto out;
1776 dprintk("RPC: worker connecting xprt %p via %s to "
1777 "%s (port %s)\n", xprt,
1778 xprt->address_strings[RPC_DISPLAY_PROTO],
1779 xprt->address_strings[RPC_DISPLAY_ADDR],
1780 xprt->address_strings[RPC_DISPLAY_PORT]);
1782 xs_udp_finish_connecting(xprt, sock);
1783 status = 0;
1784 out:
1785 xprt_clear_connecting(xprt);
1786 xprt_wake_pending_tasks(xprt, status);
1790 * We need to preserve the port number so the reply cache on the server can
1791 * find our cached RPC replies when we get around to reconnecting.
1793 static void xs_abort_connection(struct rpc_xprt *xprt, struct sock_xprt *transport)
1795 int result;
1796 struct sockaddr any;
1798 dprintk("RPC: disconnecting xprt %p to reuse port\n", xprt);
1801 * Disconnect the transport socket by doing a connect operation
1802 * with AF_UNSPEC. This should return immediately...
1804 memset(&any, 0, sizeof(any));
1805 any.sa_family = AF_UNSPEC;
1806 result = kernel_connect(transport->sock, &any, sizeof(any), 0);
1807 if (!result)
1808 xs_sock_mark_closed(xprt);
1809 else
1810 dprintk("RPC: AF_UNSPEC connect return code %d\n",
1811 result);
1814 static void xs_tcp_reuse_connection(struct rpc_xprt *xprt, struct sock_xprt *transport)
1816 unsigned int state = transport->inet->sk_state;
1818 if (state == TCP_CLOSE && transport->sock->state == SS_UNCONNECTED)
1819 return;
1820 if ((1 << state) & (TCPF_ESTABLISHED|TCPF_SYN_SENT))
1821 return;
1822 xs_abort_connection(xprt, transport);
1825 static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
1827 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1829 if (!transport->inet) {
1830 struct sock *sk = sock->sk;
1832 write_lock_bh(&sk->sk_callback_lock);
1834 xs_save_old_callbacks(transport, sk);
1836 sk->sk_user_data = xprt;
1837 sk->sk_data_ready = xs_tcp_data_ready;
1838 sk->sk_state_change = xs_tcp_state_change;
1839 sk->sk_write_space = xs_tcp_write_space;
1840 sk->sk_error_report = xs_error_report;
1841 sk->sk_allocation = GFP_ATOMIC;
1843 /* socket options */
1844 sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
1845 sock_reset_flag(sk, SOCK_LINGER);
1846 tcp_sk(sk)->linger2 = 0;
1847 tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF;
1849 xprt_clear_connected(xprt);
1851 /* Reset to new socket */
1852 transport->sock = sock;
1853 transport->inet = sk;
1855 write_unlock_bh(&sk->sk_callback_lock);
1858 if (!xprt_bound(xprt))
1859 return -ENOTCONN;
1861 /* Tell the socket layer to start connecting... */
1862 xprt->stat.connect_count++;
1863 xprt->stat.connect_start = jiffies;
1864 return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK);
1868 * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint
1869 * @xprt: RPC transport to connect
1870 * @transport: socket transport to connect
1871 * @create_sock: function to create a socket of the correct type
1873 * Invoked by a work queue tasklet.
1875 static void xs_tcp_setup_socket(struct rpc_xprt *xprt,
1876 struct sock_xprt *transport,
1877 struct socket *(*create_sock)(struct rpc_xprt *,
1878 struct sock_xprt *))
1880 struct socket *sock = transport->sock;
1881 int status = -EIO;
1883 if (xprt->shutdown)
1884 goto out;
1886 if (!sock) {
1887 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1888 sock = create_sock(xprt, transport);
1889 if (IS_ERR(sock)) {
1890 status = PTR_ERR(sock);
1891 goto out;
1893 } else {
1894 int abort_and_exit;
1896 abort_and_exit = test_and_clear_bit(XPRT_CONNECTION_ABORT,
1897 &xprt->state);
1898 /* "close" the socket, preserving the local port */
1899 xs_tcp_reuse_connection(xprt, transport);
1901 if (abort_and_exit)
1902 goto out_eagain;
1905 dprintk("RPC: worker connecting xprt %p via %s to "
1906 "%s (port %s)\n", xprt,
1907 xprt->address_strings[RPC_DISPLAY_PROTO],
1908 xprt->address_strings[RPC_DISPLAY_ADDR],
1909 xprt->address_strings[RPC_DISPLAY_PORT]);
1911 status = xs_tcp_finish_connecting(xprt, sock);
1912 dprintk("RPC: %p connect status %d connected %d sock state %d\n",
1913 xprt, -status, xprt_connected(xprt),
1914 sock->sk->sk_state);
1915 switch (status) {
1916 default:
1917 printk("%s: connect returned unhandled error %d\n",
1918 __func__, status);
1919 case -EADDRNOTAVAIL:
1920 /* We're probably in TIME_WAIT. Get rid of existing socket,
1921 * and retry
1923 set_bit(XPRT_CONNECTION_CLOSE, &xprt->state);
1924 xprt_force_disconnect(xprt);
1925 break;
1926 case -ECONNREFUSED:
1927 case -ECONNRESET:
1928 case -ENETUNREACH:
1929 /* retry with existing socket, after a delay */
1930 case 0:
1931 case -EINPROGRESS:
1932 case -EALREADY:
1933 xprt_clear_connecting(xprt);
1934 return;
1935 case -EINVAL:
1936 /* Happens, for instance, if the user specified a link
1937 * local IPv6 address without a scope-id.
1939 goto out;
1941 out_eagain:
1942 status = -EAGAIN;
1943 out:
1944 xprt_clear_connecting(xprt);
1945 xprt_wake_pending_tasks(xprt, status);
1948 static struct socket *xs_create_tcp_sock4(struct rpc_xprt *xprt,
1949 struct sock_xprt *transport)
1951 struct socket *sock;
1952 int err;
1954 /* start from scratch */
1955 err = sock_create_kern(PF_INET, SOCK_STREAM, IPPROTO_TCP, &sock);
1956 if (err < 0) {
1957 dprintk("RPC: can't create TCP transport socket (%d).\n",
1958 -err);
1959 goto out_err;
1961 xs_reclassify_socket4(sock);
1963 if (xs_bind4(transport, sock) < 0) {
1964 sock_release(sock);
1965 goto out_err;
1967 return sock;
1968 out_err:
1969 return ERR_PTR(-EIO);
1973 * xs_tcp_connect_worker4 - connect a TCP socket to a remote endpoint
1974 * @work: RPC transport to connect
1976 * Invoked by a work queue tasklet.
1978 static void xs_tcp_connect_worker4(struct work_struct *work)
1980 struct sock_xprt *transport =
1981 container_of(work, struct sock_xprt, connect_worker.work);
1982 struct rpc_xprt *xprt = &transport->xprt;
1984 xs_tcp_setup_socket(xprt, transport, xs_create_tcp_sock4);
1987 static struct socket *xs_create_tcp_sock6(struct rpc_xprt *xprt,
1988 struct sock_xprt *transport)
1990 struct socket *sock;
1991 int err;
1993 /* start from scratch */
1994 err = sock_create_kern(PF_INET6, SOCK_STREAM, IPPROTO_TCP, &sock);
1995 if (err < 0) {
1996 dprintk("RPC: can't create TCP transport socket (%d).\n",
1997 -err);
1998 goto out_err;
2000 xs_reclassify_socket6(sock);
2002 if (xs_bind6(transport, sock) < 0) {
2003 sock_release(sock);
2004 goto out_err;
2006 return sock;
2007 out_err:
2008 return ERR_PTR(-EIO);
2012 * xs_tcp_connect_worker6 - connect a TCP socket to a remote endpoint
2013 * @work: RPC transport to connect
2015 * Invoked by a work queue tasklet.
2017 static void xs_tcp_connect_worker6(struct work_struct *work)
2019 struct sock_xprt *transport =
2020 container_of(work, struct sock_xprt, connect_worker.work);
2021 struct rpc_xprt *xprt = &transport->xprt;
2023 xs_tcp_setup_socket(xprt, transport, xs_create_tcp_sock6);
2027 * xs_connect - connect a socket to a remote endpoint
2028 * @task: address of RPC task that manages state of connect request
2030 * TCP: If the remote end dropped the connection, delay reconnecting.
2032 * UDP socket connects are synchronous, but we use a work queue anyway
2033 * to guarantee that even unprivileged user processes can set up a
2034 * socket on a privileged port.
2036 * If a UDP socket connect fails, the delay behavior here prevents
2037 * retry floods (hard mounts).
2039 static void xs_connect(struct rpc_task *task)
2041 struct rpc_xprt *xprt = task->tk_xprt;
2042 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2044 if (xprt_test_and_set_connecting(xprt))
2045 return;
2047 if (transport->sock != NULL) {
2048 dprintk("RPC: xs_connect delayed xprt %p for %lu "
2049 "seconds\n",
2050 xprt, xprt->reestablish_timeout / HZ);
2051 queue_delayed_work(rpciod_workqueue,
2052 &transport->connect_worker,
2053 xprt->reestablish_timeout);
2054 xprt->reestablish_timeout <<= 1;
2055 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
2056 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2057 if (xprt->reestablish_timeout > XS_TCP_MAX_REEST_TO)
2058 xprt->reestablish_timeout = XS_TCP_MAX_REEST_TO;
2059 } else {
2060 dprintk("RPC: xs_connect scheduled xprt %p\n", xprt);
2061 queue_delayed_work(rpciod_workqueue,
2062 &transport->connect_worker, 0);
2066 static void xs_tcp_connect(struct rpc_task *task)
2068 struct rpc_xprt *xprt = task->tk_xprt;
2070 /* Exit if we need to wait for socket shutdown to complete */
2071 if (test_bit(XPRT_CLOSING, &xprt->state))
2072 return;
2073 xs_connect(task);
2077 * xs_udp_print_stats - display UDP socket-specifc stats
2078 * @xprt: rpc_xprt struct containing statistics
2079 * @seq: output file
2082 static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2084 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2086 seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %Lu %Lu\n",
2087 transport->srcport,
2088 xprt->stat.bind_count,
2089 xprt->stat.sends,
2090 xprt->stat.recvs,
2091 xprt->stat.bad_xids,
2092 xprt->stat.req_u,
2093 xprt->stat.bklog_u);
2097 * xs_tcp_print_stats - display TCP socket-specifc stats
2098 * @xprt: rpc_xprt struct containing statistics
2099 * @seq: output file
2102 static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2104 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2105 long idle_time = 0;
2107 if (xprt_connected(xprt))
2108 idle_time = (long)(jiffies - xprt->last_used) / HZ;
2110 seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu %Lu %Lu\n",
2111 transport->srcport,
2112 xprt->stat.bind_count,
2113 xprt->stat.connect_count,
2114 xprt->stat.connect_time,
2115 idle_time,
2116 xprt->stat.sends,
2117 xprt->stat.recvs,
2118 xprt->stat.bad_xids,
2119 xprt->stat.req_u,
2120 xprt->stat.bklog_u);
2124 * Allocate a bunch of pages for a scratch buffer for the rpc code. The reason
2125 * we allocate pages instead doing a kmalloc like rpc_malloc is because we want
2126 * to use the server side send routines.
2128 void *bc_malloc(struct rpc_task *task, size_t size)
2130 struct page *page;
2131 struct rpc_buffer *buf;
2133 BUG_ON(size > PAGE_SIZE - sizeof(struct rpc_buffer));
2134 page = alloc_page(GFP_KERNEL);
2136 if (!page)
2137 return NULL;
2139 buf = page_address(page);
2140 buf->len = PAGE_SIZE;
2142 return buf->data;
2146 * Free the space allocated in the bc_alloc routine
2148 void bc_free(void *buffer)
2150 struct rpc_buffer *buf;
2152 if (!buffer)
2153 return;
2155 buf = container_of(buffer, struct rpc_buffer, data);
2156 free_page((unsigned long)buf);
2160 * Use the svc_sock to send the callback. Must be called with svsk->sk_mutex
2161 * held. Borrows heavily from svc_tcp_sendto and xs_tcp_send_request.
2163 static int bc_sendto(struct rpc_rqst *req)
2165 int len;
2166 struct xdr_buf *xbufp = &req->rq_snd_buf;
2167 struct rpc_xprt *xprt = req->rq_xprt;
2168 struct sock_xprt *transport =
2169 container_of(xprt, struct sock_xprt, xprt);
2170 struct socket *sock = transport->sock;
2171 unsigned long headoff;
2172 unsigned long tailoff;
2175 * Set up the rpc header and record marker stuff
2177 xs_encode_tcp_record_marker(xbufp);
2179 tailoff = (unsigned long)xbufp->tail[0].iov_base & ~PAGE_MASK;
2180 headoff = (unsigned long)xbufp->head[0].iov_base & ~PAGE_MASK;
2181 len = svc_send_common(sock, xbufp,
2182 virt_to_page(xbufp->head[0].iov_base), headoff,
2183 xbufp->tail[0].iov_base, tailoff);
2185 if (len != xbufp->len) {
2186 printk(KERN_NOTICE "Error sending entire callback!\n");
2187 len = -EAGAIN;
2190 return len;
2194 * The send routine. Borrows from svc_send
2196 static int bc_send_request(struct rpc_task *task)
2198 struct rpc_rqst *req = task->tk_rqstp;
2199 struct svc_xprt *xprt;
2200 struct svc_sock *svsk;
2201 u32 len;
2203 dprintk("sending request with xid: %08x\n", ntohl(req->rq_xid));
2205 * Get the server socket associated with this callback xprt
2207 xprt = req->rq_xprt->bc_xprt;
2208 svsk = container_of(xprt, struct svc_sock, sk_xprt);
2211 * Grab the mutex to serialize data as the connection is shared
2212 * with the fore channel
2214 if (!mutex_trylock(&xprt->xpt_mutex)) {
2215 rpc_sleep_on(&xprt->xpt_bc_pending, task, NULL);
2216 if (!mutex_trylock(&xprt->xpt_mutex))
2217 return -EAGAIN;
2218 rpc_wake_up_queued_task(&xprt->xpt_bc_pending, task);
2220 if (test_bit(XPT_DEAD, &xprt->xpt_flags))
2221 len = -ENOTCONN;
2222 else
2223 len = bc_sendto(req);
2224 mutex_unlock(&xprt->xpt_mutex);
2226 if (len > 0)
2227 len = 0;
2229 return len;
2233 * The close routine. Since this is client initiated, we do nothing
2236 static void bc_close(struct rpc_xprt *xprt)
2238 return;
2242 * The xprt destroy routine. Again, because this connection is client
2243 * initiated, we do nothing
2246 static void bc_destroy(struct rpc_xprt *xprt)
2248 return;
2251 static struct rpc_xprt_ops xs_udp_ops = {
2252 .set_buffer_size = xs_udp_set_buffer_size,
2253 .reserve_xprt = xprt_reserve_xprt_cong,
2254 .release_xprt = xprt_release_xprt_cong,
2255 .rpcbind = rpcb_getport_async,
2256 .set_port = xs_set_port,
2257 .connect = xs_connect,
2258 .buf_alloc = rpc_malloc,
2259 .buf_free = rpc_free,
2260 .send_request = xs_udp_send_request,
2261 .set_retrans_timeout = xprt_set_retrans_timeout_rtt,
2262 .timer = xs_udp_timer,
2263 .release_request = xprt_release_rqst_cong,
2264 .close = xs_close,
2265 .destroy = xs_destroy,
2266 .print_stats = xs_udp_print_stats,
2269 static struct rpc_xprt_ops xs_tcp_ops = {
2270 .reserve_xprt = xprt_reserve_xprt,
2271 .release_xprt = xs_tcp_release_xprt,
2272 .rpcbind = rpcb_getport_async,
2273 .set_port = xs_set_port,
2274 .connect = xs_tcp_connect,
2275 .buf_alloc = rpc_malloc,
2276 .buf_free = rpc_free,
2277 .send_request = xs_tcp_send_request,
2278 .set_retrans_timeout = xprt_set_retrans_timeout_def,
2279 #if defined(CONFIG_NFS_V4_1)
2280 .release_request = bc_release_request,
2281 #endif /* CONFIG_NFS_V4_1 */
2282 .close = xs_tcp_close,
2283 .destroy = xs_destroy,
2284 .print_stats = xs_tcp_print_stats,
2288 * The rpc_xprt_ops for the server backchannel
2291 static struct rpc_xprt_ops bc_tcp_ops = {
2292 .reserve_xprt = xprt_reserve_xprt,
2293 .release_xprt = xprt_release_xprt,
2294 .buf_alloc = bc_malloc,
2295 .buf_free = bc_free,
2296 .send_request = bc_send_request,
2297 .set_retrans_timeout = xprt_set_retrans_timeout_def,
2298 .close = bc_close,
2299 .destroy = bc_destroy,
2300 .print_stats = xs_tcp_print_stats,
2303 static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args,
2304 unsigned int slot_table_size)
2306 struct rpc_xprt *xprt;
2307 struct sock_xprt *new;
2309 if (args->addrlen > sizeof(xprt->addr)) {
2310 dprintk("RPC: xs_setup_xprt: address too large\n");
2311 return ERR_PTR(-EBADF);
2314 new = kzalloc(sizeof(*new), GFP_KERNEL);
2315 if (new == NULL) {
2316 dprintk("RPC: xs_setup_xprt: couldn't allocate "
2317 "rpc_xprt\n");
2318 return ERR_PTR(-ENOMEM);
2320 xprt = &new->xprt;
2322 xprt->max_reqs = slot_table_size;
2323 xprt->slot = kcalloc(xprt->max_reqs, sizeof(struct rpc_rqst), GFP_KERNEL);
2324 if (xprt->slot == NULL) {
2325 kfree(xprt);
2326 dprintk("RPC: xs_setup_xprt: couldn't allocate slot "
2327 "table\n");
2328 return ERR_PTR(-ENOMEM);
2331 memcpy(&xprt->addr, args->dstaddr, args->addrlen);
2332 xprt->addrlen = args->addrlen;
2333 if (args->srcaddr)
2334 memcpy(&new->srcaddr, args->srcaddr, args->addrlen);
2336 return xprt;
2339 static const struct rpc_timeout xs_udp_default_timeout = {
2340 .to_initval = 5 * HZ,
2341 .to_maxval = 30 * HZ,
2342 .to_increment = 5 * HZ,
2343 .to_retries = 5,
2347 * xs_setup_udp - Set up transport to use a UDP socket
2348 * @args: rpc transport creation arguments
2351 static struct rpc_xprt *xs_setup_udp(struct xprt_create *args)
2353 struct sockaddr *addr = args->dstaddr;
2354 struct rpc_xprt *xprt;
2355 struct sock_xprt *transport;
2357 xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries);
2358 if (IS_ERR(xprt))
2359 return xprt;
2360 transport = container_of(xprt, struct sock_xprt, xprt);
2362 xprt->prot = IPPROTO_UDP;
2363 xprt->tsh_size = 0;
2364 /* XXX: header size can vary due to auth type, IPv6, etc. */
2365 xprt->max_payload = (1U << 16) - (MAX_HEADER << 3);
2367 xprt->bind_timeout = XS_BIND_TO;
2368 xprt->connect_timeout = XS_UDP_CONN_TO;
2369 xprt->reestablish_timeout = XS_UDP_REEST_TO;
2370 xprt->idle_timeout = XS_IDLE_DISC_TO;
2372 xprt->ops = &xs_udp_ops;
2374 xprt->timeout = &xs_udp_default_timeout;
2376 switch (addr->sa_family) {
2377 case AF_INET:
2378 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
2379 xprt_set_bound(xprt);
2381 INIT_DELAYED_WORK(&transport->connect_worker,
2382 xs_udp_connect_worker4);
2383 xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP);
2384 break;
2385 case AF_INET6:
2386 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
2387 xprt_set_bound(xprt);
2389 INIT_DELAYED_WORK(&transport->connect_worker,
2390 xs_udp_connect_worker6);
2391 xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6);
2392 break;
2393 default:
2394 kfree(xprt);
2395 return ERR_PTR(-EAFNOSUPPORT);
2398 if (xprt_bound(xprt))
2399 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2400 xprt->address_strings[RPC_DISPLAY_ADDR],
2401 xprt->address_strings[RPC_DISPLAY_PORT],
2402 xprt->address_strings[RPC_DISPLAY_PROTO]);
2403 else
2404 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
2405 xprt->address_strings[RPC_DISPLAY_ADDR],
2406 xprt->address_strings[RPC_DISPLAY_PROTO]);
2408 if (try_module_get(THIS_MODULE))
2409 return xprt;
2411 kfree(xprt->slot);
2412 kfree(xprt);
2413 return ERR_PTR(-EINVAL);
2416 static const struct rpc_timeout xs_tcp_default_timeout = {
2417 .to_initval = 60 * HZ,
2418 .to_maxval = 60 * HZ,
2419 .to_retries = 2,
2423 * xs_setup_tcp - Set up transport to use a TCP socket
2424 * @args: rpc transport creation arguments
2427 static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args)
2429 struct sockaddr *addr = args->dstaddr;
2430 struct rpc_xprt *xprt;
2431 struct sock_xprt *transport;
2433 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries);
2434 if (IS_ERR(xprt))
2435 return xprt;
2436 transport = container_of(xprt, struct sock_xprt, xprt);
2438 xprt->prot = IPPROTO_TCP;
2439 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
2440 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2442 xprt->bind_timeout = XS_BIND_TO;
2443 xprt->connect_timeout = XS_TCP_CONN_TO;
2444 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2445 xprt->idle_timeout = XS_IDLE_DISC_TO;
2447 xprt->ops = &xs_tcp_ops;
2448 xprt->timeout = &xs_tcp_default_timeout;
2450 switch (addr->sa_family) {
2451 case AF_INET:
2452 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
2453 xprt_set_bound(xprt);
2455 INIT_DELAYED_WORK(&transport->connect_worker,
2456 xs_tcp_connect_worker4);
2457 xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP);
2458 break;
2459 case AF_INET6:
2460 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
2461 xprt_set_bound(xprt);
2463 INIT_DELAYED_WORK(&transport->connect_worker,
2464 xs_tcp_connect_worker6);
2465 xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6);
2466 break;
2467 default:
2468 kfree(xprt);
2469 return ERR_PTR(-EAFNOSUPPORT);
2472 if (xprt_bound(xprt))
2473 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2474 xprt->address_strings[RPC_DISPLAY_ADDR],
2475 xprt->address_strings[RPC_DISPLAY_PORT],
2476 xprt->address_strings[RPC_DISPLAY_PROTO]);
2477 else
2478 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
2479 xprt->address_strings[RPC_DISPLAY_ADDR],
2480 xprt->address_strings[RPC_DISPLAY_PROTO]);
2483 if (try_module_get(THIS_MODULE))
2484 return xprt;
2486 kfree(xprt->slot);
2487 kfree(xprt);
2488 return ERR_PTR(-EINVAL);
2492 * xs_setup_bc_tcp - Set up transport to use a TCP backchannel socket
2493 * @args: rpc transport creation arguments
2496 static struct rpc_xprt *xs_setup_bc_tcp(struct xprt_create *args)
2498 struct sockaddr *addr = args->dstaddr;
2499 struct rpc_xprt *xprt;
2500 struct sock_xprt *transport;
2501 struct svc_sock *bc_sock;
2503 if (!args->bc_xprt)
2504 ERR_PTR(-EINVAL);
2506 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries);
2507 if (IS_ERR(xprt))
2508 return xprt;
2509 transport = container_of(xprt, struct sock_xprt, xprt);
2511 xprt->prot = IPPROTO_TCP;
2512 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
2513 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2514 xprt->timeout = &xs_tcp_default_timeout;
2516 /* backchannel */
2517 xprt_set_bound(xprt);
2518 xprt->bind_timeout = 0;
2519 xprt->connect_timeout = 0;
2520 xprt->reestablish_timeout = 0;
2521 xprt->idle_timeout = 0;
2524 * The backchannel uses the same socket connection as the
2525 * forechannel
2527 xprt->bc_xprt = args->bc_xprt;
2528 bc_sock = container_of(args->bc_xprt, struct svc_sock, sk_xprt);
2529 bc_sock->sk_bc_xprt = xprt;
2530 transport->sock = bc_sock->sk_sock;
2531 transport->inet = bc_sock->sk_sk;
2533 xprt->ops = &bc_tcp_ops;
2535 switch (addr->sa_family) {
2536 case AF_INET:
2537 xs_format_peer_addresses(xprt, "tcp",
2538 RPCBIND_NETID_TCP);
2539 break;
2540 case AF_INET6:
2541 xs_format_peer_addresses(xprt, "tcp",
2542 RPCBIND_NETID_TCP6);
2543 break;
2544 default:
2545 kfree(xprt);
2546 return ERR_PTR(-EAFNOSUPPORT);
2549 if (xprt_bound(xprt))
2550 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2551 xprt->address_strings[RPC_DISPLAY_ADDR],
2552 xprt->address_strings[RPC_DISPLAY_PORT],
2553 xprt->address_strings[RPC_DISPLAY_PROTO]);
2554 else
2555 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
2556 xprt->address_strings[RPC_DISPLAY_ADDR],
2557 xprt->address_strings[RPC_DISPLAY_PROTO]);
2560 * Since we don't want connections for the backchannel, we set
2561 * the xprt status to connected
2563 xprt_set_connected(xprt);
2566 if (try_module_get(THIS_MODULE))
2567 return xprt;
2568 kfree(xprt->slot);
2569 kfree(xprt);
2570 return ERR_PTR(-EINVAL);
2573 static struct xprt_class xs_udp_transport = {
2574 .list = LIST_HEAD_INIT(xs_udp_transport.list),
2575 .name = "udp",
2576 .owner = THIS_MODULE,
2577 .ident = XPRT_TRANSPORT_UDP,
2578 .setup = xs_setup_udp,
2581 static struct xprt_class xs_tcp_transport = {
2582 .list = LIST_HEAD_INIT(xs_tcp_transport.list),
2583 .name = "tcp",
2584 .owner = THIS_MODULE,
2585 .ident = XPRT_TRANSPORT_TCP,
2586 .setup = xs_setup_tcp,
2589 static struct xprt_class xs_bc_tcp_transport = {
2590 .list = LIST_HEAD_INIT(xs_bc_tcp_transport.list),
2591 .name = "tcp NFSv4.1 backchannel",
2592 .owner = THIS_MODULE,
2593 .ident = XPRT_TRANSPORT_BC_TCP,
2594 .setup = xs_setup_bc_tcp,
2598 * init_socket_xprt - set up xprtsock's sysctls, register with RPC client
2601 int init_socket_xprt(void)
2603 #ifdef RPC_DEBUG
2604 if (!sunrpc_table_header)
2605 sunrpc_table_header = register_sysctl_table(sunrpc_table);
2606 #endif
2608 xprt_register_transport(&xs_udp_transport);
2609 xprt_register_transport(&xs_tcp_transport);
2610 xprt_register_transport(&xs_bc_tcp_transport);
2612 return 0;
2616 * cleanup_socket_xprt - remove xprtsock's sysctls, unregister
2619 void cleanup_socket_xprt(void)
2621 #ifdef RPC_DEBUG
2622 if (sunrpc_table_header) {
2623 unregister_sysctl_table(sunrpc_table_header);
2624 sunrpc_table_header = NULL;
2626 #endif
2628 xprt_unregister_transport(&xs_udp_transport);
2629 xprt_unregister_transport(&xs_tcp_transport);
2630 xprt_unregister_transport(&xs_bc_tcp_transport);
2633 static int param_set_uint_minmax(const char *val, struct kernel_param *kp,
2634 unsigned int min, unsigned int max)
2636 unsigned long num;
2637 int ret;
2639 if (!val)
2640 return -EINVAL;
2641 ret = strict_strtoul(val, 0, &num);
2642 if (ret == -EINVAL || num < min || num > max)
2643 return -EINVAL;
2644 *((unsigned int *)kp->arg) = num;
2645 return 0;
2648 static int param_set_portnr(const char *val, struct kernel_param *kp)
2650 return param_set_uint_minmax(val, kp,
2651 RPC_MIN_RESVPORT,
2652 RPC_MAX_RESVPORT);
2655 static int param_get_portnr(char *buffer, struct kernel_param *kp)
2657 return param_get_uint(buffer, kp);
2659 #define param_check_portnr(name, p) \
2660 __param_check(name, p, unsigned int);
2662 module_param_named(min_resvport, xprt_min_resvport, portnr, 0644);
2663 module_param_named(max_resvport, xprt_max_resvport, portnr, 0644);
2665 static int param_set_slot_table_size(const char *val, struct kernel_param *kp)
2667 return param_set_uint_minmax(val, kp,
2668 RPC_MIN_SLOT_TABLE,
2669 RPC_MAX_SLOT_TABLE);
2672 static int param_get_slot_table_size(char *buffer, struct kernel_param *kp)
2674 return param_get_uint(buffer, kp);
2676 #define param_check_slot_table_size(name, p) \
2677 __param_check(name, p, unsigned int);
2679 module_param_named(tcp_slot_table_entries, xprt_tcp_slot_table_entries,
2680 slot_table_size, 0644);
2681 module_param_named(udp_slot_table_entries, xprt_udp_slot_table_entries,
2682 slot_table_size, 0644);