2 * linux/net/sunrpc/clnt.c
4 * This file contains the high-level RPC interface.
5 * It is modeled as a finite state machine to support both synchronous
6 * and asynchronous requests.
8 * - RPC header generation and argument serialization.
9 * - Credential refresh.
10 * - TCP connect handling.
11 * - Retry of operation when it is suspected the operation failed because
12 * of uid squashing on the server, or when the credentials were stale
13 * and need to be refreshed, or when a packet was damaged in transit.
14 * This may be have to be moved to the VFS layer.
16 * Copyright (C) 1992,1993 Rick Sladkey <jrs@world.std.com>
17 * Copyright (C) 1995,1996 Olaf Kirch <okir@monad.swb.de>
21 #include <linux/module.h>
22 #include <linux/types.h>
23 #include <linux/kallsyms.h>
25 #include <linux/namei.h>
26 #include <linux/mount.h>
27 #include <linux/slab.h>
28 #include <linux/rcupdate.h>
29 #include <linux/utsname.h>
30 #include <linux/workqueue.h>
32 #include <linux/in6.h>
35 #include <linux/sunrpc/clnt.h>
36 #include <linux/sunrpc/addr.h>
37 #include <linux/sunrpc/rpc_pipe_fs.h>
38 #include <linux/sunrpc/metrics.h>
39 #include <linux/sunrpc/bc_xprt.h>
40 #include <trace/events/sunrpc.h>
45 #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
46 # define RPCDBG_FACILITY RPCDBG_CALL
49 #define dprint_status(t) \
50 dprintk("RPC: %5u %s (status %d)\n", t->tk_pid, \
51 __func__, t->tk_status)
54 * All RPC clients are linked into this list
57 static DECLARE_WAIT_QUEUE_HEAD(destroy_wait);
60 static void call_start(struct rpc_task *task);
61 static void call_reserve(struct rpc_task *task);
62 static void call_reserveresult(struct rpc_task *task);
63 static void call_allocate(struct rpc_task *task);
64 static void call_encode(struct rpc_task *task);
65 static void call_decode(struct rpc_task *task);
66 static void call_bind(struct rpc_task *task);
67 static void call_bind_status(struct rpc_task *task);
68 static void call_transmit(struct rpc_task *task);
69 static void call_status(struct rpc_task *task);
70 static void call_transmit_status(struct rpc_task *task);
71 static void call_refresh(struct rpc_task *task);
72 static void call_refreshresult(struct rpc_task *task);
73 static void call_connect(struct rpc_task *task);
74 static void call_connect_status(struct rpc_task *task);
76 static int rpc_encode_header(struct rpc_task *task,
77 struct xdr_stream *xdr);
78 static int rpc_decode_header(struct rpc_task *task,
79 struct xdr_stream *xdr);
80 static int rpc_ping(struct rpc_clnt *clnt);
81 static void rpc_check_timeout(struct rpc_task *task);
83 static void rpc_register_client(struct rpc_clnt *clnt)
85 struct net *net = rpc_net_ns(clnt);
86 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
88 spin_lock(&sn->rpc_client_lock);
89 list_add(&clnt->cl_clients, &sn->all_clients);
90 spin_unlock(&sn->rpc_client_lock);
93 static void rpc_unregister_client(struct rpc_clnt *clnt)
95 struct net *net = rpc_net_ns(clnt);
96 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
98 spin_lock(&sn->rpc_client_lock);
99 list_del(&clnt->cl_clients);
100 spin_unlock(&sn->rpc_client_lock);
103 static void __rpc_clnt_remove_pipedir(struct rpc_clnt *clnt)
105 rpc_remove_client_dir(clnt);
108 static void rpc_clnt_remove_pipedir(struct rpc_clnt *clnt)
110 struct net *net = rpc_net_ns(clnt);
111 struct super_block *pipefs_sb;
113 pipefs_sb = rpc_get_sb_net(net);
115 __rpc_clnt_remove_pipedir(clnt);
120 static struct dentry *rpc_setup_pipedir_sb(struct super_block *sb,
121 struct rpc_clnt *clnt)
123 static uint32_t clntid;
124 const char *dir_name = clnt->cl_program->pipe_dir_name;
126 struct dentry *dir, *dentry;
128 dir = rpc_d_lookup_sb(sb, dir_name);
130 pr_info("RPC: pipefs directory doesn't exist: %s\n", dir_name);
134 snprintf(name, sizeof(name), "clnt%x", (unsigned int)clntid++);
135 name[sizeof(name) - 1] = '\0';
136 dentry = rpc_create_client_dir(dir, name, clnt);
139 if (dentry == ERR_PTR(-EEXIST))
141 printk(KERN_INFO "RPC: Couldn't create pipefs entry"
142 " %s/%s, error %ld\n",
143 dir_name, name, PTR_ERR(dentry));
151 rpc_setup_pipedir(struct super_block *pipefs_sb, struct rpc_clnt *clnt)
153 struct dentry *dentry;
155 if (clnt->cl_program->pipe_dir_name != NULL) {
156 dentry = rpc_setup_pipedir_sb(pipefs_sb, clnt);
158 return PTR_ERR(dentry);
163 static int rpc_clnt_skip_event(struct rpc_clnt *clnt, unsigned long event)
165 if (clnt->cl_program->pipe_dir_name == NULL)
169 case RPC_PIPEFS_MOUNT:
170 if (clnt->cl_pipedir_objects.pdh_dentry != NULL)
172 if (atomic_read(&clnt->cl_count) == 0)
175 case RPC_PIPEFS_UMOUNT:
176 if (clnt->cl_pipedir_objects.pdh_dentry == NULL)
183 static int __rpc_clnt_handle_event(struct rpc_clnt *clnt, unsigned long event,
184 struct super_block *sb)
186 struct dentry *dentry;
189 case RPC_PIPEFS_MOUNT:
190 dentry = rpc_setup_pipedir_sb(sb, clnt);
194 return PTR_ERR(dentry);
196 case RPC_PIPEFS_UMOUNT:
197 __rpc_clnt_remove_pipedir(clnt);
200 printk(KERN_ERR "%s: unknown event: %ld\n", __func__, event);
206 static int __rpc_pipefs_event(struct rpc_clnt *clnt, unsigned long event,
207 struct super_block *sb)
211 for (;; clnt = clnt->cl_parent) {
212 if (!rpc_clnt_skip_event(clnt, event))
213 error = __rpc_clnt_handle_event(clnt, event, sb);
214 if (error || clnt == clnt->cl_parent)
220 static struct rpc_clnt *rpc_get_client_for_event(struct net *net, int event)
222 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
223 struct rpc_clnt *clnt;
225 spin_lock(&sn->rpc_client_lock);
226 list_for_each_entry(clnt, &sn->all_clients, cl_clients) {
227 if (rpc_clnt_skip_event(clnt, event))
229 spin_unlock(&sn->rpc_client_lock);
232 spin_unlock(&sn->rpc_client_lock);
236 static int rpc_pipefs_event(struct notifier_block *nb, unsigned long event,
239 struct super_block *sb = ptr;
240 struct rpc_clnt *clnt;
243 while ((clnt = rpc_get_client_for_event(sb->s_fs_info, event))) {
244 error = __rpc_pipefs_event(clnt, event, sb);
251 static struct notifier_block rpc_clients_block = {
252 .notifier_call = rpc_pipefs_event,
253 .priority = SUNRPC_PIPEFS_RPC_PRIO,
256 int rpc_clients_notifier_register(void)
258 return rpc_pipefs_notifier_register(&rpc_clients_block);
261 void rpc_clients_notifier_unregister(void)
263 return rpc_pipefs_notifier_unregister(&rpc_clients_block);
266 static struct rpc_xprt *rpc_clnt_set_transport(struct rpc_clnt *clnt,
267 struct rpc_xprt *xprt,
268 const struct rpc_timeout *timeout)
270 struct rpc_xprt *old;
272 spin_lock(&clnt->cl_lock);
273 old = rcu_dereference_protected(clnt->cl_xprt,
274 lockdep_is_held(&clnt->cl_lock));
276 if (!xprt_bound(xprt))
277 clnt->cl_autobind = 1;
279 clnt->cl_timeout = timeout;
280 rcu_assign_pointer(clnt->cl_xprt, xprt);
281 spin_unlock(&clnt->cl_lock);
286 static void rpc_clnt_set_nodename(struct rpc_clnt *clnt, const char *nodename)
288 clnt->cl_nodelen = strlcpy(clnt->cl_nodename,
289 nodename, sizeof(clnt->cl_nodename));
292 static int rpc_client_register(struct rpc_clnt *clnt,
293 rpc_authflavor_t pseudoflavor,
294 const char *client_name)
296 struct rpc_auth_create_args auth_args = {
297 .pseudoflavor = pseudoflavor,
298 .target_name = client_name,
300 struct rpc_auth *auth;
301 struct net *net = rpc_net_ns(clnt);
302 struct super_block *pipefs_sb;
305 rpc_clnt_debugfs_register(clnt);
307 pipefs_sb = rpc_get_sb_net(net);
309 err = rpc_setup_pipedir(pipefs_sb, clnt);
314 rpc_register_client(clnt);
318 auth = rpcauth_create(&auth_args, clnt);
320 dprintk("RPC: Couldn't create auth handle (flavor %u)\n",
327 pipefs_sb = rpc_get_sb_net(net);
328 rpc_unregister_client(clnt);
329 __rpc_clnt_remove_pipedir(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;
372 /* sanity check the name before trying to print it */
373 dprintk("RPC: creating %s client for %s (xprt %p)\n",
374 program->name, args->servername, xprt);
381 if (args->version >= program->nrvers)
383 version = program->version[args->version];
388 clnt = kzalloc(sizeof(*clnt), GFP_KERNEL);
391 clnt->cl_parent = parent ? : clnt;
393 err = rpc_alloc_clid(clnt);
397 clnt->cl_procinfo = version->procs;
398 clnt->cl_maxproc = version->nrprocs;
399 clnt->cl_prog = args->prognumber ? : program->number;
400 clnt->cl_vers = version->number;
401 clnt->cl_stats = program->stats;
402 clnt->cl_metrics = rpc_alloc_iostats(clnt);
403 rpc_init_pipe_dir_head(&clnt->cl_pipedir_objects);
405 if (clnt->cl_metrics == NULL)
407 clnt->cl_program = program;
408 INIT_LIST_HEAD(&clnt->cl_tasks);
409 spin_lock_init(&clnt->cl_lock);
411 timeout = xprt->timeout;
412 if (args->timeout != NULL) {
413 memcpy(&clnt->cl_timeout_default, args->timeout,
414 sizeof(clnt->cl_timeout_default));
415 timeout = &clnt->cl_timeout_default;
418 rpc_clnt_set_transport(clnt, xprt, timeout);
419 xprt_iter_init(&clnt->cl_xpi, xps);
420 xprt_switch_put(xps);
422 clnt->cl_rtt = &clnt->cl_rtt_default;
423 rpc_init_rtt(&clnt->cl_rtt_default, clnt->cl_timeout->to_initval);
425 atomic_set(&clnt->cl_count, 1);
427 if (nodename == NULL)
428 nodename = utsname()->nodename;
429 /* save the nodename */
430 rpc_clnt_set_nodename(clnt, nodename);
432 err = rpc_client_register(clnt, args->authflavor, args->client_name);
436 atomic_inc(&parent->cl_count);
440 rpc_free_iostats(clnt->cl_metrics);
448 xprt_switch_put(xps);
453 static struct rpc_clnt *rpc_create_xprt(struct rpc_create_args *args,
454 struct rpc_xprt *xprt)
456 struct rpc_clnt *clnt = NULL;
457 struct rpc_xprt_switch *xps;
459 if (args->bc_xprt && args->bc_xprt->xpt_bc_xps) {
460 WARN_ON_ONCE(!(args->protocol & XPRT_TRANSPORT_BC));
461 xps = args->bc_xprt->xpt_bc_xps;
462 xprt_switch_get(xps);
464 xps = xprt_switch_alloc(xprt, GFP_KERNEL);
467 return ERR_PTR(-ENOMEM);
470 xprt_switch_get(xps);
471 xprt->bc_xprt->xpt_bc_xps = xps;
474 clnt = rpc_new_client(args, xps, xprt, NULL);
478 if (!(args->flags & RPC_CLNT_CREATE_NOPING)) {
479 int err = rpc_ping(clnt);
481 rpc_shutdown_client(clnt);
486 clnt->cl_softrtry = 1;
487 if (args->flags & RPC_CLNT_CREATE_HARDRTRY)
488 clnt->cl_softrtry = 0;
490 if (args->flags & RPC_CLNT_CREATE_AUTOBIND)
491 clnt->cl_autobind = 1;
492 if (args->flags & RPC_CLNT_CREATE_NO_RETRANS_TIMEOUT)
493 clnt->cl_noretranstimeo = 1;
494 if (args->flags & RPC_CLNT_CREATE_DISCRTRY)
495 clnt->cl_discrtry = 1;
496 if (!(args->flags & RPC_CLNT_CREATE_QUIET))
503 * rpc_create - create an RPC client and transport with one call
504 * @args: rpc_clnt create argument structure
506 * Creates and initializes an RPC transport and an RPC client.
508 * It can ping the server in order to determine if it is up, and to see if
509 * it supports this program and version. RPC_CLNT_CREATE_NOPING disables
510 * this behavior so asynchronous tasks can also use rpc_create.
512 struct rpc_clnt *rpc_create(struct rpc_create_args *args)
514 struct rpc_xprt *xprt;
515 struct xprt_create xprtargs = {
517 .ident = args->protocol,
518 .srcaddr = args->saddress,
519 .dstaddr = args->address,
520 .addrlen = args->addrsize,
521 .servername = args->servername,
522 .bc_xprt = args->bc_xprt,
527 WARN_ON_ONCE(!(args->protocol & XPRT_TRANSPORT_BC));
528 xprt = args->bc_xprt->xpt_bc_xprt;
531 return rpc_create_xprt(args, xprt);
535 if (args->flags & RPC_CLNT_CREATE_INFINITE_SLOTS)
536 xprtargs.flags |= XPRT_CREATE_INFINITE_SLOTS;
537 if (args->flags & RPC_CLNT_CREATE_NO_IDLE_TIMEOUT)
538 xprtargs.flags |= XPRT_CREATE_NO_IDLE_TIMEOUT;
540 * If the caller chooses not to specify a hostname, whip
541 * up a string representation of the passed-in address.
543 if (xprtargs.servername == NULL) {
544 struct sockaddr_un *sun =
545 (struct sockaddr_un *)args->address;
546 struct sockaddr_in *sin =
547 (struct sockaddr_in *)args->address;
548 struct sockaddr_in6 *sin6 =
549 (struct sockaddr_in6 *)args->address;
551 servername[0] = '\0';
552 switch (args->address->sa_family) {
554 snprintf(servername, sizeof(servername), "%s",
558 snprintf(servername, sizeof(servername), "%pI4",
559 &sin->sin_addr.s_addr);
562 snprintf(servername, sizeof(servername), "%pI6",
566 /* caller wants default server name, but
567 * address family isn't recognized. */
568 return ERR_PTR(-EINVAL);
570 xprtargs.servername = servername;
573 xprt = xprt_create_transport(&xprtargs);
575 return (struct rpc_clnt *)xprt;
578 * By default, kernel RPC client connects from a reserved port.
579 * CAP_NET_BIND_SERVICE will not be set for unprivileged requesters,
580 * but it is always enabled for rpciod, which handles the connect
584 if (args->flags & RPC_CLNT_CREATE_NONPRIVPORT)
587 return rpc_create_xprt(args, xprt);
589 EXPORT_SYMBOL_GPL(rpc_create);
592 * This function clones the RPC client structure. It allows us to share the
593 * same transport while varying parameters such as the authentication
596 static struct rpc_clnt *__rpc_clone_client(struct rpc_create_args *args,
597 struct rpc_clnt *clnt)
599 struct rpc_xprt_switch *xps;
600 struct rpc_xprt *xprt;
601 struct rpc_clnt *new;
606 xprt = xprt_get(rcu_dereference(clnt->cl_xprt));
607 xps = xprt_switch_get(rcu_dereference(clnt->cl_xpi.xpi_xpswitch));
609 if (xprt == NULL || xps == NULL) {
611 xprt_switch_put(xps);
614 args->servername = xprt->servername;
615 args->nodename = clnt->cl_nodename;
617 new = rpc_new_client(args, xps, xprt, clnt);
623 /* Turn off autobind on clones */
624 new->cl_autobind = 0;
625 new->cl_softrtry = clnt->cl_softrtry;
626 new->cl_noretranstimeo = clnt->cl_noretranstimeo;
627 new->cl_discrtry = clnt->cl_discrtry;
628 new->cl_chatty = clnt->cl_chatty;
629 new->cl_principal = clnt->cl_principal;
633 dprintk("RPC: %s: returned error %d\n", __func__, err);
638 * rpc_clone_client - Clone an RPC client structure
640 * @clnt: RPC client whose parameters are copied
642 * Returns a fresh RPC client or an ERR_PTR.
644 struct rpc_clnt *rpc_clone_client(struct rpc_clnt *clnt)
646 struct rpc_create_args args = {
647 .program = clnt->cl_program,
648 .prognumber = clnt->cl_prog,
649 .version = clnt->cl_vers,
650 .authflavor = clnt->cl_auth->au_flavor,
652 return __rpc_clone_client(&args, clnt);
654 EXPORT_SYMBOL_GPL(rpc_clone_client);
657 * rpc_clone_client_set_auth - Clone an RPC client structure and set its auth
659 * @clnt: RPC client whose parameters are copied
660 * @flavor: security flavor for new client
662 * Returns a fresh RPC client or an ERR_PTR.
665 rpc_clone_client_set_auth(struct rpc_clnt *clnt, rpc_authflavor_t flavor)
667 struct rpc_create_args args = {
668 .program = clnt->cl_program,
669 .prognumber = clnt->cl_prog,
670 .version = clnt->cl_vers,
671 .authflavor = flavor,
673 return __rpc_clone_client(&args, clnt);
675 EXPORT_SYMBOL_GPL(rpc_clone_client_set_auth);
678 * rpc_switch_client_transport: switch the RPC transport on the fly
679 * @clnt: pointer to a struct rpc_clnt
680 * @args: pointer to the new transport arguments
681 * @timeout: pointer to the new timeout parameters
683 * This function allows the caller to switch the RPC transport for the
684 * rpc_clnt structure 'clnt' to allow it to connect to a mirrored NFS
685 * server, for instance. It assumes that the caller has ensured that
686 * there are no active RPC tasks by using some form of locking.
688 * Returns zero if "clnt" is now using the new xprt. Otherwise a
689 * negative errno is returned, and "clnt" continues to use the old
692 int rpc_switch_client_transport(struct rpc_clnt *clnt,
693 struct xprt_create *args,
694 const struct rpc_timeout *timeout)
696 const struct rpc_timeout *old_timeo;
697 rpc_authflavor_t pseudoflavor;
698 struct rpc_xprt_switch *xps, *oldxps;
699 struct rpc_xprt *xprt, *old;
700 struct rpc_clnt *parent;
703 xprt = xprt_create_transport(args);
705 dprintk("RPC: failed to create new xprt for clnt %p\n",
707 return PTR_ERR(xprt);
710 xps = xprt_switch_alloc(xprt, GFP_KERNEL);
716 pseudoflavor = clnt->cl_auth->au_flavor;
718 old_timeo = clnt->cl_timeout;
719 old = rpc_clnt_set_transport(clnt, xprt, timeout);
720 oldxps = xprt_iter_xchg_switch(&clnt->cl_xpi, xps);
722 rpc_unregister_client(clnt);
723 __rpc_clnt_remove_pipedir(clnt);
724 rpc_clnt_debugfs_unregister(clnt);
727 * A new transport was created. "clnt" therefore
728 * becomes the root of a new cl_parent tree. clnt's
729 * children, if it has any, still point to the old xprt.
731 parent = clnt->cl_parent;
732 clnt->cl_parent = clnt;
735 * The old rpc_auth cache cannot be re-used. GSS
736 * contexts in particular are between a single
739 err = rpc_client_register(clnt, pseudoflavor, NULL);
745 rpc_release_client(parent);
746 xprt_switch_put(oldxps);
748 dprintk("RPC: replaced xprt for clnt %p\n", clnt);
752 xps = xprt_iter_xchg_switch(&clnt->cl_xpi, oldxps);
753 rpc_clnt_set_transport(clnt, old, old_timeo);
754 clnt->cl_parent = parent;
755 rpc_client_register(clnt, pseudoflavor, NULL);
756 xprt_switch_put(xps);
758 dprintk("RPC: failed to switch xprt for clnt %p\n", clnt);
761 EXPORT_SYMBOL_GPL(rpc_switch_client_transport);
764 int rpc_clnt_xprt_iter_init(struct rpc_clnt *clnt, struct rpc_xprt_iter *xpi)
766 struct rpc_xprt_switch *xps;
769 xps = xprt_switch_get(rcu_dereference(clnt->cl_xpi.xpi_xpswitch));
773 xprt_iter_init_listall(xpi, xps);
774 xprt_switch_put(xps);
779 * rpc_clnt_iterate_for_each_xprt - Apply a function to all transports
780 * @clnt: pointer to client
781 * @fn: function to apply
782 * @data: void pointer to function data
784 * Iterates through the list of RPC transports currently attached to the
785 * client and applies the function fn(clnt, xprt, data).
787 * On error, the iteration stops, and the function returns the error value.
789 int rpc_clnt_iterate_for_each_xprt(struct rpc_clnt *clnt,
790 int (*fn)(struct rpc_clnt *, struct rpc_xprt *, void *),
793 struct rpc_xprt_iter xpi;
796 ret = rpc_clnt_xprt_iter_init(clnt, &xpi);
800 struct rpc_xprt *xprt = xprt_iter_get_next(&xpi);
804 ret = fn(clnt, xprt, data);
809 xprt_iter_destroy(&xpi);
812 EXPORT_SYMBOL_GPL(rpc_clnt_iterate_for_each_xprt);
815 * Kill all tasks for the given client.
816 * XXX: kill their descendants as well?
818 void rpc_killall_tasks(struct rpc_clnt *clnt)
820 struct rpc_task *rovr;
823 if (list_empty(&clnt->cl_tasks))
825 dprintk("RPC: killing all tasks for client %p\n", clnt);
827 * Spin lock all_tasks to prevent changes...
829 spin_lock(&clnt->cl_lock);
830 list_for_each_entry(rovr, &clnt->cl_tasks, tk_task) {
831 if (!RPC_IS_ACTIVATED(rovr))
833 if (!(rovr->tk_flags & RPC_TASK_KILLED)) {
834 rovr->tk_flags |= RPC_TASK_KILLED;
835 rpc_exit(rovr, -EIO);
838 spin_unlock(&clnt->cl_lock);
840 EXPORT_SYMBOL_GPL(rpc_killall_tasks);
843 * Properly shut down an RPC client, terminating all outstanding
846 void rpc_shutdown_client(struct rpc_clnt *clnt)
850 dprintk_rcu("RPC: shutting down %s client for %s\n",
851 clnt->cl_program->name,
852 rcu_dereference(clnt->cl_xprt)->servername);
854 while (!list_empty(&clnt->cl_tasks)) {
855 rpc_killall_tasks(clnt);
856 wait_event_timeout(destroy_wait,
857 list_empty(&clnt->cl_tasks), 1*HZ);
860 rpc_release_client(clnt);
862 EXPORT_SYMBOL_GPL(rpc_shutdown_client);
867 static struct rpc_clnt *
868 rpc_free_client(struct rpc_clnt *clnt)
870 struct rpc_clnt *parent = NULL;
872 dprintk_rcu("RPC: destroying %s client for %s\n",
873 clnt->cl_program->name,
874 rcu_dereference(clnt->cl_xprt)->servername);
875 if (clnt->cl_parent != clnt)
876 parent = clnt->cl_parent;
877 rpc_clnt_debugfs_unregister(clnt);
878 rpc_clnt_remove_pipedir(clnt);
879 rpc_unregister_client(clnt);
880 rpc_free_iostats(clnt->cl_metrics);
881 clnt->cl_metrics = NULL;
882 xprt_put(rcu_dereference_raw(clnt->cl_xprt));
883 xprt_iter_destroy(&clnt->cl_xpi);
893 static struct rpc_clnt *
894 rpc_free_auth(struct rpc_clnt *clnt)
896 if (clnt->cl_auth == NULL)
897 return rpc_free_client(clnt);
900 * Note: RPCSEC_GSS may need to send NULL RPC calls in order to
901 * release remaining GSS contexts. This mechanism ensures
902 * that it can do so safely.
904 atomic_inc(&clnt->cl_count);
905 rpcauth_release(clnt->cl_auth);
906 clnt->cl_auth = NULL;
907 if (atomic_dec_and_test(&clnt->cl_count))
908 return rpc_free_client(clnt);
913 * Release reference to the RPC client
916 rpc_release_client(struct rpc_clnt *clnt)
918 dprintk("RPC: rpc_release_client(%p)\n", clnt);
921 if (list_empty(&clnt->cl_tasks))
922 wake_up(&destroy_wait);
923 if (!atomic_dec_and_test(&clnt->cl_count))
925 clnt = rpc_free_auth(clnt);
926 } while (clnt != NULL);
928 EXPORT_SYMBOL_GPL(rpc_release_client);
931 * rpc_bind_new_program - bind a new RPC program to an existing client
932 * @old: old rpc_client
933 * @program: rpc program to set
934 * @vers: rpc program version
936 * Clones the rpc client and sets up a new RPC program. This is mainly
937 * of use for enabling different RPC programs to share the same transport.
938 * The Sun NFSv2/v3 ACL protocol can do this.
940 struct rpc_clnt *rpc_bind_new_program(struct rpc_clnt *old,
941 const struct rpc_program *program,
944 struct rpc_create_args args = {
946 .prognumber = program->number,
948 .authflavor = old->cl_auth->au_flavor,
950 struct rpc_clnt *clnt;
953 clnt = __rpc_clone_client(&args, old);
956 err = rpc_ping(clnt);
958 rpc_shutdown_client(clnt);
964 EXPORT_SYMBOL_GPL(rpc_bind_new_program);
966 void rpc_task_release_transport(struct rpc_task *task)
968 struct rpc_xprt *xprt = task->tk_xprt;
971 task->tk_xprt = NULL;
975 EXPORT_SYMBOL_GPL(rpc_task_release_transport);
977 void rpc_task_release_client(struct rpc_task *task)
979 struct rpc_clnt *clnt = task->tk_client;
982 /* Remove from client task list */
983 spin_lock(&clnt->cl_lock);
984 list_del(&task->tk_task);
985 spin_unlock(&clnt->cl_lock);
986 task->tk_client = NULL;
988 rpc_release_client(clnt);
990 rpc_task_release_transport(task);
994 void rpc_task_set_transport(struct rpc_task *task, struct rpc_clnt *clnt)
997 task->tk_xprt = xprt_iter_get_next(&clnt->cl_xpi);
1001 void rpc_task_set_client(struct rpc_task *task, struct rpc_clnt *clnt)
1005 rpc_task_set_transport(task, clnt);
1006 task->tk_client = clnt;
1007 atomic_inc(&clnt->cl_count);
1008 if (clnt->cl_softrtry)
1009 task->tk_flags |= RPC_TASK_SOFT;
1010 if (clnt->cl_noretranstimeo)
1011 task->tk_flags |= RPC_TASK_NO_RETRANS_TIMEOUT;
1012 if (atomic_read(&clnt->cl_swapper))
1013 task->tk_flags |= RPC_TASK_SWAPPER;
1014 /* Add to the client's list of all tasks */
1015 spin_lock(&clnt->cl_lock);
1016 list_add_tail(&task->tk_task, &clnt->cl_tasks);
1017 spin_unlock(&clnt->cl_lock);
1022 rpc_task_set_rpc_message(struct rpc_task *task, const struct rpc_message *msg)
1025 task->tk_msg.rpc_proc = msg->rpc_proc;
1026 task->tk_msg.rpc_argp = msg->rpc_argp;
1027 task->tk_msg.rpc_resp = msg->rpc_resp;
1028 if (msg->rpc_cred != NULL)
1029 task->tk_msg.rpc_cred = get_cred(msg->rpc_cred);
1034 * Default callback for async RPC calls
1037 rpc_default_callback(struct rpc_task *task, void *data)
1041 static const struct rpc_call_ops rpc_default_ops = {
1042 .rpc_call_done = rpc_default_callback,
1046 * rpc_run_task - Allocate a new RPC task, then run rpc_execute against it
1047 * @task_setup_data: pointer to task initialisation data
1049 struct rpc_task *rpc_run_task(const struct rpc_task_setup *task_setup_data)
1051 struct rpc_task *task;
1053 task = rpc_new_task(task_setup_data);
1055 rpc_task_set_client(task, task_setup_data->rpc_client);
1056 rpc_task_set_rpc_message(task, task_setup_data->rpc_message);
1058 if (task->tk_action == NULL)
1059 rpc_call_start(task);
1061 atomic_inc(&task->tk_count);
1065 EXPORT_SYMBOL_GPL(rpc_run_task);
1068 * rpc_call_sync - Perform a synchronous RPC call
1069 * @clnt: pointer to RPC client
1070 * @msg: RPC call parameters
1071 * @flags: RPC call flags
1073 int rpc_call_sync(struct rpc_clnt *clnt, const struct rpc_message *msg, int flags)
1075 struct rpc_task *task;
1076 struct rpc_task_setup task_setup_data = {
1079 .callback_ops = &rpc_default_ops,
1084 WARN_ON_ONCE(flags & RPC_TASK_ASYNC);
1085 if (flags & RPC_TASK_ASYNC) {
1086 rpc_release_calldata(task_setup_data.callback_ops,
1087 task_setup_data.callback_data);
1091 task = rpc_run_task(&task_setup_data);
1093 return PTR_ERR(task);
1094 status = task->tk_status;
1098 EXPORT_SYMBOL_GPL(rpc_call_sync);
1101 * rpc_call_async - Perform an asynchronous RPC call
1102 * @clnt: pointer to RPC client
1103 * @msg: RPC call parameters
1104 * @flags: RPC call flags
1105 * @tk_ops: RPC call ops
1106 * @data: user call data
1109 rpc_call_async(struct rpc_clnt *clnt, const struct rpc_message *msg, int flags,
1110 const struct rpc_call_ops *tk_ops, void *data)
1112 struct rpc_task *task;
1113 struct rpc_task_setup task_setup_data = {
1116 .callback_ops = tk_ops,
1117 .callback_data = data,
1118 .flags = flags|RPC_TASK_ASYNC,
1121 task = rpc_run_task(&task_setup_data);
1123 return PTR_ERR(task);
1127 EXPORT_SYMBOL_GPL(rpc_call_async);
1129 #if defined(CONFIG_SUNRPC_BACKCHANNEL)
1130 static void call_bc_encode(struct rpc_task *task);
1133 * rpc_run_bc_task - Allocate a new RPC task for backchannel use, then run
1134 * rpc_execute against it
1137 struct rpc_task *rpc_run_bc_task(struct rpc_rqst *req)
1139 struct rpc_task *task;
1140 struct rpc_task_setup task_setup_data = {
1141 .callback_ops = &rpc_default_ops,
1142 .flags = RPC_TASK_SOFTCONN |
1143 RPC_TASK_NO_RETRANS_TIMEOUT,
1146 dprintk("RPC: rpc_run_bc_task req= %p\n", req);
1148 * Create an rpc_task to send the data
1150 task = rpc_new_task(&task_setup_data);
1151 xprt_init_bc_request(req, task);
1153 task->tk_action = call_bc_encode;
1154 atomic_inc(&task->tk_count);
1155 WARN_ON_ONCE(atomic_read(&task->tk_count) != 2);
1158 dprintk("RPC: rpc_run_bc_task: task= %p\n", task);
1161 #endif /* CONFIG_SUNRPC_BACKCHANNEL */
1164 * rpc_prepare_reply_pages - Prepare to receive a reply data payload into pages
1165 * @req: RPC request to prepare
1166 * @pages: vector of struct page pointers
1167 * @base: offset in first page where receive should start, in bytes
1168 * @len: expected size of the upper layer data payload, in bytes
1169 * @hdrsize: expected size of upper layer reply header, in XDR words
1172 void rpc_prepare_reply_pages(struct rpc_rqst *req, struct page **pages,
1173 unsigned int base, unsigned int len,
1174 unsigned int hdrsize)
1176 /* Subtract one to force an extra word of buffer space for the
1177 * payload's XDR pad to fall into the rcv_buf's tail iovec.
1179 hdrsize += RPC_REPHDRSIZE + req->rq_cred->cr_auth->au_ralign - 1;
1181 xdr_inline_pages(&req->rq_rcv_buf, hdrsize << 2, pages, base, len);
1182 trace_rpc_reply_pages(req);
1184 EXPORT_SYMBOL_GPL(rpc_prepare_reply_pages);
1187 rpc_call_start(struct rpc_task *task)
1189 task->tk_action = call_start;
1191 EXPORT_SYMBOL_GPL(rpc_call_start);
1194 * rpc_peeraddr - extract remote peer address from clnt's xprt
1195 * @clnt: RPC client structure
1196 * @buf: target buffer
1197 * @bufsize: length of target buffer
1199 * Returns the number of bytes that are actually in the stored address.
1201 size_t rpc_peeraddr(struct rpc_clnt *clnt, struct sockaddr *buf, size_t bufsize)
1204 struct rpc_xprt *xprt;
1207 xprt = rcu_dereference(clnt->cl_xprt);
1209 bytes = xprt->addrlen;
1210 if (bytes > bufsize)
1212 memcpy(buf, &xprt->addr, bytes);
1217 EXPORT_SYMBOL_GPL(rpc_peeraddr);
1220 * rpc_peeraddr2str - return remote peer address in printable format
1221 * @clnt: RPC client structure
1222 * @format: address format
1224 * NB: the lifetime of the memory referenced by the returned pointer is
1225 * the same as the rpc_xprt itself. As long as the caller uses this
1226 * pointer, it must hold the RCU read lock.
1228 const char *rpc_peeraddr2str(struct rpc_clnt *clnt,
1229 enum rpc_display_format_t format)
1231 struct rpc_xprt *xprt;
1233 xprt = rcu_dereference(clnt->cl_xprt);
1235 if (xprt->address_strings[format] != NULL)
1236 return xprt->address_strings[format];
1238 return "unprintable";
1240 EXPORT_SYMBOL_GPL(rpc_peeraddr2str);
1242 static const struct sockaddr_in rpc_inaddr_loopback = {
1243 .sin_family = AF_INET,
1244 .sin_addr.s_addr = htonl(INADDR_ANY),
1247 static const struct sockaddr_in6 rpc_in6addr_loopback = {
1248 .sin6_family = AF_INET6,
1249 .sin6_addr = IN6ADDR_ANY_INIT,
1253 * Try a getsockname() on a connected datagram socket. Using a
1254 * connected datagram socket prevents leaving a socket in TIME_WAIT.
1255 * This conserves the ephemeral port number space.
1257 * Returns zero and fills in "buf" if successful; otherwise, a
1258 * negative errno is returned.
1260 static int rpc_sockname(struct net *net, struct sockaddr *sap, size_t salen,
1261 struct sockaddr *buf)
1263 struct socket *sock;
1266 err = __sock_create(net, sap->sa_family,
1267 SOCK_DGRAM, IPPROTO_UDP, &sock, 1);
1269 dprintk("RPC: can't create UDP socket (%d)\n", err);
1273 switch (sap->sa_family) {
1275 err = kernel_bind(sock,
1276 (struct sockaddr *)&rpc_inaddr_loopback,
1277 sizeof(rpc_inaddr_loopback));
1280 err = kernel_bind(sock,
1281 (struct sockaddr *)&rpc_in6addr_loopback,
1282 sizeof(rpc_in6addr_loopback));
1285 err = -EAFNOSUPPORT;
1289 dprintk("RPC: can't bind UDP socket (%d)\n", err);
1293 err = kernel_connect(sock, sap, salen, 0);
1295 dprintk("RPC: can't connect UDP socket (%d)\n", err);
1299 err = kernel_getsockname(sock, buf);
1301 dprintk("RPC: getsockname failed (%d)\n", err);
1306 if (buf->sa_family == AF_INET6) {
1307 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)buf;
1308 sin6->sin6_scope_id = 0;
1310 dprintk("RPC: %s succeeded\n", __func__);
1319 * Scraping a connected socket failed, so we don't have a useable
1320 * local address. Fallback: generate an address that will prevent
1321 * the server from calling us back.
1323 * Returns zero and fills in "buf" if successful; otherwise, a
1324 * negative errno is returned.
1326 static int rpc_anyaddr(int family, struct sockaddr *buf, size_t buflen)
1330 if (buflen < sizeof(rpc_inaddr_loopback))
1332 memcpy(buf, &rpc_inaddr_loopback,
1333 sizeof(rpc_inaddr_loopback));
1336 if (buflen < sizeof(rpc_in6addr_loopback))
1338 memcpy(buf, &rpc_in6addr_loopback,
1339 sizeof(rpc_in6addr_loopback));
1342 dprintk("RPC: %s: address family not supported\n",
1344 return -EAFNOSUPPORT;
1346 dprintk("RPC: %s: succeeded\n", __func__);
1351 * rpc_localaddr - discover local endpoint address for an RPC client
1352 * @clnt: RPC client structure
1353 * @buf: target buffer
1354 * @buflen: size of target buffer, in bytes
1356 * Returns zero and fills in "buf" and "buflen" if successful;
1357 * otherwise, a negative errno is returned.
1359 * This works even if the underlying transport is not currently connected,
1360 * or if the upper layer never previously provided a source address.
1362 * The result of this function call is transient: multiple calls in
1363 * succession may give different results, depending on how local
1364 * networking configuration changes over time.
1366 int rpc_localaddr(struct rpc_clnt *clnt, struct sockaddr *buf, size_t buflen)
1368 struct sockaddr_storage address;
1369 struct sockaddr *sap = (struct sockaddr *)&address;
1370 struct rpc_xprt *xprt;
1376 xprt = rcu_dereference(clnt->cl_xprt);
1377 salen = xprt->addrlen;
1378 memcpy(sap, &xprt->addr, salen);
1379 net = get_net(xprt->xprt_net);
1382 rpc_set_port(sap, 0);
1383 err = rpc_sockname(net, sap, salen, buf);
1386 /* Couldn't discover local address, return ANYADDR */
1387 return rpc_anyaddr(sap->sa_family, buf, buflen);
1390 EXPORT_SYMBOL_GPL(rpc_localaddr);
1393 rpc_setbufsize(struct rpc_clnt *clnt, unsigned int sndsize, unsigned int rcvsize)
1395 struct rpc_xprt *xprt;
1398 xprt = rcu_dereference(clnt->cl_xprt);
1399 if (xprt->ops->set_buffer_size)
1400 xprt->ops->set_buffer_size(xprt, sndsize, rcvsize);
1403 EXPORT_SYMBOL_GPL(rpc_setbufsize);
1406 * rpc_net_ns - Get the network namespace for this RPC client
1407 * @clnt: RPC client to query
1410 struct net *rpc_net_ns(struct rpc_clnt *clnt)
1415 ret = rcu_dereference(clnt->cl_xprt)->xprt_net;
1419 EXPORT_SYMBOL_GPL(rpc_net_ns);
1422 * rpc_max_payload - Get maximum payload size for a transport, in bytes
1423 * @clnt: RPC client to query
1425 * For stream transports, this is one RPC record fragment (see RFC
1426 * 1831), as we don't support multi-record requests yet. For datagram
1427 * transports, this is the size of an IP packet minus the IP, UDP, and
1430 size_t rpc_max_payload(struct rpc_clnt *clnt)
1435 ret = rcu_dereference(clnt->cl_xprt)->max_payload;
1439 EXPORT_SYMBOL_GPL(rpc_max_payload);
1442 * rpc_max_bc_payload - Get maximum backchannel payload size, in bytes
1443 * @clnt: RPC client to query
1445 size_t rpc_max_bc_payload(struct rpc_clnt *clnt)
1447 struct rpc_xprt *xprt;
1451 xprt = rcu_dereference(clnt->cl_xprt);
1452 ret = xprt->ops->bc_maxpayload(xprt);
1456 EXPORT_SYMBOL_GPL(rpc_max_bc_payload);
1459 * rpc_force_rebind - force transport to check that remote port is unchanged
1460 * @clnt: client to rebind
1463 void rpc_force_rebind(struct rpc_clnt *clnt)
1465 if (clnt->cl_autobind) {
1467 xprt_clear_bound(rcu_dereference(clnt->cl_xprt));
1471 EXPORT_SYMBOL_GPL(rpc_force_rebind);
1474 * Restart an (async) RPC call from the call_prepare state.
1475 * Usually called from within the exit handler.
1478 rpc_restart_call_prepare(struct rpc_task *task)
1480 if (RPC_ASSASSINATED(task))
1482 task->tk_action = call_start;
1483 task->tk_status = 0;
1484 if (task->tk_ops->rpc_call_prepare != NULL)
1485 task->tk_action = rpc_prepare_task;
1488 EXPORT_SYMBOL_GPL(rpc_restart_call_prepare);
1491 * Restart an (async) RPC call. Usually called from within the
1495 rpc_restart_call(struct rpc_task *task)
1497 if (RPC_ASSASSINATED(task))
1499 task->tk_action = call_start;
1500 task->tk_status = 0;
1503 EXPORT_SYMBOL_GPL(rpc_restart_call);
1506 *rpc_proc_name(const struct rpc_task *task)
1508 const struct rpc_procinfo *proc = task->tk_msg.rpc_proc;
1512 return proc->p_name;
1522 * Other FSM states can be visited zero or more times, but
1523 * this state is visited exactly once for each RPC.
1526 call_start(struct rpc_task *task)
1528 struct rpc_clnt *clnt = task->tk_client;
1529 int idx = task->tk_msg.rpc_proc->p_statidx;
1531 trace_rpc_request(task);
1532 dprintk("RPC: %5u call_start %s%d proc %s (%s)\n", task->tk_pid,
1533 clnt->cl_program->name, clnt->cl_vers,
1534 rpc_proc_name(task),
1535 (RPC_IS_ASYNC(task) ? "async" : "sync"));
1537 /* Increment call count (version might not be valid for ping) */
1538 if (clnt->cl_program->version[clnt->cl_vers])
1539 clnt->cl_program->version[clnt->cl_vers]->counts[idx]++;
1540 clnt->cl_stats->rpccnt++;
1541 task->tk_action = call_reserve;
1542 rpc_task_set_transport(task, clnt);
1546 * 1. Reserve an RPC call slot
1549 call_reserve(struct rpc_task *task)
1551 dprint_status(task);
1553 task->tk_status = 0;
1554 task->tk_action = call_reserveresult;
1558 static void call_retry_reserve(struct rpc_task *task);
1561 * 1b. Grok the result of xprt_reserve()
1564 call_reserveresult(struct rpc_task *task)
1566 int status = task->tk_status;
1568 dprint_status(task);
1571 * After a call to xprt_reserve(), we must have either
1572 * a request slot or else an error status.
1574 task->tk_status = 0;
1576 if (task->tk_rqstp) {
1577 task->tk_action = call_refresh;
1581 printk(KERN_ERR "%s: status=%d, but no request slot, exiting\n",
1583 rpc_exit(task, -EIO);
1588 * Even though there was an error, we may have acquired
1589 * a request slot somehow. Make sure not to leak it.
1591 if (task->tk_rqstp) {
1592 printk(KERN_ERR "%s: status=%d, request allocated anyway\n",
1599 rpc_delay(task, HZ >> 2);
1601 case -EAGAIN: /* woken up; retry */
1602 task->tk_action = call_retry_reserve;
1604 case -EIO: /* probably a shutdown */
1607 printk(KERN_ERR "%s: unrecognized error %d, exiting\n",
1611 rpc_exit(task, status);
1615 * 1c. Retry reserving an RPC call slot
1618 call_retry_reserve(struct rpc_task *task)
1620 dprint_status(task);
1622 task->tk_status = 0;
1623 task->tk_action = call_reserveresult;
1624 xprt_retry_reserve(task);
1628 * 2. Bind and/or refresh the credentials
1631 call_refresh(struct rpc_task *task)
1633 dprint_status(task);
1635 task->tk_action = call_refreshresult;
1636 task->tk_status = 0;
1637 task->tk_client->cl_stats->rpcauthrefresh++;
1638 rpcauth_refreshcred(task);
1642 * 2a. Process the results of a credential refresh
1645 call_refreshresult(struct rpc_task *task)
1647 int status = task->tk_status;
1649 dprint_status(task);
1651 task->tk_status = 0;
1652 task->tk_action = call_refresh;
1655 if (rpcauth_uptodatecred(task)) {
1656 task->tk_action = call_allocate;
1659 /* Use rate-limiting and a max number of retries if refresh
1660 * had status 0 but failed to update the cred.
1664 rpc_delay(task, 3*HZ);
1670 if (!task->tk_cred_retry)
1672 task->tk_cred_retry--;
1673 dprintk("RPC: %5u %s: retry refresh creds\n",
1674 task->tk_pid, __func__);
1677 dprintk("RPC: %5u %s: refresh creds failed with error %d\n",
1678 task->tk_pid, __func__, status);
1679 rpc_exit(task, status);
1683 * 2b. Allocate the buffer. For details, see sched.c:rpc_malloc.
1684 * (Note: buffer memory is freed in xprt_release).
1687 call_allocate(struct rpc_task *task)
1689 const struct rpc_auth *auth = task->tk_rqstp->rq_cred->cr_auth;
1690 struct rpc_rqst *req = task->tk_rqstp;
1691 struct rpc_xprt *xprt = req->rq_xprt;
1692 const struct rpc_procinfo *proc = task->tk_msg.rpc_proc;
1695 dprint_status(task);
1697 task->tk_status = 0;
1698 task->tk_action = call_encode;
1703 if (proc->p_proc != 0) {
1704 BUG_ON(proc->p_arglen == 0);
1705 if (proc->p_decode != NULL)
1706 BUG_ON(proc->p_replen == 0);
1710 * Calculate the size (in quads) of the RPC call
1711 * and reply headers, and convert both values
1714 req->rq_callsize = RPC_CALLHDRSIZE + (auth->au_cslack << 1) +
1716 req->rq_callsize <<= 2;
1718 * Note: the reply buffer must at minimum allocate enough space
1719 * for the 'struct accepted_reply' from RFC5531.
1721 req->rq_rcvsize = RPC_REPHDRSIZE + auth->au_rslack + \
1722 max_t(size_t, proc->p_replen, 2);
1723 req->rq_rcvsize <<= 2;
1725 status = xprt->ops->buf_alloc(task);
1726 xprt_inject_disconnect(xprt);
1729 if (status != -ENOMEM) {
1730 rpc_exit(task, status);
1734 dprintk("RPC: %5u rpc_buffer allocation failed\n", task->tk_pid);
1736 if (RPC_IS_ASYNC(task) || !fatal_signal_pending(current)) {
1737 task->tk_action = call_allocate;
1738 rpc_delay(task, HZ>>4);
1742 rpc_exit(task, -ERESTARTSYS);
1746 rpc_task_need_encode(struct rpc_task *task)
1748 return test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate) == 0 &&
1749 (!(task->tk_flags & RPC_TASK_SENT) ||
1750 !(task->tk_flags & RPC_TASK_NO_RETRANS_TIMEOUT) ||
1751 xprt_request_need_retransmit(task));
1755 rpc_xdr_encode(struct rpc_task *task)
1757 struct rpc_rqst *req = task->tk_rqstp;
1758 struct xdr_stream xdr;
1760 xdr_buf_init(&req->rq_snd_buf,
1763 xdr_buf_init(&req->rq_rcv_buf,
1767 req->rq_snd_buf.head[0].iov_len = 0;
1768 xdr_init_encode(&xdr, &req->rq_snd_buf,
1769 req->rq_snd_buf.head[0].iov_base, req);
1770 if (rpc_encode_header(task, &xdr))
1773 task->tk_status = rpcauth_wrap_req(task, &xdr);
1777 * 3. Encode arguments of an RPC call
1780 call_encode(struct rpc_task *task)
1782 if (!rpc_task_need_encode(task))
1784 dprint_status(task);
1785 /* Encode here so that rpcsec_gss can use correct sequence number. */
1786 rpc_xdr_encode(task);
1787 /* Did the encode result in an error condition? */
1788 if (task->tk_status != 0) {
1789 /* Was the error nonfatal? */
1790 switch (task->tk_status) {
1793 rpc_delay(task, HZ >> 4);
1796 task->tk_action = call_refresh;
1799 rpc_exit(task, task->tk_status);
1803 xprt_request_prepare(task->tk_rqstp);
1806 /* Add task to reply queue before transmission to avoid races */
1807 if (rpc_reply_expected(task))
1808 xprt_request_enqueue_receive(task);
1809 xprt_request_enqueue_transmit(task);
1811 task->tk_action = call_transmit;
1812 /* Check that the connection is OK */
1813 if (!xprt_bound(task->tk_xprt))
1814 task->tk_action = call_bind;
1815 else if (!xprt_connected(task->tk_xprt))
1816 task->tk_action = call_connect;
1820 * Helpers to check if the task was already transmitted, and
1821 * to take action when that is the case.
1824 rpc_task_transmitted(struct rpc_task *task)
1826 return !test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate);
1830 rpc_task_handle_transmitted(struct rpc_task *task)
1832 xprt_end_transmit(task);
1833 task->tk_action = call_transmit_status;
1837 * 4. Get the server port number if not yet set
1840 call_bind(struct rpc_task *task)
1842 struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
1844 if (rpc_task_transmitted(task)) {
1845 rpc_task_handle_transmitted(task);
1849 if (xprt_bound(xprt)) {
1850 task->tk_action = call_connect;
1854 dprint_status(task);
1856 task->tk_action = call_bind_status;
1857 if (!xprt_prepare_transmit(task))
1860 task->tk_timeout = xprt->bind_timeout;
1861 xprt->ops->rpcbind(task);
1865 * 4a. Sort out bind result
1868 call_bind_status(struct rpc_task *task)
1872 if (rpc_task_transmitted(task)) {
1873 rpc_task_handle_transmitted(task);
1877 if (task->tk_status >= 0) {
1878 dprint_status(task);
1879 task->tk_status = 0;
1880 task->tk_action = call_connect;
1884 trace_rpc_bind_status(task);
1885 switch (task->tk_status) {
1887 dprintk("RPC: %5u rpcbind out of memory\n", task->tk_pid);
1888 rpc_delay(task, HZ >> 2);
1891 dprintk("RPC: %5u remote rpcbind: RPC program/version "
1892 "unavailable\n", task->tk_pid);
1893 /* fail immediately if this is an RPC ping */
1894 if (task->tk_msg.rpc_proc->p_proc == 0) {
1895 status = -EOPNOTSUPP;
1898 if (task->tk_rebind_retry == 0)
1900 task->tk_rebind_retry--;
1901 rpc_delay(task, 3*HZ);
1906 dprintk("RPC: %5u rpcbind request timed out\n",
1910 /* server doesn't support any rpcbind version we know of */
1911 dprintk("RPC: %5u unrecognized remote rpcbind service\n",
1914 case -EPROTONOSUPPORT:
1915 dprintk("RPC: %5u remote rpcbind version unavailable, retrying\n",
1918 case -ECONNREFUSED: /* connection problems */
1928 dprintk("RPC: %5u remote rpcbind unreachable: %d\n",
1929 task->tk_pid, task->tk_status);
1930 if (!RPC_IS_SOFTCONN(task)) {
1931 rpc_delay(task, 5*HZ);
1934 status = task->tk_status;
1937 dprintk("RPC: %5u unrecognized rpcbind error (%d)\n",
1938 task->tk_pid, -task->tk_status);
1941 rpc_exit(task, status);
1945 task->tk_status = 0;
1946 task->tk_action = call_bind;
1947 rpc_check_timeout(task);
1951 * 4b. Connect to the RPC server
1954 call_connect(struct rpc_task *task)
1956 struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
1958 if (rpc_task_transmitted(task)) {
1959 rpc_task_handle_transmitted(task);
1963 if (xprt_connected(xprt)) {
1964 task->tk_action = call_transmit;
1968 dprintk("RPC: %5u call_connect xprt %p %s connected\n",
1970 (xprt_connected(xprt) ? "is" : "is not"));
1972 task->tk_action = call_connect_status;
1973 if (task->tk_status < 0)
1975 if (task->tk_flags & RPC_TASK_NOCONNECT) {
1976 rpc_exit(task, -ENOTCONN);
1979 if (!xprt_prepare_transmit(task))
1985 * 4c. Sort out connect result
1988 call_connect_status(struct rpc_task *task)
1990 struct rpc_clnt *clnt = task->tk_client;
1991 int status = task->tk_status;
1993 if (rpc_task_transmitted(task)) {
1994 rpc_task_handle_transmitted(task);
1998 dprint_status(task);
2000 trace_rpc_connect_status(task);
2001 task->tk_status = 0;
2004 /* A positive refusal suggests a rebind is needed. */
2005 if (RPC_IS_SOFTCONN(task))
2007 if (clnt->cl_autobind) {
2008 rpc_force_rebind(clnt);
2020 xprt_conditional_disconnect(task->tk_rqstp->rq_xprt,
2021 task->tk_rqstp->rq_connect_cookie);
2022 if (RPC_IS_SOFTCONN(task))
2024 /* retry with existing socket, after a delay */
2025 rpc_delay(task, 3*HZ);
2032 clnt->cl_stats->netreconn++;
2033 task->tk_action = call_transmit;
2036 rpc_exit(task, status);
2039 /* Check for timeouts before looping back to call_bind */
2040 task->tk_action = call_bind;
2041 rpc_check_timeout(task);
2045 * 5. Transmit the RPC request, and wait for reply
2048 call_transmit(struct rpc_task *task)
2050 if (rpc_task_transmitted(task)) {
2051 rpc_task_handle_transmitted(task);
2055 dprint_status(task);
2057 task->tk_action = call_transmit_status;
2058 if (!xprt_prepare_transmit(task))
2060 task->tk_status = 0;
2061 if (test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate)) {
2062 if (!xprt_connected(task->tk_xprt)) {
2063 task->tk_status = -ENOTCONN;
2066 xprt_transmit(task);
2068 xprt_end_transmit(task);
2072 * 5a. Handle cleanup after a transmission
2075 call_transmit_status(struct rpc_task *task)
2077 task->tk_action = call_status;
2080 * Common case: success. Force the compiler to put this
2083 if (rpc_task_transmitted(task)) {
2084 task->tk_status = 0;
2085 xprt_request_wait_receive(task);
2089 switch (task->tk_status) {
2091 dprint_status(task);
2094 task->tk_status = 0;
2095 task->tk_action = call_encode;
2098 * Special cases: if we've been waiting on the
2099 * socket's write_space() callback, or if the
2100 * socket just returned a connection error,
2101 * then hold onto the transport lock.
2104 rpc_delay(task, HZ>>2);
2108 task->tk_action = call_transmit;
2109 task->tk_status = 0;
2117 if (RPC_IS_SOFTCONN(task)) {
2118 if (!task->tk_msg.rpc_proc->p_proc)
2119 trace_xprt_ping(task->tk_xprt,
2121 rpc_exit(task, task->tk_status);
2130 task->tk_action = call_bind;
2131 task->tk_status = 0;
2134 rpc_check_timeout(task);
2137 #if defined(CONFIG_SUNRPC_BACKCHANNEL)
2138 static void call_bc_transmit(struct rpc_task *task);
2139 static void call_bc_transmit_status(struct rpc_task *task);
2142 call_bc_encode(struct rpc_task *task)
2144 xprt_request_enqueue_transmit(task);
2145 task->tk_action = call_bc_transmit;
2149 * 5b. Send the backchannel RPC reply. On error, drop the reply. In
2150 * addition, disconnect on connectivity errors.
2153 call_bc_transmit(struct rpc_task *task)
2155 task->tk_action = call_bc_transmit_status;
2156 if (test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate)) {
2157 if (!xprt_prepare_transmit(task))
2159 task->tk_status = 0;
2160 xprt_transmit(task);
2162 xprt_end_transmit(task);
2166 call_bc_transmit_status(struct rpc_task *task)
2168 struct rpc_rqst *req = task->tk_rqstp;
2170 if (rpc_task_transmitted(task))
2171 task->tk_status = 0;
2173 dprint_status(task);
2175 switch (task->tk_status) {
2189 rpc_delay(task, HZ>>2);
2193 task->tk_status = 0;
2194 task->tk_action = call_bc_transmit;
2198 * Problem reaching the server. Disconnect and let the
2199 * forechannel reestablish the connection. The server will
2200 * have to retransmit the backchannel request and we'll
2201 * reprocess it. Since these ops are idempotent, there's no
2202 * need to cache our reply at this time.
2204 printk(KERN_NOTICE "RPC: Could not send backchannel reply "
2205 "error: %d\n", task->tk_status);
2206 xprt_conditional_disconnect(req->rq_xprt,
2207 req->rq_connect_cookie);
2211 * We were unable to reply and will have to drop the
2212 * request. The server should reconnect and retransmit.
2214 printk(KERN_NOTICE "RPC: Could not send backchannel reply "
2215 "error: %d\n", task->tk_status);
2218 task->tk_action = rpc_exit_task;
2220 #endif /* CONFIG_SUNRPC_BACKCHANNEL */
2223 * 6. Sort out the RPC call status
2226 call_status(struct rpc_task *task)
2228 struct rpc_clnt *clnt = task->tk_client;
2231 if (!task->tk_msg.rpc_proc->p_proc)
2232 trace_xprt_ping(task->tk_xprt, task->tk_status);
2234 dprint_status(task);
2236 status = task->tk_status;
2238 task->tk_action = call_decode;
2242 trace_rpc_call_status(task);
2243 task->tk_status = 0;
2250 if (RPC_IS_SOFTCONN(task))
2253 * Delay any retries for 3 seconds, then handle as if it
2256 rpc_delay(task, 3*HZ);
2263 rpc_force_rebind(clnt);
2266 rpc_delay(task, 3*HZ);
2273 /* shutdown or soft timeout */
2276 if (clnt->cl_chatty)
2277 printk("%s: RPC call returned error %d\n",
2278 clnt->cl_program->name, -status);
2281 task->tk_action = call_encode;
2282 rpc_check_timeout(task);
2285 rpc_exit(task, status);
2289 rpc_check_connected(const struct rpc_rqst *req)
2291 /* No allocated request or transport? return true */
2292 if (!req || !req->rq_xprt)
2294 return xprt_connected(req->rq_xprt);
2298 rpc_check_timeout(struct rpc_task *task)
2300 struct rpc_clnt *clnt = task->tk_client;
2302 if (xprt_adjust_timeout(task->tk_rqstp) == 0)
2305 dprintk("RPC: %5u call_timeout (major)\n", task->tk_pid);
2306 task->tk_timeouts++;
2308 if (RPC_IS_SOFTCONN(task) && !rpc_check_connected(task->tk_rqstp)) {
2309 rpc_exit(task, -ETIMEDOUT);
2313 if (RPC_IS_SOFT(task)) {
2314 if (clnt->cl_chatty) {
2315 printk(KERN_NOTICE "%s: server %s not responding, timed out\n",
2316 clnt->cl_program->name,
2317 task->tk_xprt->servername);
2319 if (task->tk_flags & RPC_TASK_TIMEOUT)
2320 rpc_exit(task, -ETIMEDOUT);
2322 rpc_exit(task, -EIO);
2326 if (!(task->tk_flags & RPC_CALL_MAJORSEEN)) {
2327 task->tk_flags |= RPC_CALL_MAJORSEEN;
2328 if (clnt->cl_chatty) {
2329 printk(KERN_NOTICE "%s: server %s not responding, still trying\n",
2330 clnt->cl_program->name,
2331 task->tk_xprt->servername);
2334 rpc_force_rebind(clnt);
2336 * Did our request time out due to an RPCSEC_GSS out-of-sequence
2337 * event? RFC2203 requires the server to drop all such requests.
2339 rpcauth_invalcred(task);
2343 * 7. Decode the RPC reply
2346 call_decode(struct rpc_task *task)
2348 struct rpc_clnt *clnt = task->tk_client;
2349 struct rpc_rqst *req = task->tk_rqstp;
2350 struct xdr_stream xdr;
2352 dprint_status(task);
2354 if (!task->tk_msg.rpc_proc->p_decode) {
2355 task->tk_action = rpc_exit_task;
2359 if (task->tk_flags & RPC_CALL_MAJORSEEN) {
2360 if (clnt->cl_chatty) {
2361 printk(KERN_NOTICE "%s: server %s OK\n",
2362 clnt->cl_program->name,
2363 task->tk_xprt->servername);
2365 task->tk_flags &= ~RPC_CALL_MAJORSEEN;
2369 * Ensure that we see all writes made by xprt_complete_rqst()
2370 * before it changed req->rq_reply_bytes_recvd.
2373 req->rq_rcv_buf.len = req->rq_private_buf.len;
2375 /* Check that the softirq receive buffer is valid */
2376 WARN_ON(memcmp(&req->rq_rcv_buf, &req->rq_private_buf,
2377 sizeof(req->rq_rcv_buf)) != 0);
2379 xdr_init_decode(&xdr, &req->rq_rcv_buf,
2380 req->rq_rcv_buf.head[0].iov_base, req);
2381 switch (rpc_decode_header(task, &xdr)) {
2383 task->tk_action = rpc_exit_task;
2384 task->tk_status = rpcauth_unwrap_resp(task, &xdr);
2385 dprintk("RPC: %5u %s result %d\n",
2386 task->tk_pid, __func__, task->tk_status);
2389 task->tk_status = 0;
2390 /* Note: rpc_decode_header() may have freed the RPC slot */
2391 if (task->tk_rqstp == req) {
2392 xdr_free_bvec(&req->rq_rcv_buf);
2393 req->rq_reply_bytes_recvd = 0;
2394 req->rq_rcv_buf.len = 0;
2395 if (task->tk_client->cl_discrtry)
2396 xprt_conditional_disconnect(req->rq_xprt,
2397 req->rq_connect_cookie);
2399 task->tk_action = call_encode;
2400 rpc_check_timeout(task);
2405 rpc_encode_header(struct rpc_task *task, struct xdr_stream *xdr)
2407 struct rpc_clnt *clnt = task->tk_client;
2408 struct rpc_rqst *req = task->tk_rqstp;
2413 p = xdr_reserve_space(xdr, RPC_CALLHDRSIZE << 2);
2418 *p++ = cpu_to_be32(RPC_VERSION);
2419 *p++ = cpu_to_be32(clnt->cl_prog);
2420 *p++ = cpu_to_be32(clnt->cl_vers);
2421 *p = cpu_to_be32(task->tk_msg.rpc_proc->p_proc);
2423 error = rpcauth_marshcred(task, xdr);
2428 trace_rpc_bad_callhdr(task);
2429 rpc_exit(task, error);
2434 rpc_decode_header(struct rpc_task *task, struct xdr_stream *xdr)
2436 struct rpc_clnt *clnt = task->tk_client;
2440 /* RFC-1014 says that the representation of XDR data must be a
2441 * multiple of four bytes
2442 * - if it isn't pointer subtraction in the NFS client may give
2445 if (task->tk_rqstp->rq_rcv_buf.len & 3)
2446 goto out_unparsable;
2448 p = xdr_inline_decode(xdr, 3 * sizeof(*p));
2450 goto out_unparsable;
2452 if (*p++ != rpc_reply)
2453 goto out_unparsable;
2454 if (*p++ != rpc_msg_accepted)
2455 goto out_msg_denied;
2457 error = rpcauth_checkverf(task, xdr);
2461 p = xdr_inline_decode(xdr, sizeof(*p));
2463 goto out_unparsable;
2467 case rpc_prog_unavail:
2468 trace_rpc__prog_unavail(task);
2469 error = -EPFNOSUPPORT;
2471 case rpc_prog_mismatch:
2472 trace_rpc__prog_mismatch(task);
2473 error = -EPROTONOSUPPORT;
2475 case rpc_proc_unavail:
2476 trace_rpc__proc_unavail(task);
2477 error = -EOPNOTSUPP;
2479 case rpc_garbage_args:
2480 case rpc_system_err:
2481 trace_rpc__garbage_args(task);
2485 goto out_unparsable;
2489 clnt->cl_stats->rpcgarbage++;
2490 if (task->tk_garb_retry) {
2491 task->tk_garb_retry--;
2492 task->tk_action = call_encode;
2496 rpc_exit(task, error);
2500 trace_rpc__unparsable(task);
2505 trace_rpc_bad_verifier(task);
2510 p = xdr_inline_decode(xdr, sizeof(*p));
2512 goto out_unparsable;
2514 case rpc_auth_error:
2517 trace_rpc__mismatch(task);
2518 error = -EPROTONOSUPPORT;
2521 goto out_unparsable;
2524 p = xdr_inline_decode(xdr, sizeof(*p));
2526 goto out_unparsable;
2528 case rpc_autherr_rejectedcred:
2529 case rpc_autherr_rejectedverf:
2530 case rpcsec_gsserr_credproblem:
2531 case rpcsec_gsserr_ctxproblem:
2532 if (!task->tk_cred_retry)
2534 task->tk_cred_retry--;
2535 trace_rpc__stale_creds(task);
2536 rpcauth_invalcred(task);
2537 /* Ensure we obtain a new XID! */
2539 task->tk_action = call_reserve;
2541 case rpc_autherr_badcred:
2542 case rpc_autherr_badverf:
2543 /* possibly garbled cred/verf? */
2544 if (!task->tk_garb_retry)
2546 task->tk_garb_retry--;
2547 trace_rpc__bad_creds(task);
2548 task->tk_action = call_encode;
2550 case rpc_autherr_tooweak:
2551 trace_rpc__auth_tooweak(task);
2552 pr_warn("RPC: server %s requires stronger authentication.\n",
2553 task->tk_xprt->servername);
2556 goto out_unparsable;
2561 static void rpcproc_encode_null(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
2566 static int rpcproc_decode_null(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
2572 static const struct rpc_procinfo rpcproc_null = {
2573 .p_encode = rpcproc_encode_null,
2574 .p_decode = rpcproc_decode_null,
2577 static int rpc_ping(struct rpc_clnt *clnt)
2579 struct rpc_message msg = {
2580 .rpc_proc = &rpcproc_null,
2583 err = rpc_call_sync(clnt, &msg, RPC_TASK_SOFT | RPC_TASK_SOFTCONN |
2584 RPC_TASK_NULLCREDS);
2589 struct rpc_task *rpc_call_null_helper(struct rpc_clnt *clnt,
2590 struct rpc_xprt *xprt, struct rpc_cred *cred, int flags,
2591 const struct rpc_call_ops *ops, void *data)
2593 struct rpc_message msg = {
2594 .rpc_proc = &rpcproc_null,
2596 struct rpc_task_setup task_setup_data = {
2599 .rpc_message = &msg,
2600 .rpc_op_cred = cred,
2601 .callback_ops = (ops != NULL) ? ops : &rpc_default_ops,
2602 .callback_data = data,
2603 .flags = flags | RPC_TASK_NULLCREDS,
2606 return rpc_run_task(&task_setup_data);
2609 struct rpc_task *rpc_call_null(struct rpc_clnt *clnt, struct rpc_cred *cred, int flags)
2611 return rpc_call_null_helper(clnt, NULL, cred, flags, NULL, NULL);
2613 EXPORT_SYMBOL_GPL(rpc_call_null);
2615 struct rpc_cb_add_xprt_calldata {
2616 struct rpc_xprt_switch *xps;
2617 struct rpc_xprt *xprt;
2620 static void rpc_cb_add_xprt_done(struct rpc_task *task, void *calldata)
2622 struct rpc_cb_add_xprt_calldata *data = calldata;
2624 if (task->tk_status == 0)
2625 rpc_xprt_switch_add_xprt(data->xps, data->xprt);
2628 static void rpc_cb_add_xprt_release(void *calldata)
2630 struct rpc_cb_add_xprt_calldata *data = calldata;
2632 xprt_put(data->xprt);
2633 xprt_switch_put(data->xps);
2637 static const struct rpc_call_ops rpc_cb_add_xprt_call_ops = {
2638 .rpc_call_done = rpc_cb_add_xprt_done,
2639 .rpc_release = rpc_cb_add_xprt_release,
2643 * rpc_clnt_test_and_add_xprt - Test and add a new transport to a rpc_clnt
2644 * @clnt: pointer to struct rpc_clnt
2645 * @xps: pointer to struct rpc_xprt_switch,
2646 * @xprt: pointer struct rpc_xprt
2649 int rpc_clnt_test_and_add_xprt(struct rpc_clnt *clnt,
2650 struct rpc_xprt_switch *xps, struct rpc_xprt *xprt,
2653 struct rpc_cb_add_xprt_calldata *data;
2654 struct rpc_task *task;
2656 data = kmalloc(sizeof(*data), GFP_NOFS);
2659 data->xps = xprt_switch_get(xps);
2660 data->xprt = xprt_get(xprt);
2662 task = rpc_call_null_helper(clnt, xprt, NULL,
2663 RPC_TASK_SOFT|RPC_TASK_SOFTCONN|RPC_TASK_ASYNC|RPC_TASK_NULLCREDS,
2664 &rpc_cb_add_xprt_call_ops, data);
2666 return PTR_ERR(task);
2670 EXPORT_SYMBOL_GPL(rpc_clnt_test_and_add_xprt);
2673 * rpc_clnt_setup_test_and_add_xprt()
2675 * This is an rpc_clnt_add_xprt setup() function which returns 1 so:
2676 * 1) caller of the test function must dereference the rpc_xprt_switch
2678 * 2) test function must call rpc_xprt_switch_add_xprt, usually in
2679 * the rpc_call_done routine.
2681 * Upon success (return of 1), the test function adds the new
2682 * transport to the rpc_clnt xprt switch
2684 * @clnt: struct rpc_clnt to get the new transport
2685 * @xps: the rpc_xprt_switch to hold the new transport
2686 * @xprt: the rpc_xprt to test
2687 * @data: a struct rpc_add_xprt_test pointer that holds the test function
2688 * and test function call data
2690 int rpc_clnt_setup_test_and_add_xprt(struct rpc_clnt *clnt,
2691 struct rpc_xprt_switch *xps,
2692 struct rpc_xprt *xprt,
2695 struct rpc_task *task;
2696 struct rpc_add_xprt_test *xtest = (struct rpc_add_xprt_test *)data;
2697 int status = -EADDRINUSE;
2699 xprt = xprt_get(xprt);
2700 xprt_switch_get(xps);
2702 if (rpc_xprt_switch_has_addr(xps, (struct sockaddr *)&xprt->addr))
2705 /* Test the connection */
2706 task = rpc_call_null_helper(clnt, xprt, NULL,
2707 RPC_TASK_SOFT | RPC_TASK_SOFTCONN | RPC_TASK_NULLCREDS,
2710 status = PTR_ERR(task);
2713 status = task->tk_status;
2719 /* rpc_xprt_switch and rpc_xprt are deferrenced by add_xprt_test() */
2720 xtest->add_xprt_test(clnt, xprt, xtest->data);
2723 xprt_switch_put(xps);
2725 /* so that rpc_clnt_add_xprt does not call rpc_xprt_switch_add_xprt */
2729 xprt_switch_put(xps);
2730 pr_info("RPC: rpc_clnt_test_xprt failed: %d addr %s not added\n",
2731 status, xprt->address_strings[RPC_DISPLAY_ADDR]);
2734 EXPORT_SYMBOL_GPL(rpc_clnt_setup_test_and_add_xprt);
2737 * rpc_clnt_add_xprt - Add a new transport to a rpc_clnt
2738 * @clnt: pointer to struct rpc_clnt
2739 * @xprtargs: pointer to struct xprt_create
2740 * @setup: callback to test and/or set up the connection
2741 * @data: pointer to setup function data
2743 * Creates a new transport using the parameters set in args and
2745 * If ping is set, then test that connectivity succeeds before
2746 * adding the new transport.
2749 int rpc_clnt_add_xprt(struct rpc_clnt *clnt,
2750 struct xprt_create *xprtargs,
2751 int (*setup)(struct rpc_clnt *,
2752 struct rpc_xprt_switch *,
2757 struct rpc_xprt_switch *xps;
2758 struct rpc_xprt *xprt;
2759 unsigned long connect_timeout;
2760 unsigned long reconnect_timeout;
2761 unsigned char resvport;
2765 xps = xprt_switch_get(rcu_dereference(clnt->cl_xpi.xpi_xpswitch));
2766 xprt = xprt_iter_xprt(&clnt->cl_xpi);
2767 if (xps == NULL || xprt == NULL) {
2771 resvport = xprt->resvport;
2772 connect_timeout = xprt->connect_timeout;
2773 reconnect_timeout = xprt->max_reconnect_timeout;
2776 xprt = xprt_create_transport(xprtargs);
2778 ret = PTR_ERR(xprt);
2779 goto out_put_switch;
2781 xprt->resvport = resvport;
2782 if (xprt->ops->set_connect_timeout != NULL)
2783 xprt->ops->set_connect_timeout(xprt,
2787 rpc_xprt_switch_set_roundrobin(xps);
2789 ret = setup(clnt, xps, xprt, data);
2793 rpc_xprt_switch_add_xprt(xps, xprt);
2797 xprt_switch_put(xps);
2800 EXPORT_SYMBOL_GPL(rpc_clnt_add_xprt);
2802 struct connect_timeout_data {
2803 unsigned long connect_timeout;
2804 unsigned long reconnect_timeout;
2808 rpc_xprt_set_connect_timeout(struct rpc_clnt *clnt,
2809 struct rpc_xprt *xprt,
2812 struct connect_timeout_data *timeo = data;
2814 if (xprt->ops->set_connect_timeout)
2815 xprt->ops->set_connect_timeout(xprt,
2816 timeo->connect_timeout,
2817 timeo->reconnect_timeout);
2822 rpc_set_connect_timeout(struct rpc_clnt *clnt,
2823 unsigned long connect_timeout,
2824 unsigned long reconnect_timeout)
2826 struct connect_timeout_data timeout = {
2827 .connect_timeout = connect_timeout,
2828 .reconnect_timeout = reconnect_timeout,
2830 rpc_clnt_iterate_for_each_xprt(clnt,
2831 rpc_xprt_set_connect_timeout,
2834 EXPORT_SYMBOL_GPL(rpc_set_connect_timeout);
2836 void rpc_clnt_xprt_switch_put(struct rpc_clnt *clnt)
2839 xprt_switch_put(rcu_dereference(clnt->cl_xpi.xpi_xpswitch));
2842 EXPORT_SYMBOL_GPL(rpc_clnt_xprt_switch_put);
2844 void rpc_clnt_xprt_switch_add_xprt(struct rpc_clnt *clnt, struct rpc_xprt *xprt)
2847 rpc_xprt_switch_add_xprt(rcu_dereference(clnt->cl_xpi.xpi_xpswitch),
2851 EXPORT_SYMBOL_GPL(rpc_clnt_xprt_switch_add_xprt);
2853 bool rpc_clnt_xprt_switch_has_addr(struct rpc_clnt *clnt,
2854 const struct sockaddr *sap)
2856 struct rpc_xprt_switch *xps;
2860 xps = rcu_dereference(clnt->cl_xpi.xpi_xpswitch);
2861 ret = rpc_xprt_switch_has_addr(xps, sap);
2865 EXPORT_SYMBOL_GPL(rpc_clnt_xprt_switch_has_addr);
2867 #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
2868 static void rpc_show_header(void)
2870 printk(KERN_INFO "-pid- flgs status -client- --rqstp- "
2871 "-timeout ---ops--\n");
2874 static void rpc_show_task(const struct rpc_clnt *clnt,
2875 const struct rpc_task *task)
2877 const char *rpc_waitq = "none";
2879 if (RPC_IS_QUEUED(task))
2880 rpc_waitq = rpc_qname(task->tk_waitqueue);
2882 printk(KERN_INFO "%5u %04x %6d %8p %8p %8ld %8p %sv%u %s a:%ps q:%s\n",
2883 task->tk_pid, task->tk_flags, task->tk_status,
2884 clnt, task->tk_rqstp, task->tk_timeout, task->tk_ops,
2885 clnt->cl_program->name, clnt->cl_vers, rpc_proc_name(task),
2886 task->tk_action, rpc_waitq);
2889 void rpc_show_tasks(struct net *net)
2891 struct rpc_clnt *clnt;
2892 struct rpc_task *task;
2894 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
2896 spin_lock(&sn->rpc_client_lock);
2897 list_for_each_entry(clnt, &sn->all_clients, cl_clients) {
2898 spin_lock(&clnt->cl_lock);
2899 list_for_each_entry(task, &clnt->cl_tasks, tk_task) {
2904 rpc_show_task(clnt, task);
2906 spin_unlock(&clnt->cl_lock);
2908 spin_unlock(&sn->rpc_client_lock);
2912 #if IS_ENABLED(CONFIG_SUNRPC_SWAP)
2914 rpc_clnt_swap_activate_callback(struct rpc_clnt *clnt,
2915 struct rpc_xprt *xprt,
2918 return xprt_enable_swap(xprt);
2922 rpc_clnt_swap_activate(struct rpc_clnt *clnt)
2924 if (atomic_inc_return(&clnt->cl_swapper) == 1)
2925 return rpc_clnt_iterate_for_each_xprt(clnt,
2926 rpc_clnt_swap_activate_callback, NULL);
2929 EXPORT_SYMBOL_GPL(rpc_clnt_swap_activate);
2932 rpc_clnt_swap_deactivate_callback(struct rpc_clnt *clnt,
2933 struct rpc_xprt *xprt,
2936 xprt_disable_swap(xprt);
2941 rpc_clnt_swap_deactivate(struct rpc_clnt *clnt)
2943 if (atomic_dec_if_positive(&clnt->cl_swapper) == 0)
2944 rpc_clnt_iterate_for_each_xprt(clnt,
2945 rpc_clnt_swap_deactivate_callback, NULL);
2947 EXPORT_SYMBOL_GPL(rpc_clnt_swap_deactivate);
2948 #endif /* CONFIG_SUNRPC_SWAP */