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)
47 static DECLARE_WAIT_QUEUE_HEAD(destroy_wait
);
50 static void call_start(struct rpc_task
*task
);
51 static void call_reserve(struct rpc_task
*task
);
52 static void call_reserveresult(struct rpc_task
*task
);
53 static void call_allocate(struct rpc_task
*task
);
54 static void call_encode(struct rpc_task
*task
);
55 static void call_decode(struct rpc_task
*task
);
56 static void call_bind(struct rpc_task
*task
);
57 static void call_bind_status(struct rpc_task
*task
);
58 static void call_transmit(struct rpc_task
*task
);
59 static void call_status(struct rpc_task
*task
);
60 static void call_transmit_status(struct rpc_task
*task
);
61 static void call_refresh(struct rpc_task
*task
);
62 static void call_refreshresult(struct rpc_task
*task
);
63 static void call_timeout(struct rpc_task
*task
);
64 static void call_connect(struct rpc_task
*task
);
65 static void call_connect_status(struct rpc_task
*task
);
66 static __be32
* call_header(struct rpc_task
*task
);
67 static __be32
* call_verify(struct rpc_task
*task
);
71 rpc_setup_pipedir(struct rpc_clnt
*clnt
, char *dir_name
)
73 static uint32_t clntid
;
76 clnt
->cl_vfsmnt
= ERR_PTR(-ENOENT
);
77 clnt
->cl_dentry
= ERR_PTR(-ENOENT
);
81 clnt
->cl_vfsmnt
= rpc_get_mount();
82 if (IS_ERR(clnt
->cl_vfsmnt
))
83 return PTR_ERR(clnt
->cl_vfsmnt
);
86 snprintf(clnt
->cl_pathname
, sizeof(clnt
->cl_pathname
),
87 "%s/clnt%x", dir_name
,
88 (unsigned int)clntid
++);
89 clnt
->cl_pathname
[sizeof(clnt
->cl_pathname
) - 1] = '\0';
90 clnt
->cl_dentry
= rpc_mkdir(clnt
->cl_pathname
, clnt
);
91 if (!IS_ERR(clnt
->cl_dentry
))
93 error
= PTR_ERR(clnt
->cl_dentry
);
94 if (error
!= -EEXIST
) {
95 printk(KERN_INFO
"RPC: Couldn't create pipefs entry %s, error %d\n",
96 clnt
->cl_pathname
, error
);
103 static struct rpc_clnt
* rpc_new_client(struct rpc_xprt
*xprt
, char *servname
, struct rpc_program
*program
, u32 vers
, rpc_authflavor_t flavor
)
105 struct rpc_version
*version
;
106 struct rpc_clnt
*clnt
= NULL
;
107 struct rpc_auth
*auth
;
111 dprintk("RPC: creating %s client for %s (xprt %p)\n",
112 program
->name
, servname
, xprt
);
120 if (vers
>= program
->nrvers
|| !(version
= program
->version
[vers
]))
124 clnt
= kzalloc(sizeof(*clnt
), GFP_KERNEL
);
127 clnt
->cl_parent
= clnt
;
129 clnt
->cl_server
= clnt
->cl_inline_name
;
130 len
= strlen(servname
) + 1;
131 if (len
> sizeof(clnt
->cl_inline_name
)) {
132 char *buf
= kmalloc(len
, GFP_KERNEL
);
134 clnt
->cl_server
= buf
;
136 len
= sizeof(clnt
->cl_inline_name
);
138 strlcpy(clnt
->cl_server
, servname
, len
);
140 clnt
->cl_xprt
= xprt
;
141 clnt
->cl_procinfo
= version
->procs
;
142 clnt
->cl_maxproc
= version
->nrprocs
;
143 clnt
->cl_protname
= program
->name
;
144 clnt
->cl_prog
= program
->number
;
145 clnt
->cl_vers
= version
->number
;
146 clnt
->cl_stats
= program
->stats
;
147 clnt
->cl_metrics
= rpc_alloc_iostats(clnt
);
149 if (clnt
->cl_metrics
== NULL
)
151 clnt
->cl_program
= program
;
152 INIT_LIST_HEAD(&clnt
->cl_tasks
);
153 spin_lock_init(&clnt
->cl_lock
);
155 if (!xprt_bound(clnt
->cl_xprt
))
156 clnt
->cl_autobind
= 1;
158 clnt
->cl_rtt
= &clnt
->cl_rtt_default
;
159 rpc_init_rtt(&clnt
->cl_rtt_default
, xprt
->timeout
.to_initval
);
161 kref_init(&clnt
->cl_kref
);
163 err
= rpc_setup_pipedir(clnt
, program
->pipe_dir_name
);
167 auth
= rpcauth_create(flavor
, clnt
);
169 printk(KERN_INFO
"RPC: Couldn't create auth handle (flavor %u)\n",
175 /* save the nodename */
176 clnt
->cl_nodelen
= strlen(utsname()->nodename
);
177 if (clnt
->cl_nodelen
> UNX_MAXNODENAME
)
178 clnt
->cl_nodelen
= UNX_MAXNODENAME
;
179 memcpy(clnt
->cl_nodename
, utsname()->nodename
, clnt
->cl_nodelen
);
180 rpc_register_client(clnt
);
184 if (!IS_ERR(clnt
->cl_dentry
)) {
185 rpc_rmdir(clnt
->cl_dentry
);
189 rpc_free_iostats(clnt
->cl_metrics
);
191 if (clnt
->cl_server
!= clnt
->cl_inline_name
)
192 kfree(clnt
->cl_server
);
203 * rpc_create - create an RPC client and transport with one call
204 * @args: rpc_clnt create argument structure
206 * Creates and initializes an RPC transport and an RPC client.
208 * It can ping the server in order to determine if it is up, and to see if
209 * it supports this program and version. RPC_CLNT_CREATE_NOPING disables
210 * this behavior so asynchronous tasks can also use rpc_create.
212 struct rpc_clnt
*rpc_create(struct rpc_create_args
*args
)
214 struct rpc_xprt
*xprt
;
215 struct rpc_clnt
*clnt
;
217 xprt
= xprt_create_transport(args
->protocol
, args
->address
,
218 args
->addrsize
, args
->timeout
);
220 return (struct rpc_clnt
*)xprt
;
223 * By default, kernel RPC client connects from a reserved port.
224 * CAP_NET_BIND_SERVICE will not be set for unprivileged requesters,
225 * but it is always enabled for rpciod, which handles the connect
229 if (args
->flags
& RPC_CLNT_CREATE_NONPRIVPORT
)
232 dprintk("RPC: creating %s client for %s (xprt %p)\n",
233 args
->program
->name
, args
->servername
, xprt
);
235 clnt
= rpc_new_client(xprt
, args
->servername
, args
->program
,
236 args
->version
, args
->authflavor
);
240 if (!(args
->flags
& RPC_CLNT_CREATE_NOPING
)) {
241 int err
= rpc_ping(clnt
, RPC_TASK_SOFT
|RPC_TASK_NOINTR
);
243 rpc_shutdown_client(clnt
);
248 clnt
->cl_softrtry
= 1;
249 if (args
->flags
& RPC_CLNT_CREATE_HARDRTRY
)
250 clnt
->cl_softrtry
= 0;
252 if (args
->flags
& RPC_CLNT_CREATE_INTR
)
254 if (args
->flags
& RPC_CLNT_CREATE_AUTOBIND
)
255 clnt
->cl_autobind
= 1;
256 if (args
->flags
& RPC_CLNT_CREATE_DISCRTRY
)
257 clnt
->cl_discrtry
= 1;
261 EXPORT_SYMBOL_GPL(rpc_create
);
264 * This function clones the RPC client structure. It allows us to share the
265 * same transport while varying parameters such as the authentication
269 rpc_clone_client(struct rpc_clnt
*clnt
)
271 struct rpc_clnt
*new;
274 new = kmemdup(clnt
, sizeof(*new), GFP_KERNEL
);
277 new->cl_parent
= clnt
;
278 /* Turn off autobind on clones */
279 new->cl_autobind
= 0;
280 INIT_LIST_HEAD(&new->cl_tasks
);
281 spin_lock_init(&new->cl_lock
);
282 rpc_init_rtt(&new->cl_rtt_default
, clnt
->cl_xprt
->timeout
.to_initval
);
283 new->cl_metrics
= rpc_alloc_iostats(clnt
);
284 if (new->cl_metrics
== NULL
)
286 kref_init(&new->cl_kref
);
287 err
= rpc_setup_pipedir(new, clnt
->cl_program
->pipe_dir_name
);
291 atomic_inc(&new->cl_auth
->au_count
);
292 xprt_get(clnt
->cl_xprt
);
293 kref_get(&clnt
->cl_kref
);
294 rpc_register_client(new);
298 rpc_free_iostats(new->cl_metrics
);
302 dprintk("RPC: %s: returned error %d\n", __FUNCTION__
, err
);
307 * Properly shut down an RPC client, terminating all outstanding
310 void rpc_shutdown_client(struct rpc_clnt
*clnt
)
312 dprintk("RPC: shutting down %s client for %s\n",
313 clnt
->cl_protname
, clnt
->cl_server
);
315 while (!list_empty(&clnt
->cl_tasks
)) {
316 rpc_killall_tasks(clnt
);
317 wait_event_timeout(destroy_wait
,
318 list_empty(&clnt
->cl_tasks
), 1*HZ
);
321 rpc_release_client(clnt
);
328 rpc_free_client(struct kref
*kref
)
330 struct rpc_clnt
*clnt
= container_of(kref
, struct rpc_clnt
, cl_kref
);
332 dprintk("RPC: destroying %s client for %s\n",
333 clnt
->cl_protname
, clnt
->cl_server
);
335 rpcauth_destroy(clnt
->cl_auth
);
336 clnt
->cl_auth
= NULL
;
338 if (!IS_ERR(clnt
->cl_dentry
)) {
339 rpc_rmdir(clnt
->cl_dentry
);
342 if (clnt
->cl_parent
!= clnt
) {
343 rpc_release_client(clnt
->cl_parent
);
346 if (clnt
->cl_server
!= clnt
->cl_inline_name
)
347 kfree(clnt
->cl_server
);
349 rpc_unregister_client(clnt
);
350 rpc_free_iostats(clnt
->cl_metrics
);
351 clnt
->cl_metrics
= NULL
;
352 xprt_put(clnt
->cl_xprt
);
358 * Release reference to the RPC client
361 rpc_release_client(struct rpc_clnt
*clnt
)
363 dprintk("RPC: rpc_release_client(%p)\n", clnt
);
365 if (list_empty(&clnt
->cl_tasks
))
366 wake_up(&destroy_wait
);
367 kref_put(&clnt
->cl_kref
, rpc_free_client
);
371 * rpc_bind_new_program - bind a new RPC program to an existing client
372 * @old - old rpc_client
373 * @program - rpc program to set
374 * @vers - rpc program version
376 * Clones the rpc client and sets up a new RPC program. This is mainly
377 * of use for enabling different RPC programs to share the same transport.
378 * The Sun NFSv2/v3 ACL protocol can do this.
380 struct rpc_clnt
*rpc_bind_new_program(struct rpc_clnt
*old
,
381 struct rpc_program
*program
,
384 struct rpc_clnt
*clnt
;
385 struct rpc_version
*version
;
388 BUG_ON(vers
>= program
->nrvers
|| !program
->version
[vers
]);
389 version
= program
->version
[vers
];
390 clnt
= rpc_clone_client(old
);
393 clnt
->cl_procinfo
= version
->procs
;
394 clnt
->cl_maxproc
= version
->nrprocs
;
395 clnt
->cl_protname
= program
->name
;
396 clnt
->cl_prog
= program
->number
;
397 clnt
->cl_vers
= version
->number
;
398 clnt
->cl_stats
= program
->stats
;
399 err
= rpc_ping(clnt
, RPC_TASK_SOFT
|RPC_TASK_NOINTR
);
401 rpc_shutdown_client(clnt
);
409 * Default callback for async RPC calls
412 rpc_default_callback(struct rpc_task
*task
, void *data
)
416 static const struct rpc_call_ops rpc_default_ops
= {
417 .rpc_call_done
= rpc_default_callback
,
421 * Export the signal mask handling for synchronous code that
422 * sleeps on RPC calls
424 #define RPC_INTR_SIGNALS (sigmask(SIGHUP) | sigmask(SIGINT) | sigmask(SIGQUIT) | sigmask(SIGTERM))
426 static void rpc_save_sigmask(sigset_t
*oldset
, int intr
)
428 unsigned long sigallow
= sigmask(SIGKILL
);
431 /* Block all signals except those listed in sigallow */
433 sigallow
|= RPC_INTR_SIGNALS
;
434 siginitsetinv(&sigmask
, sigallow
);
435 sigprocmask(SIG_BLOCK
, &sigmask
, oldset
);
438 static inline void rpc_task_sigmask(struct rpc_task
*task
, sigset_t
*oldset
)
440 rpc_save_sigmask(oldset
, !RPC_TASK_UNINTERRUPTIBLE(task
));
443 static inline void rpc_restore_sigmask(sigset_t
*oldset
)
445 sigprocmask(SIG_SETMASK
, oldset
, NULL
);
448 void rpc_clnt_sigmask(struct rpc_clnt
*clnt
, sigset_t
*oldset
)
450 rpc_save_sigmask(oldset
, clnt
->cl_intr
);
453 void rpc_clnt_sigunmask(struct rpc_clnt
*clnt
, sigset_t
*oldset
)
455 rpc_restore_sigmask(oldset
);
459 * New rpc_call implementation
461 int rpc_call_sync(struct rpc_clnt
*clnt
, struct rpc_message
*msg
, int flags
)
463 struct rpc_task
*task
;
467 BUG_ON(flags
& RPC_TASK_ASYNC
);
469 task
= rpc_new_task(clnt
, flags
, &rpc_default_ops
, NULL
);
473 /* Mask signals on RPC calls _and_ GSS_AUTH upcalls */
474 rpc_task_sigmask(task
, &oldset
);
476 /* Set up the call info struct and execute the task */
477 rpc_call_setup(task
, msg
, 0);
478 if (task
->tk_status
== 0) {
479 atomic_inc(&task
->tk_count
);
482 status
= task
->tk_status
;
484 rpc_restore_sigmask(&oldset
);
489 * New rpc_call implementation
492 rpc_call_async(struct rpc_clnt
*clnt
, struct rpc_message
*msg
, int flags
,
493 const struct rpc_call_ops
*tk_ops
, void *data
)
495 struct rpc_task
*task
;
499 flags
|= RPC_TASK_ASYNC
;
501 /* Create/initialize a new RPC task */
503 if (!(task
= rpc_new_task(clnt
, flags
, tk_ops
, data
)))
506 /* Mask signals on GSS_AUTH upcalls */
507 rpc_task_sigmask(task
, &oldset
);
509 rpc_call_setup(task
, msg
, 0);
511 /* Set up the call info struct and execute the task */
512 status
= task
->tk_status
;
518 rpc_restore_sigmask(&oldset
);
521 rpc_release_calldata(tk_ops
, data
);
527 rpc_call_setup(struct rpc_task
*task
, struct rpc_message
*msg
, int flags
)
530 task
->tk_flags
|= flags
;
531 /* Bind the user cred */
532 if (task
->tk_msg
.rpc_cred
!= NULL
)
533 rpcauth_holdcred(task
);
535 rpcauth_bindcred(task
);
537 if (task
->tk_status
== 0)
538 task
->tk_action
= call_start
;
540 task
->tk_action
= rpc_exit_task
;
544 * rpc_peeraddr - extract remote peer address from clnt's xprt
545 * @clnt: RPC client structure
546 * @buf: target buffer
547 * @size: length of target buffer
549 * Returns the number of bytes that are actually in the stored address.
551 size_t rpc_peeraddr(struct rpc_clnt
*clnt
, struct sockaddr
*buf
, size_t bufsize
)
554 struct rpc_xprt
*xprt
= clnt
->cl_xprt
;
556 bytes
= sizeof(xprt
->addr
);
559 memcpy(buf
, &clnt
->cl_xprt
->addr
, bytes
);
560 return xprt
->addrlen
;
562 EXPORT_SYMBOL_GPL(rpc_peeraddr
);
565 * rpc_peeraddr2str - return remote peer address in printable format
566 * @clnt: RPC client structure
567 * @format: address format
570 char *rpc_peeraddr2str(struct rpc_clnt
*clnt
, enum rpc_display_format_t format
)
572 struct rpc_xprt
*xprt
= clnt
->cl_xprt
;
574 if (xprt
->address_strings
[format
] != NULL
)
575 return xprt
->address_strings
[format
];
577 return "unprintable";
579 EXPORT_SYMBOL_GPL(rpc_peeraddr2str
);
582 rpc_setbufsize(struct rpc_clnt
*clnt
, unsigned int sndsize
, unsigned int rcvsize
)
584 struct rpc_xprt
*xprt
= clnt
->cl_xprt
;
585 if (xprt
->ops
->set_buffer_size
)
586 xprt
->ops
->set_buffer_size(xprt
, sndsize
, rcvsize
);
590 * Return size of largest payload RPC client can support, in bytes
592 * For stream transports, this is one RPC record fragment (see RFC
593 * 1831), as we don't support multi-record requests yet. For datagram
594 * transports, this is the size of an IP packet minus the IP, UDP, and
597 size_t rpc_max_payload(struct rpc_clnt
*clnt
)
599 return clnt
->cl_xprt
->max_payload
;
601 EXPORT_SYMBOL_GPL(rpc_max_payload
);
604 * rpc_force_rebind - force transport to check that remote port is unchanged
605 * @clnt: client to rebind
608 void rpc_force_rebind(struct rpc_clnt
*clnt
)
610 if (clnt
->cl_autobind
)
611 xprt_clear_bound(clnt
->cl_xprt
);
613 EXPORT_SYMBOL_GPL(rpc_force_rebind
);
616 * Restart an (async) RPC call. Usually called from within the
620 rpc_restart_call(struct rpc_task
*task
)
622 if (RPC_ASSASSINATED(task
))
625 task
->tk_action
= call_start
;
631 * Other FSM states can be visited zero or more times, but
632 * this state is visited exactly once for each RPC.
635 call_start(struct rpc_task
*task
)
637 struct rpc_clnt
*clnt
= task
->tk_client
;
639 dprintk("RPC: %5u call_start %s%d proc %d (%s)\n", task
->tk_pid
,
640 clnt
->cl_protname
, clnt
->cl_vers
,
641 task
->tk_msg
.rpc_proc
->p_proc
,
642 (RPC_IS_ASYNC(task
) ? "async" : "sync"));
644 /* Increment call count */
645 task
->tk_msg
.rpc_proc
->p_count
++;
646 clnt
->cl_stats
->rpccnt
++;
647 task
->tk_action
= call_reserve
;
651 * 1. Reserve an RPC call slot
654 call_reserve(struct rpc_task
*task
)
658 if (!rpcauth_uptodatecred(task
)) {
659 task
->tk_action
= call_refresh
;
664 task
->tk_action
= call_reserveresult
;
669 * 1b. Grok the result of xprt_reserve()
672 call_reserveresult(struct rpc_task
*task
)
674 int status
= task
->tk_status
;
679 * After a call to xprt_reserve(), we must have either
680 * a request slot or else an error status.
684 if (task
->tk_rqstp
) {
685 task
->tk_action
= call_allocate
;
689 printk(KERN_ERR
"%s: status=%d, but no request slot, exiting\n",
690 __FUNCTION__
, status
);
691 rpc_exit(task
, -EIO
);
696 * Even though there was an error, we may have acquired
697 * a request slot somehow. Make sure not to leak it.
699 if (task
->tk_rqstp
) {
700 printk(KERN_ERR
"%s: status=%d, request allocated anyway\n",
701 __FUNCTION__
, status
);
706 case -EAGAIN
: /* woken up; retry */
707 task
->tk_action
= call_reserve
;
709 case -EIO
: /* probably a shutdown */
712 printk(KERN_ERR
"%s: unrecognized error %d, exiting\n",
713 __FUNCTION__
, status
);
716 rpc_exit(task
, status
);
720 * 2. Allocate the buffer. For details, see sched.c:rpc_malloc.
721 * (Note: buffer memory is freed in xprt_release).
724 call_allocate(struct rpc_task
*task
)
726 unsigned int slack
= task
->tk_auth
->au_cslack
;
727 struct rpc_rqst
*req
= task
->tk_rqstp
;
728 struct rpc_xprt
*xprt
= task
->tk_xprt
;
729 struct rpc_procinfo
*proc
= task
->tk_msg
.rpc_proc
;
734 task
->tk_action
= call_bind
;
739 if (proc
->p_proc
!= 0) {
740 BUG_ON(proc
->p_arglen
== 0);
741 if (proc
->p_decode
!= NULL
)
742 BUG_ON(proc
->p_replen
== 0);
746 * Calculate the size (in quads) of the RPC call
747 * and reply headers, and convert both values
750 req
->rq_callsize
= RPC_CALLHDRSIZE
+ (slack
<< 1) + proc
->p_arglen
;
751 req
->rq_callsize
<<= 2;
752 req
->rq_rcvsize
= RPC_REPHDRSIZE
+ slack
+ proc
->p_replen
;
753 req
->rq_rcvsize
<<= 2;
755 req
->rq_buffer
= xprt
->ops
->buf_alloc(task
,
756 req
->rq_callsize
+ req
->rq_rcvsize
);
757 if (req
->rq_buffer
!= NULL
)
760 dprintk("RPC: %5u rpc_buffer allocation failed\n", task
->tk_pid
);
762 if (RPC_IS_ASYNC(task
) || !signalled()) {
764 task
->tk_action
= call_reserve
;
765 rpc_delay(task
, HZ
>>4);
769 rpc_exit(task
, -ERESTARTSYS
);
773 rpc_task_need_encode(struct rpc_task
*task
)
775 return task
->tk_rqstp
->rq_snd_buf
.len
== 0;
779 rpc_task_force_reencode(struct rpc_task
*task
)
781 task
->tk_rqstp
->rq_snd_buf
.len
= 0;
785 rpc_xdr_buf_init(struct xdr_buf
*buf
, void *start
, size_t len
)
787 buf
->head
[0].iov_base
= start
;
788 buf
->head
[0].iov_len
= len
;
789 buf
->tail
[0].iov_len
= 0;
796 * 3. Encode arguments of an RPC call
799 call_encode(struct rpc_task
*task
)
801 struct rpc_rqst
*req
= task
->tk_rqstp
;
807 rpc_xdr_buf_init(&req
->rq_snd_buf
,
810 rpc_xdr_buf_init(&req
->rq_rcv_buf
,
811 (char *)req
->rq_buffer
+ req
->rq_callsize
,
814 /* Encode header and provided arguments */
815 encode
= task
->tk_msg
.rpc_proc
->p_encode
;
816 if (!(p
= call_header(task
))) {
817 printk(KERN_INFO
"RPC: call_header failed, exit EIO\n");
818 rpc_exit(task
, -EIO
);
825 task
->tk_status
= rpcauth_wrap_req(task
, encode
, req
, p
,
826 task
->tk_msg
.rpc_argp
);
828 if (task
->tk_status
== -ENOMEM
) {
829 /* XXX: Is this sane? */
830 rpc_delay(task
, 3*HZ
);
831 task
->tk_status
= -EAGAIN
;
836 * 4. Get the server port number if not yet set
839 call_bind(struct rpc_task
*task
)
841 struct rpc_xprt
*xprt
= task
->tk_xprt
;
845 task
->tk_action
= call_connect
;
846 if (!xprt_bound(xprt
)) {
847 task
->tk_action
= call_bind_status
;
848 task
->tk_timeout
= xprt
->bind_timeout
;
849 xprt
->ops
->rpcbind(task
);
854 * 4a. Sort out bind result
857 call_bind_status(struct rpc_task
*task
)
859 int status
= -EACCES
;
861 if (task
->tk_status
>= 0) {
864 task
->tk_action
= call_connect
;
868 switch (task
->tk_status
) {
870 dprintk("RPC: %5u remote rpcbind: RPC program/version "
871 "unavailable\n", task
->tk_pid
);
872 rpc_delay(task
, 3*HZ
);
875 dprintk("RPC: %5u rpcbind request timed out\n",
879 dprintk("RPC: %5u remote rpcbind service unavailable\n",
882 case -EPROTONOSUPPORT
:
883 dprintk("RPC: %5u remote rpcbind version unavailable, retrying\n",
886 task
->tk_action
= call_bind
;
889 dprintk("RPC: %5u unrecognized rpcbind error (%d)\n",
890 task
->tk_pid
, -task
->tk_status
);
894 rpc_exit(task
, status
);
898 task
->tk_action
= call_timeout
;
902 * 4b. Connect to the RPC server
905 call_connect(struct rpc_task
*task
)
907 struct rpc_xprt
*xprt
= task
->tk_xprt
;
909 dprintk("RPC: %5u call_connect xprt %p %s connected\n",
911 (xprt_connected(xprt
) ? "is" : "is not"));
913 task
->tk_action
= call_transmit
;
914 if (!xprt_connected(xprt
)) {
915 task
->tk_action
= call_connect_status
;
916 if (task
->tk_status
< 0)
923 * 4c. Sort out connect result
926 call_connect_status(struct rpc_task
*task
)
928 struct rpc_clnt
*clnt
= task
->tk_client
;
929 int status
= task
->tk_status
;
935 clnt
->cl_stats
->netreconn
++;
936 task
->tk_action
= call_transmit
;
940 /* Something failed: remote service port may have changed */
941 rpc_force_rebind(clnt
);
946 task
->tk_action
= call_bind
;
947 if (!RPC_IS_SOFT(task
))
949 /* if soft mounted, test if we've timed out */
951 task
->tk_action
= call_timeout
;
954 rpc_exit(task
, -EIO
);
958 * 5. Transmit the RPC request, and wait for reply
961 call_transmit(struct rpc_task
*task
)
965 task
->tk_action
= call_status
;
966 if (task
->tk_status
< 0)
968 task
->tk_status
= xprt_prepare_transmit(task
);
969 if (task
->tk_status
!= 0)
971 task
->tk_action
= call_transmit_status
;
972 /* Encode here so that rpcsec_gss can use correct sequence number. */
973 if (rpc_task_need_encode(task
)) {
974 BUG_ON(task
->tk_rqstp
->rq_bytes_sent
!= 0);
976 /* Did the encode result in an error condition? */
977 if (task
->tk_status
!= 0)
981 if (task
->tk_status
< 0)
984 * On success, ensure that we call xprt_end_transmit() before sleeping
985 * in order to allow access to the socket to other RPC requests.
987 call_transmit_status(task
);
988 if (task
->tk_msg
.rpc_proc
->p_decode
!= NULL
)
990 task
->tk_action
= rpc_exit_task
;
991 rpc_wake_up_task(task
);
995 * 5a. Handle cleanup after a transmission
998 call_transmit_status(struct rpc_task
*task
)
1000 task
->tk_action
= call_status
;
1002 * Special case: if we've been waiting on the socket's write_space()
1003 * callback, then don't call xprt_end_transmit().
1005 if (task
->tk_status
== -EAGAIN
)
1007 xprt_end_transmit(task
);
1008 rpc_task_force_reencode(task
);
1012 * 6. Sort out the RPC call status
1015 call_status(struct rpc_task
*task
)
1017 struct rpc_clnt
*clnt
= task
->tk_client
;
1018 struct rpc_rqst
*req
= task
->tk_rqstp
;
1021 if (req
->rq_received
> 0 && !req
->rq_bytes_sent
)
1022 task
->tk_status
= req
->rq_received
;
1024 dprint_status(task
);
1026 status
= task
->tk_status
;
1028 task
->tk_action
= call_decode
;
1032 task
->tk_status
= 0;
1038 * Delay any retries for 3 seconds, then handle as if it
1041 rpc_delay(task
, 3*HZ
);
1043 task
->tk_action
= call_timeout
;
1044 if (task
->tk_client
->cl_discrtry
)
1045 xprt_disconnect(task
->tk_xprt
);
1049 rpc_force_rebind(clnt
);
1050 task
->tk_action
= call_bind
;
1053 task
->tk_action
= call_transmit
;
1056 /* shutdown or soft timeout */
1057 rpc_exit(task
, status
);
1060 printk("%s: RPC call returned error %d\n",
1061 clnt
->cl_protname
, -status
);
1062 rpc_exit(task
, status
);
1067 * 6a. Handle RPC timeout
1068 * We do not release the request slot, so we keep using the
1069 * same XID for all retransmits.
1072 call_timeout(struct rpc_task
*task
)
1074 struct rpc_clnt
*clnt
= task
->tk_client
;
1076 if (xprt_adjust_timeout(task
->tk_rqstp
) == 0) {
1077 dprintk("RPC: %5u call_timeout (minor)\n", task
->tk_pid
);
1081 dprintk("RPC: %5u call_timeout (major)\n", task
->tk_pid
);
1082 task
->tk_timeouts
++;
1084 if (RPC_IS_SOFT(task
)) {
1085 printk(KERN_NOTICE
"%s: server %s not responding, timed out\n",
1086 clnt
->cl_protname
, clnt
->cl_server
);
1087 rpc_exit(task
, -EIO
);
1091 if (!(task
->tk_flags
& RPC_CALL_MAJORSEEN
)) {
1092 task
->tk_flags
|= RPC_CALL_MAJORSEEN
;
1093 printk(KERN_NOTICE
"%s: server %s not responding, still trying\n",
1094 clnt
->cl_protname
, clnt
->cl_server
);
1096 rpc_force_rebind(clnt
);
1099 clnt
->cl_stats
->rpcretrans
++;
1100 task
->tk_action
= call_bind
;
1101 task
->tk_status
= 0;
1105 * 7. Decode the RPC reply
1108 call_decode(struct rpc_task
*task
)
1110 struct rpc_clnt
*clnt
= task
->tk_client
;
1111 struct rpc_rqst
*req
= task
->tk_rqstp
;
1112 kxdrproc_t decode
= task
->tk_msg
.rpc_proc
->p_decode
;
1115 dprintk("RPC: %5u call_decode (status %d)\n",
1116 task
->tk_pid
, task
->tk_status
);
1118 if (task
->tk_flags
& RPC_CALL_MAJORSEEN
) {
1119 printk(KERN_NOTICE
"%s: server %s OK\n",
1120 clnt
->cl_protname
, clnt
->cl_server
);
1121 task
->tk_flags
&= ~RPC_CALL_MAJORSEEN
;
1124 if (task
->tk_status
< 12) {
1125 if (!RPC_IS_SOFT(task
)) {
1126 task
->tk_action
= call_bind
;
1127 clnt
->cl_stats
->rpcretrans
++;
1130 dprintk("RPC: %s: too small RPC reply size (%d bytes)\n",
1131 clnt
->cl_protname
, task
->tk_status
);
1132 task
->tk_action
= call_timeout
;
1137 * Ensure that we see all writes made by xprt_complete_rqst()
1138 * before it changed req->rq_received.
1141 req
->rq_rcv_buf
.len
= req
->rq_private_buf
.len
;
1143 /* Check that the softirq receive buffer is valid */
1144 WARN_ON(memcmp(&req
->rq_rcv_buf
, &req
->rq_private_buf
,
1145 sizeof(req
->rq_rcv_buf
)) != 0);
1147 /* Verify the RPC header */
1148 p
= call_verify(task
);
1150 if (p
== ERR_PTR(-EAGAIN
))
1155 task
->tk_action
= rpc_exit_task
;
1159 task
->tk_status
= rpcauth_unwrap_resp(task
, decode
, req
, p
,
1160 task
->tk_msg
.rpc_resp
);
1163 dprintk("RPC: %5u call_decode result %d\n", task
->tk_pid
,
1167 req
->rq_received
= req
->rq_private_buf
.len
= 0;
1168 task
->tk_status
= 0;
1169 if (task
->tk_client
->cl_discrtry
)
1170 xprt_disconnect(task
->tk_xprt
);
1174 * 8. Refresh the credentials if rejected by the server
1177 call_refresh(struct rpc_task
*task
)
1179 dprint_status(task
);
1181 xprt_release(task
); /* Must do to obtain new XID */
1182 task
->tk_action
= call_refreshresult
;
1183 task
->tk_status
= 0;
1184 task
->tk_client
->cl_stats
->rpcauthrefresh
++;
1185 rpcauth_refreshcred(task
);
1189 * 8a. Process the results of a credential refresh
1192 call_refreshresult(struct rpc_task
*task
)
1194 int status
= task
->tk_status
;
1196 dprint_status(task
);
1198 task
->tk_status
= 0;
1199 task
->tk_action
= call_reserve
;
1200 if (status
>= 0 && rpcauth_uptodatecred(task
))
1202 if (status
== -EACCES
) {
1203 rpc_exit(task
, -EACCES
);
1206 task
->tk_action
= call_refresh
;
1207 if (status
!= -ETIMEDOUT
)
1208 rpc_delay(task
, 3*HZ
);
1213 * Call header serialization
1216 call_header(struct rpc_task
*task
)
1218 struct rpc_clnt
*clnt
= task
->tk_client
;
1219 struct rpc_rqst
*req
= task
->tk_rqstp
;
1220 __be32
*p
= req
->rq_svec
[0].iov_base
;
1222 /* FIXME: check buffer size? */
1224 p
= xprt_skip_transport_header(task
->tk_xprt
, p
);
1225 *p
++ = req
->rq_xid
; /* XID */
1226 *p
++ = htonl(RPC_CALL
); /* CALL */
1227 *p
++ = htonl(RPC_VERSION
); /* RPC version */
1228 *p
++ = htonl(clnt
->cl_prog
); /* program number */
1229 *p
++ = htonl(clnt
->cl_vers
); /* program version */
1230 *p
++ = htonl(task
->tk_msg
.rpc_proc
->p_proc
); /* procedure */
1231 p
= rpcauth_marshcred(task
, p
);
1232 req
->rq_slen
= xdr_adjust_iovec(&req
->rq_svec
[0], p
);
1237 * Reply header verification
1240 call_verify(struct rpc_task
*task
)
1242 struct kvec
*iov
= &task
->tk_rqstp
->rq_rcv_buf
.head
[0];
1243 int len
= task
->tk_rqstp
->rq_rcv_buf
.len
>> 2;
1244 __be32
*p
= iov
->iov_base
;
1246 int error
= -EACCES
;
1248 if ((task
->tk_rqstp
->rq_rcv_buf
.len
& 3) != 0) {
1249 /* RFC-1014 says that the representation of XDR data must be a
1250 * multiple of four bytes
1251 * - if it isn't pointer subtraction in the NFS client may give
1255 "call_verify: XDR representation not a multiple of"
1256 " 4 bytes: 0x%x\n", task
->tk_rqstp
->rq_rcv_buf
.len
);
1261 p
+= 1; /* skip XID */
1263 if ((n
= ntohl(*p
++)) != RPC_REPLY
) {
1264 printk(KERN_WARNING
"call_verify: not an RPC reply: %x\n", n
);
1267 if ((n
= ntohl(*p
++)) != RPC_MSG_ACCEPTED
) {
1270 switch ((n
= ntohl(*p
++))) {
1271 case RPC_AUTH_ERROR
:
1274 dprintk("RPC: %5u %s: RPC call version "
1276 task
->tk_pid
, __FUNCTION__
);
1277 error
= -EPROTONOSUPPORT
;
1280 dprintk("RPC: %5u %s: RPC call rejected, "
1281 "unknown error: %x\n",
1282 task
->tk_pid
, __FUNCTION__
, n
);
1287 switch ((n
= ntohl(*p
++))) {
1288 case RPC_AUTH_REJECTEDCRED
:
1289 case RPC_AUTH_REJECTEDVERF
:
1290 case RPCSEC_GSS_CREDPROBLEM
:
1291 case RPCSEC_GSS_CTXPROBLEM
:
1292 if (!task
->tk_cred_retry
)
1294 task
->tk_cred_retry
--;
1295 dprintk("RPC: %5u %s: retry stale creds\n",
1296 task
->tk_pid
, __FUNCTION__
);
1297 rpcauth_invalcred(task
);
1298 task
->tk_action
= call_refresh
;
1300 case RPC_AUTH_BADCRED
:
1301 case RPC_AUTH_BADVERF
:
1302 /* possibly garbled cred/verf? */
1303 if (!task
->tk_garb_retry
)
1305 task
->tk_garb_retry
--;
1306 dprintk("RPC: %5u %s: retry garbled creds\n",
1307 task
->tk_pid
, __FUNCTION__
);
1308 task
->tk_action
= call_bind
;
1310 case RPC_AUTH_TOOWEAK
:
1311 printk(KERN_NOTICE
"call_verify: server %s requires stronger "
1312 "authentication.\n", task
->tk_client
->cl_server
);
1315 printk(KERN_WARNING
"call_verify: unknown auth error: %x\n", n
);
1318 dprintk("RPC: %5u %s: call rejected %d\n",
1319 task
->tk_pid
, __FUNCTION__
, n
);
1322 if (!(p
= rpcauth_checkverf(task
, p
))) {
1323 printk(KERN_WARNING
"call_verify: auth check failed\n");
1324 goto out_garbage
; /* bad verifier, retry */
1326 len
= p
- (__be32
*)iov
->iov_base
- 1;
1329 switch ((n
= ntohl(*p
++))) {
1332 case RPC_PROG_UNAVAIL
:
1333 dprintk("RPC: %5u %s: program %u is unsupported by server %s\n",
1334 task
->tk_pid
, __FUNCTION__
,
1335 (unsigned int)task
->tk_client
->cl_prog
,
1336 task
->tk_client
->cl_server
);
1337 error
= -EPFNOSUPPORT
;
1339 case RPC_PROG_MISMATCH
:
1340 dprintk("RPC: %5u %s: program %u, version %u unsupported by "
1341 "server %s\n", task
->tk_pid
, __FUNCTION__
,
1342 (unsigned int)task
->tk_client
->cl_prog
,
1343 (unsigned int)task
->tk_client
->cl_vers
,
1344 task
->tk_client
->cl_server
);
1345 error
= -EPROTONOSUPPORT
;
1347 case RPC_PROC_UNAVAIL
:
1348 dprintk("RPC: %5u %s: proc %p unsupported by program %u, "
1349 "version %u on server %s\n",
1350 task
->tk_pid
, __FUNCTION__
,
1351 task
->tk_msg
.rpc_proc
,
1352 task
->tk_client
->cl_prog
,
1353 task
->tk_client
->cl_vers
,
1354 task
->tk_client
->cl_server
);
1355 error
= -EOPNOTSUPP
;
1357 case RPC_GARBAGE_ARGS
:
1358 dprintk("RPC: %5u %s: server saw garbage\n",
1359 task
->tk_pid
, __FUNCTION__
);
1362 printk(KERN_WARNING
"call_verify: server accept status: %x\n", n
);
1367 task
->tk_client
->cl_stats
->rpcgarbage
++;
1368 if (task
->tk_garb_retry
) {
1369 task
->tk_garb_retry
--;
1370 dprintk("RPC: %5u %s: retrying\n",
1371 task
->tk_pid
, __FUNCTION__
);
1372 task
->tk_action
= call_bind
;
1374 return ERR_PTR(-EAGAIN
);
1376 printk(KERN_WARNING
"RPC %s: retry failed, exit EIO\n", __FUNCTION__
);
1380 rpc_exit(task
, error
);
1381 return ERR_PTR(error
);
1383 printk(KERN_WARNING
"RPC %s: server reply was truncated.\n", __FUNCTION__
);
1387 static int rpcproc_encode_null(void *rqstp
, __be32
*data
, void *obj
)
1392 static int rpcproc_decode_null(void *rqstp
, __be32
*data
, void *obj
)
1397 static struct rpc_procinfo rpcproc_null
= {
1398 .p_encode
= rpcproc_encode_null
,
1399 .p_decode
= rpcproc_decode_null
,
1402 int rpc_ping(struct rpc_clnt
*clnt
, int flags
)
1404 struct rpc_message msg
= {
1405 .rpc_proc
= &rpcproc_null
,
1408 msg
.rpc_cred
= authnull_ops
.lookup_cred(NULL
, NULL
, 0);
1409 err
= rpc_call_sync(clnt
, &msg
, flags
);
1410 put_rpccred(msg
.rpc_cred
);