59155aebd3b9bf86ae8bff24c7943145afb931f2
[linux-2.6.git] / net / sunrpc / xprtsock.c
1 /*
2  * linux/net/sunrpc/xprtsock.c
3  *
4  * Client-side transport implementation for sockets.
5  *
6  * TCP callback races fixes (C) 1998 Red Hat
7  * TCP send fixes (C) 1998 Red Hat
8  * TCP NFS related read + write fixes
9  *  (C) 1999 Dave Airlie, University of Limerick, Ireland <airlied@linux.ie>
10  *
11  * Rewrite of larges part of the code in order to stabilize TCP stuff.
12  * Fix behaviour when socket buffer is full.
13  *  (C) 1999 Trond Myklebust <trond.myklebust@fys.uio.no>
14  *
15  * IP socket transport implementation, (C) 2005 Chuck Lever <cel@netapp.com>
16  *
17  * IPv6 support contributed by Gilles Quillard, Bull Open Source, 2005.
18  *   <gilles.quillard@bull.net>
19  */
20
21 #include <linux/types.h>
22 #include <linux/slab.h>
23 #include <linux/module.h>
24 #include <linux/capability.h>
25 #include <linux/pagemap.h>
26 #include <linux/errno.h>
27 #include <linux/socket.h>
28 #include <linux/in.h>
29 #include <linux/net.h>
30 #include <linux/mm.h>
31 #include <linux/udp.h>
32 #include <linux/tcp.h>
33 #include <linux/sunrpc/clnt.h>
34 #include <linux/sunrpc/sched.h>
35 #include <linux/sunrpc/xprtsock.h>
36 #include <linux/file.h>
37
38 #include <net/sock.h>
39 #include <net/checksum.h>
40 #include <net/udp.h>
41 #include <net/tcp.h>
42
43 /*
44  * xprtsock tunables
45  */
46 unsigned int xprt_udp_slot_table_entries = RPC_DEF_SLOT_TABLE;
47 unsigned int xprt_tcp_slot_table_entries = RPC_DEF_SLOT_TABLE;
48
49 unsigned int xprt_min_resvport = RPC_DEF_MIN_RESVPORT;
50 unsigned int xprt_max_resvport = RPC_DEF_MAX_RESVPORT;
51
52 /*
53  * We can register our own files under /proc/sys/sunrpc by
54  * calling register_sysctl_table() again.  The files in that
55  * directory become the union of all files registered there.
56  *
57  * We simply need to make sure that we don't collide with
58  * someone else's file names!
59  */
60
61 #ifdef RPC_DEBUG
62
63 static unsigned int min_slot_table_size = RPC_MIN_SLOT_TABLE;
64 static unsigned int max_slot_table_size = RPC_MAX_SLOT_TABLE;
65 static unsigned int xprt_min_resvport_limit = RPC_MIN_RESVPORT;
66 static unsigned int xprt_max_resvport_limit = RPC_MAX_RESVPORT;
67
68 static struct ctl_table_header *sunrpc_table_header;
69
70 /*
71  * FIXME: changing the UDP slot table size should also resize the UDP
72  *        socket buffers for existing UDP transports
73  */
74 static ctl_table xs_tunables_table[] = {
75         {
76                 .ctl_name       = CTL_SLOTTABLE_UDP,
77                 .procname       = "udp_slot_table_entries",
78                 .data           = &xprt_udp_slot_table_entries,
79                 .maxlen         = sizeof(unsigned int),
80                 .mode           = 0644,
81                 .proc_handler   = &proc_dointvec_minmax,
82                 .strategy       = &sysctl_intvec,
83                 .extra1         = &min_slot_table_size,
84                 .extra2         = &max_slot_table_size
85         },
86         {
87                 .ctl_name       = CTL_SLOTTABLE_TCP,
88                 .procname       = "tcp_slot_table_entries",
89                 .data           = &xprt_tcp_slot_table_entries,
90                 .maxlen         = sizeof(unsigned int),
91                 .mode           = 0644,
92                 .proc_handler   = &proc_dointvec_minmax,
93                 .strategy       = &sysctl_intvec,
94                 .extra1         = &min_slot_table_size,
95                 .extra2         = &max_slot_table_size
96         },
97         {
98                 .ctl_name       = CTL_MIN_RESVPORT,
99                 .procname       = "min_resvport",
100                 .data           = &xprt_min_resvport,
101                 .maxlen         = sizeof(unsigned int),
102                 .mode           = 0644,
103                 .proc_handler   = &proc_dointvec_minmax,
104                 .strategy       = &sysctl_intvec,
105                 .extra1         = &xprt_min_resvport_limit,
106                 .extra2         = &xprt_max_resvport_limit
107         },
108         {
109                 .ctl_name       = CTL_MAX_RESVPORT,
110                 .procname       = "max_resvport",
111                 .data           = &xprt_max_resvport,
112                 .maxlen         = sizeof(unsigned int),
113                 .mode           = 0644,
114                 .proc_handler   = &proc_dointvec_minmax,
115                 .strategy       = &sysctl_intvec,
116                 .extra1         = &xprt_min_resvport_limit,
117                 .extra2         = &xprt_max_resvport_limit
118         },
119         {
120                 .ctl_name = 0,
121         },
122 };
123
124 static ctl_table sunrpc_table[] = {
125         {
126                 .ctl_name       = CTL_SUNRPC,
127                 .procname       = "sunrpc",
128                 .mode           = 0555,
129                 .child          = xs_tunables_table
130         },
131         {
132                 .ctl_name = 0,
133         },
134 };
135
136 #endif
137
138 /*
139  * Time out for an RPC UDP socket connect.  UDP socket connects are
140  * synchronous, but we set a timeout anyway in case of resource
141  * exhaustion on the local host.
142  */
143 #define XS_UDP_CONN_TO          (5U * HZ)
144
145 /*
146  * Wait duration for an RPC TCP connection to be established.  Solaris
147  * NFS over TCP uses 60 seconds, for example, which is in line with how
148  * long a server takes to reboot.
149  */
150 #define XS_TCP_CONN_TO          (60U * HZ)
151
152 /*
153  * Wait duration for a reply from the RPC portmapper.
154  */
155 #define XS_BIND_TO              (60U * HZ)
156
157 /*
158  * Delay if a UDP socket connect error occurs.  This is most likely some
159  * kind of resource problem on the local host.
160  */
161 #define XS_UDP_REEST_TO         (2U * HZ)
162
163 /*
164  * The reestablish timeout allows clients to delay for a bit before attempting
165  * to reconnect to a server that just dropped our connection.
166  *
167  * We implement an exponential backoff when trying to reestablish a TCP
168  * transport connection with the server.  Some servers like to drop a TCP
169  * connection when they are overworked, so we start with a short timeout and
170  * increase over time if the server is down or not responding.
171  */
172 #define XS_TCP_INIT_REEST_TO    (3U * HZ)
173 #define XS_TCP_MAX_REEST_TO     (5U * 60 * HZ)
174
175 /*
176  * TCP idle timeout; client drops the transport socket if it is idle
177  * for this long.  Note that we also timeout UDP sockets to prevent
178  * holding port numbers when there is no RPC traffic.
179  */
180 #define XS_IDLE_DISC_TO         (5U * 60 * HZ)
181
182 #ifdef RPC_DEBUG
183 # undef  RPC_DEBUG_DATA
184 # define RPCDBG_FACILITY        RPCDBG_TRANS
185 #endif
186
187 #ifdef RPC_DEBUG_DATA
188 static void xs_pktdump(char *msg, u32 *packet, unsigned int count)
189 {
190         u8 *buf = (u8 *) packet;
191         int j;
192
193         dprintk("RPC:       %s\n", msg);
194         for (j = 0; j < count && j < 128; j += 4) {
195                 if (!(j & 31)) {
196                         if (j)
197                                 dprintk("\n");
198                         dprintk("0x%04x ", j);
199                 }
200                 dprintk("%02x%02x%02x%02x ",
201                         buf[j], buf[j+1], buf[j+2], buf[j+3]);
202         }
203         dprintk("\n");
204 }
205 #else
206 static inline void xs_pktdump(char *msg, u32 *packet, unsigned int count)
207 {
208         /* NOP */
209 }
210 #endif
211
212 struct sock_xprt {
213         struct rpc_xprt         xprt;
214
215         /*
216          * Network layer
217          */
218         struct socket *         sock;
219         struct sock *           inet;
220
221         /*
222          * State of TCP reply receive
223          */
224         __be32                  tcp_fraghdr,
225                                 tcp_xid;
226
227         u32                     tcp_offset,
228                                 tcp_reclen;
229
230         unsigned long           tcp_copied,
231                                 tcp_flags;
232
233         /*
234          * Connection of transports
235          */
236         struct delayed_work     connect_worker;
237         struct sockaddr_storage addr;
238         unsigned short          port;
239
240         /*
241          * UDP socket buffer size parameters
242          */
243         size_t                  rcvsize,
244                                 sndsize;
245
246         /*
247          * Saved socket callback addresses
248          */
249         void                    (*old_data_ready)(struct sock *, int);
250         void                    (*old_state_change)(struct sock *);
251         void                    (*old_write_space)(struct sock *);
252 };
253
254 /*
255  * TCP receive state flags
256  */
257 #define TCP_RCV_LAST_FRAG       (1UL << 0)
258 #define TCP_RCV_COPY_FRAGHDR    (1UL << 1)
259 #define TCP_RCV_COPY_XID        (1UL << 2)
260 #define TCP_RCV_COPY_DATA       (1UL << 3)
261
262 static inline struct sockaddr *xs_addr(struct rpc_xprt *xprt)
263 {
264         return (struct sockaddr *) &xprt->addr;
265 }
266
267 static inline struct sockaddr_in *xs_addr_in(struct rpc_xprt *xprt)
268 {
269         return (struct sockaddr_in *) &xprt->addr;
270 }
271
272 static inline struct sockaddr_in6 *xs_addr_in6(struct rpc_xprt *xprt)
273 {
274         return (struct sockaddr_in6 *) &xprt->addr;
275 }
276
277 static void xs_format_ipv4_peer_addresses(struct rpc_xprt *xprt,
278                                           const char *protocol,
279                                           const char *netid)
280 {
281         struct sockaddr_in *addr = xs_addr_in(xprt);
282         char *buf;
283
284         buf = kzalloc(20, GFP_KERNEL);
285         if (buf) {
286                 snprintf(buf, 20, NIPQUAD_FMT,
287                                 NIPQUAD(addr->sin_addr.s_addr));
288         }
289         xprt->address_strings[RPC_DISPLAY_ADDR] = buf;
290
291         buf = kzalloc(8, GFP_KERNEL);
292         if (buf) {
293                 snprintf(buf, 8, "%u",
294                                 ntohs(addr->sin_port));
295         }
296         xprt->address_strings[RPC_DISPLAY_PORT] = buf;
297
298         xprt->address_strings[RPC_DISPLAY_PROTO] = protocol;
299
300         buf = kzalloc(48, GFP_KERNEL);
301         if (buf) {
302                 snprintf(buf, 48, "addr="NIPQUAD_FMT" port=%u proto=%s",
303                         NIPQUAD(addr->sin_addr.s_addr),
304                         ntohs(addr->sin_port),
305                         protocol);
306         }
307         xprt->address_strings[RPC_DISPLAY_ALL] = buf;
308
309         buf = kzalloc(10, GFP_KERNEL);
310         if (buf) {
311                 snprintf(buf, 10, "%02x%02x%02x%02x",
312                                 NIPQUAD(addr->sin_addr.s_addr));
313         }
314         xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = buf;
315
316         buf = kzalloc(8, GFP_KERNEL);
317         if (buf) {
318                 snprintf(buf, 8, "%4hx",
319                                 ntohs(addr->sin_port));
320         }
321         xprt->address_strings[RPC_DISPLAY_HEX_PORT] = buf;
322
323         buf = kzalloc(30, GFP_KERNEL);
324         if (buf) {
325                 snprintf(buf, 30, NIPQUAD_FMT".%u.%u",
326                                 NIPQUAD(addr->sin_addr.s_addr),
327                                 ntohs(addr->sin_port) >> 8,
328                                 ntohs(addr->sin_port) & 0xff);
329         }
330         xprt->address_strings[RPC_DISPLAY_UNIVERSAL_ADDR] = buf;
331
332         xprt->address_strings[RPC_DISPLAY_NETID] = netid;
333 }
334
335 static void xs_format_ipv6_peer_addresses(struct rpc_xprt *xprt,
336                                           const char *protocol,
337                                           const char *netid)
338 {
339         struct sockaddr_in6 *addr = xs_addr_in6(xprt);
340         char *buf;
341
342         buf = kzalloc(40, GFP_KERNEL);
343         if (buf) {
344                 snprintf(buf, 40, NIP6_FMT,
345                                 NIP6(addr->sin6_addr));
346         }
347         xprt->address_strings[RPC_DISPLAY_ADDR] = buf;
348
349         buf = kzalloc(8, GFP_KERNEL);
350         if (buf) {
351                 snprintf(buf, 8, "%u",
352                                 ntohs(addr->sin6_port));
353         }
354         xprt->address_strings[RPC_DISPLAY_PORT] = buf;
355
356         xprt->address_strings[RPC_DISPLAY_PROTO] = protocol;
357
358         buf = kzalloc(64, GFP_KERNEL);
359         if (buf) {
360                 snprintf(buf, 64, "addr="NIP6_FMT" port=%u proto=%s",
361                                 NIP6(addr->sin6_addr),
362                                 ntohs(addr->sin6_port),
363                                 protocol);
364         }
365         xprt->address_strings[RPC_DISPLAY_ALL] = buf;
366
367         buf = kzalloc(36, GFP_KERNEL);
368         if (buf)
369                 snprintf(buf, 36, "%#p6", &addr->sin6_addr);
370
371         xprt->address_strings[RPC_DISPLAY_HEX_ADDR] = buf;
372
373         buf = kzalloc(8, GFP_KERNEL);
374         if (buf) {
375                 snprintf(buf, 8, "%4hx",
376                                 ntohs(addr->sin6_port));
377         }
378         xprt->address_strings[RPC_DISPLAY_HEX_PORT] = buf;
379
380         buf = kzalloc(50, GFP_KERNEL);
381         if (buf) {
382                 snprintf(buf, 50, NIP6_FMT".%u.%u",
383                                 NIP6(addr->sin6_addr),
384                                 ntohs(addr->sin6_port) >> 8,
385                                 ntohs(addr->sin6_port) & 0xff);
386         }
387         xprt->address_strings[RPC_DISPLAY_UNIVERSAL_ADDR] = buf;
388
389         xprt->address_strings[RPC_DISPLAY_NETID] = netid;
390 }
391
392 static void xs_free_peer_addresses(struct rpc_xprt *xprt)
393 {
394         unsigned int i;
395
396         for (i = 0; i < RPC_DISPLAY_MAX; i++)
397                 switch (i) {
398                 case RPC_DISPLAY_PROTO:
399                 case RPC_DISPLAY_NETID:
400                         continue;
401                 default:
402                         kfree(xprt->address_strings[i]);
403                 }
404 }
405
406 #define XS_SENDMSG_FLAGS        (MSG_DONTWAIT | MSG_NOSIGNAL)
407
408 static int xs_send_kvec(struct socket *sock, struct sockaddr *addr, int addrlen, struct kvec *vec, unsigned int base, int more)
409 {
410         struct msghdr msg = {
411                 .msg_name       = addr,
412                 .msg_namelen    = addrlen,
413                 .msg_flags      = XS_SENDMSG_FLAGS | (more ? MSG_MORE : 0),
414         };
415         struct kvec iov = {
416                 .iov_base       = vec->iov_base + base,
417                 .iov_len        = vec->iov_len - base,
418         };
419
420         if (iov.iov_len != 0)
421                 return kernel_sendmsg(sock, &msg, &iov, 1, iov.iov_len);
422         return kernel_sendmsg(sock, &msg, NULL, 0, 0);
423 }
424
425 static int xs_send_pagedata(struct socket *sock, struct xdr_buf *xdr, unsigned int base, int more)
426 {
427         struct page **ppage;
428         unsigned int remainder;
429         int err, sent = 0;
430
431         remainder = xdr->page_len - base;
432         base += xdr->page_base;
433         ppage = xdr->pages + (base >> PAGE_SHIFT);
434         base &= ~PAGE_MASK;
435         for(;;) {
436                 unsigned int len = min_t(unsigned int, PAGE_SIZE - base, remainder);
437                 int flags = XS_SENDMSG_FLAGS;
438
439                 remainder -= len;
440                 if (remainder != 0 || more)
441                         flags |= MSG_MORE;
442                 err = sock->ops->sendpage(sock, *ppage, base, len, flags);
443                 if (remainder == 0 || err != len)
444                         break;
445                 sent += err;
446                 ppage++;
447                 base = 0;
448         }
449         if (sent == 0)
450                 return err;
451         if (err > 0)
452                 sent += err;
453         return sent;
454 }
455
456 /**
457  * xs_sendpages - write pages directly to a socket
458  * @sock: socket to send on
459  * @addr: UDP only -- address of destination
460  * @addrlen: UDP only -- length of destination address
461  * @xdr: buffer containing this request
462  * @base: starting position in the buffer
463  *
464  */
465 static int xs_sendpages(struct socket *sock, struct sockaddr *addr, int addrlen, struct xdr_buf *xdr, unsigned int base)
466 {
467         unsigned int remainder = xdr->len - base;
468         int err, sent = 0;
469
470         if (unlikely(!sock))
471                 return -ENOTCONN;
472
473         clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags);
474         if (base != 0) {
475                 addr = NULL;
476                 addrlen = 0;
477         }
478
479         if (base < xdr->head[0].iov_len || addr != NULL) {
480                 unsigned int len = xdr->head[0].iov_len - base;
481                 remainder -= len;
482                 err = xs_send_kvec(sock, addr, addrlen, &xdr->head[0], base, remainder != 0);
483                 if (remainder == 0 || err != len)
484                         goto out;
485                 sent += err;
486                 base = 0;
487         } else
488                 base -= xdr->head[0].iov_len;
489
490         if (base < xdr->page_len) {
491                 unsigned int len = xdr->page_len - base;
492                 remainder -= len;
493                 err = xs_send_pagedata(sock, xdr, base, remainder != 0);
494                 if (remainder == 0 || err != len)
495                         goto out;
496                 sent += err;
497                 base = 0;
498         } else
499                 base -= xdr->page_len;
500
501         if (base >= xdr->tail[0].iov_len)
502                 return sent;
503         err = xs_send_kvec(sock, NULL, 0, &xdr->tail[0], base, 0);
504 out:
505         if (sent == 0)
506                 return err;
507         if (err > 0)
508                 sent += err;
509         return sent;
510 }
511
512 static void xs_nospace_callback(struct rpc_task *task)
513 {
514         struct sock_xprt *transport = container_of(task->tk_rqstp->rq_xprt, struct sock_xprt, xprt);
515
516         transport->inet->sk_write_pending--;
517         clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
518 }
519
520 /**
521  * xs_nospace - place task on wait queue if transmit was incomplete
522  * @task: task to put to sleep
523  *
524  */
525 static void xs_nospace(struct rpc_task *task)
526 {
527         struct rpc_rqst *req = task->tk_rqstp;
528         struct rpc_xprt *xprt = req->rq_xprt;
529         struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
530
531         dprintk("RPC: %5u xmit incomplete (%u left of %u)\n",
532                         task->tk_pid, req->rq_slen - req->rq_bytes_sent,
533                         req->rq_slen);
534
535         /* Protect against races with write_space */
536         spin_lock_bh(&xprt->transport_lock);
537
538         /* Don't race with disconnect */
539         if (xprt_connected(xprt)) {
540                 if (test_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags)) {
541                         /*
542                          * Notify TCP that we're limited by the application
543                          * window size
544                          */
545                         set_bit(SOCK_NOSPACE, &transport->sock->flags);
546                         transport->inet->sk_write_pending++;
547                         /* ...and wait for more buffer space */
548                         xprt_wait_for_buffer_space(task, xs_nospace_callback);
549                 }
550         } else {
551                 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
552                 task->tk_status = -ENOTCONN;
553         }
554
555         spin_unlock_bh(&xprt->transport_lock);
556 }
557
558 /**
559  * xs_udp_send_request - write an RPC request to a UDP socket
560  * @task: address of RPC task that manages the state of an RPC request
561  *
562  * Return values:
563  *        0:    The request has been sent
564  *   EAGAIN:    The socket was blocked, please call again later to
565  *              complete the request
566  * ENOTCONN:    Caller needs to invoke connect logic then call again
567  *    other:    Some other error occured, the request was not sent
568  */
569 static int xs_udp_send_request(struct rpc_task *task)
570 {
571         struct rpc_rqst *req = task->tk_rqstp;
572         struct rpc_xprt *xprt = req->rq_xprt;
573         struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
574         struct xdr_buf *xdr = &req->rq_snd_buf;
575         int status;
576
577         xs_pktdump("packet data:",
578                                 req->rq_svec->iov_base,
579                                 req->rq_svec->iov_len);
580
581         status = xs_sendpages(transport->sock,
582                               xs_addr(xprt),
583                               xprt->addrlen, xdr,
584                               req->rq_bytes_sent);
585
586         dprintk("RPC:       xs_udp_send_request(%u) = %d\n",
587                         xdr->len - req->rq_bytes_sent, status);
588
589         if (status >= 0) {
590                 task->tk_bytes_sent += status;
591                 if (status >= req->rq_slen)
592                         return 0;
593                 /* Still some bytes left; set up for a retry later. */
594                 status = -EAGAIN;
595         }
596
597         switch (status) {
598         case -EAGAIN:
599                 xs_nospace(task);
600                 break;
601         case -ENETUNREACH:
602         case -EPIPE:
603         case -ECONNREFUSED:
604                 /* When the server has died, an ICMP port unreachable message
605                  * prompts ECONNREFUSED. */
606                 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
607                 break;
608         default:
609                 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
610                 dprintk("RPC:       sendmsg returned unrecognized error %d\n",
611                         -status);
612         }
613
614         return status;
615 }
616
617 /**
618  * xs_tcp_shutdown - gracefully shut down a TCP socket
619  * @xprt: transport
620  *
621  * Initiates a graceful shutdown of the TCP socket by calling the
622  * equivalent of shutdown(SHUT_WR);
623  */
624 static void xs_tcp_shutdown(struct rpc_xprt *xprt)
625 {
626         struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
627         struct socket *sock = transport->sock;
628
629         if (sock != NULL)
630                 kernel_sock_shutdown(sock, SHUT_WR);
631 }
632
633 static inline void xs_encode_tcp_record_marker(struct xdr_buf *buf)
634 {
635         u32 reclen = buf->len - sizeof(rpc_fraghdr);
636         rpc_fraghdr *base = buf->head[0].iov_base;
637         *base = htonl(RPC_LAST_STREAM_FRAGMENT | reclen);
638 }
639
640 /**
641  * xs_tcp_send_request - write an RPC request to a TCP socket
642  * @task: address of RPC task that manages the state of an RPC request
643  *
644  * Return values:
645  *        0:    The request has been sent
646  *   EAGAIN:    The socket was blocked, please call again later to
647  *              complete the request
648  * ENOTCONN:    Caller needs to invoke connect logic then call again
649  *    other:    Some other error occured, the request was not sent
650  *
651  * XXX: In the case of soft timeouts, should we eventually give up
652  *      if sendmsg is not able to make progress?
653  */
654 static int xs_tcp_send_request(struct rpc_task *task)
655 {
656         struct rpc_rqst *req = task->tk_rqstp;
657         struct rpc_xprt *xprt = req->rq_xprt;
658         struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
659         struct xdr_buf *xdr = &req->rq_snd_buf;
660         int status;
661
662         xs_encode_tcp_record_marker(&req->rq_snd_buf);
663
664         xs_pktdump("packet data:",
665                                 req->rq_svec->iov_base,
666                                 req->rq_svec->iov_len);
667
668         /* Continue transmitting the packet/record. We must be careful
669          * to cope with writespace callbacks arriving _after_ we have
670          * called sendmsg(). */
671         while (1) {
672                 status = xs_sendpages(transport->sock,
673                                         NULL, 0, xdr, req->rq_bytes_sent);
674
675                 dprintk("RPC:       xs_tcp_send_request(%u) = %d\n",
676                                 xdr->len - req->rq_bytes_sent, status);
677
678                 if (unlikely(status < 0))
679                         break;
680
681                 /* If we've sent the entire packet, immediately
682                  * reset the count of bytes sent. */
683                 req->rq_bytes_sent += status;
684                 task->tk_bytes_sent += status;
685                 if (likely(req->rq_bytes_sent >= req->rq_slen)) {
686                         req->rq_bytes_sent = 0;
687                         return 0;
688                 }
689
690                 if (status != 0)
691                         continue;
692                 status = -EAGAIN;
693                 break;
694         }
695
696         switch (status) {
697         case -EAGAIN:
698                 xs_nospace(task);
699                 break;
700         case -ECONNREFUSED:
701         case -ECONNRESET:
702         case -ENOTCONN:
703         case -EPIPE:
704                 status = -ENOTCONN;
705                 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
706                 break;
707         default:
708                 dprintk("RPC:       sendmsg returned unrecognized error %d\n",
709                         -status);
710                 clear_bit(SOCK_ASYNC_NOSPACE, &transport->sock->flags);
711                 xs_tcp_shutdown(xprt);
712         }
713
714         return status;
715 }
716
717 /**
718  * xs_tcp_release_xprt - clean up after a tcp transmission
719  * @xprt: transport
720  * @task: rpc task
721  *
722  * This cleans up if an error causes us to abort the transmission of a request.
723  * In this case, the socket may need to be reset in order to avoid confusing
724  * the server.
725  */
726 static void xs_tcp_release_xprt(struct rpc_xprt *xprt, struct rpc_task *task)
727 {
728         struct rpc_rqst *req;
729
730         if (task != xprt->snd_task)
731                 return;
732         if (task == NULL)
733                 goto out_release;
734         req = task->tk_rqstp;
735         if (req->rq_bytes_sent == 0)
736                 goto out_release;
737         if (req->rq_bytes_sent == req->rq_snd_buf.len)
738                 goto out_release;
739         set_bit(XPRT_CLOSE_WAIT, &task->tk_xprt->state);
740 out_release:
741         xprt_release_xprt(xprt, task);
742 }
743
744 /**
745  * xs_close - close a socket
746  * @xprt: transport
747  *
748  * This is used when all requests are complete; ie, no DRC state remains
749  * on the server we want to save.
750  */
751 static void xs_close(struct rpc_xprt *xprt)
752 {
753         struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
754         struct socket *sock = transport->sock;
755         struct sock *sk = transport->inet;
756
757         if (!sk)
758                 goto clear_close_wait;
759
760         dprintk("RPC:       xs_close xprt %p\n", xprt);
761
762         write_lock_bh(&sk->sk_callback_lock);
763         transport->inet = NULL;
764         transport->sock = NULL;
765
766         sk->sk_user_data = NULL;
767         sk->sk_data_ready = transport->old_data_ready;
768         sk->sk_state_change = transport->old_state_change;
769         sk->sk_write_space = transport->old_write_space;
770         write_unlock_bh(&sk->sk_callback_lock);
771
772         sk->sk_no_check = 0;
773
774         sock_release(sock);
775 clear_close_wait:
776         smp_mb__before_clear_bit();
777         clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
778         clear_bit(XPRT_CLOSING, &xprt->state);
779         smp_mb__after_clear_bit();
780         xprt_disconnect_done(xprt);
781 }
782
783 /**
784  * xs_destroy - prepare to shutdown a transport
785  * @xprt: doomed transport
786  *
787  */
788 static void xs_destroy(struct rpc_xprt *xprt)
789 {
790         struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
791
792         dprintk("RPC:       xs_destroy xprt %p\n", xprt);
793
794         cancel_rearming_delayed_work(&transport->connect_worker);
795
796         xs_close(xprt);
797         xs_free_peer_addresses(xprt);
798         kfree(xprt->slot);
799         kfree(xprt);
800         module_put(THIS_MODULE);
801 }
802
803 static inline struct rpc_xprt *xprt_from_sock(struct sock *sk)
804 {
805         return (struct rpc_xprt *) sk->sk_user_data;
806 }
807
808 /**
809  * xs_udp_data_ready - "data ready" callback for UDP sockets
810  * @sk: socket with data to read
811  * @len: how much data to read
812  *
813  */
814 static void xs_udp_data_ready(struct sock *sk, int len)
815 {
816         struct rpc_task *task;
817         struct rpc_xprt *xprt;
818         struct rpc_rqst *rovr;
819         struct sk_buff *skb;
820         int err, repsize, copied;
821         u32 _xid;
822         __be32 *xp;
823
824         read_lock(&sk->sk_callback_lock);
825         dprintk("RPC:       xs_udp_data_ready...\n");
826         if (!(xprt = xprt_from_sock(sk)))
827                 goto out;
828
829         if ((skb = skb_recv_datagram(sk, 0, 1, &err)) == NULL)
830                 goto out;
831
832         if (xprt->shutdown)
833                 goto dropit;
834
835         repsize = skb->len - sizeof(struct udphdr);
836         if (repsize < 4) {
837                 dprintk("RPC:       impossible RPC reply size %d!\n", repsize);
838                 goto dropit;
839         }
840
841         /* Copy the XID from the skb... */
842         xp = skb_header_pointer(skb, sizeof(struct udphdr),
843                                 sizeof(_xid), &_xid);
844         if (xp == NULL)
845                 goto dropit;
846
847         /* Look up and lock the request corresponding to the given XID */
848         spin_lock(&xprt->transport_lock);
849         rovr = xprt_lookup_rqst(xprt, *xp);
850         if (!rovr)
851                 goto out_unlock;
852         task = rovr->rq_task;
853
854         if ((copied = rovr->rq_private_buf.buflen) > repsize)
855                 copied = repsize;
856
857         /* Suck it into the iovec, verify checksum if not done by hw. */
858         if (csum_partial_copy_to_xdr(&rovr->rq_private_buf, skb)) {
859                 UDPX_INC_STATS_BH(sk, UDP_MIB_INERRORS);
860                 goto out_unlock;
861         }
862
863         UDPX_INC_STATS_BH(sk, UDP_MIB_INDATAGRAMS);
864
865         /* Something worked... */
866         dst_confirm(skb->dst);
867
868         xprt_adjust_cwnd(task, copied);
869         xprt_update_rtt(task);
870         xprt_complete_rqst(task, copied);
871
872  out_unlock:
873         spin_unlock(&xprt->transport_lock);
874  dropit:
875         skb_free_datagram(sk, skb);
876  out:
877         read_unlock(&sk->sk_callback_lock);
878 }
879
880 static inline void xs_tcp_read_fraghdr(struct rpc_xprt *xprt, struct xdr_skb_reader *desc)
881 {
882         struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
883         size_t len, used;
884         char *p;
885
886         p = ((char *) &transport->tcp_fraghdr) + transport->tcp_offset;
887         len = sizeof(transport->tcp_fraghdr) - transport->tcp_offset;
888         used = xdr_skb_read_bits(desc, p, len);
889         transport->tcp_offset += used;
890         if (used != len)
891                 return;
892
893         transport->tcp_reclen = ntohl(transport->tcp_fraghdr);
894         if (transport->tcp_reclen & RPC_LAST_STREAM_FRAGMENT)
895                 transport->tcp_flags |= TCP_RCV_LAST_FRAG;
896         else
897                 transport->tcp_flags &= ~TCP_RCV_LAST_FRAG;
898         transport->tcp_reclen &= RPC_FRAGMENT_SIZE_MASK;
899
900         transport->tcp_flags &= ~TCP_RCV_COPY_FRAGHDR;
901         transport->tcp_offset = 0;
902
903         /* Sanity check of the record length */
904         if (unlikely(transport->tcp_reclen < 4)) {
905                 dprintk("RPC:       invalid TCP record fragment length\n");
906                 xprt_force_disconnect(xprt);
907                 return;
908         }
909         dprintk("RPC:       reading TCP record fragment of length %d\n",
910                         transport->tcp_reclen);
911 }
912
913 static void xs_tcp_check_fraghdr(struct sock_xprt *transport)
914 {
915         if (transport->tcp_offset == transport->tcp_reclen) {
916                 transport->tcp_flags |= TCP_RCV_COPY_FRAGHDR;
917                 transport->tcp_offset = 0;
918                 if (transport->tcp_flags & TCP_RCV_LAST_FRAG) {
919                         transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
920                         transport->tcp_flags |= TCP_RCV_COPY_XID;
921                         transport->tcp_copied = 0;
922                 }
923         }
924 }
925
926 static inline void xs_tcp_read_xid(struct sock_xprt *transport, struct xdr_skb_reader *desc)
927 {
928         size_t len, used;
929         char *p;
930
931         len = sizeof(transport->tcp_xid) - transport->tcp_offset;
932         dprintk("RPC:       reading XID (%Zu bytes)\n", len);
933         p = ((char *) &transport->tcp_xid) + transport->tcp_offset;
934         used = xdr_skb_read_bits(desc, p, len);
935         transport->tcp_offset += used;
936         if (used != len)
937                 return;
938         transport->tcp_flags &= ~TCP_RCV_COPY_XID;
939         transport->tcp_flags |= TCP_RCV_COPY_DATA;
940         transport->tcp_copied = 4;
941         dprintk("RPC:       reading reply for XID %08x\n",
942                         ntohl(transport->tcp_xid));
943         xs_tcp_check_fraghdr(transport);
944 }
945
946 static inline void xs_tcp_read_request(struct rpc_xprt *xprt, struct xdr_skb_reader *desc)
947 {
948         struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
949         struct rpc_rqst *req;
950         struct xdr_buf *rcvbuf;
951         size_t len;
952         ssize_t r;
953
954         /* Find and lock the request corresponding to this xid */
955         spin_lock(&xprt->transport_lock);
956         req = xprt_lookup_rqst(xprt, transport->tcp_xid);
957         if (!req) {
958                 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
959                 dprintk("RPC:       XID %08x request not found!\n",
960                                 ntohl(transport->tcp_xid));
961                 spin_unlock(&xprt->transport_lock);
962                 return;
963         }
964
965         rcvbuf = &req->rq_private_buf;
966         len = desc->count;
967         if (len > transport->tcp_reclen - transport->tcp_offset) {
968                 struct xdr_skb_reader my_desc;
969
970                 len = transport->tcp_reclen - transport->tcp_offset;
971                 memcpy(&my_desc, desc, sizeof(my_desc));
972                 my_desc.count = len;
973                 r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
974                                           &my_desc, xdr_skb_read_bits);
975                 desc->count -= r;
976                 desc->offset += r;
977         } else
978                 r = xdr_partial_copy_from_skb(rcvbuf, transport->tcp_copied,
979                                           desc, xdr_skb_read_bits);
980
981         if (r > 0) {
982                 transport->tcp_copied += r;
983                 transport->tcp_offset += r;
984         }
985         if (r != len) {
986                 /* Error when copying to the receive buffer,
987                  * usually because we weren't able to allocate
988                  * additional buffer pages. All we can do now
989                  * is turn off TCP_RCV_COPY_DATA, so the request
990                  * will not receive any additional updates,
991                  * and time out.
992                  * Any remaining data from this record will
993                  * be discarded.
994                  */
995                 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
996                 dprintk("RPC:       XID %08x truncated request\n",
997                                 ntohl(transport->tcp_xid));
998                 dprintk("RPC:       xprt = %p, tcp_copied = %lu, "
999                                 "tcp_offset = %u, tcp_reclen = %u\n",
1000                                 xprt, transport->tcp_copied,
1001                                 transport->tcp_offset, transport->tcp_reclen);
1002                 goto out;
1003         }
1004
1005         dprintk("RPC:       XID %08x read %Zd bytes\n",
1006                         ntohl(transport->tcp_xid), r);
1007         dprintk("RPC:       xprt = %p, tcp_copied = %lu, tcp_offset = %u, "
1008                         "tcp_reclen = %u\n", xprt, transport->tcp_copied,
1009                         transport->tcp_offset, transport->tcp_reclen);
1010
1011         if (transport->tcp_copied == req->rq_private_buf.buflen)
1012                 transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1013         else if (transport->tcp_offset == transport->tcp_reclen) {
1014                 if (transport->tcp_flags & TCP_RCV_LAST_FRAG)
1015                         transport->tcp_flags &= ~TCP_RCV_COPY_DATA;
1016         }
1017
1018 out:
1019         if (!(transport->tcp_flags & TCP_RCV_COPY_DATA))
1020                 xprt_complete_rqst(req->rq_task, transport->tcp_copied);
1021         spin_unlock(&xprt->transport_lock);
1022         xs_tcp_check_fraghdr(transport);
1023 }
1024
1025 static inline void xs_tcp_read_discard(struct sock_xprt *transport, struct xdr_skb_reader *desc)
1026 {
1027         size_t len;
1028
1029         len = transport->tcp_reclen - transport->tcp_offset;
1030         if (len > desc->count)
1031                 len = desc->count;
1032         desc->count -= len;
1033         desc->offset += len;
1034         transport->tcp_offset += len;
1035         dprintk("RPC:       discarded %Zu bytes\n", len);
1036         xs_tcp_check_fraghdr(transport);
1037 }
1038
1039 static int xs_tcp_data_recv(read_descriptor_t *rd_desc, struct sk_buff *skb, unsigned int offset, size_t len)
1040 {
1041         struct rpc_xprt *xprt = rd_desc->arg.data;
1042         struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1043         struct xdr_skb_reader desc = {
1044                 .skb    = skb,
1045                 .offset = offset,
1046                 .count  = len,
1047         };
1048
1049         dprintk("RPC:       xs_tcp_data_recv started\n");
1050         do {
1051                 /* Read in a new fragment marker if necessary */
1052                 /* Can we ever really expect to get completely empty fragments? */
1053                 if (transport->tcp_flags & TCP_RCV_COPY_FRAGHDR) {
1054                         xs_tcp_read_fraghdr(xprt, &desc);
1055                         continue;
1056                 }
1057                 /* Read in the xid if necessary */
1058                 if (transport->tcp_flags & TCP_RCV_COPY_XID) {
1059                         xs_tcp_read_xid(transport, &desc);
1060                         continue;
1061                 }
1062                 /* Read in the request data */
1063                 if (transport->tcp_flags & TCP_RCV_COPY_DATA) {
1064                         xs_tcp_read_request(xprt, &desc);
1065                         continue;
1066                 }
1067                 /* Skip over any trailing bytes on short reads */
1068                 xs_tcp_read_discard(transport, &desc);
1069         } while (desc.count);
1070         dprintk("RPC:       xs_tcp_data_recv done\n");
1071         return len - desc.count;
1072 }
1073
1074 /**
1075  * xs_tcp_data_ready - "data ready" callback for TCP sockets
1076  * @sk: socket with data to read
1077  * @bytes: how much data to read
1078  *
1079  */
1080 static void xs_tcp_data_ready(struct sock *sk, int bytes)
1081 {
1082         struct rpc_xprt *xprt;
1083         read_descriptor_t rd_desc;
1084         int read;
1085
1086         dprintk("RPC:       xs_tcp_data_ready...\n");
1087
1088         read_lock(&sk->sk_callback_lock);
1089         if (!(xprt = xprt_from_sock(sk)))
1090                 goto out;
1091         if (xprt->shutdown)
1092                 goto out;
1093
1094         /* We use rd_desc to pass struct xprt to xs_tcp_data_recv */
1095         rd_desc.arg.data = xprt;
1096         do {
1097                 rd_desc.count = 65536;
1098                 read = tcp_read_sock(sk, &rd_desc, xs_tcp_data_recv);
1099         } while (read > 0);
1100 out:
1101         read_unlock(&sk->sk_callback_lock);
1102 }
1103
1104 /**
1105  * xs_tcp_state_change - callback to handle TCP socket state changes
1106  * @sk: socket whose state has changed
1107  *
1108  */
1109 static void xs_tcp_state_change(struct sock *sk)
1110 {
1111         struct rpc_xprt *xprt;
1112
1113         read_lock(&sk->sk_callback_lock);
1114         if (!(xprt = xprt_from_sock(sk)))
1115                 goto out;
1116         dprintk("RPC:       xs_tcp_state_change client %p...\n", xprt);
1117         dprintk("RPC:       state %x conn %d dead %d zapped %d\n",
1118                         sk->sk_state, xprt_connected(xprt),
1119                         sock_flag(sk, SOCK_DEAD),
1120                         sock_flag(sk, SOCK_ZAPPED));
1121
1122         switch (sk->sk_state) {
1123         case TCP_ESTABLISHED:
1124                 spin_lock_bh(&xprt->transport_lock);
1125                 if (!xprt_test_and_set_connected(xprt)) {
1126                         struct sock_xprt *transport = container_of(xprt,
1127                                         struct sock_xprt, xprt);
1128
1129                         /* Reset TCP record info */
1130                         transport->tcp_offset = 0;
1131                         transport->tcp_reclen = 0;
1132                         transport->tcp_copied = 0;
1133                         transport->tcp_flags =
1134                                 TCP_RCV_COPY_FRAGHDR | TCP_RCV_COPY_XID;
1135
1136                         xprt_wake_pending_tasks(xprt, 0);
1137                 }
1138                 spin_unlock_bh(&xprt->transport_lock);
1139                 break;
1140         case TCP_FIN_WAIT1:
1141                 /* The client initiated a shutdown of the socket */
1142                 xprt->connect_cookie++;
1143                 xprt->reestablish_timeout = 0;
1144                 set_bit(XPRT_CLOSING, &xprt->state);
1145                 smp_mb__before_clear_bit();
1146                 clear_bit(XPRT_CONNECTED, &xprt->state);
1147                 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
1148                 smp_mb__after_clear_bit();
1149                 break;
1150         case TCP_CLOSE_WAIT:
1151                 /* The server initiated a shutdown of the socket */
1152                 set_bit(XPRT_CLOSING, &xprt->state);
1153                 xprt_force_disconnect(xprt);
1154         case TCP_SYN_SENT:
1155                 xprt->connect_cookie++;
1156         case TCP_CLOSING:
1157                 /*
1158                  * If the server closed down the connection, make sure that
1159                  * we back off before reconnecting
1160                  */
1161                 if (xprt->reestablish_timeout < XS_TCP_INIT_REEST_TO)
1162                         xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
1163                 break;
1164         case TCP_LAST_ACK:
1165                 smp_mb__before_clear_bit();
1166                 clear_bit(XPRT_CONNECTED, &xprt->state);
1167                 smp_mb__after_clear_bit();
1168                 break;
1169         case TCP_CLOSE:
1170                 smp_mb__before_clear_bit();
1171                 clear_bit(XPRT_CLOSE_WAIT, &xprt->state);
1172                 clear_bit(XPRT_CLOSING, &xprt->state);
1173                 smp_mb__after_clear_bit();
1174                 /* Mark transport as closed and wake up all pending tasks */
1175                 xprt_disconnect_done(xprt);
1176         }
1177  out:
1178         read_unlock(&sk->sk_callback_lock);
1179 }
1180
1181 /**
1182  * xs_udp_write_space - callback invoked when socket buffer space
1183  *                             becomes available
1184  * @sk: socket whose state has changed
1185  *
1186  * Called when more output buffer space is available for this socket.
1187  * We try not to wake our writers until they can make "significant"
1188  * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1189  * with a bunch of small requests.
1190  */
1191 static void xs_udp_write_space(struct sock *sk)
1192 {
1193         read_lock(&sk->sk_callback_lock);
1194
1195         /* from net/core/sock.c:sock_def_write_space */
1196         if (sock_writeable(sk)) {
1197                 struct socket *sock;
1198                 struct rpc_xprt *xprt;
1199
1200                 if (unlikely(!(sock = sk->sk_socket)))
1201                         goto out;
1202                 clear_bit(SOCK_NOSPACE, &sock->flags);
1203
1204                 if (unlikely(!(xprt = xprt_from_sock(sk))))
1205                         goto out;
1206                 if (test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags) == 0)
1207                         goto out;
1208
1209                 xprt_write_space(xprt);
1210         }
1211
1212  out:
1213         read_unlock(&sk->sk_callback_lock);
1214 }
1215
1216 /**
1217  * xs_tcp_write_space - callback invoked when socket buffer space
1218  *                             becomes available
1219  * @sk: socket whose state has changed
1220  *
1221  * Called when more output buffer space is available for this socket.
1222  * We try not to wake our writers until they can make "significant"
1223  * progress, otherwise we'll waste resources thrashing kernel_sendmsg
1224  * with a bunch of small requests.
1225  */
1226 static void xs_tcp_write_space(struct sock *sk)
1227 {
1228         read_lock(&sk->sk_callback_lock);
1229
1230         /* from net/core/stream.c:sk_stream_write_space */
1231         if (sk_stream_wspace(sk) >= sk_stream_min_wspace(sk)) {
1232                 struct socket *sock;
1233                 struct rpc_xprt *xprt;
1234
1235                 if (unlikely(!(sock = sk->sk_socket)))
1236                         goto out;
1237                 clear_bit(SOCK_NOSPACE, &sock->flags);
1238
1239                 if (unlikely(!(xprt = xprt_from_sock(sk))))
1240                         goto out;
1241                 if (test_and_clear_bit(SOCK_ASYNC_NOSPACE, &sock->flags) == 0)
1242                         goto out;
1243
1244                 xprt_write_space(xprt);
1245         }
1246
1247  out:
1248         read_unlock(&sk->sk_callback_lock);
1249 }
1250
1251 static void xs_udp_do_set_buffer_size(struct rpc_xprt *xprt)
1252 {
1253         struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1254         struct sock *sk = transport->inet;
1255
1256         if (transport->rcvsize) {
1257                 sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
1258                 sk->sk_rcvbuf = transport->rcvsize * xprt->max_reqs * 2;
1259         }
1260         if (transport->sndsize) {
1261                 sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
1262                 sk->sk_sndbuf = transport->sndsize * xprt->max_reqs * 2;
1263                 sk->sk_write_space(sk);
1264         }
1265 }
1266
1267 /**
1268  * xs_udp_set_buffer_size - set send and receive limits
1269  * @xprt: generic transport
1270  * @sndsize: requested size of send buffer, in bytes
1271  * @rcvsize: requested size of receive buffer, in bytes
1272  *
1273  * Set socket send and receive buffer size limits.
1274  */
1275 static void xs_udp_set_buffer_size(struct rpc_xprt *xprt, size_t sndsize, size_t rcvsize)
1276 {
1277         struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1278
1279         transport->sndsize = 0;
1280         if (sndsize)
1281                 transport->sndsize = sndsize + 1024;
1282         transport->rcvsize = 0;
1283         if (rcvsize)
1284                 transport->rcvsize = rcvsize + 1024;
1285
1286         xs_udp_do_set_buffer_size(xprt);
1287 }
1288
1289 /**
1290  * xs_udp_timer - called when a retransmit timeout occurs on a UDP transport
1291  * @task: task that timed out
1292  *
1293  * Adjust the congestion window after a retransmit timeout has occurred.
1294  */
1295 static void xs_udp_timer(struct rpc_task *task)
1296 {
1297         xprt_adjust_cwnd(task, -ETIMEDOUT);
1298 }
1299
1300 static unsigned short xs_get_random_port(void)
1301 {
1302         unsigned short range = xprt_max_resvport - xprt_min_resvport;
1303         unsigned short rand = (unsigned short) net_random() % range;
1304         return rand + xprt_min_resvport;
1305 }
1306
1307 /**
1308  * xs_set_port - reset the port number in the remote endpoint address
1309  * @xprt: generic transport
1310  * @port: new port number
1311  *
1312  */
1313 static void xs_set_port(struct rpc_xprt *xprt, unsigned short port)
1314 {
1315         struct sockaddr *addr = xs_addr(xprt);
1316
1317         dprintk("RPC:       setting port for xprt %p to %u\n", xprt, port);
1318
1319         switch (addr->sa_family) {
1320         case AF_INET:
1321                 ((struct sockaddr_in *)addr)->sin_port = htons(port);
1322                 break;
1323         case AF_INET6:
1324                 ((struct sockaddr_in6 *)addr)->sin6_port = htons(port);
1325                 break;
1326         default:
1327                 BUG();
1328         }
1329 }
1330
1331 static unsigned short xs_get_srcport(struct sock_xprt *transport, struct socket *sock)
1332 {
1333         unsigned short port = transport->port;
1334
1335         if (port == 0 && transport->xprt.resvport)
1336                 port = xs_get_random_port();
1337         return port;
1338 }
1339
1340 static unsigned short xs_next_srcport(struct sock_xprt *transport, struct socket *sock, unsigned short port)
1341 {
1342         if (transport->port != 0)
1343                 transport->port = 0;
1344         if (!transport->xprt.resvport)
1345                 return 0;
1346         if (port <= xprt_min_resvport || port > xprt_max_resvport)
1347                 return xprt_max_resvport;
1348         return --port;
1349 }
1350
1351 static int xs_bind4(struct sock_xprt *transport, struct socket *sock)
1352 {
1353         struct sockaddr_in myaddr = {
1354                 .sin_family = AF_INET,
1355         };
1356         struct sockaddr_in *sa;
1357         int err, nloop = 0;
1358         unsigned short port = xs_get_srcport(transport, sock);
1359         unsigned short last;
1360
1361         sa = (struct sockaddr_in *)&transport->addr;
1362         myaddr.sin_addr = sa->sin_addr;
1363         do {
1364                 myaddr.sin_port = htons(port);
1365                 err = kernel_bind(sock, (struct sockaddr *) &myaddr,
1366                                                 sizeof(myaddr));
1367                 if (port == 0)
1368                         break;
1369                 if (err == 0) {
1370                         transport->port = port;
1371                         break;
1372                 }
1373                 last = port;
1374                 port = xs_next_srcport(transport, sock, port);
1375                 if (port > last)
1376                         nloop++;
1377         } while (err == -EADDRINUSE && nloop != 2);
1378         dprintk("RPC:       %s "NIPQUAD_FMT":%u: %s (%d)\n",
1379                         __func__, NIPQUAD(myaddr.sin_addr),
1380                         port, err ? "failed" : "ok", err);
1381         return err;
1382 }
1383
1384 static int xs_bind6(struct sock_xprt *transport, struct socket *sock)
1385 {
1386         struct sockaddr_in6 myaddr = {
1387                 .sin6_family = AF_INET6,
1388         };
1389         struct sockaddr_in6 *sa;
1390         int err, nloop = 0;
1391         unsigned short port = xs_get_srcport(transport, sock);
1392         unsigned short last;
1393
1394         sa = (struct sockaddr_in6 *)&transport->addr;
1395         myaddr.sin6_addr = sa->sin6_addr;
1396         do {
1397                 myaddr.sin6_port = htons(port);
1398                 err = kernel_bind(sock, (struct sockaddr *) &myaddr,
1399                                                 sizeof(myaddr));
1400                 if (port == 0)
1401                         break;
1402                 if (err == 0) {
1403                         transport->port = port;
1404                         break;
1405                 }
1406                 last = port;
1407                 port = xs_next_srcport(transport, sock, port);
1408                 if (port > last)
1409                         nloop++;
1410         } while (err == -EADDRINUSE && nloop != 2);
1411         dprintk("RPC:       xs_bind6 "NIP6_FMT":%u: %s (%d)\n",
1412                 NIP6(myaddr.sin6_addr), port, err ? "failed" : "ok", err);
1413         return err;
1414 }
1415
1416 #ifdef CONFIG_DEBUG_LOCK_ALLOC
1417 static struct lock_class_key xs_key[2];
1418 static struct lock_class_key xs_slock_key[2];
1419
1420 static inline void xs_reclassify_socket4(struct socket *sock)
1421 {
1422         struct sock *sk = sock->sk;
1423
1424         BUG_ON(sock_owned_by_user(sk));
1425         sock_lock_init_class_and_name(sk, "slock-AF_INET-RPC",
1426                 &xs_slock_key[0], "sk_lock-AF_INET-RPC", &xs_key[0]);
1427 }
1428
1429 static inline void xs_reclassify_socket6(struct socket *sock)
1430 {
1431         struct sock *sk = sock->sk;
1432
1433         BUG_ON(sock_owned_by_user(sk));
1434         sock_lock_init_class_and_name(sk, "slock-AF_INET6-RPC",
1435                 &xs_slock_key[1], "sk_lock-AF_INET6-RPC", &xs_key[1]);
1436 }
1437 #else
1438 static inline void xs_reclassify_socket4(struct socket *sock)
1439 {
1440 }
1441
1442 static inline void xs_reclassify_socket6(struct socket *sock)
1443 {
1444 }
1445 #endif
1446
1447 static void xs_udp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
1448 {
1449         struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1450
1451         if (!transport->inet) {
1452                 struct sock *sk = sock->sk;
1453
1454                 write_lock_bh(&sk->sk_callback_lock);
1455
1456                 sk->sk_user_data = xprt;
1457                 transport->old_data_ready = sk->sk_data_ready;
1458                 transport->old_state_change = sk->sk_state_change;
1459                 transport->old_write_space = sk->sk_write_space;
1460                 sk->sk_data_ready = xs_udp_data_ready;
1461                 sk->sk_write_space = xs_udp_write_space;
1462                 sk->sk_no_check = UDP_CSUM_NORCV;
1463                 sk->sk_allocation = GFP_ATOMIC;
1464
1465                 xprt_set_connected(xprt);
1466
1467                 /* Reset to new socket */
1468                 transport->sock = sock;
1469                 transport->inet = sk;
1470
1471                 write_unlock_bh(&sk->sk_callback_lock);
1472         }
1473         xs_udp_do_set_buffer_size(xprt);
1474 }
1475
1476 /**
1477  * xs_udp_connect_worker4 - set up a UDP socket
1478  * @work: RPC transport to connect
1479  *
1480  * Invoked by a work queue tasklet.
1481  */
1482 static void xs_udp_connect_worker4(struct work_struct *work)
1483 {
1484         struct sock_xprt *transport =
1485                 container_of(work, struct sock_xprt, connect_worker.work);
1486         struct rpc_xprt *xprt = &transport->xprt;
1487         struct socket *sock = transport->sock;
1488         int err, status = -EIO;
1489
1490         if (xprt->shutdown || !xprt_bound(xprt))
1491                 goto out;
1492
1493         /* Start by resetting any existing state */
1494         xs_close(xprt);
1495
1496         if ((err = sock_create_kern(PF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock)) < 0) {
1497                 dprintk("RPC:       can't create UDP transport socket (%d).\n", -err);
1498                 goto out;
1499         }
1500         xs_reclassify_socket4(sock);
1501
1502         if (xs_bind4(transport, sock)) {
1503                 sock_release(sock);
1504                 goto out;
1505         }
1506
1507         dprintk("RPC:       worker connecting xprt %p to address: %s\n",
1508                         xprt, xprt->address_strings[RPC_DISPLAY_ALL]);
1509
1510         xs_udp_finish_connecting(xprt, sock);
1511         status = 0;
1512 out:
1513         xprt_wake_pending_tasks(xprt, status);
1514         xprt_clear_connecting(xprt);
1515 }
1516
1517 /**
1518  * xs_udp_connect_worker6 - set up a UDP socket
1519  * @work: RPC transport to connect
1520  *
1521  * Invoked by a work queue tasklet.
1522  */
1523 static void xs_udp_connect_worker6(struct work_struct *work)
1524 {
1525         struct sock_xprt *transport =
1526                 container_of(work, struct sock_xprt, connect_worker.work);
1527         struct rpc_xprt *xprt = &transport->xprt;
1528         struct socket *sock = transport->sock;
1529         int err, status = -EIO;
1530
1531         if (xprt->shutdown || !xprt_bound(xprt))
1532                 goto out;
1533
1534         /* Start by resetting any existing state */
1535         xs_close(xprt);
1536
1537         if ((err = sock_create_kern(PF_INET6, SOCK_DGRAM, IPPROTO_UDP, &sock)) < 0) {
1538                 dprintk("RPC:       can't create UDP transport socket (%d).\n", -err);
1539                 goto out;
1540         }
1541         xs_reclassify_socket6(sock);
1542
1543         if (xs_bind6(transport, sock) < 0) {
1544                 sock_release(sock);
1545                 goto out;
1546         }
1547
1548         dprintk("RPC:       worker connecting xprt %p to address: %s\n",
1549                         xprt, xprt->address_strings[RPC_DISPLAY_ALL]);
1550
1551         xs_udp_finish_connecting(xprt, sock);
1552         status = 0;
1553 out:
1554         xprt_wake_pending_tasks(xprt, status);
1555         xprt_clear_connecting(xprt);
1556 }
1557
1558 /*
1559  * We need to preserve the port number so the reply cache on the server can
1560  * find our cached RPC replies when we get around to reconnecting.
1561  */
1562 static void xs_tcp_reuse_connection(struct rpc_xprt *xprt)
1563 {
1564         int result;
1565         struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1566         struct sockaddr any;
1567
1568         dprintk("RPC:       disconnecting xprt %p to reuse port\n", xprt);
1569
1570         /*
1571          * Disconnect the transport socket by doing a connect operation
1572          * with AF_UNSPEC.  This should return immediately...
1573          */
1574         memset(&any, 0, sizeof(any));
1575         any.sa_family = AF_UNSPEC;
1576         result = kernel_connect(transport->sock, &any, sizeof(any), 0);
1577         if (result)
1578                 dprintk("RPC:       AF_UNSPEC connect return code %d\n",
1579                                 result);
1580 }
1581
1582 static int xs_tcp_finish_connecting(struct rpc_xprt *xprt, struct socket *sock)
1583 {
1584         struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1585
1586         if (!transport->inet) {
1587                 struct sock *sk = sock->sk;
1588
1589                 write_lock_bh(&sk->sk_callback_lock);
1590
1591                 sk->sk_user_data = xprt;
1592                 transport->old_data_ready = sk->sk_data_ready;
1593                 transport->old_state_change = sk->sk_state_change;
1594                 transport->old_write_space = sk->sk_write_space;
1595                 sk->sk_data_ready = xs_tcp_data_ready;
1596                 sk->sk_state_change = xs_tcp_state_change;
1597                 sk->sk_write_space = xs_tcp_write_space;
1598                 sk->sk_allocation = GFP_ATOMIC;
1599
1600                 /* socket options */
1601                 sk->sk_userlocks |= SOCK_BINDPORT_LOCK;
1602                 sock_reset_flag(sk, SOCK_LINGER);
1603                 tcp_sk(sk)->linger2 = 0;
1604                 tcp_sk(sk)->nonagle |= TCP_NAGLE_OFF;
1605
1606                 xprt_clear_connected(xprt);
1607
1608                 /* Reset to new socket */
1609                 transport->sock = sock;
1610                 transport->inet = sk;
1611
1612                 write_unlock_bh(&sk->sk_callback_lock);
1613         }
1614
1615         /* Tell the socket layer to start connecting... */
1616         xprt->stat.connect_count++;
1617         xprt->stat.connect_start = jiffies;
1618         return kernel_connect(sock, xs_addr(xprt), xprt->addrlen, O_NONBLOCK);
1619 }
1620
1621 /**
1622  * xs_tcp_connect_worker4 - connect a TCP socket to a remote endpoint
1623  * @work: RPC transport to connect
1624  *
1625  * Invoked by a work queue tasklet.
1626  */
1627 static void xs_tcp_connect_worker4(struct work_struct *work)
1628 {
1629         struct sock_xprt *transport =
1630                 container_of(work, struct sock_xprt, connect_worker.work);
1631         struct rpc_xprt *xprt = &transport->xprt;
1632         struct socket *sock = transport->sock;
1633         int err, status = -EIO;
1634
1635         if (xprt->shutdown || !xprt_bound(xprt))
1636                 goto out;
1637
1638         if (!sock) {
1639                 /* start from scratch */
1640                 if ((err = sock_create_kern(PF_INET, SOCK_STREAM, IPPROTO_TCP, &sock)) < 0) {
1641                         dprintk("RPC:       can't create TCP transport socket (%d).\n", -err);
1642                         goto out;
1643                 }
1644                 xs_reclassify_socket4(sock);
1645
1646                 if (xs_bind4(transport, sock) < 0) {
1647                         sock_release(sock);
1648                         goto out;
1649                 }
1650         } else
1651                 /* "close" the socket, preserving the local port */
1652                 xs_tcp_reuse_connection(xprt);
1653
1654         dprintk("RPC:       worker connecting xprt %p to address: %s\n",
1655                         xprt, xprt->address_strings[RPC_DISPLAY_ALL]);
1656
1657         status = xs_tcp_finish_connecting(xprt, sock);
1658         dprintk("RPC:       %p connect status %d connected %d sock state %d\n",
1659                         xprt, -status, xprt_connected(xprt),
1660                         sock->sk->sk_state);
1661         if (status < 0) {
1662                 switch (status) {
1663                         case -EINPROGRESS:
1664                         case -EALREADY:
1665                                 goto out_clear;
1666                         case -ECONNREFUSED:
1667                         case -ECONNRESET:
1668                                 /* retry with existing socket, after a delay */
1669                                 break;
1670                         default:
1671                                 /* get rid of existing socket, and retry */
1672                                 xs_tcp_shutdown(xprt);
1673                 }
1674         }
1675 out:
1676         xprt_wake_pending_tasks(xprt, status);
1677 out_clear:
1678         xprt_clear_connecting(xprt);
1679 }
1680
1681 /**
1682  * xs_tcp_connect_worker6 - connect a TCP socket to a remote endpoint
1683  * @work: RPC transport to connect
1684  *
1685  * Invoked by a work queue tasklet.
1686  */
1687 static void xs_tcp_connect_worker6(struct work_struct *work)
1688 {
1689         struct sock_xprt *transport =
1690                 container_of(work, struct sock_xprt, connect_worker.work);
1691         struct rpc_xprt *xprt = &transport->xprt;
1692         struct socket *sock = transport->sock;
1693         int err, status = -EIO;
1694
1695         if (xprt->shutdown || !xprt_bound(xprt))
1696                 goto out;
1697
1698         if (!sock) {
1699                 /* start from scratch */
1700                 if ((err = sock_create_kern(PF_INET6, SOCK_STREAM, IPPROTO_TCP, &sock)) < 0) {
1701                         dprintk("RPC:       can't create TCP transport socket (%d).\n", -err);
1702                         goto out;
1703                 }
1704                 xs_reclassify_socket6(sock);
1705
1706                 if (xs_bind6(transport, sock) < 0) {
1707                         sock_release(sock);
1708                         goto out;
1709                 }
1710         } else
1711                 /* "close" the socket, preserving the local port */
1712                 xs_tcp_reuse_connection(xprt);
1713
1714         dprintk("RPC:       worker connecting xprt %p to address: %s\n",
1715                         xprt, xprt->address_strings[RPC_DISPLAY_ALL]);
1716
1717         status = xs_tcp_finish_connecting(xprt, sock);
1718         dprintk("RPC:       %p connect status %d connected %d sock state %d\n",
1719                         xprt, -status, xprt_connected(xprt), sock->sk->sk_state);
1720         if (status < 0) {
1721                 switch (status) {
1722                         case -EINPROGRESS:
1723                         case -EALREADY:
1724                                 goto out_clear;
1725                         case -ECONNREFUSED:
1726                         case -ECONNRESET:
1727                                 /* retry with existing socket, after a delay */
1728                                 break;
1729                         default:
1730                                 /* get rid of existing socket, and retry */
1731                                 xs_tcp_shutdown(xprt);
1732                 }
1733         }
1734 out:
1735         xprt_wake_pending_tasks(xprt, status);
1736 out_clear:
1737         xprt_clear_connecting(xprt);
1738 }
1739
1740 /**
1741  * xs_connect - connect a socket to a remote endpoint
1742  * @task: address of RPC task that manages state of connect request
1743  *
1744  * TCP: If the remote end dropped the connection, delay reconnecting.
1745  *
1746  * UDP socket connects are synchronous, but we use a work queue anyway
1747  * to guarantee that even unprivileged user processes can set up a
1748  * socket on a privileged port.
1749  *
1750  * If a UDP socket connect fails, the delay behavior here prevents
1751  * retry floods (hard mounts).
1752  */
1753 static void xs_connect(struct rpc_task *task)
1754 {
1755         struct rpc_xprt *xprt = task->tk_xprt;
1756         struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1757
1758         if (xprt_test_and_set_connecting(xprt))
1759                 return;
1760
1761         if (transport->sock != NULL) {
1762                 dprintk("RPC:       xs_connect delayed xprt %p for %lu "
1763                                 "seconds\n",
1764                                 xprt, xprt->reestablish_timeout / HZ);
1765                 queue_delayed_work(rpciod_workqueue,
1766                                    &transport->connect_worker,
1767                                    xprt->reestablish_timeout);
1768                 xprt->reestablish_timeout <<= 1;
1769                 if (xprt->reestablish_timeout > XS_TCP_MAX_REEST_TO)
1770                         xprt->reestablish_timeout = XS_TCP_MAX_REEST_TO;
1771         } else {
1772                 dprintk("RPC:       xs_connect scheduled xprt %p\n", xprt);
1773                 queue_delayed_work(rpciod_workqueue,
1774                                    &transport->connect_worker, 0);
1775         }
1776 }
1777
1778 static void xs_tcp_connect(struct rpc_task *task)
1779 {
1780         struct rpc_xprt *xprt = task->tk_xprt;
1781
1782         /* Initiate graceful shutdown of the socket if not already done */
1783         if (test_bit(XPRT_CONNECTED, &xprt->state))
1784                 xs_tcp_shutdown(xprt);
1785         /* Exit if we need to wait for socket shutdown to complete */
1786         if (test_bit(XPRT_CLOSING, &xprt->state))
1787                 return;
1788         xs_connect(task);
1789 }
1790
1791 /**
1792  * xs_udp_print_stats - display UDP socket-specifc stats
1793  * @xprt: rpc_xprt struct containing statistics
1794  * @seq: output file
1795  *
1796  */
1797 static void xs_udp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
1798 {
1799         struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1800
1801         seq_printf(seq, "\txprt:\tudp %u %lu %lu %lu %lu %Lu %Lu\n",
1802                         transport->port,
1803                         xprt->stat.bind_count,
1804                         xprt->stat.sends,
1805                         xprt->stat.recvs,
1806                         xprt->stat.bad_xids,
1807                         xprt->stat.req_u,
1808                         xprt->stat.bklog_u);
1809 }
1810
1811 /**
1812  * xs_tcp_print_stats - display TCP socket-specifc stats
1813  * @xprt: rpc_xprt struct containing statistics
1814  * @seq: output file
1815  *
1816  */
1817 static void xs_tcp_print_stats(struct rpc_xprt *xprt, struct seq_file *seq)
1818 {
1819         struct sock_xprt *transport = container_of(xprt, struct sock_xprt, xprt);
1820         long idle_time = 0;
1821
1822         if (xprt_connected(xprt))
1823                 idle_time = (long)(jiffies - xprt->last_used) / HZ;
1824
1825         seq_printf(seq, "\txprt:\ttcp %u %lu %lu %lu %ld %lu %lu %lu %Lu %Lu\n",
1826                         transport->port,
1827                         xprt->stat.bind_count,
1828                         xprt->stat.connect_count,
1829                         xprt->stat.connect_time,
1830                         idle_time,
1831                         xprt->stat.sends,
1832                         xprt->stat.recvs,
1833                         xprt->stat.bad_xids,
1834                         xprt->stat.req_u,
1835                         xprt->stat.bklog_u);
1836 }
1837
1838 static struct rpc_xprt_ops xs_udp_ops = {
1839         .set_buffer_size        = xs_udp_set_buffer_size,
1840         .reserve_xprt           = xprt_reserve_xprt_cong,
1841         .release_xprt           = xprt_release_xprt_cong,
1842         .rpcbind                = rpcb_getport_async,
1843         .set_port               = xs_set_port,
1844         .connect                = xs_connect,
1845         .buf_alloc              = rpc_malloc,
1846         .buf_free               = rpc_free,
1847         .send_request           = xs_udp_send_request,
1848         .set_retrans_timeout    = xprt_set_retrans_timeout_rtt,
1849         .timer                  = xs_udp_timer,
1850         .release_request        = xprt_release_rqst_cong,
1851         .close                  = xs_close,
1852         .destroy                = xs_destroy,
1853         .print_stats            = xs_udp_print_stats,
1854 };
1855
1856 static struct rpc_xprt_ops xs_tcp_ops = {
1857         .reserve_xprt           = xprt_reserve_xprt,
1858         .release_xprt           = xs_tcp_release_xprt,
1859         .rpcbind                = rpcb_getport_async,
1860         .set_port               = xs_set_port,
1861         .connect                = xs_tcp_connect,
1862         .buf_alloc              = rpc_malloc,
1863         .buf_free               = rpc_free,
1864         .send_request           = xs_tcp_send_request,
1865         .set_retrans_timeout    = xprt_set_retrans_timeout_def,
1866         .close                  = xs_tcp_shutdown,
1867         .destroy                = xs_destroy,
1868         .print_stats            = xs_tcp_print_stats,
1869 };
1870
1871 static struct rpc_xprt *xs_setup_xprt(struct xprt_create *args,
1872                                       unsigned int slot_table_size)
1873 {
1874         struct rpc_xprt *xprt;
1875         struct sock_xprt *new;
1876
1877         if (args->addrlen > sizeof(xprt->addr)) {
1878                 dprintk("RPC:       xs_setup_xprt: address too large\n");
1879                 return ERR_PTR(-EBADF);
1880         }
1881
1882         new = kzalloc(sizeof(*new), GFP_KERNEL);
1883         if (new == NULL) {
1884                 dprintk("RPC:       xs_setup_xprt: couldn't allocate "
1885                                 "rpc_xprt\n");
1886                 return ERR_PTR(-ENOMEM);
1887         }
1888         xprt = &new->xprt;
1889
1890         xprt->max_reqs = slot_table_size;
1891         xprt->slot = kcalloc(xprt->max_reqs, sizeof(struct rpc_rqst), GFP_KERNEL);
1892         if (xprt->slot == NULL) {
1893                 kfree(xprt);
1894                 dprintk("RPC:       xs_setup_xprt: couldn't allocate slot "
1895                                 "table\n");
1896                 return ERR_PTR(-ENOMEM);
1897         }
1898
1899         memcpy(&xprt->addr, args->dstaddr, args->addrlen);
1900         xprt->addrlen = args->addrlen;
1901         if (args->srcaddr)
1902                 memcpy(&new->addr, args->srcaddr, args->addrlen);
1903
1904         return xprt;
1905 }
1906
1907 static const struct rpc_timeout xs_udp_default_timeout = {
1908         .to_initval = 5 * HZ,
1909         .to_maxval = 30 * HZ,
1910         .to_increment = 5 * HZ,
1911         .to_retries = 5,
1912 };
1913
1914 /**
1915  * xs_setup_udp - Set up transport to use a UDP socket
1916  * @args: rpc transport creation arguments
1917  *
1918  */
1919 static struct rpc_xprt *xs_setup_udp(struct xprt_create *args)
1920 {
1921         struct sockaddr *addr = args->dstaddr;
1922         struct rpc_xprt *xprt;
1923         struct sock_xprt *transport;
1924
1925         xprt = xs_setup_xprt(args, xprt_udp_slot_table_entries);
1926         if (IS_ERR(xprt))
1927                 return xprt;
1928         transport = container_of(xprt, struct sock_xprt, xprt);
1929
1930         xprt->prot = IPPROTO_UDP;
1931         xprt->tsh_size = 0;
1932         /* XXX: header size can vary due to auth type, IPv6, etc. */
1933         xprt->max_payload = (1U << 16) - (MAX_HEADER << 3);
1934
1935         xprt->bind_timeout = XS_BIND_TO;
1936         xprt->connect_timeout = XS_UDP_CONN_TO;
1937         xprt->reestablish_timeout = XS_UDP_REEST_TO;
1938         xprt->idle_timeout = XS_IDLE_DISC_TO;
1939
1940         xprt->ops = &xs_udp_ops;
1941
1942         xprt->timeout = &xs_udp_default_timeout;
1943
1944         switch (addr->sa_family) {
1945         case AF_INET:
1946                 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
1947                         xprt_set_bound(xprt);
1948
1949                 INIT_DELAYED_WORK(&transport->connect_worker,
1950                                         xs_udp_connect_worker4);
1951                 xs_format_ipv4_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP);
1952                 break;
1953         case AF_INET6:
1954                 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
1955                         xprt_set_bound(xprt);
1956
1957                 INIT_DELAYED_WORK(&transport->connect_worker,
1958                                         xs_udp_connect_worker6);
1959                 xs_format_ipv6_peer_addresses(xprt, "udp", RPCBIND_NETID_UDP6);
1960                 break;
1961         default:
1962                 kfree(xprt);
1963                 return ERR_PTR(-EAFNOSUPPORT);
1964         }
1965
1966         dprintk("RPC:       set up transport to address %s\n",
1967                         xprt->address_strings[RPC_DISPLAY_ALL]);
1968
1969         if (try_module_get(THIS_MODULE))
1970                 return xprt;
1971
1972         kfree(xprt->slot);
1973         kfree(xprt);
1974         return ERR_PTR(-EINVAL);
1975 }
1976
1977 static const struct rpc_timeout xs_tcp_default_timeout = {
1978         .to_initval = 60 * HZ,
1979         .to_maxval = 60 * HZ,
1980         .to_retries = 2,
1981 };
1982
1983 /**
1984  * xs_setup_tcp - Set up transport to use a TCP socket
1985  * @args: rpc transport creation arguments
1986  *
1987  */
1988 static struct rpc_xprt *xs_setup_tcp(struct xprt_create *args)
1989 {
1990         struct sockaddr *addr = args->dstaddr;
1991         struct rpc_xprt *xprt;
1992         struct sock_xprt *transport;
1993
1994         xprt = xs_setup_xprt(args, xprt_tcp_slot_table_entries);
1995         if (IS_ERR(xprt))
1996                 return xprt;
1997         transport = container_of(xprt, struct sock_xprt, xprt);
1998
1999         xprt->prot = IPPROTO_TCP;
2000         xprt->tsh_size = sizeof(rpc_fraghdr) / sizeof(u32);
2001         xprt->max_payload = RPC_MAX_FRAGMENT_SIZE;
2002
2003         xprt->bind_timeout = XS_BIND_TO;
2004         xprt->connect_timeout = XS_TCP_CONN_TO;
2005         xprt->reestablish_timeout = XS_TCP_INIT_REEST_TO;
2006         xprt->idle_timeout = XS_IDLE_DISC_TO;
2007
2008         xprt->ops = &xs_tcp_ops;
2009         xprt->timeout = &xs_tcp_default_timeout;
2010
2011         switch (addr->sa_family) {
2012         case AF_INET:
2013                 if (((struct sockaddr_in *)addr)->sin_port != htons(0))
2014                         xprt_set_bound(xprt);
2015
2016                 INIT_DELAYED_WORK(&transport->connect_worker, xs_tcp_connect_worker4);
2017                 xs_format_ipv4_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP);
2018                 break;
2019         case AF_INET6:
2020                 if (((struct sockaddr_in6 *)addr)->sin6_port != htons(0))
2021                         xprt_set_bound(xprt);
2022
2023                 INIT_DELAYED_WORK(&transport->connect_worker, xs_tcp_connect_worker6);
2024                 xs_format_ipv6_peer_addresses(xprt, "tcp", RPCBIND_NETID_TCP6);
2025                 break;
2026         default:
2027                 kfree(xprt);
2028                 return ERR_PTR(-EAFNOSUPPORT);
2029         }
2030
2031         dprintk("RPC:       set up transport to address %s\n",
2032                         xprt->address_strings[RPC_DISPLAY_ALL]);
2033
2034         if (try_module_get(THIS_MODULE))
2035                 return xprt;
2036
2037         kfree(xprt->slot);
2038         kfree(xprt);
2039         return ERR_PTR(-EINVAL);
2040 }
2041
2042 static struct xprt_class        xs_udp_transport = {
2043         .list           = LIST_HEAD_INIT(xs_udp_transport.list),
2044         .name           = "udp",
2045         .owner          = THIS_MODULE,
2046         .ident          = IPPROTO_UDP,
2047         .setup          = xs_setup_udp,
2048 };
2049
2050 static struct xprt_class        xs_tcp_transport = {
2051         .list           = LIST_HEAD_INIT(xs_tcp_transport.list),
2052         .name           = "tcp",
2053         .owner          = THIS_MODULE,
2054         .ident          = IPPROTO_TCP,
2055         .setup          = xs_setup_tcp,
2056 };
2057
2058 /**
2059  * init_socket_xprt - set up xprtsock's sysctls, register with RPC client
2060  *
2061  */
2062 int init_socket_xprt(void)
2063 {
2064 #ifdef RPC_DEBUG
2065         if (!sunrpc_table_header)
2066                 sunrpc_table_header = register_sysctl_table(sunrpc_table);
2067 #endif
2068
2069         xprt_register_transport(&xs_udp_transport);
2070         xprt_register_transport(&xs_tcp_transport);
2071
2072         return 0;
2073 }
2074
2075 /**
2076  * cleanup_socket_xprt - remove xprtsock's sysctls, unregister
2077  *
2078  */
2079 void cleanup_socket_xprt(void)
2080 {
2081 #ifdef RPC_DEBUG
2082         if (sunrpc_table_header) {
2083                 unregister_sysctl_table(sunrpc_table_header);
2084                 sunrpc_table_header = NULL;
2085         }
2086 #endif
2087
2088         xprt_unregister_transport(&xs_udp_transport);
2089         xprt_unregister_transport(&xs_tcp_transport);
2090 }