sync hh.org
[hh.org.git] / net / sunrpc / clnt.c
blobdfeea4fea95a7f6dfb4ea038491aaba39ced48dc
1 /*
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>
28 #include <linux/mm.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 */
40 #ifdef RPC_DEBUG
41 # define RPCDBG_FACILITY RPCDBG_CALL
42 #endif
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);
67 static int
68 rpc_setup_pipedir(struct rpc_clnt *clnt, char *dir_name)
70 static uint32_t clntid;
71 int error;
73 clnt->cl_vfsmnt = ERR_PTR(-ENOENT);
74 clnt->cl_dentry = ERR_PTR(-ENOENT);
75 if (dir_name == NULL)
76 return 0;
78 clnt->cl_vfsmnt = rpc_get_mount();
79 if (IS_ERR(clnt->cl_vfsmnt))
80 return PTR_ERR(clnt->cl_vfsmnt);
82 for (;;) {
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))
89 return 0;
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);
94 rpc_put_mount();
95 return 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;
105 int err;
106 int len;
108 dprintk("RPC: creating %s client for %s (xprt %p)\n",
109 program->name, servname, xprt);
111 err = -EINVAL;
112 if (!xprt)
113 goto out_no_xprt;
114 if (vers >= program->nrvers || !(version = program->version[vers]))
115 goto out_err;
117 err = -ENOMEM;
118 clnt = kzalloc(sizeof(*clnt), GFP_KERNEL);
119 if (!clnt)
120 goto out_err;
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);
129 if (buf != 0)
130 clnt->cl_server = buf;
131 else
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);
152 if (err < 0)
153 goto out_no_path;
155 auth = rpcauth_create(flavor, clnt);
156 if (IS_ERR(auth)) {
157 printk(KERN_INFO "RPC: Couldn't create auth handle (flavor %u)\n",
158 flavor);
159 err = PTR_ERR(auth);
160 goto out_no_auth;
163 /* save the nodename */
164 clnt->cl_nodelen = strlen(utsname()->nodename);
165 if (clnt->cl_nodelen > UNX_MAXNODENAME)
166 clnt->cl_nodelen = UNX_MAXNODENAME;
167 memcpy(clnt->cl_nodename, utsname()->nodename, clnt->cl_nodelen);
168 return clnt;
170 out_no_auth:
171 if (!IS_ERR(clnt->cl_dentry)) {
172 rpc_rmdir(clnt->cl_dentry);
173 rpc_put_mount();
175 out_no_path:
176 if (clnt->cl_server != clnt->cl_inline_name)
177 kfree(clnt->cl_server);
178 kfree(clnt);
179 out_err:
180 xprt_put(xprt);
181 out_no_xprt:
182 return ERR_PTR(err);
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);
202 if (IS_ERR(xprt))
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
209 * operation.
211 xprt->resvport = 1;
212 if (args->flags & RPC_CLNT_CREATE_NONPRIVPORT)
213 xprt->resvport = 0;
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);
220 if (IS_ERR(clnt))
221 return clnt;
223 if (!(args->flags & RPC_CLNT_CREATE_NOPING)) {
224 int err = rpc_ping(clnt, RPC_TASK_SOFT|RPC_TASK_NOINTR);
225 if (err != 0) {
226 rpc_shutdown_client(clnt);
227 return ERR_PTR(err);
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)
236 clnt->cl_intr = 1;
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;
242 return clnt;
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
249 * flavour.
251 struct rpc_clnt *
252 rpc_clone_client(struct rpc_clnt *clnt)
254 struct rpc_clnt *new;
256 new = kmemdup(clnt, sizeof(*new), GFP_KERNEL);
257 if (!new)
258 goto out_no_clnt;
259 atomic_set(&new->cl_count, 1);
260 atomic_set(&new->cl_users, 0);
261 new->cl_parent = clnt;
262 atomic_inc(&clnt->cl_count);
263 new->cl_xprt = xprt_get(clnt->cl_xprt);
264 /* Turn off autobind on clones */
265 new->cl_autobind = 0;
266 new->cl_oneshot = 0;
267 new->cl_dead = 0;
268 if (!IS_ERR(new->cl_dentry))
269 dget(new->cl_dentry);
270 rpc_init_rtt(&new->cl_rtt_default, clnt->cl_xprt->timeout.to_initval);
271 if (new->cl_auth)
272 atomic_inc(&new->cl_auth->au_count);
273 new->cl_metrics = rpc_alloc_iostats(clnt);
274 return new;
275 out_no_clnt:
276 printk(KERN_INFO "RPC: out of memory in %s\n", __FUNCTION__);
277 return ERR_PTR(-ENOMEM);
281 * Properly shut down an RPC client, terminating all outstanding
282 * requests. Note that we must be certain that cl_oneshot and
283 * cl_dead are cleared, or else the client would be destroyed
284 * when the last task releases it.
287 rpc_shutdown_client(struct rpc_clnt *clnt)
289 dprintk("RPC: shutting down %s client for %s, tasks=%d\n",
290 clnt->cl_protname, clnt->cl_server,
291 atomic_read(&clnt->cl_users));
293 while (atomic_read(&clnt->cl_users) > 0) {
294 /* Don't let rpc_release_client destroy us */
295 clnt->cl_oneshot = 0;
296 clnt->cl_dead = 0;
297 rpc_killall_tasks(clnt);
298 wait_event_timeout(destroy_wait,
299 !atomic_read(&clnt->cl_users), 1*HZ);
302 if (atomic_read(&clnt->cl_users) < 0) {
303 printk(KERN_ERR "RPC: rpc_shutdown_client clnt %p tasks=%d\n",
304 clnt, atomic_read(&clnt->cl_users));
305 #ifdef RPC_DEBUG
306 rpc_show_tasks();
307 #endif
308 BUG();
311 return rpc_destroy_client(clnt);
315 * Delete an RPC client
318 rpc_destroy_client(struct rpc_clnt *clnt)
320 if (!atomic_dec_and_test(&clnt->cl_count))
321 return 1;
322 BUG_ON(atomic_read(&clnt->cl_users) != 0);
324 dprintk("RPC: destroying %s client for %s\n",
325 clnt->cl_protname, clnt->cl_server);
326 if (clnt->cl_auth) {
327 rpcauth_destroy(clnt->cl_auth);
328 clnt->cl_auth = NULL;
330 if (clnt->cl_parent != clnt) {
331 if (!IS_ERR(clnt->cl_dentry))
332 dput(clnt->cl_dentry);
333 rpc_destroy_client(clnt->cl_parent);
334 goto out_free;
336 if (!IS_ERR(clnt->cl_dentry)) {
337 rpc_rmdir(clnt->cl_dentry);
338 rpc_put_mount();
340 if (clnt->cl_server != clnt->cl_inline_name)
341 kfree(clnt->cl_server);
342 out_free:
343 rpc_free_iostats(clnt->cl_metrics);
344 clnt->cl_metrics = NULL;
345 xprt_put(clnt->cl_xprt);
346 kfree(clnt);
347 return 0;
351 * Release an RPC client
353 void
354 rpc_release_client(struct rpc_clnt *clnt)
356 dprintk("RPC: rpc_release_client(%p, %d)\n",
357 clnt, atomic_read(&clnt->cl_users));
359 if (!atomic_dec_and_test(&clnt->cl_users))
360 return;
361 wake_up(&destroy_wait);
362 if (clnt->cl_oneshot || clnt->cl_dead)
363 rpc_destroy_client(clnt);
367 * rpc_bind_new_program - bind a new RPC program to an existing client
368 * @old - old rpc_client
369 * @program - rpc program to set
370 * @vers - rpc program version
372 * Clones the rpc client and sets up a new RPC program. This is mainly
373 * of use for enabling different RPC programs to share the same transport.
374 * The Sun NFSv2/v3 ACL protocol can do this.
376 struct rpc_clnt *rpc_bind_new_program(struct rpc_clnt *old,
377 struct rpc_program *program,
378 int vers)
380 struct rpc_clnt *clnt;
381 struct rpc_version *version;
382 int err;
384 BUG_ON(vers >= program->nrvers || !program->version[vers]);
385 version = program->version[vers];
386 clnt = rpc_clone_client(old);
387 if (IS_ERR(clnt))
388 goto out;
389 clnt->cl_procinfo = version->procs;
390 clnt->cl_maxproc = version->nrprocs;
391 clnt->cl_protname = program->name;
392 clnt->cl_prog = program->number;
393 clnt->cl_vers = version->number;
394 clnt->cl_stats = program->stats;
395 err = rpc_ping(clnt, RPC_TASK_SOFT|RPC_TASK_NOINTR);
396 if (err != 0) {
397 rpc_shutdown_client(clnt);
398 clnt = ERR_PTR(err);
400 out:
401 return clnt;
405 * Default callback for async RPC calls
407 static void
408 rpc_default_callback(struct rpc_task *task, void *data)
412 static const struct rpc_call_ops rpc_default_ops = {
413 .rpc_call_done = rpc_default_callback,
417 * Export the signal mask handling for synchronous code that
418 * sleeps on RPC calls
420 #define RPC_INTR_SIGNALS (sigmask(SIGHUP) | sigmask(SIGINT) | sigmask(SIGQUIT) | sigmask(SIGTERM))
422 static void rpc_save_sigmask(sigset_t *oldset, int intr)
424 unsigned long sigallow = sigmask(SIGKILL);
425 sigset_t sigmask;
427 /* Block all signals except those listed in sigallow */
428 if (intr)
429 sigallow |= RPC_INTR_SIGNALS;
430 siginitsetinv(&sigmask, sigallow);
431 sigprocmask(SIG_BLOCK, &sigmask, oldset);
434 static inline void rpc_task_sigmask(struct rpc_task *task, sigset_t *oldset)
436 rpc_save_sigmask(oldset, !RPC_TASK_UNINTERRUPTIBLE(task));
439 static inline void rpc_restore_sigmask(sigset_t *oldset)
441 sigprocmask(SIG_SETMASK, oldset, NULL);
444 void rpc_clnt_sigmask(struct rpc_clnt *clnt, sigset_t *oldset)
446 rpc_save_sigmask(oldset, clnt->cl_intr);
449 void rpc_clnt_sigunmask(struct rpc_clnt *clnt, sigset_t *oldset)
451 rpc_restore_sigmask(oldset);
455 * New rpc_call implementation
457 int rpc_call_sync(struct rpc_clnt *clnt, struct rpc_message *msg, int flags)
459 struct rpc_task *task;
460 sigset_t oldset;
461 int status;
463 /* If this client is slain all further I/O fails */
464 if (clnt->cl_dead)
465 return -EIO;
467 BUG_ON(flags & RPC_TASK_ASYNC);
469 status = -ENOMEM;
470 task = rpc_new_task(clnt, flags, &rpc_default_ops, NULL);
471 if (task == NULL)
472 goto out;
474 /* Mask signals on RPC calls _and_ GSS_AUTH upcalls */
475 rpc_task_sigmask(task, &oldset);
477 rpc_call_setup(task, msg, 0);
479 /* Set up the call info struct and execute the task */
480 status = task->tk_status;
481 if (status == 0) {
482 atomic_inc(&task->tk_count);
483 status = rpc_execute(task);
484 if (status == 0)
485 status = task->tk_status;
487 rpc_restore_sigmask(&oldset);
488 rpc_release_task(task);
489 out:
490 return status;
494 * New rpc_call implementation
497 rpc_call_async(struct rpc_clnt *clnt, struct rpc_message *msg, int flags,
498 const struct rpc_call_ops *tk_ops, void *data)
500 struct rpc_task *task;
501 sigset_t oldset;
502 int status;
504 /* If this client is slain all further I/O fails */
505 status = -EIO;
506 if (clnt->cl_dead)
507 goto out_release;
509 flags |= RPC_TASK_ASYNC;
511 /* Create/initialize a new RPC task */
512 status = -ENOMEM;
513 if (!(task = rpc_new_task(clnt, flags, tk_ops, data)))
514 goto out_release;
516 /* Mask signals on GSS_AUTH upcalls */
517 rpc_task_sigmask(task, &oldset);
519 rpc_call_setup(task, msg, 0);
521 /* Set up the call info struct and execute the task */
522 status = task->tk_status;
523 if (status == 0)
524 rpc_execute(task);
525 else
526 rpc_release_task(task);
528 rpc_restore_sigmask(&oldset);
529 return status;
530 out_release:
531 if (tk_ops->rpc_release != NULL)
532 tk_ops->rpc_release(data);
533 return status;
537 void
538 rpc_call_setup(struct rpc_task *task, struct rpc_message *msg, int flags)
540 task->tk_msg = *msg;
541 task->tk_flags |= flags;
542 /* Bind the user cred */
543 if (task->tk_msg.rpc_cred != NULL)
544 rpcauth_holdcred(task);
545 else
546 rpcauth_bindcred(task);
548 if (task->tk_status == 0)
549 task->tk_action = call_start;
550 else
551 task->tk_action = rpc_exit_task;
555 * rpc_peeraddr - extract remote peer address from clnt's xprt
556 * @clnt: RPC client structure
557 * @buf: target buffer
558 * @size: length of target buffer
560 * Returns the number of bytes that are actually in the stored address.
562 size_t rpc_peeraddr(struct rpc_clnt *clnt, struct sockaddr *buf, size_t bufsize)
564 size_t bytes;
565 struct rpc_xprt *xprt = clnt->cl_xprt;
567 bytes = sizeof(xprt->addr);
568 if (bytes > bufsize)
569 bytes = bufsize;
570 memcpy(buf, &clnt->cl_xprt->addr, bytes);
571 return xprt->addrlen;
573 EXPORT_SYMBOL_GPL(rpc_peeraddr);
576 * rpc_peeraddr2str - return remote peer address in printable format
577 * @clnt: RPC client structure
578 * @format: address format
581 char *rpc_peeraddr2str(struct rpc_clnt *clnt, enum rpc_display_format_t format)
583 struct rpc_xprt *xprt = clnt->cl_xprt;
584 return xprt->ops->print_addr(xprt, format);
586 EXPORT_SYMBOL_GPL(rpc_peeraddr2str);
588 void
589 rpc_setbufsize(struct rpc_clnt *clnt, unsigned int sndsize, unsigned int rcvsize)
591 struct rpc_xprt *xprt = clnt->cl_xprt;
592 if (xprt->ops->set_buffer_size)
593 xprt->ops->set_buffer_size(xprt, sndsize, rcvsize);
597 * Return size of largest payload RPC client can support, in bytes
599 * For stream transports, this is one RPC record fragment (see RFC
600 * 1831), as we don't support multi-record requests yet. For datagram
601 * transports, this is the size of an IP packet minus the IP, UDP, and
602 * RPC header sizes.
604 size_t rpc_max_payload(struct rpc_clnt *clnt)
606 return clnt->cl_xprt->max_payload;
608 EXPORT_SYMBOL_GPL(rpc_max_payload);
611 * rpc_force_rebind - force transport to check that remote port is unchanged
612 * @clnt: client to rebind
615 void rpc_force_rebind(struct rpc_clnt *clnt)
617 if (clnt->cl_autobind)
618 xprt_clear_bound(clnt->cl_xprt);
620 EXPORT_SYMBOL_GPL(rpc_force_rebind);
623 * Restart an (async) RPC call. Usually called from within the
624 * exit handler.
626 void
627 rpc_restart_call(struct rpc_task *task)
629 if (RPC_ASSASSINATED(task))
630 return;
632 task->tk_action = call_start;
636 * 0. Initial state
638 * Other FSM states can be visited zero or more times, but
639 * this state is visited exactly once for each RPC.
641 static void
642 call_start(struct rpc_task *task)
644 struct rpc_clnt *clnt = task->tk_client;
646 dprintk("RPC: %4d call_start %s%d proc %d (%s)\n", task->tk_pid,
647 clnt->cl_protname, clnt->cl_vers, task->tk_msg.rpc_proc->p_proc,
648 (RPC_IS_ASYNC(task) ? "async" : "sync"));
650 /* Increment call count */
651 task->tk_msg.rpc_proc->p_count++;
652 clnt->cl_stats->rpccnt++;
653 task->tk_action = call_reserve;
657 * 1. Reserve an RPC call slot
659 static void
660 call_reserve(struct rpc_task *task)
662 dprintk("RPC: %4d call_reserve\n", task->tk_pid);
664 if (!rpcauth_uptodatecred(task)) {
665 task->tk_action = call_refresh;
666 return;
669 task->tk_status = 0;
670 task->tk_action = call_reserveresult;
671 xprt_reserve(task);
675 * 1b. Grok the result of xprt_reserve()
677 static void
678 call_reserveresult(struct rpc_task *task)
680 int status = task->tk_status;
682 dprintk("RPC: %4d call_reserveresult (status %d)\n",
683 task->tk_pid, task->tk_status);
686 * After a call to xprt_reserve(), we must have either
687 * a request slot or else an error status.
689 task->tk_status = 0;
690 if (status >= 0) {
691 if (task->tk_rqstp) {
692 task->tk_action = call_allocate;
693 return;
696 printk(KERN_ERR "%s: status=%d, but no request slot, exiting\n",
697 __FUNCTION__, status);
698 rpc_exit(task, -EIO);
699 return;
703 * Even though there was an error, we may have acquired
704 * a request slot somehow. Make sure not to leak it.
706 if (task->tk_rqstp) {
707 printk(KERN_ERR "%s: status=%d, request allocated anyway\n",
708 __FUNCTION__, status);
709 xprt_release(task);
712 switch (status) {
713 case -EAGAIN: /* woken up; retry */
714 task->tk_action = call_reserve;
715 return;
716 case -EIO: /* probably a shutdown */
717 break;
718 default:
719 printk(KERN_ERR "%s: unrecognized error %d, exiting\n",
720 __FUNCTION__, status);
721 break;
723 rpc_exit(task, status);
727 * 2. Allocate the buffer. For details, see sched.c:rpc_malloc.
728 * (Note: buffer memory is freed in xprt_release).
730 static void
731 call_allocate(struct rpc_task *task)
733 struct rpc_rqst *req = task->tk_rqstp;
734 struct rpc_xprt *xprt = task->tk_xprt;
735 unsigned int bufsiz;
737 dprintk("RPC: %4d call_allocate (status %d)\n",
738 task->tk_pid, task->tk_status);
739 task->tk_action = call_bind;
740 if (req->rq_buffer)
741 return;
743 /* FIXME: compute buffer requirements more exactly using
744 * auth->au_wslack */
745 bufsiz = task->tk_msg.rpc_proc->p_bufsiz + RPC_SLACK_SPACE;
747 if (xprt->ops->buf_alloc(task, bufsiz << 1) != NULL)
748 return;
749 printk(KERN_INFO "RPC: buffer allocation failed for task %p\n", task);
751 if (RPC_IS_ASYNC(task) || !signalled()) {
752 xprt_release(task);
753 task->tk_action = call_reserve;
754 rpc_delay(task, HZ>>4);
755 return;
758 rpc_exit(task, -ERESTARTSYS);
761 static inline int
762 rpc_task_need_encode(struct rpc_task *task)
764 return task->tk_rqstp->rq_snd_buf.len == 0;
767 static inline void
768 rpc_task_force_reencode(struct rpc_task *task)
770 task->tk_rqstp->rq_snd_buf.len = 0;
774 * 3. Encode arguments of an RPC call
776 static void
777 call_encode(struct rpc_task *task)
779 struct rpc_rqst *req = task->tk_rqstp;
780 struct xdr_buf *sndbuf = &req->rq_snd_buf;
781 struct xdr_buf *rcvbuf = &req->rq_rcv_buf;
782 unsigned int bufsiz;
783 kxdrproc_t encode;
784 __be32 *p;
786 dprintk("RPC: %4d call_encode (status %d)\n",
787 task->tk_pid, task->tk_status);
789 /* Default buffer setup */
790 bufsiz = req->rq_bufsize >> 1;
791 sndbuf->head[0].iov_base = (void *)req->rq_buffer;
792 sndbuf->head[0].iov_len = bufsiz;
793 sndbuf->tail[0].iov_len = 0;
794 sndbuf->page_len = 0;
795 sndbuf->len = 0;
796 sndbuf->buflen = bufsiz;
797 rcvbuf->head[0].iov_base = (void *)((char *)req->rq_buffer + bufsiz);
798 rcvbuf->head[0].iov_len = bufsiz;
799 rcvbuf->tail[0].iov_len = 0;
800 rcvbuf->page_len = 0;
801 rcvbuf->len = 0;
802 rcvbuf->buflen = bufsiz;
804 /* Encode header and provided arguments */
805 encode = task->tk_msg.rpc_proc->p_encode;
806 if (!(p = call_header(task))) {
807 printk(KERN_INFO "RPC: call_header failed, exit EIO\n");
808 rpc_exit(task, -EIO);
809 return;
811 if (encode == NULL)
812 return;
814 task->tk_status = rpcauth_wrap_req(task, encode, req, p,
815 task->tk_msg.rpc_argp);
816 if (task->tk_status == -ENOMEM) {
817 /* XXX: Is this sane? */
818 rpc_delay(task, 3*HZ);
819 task->tk_status = -EAGAIN;
824 * 4. Get the server port number if not yet set
826 static void
827 call_bind(struct rpc_task *task)
829 struct rpc_xprt *xprt = task->tk_xprt;
831 dprintk("RPC: %4d call_bind (status %d)\n",
832 task->tk_pid, task->tk_status);
834 task->tk_action = call_connect;
835 if (!xprt_bound(xprt)) {
836 task->tk_action = call_bind_status;
837 task->tk_timeout = xprt->bind_timeout;
838 xprt->ops->rpcbind(task);
843 * 4a. Sort out bind result
845 static void
846 call_bind_status(struct rpc_task *task)
848 int status = -EACCES;
850 if (task->tk_status >= 0) {
851 dprintk("RPC: %4d call_bind_status (status %d)\n",
852 task->tk_pid, task->tk_status);
853 task->tk_status = 0;
854 task->tk_action = call_connect;
855 return;
858 switch (task->tk_status) {
859 case -EACCES:
860 dprintk("RPC: %4d remote rpcbind: RPC program/version unavailable\n",
861 task->tk_pid);
862 rpc_delay(task, 3*HZ);
863 goto retry_timeout;
864 case -ETIMEDOUT:
865 dprintk("RPC: %4d rpcbind request timed out\n",
866 task->tk_pid);
867 goto retry_timeout;
868 case -EPFNOSUPPORT:
869 dprintk("RPC: %4d remote rpcbind service unavailable\n",
870 task->tk_pid);
871 break;
872 case -EPROTONOSUPPORT:
873 dprintk("RPC: %4d remote rpcbind version 2 unavailable\n",
874 task->tk_pid);
875 break;
876 default:
877 dprintk("RPC: %4d unrecognized rpcbind error (%d)\n",
878 task->tk_pid, -task->tk_status);
879 status = -EIO;
882 rpc_exit(task, status);
883 return;
885 retry_timeout:
886 task->tk_action = call_timeout;
890 * 4b. Connect to the RPC server
892 static void
893 call_connect(struct rpc_task *task)
895 struct rpc_xprt *xprt = task->tk_xprt;
897 dprintk("RPC: %4d call_connect xprt %p %s connected\n",
898 task->tk_pid, xprt,
899 (xprt_connected(xprt) ? "is" : "is not"));
901 task->tk_action = call_transmit;
902 if (!xprt_connected(xprt)) {
903 task->tk_action = call_connect_status;
904 if (task->tk_status < 0)
905 return;
906 xprt_connect(task);
911 * 4c. Sort out connect result
913 static void
914 call_connect_status(struct rpc_task *task)
916 struct rpc_clnt *clnt = task->tk_client;
917 int status = task->tk_status;
919 dprintk("RPC: %5u call_connect_status (status %d)\n",
920 task->tk_pid, task->tk_status);
922 task->tk_status = 0;
923 if (status >= 0) {
924 clnt->cl_stats->netreconn++;
925 task->tk_action = call_transmit;
926 return;
929 /* Something failed: remote service port may have changed */
930 rpc_force_rebind(clnt);
932 switch (status) {
933 case -ENOTCONN:
934 case -EAGAIN:
935 task->tk_action = call_bind;
936 if (!RPC_IS_SOFT(task))
937 return;
938 /* if soft mounted, test if we've timed out */
939 case -ETIMEDOUT:
940 task->tk_action = call_timeout;
941 return;
943 rpc_exit(task, -EIO);
947 * 5. Transmit the RPC request, and wait for reply
949 static void
950 call_transmit(struct rpc_task *task)
952 dprintk("RPC: %4d call_transmit (status %d)\n",
953 task->tk_pid, task->tk_status);
955 task->tk_action = call_status;
956 if (task->tk_status < 0)
957 return;
958 task->tk_status = xprt_prepare_transmit(task);
959 if (task->tk_status != 0)
960 return;
961 task->tk_action = call_transmit_status;
962 /* Encode here so that rpcsec_gss can use correct sequence number. */
963 if (rpc_task_need_encode(task)) {
964 BUG_ON(task->tk_rqstp->rq_bytes_sent != 0);
965 call_encode(task);
966 /* Did the encode result in an error condition? */
967 if (task->tk_status != 0)
968 return;
970 xprt_transmit(task);
971 if (task->tk_status < 0)
972 return;
974 * On success, ensure that we call xprt_end_transmit() before sleeping
975 * in order to allow access to the socket to other RPC requests.
977 call_transmit_status(task);
978 if (task->tk_msg.rpc_proc->p_decode != NULL)
979 return;
980 task->tk_action = rpc_exit_task;
981 rpc_wake_up_task(task);
985 * 5a. Handle cleanup after a transmission
987 static void
988 call_transmit_status(struct rpc_task *task)
990 task->tk_action = call_status;
992 * Special case: if we've been waiting on the socket's write_space()
993 * callback, then don't call xprt_end_transmit().
995 if (task->tk_status == -EAGAIN)
996 return;
997 xprt_end_transmit(task);
998 rpc_task_force_reencode(task);
1002 * 6. Sort out the RPC call status
1004 static void
1005 call_status(struct rpc_task *task)
1007 struct rpc_clnt *clnt = task->tk_client;
1008 struct rpc_rqst *req = task->tk_rqstp;
1009 int status;
1011 if (req->rq_received > 0 && !req->rq_bytes_sent)
1012 task->tk_status = req->rq_received;
1014 dprintk("RPC: %4d call_status (status %d)\n",
1015 task->tk_pid, task->tk_status);
1017 status = task->tk_status;
1018 if (status >= 0) {
1019 task->tk_action = call_decode;
1020 return;
1023 task->tk_status = 0;
1024 switch(status) {
1025 case -EHOSTDOWN:
1026 case -EHOSTUNREACH:
1027 case -ENETUNREACH:
1029 * Delay any retries for 3 seconds, then handle as if it
1030 * were a timeout.
1032 rpc_delay(task, 3*HZ);
1033 case -ETIMEDOUT:
1034 task->tk_action = call_timeout;
1035 break;
1036 case -ECONNREFUSED:
1037 case -ENOTCONN:
1038 rpc_force_rebind(clnt);
1039 task->tk_action = call_bind;
1040 break;
1041 case -EAGAIN:
1042 task->tk_action = call_transmit;
1043 break;
1044 case -EIO:
1045 /* shutdown or soft timeout */
1046 rpc_exit(task, status);
1047 break;
1048 default:
1049 printk("%s: RPC call returned error %d\n",
1050 clnt->cl_protname, -status);
1051 rpc_exit(task, status);
1056 * 6a. Handle RPC timeout
1057 * We do not release the request slot, so we keep using the
1058 * same XID for all retransmits.
1060 static void
1061 call_timeout(struct rpc_task *task)
1063 struct rpc_clnt *clnt = task->tk_client;
1065 if (xprt_adjust_timeout(task->tk_rqstp) == 0) {
1066 dprintk("RPC: %4d call_timeout (minor)\n", task->tk_pid);
1067 goto retry;
1070 dprintk("RPC: %4d call_timeout (major)\n", task->tk_pid);
1071 task->tk_timeouts++;
1073 if (RPC_IS_SOFT(task)) {
1074 printk(KERN_NOTICE "%s: server %s not responding, timed out\n",
1075 clnt->cl_protname, clnt->cl_server);
1076 rpc_exit(task, -EIO);
1077 return;
1080 if (!(task->tk_flags & RPC_CALL_MAJORSEEN)) {
1081 task->tk_flags |= RPC_CALL_MAJORSEEN;
1082 printk(KERN_NOTICE "%s: server %s not responding, still trying\n",
1083 clnt->cl_protname, clnt->cl_server);
1085 rpc_force_rebind(clnt);
1087 retry:
1088 clnt->cl_stats->rpcretrans++;
1089 task->tk_action = call_bind;
1090 task->tk_status = 0;
1094 * 7. Decode the RPC reply
1096 static void
1097 call_decode(struct rpc_task *task)
1099 struct rpc_clnt *clnt = task->tk_client;
1100 struct rpc_rqst *req = task->tk_rqstp;
1101 kxdrproc_t decode = task->tk_msg.rpc_proc->p_decode;
1102 __be32 *p;
1104 dprintk("RPC: %4d call_decode (status %d)\n",
1105 task->tk_pid, task->tk_status);
1107 if (task->tk_flags & RPC_CALL_MAJORSEEN) {
1108 printk(KERN_NOTICE "%s: server %s OK\n",
1109 clnt->cl_protname, clnt->cl_server);
1110 task->tk_flags &= ~RPC_CALL_MAJORSEEN;
1113 if (task->tk_status < 12) {
1114 if (!RPC_IS_SOFT(task)) {
1115 task->tk_action = call_bind;
1116 clnt->cl_stats->rpcretrans++;
1117 goto out_retry;
1119 dprintk("%s: too small RPC reply size (%d bytes)\n",
1120 clnt->cl_protname, task->tk_status);
1121 task->tk_action = call_timeout;
1122 goto out_retry;
1126 * Ensure that we see all writes made by xprt_complete_rqst()
1127 * before it changed req->rq_received.
1129 smp_rmb();
1130 req->rq_rcv_buf.len = req->rq_private_buf.len;
1132 /* Check that the softirq receive buffer is valid */
1133 WARN_ON(memcmp(&req->rq_rcv_buf, &req->rq_private_buf,
1134 sizeof(req->rq_rcv_buf)) != 0);
1136 /* Verify the RPC header */
1137 p = call_verify(task);
1138 if (IS_ERR(p)) {
1139 if (p == ERR_PTR(-EAGAIN))
1140 goto out_retry;
1141 return;
1144 task->tk_action = rpc_exit_task;
1146 if (decode)
1147 task->tk_status = rpcauth_unwrap_resp(task, decode, req, p,
1148 task->tk_msg.rpc_resp);
1149 dprintk("RPC: %4d call_decode result %d\n", task->tk_pid,
1150 task->tk_status);
1151 return;
1152 out_retry:
1153 req->rq_received = req->rq_private_buf.len = 0;
1154 task->tk_status = 0;
1158 * 8. Refresh the credentials if rejected by the server
1160 static void
1161 call_refresh(struct rpc_task *task)
1163 dprintk("RPC: %4d call_refresh\n", task->tk_pid);
1165 xprt_release(task); /* Must do to obtain new XID */
1166 task->tk_action = call_refreshresult;
1167 task->tk_status = 0;
1168 task->tk_client->cl_stats->rpcauthrefresh++;
1169 rpcauth_refreshcred(task);
1173 * 8a. Process the results of a credential refresh
1175 static void
1176 call_refreshresult(struct rpc_task *task)
1178 int status = task->tk_status;
1179 dprintk("RPC: %4d call_refreshresult (status %d)\n",
1180 task->tk_pid, task->tk_status);
1182 task->tk_status = 0;
1183 task->tk_action = call_reserve;
1184 if (status >= 0 && rpcauth_uptodatecred(task))
1185 return;
1186 if (status == -EACCES) {
1187 rpc_exit(task, -EACCES);
1188 return;
1190 task->tk_action = call_refresh;
1191 if (status != -ETIMEDOUT)
1192 rpc_delay(task, 3*HZ);
1193 return;
1197 * Call header serialization
1199 static __be32 *
1200 call_header(struct rpc_task *task)
1202 struct rpc_clnt *clnt = task->tk_client;
1203 struct rpc_rqst *req = task->tk_rqstp;
1204 __be32 *p = req->rq_svec[0].iov_base;
1206 /* FIXME: check buffer size? */
1208 p = xprt_skip_transport_header(task->tk_xprt, p);
1209 *p++ = req->rq_xid; /* XID */
1210 *p++ = htonl(RPC_CALL); /* CALL */
1211 *p++ = htonl(RPC_VERSION); /* RPC version */
1212 *p++ = htonl(clnt->cl_prog); /* program number */
1213 *p++ = htonl(clnt->cl_vers); /* program version */
1214 *p++ = htonl(task->tk_msg.rpc_proc->p_proc); /* procedure */
1215 p = rpcauth_marshcred(task, p);
1216 req->rq_slen = xdr_adjust_iovec(&req->rq_svec[0], p);
1217 return p;
1221 * Reply header verification
1223 static __be32 *
1224 call_verify(struct rpc_task *task)
1226 struct kvec *iov = &task->tk_rqstp->rq_rcv_buf.head[0];
1227 int len = task->tk_rqstp->rq_rcv_buf.len >> 2;
1228 __be32 *p = iov->iov_base;
1229 u32 n;
1230 int error = -EACCES;
1232 if ((task->tk_rqstp->rq_rcv_buf.len & 3) != 0) {
1233 /* RFC-1014 says that the representation of XDR data must be a
1234 * multiple of four bytes
1235 * - if it isn't pointer subtraction in the NFS client may give
1236 * undefined results
1238 printk(KERN_WARNING
1239 "call_verify: XDR representation not a multiple of"
1240 " 4 bytes: 0x%x\n", task->tk_rqstp->rq_rcv_buf.len);
1241 goto out_eio;
1243 if ((len -= 3) < 0)
1244 goto out_overflow;
1245 p += 1; /* skip XID */
1247 if ((n = ntohl(*p++)) != RPC_REPLY) {
1248 printk(KERN_WARNING "call_verify: not an RPC reply: %x\n", n);
1249 goto out_garbage;
1251 if ((n = ntohl(*p++)) != RPC_MSG_ACCEPTED) {
1252 if (--len < 0)
1253 goto out_overflow;
1254 switch ((n = ntohl(*p++))) {
1255 case RPC_AUTH_ERROR:
1256 break;
1257 case RPC_MISMATCH:
1258 dprintk("%s: RPC call version mismatch!\n", __FUNCTION__);
1259 error = -EPROTONOSUPPORT;
1260 goto out_err;
1261 default:
1262 dprintk("%s: RPC call rejected, unknown error: %x\n", __FUNCTION__, n);
1263 goto out_eio;
1265 if (--len < 0)
1266 goto out_overflow;
1267 switch ((n = ntohl(*p++))) {
1268 case RPC_AUTH_REJECTEDCRED:
1269 case RPC_AUTH_REJECTEDVERF:
1270 case RPCSEC_GSS_CREDPROBLEM:
1271 case RPCSEC_GSS_CTXPROBLEM:
1272 if (!task->tk_cred_retry)
1273 break;
1274 task->tk_cred_retry--;
1275 dprintk("RPC: %4d call_verify: retry stale creds\n",
1276 task->tk_pid);
1277 rpcauth_invalcred(task);
1278 task->tk_action = call_refresh;
1279 goto out_retry;
1280 case RPC_AUTH_BADCRED:
1281 case RPC_AUTH_BADVERF:
1282 /* possibly garbled cred/verf? */
1283 if (!task->tk_garb_retry)
1284 break;
1285 task->tk_garb_retry--;
1286 dprintk("RPC: %4d call_verify: retry garbled creds\n",
1287 task->tk_pid);
1288 task->tk_action = call_bind;
1289 goto out_retry;
1290 case RPC_AUTH_TOOWEAK:
1291 printk(KERN_NOTICE "call_verify: server %s requires stronger "
1292 "authentication.\n", task->tk_client->cl_server);
1293 break;
1294 default:
1295 printk(KERN_WARNING "call_verify: unknown auth error: %x\n", n);
1296 error = -EIO;
1298 dprintk("RPC: %4d call_verify: call rejected %d\n",
1299 task->tk_pid, n);
1300 goto out_err;
1302 if (!(p = rpcauth_checkverf(task, p))) {
1303 printk(KERN_WARNING "call_verify: auth check failed\n");
1304 goto out_garbage; /* bad verifier, retry */
1306 len = p - (__be32 *)iov->iov_base - 1;
1307 if (len < 0)
1308 goto out_overflow;
1309 switch ((n = ntohl(*p++))) {
1310 case RPC_SUCCESS:
1311 return p;
1312 case RPC_PROG_UNAVAIL:
1313 dprintk("RPC: call_verify: program %u is unsupported by server %s\n",
1314 (unsigned int)task->tk_client->cl_prog,
1315 task->tk_client->cl_server);
1316 error = -EPFNOSUPPORT;
1317 goto out_err;
1318 case RPC_PROG_MISMATCH:
1319 dprintk("RPC: call_verify: program %u, version %u unsupported by server %s\n",
1320 (unsigned int)task->tk_client->cl_prog,
1321 (unsigned int)task->tk_client->cl_vers,
1322 task->tk_client->cl_server);
1323 error = -EPROTONOSUPPORT;
1324 goto out_err;
1325 case RPC_PROC_UNAVAIL:
1326 dprintk("RPC: call_verify: proc %p unsupported by program %u, version %u on server %s\n",
1327 task->tk_msg.rpc_proc,
1328 task->tk_client->cl_prog,
1329 task->tk_client->cl_vers,
1330 task->tk_client->cl_server);
1331 error = -EOPNOTSUPP;
1332 goto out_err;
1333 case RPC_GARBAGE_ARGS:
1334 dprintk("RPC: %4d %s: server saw garbage\n", task->tk_pid, __FUNCTION__);
1335 break; /* retry */
1336 default:
1337 printk(KERN_WARNING "call_verify: server accept status: %x\n", n);
1338 /* Also retry */
1341 out_garbage:
1342 task->tk_client->cl_stats->rpcgarbage++;
1343 if (task->tk_garb_retry) {
1344 task->tk_garb_retry--;
1345 dprintk("RPC %s: retrying %4d\n", __FUNCTION__, task->tk_pid);
1346 task->tk_action = call_bind;
1347 out_retry:
1348 return ERR_PTR(-EAGAIN);
1350 printk(KERN_WARNING "RPC %s: retry failed, exit EIO\n", __FUNCTION__);
1351 out_eio:
1352 error = -EIO;
1353 out_err:
1354 rpc_exit(task, error);
1355 return ERR_PTR(error);
1356 out_overflow:
1357 printk(KERN_WARNING "RPC %s: server reply was truncated.\n", __FUNCTION__);
1358 goto out_garbage;
1361 static int rpcproc_encode_null(void *rqstp, __be32 *data, void *obj)
1363 return 0;
1366 static int rpcproc_decode_null(void *rqstp, __be32 *data, void *obj)
1368 return 0;
1371 static struct rpc_procinfo rpcproc_null = {
1372 .p_encode = rpcproc_encode_null,
1373 .p_decode = rpcproc_decode_null,
1376 int rpc_ping(struct rpc_clnt *clnt, int flags)
1378 struct rpc_message msg = {
1379 .rpc_proc = &rpcproc_null,
1381 int err;
1382 msg.rpc_cred = authnull_ops.lookup_cred(NULL, NULL, 0);
1383 err = rpc_call_sync(clnt, &msg, flags);
1384 put_rpccred(msg.rpc_cred);
1385 return err;