KEYS: add missing permission check for request_key() destination
[linux/fpc-iii.git] / net / sunrpc / xprtsock.c
blobab52cefa8f39bb4fd4be8b70170a85835f6ca386
1 /*
2 * linux/net/sunrpc/xprtsock.c
4 * Client-side transport implementation for sockets.
6 * TCP callback races fixes (C) 1998 Red Hat
7 * TCP send fixes (C) 1998 Red Hat
8 * TCP NFS related read + write fixes
9 * (C) 1999 Dave Airlie, University of Limerick, Ireland <airlied@linux.ie>
11 * Rewrite of larges part of the code in order to stabilize TCP stuff.
12 * Fix behaviour when socket buffer is full.
13 * (C) 1999 Trond Myklebust <trond.myklebust@fys.uio.no>
15 * IP socket transport implementation, (C) 2005 Chuck Lever <cel@netapp.com>
17 * IPv6 support contributed by Gilles Quillard, Bull Open Source, 2005.
18 * <gilles.quillard@bull.net>
21 #include <linux/types.h>
22 #include <linux/string.h>
23 #include <linux/slab.h>
24 #include <linux/module.h>
25 #include <linux/capability.h>
26 #include <linux/pagemap.h>
27 #include <linux/errno.h>
28 #include <linux/socket.h>
29 #include <linux/in.h>
30 #include <linux/net.h>
31 #include <linux/mm.h>
32 #include <linux/un.h>
33 #include <linux/udp.h>
34 #include <linux/tcp.h>
35 #include <linux/sunrpc/clnt.h>
36 #include <linux/sunrpc/addr.h>
37 #include <linux/sunrpc/sched.h>
38 #include <linux/sunrpc/svcsock.h>
39 #include <linux/sunrpc/xprtsock.h>
40 #include <linux/file.h>
41 #ifdef CONFIG_SUNRPC_BACKCHANNEL
42 #include <linux/sunrpc/bc_xprt.h>
43 #endif
45 #include <net/sock.h>
46 #include <net/checksum.h>
47 #include <net/udp.h>
48 #include <net/tcp.h>
50 #include <trace/events/sunrpc.h>
52 #include "sunrpc.h"
54 static void xs_close(struct rpc_xprt *xprt);
57 * xprtsock tunables
59 static unsigned int xprt_udp_slot_table_entries = RPC_DEF_SLOT_TABLE;
60 static unsigned int xprt_tcp_slot_table_entries = RPC_MIN_SLOT_TABLE;
61 static unsigned int xprt_max_tcp_slot_table_entries = RPC_MAX_SLOT_TABLE;
63 static unsigned int xprt_min_resvport = RPC_DEF_MIN_RESVPORT;
64 static unsigned int xprt_max_resvport = RPC_DEF_MAX_RESVPORT;
66 #define XS_TCP_LINGER_TO (15U * HZ)
67 static unsigned int xs_tcp_fin_timeout __read_mostly = XS_TCP_LINGER_TO;
70 * We can register our own files under /proc/sys/sunrpc by
71 * calling register_sysctl_table() again. The files in that
72 * directory become the union of all files registered there.
74 * We simply need to make sure that we don't collide with
75 * someone else's file names!
78 #ifdef RPC_DEBUG
80 static unsigned int min_slot_table_size = RPC_MIN_SLOT_TABLE;
81 static unsigned int max_slot_table_size = RPC_MAX_SLOT_TABLE;
82 static unsigned int max_tcp_slot_table_limit = RPC_MAX_SLOT_TABLE_LIMIT;
83 static unsigned int xprt_min_resvport_limit = RPC_MIN_RESVPORT;
84 static unsigned int xprt_max_resvport_limit = RPC_MAX_RESVPORT;
86 static struct ctl_table_header *sunrpc_table_header;
89 * FIXME: changing the UDP slot table size should also resize the UDP
90 * socket buffers for existing UDP transports
92 static struct ctl_table xs_tunables_table[] = {
94 .procname = "udp_slot_table_entries",
95 .data = &xprt_udp_slot_table_entries,
96 .maxlen = sizeof(unsigned int),
97 .mode = 0644,
98 .proc_handler = proc_dointvec_minmax,
99 .extra1 = &min_slot_table_size,
100 .extra2 = &max_slot_table_size
103 .procname = "tcp_slot_table_entries",
104 .data = &xprt_tcp_slot_table_entries,
105 .maxlen = sizeof(unsigned int),
106 .mode = 0644,
107 .proc_handler = proc_dointvec_minmax,
108 .extra1 = &min_slot_table_size,
109 .extra2 = &max_slot_table_size
112 .procname = "tcp_max_slot_table_entries",
113 .data = &xprt_max_tcp_slot_table_entries,
114 .maxlen = sizeof(unsigned int),
115 .mode = 0644,
116 .proc_handler = proc_dointvec_minmax,
117 .extra1 = &min_slot_table_size,
118 .extra2 = &max_tcp_slot_table_limit
121 .procname = "min_resvport",
122 .data = &xprt_min_resvport,
123 .maxlen = sizeof(unsigned int),
124 .mode = 0644,
125 .proc_handler = proc_dointvec_minmax,
126 .extra1 = &xprt_min_resvport_limit,
127 .extra2 = &xprt_max_resvport_limit
130 .procname = "max_resvport",
131 .data = &xprt_max_resvport,
132 .maxlen = sizeof(unsigned int),
133 .mode = 0644,
134 .proc_handler = proc_dointvec_minmax,
135 .extra1 = &xprt_min_resvport_limit,
136 .extra2 = &xprt_max_resvport_limit
139 .procname = "tcp_fin_timeout",
140 .data = &xs_tcp_fin_timeout,
141 .maxlen = sizeof(xs_tcp_fin_timeout),
142 .mode = 0644,
143 .proc_handler = proc_dointvec_jiffies,
145 { },
148 static struct ctl_table sunrpc_table[] = {
150 .procname = "sunrpc",
151 .mode = 0555,
152 .child = xs_tunables_table
154 { },
157 #endif
160 * Wait duration for a reply from the RPC portmapper.
162 #define XS_BIND_TO (60U * HZ)
165 * Delay if a UDP socket connect error occurs. This is most likely some
166 * kind of resource problem on the local host.
168 #define XS_UDP_REEST_TO (2U * HZ)
171 * The reestablish timeout allows clients to delay for a bit before attempting
172 * to reconnect to a server that just dropped our connection.
174 * We implement an exponential backoff when trying to reestablish a TCP
175 * transport connection with the server. Some servers like to drop a TCP
176 * connection when they are overworked, so we start with a short timeout and
177 * increase over time if the server is down or not responding.
179 #define XS_TCP_INIT_REEST_TO (3U * HZ)
180 #define XS_TCP_MAX_REEST_TO (5U * 60 * HZ)
183 * TCP idle timeout; client drops the transport socket if it is idle
184 * for this long. Note that we also timeout UDP sockets to prevent
185 * holding port numbers when there is no RPC traffic.
187 #define XS_IDLE_DISC_TO (5U * 60 * HZ)
189 #ifdef RPC_DEBUG
190 # undef RPC_DEBUG_DATA
191 # define RPCDBG_FACILITY RPCDBG_TRANS
192 #endif
194 #ifdef RPC_DEBUG_DATA
195 static void xs_pktdump(char *msg, u32 *packet, unsigned int count)
197 u8 *buf = (u8 *) packet;
198 int j;
200 dprintk("RPC: %s\n", msg);
201 for (j = 0; j < count && j < 128; j += 4) {
202 if (!(j & 31)) {
203 if (j)
204 dprintk("\n");
205 dprintk("0x%04x ", j);
207 dprintk("%02x%02x%02x%02x ",
208 buf[j], buf[j+1], buf[j+2], buf[j+3]);
210 dprintk("\n");
212 #else
213 static inline void xs_pktdump(char *msg, u32 *packet, unsigned int count)
215 /* NOP */
217 #endif
219 struct sock_xprt {
220 struct rpc_xprt xprt;
223 * Network layer
225 struct socket * sock;
226 struct sock * inet;
229 * State of TCP reply receive
231 __be32 tcp_fraghdr,
232 tcp_xid,
233 tcp_calldir;
235 u32 tcp_offset,
236 tcp_reclen;
238 unsigned long tcp_copied,
239 tcp_flags;
242 * Connection of transports
244 struct delayed_work connect_worker;
245 struct sockaddr_storage srcaddr;
246 unsigned short srcport;
249 * UDP socket buffer size parameters
251 size_t rcvsize,
252 sndsize;
255 * Saved socket callback addresses
257 void (*old_data_ready)(struct sock *);
258 void (*old_state_change)(struct sock *);
259 void (*old_write_space)(struct sock *);
260 void (*old_error_report)(struct sock *);
264 * TCP receive state flags
266 #define TCP_RCV_LAST_FRAG (1UL << 0)
267 #define TCP_RCV_COPY_FRAGHDR (1UL << 1)
268 #define TCP_RCV_COPY_XID (1UL << 2)
269 #define TCP_RCV_COPY_DATA (1UL << 3)
270 #define TCP_RCV_READ_CALLDIR (1UL << 4)
271 #define TCP_RCV_COPY_CALLDIR (1UL << 5)
274 * TCP RPC flags
276 #define TCP_RPC_REPLY (1UL << 6)
278 static inline struct rpc_xprt *xprt_from_sock(struct sock *sk)
280 return (struct rpc_xprt *) sk->sk_user_data;
283 static inline struct sockaddr *xs_addr(struct rpc_xprt *xprt)
285 return (struct sockaddr *) &xprt->addr;
288 static inline struct sockaddr_un *xs_addr_un(struct rpc_xprt *xprt)
290 return (struct sockaddr_un *) &xprt->addr;
293 static inline struct sockaddr_in *xs_addr_in(struct rpc_xprt *xprt)
295 return (struct sockaddr_in *) &xprt->addr;
298 static inline struct sockaddr_in6 *xs_addr_in6(struct rpc_xprt *xprt)
300 return (struct sockaddr_in6 *) &xprt->addr;
303 static void xs_format_common_peer_addresses(struct rpc_xprt *xprt)
305 struct sockaddr *sap = xs_addr(xprt);
306 struct sockaddr_in6 *sin6;
307 struct sockaddr_in *sin;
308 struct sockaddr_un *sun;
309 char buf[128];
311 switch (sap->sa_family) {
312 case AF_LOCAL:
313 sun = xs_addr_un(xprt);
314 strlcpy(buf, sun->sun_path, sizeof(buf));
315 xprt->address_strings[RPC_DISPLAY_ADDR] =
316 kstrdup(buf, GFP_KERNEL);
317 break;
318 case AF_INET:
319 (void)rpc_ntop(sap, buf, sizeof(buf));
320 xprt->address_strings[RPC_DISPLAY_ADDR] =
321 kstrdup(buf, GFP_KERNEL);
322 sin = xs_addr_in(xprt);
323 snprintf(buf, sizeof(buf), "%08x", ntohl(sin->sin_addr.s_addr));
324 break;
325 case AF_INET6:
326 (void)rpc_ntop(sap, buf, sizeof(buf));
327 xprt->address_strings[RPC_DISPLAY_ADDR] =
328 kstrdup(buf, GFP_KERNEL);
329 sin6 = xs_addr_in6(xprt);
330 snprintf(buf, sizeof(buf), "%pi6", &sin6->sin6_addr);
331 break;
332 default:
333 BUG();
336 xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = kstrdup(buf, GFP_KERNEL);
339 static void xs_format_common_peer_ports(struct rpc_xprt *xprt)
341 struct sockaddr *sap = xs_addr(xprt);
342 char buf[128];
344 snprintf(buf, sizeof(buf), "%u", rpc_get_port(sap));
345 xprt->address_strings[RPC_DISPLAY_PORT] = kstrdup(buf, GFP_KERNEL);
347 snprintf(buf, sizeof(buf), "%4hx", rpc_get_port(sap));
348 xprt->address_strings[RPC_DISPLAY_HEX_PORT] = kstrdup(buf, GFP_KERNEL);
351 static void xs_format_peer_addresses(struct rpc_xprt *xprt,
352 const char *protocol,
353 const char *netid)
355 xprt->address_strings[RPC_DISPLAY_PROTO] = protocol;
356 xprt->address_strings[RPC_DISPLAY_NETID] = netid;
357 xs_format_common_peer_addresses(xprt);
358 xs_format_common_peer_ports(xprt);
361 static void xs_update_peer_port(struct rpc_xprt *xprt)
363 kfree(xprt->address_strings[RPC_DISPLAY_HEX_PORT]);
364 kfree(xprt->address_strings[RPC_DISPLAY_PORT]);
366 xs_format_common_peer_ports(xprt);
369 static void xs_free_peer_addresses(struct rpc_xprt *xprt)
371 unsigned int i;
373 for (i = 0; i < RPC_DISPLAY_MAX; i++)
374 switch (i) {
375 case RPC_DISPLAY_PROTO:
376 case RPC_DISPLAY_NETID:
377 continue;
378 default:
379 kfree(xprt->address_strings[i]);
383 #define XS_SENDMSG_FLAGS (MSG_DONTWAIT | MSG_NOSIGNAL)
385 static int xs_send_kvec(struct socket *sock, struct sockaddr *addr, int addrlen, struct kvec *vec, unsigned int base, int more)
387 struct msghdr msg = {
388 .msg_name = addr,
389 .msg_namelen = addrlen,
390 .msg_flags = XS_SENDMSG_FLAGS | (more ? MSG_MORE : 0),
392 struct kvec iov = {
393 .iov_base = vec->iov_base + base,
394 .iov_len = vec->iov_len - base,
397 if (iov.iov_len != 0)
398 return kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len);
399 return kernel_sendmsg(sock, &msg, NULL, 0, 0);
402 static int xs_send_pagedata(struct socket *sock, struct xdr_buf *xdr, unsigned int base, int more, bool zerocopy)
404 ssize_t (*do_sendpage)(struct socket *sock, struct page *page,
405 int offset, size_t size, int flags);
406 struct page **ppage;
407 unsigned int remainder;
408 int err, sent = 0;
410 remainder = xdr->page_len - base;
411 base += xdr->page_base;
412 ppage = xdr->pages + (base >> PAGE_SHIFT);
413 base &= ~PAGE_MASK;
414 do_sendpage = sock->ops->sendpage;
415 if (!zerocopy)
416 do_sendpage = sock_no_sendpage;
417 for(;;) {
418 unsigned int len = min_t(unsigned int, PAGE_SIZE - base, remainder);
419 int flags = XS_SENDMSG_FLAGS;
421 remainder -= len;
422 if (remainder != 0 || more)
423 flags |= MSG_MORE;
424 err = do_sendpage(sock, *ppage, base, len, flags);
425 if (remainder == 0 || err != len)
426 break;
427 sent += err;
428 ppage++;
429 base = 0;
431 if (sent == 0)
432 return err;
433 if (err > 0)
434 sent += err;
435 return sent;
439 * xs_sendpages - write pages directly to a socket
440 * @sock: socket to send on
441 * @addr: UDP only -- address of destination
442 * @addrlen: UDP only -- length of destination address
443 * @xdr: buffer containing this request
444 * @base: starting position in the buffer
445 * @zerocopy: true if it is safe to use sendpage()
448 static int xs_sendpages(struct socket *sock, struct sockaddr *addr, int addrlen, struct xdr_buf *xdr, unsigned int base, bool zerocopy)
450 unsigned int remainder = xdr->len - base;
451 int err, sent = 0;
453 if (unlikely(!sock))
454 return -ENOTSOCK;
456 clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags);
457 if (base != 0) {
458 addr = NULL;
459 addrlen = 0;
462 if (base < xdr->head[0].iov_len || addr != NULL) {
463 unsigned int len = xdr->head[0].iov_len - base;
464 remainder -= len;
465 err = xs_send_kvec(sock, addr, addrlen, &xdr->head[0], base, remainder != 0);
466 if (remainder == 0 || err != len)
467 goto out;
468 sent += err;
469 base = 0;
470 } else
471 base -= xdr->head[0].iov_len;
473 if (base < xdr->page_len) {
474 unsigned int len = xdr->page_len - base;
475 remainder -= len;
476 err = xs_send_pagedata(sock, xdr, base, remainder != 0, zerocopy);
477 if (remainder == 0 || err != len)
478 goto out;
479 sent += err;
480 base = 0;
481 } else
482 base -= xdr->page_len;
484 if (base >= xdr->tail[0].iov_len)
485 return sent;
486 err = xs_send_kvec(sock, NULL, 0, &xdr->tail[0], base, 0);
487 out:
488 if (sent == 0)
489 return err;
490 if (err > 0)
491 sent += err;
492 return sent;
495 static void xs_nospace_callback(struct rpc_task *task)
497 struct sock_xprt *transport = container_of(task->tk_rqstp->rq_xprt, struct sock_xprt, xprt);
499 transport->inet->sk_write_pending--;
500 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
504 * xs_nospace - place task on wait queue if transmit was incomplete
505 * @task: task to put to sleep
508 static int xs_nospace(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 sock *sk = transport->inet;
514 int ret = -EAGAIN;
516 dprintk("RPC: %5u xmit incomplete (%u left of %u)\n",
517 task->tk_pid, req->rq_slen - req->rq_bytes_sent,
518 req->rq_slen);
520 /* Protect against races with write_space */
521 spin_lock_bh(&xprt->transport_lock);
523 /* Don't race with disconnect */
524 if (xprt_connected(xprt)) {
525 if (test_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags)) {
527 * Notify TCP that we're limited by the application
528 * window size
530 set_bit(SOCK_NOSPACE, &transport->sock->flags);
531 sk->sk_write_pending++;
532 /* ...and wait for more buffer space */
533 xprt_wait_for_buffer_space(task, xs_nospace_callback);
535 } else {
536 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
537 ret = -ENOTCONN;
540 spin_unlock_bh(&xprt->transport_lock);
542 /* Race breaker in case memory is freed before above code is called */
543 sk->sk_write_space(sk);
544 return ret;
548 * Construct a stream transport record marker in @buf.
550 static inline void xs_encode_stream_record_marker(struct xdr_buf *buf)
552 u32 reclen = buf->len - sizeof(rpc_fraghdr);
553 rpc_fraghdr *base = buf->head[0].iov_base;
554 *base = cpu_to_be32(RPC_LAST_STREAM_FRAGMENT | reclen);
558 * xs_local_send_request - write an RPC request to an AF_LOCAL socket
559 * @task: RPC task that manages the state of an RPC request
561 * Return values:
562 * 0: The request has been sent
563 * EAGAIN: The socket was blocked, please call again later to
564 * complete the request
565 * ENOTCONN: Caller needs to invoke connect logic then call again
566 * other: Some other error occured, the request was not sent
568 static int xs_local_send_request(struct rpc_task *task)
570 struct rpc_rqst *req = task->tk_rqstp;
571 struct rpc_xprt *xprt = req->rq_xprt;
572 struct sock_xprt *transport =
573 container_of(xprt, struct sock_xprt, xprt);
574 struct xdr_buf *xdr = &req->rq_snd_buf;
575 int status;
577 xs_encode_stream_record_marker(&req->rq_snd_buf);
579 xs_pktdump("packet data:",
580 req->rq_svec->iov_base, req->rq_svec->iov_len);
582 status = xs_sendpages(transport->sock, NULL, 0,
583 xdr, req->rq_bytes_sent, true);
584 dprintk("RPC: %s(%u) = %d\n",
585 __func__, xdr->len - req->rq_bytes_sent, status);
586 if (likely(status >= 0)) {
587 req->rq_bytes_sent += status;
588 req->rq_xmit_bytes_sent += status;
589 if (likely(req->rq_bytes_sent >= req->rq_slen)) {
590 req->rq_bytes_sent = 0;
591 return 0;
593 status = -EAGAIN;
596 switch (status) {
597 case -EAGAIN:
598 status = xs_nospace(task);
599 break;
600 default:
601 dprintk("RPC: sendmsg returned unrecognized error %d\n",
602 -status);
603 case -EPIPE:
604 xs_close(xprt);
605 status = -ENOTCONN;
608 return status;
612 * xs_udp_send_request - write an RPC request to a UDP socket
613 * @task: address of RPC task that manages the state of an RPC request
615 * Return values:
616 * 0: The request has been sent
617 * EAGAIN: The socket was blocked, please call again later to
618 * complete the request
619 * ENOTCONN: Caller needs to invoke connect logic then call again
620 * other: Some other error occurred, the request was not sent
622 static int xs_udp_send_request(struct rpc_task *task)
624 struct rpc_rqst *req = task->tk_rqstp;
625 struct rpc_xprt *xprt = req->rq_xprt;
626 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
627 struct xdr_buf *xdr = &req->rq_snd_buf;
628 int status;
630 xs_pktdump("packet data:",
631 req->rq_svec->iov_base,
632 req->rq_svec->iov_len);
634 if (!xprt_bound(xprt))
635 return -ENOTCONN;
636 status = xs_sendpages(transport->sock,
637 xs_addr(xprt),
638 xprt->addrlen, xdr,
639 req->rq_bytes_sent, true);
641 dprintk("RPC: xs_udp_send_request(%u) = %d\n",
642 xdr->len - req->rq_bytes_sent, status);
644 if (status >= 0) {
645 req->rq_xmit_bytes_sent += status;
646 if (status >= req->rq_slen)
647 return 0;
648 /* Still some bytes left; set up for a retry later. */
649 status = -EAGAIN;
652 switch (status) {
653 case -ENOTSOCK:
654 status = -ENOTCONN;
655 /* Should we call xs_close() here? */
656 break;
657 case -EAGAIN:
658 status = xs_nospace(task);
659 break;
660 default:
661 dprintk("RPC: sendmsg returned unrecognized error %d\n",
662 -status);
663 case -ENETUNREACH:
664 case -EPIPE:
665 case -ECONNREFUSED:
666 /* When the server has died, an ICMP port unreachable message
667 * prompts ECONNREFUSED. */
668 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
671 return status;
675 * xs_tcp_shutdown - gracefully shut down a TCP socket
676 * @xprt: transport
678 * Initiates a graceful shutdown of the TCP socket by calling the
679 * equivalent of shutdown(SHUT_WR);
681 static void xs_tcp_shutdown(struct rpc_xprt *xprt)
683 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
684 struct socket *sock = transport->sock;
686 if (sock != NULL) {
687 kernel_sock_shutdown(sock, SHUT_WR);
688 trace_rpc_socket_shutdown(xprt, sock);
693 * xs_tcp_send_request - write an RPC request to a TCP socket
694 * @task: address of RPC task that manages the state of an RPC request
696 * Return values:
697 * 0: The request has been sent
698 * EAGAIN: The socket was blocked, please call again later to
699 * complete the request
700 * ENOTCONN: Caller needs to invoke connect logic then call again
701 * other: Some other error occurred, the request was not sent
703 * XXX: In the case of soft timeouts, should we eventually give up
704 * if sendmsg is not able to make progress?
706 static int xs_tcp_send_request(struct rpc_task *task)
708 struct rpc_rqst *req = task->tk_rqstp;
709 struct rpc_xprt *xprt = req->rq_xprt;
710 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
711 struct xdr_buf *xdr = &req->rq_snd_buf;
712 bool zerocopy = true;
713 int status;
715 xs_encode_stream_record_marker(&req->rq_snd_buf);
717 xs_pktdump("packet data:",
718 req->rq_svec->iov_base,
719 req->rq_svec->iov_len);
720 /* Don't use zero copy if this is a resend. If the RPC call
721 * completes while the socket holds a reference to the pages,
722 * then we may end up resending corrupted data.
724 if (task->tk_flags & RPC_TASK_SENT)
725 zerocopy = false;
727 /* Continue transmitting the packet/record. We must be careful
728 * to cope with writespace callbacks arriving _after_ we have
729 * called sendmsg(). */
730 while (1) {
731 status = xs_sendpages(transport->sock,
732 NULL, 0, xdr, req->rq_bytes_sent,
733 zerocopy);
735 dprintk("RPC: xs_tcp_send_request(%u) = %d\n",
736 xdr->len - req->rq_bytes_sent, status);
738 if (unlikely(status < 0))
739 break;
741 /* If we've sent the entire packet, immediately
742 * reset the count of bytes sent. */
743 req->rq_bytes_sent += status;
744 req->rq_xmit_bytes_sent += status;
745 if (likely(req->rq_bytes_sent >= req->rq_slen)) {
746 req->rq_bytes_sent = 0;
747 return 0;
750 if (status != 0)
751 continue;
752 status = -EAGAIN;
753 break;
756 switch (status) {
757 case -ENOTSOCK:
758 status = -ENOTCONN;
759 /* Should we call xs_close() here? */
760 break;
761 case -EAGAIN:
762 status = xs_nospace(task);
763 break;
764 default:
765 dprintk("RPC: sendmsg returned unrecognized error %d\n",
766 -status);
767 case -ECONNRESET:
768 xs_tcp_shutdown(xprt);
769 case -ECONNREFUSED:
770 case -ENOTCONN:
771 case -EPIPE:
772 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
775 return status;
779 * xs_tcp_release_xprt - clean up after a tcp transmission
780 * @xprt: transport
781 * @task: rpc task
783 * This cleans up if an error causes us to abort the transmission of a request.
784 * In this case, the socket may need to be reset in order to avoid confusing
785 * the server.
787 static void xs_tcp_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
789 struct rpc_rqst *req;
791 if (task != xprt->snd_task)
792 return;
793 if (task == NULL)
794 goto out_release;
795 req = task->tk_rqstp;
796 if (req == NULL)
797 goto out_release;
798 if (req->rq_bytes_sent == 0)
799 goto out_release;
800 if (req->rq_bytes_sent == req->rq_snd_buf.len)
801 goto out_release;
802 set_bit(XPRT_CLOSE_WAIT, &xprt->state);
803 out_release:
804 xprt_release_xprt(xprt, task);
807 static void xs_save_old_callbacks(struct sock_xprt *transport, struct sock *sk)
809 transport->old_data_ready = sk->sk_data_ready;
810 transport->old_state_change = sk->sk_state_change;
811 transport->old_write_space = sk->sk_write_space;
812 transport->old_error_report = sk->sk_error_report;
815 static void xs_restore_old_callbacks(struct sock_xprt *transport, struct sock *sk)
817 sk->sk_data_ready = transport->old_data_ready;
818 sk->sk_state_change = transport->old_state_change;
819 sk->sk_write_space = transport->old_write_space;
820 sk->sk_error_report = transport->old_error_report;
824 * xs_error_report - callback to handle TCP socket state errors
825 * @sk: socket
827 * Note: we don't call sock_error() since there may be a rpc_task
828 * using the socket, and so we don't want to clear sk->sk_err.
830 static void xs_error_report(struct sock *sk)
832 struct rpc_xprt *xprt;
833 int err;
835 read_lock_bh(&sk->sk_callback_lock);
836 if (!(xprt = xprt_from_sock(sk)))
837 goto out;
839 err = -sk->sk_err;
840 if (err == 0)
841 goto out;
842 dprintk("RPC: xs_error_report client %p, error=%d...\n",
843 xprt, -err);
844 trace_rpc_socket_error(xprt, sk->sk_socket, err);
845 if (test_bit(XPRT_CONNECTION_REUSE, &xprt->state))
846 goto out;
847 xprt_wake_pending_tasks(xprt, err);
848 out:
849 read_unlock_bh(&sk->sk_callback_lock);
852 static void xs_reset_transport(struct sock_xprt *transport)
854 struct socket *sock = transport->sock;
855 struct sock *sk = transport->inet;
857 if (sk == NULL)
858 return;
860 transport->srcport = 0;
862 write_lock_bh(&sk->sk_callback_lock);
863 transport->inet = NULL;
864 transport->sock = NULL;
866 sk->sk_user_data = NULL;
868 xs_restore_old_callbacks(transport, sk);
869 write_unlock_bh(&sk->sk_callback_lock);
871 trace_rpc_socket_close(&transport->xprt, sock);
872 sock_release(sock);
876 * xs_close - close a socket
877 * @xprt: transport
879 * This is used when all requests are complete; ie, no DRC state remains
880 * on the server we want to save.
882 * The caller _must_ be holding XPRT_LOCKED in order to avoid issues with
883 * xs_reset_transport() zeroing the socket from underneath a writer.
885 static void xs_close(struct rpc_xprt *xprt)
887 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
889 dprintk("RPC: xs_close xprt %p\n", xprt);
891 cancel_delayed_work_sync(&transport->connect_worker);
893 xs_reset_transport(transport);
894 xprt->reestablish_timeout = 0;
896 smp_mb__before_atomic();
897 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
898 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
899 clear_bit(XPRT_CLOSING, &xprt->state);
900 smp_mb__after_atomic();
901 xprt_disconnect_done(xprt);
904 static void xs_tcp_close(struct rpc_xprt *xprt)
906 if (test_and_clear_bit(XPRT_CONNECTION_CLOSE, &xprt->state))
907 xs_close(xprt);
908 else
909 xs_tcp_shutdown(xprt);
912 static void xs_xprt_free(struct rpc_xprt *xprt)
914 xs_free_peer_addresses(xprt);
915 xprt_free(xprt);
919 * xs_destroy - prepare to shutdown a transport
920 * @xprt: doomed transport
923 static void xs_destroy(struct rpc_xprt *xprt)
925 dprintk("RPC: xs_destroy xprt %p\n", xprt);
927 xs_close(xprt);
928 xs_xprt_free(xprt);
929 module_put(THIS_MODULE);
932 static int xs_local_copy_to_xdr(struct xdr_buf *xdr, struct sk_buff *skb)
934 struct xdr_skb_reader desc = {
935 .skb = skb,
936 .offset = sizeof(rpc_fraghdr),
937 .count = skb->len - sizeof(rpc_fraghdr),
940 if (xdr_partial_copy_from_skb(xdr, 0, &desc, xdr_skb_read_bits) < 0)
941 return -1;
942 if (desc.count)
943 return -1;
944 return 0;
948 * xs_local_data_ready - "data ready" callback for AF_LOCAL sockets
949 * @sk: socket with data to read
950 * @len: how much data to read
952 * Currently this assumes we can read the whole reply in a single gulp.
954 static void xs_local_data_ready(struct sock *sk)
956 struct rpc_task *task;
957 struct rpc_xprt *xprt;
958 struct rpc_rqst *rovr;
959 struct sk_buff *skb;
960 int err, repsize, copied;
961 u32 _xid;
962 __be32 *xp;
964 read_lock_bh(&sk->sk_callback_lock);
965 dprintk("RPC: %s...\n", __func__);
966 xprt = xprt_from_sock(sk);
967 if (xprt == NULL)
968 goto out;
970 skb = skb_recv_datagram(sk, 0, 1, &err);
971 if (skb == NULL)
972 goto out;
974 repsize = skb->len - sizeof(rpc_fraghdr);
975 if (repsize < 4) {
976 dprintk("RPC: impossible RPC reply size %d\n", repsize);
977 goto dropit;
980 /* Copy the XID from the skb... */
981 xp = skb_header_pointer(skb, sizeof(rpc_fraghdr), sizeof(_xid), &_xid);
982 if (xp == NULL)
983 goto dropit;
985 /* Look up and lock the request corresponding to the given XID */
986 spin_lock(&xprt->transport_lock);
987 rovr = xprt_lookup_rqst(xprt, *xp);
988 if (!rovr)
989 goto out_unlock;
990 task = rovr->rq_task;
992 copied = rovr->rq_private_buf.buflen;
993 if (copied > repsize)
994 copied = repsize;
996 if (xs_local_copy_to_xdr(&rovr->rq_private_buf, skb)) {
997 dprintk("RPC: sk_buff copy failed\n");
998 goto out_unlock;
1001 xprt_complete_rqst(task, copied);
1003 out_unlock:
1004 spin_unlock(&xprt->transport_lock);
1005 dropit:
1006 skb_free_datagram(sk, skb);
1007 out:
1008 read_unlock_bh(&sk->sk_callback_lock);
1012 * xs_udp_data_ready - "data ready" callback for UDP sockets
1013 * @sk: socket with data to read
1014 * @len: how much data to read
1017 static void xs_udp_data_ready(struct sock *sk)
1019 struct rpc_task *task;
1020 struct rpc_xprt *xprt;
1021 struct rpc_rqst *rovr;
1022 struct sk_buff *skb;
1023 int err, repsize, copied;
1024 u32 _xid;
1025 __be32 *xp;
1027 read_lock_bh(&sk->sk_callback_lock);
1028 dprintk("RPC: xs_udp_data_ready...\n");
1029 if (!(xprt = xprt_from_sock(sk)))
1030 goto out;
1032 if ((skb = skb_recv_datagram(sk, 0, 1, &err)) == NULL)
1033 goto out;
1035 repsize = skb->len - sizeof(struct udphdr);
1036 if (repsize < 4) {
1037 dprintk("RPC: impossible RPC reply size %d!\n", repsize);
1038 goto dropit;
1041 /* Copy the XID from the skb... */
1042 xp = skb_header_pointer(skb, sizeof(struct udphdr),
1043 sizeof(_xid), &_xid);
1044 if (xp == NULL)
1045 goto dropit;
1047 /* Look up and lock the request corresponding to the given XID */
1048 spin_lock(&xprt->transport_lock);
1049 rovr = xprt_lookup_rqst(xprt, *xp);
1050 if (!rovr)
1051 goto out_unlock;
1052 task = rovr->rq_task;
1054 if ((copied = rovr->rq_private_buf.buflen) > repsize)
1055 copied = repsize;
1057 /* Suck it into the iovec, verify checksum if not done by hw. */
1058 if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) {
1059 UDPX_INC_STATS_BH(sk, UDP_MIB_INERRORS);
1060 goto out_unlock;
1063 UDPX_INC_STATS_BH(sk, UDP_MIB_INDATAGRAMS);
1065 xprt_adjust_cwnd(xprt, task, copied);
1066 xprt_complete_rqst(task, copied);
1068 out_unlock:
1069 spin_unlock(&xprt->transport_lock);
1070 dropit:
1071 skb_free_datagram(sk, skb);
1072 out:
1073 read_unlock_bh(&sk->sk_callback_lock);
1077 * Helper function to force a TCP close if the server is sending
1078 * junk and/or it has put us in CLOSE_WAIT
1080 static void xs_tcp_force_close(struct rpc_xprt *xprt)
1082 set_bit(XPRT_CONNECTION_CLOSE, &xprt->state);
1083 xprt_force_disconnect(xprt);
1086 static inline void xs_tcp_read_fraghdr(struct rpc_xprt *xprt, struct xdr_skb_reader *desc)
1088 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1089 size_t len, used;
1090 char *p;
1092 p = ((char *) &transport->tcp_fraghdr) + transport->tcp_offset;
1093 len = sizeof(transport->tcp_fraghdr) - transport->tcp_offset;
1094 used = xdr_skb_read_bits(desc, p, len);
1095 transport->tcp_offset += used;
1096 if (used != len)
1097 return;
1099 transport->tcp_reclen = ntohl(transport->tcp_fraghdr);
1100 if (transport->tcp_reclen & RPC_LAST_STREAM_FRAGMENT)
1101 transport->tcp_flags |= TCP_RCV_LAST_FRAG;
1102 else
1103 transport->tcp_flags &= ~TCP_RCV_LAST_FRAG;
1104 transport->tcp_reclen &= RPC_FRAGMENT_SIZE_MASK;
1106 transport->tcp_flags &= ~TCP_RCV_COPY_FRAGHDR;
1107 transport->tcp_offset = 0;
1109 /* Sanity check of the record length */
1110 if (unlikely(transport->tcp_reclen < 8)) {
1111 dprintk("RPC: invalid TCP record fragment length\n");
1112 xs_tcp_force_close(xprt);
1113 return;
1115 dprintk("RPC: reading TCP record fragment of length %d\n",
1116 transport->tcp_reclen);
1119 static void xs_tcp_check_fraghdr(struct sock_xprt *transport)
1121 if (transport->tcp_offset == transport->tcp_reclen) {
1122 transport->tcp_flags |= TCP_RCV_COPY_FRAGHDR;
1123 transport->tcp_offset = 0;
1124 if (transport->tcp_flags & TCP_RCV_LAST_FRAG) {
1125 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1126 transport->tcp_flags |= TCP_RCV_COPY_XID;
1127 transport->tcp_copied = 0;
1132 static inline void xs_tcp_read_xid(struct sock_xprt *transport, struct xdr_skb_reader *desc)
1134 size_t len, used;
1135 char *p;
1137 len = sizeof(transport->tcp_xid) - transport->tcp_offset;
1138 dprintk("RPC: reading XID (%Zu bytes)\n", len);
1139 p = ((char *) &transport->tcp_xid) + transport->tcp_offset;
1140 used = xdr_skb_read_bits(desc, p, len);
1141 transport->tcp_offset += used;
1142 if (used != len)
1143 return;
1144 transport->tcp_flags &= ~TCP_RCV_COPY_XID;
1145 transport->tcp_flags |= TCP_RCV_READ_CALLDIR;
1146 transport->tcp_copied = 4;
1147 dprintk("RPC: reading %s XID %08x\n",
1148 (transport->tcp_flags & TCP_RPC_REPLY) ? "reply for"
1149 : "request with",
1150 ntohl(transport->tcp_xid));
1151 xs_tcp_check_fraghdr(transport);
1154 static inline void xs_tcp_read_calldir(struct sock_xprt *transport,
1155 struct xdr_skb_reader *desc)
1157 size_t len, used;
1158 u32 offset;
1159 char *p;
1162 * We want transport->tcp_offset to be 8 at the end of this routine
1163 * (4 bytes for the xid and 4 bytes for the call/reply flag).
1164 * When this function is called for the first time,
1165 * transport->tcp_offset is 4 (after having already read the xid).
1167 offset = transport->tcp_offset - sizeof(transport->tcp_xid);
1168 len = sizeof(transport->tcp_calldir) - offset;
1169 dprintk("RPC: reading CALL/REPLY flag (%Zu bytes)\n", len);
1170 p = ((char *) &transport->tcp_calldir) + offset;
1171 used = xdr_skb_read_bits(desc, p, len);
1172 transport->tcp_offset += used;
1173 if (used != len)
1174 return;
1175 transport->tcp_flags &= ~TCP_RCV_READ_CALLDIR;
1177 * We don't yet have the XDR buffer, so we will write the calldir
1178 * out after we get the buffer from the 'struct rpc_rqst'
1180 switch (ntohl(transport->tcp_calldir)) {
1181 case RPC_REPLY:
1182 transport->tcp_flags |= TCP_RCV_COPY_CALLDIR;
1183 transport->tcp_flags |= TCP_RCV_COPY_DATA;
1184 transport->tcp_flags |= TCP_RPC_REPLY;
1185 break;
1186 case RPC_CALL:
1187 transport->tcp_flags |= TCP_RCV_COPY_CALLDIR;
1188 transport->tcp_flags |= TCP_RCV_COPY_DATA;
1189 transport->tcp_flags &= ~TCP_RPC_REPLY;
1190 break;
1191 default:
1192 dprintk("RPC: invalid request message type\n");
1193 xs_tcp_force_close(&transport->xprt);
1195 xs_tcp_check_fraghdr(transport);
1198 static inline void xs_tcp_read_common(struct rpc_xprt *xprt,
1199 struct xdr_skb_reader *desc,
1200 struct rpc_rqst *req)
1202 struct sock_xprt *transport =
1203 container_of(xprt, struct sock_xprt, xprt);
1204 struct xdr_buf *rcvbuf;
1205 size_t len;
1206 ssize_t r;
1208 rcvbuf = &req->rq_private_buf;
1210 if (transport->tcp_flags & TCP_RCV_COPY_CALLDIR) {
1212 * Save the RPC direction in the XDR buffer
1214 memcpy(rcvbuf->head[0].iov_base + transport->tcp_copied,
1215 &transport->tcp_calldir,
1216 sizeof(transport->tcp_calldir));
1217 transport->tcp_copied += sizeof(transport->tcp_calldir);
1218 transport->tcp_flags &= ~TCP_RCV_COPY_CALLDIR;
1221 len = desc->count;
1222 if (len > transport->tcp_reclen - transport->tcp_offset) {
1223 struct xdr_skb_reader my_desc;
1225 len = transport->tcp_reclen - transport->tcp_offset;
1226 memcpy(&my_desc, desc, sizeof(my_desc));
1227 my_desc.count = len;
1228 r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
1229 &my_desc, xdr_skb_read_bits);
1230 desc->count -= r;
1231 desc->offset += r;
1232 } else
1233 r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
1234 desc, xdr_skb_read_bits);
1236 if (r > 0) {
1237 transport->tcp_copied += r;
1238 transport->tcp_offset += r;
1240 if (r != len) {
1241 /* Error when copying to the receive buffer,
1242 * usually because we weren't able to allocate
1243 * additional buffer pages. All we can do now
1244 * is turn off TCP_RCV_COPY_DATA, so the request
1245 * will not receive any additional updates,
1246 * and time out.
1247 * Any remaining data from this record will
1248 * be discarded.
1250 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1251 dprintk("RPC: XID %08x truncated request\n",
1252 ntohl(transport->tcp_xid));
1253 dprintk("RPC: xprt = %p, tcp_copied = %lu, "
1254 "tcp_offset = %u, tcp_reclen = %u\n",
1255 xprt, transport->tcp_copied,
1256 transport->tcp_offset, transport->tcp_reclen);
1257 return;
1260 dprintk("RPC: XID %08x read %Zd bytes\n",
1261 ntohl(transport->tcp_xid), r);
1262 dprintk("RPC: xprt = %p, tcp_copied = %lu, tcp_offset = %u, "
1263 "tcp_reclen = %u\n", xprt, transport->tcp_copied,
1264 transport->tcp_offset, transport->tcp_reclen);
1266 if (transport->tcp_copied == req->rq_private_buf.buflen)
1267 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1268 else if (transport->tcp_offset == transport->tcp_reclen) {
1269 if (transport->tcp_flags & TCP_RCV_LAST_FRAG)
1270 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1275 * Finds the request corresponding to the RPC xid and invokes the common
1276 * tcp read code to read the data.
1278 static inline int xs_tcp_read_reply(struct rpc_xprt *xprt,
1279 struct xdr_skb_reader *desc)
1281 struct sock_xprt *transport =
1282 container_of(xprt, struct sock_xprt, xprt);
1283 struct rpc_rqst *req;
1285 dprintk("RPC: read reply XID %08x\n", ntohl(transport->tcp_xid));
1287 /* Find and lock the request corresponding to this xid */
1288 spin_lock(&xprt->transport_lock);
1289 req = xprt_lookup_rqst(xprt, transport->tcp_xid);
1290 if (!req) {
1291 dprintk("RPC: XID %08x request not found!\n",
1292 ntohl(transport->tcp_xid));
1293 spin_unlock(&xprt->transport_lock);
1294 return -1;
1297 xs_tcp_read_common(xprt, desc, req);
1299 if (!(transport->tcp_flags & TCP_RCV_COPY_DATA))
1300 xprt_complete_rqst(req->rq_task, transport->tcp_copied);
1302 spin_unlock(&xprt->transport_lock);
1303 return 0;
1306 #if defined(CONFIG_SUNRPC_BACKCHANNEL)
1308 * Obtains an rpc_rqst previously allocated and invokes the common
1309 * tcp read code to read the data. The result is placed in the callback
1310 * queue.
1311 * If we're unable to obtain the rpc_rqst we schedule the closing of the
1312 * connection and return -1.
1314 static int xs_tcp_read_callback(struct rpc_xprt *xprt,
1315 struct xdr_skb_reader *desc)
1317 struct sock_xprt *transport =
1318 container_of(xprt, struct sock_xprt, xprt);
1319 struct rpc_rqst *req;
1321 /* Look up and lock the request corresponding to the given XID */
1322 spin_lock(&xprt->transport_lock);
1323 req = xprt_lookup_bc_request(xprt, transport->tcp_xid);
1324 if (req == NULL) {
1325 spin_unlock(&xprt->transport_lock);
1326 printk(KERN_WARNING "Callback slot table overflowed\n");
1327 xprt_force_disconnect(xprt);
1328 return -1;
1331 dprintk("RPC: read callback XID %08x\n", ntohl(req->rq_xid));
1332 xs_tcp_read_common(xprt, desc, req);
1334 if (!(transport->tcp_flags & TCP_RCV_COPY_DATA))
1335 xprt_complete_bc_request(req, transport->tcp_copied);
1336 spin_unlock(&xprt->transport_lock);
1338 return 0;
1341 static inline int _xs_tcp_read_data(struct rpc_xprt *xprt,
1342 struct xdr_skb_reader *desc)
1344 struct sock_xprt *transport =
1345 container_of(xprt, struct sock_xprt, xprt);
1347 return (transport->tcp_flags & TCP_RPC_REPLY) ?
1348 xs_tcp_read_reply(xprt, desc) :
1349 xs_tcp_read_callback(xprt, desc);
1351 #else
1352 static inline int _xs_tcp_read_data(struct rpc_xprt *xprt,
1353 struct xdr_skb_reader *desc)
1355 return xs_tcp_read_reply(xprt, desc);
1357 #endif /* CONFIG_SUNRPC_BACKCHANNEL */
1360 * Read data off the transport. This can be either an RPC_CALL or an
1361 * RPC_REPLY. Relay the processing to helper functions.
1363 static void xs_tcp_read_data(struct rpc_xprt *xprt,
1364 struct xdr_skb_reader *desc)
1366 struct sock_xprt *transport =
1367 container_of(xprt, struct sock_xprt, xprt);
1369 if (_xs_tcp_read_data(xprt, desc) == 0)
1370 xs_tcp_check_fraghdr(transport);
1371 else {
1373 * The transport_lock protects the request handling.
1374 * There's no need to hold it to update the tcp_flags.
1376 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1380 static inline void xs_tcp_read_discard(struct sock_xprt *transport, struct xdr_skb_reader *desc)
1382 size_t len;
1384 len = transport->tcp_reclen - transport->tcp_offset;
1385 if (len > desc->count)
1386 len = desc->count;
1387 desc->count -= len;
1388 desc->offset += len;
1389 transport->tcp_offset += len;
1390 dprintk("RPC: discarded %Zu bytes\n", len);
1391 xs_tcp_check_fraghdr(transport);
1394 static int xs_tcp_data_recv(read_descriptor_t *rd_desc, struct sk_buff *skb, unsigned int offset, size_t len)
1396 struct rpc_xprt *xprt = rd_desc->arg.data;
1397 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1398 struct xdr_skb_reader desc = {
1399 .skb = skb,
1400 .offset = offset,
1401 .count = len,
1404 dprintk("RPC: xs_tcp_data_recv started\n");
1405 do {
1406 /* Read in a new fragment marker if necessary */
1407 /* Can we ever really expect to get completely empty fragments? */
1408 if (transport->tcp_flags & TCP_RCV_COPY_FRAGHDR) {
1409 xs_tcp_read_fraghdr(xprt, &desc);
1410 continue;
1412 /* Read in the xid if necessary */
1413 if (transport->tcp_flags & TCP_RCV_COPY_XID) {
1414 xs_tcp_read_xid(transport, &desc);
1415 continue;
1417 /* Read in the call/reply flag */
1418 if (transport->tcp_flags & TCP_RCV_READ_CALLDIR) {
1419 xs_tcp_read_calldir(transport, &desc);
1420 continue;
1422 /* Read in the request data */
1423 if (transport->tcp_flags & TCP_RCV_COPY_DATA) {
1424 xs_tcp_read_data(xprt, &desc);
1425 continue;
1427 /* Skip over any trailing bytes on short reads */
1428 xs_tcp_read_discard(transport, &desc);
1429 } while (desc.count);
1430 dprintk("RPC: xs_tcp_data_recv done\n");
1431 return len - desc.count;
1435 * xs_tcp_data_ready - "data ready" callback for TCP sockets
1436 * @sk: socket with data to read
1437 * @bytes: how much data to read
1440 static void xs_tcp_data_ready(struct sock *sk)
1442 struct rpc_xprt *xprt;
1443 read_descriptor_t rd_desc;
1444 int read;
1446 dprintk("RPC: xs_tcp_data_ready...\n");
1448 read_lock_bh(&sk->sk_callback_lock);
1449 if (!(xprt = xprt_from_sock(sk)))
1450 goto out;
1451 /* Any data means we had a useful conversation, so
1452 * the we don't need to delay the next reconnect
1454 if (xprt->reestablish_timeout)
1455 xprt->reestablish_timeout = 0;
1457 /* We use rd_desc to pass struct xprt to xs_tcp_data_recv */
1458 rd_desc.arg.data = xprt;
1459 do {
1460 rd_desc.count = 65536;
1461 read = tcp_read_sock(sk, &rd_desc, xs_tcp_data_recv);
1462 } while (read > 0);
1463 out:
1464 read_unlock_bh(&sk->sk_callback_lock);
1468 * Do the equivalent of linger/linger2 handling for dealing with
1469 * broken servers that don't close the socket in a timely
1470 * fashion
1472 static void xs_tcp_schedule_linger_timeout(struct rpc_xprt *xprt,
1473 unsigned long timeout)
1475 struct sock_xprt *transport;
1477 if (xprt_test_and_set_connecting(xprt))
1478 return;
1479 set_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1480 transport = container_of(xprt, struct sock_xprt, xprt);
1481 queue_delayed_work(rpciod_workqueue, &transport->connect_worker,
1482 timeout);
1485 static void xs_tcp_cancel_linger_timeout(struct rpc_xprt *xprt)
1487 struct sock_xprt *transport;
1489 transport = container_of(xprt, struct sock_xprt, xprt);
1491 if (!test_bit(XPRT_CONNECTION_ABORT, &xprt->state) ||
1492 !cancel_delayed_work(&transport->connect_worker))
1493 return;
1494 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1495 xprt_clear_connecting(xprt);
1498 static void xs_sock_reset_connection_flags(struct rpc_xprt *xprt)
1500 smp_mb__before_atomic();
1501 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1502 clear_bit(XPRT_CONNECTION_CLOSE, &xprt->state);
1503 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
1504 clear_bit(XPRT_CLOSING, &xprt->state);
1505 smp_mb__after_atomic();
1508 static void xs_sock_mark_closed(struct rpc_xprt *xprt)
1510 xs_sock_reset_connection_flags(xprt);
1511 /* Mark transport as closed and wake up all pending tasks */
1512 xprt_disconnect_done(xprt);
1516 * xs_tcp_state_change - callback to handle TCP socket state changes
1517 * @sk: socket whose state has changed
1520 static void xs_tcp_state_change(struct sock *sk)
1522 struct rpc_xprt *xprt;
1524 read_lock_bh(&sk->sk_callback_lock);
1525 if (!(xprt = xprt_from_sock(sk)))
1526 goto out;
1527 dprintk("RPC: xs_tcp_state_change client %p...\n", xprt);
1528 dprintk("RPC: state %x conn %d dead %d zapped %d sk_shutdown %d\n",
1529 sk->sk_state, xprt_connected(xprt),
1530 sock_flag(sk, SOCK_DEAD),
1531 sock_flag(sk, SOCK_ZAPPED),
1532 sk->sk_shutdown);
1534 trace_rpc_socket_state_change(xprt, sk->sk_socket);
1535 switch (sk->sk_state) {
1536 case TCP_ESTABLISHED:
1537 spin_lock(&xprt->transport_lock);
1538 if (!xprt_test_and_set_connected(xprt)) {
1539 struct sock_xprt *transport = container_of(xprt,
1540 struct sock_xprt, xprt);
1542 /* Reset TCP record info */
1543 transport->tcp_offset = 0;
1544 transport->tcp_reclen = 0;
1545 transport->tcp_copied = 0;
1546 transport->tcp_flags =
1547 TCP_RCV_COPY_FRAGHDR | TCP_RCV_COPY_XID;
1548 xprt->connect_cookie++;
1550 xprt_wake_pending_tasks(xprt, -EAGAIN);
1552 spin_unlock(&xprt->transport_lock);
1553 break;
1554 case TCP_FIN_WAIT1:
1555 /* The client initiated a shutdown of the socket */
1556 xprt->connect_cookie++;
1557 xprt->reestablish_timeout = 0;
1558 set_bit(XPRT_CLOSING, &xprt->state);
1559 smp_mb__before_atomic();
1560 clear_bit(XPRT_CONNECTED, &xprt->state);
1561 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
1562 smp_mb__after_atomic();
1563 xs_tcp_schedule_linger_timeout(xprt, xs_tcp_fin_timeout);
1564 break;
1565 case TCP_CLOSE_WAIT:
1566 /* The server initiated a shutdown of the socket */
1567 xprt->connect_cookie++;
1568 clear_bit(XPRT_CONNECTED, &xprt->state);
1569 xs_tcp_force_close(xprt);
1570 case TCP_CLOSING:
1572 * If the server closed down the connection, make sure that
1573 * we back off before reconnecting
1575 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
1576 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
1577 break;
1578 case TCP_LAST_ACK:
1579 set_bit(XPRT_CLOSING, &xprt->state);
1580 xs_tcp_schedule_linger_timeout(xprt, xs_tcp_fin_timeout);
1581 smp_mb__before_atomic();
1582 clear_bit(XPRT_CONNECTED, &xprt->state);
1583 smp_mb__after_atomic();
1584 break;
1585 case TCP_CLOSE:
1586 xs_tcp_cancel_linger_timeout(xprt);
1587 xs_sock_mark_closed(xprt);
1589 out:
1590 read_unlock_bh(&sk->sk_callback_lock);
1593 static void xs_write_space(struct sock *sk)
1595 struct socket *sock;
1596 struct rpc_xprt *xprt;
1598 if (unlikely(!(sock = sk->sk_socket)))
1599 return;
1600 clear_bit(SOCK_NOSPACE, &sock->flags);
1602 if (unlikely(!(xprt = xprt_from_sock(sk))))
1603 return;
1604 if (test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags) == 0)
1605 return;
1607 xprt_write_space(xprt);
1611 * xs_udp_write_space - callback invoked when socket buffer space
1612 * becomes available
1613 * @sk: socket whose state has changed
1615 * Called when more output buffer space is available for this socket.
1616 * We try not to wake our writers until they can make "significant"
1617 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1618 * with a bunch of small requests.
1620 static void xs_udp_write_space(struct sock *sk)
1622 read_lock_bh(&sk->sk_callback_lock);
1624 /* from net/core/sock.c:sock_def_write_space */
1625 if (sock_writeable(sk))
1626 xs_write_space(sk);
1628 read_unlock_bh(&sk->sk_callback_lock);
1632 * xs_tcp_write_space - callback invoked when socket buffer space
1633 * becomes available
1634 * @sk: socket whose state has changed
1636 * Called when more output buffer space is available for this socket.
1637 * We try not to wake our writers until they can make "significant"
1638 * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1639 * with a bunch of small requests.
1641 static void xs_tcp_write_space(struct sock *sk)
1643 read_lock_bh(&sk->sk_callback_lock);
1645 /* from net/core/stream.c:sk_stream_write_space */
1646 if (sk_stream_is_writeable(sk))
1647 xs_write_space(sk);
1649 read_unlock_bh(&sk->sk_callback_lock);
1652 static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt)
1654 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1655 struct sock *sk = transport->inet;
1657 if (transport->rcvsize) {
1658 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
1659 sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2;
1661 if (transport->sndsize) {
1662 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
1663 sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2;
1664 sk->sk_write_space(sk);
1669 * xs_udp_set_buffer_size - set send and receive limits
1670 * @xprt: generic transport
1671 * @sndsize: requested size of send buffer, in bytes
1672 * @rcvsize: requested size of receive buffer, in bytes
1674 * Set socket send and receive buffer size limits.
1676 static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize)
1678 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1680 transport->sndsize = 0;
1681 if (sndsize)
1682 transport->sndsize = sndsize + 1024;
1683 transport->rcvsize = 0;
1684 if (rcvsize)
1685 transport->rcvsize = rcvsize + 1024;
1687 xs_udp_do_set_buffer_size(xprt);
1691 * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
1692 * @task: task that timed out
1694 * Adjust the congestion window after a retransmit timeout has occurred.
1696 static void xs_udp_timer(struct rpc_xprt *xprt, struct rpc_task *task)
1698 xprt_adjust_cwnd(xprt, task, -ETIMEDOUT);
1701 static unsigned short xs_get_random_port(void)
1703 unsigned short range = xprt_max_resvport - xprt_min_resvport;
1704 unsigned short rand = (unsigned short) prandom_u32() % range;
1705 return rand + xprt_min_resvport;
1709 * xs_set_port - reset the port number in the remote endpoint address
1710 * @xprt: generic transport
1711 * @port: new port number
1714 static void xs_set_port(struct rpc_xprt *xprt, unsigned short port)
1716 dprintk("RPC: setting port for xprt %p to %u\n", xprt, port);
1718 rpc_set_port(xs_addr(xprt), port);
1719 xs_update_peer_port(xprt);
1722 static unsigned short xs_get_srcport(struct sock_xprt *transport)
1724 unsigned short port = transport->srcport;
1726 if (port == 0 && transport->xprt.resvport)
1727 port = xs_get_random_port();
1728 return port;
1731 static unsigned short xs_next_srcport(struct sock_xprt *transport, unsigned short port)
1733 if (transport->srcport != 0)
1734 transport->srcport = 0;
1735 if (!transport->xprt.resvport)
1736 return 0;
1737 if (port <= xprt_min_resvport || port > xprt_max_resvport)
1738 return xprt_max_resvport;
1739 return --port;
1741 static int xs_bind(struct sock_xprt *transport, struct socket *sock)
1743 struct sockaddr_storage myaddr;
1744 int err, nloop = 0;
1745 unsigned short port = xs_get_srcport(transport);
1746 unsigned short last;
1748 memcpy(&myaddr, &transport->srcaddr, transport->xprt.addrlen);
1749 do {
1750 rpc_set_port((struct sockaddr *)&myaddr, port);
1751 err = kernel_bind(sock, (struct sockaddr *)&myaddr,
1752 transport->xprt.addrlen);
1753 if (port == 0)
1754 break;
1755 if (err == 0) {
1756 transport->srcport = port;
1757 break;
1759 last = port;
1760 port = xs_next_srcport(transport, port);
1761 if (port > last)
1762 nloop++;
1763 } while (err == -EADDRINUSE && nloop != 2);
1765 if (myaddr.ss_family == AF_INET)
1766 dprintk("RPC: %s %pI4:%u: %s (%d)\n", __func__,
1767 &((struct sockaddr_in *)&myaddr)->sin_addr,
1768 port, err ? "failed" : "ok", err);
1769 else
1770 dprintk("RPC: %s %pI6:%u: %s (%d)\n", __func__,
1771 &((struct sockaddr_in6 *)&myaddr)->sin6_addr,
1772 port, err ? "failed" : "ok", err);
1773 return err;
1777 * We don't support autobind on AF_LOCAL sockets
1779 static void xs_local_rpcbind(struct rpc_task *task)
1781 rcu_read_lock();
1782 xprt_set_bound(rcu_dereference(task->tk_client->cl_xprt));
1783 rcu_read_unlock();
1786 static void xs_local_set_port(struct rpc_xprt *xprt, unsigned short port)
1790 #ifdef CONFIG_DEBUG_LOCK_ALLOC
1791 static struct lock_class_key xs_key[2];
1792 static struct lock_class_key xs_slock_key[2];
1794 static inline void xs_reclassify_socketu(struct socket *sock)
1796 struct sock *sk = sock->sk;
1798 sock_lock_init_class_and_name(sk, "slock-AF_LOCAL-RPC",
1799 &xs_slock_key[1], "sk_lock-AF_LOCAL-RPC", &xs_key[1]);
1802 static inline void xs_reclassify_socket4(struct socket *sock)
1804 struct sock *sk = sock->sk;
1806 sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC",
1807 &xs_slock_key[0], "sk_lock-AF_INET-RPC", &xs_key[0]);
1810 static inline void xs_reclassify_socket6(struct socket *sock)
1812 struct sock *sk = sock->sk;
1814 sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC",
1815 &xs_slock_key[1], "sk_lock-AF_INET6-RPC", &xs_key[1]);
1818 static inline void xs_reclassify_socket(int family, struct socket *sock)
1820 WARN_ON_ONCE(sock_owned_by_user(sock->sk));
1821 if (sock_owned_by_user(sock->sk))
1822 return;
1824 switch (family) {
1825 case AF_LOCAL:
1826 xs_reclassify_socketu(sock);
1827 break;
1828 case AF_INET:
1829 xs_reclassify_socket4(sock);
1830 break;
1831 case AF_INET6:
1832 xs_reclassify_socket6(sock);
1833 break;
1836 #else
1837 static inline void xs_reclassify_socketu(struct socket *sock)
1841 static inline void xs_reclassify_socket4(struct socket *sock)
1845 static inline void xs_reclassify_socket6(struct socket *sock)
1849 static inline void xs_reclassify_socket(int family, struct socket *sock)
1852 #endif
1854 static void xs_dummy_setup_socket(struct work_struct *work)
1858 static struct socket *xs_create_sock(struct rpc_xprt *xprt,
1859 struct sock_xprt *transport, int family, int type, int protocol)
1861 struct socket *sock;
1862 int err;
1864 err = __sock_create(xprt->xprt_net, family, type, protocol, &sock, 1);
1865 if (err < 0) {
1866 dprintk("RPC: can't create %d transport socket (%d).\n",
1867 protocol, -err);
1868 goto out;
1870 xs_reclassify_socket(family, sock);
1872 err = xs_bind(transport, sock);
1873 if (err) {
1874 sock_release(sock);
1875 goto out;
1878 return sock;
1879 out:
1880 return ERR_PTR(err);
1883 static int xs_local_finish_connecting(struct rpc_xprt *xprt,
1884 struct socket *sock)
1886 struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
1887 xprt);
1889 if (!transport->inet) {
1890 struct sock *sk = sock->sk;
1892 write_lock_bh(&sk->sk_callback_lock);
1894 xs_save_old_callbacks(transport, sk);
1896 sk->sk_user_data = xprt;
1897 sk->sk_data_ready = xs_local_data_ready;
1898 sk->sk_write_space = xs_udp_write_space;
1899 sk->sk_error_report = xs_error_report;
1900 sk->sk_allocation = GFP_ATOMIC;
1902 xprt_clear_connected(xprt);
1904 /* Reset to new socket */
1905 transport->sock = sock;
1906 transport->inet = sk;
1908 write_unlock_bh(&sk->sk_callback_lock);
1911 /* Tell the socket layer to start connecting... */
1912 xprt->stat.connect_count++;
1913 xprt->stat.connect_start = jiffies;
1914 return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, 0);
1918 * xs_local_setup_socket - create AF_LOCAL socket, connect to a local endpoint
1919 * @xprt: RPC transport to connect
1920 * @transport: socket transport to connect
1921 * @create_sock: function to create a socket of the correct type
1923 static int xs_local_setup_socket(struct sock_xprt *transport)
1925 struct rpc_xprt *xprt = &transport->xprt;
1926 struct socket *sock;
1927 int status = -EIO;
1929 current->flags |= PF_FSTRANS;
1931 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1932 status = __sock_create(xprt->xprt_net, AF_LOCAL,
1933 SOCK_STREAM, 0, &sock, 1);
1934 if (status < 0) {
1935 dprintk("RPC: can't create AF_LOCAL "
1936 "transport socket (%d).\n", -status);
1937 goto out;
1939 xs_reclassify_socketu(sock);
1941 dprintk("RPC: worker connecting xprt %p via AF_LOCAL to %s\n",
1942 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1944 status = xs_local_finish_connecting(xprt, sock);
1945 trace_rpc_socket_connect(xprt, sock, status);
1946 switch (status) {
1947 case 0:
1948 dprintk("RPC: xprt %p connected to %s\n",
1949 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1950 xprt_set_connected(xprt);
1951 break;
1952 case -ENOENT:
1953 dprintk("RPC: xprt %p: socket %s does not exist\n",
1954 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1955 break;
1956 case -ECONNREFUSED:
1957 dprintk("RPC: xprt %p: connection refused for %s\n",
1958 xprt, xprt->address_strings[RPC_DISPLAY_ADDR]);
1959 break;
1960 default:
1961 printk(KERN_ERR "%s: unhandled error (%d) connecting to %s\n",
1962 __func__, -status,
1963 xprt->address_strings[RPC_DISPLAY_ADDR]);
1966 out:
1967 xprt_clear_connecting(xprt);
1968 xprt_wake_pending_tasks(xprt, status);
1969 current->flags &= ~PF_FSTRANS;
1970 return status;
1973 static void xs_local_connect(struct rpc_xprt *xprt, struct rpc_task *task)
1975 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1976 int ret;
1978 if (RPC_IS_ASYNC(task)) {
1980 * We want the AF_LOCAL connect to be resolved in the
1981 * filesystem namespace of the process making the rpc
1982 * call. Thus we connect synchronously.
1984 * If we want to support asynchronous AF_LOCAL calls,
1985 * we'll need to figure out how to pass a namespace to
1986 * connect.
1988 rpc_exit(task, -ENOTCONN);
1989 return;
1991 ret = xs_local_setup_socket(transport);
1992 if (ret && !RPC_IS_SOFTCONN(task))
1993 msleep_interruptible(15000);
1996 #ifdef CONFIG_SUNRPC_SWAP
1997 static void xs_set_memalloc(struct rpc_xprt *xprt)
1999 struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
2000 xprt);
2002 if (xprt->swapper)
2003 sk_set_memalloc(transport->inet);
2007 * xs_swapper - Tag this transport as being used for swap.
2008 * @xprt: transport to tag
2009 * @enable: enable/disable
2012 int xs_swapper(struct rpc_xprt *xprt, int enable)
2014 struct sock_xprt *transport = container_of(xprt, struct sock_xprt,
2015 xprt);
2016 int err = 0;
2018 if (enable) {
2019 xprt->swapper++;
2020 xs_set_memalloc(xprt);
2021 } else if (xprt->swapper) {
2022 xprt->swapper--;
2023 sk_clear_memalloc(transport->inet);
2026 return err;
2028 EXPORT_SYMBOL_GPL(xs_swapper);
2029 #else
2030 static void xs_set_memalloc(struct rpc_xprt *xprt)
2033 #endif
2035 static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
2037 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2039 if (!transport->inet) {
2040 struct sock *sk = sock->sk;
2042 write_lock_bh(&sk->sk_callback_lock);
2044 xs_save_old_callbacks(transport, sk);
2046 sk->sk_user_data = xprt;
2047 sk->sk_data_ready = xs_udp_data_ready;
2048 sk->sk_write_space = xs_udp_write_space;
2049 sk->sk_allocation = GFP_ATOMIC;
2051 xprt_set_connected(xprt);
2053 /* Reset to new socket */
2054 transport->sock = sock;
2055 transport->inet = sk;
2057 xs_set_memalloc(xprt);
2059 write_unlock_bh(&sk->sk_callback_lock);
2061 xs_udp_do_set_buffer_size(xprt);
2064 static void xs_udp_setup_socket(struct work_struct *work)
2066 struct sock_xprt *transport =
2067 container_of(work, struct sock_xprt, connect_worker.work);
2068 struct rpc_xprt *xprt = &transport->xprt;
2069 struct socket *sock = transport->sock;
2070 int status = -EIO;
2072 current->flags |= PF_FSTRANS;
2074 /* Start by resetting any existing state */
2075 xs_reset_transport(transport);
2076 sock = xs_create_sock(xprt, transport,
2077 xs_addr(xprt)->sa_family, SOCK_DGRAM, IPPROTO_UDP);
2078 if (IS_ERR(sock))
2079 goto out;
2081 dprintk("RPC: worker connecting xprt %p via %s to "
2082 "%s (port %s)\n", xprt,
2083 xprt->address_strings[RPC_DISPLAY_PROTO],
2084 xprt->address_strings[RPC_DISPLAY_ADDR],
2085 xprt->address_strings[RPC_DISPLAY_PORT]);
2087 xs_udp_finish_connecting(xprt, sock);
2088 trace_rpc_socket_connect(xprt, sock, 0);
2089 status = 0;
2090 out:
2091 xprt_clear_connecting(xprt);
2092 xprt_wake_pending_tasks(xprt, status);
2093 current->flags &= ~PF_FSTRANS;
2097 * We need to preserve the port number so the reply cache on the server can
2098 * find our cached RPC replies when we get around to reconnecting.
2100 static void xs_abort_connection(struct sock_xprt *transport)
2102 int result;
2103 struct sockaddr any;
2105 dprintk("RPC: disconnecting xprt %p to reuse port\n", transport);
2108 * Disconnect the transport socket by doing a connect operation
2109 * with AF_UNSPEC. This should return immediately...
2111 memset(&any, 0, sizeof(any));
2112 any.sa_family = AF_UNSPEC;
2113 result = kernel_connect(transport->sock, &any, sizeof(any), 0);
2114 trace_rpc_socket_reset_connection(&transport->xprt,
2115 transport->sock, result);
2116 if (!result)
2117 xs_sock_reset_connection_flags(&transport->xprt);
2118 dprintk("RPC: AF_UNSPEC connect return code %d\n", result);
2121 static void xs_tcp_reuse_connection(struct sock_xprt *transport)
2123 unsigned int state = transport->inet->sk_state;
2125 if (state == TCP_CLOSE && transport->sock->state == SS_UNCONNECTED) {
2126 /* we don't need to abort the connection if the socket
2127 * hasn't undergone a shutdown
2129 if (transport->inet->sk_shutdown == 0)
2130 return;
2131 dprintk("RPC: %s: TCP_CLOSEd and sk_shutdown set to %d\n",
2132 __func__, transport->inet->sk_shutdown);
2134 if ((1 << state) & (TCPF_ESTABLISHED|TCPF_SYN_SENT)) {
2135 /* we don't need to abort the connection if the socket
2136 * hasn't undergone a shutdown
2138 if (transport->inet->sk_shutdown == 0)
2139 return;
2140 dprintk("RPC: %s: ESTABLISHED/SYN_SENT "
2141 "sk_shutdown set to %d\n",
2142 __func__, transport->inet->sk_shutdown);
2144 xs_abort_connection(transport);
2147 static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
2149 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2150 int ret = -ENOTCONN;
2152 if (!transport->inet) {
2153 struct sock *sk = sock->sk;
2154 unsigned int keepidle = xprt->timeout->to_initval / HZ;
2155 unsigned int keepcnt = xprt->timeout->to_retries + 1;
2156 unsigned int opt_on = 1;
2158 /* TCP Keepalive options */
2159 kernel_setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE,
2160 (char *)&opt_on, sizeof(opt_on));
2161 kernel_setsockopt(sock, SOL_TCP, TCP_KEEPIDLE,
2162 (char *)&keepidle, sizeof(keepidle));
2163 kernel_setsockopt(sock, SOL_TCP, TCP_KEEPINTVL,
2164 (char *)&keepidle, sizeof(keepidle));
2165 kernel_setsockopt(sock, SOL_TCP, TCP_KEEPCNT,
2166 (char *)&keepcnt, sizeof(keepcnt));
2168 write_lock_bh(&sk->sk_callback_lock);
2170 xs_save_old_callbacks(transport, sk);
2172 sk->sk_user_data = xprt;
2173 sk->sk_data_ready = xs_tcp_data_ready;
2174 sk->sk_state_change = xs_tcp_state_change;
2175 sk->sk_write_space = xs_tcp_write_space;
2176 sk->sk_error_report = xs_error_report;
2177 sk->sk_allocation = GFP_ATOMIC;
2179 /* socket options */
2180 sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
2181 sock_reset_flag(sk, SOCK_LINGER);
2182 tcp_sk(sk)->linger2 = 0;
2183 tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF;
2185 xprt_clear_connected(xprt);
2187 /* Reset to new socket */
2188 transport->sock = sock;
2189 transport->inet = sk;
2191 write_unlock_bh(&sk->sk_callback_lock);
2194 if (!xprt_bound(xprt))
2195 goto out;
2197 xs_set_memalloc(xprt);
2199 /* Tell the socket layer to start connecting... */
2200 xprt->stat.connect_count++;
2201 xprt->stat.connect_start = jiffies;
2202 ret = kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK);
2203 switch (ret) {
2204 case 0:
2205 case -EINPROGRESS:
2206 /* SYN_SENT! */
2207 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
2208 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2210 out:
2211 return ret;
2215 * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint
2216 * @xprt: RPC transport to connect
2217 * @transport: socket transport to connect
2218 * @create_sock: function to create a socket of the correct type
2220 * Invoked by a work queue tasklet.
2222 static void xs_tcp_setup_socket(struct work_struct *work)
2224 struct sock_xprt *transport =
2225 container_of(work, struct sock_xprt, connect_worker.work);
2226 struct socket *sock = transport->sock;
2227 struct rpc_xprt *xprt = &transport->xprt;
2228 int status = -EIO;
2230 current->flags |= PF_FSTRANS;
2232 if (!sock) {
2233 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
2234 sock = xs_create_sock(xprt, transport,
2235 xs_addr(xprt)->sa_family, SOCK_STREAM, IPPROTO_TCP);
2236 if (IS_ERR(sock)) {
2237 status = PTR_ERR(sock);
2238 goto out;
2240 } else {
2241 int abort_and_exit;
2243 abort_and_exit = test_and_clear_bit(XPRT_CONNECTION_ABORT,
2244 &xprt->state);
2245 /* "close" the socket, preserving the local port */
2246 set_bit(XPRT_CONNECTION_REUSE, &xprt->state);
2247 xs_tcp_reuse_connection(transport);
2248 clear_bit(XPRT_CONNECTION_REUSE, &xprt->state);
2250 if (abort_and_exit)
2251 goto out_eagain;
2254 dprintk("RPC: worker connecting xprt %p via %s to "
2255 "%s (port %s)\n", xprt,
2256 xprt->address_strings[RPC_DISPLAY_PROTO],
2257 xprt->address_strings[RPC_DISPLAY_ADDR],
2258 xprt->address_strings[RPC_DISPLAY_PORT]);
2260 status = xs_tcp_finish_connecting(xprt, sock);
2261 trace_rpc_socket_connect(xprt, sock, status);
2262 dprintk("RPC: %p connect status %d connected %d sock state %d\n",
2263 xprt, -status, xprt_connected(xprt),
2264 sock->sk->sk_state);
2265 switch (status) {
2266 default:
2267 printk("%s: connect returned unhandled error %d\n",
2268 __func__, status);
2269 case -EADDRNOTAVAIL:
2270 /* We're probably in TIME_WAIT. Get rid of existing socket,
2271 * and retry
2273 xs_tcp_force_close(xprt);
2274 break;
2275 case 0:
2276 case -EINPROGRESS:
2277 case -EALREADY:
2278 xprt_clear_connecting(xprt);
2279 current->flags &= ~PF_FSTRANS;
2280 return;
2281 case -EINVAL:
2282 /* Happens, for instance, if the user specified a link
2283 * local IPv6 address without a scope-id.
2285 case -ECONNREFUSED:
2286 case -ECONNRESET:
2287 case -ENETUNREACH:
2288 /* retry with existing socket, after a delay */
2289 goto out;
2291 out_eagain:
2292 status = -EAGAIN;
2293 out:
2294 xprt_clear_connecting(xprt);
2295 xprt_wake_pending_tasks(xprt, status);
2296 current->flags &= ~PF_FSTRANS;
2300 * xs_connect - connect a socket to a remote endpoint
2301 * @xprt: pointer to transport structure
2302 * @task: address of RPC task that manages state of connect request
2304 * TCP: If the remote end dropped the connection, delay reconnecting.
2306 * UDP socket connects are synchronous, but we use a work queue anyway
2307 * to guarantee that even unprivileged user processes can set up a
2308 * socket on a privileged port.
2310 * If a UDP socket connect fails, the delay behavior here prevents
2311 * retry floods (hard mounts).
2313 static void xs_connect(struct rpc_xprt *xprt, struct rpc_task *task)
2315 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2317 if (transport->sock != NULL && !RPC_IS_SOFTCONN(task)) {
2318 dprintk("RPC: xs_connect delayed xprt %p for %lu "
2319 "seconds\n",
2320 xprt, xprt->reestablish_timeout / HZ);
2321 queue_delayed_work(rpciod_workqueue,
2322 &transport->connect_worker,
2323 xprt->reestablish_timeout);
2324 xprt->reestablish_timeout <<= 1;
2325 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
2326 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2327 if (xprt->reestablish_timeout > XS_TCP_MAX_REEST_TO)
2328 xprt->reestablish_timeout = XS_TCP_MAX_REEST_TO;
2329 } else {
2330 dprintk("RPC: xs_connect scheduled xprt %p\n", xprt);
2331 queue_delayed_work(rpciod_workqueue,
2332 &transport->connect_worker, 0);
2337 * xs_local_print_stats - display AF_LOCAL socket-specifc stats
2338 * @xprt: rpc_xprt struct containing statistics
2339 * @seq: output file
2342 static void xs_local_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2344 long idle_time = 0;
2346 if (xprt_connected(xprt))
2347 idle_time = (long)(jiffies - xprt->last_used) / HZ;
2349 seq_printf(seq, "\txprt:\tlocal %lu %lu %lu %ld %lu %lu %lu "
2350 "%llu %llu %lu %llu %llu\n",
2351 xprt->stat.bind_count,
2352 xprt->stat.connect_count,
2353 xprt->stat.connect_time,
2354 idle_time,
2355 xprt->stat.sends,
2356 xprt->stat.recvs,
2357 xprt->stat.bad_xids,
2358 xprt->stat.req_u,
2359 xprt->stat.bklog_u,
2360 xprt->stat.max_slots,
2361 xprt->stat.sending_u,
2362 xprt->stat.pending_u);
2366 * xs_udp_print_stats - display UDP socket-specifc stats
2367 * @xprt: rpc_xprt struct containing statistics
2368 * @seq: output file
2371 static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2373 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2375 seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %llu %llu "
2376 "%lu %llu %llu\n",
2377 transport->srcport,
2378 xprt->stat.bind_count,
2379 xprt->stat.sends,
2380 xprt->stat.recvs,
2381 xprt->stat.bad_xids,
2382 xprt->stat.req_u,
2383 xprt->stat.bklog_u,
2384 xprt->stat.max_slots,
2385 xprt->stat.sending_u,
2386 xprt->stat.pending_u);
2390 * xs_tcp_print_stats - display TCP socket-specifc stats
2391 * @xprt: rpc_xprt struct containing statistics
2392 * @seq: output file
2395 static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2397 struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2398 long idle_time = 0;
2400 if (xprt_connected(xprt))
2401 idle_time = (long)(jiffies - xprt->last_used) / HZ;
2403 seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu "
2404 "%llu %llu %lu %llu %llu\n",
2405 transport->srcport,
2406 xprt->stat.bind_count,
2407 xprt->stat.connect_count,
2408 xprt->stat.connect_time,
2409 idle_time,
2410 xprt->stat.sends,
2411 xprt->stat.recvs,
2412 xprt->stat.bad_xids,
2413 xprt->stat.req_u,
2414 xprt->stat.bklog_u,
2415 xprt->stat.max_slots,
2416 xprt->stat.sending_u,
2417 xprt->stat.pending_u);
2421 * Allocate a bunch of pages for a scratch buffer for the rpc code. The reason
2422 * we allocate pages instead doing a kmalloc like rpc_malloc is because we want
2423 * to use the server side send routines.
2425 static void *bc_malloc(struct rpc_task *task, size_t size)
2427 struct page *page;
2428 struct rpc_buffer *buf;
2430 WARN_ON_ONCE(size > PAGE_SIZE - sizeof(struct rpc_buffer));
2431 if (size > PAGE_SIZE - sizeof(struct rpc_buffer))
2432 return NULL;
2434 page = alloc_page(GFP_KERNEL);
2435 if (!page)
2436 return NULL;
2438 buf = page_address(page);
2439 buf->len = PAGE_SIZE;
2441 return buf->data;
2445 * Free the space allocated in the bc_alloc routine
2447 static void bc_free(void *buffer)
2449 struct rpc_buffer *buf;
2451 if (!buffer)
2452 return;
2454 buf = container_of(buffer, struct rpc_buffer, data);
2455 free_page((unsigned long)buf);
2459 * Use the svc_sock to send the callback. Must be called with svsk->sk_mutex
2460 * held. Borrows heavily from svc_tcp_sendto and xs_tcp_send_request.
2462 static int bc_sendto(struct rpc_rqst *req)
2464 int len;
2465 struct xdr_buf *xbufp = &req->rq_snd_buf;
2466 struct rpc_xprt *xprt = req->rq_xprt;
2467 struct sock_xprt *transport =
2468 container_of(xprt, struct sock_xprt, xprt);
2469 struct socket *sock = transport->sock;
2470 unsigned long headoff;
2471 unsigned long tailoff;
2473 xs_encode_stream_record_marker(xbufp);
2475 tailoff = (unsigned long)xbufp->tail[0].iov_base & ~PAGE_MASK;
2476 headoff = (unsigned long)xbufp->head[0].iov_base & ~PAGE_MASK;
2477 len = svc_send_common(sock, xbufp,
2478 virt_to_page(xbufp->head[0].iov_base), headoff,
2479 xbufp->tail[0].iov_base, tailoff);
2481 if (len != xbufp->len) {
2482 printk(KERN_NOTICE "Error sending entire callback!\n");
2483 len = -EAGAIN;
2486 return len;
2490 * The send routine. Borrows from svc_send
2492 static int bc_send_request(struct rpc_task *task)
2494 struct rpc_rqst *req = task->tk_rqstp;
2495 struct svc_xprt *xprt;
2496 u32 len;
2498 dprintk("sending request with xid: %08x\n", ntohl(req->rq_xid));
2500 * Get the server socket associated with this callback xprt
2502 xprt = req->rq_xprt->bc_xprt;
2505 * Grab the mutex to serialize data as the connection is shared
2506 * with the fore channel
2508 if (!mutex_trylock(&xprt->xpt_mutex)) {
2509 rpc_sleep_on(&xprt->xpt_bc_pending, task, NULL);
2510 if (!mutex_trylock(&xprt->xpt_mutex))
2511 return -EAGAIN;
2512 rpc_wake_up_queued_task(&xprt->xpt_bc_pending, task);
2514 if (test_bit(XPT_DEAD, &xprt->xpt_flags))
2515 len = -ENOTCONN;
2516 else
2517 len = bc_sendto(req);
2518 mutex_unlock(&xprt->xpt_mutex);
2520 if (len > 0)
2521 len = 0;
2523 return len;
2527 * The close routine. Since this is client initiated, we do nothing
2530 static void bc_close(struct rpc_xprt *xprt)
2535 * The xprt destroy routine. Again, because this connection is client
2536 * initiated, we do nothing
2539 static void bc_destroy(struct rpc_xprt *xprt)
2541 dprintk("RPC: bc_destroy xprt %p\n", xprt);
2543 xs_xprt_free(xprt);
2544 module_put(THIS_MODULE);
2547 static struct rpc_xprt_ops xs_local_ops = {
2548 .reserve_xprt = xprt_reserve_xprt,
2549 .release_xprt = xs_tcp_release_xprt,
2550 .alloc_slot = xprt_alloc_slot,
2551 .rpcbind = xs_local_rpcbind,
2552 .set_port = xs_local_set_port,
2553 .connect = xs_local_connect,
2554 .buf_alloc = rpc_malloc,
2555 .buf_free = rpc_free,
2556 .send_request = xs_local_send_request,
2557 .set_retrans_timeout = xprt_set_retrans_timeout_def,
2558 .close = xs_close,
2559 .destroy = xs_destroy,
2560 .print_stats = xs_local_print_stats,
2563 static struct rpc_xprt_ops xs_udp_ops = {
2564 .set_buffer_size = xs_udp_set_buffer_size,
2565 .reserve_xprt = xprt_reserve_xprt_cong,
2566 .release_xprt = xprt_release_xprt_cong,
2567 .alloc_slot = xprt_alloc_slot,
2568 .rpcbind = rpcb_getport_async,
2569 .set_port = xs_set_port,
2570 .connect = xs_connect,
2571 .buf_alloc = rpc_malloc,
2572 .buf_free = rpc_free,
2573 .send_request = xs_udp_send_request,
2574 .set_retrans_timeout = xprt_set_retrans_timeout_rtt,
2575 .timer = xs_udp_timer,
2576 .release_request = xprt_release_rqst_cong,
2577 .close = xs_close,
2578 .destroy = xs_destroy,
2579 .print_stats = xs_udp_print_stats,
2582 static struct rpc_xprt_ops xs_tcp_ops = {
2583 .reserve_xprt = xprt_reserve_xprt,
2584 .release_xprt = xs_tcp_release_xprt,
2585 .alloc_slot = xprt_lock_and_alloc_slot,
2586 .rpcbind = rpcb_getport_async,
2587 .set_port = xs_set_port,
2588 .connect = xs_connect,
2589 .buf_alloc = rpc_malloc,
2590 .buf_free = rpc_free,
2591 .send_request = xs_tcp_send_request,
2592 .set_retrans_timeout = xprt_set_retrans_timeout_def,
2593 .close = xs_tcp_close,
2594 .destroy = xs_destroy,
2595 .print_stats = xs_tcp_print_stats,
2599 * The rpc_xprt_ops for the server backchannel
2602 static struct rpc_xprt_ops bc_tcp_ops = {
2603 .reserve_xprt = xprt_reserve_xprt,
2604 .release_xprt = xprt_release_xprt,
2605 .alloc_slot = xprt_alloc_slot,
2606 .buf_alloc = bc_malloc,
2607 .buf_free = bc_free,
2608 .send_request = bc_send_request,
2609 .set_retrans_timeout = xprt_set_retrans_timeout_def,
2610 .close = bc_close,
2611 .destroy = bc_destroy,
2612 .print_stats = xs_tcp_print_stats,
2615 static int xs_init_anyaddr(const int family, struct sockaddr *sap)
2617 static const struct sockaddr_in sin = {
2618 .sin_family = AF_INET,
2619 .sin_addr.s_addr = htonl(INADDR_ANY),
2621 static const struct sockaddr_in6 sin6 = {
2622 .sin6_family = AF_INET6,
2623 .sin6_addr = IN6ADDR_ANY_INIT,
2626 switch (family) {
2627 case AF_LOCAL:
2628 break;
2629 case AF_INET:
2630 memcpy(sap, &sin, sizeof(sin));
2631 break;
2632 case AF_INET6:
2633 memcpy(sap, &sin6, sizeof(sin6));
2634 break;
2635 default:
2636 dprintk("RPC: %s: Bad address family\n", __func__);
2637 return -EAFNOSUPPORT;
2639 return 0;
2642 static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args,
2643 unsigned int slot_table_size,
2644 unsigned int max_slot_table_size)
2646 struct rpc_xprt *xprt;
2647 struct sock_xprt *new;
2649 if (args->addrlen > sizeof(xprt->addr)) {
2650 dprintk("RPC: xs_setup_xprt: address too large\n");
2651 return ERR_PTR(-EBADF);
2654 xprt = xprt_alloc(args->net, sizeof(*new), slot_table_size,
2655 max_slot_table_size);
2656 if (xprt == NULL) {
2657 dprintk("RPC: xs_setup_xprt: couldn't allocate "
2658 "rpc_xprt\n");
2659 return ERR_PTR(-ENOMEM);
2662 new = container_of(xprt, struct sock_xprt, xprt);
2663 memcpy(&xprt->addr, args->dstaddr, args->addrlen);
2664 xprt->addrlen = args->addrlen;
2665 if (args->srcaddr)
2666 memcpy(&new->srcaddr, args->srcaddr, args->addrlen);
2667 else {
2668 int err;
2669 err = xs_init_anyaddr(args->dstaddr->sa_family,
2670 (struct sockaddr *)&new->srcaddr);
2671 if (err != 0) {
2672 xprt_free(xprt);
2673 return ERR_PTR(err);
2677 return xprt;
2680 static const struct rpc_timeout xs_local_default_timeout = {
2681 .to_initval = 10 * HZ,
2682 .to_maxval = 10 * HZ,
2683 .to_retries = 2,
2687 * xs_setup_local - Set up transport to use an AF_LOCAL socket
2688 * @args: rpc transport creation arguments
2690 * AF_LOCAL is a "tpi_cots_ord" transport, just like TCP
2692 static struct rpc_xprt *xs_setup_local(struct xprt_create *args)
2694 struct sockaddr_un *sun = (struct sockaddr_un *)args->dstaddr;
2695 struct sock_xprt *transport;
2696 struct rpc_xprt *xprt;
2697 struct rpc_xprt *ret;
2699 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
2700 xprt_max_tcp_slot_table_entries);
2701 if (IS_ERR(xprt))
2702 return xprt;
2703 transport = container_of(xprt, struct sock_xprt, xprt);
2705 xprt->prot = 0;
2706 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
2707 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2709 xprt->bind_timeout = XS_BIND_TO;
2710 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2711 xprt->idle_timeout = XS_IDLE_DISC_TO;
2713 xprt->ops = &xs_local_ops;
2714 xprt->timeout = &xs_local_default_timeout;
2716 INIT_DELAYED_WORK(&transport->connect_worker,
2717 xs_dummy_setup_socket);
2719 switch (sun->sun_family) {
2720 case AF_LOCAL:
2721 if (sun->sun_path[0] != '/') {
2722 dprintk("RPC: bad AF_LOCAL address: %s\n",
2723 sun->sun_path);
2724 ret = ERR_PTR(-EINVAL);
2725 goto out_err;
2727 xprt_set_bound(xprt);
2728 xs_format_peer_addresses(xprt, "local", RPCBIND_NETID_LOCAL);
2729 ret = ERR_PTR(xs_local_setup_socket(transport));
2730 if (ret)
2731 goto out_err;
2732 break;
2733 default:
2734 ret = ERR_PTR(-EAFNOSUPPORT);
2735 goto out_err;
2738 dprintk("RPC: set up xprt to %s via AF_LOCAL\n",
2739 xprt->address_strings[RPC_DISPLAY_ADDR]);
2741 if (try_module_get(THIS_MODULE))
2742 return xprt;
2743 ret = ERR_PTR(-EINVAL);
2744 out_err:
2745 xs_xprt_free(xprt);
2746 return ret;
2749 static const struct rpc_timeout xs_udp_default_timeout = {
2750 .to_initval = 5 * HZ,
2751 .to_maxval = 30 * HZ,
2752 .to_increment = 5 * HZ,
2753 .to_retries = 5,
2757 * xs_setup_udp - Set up transport to use a UDP socket
2758 * @args: rpc transport creation arguments
2761 static struct rpc_xprt *xs_setup_udp(struct xprt_create *args)
2763 struct sockaddr *addr = args->dstaddr;
2764 struct rpc_xprt *xprt;
2765 struct sock_xprt *transport;
2766 struct rpc_xprt *ret;
2768 xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries,
2769 xprt_udp_slot_table_entries);
2770 if (IS_ERR(xprt))
2771 return xprt;
2772 transport = container_of(xprt, struct sock_xprt, xprt);
2774 xprt->prot = IPPROTO_UDP;
2775 xprt->tsh_size = 0;
2776 /* XXX: header size can vary due to auth type, IPv6, etc. */
2777 xprt->max_payload = (1U << 16) - (MAX_HEADER << 3);
2779 xprt->bind_timeout = XS_BIND_TO;
2780 xprt->reestablish_timeout = XS_UDP_REEST_TO;
2781 xprt->idle_timeout = XS_IDLE_DISC_TO;
2783 xprt->ops = &xs_udp_ops;
2785 xprt->timeout = &xs_udp_default_timeout;
2787 switch (addr->sa_family) {
2788 case AF_INET:
2789 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
2790 xprt_set_bound(xprt);
2792 INIT_DELAYED_WORK(&transport->connect_worker,
2793 xs_udp_setup_socket);
2794 xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP);
2795 break;
2796 case AF_INET6:
2797 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
2798 xprt_set_bound(xprt);
2800 INIT_DELAYED_WORK(&transport->connect_worker,
2801 xs_udp_setup_socket);
2802 xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6);
2803 break;
2804 default:
2805 ret = ERR_PTR(-EAFNOSUPPORT);
2806 goto out_err;
2809 if (xprt_bound(xprt))
2810 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2811 xprt->address_strings[RPC_DISPLAY_ADDR],
2812 xprt->address_strings[RPC_DISPLAY_PORT],
2813 xprt->address_strings[RPC_DISPLAY_PROTO]);
2814 else
2815 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
2816 xprt->address_strings[RPC_DISPLAY_ADDR],
2817 xprt->address_strings[RPC_DISPLAY_PROTO]);
2819 if (try_module_get(THIS_MODULE))
2820 return xprt;
2821 ret = ERR_PTR(-EINVAL);
2822 out_err:
2823 xs_xprt_free(xprt);
2824 return ret;
2827 static const struct rpc_timeout xs_tcp_default_timeout = {
2828 .to_initval = 60 * HZ,
2829 .to_maxval = 60 * HZ,
2830 .to_retries = 2,
2834 * xs_setup_tcp - Set up transport to use a TCP socket
2835 * @args: rpc transport creation arguments
2838 static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args)
2840 struct sockaddr *addr = args->dstaddr;
2841 struct rpc_xprt *xprt;
2842 struct sock_xprt *transport;
2843 struct rpc_xprt *ret;
2844 unsigned int max_slot_table_size = xprt_max_tcp_slot_table_entries;
2846 if (args->flags & XPRT_CREATE_INFINITE_SLOTS)
2847 max_slot_table_size = RPC_MAX_SLOT_TABLE_LIMIT;
2849 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
2850 max_slot_table_size);
2851 if (IS_ERR(xprt))
2852 return xprt;
2853 transport = container_of(xprt, struct sock_xprt, xprt);
2855 xprt->prot = IPPROTO_TCP;
2856 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
2857 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2859 xprt->bind_timeout = XS_BIND_TO;
2860 xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2861 xprt->idle_timeout = XS_IDLE_DISC_TO;
2863 xprt->ops = &xs_tcp_ops;
2864 xprt->timeout = &xs_tcp_default_timeout;
2866 switch (addr->sa_family) {
2867 case AF_INET:
2868 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
2869 xprt_set_bound(xprt);
2871 INIT_DELAYED_WORK(&transport->connect_worker,
2872 xs_tcp_setup_socket);
2873 xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP);
2874 break;
2875 case AF_INET6:
2876 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
2877 xprt_set_bound(xprt);
2879 INIT_DELAYED_WORK(&transport->connect_worker,
2880 xs_tcp_setup_socket);
2881 xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6);
2882 break;
2883 default:
2884 ret = ERR_PTR(-EAFNOSUPPORT);
2885 goto out_err;
2888 if (xprt_bound(xprt))
2889 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2890 xprt->address_strings[RPC_DISPLAY_ADDR],
2891 xprt->address_strings[RPC_DISPLAY_PORT],
2892 xprt->address_strings[RPC_DISPLAY_PROTO]);
2893 else
2894 dprintk("RPC: set up xprt to %s (autobind) via %s\n",
2895 xprt->address_strings[RPC_DISPLAY_ADDR],
2896 xprt->address_strings[RPC_DISPLAY_PROTO]);
2898 if (try_module_get(THIS_MODULE))
2899 return xprt;
2900 ret = ERR_PTR(-EINVAL);
2901 out_err:
2902 xs_xprt_free(xprt);
2903 return ret;
2907 * xs_setup_bc_tcp - Set up transport to use a TCP backchannel socket
2908 * @args: rpc transport creation arguments
2911 static struct rpc_xprt *xs_setup_bc_tcp(struct xprt_create *args)
2913 struct sockaddr *addr = args->dstaddr;
2914 struct rpc_xprt *xprt;
2915 struct sock_xprt *transport;
2916 struct svc_sock *bc_sock;
2917 struct rpc_xprt *ret;
2919 xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries,
2920 xprt_tcp_slot_table_entries);
2921 if (IS_ERR(xprt))
2922 return xprt;
2923 transport = container_of(xprt, struct sock_xprt, xprt);
2925 xprt->prot = IPPROTO_TCP;
2926 xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
2927 xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2928 xprt->timeout = &xs_tcp_default_timeout;
2930 /* backchannel */
2931 xprt_set_bound(xprt);
2932 xprt->bind_timeout = 0;
2933 xprt->reestablish_timeout = 0;
2934 xprt->idle_timeout = 0;
2936 xprt->ops = &bc_tcp_ops;
2938 switch (addr->sa_family) {
2939 case AF_INET:
2940 xs_format_peer_addresses(xprt, "tcp",
2941 RPCBIND_NETID_TCP);
2942 break;
2943 case AF_INET6:
2944 xs_format_peer_addresses(xprt, "tcp",
2945 RPCBIND_NETID_TCP6);
2946 break;
2947 default:
2948 ret = ERR_PTR(-EAFNOSUPPORT);
2949 goto out_err;
2952 dprintk("RPC: set up xprt to %s (port %s) via %s\n",
2953 xprt->address_strings[RPC_DISPLAY_ADDR],
2954 xprt->address_strings[RPC_DISPLAY_PORT],
2955 xprt->address_strings[RPC_DISPLAY_PROTO]);
2958 * Once we've associated a backchannel xprt with a connection,
2959 * we want to keep it around as long as the connection lasts,
2960 * in case we need to start using it for a backchannel again;
2961 * this reference won't be dropped until bc_xprt is destroyed.
2963 xprt_get(xprt);
2964 args->bc_xprt->xpt_bc_xprt = xprt;
2965 xprt->bc_xprt = args->bc_xprt;
2966 bc_sock = container_of(args->bc_xprt, struct svc_sock, sk_xprt);
2967 transport->sock = bc_sock->sk_sock;
2968 transport->inet = bc_sock->sk_sk;
2971 * Since we don't want connections for the backchannel, we set
2972 * the xprt status to connected
2974 xprt_set_connected(xprt);
2976 if (try_module_get(THIS_MODULE))
2977 return xprt;
2979 args->bc_xprt->xpt_bc_xprt = NULL;
2980 xprt_put(xprt);
2981 ret = ERR_PTR(-EINVAL);
2982 out_err:
2983 xs_xprt_free(xprt);
2984 return ret;
2987 static struct xprt_class xs_local_transport = {
2988 .list = LIST_HEAD_INIT(xs_local_transport.list),
2989 .name = "named UNIX socket",
2990 .owner = THIS_MODULE,
2991 .ident = XPRT_TRANSPORT_LOCAL,
2992 .setup = xs_setup_local,
2995 static struct xprt_class xs_udp_transport = {
2996 .list = LIST_HEAD_INIT(xs_udp_transport.list),
2997 .name = "udp",
2998 .owner = THIS_MODULE,
2999 .ident = XPRT_TRANSPORT_UDP,
3000 .setup = xs_setup_udp,
3003 static struct xprt_class xs_tcp_transport = {
3004 .list = LIST_HEAD_INIT(xs_tcp_transport.list),
3005 .name = "tcp",
3006 .owner = THIS_MODULE,
3007 .ident = XPRT_TRANSPORT_TCP,
3008 .setup = xs_setup_tcp,
3011 static struct xprt_class xs_bc_tcp_transport = {
3012 .list = LIST_HEAD_INIT(xs_bc_tcp_transport.list),
3013 .name = "tcp NFSv4.1 backchannel",
3014 .owner = THIS_MODULE,
3015 .ident = XPRT_TRANSPORT_BC_TCP,
3016 .setup = xs_setup_bc_tcp,
3020 * init_socket_xprt - set up xprtsock's sysctls, register with RPC client
3023 int init_socket_xprt(void)
3025 #ifdef RPC_DEBUG
3026 if (!sunrpc_table_header)
3027 sunrpc_table_header = register_sysctl_table(sunrpc_table);
3028 #endif
3030 xprt_register_transport(&xs_local_transport);
3031 xprt_register_transport(&xs_udp_transport);
3032 xprt_register_transport(&xs_tcp_transport);
3033 xprt_register_transport(&xs_bc_tcp_transport);
3035 return 0;
3039 * cleanup_socket_xprt - remove xprtsock's sysctls, unregister
3042 void cleanup_socket_xprt(void)
3044 #ifdef RPC_DEBUG
3045 if (sunrpc_table_header) {
3046 unregister_sysctl_table(sunrpc_table_header);
3047 sunrpc_table_header = NULL;
3049 #endif
3051 xprt_unregister_transport(&xs_local_transport);
3052 xprt_unregister_transport(&xs_udp_transport);
3053 xprt_unregister_transport(&xs_tcp_transport);
3054 xprt_unregister_transport(&xs_bc_tcp_transport);
3057 static int param_set_uint_minmax(const char *val,
3058 const struct kernel_param *kp,
3059 unsigned int min, unsigned int max)
3061 unsigned long num;
3062 int ret;
3064 if (!val)
3065 return -EINVAL;
3066 ret = strict_strtoul(val, 0, &num);
3067 if (ret == -EINVAL || num < min || num > max)
3068 return -EINVAL;
3069 *((unsigned int *)kp->arg) = num;
3070 return 0;
3073 static int param_set_portnr(const char *val, const struct kernel_param *kp)
3075 return param_set_uint_minmax(val, kp,
3076 RPC_MIN_RESVPORT,
3077 RPC_MAX_RESVPORT);
3080 static struct kernel_param_ops param_ops_portnr = {
3081 .set = param_set_portnr,
3082 .get = param_get_uint,
3085 #define param_check_portnr(name, p) \
3086 __param_check(name, p, unsigned int);
3088 module_param_named(min_resvport, xprt_min_resvport, portnr, 0644);
3089 module_param_named(max_resvport, xprt_max_resvport, portnr, 0644);
3091 static int param_set_slot_table_size(const char *val,
3092 const struct kernel_param *kp)
3094 return param_set_uint_minmax(val, kp,
3095 RPC_MIN_SLOT_TABLE,
3096 RPC_MAX_SLOT_TABLE);
3099 static struct kernel_param_ops param_ops_slot_table_size = {
3100 .set = param_set_slot_table_size,
3101 .get = param_get_uint,
3104 #define param_check_slot_table_size(name, p) \
3105 __param_check(name, p, unsigned int);
3107 static int param_set_max_slot_table_size(const char *val,
3108 const struct kernel_param *kp)
3110 return param_set_uint_minmax(val, kp,
3111 RPC_MIN_SLOT_TABLE,
3112 RPC_MAX_SLOT_TABLE_LIMIT);
3115 static struct kernel_param_ops param_ops_max_slot_table_size = {
3116 .set = param_set_max_slot_table_size,
3117 .get = param_get_uint,
3120 #define param_check_max_slot_table_size(name, p) \
3121 __param_check(name, p, unsigned int);
3123 module_param_named(tcp_slot_table_entries, xprt_tcp_slot_table_entries,
3124 slot_table_size, 0644);
3125 module_param_named(tcp_max_slot_table_entries, xprt_max_tcp_slot_table_entries,
3126 max_slot_table_size, 0644);
3127 module_param_named(udp_slot_table_entries, xprt_udp_slot_table_entries,
3128 slot_table_size, 0644);