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.
22 #include <linux/module.h>
23 #include <linux/types.h>
24 #include <linux/kallsyms.h>
26 #include <linux/namei.h>
27 #include <linux/mount.h>
28 #include <linux/slab.h>
29 #include <linux/rcupdate.h>
30 #include <linux/utsname.h>
31 #include <linux/workqueue.h>
33 #include <linux/in6.h>
36 #include <linux/sunrpc/clnt.h>
37 #include <linux/sunrpc/addr.h>
38 #include <linux/sunrpc/rpc_pipe_fs.h>
39 #include <linux/sunrpc/metrics.h>
40 #include <linux/sunrpc/bc_xprt.h>
41 #include <trace/events/sunrpc.h>
46 #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
47 # define RPCDBG_FACILITY RPCDBG_CALL
50 #define dprint_status(t) \
51 dprintk("RPC: %5u %s (status %d)\n", t->tk_pid, \
52 __func__, t->tk_status)
55 * All RPC clients are linked into this list
58 static DECLARE_WAIT_QUEUE_HEAD(destroy_wait);
61 static void call_start(struct rpc_task *task);
62 static void call_reserve(struct rpc_task *task);
63 static void call_reserveresult(struct rpc_task *task);
64 static void call_allocate(struct rpc_task *task);
65 static void call_encode(struct rpc_task *task);
66 static void call_decode(struct rpc_task *task);
67 static void call_bind(struct rpc_task *task);
68 static void call_bind_status(struct rpc_task *task);
69 static void call_transmit(struct rpc_task *task);
70 static void call_status(struct rpc_task *task);
71 static void call_transmit_status(struct rpc_task *task);
72 static void call_refresh(struct rpc_task *task);
73 static void call_refreshresult(struct rpc_task *task);
74 static void call_connect(struct rpc_task *task);
75 static void call_connect_status(struct rpc_task *task);
77 static int rpc_encode_header(struct rpc_task *task,
78 struct xdr_stream *xdr);
79 static int rpc_decode_header(struct rpc_task *task,
80 struct xdr_stream *xdr);
81 static int rpc_ping(struct rpc_clnt *clnt);
82 static void rpc_check_timeout(struct rpc_task *task);
84 static void rpc_register_client(struct rpc_clnt *clnt)
86 struct net *net = rpc_net_ns(clnt);
87 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
89 spin_lock(&sn->rpc_client_lock);
90 list_add(&clnt->cl_clients, &sn->all_clients);
91 spin_unlock(&sn->rpc_client_lock);
94 static void rpc_unregister_client(struct rpc_clnt *clnt)
96 struct net *net = rpc_net_ns(clnt);
97 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
99 spin_lock(&sn->rpc_client_lock);
100 list_del(&clnt->cl_clients);
101 spin_unlock(&sn->rpc_client_lock);
104 static void __rpc_clnt_remove_pipedir(struct rpc_clnt *clnt)
106 rpc_remove_client_dir(clnt);
109 static void rpc_clnt_remove_pipedir(struct rpc_clnt *clnt)
111 struct net *net = rpc_net_ns(clnt);
112 struct super_block *pipefs_sb;
114 pipefs_sb = rpc_get_sb_net(net);
116 __rpc_clnt_remove_pipedir(clnt);
121 static struct dentry *rpc_setup_pipedir_sb(struct super_block *sb,
122 struct rpc_clnt *clnt)
124 static uint32_t clntid;
125 const char *dir_name = clnt->cl_program->pipe_dir_name;
127 struct dentry *dir, *dentry;
129 dir = rpc_d_lookup_sb(sb, dir_name);
131 pr_info("RPC: pipefs directory doesn't exist: %s\n", dir_name);
135 snprintf(name, sizeof(name), "clnt%x", (unsigned int)clntid++);
136 name[sizeof(name) - 1] = '\0';
137 dentry = rpc_create_client_dir(dir, name, clnt);
140 if (dentry == ERR_PTR(-EEXIST))
142 printk(KERN_INFO "RPC: Couldn't create pipefs entry"
143 " %s/%s, error %ld\n",
144 dir_name, name, PTR_ERR(dentry));
152 rpc_setup_pipedir(struct super_block *pipefs_sb, struct rpc_clnt *clnt)
154 struct dentry *dentry;
156 if (clnt->cl_program->pipe_dir_name != NULL) {
157 dentry = rpc_setup_pipedir_sb(pipefs_sb, clnt);
159 return PTR_ERR(dentry);
164 static int rpc_clnt_skip_event(struct rpc_clnt *clnt, unsigned long event)
166 if (clnt->cl_program->pipe_dir_name == NULL)
170 case RPC_PIPEFS_MOUNT:
171 if (clnt->cl_pipedir_objects.pdh_dentry != NULL)
173 if (atomic_read(&clnt->cl_count) == 0)
176 case RPC_PIPEFS_UMOUNT:
177 if (clnt->cl_pipedir_objects.pdh_dentry == NULL)
184 static int __rpc_clnt_handle_event(struct rpc_clnt *clnt, unsigned long event,
185 struct super_block *sb)
187 struct dentry *dentry;
190 case RPC_PIPEFS_MOUNT:
191 dentry = rpc_setup_pipedir_sb(sb, clnt);
195 return PTR_ERR(dentry);
197 case RPC_PIPEFS_UMOUNT:
198 __rpc_clnt_remove_pipedir(clnt);
201 printk(KERN_ERR "%s: unknown event: %ld\n", __func__, event);
207 static int __rpc_pipefs_event(struct rpc_clnt *clnt, unsigned long event,
208 struct super_block *sb)
212 for (;; clnt = clnt->cl_parent) {
213 if (!rpc_clnt_skip_event(clnt, event))
214 error = __rpc_clnt_handle_event(clnt, event, sb);
215 if (error || clnt == clnt->cl_parent)
221 static struct rpc_clnt *rpc_get_client_for_event(struct net *net, int event)
223 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
224 struct rpc_clnt *clnt;
226 spin_lock(&sn->rpc_client_lock);
227 list_for_each_entry(clnt, &sn->all_clients, cl_clients) {
228 if (rpc_clnt_skip_event(clnt, event))
230 spin_unlock(&sn->rpc_client_lock);
233 spin_unlock(&sn->rpc_client_lock);
237 static int rpc_pipefs_event(struct notifier_block *nb, unsigned long event,
240 struct super_block *sb = ptr;
241 struct rpc_clnt *clnt;
244 while ((clnt = rpc_get_client_for_event(sb->s_fs_info, event))) {
245 error = __rpc_pipefs_event(clnt, event, sb);
252 static struct notifier_block rpc_clients_block = {
253 .notifier_call = rpc_pipefs_event,
254 .priority = SUNRPC_PIPEFS_RPC_PRIO,
257 int rpc_clients_notifier_register(void)
259 return rpc_pipefs_notifier_register(&rpc_clients_block);
262 void rpc_clients_notifier_unregister(void)
264 return rpc_pipefs_notifier_unregister(&rpc_clients_block);
267 static struct rpc_xprt *rpc_clnt_set_transport(struct rpc_clnt *clnt,
268 struct rpc_xprt *xprt,
269 const struct rpc_timeout *timeout)
271 struct rpc_xprt *old;
273 spin_lock(&clnt->cl_lock);
274 old = rcu_dereference_protected(clnt->cl_xprt,
275 lockdep_is_held(&clnt->cl_lock));
277 if (!xprt_bound(xprt))
278 clnt->cl_autobind = 1;
280 clnt->cl_timeout = timeout;
281 rcu_assign_pointer(clnt->cl_xprt, xprt);
282 spin_unlock(&clnt->cl_lock);
287 static void rpc_clnt_set_nodename(struct rpc_clnt *clnt, const char *nodename)
289 clnt->cl_nodelen = strlcpy(clnt->cl_nodename,
290 nodename, sizeof(clnt->cl_nodename));
293 static int rpc_client_register(struct rpc_clnt *clnt,
294 rpc_authflavor_t pseudoflavor,
295 const char *client_name)
297 struct rpc_auth_create_args auth_args = {
298 .pseudoflavor = pseudoflavor,
299 .target_name = client_name,
301 struct rpc_auth *auth;
302 struct net *net = rpc_net_ns(clnt);
303 struct super_block *pipefs_sb;
306 rpc_clnt_debugfs_register(clnt);
308 pipefs_sb = rpc_get_sb_net(net);
310 err = rpc_setup_pipedir(pipefs_sb, clnt);
315 rpc_register_client(clnt);
319 auth = rpcauth_create(&auth_args, clnt);
321 dprintk("RPC: Couldn't create auth handle (flavor %u)\n",
328 pipefs_sb = rpc_get_sb_net(net);
329 rpc_unregister_client(clnt);
330 __rpc_clnt_remove_pipedir(clnt);
334 rpc_clnt_debugfs_unregister(clnt);
338 static DEFINE_IDA(rpc_clids);
340 void rpc_cleanup_clids(void)
342 ida_destroy(&rpc_clids);
345 static int rpc_alloc_clid(struct rpc_clnt *clnt)
349 clid = ida_simple_get(&rpc_clids, 0, 0, GFP_KERNEL);
352 clnt->cl_clid = clid;
356 static void rpc_free_clid(struct rpc_clnt *clnt)
358 ida_simple_remove(&rpc_clids, clnt->cl_clid);
361 static struct rpc_clnt * rpc_new_client(const struct rpc_create_args *args,
362 struct rpc_xprt_switch *xps,
363 struct rpc_xprt *xprt,
364 struct rpc_clnt *parent)
366 const struct rpc_program *program = args->program;
367 const struct rpc_version *version;
368 struct rpc_clnt *clnt = NULL;
369 const struct rpc_timeout *timeout;
370 const char *nodename = args->nodename;
373 /* sanity check the name before trying to print it */
374 dprintk("RPC: creating %s client for %s (xprt %p)\n",
375 program->name, args->servername, xprt);
382 if (args->version >= program->nrvers)
384 version = program->version[args->version];
389 clnt = kzalloc(sizeof(*clnt), GFP_KERNEL);
392 clnt->cl_parent = parent ? : clnt;
394 err = rpc_alloc_clid(clnt);
398 clnt->cl_cred = get_cred(args->cred);
399 clnt->cl_procinfo = version->procs;
400 clnt->cl_maxproc = version->nrprocs;
401 clnt->cl_prog = args->prognumber ? : program->number;
402 clnt->cl_vers = version->number;
403 clnt->cl_stats = program->stats;
404 clnt->cl_metrics = rpc_alloc_iostats(clnt);
405 rpc_init_pipe_dir_head(&clnt->cl_pipedir_objects);
407 if (clnt->cl_metrics == NULL)
409 clnt->cl_program = program;
410 INIT_LIST_HEAD(&clnt->cl_tasks);
411 spin_lock_init(&clnt->cl_lock);
413 timeout = xprt->timeout;
414 if (args->timeout != NULL) {
415 memcpy(&clnt->cl_timeout_default, args->timeout,
416 sizeof(clnt->cl_timeout_default));
417 timeout = &clnt->cl_timeout_default;
420 rpc_clnt_set_transport(clnt, xprt, timeout);
421 xprt_iter_init(&clnt->cl_xpi, xps);
422 xprt_switch_put(xps);
424 clnt->cl_rtt = &clnt->cl_rtt_default;
425 rpc_init_rtt(&clnt->cl_rtt_default, clnt->cl_timeout->to_initval);
427 atomic_set(&clnt->cl_count, 1);
429 if (nodename == NULL)
430 nodename = utsname()->nodename;
431 /* save the nodename */
432 rpc_clnt_set_nodename(clnt, nodename);
434 err = rpc_client_register(clnt, args->authflavor, args->client_name);
438 atomic_inc(&parent->cl_count);
442 rpc_free_iostats(clnt->cl_metrics);
444 put_cred(clnt->cl_cred);
451 xprt_switch_put(xps);
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);
489 clnt->cl_softrtry = 1;
490 if (args->flags & (RPC_CLNT_CREATE_HARDRTRY|RPC_CLNT_CREATE_SOFTERR)) {
491 clnt->cl_softrtry = 0;
492 if (args->flags & RPC_CLNT_CREATE_SOFTERR)
493 clnt->cl_softerr = 1;
496 if (args->flags & RPC_CLNT_CREATE_AUTOBIND)
497 clnt->cl_autobind = 1;
498 if (args->flags & RPC_CLNT_CREATE_NO_RETRANS_TIMEOUT)
499 clnt->cl_noretranstimeo = 1;
500 if (args->flags & RPC_CLNT_CREATE_DISCRTRY)
501 clnt->cl_discrtry = 1;
502 if (!(args->flags & RPC_CLNT_CREATE_QUIET))
509 * rpc_create - create an RPC client and transport with one call
510 * @args: rpc_clnt create argument structure
512 * Creates and initializes an RPC transport and an RPC client.
514 * It can ping the server in order to determine if it is up, and to see if
515 * it supports this program and version. RPC_CLNT_CREATE_NOPING disables
516 * this behavior so asynchronous tasks can also use rpc_create.
518 struct rpc_clnt *rpc_create(struct rpc_create_args *args)
520 struct rpc_xprt *xprt;
521 struct xprt_create xprtargs = {
523 .ident = args->protocol,
524 .srcaddr = args->saddress,
525 .dstaddr = args->address,
526 .addrlen = args->addrsize,
527 .servername = args->servername,
528 .bc_xprt = args->bc_xprt,
533 WARN_ON_ONCE(!(args->protocol & XPRT_TRANSPORT_BC));
534 xprt = args->bc_xprt->xpt_bc_xprt;
537 return rpc_create_xprt(args, xprt);
541 if (args->flags & RPC_CLNT_CREATE_INFINITE_SLOTS)
542 xprtargs.flags |= XPRT_CREATE_INFINITE_SLOTS;
543 if (args->flags & RPC_CLNT_CREATE_NO_IDLE_TIMEOUT)
544 xprtargs.flags |= XPRT_CREATE_NO_IDLE_TIMEOUT;
546 * If the caller chooses not to specify a hostname, whip
547 * up a string representation of the passed-in address.
549 if (xprtargs.servername == NULL) {
550 struct sockaddr_un *sun =
551 (struct sockaddr_un *)args->address;
552 struct sockaddr_in *sin =
553 (struct sockaddr_in *)args->address;
554 struct sockaddr_in6 *sin6 =
555 (struct sockaddr_in6 *)args->address;
557 servername[0] = '\0';
558 switch (args->address->sa_family) {
560 snprintf(servername, sizeof(servername), "%s",
564 snprintf(servername, sizeof(servername), "%pI4",
565 &sin->sin_addr.s_addr);
568 snprintf(servername, sizeof(servername), "%pI6",
572 /* caller wants default server name, but
573 * address family isn't recognized. */
574 return ERR_PTR(-EINVAL);
576 xprtargs.servername = servername;
579 xprt = xprt_create_transport(&xprtargs);
581 return (struct rpc_clnt *)xprt;
584 * By default, kernel RPC client connects from a reserved port.
585 * CAP_NET_BIND_SERVICE will not be set for unprivileged requesters,
586 * but it is always enabled for rpciod, which handles the connect
590 if (args->flags & RPC_CLNT_CREATE_NONPRIVPORT)
593 return rpc_create_xprt(args, xprt);
595 EXPORT_SYMBOL_GPL(rpc_create);
598 * This function clones the RPC client structure. It allows us to share the
599 * same transport while varying parameters such as the authentication
602 static struct rpc_clnt *__rpc_clone_client(struct rpc_create_args *args,
603 struct rpc_clnt *clnt)
605 struct rpc_xprt_switch *xps;
606 struct rpc_xprt *xprt;
607 struct rpc_clnt *new;
612 xprt = xprt_get(rcu_dereference(clnt->cl_xprt));
613 xps = xprt_switch_get(rcu_dereference(clnt->cl_xpi.xpi_xpswitch));
615 if (xprt == NULL || xps == NULL) {
617 xprt_switch_put(xps);
620 args->servername = xprt->servername;
621 args->nodename = clnt->cl_nodename;
623 new = rpc_new_client(args, xps, xprt, clnt);
629 /* Turn off autobind on clones */
630 new->cl_autobind = 0;
631 new->cl_softrtry = clnt->cl_softrtry;
632 new->cl_softerr = clnt->cl_softerr;
633 new->cl_noretranstimeo = clnt->cl_noretranstimeo;
634 new->cl_discrtry = clnt->cl_discrtry;
635 new->cl_chatty = clnt->cl_chatty;
636 new->cl_principal = clnt->cl_principal;
637 new->cl_cred = get_cred(clnt->cl_cred);
641 dprintk("RPC: %s: returned error %d\n", __func__, err);
646 * rpc_clone_client - Clone an RPC client structure
648 * @clnt: RPC client whose parameters are copied
650 * Returns a fresh RPC client or an ERR_PTR.
652 struct rpc_clnt *rpc_clone_client(struct rpc_clnt *clnt)
654 struct rpc_create_args args = {
655 .program = clnt->cl_program,
656 .prognumber = clnt->cl_prog,
657 .version = clnt->cl_vers,
658 .authflavor = clnt->cl_auth->au_flavor,
659 .cred = clnt->cl_cred,
661 return __rpc_clone_client(&args, clnt);
663 EXPORT_SYMBOL_GPL(rpc_clone_client);
666 * rpc_clone_client_set_auth - Clone an RPC client structure and set its auth
668 * @clnt: RPC client whose parameters are copied
669 * @flavor: security flavor for new client
671 * Returns a fresh RPC client or an ERR_PTR.
674 rpc_clone_client_set_auth(struct rpc_clnt *clnt, rpc_authflavor_t flavor)
676 struct rpc_create_args args = {
677 .program = clnt->cl_program,
678 .prognumber = clnt->cl_prog,
679 .version = clnt->cl_vers,
680 .authflavor = flavor,
681 .cred = clnt->cl_cred,
683 return __rpc_clone_client(&args, clnt);
685 EXPORT_SYMBOL_GPL(rpc_clone_client_set_auth);
688 * rpc_switch_client_transport: switch the RPC transport on the fly
689 * @clnt: pointer to a struct rpc_clnt
690 * @args: pointer to the new transport arguments
691 * @timeout: pointer to the new timeout parameters
693 * This function allows the caller to switch the RPC transport for the
694 * rpc_clnt structure 'clnt' to allow it to connect to a mirrored NFS
695 * server, for instance. It assumes that the caller has ensured that
696 * there are no active RPC tasks by using some form of locking.
698 * Returns zero if "clnt" is now using the new xprt. Otherwise a
699 * negative errno is returned, and "clnt" continues to use the old
702 int rpc_switch_client_transport(struct rpc_clnt *clnt,
703 struct xprt_create *args,
704 const struct rpc_timeout *timeout)
706 const struct rpc_timeout *old_timeo;
707 rpc_authflavor_t pseudoflavor;
708 struct rpc_xprt_switch *xps, *oldxps;
709 struct rpc_xprt *xprt, *old;
710 struct rpc_clnt *parent;
713 xprt = xprt_create_transport(args);
715 dprintk("RPC: failed to create new xprt for clnt %p\n",
717 return PTR_ERR(xprt);
720 xps = xprt_switch_alloc(xprt, GFP_KERNEL);
726 pseudoflavor = clnt->cl_auth->au_flavor;
728 old_timeo = clnt->cl_timeout;
729 old = rpc_clnt_set_transport(clnt, xprt, timeout);
730 oldxps = xprt_iter_xchg_switch(&clnt->cl_xpi, xps);
732 rpc_unregister_client(clnt);
733 __rpc_clnt_remove_pipedir(clnt);
734 rpc_clnt_debugfs_unregister(clnt);
737 * A new transport was created. "clnt" therefore
738 * becomes the root of a new cl_parent tree. clnt's
739 * children, if it has any, still point to the old xprt.
741 parent = clnt->cl_parent;
742 clnt->cl_parent = clnt;
745 * The old rpc_auth cache cannot be re-used. GSS
746 * contexts in particular are between a single
749 err = rpc_client_register(clnt, pseudoflavor, NULL);
755 rpc_release_client(parent);
756 xprt_switch_put(oldxps);
758 dprintk("RPC: replaced xprt for clnt %p\n", clnt);
762 xps = xprt_iter_xchg_switch(&clnt->cl_xpi, oldxps);
763 rpc_clnt_set_transport(clnt, old, old_timeo);
764 clnt->cl_parent = parent;
765 rpc_client_register(clnt, pseudoflavor, NULL);
766 xprt_switch_put(xps);
768 dprintk("RPC: failed to switch xprt for clnt %p\n", clnt);
771 EXPORT_SYMBOL_GPL(rpc_switch_client_transport);
774 int rpc_clnt_xprt_iter_init(struct rpc_clnt *clnt, struct rpc_xprt_iter *xpi)
776 struct rpc_xprt_switch *xps;
779 xps = xprt_switch_get(rcu_dereference(clnt->cl_xpi.xpi_xpswitch));
783 xprt_iter_init_listall(xpi, xps);
784 xprt_switch_put(xps);
789 * rpc_clnt_iterate_for_each_xprt - Apply a function to all transports
790 * @clnt: pointer to client
791 * @fn: function to apply
792 * @data: void pointer to function data
794 * Iterates through the list of RPC transports currently attached to the
795 * client and applies the function fn(clnt, xprt, data).
797 * On error, the iteration stops, and the function returns the error value.
799 int rpc_clnt_iterate_for_each_xprt(struct rpc_clnt *clnt,
800 int (*fn)(struct rpc_clnt *, struct rpc_xprt *, void *),
803 struct rpc_xprt_iter xpi;
806 ret = rpc_clnt_xprt_iter_init(clnt, &xpi);
810 struct rpc_xprt *xprt = xprt_iter_get_next(&xpi);
814 ret = fn(clnt, xprt, data);
819 xprt_iter_destroy(&xpi);
822 EXPORT_SYMBOL_GPL(rpc_clnt_iterate_for_each_xprt);
825 * Kill all tasks for the given client.
826 * XXX: kill their descendants as well?
828 void rpc_killall_tasks(struct rpc_clnt *clnt)
830 struct rpc_task *rovr;
833 if (list_empty(&clnt->cl_tasks))
835 dprintk("RPC: killing all tasks for client %p\n", clnt);
837 * Spin lock all_tasks to prevent changes...
839 spin_lock(&clnt->cl_lock);
840 list_for_each_entry(rovr, &clnt->cl_tasks, tk_task)
841 rpc_signal_task(rovr);
842 spin_unlock(&clnt->cl_lock);
844 EXPORT_SYMBOL_GPL(rpc_killall_tasks);
847 * Properly shut down an RPC client, terminating all outstanding
850 void rpc_shutdown_client(struct rpc_clnt *clnt)
854 dprintk_rcu("RPC: shutting down %s client for %s\n",
855 clnt->cl_program->name,
856 rcu_dereference(clnt->cl_xprt)->servername);
858 while (!list_empty(&clnt->cl_tasks)) {
859 rpc_killall_tasks(clnt);
860 wait_event_timeout(destroy_wait,
861 list_empty(&clnt->cl_tasks), 1*HZ);
864 rpc_release_client(clnt);
866 EXPORT_SYMBOL_GPL(rpc_shutdown_client);
871 static struct rpc_clnt *
872 rpc_free_client(struct rpc_clnt *clnt)
874 struct rpc_clnt *parent = NULL;
876 dprintk_rcu("RPC: destroying %s client for %s\n",
877 clnt->cl_program->name,
878 rcu_dereference(clnt->cl_xprt)->servername);
879 if (clnt->cl_parent != clnt)
880 parent = clnt->cl_parent;
881 rpc_clnt_debugfs_unregister(clnt);
882 rpc_clnt_remove_pipedir(clnt);
883 rpc_unregister_client(clnt);
884 rpc_free_iostats(clnt->cl_metrics);
885 clnt->cl_metrics = NULL;
886 xprt_put(rcu_dereference_raw(clnt->cl_xprt));
887 xprt_iter_destroy(&clnt->cl_xpi);
889 put_cred(clnt->cl_cred);
898 static struct rpc_clnt *
899 rpc_free_auth(struct rpc_clnt *clnt)
901 if (clnt->cl_auth == NULL)
902 return rpc_free_client(clnt);
905 * Note: RPCSEC_GSS may need to send NULL RPC calls in order to
906 * release remaining GSS contexts. This mechanism ensures
907 * that it can do so safely.
909 atomic_inc(&clnt->cl_count);
910 rpcauth_release(clnt->cl_auth);
911 clnt->cl_auth = NULL;
912 if (atomic_dec_and_test(&clnt->cl_count))
913 return rpc_free_client(clnt);
918 * Release reference to the RPC client
921 rpc_release_client(struct rpc_clnt *clnt)
923 dprintk("RPC: rpc_release_client(%p)\n", clnt);
926 if (list_empty(&clnt->cl_tasks))
927 wake_up(&destroy_wait);
928 if (!atomic_dec_and_test(&clnt->cl_count))
930 clnt = rpc_free_auth(clnt);
931 } while (clnt != NULL);
933 EXPORT_SYMBOL_GPL(rpc_release_client);
936 * rpc_bind_new_program - bind a new RPC program to an existing client
937 * @old: old rpc_client
938 * @program: rpc program to set
939 * @vers: rpc program version
941 * Clones the rpc client and sets up a new RPC program. This is mainly
942 * of use for enabling different RPC programs to share the same transport.
943 * The Sun NFSv2/v3 ACL protocol can do this.
945 struct rpc_clnt *rpc_bind_new_program(struct rpc_clnt *old,
946 const struct rpc_program *program,
949 struct rpc_create_args args = {
951 .prognumber = program->number,
953 .authflavor = old->cl_auth->au_flavor,
954 .cred = old->cl_cred,
956 struct rpc_clnt *clnt;
959 clnt = __rpc_clone_client(&args, old);
962 err = rpc_ping(clnt);
964 rpc_shutdown_client(clnt);
970 EXPORT_SYMBOL_GPL(rpc_bind_new_program);
972 void rpc_task_release_transport(struct rpc_task *task)
974 struct rpc_xprt *xprt = task->tk_xprt;
977 task->tk_xprt = NULL;
981 EXPORT_SYMBOL_GPL(rpc_task_release_transport);
983 void rpc_task_release_client(struct rpc_task *task)
985 struct rpc_clnt *clnt = task->tk_client;
988 /* Remove from client task list */
989 spin_lock(&clnt->cl_lock);
990 list_del(&task->tk_task);
991 spin_unlock(&clnt->cl_lock);
992 task->tk_client = NULL;
994 rpc_release_client(clnt);
996 rpc_task_release_transport(task);
1000 void rpc_task_set_transport(struct rpc_task *task, struct rpc_clnt *clnt)
1003 task->tk_xprt = xprt_iter_get_next(&clnt->cl_xpi);
1007 void rpc_task_set_client(struct rpc_task *task, struct rpc_clnt *clnt)
1011 rpc_task_set_transport(task, clnt);
1012 task->tk_client = clnt;
1013 atomic_inc(&clnt->cl_count);
1014 if (clnt->cl_softrtry)
1015 task->tk_flags |= RPC_TASK_SOFT;
1016 if (clnt->cl_softerr)
1017 task->tk_flags |= RPC_TASK_TIMEOUT;
1018 if (clnt->cl_noretranstimeo)
1019 task->tk_flags |= RPC_TASK_NO_RETRANS_TIMEOUT;
1020 if (atomic_read(&clnt->cl_swapper))
1021 task->tk_flags |= RPC_TASK_SWAPPER;
1022 /* Add to the client's list of all tasks */
1023 spin_lock(&clnt->cl_lock);
1024 list_add_tail(&task->tk_task, &clnt->cl_tasks);
1025 spin_unlock(&clnt->cl_lock);
1030 rpc_task_set_rpc_message(struct rpc_task *task, const struct rpc_message *msg)
1033 task->tk_msg.rpc_proc = msg->rpc_proc;
1034 task->tk_msg.rpc_argp = msg->rpc_argp;
1035 task->tk_msg.rpc_resp = msg->rpc_resp;
1036 if (msg->rpc_cred != NULL)
1037 task->tk_msg.rpc_cred = get_cred(msg->rpc_cred);
1042 * Default callback for async RPC calls
1045 rpc_default_callback(struct rpc_task *task, void *data)
1049 static const struct rpc_call_ops rpc_default_ops = {
1050 .rpc_call_done = rpc_default_callback,
1054 * rpc_run_task - Allocate a new RPC task, then run rpc_execute against it
1055 * @task_setup_data: pointer to task initialisation data
1057 struct rpc_task *rpc_run_task(const struct rpc_task_setup *task_setup_data)
1059 struct rpc_task *task;
1061 task = rpc_new_task(task_setup_data);
1063 rpc_task_set_client(task, task_setup_data->rpc_client);
1064 rpc_task_set_rpc_message(task, task_setup_data->rpc_message);
1066 if (task->tk_action == NULL)
1067 rpc_call_start(task);
1069 atomic_inc(&task->tk_count);
1073 EXPORT_SYMBOL_GPL(rpc_run_task);
1076 * rpc_call_sync - Perform a synchronous RPC call
1077 * @clnt: pointer to RPC client
1078 * @msg: RPC call parameters
1079 * @flags: RPC call flags
1081 int rpc_call_sync(struct rpc_clnt *clnt, const struct rpc_message *msg, int flags)
1083 struct rpc_task *task;
1084 struct rpc_task_setup task_setup_data = {
1087 .callback_ops = &rpc_default_ops,
1092 WARN_ON_ONCE(flags & RPC_TASK_ASYNC);
1093 if (flags & RPC_TASK_ASYNC) {
1094 rpc_release_calldata(task_setup_data.callback_ops,
1095 task_setup_data.callback_data);
1099 task = rpc_run_task(&task_setup_data);
1101 return PTR_ERR(task);
1102 status = task->tk_status;
1106 EXPORT_SYMBOL_GPL(rpc_call_sync);
1109 * rpc_call_async - Perform an asynchronous RPC call
1110 * @clnt: pointer to RPC client
1111 * @msg: RPC call parameters
1112 * @flags: RPC call flags
1113 * @tk_ops: RPC call ops
1114 * @data: user call data
1117 rpc_call_async(struct rpc_clnt *clnt, const struct rpc_message *msg, int flags,
1118 const struct rpc_call_ops *tk_ops, void *data)
1120 struct rpc_task *task;
1121 struct rpc_task_setup task_setup_data = {
1124 .callback_ops = tk_ops,
1125 .callback_data = data,
1126 .flags = flags|RPC_TASK_ASYNC,
1129 task = rpc_run_task(&task_setup_data);
1131 return PTR_ERR(task);
1135 EXPORT_SYMBOL_GPL(rpc_call_async);
1137 #if defined(CONFIG_SUNRPC_BACKCHANNEL)
1138 static void call_bc_encode(struct rpc_task *task);
1141 * rpc_run_bc_task - Allocate a new RPC task for backchannel use, then run
1142 * rpc_execute against it
1145 struct rpc_task *rpc_run_bc_task(struct rpc_rqst *req)
1147 struct rpc_task *task;
1148 struct rpc_task_setup task_setup_data = {
1149 .callback_ops = &rpc_default_ops,
1150 .flags = RPC_TASK_SOFTCONN |
1151 RPC_TASK_NO_RETRANS_TIMEOUT,
1154 dprintk("RPC: rpc_run_bc_task req= %p\n", req);
1156 * Create an rpc_task to send the data
1158 task = rpc_new_task(&task_setup_data);
1159 xprt_init_bc_request(req, task);
1161 task->tk_action = call_bc_encode;
1162 atomic_inc(&task->tk_count);
1163 WARN_ON_ONCE(atomic_read(&task->tk_count) != 2);
1166 dprintk("RPC: rpc_run_bc_task: task= %p\n", task);
1169 #endif /* CONFIG_SUNRPC_BACKCHANNEL */
1172 * rpc_prepare_reply_pages - Prepare to receive a reply data payload into pages
1173 * @req: RPC request to prepare
1174 * @pages: vector of struct page pointers
1175 * @base: offset in first page where receive should start, in bytes
1176 * @len: expected size of the upper layer data payload, in bytes
1177 * @hdrsize: expected size of upper layer reply header, in XDR words
1180 void rpc_prepare_reply_pages(struct rpc_rqst *req, struct page **pages,
1181 unsigned int base, unsigned int len,
1182 unsigned int hdrsize)
1184 /* Subtract one to force an extra word of buffer space for the
1185 * payload's XDR pad to fall into the rcv_buf's tail iovec.
1187 hdrsize += RPC_REPHDRSIZE + req->rq_cred->cr_auth->au_ralign - 1;
1189 xdr_inline_pages(&req->rq_rcv_buf, hdrsize << 2, pages, base, len);
1190 trace_rpc_reply_pages(req);
1192 EXPORT_SYMBOL_GPL(rpc_prepare_reply_pages);
1195 rpc_call_start(struct rpc_task *task)
1197 task->tk_action = call_start;
1199 EXPORT_SYMBOL_GPL(rpc_call_start);
1202 * rpc_peeraddr - extract remote peer address from clnt's xprt
1203 * @clnt: RPC client structure
1204 * @buf: target buffer
1205 * @bufsize: length of target buffer
1207 * Returns the number of bytes that are actually in the stored address.
1209 size_t rpc_peeraddr(struct rpc_clnt *clnt, struct sockaddr *buf, size_t bufsize)
1212 struct rpc_xprt *xprt;
1215 xprt = rcu_dereference(clnt->cl_xprt);
1217 bytes = xprt->addrlen;
1218 if (bytes > bufsize)
1220 memcpy(buf, &xprt->addr, bytes);
1225 EXPORT_SYMBOL_GPL(rpc_peeraddr);
1228 * rpc_peeraddr2str - return remote peer address in printable format
1229 * @clnt: RPC client structure
1230 * @format: address format
1232 * NB: the lifetime of the memory referenced by the returned pointer is
1233 * the same as the rpc_xprt itself. As long as the caller uses this
1234 * pointer, it must hold the RCU read lock.
1236 const char *rpc_peeraddr2str(struct rpc_clnt *clnt,
1237 enum rpc_display_format_t format)
1239 struct rpc_xprt *xprt;
1241 xprt = rcu_dereference(clnt->cl_xprt);
1243 if (xprt->address_strings[format] != NULL)
1244 return xprt->address_strings[format];
1246 return "unprintable";
1248 EXPORT_SYMBOL_GPL(rpc_peeraddr2str);
1250 static const struct sockaddr_in rpc_inaddr_loopback = {
1251 .sin_family = AF_INET,
1252 .sin_addr.s_addr = htonl(INADDR_ANY),
1255 static const struct sockaddr_in6 rpc_in6addr_loopback = {
1256 .sin6_family = AF_INET6,
1257 .sin6_addr = IN6ADDR_ANY_INIT,
1261 * Try a getsockname() on a connected datagram socket. Using a
1262 * connected datagram socket prevents leaving a socket in TIME_WAIT.
1263 * This conserves the ephemeral port number space.
1265 * Returns zero and fills in "buf" if successful; otherwise, a
1266 * negative errno is returned.
1268 static int rpc_sockname(struct net *net, struct sockaddr *sap, size_t salen,
1269 struct sockaddr *buf)
1271 struct socket *sock;
1274 err = __sock_create(net, sap->sa_family,
1275 SOCK_DGRAM, IPPROTO_UDP, &sock, 1);
1277 dprintk("RPC: can't create UDP socket (%d)\n", err);
1281 switch (sap->sa_family) {
1283 err = kernel_bind(sock,
1284 (struct sockaddr *)&rpc_inaddr_loopback,
1285 sizeof(rpc_inaddr_loopback));
1288 err = kernel_bind(sock,
1289 (struct sockaddr *)&rpc_in6addr_loopback,
1290 sizeof(rpc_in6addr_loopback));
1293 err = -EAFNOSUPPORT;
1297 dprintk("RPC: can't bind UDP socket (%d)\n", err);
1301 err = kernel_connect(sock, sap, salen, 0);
1303 dprintk("RPC: can't connect UDP socket (%d)\n", err);
1307 err = kernel_getsockname(sock, buf);
1309 dprintk("RPC: getsockname failed (%d)\n", err);
1314 if (buf->sa_family == AF_INET6) {
1315 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)buf;
1316 sin6->sin6_scope_id = 0;
1318 dprintk("RPC: %s succeeded\n", __func__);
1327 * Scraping a connected socket failed, so we don't have a useable
1328 * local address. Fallback: generate an address that will prevent
1329 * the server from calling us back.
1331 * Returns zero and fills in "buf" if successful; otherwise, a
1332 * negative errno is returned.
1334 static int rpc_anyaddr(int family, struct sockaddr *buf, size_t buflen)
1338 if (buflen < sizeof(rpc_inaddr_loopback))
1340 memcpy(buf, &rpc_inaddr_loopback,
1341 sizeof(rpc_inaddr_loopback));
1344 if (buflen < sizeof(rpc_in6addr_loopback))
1346 memcpy(buf, &rpc_in6addr_loopback,
1347 sizeof(rpc_in6addr_loopback));
1350 dprintk("RPC: %s: address family not supported\n",
1352 return -EAFNOSUPPORT;
1354 dprintk("RPC: %s: succeeded\n", __func__);
1359 * rpc_localaddr - discover local endpoint address for an RPC client
1360 * @clnt: RPC client structure
1361 * @buf: target buffer
1362 * @buflen: size of target buffer, in bytes
1364 * Returns zero and fills in "buf" and "buflen" if successful;
1365 * otherwise, a negative errno is returned.
1367 * This works even if the underlying transport is not currently connected,
1368 * or if the upper layer never previously provided a source address.
1370 * The result of this function call is transient: multiple calls in
1371 * succession may give different results, depending on how local
1372 * networking configuration changes over time.
1374 int rpc_localaddr(struct rpc_clnt *clnt, struct sockaddr *buf, size_t buflen)
1376 struct sockaddr_storage address;
1377 struct sockaddr *sap = (struct sockaddr *)&address;
1378 struct rpc_xprt *xprt;
1384 xprt = rcu_dereference(clnt->cl_xprt);
1385 salen = xprt->addrlen;
1386 memcpy(sap, &xprt->addr, salen);
1387 net = get_net(xprt->xprt_net);
1390 rpc_set_port(sap, 0);
1391 err = rpc_sockname(net, sap, salen, buf);
1394 /* Couldn't discover local address, return ANYADDR */
1395 return rpc_anyaddr(sap->sa_family, buf, buflen);
1398 EXPORT_SYMBOL_GPL(rpc_localaddr);
1401 rpc_setbufsize(struct rpc_clnt *clnt, unsigned int sndsize, unsigned int rcvsize)
1403 struct rpc_xprt *xprt;
1406 xprt = rcu_dereference(clnt->cl_xprt);
1407 if (xprt->ops->set_buffer_size)
1408 xprt->ops->set_buffer_size(xprt, sndsize, rcvsize);
1411 EXPORT_SYMBOL_GPL(rpc_setbufsize);
1414 * rpc_net_ns - Get the network namespace for this RPC client
1415 * @clnt: RPC client to query
1418 struct net *rpc_net_ns(struct rpc_clnt *clnt)
1423 ret = rcu_dereference(clnt->cl_xprt)->xprt_net;
1427 EXPORT_SYMBOL_GPL(rpc_net_ns);
1430 * rpc_max_payload - Get maximum payload size for a transport, in bytes
1431 * @clnt: RPC client to query
1433 * For stream transports, this is one RPC record fragment (see RFC
1434 * 1831), as we don't support multi-record requests yet. For datagram
1435 * transports, this is the size of an IP packet minus the IP, UDP, and
1438 size_t rpc_max_payload(struct rpc_clnt *clnt)
1443 ret = rcu_dereference(clnt->cl_xprt)->max_payload;
1447 EXPORT_SYMBOL_GPL(rpc_max_payload);
1450 * rpc_max_bc_payload - Get maximum backchannel payload size, in bytes
1451 * @clnt: RPC client to query
1453 size_t rpc_max_bc_payload(struct rpc_clnt *clnt)
1455 struct rpc_xprt *xprt;
1459 xprt = rcu_dereference(clnt->cl_xprt);
1460 ret = xprt->ops->bc_maxpayload(xprt);
1464 EXPORT_SYMBOL_GPL(rpc_max_bc_payload);
1467 * rpc_force_rebind - force transport to check that remote port is unchanged
1468 * @clnt: client to rebind
1471 void rpc_force_rebind(struct rpc_clnt *clnt)
1473 if (clnt->cl_autobind) {
1475 xprt_clear_bound(rcu_dereference(clnt->cl_xprt));
1479 EXPORT_SYMBOL_GPL(rpc_force_rebind);
1482 __rpc_restart_call(struct rpc_task *task, void (*action)(struct rpc_task *))
1484 task->tk_status = 0;
1485 task->tk_rpc_status = 0;
1486 task->tk_action = action;
1491 * Restart an (async) RPC call. Usually called from within the
1495 rpc_restart_call(struct rpc_task *task)
1497 return __rpc_restart_call(task, call_start);
1499 EXPORT_SYMBOL_GPL(rpc_restart_call);
1502 * Restart an (async) RPC call from the call_prepare state.
1503 * Usually called from within the exit handler.
1506 rpc_restart_call_prepare(struct rpc_task *task)
1508 if (task->tk_ops->rpc_call_prepare != NULL)
1509 return __rpc_restart_call(task, rpc_prepare_task);
1510 return rpc_restart_call(task);
1512 EXPORT_SYMBOL_GPL(rpc_restart_call_prepare);
1515 *rpc_proc_name(const struct rpc_task *task)
1517 const struct rpc_procinfo *proc = task->tk_msg.rpc_proc;
1521 return proc->p_name;
1529 __rpc_call_rpcerror(struct rpc_task *task, int tk_status, int rpc_status)
1531 task->tk_rpc_status = rpc_status;
1532 rpc_exit(task, tk_status);
1536 rpc_call_rpcerror(struct rpc_task *task, int status)
1538 __rpc_call_rpcerror(task, status, status);
1544 * Other FSM states can be visited zero or more times, but
1545 * this state is visited exactly once for each RPC.
1548 call_start(struct rpc_task *task)
1550 struct rpc_clnt *clnt = task->tk_client;
1551 int idx = task->tk_msg.rpc_proc->p_statidx;
1553 trace_rpc_request(task);
1554 dprintk("RPC: %5u call_start %s%d proc %s (%s)\n", task->tk_pid,
1555 clnt->cl_program->name, clnt->cl_vers,
1556 rpc_proc_name(task),
1557 (RPC_IS_ASYNC(task) ? "async" : "sync"));
1559 /* Increment call count (version might not be valid for ping) */
1560 if (clnt->cl_program->version[clnt->cl_vers])
1561 clnt->cl_program->version[clnt->cl_vers]->counts[idx]++;
1562 clnt->cl_stats->rpccnt++;
1563 task->tk_action = call_reserve;
1564 rpc_task_set_transport(task, clnt);
1568 * 1. Reserve an RPC call slot
1571 call_reserve(struct rpc_task *task)
1573 dprint_status(task);
1575 task->tk_status = 0;
1576 task->tk_action = call_reserveresult;
1580 static void call_retry_reserve(struct rpc_task *task);
1583 * 1b. Grok the result of xprt_reserve()
1586 call_reserveresult(struct rpc_task *task)
1588 int status = task->tk_status;
1590 dprint_status(task);
1593 * After a call to xprt_reserve(), we must have either
1594 * a request slot or else an error status.
1596 task->tk_status = 0;
1598 if (task->tk_rqstp) {
1599 task->tk_action = call_refresh;
1603 printk(KERN_ERR "%s: status=%d, but no request slot, exiting\n",
1605 rpc_call_rpcerror(task, -EIO);
1610 * Even though there was an error, we may have acquired
1611 * a request slot somehow. Make sure not to leak it.
1613 if (task->tk_rqstp) {
1614 printk(KERN_ERR "%s: status=%d, request allocated anyway\n",
1621 rpc_delay(task, HZ >> 2);
1623 case -EAGAIN: /* woken up; retry */
1624 task->tk_action = call_retry_reserve;
1626 case -EIO: /* probably a shutdown */
1629 printk(KERN_ERR "%s: unrecognized error %d, exiting\n",
1633 rpc_call_rpcerror(task, status);
1637 * 1c. Retry reserving an RPC call slot
1640 call_retry_reserve(struct rpc_task *task)
1642 dprint_status(task);
1644 task->tk_status = 0;
1645 task->tk_action = call_reserveresult;
1646 xprt_retry_reserve(task);
1650 * 2. Bind and/or refresh the credentials
1653 call_refresh(struct rpc_task *task)
1655 dprint_status(task);
1657 task->tk_action = call_refreshresult;
1658 task->tk_status = 0;
1659 task->tk_client->cl_stats->rpcauthrefresh++;
1660 rpcauth_refreshcred(task);
1664 * 2a. Process the results of a credential refresh
1667 call_refreshresult(struct rpc_task *task)
1669 int status = task->tk_status;
1671 dprint_status(task);
1673 task->tk_status = 0;
1674 task->tk_action = call_refresh;
1677 if (rpcauth_uptodatecred(task)) {
1678 task->tk_action = call_allocate;
1681 /* Use rate-limiting and a max number of retries if refresh
1682 * had status 0 but failed to update the cred.
1686 rpc_delay(task, 3*HZ);
1692 if (!task->tk_cred_retry)
1694 task->tk_cred_retry--;
1695 dprintk("RPC: %5u %s: retry refresh creds\n",
1696 task->tk_pid, __func__);
1699 dprintk("RPC: %5u %s: refresh creds failed with error %d\n",
1700 task->tk_pid, __func__, status);
1701 rpc_call_rpcerror(task, status);
1705 * 2b. Allocate the buffer. For details, see sched.c:rpc_malloc.
1706 * (Note: buffer memory is freed in xprt_release).
1709 call_allocate(struct rpc_task *task)
1711 const struct rpc_auth *auth = task->tk_rqstp->rq_cred->cr_auth;
1712 struct rpc_rqst *req = task->tk_rqstp;
1713 struct rpc_xprt *xprt = req->rq_xprt;
1714 const struct rpc_procinfo *proc = task->tk_msg.rpc_proc;
1717 dprint_status(task);
1719 task->tk_status = 0;
1720 task->tk_action = call_encode;
1725 if (proc->p_proc != 0) {
1726 BUG_ON(proc->p_arglen == 0);
1727 if (proc->p_decode != NULL)
1728 BUG_ON(proc->p_replen == 0);
1732 * Calculate the size (in quads) of the RPC call
1733 * and reply headers, and convert both values
1736 req->rq_callsize = RPC_CALLHDRSIZE + (auth->au_cslack << 1) +
1738 req->rq_callsize <<= 2;
1740 * Note: the reply buffer must at minimum allocate enough space
1741 * for the 'struct accepted_reply' from RFC5531.
1743 req->rq_rcvsize = RPC_REPHDRSIZE + auth->au_rslack + \
1744 max_t(size_t, proc->p_replen, 2);
1745 req->rq_rcvsize <<= 2;
1747 status = xprt->ops->buf_alloc(task);
1748 xprt_inject_disconnect(xprt);
1751 if (status != -ENOMEM) {
1752 rpc_call_rpcerror(task, status);
1756 dprintk("RPC: %5u rpc_buffer allocation failed\n", task->tk_pid);
1758 if (RPC_IS_ASYNC(task) || !fatal_signal_pending(current)) {
1759 task->tk_action = call_allocate;
1760 rpc_delay(task, HZ>>4);
1764 rpc_exit(task, -ERESTARTSYS);
1768 rpc_task_need_encode(struct rpc_task *task)
1770 return test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate) == 0 &&
1771 (!(task->tk_flags & RPC_TASK_SENT) ||
1772 !(task->tk_flags & RPC_TASK_NO_RETRANS_TIMEOUT) ||
1773 xprt_request_need_retransmit(task));
1777 rpc_xdr_encode(struct rpc_task *task)
1779 struct rpc_rqst *req = task->tk_rqstp;
1780 struct xdr_stream xdr;
1782 xdr_buf_init(&req->rq_snd_buf,
1785 xdr_buf_init(&req->rq_rcv_buf,
1789 req->rq_snd_buf.head[0].iov_len = 0;
1790 xdr_init_encode(&xdr, &req->rq_snd_buf,
1791 req->rq_snd_buf.head[0].iov_base, req);
1792 if (rpc_encode_header(task, &xdr))
1795 task->tk_status = rpcauth_wrap_req(task, &xdr);
1799 * 3. Encode arguments of an RPC call
1802 call_encode(struct rpc_task *task)
1804 if (!rpc_task_need_encode(task))
1806 dprint_status(task);
1807 /* Encode here so that rpcsec_gss can use correct sequence number. */
1808 rpc_xdr_encode(task);
1809 /* Did the encode result in an error condition? */
1810 if (task->tk_status != 0) {
1811 /* Was the error nonfatal? */
1812 switch (task->tk_status) {
1815 rpc_delay(task, HZ >> 4);
1818 if (!task->tk_cred_retry) {
1819 rpc_exit(task, task->tk_status);
1821 task->tk_action = call_refresh;
1822 task->tk_cred_retry--;
1823 dprintk("RPC: %5u %s: retry refresh creds\n",
1824 task->tk_pid, __func__);
1828 rpc_call_rpcerror(task, task->tk_status);
1832 xprt_request_prepare(task->tk_rqstp);
1835 /* Add task to reply queue before transmission to avoid races */
1836 if (rpc_reply_expected(task))
1837 xprt_request_enqueue_receive(task);
1838 xprt_request_enqueue_transmit(task);
1840 task->tk_action = call_transmit;
1841 /* Check that the connection is OK */
1842 if (!xprt_bound(task->tk_xprt))
1843 task->tk_action = call_bind;
1844 else if (!xprt_connected(task->tk_xprt))
1845 task->tk_action = call_connect;
1849 * Helpers to check if the task was already transmitted, and
1850 * to take action when that is the case.
1853 rpc_task_transmitted(struct rpc_task *task)
1855 return !test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate);
1859 rpc_task_handle_transmitted(struct rpc_task *task)
1861 xprt_end_transmit(task);
1862 task->tk_action = call_transmit_status;
1866 * 4. Get the server port number if not yet set
1869 call_bind(struct rpc_task *task)
1871 struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
1873 if (rpc_task_transmitted(task)) {
1874 rpc_task_handle_transmitted(task);
1878 if (xprt_bound(xprt)) {
1879 task->tk_action = call_connect;
1883 dprint_status(task);
1885 task->tk_action = call_bind_status;
1886 if (!xprt_prepare_transmit(task))
1889 xprt->ops->rpcbind(task);
1893 * 4a. Sort out bind result
1896 call_bind_status(struct rpc_task *task)
1900 if (rpc_task_transmitted(task)) {
1901 rpc_task_handle_transmitted(task);
1905 if (task->tk_status >= 0) {
1906 dprint_status(task);
1907 task->tk_status = 0;
1908 task->tk_action = call_connect;
1912 trace_rpc_bind_status(task);
1913 switch (task->tk_status) {
1915 dprintk("RPC: %5u rpcbind out of memory\n", task->tk_pid);
1916 rpc_delay(task, HZ >> 2);
1919 dprintk("RPC: %5u remote rpcbind: RPC program/version "
1920 "unavailable\n", task->tk_pid);
1921 /* fail immediately if this is an RPC ping */
1922 if (task->tk_msg.rpc_proc->p_proc == 0) {
1923 status = -EOPNOTSUPP;
1926 if (task->tk_rebind_retry == 0)
1928 task->tk_rebind_retry--;
1929 rpc_delay(task, 3*HZ);
1934 dprintk("RPC: %5u rpcbind request timed out\n",
1938 /* server doesn't support any rpcbind version we know of */
1939 dprintk("RPC: %5u unrecognized remote rpcbind service\n",
1942 case -EPROTONOSUPPORT:
1943 dprintk("RPC: %5u remote rpcbind version unavailable, retrying\n",
1946 case -ECONNREFUSED: /* connection problems */
1956 dprintk("RPC: %5u remote rpcbind unreachable: %d\n",
1957 task->tk_pid, task->tk_status);
1958 if (!RPC_IS_SOFTCONN(task)) {
1959 rpc_delay(task, 5*HZ);
1962 status = task->tk_status;
1965 dprintk("RPC: %5u unrecognized rpcbind error (%d)\n",
1966 task->tk_pid, -task->tk_status);
1969 rpc_call_rpcerror(task, status);
1973 task->tk_status = 0;
1974 task->tk_action = call_bind;
1975 rpc_check_timeout(task);
1979 * 4b. Connect to the RPC server
1982 call_connect(struct rpc_task *task)
1984 struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
1986 if (rpc_task_transmitted(task)) {
1987 rpc_task_handle_transmitted(task);
1991 if (xprt_connected(xprt)) {
1992 task->tk_action = call_transmit;
1996 dprintk("RPC: %5u call_connect xprt %p %s connected\n",
1998 (xprt_connected(xprt) ? "is" : "is not"));
2000 task->tk_action = call_connect_status;
2001 if (task->tk_status < 0)
2003 if (task->tk_flags & RPC_TASK_NOCONNECT) {
2004 rpc_call_rpcerror(task, -ENOTCONN);
2007 if (!xprt_prepare_transmit(task))
2013 * 4c. Sort out connect result
2016 call_connect_status(struct rpc_task *task)
2018 struct rpc_clnt *clnt = task->tk_client;
2019 int status = task->tk_status;
2021 if (rpc_task_transmitted(task)) {
2022 rpc_task_handle_transmitted(task);
2026 dprint_status(task);
2028 trace_rpc_connect_status(task);
2029 task->tk_status = 0;
2032 /* A positive refusal suggests a rebind is needed. */
2033 if (RPC_IS_SOFTCONN(task))
2035 if (clnt->cl_autobind) {
2036 rpc_force_rebind(clnt);
2048 xprt_conditional_disconnect(task->tk_rqstp->rq_xprt,
2049 task->tk_rqstp->rq_connect_cookie);
2050 if (RPC_IS_SOFTCONN(task))
2052 /* retry with existing socket, after a delay */
2053 rpc_delay(task, 3*HZ);
2060 clnt->cl_stats->netreconn++;
2061 task->tk_action = call_transmit;
2064 rpc_call_rpcerror(task, status);
2067 /* Check for timeouts before looping back to call_bind */
2068 task->tk_action = call_bind;
2069 rpc_check_timeout(task);
2073 * 5. Transmit the RPC request, and wait for reply
2076 call_transmit(struct rpc_task *task)
2078 if (rpc_task_transmitted(task)) {
2079 rpc_task_handle_transmitted(task);
2083 dprint_status(task);
2085 task->tk_action = call_transmit_status;
2086 if (!xprt_prepare_transmit(task))
2088 task->tk_status = 0;
2089 if (test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate)) {
2090 if (!xprt_connected(task->tk_xprt)) {
2091 task->tk_status = -ENOTCONN;
2094 xprt_transmit(task);
2096 xprt_end_transmit(task);
2100 * 5a. Handle cleanup after a transmission
2103 call_transmit_status(struct rpc_task *task)
2105 task->tk_action = call_status;
2108 * Common case: success. Force the compiler to put this
2111 if (rpc_task_transmitted(task)) {
2112 task->tk_status = 0;
2113 xprt_request_wait_receive(task);
2117 switch (task->tk_status) {
2119 dprint_status(task);
2122 task->tk_status = 0;
2123 task->tk_action = call_encode;
2126 * Special cases: if we've been waiting on the
2127 * socket's write_space() callback, or if the
2128 * socket just returned a connection error,
2129 * then hold onto the transport lock.
2132 rpc_delay(task, HZ>>2);
2136 task->tk_action = call_transmit;
2137 task->tk_status = 0;
2145 if (RPC_IS_SOFTCONN(task)) {
2146 if (!task->tk_msg.rpc_proc->p_proc)
2147 trace_xprt_ping(task->tk_xprt,
2149 rpc_call_rpcerror(task, task->tk_status);
2158 task->tk_action = call_bind;
2159 task->tk_status = 0;
2162 rpc_check_timeout(task);
2165 #if defined(CONFIG_SUNRPC_BACKCHANNEL)
2166 static void call_bc_transmit(struct rpc_task *task);
2167 static void call_bc_transmit_status(struct rpc_task *task);
2170 call_bc_encode(struct rpc_task *task)
2172 xprt_request_enqueue_transmit(task);
2173 task->tk_action = call_bc_transmit;
2177 * 5b. Send the backchannel RPC reply. On error, drop the reply. In
2178 * addition, disconnect on connectivity errors.
2181 call_bc_transmit(struct rpc_task *task)
2183 task->tk_action = call_bc_transmit_status;
2184 if (test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate)) {
2185 if (!xprt_prepare_transmit(task))
2187 task->tk_status = 0;
2188 xprt_transmit(task);
2190 xprt_end_transmit(task);
2194 call_bc_transmit_status(struct rpc_task *task)
2196 struct rpc_rqst *req = task->tk_rqstp;
2198 if (rpc_task_transmitted(task))
2199 task->tk_status = 0;
2201 dprint_status(task);
2203 switch (task->tk_status) {
2217 rpc_delay(task, HZ>>2);
2221 task->tk_status = 0;
2222 task->tk_action = call_bc_transmit;
2226 * Problem reaching the server. Disconnect and let the
2227 * forechannel reestablish the connection. The server will
2228 * have to retransmit the backchannel request and we'll
2229 * reprocess it. Since these ops are idempotent, there's no
2230 * need to cache our reply at this time.
2232 printk(KERN_NOTICE "RPC: Could not send backchannel reply "
2233 "error: %d\n", task->tk_status);
2234 xprt_conditional_disconnect(req->rq_xprt,
2235 req->rq_connect_cookie);
2239 * We were unable to reply and will have to drop the
2240 * request. The server should reconnect and retransmit.
2242 printk(KERN_NOTICE "RPC: Could not send backchannel reply "
2243 "error: %d\n", task->tk_status);
2246 task->tk_action = rpc_exit_task;
2248 #endif /* CONFIG_SUNRPC_BACKCHANNEL */
2251 * 6. Sort out the RPC call status
2254 call_status(struct rpc_task *task)
2256 struct rpc_clnt *clnt = task->tk_client;
2259 if (!task->tk_msg.rpc_proc->p_proc)
2260 trace_xprt_ping(task->tk_xprt, task->tk_status);
2262 dprint_status(task);
2264 status = task->tk_status;
2266 task->tk_action = call_decode;
2270 trace_rpc_call_status(task);
2271 task->tk_status = 0;
2278 if (RPC_IS_SOFTCONN(task))
2281 * Delay any retries for 3 seconds, then handle as if it
2284 rpc_delay(task, 3*HZ);
2292 rpc_force_rebind(clnt);
2295 rpc_delay(task, 3*HZ);
2301 /* shutdown or soft timeout */
2304 if (clnt->cl_chatty)
2305 printk("%s: RPC call returned error %d\n",
2306 clnt->cl_program->name, -status);
2309 task->tk_action = call_encode;
2310 rpc_check_timeout(task);
2313 rpc_call_rpcerror(task, status);
2317 rpc_check_connected(const struct rpc_rqst *req)
2319 /* No allocated request or transport? return true */
2320 if (!req || !req->rq_xprt)
2322 return xprt_connected(req->rq_xprt);
2326 rpc_check_timeout(struct rpc_task *task)
2328 struct rpc_clnt *clnt = task->tk_client;
2330 if (xprt_adjust_timeout(task->tk_rqstp) == 0)
2333 dprintk("RPC: %5u call_timeout (major)\n", task->tk_pid);
2334 task->tk_timeouts++;
2336 if (RPC_IS_SOFTCONN(task) && !rpc_check_connected(task->tk_rqstp)) {
2337 rpc_call_rpcerror(task, -ETIMEDOUT);
2341 if (RPC_IS_SOFT(task)) {
2343 * Once a "no retrans timeout" soft tasks (a.k.a NFSv4) has
2344 * been sent, it should time out only if the transport
2345 * connection gets terminally broken.
2347 if ((task->tk_flags & RPC_TASK_NO_RETRANS_TIMEOUT) &&
2348 rpc_check_connected(task->tk_rqstp))
2351 if (clnt->cl_chatty) {
2352 pr_notice_ratelimited(
2353 "%s: server %s not responding, timed out\n",
2354 clnt->cl_program->name,
2355 task->tk_xprt->servername);
2357 if (task->tk_flags & RPC_TASK_TIMEOUT)
2358 rpc_call_rpcerror(task, -ETIMEDOUT);
2360 __rpc_call_rpcerror(task, -EIO, -ETIMEDOUT);
2364 if (!(task->tk_flags & RPC_CALL_MAJORSEEN)) {
2365 task->tk_flags |= RPC_CALL_MAJORSEEN;
2366 if (clnt->cl_chatty) {
2367 pr_notice_ratelimited(
2368 "%s: server %s not responding, still trying\n",
2369 clnt->cl_program->name,
2370 task->tk_xprt->servername);
2373 rpc_force_rebind(clnt);
2375 * Did our request time out due to an RPCSEC_GSS out-of-sequence
2376 * event? RFC2203 requires the server to drop all such requests.
2378 rpcauth_invalcred(task);
2382 * 7. Decode the RPC reply
2385 call_decode(struct rpc_task *task)
2387 struct rpc_clnt *clnt = task->tk_client;
2388 struct rpc_rqst *req = task->tk_rqstp;
2389 struct xdr_stream xdr;
2391 dprint_status(task);
2393 if (!task->tk_msg.rpc_proc->p_decode) {
2394 task->tk_action = rpc_exit_task;
2398 if (task->tk_flags & RPC_CALL_MAJORSEEN) {
2399 if (clnt->cl_chatty) {
2400 pr_notice_ratelimited("%s: server %s OK\n",
2401 clnt->cl_program->name,
2402 task->tk_xprt->servername);
2404 task->tk_flags &= ~RPC_CALL_MAJORSEEN;
2408 * Ensure that we see all writes made by xprt_complete_rqst()
2409 * before it changed req->rq_reply_bytes_recvd.
2412 req->rq_rcv_buf.len = req->rq_private_buf.len;
2414 /* Check that the softirq receive buffer is valid */
2415 WARN_ON(memcmp(&req->rq_rcv_buf, &req->rq_private_buf,
2416 sizeof(req->rq_rcv_buf)) != 0);
2418 xdr_init_decode(&xdr, &req->rq_rcv_buf,
2419 req->rq_rcv_buf.head[0].iov_base, req);
2420 switch (rpc_decode_header(task, &xdr)) {
2422 task->tk_action = rpc_exit_task;
2423 task->tk_status = rpcauth_unwrap_resp(task, &xdr);
2424 dprintk("RPC: %5u %s result %d\n",
2425 task->tk_pid, __func__, task->tk_status);
2428 task->tk_status = 0;
2429 xdr_free_bvec(&req->rq_rcv_buf);
2430 req->rq_reply_bytes_recvd = 0;
2431 req->rq_rcv_buf.len = 0;
2432 if (task->tk_client->cl_discrtry)
2433 xprt_conditional_disconnect(req->rq_xprt,
2434 req->rq_connect_cookie);
2435 task->tk_action = call_encode;
2436 rpc_check_timeout(task);
2439 task->tk_action = call_reserve;
2440 rpc_check_timeout(task);
2441 rpcauth_invalcred(task);
2442 /* Ensure we obtain a new XID if we retry! */
2448 rpc_encode_header(struct rpc_task *task, struct xdr_stream *xdr)
2450 struct rpc_clnt *clnt = task->tk_client;
2451 struct rpc_rqst *req = task->tk_rqstp;
2456 p = xdr_reserve_space(xdr, RPC_CALLHDRSIZE << 2);
2461 *p++ = cpu_to_be32(RPC_VERSION);
2462 *p++ = cpu_to_be32(clnt->cl_prog);
2463 *p++ = cpu_to_be32(clnt->cl_vers);
2464 *p = cpu_to_be32(task->tk_msg.rpc_proc->p_proc);
2466 error = rpcauth_marshcred(task, xdr);
2471 trace_rpc_bad_callhdr(task);
2472 rpc_exit(task, error);
2477 rpc_decode_header(struct rpc_task *task, struct xdr_stream *xdr)
2479 struct rpc_clnt *clnt = task->tk_client;
2483 /* RFC-1014 says that the representation of XDR data must be a
2484 * multiple of four bytes
2485 * - if it isn't pointer subtraction in the NFS client may give
2488 if (task->tk_rqstp->rq_rcv_buf.len & 3)
2489 goto out_unparsable;
2491 p = xdr_inline_decode(xdr, 3 * sizeof(*p));
2493 goto out_unparsable;
2495 if (*p++ != rpc_reply)
2496 goto out_unparsable;
2497 if (*p++ != rpc_msg_accepted)
2498 goto out_msg_denied;
2500 error = rpcauth_checkverf(task, xdr);
2504 p = xdr_inline_decode(xdr, sizeof(*p));
2506 goto out_unparsable;
2510 case rpc_prog_unavail:
2511 trace_rpc__prog_unavail(task);
2512 error = -EPFNOSUPPORT;
2514 case rpc_prog_mismatch:
2515 trace_rpc__prog_mismatch(task);
2516 error = -EPROTONOSUPPORT;
2518 case rpc_proc_unavail:
2519 trace_rpc__proc_unavail(task);
2520 error = -EOPNOTSUPP;
2522 case rpc_garbage_args:
2523 case rpc_system_err:
2524 trace_rpc__garbage_args(task);
2528 goto out_unparsable;
2532 clnt->cl_stats->rpcgarbage++;
2533 if (task->tk_garb_retry) {
2534 task->tk_garb_retry--;
2535 task->tk_action = call_encode;
2539 rpc_exit(task, error);
2543 trace_rpc__unparsable(task);
2548 trace_rpc_bad_verifier(task);
2553 p = xdr_inline_decode(xdr, sizeof(*p));
2555 goto out_unparsable;
2557 case rpc_auth_error:
2560 trace_rpc__mismatch(task);
2561 error = -EPROTONOSUPPORT;
2564 goto out_unparsable;
2567 p = xdr_inline_decode(xdr, sizeof(*p));
2569 goto out_unparsable;
2571 case rpc_autherr_rejectedcred:
2572 case rpc_autherr_rejectedverf:
2573 case rpcsec_gsserr_credproblem:
2574 case rpcsec_gsserr_ctxproblem:
2575 if (!task->tk_cred_retry)
2577 task->tk_cred_retry--;
2578 trace_rpc__stale_creds(task);
2579 return -EKEYREJECTED;
2580 case rpc_autherr_badcred:
2581 case rpc_autherr_badverf:
2582 /* possibly garbled cred/verf? */
2583 if (!task->tk_garb_retry)
2585 task->tk_garb_retry--;
2586 trace_rpc__bad_creds(task);
2587 task->tk_action = call_encode;
2589 case rpc_autherr_tooweak:
2590 trace_rpc__auth_tooweak(task);
2591 pr_warn("RPC: server %s requires stronger authentication.\n",
2592 task->tk_xprt->servername);
2595 goto out_unparsable;
2600 static void rpcproc_encode_null(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
2605 static int rpcproc_decode_null(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
2611 static const struct rpc_procinfo rpcproc_null = {
2612 .p_encode = rpcproc_encode_null,
2613 .p_decode = rpcproc_decode_null,
2616 static int rpc_ping(struct rpc_clnt *clnt)
2618 struct rpc_message msg = {
2619 .rpc_proc = &rpcproc_null,
2622 err = rpc_call_sync(clnt, &msg, RPC_TASK_SOFT | RPC_TASK_SOFTCONN |
2623 RPC_TASK_NULLCREDS);
2628 struct rpc_task *rpc_call_null_helper(struct rpc_clnt *clnt,
2629 struct rpc_xprt *xprt, struct rpc_cred *cred, int flags,
2630 const struct rpc_call_ops *ops, void *data)
2632 struct rpc_message msg = {
2633 .rpc_proc = &rpcproc_null,
2635 struct rpc_task_setup task_setup_data = {
2638 .rpc_message = &msg,
2639 .rpc_op_cred = cred,
2640 .callback_ops = (ops != NULL) ? ops : &rpc_default_ops,
2641 .callback_data = data,
2642 .flags = flags | RPC_TASK_NULLCREDS,
2645 return rpc_run_task(&task_setup_data);
2648 struct rpc_task *rpc_call_null(struct rpc_clnt *clnt, struct rpc_cred *cred, int flags)
2650 return rpc_call_null_helper(clnt, NULL, cred, flags, NULL, NULL);
2652 EXPORT_SYMBOL_GPL(rpc_call_null);
2654 struct rpc_cb_add_xprt_calldata {
2655 struct rpc_xprt_switch *xps;
2656 struct rpc_xprt *xprt;
2659 static void rpc_cb_add_xprt_done(struct rpc_task *task, void *calldata)
2661 struct rpc_cb_add_xprt_calldata *data = calldata;
2663 if (task->tk_status == 0)
2664 rpc_xprt_switch_add_xprt(data->xps, data->xprt);
2667 static void rpc_cb_add_xprt_release(void *calldata)
2669 struct rpc_cb_add_xprt_calldata *data = calldata;
2671 xprt_put(data->xprt);
2672 xprt_switch_put(data->xps);
2676 static const struct rpc_call_ops rpc_cb_add_xprt_call_ops = {
2677 .rpc_call_done = rpc_cb_add_xprt_done,
2678 .rpc_release = rpc_cb_add_xprt_release,
2682 * rpc_clnt_test_and_add_xprt - Test and add a new transport to a rpc_clnt
2683 * @clnt: pointer to struct rpc_clnt
2684 * @xps: pointer to struct rpc_xprt_switch,
2685 * @xprt: pointer struct rpc_xprt
2688 int rpc_clnt_test_and_add_xprt(struct rpc_clnt *clnt,
2689 struct rpc_xprt_switch *xps, struct rpc_xprt *xprt,
2692 struct rpc_cb_add_xprt_calldata *data;
2693 struct rpc_task *task;
2695 data = kmalloc(sizeof(*data), GFP_NOFS);
2698 data->xps = xprt_switch_get(xps);
2699 data->xprt = xprt_get(xprt);
2701 task = rpc_call_null_helper(clnt, xprt, NULL,
2702 RPC_TASK_SOFT|RPC_TASK_SOFTCONN|RPC_TASK_ASYNC|RPC_TASK_NULLCREDS,
2703 &rpc_cb_add_xprt_call_ops, data);
2705 return PTR_ERR(task);
2709 EXPORT_SYMBOL_GPL(rpc_clnt_test_and_add_xprt);
2712 * rpc_clnt_setup_test_and_add_xprt()
2714 * This is an rpc_clnt_add_xprt setup() function which returns 1 so:
2715 * 1) caller of the test function must dereference the rpc_xprt_switch
2717 * 2) test function must call rpc_xprt_switch_add_xprt, usually in
2718 * the rpc_call_done routine.
2720 * Upon success (return of 1), the test function adds the new
2721 * transport to the rpc_clnt xprt switch
2723 * @clnt: struct rpc_clnt to get the new transport
2724 * @xps: the rpc_xprt_switch to hold the new transport
2725 * @xprt: the rpc_xprt to test
2726 * @data: a struct rpc_add_xprt_test pointer that holds the test function
2727 * and test function call data
2729 int rpc_clnt_setup_test_and_add_xprt(struct rpc_clnt *clnt,
2730 struct rpc_xprt_switch *xps,
2731 struct rpc_xprt *xprt,
2734 struct rpc_task *task;
2735 struct rpc_add_xprt_test *xtest = (struct rpc_add_xprt_test *)data;
2736 int status = -EADDRINUSE;
2738 xprt = xprt_get(xprt);
2739 xprt_switch_get(xps);
2741 if (rpc_xprt_switch_has_addr(xps, (struct sockaddr *)&xprt->addr))
2744 /* Test the connection */
2745 task = rpc_call_null_helper(clnt, xprt, NULL,
2746 RPC_TASK_SOFT | RPC_TASK_SOFTCONN | RPC_TASK_NULLCREDS,
2749 status = PTR_ERR(task);
2752 status = task->tk_status;
2758 /* rpc_xprt_switch and rpc_xprt are deferrenced by add_xprt_test() */
2759 xtest->add_xprt_test(clnt, xprt, xtest->data);
2762 xprt_switch_put(xps);
2764 /* so that rpc_clnt_add_xprt does not call rpc_xprt_switch_add_xprt */
2768 xprt_switch_put(xps);
2769 pr_info("RPC: rpc_clnt_test_xprt failed: %d addr %s not added\n",
2770 status, xprt->address_strings[RPC_DISPLAY_ADDR]);
2773 EXPORT_SYMBOL_GPL(rpc_clnt_setup_test_and_add_xprt);
2776 * rpc_clnt_add_xprt - Add a new transport to a rpc_clnt
2777 * @clnt: pointer to struct rpc_clnt
2778 * @xprtargs: pointer to struct xprt_create
2779 * @setup: callback to test and/or set up the connection
2780 * @data: pointer to setup function data
2782 * Creates a new transport using the parameters set in args and
2784 * If ping is set, then test that connectivity succeeds before
2785 * adding the new transport.
2788 int rpc_clnt_add_xprt(struct rpc_clnt *clnt,
2789 struct xprt_create *xprtargs,
2790 int (*setup)(struct rpc_clnt *,
2791 struct rpc_xprt_switch *,
2796 struct rpc_xprt_switch *xps;
2797 struct rpc_xprt *xprt;
2798 unsigned long connect_timeout;
2799 unsigned long reconnect_timeout;
2800 unsigned char resvport;
2804 xps = xprt_switch_get(rcu_dereference(clnt->cl_xpi.xpi_xpswitch));
2805 xprt = xprt_iter_xprt(&clnt->cl_xpi);
2806 if (xps == NULL || xprt == NULL) {
2810 resvport = xprt->resvport;
2811 connect_timeout = xprt->connect_timeout;
2812 reconnect_timeout = xprt->max_reconnect_timeout;
2815 xprt = xprt_create_transport(xprtargs);
2817 ret = PTR_ERR(xprt);
2818 goto out_put_switch;
2820 xprt->resvport = resvport;
2821 if (xprt->ops->set_connect_timeout != NULL)
2822 xprt->ops->set_connect_timeout(xprt,
2826 rpc_xprt_switch_set_roundrobin(xps);
2828 ret = setup(clnt, xps, xprt, data);
2832 rpc_xprt_switch_add_xprt(xps, xprt);
2836 xprt_switch_put(xps);
2839 EXPORT_SYMBOL_GPL(rpc_clnt_add_xprt);
2841 struct connect_timeout_data {
2842 unsigned long connect_timeout;
2843 unsigned long reconnect_timeout;
2847 rpc_xprt_set_connect_timeout(struct rpc_clnt *clnt,
2848 struct rpc_xprt *xprt,
2851 struct connect_timeout_data *timeo = data;
2853 if (xprt->ops->set_connect_timeout)
2854 xprt->ops->set_connect_timeout(xprt,
2855 timeo->connect_timeout,
2856 timeo->reconnect_timeout);
2861 rpc_set_connect_timeout(struct rpc_clnt *clnt,
2862 unsigned long connect_timeout,
2863 unsigned long reconnect_timeout)
2865 struct connect_timeout_data timeout = {
2866 .connect_timeout = connect_timeout,
2867 .reconnect_timeout = reconnect_timeout,
2869 rpc_clnt_iterate_for_each_xprt(clnt,
2870 rpc_xprt_set_connect_timeout,
2873 EXPORT_SYMBOL_GPL(rpc_set_connect_timeout);
2875 void rpc_clnt_xprt_switch_put(struct rpc_clnt *clnt)
2878 xprt_switch_put(rcu_dereference(clnt->cl_xpi.xpi_xpswitch));
2881 EXPORT_SYMBOL_GPL(rpc_clnt_xprt_switch_put);
2883 void rpc_clnt_xprt_switch_add_xprt(struct rpc_clnt *clnt, struct rpc_xprt *xprt)
2886 rpc_xprt_switch_add_xprt(rcu_dereference(clnt->cl_xpi.xpi_xpswitch),
2890 EXPORT_SYMBOL_GPL(rpc_clnt_xprt_switch_add_xprt);
2892 bool rpc_clnt_xprt_switch_has_addr(struct rpc_clnt *clnt,
2893 const struct sockaddr *sap)
2895 struct rpc_xprt_switch *xps;
2899 xps = rcu_dereference(clnt->cl_xpi.xpi_xpswitch);
2900 ret = rpc_xprt_switch_has_addr(xps, sap);
2904 EXPORT_SYMBOL_GPL(rpc_clnt_xprt_switch_has_addr);
2906 #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
2907 static void rpc_show_header(void)
2909 printk(KERN_INFO "-pid- flgs status -client- --rqstp- "
2910 "-timeout ---ops--\n");
2913 static void rpc_show_task(const struct rpc_clnt *clnt,
2914 const struct rpc_task *task)
2916 const char *rpc_waitq = "none";
2918 if (RPC_IS_QUEUED(task))
2919 rpc_waitq = rpc_qname(task->tk_waitqueue);
2921 printk(KERN_INFO "%5u %04x %6d %8p %8p %8ld %8p %sv%u %s a:%ps q:%s\n",
2922 task->tk_pid, task->tk_flags, task->tk_status,
2923 clnt, task->tk_rqstp, rpc_task_timeout(task), task->tk_ops,
2924 clnt->cl_program->name, clnt->cl_vers, rpc_proc_name(task),
2925 task->tk_action, rpc_waitq);
2928 void rpc_show_tasks(struct net *net)
2930 struct rpc_clnt *clnt;
2931 struct rpc_task *task;
2933 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
2935 spin_lock(&sn->rpc_client_lock);
2936 list_for_each_entry(clnt, &sn->all_clients, cl_clients) {
2937 spin_lock(&clnt->cl_lock);
2938 list_for_each_entry(task, &clnt->cl_tasks, tk_task) {
2943 rpc_show_task(clnt, task);
2945 spin_unlock(&clnt->cl_lock);
2947 spin_unlock(&sn->rpc_client_lock);
2951 #if IS_ENABLED(CONFIG_SUNRPC_SWAP)
2953 rpc_clnt_swap_activate_callback(struct rpc_clnt *clnt,
2954 struct rpc_xprt *xprt,
2957 return xprt_enable_swap(xprt);
2961 rpc_clnt_swap_activate(struct rpc_clnt *clnt)
2963 if (atomic_inc_return(&clnt->cl_swapper) == 1)
2964 return rpc_clnt_iterate_for_each_xprt(clnt,
2965 rpc_clnt_swap_activate_callback, NULL);
2968 EXPORT_SYMBOL_GPL(rpc_clnt_swap_activate);
2971 rpc_clnt_swap_deactivate_callback(struct rpc_clnt *clnt,
2972 struct rpc_xprt *xprt,
2975 xprt_disable_swap(xprt);
2980 rpc_clnt_swap_deactivate(struct rpc_clnt *clnt)
2982 if (atomic_dec_if_positive(&clnt->cl_swapper) == 0)
2983 rpc_clnt_iterate_for_each_xprt(clnt,
2984 rpc_clnt_swap_deactivate_callback, NULL);
2986 EXPORT_SYMBOL_GPL(rpc_clnt_swap_deactivate);
2987 #endif /* CONFIG_SUNRPC_SWAP */