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