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>
33 #include <linux/in6.h>
35 #include <linux/sunrpc/clnt.h>
36 #include <linux/sunrpc/rpc_pipe_fs.h>
37 #include <linux/sunrpc/metrics.h>
41 # define RPCDBG_FACILITY RPCDBG_CALL
44 #define dprint_status(t) \
45 dprintk("RPC: %5u %s (status %d)\n", t->tk_pid, \
46 __FUNCTION__, t->tk_status)
49 * All RPC clients are linked into this list
51 static LIST_HEAD(all_clients
);
52 static DEFINE_SPINLOCK(rpc_client_lock
);
54 static DECLARE_WAIT_QUEUE_HEAD(destroy_wait
);
57 static void call_start(struct rpc_task
*task
);
58 static void call_reserve(struct rpc_task
*task
);
59 static void call_reserveresult(struct rpc_task
*task
);
60 static void call_allocate(struct rpc_task
*task
);
61 static void call_encode(struct rpc_task
*task
);
62 static void call_decode(struct rpc_task
*task
);
63 static void call_bind(struct rpc_task
*task
);
64 static void call_bind_status(struct rpc_task
*task
);
65 static void call_transmit(struct rpc_task
*task
);
66 static void call_status(struct rpc_task
*task
);
67 static void call_transmit_status(struct rpc_task
*task
);
68 static void call_refresh(struct rpc_task
*task
);
69 static void call_refreshresult(struct rpc_task
*task
);
70 static void call_timeout(struct rpc_task
*task
);
71 static void call_connect(struct rpc_task
*task
);
72 static void call_connect_status(struct rpc_task
*task
);
73 static __be32
* call_header(struct rpc_task
*task
);
74 static __be32
* call_verify(struct rpc_task
*task
);
76 static int rpc_ping(struct rpc_clnt
*clnt
, int flags
);
78 static void rpc_register_client(struct rpc_clnt
*clnt
)
80 spin_lock(&rpc_client_lock
);
81 list_add(&clnt
->cl_clients
, &all_clients
);
82 spin_unlock(&rpc_client_lock
);
85 static void rpc_unregister_client(struct rpc_clnt
*clnt
)
87 spin_lock(&rpc_client_lock
);
88 list_del(&clnt
->cl_clients
);
89 spin_unlock(&rpc_client_lock
);
93 rpc_setup_pipedir(struct rpc_clnt
*clnt
, char *dir_name
)
95 static uint32_t clntid
;
98 clnt
->cl_vfsmnt
= ERR_PTR(-ENOENT
);
99 clnt
->cl_dentry
= ERR_PTR(-ENOENT
);
100 if (dir_name
== NULL
)
103 clnt
->cl_vfsmnt
= rpc_get_mount();
104 if (IS_ERR(clnt
->cl_vfsmnt
))
105 return PTR_ERR(clnt
->cl_vfsmnt
);
108 snprintf(clnt
->cl_pathname
, sizeof(clnt
->cl_pathname
),
109 "%s/clnt%x", dir_name
,
110 (unsigned int)clntid
++);
111 clnt
->cl_pathname
[sizeof(clnt
->cl_pathname
) - 1] = '\0';
112 clnt
->cl_dentry
= rpc_mkdir(clnt
->cl_pathname
, clnt
);
113 if (!IS_ERR(clnt
->cl_dentry
))
115 error
= PTR_ERR(clnt
->cl_dentry
);
116 if (error
!= -EEXIST
) {
117 printk(KERN_INFO
"RPC: Couldn't create pipefs entry %s, error %d\n",
118 clnt
->cl_pathname
, error
);
125 static struct rpc_clnt
* rpc_new_client(const struct rpc_create_args
*args
, struct rpc_xprt
*xprt
)
127 struct rpc_program
*program
= args
->program
;
128 struct rpc_version
*version
;
129 struct rpc_clnt
*clnt
= NULL
;
130 struct rpc_auth
*auth
;
134 /* sanity check the name before trying to print it */
136 len
= strlen(args
->servername
);
137 if (len
> RPC_MAXNETNAMELEN
)
141 dprintk("RPC: creating %s client for %s (xprt %p)\n",
142 program
->name
, args
->servername
, xprt
);
151 if (args
->version
>= program
->nrvers
)
153 version
= program
->version
[args
->version
];
158 clnt
= kzalloc(sizeof(*clnt
), GFP_KERNEL
);
161 clnt
->cl_parent
= clnt
;
163 clnt
->cl_server
= clnt
->cl_inline_name
;
164 if (len
> sizeof(clnt
->cl_inline_name
)) {
165 char *buf
= kmalloc(len
, GFP_KERNEL
);
167 clnt
->cl_server
= buf
;
169 len
= sizeof(clnt
->cl_inline_name
);
171 strlcpy(clnt
->cl_server
, args
->servername
, len
);
173 clnt
->cl_xprt
= xprt
;
174 clnt
->cl_procinfo
= version
->procs
;
175 clnt
->cl_maxproc
= version
->nrprocs
;
176 clnt
->cl_protname
= program
->name
;
177 clnt
->cl_prog
= program
->number
;
178 clnt
->cl_vers
= version
->number
;
179 clnt
->cl_stats
= program
->stats
;
180 clnt
->cl_metrics
= rpc_alloc_iostats(clnt
);
182 if (clnt
->cl_metrics
== NULL
)
184 clnt
->cl_program
= program
;
185 INIT_LIST_HEAD(&clnt
->cl_tasks
);
186 spin_lock_init(&clnt
->cl_lock
);
188 if (!xprt_bound(clnt
->cl_xprt
))
189 clnt
->cl_autobind
= 1;
191 clnt
->cl_timeout
= xprt
->timeout
;
192 if (args
->timeout
!= NULL
) {
193 memcpy(&clnt
->cl_timeout_default
, args
->timeout
,
194 sizeof(clnt
->cl_timeout_default
));
195 clnt
->cl_timeout
= &clnt
->cl_timeout_default
;
198 clnt
->cl_rtt
= &clnt
->cl_rtt_default
;
199 rpc_init_rtt(&clnt
->cl_rtt_default
, clnt
->cl_timeout
->to_initval
);
201 kref_init(&clnt
->cl_kref
);
203 err
= rpc_setup_pipedir(clnt
, program
->pipe_dir_name
);
207 auth
= rpcauth_create(args
->authflavor
, clnt
);
209 printk(KERN_INFO
"RPC: Couldn't create auth handle (flavor %u)\n",
215 /* save the nodename */
216 clnt
->cl_nodelen
= strlen(utsname()->nodename
);
217 if (clnt
->cl_nodelen
> UNX_MAXNODENAME
)
218 clnt
->cl_nodelen
= UNX_MAXNODENAME
;
219 memcpy(clnt
->cl_nodename
, utsname()->nodename
, clnt
->cl_nodelen
);
220 rpc_register_client(clnt
);
224 if (!IS_ERR(clnt
->cl_dentry
)) {
225 rpc_rmdir(clnt
->cl_dentry
);
229 rpc_free_iostats(clnt
->cl_metrics
);
231 if (clnt
->cl_server
!= clnt
->cl_inline_name
)
232 kfree(clnt
->cl_server
);
243 * rpc_create - create an RPC client and transport with one call
244 * @args: rpc_clnt create argument structure
246 * Creates and initializes an RPC transport and an RPC client.
248 * It can ping the server in order to determine if it is up, and to see if
249 * it supports this program and version. RPC_CLNT_CREATE_NOPING disables
250 * this behavior so asynchronous tasks can also use rpc_create.
252 struct rpc_clnt
*rpc_create(struct rpc_create_args
*args
)
254 struct rpc_xprt
*xprt
;
255 struct rpc_clnt
*clnt
;
256 struct xprt_create xprtargs
= {
257 .ident
= args
->protocol
,
258 .srcaddr
= args
->saddress
,
259 .dstaddr
= args
->address
,
260 .addrlen
= args
->addrsize
,
264 xprt
= xprt_create_transport(&xprtargs
);
266 return (struct rpc_clnt
*)xprt
;
269 * If the caller chooses not to specify a hostname, whip
270 * up a string representation of the passed-in address.
272 if (args
->servername
== NULL
) {
273 servername
[0] = '\0';
274 switch (args
->address
->sa_family
) {
276 struct sockaddr_in
*sin
=
277 (struct sockaddr_in
*)args
->address
;
278 snprintf(servername
, sizeof(servername
), NIPQUAD_FMT
,
279 NIPQUAD(sin
->sin_addr
.s_addr
));
283 struct sockaddr_in6
*sin
=
284 (struct sockaddr_in6
*)args
->address
;
285 snprintf(servername
, sizeof(servername
), NIP6_FMT
,
286 NIP6(sin
->sin6_addr
));
290 /* caller wants default server name, but
291 * address family isn't recognized. */
292 return ERR_PTR(-EINVAL
);
294 args
->servername
= servername
;
297 xprt
= xprt_create_transport(&xprtargs
);
299 return (struct rpc_clnt
*)xprt
;
302 * By default, kernel RPC client connects from a reserved port.
303 * CAP_NET_BIND_SERVICE will not be set for unprivileged requesters,
304 * but it is always enabled for rpciod, which handles the connect
308 if (args
->flags
& RPC_CLNT_CREATE_NONPRIVPORT
)
311 clnt
= rpc_new_client(args
, xprt
);
315 if (!(args
->flags
& RPC_CLNT_CREATE_NOPING
)) {
316 int err
= rpc_ping(clnt
, RPC_TASK_SOFT
|RPC_TASK_NOINTR
);
318 rpc_shutdown_client(clnt
);
323 clnt
->cl_softrtry
= 1;
324 if (args
->flags
& RPC_CLNT_CREATE_HARDRTRY
)
325 clnt
->cl_softrtry
= 0;
327 if (args
->flags
& RPC_CLNT_CREATE_INTR
)
329 if (args
->flags
& RPC_CLNT_CREATE_AUTOBIND
)
330 clnt
->cl_autobind
= 1;
331 if (args
->flags
& RPC_CLNT_CREATE_DISCRTRY
)
332 clnt
->cl_discrtry
= 1;
336 EXPORT_SYMBOL_GPL(rpc_create
);
339 * This function clones the RPC client structure. It allows us to share the
340 * same transport while varying parameters such as the authentication
344 rpc_clone_client(struct rpc_clnt
*clnt
)
346 struct rpc_clnt
*new;
349 new = kmemdup(clnt
, sizeof(*new), GFP_KERNEL
);
352 new->cl_parent
= clnt
;
353 /* Turn off autobind on clones */
354 new->cl_autobind
= 0;
355 INIT_LIST_HEAD(&new->cl_tasks
);
356 spin_lock_init(&new->cl_lock
);
357 rpc_init_rtt(&new->cl_rtt_default
, clnt
->cl_timeout
->to_initval
);
358 new->cl_metrics
= rpc_alloc_iostats(clnt
);
359 if (new->cl_metrics
== NULL
)
361 kref_init(&new->cl_kref
);
362 err
= rpc_setup_pipedir(new, clnt
->cl_program
->pipe_dir_name
);
366 atomic_inc(&new->cl_auth
->au_count
);
367 xprt_get(clnt
->cl_xprt
);
368 kref_get(&clnt
->cl_kref
);
369 rpc_register_client(new);
373 rpc_free_iostats(new->cl_metrics
);
377 dprintk("RPC: %s: returned error %d\n", __FUNCTION__
, err
);
380 EXPORT_SYMBOL_GPL(rpc_clone_client
);
383 * Properly shut down an RPC client, terminating all outstanding
386 void rpc_shutdown_client(struct rpc_clnt
*clnt
)
388 dprintk("RPC: shutting down %s client for %s\n",
389 clnt
->cl_protname
, clnt
->cl_server
);
391 while (!list_empty(&clnt
->cl_tasks
)) {
392 rpc_killall_tasks(clnt
);
393 wait_event_timeout(destroy_wait
,
394 list_empty(&clnt
->cl_tasks
), 1*HZ
);
397 rpc_release_client(clnt
);
399 EXPORT_SYMBOL_GPL(rpc_shutdown_client
);
405 rpc_free_client(struct kref
*kref
)
407 struct rpc_clnt
*clnt
= container_of(kref
, struct rpc_clnt
, cl_kref
);
409 dprintk("RPC: destroying %s client for %s\n",
410 clnt
->cl_protname
, clnt
->cl_server
);
411 if (!IS_ERR(clnt
->cl_dentry
)) {
412 rpc_rmdir(clnt
->cl_dentry
);
415 if (clnt
->cl_parent
!= clnt
) {
416 rpc_release_client(clnt
->cl_parent
);
419 if (clnt
->cl_server
!= clnt
->cl_inline_name
)
420 kfree(clnt
->cl_server
);
422 rpc_unregister_client(clnt
);
423 rpc_free_iostats(clnt
->cl_metrics
);
424 clnt
->cl_metrics
= NULL
;
425 xprt_put(clnt
->cl_xprt
);
434 rpc_free_auth(struct kref
*kref
)
436 struct rpc_clnt
*clnt
= container_of(kref
, struct rpc_clnt
, cl_kref
);
438 if (clnt
->cl_auth
== NULL
) {
439 rpc_free_client(kref
);
444 * Note: RPCSEC_GSS may need to send NULL RPC calls in order to
445 * release remaining GSS contexts. This mechanism ensures
446 * that it can do so safely.
449 rpcauth_release(clnt
->cl_auth
);
450 clnt
->cl_auth
= NULL
;
451 kref_put(kref
, rpc_free_client
);
455 * Release reference to the RPC client
458 rpc_release_client(struct rpc_clnt
*clnt
)
460 dprintk("RPC: rpc_release_client(%p)\n", clnt
);
462 if (list_empty(&clnt
->cl_tasks
))
463 wake_up(&destroy_wait
);
464 kref_put(&clnt
->cl_kref
, rpc_free_auth
);
468 * rpc_bind_new_program - bind a new RPC program to an existing client
469 * @old - old rpc_client
470 * @program - rpc program to set
471 * @vers - rpc program version
473 * Clones the rpc client and sets up a new RPC program. This is mainly
474 * of use for enabling different RPC programs to share the same transport.
475 * The Sun NFSv2/v3 ACL protocol can do this.
477 struct rpc_clnt
*rpc_bind_new_program(struct rpc_clnt
*old
,
478 struct rpc_program
*program
,
481 struct rpc_clnt
*clnt
;
482 struct rpc_version
*version
;
485 BUG_ON(vers
>= program
->nrvers
|| !program
->version
[vers
]);
486 version
= program
->version
[vers
];
487 clnt
= rpc_clone_client(old
);
490 clnt
->cl_procinfo
= version
->procs
;
491 clnt
->cl_maxproc
= version
->nrprocs
;
492 clnt
->cl_protname
= program
->name
;
493 clnt
->cl_prog
= program
->number
;
494 clnt
->cl_vers
= version
->number
;
495 clnt
->cl_stats
= program
->stats
;
496 err
= rpc_ping(clnt
, RPC_TASK_SOFT
|RPC_TASK_NOINTR
);
498 rpc_shutdown_client(clnt
);
504 EXPORT_SYMBOL_GPL(rpc_bind_new_program
);
507 * Default callback for async RPC calls
510 rpc_default_callback(struct rpc_task
*task
, void *data
)
514 static const struct rpc_call_ops rpc_default_ops
= {
515 .rpc_call_done
= rpc_default_callback
,
519 * Export the signal mask handling for synchronous code that
520 * sleeps on RPC calls
522 #define RPC_INTR_SIGNALS (sigmask(SIGHUP) | sigmask(SIGINT) | sigmask(SIGQUIT) | sigmask(SIGTERM))
524 static void rpc_save_sigmask(sigset_t
*oldset
, int intr
)
526 unsigned long sigallow
= sigmask(SIGKILL
);
529 /* Block all signals except those listed in sigallow */
531 sigallow
|= RPC_INTR_SIGNALS
;
532 siginitsetinv(&sigmask
, sigallow
);
533 sigprocmask(SIG_BLOCK
, &sigmask
, oldset
);
536 static void rpc_task_sigmask(struct rpc_task
*task
, sigset_t
*oldset
)
538 rpc_save_sigmask(oldset
, !RPC_TASK_UNINTERRUPTIBLE(task
));
541 static void rpc_restore_sigmask(sigset_t
*oldset
)
543 sigprocmask(SIG_SETMASK
, oldset
, NULL
);
546 void rpc_clnt_sigmask(struct rpc_clnt
*clnt
, sigset_t
*oldset
)
548 rpc_save_sigmask(oldset
, clnt
->cl_intr
);
550 EXPORT_SYMBOL_GPL(rpc_clnt_sigmask
);
552 void rpc_clnt_sigunmask(struct rpc_clnt
*clnt
, sigset_t
*oldset
)
554 rpc_restore_sigmask(oldset
);
556 EXPORT_SYMBOL_GPL(rpc_clnt_sigunmask
);
559 * rpc_run_task - Allocate a new RPC task, then run rpc_execute against it
560 * @task_setup_data: pointer to task initialisation data
562 struct rpc_task
*rpc_run_task(const struct rpc_task_setup
*task_setup_data
)
564 struct rpc_task
*task
, *ret
;
567 task
= rpc_new_task(task_setup_data
);
569 rpc_release_calldata(task_setup_data
->callback_ops
,
570 task_setup_data
->callback_data
);
571 ret
= ERR_PTR(-ENOMEM
);
575 if (task
->tk_status
!= 0) {
576 ret
= ERR_PTR(task
->tk_status
);
580 atomic_inc(&task
->tk_count
);
581 /* Mask signals on synchronous RPC calls and RPCSEC_GSS upcalls */
582 if (!RPC_IS_ASYNC(task
)) {
583 rpc_task_sigmask(task
, &oldset
);
585 rpc_restore_sigmask(&oldset
);
592 EXPORT_SYMBOL_GPL(rpc_run_task
);
595 * rpc_call_sync - Perform a synchronous RPC call
596 * @clnt: pointer to RPC client
597 * @msg: RPC call parameters
598 * @flags: RPC call flags
600 int rpc_call_sync(struct rpc_clnt
*clnt
, struct rpc_message
*msg
, int flags
)
602 struct rpc_task
*task
;
603 struct rpc_task_setup task_setup_data
= {
606 .callback_ops
= &rpc_default_ops
,
611 BUG_ON(flags
& RPC_TASK_ASYNC
);
613 task
= rpc_run_task(&task_setup_data
);
615 return PTR_ERR(task
);
616 status
= task
->tk_status
;
620 EXPORT_SYMBOL_GPL(rpc_call_sync
);
623 * rpc_call_async - Perform an asynchronous RPC call
624 * @clnt: pointer to RPC client
625 * @msg: RPC call parameters
626 * @flags: RPC call flags
628 * @data: user call data
631 rpc_call_async(struct rpc_clnt
*clnt
, struct rpc_message
*msg
, int flags
,
632 const struct rpc_call_ops
*tk_ops
, void *data
)
634 struct rpc_task
*task
;
635 struct rpc_task_setup task_setup_data
= {
638 .callback_ops
= tk_ops
,
639 .callback_data
= data
,
640 .flags
= flags
|RPC_TASK_ASYNC
,
643 task
= rpc_run_task(&task_setup_data
);
645 return PTR_ERR(task
);
649 EXPORT_SYMBOL_GPL(rpc_call_async
);
652 rpc_call_start(struct rpc_task
*task
)
654 task
->tk_action
= call_start
;
656 EXPORT_SYMBOL_GPL(rpc_call_start
);
659 * rpc_peeraddr - extract remote peer address from clnt's xprt
660 * @clnt: RPC client structure
661 * @buf: target buffer
662 * @size: length of target buffer
664 * Returns the number of bytes that are actually in the stored address.
666 size_t rpc_peeraddr(struct rpc_clnt
*clnt
, struct sockaddr
*buf
, size_t bufsize
)
669 struct rpc_xprt
*xprt
= clnt
->cl_xprt
;
671 bytes
= sizeof(xprt
->addr
);
674 memcpy(buf
, &clnt
->cl_xprt
->addr
, bytes
);
675 return xprt
->addrlen
;
677 EXPORT_SYMBOL_GPL(rpc_peeraddr
);
680 * rpc_peeraddr2str - return remote peer address in printable format
681 * @clnt: RPC client structure
682 * @format: address format
685 const char *rpc_peeraddr2str(struct rpc_clnt
*clnt
,
686 enum rpc_display_format_t format
)
688 struct rpc_xprt
*xprt
= clnt
->cl_xprt
;
690 if (xprt
->address_strings
[format
] != NULL
)
691 return xprt
->address_strings
[format
];
693 return "unprintable";
695 EXPORT_SYMBOL_GPL(rpc_peeraddr2str
);
698 rpc_setbufsize(struct rpc_clnt
*clnt
, unsigned int sndsize
, unsigned int rcvsize
)
700 struct rpc_xprt
*xprt
= clnt
->cl_xprt
;
701 if (xprt
->ops
->set_buffer_size
)
702 xprt
->ops
->set_buffer_size(xprt
, sndsize
, rcvsize
);
704 EXPORT_SYMBOL_GPL(rpc_setbufsize
);
707 * Return size of largest payload RPC client can support, in bytes
709 * For stream transports, this is one RPC record fragment (see RFC
710 * 1831), as we don't support multi-record requests yet. For datagram
711 * transports, this is the size of an IP packet minus the IP, UDP, and
714 size_t rpc_max_payload(struct rpc_clnt
*clnt
)
716 return clnt
->cl_xprt
->max_payload
;
718 EXPORT_SYMBOL_GPL(rpc_max_payload
);
721 * rpc_force_rebind - force transport to check that remote port is unchanged
722 * @clnt: client to rebind
725 void rpc_force_rebind(struct rpc_clnt
*clnt
)
727 if (clnt
->cl_autobind
)
728 xprt_clear_bound(clnt
->cl_xprt
);
730 EXPORT_SYMBOL_GPL(rpc_force_rebind
);
733 * Restart an (async) RPC call. Usually called from within the
737 rpc_restart_call(struct rpc_task
*task
)
739 if (RPC_ASSASSINATED(task
))
742 task
->tk_action
= call_start
;
744 EXPORT_SYMBOL_GPL(rpc_restart_call
);
749 * Other FSM states can be visited zero or more times, but
750 * this state is visited exactly once for each RPC.
753 call_start(struct rpc_task
*task
)
755 struct rpc_clnt
*clnt
= task
->tk_client
;
757 dprintk("RPC: %5u call_start %s%d proc %d (%s)\n", task
->tk_pid
,
758 clnt
->cl_protname
, clnt
->cl_vers
,
759 task
->tk_msg
.rpc_proc
->p_proc
,
760 (RPC_IS_ASYNC(task
) ? "async" : "sync"));
762 /* Increment call count */
763 task
->tk_msg
.rpc_proc
->p_count
++;
764 clnt
->cl_stats
->rpccnt
++;
765 task
->tk_action
= call_reserve
;
769 * 1. Reserve an RPC call slot
772 call_reserve(struct rpc_task
*task
)
776 if (!rpcauth_uptodatecred(task
)) {
777 task
->tk_action
= call_refresh
;
782 task
->tk_action
= call_reserveresult
;
787 * 1b. Grok the result of xprt_reserve()
790 call_reserveresult(struct rpc_task
*task
)
792 int status
= task
->tk_status
;
797 * After a call to xprt_reserve(), we must have either
798 * a request slot or else an error status.
802 if (task
->tk_rqstp
) {
803 task
->tk_action
= call_allocate
;
807 printk(KERN_ERR
"%s: status=%d, but no request slot, exiting\n",
808 __FUNCTION__
, status
);
809 rpc_exit(task
, -EIO
);
814 * Even though there was an error, we may have acquired
815 * a request slot somehow. Make sure not to leak it.
817 if (task
->tk_rqstp
) {
818 printk(KERN_ERR
"%s: status=%d, request allocated anyway\n",
819 __FUNCTION__
, status
);
824 case -EAGAIN
: /* woken up; retry */
825 task
->tk_action
= call_reserve
;
827 case -EIO
: /* probably a shutdown */
830 printk(KERN_ERR
"%s: unrecognized error %d, exiting\n",
831 __FUNCTION__
, status
);
834 rpc_exit(task
, status
);
838 * 2. Allocate the buffer. For details, see sched.c:rpc_malloc.
839 * (Note: buffer memory is freed in xprt_release).
842 call_allocate(struct rpc_task
*task
)
844 unsigned int slack
= task
->tk_msg
.rpc_cred
->cr_auth
->au_cslack
;
845 struct rpc_rqst
*req
= task
->tk_rqstp
;
846 struct rpc_xprt
*xprt
= task
->tk_xprt
;
847 struct rpc_procinfo
*proc
= task
->tk_msg
.rpc_proc
;
852 task
->tk_action
= call_bind
;
857 if (proc
->p_proc
!= 0) {
858 BUG_ON(proc
->p_arglen
== 0);
859 if (proc
->p_decode
!= NULL
)
860 BUG_ON(proc
->p_replen
== 0);
864 * Calculate the size (in quads) of the RPC call
865 * and reply headers, and convert both values
868 req
->rq_callsize
= RPC_CALLHDRSIZE
+ (slack
<< 1) + proc
->p_arglen
;
869 req
->rq_callsize
<<= 2;
870 req
->rq_rcvsize
= RPC_REPHDRSIZE
+ slack
+ proc
->p_replen
;
871 req
->rq_rcvsize
<<= 2;
873 req
->rq_buffer
= xprt
->ops
->buf_alloc(task
,
874 req
->rq_callsize
+ req
->rq_rcvsize
);
875 if (req
->rq_buffer
!= NULL
)
878 dprintk("RPC: %5u rpc_buffer allocation failed\n", task
->tk_pid
);
880 if (RPC_IS_ASYNC(task
) || !signalled()) {
881 task
->tk_action
= call_allocate
;
882 rpc_delay(task
, HZ
>>4);
886 rpc_exit(task
, -ERESTARTSYS
);
890 rpc_task_need_encode(struct rpc_task
*task
)
892 return task
->tk_rqstp
->rq_snd_buf
.len
== 0;
896 rpc_task_force_reencode(struct rpc_task
*task
)
898 task
->tk_rqstp
->rq_snd_buf
.len
= 0;
902 rpc_xdr_buf_init(struct xdr_buf
*buf
, void *start
, size_t len
)
904 buf
->head
[0].iov_base
= start
;
905 buf
->head
[0].iov_len
= len
;
906 buf
->tail
[0].iov_len
= 0;
914 * 3. Encode arguments of an RPC call
917 call_encode(struct rpc_task
*task
)
919 struct rpc_rqst
*req
= task
->tk_rqstp
;
925 rpc_xdr_buf_init(&req
->rq_snd_buf
,
928 rpc_xdr_buf_init(&req
->rq_rcv_buf
,
929 (char *)req
->rq_buffer
+ req
->rq_callsize
,
932 /* Encode header and provided arguments */
933 encode
= task
->tk_msg
.rpc_proc
->p_encode
;
934 if (!(p
= call_header(task
))) {
935 printk(KERN_INFO
"RPC: call_header failed, exit EIO\n");
936 rpc_exit(task
, -EIO
);
942 task
->tk_status
= rpcauth_wrap_req(task
, encode
, req
, p
,
943 task
->tk_msg
.rpc_argp
);
944 if (task
->tk_status
== -ENOMEM
) {
945 /* XXX: Is this sane? */
946 rpc_delay(task
, 3*HZ
);
947 task
->tk_status
= -EAGAIN
;
952 * 4. Get the server port number if not yet set
955 call_bind(struct rpc_task
*task
)
957 struct rpc_xprt
*xprt
= task
->tk_xprt
;
961 task
->tk_action
= call_connect
;
962 if (!xprt_bound(xprt
)) {
963 task
->tk_action
= call_bind_status
;
964 task
->tk_timeout
= xprt
->bind_timeout
;
965 xprt
->ops
->rpcbind(task
);
970 * 4a. Sort out bind result
973 call_bind_status(struct rpc_task
*task
)
977 if (task
->tk_status
>= 0) {
980 task
->tk_action
= call_connect
;
984 switch (task
->tk_status
) {
986 dprintk("RPC: %5u rpcbind waiting for another request "
987 "to finish\n", task
->tk_pid
);
988 /* avoid busy-waiting here -- could be a network outage. */
989 rpc_delay(task
, 5*HZ
);
992 dprintk("RPC: %5u remote rpcbind: RPC program/version "
993 "unavailable\n", task
->tk_pid
);
994 /* fail immediately if this is an RPC ping */
995 if (task
->tk_msg
.rpc_proc
->p_proc
== 0) {
996 status
= -EOPNOTSUPP
;
999 rpc_delay(task
, 3*HZ
);
1002 dprintk("RPC: %5u rpcbind request timed out\n",
1006 /* server doesn't support any rpcbind version we know of */
1007 dprintk("RPC: %5u remote rpcbind service unavailable\n",
1010 case -EPROTONOSUPPORT
:
1011 dprintk("RPC: %5u remote rpcbind version unavailable, retrying\n",
1013 task
->tk_status
= 0;
1014 task
->tk_action
= call_bind
;
1017 dprintk("RPC: %5u unrecognized rpcbind error (%d)\n",
1018 task
->tk_pid
, -task
->tk_status
);
1021 rpc_exit(task
, status
);
1025 task
->tk_action
= call_timeout
;
1029 * 4b. Connect to the RPC server
1032 call_connect(struct rpc_task
*task
)
1034 struct rpc_xprt
*xprt
= task
->tk_xprt
;
1036 dprintk("RPC: %5u call_connect xprt %p %s connected\n",
1038 (xprt_connected(xprt
) ? "is" : "is not"));
1040 task
->tk_action
= call_transmit
;
1041 if (!xprt_connected(xprt
)) {
1042 task
->tk_action
= call_connect_status
;
1043 if (task
->tk_status
< 0)
1050 * 4c. Sort out connect result
1053 call_connect_status(struct rpc_task
*task
)
1055 struct rpc_clnt
*clnt
= task
->tk_client
;
1056 int status
= task
->tk_status
;
1058 dprint_status(task
);
1060 task
->tk_status
= 0;
1062 clnt
->cl_stats
->netreconn
++;
1063 task
->tk_action
= call_transmit
;
1067 /* Something failed: remote service port may have changed */
1068 rpc_force_rebind(clnt
);
1073 task
->tk_action
= call_bind
;
1074 if (!RPC_IS_SOFT(task
))
1076 /* if soft mounted, test if we've timed out */
1078 task
->tk_action
= call_timeout
;
1081 rpc_exit(task
, -EIO
);
1085 * 5. Transmit the RPC request, and wait for reply
1088 call_transmit(struct rpc_task
*task
)
1090 dprint_status(task
);
1092 task
->tk_action
= call_status
;
1093 if (task
->tk_status
< 0)
1095 task
->tk_status
= xprt_prepare_transmit(task
);
1096 if (task
->tk_status
!= 0)
1098 task
->tk_action
= call_transmit_status
;
1099 /* Encode here so that rpcsec_gss can use correct sequence number. */
1100 if (rpc_task_need_encode(task
)) {
1101 BUG_ON(task
->tk_rqstp
->rq_bytes_sent
!= 0);
1103 /* Did the encode result in an error condition? */
1104 if (task
->tk_status
!= 0)
1107 xprt_transmit(task
);
1108 if (task
->tk_status
< 0)
1111 * On success, ensure that we call xprt_end_transmit() before sleeping
1112 * in order to allow access to the socket to other RPC requests.
1114 call_transmit_status(task
);
1115 if (task
->tk_msg
.rpc_proc
->p_decode
!= NULL
)
1117 task
->tk_action
= rpc_exit_task
;
1118 rpc_wake_up_task(task
);
1122 * 5a. Handle cleanup after a transmission
1125 call_transmit_status(struct rpc_task
*task
)
1127 task
->tk_action
= call_status
;
1129 * Special case: if we've been waiting on the socket's write_space()
1130 * callback, then don't call xprt_end_transmit().
1132 if (task
->tk_status
== -EAGAIN
)
1134 xprt_end_transmit(task
);
1135 rpc_task_force_reencode(task
);
1139 * 6. Sort out the RPC call status
1142 call_status(struct rpc_task
*task
)
1144 struct rpc_clnt
*clnt
= task
->tk_client
;
1145 struct rpc_rqst
*req
= task
->tk_rqstp
;
1148 if (req
->rq_received
> 0 && !req
->rq_bytes_sent
)
1149 task
->tk_status
= req
->rq_received
;
1151 dprint_status(task
);
1153 status
= task
->tk_status
;
1155 task
->tk_action
= call_decode
;
1159 task
->tk_status
= 0;
1165 * Delay any retries for 3 seconds, then handle as if it
1168 rpc_delay(task
, 3*HZ
);
1170 task
->tk_action
= call_timeout
;
1171 if (task
->tk_client
->cl_discrtry
)
1172 xprt_force_disconnect(task
->tk_xprt
);
1176 rpc_force_rebind(clnt
);
1177 task
->tk_action
= call_bind
;
1180 task
->tk_action
= call_transmit
;
1183 /* shutdown or soft timeout */
1184 rpc_exit(task
, status
);
1187 printk("%s: RPC call returned error %d\n",
1188 clnt
->cl_protname
, -status
);
1189 rpc_exit(task
, status
);
1194 * 6a. Handle RPC timeout
1195 * We do not release the request slot, so we keep using the
1196 * same XID for all retransmits.
1199 call_timeout(struct rpc_task
*task
)
1201 struct rpc_clnt
*clnt
= task
->tk_client
;
1203 if (xprt_adjust_timeout(task
->tk_rqstp
) == 0) {
1204 dprintk("RPC: %5u call_timeout (minor)\n", task
->tk_pid
);
1208 dprintk("RPC: %5u call_timeout (major)\n", task
->tk_pid
);
1209 task
->tk_timeouts
++;
1211 if (RPC_IS_SOFT(task
)) {
1212 printk(KERN_NOTICE
"%s: server %s not responding, timed out\n",
1213 clnt
->cl_protname
, clnt
->cl_server
);
1214 rpc_exit(task
, -EIO
);
1218 if (!(task
->tk_flags
& RPC_CALL_MAJORSEEN
)) {
1219 task
->tk_flags
|= RPC_CALL_MAJORSEEN
;
1220 printk(KERN_NOTICE
"%s: server %s not responding, still trying\n",
1221 clnt
->cl_protname
, clnt
->cl_server
);
1223 rpc_force_rebind(clnt
);
1226 clnt
->cl_stats
->rpcretrans
++;
1227 task
->tk_action
= call_bind
;
1228 task
->tk_status
= 0;
1232 * 7. Decode the RPC reply
1235 call_decode(struct rpc_task
*task
)
1237 struct rpc_clnt
*clnt
= task
->tk_client
;
1238 struct rpc_rqst
*req
= task
->tk_rqstp
;
1239 kxdrproc_t decode
= task
->tk_msg
.rpc_proc
->p_decode
;
1242 dprintk("RPC: %5u call_decode (status %d)\n",
1243 task
->tk_pid
, task
->tk_status
);
1245 if (task
->tk_flags
& RPC_CALL_MAJORSEEN
) {
1246 printk(KERN_NOTICE
"%s: server %s OK\n",
1247 clnt
->cl_protname
, clnt
->cl_server
);
1248 task
->tk_flags
&= ~RPC_CALL_MAJORSEEN
;
1251 if (task
->tk_status
< 12) {
1252 if (!RPC_IS_SOFT(task
)) {
1253 task
->tk_action
= call_bind
;
1254 clnt
->cl_stats
->rpcretrans
++;
1257 dprintk("RPC: %s: too small RPC reply size (%d bytes)\n",
1258 clnt
->cl_protname
, task
->tk_status
);
1259 task
->tk_action
= call_timeout
;
1264 * Ensure that we see all writes made by xprt_complete_rqst()
1265 * before it changed req->rq_received.
1268 req
->rq_rcv_buf
.len
= req
->rq_private_buf
.len
;
1270 /* Check that the softirq receive buffer is valid */
1271 WARN_ON(memcmp(&req
->rq_rcv_buf
, &req
->rq_private_buf
,
1272 sizeof(req
->rq_rcv_buf
)) != 0);
1274 /* Verify the RPC header */
1275 p
= call_verify(task
);
1277 if (p
== ERR_PTR(-EAGAIN
))
1282 task
->tk_action
= rpc_exit_task
;
1285 task
->tk_status
= rpcauth_unwrap_resp(task
, decode
, req
, p
,
1286 task
->tk_msg
.rpc_resp
);
1288 dprintk("RPC: %5u call_decode result %d\n", task
->tk_pid
,
1292 req
->rq_received
= req
->rq_private_buf
.len
= 0;
1293 task
->tk_status
= 0;
1294 if (task
->tk_client
->cl_discrtry
)
1295 xprt_force_disconnect(task
->tk_xprt
);
1299 * 8. Refresh the credentials if rejected by the server
1302 call_refresh(struct rpc_task
*task
)
1304 dprint_status(task
);
1306 task
->tk_action
= call_refreshresult
;
1307 task
->tk_status
= 0;
1308 task
->tk_client
->cl_stats
->rpcauthrefresh
++;
1309 rpcauth_refreshcred(task
);
1313 * 8a. Process the results of a credential refresh
1316 call_refreshresult(struct rpc_task
*task
)
1318 int status
= task
->tk_status
;
1320 dprint_status(task
);
1322 task
->tk_status
= 0;
1323 task
->tk_action
= call_reserve
;
1324 if (status
>= 0 && rpcauth_uptodatecred(task
))
1326 if (status
== -EACCES
) {
1327 rpc_exit(task
, -EACCES
);
1330 task
->tk_action
= call_refresh
;
1331 if (status
!= -ETIMEDOUT
)
1332 rpc_delay(task
, 3*HZ
);
1337 * Call header serialization
1340 call_header(struct rpc_task
*task
)
1342 struct rpc_clnt
*clnt
= task
->tk_client
;
1343 struct rpc_rqst
*req
= task
->tk_rqstp
;
1344 __be32
*p
= req
->rq_svec
[0].iov_base
;
1346 /* FIXME: check buffer size? */
1348 p
= xprt_skip_transport_header(task
->tk_xprt
, p
);
1349 *p
++ = req
->rq_xid
; /* XID */
1350 *p
++ = htonl(RPC_CALL
); /* CALL */
1351 *p
++ = htonl(RPC_VERSION
); /* RPC version */
1352 *p
++ = htonl(clnt
->cl_prog
); /* program number */
1353 *p
++ = htonl(clnt
->cl_vers
); /* program version */
1354 *p
++ = htonl(task
->tk_msg
.rpc_proc
->p_proc
); /* procedure */
1355 p
= rpcauth_marshcred(task
, p
);
1356 req
->rq_slen
= xdr_adjust_iovec(&req
->rq_svec
[0], p
);
1361 * Reply header verification
1364 call_verify(struct rpc_task
*task
)
1366 struct kvec
*iov
= &task
->tk_rqstp
->rq_rcv_buf
.head
[0];
1367 int len
= task
->tk_rqstp
->rq_rcv_buf
.len
>> 2;
1368 __be32
*p
= iov
->iov_base
;
1370 int error
= -EACCES
;
1372 if ((task
->tk_rqstp
->rq_rcv_buf
.len
& 3) != 0) {
1373 /* RFC-1014 says that the representation of XDR data must be a
1374 * multiple of four bytes
1375 * - if it isn't pointer subtraction in the NFS client may give
1378 dprintk("RPC: %5u %s: XDR representation not a multiple of"
1379 " 4 bytes: 0x%x\n", task
->tk_pid
, __FUNCTION__
,
1380 task
->tk_rqstp
->rq_rcv_buf
.len
);
1385 p
+= 1; /* skip XID */
1387 if ((n
= ntohl(*p
++)) != RPC_REPLY
) {
1388 dprintk("RPC: %5u %s: not an RPC reply: %x\n",
1389 task
->tk_pid
, __FUNCTION__
, n
);
1392 if ((n
= ntohl(*p
++)) != RPC_MSG_ACCEPTED
) {
1395 switch ((n
= ntohl(*p
++))) {
1396 case RPC_AUTH_ERROR
:
1399 dprintk("RPC: %5u %s: RPC call version "
1401 task
->tk_pid
, __FUNCTION__
);
1402 error
= -EPROTONOSUPPORT
;
1405 dprintk("RPC: %5u %s: RPC call rejected, "
1406 "unknown error: %x\n",
1407 task
->tk_pid
, __FUNCTION__
, n
);
1412 switch ((n
= ntohl(*p
++))) {
1413 case RPC_AUTH_REJECTEDCRED
:
1414 case RPC_AUTH_REJECTEDVERF
:
1415 case RPCSEC_GSS_CREDPROBLEM
:
1416 case RPCSEC_GSS_CTXPROBLEM
:
1417 if (!task
->tk_cred_retry
)
1419 task
->tk_cred_retry
--;
1420 dprintk("RPC: %5u %s: retry stale creds\n",
1421 task
->tk_pid
, __FUNCTION__
);
1422 rpcauth_invalcred(task
);
1423 /* Ensure we obtain a new XID! */
1425 task
->tk_action
= call_refresh
;
1427 case RPC_AUTH_BADCRED
:
1428 case RPC_AUTH_BADVERF
:
1429 /* possibly garbled cred/verf? */
1430 if (!task
->tk_garb_retry
)
1432 task
->tk_garb_retry
--;
1433 dprintk("RPC: %5u %s: retry garbled creds\n",
1434 task
->tk_pid
, __FUNCTION__
);
1435 task
->tk_action
= call_bind
;
1437 case RPC_AUTH_TOOWEAK
:
1438 printk(KERN_NOTICE
"call_verify: server %s requires stronger "
1439 "authentication.\n", task
->tk_client
->cl_server
);
1442 dprintk("RPC: %5u %s: unknown auth error: %x\n",
1443 task
->tk_pid
, __FUNCTION__
, n
);
1446 dprintk("RPC: %5u %s: call rejected %d\n",
1447 task
->tk_pid
, __FUNCTION__
, n
);
1450 if (!(p
= rpcauth_checkverf(task
, p
))) {
1451 dprintk("RPC: %5u %s: auth check failed\n",
1452 task
->tk_pid
, __FUNCTION__
);
1453 goto out_garbage
; /* bad verifier, retry */
1455 len
= p
- (__be32
*)iov
->iov_base
- 1;
1458 switch ((n
= ntohl(*p
++))) {
1461 case RPC_PROG_UNAVAIL
:
1462 dprintk("RPC: %5u %s: program %u is unsupported by server %s\n",
1463 task
->tk_pid
, __FUNCTION__
,
1464 (unsigned int)task
->tk_client
->cl_prog
,
1465 task
->tk_client
->cl_server
);
1466 error
= -EPFNOSUPPORT
;
1468 case RPC_PROG_MISMATCH
:
1469 dprintk("RPC: %5u %s: program %u, version %u unsupported by "
1470 "server %s\n", task
->tk_pid
, __FUNCTION__
,
1471 (unsigned int)task
->tk_client
->cl_prog
,
1472 (unsigned int)task
->tk_client
->cl_vers
,
1473 task
->tk_client
->cl_server
);
1474 error
= -EPROTONOSUPPORT
;
1476 case RPC_PROC_UNAVAIL
:
1477 dprintk("RPC: %5u %s: proc %p unsupported by program %u, "
1478 "version %u on server %s\n",
1479 task
->tk_pid
, __FUNCTION__
,
1480 task
->tk_msg
.rpc_proc
,
1481 task
->tk_client
->cl_prog
,
1482 task
->tk_client
->cl_vers
,
1483 task
->tk_client
->cl_server
);
1484 error
= -EOPNOTSUPP
;
1486 case RPC_GARBAGE_ARGS
:
1487 dprintk("RPC: %5u %s: server saw garbage\n",
1488 task
->tk_pid
, __FUNCTION__
);
1491 dprintk("RPC: %5u %s: server accept status: %x\n",
1492 task
->tk_pid
, __FUNCTION__
, n
);
1497 task
->tk_client
->cl_stats
->rpcgarbage
++;
1498 if (task
->tk_garb_retry
) {
1499 task
->tk_garb_retry
--;
1500 dprintk("RPC: %5u %s: retrying\n",
1501 task
->tk_pid
, __FUNCTION__
);
1502 task
->tk_action
= call_bind
;
1504 return ERR_PTR(-EAGAIN
);
1509 rpc_exit(task
, error
);
1510 dprintk("RPC: %5u %s: call failed with error %d\n", task
->tk_pid
,
1511 __FUNCTION__
, error
);
1512 return ERR_PTR(error
);
1514 dprintk("RPC: %5u %s: server reply was truncated.\n", task
->tk_pid
,
1519 static int rpcproc_encode_null(void *rqstp
, __be32
*data
, void *obj
)
1524 static int rpcproc_decode_null(void *rqstp
, __be32
*data
, void *obj
)
1529 static struct rpc_procinfo rpcproc_null
= {
1530 .p_encode
= rpcproc_encode_null
,
1531 .p_decode
= rpcproc_decode_null
,
1534 static int rpc_ping(struct rpc_clnt
*clnt
, int flags
)
1536 struct rpc_message msg
= {
1537 .rpc_proc
= &rpcproc_null
,
1540 msg
.rpc_cred
= authnull_ops
.lookup_cred(NULL
, NULL
, 0);
1541 err
= rpc_call_sync(clnt
, &msg
, flags
);
1542 put_rpccred(msg
.rpc_cred
);
1546 struct rpc_task
*rpc_call_null(struct rpc_clnt
*clnt
, struct rpc_cred
*cred
, int flags
)
1548 struct rpc_message msg
= {
1549 .rpc_proc
= &rpcproc_null
,
1552 struct rpc_task_setup task_setup_data
= {
1554 .rpc_message
= &msg
,
1555 .callback_ops
= &rpc_default_ops
,
1558 return rpc_run_task(&task_setup_data
);
1560 EXPORT_SYMBOL_GPL(rpc_call_null
);
1563 void rpc_show_tasks(void)
1565 struct rpc_clnt
*clnt
;
1568 spin_lock(&rpc_client_lock
);
1569 if (list_empty(&all_clients
))
1571 printk("-pid- proc flgs status -client- -prog- --rqstp- -timeout "
1572 "-rpcwait -action- ---ops--\n");
1573 list_for_each_entry(clnt
, &all_clients
, cl_clients
) {
1574 if (list_empty(&clnt
->cl_tasks
))
1576 spin_lock(&clnt
->cl_lock
);
1577 list_for_each_entry(t
, &clnt
->cl_tasks
, tk_task
) {
1578 const char *rpc_waitq
= "none";
1581 if (t
->tk_msg
.rpc_proc
)
1582 proc
= t
->tk_msg
.rpc_proc
->p_proc
;
1586 if (RPC_IS_QUEUED(t
))
1587 rpc_waitq
= rpc_qname(t
->u
.tk_wait
.rpc_waitq
);
1589 printk("%5u %04d %04x %6d %8p %6d %8p %8ld %8s %8p %8p\n",
1591 t
->tk_flags
, t
->tk_status
,
1593 (t
->tk_client
? t
->tk_client
->cl_prog
: 0),
1594 t
->tk_rqstp
, t
->tk_timeout
,
1596 t
->tk_action
, t
->tk_ops
);
1598 spin_unlock(&clnt
->cl_lock
);
1601 spin_unlock(&rpc_client_lock
);