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)
791 struct rpc_xprt_switch *xps;
794 xps = xprt_switch_get(rcu_dereference(clnt->cl_xpi.xpi_xpswitch));
798 xprt_iter_init_listall(xpi, xps);
799 xprt_switch_put(xps);
804 * rpc_clnt_iterate_for_each_xprt - Apply a function to all transports
805 * @clnt: pointer to client
806 * @fn: function to apply
807 * @data: void pointer to function data
809 * Iterates through the list of RPC transports currently attached to the
810 * client and applies the function fn(clnt, xprt, data).
812 * On error, the iteration stops, and the function returns the error value.
814 int rpc_clnt_iterate_for_each_xprt(struct rpc_clnt *clnt,
815 int (*fn)(struct rpc_clnt *, struct rpc_xprt *, void *),
818 struct rpc_xprt_iter xpi;
821 ret = rpc_clnt_xprt_iter_init(clnt, &xpi);
825 struct rpc_xprt *xprt = xprt_iter_get_next(&xpi);
829 ret = fn(clnt, xprt, data);
834 xprt_iter_destroy(&xpi);
837 EXPORT_SYMBOL_GPL(rpc_clnt_iterate_for_each_xprt);
840 * Kill all tasks for the given client.
841 * XXX: kill their descendants as well?
843 void rpc_killall_tasks(struct rpc_clnt *clnt)
845 struct rpc_task *rovr;
848 if (list_empty(&clnt->cl_tasks))
852 * Spin lock all_tasks to prevent changes...
854 trace_rpc_clnt_killall(clnt);
855 spin_lock(&clnt->cl_lock);
856 list_for_each_entry(rovr, &clnt->cl_tasks, tk_task)
857 rpc_signal_task(rovr);
858 spin_unlock(&clnt->cl_lock);
860 EXPORT_SYMBOL_GPL(rpc_killall_tasks);
863 * Properly shut down an RPC client, terminating all outstanding
866 void rpc_shutdown_client(struct rpc_clnt *clnt)
870 trace_rpc_clnt_shutdown(clnt);
872 while (!list_empty(&clnt->cl_tasks)) {
873 rpc_killall_tasks(clnt);
874 wait_event_timeout(destroy_wait,
875 list_empty(&clnt->cl_tasks), 1*HZ);
878 rpc_release_client(clnt);
880 EXPORT_SYMBOL_GPL(rpc_shutdown_client);
885 static void rpc_free_client_work(struct work_struct *work)
887 struct rpc_clnt *clnt = container_of(work, struct rpc_clnt, cl_work);
889 trace_rpc_clnt_free(clnt);
891 /* These might block on processes that might allocate memory,
892 * so they cannot be called in rpciod, so they are handled separately
895 rpc_sysfs_client_destroy(clnt);
896 rpc_clnt_debugfs_unregister(clnt);
898 rpc_clnt_remove_pipedir(clnt);
899 xprt_put(rcu_dereference_raw(clnt->cl_xprt));
904 static struct rpc_clnt *
905 rpc_free_client(struct rpc_clnt *clnt)
907 struct rpc_clnt *parent = NULL;
909 trace_rpc_clnt_release(clnt);
910 if (clnt->cl_parent != clnt)
911 parent = clnt->cl_parent;
912 rpc_unregister_client(clnt);
913 rpc_free_iostats(clnt->cl_metrics);
914 clnt->cl_metrics = NULL;
915 xprt_iter_destroy(&clnt->cl_xpi);
916 put_cred(clnt->cl_cred);
918 INIT_WORK(&clnt->cl_work, rpc_free_client_work);
919 schedule_work(&clnt->cl_work);
926 static struct rpc_clnt *
927 rpc_free_auth(struct rpc_clnt *clnt)
930 * Note: RPCSEC_GSS may need to send NULL RPC calls in order to
931 * release remaining GSS contexts. This mechanism ensures
932 * that it can do so safely.
934 if (clnt->cl_auth != NULL) {
935 rpcauth_release(clnt->cl_auth);
936 clnt->cl_auth = NULL;
938 if (refcount_dec_and_test(&clnt->cl_count))
939 return rpc_free_client(clnt);
944 * Release reference to the RPC client
947 rpc_release_client(struct rpc_clnt *clnt)
950 if (list_empty(&clnt->cl_tasks))
951 wake_up(&destroy_wait);
952 if (refcount_dec_not_one(&clnt->cl_count))
954 clnt = rpc_free_auth(clnt);
955 } while (clnt != NULL);
957 EXPORT_SYMBOL_GPL(rpc_release_client);
960 * rpc_bind_new_program - bind a new RPC program to an existing client
961 * @old: old rpc_client
962 * @program: rpc program to set
963 * @vers: rpc program version
965 * Clones the rpc client and sets up a new RPC program. This is mainly
966 * of use for enabling different RPC programs to share the same transport.
967 * The Sun NFSv2/v3 ACL protocol can do this.
969 struct rpc_clnt *rpc_bind_new_program(struct rpc_clnt *old,
970 const struct rpc_program *program,
973 struct rpc_create_args args = {
975 .prognumber = program->number,
977 .authflavor = old->cl_auth->au_flavor,
978 .cred = old->cl_cred,
980 struct rpc_clnt *clnt;
983 clnt = __rpc_clone_client(&args, old);
986 err = rpc_ping(clnt);
988 rpc_shutdown_client(clnt);
994 EXPORT_SYMBOL_GPL(rpc_bind_new_program);
997 rpc_task_get_xprt(struct rpc_clnt *clnt, struct rpc_xprt *xprt)
999 struct rpc_xprt_switch *xps;
1004 xps = rcu_dereference(clnt->cl_xpi.xpi_xpswitch);
1005 atomic_long_inc(&xps->xps_queuelen);
1007 atomic_long_inc(&xprt->queuelen);
1013 rpc_task_release_xprt(struct rpc_clnt *clnt, struct rpc_xprt *xprt)
1015 struct rpc_xprt_switch *xps;
1017 atomic_long_dec(&xprt->queuelen);
1019 xps = rcu_dereference(clnt->cl_xpi.xpi_xpswitch);
1020 atomic_long_dec(&xps->xps_queuelen);
1026 void rpc_task_release_transport(struct rpc_task *task)
1028 struct rpc_xprt *xprt = task->tk_xprt;
1031 task->tk_xprt = NULL;
1032 if (task->tk_client)
1033 rpc_task_release_xprt(task->tk_client, xprt);
1038 EXPORT_SYMBOL_GPL(rpc_task_release_transport);
1040 void rpc_task_release_client(struct rpc_task *task)
1042 struct rpc_clnt *clnt = task->tk_client;
1044 rpc_task_release_transport(task);
1046 /* Remove from client task list */
1047 spin_lock(&clnt->cl_lock);
1048 list_del(&task->tk_task);
1049 spin_unlock(&clnt->cl_lock);
1050 task->tk_client = NULL;
1052 rpc_release_client(clnt);
1056 static struct rpc_xprt *
1057 rpc_task_get_first_xprt(struct rpc_clnt *clnt)
1059 struct rpc_xprt *xprt;
1062 xprt = xprt_get(rcu_dereference(clnt->cl_xprt));
1064 return rpc_task_get_xprt(clnt, xprt);
1067 static struct rpc_xprt *
1068 rpc_task_get_next_xprt(struct rpc_clnt *clnt)
1070 return rpc_task_get_xprt(clnt, xprt_iter_get_next(&clnt->cl_xpi));
1074 void rpc_task_set_transport(struct rpc_task *task, struct rpc_clnt *clnt)
1076 if (task->tk_xprt) {
1077 if (!(test_bit(XPRT_OFFLINE, &task->tk_xprt->state) &&
1078 (task->tk_flags & RPC_TASK_MOVEABLE)))
1081 xprt_put(task->tk_xprt);
1083 if (task->tk_flags & RPC_TASK_NO_ROUND_ROBIN)
1084 task->tk_xprt = rpc_task_get_first_xprt(clnt);
1086 task->tk_xprt = rpc_task_get_next_xprt(clnt);
1090 void rpc_task_set_client(struct rpc_task *task, struct rpc_clnt *clnt)
1094 rpc_task_set_transport(task, clnt);
1095 task->tk_client = clnt;
1096 refcount_inc(&clnt->cl_count);
1097 if (clnt->cl_softrtry)
1098 task->tk_flags |= RPC_TASK_SOFT;
1099 if (clnt->cl_softerr)
1100 task->tk_flags |= RPC_TASK_TIMEOUT;
1101 if (clnt->cl_noretranstimeo)
1102 task->tk_flags |= RPC_TASK_NO_RETRANS_TIMEOUT;
1103 if (atomic_read(&clnt->cl_swapper))
1104 task->tk_flags |= RPC_TASK_SWAPPER;
1105 /* Add to the client's list of all tasks */
1106 spin_lock(&clnt->cl_lock);
1107 list_add_tail(&task->tk_task, &clnt->cl_tasks);
1108 spin_unlock(&clnt->cl_lock);
1113 rpc_task_set_rpc_message(struct rpc_task *task, const struct rpc_message *msg)
1116 task->tk_msg.rpc_proc = msg->rpc_proc;
1117 task->tk_msg.rpc_argp = msg->rpc_argp;
1118 task->tk_msg.rpc_resp = msg->rpc_resp;
1119 task->tk_msg.rpc_cred = msg->rpc_cred;
1120 if (!(task->tk_flags & RPC_TASK_CRED_NOREF))
1121 get_cred(task->tk_msg.rpc_cred);
1126 * Default callback for async RPC calls
1129 rpc_default_callback(struct rpc_task *task, void *data)
1133 static const struct rpc_call_ops rpc_default_ops = {
1134 .rpc_call_done = rpc_default_callback,
1138 * rpc_run_task - Allocate a new RPC task, then run rpc_execute against it
1139 * @task_setup_data: pointer to task initialisation data
1141 struct rpc_task *rpc_run_task(const struct rpc_task_setup *task_setup_data)
1143 struct rpc_task *task;
1145 task = rpc_new_task(task_setup_data);
1147 if (!RPC_IS_ASYNC(task))
1148 task->tk_flags |= RPC_TASK_CRED_NOREF;
1150 rpc_task_set_client(task, task_setup_data->rpc_client);
1151 rpc_task_set_rpc_message(task, task_setup_data->rpc_message);
1153 if (task->tk_action == NULL)
1154 rpc_call_start(task);
1156 atomic_inc(&task->tk_count);
1160 EXPORT_SYMBOL_GPL(rpc_run_task);
1163 * rpc_call_sync - Perform a synchronous RPC call
1164 * @clnt: pointer to RPC client
1165 * @msg: RPC call parameters
1166 * @flags: RPC call flags
1168 int rpc_call_sync(struct rpc_clnt *clnt, const struct rpc_message *msg, int flags)
1170 struct rpc_task *task;
1171 struct rpc_task_setup task_setup_data = {
1174 .callback_ops = &rpc_default_ops,
1179 WARN_ON_ONCE(flags & RPC_TASK_ASYNC);
1180 if (flags & RPC_TASK_ASYNC) {
1181 rpc_release_calldata(task_setup_data.callback_ops,
1182 task_setup_data.callback_data);
1186 task = rpc_run_task(&task_setup_data);
1188 return PTR_ERR(task);
1189 status = task->tk_status;
1193 EXPORT_SYMBOL_GPL(rpc_call_sync);
1196 * rpc_call_async - Perform an asynchronous RPC call
1197 * @clnt: pointer to RPC client
1198 * @msg: RPC call parameters
1199 * @flags: RPC call flags
1200 * @tk_ops: RPC call ops
1201 * @data: user call data
1204 rpc_call_async(struct rpc_clnt *clnt, const struct rpc_message *msg, int flags,
1205 const struct rpc_call_ops *tk_ops, void *data)
1207 struct rpc_task *task;
1208 struct rpc_task_setup task_setup_data = {
1211 .callback_ops = tk_ops,
1212 .callback_data = data,
1213 .flags = flags|RPC_TASK_ASYNC,
1216 task = rpc_run_task(&task_setup_data);
1218 return PTR_ERR(task);
1222 EXPORT_SYMBOL_GPL(rpc_call_async);
1224 #if defined(CONFIG_SUNRPC_BACKCHANNEL)
1225 static void call_bc_encode(struct rpc_task *task);
1228 * rpc_run_bc_task - Allocate a new RPC task for backchannel use, then run
1229 * rpc_execute against it
1232 struct rpc_task *rpc_run_bc_task(struct rpc_rqst *req)
1234 struct rpc_task *task;
1235 struct rpc_task_setup task_setup_data = {
1236 .callback_ops = &rpc_default_ops,
1237 .flags = RPC_TASK_SOFTCONN |
1238 RPC_TASK_NO_RETRANS_TIMEOUT,
1241 dprintk("RPC: rpc_run_bc_task req= %p\n", req);
1243 * Create an rpc_task to send the data
1245 task = rpc_new_task(&task_setup_data);
1246 xprt_init_bc_request(req, task);
1248 task->tk_action = call_bc_encode;
1249 atomic_inc(&task->tk_count);
1250 WARN_ON_ONCE(atomic_read(&task->tk_count) != 2);
1253 dprintk("RPC: rpc_run_bc_task: task= %p\n", task);
1256 #endif /* CONFIG_SUNRPC_BACKCHANNEL */
1259 * rpc_prepare_reply_pages - Prepare to receive a reply data payload into pages
1260 * @req: RPC request to prepare
1261 * @pages: vector of struct page pointers
1262 * @base: offset in first page where receive should start, in bytes
1263 * @len: expected size of the upper layer data payload, in bytes
1264 * @hdrsize: expected size of upper layer reply header, in XDR words
1267 void rpc_prepare_reply_pages(struct rpc_rqst *req, struct page **pages,
1268 unsigned int base, unsigned int len,
1269 unsigned int hdrsize)
1271 hdrsize += RPC_REPHDRSIZE + req->rq_cred->cr_auth->au_ralign;
1273 xdr_inline_pages(&req->rq_rcv_buf, hdrsize << 2, pages, base, len);
1274 trace_rpc_xdr_reply_pages(req->rq_task, &req->rq_rcv_buf);
1276 EXPORT_SYMBOL_GPL(rpc_prepare_reply_pages);
1279 rpc_call_start(struct rpc_task *task)
1281 task->tk_action = call_start;
1283 EXPORT_SYMBOL_GPL(rpc_call_start);
1286 * rpc_peeraddr - extract remote peer address from clnt's xprt
1287 * @clnt: RPC client structure
1288 * @buf: target buffer
1289 * @bufsize: length of target buffer
1291 * Returns the number of bytes that are actually in the stored address.
1293 size_t rpc_peeraddr(struct rpc_clnt *clnt, struct sockaddr *buf, size_t bufsize)
1296 struct rpc_xprt *xprt;
1299 xprt = rcu_dereference(clnt->cl_xprt);
1301 bytes = xprt->addrlen;
1302 if (bytes > bufsize)
1304 memcpy(buf, &xprt->addr, bytes);
1309 EXPORT_SYMBOL_GPL(rpc_peeraddr);
1312 * rpc_peeraddr2str - return remote peer address in printable format
1313 * @clnt: RPC client structure
1314 * @format: address format
1316 * NB: the lifetime of the memory referenced by the returned pointer is
1317 * the same as the rpc_xprt itself. As long as the caller uses this
1318 * pointer, it must hold the RCU read lock.
1320 const char *rpc_peeraddr2str(struct rpc_clnt *clnt,
1321 enum rpc_display_format_t format)
1323 struct rpc_xprt *xprt;
1325 xprt = rcu_dereference(clnt->cl_xprt);
1327 if (xprt->address_strings[format] != NULL)
1328 return xprt->address_strings[format];
1330 return "unprintable";
1332 EXPORT_SYMBOL_GPL(rpc_peeraddr2str);
1334 static const struct sockaddr_in rpc_inaddr_loopback = {
1335 .sin_family = AF_INET,
1336 .sin_addr.s_addr = htonl(INADDR_ANY),
1339 static const struct sockaddr_in6 rpc_in6addr_loopback = {
1340 .sin6_family = AF_INET6,
1341 .sin6_addr = IN6ADDR_ANY_INIT,
1345 * Try a getsockname() on a connected datagram socket. Using a
1346 * connected datagram socket prevents leaving a socket in TIME_WAIT.
1347 * This conserves the ephemeral port number space.
1349 * Returns zero and fills in "buf" if successful; otherwise, a
1350 * negative errno is returned.
1352 static int rpc_sockname(struct net *net, struct sockaddr *sap, size_t salen,
1353 struct sockaddr *buf)
1355 struct socket *sock;
1358 err = __sock_create(net, sap->sa_family,
1359 SOCK_DGRAM, IPPROTO_UDP, &sock, 1);
1361 dprintk("RPC: can't create UDP socket (%d)\n", err);
1365 switch (sap->sa_family) {
1367 err = kernel_bind(sock,
1368 (struct sockaddr *)&rpc_inaddr_loopback,
1369 sizeof(rpc_inaddr_loopback));
1372 err = kernel_bind(sock,
1373 (struct sockaddr *)&rpc_in6addr_loopback,
1374 sizeof(rpc_in6addr_loopback));
1377 err = -EAFNOSUPPORT;
1381 dprintk("RPC: can't bind UDP socket (%d)\n", err);
1385 err = kernel_connect(sock, sap, salen, 0);
1387 dprintk("RPC: can't connect UDP socket (%d)\n", err);
1391 err = kernel_getsockname(sock, buf);
1393 dprintk("RPC: getsockname failed (%d)\n", err);
1398 if (buf->sa_family == AF_INET6) {
1399 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)buf;
1400 sin6->sin6_scope_id = 0;
1402 dprintk("RPC: %s succeeded\n", __func__);
1411 * Scraping a connected socket failed, so we don't have a useable
1412 * local address. Fallback: generate an address that will prevent
1413 * the server from calling us back.
1415 * Returns zero and fills in "buf" if successful; otherwise, a
1416 * negative errno is returned.
1418 static int rpc_anyaddr(int family, struct sockaddr *buf, size_t buflen)
1422 if (buflen < sizeof(rpc_inaddr_loopback))
1424 memcpy(buf, &rpc_inaddr_loopback,
1425 sizeof(rpc_inaddr_loopback));
1428 if (buflen < sizeof(rpc_in6addr_loopback))
1430 memcpy(buf, &rpc_in6addr_loopback,
1431 sizeof(rpc_in6addr_loopback));
1434 dprintk("RPC: %s: address family not supported\n",
1436 return -EAFNOSUPPORT;
1438 dprintk("RPC: %s: succeeded\n", __func__);
1443 * rpc_localaddr - discover local endpoint address for an RPC client
1444 * @clnt: RPC client structure
1445 * @buf: target buffer
1446 * @buflen: size of target buffer, in bytes
1448 * Returns zero and fills in "buf" and "buflen" if successful;
1449 * otherwise, a negative errno is returned.
1451 * This works even if the underlying transport is not currently connected,
1452 * or if the upper layer never previously provided a source address.
1454 * The result of this function call is transient: multiple calls in
1455 * succession may give different results, depending on how local
1456 * networking configuration changes over time.
1458 int rpc_localaddr(struct rpc_clnt *clnt, struct sockaddr *buf, size_t buflen)
1460 struct sockaddr_storage address;
1461 struct sockaddr *sap = (struct sockaddr *)&address;
1462 struct rpc_xprt *xprt;
1468 xprt = rcu_dereference(clnt->cl_xprt);
1469 salen = xprt->addrlen;
1470 memcpy(sap, &xprt->addr, salen);
1471 net = get_net(xprt->xprt_net);
1474 rpc_set_port(sap, 0);
1475 err = rpc_sockname(net, sap, salen, buf);
1478 /* Couldn't discover local address, return ANYADDR */
1479 return rpc_anyaddr(sap->sa_family, buf, buflen);
1482 EXPORT_SYMBOL_GPL(rpc_localaddr);
1485 rpc_setbufsize(struct rpc_clnt *clnt, unsigned int sndsize, unsigned int rcvsize)
1487 struct rpc_xprt *xprt;
1490 xprt = rcu_dereference(clnt->cl_xprt);
1491 if (xprt->ops->set_buffer_size)
1492 xprt->ops->set_buffer_size(xprt, sndsize, rcvsize);
1495 EXPORT_SYMBOL_GPL(rpc_setbufsize);
1498 * rpc_net_ns - Get the network namespace for this RPC client
1499 * @clnt: RPC client to query
1502 struct net *rpc_net_ns(struct rpc_clnt *clnt)
1507 ret = rcu_dereference(clnt->cl_xprt)->xprt_net;
1511 EXPORT_SYMBOL_GPL(rpc_net_ns);
1514 * rpc_max_payload - Get maximum payload size for a transport, in bytes
1515 * @clnt: RPC client to query
1517 * For stream transports, this is one RPC record fragment (see RFC
1518 * 1831), as we don't support multi-record requests yet. For datagram
1519 * transports, this is the size of an IP packet minus the IP, UDP, and
1522 size_t rpc_max_payload(struct rpc_clnt *clnt)
1527 ret = rcu_dereference(clnt->cl_xprt)->max_payload;
1531 EXPORT_SYMBOL_GPL(rpc_max_payload);
1534 * rpc_max_bc_payload - Get maximum backchannel payload size, in bytes
1535 * @clnt: RPC client to query
1537 size_t rpc_max_bc_payload(struct rpc_clnt *clnt)
1539 struct rpc_xprt *xprt;
1543 xprt = rcu_dereference(clnt->cl_xprt);
1544 ret = xprt->ops->bc_maxpayload(xprt);
1548 EXPORT_SYMBOL_GPL(rpc_max_bc_payload);
1550 unsigned int rpc_num_bc_slots(struct rpc_clnt *clnt)
1552 struct rpc_xprt *xprt;
1556 xprt = rcu_dereference(clnt->cl_xprt);
1557 ret = xprt->ops->bc_num_slots(xprt);
1561 EXPORT_SYMBOL_GPL(rpc_num_bc_slots);
1564 * rpc_force_rebind - force transport to check that remote port is unchanged
1565 * @clnt: client to rebind
1568 void rpc_force_rebind(struct rpc_clnt *clnt)
1570 if (clnt->cl_autobind) {
1572 xprt_clear_bound(rcu_dereference(clnt->cl_xprt));
1576 EXPORT_SYMBOL_GPL(rpc_force_rebind);
1579 __rpc_restart_call(struct rpc_task *task, void (*action)(struct rpc_task *))
1581 task->tk_status = 0;
1582 task->tk_rpc_status = 0;
1583 task->tk_action = action;
1588 * Restart an (async) RPC call. Usually called from within the
1592 rpc_restart_call(struct rpc_task *task)
1594 return __rpc_restart_call(task, call_start);
1596 EXPORT_SYMBOL_GPL(rpc_restart_call);
1599 * Restart an (async) RPC call from the call_prepare state.
1600 * Usually called from within the exit handler.
1603 rpc_restart_call_prepare(struct rpc_task *task)
1605 if (task->tk_ops->rpc_call_prepare != NULL)
1606 return __rpc_restart_call(task, rpc_prepare_task);
1607 return rpc_restart_call(task);
1609 EXPORT_SYMBOL_GPL(rpc_restart_call_prepare);
1612 *rpc_proc_name(const struct rpc_task *task)
1614 const struct rpc_procinfo *proc = task->tk_msg.rpc_proc;
1618 return proc->p_name;
1626 __rpc_call_rpcerror(struct rpc_task *task, int tk_status, int rpc_status)
1628 trace_rpc_call_rpcerror(task, tk_status, rpc_status);
1629 task->tk_rpc_status = rpc_status;
1630 rpc_exit(task, tk_status);
1634 rpc_call_rpcerror(struct rpc_task *task, int status)
1636 __rpc_call_rpcerror(task, status, status);
1642 * Other FSM states can be visited zero or more times, but
1643 * this state is visited exactly once for each RPC.
1646 call_start(struct rpc_task *task)
1648 struct rpc_clnt *clnt = task->tk_client;
1649 int idx = task->tk_msg.rpc_proc->p_statidx;
1651 trace_rpc_request(task);
1653 /* Increment call count (version might not be valid for ping) */
1654 if (clnt->cl_program->version[clnt->cl_vers])
1655 clnt->cl_program->version[clnt->cl_vers]->counts[idx]++;
1656 clnt->cl_stats->rpccnt++;
1657 task->tk_action = call_reserve;
1658 rpc_task_set_transport(task, clnt);
1662 * 1. Reserve an RPC call slot
1665 call_reserve(struct rpc_task *task)
1667 task->tk_status = 0;
1668 task->tk_action = call_reserveresult;
1672 static void call_retry_reserve(struct rpc_task *task);
1675 * 1b. Grok the result of xprt_reserve()
1678 call_reserveresult(struct rpc_task *task)
1680 int status = task->tk_status;
1683 * After a call to xprt_reserve(), we must have either
1684 * a request slot or else an error status.
1686 task->tk_status = 0;
1688 if (task->tk_rqstp) {
1689 task->tk_action = call_refresh;
1693 rpc_call_rpcerror(task, -EIO);
1699 rpc_delay(task, HZ >> 2);
1701 case -EAGAIN: /* woken up; retry */
1702 task->tk_action = call_retry_reserve;
1705 rpc_call_rpcerror(task, status);
1710 * 1c. Retry reserving an RPC call slot
1713 call_retry_reserve(struct rpc_task *task)
1715 task->tk_status = 0;
1716 task->tk_action = call_reserveresult;
1717 xprt_retry_reserve(task);
1721 * 2. Bind and/or refresh the credentials
1724 call_refresh(struct rpc_task *task)
1726 task->tk_action = call_refreshresult;
1727 task->tk_status = 0;
1728 task->tk_client->cl_stats->rpcauthrefresh++;
1729 rpcauth_refreshcred(task);
1733 * 2a. Process the results of a credential refresh
1736 call_refreshresult(struct rpc_task *task)
1738 int status = task->tk_status;
1740 task->tk_status = 0;
1741 task->tk_action = call_refresh;
1744 if (rpcauth_uptodatecred(task)) {
1745 task->tk_action = call_allocate;
1748 /* Use rate-limiting and a max number of retries if refresh
1749 * had status 0 but failed to update the cred.
1753 rpc_delay(task, 3*HZ);
1759 if (!task->tk_cred_retry)
1761 task->tk_cred_retry--;
1762 trace_rpc_retry_refresh_status(task);
1765 trace_rpc_refresh_status(task);
1766 rpc_call_rpcerror(task, status);
1770 * 2b. Allocate the buffer. For details, see sched.c:rpc_malloc.
1771 * (Note: buffer memory is freed in xprt_release).
1774 call_allocate(struct rpc_task *task)
1776 const struct rpc_auth *auth = task->tk_rqstp->rq_cred->cr_auth;
1777 struct rpc_rqst *req = task->tk_rqstp;
1778 struct rpc_xprt *xprt = req->rq_xprt;
1779 const struct rpc_procinfo *proc = task->tk_msg.rpc_proc;
1782 task->tk_status = 0;
1783 task->tk_action = call_encode;
1788 if (proc->p_proc != 0) {
1789 BUG_ON(proc->p_arglen == 0);
1790 if (proc->p_decode != NULL)
1791 BUG_ON(proc->p_replen == 0);
1795 * Calculate the size (in quads) of the RPC call
1796 * and reply headers, and convert both values
1799 req->rq_callsize = RPC_CALLHDRSIZE + (auth->au_cslack << 1) +
1801 req->rq_callsize <<= 2;
1803 * Note: the reply buffer must at minimum allocate enough space
1804 * for the 'struct accepted_reply' from RFC5531.
1806 req->rq_rcvsize = RPC_REPHDRSIZE + auth->au_rslack + \
1807 max_t(size_t, proc->p_replen, 2);
1808 req->rq_rcvsize <<= 2;
1810 status = xprt->ops->buf_alloc(task);
1811 trace_rpc_buf_alloc(task, status);
1814 if (status != -ENOMEM) {
1815 rpc_call_rpcerror(task, status);
1819 if (RPC_IS_ASYNC(task) || !fatal_signal_pending(current)) {
1820 task->tk_action = call_allocate;
1821 rpc_delay(task, HZ>>4);
1825 rpc_call_rpcerror(task, -ERESTARTSYS);
1829 rpc_task_need_encode(struct rpc_task *task)
1831 return test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate) == 0 &&
1832 (!(task->tk_flags & RPC_TASK_SENT) ||
1833 !(task->tk_flags & RPC_TASK_NO_RETRANS_TIMEOUT) ||
1834 xprt_request_need_retransmit(task));
1838 rpc_xdr_encode(struct rpc_task *task)
1840 struct rpc_rqst *req = task->tk_rqstp;
1841 struct xdr_stream xdr;
1843 xdr_buf_init(&req->rq_snd_buf,
1846 xdr_buf_init(&req->rq_rcv_buf,
1850 req->rq_reply_bytes_recvd = 0;
1851 req->rq_snd_buf.head[0].iov_len = 0;
1852 xdr_init_encode(&xdr, &req->rq_snd_buf,
1853 req->rq_snd_buf.head[0].iov_base, req);
1854 xdr_free_bvec(&req->rq_snd_buf);
1855 if (rpc_encode_header(task, &xdr))
1858 task->tk_status = rpcauth_wrap_req(task, &xdr);
1862 * 3. Encode arguments of an RPC call
1865 call_encode(struct rpc_task *task)
1867 if (!rpc_task_need_encode(task))
1870 /* Dequeue task from the receive queue while we're encoding */
1871 xprt_request_dequeue_xprt(task);
1872 /* Encode here so that rpcsec_gss can use correct sequence number. */
1873 rpc_xdr_encode(task);
1874 /* Did the encode result in an error condition? */
1875 if (task->tk_status != 0) {
1876 /* Was the error nonfatal? */
1877 switch (task->tk_status) {
1880 rpc_delay(task, HZ >> 4);
1883 if (!task->tk_cred_retry) {
1884 rpc_call_rpcerror(task, task->tk_status);
1886 task->tk_action = call_refresh;
1887 task->tk_cred_retry--;
1888 trace_rpc_retry_refresh_status(task);
1892 rpc_call_rpcerror(task, task->tk_status);
1897 /* Add task to reply queue before transmission to avoid races */
1898 if (rpc_reply_expected(task))
1899 xprt_request_enqueue_receive(task);
1900 xprt_request_enqueue_transmit(task);
1902 task->tk_action = call_transmit;
1903 /* Check that the connection is OK */
1904 if (!xprt_bound(task->tk_xprt))
1905 task->tk_action = call_bind;
1906 else if (!xprt_connected(task->tk_xprt))
1907 task->tk_action = call_connect;
1911 * Helpers to check if the task was already transmitted, and
1912 * to take action when that is the case.
1915 rpc_task_transmitted(struct rpc_task *task)
1917 return !test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate);
1921 rpc_task_handle_transmitted(struct rpc_task *task)
1923 xprt_end_transmit(task);
1924 task->tk_action = call_transmit_status;
1928 * 4. Get the server port number if not yet set
1931 call_bind(struct rpc_task *task)
1933 struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
1935 if (rpc_task_transmitted(task)) {
1936 rpc_task_handle_transmitted(task);
1940 if (xprt_bound(xprt)) {
1941 task->tk_action = call_connect;
1945 task->tk_action = call_bind_status;
1946 if (!xprt_prepare_transmit(task))
1949 xprt->ops->rpcbind(task);
1953 * 4a. Sort out bind result
1956 call_bind_status(struct rpc_task *task)
1958 struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
1961 if (rpc_task_transmitted(task)) {
1962 rpc_task_handle_transmitted(task);
1966 if (task->tk_status >= 0)
1968 if (xprt_bound(xprt)) {
1969 task->tk_status = 0;
1973 switch (task->tk_status) {
1975 rpc_delay(task, HZ >> 2);
1978 trace_rpcb_prog_unavail_err(task);
1979 /* fail immediately if this is an RPC ping */
1980 if (task->tk_msg.rpc_proc->p_proc == 0) {
1981 status = -EOPNOTSUPP;
1984 if (task->tk_rebind_retry == 0)
1986 task->tk_rebind_retry--;
1987 rpc_delay(task, 3*HZ);
1990 rpc_delay(task, HZ >> 2);
1995 trace_rpcb_timeout_err(task);
1998 /* server doesn't support any rpcbind version we know of */
1999 trace_rpcb_bind_version_err(task);
2001 case -EPROTONOSUPPORT:
2002 trace_rpcb_bind_version_err(task);
2004 case -ECONNREFUSED: /* connection problems */
2013 trace_rpcb_unreachable_err(task);
2014 if (!RPC_IS_SOFTCONN(task)) {
2015 rpc_delay(task, 5*HZ);
2018 status = task->tk_status;
2021 trace_rpcb_unrecognized_err(task);
2024 rpc_call_rpcerror(task, status);
2027 task->tk_action = call_connect;
2030 task->tk_status = 0;
2031 task->tk_action = call_bind;
2032 rpc_check_timeout(task);
2036 * 4b. Connect to the RPC server
2039 call_connect(struct rpc_task *task)
2041 struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
2043 if (rpc_task_transmitted(task)) {
2044 rpc_task_handle_transmitted(task);
2048 if (xprt_connected(xprt)) {
2049 task->tk_action = call_transmit;
2053 task->tk_action = call_connect_status;
2054 if (task->tk_status < 0)
2056 if (task->tk_flags & RPC_TASK_NOCONNECT) {
2057 rpc_call_rpcerror(task, -ENOTCONN);
2060 if (!xprt_prepare_transmit(task))
2066 * 4c. Sort out connect result
2069 call_connect_status(struct rpc_task *task)
2071 struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
2072 struct rpc_clnt *clnt = task->tk_client;
2073 int status = task->tk_status;
2075 if (rpc_task_transmitted(task)) {
2076 rpc_task_handle_transmitted(task);
2080 trace_rpc_connect_status(task);
2082 if (task->tk_status == 0) {
2083 clnt->cl_stats->netreconn++;
2086 if (xprt_connected(xprt)) {
2087 task->tk_status = 0;
2091 task->tk_status = 0;
2094 /* A positive refusal suggests a rebind is needed. */
2095 if (RPC_IS_SOFTCONN(task))
2097 if (clnt->cl_autobind) {
2098 rpc_force_rebind(clnt);
2109 xprt_conditional_disconnect(task->tk_rqstp->rq_xprt,
2110 task->tk_rqstp->rq_connect_cookie);
2111 if (RPC_IS_SOFTCONN(task))
2113 /* retry with existing socket, after a delay */
2114 rpc_delay(task, 3*HZ);
2120 if (!(task->tk_flags & RPC_TASK_NO_ROUND_ROBIN) &&
2121 (task->tk_flags & RPC_TASK_MOVEABLE) &&
2122 test_bit(XPRT_REMOVE, &xprt->state)) {
2123 struct rpc_xprt *saved = task->tk_xprt;
2124 struct rpc_xprt_switch *xps;
2127 xps = xprt_switch_get(rcu_dereference(clnt->cl_xpi.xpi_xpswitch));
2129 if (xps->xps_nxprts > 1) {
2133 value = atomic_long_dec_return(&xprt->queuelen);
2135 rpc_xprt_switch_remove_xprt(xps, saved);
2137 task->tk_xprt = NULL;
2138 task->tk_action = call_start;
2140 xprt_switch_put(xps);
2146 rpc_delay(task, HZ >> 2);
2149 rpc_call_rpcerror(task, status);
2152 task->tk_action = call_transmit;
2155 /* Check for timeouts before looping back to call_bind */
2156 task->tk_action = call_bind;
2157 rpc_check_timeout(task);
2161 * 5. Transmit the RPC request, and wait for reply
2164 call_transmit(struct rpc_task *task)
2166 if (rpc_task_transmitted(task)) {
2167 rpc_task_handle_transmitted(task);
2171 task->tk_action = call_transmit_status;
2172 if (!xprt_prepare_transmit(task))
2174 task->tk_status = 0;
2175 if (test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate)) {
2176 if (!xprt_connected(task->tk_xprt)) {
2177 task->tk_status = -ENOTCONN;
2180 xprt_transmit(task);
2182 xprt_end_transmit(task);
2186 * 5a. Handle cleanup after a transmission
2189 call_transmit_status(struct rpc_task *task)
2191 task->tk_action = call_status;
2194 * Common case: success. Force the compiler to put this
2197 if (rpc_task_transmitted(task)) {
2198 task->tk_status = 0;
2199 xprt_request_wait_receive(task);
2203 switch (task->tk_status) {
2207 task->tk_status = 0;
2208 task->tk_action = call_encode;
2211 * Special cases: if we've been waiting on the
2212 * socket's write_space() callback, or if the
2213 * socket just returned a connection error,
2214 * then hold onto the transport lock.
2218 rpc_delay(task, HZ>>2);
2222 task->tk_action = call_transmit;
2223 task->tk_status = 0;
2231 if (RPC_IS_SOFTCONN(task)) {
2232 if (!task->tk_msg.rpc_proc->p_proc)
2233 trace_xprt_ping(task->tk_xprt,
2235 rpc_call_rpcerror(task, task->tk_status);
2244 task->tk_action = call_bind;
2245 task->tk_status = 0;
2248 rpc_check_timeout(task);
2251 #if defined(CONFIG_SUNRPC_BACKCHANNEL)
2252 static void call_bc_transmit(struct rpc_task *task);
2253 static void call_bc_transmit_status(struct rpc_task *task);
2256 call_bc_encode(struct rpc_task *task)
2258 xprt_request_enqueue_transmit(task);
2259 task->tk_action = call_bc_transmit;
2263 * 5b. Send the backchannel RPC reply. On error, drop the reply. In
2264 * addition, disconnect on connectivity errors.
2267 call_bc_transmit(struct rpc_task *task)
2269 task->tk_action = call_bc_transmit_status;
2270 if (test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate)) {
2271 if (!xprt_prepare_transmit(task))
2273 task->tk_status = 0;
2274 xprt_transmit(task);
2276 xprt_end_transmit(task);
2280 call_bc_transmit_status(struct rpc_task *task)
2282 struct rpc_rqst *req = task->tk_rqstp;
2284 if (rpc_task_transmitted(task))
2285 task->tk_status = 0;
2287 switch (task->tk_status) {
2302 rpc_delay(task, HZ>>2);
2306 task->tk_status = 0;
2307 task->tk_action = call_bc_transmit;
2311 * Problem reaching the server. Disconnect and let the
2312 * forechannel reestablish the connection. The server will
2313 * have to retransmit the backchannel request and we'll
2314 * reprocess it. Since these ops are idempotent, there's no
2315 * need to cache our reply at this time.
2317 printk(KERN_NOTICE "RPC: Could not send backchannel reply "
2318 "error: %d\n", task->tk_status);
2319 xprt_conditional_disconnect(req->rq_xprt,
2320 req->rq_connect_cookie);
2324 * We were unable to reply and will have to drop the
2325 * request. The server should reconnect and retransmit.
2327 printk(KERN_NOTICE "RPC: Could not send backchannel reply "
2328 "error: %d\n", task->tk_status);
2331 task->tk_action = rpc_exit_task;
2333 #endif /* CONFIG_SUNRPC_BACKCHANNEL */
2336 * 6. Sort out the RPC call status
2339 call_status(struct rpc_task *task)
2341 struct rpc_clnt *clnt = task->tk_client;
2344 if (!task->tk_msg.rpc_proc->p_proc)
2345 trace_xprt_ping(task->tk_xprt, task->tk_status);
2347 status = task->tk_status;
2349 task->tk_action = call_decode;
2353 trace_rpc_call_status(task);
2354 task->tk_status = 0;
2361 if (RPC_IS_SOFTCONN(task))
2364 * Delay any retries for 3 seconds, then handle as if it
2367 rpc_delay(task, 3*HZ);
2375 rpc_force_rebind(clnt);
2378 rpc_delay(task, 3*HZ);
2386 rpc_delay(task, HZ>>2);
2389 /* shutdown or soft timeout */
2392 if (clnt->cl_chatty)
2393 printk("%s: RPC call returned error %d\n",
2394 clnt->cl_program->name, -status);
2397 task->tk_action = call_encode;
2398 if (status != -ECONNRESET && status != -ECONNABORTED)
2399 rpc_check_timeout(task);
2402 rpc_call_rpcerror(task, status);
2406 rpc_check_connected(const struct rpc_rqst *req)
2408 /* No allocated request or transport? return true */
2409 if (!req || !req->rq_xprt)
2411 return xprt_connected(req->rq_xprt);
2415 rpc_check_timeout(struct rpc_task *task)
2417 struct rpc_clnt *clnt = task->tk_client;
2419 if (RPC_SIGNALLED(task)) {
2420 rpc_call_rpcerror(task, -ERESTARTSYS);
2424 if (xprt_adjust_timeout(task->tk_rqstp) == 0)
2427 trace_rpc_timeout_status(task);
2428 task->tk_timeouts++;
2430 if (RPC_IS_SOFTCONN(task) && !rpc_check_connected(task->tk_rqstp)) {
2431 rpc_call_rpcerror(task, -ETIMEDOUT);
2435 if (RPC_IS_SOFT(task)) {
2437 * Once a "no retrans timeout" soft tasks (a.k.a NFSv4) has
2438 * been sent, it should time out only if the transport
2439 * connection gets terminally broken.
2441 if ((task->tk_flags & RPC_TASK_NO_RETRANS_TIMEOUT) &&
2442 rpc_check_connected(task->tk_rqstp))
2445 if (clnt->cl_chatty) {
2446 pr_notice_ratelimited(
2447 "%s: server %s not responding, timed out\n",
2448 clnt->cl_program->name,
2449 task->tk_xprt->servername);
2451 if (task->tk_flags & RPC_TASK_TIMEOUT)
2452 rpc_call_rpcerror(task, -ETIMEDOUT);
2454 __rpc_call_rpcerror(task, -EIO, -ETIMEDOUT);
2458 if (!(task->tk_flags & RPC_CALL_MAJORSEEN)) {
2459 task->tk_flags |= RPC_CALL_MAJORSEEN;
2460 if (clnt->cl_chatty) {
2461 pr_notice_ratelimited(
2462 "%s: server %s not responding, still trying\n",
2463 clnt->cl_program->name,
2464 task->tk_xprt->servername);
2467 rpc_force_rebind(clnt);
2469 * Did our request time out due to an RPCSEC_GSS out-of-sequence
2470 * event? RFC2203 requires the server to drop all such requests.
2472 rpcauth_invalcred(task);
2476 * 7. Decode the RPC reply
2479 call_decode(struct rpc_task *task)
2481 struct rpc_clnt *clnt = task->tk_client;
2482 struct rpc_rqst *req = task->tk_rqstp;
2483 struct xdr_stream xdr;
2486 if (!task->tk_msg.rpc_proc->p_decode) {
2487 task->tk_action = rpc_exit_task;
2491 if (task->tk_flags & RPC_CALL_MAJORSEEN) {
2492 if (clnt->cl_chatty) {
2493 pr_notice_ratelimited("%s: server %s OK\n",
2494 clnt->cl_program->name,
2495 task->tk_xprt->servername);
2497 task->tk_flags &= ~RPC_CALL_MAJORSEEN;
2501 * Did we ever call xprt_complete_rqst()? If not, we should assume
2502 * the message is incomplete.
2505 if (!req->rq_reply_bytes_recvd)
2508 /* Ensure that we see all writes made by xprt_complete_rqst()
2509 * before it changed req->rq_reply_bytes_recvd.
2513 req->rq_rcv_buf.len = req->rq_private_buf.len;
2514 trace_rpc_xdr_recvfrom(task, &req->rq_rcv_buf);
2516 /* Check that the softirq receive buffer is valid */
2517 WARN_ON(memcmp(&req->rq_rcv_buf, &req->rq_private_buf,
2518 sizeof(req->rq_rcv_buf)) != 0);
2520 xdr_init_decode(&xdr, &req->rq_rcv_buf,
2521 req->rq_rcv_buf.head[0].iov_base, req);
2522 err = rpc_decode_header(task, &xdr);
2526 task->tk_action = rpc_exit_task;
2527 task->tk_status = rpcauth_unwrap_resp(task, &xdr);
2530 task->tk_status = 0;
2531 if (task->tk_client->cl_discrtry)
2532 xprt_conditional_disconnect(req->rq_xprt,
2533 req->rq_connect_cookie);
2534 task->tk_action = call_encode;
2535 rpc_check_timeout(task);
2538 task->tk_action = call_reserve;
2539 rpc_check_timeout(task);
2540 rpcauth_invalcred(task);
2541 /* Ensure we obtain a new XID if we retry! */
2547 rpc_encode_header(struct rpc_task *task, struct xdr_stream *xdr)
2549 struct rpc_clnt *clnt = task->tk_client;
2550 struct rpc_rqst *req = task->tk_rqstp;
2555 p = xdr_reserve_space(xdr, RPC_CALLHDRSIZE << 2);
2560 *p++ = cpu_to_be32(RPC_VERSION);
2561 *p++ = cpu_to_be32(clnt->cl_prog);
2562 *p++ = cpu_to_be32(clnt->cl_vers);
2563 *p = cpu_to_be32(task->tk_msg.rpc_proc->p_proc);
2565 error = rpcauth_marshcred(task, xdr);
2570 trace_rpc_bad_callhdr(task);
2571 rpc_call_rpcerror(task, error);
2576 rpc_decode_header(struct rpc_task *task, struct xdr_stream *xdr)
2578 struct rpc_clnt *clnt = task->tk_client;
2582 /* RFC-1014 says that the representation of XDR data must be a
2583 * multiple of four bytes
2584 * - if it isn't pointer subtraction in the NFS client may give
2587 if (task->tk_rqstp->rq_rcv_buf.len & 3)
2588 goto out_unparsable;
2590 p = xdr_inline_decode(xdr, 3 * sizeof(*p));
2592 goto out_unparsable;
2594 if (*p++ != rpc_reply)
2595 goto out_unparsable;
2596 if (*p++ != rpc_msg_accepted)
2597 goto out_msg_denied;
2599 error = rpcauth_checkverf(task, xdr);
2603 p = xdr_inline_decode(xdr, sizeof(*p));
2605 goto out_unparsable;
2609 case rpc_prog_unavail:
2610 trace_rpc__prog_unavail(task);
2611 error = -EPFNOSUPPORT;
2613 case rpc_prog_mismatch:
2614 trace_rpc__prog_mismatch(task);
2615 error = -EPROTONOSUPPORT;
2617 case rpc_proc_unavail:
2618 trace_rpc__proc_unavail(task);
2619 error = -EOPNOTSUPP;
2621 case rpc_garbage_args:
2622 case rpc_system_err:
2623 trace_rpc__garbage_args(task);
2627 goto out_unparsable;
2631 clnt->cl_stats->rpcgarbage++;
2632 if (task->tk_garb_retry) {
2633 task->tk_garb_retry--;
2634 task->tk_action = call_encode;
2638 rpc_call_rpcerror(task, error);
2642 trace_rpc__unparsable(task);
2647 trace_rpc_bad_verifier(task);
2652 p = xdr_inline_decode(xdr, sizeof(*p));
2654 goto out_unparsable;
2656 case rpc_auth_error:
2659 trace_rpc__mismatch(task);
2660 error = -EPROTONOSUPPORT;
2663 goto out_unparsable;
2666 p = xdr_inline_decode(xdr, sizeof(*p));
2668 goto out_unparsable;
2670 case rpc_autherr_rejectedcred:
2671 case rpc_autherr_rejectedverf:
2672 case rpcsec_gsserr_credproblem:
2673 case rpcsec_gsserr_ctxproblem:
2674 if (!task->tk_cred_retry)
2676 task->tk_cred_retry--;
2677 trace_rpc__stale_creds(task);
2678 return -EKEYREJECTED;
2679 case rpc_autherr_badcred:
2680 case rpc_autherr_badverf:
2681 /* possibly garbled cred/verf? */
2682 if (!task->tk_garb_retry)
2684 task->tk_garb_retry--;
2685 trace_rpc__bad_creds(task);
2686 task->tk_action = call_encode;
2688 case rpc_autherr_tooweak:
2689 trace_rpc__auth_tooweak(task);
2690 pr_warn("RPC: server %s requires stronger authentication.\n",
2691 task->tk_xprt->servername);
2694 goto out_unparsable;
2699 static void rpcproc_encode_null(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
2704 static int rpcproc_decode_null(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
2710 static const struct rpc_procinfo rpcproc_null = {
2711 .p_encode = rpcproc_encode_null,
2712 .p_decode = rpcproc_decode_null,
2715 static const struct rpc_procinfo rpcproc_null_noreply = {
2716 .p_encode = rpcproc_encode_null,
2720 rpc_null_call_prepare(struct rpc_task *task, void *data)
2722 task->tk_flags &= ~RPC_TASK_NO_RETRANS_TIMEOUT;
2723 rpc_call_start(task);
2726 static const struct rpc_call_ops rpc_null_ops = {
2727 .rpc_call_prepare = rpc_null_call_prepare,
2728 .rpc_call_done = rpc_default_callback,
2732 struct rpc_task *rpc_call_null_helper(struct rpc_clnt *clnt,
2733 struct rpc_xprt *xprt, struct rpc_cred *cred, int flags,
2734 const struct rpc_call_ops *ops, void *data)
2736 struct rpc_message msg = {
2737 .rpc_proc = &rpcproc_null,
2739 struct rpc_task_setup task_setup_data = {
2742 .rpc_message = &msg,
2743 .rpc_op_cred = cred,
2744 .callback_ops = ops ?: &rpc_null_ops,
2745 .callback_data = data,
2746 .flags = flags | RPC_TASK_SOFT | RPC_TASK_SOFTCONN |
2750 return rpc_run_task(&task_setup_data);
2753 struct rpc_task *rpc_call_null(struct rpc_clnt *clnt, struct rpc_cred *cred, int flags)
2755 return rpc_call_null_helper(clnt, NULL, cred, flags, NULL, NULL);
2757 EXPORT_SYMBOL_GPL(rpc_call_null);
2759 static int rpc_ping(struct rpc_clnt *clnt)
2761 struct rpc_task *task;
2764 task = rpc_call_null_helper(clnt, NULL, NULL, 0, NULL, NULL);
2766 return PTR_ERR(task);
2767 status = task->tk_status;
2772 static int rpc_ping_noreply(struct rpc_clnt *clnt)
2774 struct rpc_message msg = {
2775 .rpc_proc = &rpcproc_null_noreply,
2777 struct rpc_task_setup task_setup_data = {
2779 .rpc_message = &msg,
2780 .callback_ops = &rpc_null_ops,
2781 .flags = RPC_TASK_SOFT | RPC_TASK_SOFTCONN | RPC_TASK_NULLCREDS,
2783 struct rpc_task *task;
2786 task = rpc_run_task(&task_setup_data);
2788 return PTR_ERR(task);
2789 status = task->tk_status;
2794 struct rpc_cb_add_xprt_calldata {
2795 struct rpc_xprt_switch *xps;
2796 struct rpc_xprt *xprt;
2799 static void rpc_cb_add_xprt_done(struct rpc_task *task, void *calldata)
2801 struct rpc_cb_add_xprt_calldata *data = calldata;
2803 if (task->tk_status == 0)
2804 rpc_xprt_switch_add_xprt(data->xps, data->xprt);
2807 static void rpc_cb_add_xprt_release(void *calldata)
2809 struct rpc_cb_add_xprt_calldata *data = calldata;
2811 xprt_put(data->xprt);
2812 xprt_switch_put(data->xps);
2816 static const struct rpc_call_ops rpc_cb_add_xprt_call_ops = {
2817 .rpc_call_prepare = rpc_null_call_prepare,
2818 .rpc_call_done = rpc_cb_add_xprt_done,
2819 .rpc_release = rpc_cb_add_xprt_release,
2823 * rpc_clnt_test_and_add_xprt - Test and add a new transport to a rpc_clnt
2824 * @clnt: pointer to struct rpc_clnt
2825 * @xps: pointer to struct rpc_xprt_switch,
2826 * @xprt: pointer struct rpc_xprt
2829 int rpc_clnt_test_and_add_xprt(struct rpc_clnt *clnt,
2830 struct rpc_xprt_switch *xps, struct rpc_xprt *xprt,
2833 struct rpc_cb_add_xprt_calldata *data;
2834 struct rpc_task *task;
2836 if (xps->xps_nunique_destaddr_xprts + 1 > clnt->cl_max_connect) {
2838 pr_warn("SUNRPC: reached max allowed number (%d) did not add "
2839 "transport to server: %s\n", clnt->cl_max_connect,
2840 rpc_peeraddr2str(clnt, RPC_DISPLAY_ADDR));
2845 data = kmalloc(sizeof(*data), GFP_NOFS);
2848 data->xps = xprt_switch_get(xps);
2849 data->xprt = xprt_get(xprt);
2850 if (rpc_xprt_switch_has_addr(data->xps, (struct sockaddr *)&xprt->addr)) {
2851 rpc_cb_add_xprt_release(data);
2855 task = rpc_call_null_helper(clnt, xprt, NULL, RPC_TASK_ASYNC,
2856 &rpc_cb_add_xprt_call_ops, data);
2857 data->xps->xps_nunique_destaddr_xprts++;
2862 EXPORT_SYMBOL_GPL(rpc_clnt_test_and_add_xprt);
2865 * rpc_clnt_setup_test_and_add_xprt()
2867 * This is an rpc_clnt_add_xprt setup() function which returns 1 so:
2868 * 1) caller of the test function must dereference the rpc_xprt_switch
2870 * 2) test function must call rpc_xprt_switch_add_xprt, usually in
2871 * the rpc_call_done routine.
2873 * Upon success (return of 1), the test function adds the new
2874 * transport to the rpc_clnt xprt switch
2876 * @clnt: struct rpc_clnt to get the new transport
2877 * @xps: the rpc_xprt_switch to hold the new transport
2878 * @xprt: the rpc_xprt to test
2879 * @data: a struct rpc_add_xprt_test pointer that holds the test function
2880 * and test function call data
2882 int rpc_clnt_setup_test_and_add_xprt(struct rpc_clnt *clnt,
2883 struct rpc_xprt_switch *xps,
2884 struct rpc_xprt *xprt,
2887 struct rpc_task *task;
2888 struct rpc_add_xprt_test *xtest = (struct rpc_add_xprt_test *)data;
2889 int status = -EADDRINUSE;
2891 xprt = xprt_get(xprt);
2892 xprt_switch_get(xps);
2894 if (rpc_xprt_switch_has_addr(xps, (struct sockaddr *)&xprt->addr))
2897 /* Test the connection */
2898 task = rpc_call_null_helper(clnt, xprt, NULL, 0, NULL, NULL);
2900 status = PTR_ERR(task);
2903 status = task->tk_status;
2909 /* rpc_xprt_switch and rpc_xprt are deferrenced by add_xprt_test() */
2910 xtest->add_xprt_test(clnt, xprt, xtest->data);
2913 xprt_switch_put(xps);
2915 /* so that rpc_clnt_add_xprt does not call rpc_xprt_switch_add_xprt */
2919 xprt_switch_put(xps);
2920 pr_info("RPC: rpc_clnt_test_xprt failed: %d addr %s not added\n",
2921 status, xprt->address_strings[RPC_DISPLAY_ADDR]);
2924 EXPORT_SYMBOL_GPL(rpc_clnt_setup_test_and_add_xprt);
2927 * rpc_clnt_add_xprt - Add a new transport to a rpc_clnt
2928 * @clnt: pointer to struct rpc_clnt
2929 * @xprtargs: pointer to struct xprt_create
2930 * @setup: callback to test and/or set up the connection
2931 * @data: pointer to setup function data
2933 * Creates a new transport using the parameters set in args and
2935 * If ping is set, then test that connectivity succeeds before
2936 * adding the new transport.
2939 int rpc_clnt_add_xprt(struct rpc_clnt *clnt,
2940 struct xprt_create *xprtargs,
2941 int (*setup)(struct rpc_clnt *,
2942 struct rpc_xprt_switch *,
2947 struct rpc_xprt_switch *xps;
2948 struct rpc_xprt *xprt;
2949 unsigned long connect_timeout;
2950 unsigned long reconnect_timeout;
2951 unsigned char resvport, reuseport;
2955 xps = xprt_switch_get(rcu_dereference(clnt->cl_xpi.xpi_xpswitch));
2956 xprt = xprt_iter_xprt(&clnt->cl_xpi);
2957 if (xps == NULL || xprt == NULL) {
2959 xprt_switch_put(xps);
2962 resvport = xprt->resvport;
2963 reuseport = xprt->reuseport;
2964 connect_timeout = xprt->connect_timeout;
2965 reconnect_timeout = xprt->max_reconnect_timeout;
2966 ident = xprt->xprt_class->ident;
2969 if (!xprtargs->ident)
2970 xprtargs->ident = ident;
2971 xprt = xprt_create_transport(xprtargs);
2973 ret = PTR_ERR(xprt);
2974 goto out_put_switch;
2976 xprt->resvport = resvport;
2977 xprt->reuseport = reuseport;
2978 if (xprt->ops->set_connect_timeout != NULL)
2979 xprt->ops->set_connect_timeout(xprt,
2983 rpc_xprt_switch_set_roundrobin(xps);
2985 ret = setup(clnt, xps, xprt, data);
2989 rpc_xprt_switch_add_xprt(xps, xprt);
2993 xprt_switch_put(xps);
2996 EXPORT_SYMBOL_GPL(rpc_clnt_add_xprt);
2998 struct connect_timeout_data {
2999 unsigned long connect_timeout;
3000 unsigned long reconnect_timeout;
3004 rpc_xprt_set_connect_timeout(struct rpc_clnt *clnt,
3005 struct rpc_xprt *xprt,
3008 struct connect_timeout_data *timeo = data;
3010 if (xprt->ops->set_connect_timeout)
3011 xprt->ops->set_connect_timeout(xprt,
3012 timeo->connect_timeout,
3013 timeo->reconnect_timeout);
3018 rpc_set_connect_timeout(struct rpc_clnt *clnt,
3019 unsigned long connect_timeout,
3020 unsigned long reconnect_timeout)
3022 struct connect_timeout_data timeout = {
3023 .connect_timeout = connect_timeout,
3024 .reconnect_timeout = reconnect_timeout,
3026 rpc_clnt_iterate_for_each_xprt(clnt,
3027 rpc_xprt_set_connect_timeout,
3030 EXPORT_SYMBOL_GPL(rpc_set_connect_timeout);
3032 void rpc_clnt_xprt_switch_put(struct rpc_clnt *clnt)
3035 xprt_switch_put(rcu_dereference(clnt->cl_xpi.xpi_xpswitch));
3038 EXPORT_SYMBOL_GPL(rpc_clnt_xprt_switch_put);
3040 void rpc_clnt_xprt_switch_add_xprt(struct rpc_clnt *clnt, struct rpc_xprt *xprt)
3043 rpc_xprt_switch_add_xprt(rcu_dereference(clnt->cl_xpi.xpi_xpswitch),
3047 EXPORT_SYMBOL_GPL(rpc_clnt_xprt_switch_add_xprt);
3049 bool rpc_clnt_xprt_switch_has_addr(struct rpc_clnt *clnt,
3050 const struct sockaddr *sap)
3052 struct rpc_xprt_switch *xps;
3056 xps = rcu_dereference(clnt->cl_xpi.xpi_xpswitch);
3057 ret = rpc_xprt_switch_has_addr(xps, sap);
3061 EXPORT_SYMBOL_GPL(rpc_clnt_xprt_switch_has_addr);
3063 #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
3064 static void rpc_show_header(void)
3066 printk(KERN_INFO "-pid- flgs status -client- --rqstp- "
3067 "-timeout ---ops--\n");
3070 static void rpc_show_task(const struct rpc_clnt *clnt,
3071 const struct rpc_task *task)
3073 const char *rpc_waitq = "none";
3075 if (RPC_IS_QUEUED(task))
3076 rpc_waitq = rpc_qname(task->tk_waitqueue);
3078 printk(KERN_INFO "%5u %04x %6d %8p %8p %8ld %8p %sv%u %s a:%ps q:%s\n",
3079 task->tk_pid, task->tk_flags, task->tk_status,
3080 clnt, task->tk_rqstp, rpc_task_timeout(task), task->tk_ops,
3081 clnt->cl_program->name, clnt->cl_vers, rpc_proc_name(task),
3082 task->tk_action, rpc_waitq);
3085 void rpc_show_tasks(struct net *net)
3087 struct rpc_clnt *clnt;
3088 struct rpc_task *task;
3090 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
3092 spin_lock(&sn->rpc_client_lock);
3093 list_for_each_entry(clnt, &sn->all_clients, cl_clients) {
3094 spin_lock(&clnt->cl_lock);
3095 list_for_each_entry(task, &clnt->cl_tasks, tk_task) {
3100 rpc_show_task(clnt, task);
3102 spin_unlock(&clnt->cl_lock);
3104 spin_unlock(&sn->rpc_client_lock);
3108 #if IS_ENABLED(CONFIG_SUNRPC_SWAP)
3110 rpc_clnt_swap_activate_callback(struct rpc_clnt *clnt,
3111 struct rpc_xprt *xprt,
3114 return xprt_enable_swap(xprt);
3118 rpc_clnt_swap_activate(struct rpc_clnt *clnt)
3120 if (atomic_inc_return(&clnt->cl_swapper) == 1)
3121 return rpc_clnt_iterate_for_each_xprt(clnt,
3122 rpc_clnt_swap_activate_callback, NULL);
3125 EXPORT_SYMBOL_GPL(rpc_clnt_swap_activate);
3128 rpc_clnt_swap_deactivate_callback(struct rpc_clnt *clnt,
3129 struct rpc_xprt *xprt,
3132 xprt_disable_swap(xprt);
3137 rpc_clnt_swap_deactivate(struct rpc_clnt *clnt)
3139 if (atomic_dec_if_positive(&clnt->cl_swapper) == 0)
3140 rpc_clnt_iterate_for_each_xprt(clnt,
3141 rpc_clnt_swap_deactivate_callback, NULL);
3143 EXPORT_SYMBOL_GPL(rpc_clnt_swap_deactivate);
3144 #endif /* CONFIG_SUNRPC_SWAP */