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