1 // SPDX-License-Identifier: GPL-2.0-only
3 * linux/net/sunrpc/clnt.c
5 * This file contains the high-level RPC interface.
6 * It is modeled as a finite state machine to support both synchronous
7 * and asynchronous requests.
9 * - RPC header generation and argument serialization.
10 * - Credential refresh.
11 * - TCP connect handling.
12 * - Retry of operation when it is suspected the operation failed because
13 * of uid squashing on the server, or when the credentials were stale
14 * and need to be refreshed, or when a packet was damaged in transit.
15 * This may be have to be moved to the VFS layer.
22 #include <linux/module.h>
23 #include <linux/types.h>
24 #include <linux/kallsyms.h>
26 #include <linux/namei.h>
27 #include <linux/mount.h>
28 #include <linux/slab.h>
29 #include <linux/rcupdate.h>
30 #include <linux/utsname.h>
31 #include <linux/workqueue.h>
33 #include <linux/in6.h>
36 #include <linux/sunrpc/clnt.h>
37 #include <linux/sunrpc/addr.h>
38 #include <linux/sunrpc/rpc_pipe_fs.h>
39 #include <linux/sunrpc/metrics.h>
40 #include <linux/sunrpc/bc_xprt.h>
41 #include <trace/events/sunrpc.h>
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;
391 clnt->cl_xprtsec = args->xprtsec;
393 err = rpc_alloc_clid(clnt);
397 clnt->cl_cred = get_cred(args->cred);
398 clnt->cl_procinfo = version->procs;
399 clnt->cl_maxproc = version->nrprocs;
400 clnt->cl_prog = args->prognumber ? : program->number;
401 clnt->cl_vers = version->number;
402 clnt->cl_stats = program->stats;
403 clnt->cl_metrics = rpc_alloc_iostats(clnt);
404 rpc_init_pipe_dir_head(&clnt->cl_pipedir_objects);
406 if (clnt->cl_metrics == NULL)
408 clnt->cl_program = program;
409 INIT_LIST_HEAD(&clnt->cl_tasks);
410 spin_lock_init(&clnt->cl_lock);
412 timeout = xprt->timeout;
413 if (args->timeout != NULL) {
414 memcpy(&clnt->cl_timeout_default, args->timeout,
415 sizeof(clnt->cl_timeout_default));
416 timeout = &clnt->cl_timeout_default;
419 rpc_clnt_set_transport(clnt, xprt, timeout);
421 xprt_iter_init(&clnt->cl_xpi, xps);
422 xprt_switch_put(xps);
424 clnt->cl_rtt = &clnt->cl_rtt_default;
425 rpc_init_rtt(&clnt->cl_rtt_default, clnt->cl_timeout->to_initval);
427 refcount_set(&clnt->cl_count, 1);
429 if (nodename == NULL)
430 nodename = utsname()->nodename;
431 /* save the nodename */
432 rpc_clnt_set_nodename(clnt, nodename);
434 rpc_sysfs_client_setup(clnt, xps, rpc_net_ns(clnt));
435 err = rpc_client_register(clnt, args->authflavor, args->client_name);
439 refcount_inc(&parent->cl_count);
441 trace_rpc_clnt_new(clnt, xprt, args);
445 rpc_free_iostats(clnt->cl_metrics);
447 put_cred(clnt->cl_cred);
454 xprt_switch_put(xps);
456 trace_rpc_clnt_new_err(program->name, args->servername, err);
460 static struct rpc_clnt *rpc_create_xprt(struct rpc_create_args *args,
461 struct rpc_xprt *xprt)
463 struct rpc_clnt *clnt = NULL;
464 struct rpc_xprt_switch *xps;
466 if (args->bc_xprt && args->bc_xprt->xpt_bc_xps) {
467 WARN_ON_ONCE(!(args->protocol & XPRT_TRANSPORT_BC));
468 xps = args->bc_xprt->xpt_bc_xps;
469 xprt_switch_get(xps);
471 xps = xprt_switch_alloc(xprt, GFP_KERNEL);
474 return ERR_PTR(-ENOMEM);
477 xprt_switch_get(xps);
478 xprt->bc_xprt->xpt_bc_xps = xps;
481 clnt = rpc_new_client(args, xps, xprt, NULL);
485 if (!(args->flags & RPC_CLNT_CREATE_NOPING)) {
486 int err = rpc_ping(clnt);
488 rpc_shutdown_client(clnt);
491 } else if (args->flags & RPC_CLNT_CREATE_CONNECTED) {
492 int err = rpc_ping_noreply(clnt);
494 rpc_shutdown_client(clnt);
499 clnt->cl_softrtry = 1;
500 if (args->flags & (RPC_CLNT_CREATE_HARDRTRY|RPC_CLNT_CREATE_SOFTERR)) {
501 clnt->cl_softrtry = 0;
502 if (args->flags & RPC_CLNT_CREATE_SOFTERR)
503 clnt->cl_softerr = 1;
506 if (args->flags & RPC_CLNT_CREATE_AUTOBIND)
507 clnt->cl_autobind = 1;
508 if (args->flags & RPC_CLNT_CREATE_NO_RETRANS_TIMEOUT)
509 clnt->cl_noretranstimeo = 1;
510 if (args->flags & RPC_CLNT_CREATE_DISCRTRY)
511 clnt->cl_discrtry = 1;
512 if (!(args->flags & RPC_CLNT_CREATE_QUIET))
519 * rpc_create - create an RPC client and transport with one call
520 * @args: rpc_clnt create argument structure
522 * Creates and initializes an RPC transport and an RPC client.
524 * It can ping the server in order to determine if it is up, and to see if
525 * it supports this program and version. RPC_CLNT_CREATE_NOPING disables
526 * this behavior so asynchronous tasks can also use rpc_create.
528 struct rpc_clnt *rpc_create(struct rpc_create_args *args)
530 struct rpc_xprt *xprt;
531 struct xprt_create xprtargs = {
533 .ident = args->protocol,
534 .srcaddr = args->saddress,
535 .dstaddr = args->address,
536 .addrlen = args->addrsize,
537 .servername = args->servername,
538 .bc_xprt = args->bc_xprt,
539 .xprtsec = args->xprtsec,
540 .connect_timeout = args->connect_timeout,
541 .reconnect_timeout = args->reconnect_timeout,
544 struct rpc_clnt *clnt;
548 WARN_ON_ONCE(!(args->protocol & XPRT_TRANSPORT_BC));
549 xprt = args->bc_xprt->xpt_bc_xprt;
552 return rpc_create_xprt(args, xprt);
556 if (args->flags & RPC_CLNT_CREATE_INFINITE_SLOTS)
557 xprtargs.flags |= XPRT_CREATE_INFINITE_SLOTS;
558 if (args->flags & RPC_CLNT_CREATE_NO_IDLE_TIMEOUT)
559 xprtargs.flags |= XPRT_CREATE_NO_IDLE_TIMEOUT;
561 * If the caller chooses not to specify a hostname, whip
562 * up a string representation of the passed-in address.
564 if (xprtargs.servername == NULL) {
565 struct sockaddr_un *sun =
566 (struct sockaddr_un *)args->address;
567 struct sockaddr_in *sin =
568 (struct sockaddr_in *)args->address;
569 struct sockaddr_in6 *sin6 =
570 (struct sockaddr_in6 *)args->address;
572 servername[0] = '\0';
573 switch (args->address->sa_family) {
575 if (sun->sun_path[0])
576 snprintf(servername, sizeof(servername), "%s",
579 snprintf(servername, sizeof(servername), "@%s",
583 snprintf(servername, sizeof(servername), "%pI4",
584 &sin->sin_addr.s_addr);
587 snprintf(servername, sizeof(servername), "%pI6",
591 /* caller wants default server name, but
592 * address family isn't recognized. */
593 return ERR_PTR(-EINVAL);
595 xprtargs.servername = servername;
598 xprt = xprt_create_transport(&xprtargs);
600 return (struct rpc_clnt *)xprt;
603 * By default, kernel RPC client connects from a reserved port.
604 * CAP_NET_BIND_SERVICE will not be set for unprivileged requesters,
605 * but it is always enabled for rpciod, which handles the connect
609 if (args->flags & RPC_CLNT_CREATE_NONPRIVPORT)
612 if (args->flags & RPC_CLNT_CREATE_REUSEPORT)
615 clnt = rpc_create_xprt(args, xprt);
616 if (IS_ERR(clnt) || args->nconnect <= 1)
619 for (i = 0; i < args->nconnect - 1; i++) {
620 if (rpc_clnt_add_xprt(clnt, &xprtargs, NULL, NULL) < 0)
625 EXPORT_SYMBOL_GPL(rpc_create);
628 * This function clones the RPC client structure. It allows us to share the
629 * same transport while varying parameters such as the authentication
632 static struct rpc_clnt *__rpc_clone_client(struct rpc_create_args *args,
633 struct rpc_clnt *clnt)
635 struct rpc_xprt_switch *xps;
636 struct rpc_xprt *xprt;
637 struct rpc_clnt *new;
642 xprt = xprt_get(rcu_dereference(clnt->cl_xprt));
643 xps = xprt_switch_get(rcu_dereference(clnt->cl_xpi.xpi_xpswitch));
645 if (xprt == NULL || xps == NULL) {
647 xprt_switch_put(xps);
650 args->servername = xprt->servername;
651 args->nodename = clnt->cl_nodename;
653 new = rpc_new_client(args, xps, xprt, clnt);
657 /* Turn off autobind on clones */
658 new->cl_autobind = 0;
659 new->cl_softrtry = clnt->cl_softrtry;
660 new->cl_softerr = clnt->cl_softerr;
661 new->cl_noretranstimeo = clnt->cl_noretranstimeo;
662 new->cl_discrtry = clnt->cl_discrtry;
663 new->cl_chatty = clnt->cl_chatty;
664 new->cl_principal = clnt->cl_principal;
665 new->cl_max_connect = clnt->cl_max_connect;
669 trace_rpc_clnt_clone_err(clnt, err);
674 * rpc_clone_client - Clone an RPC client structure
676 * @clnt: RPC client whose parameters are copied
678 * Returns a fresh RPC client or an ERR_PTR.
680 struct rpc_clnt *rpc_clone_client(struct rpc_clnt *clnt)
682 struct rpc_create_args args = {
683 .program = clnt->cl_program,
684 .prognumber = clnt->cl_prog,
685 .version = clnt->cl_vers,
686 .authflavor = clnt->cl_auth->au_flavor,
687 .cred = clnt->cl_cred,
689 return __rpc_clone_client(&args, clnt);
691 EXPORT_SYMBOL_GPL(rpc_clone_client);
694 * rpc_clone_client_set_auth - Clone an RPC client structure and set its auth
696 * @clnt: RPC client whose parameters are copied
697 * @flavor: security flavor for new client
699 * Returns a fresh RPC client or an ERR_PTR.
702 rpc_clone_client_set_auth(struct rpc_clnt *clnt, rpc_authflavor_t flavor)
704 struct rpc_create_args args = {
705 .program = clnt->cl_program,
706 .prognumber = clnt->cl_prog,
707 .version = clnt->cl_vers,
708 .authflavor = flavor,
709 .cred = clnt->cl_cred,
711 return __rpc_clone_client(&args, clnt);
713 EXPORT_SYMBOL_GPL(rpc_clone_client_set_auth);
716 * rpc_switch_client_transport: switch the RPC transport on the fly
717 * @clnt: pointer to a struct rpc_clnt
718 * @args: pointer to the new transport arguments
719 * @timeout: pointer to the new timeout parameters
721 * This function allows the caller to switch the RPC transport for the
722 * rpc_clnt structure 'clnt' to allow it to connect to a mirrored NFS
723 * server, for instance. It assumes that the caller has ensured that
724 * there are no active RPC tasks by using some form of locking.
726 * Returns zero if "clnt" is now using the new xprt. Otherwise a
727 * negative errno is returned, and "clnt" continues to use the old
730 int rpc_switch_client_transport(struct rpc_clnt *clnt,
731 struct xprt_create *args,
732 const struct rpc_timeout *timeout)
734 const struct rpc_timeout *old_timeo;
735 rpc_authflavor_t pseudoflavor;
736 struct rpc_xprt_switch *xps, *oldxps;
737 struct rpc_xprt *xprt, *old;
738 struct rpc_clnt *parent;
741 args->xprtsec = clnt->cl_xprtsec;
742 xprt = xprt_create_transport(args);
744 return PTR_ERR(xprt);
746 xps = xprt_switch_alloc(xprt, GFP_KERNEL);
752 pseudoflavor = clnt->cl_auth->au_flavor;
754 old_timeo = clnt->cl_timeout;
755 old = rpc_clnt_set_transport(clnt, xprt, timeout);
756 oldxps = xprt_iter_xchg_switch(&clnt->cl_xpi, xps);
758 rpc_unregister_client(clnt);
759 __rpc_clnt_remove_pipedir(clnt);
760 rpc_sysfs_client_destroy(clnt);
761 rpc_clnt_debugfs_unregister(clnt);
764 * A new transport was created. "clnt" therefore
765 * becomes the root of a new cl_parent tree. clnt's
766 * children, if it has any, still point to the old xprt.
768 parent = clnt->cl_parent;
769 clnt->cl_parent = clnt;
772 * The old rpc_auth cache cannot be re-used. GSS
773 * contexts in particular are between a single
776 err = rpc_client_register(clnt, pseudoflavor, NULL);
782 rpc_release_client(parent);
783 xprt_switch_put(oldxps);
785 trace_rpc_clnt_replace_xprt(clnt);
789 xps = xprt_iter_xchg_switch(&clnt->cl_xpi, oldxps);
790 rpc_clnt_set_transport(clnt, old, old_timeo);
791 clnt->cl_parent = parent;
792 rpc_client_register(clnt, pseudoflavor, NULL);
793 xprt_switch_put(xps);
795 trace_rpc_clnt_replace_xprt_err(clnt);
798 EXPORT_SYMBOL_GPL(rpc_switch_client_transport);
801 int _rpc_clnt_xprt_iter_init(struct rpc_clnt *clnt, struct rpc_xprt_iter *xpi,
802 void func(struct rpc_xprt_iter *xpi, struct rpc_xprt_switch *xps))
804 struct rpc_xprt_switch *xps;
807 xps = xprt_switch_get(rcu_dereference(clnt->cl_xpi.xpi_xpswitch));
812 xprt_switch_put(xps);
817 int rpc_clnt_xprt_iter_init(struct rpc_clnt *clnt, struct rpc_xprt_iter *xpi)
819 return _rpc_clnt_xprt_iter_init(clnt, xpi, xprt_iter_init_listall);
823 int rpc_clnt_xprt_iter_offline_init(struct rpc_clnt *clnt,
824 struct rpc_xprt_iter *xpi)
826 return _rpc_clnt_xprt_iter_init(clnt, xpi, xprt_iter_init_listoffline);
830 * rpc_clnt_iterate_for_each_xprt - Apply a function to all transports
831 * @clnt: pointer to client
832 * @fn: function to apply
833 * @data: void pointer to function data
835 * Iterates through the list of RPC transports currently attached to the
836 * client and applies the function fn(clnt, xprt, data).
838 * On error, the iteration stops, and the function returns the error value.
840 int rpc_clnt_iterate_for_each_xprt(struct rpc_clnt *clnt,
841 int (*fn)(struct rpc_clnt *, struct rpc_xprt *, void *),
844 struct rpc_xprt_iter xpi;
847 ret = rpc_clnt_xprt_iter_init(clnt, &xpi);
851 struct rpc_xprt *xprt = xprt_iter_get_next(&xpi);
855 ret = fn(clnt, xprt, data);
860 xprt_iter_destroy(&xpi);
863 EXPORT_SYMBOL_GPL(rpc_clnt_iterate_for_each_xprt);
866 * Kill all tasks for the given client.
867 * XXX: kill their descendants as well?
869 void rpc_killall_tasks(struct rpc_clnt *clnt)
871 struct rpc_task *rovr;
874 if (list_empty(&clnt->cl_tasks))
878 * Spin lock all_tasks to prevent changes...
880 trace_rpc_clnt_killall(clnt);
881 spin_lock(&clnt->cl_lock);
882 list_for_each_entry(rovr, &clnt->cl_tasks, tk_task)
883 rpc_signal_task(rovr);
884 spin_unlock(&clnt->cl_lock);
886 EXPORT_SYMBOL_GPL(rpc_killall_tasks);
889 * rpc_cancel_tasks - try to cancel a set of RPC tasks
890 * @clnt: Pointer to RPC client
891 * @error: RPC task error value to set
892 * @fnmatch: Pointer to selector function
895 * Uses @fnmatch to define a set of RPC tasks that are to be cancelled.
896 * The argument @error must be a negative error value.
898 unsigned long rpc_cancel_tasks(struct rpc_clnt *clnt, int error,
899 bool (*fnmatch)(const struct rpc_task *,
903 struct rpc_task *task;
904 unsigned long count = 0;
906 if (list_empty(&clnt->cl_tasks))
909 * Spin lock all_tasks to prevent changes...
911 spin_lock(&clnt->cl_lock);
912 list_for_each_entry(task, &clnt->cl_tasks, tk_task) {
913 if (!RPC_IS_ACTIVATED(task))
915 if (!fnmatch(task, data))
917 rpc_task_try_cancel(task, error);
920 spin_unlock(&clnt->cl_lock);
923 EXPORT_SYMBOL_GPL(rpc_cancel_tasks);
925 static int rpc_clnt_disconnect_xprt(struct rpc_clnt *clnt,
926 struct rpc_xprt *xprt, void *dummy)
928 if (xprt_connected(xprt))
929 xprt_force_disconnect(xprt);
933 void rpc_clnt_disconnect(struct rpc_clnt *clnt)
935 rpc_clnt_iterate_for_each_xprt(clnt, rpc_clnt_disconnect_xprt, NULL);
937 EXPORT_SYMBOL_GPL(rpc_clnt_disconnect);
940 * Properly shut down an RPC client, terminating all outstanding
943 void rpc_shutdown_client(struct rpc_clnt *clnt)
947 trace_rpc_clnt_shutdown(clnt);
949 while (!list_empty(&clnt->cl_tasks)) {
950 rpc_killall_tasks(clnt);
951 wait_event_timeout(destroy_wait,
952 list_empty(&clnt->cl_tasks), 1*HZ);
955 rpc_release_client(clnt);
957 EXPORT_SYMBOL_GPL(rpc_shutdown_client);
962 static void rpc_free_client_work(struct work_struct *work)
964 struct rpc_clnt *clnt = container_of(work, struct rpc_clnt, cl_work);
966 trace_rpc_clnt_free(clnt);
968 /* These might block on processes that might allocate memory,
969 * so they cannot be called in rpciod, so they are handled separately
972 rpc_sysfs_client_destroy(clnt);
973 rpc_clnt_debugfs_unregister(clnt);
975 rpc_clnt_remove_pipedir(clnt);
976 xprt_put(rcu_dereference_raw(clnt->cl_xprt));
981 static struct rpc_clnt *
982 rpc_free_client(struct rpc_clnt *clnt)
984 struct rpc_clnt *parent = NULL;
986 trace_rpc_clnt_release(clnt);
987 if (clnt->cl_parent != clnt)
988 parent = clnt->cl_parent;
989 rpc_unregister_client(clnt);
990 rpc_free_iostats(clnt->cl_metrics);
991 clnt->cl_metrics = NULL;
992 xprt_iter_destroy(&clnt->cl_xpi);
993 put_cred(clnt->cl_cred);
995 INIT_WORK(&clnt->cl_work, rpc_free_client_work);
996 schedule_work(&clnt->cl_work);
1001 * Free an RPC client
1003 static struct rpc_clnt *
1004 rpc_free_auth(struct rpc_clnt *clnt)
1007 * Note: RPCSEC_GSS may need to send NULL RPC calls in order to
1008 * release remaining GSS contexts. This mechanism ensures
1009 * that it can do so safely.
1011 if (clnt->cl_auth != NULL) {
1012 rpcauth_release(clnt->cl_auth);
1013 clnt->cl_auth = NULL;
1015 if (refcount_dec_and_test(&clnt->cl_count))
1016 return rpc_free_client(clnt);
1021 * Release reference to the RPC client
1024 rpc_release_client(struct rpc_clnt *clnt)
1027 if (list_empty(&clnt->cl_tasks))
1028 wake_up(&destroy_wait);
1029 if (refcount_dec_not_one(&clnt->cl_count))
1031 clnt = rpc_free_auth(clnt);
1032 } while (clnt != NULL);
1034 EXPORT_SYMBOL_GPL(rpc_release_client);
1037 * rpc_bind_new_program - bind a new RPC program to an existing client
1038 * @old: old rpc_client
1039 * @program: rpc program to set
1040 * @vers: rpc program version
1042 * Clones the rpc client and sets up a new RPC program. This is mainly
1043 * of use for enabling different RPC programs to share the same transport.
1044 * The Sun NFSv2/v3 ACL protocol can do this.
1046 struct rpc_clnt *rpc_bind_new_program(struct rpc_clnt *old,
1047 const struct rpc_program *program,
1050 struct rpc_create_args args = {
1052 .prognumber = program->number,
1054 .authflavor = old->cl_auth->au_flavor,
1055 .cred = old->cl_cred,
1057 struct rpc_clnt *clnt;
1060 clnt = __rpc_clone_client(&args, old);
1063 err = rpc_ping(clnt);
1065 rpc_shutdown_client(clnt);
1066 clnt = ERR_PTR(err);
1071 EXPORT_SYMBOL_GPL(rpc_bind_new_program);
1074 rpc_task_get_xprt(struct rpc_clnt *clnt, struct rpc_xprt *xprt)
1076 struct rpc_xprt_switch *xps;
1081 xps = rcu_dereference(clnt->cl_xpi.xpi_xpswitch);
1082 atomic_long_inc(&xps->xps_queuelen);
1084 atomic_long_inc(&xprt->queuelen);
1090 rpc_task_release_xprt(struct rpc_clnt *clnt, struct rpc_xprt *xprt)
1092 struct rpc_xprt_switch *xps;
1094 atomic_long_dec(&xprt->queuelen);
1096 xps = rcu_dereference(clnt->cl_xpi.xpi_xpswitch);
1097 atomic_long_dec(&xps->xps_queuelen);
1103 void rpc_task_release_transport(struct rpc_task *task)
1105 struct rpc_xprt *xprt = task->tk_xprt;
1108 task->tk_xprt = NULL;
1109 if (task->tk_client)
1110 rpc_task_release_xprt(task->tk_client, xprt);
1115 EXPORT_SYMBOL_GPL(rpc_task_release_transport);
1117 void rpc_task_release_client(struct rpc_task *task)
1119 struct rpc_clnt *clnt = task->tk_client;
1121 rpc_task_release_transport(task);
1123 /* Remove from client task list */
1124 spin_lock(&clnt->cl_lock);
1125 list_del(&task->tk_task);
1126 spin_unlock(&clnt->cl_lock);
1127 task->tk_client = NULL;
1129 rpc_release_client(clnt);
1133 static struct rpc_xprt *
1134 rpc_task_get_first_xprt(struct rpc_clnt *clnt)
1136 struct rpc_xprt *xprt;
1139 xprt = xprt_get(rcu_dereference(clnt->cl_xprt));
1141 return rpc_task_get_xprt(clnt, xprt);
1144 static struct rpc_xprt *
1145 rpc_task_get_next_xprt(struct rpc_clnt *clnt)
1147 return rpc_task_get_xprt(clnt, xprt_iter_get_next(&clnt->cl_xpi));
1151 void rpc_task_set_transport(struct rpc_task *task, struct rpc_clnt *clnt)
1153 if (task->tk_xprt) {
1154 if (!(test_bit(XPRT_OFFLINE, &task->tk_xprt->state) &&
1155 (task->tk_flags & RPC_TASK_MOVEABLE)))
1158 xprt_put(task->tk_xprt);
1160 if (task->tk_flags & RPC_TASK_NO_ROUND_ROBIN)
1161 task->tk_xprt = rpc_task_get_first_xprt(clnt);
1163 task->tk_xprt = rpc_task_get_next_xprt(clnt);
1167 void rpc_task_set_client(struct rpc_task *task, struct rpc_clnt *clnt)
1169 rpc_task_set_transport(task, clnt);
1170 task->tk_client = clnt;
1171 refcount_inc(&clnt->cl_count);
1172 if (clnt->cl_softrtry)
1173 task->tk_flags |= RPC_TASK_SOFT;
1174 if (clnt->cl_softerr)
1175 task->tk_flags |= RPC_TASK_TIMEOUT;
1176 if (clnt->cl_noretranstimeo)
1177 task->tk_flags |= RPC_TASK_NO_RETRANS_TIMEOUT;
1178 /* Add to the client's list of all tasks */
1179 spin_lock(&clnt->cl_lock);
1180 list_add_tail(&task->tk_task, &clnt->cl_tasks);
1181 spin_unlock(&clnt->cl_lock);
1185 rpc_task_set_rpc_message(struct rpc_task *task, const struct rpc_message *msg)
1188 task->tk_msg.rpc_proc = msg->rpc_proc;
1189 task->tk_msg.rpc_argp = msg->rpc_argp;
1190 task->tk_msg.rpc_resp = msg->rpc_resp;
1191 task->tk_msg.rpc_cred = msg->rpc_cred;
1192 if (!(task->tk_flags & RPC_TASK_CRED_NOREF))
1193 get_cred(task->tk_msg.rpc_cred);
1198 * Default callback for async RPC calls
1201 rpc_default_callback(struct rpc_task *task, void *data)
1205 static const struct rpc_call_ops rpc_default_ops = {
1206 .rpc_call_done = rpc_default_callback,
1210 * rpc_run_task - Allocate a new RPC task, then run rpc_execute against it
1211 * @task_setup_data: pointer to task initialisation data
1213 struct rpc_task *rpc_run_task(const struct rpc_task_setup *task_setup_data)
1215 struct rpc_task *task;
1217 task = rpc_new_task(task_setup_data);
1221 if (!RPC_IS_ASYNC(task))
1222 task->tk_flags |= RPC_TASK_CRED_NOREF;
1224 rpc_task_set_client(task, task_setup_data->rpc_client);
1225 rpc_task_set_rpc_message(task, task_setup_data->rpc_message);
1227 if (task->tk_action == NULL)
1228 rpc_call_start(task);
1230 atomic_inc(&task->tk_count);
1234 EXPORT_SYMBOL_GPL(rpc_run_task);
1237 * rpc_call_sync - Perform a synchronous RPC call
1238 * @clnt: pointer to RPC client
1239 * @msg: RPC call parameters
1240 * @flags: RPC call flags
1242 int rpc_call_sync(struct rpc_clnt *clnt, const struct rpc_message *msg, int flags)
1244 struct rpc_task *task;
1245 struct rpc_task_setup task_setup_data = {
1248 .callback_ops = &rpc_default_ops,
1253 WARN_ON_ONCE(flags & RPC_TASK_ASYNC);
1254 if (flags & RPC_TASK_ASYNC) {
1255 rpc_release_calldata(task_setup_data.callback_ops,
1256 task_setup_data.callback_data);
1260 task = rpc_run_task(&task_setup_data);
1262 return PTR_ERR(task);
1263 status = task->tk_status;
1267 EXPORT_SYMBOL_GPL(rpc_call_sync);
1270 * rpc_call_async - Perform an asynchronous RPC call
1271 * @clnt: pointer to RPC client
1272 * @msg: RPC call parameters
1273 * @flags: RPC call flags
1274 * @tk_ops: RPC call ops
1275 * @data: user call data
1278 rpc_call_async(struct rpc_clnt *clnt, const struct rpc_message *msg, int flags,
1279 const struct rpc_call_ops *tk_ops, void *data)
1281 struct rpc_task *task;
1282 struct rpc_task_setup task_setup_data = {
1285 .callback_ops = tk_ops,
1286 .callback_data = data,
1287 .flags = flags|RPC_TASK_ASYNC,
1290 task = rpc_run_task(&task_setup_data);
1292 return PTR_ERR(task);
1296 EXPORT_SYMBOL_GPL(rpc_call_async);
1298 #if defined(CONFIG_SUNRPC_BACKCHANNEL)
1299 static void call_bc_encode(struct rpc_task *task);
1302 * rpc_run_bc_task - Allocate a new RPC task for backchannel use, then run
1303 * rpc_execute against it
1306 struct rpc_task *rpc_run_bc_task(struct rpc_rqst *req)
1308 struct rpc_task *task;
1309 struct rpc_task_setup task_setup_data = {
1310 .callback_ops = &rpc_default_ops,
1311 .flags = RPC_TASK_SOFTCONN |
1312 RPC_TASK_NO_RETRANS_TIMEOUT,
1315 dprintk("RPC: rpc_run_bc_task req= %p\n", req);
1317 * Create an rpc_task to send the data
1319 task = rpc_new_task(&task_setup_data);
1321 xprt_free_bc_request(req);
1325 xprt_init_bc_request(req, task);
1327 task->tk_action = call_bc_encode;
1328 atomic_inc(&task->tk_count);
1329 WARN_ON_ONCE(atomic_read(&task->tk_count) != 2);
1332 dprintk("RPC: rpc_run_bc_task: task= %p\n", task);
1335 #endif /* CONFIG_SUNRPC_BACKCHANNEL */
1338 * rpc_prepare_reply_pages - Prepare to receive a reply data payload into pages
1339 * @req: RPC request to prepare
1340 * @pages: vector of struct page pointers
1341 * @base: offset in first page where receive should start, in bytes
1342 * @len: expected size of the upper layer data payload, in bytes
1343 * @hdrsize: expected size of upper layer reply header, in XDR words
1346 void rpc_prepare_reply_pages(struct rpc_rqst *req, struct page **pages,
1347 unsigned int base, unsigned int len,
1348 unsigned int hdrsize)
1350 hdrsize += RPC_REPHDRSIZE + req->rq_cred->cr_auth->au_ralign;
1352 xdr_inline_pages(&req->rq_rcv_buf, hdrsize << 2, pages, base, len);
1353 trace_rpc_xdr_reply_pages(req->rq_task, &req->rq_rcv_buf);
1355 EXPORT_SYMBOL_GPL(rpc_prepare_reply_pages);
1358 rpc_call_start(struct rpc_task *task)
1360 task->tk_action = call_start;
1362 EXPORT_SYMBOL_GPL(rpc_call_start);
1365 * rpc_peeraddr - extract remote peer address from clnt's xprt
1366 * @clnt: RPC client structure
1367 * @buf: target buffer
1368 * @bufsize: length of target buffer
1370 * Returns the number of bytes that are actually in the stored address.
1372 size_t rpc_peeraddr(struct rpc_clnt *clnt, struct sockaddr *buf, size_t bufsize)
1375 struct rpc_xprt *xprt;
1378 xprt = rcu_dereference(clnt->cl_xprt);
1380 bytes = xprt->addrlen;
1381 if (bytes > bufsize)
1383 memcpy(buf, &xprt->addr, bytes);
1388 EXPORT_SYMBOL_GPL(rpc_peeraddr);
1391 * rpc_peeraddr2str - return remote peer address in printable format
1392 * @clnt: RPC client structure
1393 * @format: address format
1395 * NB: the lifetime of the memory referenced by the returned pointer is
1396 * the same as the rpc_xprt itself. As long as the caller uses this
1397 * pointer, it must hold the RCU read lock.
1399 const char *rpc_peeraddr2str(struct rpc_clnt *clnt,
1400 enum rpc_display_format_t format)
1402 struct rpc_xprt *xprt;
1404 xprt = rcu_dereference(clnt->cl_xprt);
1406 if (xprt->address_strings[format] != NULL)
1407 return xprt->address_strings[format];
1409 return "unprintable";
1411 EXPORT_SYMBOL_GPL(rpc_peeraddr2str);
1413 static const struct sockaddr_in rpc_inaddr_loopback = {
1414 .sin_family = AF_INET,
1415 .sin_addr.s_addr = htonl(INADDR_ANY),
1418 static const struct sockaddr_in6 rpc_in6addr_loopback = {
1419 .sin6_family = AF_INET6,
1420 .sin6_addr = IN6ADDR_ANY_INIT,
1424 * Try a getsockname() on a connected datagram socket. Using a
1425 * connected datagram socket prevents leaving a socket in TIME_WAIT.
1426 * This conserves the ephemeral port number space.
1428 * Returns zero and fills in "buf" if successful; otherwise, a
1429 * negative errno is returned.
1431 static int rpc_sockname(struct net *net, struct sockaddr *sap, size_t salen,
1432 struct sockaddr *buf)
1434 struct socket *sock;
1437 err = __sock_create(net, sap->sa_family,
1438 SOCK_DGRAM, IPPROTO_UDP, &sock, 1);
1440 dprintk("RPC: can't create UDP socket (%d)\n", err);
1444 switch (sap->sa_family) {
1446 err = kernel_bind(sock,
1447 (struct sockaddr *)&rpc_inaddr_loopback,
1448 sizeof(rpc_inaddr_loopback));
1451 err = kernel_bind(sock,
1452 (struct sockaddr *)&rpc_in6addr_loopback,
1453 sizeof(rpc_in6addr_loopback));
1456 err = -EAFNOSUPPORT;
1460 dprintk("RPC: can't bind UDP socket (%d)\n", err);
1464 err = kernel_connect(sock, sap, salen, 0);
1466 dprintk("RPC: can't connect UDP socket (%d)\n", err);
1470 err = kernel_getsockname(sock, buf);
1472 dprintk("RPC: getsockname failed (%d)\n", err);
1477 if (buf->sa_family == AF_INET6) {
1478 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)buf;
1479 sin6->sin6_scope_id = 0;
1481 dprintk("RPC: %s succeeded\n", __func__);
1490 * Scraping a connected socket failed, so we don't have a useable
1491 * local address. Fallback: generate an address that will prevent
1492 * the server from calling us back.
1494 * Returns zero and fills in "buf" if successful; otherwise, a
1495 * negative errno is returned.
1497 static int rpc_anyaddr(int family, struct sockaddr *buf, size_t buflen)
1501 if (buflen < sizeof(rpc_inaddr_loopback))
1503 memcpy(buf, &rpc_inaddr_loopback,
1504 sizeof(rpc_inaddr_loopback));
1507 if (buflen < sizeof(rpc_in6addr_loopback))
1509 memcpy(buf, &rpc_in6addr_loopback,
1510 sizeof(rpc_in6addr_loopback));
1513 dprintk("RPC: %s: address family not supported\n",
1515 return -EAFNOSUPPORT;
1517 dprintk("RPC: %s: succeeded\n", __func__);
1522 * rpc_localaddr - discover local endpoint address for an RPC client
1523 * @clnt: RPC client structure
1524 * @buf: target buffer
1525 * @buflen: size of target buffer, in bytes
1527 * Returns zero and fills in "buf" and "buflen" if successful;
1528 * otherwise, a negative errno is returned.
1530 * This works even if the underlying transport is not currently connected,
1531 * or if the upper layer never previously provided a source address.
1533 * The result of this function call is transient: multiple calls in
1534 * succession may give different results, depending on how local
1535 * networking configuration changes over time.
1537 int rpc_localaddr(struct rpc_clnt *clnt, struct sockaddr *buf, size_t buflen)
1539 struct sockaddr_storage address;
1540 struct sockaddr *sap = (struct sockaddr *)&address;
1541 struct rpc_xprt *xprt;
1547 xprt = rcu_dereference(clnt->cl_xprt);
1548 salen = xprt->addrlen;
1549 memcpy(sap, &xprt->addr, salen);
1550 net = get_net(xprt->xprt_net);
1553 rpc_set_port(sap, 0);
1554 err = rpc_sockname(net, sap, salen, buf);
1557 /* Couldn't discover local address, return ANYADDR */
1558 return rpc_anyaddr(sap->sa_family, buf, buflen);
1561 EXPORT_SYMBOL_GPL(rpc_localaddr);
1564 rpc_setbufsize(struct rpc_clnt *clnt, unsigned int sndsize, unsigned int rcvsize)
1566 struct rpc_xprt *xprt;
1569 xprt = rcu_dereference(clnt->cl_xprt);
1570 if (xprt->ops->set_buffer_size)
1571 xprt->ops->set_buffer_size(xprt, sndsize, rcvsize);
1574 EXPORT_SYMBOL_GPL(rpc_setbufsize);
1577 * rpc_net_ns - Get the network namespace for this RPC client
1578 * @clnt: RPC client to query
1581 struct net *rpc_net_ns(struct rpc_clnt *clnt)
1586 ret = rcu_dereference(clnt->cl_xprt)->xprt_net;
1590 EXPORT_SYMBOL_GPL(rpc_net_ns);
1593 * rpc_max_payload - Get maximum payload size for a transport, in bytes
1594 * @clnt: RPC client to query
1596 * For stream transports, this is one RPC record fragment (see RFC
1597 * 1831), as we don't support multi-record requests yet. For datagram
1598 * transports, this is the size of an IP packet minus the IP, UDP, and
1601 size_t rpc_max_payload(struct rpc_clnt *clnt)
1606 ret = rcu_dereference(clnt->cl_xprt)->max_payload;
1610 EXPORT_SYMBOL_GPL(rpc_max_payload);
1613 * rpc_max_bc_payload - Get maximum backchannel payload size, in bytes
1614 * @clnt: RPC client to query
1616 size_t rpc_max_bc_payload(struct rpc_clnt *clnt)
1618 struct rpc_xprt *xprt;
1622 xprt = rcu_dereference(clnt->cl_xprt);
1623 ret = xprt->ops->bc_maxpayload(xprt);
1627 EXPORT_SYMBOL_GPL(rpc_max_bc_payload);
1629 unsigned int rpc_num_bc_slots(struct rpc_clnt *clnt)
1631 struct rpc_xprt *xprt;
1635 xprt = rcu_dereference(clnt->cl_xprt);
1636 ret = xprt->ops->bc_num_slots(xprt);
1640 EXPORT_SYMBOL_GPL(rpc_num_bc_slots);
1643 * rpc_force_rebind - force transport to check that remote port is unchanged
1644 * @clnt: client to rebind
1647 void rpc_force_rebind(struct rpc_clnt *clnt)
1649 if (clnt->cl_autobind) {
1651 xprt_clear_bound(rcu_dereference(clnt->cl_xprt));
1655 EXPORT_SYMBOL_GPL(rpc_force_rebind);
1658 __rpc_restart_call(struct rpc_task *task, void (*action)(struct rpc_task *))
1660 task->tk_status = 0;
1661 task->tk_rpc_status = 0;
1662 task->tk_action = action;
1667 * Restart an (async) RPC call. Usually called from within the
1671 rpc_restart_call(struct rpc_task *task)
1673 return __rpc_restart_call(task, call_start);
1675 EXPORT_SYMBOL_GPL(rpc_restart_call);
1678 * Restart an (async) RPC call from the call_prepare state.
1679 * Usually called from within the exit handler.
1682 rpc_restart_call_prepare(struct rpc_task *task)
1684 if (task->tk_ops->rpc_call_prepare != NULL)
1685 return __rpc_restart_call(task, rpc_prepare_task);
1686 return rpc_restart_call(task);
1688 EXPORT_SYMBOL_GPL(rpc_restart_call_prepare);
1691 *rpc_proc_name(const struct rpc_task *task)
1693 const struct rpc_procinfo *proc = task->tk_msg.rpc_proc;
1697 return proc->p_name;
1705 __rpc_call_rpcerror(struct rpc_task *task, int tk_status, int rpc_status)
1707 trace_rpc_call_rpcerror(task, tk_status, rpc_status);
1708 rpc_task_set_rpc_status(task, rpc_status);
1709 rpc_exit(task, tk_status);
1713 rpc_call_rpcerror(struct rpc_task *task, int status)
1715 __rpc_call_rpcerror(task, status, status);
1721 * Other FSM states can be visited zero or more times, but
1722 * this state is visited exactly once for each RPC.
1725 call_start(struct rpc_task *task)
1727 struct rpc_clnt *clnt = task->tk_client;
1728 int idx = task->tk_msg.rpc_proc->p_statidx;
1730 trace_rpc_request(task);
1732 if (task->tk_client->cl_shutdown) {
1733 rpc_call_rpcerror(task, -EIO);
1737 /* Increment call count (version might not be valid for ping) */
1738 if (clnt->cl_program->version[clnt->cl_vers])
1739 clnt->cl_program->version[clnt->cl_vers]->counts[idx]++;
1740 clnt->cl_stats->rpccnt++;
1741 task->tk_action = call_reserve;
1742 rpc_task_set_transport(task, clnt);
1746 * 1. Reserve an RPC call slot
1749 call_reserve(struct rpc_task *task)
1751 task->tk_status = 0;
1752 task->tk_action = call_reserveresult;
1756 static void call_retry_reserve(struct rpc_task *task);
1759 * 1b. Grok the result of xprt_reserve()
1762 call_reserveresult(struct rpc_task *task)
1764 int status = task->tk_status;
1767 * After a call to xprt_reserve(), we must have either
1768 * a request slot or else an error status.
1770 task->tk_status = 0;
1772 if (task->tk_rqstp) {
1773 task->tk_action = call_refresh;
1777 rpc_call_rpcerror(task, -EIO);
1783 rpc_delay(task, HZ >> 2);
1785 case -EAGAIN: /* woken up; retry */
1786 task->tk_action = call_retry_reserve;
1789 rpc_call_rpcerror(task, status);
1794 * 1c. Retry reserving an RPC call slot
1797 call_retry_reserve(struct rpc_task *task)
1799 task->tk_status = 0;
1800 task->tk_action = call_reserveresult;
1801 xprt_retry_reserve(task);
1805 * 2. Bind and/or refresh the credentials
1808 call_refresh(struct rpc_task *task)
1810 task->tk_action = call_refreshresult;
1811 task->tk_status = 0;
1812 task->tk_client->cl_stats->rpcauthrefresh++;
1813 rpcauth_refreshcred(task);
1817 * 2a. Process the results of a credential refresh
1820 call_refreshresult(struct rpc_task *task)
1822 int status = task->tk_status;
1824 task->tk_status = 0;
1825 task->tk_action = call_refresh;
1828 if (rpcauth_uptodatecred(task)) {
1829 task->tk_action = call_allocate;
1832 /* Use rate-limiting and a max number of retries if refresh
1833 * had status 0 but failed to update the cred.
1837 rpc_delay(task, 3*HZ);
1843 if (!task->tk_cred_retry)
1845 task->tk_cred_retry--;
1846 trace_rpc_retry_refresh_status(task);
1849 rpc_delay(task, HZ >> 4);
1852 trace_rpc_refresh_status(task);
1853 rpc_call_rpcerror(task, status);
1857 * 2b. Allocate the buffer. For details, see sched.c:rpc_malloc.
1858 * (Note: buffer memory is freed in xprt_release).
1861 call_allocate(struct rpc_task *task)
1863 const struct rpc_auth *auth = task->tk_rqstp->rq_cred->cr_auth;
1864 struct rpc_rqst *req = task->tk_rqstp;
1865 struct rpc_xprt *xprt = req->rq_xprt;
1866 const struct rpc_procinfo *proc = task->tk_msg.rpc_proc;
1869 task->tk_status = 0;
1870 task->tk_action = call_encode;
1875 if (proc->p_proc != 0) {
1876 BUG_ON(proc->p_arglen == 0);
1877 if (proc->p_decode != NULL)
1878 BUG_ON(proc->p_replen == 0);
1882 * Calculate the size (in quads) of the RPC call
1883 * and reply headers, and convert both values
1886 req->rq_callsize = RPC_CALLHDRSIZE + (auth->au_cslack << 1) +
1888 req->rq_callsize <<= 2;
1890 * Note: the reply buffer must at minimum allocate enough space
1891 * for the 'struct accepted_reply' from RFC5531.
1893 req->rq_rcvsize = RPC_REPHDRSIZE + auth->au_rslack + \
1894 max_t(size_t, proc->p_replen, 2);
1895 req->rq_rcvsize <<= 2;
1897 status = xprt->ops->buf_alloc(task);
1898 trace_rpc_buf_alloc(task, status);
1901 if (status != -ENOMEM) {
1902 rpc_call_rpcerror(task, status);
1906 if (RPC_IS_ASYNC(task) || !fatal_signal_pending(current)) {
1907 task->tk_action = call_allocate;
1908 rpc_delay(task, HZ>>4);
1912 rpc_call_rpcerror(task, -ERESTARTSYS);
1916 rpc_task_need_encode(struct rpc_task *task)
1918 return test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate) == 0 &&
1919 (!(task->tk_flags & RPC_TASK_SENT) ||
1920 !(task->tk_flags & RPC_TASK_NO_RETRANS_TIMEOUT) ||
1921 xprt_request_need_retransmit(task));
1925 rpc_xdr_encode(struct rpc_task *task)
1927 struct rpc_rqst *req = task->tk_rqstp;
1928 struct xdr_stream xdr;
1930 xdr_buf_init(&req->rq_snd_buf,
1933 xdr_buf_init(&req->rq_rcv_buf,
1937 req->rq_reply_bytes_recvd = 0;
1938 req->rq_snd_buf.head[0].iov_len = 0;
1939 xdr_init_encode(&xdr, &req->rq_snd_buf,
1940 req->rq_snd_buf.head[0].iov_base, req);
1941 if (rpc_encode_header(task, &xdr))
1944 task->tk_status = rpcauth_wrap_req(task, &xdr);
1948 * 3. Encode arguments of an RPC call
1951 call_encode(struct rpc_task *task)
1953 if (!rpc_task_need_encode(task))
1956 /* Dequeue task from the receive queue while we're encoding */
1957 xprt_request_dequeue_xprt(task);
1958 /* Encode here so that rpcsec_gss can use correct sequence number. */
1959 rpc_xdr_encode(task);
1960 /* Add task to reply queue before transmission to avoid races */
1961 if (task->tk_status == 0 && rpc_reply_expected(task))
1962 task->tk_status = xprt_request_enqueue_receive(task);
1963 /* Did the encode result in an error condition? */
1964 if (task->tk_status != 0) {
1965 /* Was the error nonfatal? */
1966 switch (task->tk_status) {
1969 rpc_delay(task, HZ >> 4);
1972 if (!task->tk_cred_retry) {
1973 rpc_call_rpcerror(task, task->tk_status);
1975 task->tk_action = call_refresh;
1976 task->tk_cred_retry--;
1977 trace_rpc_retry_refresh_status(task);
1981 rpc_call_rpcerror(task, task->tk_status);
1986 xprt_request_enqueue_transmit(task);
1988 task->tk_action = call_transmit;
1989 /* Check that the connection is OK */
1990 if (!xprt_bound(task->tk_xprt))
1991 task->tk_action = call_bind;
1992 else if (!xprt_connected(task->tk_xprt))
1993 task->tk_action = call_connect;
1997 * Helpers to check if the task was already transmitted, and
1998 * to take action when that is the case.
2001 rpc_task_transmitted(struct rpc_task *task)
2003 return !test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate);
2007 rpc_task_handle_transmitted(struct rpc_task *task)
2009 xprt_end_transmit(task);
2010 task->tk_action = call_transmit_status;
2014 * 4. Get the server port number if not yet set
2017 call_bind(struct rpc_task *task)
2019 struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
2021 if (rpc_task_transmitted(task)) {
2022 rpc_task_handle_transmitted(task);
2026 if (xprt_bound(xprt)) {
2027 task->tk_action = call_connect;
2031 task->tk_action = call_bind_status;
2032 if (!xprt_prepare_transmit(task))
2035 xprt->ops->rpcbind(task);
2039 * 4a. Sort out bind result
2042 call_bind_status(struct rpc_task *task)
2044 struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
2047 if (rpc_task_transmitted(task)) {
2048 rpc_task_handle_transmitted(task);
2052 if (task->tk_status >= 0)
2054 if (xprt_bound(xprt)) {
2055 task->tk_status = 0;
2059 switch (task->tk_status) {
2061 rpc_delay(task, HZ >> 2);
2064 trace_rpcb_prog_unavail_err(task);
2065 /* fail immediately if this is an RPC ping */
2066 if (task->tk_msg.rpc_proc->p_proc == 0) {
2067 status = -EOPNOTSUPP;
2070 rpc_delay(task, 3*HZ);
2073 rpc_delay(task, HZ >> 2);
2078 trace_rpcb_timeout_err(task);
2081 /* server doesn't support any rpcbind version we know of */
2082 trace_rpcb_bind_version_err(task);
2084 case -EPROTONOSUPPORT:
2085 trace_rpcb_bind_version_err(task);
2087 case -ECONNREFUSED: /* connection problems */
2096 trace_rpcb_unreachable_err(task);
2097 if (!RPC_IS_SOFTCONN(task)) {
2098 rpc_delay(task, 5*HZ);
2101 status = task->tk_status;
2104 trace_rpcb_unrecognized_err(task);
2107 rpc_call_rpcerror(task, status);
2110 task->tk_action = call_connect;
2113 task->tk_status = 0;
2114 task->tk_action = call_bind;
2115 rpc_check_timeout(task);
2119 * 4b. Connect to the RPC server
2122 call_connect(struct rpc_task *task)
2124 struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
2126 if (rpc_task_transmitted(task)) {
2127 rpc_task_handle_transmitted(task);
2131 if (xprt_connected(xprt)) {
2132 task->tk_action = call_transmit;
2136 task->tk_action = call_connect_status;
2137 if (task->tk_status < 0)
2139 if (task->tk_flags & RPC_TASK_NOCONNECT) {
2140 rpc_call_rpcerror(task, -ENOTCONN);
2143 if (!xprt_prepare_transmit(task))
2149 * 4c. Sort out connect result
2152 call_connect_status(struct rpc_task *task)
2154 struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
2155 struct rpc_clnt *clnt = task->tk_client;
2156 int status = task->tk_status;
2158 if (rpc_task_transmitted(task)) {
2159 rpc_task_handle_transmitted(task);
2163 trace_rpc_connect_status(task);
2165 if (task->tk_status == 0) {
2166 clnt->cl_stats->netreconn++;
2169 if (xprt_connected(xprt)) {
2170 task->tk_status = 0;
2174 task->tk_status = 0;
2178 /* A positive refusal suggests a rebind is needed. */
2179 if (RPC_IS_SOFTCONN(task))
2181 if (clnt->cl_autobind) {
2182 rpc_force_rebind(clnt);
2192 xprt_conditional_disconnect(task->tk_rqstp->rq_xprt,
2193 task->tk_rqstp->rq_connect_cookie);
2194 if (RPC_IS_SOFTCONN(task))
2196 /* retry with existing socket, after a delay */
2197 rpc_delay(task, 3*HZ);
2203 if (!(task->tk_flags & RPC_TASK_NO_ROUND_ROBIN) &&
2204 (task->tk_flags & RPC_TASK_MOVEABLE) &&
2205 test_bit(XPRT_REMOVE, &xprt->state)) {
2206 struct rpc_xprt *saved = task->tk_xprt;
2207 struct rpc_xprt_switch *xps;
2210 xps = xprt_switch_get(rcu_dereference(clnt->cl_xpi.xpi_xpswitch));
2212 if (xps->xps_nxprts > 1) {
2216 value = atomic_long_dec_return(&xprt->queuelen);
2218 rpc_xprt_switch_remove_xprt(xps, saved,
2221 task->tk_xprt = NULL;
2222 task->tk_action = call_start;
2224 xprt_switch_put(xps);
2230 rpc_delay(task, HZ >> 2);
2233 rpc_call_rpcerror(task, status);
2236 task->tk_action = call_transmit;
2239 /* Check for timeouts before looping back to call_bind */
2240 task->tk_action = call_bind;
2242 rpc_check_timeout(task);
2246 * 5. Transmit the RPC request, and wait for reply
2249 call_transmit(struct rpc_task *task)
2251 if (rpc_task_transmitted(task)) {
2252 rpc_task_handle_transmitted(task);
2256 task->tk_action = call_transmit_status;
2257 if (!xprt_prepare_transmit(task))
2259 task->tk_status = 0;
2260 if (test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate)) {
2261 if (!xprt_connected(task->tk_xprt)) {
2262 task->tk_status = -ENOTCONN;
2265 xprt_transmit(task);
2267 xprt_end_transmit(task);
2271 * 5a. Handle cleanup after a transmission
2274 call_transmit_status(struct rpc_task *task)
2276 task->tk_action = call_status;
2279 * Common case: success. Force the compiler to put this
2282 if (rpc_task_transmitted(task)) {
2283 task->tk_status = 0;
2284 xprt_request_wait_receive(task);
2288 switch (task->tk_status) {
2292 task->tk_status = 0;
2293 task->tk_action = call_encode;
2296 * Special cases: if we've been waiting on the
2297 * socket's write_space() callback, or if the
2298 * socket just returned a connection error,
2299 * then hold onto the transport lock.
2303 rpc_delay(task, HZ>>2);
2307 task->tk_action = call_transmit;
2308 task->tk_status = 0;
2316 if (RPC_IS_SOFTCONN(task)) {
2317 if (!task->tk_msg.rpc_proc->p_proc)
2318 trace_xprt_ping(task->tk_xprt,
2320 rpc_call_rpcerror(task, task->tk_status);
2329 task->tk_action = call_bind;
2330 task->tk_status = 0;
2333 rpc_check_timeout(task);
2336 #if defined(CONFIG_SUNRPC_BACKCHANNEL)
2337 static void call_bc_transmit(struct rpc_task *task);
2338 static void call_bc_transmit_status(struct rpc_task *task);
2341 call_bc_encode(struct rpc_task *task)
2343 xprt_request_enqueue_transmit(task);
2344 task->tk_action = call_bc_transmit;
2348 * 5b. Send the backchannel RPC reply. On error, drop the reply. In
2349 * addition, disconnect on connectivity errors.
2352 call_bc_transmit(struct rpc_task *task)
2354 task->tk_action = call_bc_transmit_status;
2355 if (test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate)) {
2356 if (!xprt_prepare_transmit(task))
2358 task->tk_status = 0;
2359 xprt_transmit(task);
2361 xprt_end_transmit(task);
2365 call_bc_transmit_status(struct rpc_task *task)
2367 struct rpc_rqst *req = task->tk_rqstp;
2369 if (rpc_task_transmitted(task))
2370 task->tk_status = 0;
2372 switch (task->tk_status) {
2387 rpc_delay(task, HZ>>2);
2391 task->tk_status = 0;
2392 task->tk_action = call_bc_transmit;
2396 * Problem reaching the server. Disconnect and let the
2397 * forechannel reestablish the connection. The server will
2398 * have to retransmit the backchannel request and we'll
2399 * reprocess it. Since these ops are idempotent, there's no
2400 * need to cache our reply at this time.
2402 printk(KERN_NOTICE "RPC: Could not send backchannel reply "
2403 "error: %d\n", task->tk_status);
2404 xprt_conditional_disconnect(req->rq_xprt,
2405 req->rq_connect_cookie);
2409 * We were unable to reply and will have to drop the
2410 * request. The server should reconnect and retransmit.
2412 printk(KERN_NOTICE "RPC: Could not send backchannel reply "
2413 "error: %d\n", task->tk_status);
2416 task->tk_action = rpc_exit_task;
2418 #endif /* CONFIG_SUNRPC_BACKCHANNEL */
2421 * 6. Sort out the RPC call status
2424 call_status(struct rpc_task *task)
2426 struct rpc_clnt *clnt = task->tk_client;
2429 if (!task->tk_msg.rpc_proc->p_proc)
2430 trace_xprt_ping(task->tk_xprt, task->tk_status);
2432 status = task->tk_status;
2434 task->tk_action = call_decode;
2438 trace_rpc_call_status(task);
2439 task->tk_status = 0;
2446 if (RPC_IS_SOFTCONN(task))
2449 * Delay any retries for 3 seconds, then handle as if it
2452 rpc_delay(task, 3*HZ);
2460 rpc_force_rebind(clnt);
2463 rpc_delay(task, 3*HZ);
2471 rpc_delay(task, HZ>>2);
2474 /* shutdown or soft timeout */
2477 if (clnt->cl_chatty)
2478 printk("%s: RPC call returned error %d\n",
2479 clnt->cl_program->name, -status);
2482 task->tk_action = call_encode;
2483 rpc_check_timeout(task);
2486 rpc_call_rpcerror(task, status);
2490 rpc_check_connected(const struct rpc_rqst *req)
2492 /* No allocated request or transport? return true */
2493 if (!req || !req->rq_xprt)
2495 return xprt_connected(req->rq_xprt);
2499 rpc_check_timeout(struct rpc_task *task)
2501 struct rpc_clnt *clnt = task->tk_client;
2503 if (RPC_SIGNALLED(task))
2506 if (xprt_adjust_timeout(task->tk_rqstp) == 0)
2509 trace_rpc_timeout_status(task);
2510 task->tk_timeouts++;
2512 if (RPC_IS_SOFTCONN(task) && !rpc_check_connected(task->tk_rqstp)) {
2513 rpc_call_rpcerror(task, -ETIMEDOUT);
2517 if (RPC_IS_SOFT(task)) {
2519 * Once a "no retrans timeout" soft tasks (a.k.a NFSv4) has
2520 * been sent, it should time out only if the transport
2521 * connection gets terminally broken.
2523 if ((task->tk_flags & RPC_TASK_NO_RETRANS_TIMEOUT) &&
2524 rpc_check_connected(task->tk_rqstp))
2527 if (clnt->cl_chatty) {
2528 pr_notice_ratelimited(
2529 "%s: server %s not responding, timed out\n",
2530 clnt->cl_program->name,
2531 task->tk_xprt->servername);
2533 if (task->tk_flags & RPC_TASK_TIMEOUT)
2534 rpc_call_rpcerror(task, -ETIMEDOUT);
2536 __rpc_call_rpcerror(task, -EIO, -ETIMEDOUT);
2540 if (!(task->tk_flags & RPC_CALL_MAJORSEEN)) {
2541 task->tk_flags |= RPC_CALL_MAJORSEEN;
2542 if (clnt->cl_chatty) {
2543 pr_notice_ratelimited(
2544 "%s: server %s not responding, still trying\n",
2545 clnt->cl_program->name,
2546 task->tk_xprt->servername);
2549 rpc_force_rebind(clnt);
2551 * Did our request time out due to an RPCSEC_GSS out-of-sequence
2552 * event? RFC2203 requires the server to drop all such requests.
2554 rpcauth_invalcred(task);
2558 * 7. Decode the RPC reply
2561 call_decode(struct rpc_task *task)
2563 struct rpc_clnt *clnt = task->tk_client;
2564 struct rpc_rqst *req = task->tk_rqstp;
2565 struct xdr_stream xdr;
2568 if (!task->tk_msg.rpc_proc->p_decode) {
2569 task->tk_action = rpc_exit_task;
2573 if (task->tk_flags & RPC_CALL_MAJORSEEN) {
2574 if (clnt->cl_chatty) {
2575 pr_notice_ratelimited("%s: server %s OK\n",
2576 clnt->cl_program->name,
2577 task->tk_xprt->servername);
2579 task->tk_flags &= ~RPC_CALL_MAJORSEEN;
2583 * Did we ever call xprt_complete_rqst()? If not, we should assume
2584 * the message is incomplete.
2587 if (!req->rq_reply_bytes_recvd)
2590 /* Ensure that we see all writes made by xprt_complete_rqst()
2591 * before it changed req->rq_reply_bytes_recvd.
2595 req->rq_rcv_buf.len = req->rq_private_buf.len;
2596 trace_rpc_xdr_recvfrom(task, &req->rq_rcv_buf);
2598 /* Check that the softirq receive buffer is valid */
2599 WARN_ON(memcmp(&req->rq_rcv_buf, &req->rq_private_buf,
2600 sizeof(req->rq_rcv_buf)) != 0);
2602 xdr_init_decode(&xdr, &req->rq_rcv_buf,
2603 req->rq_rcv_buf.head[0].iov_base, req);
2604 err = rpc_decode_header(task, &xdr);
2608 task->tk_action = rpc_exit_task;
2609 task->tk_status = rpcauth_unwrap_resp(task, &xdr);
2610 xdr_finish_decode(&xdr);
2613 task->tk_status = 0;
2614 if (task->tk_client->cl_discrtry)
2615 xprt_conditional_disconnect(req->rq_xprt,
2616 req->rq_connect_cookie);
2617 task->tk_action = call_encode;
2618 rpc_check_timeout(task);
2621 task->tk_action = call_reserve;
2622 rpc_check_timeout(task);
2623 rpcauth_invalcred(task);
2624 /* Ensure we obtain a new XID if we retry! */
2630 rpc_encode_header(struct rpc_task *task, struct xdr_stream *xdr)
2632 struct rpc_clnt *clnt = task->tk_client;
2633 struct rpc_rqst *req = task->tk_rqstp;
2638 p = xdr_reserve_space(xdr, RPC_CALLHDRSIZE << 2);
2643 *p++ = cpu_to_be32(RPC_VERSION);
2644 *p++ = cpu_to_be32(clnt->cl_prog);
2645 *p++ = cpu_to_be32(clnt->cl_vers);
2646 *p = cpu_to_be32(task->tk_msg.rpc_proc->p_proc);
2648 error = rpcauth_marshcred(task, xdr);
2653 trace_rpc_bad_callhdr(task);
2654 rpc_call_rpcerror(task, error);
2659 rpc_decode_header(struct rpc_task *task, struct xdr_stream *xdr)
2661 struct rpc_clnt *clnt = task->tk_client;
2665 /* RFC-1014 says that the representation of XDR data must be a
2666 * multiple of four bytes
2667 * - if it isn't pointer subtraction in the NFS client may give
2670 if (task->tk_rqstp->rq_rcv_buf.len & 3)
2671 goto out_unparsable;
2673 p = xdr_inline_decode(xdr, 3 * sizeof(*p));
2675 goto out_unparsable;
2677 if (*p++ != rpc_reply)
2678 goto out_unparsable;
2679 if (*p++ != rpc_msg_accepted)
2680 goto out_msg_denied;
2682 error = rpcauth_checkverf(task, xdr);
2686 p = xdr_inline_decode(xdr, sizeof(*p));
2688 goto out_unparsable;
2692 case rpc_prog_unavail:
2693 trace_rpc__prog_unavail(task);
2694 error = -EPFNOSUPPORT;
2696 case rpc_prog_mismatch:
2697 trace_rpc__prog_mismatch(task);
2698 error = -EPROTONOSUPPORT;
2700 case rpc_proc_unavail:
2701 trace_rpc__proc_unavail(task);
2702 error = -EOPNOTSUPP;
2704 case rpc_garbage_args:
2705 case rpc_system_err:
2706 trace_rpc__garbage_args(task);
2710 goto out_unparsable;
2714 clnt->cl_stats->rpcgarbage++;
2715 if (task->tk_garb_retry) {
2716 task->tk_garb_retry--;
2717 task->tk_action = call_encode;
2721 rpc_call_rpcerror(task, error);
2725 trace_rpc__unparsable(task);
2730 trace_rpc_bad_verifier(task);
2732 case -EPROTONOSUPPORT:
2735 /* Re-encode with a fresh cred */
2743 p = xdr_inline_decode(xdr, sizeof(*p));
2745 goto out_unparsable;
2747 case rpc_auth_error:
2750 trace_rpc__mismatch(task);
2751 error = -EPROTONOSUPPORT;
2754 goto out_unparsable;
2757 p = xdr_inline_decode(xdr, sizeof(*p));
2759 goto out_unparsable;
2761 case rpc_autherr_rejectedcred:
2762 case rpc_autherr_rejectedverf:
2763 case rpcsec_gsserr_credproblem:
2764 case rpcsec_gsserr_ctxproblem:
2765 rpcauth_invalcred(task);
2766 if (!task->tk_cred_retry)
2768 task->tk_cred_retry--;
2769 trace_rpc__stale_creds(task);
2770 return -EKEYREJECTED;
2771 case rpc_autherr_badcred:
2772 case rpc_autherr_badverf:
2773 /* possibly garbled cred/verf? */
2774 if (!task->tk_garb_retry)
2776 task->tk_garb_retry--;
2777 trace_rpc__bad_creds(task);
2778 task->tk_action = call_encode;
2780 case rpc_autherr_tooweak:
2781 trace_rpc__auth_tooweak(task);
2782 pr_warn("RPC: server %s requires stronger authentication.\n",
2783 task->tk_xprt->servername);
2786 goto out_unparsable;
2791 static void rpcproc_encode_null(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
2796 static int rpcproc_decode_null(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
2802 static const struct rpc_procinfo rpcproc_null = {
2803 .p_encode = rpcproc_encode_null,
2804 .p_decode = rpcproc_decode_null,
2807 static const struct rpc_procinfo rpcproc_null_noreply = {
2808 .p_encode = rpcproc_encode_null,
2812 rpc_null_call_prepare(struct rpc_task *task, void *data)
2814 task->tk_flags &= ~RPC_TASK_NO_RETRANS_TIMEOUT;
2815 rpc_call_start(task);
2818 static const struct rpc_call_ops rpc_null_ops = {
2819 .rpc_call_prepare = rpc_null_call_prepare,
2820 .rpc_call_done = rpc_default_callback,
2824 struct rpc_task *rpc_call_null_helper(struct rpc_clnt *clnt,
2825 struct rpc_xprt *xprt, struct rpc_cred *cred, int flags,
2826 const struct rpc_call_ops *ops, void *data)
2828 struct rpc_message msg = {
2829 .rpc_proc = &rpcproc_null,
2831 struct rpc_task_setup task_setup_data = {
2834 .rpc_message = &msg,
2835 .rpc_op_cred = cred,
2836 .callback_ops = ops ?: &rpc_null_ops,
2837 .callback_data = data,
2838 .flags = flags | RPC_TASK_SOFT | RPC_TASK_SOFTCONN |
2842 return rpc_run_task(&task_setup_data);
2845 struct rpc_task *rpc_call_null(struct rpc_clnt *clnt, struct rpc_cred *cred, int flags)
2847 return rpc_call_null_helper(clnt, NULL, cred, flags, NULL, NULL);
2849 EXPORT_SYMBOL_GPL(rpc_call_null);
2851 static int rpc_ping(struct rpc_clnt *clnt)
2853 struct rpc_task *task;
2856 if (clnt->cl_auth->au_ops->ping)
2857 return clnt->cl_auth->au_ops->ping(clnt);
2859 task = rpc_call_null_helper(clnt, NULL, NULL, 0, NULL, NULL);
2861 return PTR_ERR(task);
2862 status = task->tk_status;
2867 static int rpc_ping_noreply(struct rpc_clnt *clnt)
2869 struct rpc_message msg = {
2870 .rpc_proc = &rpcproc_null_noreply,
2872 struct rpc_task_setup task_setup_data = {
2874 .rpc_message = &msg,
2875 .callback_ops = &rpc_null_ops,
2876 .flags = RPC_TASK_SOFT | RPC_TASK_SOFTCONN | RPC_TASK_NULLCREDS,
2878 struct rpc_task *task;
2881 task = rpc_run_task(&task_setup_data);
2883 return PTR_ERR(task);
2884 status = task->tk_status;
2889 struct rpc_cb_add_xprt_calldata {
2890 struct rpc_xprt_switch *xps;
2891 struct rpc_xprt *xprt;
2894 static void rpc_cb_add_xprt_done(struct rpc_task *task, void *calldata)
2896 struct rpc_cb_add_xprt_calldata *data = calldata;
2898 if (task->tk_status == 0)
2899 rpc_xprt_switch_add_xprt(data->xps, data->xprt);
2902 static void rpc_cb_add_xprt_release(void *calldata)
2904 struct rpc_cb_add_xprt_calldata *data = calldata;
2906 xprt_put(data->xprt);
2907 xprt_switch_put(data->xps);
2911 static const struct rpc_call_ops rpc_cb_add_xprt_call_ops = {
2912 .rpc_call_prepare = rpc_null_call_prepare,
2913 .rpc_call_done = rpc_cb_add_xprt_done,
2914 .rpc_release = rpc_cb_add_xprt_release,
2918 * rpc_clnt_test_and_add_xprt - Test and add a new transport to a rpc_clnt
2919 * @clnt: pointer to struct rpc_clnt
2920 * @xps: pointer to struct rpc_xprt_switch,
2921 * @xprt: pointer struct rpc_xprt
2922 * @in_max_connect: pointer to the max_connect value for the passed in xprt transport
2924 int rpc_clnt_test_and_add_xprt(struct rpc_clnt *clnt,
2925 struct rpc_xprt_switch *xps, struct rpc_xprt *xprt,
2926 void *in_max_connect)
2928 struct rpc_cb_add_xprt_calldata *data;
2929 struct rpc_task *task;
2930 int max_connect = clnt->cl_max_connect;
2933 max_connect = *(int *)in_max_connect;
2934 if (xps->xps_nunique_destaddr_xprts + 1 > max_connect) {
2936 pr_warn("SUNRPC: reached max allowed number (%d) did not add "
2937 "transport to server: %s\n", max_connect,
2938 rpc_peeraddr2str(clnt, RPC_DISPLAY_ADDR));
2943 data = kmalloc(sizeof(*data), GFP_KERNEL);
2946 data->xps = xprt_switch_get(xps);
2947 data->xprt = xprt_get(xprt);
2948 if (rpc_xprt_switch_has_addr(data->xps, (struct sockaddr *)&xprt->addr)) {
2949 rpc_cb_add_xprt_release(data);
2953 task = rpc_call_null_helper(clnt, xprt, NULL, RPC_TASK_ASYNC,
2954 &rpc_cb_add_xprt_call_ops, data);
2956 return PTR_ERR(task);
2958 data->xps->xps_nunique_destaddr_xprts++;
2963 EXPORT_SYMBOL_GPL(rpc_clnt_test_and_add_xprt);
2965 static int rpc_clnt_add_xprt_helper(struct rpc_clnt *clnt,
2966 struct rpc_xprt *xprt,
2967 struct rpc_add_xprt_test *data)
2969 struct rpc_task *task;
2970 int status = -EADDRINUSE;
2972 /* Test the connection */
2973 task = rpc_call_null_helper(clnt, xprt, NULL, 0, NULL, NULL);
2975 return PTR_ERR(task);
2977 status = task->tk_status;
2983 /* rpc_xprt_switch and rpc_xprt are deferrenced by add_xprt_test() */
2984 data->add_xprt_test(clnt, xprt, data->data);
2990 * rpc_clnt_setup_test_and_add_xprt()
2992 * This is an rpc_clnt_add_xprt setup() function which returns 1 so:
2993 * 1) caller of the test function must dereference the rpc_xprt_switch
2995 * 2) test function must call rpc_xprt_switch_add_xprt, usually in
2996 * the rpc_call_done routine.
2998 * Upon success (return of 1), the test function adds the new
2999 * transport to the rpc_clnt xprt switch
3001 * @clnt: struct rpc_clnt to get the new transport
3002 * @xps: the rpc_xprt_switch to hold the new transport
3003 * @xprt: the rpc_xprt to test
3004 * @data: a struct rpc_add_xprt_test pointer that holds the test function
3005 * and test function call data
3007 int rpc_clnt_setup_test_and_add_xprt(struct rpc_clnt *clnt,
3008 struct rpc_xprt_switch *xps,
3009 struct rpc_xprt *xprt,
3012 int status = -EADDRINUSE;
3014 xprt = xprt_get(xprt);
3015 xprt_switch_get(xps);
3017 if (rpc_xprt_switch_has_addr(xps, (struct sockaddr *)&xprt->addr))
3020 status = rpc_clnt_add_xprt_helper(clnt, xprt, data);
3027 xprt_switch_put(xps);
3029 pr_info("RPC: rpc_clnt_test_xprt failed: %d addr %s not "
3031 xprt->address_strings[RPC_DISPLAY_ADDR]);
3032 /* so that rpc_clnt_add_xprt does not call rpc_xprt_switch_add_xprt */
3035 EXPORT_SYMBOL_GPL(rpc_clnt_setup_test_and_add_xprt);
3038 * rpc_clnt_add_xprt - Add a new transport to a rpc_clnt
3039 * @clnt: pointer to struct rpc_clnt
3040 * @xprtargs: pointer to struct xprt_create
3041 * @setup: callback to test and/or set up the connection
3042 * @data: pointer to setup function data
3044 * Creates a new transport using the parameters set in args and
3046 * If ping is set, then test that connectivity succeeds before
3047 * adding the new transport.
3050 int rpc_clnt_add_xprt(struct rpc_clnt *clnt,
3051 struct xprt_create *xprtargs,
3052 int (*setup)(struct rpc_clnt *,
3053 struct rpc_xprt_switch *,
3058 struct rpc_xprt_switch *xps;
3059 struct rpc_xprt *xprt;
3060 unsigned long connect_timeout;
3061 unsigned long reconnect_timeout;
3062 unsigned char resvport, reuseport;
3066 xps = xprt_switch_get(rcu_dereference(clnt->cl_xpi.xpi_xpswitch));
3067 xprt = xprt_iter_xprt(&clnt->cl_xpi);
3068 if (xps == NULL || xprt == NULL) {
3070 xprt_switch_put(xps);
3073 resvport = xprt->resvport;
3074 reuseport = xprt->reuseport;
3075 connect_timeout = xprt->connect_timeout;
3076 reconnect_timeout = xprt->max_reconnect_timeout;
3077 ident = xprt->xprt_class->ident;
3080 if (!xprtargs->ident)
3081 xprtargs->ident = ident;
3082 xprtargs->xprtsec = clnt->cl_xprtsec;
3083 xprt = xprt_create_transport(xprtargs);
3085 ret = PTR_ERR(xprt);
3086 goto out_put_switch;
3088 xprt->resvport = resvport;
3089 xprt->reuseport = reuseport;
3091 if (xprtargs->connect_timeout)
3092 connect_timeout = xprtargs->connect_timeout;
3093 if (xprtargs->reconnect_timeout)
3094 reconnect_timeout = xprtargs->reconnect_timeout;
3095 if (xprt->ops->set_connect_timeout != NULL)
3096 xprt->ops->set_connect_timeout(xprt,
3100 rpc_xprt_switch_set_roundrobin(xps);
3102 ret = setup(clnt, xps, xprt, data);
3106 rpc_xprt_switch_add_xprt(xps, xprt);
3110 xprt_switch_put(xps);
3113 EXPORT_SYMBOL_GPL(rpc_clnt_add_xprt);
3115 static int rpc_xprt_probe_trunked(struct rpc_clnt *clnt,
3116 struct rpc_xprt *xprt,
3117 struct rpc_add_xprt_test *data)
3119 struct rpc_xprt_switch *xps;
3120 struct rpc_xprt *main_xprt;
3126 main_xprt = xprt_get(rcu_dereference(clnt->cl_xprt));
3127 xps = xprt_switch_get(rcu_dereference(clnt->cl_xpi.xpi_xpswitch));
3128 status = rpc_cmp_addr_port((struct sockaddr *)&xprt->addr,
3129 (struct sockaddr *)&main_xprt->addr);
3131 xprt_put(main_xprt);
3132 if (status || !test_bit(XPRT_OFFLINE, &xprt->state))
3135 status = rpc_clnt_add_xprt_helper(clnt, xprt, data);
3138 xprt_switch_put(xps);
3142 /* rpc_clnt_probe_trunked_xprt -- probe offlined transport for session trunking
3143 * @clnt rpc_clnt structure
3145 * For each offlined transport found in the rpc_clnt structure call
3146 * the function rpc_xprt_probe_trunked() which will determine if this
3147 * transport still belongs to the trunking group.
3149 void rpc_clnt_probe_trunked_xprts(struct rpc_clnt *clnt,
3150 struct rpc_add_xprt_test *data)
3152 struct rpc_xprt_iter xpi;
3155 ret = rpc_clnt_xprt_iter_offline_init(clnt, &xpi);
3159 struct rpc_xprt *xprt = xprt_iter_get_next(&xpi);
3163 ret = rpc_xprt_probe_trunked(clnt, xprt, data);
3167 xprt_iter_rewind(&xpi);
3169 xprt_iter_destroy(&xpi);
3171 EXPORT_SYMBOL_GPL(rpc_clnt_probe_trunked_xprts);
3173 static int rpc_xprt_offline(struct rpc_clnt *clnt,
3174 struct rpc_xprt *xprt,
3177 struct rpc_xprt *main_xprt;
3178 struct rpc_xprt_switch *xps;
3184 main_xprt = xprt_get(rcu_dereference(clnt->cl_xprt));
3185 xps = xprt_switch_get(rcu_dereference(clnt->cl_xpi.xpi_xpswitch));
3186 err = rpc_cmp_addr_port((struct sockaddr *)&xprt->addr,
3187 (struct sockaddr *)&main_xprt->addr);
3189 xprt_put(main_xprt);
3193 if (wait_on_bit_lock(&xprt->state, XPRT_LOCKED, TASK_KILLABLE)) {
3197 xprt_set_offline_locked(xprt, xps);
3199 xprt_release_write(xprt, NULL);
3202 xprt_switch_put(xps);
3206 /* rpc_clnt_manage_trunked_xprts -- offline trunked transports
3207 * @clnt rpc_clnt structure
3209 * For each active transport found in the rpc_clnt structure call
3210 * the function rpc_xprt_offline() which will identify trunked transports
3211 * and will mark them offline.
3213 void rpc_clnt_manage_trunked_xprts(struct rpc_clnt *clnt)
3215 rpc_clnt_iterate_for_each_xprt(clnt, rpc_xprt_offline, NULL);
3217 EXPORT_SYMBOL_GPL(rpc_clnt_manage_trunked_xprts);
3219 struct connect_timeout_data {
3220 unsigned long connect_timeout;
3221 unsigned long reconnect_timeout;
3225 rpc_xprt_set_connect_timeout(struct rpc_clnt *clnt,
3226 struct rpc_xprt *xprt,
3229 struct connect_timeout_data *timeo = data;
3231 if (xprt->ops->set_connect_timeout)
3232 xprt->ops->set_connect_timeout(xprt,
3233 timeo->connect_timeout,
3234 timeo->reconnect_timeout);
3239 rpc_set_connect_timeout(struct rpc_clnt *clnt,
3240 unsigned long connect_timeout,
3241 unsigned long reconnect_timeout)
3243 struct connect_timeout_data timeout = {
3244 .connect_timeout = connect_timeout,
3245 .reconnect_timeout = reconnect_timeout,
3247 rpc_clnt_iterate_for_each_xprt(clnt,
3248 rpc_xprt_set_connect_timeout,
3251 EXPORT_SYMBOL_GPL(rpc_set_connect_timeout);
3253 void rpc_clnt_xprt_switch_put(struct rpc_clnt *clnt)
3256 xprt_switch_put(rcu_dereference(clnt->cl_xpi.xpi_xpswitch));
3259 EXPORT_SYMBOL_GPL(rpc_clnt_xprt_switch_put);
3261 void rpc_clnt_xprt_set_online(struct rpc_clnt *clnt, struct rpc_xprt *xprt)
3263 struct rpc_xprt_switch *xps;
3266 xps = rcu_dereference(clnt->cl_xpi.xpi_xpswitch);
3268 xprt_set_online_locked(xprt, xps);
3271 void rpc_clnt_xprt_switch_add_xprt(struct rpc_clnt *clnt, struct rpc_xprt *xprt)
3273 if (rpc_clnt_xprt_switch_has_addr(clnt,
3274 (const struct sockaddr *)&xprt->addr)) {
3275 return rpc_clnt_xprt_set_online(clnt, xprt);
3278 rpc_xprt_switch_add_xprt(rcu_dereference(clnt->cl_xpi.xpi_xpswitch),
3282 EXPORT_SYMBOL_GPL(rpc_clnt_xprt_switch_add_xprt);
3284 void rpc_clnt_xprt_switch_remove_xprt(struct rpc_clnt *clnt, struct rpc_xprt *xprt)
3286 struct rpc_xprt_switch *xps;
3289 xps = rcu_dereference(clnt->cl_xpi.xpi_xpswitch);
3290 rpc_xprt_switch_remove_xprt(rcu_dereference(clnt->cl_xpi.xpi_xpswitch),
3292 xps->xps_nunique_destaddr_xprts--;
3295 EXPORT_SYMBOL_GPL(rpc_clnt_xprt_switch_remove_xprt);
3297 bool rpc_clnt_xprt_switch_has_addr(struct rpc_clnt *clnt,
3298 const struct sockaddr *sap)
3300 struct rpc_xprt_switch *xps;
3304 xps = rcu_dereference(clnt->cl_xpi.xpi_xpswitch);
3305 ret = rpc_xprt_switch_has_addr(xps, sap);
3309 EXPORT_SYMBOL_GPL(rpc_clnt_xprt_switch_has_addr);
3311 #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
3312 static void rpc_show_header(void)
3314 printk(KERN_INFO "-pid- flgs status -client- --rqstp- "
3315 "-timeout ---ops--\n");
3318 static void rpc_show_task(const struct rpc_clnt *clnt,
3319 const struct rpc_task *task)
3321 const char *rpc_waitq = "none";
3323 if (RPC_IS_QUEUED(task))
3324 rpc_waitq = rpc_qname(task->tk_waitqueue);
3326 printk(KERN_INFO "%5u %04x %6d %8p %8p %8ld %8p %sv%u %s a:%ps q:%s\n",
3327 task->tk_pid, task->tk_flags, task->tk_status,
3328 clnt, task->tk_rqstp, rpc_task_timeout(task), task->tk_ops,
3329 clnt->cl_program->name, clnt->cl_vers, rpc_proc_name(task),
3330 task->tk_action, rpc_waitq);
3333 void rpc_show_tasks(struct net *net)
3335 struct rpc_clnt *clnt;
3336 struct rpc_task *task;
3338 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
3340 spin_lock(&sn->rpc_client_lock);
3341 list_for_each_entry(clnt, &sn->all_clients, cl_clients) {
3342 spin_lock(&clnt->cl_lock);
3343 list_for_each_entry(task, &clnt->cl_tasks, tk_task) {
3348 rpc_show_task(clnt, task);
3350 spin_unlock(&clnt->cl_lock);
3352 spin_unlock(&sn->rpc_client_lock);
3356 #if IS_ENABLED(CONFIG_SUNRPC_SWAP)
3358 rpc_clnt_swap_activate_callback(struct rpc_clnt *clnt,
3359 struct rpc_xprt *xprt,
3362 return xprt_enable_swap(xprt);
3366 rpc_clnt_swap_activate(struct rpc_clnt *clnt)
3368 while (clnt != clnt->cl_parent)
3369 clnt = clnt->cl_parent;
3370 if (atomic_inc_return(&clnt->cl_swapper) == 1)
3371 return rpc_clnt_iterate_for_each_xprt(clnt,
3372 rpc_clnt_swap_activate_callback, NULL);
3375 EXPORT_SYMBOL_GPL(rpc_clnt_swap_activate);
3378 rpc_clnt_swap_deactivate_callback(struct rpc_clnt *clnt,
3379 struct rpc_xprt *xprt,
3382 xprt_disable_swap(xprt);
3387 rpc_clnt_swap_deactivate(struct rpc_clnt *clnt)
3389 while (clnt != clnt->cl_parent)
3390 clnt = clnt->cl_parent;
3391 if (atomic_dec_if_positive(&clnt->cl_swapper) == 0)
3392 rpc_clnt_iterate_for_each_xprt(clnt,
3393 rpc_clnt_swap_deactivate_callback, NULL);
3395 EXPORT_SYMBOL_GPL(rpc_clnt_swap_deactivate);
3396 #endif /* CONFIG_SUNRPC_SWAP */