2 * linux/net/sunrpc/clnt.c
4 * This file contains the high-level RPC interface.
5 * It is modeled as a finite state machine to support both synchronous
6 * and asynchronous requests.
8 * - RPC header generation and argument serialization.
9 * - Credential refresh.
10 * - TCP connect handling.
11 * - Retry of operation when it is suspected the operation failed because
12 * of uid squashing on the server, or when the credentials were stale
13 * and need to be refreshed, or when a packet was damaged in transit.
14 * This may be have to be moved to the VFS layer.
16 * NB: BSD uses a more intelligent approach to guessing when a request
17 * or reply has been lost by keeping the RTO estimate for each procedure.
18 * We currently make do with a constant timeout value.
20 * Copyright (C) 1992,1993 Rick Sladkey <jrs@world.std.com>
21 * Copyright (C) 1995,1996 Olaf Kirch <okir@monad.swb.de>
24 #include <asm/system.h>
26 #include <linux/module.h>
27 #include <linux/types.h>
29 #include <linux/slab.h>
30 #include <linux/smp_lock.h>
31 #include <linux/utsname.h>
32 #include <linux/workqueue.h>
33 #include <linux/in6.h>
35 #include <linux/sunrpc/clnt.h>
36 #include <linux/sunrpc/rpc_pipe_fs.h>
37 #include <linux/sunrpc/metrics.h>
41 # define RPCDBG_FACILITY RPCDBG_CALL
44 #define dprint_status(t) \
45 dprintk("RPC: %5u %s (status %d)\n", t->tk_pid, \
46 __FUNCTION__, t->tk_status)
49 * All RPC clients are linked into this list
51 static LIST_HEAD(all_clients
);
52 static DEFINE_SPINLOCK(rpc_client_lock
);
54 static DECLARE_WAIT_QUEUE_HEAD(destroy_wait
);
57 static void call_start(struct rpc_task
*task
);
58 static void call_reserve(struct rpc_task
*task
);
59 static void call_reserveresult(struct rpc_task
*task
);
60 static void call_allocate(struct rpc_task
*task
);
61 static void call_encode(struct rpc_task
*task
);
62 static void call_decode(struct rpc_task
*task
);
63 static void call_bind(struct rpc_task
*task
);
64 static void call_bind_status(struct rpc_task
*task
);
65 static void call_transmit(struct rpc_task
*task
);
66 static void call_status(struct rpc_task
*task
);
67 static void call_transmit_status(struct rpc_task
*task
);
68 static void call_refresh(struct rpc_task
*task
);
69 static void call_refreshresult(struct rpc_task
*task
);
70 static void call_timeout(struct rpc_task
*task
);
71 static void call_connect(struct rpc_task
*task
);
72 static void call_connect_status(struct rpc_task
*task
);
73 static __be32
* call_header(struct rpc_task
*task
);
74 static __be32
* call_verify(struct rpc_task
*task
);
76 static int rpc_ping(struct rpc_clnt
*clnt
, int flags
);
78 static void rpc_register_client(struct rpc_clnt
*clnt
)
80 spin_lock(&rpc_client_lock
);
81 list_add(&clnt
->cl_clients
, &all_clients
);
82 spin_unlock(&rpc_client_lock
);
85 static void rpc_unregister_client(struct rpc_clnt
*clnt
)
87 spin_lock(&rpc_client_lock
);
88 list_del(&clnt
->cl_clients
);
89 spin_unlock(&rpc_client_lock
);
93 rpc_setup_pipedir(struct rpc_clnt
*clnt
, char *dir_name
)
95 static uint32_t clntid
;
98 clnt
->cl_vfsmnt
= ERR_PTR(-ENOENT
);
99 clnt
->cl_dentry
= ERR_PTR(-ENOENT
);
100 if (dir_name
== NULL
)
103 clnt
->cl_vfsmnt
= rpc_get_mount();
104 if (IS_ERR(clnt
->cl_vfsmnt
))
105 return PTR_ERR(clnt
->cl_vfsmnt
);
108 snprintf(clnt
->cl_pathname
, sizeof(clnt
->cl_pathname
),
109 "%s/clnt%x", dir_name
,
110 (unsigned int)clntid
++);
111 clnt
->cl_pathname
[sizeof(clnt
->cl_pathname
) - 1] = '\0';
112 clnt
->cl_dentry
= rpc_mkdir(clnt
->cl_pathname
, clnt
);
113 if (!IS_ERR(clnt
->cl_dentry
))
115 error
= PTR_ERR(clnt
->cl_dentry
);
116 if (error
!= -EEXIST
) {
117 printk(KERN_INFO
"RPC: Couldn't create pipefs entry %s, error %d\n",
118 clnt
->cl_pathname
, error
);
125 static struct rpc_clnt
* rpc_new_client(const struct rpc_create_args
*args
, struct rpc_xprt
*xprt
)
127 struct rpc_program
*program
= args
->program
;
128 struct rpc_version
*version
;
129 struct rpc_clnt
*clnt
= NULL
;
130 struct rpc_auth
*auth
;
134 /* sanity check the name before trying to print it */
136 len
= strlen(args
->servername
);
137 if (len
> RPC_MAXNETNAMELEN
)
141 dprintk("RPC: creating %s client for %s (xprt %p)\n",
142 program
->name
, args
->servername
, xprt
);
151 if (args
->version
>= program
->nrvers
)
153 version
= program
->version
[args
->version
];
158 clnt
= kzalloc(sizeof(*clnt
), GFP_KERNEL
);
161 clnt
->cl_parent
= clnt
;
163 clnt
->cl_server
= clnt
->cl_inline_name
;
164 if (len
> sizeof(clnt
->cl_inline_name
)) {
165 char *buf
= kmalloc(len
, GFP_KERNEL
);
167 clnt
->cl_server
= buf
;
169 len
= sizeof(clnt
->cl_inline_name
);
171 strlcpy(clnt
->cl_server
, args
->servername
, len
);
173 clnt
->cl_xprt
= xprt
;
174 clnt
->cl_procinfo
= version
->procs
;
175 clnt
->cl_maxproc
= version
->nrprocs
;
176 clnt
->cl_protname
= program
->name
;
177 clnt
->cl_prog
= program
->number
;
178 clnt
->cl_vers
= version
->number
;
179 clnt
->cl_stats
= program
->stats
;
180 clnt
->cl_metrics
= rpc_alloc_iostats(clnt
);
182 if (clnt
->cl_metrics
== NULL
)
184 clnt
->cl_program
= program
;
185 INIT_LIST_HEAD(&clnt
->cl_tasks
);
186 spin_lock_init(&clnt
->cl_lock
);
188 if (!xprt_bound(clnt
->cl_xprt
))
189 clnt
->cl_autobind
= 1;
191 clnt
->cl_timeout
= xprt
->timeout
;
192 if (args
->timeout
!= NULL
) {
193 memcpy(&clnt
->cl_timeout_default
, args
->timeout
,
194 sizeof(clnt
->cl_timeout_default
));
195 clnt
->cl_timeout
= &clnt
->cl_timeout_default
;
198 clnt
->cl_rtt
= &clnt
->cl_rtt_default
;
199 rpc_init_rtt(&clnt
->cl_rtt_default
, clnt
->cl_timeout
->to_initval
);
201 kref_init(&clnt
->cl_kref
);
203 err
= rpc_setup_pipedir(clnt
, program
->pipe_dir_name
);
207 auth
= rpcauth_create(args
->authflavor
, clnt
);
209 printk(KERN_INFO
"RPC: Couldn't create auth handle (flavor %u)\n",
215 /* save the nodename */
216 clnt
->cl_nodelen
= strlen(utsname()->nodename
);
217 if (clnt
->cl_nodelen
> UNX_MAXNODENAME
)
218 clnt
->cl_nodelen
= UNX_MAXNODENAME
;
219 memcpy(clnt
->cl_nodename
, utsname()->nodename
, clnt
->cl_nodelen
);
220 rpc_register_client(clnt
);
224 if (!IS_ERR(clnt
->cl_dentry
)) {
225 rpc_rmdir(clnt
->cl_dentry
);
229 rpc_free_iostats(clnt
->cl_metrics
);
231 if (clnt
->cl_server
!= clnt
->cl_inline_name
)
232 kfree(clnt
->cl_server
);
243 * rpc_create - create an RPC client and transport with one call
244 * @args: rpc_clnt create argument structure
246 * Creates and initializes an RPC transport and an RPC client.
248 * It can ping the server in order to determine if it is up, and to see if
249 * it supports this program and version. RPC_CLNT_CREATE_NOPING disables
250 * this behavior so asynchronous tasks can also use rpc_create.
252 struct rpc_clnt
*rpc_create(struct rpc_create_args
*args
)
254 struct rpc_xprt
*xprt
;
255 struct rpc_clnt
*clnt
;
256 struct xprt_create xprtargs
= {
257 .ident
= args
->protocol
,
258 .srcaddr
= args
->saddress
,
259 .dstaddr
= args
->address
,
260 .addrlen
= args
->addrsize
,
264 xprt
= xprt_create_transport(&xprtargs
);
266 return (struct rpc_clnt
*)xprt
;
269 * If the caller chooses not to specify a hostname, whip
270 * up a string representation of the passed-in address.
272 if (args
->servername
== NULL
) {
273 servername
[0] = '\0';
274 switch (args
->address
->sa_family
) {
276 struct sockaddr_in
*sin
=
277 (struct sockaddr_in
*)args
->address
;
278 snprintf(servername
, sizeof(servername
), NIPQUAD_FMT
,
279 NIPQUAD(sin
->sin_addr
.s_addr
));
283 struct sockaddr_in6
*sin
=
284 (struct sockaddr_in6
*)args
->address
;
285 snprintf(servername
, sizeof(servername
), NIP6_FMT
,
286 NIP6(sin
->sin6_addr
));
290 /* caller wants default server name, but
291 * address family isn't recognized. */
292 return ERR_PTR(-EINVAL
);
294 args
->servername
= servername
;
297 xprt
= xprt_create_transport(&xprtargs
);
299 return (struct rpc_clnt
*)xprt
;
302 * By default, kernel RPC client connects from a reserved port.
303 * CAP_NET_BIND_SERVICE will not be set for unprivileged requesters,
304 * but it is always enabled for rpciod, which handles the connect
308 if (args
->flags
& RPC_CLNT_CREATE_NONPRIVPORT
)
311 clnt
= rpc_new_client(args
, xprt
);
315 if (!(args
->flags
& RPC_CLNT_CREATE_NOPING
)) {
316 int err
= rpc_ping(clnt
, RPC_TASK_SOFT
|RPC_TASK_NOINTR
);
318 rpc_shutdown_client(clnt
);
323 clnt
->cl_softrtry
= 1;
324 if (args
->flags
& RPC_CLNT_CREATE_HARDRTRY
)
325 clnt
->cl_softrtry
= 0;
327 if (args
->flags
& RPC_CLNT_CREATE_INTR
)
329 if (args
->flags
& RPC_CLNT_CREATE_AUTOBIND
)
330 clnt
->cl_autobind
= 1;
331 if (args
->flags
& RPC_CLNT_CREATE_DISCRTRY
)
332 clnt
->cl_discrtry
= 1;
336 EXPORT_SYMBOL_GPL(rpc_create
);
339 * This function clones the RPC client structure. It allows us to share the
340 * same transport while varying parameters such as the authentication
344 rpc_clone_client(struct rpc_clnt
*clnt
)
346 struct rpc_clnt
*new;
349 new = kmemdup(clnt
, sizeof(*new), GFP_KERNEL
);
352 new->cl_parent
= clnt
;
353 /* Turn off autobind on clones */
354 new->cl_autobind
= 0;
355 INIT_LIST_HEAD(&new->cl_tasks
);
356 spin_lock_init(&new->cl_lock
);
357 rpc_init_rtt(&new->cl_rtt_default
, clnt
->cl_timeout
->to_initval
);
358 new->cl_metrics
= rpc_alloc_iostats(clnt
);
359 if (new->cl_metrics
== NULL
)
361 kref_init(&new->cl_kref
);
362 err
= rpc_setup_pipedir(new, clnt
->cl_program
->pipe_dir_name
);
366 atomic_inc(&new->cl_auth
->au_count
);
367 xprt_get(clnt
->cl_xprt
);
368 kref_get(&clnt
->cl_kref
);
369 rpc_register_client(new);
373 rpc_free_iostats(new->cl_metrics
);
377 dprintk("RPC: %s: returned error %d\n", __FUNCTION__
, err
);
380 EXPORT_SYMBOL_GPL(rpc_clone_client
);
383 * Properly shut down an RPC client, terminating all outstanding
386 void rpc_shutdown_client(struct rpc_clnt
*clnt
)
388 dprintk("RPC: shutting down %s client for %s\n",
389 clnt
->cl_protname
, clnt
->cl_server
);
391 while (!list_empty(&clnt
->cl_tasks
)) {
392 rpc_killall_tasks(clnt
);
393 wait_event_timeout(destroy_wait
,
394 list_empty(&clnt
->cl_tasks
), 1*HZ
);
397 rpc_release_client(clnt
);
399 EXPORT_SYMBOL_GPL(rpc_shutdown_client
);
405 rpc_free_client(struct kref
*kref
)
407 struct rpc_clnt
*clnt
= container_of(kref
, struct rpc_clnt
, cl_kref
);
409 dprintk("RPC: destroying %s client for %s\n",
410 clnt
->cl_protname
, clnt
->cl_server
);
411 if (!IS_ERR(clnt
->cl_dentry
)) {
412 rpc_rmdir(clnt
->cl_dentry
);
415 if (clnt
->cl_parent
!= clnt
) {
416 rpc_release_client(clnt
->cl_parent
);
419 if (clnt
->cl_server
!= clnt
->cl_inline_name
)
420 kfree(clnt
->cl_server
);
422 rpc_unregister_client(clnt
);
423 rpc_free_iostats(clnt
->cl_metrics
);
424 clnt
->cl_metrics
= NULL
;
425 xprt_put(clnt
->cl_xprt
);
434 rpc_free_auth(struct kref
*kref
)
436 struct rpc_clnt
*clnt
= container_of(kref
, struct rpc_clnt
, cl_kref
);
438 if (clnt
->cl_auth
== NULL
) {
439 rpc_free_client(kref
);
444 * Note: RPCSEC_GSS may need to send NULL RPC calls in order to
445 * release remaining GSS contexts. This mechanism ensures
446 * that it can do so safely.
449 rpcauth_release(clnt
->cl_auth
);
450 clnt
->cl_auth
= NULL
;
451 kref_put(kref
, rpc_free_client
);
455 * Release reference to the RPC client
458 rpc_release_client(struct rpc_clnt
*clnt
)
460 dprintk("RPC: rpc_release_client(%p)\n", clnt
);
462 if (list_empty(&clnt
->cl_tasks
))
463 wake_up(&destroy_wait
);
464 kref_put(&clnt
->cl_kref
, rpc_free_auth
);
468 * rpc_bind_new_program - bind a new RPC program to an existing client
469 * @old - old rpc_client
470 * @program - rpc program to set
471 * @vers - rpc program version
473 * Clones the rpc client and sets up a new RPC program. This is mainly
474 * of use for enabling different RPC programs to share the same transport.
475 * The Sun NFSv2/v3 ACL protocol can do this.
477 struct rpc_clnt
*rpc_bind_new_program(struct rpc_clnt
*old
,
478 struct rpc_program
*program
,
481 struct rpc_clnt
*clnt
;
482 struct rpc_version
*version
;
485 BUG_ON(vers
>= program
->nrvers
|| !program
->version
[vers
]);
486 version
= program
->version
[vers
];
487 clnt
= rpc_clone_client(old
);
490 clnt
->cl_procinfo
= version
->procs
;
491 clnt
->cl_maxproc
= version
->nrprocs
;
492 clnt
->cl_protname
= program
->name
;
493 clnt
->cl_prog
= program
->number
;
494 clnt
->cl_vers
= version
->number
;
495 clnt
->cl_stats
= program
->stats
;
496 err
= rpc_ping(clnt
, RPC_TASK_SOFT
|RPC_TASK_NOINTR
);
498 rpc_shutdown_client(clnt
);
504 EXPORT_SYMBOL_GPL(rpc_bind_new_program
);
507 * Default callback for async RPC calls
510 rpc_default_callback(struct rpc_task
*task
, void *data
)
514 static const struct rpc_call_ops rpc_default_ops
= {
515 .rpc_call_done
= rpc_default_callback
,
519 * Export the signal mask handling for synchronous code that
520 * sleeps on RPC calls
522 #define RPC_INTR_SIGNALS (sigmask(SIGHUP) | sigmask(SIGINT) | sigmask(SIGQUIT) | sigmask(SIGTERM))
524 static void rpc_save_sigmask(sigset_t
*oldset
, int intr
)
526 unsigned long sigallow
= sigmask(SIGKILL
);
529 /* Block all signals except those listed in sigallow */
531 sigallow
|= RPC_INTR_SIGNALS
;
532 siginitsetinv(&sigmask
, sigallow
);
533 sigprocmask(SIG_BLOCK
, &sigmask
, oldset
);
536 static void rpc_task_sigmask(struct rpc_task
*task
, sigset_t
*oldset
)
538 rpc_save_sigmask(oldset
, !RPC_TASK_UNINTERRUPTIBLE(task
));
541 static void rpc_restore_sigmask(sigset_t
*oldset
)
543 sigprocmask(SIG_SETMASK
, oldset
, NULL
);
546 void rpc_clnt_sigmask(struct rpc_clnt
*clnt
, sigset_t
*oldset
)
548 rpc_save_sigmask(oldset
, clnt
->cl_intr
);
550 EXPORT_SYMBOL_GPL(rpc_clnt_sigmask
);
552 void rpc_clnt_sigunmask(struct rpc_clnt
*clnt
, sigset_t
*oldset
)
554 rpc_restore_sigmask(oldset
);
556 EXPORT_SYMBOL_GPL(rpc_clnt_sigunmask
);
559 * rpc_run_task - Allocate a new RPC task, then run rpc_execute against it
560 * @task_setup_data: pointer to task initialisation data
562 struct rpc_task
*rpc_run_task(const struct rpc_task_setup
*task_setup_data
)
564 struct rpc_task
*task
, *ret
;
567 task
= rpc_new_task(task_setup_data
);
569 rpc_release_calldata(task_setup_data
->callback_ops
,
570 task_setup_data
->callback_data
);
571 ret
= ERR_PTR(-ENOMEM
);
575 if (task
->tk_status
!= 0) {
576 ret
= ERR_PTR(task
->tk_status
);
580 atomic_inc(&task
->tk_count
);
581 /* Mask signals on synchronous RPC calls and RPCSEC_GSS upcalls */
582 rpc_task_sigmask(task
, &oldset
);
584 rpc_restore_sigmask(&oldset
);
589 EXPORT_SYMBOL_GPL(rpc_run_task
);
592 * rpc_call_sync - Perform a synchronous RPC call
593 * @clnt: pointer to RPC client
594 * @msg: RPC call parameters
595 * @flags: RPC call flags
597 int rpc_call_sync(struct rpc_clnt
*clnt
, struct rpc_message
*msg
, int flags
)
599 struct rpc_task
*task
;
600 struct rpc_task_setup task_setup_data
= {
603 .callback_ops
= &rpc_default_ops
,
608 BUG_ON(flags
& RPC_TASK_ASYNC
);
610 task
= rpc_run_task(&task_setup_data
);
612 return PTR_ERR(task
);
613 status
= task
->tk_status
;
617 EXPORT_SYMBOL_GPL(rpc_call_sync
);
620 * rpc_call_async - Perform an asynchronous RPC call
621 * @clnt: pointer to RPC client
622 * @msg: RPC call parameters
623 * @flags: RPC call flags
625 * @data: user call data
628 rpc_call_async(struct rpc_clnt
*clnt
, struct rpc_message
*msg
, int flags
,
629 const struct rpc_call_ops
*tk_ops
, void *data
)
631 struct rpc_task
*task
;
632 struct rpc_task_setup task_setup_data
= {
635 .callback_ops
= tk_ops
,
636 .callback_data
= data
,
637 .flags
= flags
|RPC_TASK_ASYNC
,
640 task
= rpc_run_task(&task_setup_data
);
642 return PTR_ERR(task
);
646 EXPORT_SYMBOL_GPL(rpc_call_async
);
649 rpc_call_start(struct rpc_task
*task
)
651 task
->tk_action
= call_start
;
653 EXPORT_SYMBOL_GPL(rpc_call_start
);
656 * rpc_peeraddr - extract remote peer address from clnt's xprt
657 * @clnt: RPC client structure
658 * @buf: target buffer
659 * @size: length of target buffer
661 * Returns the number of bytes that are actually in the stored address.
663 size_t rpc_peeraddr(struct rpc_clnt
*clnt
, struct sockaddr
*buf
, size_t bufsize
)
666 struct rpc_xprt
*xprt
= clnt
->cl_xprt
;
668 bytes
= sizeof(xprt
->addr
);
671 memcpy(buf
, &clnt
->cl_xprt
->addr
, bytes
);
672 return xprt
->addrlen
;
674 EXPORT_SYMBOL_GPL(rpc_peeraddr
);
677 * rpc_peeraddr2str - return remote peer address in printable format
678 * @clnt: RPC client structure
679 * @format: address format
682 const char *rpc_peeraddr2str(struct rpc_clnt
*clnt
,
683 enum rpc_display_format_t format
)
685 struct rpc_xprt
*xprt
= clnt
->cl_xprt
;
687 if (xprt
->address_strings
[format
] != NULL
)
688 return xprt
->address_strings
[format
];
690 return "unprintable";
692 EXPORT_SYMBOL_GPL(rpc_peeraddr2str
);
695 rpc_setbufsize(struct rpc_clnt
*clnt
, unsigned int sndsize
, unsigned int rcvsize
)
697 struct rpc_xprt
*xprt
= clnt
->cl_xprt
;
698 if (xprt
->ops
->set_buffer_size
)
699 xprt
->ops
->set_buffer_size(xprt
, sndsize
, rcvsize
);
701 EXPORT_SYMBOL_GPL(rpc_setbufsize
);
704 * Return size of largest payload RPC client can support, in bytes
706 * For stream transports, this is one RPC record fragment (see RFC
707 * 1831), as we don't support multi-record requests yet. For datagram
708 * transports, this is the size of an IP packet minus the IP, UDP, and
711 size_t rpc_max_payload(struct rpc_clnt
*clnt
)
713 return clnt
->cl_xprt
->max_payload
;
715 EXPORT_SYMBOL_GPL(rpc_max_payload
);
718 * rpc_force_rebind - force transport to check that remote port is unchanged
719 * @clnt: client to rebind
722 void rpc_force_rebind(struct rpc_clnt
*clnt
)
724 if (clnt
->cl_autobind
)
725 xprt_clear_bound(clnt
->cl_xprt
);
727 EXPORT_SYMBOL_GPL(rpc_force_rebind
);
730 * Restart an (async) RPC call. Usually called from within the
734 rpc_restart_call(struct rpc_task
*task
)
736 if (RPC_ASSASSINATED(task
))
739 task
->tk_action
= call_start
;
741 EXPORT_SYMBOL_GPL(rpc_restart_call
);
746 * Other FSM states can be visited zero or more times, but
747 * this state is visited exactly once for each RPC.
750 call_start(struct rpc_task
*task
)
752 struct rpc_clnt
*clnt
= task
->tk_client
;
754 dprintk("RPC: %5u call_start %s%d proc %d (%s)\n", task
->tk_pid
,
755 clnt
->cl_protname
, clnt
->cl_vers
,
756 task
->tk_msg
.rpc_proc
->p_proc
,
757 (RPC_IS_ASYNC(task
) ? "async" : "sync"));
759 /* Increment call count */
760 task
->tk_msg
.rpc_proc
->p_count
++;
761 clnt
->cl_stats
->rpccnt
++;
762 task
->tk_action
= call_reserve
;
766 * 1. Reserve an RPC call slot
769 call_reserve(struct rpc_task
*task
)
773 if (!rpcauth_uptodatecred(task
)) {
774 task
->tk_action
= call_refresh
;
779 task
->tk_action
= call_reserveresult
;
784 * 1b. Grok the result of xprt_reserve()
787 call_reserveresult(struct rpc_task
*task
)
789 int status
= task
->tk_status
;
794 * After a call to xprt_reserve(), we must have either
795 * a request slot or else an error status.
799 if (task
->tk_rqstp
) {
800 task
->tk_action
= call_allocate
;
804 printk(KERN_ERR
"%s: status=%d, but no request slot, exiting\n",
805 __FUNCTION__
, status
);
806 rpc_exit(task
, -EIO
);
811 * Even though there was an error, we may have acquired
812 * a request slot somehow. Make sure not to leak it.
814 if (task
->tk_rqstp
) {
815 printk(KERN_ERR
"%s: status=%d, request allocated anyway\n",
816 __FUNCTION__
, status
);
821 case -EAGAIN
: /* woken up; retry */
822 task
->tk_action
= call_reserve
;
824 case -EIO
: /* probably a shutdown */
827 printk(KERN_ERR
"%s: unrecognized error %d, exiting\n",
828 __FUNCTION__
, status
);
831 rpc_exit(task
, status
);
835 * 2. Allocate the buffer. For details, see sched.c:rpc_malloc.
836 * (Note: buffer memory is freed in xprt_release).
839 call_allocate(struct rpc_task
*task
)
841 unsigned int slack
= task
->tk_msg
.rpc_cred
->cr_auth
->au_cslack
;
842 struct rpc_rqst
*req
= task
->tk_rqstp
;
843 struct rpc_xprt
*xprt
= task
->tk_xprt
;
844 struct rpc_procinfo
*proc
= task
->tk_msg
.rpc_proc
;
849 task
->tk_action
= call_bind
;
854 if (proc
->p_proc
!= 0) {
855 BUG_ON(proc
->p_arglen
== 0);
856 if (proc
->p_decode
!= NULL
)
857 BUG_ON(proc
->p_replen
== 0);
861 * Calculate the size (in quads) of the RPC call
862 * and reply headers, and convert both values
865 req
->rq_callsize
= RPC_CALLHDRSIZE
+ (slack
<< 1) + proc
->p_arglen
;
866 req
->rq_callsize
<<= 2;
867 req
->rq_rcvsize
= RPC_REPHDRSIZE
+ slack
+ proc
->p_replen
;
868 req
->rq_rcvsize
<<= 2;
870 req
->rq_buffer
= xprt
->ops
->buf_alloc(task
,
871 req
->rq_callsize
+ req
->rq_rcvsize
);
872 if (req
->rq_buffer
!= NULL
)
875 dprintk("RPC: %5u rpc_buffer allocation failed\n", task
->tk_pid
);
877 if (RPC_IS_ASYNC(task
) || !signalled()) {
878 task
->tk_action
= call_allocate
;
879 rpc_delay(task
, HZ
>>4);
883 rpc_exit(task
, -ERESTARTSYS
);
887 rpc_task_need_encode(struct rpc_task
*task
)
889 return task
->tk_rqstp
->rq_snd_buf
.len
== 0;
893 rpc_task_force_reencode(struct rpc_task
*task
)
895 task
->tk_rqstp
->rq_snd_buf
.len
= 0;
899 rpc_xdr_buf_init(struct xdr_buf
*buf
, void *start
, size_t len
)
901 buf
->head
[0].iov_base
= start
;
902 buf
->head
[0].iov_len
= len
;
903 buf
->tail
[0].iov_len
= 0;
911 * 3. Encode arguments of an RPC call
914 call_encode(struct rpc_task
*task
)
916 struct rpc_rqst
*req
= task
->tk_rqstp
;
922 rpc_xdr_buf_init(&req
->rq_snd_buf
,
925 rpc_xdr_buf_init(&req
->rq_rcv_buf
,
926 (char *)req
->rq_buffer
+ req
->rq_callsize
,
929 /* Encode header and provided arguments */
930 encode
= task
->tk_msg
.rpc_proc
->p_encode
;
931 if (!(p
= call_header(task
))) {
932 printk(KERN_INFO
"RPC: call_header failed, exit EIO\n");
933 rpc_exit(task
, -EIO
);
939 task
->tk_status
= rpcauth_wrap_req(task
, encode
, req
, p
,
940 task
->tk_msg
.rpc_argp
);
941 if (task
->tk_status
== -ENOMEM
) {
942 /* XXX: Is this sane? */
943 rpc_delay(task
, 3*HZ
);
944 task
->tk_status
= -EAGAIN
;
949 * 4. Get the server port number if not yet set
952 call_bind(struct rpc_task
*task
)
954 struct rpc_xprt
*xprt
= task
->tk_xprt
;
958 task
->tk_action
= call_connect
;
959 if (!xprt_bound(xprt
)) {
960 task
->tk_action
= call_bind_status
;
961 task
->tk_timeout
= xprt
->bind_timeout
;
962 xprt
->ops
->rpcbind(task
);
967 * 4a. Sort out bind result
970 call_bind_status(struct rpc_task
*task
)
974 if (task
->tk_status
>= 0) {
977 task
->tk_action
= call_connect
;
981 switch (task
->tk_status
) {
983 dprintk("RPC: %5u rpcbind waiting for another request "
984 "to finish\n", task
->tk_pid
);
985 /* avoid busy-waiting here -- could be a network outage. */
986 rpc_delay(task
, 5*HZ
);
989 dprintk("RPC: %5u remote rpcbind: RPC program/version "
990 "unavailable\n", task
->tk_pid
);
991 /* fail immediately if this is an RPC ping */
992 if (task
->tk_msg
.rpc_proc
->p_proc
== 0) {
993 status
= -EOPNOTSUPP
;
996 rpc_delay(task
, 3*HZ
);
999 dprintk("RPC: %5u rpcbind request timed out\n",
1003 /* server doesn't support any rpcbind version we know of */
1004 dprintk("RPC: %5u remote rpcbind service unavailable\n",
1007 case -EPROTONOSUPPORT
:
1008 dprintk("RPC: %5u remote rpcbind version unavailable, retrying\n",
1010 task
->tk_status
= 0;
1011 task
->tk_action
= call_bind
;
1014 dprintk("RPC: %5u unrecognized rpcbind error (%d)\n",
1015 task
->tk_pid
, -task
->tk_status
);
1018 rpc_exit(task
, status
);
1022 task
->tk_action
= call_timeout
;
1026 * 4b. Connect to the RPC server
1029 call_connect(struct rpc_task
*task
)
1031 struct rpc_xprt
*xprt
= task
->tk_xprt
;
1033 dprintk("RPC: %5u call_connect xprt %p %s connected\n",
1035 (xprt_connected(xprt
) ? "is" : "is not"));
1037 task
->tk_action
= call_transmit
;
1038 if (!xprt_connected(xprt
)) {
1039 task
->tk_action
= call_connect_status
;
1040 if (task
->tk_status
< 0)
1047 * 4c. Sort out connect result
1050 call_connect_status(struct rpc_task
*task
)
1052 struct rpc_clnt
*clnt
= task
->tk_client
;
1053 int status
= task
->tk_status
;
1055 dprint_status(task
);
1057 task
->tk_status
= 0;
1059 clnt
->cl_stats
->netreconn
++;
1060 task
->tk_action
= call_transmit
;
1064 /* Something failed: remote service port may have changed */
1065 rpc_force_rebind(clnt
);
1070 task
->tk_action
= call_bind
;
1071 if (!RPC_IS_SOFT(task
))
1073 /* if soft mounted, test if we've timed out */
1075 task
->tk_action
= call_timeout
;
1078 rpc_exit(task
, -EIO
);
1082 * 5. Transmit the RPC request, and wait for reply
1085 call_transmit(struct rpc_task
*task
)
1087 dprint_status(task
);
1089 task
->tk_action
= call_status
;
1090 if (task
->tk_status
< 0)
1092 task
->tk_status
= xprt_prepare_transmit(task
);
1093 if (task
->tk_status
!= 0)
1095 task
->tk_action
= call_transmit_status
;
1096 /* Encode here so that rpcsec_gss can use correct sequence number. */
1097 if (rpc_task_need_encode(task
)) {
1098 BUG_ON(task
->tk_rqstp
->rq_bytes_sent
!= 0);
1100 /* Did the encode result in an error condition? */
1101 if (task
->tk_status
!= 0)
1104 xprt_transmit(task
);
1105 if (task
->tk_status
< 0)
1108 * On success, ensure that we call xprt_end_transmit() before sleeping
1109 * in order to allow access to the socket to other RPC requests.
1111 call_transmit_status(task
);
1112 if (task
->tk_msg
.rpc_proc
->p_decode
!= NULL
)
1114 task
->tk_action
= rpc_exit_task
;
1115 rpc_wake_up_task(task
);
1119 * 5a. Handle cleanup after a transmission
1122 call_transmit_status(struct rpc_task
*task
)
1124 task
->tk_action
= call_status
;
1126 * Special case: if we've been waiting on the socket's write_space()
1127 * callback, then don't call xprt_end_transmit().
1129 if (task
->tk_status
== -EAGAIN
)
1131 xprt_end_transmit(task
);
1132 rpc_task_force_reencode(task
);
1136 * 6. Sort out the RPC call status
1139 call_status(struct rpc_task
*task
)
1141 struct rpc_clnt
*clnt
= task
->tk_client
;
1142 struct rpc_rqst
*req
= task
->tk_rqstp
;
1145 if (req
->rq_received
> 0 && !req
->rq_bytes_sent
)
1146 task
->tk_status
= req
->rq_received
;
1148 dprint_status(task
);
1150 status
= task
->tk_status
;
1152 task
->tk_action
= call_decode
;
1156 task
->tk_status
= 0;
1162 * Delay any retries for 3 seconds, then handle as if it
1165 rpc_delay(task
, 3*HZ
);
1167 task
->tk_action
= call_timeout
;
1168 if (task
->tk_client
->cl_discrtry
)
1169 xprt_force_disconnect(task
->tk_xprt
);
1173 rpc_force_rebind(clnt
);
1174 task
->tk_action
= call_bind
;
1177 task
->tk_action
= call_transmit
;
1180 /* shutdown or soft timeout */
1181 rpc_exit(task
, status
);
1184 printk("%s: RPC call returned error %d\n",
1185 clnt
->cl_protname
, -status
);
1186 rpc_exit(task
, status
);
1191 * 6a. Handle RPC timeout
1192 * We do not release the request slot, so we keep using the
1193 * same XID for all retransmits.
1196 call_timeout(struct rpc_task
*task
)
1198 struct rpc_clnt
*clnt
= task
->tk_client
;
1200 if (xprt_adjust_timeout(task
->tk_rqstp
) == 0) {
1201 dprintk("RPC: %5u call_timeout (minor)\n", task
->tk_pid
);
1205 dprintk("RPC: %5u call_timeout (major)\n", task
->tk_pid
);
1206 task
->tk_timeouts
++;
1208 if (RPC_IS_SOFT(task
)) {
1209 printk(KERN_NOTICE
"%s: server %s not responding, timed out\n",
1210 clnt
->cl_protname
, clnt
->cl_server
);
1211 rpc_exit(task
, -EIO
);
1215 if (!(task
->tk_flags
& RPC_CALL_MAJORSEEN
)) {
1216 task
->tk_flags
|= RPC_CALL_MAJORSEEN
;
1217 printk(KERN_NOTICE
"%s: server %s not responding, still trying\n",
1218 clnt
->cl_protname
, clnt
->cl_server
);
1220 rpc_force_rebind(clnt
);
1223 clnt
->cl_stats
->rpcretrans
++;
1224 task
->tk_action
= call_bind
;
1225 task
->tk_status
= 0;
1229 * 7. Decode the RPC reply
1232 call_decode(struct rpc_task
*task
)
1234 struct rpc_clnt
*clnt
= task
->tk_client
;
1235 struct rpc_rqst
*req
= task
->tk_rqstp
;
1236 kxdrproc_t decode
= task
->tk_msg
.rpc_proc
->p_decode
;
1239 dprintk("RPC: %5u call_decode (status %d)\n",
1240 task
->tk_pid
, task
->tk_status
);
1242 if (task
->tk_flags
& RPC_CALL_MAJORSEEN
) {
1243 printk(KERN_NOTICE
"%s: server %s OK\n",
1244 clnt
->cl_protname
, clnt
->cl_server
);
1245 task
->tk_flags
&= ~RPC_CALL_MAJORSEEN
;
1248 if (task
->tk_status
< 12) {
1249 if (!RPC_IS_SOFT(task
)) {
1250 task
->tk_action
= call_bind
;
1251 clnt
->cl_stats
->rpcretrans
++;
1254 dprintk("RPC: %s: too small RPC reply size (%d bytes)\n",
1255 clnt
->cl_protname
, task
->tk_status
);
1256 task
->tk_action
= call_timeout
;
1261 * Ensure that we see all writes made by xprt_complete_rqst()
1262 * before it changed req->rq_received.
1265 req
->rq_rcv_buf
.len
= req
->rq_private_buf
.len
;
1267 /* Check that the softirq receive buffer is valid */
1268 WARN_ON(memcmp(&req
->rq_rcv_buf
, &req
->rq_private_buf
,
1269 sizeof(req
->rq_rcv_buf
)) != 0);
1271 /* Verify the RPC header */
1272 p
= call_verify(task
);
1274 if (p
== ERR_PTR(-EAGAIN
))
1279 task
->tk_action
= rpc_exit_task
;
1282 task
->tk_status
= rpcauth_unwrap_resp(task
, decode
, req
, p
,
1283 task
->tk_msg
.rpc_resp
);
1285 dprintk("RPC: %5u call_decode result %d\n", task
->tk_pid
,
1289 req
->rq_received
= req
->rq_private_buf
.len
= 0;
1290 task
->tk_status
= 0;
1291 if (task
->tk_client
->cl_discrtry
)
1292 xprt_force_disconnect(task
->tk_xprt
);
1296 * 8. Refresh the credentials if rejected by the server
1299 call_refresh(struct rpc_task
*task
)
1301 dprint_status(task
);
1303 task
->tk_action
= call_refreshresult
;
1304 task
->tk_status
= 0;
1305 task
->tk_client
->cl_stats
->rpcauthrefresh
++;
1306 rpcauth_refreshcred(task
);
1310 * 8a. Process the results of a credential refresh
1313 call_refreshresult(struct rpc_task
*task
)
1315 int status
= task
->tk_status
;
1317 dprint_status(task
);
1319 task
->tk_status
= 0;
1320 task
->tk_action
= call_reserve
;
1321 if (status
>= 0 && rpcauth_uptodatecred(task
))
1323 if (status
== -EACCES
) {
1324 rpc_exit(task
, -EACCES
);
1327 task
->tk_action
= call_refresh
;
1328 if (status
!= -ETIMEDOUT
)
1329 rpc_delay(task
, 3*HZ
);
1334 * Call header serialization
1337 call_header(struct rpc_task
*task
)
1339 struct rpc_clnt
*clnt
= task
->tk_client
;
1340 struct rpc_rqst
*req
= task
->tk_rqstp
;
1341 __be32
*p
= req
->rq_svec
[0].iov_base
;
1343 /* FIXME: check buffer size? */
1345 p
= xprt_skip_transport_header(task
->tk_xprt
, p
);
1346 *p
++ = req
->rq_xid
; /* XID */
1347 *p
++ = htonl(RPC_CALL
); /* CALL */
1348 *p
++ = htonl(RPC_VERSION
); /* RPC version */
1349 *p
++ = htonl(clnt
->cl_prog
); /* program number */
1350 *p
++ = htonl(clnt
->cl_vers
); /* program version */
1351 *p
++ = htonl(task
->tk_msg
.rpc_proc
->p_proc
); /* procedure */
1352 p
= rpcauth_marshcred(task
, p
);
1353 req
->rq_slen
= xdr_adjust_iovec(&req
->rq_svec
[0], p
);
1358 * Reply header verification
1361 call_verify(struct rpc_task
*task
)
1363 struct kvec
*iov
= &task
->tk_rqstp
->rq_rcv_buf
.head
[0];
1364 int len
= task
->tk_rqstp
->rq_rcv_buf
.len
>> 2;
1365 __be32
*p
= iov
->iov_base
;
1367 int error
= -EACCES
;
1369 if ((task
->tk_rqstp
->rq_rcv_buf
.len
& 3) != 0) {
1370 /* RFC-1014 says that the representation of XDR data must be a
1371 * multiple of four bytes
1372 * - if it isn't pointer subtraction in the NFS client may give
1375 dprintk("RPC: %5u %s: XDR representation not a multiple of"
1376 " 4 bytes: 0x%x\n", task
->tk_pid
, __FUNCTION__
,
1377 task
->tk_rqstp
->rq_rcv_buf
.len
);
1382 p
+= 1; /* skip XID */
1384 if ((n
= ntohl(*p
++)) != RPC_REPLY
) {
1385 dprintk("RPC: %5u %s: not an RPC reply: %x\n",
1386 task
->tk_pid
, __FUNCTION__
, n
);
1389 if ((n
= ntohl(*p
++)) != RPC_MSG_ACCEPTED
) {
1392 switch ((n
= ntohl(*p
++))) {
1393 case RPC_AUTH_ERROR
:
1396 dprintk("RPC: %5u %s: RPC call version "
1398 task
->tk_pid
, __FUNCTION__
);
1399 error
= -EPROTONOSUPPORT
;
1402 dprintk("RPC: %5u %s: RPC call rejected, "
1403 "unknown error: %x\n",
1404 task
->tk_pid
, __FUNCTION__
, n
);
1409 switch ((n
= ntohl(*p
++))) {
1410 case RPC_AUTH_REJECTEDCRED
:
1411 case RPC_AUTH_REJECTEDVERF
:
1412 case RPCSEC_GSS_CREDPROBLEM
:
1413 case RPCSEC_GSS_CTXPROBLEM
:
1414 if (!task
->tk_cred_retry
)
1416 task
->tk_cred_retry
--;
1417 dprintk("RPC: %5u %s: retry stale creds\n",
1418 task
->tk_pid
, __FUNCTION__
);
1419 rpcauth_invalcred(task
);
1420 /* Ensure we obtain a new XID! */
1422 task
->tk_action
= call_refresh
;
1424 case RPC_AUTH_BADCRED
:
1425 case RPC_AUTH_BADVERF
:
1426 /* possibly garbled cred/verf? */
1427 if (!task
->tk_garb_retry
)
1429 task
->tk_garb_retry
--;
1430 dprintk("RPC: %5u %s: retry garbled creds\n",
1431 task
->tk_pid
, __FUNCTION__
);
1432 task
->tk_action
= call_bind
;
1434 case RPC_AUTH_TOOWEAK
:
1435 printk(KERN_NOTICE
"call_verify: server %s requires stronger "
1436 "authentication.\n", task
->tk_client
->cl_server
);
1439 dprintk("RPC: %5u %s: unknown auth error: %x\n",
1440 task
->tk_pid
, __FUNCTION__
, n
);
1443 dprintk("RPC: %5u %s: call rejected %d\n",
1444 task
->tk_pid
, __FUNCTION__
, n
);
1447 if (!(p
= rpcauth_checkverf(task
, p
))) {
1448 dprintk("RPC: %5u %s: auth check failed\n",
1449 task
->tk_pid
, __FUNCTION__
);
1450 goto out_garbage
; /* bad verifier, retry */
1452 len
= p
- (__be32
*)iov
->iov_base
- 1;
1455 switch ((n
= ntohl(*p
++))) {
1458 case RPC_PROG_UNAVAIL
:
1459 dprintk("RPC: %5u %s: program %u is unsupported by server %s\n",
1460 task
->tk_pid
, __FUNCTION__
,
1461 (unsigned int)task
->tk_client
->cl_prog
,
1462 task
->tk_client
->cl_server
);
1463 error
= -EPFNOSUPPORT
;
1465 case RPC_PROG_MISMATCH
:
1466 dprintk("RPC: %5u %s: program %u, version %u unsupported by "
1467 "server %s\n", task
->tk_pid
, __FUNCTION__
,
1468 (unsigned int)task
->tk_client
->cl_prog
,
1469 (unsigned int)task
->tk_client
->cl_vers
,
1470 task
->tk_client
->cl_server
);
1471 error
= -EPROTONOSUPPORT
;
1473 case RPC_PROC_UNAVAIL
:
1474 dprintk("RPC: %5u %s: proc %p unsupported by program %u, "
1475 "version %u on server %s\n",
1476 task
->tk_pid
, __FUNCTION__
,
1477 task
->tk_msg
.rpc_proc
,
1478 task
->tk_client
->cl_prog
,
1479 task
->tk_client
->cl_vers
,
1480 task
->tk_client
->cl_server
);
1481 error
= -EOPNOTSUPP
;
1483 case RPC_GARBAGE_ARGS
:
1484 dprintk("RPC: %5u %s: server saw garbage\n",
1485 task
->tk_pid
, __FUNCTION__
);
1488 dprintk("RPC: %5u %s: server accept status: %x\n",
1489 task
->tk_pid
, __FUNCTION__
, n
);
1494 task
->tk_client
->cl_stats
->rpcgarbage
++;
1495 if (task
->tk_garb_retry
) {
1496 task
->tk_garb_retry
--;
1497 dprintk("RPC: %5u %s: retrying\n",
1498 task
->tk_pid
, __FUNCTION__
);
1499 task
->tk_action
= call_bind
;
1501 return ERR_PTR(-EAGAIN
);
1506 rpc_exit(task
, error
);
1507 dprintk("RPC: %5u %s: call failed with error %d\n", task
->tk_pid
,
1508 __FUNCTION__
, error
);
1509 return ERR_PTR(error
);
1511 dprintk("RPC: %5u %s: server reply was truncated.\n", task
->tk_pid
,
1516 static int rpcproc_encode_null(void *rqstp
, __be32
*data
, void *obj
)
1521 static int rpcproc_decode_null(void *rqstp
, __be32
*data
, void *obj
)
1526 static struct rpc_procinfo rpcproc_null
= {
1527 .p_encode
= rpcproc_encode_null
,
1528 .p_decode
= rpcproc_decode_null
,
1531 static int rpc_ping(struct rpc_clnt
*clnt
, int flags
)
1533 struct rpc_message msg
= {
1534 .rpc_proc
= &rpcproc_null
,
1537 msg
.rpc_cred
= authnull_ops
.lookup_cred(NULL
, NULL
, 0);
1538 err
= rpc_call_sync(clnt
, &msg
, flags
);
1539 put_rpccred(msg
.rpc_cred
);
1543 struct rpc_task
*rpc_call_null(struct rpc_clnt
*clnt
, struct rpc_cred
*cred
, int flags
)
1545 struct rpc_message msg
= {
1546 .rpc_proc
= &rpcproc_null
,
1549 struct rpc_task_setup task_setup_data
= {
1551 .rpc_message
= &msg
,
1552 .callback_ops
= &rpc_default_ops
,
1555 return rpc_run_task(&task_setup_data
);
1557 EXPORT_SYMBOL_GPL(rpc_call_null
);
1560 void rpc_show_tasks(void)
1562 struct rpc_clnt
*clnt
;
1565 spin_lock(&rpc_client_lock
);
1566 if (list_empty(&all_clients
))
1568 printk("-pid- proc flgs status -client- -prog- --rqstp- -timeout "
1569 "-rpcwait -action- ---ops--\n");
1570 list_for_each_entry(clnt
, &all_clients
, cl_clients
) {
1571 if (list_empty(&clnt
->cl_tasks
))
1573 spin_lock(&clnt
->cl_lock
);
1574 list_for_each_entry(t
, &clnt
->cl_tasks
, tk_task
) {
1575 const char *rpc_waitq
= "none";
1578 if (t
->tk_msg
.rpc_proc
)
1579 proc
= t
->tk_msg
.rpc_proc
->p_proc
;
1583 if (RPC_IS_QUEUED(t
))
1584 rpc_waitq
= rpc_qname(t
->u
.tk_wait
.rpc_waitq
);
1586 printk("%5u %04d %04x %6d %8p %6d %8p %8ld %8s %8p %8p\n",
1588 t
->tk_flags
, t
->tk_status
,
1590 (t
->tk_client
? t
->tk_client
->cl_prog
: 0),
1591 t
->tk_rqstp
, t
->tk_timeout
,
1593 t
->tk_action
, t
->tk_ops
);
1595 spin_unlock(&clnt
->cl_lock
);
1598 spin_unlock(&rpc_client_lock
);