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