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