[PATCH] NFS: large non-page-aligned direct I/O clobbers memory
[linux-2.6.git] / net / sunrpc / clnt.c
blob3e19d321067a9ce16db7aa37f65449638e2aba32
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 u32 * call_header(struct rpc_task *task);
64 static u32 * 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;
101 * Create an RPC client
102 * FIXME: This should also take a flags argument (as in task->tk_flags).
103 * It's called (among others) from pmap_create_client, which may in
104 * turn be called by an async task. In this case, rpciod should not be
105 * made to sleep too long.
107 struct rpc_clnt *
108 rpc_new_client(struct rpc_xprt *xprt, char *servname,
109 struct rpc_program *program, u32 vers,
110 rpc_authflavor_t flavor)
112 struct rpc_version *version;
113 struct rpc_clnt *clnt = NULL;
114 struct rpc_auth *auth;
115 int err;
116 int len;
118 dprintk("RPC: creating %s client for %s (xprt %p)\n",
119 program->name, servname, xprt);
121 err = -EINVAL;
122 if (!xprt)
123 goto out_no_xprt;
124 if (vers >= program->nrvers || !(version = program->version[vers]))
125 goto out_err;
127 err = -ENOMEM;
128 clnt = kzalloc(sizeof(*clnt), GFP_KERNEL);
129 if (!clnt)
130 goto out_err;
131 atomic_set(&clnt->cl_users, 0);
132 atomic_set(&clnt->cl_count, 1);
133 clnt->cl_parent = clnt;
135 clnt->cl_server = clnt->cl_inline_name;
136 len = strlen(servname) + 1;
137 if (len > sizeof(clnt->cl_inline_name)) {
138 char *buf = kmalloc(len, GFP_KERNEL);
139 if (buf != 0)
140 clnt->cl_server = buf;
141 else
142 len = sizeof(clnt->cl_inline_name);
144 strlcpy(clnt->cl_server, servname, len);
146 clnt->cl_xprt = xprt;
147 clnt->cl_procinfo = version->procs;
148 clnt->cl_maxproc = version->nrprocs;
149 clnt->cl_protname = program->name;
150 clnt->cl_pmap = &clnt->cl_pmap_default;
151 clnt->cl_port = xprt->addr.sin_port;
152 clnt->cl_prog = program->number;
153 clnt->cl_vers = version->number;
154 clnt->cl_prot = xprt->prot;
155 clnt->cl_stats = program->stats;
156 clnt->cl_metrics = rpc_alloc_iostats(clnt);
157 rpc_init_wait_queue(&clnt->cl_pmap_default.pm_bindwait, "bindwait");
159 if (!clnt->cl_port)
160 clnt->cl_autobind = 1;
162 clnt->cl_rtt = &clnt->cl_rtt_default;
163 rpc_init_rtt(&clnt->cl_rtt_default, xprt->timeout.to_initval);
165 err = rpc_setup_pipedir(clnt, program->pipe_dir_name);
166 if (err < 0)
167 goto out_no_path;
169 auth = rpcauth_create(flavor, clnt);
170 if (IS_ERR(auth)) {
171 printk(KERN_INFO "RPC: Couldn't create auth handle (flavor %u)\n",
172 flavor);
173 err = PTR_ERR(auth);
174 goto out_no_auth;
177 /* save the nodename */
178 clnt->cl_nodelen = strlen(system_utsname.nodename);
179 if (clnt->cl_nodelen > UNX_MAXNODENAME)
180 clnt->cl_nodelen = UNX_MAXNODENAME;
181 memcpy(clnt->cl_nodename, system_utsname.nodename, clnt->cl_nodelen);
182 return clnt;
184 out_no_auth:
185 if (!IS_ERR(clnt->cl_dentry)) {
186 rpc_rmdir(clnt->cl_dentry);
187 rpc_put_mount();
189 out_no_path:
190 if (clnt->cl_server != clnt->cl_inline_name)
191 kfree(clnt->cl_server);
192 kfree(clnt);
193 out_err:
194 xprt_destroy(xprt);
195 out_no_xprt:
196 return ERR_PTR(err);
200 * Create an RPC client
201 * @xprt - pointer to xprt struct
202 * @servname - name of server
203 * @info - rpc_program
204 * @version - rpc_program version
205 * @authflavor - rpc_auth flavour to use
207 * Creates an RPC client structure, then pings the server in order to
208 * determine if it is up, and if it supports this program and version.
210 * This function should never be called by asynchronous tasks such as
211 * the portmapper.
213 struct rpc_clnt *rpc_create_client(struct rpc_xprt *xprt, char *servname,
214 struct rpc_program *info, u32 version, rpc_authflavor_t authflavor)
216 struct rpc_clnt *clnt;
217 int err;
219 clnt = rpc_new_client(xprt, servname, info, version, authflavor);
220 if (IS_ERR(clnt))
221 return clnt;
222 err = rpc_ping(clnt, RPC_TASK_SOFT|RPC_TASK_NOINTR);
223 if (err == 0)
224 return clnt;
225 rpc_shutdown_client(clnt);
226 return ERR_PTR(err);
230 * This function clones the RPC client structure. It allows us to share the
231 * same transport while varying parameters such as the authentication
232 * flavour.
234 struct rpc_clnt *
235 rpc_clone_client(struct rpc_clnt *clnt)
237 struct rpc_clnt *new;
239 new = kmalloc(sizeof(*new), GFP_KERNEL);
240 if (!new)
241 goto out_no_clnt;
242 memcpy(new, clnt, sizeof(*new));
243 atomic_set(&new->cl_count, 1);
244 atomic_set(&new->cl_users, 0);
245 new->cl_parent = clnt;
246 atomic_inc(&clnt->cl_count);
247 /* Duplicate portmapper */
248 rpc_init_wait_queue(&new->cl_pmap_default.pm_bindwait, "bindwait");
249 /* Turn off autobind on clones */
250 new->cl_autobind = 0;
251 new->cl_oneshot = 0;
252 new->cl_dead = 0;
253 if (!IS_ERR(new->cl_dentry))
254 dget(new->cl_dentry);
255 rpc_init_rtt(&new->cl_rtt_default, clnt->cl_xprt->timeout.to_initval);
256 if (new->cl_auth)
257 atomic_inc(&new->cl_auth->au_count);
258 new->cl_pmap = &new->cl_pmap_default;
259 new->cl_metrics = rpc_alloc_iostats(clnt);
260 return new;
261 out_no_clnt:
262 printk(KERN_INFO "RPC: out of memory in %s\n", __FUNCTION__);
263 return ERR_PTR(-ENOMEM);
267 * Properly shut down an RPC client, terminating all outstanding
268 * requests. Note that we must be certain that cl_oneshot and
269 * cl_dead are cleared, or else the client would be destroyed
270 * when the last task releases it.
273 rpc_shutdown_client(struct rpc_clnt *clnt)
275 dprintk("RPC: shutting down %s client for %s, tasks=%d\n",
276 clnt->cl_protname, clnt->cl_server,
277 atomic_read(&clnt->cl_users));
279 while (atomic_read(&clnt->cl_users) > 0) {
280 /* Don't let rpc_release_client destroy us */
281 clnt->cl_oneshot = 0;
282 clnt->cl_dead = 0;
283 rpc_killall_tasks(clnt);
284 wait_event_timeout(destroy_wait,
285 !atomic_read(&clnt->cl_users), 1*HZ);
288 if (atomic_read(&clnt->cl_users) < 0) {
289 printk(KERN_ERR "RPC: rpc_shutdown_client clnt %p tasks=%d\n",
290 clnt, atomic_read(&clnt->cl_users));
291 #ifdef RPC_DEBUG
292 rpc_show_tasks();
293 #endif
294 BUG();
297 return rpc_destroy_client(clnt);
301 * Delete an RPC client
304 rpc_destroy_client(struct rpc_clnt *clnt)
306 if (!atomic_dec_and_test(&clnt->cl_count))
307 return 1;
308 BUG_ON(atomic_read(&clnt->cl_users) != 0);
310 dprintk("RPC: destroying %s client for %s\n",
311 clnt->cl_protname, clnt->cl_server);
312 if (clnt->cl_auth) {
313 rpcauth_destroy(clnt->cl_auth);
314 clnt->cl_auth = NULL;
316 if (clnt->cl_parent != clnt) {
317 if (!IS_ERR(clnt->cl_dentry))
318 dput(clnt->cl_dentry);
319 rpc_destroy_client(clnt->cl_parent);
320 goto out_free;
322 if (!IS_ERR(clnt->cl_dentry)) {
323 rpc_rmdir(clnt->cl_dentry);
324 rpc_put_mount();
326 if (clnt->cl_xprt) {
327 xprt_destroy(clnt->cl_xprt);
328 clnt->cl_xprt = NULL;
330 if (clnt->cl_server != clnt->cl_inline_name)
331 kfree(clnt->cl_server);
332 out_free:
333 rpc_free_iostats(clnt->cl_metrics);
334 clnt->cl_metrics = NULL;
335 kfree(clnt);
336 return 0;
340 * Release an RPC client
342 void
343 rpc_release_client(struct rpc_clnt *clnt)
345 dprintk("RPC: rpc_release_client(%p, %d)\n",
346 clnt, atomic_read(&clnt->cl_users));
348 if (!atomic_dec_and_test(&clnt->cl_users))
349 return;
350 wake_up(&destroy_wait);
351 if (clnt->cl_oneshot || clnt->cl_dead)
352 rpc_destroy_client(clnt);
356 * rpc_bind_new_program - bind a new RPC program to an existing client
357 * @old - old rpc_client
358 * @program - rpc program to set
359 * @vers - rpc program version
361 * Clones the rpc client and sets up a new RPC program. This is mainly
362 * of use for enabling different RPC programs to share the same transport.
363 * The Sun NFSv2/v3 ACL protocol can do this.
365 struct rpc_clnt *rpc_bind_new_program(struct rpc_clnt *old,
366 struct rpc_program *program,
367 int vers)
369 struct rpc_clnt *clnt;
370 struct rpc_version *version;
371 int err;
373 BUG_ON(vers >= program->nrvers || !program->version[vers]);
374 version = program->version[vers];
375 clnt = rpc_clone_client(old);
376 if (IS_ERR(clnt))
377 goto out;
378 clnt->cl_procinfo = version->procs;
379 clnt->cl_maxproc = version->nrprocs;
380 clnt->cl_protname = program->name;
381 clnt->cl_prog = program->number;
382 clnt->cl_vers = version->number;
383 clnt->cl_stats = program->stats;
384 err = rpc_ping(clnt, RPC_TASK_SOFT|RPC_TASK_NOINTR);
385 if (err != 0) {
386 rpc_shutdown_client(clnt);
387 clnt = ERR_PTR(err);
389 out:
390 return clnt;
394 * Default callback for async RPC calls
396 static void
397 rpc_default_callback(struct rpc_task *task, void *data)
401 static const struct rpc_call_ops rpc_default_ops = {
402 .rpc_call_done = rpc_default_callback,
406 * Export the signal mask handling for synchronous code that
407 * sleeps on RPC calls
409 #define RPC_INTR_SIGNALS (sigmask(SIGHUP) | sigmask(SIGINT) | sigmask(SIGQUIT) | sigmask(SIGTERM))
411 static void rpc_save_sigmask(sigset_t *oldset, int intr)
413 unsigned long sigallow = sigmask(SIGKILL);
414 sigset_t sigmask;
416 /* Block all signals except those listed in sigallow */
417 if (intr)
418 sigallow |= RPC_INTR_SIGNALS;
419 siginitsetinv(&sigmask, sigallow);
420 sigprocmask(SIG_BLOCK, &sigmask, oldset);
423 static inline void rpc_task_sigmask(struct rpc_task *task, sigset_t *oldset)
425 rpc_save_sigmask(oldset, !RPC_TASK_UNINTERRUPTIBLE(task));
428 static inline void rpc_restore_sigmask(sigset_t *oldset)
430 sigprocmask(SIG_SETMASK, oldset, NULL);
433 void rpc_clnt_sigmask(struct rpc_clnt *clnt, sigset_t *oldset)
435 rpc_save_sigmask(oldset, clnt->cl_intr);
438 void rpc_clnt_sigunmask(struct rpc_clnt *clnt, sigset_t *oldset)
440 rpc_restore_sigmask(oldset);
444 * New rpc_call implementation
446 int rpc_call_sync(struct rpc_clnt *clnt, struct rpc_message *msg, int flags)
448 struct rpc_task *task;
449 sigset_t oldset;
450 int status;
452 /* If this client is slain all further I/O fails */
453 if (clnt->cl_dead)
454 return -EIO;
456 BUG_ON(flags & RPC_TASK_ASYNC);
458 status = -ENOMEM;
459 task = rpc_new_task(clnt, flags, &rpc_default_ops, NULL);
460 if (task == NULL)
461 goto out;
463 /* Mask signals on RPC calls _and_ GSS_AUTH upcalls */
464 rpc_task_sigmask(task, &oldset);
466 rpc_call_setup(task, msg, 0);
468 /* Set up the call info struct and execute the task */
469 status = task->tk_status;
470 if (status == 0) {
471 atomic_inc(&task->tk_count);
472 status = rpc_execute(task);
473 if (status == 0)
474 status = task->tk_status;
476 rpc_restore_sigmask(&oldset);
477 rpc_release_task(task);
478 out:
479 return status;
483 * New rpc_call implementation
486 rpc_call_async(struct rpc_clnt *clnt, struct rpc_message *msg, int flags,
487 const struct rpc_call_ops *tk_ops, void *data)
489 struct rpc_task *task;
490 sigset_t oldset;
491 int status;
493 /* If this client is slain all further I/O fails */
494 status = -EIO;
495 if (clnt->cl_dead)
496 goto out_release;
498 flags |= RPC_TASK_ASYNC;
500 /* Create/initialize a new RPC task */
501 status = -ENOMEM;
502 if (!(task = rpc_new_task(clnt, flags, tk_ops, data)))
503 goto out_release;
505 /* Mask signals on GSS_AUTH upcalls */
506 rpc_task_sigmask(task, &oldset);
508 rpc_call_setup(task, msg, 0);
510 /* Set up the call info struct and execute the task */
511 status = task->tk_status;
512 if (status == 0)
513 rpc_execute(task);
514 else
515 rpc_release_task(task);
517 rpc_restore_sigmask(&oldset);
518 return status;
519 out_release:
520 if (tk_ops->rpc_release != NULL)
521 tk_ops->rpc_release(data);
522 return status;
526 void
527 rpc_call_setup(struct rpc_task *task, struct rpc_message *msg, int flags)
529 task->tk_msg = *msg;
530 task->tk_flags |= flags;
531 /* Bind the user cred */
532 if (task->tk_msg.rpc_cred != NULL)
533 rpcauth_holdcred(task);
534 else
535 rpcauth_bindcred(task);
537 if (task->tk_status == 0)
538 task->tk_action = call_start;
539 else
540 task->tk_action = rpc_exit_task;
543 void
544 rpc_setbufsize(struct rpc_clnt *clnt, unsigned int sndsize, unsigned int rcvsize)
546 struct rpc_xprt *xprt = clnt->cl_xprt;
547 if (xprt->ops->set_buffer_size)
548 xprt->ops->set_buffer_size(xprt, sndsize, rcvsize);
552 * Return size of largest payload RPC client can support, in bytes
554 * For stream transports, this is one RPC record fragment (see RFC
555 * 1831), as we don't support multi-record requests yet. For datagram
556 * transports, this is the size of an IP packet minus the IP, UDP, and
557 * RPC header sizes.
559 size_t rpc_max_payload(struct rpc_clnt *clnt)
561 return clnt->cl_xprt->max_payload;
563 EXPORT_SYMBOL(rpc_max_payload);
566 * rpc_force_rebind - force transport to check that remote port is unchanged
567 * @clnt: client to rebind
570 void rpc_force_rebind(struct rpc_clnt *clnt)
572 if (clnt->cl_autobind)
573 clnt->cl_port = 0;
575 EXPORT_SYMBOL(rpc_force_rebind);
578 * Restart an (async) RPC call. Usually called from within the
579 * exit handler.
581 void
582 rpc_restart_call(struct rpc_task *task)
584 if (RPC_ASSASSINATED(task))
585 return;
587 task->tk_action = call_start;
591 * 0. Initial state
593 * Other FSM states can be visited zero or more times, but
594 * this state is visited exactly once for each RPC.
596 static void
597 call_start(struct rpc_task *task)
599 struct rpc_clnt *clnt = task->tk_client;
601 dprintk("RPC: %4d call_start %s%d proc %d (%s)\n", task->tk_pid,
602 clnt->cl_protname, clnt->cl_vers, task->tk_msg.rpc_proc->p_proc,
603 (RPC_IS_ASYNC(task) ? "async" : "sync"));
605 /* Increment call count */
606 task->tk_msg.rpc_proc->p_count++;
607 clnt->cl_stats->rpccnt++;
608 task->tk_action = call_reserve;
612 * 1. Reserve an RPC call slot
614 static void
615 call_reserve(struct rpc_task *task)
617 dprintk("RPC: %4d call_reserve\n", task->tk_pid);
619 if (!rpcauth_uptodatecred(task)) {
620 task->tk_action = call_refresh;
621 return;
624 task->tk_status = 0;
625 task->tk_action = call_reserveresult;
626 xprt_reserve(task);
630 * 1b. Grok the result of xprt_reserve()
632 static void
633 call_reserveresult(struct rpc_task *task)
635 int status = task->tk_status;
637 dprintk("RPC: %4d call_reserveresult (status %d)\n",
638 task->tk_pid, task->tk_status);
641 * After a call to xprt_reserve(), we must have either
642 * a request slot or else an error status.
644 task->tk_status = 0;
645 if (status >= 0) {
646 if (task->tk_rqstp) {
647 task->tk_action = call_allocate;
648 return;
651 printk(KERN_ERR "%s: status=%d, but no request slot, exiting\n",
652 __FUNCTION__, status);
653 rpc_exit(task, -EIO);
654 return;
658 * Even though there was an error, we may have acquired
659 * a request slot somehow. Make sure not to leak it.
661 if (task->tk_rqstp) {
662 printk(KERN_ERR "%s: status=%d, request allocated anyway\n",
663 __FUNCTION__, status);
664 xprt_release(task);
667 switch (status) {
668 case -EAGAIN: /* woken up; retry */
669 task->tk_action = call_reserve;
670 return;
671 case -EIO: /* probably a shutdown */
672 break;
673 default:
674 printk(KERN_ERR "%s: unrecognized error %d, exiting\n",
675 __FUNCTION__, status);
676 break;
678 rpc_exit(task, status);
682 * 2. Allocate the buffer. For details, see sched.c:rpc_malloc.
683 * (Note: buffer memory is freed in xprt_release).
685 static void
686 call_allocate(struct rpc_task *task)
688 struct rpc_rqst *req = task->tk_rqstp;
689 struct rpc_xprt *xprt = task->tk_xprt;
690 unsigned int bufsiz;
692 dprintk("RPC: %4d call_allocate (status %d)\n",
693 task->tk_pid, task->tk_status);
694 task->tk_action = call_bind;
695 if (req->rq_buffer)
696 return;
698 /* FIXME: compute buffer requirements more exactly using
699 * auth->au_wslack */
700 bufsiz = task->tk_msg.rpc_proc->p_bufsiz + RPC_SLACK_SPACE;
702 if (xprt->ops->buf_alloc(task, bufsiz << 1) != NULL)
703 return;
704 printk(KERN_INFO "RPC: buffer allocation failed for task %p\n", task);
706 if (RPC_IS_ASYNC(task) || !signalled()) {
707 xprt_release(task);
708 task->tk_action = call_reserve;
709 rpc_delay(task, HZ>>4);
710 return;
713 rpc_exit(task, -ERESTARTSYS);
716 static inline int
717 rpc_task_need_encode(struct rpc_task *task)
719 return task->tk_rqstp->rq_snd_buf.len == 0;
722 static inline void
723 rpc_task_force_reencode(struct rpc_task *task)
725 task->tk_rqstp->rq_snd_buf.len = 0;
729 * 3. Encode arguments of an RPC call
731 static void
732 call_encode(struct rpc_task *task)
734 struct rpc_rqst *req = task->tk_rqstp;
735 struct xdr_buf *sndbuf = &req->rq_snd_buf;
736 struct xdr_buf *rcvbuf = &req->rq_rcv_buf;
737 unsigned int bufsiz;
738 kxdrproc_t encode;
739 u32 *p;
741 dprintk("RPC: %4d call_encode (status %d)\n",
742 task->tk_pid, task->tk_status);
744 /* Default buffer setup */
745 bufsiz = req->rq_bufsize >> 1;
746 sndbuf->head[0].iov_base = (void *)req->rq_buffer;
747 sndbuf->head[0].iov_len = bufsiz;
748 sndbuf->tail[0].iov_len = 0;
749 sndbuf->page_len = 0;
750 sndbuf->len = 0;
751 sndbuf->buflen = bufsiz;
752 rcvbuf->head[0].iov_base = (void *)((char *)req->rq_buffer + bufsiz);
753 rcvbuf->head[0].iov_len = bufsiz;
754 rcvbuf->tail[0].iov_len = 0;
755 rcvbuf->page_len = 0;
756 rcvbuf->len = 0;
757 rcvbuf->buflen = bufsiz;
759 /* Encode header and provided arguments */
760 encode = task->tk_msg.rpc_proc->p_encode;
761 if (!(p = call_header(task))) {
762 printk(KERN_INFO "RPC: call_header failed, exit EIO\n");
763 rpc_exit(task, -EIO);
764 return;
766 if (encode == NULL)
767 return;
769 task->tk_status = rpcauth_wrap_req(task, encode, req, p,
770 task->tk_msg.rpc_argp);
771 if (task->tk_status == -ENOMEM) {
772 /* XXX: Is this sane? */
773 rpc_delay(task, 3*HZ);
774 task->tk_status = -EAGAIN;
779 * 4. Get the server port number if not yet set
781 static void
782 call_bind(struct rpc_task *task)
784 struct rpc_clnt *clnt = task->tk_client;
786 dprintk("RPC: %4d call_bind (status %d)\n",
787 task->tk_pid, task->tk_status);
789 task->tk_action = call_connect;
790 if (!clnt->cl_port) {
791 task->tk_action = call_bind_status;
792 task->tk_timeout = task->tk_xprt->bind_timeout;
793 rpc_getport(task, clnt);
798 * 4a. Sort out bind result
800 static void
801 call_bind_status(struct rpc_task *task)
803 int status = -EACCES;
805 if (task->tk_status >= 0) {
806 dprintk("RPC: %4d call_bind_status (status %d)\n",
807 task->tk_pid, task->tk_status);
808 task->tk_status = 0;
809 task->tk_action = call_connect;
810 return;
813 switch (task->tk_status) {
814 case -EACCES:
815 dprintk("RPC: %4d remote rpcbind: RPC program/version unavailable\n",
816 task->tk_pid);
817 rpc_delay(task, 3*HZ);
818 goto retry_bind;
819 case -ETIMEDOUT:
820 dprintk("RPC: %4d rpcbind request timed out\n",
821 task->tk_pid);
822 if (RPC_IS_SOFT(task)) {
823 status = -EIO;
824 break;
826 goto retry_bind;
827 case -EPFNOSUPPORT:
828 dprintk("RPC: %4d remote rpcbind service unavailable\n",
829 task->tk_pid);
830 break;
831 case -EPROTONOSUPPORT:
832 dprintk("RPC: %4d remote rpcbind version 2 unavailable\n",
833 task->tk_pid);
834 break;
835 default:
836 dprintk("RPC: %4d unrecognized rpcbind error (%d)\n",
837 task->tk_pid, -task->tk_status);
838 status = -EIO;
839 break;
842 rpc_exit(task, status);
843 return;
845 retry_bind:
846 task->tk_status = 0;
847 task->tk_action = call_bind;
848 return;
852 * 4b. Connect to the RPC server
854 static void
855 call_connect(struct rpc_task *task)
857 struct rpc_xprt *xprt = task->tk_xprt;
859 dprintk("RPC: %4d call_connect xprt %p %s connected\n",
860 task->tk_pid, xprt,
861 (xprt_connected(xprt) ? "is" : "is not"));
863 task->tk_action = call_transmit;
864 if (!xprt_connected(xprt)) {
865 task->tk_action = call_connect_status;
866 if (task->tk_status < 0)
867 return;
868 xprt_connect(task);
873 * 4c. Sort out connect result
875 static void
876 call_connect_status(struct rpc_task *task)
878 struct rpc_clnt *clnt = task->tk_client;
879 int status = task->tk_status;
881 dprintk("RPC: %5u call_connect_status (status %d)\n",
882 task->tk_pid, task->tk_status);
884 task->tk_status = 0;
885 if (status >= 0) {
886 clnt->cl_stats->netreconn++;
887 task->tk_action = call_transmit;
888 return;
891 /* Something failed: remote service port may have changed */
892 rpc_force_rebind(clnt);
894 switch (status) {
895 case -ENOTCONN:
896 case -ETIMEDOUT:
897 case -EAGAIN:
898 task->tk_action = call_bind;
899 break;
900 default:
901 rpc_exit(task, -EIO);
902 break;
907 * 5. Transmit the RPC request, and wait for reply
909 static void
910 call_transmit(struct rpc_task *task)
912 dprintk("RPC: %4d call_transmit (status %d)\n",
913 task->tk_pid, task->tk_status);
915 task->tk_action = call_status;
916 if (task->tk_status < 0)
917 return;
918 task->tk_status = xprt_prepare_transmit(task);
919 if (task->tk_status != 0)
920 return;
921 task->tk_action = call_transmit_status;
922 /* Encode here so that rpcsec_gss can use correct sequence number. */
923 if (rpc_task_need_encode(task)) {
924 BUG_ON(task->tk_rqstp->rq_bytes_sent != 0);
925 call_encode(task);
926 /* Did the encode result in an error condition? */
927 if (task->tk_status != 0)
928 return;
930 xprt_transmit(task);
931 if (task->tk_status < 0)
932 return;
934 * On success, ensure that we call xprt_end_transmit() before sleeping
935 * in order to allow access to the socket to other RPC requests.
937 call_transmit_status(task);
938 if (task->tk_msg.rpc_proc->p_decode != NULL)
939 return;
940 task->tk_action = rpc_exit_task;
941 rpc_wake_up_task(task);
945 * 5a. Handle cleanup after a transmission
947 static void
948 call_transmit_status(struct rpc_task *task)
950 task->tk_action = call_status;
952 * Special case: if we've been waiting on the socket's write_space()
953 * callback, then don't call xprt_end_transmit().
955 if (task->tk_status == -EAGAIN)
956 return;
957 xprt_end_transmit(task);
958 rpc_task_force_reencode(task);
962 * 6. Sort out the RPC call status
964 static void
965 call_status(struct rpc_task *task)
967 struct rpc_clnt *clnt = task->tk_client;
968 struct rpc_rqst *req = task->tk_rqstp;
969 int status;
971 if (req->rq_received > 0 && !req->rq_bytes_sent)
972 task->tk_status = req->rq_received;
974 dprintk("RPC: %4d call_status (status %d)\n",
975 task->tk_pid, task->tk_status);
977 status = task->tk_status;
978 if (status >= 0) {
979 task->tk_action = call_decode;
980 return;
983 task->tk_status = 0;
984 switch(status) {
985 case -ETIMEDOUT:
986 task->tk_action = call_timeout;
987 break;
988 case -ECONNREFUSED:
989 case -ENOTCONN:
990 rpc_force_rebind(clnt);
991 task->tk_action = call_bind;
992 break;
993 case -EAGAIN:
994 task->tk_action = call_transmit;
995 break;
996 case -EIO:
997 /* shutdown or soft timeout */
998 rpc_exit(task, status);
999 break;
1000 default:
1001 printk("%s: RPC call returned error %d\n",
1002 clnt->cl_protname, -status);
1003 rpc_exit(task, status);
1004 break;
1009 * 6a. Handle RPC timeout
1010 * We do not release the request slot, so we keep using the
1011 * same XID for all retransmits.
1013 static void
1014 call_timeout(struct rpc_task *task)
1016 struct rpc_clnt *clnt = task->tk_client;
1018 if (xprt_adjust_timeout(task->tk_rqstp) == 0) {
1019 dprintk("RPC: %4d call_timeout (minor)\n", task->tk_pid);
1020 goto retry;
1023 dprintk("RPC: %4d call_timeout (major)\n", task->tk_pid);
1024 task->tk_timeouts++;
1026 if (RPC_IS_SOFT(task)) {
1027 printk(KERN_NOTICE "%s: server %s not responding, timed out\n",
1028 clnt->cl_protname, clnt->cl_server);
1029 rpc_exit(task, -EIO);
1030 return;
1033 if (!(task->tk_flags & RPC_CALL_MAJORSEEN)) {
1034 task->tk_flags |= RPC_CALL_MAJORSEEN;
1035 printk(KERN_NOTICE "%s: server %s not responding, still trying\n",
1036 clnt->cl_protname, clnt->cl_server);
1038 rpc_force_rebind(clnt);
1040 retry:
1041 clnt->cl_stats->rpcretrans++;
1042 task->tk_action = call_bind;
1043 task->tk_status = 0;
1047 * 7. Decode the RPC reply
1049 static void
1050 call_decode(struct rpc_task *task)
1052 struct rpc_clnt *clnt = task->tk_client;
1053 struct rpc_rqst *req = task->tk_rqstp;
1054 kxdrproc_t decode = task->tk_msg.rpc_proc->p_decode;
1055 u32 *p;
1057 dprintk("RPC: %4d call_decode (status %d)\n",
1058 task->tk_pid, task->tk_status);
1060 if (task->tk_flags & RPC_CALL_MAJORSEEN) {
1061 printk(KERN_NOTICE "%s: server %s OK\n",
1062 clnt->cl_protname, clnt->cl_server);
1063 task->tk_flags &= ~RPC_CALL_MAJORSEEN;
1066 if (task->tk_status < 12) {
1067 if (!RPC_IS_SOFT(task)) {
1068 task->tk_action = call_bind;
1069 clnt->cl_stats->rpcretrans++;
1070 goto out_retry;
1072 printk(KERN_WARNING "%s: too small RPC reply size (%d bytes)\n",
1073 clnt->cl_protname, task->tk_status);
1074 rpc_exit(task, -EIO);
1075 return;
1079 * Ensure that we see all writes made by xprt_complete_rqst()
1080 * before it changed req->rq_received.
1082 smp_rmb();
1083 req->rq_rcv_buf.len = req->rq_private_buf.len;
1085 /* Check that the softirq receive buffer is valid */
1086 WARN_ON(memcmp(&req->rq_rcv_buf, &req->rq_private_buf,
1087 sizeof(req->rq_rcv_buf)) != 0);
1089 /* Verify the RPC header */
1090 p = call_verify(task);
1091 if (IS_ERR(p)) {
1092 if (p == ERR_PTR(-EAGAIN))
1093 goto out_retry;
1094 return;
1097 task->tk_action = rpc_exit_task;
1099 if (decode)
1100 task->tk_status = rpcauth_unwrap_resp(task, decode, req, p,
1101 task->tk_msg.rpc_resp);
1102 dprintk("RPC: %4d call_decode result %d\n", task->tk_pid,
1103 task->tk_status);
1104 return;
1105 out_retry:
1106 req->rq_received = req->rq_private_buf.len = 0;
1107 task->tk_status = 0;
1111 * 8. Refresh the credentials if rejected by the server
1113 static void
1114 call_refresh(struct rpc_task *task)
1116 dprintk("RPC: %4d call_refresh\n", task->tk_pid);
1118 xprt_release(task); /* Must do to obtain new XID */
1119 task->tk_action = call_refreshresult;
1120 task->tk_status = 0;
1121 task->tk_client->cl_stats->rpcauthrefresh++;
1122 rpcauth_refreshcred(task);
1126 * 8a. Process the results of a credential refresh
1128 static void
1129 call_refreshresult(struct rpc_task *task)
1131 int status = task->tk_status;
1132 dprintk("RPC: %4d call_refreshresult (status %d)\n",
1133 task->tk_pid, task->tk_status);
1135 task->tk_status = 0;
1136 task->tk_action = call_reserve;
1137 if (status >= 0 && rpcauth_uptodatecred(task))
1138 return;
1139 if (status == -EACCES) {
1140 rpc_exit(task, -EACCES);
1141 return;
1143 task->tk_action = call_refresh;
1144 if (status != -ETIMEDOUT)
1145 rpc_delay(task, 3*HZ);
1146 return;
1150 * Call header serialization
1152 static u32 *
1153 call_header(struct rpc_task *task)
1155 struct rpc_clnt *clnt = task->tk_client;
1156 struct rpc_rqst *req = task->tk_rqstp;
1157 u32 *p = req->rq_svec[0].iov_base;
1159 /* FIXME: check buffer size? */
1161 p = xprt_skip_transport_header(task->tk_xprt, p);
1162 *p++ = req->rq_xid; /* XID */
1163 *p++ = htonl(RPC_CALL); /* CALL */
1164 *p++ = htonl(RPC_VERSION); /* RPC version */
1165 *p++ = htonl(clnt->cl_prog); /* program number */
1166 *p++ = htonl(clnt->cl_vers); /* program version */
1167 *p++ = htonl(task->tk_msg.rpc_proc->p_proc); /* procedure */
1168 p = rpcauth_marshcred(task, p);
1169 req->rq_slen = xdr_adjust_iovec(&req->rq_svec[0], p);
1170 return p;
1174 * Reply header verification
1176 static u32 *
1177 call_verify(struct rpc_task *task)
1179 struct kvec *iov = &task->tk_rqstp->rq_rcv_buf.head[0];
1180 int len = task->tk_rqstp->rq_rcv_buf.len >> 2;
1181 u32 *p = iov->iov_base, n;
1182 int error = -EACCES;
1184 if ((task->tk_rqstp->rq_rcv_buf.len & 3) != 0) {
1185 /* RFC-1014 says that the representation of XDR data must be a
1186 * multiple of four bytes
1187 * - if it isn't pointer subtraction in the NFS client may give
1188 * undefined results
1190 printk(KERN_WARNING
1191 "call_verify: XDR representation not a multiple of"
1192 " 4 bytes: 0x%x\n", task->tk_rqstp->rq_rcv_buf.len);
1193 goto out_eio;
1195 if ((len -= 3) < 0)
1196 goto out_overflow;
1197 p += 1; /* skip XID */
1199 if ((n = ntohl(*p++)) != RPC_REPLY) {
1200 printk(KERN_WARNING "call_verify: not an RPC reply: %x\n", n);
1201 goto out_garbage;
1203 if ((n = ntohl(*p++)) != RPC_MSG_ACCEPTED) {
1204 if (--len < 0)
1205 goto out_overflow;
1206 switch ((n = ntohl(*p++))) {
1207 case RPC_AUTH_ERROR:
1208 break;
1209 case RPC_MISMATCH:
1210 dprintk("%s: RPC call version mismatch!\n", __FUNCTION__);
1211 error = -EPROTONOSUPPORT;
1212 goto out_err;
1213 default:
1214 dprintk("%s: RPC call rejected, unknown error: %x\n", __FUNCTION__, n);
1215 goto out_eio;
1217 if (--len < 0)
1218 goto out_overflow;
1219 switch ((n = ntohl(*p++))) {
1220 case RPC_AUTH_REJECTEDCRED:
1221 case RPC_AUTH_REJECTEDVERF:
1222 case RPCSEC_GSS_CREDPROBLEM:
1223 case RPCSEC_GSS_CTXPROBLEM:
1224 if (!task->tk_cred_retry)
1225 break;
1226 task->tk_cred_retry--;
1227 dprintk("RPC: %4d call_verify: retry stale creds\n",
1228 task->tk_pid);
1229 rpcauth_invalcred(task);
1230 task->tk_action = call_refresh;
1231 goto out_retry;
1232 case RPC_AUTH_BADCRED:
1233 case RPC_AUTH_BADVERF:
1234 /* possibly garbled cred/verf? */
1235 if (!task->tk_garb_retry)
1236 break;
1237 task->tk_garb_retry--;
1238 dprintk("RPC: %4d call_verify: retry garbled creds\n",
1239 task->tk_pid);
1240 task->tk_action = call_bind;
1241 goto out_retry;
1242 case RPC_AUTH_TOOWEAK:
1243 printk(KERN_NOTICE "call_verify: server %s requires stronger "
1244 "authentication.\n", task->tk_client->cl_server);
1245 break;
1246 default:
1247 printk(KERN_WARNING "call_verify: unknown auth error: %x\n", n);
1248 error = -EIO;
1250 dprintk("RPC: %4d call_verify: call rejected %d\n",
1251 task->tk_pid, n);
1252 goto out_err;
1254 if (!(p = rpcauth_checkverf(task, p))) {
1255 printk(KERN_WARNING "call_verify: auth check failed\n");
1256 goto out_garbage; /* bad verifier, retry */
1258 len = p - (u32 *)iov->iov_base - 1;
1259 if (len < 0)
1260 goto out_overflow;
1261 switch ((n = ntohl(*p++))) {
1262 case RPC_SUCCESS:
1263 return p;
1264 case RPC_PROG_UNAVAIL:
1265 dprintk("RPC: call_verify: program %u is unsupported by server %s\n",
1266 (unsigned int)task->tk_client->cl_prog,
1267 task->tk_client->cl_server);
1268 error = -EPFNOSUPPORT;
1269 goto out_err;
1270 case RPC_PROG_MISMATCH:
1271 dprintk("RPC: call_verify: program %u, version %u unsupported by server %s\n",
1272 (unsigned int)task->tk_client->cl_prog,
1273 (unsigned int)task->tk_client->cl_vers,
1274 task->tk_client->cl_server);
1275 error = -EPROTONOSUPPORT;
1276 goto out_err;
1277 case RPC_PROC_UNAVAIL:
1278 dprintk("RPC: call_verify: proc %p unsupported by program %u, version %u on server %s\n",
1279 task->tk_msg.rpc_proc,
1280 task->tk_client->cl_prog,
1281 task->tk_client->cl_vers,
1282 task->tk_client->cl_server);
1283 error = -EOPNOTSUPP;
1284 goto out_err;
1285 case RPC_GARBAGE_ARGS:
1286 dprintk("RPC: %4d %s: server saw garbage\n", task->tk_pid, __FUNCTION__);
1287 break; /* retry */
1288 default:
1289 printk(KERN_WARNING "call_verify: server accept status: %x\n", n);
1290 /* Also retry */
1293 out_garbage:
1294 task->tk_client->cl_stats->rpcgarbage++;
1295 if (task->tk_garb_retry) {
1296 task->tk_garb_retry--;
1297 dprintk("RPC %s: retrying %4d\n", __FUNCTION__, task->tk_pid);
1298 task->tk_action = call_bind;
1299 out_retry:
1300 return ERR_PTR(-EAGAIN);
1302 printk(KERN_WARNING "RPC %s: retry failed, exit EIO\n", __FUNCTION__);
1303 out_eio:
1304 error = -EIO;
1305 out_err:
1306 rpc_exit(task, error);
1307 return ERR_PTR(error);
1308 out_overflow:
1309 printk(KERN_WARNING "RPC %s: server reply was truncated.\n", __FUNCTION__);
1310 goto out_garbage;
1313 static int rpcproc_encode_null(void *rqstp, u32 *data, void *obj)
1315 return 0;
1318 static int rpcproc_decode_null(void *rqstp, u32 *data, void *obj)
1320 return 0;
1323 static struct rpc_procinfo rpcproc_null = {
1324 .p_encode = rpcproc_encode_null,
1325 .p_decode = rpcproc_decode_null,
1328 int rpc_ping(struct rpc_clnt *clnt, int flags)
1330 struct rpc_message msg = {
1331 .rpc_proc = &rpcproc_null,
1333 int err;
1334 msg.rpc_cred = authnull_ops.lookup_cred(NULL, NULL, 0);
1335 err = rpc_call_sync(clnt, &msg, flags);
1336 put_rpccred(msg.rpc_cred);
1337 return err;