SUNRPC: introduce per-task RPC iostats
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / net / sunrpc / clnt.c
CommitLineData
1da177e4 1/*
55aa4f58 2 * linux/net/sunrpc/clnt.c
1da177e4
LT
3 *
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.
7 *
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.
15 *
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.
19 *
20 * Copyright (C) 1992,1993 Rick Sladkey <jrs@world.std.com>
21 * Copyright (C) 1995,1996 Olaf Kirch <okir@monad.swb.de>
22 */
23
24#include <asm/system.h>
25
26#include <linux/module.h>
27#include <linux/types.h>
28#include <linux/mm.h>
29#include <linux/slab.h>
1da177e4
LT
30#include <linux/utsname.h>
31
32#include <linux/sunrpc/clnt.h>
33#include <linux/workqueue.h>
34#include <linux/sunrpc/rpc_pipe_fs.h>
35
36#include <linux/nfs.h>
37
38
39#define RPC_SLACK_SPACE (1024) /* total overkill */
40
41#ifdef RPC_DEBUG
42# define RPCDBG_FACILITY RPCDBG_CALL
43#endif
44
45static DECLARE_WAIT_QUEUE_HEAD(destroy_wait);
46
47
48static void call_start(struct rpc_task *task);
49static void call_reserve(struct rpc_task *task);
50static void call_reserveresult(struct rpc_task *task);
51static void call_allocate(struct rpc_task *task);
52static void call_encode(struct rpc_task *task);
53static void call_decode(struct rpc_task *task);
54static void call_bind(struct rpc_task *task);
da351878 55static void call_bind_status(struct rpc_task *task);
1da177e4
LT
56static void call_transmit(struct rpc_task *task);
57static void call_status(struct rpc_task *task);
940e3318 58static void call_transmit_status(struct rpc_task *task);
1da177e4
LT
59static void call_refresh(struct rpc_task *task);
60static void call_refreshresult(struct rpc_task *task);
61static void call_timeout(struct rpc_task *task);
62static void call_connect(struct rpc_task *task);
63static void call_connect_status(struct rpc_task *task);
64static u32 * call_header(struct rpc_task *task);
65static u32 * call_verify(struct rpc_task *task);
66
67
68static int
69rpc_setup_pipedir(struct rpc_clnt *clnt, char *dir_name)
70{
f134585a 71 static uint32_t clntid;
1da177e4
LT
72 int error;
73
74 if (dir_name == NULL)
75 return 0;
f134585a
TM
76 for (;;) {
77 snprintf(clnt->cl_pathname, sizeof(clnt->cl_pathname),
78 "%s/clnt%x", dir_name,
79 (unsigned int)clntid++);
80 clnt->cl_pathname[sizeof(clnt->cl_pathname) - 1] = '\0';
81 clnt->cl_dentry = rpc_mkdir(clnt->cl_pathname, clnt);
82 if (!IS_ERR(clnt->cl_dentry))
83 return 0;
1da177e4 84 error = PTR_ERR(clnt->cl_dentry);
f134585a
TM
85 if (error != -EEXIST) {
86 printk(KERN_INFO "RPC: Couldn't create pipefs entry %s, error %d\n",
87 clnt->cl_pathname, error);
88 return error;
89 }
1da177e4
LT
90 }
91}
92
93/*
94 * Create an RPC client
95 * FIXME: This should also take a flags argument (as in task->tk_flags).
96 * It's called (among others) from pmap_create_client, which may in
97 * turn be called by an async task. In this case, rpciod should not be
98 * made to sleep too long.
99 */
100struct rpc_clnt *
5ee0ed7d 101rpc_new_client(struct rpc_xprt *xprt, char *servname,
1da177e4
LT
102 struct rpc_program *program, u32 vers,
103 rpc_authflavor_t flavor)
104{
105 struct rpc_version *version;
106 struct rpc_clnt *clnt = NULL;
6a19275a 107 struct rpc_auth *auth;
1da177e4
LT
108 int err;
109 int len;
110
111 dprintk("RPC: creating %s client for %s (xprt %p)\n",
112 program->name, servname, xprt);
113
114 err = -EINVAL;
115 if (!xprt)
712917d1 116 goto out_no_xprt;
1da177e4
LT
117 if (vers >= program->nrvers || !(version = program->version[vers]))
118 goto out_err;
119
120 err = -ENOMEM;
8b3a7005 121 clnt = kmalloc(sizeof(*clnt), GFP_KERNEL);
1da177e4
LT
122 if (!clnt)
123 goto out_err;
124 memset(clnt, 0, sizeof(*clnt));
125 atomic_set(&clnt->cl_users, 0);
126 atomic_set(&clnt->cl_count, 1);
127 clnt->cl_parent = clnt;
128
129 clnt->cl_server = clnt->cl_inline_name;
130 len = strlen(servname) + 1;
131 if (len > sizeof(clnt->cl_inline_name)) {
132 char *buf = kmalloc(len, GFP_KERNEL);
133 if (buf != 0)
134 clnt->cl_server = buf;
135 else
136 len = sizeof(clnt->cl_inline_name);
137 }
138 strlcpy(clnt->cl_server, servname, len);
139
140 clnt->cl_xprt = xprt;
141 clnt->cl_procinfo = version->procs;
142 clnt->cl_maxproc = version->nrprocs;
143 clnt->cl_protname = program->name;
144 clnt->cl_pmap = &clnt->cl_pmap_default;
145 clnt->cl_port = xprt->addr.sin_port;
146 clnt->cl_prog = program->number;
147 clnt->cl_vers = version->number;
148 clnt->cl_prot = xprt->prot;
149 clnt->cl_stats = program->stats;
150 rpc_init_wait_queue(&clnt->cl_pmap_default.pm_bindwait, "bindwait");
151
152 if (!clnt->cl_port)
153 clnt->cl_autobind = 1;
154
155 clnt->cl_rtt = &clnt->cl_rtt_default;
156 rpc_init_rtt(&clnt->cl_rtt_default, xprt->timeout.to_initval);
157
158 err = rpc_setup_pipedir(clnt, program->pipe_dir_name);
159 if (err < 0)
160 goto out_no_path;
161
6a19275a
BF
162 auth = rpcauth_create(flavor, clnt);
163 if (IS_ERR(auth)) {
1da177e4
LT
164 printk(KERN_INFO "RPC: Couldn't create auth handle (flavor %u)\n",
165 flavor);
6a19275a 166 err = PTR_ERR(auth);
1da177e4
LT
167 goto out_no_auth;
168 }
169
170 /* save the nodename */
171 clnt->cl_nodelen = strlen(system_utsname.nodename);
172 if (clnt->cl_nodelen > UNX_MAXNODENAME)
173 clnt->cl_nodelen = UNX_MAXNODENAME;
174 memcpy(clnt->cl_nodename, system_utsname.nodename, clnt->cl_nodelen);
175 return clnt;
176
177out_no_auth:
f134585a 178 rpc_rmdir(clnt->cl_pathname);
1da177e4
LT
179out_no_path:
180 if (clnt->cl_server != clnt->cl_inline_name)
181 kfree(clnt->cl_server);
182 kfree(clnt);
183out_err:
5b616f5d 184 xprt_destroy(xprt);
712917d1 185out_no_xprt:
1da177e4
LT
186 return ERR_PTR(err);
187}
188
5ee0ed7d
TM
189/**
190 * Create an RPC client
191 * @xprt - pointer to xprt struct
192 * @servname - name of server
193 * @info - rpc_program
194 * @version - rpc_program version
195 * @authflavor - rpc_auth flavour to use
196 *
197 * Creates an RPC client structure, then pings the server in order to
198 * determine if it is up, and if it supports this program and version.
199 *
200 * This function should never be called by asynchronous tasks such as
201 * the portmapper.
202 */
203struct rpc_clnt *rpc_create_client(struct rpc_xprt *xprt, char *servname,
204 struct rpc_program *info, u32 version, rpc_authflavor_t authflavor)
205{
206 struct rpc_clnt *clnt;
207 int err;
208
209 clnt = rpc_new_client(xprt, servname, info, version, authflavor);
210 if (IS_ERR(clnt))
211 return clnt;
212 err = rpc_ping(clnt, RPC_TASK_SOFT|RPC_TASK_NOINTR);
213 if (err == 0)
214 return clnt;
215 rpc_shutdown_client(clnt);
216 return ERR_PTR(err);
217}
218
1da177e4
LT
219/*
220 * This function clones the RPC client structure. It allows us to share the
221 * same transport while varying parameters such as the authentication
222 * flavour.
223 */
224struct rpc_clnt *
225rpc_clone_client(struct rpc_clnt *clnt)
226{
227 struct rpc_clnt *new;
228
8b3a7005 229 new = kmalloc(sizeof(*new), GFP_KERNEL);
1da177e4
LT
230 if (!new)
231 goto out_no_clnt;
232 memcpy(new, clnt, sizeof(*new));
233 atomic_set(&new->cl_count, 1);
234 atomic_set(&new->cl_users, 0);
235 new->cl_parent = clnt;
236 atomic_inc(&clnt->cl_count);
237 /* Duplicate portmapper */
238 rpc_init_wait_queue(&new->cl_pmap_default.pm_bindwait, "bindwait");
239 /* Turn off autobind on clones */
240 new->cl_autobind = 0;
241 new->cl_oneshot = 0;
242 new->cl_dead = 0;
12de3b35 243 dget(new->cl_dentry);
1da177e4
LT
244 rpc_init_rtt(&new->cl_rtt_default, clnt->cl_xprt->timeout.to_initval);
245 if (new->cl_auth)
246 atomic_inc(&new->cl_auth->au_count);
007e251f
AG
247 new->cl_pmap = &new->cl_pmap_default;
248 rpc_init_wait_queue(&new->cl_pmap_default.pm_bindwait, "bindwait");
1da177e4
LT
249 return new;
250out_no_clnt:
251 printk(KERN_INFO "RPC: out of memory in %s\n", __FUNCTION__);
252 return ERR_PTR(-ENOMEM);
253}
254
255/*
256 * Properly shut down an RPC client, terminating all outstanding
257 * requests. Note that we must be certain that cl_oneshot and
258 * cl_dead are cleared, or else the client would be destroyed
259 * when the last task releases it.
260 */
261int
262rpc_shutdown_client(struct rpc_clnt *clnt)
263{
264 dprintk("RPC: shutting down %s client for %s, tasks=%d\n",
265 clnt->cl_protname, clnt->cl_server,
266 atomic_read(&clnt->cl_users));
267
268 while (atomic_read(&clnt->cl_users) > 0) {
269 /* Don't let rpc_release_client destroy us */
270 clnt->cl_oneshot = 0;
271 clnt->cl_dead = 0;
272 rpc_killall_tasks(clnt);
532347e2 273 wait_event_timeout(destroy_wait,
4f47707b 274 !atomic_read(&clnt->cl_users), 1*HZ);
1da177e4
LT
275 }
276
277 if (atomic_read(&clnt->cl_users) < 0) {
278 printk(KERN_ERR "RPC: rpc_shutdown_client clnt %p tasks=%d\n",
279 clnt, atomic_read(&clnt->cl_users));
280#ifdef RPC_DEBUG
281 rpc_show_tasks();
282#endif
283 BUG();
284 }
285
286 return rpc_destroy_client(clnt);
287}
288
289/*
290 * Delete an RPC client
291 */
292int
293rpc_destroy_client(struct rpc_clnt *clnt)
294{
295 if (!atomic_dec_and_test(&clnt->cl_count))
296 return 1;
297 BUG_ON(atomic_read(&clnt->cl_users) != 0);
298
299 dprintk("RPC: destroying %s client for %s\n",
300 clnt->cl_protname, clnt->cl_server);
301 if (clnt->cl_auth) {
302 rpcauth_destroy(clnt->cl_auth);
303 clnt->cl_auth = NULL;
304 }
305 if (clnt->cl_parent != clnt) {
306 rpc_destroy_client(clnt->cl_parent);
307 goto out_free;
308 }
f134585a
TM
309 if (clnt->cl_pathname[0])
310 rpc_rmdir(clnt->cl_pathname);
1da177e4
LT
311 if (clnt->cl_xprt) {
312 xprt_destroy(clnt->cl_xprt);
313 clnt->cl_xprt = NULL;
314 }
315 if (clnt->cl_server != clnt->cl_inline_name)
316 kfree(clnt->cl_server);
317out_free:
12de3b35
TM
318 if (clnt->cl_dentry)
319 dput(clnt->cl_dentry);
1da177e4
LT
320 kfree(clnt);
321 return 0;
322}
323
324/*
325 * Release an RPC client
326 */
327void
328rpc_release_client(struct rpc_clnt *clnt)
329{
330 dprintk("RPC: rpc_release_client(%p, %d)\n",
331 clnt, atomic_read(&clnt->cl_users));
332
333 if (!atomic_dec_and_test(&clnt->cl_users))
334 return;
335 wake_up(&destroy_wait);
336 if (clnt->cl_oneshot || clnt->cl_dead)
337 rpc_destroy_client(clnt);
338}
339
007e251f
AG
340/**
341 * rpc_bind_new_program - bind a new RPC program to an existing client
342 * @old - old rpc_client
343 * @program - rpc program to set
344 * @vers - rpc program version
345 *
346 * Clones the rpc client and sets up a new RPC program. This is mainly
347 * of use for enabling different RPC programs to share the same transport.
348 * The Sun NFSv2/v3 ACL protocol can do this.
349 */
350struct rpc_clnt *rpc_bind_new_program(struct rpc_clnt *old,
351 struct rpc_program *program,
352 int vers)
353{
354 struct rpc_clnt *clnt;
355 struct rpc_version *version;
356 int err;
357
358 BUG_ON(vers >= program->nrvers || !program->version[vers]);
359 version = program->version[vers];
360 clnt = rpc_clone_client(old);
361 if (IS_ERR(clnt))
362 goto out;
363 clnt->cl_procinfo = version->procs;
364 clnt->cl_maxproc = version->nrprocs;
365 clnt->cl_protname = program->name;
366 clnt->cl_prog = program->number;
367 clnt->cl_vers = version->number;
368 clnt->cl_stats = program->stats;
369 err = rpc_ping(clnt, RPC_TASK_SOFT|RPC_TASK_NOINTR);
370 if (err != 0) {
371 rpc_shutdown_client(clnt);
372 clnt = ERR_PTR(err);
373 }
374out:
375 return clnt;
376}
377
1da177e4
LT
378/*
379 * Default callback for async RPC calls
380 */
381static void
963d8fe5 382rpc_default_callback(struct rpc_task *task, void *data)
1da177e4
LT
383{
384}
385
963d8fe5
TM
386static const struct rpc_call_ops rpc_default_ops = {
387 .rpc_call_done = rpc_default_callback,
388};
389
1da177e4 390/*
14b218a8 391 * Export the signal mask handling for synchronous code that
1da177e4
LT
392 * sleeps on RPC calls
393 */
2bd61579 394#define RPC_INTR_SIGNALS (sigmask(SIGHUP) | sigmask(SIGINT) | sigmask(SIGQUIT) | sigmask(SIGTERM))
1da177e4 395
14b218a8
TM
396static void rpc_save_sigmask(sigset_t *oldset, int intr)
397{
2bd61579 398 unsigned long sigallow = sigmask(SIGKILL);
14b218a8
TM
399 sigset_t sigmask;
400
401 /* Block all signals except those listed in sigallow */
402 if (intr)
403 sigallow |= RPC_INTR_SIGNALS;
404 siginitsetinv(&sigmask, sigallow);
405 sigprocmask(SIG_BLOCK, &sigmask, oldset);
406}
407
408static inline void rpc_task_sigmask(struct rpc_task *task, sigset_t *oldset)
409{
410 rpc_save_sigmask(oldset, !RPC_TASK_UNINTERRUPTIBLE(task));
411}
412
413static inline void rpc_restore_sigmask(sigset_t *oldset)
414{
415 sigprocmask(SIG_SETMASK, oldset, NULL);
416}
417
1da177e4
LT
418void rpc_clnt_sigmask(struct rpc_clnt *clnt, sigset_t *oldset)
419{
14b218a8 420 rpc_save_sigmask(oldset, clnt->cl_intr);
1da177e4
LT
421}
422
423void rpc_clnt_sigunmask(struct rpc_clnt *clnt, sigset_t *oldset)
424{
14b218a8 425 rpc_restore_sigmask(oldset);
1da177e4
LT
426}
427
428/*
429 * New rpc_call implementation
430 */
431int rpc_call_sync(struct rpc_clnt *clnt, struct rpc_message *msg, int flags)
432{
433 struct rpc_task *task;
434 sigset_t oldset;
435 int status;
436
437 /* If this client is slain all further I/O fails */
438 if (clnt->cl_dead)
439 return -EIO;
440
441 BUG_ON(flags & RPC_TASK_ASYNC);
442
1da177e4 443 status = -ENOMEM;
963d8fe5 444 task = rpc_new_task(clnt, flags, &rpc_default_ops, NULL);
1da177e4
LT
445 if (task == NULL)
446 goto out;
447
14b218a8
TM
448 /* Mask signals on RPC calls _and_ GSS_AUTH upcalls */
449 rpc_task_sigmask(task, &oldset);
450
1da177e4
LT
451 rpc_call_setup(task, msg, 0);
452
453 /* Set up the call info struct and execute the task */
e60859ac
TM
454 status = task->tk_status;
455 if (status == 0) {
456 atomic_inc(&task->tk_count);
1da177e4 457 status = rpc_execute(task);
e60859ac
TM
458 if (status == 0)
459 status = task->tk_status;
1da177e4 460 }
14b218a8 461 rpc_restore_sigmask(&oldset);
e60859ac 462 rpc_release_task(task);
1da177e4 463out:
1da177e4
LT
464 return status;
465}
466
467/*
468 * New rpc_call implementation
469 */
470int
471rpc_call_async(struct rpc_clnt *clnt, struct rpc_message *msg, int flags,
963d8fe5 472 const struct rpc_call_ops *tk_ops, void *data)
1da177e4
LT
473{
474 struct rpc_task *task;
475 sigset_t oldset;
476 int status;
477
478 /* If this client is slain all further I/O fails */
479 if (clnt->cl_dead)
480 return -EIO;
481
482 flags |= RPC_TASK_ASYNC;
483
1da177e4 484 /* Create/initialize a new RPC task */
1da177e4 485 status = -ENOMEM;
963d8fe5 486 if (!(task = rpc_new_task(clnt, flags, tk_ops, data)))
1da177e4 487 goto out;
1da177e4 488
14b218a8
TM
489 /* Mask signals on GSS_AUTH upcalls */
490 rpc_task_sigmask(task, &oldset);
491
1da177e4
LT
492 rpc_call_setup(task, msg, 0);
493
494 /* Set up the call info struct and execute the task */
495 status = task->tk_status;
496 if (status == 0)
497 rpc_execute(task);
498 else
499 rpc_release_task(task);
500
14b218a8 501 rpc_restore_sigmask(&oldset);
1da177e4 502out:
1da177e4
LT
503 return status;
504}
505
506
507void
508rpc_call_setup(struct rpc_task *task, struct rpc_message *msg, int flags)
509{
510 task->tk_msg = *msg;
511 task->tk_flags |= flags;
512 /* Bind the user cred */
513 if (task->tk_msg.rpc_cred != NULL)
514 rpcauth_holdcred(task);
515 else
516 rpcauth_bindcred(task);
517
518 if (task->tk_status == 0)
519 task->tk_action = call_start;
520 else
abbcf28f 521 task->tk_action = rpc_exit_task;
1da177e4
LT
522}
523
524void
525rpc_setbufsize(struct rpc_clnt *clnt, unsigned int sndsize, unsigned int rcvsize)
526{
527 struct rpc_xprt *xprt = clnt->cl_xprt;
470056c2
CL
528 if (xprt->ops->set_buffer_size)
529 xprt->ops->set_buffer_size(xprt, sndsize, rcvsize);
1da177e4
LT
530}
531
532/*
533 * Return size of largest payload RPC client can support, in bytes
534 *
535 * For stream transports, this is one RPC record fragment (see RFC
536 * 1831), as we don't support multi-record requests yet. For datagram
537 * transports, this is the size of an IP packet minus the IP, UDP, and
538 * RPC header sizes.
539 */
540size_t rpc_max_payload(struct rpc_clnt *clnt)
541{
542 return clnt->cl_xprt->max_payload;
543}
544EXPORT_SYMBOL(rpc_max_payload);
545
35f5a422
CL
546/**
547 * rpc_force_rebind - force transport to check that remote port is unchanged
548 * @clnt: client to rebind
549 *
550 */
551void rpc_force_rebind(struct rpc_clnt *clnt)
552{
553 if (clnt->cl_autobind)
554 clnt->cl_port = 0;
555}
556EXPORT_SYMBOL(rpc_force_rebind);
557
1da177e4
LT
558/*
559 * Restart an (async) RPC call. Usually called from within the
560 * exit handler.
561 */
562void
563rpc_restart_call(struct rpc_task *task)
564{
565 if (RPC_ASSASSINATED(task))
566 return;
567
568 task->tk_action = call_start;
569}
570
571/*
572 * 0. Initial state
573 *
574 * Other FSM states can be visited zero or more times, but
575 * this state is visited exactly once for each RPC.
576 */
577static void
578call_start(struct rpc_task *task)
579{
580 struct rpc_clnt *clnt = task->tk_client;
581
582 dprintk("RPC: %4d call_start %s%d proc %d (%s)\n", task->tk_pid,
583 clnt->cl_protname, clnt->cl_vers, task->tk_msg.rpc_proc->p_proc,
584 (RPC_IS_ASYNC(task) ? "async" : "sync"));
585
586 /* Increment call count */
587 task->tk_msg.rpc_proc->p_count++;
588 clnt->cl_stats->rpccnt++;
589 task->tk_action = call_reserve;
590}
591
592/*
593 * 1. Reserve an RPC call slot
594 */
595static void
596call_reserve(struct rpc_task *task)
597{
598 dprintk("RPC: %4d call_reserve\n", task->tk_pid);
599
600 if (!rpcauth_uptodatecred(task)) {
601 task->tk_action = call_refresh;
602 return;
603 }
604
605 task->tk_status = 0;
606 task->tk_action = call_reserveresult;
607 xprt_reserve(task);
608}
609
610/*
611 * 1b. Grok the result of xprt_reserve()
612 */
613static void
614call_reserveresult(struct rpc_task *task)
615{
616 int status = task->tk_status;
617
618 dprintk("RPC: %4d call_reserveresult (status %d)\n",
619 task->tk_pid, task->tk_status);
620
621 /*
622 * After a call to xprt_reserve(), we must have either
623 * a request slot or else an error status.
624 */
625 task->tk_status = 0;
626 if (status >= 0) {
627 if (task->tk_rqstp) {
628 task->tk_action = call_allocate;
629 return;
630 }
631
632 printk(KERN_ERR "%s: status=%d, but no request slot, exiting\n",
633 __FUNCTION__, status);
634 rpc_exit(task, -EIO);
635 return;
636 }
637
638 /*
639 * Even though there was an error, we may have acquired
640 * a request slot somehow. Make sure not to leak it.
641 */
642 if (task->tk_rqstp) {
643 printk(KERN_ERR "%s: status=%d, request allocated anyway\n",
644 __FUNCTION__, status);
645 xprt_release(task);
646 }
647
648 switch (status) {
649 case -EAGAIN: /* woken up; retry */
650 task->tk_action = call_reserve;
651 return;
652 case -EIO: /* probably a shutdown */
653 break;
654 default:
655 printk(KERN_ERR "%s: unrecognized error %d, exiting\n",
656 __FUNCTION__, status);
657 break;
658 }
659 rpc_exit(task, status);
660}
661
662/*
663 * 2. Allocate the buffer. For details, see sched.c:rpc_malloc.
02107148 664 * (Note: buffer memory is freed in xprt_release).
1da177e4
LT
665 */
666static void
667call_allocate(struct rpc_task *task)
668{
02107148
CL
669 struct rpc_rqst *req = task->tk_rqstp;
670 struct rpc_xprt *xprt = task->tk_xprt;
1da177e4
LT
671 unsigned int bufsiz;
672
673 dprintk("RPC: %4d call_allocate (status %d)\n",
674 task->tk_pid, task->tk_status);
675 task->tk_action = call_bind;
02107148 676 if (req->rq_buffer)
1da177e4
LT
677 return;
678
679 /* FIXME: compute buffer requirements more exactly using
680 * auth->au_wslack */
681 bufsiz = task->tk_msg.rpc_proc->p_bufsiz + RPC_SLACK_SPACE;
682
02107148 683 if (xprt->ops->buf_alloc(task, bufsiz << 1) != NULL)
1da177e4
LT
684 return;
685 printk(KERN_INFO "RPC: buffer allocation failed for task %p\n", task);
686
14b218a8 687 if (RPC_IS_ASYNC(task) || !signalled()) {
1da177e4
LT
688 xprt_release(task);
689 task->tk_action = call_reserve;
690 rpc_delay(task, HZ>>4);
691 return;
692 }
693
694 rpc_exit(task, -ERESTARTSYS);
695}
696
940e3318
TM
697static inline int
698rpc_task_need_encode(struct rpc_task *task)
699{
700 return task->tk_rqstp->rq_snd_buf.len == 0;
701}
702
703static inline void
704rpc_task_force_reencode(struct rpc_task *task)
705{
706 task->tk_rqstp->rq_snd_buf.len = 0;
707}
708
1da177e4
LT
709/*
710 * 3. Encode arguments of an RPC call
711 */
712static void
713call_encode(struct rpc_task *task)
714{
1da177e4
LT
715 struct rpc_rqst *req = task->tk_rqstp;
716 struct xdr_buf *sndbuf = &req->rq_snd_buf;
717 struct xdr_buf *rcvbuf = &req->rq_rcv_buf;
718 unsigned int bufsiz;
719 kxdrproc_t encode;
1da177e4
LT
720 u32 *p;
721
722 dprintk("RPC: %4d call_encode (status %d)\n",
723 task->tk_pid, task->tk_status);
724
725 /* Default buffer setup */
02107148
CL
726 bufsiz = req->rq_bufsize >> 1;
727 sndbuf->head[0].iov_base = (void *)req->rq_buffer;
1da177e4
LT
728 sndbuf->head[0].iov_len = bufsiz;
729 sndbuf->tail[0].iov_len = 0;
730 sndbuf->page_len = 0;
731 sndbuf->len = 0;
732 sndbuf->buflen = bufsiz;
02107148 733 rcvbuf->head[0].iov_base = (void *)((char *)req->rq_buffer + bufsiz);
1da177e4
LT
734 rcvbuf->head[0].iov_len = bufsiz;
735 rcvbuf->tail[0].iov_len = 0;
736 rcvbuf->page_len = 0;
737 rcvbuf->len = 0;
738 rcvbuf->buflen = bufsiz;
739
740 /* Encode header and provided arguments */
741 encode = task->tk_msg.rpc_proc->p_encode;
742 if (!(p = call_header(task))) {
743 printk(KERN_INFO "RPC: call_header failed, exit EIO\n");
744 rpc_exit(task, -EIO);
745 return;
746 }
f3680312
BF
747 if (encode == NULL)
748 return;
749
750 task->tk_status = rpcauth_wrap_req(task, encode, req, p,
751 task->tk_msg.rpc_argp);
752 if (task->tk_status == -ENOMEM) {
753 /* XXX: Is this sane? */
754 rpc_delay(task, 3*HZ);
755 task->tk_status = -EAGAIN;
756 }
1da177e4
LT
757}
758
759/*
760 * 4. Get the server port number if not yet set
761 */
762static void
763call_bind(struct rpc_task *task)
764{
765 struct rpc_clnt *clnt = task->tk_client;
1da177e4 766
da351878
CL
767 dprintk("RPC: %4d call_bind (status %d)\n",
768 task->tk_pid, task->tk_status);
1da177e4 769
da351878 770 task->tk_action = call_connect;
1da177e4 771 if (!clnt->cl_port) {
da351878 772 task->tk_action = call_bind_status;
03bf4b70 773 task->tk_timeout = task->tk_xprt->bind_timeout;
1da177e4
LT
774 rpc_getport(task, clnt);
775 }
776}
777
778/*
da351878
CL
779 * 4a. Sort out bind result
780 */
781static void
782call_bind_status(struct rpc_task *task)
783{
784 int status = -EACCES;
785
786 if (task->tk_status >= 0) {
787 dprintk("RPC: %4d call_bind_status (status %d)\n",
788 task->tk_pid, task->tk_status);
789 task->tk_status = 0;
790 task->tk_action = call_connect;
791 return;
792 }
793
794 switch (task->tk_status) {
795 case -EACCES:
796 dprintk("RPC: %4d remote rpcbind: RPC program/version unavailable\n",
797 task->tk_pid);
ea635a51
CL
798 rpc_delay(task, 3*HZ);
799 goto retry_bind;
da351878
CL
800 case -ETIMEDOUT:
801 dprintk("RPC: %4d rpcbind request timed out\n",
802 task->tk_pid);
803 if (RPC_IS_SOFT(task)) {
804 status = -EIO;
805 break;
806 }
807 goto retry_bind;
808 case -EPFNOSUPPORT:
809 dprintk("RPC: %4d remote rpcbind service unavailable\n",
810 task->tk_pid);
811 break;
812 case -EPROTONOSUPPORT:
813 dprintk("RPC: %4d remote rpcbind version 2 unavailable\n",
814 task->tk_pid);
815 break;
816 default:
817 dprintk("RPC: %4d unrecognized rpcbind error (%d)\n",
818 task->tk_pid, -task->tk_status);
819 status = -EIO;
820 break;
821 }
822
823 rpc_exit(task, status);
824 return;
825
826retry_bind:
827 task->tk_status = 0;
828 task->tk_action = call_bind;
829 return;
830}
831
832/*
833 * 4b. Connect to the RPC server
1da177e4
LT
834 */
835static void
836call_connect(struct rpc_task *task)
837{
da351878 838 struct rpc_xprt *xprt = task->tk_xprt;
1da177e4 839
da351878
CL
840 dprintk("RPC: %4d call_connect xprt %p %s connected\n",
841 task->tk_pid, xprt,
842 (xprt_connected(xprt) ? "is" : "is not"));
1da177e4 843
da351878
CL
844 task->tk_action = call_transmit;
845 if (!xprt_connected(xprt)) {
846 task->tk_action = call_connect_status;
847 if (task->tk_status < 0)
848 return;
849 xprt_connect(task);
1da177e4 850 }
1da177e4
LT
851}
852
853/*
da351878 854 * 4c. Sort out connect result
1da177e4
LT
855 */
856static void
857call_connect_status(struct rpc_task *task)
858{
859 struct rpc_clnt *clnt = task->tk_client;
860 int status = task->tk_status;
861
da351878
CL
862 dprintk("RPC: %5u call_connect_status (status %d)\n",
863 task->tk_pid, task->tk_status);
864
1da177e4
LT
865 task->tk_status = 0;
866 if (status >= 0) {
867 clnt->cl_stats->netreconn++;
868 task->tk_action = call_transmit;
869 return;
870 }
871
da351878 872 /* Something failed: remote service port may have changed */
35f5a422 873 rpc_force_rebind(clnt);
da351878 874
1da177e4
LT
875 switch (status) {
876 case -ENOTCONN:
877 case -ETIMEDOUT:
878 case -EAGAIN:
da351878 879 task->tk_action = call_bind;
1da177e4
LT
880 break;
881 default:
882 rpc_exit(task, -EIO);
da351878 883 break;
1da177e4
LT
884 }
885}
886
887/*
888 * 5. Transmit the RPC request, and wait for reply
889 */
890static void
891call_transmit(struct rpc_task *task)
892{
893 dprintk("RPC: %4d call_transmit (status %d)\n",
894 task->tk_pid, task->tk_status);
895
896 task->tk_action = call_status;
897 if (task->tk_status < 0)
898 return;
899 task->tk_status = xprt_prepare_transmit(task);
900 if (task->tk_status != 0)
901 return;
902 /* Encode here so that rpcsec_gss can use correct sequence number. */
940e3318
TM
903 if (rpc_task_need_encode(task)) {
904 task->tk_rqstp->rq_bytes_sent = 0;
1da177e4 905 call_encode(task);
5e5ce5be
TM
906 /* Did the encode result in an error condition? */
907 if (task->tk_status != 0)
908 goto out_nosend;
909 }
940e3318 910 task->tk_action = call_transmit_status;
1da177e4
LT
911 xprt_transmit(task);
912 if (task->tk_status < 0)
913 return;
914 if (!task->tk_msg.rpc_proc->p_decode) {
abbcf28f 915 task->tk_action = rpc_exit_task;
1da177e4
LT
916 rpc_wake_up_task(task);
917 }
5e5ce5be
TM
918 return;
919out_nosend:
920 /* release socket write lock before attempting to handle error */
921 xprt_abort_transmit(task);
940e3318 922 rpc_task_force_reencode(task);
1da177e4
LT
923}
924
925/*
926 * 6. Sort out the RPC call status
927 */
928static void
929call_status(struct rpc_task *task)
930{
931 struct rpc_clnt *clnt = task->tk_client;
932 struct rpc_rqst *req = task->tk_rqstp;
933 int status;
934
935 if (req->rq_received > 0 && !req->rq_bytes_sent)
936 task->tk_status = req->rq_received;
937
938 dprintk("RPC: %4d call_status (status %d)\n",
939 task->tk_pid, task->tk_status);
940
941 status = task->tk_status;
942 if (status >= 0) {
943 task->tk_action = call_decode;
944 return;
945 }
946
947 task->tk_status = 0;
948 switch(status) {
949 case -ETIMEDOUT:
950 task->tk_action = call_timeout;
951 break;
952 case -ECONNREFUSED:
953 case -ENOTCONN:
35f5a422 954 rpc_force_rebind(clnt);
1da177e4
LT
955 task->tk_action = call_bind;
956 break;
957 case -EAGAIN:
958 task->tk_action = call_transmit;
959 break;
960 case -EIO:
961 /* shutdown or soft timeout */
962 rpc_exit(task, status);
963 break;
964 default:
f518e35a 965 printk("%s: RPC call returned error %d\n",
1da177e4
LT
966 clnt->cl_protname, -status);
967 rpc_exit(task, status);
968 break;
969 }
970}
971
972/*
940e3318
TM
973 * 6a. Handle transmission errors.
974 */
975static void
976call_transmit_status(struct rpc_task *task)
977{
978 if (task->tk_status != -EAGAIN)
979 rpc_task_force_reencode(task);
980 call_status(task);
981}
982
983/*
984 * 6b. Handle RPC timeout
1da177e4
LT
985 * We do not release the request slot, so we keep using the
986 * same XID for all retransmits.
987 */
988static void
989call_timeout(struct rpc_task *task)
990{
991 struct rpc_clnt *clnt = task->tk_client;
992
993 if (xprt_adjust_timeout(task->tk_rqstp) == 0) {
994 dprintk("RPC: %4d call_timeout (minor)\n", task->tk_pid);
995 goto retry;
996 }
997
998 dprintk("RPC: %4d call_timeout (major)\n", task->tk_pid);
ef759a2e
CL
999 task->tk_timeouts++;
1000
1da177e4 1001 if (RPC_IS_SOFT(task)) {
f518e35a 1002 printk(KERN_NOTICE "%s: server %s not responding, timed out\n",
1da177e4
LT
1003 clnt->cl_protname, clnt->cl_server);
1004 rpc_exit(task, -EIO);
1005 return;
1006 }
1007
f518e35a 1008 if (!(task->tk_flags & RPC_CALL_MAJORSEEN)) {
1da177e4
LT
1009 task->tk_flags |= RPC_CALL_MAJORSEEN;
1010 printk(KERN_NOTICE "%s: server %s not responding, still trying\n",
1011 clnt->cl_protname, clnt->cl_server);
1012 }
35f5a422 1013 rpc_force_rebind(clnt);
1da177e4
LT
1014
1015retry:
1016 clnt->cl_stats->rpcretrans++;
1017 task->tk_action = call_bind;
1018 task->tk_status = 0;
1019}
1020
1021/*
1022 * 7. Decode the RPC reply
1023 */
1024static void
1025call_decode(struct rpc_task *task)
1026{
1027 struct rpc_clnt *clnt = task->tk_client;
1028 struct rpc_rqst *req = task->tk_rqstp;
1029 kxdrproc_t decode = task->tk_msg.rpc_proc->p_decode;
1030 u32 *p;
1031
1032 dprintk("RPC: %4d call_decode (status %d)\n",
1033 task->tk_pid, task->tk_status);
1034
f518e35a 1035 if (task->tk_flags & RPC_CALL_MAJORSEEN) {
1da177e4
LT
1036 printk(KERN_NOTICE "%s: server %s OK\n",
1037 clnt->cl_protname, clnt->cl_server);
1038 task->tk_flags &= ~RPC_CALL_MAJORSEEN;
1039 }
1040
1041 if (task->tk_status < 12) {
1042 if (!RPC_IS_SOFT(task)) {
1043 task->tk_action = call_bind;
1044 clnt->cl_stats->rpcretrans++;
1045 goto out_retry;
1046 }
1047 printk(KERN_WARNING "%s: too small RPC reply size (%d bytes)\n",
1048 clnt->cl_protname, task->tk_status);
1049 rpc_exit(task, -EIO);
1050 return;
1051 }
1052
1053 req->rq_rcv_buf.len = req->rq_private_buf.len;
1054
1055 /* Check that the softirq receive buffer is valid */
1056 WARN_ON(memcmp(&req->rq_rcv_buf, &req->rq_private_buf,
1057 sizeof(req->rq_rcv_buf)) != 0);
1058
1059 /* Verify the RPC header */
abbcf28f
TM
1060 p = call_verify(task);
1061 if (IS_ERR(p)) {
1062 if (p == ERR_PTR(-EAGAIN))
1063 goto out_retry;
1064 return;
1da177e4
LT
1065 }
1066
abbcf28f 1067 task->tk_action = rpc_exit_task;
1da177e4
LT
1068
1069 if (decode)
1070 task->tk_status = rpcauth_unwrap_resp(task, decode, req, p,
1071 task->tk_msg.rpc_resp);
1072 dprintk("RPC: %4d call_decode result %d\n", task->tk_pid,
1073 task->tk_status);
1074 return;
1075out_retry:
1076 req->rq_received = req->rq_private_buf.len = 0;
1077 task->tk_status = 0;
1078}
1079
1080/*
1081 * 8. Refresh the credentials if rejected by the server
1082 */
1083static void
1084call_refresh(struct rpc_task *task)
1085{
1086 dprintk("RPC: %4d call_refresh\n", task->tk_pid);
1087
1088 xprt_release(task); /* Must do to obtain new XID */
1089 task->tk_action = call_refreshresult;
1090 task->tk_status = 0;
1091 task->tk_client->cl_stats->rpcauthrefresh++;
1092 rpcauth_refreshcred(task);
1093}
1094
1095/*
1096 * 8a. Process the results of a credential refresh
1097 */
1098static void
1099call_refreshresult(struct rpc_task *task)
1100{
1101 int status = task->tk_status;
1102 dprintk("RPC: %4d call_refreshresult (status %d)\n",
1103 task->tk_pid, task->tk_status);
1104
1105 task->tk_status = 0;
1106 task->tk_action = call_reserve;
1107 if (status >= 0 && rpcauth_uptodatecred(task))
1108 return;
1109 if (status == -EACCES) {
1110 rpc_exit(task, -EACCES);
1111 return;
1112 }
1113 task->tk_action = call_refresh;
1114 if (status != -ETIMEDOUT)
1115 rpc_delay(task, 3*HZ);
1116 return;
1117}
1118
1119/*
1120 * Call header serialization
1121 */
1122static u32 *
1123call_header(struct rpc_task *task)
1124{
1125 struct rpc_clnt *clnt = task->tk_client;
1da177e4
LT
1126 struct rpc_rqst *req = task->tk_rqstp;
1127 u32 *p = req->rq_svec[0].iov_base;
1128
1129 /* FIXME: check buffer size? */
808012fb
CL
1130
1131 p = xprt_skip_transport_header(task->tk_xprt, p);
1da177e4
LT
1132 *p++ = req->rq_xid; /* XID */
1133 *p++ = htonl(RPC_CALL); /* CALL */
1134 *p++ = htonl(RPC_VERSION); /* RPC version */
1135 *p++ = htonl(clnt->cl_prog); /* program number */
1136 *p++ = htonl(clnt->cl_vers); /* program version */
1137 *p++ = htonl(task->tk_msg.rpc_proc->p_proc); /* procedure */
334ccfd5
TM
1138 p = rpcauth_marshcred(task, p);
1139 req->rq_slen = xdr_adjust_iovec(&req->rq_svec[0], p);
1140 return p;
1da177e4
LT
1141}
1142
1143/*
1144 * Reply header verification
1145 */
1146static u32 *
1147call_verify(struct rpc_task *task)
1148{
1149 struct kvec *iov = &task->tk_rqstp->rq_rcv_buf.head[0];
1150 int len = task->tk_rqstp->rq_rcv_buf.len >> 2;
1151 u32 *p = iov->iov_base, n;
1152 int error = -EACCES;
1153
1154 if ((len -= 3) < 0)
1155 goto out_overflow;
1156 p += 1; /* skip XID */
1157
1158 if ((n = ntohl(*p++)) != RPC_REPLY) {
1159 printk(KERN_WARNING "call_verify: not an RPC reply: %x\n", n);
abbcf28f 1160 goto out_garbage;
1da177e4
LT
1161 }
1162 if ((n = ntohl(*p++)) != RPC_MSG_ACCEPTED) {
1163 if (--len < 0)
1164 goto out_overflow;
1165 switch ((n = ntohl(*p++))) {
1166 case RPC_AUTH_ERROR:
1167 break;
1168 case RPC_MISMATCH:
cdf47706
AG
1169 dprintk("%s: RPC call version mismatch!\n", __FUNCTION__);
1170 error = -EPROTONOSUPPORT;
1171 goto out_err;
1da177e4 1172 default:
cdf47706 1173 dprintk("%s: RPC call rejected, unknown error: %x\n", __FUNCTION__, n);
1da177e4
LT
1174 goto out_eio;
1175 }
1176 if (--len < 0)
1177 goto out_overflow;
1178 switch ((n = ntohl(*p++))) {
1179 case RPC_AUTH_REJECTEDCRED:
1180 case RPC_AUTH_REJECTEDVERF:
1181 case RPCSEC_GSS_CREDPROBLEM:
1182 case RPCSEC_GSS_CTXPROBLEM:
1183 if (!task->tk_cred_retry)
1184 break;
1185 task->tk_cred_retry--;
1186 dprintk("RPC: %4d call_verify: retry stale creds\n",
1187 task->tk_pid);
1188 rpcauth_invalcred(task);
1189 task->tk_action = call_refresh;
abbcf28f 1190 goto out_retry;
1da177e4
LT
1191 case RPC_AUTH_BADCRED:
1192 case RPC_AUTH_BADVERF:
1193 /* possibly garbled cred/verf? */
1194 if (!task->tk_garb_retry)
1195 break;
1196 task->tk_garb_retry--;
1197 dprintk("RPC: %4d call_verify: retry garbled creds\n",
1198 task->tk_pid);
1199 task->tk_action = call_bind;
abbcf28f 1200 goto out_retry;
1da177e4 1201 case RPC_AUTH_TOOWEAK:
1356b8c2
LS
1202 printk(KERN_NOTICE "call_verify: server %s requires stronger "
1203 "authentication.\n", task->tk_client->cl_server);
1da177e4
LT
1204 break;
1205 default:
1206 printk(KERN_WARNING "call_verify: unknown auth error: %x\n", n);
1207 error = -EIO;
1208 }
1209 dprintk("RPC: %4d call_verify: call rejected %d\n",
1210 task->tk_pid, n);
1211 goto out_err;
1212 }
1213 if (!(p = rpcauth_checkverf(task, p))) {
1214 printk(KERN_WARNING "call_verify: auth check failed\n");
abbcf28f 1215 goto out_garbage; /* bad verifier, retry */
1da177e4
LT
1216 }
1217 len = p - (u32 *)iov->iov_base - 1;
1218 if (len < 0)
1219 goto out_overflow;
1220 switch ((n = ntohl(*p++))) {
1221 case RPC_SUCCESS:
1222 return p;
1223 case RPC_PROG_UNAVAIL:
cdf47706 1224 dprintk("RPC: call_verify: program %u is unsupported by server %s\n",
1da177e4
LT
1225 (unsigned int)task->tk_client->cl_prog,
1226 task->tk_client->cl_server);
cdf47706
AG
1227 error = -EPFNOSUPPORT;
1228 goto out_err;
1da177e4 1229 case RPC_PROG_MISMATCH:
cdf47706 1230 dprintk("RPC: call_verify: program %u, version %u unsupported by server %s\n",
1da177e4
LT
1231 (unsigned int)task->tk_client->cl_prog,
1232 (unsigned int)task->tk_client->cl_vers,
1233 task->tk_client->cl_server);
cdf47706
AG
1234 error = -EPROTONOSUPPORT;
1235 goto out_err;
1da177e4 1236 case RPC_PROC_UNAVAIL:
cdf47706 1237 dprintk("RPC: call_verify: proc %p unsupported by program %u, version %u on server %s\n",
1da177e4
LT
1238 task->tk_msg.rpc_proc,
1239 task->tk_client->cl_prog,
1240 task->tk_client->cl_vers,
1241 task->tk_client->cl_server);
cdf47706
AG
1242 error = -EOPNOTSUPP;
1243 goto out_err;
1da177e4
LT
1244 case RPC_GARBAGE_ARGS:
1245 dprintk("RPC: %4d %s: server saw garbage\n", task->tk_pid, __FUNCTION__);
1246 break; /* retry */
1247 default:
1248 printk(KERN_WARNING "call_verify: server accept status: %x\n", n);
1249 /* Also retry */
1250 }
1251
abbcf28f 1252out_garbage:
1da177e4
LT
1253 task->tk_client->cl_stats->rpcgarbage++;
1254 if (task->tk_garb_retry) {
1255 task->tk_garb_retry--;
cdf47706 1256 dprintk("RPC %s: retrying %4d\n", __FUNCTION__, task->tk_pid);
1da177e4 1257 task->tk_action = call_bind;
abbcf28f
TM
1258out_retry:
1259 return ERR_PTR(-EAGAIN);
1da177e4
LT
1260 }
1261 printk(KERN_WARNING "RPC %s: retry failed, exit EIO\n", __FUNCTION__);
1262out_eio:
1263 error = -EIO;
1264out_err:
1265 rpc_exit(task, error);
abbcf28f 1266 return ERR_PTR(error);
1da177e4
LT
1267out_overflow:
1268 printk(KERN_WARNING "RPC %s: server reply was truncated.\n", __FUNCTION__);
abbcf28f 1269 goto out_garbage;
1da177e4 1270}
5ee0ed7d
TM
1271
1272static int rpcproc_encode_null(void *rqstp, u32 *data, void *obj)
1273{
1274 return 0;
1275}
1276
1277static int rpcproc_decode_null(void *rqstp, u32 *data, void *obj)
1278{
1279 return 0;
1280}
1281
1282static struct rpc_procinfo rpcproc_null = {
1283 .p_encode = rpcproc_encode_null,
1284 .p_decode = rpcproc_decode_null,
1285};
1286
1287int rpc_ping(struct rpc_clnt *clnt, int flags)
1288{
1289 struct rpc_message msg = {
1290 .rpc_proc = &rpcproc_null,
1291 };
1292 int err;
1293 msg.rpc_cred = authnull_ops.lookup_cred(NULL, NULL, 0);
1294 err = rpc_call_sync(clnt, &msg, flags);
1295 put_rpccred(msg.rpc_cred);
1296 return err;
1297}