2 * linux/net/sunrpc/clnt.c
4 * This file contains the high-level RPC interface.
5 * It is modeled as a finite state machine to support both synchronous
6 * and asynchronous requests.
8 * - RPC header generation and argument serialization.
9 * - Credential refresh.
10 * - TCP connect handling.
11 * - Retry of operation when it is suspected the operation failed because
12 * of uid squashing on the server, or when the credentials were stale
13 * and need to be refreshed, or when a packet was damaged in transit.
14 * This may be have to be moved to the VFS layer.
16 * Copyright (C) 1992,1993 Rick Sladkey <jrs@world.std.com>
17 * Copyright (C) 1995,1996 Olaf Kirch <okir@monad.swb.de>
21 #include <linux/module.h>
22 #include <linux/types.h>
23 #include <linux/kallsyms.h>
25 #include <linux/namei.h>
26 #include <linux/mount.h>
27 #include <linux/slab.h>
28 #include <linux/utsname.h>
29 #include <linux/workqueue.h>
31 #include <linux/in6.h>
33 #include <linux/rcupdate.h>
35 #include <linux/sunrpc/clnt.h>
36 #include <linux/sunrpc/addr.h>
37 #include <linux/sunrpc/rpc_pipe_fs.h>
38 #include <linux/sunrpc/metrics.h>
39 #include <linux/sunrpc/bc_xprt.h>
40 #include <trace/events/sunrpc.h>
46 # define RPCDBG_FACILITY RPCDBG_CALL
49 #define dprint_status(t) \
50 dprintk("RPC: %5u %s (status %d)\n", t->tk_pid, \
51 __func__, t->tk_status)
54 * All RPC clients are linked into this list
57 static DECLARE_WAIT_QUEUE_HEAD(destroy_wait);
60 static void call_start(struct rpc_task *task);
61 static void call_reserve(struct rpc_task *task);
62 static void call_reserveresult(struct rpc_task *task);
63 static void call_allocate(struct rpc_task *task);
64 static void call_decode(struct rpc_task *task);
65 static void call_bind(struct rpc_task *task);
66 static void call_bind_status(struct rpc_task *task);
67 static void call_transmit(struct rpc_task *task);
68 #if defined(CONFIG_SUNRPC_BACKCHANNEL)
69 static void call_bc_transmit(struct rpc_task *task);
70 #endif /* CONFIG_SUNRPC_BACKCHANNEL */
71 static void call_status(struct rpc_task *task);
72 static void call_transmit_status(struct rpc_task *task);
73 static void call_refresh(struct rpc_task *task);
74 static void call_refreshresult(struct rpc_task *task);
75 static void call_timeout(struct rpc_task *task);
76 static void call_connect(struct rpc_task *task);
77 static void call_connect_status(struct rpc_task *task);
79 static __be32 *rpc_encode_header(struct rpc_task *task);
80 static __be32 *rpc_verify_header(struct rpc_task *task);
81 static int rpc_ping(struct rpc_clnt *clnt);
83 static void rpc_register_client(struct rpc_clnt *clnt)
85 struct net *net = rpc_net_ns(clnt);
86 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
88 spin_lock(&sn->rpc_client_lock);
89 list_add(&clnt->cl_clients, &sn->all_clients);
90 spin_unlock(&sn->rpc_client_lock);
93 static void rpc_unregister_client(struct rpc_clnt *clnt)
95 struct net *net = rpc_net_ns(clnt);
96 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
98 spin_lock(&sn->rpc_client_lock);
99 list_del(&clnt->cl_clients);
100 spin_unlock(&sn->rpc_client_lock);
103 static void __rpc_clnt_remove_pipedir(struct rpc_clnt *clnt)
105 if (clnt->cl_dentry) {
106 if (clnt->cl_auth && clnt->cl_auth->au_ops->pipes_destroy)
107 clnt->cl_auth->au_ops->pipes_destroy(clnt->cl_auth);
108 rpc_remove_client_dir(clnt->cl_dentry, clnt);
110 clnt->cl_dentry = NULL;
113 static void rpc_clnt_remove_pipedir(struct rpc_clnt *clnt)
115 struct net *net = rpc_net_ns(clnt);
116 struct super_block *pipefs_sb;
118 pipefs_sb = rpc_get_sb_net(net);
120 __rpc_clnt_remove_pipedir(clnt);
125 static struct dentry *rpc_setup_pipedir_sb(struct super_block *sb,
126 struct rpc_clnt *clnt)
128 static uint32_t clntid;
129 const char *dir_name = clnt->cl_program->pipe_dir_name;
131 struct dentry *dir, *dentry;
133 dir = rpc_d_lookup_sb(sb, dir_name);
135 pr_info("RPC: pipefs directory doesn't exist: %s\n", dir_name);
139 snprintf(name, sizeof(name), "clnt%x", (unsigned int)clntid++);
140 name[sizeof(name) - 1] = '\0';
141 dentry = rpc_create_client_dir(dir, name, clnt);
144 if (dentry == ERR_PTR(-EEXIST))
146 printk(KERN_INFO "RPC: Couldn't create pipefs entry"
147 " %s/%s, error %ld\n",
148 dir_name, name, PTR_ERR(dentry));
156 rpc_setup_pipedir(struct super_block *pipefs_sb, struct rpc_clnt *clnt)
158 struct dentry *dentry;
160 if (clnt->cl_program->pipe_dir_name == NULL)
162 clnt->cl_dentry = NULL;
163 dentry = rpc_setup_pipedir_sb(pipefs_sb, clnt);
165 return PTR_ERR(dentry);
166 clnt->cl_dentry = dentry;
171 static int rpc_clnt_skip_event(struct rpc_clnt *clnt, unsigned long event)
173 if (clnt->cl_program->pipe_dir_name == NULL)
176 if (((event == RPC_PIPEFS_MOUNT) && clnt->cl_dentry) ||
177 ((event == RPC_PIPEFS_UMOUNT) && !clnt->cl_dentry))
179 if ((event == RPC_PIPEFS_MOUNT) && atomic_read(&clnt->cl_count) == 0)
184 static int __rpc_clnt_handle_event(struct rpc_clnt *clnt, unsigned long event,
185 struct super_block *sb)
187 struct dentry *dentry;
191 case RPC_PIPEFS_MOUNT:
192 dentry = rpc_setup_pipedir_sb(sb, clnt);
196 return PTR_ERR(dentry);
197 clnt->cl_dentry = dentry;
198 if (clnt->cl_auth->au_ops->pipes_create) {
199 err = clnt->cl_auth->au_ops->pipes_create(clnt->cl_auth);
201 __rpc_clnt_remove_pipedir(clnt);
204 case RPC_PIPEFS_UMOUNT:
205 __rpc_clnt_remove_pipedir(clnt);
208 printk(KERN_ERR "%s: unknown event: %ld\n", __func__, event);
214 static int __rpc_pipefs_event(struct rpc_clnt *clnt, unsigned long event,
215 struct super_block *sb)
219 for (;; clnt = clnt->cl_parent) {
220 if (!rpc_clnt_skip_event(clnt, event))
221 error = __rpc_clnt_handle_event(clnt, event, sb);
222 if (error || clnt == clnt->cl_parent)
228 static struct rpc_clnt *rpc_get_client_for_event(struct net *net, int event)
230 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
231 struct rpc_clnt *clnt;
233 spin_lock(&sn->rpc_client_lock);
234 list_for_each_entry(clnt, &sn->all_clients, cl_clients) {
235 if (rpc_clnt_skip_event(clnt, event))
237 spin_unlock(&sn->rpc_client_lock);
240 spin_unlock(&sn->rpc_client_lock);
244 static int rpc_pipefs_event(struct notifier_block *nb, unsigned long event,
247 struct super_block *sb = ptr;
248 struct rpc_clnt *clnt;
251 while ((clnt = rpc_get_client_for_event(sb->s_fs_info, event))) {
252 error = __rpc_pipefs_event(clnt, event, sb);
259 static struct notifier_block rpc_clients_block = {
260 .notifier_call = rpc_pipefs_event,
261 .priority = SUNRPC_PIPEFS_RPC_PRIO,
264 int rpc_clients_notifier_register(void)
266 return rpc_pipefs_notifier_register(&rpc_clients_block);
269 void rpc_clients_notifier_unregister(void)
271 return rpc_pipefs_notifier_unregister(&rpc_clients_block);
274 static void rpc_clnt_set_nodename(struct rpc_clnt *clnt, const char *nodename)
276 clnt->cl_nodelen = strlen(nodename);
277 if (clnt->cl_nodelen > UNX_MAXNODENAME)
278 clnt->cl_nodelen = UNX_MAXNODENAME;
279 memcpy(clnt->cl_nodename, nodename, clnt->cl_nodelen);
282 static int rpc_client_register(const struct rpc_create_args *args,
283 struct rpc_clnt *clnt)
285 struct rpc_auth_create_args auth_args = {
286 .pseudoflavor = args->authflavor,
287 .target_name = args->client_name,
289 struct rpc_auth *auth;
290 struct net *net = rpc_net_ns(clnt);
291 struct super_block *pipefs_sb;
294 pipefs_sb = rpc_get_sb_net(net);
296 err = rpc_setup_pipedir(pipefs_sb, clnt);
301 rpc_register_client(clnt);
305 auth = rpcauth_create(&auth_args, clnt);
307 dprintk("RPC: Couldn't create auth handle (flavor %u)\n",
314 pipefs_sb = rpc_get_sb_net(net);
315 rpc_unregister_client(clnt);
316 __rpc_clnt_remove_pipedir(clnt);
323 static struct rpc_clnt * rpc_new_client(const struct rpc_create_args *args, struct rpc_xprt *xprt)
325 const struct rpc_program *program = args->program;
326 const struct rpc_version *version;
327 struct rpc_clnt *clnt = NULL;
330 /* sanity check the name before trying to print it */
331 dprintk("RPC: creating %s client for %s (xprt %p)\n",
332 program->name, args->servername, xprt);
339 if (args->version >= program->nrvers)
341 version = program->version[args->version];
346 clnt = kzalloc(sizeof(*clnt), GFP_KERNEL);
349 clnt->cl_parent = clnt;
351 rcu_assign_pointer(clnt->cl_xprt, xprt);
352 clnt->cl_procinfo = version->procs;
353 clnt->cl_maxproc = version->nrprocs;
354 clnt->cl_prog = args->prognumber ? : program->number;
355 clnt->cl_vers = version->number;
356 clnt->cl_stats = program->stats;
357 clnt->cl_metrics = rpc_alloc_iostats(clnt);
358 rpc_init_pipe_dir_head(&clnt->cl_pipedir_objects);
360 if (clnt->cl_metrics == NULL)
362 clnt->cl_program = program;
363 INIT_LIST_HEAD(&clnt->cl_tasks);
364 spin_lock_init(&clnt->cl_lock);
366 if (!xprt_bound(xprt))
367 clnt->cl_autobind = 1;
369 clnt->cl_timeout = xprt->timeout;
370 if (args->timeout != NULL) {
371 memcpy(&clnt->cl_timeout_default, args->timeout,
372 sizeof(clnt->cl_timeout_default));
373 clnt->cl_timeout = &clnt->cl_timeout_default;
376 clnt->cl_rtt = &clnt->cl_rtt_default;
377 rpc_init_rtt(&clnt->cl_rtt_default, clnt->cl_timeout->to_initval);
379 atomic_set(&clnt->cl_count, 1);
381 /* save the nodename */
382 rpc_clnt_set_nodename(clnt, utsname()->nodename);
384 err = rpc_client_register(args, clnt);
390 rpc_free_iostats(clnt->cl_metrics);
401 * rpc_create - create an RPC client and transport with one call
402 * @args: rpc_clnt create argument structure
404 * Creates and initializes an RPC transport and an RPC client.
406 * It can ping the server in order to determine if it is up, and to see if
407 * it supports this program and version. RPC_CLNT_CREATE_NOPING disables
408 * this behavior so asynchronous tasks can also use rpc_create.
410 struct rpc_clnt *rpc_create(struct rpc_create_args *args)
412 struct rpc_xprt *xprt;
413 struct rpc_clnt *clnt;
414 struct xprt_create xprtargs = {
416 .ident = args->protocol,
417 .srcaddr = args->saddress,
418 .dstaddr = args->address,
419 .addrlen = args->addrsize,
420 .servername = args->servername,
421 .bc_xprt = args->bc_xprt,
425 if (args->flags & RPC_CLNT_CREATE_INFINITE_SLOTS)
426 xprtargs.flags |= XPRT_CREATE_INFINITE_SLOTS;
427 if (args->flags & RPC_CLNT_CREATE_NO_IDLE_TIMEOUT)
428 xprtargs.flags |= XPRT_CREATE_NO_IDLE_TIMEOUT;
430 * If the caller chooses not to specify a hostname, whip
431 * up a string representation of the passed-in address.
433 if (xprtargs.servername == NULL) {
434 struct sockaddr_un *sun =
435 (struct sockaddr_un *)args->address;
436 struct sockaddr_in *sin =
437 (struct sockaddr_in *)args->address;
438 struct sockaddr_in6 *sin6 =
439 (struct sockaddr_in6 *)args->address;
441 servername[0] = '\0';
442 switch (args->address->sa_family) {
444 snprintf(servername, sizeof(servername), "%s",
448 snprintf(servername, sizeof(servername), "%pI4",
449 &sin->sin_addr.s_addr);
452 snprintf(servername, sizeof(servername), "%pI6",
456 /* caller wants default server name, but
457 * address family isn't recognized. */
458 return ERR_PTR(-EINVAL);
460 xprtargs.servername = servername;
463 xprt = xprt_create_transport(&xprtargs);
465 return (struct rpc_clnt *)xprt;
468 * By default, kernel RPC client connects from a reserved port.
469 * CAP_NET_BIND_SERVICE will not be set for unprivileged requesters,
470 * but it is always enabled for rpciod, which handles the connect
474 if (args->flags & RPC_CLNT_CREATE_NONPRIVPORT)
477 clnt = rpc_new_client(args, xprt);
481 if (!(args->flags & RPC_CLNT_CREATE_NOPING)) {
482 int err = rpc_ping(clnt);
484 rpc_shutdown_client(clnt);
489 clnt->cl_softrtry = 1;
490 if (args->flags & RPC_CLNT_CREATE_HARDRTRY)
491 clnt->cl_softrtry = 0;
493 if (args->flags & RPC_CLNT_CREATE_AUTOBIND)
494 clnt->cl_autobind = 1;
495 if (args->flags & RPC_CLNT_CREATE_DISCRTRY)
496 clnt->cl_discrtry = 1;
497 if (!(args->flags & RPC_CLNT_CREATE_QUIET))
502 EXPORT_SYMBOL_GPL(rpc_create);
505 * This function clones the RPC client structure. It allows us to share the
506 * same transport while varying parameters such as the authentication
509 static struct rpc_clnt *__rpc_clone_client(struct rpc_create_args *args,
510 struct rpc_clnt *clnt)
512 struct rpc_xprt *xprt;
513 struct rpc_clnt *new;
518 xprt = xprt_get(rcu_dereference(clnt->cl_xprt));
522 args->servername = xprt->servername;
524 new = rpc_new_client(args, xprt);
530 atomic_inc(&clnt->cl_count);
531 new->cl_parent = clnt;
533 /* Turn off autobind on clones */
534 new->cl_autobind = 0;
535 new->cl_softrtry = clnt->cl_softrtry;
536 new->cl_discrtry = clnt->cl_discrtry;
537 new->cl_chatty = clnt->cl_chatty;
541 dprintk("RPC: %s: returned error %d\n", __func__, err);
546 * rpc_clone_client - Clone an RPC client structure
548 * @clnt: RPC client whose parameters are copied
550 * Returns a fresh RPC client or an ERR_PTR.
552 struct rpc_clnt *rpc_clone_client(struct rpc_clnt *clnt)
554 struct rpc_create_args args = {
555 .program = clnt->cl_program,
556 .prognumber = clnt->cl_prog,
557 .version = clnt->cl_vers,
558 .authflavor = clnt->cl_auth->au_flavor,
560 return __rpc_clone_client(&args, clnt);
562 EXPORT_SYMBOL_GPL(rpc_clone_client);
565 * rpc_clone_client_set_auth - Clone an RPC client structure and set its auth
567 * @clnt: RPC client whose parameters are copied
568 * @flavor: security flavor for new client
570 * Returns a fresh RPC client or an ERR_PTR.
573 rpc_clone_client_set_auth(struct rpc_clnt *clnt, rpc_authflavor_t flavor)
575 struct rpc_create_args args = {
576 .program = clnt->cl_program,
577 .prognumber = clnt->cl_prog,
578 .version = clnt->cl_vers,
579 .authflavor = flavor,
581 return __rpc_clone_client(&args, clnt);
583 EXPORT_SYMBOL_GPL(rpc_clone_client_set_auth);
586 * Kill all tasks for the given client.
587 * XXX: kill their descendants as well?
589 void rpc_killall_tasks(struct rpc_clnt *clnt)
591 struct rpc_task *rovr;
594 if (list_empty(&clnt->cl_tasks))
596 dprintk("RPC: killing all tasks for client %p\n", clnt);
598 * Spin lock all_tasks to prevent changes...
600 spin_lock(&clnt->cl_lock);
601 list_for_each_entry(rovr, &clnt->cl_tasks, tk_task) {
602 if (!RPC_IS_ACTIVATED(rovr))
604 if (!(rovr->tk_flags & RPC_TASK_KILLED)) {
605 rovr->tk_flags |= RPC_TASK_KILLED;
606 rpc_exit(rovr, -EIO);
607 if (RPC_IS_QUEUED(rovr))
608 rpc_wake_up_queued_task(rovr->tk_waitqueue,
612 spin_unlock(&clnt->cl_lock);
614 EXPORT_SYMBOL_GPL(rpc_killall_tasks);
617 * Properly shut down an RPC client, terminating all outstanding
620 void rpc_shutdown_client(struct rpc_clnt *clnt)
624 dprintk_rcu("RPC: shutting down %s client for %s\n",
625 clnt->cl_program->name,
626 rcu_dereference(clnt->cl_xprt)->servername);
628 while (!list_empty(&clnt->cl_tasks)) {
629 rpc_killall_tasks(clnt);
630 wait_event_timeout(destroy_wait,
631 list_empty(&clnt->cl_tasks), 1*HZ);
634 rpc_release_client(clnt);
636 EXPORT_SYMBOL_GPL(rpc_shutdown_client);
642 rpc_free_client(struct rpc_clnt *clnt)
644 dprintk_rcu("RPC: destroying %s client for %s\n",
645 clnt->cl_program->name,
646 rcu_dereference(clnt->cl_xprt)->servername);
647 if (clnt->cl_parent != clnt)
648 rpc_release_client(clnt->cl_parent);
649 rpc_clnt_remove_pipedir(clnt);
650 rpc_unregister_client(clnt);
651 rpc_free_iostats(clnt->cl_metrics);
652 clnt->cl_metrics = NULL;
653 xprt_put(rcu_dereference_raw(clnt->cl_xprt));
662 rpc_free_auth(struct rpc_clnt *clnt)
664 if (clnt->cl_auth == NULL) {
665 rpc_free_client(clnt);
670 * Note: RPCSEC_GSS may need to send NULL RPC calls in order to
671 * release remaining GSS contexts. This mechanism ensures
672 * that it can do so safely.
674 atomic_inc(&clnt->cl_count);
675 rpcauth_release(clnt->cl_auth);
676 clnt->cl_auth = NULL;
677 if (atomic_dec_and_test(&clnt->cl_count))
678 rpc_free_client(clnt);
682 * Release reference to the RPC client
685 rpc_release_client(struct rpc_clnt *clnt)
687 dprintk("RPC: rpc_release_client(%p)\n", clnt);
689 if (list_empty(&clnt->cl_tasks))
690 wake_up(&destroy_wait);
691 if (atomic_dec_and_test(&clnt->cl_count))
694 EXPORT_SYMBOL_GPL(rpc_release_client);
697 * rpc_bind_new_program - bind a new RPC program to an existing client
698 * @old: old rpc_client
699 * @program: rpc program to set
700 * @vers: rpc program version
702 * Clones the rpc client and sets up a new RPC program. This is mainly
703 * of use for enabling different RPC programs to share the same transport.
704 * The Sun NFSv2/v3 ACL protocol can do this.
706 struct rpc_clnt *rpc_bind_new_program(struct rpc_clnt *old,
707 const struct rpc_program *program,
710 struct rpc_create_args args = {
712 .prognumber = program->number,
714 .authflavor = old->cl_auth->au_flavor,
716 struct rpc_clnt *clnt;
719 clnt = __rpc_clone_client(&args, old);
722 err = rpc_ping(clnt);
724 rpc_shutdown_client(clnt);
730 EXPORT_SYMBOL_GPL(rpc_bind_new_program);
732 void rpc_task_release_client(struct rpc_task *task)
734 struct rpc_clnt *clnt = task->tk_client;
737 /* Remove from client task list */
738 spin_lock(&clnt->cl_lock);
739 list_del(&task->tk_task);
740 spin_unlock(&clnt->cl_lock);
741 task->tk_client = NULL;
743 rpc_release_client(clnt);
748 void rpc_task_set_client(struct rpc_task *task, struct rpc_clnt *clnt)
751 rpc_task_release_client(task);
752 task->tk_client = clnt;
753 atomic_inc(&clnt->cl_count);
754 if (clnt->cl_softrtry)
755 task->tk_flags |= RPC_TASK_SOFT;
756 if (sk_memalloc_socks()) {
757 struct rpc_xprt *xprt;
760 xprt = rcu_dereference(clnt->cl_xprt);
762 task->tk_flags |= RPC_TASK_SWAPPER;
765 /* Add to the client's list of all tasks */
766 spin_lock(&clnt->cl_lock);
767 list_add_tail(&task->tk_task, &clnt->cl_tasks);
768 spin_unlock(&clnt->cl_lock);
772 void rpc_task_reset_client(struct rpc_task *task, struct rpc_clnt *clnt)
774 rpc_task_release_client(task);
775 rpc_task_set_client(task, clnt);
777 EXPORT_SYMBOL_GPL(rpc_task_reset_client);
781 rpc_task_set_rpc_message(struct rpc_task *task, const struct rpc_message *msg)
784 task->tk_msg.rpc_proc = msg->rpc_proc;
785 task->tk_msg.rpc_argp = msg->rpc_argp;
786 task->tk_msg.rpc_resp = msg->rpc_resp;
787 if (msg->rpc_cred != NULL)
788 task->tk_msg.rpc_cred = get_rpccred(msg->rpc_cred);
793 * Default callback for async RPC calls
796 rpc_default_callback(struct rpc_task *task, void *data)
800 static const struct rpc_call_ops rpc_default_ops = {
801 .rpc_call_done = rpc_default_callback,
805 * rpc_run_task - Allocate a new RPC task, then run rpc_execute against it
806 * @task_setup_data: pointer to task initialisation data
808 struct rpc_task *rpc_run_task(const struct rpc_task_setup *task_setup_data)
810 struct rpc_task *task;
812 task = rpc_new_task(task_setup_data);
816 rpc_task_set_client(task, task_setup_data->rpc_client);
817 rpc_task_set_rpc_message(task, task_setup_data->rpc_message);
819 if (task->tk_action == NULL)
820 rpc_call_start(task);
822 atomic_inc(&task->tk_count);
827 EXPORT_SYMBOL_GPL(rpc_run_task);
830 * rpc_call_sync - Perform a synchronous RPC call
831 * @clnt: pointer to RPC client
832 * @msg: RPC call parameters
833 * @flags: RPC call flags
835 int rpc_call_sync(struct rpc_clnt *clnt, const struct rpc_message *msg, int flags)
837 struct rpc_task *task;
838 struct rpc_task_setup task_setup_data = {
841 .callback_ops = &rpc_default_ops,
846 WARN_ON_ONCE(flags & RPC_TASK_ASYNC);
847 if (flags & RPC_TASK_ASYNC) {
848 rpc_release_calldata(task_setup_data.callback_ops,
849 task_setup_data.callback_data);
853 task = rpc_run_task(&task_setup_data);
855 return PTR_ERR(task);
856 status = task->tk_status;
860 EXPORT_SYMBOL_GPL(rpc_call_sync);
863 * rpc_call_async - Perform an asynchronous RPC call
864 * @clnt: pointer to RPC client
865 * @msg: RPC call parameters
866 * @flags: RPC call flags
867 * @tk_ops: RPC call ops
868 * @data: user call data
871 rpc_call_async(struct rpc_clnt *clnt, const struct rpc_message *msg, int flags,
872 const struct rpc_call_ops *tk_ops, void *data)
874 struct rpc_task *task;
875 struct rpc_task_setup task_setup_data = {
878 .callback_ops = tk_ops,
879 .callback_data = data,
880 .flags = flags|RPC_TASK_ASYNC,
883 task = rpc_run_task(&task_setup_data);
885 return PTR_ERR(task);
889 EXPORT_SYMBOL_GPL(rpc_call_async);
891 #if defined(CONFIG_SUNRPC_BACKCHANNEL)
893 * rpc_run_bc_task - Allocate a new RPC task for backchannel use, then run
894 * rpc_execute against it
896 * @tk_ops: RPC call ops
898 struct rpc_task *rpc_run_bc_task(struct rpc_rqst *req,
899 const struct rpc_call_ops *tk_ops)
901 struct rpc_task *task;
902 struct xdr_buf *xbufp = &req->rq_snd_buf;
903 struct rpc_task_setup task_setup_data = {
904 .callback_ops = tk_ops,
907 dprintk("RPC: rpc_run_bc_task req= %p\n", req);
909 * Create an rpc_task to send the data
911 task = rpc_new_task(&task_setup_data);
913 xprt_free_bc_request(req);
916 task->tk_rqstp = req;
919 * Set up the xdr_buf length.
920 * This also indicates that the buffer is XDR encoded already.
922 xbufp->len = xbufp->head[0].iov_len + xbufp->page_len +
923 xbufp->tail[0].iov_len;
925 task->tk_action = call_bc_transmit;
926 atomic_inc(&task->tk_count);
927 WARN_ON_ONCE(atomic_read(&task->tk_count) != 2);
931 dprintk("RPC: rpc_run_bc_task: task= %p\n", task);
934 #endif /* CONFIG_SUNRPC_BACKCHANNEL */
937 rpc_call_start(struct rpc_task *task)
939 task->tk_action = call_start;
941 EXPORT_SYMBOL_GPL(rpc_call_start);
944 * rpc_peeraddr - extract remote peer address from clnt's xprt
945 * @clnt: RPC client structure
946 * @buf: target buffer
947 * @bufsize: length of target buffer
949 * Returns the number of bytes that are actually in the stored address.
951 size_t rpc_peeraddr(struct rpc_clnt *clnt, struct sockaddr *buf, size_t bufsize)
954 struct rpc_xprt *xprt;
957 xprt = rcu_dereference(clnt->cl_xprt);
959 bytes = xprt->addrlen;
962 memcpy(buf, &xprt->addr, bytes);
967 EXPORT_SYMBOL_GPL(rpc_peeraddr);
970 * rpc_peeraddr2str - return remote peer address in printable format
971 * @clnt: RPC client structure
972 * @format: address format
974 * NB: the lifetime of the memory referenced by the returned pointer is
975 * the same as the rpc_xprt itself. As long as the caller uses this
976 * pointer, it must hold the RCU read lock.
978 const char *rpc_peeraddr2str(struct rpc_clnt *clnt,
979 enum rpc_display_format_t format)
981 struct rpc_xprt *xprt;
983 xprt = rcu_dereference(clnt->cl_xprt);
985 if (xprt->address_strings[format] != NULL)
986 return xprt->address_strings[format];
988 return "unprintable";
990 EXPORT_SYMBOL_GPL(rpc_peeraddr2str);
992 static const struct sockaddr_in rpc_inaddr_loopback = {
993 .sin_family = AF_INET,
994 .sin_addr.s_addr = htonl(INADDR_ANY),
997 static const struct sockaddr_in6 rpc_in6addr_loopback = {
998 .sin6_family = AF_INET6,
999 .sin6_addr = IN6ADDR_ANY_INIT,
1003 * Try a getsockname() on a connected datagram socket. Using a
1004 * connected datagram socket prevents leaving a socket in TIME_WAIT.
1005 * This conserves the ephemeral port number space.
1007 * Returns zero and fills in "buf" if successful; otherwise, a
1008 * negative errno is returned.
1010 static int rpc_sockname(struct net *net, struct sockaddr *sap, size_t salen,
1011 struct sockaddr *buf, int buflen)
1013 struct socket *sock;
1016 err = __sock_create(net, sap->sa_family,
1017 SOCK_DGRAM, IPPROTO_UDP, &sock, 1);
1019 dprintk("RPC: can't create UDP socket (%d)\n", err);
1023 switch (sap->sa_family) {
1025 err = kernel_bind(sock,
1026 (struct sockaddr *)&rpc_inaddr_loopback,
1027 sizeof(rpc_inaddr_loopback));
1030 err = kernel_bind(sock,
1031 (struct sockaddr *)&rpc_in6addr_loopback,
1032 sizeof(rpc_in6addr_loopback));
1035 err = -EAFNOSUPPORT;
1039 dprintk("RPC: can't bind UDP socket (%d)\n", err);
1043 err = kernel_connect(sock, sap, salen, 0);
1045 dprintk("RPC: can't connect UDP socket (%d)\n", err);
1049 err = kernel_getsockname(sock, buf, &buflen);
1051 dprintk("RPC: getsockname failed (%d)\n", err);
1056 if (buf->sa_family == AF_INET6) {
1057 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)buf;
1058 sin6->sin6_scope_id = 0;
1060 dprintk("RPC: %s succeeded\n", __func__);
1069 * Scraping a connected socket failed, so we don't have a useable
1070 * local address. Fallback: generate an address that will prevent
1071 * the server from calling us back.
1073 * Returns zero and fills in "buf" if successful; otherwise, a
1074 * negative errno is returned.
1076 static int rpc_anyaddr(int family, struct sockaddr *buf, size_t buflen)
1080 if (buflen < sizeof(rpc_inaddr_loopback))
1082 memcpy(buf, &rpc_inaddr_loopback,
1083 sizeof(rpc_inaddr_loopback));
1086 if (buflen < sizeof(rpc_in6addr_loopback))
1088 memcpy(buf, &rpc_in6addr_loopback,
1089 sizeof(rpc_in6addr_loopback));
1091 dprintk("RPC: %s: address family not supported\n",
1093 return -EAFNOSUPPORT;
1095 dprintk("RPC: %s: succeeded\n", __func__);
1100 * rpc_localaddr - discover local endpoint address for an RPC client
1101 * @clnt: RPC client structure
1102 * @buf: target buffer
1103 * @buflen: size of target buffer, in bytes
1105 * Returns zero and fills in "buf" and "buflen" if successful;
1106 * otherwise, a negative errno is returned.
1108 * This works even if the underlying transport is not currently connected,
1109 * or if the upper layer never previously provided a source address.
1111 * The result of this function call is transient: multiple calls in
1112 * succession may give different results, depending on how local
1113 * networking configuration changes over time.
1115 int rpc_localaddr(struct rpc_clnt *clnt, struct sockaddr *buf, size_t buflen)
1117 struct sockaddr_storage address;
1118 struct sockaddr *sap = (struct sockaddr *)&address;
1119 struct rpc_xprt *xprt;
1125 xprt = rcu_dereference(clnt->cl_xprt);
1126 salen = xprt->addrlen;
1127 memcpy(sap, &xprt->addr, salen);
1128 net = get_net(xprt->xprt_net);
1131 rpc_set_port(sap, 0);
1132 err = rpc_sockname(net, sap, salen, buf, buflen);
1135 /* Couldn't discover local address, return ANYADDR */
1136 return rpc_anyaddr(sap->sa_family, buf, buflen);
1139 EXPORT_SYMBOL_GPL(rpc_localaddr);
1142 rpc_setbufsize(struct rpc_clnt *clnt, unsigned int sndsize, unsigned int rcvsize)
1144 struct rpc_xprt *xprt;
1147 xprt = rcu_dereference(clnt->cl_xprt);
1148 if (xprt->ops->set_buffer_size)
1149 xprt->ops->set_buffer_size(xprt, sndsize, rcvsize);
1152 EXPORT_SYMBOL_GPL(rpc_setbufsize);
1155 * rpc_protocol - Get transport protocol number for an RPC client
1156 * @clnt: RPC client to query
1159 int rpc_protocol(struct rpc_clnt *clnt)
1164 protocol = rcu_dereference(clnt->cl_xprt)->prot;
1168 EXPORT_SYMBOL_GPL(rpc_protocol);
1171 * rpc_net_ns - Get the network namespace for this RPC client
1172 * @clnt: RPC client to query
1175 struct net *rpc_net_ns(struct rpc_clnt *clnt)
1180 ret = rcu_dereference(clnt->cl_xprt)->xprt_net;
1184 EXPORT_SYMBOL_GPL(rpc_net_ns);
1187 * rpc_max_payload - Get maximum payload size for a transport, in bytes
1188 * @clnt: RPC client to query
1190 * For stream transports, this is one RPC record fragment (see RFC
1191 * 1831), as we don't support multi-record requests yet. For datagram
1192 * transports, this is the size of an IP packet minus the IP, UDP, and
1195 size_t rpc_max_payload(struct rpc_clnt *clnt)
1200 ret = rcu_dereference(clnt->cl_xprt)->max_payload;
1204 EXPORT_SYMBOL_GPL(rpc_max_payload);
1207 * rpc_get_timeout - Get timeout for transport in units of HZ
1208 * @clnt: RPC client to query
1210 unsigned long rpc_get_timeout(struct rpc_clnt *clnt)
1215 ret = rcu_dereference(clnt->cl_xprt)->timeout->to_initval;
1219 EXPORT_SYMBOL_GPL(rpc_get_timeout);
1222 * rpc_force_rebind - force transport to check that remote port is unchanged
1223 * @clnt: client to rebind
1226 void rpc_force_rebind(struct rpc_clnt *clnt)
1228 if (clnt->cl_autobind) {
1230 xprt_clear_bound(rcu_dereference(clnt->cl_xprt));
1234 EXPORT_SYMBOL_GPL(rpc_force_rebind);
1237 * Restart an (async) RPC call from the call_prepare state.
1238 * Usually called from within the exit handler.
1241 rpc_restart_call_prepare(struct rpc_task *task)
1243 if (RPC_ASSASSINATED(task))
1245 task->tk_action = call_start;
1246 if (task->tk_ops->rpc_call_prepare != NULL)
1247 task->tk_action = rpc_prepare_task;
1250 EXPORT_SYMBOL_GPL(rpc_restart_call_prepare);
1253 * Restart an (async) RPC call. Usually called from within the
1257 rpc_restart_call(struct rpc_task *task)
1259 if (RPC_ASSASSINATED(task))
1261 task->tk_action = call_start;
1264 EXPORT_SYMBOL_GPL(rpc_restart_call);
1267 static const char *rpc_proc_name(const struct rpc_task *task)
1269 const struct rpc_procinfo *proc = task->tk_msg.rpc_proc;
1273 return proc->p_name;
1284 * Other FSM states can be visited zero or more times, but
1285 * this state is visited exactly once for each RPC.
1288 call_start(struct rpc_task *task)
1290 struct rpc_clnt *clnt = task->tk_client;
1292 dprintk("RPC: %5u call_start %s%d proc %s (%s)\n", task->tk_pid,
1293 clnt->cl_program->name, clnt->cl_vers,
1294 rpc_proc_name(task),
1295 (RPC_IS_ASYNC(task) ? "async" : "sync"));
1297 /* Increment call count */
1298 task->tk_msg.rpc_proc->p_count++;
1299 clnt->cl_stats->rpccnt++;
1300 task->tk_action = call_reserve;
1304 * 1. Reserve an RPC call slot
1307 call_reserve(struct rpc_task *task)
1309 dprint_status(task);
1311 task->tk_status = 0;
1312 task->tk_action = call_reserveresult;
1316 static void call_retry_reserve(struct rpc_task *task);
1319 * 1b. Grok the result of xprt_reserve()
1322 call_reserveresult(struct rpc_task *task)
1324 int status = task->tk_status;
1326 dprint_status(task);
1329 * After a call to xprt_reserve(), we must have either
1330 * a request slot or else an error status.
1332 task->tk_status = 0;
1334 if (task->tk_rqstp) {
1335 task->tk_action = call_refresh;
1339 printk(KERN_ERR "%s: status=%d, but no request slot, exiting\n",
1341 rpc_exit(task, -EIO);
1346 * Even though there was an error, we may have acquired
1347 * a request slot somehow. Make sure not to leak it.
1349 if (task->tk_rqstp) {
1350 printk(KERN_ERR "%s: status=%d, request allocated anyway\n",
1357 rpc_delay(task, HZ >> 2);
1358 case -EAGAIN: /* woken up; retry */
1359 task->tk_action = call_retry_reserve;
1361 case -EIO: /* probably a shutdown */
1364 printk(KERN_ERR "%s: unrecognized error %d, exiting\n",
1368 rpc_exit(task, status);
1372 * 1c. Retry reserving an RPC call slot
1375 call_retry_reserve(struct rpc_task *task)
1377 dprint_status(task);
1379 task->tk_status = 0;
1380 task->tk_action = call_reserveresult;
1381 xprt_retry_reserve(task);
1385 * 2. Bind and/or refresh the credentials
1388 call_refresh(struct rpc_task *task)
1390 dprint_status(task);
1392 task->tk_action = call_refreshresult;
1393 task->tk_status = 0;
1394 task->tk_client->cl_stats->rpcauthrefresh++;
1395 rpcauth_refreshcred(task);
1399 * 2a. Process the results of a credential refresh
1402 call_refreshresult(struct rpc_task *task)
1404 int status = task->tk_status;
1406 dprint_status(task);
1408 task->tk_status = 0;
1409 task->tk_action = call_refresh;
1412 if (rpcauth_uptodatecred(task))
1413 task->tk_action = call_allocate;
1416 rpc_delay(task, 3*HZ);
1420 if (!task->tk_cred_retry)
1422 task->tk_cred_retry--;
1423 dprintk("RPC: %5u %s: retry refresh creds\n",
1424 task->tk_pid, __func__);
1427 dprintk("RPC: %5u %s: refresh creds failed with error %d\n",
1428 task->tk_pid, __func__, status);
1429 rpc_exit(task, status);
1433 * 2b. Allocate the buffer. For details, see sched.c:rpc_malloc.
1434 * (Note: buffer memory is freed in xprt_release).
1437 call_allocate(struct rpc_task *task)
1439 unsigned int slack = task->tk_rqstp->rq_cred->cr_auth->au_cslack;
1440 struct rpc_rqst *req = task->tk_rqstp;
1441 struct rpc_xprt *xprt = req->rq_xprt;
1442 struct rpc_procinfo *proc = task->tk_msg.rpc_proc;
1444 dprint_status(task);
1446 task->tk_status = 0;
1447 task->tk_action = call_bind;
1452 if (proc->p_proc != 0) {
1453 BUG_ON(proc->p_arglen == 0);
1454 if (proc->p_decode != NULL)
1455 BUG_ON(proc->p_replen == 0);
1459 * Calculate the size (in quads) of the RPC call
1460 * and reply headers, and convert both values
1463 req->rq_callsize = RPC_CALLHDRSIZE + (slack << 1) + proc->p_arglen;
1464 req->rq_callsize <<= 2;
1465 req->rq_rcvsize = RPC_REPHDRSIZE + slack + proc->p_replen;
1466 req->rq_rcvsize <<= 2;
1468 req->rq_buffer = xprt->ops->buf_alloc(task,
1469 req->rq_callsize + req->rq_rcvsize);
1470 if (req->rq_buffer != NULL)
1473 dprintk("RPC: %5u rpc_buffer allocation failed\n", task->tk_pid);
1475 if (RPC_IS_ASYNC(task) || !fatal_signal_pending(current)) {
1476 task->tk_action = call_allocate;
1477 rpc_delay(task, HZ>>4);
1481 rpc_exit(task, -ERESTARTSYS);
1485 rpc_task_need_encode(struct rpc_task *task)
1487 return task->tk_rqstp->rq_snd_buf.len == 0;
1491 rpc_task_force_reencode(struct rpc_task *task)
1493 task->tk_rqstp->rq_snd_buf.len = 0;
1494 task->tk_rqstp->rq_bytes_sent = 0;
1498 rpc_xdr_buf_init(struct xdr_buf *buf, void *start, size_t len)
1500 buf->head[0].iov_base = start;
1501 buf->head[0].iov_len = len;
1502 buf->tail[0].iov_len = 0;
1510 * 3. Encode arguments of an RPC call
1513 rpc_xdr_encode(struct rpc_task *task)
1515 struct rpc_rqst *req = task->tk_rqstp;
1519 dprint_status(task);
1521 rpc_xdr_buf_init(&req->rq_snd_buf,
1524 rpc_xdr_buf_init(&req->rq_rcv_buf,
1525 (char *)req->rq_buffer + req->rq_callsize,
1528 p = rpc_encode_header(task);
1530 printk(KERN_INFO "RPC: couldn't encode RPC header, exit EIO\n");
1531 rpc_exit(task, -EIO);
1535 encode = task->tk_msg.rpc_proc->p_encode;
1539 task->tk_status = rpcauth_wrap_req(task, encode, req, p,
1540 task->tk_msg.rpc_argp);
1544 * 4. Get the server port number if not yet set
1547 call_bind(struct rpc_task *task)
1549 struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
1551 dprint_status(task);
1553 task->tk_action = call_connect;
1554 if (!xprt_bound(xprt)) {
1555 task->tk_action = call_bind_status;
1556 task->tk_timeout = xprt->bind_timeout;
1557 xprt->ops->rpcbind(task);
1562 * 4a. Sort out bind result
1565 call_bind_status(struct rpc_task *task)
1569 if (task->tk_status >= 0) {
1570 dprint_status(task);
1571 task->tk_status = 0;
1572 task->tk_action = call_connect;
1576 trace_rpc_bind_status(task);
1577 switch (task->tk_status) {
1579 dprintk("RPC: %5u rpcbind out of memory\n", task->tk_pid);
1580 rpc_delay(task, HZ >> 2);
1583 dprintk("RPC: %5u remote rpcbind: RPC program/version "
1584 "unavailable\n", task->tk_pid);
1585 /* fail immediately if this is an RPC ping */
1586 if (task->tk_msg.rpc_proc->p_proc == 0) {
1587 status = -EOPNOTSUPP;
1590 if (task->tk_rebind_retry == 0)
1592 task->tk_rebind_retry--;
1593 rpc_delay(task, 3*HZ);
1596 dprintk("RPC: %5u rpcbind request timed out\n",
1600 /* server doesn't support any rpcbind version we know of */
1601 dprintk("RPC: %5u unrecognized remote rpcbind service\n",
1604 case -EPROTONOSUPPORT:
1605 dprintk("RPC: %5u remote rpcbind version unavailable, retrying\n",
1607 task->tk_status = 0;
1608 task->tk_action = call_bind;
1610 case -ECONNREFUSED: /* connection problems */
1617 dprintk("RPC: %5u remote rpcbind unreachable: %d\n",
1618 task->tk_pid, task->tk_status);
1619 if (!RPC_IS_SOFTCONN(task)) {
1620 rpc_delay(task, 5*HZ);
1623 status = task->tk_status;
1626 dprintk("RPC: %5u unrecognized rpcbind error (%d)\n",
1627 task->tk_pid, -task->tk_status);
1630 rpc_exit(task, status);
1634 task->tk_action = call_timeout;
1638 * 4b. Connect to the RPC server
1641 call_connect(struct rpc_task *task)
1643 struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
1645 dprintk("RPC: %5u call_connect xprt %p %s connected\n",
1647 (xprt_connected(xprt) ? "is" : "is not"));
1649 task->tk_action = call_transmit;
1650 if (!xprt_connected(xprt)) {
1651 task->tk_action = call_connect_status;
1652 if (task->tk_status < 0)
1659 * 4c. Sort out connect result
1662 call_connect_status(struct rpc_task *task)
1664 struct rpc_clnt *clnt = task->tk_client;
1665 int status = task->tk_status;
1667 dprint_status(task);
1669 trace_rpc_connect_status(task, status);
1671 /* if soft mounted, test if we've timed out */
1673 task->tk_action = call_timeout;
1678 if (RPC_IS_SOFTCONN(task))
1680 /* retry with existing socket, after a delay */
1683 task->tk_status = 0;
1684 clnt->cl_stats->netreconn++;
1685 task->tk_action = call_transmit;
1688 rpc_exit(task, status);
1692 * 5. Transmit the RPC request, and wait for reply
1695 call_transmit(struct rpc_task *task)
1697 dprint_status(task);
1699 task->tk_action = call_status;
1700 if (task->tk_status < 0)
1702 task->tk_status = xprt_prepare_transmit(task);
1703 if (task->tk_status != 0)
1705 task->tk_action = call_transmit_status;
1706 /* Encode here so that rpcsec_gss can use correct sequence number. */
1707 if (rpc_task_need_encode(task)) {
1708 rpc_xdr_encode(task);
1709 /* Did the encode result in an error condition? */
1710 if (task->tk_status != 0) {
1711 /* Was the error nonfatal? */
1712 if (task->tk_status == -EAGAIN)
1713 rpc_delay(task, HZ >> 4);
1715 rpc_exit(task, task->tk_status);
1719 xprt_transmit(task);
1720 if (task->tk_status < 0)
1723 * On success, ensure that we call xprt_end_transmit() before sleeping
1724 * in order to allow access to the socket to other RPC requests.
1726 call_transmit_status(task);
1727 if (rpc_reply_expected(task))
1729 task->tk_action = rpc_exit_task;
1730 rpc_wake_up_queued_task(&task->tk_rqstp->rq_xprt->pending, task);
1734 * 5a. Handle cleanup after a transmission
1737 call_transmit_status(struct rpc_task *task)
1739 task->tk_action = call_status;
1742 * Common case: success. Force the compiler to put this
1745 if (task->tk_status == 0) {
1746 xprt_end_transmit(task);
1747 rpc_task_force_reencode(task);
1751 switch (task->tk_status) {
1755 dprint_status(task);
1756 xprt_end_transmit(task);
1757 rpc_task_force_reencode(task);
1760 * Special cases: if we've been waiting on the
1761 * socket's write_space() callback, or if the
1762 * socket just returned a connection error,
1763 * then hold onto the transport lock.
1769 if (RPC_IS_SOFTCONN(task)) {
1770 xprt_end_transmit(task);
1771 rpc_exit(task, task->tk_status);
1777 rpc_task_force_reencode(task);
1781 #if defined(CONFIG_SUNRPC_BACKCHANNEL)
1783 * 5b. Send the backchannel RPC reply. On error, drop the reply. In
1784 * addition, disconnect on connectivity errors.
1787 call_bc_transmit(struct rpc_task *task)
1789 struct rpc_rqst *req = task->tk_rqstp;
1791 task->tk_status = xprt_prepare_transmit(task);
1792 if (task->tk_status == -EAGAIN) {
1794 * Could not reserve the transport. Try again after the
1795 * transport is released.
1797 task->tk_status = 0;
1798 task->tk_action = call_bc_transmit;
1802 task->tk_action = rpc_exit_task;
1803 if (task->tk_status < 0) {
1804 printk(KERN_NOTICE "RPC: Could not send backchannel reply "
1805 "error: %d\n", task->tk_status);
1809 xprt_transmit(task);
1810 xprt_end_transmit(task);
1811 dprint_status(task);
1812 switch (task->tk_status) {
1821 * Problem reaching the server. Disconnect and let the
1822 * forechannel reestablish the connection. The server will
1823 * have to retransmit the backchannel request and we'll
1824 * reprocess it. Since these ops are idempotent, there's no
1825 * need to cache our reply at this time.
1827 printk(KERN_NOTICE "RPC: Could not send backchannel reply "
1828 "error: %d\n", task->tk_status);
1829 xprt_conditional_disconnect(req->rq_xprt,
1830 req->rq_connect_cookie);
1834 * We were unable to reply and will have to drop the
1835 * request. The server should reconnect and retransmit.
1837 WARN_ON_ONCE(task->tk_status == -EAGAIN);
1838 printk(KERN_NOTICE "RPC: Could not send backchannel reply "
1839 "error: %d\n", task->tk_status);
1842 rpc_wake_up_queued_task(&req->rq_xprt->pending, task);
1844 #endif /* CONFIG_SUNRPC_BACKCHANNEL */
1847 * 6. Sort out the RPC call status
1850 call_status(struct rpc_task *task)
1852 struct rpc_clnt *clnt = task->tk_client;
1853 struct rpc_rqst *req = task->tk_rqstp;
1856 if (req->rq_reply_bytes_recvd > 0 && !req->rq_bytes_sent)
1857 task->tk_status = req->rq_reply_bytes_recvd;
1859 dprint_status(task);
1861 status = task->tk_status;
1863 task->tk_action = call_decode;
1867 trace_rpc_call_status(task);
1868 task->tk_status = 0;
1874 * Delay any retries for 3 seconds, then handle as if it
1877 rpc_delay(task, 3*HZ);
1879 task->tk_action = call_timeout;
1880 if (task->tk_client->cl_discrtry)
1881 xprt_conditional_disconnect(req->rq_xprt,
1882 req->rq_connect_cookie);
1886 rpc_force_rebind(clnt);
1887 rpc_delay(task, 3*HZ);
1890 task->tk_action = call_bind;
1893 task->tk_action = call_transmit;
1896 /* shutdown or soft timeout */
1897 rpc_exit(task, status);
1900 if (clnt->cl_chatty)
1901 printk("%s: RPC call returned error %d\n",
1902 clnt->cl_program->name, -status);
1903 rpc_exit(task, status);
1908 * 6a. Handle RPC timeout
1909 * We do not release the request slot, so we keep using the
1910 * same XID for all retransmits.
1913 call_timeout(struct rpc_task *task)
1915 struct rpc_clnt *clnt = task->tk_client;
1917 if (xprt_adjust_timeout(task->tk_rqstp) == 0) {
1918 dprintk("RPC: %5u call_timeout (minor)\n", task->tk_pid);
1922 dprintk("RPC: %5u call_timeout (major)\n", task->tk_pid);
1923 task->tk_timeouts++;
1925 if (RPC_IS_SOFTCONN(task)) {
1926 rpc_exit(task, -ETIMEDOUT);
1929 if (RPC_IS_SOFT(task)) {
1930 if (clnt->cl_chatty) {
1932 printk(KERN_NOTICE "%s: server %s not responding, timed out\n",
1933 clnt->cl_program->name,
1934 rcu_dereference(clnt->cl_xprt)->servername);
1937 if (task->tk_flags & RPC_TASK_TIMEOUT)
1938 rpc_exit(task, -ETIMEDOUT);
1940 rpc_exit(task, -EIO);
1944 if (!(task->tk_flags & RPC_CALL_MAJORSEEN)) {
1945 task->tk_flags |= RPC_CALL_MAJORSEEN;
1946 if (clnt->cl_chatty) {
1948 printk(KERN_NOTICE "%s: server %s not responding, still trying\n",
1949 clnt->cl_program->name,
1950 rcu_dereference(clnt->cl_xprt)->servername);
1954 rpc_force_rebind(clnt);
1956 * Did our request time out due to an RPCSEC_GSS out-of-sequence
1957 * event? RFC2203 requires the server to drop all such requests.
1959 rpcauth_invalcred(task);
1962 clnt->cl_stats->rpcretrans++;
1963 task->tk_action = call_bind;
1964 task->tk_status = 0;
1968 * 7. Decode the RPC reply
1971 call_decode(struct rpc_task *task)
1973 struct rpc_clnt *clnt = task->tk_client;
1974 struct rpc_rqst *req = task->tk_rqstp;
1975 kxdrdproc_t decode = task->tk_msg.rpc_proc->p_decode;
1978 dprint_status(task);
1980 if (task->tk_flags & RPC_CALL_MAJORSEEN) {
1981 if (clnt->cl_chatty) {
1983 printk(KERN_NOTICE "%s: server %s OK\n",
1984 clnt->cl_program->name,
1985 rcu_dereference(clnt->cl_xprt)->servername);
1988 task->tk_flags &= ~RPC_CALL_MAJORSEEN;
1992 * Ensure that we see all writes made by xprt_complete_rqst()
1993 * before it changed req->rq_reply_bytes_recvd.
1996 req->rq_rcv_buf.len = req->rq_private_buf.len;
1998 /* Check that the softirq receive buffer is valid */
1999 WARN_ON(memcmp(&req->rq_rcv_buf, &req->rq_private_buf,
2000 sizeof(req->rq_rcv_buf)) != 0);
2002 if (req->rq_rcv_buf.len < 12) {
2003 if (!RPC_IS_SOFT(task)) {
2004 task->tk_action = call_bind;
2005 clnt->cl_stats->rpcretrans++;
2008 dprintk("RPC: %s: too small RPC reply size (%d bytes)\n",
2009 clnt->cl_program->name, task->tk_status);
2010 task->tk_action = call_timeout;
2014 p = rpc_verify_header(task);
2016 if (p == ERR_PTR(-EAGAIN))
2021 task->tk_action = rpc_exit_task;
2024 task->tk_status = rpcauth_unwrap_resp(task, decode, req, p,
2025 task->tk_msg.rpc_resp);
2027 dprintk("RPC: %5u call_decode result %d\n", task->tk_pid,
2031 task->tk_status = 0;
2032 /* Note: rpc_verify_header() may have freed the RPC slot */
2033 if (task->tk_rqstp == req) {
2034 req->rq_reply_bytes_recvd = req->rq_rcv_buf.len = 0;
2035 if (task->tk_client->cl_discrtry)
2036 xprt_conditional_disconnect(req->rq_xprt,
2037 req->rq_connect_cookie);
2042 rpc_encode_header(struct rpc_task *task)
2044 struct rpc_clnt *clnt = task->tk_client;
2045 struct rpc_rqst *req = task->tk_rqstp;
2046 __be32 *p = req->rq_svec[0].iov_base;
2048 /* FIXME: check buffer size? */
2050 p = xprt_skip_transport_header(req->rq_xprt, p);
2051 *p++ = req->rq_xid; /* XID */
2052 *p++ = htonl(RPC_CALL); /* CALL */
2053 *p++ = htonl(RPC_VERSION); /* RPC version */
2054 *p++ = htonl(clnt->cl_prog); /* program number */
2055 *p++ = htonl(clnt->cl_vers); /* program version */
2056 *p++ = htonl(task->tk_msg.rpc_proc->p_proc); /* procedure */
2057 p = rpcauth_marshcred(task, p);
2058 req->rq_slen = xdr_adjust_iovec(&req->rq_svec[0], p);
2063 rpc_verify_header(struct rpc_task *task)
2065 struct rpc_clnt *clnt = task->tk_client;
2066 struct kvec *iov = &task->tk_rqstp->rq_rcv_buf.head[0];
2067 int len = task->tk_rqstp->rq_rcv_buf.len >> 2;
2068 __be32 *p = iov->iov_base;
2070 int error = -EACCES;
2072 if ((task->tk_rqstp->rq_rcv_buf.len & 3) != 0) {
2073 /* RFC-1014 says that the representation of XDR data must be a
2074 * multiple of four bytes
2075 * - if it isn't pointer subtraction in the NFS client may give
2078 dprintk("RPC: %5u %s: XDR representation not a multiple of"
2079 " 4 bytes: 0x%x\n", task->tk_pid, __func__,
2080 task->tk_rqstp->rq_rcv_buf.len);
2086 p += 1; /* skip XID */
2087 if ((n = ntohl(*p++)) != RPC_REPLY) {
2088 dprintk("RPC: %5u %s: not an RPC reply: %x\n",
2089 task->tk_pid, __func__, n);
2093 if ((n = ntohl(*p++)) != RPC_MSG_ACCEPTED) {
2096 switch ((n = ntohl(*p++))) {
2097 case RPC_AUTH_ERROR:
2100 dprintk("RPC: %5u %s: RPC call version mismatch!\n",
2101 task->tk_pid, __func__);
2102 error = -EPROTONOSUPPORT;
2105 dprintk("RPC: %5u %s: RPC call rejected, "
2106 "unknown error: %x\n",
2107 task->tk_pid, __func__, n);
2112 switch ((n = ntohl(*p++))) {
2113 case RPC_AUTH_REJECTEDCRED:
2114 case RPC_AUTH_REJECTEDVERF:
2115 case RPCSEC_GSS_CREDPROBLEM:
2116 case RPCSEC_GSS_CTXPROBLEM:
2117 if (!task->tk_cred_retry)
2119 task->tk_cred_retry--;
2120 dprintk("RPC: %5u %s: retry stale creds\n",
2121 task->tk_pid, __func__);
2122 rpcauth_invalcred(task);
2123 /* Ensure we obtain a new XID! */
2125 task->tk_action = call_reserve;
2127 case RPC_AUTH_BADCRED:
2128 case RPC_AUTH_BADVERF:
2129 /* possibly garbled cred/verf? */
2130 if (!task->tk_garb_retry)
2132 task->tk_garb_retry--;
2133 dprintk("RPC: %5u %s: retry garbled creds\n",
2134 task->tk_pid, __func__);
2135 task->tk_action = call_bind;
2137 case RPC_AUTH_TOOWEAK:
2139 printk(KERN_NOTICE "RPC: server %s requires stronger "
2140 "authentication.\n",
2141 rcu_dereference(clnt->cl_xprt)->servername);
2145 dprintk("RPC: %5u %s: unknown auth error: %x\n",
2146 task->tk_pid, __func__, n);
2149 dprintk("RPC: %5u %s: call rejected %d\n",
2150 task->tk_pid, __func__, n);
2153 if (!(p = rpcauth_checkverf(task, p))) {
2154 dprintk("RPC: %5u %s: auth check failed\n",
2155 task->tk_pid, __func__);
2156 goto out_garbage; /* bad verifier, retry */
2158 len = p - (__be32 *)iov->iov_base - 1;
2161 switch ((n = ntohl(*p++))) {
2164 case RPC_PROG_UNAVAIL:
2165 dprintk_rcu("RPC: %5u %s: program %u is unsupported "
2166 "by server %s\n", task->tk_pid, __func__,
2167 (unsigned int)clnt->cl_prog,
2168 rcu_dereference(clnt->cl_xprt)->servername);
2169 error = -EPFNOSUPPORT;
2171 case RPC_PROG_MISMATCH:
2172 dprintk_rcu("RPC: %5u %s: program %u, version %u unsupported "
2173 "by server %s\n", task->tk_pid, __func__,
2174 (unsigned int)clnt->cl_prog,
2175 (unsigned int)clnt->cl_vers,
2176 rcu_dereference(clnt->cl_xprt)->servername);
2177 error = -EPROTONOSUPPORT;
2179 case RPC_PROC_UNAVAIL:
2180 dprintk_rcu("RPC: %5u %s: proc %s unsupported by program %u, "
2181 "version %u on server %s\n",
2182 task->tk_pid, __func__,
2183 rpc_proc_name(task),
2184 clnt->cl_prog, clnt->cl_vers,
2185 rcu_dereference(clnt->cl_xprt)->servername);
2186 error = -EOPNOTSUPP;
2188 case RPC_GARBAGE_ARGS:
2189 dprintk("RPC: %5u %s: server saw garbage\n",
2190 task->tk_pid, __func__);
2193 dprintk("RPC: %5u %s: server accept status: %x\n",
2194 task->tk_pid, __func__, n);
2199 clnt->cl_stats->rpcgarbage++;
2200 if (task->tk_garb_retry) {
2201 task->tk_garb_retry--;
2202 dprintk("RPC: %5u %s: retrying\n",
2203 task->tk_pid, __func__);
2204 task->tk_action = call_bind;
2206 return ERR_PTR(-EAGAIN);
2211 rpc_exit(task, error);
2212 dprintk("RPC: %5u %s: call failed with error %d\n", task->tk_pid,
2214 return ERR_PTR(error);
2216 dprintk("RPC: %5u %s: server reply was truncated.\n", task->tk_pid,
2221 static void rpcproc_encode_null(void *rqstp, struct xdr_stream *xdr, void *obj)
2225 static int rpcproc_decode_null(void *rqstp, struct xdr_stream *xdr, void *obj)
2230 static struct rpc_procinfo rpcproc_null = {
2231 .p_encode = rpcproc_encode_null,
2232 .p_decode = rpcproc_decode_null,
2235 static int rpc_ping(struct rpc_clnt *clnt)
2237 struct rpc_message msg = {
2238 .rpc_proc = &rpcproc_null,
2241 msg.rpc_cred = authnull_ops.lookup_cred(NULL, NULL, 0);
2242 err = rpc_call_sync(clnt, &msg, RPC_TASK_SOFT | RPC_TASK_SOFTCONN);
2243 put_rpccred(msg.rpc_cred);
2247 struct rpc_task *rpc_call_null(struct rpc_clnt *clnt, struct rpc_cred *cred, int flags)
2249 struct rpc_message msg = {
2250 .rpc_proc = &rpcproc_null,
2253 struct rpc_task_setup task_setup_data = {
2255 .rpc_message = &msg,
2256 .callback_ops = &rpc_default_ops,
2259 return rpc_run_task(&task_setup_data);
2261 EXPORT_SYMBOL_GPL(rpc_call_null);
2264 static void rpc_show_header(void)
2266 printk(KERN_INFO "-pid- flgs status -client- --rqstp- "
2267 "-timeout ---ops--\n");
2270 static void rpc_show_task(const struct rpc_clnt *clnt,
2271 const struct rpc_task *task)
2273 const char *rpc_waitq = "none";
2275 if (RPC_IS_QUEUED(task))
2276 rpc_waitq = rpc_qname(task->tk_waitqueue);
2278 printk(KERN_INFO "%5u %04x %6d %8p %8p %8ld %8p %sv%u %s a:%ps q:%s\n",
2279 task->tk_pid, task->tk_flags, task->tk_status,
2280 clnt, task->tk_rqstp, task->tk_timeout, task->tk_ops,
2281 clnt->cl_program->name, clnt->cl_vers, rpc_proc_name(task),
2282 task->tk_action, rpc_waitq);
2285 void rpc_show_tasks(struct net *net)
2287 struct rpc_clnt *clnt;
2288 struct rpc_task *task;
2290 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
2292 spin_lock(&sn->rpc_client_lock);
2293 list_for_each_entry(clnt, &sn->all_clients, cl_clients) {
2294 spin_lock(&clnt->cl_lock);
2295 list_for_each_entry(task, &clnt->cl_tasks, tk_task) {
2300 rpc_show_task(clnt, task);
2302 spin_unlock(&clnt->cl_lock);
2304 spin_unlock(&sn->rpc_client_lock);