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

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

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

~ [ 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