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 rpc_remove_client_dir(clnt);
108 static void rpc_clnt_remove_pipedir(struct rpc_clnt *clnt)
110 struct net *net = rpc_net_ns(clnt);
111 struct super_block *pipefs_sb;
113 pipefs_sb = rpc_get_sb_net(net);
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 if (clnt->cl_program->pipe_dir_name != NULL) {
156 dentry = rpc_setup_pipedir_sb(pipefs_sb, clnt);
158 return PTR_ERR(dentry);
163 static int rpc_clnt_skip_event(struct rpc_clnt *clnt, unsigned long event)
165 if (clnt->cl_program->pipe_dir_name == NULL)
169 case RPC_PIPEFS_MOUNT:
170 if (clnt->cl_pipedir_objects.pdh_dentry != NULL)
172 if (atomic_read(&clnt->cl_count) == 0)
175 case RPC_PIPEFS_UMOUNT:
176 if (clnt->cl_pipedir_objects.pdh_dentry == NULL)
183 static int __rpc_clnt_handle_event(struct rpc_clnt *clnt, unsigned long event,
184 struct super_block *sb)
186 struct dentry *dentry;
190 case RPC_PIPEFS_MOUNT:
191 dentry = rpc_setup_pipedir_sb(sb, clnt);
195 return PTR_ERR(dentry);
197 case RPC_PIPEFS_UMOUNT:
198 __rpc_clnt_remove_pipedir(clnt);
201 printk(KERN_ERR "%s: unknown event: %ld\n", __func__, event);
207 static int __rpc_pipefs_event(struct rpc_clnt *clnt, unsigned long event,
208 struct super_block *sb)
212 for (;; clnt = clnt->cl_parent) {
213 if (!rpc_clnt_skip_event(clnt, event))
214 error = __rpc_clnt_handle_event(clnt, event, sb);
215 if (error || clnt == clnt->cl_parent)
221 static struct rpc_clnt *rpc_get_client_for_event(struct net *net, int event)
223 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
224 struct rpc_clnt *clnt;
226 spin_lock(&sn->rpc_client_lock);
227 list_for_each_entry(clnt, &sn->all_clients, cl_clients) {
228 if (rpc_clnt_skip_event(clnt, event))
230 spin_unlock(&sn->rpc_client_lock);
233 spin_unlock(&sn->rpc_client_lock);
237 static int rpc_pipefs_event(struct notifier_block *nb, unsigned long event,
240 struct super_block *sb = ptr;
241 struct rpc_clnt *clnt;
244 while ((clnt = rpc_get_client_for_event(sb->s_fs_info, event))) {
245 error = __rpc_pipefs_event(clnt, event, sb);
252 static struct notifier_block rpc_clients_block = {
253 .notifier_call = rpc_pipefs_event,
254 .priority = SUNRPC_PIPEFS_RPC_PRIO,
257 int rpc_clients_notifier_register(void)
259 return rpc_pipefs_notifier_register(&rpc_clients_block);
262 void rpc_clients_notifier_unregister(void)
264 return rpc_pipefs_notifier_unregister(&rpc_clients_block);
267 static void rpc_clnt_set_nodename(struct rpc_clnt *clnt, const char *nodename)
269 clnt->cl_nodelen = strlen(nodename);
270 if (clnt->cl_nodelen > UNX_MAXNODENAME)
271 clnt->cl_nodelen = UNX_MAXNODENAME;
272 memcpy(clnt->cl_nodename, nodename, clnt->cl_nodelen);
275 static int rpc_client_register(const struct rpc_create_args *args,
276 struct rpc_clnt *clnt)
278 struct rpc_auth_create_args auth_args = {
279 .pseudoflavor = args->authflavor,
280 .target_name = args->client_name,
282 struct rpc_auth *auth;
283 struct net *net = rpc_net_ns(clnt);
284 struct super_block *pipefs_sb;
287 pipefs_sb = rpc_get_sb_net(net);
289 err = rpc_setup_pipedir(pipefs_sb, clnt);
294 rpc_register_client(clnt);
298 auth = rpcauth_create(&auth_args, clnt);
300 dprintk("RPC: Couldn't create auth handle (flavor %u)\n",
307 pipefs_sb = rpc_get_sb_net(net);
308 rpc_unregister_client(clnt);
309 __rpc_clnt_remove_pipedir(clnt);
316 static struct rpc_clnt * rpc_new_client(const struct rpc_create_args *args,
317 struct rpc_xprt *xprt,
318 struct rpc_clnt *parent)
320 const struct rpc_program *program = args->program;
321 const struct rpc_version *version;
322 struct rpc_clnt *clnt = NULL;
325 /* sanity check the name before trying to print it */
326 dprintk("RPC: creating %s client for %s (xprt %p)\n",
327 program->name, args->servername, xprt);
334 if (args->version >= program->nrvers)
336 version = program->version[args->version];
341 clnt = kzalloc(sizeof(*clnt), GFP_KERNEL);
344 clnt->cl_parent = parent ? : clnt;
346 rcu_assign_pointer(clnt->cl_xprt, xprt);
347 clnt->cl_procinfo = version->procs;
348 clnt->cl_maxproc = version->nrprocs;
349 clnt->cl_prog = args->prognumber ? : program->number;
350 clnt->cl_vers = version->number;
351 clnt->cl_stats = program->stats;
352 clnt->cl_metrics = rpc_alloc_iostats(clnt);
353 rpc_init_pipe_dir_head(&clnt->cl_pipedir_objects);
355 if (clnt->cl_metrics == NULL)
357 clnt->cl_program = program;
358 INIT_LIST_HEAD(&clnt->cl_tasks);
359 spin_lock_init(&clnt->cl_lock);
361 if (!xprt_bound(xprt))
362 clnt->cl_autobind = 1;
364 clnt->cl_timeout = xprt->timeout;
365 if (args->timeout != NULL) {
366 memcpy(&clnt->cl_timeout_default, args->timeout,
367 sizeof(clnt->cl_timeout_default));
368 clnt->cl_timeout = &clnt->cl_timeout_default;
371 clnt->cl_rtt = &clnt->cl_rtt_default;
372 rpc_init_rtt(&clnt->cl_rtt_default, clnt->cl_timeout->to_initval);
374 atomic_set(&clnt->cl_count, 1);
376 /* save the nodename */
377 rpc_clnt_set_nodename(clnt, utsname()->nodename);
379 err = rpc_client_register(args, clnt);
383 atomic_inc(&parent->cl_count);
387 rpc_free_iostats(clnt->cl_metrics);
398 * rpc_create - create an RPC client and transport with one call
399 * @args: rpc_clnt create argument structure
401 * Creates and initializes an RPC transport and an RPC client.
403 * It can ping the server in order to determine if it is up, and to see if
404 * it supports this program and version. RPC_CLNT_CREATE_NOPING disables
405 * this behavior so asynchronous tasks can also use rpc_create.
407 struct rpc_clnt *rpc_create(struct rpc_create_args *args)
409 struct rpc_xprt *xprt;
410 struct rpc_clnt *clnt;
411 struct xprt_create xprtargs = {
413 .ident = args->protocol,
414 .srcaddr = args->saddress,
415 .dstaddr = args->address,
416 .addrlen = args->addrsize,
417 .servername = args->servername,
418 .bc_xprt = args->bc_xprt,
422 if (args->flags & RPC_CLNT_CREATE_INFINITE_SLOTS)
423 xprtargs.flags |= XPRT_CREATE_INFINITE_SLOTS;
424 if (args->flags & RPC_CLNT_CREATE_NO_IDLE_TIMEOUT)
425 xprtargs.flags |= XPRT_CREATE_NO_IDLE_TIMEOUT;
427 * If the caller chooses not to specify a hostname, whip
428 * up a string representation of the passed-in address.
430 if (xprtargs.servername == NULL) {
431 struct sockaddr_un *sun =
432 (struct sockaddr_un *)args->address;
433 struct sockaddr_in *sin =
434 (struct sockaddr_in *)args->address;
435 struct sockaddr_in6 *sin6 =
436 (struct sockaddr_in6 *)args->address;
438 servername[0] = '\0';
439 switch (args->address->sa_family) {
441 snprintf(servername, sizeof(servername), "%s",
445 snprintf(servername, sizeof(servername), "%pI4",
446 &sin->sin_addr.s_addr);
449 snprintf(servername, sizeof(servername), "%pI6",
453 /* caller wants default server name, but
454 * address family isn't recognized. */
455 return ERR_PTR(-EINVAL);
457 xprtargs.servername = servername;
460 xprt = xprt_create_transport(&xprtargs);
462 return (struct rpc_clnt *)xprt;
465 * By default, kernel RPC client connects from a reserved port.
466 * CAP_NET_BIND_SERVICE will not be set for unprivileged requesters,
467 * but it is always enabled for rpciod, which handles the connect
471 if (args->flags & RPC_CLNT_CREATE_NONPRIVPORT)
474 clnt = rpc_new_client(args, xprt, NULL);
478 if (!(args->flags & RPC_CLNT_CREATE_NOPING)) {
479 int err = rpc_ping(clnt);
481 rpc_shutdown_client(clnt);
486 clnt->cl_softrtry = 1;
487 if (args->flags & RPC_CLNT_CREATE_HARDRTRY)
488 clnt->cl_softrtry = 0;
490 if (args->flags & RPC_CLNT_CREATE_AUTOBIND)
491 clnt->cl_autobind = 1;
492 if (args->flags & RPC_CLNT_CREATE_DISCRTRY)
493 clnt->cl_discrtry = 1;
494 if (!(args->flags & RPC_CLNT_CREATE_QUIET))
499 EXPORT_SYMBOL_GPL(rpc_create);
502 * This function clones the RPC client structure. It allows us to share the
503 * same transport while varying parameters such as the authentication
506 static struct rpc_clnt *__rpc_clone_client(struct rpc_create_args *args,
507 struct rpc_clnt *clnt)
509 struct rpc_xprt *xprt;
510 struct rpc_clnt *new;
515 xprt = xprt_get(rcu_dereference(clnt->cl_xprt));
519 args->servername = xprt->servername;
521 new = rpc_new_client(args, xprt, clnt);
527 /* Turn off autobind on clones */
528 new->cl_autobind = 0;
529 new->cl_softrtry = clnt->cl_softrtry;
530 new->cl_discrtry = clnt->cl_discrtry;
531 new->cl_chatty = clnt->cl_chatty;
535 dprintk("RPC: %s: returned error %d\n", __func__, err);
540 * rpc_clone_client - Clone an RPC client structure
542 * @clnt: RPC client whose parameters are copied
544 * Returns a fresh RPC client or an ERR_PTR.
546 struct rpc_clnt *rpc_clone_client(struct rpc_clnt *clnt)
548 struct rpc_create_args args = {
549 .program = clnt->cl_program,
550 .prognumber = clnt->cl_prog,
551 .version = clnt->cl_vers,
552 .authflavor = clnt->cl_auth->au_flavor,
554 return __rpc_clone_client(&args, clnt);
556 EXPORT_SYMBOL_GPL(rpc_clone_client);
559 * rpc_clone_client_set_auth - Clone an RPC client structure and set its auth
561 * @clnt: RPC client whose parameters are copied
562 * @flavor: security flavor for new client
564 * Returns a fresh RPC client or an ERR_PTR.
567 rpc_clone_client_set_auth(struct rpc_clnt *clnt, rpc_authflavor_t flavor)
569 struct rpc_create_args args = {
570 .program = clnt->cl_program,
571 .prognumber = clnt->cl_prog,
572 .version = clnt->cl_vers,
573 .authflavor = flavor,
575 return __rpc_clone_client(&args, clnt);
577 EXPORT_SYMBOL_GPL(rpc_clone_client_set_auth);
580 * Kill all tasks for the given client.
581 * XXX: kill their descendants as well?
583 void rpc_killall_tasks(struct rpc_clnt *clnt)
585 struct rpc_task *rovr;
588 if (list_empty(&clnt->cl_tasks))
590 dprintk("RPC: killing all tasks for client %p\n", clnt);
592 * Spin lock all_tasks to prevent changes...
594 spin_lock(&clnt->cl_lock);
595 list_for_each_entry(rovr, &clnt->cl_tasks, tk_task) {
596 if (!RPC_IS_ACTIVATED(rovr))
598 if (!(rovr->tk_flags & RPC_TASK_KILLED)) {
599 rovr->tk_flags |= RPC_TASK_KILLED;
600 rpc_exit(rovr, -EIO);
601 if (RPC_IS_QUEUED(rovr))
602 rpc_wake_up_queued_task(rovr->tk_waitqueue,
606 spin_unlock(&clnt->cl_lock);
608 EXPORT_SYMBOL_GPL(rpc_killall_tasks);
611 * Properly shut down an RPC client, terminating all outstanding
614 void rpc_shutdown_client(struct rpc_clnt *clnt)
618 dprintk_rcu("RPC: shutting down %s client for %s\n",
619 clnt->cl_program->name,
620 rcu_dereference(clnt->cl_xprt)->servername);
622 while (!list_empty(&clnt->cl_tasks)) {
623 rpc_killall_tasks(clnt);
624 wait_event_timeout(destroy_wait,
625 list_empty(&clnt->cl_tasks), 1*HZ);
628 rpc_release_client(clnt);
630 EXPORT_SYMBOL_GPL(rpc_shutdown_client);
636 rpc_free_client(struct rpc_clnt *clnt)
638 dprintk_rcu("RPC: destroying %s client for %s\n",
639 clnt->cl_program->name,
640 rcu_dereference(clnt->cl_xprt)->servername);
641 if (clnt->cl_parent != clnt)
642 rpc_release_client(clnt->cl_parent);
643 rpc_clnt_remove_pipedir(clnt);
644 rpc_unregister_client(clnt);
645 rpc_free_iostats(clnt->cl_metrics);
646 clnt->cl_metrics = NULL;
647 xprt_put(rcu_dereference_raw(clnt->cl_xprt));
656 rpc_free_auth(struct rpc_clnt *clnt)
658 if (clnt->cl_auth == NULL) {
659 rpc_free_client(clnt);
664 * Note: RPCSEC_GSS may need to send NULL RPC calls in order to
665 * release remaining GSS contexts. This mechanism ensures
666 * that it can do so safely.
668 atomic_inc(&clnt->cl_count);
669 rpcauth_release(clnt->cl_auth);
670 clnt->cl_auth = NULL;
671 if (atomic_dec_and_test(&clnt->cl_count))
672 rpc_free_client(clnt);
676 * Release reference to the RPC client
679 rpc_release_client(struct rpc_clnt *clnt)
681 dprintk("RPC: rpc_release_client(%p)\n", clnt);
683 if (list_empty(&clnt->cl_tasks))
684 wake_up(&destroy_wait);
685 if (atomic_dec_and_test(&clnt->cl_count))
688 EXPORT_SYMBOL_GPL(rpc_release_client);
691 * rpc_bind_new_program - bind a new RPC program to an existing client
692 * @old: old rpc_client
693 * @program: rpc program to set
694 * @vers: rpc program version
696 * Clones the rpc client and sets up a new RPC program. This is mainly
697 * of use for enabling different RPC programs to share the same transport.
698 * The Sun NFSv2/v3 ACL protocol can do this.
700 struct rpc_clnt *rpc_bind_new_program(struct rpc_clnt *old,
701 const struct rpc_program *program,
704 struct rpc_create_args args = {
706 .prognumber = program->number,
708 .authflavor = old->cl_auth->au_flavor,
710 struct rpc_clnt *clnt;
713 clnt = __rpc_clone_client(&args, old);
716 err = rpc_ping(clnt);
718 rpc_shutdown_client(clnt);
724 EXPORT_SYMBOL_GPL(rpc_bind_new_program);
726 void rpc_task_release_client(struct rpc_task *task)
728 struct rpc_clnt *clnt = task->tk_client;
731 /* Remove from client task list */
732 spin_lock(&clnt->cl_lock);
733 list_del(&task->tk_task);
734 spin_unlock(&clnt->cl_lock);
735 task->tk_client = NULL;
737 rpc_release_client(clnt);
742 void rpc_task_set_client(struct rpc_task *task, struct rpc_clnt *clnt)
745 rpc_task_release_client(task);
746 task->tk_client = clnt;
747 atomic_inc(&clnt->cl_count);
748 if (clnt->cl_softrtry)
749 task->tk_flags |= RPC_TASK_SOFT;
750 if (sk_memalloc_socks()) {
751 struct rpc_xprt *xprt;
754 xprt = rcu_dereference(clnt->cl_xprt);
756 task->tk_flags |= RPC_TASK_SWAPPER;
759 /* Add to the client's list of all tasks */
760 spin_lock(&clnt->cl_lock);
761 list_add_tail(&task->tk_task, &clnt->cl_tasks);
762 spin_unlock(&clnt->cl_lock);
766 void rpc_task_reset_client(struct rpc_task *task, struct rpc_clnt *clnt)
768 rpc_task_release_client(task);
769 rpc_task_set_client(task, clnt);
771 EXPORT_SYMBOL_GPL(rpc_task_reset_client);
775 rpc_task_set_rpc_message(struct rpc_task *task, const struct rpc_message *msg)
778 task->tk_msg.rpc_proc = msg->rpc_proc;
779 task->tk_msg.rpc_argp = msg->rpc_argp;
780 task->tk_msg.rpc_resp = msg->rpc_resp;
781 if (msg->rpc_cred != NULL)
782 task->tk_msg.rpc_cred = get_rpccred(msg->rpc_cred);
787 * Default callback for async RPC calls
790 rpc_default_callback(struct rpc_task *task, void *data)
794 static const struct rpc_call_ops rpc_default_ops = {
795 .rpc_call_done = rpc_default_callback,
799 * rpc_run_task - Allocate a new RPC task, then run rpc_execute against it
800 * @task_setup_data: pointer to task initialisation data
802 struct rpc_task *rpc_run_task(const struct rpc_task_setup *task_setup_data)
804 struct rpc_task *task;
806 task = rpc_new_task(task_setup_data);
810 rpc_task_set_client(task, task_setup_data->rpc_client);
811 rpc_task_set_rpc_message(task, task_setup_data->rpc_message);
813 if (task->tk_action == NULL)
814 rpc_call_start(task);
816 atomic_inc(&task->tk_count);
821 EXPORT_SYMBOL_GPL(rpc_run_task);
824 * rpc_call_sync - Perform a synchronous RPC call
825 * @clnt: pointer to RPC client
826 * @msg: RPC call parameters
827 * @flags: RPC call flags
829 int rpc_call_sync(struct rpc_clnt *clnt, const struct rpc_message *msg, int flags)
831 struct rpc_task *task;
832 struct rpc_task_setup task_setup_data = {
835 .callback_ops = &rpc_default_ops,
840 WARN_ON_ONCE(flags & RPC_TASK_ASYNC);
841 if (flags & RPC_TASK_ASYNC) {
842 rpc_release_calldata(task_setup_data.callback_ops,
843 task_setup_data.callback_data);
847 task = rpc_run_task(&task_setup_data);
849 return PTR_ERR(task);
850 status = task->tk_status;
854 EXPORT_SYMBOL_GPL(rpc_call_sync);
857 * rpc_call_async - Perform an asynchronous RPC call
858 * @clnt: pointer to RPC client
859 * @msg: RPC call parameters
860 * @flags: RPC call flags
861 * @tk_ops: RPC call ops
862 * @data: user call data
865 rpc_call_async(struct rpc_clnt *clnt, const struct rpc_message *msg, int flags,
866 const struct rpc_call_ops *tk_ops, void *data)
868 struct rpc_task *task;
869 struct rpc_task_setup task_setup_data = {
872 .callback_ops = tk_ops,
873 .callback_data = data,
874 .flags = flags|RPC_TASK_ASYNC,
877 task = rpc_run_task(&task_setup_data);
879 return PTR_ERR(task);
883 EXPORT_SYMBOL_GPL(rpc_call_async);
885 #if defined(CONFIG_SUNRPC_BACKCHANNEL)
887 * rpc_run_bc_task - Allocate a new RPC task for backchannel use, then run
888 * rpc_execute against it
890 * @tk_ops: RPC call ops
892 struct rpc_task *rpc_run_bc_task(struct rpc_rqst *req,
893 const struct rpc_call_ops *tk_ops)
895 struct rpc_task *task;
896 struct xdr_buf *xbufp = &req->rq_snd_buf;
897 struct rpc_task_setup task_setup_data = {
898 .callback_ops = tk_ops,
901 dprintk("RPC: rpc_run_bc_task req= %p\n", req);
903 * Create an rpc_task to send the data
905 task = rpc_new_task(&task_setup_data);
907 xprt_free_bc_request(req);
910 task->tk_rqstp = req;
913 * Set up the xdr_buf length.
914 * This also indicates that the buffer is XDR encoded already.
916 xbufp->len = xbufp->head[0].iov_len + xbufp->page_len +
917 xbufp->tail[0].iov_len;
919 task->tk_action = call_bc_transmit;
920 atomic_inc(&task->tk_count);
921 WARN_ON_ONCE(atomic_read(&task->tk_count) != 2);
925 dprintk("RPC: rpc_run_bc_task: task= %p\n", task);
928 #endif /* CONFIG_SUNRPC_BACKCHANNEL */
931 rpc_call_start(struct rpc_task *task)
933 task->tk_action = call_start;
935 EXPORT_SYMBOL_GPL(rpc_call_start);
938 * rpc_peeraddr - extract remote peer address from clnt's xprt
939 * @clnt: RPC client structure
940 * @buf: target buffer
941 * @bufsize: length of target buffer
943 * Returns the number of bytes that are actually in the stored address.
945 size_t rpc_peeraddr(struct rpc_clnt *clnt, struct sockaddr *buf, size_t bufsize)
948 struct rpc_xprt *xprt;
951 xprt = rcu_dereference(clnt->cl_xprt);
953 bytes = xprt->addrlen;
956 memcpy(buf, &xprt->addr, bytes);
961 EXPORT_SYMBOL_GPL(rpc_peeraddr);
964 * rpc_peeraddr2str - return remote peer address in printable format
965 * @clnt: RPC client structure
966 * @format: address format
968 * NB: the lifetime of the memory referenced by the returned pointer is
969 * the same as the rpc_xprt itself. As long as the caller uses this
970 * pointer, it must hold the RCU read lock.
972 const char *rpc_peeraddr2str(struct rpc_clnt *clnt,
973 enum rpc_display_format_t format)
975 struct rpc_xprt *xprt;
977 xprt = rcu_dereference(clnt->cl_xprt);
979 if (xprt->address_strings[format] != NULL)
980 return xprt->address_strings[format];
982 return "unprintable";
984 EXPORT_SYMBOL_GPL(rpc_peeraddr2str);
986 static const struct sockaddr_in rpc_inaddr_loopback = {
987 .sin_family = AF_INET,
988 .sin_addr.s_addr = htonl(INADDR_ANY),
991 static const struct sockaddr_in6 rpc_in6addr_loopback = {
992 .sin6_family = AF_INET6,
993 .sin6_addr = IN6ADDR_ANY_INIT,
997 * Try a getsockname() on a connected datagram socket. Using a
998 * connected datagram socket prevents leaving a socket in TIME_WAIT.
999 * This conserves the ephemeral port number space.
1001 * Returns zero and fills in "buf" if successful; otherwise, a
1002 * negative errno is returned.
1004 static int rpc_sockname(struct net *net, struct sockaddr *sap, size_t salen,
1005 struct sockaddr *buf, int buflen)
1007 struct socket *sock;
1010 err = __sock_create(net, sap->sa_family,
1011 SOCK_DGRAM, IPPROTO_UDP, &sock, 1);
1013 dprintk("RPC: can't create UDP socket (%d)\n", err);
1017 switch (sap->sa_family) {
1019 err = kernel_bind(sock,
1020 (struct sockaddr *)&rpc_inaddr_loopback,
1021 sizeof(rpc_inaddr_loopback));
1024 err = kernel_bind(sock,
1025 (struct sockaddr *)&rpc_in6addr_loopback,
1026 sizeof(rpc_in6addr_loopback));
1029 err = -EAFNOSUPPORT;
1033 dprintk("RPC: can't bind UDP socket (%d)\n", err);
1037 err = kernel_connect(sock, sap, salen, 0);
1039 dprintk("RPC: can't connect UDP socket (%d)\n", err);
1043 err = kernel_getsockname(sock, buf, &buflen);
1045 dprintk("RPC: getsockname failed (%d)\n", err);
1050 if (buf->sa_family == AF_INET6) {
1051 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)buf;
1052 sin6->sin6_scope_id = 0;
1054 dprintk("RPC: %s succeeded\n", __func__);
1063 * Scraping a connected socket failed, so we don't have a useable
1064 * local address. Fallback: generate an address that will prevent
1065 * the server from calling us back.
1067 * Returns zero and fills in "buf" if successful; otherwise, a
1068 * negative errno is returned.
1070 static int rpc_anyaddr(int family, struct sockaddr *buf, size_t buflen)
1074 if (buflen < sizeof(rpc_inaddr_loopback))
1076 memcpy(buf, &rpc_inaddr_loopback,
1077 sizeof(rpc_inaddr_loopback));
1080 if (buflen < sizeof(rpc_in6addr_loopback))
1082 memcpy(buf, &rpc_in6addr_loopback,
1083 sizeof(rpc_in6addr_loopback));
1085 dprintk("RPC: %s: address family not supported\n",
1087 return -EAFNOSUPPORT;
1089 dprintk("RPC: %s: succeeded\n", __func__);
1094 * rpc_localaddr - discover local endpoint address for an RPC client
1095 * @clnt: RPC client structure
1096 * @buf: target buffer
1097 * @buflen: size of target buffer, in bytes
1099 * Returns zero and fills in "buf" and "buflen" if successful;
1100 * otherwise, a negative errno is returned.
1102 * This works even if the underlying transport is not currently connected,
1103 * or if the upper layer never previously provided a source address.
1105 * The result of this function call is transient: multiple calls in
1106 * succession may give different results, depending on how local
1107 * networking configuration changes over time.
1109 int rpc_localaddr(struct rpc_clnt *clnt, struct sockaddr *buf, size_t buflen)
1111 struct sockaddr_storage address;
1112 struct sockaddr *sap = (struct sockaddr *)&address;
1113 struct rpc_xprt *xprt;
1119 xprt = rcu_dereference(clnt->cl_xprt);
1120 salen = xprt->addrlen;
1121 memcpy(sap, &xprt->addr, salen);
1122 net = get_net(xprt->xprt_net);
1125 rpc_set_port(sap, 0);
1126 err = rpc_sockname(net, sap, salen, buf, buflen);
1129 /* Couldn't discover local address, return ANYADDR */
1130 return rpc_anyaddr(sap->sa_family, buf, buflen);
1133 EXPORT_SYMBOL_GPL(rpc_localaddr);
1136 rpc_setbufsize(struct rpc_clnt *clnt, unsigned int sndsize, unsigned int rcvsize)
1138 struct rpc_xprt *xprt;
1141 xprt = rcu_dereference(clnt->cl_xprt);
1142 if (xprt->ops->set_buffer_size)
1143 xprt->ops->set_buffer_size(xprt, sndsize, rcvsize);
1146 EXPORT_SYMBOL_GPL(rpc_setbufsize);
1149 * rpc_protocol - Get transport protocol number for an RPC client
1150 * @clnt: RPC client to query
1153 int rpc_protocol(struct rpc_clnt *clnt)
1158 protocol = rcu_dereference(clnt->cl_xprt)->prot;
1162 EXPORT_SYMBOL_GPL(rpc_protocol);
1165 * rpc_net_ns - Get the network namespace for this RPC client
1166 * @clnt: RPC client to query
1169 struct net *rpc_net_ns(struct rpc_clnt *clnt)
1174 ret = rcu_dereference(clnt->cl_xprt)->xprt_net;
1178 EXPORT_SYMBOL_GPL(rpc_net_ns);
1181 * rpc_max_payload - Get maximum payload size for a transport, in bytes
1182 * @clnt: RPC client to query
1184 * For stream transports, this is one RPC record fragment (see RFC
1185 * 1831), as we don't support multi-record requests yet. For datagram
1186 * transports, this is the size of an IP packet minus the IP, UDP, and
1189 size_t rpc_max_payload(struct rpc_clnt *clnt)
1194 ret = rcu_dereference(clnt->cl_xprt)->max_payload;
1198 EXPORT_SYMBOL_GPL(rpc_max_payload);
1201 * rpc_get_timeout - Get timeout for transport in units of HZ
1202 * @clnt: RPC client to query
1204 unsigned long rpc_get_timeout(struct rpc_clnt *clnt)
1209 ret = rcu_dereference(clnt->cl_xprt)->timeout->to_initval;
1213 EXPORT_SYMBOL_GPL(rpc_get_timeout);
1216 * rpc_force_rebind - force transport to check that remote port is unchanged
1217 * @clnt: client to rebind
1220 void rpc_force_rebind(struct rpc_clnt *clnt)
1222 if (clnt->cl_autobind) {
1224 xprt_clear_bound(rcu_dereference(clnt->cl_xprt));
1228 EXPORT_SYMBOL_GPL(rpc_force_rebind);
1231 * Restart an (async) RPC call from the call_prepare state.
1232 * Usually called from within the exit handler.
1235 rpc_restart_call_prepare(struct rpc_task *task)
1237 if (RPC_ASSASSINATED(task))
1239 task->tk_action = call_start;
1240 if (task->tk_ops->rpc_call_prepare != NULL)
1241 task->tk_action = rpc_prepare_task;
1244 EXPORT_SYMBOL_GPL(rpc_restart_call_prepare);
1247 * Restart an (async) RPC call. Usually called from within the
1251 rpc_restart_call(struct rpc_task *task)
1253 if (RPC_ASSASSINATED(task))
1255 task->tk_action = call_start;
1258 EXPORT_SYMBOL_GPL(rpc_restart_call);
1261 static const char *rpc_proc_name(const struct rpc_task *task)
1263 const struct rpc_procinfo *proc = task->tk_msg.rpc_proc;
1267 return proc->p_name;
1278 * Other FSM states can be visited zero or more times, but
1279 * this state is visited exactly once for each RPC.
1282 call_start(struct rpc_task *task)
1284 struct rpc_clnt *clnt = task->tk_client;
1286 dprintk("RPC: %5u call_start %s%d proc %s (%s)\n", task->tk_pid,
1287 clnt->cl_program->name, clnt->cl_vers,
1288 rpc_proc_name(task),
1289 (RPC_IS_ASYNC(task) ? "async" : "sync"));
1291 /* Increment call count */
1292 task->tk_msg.rpc_proc->p_count++;
1293 clnt->cl_stats->rpccnt++;
1294 task->tk_action = call_reserve;
1298 * 1. Reserve an RPC call slot
1301 call_reserve(struct rpc_task *task)
1303 dprint_status(task);
1305 task->tk_status = 0;
1306 task->tk_action = call_reserveresult;
1310 static void call_retry_reserve(struct rpc_task *task);
1313 * 1b. Grok the result of xprt_reserve()
1316 call_reserveresult(struct rpc_task *task)
1318 int status = task->tk_status;
1320 dprint_status(task);
1323 * After a call to xprt_reserve(), we must have either
1324 * a request slot or else an error status.
1326 task->tk_status = 0;
1328 if (task->tk_rqstp) {
1329 task->tk_action = call_refresh;
1333 printk(KERN_ERR "%s: status=%d, but no request slot, exiting\n",
1335 rpc_exit(task, -EIO);
1340 * Even though there was an error, we may have acquired
1341 * a request slot somehow. Make sure not to leak it.
1343 if (task->tk_rqstp) {
1344 printk(KERN_ERR "%s: status=%d, request allocated anyway\n",
1351 rpc_delay(task, HZ >> 2);
1352 case -EAGAIN: /* woken up; retry */
1353 task->tk_action = call_retry_reserve;
1355 case -EIO: /* probably a shutdown */
1358 printk(KERN_ERR "%s: unrecognized error %d, exiting\n",
1362 rpc_exit(task, status);
1366 * 1c. Retry reserving an RPC call slot
1369 call_retry_reserve(struct rpc_task *task)
1371 dprint_status(task);
1373 task->tk_status = 0;
1374 task->tk_action = call_reserveresult;
1375 xprt_retry_reserve(task);
1379 * 2. Bind and/or refresh the credentials
1382 call_refresh(struct rpc_task *task)
1384 dprint_status(task);
1386 task->tk_action = call_refreshresult;
1387 task->tk_status = 0;
1388 task->tk_client->cl_stats->rpcauthrefresh++;
1389 rpcauth_refreshcred(task);
1393 * 2a. Process the results of a credential refresh
1396 call_refreshresult(struct rpc_task *task)
1398 int status = task->tk_status;
1400 dprint_status(task);
1402 task->tk_status = 0;
1403 task->tk_action = call_refresh;
1406 if (rpcauth_uptodatecred(task))
1407 task->tk_action = call_allocate;
1410 rpc_delay(task, 3*HZ);
1414 if (!task->tk_cred_retry)
1416 task->tk_cred_retry--;
1417 dprintk("RPC: %5u %s: retry refresh creds\n",
1418 task->tk_pid, __func__);
1421 dprintk("RPC: %5u %s: refresh creds failed with error %d\n",
1422 task->tk_pid, __func__, status);
1423 rpc_exit(task, status);
1427 * 2b. Allocate the buffer. For details, see sched.c:rpc_malloc.
1428 * (Note: buffer memory is freed in xprt_release).
1431 call_allocate(struct rpc_task *task)
1433 unsigned int slack = task->tk_rqstp->rq_cred->cr_auth->au_cslack;
1434 struct rpc_rqst *req = task->tk_rqstp;
1435 struct rpc_xprt *xprt = req->rq_xprt;
1436 struct rpc_procinfo *proc = task->tk_msg.rpc_proc;
1438 dprint_status(task);
1440 task->tk_status = 0;
1441 task->tk_action = call_bind;
1446 if (proc->p_proc != 0) {
1447 BUG_ON(proc->p_arglen == 0);
1448 if (proc->p_decode != NULL)
1449 BUG_ON(proc->p_replen == 0);
1453 * Calculate the size (in quads) of the RPC call
1454 * and reply headers, and convert both values
1457 req->rq_callsize = RPC_CALLHDRSIZE + (slack << 1) + proc->p_arglen;
1458 req->rq_callsize <<= 2;
1459 req->rq_rcvsize = RPC_REPHDRSIZE + slack + proc->p_replen;
1460 req->rq_rcvsize <<= 2;
1462 req->rq_buffer = xprt->ops->buf_alloc(task,
1463 req->rq_callsize + req->rq_rcvsize);
1464 if (req->rq_buffer != NULL)
1467 dprintk("RPC: %5u rpc_buffer allocation failed\n", task->tk_pid);
1469 if (RPC_IS_ASYNC(task) || !fatal_signal_pending(current)) {
1470 task->tk_action = call_allocate;
1471 rpc_delay(task, HZ>>4);
1475 rpc_exit(task, -ERESTARTSYS);
1479 rpc_task_need_encode(struct rpc_task *task)
1481 return task->tk_rqstp->rq_snd_buf.len == 0;
1485 rpc_task_force_reencode(struct rpc_task *task)
1487 task->tk_rqstp->rq_snd_buf.len = 0;
1488 task->tk_rqstp->rq_bytes_sent = 0;
1492 rpc_xdr_buf_init(struct xdr_buf *buf, void *start, size_t len)
1494 buf->head[0].iov_base = start;
1495 buf->head[0].iov_len = len;
1496 buf->tail[0].iov_len = 0;
1504 * 3. Encode arguments of an RPC call
1507 rpc_xdr_encode(struct rpc_task *task)
1509 struct rpc_rqst *req = task->tk_rqstp;
1513 dprint_status(task);
1515 rpc_xdr_buf_init(&req->rq_snd_buf,
1518 rpc_xdr_buf_init(&req->rq_rcv_buf,
1519 (char *)req->rq_buffer + req->rq_callsize,
1522 p = rpc_encode_header(task);
1524 printk(KERN_INFO "RPC: couldn't encode RPC header, exit EIO\n");
1525 rpc_exit(task, -EIO);
1529 encode = task->tk_msg.rpc_proc->p_encode;
1533 task->tk_status = rpcauth_wrap_req(task, encode, req, p,
1534 task->tk_msg.rpc_argp);
1538 * 4. Get the server port number if not yet set
1541 call_bind(struct rpc_task *task)
1543 struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
1545 dprint_status(task);
1547 task->tk_action = call_connect;
1548 if (!xprt_bound(xprt)) {
1549 task->tk_action = call_bind_status;
1550 task->tk_timeout = xprt->bind_timeout;
1551 xprt->ops->rpcbind(task);
1556 * 4a. Sort out bind result
1559 call_bind_status(struct rpc_task *task)
1563 if (task->tk_status >= 0) {
1564 dprint_status(task);
1565 task->tk_status = 0;
1566 task->tk_action = call_connect;
1570 trace_rpc_bind_status(task);
1571 switch (task->tk_status) {
1573 dprintk("RPC: %5u rpcbind out of memory\n", task->tk_pid);
1574 rpc_delay(task, HZ >> 2);
1577 dprintk("RPC: %5u remote rpcbind: RPC program/version "
1578 "unavailable\n", task->tk_pid);
1579 /* fail immediately if this is an RPC ping */
1580 if (task->tk_msg.rpc_proc->p_proc == 0) {
1581 status = -EOPNOTSUPP;
1584 if (task->tk_rebind_retry == 0)
1586 task->tk_rebind_retry--;
1587 rpc_delay(task, 3*HZ);
1590 dprintk("RPC: %5u rpcbind request timed out\n",
1594 /* server doesn't support any rpcbind version we know of */
1595 dprintk("RPC: %5u unrecognized remote rpcbind service\n",
1598 case -EPROTONOSUPPORT:
1599 dprintk("RPC: %5u remote rpcbind version unavailable, retrying\n",
1601 task->tk_status = 0;
1602 task->tk_action = call_bind;
1604 case -ECONNREFUSED: /* connection problems */
1611 dprintk("RPC: %5u remote rpcbind unreachable: %d\n",
1612 task->tk_pid, task->tk_status);
1613 if (!RPC_IS_SOFTCONN(task)) {
1614 rpc_delay(task, 5*HZ);
1617 status = task->tk_status;
1620 dprintk("RPC: %5u unrecognized rpcbind error (%d)\n",
1621 task->tk_pid, -task->tk_status);
1624 rpc_exit(task, status);
1628 task->tk_action = call_timeout;
1632 * 4b. Connect to the RPC server
1635 call_connect(struct rpc_task *task)
1637 struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
1639 dprintk("RPC: %5u call_connect xprt %p %s connected\n",
1641 (xprt_connected(xprt) ? "is" : "is not"));
1643 task->tk_action = call_transmit;
1644 if (!xprt_connected(xprt)) {
1645 task->tk_action = call_connect_status;
1646 if (task->tk_status < 0)
1653 * 4c. Sort out connect result
1656 call_connect_status(struct rpc_task *task)
1658 struct rpc_clnt *clnt = task->tk_client;
1659 int status = task->tk_status;
1661 dprint_status(task);
1663 trace_rpc_connect_status(task, status);
1665 /* if soft mounted, test if we've timed out */
1667 task->tk_action = call_timeout;
1672 if (RPC_IS_SOFTCONN(task))
1674 /* retry with existing socket, after a delay */
1677 task->tk_status = 0;
1678 clnt->cl_stats->netreconn++;
1679 task->tk_action = call_transmit;
1682 rpc_exit(task, status);
1686 * 5. Transmit the RPC request, and wait for reply
1689 call_transmit(struct rpc_task *task)
1691 dprint_status(task);
1693 task->tk_action = call_status;
1694 if (task->tk_status < 0)
1696 task->tk_status = xprt_prepare_transmit(task);
1697 if (task->tk_status != 0)
1699 task->tk_action = call_transmit_status;
1700 /* Encode here so that rpcsec_gss can use correct sequence number. */
1701 if (rpc_task_need_encode(task)) {
1702 rpc_xdr_encode(task);
1703 /* Did the encode result in an error condition? */
1704 if (task->tk_status != 0) {
1705 /* Was the error nonfatal? */
1706 if (task->tk_status == -EAGAIN)
1707 rpc_delay(task, HZ >> 4);
1709 rpc_exit(task, task->tk_status);
1713 xprt_transmit(task);
1714 if (task->tk_status < 0)
1717 * On success, ensure that we call xprt_end_transmit() before sleeping
1718 * in order to allow access to the socket to other RPC requests.
1720 call_transmit_status(task);
1721 if (rpc_reply_expected(task))
1723 task->tk_action = rpc_exit_task;
1724 rpc_wake_up_queued_task(&task->tk_rqstp->rq_xprt->pending, task);
1728 * 5a. Handle cleanup after a transmission
1731 call_transmit_status(struct rpc_task *task)
1733 task->tk_action = call_status;
1736 * Common case: success. Force the compiler to put this
1739 if (task->tk_status == 0) {
1740 xprt_end_transmit(task);
1741 rpc_task_force_reencode(task);
1745 switch (task->tk_status) {
1749 dprint_status(task);
1750 xprt_end_transmit(task);
1751 rpc_task_force_reencode(task);
1754 * Special cases: if we've been waiting on the
1755 * socket's write_space() callback, or if the
1756 * socket just returned a connection error,
1757 * then hold onto the transport lock.
1763 if (RPC_IS_SOFTCONN(task)) {
1764 xprt_end_transmit(task);
1765 rpc_exit(task, task->tk_status);
1771 rpc_task_force_reencode(task);
1775 #if defined(CONFIG_SUNRPC_BACKCHANNEL)
1777 * 5b. Send the backchannel RPC reply. On error, drop the reply. In
1778 * addition, disconnect on connectivity errors.
1781 call_bc_transmit(struct rpc_task *task)
1783 struct rpc_rqst *req = task->tk_rqstp;
1785 task->tk_status = xprt_prepare_transmit(task);
1786 if (task->tk_status == -EAGAIN) {
1788 * Could not reserve the transport. Try again after the
1789 * transport is released.
1791 task->tk_status = 0;
1792 task->tk_action = call_bc_transmit;
1796 task->tk_action = rpc_exit_task;
1797 if (task->tk_status < 0) {
1798 printk(KERN_NOTICE "RPC: Could not send backchannel reply "
1799 "error: %d\n", task->tk_status);
1803 xprt_transmit(task);
1804 xprt_end_transmit(task);
1805 dprint_status(task);
1806 switch (task->tk_status) {
1815 * Problem reaching the server. Disconnect and let the
1816 * forechannel reestablish the connection. The server will
1817 * have to retransmit the backchannel request and we'll
1818 * reprocess it. Since these ops are idempotent, there's no
1819 * need to cache our reply at this time.
1821 printk(KERN_NOTICE "RPC: Could not send backchannel reply "
1822 "error: %d\n", task->tk_status);
1823 xprt_conditional_disconnect(req->rq_xprt,
1824 req->rq_connect_cookie);
1828 * We were unable to reply and will have to drop the
1829 * request. The server should reconnect and retransmit.
1831 WARN_ON_ONCE(task->tk_status == -EAGAIN);
1832 printk(KERN_NOTICE "RPC: Could not send backchannel reply "
1833 "error: %d\n", task->tk_status);
1836 rpc_wake_up_queued_task(&req->rq_xprt->pending, task);
1838 #endif /* CONFIG_SUNRPC_BACKCHANNEL */
1841 * 6. Sort out the RPC call status
1844 call_status(struct rpc_task *task)
1846 struct rpc_clnt *clnt = task->tk_client;
1847 struct rpc_rqst *req = task->tk_rqstp;
1850 if (req->rq_reply_bytes_recvd > 0 && !req->rq_bytes_sent)
1851 task->tk_status = req->rq_reply_bytes_recvd;
1853 dprint_status(task);
1855 status = task->tk_status;
1857 task->tk_action = call_decode;
1861 trace_rpc_call_status(task);
1862 task->tk_status = 0;
1868 * Delay any retries for 3 seconds, then handle as if it
1871 rpc_delay(task, 3*HZ);
1873 task->tk_action = call_timeout;
1874 if (task->tk_client->cl_discrtry)
1875 xprt_conditional_disconnect(req->rq_xprt,
1876 req->rq_connect_cookie);
1880 rpc_force_rebind(clnt);
1881 rpc_delay(task, 3*HZ);
1884 task->tk_action = call_bind;
1887 task->tk_action = call_transmit;
1890 /* shutdown or soft timeout */
1891 rpc_exit(task, status);
1894 if (clnt->cl_chatty)
1895 printk("%s: RPC call returned error %d\n",
1896 clnt->cl_program->name, -status);
1897 rpc_exit(task, status);
1902 * 6a. Handle RPC timeout
1903 * We do not release the request slot, so we keep using the
1904 * same XID for all retransmits.
1907 call_timeout(struct rpc_task *task)
1909 struct rpc_clnt *clnt = task->tk_client;
1911 if (xprt_adjust_timeout(task->tk_rqstp) == 0) {
1912 dprintk("RPC: %5u call_timeout (minor)\n", task->tk_pid);
1916 dprintk("RPC: %5u call_timeout (major)\n", task->tk_pid);
1917 task->tk_timeouts++;
1919 if (RPC_IS_SOFTCONN(task)) {
1920 rpc_exit(task, -ETIMEDOUT);
1923 if (RPC_IS_SOFT(task)) {
1924 if (clnt->cl_chatty) {
1926 printk(KERN_NOTICE "%s: server %s not responding, timed out\n",
1927 clnt->cl_program->name,
1928 rcu_dereference(clnt->cl_xprt)->servername);
1931 if (task->tk_flags & RPC_TASK_TIMEOUT)
1932 rpc_exit(task, -ETIMEDOUT);
1934 rpc_exit(task, -EIO);
1938 if (!(task->tk_flags & RPC_CALL_MAJORSEEN)) {
1939 task->tk_flags |= RPC_CALL_MAJORSEEN;
1940 if (clnt->cl_chatty) {
1942 printk(KERN_NOTICE "%s: server %s not responding, still trying\n",
1943 clnt->cl_program->name,
1944 rcu_dereference(clnt->cl_xprt)->servername);
1948 rpc_force_rebind(clnt);
1950 * Did our request time out due to an RPCSEC_GSS out-of-sequence
1951 * event? RFC2203 requires the server to drop all such requests.
1953 rpcauth_invalcred(task);
1956 clnt->cl_stats->rpcretrans++;
1957 task->tk_action = call_bind;
1958 task->tk_status = 0;
1962 * 7. Decode the RPC reply
1965 call_decode(struct rpc_task *task)
1967 struct rpc_clnt *clnt = task->tk_client;
1968 struct rpc_rqst *req = task->tk_rqstp;
1969 kxdrdproc_t decode = task->tk_msg.rpc_proc->p_decode;
1972 dprint_status(task);
1974 if (task->tk_flags & RPC_CALL_MAJORSEEN) {
1975 if (clnt->cl_chatty) {
1977 printk(KERN_NOTICE "%s: server %s OK\n",
1978 clnt->cl_program->name,
1979 rcu_dereference(clnt->cl_xprt)->servername);
1982 task->tk_flags &= ~RPC_CALL_MAJORSEEN;
1986 * Ensure that we see all writes made by xprt_complete_rqst()
1987 * before it changed req->rq_reply_bytes_recvd.
1990 req->rq_rcv_buf.len = req->rq_private_buf.len;
1992 /* Check that the softirq receive buffer is valid */
1993 WARN_ON(memcmp(&req->rq_rcv_buf, &req->rq_private_buf,
1994 sizeof(req->rq_rcv_buf)) != 0);
1996 if (req->rq_rcv_buf.len < 12) {
1997 if (!RPC_IS_SOFT(task)) {
1998 task->tk_action = call_bind;
1999 clnt->cl_stats->rpcretrans++;
2002 dprintk("RPC: %s: too small RPC reply size (%d bytes)\n",
2003 clnt->cl_program->name, task->tk_status);
2004 task->tk_action = call_timeout;
2008 p = rpc_verify_header(task);
2010 if (p == ERR_PTR(-EAGAIN))
2015 task->tk_action = rpc_exit_task;
2018 task->tk_status = rpcauth_unwrap_resp(task, decode, req, p,
2019 task->tk_msg.rpc_resp);
2021 dprintk("RPC: %5u call_decode result %d\n", task->tk_pid,
2025 task->tk_status = 0;
2026 /* Note: rpc_verify_header() may have freed the RPC slot */
2027 if (task->tk_rqstp == req) {
2028 req->rq_reply_bytes_recvd = req->rq_rcv_buf.len = 0;
2029 if (task->tk_client->cl_discrtry)
2030 xprt_conditional_disconnect(req->rq_xprt,
2031 req->rq_connect_cookie);
2036 rpc_encode_header(struct rpc_task *task)
2038 struct rpc_clnt *clnt = task->tk_client;
2039 struct rpc_rqst *req = task->tk_rqstp;
2040 __be32 *p = req->rq_svec[0].iov_base;
2042 /* FIXME: check buffer size? */
2044 p = xprt_skip_transport_header(req->rq_xprt, p);
2045 *p++ = req->rq_xid; /* XID */
2046 *p++ = htonl(RPC_CALL); /* CALL */
2047 *p++ = htonl(RPC_VERSION); /* RPC version */
2048 *p++ = htonl(clnt->cl_prog); /* program number */
2049 *p++ = htonl(clnt->cl_vers); /* program version */
2050 *p++ = htonl(task->tk_msg.rpc_proc->p_proc); /* procedure */
2051 p = rpcauth_marshcred(task, p);
2052 req->rq_slen = xdr_adjust_iovec(&req->rq_svec[0], p);
2057 rpc_verify_header(struct rpc_task *task)
2059 struct rpc_clnt *clnt = task->tk_client;
2060 struct kvec *iov = &task->tk_rqstp->rq_rcv_buf.head[0];
2061 int len = task->tk_rqstp->rq_rcv_buf.len >> 2;
2062 __be32 *p = iov->iov_base;
2064 int error = -EACCES;
2066 if ((task->tk_rqstp->rq_rcv_buf.len & 3) != 0) {
2067 /* RFC-1014 says that the representation of XDR data must be a
2068 * multiple of four bytes
2069 * - if it isn't pointer subtraction in the NFS client may give
2072 dprintk("RPC: %5u %s: XDR representation not a multiple of"
2073 " 4 bytes: 0x%x\n", task->tk_pid, __func__,
2074 task->tk_rqstp->rq_rcv_buf.len);
2080 p += 1; /* skip XID */
2081 if ((n = ntohl(*p++)) != RPC_REPLY) {
2082 dprintk("RPC: %5u %s: not an RPC reply: %x\n",
2083 task->tk_pid, __func__, n);
2087 if ((n = ntohl(*p++)) != RPC_MSG_ACCEPTED) {
2090 switch ((n = ntohl(*p++))) {
2091 case RPC_AUTH_ERROR:
2094 dprintk("RPC: %5u %s: RPC call version mismatch!\n",
2095 task->tk_pid, __func__);
2096 error = -EPROTONOSUPPORT;
2099 dprintk("RPC: %5u %s: RPC call rejected, "
2100 "unknown error: %x\n",
2101 task->tk_pid, __func__, n);
2106 switch ((n = ntohl(*p++))) {
2107 case RPC_AUTH_REJECTEDCRED:
2108 case RPC_AUTH_REJECTEDVERF:
2109 case RPCSEC_GSS_CREDPROBLEM:
2110 case RPCSEC_GSS_CTXPROBLEM:
2111 if (!task->tk_cred_retry)
2113 task->tk_cred_retry--;
2114 dprintk("RPC: %5u %s: retry stale creds\n",
2115 task->tk_pid, __func__);
2116 rpcauth_invalcred(task);
2117 /* Ensure we obtain a new XID! */
2119 task->tk_action = call_reserve;
2121 case RPC_AUTH_BADCRED:
2122 case RPC_AUTH_BADVERF:
2123 /* possibly garbled cred/verf? */
2124 if (!task->tk_garb_retry)
2126 task->tk_garb_retry--;
2127 dprintk("RPC: %5u %s: retry garbled creds\n",
2128 task->tk_pid, __func__);
2129 task->tk_action = call_bind;
2131 case RPC_AUTH_TOOWEAK:
2133 printk(KERN_NOTICE "RPC: server %s requires stronger "
2134 "authentication.\n",
2135 rcu_dereference(clnt->cl_xprt)->servername);
2139 dprintk("RPC: %5u %s: unknown auth error: %x\n",
2140 task->tk_pid, __func__, n);
2143 dprintk("RPC: %5u %s: call rejected %d\n",
2144 task->tk_pid, __func__, n);
2147 if (!(p = rpcauth_checkverf(task, p))) {
2148 dprintk("RPC: %5u %s: auth check failed\n",
2149 task->tk_pid, __func__);
2150 goto out_garbage; /* bad verifier, retry */
2152 len = p - (__be32 *)iov->iov_base - 1;
2155 switch ((n = ntohl(*p++))) {
2158 case RPC_PROG_UNAVAIL:
2159 dprintk_rcu("RPC: %5u %s: program %u is unsupported "
2160 "by server %s\n", task->tk_pid, __func__,
2161 (unsigned int)clnt->cl_prog,
2162 rcu_dereference(clnt->cl_xprt)->servername);
2163 error = -EPFNOSUPPORT;
2165 case RPC_PROG_MISMATCH:
2166 dprintk_rcu("RPC: %5u %s: program %u, version %u unsupported "
2167 "by server %s\n", task->tk_pid, __func__,
2168 (unsigned int)clnt->cl_prog,
2169 (unsigned int)clnt->cl_vers,
2170 rcu_dereference(clnt->cl_xprt)->servername);
2171 error = -EPROTONOSUPPORT;
2173 case RPC_PROC_UNAVAIL:
2174 dprintk_rcu("RPC: %5u %s: proc %s unsupported by program %u, "
2175 "version %u on server %s\n",
2176 task->tk_pid, __func__,
2177 rpc_proc_name(task),
2178 clnt->cl_prog, clnt->cl_vers,
2179 rcu_dereference(clnt->cl_xprt)->servername);
2180 error = -EOPNOTSUPP;
2182 case RPC_GARBAGE_ARGS:
2183 dprintk("RPC: %5u %s: server saw garbage\n",
2184 task->tk_pid, __func__);
2187 dprintk("RPC: %5u %s: server accept status: %x\n",
2188 task->tk_pid, __func__, n);
2193 clnt->cl_stats->rpcgarbage++;
2194 if (task->tk_garb_retry) {
2195 task->tk_garb_retry--;
2196 dprintk("RPC: %5u %s: retrying\n",
2197 task->tk_pid, __func__);
2198 task->tk_action = call_bind;
2200 return ERR_PTR(-EAGAIN);
2205 rpc_exit(task, error);
2206 dprintk("RPC: %5u %s: call failed with error %d\n", task->tk_pid,
2208 return ERR_PTR(error);
2210 dprintk("RPC: %5u %s: server reply was truncated.\n", task->tk_pid,
2215 static void rpcproc_encode_null(void *rqstp, struct xdr_stream *xdr, void *obj)
2219 static int rpcproc_decode_null(void *rqstp, struct xdr_stream *xdr, void *obj)
2224 static struct rpc_procinfo rpcproc_null = {
2225 .p_encode = rpcproc_encode_null,
2226 .p_decode = rpcproc_decode_null,
2229 static int rpc_ping(struct rpc_clnt *clnt)
2231 struct rpc_message msg = {
2232 .rpc_proc = &rpcproc_null,
2235 msg.rpc_cred = authnull_ops.lookup_cred(NULL, NULL, 0);
2236 err = rpc_call_sync(clnt, &msg, RPC_TASK_SOFT | RPC_TASK_SOFTCONN);
2237 put_rpccred(msg.rpc_cred);
2241 struct rpc_task *rpc_call_null(struct rpc_clnt *clnt, struct rpc_cred *cred, int flags)
2243 struct rpc_message msg = {
2244 .rpc_proc = &rpcproc_null,
2247 struct rpc_task_setup task_setup_data = {
2249 .rpc_message = &msg,
2250 .callback_ops = &rpc_default_ops,
2253 return rpc_run_task(&task_setup_data);
2255 EXPORT_SYMBOL_GPL(rpc_call_null);
2258 static void rpc_show_header(void)
2260 printk(KERN_INFO "-pid- flgs status -client- --rqstp- "
2261 "-timeout ---ops--\n");
2264 static void rpc_show_task(const struct rpc_clnt *clnt,
2265 const struct rpc_task *task)
2267 const char *rpc_waitq = "none";
2269 if (RPC_IS_QUEUED(task))
2270 rpc_waitq = rpc_qname(task->tk_waitqueue);
2272 printk(KERN_INFO "%5u %04x %6d %8p %8p %8ld %8p %sv%u %s a:%ps q:%s\n",
2273 task->tk_pid, task->tk_flags, task->tk_status,
2274 clnt, task->tk_rqstp, task->tk_timeout, task->tk_ops,
2275 clnt->cl_program->name, clnt->cl_vers, rpc_proc_name(task),
2276 task->tk_action, rpc_waitq);
2279 void rpc_show_tasks(struct net *net)
2281 struct rpc_clnt *clnt;
2282 struct rpc_task *task;
2284 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
2286 spin_lock(&sn->rpc_client_lock);
2287 list_for_each_entry(clnt, &sn->all_clients, cl_clients) {
2288 spin_lock(&clnt->cl_lock);
2289 list_for_each_entry(task, &clnt->cl_tasks, tk_task) {
2294 rpc_show_task(clnt, task);
2296 spin_unlock(&clnt->cl_lock);
2298 spin_unlock(&sn->rpc_client_lock);