~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/net/sunrpc/xprtsock.c

Version: ~ [ linux-5.6-rc1 ] ~ [ linux-5.5.2 ] ~ [ linux-5.4.17 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.102 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.170 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.213 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.213 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.81 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp