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 dprintk("RPC: creating %s client for %s (xprt %p)\n",
133 program
->name
, servname
, xprt
);
141 if (vers
>= program
->nrvers
|| !(version
= program
->version
[vers
]))
145 clnt
= kzalloc(sizeof(*clnt
), GFP_KERNEL
);
148 clnt
->cl_parent
= clnt
;
150 clnt
->cl_server
= clnt
->cl_inline_name
;
151 len
= strlen(servname
) + 1;
152 if (len
> sizeof(clnt
->cl_inline_name
)) {
153 char *buf
= kmalloc(len
, GFP_KERNEL
);
155 clnt
->cl_server
= buf
;
157 len
= sizeof(clnt
->cl_inline_name
);
159 strlcpy(clnt
->cl_server
, servname
, len
);
161 clnt
->cl_xprt
= xprt
;
162 clnt
->cl_procinfo
= version
->procs
;
163 clnt
->cl_maxproc
= version
->nrprocs
;
164 clnt
->cl_protname
= program
->name
;
165 clnt
->cl_prog
= program
->number
;
166 clnt
->cl_vers
= version
->number
;
167 clnt
->cl_stats
= program
->stats
;
168 clnt
->cl_metrics
= rpc_alloc_iostats(clnt
);
170 if (clnt
->cl_metrics
== NULL
)
172 clnt
->cl_program
= program
;
173 INIT_LIST_HEAD(&clnt
->cl_tasks
);
174 spin_lock_init(&clnt
->cl_lock
);
176 if (!xprt_bound(clnt
->cl_xprt
))
177 clnt
->cl_autobind
= 1;
179 clnt
->cl_rtt
= &clnt
->cl_rtt_default
;
180 rpc_init_rtt(&clnt
->cl_rtt_default
, xprt
->timeout
.to_initval
);
182 kref_init(&clnt
->cl_kref
);
184 err
= rpc_setup_pipedir(clnt
, program
->pipe_dir_name
);
188 auth
= rpcauth_create(flavor
, clnt
);
190 printk(KERN_INFO
"RPC: Couldn't create auth handle (flavor %u)\n",
196 /* save the nodename */
197 clnt
->cl_nodelen
= strlen(utsname()->nodename
);
198 if (clnt
->cl_nodelen
> UNX_MAXNODENAME
)
199 clnt
->cl_nodelen
= UNX_MAXNODENAME
;
200 memcpy(clnt
->cl_nodename
, utsname()->nodename
, clnt
->cl_nodelen
);
201 rpc_register_client(clnt
);
205 if (!IS_ERR(clnt
->cl_dentry
)) {
206 rpc_rmdir(clnt
->cl_dentry
);
210 rpc_free_iostats(clnt
->cl_metrics
);
212 if (clnt
->cl_server
!= clnt
->cl_inline_name
)
213 kfree(clnt
->cl_server
);
224 * rpc_create - create an RPC client and transport with one call
225 * @args: rpc_clnt create argument structure
227 * Creates and initializes an RPC transport and an RPC client.
229 * It can ping the server in order to determine if it is up, and to see if
230 * it supports this program and version. RPC_CLNT_CREATE_NOPING disables
231 * this behavior so asynchronous tasks can also use rpc_create.
233 struct rpc_clnt
*rpc_create(struct rpc_create_args
*args
)
235 struct rpc_xprt
*xprt
;
236 struct rpc_clnt
*clnt
;
237 struct rpc_xprtsock_create xprtargs
= {
238 .proto
= args
->protocol
,
239 .srcaddr
= args
->saddress
,
240 .dstaddr
= args
->address
,
241 .addrlen
= args
->addrsize
,
242 .timeout
= args
->timeout
246 xprt
= xprt_create_transport(&xprtargs
);
248 return (struct rpc_clnt
*)xprt
;
251 * If the caller chooses not to specify a hostname, whip
252 * up a string representation of the passed-in address.
254 if (args
->servername
== NULL
) {
255 struct sockaddr_in
*addr
=
256 (struct sockaddr_in
*) &args
->address
;
257 snprintf(servername
, sizeof(servername
), NIPQUAD_FMT
,
258 NIPQUAD(addr
->sin_addr
.s_addr
));
259 args
->servername
= servername
;
263 * By default, kernel RPC client connects from a reserved port.
264 * CAP_NET_BIND_SERVICE will not be set for unprivileged requesters,
265 * but it is always enabled for rpciod, which handles the connect
269 if (args
->flags
& RPC_CLNT_CREATE_NONPRIVPORT
)
272 clnt
= rpc_new_client(xprt
, args
->servername
, args
->program
,
273 args
->version
, args
->authflavor
);
277 if (!(args
->flags
& RPC_CLNT_CREATE_NOPING
)) {
278 int err
= rpc_ping(clnt
, RPC_TASK_SOFT
|RPC_TASK_NOINTR
);
280 rpc_shutdown_client(clnt
);
285 clnt
->cl_softrtry
= 1;
286 if (args
->flags
& RPC_CLNT_CREATE_HARDRTRY
)
287 clnt
->cl_softrtry
= 0;
289 if (args
->flags
& RPC_CLNT_CREATE_INTR
)
291 if (args
->flags
& RPC_CLNT_CREATE_AUTOBIND
)
292 clnt
->cl_autobind
= 1;
293 if (args
->flags
& RPC_CLNT_CREATE_DISCRTRY
)
294 clnt
->cl_discrtry
= 1;
298 EXPORT_SYMBOL_GPL(rpc_create
);
301 * This function clones the RPC client structure. It allows us to share the
302 * same transport while varying parameters such as the authentication
306 rpc_clone_client(struct rpc_clnt
*clnt
)
308 struct rpc_clnt
*new;
311 new = kmemdup(clnt
, sizeof(*new), GFP_KERNEL
);
314 new->cl_parent
= clnt
;
315 /* Turn off autobind on clones */
316 new->cl_autobind
= 0;
317 INIT_LIST_HEAD(&new->cl_tasks
);
318 spin_lock_init(&new->cl_lock
);
319 rpc_init_rtt(&new->cl_rtt_default
, clnt
->cl_xprt
->timeout
.to_initval
);
320 new->cl_metrics
= rpc_alloc_iostats(clnt
);
321 if (new->cl_metrics
== NULL
)
323 kref_init(&new->cl_kref
);
324 err
= rpc_setup_pipedir(new, clnt
->cl_program
->pipe_dir_name
);
328 atomic_inc(&new->cl_auth
->au_count
);
329 xprt_get(clnt
->cl_xprt
);
330 kref_get(&clnt
->cl_kref
);
331 rpc_register_client(new);
335 rpc_free_iostats(new->cl_metrics
);
339 dprintk("RPC: %s: returned error %d\n", __FUNCTION__
, err
);
344 * Properly shut down an RPC client, terminating all outstanding
347 void rpc_shutdown_client(struct rpc_clnt
*clnt
)
349 dprintk("RPC: shutting down %s client for %s\n",
350 clnt
->cl_protname
, clnt
->cl_server
);
352 while (!list_empty(&clnt
->cl_tasks
)) {
353 rpc_killall_tasks(clnt
);
354 wait_event_timeout(destroy_wait
,
355 list_empty(&clnt
->cl_tasks
), 1*HZ
);
358 rpc_release_client(clnt
);
365 rpc_free_client(struct kref
*kref
)
367 struct rpc_clnt
*clnt
= container_of(kref
, struct rpc_clnt
, cl_kref
);
369 dprintk("RPC: destroying %s client for %s\n",
370 clnt
->cl_protname
, clnt
->cl_server
);
371 if (!IS_ERR(clnt
->cl_dentry
)) {
372 rpc_rmdir(clnt
->cl_dentry
);
375 if (clnt
->cl_parent
!= clnt
) {
376 rpc_release_client(clnt
->cl_parent
);
379 if (clnt
->cl_server
!= clnt
->cl_inline_name
)
380 kfree(clnt
->cl_server
);
382 rpc_unregister_client(clnt
);
383 rpc_free_iostats(clnt
->cl_metrics
);
384 clnt
->cl_metrics
= NULL
;
385 xprt_put(clnt
->cl_xprt
);
394 rpc_free_auth(struct kref
*kref
)
396 struct rpc_clnt
*clnt
= container_of(kref
, struct rpc_clnt
, cl_kref
);
398 if (clnt
->cl_auth
== NULL
) {
399 rpc_free_client(kref
);
404 * Note: RPCSEC_GSS may need to send NULL RPC calls in order to
405 * release remaining GSS contexts. This mechanism ensures
406 * that it can do so safely.
409 rpcauth_release(clnt
->cl_auth
);
410 clnt
->cl_auth
= NULL
;
411 kref_put(kref
, rpc_free_client
);
415 * Release reference to the RPC client
418 rpc_release_client(struct rpc_clnt
*clnt
)
420 dprintk("RPC: rpc_release_client(%p)\n", clnt
);
422 if (list_empty(&clnt
->cl_tasks
))
423 wake_up(&destroy_wait
);
424 kref_put(&clnt
->cl_kref
, rpc_free_auth
);
428 * rpc_bind_new_program - bind a new RPC program to an existing client
429 * @old - old rpc_client
430 * @program - rpc program to set
431 * @vers - rpc program version
433 * Clones the rpc client and sets up a new RPC program. This is mainly
434 * of use for enabling different RPC programs to share the same transport.
435 * The Sun NFSv2/v3 ACL protocol can do this.
437 struct rpc_clnt
*rpc_bind_new_program(struct rpc_clnt
*old
,
438 struct rpc_program
*program
,
441 struct rpc_clnt
*clnt
;
442 struct rpc_version
*version
;
445 BUG_ON(vers
>= program
->nrvers
|| !program
->version
[vers
]);
446 version
= program
->version
[vers
];
447 clnt
= rpc_clone_client(old
);
450 clnt
->cl_procinfo
= version
->procs
;
451 clnt
->cl_maxproc
= version
->nrprocs
;
452 clnt
->cl_protname
= program
->name
;
453 clnt
->cl_prog
= program
->number
;
454 clnt
->cl_vers
= version
->number
;
455 clnt
->cl_stats
= program
->stats
;
456 err
= rpc_ping(clnt
, RPC_TASK_SOFT
|RPC_TASK_NOINTR
);
458 rpc_shutdown_client(clnt
);
466 * Default callback for async RPC calls
469 rpc_default_callback(struct rpc_task
*task
, void *data
)
473 static const struct rpc_call_ops rpc_default_ops
= {
474 .rpc_call_done
= rpc_default_callback
,
478 * Export the signal mask handling for synchronous code that
479 * sleeps on RPC calls
481 #define RPC_INTR_SIGNALS (sigmask(SIGHUP) | sigmask(SIGINT) | sigmask(SIGQUIT) | sigmask(SIGTERM))
483 static void rpc_save_sigmask(sigset_t
*oldset
, int intr
)
485 unsigned long sigallow
= sigmask(SIGKILL
);
488 /* Block all signals except those listed in sigallow */
490 sigallow
|= RPC_INTR_SIGNALS
;
491 siginitsetinv(&sigmask
, sigallow
);
492 sigprocmask(SIG_BLOCK
, &sigmask
, oldset
);
495 static inline void rpc_task_sigmask(struct rpc_task
*task
, sigset_t
*oldset
)
497 rpc_save_sigmask(oldset
, !RPC_TASK_UNINTERRUPTIBLE(task
));
500 static inline void rpc_restore_sigmask(sigset_t
*oldset
)
502 sigprocmask(SIG_SETMASK
, oldset
, NULL
);
505 void rpc_clnt_sigmask(struct rpc_clnt
*clnt
, sigset_t
*oldset
)
507 rpc_save_sigmask(oldset
, clnt
->cl_intr
);
510 void rpc_clnt_sigunmask(struct rpc_clnt
*clnt
, sigset_t
*oldset
)
512 rpc_restore_sigmask(oldset
);
516 struct rpc_task
*rpc_do_run_task(struct rpc_clnt
*clnt
,
517 struct rpc_message
*msg
,
519 const struct rpc_call_ops
*ops
,
522 struct rpc_task
*task
, *ret
;
525 task
= rpc_new_task(clnt
, flags
, ops
, data
);
527 rpc_release_calldata(ops
, data
);
528 return ERR_PTR(-ENOMEM
);
531 /* Mask signals on synchronous RPC calls and RPCSEC_GSS upcalls */
532 rpc_task_sigmask(task
, &oldset
);
534 rpc_call_setup(task
, msg
, 0);
535 if (task
->tk_status
!= 0) {
536 ret
= ERR_PTR(task
->tk_status
);
541 atomic_inc(&task
->tk_count
);
545 rpc_restore_sigmask(&oldset
);
550 * rpc_call_sync - Perform a synchronous RPC call
551 * @clnt: pointer to RPC client
552 * @msg: RPC call parameters
553 * @flags: RPC call flags
555 int rpc_call_sync(struct rpc_clnt
*clnt
, struct rpc_message
*msg
, int flags
)
557 struct rpc_task
*task
;
560 BUG_ON(flags
& RPC_TASK_ASYNC
);
562 task
= rpc_do_run_task(clnt
, msg
, flags
, &rpc_default_ops
, NULL
);
564 return PTR_ERR(task
);
565 status
= task
->tk_status
;
571 * rpc_call_async - Perform an asynchronous RPC call
572 * @clnt: pointer to RPC client
573 * @msg: RPC call parameters
574 * @flags: RPC call flags
576 * @data: user call data
579 rpc_call_async(struct rpc_clnt
*clnt
, struct rpc_message
*msg
, int flags
,
580 const struct rpc_call_ops
*tk_ops
, void *data
)
582 struct rpc_task
*task
;
584 task
= rpc_do_run_task(clnt
, msg
, flags
|RPC_TASK_ASYNC
, tk_ops
, data
);
586 return PTR_ERR(task
);
592 * rpc_run_task - Allocate a new RPC task, then run rpc_execute against it
593 * @clnt: pointer to RPC client
596 * @data: user call data
598 struct rpc_task
*rpc_run_task(struct rpc_clnt
*clnt
, int flags
,
599 const struct rpc_call_ops
*tk_ops
,
602 return rpc_do_run_task(clnt
, NULL
, flags
, tk_ops
, data
);
604 EXPORT_SYMBOL(rpc_run_task
);
607 rpc_call_setup(struct rpc_task
*task
, struct rpc_message
*msg
, int flags
)
610 task
->tk_flags
|= flags
;
611 /* Bind the user cred */
612 if (task
->tk_msg
.rpc_cred
!= NULL
)
613 rpcauth_holdcred(task
);
615 rpcauth_bindcred(task
);
617 if (task
->tk_status
== 0)
618 task
->tk_action
= call_start
;
620 task
->tk_action
= rpc_exit_task
;
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
);
670 * Return size of largest payload RPC client can support, in bytes
672 * For stream transports, this is one RPC record fragment (see RFC
673 * 1831), as we don't support multi-record requests yet. For datagram
674 * transports, this is the size of an IP packet minus the IP, UDP, and
677 size_t rpc_max_payload(struct rpc_clnt
*clnt
)
679 return clnt
->cl_xprt
->max_payload
;
681 EXPORT_SYMBOL_GPL(rpc_max_payload
);
684 * rpc_force_rebind - force transport to check that remote port is unchanged
685 * @clnt: client to rebind
688 void rpc_force_rebind(struct rpc_clnt
*clnt
)
690 if (clnt
->cl_autobind
)
691 xprt_clear_bound(clnt
->cl_xprt
);
693 EXPORT_SYMBOL_GPL(rpc_force_rebind
);
696 * Restart an (async) RPC call. Usually called from within the
700 rpc_restart_call(struct rpc_task
*task
)
702 if (RPC_ASSASSINATED(task
))
705 task
->tk_action
= call_start
;
711 * Other FSM states can be visited zero or more times, but
712 * this state is visited exactly once for each RPC.
715 call_start(struct rpc_task
*task
)
717 struct rpc_clnt
*clnt
= task
->tk_client
;
719 dprintk("RPC: %5u call_start %s%d proc %d (%s)\n", task
->tk_pid
,
720 clnt
->cl_protname
, clnt
->cl_vers
,
721 task
->tk_msg
.rpc_proc
->p_proc
,
722 (RPC_IS_ASYNC(task
) ? "async" : "sync"));
724 /* Increment call count */
725 task
->tk_msg
.rpc_proc
->p_count
++;
726 clnt
->cl_stats
->rpccnt
++;
727 task
->tk_action
= call_reserve
;
731 * 1. Reserve an RPC call slot
734 call_reserve(struct rpc_task
*task
)
738 if (!rpcauth_uptodatecred(task
)) {
739 task
->tk_action
= call_refresh
;
744 task
->tk_action
= call_reserveresult
;
749 * 1b. Grok the result of xprt_reserve()
752 call_reserveresult(struct rpc_task
*task
)
754 int status
= task
->tk_status
;
759 * After a call to xprt_reserve(), we must have either
760 * a request slot or else an error status.
764 if (task
->tk_rqstp
) {
765 task
->tk_action
= call_allocate
;
769 printk(KERN_ERR
"%s: status=%d, but no request slot, exiting\n",
770 __FUNCTION__
, status
);
771 rpc_exit(task
, -EIO
);
776 * Even though there was an error, we may have acquired
777 * a request slot somehow. Make sure not to leak it.
779 if (task
->tk_rqstp
) {
780 printk(KERN_ERR
"%s: status=%d, request allocated anyway\n",
781 __FUNCTION__
, status
);
786 case -EAGAIN
: /* woken up; retry */
787 task
->tk_action
= call_reserve
;
789 case -EIO
: /* probably a shutdown */
792 printk(KERN_ERR
"%s: unrecognized error %d, exiting\n",
793 __FUNCTION__
, status
);
796 rpc_exit(task
, status
);
800 * 2. Allocate the buffer. For details, see sched.c:rpc_malloc.
801 * (Note: buffer memory is freed in xprt_release).
804 call_allocate(struct rpc_task
*task
)
806 unsigned int slack
= task
->tk_msg
.rpc_cred
->cr_auth
->au_cslack
;
807 struct rpc_rqst
*req
= task
->tk_rqstp
;
808 struct rpc_xprt
*xprt
= task
->tk_xprt
;
809 struct rpc_procinfo
*proc
= task
->tk_msg
.rpc_proc
;
814 task
->tk_action
= call_bind
;
819 if (proc
->p_proc
!= 0) {
820 BUG_ON(proc
->p_arglen
== 0);
821 if (proc
->p_decode
!= NULL
)
822 BUG_ON(proc
->p_replen
== 0);
826 * Calculate the size (in quads) of the RPC call
827 * and reply headers, and convert both values
830 req
->rq_callsize
= RPC_CALLHDRSIZE
+ (slack
<< 1) + proc
->p_arglen
;
831 req
->rq_callsize
<<= 2;
832 req
->rq_rcvsize
= RPC_REPHDRSIZE
+ slack
+ proc
->p_replen
;
833 req
->rq_rcvsize
<<= 2;
835 req
->rq_buffer
= xprt
->ops
->buf_alloc(task
,
836 req
->rq_callsize
+ req
->rq_rcvsize
);
837 if (req
->rq_buffer
!= NULL
)
840 dprintk("RPC: %5u rpc_buffer allocation failed\n", task
->tk_pid
);
842 if (RPC_IS_ASYNC(task
) || !signalled()) {
844 task
->tk_action
= call_reserve
;
845 rpc_delay(task
, HZ
>>4);
849 rpc_exit(task
, -ERESTARTSYS
);
853 rpc_task_need_encode(struct rpc_task
*task
)
855 return task
->tk_rqstp
->rq_snd_buf
.len
== 0;
859 rpc_task_force_reencode(struct rpc_task
*task
)
861 task
->tk_rqstp
->rq_snd_buf
.len
= 0;
865 rpc_xdr_buf_init(struct xdr_buf
*buf
, void *start
, size_t len
)
867 buf
->head
[0].iov_base
= start
;
868 buf
->head
[0].iov_len
= len
;
869 buf
->tail
[0].iov_len
= 0;
876 * 3. Encode arguments of an RPC call
879 call_encode(struct rpc_task
*task
)
881 struct rpc_rqst
*req
= task
->tk_rqstp
;
887 rpc_xdr_buf_init(&req
->rq_snd_buf
,
890 rpc_xdr_buf_init(&req
->rq_rcv_buf
,
891 (char *)req
->rq_buffer
+ req
->rq_callsize
,
894 /* Encode header and provided arguments */
895 encode
= task
->tk_msg
.rpc_proc
->p_encode
;
896 if (!(p
= call_header(task
))) {
897 printk(KERN_INFO
"RPC: call_header failed, exit EIO\n");
898 rpc_exit(task
, -EIO
);
904 task
->tk_status
= rpcauth_wrap_req(task
, encode
, req
, p
,
905 task
->tk_msg
.rpc_argp
);
906 if (task
->tk_status
== -ENOMEM
) {
907 /* XXX: Is this sane? */
908 rpc_delay(task
, 3*HZ
);
909 task
->tk_status
= -EAGAIN
;
914 * 4. Get the server port number if not yet set
917 call_bind(struct rpc_task
*task
)
919 struct rpc_xprt
*xprt
= task
->tk_xprt
;
923 task
->tk_action
= call_connect
;
924 if (!xprt_bound(xprt
)) {
925 task
->tk_action
= call_bind_status
;
926 task
->tk_timeout
= xprt
->bind_timeout
;
927 xprt
->ops
->rpcbind(task
);
932 * 4a. Sort out bind result
935 call_bind_status(struct rpc_task
*task
)
937 int status
= -EACCES
;
939 if (task
->tk_status
>= 0) {
942 task
->tk_action
= call_connect
;
946 switch (task
->tk_status
) {
948 dprintk("RPC: %5u remote rpcbind: RPC program/version "
949 "unavailable\n", task
->tk_pid
);
950 rpc_delay(task
, 3*HZ
);
953 dprintk("RPC: %5u rpcbind request timed out\n",
957 dprintk("RPC: %5u remote rpcbind service unavailable\n",
960 case -EPROTONOSUPPORT
:
961 dprintk("RPC: %5u remote rpcbind version unavailable, retrying\n",
964 task
->tk_action
= call_bind
;
967 dprintk("RPC: %5u unrecognized rpcbind error (%d)\n",
968 task
->tk_pid
, -task
->tk_status
);
972 rpc_exit(task
, status
);
976 task
->tk_action
= call_timeout
;
980 * 4b. Connect to the RPC server
983 call_connect(struct rpc_task
*task
)
985 struct rpc_xprt
*xprt
= task
->tk_xprt
;
987 dprintk("RPC: %5u call_connect xprt %p %s connected\n",
989 (xprt_connected(xprt
) ? "is" : "is not"));
991 task
->tk_action
= call_transmit
;
992 if (!xprt_connected(xprt
)) {
993 task
->tk_action
= call_connect_status
;
994 if (task
->tk_status
< 0)
1001 * 4c. Sort out connect result
1004 call_connect_status(struct rpc_task
*task
)
1006 struct rpc_clnt
*clnt
= task
->tk_client
;
1007 int status
= task
->tk_status
;
1009 dprint_status(task
);
1011 task
->tk_status
= 0;
1013 clnt
->cl_stats
->netreconn
++;
1014 task
->tk_action
= call_transmit
;
1018 /* Something failed: remote service port may have changed */
1019 rpc_force_rebind(clnt
);
1024 task
->tk_action
= call_bind
;
1025 if (!RPC_IS_SOFT(task
))
1027 /* if soft mounted, test if we've timed out */
1029 task
->tk_action
= call_timeout
;
1032 rpc_exit(task
, -EIO
);
1036 * 5. Transmit the RPC request, and wait for reply
1039 call_transmit(struct rpc_task
*task
)
1041 dprint_status(task
);
1043 task
->tk_action
= call_status
;
1044 if (task
->tk_status
< 0)
1046 task
->tk_status
= xprt_prepare_transmit(task
);
1047 if (task
->tk_status
!= 0)
1049 task
->tk_action
= call_transmit_status
;
1050 /* Encode here so that rpcsec_gss can use correct sequence number. */
1051 if (rpc_task_need_encode(task
)) {
1052 BUG_ON(task
->tk_rqstp
->rq_bytes_sent
!= 0);
1054 /* Did the encode result in an error condition? */
1055 if (task
->tk_status
!= 0)
1058 xprt_transmit(task
);
1059 if (task
->tk_status
< 0)
1062 * On success, ensure that we call xprt_end_transmit() before sleeping
1063 * in order to allow access to the socket to other RPC requests.
1065 call_transmit_status(task
);
1066 if (task
->tk_msg
.rpc_proc
->p_decode
!= NULL
)
1068 task
->tk_action
= rpc_exit_task
;
1069 rpc_wake_up_task(task
);
1073 * 5a. Handle cleanup after a transmission
1076 call_transmit_status(struct rpc_task
*task
)
1078 task
->tk_action
= call_status
;
1080 * Special case: if we've been waiting on the socket's write_space()
1081 * callback, then don't call xprt_end_transmit().
1083 if (task
->tk_status
== -EAGAIN
)
1085 xprt_end_transmit(task
);
1086 rpc_task_force_reencode(task
);
1090 * 6. Sort out the RPC call status
1093 call_status(struct rpc_task
*task
)
1095 struct rpc_clnt
*clnt
= task
->tk_client
;
1096 struct rpc_rqst
*req
= task
->tk_rqstp
;
1099 if (req
->rq_received
> 0 && !req
->rq_bytes_sent
)
1100 task
->tk_status
= req
->rq_received
;
1102 dprint_status(task
);
1104 status
= task
->tk_status
;
1106 task
->tk_action
= call_decode
;
1110 task
->tk_status
= 0;
1116 * Delay any retries for 3 seconds, then handle as if it
1119 rpc_delay(task
, 3*HZ
);
1121 task
->tk_action
= call_timeout
;
1122 if (task
->tk_client
->cl_discrtry
)
1123 xprt_disconnect(task
->tk_xprt
);
1127 rpc_force_rebind(clnt
);
1128 task
->tk_action
= call_bind
;
1131 task
->tk_action
= call_transmit
;
1134 /* shutdown or soft timeout */
1135 rpc_exit(task
, status
);
1138 printk("%s: RPC call returned error %d\n",
1139 clnt
->cl_protname
, -status
);
1140 rpc_exit(task
, status
);
1145 * 6a. Handle RPC timeout
1146 * We do not release the request slot, so we keep using the
1147 * same XID for all retransmits.
1150 call_timeout(struct rpc_task
*task
)
1152 struct rpc_clnt
*clnt
= task
->tk_client
;
1154 if (xprt_adjust_timeout(task
->tk_rqstp
) == 0) {
1155 dprintk("RPC: %5u call_timeout (minor)\n", task
->tk_pid
);
1159 dprintk("RPC: %5u call_timeout (major)\n", task
->tk_pid
);
1160 task
->tk_timeouts
++;
1162 if (RPC_IS_SOFT(task
)) {
1163 printk(KERN_NOTICE
"%s: server %s not responding, timed out\n",
1164 clnt
->cl_protname
, clnt
->cl_server
);
1165 rpc_exit(task
, -EIO
);
1169 if (!(task
->tk_flags
& RPC_CALL_MAJORSEEN
)) {
1170 task
->tk_flags
|= RPC_CALL_MAJORSEEN
;
1171 printk(KERN_NOTICE
"%s: server %s not responding, still trying\n",
1172 clnt
->cl_protname
, clnt
->cl_server
);
1174 rpc_force_rebind(clnt
);
1177 clnt
->cl_stats
->rpcretrans
++;
1178 task
->tk_action
= call_bind
;
1179 task
->tk_status
= 0;
1183 * 7. Decode the RPC reply
1186 call_decode(struct rpc_task
*task
)
1188 struct rpc_clnt
*clnt
= task
->tk_client
;
1189 struct rpc_rqst
*req
= task
->tk_rqstp
;
1190 kxdrproc_t decode
= task
->tk_msg
.rpc_proc
->p_decode
;
1193 dprintk("RPC: %5u call_decode (status %d)\n",
1194 task
->tk_pid
, task
->tk_status
);
1196 if (task
->tk_flags
& RPC_CALL_MAJORSEEN
) {
1197 printk(KERN_NOTICE
"%s: server %s OK\n",
1198 clnt
->cl_protname
, clnt
->cl_server
);
1199 task
->tk_flags
&= ~RPC_CALL_MAJORSEEN
;
1202 if (task
->tk_status
< 12) {
1203 if (!RPC_IS_SOFT(task
)) {
1204 task
->tk_action
= call_bind
;
1205 clnt
->cl_stats
->rpcretrans
++;
1208 dprintk("RPC: %s: too small RPC reply size (%d bytes)\n",
1209 clnt
->cl_protname
, task
->tk_status
);
1210 task
->tk_action
= call_timeout
;
1215 * Ensure that we see all writes made by xprt_complete_rqst()
1216 * before it changed req->rq_received.
1219 req
->rq_rcv_buf
.len
= req
->rq_private_buf
.len
;
1221 /* Check that the softirq receive buffer is valid */
1222 WARN_ON(memcmp(&req
->rq_rcv_buf
, &req
->rq_private_buf
,
1223 sizeof(req
->rq_rcv_buf
)) != 0);
1225 /* Verify the RPC header */
1226 p
= call_verify(task
);
1228 if (p
== ERR_PTR(-EAGAIN
))
1233 task
->tk_action
= rpc_exit_task
;
1236 task
->tk_status
= rpcauth_unwrap_resp(task
, decode
, req
, p
,
1237 task
->tk_msg
.rpc_resp
);
1239 dprintk("RPC: %5u call_decode result %d\n", task
->tk_pid
,
1243 req
->rq_received
= req
->rq_private_buf
.len
= 0;
1244 task
->tk_status
= 0;
1245 if (task
->tk_client
->cl_discrtry
)
1246 xprt_disconnect(task
->tk_xprt
);
1250 * 8. Refresh the credentials if rejected by the server
1253 call_refresh(struct rpc_task
*task
)
1255 dprint_status(task
);
1257 xprt_release(task
); /* Must do to obtain new XID */
1258 task
->tk_action
= call_refreshresult
;
1259 task
->tk_status
= 0;
1260 task
->tk_client
->cl_stats
->rpcauthrefresh
++;
1261 rpcauth_refreshcred(task
);
1265 * 8a. Process the results of a credential refresh
1268 call_refreshresult(struct rpc_task
*task
)
1270 int status
= task
->tk_status
;
1272 dprint_status(task
);
1274 task
->tk_status
= 0;
1275 task
->tk_action
= call_reserve
;
1276 if (status
>= 0 && rpcauth_uptodatecred(task
))
1278 if (status
== -EACCES
) {
1279 rpc_exit(task
, -EACCES
);
1282 task
->tk_action
= call_refresh
;
1283 if (status
!= -ETIMEDOUT
)
1284 rpc_delay(task
, 3*HZ
);
1289 * Call header serialization
1292 call_header(struct rpc_task
*task
)
1294 struct rpc_clnt
*clnt
= task
->tk_client
;
1295 struct rpc_rqst
*req
= task
->tk_rqstp
;
1296 __be32
*p
= req
->rq_svec
[0].iov_base
;
1298 /* FIXME: check buffer size? */
1300 p
= xprt_skip_transport_header(task
->tk_xprt
, p
);
1301 *p
++ = req
->rq_xid
; /* XID */
1302 *p
++ = htonl(RPC_CALL
); /* CALL */
1303 *p
++ = htonl(RPC_VERSION
); /* RPC version */
1304 *p
++ = htonl(clnt
->cl_prog
); /* program number */
1305 *p
++ = htonl(clnt
->cl_vers
); /* program version */
1306 *p
++ = htonl(task
->tk_msg
.rpc_proc
->p_proc
); /* procedure */
1307 p
= rpcauth_marshcred(task
, p
);
1308 req
->rq_slen
= xdr_adjust_iovec(&req
->rq_svec
[0], p
);
1313 * Reply header verification
1316 call_verify(struct rpc_task
*task
)
1318 struct kvec
*iov
= &task
->tk_rqstp
->rq_rcv_buf
.head
[0];
1319 int len
= task
->tk_rqstp
->rq_rcv_buf
.len
>> 2;
1320 __be32
*p
= iov
->iov_base
;
1322 int error
= -EACCES
;
1324 if ((task
->tk_rqstp
->rq_rcv_buf
.len
& 3) != 0) {
1325 /* RFC-1014 says that the representation of XDR data must be a
1326 * multiple of four bytes
1327 * - if it isn't pointer subtraction in the NFS client may give
1330 dprintk("RPC: %5u %s: XDR representation not a multiple of"
1331 " 4 bytes: 0x%x\n", task
->tk_pid
, __FUNCTION__
,
1332 task
->tk_rqstp
->rq_rcv_buf
.len
);
1337 p
+= 1; /* skip XID */
1339 if ((n
= ntohl(*p
++)) != RPC_REPLY
) {
1340 dprintk("RPC: %5u %s: not an RPC reply: %x\n",
1341 task
->tk_pid
, __FUNCTION__
, n
);
1344 if ((n
= ntohl(*p
++)) != RPC_MSG_ACCEPTED
) {
1347 switch ((n
= ntohl(*p
++))) {
1348 case RPC_AUTH_ERROR
:
1351 dprintk("RPC: %5u %s: RPC call version "
1353 task
->tk_pid
, __FUNCTION__
);
1354 error
= -EPROTONOSUPPORT
;
1357 dprintk("RPC: %5u %s: RPC call rejected, "
1358 "unknown error: %x\n",
1359 task
->tk_pid
, __FUNCTION__
, n
);
1364 switch ((n
= ntohl(*p
++))) {
1365 case RPC_AUTH_REJECTEDCRED
:
1366 case RPC_AUTH_REJECTEDVERF
:
1367 case RPCSEC_GSS_CREDPROBLEM
:
1368 case RPCSEC_GSS_CTXPROBLEM
:
1369 if (!task
->tk_cred_retry
)
1371 task
->tk_cred_retry
--;
1372 dprintk("RPC: %5u %s: retry stale creds\n",
1373 task
->tk_pid
, __FUNCTION__
);
1374 rpcauth_invalcred(task
);
1375 task
->tk_action
= call_refresh
;
1377 case RPC_AUTH_BADCRED
:
1378 case RPC_AUTH_BADVERF
:
1379 /* possibly garbled cred/verf? */
1380 if (!task
->tk_garb_retry
)
1382 task
->tk_garb_retry
--;
1383 dprintk("RPC: %5u %s: retry garbled creds\n",
1384 task
->tk_pid
, __FUNCTION__
);
1385 task
->tk_action
= call_bind
;
1387 case RPC_AUTH_TOOWEAK
:
1388 printk(KERN_NOTICE
"call_verify: server %s requires stronger "
1389 "authentication.\n", task
->tk_client
->cl_server
);
1392 dprintk("RPC: %5u %s: unknown auth error: %x\n",
1393 task
->tk_pid
, __FUNCTION__
, n
);
1396 dprintk("RPC: %5u %s: call rejected %d\n",
1397 task
->tk_pid
, __FUNCTION__
, n
);
1400 if (!(p
= rpcauth_checkverf(task
, p
))) {
1401 dprintk("RPC: %5u %s: auth check failed\n",
1402 task
->tk_pid
, __FUNCTION__
);
1403 goto out_garbage
; /* bad verifier, retry */
1405 len
= p
- (__be32
*)iov
->iov_base
- 1;
1408 switch ((n
= ntohl(*p
++))) {
1411 case RPC_PROG_UNAVAIL
:
1412 dprintk("RPC: %5u %s: program %u is unsupported by server %s\n",
1413 task
->tk_pid
, __FUNCTION__
,
1414 (unsigned int)task
->tk_client
->cl_prog
,
1415 task
->tk_client
->cl_server
);
1416 error
= -EPFNOSUPPORT
;
1418 case RPC_PROG_MISMATCH
:
1419 dprintk("RPC: %5u %s: program %u, version %u unsupported by "
1420 "server %s\n", task
->tk_pid
, __FUNCTION__
,
1421 (unsigned int)task
->tk_client
->cl_prog
,
1422 (unsigned int)task
->tk_client
->cl_vers
,
1423 task
->tk_client
->cl_server
);
1424 error
= -EPROTONOSUPPORT
;
1426 case RPC_PROC_UNAVAIL
:
1427 dprintk("RPC: %5u %s: proc %p unsupported by program %u, "
1428 "version %u on server %s\n",
1429 task
->tk_pid
, __FUNCTION__
,
1430 task
->tk_msg
.rpc_proc
,
1431 task
->tk_client
->cl_prog
,
1432 task
->tk_client
->cl_vers
,
1433 task
->tk_client
->cl_server
);
1434 error
= -EOPNOTSUPP
;
1436 case RPC_GARBAGE_ARGS
:
1437 dprintk("RPC: %5u %s: server saw garbage\n",
1438 task
->tk_pid
, __FUNCTION__
);
1441 dprintk("RPC: %5u %s: server accept status: %x\n",
1442 task
->tk_pid
, __FUNCTION__
, n
);
1447 task
->tk_client
->cl_stats
->rpcgarbage
++;
1448 if (task
->tk_garb_retry
) {
1449 task
->tk_garb_retry
--;
1450 dprintk("RPC: %5u %s: retrying\n",
1451 task
->tk_pid
, __FUNCTION__
);
1452 task
->tk_action
= call_bind
;
1454 return ERR_PTR(-EAGAIN
);
1459 rpc_exit(task
, error
);
1460 dprintk("RPC: %5u %s: call failed with error %d\n", task
->tk_pid
,
1461 __FUNCTION__
, error
);
1462 return ERR_PTR(error
);
1464 dprintk("RPC: %5u %s: server reply was truncated.\n", task
->tk_pid
,
1469 static int rpcproc_encode_null(void *rqstp
, __be32
*data
, void *obj
)
1474 static int rpcproc_decode_null(void *rqstp
, __be32
*data
, void *obj
)
1479 static struct rpc_procinfo rpcproc_null
= {
1480 .p_encode
= rpcproc_encode_null
,
1481 .p_decode
= rpcproc_decode_null
,
1484 static int rpc_ping(struct rpc_clnt
*clnt
, int flags
)
1486 struct rpc_message msg
= {
1487 .rpc_proc
= &rpcproc_null
,
1490 msg
.rpc_cred
= authnull_ops
.lookup_cred(NULL
, NULL
, 0);
1491 err
= rpc_call_sync(clnt
, &msg
, flags
);
1492 put_rpccred(msg
.rpc_cred
);
1496 struct rpc_task
*rpc_call_null(struct rpc_clnt
*clnt
, struct rpc_cred
*cred
, int flags
)
1498 struct rpc_message msg
= {
1499 .rpc_proc
= &rpcproc_null
,
1502 return rpc_do_run_task(clnt
, &msg
, flags
, &rpc_default_ops
, NULL
);
1504 EXPORT_SYMBOL(rpc_call_null
);
1507 void rpc_show_tasks(void)
1509 struct rpc_clnt
*clnt
;
1512 spin_lock(&rpc_client_lock
);
1513 if (list_empty(&all_clients
))
1515 printk("-pid- proc flgs status -client- -prog- --rqstp- -timeout "
1516 "-rpcwait -action- ---ops--\n");
1517 list_for_each_entry(clnt
, &all_clients
, cl_clients
) {
1518 if (list_empty(&clnt
->cl_tasks
))
1520 spin_lock(&clnt
->cl_lock
);
1521 list_for_each_entry(t
, &clnt
->cl_tasks
, tk_task
) {
1522 const char *rpc_waitq
= "none";
1524 if (RPC_IS_QUEUED(t
))
1525 rpc_waitq
= rpc_qname(t
->u
.tk_wait
.rpc_waitq
);
1527 printk("%5u %04d %04x %6d %8p %6d %8p %8ld %8s %8p %8p\n",
1529 (t
->tk_msg
.rpc_proc
? t
->tk_msg
.rpc_proc
->p_proc
: -1),
1530 t
->tk_flags
, t
->tk_status
,
1532 (t
->tk_client
? t
->tk_client
->cl_prog
: 0),
1533 t
->tk_rqstp
, t
->tk_timeout
,
1535 t
->tk_action
, t
->tk_ops
);
1537 spin_unlock(&clnt
->cl_lock
);
1540 spin_unlock(&rpc_client_lock
);