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(struct rpc_clnt
*clnt
,
276 rpc_authflavor_t pseudoflavor
,
277 const char *client_name
)
279 struct rpc_auth_create_args auth_args
= {
280 .pseudoflavor
= pseudoflavor
,
281 .target_name
= client_name
,
283 struct rpc_auth
*auth
;
284 struct net
*net
= rpc_net_ns(clnt
);
285 struct super_block
*pipefs_sb
;
288 pipefs_sb
= rpc_get_sb_net(net
);
290 err
= rpc_setup_pipedir(pipefs_sb
, clnt
);
295 rpc_register_client(clnt
);
299 auth
= rpcauth_create(&auth_args
, clnt
);
301 dprintk("RPC: Couldn't create auth handle (flavor %u)\n",
308 pipefs_sb
= rpc_get_sb_net(net
);
309 rpc_unregister_client(clnt
);
310 __rpc_clnt_remove_pipedir(clnt
);
317 static DEFINE_IDA(rpc_clids
);
319 static int rpc_alloc_clid(struct rpc_clnt
*clnt
)
323 clid
= ida_simple_get(&rpc_clids
, 0, 0, GFP_KERNEL
);
326 clnt
->cl_clid
= clid
;
330 static void rpc_free_clid(struct rpc_clnt
*clnt
)
332 ida_simple_remove(&rpc_clids
, clnt
->cl_clid
);
335 static struct rpc_clnt
* rpc_new_client(const struct rpc_create_args
*args
,
336 struct rpc_xprt
*xprt
,
337 struct rpc_clnt
*parent
)
339 const struct rpc_program
*program
= args
->program
;
340 const struct rpc_version
*version
;
341 struct rpc_clnt
*clnt
= NULL
;
344 /* sanity check the name before trying to print it */
345 dprintk("RPC: creating %s client for %s (xprt %p)\n",
346 program
->name
, args
->servername
, xprt
);
353 if (args
->version
>= program
->nrvers
)
355 version
= program
->version
[args
->version
];
360 clnt
= kzalloc(sizeof(*clnt
), GFP_KERNEL
);
363 clnt
->cl_parent
= parent
? : clnt
;
365 err
= rpc_alloc_clid(clnt
);
369 rcu_assign_pointer(clnt
->cl_xprt
, xprt
);
370 clnt
->cl_procinfo
= version
->procs
;
371 clnt
->cl_maxproc
= version
->nrprocs
;
372 clnt
->cl_prog
= args
->prognumber
? : program
->number
;
373 clnt
->cl_vers
= version
->number
;
374 clnt
->cl_stats
= program
->stats
;
375 clnt
->cl_metrics
= rpc_alloc_iostats(clnt
);
376 rpc_init_pipe_dir_head(&clnt
->cl_pipedir_objects
);
378 if (clnt
->cl_metrics
== NULL
)
380 clnt
->cl_program
= program
;
381 INIT_LIST_HEAD(&clnt
->cl_tasks
);
382 spin_lock_init(&clnt
->cl_lock
);
384 if (!xprt_bound(xprt
))
385 clnt
->cl_autobind
= 1;
387 clnt
->cl_timeout
= xprt
->timeout
;
388 if (args
->timeout
!= NULL
) {
389 memcpy(&clnt
->cl_timeout_default
, args
->timeout
,
390 sizeof(clnt
->cl_timeout_default
));
391 clnt
->cl_timeout
= &clnt
->cl_timeout_default
;
394 clnt
->cl_rtt
= &clnt
->cl_rtt_default
;
395 rpc_init_rtt(&clnt
->cl_rtt_default
, clnt
->cl_timeout
->to_initval
);
397 atomic_set(&clnt
->cl_count
, 1);
399 /* save the nodename */
400 rpc_clnt_set_nodename(clnt
, utsname()->nodename
);
402 err
= rpc_client_register(clnt
, args
->authflavor
, args
->client_name
);
406 atomic_inc(&parent
->cl_count
);
410 rpc_free_iostats(clnt
->cl_metrics
);
423 * rpc_create - create an RPC client and transport with one call
424 * @args: rpc_clnt create argument structure
426 * Creates and initializes an RPC transport and an RPC client.
428 * It can ping the server in order to determine if it is up, and to see if
429 * it supports this program and version. RPC_CLNT_CREATE_NOPING disables
430 * this behavior so asynchronous tasks can also use rpc_create.
432 struct rpc_clnt
*rpc_create(struct rpc_create_args
*args
)
434 struct rpc_xprt
*xprt
;
435 struct rpc_clnt
*clnt
;
436 struct xprt_create xprtargs
= {
438 .ident
= args
->protocol
,
439 .srcaddr
= args
->saddress
,
440 .dstaddr
= args
->address
,
441 .addrlen
= args
->addrsize
,
442 .servername
= args
->servername
,
443 .bc_xprt
= args
->bc_xprt
,
447 if (args
->flags
& RPC_CLNT_CREATE_INFINITE_SLOTS
)
448 xprtargs
.flags
|= XPRT_CREATE_INFINITE_SLOTS
;
449 if (args
->flags
& RPC_CLNT_CREATE_NO_IDLE_TIMEOUT
)
450 xprtargs
.flags
|= XPRT_CREATE_NO_IDLE_TIMEOUT
;
452 * If the caller chooses not to specify a hostname, whip
453 * up a string representation of the passed-in address.
455 if (xprtargs
.servername
== NULL
) {
456 struct sockaddr_un
*sun
=
457 (struct sockaddr_un
*)args
->address
;
458 struct sockaddr_in
*sin
=
459 (struct sockaddr_in
*)args
->address
;
460 struct sockaddr_in6
*sin6
=
461 (struct sockaddr_in6
*)args
->address
;
463 servername
[0] = '\0';
464 switch (args
->address
->sa_family
) {
466 snprintf(servername
, sizeof(servername
), "%s",
470 snprintf(servername
, sizeof(servername
), "%pI4",
471 &sin
->sin_addr
.s_addr
);
474 snprintf(servername
, sizeof(servername
), "%pI6",
478 /* caller wants default server name, but
479 * address family isn't recognized. */
480 return ERR_PTR(-EINVAL
);
482 xprtargs
.servername
= servername
;
485 xprt
= xprt_create_transport(&xprtargs
);
487 return (struct rpc_clnt
*)xprt
;
490 * By default, kernel RPC client connects from a reserved port.
491 * CAP_NET_BIND_SERVICE will not be set for unprivileged requesters,
492 * but it is always enabled for rpciod, which handles the connect
496 if (args
->flags
& RPC_CLNT_CREATE_NONPRIVPORT
)
499 clnt
= rpc_new_client(args
, xprt
, NULL
);
503 if (!(args
->flags
& RPC_CLNT_CREATE_NOPING
)) {
504 int err
= rpc_ping(clnt
);
506 rpc_shutdown_client(clnt
);
511 clnt
->cl_softrtry
= 1;
512 if (args
->flags
& RPC_CLNT_CREATE_HARDRTRY
)
513 clnt
->cl_softrtry
= 0;
515 if (args
->flags
& RPC_CLNT_CREATE_AUTOBIND
)
516 clnt
->cl_autobind
= 1;
517 if (args
->flags
& RPC_CLNT_CREATE_DISCRTRY
)
518 clnt
->cl_discrtry
= 1;
519 if (!(args
->flags
& RPC_CLNT_CREATE_QUIET
))
524 EXPORT_SYMBOL_GPL(rpc_create
);
527 * This function clones the RPC client structure. It allows us to share the
528 * same transport while varying parameters such as the authentication
531 static struct rpc_clnt
*__rpc_clone_client(struct rpc_create_args
*args
,
532 struct rpc_clnt
*clnt
)
534 struct rpc_xprt
*xprt
;
535 struct rpc_clnt
*new;
540 xprt
= xprt_get(rcu_dereference(clnt
->cl_xprt
));
544 args
->servername
= xprt
->servername
;
546 new = rpc_new_client(args
, xprt
, clnt
);
552 /* Turn off autobind on clones */
553 new->cl_autobind
= 0;
554 new->cl_softrtry
= clnt
->cl_softrtry
;
555 new->cl_discrtry
= clnt
->cl_discrtry
;
556 new->cl_chatty
= clnt
->cl_chatty
;
560 dprintk("RPC: %s: returned error %d\n", __func__
, err
);
565 * rpc_clone_client - Clone an RPC client structure
567 * @clnt: RPC client whose parameters are copied
569 * Returns a fresh RPC client or an ERR_PTR.
571 struct rpc_clnt
*rpc_clone_client(struct rpc_clnt
*clnt
)
573 struct rpc_create_args args
= {
574 .program
= clnt
->cl_program
,
575 .prognumber
= clnt
->cl_prog
,
576 .version
= clnt
->cl_vers
,
577 .authflavor
= clnt
->cl_auth
->au_flavor
,
579 return __rpc_clone_client(&args
, clnt
);
581 EXPORT_SYMBOL_GPL(rpc_clone_client
);
584 * rpc_clone_client_set_auth - Clone an RPC client structure and set its auth
586 * @clnt: RPC client whose parameters are copied
587 * @flavor: security flavor for new client
589 * Returns a fresh RPC client or an ERR_PTR.
592 rpc_clone_client_set_auth(struct rpc_clnt
*clnt
, rpc_authflavor_t flavor
)
594 struct rpc_create_args args
= {
595 .program
= clnt
->cl_program
,
596 .prognumber
= clnt
->cl_prog
,
597 .version
= clnt
->cl_vers
,
598 .authflavor
= flavor
,
600 return __rpc_clone_client(&args
, clnt
);
602 EXPORT_SYMBOL_GPL(rpc_clone_client_set_auth
);
605 * Kill all tasks for the given client.
606 * XXX: kill their descendants as well?
608 void rpc_killall_tasks(struct rpc_clnt
*clnt
)
610 struct rpc_task
*rovr
;
613 if (list_empty(&clnt
->cl_tasks
))
615 dprintk("RPC: killing all tasks for client %p\n", clnt
);
617 * Spin lock all_tasks to prevent changes...
619 spin_lock(&clnt
->cl_lock
);
620 list_for_each_entry(rovr
, &clnt
->cl_tasks
, tk_task
) {
621 if (!RPC_IS_ACTIVATED(rovr
))
623 if (!(rovr
->tk_flags
& RPC_TASK_KILLED
)) {
624 rovr
->tk_flags
|= RPC_TASK_KILLED
;
625 rpc_exit(rovr
, -EIO
);
626 if (RPC_IS_QUEUED(rovr
))
627 rpc_wake_up_queued_task(rovr
->tk_waitqueue
,
631 spin_unlock(&clnt
->cl_lock
);
633 EXPORT_SYMBOL_GPL(rpc_killall_tasks
);
636 * Properly shut down an RPC client, terminating all outstanding
639 void rpc_shutdown_client(struct rpc_clnt
*clnt
)
643 dprintk_rcu("RPC: shutting down %s client for %s\n",
644 clnt
->cl_program
->name
,
645 rcu_dereference(clnt
->cl_xprt
)->servername
);
647 while (!list_empty(&clnt
->cl_tasks
)) {
648 rpc_killall_tasks(clnt
);
649 wait_event_timeout(destroy_wait
,
650 list_empty(&clnt
->cl_tasks
), 1*HZ
);
653 rpc_release_client(clnt
);
655 EXPORT_SYMBOL_GPL(rpc_shutdown_client
);
661 rpc_free_client(struct rpc_clnt
*clnt
)
663 dprintk_rcu("RPC: destroying %s client for %s\n",
664 clnt
->cl_program
->name
,
665 rcu_dereference(clnt
->cl_xprt
)->servername
);
666 if (clnt
->cl_parent
!= clnt
)
667 rpc_release_client(clnt
->cl_parent
);
668 rpc_clnt_remove_pipedir(clnt
);
669 rpc_unregister_client(clnt
);
670 rpc_free_iostats(clnt
->cl_metrics
);
671 clnt
->cl_metrics
= NULL
;
672 xprt_put(rcu_dereference_raw(clnt
->cl_xprt
));
682 rpc_free_auth(struct rpc_clnt
*clnt
)
684 if (clnt
->cl_auth
== NULL
) {
685 rpc_free_client(clnt
);
690 * Note: RPCSEC_GSS may need to send NULL RPC calls in order to
691 * release remaining GSS contexts. This mechanism ensures
692 * that it can do so safely.
694 atomic_inc(&clnt
->cl_count
);
695 rpcauth_release(clnt
->cl_auth
);
696 clnt
->cl_auth
= NULL
;
697 if (atomic_dec_and_test(&clnt
->cl_count
))
698 rpc_free_client(clnt
);
702 * Release reference to the RPC client
705 rpc_release_client(struct rpc_clnt
*clnt
)
707 dprintk("RPC: rpc_release_client(%p)\n", clnt
);
709 if (list_empty(&clnt
->cl_tasks
))
710 wake_up(&destroy_wait
);
711 if (atomic_dec_and_test(&clnt
->cl_count
))
714 EXPORT_SYMBOL_GPL(rpc_release_client
);
717 * rpc_bind_new_program - bind a new RPC program to an existing client
718 * @old: old rpc_client
719 * @program: rpc program to set
720 * @vers: rpc program version
722 * Clones the rpc client and sets up a new RPC program. This is mainly
723 * of use for enabling different RPC programs to share the same transport.
724 * The Sun NFSv2/v3 ACL protocol can do this.
726 struct rpc_clnt
*rpc_bind_new_program(struct rpc_clnt
*old
,
727 const struct rpc_program
*program
,
730 struct rpc_create_args args
= {
732 .prognumber
= program
->number
,
734 .authflavor
= old
->cl_auth
->au_flavor
,
736 struct rpc_clnt
*clnt
;
739 clnt
= __rpc_clone_client(&args
, old
);
742 err
= rpc_ping(clnt
);
744 rpc_shutdown_client(clnt
);
750 EXPORT_SYMBOL_GPL(rpc_bind_new_program
);
752 void rpc_task_release_client(struct rpc_task
*task
)
754 struct rpc_clnt
*clnt
= task
->tk_client
;
757 /* Remove from client task list */
758 spin_lock(&clnt
->cl_lock
);
759 list_del(&task
->tk_task
);
760 spin_unlock(&clnt
->cl_lock
);
761 task
->tk_client
= NULL
;
763 rpc_release_client(clnt
);
768 void rpc_task_set_client(struct rpc_task
*task
, struct rpc_clnt
*clnt
)
771 rpc_task_release_client(task
);
772 task
->tk_client
= clnt
;
773 atomic_inc(&clnt
->cl_count
);
774 if (clnt
->cl_softrtry
)
775 task
->tk_flags
|= RPC_TASK_SOFT
;
776 if (clnt
->cl_noretranstimeo
)
777 task
->tk_flags
|= RPC_TASK_NO_RETRANS_TIMEOUT
;
778 if (sk_memalloc_socks()) {
779 struct rpc_xprt
*xprt
;
782 xprt
= rcu_dereference(clnt
->cl_xprt
);
784 task
->tk_flags
|= RPC_TASK_SWAPPER
;
787 /* Add to the client's list of all tasks */
788 spin_lock(&clnt
->cl_lock
);
789 list_add_tail(&task
->tk_task
, &clnt
->cl_tasks
);
790 spin_unlock(&clnt
->cl_lock
);
794 void rpc_task_reset_client(struct rpc_task
*task
, struct rpc_clnt
*clnt
)
796 rpc_task_release_client(task
);
797 rpc_task_set_client(task
, clnt
);
799 EXPORT_SYMBOL_GPL(rpc_task_reset_client
);
803 rpc_task_set_rpc_message(struct rpc_task
*task
, const struct rpc_message
*msg
)
806 task
->tk_msg
.rpc_proc
= msg
->rpc_proc
;
807 task
->tk_msg
.rpc_argp
= msg
->rpc_argp
;
808 task
->tk_msg
.rpc_resp
= msg
->rpc_resp
;
809 if (msg
->rpc_cred
!= NULL
)
810 task
->tk_msg
.rpc_cred
= get_rpccred(msg
->rpc_cred
);
815 * Default callback for async RPC calls
818 rpc_default_callback(struct rpc_task
*task
, void *data
)
822 static const struct rpc_call_ops rpc_default_ops
= {
823 .rpc_call_done
= rpc_default_callback
,
827 * rpc_run_task - Allocate a new RPC task, then run rpc_execute against it
828 * @task_setup_data: pointer to task initialisation data
830 struct rpc_task
*rpc_run_task(const struct rpc_task_setup
*task_setup_data
)
832 struct rpc_task
*task
;
834 task
= rpc_new_task(task_setup_data
);
838 rpc_task_set_client(task
, task_setup_data
->rpc_client
);
839 rpc_task_set_rpc_message(task
, task_setup_data
->rpc_message
);
841 if (task
->tk_action
== NULL
)
842 rpc_call_start(task
);
844 atomic_inc(&task
->tk_count
);
849 EXPORT_SYMBOL_GPL(rpc_run_task
);
852 * rpc_call_sync - Perform a synchronous RPC call
853 * @clnt: pointer to RPC client
854 * @msg: RPC call parameters
855 * @flags: RPC call flags
857 int rpc_call_sync(struct rpc_clnt
*clnt
, const struct rpc_message
*msg
, int flags
)
859 struct rpc_task
*task
;
860 struct rpc_task_setup task_setup_data
= {
863 .callback_ops
= &rpc_default_ops
,
868 WARN_ON_ONCE(flags
& RPC_TASK_ASYNC
);
869 if (flags
& RPC_TASK_ASYNC
) {
870 rpc_release_calldata(task_setup_data
.callback_ops
,
871 task_setup_data
.callback_data
);
875 task
= rpc_run_task(&task_setup_data
);
877 return PTR_ERR(task
);
878 status
= task
->tk_status
;
882 EXPORT_SYMBOL_GPL(rpc_call_sync
);
885 * rpc_call_async - Perform an asynchronous RPC call
886 * @clnt: pointer to RPC client
887 * @msg: RPC call parameters
888 * @flags: RPC call flags
889 * @tk_ops: RPC call ops
890 * @data: user call data
893 rpc_call_async(struct rpc_clnt
*clnt
, const struct rpc_message
*msg
, int flags
,
894 const struct rpc_call_ops
*tk_ops
, void *data
)
896 struct rpc_task
*task
;
897 struct rpc_task_setup task_setup_data
= {
900 .callback_ops
= tk_ops
,
901 .callback_data
= data
,
902 .flags
= flags
|RPC_TASK_ASYNC
,
905 task
= rpc_run_task(&task_setup_data
);
907 return PTR_ERR(task
);
911 EXPORT_SYMBOL_GPL(rpc_call_async
);
913 #if defined(CONFIG_SUNRPC_BACKCHANNEL)
915 * rpc_run_bc_task - Allocate a new RPC task for backchannel use, then run
916 * rpc_execute against it
918 * @tk_ops: RPC call ops
920 struct rpc_task
*rpc_run_bc_task(struct rpc_rqst
*req
,
921 const struct rpc_call_ops
*tk_ops
)
923 struct rpc_task
*task
;
924 struct xdr_buf
*xbufp
= &req
->rq_snd_buf
;
925 struct rpc_task_setup task_setup_data
= {
926 .callback_ops
= tk_ops
,
929 dprintk("RPC: rpc_run_bc_task req= %p\n", req
);
931 * Create an rpc_task to send the data
933 task
= rpc_new_task(&task_setup_data
);
935 xprt_free_bc_request(req
);
938 task
->tk_rqstp
= req
;
941 * Set up the xdr_buf length.
942 * This also indicates that the buffer is XDR encoded already.
944 xbufp
->len
= xbufp
->head
[0].iov_len
+ xbufp
->page_len
+
945 xbufp
->tail
[0].iov_len
;
947 task
->tk_action
= call_bc_transmit
;
948 atomic_inc(&task
->tk_count
);
949 WARN_ON_ONCE(atomic_read(&task
->tk_count
) != 2);
953 dprintk("RPC: rpc_run_bc_task: task= %p\n", task
);
956 #endif /* CONFIG_SUNRPC_BACKCHANNEL */
959 rpc_call_start(struct rpc_task
*task
)
961 task
->tk_action
= call_start
;
963 EXPORT_SYMBOL_GPL(rpc_call_start
);
966 * rpc_peeraddr - extract remote peer address from clnt's xprt
967 * @clnt: RPC client structure
968 * @buf: target buffer
969 * @bufsize: length of target buffer
971 * Returns the number of bytes that are actually in the stored address.
973 size_t rpc_peeraddr(struct rpc_clnt
*clnt
, struct sockaddr
*buf
, size_t bufsize
)
976 struct rpc_xprt
*xprt
;
979 xprt
= rcu_dereference(clnt
->cl_xprt
);
981 bytes
= xprt
->addrlen
;
984 memcpy(buf
, &xprt
->addr
, bytes
);
989 EXPORT_SYMBOL_GPL(rpc_peeraddr
);
992 * rpc_peeraddr2str - return remote peer address in printable format
993 * @clnt: RPC client structure
994 * @format: address format
996 * NB: the lifetime of the memory referenced by the returned pointer is
997 * the same as the rpc_xprt itself. As long as the caller uses this
998 * pointer, it must hold the RCU read lock.
1000 const char *rpc_peeraddr2str(struct rpc_clnt
*clnt
,
1001 enum rpc_display_format_t format
)
1003 struct rpc_xprt
*xprt
;
1005 xprt
= rcu_dereference(clnt
->cl_xprt
);
1007 if (xprt
->address_strings
[format
] != NULL
)
1008 return xprt
->address_strings
[format
];
1010 return "unprintable";
1012 EXPORT_SYMBOL_GPL(rpc_peeraddr2str
);
1014 static const struct sockaddr_in rpc_inaddr_loopback
= {
1015 .sin_family
= AF_INET
,
1016 .sin_addr
.s_addr
= htonl(INADDR_ANY
),
1019 static const struct sockaddr_in6 rpc_in6addr_loopback
= {
1020 .sin6_family
= AF_INET6
,
1021 .sin6_addr
= IN6ADDR_ANY_INIT
,
1025 * Try a getsockname() on a connected datagram socket. Using a
1026 * connected datagram socket prevents leaving a socket in TIME_WAIT.
1027 * This conserves the ephemeral port number space.
1029 * Returns zero and fills in "buf" if successful; otherwise, a
1030 * negative errno is returned.
1032 static int rpc_sockname(struct net
*net
, struct sockaddr
*sap
, size_t salen
,
1033 struct sockaddr
*buf
, int buflen
)
1035 struct socket
*sock
;
1038 err
= __sock_create(net
, sap
->sa_family
,
1039 SOCK_DGRAM
, IPPROTO_UDP
, &sock
, 1);
1041 dprintk("RPC: can't create UDP socket (%d)\n", err
);
1045 switch (sap
->sa_family
) {
1047 err
= kernel_bind(sock
,
1048 (struct sockaddr
*)&rpc_inaddr_loopback
,
1049 sizeof(rpc_inaddr_loopback
));
1052 err
= kernel_bind(sock
,
1053 (struct sockaddr
*)&rpc_in6addr_loopback
,
1054 sizeof(rpc_in6addr_loopback
));
1057 err
= -EAFNOSUPPORT
;
1061 dprintk("RPC: can't bind UDP socket (%d)\n", err
);
1065 err
= kernel_connect(sock
, sap
, salen
, 0);
1067 dprintk("RPC: can't connect UDP socket (%d)\n", err
);
1071 err
= kernel_getsockname(sock
, buf
, &buflen
);
1073 dprintk("RPC: getsockname failed (%d)\n", err
);
1078 if (buf
->sa_family
== AF_INET6
) {
1079 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)buf
;
1080 sin6
->sin6_scope_id
= 0;
1082 dprintk("RPC: %s succeeded\n", __func__
);
1091 * Scraping a connected socket failed, so we don't have a useable
1092 * local address. Fallback: generate an address that will prevent
1093 * the server from calling us back.
1095 * Returns zero and fills in "buf" if successful; otherwise, a
1096 * negative errno is returned.
1098 static int rpc_anyaddr(int family
, struct sockaddr
*buf
, size_t buflen
)
1102 if (buflen
< sizeof(rpc_inaddr_loopback
))
1104 memcpy(buf
, &rpc_inaddr_loopback
,
1105 sizeof(rpc_inaddr_loopback
));
1108 if (buflen
< sizeof(rpc_in6addr_loopback
))
1110 memcpy(buf
, &rpc_in6addr_loopback
,
1111 sizeof(rpc_in6addr_loopback
));
1113 dprintk("RPC: %s: address family not supported\n",
1115 return -EAFNOSUPPORT
;
1117 dprintk("RPC: %s: succeeded\n", __func__
);
1122 * rpc_localaddr - discover local endpoint address for an RPC client
1123 * @clnt: RPC client structure
1124 * @buf: target buffer
1125 * @buflen: size of target buffer, in bytes
1127 * Returns zero and fills in "buf" and "buflen" if successful;
1128 * otherwise, a negative errno is returned.
1130 * This works even if the underlying transport is not currently connected,
1131 * or if the upper layer never previously provided a source address.
1133 * The result of this function call is transient: multiple calls in
1134 * succession may give different results, depending on how local
1135 * networking configuration changes over time.
1137 int rpc_localaddr(struct rpc_clnt
*clnt
, struct sockaddr
*buf
, size_t buflen
)
1139 struct sockaddr_storage address
;
1140 struct sockaddr
*sap
= (struct sockaddr
*)&address
;
1141 struct rpc_xprt
*xprt
;
1147 xprt
= rcu_dereference(clnt
->cl_xprt
);
1148 salen
= xprt
->addrlen
;
1149 memcpy(sap
, &xprt
->addr
, salen
);
1150 net
= get_net(xprt
->xprt_net
);
1153 rpc_set_port(sap
, 0);
1154 err
= rpc_sockname(net
, sap
, salen
, buf
, buflen
);
1157 /* Couldn't discover local address, return ANYADDR */
1158 return rpc_anyaddr(sap
->sa_family
, buf
, buflen
);
1161 EXPORT_SYMBOL_GPL(rpc_localaddr
);
1164 rpc_setbufsize(struct rpc_clnt
*clnt
, unsigned int sndsize
, unsigned int rcvsize
)
1166 struct rpc_xprt
*xprt
;
1169 xprt
= rcu_dereference(clnt
->cl_xprt
);
1170 if (xprt
->ops
->set_buffer_size
)
1171 xprt
->ops
->set_buffer_size(xprt
, sndsize
, rcvsize
);
1174 EXPORT_SYMBOL_GPL(rpc_setbufsize
);
1177 * rpc_protocol - Get transport protocol number for an RPC client
1178 * @clnt: RPC client to query
1181 int rpc_protocol(struct rpc_clnt
*clnt
)
1186 protocol
= rcu_dereference(clnt
->cl_xprt
)->prot
;
1190 EXPORT_SYMBOL_GPL(rpc_protocol
);
1193 * rpc_net_ns - Get the network namespace for this RPC client
1194 * @clnt: RPC client to query
1197 struct net
*rpc_net_ns(struct rpc_clnt
*clnt
)
1202 ret
= rcu_dereference(clnt
->cl_xprt
)->xprt_net
;
1206 EXPORT_SYMBOL_GPL(rpc_net_ns
);
1209 * rpc_max_payload - Get maximum payload size for a transport, in bytes
1210 * @clnt: RPC client to query
1212 * For stream transports, this is one RPC record fragment (see RFC
1213 * 1831), as we don't support multi-record requests yet. For datagram
1214 * transports, this is the size of an IP packet minus the IP, UDP, and
1217 size_t rpc_max_payload(struct rpc_clnt
*clnt
)
1222 ret
= rcu_dereference(clnt
->cl_xprt
)->max_payload
;
1226 EXPORT_SYMBOL_GPL(rpc_max_payload
);
1229 * rpc_get_timeout - Get timeout for transport in units of HZ
1230 * @clnt: RPC client to query
1232 unsigned long rpc_get_timeout(struct rpc_clnt
*clnt
)
1237 ret
= rcu_dereference(clnt
->cl_xprt
)->timeout
->to_initval
;
1241 EXPORT_SYMBOL_GPL(rpc_get_timeout
);
1244 * rpc_force_rebind - force transport to check that remote port is unchanged
1245 * @clnt: client to rebind
1248 void rpc_force_rebind(struct rpc_clnt
*clnt
)
1250 if (clnt
->cl_autobind
) {
1252 xprt_clear_bound(rcu_dereference(clnt
->cl_xprt
));
1256 EXPORT_SYMBOL_GPL(rpc_force_rebind
);
1259 * Restart an (async) RPC call from the call_prepare state.
1260 * Usually called from within the exit handler.
1263 rpc_restart_call_prepare(struct rpc_task
*task
)
1265 if (RPC_ASSASSINATED(task
))
1267 task
->tk_action
= call_start
;
1268 if (task
->tk_ops
->rpc_call_prepare
!= NULL
)
1269 task
->tk_action
= rpc_prepare_task
;
1272 EXPORT_SYMBOL_GPL(rpc_restart_call_prepare
);
1275 * Restart an (async) RPC call. Usually called from within the
1279 rpc_restart_call(struct rpc_task
*task
)
1281 if (RPC_ASSASSINATED(task
))
1283 task
->tk_action
= call_start
;
1286 EXPORT_SYMBOL_GPL(rpc_restart_call
);
1289 static const char *rpc_proc_name(const struct rpc_task
*task
)
1291 const struct rpc_procinfo
*proc
= task
->tk_msg
.rpc_proc
;
1295 return proc
->p_name
;
1306 * Other FSM states can be visited zero or more times, but
1307 * this state is visited exactly once for each RPC.
1310 call_start(struct rpc_task
*task
)
1312 struct rpc_clnt
*clnt
= task
->tk_client
;
1314 dprintk("RPC: %5u call_start %s%d proc %s (%s)\n", task
->tk_pid
,
1315 clnt
->cl_program
->name
, clnt
->cl_vers
,
1316 rpc_proc_name(task
),
1317 (RPC_IS_ASYNC(task
) ? "async" : "sync"));
1319 /* Increment call count */
1320 task
->tk_msg
.rpc_proc
->p_count
++;
1321 clnt
->cl_stats
->rpccnt
++;
1322 task
->tk_action
= call_reserve
;
1326 * 1. Reserve an RPC call slot
1329 call_reserve(struct rpc_task
*task
)
1331 dprint_status(task
);
1333 task
->tk_status
= 0;
1334 task
->tk_action
= call_reserveresult
;
1338 static void call_retry_reserve(struct rpc_task
*task
);
1341 * 1b. Grok the result of xprt_reserve()
1344 call_reserveresult(struct rpc_task
*task
)
1346 int status
= task
->tk_status
;
1348 dprint_status(task
);
1351 * After a call to xprt_reserve(), we must have either
1352 * a request slot or else an error status.
1354 task
->tk_status
= 0;
1356 if (task
->tk_rqstp
) {
1357 task
->tk_action
= call_refresh
;
1361 printk(KERN_ERR
"%s: status=%d, but no request slot, exiting\n",
1363 rpc_exit(task
, -EIO
);
1368 * Even though there was an error, we may have acquired
1369 * a request slot somehow. Make sure not to leak it.
1371 if (task
->tk_rqstp
) {
1372 printk(KERN_ERR
"%s: status=%d, request allocated anyway\n",
1379 rpc_delay(task
, HZ
>> 2);
1380 case -EAGAIN
: /* woken up; retry */
1381 task
->tk_action
= call_retry_reserve
;
1383 case -EIO
: /* probably a shutdown */
1386 printk(KERN_ERR
"%s: unrecognized error %d, exiting\n",
1390 rpc_exit(task
, status
);
1394 * 1c. Retry reserving an RPC call slot
1397 call_retry_reserve(struct rpc_task
*task
)
1399 dprint_status(task
);
1401 task
->tk_status
= 0;
1402 task
->tk_action
= call_reserveresult
;
1403 xprt_retry_reserve(task
);
1407 * 2. Bind and/or refresh the credentials
1410 call_refresh(struct rpc_task
*task
)
1412 dprint_status(task
);
1414 task
->tk_action
= call_refreshresult
;
1415 task
->tk_status
= 0;
1416 task
->tk_client
->cl_stats
->rpcauthrefresh
++;
1417 rpcauth_refreshcred(task
);
1421 * 2a. Process the results of a credential refresh
1424 call_refreshresult(struct rpc_task
*task
)
1426 int status
= task
->tk_status
;
1428 dprint_status(task
);
1430 task
->tk_status
= 0;
1431 task
->tk_action
= call_refresh
;
1434 if (rpcauth_uptodatecred(task
))
1435 task
->tk_action
= call_allocate
;
1438 rpc_delay(task
, 3*HZ
);
1442 if (!task
->tk_cred_retry
)
1444 task
->tk_cred_retry
--;
1445 dprintk("RPC: %5u %s: retry refresh creds\n",
1446 task
->tk_pid
, __func__
);
1449 dprintk("RPC: %5u %s: refresh creds failed with error %d\n",
1450 task
->tk_pid
, __func__
, status
);
1451 rpc_exit(task
, status
);
1455 * 2b. Allocate the buffer. For details, see sched.c:rpc_malloc.
1456 * (Note: buffer memory is freed in xprt_release).
1459 call_allocate(struct rpc_task
*task
)
1461 unsigned int slack
= task
->tk_rqstp
->rq_cred
->cr_auth
->au_cslack
;
1462 struct rpc_rqst
*req
= task
->tk_rqstp
;
1463 struct rpc_xprt
*xprt
= req
->rq_xprt
;
1464 struct rpc_procinfo
*proc
= task
->tk_msg
.rpc_proc
;
1466 dprint_status(task
);
1468 task
->tk_status
= 0;
1469 task
->tk_action
= call_bind
;
1474 if (proc
->p_proc
!= 0) {
1475 BUG_ON(proc
->p_arglen
== 0);
1476 if (proc
->p_decode
!= NULL
)
1477 BUG_ON(proc
->p_replen
== 0);
1481 * Calculate the size (in quads) of the RPC call
1482 * and reply headers, and convert both values
1485 req
->rq_callsize
= RPC_CALLHDRSIZE
+ (slack
<< 1) + proc
->p_arglen
;
1486 req
->rq_callsize
<<= 2;
1487 req
->rq_rcvsize
= RPC_REPHDRSIZE
+ slack
+ proc
->p_replen
;
1488 req
->rq_rcvsize
<<= 2;
1490 req
->rq_buffer
= xprt
->ops
->buf_alloc(task
,
1491 req
->rq_callsize
+ req
->rq_rcvsize
);
1492 if (req
->rq_buffer
!= NULL
)
1495 dprintk("RPC: %5u rpc_buffer allocation failed\n", task
->tk_pid
);
1497 if (RPC_IS_ASYNC(task
) || !fatal_signal_pending(current
)) {
1498 task
->tk_action
= call_allocate
;
1499 rpc_delay(task
, HZ
>>4);
1503 rpc_exit(task
, -ERESTARTSYS
);
1507 rpc_task_need_encode(struct rpc_task
*task
)
1509 return task
->tk_rqstp
->rq_snd_buf
.len
== 0;
1513 rpc_task_force_reencode(struct rpc_task
*task
)
1515 task
->tk_rqstp
->rq_snd_buf
.len
= 0;
1516 task
->tk_rqstp
->rq_bytes_sent
= 0;
1520 rpc_xdr_buf_init(struct xdr_buf
*buf
, void *start
, size_t len
)
1522 buf
->head
[0].iov_base
= start
;
1523 buf
->head
[0].iov_len
= len
;
1524 buf
->tail
[0].iov_len
= 0;
1532 * 3. Encode arguments of an RPC call
1535 rpc_xdr_encode(struct rpc_task
*task
)
1537 struct rpc_rqst
*req
= task
->tk_rqstp
;
1541 dprint_status(task
);
1543 rpc_xdr_buf_init(&req
->rq_snd_buf
,
1546 rpc_xdr_buf_init(&req
->rq_rcv_buf
,
1547 (char *)req
->rq_buffer
+ req
->rq_callsize
,
1550 p
= rpc_encode_header(task
);
1552 printk(KERN_INFO
"RPC: couldn't encode RPC header, exit EIO\n");
1553 rpc_exit(task
, -EIO
);
1557 encode
= task
->tk_msg
.rpc_proc
->p_encode
;
1561 task
->tk_status
= rpcauth_wrap_req(task
, encode
, req
, p
,
1562 task
->tk_msg
.rpc_argp
);
1566 * 4. Get the server port number if not yet set
1569 call_bind(struct rpc_task
*task
)
1571 struct rpc_xprt
*xprt
= task
->tk_rqstp
->rq_xprt
;
1573 dprint_status(task
);
1575 task
->tk_action
= call_connect
;
1576 if (!xprt_bound(xprt
)) {
1577 task
->tk_action
= call_bind_status
;
1578 task
->tk_timeout
= xprt
->bind_timeout
;
1579 xprt
->ops
->rpcbind(task
);
1584 * 4a. Sort out bind result
1587 call_bind_status(struct rpc_task
*task
)
1591 if (task
->tk_status
>= 0) {
1592 dprint_status(task
);
1593 task
->tk_status
= 0;
1594 task
->tk_action
= call_connect
;
1598 trace_rpc_bind_status(task
);
1599 switch (task
->tk_status
) {
1601 dprintk("RPC: %5u rpcbind out of memory\n", task
->tk_pid
);
1602 rpc_delay(task
, HZ
>> 2);
1605 dprintk("RPC: %5u remote rpcbind: RPC program/version "
1606 "unavailable\n", task
->tk_pid
);
1607 /* fail immediately if this is an RPC ping */
1608 if (task
->tk_msg
.rpc_proc
->p_proc
== 0) {
1609 status
= -EOPNOTSUPP
;
1612 if (task
->tk_rebind_retry
== 0)
1614 task
->tk_rebind_retry
--;
1615 rpc_delay(task
, 3*HZ
);
1618 dprintk("RPC: %5u rpcbind request timed out\n",
1622 /* server doesn't support any rpcbind version we know of */
1623 dprintk("RPC: %5u unrecognized remote rpcbind service\n",
1626 case -EPROTONOSUPPORT
:
1627 dprintk("RPC: %5u remote rpcbind version unavailable, retrying\n",
1629 task
->tk_status
= 0;
1630 task
->tk_action
= call_bind
;
1632 case -ECONNREFUSED
: /* connection problems */
1639 dprintk("RPC: %5u remote rpcbind unreachable: %d\n",
1640 task
->tk_pid
, task
->tk_status
);
1641 if (!RPC_IS_SOFTCONN(task
)) {
1642 rpc_delay(task
, 5*HZ
);
1645 status
= task
->tk_status
;
1648 dprintk("RPC: %5u unrecognized rpcbind error (%d)\n",
1649 task
->tk_pid
, -task
->tk_status
);
1652 rpc_exit(task
, status
);
1656 task
->tk_action
= call_timeout
;
1660 * 4b. Connect to the RPC server
1663 call_connect(struct rpc_task
*task
)
1665 struct rpc_xprt
*xprt
= task
->tk_rqstp
->rq_xprt
;
1667 dprintk("RPC: %5u call_connect xprt %p %s connected\n",
1669 (xprt_connected(xprt
) ? "is" : "is not"));
1671 task
->tk_action
= call_transmit
;
1672 if (!xprt_connected(xprt
)) {
1673 task
->tk_action
= call_connect_status
;
1674 if (task
->tk_status
< 0)
1676 if (task
->tk_flags
& RPC_TASK_NOCONNECT
) {
1677 rpc_exit(task
, -ENOTCONN
);
1685 * 4c. Sort out connect result
1688 call_connect_status(struct rpc_task
*task
)
1690 struct rpc_clnt
*clnt
= task
->tk_client
;
1691 int status
= task
->tk_status
;
1693 dprint_status(task
);
1695 trace_rpc_connect_status(task
, status
);
1696 task
->tk_status
= 0;
1698 /* if soft mounted, test if we've timed out */
1700 task
->tk_action
= call_timeout
;
1705 /* retry with existing socket, after a delay */
1706 rpc_delay(task
, 3*HZ
);
1707 if (RPC_IS_SOFTCONN(task
))
1710 task
->tk_action
= call_bind
;
1713 clnt
->cl_stats
->netreconn
++;
1714 task
->tk_action
= call_transmit
;
1717 rpc_exit(task
, status
);
1721 * 5. Transmit the RPC request, and wait for reply
1724 call_transmit(struct rpc_task
*task
)
1726 int is_retrans
= RPC_WAS_SENT(task
);
1728 dprint_status(task
);
1730 task
->tk_action
= call_status
;
1731 if (task
->tk_status
< 0)
1733 if (!xprt_prepare_transmit(task
))
1735 task
->tk_action
= call_transmit_status
;
1736 /* Encode here so that rpcsec_gss can use correct sequence number. */
1737 if (rpc_task_need_encode(task
)) {
1738 rpc_xdr_encode(task
);
1739 /* Did the encode result in an error condition? */
1740 if (task
->tk_status
!= 0) {
1741 /* Was the error nonfatal? */
1742 if (task
->tk_status
== -EAGAIN
)
1743 rpc_delay(task
, HZ
>> 4);
1745 rpc_exit(task
, task
->tk_status
);
1749 xprt_transmit(task
);
1750 if (task
->tk_status
< 0)
1753 task
->tk_client
->cl_stats
->rpcretrans
++;
1755 * On success, ensure that we call xprt_end_transmit() before sleeping
1756 * in order to allow access to the socket to other RPC requests.
1758 call_transmit_status(task
);
1759 if (rpc_reply_expected(task
))
1761 task
->tk_action
= rpc_exit_task
;
1762 rpc_wake_up_queued_task(&task
->tk_rqstp
->rq_xprt
->pending
, task
);
1766 * 5a. Handle cleanup after a transmission
1769 call_transmit_status(struct rpc_task
*task
)
1771 task
->tk_action
= call_status
;
1774 * Common case: success. Force the compiler to put this
1777 if (task
->tk_status
== 0) {
1778 xprt_end_transmit(task
);
1779 rpc_task_force_reencode(task
);
1783 switch (task
->tk_status
) {
1787 dprint_status(task
);
1788 xprt_end_transmit(task
);
1789 rpc_task_force_reencode(task
);
1792 * Special cases: if we've been waiting on the
1793 * socket's write_space() callback, or if the
1794 * socket just returned a connection error,
1795 * then hold onto the transport lock.
1801 if (RPC_IS_SOFTCONN(task
)) {
1802 xprt_end_transmit(task
);
1803 rpc_exit(task
, task
->tk_status
);
1809 rpc_task_force_reencode(task
);
1813 #if defined(CONFIG_SUNRPC_BACKCHANNEL)
1815 * 5b. Send the backchannel RPC reply. On error, drop the reply. In
1816 * addition, disconnect on connectivity errors.
1819 call_bc_transmit(struct rpc_task
*task
)
1821 struct rpc_rqst
*req
= task
->tk_rqstp
;
1823 if (!xprt_prepare_transmit(task
)) {
1825 * Could not reserve the transport. Try again after the
1826 * transport is released.
1828 task
->tk_status
= 0;
1829 task
->tk_action
= call_bc_transmit
;
1833 task
->tk_action
= rpc_exit_task
;
1834 if (task
->tk_status
< 0) {
1835 printk(KERN_NOTICE
"RPC: Could not send backchannel reply "
1836 "error: %d\n", task
->tk_status
);
1840 xprt_transmit(task
);
1841 xprt_end_transmit(task
);
1842 dprint_status(task
);
1843 switch (task
->tk_status
) {
1852 * Problem reaching the server. Disconnect and let the
1853 * forechannel reestablish the connection. The server will
1854 * have to retransmit the backchannel request and we'll
1855 * reprocess it. Since these ops are idempotent, there's no
1856 * need to cache our reply at this time.
1858 printk(KERN_NOTICE
"RPC: Could not send backchannel reply "
1859 "error: %d\n", task
->tk_status
);
1860 xprt_conditional_disconnect(req
->rq_xprt
,
1861 req
->rq_connect_cookie
);
1865 * We were unable to reply and will have to drop the
1866 * request. The server should reconnect and retransmit.
1868 WARN_ON_ONCE(task
->tk_status
== -EAGAIN
);
1869 printk(KERN_NOTICE
"RPC: Could not send backchannel reply "
1870 "error: %d\n", task
->tk_status
);
1873 rpc_wake_up_queued_task(&req
->rq_xprt
->pending
, task
);
1875 #endif /* CONFIG_SUNRPC_BACKCHANNEL */
1878 * 6. Sort out the RPC call status
1881 call_status(struct rpc_task
*task
)
1883 struct rpc_clnt
*clnt
= task
->tk_client
;
1884 struct rpc_rqst
*req
= task
->tk_rqstp
;
1887 if (req
->rq_reply_bytes_recvd
> 0 && !req
->rq_bytes_sent
)
1888 task
->tk_status
= req
->rq_reply_bytes_recvd
;
1890 dprint_status(task
);
1892 status
= task
->tk_status
;
1894 task
->tk_action
= call_decode
;
1898 trace_rpc_call_status(task
);
1899 task
->tk_status
= 0;
1905 * Delay any retries for 3 seconds, then handle as if it
1908 rpc_delay(task
, 3*HZ
);
1910 task
->tk_action
= call_timeout
;
1911 if (!(task
->tk_flags
& RPC_TASK_NO_RETRANS_TIMEOUT
)
1912 && task
->tk_client
->cl_discrtry
)
1913 xprt_conditional_disconnect(req
->rq_xprt
,
1914 req
->rq_connect_cookie
);
1918 rpc_force_rebind(clnt
);
1919 rpc_delay(task
, 3*HZ
);
1922 task
->tk_action
= call_bind
;
1925 task
->tk_action
= call_transmit
;
1928 /* shutdown or soft timeout */
1929 rpc_exit(task
, status
);
1932 if (clnt
->cl_chatty
)
1933 printk("%s: RPC call returned error %d\n",
1934 clnt
->cl_program
->name
, -status
);
1935 rpc_exit(task
, status
);
1940 * 6a. Handle RPC timeout
1941 * We do not release the request slot, so we keep using the
1942 * same XID for all retransmits.
1945 call_timeout(struct rpc_task
*task
)
1947 struct rpc_clnt
*clnt
= task
->tk_client
;
1949 if (xprt_adjust_timeout(task
->tk_rqstp
) == 0) {
1950 dprintk("RPC: %5u call_timeout (minor)\n", task
->tk_pid
);
1954 dprintk("RPC: %5u call_timeout (major)\n", task
->tk_pid
);
1955 task
->tk_timeouts
++;
1957 if (RPC_IS_SOFTCONN(task
)) {
1958 rpc_exit(task
, -ETIMEDOUT
);
1961 if (RPC_IS_SOFT(task
)) {
1962 if (clnt
->cl_chatty
) {
1964 printk(KERN_NOTICE
"%s: server %s not responding, timed out\n",
1965 clnt
->cl_program
->name
,
1966 rcu_dereference(clnt
->cl_xprt
)->servername
);
1969 if (task
->tk_flags
& RPC_TASK_TIMEOUT
)
1970 rpc_exit(task
, -ETIMEDOUT
);
1972 rpc_exit(task
, -EIO
);
1976 if (!(task
->tk_flags
& RPC_CALL_MAJORSEEN
)) {
1977 task
->tk_flags
|= RPC_CALL_MAJORSEEN
;
1978 if (clnt
->cl_chatty
) {
1980 printk(KERN_NOTICE
"%s: server %s not responding, still trying\n",
1981 clnt
->cl_program
->name
,
1982 rcu_dereference(clnt
->cl_xprt
)->servername
);
1986 rpc_force_rebind(clnt
);
1988 * Did our request time out due to an RPCSEC_GSS out-of-sequence
1989 * event? RFC2203 requires the server to drop all such requests.
1991 rpcauth_invalcred(task
);
1994 task
->tk_action
= call_bind
;
1995 task
->tk_status
= 0;
1999 * 7. Decode the RPC reply
2002 call_decode(struct rpc_task
*task
)
2004 struct rpc_clnt
*clnt
= task
->tk_client
;
2005 struct rpc_rqst
*req
= task
->tk_rqstp
;
2006 kxdrdproc_t decode
= task
->tk_msg
.rpc_proc
->p_decode
;
2009 dprint_status(task
);
2011 if (task
->tk_flags
& RPC_CALL_MAJORSEEN
) {
2012 if (clnt
->cl_chatty
) {
2014 printk(KERN_NOTICE
"%s: server %s OK\n",
2015 clnt
->cl_program
->name
,
2016 rcu_dereference(clnt
->cl_xprt
)->servername
);
2019 task
->tk_flags
&= ~RPC_CALL_MAJORSEEN
;
2023 * Ensure that we see all writes made by xprt_complete_rqst()
2024 * before it changed req->rq_reply_bytes_recvd.
2027 req
->rq_rcv_buf
.len
= req
->rq_private_buf
.len
;
2029 /* Check that the softirq receive buffer is valid */
2030 WARN_ON(memcmp(&req
->rq_rcv_buf
, &req
->rq_private_buf
,
2031 sizeof(req
->rq_rcv_buf
)) != 0);
2033 if (req
->rq_rcv_buf
.len
< 12) {
2034 if (!RPC_IS_SOFT(task
)) {
2035 task
->tk_action
= call_bind
;
2038 dprintk("RPC: %s: too small RPC reply size (%d bytes)\n",
2039 clnt
->cl_program
->name
, task
->tk_status
);
2040 task
->tk_action
= call_timeout
;
2044 p
= rpc_verify_header(task
);
2046 if (p
== ERR_PTR(-EAGAIN
))
2051 task
->tk_action
= rpc_exit_task
;
2054 task
->tk_status
= rpcauth_unwrap_resp(task
, decode
, req
, p
,
2055 task
->tk_msg
.rpc_resp
);
2057 dprintk("RPC: %5u call_decode result %d\n", task
->tk_pid
,
2061 task
->tk_status
= 0;
2062 /* Note: rpc_verify_header() may have freed the RPC slot */
2063 if (task
->tk_rqstp
== req
) {
2064 req
->rq_reply_bytes_recvd
= req
->rq_rcv_buf
.len
= 0;
2065 if (task
->tk_client
->cl_discrtry
)
2066 xprt_conditional_disconnect(req
->rq_xprt
,
2067 req
->rq_connect_cookie
);
2072 rpc_encode_header(struct rpc_task
*task
)
2074 struct rpc_clnt
*clnt
= task
->tk_client
;
2075 struct rpc_rqst
*req
= task
->tk_rqstp
;
2076 __be32
*p
= req
->rq_svec
[0].iov_base
;
2078 /* FIXME: check buffer size? */
2080 p
= xprt_skip_transport_header(req
->rq_xprt
, p
);
2081 *p
++ = req
->rq_xid
; /* XID */
2082 *p
++ = htonl(RPC_CALL
); /* CALL */
2083 *p
++ = htonl(RPC_VERSION
); /* RPC version */
2084 *p
++ = htonl(clnt
->cl_prog
); /* program number */
2085 *p
++ = htonl(clnt
->cl_vers
); /* program version */
2086 *p
++ = htonl(task
->tk_msg
.rpc_proc
->p_proc
); /* procedure */
2087 p
= rpcauth_marshcred(task
, p
);
2088 req
->rq_slen
= xdr_adjust_iovec(&req
->rq_svec
[0], p
);
2093 rpc_verify_header(struct rpc_task
*task
)
2095 struct rpc_clnt
*clnt
= task
->tk_client
;
2096 struct kvec
*iov
= &task
->tk_rqstp
->rq_rcv_buf
.head
[0];
2097 int len
= task
->tk_rqstp
->rq_rcv_buf
.len
>> 2;
2098 __be32
*p
= iov
->iov_base
;
2100 int error
= -EACCES
;
2102 if ((task
->tk_rqstp
->rq_rcv_buf
.len
& 3) != 0) {
2103 /* RFC-1014 says that the representation of XDR data must be a
2104 * multiple of four bytes
2105 * - if it isn't pointer subtraction in the NFS client may give
2108 dprintk("RPC: %5u %s: XDR representation not a multiple of"
2109 " 4 bytes: 0x%x\n", task
->tk_pid
, __func__
,
2110 task
->tk_rqstp
->rq_rcv_buf
.len
);
2117 p
+= 1; /* skip XID */
2118 if ((n
= ntohl(*p
++)) != RPC_REPLY
) {
2119 dprintk("RPC: %5u %s: not an RPC reply: %x\n",
2120 task
->tk_pid
, __func__
, n
);
2125 if ((n
= ntohl(*p
++)) != RPC_MSG_ACCEPTED
) {
2128 switch ((n
= ntohl(*p
++))) {
2129 case RPC_AUTH_ERROR
:
2132 dprintk("RPC: %5u %s: RPC call version mismatch!\n",
2133 task
->tk_pid
, __func__
);
2134 error
= -EPROTONOSUPPORT
;
2137 dprintk("RPC: %5u %s: RPC call rejected, "
2138 "unknown error: %x\n",
2139 task
->tk_pid
, __func__
, n
);
2145 switch ((n
= ntohl(*p
++))) {
2146 case RPC_AUTH_REJECTEDCRED
:
2147 case RPC_AUTH_REJECTEDVERF
:
2148 case RPCSEC_GSS_CREDPROBLEM
:
2149 case RPCSEC_GSS_CTXPROBLEM
:
2150 if (!task
->tk_cred_retry
)
2152 task
->tk_cred_retry
--;
2153 dprintk("RPC: %5u %s: retry stale creds\n",
2154 task
->tk_pid
, __func__
);
2155 rpcauth_invalcred(task
);
2156 /* Ensure we obtain a new XID! */
2158 task
->tk_action
= call_reserve
;
2160 case RPC_AUTH_BADCRED
:
2161 case RPC_AUTH_BADVERF
:
2162 /* possibly garbled cred/verf? */
2163 if (!task
->tk_garb_retry
)
2165 task
->tk_garb_retry
--;
2166 dprintk("RPC: %5u %s: retry garbled creds\n",
2167 task
->tk_pid
, __func__
);
2168 task
->tk_action
= call_bind
;
2170 case RPC_AUTH_TOOWEAK
:
2172 printk(KERN_NOTICE
"RPC: server %s requires stronger "
2173 "authentication.\n",
2174 rcu_dereference(clnt
->cl_xprt
)->servername
);
2178 dprintk("RPC: %5u %s: unknown auth error: %x\n",
2179 task
->tk_pid
, __func__
, n
);
2182 dprintk("RPC: %5u %s: call rejected %d\n",
2183 task
->tk_pid
, __func__
, n
);
2186 p
= rpcauth_checkverf(task
, p
);
2189 dprintk("RPC: %5u %s: auth check failed with %d\n",
2190 task
->tk_pid
, __func__
, error
);
2191 goto out_garbage
; /* bad verifier, retry */
2193 len
= p
- (__be32
*)iov
->iov_base
- 1;
2196 switch ((n
= ntohl(*p
++))) {
2199 case RPC_PROG_UNAVAIL
:
2200 dprintk_rcu("RPC: %5u %s: program %u is unsupported "
2201 "by server %s\n", task
->tk_pid
, __func__
,
2202 (unsigned int)clnt
->cl_prog
,
2203 rcu_dereference(clnt
->cl_xprt
)->servername
);
2204 error
= -EPFNOSUPPORT
;
2206 case RPC_PROG_MISMATCH
:
2207 dprintk_rcu("RPC: %5u %s: program %u, version %u unsupported "
2208 "by server %s\n", task
->tk_pid
, __func__
,
2209 (unsigned int)clnt
->cl_prog
,
2210 (unsigned int)clnt
->cl_vers
,
2211 rcu_dereference(clnt
->cl_xprt
)->servername
);
2212 error
= -EPROTONOSUPPORT
;
2214 case RPC_PROC_UNAVAIL
:
2215 dprintk_rcu("RPC: %5u %s: proc %s unsupported by program %u, "
2216 "version %u on server %s\n",
2217 task
->tk_pid
, __func__
,
2218 rpc_proc_name(task
),
2219 clnt
->cl_prog
, clnt
->cl_vers
,
2220 rcu_dereference(clnt
->cl_xprt
)->servername
);
2221 error
= -EOPNOTSUPP
;
2223 case RPC_GARBAGE_ARGS
:
2224 dprintk("RPC: %5u %s: server saw garbage\n",
2225 task
->tk_pid
, __func__
);
2228 dprintk("RPC: %5u %s: server accept status: %x\n",
2229 task
->tk_pid
, __func__
, n
);
2234 clnt
->cl_stats
->rpcgarbage
++;
2235 if (task
->tk_garb_retry
) {
2236 task
->tk_garb_retry
--;
2237 dprintk("RPC: %5u %s: retrying\n",
2238 task
->tk_pid
, __func__
);
2239 task
->tk_action
= call_bind
;
2241 return ERR_PTR(-EAGAIN
);
2244 rpc_exit(task
, error
);
2245 dprintk("RPC: %5u %s: call failed with error %d\n", task
->tk_pid
,
2247 return ERR_PTR(error
);
2249 dprintk("RPC: %5u %s: server reply was truncated.\n", task
->tk_pid
,
2254 static void rpcproc_encode_null(void *rqstp
, struct xdr_stream
*xdr
, void *obj
)
2258 static int rpcproc_decode_null(void *rqstp
, struct xdr_stream
*xdr
, void *obj
)
2263 static struct rpc_procinfo rpcproc_null
= {
2264 .p_encode
= rpcproc_encode_null
,
2265 .p_decode
= rpcproc_decode_null
,
2268 static int rpc_ping(struct rpc_clnt
*clnt
)
2270 struct rpc_message msg
= {
2271 .rpc_proc
= &rpcproc_null
,
2274 msg
.rpc_cred
= authnull_ops
.lookup_cred(NULL
, NULL
, 0);
2275 err
= rpc_call_sync(clnt
, &msg
, RPC_TASK_SOFT
| RPC_TASK_SOFTCONN
);
2276 put_rpccred(msg
.rpc_cred
);
2280 struct rpc_task
*rpc_call_null(struct rpc_clnt
*clnt
, struct rpc_cred
*cred
, int flags
)
2282 struct rpc_message msg
= {
2283 .rpc_proc
= &rpcproc_null
,
2286 struct rpc_task_setup task_setup_data
= {
2288 .rpc_message
= &msg
,
2289 .callback_ops
= &rpc_default_ops
,
2292 return rpc_run_task(&task_setup_data
);
2294 EXPORT_SYMBOL_GPL(rpc_call_null
);
2297 static void rpc_show_header(void)
2299 printk(KERN_INFO
"-pid- flgs status -client- --rqstp- "
2300 "-timeout ---ops--\n");
2303 static void rpc_show_task(const struct rpc_clnt
*clnt
,
2304 const struct rpc_task
*task
)
2306 const char *rpc_waitq
= "none";
2308 if (RPC_IS_QUEUED(task
))
2309 rpc_waitq
= rpc_qname(task
->tk_waitqueue
);
2311 printk(KERN_INFO
"%5u %04x %6d %8p %8p %8ld %8p %sv%u %s a:%ps q:%s\n",
2312 task
->tk_pid
, task
->tk_flags
, task
->tk_status
,
2313 clnt
, task
->tk_rqstp
, task
->tk_timeout
, task
->tk_ops
,
2314 clnt
->cl_program
->name
, clnt
->cl_vers
, rpc_proc_name(task
),
2315 task
->tk_action
, rpc_waitq
);
2318 void rpc_show_tasks(struct net
*net
)
2320 struct rpc_clnt
*clnt
;
2321 struct rpc_task
*task
;
2323 struct sunrpc_net
*sn
= net_generic(net
, sunrpc_net_id
);
2325 spin_lock(&sn
->rpc_client_lock
);
2326 list_for_each_entry(clnt
, &sn
->all_clients
, cl_clients
) {
2327 spin_lock(&clnt
->cl_lock
);
2328 list_for_each_entry(task
, &clnt
->cl_tasks
, tk_task
) {
2333 rpc_show_task(clnt
, task
);
2335 spin_unlock(&clnt
->cl_lock
);
2337 spin_unlock(&sn
->rpc_client_lock
);