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 (clnt->cl_metrics == NULL)
147 clnt->cl_program = program;
149 if (!xprt_bound(clnt->cl_xprt))
150 clnt->cl_autobind = 1;
152 clnt->cl_rtt = &clnt->cl_rtt_default;
153 rpc_init_rtt(&clnt->cl_rtt_default, xprt->timeout.to_initval);
155 err = rpc_setup_pipedir(clnt, program->pipe_dir_name);
159 auth = rpcauth_create(flavor, clnt);
161 printk(KERN_INFO "RPC: Couldn't create auth handle (flavor %u)\n",
167 /* save the nodename */
168 clnt->cl_nodelen = strlen(utsname()->nodename);
169 if (clnt->cl_nodelen > UNX_MAXNODENAME)
170 clnt->cl_nodelen = UNX_MAXNODENAME;
171 memcpy(clnt->cl_nodename, utsname()->nodename, clnt->cl_nodelen);
175 if (!IS_ERR(clnt->cl_dentry)) {
176 rpc_rmdir(clnt->cl_dentry);
180 rpc_free_iostats(clnt->cl_metrics);
182 if (clnt->cl_server != clnt->cl_inline_name)
183 kfree(clnt->cl_server);
192 * rpc_create - create an RPC client and transport with one call
193 * @args: rpc_clnt create argument structure
195 * Creates and initializes an RPC transport and an RPC client.
197 * It can ping the server in order to determine if it is up, and to see if
198 * it supports this program and version. RPC_CLNT_CREATE_NOPING disables
199 * this behavior so asynchronous tasks can also use rpc_create.
201 struct rpc_clnt *rpc_create(struct rpc_create_args *args)
203 struct rpc_xprt *xprt;
204 struct rpc_clnt *clnt;
206 xprt = xprt_create_transport(args->protocol, args->address,
207 args->addrsize, args->timeout);
209 return (struct rpc_clnt *)xprt;
212 * By default, kernel RPC client connects from a reserved port.
213 * CAP_NET_BIND_SERVICE will not be set for unprivileged requesters,
214 * but it is always enabled for rpciod, which handles the connect
218 if (args->flags & RPC_CLNT_CREATE_NONPRIVPORT)
221 dprintk("RPC: creating %s client for %s (xprt %p)\n",
222 args->program->name, args->servername, xprt);
224 clnt = rpc_new_client(xprt, args->servername, args->program,
225 args->version, args->authflavor);
229 if (!(args->flags & RPC_CLNT_CREATE_NOPING)) {
230 int err = rpc_ping(clnt, RPC_TASK_SOFT|RPC_TASK_NOINTR);
232 rpc_shutdown_client(clnt);
237 clnt->cl_softrtry = 1;
238 if (args->flags & RPC_CLNT_CREATE_HARDRTRY)
239 clnt->cl_softrtry = 0;
241 if (args->flags & RPC_CLNT_CREATE_INTR)
243 if (args->flags & RPC_CLNT_CREATE_AUTOBIND)
244 clnt->cl_autobind = 1;
245 if (args->flags & RPC_CLNT_CREATE_ONESHOT)
246 clnt->cl_oneshot = 1;
250 EXPORT_SYMBOL_GPL(rpc_create);
253 * This function clones the RPC client structure. It allows us to share the
254 * same transport while varying parameters such as the authentication
258 rpc_clone_client(struct rpc_clnt *clnt)
260 struct rpc_clnt *new;
263 new = kmemdup(clnt, sizeof(*new), GFP_KERNEL);
266 atomic_set(&new->cl_count, 1);
267 atomic_set(&new->cl_users, 0);
268 new->cl_metrics = rpc_alloc_iostats(clnt);
269 if (new->cl_metrics == NULL)
271 err = rpc_setup_pipedir(new, clnt->cl_program->pipe_dir_name);
274 new->cl_parent = clnt;
275 atomic_inc(&clnt->cl_count);
276 new->cl_xprt = xprt_get(clnt->cl_xprt);
277 /* Turn off autobind on clones */
278 new->cl_autobind = 0;
281 rpc_init_rtt(&new->cl_rtt_default, clnt->cl_xprt->timeout.to_initval);
283 atomic_inc(&new->cl_auth->au_count);
286 rpc_free_iostats(new->cl_metrics);
290 dprintk("RPC: %s returned error %d\n", __FUNCTION__, err);
295 * Properly shut down an RPC client, terminating all outstanding
296 * requests. Note that we must be certain that cl_oneshot and
297 * cl_dead are cleared, or else the client would be destroyed
298 * when the last task releases it.
301 rpc_shutdown_client(struct rpc_clnt *clnt)
303 dprintk("RPC: shutting down %s client for %s, tasks=%d\n",
304 clnt->cl_protname, clnt->cl_server,
305 atomic_read(&clnt->cl_users));
307 while (atomic_read(&clnt->cl_users) > 0) {
308 /* Don't let rpc_release_client destroy us */
309 clnt->cl_oneshot = 0;
311 rpc_killall_tasks(clnt);
312 wait_event_timeout(destroy_wait,
313 !atomic_read(&clnt->cl_users), 1*HZ);
316 if (atomic_read(&clnt->cl_users) < 0) {
317 printk(KERN_ERR "RPC: rpc_shutdown_client clnt %p tasks=%d\n",
318 clnt, atomic_read(&clnt->cl_users));
325 return rpc_destroy_client(clnt);
329 * Delete an RPC client
332 rpc_destroy_client(struct rpc_clnt *clnt)
334 if (!atomic_dec_and_test(&clnt->cl_count))
336 BUG_ON(atomic_read(&clnt->cl_users) != 0);
338 dprintk("RPC: destroying %s client for %s\n",
339 clnt->cl_protname, clnt->cl_server);
341 rpcauth_destroy(clnt->cl_auth);
342 clnt->cl_auth = NULL;
344 if (!IS_ERR(clnt->cl_dentry)) {
345 rpc_rmdir(clnt->cl_dentry);
348 if (clnt->cl_parent != clnt) {
349 rpc_destroy_client(clnt->cl_parent);
352 if (clnt->cl_server != clnt->cl_inline_name)
353 kfree(clnt->cl_server);
355 rpc_free_iostats(clnt->cl_metrics);
356 clnt->cl_metrics = NULL;
357 xprt_put(clnt->cl_xprt);
363 * Release an RPC client
366 rpc_release_client(struct rpc_clnt *clnt)
368 dprintk("RPC: rpc_release_client(%p, %d)\n",
369 clnt, atomic_read(&clnt->cl_users));
371 if (!atomic_dec_and_test(&clnt->cl_users))
373 wake_up(&destroy_wait);
374 if (clnt->cl_oneshot || clnt->cl_dead)
375 rpc_destroy_client(clnt);
379 * rpc_bind_new_program - bind a new RPC program to an existing client
380 * @old - old rpc_client
381 * @program - rpc program to set
382 * @vers - rpc program version
384 * Clones the rpc client and sets up a new RPC program. This is mainly
385 * of use for enabling different RPC programs to share the same transport.
386 * The Sun NFSv2/v3 ACL protocol can do this.
388 struct rpc_clnt *rpc_bind_new_program(struct rpc_clnt *old,
389 struct rpc_program *program,
392 struct rpc_clnt *clnt;
393 struct rpc_version *version;
396 BUG_ON(vers >= program->nrvers || !program->version[vers]);
397 version = program->version[vers];
398 clnt = rpc_clone_client(old);
401 clnt->cl_procinfo = version->procs;
402 clnt->cl_maxproc = version->nrprocs;
403 clnt->cl_protname = program->name;
404 clnt->cl_prog = program->number;
405 clnt->cl_vers = version->number;
406 clnt->cl_stats = program->stats;
407 err = rpc_ping(clnt, RPC_TASK_SOFT|RPC_TASK_NOINTR);
409 rpc_shutdown_client(clnt);
417 * Default callback for async RPC calls
420 rpc_default_callback(struct rpc_task *task, void *data)
424 static const struct rpc_call_ops rpc_default_ops = {
425 .rpc_call_done = rpc_default_callback,
429 * Export the signal mask handling for synchronous code that
430 * sleeps on RPC calls
432 #define RPC_INTR_SIGNALS (sigmask(SIGHUP) | sigmask(SIGINT) | sigmask(SIGQUIT) | sigmask(SIGTERM))
434 static void rpc_save_sigmask(sigset_t *oldset, int intr)
436 unsigned long sigallow = sigmask(SIGKILL);
439 /* Block all signals except those listed in sigallow */
441 sigallow |= RPC_INTR_SIGNALS;
442 siginitsetinv(&sigmask, sigallow);
443 sigprocmask(SIG_BLOCK, &sigmask, oldset);
446 static inline void rpc_task_sigmask(struct rpc_task *task, sigset_t *oldset)
448 rpc_save_sigmask(oldset, !RPC_TASK_UNINTERRUPTIBLE(task));
451 static inline void rpc_restore_sigmask(sigset_t *oldset)
453 sigprocmask(SIG_SETMASK, oldset, NULL);
456 void rpc_clnt_sigmask(struct rpc_clnt *clnt, sigset_t *oldset)
458 rpc_save_sigmask(oldset, clnt->cl_intr);
461 void rpc_clnt_sigunmask(struct rpc_clnt *clnt, sigset_t *oldset)
463 rpc_restore_sigmask(oldset);
467 * New rpc_call implementation
469 int rpc_call_sync(struct rpc_clnt *clnt, struct rpc_message *msg, int flags)
471 struct rpc_task *task;
475 /* If this client is slain all further I/O fails */
479 BUG_ON(flags & RPC_TASK_ASYNC);
481 task = rpc_new_task(clnt, flags, &rpc_default_ops, NULL);
485 /* Mask signals on RPC calls _and_ GSS_AUTH upcalls */
486 rpc_task_sigmask(task, &oldset);
488 rpc_call_setup(task, msg, 0);
490 /* Set up the call info struct and execute the task */
491 status = task->tk_status;
493 rpc_release_task(task);
496 atomic_inc(&task->tk_count);
497 status = rpc_execute(task);
499 status = task->tk_status;
502 rpc_restore_sigmask(&oldset);
507 * New rpc_call implementation
510 rpc_call_async(struct rpc_clnt *clnt, struct rpc_message *msg, int flags,
511 const struct rpc_call_ops *tk_ops, void *data)
513 struct rpc_task *task;
517 /* If this client is slain all further I/O fails */
522 flags |= RPC_TASK_ASYNC;
524 /* Create/initialize a new RPC task */
526 if (!(task = rpc_new_task(clnt, flags, tk_ops, data)))
529 /* Mask signals on GSS_AUTH upcalls */
530 rpc_task_sigmask(task, &oldset);
532 rpc_call_setup(task, msg, 0);
534 /* Set up the call info struct and execute the task */
535 status = task->tk_status;
539 rpc_release_task(task);
541 rpc_restore_sigmask(&oldset);
544 if (tk_ops->rpc_release != NULL)
545 tk_ops->rpc_release(data);
551 rpc_call_setup(struct rpc_task *task, struct rpc_message *msg, int flags)
554 task->tk_flags |= flags;
555 /* Bind the user cred */
556 if (task->tk_msg.rpc_cred != NULL)
557 rpcauth_holdcred(task);
559 rpcauth_bindcred(task);
561 if (task->tk_status == 0)
562 task->tk_action = call_start;
564 task->tk_action = rpc_exit_task;
568 * rpc_peeraddr - extract remote peer address from clnt's xprt
569 * @clnt: RPC client structure
570 * @buf: target buffer
571 * @size: length of target buffer
573 * Returns the number of bytes that are actually in the stored address.
575 size_t rpc_peeraddr(struct rpc_clnt *clnt, struct sockaddr *buf, size_t bufsize)
578 struct rpc_xprt *xprt = clnt->cl_xprt;
580 bytes = sizeof(xprt->addr);
583 memcpy(buf, &clnt->cl_xprt->addr, bytes);
584 return xprt->addrlen;
586 EXPORT_SYMBOL_GPL(rpc_peeraddr);
589 * rpc_peeraddr2str - return remote peer address in printable format
590 * @clnt: RPC client structure
591 * @format: address format
594 char *rpc_peeraddr2str(struct rpc_clnt *clnt, enum rpc_display_format_t format)
596 struct rpc_xprt *xprt = clnt->cl_xprt;
597 return xprt->ops->print_addr(xprt, format);
599 EXPORT_SYMBOL_GPL(rpc_peeraddr2str);
602 rpc_setbufsize(struct rpc_clnt *clnt, unsigned int sndsize, unsigned int rcvsize)
604 struct rpc_xprt *xprt = clnt->cl_xprt;
605 if (xprt->ops->set_buffer_size)
606 xprt->ops->set_buffer_size(xprt, sndsize, rcvsize);
610 * Return size of largest payload RPC client can support, in bytes
612 * For stream transports, this is one RPC record fragment (see RFC
613 * 1831), as we don't support multi-record requests yet. For datagram
614 * transports, this is the size of an IP packet minus the IP, UDP, and
617 size_t rpc_max_payload(struct rpc_clnt *clnt)
619 return clnt->cl_xprt->max_payload;
621 EXPORT_SYMBOL_GPL(rpc_max_payload);
624 * rpc_force_rebind - force transport to check that remote port is unchanged
625 * @clnt: client to rebind
628 void rpc_force_rebind(struct rpc_clnt *clnt)
630 if (clnt->cl_autobind)
631 xprt_clear_bound(clnt->cl_xprt);
633 EXPORT_SYMBOL_GPL(rpc_force_rebind);
636 * Restart an (async) RPC call. Usually called from within the
640 rpc_restart_call(struct rpc_task *task)
642 if (RPC_ASSASSINATED(task))
645 task->tk_action = call_start;
651 * Other FSM states can be visited zero or more times, but
652 * this state is visited exactly once for each RPC.
655 call_start(struct rpc_task *task)
657 struct rpc_clnt *clnt = task->tk_client;
659 dprintk("RPC: %4d call_start %s%d proc %d (%s)\n", task->tk_pid,
660 clnt->cl_protname, clnt->cl_vers, task->tk_msg.rpc_proc->p_proc,
661 (RPC_IS_ASYNC(task) ? "async" : "sync"));
663 /* Increment call count */
664 task->tk_msg.rpc_proc->p_count++;
665 clnt->cl_stats->rpccnt++;
666 task->tk_action = call_reserve;
670 * 1. Reserve an RPC call slot
673 call_reserve(struct rpc_task *task)
675 dprintk("RPC: %4d call_reserve\n", task->tk_pid);
677 if (!rpcauth_uptodatecred(task)) {
678 task->tk_action = call_refresh;
683 task->tk_action = call_reserveresult;
688 * 1b. Grok the result of xprt_reserve()
691 call_reserveresult(struct rpc_task *task)
693 int status = task->tk_status;
695 dprintk("RPC: %4d call_reserveresult (status %d)\n",
696 task->tk_pid, task->tk_status);
699 * After a call to xprt_reserve(), we must have either
700 * a request slot or else an error status.
704 if (task->tk_rqstp) {
705 task->tk_action = call_allocate;
709 printk(KERN_ERR "%s: status=%d, but no request slot, exiting\n",
710 __FUNCTION__, status);
711 rpc_exit(task, -EIO);
716 * Even though there was an error, we may have acquired
717 * a request slot somehow. Make sure not to leak it.
719 if (task->tk_rqstp) {
720 printk(KERN_ERR "%s: status=%d, request allocated anyway\n",
721 __FUNCTION__, status);
726 case -EAGAIN: /* woken up; retry */
727 task->tk_action = call_reserve;
729 case -EIO: /* probably a shutdown */
732 printk(KERN_ERR "%s: unrecognized error %d, exiting\n",
733 __FUNCTION__, status);
736 rpc_exit(task, status);
740 * 2. Allocate the buffer. For details, see sched.c:rpc_malloc.
741 * (Note: buffer memory is freed in xprt_release).
744 call_allocate(struct rpc_task *task)
746 struct rpc_rqst *req = task->tk_rqstp;
747 struct rpc_xprt *xprt = task->tk_xprt;
750 dprintk("RPC: %4d call_allocate (status %d)\n",
751 task->tk_pid, task->tk_status);
752 task->tk_action = call_bind;
756 /* FIXME: compute buffer requirements more exactly using
758 bufsiz = task->tk_msg.rpc_proc->p_bufsiz + RPC_SLACK_SPACE;
760 if (xprt->ops->buf_alloc(task, bufsiz << 1) != NULL)
762 printk(KERN_INFO "RPC: buffer allocation failed for task %p\n", task);
764 if (RPC_IS_ASYNC(task) || !signalled()) {
766 task->tk_action = call_reserve;
767 rpc_delay(task, HZ>>4);
771 rpc_exit(task, -ERESTARTSYS);
775 rpc_task_need_encode(struct rpc_task *task)
777 return task->tk_rqstp->rq_snd_buf.len == 0;
781 rpc_task_force_reencode(struct rpc_task *task)
783 task->tk_rqstp->rq_snd_buf.len = 0;
787 * 3. Encode arguments of an RPC call
790 call_encode(struct rpc_task *task)
792 struct rpc_rqst *req = task->tk_rqstp;
793 struct xdr_buf *sndbuf = &req->rq_snd_buf;
794 struct xdr_buf *rcvbuf = &req->rq_rcv_buf;
799 dprintk("RPC: %4d call_encode (status %d)\n",
800 task->tk_pid, task->tk_status);
802 /* Default buffer setup */
803 bufsiz = req->rq_bufsize >> 1;
804 sndbuf->head[0].iov_base = (void *)req->rq_buffer;
805 sndbuf->head[0].iov_len = bufsiz;
806 sndbuf->tail[0].iov_len = 0;
807 sndbuf->page_len = 0;
809 sndbuf->buflen = bufsiz;
810 rcvbuf->head[0].iov_base = (void *)((char *)req->rq_buffer + bufsiz);
811 rcvbuf->head[0].iov_len = bufsiz;
812 rcvbuf->tail[0].iov_len = 0;
813 rcvbuf->page_len = 0;
815 rcvbuf->buflen = bufsiz;
817 /* Encode header and provided arguments */
818 encode = task->tk_msg.rpc_proc->p_encode;
819 if (!(p = call_header(task))) {
820 printk(KERN_INFO "RPC: call_header failed, exit EIO\n");
821 rpc_exit(task, -EIO);
827 task->tk_status = rpcauth_wrap_req(task, encode, req, p,
828 task->tk_msg.rpc_argp);
829 if (task->tk_status == -ENOMEM) {
830 /* XXX: Is this sane? */
831 rpc_delay(task, 3*HZ);
832 task->tk_status = -EAGAIN;
837 * 4. Get the server port number if not yet set
840 call_bind(struct rpc_task *task)
842 struct rpc_xprt *xprt = task->tk_xprt;
844 dprintk("RPC: %4d call_bind (status %d)\n",
845 task->tk_pid, task->tk_status);
847 task->tk_action = call_connect;
848 if (!xprt_bound(xprt)) {
849 task->tk_action = call_bind_status;
850 task->tk_timeout = xprt->bind_timeout;
851 xprt->ops->rpcbind(task);
856 * 4a. Sort out bind result
859 call_bind_status(struct rpc_task *task)
861 int status = -EACCES;
863 if (task->tk_status >= 0) {
864 dprintk("RPC: %4d call_bind_status (status %d)\n",
865 task->tk_pid, task->tk_status);
867 task->tk_action = call_connect;
871 switch (task->tk_status) {
873 dprintk("RPC: %4d remote rpcbind: RPC program/version unavailable\n",
875 rpc_delay(task, 3*HZ);
878 dprintk("RPC: %4d rpcbind request timed out\n",
882 dprintk("RPC: %4d remote rpcbind service unavailable\n",
885 case -EPROTONOSUPPORT:
886 dprintk("RPC: %4d remote rpcbind version 2 unavailable\n",
890 dprintk("RPC: %4d unrecognized rpcbind error (%d)\n",
891 task->tk_pid, -task->tk_status);
895 rpc_exit(task, status);
899 task->tk_action = call_timeout;
903 * 4b. Connect to the RPC server
906 call_connect(struct rpc_task *task)
908 struct rpc_xprt *xprt = task->tk_xprt;
910 dprintk("RPC: %4d call_connect xprt %p %s connected\n",
912 (xprt_connected(xprt) ? "is" : "is not"));
914 task->tk_action = call_transmit;
915 if (!xprt_connected(xprt)) {
916 task->tk_action = call_connect_status;
917 if (task->tk_status < 0)
924 * 4c. Sort out connect result
927 call_connect_status(struct rpc_task *task)
929 struct rpc_clnt *clnt = task->tk_client;
930 int status = task->tk_status;
932 dprintk("RPC: %5u call_connect_status (status %d)\n",
933 task->tk_pid, task->tk_status);
937 clnt->cl_stats->netreconn++;
938 task->tk_action = call_transmit;
942 /* Something failed: remote service port may have changed */
943 rpc_force_rebind(clnt);
948 task->tk_action = call_bind;
949 if (!RPC_IS_SOFT(task))
951 /* if soft mounted, test if we've timed out */
953 task->tk_action = call_timeout;
956 rpc_exit(task, -EIO);
960 * 5. Transmit the RPC request, and wait for reply
963 call_transmit(struct rpc_task *task)
965 dprintk("RPC: %4d call_transmit (status %d)\n",
966 task->tk_pid, task->tk_status);
968 task->tk_action = call_status;
969 if (task->tk_status < 0)
971 task->tk_status = xprt_prepare_transmit(task);
972 if (task->tk_status != 0)
974 task->tk_action = call_transmit_status;
975 /* Encode here so that rpcsec_gss can use correct sequence number. */
976 if (rpc_task_need_encode(task)) {
977 BUG_ON(task->tk_rqstp->rq_bytes_sent != 0);
979 /* Did the encode result in an error condition? */
980 if (task->tk_status != 0)
984 if (task->tk_status < 0)
987 * On success, ensure that we call xprt_end_transmit() before sleeping
988 * in order to allow access to the socket to other RPC requests.
990 call_transmit_status(task);
991 if (task->tk_msg.rpc_proc->p_decode != NULL)
993 task->tk_action = rpc_exit_task;
994 rpc_wake_up_task(task);
998 * 5a. Handle cleanup after a transmission
1001 call_transmit_status(struct rpc_task *task)
1003 task->tk_action = call_status;
1005 * Special case: if we've been waiting on the socket's write_space()
1006 * callback, then don't call xprt_end_transmit().
1008 if (task->tk_status == -EAGAIN)
1010 xprt_end_transmit(task);
1011 rpc_task_force_reencode(task);
1015 * 6. Sort out the RPC call status
1018 call_status(struct rpc_task *task)
1020 struct rpc_clnt *clnt = task->tk_client;
1021 struct rpc_rqst *req = task->tk_rqstp;
1024 if (req->rq_received > 0 && !req->rq_bytes_sent)
1025 task->tk_status = req->rq_received;
1027 dprintk("RPC: %4d call_status (status %d)\n",
1028 task->tk_pid, task->tk_status);
1030 status = task->tk_status;
1032 task->tk_action = call_decode;
1036 task->tk_status = 0;
1042 * Delay any retries for 3 seconds, then handle as if it
1045 rpc_delay(task, 3*HZ);
1047 task->tk_action = call_timeout;
1051 rpc_force_rebind(clnt);
1052 task->tk_action = call_bind;
1055 task->tk_action = call_transmit;
1058 /* shutdown or soft timeout */
1059 rpc_exit(task, status);
1062 printk("%s: RPC call returned error %d\n",
1063 clnt->cl_protname, -status);
1064 rpc_exit(task, status);
1069 * 6a. Handle RPC timeout
1070 * We do not release the request slot, so we keep using the
1071 * same XID for all retransmits.
1074 call_timeout(struct rpc_task *task)
1076 struct rpc_clnt *clnt = task->tk_client;
1078 if (xprt_adjust_timeout(task->tk_rqstp) == 0) {
1079 dprintk("RPC: %4d call_timeout (minor)\n", task->tk_pid);
1083 dprintk("RPC: %4d call_timeout (major)\n", task->tk_pid);
1084 task->tk_timeouts++;
1086 if (RPC_IS_SOFT(task)) {
1087 printk(KERN_NOTICE "%s: server %s not responding, timed out\n",
1088 clnt->cl_protname, clnt->cl_server);
1089 rpc_exit(task, -EIO);
1093 if (!(task->tk_flags & RPC_CALL_MAJORSEEN)) {
1094 task->tk_flags |= RPC_CALL_MAJORSEEN;
1095 printk(KERN_NOTICE "%s: server %s not responding, still trying\n",
1096 clnt->cl_protname, clnt->cl_server);
1098 rpc_force_rebind(clnt);
1101 clnt->cl_stats->rpcretrans++;
1102 task->tk_action = call_bind;
1103 task->tk_status = 0;
1107 * 7. Decode the RPC reply
1110 call_decode(struct rpc_task *task)
1112 struct rpc_clnt *clnt = task->tk_client;
1113 struct rpc_rqst *req = task->tk_rqstp;
1114 kxdrproc_t decode = task->tk_msg.rpc_proc->p_decode;
1117 dprintk("RPC: %4d call_decode (status %d)\n",
1118 task->tk_pid, task->tk_status);
1120 if (task->tk_flags & RPC_CALL_MAJORSEEN) {
1121 printk(KERN_NOTICE "%s: server %s OK\n",
1122 clnt->cl_protname, clnt->cl_server);
1123 task->tk_flags &= ~RPC_CALL_MAJORSEEN;
1126 if (task->tk_status < 12) {
1127 if (!RPC_IS_SOFT(task)) {
1128 task->tk_action = call_bind;
1129 clnt->cl_stats->rpcretrans++;
1132 dprintk("%s: too small RPC reply size (%d bytes)\n",
1133 clnt->cl_protname, task->tk_status);
1134 task->tk_action = call_timeout;
1139 * Ensure that we see all writes made by xprt_complete_rqst()
1140 * before it changed req->rq_received.
1143 req->rq_rcv_buf.len = req->rq_private_buf.len;
1145 /* Check that the softirq receive buffer is valid */
1146 WARN_ON(memcmp(&req->rq_rcv_buf, &req->rq_private_buf,
1147 sizeof(req->rq_rcv_buf)) != 0);
1149 /* Verify the RPC header */
1150 p = call_verify(task);
1152 if (p == ERR_PTR(-EAGAIN))
1157 task->tk_action = rpc_exit_task;
1160 task->tk_status = rpcauth_unwrap_resp(task, decode, req, p,
1161 task->tk_msg.rpc_resp);
1162 dprintk("RPC: %4d call_decode result %d\n", task->tk_pid,
1166 req->rq_received = req->rq_private_buf.len = 0;
1167 task->tk_status = 0;
1171 * 8. Refresh the credentials if rejected by the server
1174 call_refresh(struct rpc_task *task)
1176 dprintk("RPC: %4d call_refresh\n", task->tk_pid);
1178 xprt_release(task); /* Must do to obtain new XID */
1179 task->tk_action = call_refreshresult;
1180 task->tk_status = 0;
1181 task->tk_client->cl_stats->rpcauthrefresh++;
1182 rpcauth_refreshcred(task);
1186 * 8a. Process the results of a credential refresh
1189 call_refreshresult(struct rpc_task *task)
1191 int status = task->tk_status;
1192 dprintk("RPC: %4d call_refreshresult (status %d)\n",
1193 task->tk_pid, task->tk_status);
1195 task->tk_status = 0;
1196 task->tk_action = call_reserve;
1197 if (status >= 0 && rpcauth_uptodatecred(task))
1199 if (status == -EACCES) {
1200 rpc_exit(task, -EACCES);
1203 task->tk_action = call_refresh;
1204 if (status != -ETIMEDOUT)
1205 rpc_delay(task, 3*HZ);
1210 * Call header serialization
1213 call_header(struct rpc_task *task)
1215 struct rpc_clnt *clnt = task->tk_client;
1216 struct rpc_rqst *req = task->tk_rqstp;
1217 __be32 *p = req->rq_svec[0].iov_base;
1219 /* FIXME: check buffer size? */
1221 p = xprt_skip_transport_header(task->tk_xprt, p);
1222 *p++ = req->rq_xid; /* XID */
1223 *p++ = htonl(RPC_CALL); /* CALL */
1224 *p++ = htonl(RPC_VERSION); /* RPC version */
1225 *p++ = htonl(clnt->cl_prog); /* program number */
1226 *p++ = htonl(clnt->cl_vers); /* program version */
1227 *p++ = htonl(task->tk_msg.rpc_proc->p_proc); /* procedure */
1228 p = rpcauth_marshcred(task, p);
1229 req->rq_slen = xdr_adjust_iovec(&req->rq_svec[0], p);
1234 * Reply header verification
1237 call_verify(struct rpc_task *task)
1239 struct kvec *iov = &task->tk_rqstp->rq_rcv_buf.head[0];
1240 int len = task->tk_rqstp->rq_rcv_buf.len >> 2;
1241 __be32 *p = iov->iov_base;
1243 int error = -EACCES;
1245 if ((task->tk_rqstp->rq_rcv_buf.len & 3) != 0) {
1246 /* RFC-1014 says that the representation of XDR data must be a
1247 * multiple of four bytes
1248 * - if it isn't pointer subtraction in the NFS client may give
1252 "call_verify: XDR representation not a multiple of"
1253 " 4 bytes: 0x%x\n", task->tk_rqstp->rq_rcv_buf.len);
1258 p += 1; /* skip XID */
1260 if ((n = ntohl(*p++)) != RPC_REPLY) {
1261 printk(KERN_WARNING "call_verify: not an RPC reply: %x\n", n);
1264 if ((n = ntohl(*p++)) != RPC_MSG_ACCEPTED) {
1267 switch ((n = ntohl(*p++))) {
1268 case RPC_AUTH_ERROR:
1271 dprintk("%s: RPC call version mismatch!\n", __FUNCTION__);
1272 error = -EPROTONOSUPPORT;
1275 dprintk("%s: RPC call rejected, unknown error: %x\n", __FUNCTION__, n);
1280 switch ((n = ntohl(*p++))) {
1281 case RPC_AUTH_REJECTEDCRED:
1282 case RPC_AUTH_REJECTEDVERF:
1283 case RPCSEC_GSS_CREDPROBLEM:
1284 case RPCSEC_GSS_CTXPROBLEM:
1285 if (!task->tk_cred_retry)
1287 task->tk_cred_retry--;
1288 dprintk("RPC: %4d call_verify: retry stale creds\n",
1290 rpcauth_invalcred(task);
1291 task->tk_action = call_refresh;
1293 case RPC_AUTH_BADCRED:
1294 case RPC_AUTH_BADVERF:
1295 /* possibly garbled cred/verf? */
1296 if (!task->tk_garb_retry)
1298 task->tk_garb_retry--;
1299 dprintk("RPC: %4d call_verify: retry garbled creds\n",
1301 task->tk_action = call_bind;
1303 case RPC_AUTH_TOOWEAK:
1304 printk(KERN_NOTICE "call_verify: server %s requires stronger "
1305 "authentication.\n", task->tk_client->cl_server);
1308 printk(KERN_WARNING "call_verify: unknown auth error: %x\n", n);
1311 dprintk("RPC: %4d call_verify: call rejected %d\n",
1315 if (!(p = rpcauth_checkverf(task, p))) {
1316 printk(KERN_WARNING "call_verify: auth check failed\n");
1317 goto out_garbage; /* bad verifier, retry */
1319 len = p - (__be32 *)iov->iov_base - 1;
1322 switch ((n = ntohl(*p++))) {
1325 case RPC_PROG_UNAVAIL:
1326 dprintk("RPC: call_verify: program %u is unsupported by server %s\n",
1327 (unsigned int)task->tk_client->cl_prog,
1328 task->tk_client->cl_server);
1329 error = -EPFNOSUPPORT;
1331 case RPC_PROG_MISMATCH:
1332 dprintk("RPC: call_verify: program %u, version %u unsupported by server %s\n",
1333 (unsigned int)task->tk_client->cl_prog,
1334 (unsigned int)task->tk_client->cl_vers,
1335 task->tk_client->cl_server);
1336 error = -EPROTONOSUPPORT;
1338 case RPC_PROC_UNAVAIL:
1339 dprintk("RPC: call_verify: proc %p unsupported by program %u, version %u on server %s\n",
1340 task->tk_msg.rpc_proc,
1341 task->tk_client->cl_prog,
1342 task->tk_client->cl_vers,
1343 task->tk_client->cl_server);
1344 error = -EOPNOTSUPP;
1346 case RPC_GARBAGE_ARGS:
1347 dprintk("RPC: %4d %s: server saw garbage\n", task->tk_pid, __FUNCTION__);
1350 printk(KERN_WARNING "call_verify: server accept status: %x\n", n);
1355 task->tk_client->cl_stats->rpcgarbage++;
1356 if (task->tk_garb_retry) {
1357 task->tk_garb_retry--;
1358 dprintk("RPC %s: retrying %4d\n", __FUNCTION__, task->tk_pid);
1359 task->tk_action = call_bind;
1361 return ERR_PTR(-EAGAIN);
1363 printk(KERN_WARNING "RPC %s: retry failed, exit EIO\n", __FUNCTION__);
1367 rpc_exit(task, error);
1368 return ERR_PTR(error);
1370 printk(KERN_WARNING "RPC %s: server reply was truncated.\n", __FUNCTION__);
1374 static int rpcproc_encode_null(void *rqstp, __be32 *data, void *obj)
1379 static int rpcproc_decode_null(void *rqstp, __be32 *data, void *obj)
1384 static struct rpc_procinfo rpcproc_null = {
1385 .p_encode = rpcproc_encode_null,
1386 .p_decode = rpcproc_decode_null,
1389 int rpc_ping(struct rpc_clnt *clnt, int flags)
1391 struct rpc_message msg = {
1392 .rpc_proc = &rpcproc_null,
1395 msg.rpc_cred = authnull_ops.lookup_cred(NULL, NULL, 0);
1396 err = rpc_call_sync(clnt, &msg, flags);
1397 put_rpccred(msg.rpc_cred);