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);
145 if (!xprt_bound(clnt->cl_xprt))
146 clnt->cl_autobind = 1;
148 clnt->cl_rtt = &clnt->cl_rtt_default;
149 rpc_init_rtt(&clnt->cl_rtt_default, xprt->timeout.to_initval);
151 err = rpc_setup_pipedir(clnt, program->pipe_dir_name);
155 auth = rpcauth_create(flavor, clnt);
157 printk(KERN_INFO "RPC: Couldn't create auth handle (flavor %u)\n",
163 /* save the nodename */
164 clnt->cl_nodelen = strlen(system_utsname.nodename);
165 if (clnt->cl_nodelen > UNX_MAXNODENAME)
166 clnt->cl_nodelen = UNX_MAXNODENAME;
167 memcpy(clnt->cl_nodename, system_utsname.nodename, clnt->cl_nodelen);
171 if (!IS_ERR(clnt->cl_dentry)) {
172 rpc_rmdir(clnt->cl_dentry);
176 if (clnt->cl_server != clnt->cl_inline_name)
177 kfree(clnt->cl_server);
186 * rpc_create - create an RPC client and transport with one call
187 * @args: rpc_clnt create argument structure
189 * Creates and initializes an RPC transport and an RPC client.
191 * It can ping the server in order to determine if it is up, and to see if
192 * it supports this program and version. RPC_CLNT_CREATE_NOPING disables
193 * this behavior so asynchronous tasks can also use rpc_create.
195 struct rpc_clnt *rpc_create(struct rpc_create_args *args)
197 struct rpc_xprt *xprt;
198 struct rpc_clnt *clnt;
200 xprt = xprt_create_transport(args->protocol, args->address,
201 args->addrsize, args->timeout);
203 return (struct rpc_clnt *)xprt;
206 * By default, kernel RPC client connects from a reserved port.
207 * CAP_NET_BIND_SERVICE will not be set for unprivileged requesters,
208 * but it is always enabled for rpciod, which handles the connect
212 if (args->flags & RPC_CLNT_CREATE_NONPRIVPORT)
215 dprintk("RPC: creating %s client for %s (xprt %p)\n",
216 args->program->name, args->servername, xprt);
218 clnt = rpc_new_client(xprt, args->servername, args->program,
219 args->version, args->authflavor);
223 if (!(args->flags & RPC_CLNT_CREATE_NOPING)) {
224 int err = rpc_ping(clnt, RPC_TASK_SOFT|RPC_TASK_NOINTR);
226 rpc_shutdown_client(clnt);
231 clnt->cl_softrtry = 1;
232 if (args->flags & RPC_CLNT_CREATE_HARDRTRY)
233 clnt->cl_softrtry = 0;
235 if (args->flags & RPC_CLNT_CREATE_INTR)
237 if (args->flags & RPC_CLNT_CREATE_AUTOBIND)
238 clnt->cl_autobind = 1;
239 if (args->flags & RPC_CLNT_CREATE_ONESHOT)
240 clnt->cl_oneshot = 1;
244 EXPORT_SYMBOL_GPL(rpc_create);
247 * This function clones the RPC client structure. It allows us to share the
248 * same transport while varying parameters such as the authentication
252 rpc_clone_client(struct rpc_clnt *clnt)
254 struct rpc_clnt *new;
256 new = kmalloc(sizeof(*new), GFP_KERNEL);
259 memcpy(new, clnt, sizeof(*new));
260 atomic_set(&new->cl_count, 1);
261 atomic_set(&new->cl_users, 0);
262 new->cl_parent = clnt;
263 atomic_inc(&clnt->cl_count);
264 new->cl_xprt = xprt_get(clnt->cl_xprt);
265 /* Turn off autobind on clones */
266 new->cl_autobind = 0;
269 if (!IS_ERR(new->cl_dentry))
270 dget(new->cl_dentry);
271 rpc_init_rtt(&new->cl_rtt_default, clnt->cl_xprt->timeout.to_initval);
273 atomic_inc(&new->cl_auth->au_count);
274 new->cl_metrics = rpc_alloc_iostats(clnt);
277 printk(KERN_INFO "RPC: out of memory in %s\n", __FUNCTION__);
278 return ERR_PTR(-ENOMEM);
282 * Properly shut down an RPC client, terminating all outstanding
283 * requests. Note that we must be certain that cl_oneshot and
284 * cl_dead are cleared, or else the client would be destroyed
285 * when the last task releases it.
288 rpc_shutdown_client(struct rpc_clnt *clnt)
290 dprintk("RPC: shutting down %s client for %s, tasks=%d\n",
291 clnt->cl_protname, clnt->cl_server,
292 atomic_read(&clnt->cl_users));
294 while (atomic_read(&clnt->cl_users) > 0) {
295 /* Don't let rpc_release_client destroy us */
296 clnt->cl_oneshot = 0;
298 rpc_killall_tasks(clnt);
299 wait_event_timeout(destroy_wait,
300 !atomic_read(&clnt->cl_users), 1*HZ);
303 if (atomic_read(&clnt->cl_users) < 0) {
304 printk(KERN_ERR "RPC: rpc_shutdown_client clnt %p tasks=%d\n",
305 clnt, atomic_read(&clnt->cl_users));
312 return rpc_destroy_client(clnt);
316 * Delete an RPC client
319 rpc_destroy_client(struct rpc_clnt *clnt)
321 if (!atomic_dec_and_test(&clnt->cl_count))
323 BUG_ON(atomic_read(&clnt->cl_users) != 0);
325 dprintk("RPC: destroying %s client for %s\n",
326 clnt->cl_protname, clnt->cl_server);
328 rpcauth_destroy(clnt->cl_auth);
329 clnt->cl_auth = NULL;
331 if (clnt->cl_parent != clnt) {
332 if (!IS_ERR(clnt->cl_dentry))
333 dput(clnt->cl_dentry);
334 rpc_destroy_client(clnt->cl_parent);
337 if (!IS_ERR(clnt->cl_dentry)) {
338 rpc_rmdir(clnt->cl_dentry);
341 if (clnt->cl_server != clnt->cl_inline_name)
342 kfree(clnt->cl_server);
344 rpc_free_iostats(clnt->cl_metrics);
345 clnt->cl_metrics = NULL;
346 xprt_put(clnt->cl_xprt);
352 * Release an RPC client
355 rpc_release_client(struct rpc_clnt *clnt)
357 dprintk("RPC: rpc_release_client(%p, %d)\n",
358 clnt, atomic_read(&clnt->cl_users));
360 if (!atomic_dec_and_test(&clnt->cl_users))
362 wake_up(&destroy_wait);
363 if (clnt->cl_oneshot || clnt->cl_dead)
364 rpc_destroy_client(clnt);
368 * rpc_bind_new_program - bind a new RPC program to an existing client
369 * @old - old rpc_client
370 * @program - rpc program to set
371 * @vers - rpc program version
373 * Clones the rpc client and sets up a new RPC program. This is mainly
374 * of use for enabling different RPC programs to share the same transport.
375 * The Sun NFSv2/v3 ACL protocol can do this.
377 struct rpc_clnt *rpc_bind_new_program(struct rpc_clnt *old,
378 struct rpc_program *program,
381 struct rpc_clnt *clnt;
382 struct rpc_version *version;
385 BUG_ON(vers >= program->nrvers || !program->version[vers]);
386 version = program->version[vers];
387 clnt = rpc_clone_client(old);
390 clnt->cl_procinfo = version->procs;
391 clnt->cl_maxproc = version->nrprocs;
392 clnt->cl_protname = program->name;
393 clnt->cl_prog = program->number;
394 clnt->cl_vers = version->number;
395 clnt->cl_stats = program->stats;
396 err = rpc_ping(clnt, RPC_TASK_SOFT|RPC_TASK_NOINTR);
398 rpc_shutdown_client(clnt);
406 * Default callback for async RPC calls
409 rpc_default_callback(struct rpc_task *task, void *data)
413 static const struct rpc_call_ops rpc_default_ops = {
414 .rpc_call_done = rpc_default_callback,
418 * Export the signal mask handling for synchronous code that
419 * sleeps on RPC calls
421 #define RPC_INTR_SIGNALS (sigmask(SIGHUP) | sigmask(SIGINT) | sigmask(SIGQUIT) | sigmask(SIGTERM))
423 static void rpc_save_sigmask(sigset_t *oldset, int intr)
425 unsigned long sigallow = sigmask(SIGKILL);
428 /* Block all signals except those listed in sigallow */
430 sigallow |= RPC_INTR_SIGNALS;
431 siginitsetinv(&sigmask, sigallow);
432 sigprocmask(SIG_BLOCK, &sigmask, oldset);
435 static inline void rpc_task_sigmask(struct rpc_task *task, sigset_t *oldset)
437 rpc_save_sigmask(oldset, !RPC_TASK_UNINTERRUPTIBLE(task));
440 static inline void rpc_restore_sigmask(sigset_t *oldset)
442 sigprocmask(SIG_SETMASK, oldset, NULL);
445 void rpc_clnt_sigmask(struct rpc_clnt *clnt, sigset_t *oldset)
447 rpc_save_sigmask(oldset, clnt->cl_intr);
450 void rpc_clnt_sigunmask(struct rpc_clnt *clnt, sigset_t *oldset)
452 rpc_restore_sigmask(oldset);
456 * New rpc_call implementation
458 int rpc_call_sync(struct rpc_clnt *clnt, struct rpc_message *msg, int flags)
460 struct rpc_task *task;
464 /* If this client is slain all further I/O fails */
468 BUG_ON(flags & RPC_TASK_ASYNC);
471 task = rpc_new_task(clnt, flags, &rpc_default_ops, NULL);
475 /* Mask signals on RPC calls _and_ GSS_AUTH upcalls */
476 rpc_task_sigmask(task, &oldset);
478 rpc_call_setup(task, msg, 0);
480 /* Set up the call info struct and execute the task */
481 status = task->tk_status;
483 atomic_inc(&task->tk_count);
484 status = rpc_execute(task);
486 status = task->tk_status;
488 rpc_restore_sigmask(&oldset);
489 rpc_release_task(task);
495 * New rpc_call implementation
498 rpc_call_async(struct rpc_clnt *clnt, struct rpc_message *msg, int flags,
499 const struct rpc_call_ops *tk_ops, void *data)
501 struct rpc_task *task;
505 /* If this client is slain all further I/O fails */
510 flags |= RPC_TASK_ASYNC;
512 /* Create/initialize a new RPC task */
514 if (!(task = rpc_new_task(clnt, flags, tk_ops, data)))
517 /* Mask signals on GSS_AUTH upcalls */
518 rpc_task_sigmask(task, &oldset);
520 rpc_call_setup(task, msg, 0);
522 /* Set up the call info struct and execute the task */
523 status = task->tk_status;
527 rpc_release_task(task);
529 rpc_restore_sigmask(&oldset);
532 if (tk_ops->rpc_release != NULL)
533 tk_ops->rpc_release(data);
539 rpc_call_setup(struct rpc_task *task, struct rpc_message *msg, int flags)
542 task->tk_flags |= flags;
543 /* Bind the user cred */
544 if (task->tk_msg.rpc_cred != NULL)
545 rpcauth_holdcred(task);
547 rpcauth_bindcred(task);
549 if (task->tk_status == 0)
550 task->tk_action = call_start;
552 task->tk_action = rpc_exit_task;
556 * rpc_peeraddr - extract remote peer address from clnt's xprt
557 * @clnt: RPC client structure
558 * @buf: target buffer
559 * @size: length of target buffer
561 * Returns the number of bytes that are actually in the stored address.
563 size_t rpc_peeraddr(struct rpc_clnt *clnt, struct sockaddr *buf, size_t bufsize)
566 struct rpc_xprt *xprt = clnt->cl_xprt;
568 bytes = sizeof(xprt->addr);
571 memcpy(buf, &clnt->cl_xprt->addr, bytes);
572 return xprt->addrlen;
574 EXPORT_SYMBOL_GPL(rpc_peeraddr);
577 * rpc_peeraddr2str - return remote peer address in printable format
578 * @clnt: RPC client structure
579 * @format: address format
582 char *rpc_peeraddr2str(struct rpc_clnt *clnt, enum rpc_display_format_t format)
584 struct rpc_xprt *xprt = clnt->cl_xprt;
585 return xprt->ops->print_addr(xprt, format);
587 EXPORT_SYMBOL_GPL(rpc_peeraddr2str);
590 rpc_setbufsize(struct rpc_clnt *clnt, unsigned int sndsize, unsigned int rcvsize)
592 struct rpc_xprt *xprt = clnt->cl_xprt;
593 if (xprt->ops->set_buffer_size)
594 xprt->ops->set_buffer_size(xprt, sndsize, rcvsize);
598 * Return size of largest payload RPC client can support, in bytes
600 * For stream transports, this is one RPC record fragment (see RFC
601 * 1831), as we don't support multi-record requests yet. For datagram
602 * transports, this is the size of an IP packet minus the IP, UDP, and
605 size_t rpc_max_payload(struct rpc_clnt *clnt)
607 return clnt->cl_xprt->max_payload;
609 EXPORT_SYMBOL_GPL(rpc_max_payload);
612 * rpc_force_rebind - force transport to check that remote port is unchanged
613 * @clnt: client to rebind
616 void rpc_force_rebind(struct rpc_clnt *clnt)
618 if (clnt->cl_autobind)
619 xprt_clear_bound(clnt->cl_xprt);
621 EXPORT_SYMBOL_GPL(rpc_force_rebind);
624 * Restart an (async) RPC call. Usually called from within the
628 rpc_restart_call(struct rpc_task *task)
630 if (RPC_ASSASSINATED(task))
633 task->tk_action = call_start;
639 * Other FSM states can be visited zero or more times, but
640 * this state is visited exactly once for each RPC.
643 call_start(struct rpc_task *task)
645 struct rpc_clnt *clnt = task->tk_client;
647 dprintk("RPC: %4d call_start %s%d proc %d (%s)\n", task->tk_pid,
648 clnt->cl_protname, clnt->cl_vers, task->tk_msg.rpc_proc->p_proc,
649 (RPC_IS_ASYNC(task) ? "async" : "sync"));
651 /* Increment call count */
652 task->tk_msg.rpc_proc->p_count++;
653 clnt->cl_stats->rpccnt++;
654 task->tk_action = call_reserve;
658 * 1. Reserve an RPC call slot
661 call_reserve(struct rpc_task *task)
663 dprintk("RPC: %4d call_reserve\n", task->tk_pid);
665 if (!rpcauth_uptodatecred(task)) {
666 task->tk_action = call_refresh;
671 task->tk_action = call_reserveresult;
676 * 1b. Grok the result of xprt_reserve()
679 call_reserveresult(struct rpc_task *task)
681 int status = task->tk_status;
683 dprintk("RPC: %4d call_reserveresult (status %d)\n",
684 task->tk_pid, task->tk_status);
687 * After a call to xprt_reserve(), we must have either
688 * a request slot or else an error status.
692 if (task->tk_rqstp) {
693 task->tk_action = call_allocate;
697 printk(KERN_ERR "%s: status=%d, but no request slot, exiting\n",
698 __FUNCTION__, status);
699 rpc_exit(task, -EIO);
704 * Even though there was an error, we may have acquired
705 * a request slot somehow. Make sure not to leak it.
707 if (task->tk_rqstp) {
708 printk(KERN_ERR "%s: status=%d, request allocated anyway\n",
709 __FUNCTION__, status);
714 case -EAGAIN: /* woken up; retry */
715 task->tk_action = call_reserve;
717 case -EIO: /* probably a shutdown */
720 printk(KERN_ERR "%s: unrecognized error %d, exiting\n",
721 __FUNCTION__, status);
724 rpc_exit(task, status);
728 * 2. Allocate the buffer. For details, see sched.c:rpc_malloc.
729 * (Note: buffer memory is freed in xprt_release).
732 call_allocate(struct rpc_task *task)
734 struct rpc_rqst *req = task->tk_rqstp;
735 struct rpc_xprt *xprt = task->tk_xprt;
738 dprintk("RPC: %4d call_allocate (status %d)\n",
739 task->tk_pid, task->tk_status);
740 task->tk_action = call_bind;
744 /* FIXME: compute buffer requirements more exactly using
746 bufsiz = task->tk_msg.rpc_proc->p_bufsiz + RPC_SLACK_SPACE;
748 if (xprt->ops->buf_alloc(task, bufsiz << 1) != NULL)
750 printk(KERN_INFO "RPC: buffer allocation failed for task %p\n", task);
752 if (RPC_IS_ASYNC(task) || !signalled()) {
754 task->tk_action = call_reserve;
755 rpc_delay(task, HZ>>4);
759 rpc_exit(task, -ERESTARTSYS);
763 rpc_task_need_encode(struct rpc_task *task)
765 return task->tk_rqstp->rq_snd_buf.len == 0;
769 rpc_task_force_reencode(struct rpc_task *task)
771 task->tk_rqstp->rq_snd_buf.len = 0;
775 * 3. Encode arguments of an RPC call
778 call_encode(struct rpc_task *task)
780 struct rpc_rqst *req = task->tk_rqstp;
781 struct xdr_buf *sndbuf = &req->rq_snd_buf;
782 struct xdr_buf *rcvbuf = &req->rq_rcv_buf;
787 dprintk("RPC: %4d call_encode (status %d)\n",
788 task->tk_pid, task->tk_status);
790 /* Default buffer setup */
791 bufsiz = req->rq_bufsize >> 1;
792 sndbuf->head[0].iov_base = (void *)req->rq_buffer;
793 sndbuf->head[0].iov_len = bufsiz;
794 sndbuf->tail[0].iov_len = 0;
795 sndbuf->page_len = 0;
797 sndbuf->buflen = bufsiz;
798 rcvbuf->head[0].iov_base = (void *)((char *)req->rq_buffer + bufsiz);
799 rcvbuf->head[0].iov_len = bufsiz;
800 rcvbuf->tail[0].iov_len = 0;
801 rcvbuf->page_len = 0;
803 rcvbuf->buflen = bufsiz;
805 /* Encode header and provided arguments */
806 encode = task->tk_msg.rpc_proc->p_encode;
807 if (!(p = call_header(task))) {
808 printk(KERN_INFO "RPC: call_header failed, exit EIO\n");
809 rpc_exit(task, -EIO);
815 task->tk_status = rpcauth_wrap_req(task, encode, req, p,
816 task->tk_msg.rpc_argp);
817 if (task->tk_status == -ENOMEM) {
818 /* XXX: Is this sane? */
819 rpc_delay(task, 3*HZ);
820 task->tk_status = -EAGAIN;
825 * 4. Get the server port number if not yet set
828 call_bind(struct rpc_task *task)
830 struct rpc_xprt *xprt = task->tk_xprt;
832 dprintk("RPC: %4d call_bind (status %d)\n",
833 task->tk_pid, task->tk_status);
835 task->tk_action = call_connect;
836 if (!xprt_bound(xprt)) {
837 task->tk_action = call_bind_status;
838 task->tk_timeout = xprt->bind_timeout;
839 xprt->ops->rpcbind(task);
844 * 4a. Sort out bind result
847 call_bind_status(struct rpc_task *task)
849 int status = -EACCES;
851 if (task->tk_status >= 0) {
852 dprintk("RPC: %4d call_bind_status (status %d)\n",
853 task->tk_pid, task->tk_status);
855 task->tk_action = call_connect;
859 switch (task->tk_status) {
861 dprintk("RPC: %4d remote rpcbind: RPC program/version unavailable\n",
863 rpc_delay(task, 3*HZ);
866 dprintk("RPC: %4d rpcbind request timed out\n",
870 dprintk("RPC: %4d remote rpcbind service unavailable\n",
873 case -EPROTONOSUPPORT:
874 dprintk("RPC: %4d remote rpcbind version 2 unavailable\n",
878 dprintk("RPC: %4d unrecognized rpcbind error (%d)\n",
879 task->tk_pid, -task->tk_status);
883 rpc_exit(task, status);
887 task->tk_action = call_timeout;
891 * 4b. Connect to the RPC server
894 call_connect(struct rpc_task *task)
896 struct rpc_xprt *xprt = task->tk_xprt;
898 dprintk("RPC: %4d call_connect xprt %p %s connected\n",
900 (xprt_connected(xprt) ? "is" : "is not"));
902 task->tk_action = call_transmit;
903 if (!xprt_connected(xprt)) {
904 task->tk_action = call_connect_status;
905 if (task->tk_status < 0)
912 * 4c. Sort out connect result
915 call_connect_status(struct rpc_task *task)
917 struct rpc_clnt *clnt = task->tk_client;
918 int status = task->tk_status;
920 dprintk("RPC: %5u call_connect_status (status %d)\n",
921 task->tk_pid, task->tk_status);
925 clnt->cl_stats->netreconn++;
926 task->tk_action = call_transmit;
930 /* Something failed: remote service port may have changed */
931 rpc_force_rebind(clnt);
936 task->tk_action = call_bind;
937 if (!RPC_IS_SOFT(task))
939 /* if soft mounted, test if we've timed out */
941 task->tk_action = call_timeout;
944 rpc_exit(task, -EIO);
948 * 5. Transmit the RPC request, and wait for reply
951 call_transmit(struct rpc_task *task)
953 dprintk("RPC: %4d call_transmit (status %d)\n",
954 task->tk_pid, task->tk_status);
956 task->tk_action = call_status;
957 if (task->tk_status < 0)
959 task->tk_status = xprt_prepare_transmit(task);
960 if (task->tk_status != 0)
962 task->tk_action = call_transmit_status;
963 /* Encode here so that rpcsec_gss can use correct sequence number. */
964 if (rpc_task_need_encode(task)) {
965 BUG_ON(task->tk_rqstp->rq_bytes_sent != 0);
967 /* Did the encode result in an error condition? */
968 if (task->tk_status != 0)
972 if (task->tk_status < 0)
975 * On success, ensure that we call xprt_end_transmit() before sleeping
976 * in order to allow access to the socket to other RPC requests.
978 call_transmit_status(task);
979 if (task->tk_msg.rpc_proc->p_decode != NULL)
981 task->tk_action = rpc_exit_task;
982 rpc_wake_up_task(task);
986 * 5a. Handle cleanup after a transmission
989 call_transmit_status(struct rpc_task *task)
991 task->tk_action = call_status;
993 * Special case: if we've been waiting on the socket's write_space()
994 * callback, then don't call xprt_end_transmit().
996 if (task->tk_status == -EAGAIN)
998 xprt_end_transmit(task);
999 rpc_task_force_reencode(task);
1003 * 6. Sort out the RPC call status
1006 call_status(struct rpc_task *task)
1008 struct rpc_clnt *clnt = task->tk_client;
1009 struct rpc_rqst *req = task->tk_rqstp;
1012 if (req->rq_received > 0 && !req->rq_bytes_sent)
1013 task->tk_status = req->rq_received;
1015 dprintk("RPC: %4d call_status (status %d)\n",
1016 task->tk_pid, task->tk_status);
1018 status = task->tk_status;
1020 task->tk_action = call_decode;
1024 task->tk_status = 0;
1030 * Delay any retries for 3 seconds, then handle as if it
1033 rpc_delay(task, 3*HZ);
1035 task->tk_action = call_timeout;
1039 rpc_force_rebind(clnt);
1040 task->tk_action = call_bind;
1043 task->tk_action = call_transmit;
1046 /* shutdown or soft timeout */
1047 rpc_exit(task, status);
1050 printk("%s: RPC call returned error %d\n",
1051 clnt->cl_protname, -status);
1052 rpc_exit(task, status);
1057 * 6a. Handle RPC timeout
1058 * We do not release the request slot, so we keep using the
1059 * same XID for all retransmits.
1062 call_timeout(struct rpc_task *task)
1064 struct rpc_clnt *clnt = task->tk_client;
1066 if (xprt_adjust_timeout(task->tk_rqstp) == 0) {
1067 dprintk("RPC: %4d call_timeout (minor)\n", task->tk_pid);
1071 dprintk("RPC: %4d call_timeout (major)\n", task->tk_pid);
1072 task->tk_timeouts++;
1074 if (RPC_IS_SOFT(task)) {
1075 printk(KERN_NOTICE "%s: server %s not responding, timed out\n",
1076 clnt->cl_protname, clnt->cl_server);
1077 rpc_exit(task, -EIO);
1081 if (!(task->tk_flags & RPC_CALL_MAJORSEEN)) {
1082 task->tk_flags |= RPC_CALL_MAJORSEEN;
1083 printk(KERN_NOTICE "%s: server %s not responding, still trying\n",
1084 clnt->cl_protname, clnt->cl_server);
1086 rpc_force_rebind(clnt);
1089 clnt->cl_stats->rpcretrans++;
1090 task->tk_action = call_bind;
1091 task->tk_status = 0;
1095 * 7. Decode the RPC reply
1098 call_decode(struct rpc_task *task)
1100 struct rpc_clnt *clnt = task->tk_client;
1101 struct rpc_rqst *req = task->tk_rqstp;
1102 kxdrproc_t decode = task->tk_msg.rpc_proc->p_decode;
1105 dprintk("RPC: %4d call_decode (status %d)\n",
1106 task->tk_pid, task->tk_status);
1108 if (task->tk_flags & RPC_CALL_MAJORSEEN) {
1109 printk(KERN_NOTICE "%s: server %s OK\n",
1110 clnt->cl_protname, clnt->cl_server);
1111 task->tk_flags &= ~RPC_CALL_MAJORSEEN;
1114 if (task->tk_status < 12) {
1115 if (!RPC_IS_SOFT(task)) {
1116 task->tk_action = call_bind;
1117 clnt->cl_stats->rpcretrans++;
1120 dprintk("%s: too small RPC reply size (%d bytes)\n",
1121 clnt->cl_protname, task->tk_status);
1122 task->tk_action = call_timeout;
1127 * Ensure that we see all writes made by xprt_complete_rqst()
1128 * before it changed req->rq_received.
1131 req->rq_rcv_buf.len = req->rq_private_buf.len;
1133 /* Check that the softirq receive buffer is valid */
1134 WARN_ON(memcmp(&req->rq_rcv_buf, &req->rq_private_buf,
1135 sizeof(req->rq_rcv_buf)) != 0);
1137 /* Verify the RPC header */
1138 p = call_verify(task);
1140 if (p == ERR_PTR(-EAGAIN))
1145 task->tk_action = rpc_exit_task;
1148 task->tk_status = rpcauth_unwrap_resp(task, decode, req, p,
1149 task->tk_msg.rpc_resp);
1150 dprintk("RPC: %4d call_decode result %d\n", task->tk_pid,
1154 req->rq_received = req->rq_private_buf.len = 0;
1155 task->tk_status = 0;
1159 * 8. Refresh the credentials if rejected by the server
1162 call_refresh(struct rpc_task *task)
1164 dprintk("RPC: %4d call_refresh\n", task->tk_pid);
1166 xprt_release(task); /* Must do to obtain new XID */
1167 task->tk_action = call_refreshresult;
1168 task->tk_status = 0;
1169 task->tk_client->cl_stats->rpcauthrefresh++;
1170 rpcauth_refreshcred(task);
1174 * 8a. Process the results of a credential refresh
1177 call_refreshresult(struct rpc_task *task)
1179 int status = task->tk_status;
1180 dprintk("RPC: %4d call_refreshresult (status %d)\n",
1181 task->tk_pid, task->tk_status);
1183 task->tk_status = 0;
1184 task->tk_action = call_reserve;
1185 if (status >= 0 && rpcauth_uptodatecred(task))
1187 if (status == -EACCES) {
1188 rpc_exit(task, -EACCES);
1191 task->tk_action = call_refresh;
1192 if (status != -ETIMEDOUT)
1193 rpc_delay(task, 3*HZ);
1198 * Call header serialization
1201 call_header(struct rpc_task *task)
1203 struct rpc_clnt *clnt = task->tk_client;
1204 struct rpc_rqst *req = task->tk_rqstp;
1205 __be32 *p = req->rq_svec[0].iov_base;
1207 /* FIXME: check buffer size? */
1209 p = xprt_skip_transport_header(task->tk_xprt, p);
1210 *p++ = req->rq_xid; /* XID */
1211 *p++ = htonl(RPC_CALL); /* CALL */
1212 *p++ = htonl(RPC_VERSION); /* RPC version */
1213 *p++ = htonl(clnt->cl_prog); /* program number */
1214 *p++ = htonl(clnt->cl_vers); /* program version */
1215 *p++ = htonl(task->tk_msg.rpc_proc->p_proc); /* procedure */
1216 p = rpcauth_marshcred(task, p);
1217 req->rq_slen = xdr_adjust_iovec(&req->rq_svec[0], p);
1222 * Reply header verification
1225 call_verify(struct rpc_task *task)
1227 struct kvec *iov = &task->tk_rqstp->rq_rcv_buf.head[0];
1228 int len = task->tk_rqstp->rq_rcv_buf.len >> 2;
1229 __be32 *p = iov->iov_base;
1231 int error = -EACCES;
1233 if ((task->tk_rqstp->rq_rcv_buf.len & 3) != 0) {
1234 /* RFC-1014 says that the representation of XDR data must be a
1235 * multiple of four bytes
1236 * - if it isn't pointer subtraction in the NFS client may give
1240 "call_verify: XDR representation not a multiple of"
1241 " 4 bytes: 0x%x\n", task->tk_rqstp->rq_rcv_buf.len);
1246 p += 1; /* skip XID */
1248 if ((n = ntohl(*p++)) != RPC_REPLY) {
1249 printk(KERN_WARNING "call_verify: not an RPC reply: %x\n", n);
1252 if ((n = ntohl(*p++)) != RPC_MSG_ACCEPTED) {
1255 switch ((n = ntohl(*p++))) {
1256 case RPC_AUTH_ERROR:
1259 dprintk("%s: RPC call version mismatch!\n", __FUNCTION__);
1260 error = -EPROTONOSUPPORT;
1263 dprintk("%s: RPC call rejected, unknown error: %x\n", __FUNCTION__, n);
1268 switch ((n = ntohl(*p++))) {
1269 case RPC_AUTH_REJECTEDCRED:
1270 case RPC_AUTH_REJECTEDVERF:
1271 case RPCSEC_GSS_CREDPROBLEM:
1272 case RPCSEC_GSS_CTXPROBLEM:
1273 if (!task->tk_cred_retry)
1275 task->tk_cred_retry--;
1276 dprintk("RPC: %4d call_verify: retry stale creds\n",
1278 rpcauth_invalcred(task);
1279 task->tk_action = call_refresh;
1281 case RPC_AUTH_BADCRED:
1282 case RPC_AUTH_BADVERF:
1283 /* possibly garbled cred/verf? */
1284 if (!task->tk_garb_retry)
1286 task->tk_garb_retry--;
1287 dprintk("RPC: %4d call_verify: retry garbled creds\n",
1289 task->tk_action = call_bind;
1291 case RPC_AUTH_TOOWEAK:
1292 printk(KERN_NOTICE "call_verify: server %s requires stronger "
1293 "authentication.\n", task->tk_client->cl_server);
1296 printk(KERN_WARNING "call_verify: unknown auth error: %x\n", n);
1299 dprintk("RPC: %4d call_verify: call rejected %d\n",
1303 if (!(p = rpcauth_checkverf(task, p))) {
1304 printk(KERN_WARNING "call_verify: auth check failed\n");
1305 goto out_garbage; /* bad verifier, retry */
1307 len = p - (__be32 *)iov->iov_base - 1;
1310 switch ((n = ntohl(*p++))) {
1313 case RPC_PROG_UNAVAIL:
1314 dprintk("RPC: call_verify: program %u is unsupported by server %s\n",
1315 (unsigned int)task->tk_client->cl_prog,
1316 task->tk_client->cl_server);
1317 error = -EPFNOSUPPORT;
1319 case RPC_PROG_MISMATCH:
1320 dprintk("RPC: call_verify: program %u, version %u unsupported by server %s\n",
1321 (unsigned int)task->tk_client->cl_prog,
1322 (unsigned int)task->tk_client->cl_vers,
1323 task->tk_client->cl_server);
1324 error = -EPROTONOSUPPORT;
1326 case RPC_PROC_UNAVAIL:
1327 dprintk("RPC: call_verify: proc %p unsupported by program %u, version %u on server %s\n",
1328 task->tk_msg.rpc_proc,
1329 task->tk_client->cl_prog,
1330 task->tk_client->cl_vers,
1331 task->tk_client->cl_server);
1332 error = -EOPNOTSUPP;
1334 case RPC_GARBAGE_ARGS:
1335 dprintk("RPC: %4d %s: server saw garbage\n", task->tk_pid, __FUNCTION__);
1338 printk(KERN_WARNING "call_verify: server accept status: %x\n", n);
1343 task->tk_client->cl_stats->rpcgarbage++;
1344 if (task->tk_garb_retry) {
1345 task->tk_garb_retry--;
1346 dprintk("RPC %s: retrying %4d\n", __FUNCTION__, task->tk_pid);
1347 task->tk_action = call_bind;
1349 return ERR_PTR(-EAGAIN);
1351 printk(KERN_WARNING "RPC %s: retry failed, exit EIO\n", __FUNCTION__);
1355 rpc_exit(task, error);
1356 return ERR_PTR(error);
1358 printk(KERN_WARNING "RPC %s: server reply was truncated.\n", __FUNCTION__);
1362 static int rpcproc_encode_null(void *rqstp, __be32 *data, void *obj)
1367 static int rpcproc_decode_null(void *rqstp, __be32 *data, void *obj)
1372 static struct rpc_procinfo rpcproc_null = {
1373 .p_encode = rpcproc_encode_null,
1374 .p_decode = rpcproc_decode_null,
1377 int rpc_ping(struct rpc_clnt *clnt, int flags)
1379 struct rpc_message msg = {
1380 .rpc_proc = &rpcproc_null,
1383 msg.rpc_cred = authnull_ops.lookup_cred(NULL, NULL, 0);
1384 err = rpc_call_sync(clnt, &msg, flags);
1385 put_rpccred(msg.rpc_cred);