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>
39 #define RPC_SLACK_SPACE (1024) /* total overkill */
42 # define RPCDBG_FACILITY RPCDBG_CALL
45 #define dprint_status(t) \
46 dprintk("RPC: %5u %s (status %d)\n", t->tk_pid, \
47 __FUNCTION__, t->tk_status)
49 static DECLARE_WAIT_QUEUE_HEAD(destroy_wait
);
52 static void call_start(struct rpc_task
*task
);
53 static void call_reserve(struct rpc_task
*task
);
54 static void call_reserveresult(struct rpc_task
*task
);
55 static void call_allocate(struct rpc_task
*task
);
56 static void call_encode(struct rpc_task
*task
);
57 static void call_decode(struct rpc_task
*task
);
58 static void call_bind(struct rpc_task
*task
);
59 static void call_bind_status(struct rpc_task
*task
);
60 static void call_transmit(struct rpc_task
*task
);
61 static void call_status(struct rpc_task
*task
);
62 static void call_transmit_status(struct rpc_task
*task
);
63 static void call_refresh(struct rpc_task
*task
);
64 static void call_refreshresult(struct rpc_task
*task
);
65 static void call_timeout(struct rpc_task
*task
);
66 static void call_connect(struct rpc_task
*task
);
67 static void call_connect_status(struct rpc_task
*task
);
68 static __be32
* call_header(struct rpc_task
*task
);
69 static __be32
* call_verify(struct rpc_task
*task
);
73 rpc_setup_pipedir(struct rpc_clnt
*clnt
, char *dir_name
)
75 static uint32_t clntid
;
78 clnt
->cl_vfsmnt
= ERR_PTR(-ENOENT
);
79 clnt
->cl_dentry
= ERR_PTR(-ENOENT
);
83 clnt
->cl_vfsmnt
= rpc_get_mount();
84 if (IS_ERR(clnt
->cl_vfsmnt
))
85 return PTR_ERR(clnt
->cl_vfsmnt
);
88 snprintf(clnt
->cl_pathname
, sizeof(clnt
->cl_pathname
),
89 "%s/clnt%x", dir_name
,
90 (unsigned int)clntid
++);
91 clnt
->cl_pathname
[sizeof(clnt
->cl_pathname
) - 1] = '\0';
92 clnt
->cl_dentry
= rpc_mkdir(clnt
->cl_pathname
, clnt
);
93 if (!IS_ERR(clnt
->cl_dentry
))
95 error
= PTR_ERR(clnt
->cl_dentry
);
96 if (error
!= -EEXIST
) {
97 printk(KERN_INFO
"RPC: Couldn't create pipefs entry %s, error %d\n",
98 clnt
->cl_pathname
, error
);
105 static struct rpc_clnt
* rpc_new_client(struct rpc_xprt
*xprt
, char *servname
, struct rpc_program
*program
, u32 vers
, rpc_authflavor_t flavor
)
107 struct rpc_version
*version
;
108 struct rpc_clnt
*clnt
= NULL
;
109 struct rpc_auth
*auth
;
113 dprintk("RPC: creating %s client for %s (xprt %p)\n",
114 program
->name
, servname
, xprt
);
119 if (vers
>= program
->nrvers
|| !(version
= program
->version
[vers
]))
123 clnt
= kzalloc(sizeof(*clnt
), GFP_KERNEL
);
126 atomic_set(&clnt
->cl_users
, 0);
127 atomic_set(&clnt
->cl_count
, 1);
128 clnt
->cl_parent
= clnt
;
130 clnt
->cl_server
= clnt
->cl_inline_name
;
131 len
= strlen(servname
) + 1;
132 if (len
> sizeof(clnt
->cl_inline_name
)) {
133 char *buf
= kmalloc(len
, GFP_KERNEL
);
135 clnt
->cl_server
= buf
;
137 len
= sizeof(clnt
->cl_inline_name
);
139 strlcpy(clnt
->cl_server
, servname
, len
);
141 clnt
->cl_xprt
= xprt
;
142 clnt
->cl_procinfo
= version
->procs
;
143 clnt
->cl_maxproc
= version
->nrprocs
;
144 clnt
->cl_protname
= program
->name
;
145 clnt
->cl_prog
= program
->number
;
146 clnt
->cl_vers
= version
->number
;
147 clnt
->cl_stats
= program
->stats
;
148 clnt
->cl_metrics
= rpc_alloc_iostats(clnt
);
150 if (clnt
->cl_metrics
== NULL
)
152 clnt
->cl_program
= program
;
154 if (!xprt_bound(clnt
->cl_xprt
))
155 clnt
->cl_autobind
= 1;
157 clnt
->cl_rtt
= &clnt
->cl_rtt_default
;
158 rpc_init_rtt(&clnt
->cl_rtt_default
, xprt
->timeout
.to_initval
);
160 err
= rpc_setup_pipedir(clnt
, program
->pipe_dir_name
);
164 auth
= rpcauth_create(flavor
, clnt
);
166 printk(KERN_INFO
"RPC: Couldn't create auth handle (flavor %u)\n",
172 /* save the nodename */
173 clnt
->cl_nodelen
= strlen(utsname()->nodename
);
174 if (clnt
->cl_nodelen
> UNX_MAXNODENAME
)
175 clnt
->cl_nodelen
= UNX_MAXNODENAME
;
176 memcpy(clnt
->cl_nodename
, utsname()->nodename
, clnt
->cl_nodelen
);
180 if (!IS_ERR(clnt
->cl_dentry
)) {
181 rpc_rmdir(clnt
->cl_dentry
);
185 rpc_free_iostats(clnt
->cl_metrics
);
187 if (clnt
->cl_server
!= clnt
->cl_inline_name
)
188 kfree(clnt
->cl_server
);
197 * rpc_create - create an RPC client and transport with one call
198 * @args: rpc_clnt create argument structure
200 * Creates and initializes an RPC transport and an RPC client.
202 * It can ping the server in order to determine if it is up, and to see if
203 * it supports this program and version. RPC_CLNT_CREATE_NOPING disables
204 * this behavior so asynchronous tasks can also use rpc_create.
206 struct rpc_clnt
*rpc_create(struct rpc_create_args
*args
)
208 struct rpc_xprt
*xprt
;
209 struct rpc_clnt
*clnt
;
211 xprt
= xprt_create_transport(args
->protocol
, args
->address
,
212 args
->addrsize
, args
->timeout
);
214 return (struct rpc_clnt
*)xprt
;
217 * By default, kernel RPC client connects from a reserved port.
218 * CAP_NET_BIND_SERVICE will not be set for unprivileged requesters,
219 * but it is always enabled for rpciod, which handles the connect
223 if (args
->flags
& RPC_CLNT_CREATE_NONPRIVPORT
)
226 dprintk("RPC: creating %s client for %s (xprt %p)\n",
227 args
->program
->name
, args
->servername
, xprt
);
229 clnt
= rpc_new_client(xprt
, args
->servername
, args
->program
,
230 args
->version
, args
->authflavor
);
234 if (!(args
->flags
& RPC_CLNT_CREATE_NOPING
)) {
235 int err
= rpc_ping(clnt
, RPC_TASK_SOFT
|RPC_TASK_NOINTR
);
237 rpc_shutdown_client(clnt
);
242 clnt
->cl_softrtry
= 1;
243 if (args
->flags
& RPC_CLNT_CREATE_HARDRTRY
)
244 clnt
->cl_softrtry
= 0;
246 if (args
->flags
& RPC_CLNT_CREATE_INTR
)
248 if (args
->flags
& RPC_CLNT_CREATE_AUTOBIND
)
249 clnt
->cl_autobind
= 1;
250 if (args
->flags
& RPC_CLNT_CREATE_ONESHOT
)
251 clnt
->cl_oneshot
= 1;
252 if (args
->flags
& RPC_CLNT_CREATE_DISCRTRY
)
253 clnt
->cl_discrtry
= 1;
257 EXPORT_SYMBOL_GPL(rpc_create
);
260 * This function clones the RPC client structure. It allows us to share the
261 * same transport while varying parameters such as the authentication
265 rpc_clone_client(struct rpc_clnt
*clnt
)
267 struct rpc_clnt
*new;
270 new = kmemdup(clnt
, sizeof(*new), GFP_KERNEL
);
273 atomic_set(&new->cl_count
, 1);
274 atomic_set(&new->cl_users
, 0);
275 new->cl_metrics
= rpc_alloc_iostats(clnt
);
276 if (new->cl_metrics
== NULL
)
278 err
= rpc_setup_pipedir(new, clnt
->cl_program
->pipe_dir_name
);
281 new->cl_parent
= clnt
;
282 atomic_inc(&clnt
->cl_count
);
283 new->cl_xprt
= xprt_get(clnt
->cl_xprt
);
284 /* Turn off autobind on clones */
285 new->cl_autobind
= 0;
288 rpc_init_rtt(&new->cl_rtt_default
, clnt
->cl_xprt
->timeout
.to_initval
);
290 atomic_inc(&new->cl_auth
->au_count
);
293 rpc_free_iostats(new->cl_metrics
);
297 dprintk("RPC: %s: returned error %d\n", __FUNCTION__
, err
);
302 * Properly shut down an RPC client, terminating all outstanding
303 * requests. Note that we must be certain that cl_oneshot and
304 * cl_dead are cleared, or else the client would be destroyed
305 * when the last task releases it.
308 rpc_shutdown_client(struct rpc_clnt
*clnt
)
310 dprintk("RPC: shutting down %s client for %s, tasks=%d\n",
311 clnt
->cl_protname
, clnt
->cl_server
,
312 atomic_read(&clnt
->cl_users
));
314 while (atomic_read(&clnt
->cl_users
) > 0) {
315 /* Don't let rpc_release_client destroy us */
316 clnt
->cl_oneshot
= 0;
318 rpc_killall_tasks(clnt
);
319 wait_event_timeout(destroy_wait
,
320 !atomic_read(&clnt
->cl_users
), 1*HZ
);
323 if (atomic_read(&clnt
->cl_users
) < 0) {
324 printk(KERN_ERR
"RPC: rpc_shutdown_client clnt %p tasks=%d\n",
325 clnt
, atomic_read(&clnt
->cl_users
));
332 return rpc_destroy_client(clnt
);
336 * Delete an RPC client
339 rpc_destroy_client(struct rpc_clnt
*clnt
)
341 if (!atomic_dec_and_test(&clnt
->cl_count
))
343 BUG_ON(atomic_read(&clnt
->cl_users
) != 0);
345 dprintk("RPC: destroying %s client for %s\n",
346 clnt
->cl_protname
, clnt
->cl_server
);
348 rpcauth_destroy(clnt
->cl_auth
);
349 clnt
->cl_auth
= NULL
;
351 if (!IS_ERR(clnt
->cl_dentry
)) {
352 rpc_rmdir(clnt
->cl_dentry
);
355 if (clnt
->cl_parent
!= clnt
) {
356 rpc_destroy_client(clnt
->cl_parent
);
359 if (clnt
->cl_server
!= clnt
->cl_inline_name
)
360 kfree(clnt
->cl_server
);
362 rpc_free_iostats(clnt
->cl_metrics
);
363 clnt
->cl_metrics
= NULL
;
364 xprt_put(clnt
->cl_xprt
);
370 * Release an RPC client
373 rpc_release_client(struct rpc_clnt
*clnt
)
375 dprintk("RPC: rpc_release_client(%p, %d)\n",
376 clnt
, atomic_read(&clnt
->cl_users
));
378 if (!atomic_dec_and_test(&clnt
->cl_users
))
380 wake_up(&destroy_wait
);
381 if (clnt
->cl_oneshot
|| clnt
->cl_dead
)
382 rpc_destroy_client(clnt
);
386 * rpc_bind_new_program - bind a new RPC program to an existing client
387 * @old - old rpc_client
388 * @program - rpc program to set
389 * @vers - rpc program version
391 * Clones the rpc client and sets up a new RPC program. This is mainly
392 * of use for enabling different RPC programs to share the same transport.
393 * The Sun NFSv2/v3 ACL protocol can do this.
395 struct rpc_clnt
*rpc_bind_new_program(struct rpc_clnt
*old
,
396 struct rpc_program
*program
,
399 struct rpc_clnt
*clnt
;
400 struct rpc_version
*version
;
403 BUG_ON(vers
>= program
->nrvers
|| !program
->version
[vers
]);
404 version
= program
->version
[vers
];
405 clnt
= rpc_clone_client(old
);
408 clnt
->cl_procinfo
= version
->procs
;
409 clnt
->cl_maxproc
= version
->nrprocs
;
410 clnt
->cl_protname
= program
->name
;
411 clnt
->cl_prog
= program
->number
;
412 clnt
->cl_vers
= version
->number
;
413 clnt
->cl_stats
= program
->stats
;
414 err
= rpc_ping(clnt
, RPC_TASK_SOFT
|RPC_TASK_NOINTR
);
416 rpc_shutdown_client(clnt
);
424 * Default callback for async RPC calls
427 rpc_default_callback(struct rpc_task
*task
, void *data
)
431 static const struct rpc_call_ops rpc_default_ops
= {
432 .rpc_call_done
= rpc_default_callback
,
436 * Export the signal mask handling for synchronous code that
437 * sleeps on RPC calls
439 #define RPC_INTR_SIGNALS (sigmask(SIGHUP) | sigmask(SIGINT) | sigmask(SIGQUIT) | sigmask(SIGTERM))
441 static void rpc_save_sigmask(sigset_t
*oldset
, int intr
)
443 unsigned long sigallow
= sigmask(SIGKILL
);
446 /* Block all signals except those listed in sigallow */
448 sigallow
|= RPC_INTR_SIGNALS
;
449 siginitsetinv(&sigmask
, sigallow
);
450 sigprocmask(SIG_BLOCK
, &sigmask
, oldset
);
453 static inline void rpc_task_sigmask(struct rpc_task
*task
, sigset_t
*oldset
)
455 rpc_save_sigmask(oldset
, !RPC_TASK_UNINTERRUPTIBLE(task
));
458 static inline void rpc_restore_sigmask(sigset_t
*oldset
)
460 sigprocmask(SIG_SETMASK
, oldset
, NULL
);
463 void rpc_clnt_sigmask(struct rpc_clnt
*clnt
, sigset_t
*oldset
)
465 rpc_save_sigmask(oldset
, clnt
->cl_intr
);
468 void rpc_clnt_sigunmask(struct rpc_clnt
*clnt
, sigset_t
*oldset
)
470 rpc_restore_sigmask(oldset
);
474 * New rpc_call implementation
476 int rpc_call_sync(struct rpc_clnt
*clnt
, struct rpc_message
*msg
, int flags
)
478 struct rpc_task
*task
;
482 /* If this client is slain all further I/O fails */
486 BUG_ON(flags
& RPC_TASK_ASYNC
);
488 task
= rpc_new_task(clnt
, flags
, &rpc_default_ops
, NULL
);
492 /* Mask signals on RPC calls _and_ GSS_AUTH upcalls */
493 rpc_task_sigmask(task
, &oldset
);
495 /* Set up the call info struct and execute the task */
496 rpc_call_setup(task
, msg
, 0);
497 if (task
->tk_status
== 0) {
498 atomic_inc(&task
->tk_count
);
501 status
= task
->tk_status
;
503 rpc_restore_sigmask(&oldset
);
508 * New rpc_call implementation
511 rpc_call_async(struct rpc_clnt
*clnt
, struct rpc_message
*msg
, int flags
,
512 const struct rpc_call_ops
*tk_ops
, void *data
)
514 struct rpc_task
*task
;
518 /* If this client is slain all further I/O fails */
523 flags
|= RPC_TASK_ASYNC
;
525 /* Create/initialize a new RPC task */
527 if (!(task
= rpc_new_task(clnt
, flags
, tk_ops
, data
)))
530 /* Mask signals on GSS_AUTH upcalls */
531 rpc_task_sigmask(task
, &oldset
);
533 rpc_call_setup(task
, msg
, 0);
535 /* Set up the call info struct and execute the task */
536 status
= task
->tk_status
;
542 rpc_restore_sigmask(&oldset
);
545 rpc_release_calldata(tk_ops
, data
);
551 rpc_call_setup(struct rpc_task
*task
, struct rpc_message
*msg
, int flags
)
554 task
->tk_flags
|= flags
;
555 /* Bind the user cred */
556 if (task
->tk_msg
.rpc_cred
!= NULL
)
557 rpcauth_holdcred(task
);
559 rpcauth_bindcred(task
);
561 if (task
->tk_status
== 0)
562 task
->tk_action
= call_start
;
564 task
->tk_action
= rpc_exit_task
;
568 * rpc_peeraddr - extract remote peer address from clnt's xprt
569 * @clnt: RPC client structure
570 * @buf: target buffer
571 * @size: length of target buffer
573 * Returns the number of bytes that are actually in the stored address.
575 size_t rpc_peeraddr(struct rpc_clnt
*clnt
, struct sockaddr
*buf
, size_t bufsize
)
578 struct rpc_xprt
*xprt
= clnt
->cl_xprt
;
580 bytes
= sizeof(xprt
->addr
);
583 memcpy(buf
, &clnt
->cl_xprt
->addr
, bytes
);
584 return xprt
->addrlen
;
586 EXPORT_SYMBOL_GPL(rpc_peeraddr
);
589 * rpc_peeraddr2str - return remote peer address in printable format
590 * @clnt: RPC client structure
591 * @format: address format
594 char *rpc_peeraddr2str(struct rpc_clnt
*clnt
, enum rpc_display_format_t format
)
596 struct rpc_xprt
*xprt
= clnt
->cl_xprt
;
598 if (xprt
->address_strings
[format
] != NULL
)
599 return xprt
->address_strings
[format
];
601 return "unprintable";
603 EXPORT_SYMBOL_GPL(rpc_peeraddr2str
);
606 rpc_setbufsize(struct rpc_clnt
*clnt
, unsigned int sndsize
, unsigned int rcvsize
)
608 struct rpc_xprt
*xprt
= clnt
->cl_xprt
;
609 if (xprt
->ops
->set_buffer_size
)
610 xprt
->ops
->set_buffer_size(xprt
, sndsize
, rcvsize
);
614 * Return size of largest payload RPC client can support, in bytes
616 * For stream transports, this is one RPC record fragment (see RFC
617 * 1831), as we don't support multi-record requests yet. For datagram
618 * transports, this is the size of an IP packet minus the IP, UDP, and
621 size_t rpc_max_payload(struct rpc_clnt
*clnt
)
623 return clnt
->cl_xprt
->max_payload
;
625 EXPORT_SYMBOL_GPL(rpc_max_payload
);
628 * rpc_force_rebind - force transport to check that remote port is unchanged
629 * @clnt: client to rebind
632 void rpc_force_rebind(struct rpc_clnt
*clnt
)
634 if (clnt
->cl_autobind
)
635 xprt_clear_bound(clnt
->cl_xprt
);
637 EXPORT_SYMBOL_GPL(rpc_force_rebind
);
640 * Restart an (async) RPC call. Usually called from within the
644 rpc_restart_call(struct rpc_task
*task
)
646 if (RPC_ASSASSINATED(task
))
649 task
->tk_action
= call_start
;
655 * Other FSM states can be visited zero or more times, but
656 * this state is visited exactly once for each RPC.
659 call_start(struct rpc_task
*task
)
661 struct rpc_clnt
*clnt
= task
->tk_client
;
663 dprintk("RPC: %5u call_start %s%d proc %d (%s)\n", task
->tk_pid
,
664 clnt
->cl_protname
, clnt
->cl_vers
,
665 task
->tk_msg
.rpc_proc
->p_proc
,
666 (RPC_IS_ASYNC(task
) ? "async" : "sync"));
668 /* Increment call count */
669 task
->tk_msg
.rpc_proc
->p_count
++;
670 clnt
->cl_stats
->rpccnt
++;
671 task
->tk_action
= call_reserve
;
675 * 1. Reserve an RPC call slot
678 call_reserve(struct rpc_task
*task
)
682 if (!rpcauth_uptodatecred(task
)) {
683 task
->tk_action
= call_refresh
;
688 task
->tk_action
= call_reserveresult
;
693 * 1b. Grok the result of xprt_reserve()
696 call_reserveresult(struct rpc_task
*task
)
698 int status
= task
->tk_status
;
703 * After a call to xprt_reserve(), we must have either
704 * a request slot or else an error status.
708 if (task
->tk_rqstp
) {
709 task
->tk_action
= call_allocate
;
713 printk(KERN_ERR
"%s: status=%d, but no request slot, exiting\n",
714 __FUNCTION__
, status
);
715 rpc_exit(task
, -EIO
);
720 * Even though there was an error, we may have acquired
721 * a request slot somehow. Make sure not to leak it.
723 if (task
->tk_rqstp
) {
724 printk(KERN_ERR
"%s: status=%d, request allocated anyway\n",
725 __FUNCTION__
, status
);
730 case -EAGAIN
: /* woken up; retry */
731 task
->tk_action
= call_reserve
;
733 case -EIO
: /* probably a shutdown */
736 printk(KERN_ERR
"%s: unrecognized error %d, exiting\n",
737 __FUNCTION__
, status
);
740 rpc_exit(task
, status
);
744 * 2. Allocate the buffer. For details, see sched.c:rpc_malloc.
745 * (Note: buffer memory is freed in xprt_release).
748 call_allocate(struct rpc_task
*task
)
750 struct rpc_rqst
*req
= task
->tk_rqstp
;
751 struct rpc_xprt
*xprt
= task
->tk_xprt
;
756 task
->tk_action
= call_bind
;
760 /* FIXME: compute buffer requirements more exactly using
762 bufsiz
= task
->tk_msg
.rpc_proc
->p_bufsiz
+ RPC_SLACK_SPACE
;
764 if (xprt
->ops
->buf_alloc(task
, bufsiz
<< 1) != NULL
)
767 dprintk("RPC: %5u rpc_buffer allocation failed\n", task
->tk_pid
);
769 if (RPC_IS_ASYNC(task
) || !signalled()) {
771 task
->tk_action
= call_reserve
;
772 rpc_delay(task
, HZ
>>4);
776 rpc_exit(task
, -ERESTARTSYS
);
780 rpc_task_need_encode(struct rpc_task
*task
)
782 return task
->tk_rqstp
->rq_snd_buf
.len
== 0;
786 rpc_task_force_reencode(struct rpc_task
*task
)
788 task
->tk_rqstp
->rq_snd_buf
.len
= 0;
792 * 3. Encode arguments of an RPC call
795 call_encode(struct rpc_task
*task
)
797 struct rpc_rqst
*req
= task
->tk_rqstp
;
798 struct xdr_buf
*sndbuf
= &req
->rq_snd_buf
;
799 struct xdr_buf
*rcvbuf
= &req
->rq_rcv_buf
;
806 /* Default buffer setup */
807 bufsiz
= req
->rq_bufsize
>> 1;
808 sndbuf
->head
[0].iov_base
= (void *)req
->rq_buffer
;
809 sndbuf
->head
[0].iov_len
= bufsiz
;
810 sndbuf
->tail
[0].iov_len
= 0;
811 sndbuf
->page_len
= 0;
813 sndbuf
->buflen
= bufsiz
;
814 rcvbuf
->head
[0].iov_base
= (void *)((char *)req
->rq_buffer
+ bufsiz
);
815 rcvbuf
->head
[0].iov_len
= bufsiz
;
816 rcvbuf
->tail
[0].iov_len
= 0;
817 rcvbuf
->page_len
= 0;
819 rcvbuf
->buflen
= bufsiz
;
821 /* Encode header and provided arguments */
822 encode
= task
->tk_msg
.rpc_proc
->p_encode
;
823 if (!(p
= call_header(task
))) {
824 printk(KERN_INFO
"RPC: call_header failed, exit EIO\n");
825 rpc_exit(task
, -EIO
);
832 task
->tk_status
= rpcauth_wrap_req(task
, encode
, req
, p
,
833 task
->tk_msg
.rpc_argp
);
835 if (task
->tk_status
== -ENOMEM
) {
836 /* XXX: Is this sane? */
837 rpc_delay(task
, 3*HZ
);
838 task
->tk_status
= -EAGAIN
;
843 * 4. Get the server port number if not yet set
846 call_bind(struct rpc_task
*task
)
848 struct rpc_xprt
*xprt
= task
->tk_xprt
;
852 task
->tk_action
= call_connect
;
853 if (!xprt_bound(xprt
)) {
854 task
->tk_action
= call_bind_status
;
855 task
->tk_timeout
= xprt
->bind_timeout
;
856 xprt
->ops
->rpcbind(task
);
861 * 4a. Sort out bind result
864 call_bind_status(struct rpc_task
*task
)
866 int status
= -EACCES
;
868 if (task
->tk_status
>= 0) {
871 task
->tk_action
= call_connect
;
875 switch (task
->tk_status
) {
877 dprintk("RPC: %5u remote rpcbind: RPC program/version "
878 "unavailable\n", task
->tk_pid
);
879 rpc_delay(task
, 3*HZ
);
882 dprintk("RPC: %5u rpcbind request timed out\n",
886 dprintk("RPC: %5u remote rpcbind service unavailable\n",
889 case -EPROTONOSUPPORT
:
890 dprintk("RPC: %5u remote rpcbind version 2 unavailable\n",
894 dprintk("RPC: %5u unrecognized rpcbind error (%d)\n",
895 task
->tk_pid
, -task
->tk_status
);
899 rpc_exit(task
, status
);
903 task
->tk_action
= call_timeout
;
907 * 4b. Connect to the RPC server
910 call_connect(struct rpc_task
*task
)
912 struct rpc_xprt
*xprt
= task
->tk_xprt
;
914 dprintk("RPC: %5u call_connect xprt %p %s connected\n",
916 (xprt_connected(xprt
) ? "is" : "is not"));
918 task
->tk_action
= call_transmit
;
919 if (!xprt_connected(xprt
)) {
920 task
->tk_action
= call_connect_status
;
921 if (task
->tk_status
< 0)
928 * 4c. Sort out connect result
931 call_connect_status(struct rpc_task
*task
)
933 struct rpc_clnt
*clnt
= task
->tk_client
;
934 int status
= task
->tk_status
;
940 clnt
->cl_stats
->netreconn
++;
941 task
->tk_action
= call_transmit
;
945 /* Something failed: remote service port may have changed */
946 rpc_force_rebind(clnt
);
951 task
->tk_action
= call_bind
;
952 if (!RPC_IS_SOFT(task
))
954 /* if soft mounted, test if we've timed out */
956 task
->tk_action
= call_timeout
;
959 rpc_exit(task
, -EIO
);
963 * 5. Transmit the RPC request, and wait for reply
966 call_transmit(struct rpc_task
*task
)
970 task
->tk_action
= call_status
;
971 if (task
->tk_status
< 0)
973 task
->tk_status
= xprt_prepare_transmit(task
);
974 if (task
->tk_status
!= 0)
976 task
->tk_action
= call_transmit_status
;
977 /* Encode here so that rpcsec_gss can use correct sequence number. */
978 if (rpc_task_need_encode(task
)) {
979 BUG_ON(task
->tk_rqstp
->rq_bytes_sent
!= 0);
981 /* Did the encode result in an error condition? */
982 if (task
->tk_status
!= 0)
986 if (task
->tk_status
< 0)
989 * On success, ensure that we call xprt_end_transmit() before sleeping
990 * in order to allow access to the socket to other RPC requests.
992 call_transmit_status(task
);
993 if (task
->tk_msg
.rpc_proc
->p_decode
!= NULL
)
995 task
->tk_action
= rpc_exit_task
;
996 rpc_wake_up_task(task
);
1000 * 5a. Handle cleanup after a transmission
1003 call_transmit_status(struct rpc_task
*task
)
1005 task
->tk_action
= call_status
;
1007 * Special case: if we've been waiting on the socket's write_space()
1008 * callback, then don't call xprt_end_transmit().
1010 if (task
->tk_status
== -EAGAIN
)
1012 xprt_end_transmit(task
);
1013 rpc_task_force_reencode(task
);
1017 * 6. Sort out the RPC call status
1020 call_status(struct rpc_task
*task
)
1022 struct rpc_clnt
*clnt
= task
->tk_client
;
1023 struct rpc_rqst
*req
= task
->tk_rqstp
;
1026 if (req
->rq_received
> 0 && !req
->rq_bytes_sent
)
1027 task
->tk_status
= req
->rq_received
;
1029 dprint_status(task
);
1031 status
= task
->tk_status
;
1033 task
->tk_action
= call_decode
;
1037 task
->tk_status
= 0;
1043 * Delay any retries for 3 seconds, then handle as if it
1046 rpc_delay(task
, 3*HZ
);
1048 task
->tk_action
= call_timeout
;
1049 if (task
->tk_client
->cl_discrtry
)
1050 xprt_disconnect(task
->tk_xprt
);
1054 rpc_force_rebind(clnt
);
1055 task
->tk_action
= call_bind
;
1058 task
->tk_action
= call_transmit
;
1061 /* shutdown or soft timeout */
1062 rpc_exit(task
, status
);
1065 printk("%s: RPC call returned error %d\n",
1066 clnt
->cl_protname
, -status
);
1067 rpc_exit(task
, status
);
1072 * 6a. Handle RPC timeout
1073 * We do not release the request slot, so we keep using the
1074 * same XID for all retransmits.
1077 call_timeout(struct rpc_task
*task
)
1079 struct rpc_clnt
*clnt
= task
->tk_client
;
1081 if (xprt_adjust_timeout(task
->tk_rqstp
) == 0) {
1082 dprintk("RPC: %5u call_timeout (minor)\n", task
->tk_pid
);
1086 dprintk("RPC: %5u call_timeout (major)\n", task
->tk_pid
);
1087 task
->tk_timeouts
++;
1089 if (RPC_IS_SOFT(task
)) {
1090 printk(KERN_NOTICE
"%s: server %s not responding, timed out\n",
1091 clnt
->cl_protname
, clnt
->cl_server
);
1092 rpc_exit(task
, -EIO
);
1096 if (!(task
->tk_flags
& RPC_CALL_MAJORSEEN
)) {
1097 task
->tk_flags
|= RPC_CALL_MAJORSEEN
;
1098 printk(KERN_NOTICE
"%s: server %s not responding, still trying\n",
1099 clnt
->cl_protname
, clnt
->cl_server
);
1101 rpc_force_rebind(clnt
);
1104 clnt
->cl_stats
->rpcretrans
++;
1105 task
->tk_action
= call_bind
;
1106 task
->tk_status
= 0;
1110 * 7. Decode the RPC reply
1113 call_decode(struct rpc_task
*task
)
1115 struct rpc_clnt
*clnt
= task
->tk_client
;
1116 struct rpc_rqst
*req
= task
->tk_rqstp
;
1117 kxdrproc_t decode
= task
->tk_msg
.rpc_proc
->p_decode
;
1120 dprintk("RPC: %5u call_decode (status %d)\n",
1121 task
->tk_pid
, task
->tk_status
);
1123 if (task
->tk_flags
& RPC_CALL_MAJORSEEN
) {
1124 printk(KERN_NOTICE
"%s: server %s OK\n",
1125 clnt
->cl_protname
, clnt
->cl_server
);
1126 task
->tk_flags
&= ~RPC_CALL_MAJORSEEN
;
1129 if (task
->tk_status
< 12) {
1130 if (!RPC_IS_SOFT(task
)) {
1131 task
->tk_action
= call_bind
;
1132 clnt
->cl_stats
->rpcretrans
++;
1135 dprintk("RPC: %s: too small RPC reply size (%d bytes)\n",
1136 clnt
->cl_protname
, task
->tk_status
);
1137 task
->tk_action
= call_timeout
;
1142 * Ensure that we see all writes made by xprt_complete_rqst()
1143 * before it changed req->rq_received.
1146 req
->rq_rcv_buf
.len
= req
->rq_private_buf
.len
;
1148 /* Check that the softirq receive buffer is valid */
1149 WARN_ON(memcmp(&req
->rq_rcv_buf
, &req
->rq_private_buf
,
1150 sizeof(req
->rq_rcv_buf
)) != 0);
1152 /* Verify the RPC header */
1153 p
= call_verify(task
);
1155 if (p
== ERR_PTR(-EAGAIN
))
1160 task
->tk_action
= rpc_exit_task
;
1164 task
->tk_status
= rpcauth_unwrap_resp(task
, decode
, req
, p
,
1165 task
->tk_msg
.rpc_resp
);
1168 dprintk("RPC: %5u call_decode result %d\n", task
->tk_pid
,
1172 req
->rq_received
= req
->rq_private_buf
.len
= 0;
1173 task
->tk_status
= 0;
1174 if (task
->tk_client
->cl_discrtry
)
1175 xprt_disconnect(task
->tk_xprt
);
1179 * 8. Refresh the credentials if rejected by the server
1182 call_refresh(struct rpc_task
*task
)
1184 dprint_status(task
);
1186 xprt_release(task
); /* Must do to obtain new XID */
1187 task
->tk_action
= call_refreshresult
;
1188 task
->tk_status
= 0;
1189 task
->tk_client
->cl_stats
->rpcauthrefresh
++;
1190 rpcauth_refreshcred(task
);
1194 * 8a. Process the results of a credential refresh
1197 call_refreshresult(struct rpc_task
*task
)
1199 int status
= task
->tk_status
;
1201 dprint_status(task
);
1203 task
->tk_status
= 0;
1204 task
->tk_action
= call_reserve
;
1205 if (status
>= 0 && rpcauth_uptodatecred(task
))
1207 if (status
== -EACCES
) {
1208 rpc_exit(task
, -EACCES
);
1211 task
->tk_action
= call_refresh
;
1212 if (status
!= -ETIMEDOUT
)
1213 rpc_delay(task
, 3*HZ
);
1218 * Call header serialization
1221 call_header(struct rpc_task
*task
)
1223 struct rpc_clnt
*clnt
= task
->tk_client
;
1224 struct rpc_rqst
*req
= task
->tk_rqstp
;
1225 __be32
*p
= req
->rq_svec
[0].iov_base
;
1227 /* FIXME: check buffer size? */
1229 p
= xprt_skip_transport_header(task
->tk_xprt
, p
);
1230 *p
++ = req
->rq_xid
; /* XID */
1231 *p
++ = htonl(RPC_CALL
); /* CALL */
1232 *p
++ = htonl(RPC_VERSION
); /* RPC version */
1233 *p
++ = htonl(clnt
->cl_prog
); /* program number */
1234 *p
++ = htonl(clnt
->cl_vers
); /* program version */
1235 *p
++ = htonl(task
->tk_msg
.rpc_proc
->p_proc
); /* procedure */
1236 p
= rpcauth_marshcred(task
, p
);
1237 req
->rq_slen
= xdr_adjust_iovec(&req
->rq_svec
[0], p
);
1242 * Reply header verification
1245 call_verify(struct rpc_task
*task
)
1247 struct kvec
*iov
= &task
->tk_rqstp
->rq_rcv_buf
.head
[0];
1248 int len
= task
->tk_rqstp
->rq_rcv_buf
.len
>> 2;
1249 __be32
*p
= iov
->iov_base
;
1251 int error
= -EACCES
;
1253 if ((task
->tk_rqstp
->rq_rcv_buf
.len
& 3) != 0) {
1254 /* RFC-1014 says that the representation of XDR data must be a
1255 * multiple of four bytes
1256 * - if it isn't pointer subtraction in the NFS client may give
1260 "call_verify: XDR representation not a multiple of"
1261 " 4 bytes: 0x%x\n", task
->tk_rqstp
->rq_rcv_buf
.len
);
1266 p
+= 1; /* skip XID */
1268 if ((n
= ntohl(*p
++)) != RPC_REPLY
) {
1269 printk(KERN_WARNING
"call_verify: not an RPC reply: %x\n", n
);
1272 if ((n
= ntohl(*p
++)) != RPC_MSG_ACCEPTED
) {
1275 switch ((n
= ntohl(*p
++))) {
1276 case RPC_AUTH_ERROR
:
1279 dprintk("RPC: %5u %s: RPC call version "
1281 task
->tk_pid
, __FUNCTION__
);
1282 error
= -EPROTONOSUPPORT
;
1285 dprintk("RPC: %5u %s: RPC call rejected, "
1286 "unknown error: %x\n",
1287 task
->tk_pid
, __FUNCTION__
, n
);
1292 switch ((n
= ntohl(*p
++))) {
1293 case RPC_AUTH_REJECTEDCRED
:
1294 case RPC_AUTH_REJECTEDVERF
:
1295 case RPCSEC_GSS_CREDPROBLEM
:
1296 case RPCSEC_GSS_CTXPROBLEM
:
1297 if (!task
->tk_cred_retry
)
1299 task
->tk_cred_retry
--;
1300 dprintk("RPC: %5u %s: retry stale creds\n",
1301 task
->tk_pid
, __FUNCTION__
);
1302 rpcauth_invalcred(task
);
1303 task
->tk_action
= call_refresh
;
1305 case RPC_AUTH_BADCRED
:
1306 case RPC_AUTH_BADVERF
:
1307 /* possibly garbled cred/verf? */
1308 if (!task
->tk_garb_retry
)
1310 task
->tk_garb_retry
--;
1311 dprintk("RPC: %5u %s: retry garbled creds\n",
1312 task
->tk_pid
, __FUNCTION__
);
1313 task
->tk_action
= call_bind
;
1315 case RPC_AUTH_TOOWEAK
:
1316 printk(KERN_NOTICE
"call_verify: server %s requires stronger "
1317 "authentication.\n", task
->tk_client
->cl_server
);
1320 printk(KERN_WARNING
"call_verify: unknown auth error: %x\n", n
);
1323 dprintk("RPC: %5u %s: call rejected %d\n",
1324 task
->tk_pid
, __FUNCTION__
, n
);
1327 if (!(p
= rpcauth_checkverf(task
, p
))) {
1328 printk(KERN_WARNING
"call_verify: auth check failed\n");
1329 goto out_garbage
; /* bad verifier, retry */
1331 len
= p
- (__be32
*)iov
->iov_base
- 1;
1334 switch ((n
= ntohl(*p
++))) {
1337 case RPC_PROG_UNAVAIL
:
1338 dprintk("RPC: %5u %s: program %u is unsupported by server %s\n",
1339 task
->tk_pid
, __FUNCTION__
,
1340 (unsigned int)task
->tk_client
->cl_prog
,
1341 task
->tk_client
->cl_server
);
1342 error
= -EPFNOSUPPORT
;
1344 case RPC_PROG_MISMATCH
:
1345 dprintk("RPC: %5u %s: program %u, version %u unsupported by "
1346 "server %s\n", task
->tk_pid
, __FUNCTION__
,
1347 (unsigned int)task
->tk_client
->cl_prog
,
1348 (unsigned int)task
->tk_client
->cl_vers
,
1349 task
->tk_client
->cl_server
);
1350 error
= -EPROTONOSUPPORT
;
1352 case RPC_PROC_UNAVAIL
:
1353 dprintk("RPC: %5u %s: proc %p unsupported by program %u, "
1354 "version %u on server %s\n",
1355 task
->tk_pid
, __FUNCTION__
,
1356 task
->tk_msg
.rpc_proc
,
1357 task
->tk_client
->cl_prog
,
1358 task
->tk_client
->cl_vers
,
1359 task
->tk_client
->cl_server
);
1360 error
= -EOPNOTSUPP
;
1362 case RPC_GARBAGE_ARGS
:
1363 dprintk("RPC: %5u %s: server saw garbage\n",
1364 task
->tk_pid
, __FUNCTION__
);
1367 printk(KERN_WARNING
"call_verify: server accept status: %x\n", n
);
1372 task
->tk_client
->cl_stats
->rpcgarbage
++;
1373 if (task
->tk_garb_retry
) {
1374 task
->tk_garb_retry
--;
1375 dprintk("RPC: %5u %s: retrying\n",
1376 task
->tk_pid
, __FUNCTION__
);
1377 task
->tk_action
= call_bind
;
1379 return ERR_PTR(-EAGAIN
);
1381 printk(KERN_WARNING
"RPC %s: retry failed, exit EIO\n", __FUNCTION__
);
1385 rpc_exit(task
, error
);
1386 return ERR_PTR(error
);
1388 printk(KERN_WARNING
"RPC %s: server reply was truncated.\n", __FUNCTION__
);
1392 static int rpcproc_encode_null(void *rqstp
, __be32
*data
, void *obj
)
1397 static int rpcproc_decode_null(void *rqstp
, __be32
*data
, void *obj
)
1402 static struct rpc_procinfo rpcproc_null
= {
1403 .p_encode
= rpcproc_encode_null
,
1404 .p_decode
= rpcproc_decode_null
,
1407 int rpc_ping(struct rpc_clnt
*clnt
, int flags
)
1409 struct rpc_message msg
= {
1410 .rpc_proc
= &rpcproc_null
,
1413 msg
.rpc_cred
= authnull_ops
.lookup_cred(NULL
, NULL
, 0);
1414 err
= rpc_call_sync(clnt
, &msg
, flags
);
1415 put_rpccred(msg
.rpc_cred
);