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

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

Version: ~ [ linux-5.8-rc3 ] ~ [ linux-5.7.5 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.48 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.129 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.185 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.228 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.228 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.85 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

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