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