1 // SPDX-License-Identifier: GPL-2.0-only
3 * linux/net/sunrpc/clnt.c
5 * This file contains the high-level RPC interface.
6 * It is modeled as a finite state machine to support both synchronous
7 * and asynchronous requests.
9 * - RPC header generation and argument serialization.
10 * - Credential refresh.
11 * - TCP connect handling.
12 * - Retry of operation when it is suspected the operation failed because
13 * of uid squashing on the server, or when the credentials were stale
14 * and need to be refreshed, or when a packet was damaged in transit.
15 * This may be have to be moved to the VFS layer.
17 * Copyright (C) 1992,1993 Rick Sladkey <jrs@world.std.com>
18 * Copyright (C) 1995,1996 Olaf Kirch <okir@monad.swb.de>
22 #include <linux/module.h>
23 #include <linux/types.h>
24 #include <linux/kallsyms.h>
26 #include <linux/namei.h>
27 #include <linux/mount.h>
28 #include <linux/slab.h>
29 #include <linux/rcupdate.h>
30 #include <linux/utsname.h>
31 #include <linux/workqueue.h>
33 #include <linux/in6.h>
36 #include <linux/sunrpc/clnt.h>
37 #include <linux/sunrpc/addr.h>
38 #include <linux/sunrpc/rpc_pipe_fs.h>
39 #include <linux/sunrpc/metrics.h>
40 #include <linux/sunrpc/bc_xprt.h>
41 #include <trace/events/sunrpc.h>
46 #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
47 # define RPCDBG_FACILITY RPCDBG_CALL
51 * All RPC clients are linked into this list
54 static DECLARE_WAIT_QUEUE_HEAD(destroy_wait
);
57 static void call_start(struct rpc_task
*task
);
58 static void call_reserve(struct rpc_task
*task
);
59 static void call_reserveresult(struct rpc_task
*task
);
60 static void call_allocate(struct rpc_task
*task
);
61 static void call_encode(struct rpc_task
*task
);
62 static void call_decode(struct rpc_task
*task
);
63 static void call_bind(struct rpc_task
*task
);
64 static void call_bind_status(struct rpc_task
*task
);
65 static void call_transmit(struct rpc_task
*task
);
66 static void call_status(struct rpc_task
*task
);
67 static void call_transmit_status(struct rpc_task
*task
);
68 static void call_refresh(struct rpc_task
*task
);
69 static void call_refreshresult(struct rpc_task
*task
);
70 static void call_connect(struct rpc_task
*task
);
71 static void call_connect_status(struct rpc_task
*task
);
73 static int rpc_encode_header(struct rpc_task
*task
,
74 struct xdr_stream
*xdr
);
75 static int rpc_decode_header(struct rpc_task
*task
,
76 struct xdr_stream
*xdr
);
77 static int rpc_ping(struct rpc_clnt
*clnt
);
78 static void rpc_check_timeout(struct rpc_task
*task
);
80 static void rpc_register_client(struct rpc_clnt
*clnt
)
82 struct net
*net
= rpc_net_ns(clnt
);
83 struct sunrpc_net
*sn
= net_generic(net
, sunrpc_net_id
);
85 spin_lock(&sn
->rpc_client_lock
);
86 list_add(&clnt
->cl_clients
, &sn
->all_clients
);
87 spin_unlock(&sn
->rpc_client_lock
);
90 static void rpc_unregister_client(struct rpc_clnt
*clnt
)
92 struct net
*net
= rpc_net_ns(clnt
);
93 struct sunrpc_net
*sn
= net_generic(net
, sunrpc_net_id
);
95 spin_lock(&sn
->rpc_client_lock
);
96 list_del(&clnt
->cl_clients
);
97 spin_unlock(&sn
->rpc_client_lock
);
100 static void __rpc_clnt_remove_pipedir(struct rpc_clnt
*clnt
)
102 rpc_remove_client_dir(clnt
);
105 static void rpc_clnt_remove_pipedir(struct rpc_clnt
*clnt
)
107 struct net
*net
= rpc_net_ns(clnt
);
108 struct super_block
*pipefs_sb
;
110 pipefs_sb
= rpc_get_sb_net(net
);
112 __rpc_clnt_remove_pipedir(clnt
);
117 static struct dentry
*rpc_setup_pipedir_sb(struct super_block
*sb
,
118 struct rpc_clnt
*clnt
)
120 static uint32_t clntid
;
121 const char *dir_name
= clnt
->cl_program
->pipe_dir_name
;
123 struct dentry
*dir
, *dentry
;
125 dir
= rpc_d_lookup_sb(sb
, dir_name
);
127 pr_info("RPC: pipefs directory doesn't exist: %s\n", dir_name
);
131 snprintf(name
, sizeof(name
), "clnt%x", (unsigned int)clntid
++);
132 name
[sizeof(name
) - 1] = '\0';
133 dentry
= rpc_create_client_dir(dir
, name
, clnt
);
136 if (dentry
== ERR_PTR(-EEXIST
))
138 printk(KERN_INFO
"RPC: Couldn't create pipefs entry"
139 " %s/%s, error %ld\n",
140 dir_name
, name
, PTR_ERR(dentry
));
148 rpc_setup_pipedir(struct super_block
*pipefs_sb
, struct rpc_clnt
*clnt
)
150 struct dentry
*dentry
;
152 if (clnt
->cl_program
->pipe_dir_name
!= NULL
) {
153 dentry
= rpc_setup_pipedir_sb(pipefs_sb
, clnt
);
155 return PTR_ERR(dentry
);
160 static int rpc_clnt_skip_event(struct rpc_clnt
*clnt
, unsigned long event
)
162 if (clnt
->cl_program
->pipe_dir_name
== NULL
)
166 case RPC_PIPEFS_MOUNT
:
167 if (clnt
->cl_pipedir_objects
.pdh_dentry
!= NULL
)
169 if (atomic_read(&clnt
->cl_count
) == 0)
172 case RPC_PIPEFS_UMOUNT
:
173 if (clnt
->cl_pipedir_objects
.pdh_dentry
== NULL
)
180 static int __rpc_clnt_handle_event(struct rpc_clnt
*clnt
, unsigned long event
,
181 struct super_block
*sb
)
183 struct dentry
*dentry
;
186 case RPC_PIPEFS_MOUNT
:
187 dentry
= rpc_setup_pipedir_sb(sb
, clnt
);
191 return PTR_ERR(dentry
);
193 case RPC_PIPEFS_UMOUNT
:
194 __rpc_clnt_remove_pipedir(clnt
);
197 printk(KERN_ERR
"%s: unknown event: %ld\n", __func__
, event
);
203 static int __rpc_pipefs_event(struct rpc_clnt
*clnt
, unsigned long event
,
204 struct super_block
*sb
)
208 for (;; clnt
= clnt
->cl_parent
) {
209 if (!rpc_clnt_skip_event(clnt
, event
))
210 error
= __rpc_clnt_handle_event(clnt
, event
, sb
);
211 if (error
|| clnt
== clnt
->cl_parent
)
217 static struct rpc_clnt
*rpc_get_client_for_event(struct net
*net
, int event
)
219 struct sunrpc_net
*sn
= net_generic(net
, sunrpc_net_id
);
220 struct rpc_clnt
*clnt
;
222 spin_lock(&sn
->rpc_client_lock
);
223 list_for_each_entry(clnt
, &sn
->all_clients
, cl_clients
) {
224 if (rpc_clnt_skip_event(clnt
, event
))
226 spin_unlock(&sn
->rpc_client_lock
);
229 spin_unlock(&sn
->rpc_client_lock
);
233 static int rpc_pipefs_event(struct notifier_block
*nb
, unsigned long event
,
236 struct super_block
*sb
= ptr
;
237 struct rpc_clnt
*clnt
;
240 while ((clnt
= rpc_get_client_for_event(sb
->s_fs_info
, event
))) {
241 error
= __rpc_pipefs_event(clnt
, event
, sb
);
248 static struct notifier_block rpc_clients_block
= {
249 .notifier_call
= rpc_pipefs_event
,
250 .priority
= SUNRPC_PIPEFS_RPC_PRIO
,
253 int rpc_clients_notifier_register(void)
255 return rpc_pipefs_notifier_register(&rpc_clients_block
);
258 void rpc_clients_notifier_unregister(void)
260 return rpc_pipefs_notifier_unregister(&rpc_clients_block
);
263 static struct rpc_xprt
*rpc_clnt_set_transport(struct rpc_clnt
*clnt
,
264 struct rpc_xprt
*xprt
,
265 const struct rpc_timeout
*timeout
)
267 struct rpc_xprt
*old
;
269 spin_lock(&clnt
->cl_lock
);
270 old
= rcu_dereference_protected(clnt
->cl_xprt
,
271 lockdep_is_held(&clnt
->cl_lock
));
273 if (!xprt_bound(xprt
))
274 clnt
->cl_autobind
= 1;
276 clnt
->cl_timeout
= timeout
;
277 rcu_assign_pointer(clnt
->cl_xprt
, xprt
);
278 spin_unlock(&clnt
->cl_lock
);
283 static void rpc_clnt_set_nodename(struct rpc_clnt
*clnt
, const char *nodename
)
285 clnt
->cl_nodelen
= strlcpy(clnt
->cl_nodename
,
286 nodename
, sizeof(clnt
->cl_nodename
));
289 static int rpc_client_register(struct rpc_clnt
*clnt
,
290 rpc_authflavor_t pseudoflavor
,
291 const char *client_name
)
293 struct rpc_auth_create_args auth_args
= {
294 .pseudoflavor
= pseudoflavor
,
295 .target_name
= client_name
,
297 struct rpc_auth
*auth
;
298 struct net
*net
= rpc_net_ns(clnt
);
299 struct super_block
*pipefs_sb
;
302 rpc_clnt_debugfs_register(clnt
);
304 pipefs_sb
= rpc_get_sb_net(net
);
306 err
= rpc_setup_pipedir(pipefs_sb
, clnt
);
311 rpc_register_client(clnt
);
315 auth
= rpcauth_create(&auth_args
, clnt
);
317 dprintk("RPC: Couldn't create auth handle (flavor %u)\n",
324 pipefs_sb
= rpc_get_sb_net(net
);
325 rpc_unregister_client(clnt
);
326 __rpc_clnt_remove_pipedir(clnt
);
330 rpc_clnt_debugfs_unregister(clnt
);
334 static DEFINE_IDA(rpc_clids
);
336 void rpc_cleanup_clids(void)
338 ida_destroy(&rpc_clids
);
341 static int rpc_alloc_clid(struct rpc_clnt
*clnt
)
345 clid
= ida_simple_get(&rpc_clids
, 0, 0, GFP_KERNEL
);
348 clnt
->cl_clid
= clid
;
352 static void rpc_free_clid(struct rpc_clnt
*clnt
)
354 ida_simple_remove(&rpc_clids
, clnt
->cl_clid
);
357 static struct rpc_clnt
* rpc_new_client(const struct rpc_create_args
*args
,
358 struct rpc_xprt_switch
*xps
,
359 struct rpc_xprt
*xprt
,
360 struct rpc_clnt
*parent
)
362 const struct rpc_program
*program
= args
->program
;
363 const struct rpc_version
*version
;
364 struct rpc_clnt
*clnt
= NULL
;
365 const struct rpc_timeout
*timeout
;
366 const char *nodename
= args
->nodename
;
374 if (args
->version
>= program
->nrvers
)
376 version
= program
->version
[args
->version
];
381 clnt
= kzalloc(sizeof(*clnt
), GFP_KERNEL
);
384 clnt
->cl_parent
= parent
? : clnt
;
386 err
= rpc_alloc_clid(clnt
);
390 clnt
->cl_cred
= get_cred(args
->cred
);
391 clnt
->cl_procinfo
= version
->procs
;
392 clnt
->cl_maxproc
= version
->nrprocs
;
393 clnt
->cl_prog
= args
->prognumber
? : program
->number
;
394 clnt
->cl_vers
= version
->number
;
395 clnt
->cl_stats
= program
->stats
;
396 clnt
->cl_metrics
= rpc_alloc_iostats(clnt
);
397 rpc_init_pipe_dir_head(&clnt
->cl_pipedir_objects
);
399 if (clnt
->cl_metrics
== NULL
)
401 clnt
->cl_program
= program
;
402 INIT_LIST_HEAD(&clnt
->cl_tasks
);
403 spin_lock_init(&clnt
->cl_lock
);
405 timeout
= xprt
->timeout
;
406 if (args
->timeout
!= NULL
) {
407 memcpy(&clnt
->cl_timeout_default
, args
->timeout
,
408 sizeof(clnt
->cl_timeout_default
));
409 timeout
= &clnt
->cl_timeout_default
;
412 rpc_clnt_set_transport(clnt
, xprt
, timeout
);
413 xprt_iter_init(&clnt
->cl_xpi
, xps
);
414 xprt_switch_put(xps
);
416 clnt
->cl_rtt
= &clnt
->cl_rtt_default
;
417 rpc_init_rtt(&clnt
->cl_rtt_default
, clnt
->cl_timeout
->to_initval
);
419 atomic_set(&clnt
->cl_count
, 1);
421 if (nodename
== NULL
)
422 nodename
= utsname()->nodename
;
423 /* save the nodename */
424 rpc_clnt_set_nodename(clnt
, nodename
);
426 err
= rpc_client_register(clnt
, args
->authflavor
, args
->client_name
);
430 atomic_inc(&parent
->cl_count
);
432 trace_rpc_clnt_new(clnt
, xprt
, program
->name
, args
->servername
);
436 rpc_free_iostats(clnt
->cl_metrics
);
438 put_cred(clnt
->cl_cred
);
445 xprt_switch_put(xps
);
447 trace_rpc_clnt_new_err(program
->name
, args
->servername
, err
);
451 static struct rpc_clnt
*rpc_create_xprt(struct rpc_create_args
*args
,
452 struct rpc_xprt
*xprt
)
454 struct rpc_clnt
*clnt
= NULL
;
455 struct rpc_xprt_switch
*xps
;
457 if (args
->bc_xprt
&& args
->bc_xprt
->xpt_bc_xps
) {
458 WARN_ON_ONCE(!(args
->protocol
& XPRT_TRANSPORT_BC
));
459 xps
= args
->bc_xprt
->xpt_bc_xps
;
460 xprt_switch_get(xps
);
462 xps
= xprt_switch_alloc(xprt
, GFP_KERNEL
);
465 return ERR_PTR(-ENOMEM
);
468 xprt_switch_get(xps
);
469 xprt
->bc_xprt
->xpt_bc_xps
= xps
;
472 clnt
= rpc_new_client(args
, xps
, xprt
, NULL
);
476 if (!(args
->flags
& RPC_CLNT_CREATE_NOPING
)) {
477 int err
= rpc_ping(clnt
);
479 rpc_shutdown_client(clnt
);
484 clnt
->cl_softrtry
= 1;
485 if (args
->flags
& (RPC_CLNT_CREATE_HARDRTRY
|RPC_CLNT_CREATE_SOFTERR
)) {
486 clnt
->cl_softrtry
= 0;
487 if (args
->flags
& RPC_CLNT_CREATE_SOFTERR
)
488 clnt
->cl_softerr
= 1;
491 if (args
->flags
& RPC_CLNT_CREATE_AUTOBIND
)
492 clnt
->cl_autobind
= 1;
493 if (args
->flags
& RPC_CLNT_CREATE_NO_RETRANS_TIMEOUT
)
494 clnt
->cl_noretranstimeo
= 1;
495 if (args
->flags
& RPC_CLNT_CREATE_DISCRTRY
)
496 clnt
->cl_discrtry
= 1;
497 if (!(args
->flags
& RPC_CLNT_CREATE_QUIET
))
504 * rpc_create - create an RPC client and transport with one call
505 * @args: rpc_clnt create argument structure
507 * Creates and initializes an RPC transport and an RPC client.
509 * It can ping the server in order to determine if it is up, and to see if
510 * it supports this program and version. RPC_CLNT_CREATE_NOPING disables
511 * this behavior so asynchronous tasks can also use rpc_create.
513 struct rpc_clnt
*rpc_create(struct rpc_create_args
*args
)
515 struct rpc_xprt
*xprt
;
516 struct xprt_create xprtargs
= {
518 .ident
= args
->protocol
,
519 .srcaddr
= args
->saddress
,
520 .dstaddr
= args
->address
,
521 .addrlen
= args
->addrsize
,
522 .servername
= args
->servername
,
523 .bc_xprt
= args
->bc_xprt
,
526 struct rpc_clnt
*clnt
;
530 WARN_ON_ONCE(!(args
->protocol
& XPRT_TRANSPORT_BC
));
531 xprt
= args
->bc_xprt
->xpt_bc_xprt
;
534 return rpc_create_xprt(args
, xprt
);
538 if (args
->flags
& RPC_CLNT_CREATE_INFINITE_SLOTS
)
539 xprtargs
.flags
|= XPRT_CREATE_INFINITE_SLOTS
;
540 if (args
->flags
& RPC_CLNT_CREATE_NO_IDLE_TIMEOUT
)
541 xprtargs
.flags
|= XPRT_CREATE_NO_IDLE_TIMEOUT
;
543 * If the caller chooses not to specify a hostname, whip
544 * up a string representation of the passed-in address.
546 if (xprtargs
.servername
== NULL
) {
547 struct sockaddr_un
*sun
=
548 (struct sockaddr_un
*)args
->address
;
549 struct sockaddr_in
*sin
=
550 (struct sockaddr_in
*)args
->address
;
551 struct sockaddr_in6
*sin6
=
552 (struct sockaddr_in6
*)args
->address
;
554 servername
[0] = '\0';
555 switch (args
->address
->sa_family
) {
557 snprintf(servername
, sizeof(servername
), "%s",
561 snprintf(servername
, sizeof(servername
), "%pI4",
562 &sin
->sin_addr
.s_addr
);
565 snprintf(servername
, sizeof(servername
), "%pI6",
569 /* caller wants default server name, but
570 * address family isn't recognized. */
571 return ERR_PTR(-EINVAL
);
573 xprtargs
.servername
= servername
;
576 xprt
= xprt_create_transport(&xprtargs
);
578 return (struct rpc_clnt
*)xprt
;
581 * By default, kernel RPC client connects from a reserved port.
582 * CAP_NET_BIND_SERVICE will not be set for unprivileged requesters,
583 * but it is always enabled for rpciod, which handles the connect
587 if (args
->flags
& RPC_CLNT_CREATE_NONPRIVPORT
)
590 if (args
->flags
& RPC_CLNT_CREATE_REUSEPORT
)
593 clnt
= rpc_create_xprt(args
, xprt
);
594 if (IS_ERR(clnt
) || args
->nconnect
<= 1)
597 for (i
= 0; i
< args
->nconnect
- 1; i
++) {
598 if (rpc_clnt_add_xprt(clnt
, &xprtargs
, NULL
, NULL
) < 0)
603 EXPORT_SYMBOL_GPL(rpc_create
);
606 * This function clones the RPC client structure. It allows us to share the
607 * same transport while varying parameters such as the authentication
610 static struct rpc_clnt
*__rpc_clone_client(struct rpc_create_args
*args
,
611 struct rpc_clnt
*clnt
)
613 struct rpc_xprt_switch
*xps
;
614 struct rpc_xprt
*xprt
;
615 struct rpc_clnt
*new;
620 xprt
= xprt_get(rcu_dereference(clnt
->cl_xprt
));
621 xps
= xprt_switch_get(rcu_dereference(clnt
->cl_xpi
.xpi_xpswitch
));
623 if (xprt
== NULL
|| xps
== NULL
) {
625 xprt_switch_put(xps
);
628 args
->servername
= xprt
->servername
;
629 args
->nodename
= clnt
->cl_nodename
;
631 new = rpc_new_client(args
, xps
, xprt
, clnt
);
635 /* Turn off autobind on clones */
636 new->cl_autobind
= 0;
637 new->cl_softrtry
= clnt
->cl_softrtry
;
638 new->cl_softerr
= clnt
->cl_softerr
;
639 new->cl_noretranstimeo
= clnt
->cl_noretranstimeo
;
640 new->cl_discrtry
= clnt
->cl_discrtry
;
641 new->cl_chatty
= clnt
->cl_chatty
;
642 new->cl_principal
= clnt
->cl_principal
;
646 trace_rpc_clnt_clone_err(clnt
, err
);
651 * rpc_clone_client - Clone an RPC client structure
653 * @clnt: RPC client whose parameters are copied
655 * Returns a fresh RPC client or an ERR_PTR.
657 struct rpc_clnt
*rpc_clone_client(struct rpc_clnt
*clnt
)
659 struct rpc_create_args args
= {
660 .program
= clnt
->cl_program
,
661 .prognumber
= clnt
->cl_prog
,
662 .version
= clnt
->cl_vers
,
663 .authflavor
= clnt
->cl_auth
->au_flavor
,
664 .cred
= clnt
->cl_cred
,
666 return __rpc_clone_client(&args
, clnt
);
668 EXPORT_SYMBOL_GPL(rpc_clone_client
);
671 * rpc_clone_client_set_auth - Clone an RPC client structure and set its auth
673 * @clnt: RPC client whose parameters are copied
674 * @flavor: security flavor for new client
676 * Returns a fresh RPC client or an ERR_PTR.
679 rpc_clone_client_set_auth(struct rpc_clnt
*clnt
, rpc_authflavor_t flavor
)
681 struct rpc_create_args args
= {
682 .program
= clnt
->cl_program
,
683 .prognumber
= clnt
->cl_prog
,
684 .version
= clnt
->cl_vers
,
685 .authflavor
= flavor
,
686 .cred
= clnt
->cl_cred
,
688 return __rpc_clone_client(&args
, clnt
);
690 EXPORT_SYMBOL_GPL(rpc_clone_client_set_auth
);
693 * rpc_switch_client_transport: switch the RPC transport on the fly
694 * @clnt: pointer to a struct rpc_clnt
695 * @args: pointer to the new transport arguments
696 * @timeout: pointer to the new timeout parameters
698 * This function allows the caller to switch the RPC transport for the
699 * rpc_clnt structure 'clnt' to allow it to connect to a mirrored NFS
700 * server, for instance. It assumes that the caller has ensured that
701 * there are no active RPC tasks by using some form of locking.
703 * Returns zero if "clnt" is now using the new xprt. Otherwise a
704 * negative errno is returned, and "clnt" continues to use the old
707 int rpc_switch_client_transport(struct rpc_clnt
*clnt
,
708 struct xprt_create
*args
,
709 const struct rpc_timeout
*timeout
)
711 const struct rpc_timeout
*old_timeo
;
712 rpc_authflavor_t pseudoflavor
;
713 struct rpc_xprt_switch
*xps
, *oldxps
;
714 struct rpc_xprt
*xprt
, *old
;
715 struct rpc_clnt
*parent
;
718 xprt
= xprt_create_transport(args
);
720 return PTR_ERR(xprt
);
722 xps
= xprt_switch_alloc(xprt
, GFP_KERNEL
);
728 pseudoflavor
= clnt
->cl_auth
->au_flavor
;
730 old_timeo
= clnt
->cl_timeout
;
731 old
= rpc_clnt_set_transport(clnt
, xprt
, timeout
);
732 oldxps
= xprt_iter_xchg_switch(&clnt
->cl_xpi
, xps
);
734 rpc_unregister_client(clnt
);
735 __rpc_clnt_remove_pipedir(clnt
);
736 rpc_clnt_debugfs_unregister(clnt
);
739 * A new transport was created. "clnt" therefore
740 * becomes the root of a new cl_parent tree. clnt's
741 * children, if it has any, still point to the old xprt.
743 parent
= clnt
->cl_parent
;
744 clnt
->cl_parent
= clnt
;
747 * The old rpc_auth cache cannot be re-used. GSS
748 * contexts in particular are between a single
751 err
= rpc_client_register(clnt
, pseudoflavor
, NULL
);
757 rpc_release_client(parent
);
758 xprt_switch_put(oldxps
);
760 trace_rpc_clnt_replace_xprt(clnt
);
764 xps
= xprt_iter_xchg_switch(&clnt
->cl_xpi
, oldxps
);
765 rpc_clnt_set_transport(clnt
, old
, old_timeo
);
766 clnt
->cl_parent
= parent
;
767 rpc_client_register(clnt
, pseudoflavor
, NULL
);
768 xprt_switch_put(xps
);
770 trace_rpc_clnt_replace_xprt_err(clnt
);
773 EXPORT_SYMBOL_GPL(rpc_switch_client_transport
);
776 int rpc_clnt_xprt_iter_init(struct rpc_clnt
*clnt
, struct rpc_xprt_iter
*xpi
)
778 struct rpc_xprt_switch
*xps
;
781 xps
= xprt_switch_get(rcu_dereference(clnt
->cl_xpi
.xpi_xpswitch
));
785 xprt_iter_init_listall(xpi
, xps
);
786 xprt_switch_put(xps
);
791 * rpc_clnt_iterate_for_each_xprt - Apply a function to all transports
792 * @clnt: pointer to client
793 * @fn: function to apply
794 * @data: void pointer to function data
796 * Iterates through the list of RPC transports currently attached to the
797 * client and applies the function fn(clnt, xprt, data).
799 * On error, the iteration stops, and the function returns the error value.
801 int rpc_clnt_iterate_for_each_xprt(struct rpc_clnt
*clnt
,
802 int (*fn
)(struct rpc_clnt
*, struct rpc_xprt
*, void *),
805 struct rpc_xprt_iter xpi
;
808 ret
= rpc_clnt_xprt_iter_init(clnt
, &xpi
);
812 struct rpc_xprt
*xprt
= xprt_iter_get_next(&xpi
);
816 ret
= fn(clnt
, xprt
, data
);
821 xprt_iter_destroy(&xpi
);
824 EXPORT_SYMBOL_GPL(rpc_clnt_iterate_for_each_xprt
);
827 * Kill all tasks for the given client.
828 * XXX: kill their descendants as well?
830 void rpc_killall_tasks(struct rpc_clnt
*clnt
)
832 struct rpc_task
*rovr
;
835 if (list_empty(&clnt
->cl_tasks
))
839 * Spin lock all_tasks to prevent changes...
841 trace_rpc_clnt_killall(clnt
);
842 spin_lock(&clnt
->cl_lock
);
843 list_for_each_entry(rovr
, &clnt
->cl_tasks
, tk_task
)
844 rpc_signal_task(rovr
);
845 spin_unlock(&clnt
->cl_lock
);
847 EXPORT_SYMBOL_GPL(rpc_killall_tasks
);
850 * Properly shut down an RPC client, terminating all outstanding
853 void rpc_shutdown_client(struct rpc_clnt
*clnt
)
857 trace_rpc_clnt_shutdown(clnt
);
859 while (!list_empty(&clnt
->cl_tasks
)) {
860 rpc_killall_tasks(clnt
);
861 wait_event_timeout(destroy_wait
,
862 list_empty(&clnt
->cl_tasks
), 1*HZ
);
865 rpc_release_client(clnt
);
867 EXPORT_SYMBOL_GPL(rpc_shutdown_client
);
872 static void rpc_free_client_work(struct work_struct
*work
)
874 struct rpc_clnt
*clnt
= container_of(work
, struct rpc_clnt
, cl_work
);
876 trace_rpc_clnt_free(clnt
);
878 /* These might block on processes that might allocate memory,
879 * so they cannot be called in rpciod, so they are handled separately
882 rpc_clnt_debugfs_unregister(clnt
);
884 rpc_clnt_remove_pipedir(clnt
);
885 xprt_put(rcu_dereference_raw(clnt
->cl_xprt
));
890 static struct rpc_clnt
*
891 rpc_free_client(struct rpc_clnt
*clnt
)
893 struct rpc_clnt
*parent
= NULL
;
895 trace_rpc_clnt_release(clnt
);
896 if (clnt
->cl_parent
!= clnt
)
897 parent
= clnt
->cl_parent
;
898 rpc_unregister_client(clnt
);
899 rpc_free_iostats(clnt
->cl_metrics
);
900 clnt
->cl_metrics
= NULL
;
901 xprt_iter_destroy(&clnt
->cl_xpi
);
902 put_cred(clnt
->cl_cred
);
904 INIT_WORK(&clnt
->cl_work
, rpc_free_client_work
);
905 schedule_work(&clnt
->cl_work
);
912 static struct rpc_clnt
*
913 rpc_free_auth(struct rpc_clnt
*clnt
)
915 if (clnt
->cl_auth
== NULL
)
916 return rpc_free_client(clnt
);
919 * Note: RPCSEC_GSS may need to send NULL RPC calls in order to
920 * release remaining GSS contexts. This mechanism ensures
921 * that it can do so safely.
923 atomic_inc(&clnt
->cl_count
);
924 rpcauth_release(clnt
->cl_auth
);
925 clnt
->cl_auth
= NULL
;
926 if (atomic_dec_and_test(&clnt
->cl_count
))
927 return rpc_free_client(clnt
);
932 * Release reference to the RPC client
935 rpc_release_client(struct rpc_clnt
*clnt
)
938 if (list_empty(&clnt
->cl_tasks
))
939 wake_up(&destroy_wait
);
940 if (!atomic_dec_and_test(&clnt
->cl_count
))
942 clnt
= rpc_free_auth(clnt
);
943 } while (clnt
!= NULL
);
945 EXPORT_SYMBOL_GPL(rpc_release_client
);
948 * rpc_bind_new_program - bind a new RPC program to an existing client
949 * @old: old rpc_client
950 * @program: rpc program to set
951 * @vers: rpc program version
953 * Clones the rpc client and sets up a new RPC program. This is mainly
954 * of use for enabling different RPC programs to share the same transport.
955 * The Sun NFSv2/v3 ACL protocol can do this.
957 struct rpc_clnt
*rpc_bind_new_program(struct rpc_clnt
*old
,
958 const struct rpc_program
*program
,
961 struct rpc_create_args args
= {
963 .prognumber
= program
->number
,
965 .authflavor
= old
->cl_auth
->au_flavor
,
966 .cred
= old
->cl_cred
,
968 struct rpc_clnt
*clnt
;
971 clnt
= __rpc_clone_client(&args
, old
);
974 err
= rpc_ping(clnt
);
976 rpc_shutdown_client(clnt
);
982 EXPORT_SYMBOL_GPL(rpc_bind_new_program
);
985 rpc_task_get_xprt(struct rpc_clnt
*clnt
, struct rpc_xprt
*xprt
)
987 struct rpc_xprt_switch
*xps
;
992 xps
= rcu_dereference(clnt
->cl_xpi
.xpi_xpswitch
);
993 atomic_long_inc(&xps
->xps_queuelen
);
995 atomic_long_inc(&xprt
->queuelen
);
1001 rpc_task_release_xprt(struct rpc_clnt
*clnt
, struct rpc_xprt
*xprt
)
1003 struct rpc_xprt_switch
*xps
;
1005 atomic_long_dec(&xprt
->queuelen
);
1007 xps
= rcu_dereference(clnt
->cl_xpi
.xpi_xpswitch
);
1008 atomic_long_dec(&xps
->xps_queuelen
);
1014 void rpc_task_release_transport(struct rpc_task
*task
)
1016 struct rpc_xprt
*xprt
= task
->tk_xprt
;
1019 task
->tk_xprt
= NULL
;
1020 if (task
->tk_client
)
1021 rpc_task_release_xprt(task
->tk_client
, xprt
);
1026 EXPORT_SYMBOL_GPL(rpc_task_release_transport
);
1028 void rpc_task_release_client(struct rpc_task
*task
)
1030 struct rpc_clnt
*clnt
= task
->tk_client
;
1032 rpc_task_release_transport(task
);
1034 /* Remove from client task list */
1035 spin_lock(&clnt
->cl_lock
);
1036 list_del(&task
->tk_task
);
1037 spin_unlock(&clnt
->cl_lock
);
1038 task
->tk_client
= NULL
;
1040 rpc_release_client(clnt
);
1044 static struct rpc_xprt
*
1045 rpc_task_get_first_xprt(struct rpc_clnt
*clnt
)
1047 struct rpc_xprt
*xprt
;
1050 xprt
= xprt_get(rcu_dereference(clnt
->cl_xprt
));
1052 return rpc_task_get_xprt(clnt
, xprt
);
1055 static struct rpc_xprt
*
1056 rpc_task_get_next_xprt(struct rpc_clnt
*clnt
)
1058 return rpc_task_get_xprt(clnt
, xprt_iter_get_next(&clnt
->cl_xpi
));
1062 void rpc_task_set_transport(struct rpc_task
*task
, struct rpc_clnt
*clnt
)
1066 if (task
->tk_flags
& RPC_TASK_NO_ROUND_ROBIN
)
1067 task
->tk_xprt
= rpc_task_get_first_xprt(clnt
);
1069 task
->tk_xprt
= rpc_task_get_next_xprt(clnt
);
1073 void rpc_task_set_client(struct rpc_task
*task
, struct rpc_clnt
*clnt
)
1077 rpc_task_set_transport(task
, clnt
);
1078 task
->tk_client
= clnt
;
1079 atomic_inc(&clnt
->cl_count
);
1080 if (clnt
->cl_softrtry
)
1081 task
->tk_flags
|= RPC_TASK_SOFT
;
1082 if (clnt
->cl_softerr
)
1083 task
->tk_flags
|= RPC_TASK_TIMEOUT
;
1084 if (clnt
->cl_noretranstimeo
)
1085 task
->tk_flags
|= RPC_TASK_NO_RETRANS_TIMEOUT
;
1086 if (atomic_read(&clnt
->cl_swapper
))
1087 task
->tk_flags
|= RPC_TASK_SWAPPER
;
1088 /* Add to the client's list of all tasks */
1089 spin_lock(&clnt
->cl_lock
);
1090 list_add_tail(&task
->tk_task
, &clnt
->cl_tasks
);
1091 spin_unlock(&clnt
->cl_lock
);
1096 rpc_task_set_rpc_message(struct rpc_task
*task
, const struct rpc_message
*msg
)
1099 task
->tk_msg
.rpc_proc
= msg
->rpc_proc
;
1100 task
->tk_msg
.rpc_argp
= msg
->rpc_argp
;
1101 task
->tk_msg
.rpc_resp
= msg
->rpc_resp
;
1102 task
->tk_msg
.rpc_cred
= msg
->rpc_cred
;
1103 if (!(task
->tk_flags
& RPC_TASK_CRED_NOREF
))
1104 get_cred(task
->tk_msg
.rpc_cred
);
1109 * Default callback for async RPC calls
1112 rpc_default_callback(struct rpc_task
*task
, void *data
)
1116 static const struct rpc_call_ops rpc_default_ops
= {
1117 .rpc_call_done
= rpc_default_callback
,
1121 * rpc_run_task - Allocate a new RPC task, then run rpc_execute against it
1122 * @task_setup_data: pointer to task initialisation data
1124 struct rpc_task
*rpc_run_task(const struct rpc_task_setup
*task_setup_data
)
1126 struct rpc_task
*task
;
1128 task
= rpc_new_task(task_setup_data
);
1130 if (!RPC_IS_ASYNC(task
))
1131 task
->tk_flags
|= RPC_TASK_CRED_NOREF
;
1133 rpc_task_set_client(task
, task_setup_data
->rpc_client
);
1134 rpc_task_set_rpc_message(task
, task_setup_data
->rpc_message
);
1136 if (task
->tk_action
== NULL
)
1137 rpc_call_start(task
);
1139 atomic_inc(&task
->tk_count
);
1143 EXPORT_SYMBOL_GPL(rpc_run_task
);
1146 * rpc_call_sync - Perform a synchronous RPC call
1147 * @clnt: pointer to RPC client
1148 * @msg: RPC call parameters
1149 * @flags: RPC call flags
1151 int rpc_call_sync(struct rpc_clnt
*clnt
, const struct rpc_message
*msg
, int flags
)
1153 struct rpc_task
*task
;
1154 struct rpc_task_setup task_setup_data
= {
1157 .callback_ops
= &rpc_default_ops
,
1162 WARN_ON_ONCE(flags
& RPC_TASK_ASYNC
);
1163 if (flags
& RPC_TASK_ASYNC
) {
1164 rpc_release_calldata(task_setup_data
.callback_ops
,
1165 task_setup_data
.callback_data
);
1169 task
= rpc_run_task(&task_setup_data
);
1171 return PTR_ERR(task
);
1172 status
= task
->tk_status
;
1176 EXPORT_SYMBOL_GPL(rpc_call_sync
);
1179 * rpc_call_async - Perform an asynchronous RPC call
1180 * @clnt: pointer to RPC client
1181 * @msg: RPC call parameters
1182 * @flags: RPC call flags
1183 * @tk_ops: RPC call ops
1184 * @data: user call data
1187 rpc_call_async(struct rpc_clnt
*clnt
, const struct rpc_message
*msg
, int flags
,
1188 const struct rpc_call_ops
*tk_ops
, void *data
)
1190 struct rpc_task
*task
;
1191 struct rpc_task_setup task_setup_data
= {
1194 .callback_ops
= tk_ops
,
1195 .callback_data
= data
,
1196 .flags
= flags
|RPC_TASK_ASYNC
,
1199 task
= rpc_run_task(&task_setup_data
);
1201 return PTR_ERR(task
);
1205 EXPORT_SYMBOL_GPL(rpc_call_async
);
1207 #if defined(CONFIG_SUNRPC_BACKCHANNEL)
1208 static void call_bc_encode(struct rpc_task
*task
);
1211 * rpc_run_bc_task - Allocate a new RPC task for backchannel use, then run
1212 * rpc_execute against it
1215 struct rpc_task
*rpc_run_bc_task(struct rpc_rqst
*req
)
1217 struct rpc_task
*task
;
1218 struct rpc_task_setup task_setup_data
= {
1219 .callback_ops
= &rpc_default_ops
,
1220 .flags
= RPC_TASK_SOFTCONN
|
1221 RPC_TASK_NO_RETRANS_TIMEOUT
,
1224 dprintk("RPC: rpc_run_bc_task req= %p\n", req
);
1226 * Create an rpc_task to send the data
1228 task
= rpc_new_task(&task_setup_data
);
1229 xprt_init_bc_request(req
, task
);
1231 task
->tk_action
= call_bc_encode
;
1232 atomic_inc(&task
->tk_count
);
1233 WARN_ON_ONCE(atomic_read(&task
->tk_count
) != 2);
1236 dprintk("RPC: rpc_run_bc_task: task= %p\n", task
);
1239 #endif /* CONFIG_SUNRPC_BACKCHANNEL */
1242 * rpc_prepare_reply_pages - Prepare to receive a reply data payload into pages
1243 * @req: RPC request to prepare
1244 * @pages: vector of struct page pointers
1245 * @base: offset in first page where receive should start, in bytes
1246 * @len: expected size of the upper layer data payload, in bytes
1247 * @hdrsize: expected size of upper layer reply header, in XDR words
1250 void rpc_prepare_reply_pages(struct rpc_rqst
*req
, struct page
**pages
,
1251 unsigned int base
, unsigned int len
,
1252 unsigned int hdrsize
)
1254 hdrsize
+= RPC_REPHDRSIZE
+ req
->rq_cred
->cr_auth
->au_ralign
;
1256 xdr_inline_pages(&req
->rq_rcv_buf
, hdrsize
<< 2, pages
, base
, len
);
1257 trace_rpc_xdr_reply_pages(req
->rq_task
, &req
->rq_rcv_buf
);
1259 EXPORT_SYMBOL_GPL(rpc_prepare_reply_pages
);
1262 rpc_call_start(struct rpc_task
*task
)
1264 task
->tk_action
= call_start
;
1266 EXPORT_SYMBOL_GPL(rpc_call_start
);
1269 * rpc_peeraddr - extract remote peer address from clnt's xprt
1270 * @clnt: RPC client structure
1271 * @buf: target buffer
1272 * @bufsize: length of target buffer
1274 * Returns the number of bytes that are actually in the stored address.
1276 size_t rpc_peeraddr(struct rpc_clnt
*clnt
, struct sockaddr
*buf
, size_t bufsize
)
1279 struct rpc_xprt
*xprt
;
1282 xprt
= rcu_dereference(clnt
->cl_xprt
);
1284 bytes
= xprt
->addrlen
;
1285 if (bytes
> bufsize
)
1287 memcpy(buf
, &xprt
->addr
, bytes
);
1292 EXPORT_SYMBOL_GPL(rpc_peeraddr
);
1295 * rpc_peeraddr2str - return remote peer address in printable format
1296 * @clnt: RPC client structure
1297 * @format: address format
1299 * NB: the lifetime of the memory referenced by the returned pointer is
1300 * the same as the rpc_xprt itself. As long as the caller uses this
1301 * pointer, it must hold the RCU read lock.
1303 const char *rpc_peeraddr2str(struct rpc_clnt
*clnt
,
1304 enum rpc_display_format_t format
)
1306 struct rpc_xprt
*xprt
;
1308 xprt
= rcu_dereference(clnt
->cl_xprt
);
1310 if (xprt
->address_strings
[format
] != NULL
)
1311 return xprt
->address_strings
[format
];
1313 return "unprintable";
1315 EXPORT_SYMBOL_GPL(rpc_peeraddr2str
);
1317 static const struct sockaddr_in rpc_inaddr_loopback
= {
1318 .sin_family
= AF_INET
,
1319 .sin_addr
.s_addr
= htonl(INADDR_ANY
),
1322 static const struct sockaddr_in6 rpc_in6addr_loopback
= {
1323 .sin6_family
= AF_INET6
,
1324 .sin6_addr
= IN6ADDR_ANY_INIT
,
1328 * Try a getsockname() on a connected datagram socket. Using a
1329 * connected datagram socket prevents leaving a socket in TIME_WAIT.
1330 * This conserves the ephemeral port number space.
1332 * Returns zero and fills in "buf" if successful; otherwise, a
1333 * negative errno is returned.
1335 static int rpc_sockname(struct net
*net
, struct sockaddr
*sap
, size_t salen
,
1336 struct sockaddr
*buf
)
1338 struct socket
*sock
;
1341 err
= __sock_create(net
, sap
->sa_family
,
1342 SOCK_DGRAM
, IPPROTO_UDP
, &sock
, 1);
1344 dprintk("RPC: can't create UDP socket (%d)\n", err
);
1348 switch (sap
->sa_family
) {
1350 err
= kernel_bind(sock
,
1351 (struct sockaddr
*)&rpc_inaddr_loopback
,
1352 sizeof(rpc_inaddr_loopback
));
1355 err
= kernel_bind(sock
,
1356 (struct sockaddr
*)&rpc_in6addr_loopback
,
1357 sizeof(rpc_in6addr_loopback
));
1360 err
= -EAFNOSUPPORT
;
1364 dprintk("RPC: can't bind UDP socket (%d)\n", err
);
1368 err
= kernel_connect(sock
, sap
, salen
, 0);
1370 dprintk("RPC: can't connect UDP socket (%d)\n", err
);
1374 err
= kernel_getsockname(sock
, buf
);
1376 dprintk("RPC: getsockname failed (%d)\n", err
);
1381 if (buf
->sa_family
== AF_INET6
) {
1382 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)buf
;
1383 sin6
->sin6_scope_id
= 0;
1385 dprintk("RPC: %s succeeded\n", __func__
);
1394 * Scraping a connected socket failed, so we don't have a useable
1395 * local address. Fallback: generate an address that will prevent
1396 * the server from calling us back.
1398 * Returns zero and fills in "buf" if successful; otherwise, a
1399 * negative errno is returned.
1401 static int rpc_anyaddr(int family
, struct sockaddr
*buf
, size_t buflen
)
1405 if (buflen
< sizeof(rpc_inaddr_loopback
))
1407 memcpy(buf
, &rpc_inaddr_loopback
,
1408 sizeof(rpc_inaddr_loopback
));
1411 if (buflen
< sizeof(rpc_in6addr_loopback
))
1413 memcpy(buf
, &rpc_in6addr_loopback
,
1414 sizeof(rpc_in6addr_loopback
));
1417 dprintk("RPC: %s: address family not supported\n",
1419 return -EAFNOSUPPORT
;
1421 dprintk("RPC: %s: succeeded\n", __func__
);
1426 * rpc_localaddr - discover local endpoint address for an RPC client
1427 * @clnt: RPC client structure
1428 * @buf: target buffer
1429 * @buflen: size of target buffer, in bytes
1431 * Returns zero and fills in "buf" and "buflen" if successful;
1432 * otherwise, a negative errno is returned.
1434 * This works even if the underlying transport is not currently connected,
1435 * or if the upper layer never previously provided a source address.
1437 * The result of this function call is transient: multiple calls in
1438 * succession may give different results, depending on how local
1439 * networking configuration changes over time.
1441 int rpc_localaddr(struct rpc_clnt
*clnt
, struct sockaddr
*buf
, size_t buflen
)
1443 struct sockaddr_storage address
;
1444 struct sockaddr
*sap
= (struct sockaddr
*)&address
;
1445 struct rpc_xprt
*xprt
;
1451 xprt
= rcu_dereference(clnt
->cl_xprt
);
1452 salen
= xprt
->addrlen
;
1453 memcpy(sap
, &xprt
->addr
, salen
);
1454 net
= get_net(xprt
->xprt_net
);
1457 rpc_set_port(sap
, 0);
1458 err
= rpc_sockname(net
, sap
, salen
, buf
);
1461 /* Couldn't discover local address, return ANYADDR */
1462 return rpc_anyaddr(sap
->sa_family
, buf
, buflen
);
1465 EXPORT_SYMBOL_GPL(rpc_localaddr
);
1468 rpc_setbufsize(struct rpc_clnt
*clnt
, unsigned int sndsize
, unsigned int rcvsize
)
1470 struct rpc_xprt
*xprt
;
1473 xprt
= rcu_dereference(clnt
->cl_xprt
);
1474 if (xprt
->ops
->set_buffer_size
)
1475 xprt
->ops
->set_buffer_size(xprt
, sndsize
, rcvsize
);
1478 EXPORT_SYMBOL_GPL(rpc_setbufsize
);
1481 * rpc_net_ns - Get the network namespace for this RPC client
1482 * @clnt: RPC client to query
1485 struct net
*rpc_net_ns(struct rpc_clnt
*clnt
)
1490 ret
= rcu_dereference(clnt
->cl_xprt
)->xprt_net
;
1494 EXPORT_SYMBOL_GPL(rpc_net_ns
);
1497 * rpc_max_payload - Get maximum payload size for a transport, in bytes
1498 * @clnt: RPC client to query
1500 * For stream transports, this is one RPC record fragment (see RFC
1501 * 1831), as we don't support multi-record requests yet. For datagram
1502 * transports, this is the size of an IP packet minus the IP, UDP, and
1505 size_t rpc_max_payload(struct rpc_clnt
*clnt
)
1510 ret
= rcu_dereference(clnt
->cl_xprt
)->max_payload
;
1514 EXPORT_SYMBOL_GPL(rpc_max_payload
);
1517 * rpc_max_bc_payload - Get maximum backchannel payload size, in bytes
1518 * @clnt: RPC client to query
1520 size_t rpc_max_bc_payload(struct rpc_clnt
*clnt
)
1522 struct rpc_xprt
*xprt
;
1526 xprt
= rcu_dereference(clnt
->cl_xprt
);
1527 ret
= xprt
->ops
->bc_maxpayload(xprt
);
1531 EXPORT_SYMBOL_GPL(rpc_max_bc_payload
);
1533 unsigned int rpc_num_bc_slots(struct rpc_clnt
*clnt
)
1535 struct rpc_xprt
*xprt
;
1539 xprt
= rcu_dereference(clnt
->cl_xprt
);
1540 ret
= xprt
->ops
->bc_num_slots(xprt
);
1544 EXPORT_SYMBOL_GPL(rpc_num_bc_slots
);
1547 * rpc_force_rebind - force transport to check that remote port is unchanged
1548 * @clnt: client to rebind
1551 void rpc_force_rebind(struct rpc_clnt
*clnt
)
1553 if (clnt
->cl_autobind
) {
1555 xprt_clear_bound(rcu_dereference(clnt
->cl_xprt
));
1559 EXPORT_SYMBOL_GPL(rpc_force_rebind
);
1562 __rpc_restart_call(struct rpc_task
*task
, void (*action
)(struct rpc_task
*))
1564 task
->tk_status
= 0;
1565 task
->tk_rpc_status
= 0;
1566 task
->tk_action
= action
;
1571 * Restart an (async) RPC call. Usually called from within the
1575 rpc_restart_call(struct rpc_task
*task
)
1577 return __rpc_restart_call(task
, call_start
);
1579 EXPORT_SYMBOL_GPL(rpc_restart_call
);
1582 * Restart an (async) RPC call from the call_prepare state.
1583 * Usually called from within the exit handler.
1586 rpc_restart_call_prepare(struct rpc_task
*task
)
1588 if (task
->tk_ops
->rpc_call_prepare
!= NULL
)
1589 return __rpc_restart_call(task
, rpc_prepare_task
);
1590 return rpc_restart_call(task
);
1592 EXPORT_SYMBOL_GPL(rpc_restart_call_prepare
);
1595 *rpc_proc_name(const struct rpc_task
*task
)
1597 const struct rpc_procinfo
*proc
= task
->tk_msg
.rpc_proc
;
1601 return proc
->p_name
;
1609 __rpc_call_rpcerror(struct rpc_task
*task
, int tk_status
, int rpc_status
)
1611 trace_rpc_call_rpcerror(task
, tk_status
, rpc_status
);
1612 task
->tk_rpc_status
= rpc_status
;
1613 rpc_exit(task
, tk_status
);
1617 rpc_call_rpcerror(struct rpc_task
*task
, int status
)
1619 __rpc_call_rpcerror(task
, status
, status
);
1625 * Other FSM states can be visited zero or more times, but
1626 * this state is visited exactly once for each RPC.
1629 call_start(struct rpc_task
*task
)
1631 struct rpc_clnt
*clnt
= task
->tk_client
;
1632 int idx
= task
->tk_msg
.rpc_proc
->p_statidx
;
1634 trace_rpc_request(task
);
1636 /* Increment call count (version might not be valid for ping) */
1637 if (clnt
->cl_program
->version
[clnt
->cl_vers
])
1638 clnt
->cl_program
->version
[clnt
->cl_vers
]->counts
[idx
]++;
1639 clnt
->cl_stats
->rpccnt
++;
1640 task
->tk_action
= call_reserve
;
1641 rpc_task_set_transport(task
, clnt
);
1645 * 1. Reserve an RPC call slot
1648 call_reserve(struct rpc_task
*task
)
1650 task
->tk_status
= 0;
1651 task
->tk_action
= call_reserveresult
;
1655 static void call_retry_reserve(struct rpc_task
*task
);
1658 * 1b. Grok the result of xprt_reserve()
1661 call_reserveresult(struct rpc_task
*task
)
1663 int status
= task
->tk_status
;
1666 * After a call to xprt_reserve(), we must have either
1667 * a request slot or else an error status.
1669 task
->tk_status
= 0;
1671 if (task
->tk_rqstp
) {
1672 task
->tk_action
= call_refresh
;
1676 rpc_call_rpcerror(task
, -EIO
);
1681 * Even though there was an error, we may have acquired
1682 * a request slot somehow. Make sure not to leak it.
1689 rpc_delay(task
, HZ
>> 2);
1691 case -EAGAIN
: /* woken up; retry */
1692 task
->tk_action
= call_retry_reserve
;
1695 rpc_call_rpcerror(task
, status
);
1700 * 1c. Retry reserving an RPC call slot
1703 call_retry_reserve(struct rpc_task
*task
)
1705 task
->tk_status
= 0;
1706 task
->tk_action
= call_reserveresult
;
1707 xprt_retry_reserve(task
);
1711 * 2. Bind and/or refresh the credentials
1714 call_refresh(struct rpc_task
*task
)
1716 task
->tk_action
= call_refreshresult
;
1717 task
->tk_status
= 0;
1718 task
->tk_client
->cl_stats
->rpcauthrefresh
++;
1719 rpcauth_refreshcred(task
);
1723 * 2a. Process the results of a credential refresh
1726 call_refreshresult(struct rpc_task
*task
)
1728 int status
= task
->tk_status
;
1730 task
->tk_status
= 0;
1731 task
->tk_action
= call_refresh
;
1734 if (rpcauth_uptodatecred(task
)) {
1735 task
->tk_action
= call_allocate
;
1738 /* Use rate-limiting and a max number of retries if refresh
1739 * had status 0 but failed to update the cred.
1743 rpc_delay(task
, 3*HZ
);
1749 if (!task
->tk_cred_retry
)
1751 task
->tk_cred_retry
--;
1752 trace_rpc_retry_refresh_status(task
);
1755 trace_rpc_refresh_status(task
);
1756 rpc_call_rpcerror(task
, status
);
1760 * 2b. Allocate the buffer. For details, see sched.c:rpc_malloc.
1761 * (Note: buffer memory is freed in xprt_release).
1764 call_allocate(struct rpc_task
*task
)
1766 const struct rpc_auth
*auth
= task
->tk_rqstp
->rq_cred
->cr_auth
;
1767 struct rpc_rqst
*req
= task
->tk_rqstp
;
1768 struct rpc_xprt
*xprt
= req
->rq_xprt
;
1769 const struct rpc_procinfo
*proc
= task
->tk_msg
.rpc_proc
;
1772 task
->tk_status
= 0;
1773 task
->tk_action
= call_encode
;
1778 if (proc
->p_proc
!= 0) {
1779 BUG_ON(proc
->p_arglen
== 0);
1780 if (proc
->p_decode
!= NULL
)
1781 BUG_ON(proc
->p_replen
== 0);
1785 * Calculate the size (in quads) of the RPC call
1786 * and reply headers, and convert both values
1789 req
->rq_callsize
= RPC_CALLHDRSIZE
+ (auth
->au_cslack
<< 1) +
1791 req
->rq_callsize
<<= 2;
1793 * Note: the reply buffer must at minimum allocate enough space
1794 * for the 'struct accepted_reply' from RFC5531.
1796 req
->rq_rcvsize
= RPC_REPHDRSIZE
+ auth
->au_rslack
+ \
1797 max_t(size_t, proc
->p_replen
, 2);
1798 req
->rq_rcvsize
<<= 2;
1800 status
= xprt
->ops
->buf_alloc(task
);
1801 trace_rpc_buf_alloc(task
, status
);
1802 xprt_inject_disconnect(xprt
);
1805 if (status
!= -ENOMEM
) {
1806 rpc_call_rpcerror(task
, status
);
1810 if (RPC_IS_ASYNC(task
) || !fatal_signal_pending(current
)) {
1811 task
->tk_action
= call_allocate
;
1812 rpc_delay(task
, HZ
>>4);
1816 rpc_call_rpcerror(task
, -ERESTARTSYS
);
1820 rpc_task_need_encode(struct rpc_task
*task
)
1822 return test_bit(RPC_TASK_NEED_XMIT
, &task
->tk_runstate
) == 0 &&
1823 (!(task
->tk_flags
& RPC_TASK_SENT
) ||
1824 !(task
->tk_flags
& RPC_TASK_NO_RETRANS_TIMEOUT
) ||
1825 xprt_request_need_retransmit(task
));
1829 rpc_xdr_encode(struct rpc_task
*task
)
1831 struct rpc_rqst
*req
= task
->tk_rqstp
;
1832 struct xdr_stream xdr
;
1834 xdr_buf_init(&req
->rq_snd_buf
,
1837 xdr_buf_init(&req
->rq_rcv_buf
,
1841 req
->rq_reply_bytes_recvd
= 0;
1842 req
->rq_snd_buf
.head
[0].iov_len
= 0;
1843 xdr_init_encode(&xdr
, &req
->rq_snd_buf
,
1844 req
->rq_snd_buf
.head
[0].iov_base
, req
);
1845 xdr_free_bvec(&req
->rq_snd_buf
);
1846 if (rpc_encode_header(task
, &xdr
))
1849 task
->tk_status
= rpcauth_wrap_req(task
, &xdr
);
1853 * 3. Encode arguments of an RPC call
1856 call_encode(struct rpc_task
*task
)
1858 if (!rpc_task_need_encode(task
))
1861 /* Dequeue task from the receive queue while we're encoding */
1862 xprt_request_dequeue_xprt(task
);
1863 /* Encode here so that rpcsec_gss can use correct sequence number. */
1864 rpc_xdr_encode(task
);
1865 /* Did the encode result in an error condition? */
1866 if (task
->tk_status
!= 0) {
1867 /* Was the error nonfatal? */
1868 switch (task
->tk_status
) {
1871 rpc_delay(task
, HZ
>> 4);
1874 if (!task
->tk_cred_retry
) {
1875 rpc_exit(task
, task
->tk_status
);
1877 task
->tk_action
= call_refresh
;
1878 task
->tk_cred_retry
--;
1879 trace_rpc_retry_refresh_status(task
);
1883 rpc_call_rpcerror(task
, task
->tk_status
);
1888 /* Add task to reply queue before transmission to avoid races */
1889 if (rpc_reply_expected(task
))
1890 xprt_request_enqueue_receive(task
);
1891 xprt_request_enqueue_transmit(task
);
1893 task
->tk_action
= call_transmit
;
1894 /* Check that the connection is OK */
1895 if (!xprt_bound(task
->tk_xprt
))
1896 task
->tk_action
= call_bind
;
1897 else if (!xprt_connected(task
->tk_xprt
))
1898 task
->tk_action
= call_connect
;
1902 * Helpers to check if the task was already transmitted, and
1903 * to take action when that is the case.
1906 rpc_task_transmitted(struct rpc_task
*task
)
1908 return !test_bit(RPC_TASK_NEED_XMIT
, &task
->tk_runstate
);
1912 rpc_task_handle_transmitted(struct rpc_task
*task
)
1914 xprt_end_transmit(task
);
1915 task
->tk_action
= call_transmit_status
;
1919 * 4. Get the server port number if not yet set
1922 call_bind(struct rpc_task
*task
)
1924 struct rpc_xprt
*xprt
= task
->tk_rqstp
->rq_xprt
;
1926 if (rpc_task_transmitted(task
)) {
1927 rpc_task_handle_transmitted(task
);
1931 if (xprt_bound(xprt
)) {
1932 task
->tk_action
= call_connect
;
1936 task
->tk_action
= call_bind_status
;
1937 if (!xprt_prepare_transmit(task
))
1940 xprt
->ops
->rpcbind(task
);
1944 * 4a. Sort out bind result
1947 call_bind_status(struct rpc_task
*task
)
1949 struct rpc_xprt
*xprt
= task
->tk_rqstp
->rq_xprt
;
1952 if (rpc_task_transmitted(task
)) {
1953 rpc_task_handle_transmitted(task
);
1957 if (task
->tk_status
>= 0)
1959 if (xprt_bound(xprt
)) {
1960 task
->tk_status
= 0;
1964 switch (task
->tk_status
) {
1966 rpc_delay(task
, HZ
>> 2);
1969 trace_rpcb_prog_unavail_err(task
);
1970 /* fail immediately if this is an RPC ping */
1971 if (task
->tk_msg
.rpc_proc
->p_proc
== 0) {
1972 status
= -EOPNOTSUPP
;
1975 if (task
->tk_rebind_retry
== 0)
1977 task
->tk_rebind_retry
--;
1978 rpc_delay(task
, 3*HZ
);
1981 rpc_delay(task
, HZ
>> 2);
1986 trace_rpcb_timeout_err(task
);
1989 /* server doesn't support any rpcbind version we know of */
1990 trace_rpcb_bind_version_err(task
);
1992 case -EPROTONOSUPPORT
:
1993 trace_rpcb_bind_version_err(task
);
1995 case -ECONNREFUSED
: /* connection problems */
2004 trace_rpcb_unreachable_err(task
);
2005 if (!RPC_IS_SOFTCONN(task
)) {
2006 rpc_delay(task
, 5*HZ
);
2009 status
= task
->tk_status
;
2012 trace_rpcb_unrecognized_err(task
);
2015 rpc_call_rpcerror(task
, status
);
2018 task
->tk_action
= call_connect
;
2021 task
->tk_status
= 0;
2022 task
->tk_action
= call_bind
;
2023 rpc_check_timeout(task
);
2027 * 4b. Connect to the RPC server
2030 call_connect(struct rpc_task
*task
)
2032 struct rpc_xprt
*xprt
= task
->tk_rqstp
->rq_xprt
;
2034 if (rpc_task_transmitted(task
)) {
2035 rpc_task_handle_transmitted(task
);
2039 if (xprt_connected(xprt
)) {
2040 task
->tk_action
= call_transmit
;
2044 task
->tk_action
= call_connect_status
;
2045 if (task
->tk_status
< 0)
2047 if (task
->tk_flags
& RPC_TASK_NOCONNECT
) {
2048 rpc_call_rpcerror(task
, -ENOTCONN
);
2051 if (!xprt_prepare_transmit(task
))
2057 * 4c. Sort out connect result
2060 call_connect_status(struct rpc_task
*task
)
2062 struct rpc_xprt
*xprt
= task
->tk_rqstp
->rq_xprt
;
2063 struct rpc_clnt
*clnt
= task
->tk_client
;
2064 int status
= task
->tk_status
;
2066 if (rpc_task_transmitted(task
)) {
2067 rpc_task_handle_transmitted(task
);
2071 trace_rpc_connect_status(task
);
2073 if (task
->tk_status
== 0) {
2074 clnt
->cl_stats
->netreconn
++;
2077 if (xprt_connected(xprt
)) {
2078 task
->tk_status
= 0;
2082 task
->tk_status
= 0;
2085 /* A positive refusal suggests a rebind is needed. */
2086 if (RPC_IS_SOFTCONN(task
))
2088 if (clnt
->cl_autobind
) {
2089 rpc_force_rebind(clnt
);
2100 xprt_conditional_disconnect(task
->tk_rqstp
->rq_xprt
,
2101 task
->tk_rqstp
->rq_connect_cookie
);
2102 if (RPC_IS_SOFTCONN(task
))
2104 /* retry with existing socket, after a delay */
2105 rpc_delay(task
, 3*HZ
);
2113 rpc_delay(task
, HZ
>> 2);
2116 rpc_call_rpcerror(task
, status
);
2119 task
->tk_action
= call_transmit
;
2122 /* Check for timeouts before looping back to call_bind */
2123 task
->tk_action
= call_bind
;
2124 rpc_check_timeout(task
);
2128 * 5. Transmit the RPC request, and wait for reply
2131 call_transmit(struct rpc_task
*task
)
2133 if (rpc_task_transmitted(task
)) {
2134 rpc_task_handle_transmitted(task
);
2138 task
->tk_action
= call_transmit_status
;
2139 if (!xprt_prepare_transmit(task
))
2141 task
->tk_status
= 0;
2142 if (test_bit(RPC_TASK_NEED_XMIT
, &task
->tk_runstate
)) {
2143 if (!xprt_connected(task
->tk_xprt
)) {
2144 task
->tk_status
= -ENOTCONN
;
2147 xprt_transmit(task
);
2149 xprt_end_transmit(task
);
2153 * 5a. Handle cleanup after a transmission
2156 call_transmit_status(struct rpc_task
*task
)
2158 task
->tk_action
= call_status
;
2161 * Common case: success. Force the compiler to put this
2164 if (rpc_task_transmitted(task
)) {
2165 task
->tk_status
= 0;
2166 xprt_request_wait_receive(task
);
2170 switch (task
->tk_status
) {
2174 task
->tk_status
= 0;
2175 task
->tk_action
= call_encode
;
2178 * Special cases: if we've been waiting on the
2179 * socket's write_space() callback, or if the
2180 * socket just returned a connection error,
2181 * then hold onto the transport lock.
2184 rpc_delay(task
, HZ
>>2);
2188 task
->tk_action
= call_transmit
;
2189 task
->tk_status
= 0;
2197 if (RPC_IS_SOFTCONN(task
)) {
2198 if (!task
->tk_msg
.rpc_proc
->p_proc
)
2199 trace_xprt_ping(task
->tk_xprt
,
2201 rpc_call_rpcerror(task
, task
->tk_status
);
2210 task
->tk_action
= call_bind
;
2211 task
->tk_status
= 0;
2214 rpc_check_timeout(task
);
2217 #if defined(CONFIG_SUNRPC_BACKCHANNEL)
2218 static void call_bc_transmit(struct rpc_task
*task
);
2219 static void call_bc_transmit_status(struct rpc_task
*task
);
2222 call_bc_encode(struct rpc_task
*task
)
2224 xprt_request_enqueue_transmit(task
);
2225 task
->tk_action
= call_bc_transmit
;
2229 * 5b. Send the backchannel RPC reply. On error, drop the reply. In
2230 * addition, disconnect on connectivity errors.
2233 call_bc_transmit(struct rpc_task
*task
)
2235 task
->tk_action
= call_bc_transmit_status
;
2236 if (test_bit(RPC_TASK_NEED_XMIT
, &task
->tk_runstate
)) {
2237 if (!xprt_prepare_transmit(task
))
2239 task
->tk_status
= 0;
2240 xprt_transmit(task
);
2242 xprt_end_transmit(task
);
2246 call_bc_transmit_status(struct rpc_task
*task
)
2248 struct rpc_rqst
*req
= task
->tk_rqstp
;
2250 if (rpc_task_transmitted(task
))
2251 task
->tk_status
= 0;
2253 switch (task
->tk_status
) {
2267 rpc_delay(task
, HZ
>>2);
2271 task
->tk_status
= 0;
2272 task
->tk_action
= call_bc_transmit
;
2276 * Problem reaching the server. Disconnect and let the
2277 * forechannel reestablish the connection. The server will
2278 * have to retransmit the backchannel request and we'll
2279 * reprocess it. Since these ops are idempotent, there's no
2280 * need to cache our reply at this time.
2282 printk(KERN_NOTICE
"RPC: Could not send backchannel reply "
2283 "error: %d\n", task
->tk_status
);
2284 xprt_conditional_disconnect(req
->rq_xprt
,
2285 req
->rq_connect_cookie
);
2289 * We were unable to reply and will have to drop the
2290 * request. The server should reconnect and retransmit.
2292 printk(KERN_NOTICE
"RPC: Could not send backchannel reply "
2293 "error: %d\n", task
->tk_status
);
2296 task
->tk_action
= rpc_exit_task
;
2298 #endif /* CONFIG_SUNRPC_BACKCHANNEL */
2301 * 6. Sort out the RPC call status
2304 call_status(struct rpc_task
*task
)
2306 struct rpc_clnt
*clnt
= task
->tk_client
;
2309 if (!task
->tk_msg
.rpc_proc
->p_proc
)
2310 trace_xprt_ping(task
->tk_xprt
, task
->tk_status
);
2312 status
= task
->tk_status
;
2314 task
->tk_action
= call_decode
;
2318 trace_rpc_call_status(task
);
2319 task
->tk_status
= 0;
2326 if (RPC_IS_SOFTCONN(task
))
2329 * Delay any retries for 3 seconds, then handle as if it
2332 rpc_delay(task
, 3*HZ
);
2340 rpc_force_rebind(clnt
);
2343 rpc_delay(task
, 3*HZ
);
2349 /* shutdown or soft timeout */
2352 if (clnt
->cl_chatty
)
2353 printk("%s: RPC call returned error %d\n",
2354 clnt
->cl_program
->name
, -status
);
2357 task
->tk_action
= call_encode
;
2358 if (status
!= -ECONNRESET
&& status
!= -ECONNABORTED
)
2359 rpc_check_timeout(task
);
2362 rpc_call_rpcerror(task
, status
);
2366 rpc_check_connected(const struct rpc_rqst
*req
)
2368 /* No allocated request or transport? return true */
2369 if (!req
|| !req
->rq_xprt
)
2371 return xprt_connected(req
->rq_xprt
);
2375 rpc_check_timeout(struct rpc_task
*task
)
2377 struct rpc_clnt
*clnt
= task
->tk_client
;
2379 if (RPC_SIGNALLED(task
)) {
2380 rpc_call_rpcerror(task
, -ERESTARTSYS
);
2384 if (xprt_adjust_timeout(task
->tk_rqstp
) == 0)
2387 trace_rpc_timeout_status(task
);
2388 task
->tk_timeouts
++;
2390 if (RPC_IS_SOFTCONN(task
) && !rpc_check_connected(task
->tk_rqstp
)) {
2391 rpc_call_rpcerror(task
, -ETIMEDOUT
);
2395 if (RPC_IS_SOFT(task
)) {
2397 * Once a "no retrans timeout" soft tasks (a.k.a NFSv4) has
2398 * been sent, it should time out only if the transport
2399 * connection gets terminally broken.
2401 if ((task
->tk_flags
& RPC_TASK_NO_RETRANS_TIMEOUT
) &&
2402 rpc_check_connected(task
->tk_rqstp
))
2405 if (clnt
->cl_chatty
) {
2406 pr_notice_ratelimited(
2407 "%s: server %s not responding, timed out\n",
2408 clnt
->cl_program
->name
,
2409 task
->tk_xprt
->servername
);
2411 if (task
->tk_flags
& RPC_TASK_TIMEOUT
)
2412 rpc_call_rpcerror(task
, -ETIMEDOUT
);
2414 __rpc_call_rpcerror(task
, -EIO
, -ETIMEDOUT
);
2418 if (!(task
->tk_flags
& RPC_CALL_MAJORSEEN
)) {
2419 task
->tk_flags
|= RPC_CALL_MAJORSEEN
;
2420 if (clnt
->cl_chatty
) {
2421 pr_notice_ratelimited(
2422 "%s: server %s not responding, still trying\n",
2423 clnt
->cl_program
->name
,
2424 task
->tk_xprt
->servername
);
2427 rpc_force_rebind(clnt
);
2429 * Did our request time out due to an RPCSEC_GSS out-of-sequence
2430 * event? RFC2203 requires the server to drop all such requests.
2432 rpcauth_invalcred(task
);
2436 * 7. Decode the RPC reply
2439 call_decode(struct rpc_task
*task
)
2441 struct rpc_clnt
*clnt
= task
->tk_client
;
2442 struct rpc_rqst
*req
= task
->tk_rqstp
;
2443 struct xdr_stream xdr
;
2446 if (!task
->tk_msg
.rpc_proc
->p_decode
) {
2447 task
->tk_action
= rpc_exit_task
;
2451 if (task
->tk_flags
& RPC_CALL_MAJORSEEN
) {
2452 if (clnt
->cl_chatty
) {
2453 pr_notice_ratelimited("%s: server %s OK\n",
2454 clnt
->cl_program
->name
,
2455 task
->tk_xprt
->servername
);
2457 task
->tk_flags
&= ~RPC_CALL_MAJORSEEN
;
2461 * Ensure that we see all writes made by xprt_complete_rqst()
2462 * before it changed req->rq_reply_bytes_recvd.
2467 * Did we ever call xprt_complete_rqst()? If not, we should assume
2468 * the message is incomplete.
2471 if (!req
->rq_reply_bytes_recvd
)
2474 req
->rq_rcv_buf
.len
= req
->rq_private_buf
.len
;
2475 trace_rpc_xdr_recvfrom(task
, &req
->rq_rcv_buf
);
2477 /* Check that the softirq receive buffer is valid */
2478 WARN_ON(memcmp(&req
->rq_rcv_buf
, &req
->rq_private_buf
,
2479 sizeof(req
->rq_rcv_buf
)) != 0);
2481 xdr_init_decode(&xdr
, &req
->rq_rcv_buf
,
2482 req
->rq_rcv_buf
.head
[0].iov_base
, req
);
2483 err
= rpc_decode_header(task
, &xdr
);
2487 task
->tk_action
= rpc_exit_task
;
2488 task
->tk_status
= rpcauth_unwrap_resp(task
, &xdr
);
2491 task
->tk_status
= 0;
2492 if (task
->tk_client
->cl_discrtry
)
2493 xprt_conditional_disconnect(req
->rq_xprt
,
2494 req
->rq_connect_cookie
);
2495 task
->tk_action
= call_encode
;
2496 rpc_check_timeout(task
);
2499 task
->tk_action
= call_reserve
;
2500 rpc_check_timeout(task
);
2501 rpcauth_invalcred(task
);
2502 /* Ensure we obtain a new XID if we retry! */
2508 rpc_encode_header(struct rpc_task
*task
, struct xdr_stream
*xdr
)
2510 struct rpc_clnt
*clnt
= task
->tk_client
;
2511 struct rpc_rqst
*req
= task
->tk_rqstp
;
2516 p
= xdr_reserve_space(xdr
, RPC_CALLHDRSIZE
<< 2);
2521 *p
++ = cpu_to_be32(RPC_VERSION
);
2522 *p
++ = cpu_to_be32(clnt
->cl_prog
);
2523 *p
++ = cpu_to_be32(clnt
->cl_vers
);
2524 *p
= cpu_to_be32(task
->tk_msg
.rpc_proc
->p_proc
);
2526 error
= rpcauth_marshcred(task
, xdr
);
2531 trace_rpc_bad_callhdr(task
);
2532 rpc_call_rpcerror(task
, error
);
2537 rpc_decode_header(struct rpc_task
*task
, struct xdr_stream
*xdr
)
2539 struct rpc_clnt
*clnt
= task
->tk_client
;
2543 /* RFC-1014 says that the representation of XDR data must be a
2544 * multiple of four bytes
2545 * - if it isn't pointer subtraction in the NFS client may give
2548 if (task
->tk_rqstp
->rq_rcv_buf
.len
& 3)
2549 goto out_unparsable
;
2551 p
= xdr_inline_decode(xdr
, 3 * sizeof(*p
));
2553 goto out_unparsable
;
2555 if (*p
++ != rpc_reply
)
2556 goto out_unparsable
;
2557 if (*p
++ != rpc_msg_accepted
)
2558 goto out_msg_denied
;
2560 error
= rpcauth_checkverf(task
, xdr
);
2564 p
= xdr_inline_decode(xdr
, sizeof(*p
));
2566 goto out_unparsable
;
2570 case rpc_prog_unavail
:
2571 trace_rpc__prog_unavail(task
);
2572 error
= -EPFNOSUPPORT
;
2574 case rpc_prog_mismatch
:
2575 trace_rpc__prog_mismatch(task
);
2576 error
= -EPROTONOSUPPORT
;
2578 case rpc_proc_unavail
:
2579 trace_rpc__proc_unavail(task
);
2580 error
= -EOPNOTSUPP
;
2582 case rpc_garbage_args
:
2583 case rpc_system_err
:
2584 trace_rpc__garbage_args(task
);
2588 goto out_unparsable
;
2592 clnt
->cl_stats
->rpcgarbage
++;
2593 if (task
->tk_garb_retry
) {
2594 task
->tk_garb_retry
--;
2595 task
->tk_action
= call_encode
;
2599 rpc_call_rpcerror(task
, error
);
2603 trace_rpc__unparsable(task
);
2608 trace_rpc_bad_verifier(task
);
2613 p
= xdr_inline_decode(xdr
, sizeof(*p
));
2615 goto out_unparsable
;
2617 case rpc_auth_error
:
2620 trace_rpc__mismatch(task
);
2621 error
= -EPROTONOSUPPORT
;
2624 goto out_unparsable
;
2627 p
= xdr_inline_decode(xdr
, sizeof(*p
));
2629 goto out_unparsable
;
2631 case rpc_autherr_rejectedcred
:
2632 case rpc_autherr_rejectedverf
:
2633 case rpcsec_gsserr_credproblem
:
2634 case rpcsec_gsserr_ctxproblem
:
2635 if (!task
->tk_cred_retry
)
2637 task
->tk_cred_retry
--;
2638 trace_rpc__stale_creds(task
);
2639 return -EKEYREJECTED
;
2640 case rpc_autherr_badcred
:
2641 case rpc_autherr_badverf
:
2642 /* possibly garbled cred/verf? */
2643 if (!task
->tk_garb_retry
)
2645 task
->tk_garb_retry
--;
2646 trace_rpc__bad_creds(task
);
2647 task
->tk_action
= call_encode
;
2649 case rpc_autherr_tooweak
:
2650 trace_rpc__auth_tooweak(task
);
2651 pr_warn("RPC: server %s requires stronger authentication.\n",
2652 task
->tk_xprt
->servername
);
2655 goto out_unparsable
;
2660 static void rpcproc_encode_null(struct rpc_rqst
*rqstp
, struct xdr_stream
*xdr
,
2665 static int rpcproc_decode_null(struct rpc_rqst
*rqstp
, struct xdr_stream
*xdr
,
2671 static const struct rpc_procinfo rpcproc_null
= {
2672 .p_encode
= rpcproc_encode_null
,
2673 .p_decode
= rpcproc_decode_null
,
2676 static int rpc_ping(struct rpc_clnt
*clnt
)
2678 struct rpc_message msg
= {
2679 .rpc_proc
= &rpcproc_null
,
2682 err
= rpc_call_sync(clnt
, &msg
, RPC_TASK_SOFT
| RPC_TASK_SOFTCONN
|
2683 RPC_TASK_NULLCREDS
);
2688 struct rpc_task
*rpc_call_null_helper(struct rpc_clnt
*clnt
,
2689 struct rpc_xprt
*xprt
, struct rpc_cred
*cred
, int flags
,
2690 const struct rpc_call_ops
*ops
, void *data
)
2692 struct rpc_message msg
= {
2693 .rpc_proc
= &rpcproc_null
,
2695 struct rpc_task_setup task_setup_data
= {
2698 .rpc_message
= &msg
,
2699 .rpc_op_cred
= cred
,
2700 .callback_ops
= (ops
!= NULL
) ? ops
: &rpc_default_ops
,
2701 .callback_data
= data
,
2702 .flags
= flags
| RPC_TASK_SOFT
| RPC_TASK_SOFTCONN
|
2706 return rpc_run_task(&task_setup_data
);
2709 struct rpc_task
*rpc_call_null(struct rpc_clnt
*clnt
, struct rpc_cred
*cred
, int flags
)
2711 return rpc_call_null_helper(clnt
, NULL
, cred
, flags
, NULL
, NULL
);
2713 EXPORT_SYMBOL_GPL(rpc_call_null
);
2715 struct rpc_cb_add_xprt_calldata
{
2716 struct rpc_xprt_switch
*xps
;
2717 struct rpc_xprt
*xprt
;
2720 static void rpc_cb_add_xprt_done(struct rpc_task
*task
, void *calldata
)
2722 struct rpc_cb_add_xprt_calldata
*data
= calldata
;
2724 if (task
->tk_status
== 0)
2725 rpc_xprt_switch_add_xprt(data
->xps
, data
->xprt
);
2728 static void rpc_cb_add_xprt_release(void *calldata
)
2730 struct rpc_cb_add_xprt_calldata
*data
= calldata
;
2732 xprt_put(data
->xprt
);
2733 xprt_switch_put(data
->xps
);
2737 static const struct rpc_call_ops rpc_cb_add_xprt_call_ops
= {
2738 .rpc_call_done
= rpc_cb_add_xprt_done
,
2739 .rpc_release
= rpc_cb_add_xprt_release
,
2743 * rpc_clnt_test_and_add_xprt - Test and add a new transport to a rpc_clnt
2744 * @clnt: pointer to struct rpc_clnt
2745 * @xps: pointer to struct rpc_xprt_switch,
2746 * @xprt: pointer struct rpc_xprt
2749 int rpc_clnt_test_and_add_xprt(struct rpc_clnt
*clnt
,
2750 struct rpc_xprt_switch
*xps
, struct rpc_xprt
*xprt
,
2753 struct rpc_cb_add_xprt_calldata
*data
;
2754 struct rpc_task
*task
;
2756 data
= kmalloc(sizeof(*data
), GFP_NOFS
);
2759 data
->xps
= xprt_switch_get(xps
);
2760 data
->xprt
= xprt_get(xprt
);
2761 if (rpc_xprt_switch_has_addr(data
->xps
, (struct sockaddr
*)&xprt
->addr
)) {
2762 rpc_cb_add_xprt_release(data
);
2766 task
= rpc_call_null_helper(clnt
, xprt
, NULL
, RPC_TASK_ASYNC
,
2767 &rpc_cb_add_xprt_call_ops
, data
);
2773 EXPORT_SYMBOL_GPL(rpc_clnt_test_and_add_xprt
);
2776 * rpc_clnt_setup_test_and_add_xprt()
2778 * This is an rpc_clnt_add_xprt setup() function which returns 1 so:
2779 * 1) caller of the test function must dereference the rpc_xprt_switch
2781 * 2) test function must call rpc_xprt_switch_add_xprt, usually in
2782 * the rpc_call_done routine.
2784 * Upon success (return of 1), the test function adds the new
2785 * transport to the rpc_clnt xprt switch
2787 * @clnt: struct rpc_clnt to get the new transport
2788 * @xps: the rpc_xprt_switch to hold the new transport
2789 * @xprt: the rpc_xprt to test
2790 * @data: a struct rpc_add_xprt_test pointer that holds the test function
2791 * and test function call data
2793 int rpc_clnt_setup_test_and_add_xprt(struct rpc_clnt
*clnt
,
2794 struct rpc_xprt_switch
*xps
,
2795 struct rpc_xprt
*xprt
,
2798 struct rpc_task
*task
;
2799 struct rpc_add_xprt_test
*xtest
= (struct rpc_add_xprt_test
*)data
;
2800 int status
= -EADDRINUSE
;
2802 xprt
= xprt_get(xprt
);
2803 xprt_switch_get(xps
);
2805 if (rpc_xprt_switch_has_addr(xps
, (struct sockaddr
*)&xprt
->addr
))
2808 /* Test the connection */
2809 task
= rpc_call_null_helper(clnt
, xprt
, NULL
, 0, NULL
, NULL
);
2811 status
= PTR_ERR(task
);
2814 status
= task
->tk_status
;
2820 /* rpc_xprt_switch and rpc_xprt are deferrenced by add_xprt_test() */
2821 xtest
->add_xprt_test(clnt
, xprt
, xtest
->data
);
2824 xprt_switch_put(xps
);
2826 /* so that rpc_clnt_add_xprt does not call rpc_xprt_switch_add_xprt */
2830 xprt_switch_put(xps
);
2831 pr_info("RPC: rpc_clnt_test_xprt failed: %d addr %s not added\n",
2832 status
, xprt
->address_strings
[RPC_DISPLAY_ADDR
]);
2835 EXPORT_SYMBOL_GPL(rpc_clnt_setup_test_and_add_xprt
);
2838 * rpc_clnt_add_xprt - Add a new transport to a rpc_clnt
2839 * @clnt: pointer to struct rpc_clnt
2840 * @xprtargs: pointer to struct xprt_create
2841 * @setup: callback to test and/or set up the connection
2842 * @data: pointer to setup function data
2844 * Creates a new transport using the parameters set in args and
2846 * If ping is set, then test that connectivity succeeds before
2847 * adding the new transport.
2850 int rpc_clnt_add_xprt(struct rpc_clnt
*clnt
,
2851 struct xprt_create
*xprtargs
,
2852 int (*setup
)(struct rpc_clnt
*,
2853 struct rpc_xprt_switch
*,
2858 struct rpc_xprt_switch
*xps
;
2859 struct rpc_xprt
*xprt
;
2860 unsigned long connect_timeout
;
2861 unsigned long reconnect_timeout
;
2862 unsigned char resvport
, reuseport
;
2866 xps
= xprt_switch_get(rcu_dereference(clnt
->cl_xpi
.xpi_xpswitch
));
2867 xprt
= xprt_iter_xprt(&clnt
->cl_xpi
);
2868 if (xps
== NULL
|| xprt
== NULL
) {
2870 xprt_switch_put(xps
);
2873 resvport
= xprt
->resvport
;
2874 reuseport
= xprt
->reuseport
;
2875 connect_timeout
= xprt
->connect_timeout
;
2876 reconnect_timeout
= xprt
->max_reconnect_timeout
;
2879 xprt
= xprt_create_transport(xprtargs
);
2881 ret
= PTR_ERR(xprt
);
2882 goto out_put_switch
;
2884 xprt
->resvport
= resvport
;
2885 xprt
->reuseport
= reuseport
;
2886 if (xprt
->ops
->set_connect_timeout
!= NULL
)
2887 xprt
->ops
->set_connect_timeout(xprt
,
2891 rpc_xprt_switch_set_roundrobin(xps
);
2893 ret
= setup(clnt
, xps
, xprt
, data
);
2897 rpc_xprt_switch_add_xprt(xps
, xprt
);
2901 xprt_switch_put(xps
);
2904 EXPORT_SYMBOL_GPL(rpc_clnt_add_xprt
);
2906 struct connect_timeout_data
{
2907 unsigned long connect_timeout
;
2908 unsigned long reconnect_timeout
;
2912 rpc_xprt_set_connect_timeout(struct rpc_clnt
*clnt
,
2913 struct rpc_xprt
*xprt
,
2916 struct connect_timeout_data
*timeo
= data
;
2918 if (xprt
->ops
->set_connect_timeout
)
2919 xprt
->ops
->set_connect_timeout(xprt
,
2920 timeo
->connect_timeout
,
2921 timeo
->reconnect_timeout
);
2926 rpc_set_connect_timeout(struct rpc_clnt
*clnt
,
2927 unsigned long connect_timeout
,
2928 unsigned long reconnect_timeout
)
2930 struct connect_timeout_data timeout
= {
2931 .connect_timeout
= connect_timeout
,
2932 .reconnect_timeout
= reconnect_timeout
,
2934 rpc_clnt_iterate_for_each_xprt(clnt
,
2935 rpc_xprt_set_connect_timeout
,
2938 EXPORT_SYMBOL_GPL(rpc_set_connect_timeout
);
2940 void rpc_clnt_xprt_switch_put(struct rpc_clnt
*clnt
)
2943 xprt_switch_put(rcu_dereference(clnt
->cl_xpi
.xpi_xpswitch
));
2946 EXPORT_SYMBOL_GPL(rpc_clnt_xprt_switch_put
);
2948 void rpc_clnt_xprt_switch_add_xprt(struct rpc_clnt
*clnt
, struct rpc_xprt
*xprt
)
2951 rpc_xprt_switch_add_xprt(rcu_dereference(clnt
->cl_xpi
.xpi_xpswitch
),
2955 EXPORT_SYMBOL_GPL(rpc_clnt_xprt_switch_add_xprt
);
2957 bool rpc_clnt_xprt_switch_has_addr(struct rpc_clnt
*clnt
,
2958 const struct sockaddr
*sap
)
2960 struct rpc_xprt_switch
*xps
;
2964 xps
= rcu_dereference(clnt
->cl_xpi
.xpi_xpswitch
);
2965 ret
= rpc_xprt_switch_has_addr(xps
, sap
);
2969 EXPORT_SYMBOL_GPL(rpc_clnt_xprt_switch_has_addr
);
2971 #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
2972 static void rpc_show_header(void)
2974 printk(KERN_INFO
"-pid- flgs status -client- --rqstp- "
2975 "-timeout ---ops--\n");
2978 static void rpc_show_task(const struct rpc_clnt
*clnt
,
2979 const struct rpc_task
*task
)
2981 const char *rpc_waitq
= "none";
2983 if (RPC_IS_QUEUED(task
))
2984 rpc_waitq
= rpc_qname(task
->tk_waitqueue
);
2986 printk(KERN_INFO
"%5u %04x %6d %8p %8p %8ld %8p %sv%u %s a:%ps q:%s\n",
2987 task
->tk_pid
, task
->tk_flags
, task
->tk_status
,
2988 clnt
, task
->tk_rqstp
, rpc_task_timeout(task
), task
->tk_ops
,
2989 clnt
->cl_program
->name
, clnt
->cl_vers
, rpc_proc_name(task
),
2990 task
->tk_action
, rpc_waitq
);
2993 void rpc_show_tasks(struct net
*net
)
2995 struct rpc_clnt
*clnt
;
2996 struct rpc_task
*task
;
2998 struct sunrpc_net
*sn
= net_generic(net
, sunrpc_net_id
);
3000 spin_lock(&sn
->rpc_client_lock
);
3001 list_for_each_entry(clnt
, &sn
->all_clients
, cl_clients
) {
3002 spin_lock(&clnt
->cl_lock
);
3003 list_for_each_entry(task
, &clnt
->cl_tasks
, tk_task
) {
3008 rpc_show_task(clnt
, task
);
3010 spin_unlock(&clnt
->cl_lock
);
3012 spin_unlock(&sn
->rpc_client_lock
);
3016 #if IS_ENABLED(CONFIG_SUNRPC_SWAP)
3018 rpc_clnt_swap_activate_callback(struct rpc_clnt
*clnt
,
3019 struct rpc_xprt
*xprt
,
3022 return xprt_enable_swap(xprt
);
3026 rpc_clnt_swap_activate(struct rpc_clnt
*clnt
)
3028 if (atomic_inc_return(&clnt
->cl_swapper
) == 1)
3029 return rpc_clnt_iterate_for_each_xprt(clnt
,
3030 rpc_clnt_swap_activate_callback
, NULL
);
3033 EXPORT_SYMBOL_GPL(rpc_clnt_swap_activate
);
3036 rpc_clnt_swap_deactivate_callback(struct rpc_clnt
*clnt
,
3037 struct rpc_xprt
*xprt
,
3040 xprt_disable_swap(xprt
);
3045 rpc_clnt_swap_deactivate(struct rpc_clnt
*clnt
)
3047 if (atomic_dec_if_positive(&clnt
->cl_swapper
) == 0)
3048 rpc_clnt_iterate_for_each_xprt(clnt
,
3049 rpc_clnt_swap_deactivate_callback
, NULL
);
3051 EXPORT_SYMBOL_GPL(rpc_clnt_swap_deactivate
);
3052 #endif /* CONFIG_SUNRPC_SWAP */