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 * NB: BSD uses a more intelligent approach to guessing when a request
17 * or reply has been lost by keeping the RTO estimate for each procedure.
18 * We currently make do with a constant timeout value.
20 * Copyright (C) 1992,1993 Rick Sladkey <jrs@world.std.com>
21 * Copyright (C) 1995,1996 Olaf Kirch <okir@monad.swb.de>
24 #include <asm/system.h>
26 #include <linux/module.h>
27 #include <linux/types.h>
29 #include <linux/slab.h>
30 #include <linux/smp_lock.h>
31 #include <linux/utsname.h>
32 #include <linux/workqueue.h>
34 #include <linux/sunrpc/clnt.h>
35 #include <linux/sunrpc/rpc_pipe_fs.h>
36 #include <linux/sunrpc/metrics.h>
40 # define RPCDBG_FACILITY RPCDBG_CALL
43 #define dprint_status(t) \
44 dprintk("RPC: %5u %s (status %d)\n", t->tk_pid, \
45 __FUNCTION__, t->tk_status)
48 * All RPC clients are linked into this list
50 static LIST_HEAD(all_clients
);
51 static DEFINE_SPINLOCK(rpc_client_lock
);
53 static DECLARE_WAIT_QUEUE_HEAD(destroy_wait
);
56 static void call_start(struct rpc_task
*task
);
57 static void call_reserve(struct rpc_task
*task
);
58 static void call_reserveresult(struct rpc_task
*task
);
59 static void call_allocate(struct rpc_task
*task
);
60 static void call_encode(struct rpc_task
*task
);
61 static void call_decode(struct rpc_task
*task
);
62 static void call_bind(struct rpc_task
*task
);
63 static void call_bind_status(struct rpc_task
*task
);
64 static void call_transmit(struct rpc_task
*task
);
65 static void call_status(struct rpc_task
*task
);
66 static void call_transmit_status(struct rpc_task
*task
);
67 static void call_refresh(struct rpc_task
*task
);
68 static void call_refreshresult(struct rpc_task
*task
);
69 static void call_timeout(struct rpc_task
*task
);
70 static void call_connect(struct rpc_task
*task
);
71 static void call_connect_status(struct rpc_task
*task
);
72 static __be32
* call_header(struct rpc_task
*task
);
73 static __be32
* call_verify(struct rpc_task
*task
);
75 static int rpc_ping(struct rpc_clnt
*clnt
, int flags
);
77 static void rpc_register_client(struct rpc_clnt
*clnt
)
79 spin_lock(&rpc_client_lock
);
80 list_add(&clnt
->cl_clients
, &all_clients
);
81 spin_unlock(&rpc_client_lock
);
84 static void rpc_unregister_client(struct rpc_clnt
*clnt
)
86 spin_lock(&rpc_client_lock
);
87 list_del(&clnt
->cl_clients
);
88 spin_unlock(&rpc_client_lock
);
92 rpc_setup_pipedir(struct rpc_clnt
*clnt
, char *dir_name
)
94 static uint32_t clntid
;
97 clnt
->cl_vfsmnt
= ERR_PTR(-ENOENT
);
98 clnt
->cl_dentry
= ERR_PTR(-ENOENT
);
102 clnt
->cl_vfsmnt
= rpc_get_mount();
103 if (IS_ERR(clnt
->cl_vfsmnt
))
104 return PTR_ERR(clnt
->cl_vfsmnt
);
107 snprintf(clnt
->cl_pathname
, sizeof(clnt
->cl_pathname
),
108 "%s/clnt%x", dir_name
,
109 (unsigned int)clntid
++);
110 clnt
->cl_pathname
[sizeof(clnt
->cl_pathname
) - 1] = '\0';
111 clnt
->cl_dentry
= rpc_mkdir(clnt
->cl_pathname
, clnt
);
112 if (!IS_ERR(clnt
->cl_dentry
))
114 error
= PTR_ERR(clnt
->cl_dentry
);
115 if (error
!= -EEXIST
) {
116 printk(KERN_INFO
"RPC: Couldn't create pipefs entry %s, error %d\n",
117 clnt
->cl_pathname
, error
);
124 static struct rpc_clnt
* rpc_new_client(struct rpc_xprt
*xprt
, char *servname
, struct rpc_program
*program
, u32 vers
, rpc_authflavor_t flavor
)
126 struct rpc_version
*version
;
127 struct rpc_clnt
*clnt
= NULL
;
128 struct rpc_auth
*auth
;
132 /* sanity check the name before trying to print it */
134 len
= strlen(servname
);
135 if (len
> RPC_MAXNETNAMELEN
)
139 dprintk("RPC: creating %s client for %s (xprt %p)\n",
140 program
->name
, servname
, xprt
);
148 if (vers
>= program
->nrvers
|| !(version
= program
->version
[vers
]))
152 clnt
= kzalloc(sizeof(*clnt
), GFP_KERNEL
);
155 clnt
->cl_parent
= clnt
;
157 clnt
->cl_server
= clnt
->cl_inline_name
;
158 if (len
> sizeof(clnt
->cl_inline_name
)) {
159 char *buf
= kmalloc(len
, GFP_KERNEL
);
161 clnt
->cl_server
= buf
;
163 len
= sizeof(clnt
->cl_inline_name
);
165 strlcpy(clnt
->cl_server
, servname
, len
);
167 clnt
->cl_xprt
= xprt
;
168 clnt
->cl_procinfo
= version
->procs
;
169 clnt
->cl_maxproc
= version
->nrprocs
;
170 clnt
->cl_protname
= program
->name
;
171 clnt
->cl_prog
= program
->number
;
172 clnt
->cl_vers
= version
->number
;
173 clnt
->cl_stats
= program
->stats
;
174 clnt
->cl_metrics
= rpc_alloc_iostats(clnt
);
176 if (clnt
->cl_metrics
== NULL
)
178 clnt
->cl_program
= program
;
179 INIT_LIST_HEAD(&clnt
->cl_tasks
);
180 spin_lock_init(&clnt
->cl_lock
);
182 if (!xprt_bound(clnt
->cl_xprt
))
183 clnt
->cl_autobind
= 1;
185 clnt
->cl_rtt
= &clnt
->cl_rtt_default
;
186 rpc_init_rtt(&clnt
->cl_rtt_default
, xprt
->timeout
.to_initval
);
188 kref_init(&clnt
->cl_kref
);
190 err
= rpc_setup_pipedir(clnt
, program
->pipe_dir_name
);
194 auth
= rpcauth_create(flavor
, clnt
);
196 printk(KERN_INFO
"RPC: Couldn't create auth handle (flavor %u)\n",
202 /* save the nodename */
203 clnt
->cl_nodelen
= strlen(utsname()->nodename
);
204 if (clnt
->cl_nodelen
> UNX_MAXNODENAME
)
205 clnt
->cl_nodelen
= UNX_MAXNODENAME
;
206 memcpy(clnt
->cl_nodename
, utsname()->nodename
, clnt
->cl_nodelen
);
207 rpc_register_client(clnt
);
211 if (!IS_ERR(clnt
->cl_dentry
)) {
212 rpc_rmdir(clnt
->cl_dentry
);
216 rpc_free_iostats(clnt
->cl_metrics
);
218 if (clnt
->cl_server
!= clnt
->cl_inline_name
)
219 kfree(clnt
->cl_server
);
230 * rpc_create - create an RPC client and transport with one call
231 * @args: rpc_clnt create argument structure
233 * Creates and initializes an RPC transport and an RPC client.
235 * It can ping the server in order to determine if it is up, and to see if
236 * it supports this program and version. RPC_CLNT_CREATE_NOPING disables
237 * this behavior so asynchronous tasks can also use rpc_create.
239 struct rpc_clnt
*rpc_create(struct rpc_create_args
*args
)
241 struct rpc_xprt
*xprt
;
242 struct rpc_clnt
*clnt
;
243 struct xprt_create xprtargs
= {
244 .ident
= args
->protocol
,
245 .srcaddr
= args
->saddress
,
246 .dstaddr
= args
->address
,
247 .addrlen
= args
->addrsize
,
248 .timeout
= args
->timeout
252 xprt
= xprt_create_transport(&xprtargs
);
254 return (struct rpc_clnt
*)xprt
;
257 * If the caller chooses not to specify a hostname, whip
258 * up a string representation of the passed-in address.
260 if (args
->servername
== NULL
) {
261 struct sockaddr_in
*addr
=
262 (struct sockaddr_in
*) args
->address
;
263 snprintf(servername
, sizeof(servername
), NIPQUAD_FMT
,
264 NIPQUAD(addr
->sin_addr
.s_addr
));
265 args
->servername
= servername
;
269 * By default, kernel RPC client connects from a reserved port.
270 * CAP_NET_BIND_SERVICE will not be set for unprivileged requesters,
271 * but it is always enabled for rpciod, which handles the connect
275 if (args
->flags
& RPC_CLNT_CREATE_NONPRIVPORT
)
278 clnt
= rpc_new_client(xprt
, args
->servername
, args
->program
,
279 args
->version
, args
->authflavor
);
283 if (!(args
->flags
& RPC_CLNT_CREATE_NOPING
)) {
284 int err
= rpc_ping(clnt
, RPC_TASK_SOFT
|RPC_TASK_NOINTR
);
286 rpc_shutdown_client(clnt
);
291 clnt
->cl_softrtry
= 1;
292 if (args
->flags
& RPC_CLNT_CREATE_HARDRTRY
)
293 clnt
->cl_softrtry
= 0;
295 if (args
->flags
& RPC_CLNT_CREATE_INTR
)
297 if (args
->flags
& RPC_CLNT_CREATE_AUTOBIND
)
298 clnt
->cl_autobind
= 1;
299 if (args
->flags
& RPC_CLNT_CREATE_DISCRTRY
)
300 clnt
->cl_discrtry
= 1;
304 EXPORT_SYMBOL_GPL(rpc_create
);
307 * This function clones the RPC client structure. It allows us to share the
308 * same transport while varying parameters such as the authentication
312 rpc_clone_client(struct rpc_clnt
*clnt
)
314 struct rpc_clnt
*new;
317 new = kmemdup(clnt
, sizeof(*new), GFP_KERNEL
);
320 new->cl_parent
= clnt
;
321 /* Turn off autobind on clones */
322 new->cl_autobind
= 0;
323 INIT_LIST_HEAD(&new->cl_tasks
);
324 spin_lock_init(&new->cl_lock
);
325 rpc_init_rtt(&new->cl_rtt_default
, clnt
->cl_xprt
->timeout
.to_initval
);
326 new->cl_metrics
= rpc_alloc_iostats(clnt
);
327 if (new->cl_metrics
== NULL
)
329 kref_init(&new->cl_kref
);
330 err
= rpc_setup_pipedir(new, clnt
->cl_program
->pipe_dir_name
);
334 atomic_inc(&new->cl_auth
->au_count
);
335 xprt_get(clnt
->cl_xprt
);
336 kref_get(&clnt
->cl_kref
);
337 rpc_register_client(new);
341 rpc_free_iostats(new->cl_metrics
);
345 dprintk("RPC: %s: returned error %d\n", __FUNCTION__
, err
);
348 EXPORT_SYMBOL_GPL(rpc_clone_client
);
351 * Properly shut down an RPC client, terminating all outstanding
354 void rpc_shutdown_client(struct rpc_clnt
*clnt
)
356 dprintk("RPC: shutting down %s client for %s\n",
357 clnt
->cl_protname
, clnt
->cl_server
);
359 while (!list_empty(&clnt
->cl_tasks
)) {
360 rpc_killall_tasks(clnt
);
361 wait_event_timeout(destroy_wait
,
362 list_empty(&clnt
->cl_tasks
), 1*HZ
);
365 rpc_release_client(clnt
);
367 EXPORT_SYMBOL_GPL(rpc_shutdown_client
);
373 rpc_free_client(struct kref
*kref
)
375 struct rpc_clnt
*clnt
= container_of(kref
, struct rpc_clnt
, cl_kref
);
377 dprintk("RPC: destroying %s client for %s\n",
378 clnt
->cl_protname
, clnt
->cl_server
);
379 if (!IS_ERR(clnt
->cl_dentry
)) {
380 rpc_rmdir(clnt
->cl_dentry
);
383 if (clnt
->cl_parent
!= clnt
) {
384 rpc_release_client(clnt
->cl_parent
);
387 if (clnt
->cl_server
!= clnt
->cl_inline_name
)
388 kfree(clnt
->cl_server
);
390 rpc_unregister_client(clnt
);
391 rpc_free_iostats(clnt
->cl_metrics
);
392 clnt
->cl_metrics
= NULL
;
393 xprt_put(clnt
->cl_xprt
);
402 rpc_free_auth(struct kref
*kref
)
404 struct rpc_clnt
*clnt
= container_of(kref
, struct rpc_clnt
, cl_kref
);
406 if (clnt
->cl_auth
== NULL
) {
407 rpc_free_client(kref
);
412 * Note: RPCSEC_GSS may need to send NULL RPC calls in order to
413 * release remaining GSS contexts. This mechanism ensures
414 * that it can do so safely.
417 rpcauth_release(clnt
->cl_auth
);
418 clnt
->cl_auth
= NULL
;
419 kref_put(kref
, rpc_free_client
);
423 * Release reference to the RPC client
426 rpc_release_client(struct rpc_clnt
*clnt
)
428 dprintk("RPC: rpc_release_client(%p)\n", clnt
);
430 if (list_empty(&clnt
->cl_tasks
))
431 wake_up(&destroy_wait
);
432 kref_put(&clnt
->cl_kref
, rpc_free_auth
);
436 * rpc_bind_new_program - bind a new RPC program to an existing client
437 * @old - old rpc_client
438 * @program - rpc program to set
439 * @vers - rpc program version
441 * Clones the rpc client and sets up a new RPC program. This is mainly
442 * of use for enabling different RPC programs to share the same transport.
443 * The Sun NFSv2/v3 ACL protocol can do this.
445 struct rpc_clnt
*rpc_bind_new_program(struct rpc_clnt
*old
,
446 struct rpc_program
*program
,
449 struct rpc_clnt
*clnt
;
450 struct rpc_version
*version
;
453 BUG_ON(vers
>= program
->nrvers
|| !program
->version
[vers
]);
454 version
= program
->version
[vers
];
455 clnt
= rpc_clone_client(old
);
458 clnt
->cl_procinfo
= version
->procs
;
459 clnt
->cl_maxproc
= version
->nrprocs
;
460 clnt
->cl_protname
= program
->name
;
461 clnt
->cl_prog
= program
->number
;
462 clnt
->cl_vers
= version
->number
;
463 clnt
->cl_stats
= program
->stats
;
464 err
= rpc_ping(clnt
, RPC_TASK_SOFT
|RPC_TASK_NOINTR
);
466 rpc_shutdown_client(clnt
);
472 EXPORT_SYMBOL_GPL(rpc_bind_new_program
);
475 * Default callback for async RPC calls
478 rpc_default_callback(struct rpc_task
*task
, void *data
)
482 static const struct rpc_call_ops rpc_default_ops
= {
483 .rpc_call_done
= rpc_default_callback
,
487 * Export the signal mask handling for synchronous code that
488 * sleeps on RPC calls
490 #define RPC_INTR_SIGNALS (sigmask(SIGHUP) | sigmask(SIGINT) | sigmask(SIGQUIT) | sigmask(SIGTERM))
492 static void rpc_save_sigmask(sigset_t
*oldset
, int intr
)
494 unsigned long sigallow
= sigmask(SIGKILL
);
497 /* Block all signals except those listed in sigallow */
499 sigallow
|= RPC_INTR_SIGNALS
;
500 siginitsetinv(&sigmask
, sigallow
);
501 sigprocmask(SIG_BLOCK
, &sigmask
, oldset
);
504 static void rpc_task_sigmask(struct rpc_task
*task
, sigset_t
*oldset
)
506 rpc_save_sigmask(oldset
, !RPC_TASK_UNINTERRUPTIBLE(task
));
509 static void rpc_restore_sigmask(sigset_t
*oldset
)
511 sigprocmask(SIG_SETMASK
, oldset
, NULL
);
514 void rpc_clnt_sigmask(struct rpc_clnt
*clnt
, sigset_t
*oldset
)
516 rpc_save_sigmask(oldset
, clnt
->cl_intr
);
518 EXPORT_SYMBOL_GPL(rpc_clnt_sigmask
);
520 void rpc_clnt_sigunmask(struct rpc_clnt
*clnt
, sigset_t
*oldset
)
522 rpc_restore_sigmask(oldset
);
524 EXPORT_SYMBOL_GPL(rpc_clnt_sigunmask
);
527 * rpc_run_task - Allocate a new RPC task, then run rpc_execute against it
528 * @task_setup_data: pointer to task initialisation data
530 struct rpc_task
*rpc_run_task(const struct rpc_task_setup
*task_setup_data
)
532 struct rpc_task
*task
, *ret
;
535 task
= rpc_new_task(task_setup_data
);
537 rpc_release_calldata(task_setup_data
->callback_ops
,
538 task_setup_data
->callback_data
);
539 ret
= ERR_PTR(-ENOMEM
);
543 if (task
->tk_status
!= 0) {
544 ret
= ERR_PTR(task
->tk_status
);
548 atomic_inc(&task
->tk_count
);
549 /* Mask signals on synchronous RPC calls and RPCSEC_GSS upcalls */
550 rpc_task_sigmask(task
, &oldset
);
552 rpc_restore_sigmask(&oldset
);
557 EXPORT_SYMBOL_GPL(rpc_run_task
);
560 * rpc_call_sync - Perform a synchronous RPC call
561 * @clnt: pointer to RPC client
562 * @msg: RPC call parameters
563 * @flags: RPC call flags
565 int rpc_call_sync(struct rpc_clnt
*clnt
, struct rpc_message
*msg
, int flags
)
567 struct rpc_task
*task
;
568 struct rpc_task_setup task_setup_data
= {
571 .callback_ops
= &rpc_default_ops
,
576 BUG_ON(flags
& RPC_TASK_ASYNC
);
578 task
= rpc_run_task(&task_setup_data
);
580 return PTR_ERR(task
);
581 status
= task
->tk_status
;
585 EXPORT_SYMBOL_GPL(rpc_call_sync
);
588 * rpc_call_async - Perform an asynchronous RPC call
589 * @clnt: pointer to RPC client
590 * @msg: RPC call parameters
591 * @flags: RPC call flags
593 * @data: user call data
596 rpc_call_async(struct rpc_clnt
*clnt
, struct rpc_message
*msg
, int flags
,
597 const struct rpc_call_ops
*tk_ops
, void *data
)
599 struct rpc_task
*task
;
600 struct rpc_task_setup task_setup_data
= {
603 .callback_ops
= tk_ops
,
604 .callback_data
= data
,
605 .flags
= flags
|RPC_TASK_ASYNC
,
608 task
= rpc_run_task(&task_setup_data
);
610 return PTR_ERR(task
);
614 EXPORT_SYMBOL_GPL(rpc_call_async
);
617 rpc_call_start(struct rpc_task
*task
)
619 task
->tk_action
= call_start
;
621 EXPORT_SYMBOL_GPL(rpc_call_start
);
624 * rpc_peeraddr - extract remote peer address from clnt's xprt
625 * @clnt: RPC client structure
626 * @buf: target buffer
627 * @size: length of target buffer
629 * Returns the number of bytes that are actually in the stored address.
631 size_t rpc_peeraddr(struct rpc_clnt
*clnt
, struct sockaddr
*buf
, size_t bufsize
)
634 struct rpc_xprt
*xprt
= clnt
->cl_xprt
;
636 bytes
= sizeof(xprt
->addr
);
639 memcpy(buf
, &clnt
->cl_xprt
->addr
, bytes
);
640 return xprt
->addrlen
;
642 EXPORT_SYMBOL_GPL(rpc_peeraddr
);
645 * rpc_peeraddr2str - return remote peer address in printable format
646 * @clnt: RPC client structure
647 * @format: address format
650 char *rpc_peeraddr2str(struct rpc_clnt
*clnt
, enum rpc_display_format_t format
)
652 struct rpc_xprt
*xprt
= clnt
->cl_xprt
;
654 if (xprt
->address_strings
[format
] != NULL
)
655 return xprt
->address_strings
[format
];
657 return "unprintable";
659 EXPORT_SYMBOL_GPL(rpc_peeraddr2str
);
662 rpc_setbufsize(struct rpc_clnt
*clnt
, unsigned int sndsize
, unsigned int rcvsize
)
664 struct rpc_xprt
*xprt
= clnt
->cl_xprt
;
665 if (xprt
->ops
->set_buffer_size
)
666 xprt
->ops
->set_buffer_size(xprt
, sndsize
, rcvsize
);
668 EXPORT_SYMBOL_GPL(rpc_setbufsize
);
671 * Return size of largest payload RPC client can support, in bytes
673 * For stream transports, this is one RPC record fragment (see RFC
674 * 1831), as we don't support multi-record requests yet. For datagram
675 * transports, this is the size of an IP packet minus the IP, UDP, and
678 size_t rpc_max_payload(struct rpc_clnt
*clnt
)
680 return clnt
->cl_xprt
->max_payload
;
682 EXPORT_SYMBOL_GPL(rpc_max_payload
);
685 * rpc_force_rebind - force transport to check that remote port is unchanged
686 * @clnt: client to rebind
689 void rpc_force_rebind(struct rpc_clnt
*clnt
)
691 if (clnt
->cl_autobind
)
692 xprt_clear_bound(clnt
->cl_xprt
);
694 EXPORT_SYMBOL_GPL(rpc_force_rebind
);
697 * Restart an (async) RPC call. Usually called from within the
701 rpc_restart_call(struct rpc_task
*task
)
703 if (RPC_ASSASSINATED(task
))
706 task
->tk_action
= call_start
;
708 EXPORT_SYMBOL_GPL(rpc_restart_call
);
713 * Other FSM states can be visited zero or more times, but
714 * this state is visited exactly once for each RPC.
717 call_start(struct rpc_task
*task
)
719 struct rpc_clnt
*clnt
= task
->tk_client
;
721 dprintk("RPC: %5u call_start %s%d proc %d (%s)\n", task
->tk_pid
,
722 clnt
->cl_protname
, clnt
->cl_vers
,
723 task
->tk_msg
.rpc_proc
->p_proc
,
724 (RPC_IS_ASYNC(task
) ? "async" : "sync"));
726 /* Increment call count */
727 task
->tk_msg
.rpc_proc
->p_count
++;
728 clnt
->cl_stats
->rpccnt
++;
729 task
->tk_action
= call_reserve
;
733 * 1. Reserve an RPC call slot
736 call_reserve(struct rpc_task
*task
)
740 if (!rpcauth_uptodatecred(task
)) {
741 task
->tk_action
= call_refresh
;
746 task
->tk_action
= call_reserveresult
;
751 * 1b. Grok the result of xprt_reserve()
754 call_reserveresult(struct rpc_task
*task
)
756 int status
= task
->tk_status
;
761 * After a call to xprt_reserve(), we must have either
762 * a request slot or else an error status.
766 if (task
->tk_rqstp
) {
767 task
->tk_action
= call_allocate
;
771 printk(KERN_ERR
"%s: status=%d, but no request slot, exiting\n",
772 __FUNCTION__
, status
);
773 rpc_exit(task
, -EIO
);
778 * Even though there was an error, we may have acquired
779 * a request slot somehow. Make sure not to leak it.
781 if (task
->tk_rqstp
) {
782 printk(KERN_ERR
"%s: status=%d, request allocated anyway\n",
783 __FUNCTION__
, status
);
788 case -EAGAIN
: /* woken up; retry */
789 task
->tk_action
= call_reserve
;
791 case -EIO
: /* probably a shutdown */
794 printk(KERN_ERR
"%s: unrecognized error %d, exiting\n",
795 __FUNCTION__
, status
);
798 rpc_exit(task
, status
);
802 * 2. Allocate the buffer. For details, see sched.c:rpc_malloc.
803 * (Note: buffer memory is freed in xprt_release).
806 call_allocate(struct rpc_task
*task
)
808 unsigned int slack
= task
->tk_msg
.rpc_cred
->cr_auth
->au_cslack
;
809 struct rpc_rqst
*req
= task
->tk_rqstp
;
810 struct rpc_xprt
*xprt
= task
->tk_xprt
;
811 struct rpc_procinfo
*proc
= task
->tk_msg
.rpc_proc
;
816 task
->tk_action
= call_bind
;
821 if (proc
->p_proc
!= 0) {
822 BUG_ON(proc
->p_arglen
== 0);
823 if (proc
->p_decode
!= NULL
)
824 BUG_ON(proc
->p_replen
== 0);
828 * Calculate the size (in quads) of the RPC call
829 * and reply headers, and convert both values
832 req
->rq_callsize
= RPC_CALLHDRSIZE
+ (slack
<< 1) + proc
->p_arglen
;
833 req
->rq_callsize
<<= 2;
834 req
->rq_rcvsize
= RPC_REPHDRSIZE
+ slack
+ proc
->p_replen
;
835 req
->rq_rcvsize
<<= 2;
837 req
->rq_buffer
= xprt
->ops
->buf_alloc(task
,
838 req
->rq_callsize
+ req
->rq_rcvsize
);
839 if (req
->rq_buffer
!= NULL
)
842 dprintk("RPC: %5u rpc_buffer allocation failed\n", task
->tk_pid
);
844 if (RPC_IS_ASYNC(task
) || !signalled()) {
845 task
->tk_action
= call_allocate
;
846 rpc_delay(task
, HZ
>>4);
850 rpc_exit(task
, -ERESTARTSYS
);
854 rpc_task_need_encode(struct rpc_task
*task
)
856 return task
->tk_rqstp
->rq_snd_buf
.len
== 0;
860 rpc_task_force_reencode(struct rpc_task
*task
)
862 task
->tk_rqstp
->rq_snd_buf
.len
= 0;
866 rpc_xdr_buf_init(struct xdr_buf
*buf
, void *start
, size_t len
)
868 buf
->head
[0].iov_base
= start
;
869 buf
->head
[0].iov_len
= len
;
870 buf
->tail
[0].iov_len
= 0;
878 * 3. Encode arguments of an RPC call
881 call_encode(struct rpc_task
*task
)
883 struct rpc_rqst
*req
= task
->tk_rqstp
;
889 rpc_xdr_buf_init(&req
->rq_snd_buf
,
892 rpc_xdr_buf_init(&req
->rq_rcv_buf
,
893 (char *)req
->rq_buffer
+ req
->rq_callsize
,
896 /* Encode header and provided arguments */
897 encode
= task
->tk_msg
.rpc_proc
->p_encode
;
898 if (!(p
= call_header(task
))) {
899 printk(KERN_INFO
"RPC: call_header failed, exit EIO\n");
900 rpc_exit(task
, -EIO
);
906 task
->tk_status
= rpcauth_wrap_req(task
, encode
, req
, p
,
907 task
->tk_msg
.rpc_argp
);
908 if (task
->tk_status
== -ENOMEM
) {
909 /* XXX: Is this sane? */
910 rpc_delay(task
, 3*HZ
);
911 task
->tk_status
= -EAGAIN
;
916 * 4. Get the server port number if not yet set
919 call_bind(struct rpc_task
*task
)
921 struct rpc_xprt
*xprt
= task
->tk_xprt
;
925 task
->tk_action
= call_connect
;
926 if (!xprt_bound(xprt
)) {
927 task
->tk_action
= call_bind_status
;
928 task
->tk_timeout
= xprt
->bind_timeout
;
929 xprt
->ops
->rpcbind(task
);
934 * 4a. Sort out bind result
937 call_bind_status(struct rpc_task
*task
)
941 if (task
->tk_status
>= 0) {
944 task
->tk_action
= call_connect
;
948 switch (task
->tk_status
) {
950 dprintk("RPC: %5u rpcbind waiting for another request "
951 "to finish\n", task
->tk_pid
);
952 /* avoid busy-waiting here -- could be a network outage. */
953 rpc_delay(task
, 5*HZ
);
956 dprintk("RPC: %5u remote rpcbind: RPC program/version "
957 "unavailable\n", task
->tk_pid
);
958 /* fail immediately if this is an RPC ping */
959 if (task
->tk_msg
.rpc_proc
->p_proc
== 0) {
960 status
= -EOPNOTSUPP
;
963 rpc_delay(task
, 3*HZ
);
966 dprintk("RPC: %5u rpcbind request timed out\n",
970 /* server doesn't support any rpcbind version we know of */
971 dprintk("RPC: %5u remote rpcbind service unavailable\n",
974 case -EPROTONOSUPPORT
:
975 dprintk("RPC: %5u remote rpcbind version unavailable, retrying\n",
978 task
->tk_action
= call_bind
;
981 dprintk("RPC: %5u unrecognized rpcbind error (%d)\n",
982 task
->tk_pid
, -task
->tk_status
);
985 rpc_exit(task
, status
);
989 task
->tk_action
= call_timeout
;
993 * 4b. Connect to the RPC server
996 call_connect(struct rpc_task
*task
)
998 struct rpc_xprt
*xprt
= task
->tk_xprt
;
1000 dprintk("RPC: %5u call_connect xprt %p %s connected\n",
1002 (xprt_connected(xprt
) ? "is" : "is not"));
1004 task
->tk_action
= call_transmit
;
1005 if (!xprt_connected(xprt
)) {
1006 task
->tk_action
= call_connect_status
;
1007 if (task
->tk_status
< 0)
1014 * 4c. Sort out connect result
1017 call_connect_status(struct rpc_task
*task
)
1019 struct rpc_clnt
*clnt
= task
->tk_client
;
1020 int status
= task
->tk_status
;
1022 dprint_status(task
);
1024 task
->tk_status
= 0;
1026 clnt
->cl_stats
->netreconn
++;
1027 task
->tk_action
= call_transmit
;
1031 /* Something failed: remote service port may have changed */
1032 rpc_force_rebind(clnt
);
1037 task
->tk_action
= call_bind
;
1038 if (!RPC_IS_SOFT(task
))
1040 /* if soft mounted, test if we've timed out */
1042 task
->tk_action
= call_timeout
;
1045 rpc_exit(task
, -EIO
);
1049 * 5. Transmit the RPC request, and wait for reply
1052 call_transmit(struct rpc_task
*task
)
1054 dprint_status(task
);
1056 task
->tk_action
= call_status
;
1057 if (task
->tk_status
< 0)
1059 task
->tk_status
= xprt_prepare_transmit(task
);
1060 if (task
->tk_status
!= 0)
1062 task
->tk_action
= call_transmit_status
;
1063 /* Encode here so that rpcsec_gss can use correct sequence number. */
1064 if (rpc_task_need_encode(task
)) {
1065 BUG_ON(task
->tk_rqstp
->rq_bytes_sent
!= 0);
1067 /* Did the encode result in an error condition? */
1068 if (task
->tk_status
!= 0)
1071 xprt_transmit(task
);
1072 if (task
->tk_status
< 0)
1075 * On success, ensure that we call xprt_end_transmit() before sleeping
1076 * in order to allow access to the socket to other RPC requests.
1078 call_transmit_status(task
);
1079 if (task
->tk_msg
.rpc_proc
->p_decode
!= NULL
)
1081 task
->tk_action
= rpc_exit_task
;
1082 rpc_wake_up_task(task
);
1086 * 5a. Handle cleanup after a transmission
1089 call_transmit_status(struct rpc_task
*task
)
1091 task
->tk_action
= call_status
;
1093 * Special case: if we've been waiting on the socket's write_space()
1094 * callback, then don't call xprt_end_transmit().
1096 if (task
->tk_status
== -EAGAIN
)
1098 xprt_end_transmit(task
);
1099 rpc_task_force_reencode(task
);
1103 * 6. Sort out the RPC call status
1106 call_status(struct rpc_task
*task
)
1108 struct rpc_clnt
*clnt
= task
->tk_client
;
1109 struct rpc_rqst
*req
= task
->tk_rqstp
;
1112 if (req
->rq_received
> 0 && !req
->rq_bytes_sent
)
1113 task
->tk_status
= req
->rq_received
;
1115 dprint_status(task
);
1117 status
= task
->tk_status
;
1119 task
->tk_action
= call_decode
;
1123 task
->tk_status
= 0;
1129 * Delay any retries for 3 seconds, then handle as if it
1132 rpc_delay(task
, 3*HZ
);
1134 task
->tk_action
= call_timeout
;
1135 if (task
->tk_client
->cl_discrtry
)
1136 xprt_force_disconnect(task
->tk_xprt
);
1140 rpc_force_rebind(clnt
);
1141 task
->tk_action
= call_bind
;
1144 task
->tk_action
= call_transmit
;
1147 /* shutdown or soft timeout */
1148 rpc_exit(task
, status
);
1151 printk("%s: RPC call returned error %d\n",
1152 clnt
->cl_protname
, -status
);
1153 rpc_exit(task
, status
);
1158 * 6a. Handle RPC timeout
1159 * We do not release the request slot, so we keep using the
1160 * same XID for all retransmits.
1163 call_timeout(struct rpc_task
*task
)
1165 struct rpc_clnt
*clnt
= task
->tk_client
;
1167 if (xprt_adjust_timeout(task
->tk_rqstp
) == 0) {
1168 dprintk("RPC: %5u call_timeout (minor)\n", task
->tk_pid
);
1172 dprintk("RPC: %5u call_timeout (major)\n", task
->tk_pid
);
1173 task
->tk_timeouts
++;
1175 if (RPC_IS_SOFT(task
)) {
1176 printk(KERN_NOTICE
"%s: server %s not responding, timed out\n",
1177 clnt
->cl_protname
, clnt
->cl_server
);
1178 rpc_exit(task
, -EIO
);
1182 if (!(task
->tk_flags
& RPC_CALL_MAJORSEEN
)) {
1183 task
->tk_flags
|= RPC_CALL_MAJORSEEN
;
1184 printk(KERN_NOTICE
"%s: server %s not responding, still trying\n",
1185 clnt
->cl_protname
, clnt
->cl_server
);
1187 rpc_force_rebind(clnt
);
1190 clnt
->cl_stats
->rpcretrans
++;
1191 task
->tk_action
= call_bind
;
1192 task
->tk_status
= 0;
1196 * 7. Decode the RPC reply
1199 call_decode(struct rpc_task
*task
)
1201 struct rpc_clnt
*clnt
= task
->tk_client
;
1202 struct rpc_rqst
*req
= task
->tk_rqstp
;
1203 kxdrproc_t decode
= task
->tk_msg
.rpc_proc
->p_decode
;
1206 dprintk("RPC: %5u call_decode (status %d)\n",
1207 task
->tk_pid
, task
->tk_status
);
1209 if (task
->tk_flags
& RPC_CALL_MAJORSEEN
) {
1210 printk(KERN_NOTICE
"%s: server %s OK\n",
1211 clnt
->cl_protname
, clnt
->cl_server
);
1212 task
->tk_flags
&= ~RPC_CALL_MAJORSEEN
;
1215 if (task
->tk_status
< 12) {
1216 if (!RPC_IS_SOFT(task
)) {
1217 task
->tk_action
= call_bind
;
1218 clnt
->cl_stats
->rpcretrans
++;
1221 dprintk("RPC: %s: too small RPC reply size (%d bytes)\n",
1222 clnt
->cl_protname
, task
->tk_status
);
1223 task
->tk_action
= call_timeout
;
1228 * Ensure that we see all writes made by xprt_complete_rqst()
1229 * before it changed req->rq_received.
1232 req
->rq_rcv_buf
.len
= req
->rq_private_buf
.len
;
1234 /* Check that the softirq receive buffer is valid */
1235 WARN_ON(memcmp(&req
->rq_rcv_buf
, &req
->rq_private_buf
,
1236 sizeof(req
->rq_rcv_buf
)) != 0);
1238 /* Verify the RPC header */
1239 p
= call_verify(task
);
1241 if (p
== ERR_PTR(-EAGAIN
))
1246 task
->tk_action
= rpc_exit_task
;
1249 task
->tk_status
= rpcauth_unwrap_resp(task
, decode
, req
, p
,
1250 task
->tk_msg
.rpc_resp
);
1252 dprintk("RPC: %5u call_decode result %d\n", task
->tk_pid
,
1256 req
->rq_received
= req
->rq_private_buf
.len
= 0;
1257 task
->tk_status
= 0;
1258 if (task
->tk_client
->cl_discrtry
)
1259 xprt_force_disconnect(task
->tk_xprt
);
1263 * 8. Refresh the credentials if rejected by the server
1266 call_refresh(struct rpc_task
*task
)
1268 dprint_status(task
);
1270 task
->tk_action
= call_refreshresult
;
1271 task
->tk_status
= 0;
1272 task
->tk_client
->cl_stats
->rpcauthrefresh
++;
1273 rpcauth_refreshcred(task
);
1277 * 8a. Process the results of a credential refresh
1280 call_refreshresult(struct rpc_task
*task
)
1282 int status
= task
->tk_status
;
1284 dprint_status(task
);
1286 task
->tk_status
= 0;
1287 task
->tk_action
= call_reserve
;
1288 if (status
>= 0 && rpcauth_uptodatecred(task
))
1290 if (status
== -EACCES
) {
1291 rpc_exit(task
, -EACCES
);
1294 task
->tk_action
= call_refresh
;
1295 if (status
!= -ETIMEDOUT
)
1296 rpc_delay(task
, 3*HZ
);
1301 * Call header serialization
1304 call_header(struct rpc_task
*task
)
1306 struct rpc_clnt
*clnt
= task
->tk_client
;
1307 struct rpc_rqst
*req
= task
->tk_rqstp
;
1308 __be32
*p
= req
->rq_svec
[0].iov_base
;
1310 /* FIXME: check buffer size? */
1312 p
= xprt_skip_transport_header(task
->tk_xprt
, p
);
1313 *p
++ = req
->rq_xid
; /* XID */
1314 *p
++ = htonl(RPC_CALL
); /* CALL */
1315 *p
++ = htonl(RPC_VERSION
); /* RPC version */
1316 *p
++ = htonl(clnt
->cl_prog
); /* program number */
1317 *p
++ = htonl(clnt
->cl_vers
); /* program version */
1318 *p
++ = htonl(task
->tk_msg
.rpc_proc
->p_proc
); /* procedure */
1319 p
= rpcauth_marshcred(task
, p
);
1320 req
->rq_slen
= xdr_adjust_iovec(&req
->rq_svec
[0], p
);
1325 * Reply header verification
1328 call_verify(struct rpc_task
*task
)
1330 struct kvec
*iov
= &task
->tk_rqstp
->rq_rcv_buf
.head
[0];
1331 int len
= task
->tk_rqstp
->rq_rcv_buf
.len
>> 2;
1332 __be32
*p
= iov
->iov_base
;
1334 int error
= -EACCES
;
1336 if ((task
->tk_rqstp
->rq_rcv_buf
.len
& 3) != 0) {
1337 /* RFC-1014 says that the representation of XDR data must be a
1338 * multiple of four bytes
1339 * - if it isn't pointer subtraction in the NFS client may give
1342 dprintk("RPC: %5u %s: XDR representation not a multiple of"
1343 " 4 bytes: 0x%x\n", task
->tk_pid
, __FUNCTION__
,
1344 task
->tk_rqstp
->rq_rcv_buf
.len
);
1349 p
+= 1; /* skip XID */
1351 if ((n
= ntohl(*p
++)) != RPC_REPLY
) {
1352 dprintk("RPC: %5u %s: not an RPC reply: %x\n",
1353 task
->tk_pid
, __FUNCTION__
, n
);
1356 if ((n
= ntohl(*p
++)) != RPC_MSG_ACCEPTED
) {
1359 switch ((n
= ntohl(*p
++))) {
1360 case RPC_AUTH_ERROR
:
1363 dprintk("RPC: %5u %s: RPC call version "
1365 task
->tk_pid
, __FUNCTION__
);
1366 error
= -EPROTONOSUPPORT
;
1369 dprintk("RPC: %5u %s: RPC call rejected, "
1370 "unknown error: %x\n",
1371 task
->tk_pid
, __FUNCTION__
, n
);
1376 switch ((n
= ntohl(*p
++))) {
1377 case RPC_AUTH_REJECTEDCRED
:
1378 case RPC_AUTH_REJECTEDVERF
:
1379 case RPCSEC_GSS_CREDPROBLEM
:
1380 case RPCSEC_GSS_CTXPROBLEM
:
1381 if (!task
->tk_cred_retry
)
1383 task
->tk_cred_retry
--;
1384 dprintk("RPC: %5u %s: retry stale creds\n",
1385 task
->tk_pid
, __FUNCTION__
);
1386 rpcauth_invalcred(task
);
1387 /* Ensure we obtain a new XID! */
1389 task
->tk_action
= call_refresh
;
1391 case RPC_AUTH_BADCRED
:
1392 case RPC_AUTH_BADVERF
:
1393 /* possibly garbled cred/verf? */
1394 if (!task
->tk_garb_retry
)
1396 task
->tk_garb_retry
--;
1397 dprintk("RPC: %5u %s: retry garbled creds\n",
1398 task
->tk_pid
, __FUNCTION__
);
1399 task
->tk_action
= call_bind
;
1401 case RPC_AUTH_TOOWEAK
:
1402 printk(KERN_NOTICE
"call_verify: server %s requires stronger "
1403 "authentication.\n", task
->tk_client
->cl_server
);
1406 dprintk("RPC: %5u %s: unknown auth error: %x\n",
1407 task
->tk_pid
, __FUNCTION__
, n
);
1410 dprintk("RPC: %5u %s: call rejected %d\n",
1411 task
->tk_pid
, __FUNCTION__
, n
);
1414 if (!(p
= rpcauth_checkverf(task
, p
))) {
1415 dprintk("RPC: %5u %s: auth check failed\n",
1416 task
->tk_pid
, __FUNCTION__
);
1417 goto out_garbage
; /* bad verifier, retry */
1419 len
= p
- (__be32
*)iov
->iov_base
- 1;
1422 switch ((n
= ntohl(*p
++))) {
1425 case RPC_PROG_UNAVAIL
:
1426 dprintk("RPC: %5u %s: program %u is unsupported by server %s\n",
1427 task
->tk_pid
, __FUNCTION__
,
1428 (unsigned int)task
->tk_client
->cl_prog
,
1429 task
->tk_client
->cl_server
);
1430 error
= -EPFNOSUPPORT
;
1432 case RPC_PROG_MISMATCH
:
1433 dprintk("RPC: %5u %s: program %u, version %u unsupported by "
1434 "server %s\n", task
->tk_pid
, __FUNCTION__
,
1435 (unsigned int)task
->tk_client
->cl_prog
,
1436 (unsigned int)task
->tk_client
->cl_vers
,
1437 task
->tk_client
->cl_server
);
1438 error
= -EPROTONOSUPPORT
;
1440 case RPC_PROC_UNAVAIL
:
1441 dprintk("RPC: %5u %s: proc %p unsupported by program %u, "
1442 "version %u on server %s\n",
1443 task
->tk_pid
, __FUNCTION__
,
1444 task
->tk_msg
.rpc_proc
,
1445 task
->tk_client
->cl_prog
,
1446 task
->tk_client
->cl_vers
,
1447 task
->tk_client
->cl_server
);
1448 error
= -EOPNOTSUPP
;
1450 case RPC_GARBAGE_ARGS
:
1451 dprintk("RPC: %5u %s: server saw garbage\n",
1452 task
->tk_pid
, __FUNCTION__
);
1455 dprintk("RPC: %5u %s: server accept status: %x\n",
1456 task
->tk_pid
, __FUNCTION__
, n
);
1461 task
->tk_client
->cl_stats
->rpcgarbage
++;
1462 if (task
->tk_garb_retry
) {
1463 task
->tk_garb_retry
--;
1464 dprintk("RPC: %5u %s: retrying\n",
1465 task
->tk_pid
, __FUNCTION__
);
1466 task
->tk_action
= call_bind
;
1468 return ERR_PTR(-EAGAIN
);
1473 rpc_exit(task
, error
);
1474 dprintk("RPC: %5u %s: call failed with error %d\n", task
->tk_pid
,
1475 __FUNCTION__
, error
);
1476 return ERR_PTR(error
);
1478 dprintk("RPC: %5u %s: server reply was truncated.\n", task
->tk_pid
,
1483 static int rpcproc_encode_null(void *rqstp
, __be32
*data
, void *obj
)
1488 static int rpcproc_decode_null(void *rqstp
, __be32
*data
, void *obj
)
1493 static struct rpc_procinfo rpcproc_null
= {
1494 .p_encode
= rpcproc_encode_null
,
1495 .p_decode
= rpcproc_decode_null
,
1498 static int rpc_ping(struct rpc_clnt
*clnt
, int flags
)
1500 struct rpc_message msg
= {
1501 .rpc_proc
= &rpcproc_null
,
1504 msg
.rpc_cred
= authnull_ops
.lookup_cred(NULL
, NULL
, 0);
1505 err
= rpc_call_sync(clnt
, &msg
, flags
);
1506 put_rpccred(msg
.rpc_cred
);
1510 struct rpc_task
*rpc_call_null(struct rpc_clnt
*clnt
, struct rpc_cred
*cred
, int flags
)
1512 struct rpc_message msg
= {
1513 .rpc_proc
= &rpcproc_null
,
1516 struct rpc_task_setup task_setup_data
= {
1518 .rpc_message
= &msg
,
1519 .callback_ops
= &rpc_default_ops
,
1522 return rpc_run_task(&task_setup_data
);
1524 EXPORT_SYMBOL_GPL(rpc_call_null
);
1527 void rpc_show_tasks(void)
1529 struct rpc_clnt
*clnt
;
1532 spin_lock(&rpc_client_lock
);
1533 if (list_empty(&all_clients
))
1535 printk("-pid- proc flgs status -client- -prog- --rqstp- -timeout "
1536 "-rpcwait -action- ---ops--\n");
1537 list_for_each_entry(clnt
, &all_clients
, cl_clients
) {
1538 if (list_empty(&clnt
->cl_tasks
))
1540 spin_lock(&clnt
->cl_lock
);
1541 list_for_each_entry(t
, &clnt
->cl_tasks
, tk_task
) {
1542 const char *rpc_waitq
= "none";
1545 if (t
->tk_msg
.rpc_proc
)
1546 proc
= t
->tk_msg
.rpc_proc
->p_proc
;
1550 if (RPC_IS_QUEUED(t
))
1551 rpc_waitq
= rpc_qname(t
->u
.tk_wait
.rpc_waitq
);
1553 printk("%5u %04d %04x %6d %8p %6d %8p %8ld %8s %8p %8p\n",
1555 t
->tk_flags
, t
->tk_status
,
1557 (t
->tk_client
? t
->tk_client
->cl_prog
: 0),
1558 t
->tk_rqstp
, t
->tk_timeout
,
1560 t
->tk_action
, t
->tk_ops
);
1562 spin_unlock(&clnt
->cl_lock
);
1565 spin_unlock(&rpc_client_lock
);