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/utsname.h>
31 #include <linux/workqueue.h>
33 #include <linux/sunrpc/clnt.h>
34 #include <linux/sunrpc/rpc_pipe_fs.h>
35 #include <linux/sunrpc/metrics.h>
38 #define RPC_SLACK_SPACE (1024) /* total overkill */
41 # define RPCDBG_FACILITY RPCDBG_CALL
44 static DECLARE_WAIT_QUEUE_HEAD(destroy_wait
);
47 static void call_start(struct rpc_task
*task
);
48 static void call_reserve(struct rpc_task
*task
);
49 static void call_reserveresult(struct rpc_task
*task
);
50 static void call_allocate(struct rpc_task
*task
);
51 static void call_encode(struct rpc_task
*task
);
52 static void call_decode(struct rpc_task
*task
);
53 static void call_bind(struct rpc_task
*task
);
54 static void call_bind_status(struct rpc_task
*task
);
55 static void call_transmit(struct rpc_task
*task
);
56 static void call_status(struct rpc_task
*task
);
57 static void call_transmit_status(struct rpc_task
*task
);
58 static void call_refresh(struct rpc_task
*task
);
59 static void call_refreshresult(struct rpc_task
*task
);
60 static void call_timeout(struct rpc_task
*task
);
61 static void call_connect(struct rpc_task
*task
);
62 static void call_connect_status(struct rpc_task
*task
);
63 static __be32
* call_header(struct rpc_task
*task
);
64 static __be32
* call_verify(struct rpc_task
*task
);
68 rpc_setup_pipedir(struct rpc_clnt
*clnt
, char *dir_name
)
70 static uint32_t clntid
;
73 clnt
->cl_vfsmnt
= ERR_PTR(-ENOENT
);
74 clnt
->cl_dentry
= ERR_PTR(-ENOENT
);
78 clnt
->cl_vfsmnt
= rpc_get_mount();
79 if (IS_ERR(clnt
->cl_vfsmnt
))
80 return PTR_ERR(clnt
->cl_vfsmnt
);
83 snprintf(clnt
->cl_pathname
, sizeof(clnt
->cl_pathname
),
84 "%s/clnt%x", dir_name
,
85 (unsigned int)clntid
++);
86 clnt
->cl_pathname
[sizeof(clnt
->cl_pathname
) - 1] = '\0';
87 clnt
->cl_dentry
= rpc_mkdir(clnt
->cl_pathname
, clnt
);
88 if (!IS_ERR(clnt
->cl_dentry
))
90 error
= PTR_ERR(clnt
->cl_dentry
);
91 if (error
!= -EEXIST
) {
92 printk(KERN_INFO
"RPC: Couldn't create pipefs entry %s, error %d\n",
93 clnt
->cl_pathname
, error
);
100 static struct rpc_clnt
* rpc_new_client(struct rpc_xprt
*xprt
, char *servname
, struct rpc_program
*program
, u32 vers
, rpc_authflavor_t flavor
)
102 struct rpc_version
*version
;
103 struct rpc_clnt
*clnt
= NULL
;
104 struct rpc_auth
*auth
;
108 dprintk("RPC: creating %s client for %s (xprt %p)\n",
109 program
->name
, servname
, xprt
);
114 if (vers
>= program
->nrvers
|| !(version
= program
->version
[vers
]))
118 clnt
= kzalloc(sizeof(*clnt
), GFP_KERNEL
);
121 atomic_set(&clnt
->cl_users
, 0);
122 atomic_set(&clnt
->cl_count
, 1);
123 clnt
->cl_parent
= clnt
;
125 clnt
->cl_server
= clnt
->cl_inline_name
;
126 len
= strlen(servname
) + 1;
127 if (len
> sizeof(clnt
->cl_inline_name
)) {
128 char *buf
= kmalloc(len
, GFP_KERNEL
);
130 clnt
->cl_server
= buf
;
132 len
= sizeof(clnt
->cl_inline_name
);
134 strlcpy(clnt
->cl_server
, servname
, len
);
136 clnt
->cl_xprt
= xprt
;
137 clnt
->cl_procinfo
= version
->procs
;
138 clnt
->cl_maxproc
= version
->nrprocs
;
139 clnt
->cl_protname
= program
->name
;
140 clnt
->cl_prog
= program
->number
;
141 clnt
->cl_vers
= version
->number
;
142 clnt
->cl_stats
= program
->stats
;
143 clnt
->cl_metrics
= rpc_alloc_iostats(clnt
);
144 clnt
->cl_program
= program
;
146 if (!xprt_bound(clnt
->cl_xprt
))
147 clnt
->cl_autobind
= 1;
149 clnt
->cl_rtt
= &clnt
->cl_rtt_default
;
150 rpc_init_rtt(&clnt
->cl_rtt_default
, xprt
->timeout
.to_initval
);
152 err
= rpc_setup_pipedir(clnt
, program
->pipe_dir_name
);
156 auth
= rpcauth_create(flavor
, clnt
);
158 printk(KERN_INFO
"RPC: Couldn't create auth handle (flavor %u)\n",
164 /* save the nodename */
165 clnt
->cl_nodelen
= strlen(utsname()->nodename
);
166 if (clnt
->cl_nodelen
> UNX_MAXNODENAME
)
167 clnt
->cl_nodelen
= UNX_MAXNODENAME
;
168 memcpy(clnt
->cl_nodename
, utsname()->nodename
, clnt
->cl_nodelen
);
172 if (!IS_ERR(clnt
->cl_dentry
)) {
173 rpc_rmdir(clnt
->cl_dentry
);
177 if (clnt
->cl_server
!= clnt
->cl_inline_name
)
178 kfree(clnt
->cl_server
);
187 * rpc_create - create an RPC client and transport with one call
188 * @args: rpc_clnt create argument structure
190 * Creates and initializes an RPC transport and an RPC client.
192 * It can ping the server in order to determine if it is up, and to see if
193 * it supports this program and version. RPC_CLNT_CREATE_NOPING disables
194 * this behavior so asynchronous tasks can also use rpc_create.
196 struct rpc_clnt
*rpc_create(struct rpc_create_args
*args
)
198 struct rpc_xprt
*xprt
;
199 struct rpc_clnt
*clnt
;
201 xprt
= xprt_create_transport(args
->protocol
, args
->address
,
202 args
->addrsize
, args
->timeout
);
204 return (struct rpc_clnt
*)xprt
;
207 * By default, kernel RPC client connects from a reserved port.
208 * CAP_NET_BIND_SERVICE will not be set for unprivileged requesters,
209 * but it is always enabled for rpciod, which handles the connect
213 if (args
->flags
& RPC_CLNT_CREATE_NONPRIVPORT
)
216 dprintk("RPC: creating %s client for %s (xprt %p)\n",
217 args
->program
->name
, args
->servername
, xprt
);
219 clnt
= rpc_new_client(xprt
, args
->servername
, args
->program
,
220 args
->version
, args
->authflavor
);
224 if (!(args
->flags
& RPC_CLNT_CREATE_NOPING
)) {
225 int err
= rpc_ping(clnt
, RPC_TASK_SOFT
|RPC_TASK_NOINTR
);
227 rpc_shutdown_client(clnt
);
232 clnt
->cl_softrtry
= 1;
233 if (args
->flags
& RPC_CLNT_CREATE_HARDRTRY
)
234 clnt
->cl_softrtry
= 0;
236 if (args
->flags
& RPC_CLNT_CREATE_INTR
)
238 if (args
->flags
& RPC_CLNT_CREATE_AUTOBIND
)
239 clnt
->cl_autobind
= 1;
240 if (args
->flags
& RPC_CLNT_CREATE_ONESHOT
)
241 clnt
->cl_oneshot
= 1;
245 EXPORT_SYMBOL_GPL(rpc_create
);
248 * This function clones the RPC client structure. It allows us to share the
249 * same transport while varying parameters such as the authentication
253 rpc_clone_client(struct rpc_clnt
*clnt
)
255 struct rpc_clnt
*new;
258 new = kmalloc(sizeof(*new), GFP_KERNEL
);
261 memcpy(new, clnt
, sizeof(*new));
262 atomic_set(&new->cl_count
, 1);
263 atomic_set(&new->cl_users
, 0);
264 new->cl_metrics
= rpc_alloc_iostats(clnt
);
265 err
= rpc_setup_pipedir(new, clnt
->cl_program
->pipe_dir_name
);
268 new->cl_parent
= clnt
;
269 atomic_inc(&clnt
->cl_count
);
270 new->cl_xprt
= xprt_get(clnt
->cl_xprt
);
271 /* Turn off autobind on clones */
272 new->cl_autobind
= 0;
275 rpc_init_rtt(&new->cl_rtt_default
, clnt
->cl_xprt
->timeout
.to_initval
);
277 atomic_inc(&new->cl_auth
->au_count
);
280 rpc_free_iostats(new->cl_metrics
);
283 dprintk("RPC: %s returned error %d\n", __FUNCTION__
, err
);
288 * Properly shut down an RPC client, terminating all outstanding
289 * requests. Note that we must be certain that cl_oneshot and
290 * cl_dead are cleared, or else the client would be destroyed
291 * when the last task releases it.
294 rpc_shutdown_client(struct rpc_clnt
*clnt
)
296 dprintk("RPC: shutting down %s client for %s, tasks=%d\n",
297 clnt
->cl_protname
, clnt
->cl_server
,
298 atomic_read(&clnt
->cl_users
));
300 while (atomic_read(&clnt
->cl_users
) > 0) {
301 /* Don't let rpc_release_client destroy us */
302 clnt
->cl_oneshot
= 0;
304 rpc_killall_tasks(clnt
);
305 wait_event_timeout(destroy_wait
,
306 !atomic_read(&clnt
->cl_users
), 1*HZ
);
309 if (atomic_read(&clnt
->cl_users
) < 0) {
310 printk(KERN_ERR
"RPC: rpc_shutdown_client clnt %p tasks=%d\n",
311 clnt
, atomic_read(&clnt
->cl_users
));
318 return rpc_destroy_client(clnt
);
322 * Delete an RPC client
325 rpc_destroy_client(struct rpc_clnt
*clnt
)
327 if (!atomic_dec_and_test(&clnt
->cl_count
))
329 BUG_ON(atomic_read(&clnt
->cl_users
) != 0);
331 dprintk("RPC: destroying %s client for %s\n",
332 clnt
->cl_protname
, clnt
->cl_server
);
334 rpcauth_destroy(clnt
->cl_auth
);
335 clnt
->cl_auth
= NULL
;
337 if (!IS_ERR(clnt
->cl_dentry
)) {
338 rpc_rmdir(clnt
->cl_dentry
);
341 if (clnt
->cl_parent
!= clnt
) {
342 rpc_destroy_client(clnt
->cl_parent
);
345 if (clnt
->cl_server
!= clnt
->cl_inline_name
)
346 kfree(clnt
->cl_server
);
348 rpc_free_iostats(clnt
->cl_metrics
);
349 clnt
->cl_metrics
= NULL
;
350 xprt_put(clnt
->cl_xprt
);
356 * Release an RPC client
359 rpc_release_client(struct rpc_clnt
*clnt
)
361 dprintk("RPC: rpc_release_client(%p, %d)\n",
362 clnt
, atomic_read(&clnt
->cl_users
));
364 if (!atomic_dec_and_test(&clnt
->cl_users
))
366 wake_up(&destroy_wait
);
367 if (clnt
->cl_oneshot
|| clnt
->cl_dead
)
368 rpc_destroy_client(clnt
);
372 * rpc_bind_new_program - bind a new RPC program to an existing client
373 * @old - old rpc_client
374 * @program - rpc program to set
375 * @vers - rpc program version
377 * Clones the rpc client and sets up a new RPC program. This is mainly
378 * of use for enabling different RPC programs to share the same transport.
379 * The Sun NFSv2/v3 ACL protocol can do this.
381 struct rpc_clnt
*rpc_bind_new_program(struct rpc_clnt
*old
,
382 struct rpc_program
*program
,
385 struct rpc_clnt
*clnt
;
386 struct rpc_version
*version
;
389 BUG_ON(vers
>= program
->nrvers
|| !program
->version
[vers
]);
390 version
= program
->version
[vers
];
391 clnt
= rpc_clone_client(old
);
394 clnt
->cl_procinfo
= version
->procs
;
395 clnt
->cl_maxproc
= version
->nrprocs
;
396 clnt
->cl_protname
= program
->name
;
397 clnt
->cl_prog
= program
->number
;
398 clnt
->cl_vers
= version
->number
;
399 clnt
->cl_stats
= program
->stats
;
400 err
= rpc_ping(clnt
, RPC_TASK_SOFT
|RPC_TASK_NOINTR
);
402 rpc_shutdown_client(clnt
);
410 * Default callback for async RPC calls
413 rpc_default_callback(struct rpc_task
*task
, void *data
)
417 static const struct rpc_call_ops rpc_default_ops
= {
418 .rpc_call_done
= rpc_default_callback
,
422 * Export the signal mask handling for synchronous code that
423 * sleeps on RPC calls
425 #define RPC_INTR_SIGNALS (sigmask(SIGHUP) | sigmask(SIGINT) | sigmask(SIGQUIT) | sigmask(SIGTERM))
427 static void rpc_save_sigmask(sigset_t
*oldset
, int intr
)
429 unsigned long sigallow
= sigmask(SIGKILL
);
432 /* Block all signals except those listed in sigallow */
434 sigallow
|= RPC_INTR_SIGNALS
;
435 siginitsetinv(&sigmask
, sigallow
);
436 sigprocmask(SIG_BLOCK
, &sigmask
, oldset
);
439 static inline void rpc_task_sigmask(struct rpc_task
*task
, sigset_t
*oldset
)
441 rpc_save_sigmask(oldset
, !RPC_TASK_UNINTERRUPTIBLE(task
));
444 static inline void rpc_restore_sigmask(sigset_t
*oldset
)
446 sigprocmask(SIG_SETMASK
, oldset
, NULL
);
449 void rpc_clnt_sigmask(struct rpc_clnt
*clnt
, sigset_t
*oldset
)
451 rpc_save_sigmask(oldset
, clnt
->cl_intr
);
454 void rpc_clnt_sigunmask(struct rpc_clnt
*clnt
, sigset_t
*oldset
)
456 rpc_restore_sigmask(oldset
);
460 * New rpc_call implementation
462 int rpc_call_sync(struct rpc_clnt
*clnt
, struct rpc_message
*msg
, int flags
)
464 struct rpc_task
*task
;
468 /* If this client is slain all further I/O fails */
472 BUG_ON(flags
& RPC_TASK_ASYNC
);
475 task
= rpc_new_task(clnt
, flags
, &rpc_default_ops
, NULL
);
479 /* Mask signals on RPC calls _and_ GSS_AUTH upcalls */
480 rpc_task_sigmask(task
, &oldset
);
482 rpc_call_setup(task
, msg
, 0);
484 /* Set up the call info struct and execute the task */
485 status
= task
->tk_status
;
487 atomic_inc(&task
->tk_count
);
488 status
= rpc_execute(task
);
490 status
= task
->tk_status
;
492 rpc_restore_sigmask(&oldset
);
493 rpc_release_task(task
);
499 * New rpc_call implementation
502 rpc_call_async(struct rpc_clnt
*clnt
, struct rpc_message
*msg
, int flags
,
503 const struct rpc_call_ops
*tk_ops
, void *data
)
505 struct rpc_task
*task
;
509 /* If this client is slain all further I/O fails */
514 flags
|= RPC_TASK_ASYNC
;
516 /* Create/initialize a new RPC task */
518 if (!(task
= rpc_new_task(clnt
, flags
, tk_ops
, data
)))
521 /* Mask signals on GSS_AUTH upcalls */
522 rpc_task_sigmask(task
, &oldset
);
524 rpc_call_setup(task
, msg
, 0);
526 /* Set up the call info struct and execute the task */
527 status
= task
->tk_status
;
531 rpc_release_task(task
);
533 rpc_restore_sigmask(&oldset
);
536 if (tk_ops
->rpc_release
!= NULL
)
537 tk_ops
->rpc_release(data
);
543 rpc_call_setup(struct rpc_task
*task
, struct rpc_message
*msg
, int flags
)
546 task
->tk_flags
|= flags
;
547 /* Bind the user cred */
548 if (task
->tk_msg
.rpc_cred
!= NULL
)
549 rpcauth_holdcred(task
);
551 rpcauth_bindcred(task
);
553 if (task
->tk_status
== 0)
554 task
->tk_action
= call_start
;
556 task
->tk_action
= rpc_exit_task
;
560 * rpc_peeraddr - extract remote peer address from clnt's xprt
561 * @clnt: RPC client structure
562 * @buf: target buffer
563 * @size: length of target buffer
565 * Returns the number of bytes that are actually in the stored address.
567 size_t rpc_peeraddr(struct rpc_clnt
*clnt
, struct sockaddr
*buf
, size_t bufsize
)
570 struct rpc_xprt
*xprt
= clnt
->cl_xprt
;
572 bytes
= sizeof(xprt
->addr
);
575 memcpy(buf
, &clnt
->cl_xprt
->addr
, bytes
);
576 return xprt
->addrlen
;
578 EXPORT_SYMBOL_GPL(rpc_peeraddr
);
581 * rpc_peeraddr2str - return remote peer address in printable format
582 * @clnt: RPC client structure
583 * @format: address format
586 char *rpc_peeraddr2str(struct rpc_clnt
*clnt
, enum rpc_display_format_t format
)
588 struct rpc_xprt
*xprt
= clnt
->cl_xprt
;
589 return xprt
->ops
->print_addr(xprt
, format
);
591 EXPORT_SYMBOL_GPL(rpc_peeraddr2str
);
594 rpc_setbufsize(struct rpc_clnt
*clnt
, unsigned int sndsize
, unsigned int rcvsize
)
596 struct rpc_xprt
*xprt
= clnt
->cl_xprt
;
597 if (xprt
->ops
->set_buffer_size
)
598 xprt
->ops
->set_buffer_size(xprt
, sndsize
, rcvsize
);
602 * Return size of largest payload RPC client can support, in bytes
604 * For stream transports, this is one RPC record fragment (see RFC
605 * 1831), as we don't support multi-record requests yet. For datagram
606 * transports, this is the size of an IP packet minus the IP, UDP, and
609 size_t rpc_max_payload(struct rpc_clnt
*clnt
)
611 return clnt
->cl_xprt
->max_payload
;
613 EXPORT_SYMBOL_GPL(rpc_max_payload
);
616 * rpc_force_rebind - force transport to check that remote port is unchanged
617 * @clnt: client to rebind
620 void rpc_force_rebind(struct rpc_clnt
*clnt
)
622 if (clnt
->cl_autobind
)
623 xprt_clear_bound(clnt
->cl_xprt
);
625 EXPORT_SYMBOL_GPL(rpc_force_rebind
);
628 * Restart an (async) RPC call. Usually called from within the
632 rpc_restart_call(struct rpc_task
*task
)
634 if (RPC_ASSASSINATED(task
))
637 task
->tk_action
= call_start
;
643 * Other FSM states can be visited zero or more times, but
644 * this state is visited exactly once for each RPC.
647 call_start(struct rpc_task
*task
)
649 struct rpc_clnt
*clnt
= task
->tk_client
;
651 dprintk("RPC: %4d call_start %s%d proc %d (%s)\n", task
->tk_pid
,
652 clnt
->cl_protname
, clnt
->cl_vers
, task
->tk_msg
.rpc_proc
->p_proc
,
653 (RPC_IS_ASYNC(task
) ? "async" : "sync"));
655 /* Increment call count */
656 task
->tk_msg
.rpc_proc
->p_count
++;
657 clnt
->cl_stats
->rpccnt
++;
658 task
->tk_action
= call_reserve
;
662 * 1. Reserve an RPC call slot
665 call_reserve(struct rpc_task
*task
)
667 dprintk("RPC: %4d call_reserve\n", task
->tk_pid
);
669 if (!rpcauth_uptodatecred(task
)) {
670 task
->tk_action
= call_refresh
;
675 task
->tk_action
= call_reserveresult
;
680 * 1b. Grok the result of xprt_reserve()
683 call_reserveresult(struct rpc_task
*task
)
685 int status
= task
->tk_status
;
687 dprintk("RPC: %4d call_reserveresult (status %d)\n",
688 task
->tk_pid
, task
->tk_status
);
691 * After a call to xprt_reserve(), we must have either
692 * a request slot or else an error status.
696 if (task
->tk_rqstp
) {
697 task
->tk_action
= call_allocate
;
701 printk(KERN_ERR
"%s: status=%d, but no request slot, exiting\n",
702 __FUNCTION__
, status
);
703 rpc_exit(task
, -EIO
);
708 * Even though there was an error, we may have acquired
709 * a request slot somehow. Make sure not to leak it.
711 if (task
->tk_rqstp
) {
712 printk(KERN_ERR
"%s: status=%d, request allocated anyway\n",
713 __FUNCTION__
, status
);
718 case -EAGAIN
: /* woken up; retry */
719 task
->tk_action
= call_reserve
;
721 case -EIO
: /* probably a shutdown */
724 printk(KERN_ERR
"%s: unrecognized error %d, exiting\n",
725 __FUNCTION__
, status
);
728 rpc_exit(task
, status
);
732 * 2. Allocate the buffer. For details, see sched.c:rpc_malloc.
733 * (Note: buffer memory is freed in xprt_release).
736 call_allocate(struct rpc_task
*task
)
738 struct rpc_rqst
*req
= task
->tk_rqstp
;
739 struct rpc_xprt
*xprt
= task
->tk_xprt
;
742 dprintk("RPC: %4d call_allocate (status %d)\n",
743 task
->tk_pid
, task
->tk_status
);
744 task
->tk_action
= call_bind
;
748 /* FIXME: compute buffer requirements more exactly using
750 bufsiz
= task
->tk_msg
.rpc_proc
->p_bufsiz
+ RPC_SLACK_SPACE
;
752 if (xprt
->ops
->buf_alloc(task
, bufsiz
<< 1) != NULL
)
754 printk(KERN_INFO
"RPC: buffer allocation failed for task %p\n", task
);
756 if (RPC_IS_ASYNC(task
) || !signalled()) {
758 task
->tk_action
= call_reserve
;
759 rpc_delay(task
, HZ
>>4);
763 rpc_exit(task
, -ERESTARTSYS
);
767 rpc_task_need_encode(struct rpc_task
*task
)
769 return task
->tk_rqstp
->rq_snd_buf
.len
== 0;
773 rpc_task_force_reencode(struct rpc_task
*task
)
775 task
->tk_rqstp
->rq_snd_buf
.len
= 0;
779 * 3. Encode arguments of an RPC call
782 call_encode(struct rpc_task
*task
)
784 struct rpc_rqst
*req
= task
->tk_rqstp
;
785 struct xdr_buf
*sndbuf
= &req
->rq_snd_buf
;
786 struct xdr_buf
*rcvbuf
= &req
->rq_rcv_buf
;
791 dprintk("RPC: %4d call_encode (status %d)\n",
792 task
->tk_pid
, task
->tk_status
);
794 /* Default buffer setup */
795 bufsiz
= req
->rq_bufsize
>> 1;
796 sndbuf
->head
[0].iov_base
= (void *)req
->rq_buffer
;
797 sndbuf
->head
[0].iov_len
= bufsiz
;
798 sndbuf
->tail
[0].iov_len
= 0;
799 sndbuf
->page_len
= 0;
801 sndbuf
->buflen
= bufsiz
;
802 rcvbuf
->head
[0].iov_base
= (void *)((char *)req
->rq_buffer
+ bufsiz
);
803 rcvbuf
->head
[0].iov_len
= bufsiz
;
804 rcvbuf
->tail
[0].iov_len
= 0;
805 rcvbuf
->page_len
= 0;
807 rcvbuf
->buflen
= bufsiz
;
809 /* Encode header and provided arguments */
810 encode
= task
->tk_msg
.rpc_proc
->p_encode
;
811 if (!(p
= call_header(task
))) {
812 printk(KERN_INFO
"RPC: call_header failed, exit EIO\n");
813 rpc_exit(task
, -EIO
);
819 task
->tk_status
= rpcauth_wrap_req(task
, encode
, req
, p
,
820 task
->tk_msg
.rpc_argp
);
821 if (task
->tk_status
== -ENOMEM
) {
822 /* XXX: Is this sane? */
823 rpc_delay(task
, 3*HZ
);
824 task
->tk_status
= -EAGAIN
;
829 * 4. Get the server port number if not yet set
832 call_bind(struct rpc_task
*task
)
834 struct rpc_xprt
*xprt
= task
->tk_xprt
;
836 dprintk("RPC: %4d call_bind (status %d)\n",
837 task
->tk_pid
, task
->tk_status
);
839 task
->tk_action
= call_connect
;
840 if (!xprt_bound(xprt
)) {
841 task
->tk_action
= call_bind_status
;
842 task
->tk_timeout
= xprt
->bind_timeout
;
843 xprt
->ops
->rpcbind(task
);
848 * 4a. Sort out bind result
851 call_bind_status(struct rpc_task
*task
)
853 int status
= -EACCES
;
855 if (task
->tk_status
>= 0) {
856 dprintk("RPC: %4d call_bind_status (status %d)\n",
857 task
->tk_pid
, task
->tk_status
);
859 task
->tk_action
= call_connect
;
863 switch (task
->tk_status
) {
865 dprintk("RPC: %4d remote rpcbind: RPC program/version unavailable\n",
867 rpc_delay(task
, 3*HZ
);
870 dprintk("RPC: %4d rpcbind request timed out\n",
874 dprintk("RPC: %4d remote rpcbind service unavailable\n",
877 case -EPROTONOSUPPORT
:
878 dprintk("RPC: %4d remote rpcbind version 2 unavailable\n",
882 dprintk("RPC: %4d unrecognized rpcbind error (%d)\n",
883 task
->tk_pid
, -task
->tk_status
);
887 rpc_exit(task
, status
);
891 task
->tk_action
= call_timeout
;
895 * 4b. Connect to the RPC server
898 call_connect(struct rpc_task
*task
)
900 struct rpc_xprt
*xprt
= task
->tk_xprt
;
902 dprintk("RPC: %4d call_connect xprt %p %s connected\n",
904 (xprt_connected(xprt
) ? "is" : "is not"));
906 task
->tk_action
= call_transmit
;
907 if (!xprt_connected(xprt
)) {
908 task
->tk_action
= call_connect_status
;
909 if (task
->tk_status
< 0)
916 * 4c. Sort out connect result
919 call_connect_status(struct rpc_task
*task
)
921 struct rpc_clnt
*clnt
= task
->tk_client
;
922 int status
= task
->tk_status
;
924 dprintk("RPC: %5u call_connect_status (status %d)\n",
925 task
->tk_pid
, task
->tk_status
);
929 clnt
->cl_stats
->netreconn
++;
930 task
->tk_action
= call_transmit
;
934 /* Something failed: remote service port may have changed */
935 rpc_force_rebind(clnt
);
940 task
->tk_action
= call_bind
;
941 if (!RPC_IS_SOFT(task
))
943 /* if soft mounted, test if we've timed out */
945 task
->tk_action
= call_timeout
;
948 rpc_exit(task
, -EIO
);
952 * 5. Transmit the RPC request, and wait for reply
955 call_transmit(struct rpc_task
*task
)
957 dprintk("RPC: %4d call_transmit (status %d)\n",
958 task
->tk_pid
, task
->tk_status
);
960 task
->tk_action
= call_status
;
961 if (task
->tk_status
< 0)
963 task
->tk_status
= xprt_prepare_transmit(task
);
964 if (task
->tk_status
!= 0)
966 task
->tk_action
= call_transmit_status
;
967 /* Encode here so that rpcsec_gss can use correct sequence number. */
968 if (rpc_task_need_encode(task
)) {
969 BUG_ON(task
->tk_rqstp
->rq_bytes_sent
!= 0);
971 /* Did the encode result in an error condition? */
972 if (task
->tk_status
!= 0)
976 if (task
->tk_status
< 0)
979 * On success, ensure that we call xprt_end_transmit() before sleeping
980 * in order to allow access to the socket to other RPC requests.
982 call_transmit_status(task
);
983 if (task
->tk_msg
.rpc_proc
->p_decode
!= NULL
)
985 task
->tk_action
= rpc_exit_task
;
986 rpc_wake_up_task(task
);
990 * 5a. Handle cleanup after a transmission
993 call_transmit_status(struct rpc_task
*task
)
995 task
->tk_action
= call_status
;
997 * Special case: if we've been waiting on the socket's write_space()
998 * callback, then don't call xprt_end_transmit().
1000 if (task
->tk_status
== -EAGAIN
)
1002 xprt_end_transmit(task
);
1003 rpc_task_force_reencode(task
);
1007 * 6. Sort out the RPC call status
1010 call_status(struct rpc_task
*task
)
1012 struct rpc_clnt
*clnt
= task
->tk_client
;
1013 struct rpc_rqst
*req
= task
->tk_rqstp
;
1016 if (req
->rq_received
> 0 && !req
->rq_bytes_sent
)
1017 task
->tk_status
= req
->rq_received
;
1019 dprintk("RPC: %4d call_status (status %d)\n",
1020 task
->tk_pid
, task
->tk_status
);
1022 status
= task
->tk_status
;
1024 task
->tk_action
= call_decode
;
1028 task
->tk_status
= 0;
1034 * Delay any retries for 3 seconds, then handle as if it
1037 rpc_delay(task
, 3*HZ
);
1039 task
->tk_action
= call_timeout
;
1043 rpc_force_rebind(clnt
);
1044 task
->tk_action
= call_bind
;
1047 task
->tk_action
= call_transmit
;
1050 /* shutdown or soft timeout */
1051 rpc_exit(task
, status
);
1054 printk("%s: RPC call returned error %d\n",
1055 clnt
->cl_protname
, -status
);
1056 rpc_exit(task
, status
);
1061 * 6a. Handle RPC timeout
1062 * We do not release the request slot, so we keep using the
1063 * same XID for all retransmits.
1066 call_timeout(struct rpc_task
*task
)
1068 struct rpc_clnt
*clnt
= task
->tk_client
;
1070 if (xprt_adjust_timeout(task
->tk_rqstp
) == 0) {
1071 dprintk("RPC: %4d call_timeout (minor)\n", task
->tk_pid
);
1075 dprintk("RPC: %4d call_timeout (major)\n", task
->tk_pid
);
1076 task
->tk_timeouts
++;
1078 if (RPC_IS_SOFT(task
)) {
1079 printk(KERN_NOTICE
"%s: server %s not responding, timed out\n",
1080 clnt
->cl_protname
, clnt
->cl_server
);
1081 rpc_exit(task
, -EIO
);
1085 if (!(task
->tk_flags
& RPC_CALL_MAJORSEEN
)) {
1086 task
->tk_flags
|= RPC_CALL_MAJORSEEN
;
1087 printk(KERN_NOTICE
"%s: server %s not responding, still trying\n",
1088 clnt
->cl_protname
, clnt
->cl_server
);
1090 rpc_force_rebind(clnt
);
1093 clnt
->cl_stats
->rpcretrans
++;
1094 task
->tk_action
= call_bind
;
1095 task
->tk_status
= 0;
1099 * 7. Decode the RPC reply
1102 call_decode(struct rpc_task
*task
)
1104 struct rpc_clnt
*clnt
= task
->tk_client
;
1105 struct rpc_rqst
*req
= task
->tk_rqstp
;
1106 kxdrproc_t decode
= task
->tk_msg
.rpc_proc
->p_decode
;
1109 dprintk("RPC: %4d call_decode (status %d)\n",
1110 task
->tk_pid
, task
->tk_status
);
1112 if (task
->tk_flags
& RPC_CALL_MAJORSEEN
) {
1113 printk(KERN_NOTICE
"%s: server %s OK\n",
1114 clnt
->cl_protname
, clnt
->cl_server
);
1115 task
->tk_flags
&= ~RPC_CALL_MAJORSEEN
;
1118 if (task
->tk_status
< 12) {
1119 if (!RPC_IS_SOFT(task
)) {
1120 task
->tk_action
= call_bind
;
1121 clnt
->cl_stats
->rpcretrans
++;
1124 dprintk("%s: too small RPC reply size (%d bytes)\n",
1125 clnt
->cl_protname
, task
->tk_status
);
1126 task
->tk_action
= call_timeout
;
1131 * Ensure that we see all writes made by xprt_complete_rqst()
1132 * before it changed req->rq_received.
1135 req
->rq_rcv_buf
.len
= req
->rq_private_buf
.len
;
1137 /* Check that the softirq receive buffer is valid */
1138 WARN_ON(memcmp(&req
->rq_rcv_buf
, &req
->rq_private_buf
,
1139 sizeof(req
->rq_rcv_buf
)) != 0);
1141 /* Verify the RPC header */
1142 p
= call_verify(task
);
1144 if (p
== ERR_PTR(-EAGAIN
))
1149 task
->tk_action
= rpc_exit_task
;
1152 task
->tk_status
= rpcauth_unwrap_resp(task
, decode
, req
, p
,
1153 task
->tk_msg
.rpc_resp
);
1154 dprintk("RPC: %4d call_decode result %d\n", task
->tk_pid
,
1158 req
->rq_received
= req
->rq_private_buf
.len
= 0;
1159 task
->tk_status
= 0;
1163 * 8. Refresh the credentials if rejected by the server
1166 call_refresh(struct rpc_task
*task
)
1168 dprintk("RPC: %4d call_refresh\n", task
->tk_pid
);
1170 xprt_release(task
); /* Must do to obtain new XID */
1171 task
->tk_action
= call_refreshresult
;
1172 task
->tk_status
= 0;
1173 task
->tk_client
->cl_stats
->rpcauthrefresh
++;
1174 rpcauth_refreshcred(task
);
1178 * 8a. Process the results of a credential refresh
1181 call_refreshresult(struct rpc_task
*task
)
1183 int status
= task
->tk_status
;
1184 dprintk("RPC: %4d call_refreshresult (status %d)\n",
1185 task
->tk_pid
, task
->tk_status
);
1187 task
->tk_status
= 0;
1188 task
->tk_action
= call_reserve
;
1189 if (status
>= 0 && rpcauth_uptodatecred(task
))
1191 if (status
== -EACCES
) {
1192 rpc_exit(task
, -EACCES
);
1195 task
->tk_action
= call_refresh
;
1196 if (status
!= -ETIMEDOUT
)
1197 rpc_delay(task
, 3*HZ
);
1202 * Call header serialization
1205 call_header(struct rpc_task
*task
)
1207 struct rpc_clnt
*clnt
= task
->tk_client
;
1208 struct rpc_rqst
*req
= task
->tk_rqstp
;
1209 __be32
*p
= req
->rq_svec
[0].iov_base
;
1211 /* FIXME: check buffer size? */
1213 p
= xprt_skip_transport_header(task
->tk_xprt
, p
);
1214 *p
++ = req
->rq_xid
; /* XID */
1215 *p
++ = htonl(RPC_CALL
); /* CALL */
1216 *p
++ = htonl(RPC_VERSION
); /* RPC version */
1217 *p
++ = htonl(clnt
->cl_prog
); /* program number */
1218 *p
++ = htonl(clnt
->cl_vers
); /* program version */
1219 *p
++ = htonl(task
->tk_msg
.rpc_proc
->p_proc
); /* procedure */
1220 p
= rpcauth_marshcred(task
, p
);
1221 req
->rq_slen
= xdr_adjust_iovec(&req
->rq_svec
[0], p
);
1226 * Reply header verification
1229 call_verify(struct rpc_task
*task
)
1231 struct kvec
*iov
= &task
->tk_rqstp
->rq_rcv_buf
.head
[0];
1232 int len
= task
->tk_rqstp
->rq_rcv_buf
.len
>> 2;
1233 __be32
*p
= iov
->iov_base
;
1235 int error
= -EACCES
;
1237 if ((task
->tk_rqstp
->rq_rcv_buf
.len
& 3) != 0) {
1238 /* RFC-1014 says that the representation of XDR data must be a
1239 * multiple of four bytes
1240 * - if it isn't pointer subtraction in the NFS client may give
1244 "call_verify: XDR representation not a multiple of"
1245 " 4 bytes: 0x%x\n", task
->tk_rqstp
->rq_rcv_buf
.len
);
1250 p
+= 1; /* skip XID */
1252 if ((n
= ntohl(*p
++)) != RPC_REPLY
) {
1253 printk(KERN_WARNING
"call_verify: not an RPC reply: %x\n", n
);
1256 if ((n
= ntohl(*p
++)) != RPC_MSG_ACCEPTED
) {
1259 switch ((n
= ntohl(*p
++))) {
1260 case RPC_AUTH_ERROR
:
1263 dprintk("%s: RPC call version mismatch!\n", __FUNCTION__
);
1264 error
= -EPROTONOSUPPORT
;
1267 dprintk("%s: RPC call rejected, unknown error: %x\n", __FUNCTION__
, n
);
1272 switch ((n
= ntohl(*p
++))) {
1273 case RPC_AUTH_REJECTEDCRED
:
1274 case RPC_AUTH_REJECTEDVERF
:
1275 case RPCSEC_GSS_CREDPROBLEM
:
1276 case RPCSEC_GSS_CTXPROBLEM
:
1277 if (!task
->tk_cred_retry
)
1279 task
->tk_cred_retry
--;
1280 dprintk("RPC: %4d call_verify: retry stale creds\n",
1282 rpcauth_invalcred(task
);
1283 task
->tk_action
= call_refresh
;
1285 case RPC_AUTH_BADCRED
:
1286 case RPC_AUTH_BADVERF
:
1287 /* possibly garbled cred/verf? */
1288 if (!task
->tk_garb_retry
)
1290 task
->tk_garb_retry
--;
1291 dprintk("RPC: %4d call_verify: retry garbled creds\n",
1293 task
->tk_action
= call_bind
;
1295 case RPC_AUTH_TOOWEAK
:
1296 printk(KERN_NOTICE
"call_verify: server %s requires stronger "
1297 "authentication.\n", task
->tk_client
->cl_server
);
1300 printk(KERN_WARNING
"call_verify: unknown auth error: %x\n", n
);
1303 dprintk("RPC: %4d call_verify: call rejected %d\n",
1307 if (!(p
= rpcauth_checkverf(task
, p
))) {
1308 printk(KERN_WARNING
"call_verify: auth check failed\n");
1309 goto out_garbage
; /* bad verifier, retry */
1311 len
= p
- (__be32
*)iov
->iov_base
- 1;
1314 switch ((n
= ntohl(*p
++))) {
1317 case RPC_PROG_UNAVAIL
:
1318 dprintk("RPC: call_verify: program %u is unsupported by server %s\n",
1319 (unsigned int)task
->tk_client
->cl_prog
,
1320 task
->tk_client
->cl_server
);
1321 error
= -EPFNOSUPPORT
;
1323 case RPC_PROG_MISMATCH
:
1324 dprintk("RPC: call_verify: program %u, version %u unsupported by server %s\n",
1325 (unsigned int)task
->tk_client
->cl_prog
,
1326 (unsigned int)task
->tk_client
->cl_vers
,
1327 task
->tk_client
->cl_server
);
1328 error
= -EPROTONOSUPPORT
;
1330 case RPC_PROC_UNAVAIL
:
1331 dprintk("RPC: call_verify: proc %p unsupported by program %u, version %u on server %s\n",
1332 task
->tk_msg
.rpc_proc
,
1333 task
->tk_client
->cl_prog
,
1334 task
->tk_client
->cl_vers
,
1335 task
->tk_client
->cl_server
);
1336 error
= -EOPNOTSUPP
;
1338 case RPC_GARBAGE_ARGS
:
1339 dprintk("RPC: %4d %s: server saw garbage\n", task
->tk_pid
, __FUNCTION__
);
1342 printk(KERN_WARNING
"call_verify: server accept status: %x\n", n
);
1347 task
->tk_client
->cl_stats
->rpcgarbage
++;
1348 if (task
->tk_garb_retry
) {
1349 task
->tk_garb_retry
--;
1350 dprintk("RPC %s: retrying %4d\n", __FUNCTION__
, task
->tk_pid
);
1351 task
->tk_action
= call_bind
;
1353 return ERR_PTR(-EAGAIN
);
1355 printk(KERN_WARNING
"RPC %s: retry failed, exit EIO\n", __FUNCTION__
);
1359 rpc_exit(task
, error
);
1360 return ERR_PTR(error
);
1362 printk(KERN_WARNING
"RPC %s: server reply was truncated.\n", __FUNCTION__
);
1366 static int rpcproc_encode_null(void *rqstp
, __be32
*data
, void *obj
)
1371 static int rpcproc_decode_null(void *rqstp
, __be32
*data
, void *obj
)
1376 static struct rpc_procinfo rpcproc_null
= {
1377 .p_encode
= rpcproc_encode_null
,
1378 .p_decode
= rpcproc_decode_null
,
1381 int rpc_ping(struct rpc_clnt
*clnt
, int flags
)
1383 struct rpc_message msg
= {
1384 .rpc_proc
= &rpcproc_null
,
1387 msg
.rpc_cred
= authnull_ops
.lookup_cred(NULL
, NULL
, 0);
1388 err
= rpc_call_sync(clnt
, &msg
, flags
);
1389 put_rpccred(msg
.rpc_cred
);