Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net-2.6
[linux-2.6.git] / net / sunrpc / xprtsock.c
1 /*
2  * linux/net/sunrpc/xprtsock.c
3  *
4  * Client-side transport implementation for sockets.
5  *
6  * TCP callback races fixes (C) 1998 Red Hat
7  * TCP send fixes (C) 1998 Red Hat
8  * TCP NFS related read + write fixes
9  *  (C) 1999 Dave Airlie, University of Limerick, Ireland <airlied@linux.ie>
10  *
11  * Rewrite of larges part of the code in order to stabilize TCP stuff.
12  * Fix behaviour when socket buffer is full.
13  *  (C) 1999 Trond Myklebust <trond.myklebust@fys.uio.no>
14  *
15  * IP socket transport implementation, (C) 2005 Chuck Lever <cel@netapp.com>
16  *
17  * IPv6 support contributed by Gilles Quillard, Bull Open Source, 2005.
18  *   <gilles.quillard@bull.net>
19  */
20
21 #include <linux/types.h>
22 #include <linux/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
41
42 #include <net/sock.h>
43 #include <net/checksum.h>
44 #include <net/udp.h>
45 #include <net/tcp.h>
46
47 #include "sunrpc.h"
48 /*
49  * xprtsock tunables
50  */
51 unsigned int xprt_udp_slot_table_entries = RPC_DEF_SLOT_TABLE;
52 unsigned int xprt_tcp_slot_table_entries = RPC_DEF_SLOT_TABLE;
53
54 unsigned int xprt_min_resvport = RPC_DEF_MIN_RESVPORT;
55 unsigned int xprt_max_resvport = RPC_DEF_MAX_RESVPORT;
56
57 #define XS_TCP_LINGER_TO        (15U * HZ)
58 static unsigned int xs_tcp_fin_timeout __read_mostly = XS_TCP_LINGER_TO;
59
60 /*
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.
64  *
65  * We simply need to make sure that we don't collide with
66  * someone else's file names!
67  */
68
69 #ifdef RPC_DEBUG
70
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;
75
76 static struct ctl_table_header *sunrpc_table_header;
77
78 /*
79  * FIXME: changing the UDP slot table size should also resize the UDP
80  *        socket buffers for existing UDP transports
81  */
82 static ctl_table xs_tunables_table[] = {
83         {
84                 .procname       = "udp_slot_table_entries",
85                 .data           = &xprt_udp_slot_table_entries,
86                 .maxlen         = sizeof(unsigned int),
87                 .mode           = 0644,
88                 .proc_handler   = proc_dointvec_minmax,
89                 .extra1         = &min_slot_table_size,
90                 .extra2         = &max_slot_table_size
91         },
92         {
93                 .procname       = "tcp_slot_table_entries",
94                 .data           = &xprt_tcp_slot_table_entries,
95                 .maxlen         = sizeof(unsigned int),
96                 .mode           = 0644,
97                 .proc_handler   = proc_dointvec_minmax,
98                 .extra1         = &min_slot_table_size,
99                 .extra2         = &max_slot_table_size
100         },
101         {
102                 .procname       = "min_resvport",
103                 .data           = &xprt_min_resvport,
104                 .maxlen         = sizeof(unsigned int),
105                 .mode           = 0644,
106                 .proc_handler   = proc_dointvec_minmax,
107                 .extra1         = &xprt_min_resvport_limit,
108                 .extra2         = &xprt_max_resvport_limit
109         },
110         {
111                 .procname       = "max_resvport",
112                 .data           = &xprt_max_resvport,
113                 .maxlen         = sizeof(unsigned int),
114                 .mode           = 0644,
115                 .proc_handler   = proc_dointvec_minmax,
116                 .extra1         = &xprt_min_resvport_limit,
117                 .extra2         = &xprt_max_resvport_limit
118         },
119         {
120                 .procname       = "tcp_fin_timeout",
121                 .data           = &xs_tcp_fin_timeout,
122                 .maxlen         = sizeof(xs_tcp_fin_timeout),
123                 .mode           = 0644,
124                 .proc_handler   = proc_dointvec_jiffies,
125         },
126         { },
127 };
128
129 static ctl_table sunrpc_table[] = {
130         {
131                 .procname       = "sunrpc",
132                 .mode           = 0555,
133                 .child          = xs_tunables_table
134         },
135         { },
136 };
137
138 #endif
139
140 /*
141  * Time out for an RPC UDP socket connect.  UDP socket connects are
142  * synchronous, but we set a timeout anyway in case of resource
143  * exhaustion on the local host.
144  */
145 #define XS_UDP_CONN_TO          (5U * HZ)
146
147 /*
148  * Wait duration for an RPC TCP connection to be established.  Solaris
149  * NFS over TCP uses 60 seconds, for example, which is in line with how
150  * long a server takes to reboot.
151  */
152 #define XS_TCP_CONN_TO          (60U * HZ)
153
154 /*
155  * Wait duration for a reply from the RPC portmapper.
156  */
157 #define XS_BIND_TO              (60U * HZ)
158
159 /*
160  * Delay if a UDP socket connect error occurs.  This is most likely some
161  * kind of resource problem on the local host.
162  */
163 #define XS_UDP_REEST_TO         (2U * HZ)
164
165 /*
166  * The reestablish timeout allows clients to delay for a bit before attempting
167  * to reconnect to a server that just dropped our connection.
168  *
169  * We implement an exponential backoff when trying to reestablish a TCP
170  * transport connection with the server.  Some servers like to drop a TCP
171  * connection when they are overworked, so we start with a short timeout and
172  * increase over time if the server is down or not responding.
173  */
174 #define XS_TCP_INIT_REEST_TO    (3U * HZ)
175 #define XS_TCP_MAX_REEST_TO     (5U * 60 * HZ)
176
177 /*
178  * TCP idle timeout; client drops the transport socket if it is idle
179  * for this long.  Note that we also timeout UDP sockets to prevent
180  * holding port numbers when there is no RPC traffic.
181  */
182 #define XS_IDLE_DISC_TO         (5U * 60 * HZ)
183
184 #ifdef RPC_DEBUG
185 # undef  RPC_DEBUG_DATA
186 # define RPCDBG_FACILITY        RPCDBG_TRANS
187 #endif
188
189 #ifdef RPC_DEBUG_DATA
190 static void xs_pktdump(char *msg, u32 *packet, unsigned int count)
191 {
192         u8 *buf = (u8 *) packet;
193         int j;
194
195         dprintk("RPC:       %s\n", msg);
196         for (j = 0; j < count && j < 128; j += 4) {
197                 if (!(j & 31)) {
198                         if (j)
199                                 dprintk("\n");
200                         dprintk("0x%04x ", j);
201                 }
202                 dprintk("%02x%02x%02x%02x ",
203                         buf[j], buf[j+1], buf[j+2], buf[j+3]);
204         }
205         dprintk("\n");
206 }
207 #else
208 static inline void xs_pktdump(char *msg, u32 *packet, unsigned int count)
209 {
210         /* NOP */
211 }
212 #endif
213
214 struct sock_xprt {
215         struct rpc_xprt         xprt;
216
217         /*
218          * Network layer
219          */
220         struct socket *         sock;
221         struct sock *           inet;
222
223         /*
224          * State of TCP reply receive
225          */
226         __be32                  tcp_fraghdr,
227                                 tcp_xid;
228
229         u32                     tcp_offset,
230                                 tcp_reclen;
231
232         unsigned long           tcp_copied,
233                                 tcp_flags;
234
235         /*
236          * Connection of transports
237          */
238         struct delayed_work     connect_worker;
239         struct sockaddr_storage srcaddr;
240         unsigned short          srcport;
241
242         /*
243          * UDP socket buffer size parameters
244          */
245         size_t                  rcvsize,
246                                 sndsize;
247
248         /*
249          * Saved socket callback addresses
250          */
251         void                    (*old_data_ready)(struct sock *, int);
252         void                    (*old_state_change)(struct sock *);
253         void                    (*old_write_space)(struct sock *);
254         void                    (*old_error_report)(struct sock *);
255 };
256
257 /*
258  * TCP receive state flags
259  */
260 #define TCP_RCV_LAST_FRAG       (1UL << 0)
261 #define TCP_RCV_COPY_FRAGHDR    (1UL << 1)
262 #define TCP_RCV_COPY_XID        (1UL << 2)
263 #define TCP_RCV_COPY_DATA       (1UL << 3)
264 #define TCP_RCV_READ_CALLDIR    (1UL << 4)
265 #define TCP_RCV_COPY_CALLDIR    (1UL << 5)
266
267 /*
268  * TCP RPC flags
269  */
270 #define TCP_RPC_REPLY           (1UL << 6)
271
272 static inline struct sockaddr *xs_addr(struct rpc_xprt *xprt)
273 {
274         return (struct sockaddr *) &xprt->addr;
275 }
276
277 static inline struct sockaddr_in *xs_addr_in(struct rpc_xprt *xprt)
278 {
279         return (struct sockaddr_in *) &xprt->addr;
280 }
281
282 static inline struct sockaddr_in6 *xs_addr_in6(struct rpc_xprt *xprt)
283 {
284         return (struct sockaddr_in6 *) &xprt->addr;
285 }
286
287 static void xs_format_common_peer_addresses(struct rpc_xprt *xprt)
288 {
289         struct sockaddr *sap = xs_addr(xprt);
290         struct sockaddr_in6 *sin6;
291         struct sockaddr_in *sin;
292         char buf[128];
293
294         (void)rpc_ntop(sap, buf, sizeof(buf));
295         xprt->address_strings[RPC_DISPLAY_ADDR] = kstrdup(buf, GFP_KERNEL);
296
297         switch (sap->sa_family) {
298         case AF_INET:
299                 sin = xs_addr_in(xprt);
300                 snprintf(buf, sizeof(buf), "%08x", ntohl(sin->sin_addr.s_addr));
301                 break;
302         case AF_INET6:
303                 sin6 = xs_addr_in6(xprt);
304                 snprintf(buf, sizeof(buf), "%pi6", &sin6->sin6_addr);
305                 break;
306         default:
307                 BUG();
308         }
309         xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = kstrdup(buf, GFP_KERNEL);
310 }
311
312 static void xs_format_common_peer_ports(struct rpc_xprt *xprt)
313 {
314         struct sockaddr *sap = xs_addr(xprt);
315         char buf[128];
316
317         snprintf(buf, sizeof(buf), "%u", rpc_get_port(sap));
318         xprt->address_strings[RPC_DISPLAY_PORT] = kstrdup(buf, GFP_KERNEL);
319
320         snprintf(buf, sizeof(buf), "%4hx", rpc_get_port(sap));
321         xprt->address_strings[RPC_DISPLAY_HEX_PORT] = kstrdup(buf, GFP_KERNEL);
322 }
323
324 static void xs_format_peer_addresses(struct rpc_xprt *xprt,
325                                      const char *protocol,
326                                      const char *netid)
327 {
328         xprt->address_strings[RPC_DISPLAY_PROTO] = protocol;
329         xprt->address_strings[RPC_DISPLAY_NETID] = netid;
330         xs_format_common_peer_addresses(xprt);
331         xs_format_common_peer_ports(xprt);
332 }
333
334 static void xs_update_peer_port(struct rpc_xprt *xprt)
335 {
336         kfree(xprt->address_strings[RPC_DISPLAY_HEX_PORT]);
337         kfree(xprt->address_strings[RPC_DISPLAY_PORT]);
338
339         xs_format_common_peer_ports(xprt);
340 }
341
342 static void xs_free_peer_addresses(struct rpc_xprt *xprt)
343 {
344         unsigned int i;
345
346         for (i = 0; i < RPC_DISPLAY_MAX; i++)
347                 switch (i) {
348                 case RPC_DISPLAY_PROTO:
349                 case RPC_DISPLAY_NETID:
350                         continue;
351                 default:
352                         kfree(xprt->address_strings[i]);
353                 }
354 }
355
356 #define XS_SENDMSG_FLAGS        (MSG_DONTWAIT | MSG_NOSIGNAL)
357
358 static int xs_send_kvec(struct socket *sock, struct sockaddr *addr, int addrlen, struct kvec *vec, unsigned int base, int more)
359 {
360         struct msghdr msg = {
361                 .msg_name       = addr,
362                 .msg_namelen    = addrlen,
363                 .msg_flags      = XS_SENDMSG_FLAGS | (more ? MSG_MORE : 0),
364         };
365         struct kvec iov = {
366                 .iov_base       = vec->iov_base + base,
367                 .iov_len        = vec->iov_len - base,
368         };
369
370         if (iov.iov_len != 0)
371                 return kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len);
372         return kernel_sendmsg(sock, &msg, NULL, 0, 0);
373 }
374
375 static int xs_send_pagedata(struct socket *sock, struct xdr_buf *xdr, unsigned int base, int more)
376 {
377         struct page **ppage;
378         unsigned int remainder;
379         int err, sent = 0;
380
381         remainder = xdr->page_len - base;
382         base += xdr->page_base;
383         ppage = xdr->pages + (base >> PAGE_SHIFT);
384         base &= ~PAGE_MASK;
385         for(;;) {
386                 unsigned int len = min_t(unsigned int, PAGE_SIZE - base, remainder);
387                 int flags = XS_SENDMSG_FLAGS;
388
389                 remainder -= len;
390                 if (remainder != 0 || more)
391                         flags |= MSG_MORE;
392                 err = sock->ops->sendpage(sock, *ppage, base, len, flags);
393                 if (remainder == 0 || err != len)
394                         break;
395                 sent += err;
396                 ppage++;
397                 base = 0;
398         }
399         if (sent == 0)
400                 return err;
401         if (err > 0)
402                 sent += err;
403         return sent;
404 }
405
406 /**
407  * xs_sendpages - write pages directly to a socket
408  * @sock: socket to send on
409  * @addr: UDP only -- address of destination
410  * @addrlen: UDP only -- length of destination address
411  * @xdr: buffer containing this request
412  * @base: starting position in the buffer
413  *
414  */
415 static int xs_sendpages(struct socket *sock, struct sockaddr *addr, int addrlen, struct xdr_buf *xdr, unsigned int base)
416 {
417         unsigned int remainder = xdr->len - base;
418         int err, sent = 0;
419
420         if (unlikely(!sock))
421                 return -ENOTSOCK;
422
423         clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags);
424         if (base != 0) {
425                 addr = NULL;
426                 addrlen = 0;
427         }
428
429         if (base < xdr->head[0].iov_len || addr != NULL) {
430                 unsigned int len = xdr->head[0].iov_len - base;
431                 remainder -= len;
432                 err = xs_send_kvec(sock, addr, addrlen, &xdr->head[0], base, remainder != 0);
433                 if (remainder == 0 || err != len)
434                         goto out;
435                 sent += err;
436                 base = 0;
437         } else
438                 base -= xdr->head[0].iov_len;
439
440         if (base < xdr->page_len) {
441                 unsigned int len = xdr->page_len - base;
442                 remainder -= len;
443                 err = xs_send_pagedata(sock, xdr, base, remainder != 0);
444                 if (remainder == 0 || err != len)
445                         goto out;
446                 sent += err;
447                 base = 0;
448         } else
449                 base -= xdr->page_len;
450
451         if (base >= xdr->tail[0].iov_len)
452                 return sent;
453         err = xs_send_kvec(sock, NULL, 0, &xdr->tail[0], base, 0);
454 out:
455         if (sent == 0)
456                 return err;
457         if (err > 0)
458                 sent += err;
459         return sent;
460 }
461
462 static void xs_nospace_callback(struct rpc_task *task)
463 {
464         struct sock_xprt *transport = container_of(task->tk_rqstp->rq_xprt, struct sock_xprt, xprt);
465
466         transport->inet->sk_write_pending--;
467         clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
468 }
469
470 /**
471  * xs_nospace - place task on wait queue if transmit was incomplete
472  * @task: task to put to sleep
473  *
474  */
475 static int xs_nospace(struct rpc_task *task)
476 {
477         struct rpc_rqst *req = task->tk_rqstp;
478         struct rpc_xprt *xprt = req->rq_xprt;
479         struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
480         int ret = 0;
481
482         dprintk("RPC: %5u xmit incomplete (%u left of %u)\n",
483                         task->tk_pid, req->rq_slen - req->rq_bytes_sent,
484                         req->rq_slen);
485
486         /* Protect against races with write_space */
487         spin_lock_bh(&xprt->transport_lock);
488
489         /* Don't race with disconnect */
490         if (xprt_connected(xprt)) {
491                 if (test_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags)) {
492                         ret = -EAGAIN;
493                         /*
494                          * Notify TCP that we're limited by the application
495                          * window size
496                          */
497                         set_bit(SOCK_NOSPACE, &transport->sock->flags);
498                         transport->inet->sk_write_pending++;
499                         /* ...and wait for more buffer space */
500                         xprt_wait_for_buffer_space(task, xs_nospace_callback);
501                 }
502         } else {
503                 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
504                 ret = -ENOTCONN;
505         }
506
507         spin_unlock_bh(&xprt->transport_lock);
508         return ret;
509 }
510
511 /**
512  * xs_udp_send_request - write an RPC request to a UDP socket
513  * @task: address of RPC task that manages the state of an RPC request
514  *
515  * Return values:
516  *        0:    The request has been sent
517  *   EAGAIN:    The socket was blocked, please call again later to
518  *              complete the request
519  * ENOTCONN:    Caller needs to invoke connect logic then call again
520  *    other:    Some other error occured, the request was not sent
521  */
522 static int xs_udp_send_request(struct rpc_task *task)
523 {
524         struct rpc_rqst *req = task->tk_rqstp;
525         struct rpc_xprt *xprt = req->rq_xprt;
526         struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
527         struct xdr_buf *xdr = &req->rq_snd_buf;
528         int status;
529
530         xs_pktdump("packet data:",
531                                 req->rq_svec->iov_base,
532                                 req->rq_svec->iov_len);
533
534         if (!xprt_bound(xprt))
535                 return -ENOTCONN;
536         status = xs_sendpages(transport->sock,
537                               xs_addr(xprt),
538                               xprt->addrlen, xdr,
539                               req->rq_bytes_sent);
540
541         dprintk("RPC:       xs_udp_send_request(%u) = %d\n",
542                         xdr->len - req->rq_bytes_sent, status);
543
544         if (status >= 0) {
545                 task->tk_bytes_sent += status;
546                 if (status >= req->rq_slen)
547                         return 0;
548                 /* Still some bytes left; set up for a retry later. */
549                 status = -EAGAIN;
550         }
551         if (!transport->sock)
552                 goto out;
553
554         switch (status) {
555         case -ENOTSOCK:
556                 status = -ENOTCONN;
557                 /* Should we call xs_close() here? */
558                 break;
559         case -EAGAIN:
560                 status = xs_nospace(task);
561                 break;
562         default:
563                 dprintk("RPC:       sendmsg returned unrecognized error %d\n",
564                         -status);
565         case -ENETUNREACH:
566         case -EPIPE:
567         case -ECONNREFUSED:
568                 /* When the server has died, an ICMP port unreachable message
569                  * prompts ECONNREFUSED. */
570                 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
571         }
572 out:
573         return status;
574 }
575
576 /**
577  * xs_tcp_shutdown - gracefully shut down a TCP socket
578  * @xprt: transport
579  *
580  * Initiates a graceful shutdown of the TCP socket by calling the
581  * equivalent of shutdown(SHUT_WR);
582  */
583 static void xs_tcp_shutdown(struct rpc_xprt *xprt)
584 {
585         struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
586         struct socket *sock = transport->sock;
587
588         if (sock != NULL)
589                 kernel_sock_shutdown(sock, SHUT_WR);
590 }
591
592 static inline void xs_encode_tcp_record_marker(struct xdr_buf *buf)
593 {
594         u32 reclen = buf->len - sizeof(rpc_fraghdr);
595         rpc_fraghdr *base = buf->head[0].iov_base;
596         *base = htonl(RPC_LAST_STREAM_FRAGMENT | reclen);
597 }
598
599 /**
600  * xs_tcp_send_request - write an RPC request to a TCP socket
601  * @task: address of RPC task that manages the state of an RPC request
602  *
603  * Return values:
604  *        0:    The request has been sent
605  *   EAGAIN:    The socket was blocked, please call again later to
606  *              complete the request
607  * ENOTCONN:    Caller needs to invoke connect logic then call again
608  *    other:    Some other error occured, the request was not sent
609  *
610  * XXX: In the case of soft timeouts, should we eventually give up
611  *      if sendmsg is not able to make progress?
612  */
613 static int xs_tcp_send_request(struct rpc_task *task)
614 {
615         struct rpc_rqst *req = task->tk_rqstp;
616         struct rpc_xprt *xprt = req->rq_xprt;
617         struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
618         struct xdr_buf *xdr = &req->rq_snd_buf;
619         int status;
620
621         xs_encode_tcp_record_marker(&req->rq_snd_buf);
622
623         xs_pktdump("packet data:",
624                                 req->rq_svec->iov_base,
625                                 req->rq_svec->iov_len);
626
627         /* Continue transmitting the packet/record. We must be careful
628          * to cope with writespace callbacks arriving _after_ we have
629          * called sendmsg(). */
630         while (1) {
631                 status = xs_sendpages(transport->sock,
632                                         NULL, 0, xdr, req->rq_bytes_sent);
633
634                 dprintk("RPC:       xs_tcp_send_request(%u) = %d\n",
635                                 xdr->len - req->rq_bytes_sent, status);
636
637                 if (unlikely(status < 0))
638                         break;
639
640                 /* If we've sent the entire packet, immediately
641                  * reset the count of bytes sent. */
642                 req->rq_bytes_sent += status;
643                 task->tk_bytes_sent += status;
644                 if (likely(req->rq_bytes_sent >= req->rq_slen)) {
645                         req->rq_bytes_sent = 0;
646                         return 0;
647                 }
648
649                 if (status != 0)
650                         continue;
651                 status = -EAGAIN;
652                 break;
653         }
654         if (!transport->sock)
655                 goto out;
656
657         switch (status) {
658         case -ENOTSOCK:
659                 status = -ENOTCONN;
660                 /* Should we call xs_close() here? */
661                 break;
662         case -EAGAIN:
663                 status = xs_nospace(task);
664                 break;
665         default:
666                 dprintk("RPC:       sendmsg returned unrecognized error %d\n",
667                         -status);
668         case -ECONNRESET:
669         case -EPIPE:
670                 xs_tcp_shutdown(xprt);
671         case -ECONNREFUSED:
672         case -ENOTCONN:
673                 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
674         }
675 out:
676         return status;
677 }
678
679 /**
680  * xs_tcp_release_xprt - clean up after a tcp transmission
681  * @xprt: transport
682  * @task: rpc task
683  *
684  * This cleans up if an error causes us to abort the transmission of a request.
685  * In this case, the socket may need to be reset in order to avoid confusing
686  * the server.
687  */
688 static void xs_tcp_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
689 {
690         struct rpc_rqst *req;
691
692         if (task != xprt->snd_task)
693                 return;
694         if (task == NULL)
695                 goto out_release;
696         req = task->tk_rqstp;
697         if (req->rq_bytes_sent == 0)
698                 goto out_release;
699         if (req->rq_bytes_sent == req->rq_snd_buf.len)
700                 goto out_release;
701         set_bit(XPRT_CLOSE_WAIT, &task->tk_xprt->state);
702 out_release:
703         xprt_release_xprt(xprt, task);
704 }
705
706 static void xs_save_old_callbacks(struct sock_xprt *transport, struct sock *sk)
707 {
708         transport->old_data_ready = sk->sk_data_ready;
709         transport->old_state_change = sk->sk_state_change;
710         transport->old_write_space = sk->sk_write_space;
711         transport->old_error_report = sk->sk_error_report;
712 }
713
714 static void xs_restore_old_callbacks(struct sock_xprt *transport, struct sock *sk)
715 {
716         sk->sk_data_ready = transport->old_data_ready;
717         sk->sk_state_change = transport->old_state_change;
718         sk->sk_write_space = transport->old_write_space;
719         sk->sk_error_report = transport->old_error_report;
720 }
721
722 static void xs_reset_transport(struct sock_xprt *transport)
723 {
724         struct socket *sock = transport->sock;
725         struct sock *sk = transport->inet;
726
727         if (sk == NULL)
728                 return;
729
730         write_lock_bh(&sk->sk_callback_lock);
731         transport->inet = NULL;
732         transport->sock = NULL;
733
734         sk->sk_user_data = NULL;
735
736         xs_restore_old_callbacks(transport, sk);
737         write_unlock_bh(&sk->sk_callback_lock);
738
739         sk->sk_no_check = 0;
740
741         sock_release(sock);
742 }
743
744 /**
745  * xs_close - close a socket
746  * @xprt: transport
747  *
748  * This is used when all requests are complete; ie, no DRC state remains
749  * on the server we want to save.
750  *
751  * The caller _must_ be holding XPRT_LOCKED in order to avoid issues with
752  * xs_reset_transport() zeroing the socket from underneath a writer.
753  */
754 static void xs_close(struct rpc_xprt *xprt)
755 {
756         struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
757
758         dprintk("RPC:       xs_close xprt %p\n", xprt);
759
760         xs_reset_transport(transport);
761         xprt->reestablish_timeout = 0;
762
763         smp_mb__before_clear_bit();
764         clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
765         clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
766         clear_bit(XPRT_CLOSING, &xprt->state);
767         smp_mb__after_clear_bit();
768         xprt_disconnect_done(xprt);
769 }
770
771 static void xs_tcp_close(struct rpc_xprt *xprt)
772 {
773         if (test_and_clear_bit(XPRT_CONNECTION_CLOSE, &xprt->state))
774                 xs_close(xprt);
775         else
776                 xs_tcp_shutdown(xprt);
777 }
778
779 /**
780  * xs_destroy - prepare to shutdown a transport
781  * @xprt: doomed transport
782  *
783  */
784 static void xs_destroy(struct rpc_xprt *xprt)
785 {
786         struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
787
788         dprintk("RPC:       xs_destroy xprt %p\n", xprt);
789
790         cancel_rearming_delayed_work(&transport->connect_worker);
791
792         xs_close(xprt);
793         xs_free_peer_addresses(xprt);
794         kfree(xprt->slot);
795         kfree(xprt);
796         module_put(THIS_MODULE);
797 }
798
799 static inline struct rpc_xprt *xprt_from_sock(struct sock *sk)
800 {
801         return (struct rpc_xprt *) sk->sk_user_data;
802 }
803
804 /**
805  * xs_udp_data_ready - "data ready" callback for UDP sockets
806  * @sk: socket with data to read
807  * @len: how much data to read
808  *
809  */
810 static void xs_udp_data_ready(struct sock *sk, int len)
811 {
812         struct rpc_task *task;
813         struct rpc_xprt *xprt;
814         struct rpc_rqst *rovr;
815         struct sk_buff *skb;
816         int err, repsize, copied;
817         u32 _xid;
818         __be32 *xp;
819
820         read_lock(&sk->sk_callback_lock);
821         dprintk("RPC:       xs_udp_data_ready...\n");
822         if (!(xprt = xprt_from_sock(sk)))
823                 goto out;
824
825         if ((skb = skb_recv_datagram(sk, 0, 1, &err)) == NULL)
826                 goto out;
827
828         if (xprt->shutdown)
829                 goto dropit;
830
831         repsize = skb->len - sizeof(struct udphdr);
832         if (repsize < 4) {
833                 dprintk("RPC:       impossible RPC reply size %d!\n", repsize);
834                 goto dropit;
835         }
836
837         /* Copy the XID from the skb... */
838         xp = skb_header_pointer(skb, sizeof(struct udphdr),
839                                 sizeof(_xid), &_xid);
840         if (xp == NULL)
841                 goto dropit;
842
843         /* Look up and lock the request corresponding to the given XID */
844         spin_lock(&xprt->transport_lock);
845         rovr = xprt_lookup_rqst(xprt, *xp);
846         if (!rovr)
847                 goto out_unlock;
848         task = rovr->rq_task;
849
850         if ((copied = rovr->rq_private_buf.buflen) > repsize)
851                 copied = repsize;
852
853         /* Suck it into the iovec, verify checksum if not done by hw. */
854         if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) {
855                 UDPX_INC_STATS_BH(sk, UDP_MIB_INERRORS);
856                 goto out_unlock;
857         }
858
859         UDPX_INC_STATS_BH(sk, UDP_MIB_INDATAGRAMS);
860
861         /* Something worked... */
862         dst_confirm(skb_dst(skb));
863
864         xprt_adjust_cwnd(task, copied);
865         xprt_update_rtt(task);
866         xprt_complete_rqst(task, copied);
867
868  out_unlock:
869         spin_unlock(&xprt->transport_lock);
870  dropit:
871         skb_free_datagram(sk, skb);
872  out:
873         read_unlock(&sk->sk_callback_lock);
874 }
875
876 static inline void xs_tcp_read_fraghdr(struct rpc_xprt *xprt, struct xdr_skb_reader *desc)
877 {
878         struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
879         size_t len, used;
880         char *p;
881
882         p = ((char *) &transport->tcp_fraghdr) + transport->tcp_offset;
883         len = sizeof(transport->tcp_fraghdr) - transport->tcp_offset;
884         used = xdr_skb_read_bits(desc, p, len);
885         transport->tcp_offset += used;
886         if (used != len)
887                 return;
888
889         transport->tcp_reclen = ntohl(transport->tcp_fraghdr);
890         if (transport->tcp_reclen & RPC_LAST_STREAM_FRAGMENT)
891                 transport->tcp_flags |= TCP_RCV_LAST_FRAG;
892         else
893                 transport->tcp_flags &= ~TCP_RCV_LAST_FRAG;
894         transport->tcp_reclen &= RPC_FRAGMENT_SIZE_MASK;
895
896         transport->tcp_flags &= ~TCP_RCV_COPY_FRAGHDR;
897         transport->tcp_offset = 0;
898
899         /* Sanity check of the record length */
900         if (unlikely(transport->tcp_reclen < 8)) {
901                 dprintk("RPC:       invalid TCP record fragment length\n");
902                 xprt_force_disconnect(xprt);
903                 return;
904         }
905         dprintk("RPC:       reading TCP record fragment of length %d\n",
906                         transport->tcp_reclen);
907 }
908
909 static void xs_tcp_check_fraghdr(struct sock_xprt *transport)
910 {
911         if (transport->tcp_offset == transport->tcp_reclen) {
912                 transport->tcp_flags |= TCP_RCV_COPY_FRAGHDR;
913                 transport->tcp_offset = 0;
914                 if (transport->tcp_flags & TCP_RCV_LAST_FRAG) {
915                         transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
916                         transport->tcp_flags |= TCP_RCV_COPY_XID;
917                         transport->tcp_copied = 0;
918                 }
919         }
920 }
921
922 static inline void xs_tcp_read_xid(struct sock_xprt *transport, struct xdr_skb_reader *desc)
923 {
924         size_t len, used;
925         char *p;
926
927         len = sizeof(transport->tcp_xid) - transport->tcp_offset;
928         dprintk("RPC:       reading XID (%Zu bytes)\n", len);
929         p = ((char *) &transport->tcp_xid) + transport->tcp_offset;
930         used = xdr_skb_read_bits(desc, p, len);
931         transport->tcp_offset += used;
932         if (used != len)
933                 return;
934         transport->tcp_flags &= ~TCP_RCV_COPY_XID;
935         transport->tcp_flags |= TCP_RCV_READ_CALLDIR;
936         transport->tcp_copied = 4;
937         dprintk("RPC:       reading %s XID %08x\n",
938                         (transport->tcp_flags & TCP_RPC_REPLY) ? "reply for"
939                                                               : "request with",
940                         ntohl(transport->tcp_xid));
941         xs_tcp_check_fraghdr(transport);
942 }
943
944 static inline void xs_tcp_read_calldir(struct sock_xprt *transport,
945                                        struct xdr_skb_reader *desc)
946 {
947         size_t len, used;
948         u32 offset;
949         __be32  calldir;
950
951         /*
952          * We want transport->tcp_offset to be 8 at the end of this routine
953          * (4 bytes for the xid and 4 bytes for the call/reply flag).
954          * When this function is called for the first time,
955          * transport->tcp_offset is 4 (after having already read the xid).
956          */
957         offset = transport->tcp_offset - sizeof(transport->tcp_xid);
958         len = sizeof(calldir) - offset;
959         dprintk("RPC:       reading CALL/REPLY flag (%Zu bytes)\n", len);
960         used = xdr_skb_read_bits(desc, &calldir, len);
961         transport->tcp_offset += used;
962         if (used != len)
963                 return;
964         transport->tcp_flags &= ~TCP_RCV_READ_CALLDIR;
965         transport->tcp_flags |= TCP_RCV_COPY_CALLDIR;
966         transport->tcp_flags |= TCP_RCV_COPY_DATA;
967         /*
968          * We don't yet have the XDR buffer, so we will write the calldir
969          * out after we get the buffer from the 'struct rpc_rqst'
970          */
971         if (ntohl(calldir) == RPC_REPLY)
972                 transport->tcp_flags |= TCP_RPC_REPLY;
973         else
974                 transport->tcp_flags &= ~TCP_RPC_REPLY;
975         dprintk("RPC:       reading %s CALL/REPLY flag %08x\n",
976                         (transport->tcp_flags & TCP_RPC_REPLY) ?
977                                 "reply for" : "request with", calldir);
978         xs_tcp_check_fraghdr(transport);
979 }
980
981 static inline void xs_tcp_read_common(struct rpc_xprt *xprt,
982                                      struct xdr_skb_reader *desc,
983                                      struct rpc_rqst *req)
984 {
985         struct sock_xprt *transport =
986                                 container_of(xprt, struct sock_xprt, xprt);
987         struct xdr_buf *rcvbuf;
988         size_t len;
989         ssize_t r;
990
991         rcvbuf = &req->rq_private_buf;
992
993         if (transport->tcp_flags & TCP_RCV_COPY_CALLDIR) {
994                 /*
995                  * Save the RPC direction in the XDR buffer
996                  */
997                 __be32  calldir = transport->tcp_flags & TCP_RPC_REPLY ?
998                                         htonl(RPC_REPLY) : 0;
999
1000                 memcpy(rcvbuf->head[0].iov_base + transport->tcp_copied,
1001                         &calldir, sizeof(calldir));
1002                 transport->tcp_copied += sizeof(calldir);
1003                 transport->tcp_flags &= ~TCP_RCV_COPY_CALLDIR;
1004         }
1005
1006         len = desc->count;
1007         if (len > transport->tcp_reclen - transport->tcp_offset) {
1008                 struct xdr_skb_reader my_desc;
1009
1010                 len = transport->tcp_reclen - transport->tcp_offset;
1011                 memcpy(&my_desc, desc, sizeof(my_desc));
1012                 my_desc.count = len;
1013                 r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
1014                                           &my_desc, xdr_skb_read_bits);
1015                 desc->count -= r;
1016                 desc->offset += r;
1017         } else
1018                 r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
1019                                           desc, xdr_skb_read_bits);
1020
1021         if (r > 0) {
1022                 transport->tcp_copied += r;
1023                 transport->tcp_offset += r;
1024         }
1025         if (r != len) {
1026                 /* Error when copying to the receive buffer,
1027                  * usually because we weren't able to allocate
1028                  * additional buffer pages. All we can do now
1029                  * is turn off TCP_RCV_COPY_DATA, so the request
1030                  * will not receive any additional updates,
1031                  * and time out.
1032                  * Any remaining data from this record will
1033                  * be discarded.
1034                  */
1035                 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1036                 dprintk("RPC:       XID %08x truncated request\n",
1037                                 ntohl(transport->tcp_xid));
1038                 dprintk("RPC:       xprt = %p, tcp_copied = %lu, "
1039                                 "tcp_offset = %u, tcp_reclen = %u\n",
1040                                 xprt, transport->tcp_copied,
1041                                 transport->tcp_offset, transport->tcp_reclen);
1042                 return;
1043         }
1044
1045         dprintk("RPC:       XID %08x read %Zd bytes\n",
1046                         ntohl(transport->tcp_xid), r);
1047         dprintk("RPC:       xprt = %p, tcp_copied = %lu, tcp_offset = %u, "
1048                         "tcp_reclen = %u\n", xprt, transport->tcp_copied,
1049                         transport->tcp_offset, transport->tcp_reclen);
1050
1051         if (transport->tcp_copied == req->rq_private_buf.buflen)
1052                 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1053         else if (transport->tcp_offset == transport->tcp_reclen) {
1054                 if (transport->tcp_flags & TCP_RCV_LAST_FRAG)
1055                         transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1056         }
1057
1058         return;
1059 }
1060
1061 /*
1062  * Finds the request corresponding to the RPC xid and invokes the common
1063  * tcp read code to read the data.
1064  */
1065 static inline int xs_tcp_read_reply(struct rpc_xprt *xprt,
1066                                     struct xdr_skb_reader *desc)
1067 {
1068         struct sock_xprt *transport =
1069                                 container_of(xprt, struct sock_xprt, xprt);
1070         struct rpc_rqst *req;
1071
1072         dprintk("RPC:       read reply XID %08x\n", ntohl(transport->tcp_xid));
1073
1074         /* Find and lock the request corresponding to this xid */
1075         spin_lock(&xprt->transport_lock);
1076         req = xprt_lookup_rqst(xprt, transport->tcp_xid);
1077         if (!req) {
1078                 dprintk("RPC:       XID %08x request not found!\n",
1079                                 ntohl(transport->tcp_xid));
1080                 spin_unlock(&xprt->transport_lock);
1081                 return -1;
1082         }
1083
1084         xs_tcp_read_common(xprt, desc, req);
1085
1086         if (!(transport->tcp_flags & TCP_RCV_COPY_DATA))
1087                 xprt_complete_rqst(req->rq_task, transport->tcp_copied);
1088
1089         spin_unlock(&xprt->transport_lock);
1090         return 0;
1091 }
1092
1093 #if defined(CONFIG_NFS_V4_1)
1094 /*
1095  * Obtains an rpc_rqst previously allocated and invokes the common
1096  * tcp read code to read the data.  The result is placed in the callback
1097  * queue.
1098  * If we're unable to obtain the rpc_rqst we schedule the closing of the
1099  * connection and return -1.
1100  */
1101 static inline int xs_tcp_read_callback(struct rpc_xprt *xprt,
1102                                        struct xdr_skb_reader *desc)
1103 {
1104         struct sock_xprt *transport =
1105                                 container_of(xprt, struct sock_xprt, xprt);
1106         struct rpc_rqst *req;
1107
1108         req = xprt_alloc_bc_request(xprt);
1109         if (req == NULL) {
1110                 printk(KERN_WARNING "Callback slot table overflowed\n");
1111                 xprt_force_disconnect(xprt);
1112                 return -1;
1113         }
1114
1115         req->rq_xid = transport->tcp_xid;
1116         dprintk("RPC:       read callback  XID %08x\n", ntohl(req->rq_xid));
1117         xs_tcp_read_common(xprt, desc, req);
1118
1119         if (!(transport->tcp_flags & TCP_RCV_COPY_DATA)) {
1120                 struct svc_serv *bc_serv = xprt->bc_serv;
1121
1122                 /*
1123                  * Add callback request to callback list.  The callback
1124                  * service sleeps on the sv_cb_waitq waiting for new
1125                  * requests.  Wake it up after adding enqueing the
1126                  * request.
1127                  */
1128                 dprintk("RPC:       add callback request to list\n");
1129                 spin_lock(&bc_serv->sv_cb_lock);
1130                 list_add(&req->rq_bc_list, &bc_serv->sv_cb_list);
1131                 spin_unlock(&bc_serv->sv_cb_lock);
1132                 wake_up(&bc_serv->sv_cb_waitq);
1133         }
1134
1135         req->rq_private_buf.len = transport->tcp_copied;
1136
1137         return 0;
1138 }
1139
1140 static inline int _xs_tcp_read_data(struct rpc_xprt *xprt,
1141                                         struct xdr_skb_reader *desc)
1142 {
1143         struct sock_xprt *transport =
1144                                 container_of(xprt, struct sock_xprt, xprt);
1145
1146         return (transport->tcp_flags & TCP_RPC_REPLY) ?
1147                 xs_tcp_read_reply(xprt, desc) :
1148                 xs_tcp_read_callback(xprt, desc);
1149 }
1150 #else
1151 static inline int _xs_tcp_read_data(struct rpc_xprt *xprt,
1152                                         struct xdr_skb_reader *desc)
1153 {
1154         return xs_tcp_read_reply(xprt, desc);
1155 }
1156 #endif /* CONFIG_NFS_V4_1 */
1157
1158 /*
1159  * Read data off the transport.  This can be either an RPC_CALL or an
1160  * RPC_REPLY.  Relay the processing to helper functions.
1161  */
1162 static void xs_tcp_read_data(struct rpc_xprt *xprt,
1163                                     struct xdr_skb_reader *desc)
1164 {
1165         struct sock_xprt *transport =
1166                                 container_of(xprt, struct sock_xprt, xprt);
1167
1168         if (_xs_tcp_read_data(xprt, desc) == 0)
1169                 xs_tcp_check_fraghdr(transport);
1170         else {
1171                 /*
1172                  * The transport_lock protects the request handling.
1173                  * There's no need to hold it to update the tcp_flags.
1174                  */
1175                 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1176         }
1177 }
1178
1179 static inline void xs_tcp_read_discard(struct sock_xprt *transport, struct xdr_skb_reader *desc)
1180 {
1181         size_t len;
1182
1183         len = transport->tcp_reclen - transport->tcp_offset;
1184         if (len > desc->count)
1185                 len = desc->count;
1186         desc->count -= len;
1187         desc->offset += len;
1188         transport->tcp_offset += len;
1189         dprintk("RPC:       discarded %Zu bytes\n", len);
1190         xs_tcp_check_fraghdr(transport);
1191 }
1192
1193 static int xs_tcp_data_recv(read_descriptor_t *rd_desc, struct sk_buff *skb, unsigned int offset, size_t len)
1194 {
1195         struct rpc_xprt *xprt = rd_desc->arg.data;
1196         struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1197         struct xdr_skb_reader desc = {
1198                 .skb    = skb,
1199                 .offset = offset,
1200                 .count  = len,
1201         };
1202
1203         dprintk("RPC:       xs_tcp_data_recv started\n");
1204         do {
1205                 /* Read in a new fragment marker if necessary */
1206                 /* Can we ever really expect to get completely empty fragments? */
1207                 if (transport->tcp_flags & TCP_RCV_COPY_FRAGHDR) {
1208                         xs_tcp_read_fraghdr(xprt, &desc);
1209                         continue;
1210                 }
1211                 /* Read in the xid if necessary */
1212                 if (transport->tcp_flags & TCP_RCV_COPY_XID) {
1213                         xs_tcp_read_xid(transport, &desc);
1214                         continue;
1215                 }
1216                 /* Read in the call/reply flag */
1217                 if (transport->tcp_flags & TCP_RCV_READ_CALLDIR) {
1218                         xs_tcp_read_calldir(transport, &desc);
1219                         continue;
1220                 }
1221                 /* Read in the request data */
1222                 if (transport->tcp_flags & TCP_RCV_COPY_DATA) {
1223                         xs_tcp_read_data(xprt, &desc);
1224                         continue;
1225                 }
1226                 /* Skip over any trailing bytes on short reads */
1227                 xs_tcp_read_discard(transport, &desc);
1228         } while (desc.count);
1229         dprintk("RPC:       xs_tcp_data_recv done\n");
1230         return len - desc.count;
1231 }
1232
1233 /**
1234  * xs_tcp_data_ready - "data ready" callback for TCP sockets
1235  * @sk: socket with data to read
1236  * @bytes: how much data to read
1237  *
1238  */
1239 static void xs_tcp_data_ready(struct sock *sk, int bytes)
1240 {
1241         struct rpc_xprt *xprt;
1242         read_descriptor_t rd_desc;
1243         int read;
1244
1245         dprintk("RPC:       xs_tcp_data_ready...\n");
1246
1247         read_lock(&sk->sk_callback_lock);
1248         if (!(xprt = xprt_from_sock(sk)))
1249                 goto out;
1250         if (xprt->shutdown)
1251                 goto out;
1252
1253         /* Any data means we had a useful conversation, so
1254          * the we don't need to delay the next reconnect
1255          */
1256         if (xprt->reestablish_timeout)
1257                 xprt->reestablish_timeout = 0;
1258
1259         /* We use rd_desc to pass struct xprt to xs_tcp_data_recv */
1260         rd_desc.arg.data = xprt;
1261         do {
1262                 rd_desc.count = 65536;
1263                 read = tcp_read_sock(sk, &rd_desc, xs_tcp_data_recv);
1264         } while (read > 0);
1265 out:
1266         read_unlock(&sk->sk_callback_lock);
1267 }
1268
1269 /*
1270  * Do the equivalent of linger/linger2 handling for dealing with
1271  * broken servers that don't close the socket in a timely
1272  * fashion
1273  */
1274 static void xs_tcp_schedule_linger_timeout(struct rpc_xprt *xprt,
1275                 unsigned long timeout)
1276 {
1277         struct sock_xprt *transport;
1278
1279         if (xprt_test_and_set_connecting(xprt))
1280                 return;
1281         set_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1282         transport = container_of(xprt, struct sock_xprt, xprt);
1283         queue_delayed_work(rpciod_workqueue, &transport->connect_worker,
1284                            timeout);
1285 }
1286
1287 static void xs_tcp_cancel_linger_timeout(struct rpc_xprt *xprt)
1288 {
1289         struct sock_xprt *transport;
1290
1291         transport = container_of(xprt, struct sock_xprt, xprt);
1292
1293         if (!test_bit(XPRT_CONNECTION_ABORT, &xprt->state) ||
1294             !cancel_delayed_work(&transport->connect_worker))
1295                 return;
1296         clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1297         xprt_clear_connecting(xprt);
1298 }
1299
1300 static void xs_sock_mark_closed(struct rpc_xprt *xprt)
1301 {
1302         smp_mb__before_clear_bit();
1303         clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
1304         clear_bit(XPRT_CLOSING, &xprt->state);
1305         smp_mb__after_clear_bit();
1306         /* Mark transport as closed and wake up all pending tasks */
1307         xprt_disconnect_done(xprt);
1308 }
1309
1310 /**
1311  * xs_tcp_state_change - callback to handle TCP socket state changes
1312  * @sk: socket whose state has changed
1313  *
1314  */
1315 static void xs_tcp_state_change(struct sock *sk)
1316 {
1317         struct rpc_xprt *xprt;
1318
1319         read_lock(&sk->sk_callback_lock);
1320         if (!(xprt = xprt_from_sock(sk)))
1321                 goto out;
1322         dprintk("RPC:       xs_tcp_state_change client %p...\n", xprt);
1323         dprintk("RPC:       state %x conn %d dead %d zapped %d\n",
1324                         sk->sk_state, xprt_connected(xprt),
1325                         sock_flag(sk, SOCK_DEAD),
1326                         sock_flag(sk, SOCK_ZAPPED));
1327
1328         switch (sk->sk_state) {
1329         case TCP_ESTABLISHED:
1330                 spin_lock_bh(&xprt->transport_lock);
1331                 if (!xprt_test_and_set_connected(xprt)) {
1332                         struct sock_xprt *transport = container_of(xprt,
1333                                         struct sock_xprt, xprt);
1334
1335                         /* Reset TCP record info */
1336                         transport->tcp_offset = 0;
1337                         transport->tcp_reclen = 0;
1338                         transport->tcp_copied = 0;
1339                         transport->tcp_flags =
1340                                 TCP_RCV_COPY_FRAGHDR | TCP_RCV_COPY_XID;
1341
1342                         xprt_wake_pending_tasks(xprt, -EAGAIN);
1343                 }
1344                 spin_unlock_bh(&xprt->transport_lock);
1345                 break;
1346         case TCP_FIN_WAIT1:
1347                 /* The client initiated a shutdown of the socket */
1348                 xprt->connect_cookie++;
1349                 xprt->reestablish_timeout = 0;
1350                 set_bit(XPRT_CLOSING, &xprt->state);
1351                 smp_mb__before_clear_bit();
1352                 clear_bit(XPRT_CONNECTED, &xprt->state);
1353                 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
1354                 smp_mb__after_clear_bit();
1355                 xs_tcp_schedule_linger_timeout(xprt, xs_tcp_fin_timeout);
1356                 break;
1357         case TCP_CLOSE_WAIT:
1358                 /* The server initiated a shutdown of the socket */
1359                 xprt_force_disconnect(xprt);
1360         case TCP_SYN_SENT:
1361                 xprt->connect_cookie++;
1362         case TCP_CLOSING:
1363                 /*
1364                  * If the server closed down the connection, make sure that
1365                  * we back off before reconnecting
1366                  */
1367                 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
1368                         xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
1369                 break;
1370         case TCP_LAST_ACK:
1371                 set_bit(XPRT_CLOSING, &xprt->state);
1372                 xs_tcp_schedule_linger_timeout(xprt, xs_tcp_fin_timeout);
1373                 smp_mb__before_clear_bit();
1374                 clear_bit(XPRT_CONNECTED, &xprt->state);
1375                 smp_mb__after_clear_bit();
1376                 break;
1377         case TCP_CLOSE:
1378                 xs_tcp_cancel_linger_timeout(xprt);
1379                 xs_sock_mark_closed(xprt);
1380         }
1381  out:
1382         read_unlock(&sk->sk_callback_lock);
1383 }
1384
1385 /**
1386  * xs_error_report - callback mainly for catching socket errors
1387  * @sk: socket
1388  */
1389 static void xs_error_report(struct sock *sk)
1390 {
1391         struct rpc_xprt *xprt;
1392
1393         read_lock(&sk->sk_callback_lock);
1394         if (!(xprt = xprt_from_sock(sk)))
1395                 goto out;
1396         dprintk("RPC:       %s client %p...\n"
1397                         "RPC:       error %d\n",
1398                         __func__, xprt, sk->sk_err);
1399         xprt_wake_pending_tasks(xprt, -EAGAIN);
1400 out:
1401         read_unlock(&sk->sk_callback_lock);
1402 }
1403
1404 static void xs_write_space(struct sock *sk)
1405 {
1406         struct socket *sock;
1407         struct rpc_xprt *xprt;
1408
1409         if (unlikely(!(sock = sk->sk_socket)))
1410                 return;
1411         clear_bit(SOCK_NOSPACE, &sock->flags);
1412
1413         if (unlikely(!(xprt = xprt_from_sock(sk))))
1414                 return;
1415         if (test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags) == 0)
1416                 return;
1417
1418         xprt_write_space(xprt);
1419 }
1420
1421 /**
1422  * xs_udp_write_space - callback invoked when socket buffer space
1423  *                             becomes available
1424  * @sk: socket whose state has changed
1425  *
1426  * Called when more output buffer space is available for this socket.
1427  * We try not to wake our writers until they can make "significant"
1428  * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1429  * with a bunch of small requests.
1430  */
1431 static void xs_udp_write_space(struct sock *sk)
1432 {
1433         read_lock(&sk->sk_callback_lock);
1434
1435         /* from net/core/sock.c:sock_def_write_space */
1436         if (sock_writeable(sk))
1437                 xs_write_space(sk);
1438
1439         read_unlock(&sk->sk_callback_lock);
1440 }
1441
1442 /**
1443  * xs_tcp_write_space - callback invoked when socket buffer space
1444  *                             becomes available
1445  * @sk: socket whose state has changed
1446  *
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.
1451  */
1452 static void xs_tcp_write_space(struct sock *sk)
1453 {
1454         read_lock(&sk->sk_callback_lock);
1455
1456         /* from net/core/stream.c:sk_stream_write_space */
1457         if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk))
1458                 xs_write_space(sk);
1459
1460         read_unlock(&sk->sk_callback_lock);
1461 }
1462
1463 static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt)
1464 {
1465         struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1466         struct sock *sk = transport->inet;
1467
1468         if (transport->rcvsize) {
1469                 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
1470                 sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2;
1471         }
1472         if (transport->sndsize) {
1473                 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
1474                 sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2;
1475                 sk->sk_write_space(sk);
1476         }
1477 }
1478
1479 /**
1480  * xs_udp_set_buffer_size - set send and receive limits
1481  * @xprt: generic transport
1482  * @sndsize: requested size of send buffer, in bytes
1483  * @rcvsize: requested size of receive buffer, in bytes
1484  *
1485  * Set socket send and receive buffer size limits.
1486  */
1487 static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize)
1488 {
1489         struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1490
1491         transport->sndsize = 0;
1492         if (sndsize)
1493                 transport->sndsize = sndsize + 1024;
1494         transport->rcvsize = 0;
1495         if (rcvsize)
1496                 transport->rcvsize = rcvsize + 1024;
1497
1498         xs_udp_do_set_buffer_size(xprt);
1499 }
1500
1501 /**
1502  * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
1503  * @task: task that timed out
1504  *
1505  * Adjust the congestion window after a retransmit timeout has occurred.
1506  */
1507 static void xs_udp_timer(struct rpc_task *task)
1508 {
1509         xprt_adjust_cwnd(task, -ETIMEDOUT);
1510 }
1511
1512 static unsigned short xs_get_random_port(void)
1513 {
1514         unsigned short range = xprt_max_resvport - xprt_min_resvport;
1515         unsigned short rand = (unsigned short) net_random() % range;
1516         return rand + xprt_min_resvport;
1517 }
1518
1519 /**
1520  * xs_set_port - reset the port number in the remote endpoint address
1521  * @xprt: generic transport
1522  * @port: new port number
1523  *
1524  */
1525 static void xs_set_port(struct rpc_xprt *xprt, unsigned short port)
1526 {
1527         dprintk("RPC:       setting port for xprt %p to %u\n", xprt, port);
1528
1529         rpc_set_port(xs_addr(xprt), port);
1530         xs_update_peer_port(xprt);
1531 }
1532
1533 static unsigned short xs_get_srcport(struct sock_xprt *transport, struct socket *sock)
1534 {
1535         unsigned short port = transport->srcport;
1536
1537         if (port == 0 && transport->xprt.resvport)
1538                 port = xs_get_random_port();
1539         return port;
1540 }
1541
1542 static unsigned short xs_next_srcport(struct sock_xprt *transport, struct socket *sock, unsigned short port)
1543 {
1544         if (transport->srcport != 0)
1545                 transport->srcport = 0;
1546         if (!transport->xprt.resvport)
1547                 return 0;
1548         if (port <= xprt_min_resvport || port > xprt_max_resvport)
1549                 return xprt_max_resvport;
1550         return --port;
1551 }
1552
1553 static int xs_bind4(struct sock_xprt *transport, struct socket *sock)
1554 {
1555         struct sockaddr_in myaddr = {
1556                 .sin_family = AF_INET,
1557         };
1558         struct sockaddr_in *sa;
1559         int err, nloop = 0;
1560         unsigned short port = xs_get_srcport(transport, sock);
1561         unsigned short last;
1562
1563         sa = (struct sockaddr_in *)&transport->srcaddr;
1564         myaddr.sin_addr = sa->sin_addr;
1565         do {
1566                 myaddr.sin_port = htons(port);
1567                 err = kernel_bind(sock, (struct sockaddr *) &myaddr,
1568                                                 sizeof(myaddr));
1569                 if (port == 0)
1570                         break;
1571                 if (err == 0) {
1572                         transport->srcport = port;
1573                         break;
1574                 }
1575                 last = port;
1576                 port = xs_next_srcport(transport, sock, port);
1577                 if (port > last)
1578                         nloop++;
1579         } while (err == -EADDRINUSE && nloop != 2);
1580         dprintk("RPC:       %s %pI4:%u: %s (%d)\n",
1581                         __func__, &myaddr.sin_addr,
1582                         port, err ? "failed" : "ok", err);
1583         return err;
1584 }
1585
1586 static int xs_bind6(struct sock_xprt *transport, struct socket *sock)
1587 {
1588         struct sockaddr_in6 myaddr = {
1589                 .sin6_family = AF_INET6,
1590         };
1591         struct sockaddr_in6 *sa;
1592         int err, nloop = 0;
1593         unsigned short port = xs_get_srcport(transport, sock);
1594         unsigned short last;
1595
1596         sa = (struct sockaddr_in6 *)&transport->srcaddr;
1597         myaddr.sin6_addr = sa->sin6_addr;
1598         do {
1599                 myaddr.sin6_port = htons(port);
1600                 err = kernel_bind(sock, (struct sockaddr *) &myaddr,
1601                                                 sizeof(myaddr));
1602                 if (port == 0)
1603                         break;
1604                 if (err == 0) {
1605                         transport->srcport = port;
1606                         break;
1607                 }
1608                 last = port;
1609                 port = xs_next_srcport(transport, sock, port);
1610                 if (port > last)
1611                         nloop++;
1612         } while (err == -EADDRINUSE && nloop != 2);
1613         dprintk("RPC:       xs_bind6 %pI6:%u: %s (%d)\n",
1614                 &myaddr.sin6_addr, port, err ? "failed" : "ok", err);
1615         return err;
1616 }
1617
1618 #ifdef CONFIG_DEBUG_LOCK_ALLOC
1619 static struct lock_class_key xs_key[2];
1620 static struct lock_class_key xs_slock_key[2];
1621
1622 static inline void xs_reclassify_socket4(struct socket *sock)
1623 {
1624         struct sock *sk = sock->sk;
1625
1626         BUG_ON(sock_owned_by_user(sk));
1627         sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC",
1628                 &xs_slock_key[0], "sk_lock-AF_INET-RPC", &xs_key[0]);
1629 }
1630
1631 static inline void xs_reclassify_socket6(struct socket *sock)
1632 {
1633         struct sock *sk = sock->sk;
1634
1635         BUG_ON(sock_owned_by_user(sk));
1636         sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC",
1637                 &xs_slock_key[1], "sk_lock-AF_INET6-RPC", &xs_key[1]);
1638 }
1639 #else
1640 static inline void xs_reclassify_socket4(struct socket *sock)
1641 {
1642 }
1643
1644 static inline void xs_reclassify_socket6(struct socket *sock)
1645 {
1646 }
1647 #endif
1648
1649 static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
1650 {
1651         struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1652
1653         if (!transport->inet) {
1654                 struct sock *sk = sock->sk;
1655
1656                 write_lock_bh(&sk->sk_callback_lock);
1657
1658                 xs_save_old_callbacks(transport, sk);
1659
1660                 sk->sk_user_data = xprt;
1661                 sk->sk_data_ready = xs_udp_data_ready;
1662                 sk->sk_write_space = xs_udp_write_space;
1663                 sk->sk_error_report = xs_error_report;
1664                 sk->sk_no_check = UDP_CSUM_NORCV;
1665                 sk->sk_allocation = GFP_ATOMIC;
1666
1667                 xprt_set_connected(xprt);
1668
1669                 /* Reset to new socket */
1670                 transport->sock = sock;
1671                 transport->inet = sk;
1672
1673                 write_unlock_bh(&sk->sk_callback_lock);
1674         }
1675         xs_udp_do_set_buffer_size(xprt);
1676 }
1677
1678 /**
1679  * xs_udp_connect_worker4 - set up a UDP socket
1680  * @work: RPC transport to connect
1681  *
1682  * Invoked by a work queue tasklet.
1683  */
1684 static void xs_udp_connect_worker4(struct work_struct *work)
1685 {
1686         struct sock_xprt *transport =
1687                 container_of(work, struct sock_xprt, connect_worker.work);
1688         struct rpc_xprt *xprt = &transport->xprt;
1689         struct socket *sock = transport->sock;
1690         int err, status = -EIO;
1691
1692         if (xprt->shutdown)
1693                 goto out;
1694
1695         /* Start by resetting any existing state */
1696         xs_reset_transport(transport);
1697
1698         err = sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock);
1699         if (err < 0) {
1700                 dprintk("RPC:       can't create UDP transport socket (%d).\n", -err);
1701                 goto out;
1702         }
1703         xs_reclassify_socket4(sock);
1704
1705         if (xs_bind4(transport, sock)) {
1706                 sock_release(sock);
1707                 goto out;
1708         }
1709
1710         dprintk("RPC:       worker connecting xprt %p via %s to "
1711                                 "%s (port %s)\n", xprt,
1712                         xprt->address_strings[RPC_DISPLAY_PROTO],
1713                         xprt->address_strings[RPC_DISPLAY_ADDR],
1714                         xprt->address_strings[RPC_DISPLAY_PORT]);
1715
1716         xs_udp_finish_connecting(xprt, sock);
1717         status = 0;
1718 out:
1719         xprt_clear_connecting(xprt);
1720         xprt_wake_pending_tasks(xprt, status);
1721 }
1722
1723 /**
1724  * xs_udp_connect_worker6 - set up a UDP socket
1725  * @work: RPC transport to connect
1726  *
1727  * Invoked by a work queue tasklet.
1728  */
1729 static void xs_udp_connect_worker6(struct work_struct *work)
1730 {
1731         struct sock_xprt *transport =
1732                 container_of(work, struct sock_xprt, connect_worker.work);
1733         struct rpc_xprt *xprt = &transport->xprt;
1734         struct socket *sock = transport->sock;
1735         int err, status = -EIO;
1736
1737         if (xprt->shutdown)
1738                 goto out;
1739
1740         /* Start by resetting any existing state */
1741         xs_reset_transport(transport);
1742
1743         err = sock_create_kern(PF_INET6, SOCK_DGRAM, IPPROTO_UDP, &sock);
1744         if (err < 0) {
1745                 dprintk("RPC:       can't create UDP transport socket (%d).\n", -err);
1746                 goto out;
1747         }
1748         xs_reclassify_socket6(sock);
1749
1750         if (xs_bind6(transport, sock) < 0) {
1751                 sock_release(sock);
1752                 goto out;
1753         }
1754
1755         dprintk("RPC:       worker connecting xprt %p via %s to "
1756                                 "%s (port %s)\n", xprt,
1757                         xprt->address_strings[RPC_DISPLAY_PROTO],
1758                         xprt->address_strings[RPC_DISPLAY_ADDR],
1759                         xprt->address_strings[RPC_DISPLAY_PORT]);
1760
1761         xs_udp_finish_connecting(xprt, sock);
1762         status = 0;
1763 out:
1764         xprt_clear_connecting(xprt);
1765         xprt_wake_pending_tasks(xprt, status);
1766 }
1767
1768 /*
1769  * We need to preserve the port number so the reply cache on the server can
1770  * find our cached RPC replies when we get around to reconnecting.
1771  */
1772 static void xs_abort_connection(struct rpc_xprt *xprt, struct sock_xprt *transport)
1773 {
1774         int result;
1775         struct sockaddr any;
1776
1777         dprintk("RPC:       disconnecting xprt %p to reuse port\n", xprt);
1778
1779         /*
1780          * Disconnect the transport socket by doing a connect operation
1781          * with AF_UNSPEC.  This should return immediately...
1782          */
1783         memset(&any, 0, sizeof(any));
1784         any.sa_family = AF_UNSPEC;
1785         result = kernel_connect(transport->sock, &any, sizeof(any), 0);
1786         if (!result)
1787                 xs_sock_mark_closed(xprt);
1788         else
1789                 dprintk("RPC:       AF_UNSPEC connect return code %d\n",
1790                                 result);
1791 }
1792
1793 static void xs_tcp_reuse_connection(struct rpc_xprt *xprt, struct sock_xprt *transport)
1794 {
1795         unsigned int state = transport->inet->sk_state;
1796
1797         if (state == TCP_CLOSE && transport->sock->state == SS_UNCONNECTED)
1798                 return;
1799         if ((1 << state) & (TCPF_ESTABLISHED|TCPF_SYN_SENT))
1800                 return;
1801         xs_abort_connection(xprt, transport);
1802 }
1803
1804 static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
1805 {
1806         struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1807
1808         if (!transport->inet) {
1809                 struct sock *sk = sock->sk;
1810
1811                 write_lock_bh(&sk->sk_callback_lock);
1812
1813                 xs_save_old_callbacks(transport, sk);
1814
1815                 sk->sk_user_data = xprt;
1816                 sk->sk_data_ready = xs_tcp_data_ready;
1817                 sk->sk_state_change = xs_tcp_state_change;
1818                 sk->sk_write_space = xs_tcp_write_space;
1819                 sk->sk_error_report = xs_error_report;
1820                 sk->sk_allocation = GFP_ATOMIC;
1821
1822                 /* socket options */
1823                 sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
1824                 sock_reset_flag(sk, SOCK_LINGER);
1825                 tcp_sk(sk)->linger2 = 0;
1826                 tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF;
1827
1828                 xprt_clear_connected(xprt);
1829
1830                 /* Reset to new socket */
1831                 transport->sock = sock;
1832                 transport->inet = sk;
1833
1834                 write_unlock_bh(&sk->sk_callback_lock);
1835         }
1836
1837         if (!xprt_bound(xprt))
1838                 return -ENOTCONN;
1839
1840         /* Tell the socket layer to start connecting... */
1841         xprt->stat.connect_count++;
1842         xprt->stat.connect_start = jiffies;
1843         return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK);
1844 }
1845
1846 /**
1847  * xs_tcp_setup_socket - create a TCP socket and connect to a remote endpoint
1848  * @xprt: RPC transport to connect
1849  * @transport: socket transport to connect
1850  * @create_sock: function to create a socket of the correct type
1851  *
1852  * Invoked by a work queue tasklet.
1853  */
1854 static void xs_tcp_setup_socket(struct rpc_xprt *xprt,
1855                 struct sock_xprt *transport,
1856                 struct socket *(*create_sock)(struct rpc_xprt *,
1857                         struct sock_xprt *))
1858 {
1859         struct socket *sock = transport->sock;
1860         int status = -EIO;
1861
1862         if (xprt->shutdown)
1863                 goto out;
1864
1865         if (!sock) {
1866                 clear_bit(XPRT_CONNECTION_ABORT, &xprt->state);
1867                 sock = create_sock(xprt, transport);
1868                 if (IS_ERR(sock)) {
1869                         status = PTR_ERR(sock);
1870                         goto out;
1871                 }
1872         } else {
1873                 int abort_and_exit;
1874
1875                 abort_and_exit = test_and_clear_bit(XPRT_CONNECTION_ABORT,
1876                                 &xprt->state);
1877                 /* "close" the socket, preserving the local port */
1878                 xs_tcp_reuse_connection(xprt, transport);
1879
1880                 if (abort_and_exit)
1881                         goto out_eagain;
1882         }
1883
1884         dprintk("RPC:       worker connecting xprt %p via %s to "
1885                                 "%s (port %s)\n", xprt,
1886                         xprt->address_strings[RPC_DISPLAY_PROTO],
1887                         xprt->address_strings[RPC_DISPLAY_ADDR],
1888                         xprt->address_strings[RPC_DISPLAY_PORT]);
1889
1890         status = xs_tcp_finish_connecting(xprt, sock);
1891         dprintk("RPC:       %p connect status %d connected %d sock state %d\n",
1892                         xprt, -status, xprt_connected(xprt),
1893                         sock->sk->sk_state);
1894         switch (status) {
1895         default:
1896                 printk("%s: connect returned unhandled error %d\n",
1897                         __func__, status);
1898         case -EADDRNOTAVAIL:
1899                 /* We're probably in TIME_WAIT. Get rid of existing socket,
1900                  * and retry
1901                  */
1902                 set_bit(XPRT_CONNECTION_CLOSE, &xprt->state);
1903                 xprt_force_disconnect(xprt);
1904                 break;
1905         case -ECONNREFUSED:
1906         case -ECONNRESET:
1907         case -ENETUNREACH:
1908                 /* retry with existing socket, after a delay */
1909         case 0:
1910         case -EINPROGRESS:
1911         case -EALREADY:
1912                 xprt_clear_connecting(xprt);
1913                 return;
1914         case -EINVAL:
1915                 /* Happens, for instance, if the user specified a link
1916                  * local IPv6 address without a scope-id.
1917                  */
1918                 goto out;
1919         }
1920 out_eagain:
1921         status = -EAGAIN;
1922 out:
1923         xprt_clear_connecting(xprt);
1924         xprt_wake_pending_tasks(xprt, status);
1925 }
1926
1927 static struct socket *xs_create_tcp_sock4(struct rpc_xprt *xprt,
1928                 struct sock_xprt *transport)
1929 {
1930         struct socket *sock;
1931         int err;
1932
1933         /* start from scratch */
1934         err = sock_create_kern(PF_INET, SOCK_STREAM, IPPROTO_TCP, &sock);
1935         if (err < 0) {
1936                 dprintk("RPC:       can't create TCP transport socket (%d).\n",
1937                                 -err);
1938                 goto out_err;
1939         }
1940         xs_reclassify_socket4(sock);
1941
1942         if (xs_bind4(transport, sock) < 0) {
1943                 sock_release(sock);
1944                 goto out_err;
1945         }
1946         return sock;
1947 out_err:
1948         return ERR_PTR(-EIO);
1949 }
1950
1951 /**
1952  * xs_tcp_connect_worker4 - connect a TCP socket to a remote endpoint
1953  * @work: RPC transport to connect
1954  *
1955  * Invoked by a work queue tasklet.
1956  */
1957 static void xs_tcp_connect_worker4(struct work_struct *work)
1958 {
1959         struct sock_xprt *transport =
1960                 container_of(work, struct sock_xprt, connect_worker.work);
1961         struct rpc_xprt *xprt = &transport->xprt;
1962
1963         xs_tcp_setup_socket(xprt, transport, xs_create_tcp_sock4);
1964 }
1965
1966 static struct socket *xs_create_tcp_sock6(struct rpc_xprt *xprt,
1967                 struct sock_xprt *transport)
1968 {
1969         struct socket *sock;
1970         int err;
1971
1972         /* start from scratch */
1973         err = sock_create_kern(PF_INET6, SOCK_STREAM, IPPROTO_TCP, &sock);
1974         if (err < 0) {
1975                 dprintk("RPC:       can't create TCP transport socket (%d).\n",
1976                                 -err);
1977                 goto out_err;
1978         }
1979         xs_reclassify_socket6(sock);
1980
1981         if (xs_bind6(transport, sock) < 0) {
1982                 sock_release(sock);
1983                 goto out_err;
1984         }
1985         return sock;
1986 out_err:
1987         return ERR_PTR(-EIO);
1988 }
1989
1990 /**
1991  * xs_tcp_connect_worker6 - connect a TCP socket to a remote endpoint
1992  * @work: RPC transport to connect
1993  *
1994  * Invoked by a work queue tasklet.
1995  */
1996 static void xs_tcp_connect_worker6(struct work_struct *work)
1997 {
1998         struct sock_xprt *transport =
1999                 container_of(work, struct sock_xprt, connect_worker.work);
2000         struct rpc_xprt *xprt = &transport->xprt;
2001
2002         xs_tcp_setup_socket(xprt, transport, xs_create_tcp_sock6);
2003 }
2004
2005 /**
2006  * xs_connect - connect a socket to a remote endpoint
2007  * @task: address of RPC task that manages state of connect request
2008  *
2009  * TCP: If the remote end dropped the connection, delay reconnecting.
2010  *
2011  * UDP socket connects are synchronous, but we use a work queue anyway
2012  * to guarantee that even unprivileged user processes can set up a
2013  * socket on a privileged port.
2014  *
2015  * If a UDP socket connect fails, the delay behavior here prevents
2016  * retry floods (hard mounts).
2017  */
2018 static void xs_connect(struct rpc_task *task)
2019 {
2020         struct rpc_xprt *xprt = task->tk_xprt;
2021         struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2022
2023         if (xprt_test_and_set_connecting(xprt))
2024                 return;
2025
2026         if (transport->sock != NULL && !RPC_IS_SOFTCONN(task)) {
2027                 dprintk("RPC:       xs_connect delayed xprt %p for %lu "
2028                                 "seconds\n",
2029                                 xprt, xprt->reestablish_timeout / HZ);
2030                 queue_delayed_work(rpciod_workqueue,
2031                                    &transport->connect_worker,
2032                                    xprt->reestablish_timeout);
2033                 xprt->reestablish_timeout <<= 1;
2034                 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
2035                         xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2036                 if (xprt->reestablish_timeout > XS_TCP_MAX_REEST_TO)
2037                         xprt->reestablish_timeout = XS_TCP_MAX_REEST_TO;
2038         } else {
2039                 dprintk("RPC:       xs_connect scheduled xprt %p\n", xprt);
2040                 queue_delayed_work(rpciod_workqueue,
2041                                    &transport->connect_worker, 0);
2042         }
2043 }
2044
2045 static void xs_tcp_connect(struct rpc_task *task)
2046 {
2047         struct rpc_xprt *xprt = task->tk_xprt;
2048
2049         /* Exit if we need to wait for socket shutdown to complete */
2050         if (test_bit(XPRT_CLOSING, &xprt->state))
2051                 return;
2052         xs_connect(task);
2053 }
2054
2055 /**
2056  * xs_udp_print_stats - display UDP socket-specifc stats
2057  * @xprt: rpc_xprt struct containing statistics
2058  * @seq: output file
2059  *
2060  */
2061 static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2062 {
2063         struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2064
2065         seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %Lu %Lu\n",
2066                         transport->srcport,
2067                         xprt->stat.bind_count,
2068                         xprt->stat.sends,
2069                         xprt->stat.recvs,
2070                         xprt->stat.bad_xids,
2071                         xprt->stat.req_u,
2072                         xprt->stat.bklog_u);
2073 }
2074
2075 /**
2076  * xs_tcp_print_stats - display TCP socket-specifc stats
2077  * @xprt: rpc_xprt struct containing statistics
2078  * @seq: output file
2079  *
2080  */
2081 static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
2082 {
2083         struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
2084         long idle_time = 0;
2085
2086         if (xprt_connected(xprt))
2087                 idle_time = (long)(jiffies - xprt->last_used) / HZ;
2088
2089         seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu %Lu %Lu\n",
2090                         transport->srcport,
2091                         xprt->stat.bind_count,
2092                         xprt->stat.connect_count,
2093                         xprt->stat.connect_time,
2094                         idle_time,
2095                         xprt->stat.sends,
2096                         xprt->stat.recvs,
2097                         xprt->stat.bad_xids,
2098                         xprt->stat.req_u,
2099                         xprt->stat.bklog_u);
2100 }
2101
2102 /*
2103  * Allocate a bunch of pages for a scratch buffer for the rpc code. The reason
2104  * we allocate pages instead doing a kmalloc like rpc_malloc is because we want
2105  * to use the server side send routines.
2106  */
2107 static void *bc_malloc(struct rpc_task *task, size_t size)
2108 {
2109         struct page *page;
2110         struct rpc_buffer *buf;
2111
2112         BUG_ON(size > PAGE_SIZE - sizeof(struct rpc_buffer));
2113         page = alloc_page(GFP_KERNEL);
2114
2115         if (!page)
2116                 return NULL;
2117
2118         buf = page_address(page);
2119         buf->len = PAGE_SIZE;
2120
2121         return buf->data;
2122 }
2123
2124 /*
2125  * Free the space allocated in the bc_alloc routine
2126  */
2127 static void bc_free(void *buffer)
2128 {
2129         struct rpc_buffer *buf;
2130
2131         if (!buffer)
2132                 return;
2133
2134         buf = container_of(buffer, struct rpc_buffer, data);
2135         free_page((unsigned long)buf);
2136 }
2137
2138 /*
2139  * Use the svc_sock to send the callback. Must be called with svsk->sk_mutex
2140  * held. Borrows heavily from svc_tcp_sendto and xs_tcp_send_request.
2141  */
2142 static int bc_sendto(struct rpc_rqst *req)
2143 {
2144         int len;
2145         struct xdr_buf *xbufp = &req->rq_snd_buf;
2146         struct rpc_xprt *xprt = req->rq_xprt;
2147         struct sock_xprt *transport =
2148                                 container_of(xprt, struct sock_xprt, xprt);
2149         struct socket *sock = transport->sock;
2150         unsigned long headoff;
2151         unsigned long tailoff;
2152
2153         /*
2154          * Set up the rpc header and record marker stuff
2155          */
2156         xs_encode_tcp_record_marker(xbufp);
2157
2158         tailoff = (unsigned long)xbufp->tail[0].iov_base & ~PAGE_MASK;
2159         headoff = (unsigned long)xbufp->head[0].iov_base & ~PAGE_MASK;
2160         len = svc_send_common(sock, xbufp,
2161                               virt_to_page(xbufp->head[0].iov_base), headoff,
2162                               xbufp->tail[0].iov_base, tailoff);
2163
2164         if (len != xbufp->len) {
2165                 printk(KERN_NOTICE "Error sending entire callback!\n");
2166                 len = -EAGAIN;
2167         }
2168
2169         return len;
2170 }
2171
2172 /*
2173  * The send routine. Borrows from svc_send
2174  */
2175 static int bc_send_request(struct rpc_task *task)
2176 {
2177         struct rpc_rqst *req = task->tk_rqstp;
2178         struct svc_xprt *xprt;
2179         struct svc_sock         *svsk;
2180         u32                     len;
2181
2182         dprintk("sending request with xid: %08x\n", ntohl(req->rq_xid));
2183         /*
2184          * Get the server socket associated with this callback xprt
2185          */
2186         xprt = req->rq_xprt->bc_xprt;
2187         svsk = container_of(xprt, struct svc_sock, sk_xprt);
2188
2189         /*
2190          * Grab the mutex to serialize data as the connection is shared
2191          * with the fore channel
2192          */
2193         if (!mutex_trylock(&xprt->xpt_mutex)) {
2194                 rpc_sleep_on(&xprt->xpt_bc_pending, task, NULL);
2195                 if (!mutex_trylock(&xprt->xpt_mutex))
2196                         return -EAGAIN;
2197                 rpc_wake_up_queued_task(&xprt->xpt_bc_pending, task);
2198         }
2199         if (test_bit(XPT_DEAD, &xprt->xpt_flags))
2200                 len = -ENOTCONN;
2201         else
2202                 len = bc_sendto(req);
2203         mutex_unlock(&xprt->xpt_mutex);
2204
2205         if (len > 0)
2206                 len = 0;
2207
2208         return len;
2209 }
2210
2211 /*
2212  * The close routine. Since this is client initiated, we do nothing
2213  */
2214
2215 static void bc_close(struct rpc_xprt *xprt)
2216 {
2217         return;
2218 }
2219
2220 /*
2221  * The xprt destroy routine. Again, because this connection is client
2222  * initiated, we do nothing
2223  */
2224
2225 static void bc_destroy(struct rpc_xprt *xprt)
2226 {
2227         return;
2228 }
2229
2230 static struct rpc_xprt_ops xs_udp_ops = {
2231         .set_buffer_size        = xs_udp_set_buffer_size,
2232         .reserve_xprt           = xprt_reserve_xprt_cong,
2233         .release_xprt           = xprt_release_xprt_cong,
2234         .rpcbind                = rpcb_getport_async,
2235         .set_port               = xs_set_port,
2236         .connect                = xs_connect,
2237         .buf_alloc              = rpc_malloc,
2238         .buf_free               = rpc_free,
2239         .send_request           = xs_udp_send_request,
2240         .set_retrans_timeout    = xprt_set_retrans_timeout_rtt,
2241         .timer                  = xs_udp_timer,
2242         .release_request        = xprt_release_rqst_cong,
2243         .close                  = xs_close,
2244         .destroy                = xs_destroy,
2245         .print_stats            = xs_udp_print_stats,
2246 };
2247
2248 static struct rpc_xprt_ops xs_tcp_ops = {
2249         .reserve_xprt           = xprt_reserve_xprt,
2250         .release_xprt           = xs_tcp_release_xprt,
2251         .rpcbind                = rpcb_getport_async,
2252         .set_port               = xs_set_port,
2253         .connect                = xs_tcp_connect,
2254         .buf_alloc              = rpc_malloc,
2255         .buf_free               = rpc_free,
2256         .send_request           = xs_tcp_send_request,
2257         .set_retrans_timeout    = xprt_set_retrans_timeout_def,
2258 #if defined(CONFIG_NFS_V4_1)
2259         .release_request        = bc_release_request,
2260 #endif /* CONFIG_NFS_V4_1 */
2261         .close                  = xs_tcp_close,
2262         .destroy                = xs_destroy,
2263         .print_stats            = xs_tcp_print_stats,
2264 };
2265
2266 /*
2267  * The rpc_xprt_ops for the server backchannel
2268  */
2269
2270 static struct rpc_xprt_ops bc_tcp_ops = {
2271         .reserve_xprt           = xprt_reserve_xprt,
2272         .release_xprt           = xprt_release_xprt,
2273         .buf_alloc              = bc_malloc,
2274         .buf_free               = bc_free,
2275         .send_request           = bc_send_request,
2276         .set_retrans_timeout    = xprt_set_retrans_timeout_def,
2277         .close                  = bc_close,
2278         .destroy                = bc_destroy,
2279         .print_stats            = xs_tcp_print_stats,
2280 };
2281
2282 static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args,
2283                                       unsigned int slot_table_size)
2284 {
2285         struct rpc_xprt *xprt;
2286         struct sock_xprt *new;
2287
2288         if (args->addrlen > sizeof(xprt->addr)) {
2289                 dprintk("RPC:       xs_setup_xprt: address too large\n");
2290                 return ERR_PTR(-EBADF);
2291         }
2292
2293         new = kzalloc(sizeof(*new), GFP_KERNEL);
2294         if (new == NULL) {
2295                 dprintk("RPC:       xs_setup_xprt: couldn't allocate "
2296                                 "rpc_xprt\n");
2297                 return ERR_PTR(-ENOMEM);
2298         }
2299         xprt = &new->xprt;
2300
2301         xprt->max_reqs = slot_table_size;
2302         xprt->slot = kcalloc(xprt->max_reqs, sizeof(struct rpc_rqst), GFP_KERNEL);
2303         if (xprt->slot == NULL) {
2304                 kfree(xprt);
2305                 dprintk("RPC:       xs_setup_xprt: couldn't allocate slot "
2306                                 "table\n");
2307                 return ERR_PTR(-ENOMEM);
2308         }
2309
2310         memcpy(&xprt->addr, args->dstaddr, args->addrlen);
2311         xprt->addrlen = args->addrlen;
2312         if (args->srcaddr)
2313                 memcpy(&new->srcaddr, args->srcaddr, args->addrlen);
2314
2315         return xprt;
2316 }
2317
2318 static const struct rpc_timeout xs_udp_default_timeout = {
2319         .to_initval = 5 * HZ,
2320         .to_maxval = 30 * HZ,
2321         .to_increment = 5 * HZ,
2322         .to_retries = 5,
2323 };
2324
2325 /**
2326  * xs_setup_udp - Set up transport to use a UDP socket
2327  * @args: rpc transport creation arguments
2328  *
2329  */
2330 static struct rpc_xprt *xs_setup_udp(struct xprt_create *args)
2331 {
2332         struct sockaddr *addr = args->dstaddr;
2333         struct rpc_xprt *xprt;
2334         struct sock_xprt *transport;
2335
2336         xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries);
2337         if (IS_ERR(xprt))
2338                 return xprt;
2339         transport = container_of(xprt, struct sock_xprt, xprt);
2340
2341         xprt->prot = IPPROTO_UDP;
2342         xprt->tsh_size = 0;
2343         /* XXX: header size can vary due to auth type, IPv6, etc. */
2344         xprt->max_payload = (1U << 16) - (MAX_HEADER << 3);
2345
2346         xprt->bind_timeout = XS_BIND_TO;
2347         xprt->connect_timeout = XS_UDP_CONN_TO;
2348         xprt->reestablish_timeout = XS_UDP_REEST_TO;
2349         xprt->idle_timeout = XS_IDLE_DISC_TO;
2350
2351         xprt->ops = &xs_udp_ops;
2352
2353         xprt->timeout = &xs_udp_default_timeout;
2354
2355         switch (addr->sa_family) {
2356         case AF_INET:
2357                 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
2358                         xprt_set_bound(xprt);
2359
2360                 INIT_DELAYED_WORK(&transport->connect_worker,
2361                                         xs_udp_connect_worker4);
2362                 xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP);
2363                 break;
2364         case AF_INET6:
2365                 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
2366                         xprt_set_bound(xprt);
2367
2368                 INIT_DELAYED_WORK(&transport->connect_worker,
2369                                         xs_udp_connect_worker6);
2370                 xs_format_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6);
2371                 break;
2372         default:
2373                 kfree(xprt);
2374                 return ERR_PTR(-EAFNOSUPPORT);
2375         }
2376
2377         if (xprt_bound(xprt))
2378                 dprintk("RPC:       set up xprt to %s (port %s) via %s\n",
2379                                 xprt->address_strings[RPC_DISPLAY_ADDR],
2380                                 xprt->address_strings[RPC_DISPLAY_PORT],
2381                                 xprt->address_strings[RPC_DISPLAY_PROTO]);
2382         else
2383                 dprintk("RPC:       set up xprt to %s (autobind) via %s\n",
2384                                 xprt->address_strings[RPC_DISPLAY_ADDR],
2385                                 xprt->address_strings[RPC_DISPLAY_PROTO]);
2386
2387         if (try_module_get(THIS_MODULE))
2388                 return xprt;
2389
2390         kfree(xprt->slot);
2391         kfree(xprt);
2392         return ERR_PTR(-EINVAL);
2393 }
2394
2395 static const struct rpc_timeout xs_tcp_default_timeout = {
2396         .to_initval = 60 * HZ,
2397         .to_maxval = 60 * HZ,
2398         .to_retries = 2,
2399 };
2400
2401 /**
2402  * xs_setup_tcp - Set up transport to use a TCP socket
2403  * @args: rpc transport creation arguments
2404  *
2405  */
2406 static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args)
2407 {
2408         struct sockaddr *addr = args->dstaddr;
2409         struct rpc_xprt *xprt;
2410         struct sock_xprt *transport;
2411
2412         xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries);
2413         if (IS_ERR(xprt))
2414                 return xprt;
2415         transport = container_of(xprt, struct sock_xprt, xprt);
2416
2417         xprt->prot = IPPROTO_TCP;
2418         xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
2419         xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2420
2421         xprt->bind_timeout = XS_BIND_TO;
2422         xprt->connect_timeout = XS_TCP_CONN_TO;
2423         xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2424         xprt->idle_timeout = XS_IDLE_DISC_TO;
2425
2426         xprt->ops = &xs_tcp_ops;
2427         xprt->timeout = &xs_tcp_default_timeout;
2428
2429         switch (addr->sa_family) {
2430         case AF_INET:
2431                 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
2432                         xprt_set_bound(xprt);
2433
2434                 INIT_DELAYED_WORK(&transport->connect_worker,
2435                                         xs_tcp_connect_worker4);
2436                 xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP);
2437                 break;
2438         case AF_INET6:
2439                 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
2440                         xprt_set_bound(xprt);
2441
2442                 INIT_DELAYED_WORK(&transport->connect_worker,
2443                                         xs_tcp_connect_worker6);
2444                 xs_format_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6);
2445                 break;
2446         default:
2447                 kfree(xprt);
2448                 return ERR_PTR(-EAFNOSUPPORT);
2449         }
2450
2451         if (xprt_bound(xprt))
2452                 dprintk("RPC:       set up xprt to %s (port %s) via %s\n",
2453                                 xprt->address_strings[RPC_DISPLAY_ADDR],
2454                                 xprt->address_strings[RPC_DISPLAY_PORT],
2455                                 xprt->address_strings[RPC_DISPLAY_PROTO]);
2456         else
2457                 dprintk("RPC:       set up xprt to %s (autobind) via %s\n",
2458                                 xprt->address_strings[RPC_DISPLAY_ADDR],
2459                                 xprt->address_strings[RPC_DISPLAY_PROTO]);
2460
2461
2462         if (try_module_get(THIS_MODULE))
2463                 return xprt;
2464
2465         kfree(xprt->slot);
2466         kfree(xprt);
2467         return ERR_PTR(-EINVAL);
2468 }
2469
2470 /**
2471  * xs_setup_bc_tcp - Set up transport to use a TCP backchannel socket
2472  * @args: rpc transport creation arguments
2473  *
2474  */
2475 static struct rpc_xprt *xs_setup_bc_tcp(struct xprt_create *args)
2476 {
2477         struct sockaddr *addr = args->dstaddr;
2478         struct rpc_xprt *xprt;
2479         struct sock_xprt *transport;
2480         struct svc_sock *bc_sock;
2481
2482         if (!args->bc_xprt)
2483                 ERR_PTR(-EINVAL);
2484
2485         xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries);
2486         if (IS_ERR(xprt))
2487                 return xprt;
2488         transport = container_of(xprt, struct sock_xprt, xprt);
2489
2490         xprt->prot = IPPROTO_TCP;
2491         xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
2492         xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2493         xprt->timeout = &xs_tcp_default_timeout;
2494
2495         /* backchannel */
2496         xprt_set_bound(xprt);
2497         xprt->bind_timeout = 0;
2498         xprt->connect_timeout = 0;
2499         xprt->reestablish_timeout = 0;
2500         xprt->idle_timeout = 0;
2501
2502         /*
2503          * The backchannel uses the same socket connection as the
2504          * forechannel
2505          */
2506         xprt->bc_xprt = args->bc_xprt;
2507         bc_sock = container_of(args->bc_xprt, struct svc_sock, sk_xprt);
2508         bc_sock->sk_bc_xprt = xprt;
2509         transport->sock = bc_sock->sk_sock;
2510         transport->inet = bc_sock->sk_sk;
2511
2512         xprt->ops = &bc_tcp_ops;
2513
2514         switch (addr->sa_family) {
2515         case AF_INET:
2516                 xs_format_peer_addresses(xprt, "tcp",
2517                                          RPCBIND_NETID_TCP);
2518                 break;
2519         case AF_INET6:
2520                 xs_format_peer_addresses(xprt, "tcp",
2521                                    RPCBIND_NETID_TCP6);
2522                 break;
2523         default:
2524                 kfree(xprt);
2525                 return ERR_PTR(-EAFNOSUPPORT);
2526         }
2527
2528         if (xprt_bound(xprt))
2529                 dprintk("RPC:       set up xprt to %s (port %s) via %s\n",
2530                                 xprt->address_strings[RPC_DISPLAY_ADDR],
2531                                 xprt->address_strings[RPC_DISPLAY_PORT],
2532                                 xprt->address_strings[RPC_DISPLAY_PROTO]);
2533         else
2534                 dprintk("RPC:       set up xprt to %s (autobind) via %s\n",
2535                                 xprt->address_strings[RPC_DISPLAY_ADDR],
2536                                 xprt->address_strings[RPC_DISPLAY_PROTO]);
2537
2538         /*
2539          * Since we don't want connections for the backchannel, we set
2540          * the xprt status to connected
2541          */
2542         xprt_set_connected(xprt);
2543
2544
2545         if (try_module_get(THIS_MODULE))
2546                 return xprt;
2547         kfree(xprt->slot);
2548         kfree(xprt);
2549         return ERR_PTR(-EINVAL);
2550 }
2551
2552 static struct xprt_class        xs_udp_transport = {
2553         .list           = LIST_HEAD_INIT(xs_udp_transport.list),
2554         .name           = "udp",
2555         .owner          = THIS_MODULE,
2556         .ident          = XPRT_TRANSPORT_UDP,
2557         .setup          = xs_setup_udp,
2558 };
2559
2560 static struct xprt_class        xs_tcp_transport = {
2561         .list           = LIST_HEAD_INIT(xs_tcp_transport.list),
2562         .name           = "tcp",
2563         .owner          = THIS_MODULE,
2564         .ident          = XPRT_TRANSPORT_TCP,
2565         .setup          = xs_setup_tcp,
2566 };
2567
2568 static struct xprt_class        xs_bc_tcp_transport = {
2569         .list           = LIST_HEAD_INIT(xs_bc_tcp_transport.list),
2570         .name           = "tcp NFSv4.1 backchannel",
2571         .owner          = THIS_MODULE,
2572         .ident          = XPRT_TRANSPORT_BC_TCP,
2573         .setup          = xs_setup_bc_tcp,
2574 };
2575
2576 /**
2577  * init_socket_xprt - set up xprtsock's sysctls, register with RPC client
2578  *
2579  */
2580 int init_socket_xprt(void)
2581 {
2582 #ifdef RPC_DEBUG
2583         if (!sunrpc_table_header)
2584                 sunrpc_table_header = register_sysctl_table(sunrpc_table);
2585 #endif
2586
2587         xprt_register_transport(&xs_udp_transport);
2588         xprt_register_transport(&xs_tcp_transport);
2589         xprt_register_transport(&xs_bc_tcp_transport);
2590
2591         return 0;
2592 }
2593
2594 /**
2595  * cleanup_socket_xprt - remove xprtsock's sysctls, unregister
2596  *
2597  */
2598 void cleanup_socket_xprt(void)
2599 {
2600 #ifdef RPC_DEBUG
2601         if (sunrpc_table_header) {
2602                 unregister_sysctl_table(sunrpc_table_header);
2603                 sunrpc_table_header = NULL;
2604         }
2605 #endif
2606
2607         xprt_unregister_transport(&xs_udp_transport);
2608         xprt_unregister_transport(&xs_tcp_transport);
2609         xprt_unregister_transport(&xs_bc_tcp_transport);
2610 }
2611
2612 static int param_set_uint_minmax(const char *val, struct kernel_param *kp,
2613                 unsigned int min, unsigned int max)
2614 {
2615         unsigned long num;
2616         int ret;
2617
2618         if (!val)
2619                 return -EINVAL;
2620         ret = strict_strtoul(val, 0, &num);
2621         if (ret == -EINVAL || num < min || num > max)
2622                 return -EINVAL;
2623         *((unsigned int *)kp->arg) = num;
2624         return 0;
2625 }
2626
2627 static int param_set_portnr(const char *val, struct kernel_param *kp)
2628 {
2629         return param_set_uint_minmax(val, kp,
2630                         RPC_MIN_RESVPORT,
2631                         RPC_MAX_RESVPORT);
2632 }
2633
2634 static int param_get_portnr(char *buffer, struct kernel_param *kp)
2635 {
2636         return param_get_uint(buffer, kp);
2637 }
2638 #define param_check_portnr(name, p) \
2639         __param_check(name, p, unsigned int);
2640
2641 module_param_named(min_resvport, xprt_min_resvport, portnr, 0644);
2642 module_param_named(max_resvport, xprt_max_resvport, portnr, 0644);
2643
2644 static int param_set_slot_table_size(const char *val, struct kernel_param *kp)
2645 {
2646         return param_set_uint_minmax(val, kp,
2647                         RPC_MIN_SLOT_TABLE,
2648                         RPC_MAX_SLOT_TABLE);
2649 }
2650
2651 static int param_get_slot_table_size(char *buffer, struct kernel_param *kp)
2652 {
2653         return param_get_uint(buffer, kp);
2654 }
2655 #define param_check_slot_table_size(name, p) \
2656         __param_check(name, p, unsigned int);
2657
2658 module_param_named(tcp_slot_table_entries, xprt_tcp_slot_table_entries,
2659                    slot_table_size, 0644);
2660 module_param_named(udp_slot_table_entries, xprt_udp_slot_table_entries,
2661                    slot_table_size, 0644);
2662