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 = 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,
681 return __rpc_clone_client(&args, clnt);
683 EXPORT_SYMBOL_GPL(rpc_clone_client);
686 * rpc_clone_client_set_auth - Clone an RPC client structure and set its auth
688 * @clnt: RPC client whose parameters are copied
689 * @flavor: security flavor for new client
691 * Returns a fresh RPC client or an ERR_PTR.
694 rpc_clone_client_set_auth(struct rpc_clnt *clnt, rpc_authflavor_t flavor)
696 struct rpc_create_args args = {
697 .program = clnt->cl_program,
698 .prognumber = clnt->cl_prog,
699 .version = clnt->cl_vers,
700 .authflavor = flavor,
701 .cred = clnt->cl_cred,
703 return __rpc_clone_client(&args, clnt);
705 EXPORT_SYMBOL_GPL(rpc_clone_client_set_auth);
708 * rpc_switch_client_transport: switch the RPC transport on the fly
709 * @clnt: pointer to a struct rpc_clnt
710 * @args: pointer to the new transport arguments
711 * @timeout: pointer to the new timeout parameters
713 * This function allows the caller to switch the RPC transport for the
714 * rpc_clnt structure 'clnt' to allow it to connect to a mirrored NFS
715 * server, for instance. It assumes that the caller has ensured that
716 * there are no active RPC tasks by using some form of locking.
718 * Returns zero if "clnt" is now using the new xprt. Otherwise a
719 * negative errno is returned, and "clnt" continues to use the old
722 int rpc_switch_client_transport(struct rpc_clnt *clnt,
723 struct xprt_create *args,
724 const struct rpc_timeout *timeout)
726 const struct rpc_timeout *old_timeo;
727 rpc_authflavor_t pseudoflavor;
728 struct rpc_xprt_switch *xps, *oldxps;
729 struct rpc_xprt *xprt, *old;
730 struct rpc_clnt *parent;
733 xprt = xprt_create_transport(args);
735 return PTR_ERR(xprt);
737 xps = xprt_switch_alloc(xprt, GFP_KERNEL);
743 pseudoflavor = clnt->cl_auth->au_flavor;
745 old_timeo = clnt->cl_timeout;
746 old = rpc_clnt_set_transport(clnt, xprt, timeout);
747 oldxps = xprt_iter_xchg_switch(&clnt->cl_xpi, xps);
749 rpc_unregister_client(clnt);
750 __rpc_clnt_remove_pipedir(clnt);
751 rpc_sysfs_client_destroy(clnt);
752 rpc_clnt_debugfs_unregister(clnt);
755 * A new transport was created. "clnt" therefore
756 * becomes the root of a new cl_parent tree. clnt's
757 * children, if it has any, still point to the old xprt.
759 parent = clnt->cl_parent;
760 clnt->cl_parent = clnt;
763 * The old rpc_auth cache cannot be re-used. GSS
764 * contexts in particular are between a single
767 err = rpc_client_register(clnt, pseudoflavor, NULL);
773 rpc_release_client(parent);
774 xprt_switch_put(oldxps);
776 trace_rpc_clnt_replace_xprt(clnt);
780 xps = xprt_iter_xchg_switch(&clnt->cl_xpi, oldxps);
781 rpc_clnt_set_transport(clnt, old, old_timeo);
782 clnt->cl_parent = parent;
783 rpc_client_register(clnt, pseudoflavor, NULL);
784 xprt_switch_put(xps);
786 trace_rpc_clnt_replace_xprt_err(clnt);
789 EXPORT_SYMBOL_GPL(rpc_switch_client_transport);
792 int _rpc_clnt_xprt_iter_init(struct rpc_clnt *clnt, struct rpc_xprt_iter *xpi,
793 void func(struct rpc_xprt_iter *xpi, struct rpc_xprt_switch *xps))
795 struct rpc_xprt_switch *xps;
798 xps = xprt_switch_get(rcu_dereference(clnt->cl_xpi.xpi_xpswitch));
803 xprt_switch_put(xps);
808 int rpc_clnt_xprt_iter_init(struct rpc_clnt *clnt, struct rpc_xprt_iter *xpi)
810 return _rpc_clnt_xprt_iter_init(clnt, xpi, xprt_iter_init_listall);
814 int rpc_clnt_xprt_iter_offline_init(struct rpc_clnt *clnt,
815 struct rpc_xprt_iter *xpi)
817 return _rpc_clnt_xprt_iter_init(clnt, xpi, xprt_iter_init_listoffline);
821 * rpc_clnt_iterate_for_each_xprt - Apply a function to all transports
822 * @clnt: pointer to client
823 * @fn: function to apply
824 * @data: void pointer to function data
826 * Iterates through the list of RPC transports currently attached to the
827 * client and applies the function fn(clnt, xprt, data).
829 * On error, the iteration stops, and the function returns the error value.
831 int rpc_clnt_iterate_for_each_xprt(struct rpc_clnt *clnt,
832 int (*fn)(struct rpc_clnt *, struct rpc_xprt *, void *),
835 struct rpc_xprt_iter xpi;
838 ret = rpc_clnt_xprt_iter_init(clnt, &xpi);
842 struct rpc_xprt *xprt = xprt_iter_get_next(&xpi);
846 ret = fn(clnt, xprt, data);
851 xprt_iter_destroy(&xpi);
854 EXPORT_SYMBOL_GPL(rpc_clnt_iterate_for_each_xprt);
857 * Kill all tasks for the given client.
858 * XXX: kill their descendants as well?
860 void rpc_killall_tasks(struct rpc_clnt *clnt)
862 struct rpc_task *rovr;
865 if (list_empty(&clnt->cl_tasks))
869 * Spin lock all_tasks to prevent changes...
871 trace_rpc_clnt_killall(clnt);
872 spin_lock(&clnt->cl_lock);
873 list_for_each_entry(rovr, &clnt->cl_tasks, tk_task)
874 rpc_signal_task(rovr);
875 spin_unlock(&clnt->cl_lock);
877 EXPORT_SYMBOL_GPL(rpc_killall_tasks);
880 * rpc_cancel_tasks - try to cancel a set of RPC tasks
881 * @clnt: Pointer to RPC client
882 * @error: RPC task error value to set
883 * @fnmatch: Pointer to selector function
886 * Uses @fnmatch to define a set of RPC tasks that are to be cancelled.
887 * The argument @error must be a negative error value.
889 unsigned long rpc_cancel_tasks(struct rpc_clnt *clnt, int error,
890 bool (*fnmatch)(const struct rpc_task *,
894 struct rpc_task *task;
895 unsigned long count = 0;
897 if (list_empty(&clnt->cl_tasks))
900 * Spin lock all_tasks to prevent changes...
902 spin_lock(&clnt->cl_lock);
903 list_for_each_entry(task, &clnt->cl_tasks, tk_task) {
904 if (!RPC_IS_ACTIVATED(task))
906 if (!fnmatch(task, data))
908 rpc_task_try_cancel(task, error);
911 spin_unlock(&clnt->cl_lock);
914 EXPORT_SYMBOL_GPL(rpc_cancel_tasks);
916 static int rpc_clnt_disconnect_xprt(struct rpc_clnt *clnt,
917 struct rpc_xprt *xprt, void *dummy)
919 if (xprt_connected(xprt))
920 xprt_force_disconnect(xprt);
924 void rpc_clnt_disconnect(struct rpc_clnt *clnt)
926 rpc_clnt_iterate_for_each_xprt(clnt, rpc_clnt_disconnect_xprt, NULL);
928 EXPORT_SYMBOL_GPL(rpc_clnt_disconnect);
931 * Properly shut down an RPC client, terminating all outstanding
934 void rpc_shutdown_client(struct rpc_clnt *clnt)
938 trace_rpc_clnt_shutdown(clnt);
940 while (!list_empty(&clnt->cl_tasks)) {
941 rpc_killall_tasks(clnt);
942 wait_event_timeout(destroy_wait,
943 list_empty(&clnt->cl_tasks), 1*HZ);
946 rpc_release_client(clnt);
948 EXPORT_SYMBOL_GPL(rpc_shutdown_client);
953 static void rpc_free_client_work(struct work_struct *work)
955 struct rpc_clnt *clnt = container_of(work, struct rpc_clnt, cl_work);
957 trace_rpc_clnt_free(clnt);
959 /* These might block on processes that might allocate memory,
960 * so they cannot be called in rpciod, so they are handled separately
963 rpc_sysfs_client_destroy(clnt);
964 rpc_clnt_debugfs_unregister(clnt);
966 rpc_clnt_remove_pipedir(clnt);
967 xprt_put(rcu_dereference_raw(clnt->cl_xprt));
972 static struct rpc_clnt *
973 rpc_free_client(struct rpc_clnt *clnt)
975 struct rpc_clnt *parent = NULL;
977 trace_rpc_clnt_release(clnt);
978 if (clnt->cl_parent != clnt)
979 parent = clnt->cl_parent;
980 rpc_unregister_client(clnt);
981 rpc_free_iostats(clnt->cl_metrics);
982 clnt->cl_metrics = NULL;
983 xprt_iter_destroy(&clnt->cl_xpi);
984 put_cred(clnt->cl_cred);
986 INIT_WORK(&clnt->cl_work, rpc_free_client_work);
987 schedule_work(&clnt->cl_work);
994 static struct rpc_clnt *
995 rpc_free_auth(struct rpc_clnt *clnt)
998 * Note: RPCSEC_GSS may need to send NULL RPC calls in order to
999 * release remaining GSS contexts. This mechanism ensures
1000 * that it can do so safely.
1002 if (clnt->cl_auth != NULL) {
1003 rpcauth_release(clnt->cl_auth);
1004 clnt->cl_auth = NULL;
1006 if (refcount_dec_and_test(&clnt->cl_count))
1007 return rpc_free_client(clnt);
1012 * Release reference to the RPC client
1015 rpc_release_client(struct rpc_clnt *clnt)
1018 if (list_empty(&clnt->cl_tasks))
1019 wake_up(&destroy_wait);
1020 if (refcount_dec_not_one(&clnt->cl_count))
1022 clnt = rpc_free_auth(clnt);
1023 } while (clnt != NULL);
1025 EXPORT_SYMBOL_GPL(rpc_release_client);
1028 * rpc_bind_new_program - bind a new RPC program to an existing client
1029 * @old: old rpc_client
1030 * @program: rpc program to set
1031 * @vers: rpc program version
1033 * Clones the rpc client and sets up a new RPC program. This is mainly
1034 * of use for enabling different RPC programs to share the same transport.
1035 * The Sun NFSv2/v3 ACL protocol can do this.
1037 struct rpc_clnt *rpc_bind_new_program(struct rpc_clnt *old,
1038 const struct rpc_program *program,
1041 struct rpc_create_args args = {
1043 .prognumber = program->number,
1045 .authflavor = old->cl_auth->au_flavor,
1046 .cred = old->cl_cred,
1048 struct rpc_clnt *clnt;
1051 clnt = __rpc_clone_client(&args, old);
1054 err = rpc_ping(clnt);
1056 rpc_shutdown_client(clnt);
1057 clnt = ERR_PTR(err);
1062 EXPORT_SYMBOL_GPL(rpc_bind_new_program);
1065 rpc_task_get_xprt(struct rpc_clnt *clnt, struct rpc_xprt *xprt)
1067 struct rpc_xprt_switch *xps;
1072 xps = rcu_dereference(clnt->cl_xpi.xpi_xpswitch);
1073 atomic_long_inc(&xps->xps_queuelen);
1075 atomic_long_inc(&xprt->queuelen);
1081 rpc_task_release_xprt(struct rpc_clnt *clnt, struct rpc_xprt *xprt)
1083 struct rpc_xprt_switch *xps;
1085 atomic_long_dec(&xprt->queuelen);
1087 xps = rcu_dereference(clnt->cl_xpi.xpi_xpswitch);
1088 atomic_long_dec(&xps->xps_queuelen);
1094 void rpc_task_release_transport(struct rpc_task *task)
1096 struct rpc_xprt *xprt = task->tk_xprt;
1099 task->tk_xprt = NULL;
1100 if (task->tk_client)
1101 rpc_task_release_xprt(task->tk_client, xprt);
1106 EXPORT_SYMBOL_GPL(rpc_task_release_transport);
1108 void rpc_task_release_client(struct rpc_task *task)
1110 struct rpc_clnt *clnt = task->tk_client;
1112 rpc_task_release_transport(task);
1114 /* Remove from client task list */
1115 spin_lock(&clnt->cl_lock);
1116 list_del(&task->tk_task);
1117 spin_unlock(&clnt->cl_lock);
1118 task->tk_client = NULL;
1120 rpc_release_client(clnt);
1124 static struct rpc_xprt *
1125 rpc_task_get_first_xprt(struct rpc_clnt *clnt)
1127 struct rpc_xprt *xprt;
1130 xprt = xprt_get(rcu_dereference(clnt->cl_xprt));
1132 return rpc_task_get_xprt(clnt, xprt);
1135 static struct rpc_xprt *
1136 rpc_task_get_next_xprt(struct rpc_clnt *clnt)
1138 return rpc_task_get_xprt(clnt, xprt_iter_get_next(&clnt->cl_xpi));
1142 void rpc_task_set_transport(struct rpc_task *task, struct rpc_clnt *clnt)
1144 if (task->tk_xprt) {
1145 if (!(test_bit(XPRT_OFFLINE, &task->tk_xprt->state) &&
1146 (task->tk_flags & RPC_TASK_MOVEABLE)))
1149 xprt_put(task->tk_xprt);
1151 if (task->tk_flags & RPC_TASK_NO_ROUND_ROBIN)
1152 task->tk_xprt = rpc_task_get_first_xprt(clnt);
1154 task->tk_xprt = rpc_task_get_next_xprt(clnt);
1158 void rpc_task_set_client(struct rpc_task *task, struct rpc_clnt *clnt)
1160 rpc_task_set_transport(task, clnt);
1161 task->tk_client = clnt;
1162 refcount_inc(&clnt->cl_count);
1163 if (clnt->cl_softrtry)
1164 task->tk_flags |= RPC_TASK_SOFT;
1165 if (clnt->cl_softerr)
1166 task->tk_flags |= RPC_TASK_TIMEOUT;
1167 if (clnt->cl_noretranstimeo)
1168 task->tk_flags |= RPC_TASK_NO_RETRANS_TIMEOUT;
1169 /* Add to the client's list of all tasks */
1170 spin_lock(&clnt->cl_lock);
1171 list_add_tail(&task->tk_task, &clnt->cl_tasks);
1172 spin_unlock(&clnt->cl_lock);
1176 rpc_task_set_rpc_message(struct rpc_task *task, const struct rpc_message *msg)
1179 task->tk_msg.rpc_proc = msg->rpc_proc;
1180 task->tk_msg.rpc_argp = msg->rpc_argp;
1181 task->tk_msg.rpc_resp = msg->rpc_resp;
1182 task->tk_msg.rpc_cred = msg->rpc_cred;
1183 if (!(task->tk_flags & RPC_TASK_CRED_NOREF))
1184 get_cred(task->tk_msg.rpc_cred);
1189 * Default callback for async RPC calls
1192 rpc_default_callback(struct rpc_task *task, void *data)
1196 static const struct rpc_call_ops rpc_default_ops = {
1197 .rpc_call_done = rpc_default_callback,
1201 * rpc_run_task - Allocate a new RPC task, then run rpc_execute against it
1202 * @task_setup_data: pointer to task initialisation data
1204 struct rpc_task *rpc_run_task(const struct rpc_task_setup *task_setup_data)
1206 struct rpc_task *task;
1208 task = rpc_new_task(task_setup_data);
1212 if (!RPC_IS_ASYNC(task))
1213 task->tk_flags |= RPC_TASK_CRED_NOREF;
1215 rpc_task_set_client(task, task_setup_data->rpc_client);
1216 rpc_task_set_rpc_message(task, task_setup_data->rpc_message);
1218 if (task->tk_action == NULL)
1219 rpc_call_start(task);
1221 atomic_inc(&task->tk_count);
1225 EXPORT_SYMBOL_GPL(rpc_run_task);
1228 * rpc_call_sync - Perform a synchronous RPC call
1229 * @clnt: pointer to RPC client
1230 * @msg: RPC call parameters
1231 * @flags: RPC call flags
1233 int rpc_call_sync(struct rpc_clnt *clnt, const struct rpc_message *msg, int flags)
1235 struct rpc_task *task;
1236 struct rpc_task_setup task_setup_data = {
1239 .callback_ops = &rpc_default_ops,
1244 WARN_ON_ONCE(flags & RPC_TASK_ASYNC);
1245 if (flags & RPC_TASK_ASYNC) {
1246 rpc_release_calldata(task_setup_data.callback_ops,
1247 task_setup_data.callback_data);
1251 task = rpc_run_task(&task_setup_data);
1253 return PTR_ERR(task);
1254 status = task->tk_status;
1258 EXPORT_SYMBOL_GPL(rpc_call_sync);
1261 * rpc_call_async - Perform an asynchronous RPC call
1262 * @clnt: pointer to RPC client
1263 * @msg: RPC call parameters
1264 * @flags: RPC call flags
1265 * @tk_ops: RPC call ops
1266 * @data: user call data
1269 rpc_call_async(struct rpc_clnt *clnt, const struct rpc_message *msg, int flags,
1270 const struct rpc_call_ops *tk_ops, void *data)
1272 struct rpc_task *task;
1273 struct rpc_task_setup task_setup_data = {
1276 .callback_ops = tk_ops,
1277 .callback_data = data,
1278 .flags = flags|RPC_TASK_ASYNC,
1281 task = rpc_run_task(&task_setup_data);
1283 return PTR_ERR(task);
1287 EXPORT_SYMBOL_GPL(rpc_call_async);
1289 #if defined(CONFIG_SUNRPC_BACKCHANNEL)
1290 static void call_bc_encode(struct rpc_task *task);
1293 * rpc_run_bc_task - Allocate a new RPC task for backchannel use, then run
1294 * rpc_execute against it
1297 struct rpc_task *rpc_run_bc_task(struct rpc_rqst *req)
1299 struct rpc_task *task;
1300 struct rpc_task_setup task_setup_data = {
1301 .callback_ops = &rpc_default_ops,
1302 .flags = RPC_TASK_SOFTCONN |
1303 RPC_TASK_NO_RETRANS_TIMEOUT,
1306 dprintk("RPC: rpc_run_bc_task req= %p\n", req);
1308 * Create an rpc_task to send the data
1310 task = rpc_new_task(&task_setup_data);
1312 xprt_free_bc_request(req);
1316 xprt_init_bc_request(req, task);
1318 task->tk_action = call_bc_encode;
1319 atomic_inc(&task->tk_count);
1320 WARN_ON_ONCE(atomic_read(&task->tk_count) != 2);
1323 dprintk("RPC: rpc_run_bc_task: task= %p\n", task);
1326 #endif /* CONFIG_SUNRPC_BACKCHANNEL */
1329 * rpc_prepare_reply_pages - Prepare to receive a reply data payload into pages
1330 * @req: RPC request to prepare
1331 * @pages: vector of struct page pointers
1332 * @base: offset in first page where receive should start, in bytes
1333 * @len: expected size of the upper layer data payload, in bytes
1334 * @hdrsize: expected size of upper layer reply header, in XDR words
1337 void rpc_prepare_reply_pages(struct rpc_rqst *req, struct page **pages,
1338 unsigned int base, unsigned int len,
1339 unsigned int hdrsize)
1341 hdrsize += RPC_REPHDRSIZE + req->rq_cred->cr_auth->au_ralign;
1343 xdr_inline_pages(&req->rq_rcv_buf, hdrsize << 2, pages, base, len);
1344 trace_rpc_xdr_reply_pages(req->rq_task, &req->rq_rcv_buf);
1346 EXPORT_SYMBOL_GPL(rpc_prepare_reply_pages);
1349 rpc_call_start(struct rpc_task *task)
1351 task->tk_action = call_start;
1353 EXPORT_SYMBOL_GPL(rpc_call_start);
1356 * rpc_peeraddr - extract remote peer address from clnt's xprt
1357 * @clnt: RPC client structure
1358 * @buf: target buffer
1359 * @bufsize: length of target buffer
1361 * Returns the number of bytes that are actually in the stored address.
1363 size_t rpc_peeraddr(struct rpc_clnt *clnt, struct sockaddr *buf, size_t bufsize)
1366 struct rpc_xprt *xprt;
1369 xprt = rcu_dereference(clnt->cl_xprt);
1371 bytes = xprt->addrlen;
1372 if (bytes > bufsize)
1374 memcpy(buf, &xprt->addr, bytes);
1379 EXPORT_SYMBOL_GPL(rpc_peeraddr);
1382 * rpc_peeraddr2str - return remote peer address in printable format
1383 * @clnt: RPC client structure
1384 * @format: address format
1386 * NB: the lifetime of the memory referenced by the returned pointer is
1387 * the same as the rpc_xprt itself. As long as the caller uses this
1388 * pointer, it must hold the RCU read lock.
1390 const char *rpc_peeraddr2str(struct rpc_clnt *clnt,
1391 enum rpc_display_format_t format)
1393 struct rpc_xprt *xprt;
1395 xprt = rcu_dereference(clnt->cl_xprt);
1397 if (xprt->address_strings[format] != NULL)
1398 return xprt->address_strings[format];
1400 return "unprintable";
1402 EXPORT_SYMBOL_GPL(rpc_peeraddr2str);
1404 static const struct sockaddr_in rpc_inaddr_loopback = {
1405 .sin_family = AF_INET,
1406 .sin_addr.s_addr = htonl(INADDR_ANY),
1409 static const struct sockaddr_in6 rpc_in6addr_loopback = {
1410 .sin6_family = AF_INET6,
1411 .sin6_addr = IN6ADDR_ANY_INIT,
1415 * Try a getsockname() on a connected datagram socket. Using a
1416 * connected datagram socket prevents leaving a socket in TIME_WAIT.
1417 * This conserves the ephemeral port number space.
1419 * Returns zero and fills in "buf" if successful; otherwise, a
1420 * negative errno is returned.
1422 static int rpc_sockname(struct net *net, struct sockaddr *sap, size_t salen,
1423 struct sockaddr *buf)
1425 struct socket *sock;
1428 err = __sock_create(net, sap->sa_family,
1429 SOCK_DGRAM, IPPROTO_UDP, &sock, 1);
1431 dprintk("RPC: can't create UDP socket (%d)\n", err);
1435 switch (sap->sa_family) {
1437 err = kernel_bind(sock,
1438 (struct sockaddr *)&rpc_inaddr_loopback,
1439 sizeof(rpc_inaddr_loopback));
1442 err = kernel_bind(sock,
1443 (struct sockaddr *)&rpc_in6addr_loopback,
1444 sizeof(rpc_in6addr_loopback));
1447 err = -EAFNOSUPPORT;
1451 dprintk("RPC: can't bind UDP socket (%d)\n", err);
1455 err = kernel_connect(sock, sap, salen, 0);
1457 dprintk("RPC: can't connect UDP socket (%d)\n", err);
1461 err = kernel_getsockname(sock, buf);
1463 dprintk("RPC: getsockname failed (%d)\n", err);
1468 if (buf->sa_family == AF_INET6) {
1469 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)buf;
1470 sin6->sin6_scope_id = 0;
1472 dprintk("RPC: %s succeeded\n", __func__);
1481 * Scraping a connected socket failed, so we don't have a useable
1482 * local address. Fallback: generate an address that will prevent
1483 * the server from calling us back.
1485 * Returns zero and fills in "buf" if successful; otherwise, a
1486 * negative errno is returned.
1488 static int rpc_anyaddr(int family, struct sockaddr *buf, size_t buflen)
1492 if (buflen < sizeof(rpc_inaddr_loopback))
1494 memcpy(buf, &rpc_inaddr_loopback,
1495 sizeof(rpc_inaddr_loopback));
1498 if (buflen < sizeof(rpc_in6addr_loopback))
1500 memcpy(buf, &rpc_in6addr_loopback,
1501 sizeof(rpc_in6addr_loopback));
1504 dprintk("RPC: %s: address family not supported\n",
1506 return -EAFNOSUPPORT;
1508 dprintk("RPC: %s: succeeded\n", __func__);
1513 * rpc_localaddr - discover local endpoint address for an RPC client
1514 * @clnt: RPC client structure
1515 * @buf: target buffer
1516 * @buflen: size of target buffer, in bytes
1518 * Returns zero and fills in "buf" and "buflen" if successful;
1519 * otherwise, a negative errno is returned.
1521 * This works even if the underlying transport is not currently connected,
1522 * or if the upper layer never previously provided a source address.
1524 * The result of this function call is transient: multiple calls in
1525 * succession may give different results, depending on how local
1526 * networking configuration changes over time.
1528 int rpc_localaddr(struct rpc_clnt *clnt, struct sockaddr *buf, size_t buflen)
1530 struct sockaddr_storage address;
1531 struct sockaddr *sap = (struct sockaddr *)&address;
1532 struct rpc_xprt *xprt;
1538 xprt = rcu_dereference(clnt->cl_xprt);
1539 salen = xprt->addrlen;
1540 memcpy(sap, &xprt->addr, salen);
1541 net = get_net(xprt->xprt_net);
1544 rpc_set_port(sap, 0);
1545 err = rpc_sockname(net, sap, salen, buf);
1548 /* Couldn't discover local address, return ANYADDR */
1549 return rpc_anyaddr(sap->sa_family, buf, buflen);
1552 EXPORT_SYMBOL_GPL(rpc_localaddr);
1555 rpc_setbufsize(struct rpc_clnt *clnt, unsigned int sndsize, unsigned int rcvsize)
1557 struct rpc_xprt *xprt;
1560 xprt = rcu_dereference(clnt->cl_xprt);
1561 if (xprt->ops->set_buffer_size)
1562 xprt->ops->set_buffer_size(xprt, sndsize, rcvsize);
1565 EXPORT_SYMBOL_GPL(rpc_setbufsize);
1568 * rpc_net_ns - Get the network namespace for this RPC client
1569 * @clnt: RPC client to query
1572 struct net *rpc_net_ns(struct rpc_clnt *clnt)
1577 ret = rcu_dereference(clnt->cl_xprt)->xprt_net;
1581 EXPORT_SYMBOL_GPL(rpc_net_ns);
1584 * rpc_max_payload - Get maximum payload size for a transport, in bytes
1585 * @clnt: RPC client to query
1587 * For stream transports, this is one RPC record fragment (see RFC
1588 * 1831), as we don't support multi-record requests yet. For datagram
1589 * transports, this is the size of an IP packet minus the IP, UDP, and
1592 size_t rpc_max_payload(struct rpc_clnt *clnt)
1597 ret = rcu_dereference(clnt->cl_xprt)->max_payload;
1601 EXPORT_SYMBOL_GPL(rpc_max_payload);
1604 * rpc_max_bc_payload - Get maximum backchannel payload size, in bytes
1605 * @clnt: RPC client to query
1607 size_t rpc_max_bc_payload(struct rpc_clnt *clnt)
1609 struct rpc_xprt *xprt;
1613 xprt = rcu_dereference(clnt->cl_xprt);
1614 ret = xprt->ops->bc_maxpayload(xprt);
1618 EXPORT_SYMBOL_GPL(rpc_max_bc_payload);
1620 unsigned int rpc_num_bc_slots(struct rpc_clnt *clnt)
1622 struct rpc_xprt *xprt;
1626 xprt = rcu_dereference(clnt->cl_xprt);
1627 ret = xprt->ops->bc_num_slots(xprt);
1631 EXPORT_SYMBOL_GPL(rpc_num_bc_slots);
1634 * rpc_force_rebind - force transport to check that remote port is unchanged
1635 * @clnt: client to rebind
1638 void rpc_force_rebind(struct rpc_clnt *clnt)
1640 if (clnt->cl_autobind) {
1642 xprt_clear_bound(rcu_dereference(clnt->cl_xprt));
1646 EXPORT_SYMBOL_GPL(rpc_force_rebind);
1649 __rpc_restart_call(struct rpc_task *task, void (*action)(struct rpc_task *))
1651 task->tk_status = 0;
1652 task->tk_rpc_status = 0;
1653 task->tk_action = action;
1658 * Restart an (async) RPC call. Usually called from within the
1662 rpc_restart_call(struct rpc_task *task)
1664 return __rpc_restart_call(task, call_start);
1666 EXPORT_SYMBOL_GPL(rpc_restart_call);
1669 * Restart an (async) RPC call from the call_prepare state.
1670 * Usually called from within the exit handler.
1673 rpc_restart_call_prepare(struct rpc_task *task)
1675 if (task->tk_ops->rpc_call_prepare != NULL)
1676 return __rpc_restart_call(task, rpc_prepare_task);
1677 return rpc_restart_call(task);
1679 EXPORT_SYMBOL_GPL(rpc_restart_call_prepare);
1682 *rpc_proc_name(const struct rpc_task *task)
1684 const struct rpc_procinfo *proc = task->tk_msg.rpc_proc;
1688 return proc->p_name;
1696 __rpc_call_rpcerror(struct rpc_task *task, int tk_status, int rpc_status)
1698 trace_rpc_call_rpcerror(task, tk_status, rpc_status);
1699 rpc_task_set_rpc_status(task, rpc_status);
1700 rpc_exit(task, tk_status);
1704 rpc_call_rpcerror(struct rpc_task *task, int status)
1706 __rpc_call_rpcerror(task, status, status);
1712 * Other FSM states can be visited zero or more times, but
1713 * this state is visited exactly once for each RPC.
1716 call_start(struct rpc_task *task)
1718 struct rpc_clnt *clnt = task->tk_client;
1719 int idx = task->tk_msg.rpc_proc->p_statidx;
1721 trace_rpc_request(task);
1723 /* Increment call count (version might not be valid for ping) */
1724 if (clnt->cl_program->version[clnt->cl_vers])
1725 clnt->cl_program->version[clnt->cl_vers]->counts[idx]++;
1726 clnt->cl_stats->rpccnt++;
1727 task->tk_action = call_reserve;
1728 rpc_task_set_transport(task, clnt);
1732 * 1. Reserve an RPC call slot
1735 call_reserve(struct rpc_task *task)
1737 task->tk_status = 0;
1738 task->tk_action = call_reserveresult;
1742 static void call_retry_reserve(struct rpc_task *task);
1745 * 1b. Grok the result of xprt_reserve()
1748 call_reserveresult(struct rpc_task *task)
1750 int status = task->tk_status;
1753 * After a call to xprt_reserve(), we must have either
1754 * a request slot or else an error status.
1756 task->tk_status = 0;
1758 if (task->tk_rqstp) {
1759 task->tk_action = call_refresh;
1763 rpc_call_rpcerror(task, -EIO);
1769 rpc_delay(task, HZ >> 2);
1771 case -EAGAIN: /* woken up; retry */
1772 task->tk_action = call_retry_reserve;
1775 rpc_call_rpcerror(task, status);
1780 * 1c. Retry reserving an RPC call slot
1783 call_retry_reserve(struct rpc_task *task)
1785 task->tk_status = 0;
1786 task->tk_action = call_reserveresult;
1787 xprt_retry_reserve(task);
1791 * 2. Bind and/or refresh the credentials
1794 call_refresh(struct rpc_task *task)
1796 task->tk_action = call_refreshresult;
1797 task->tk_status = 0;
1798 task->tk_client->cl_stats->rpcauthrefresh++;
1799 rpcauth_refreshcred(task);
1803 * 2a. Process the results of a credential refresh
1806 call_refreshresult(struct rpc_task *task)
1808 int status = task->tk_status;
1810 task->tk_status = 0;
1811 task->tk_action = call_refresh;
1814 if (rpcauth_uptodatecred(task)) {
1815 task->tk_action = call_allocate;
1818 /* Use rate-limiting and a max number of retries if refresh
1819 * had status 0 but failed to update the cred.
1823 rpc_delay(task, 3*HZ);
1829 if (!task->tk_cred_retry)
1831 task->tk_cred_retry--;
1832 trace_rpc_retry_refresh_status(task);
1835 rpc_delay(task, HZ >> 4);
1838 trace_rpc_refresh_status(task);
1839 rpc_call_rpcerror(task, status);
1843 * 2b. Allocate the buffer. For details, see sched.c:rpc_malloc.
1844 * (Note: buffer memory is freed in xprt_release).
1847 call_allocate(struct rpc_task *task)
1849 const struct rpc_auth *auth = task->tk_rqstp->rq_cred->cr_auth;
1850 struct rpc_rqst *req = task->tk_rqstp;
1851 struct rpc_xprt *xprt = req->rq_xprt;
1852 const struct rpc_procinfo *proc = task->tk_msg.rpc_proc;
1855 task->tk_status = 0;
1856 task->tk_action = call_encode;
1861 if (proc->p_proc != 0) {
1862 BUG_ON(proc->p_arglen == 0);
1863 if (proc->p_decode != NULL)
1864 BUG_ON(proc->p_replen == 0);
1868 * Calculate the size (in quads) of the RPC call
1869 * and reply headers, and convert both values
1872 req->rq_callsize = RPC_CALLHDRSIZE + (auth->au_cslack << 1) +
1874 req->rq_callsize <<= 2;
1876 * Note: the reply buffer must at minimum allocate enough space
1877 * for the 'struct accepted_reply' from RFC5531.
1879 req->rq_rcvsize = RPC_REPHDRSIZE + auth->au_rslack + \
1880 max_t(size_t, proc->p_replen, 2);
1881 req->rq_rcvsize <<= 2;
1883 status = xprt->ops->buf_alloc(task);
1884 trace_rpc_buf_alloc(task, status);
1887 if (status != -ENOMEM) {
1888 rpc_call_rpcerror(task, status);
1892 if (RPC_IS_ASYNC(task) || !fatal_signal_pending(current)) {
1893 task->tk_action = call_allocate;
1894 rpc_delay(task, HZ>>4);
1898 rpc_call_rpcerror(task, -ERESTARTSYS);
1902 rpc_task_need_encode(struct rpc_task *task)
1904 return test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate) == 0 &&
1905 (!(task->tk_flags & RPC_TASK_SENT) ||
1906 !(task->tk_flags & RPC_TASK_NO_RETRANS_TIMEOUT) ||
1907 xprt_request_need_retransmit(task));
1911 rpc_xdr_encode(struct rpc_task *task)
1913 struct rpc_rqst *req = task->tk_rqstp;
1914 struct xdr_stream xdr;
1916 xdr_buf_init(&req->rq_snd_buf,
1919 xdr_buf_init(&req->rq_rcv_buf,
1923 req->rq_reply_bytes_recvd = 0;
1924 req->rq_snd_buf.head[0].iov_len = 0;
1925 xdr_init_encode(&xdr, &req->rq_snd_buf,
1926 req->rq_snd_buf.head[0].iov_base, req);
1927 if (rpc_encode_header(task, &xdr))
1930 task->tk_status = rpcauth_wrap_req(task, &xdr);
1934 * 3. Encode arguments of an RPC call
1937 call_encode(struct rpc_task *task)
1939 if (!rpc_task_need_encode(task))
1942 /* Dequeue task from the receive queue while we're encoding */
1943 xprt_request_dequeue_xprt(task);
1944 /* Encode here so that rpcsec_gss can use correct sequence number. */
1945 rpc_xdr_encode(task);
1946 /* Add task to reply queue before transmission to avoid races */
1947 if (task->tk_status == 0 && rpc_reply_expected(task))
1948 task->tk_status = xprt_request_enqueue_receive(task);
1949 /* Did the encode result in an error condition? */
1950 if (task->tk_status != 0) {
1951 /* Was the error nonfatal? */
1952 switch (task->tk_status) {
1955 rpc_delay(task, HZ >> 4);
1958 if (!task->tk_cred_retry) {
1959 rpc_call_rpcerror(task, task->tk_status);
1961 task->tk_action = call_refresh;
1962 task->tk_cred_retry--;
1963 trace_rpc_retry_refresh_status(task);
1967 rpc_call_rpcerror(task, task->tk_status);
1972 xprt_request_enqueue_transmit(task);
1974 task->tk_action = call_transmit;
1975 /* Check that the connection is OK */
1976 if (!xprt_bound(task->tk_xprt))
1977 task->tk_action = call_bind;
1978 else if (!xprt_connected(task->tk_xprt))
1979 task->tk_action = call_connect;
1983 * Helpers to check if the task was already transmitted, and
1984 * to take action when that is the case.
1987 rpc_task_transmitted(struct rpc_task *task)
1989 return !test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate);
1993 rpc_task_handle_transmitted(struct rpc_task *task)
1995 xprt_end_transmit(task);
1996 task->tk_action = call_transmit_status;
2000 * 4. Get the server port number if not yet set
2003 call_bind(struct rpc_task *task)
2005 struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
2007 if (rpc_task_transmitted(task)) {
2008 rpc_task_handle_transmitted(task);
2012 if (xprt_bound(xprt)) {
2013 task->tk_action = call_connect;
2017 task->tk_action = call_bind_status;
2018 if (!xprt_prepare_transmit(task))
2021 xprt->ops->rpcbind(task);
2025 * 4a. Sort out bind result
2028 call_bind_status(struct rpc_task *task)
2030 struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
2033 if (rpc_task_transmitted(task)) {
2034 rpc_task_handle_transmitted(task);
2038 if (task->tk_status >= 0)
2040 if (xprt_bound(xprt)) {
2041 task->tk_status = 0;
2045 switch (task->tk_status) {
2047 rpc_delay(task, HZ >> 2);
2050 trace_rpcb_prog_unavail_err(task);
2051 /* fail immediately if this is an RPC ping */
2052 if (task->tk_msg.rpc_proc->p_proc == 0) {
2053 status = -EOPNOTSUPP;
2056 rpc_delay(task, 3*HZ);
2059 rpc_delay(task, HZ >> 2);
2064 trace_rpcb_timeout_err(task);
2067 /* server doesn't support any rpcbind version we know of */
2068 trace_rpcb_bind_version_err(task);
2070 case -EPROTONOSUPPORT:
2071 trace_rpcb_bind_version_err(task);
2073 case -ECONNREFUSED: /* connection problems */
2082 trace_rpcb_unreachable_err(task);
2083 if (!RPC_IS_SOFTCONN(task)) {
2084 rpc_delay(task, 5*HZ);
2087 status = task->tk_status;
2090 trace_rpcb_unrecognized_err(task);
2093 rpc_call_rpcerror(task, status);
2096 task->tk_action = call_connect;
2099 task->tk_status = 0;
2100 task->tk_action = call_bind;
2101 rpc_check_timeout(task);
2105 * 4b. Connect to the RPC server
2108 call_connect(struct rpc_task *task)
2110 struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
2112 if (rpc_task_transmitted(task)) {
2113 rpc_task_handle_transmitted(task);
2117 if (xprt_connected(xprt)) {
2118 task->tk_action = call_transmit;
2122 task->tk_action = call_connect_status;
2123 if (task->tk_status < 0)
2125 if (task->tk_flags & RPC_TASK_NOCONNECT) {
2126 rpc_call_rpcerror(task, -ENOTCONN);
2129 if (!xprt_prepare_transmit(task))
2135 * 4c. Sort out connect result
2138 call_connect_status(struct rpc_task *task)
2140 struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
2141 struct rpc_clnt *clnt = task->tk_client;
2142 int status = task->tk_status;
2144 if (rpc_task_transmitted(task)) {
2145 rpc_task_handle_transmitted(task);
2149 trace_rpc_connect_status(task);
2151 if (task->tk_status == 0) {
2152 clnt->cl_stats->netreconn++;
2155 if (xprt_connected(xprt)) {
2156 task->tk_status = 0;
2160 task->tk_status = 0;
2164 /* A positive refusal suggests a rebind is needed. */
2165 if (RPC_IS_SOFTCONN(task))
2167 if (clnt->cl_autobind) {
2168 rpc_force_rebind(clnt);
2178 xprt_conditional_disconnect(task->tk_rqstp->rq_xprt,
2179 task->tk_rqstp->rq_connect_cookie);
2180 if (RPC_IS_SOFTCONN(task))
2182 /* retry with existing socket, after a delay */
2183 rpc_delay(task, 3*HZ);
2189 if (!(task->tk_flags & RPC_TASK_NO_ROUND_ROBIN) &&
2190 (task->tk_flags & RPC_TASK_MOVEABLE) &&
2191 test_bit(XPRT_REMOVE, &xprt->state)) {
2192 struct rpc_xprt *saved = task->tk_xprt;
2193 struct rpc_xprt_switch *xps;
2196 xps = xprt_switch_get(rcu_dereference(clnt->cl_xpi.xpi_xpswitch));
2198 if (xps->xps_nxprts > 1) {
2202 value = atomic_long_dec_return(&xprt->queuelen);
2204 rpc_xprt_switch_remove_xprt(xps, saved,
2207 task->tk_xprt = NULL;
2208 task->tk_action = call_start;
2210 xprt_switch_put(xps);
2216 rpc_delay(task, HZ >> 2);
2219 rpc_call_rpcerror(task, status);
2222 task->tk_action = call_transmit;
2225 /* Check for timeouts before looping back to call_bind */
2226 task->tk_action = call_bind;
2227 rpc_check_timeout(task);
2231 * 5. Transmit the RPC request, and wait for reply
2234 call_transmit(struct rpc_task *task)
2236 if (rpc_task_transmitted(task)) {
2237 rpc_task_handle_transmitted(task);
2241 task->tk_action = call_transmit_status;
2242 if (!xprt_prepare_transmit(task))
2244 task->tk_status = 0;
2245 if (test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate)) {
2246 if (!xprt_connected(task->tk_xprt)) {
2247 task->tk_status = -ENOTCONN;
2250 xprt_transmit(task);
2252 xprt_end_transmit(task);
2256 * 5a. Handle cleanup after a transmission
2259 call_transmit_status(struct rpc_task *task)
2261 task->tk_action = call_status;
2264 * Common case: success. Force the compiler to put this
2267 if (rpc_task_transmitted(task)) {
2268 task->tk_status = 0;
2269 xprt_request_wait_receive(task);
2273 switch (task->tk_status) {
2277 task->tk_status = 0;
2278 task->tk_action = call_encode;
2281 * Special cases: if we've been waiting on the
2282 * socket's write_space() callback, or if the
2283 * socket just returned a connection error,
2284 * then hold onto the transport lock.
2288 rpc_delay(task, HZ>>2);
2292 task->tk_action = call_transmit;
2293 task->tk_status = 0;
2301 if (RPC_IS_SOFTCONN(task)) {
2302 if (!task->tk_msg.rpc_proc->p_proc)
2303 trace_xprt_ping(task->tk_xprt,
2305 rpc_call_rpcerror(task, task->tk_status);
2314 task->tk_action = call_bind;
2315 task->tk_status = 0;
2318 rpc_check_timeout(task);
2321 #if defined(CONFIG_SUNRPC_BACKCHANNEL)
2322 static void call_bc_transmit(struct rpc_task *task);
2323 static void call_bc_transmit_status(struct rpc_task *task);
2326 call_bc_encode(struct rpc_task *task)
2328 xprt_request_enqueue_transmit(task);
2329 task->tk_action = call_bc_transmit;
2333 * 5b. Send the backchannel RPC reply. On error, drop the reply. In
2334 * addition, disconnect on connectivity errors.
2337 call_bc_transmit(struct rpc_task *task)
2339 task->tk_action = call_bc_transmit_status;
2340 if (test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate)) {
2341 if (!xprt_prepare_transmit(task))
2343 task->tk_status = 0;
2344 xprt_transmit(task);
2346 xprt_end_transmit(task);
2350 call_bc_transmit_status(struct rpc_task *task)
2352 struct rpc_rqst *req = task->tk_rqstp;
2354 if (rpc_task_transmitted(task))
2355 task->tk_status = 0;
2357 switch (task->tk_status) {
2372 rpc_delay(task, HZ>>2);
2376 task->tk_status = 0;
2377 task->tk_action = call_bc_transmit;
2381 * Problem reaching the server. Disconnect and let the
2382 * forechannel reestablish the connection. The server will
2383 * have to retransmit the backchannel request and we'll
2384 * reprocess it. Since these ops are idempotent, there's no
2385 * need to cache our reply at this time.
2387 printk(KERN_NOTICE "RPC: Could not send backchannel reply "
2388 "error: %d\n", task->tk_status);
2389 xprt_conditional_disconnect(req->rq_xprt,
2390 req->rq_connect_cookie);
2394 * We were unable to reply and will have to drop the
2395 * request. The server should reconnect and retransmit.
2397 printk(KERN_NOTICE "RPC: Could not send backchannel reply "
2398 "error: %d\n", task->tk_status);
2401 task->tk_action = rpc_exit_task;
2403 #endif /* CONFIG_SUNRPC_BACKCHANNEL */
2406 * 6. Sort out the RPC call status
2409 call_status(struct rpc_task *task)
2411 struct rpc_clnt *clnt = task->tk_client;
2414 if (!task->tk_msg.rpc_proc->p_proc)
2415 trace_xprt_ping(task->tk_xprt, task->tk_status);
2417 status = task->tk_status;
2419 task->tk_action = call_decode;
2423 trace_rpc_call_status(task);
2424 task->tk_status = 0;
2431 if (RPC_IS_SOFTCONN(task))
2434 * Delay any retries for 3 seconds, then handle as if it
2437 rpc_delay(task, 3*HZ);
2445 rpc_force_rebind(clnt);
2448 rpc_delay(task, 3*HZ);
2456 rpc_delay(task, HZ>>2);
2459 /* shutdown or soft timeout */
2462 if (clnt->cl_chatty)
2463 printk("%s: RPC call returned error %d\n",
2464 clnt->cl_program->name, -status);
2467 task->tk_action = call_encode;
2468 rpc_check_timeout(task);
2471 rpc_call_rpcerror(task, status);
2475 rpc_check_connected(const struct rpc_rqst *req)
2477 /* No allocated request or transport? return true */
2478 if (!req || !req->rq_xprt)
2480 return xprt_connected(req->rq_xprt);
2484 rpc_check_timeout(struct rpc_task *task)
2486 struct rpc_clnt *clnt = task->tk_client;
2488 if (RPC_SIGNALLED(task))
2491 if (xprt_adjust_timeout(task->tk_rqstp) == 0)
2494 trace_rpc_timeout_status(task);
2495 task->tk_timeouts++;
2497 if (RPC_IS_SOFTCONN(task) && !rpc_check_connected(task->tk_rqstp)) {
2498 rpc_call_rpcerror(task, -ETIMEDOUT);
2502 if (RPC_IS_SOFT(task)) {
2504 * Once a "no retrans timeout" soft tasks (a.k.a NFSv4) has
2505 * been sent, it should time out only if the transport
2506 * connection gets terminally broken.
2508 if ((task->tk_flags & RPC_TASK_NO_RETRANS_TIMEOUT) &&
2509 rpc_check_connected(task->tk_rqstp))
2512 if (clnt->cl_chatty) {
2513 pr_notice_ratelimited(
2514 "%s: server %s not responding, timed out\n",
2515 clnt->cl_program->name,
2516 task->tk_xprt->servername);
2518 if (task->tk_flags & RPC_TASK_TIMEOUT)
2519 rpc_call_rpcerror(task, -ETIMEDOUT);
2521 __rpc_call_rpcerror(task, -EIO, -ETIMEDOUT);
2525 if (!(task->tk_flags & RPC_CALL_MAJORSEEN)) {
2526 task->tk_flags |= RPC_CALL_MAJORSEEN;
2527 if (clnt->cl_chatty) {
2528 pr_notice_ratelimited(
2529 "%s: server %s not responding, still trying\n",
2530 clnt->cl_program->name,
2531 task->tk_xprt->servername);
2534 rpc_force_rebind(clnt);
2536 * Did our request time out due to an RPCSEC_GSS out-of-sequence
2537 * event? RFC2203 requires the server to drop all such requests.
2539 rpcauth_invalcred(task);
2543 * 7. Decode the RPC reply
2546 call_decode(struct rpc_task *task)
2548 struct rpc_clnt *clnt = task->tk_client;
2549 struct rpc_rqst *req = task->tk_rqstp;
2550 struct xdr_stream xdr;
2553 if (!task->tk_msg.rpc_proc->p_decode) {
2554 task->tk_action = rpc_exit_task;
2558 if (task->tk_flags & RPC_CALL_MAJORSEEN) {
2559 if (clnt->cl_chatty) {
2560 pr_notice_ratelimited("%s: server %s OK\n",
2561 clnt->cl_program->name,
2562 task->tk_xprt->servername);
2564 task->tk_flags &= ~RPC_CALL_MAJORSEEN;
2568 * Did we ever call xprt_complete_rqst()? If not, we should assume
2569 * the message is incomplete.
2572 if (!req->rq_reply_bytes_recvd)
2575 /* Ensure that we see all writes made by xprt_complete_rqst()
2576 * before it changed req->rq_reply_bytes_recvd.
2580 req->rq_rcv_buf.len = req->rq_private_buf.len;
2581 trace_rpc_xdr_recvfrom(task, &req->rq_rcv_buf);
2583 /* Check that the softirq receive buffer is valid */
2584 WARN_ON(memcmp(&req->rq_rcv_buf, &req->rq_private_buf,
2585 sizeof(req->rq_rcv_buf)) != 0);
2587 xdr_init_decode(&xdr, &req->rq_rcv_buf,
2588 req->rq_rcv_buf.head[0].iov_base, req);
2589 err = rpc_decode_header(task, &xdr);
2593 task->tk_action = rpc_exit_task;
2594 task->tk_status = rpcauth_unwrap_resp(task, &xdr);
2597 task->tk_status = 0;
2598 if (task->tk_client->cl_discrtry)
2599 xprt_conditional_disconnect(req->rq_xprt,
2600 req->rq_connect_cookie);
2601 task->tk_action = call_encode;
2602 rpc_check_timeout(task);
2605 task->tk_action = call_reserve;
2606 rpc_check_timeout(task);
2607 rpcauth_invalcred(task);
2608 /* Ensure we obtain a new XID if we retry! */
2614 rpc_encode_header(struct rpc_task *task, struct xdr_stream *xdr)
2616 struct rpc_clnt *clnt = task->tk_client;
2617 struct rpc_rqst *req = task->tk_rqstp;
2622 p = xdr_reserve_space(xdr, RPC_CALLHDRSIZE << 2);
2627 *p++ = cpu_to_be32(RPC_VERSION);
2628 *p++ = cpu_to_be32(clnt->cl_prog);
2629 *p++ = cpu_to_be32(clnt->cl_vers);
2630 *p = cpu_to_be32(task->tk_msg.rpc_proc->p_proc);
2632 error = rpcauth_marshcred(task, xdr);
2637 trace_rpc_bad_callhdr(task);
2638 rpc_call_rpcerror(task, error);
2643 rpc_decode_header(struct rpc_task *task, struct xdr_stream *xdr)
2645 struct rpc_clnt *clnt = task->tk_client;
2649 /* RFC-1014 says that the representation of XDR data must be a
2650 * multiple of four bytes
2651 * - if it isn't pointer subtraction in the NFS client may give
2654 if (task->tk_rqstp->rq_rcv_buf.len & 3)
2655 goto out_unparsable;
2657 p = xdr_inline_decode(xdr, 3 * sizeof(*p));
2659 goto out_unparsable;
2661 if (*p++ != rpc_reply)
2662 goto out_unparsable;
2663 if (*p++ != rpc_msg_accepted)
2664 goto out_msg_denied;
2666 error = rpcauth_checkverf(task, xdr);
2670 p = xdr_inline_decode(xdr, sizeof(*p));
2672 goto out_unparsable;
2676 case rpc_prog_unavail:
2677 trace_rpc__prog_unavail(task);
2678 error = -EPFNOSUPPORT;
2680 case rpc_prog_mismatch:
2681 trace_rpc__prog_mismatch(task);
2682 error = -EPROTONOSUPPORT;
2684 case rpc_proc_unavail:
2685 trace_rpc__proc_unavail(task);
2686 error = -EOPNOTSUPP;
2688 case rpc_garbage_args:
2689 case rpc_system_err:
2690 trace_rpc__garbage_args(task);
2694 goto out_unparsable;
2698 clnt->cl_stats->rpcgarbage++;
2699 if (task->tk_garb_retry) {
2700 task->tk_garb_retry--;
2701 task->tk_action = call_encode;
2705 rpc_call_rpcerror(task, error);
2709 trace_rpc__unparsable(task);
2714 trace_rpc_bad_verifier(task);
2719 p = xdr_inline_decode(xdr, sizeof(*p));
2721 goto out_unparsable;
2723 case rpc_auth_error:
2726 trace_rpc__mismatch(task);
2727 error = -EPROTONOSUPPORT;
2730 goto out_unparsable;
2733 p = xdr_inline_decode(xdr, sizeof(*p));
2735 goto out_unparsable;
2737 case rpc_autherr_rejectedcred:
2738 case rpc_autherr_rejectedverf:
2739 case rpcsec_gsserr_credproblem:
2740 case rpcsec_gsserr_ctxproblem:
2741 rpcauth_invalcred(task);
2742 if (!task->tk_cred_retry)
2744 task->tk_cred_retry--;
2745 trace_rpc__stale_creds(task);
2746 return -EKEYREJECTED;
2747 case rpc_autherr_badcred:
2748 case rpc_autherr_badverf:
2749 /* possibly garbled cred/verf? */
2750 if (!task->tk_garb_retry)
2752 task->tk_garb_retry--;
2753 trace_rpc__bad_creds(task);
2754 task->tk_action = call_encode;
2756 case rpc_autherr_tooweak:
2757 trace_rpc__auth_tooweak(task);
2758 pr_warn("RPC: server %s requires stronger authentication.\n",
2759 task->tk_xprt->servername);
2762 goto out_unparsable;
2767 static void rpcproc_encode_null(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
2772 static int rpcproc_decode_null(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
2778 static const struct rpc_procinfo rpcproc_null = {
2779 .p_encode = rpcproc_encode_null,
2780 .p_decode = rpcproc_decode_null,
2783 static const struct rpc_procinfo rpcproc_null_noreply = {
2784 .p_encode = rpcproc_encode_null,
2788 rpc_null_call_prepare(struct rpc_task *task, void *data)
2790 task->tk_flags &= ~RPC_TASK_NO_RETRANS_TIMEOUT;
2791 rpc_call_start(task);
2794 static const struct rpc_call_ops rpc_null_ops = {
2795 .rpc_call_prepare = rpc_null_call_prepare,
2796 .rpc_call_done = rpc_default_callback,
2800 struct rpc_task *rpc_call_null_helper(struct rpc_clnt *clnt,
2801 struct rpc_xprt *xprt, struct rpc_cred *cred, int flags,
2802 const struct rpc_call_ops *ops, void *data)
2804 struct rpc_message msg = {
2805 .rpc_proc = &rpcproc_null,
2807 struct rpc_task_setup task_setup_data = {
2810 .rpc_message = &msg,
2811 .rpc_op_cred = cred,
2812 .callback_ops = ops ?: &rpc_null_ops,
2813 .callback_data = data,
2814 .flags = flags | RPC_TASK_SOFT | RPC_TASK_SOFTCONN |
2818 return rpc_run_task(&task_setup_data);
2821 struct rpc_task *rpc_call_null(struct rpc_clnt *clnt, struct rpc_cred *cred, int flags)
2823 return rpc_call_null_helper(clnt, NULL, cred, flags, NULL, NULL);
2825 EXPORT_SYMBOL_GPL(rpc_call_null);
2827 static int rpc_ping(struct rpc_clnt *clnt)
2829 struct rpc_task *task;
2832 task = rpc_call_null_helper(clnt, NULL, NULL, 0, NULL, NULL);
2834 return PTR_ERR(task);
2835 status = task->tk_status;
2840 static int rpc_ping_noreply(struct rpc_clnt *clnt)
2842 struct rpc_message msg = {
2843 .rpc_proc = &rpcproc_null_noreply,
2845 struct rpc_task_setup task_setup_data = {
2847 .rpc_message = &msg,
2848 .callback_ops = &rpc_null_ops,
2849 .flags = RPC_TASK_SOFT | RPC_TASK_SOFTCONN | RPC_TASK_NULLCREDS,
2851 struct rpc_task *task;
2854 task = rpc_run_task(&task_setup_data);
2856 return PTR_ERR(task);
2857 status = task->tk_status;
2862 struct rpc_cb_add_xprt_calldata {
2863 struct rpc_xprt_switch *xps;
2864 struct rpc_xprt *xprt;
2867 static void rpc_cb_add_xprt_done(struct rpc_task *task, void *calldata)
2869 struct rpc_cb_add_xprt_calldata *data = calldata;
2871 if (task->tk_status == 0)
2872 rpc_xprt_switch_add_xprt(data->xps, data->xprt);
2875 static void rpc_cb_add_xprt_release(void *calldata)
2877 struct rpc_cb_add_xprt_calldata *data = calldata;
2879 xprt_put(data->xprt);
2880 xprt_switch_put(data->xps);
2884 static const struct rpc_call_ops rpc_cb_add_xprt_call_ops = {
2885 .rpc_call_prepare = rpc_null_call_prepare,
2886 .rpc_call_done = rpc_cb_add_xprt_done,
2887 .rpc_release = rpc_cb_add_xprt_release,
2891 * rpc_clnt_test_and_add_xprt - Test and add a new transport to a rpc_clnt
2892 * @clnt: pointer to struct rpc_clnt
2893 * @xps: pointer to struct rpc_xprt_switch,
2894 * @xprt: pointer struct rpc_xprt
2895 * @in_max_connect: pointer to the max_connect value for the passed in xprt transport
2897 int rpc_clnt_test_and_add_xprt(struct rpc_clnt *clnt,
2898 struct rpc_xprt_switch *xps, struct rpc_xprt *xprt,
2899 void *in_max_connect)
2901 struct rpc_cb_add_xprt_calldata *data;
2902 struct rpc_task *task;
2903 int max_connect = clnt->cl_max_connect;
2906 max_connect = *(int *)in_max_connect;
2907 if (xps->xps_nunique_destaddr_xprts + 1 > max_connect) {
2909 pr_warn("SUNRPC: reached max allowed number (%d) did not add "
2910 "transport to server: %s\n", max_connect,
2911 rpc_peeraddr2str(clnt, RPC_DISPLAY_ADDR));
2916 data = kmalloc(sizeof(*data), GFP_KERNEL);
2919 data->xps = xprt_switch_get(xps);
2920 data->xprt = xprt_get(xprt);
2921 if (rpc_xprt_switch_has_addr(data->xps, (struct sockaddr *)&xprt->addr)) {
2922 rpc_cb_add_xprt_release(data);
2926 task = rpc_call_null_helper(clnt, xprt, NULL, RPC_TASK_ASYNC,
2927 &rpc_cb_add_xprt_call_ops, data);
2929 return PTR_ERR(task);
2931 data->xps->xps_nunique_destaddr_xprts++;
2936 EXPORT_SYMBOL_GPL(rpc_clnt_test_and_add_xprt);
2938 static int rpc_clnt_add_xprt_helper(struct rpc_clnt *clnt,
2939 struct rpc_xprt *xprt,
2940 struct rpc_add_xprt_test *data)
2942 struct rpc_task *task;
2943 int status = -EADDRINUSE;
2945 /* Test the connection */
2946 task = rpc_call_null_helper(clnt, xprt, NULL, 0, NULL, NULL);
2948 return PTR_ERR(task);
2950 status = task->tk_status;
2956 /* rpc_xprt_switch and rpc_xprt are deferrenced by add_xprt_test() */
2957 data->add_xprt_test(clnt, xprt, data->data);
2963 * rpc_clnt_setup_test_and_add_xprt()
2965 * This is an rpc_clnt_add_xprt setup() function which returns 1 so:
2966 * 1) caller of the test function must dereference the rpc_xprt_switch
2968 * 2) test function must call rpc_xprt_switch_add_xprt, usually in
2969 * the rpc_call_done routine.
2971 * Upon success (return of 1), the test function adds the new
2972 * transport to the rpc_clnt xprt switch
2974 * @clnt: struct rpc_clnt to get the new transport
2975 * @xps: the rpc_xprt_switch to hold the new transport
2976 * @xprt: the rpc_xprt to test
2977 * @data: a struct rpc_add_xprt_test pointer that holds the test function
2978 * and test function call data
2980 int rpc_clnt_setup_test_and_add_xprt(struct rpc_clnt *clnt,
2981 struct rpc_xprt_switch *xps,
2982 struct rpc_xprt *xprt,
2985 int status = -EADDRINUSE;
2987 xprt = xprt_get(xprt);
2988 xprt_switch_get(xps);
2990 if (rpc_xprt_switch_has_addr(xps, (struct sockaddr *)&xprt->addr))
2993 status = rpc_clnt_add_xprt_helper(clnt, xprt, data);
3000 xprt_switch_put(xps);
3002 pr_info("RPC: rpc_clnt_test_xprt failed: %d addr %s not "
3004 xprt->address_strings[RPC_DISPLAY_ADDR]);
3005 /* so that rpc_clnt_add_xprt does not call rpc_xprt_switch_add_xprt */
3008 EXPORT_SYMBOL_GPL(rpc_clnt_setup_test_and_add_xprt);
3011 * rpc_clnt_add_xprt - Add a new transport to a rpc_clnt
3012 * @clnt: pointer to struct rpc_clnt
3013 * @xprtargs: pointer to struct xprt_create
3014 * @setup: callback to test and/or set up the connection
3015 * @data: pointer to setup function data
3017 * Creates a new transport using the parameters set in args and
3019 * If ping is set, then test that connectivity succeeds before
3020 * adding the new transport.
3023 int rpc_clnt_add_xprt(struct rpc_clnt *clnt,
3024 struct xprt_create *xprtargs,
3025 int (*setup)(struct rpc_clnt *,
3026 struct rpc_xprt_switch *,
3031 struct rpc_xprt_switch *xps;
3032 struct rpc_xprt *xprt;
3033 unsigned long connect_timeout;
3034 unsigned long reconnect_timeout;
3035 unsigned char resvport, reuseport;
3039 xps = xprt_switch_get(rcu_dereference(clnt->cl_xpi.xpi_xpswitch));
3040 xprt = xprt_iter_xprt(&clnt->cl_xpi);
3041 if (xps == NULL || xprt == NULL) {
3043 xprt_switch_put(xps);
3046 resvport = xprt->resvport;
3047 reuseport = xprt->reuseport;
3048 connect_timeout = xprt->connect_timeout;
3049 reconnect_timeout = xprt->max_reconnect_timeout;
3050 ident = xprt->xprt_class->ident;
3053 if (!xprtargs->ident)
3054 xprtargs->ident = ident;
3055 xprt = xprt_create_transport(xprtargs);
3057 ret = PTR_ERR(xprt);
3058 goto out_put_switch;
3060 xprt->resvport = resvport;
3061 xprt->reuseport = reuseport;
3062 if (xprt->ops->set_connect_timeout != NULL)
3063 xprt->ops->set_connect_timeout(xprt,
3067 rpc_xprt_switch_set_roundrobin(xps);
3069 ret = setup(clnt, xps, xprt, data);
3073 rpc_xprt_switch_add_xprt(xps, xprt);
3077 xprt_switch_put(xps);
3080 EXPORT_SYMBOL_GPL(rpc_clnt_add_xprt);
3082 static int rpc_xprt_probe_trunked(struct rpc_clnt *clnt,
3083 struct rpc_xprt *xprt,
3084 struct rpc_add_xprt_test *data)
3086 struct rpc_xprt_switch *xps;
3087 struct rpc_xprt *main_xprt;
3093 main_xprt = xprt_get(rcu_dereference(clnt->cl_xprt));
3094 xps = xprt_switch_get(rcu_dereference(clnt->cl_xpi.xpi_xpswitch));
3095 status = rpc_cmp_addr_port((struct sockaddr *)&xprt->addr,
3096 (struct sockaddr *)&main_xprt->addr);
3098 xprt_put(main_xprt);
3099 if (status || !test_bit(XPRT_OFFLINE, &xprt->state))
3102 status = rpc_clnt_add_xprt_helper(clnt, xprt, data);
3105 xprt_switch_put(xps);
3109 /* rpc_clnt_probe_trunked_xprt -- probe offlined transport for session trunking
3110 * @clnt rpc_clnt structure
3112 * For each offlined transport found in the rpc_clnt structure call
3113 * the function rpc_xprt_probe_trunked() which will determine if this
3114 * transport still belongs to the trunking group.
3116 void rpc_clnt_probe_trunked_xprts(struct rpc_clnt *clnt,
3117 struct rpc_add_xprt_test *data)
3119 struct rpc_xprt_iter xpi;
3122 ret = rpc_clnt_xprt_iter_offline_init(clnt, &xpi);
3126 struct rpc_xprt *xprt = xprt_iter_get_next(&xpi);
3130 ret = rpc_xprt_probe_trunked(clnt, xprt, data);
3134 xprt_iter_rewind(&xpi);
3136 xprt_iter_destroy(&xpi);
3138 EXPORT_SYMBOL_GPL(rpc_clnt_probe_trunked_xprts);
3140 static int rpc_xprt_offline(struct rpc_clnt *clnt,
3141 struct rpc_xprt *xprt,
3144 struct rpc_xprt *main_xprt;
3145 struct rpc_xprt_switch *xps;
3151 main_xprt = xprt_get(rcu_dereference(clnt->cl_xprt));
3152 xps = xprt_switch_get(rcu_dereference(clnt->cl_xpi.xpi_xpswitch));
3153 err = rpc_cmp_addr_port((struct sockaddr *)&xprt->addr,
3154 (struct sockaddr *)&main_xprt->addr);
3156 xprt_put(main_xprt);
3160 if (wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_KILLABLE)) {
3164 xprt_set_offline_locked(xprt, xps);
3166 xprt_release_write(xprt, NULL);
3169 xprt_switch_put(xps);
3173 /* rpc_clnt_manage_trunked_xprts -- offline trunked transports
3174 * @clnt rpc_clnt structure
3176 * For each active transport found in the rpc_clnt structure call
3177 * the function rpc_xprt_offline() which will identify trunked transports
3178 * and will mark them offline.
3180 void rpc_clnt_manage_trunked_xprts(struct rpc_clnt *clnt)
3182 rpc_clnt_iterate_for_each_xprt(clnt, rpc_xprt_offline, NULL);
3184 EXPORT_SYMBOL_GPL(rpc_clnt_manage_trunked_xprts);
3186 struct connect_timeout_data {
3187 unsigned long connect_timeout;
3188 unsigned long reconnect_timeout;
3192 rpc_xprt_set_connect_timeout(struct rpc_clnt *clnt,
3193 struct rpc_xprt *xprt,
3196 struct connect_timeout_data *timeo = data;
3198 if (xprt->ops->set_connect_timeout)
3199 xprt->ops->set_connect_timeout(xprt,
3200 timeo->connect_timeout,
3201 timeo->reconnect_timeout);
3206 rpc_set_connect_timeout(struct rpc_clnt *clnt,
3207 unsigned long connect_timeout,
3208 unsigned long reconnect_timeout)
3210 struct connect_timeout_data timeout = {
3211 .connect_timeout = connect_timeout,
3212 .reconnect_timeout = reconnect_timeout,
3214 rpc_clnt_iterate_for_each_xprt(clnt,
3215 rpc_xprt_set_connect_timeout,
3218 EXPORT_SYMBOL_GPL(rpc_set_connect_timeout);
3220 void rpc_clnt_xprt_switch_put(struct rpc_clnt *clnt)
3223 xprt_switch_put(rcu_dereference(clnt->cl_xpi.xpi_xpswitch));
3226 EXPORT_SYMBOL_GPL(rpc_clnt_xprt_switch_put);
3228 void rpc_clnt_xprt_set_online(struct rpc_clnt *clnt, struct rpc_xprt *xprt)
3230 struct rpc_xprt_switch *xps;
3233 xps = rcu_dereference(clnt->cl_xpi.xpi_xpswitch);
3235 xprt_set_online_locked(xprt, xps);
3238 void rpc_clnt_xprt_switch_add_xprt(struct rpc_clnt *clnt, struct rpc_xprt *xprt)
3240 if (rpc_clnt_xprt_switch_has_addr(clnt,
3241 (const struct sockaddr *)&xprt->addr)) {
3242 return rpc_clnt_xprt_set_online(clnt, xprt);
3245 rpc_xprt_switch_add_xprt(rcu_dereference(clnt->cl_xpi.xpi_xpswitch),
3249 EXPORT_SYMBOL_GPL(rpc_clnt_xprt_switch_add_xprt);
3251 void rpc_clnt_xprt_switch_remove_xprt(struct rpc_clnt *clnt, struct rpc_xprt *xprt)
3253 struct rpc_xprt_switch *xps;
3256 xps = rcu_dereference(clnt->cl_xpi.xpi_xpswitch);
3257 rpc_xprt_switch_remove_xprt(rcu_dereference(clnt->cl_xpi.xpi_xpswitch),
3259 xps->xps_nunique_destaddr_xprts--;
3262 EXPORT_SYMBOL_GPL(rpc_clnt_xprt_switch_remove_xprt);
3264 bool rpc_clnt_xprt_switch_has_addr(struct rpc_clnt *clnt,
3265 const struct sockaddr *sap)
3267 struct rpc_xprt_switch *xps;
3271 xps = rcu_dereference(clnt->cl_xpi.xpi_xpswitch);
3272 ret = rpc_xprt_switch_has_addr(xps, sap);
3276 EXPORT_SYMBOL_GPL(rpc_clnt_xprt_switch_has_addr);
3278 #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
3279 static void rpc_show_header(void)
3281 printk(KERN_INFO "-pid- flgs status -client- --rqstp- "
3282 "-timeout ---ops--\n");
3285 static void rpc_show_task(const struct rpc_clnt *clnt,
3286 const struct rpc_task *task)
3288 const char *rpc_waitq = "none";
3290 if (RPC_IS_QUEUED(task))
3291 rpc_waitq = rpc_qname(task->tk_waitqueue);
3293 printk(KERN_INFO "%5u %04x %6d %8p %8p %8ld %8p %sv%u %s a:%ps q:%s\n",
3294 task->tk_pid, task->tk_flags, task->tk_status,
3295 clnt, task->tk_rqstp, rpc_task_timeout(task), task->tk_ops,
3296 clnt->cl_program->name, clnt->cl_vers, rpc_proc_name(task),
3297 task->tk_action, rpc_waitq);
3300 void rpc_show_tasks(struct net *net)
3302 struct rpc_clnt *clnt;
3303 struct rpc_task *task;
3305 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
3307 spin_lock(&sn->rpc_client_lock);
3308 list_for_each_entry(clnt, &sn->all_clients, cl_clients) {
3309 spin_lock(&clnt->cl_lock);
3310 list_for_each_entry(task, &clnt->cl_tasks, tk_task) {
3315 rpc_show_task(clnt, task);
3317 spin_unlock(&clnt->cl_lock);
3319 spin_unlock(&sn->rpc_client_lock);
3323 #if IS_ENABLED(CONFIG_SUNRPC_SWAP)
3325 rpc_clnt_swap_activate_callback(struct rpc_clnt *clnt,
3326 struct rpc_xprt *xprt,
3329 return xprt_enable_swap(xprt);
3333 rpc_clnt_swap_activate(struct rpc_clnt *clnt)
3335 while (clnt != clnt->cl_parent)
3336 clnt = clnt->cl_parent;
3337 if (atomic_inc_return(&clnt->cl_swapper) == 1)
3338 return rpc_clnt_iterate_for_each_xprt(clnt,
3339 rpc_clnt_swap_activate_callback, NULL);
3342 EXPORT_SYMBOL_GPL(rpc_clnt_swap_activate);
3345 rpc_clnt_swap_deactivate_callback(struct rpc_clnt *clnt,
3346 struct rpc_xprt *xprt,
3349 xprt_disable_swap(xprt);
3354 rpc_clnt_swap_deactivate(struct rpc_clnt *clnt)
3356 while (clnt != clnt->cl_parent)
3357 clnt = clnt->cl_parent;
3358 if (atomic_dec_if_positive(&clnt->cl_swapper) == 0)
3359 rpc_clnt_iterate_for_each_xprt(clnt,
3360 rpc_clnt_swap_deactivate_callback, NULL);
3362 EXPORT_SYMBOL_GPL(rpc_clnt_swap_deactivate);
3363 #endif /* CONFIG_SUNRPC_SWAP */