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>
47 #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
48 # define RPCDBG_FACILITY RPCDBG_CALL
52 * All RPC clients are linked into this list
55 static DECLARE_WAIT_QUEUE_HEAD(destroy_wait);
58 static void call_start(struct rpc_task *task);
59 static void call_reserve(struct rpc_task *task);
60 static void call_reserveresult(struct rpc_task *task);
61 static void call_allocate(struct rpc_task *task);
62 static void call_encode(struct rpc_task *task);
63 static void call_decode(struct rpc_task *task);
64 static void call_bind(struct rpc_task *task);
65 static void call_bind_status(struct rpc_task *task);
66 static void call_transmit(struct rpc_task *task);
67 static void call_status(struct rpc_task *task);
68 static void call_transmit_status(struct rpc_task *task);
69 static void call_refresh(struct rpc_task *task);
70 static void call_refreshresult(struct rpc_task *task);
71 static void call_connect(struct rpc_task *task);
72 static void call_connect_status(struct rpc_task *task);
74 static int rpc_encode_header(struct rpc_task *task,
75 struct xdr_stream *xdr);
76 static int rpc_decode_header(struct rpc_task *task,
77 struct xdr_stream *xdr);
78 static int rpc_ping(struct rpc_clnt *clnt);
79 static int rpc_ping_noreply(struct rpc_clnt *clnt);
80 static void rpc_check_timeout(struct rpc_task *task);
82 static void rpc_register_client(struct rpc_clnt *clnt)
84 struct net *net = rpc_net_ns(clnt);
85 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
87 spin_lock(&sn->rpc_client_lock);
88 list_add(&clnt->cl_clients, &sn->all_clients);
89 spin_unlock(&sn->rpc_client_lock);
92 static void rpc_unregister_client(struct rpc_clnt *clnt)
94 struct net *net = rpc_net_ns(clnt);
95 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
97 spin_lock(&sn->rpc_client_lock);
98 list_del(&clnt->cl_clients);
99 spin_unlock(&sn->rpc_client_lock);
102 static void __rpc_clnt_remove_pipedir(struct rpc_clnt *clnt)
104 rpc_remove_client_dir(clnt);
107 static void rpc_clnt_remove_pipedir(struct rpc_clnt *clnt)
109 struct net *net = rpc_net_ns(clnt);
110 struct super_block *pipefs_sb;
112 pipefs_sb = rpc_get_sb_net(net);
114 __rpc_clnt_remove_pipedir(clnt);
119 static struct dentry *rpc_setup_pipedir_sb(struct super_block *sb,
120 struct rpc_clnt *clnt)
122 static uint32_t clntid;
123 const char *dir_name = clnt->cl_program->pipe_dir_name;
125 struct dentry *dir, *dentry;
127 dir = rpc_d_lookup_sb(sb, dir_name);
129 pr_info("RPC: pipefs directory doesn't exist: %s\n", dir_name);
133 snprintf(name, sizeof(name), "clnt%x", (unsigned int)clntid++);
134 name[sizeof(name) - 1] = '\0';
135 dentry = rpc_create_client_dir(dir, name, clnt);
138 if (dentry == ERR_PTR(-EEXIST))
140 printk(KERN_INFO "RPC: Couldn't create pipefs entry"
141 " %s/%s, error %ld\n",
142 dir_name, name, PTR_ERR(dentry));
150 rpc_setup_pipedir(struct super_block *pipefs_sb, struct rpc_clnt *clnt)
152 struct dentry *dentry;
154 if (clnt->cl_program->pipe_dir_name != NULL) {
155 dentry = rpc_setup_pipedir_sb(pipefs_sb, clnt);
157 return PTR_ERR(dentry);
162 static int rpc_clnt_skip_event(struct rpc_clnt *clnt, unsigned long event)
164 if (clnt->cl_program->pipe_dir_name == NULL)
168 case RPC_PIPEFS_MOUNT:
169 if (clnt->cl_pipedir_objects.pdh_dentry != NULL)
171 if (refcount_read(&clnt->cl_count) == 0)
174 case RPC_PIPEFS_UMOUNT:
175 if (clnt->cl_pipedir_objects.pdh_dentry == NULL)
182 static int __rpc_clnt_handle_event(struct rpc_clnt *clnt, unsigned long event,
183 struct super_block *sb)
185 struct dentry *dentry;
188 case RPC_PIPEFS_MOUNT:
189 dentry = rpc_setup_pipedir_sb(sb, clnt);
193 return PTR_ERR(dentry);
195 case RPC_PIPEFS_UMOUNT:
196 __rpc_clnt_remove_pipedir(clnt);
199 printk(KERN_ERR "%s: unknown event: %ld\n", __func__, event);
205 static int __rpc_pipefs_event(struct rpc_clnt *clnt, unsigned long event,
206 struct super_block *sb)
210 for (;; clnt = clnt->cl_parent) {
211 if (!rpc_clnt_skip_event(clnt, event))
212 error = __rpc_clnt_handle_event(clnt, event, sb);
213 if (error || clnt == clnt->cl_parent)
219 static struct rpc_clnt *rpc_get_client_for_event(struct net *net, int event)
221 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
222 struct rpc_clnt *clnt;
224 spin_lock(&sn->rpc_client_lock);
225 list_for_each_entry(clnt, &sn->all_clients, cl_clients) {
226 if (rpc_clnt_skip_event(clnt, event))
228 spin_unlock(&sn->rpc_client_lock);
231 spin_unlock(&sn->rpc_client_lock);
235 static int rpc_pipefs_event(struct notifier_block *nb, unsigned long event,
238 struct super_block *sb = ptr;
239 struct rpc_clnt *clnt;
242 while ((clnt = rpc_get_client_for_event(sb->s_fs_info, event))) {
243 error = __rpc_pipefs_event(clnt, event, sb);
250 static struct notifier_block rpc_clients_block = {
251 .notifier_call = rpc_pipefs_event,
252 .priority = SUNRPC_PIPEFS_RPC_PRIO,
255 int rpc_clients_notifier_register(void)
257 return rpc_pipefs_notifier_register(&rpc_clients_block);
260 void rpc_clients_notifier_unregister(void)
262 return rpc_pipefs_notifier_unregister(&rpc_clients_block);
265 static struct rpc_xprt *rpc_clnt_set_transport(struct rpc_clnt *clnt,
266 struct rpc_xprt *xprt,
267 const struct rpc_timeout *timeout)
269 struct rpc_xprt *old;
271 spin_lock(&clnt->cl_lock);
272 old = rcu_dereference_protected(clnt->cl_xprt,
273 lockdep_is_held(&clnt->cl_lock));
275 if (!xprt_bound(xprt))
276 clnt->cl_autobind = 1;
278 clnt->cl_timeout = timeout;
279 rcu_assign_pointer(clnt->cl_xprt, xprt);
280 spin_unlock(&clnt->cl_lock);
285 static void rpc_clnt_set_nodename(struct rpc_clnt *clnt, const char *nodename)
287 clnt->cl_nodelen = strlcpy(clnt->cl_nodename,
288 nodename, sizeof(clnt->cl_nodename));
291 static int rpc_client_register(struct rpc_clnt *clnt,
292 rpc_authflavor_t pseudoflavor,
293 const char *client_name)
295 struct rpc_auth_create_args auth_args = {
296 .pseudoflavor = pseudoflavor,
297 .target_name = client_name,
299 struct rpc_auth *auth;
300 struct net *net = rpc_net_ns(clnt);
301 struct super_block *pipefs_sb;
304 rpc_clnt_debugfs_register(clnt);
306 pipefs_sb = rpc_get_sb_net(net);
308 err = rpc_setup_pipedir(pipefs_sb, clnt);
313 rpc_register_client(clnt);
317 auth = rpcauth_create(&auth_args, clnt);
319 dprintk("RPC: Couldn't create auth handle (flavor %u)\n",
326 pipefs_sb = rpc_get_sb_net(net);
327 rpc_unregister_client(clnt);
328 __rpc_clnt_remove_pipedir(clnt);
332 rpc_sysfs_client_destroy(clnt);
333 rpc_clnt_debugfs_unregister(clnt);
337 static DEFINE_IDA(rpc_clids);
339 void rpc_cleanup_clids(void)
341 ida_destroy(&rpc_clids);
344 static int rpc_alloc_clid(struct rpc_clnt *clnt)
348 clid = ida_simple_get(&rpc_clids, 0, 0, GFP_KERNEL);
351 clnt->cl_clid = clid;
355 static void rpc_free_clid(struct rpc_clnt *clnt)
357 ida_simple_remove(&rpc_clids, clnt->cl_clid);
360 static struct rpc_clnt * rpc_new_client(const struct rpc_create_args *args,
361 struct rpc_xprt_switch *xps,
362 struct rpc_xprt *xprt,
363 struct rpc_clnt *parent)
365 const struct rpc_program *program = args->program;
366 const struct rpc_version *version;
367 struct rpc_clnt *clnt = NULL;
368 const struct rpc_timeout *timeout;
369 const char *nodename = args->nodename;
377 if (args->version >= program->nrvers)
379 version = program->version[args->version];
384 clnt = kzalloc(sizeof(*clnt), GFP_KERNEL);
387 clnt->cl_parent = parent ? : clnt;
389 err = rpc_alloc_clid(clnt);
393 clnt->cl_cred = get_cred(args->cred);
394 clnt->cl_procinfo = version->procs;
395 clnt->cl_maxproc = version->nrprocs;
396 clnt->cl_prog = args->prognumber ? : program->number;
397 clnt->cl_vers = version->number;
398 clnt->cl_stats = program->stats;
399 clnt->cl_metrics = rpc_alloc_iostats(clnt);
400 rpc_init_pipe_dir_head(&clnt->cl_pipedir_objects);
402 if (clnt->cl_metrics == NULL)
404 clnt->cl_program = program;
405 INIT_LIST_HEAD(&clnt->cl_tasks);
406 spin_lock_init(&clnt->cl_lock);
408 timeout = xprt->timeout;
409 if (args->timeout != NULL) {
410 memcpy(&clnt->cl_timeout_default, args->timeout,
411 sizeof(clnt->cl_timeout_default));
412 timeout = &clnt->cl_timeout_default;
415 rpc_clnt_set_transport(clnt, xprt, timeout);
417 xprt_iter_init(&clnt->cl_xpi, xps);
418 xprt_switch_put(xps);
420 clnt->cl_rtt = &clnt->cl_rtt_default;
421 rpc_init_rtt(&clnt->cl_rtt_default, clnt->cl_timeout->to_initval);
423 refcount_set(&clnt->cl_count, 1);
425 if (nodename == NULL)
426 nodename = utsname()->nodename;
427 /* save the nodename */
428 rpc_clnt_set_nodename(clnt, nodename);
430 rpc_sysfs_client_setup(clnt, xps, rpc_net_ns(clnt));
431 err = rpc_client_register(clnt, args->authflavor, args->client_name);
435 refcount_inc(&parent->cl_count);
437 trace_rpc_clnt_new(clnt, xprt, program->name, args->servername);
441 rpc_free_iostats(clnt->cl_metrics);
443 put_cred(clnt->cl_cred);
450 xprt_switch_put(xps);
452 trace_rpc_clnt_new_err(program->name, args->servername, err);
456 static struct rpc_clnt *rpc_create_xprt(struct rpc_create_args *args,
457 struct rpc_xprt *xprt)
459 struct rpc_clnt *clnt = NULL;
460 struct rpc_xprt_switch *xps;
462 if (args->bc_xprt && args->bc_xprt->xpt_bc_xps) {
463 WARN_ON_ONCE(!(args->protocol & XPRT_TRANSPORT_BC));
464 xps = args->bc_xprt->xpt_bc_xps;
465 xprt_switch_get(xps);
467 xps = xprt_switch_alloc(xprt, GFP_KERNEL);
470 return ERR_PTR(-ENOMEM);
473 xprt_switch_get(xps);
474 xprt->bc_xprt->xpt_bc_xps = xps;
477 clnt = rpc_new_client(args, xps, xprt, NULL);
481 if (!(args->flags & RPC_CLNT_CREATE_NOPING)) {
482 int err = rpc_ping(clnt);
484 rpc_shutdown_client(clnt);
487 } else if (args->flags & RPC_CLNT_CREATE_CONNECTED) {
488 int err = rpc_ping_noreply(clnt);
490 rpc_shutdown_client(clnt);
495 clnt->cl_softrtry = 1;
496 if (args->flags & (RPC_CLNT_CREATE_HARDRTRY|RPC_CLNT_CREATE_SOFTERR)) {
497 clnt->cl_softrtry = 0;
498 if (args->flags & RPC_CLNT_CREATE_SOFTERR)
499 clnt->cl_softerr = 1;
502 if (args->flags & RPC_CLNT_CREATE_AUTOBIND)
503 clnt->cl_autobind = 1;
504 if (args->flags & RPC_CLNT_CREATE_NO_RETRANS_TIMEOUT)
505 clnt->cl_noretranstimeo = 1;
506 if (args->flags & RPC_CLNT_CREATE_DISCRTRY)
507 clnt->cl_discrtry = 1;
508 if (!(args->flags & RPC_CLNT_CREATE_QUIET))
515 * rpc_create - create an RPC client and transport with one call
516 * @args: rpc_clnt create argument structure
518 * Creates and initializes an RPC transport and an RPC client.
520 * It can ping the server in order to determine if it is up, and to see if
521 * it supports this program and version. RPC_CLNT_CREATE_NOPING disables
522 * this behavior so asynchronous tasks can also use rpc_create.
524 struct rpc_clnt *rpc_create(struct rpc_create_args *args)
526 struct rpc_xprt *xprt;
527 struct xprt_create xprtargs = {
529 .ident = args->protocol,
530 .srcaddr = args->saddress,
531 .dstaddr = args->address,
532 .addrlen = args->addrsize,
533 .servername = args->servername,
534 .bc_xprt = args->bc_xprt,
537 struct rpc_clnt *clnt;
541 WARN_ON_ONCE(!(args->protocol & XPRT_TRANSPORT_BC));
542 xprt = args->bc_xprt->xpt_bc_xprt;
545 return rpc_create_xprt(args, xprt);
549 if (args->flags & RPC_CLNT_CREATE_INFINITE_SLOTS)
550 xprtargs.flags |= XPRT_CREATE_INFINITE_SLOTS;
551 if (args->flags & RPC_CLNT_CREATE_NO_IDLE_TIMEOUT)
552 xprtargs.flags |= XPRT_CREATE_NO_IDLE_TIMEOUT;
554 * If the caller chooses not to specify a hostname, whip
555 * up a string representation of the passed-in address.
557 if (xprtargs.servername == NULL) {
558 struct sockaddr_un *sun =
559 (struct sockaddr_un *)args->address;
560 struct sockaddr_in *sin =
561 (struct sockaddr_in *)args->address;
562 struct sockaddr_in6 *sin6 =
563 (struct sockaddr_in6 *)args->address;
565 servername[0] = '\0';
566 switch (args->address->sa_family) {
568 snprintf(servername, sizeof(servername), "%s",
572 snprintf(servername, sizeof(servername), "%pI4",
573 &sin->sin_addr.s_addr);
576 snprintf(servername, sizeof(servername), "%pI6",
580 /* caller wants default server name, but
581 * address family isn't recognized. */
582 return ERR_PTR(-EINVAL);
584 xprtargs.servername = servername;
587 xprt = xprt_create_transport(&xprtargs);
589 return (struct rpc_clnt *)xprt;
592 * By default, kernel RPC client connects from a reserved port.
593 * CAP_NET_BIND_SERVICE will not be set for unprivileged requesters,
594 * but it is always enabled for rpciod, which handles the connect
598 if (args->flags & RPC_CLNT_CREATE_NONPRIVPORT)
601 if (args->flags & RPC_CLNT_CREATE_REUSEPORT)
604 clnt = rpc_create_xprt(args, xprt);
605 if (IS_ERR(clnt) || args->nconnect <= 1)
608 for (i = 0; i < args->nconnect - 1; i++) {
609 if (rpc_clnt_add_xprt(clnt, &xprtargs, NULL, NULL) < 0)
614 EXPORT_SYMBOL_GPL(rpc_create);
617 * This function clones the RPC client structure. It allows us to share the
618 * same transport while varying parameters such as the authentication
621 static struct rpc_clnt *__rpc_clone_client(struct rpc_create_args *args,
622 struct rpc_clnt *clnt)
624 struct rpc_xprt_switch *xps;
625 struct rpc_xprt *xprt;
626 struct rpc_clnt *new;
631 xprt = xprt_get(rcu_dereference(clnt->cl_xprt));
632 xps = xprt_switch_get(rcu_dereference(clnt->cl_xpi.xpi_xpswitch));
634 if (xprt == NULL || xps == NULL) {
636 xprt_switch_put(xps);
639 args->servername = xprt->servername;
640 args->nodename = clnt->cl_nodename;
642 new = rpc_new_client(args, xps, xprt, clnt);
646 /* Turn off autobind on clones */
647 new->cl_autobind = 0;
648 new->cl_softrtry = clnt->cl_softrtry;
649 new->cl_softerr = clnt->cl_softerr;
650 new->cl_noretranstimeo = clnt->cl_noretranstimeo;
651 new->cl_discrtry = clnt->cl_discrtry;
652 new->cl_chatty = clnt->cl_chatty;
653 new->cl_principal = clnt->cl_principal;
654 new->cl_max_connect = clnt->cl_max_connect;
658 trace_rpc_clnt_clone_err(clnt, err);
663 * rpc_clone_client - Clone an RPC client structure
665 * @clnt: RPC client whose parameters are copied
667 * Returns a fresh RPC client or an ERR_PTR.
669 struct rpc_clnt *rpc_clone_client(struct rpc_clnt *clnt)
671 struct rpc_create_args args = {
672 .program = clnt->cl_program,
673 .prognumber = clnt->cl_prog,
674 .version = clnt->cl_vers,
675 .authflavor = clnt->cl_auth->au_flavor,
676 .cred = clnt->cl_cred,
678 return __rpc_clone_client(&args, clnt);
680 EXPORT_SYMBOL_GPL(rpc_clone_client);
683 * rpc_clone_client_set_auth - Clone an RPC client structure and set its auth
685 * @clnt: RPC client whose parameters are copied
686 * @flavor: security flavor for new client
688 * Returns a fresh RPC client or an ERR_PTR.
691 rpc_clone_client_set_auth(struct rpc_clnt *clnt, rpc_authflavor_t flavor)
693 struct rpc_create_args args = {
694 .program = clnt->cl_program,
695 .prognumber = clnt->cl_prog,
696 .version = clnt->cl_vers,
697 .authflavor = flavor,
698 .cred = clnt->cl_cred,
700 return __rpc_clone_client(&args, clnt);
702 EXPORT_SYMBOL_GPL(rpc_clone_client_set_auth);
705 * rpc_switch_client_transport: switch the RPC transport on the fly
706 * @clnt: pointer to a struct rpc_clnt
707 * @args: pointer to the new transport arguments
708 * @timeout: pointer to the new timeout parameters
710 * This function allows the caller to switch the RPC transport for the
711 * rpc_clnt structure 'clnt' to allow it to connect to a mirrored NFS
712 * server, for instance. It assumes that the caller has ensured that
713 * there are no active RPC tasks by using some form of locking.
715 * Returns zero if "clnt" is now using the new xprt. Otherwise a
716 * negative errno is returned, and "clnt" continues to use the old
719 int rpc_switch_client_transport(struct rpc_clnt *clnt,
720 struct xprt_create *args,
721 const struct rpc_timeout *timeout)
723 const struct rpc_timeout *old_timeo;
724 rpc_authflavor_t pseudoflavor;
725 struct rpc_xprt_switch *xps, *oldxps;
726 struct rpc_xprt *xprt, *old;
727 struct rpc_clnt *parent;
730 xprt = xprt_create_transport(args);
732 return PTR_ERR(xprt);
734 xps = xprt_switch_alloc(xprt, GFP_KERNEL);
740 pseudoflavor = clnt->cl_auth->au_flavor;
742 old_timeo = clnt->cl_timeout;
743 old = rpc_clnt_set_transport(clnt, xprt, timeout);
744 oldxps = xprt_iter_xchg_switch(&clnt->cl_xpi, xps);
746 rpc_unregister_client(clnt);
747 __rpc_clnt_remove_pipedir(clnt);
748 rpc_sysfs_client_destroy(clnt);
749 rpc_clnt_debugfs_unregister(clnt);
752 * A new transport was created. "clnt" therefore
753 * becomes the root of a new cl_parent tree. clnt's
754 * children, if it has any, still point to the old xprt.
756 parent = clnt->cl_parent;
757 clnt->cl_parent = clnt;
760 * The old rpc_auth cache cannot be re-used. GSS
761 * contexts in particular are between a single
764 err = rpc_client_register(clnt, pseudoflavor, NULL);
770 rpc_release_client(parent);
771 xprt_switch_put(oldxps);
773 trace_rpc_clnt_replace_xprt(clnt);
777 xps = xprt_iter_xchg_switch(&clnt->cl_xpi, oldxps);
778 rpc_clnt_set_transport(clnt, old, old_timeo);
779 clnt->cl_parent = parent;
780 rpc_client_register(clnt, pseudoflavor, NULL);
781 xprt_switch_put(xps);
783 trace_rpc_clnt_replace_xprt_err(clnt);
786 EXPORT_SYMBOL_GPL(rpc_switch_client_transport);
789 int _rpc_clnt_xprt_iter_init(struct rpc_clnt *clnt, struct rpc_xprt_iter *xpi,
790 void func(struct rpc_xprt_iter *xpi, struct rpc_xprt_switch *xps))
792 struct rpc_xprt_switch *xps;
795 xps = xprt_switch_get(rcu_dereference(clnt->cl_xpi.xpi_xpswitch));
800 xprt_switch_put(xps);
805 int rpc_clnt_xprt_iter_init(struct rpc_clnt *clnt, struct rpc_xprt_iter *xpi)
807 return _rpc_clnt_xprt_iter_init(clnt, xpi, xprt_iter_init_listall);
811 int rpc_clnt_xprt_iter_offline_init(struct rpc_clnt *clnt,
812 struct rpc_xprt_iter *xpi)
814 return _rpc_clnt_xprt_iter_init(clnt, xpi, xprt_iter_init_listoffline);
818 * rpc_clnt_iterate_for_each_xprt - Apply a function to all transports
819 * @clnt: pointer to client
820 * @fn: function to apply
821 * @data: void pointer to function data
823 * Iterates through the list of RPC transports currently attached to the
824 * client and applies the function fn(clnt, xprt, data).
826 * On error, the iteration stops, and the function returns the error value.
828 int rpc_clnt_iterate_for_each_xprt(struct rpc_clnt *clnt,
829 int (*fn)(struct rpc_clnt *, struct rpc_xprt *, void *),
832 struct rpc_xprt_iter xpi;
835 ret = rpc_clnt_xprt_iter_init(clnt, &xpi);
839 struct rpc_xprt *xprt = xprt_iter_get_next(&xpi);
843 ret = fn(clnt, xprt, data);
848 xprt_iter_destroy(&xpi);
851 EXPORT_SYMBOL_GPL(rpc_clnt_iterate_for_each_xprt);
854 * Kill all tasks for the given client.
855 * XXX: kill their descendants as well?
857 void rpc_killall_tasks(struct rpc_clnt *clnt)
859 struct rpc_task *rovr;
862 if (list_empty(&clnt->cl_tasks))
866 * Spin lock all_tasks to prevent changes...
868 trace_rpc_clnt_killall(clnt);
869 spin_lock(&clnt->cl_lock);
870 list_for_each_entry(rovr, &clnt->cl_tasks, tk_task)
871 rpc_signal_task(rovr);
872 spin_unlock(&clnt->cl_lock);
874 EXPORT_SYMBOL_GPL(rpc_killall_tasks);
877 * Properly shut down an RPC client, terminating all outstanding
880 void rpc_shutdown_client(struct rpc_clnt *clnt)
884 trace_rpc_clnt_shutdown(clnt);
886 while (!list_empty(&clnt->cl_tasks)) {
887 rpc_killall_tasks(clnt);
888 wait_event_timeout(destroy_wait,
889 list_empty(&clnt->cl_tasks), 1*HZ);
892 rpc_release_client(clnt);
894 EXPORT_SYMBOL_GPL(rpc_shutdown_client);
899 static void rpc_free_client_work(struct work_struct *work)
901 struct rpc_clnt *clnt = container_of(work, struct rpc_clnt, cl_work);
903 trace_rpc_clnt_free(clnt);
905 /* These might block on processes that might allocate memory,
906 * so they cannot be called in rpciod, so they are handled separately
909 rpc_sysfs_client_destroy(clnt);
910 rpc_clnt_debugfs_unregister(clnt);
912 rpc_clnt_remove_pipedir(clnt);
913 xprt_put(rcu_dereference_raw(clnt->cl_xprt));
918 static struct rpc_clnt *
919 rpc_free_client(struct rpc_clnt *clnt)
921 struct rpc_clnt *parent = NULL;
923 trace_rpc_clnt_release(clnt);
924 if (clnt->cl_parent != clnt)
925 parent = clnt->cl_parent;
926 rpc_unregister_client(clnt);
927 rpc_free_iostats(clnt->cl_metrics);
928 clnt->cl_metrics = NULL;
929 xprt_iter_destroy(&clnt->cl_xpi);
930 put_cred(clnt->cl_cred);
932 INIT_WORK(&clnt->cl_work, rpc_free_client_work);
933 schedule_work(&clnt->cl_work);
940 static struct rpc_clnt *
941 rpc_free_auth(struct rpc_clnt *clnt)
944 * Note: RPCSEC_GSS may need to send NULL RPC calls in order to
945 * release remaining GSS contexts. This mechanism ensures
946 * that it can do so safely.
948 if (clnt->cl_auth != NULL) {
949 rpcauth_release(clnt->cl_auth);
950 clnt->cl_auth = NULL;
952 if (refcount_dec_and_test(&clnt->cl_count))
953 return rpc_free_client(clnt);
958 * Release reference to the RPC client
961 rpc_release_client(struct rpc_clnt *clnt)
964 if (list_empty(&clnt->cl_tasks))
965 wake_up(&destroy_wait);
966 if (refcount_dec_not_one(&clnt->cl_count))
968 clnt = rpc_free_auth(clnt);
969 } while (clnt != NULL);
971 EXPORT_SYMBOL_GPL(rpc_release_client);
974 * rpc_bind_new_program - bind a new RPC program to an existing client
975 * @old: old rpc_client
976 * @program: rpc program to set
977 * @vers: rpc program version
979 * Clones the rpc client and sets up a new RPC program. This is mainly
980 * of use for enabling different RPC programs to share the same transport.
981 * The Sun NFSv2/v3 ACL protocol can do this.
983 struct rpc_clnt *rpc_bind_new_program(struct rpc_clnt *old,
984 const struct rpc_program *program,
987 struct rpc_create_args args = {
989 .prognumber = program->number,
991 .authflavor = old->cl_auth->au_flavor,
992 .cred = old->cl_cred,
994 struct rpc_clnt *clnt;
997 clnt = __rpc_clone_client(&args, old);
1000 err = rpc_ping(clnt);
1002 rpc_shutdown_client(clnt);
1003 clnt = ERR_PTR(err);
1008 EXPORT_SYMBOL_GPL(rpc_bind_new_program);
1011 rpc_task_get_xprt(struct rpc_clnt *clnt, struct rpc_xprt *xprt)
1013 struct rpc_xprt_switch *xps;
1018 xps = rcu_dereference(clnt->cl_xpi.xpi_xpswitch);
1019 atomic_long_inc(&xps->xps_queuelen);
1021 atomic_long_inc(&xprt->queuelen);
1027 rpc_task_release_xprt(struct rpc_clnt *clnt, struct rpc_xprt *xprt)
1029 struct rpc_xprt_switch *xps;
1031 atomic_long_dec(&xprt->queuelen);
1033 xps = rcu_dereference(clnt->cl_xpi.xpi_xpswitch);
1034 atomic_long_dec(&xps->xps_queuelen);
1040 void rpc_task_release_transport(struct rpc_task *task)
1042 struct rpc_xprt *xprt = task->tk_xprt;
1045 task->tk_xprt = NULL;
1046 if (task->tk_client)
1047 rpc_task_release_xprt(task->tk_client, xprt);
1052 EXPORT_SYMBOL_GPL(rpc_task_release_transport);
1054 void rpc_task_release_client(struct rpc_task *task)
1056 struct rpc_clnt *clnt = task->tk_client;
1058 rpc_task_release_transport(task);
1060 /* Remove from client task list */
1061 spin_lock(&clnt->cl_lock);
1062 list_del(&task->tk_task);
1063 spin_unlock(&clnt->cl_lock);
1064 task->tk_client = NULL;
1066 rpc_release_client(clnt);
1070 static struct rpc_xprt *
1071 rpc_task_get_first_xprt(struct rpc_clnt *clnt)
1073 struct rpc_xprt *xprt;
1076 xprt = xprt_get(rcu_dereference(clnt->cl_xprt));
1078 return rpc_task_get_xprt(clnt, xprt);
1081 static struct rpc_xprt *
1082 rpc_task_get_next_xprt(struct rpc_clnt *clnt)
1084 return rpc_task_get_xprt(clnt, xprt_iter_get_next(&clnt->cl_xpi));
1088 void rpc_task_set_transport(struct rpc_task *task, struct rpc_clnt *clnt)
1090 if (task->tk_xprt) {
1091 if (!(test_bit(XPRT_OFFLINE, &task->tk_xprt->state) &&
1092 (task->tk_flags & RPC_TASK_MOVEABLE)))
1095 xprt_put(task->tk_xprt);
1097 if (task->tk_flags & RPC_TASK_NO_ROUND_ROBIN)
1098 task->tk_xprt = rpc_task_get_first_xprt(clnt);
1100 task->tk_xprt = rpc_task_get_next_xprt(clnt);
1104 void rpc_task_set_client(struct rpc_task *task, struct rpc_clnt *clnt)
1106 rpc_task_set_transport(task, clnt);
1107 task->tk_client = clnt;
1108 refcount_inc(&clnt->cl_count);
1109 if (clnt->cl_softrtry)
1110 task->tk_flags |= RPC_TASK_SOFT;
1111 if (clnt->cl_softerr)
1112 task->tk_flags |= RPC_TASK_TIMEOUT;
1113 if (clnt->cl_noretranstimeo)
1114 task->tk_flags |= RPC_TASK_NO_RETRANS_TIMEOUT;
1115 /* Add to the client's list of all tasks */
1116 spin_lock(&clnt->cl_lock);
1117 list_add_tail(&task->tk_task, &clnt->cl_tasks);
1118 spin_unlock(&clnt->cl_lock);
1122 rpc_task_set_rpc_message(struct rpc_task *task, const struct rpc_message *msg)
1125 task->tk_msg.rpc_proc = msg->rpc_proc;
1126 task->tk_msg.rpc_argp = msg->rpc_argp;
1127 task->tk_msg.rpc_resp = msg->rpc_resp;
1128 task->tk_msg.rpc_cred = msg->rpc_cred;
1129 if (!(task->tk_flags & RPC_TASK_CRED_NOREF))
1130 get_cred(task->tk_msg.rpc_cred);
1135 * Default callback for async RPC calls
1138 rpc_default_callback(struct rpc_task *task, void *data)
1142 static const struct rpc_call_ops rpc_default_ops = {
1143 .rpc_call_done = rpc_default_callback,
1147 * rpc_run_task - Allocate a new RPC task, then run rpc_execute against it
1148 * @task_setup_data: pointer to task initialisation data
1150 struct rpc_task *rpc_run_task(const struct rpc_task_setup *task_setup_data)
1152 struct rpc_task *task;
1154 task = rpc_new_task(task_setup_data);
1158 if (!RPC_IS_ASYNC(task))
1159 task->tk_flags |= RPC_TASK_CRED_NOREF;
1161 rpc_task_set_client(task, task_setup_data->rpc_client);
1162 rpc_task_set_rpc_message(task, task_setup_data->rpc_message);
1164 if (task->tk_action == NULL)
1165 rpc_call_start(task);
1167 atomic_inc(&task->tk_count);
1171 EXPORT_SYMBOL_GPL(rpc_run_task);
1174 * rpc_call_sync - Perform a synchronous RPC call
1175 * @clnt: pointer to RPC client
1176 * @msg: RPC call parameters
1177 * @flags: RPC call flags
1179 int rpc_call_sync(struct rpc_clnt *clnt, const struct rpc_message *msg, int flags)
1181 struct rpc_task *task;
1182 struct rpc_task_setup task_setup_data = {
1185 .callback_ops = &rpc_default_ops,
1190 WARN_ON_ONCE(flags & RPC_TASK_ASYNC);
1191 if (flags & RPC_TASK_ASYNC) {
1192 rpc_release_calldata(task_setup_data.callback_ops,
1193 task_setup_data.callback_data);
1197 task = rpc_run_task(&task_setup_data);
1199 return PTR_ERR(task);
1200 status = task->tk_status;
1204 EXPORT_SYMBOL_GPL(rpc_call_sync);
1207 * rpc_call_async - Perform an asynchronous RPC call
1208 * @clnt: pointer to RPC client
1209 * @msg: RPC call parameters
1210 * @flags: RPC call flags
1211 * @tk_ops: RPC call ops
1212 * @data: user call data
1215 rpc_call_async(struct rpc_clnt *clnt, const struct rpc_message *msg, int flags,
1216 const struct rpc_call_ops *tk_ops, void *data)
1218 struct rpc_task *task;
1219 struct rpc_task_setup task_setup_data = {
1222 .callback_ops = tk_ops,
1223 .callback_data = data,
1224 .flags = flags|RPC_TASK_ASYNC,
1227 task = rpc_run_task(&task_setup_data);
1229 return PTR_ERR(task);
1233 EXPORT_SYMBOL_GPL(rpc_call_async);
1235 #if defined(CONFIG_SUNRPC_BACKCHANNEL)
1236 static void call_bc_encode(struct rpc_task *task);
1239 * rpc_run_bc_task - Allocate a new RPC task for backchannel use, then run
1240 * rpc_execute against it
1243 struct rpc_task *rpc_run_bc_task(struct rpc_rqst *req)
1245 struct rpc_task *task;
1246 struct rpc_task_setup task_setup_data = {
1247 .callback_ops = &rpc_default_ops,
1248 .flags = RPC_TASK_SOFTCONN |
1249 RPC_TASK_NO_RETRANS_TIMEOUT,
1252 dprintk("RPC: rpc_run_bc_task req= %p\n", req);
1254 * Create an rpc_task to send the data
1256 task = rpc_new_task(&task_setup_data);
1258 xprt_free_bc_request(req);
1262 xprt_init_bc_request(req, task);
1264 task->tk_action = call_bc_encode;
1265 atomic_inc(&task->tk_count);
1266 WARN_ON_ONCE(atomic_read(&task->tk_count) != 2);
1269 dprintk("RPC: rpc_run_bc_task: task= %p\n", task);
1272 #endif /* CONFIG_SUNRPC_BACKCHANNEL */
1275 * rpc_prepare_reply_pages - Prepare to receive a reply data payload into pages
1276 * @req: RPC request to prepare
1277 * @pages: vector of struct page pointers
1278 * @base: offset in first page where receive should start, in bytes
1279 * @len: expected size of the upper layer data payload, in bytes
1280 * @hdrsize: expected size of upper layer reply header, in XDR words
1283 void rpc_prepare_reply_pages(struct rpc_rqst *req, struct page **pages,
1284 unsigned int base, unsigned int len,
1285 unsigned int hdrsize)
1287 hdrsize += RPC_REPHDRSIZE + req->rq_cred->cr_auth->au_ralign;
1289 xdr_inline_pages(&req->rq_rcv_buf, hdrsize << 2, pages, base, len);
1290 trace_rpc_xdr_reply_pages(req->rq_task, &req->rq_rcv_buf);
1292 EXPORT_SYMBOL_GPL(rpc_prepare_reply_pages);
1295 rpc_call_start(struct rpc_task *task)
1297 task->tk_action = call_start;
1299 EXPORT_SYMBOL_GPL(rpc_call_start);
1302 * rpc_peeraddr - extract remote peer address from clnt's xprt
1303 * @clnt: RPC client structure
1304 * @buf: target buffer
1305 * @bufsize: length of target buffer
1307 * Returns the number of bytes that are actually in the stored address.
1309 size_t rpc_peeraddr(struct rpc_clnt *clnt, struct sockaddr *buf, size_t bufsize)
1312 struct rpc_xprt *xprt;
1315 xprt = rcu_dereference(clnt->cl_xprt);
1317 bytes = xprt->addrlen;
1318 if (bytes > bufsize)
1320 memcpy(buf, &xprt->addr, bytes);
1325 EXPORT_SYMBOL_GPL(rpc_peeraddr);
1328 * rpc_peeraddr2str - return remote peer address in printable format
1329 * @clnt: RPC client structure
1330 * @format: address format
1332 * NB: the lifetime of the memory referenced by the returned pointer is
1333 * the same as the rpc_xprt itself. As long as the caller uses this
1334 * pointer, it must hold the RCU read lock.
1336 const char *rpc_peeraddr2str(struct rpc_clnt *clnt,
1337 enum rpc_display_format_t format)
1339 struct rpc_xprt *xprt;
1341 xprt = rcu_dereference(clnt->cl_xprt);
1343 if (xprt->address_strings[format] != NULL)
1344 return xprt->address_strings[format];
1346 return "unprintable";
1348 EXPORT_SYMBOL_GPL(rpc_peeraddr2str);
1350 static const struct sockaddr_in rpc_inaddr_loopback = {
1351 .sin_family = AF_INET,
1352 .sin_addr.s_addr = htonl(INADDR_ANY),
1355 static const struct sockaddr_in6 rpc_in6addr_loopback = {
1356 .sin6_family = AF_INET6,
1357 .sin6_addr = IN6ADDR_ANY_INIT,
1361 * Try a getsockname() on a connected datagram socket. Using a
1362 * connected datagram socket prevents leaving a socket in TIME_WAIT.
1363 * This conserves the ephemeral port number space.
1365 * Returns zero and fills in "buf" if successful; otherwise, a
1366 * negative errno is returned.
1368 static int rpc_sockname(struct net *net, struct sockaddr *sap, size_t salen,
1369 struct sockaddr *buf)
1371 struct socket *sock;
1374 err = __sock_create(net, sap->sa_family,
1375 SOCK_DGRAM, IPPROTO_UDP, &sock, 1);
1377 dprintk("RPC: can't create UDP socket (%d)\n", err);
1381 switch (sap->sa_family) {
1383 err = kernel_bind(sock,
1384 (struct sockaddr *)&rpc_inaddr_loopback,
1385 sizeof(rpc_inaddr_loopback));
1388 err = kernel_bind(sock,
1389 (struct sockaddr *)&rpc_in6addr_loopback,
1390 sizeof(rpc_in6addr_loopback));
1393 err = -EAFNOSUPPORT;
1397 dprintk("RPC: can't bind UDP socket (%d)\n", err);
1401 err = kernel_connect(sock, sap, salen, 0);
1403 dprintk("RPC: can't connect UDP socket (%d)\n", err);
1407 err = kernel_getsockname(sock, buf);
1409 dprintk("RPC: getsockname failed (%d)\n", err);
1414 if (buf->sa_family == AF_INET6) {
1415 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)buf;
1416 sin6->sin6_scope_id = 0;
1418 dprintk("RPC: %s succeeded\n", __func__);
1427 * Scraping a connected socket failed, so we don't have a useable
1428 * local address. Fallback: generate an address that will prevent
1429 * the server from calling us back.
1431 * Returns zero and fills in "buf" if successful; otherwise, a
1432 * negative errno is returned.
1434 static int rpc_anyaddr(int family, struct sockaddr *buf, size_t buflen)
1438 if (buflen < sizeof(rpc_inaddr_loopback))
1440 memcpy(buf, &rpc_inaddr_loopback,
1441 sizeof(rpc_inaddr_loopback));
1444 if (buflen < sizeof(rpc_in6addr_loopback))
1446 memcpy(buf, &rpc_in6addr_loopback,
1447 sizeof(rpc_in6addr_loopback));
1450 dprintk("RPC: %s: address family not supported\n",
1452 return -EAFNOSUPPORT;
1454 dprintk("RPC: %s: succeeded\n", __func__);
1459 * rpc_localaddr - discover local endpoint address for an RPC client
1460 * @clnt: RPC client structure
1461 * @buf: target buffer
1462 * @buflen: size of target buffer, in bytes
1464 * Returns zero and fills in "buf" and "buflen" if successful;
1465 * otherwise, a negative errno is returned.
1467 * This works even if the underlying transport is not currently connected,
1468 * or if the upper layer never previously provided a source address.
1470 * The result of this function call is transient: multiple calls in
1471 * succession may give different results, depending on how local
1472 * networking configuration changes over time.
1474 int rpc_localaddr(struct rpc_clnt *clnt, struct sockaddr *buf, size_t buflen)
1476 struct sockaddr_storage address;
1477 struct sockaddr *sap = (struct sockaddr *)&address;
1478 struct rpc_xprt *xprt;
1484 xprt = rcu_dereference(clnt->cl_xprt);
1485 salen = xprt->addrlen;
1486 memcpy(sap, &xprt->addr, salen);
1487 net = get_net(xprt->xprt_net);
1490 rpc_set_port(sap, 0);
1491 err = rpc_sockname(net, sap, salen, buf);
1494 /* Couldn't discover local address, return ANYADDR */
1495 return rpc_anyaddr(sap->sa_family, buf, buflen);
1498 EXPORT_SYMBOL_GPL(rpc_localaddr);
1501 rpc_setbufsize(struct rpc_clnt *clnt, unsigned int sndsize, unsigned int rcvsize)
1503 struct rpc_xprt *xprt;
1506 xprt = rcu_dereference(clnt->cl_xprt);
1507 if (xprt->ops->set_buffer_size)
1508 xprt->ops->set_buffer_size(xprt, sndsize, rcvsize);
1511 EXPORT_SYMBOL_GPL(rpc_setbufsize);
1514 * rpc_net_ns - Get the network namespace for this RPC client
1515 * @clnt: RPC client to query
1518 struct net *rpc_net_ns(struct rpc_clnt *clnt)
1523 ret = rcu_dereference(clnt->cl_xprt)->xprt_net;
1527 EXPORT_SYMBOL_GPL(rpc_net_ns);
1530 * rpc_max_payload - Get maximum payload size for a transport, in bytes
1531 * @clnt: RPC client to query
1533 * For stream transports, this is one RPC record fragment (see RFC
1534 * 1831), as we don't support multi-record requests yet. For datagram
1535 * transports, this is the size of an IP packet minus the IP, UDP, and
1538 size_t rpc_max_payload(struct rpc_clnt *clnt)
1543 ret = rcu_dereference(clnt->cl_xprt)->max_payload;
1547 EXPORT_SYMBOL_GPL(rpc_max_payload);
1550 * rpc_max_bc_payload - Get maximum backchannel payload size, in bytes
1551 * @clnt: RPC client to query
1553 size_t rpc_max_bc_payload(struct rpc_clnt *clnt)
1555 struct rpc_xprt *xprt;
1559 xprt = rcu_dereference(clnt->cl_xprt);
1560 ret = xprt->ops->bc_maxpayload(xprt);
1564 EXPORT_SYMBOL_GPL(rpc_max_bc_payload);
1566 unsigned int rpc_num_bc_slots(struct rpc_clnt *clnt)
1568 struct rpc_xprt *xprt;
1572 xprt = rcu_dereference(clnt->cl_xprt);
1573 ret = xprt->ops->bc_num_slots(xprt);
1577 EXPORT_SYMBOL_GPL(rpc_num_bc_slots);
1580 * rpc_force_rebind - force transport to check that remote port is unchanged
1581 * @clnt: client to rebind
1584 void rpc_force_rebind(struct rpc_clnt *clnt)
1586 if (clnt->cl_autobind) {
1588 xprt_clear_bound(rcu_dereference(clnt->cl_xprt));
1592 EXPORT_SYMBOL_GPL(rpc_force_rebind);
1595 __rpc_restart_call(struct rpc_task *task, void (*action)(struct rpc_task *))
1597 task->tk_status = 0;
1598 task->tk_rpc_status = 0;
1599 task->tk_action = action;
1604 * Restart an (async) RPC call. Usually called from within the
1608 rpc_restart_call(struct rpc_task *task)
1610 return __rpc_restart_call(task, call_start);
1612 EXPORT_SYMBOL_GPL(rpc_restart_call);
1615 * Restart an (async) RPC call from the call_prepare state.
1616 * Usually called from within the exit handler.
1619 rpc_restart_call_prepare(struct rpc_task *task)
1621 if (task->tk_ops->rpc_call_prepare != NULL)
1622 return __rpc_restart_call(task, rpc_prepare_task);
1623 return rpc_restart_call(task);
1625 EXPORT_SYMBOL_GPL(rpc_restart_call_prepare);
1628 *rpc_proc_name(const struct rpc_task *task)
1630 const struct rpc_procinfo *proc = task->tk_msg.rpc_proc;
1634 return proc->p_name;
1642 __rpc_call_rpcerror(struct rpc_task *task, int tk_status, int rpc_status)
1644 trace_rpc_call_rpcerror(task, tk_status, rpc_status);
1645 task->tk_rpc_status = rpc_status;
1646 rpc_exit(task, tk_status);
1650 rpc_call_rpcerror(struct rpc_task *task, int status)
1652 __rpc_call_rpcerror(task, status, status);
1658 * Other FSM states can be visited zero or more times, but
1659 * this state is visited exactly once for each RPC.
1662 call_start(struct rpc_task *task)
1664 struct rpc_clnt *clnt = task->tk_client;
1665 int idx = task->tk_msg.rpc_proc->p_statidx;
1667 trace_rpc_request(task);
1669 /* Increment call count (version might not be valid for ping) */
1670 if (clnt->cl_program->version[clnt->cl_vers])
1671 clnt->cl_program->version[clnt->cl_vers]->counts[idx]++;
1672 clnt->cl_stats->rpccnt++;
1673 task->tk_action = call_reserve;
1674 rpc_task_set_transport(task, clnt);
1678 * 1. Reserve an RPC call slot
1681 call_reserve(struct rpc_task *task)
1683 task->tk_status = 0;
1684 task->tk_action = call_reserveresult;
1688 static void call_retry_reserve(struct rpc_task *task);
1691 * 1b. Grok the result of xprt_reserve()
1694 call_reserveresult(struct rpc_task *task)
1696 int status = task->tk_status;
1699 * After a call to xprt_reserve(), we must have either
1700 * a request slot or else an error status.
1702 task->tk_status = 0;
1704 if (task->tk_rqstp) {
1705 task->tk_action = call_refresh;
1709 rpc_call_rpcerror(task, -EIO);
1715 rpc_delay(task, HZ >> 2);
1717 case -EAGAIN: /* woken up; retry */
1718 task->tk_action = call_retry_reserve;
1721 rpc_call_rpcerror(task, status);
1726 * 1c. Retry reserving an RPC call slot
1729 call_retry_reserve(struct rpc_task *task)
1731 task->tk_status = 0;
1732 task->tk_action = call_reserveresult;
1733 xprt_retry_reserve(task);
1737 * 2. Bind and/or refresh the credentials
1740 call_refresh(struct rpc_task *task)
1742 task->tk_action = call_refreshresult;
1743 task->tk_status = 0;
1744 task->tk_client->cl_stats->rpcauthrefresh++;
1745 rpcauth_refreshcred(task);
1749 * 2a. Process the results of a credential refresh
1752 call_refreshresult(struct rpc_task *task)
1754 int status = task->tk_status;
1756 task->tk_status = 0;
1757 task->tk_action = call_refresh;
1760 if (rpcauth_uptodatecred(task)) {
1761 task->tk_action = call_allocate;
1764 /* Use rate-limiting and a max number of retries if refresh
1765 * had status 0 but failed to update the cred.
1769 rpc_delay(task, 3*HZ);
1775 if (!task->tk_cred_retry)
1777 task->tk_cred_retry--;
1778 trace_rpc_retry_refresh_status(task);
1781 rpc_delay(task, HZ >> 4);
1784 trace_rpc_refresh_status(task);
1785 rpc_call_rpcerror(task, status);
1789 * 2b. Allocate the buffer. For details, see sched.c:rpc_malloc.
1790 * (Note: buffer memory is freed in xprt_release).
1793 call_allocate(struct rpc_task *task)
1795 const struct rpc_auth *auth = task->tk_rqstp->rq_cred->cr_auth;
1796 struct rpc_rqst *req = task->tk_rqstp;
1797 struct rpc_xprt *xprt = req->rq_xprt;
1798 const struct rpc_procinfo *proc = task->tk_msg.rpc_proc;
1801 task->tk_status = 0;
1802 task->tk_action = call_encode;
1807 if (proc->p_proc != 0) {
1808 BUG_ON(proc->p_arglen == 0);
1809 if (proc->p_decode != NULL)
1810 BUG_ON(proc->p_replen == 0);
1814 * Calculate the size (in quads) of the RPC call
1815 * and reply headers, and convert both values
1818 req->rq_callsize = RPC_CALLHDRSIZE + (auth->au_cslack << 1) +
1820 req->rq_callsize <<= 2;
1822 * Note: the reply buffer must at minimum allocate enough space
1823 * for the 'struct accepted_reply' from RFC5531.
1825 req->rq_rcvsize = RPC_REPHDRSIZE + auth->au_rslack + \
1826 max_t(size_t, proc->p_replen, 2);
1827 req->rq_rcvsize <<= 2;
1829 status = xprt->ops->buf_alloc(task);
1830 trace_rpc_buf_alloc(task, status);
1833 if (status != -ENOMEM) {
1834 rpc_call_rpcerror(task, status);
1838 if (RPC_IS_ASYNC(task) || !fatal_signal_pending(current)) {
1839 task->tk_action = call_allocate;
1840 rpc_delay(task, HZ>>4);
1844 rpc_call_rpcerror(task, -ERESTARTSYS);
1848 rpc_task_need_encode(struct rpc_task *task)
1850 return test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate) == 0 &&
1851 (!(task->tk_flags & RPC_TASK_SENT) ||
1852 !(task->tk_flags & RPC_TASK_NO_RETRANS_TIMEOUT) ||
1853 xprt_request_need_retransmit(task));
1857 rpc_xdr_encode(struct rpc_task *task)
1859 struct rpc_rqst *req = task->tk_rqstp;
1860 struct xdr_stream xdr;
1862 xdr_buf_init(&req->rq_snd_buf,
1865 xdr_buf_init(&req->rq_rcv_buf,
1869 req->rq_reply_bytes_recvd = 0;
1870 req->rq_snd_buf.head[0].iov_len = 0;
1871 xdr_init_encode(&xdr, &req->rq_snd_buf,
1872 req->rq_snd_buf.head[0].iov_base, req);
1873 if (rpc_encode_header(task, &xdr))
1876 task->tk_status = rpcauth_wrap_req(task, &xdr);
1880 * 3. Encode arguments of an RPC call
1883 call_encode(struct rpc_task *task)
1885 if (!rpc_task_need_encode(task))
1888 /* Dequeue task from the receive queue while we're encoding */
1889 xprt_request_dequeue_xprt(task);
1890 /* Encode here so that rpcsec_gss can use correct sequence number. */
1891 rpc_xdr_encode(task);
1892 /* Add task to reply queue before transmission to avoid races */
1893 if (task->tk_status == 0 && rpc_reply_expected(task))
1894 task->tk_status = xprt_request_enqueue_receive(task);
1895 /* Did the encode result in an error condition? */
1896 if (task->tk_status != 0) {
1897 /* Was the error nonfatal? */
1898 switch (task->tk_status) {
1901 rpc_delay(task, HZ >> 4);
1904 if (!task->tk_cred_retry) {
1905 rpc_call_rpcerror(task, task->tk_status);
1907 task->tk_action = call_refresh;
1908 task->tk_cred_retry--;
1909 trace_rpc_retry_refresh_status(task);
1913 rpc_call_rpcerror(task, task->tk_status);
1918 xprt_request_enqueue_transmit(task);
1920 task->tk_action = call_transmit;
1921 /* Check that the connection is OK */
1922 if (!xprt_bound(task->tk_xprt))
1923 task->tk_action = call_bind;
1924 else if (!xprt_connected(task->tk_xprt))
1925 task->tk_action = call_connect;
1929 * Helpers to check if the task was already transmitted, and
1930 * to take action when that is the case.
1933 rpc_task_transmitted(struct rpc_task *task)
1935 return !test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate);
1939 rpc_task_handle_transmitted(struct rpc_task *task)
1941 xprt_end_transmit(task);
1942 task->tk_action = call_transmit_status;
1946 * 4. Get the server port number if not yet set
1949 call_bind(struct rpc_task *task)
1951 struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
1953 if (rpc_task_transmitted(task)) {
1954 rpc_task_handle_transmitted(task);
1958 if (xprt_bound(xprt)) {
1959 task->tk_action = call_connect;
1963 task->tk_action = call_bind_status;
1964 if (!xprt_prepare_transmit(task))
1967 xprt->ops->rpcbind(task);
1971 * 4a. Sort out bind result
1974 call_bind_status(struct rpc_task *task)
1976 struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
1979 if (rpc_task_transmitted(task)) {
1980 rpc_task_handle_transmitted(task);
1984 if (task->tk_status >= 0)
1986 if (xprt_bound(xprt)) {
1987 task->tk_status = 0;
1991 switch (task->tk_status) {
1993 rpc_delay(task, HZ >> 2);
1996 trace_rpcb_prog_unavail_err(task);
1997 /* fail immediately if this is an RPC ping */
1998 if (task->tk_msg.rpc_proc->p_proc == 0) {
1999 status = -EOPNOTSUPP;
2002 if (task->tk_rebind_retry == 0)
2004 task->tk_rebind_retry--;
2005 rpc_delay(task, 3*HZ);
2008 rpc_delay(task, HZ >> 2);
2013 trace_rpcb_timeout_err(task);
2016 /* server doesn't support any rpcbind version we know of */
2017 trace_rpcb_bind_version_err(task);
2019 case -EPROTONOSUPPORT:
2020 trace_rpcb_bind_version_err(task);
2022 case -ECONNREFUSED: /* connection problems */
2031 trace_rpcb_unreachable_err(task);
2032 if (!RPC_IS_SOFTCONN(task)) {
2033 rpc_delay(task, 5*HZ);
2036 status = task->tk_status;
2039 trace_rpcb_unrecognized_err(task);
2042 rpc_call_rpcerror(task, status);
2045 task->tk_action = call_connect;
2048 task->tk_status = 0;
2049 task->tk_action = call_bind;
2050 rpc_check_timeout(task);
2054 * 4b. Connect to the RPC server
2057 call_connect(struct rpc_task *task)
2059 struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
2061 if (rpc_task_transmitted(task)) {
2062 rpc_task_handle_transmitted(task);
2066 if (xprt_connected(xprt)) {
2067 task->tk_action = call_transmit;
2071 task->tk_action = call_connect_status;
2072 if (task->tk_status < 0)
2074 if (task->tk_flags & RPC_TASK_NOCONNECT) {
2075 rpc_call_rpcerror(task, -ENOTCONN);
2078 if (!xprt_prepare_transmit(task))
2084 * 4c. Sort out connect result
2087 call_connect_status(struct rpc_task *task)
2089 struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
2090 struct rpc_clnt *clnt = task->tk_client;
2091 int status = task->tk_status;
2093 if (rpc_task_transmitted(task)) {
2094 rpc_task_handle_transmitted(task);
2098 trace_rpc_connect_status(task);
2100 if (task->tk_status == 0) {
2101 clnt->cl_stats->netreconn++;
2104 if (xprt_connected(xprt)) {
2105 task->tk_status = 0;
2109 task->tk_status = 0;
2112 /* A positive refusal suggests a rebind is needed. */
2113 if (RPC_IS_SOFTCONN(task))
2115 if (clnt->cl_autobind) {
2116 rpc_force_rebind(clnt);
2127 xprt_conditional_disconnect(task->tk_rqstp->rq_xprt,
2128 task->tk_rqstp->rq_connect_cookie);
2129 if (RPC_IS_SOFTCONN(task))
2131 /* retry with existing socket, after a delay */
2132 rpc_delay(task, 3*HZ);
2138 if (!(task->tk_flags & RPC_TASK_NO_ROUND_ROBIN) &&
2139 (task->tk_flags & RPC_TASK_MOVEABLE) &&
2140 test_bit(XPRT_REMOVE, &xprt->state)) {
2141 struct rpc_xprt *saved = task->tk_xprt;
2142 struct rpc_xprt_switch *xps;
2145 xps = xprt_switch_get(rcu_dereference(clnt->cl_xpi.xpi_xpswitch));
2147 if (xps->xps_nxprts > 1) {
2151 value = atomic_long_dec_return(&xprt->queuelen);
2153 rpc_xprt_switch_remove_xprt(xps, saved,
2156 task->tk_xprt = NULL;
2157 task->tk_action = call_start;
2159 xprt_switch_put(xps);
2165 rpc_delay(task, HZ >> 2);
2168 rpc_call_rpcerror(task, status);
2171 task->tk_action = call_transmit;
2174 /* Check for timeouts before looping back to call_bind */
2175 task->tk_action = call_bind;
2176 rpc_check_timeout(task);
2180 * 5. Transmit the RPC request, and wait for reply
2183 call_transmit(struct rpc_task *task)
2185 if (rpc_task_transmitted(task)) {
2186 rpc_task_handle_transmitted(task);
2190 task->tk_action = call_transmit_status;
2191 if (!xprt_prepare_transmit(task))
2193 task->tk_status = 0;
2194 if (test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate)) {
2195 if (!xprt_connected(task->tk_xprt)) {
2196 task->tk_status = -ENOTCONN;
2199 xprt_transmit(task);
2201 xprt_end_transmit(task);
2205 * 5a. Handle cleanup after a transmission
2208 call_transmit_status(struct rpc_task *task)
2210 task->tk_action = call_status;
2213 * Common case: success. Force the compiler to put this
2216 if (rpc_task_transmitted(task)) {
2217 task->tk_status = 0;
2218 xprt_request_wait_receive(task);
2222 switch (task->tk_status) {
2226 task->tk_status = 0;
2227 task->tk_action = call_encode;
2230 * Special cases: if we've been waiting on the
2231 * socket's write_space() callback, or if the
2232 * socket just returned a connection error,
2233 * then hold onto the transport lock.
2237 rpc_delay(task, HZ>>2);
2241 task->tk_action = call_transmit;
2242 task->tk_status = 0;
2250 if (RPC_IS_SOFTCONN(task)) {
2251 if (!task->tk_msg.rpc_proc->p_proc)
2252 trace_xprt_ping(task->tk_xprt,
2254 rpc_call_rpcerror(task, task->tk_status);
2263 task->tk_action = call_bind;
2264 task->tk_status = 0;
2267 rpc_check_timeout(task);
2270 #if defined(CONFIG_SUNRPC_BACKCHANNEL)
2271 static void call_bc_transmit(struct rpc_task *task);
2272 static void call_bc_transmit_status(struct rpc_task *task);
2275 call_bc_encode(struct rpc_task *task)
2277 xprt_request_enqueue_transmit(task);
2278 task->tk_action = call_bc_transmit;
2282 * 5b. Send the backchannel RPC reply. On error, drop the reply. In
2283 * addition, disconnect on connectivity errors.
2286 call_bc_transmit(struct rpc_task *task)
2288 task->tk_action = call_bc_transmit_status;
2289 if (test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate)) {
2290 if (!xprt_prepare_transmit(task))
2292 task->tk_status = 0;
2293 xprt_transmit(task);
2295 xprt_end_transmit(task);
2299 call_bc_transmit_status(struct rpc_task *task)
2301 struct rpc_rqst *req = task->tk_rqstp;
2303 if (rpc_task_transmitted(task))
2304 task->tk_status = 0;
2306 switch (task->tk_status) {
2321 rpc_delay(task, HZ>>2);
2325 task->tk_status = 0;
2326 task->tk_action = call_bc_transmit;
2330 * Problem reaching the server. Disconnect and let the
2331 * forechannel reestablish the connection. The server will
2332 * have to retransmit the backchannel request and we'll
2333 * reprocess it. Since these ops are idempotent, there's no
2334 * need to cache our reply at this time.
2336 printk(KERN_NOTICE "RPC: Could not send backchannel reply "
2337 "error: %d\n", task->tk_status);
2338 xprt_conditional_disconnect(req->rq_xprt,
2339 req->rq_connect_cookie);
2343 * We were unable to reply and will have to drop the
2344 * request. The server should reconnect and retransmit.
2346 printk(KERN_NOTICE "RPC: Could not send backchannel reply "
2347 "error: %d\n", task->tk_status);
2350 task->tk_action = rpc_exit_task;
2352 #endif /* CONFIG_SUNRPC_BACKCHANNEL */
2355 * 6. Sort out the RPC call status
2358 call_status(struct rpc_task *task)
2360 struct rpc_clnt *clnt = task->tk_client;
2363 if (!task->tk_msg.rpc_proc->p_proc)
2364 trace_xprt_ping(task->tk_xprt, task->tk_status);
2366 status = task->tk_status;
2368 task->tk_action = call_decode;
2372 trace_rpc_call_status(task);
2373 task->tk_status = 0;
2380 if (RPC_IS_SOFTCONN(task))
2383 * Delay any retries for 3 seconds, then handle as if it
2386 rpc_delay(task, 3*HZ);
2394 rpc_force_rebind(clnt);
2397 rpc_delay(task, 3*HZ);
2405 rpc_delay(task, HZ>>2);
2408 /* shutdown or soft timeout */
2411 if (clnt->cl_chatty)
2412 printk("%s: RPC call returned error %d\n",
2413 clnt->cl_program->name, -status);
2416 task->tk_action = call_encode;
2417 if (status != -ECONNRESET && status != -ECONNABORTED)
2418 rpc_check_timeout(task);
2421 rpc_call_rpcerror(task, status);
2425 rpc_check_connected(const struct rpc_rqst *req)
2427 /* No allocated request or transport? return true */
2428 if (!req || !req->rq_xprt)
2430 return xprt_connected(req->rq_xprt);
2434 rpc_check_timeout(struct rpc_task *task)
2436 struct rpc_clnt *clnt = task->tk_client;
2438 if (RPC_SIGNALLED(task)) {
2439 rpc_call_rpcerror(task, -ERESTARTSYS);
2443 if (xprt_adjust_timeout(task->tk_rqstp) == 0)
2446 trace_rpc_timeout_status(task);
2447 task->tk_timeouts++;
2449 if (RPC_IS_SOFTCONN(task) && !rpc_check_connected(task->tk_rqstp)) {
2450 rpc_call_rpcerror(task, -ETIMEDOUT);
2454 if (RPC_IS_SOFT(task)) {
2456 * Once a "no retrans timeout" soft tasks (a.k.a NFSv4) has
2457 * been sent, it should time out only if the transport
2458 * connection gets terminally broken.
2460 if ((task->tk_flags & RPC_TASK_NO_RETRANS_TIMEOUT) &&
2461 rpc_check_connected(task->tk_rqstp))
2464 if (clnt->cl_chatty) {
2465 pr_notice_ratelimited(
2466 "%s: server %s not responding, timed out\n",
2467 clnt->cl_program->name,
2468 task->tk_xprt->servername);
2470 if (task->tk_flags & RPC_TASK_TIMEOUT)
2471 rpc_call_rpcerror(task, -ETIMEDOUT);
2473 __rpc_call_rpcerror(task, -EIO, -ETIMEDOUT);
2477 if (!(task->tk_flags & RPC_CALL_MAJORSEEN)) {
2478 task->tk_flags |= RPC_CALL_MAJORSEEN;
2479 if (clnt->cl_chatty) {
2480 pr_notice_ratelimited(
2481 "%s: server %s not responding, still trying\n",
2482 clnt->cl_program->name,
2483 task->tk_xprt->servername);
2486 rpc_force_rebind(clnt);
2488 * Did our request time out due to an RPCSEC_GSS out-of-sequence
2489 * event? RFC2203 requires the server to drop all such requests.
2491 rpcauth_invalcred(task);
2495 * 7. Decode the RPC reply
2498 call_decode(struct rpc_task *task)
2500 struct rpc_clnt *clnt = task->tk_client;
2501 struct rpc_rqst *req = task->tk_rqstp;
2502 struct xdr_stream xdr;
2505 if (!task->tk_msg.rpc_proc->p_decode) {
2506 task->tk_action = rpc_exit_task;
2510 if (task->tk_flags & RPC_CALL_MAJORSEEN) {
2511 if (clnt->cl_chatty) {
2512 pr_notice_ratelimited("%s: server %s OK\n",
2513 clnt->cl_program->name,
2514 task->tk_xprt->servername);
2516 task->tk_flags &= ~RPC_CALL_MAJORSEEN;
2520 * Did we ever call xprt_complete_rqst()? If not, we should assume
2521 * the message is incomplete.
2524 if (!req->rq_reply_bytes_recvd)
2527 /* Ensure that we see all writes made by xprt_complete_rqst()
2528 * before it changed req->rq_reply_bytes_recvd.
2532 req->rq_rcv_buf.len = req->rq_private_buf.len;
2533 trace_rpc_xdr_recvfrom(task, &req->rq_rcv_buf);
2535 /* Check that the softirq receive buffer is valid */
2536 WARN_ON(memcmp(&req->rq_rcv_buf, &req->rq_private_buf,
2537 sizeof(req->rq_rcv_buf)) != 0);
2539 xdr_init_decode(&xdr, &req->rq_rcv_buf,
2540 req->rq_rcv_buf.head[0].iov_base, req);
2541 err = rpc_decode_header(task, &xdr);
2545 task->tk_action = rpc_exit_task;
2546 task->tk_status = rpcauth_unwrap_resp(task, &xdr);
2549 task->tk_status = 0;
2550 if (task->tk_client->cl_discrtry)
2551 xprt_conditional_disconnect(req->rq_xprt,
2552 req->rq_connect_cookie);
2553 task->tk_action = call_encode;
2554 rpc_check_timeout(task);
2557 task->tk_action = call_reserve;
2558 rpc_check_timeout(task);
2559 rpcauth_invalcred(task);
2560 /* Ensure we obtain a new XID if we retry! */
2566 rpc_encode_header(struct rpc_task *task, struct xdr_stream *xdr)
2568 struct rpc_clnt *clnt = task->tk_client;
2569 struct rpc_rqst *req = task->tk_rqstp;
2574 p = xdr_reserve_space(xdr, RPC_CALLHDRSIZE << 2);
2579 *p++ = cpu_to_be32(RPC_VERSION);
2580 *p++ = cpu_to_be32(clnt->cl_prog);
2581 *p++ = cpu_to_be32(clnt->cl_vers);
2582 *p = cpu_to_be32(task->tk_msg.rpc_proc->p_proc);
2584 error = rpcauth_marshcred(task, xdr);
2589 trace_rpc_bad_callhdr(task);
2590 rpc_call_rpcerror(task, error);
2595 rpc_decode_header(struct rpc_task *task, struct xdr_stream *xdr)
2597 struct rpc_clnt *clnt = task->tk_client;
2601 /* RFC-1014 says that the representation of XDR data must be a
2602 * multiple of four bytes
2603 * - if it isn't pointer subtraction in the NFS client may give
2606 if (task->tk_rqstp->rq_rcv_buf.len & 3)
2607 goto out_unparsable;
2609 p = xdr_inline_decode(xdr, 3 * sizeof(*p));
2611 goto out_unparsable;
2613 if (*p++ != rpc_reply)
2614 goto out_unparsable;
2615 if (*p++ != rpc_msg_accepted)
2616 goto out_msg_denied;
2618 error = rpcauth_checkverf(task, xdr);
2622 p = xdr_inline_decode(xdr, sizeof(*p));
2624 goto out_unparsable;
2628 case rpc_prog_unavail:
2629 trace_rpc__prog_unavail(task);
2630 error = -EPFNOSUPPORT;
2632 case rpc_prog_mismatch:
2633 trace_rpc__prog_mismatch(task);
2634 error = -EPROTONOSUPPORT;
2636 case rpc_proc_unavail:
2637 trace_rpc__proc_unavail(task);
2638 error = -EOPNOTSUPP;
2640 case rpc_garbage_args:
2641 case rpc_system_err:
2642 trace_rpc__garbage_args(task);
2646 goto out_unparsable;
2650 clnt->cl_stats->rpcgarbage++;
2651 if (task->tk_garb_retry) {
2652 task->tk_garb_retry--;
2653 task->tk_action = call_encode;
2657 rpc_call_rpcerror(task, error);
2661 trace_rpc__unparsable(task);
2666 trace_rpc_bad_verifier(task);
2671 p = xdr_inline_decode(xdr, sizeof(*p));
2673 goto out_unparsable;
2675 case rpc_auth_error:
2678 trace_rpc__mismatch(task);
2679 error = -EPROTONOSUPPORT;
2682 goto out_unparsable;
2685 p = xdr_inline_decode(xdr, sizeof(*p));
2687 goto out_unparsable;
2689 case rpc_autherr_rejectedcred:
2690 case rpc_autherr_rejectedverf:
2691 case rpcsec_gsserr_credproblem:
2692 case rpcsec_gsserr_ctxproblem:
2693 if (!task->tk_cred_retry)
2695 task->tk_cred_retry--;
2696 trace_rpc__stale_creds(task);
2697 return -EKEYREJECTED;
2698 case rpc_autherr_badcred:
2699 case rpc_autherr_badverf:
2700 /* possibly garbled cred/verf? */
2701 if (!task->tk_garb_retry)
2703 task->tk_garb_retry--;
2704 trace_rpc__bad_creds(task);
2705 task->tk_action = call_encode;
2707 case rpc_autherr_tooweak:
2708 trace_rpc__auth_tooweak(task);
2709 pr_warn("RPC: server %s requires stronger authentication.\n",
2710 task->tk_xprt->servername);
2713 goto out_unparsable;
2718 static void rpcproc_encode_null(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
2723 static int rpcproc_decode_null(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
2729 static const struct rpc_procinfo rpcproc_null = {
2730 .p_encode = rpcproc_encode_null,
2731 .p_decode = rpcproc_decode_null,
2734 static const struct rpc_procinfo rpcproc_null_noreply = {
2735 .p_encode = rpcproc_encode_null,
2739 rpc_null_call_prepare(struct rpc_task *task, void *data)
2741 task->tk_flags &= ~RPC_TASK_NO_RETRANS_TIMEOUT;
2742 rpc_call_start(task);
2745 static const struct rpc_call_ops rpc_null_ops = {
2746 .rpc_call_prepare = rpc_null_call_prepare,
2747 .rpc_call_done = rpc_default_callback,
2751 struct rpc_task *rpc_call_null_helper(struct rpc_clnt *clnt,
2752 struct rpc_xprt *xprt, struct rpc_cred *cred, int flags,
2753 const struct rpc_call_ops *ops, void *data)
2755 struct rpc_message msg = {
2756 .rpc_proc = &rpcproc_null,
2758 struct rpc_task_setup task_setup_data = {
2761 .rpc_message = &msg,
2762 .rpc_op_cred = cred,
2763 .callback_ops = ops ?: &rpc_null_ops,
2764 .callback_data = data,
2765 .flags = flags | RPC_TASK_SOFT | RPC_TASK_SOFTCONN |
2769 return rpc_run_task(&task_setup_data);
2772 struct rpc_task *rpc_call_null(struct rpc_clnt *clnt, struct rpc_cred *cred, int flags)
2774 return rpc_call_null_helper(clnt, NULL, cred, flags, NULL, NULL);
2776 EXPORT_SYMBOL_GPL(rpc_call_null);
2778 static int rpc_ping(struct rpc_clnt *clnt)
2780 struct rpc_task *task;
2783 task = rpc_call_null_helper(clnt, NULL, NULL, 0, NULL, NULL);
2785 return PTR_ERR(task);
2786 status = task->tk_status;
2791 static int rpc_ping_noreply(struct rpc_clnt *clnt)
2793 struct rpc_message msg = {
2794 .rpc_proc = &rpcproc_null_noreply,
2796 struct rpc_task_setup task_setup_data = {
2798 .rpc_message = &msg,
2799 .callback_ops = &rpc_null_ops,
2800 .flags = RPC_TASK_SOFT | RPC_TASK_SOFTCONN | RPC_TASK_NULLCREDS,
2802 struct rpc_task *task;
2805 task = rpc_run_task(&task_setup_data);
2807 return PTR_ERR(task);
2808 status = task->tk_status;
2813 struct rpc_cb_add_xprt_calldata {
2814 struct rpc_xprt_switch *xps;
2815 struct rpc_xprt *xprt;
2818 static void rpc_cb_add_xprt_done(struct rpc_task *task, void *calldata)
2820 struct rpc_cb_add_xprt_calldata *data = calldata;
2822 if (task->tk_status == 0)
2823 rpc_xprt_switch_add_xprt(data->xps, data->xprt);
2826 static void rpc_cb_add_xprt_release(void *calldata)
2828 struct rpc_cb_add_xprt_calldata *data = calldata;
2830 xprt_put(data->xprt);
2831 xprt_switch_put(data->xps);
2835 static const struct rpc_call_ops rpc_cb_add_xprt_call_ops = {
2836 .rpc_call_prepare = rpc_null_call_prepare,
2837 .rpc_call_done = rpc_cb_add_xprt_done,
2838 .rpc_release = rpc_cb_add_xprt_release,
2842 * rpc_clnt_test_and_add_xprt - Test and add a new transport to a rpc_clnt
2843 * @clnt: pointer to struct rpc_clnt
2844 * @xps: pointer to struct rpc_xprt_switch,
2845 * @xprt: pointer struct rpc_xprt
2848 int rpc_clnt_test_and_add_xprt(struct rpc_clnt *clnt,
2849 struct rpc_xprt_switch *xps, struct rpc_xprt *xprt,
2852 struct rpc_cb_add_xprt_calldata *data;
2853 struct rpc_task *task;
2855 if (xps->xps_nunique_destaddr_xprts + 1 > clnt->cl_max_connect) {
2857 pr_warn("SUNRPC: reached max allowed number (%d) did not add "
2858 "transport to server: %s\n", clnt->cl_max_connect,
2859 rpc_peeraddr2str(clnt, RPC_DISPLAY_ADDR));
2864 data = kmalloc(sizeof(*data), GFP_KERNEL);
2867 data->xps = xprt_switch_get(xps);
2868 data->xprt = xprt_get(xprt);
2869 if (rpc_xprt_switch_has_addr(data->xps, (struct sockaddr *)&xprt->addr)) {
2870 rpc_cb_add_xprt_release(data);
2874 task = rpc_call_null_helper(clnt, xprt, NULL, RPC_TASK_ASYNC,
2875 &rpc_cb_add_xprt_call_ops, data);
2877 return PTR_ERR(task);
2879 data->xps->xps_nunique_destaddr_xprts++;
2884 EXPORT_SYMBOL_GPL(rpc_clnt_test_and_add_xprt);
2886 static int rpc_clnt_add_xprt_helper(struct rpc_clnt *clnt,
2887 struct rpc_xprt *xprt,
2888 struct rpc_add_xprt_test *data)
2890 struct rpc_task *task;
2891 int status = -EADDRINUSE;
2893 /* Test the connection */
2894 task = rpc_call_null_helper(clnt, xprt, NULL, 0, NULL, NULL);
2896 return PTR_ERR(task);
2898 status = task->tk_status;
2904 /* rpc_xprt_switch and rpc_xprt are deferrenced by add_xprt_test() */
2905 data->add_xprt_test(clnt, xprt, data->data);
2911 * rpc_clnt_setup_test_and_add_xprt()
2913 * This is an rpc_clnt_add_xprt setup() function which returns 1 so:
2914 * 1) caller of the test function must dereference the rpc_xprt_switch
2916 * 2) test function must call rpc_xprt_switch_add_xprt, usually in
2917 * the rpc_call_done routine.
2919 * Upon success (return of 1), the test function adds the new
2920 * transport to the rpc_clnt xprt switch
2922 * @clnt: struct rpc_clnt to get the new transport
2923 * @xps: the rpc_xprt_switch to hold the new transport
2924 * @xprt: the rpc_xprt to test
2925 * @data: a struct rpc_add_xprt_test pointer that holds the test function
2926 * and test function call data
2928 int rpc_clnt_setup_test_and_add_xprt(struct rpc_clnt *clnt,
2929 struct rpc_xprt_switch *xps,
2930 struct rpc_xprt *xprt,
2933 int status = -EADDRINUSE;
2935 xprt = xprt_get(xprt);
2936 xprt_switch_get(xps);
2938 if (rpc_xprt_switch_has_addr(xps, (struct sockaddr *)&xprt->addr))
2941 status = rpc_clnt_add_xprt_helper(clnt, xprt, data);
2948 xprt_switch_put(xps);
2950 pr_info("RPC: rpc_clnt_test_xprt failed: %d addr %s not "
2952 xprt->address_strings[RPC_DISPLAY_ADDR]);
2953 /* so that rpc_clnt_add_xprt does not call rpc_xprt_switch_add_xprt */
2956 EXPORT_SYMBOL_GPL(rpc_clnt_setup_test_and_add_xprt);
2959 * rpc_clnt_add_xprt - Add a new transport to a rpc_clnt
2960 * @clnt: pointer to struct rpc_clnt
2961 * @xprtargs: pointer to struct xprt_create
2962 * @setup: callback to test and/or set up the connection
2963 * @data: pointer to setup function data
2965 * Creates a new transport using the parameters set in args and
2967 * If ping is set, then test that connectivity succeeds before
2968 * adding the new transport.
2971 int rpc_clnt_add_xprt(struct rpc_clnt *clnt,
2972 struct xprt_create *xprtargs,
2973 int (*setup)(struct rpc_clnt *,
2974 struct rpc_xprt_switch *,
2979 struct rpc_xprt_switch *xps;
2980 struct rpc_xprt *xprt;
2981 unsigned long connect_timeout;
2982 unsigned long reconnect_timeout;
2983 unsigned char resvport, reuseport;
2987 xps = xprt_switch_get(rcu_dereference(clnt->cl_xpi.xpi_xpswitch));
2988 xprt = xprt_iter_xprt(&clnt->cl_xpi);
2989 if (xps == NULL || xprt == NULL) {
2991 xprt_switch_put(xps);
2994 resvport = xprt->resvport;
2995 reuseport = xprt->reuseport;
2996 connect_timeout = xprt->connect_timeout;
2997 reconnect_timeout = xprt->max_reconnect_timeout;
2998 ident = xprt->xprt_class->ident;
3001 if (!xprtargs->ident)
3002 xprtargs->ident = ident;
3003 xprt = xprt_create_transport(xprtargs);
3005 ret = PTR_ERR(xprt);
3006 goto out_put_switch;
3008 xprt->resvport = resvport;
3009 xprt->reuseport = reuseport;
3010 if (xprt->ops->set_connect_timeout != NULL)
3011 xprt->ops->set_connect_timeout(xprt,
3015 rpc_xprt_switch_set_roundrobin(xps);
3017 ret = setup(clnt, xps, xprt, data);
3021 rpc_xprt_switch_add_xprt(xps, xprt);
3025 xprt_switch_put(xps);
3028 EXPORT_SYMBOL_GPL(rpc_clnt_add_xprt);
3030 static int rpc_xprt_probe_trunked(struct rpc_clnt *clnt,
3031 struct rpc_xprt *xprt,
3032 struct rpc_add_xprt_test *data)
3034 struct rpc_xprt_switch *xps;
3035 struct rpc_xprt *main_xprt;
3041 main_xprt = xprt_get(rcu_dereference(clnt->cl_xprt));
3042 xps = xprt_switch_get(rcu_dereference(clnt->cl_xpi.xpi_xpswitch));
3043 status = rpc_cmp_addr_port((struct sockaddr *)&xprt->addr,
3044 (struct sockaddr *)&main_xprt->addr);
3046 xprt_put(main_xprt);
3047 if (status || !test_bit(XPRT_OFFLINE, &xprt->state))
3050 status = rpc_clnt_add_xprt_helper(clnt, xprt, data);
3053 xprt_switch_put(xps);
3057 /* rpc_clnt_probe_trunked_xprt -- probe offlined transport for session trunking
3058 * @clnt rpc_clnt structure
3060 * For each offlined transport found in the rpc_clnt structure call
3061 * the function rpc_xprt_probe_trunked() which will determine if this
3062 * transport still belongs to the trunking group.
3064 void rpc_clnt_probe_trunked_xprts(struct rpc_clnt *clnt,
3065 struct rpc_add_xprt_test *data)
3067 struct rpc_xprt_iter xpi;
3070 ret = rpc_clnt_xprt_iter_offline_init(clnt, &xpi);
3074 struct rpc_xprt *xprt = xprt_iter_get_next(&xpi);
3078 ret = rpc_xprt_probe_trunked(clnt, xprt, data);
3082 xprt_iter_rewind(&xpi);
3084 xprt_iter_destroy(&xpi);
3086 EXPORT_SYMBOL_GPL(rpc_clnt_probe_trunked_xprts);
3088 static int rpc_xprt_offline(struct rpc_clnt *clnt,
3089 struct rpc_xprt *xprt,
3092 struct rpc_xprt *main_xprt;
3093 struct rpc_xprt_switch *xps;
3099 main_xprt = xprt_get(rcu_dereference(clnt->cl_xprt));
3100 xps = xprt_switch_get(rcu_dereference(clnt->cl_xpi.xpi_xpswitch));
3101 err = rpc_cmp_addr_port((struct sockaddr *)&xprt->addr,
3102 (struct sockaddr *)&main_xprt->addr);
3104 xprt_put(main_xprt);
3108 if (wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_KILLABLE)) {
3112 xprt_set_offline_locked(xprt, xps);
3114 xprt_release_write(xprt, NULL);
3117 xprt_switch_put(xps);
3121 /* rpc_clnt_manage_trunked_xprts -- offline trunked transports
3122 * @clnt rpc_clnt structure
3124 * For each active transport found in the rpc_clnt structure call
3125 * the function rpc_xprt_offline() which will identify trunked transports
3126 * and will mark them offline.
3128 void rpc_clnt_manage_trunked_xprts(struct rpc_clnt *clnt)
3130 rpc_clnt_iterate_for_each_xprt(clnt, rpc_xprt_offline, NULL);
3132 EXPORT_SYMBOL_GPL(rpc_clnt_manage_trunked_xprts);
3134 struct connect_timeout_data {
3135 unsigned long connect_timeout;
3136 unsigned long reconnect_timeout;
3140 rpc_xprt_set_connect_timeout(struct rpc_clnt *clnt,
3141 struct rpc_xprt *xprt,
3144 struct connect_timeout_data *timeo = data;
3146 if (xprt->ops->set_connect_timeout)
3147 xprt->ops->set_connect_timeout(xprt,
3148 timeo->connect_timeout,
3149 timeo->reconnect_timeout);
3154 rpc_set_connect_timeout(struct rpc_clnt *clnt,
3155 unsigned long connect_timeout,
3156 unsigned long reconnect_timeout)
3158 struct connect_timeout_data timeout = {
3159 .connect_timeout = connect_timeout,
3160 .reconnect_timeout = reconnect_timeout,
3162 rpc_clnt_iterate_for_each_xprt(clnt,
3163 rpc_xprt_set_connect_timeout,
3166 EXPORT_SYMBOL_GPL(rpc_set_connect_timeout);
3168 void rpc_clnt_xprt_switch_put(struct rpc_clnt *clnt)
3171 xprt_switch_put(rcu_dereference(clnt->cl_xpi.xpi_xpswitch));
3174 EXPORT_SYMBOL_GPL(rpc_clnt_xprt_switch_put);
3176 void rpc_clnt_xprt_set_online(struct rpc_clnt *clnt, struct rpc_xprt *xprt)
3178 struct rpc_xprt_switch *xps;
3181 xps = rcu_dereference(clnt->cl_xpi.xpi_xpswitch);
3183 xprt_set_online_locked(xprt, xps);
3186 void rpc_clnt_xprt_switch_add_xprt(struct rpc_clnt *clnt, struct rpc_xprt *xprt)
3188 if (rpc_clnt_xprt_switch_has_addr(clnt,
3189 (const struct sockaddr *)&xprt->addr)) {
3190 return rpc_clnt_xprt_set_online(clnt, xprt);
3193 rpc_xprt_switch_add_xprt(rcu_dereference(clnt->cl_xpi.xpi_xpswitch),
3197 EXPORT_SYMBOL_GPL(rpc_clnt_xprt_switch_add_xprt);
3199 void rpc_clnt_xprt_switch_remove_xprt(struct rpc_clnt *clnt, struct rpc_xprt *xprt)
3201 struct rpc_xprt_switch *xps;
3204 xps = rcu_dereference(clnt->cl_xpi.xpi_xpswitch);
3205 rpc_xprt_switch_remove_xprt(rcu_dereference(clnt->cl_xpi.xpi_xpswitch),
3207 xps->xps_nunique_destaddr_xprts--;
3210 EXPORT_SYMBOL_GPL(rpc_clnt_xprt_switch_remove_xprt);
3212 bool rpc_clnt_xprt_switch_has_addr(struct rpc_clnt *clnt,
3213 const struct sockaddr *sap)
3215 struct rpc_xprt_switch *xps;
3219 xps = rcu_dereference(clnt->cl_xpi.xpi_xpswitch);
3220 ret = rpc_xprt_switch_has_addr(xps, sap);
3224 EXPORT_SYMBOL_GPL(rpc_clnt_xprt_switch_has_addr);
3226 #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
3227 static void rpc_show_header(void)
3229 printk(KERN_INFO "-pid- flgs status -client- --rqstp- "
3230 "-timeout ---ops--\n");
3233 static void rpc_show_task(const struct rpc_clnt *clnt,
3234 const struct rpc_task *task)
3236 const char *rpc_waitq = "none";
3238 if (RPC_IS_QUEUED(task))
3239 rpc_waitq = rpc_qname(task->tk_waitqueue);
3241 printk(KERN_INFO "%5u %04x %6d %8p %8p %8ld %8p %sv%u %s a:%ps q:%s\n",
3242 task->tk_pid, task->tk_flags, task->tk_status,
3243 clnt, task->tk_rqstp, rpc_task_timeout(task), task->tk_ops,
3244 clnt->cl_program->name, clnt->cl_vers, rpc_proc_name(task),
3245 task->tk_action, rpc_waitq);
3248 void rpc_show_tasks(struct net *net)
3250 struct rpc_clnt *clnt;
3251 struct rpc_task *task;
3253 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
3255 spin_lock(&sn->rpc_client_lock);
3256 list_for_each_entry(clnt, &sn->all_clients, cl_clients) {
3257 spin_lock(&clnt->cl_lock);
3258 list_for_each_entry(task, &clnt->cl_tasks, tk_task) {
3263 rpc_show_task(clnt, task);
3265 spin_unlock(&clnt->cl_lock);
3267 spin_unlock(&sn->rpc_client_lock);
3271 #if IS_ENABLED(CONFIG_SUNRPC_SWAP)
3273 rpc_clnt_swap_activate_callback(struct rpc_clnt *clnt,
3274 struct rpc_xprt *xprt,
3277 return xprt_enable_swap(xprt);
3281 rpc_clnt_swap_activate(struct rpc_clnt *clnt)
3283 while (clnt != clnt->cl_parent)
3284 clnt = clnt->cl_parent;
3285 if (atomic_inc_return(&clnt->cl_swapper) == 1)
3286 return rpc_clnt_iterate_for_each_xprt(clnt,
3287 rpc_clnt_swap_activate_callback, NULL);
3290 EXPORT_SYMBOL_GPL(rpc_clnt_swap_activate);
3293 rpc_clnt_swap_deactivate_callback(struct rpc_clnt *clnt,
3294 struct rpc_xprt *xprt,
3297 xprt_disable_swap(xprt);
3302 rpc_clnt_swap_deactivate(struct rpc_clnt *clnt)
3304 if (atomic_dec_if_positive(&clnt->cl_swapper) == 0)
3305 rpc_clnt_iterate_for_each_xprt(clnt,
3306 rpc_clnt_swap_deactivate_callback, NULL);
3308 EXPORT_SYMBOL_GPL(rpc_clnt_swap_deactivate);
3309 #endif /* CONFIG_SUNRPC_SWAP */