1 // SPDX-License-Identifier: GPL-2.0-only
3 * linux/net/sunrpc/clnt.c
5 * This file contains the high-level RPC interface.
6 * It is modeled as a finite state machine to support both synchronous
7 * and asynchronous requests.
9 * - RPC header generation and argument serialization.
10 * - Credential refresh.
11 * - TCP connect handling.
12 * - Retry of operation when it is suspected the operation failed because
13 * of uid squashing on the server, or when the credentials were stale
14 * and need to be refreshed, or when a packet was damaged in transit.
15 * This may be have to be moved to the VFS layer.
17 * Copyright (C) 1992,1993 Rick Sladkey <jrs@world.std.com>
18 * Copyright (C) 1995,1996 Olaf Kirch <okir@monad.swb.de>
22 #include <linux/module.h>
23 #include <linux/types.h>
24 #include <linux/kallsyms.h>
26 #include <linux/namei.h>
27 #include <linux/mount.h>
28 #include <linux/slab.h>
29 #include <linux/rcupdate.h>
30 #include <linux/utsname.h>
31 #include <linux/workqueue.h>
33 #include <linux/in6.h>
36 #include <linux/sunrpc/clnt.h>
37 #include <linux/sunrpc/addr.h>
38 #include <linux/sunrpc/rpc_pipe_fs.h>
39 #include <linux/sunrpc/metrics.h>
40 #include <linux/sunrpc/bc_xprt.h>
41 #include <trace/events/sunrpc.h>
47 #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
48 # define RPCDBG_FACILITY RPCDBG_CALL
52 * All RPC clients are linked into this list
55 static DECLARE_WAIT_QUEUE_HEAD(destroy_wait);
58 static void call_start(struct rpc_task *task);
59 static void call_reserve(struct rpc_task *task);
60 static void call_reserveresult(struct rpc_task *task);
61 static void call_allocate(struct rpc_task *task);
62 static void call_encode(struct rpc_task *task);
63 static void call_decode(struct rpc_task *task);
64 static void call_bind(struct rpc_task *task);
65 static void call_bind_status(struct rpc_task *task);
66 static void call_transmit(struct rpc_task *task);
67 static void call_status(struct rpc_task *task);
68 static void call_transmit_status(struct rpc_task *task);
69 static void call_refresh(struct rpc_task *task);
70 static void call_refreshresult(struct rpc_task *task);
71 static void call_connect(struct rpc_task *task);
72 static void call_connect_status(struct rpc_task *task);
74 static int rpc_encode_header(struct rpc_task *task,
75 struct xdr_stream *xdr);
76 static int rpc_decode_header(struct rpc_task *task,
77 struct xdr_stream *xdr);
78 static int rpc_ping(struct rpc_clnt *clnt);
79 static int rpc_ping_noreply(struct rpc_clnt *clnt);
80 static void rpc_check_timeout(struct rpc_task *task);
82 static void rpc_register_client(struct rpc_clnt *clnt)
84 struct net *net = rpc_net_ns(clnt);
85 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
87 spin_lock(&sn->rpc_client_lock);
88 list_add(&clnt->cl_clients, &sn->all_clients);
89 spin_unlock(&sn->rpc_client_lock);
92 static void rpc_unregister_client(struct rpc_clnt *clnt)
94 struct net *net = rpc_net_ns(clnt);
95 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
97 spin_lock(&sn->rpc_client_lock);
98 list_del(&clnt->cl_clients);
99 spin_unlock(&sn->rpc_client_lock);
102 static void __rpc_clnt_remove_pipedir(struct rpc_clnt *clnt)
104 rpc_remove_client_dir(clnt);
107 static void rpc_clnt_remove_pipedir(struct rpc_clnt *clnt)
109 struct net *net = rpc_net_ns(clnt);
110 struct super_block *pipefs_sb;
112 pipefs_sb = rpc_get_sb_net(net);
114 if (pipefs_sb == clnt->pipefs_sb)
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 clnt->pipefs_sb = pipefs_sb;
157 if (clnt->cl_program->pipe_dir_name != NULL) {
158 dentry = rpc_setup_pipedir_sb(pipefs_sb, clnt);
160 return PTR_ERR(dentry);
165 static int rpc_clnt_skip_event(struct rpc_clnt *clnt, unsigned long event)
167 if (clnt->cl_program->pipe_dir_name == NULL)
171 case RPC_PIPEFS_MOUNT:
172 if (clnt->cl_pipedir_objects.pdh_dentry != NULL)
174 if (refcount_read(&clnt->cl_count) == 0)
177 case RPC_PIPEFS_UMOUNT:
178 if (clnt->cl_pipedir_objects.pdh_dentry == NULL)
185 static int __rpc_clnt_handle_event(struct rpc_clnt *clnt, unsigned long event,
186 struct super_block *sb)
188 struct dentry *dentry;
191 case RPC_PIPEFS_MOUNT:
192 dentry = rpc_setup_pipedir_sb(sb, clnt);
196 return PTR_ERR(dentry);
198 case RPC_PIPEFS_UMOUNT:
199 __rpc_clnt_remove_pipedir(clnt);
202 printk(KERN_ERR "%s: unknown event: %ld\n", __func__, event);
208 static int __rpc_pipefs_event(struct rpc_clnt *clnt, unsigned long event,
209 struct super_block *sb)
213 for (;; clnt = clnt->cl_parent) {
214 if (!rpc_clnt_skip_event(clnt, event))
215 error = __rpc_clnt_handle_event(clnt, event, sb);
216 if (error || clnt == clnt->cl_parent)
222 static struct rpc_clnt *rpc_get_client_for_event(struct net *net, int event)
224 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
225 struct rpc_clnt *clnt;
227 spin_lock(&sn->rpc_client_lock);
228 list_for_each_entry(clnt, &sn->all_clients, cl_clients) {
229 if (rpc_clnt_skip_event(clnt, event))
231 spin_unlock(&sn->rpc_client_lock);
234 spin_unlock(&sn->rpc_client_lock);
238 static int rpc_pipefs_event(struct notifier_block *nb, unsigned long event,
241 struct super_block *sb = ptr;
242 struct rpc_clnt *clnt;
245 while ((clnt = rpc_get_client_for_event(sb->s_fs_info, event))) {
246 error = __rpc_pipefs_event(clnt, event, sb);
253 static struct notifier_block rpc_clients_block = {
254 .notifier_call = rpc_pipefs_event,
255 .priority = SUNRPC_PIPEFS_RPC_PRIO,
258 int rpc_clients_notifier_register(void)
260 return rpc_pipefs_notifier_register(&rpc_clients_block);
263 void rpc_clients_notifier_unregister(void)
265 return rpc_pipefs_notifier_unregister(&rpc_clients_block);
268 static struct rpc_xprt *rpc_clnt_set_transport(struct rpc_clnt *clnt,
269 struct rpc_xprt *xprt,
270 const struct rpc_timeout *timeout)
272 struct rpc_xprt *old;
274 spin_lock(&clnt->cl_lock);
275 old = rcu_dereference_protected(clnt->cl_xprt,
276 lockdep_is_held(&clnt->cl_lock));
278 if (!xprt_bound(xprt))
279 clnt->cl_autobind = 1;
281 clnt->cl_timeout = timeout;
282 rcu_assign_pointer(clnt->cl_xprt, xprt);
283 spin_unlock(&clnt->cl_lock);
288 static void rpc_clnt_set_nodename(struct rpc_clnt *clnt, const char *nodename)
290 clnt->cl_nodelen = strlcpy(clnt->cl_nodename,
291 nodename, sizeof(clnt->cl_nodename));
294 static int rpc_client_register(struct rpc_clnt *clnt,
295 rpc_authflavor_t pseudoflavor,
296 const char *client_name)
298 struct rpc_auth_create_args auth_args = {
299 .pseudoflavor = pseudoflavor,
300 .target_name = client_name,
302 struct rpc_auth *auth;
303 struct net *net = rpc_net_ns(clnt);
304 struct super_block *pipefs_sb;
307 rpc_clnt_debugfs_register(clnt);
309 pipefs_sb = rpc_get_sb_net(net);
311 err = rpc_setup_pipedir(pipefs_sb, clnt);
316 rpc_register_client(clnt);
320 auth = rpcauth_create(&auth_args, clnt);
322 dprintk("RPC: Couldn't create auth handle (flavor %u)\n",
329 pipefs_sb = rpc_get_sb_net(net);
330 rpc_unregister_client(clnt);
331 __rpc_clnt_remove_pipedir(clnt);
335 rpc_sysfs_client_destroy(clnt);
336 rpc_clnt_debugfs_unregister(clnt);
340 static DEFINE_IDA(rpc_clids);
342 void rpc_cleanup_clids(void)
344 ida_destroy(&rpc_clids);
347 static int rpc_alloc_clid(struct rpc_clnt *clnt)
351 clid = ida_alloc(&rpc_clids, GFP_KERNEL);
354 clnt->cl_clid = clid;
358 static void rpc_free_clid(struct rpc_clnt *clnt)
360 ida_free(&rpc_clids, clnt->cl_clid);
363 static struct rpc_clnt * rpc_new_client(const struct rpc_create_args *args,
364 struct rpc_xprt_switch *xps,
365 struct rpc_xprt *xprt,
366 struct rpc_clnt *parent)
368 const struct rpc_program *program = args->program;
369 const struct rpc_version *version;
370 struct rpc_clnt *clnt = NULL;
371 const struct rpc_timeout *timeout;
372 const char *nodename = args->nodename;
380 if (args->version >= program->nrvers)
382 version = program->version[args->version];
387 clnt = kzalloc(sizeof(*clnt), GFP_KERNEL);
390 clnt->cl_parent = parent ? : clnt;
392 err = rpc_alloc_clid(clnt);
396 clnt->cl_cred = get_cred(args->cred);
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 = args->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);
420 xprt_iter_init(&clnt->cl_xpi, xps);
421 xprt_switch_put(xps);
423 clnt->cl_rtt = &clnt->cl_rtt_default;
424 rpc_init_rtt(&clnt->cl_rtt_default, clnt->cl_timeout->to_initval);
426 refcount_set(&clnt->cl_count, 1);
428 if (nodename == NULL)
429 nodename = utsname()->nodename;
430 /* save the nodename */
431 rpc_clnt_set_nodename(clnt, nodename);
433 rpc_sysfs_client_setup(clnt, xps, rpc_net_ns(clnt));
434 err = rpc_client_register(clnt, args->authflavor, args->client_name);
438 refcount_inc(&parent->cl_count);
440 trace_rpc_clnt_new(clnt, xprt, program->name, args->servername);
444 rpc_free_iostats(clnt->cl_metrics);
446 put_cred(clnt->cl_cred);
453 xprt_switch_put(xps);
455 trace_rpc_clnt_new_err(program->name, args->servername, err);
459 static struct rpc_clnt *rpc_create_xprt(struct rpc_create_args *args,
460 struct rpc_xprt *xprt)
462 struct rpc_clnt *clnt = NULL;
463 struct rpc_xprt_switch *xps;
465 if (args->bc_xprt && args->bc_xprt->xpt_bc_xps) {
466 WARN_ON_ONCE(!(args->protocol & XPRT_TRANSPORT_BC));
467 xps = args->bc_xprt->xpt_bc_xps;
468 xprt_switch_get(xps);
470 xps = xprt_switch_alloc(xprt, GFP_KERNEL);
473 return ERR_PTR(-ENOMEM);
476 xprt_switch_get(xps);
477 xprt->bc_xprt->xpt_bc_xps = xps;
480 clnt = rpc_new_client(args, xps, xprt, NULL);
484 if (!(args->flags & RPC_CLNT_CREATE_NOPING)) {
485 int err = rpc_ping(clnt);
487 rpc_shutdown_client(clnt);
490 } else if (args->flags & RPC_CLNT_CREATE_CONNECTED) {
491 int err = rpc_ping_noreply(clnt);
493 rpc_shutdown_client(clnt);
498 clnt->cl_softrtry = 1;
499 if (args->flags & (RPC_CLNT_CREATE_HARDRTRY|RPC_CLNT_CREATE_SOFTERR)) {
500 clnt->cl_softrtry = 0;
501 if (args->flags & RPC_CLNT_CREATE_SOFTERR)
502 clnt->cl_softerr = 1;
505 if (args->flags & RPC_CLNT_CREATE_AUTOBIND)
506 clnt->cl_autobind = 1;
507 if (args->flags & RPC_CLNT_CREATE_NO_RETRANS_TIMEOUT)
508 clnt->cl_noretranstimeo = 1;
509 if (args->flags & RPC_CLNT_CREATE_DISCRTRY)
510 clnt->cl_discrtry = 1;
511 if (!(args->flags & RPC_CLNT_CREATE_QUIET))
518 * rpc_create - create an RPC client and transport with one call
519 * @args: rpc_clnt create argument structure
521 * Creates and initializes an RPC transport and an RPC client.
523 * It can ping the server in order to determine if it is up, and to see if
524 * it supports this program and version. RPC_CLNT_CREATE_NOPING disables
525 * this behavior so asynchronous tasks can also use rpc_create.
527 struct rpc_clnt *rpc_create(struct rpc_create_args *args)
529 struct rpc_xprt *xprt;
530 struct xprt_create xprtargs = {
532 .ident = args->protocol,
533 .srcaddr = args->saddress,
534 .dstaddr = args->address,
535 .addrlen = args->addrsize,
536 .servername = args->servername,
537 .bc_xprt = args->bc_xprt,
540 struct rpc_clnt *clnt;
544 WARN_ON_ONCE(!(args->protocol & XPRT_TRANSPORT_BC));
545 xprt = args->bc_xprt->xpt_bc_xprt;
548 return rpc_create_xprt(args, xprt);
552 if (args->flags & RPC_CLNT_CREATE_INFINITE_SLOTS)
553 xprtargs.flags |= XPRT_CREATE_INFINITE_SLOTS;
554 if (args->flags & RPC_CLNT_CREATE_NO_IDLE_TIMEOUT)
555 xprtargs.flags |= XPRT_CREATE_NO_IDLE_TIMEOUT;
557 * If the caller chooses not to specify a hostname, whip
558 * up a string representation of the passed-in address.
560 if (xprtargs.servername == NULL) {
561 struct sockaddr_un *sun =
562 (struct sockaddr_un *)args->address;
563 struct sockaddr_in *sin =
564 (struct sockaddr_in *)args->address;
565 struct sockaddr_in6 *sin6 =
566 (struct sockaddr_in6 *)args->address;
568 servername[0] = '\0';
569 switch (args->address->sa_family) {
571 snprintf(servername, sizeof(servername), "%s",
575 snprintf(servername, sizeof(servername), "%pI4",
576 &sin->sin_addr.s_addr);
579 snprintf(servername, sizeof(servername), "%pI6",
583 /* caller wants default server name, but
584 * address family isn't recognized. */
585 return ERR_PTR(-EINVAL);
587 xprtargs.servername = servername;
590 xprt = xprt_create_transport(&xprtargs);
592 return (struct rpc_clnt *)xprt;
595 * By default, kernel RPC client connects from a reserved port.
596 * CAP_NET_BIND_SERVICE will not be set for unprivileged requesters,
597 * but it is always enabled for rpciod, which handles the connect
601 if (args->flags & RPC_CLNT_CREATE_NONPRIVPORT)
604 if (args->flags & RPC_CLNT_CREATE_REUSEPORT)
607 clnt = rpc_create_xprt(args, xprt);
608 if (IS_ERR(clnt) || args->nconnect <= 1)
611 for (i = 0; i < args->nconnect - 1; i++) {
612 if (rpc_clnt_add_xprt(clnt, &xprtargs, NULL, NULL) < 0)
617 EXPORT_SYMBOL_GPL(rpc_create);
620 * This function clones the RPC client structure. It allows us to share the
621 * same transport while varying parameters such as the authentication
624 static struct rpc_clnt *__rpc_clone_client(struct rpc_create_args *args,
625 struct rpc_clnt *clnt)
627 struct rpc_xprt_switch *xps;
628 struct rpc_xprt *xprt;
629 struct rpc_clnt *new;
634 xprt = xprt_get(rcu_dereference(clnt->cl_xprt));
635 xps = xprt_switch_get(rcu_dereference(clnt->cl_xpi.xpi_xpswitch));
637 if (xprt == NULL || xps == NULL) {
639 xprt_switch_put(xps);
642 args->servername = xprt->servername;
643 args->nodename = clnt->cl_nodename;
645 new = rpc_new_client(args, xps, xprt, clnt);
649 /* Turn off autobind on clones */
650 new->cl_autobind = 0;
651 new->cl_softrtry = clnt->cl_softrtry;
652 new->cl_softerr = clnt->cl_softerr;
653 new->cl_noretranstimeo = clnt->cl_noretranstimeo;
654 new->cl_discrtry = clnt->cl_discrtry;
655 new->cl_chatty = clnt->cl_chatty;
656 new->cl_principal = clnt->cl_principal;
657 new->cl_max_connect = clnt->cl_max_connect;
661 trace_rpc_clnt_clone_err(clnt, err);
666 * rpc_clone_client - Clone an RPC client structure
668 * @clnt: RPC client whose parameters are copied
670 * Returns a fresh RPC client or an ERR_PTR.
672 struct rpc_clnt *rpc_clone_client(struct rpc_clnt *clnt)
674 struct rpc_create_args args = {
675 .program = clnt->cl_program,
676 .prognumber = clnt->cl_prog,
677 .version = clnt->cl_vers,
678 .authflavor = clnt->cl_auth->au_flavor,
679 .cred = clnt->cl_cred,
680 .stats = clnt->cl_stats,
682 return __rpc_clone_client(&args, clnt);
684 EXPORT_SYMBOL_GPL(rpc_clone_client);
687 * rpc_clone_client_set_auth - Clone an RPC client structure and set its auth
689 * @clnt: RPC client whose parameters are copied
690 * @flavor: security flavor for new client
692 * Returns a fresh RPC client or an ERR_PTR.
695 rpc_clone_client_set_auth(struct rpc_clnt *clnt, rpc_authflavor_t flavor)
697 struct rpc_create_args args = {
698 .program = clnt->cl_program,
699 .prognumber = clnt->cl_prog,
700 .version = clnt->cl_vers,
701 .authflavor = flavor,
702 .cred = clnt->cl_cred,
703 .stats = clnt->cl_stats,
705 return __rpc_clone_client(&args, clnt);
707 EXPORT_SYMBOL_GPL(rpc_clone_client_set_auth);
710 * rpc_switch_client_transport: switch the RPC transport on the fly
711 * @clnt: pointer to a struct rpc_clnt
712 * @args: pointer to the new transport arguments
713 * @timeout: pointer to the new timeout parameters
715 * This function allows the caller to switch the RPC transport for the
716 * rpc_clnt structure 'clnt' to allow it to connect to a mirrored NFS
717 * server, for instance. It assumes that the caller has ensured that
718 * there are no active RPC tasks by using some form of locking.
720 * Returns zero if "clnt" is now using the new xprt. Otherwise a
721 * negative errno is returned, and "clnt" continues to use the old
724 int rpc_switch_client_transport(struct rpc_clnt *clnt,
725 struct xprt_create *args,
726 const struct rpc_timeout *timeout)
728 const struct rpc_timeout *old_timeo;
729 rpc_authflavor_t pseudoflavor;
730 struct rpc_xprt_switch *xps, *oldxps;
731 struct rpc_xprt *xprt, *old;
732 struct rpc_clnt *parent;
735 xprt = xprt_create_transport(args);
737 return PTR_ERR(xprt);
739 xps = xprt_switch_alloc(xprt, GFP_KERNEL);
745 pseudoflavor = clnt->cl_auth->au_flavor;
747 old_timeo = clnt->cl_timeout;
748 old = rpc_clnt_set_transport(clnt, xprt, timeout);
749 oldxps = xprt_iter_xchg_switch(&clnt->cl_xpi, xps);
751 rpc_unregister_client(clnt);
752 __rpc_clnt_remove_pipedir(clnt);
753 rpc_sysfs_client_destroy(clnt);
754 rpc_clnt_debugfs_unregister(clnt);
757 * A new transport was created. "clnt" therefore
758 * becomes the root of a new cl_parent tree. clnt's
759 * children, if it has any, still point to the old xprt.
761 parent = clnt->cl_parent;
762 clnt->cl_parent = clnt;
765 * The old rpc_auth cache cannot be re-used. GSS
766 * contexts in particular are between a single
769 err = rpc_client_register(clnt, pseudoflavor, NULL);
775 rpc_release_client(parent);
776 xprt_switch_put(oldxps);
778 trace_rpc_clnt_replace_xprt(clnt);
782 xps = xprt_iter_xchg_switch(&clnt->cl_xpi, oldxps);
783 rpc_clnt_set_transport(clnt, old, old_timeo);
784 clnt->cl_parent = parent;
785 rpc_client_register(clnt, pseudoflavor, NULL);
786 xprt_switch_put(xps);
788 trace_rpc_clnt_replace_xprt_err(clnt);
791 EXPORT_SYMBOL_GPL(rpc_switch_client_transport);
794 int _rpc_clnt_xprt_iter_init(struct rpc_clnt *clnt, struct rpc_xprt_iter *xpi,
795 void func(struct rpc_xprt_iter *xpi, struct rpc_xprt_switch *xps))
797 struct rpc_xprt_switch *xps;
800 xps = xprt_switch_get(rcu_dereference(clnt->cl_xpi.xpi_xpswitch));
805 xprt_switch_put(xps);
810 int rpc_clnt_xprt_iter_init(struct rpc_clnt *clnt, struct rpc_xprt_iter *xpi)
812 return _rpc_clnt_xprt_iter_init(clnt, xpi, xprt_iter_init_listall);
816 int rpc_clnt_xprt_iter_offline_init(struct rpc_clnt *clnt,
817 struct rpc_xprt_iter *xpi)
819 return _rpc_clnt_xprt_iter_init(clnt, xpi, xprt_iter_init_listoffline);
823 * rpc_clnt_iterate_for_each_xprt - Apply a function to all transports
824 * @clnt: pointer to client
825 * @fn: function to apply
826 * @data: void pointer to function data
828 * Iterates through the list of RPC transports currently attached to the
829 * client and applies the function fn(clnt, xprt, data).
831 * On error, the iteration stops, and the function returns the error value.
833 int rpc_clnt_iterate_for_each_xprt(struct rpc_clnt *clnt,
834 int (*fn)(struct rpc_clnt *, struct rpc_xprt *, void *),
837 struct rpc_xprt_iter xpi;
840 ret = rpc_clnt_xprt_iter_init(clnt, &xpi);
844 struct rpc_xprt *xprt = xprt_iter_get_next(&xpi);
848 ret = fn(clnt, xprt, data);
853 xprt_iter_destroy(&xpi);
856 EXPORT_SYMBOL_GPL(rpc_clnt_iterate_for_each_xprt);
859 * Kill all tasks for the given client.
860 * XXX: kill their descendants as well?
862 void rpc_killall_tasks(struct rpc_clnt *clnt)
864 struct rpc_task *rovr;
867 if (list_empty(&clnt->cl_tasks))
871 * Spin lock all_tasks to prevent changes...
873 trace_rpc_clnt_killall(clnt);
874 spin_lock(&clnt->cl_lock);
875 list_for_each_entry(rovr, &clnt->cl_tasks, tk_task)
876 rpc_signal_task(rovr);
877 spin_unlock(&clnt->cl_lock);
879 EXPORT_SYMBOL_GPL(rpc_killall_tasks);
882 * rpc_cancel_tasks - try to cancel a set of RPC tasks
883 * @clnt: Pointer to RPC client
884 * @error: RPC task error value to set
885 * @fnmatch: Pointer to selector function
888 * Uses @fnmatch to define a set of RPC tasks that are to be cancelled.
889 * The argument @error must be a negative error value.
891 unsigned long rpc_cancel_tasks(struct rpc_clnt *clnt, int error,
892 bool (*fnmatch)(const struct rpc_task *,
896 struct rpc_task *task;
897 unsigned long count = 0;
899 if (list_empty(&clnt->cl_tasks))
902 * Spin lock all_tasks to prevent changes...
904 spin_lock(&clnt->cl_lock);
905 list_for_each_entry(task, &clnt->cl_tasks, tk_task) {
906 if (!RPC_IS_ACTIVATED(task))
908 if (!fnmatch(task, data))
910 rpc_task_try_cancel(task, error);
913 spin_unlock(&clnt->cl_lock);
916 EXPORT_SYMBOL_GPL(rpc_cancel_tasks);
918 static int rpc_clnt_disconnect_xprt(struct rpc_clnt *clnt,
919 struct rpc_xprt *xprt, void *dummy)
921 if (xprt_connected(xprt))
922 xprt_force_disconnect(xprt);
926 void rpc_clnt_disconnect(struct rpc_clnt *clnt)
928 rpc_clnt_iterate_for_each_xprt(clnt, rpc_clnt_disconnect_xprt, NULL);
930 EXPORT_SYMBOL_GPL(rpc_clnt_disconnect);
933 * Properly shut down an RPC client, terminating all outstanding
936 void rpc_shutdown_client(struct rpc_clnt *clnt)
940 trace_rpc_clnt_shutdown(clnt);
942 while (!list_empty(&clnt->cl_tasks)) {
943 rpc_killall_tasks(clnt);
944 wait_event_timeout(destroy_wait,
945 list_empty(&clnt->cl_tasks), 1*HZ);
948 rpc_release_client(clnt);
950 EXPORT_SYMBOL_GPL(rpc_shutdown_client);
955 static void rpc_free_client_work(struct work_struct *work)
957 struct rpc_clnt *clnt = container_of(work, struct rpc_clnt, cl_work);
959 trace_rpc_clnt_free(clnt);
961 /* These might block on processes that might allocate memory,
962 * so they cannot be called in rpciod, so they are handled separately
965 rpc_sysfs_client_destroy(clnt);
966 rpc_clnt_debugfs_unregister(clnt);
968 rpc_clnt_remove_pipedir(clnt);
969 xprt_put(rcu_dereference_raw(clnt->cl_xprt));
974 static struct rpc_clnt *
975 rpc_free_client(struct rpc_clnt *clnt)
977 struct rpc_clnt *parent = NULL;
979 trace_rpc_clnt_release(clnt);
980 if (clnt->cl_parent != clnt)
981 parent = clnt->cl_parent;
982 rpc_unregister_client(clnt);
983 rpc_free_iostats(clnt->cl_metrics);
984 clnt->cl_metrics = NULL;
985 xprt_iter_destroy(&clnt->cl_xpi);
986 put_cred(clnt->cl_cred);
988 INIT_WORK(&clnt->cl_work, rpc_free_client_work);
989 schedule_work(&clnt->cl_work);
996 static struct rpc_clnt *
997 rpc_free_auth(struct rpc_clnt *clnt)
1000 * Note: RPCSEC_GSS may need to send NULL RPC calls in order to
1001 * release remaining GSS contexts. This mechanism ensures
1002 * that it can do so safely.
1004 if (clnt->cl_auth != NULL) {
1005 rpcauth_release(clnt->cl_auth);
1006 clnt->cl_auth = NULL;
1008 if (refcount_dec_and_test(&clnt->cl_count))
1009 return rpc_free_client(clnt);
1014 * Release reference to the RPC client
1017 rpc_release_client(struct rpc_clnt *clnt)
1020 if (list_empty(&clnt->cl_tasks))
1021 wake_up(&destroy_wait);
1022 if (refcount_dec_not_one(&clnt->cl_count))
1024 clnt = rpc_free_auth(clnt);
1025 } while (clnt != NULL);
1027 EXPORT_SYMBOL_GPL(rpc_release_client);
1030 * rpc_bind_new_program - bind a new RPC program to an existing client
1031 * @old: old rpc_client
1032 * @program: rpc program to set
1033 * @vers: rpc program version
1035 * Clones the rpc client and sets up a new RPC program. This is mainly
1036 * of use for enabling different RPC programs to share the same transport.
1037 * The Sun NFSv2/v3 ACL protocol can do this.
1039 struct rpc_clnt *rpc_bind_new_program(struct rpc_clnt *old,
1040 const struct rpc_program *program,
1043 struct rpc_create_args args = {
1045 .prognumber = program->number,
1047 .authflavor = old->cl_auth->au_flavor,
1048 .cred = old->cl_cred,
1049 .stats = old->cl_stats,
1051 struct rpc_clnt *clnt;
1054 clnt = __rpc_clone_client(&args, old);
1057 err = rpc_ping(clnt);
1059 rpc_shutdown_client(clnt);
1060 clnt = ERR_PTR(err);
1065 EXPORT_SYMBOL_GPL(rpc_bind_new_program);
1068 rpc_task_get_xprt(struct rpc_clnt *clnt, struct rpc_xprt *xprt)
1070 struct rpc_xprt_switch *xps;
1075 xps = rcu_dereference(clnt->cl_xpi.xpi_xpswitch);
1076 atomic_long_inc(&xps->xps_queuelen);
1078 atomic_long_inc(&xprt->queuelen);
1084 rpc_task_release_xprt(struct rpc_clnt *clnt, struct rpc_xprt *xprt)
1086 struct rpc_xprt_switch *xps;
1088 atomic_long_dec(&xprt->queuelen);
1090 xps = rcu_dereference(clnt->cl_xpi.xpi_xpswitch);
1091 atomic_long_dec(&xps->xps_queuelen);
1097 void rpc_task_release_transport(struct rpc_task *task)
1099 struct rpc_xprt *xprt = task->tk_xprt;
1102 task->tk_xprt = NULL;
1103 if (task->tk_client)
1104 rpc_task_release_xprt(task->tk_client, xprt);
1109 EXPORT_SYMBOL_GPL(rpc_task_release_transport);
1111 void rpc_task_release_client(struct rpc_task *task)
1113 struct rpc_clnt *clnt = task->tk_client;
1115 rpc_task_release_transport(task);
1117 /* Remove from client task list */
1118 spin_lock(&clnt->cl_lock);
1119 list_del(&task->tk_task);
1120 spin_unlock(&clnt->cl_lock);
1121 task->tk_client = NULL;
1123 rpc_release_client(clnt);
1127 static struct rpc_xprt *
1128 rpc_task_get_first_xprt(struct rpc_clnt *clnt)
1130 struct rpc_xprt *xprt;
1133 xprt = xprt_get(rcu_dereference(clnt->cl_xprt));
1135 return rpc_task_get_xprt(clnt, xprt);
1138 static struct rpc_xprt *
1139 rpc_task_get_next_xprt(struct rpc_clnt *clnt)
1141 return rpc_task_get_xprt(clnt, xprt_iter_get_next(&clnt->cl_xpi));
1145 void rpc_task_set_transport(struct rpc_task *task, struct rpc_clnt *clnt)
1147 if (task->tk_xprt) {
1148 if (!(test_bit(XPRT_OFFLINE, &task->tk_xprt->state) &&
1149 (task->tk_flags & RPC_TASK_MOVEABLE)))
1152 xprt_put(task->tk_xprt);
1154 if (task->tk_flags & RPC_TASK_NO_ROUND_ROBIN)
1155 task->tk_xprt = rpc_task_get_first_xprt(clnt);
1157 task->tk_xprt = rpc_task_get_next_xprt(clnt);
1161 void rpc_task_set_client(struct rpc_task *task, struct rpc_clnt *clnt)
1163 rpc_task_set_transport(task, clnt);
1164 task->tk_client = clnt;
1165 refcount_inc(&clnt->cl_count);
1166 if (clnt->cl_softrtry)
1167 task->tk_flags |= RPC_TASK_SOFT;
1168 if (clnt->cl_softerr)
1169 task->tk_flags |= RPC_TASK_TIMEOUT;
1170 if (clnt->cl_noretranstimeo)
1171 task->tk_flags |= RPC_TASK_NO_RETRANS_TIMEOUT;
1172 /* Add to the client's list of all tasks */
1173 spin_lock(&clnt->cl_lock);
1174 list_add_tail(&task->tk_task, &clnt->cl_tasks);
1175 spin_unlock(&clnt->cl_lock);
1179 rpc_task_set_rpc_message(struct rpc_task *task, const struct rpc_message *msg)
1182 task->tk_msg.rpc_proc = msg->rpc_proc;
1183 task->tk_msg.rpc_argp = msg->rpc_argp;
1184 task->tk_msg.rpc_resp = msg->rpc_resp;
1185 task->tk_msg.rpc_cred = msg->rpc_cred;
1186 if (!(task->tk_flags & RPC_TASK_CRED_NOREF))
1187 get_cred(task->tk_msg.rpc_cred);
1192 * Default callback for async RPC calls
1195 rpc_default_callback(struct rpc_task *task, void *data)
1199 static const struct rpc_call_ops rpc_default_ops = {
1200 .rpc_call_done = rpc_default_callback,
1204 * rpc_run_task - Allocate a new RPC task, then run rpc_execute against it
1205 * @task_setup_data: pointer to task initialisation data
1207 struct rpc_task *rpc_run_task(const struct rpc_task_setup *task_setup_data)
1209 struct rpc_task *task;
1211 task = rpc_new_task(task_setup_data);
1215 if (!RPC_IS_ASYNC(task))
1216 task->tk_flags |= RPC_TASK_CRED_NOREF;
1218 rpc_task_set_client(task, task_setup_data->rpc_client);
1219 rpc_task_set_rpc_message(task, task_setup_data->rpc_message);
1221 if (task->tk_action == NULL)
1222 rpc_call_start(task);
1224 atomic_inc(&task->tk_count);
1228 EXPORT_SYMBOL_GPL(rpc_run_task);
1231 * rpc_call_sync - Perform a synchronous RPC call
1232 * @clnt: pointer to RPC client
1233 * @msg: RPC call parameters
1234 * @flags: RPC call flags
1236 int rpc_call_sync(struct rpc_clnt *clnt, const struct rpc_message *msg, int flags)
1238 struct rpc_task *task;
1239 struct rpc_task_setup task_setup_data = {
1242 .callback_ops = &rpc_default_ops,
1247 WARN_ON_ONCE(flags & RPC_TASK_ASYNC);
1248 if (flags & RPC_TASK_ASYNC) {
1249 rpc_release_calldata(task_setup_data.callback_ops,
1250 task_setup_data.callback_data);
1254 task = rpc_run_task(&task_setup_data);
1256 return PTR_ERR(task);
1257 status = task->tk_status;
1261 EXPORT_SYMBOL_GPL(rpc_call_sync);
1264 * rpc_call_async - Perform an asynchronous RPC call
1265 * @clnt: pointer to RPC client
1266 * @msg: RPC call parameters
1267 * @flags: RPC call flags
1268 * @tk_ops: RPC call ops
1269 * @data: user call data
1272 rpc_call_async(struct rpc_clnt *clnt, const struct rpc_message *msg, int flags,
1273 const struct rpc_call_ops *tk_ops, void *data)
1275 struct rpc_task *task;
1276 struct rpc_task_setup task_setup_data = {
1279 .callback_ops = tk_ops,
1280 .callback_data = data,
1281 .flags = flags|RPC_TASK_ASYNC,
1284 task = rpc_run_task(&task_setup_data);
1286 return PTR_ERR(task);
1290 EXPORT_SYMBOL_GPL(rpc_call_async);
1292 #if defined(CONFIG_SUNRPC_BACKCHANNEL)
1293 static void call_bc_encode(struct rpc_task *task);
1296 * rpc_run_bc_task - Allocate a new RPC task for backchannel use, then run
1297 * rpc_execute against it
1300 struct rpc_task *rpc_run_bc_task(struct rpc_rqst *req)
1302 struct rpc_task *task;
1303 struct rpc_task_setup task_setup_data = {
1304 .callback_ops = &rpc_default_ops,
1305 .flags = RPC_TASK_SOFTCONN |
1306 RPC_TASK_NO_RETRANS_TIMEOUT,
1309 dprintk("RPC: rpc_run_bc_task req= %p\n", req);
1311 * Create an rpc_task to send the data
1313 task = rpc_new_task(&task_setup_data);
1315 xprt_free_bc_request(req);
1319 xprt_init_bc_request(req, task);
1321 task->tk_action = call_bc_encode;
1322 atomic_inc(&task->tk_count);
1323 WARN_ON_ONCE(atomic_read(&task->tk_count) != 2);
1326 dprintk("RPC: rpc_run_bc_task: task= %p\n", task);
1329 #endif /* CONFIG_SUNRPC_BACKCHANNEL */
1332 * rpc_prepare_reply_pages - Prepare to receive a reply data payload into pages
1333 * @req: RPC request to prepare
1334 * @pages: vector of struct page pointers
1335 * @base: offset in first page where receive should start, in bytes
1336 * @len: expected size of the upper layer data payload, in bytes
1337 * @hdrsize: expected size of upper layer reply header, in XDR words
1340 void rpc_prepare_reply_pages(struct rpc_rqst *req, struct page **pages,
1341 unsigned int base, unsigned int len,
1342 unsigned int hdrsize)
1344 hdrsize += RPC_REPHDRSIZE + req->rq_cred->cr_auth->au_ralign;
1346 xdr_inline_pages(&req->rq_rcv_buf, hdrsize << 2, pages, base, len);
1347 trace_rpc_xdr_reply_pages(req->rq_task, &req->rq_rcv_buf);
1349 EXPORT_SYMBOL_GPL(rpc_prepare_reply_pages);
1352 rpc_call_start(struct rpc_task *task)
1354 task->tk_action = call_start;
1356 EXPORT_SYMBOL_GPL(rpc_call_start);
1359 * rpc_peeraddr - extract remote peer address from clnt's xprt
1360 * @clnt: RPC client structure
1361 * @buf: target buffer
1362 * @bufsize: length of target buffer
1364 * Returns the number of bytes that are actually in the stored address.
1366 size_t rpc_peeraddr(struct rpc_clnt *clnt, struct sockaddr *buf, size_t bufsize)
1369 struct rpc_xprt *xprt;
1372 xprt = rcu_dereference(clnt->cl_xprt);
1374 bytes = xprt->addrlen;
1375 if (bytes > bufsize)
1377 memcpy(buf, &xprt->addr, bytes);
1382 EXPORT_SYMBOL_GPL(rpc_peeraddr);
1385 * rpc_peeraddr2str - return remote peer address in printable format
1386 * @clnt: RPC client structure
1387 * @format: address format
1389 * NB: the lifetime of the memory referenced by the returned pointer is
1390 * the same as the rpc_xprt itself. As long as the caller uses this
1391 * pointer, it must hold the RCU read lock.
1393 const char *rpc_peeraddr2str(struct rpc_clnt *clnt,
1394 enum rpc_display_format_t format)
1396 struct rpc_xprt *xprt;
1398 xprt = rcu_dereference(clnt->cl_xprt);
1400 if (xprt->address_strings[format] != NULL)
1401 return xprt->address_strings[format];
1403 return "unprintable";
1405 EXPORT_SYMBOL_GPL(rpc_peeraddr2str);
1407 static const struct sockaddr_in rpc_inaddr_loopback = {
1408 .sin_family = AF_INET,
1409 .sin_addr.s_addr = htonl(INADDR_ANY),
1412 static const struct sockaddr_in6 rpc_in6addr_loopback = {
1413 .sin6_family = AF_INET6,
1414 .sin6_addr = IN6ADDR_ANY_INIT,
1418 * Try a getsockname() on a connected datagram socket. Using a
1419 * connected datagram socket prevents leaving a socket in TIME_WAIT.
1420 * This conserves the ephemeral port number space.
1422 * Returns zero and fills in "buf" if successful; otherwise, a
1423 * negative errno is returned.
1425 static int rpc_sockname(struct net *net, struct sockaddr *sap, size_t salen,
1426 struct sockaddr *buf)
1428 struct socket *sock;
1431 err = __sock_create(net, sap->sa_family,
1432 SOCK_DGRAM, IPPROTO_UDP, &sock, 1);
1434 dprintk("RPC: can't create UDP socket (%d)\n", err);
1438 switch (sap->sa_family) {
1440 err = kernel_bind(sock,
1441 (struct sockaddr *)&rpc_inaddr_loopback,
1442 sizeof(rpc_inaddr_loopback));
1445 err = kernel_bind(sock,
1446 (struct sockaddr *)&rpc_in6addr_loopback,
1447 sizeof(rpc_in6addr_loopback));
1450 err = -EAFNOSUPPORT;
1454 dprintk("RPC: can't bind UDP socket (%d)\n", err);
1458 err = kernel_connect(sock, sap, salen, 0);
1460 dprintk("RPC: can't connect UDP socket (%d)\n", err);
1464 err = kernel_getsockname(sock, buf);
1466 dprintk("RPC: getsockname failed (%d)\n", err);
1471 if (buf->sa_family == AF_INET6) {
1472 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)buf;
1473 sin6->sin6_scope_id = 0;
1475 dprintk("RPC: %s succeeded\n", __func__);
1484 * Scraping a connected socket failed, so we don't have a useable
1485 * local address. Fallback: generate an address that will prevent
1486 * the server from calling us back.
1488 * Returns zero and fills in "buf" if successful; otherwise, a
1489 * negative errno is returned.
1491 static int rpc_anyaddr(int family, struct sockaddr *buf, size_t buflen)
1495 if (buflen < sizeof(rpc_inaddr_loopback))
1497 memcpy(buf, &rpc_inaddr_loopback,
1498 sizeof(rpc_inaddr_loopback));
1501 if (buflen < sizeof(rpc_in6addr_loopback))
1503 memcpy(buf, &rpc_in6addr_loopback,
1504 sizeof(rpc_in6addr_loopback));
1507 dprintk("RPC: %s: address family not supported\n",
1509 return -EAFNOSUPPORT;
1511 dprintk("RPC: %s: succeeded\n", __func__);
1516 * rpc_localaddr - discover local endpoint address for an RPC client
1517 * @clnt: RPC client structure
1518 * @buf: target buffer
1519 * @buflen: size of target buffer, in bytes
1521 * Returns zero and fills in "buf" and "buflen" if successful;
1522 * otherwise, a negative errno is returned.
1524 * This works even if the underlying transport is not currently connected,
1525 * or if the upper layer never previously provided a source address.
1527 * The result of this function call is transient: multiple calls in
1528 * succession may give different results, depending on how local
1529 * networking configuration changes over time.
1531 int rpc_localaddr(struct rpc_clnt *clnt, struct sockaddr *buf, size_t buflen)
1533 struct sockaddr_storage address;
1534 struct sockaddr *sap = (struct sockaddr *)&address;
1535 struct rpc_xprt *xprt;
1541 xprt = rcu_dereference(clnt->cl_xprt);
1542 salen = xprt->addrlen;
1543 memcpy(sap, &xprt->addr, salen);
1544 net = get_net(xprt->xprt_net);
1547 rpc_set_port(sap, 0);
1548 err = rpc_sockname(net, sap, salen, buf);
1551 /* Couldn't discover local address, return ANYADDR */
1552 return rpc_anyaddr(sap->sa_family, buf, buflen);
1555 EXPORT_SYMBOL_GPL(rpc_localaddr);
1558 rpc_setbufsize(struct rpc_clnt *clnt, unsigned int sndsize, unsigned int rcvsize)
1560 struct rpc_xprt *xprt;
1563 xprt = rcu_dereference(clnt->cl_xprt);
1564 if (xprt->ops->set_buffer_size)
1565 xprt->ops->set_buffer_size(xprt, sndsize, rcvsize);
1568 EXPORT_SYMBOL_GPL(rpc_setbufsize);
1571 * rpc_net_ns - Get the network namespace for this RPC client
1572 * @clnt: RPC client to query
1575 struct net *rpc_net_ns(struct rpc_clnt *clnt)
1580 ret = rcu_dereference(clnt->cl_xprt)->xprt_net;
1584 EXPORT_SYMBOL_GPL(rpc_net_ns);
1587 * rpc_max_payload - Get maximum payload size for a transport, in bytes
1588 * @clnt: RPC client to query
1590 * For stream transports, this is one RPC record fragment (see RFC
1591 * 1831), as we don't support multi-record requests yet. For datagram
1592 * transports, this is the size of an IP packet minus the IP, UDP, and
1595 size_t rpc_max_payload(struct rpc_clnt *clnt)
1600 ret = rcu_dereference(clnt->cl_xprt)->max_payload;
1604 EXPORT_SYMBOL_GPL(rpc_max_payload);
1607 * rpc_max_bc_payload - Get maximum backchannel payload size, in bytes
1608 * @clnt: RPC client to query
1610 size_t rpc_max_bc_payload(struct rpc_clnt *clnt)
1612 struct rpc_xprt *xprt;
1616 xprt = rcu_dereference(clnt->cl_xprt);
1617 ret = xprt->ops->bc_maxpayload(xprt);
1621 EXPORT_SYMBOL_GPL(rpc_max_bc_payload);
1623 unsigned int rpc_num_bc_slots(struct rpc_clnt *clnt)
1625 struct rpc_xprt *xprt;
1629 xprt = rcu_dereference(clnt->cl_xprt);
1630 ret = xprt->ops->bc_num_slots(xprt);
1634 EXPORT_SYMBOL_GPL(rpc_num_bc_slots);
1637 * rpc_force_rebind - force transport to check that remote port is unchanged
1638 * @clnt: client to rebind
1641 void rpc_force_rebind(struct rpc_clnt *clnt)
1643 if (clnt->cl_autobind) {
1645 xprt_clear_bound(rcu_dereference(clnt->cl_xprt));
1649 EXPORT_SYMBOL_GPL(rpc_force_rebind);
1652 __rpc_restart_call(struct rpc_task *task, void (*action)(struct rpc_task *))
1654 task->tk_status = 0;
1655 task->tk_rpc_status = 0;
1656 task->tk_action = action;
1661 * Restart an (async) RPC call. Usually called from within the
1665 rpc_restart_call(struct rpc_task *task)
1667 return __rpc_restart_call(task, call_start);
1669 EXPORT_SYMBOL_GPL(rpc_restart_call);
1672 * Restart an (async) RPC call from the call_prepare state.
1673 * Usually called from within the exit handler.
1676 rpc_restart_call_prepare(struct rpc_task *task)
1678 if (task->tk_ops->rpc_call_prepare != NULL)
1679 return __rpc_restart_call(task, rpc_prepare_task);
1680 return rpc_restart_call(task);
1682 EXPORT_SYMBOL_GPL(rpc_restart_call_prepare);
1685 *rpc_proc_name(const struct rpc_task *task)
1687 const struct rpc_procinfo *proc = task->tk_msg.rpc_proc;
1691 return proc->p_name;
1699 __rpc_call_rpcerror(struct rpc_task *task, int tk_status, int rpc_status)
1701 trace_rpc_call_rpcerror(task, tk_status, rpc_status);
1702 rpc_task_set_rpc_status(task, rpc_status);
1703 rpc_exit(task, tk_status);
1707 rpc_call_rpcerror(struct rpc_task *task, int status)
1709 __rpc_call_rpcerror(task, status, status);
1715 * Other FSM states can be visited zero or more times, but
1716 * this state is visited exactly once for each RPC.
1719 call_start(struct rpc_task *task)
1721 struct rpc_clnt *clnt = task->tk_client;
1722 int idx = task->tk_msg.rpc_proc->p_statidx;
1724 trace_rpc_request(task);
1726 /* Increment call count (version might not be valid for ping) */
1727 if (clnt->cl_program->version[clnt->cl_vers])
1728 clnt->cl_program->version[clnt->cl_vers]->counts[idx]++;
1729 clnt->cl_stats->rpccnt++;
1730 task->tk_action = call_reserve;
1731 rpc_task_set_transport(task, clnt);
1735 * 1. Reserve an RPC call slot
1738 call_reserve(struct rpc_task *task)
1740 task->tk_status = 0;
1741 task->tk_action = call_reserveresult;
1745 static void call_retry_reserve(struct rpc_task *task);
1748 * 1b. Grok the result of xprt_reserve()
1751 call_reserveresult(struct rpc_task *task)
1753 int status = task->tk_status;
1756 * After a call to xprt_reserve(), we must have either
1757 * a request slot or else an error status.
1759 task->tk_status = 0;
1761 if (task->tk_rqstp) {
1762 task->tk_action = call_refresh;
1766 rpc_call_rpcerror(task, -EIO);
1772 rpc_delay(task, HZ >> 2);
1774 case -EAGAIN: /* woken up; retry */
1775 task->tk_action = call_retry_reserve;
1778 rpc_call_rpcerror(task, status);
1783 * 1c. Retry reserving an RPC call slot
1786 call_retry_reserve(struct rpc_task *task)
1788 task->tk_status = 0;
1789 task->tk_action = call_reserveresult;
1790 xprt_retry_reserve(task);
1794 * 2. Bind and/or refresh the credentials
1797 call_refresh(struct rpc_task *task)
1799 task->tk_action = call_refreshresult;
1800 task->tk_status = 0;
1801 task->tk_client->cl_stats->rpcauthrefresh++;
1802 rpcauth_refreshcred(task);
1806 * 2a. Process the results of a credential refresh
1809 call_refreshresult(struct rpc_task *task)
1811 int status = task->tk_status;
1813 task->tk_status = 0;
1814 task->tk_action = call_refresh;
1817 if (rpcauth_uptodatecred(task)) {
1818 task->tk_action = call_allocate;
1821 /* Use rate-limiting and a max number of retries if refresh
1822 * had status 0 but failed to update the cred.
1826 rpc_delay(task, 3*HZ);
1832 if (!task->tk_cred_retry)
1834 task->tk_cred_retry--;
1835 trace_rpc_retry_refresh_status(task);
1838 rpc_delay(task, HZ >> 4);
1841 trace_rpc_refresh_status(task);
1842 rpc_call_rpcerror(task, status);
1846 * 2b. Allocate the buffer. For details, see sched.c:rpc_malloc.
1847 * (Note: buffer memory is freed in xprt_release).
1850 call_allocate(struct rpc_task *task)
1852 const struct rpc_auth *auth = task->tk_rqstp->rq_cred->cr_auth;
1853 struct rpc_rqst *req = task->tk_rqstp;
1854 struct rpc_xprt *xprt = req->rq_xprt;
1855 const struct rpc_procinfo *proc = task->tk_msg.rpc_proc;
1858 task->tk_status = 0;
1859 task->tk_action = call_encode;
1864 if (proc->p_proc != 0) {
1865 BUG_ON(proc->p_arglen == 0);
1866 if (proc->p_decode != NULL)
1867 BUG_ON(proc->p_replen == 0);
1871 * Calculate the size (in quads) of the RPC call
1872 * and reply headers, and convert both values
1875 req->rq_callsize = RPC_CALLHDRSIZE + (auth->au_cslack << 1) +
1877 req->rq_callsize <<= 2;
1879 * Note: the reply buffer must at minimum allocate enough space
1880 * for the 'struct accepted_reply' from RFC5531.
1882 req->rq_rcvsize = RPC_REPHDRSIZE + auth->au_rslack + \
1883 max_t(size_t, proc->p_replen, 2);
1884 req->rq_rcvsize <<= 2;
1886 status = xprt->ops->buf_alloc(task);
1887 trace_rpc_buf_alloc(task, status);
1890 if (status != -ENOMEM) {
1891 rpc_call_rpcerror(task, status);
1895 if (RPC_IS_ASYNC(task) || !fatal_signal_pending(current)) {
1896 task->tk_action = call_allocate;
1897 rpc_delay(task, HZ>>4);
1901 rpc_call_rpcerror(task, -ERESTARTSYS);
1905 rpc_task_need_encode(struct rpc_task *task)
1907 return test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate) == 0 &&
1908 (!(task->tk_flags & RPC_TASK_SENT) ||
1909 !(task->tk_flags & RPC_TASK_NO_RETRANS_TIMEOUT) ||
1910 xprt_request_need_retransmit(task));
1914 rpc_xdr_encode(struct rpc_task *task)
1916 struct rpc_rqst *req = task->tk_rqstp;
1917 struct xdr_stream xdr;
1919 xdr_buf_init(&req->rq_snd_buf,
1922 xdr_buf_init(&req->rq_rcv_buf,
1926 req->rq_reply_bytes_recvd = 0;
1927 req->rq_snd_buf.head[0].iov_len = 0;
1928 xdr_init_encode(&xdr, &req->rq_snd_buf,
1929 req->rq_snd_buf.head[0].iov_base, req);
1930 if (rpc_encode_header(task, &xdr))
1933 task->tk_status = rpcauth_wrap_req(task, &xdr);
1937 * 3. Encode arguments of an RPC call
1940 call_encode(struct rpc_task *task)
1942 if (!rpc_task_need_encode(task))
1945 /* Dequeue task from the receive queue while we're encoding */
1946 xprt_request_dequeue_xprt(task);
1947 /* Encode here so that rpcsec_gss can use correct sequence number. */
1948 rpc_xdr_encode(task);
1949 /* Add task to reply queue before transmission to avoid races */
1950 if (task->tk_status == 0 && rpc_reply_expected(task))
1951 task->tk_status = xprt_request_enqueue_receive(task);
1952 /* Did the encode result in an error condition? */
1953 if (task->tk_status != 0) {
1954 /* Was the error nonfatal? */
1955 switch (task->tk_status) {
1958 rpc_delay(task, HZ >> 4);
1961 if (!task->tk_cred_retry) {
1962 rpc_call_rpcerror(task, task->tk_status);
1964 task->tk_action = call_refresh;
1965 task->tk_cred_retry--;
1966 trace_rpc_retry_refresh_status(task);
1970 rpc_call_rpcerror(task, task->tk_status);
1975 xprt_request_enqueue_transmit(task);
1977 task->tk_action = call_transmit;
1978 /* Check that the connection is OK */
1979 if (!xprt_bound(task->tk_xprt))
1980 task->tk_action = call_bind;
1981 else if (!xprt_connected(task->tk_xprt))
1982 task->tk_action = call_connect;
1986 * Helpers to check if the task was already transmitted, and
1987 * to take action when that is the case.
1990 rpc_task_transmitted(struct rpc_task *task)
1992 return !test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate);
1996 rpc_task_handle_transmitted(struct rpc_task *task)
1998 xprt_end_transmit(task);
1999 task->tk_action = call_transmit_status;
2003 * 4. Get the server port number if not yet set
2006 call_bind(struct rpc_task *task)
2008 struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
2010 if (rpc_task_transmitted(task)) {
2011 rpc_task_handle_transmitted(task);
2015 if (xprt_bound(xprt)) {
2016 task->tk_action = call_connect;
2020 task->tk_action = call_bind_status;
2021 if (!xprt_prepare_transmit(task))
2024 xprt->ops->rpcbind(task);
2028 * 4a. Sort out bind result
2031 call_bind_status(struct rpc_task *task)
2033 struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
2036 if (rpc_task_transmitted(task)) {
2037 rpc_task_handle_transmitted(task);
2041 if (task->tk_status >= 0)
2043 if (xprt_bound(xprt)) {
2044 task->tk_status = 0;
2048 switch (task->tk_status) {
2050 rpc_delay(task, HZ >> 2);
2053 trace_rpcb_prog_unavail_err(task);
2054 /* fail immediately if this is an RPC ping */
2055 if (task->tk_msg.rpc_proc->p_proc == 0) {
2056 status = -EOPNOTSUPP;
2059 rpc_delay(task, 3*HZ);
2062 rpc_delay(task, HZ >> 2);
2067 trace_rpcb_timeout_err(task);
2070 /* server doesn't support any rpcbind version we know of */
2071 trace_rpcb_bind_version_err(task);
2073 case -EPROTONOSUPPORT:
2074 trace_rpcb_bind_version_err(task);
2076 case -ECONNREFUSED: /* connection problems */
2085 trace_rpcb_unreachable_err(task);
2086 if (!RPC_IS_SOFTCONN(task)) {
2087 rpc_delay(task, 5*HZ);
2090 status = task->tk_status;
2093 trace_rpcb_unrecognized_err(task);
2096 rpc_call_rpcerror(task, status);
2099 task->tk_action = call_connect;
2102 task->tk_status = 0;
2103 task->tk_action = call_bind;
2104 rpc_check_timeout(task);
2108 * 4b. Connect to the RPC server
2111 call_connect(struct rpc_task *task)
2113 struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
2115 if (rpc_task_transmitted(task)) {
2116 rpc_task_handle_transmitted(task);
2120 if (xprt_connected(xprt)) {
2121 task->tk_action = call_transmit;
2125 task->tk_action = call_connect_status;
2126 if (task->tk_status < 0)
2128 if (task->tk_flags & RPC_TASK_NOCONNECT) {
2129 rpc_call_rpcerror(task, -ENOTCONN);
2132 if (!xprt_prepare_transmit(task))
2138 * 4c. Sort out connect result
2141 call_connect_status(struct rpc_task *task)
2143 struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
2144 struct rpc_clnt *clnt = task->tk_client;
2145 int status = task->tk_status;
2147 if (rpc_task_transmitted(task)) {
2148 rpc_task_handle_transmitted(task);
2152 trace_rpc_connect_status(task);
2154 if (task->tk_status == 0) {
2155 clnt->cl_stats->netreconn++;
2158 if (xprt_connected(xprt)) {
2159 task->tk_status = 0;
2163 task->tk_status = 0;
2167 /* A positive refusal suggests a rebind is needed. */
2168 if (RPC_IS_SOFTCONN(task))
2170 if (clnt->cl_autobind) {
2171 rpc_force_rebind(clnt);
2181 xprt_conditional_disconnect(task->tk_rqstp->rq_xprt,
2182 task->tk_rqstp->rq_connect_cookie);
2183 if (RPC_IS_SOFTCONN(task))
2185 /* retry with existing socket, after a delay */
2186 rpc_delay(task, 3*HZ);
2192 if (!(task->tk_flags & RPC_TASK_NO_ROUND_ROBIN) &&
2193 (task->tk_flags & RPC_TASK_MOVEABLE) &&
2194 test_bit(XPRT_REMOVE, &xprt->state)) {
2195 struct rpc_xprt *saved = task->tk_xprt;
2196 struct rpc_xprt_switch *xps;
2199 xps = xprt_switch_get(rcu_dereference(clnt->cl_xpi.xpi_xpswitch));
2201 if (xps->xps_nxprts > 1) {
2205 value = atomic_long_dec_return(&xprt->queuelen);
2207 rpc_xprt_switch_remove_xprt(xps, saved,
2210 task->tk_xprt = NULL;
2211 task->tk_action = call_start;
2213 xprt_switch_put(xps);
2219 rpc_delay(task, HZ >> 2);
2222 rpc_call_rpcerror(task, status);
2225 task->tk_action = call_transmit;
2228 /* Check for timeouts before looping back to call_bind */
2229 task->tk_action = call_bind;
2230 rpc_check_timeout(task);
2234 * 5. Transmit the RPC request, and wait for reply
2237 call_transmit(struct rpc_task *task)
2239 if (rpc_task_transmitted(task)) {
2240 rpc_task_handle_transmitted(task);
2244 task->tk_action = call_transmit_status;
2245 if (!xprt_prepare_transmit(task))
2247 task->tk_status = 0;
2248 if (test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate)) {
2249 if (!xprt_connected(task->tk_xprt)) {
2250 task->tk_status = -ENOTCONN;
2253 xprt_transmit(task);
2255 xprt_end_transmit(task);
2259 * 5a. Handle cleanup after a transmission
2262 call_transmit_status(struct rpc_task *task)
2264 task->tk_action = call_status;
2267 * Common case: success. Force the compiler to put this
2270 if (rpc_task_transmitted(task)) {
2271 task->tk_status = 0;
2272 xprt_request_wait_receive(task);
2276 switch (task->tk_status) {
2280 task->tk_status = 0;
2281 task->tk_action = call_encode;
2284 * Special cases: if we've been waiting on the
2285 * socket's write_space() callback, or if the
2286 * socket just returned a connection error,
2287 * then hold onto the transport lock.
2291 rpc_delay(task, HZ>>2);
2295 task->tk_action = call_transmit;
2296 task->tk_status = 0;
2304 if (RPC_IS_SOFTCONN(task)) {
2305 if (!task->tk_msg.rpc_proc->p_proc)
2306 trace_xprt_ping(task->tk_xprt,
2308 rpc_call_rpcerror(task, task->tk_status);
2317 task->tk_action = call_bind;
2318 task->tk_status = 0;
2321 rpc_check_timeout(task);
2324 #if defined(CONFIG_SUNRPC_BACKCHANNEL)
2325 static void call_bc_transmit(struct rpc_task *task);
2326 static void call_bc_transmit_status(struct rpc_task *task);
2329 call_bc_encode(struct rpc_task *task)
2331 xprt_request_enqueue_transmit(task);
2332 task->tk_action = call_bc_transmit;
2336 * 5b. Send the backchannel RPC reply. On error, drop the reply. In
2337 * addition, disconnect on connectivity errors.
2340 call_bc_transmit(struct rpc_task *task)
2342 task->tk_action = call_bc_transmit_status;
2343 if (test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate)) {
2344 if (!xprt_prepare_transmit(task))
2346 task->tk_status = 0;
2347 xprt_transmit(task);
2349 xprt_end_transmit(task);
2353 call_bc_transmit_status(struct rpc_task *task)
2355 struct rpc_rqst *req = task->tk_rqstp;
2357 if (rpc_task_transmitted(task))
2358 task->tk_status = 0;
2360 switch (task->tk_status) {
2375 rpc_delay(task, HZ>>2);
2379 task->tk_status = 0;
2380 task->tk_action = call_bc_transmit;
2384 * Problem reaching the server. Disconnect and let the
2385 * forechannel reestablish the connection. The server will
2386 * have to retransmit the backchannel request and we'll
2387 * reprocess it. Since these ops are idempotent, there's no
2388 * need to cache our reply at this time.
2390 printk(KERN_NOTICE "RPC: Could not send backchannel reply "
2391 "error: %d\n", task->tk_status);
2392 xprt_conditional_disconnect(req->rq_xprt,
2393 req->rq_connect_cookie);
2397 * We were unable to reply and will have to drop the
2398 * request. The server should reconnect and retransmit.
2400 printk(KERN_NOTICE "RPC: Could not send backchannel reply "
2401 "error: %d\n", task->tk_status);
2404 task->tk_action = rpc_exit_task;
2406 #endif /* CONFIG_SUNRPC_BACKCHANNEL */
2409 * 6. Sort out the RPC call status
2412 call_status(struct rpc_task *task)
2414 struct rpc_clnt *clnt = task->tk_client;
2417 if (!task->tk_msg.rpc_proc->p_proc)
2418 trace_xprt_ping(task->tk_xprt, task->tk_status);
2420 status = task->tk_status;
2422 task->tk_action = call_decode;
2426 trace_rpc_call_status(task);
2427 task->tk_status = 0;
2434 if (RPC_IS_SOFTCONN(task))
2437 * Delay any retries for 3 seconds, then handle as if it
2440 rpc_delay(task, 3*HZ);
2448 rpc_force_rebind(clnt);
2451 rpc_delay(task, 3*HZ);
2459 rpc_delay(task, HZ>>2);
2462 /* shutdown or soft timeout */
2465 if (clnt->cl_chatty)
2466 printk("%s: RPC call returned error %d\n",
2467 clnt->cl_program->name, -status);
2470 task->tk_action = call_encode;
2471 rpc_check_timeout(task);
2474 rpc_call_rpcerror(task, status);
2478 rpc_check_connected(const struct rpc_rqst *req)
2480 /* No allocated request or transport? return true */
2481 if (!req || !req->rq_xprt)
2483 return xprt_connected(req->rq_xprt);
2487 rpc_check_timeout(struct rpc_task *task)
2489 struct rpc_clnt *clnt = task->tk_client;
2491 if (RPC_SIGNALLED(task))
2494 if (xprt_adjust_timeout(task->tk_rqstp) == 0)
2497 trace_rpc_timeout_status(task);
2498 task->tk_timeouts++;
2500 if (RPC_IS_SOFTCONN(task) && !rpc_check_connected(task->tk_rqstp)) {
2501 rpc_call_rpcerror(task, -ETIMEDOUT);
2505 if (RPC_IS_SOFT(task)) {
2507 * Once a "no retrans timeout" soft tasks (a.k.a NFSv4) has
2508 * been sent, it should time out only if the transport
2509 * connection gets terminally broken.
2511 if ((task->tk_flags & RPC_TASK_NO_RETRANS_TIMEOUT) &&
2512 rpc_check_connected(task->tk_rqstp))
2515 if (clnt->cl_chatty) {
2516 pr_notice_ratelimited(
2517 "%s: server %s not responding, timed out\n",
2518 clnt->cl_program->name,
2519 task->tk_xprt->servername);
2521 if (task->tk_flags & RPC_TASK_TIMEOUT)
2522 rpc_call_rpcerror(task, -ETIMEDOUT);
2524 __rpc_call_rpcerror(task, -EIO, -ETIMEDOUT);
2528 if (!(task->tk_flags & RPC_CALL_MAJORSEEN)) {
2529 task->tk_flags |= RPC_CALL_MAJORSEEN;
2530 if (clnt->cl_chatty) {
2531 pr_notice_ratelimited(
2532 "%s: server %s not responding, still trying\n",
2533 clnt->cl_program->name,
2534 task->tk_xprt->servername);
2537 rpc_force_rebind(clnt);
2539 * Did our request time out due to an RPCSEC_GSS out-of-sequence
2540 * event? RFC2203 requires the server to drop all such requests.
2542 rpcauth_invalcred(task);
2546 * 7. Decode the RPC reply
2549 call_decode(struct rpc_task *task)
2551 struct rpc_clnt *clnt = task->tk_client;
2552 struct rpc_rqst *req = task->tk_rqstp;
2553 struct xdr_stream xdr;
2556 if (!task->tk_msg.rpc_proc->p_decode) {
2557 task->tk_action = rpc_exit_task;
2561 if (task->tk_flags & RPC_CALL_MAJORSEEN) {
2562 if (clnt->cl_chatty) {
2563 pr_notice_ratelimited("%s: server %s OK\n",
2564 clnt->cl_program->name,
2565 task->tk_xprt->servername);
2567 task->tk_flags &= ~RPC_CALL_MAJORSEEN;
2571 * Did we ever call xprt_complete_rqst()? If not, we should assume
2572 * the message is incomplete.
2575 if (!req->rq_reply_bytes_recvd)
2578 /* Ensure that we see all writes made by xprt_complete_rqst()
2579 * before it changed req->rq_reply_bytes_recvd.
2583 req->rq_rcv_buf.len = req->rq_private_buf.len;
2584 trace_rpc_xdr_recvfrom(task, &req->rq_rcv_buf);
2586 /* Check that the softirq receive buffer is valid */
2587 WARN_ON(memcmp(&req->rq_rcv_buf, &req->rq_private_buf,
2588 sizeof(req->rq_rcv_buf)) != 0);
2590 xdr_init_decode(&xdr, &req->rq_rcv_buf,
2591 req->rq_rcv_buf.head[0].iov_base, req);
2592 err = rpc_decode_header(task, &xdr);
2596 task->tk_action = rpc_exit_task;
2597 task->tk_status = rpcauth_unwrap_resp(task, &xdr);
2600 task->tk_status = 0;
2601 if (task->tk_client->cl_discrtry)
2602 xprt_conditional_disconnect(req->rq_xprt,
2603 req->rq_connect_cookie);
2604 task->tk_action = call_encode;
2605 rpc_check_timeout(task);
2608 task->tk_action = call_reserve;
2609 rpc_check_timeout(task);
2610 rpcauth_invalcred(task);
2611 /* Ensure we obtain a new XID if we retry! */
2617 rpc_encode_header(struct rpc_task *task, struct xdr_stream *xdr)
2619 struct rpc_clnt *clnt = task->tk_client;
2620 struct rpc_rqst *req = task->tk_rqstp;
2625 p = xdr_reserve_space(xdr, RPC_CALLHDRSIZE << 2);
2630 *p++ = cpu_to_be32(RPC_VERSION);
2631 *p++ = cpu_to_be32(clnt->cl_prog);
2632 *p++ = cpu_to_be32(clnt->cl_vers);
2633 *p = cpu_to_be32(task->tk_msg.rpc_proc->p_proc);
2635 error = rpcauth_marshcred(task, xdr);
2640 trace_rpc_bad_callhdr(task);
2641 rpc_call_rpcerror(task, error);
2646 rpc_decode_header(struct rpc_task *task, struct xdr_stream *xdr)
2648 struct rpc_clnt *clnt = task->tk_client;
2652 /* RFC-1014 says that the representation of XDR data must be a
2653 * multiple of four bytes
2654 * - if it isn't pointer subtraction in the NFS client may give
2657 if (task->tk_rqstp->rq_rcv_buf.len & 3)
2658 goto out_unparsable;
2660 p = xdr_inline_decode(xdr, 3 * sizeof(*p));
2662 goto out_unparsable;
2664 if (*p++ != rpc_reply)
2665 goto out_unparsable;
2666 if (*p++ != rpc_msg_accepted)
2667 goto out_msg_denied;
2669 error = rpcauth_checkverf(task, xdr);
2673 p = xdr_inline_decode(xdr, sizeof(*p));
2675 goto out_unparsable;
2679 case rpc_prog_unavail:
2680 trace_rpc__prog_unavail(task);
2681 error = -EPFNOSUPPORT;
2683 case rpc_prog_mismatch:
2684 trace_rpc__prog_mismatch(task);
2685 error = -EPROTONOSUPPORT;
2687 case rpc_proc_unavail:
2688 trace_rpc__proc_unavail(task);
2689 error = -EOPNOTSUPP;
2691 case rpc_garbage_args:
2692 case rpc_system_err:
2693 trace_rpc__garbage_args(task);
2697 goto out_unparsable;
2701 clnt->cl_stats->rpcgarbage++;
2702 if (task->tk_garb_retry) {
2703 task->tk_garb_retry--;
2704 task->tk_action = call_encode;
2708 rpc_call_rpcerror(task, error);
2712 trace_rpc__unparsable(task);
2717 trace_rpc_bad_verifier(task);
2722 p = xdr_inline_decode(xdr, sizeof(*p));
2724 goto out_unparsable;
2726 case rpc_auth_error:
2729 trace_rpc__mismatch(task);
2730 error = -EPROTONOSUPPORT;
2733 goto out_unparsable;
2736 p = xdr_inline_decode(xdr, sizeof(*p));
2738 goto out_unparsable;
2740 case rpc_autherr_rejectedcred:
2741 case rpc_autherr_rejectedverf:
2742 case rpcsec_gsserr_credproblem:
2743 case rpcsec_gsserr_ctxproblem:
2744 rpcauth_invalcred(task);
2745 if (!task->tk_cred_retry)
2747 task->tk_cred_retry--;
2748 trace_rpc__stale_creds(task);
2749 return -EKEYREJECTED;
2750 case rpc_autherr_badcred:
2751 case rpc_autherr_badverf:
2752 /* possibly garbled cred/verf? */
2753 if (!task->tk_garb_retry)
2755 task->tk_garb_retry--;
2756 trace_rpc__bad_creds(task);
2757 task->tk_action = call_encode;
2759 case rpc_autherr_tooweak:
2760 trace_rpc__auth_tooweak(task);
2761 pr_warn("RPC: server %s requires stronger authentication.\n",
2762 task->tk_xprt->servername);
2765 goto out_unparsable;
2770 static void rpcproc_encode_null(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
2775 static int rpcproc_decode_null(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
2781 static const struct rpc_procinfo rpcproc_null = {
2782 .p_encode = rpcproc_encode_null,
2783 .p_decode = rpcproc_decode_null,
2786 static const struct rpc_procinfo rpcproc_null_noreply = {
2787 .p_encode = rpcproc_encode_null,
2791 rpc_null_call_prepare(struct rpc_task *task, void *data)
2793 task->tk_flags &= ~RPC_TASK_NO_RETRANS_TIMEOUT;
2794 rpc_call_start(task);
2797 static const struct rpc_call_ops rpc_null_ops = {
2798 .rpc_call_prepare = rpc_null_call_prepare,
2799 .rpc_call_done = rpc_default_callback,
2803 struct rpc_task *rpc_call_null_helper(struct rpc_clnt *clnt,
2804 struct rpc_xprt *xprt, struct rpc_cred *cred, int flags,
2805 const struct rpc_call_ops *ops, void *data)
2807 struct rpc_message msg = {
2808 .rpc_proc = &rpcproc_null,
2810 struct rpc_task_setup task_setup_data = {
2813 .rpc_message = &msg,
2814 .rpc_op_cred = cred,
2815 .callback_ops = ops ?: &rpc_null_ops,
2816 .callback_data = data,
2817 .flags = flags | RPC_TASK_SOFT | RPC_TASK_SOFTCONN |
2821 return rpc_run_task(&task_setup_data);
2824 struct rpc_task *rpc_call_null(struct rpc_clnt *clnt, struct rpc_cred *cred, int flags)
2826 return rpc_call_null_helper(clnt, NULL, cred, flags, NULL, NULL);
2828 EXPORT_SYMBOL_GPL(rpc_call_null);
2830 static int rpc_ping(struct rpc_clnt *clnt)
2832 struct rpc_task *task;
2835 task = rpc_call_null_helper(clnt, NULL, NULL, 0, NULL, NULL);
2837 return PTR_ERR(task);
2838 status = task->tk_status;
2843 static int rpc_ping_noreply(struct rpc_clnt *clnt)
2845 struct rpc_message msg = {
2846 .rpc_proc = &rpcproc_null_noreply,
2848 struct rpc_task_setup task_setup_data = {
2850 .rpc_message = &msg,
2851 .callback_ops = &rpc_null_ops,
2852 .flags = RPC_TASK_SOFT | RPC_TASK_SOFTCONN | RPC_TASK_NULLCREDS,
2854 struct rpc_task *task;
2857 task = rpc_run_task(&task_setup_data);
2859 return PTR_ERR(task);
2860 status = task->tk_status;
2865 struct rpc_cb_add_xprt_calldata {
2866 struct rpc_xprt_switch *xps;
2867 struct rpc_xprt *xprt;
2870 static void rpc_cb_add_xprt_done(struct rpc_task *task, void *calldata)
2872 struct rpc_cb_add_xprt_calldata *data = calldata;
2874 if (task->tk_status == 0)
2875 rpc_xprt_switch_add_xprt(data->xps, data->xprt);
2878 static void rpc_cb_add_xprt_release(void *calldata)
2880 struct rpc_cb_add_xprt_calldata *data = calldata;
2882 xprt_put(data->xprt);
2883 xprt_switch_put(data->xps);
2887 static const struct rpc_call_ops rpc_cb_add_xprt_call_ops = {
2888 .rpc_call_prepare = rpc_null_call_prepare,
2889 .rpc_call_done = rpc_cb_add_xprt_done,
2890 .rpc_release = rpc_cb_add_xprt_release,
2894 * rpc_clnt_test_and_add_xprt - Test and add a new transport to a rpc_clnt
2895 * @clnt: pointer to struct rpc_clnt
2896 * @xps: pointer to struct rpc_xprt_switch,
2897 * @xprt: pointer struct rpc_xprt
2898 * @in_max_connect: pointer to the max_connect value for the passed in xprt transport
2900 int rpc_clnt_test_and_add_xprt(struct rpc_clnt *clnt,
2901 struct rpc_xprt_switch *xps, struct rpc_xprt *xprt,
2902 void *in_max_connect)
2904 struct rpc_cb_add_xprt_calldata *data;
2905 struct rpc_task *task;
2906 int max_connect = clnt->cl_max_connect;
2909 max_connect = *(int *)in_max_connect;
2910 if (xps->xps_nunique_destaddr_xprts + 1 > max_connect) {
2912 pr_warn("SUNRPC: reached max allowed number (%d) did not add "
2913 "transport to server: %s\n", max_connect,
2914 rpc_peeraddr2str(clnt, RPC_DISPLAY_ADDR));
2919 data = kmalloc(sizeof(*data), GFP_KERNEL);
2922 data->xps = xprt_switch_get(xps);
2923 data->xprt = xprt_get(xprt);
2924 if (rpc_xprt_switch_has_addr(data->xps, (struct sockaddr *)&xprt->addr)) {
2925 rpc_cb_add_xprt_release(data);
2929 task = rpc_call_null_helper(clnt, xprt, NULL, RPC_TASK_ASYNC,
2930 &rpc_cb_add_xprt_call_ops, data);
2932 return PTR_ERR(task);
2934 data->xps->xps_nunique_destaddr_xprts++;
2939 EXPORT_SYMBOL_GPL(rpc_clnt_test_and_add_xprt);
2941 static int rpc_clnt_add_xprt_helper(struct rpc_clnt *clnt,
2942 struct rpc_xprt *xprt,
2943 struct rpc_add_xprt_test *data)
2945 struct rpc_task *task;
2946 int status = -EADDRINUSE;
2948 /* Test the connection */
2949 task = rpc_call_null_helper(clnt, xprt, NULL, 0, NULL, NULL);
2951 return PTR_ERR(task);
2953 status = task->tk_status;
2959 /* rpc_xprt_switch and rpc_xprt are deferrenced by add_xprt_test() */
2960 data->add_xprt_test(clnt, xprt, data->data);
2966 * rpc_clnt_setup_test_and_add_xprt()
2968 * This is an rpc_clnt_add_xprt setup() function which returns 1 so:
2969 * 1) caller of the test function must dereference the rpc_xprt_switch
2971 * 2) test function must call rpc_xprt_switch_add_xprt, usually in
2972 * the rpc_call_done routine.
2974 * Upon success (return of 1), the test function adds the new
2975 * transport to the rpc_clnt xprt switch
2977 * @clnt: struct rpc_clnt to get the new transport
2978 * @xps: the rpc_xprt_switch to hold the new transport
2979 * @xprt: the rpc_xprt to test
2980 * @data: a struct rpc_add_xprt_test pointer that holds the test function
2981 * and test function call data
2983 int rpc_clnt_setup_test_and_add_xprt(struct rpc_clnt *clnt,
2984 struct rpc_xprt_switch *xps,
2985 struct rpc_xprt *xprt,
2988 int status = -EADDRINUSE;
2990 xprt = xprt_get(xprt);
2991 xprt_switch_get(xps);
2993 if (rpc_xprt_switch_has_addr(xps, (struct sockaddr *)&xprt->addr))
2996 status = rpc_clnt_add_xprt_helper(clnt, xprt, data);
3003 xprt_switch_put(xps);
3005 pr_info("RPC: rpc_clnt_test_xprt failed: %d addr %s not "
3007 xprt->address_strings[RPC_DISPLAY_ADDR]);
3008 /* so that rpc_clnt_add_xprt does not call rpc_xprt_switch_add_xprt */
3011 EXPORT_SYMBOL_GPL(rpc_clnt_setup_test_and_add_xprt);
3014 * rpc_clnt_add_xprt - Add a new transport to a rpc_clnt
3015 * @clnt: pointer to struct rpc_clnt
3016 * @xprtargs: pointer to struct xprt_create
3017 * @setup: callback to test and/or set up the connection
3018 * @data: pointer to setup function data
3020 * Creates a new transport using the parameters set in args and
3022 * If ping is set, then test that connectivity succeeds before
3023 * adding the new transport.
3026 int rpc_clnt_add_xprt(struct rpc_clnt *clnt,
3027 struct xprt_create *xprtargs,
3028 int (*setup)(struct rpc_clnt *,
3029 struct rpc_xprt_switch *,
3034 struct rpc_xprt_switch *xps;
3035 struct rpc_xprt *xprt;
3036 unsigned long connect_timeout;
3037 unsigned long reconnect_timeout;
3038 unsigned char resvport, reuseport;
3042 xps = xprt_switch_get(rcu_dereference(clnt->cl_xpi.xpi_xpswitch));
3043 xprt = xprt_iter_xprt(&clnt->cl_xpi);
3044 if (xps == NULL || xprt == NULL) {
3046 xprt_switch_put(xps);
3049 resvport = xprt->resvport;
3050 reuseport = xprt->reuseport;
3051 connect_timeout = xprt->connect_timeout;
3052 reconnect_timeout = xprt->max_reconnect_timeout;
3053 ident = xprt->xprt_class->ident;
3056 if (!xprtargs->ident)
3057 xprtargs->ident = ident;
3058 xprt = xprt_create_transport(xprtargs);
3060 ret = PTR_ERR(xprt);
3061 goto out_put_switch;
3063 xprt->resvport = resvport;
3064 xprt->reuseport = reuseport;
3065 if (xprt->ops->set_connect_timeout != NULL)
3066 xprt->ops->set_connect_timeout(xprt,
3070 rpc_xprt_switch_set_roundrobin(xps);
3072 ret = setup(clnt, xps, xprt, data);
3076 rpc_xprt_switch_add_xprt(xps, xprt);
3080 xprt_switch_put(xps);
3083 EXPORT_SYMBOL_GPL(rpc_clnt_add_xprt);
3085 static int rpc_xprt_probe_trunked(struct rpc_clnt *clnt,
3086 struct rpc_xprt *xprt,
3087 struct rpc_add_xprt_test *data)
3089 struct rpc_xprt_switch *xps;
3090 struct rpc_xprt *main_xprt;
3096 main_xprt = xprt_get(rcu_dereference(clnt->cl_xprt));
3097 xps = xprt_switch_get(rcu_dereference(clnt->cl_xpi.xpi_xpswitch));
3098 status = rpc_cmp_addr_port((struct sockaddr *)&xprt->addr,
3099 (struct sockaddr *)&main_xprt->addr);
3101 xprt_put(main_xprt);
3102 if (status || !test_bit(XPRT_OFFLINE, &xprt->state))
3105 status = rpc_clnt_add_xprt_helper(clnt, xprt, data);
3108 xprt_switch_put(xps);
3112 /* rpc_clnt_probe_trunked_xprt -- probe offlined transport for session trunking
3113 * @clnt rpc_clnt structure
3115 * For each offlined transport found in the rpc_clnt structure call
3116 * the function rpc_xprt_probe_trunked() which will determine if this
3117 * transport still belongs to the trunking group.
3119 void rpc_clnt_probe_trunked_xprts(struct rpc_clnt *clnt,
3120 struct rpc_add_xprt_test *data)
3122 struct rpc_xprt_iter xpi;
3125 ret = rpc_clnt_xprt_iter_offline_init(clnt, &xpi);
3129 struct rpc_xprt *xprt = xprt_iter_get_next(&xpi);
3133 ret = rpc_xprt_probe_trunked(clnt, xprt, data);
3137 xprt_iter_rewind(&xpi);
3139 xprt_iter_destroy(&xpi);
3141 EXPORT_SYMBOL_GPL(rpc_clnt_probe_trunked_xprts);
3143 static int rpc_xprt_offline(struct rpc_clnt *clnt,
3144 struct rpc_xprt *xprt,
3147 struct rpc_xprt *main_xprt;
3148 struct rpc_xprt_switch *xps;
3154 main_xprt = xprt_get(rcu_dereference(clnt->cl_xprt));
3155 xps = xprt_switch_get(rcu_dereference(clnt->cl_xpi.xpi_xpswitch));
3156 err = rpc_cmp_addr_port((struct sockaddr *)&xprt->addr,
3157 (struct sockaddr *)&main_xprt->addr);
3159 xprt_put(main_xprt);
3163 if (wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_KILLABLE)) {
3167 xprt_set_offline_locked(xprt, xps);
3169 xprt_release_write(xprt, NULL);
3172 xprt_switch_put(xps);
3176 /* rpc_clnt_manage_trunked_xprts -- offline trunked transports
3177 * @clnt rpc_clnt structure
3179 * For each active transport found in the rpc_clnt structure call
3180 * the function rpc_xprt_offline() which will identify trunked transports
3181 * and will mark them offline.
3183 void rpc_clnt_manage_trunked_xprts(struct rpc_clnt *clnt)
3185 rpc_clnt_iterate_for_each_xprt(clnt, rpc_xprt_offline, NULL);
3187 EXPORT_SYMBOL_GPL(rpc_clnt_manage_trunked_xprts);
3189 struct connect_timeout_data {
3190 unsigned long connect_timeout;
3191 unsigned long reconnect_timeout;
3195 rpc_xprt_set_connect_timeout(struct rpc_clnt *clnt,
3196 struct rpc_xprt *xprt,
3199 struct connect_timeout_data *timeo = data;
3201 if (xprt->ops->set_connect_timeout)
3202 xprt->ops->set_connect_timeout(xprt,
3203 timeo->connect_timeout,
3204 timeo->reconnect_timeout);
3209 rpc_set_connect_timeout(struct rpc_clnt *clnt,
3210 unsigned long connect_timeout,
3211 unsigned long reconnect_timeout)
3213 struct connect_timeout_data timeout = {
3214 .connect_timeout = connect_timeout,
3215 .reconnect_timeout = reconnect_timeout,
3217 rpc_clnt_iterate_for_each_xprt(clnt,
3218 rpc_xprt_set_connect_timeout,
3221 EXPORT_SYMBOL_GPL(rpc_set_connect_timeout);
3223 void rpc_clnt_xprt_switch_put(struct rpc_clnt *clnt)
3226 xprt_switch_put(rcu_dereference(clnt->cl_xpi.xpi_xpswitch));
3229 EXPORT_SYMBOL_GPL(rpc_clnt_xprt_switch_put);
3231 void rpc_clnt_xprt_set_online(struct rpc_clnt *clnt, struct rpc_xprt *xprt)
3233 struct rpc_xprt_switch *xps;
3236 xps = rcu_dereference(clnt->cl_xpi.xpi_xpswitch);
3238 xprt_set_online_locked(xprt, xps);
3241 void rpc_clnt_xprt_switch_add_xprt(struct rpc_clnt *clnt, struct rpc_xprt *xprt)
3243 if (rpc_clnt_xprt_switch_has_addr(clnt,
3244 (const struct sockaddr *)&xprt->addr)) {
3245 return rpc_clnt_xprt_set_online(clnt, xprt);
3248 rpc_xprt_switch_add_xprt(rcu_dereference(clnt->cl_xpi.xpi_xpswitch),
3252 EXPORT_SYMBOL_GPL(rpc_clnt_xprt_switch_add_xprt);
3254 void rpc_clnt_xprt_switch_remove_xprt(struct rpc_clnt *clnt, struct rpc_xprt *xprt)
3256 struct rpc_xprt_switch *xps;
3259 xps = rcu_dereference(clnt->cl_xpi.xpi_xpswitch);
3260 rpc_xprt_switch_remove_xprt(rcu_dereference(clnt->cl_xpi.xpi_xpswitch),
3262 xps->xps_nunique_destaddr_xprts--;
3265 EXPORT_SYMBOL_GPL(rpc_clnt_xprt_switch_remove_xprt);
3267 bool rpc_clnt_xprt_switch_has_addr(struct rpc_clnt *clnt,
3268 const struct sockaddr *sap)
3270 struct rpc_xprt_switch *xps;
3274 xps = rcu_dereference(clnt->cl_xpi.xpi_xpswitch);
3275 ret = rpc_xprt_switch_has_addr(xps, sap);
3279 EXPORT_SYMBOL_GPL(rpc_clnt_xprt_switch_has_addr);
3281 #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
3282 static void rpc_show_header(void)
3284 printk(KERN_INFO "-pid- flgs status -client- --rqstp- "
3285 "-timeout ---ops--\n");
3288 static void rpc_show_task(const struct rpc_clnt *clnt,
3289 const struct rpc_task *task)
3291 const char *rpc_waitq = "none";
3293 if (RPC_IS_QUEUED(task))
3294 rpc_waitq = rpc_qname(task->tk_waitqueue);
3296 printk(KERN_INFO "%5u %04x %6d %8p %8p %8ld %8p %sv%u %s a:%ps q:%s\n",
3297 task->tk_pid, task->tk_flags, task->tk_status,
3298 clnt, task->tk_rqstp, rpc_task_timeout(task), task->tk_ops,
3299 clnt->cl_program->name, clnt->cl_vers, rpc_proc_name(task),
3300 task->tk_action, rpc_waitq);
3303 void rpc_show_tasks(struct net *net)
3305 struct rpc_clnt *clnt;
3306 struct rpc_task *task;
3308 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
3310 spin_lock(&sn->rpc_client_lock);
3311 list_for_each_entry(clnt, &sn->all_clients, cl_clients) {
3312 spin_lock(&clnt->cl_lock);
3313 list_for_each_entry(task, &clnt->cl_tasks, tk_task) {
3318 rpc_show_task(clnt, task);
3320 spin_unlock(&clnt->cl_lock);
3322 spin_unlock(&sn->rpc_client_lock);
3326 #if IS_ENABLED(CONFIG_SUNRPC_SWAP)
3328 rpc_clnt_swap_activate_callback(struct rpc_clnt *clnt,
3329 struct rpc_xprt *xprt,
3332 return xprt_enable_swap(xprt);
3336 rpc_clnt_swap_activate(struct rpc_clnt *clnt)
3338 while (clnt != clnt->cl_parent)
3339 clnt = clnt->cl_parent;
3340 if (atomic_inc_return(&clnt->cl_swapper) == 1)
3341 return rpc_clnt_iterate_for_each_xprt(clnt,
3342 rpc_clnt_swap_activate_callback, NULL);
3345 EXPORT_SYMBOL_GPL(rpc_clnt_swap_activate);
3348 rpc_clnt_swap_deactivate_callback(struct rpc_clnt *clnt,
3349 struct rpc_xprt *xprt,
3352 xprt_disable_swap(xprt);
3357 rpc_clnt_swap_deactivate(struct rpc_clnt *clnt)
3359 while (clnt != clnt->cl_parent)
3360 clnt = clnt->cl_parent;
3361 if (atomic_dec_if_positive(&clnt->cl_swapper) == 0)
3362 rpc_clnt_iterate_for_each_xprt(clnt,
3363 rpc_clnt_swap_deactivate_callback, NULL);
3365 EXPORT_SYMBOL_GPL(rpc_clnt_swap_deactivate);
3366 #endif /* CONFIG_SUNRPC_SWAP */