SUNRPC: Allow rpc_init_task() to initialise the rpc_task->tk_msg
[GitHub/moto-9609/android_kernel_motorola_exynos9610.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>
6d5fcb5a 30#include <linux/smp_lock.h>
1da177e4 31#include <linux/utsname.h>
11c556b3 32#include <linux/workqueue.h>
1da177e4
LT
33
34#include <linux/sunrpc/clnt.h>
1da177e4 35#include <linux/sunrpc/rpc_pipe_fs.h>
11c556b3 36#include <linux/sunrpc/metrics.h>
1da177e4
LT
37
38
1da177e4
LT
39#ifdef RPC_DEBUG
40# define RPCDBG_FACILITY RPCDBG_CALL
41#endif
42
46121cf7
CL
43#define dprint_status(t) \
44 dprintk("RPC: %5u %s (status %d)\n", t->tk_pid, \
45 __FUNCTION__, t->tk_status)
46
188fef11
TM
47/*
48 * All RPC clients are linked into this list
49 */
50static LIST_HEAD(all_clients);
51static DEFINE_SPINLOCK(rpc_client_lock);
52
1da177e4
LT
53static DECLARE_WAIT_QUEUE_HEAD(destroy_wait);
54
55
56static void call_start(struct rpc_task *task);
57static void call_reserve(struct rpc_task *task);
58static void call_reserveresult(struct rpc_task *task);
59static void call_allocate(struct rpc_task *task);
60static void call_encode(struct rpc_task *task);
61static void call_decode(struct rpc_task *task);
62static void call_bind(struct rpc_task *task);
da351878 63static void call_bind_status(struct rpc_task *task);
1da177e4
LT
64static void call_transmit(struct rpc_task *task);
65static void call_status(struct rpc_task *task);
940e3318 66static void call_transmit_status(struct rpc_task *task);
1da177e4
LT
67static void call_refresh(struct rpc_task *task);
68static void call_refreshresult(struct rpc_task *task);
69static void call_timeout(struct rpc_task *task);
70static void call_connect(struct rpc_task *task);
71static void call_connect_status(struct rpc_task *task);
d8ed029d
AD
72static __be32 * call_header(struct rpc_task *task);
73static __be32 * call_verify(struct rpc_task *task);
1da177e4 74
64c91a1f
TM
75static int rpc_ping(struct rpc_clnt *clnt, int flags);
76
188fef11
TM
77static void rpc_register_client(struct rpc_clnt *clnt)
78{
79 spin_lock(&rpc_client_lock);
80 list_add(&clnt->cl_clients, &all_clients);
81 spin_unlock(&rpc_client_lock);
82}
83
84static void rpc_unregister_client(struct rpc_clnt *clnt)
85{
86 spin_lock(&rpc_client_lock);
87 list_del(&clnt->cl_clients);
88 spin_unlock(&rpc_client_lock);
89}
1da177e4
LT
90
91static int
92rpc_setup_pipedir(struct rpc_clnt *clnt, char *dir_name)
93{
f134585a 94 static uint32_t clntid;
1da177e4
LT
95 int error;
96
54281548
TM
97 clnt->cl_vfsmnt = ERR_PTR(-ENOENT);
98 clnt->cl_dentry = ERR_PTR(-ENOENT);
1da177e4
LT
99 if (dir_name == NULL)
100 return 0;
54281548
TM
101
102 clnt->cl_vfsmnt = rpc_get_mount();
103 if (IS_ERR(clnt->cl_vfsmnt))
104 return PTR_ERR(clnt->cl_vfsmnt);
105
f134585a
TM
106 for (;;) {
107 snprintf(clnt->cl_pathname, sizeof(clnt->cl_pathname),
108 "%s/clnt%x", dir_name,
109 (unsigned int)clntid++);
110 clnt->cl_pathname[sizeof(clnt->cl_pathname) - 1] = '\0';
111 clnt->cl_dentry = rpc_mkdir(clnt->cl_pathname, clnt);
112 if (!IS_ERR(clnt->cl_dentry))
113 return 0;
1da177e4 114 error = PTR_ERR(clnt->cl_dentry);
f134585a
TM
115 if (error != -EEXIST) {
116 printk(KERN_INFO "RPC: Couldn't create pipefs entry %s, error %d\n",
117 clnt->cl_pathname, error);
54281548 118 rpc_put_mount();
f134585a
TM
119 return error;
120 }
1da177e4
LT
121 }
122}
123
ff9aa5e5 124static struct rpc_clnt * rpc_new_client(struct rpc_xprt *xprt, char *servname, struct rpc_program *program, u32 vers, rpc_authflavor_t flavor)
1da177e4
LT
125{
126 struct rpc_version *version;
127 struct rpc_clnt *clnt = NULL;
6a19275a 128 struct rpc_auth *auth;
1da177e4 129 int err;
06b8d255
CL
130 size_t len;
131
132 /* sanity check the name before trying to print it */
133 err = -EINVAL;
134 len = strlen(servname);
135 if (len > RPC_MAXNETNAMELEN)
136 goto out_no_rpciod;
137 len++;
1da177e4 138
46121cf7
CL
139 dprintk("RPC: creating %s client for %s (xprt %p)\n",
140 program->name, servname, xprt);
1da177e4 141
4ada539e
TM
142 err = rpciod_up();
143 if (err)
144 goto out_no_rpciod;
1da177e4
LT
145 err = -EINVAL;
146 if (!xprt)
712917d1 147 goto out_no_xprt;
1da177e4
LT
148 if (vers >= program->nrvers || !(version = program->version[vers]))
149 goto out_err;
150
151 err = -ENOMEM;
0da974f4 152 clnt = kzalloc(sizeof(*clnt), GFP_KERNEL);
1da177e4
LT
153 if (!clnt)
154 goto out_err;
1da177e4
LT
155 clnt->cl_parent = clnt;
156
157 clnt->cl_server = clnt->cl_inline_name;
1da177e4
LT
158 if (len > sizeof(clnt->cl_inline_name)) {
159 char *buf = kmalloc(len, GFP_KERNEL);
160 if (buf != 0)
161 clnt->cl_server = buf;
162 else
163 len = sizeof(clnt->cl_inline_name);
164 }
165 strlcpy(clnt->cl_server, servname, len);
166
167 clnt->cl_xprt = xprt;
168 clnt->cl_procinfo = version->procs;
169 clnt->cl_maxproc = version->nrprocs;
170 clnt->cl_protname = program->name;
1da177e4
LT
171 clnt->cl_prog = program->number;
172 clnt->cl_vers = version->number;
1da177e4 173 clnt->cl_stats = program->stats;
11c556b3 174 clnt->cl_metrics = rpc_alloc_iostats(clnt);
23bf85ba
TM
175 err = -ENOMEM;
176 if (clnt->cl_metrics == NULL)
177 goto out_no_stats;
3e32a5d9 178 clnt->cl_program = program;
6529eba0 179 INIT_LIST_HEAD(&clnt->cl_tasks);
4bef61ff 180 spin_lock_init(&clnt->cl_lock);
1da177e4 181
ec739ef0 182 if (!xprt_bound(clnt->cl_xprt))
1da177e4
LT
183 clnt->cl_autobind = 1;
184
185 clnt->cl_rtt = &clnt->cl_rtt_default;
186 rpc_init_rtt(&clnt->cl_rtt_default, xprt->timeout.to_initval);
187
34f52e35
TM
188 kref_init(&clnt->cl_kref);
189
1da177e4
LT
190 err = rpc_setup_pipedir(clnt, program->pipe_dir_name);
191 if (err < 0)
192 goto out_no_path;
193
6a19275a
BF
194 auth = rpcauth_create(flavor, clnt);
195 if (IS_ERR(auth)) {
1da177e4
LT
196 printk(KERN_INFO "RPC: Couldn't create auth handle (flavor %u)\n",
197 flavor);
6a19275a 198 err = PTR_ERR(auth);
1da177e4
LT
199 goto out_no_auth;
200 }
201
202 /* save the nodename */
e9ff3990 203 clnt->cl_nodelen = strlen(utsname()->nodename);
1da177e4
LT
204 if (clnt->cl_nodelen > UNX_MAXNODENAME)
205 clnt->cl_nodelen = UNX_MAXNODENAME;
e9ff3990 206 memcpy(clnt->cl_nodename, utsname()->nodename, clnt->cl_nodelen);
6529eba0 207 rpc_register_client(clnt);
1da177e4
LT
208 return clnt;
209
210out_no_auth:
54281548 211 if (!IS_ERR(clnt->cl_dentry)) {
dff02cc1 212 rpc_rmdir(clnt->cl_dentry);
54281548
TM
213 rpc_put_mount();
214 }
1da177e4 215out_no_path:
23bf85ba
TM
216 rpc_free_iostats(clnt->cl_metrics);
217out_no_stats:
1da177e4
LT
218 if (clnt->cl_server != clnt->cl_inline_name)
219 kfree(clnt->cl_server);
220 kfree(clnt);
221out_err:
6b6ca86b 222 xprt_put(xprt);
712917d1 223out_no_xprt:
4ada539e
TM
224 rpciod_down();
225out_no_rpciod:
1da177e4
LT
226 return ERR_PTR(err);
227}
228
c2866763
CL
229/*
230 * rpc_create - create an RPC client and transport with one call
231 * @args: rpc_clnt create argument structure
232 *
233 * Creates and initializes an RPC transport and an RPC client.
234 *
235 * It can ping the server in order to determine if it is up, and to see if
236 * it supports this program and version. RPC_CLNT_CREATE_NOPING disables
237 * this behavior so asynchronous tasks can also use rpc_create.
238 */
239struct rpc_clnt *rpc_create(struct rpc_create_args *args)
240{
241 struct rpc_xprt *xprt;
242 struct rpc_clnt *clnt;
3c341b0b 243 struct xprt_create xprtargs = {
4fa016eb 244 .ident = args->protocol,
d3bc9a1d 245 .srcaddr = args->saddress,
96802a09
FM
246 .dstaddr = args->address,
247 .addrlen = args->addrsize,
248 .timeout = args->timeout
249 };
43780b87 250 char servername[20];
c2866763 251
96802a09 252 xprt = xprt_create_transport(&xprtargs);
c2866763
CL
253 if (IS_ERR(xprt))
254 return (struct rpc_clnt *)xprt;
255
43780b87
CL
256 /*
257 * If the caller chooses not to specify a hostname, whip
258 * up a string representation of the passed-in address.
259 */
260 if (args->servername == NULL) {
261 struct sockaddr_in *addr =
afde94f3 262 (struct sockaddr_in *) args->address;
43780b87
CL
263 snprintf(servername, sizeof(servername), NIPQUAD_FMT,
264 NIPQUAD(addr->sin_addr.s_addr));
265 args->servername = servername;
266 }
267
c2866763
CL
268 /*
269 * By default, kernel RPC client connects from a reserved port.
270 * CAP_NET_BIND_SERVICE will not be set for unprivileged requesters,
271 * but it is always enabled for rpciod, which handles the connect
272 * operation.
273 */
274 xprt->resvport = 1;
275 if (args->flags & RPC_CLNT_CREATE_NONPRIVPORT)
276 xprt->resvport = 0;
277
c2866763
CL
278 clnt = rpc_new_client(xprt, args->servername, args->program,
279 args->version, args->authflavor);
280 if (IS_ERR(clnt))
281 return clnt;
282
283 if (!(args->flags & RPC_CLNT_CREATE_NOPING)) {
284 int err = rpc_ping(clnt, RPC_TASK_SOFT|RPC_TASK_NOINTR);
285 if (err != 0) {
286 rpc_shutdown_client(clnt);
287 return ERR_PTR(err);
288 }
289 }
290
291 clnt->cl_softrtry = 1;
292 if (args->flags & RPC_CLNT_CREATE_HARDRTRY)
293 clnt->cl_softrtry = 0;
294
295 if (args->flags & RPC_CLNT_CREATE_INTR)
296 clnt->cl_intr = 1;
297 if (args->flags & RPC_CLNT_CREATE_AUTOBIND)
298 clnt->cl_autobind = 1;
43d78ef2
CL
299 if (args->flags & RPC_CLNT_CREATE_DISCRTRY)
300 clnt->cl_discrtry = 1;
c2866763
CL
301
302 return clnt;
303}
b86acd50 304EXPORT_SYMBOL_GPL(rpc_create);
c2866763 305
1da177e4
LT
306/*
307 * This function clones the RPC client structure. It allows us to share the
308 * same transport while varying parameters such as the authentication
309 * flavour.
310 */
311struct rpc_clnt *
312rpc_clone_client(struct rpc_clnt *clnt)
313{
314 struct rpc_clnt *new;
3e32a5d9 315 int err = -ENOMEM;
1da177e4 316
e69062b4 317 new = kmemdup(clnt, sizeof(*new), GFP_KERNEL);
1da177e4
LT
318 if (!new)
319 goto out_no_clnt;
d431a555
TM
320 new->cl_parent = clnt;
321 /* Turn off autobind on clones */
322 new->cl_autobind = 0;
323 INIT_LIST_HEAD(&new->cl_tasks);
324 spin_lock_init(&new->cl_lock);
325 rpc_init_rtt(&new->cl_rtt_default, clnt->cl_xprt->timeout.to_initval);
23bf85ba
TM
326 new->cl_metrics = rpc_alloc_iostats(clnt);
327 if (new->cl_metrics == NULL)
328 goto out_no_stats;
34f52e35 329 kref_init(&new->cl_kref);
3e32a5d9
TM
330 err = rpc_setup_pipedir(new, clnt->cl_program->pipe_dir_name);
331 if (err != 0)
332 goto out_no_path;
1da177e4
LT
333 if (new->cl_auth)
334 atomic_inc(&new->cl_auth->au_count);
d431a555
TM
335 xprt_get(clnt->cl_xprt);
336 kref_get(&clnt->cl_kref);
6529eba0 337 rpc_register_client(new);
4ada539e 338 rpciod_up();
1da177e4 339 return new;
3e32a5d9
TM
340out_no_path:
341 rpc_free_iostats(new->cl_metrics);
23bf85ba
TM
342out_no_stats:
343 kfree(new);
1da177e4 344out_no_clnt:
46121cf7 345 dprintk("RPC: %s: returned error %d\n", __FUNCTION__, err);
3e32a5d9 346 return ERR_PTR(err);
1da177e4 347}
e8914c65 348EXPORT_SYMBOL_GPL(rpc_clone_client);
1da177e4
LT
349
350/*
351 * Properly shut down an RPC client, terminating all outstanding
90c5755f 352 * requests.
1da177e4 353 */
4c402b40 354void rpc_shutdown_client(struct rpc_clnt *clnt)
1da177e4 355{
34f52e35
TM
356 dprintk("RPC: shutting down %s client for %s\n",
357 clnt->cl_protname, clnt->cl_server);
1da177e4 358
34f52e35 359 while (!list_empty(&clnt->cl_tasks)) {
1da177e4 360 rpc_killall_tasks(clnt);
532347e2 361 wait_event_timeout(destroy_wait,
34f52e35 362 list_empty(&clnt->cl_tasks), 1*HZ);
1da177e4
LT
363 }
364
4c402b40 365 rpc_release_client(clnt);
1da177e4 366}
e8914c65 367EXPORT_SYMBOL_GPL(rpc_shutdown_client);
1da177e4
LT
368
369/*
34f52e35 370 * Free an RPC client
1da177e4 371 */
34f52e35
TM
372static void
373rpc_free_client(struct kref *kref)
1da177e4 374{
34f52e35 375 struct rpc_clnt *clnt = container_of(kref, struct rpc_clnt, cl_kref);
1da177e4 376
46121cf7 377 dprintk("RPC: destroying %s client for %s\n",
1da177e4 378 clnt->cl_protname, clnt->cl_server);
8f8e7a50 379 if (!IS_ERR(clnt->cl_dentry)) {
dff02cc1 380 rpc_rmdir(clnt->cl_dentry);
8f8e7a50
TM
381 rpc_put_mount();
382 }
3e32a5d9 383 if (clnt->cl_parent != clnt) {
8ad7c892 384 rpc_release_client(clnt->cl_parent);
3e32a5d9
TM
385 goto out_free;
386 }
1da177e4
LT
387 if (clnt->cl_server != clnt->cl_inline_name)
388 kfree(clnt->cl_server);
389out_free:
6529eba0 390 rpc_unregister_client(clnt);
11c556b3
CL
391 rpc_free_iostats(clnt->cl_metrics);
392 clnt->cl_metrics = NULL;
6b6ca86b 393 xprt_put(clnt->cl_xprt);
4ada539e 394 rpciod_down();
1da177e4 395 kfree(clnt);
1da177e4
LT
396}
397
1dd17ec6
TM
398/*
399 * Free an RPC client
400 */
401static void
402rpc_free_auth(struct kref *kref)
403{
404 struct rpc_clnt *clnt = container_of(kref, struct rpc_clnt, cl_kref);
405
406 if (clnt->cl_auth == NULL) {
407 rpc_free_client(kref);
408 return;
409 }
410
411 /*
412 * Note: RPCSEC_GSS may need to send NULL RPC calls in order to
413 * release remaining GSS contexts. This mechanism ensures
414 * that it can do so safely.
415 */
416 kref_init(kref);
417 rpcauth_release(clnt->cl_auth);
418 clnt->cl_auth = NULL;
419 kref_put(kref, rpc_free_client);
420}
421
1da177e4 422/*
34f52e35 423 * Release reference to the RPC client
1da177e4
LT
424 */
425void
426rpc_release_client(struct rpc_clnt *clnt)
427{
34f52e35 428 dprintk("RPC: rpc_release_client(%p)\n", clnt);
1da177e4 429
34f52e35
TM
430 if (list_empty(&clnt->cl_tasks))
431 wake_up(&destroy_wait);
1dd17ec6 432 kref_put(&clnt->cl_kref, rpc_free_auth);
34f52e35
TM
433}
434
007e251f
AG
435/**
436 * rpc_bind_new_program - bind a new RPC program to an existing client
437 * @old - old rpc_client
438 * @program - rpc program to set
439 * @vers - rpc program version
440 *
441 * Clones the rpc client and sets up a new RPC program. This is mainly
442 * of use for enabling different RPC programs to share the same transport.
443 * The Sun NFSv2/v3 ACL protocol can do this.
444 */
445struct rpc_clnt *rpc_bind_new_program(struct rpc_clnt *old,
446 struct rpc_program *program,
89eb21c3 447 u32 vers)
007e251f
AG
448{
449 struct rpc_clnt *clnt;
450 struct rpc_version *version;
451 int err;
452
453 BUG_ON(vers >= program->nrvers || !program->version[vers]);
454 version = program->version[vers];
455 clnt = rpc_clone_client(old);
456 if (IS_ERR(clnt))
457 goto out;
458 clnt->cl_procinfo = version->procs;
459 clnt->cl_maxproc = version->nrprocs;
460 clnt->cl_protname = program->name;
461 clnt->cl_prog = program->number;
462 clnt->cl_vers = version->number;
463 clnt->cl_stats = program->stats;
464 err = rpc_ping(clnt, RPC_TASK_SOFT|RPC_TASK_NOINTR);
465 if (err != 0) {
466 rpc_shutdown_client(clnt);
467 clnt = ERR_PTR(err);
468 }
cca5172a 469out:
007e251f
AG
470 return clnt;
471}
e8914c65 472EXPORT_SYMBOL_GPL(rpc_bind_new_program);
007e251f 473
1da177e4
LT
474/*
475 * Default callback for async RPC calls
476 */
477static void
963d8fe5 478rpc_default_callback(struct rpc_task *task, void *data)
1da177e4
LT
479{
480}
481
963d8fe5
TM
482static const struct rpc_call_ops rpc_default_ops = {
483 .rpc_call_done = rpc_default_callback,
484};
485
1da177e4 486/*
14b218a8 487 * Export the signal mask handling for synchronous code that
1da177e4
LT
488 * sleeps on RPC calls
489 */
2bd61579 490#define RPC_INTR_SIGNALS (sigmask(SIGHUP) | sigmask(SIGINT) | sigmask(SIGQUIT) | sigmask(SIGTERM))
cca5172a 491
14b218a8
TM
492static void rpc_save_sigmask(sigset_t *oldset, int intr)
493{
2bd61579 494 unsigned long sigallow = sigmask(SIGKILL);
14b218a8
TM
495 sigset_t sigmask;
496
497 /* Block all signals except those listed in sigallow */
498 if (intr)
499 sigallow |= RPC_INTR_SIGNALS;
500 siginitsetinv(&sigmask, sigallow);
501 sigprocmask(SIG_BLOCK, &sigmask, oldset);
502}
503
50859259 504static void rpc_task_sigmask(struct rpc_task *task, sigset_t *oldset)
14b218a8
TM
505{
506 rpc_save_sigmask(oldset, !RPC_TASK_UNINTERRUPTIBLE(task));
507}
508
50859259 509static void rpc_restore_sigmask(sigset_t *oldset)
14b218a8
TM
510{
511 sigprocmask(SIG_SETMASK, oldset, NULL);
512}
513
1da177e4
LT
514void rpc_clnt_sigmask(struct rpc_clnt *clnt, sigset_t *oldset)
515{
14b218a8 516 rpc_save_sigmask(oldset, clnt->cl_intr);
1da177e4 517}
e8914c65 518EXPORT_SYMBOL_GPL(rpc_clnt_sigmask);
1da177e4
LT
519
520void rpc_clnt_sigunmask(struct rpc_clnt *clnt, sigset_t *oldset)
521{
14b218a8 522 rpc_restore_sigmask(oldset);
1da177e4 523}
e8914c65 524EXPORT_SYMBOL_GPL(rpc_clnt_sigunmask);
1da177e4 525
c970aa85
TM
526/**
527 * rpc_run_task - Allocate a new RPC task, then run rpc_execute against it
528 * @task_setup_data: pointer to task initialisation data
529 */
530struct rpc_task *rpc_run_task(const struct rpc_task_setup *task_setup_data)
6e5b70e9
TM
531{
532 struct rpc_task *task, *ret;
533 sigset_t oldset;
534
84115e1c 535 task = rpc_new_task(task_setup_data);
6e5b70e9 536 if (task == NULL) {
84115e1c
TM
537 rpc_release_calldata(task_setup_data->callback_ops,
538 task_setup_data->callback_data);
50859259
TM
539 ret = ERR_PTR(-ENOMEM);
540 goto out;
6e5b70e9
TM
541 }
542
b3ef8b3b
TM
543 if (task->tk_status != 0) {
544 ret = ERR_PTR(task->tk_status);
545 rpc_put_task(task);
546 goto out;
6e5b70e9
TM
547 }
548 atomic_inc(&task->tk_count);
50859259
TM
549 /* Mask signals on synchronous RPC calls and RPCSEC_GSS upcalls */
550 rpc_task_sigmask(task, &oldset);
6e5b70e9 551 rpc_execute(task);
50859259 552 rpc_restore_sigmask(&oldset);
6e5b70e9
TM
553 ret = task;
554out:
6e5b70e9
TM
555 return ret;
556}
c970aa85 557EXPORT_SYMBOL_GPL(rpc_run_task);
6e5b70e9
TM
558
559/**
560 * rpc_call_sync - Perform a synchronous RPC call
561 * @clnt: pointer to RPC client
562 * @msg: RPC call parameters
563 * @flags: RPC call flags
1da177e4
LT
564 */
565int rpc_call_sync(struct rpc_clnt *clnt, struct rpc_message *msg, int flags)
566{
567 struct rpc_task *task;
84115e1c
TM
568 struct rpc_task_setup task_setup_data = {
569 .rpc_client = clnt,
570 .rpc_message = msg,
571 .callback_ops = &rpc_default_ops,
572 .flags = flags,
573 };
6e5b70e9 574 int status;
1da177e4 575
1da177e4
LT
576 BUG_ON(flags & RPC_TASK_ASYNC);
577
c970aa85 578 task = rpc_run_task(&task_setup_data);
6e5b70e9
TM
579 if (IS_ERR(task))
580 return PTR_ERR(task);
e60859ac 581 status = task->tk_status;
bde8f00c 582 rpc_put_task(task);
1da177e4
LT
583 return status;
584}
e8914c65 585EXPORT_SYMBOL_GPL(rpc_call_sync);
1da177e4 586
6e5b70e9
TM
587/**
588 * rpc_call_async - Perform an asynchronous RPC call
589 * @clnt: pointer to RPC client
590 * @msg: RPC call parameters
591 * @flags: RPC call flags
592 * @ops: RPC call ops
593 * @data: user call data
1da177e4
LT
594 */
595int
596rpc_call_async(struct rpc_clnt *clnt, struct rpc_message *msg, int flags,
963d8fe5 597 const struct rpc_call_ops *tk_ops, void *data)
1da177e4
LT
598{
599 struct rpc_task *task;
84115e1c
TM
600 struct rpc_task_setup task_setup_data = {
601 .rpc_client = clnt,
602 .rpc_message = msg,
603 .callback_ops = tk_ops,
604 .callback_data = data,
605 .flags = flags|RPC_TASK_ASYNC,
606 };
1da177e4 607
c970aa85 608 task = rpc_run_task(&task_setup_data);
6e5b70e9
TM
609 if (IS_ERR(task))
610 return PTR_ERR(task);
611 rpc_put_task(task);
612 return 0;
1da177e4 613}
e8914c65 614EXPORT_SYMBOL_GPL(rpc_call_async);
1da177e4 615
1da177e4 616void
84115e1c 617rpc_call_setup(struct rpc_task *task, const struct rpc_message *msg, int flags)
1da177e4
LT
618{
619 task->tk_msg = *msg;
620 task->tk_flags |= flags;
621 /* Bind the user cred */
622 if (task->tk_msg.rpc_cred != NULL)
623 rpcauth_holdcred(task);
624 else
625 rpcauth_bindcred(task);
626
627 if (task->tk_status == 0)
628 task->tk_action = call_start;
629 else
abbcf28f 630 task->tk_action = rpc_exit_task;
1da177e4 631}
e8914c65 632EXPORT_SYMBOL_GPL(rpc_call_setup);
1da177e4 633
77de2c59
TM
634void
635rpc_call_start(struct rpc_task *task)
636{
637 task->tk_action = call_start;
638}
639EXPORT_SYMBOL_GPL(rpc_call_start);
640
ed39440a
CL
641/**
642 * rpc_peeraddr - extract remote peer address from clnt's xprt
643 * @clnt: RPC client structure
644 * @buf: target buffer
645 * @size: length of target buffer
646 *
647 * Returns the number of bytes that are actually in the stored address.
648 */
649size_t rpc_peeraddr(struct rpc_clnt *clnt, struct sockaddr *buf, size_t bufsize)
650{
651 size_t bytes;
652 struct rpc_xprt *xprt = clnt->cl_xprt;
653
654 bytes = sizeof(xprt->addr);
655 if (bytes > bufsize)
656 bytes = bufsize;
657 memcpy(buf, &clnt->cl_xprt->addr, bytes);
c4efcb1d 658 return xprt->addrlen;
ed39440a 659}
b86acd50 660EXPORT_SYMBOL_GPL(rpc_peeraddr);
ed39440a 661
f425eba4
CL
662/**
663 * rpc_peeraddr2str - return remote peer address in printable format
664 * @clnt: RPC client structure
665 * @format: address format
666 *
667 */
668char *rpc_peeraddr2str(struct rpc_clnt *clnt, enum rpc_display_format_t format)
669{
670 struct rpc_xprt *xprt = clnt->cl_xprt;
7559c7a2
CL
671
672 if (xprt->address_strings[format] != NULL)
673 return xprt->address_strings[format];
674 else
675 return "unprintable";
f425eba4 676}
b86acd50 677EXPORT_SYMBOL_GPL(rpc_peeraddr2str);
f425eba4 678
1da177e4
LT
679void
680rpc_setbufsize(struct rpc_clnt *clnt, unsigned int sndsize, unsigned int rcvsize)
681{
682 struct rpc_xprt *xprt = clnt->cl_xprt;
470056c2
CL
683 if (xprt->ops->set_buffer_size)
684 xprt->ops->set_buffer_size(xprt, sndsize, rcvsize);
1da177e4 685}
e8914c65 686EXPORT_SYMBOL_GPL(rpc_setbufsize);
1da177e4
LT
687
688/*
689 * Return size of largest payload RPC client can support, in bytes
690 *
691 * For stream transports, this is one RPC record fragment (see RFC
692 * 1831), as we don't support multi-record requests yet. For datagram
693 * transports, this is the size of an IP packet minus the IP, UDP, and
694 * RPC header sizes.
695 */
696size_t rpc_max_payload(struct rpc_clnt *clnt)
697{
698 return clnt->cl_xprt->max_payload;
699}
b86acd50 700EXPORT_SYMBOL_GPL(rpc_max_payload);
1da177e4 701
35f5a422
CL
702/**
703 * rpc_force_rebind - force transport to check that remote port is unchanged
704 * @clnt: client to rebind
705 *
706 */
707void rpc_force_rebind(struct rpc_clnt *clnt)
708{
709 if (clnt->cl_autobind)
ec739ef0 710 xprt_clear_bound(clnt->cl_xprt);
35f5a422 711}
b86acd50 712EXPORT_SYMBOL_GPL(rpc_force_rebind);
35f5a422 713
1da177e4
LT
714/*
715 * Restart an (async) RPC call. Usually called from within the
716 * exit handler.
717 */
718void
719rpc_restart_call(struct rpc_task *task)
720{
721 if (RPC_ASSASSINATED(task))
722 return;
723
724 task->tk_action = call_start;
725}
e8914c65 726EXPORT_SYMBOL_GPL(rpc_restart_call);
1da177e4
LT
727
728/*
729 * 0. Initial state
730 *
731 * Other FSM states can be visited zero or more times, but
732 * this state is visited exactly once for each RPC.
733 */
734static void
735call_start(struct rpc_task *task)
736{
737 struct rpc_clnt *clnt = task->tk_client;
738
46121cf7
CL
739 dprintk("RPC: %5u call_start %s%d proc %d (%s)\n", task->tk_pid,
740 clnt->cl_protname, clnt->cl_vers,
741 task->tk_msg.rpc_proc->p_proc,
742 (RPC_IS_ASYNC(task) ? "async" : "sync"));
1da177e4
LT
743
744 /* Increment call count */
745 task->tk_msg.rpc_proc->p_count++;
746 clnt->cl_stats->rpccnt++;
747 task->tk_action = call_reserve;
748}
749
750/*
751 * 1. Reserve an RPC call slot
752 */
753static void
754call_reserve(struct rpc_task *task)
755{
46121cf7 756 dprint_status(task);
1da177e4
LT
757
758 if (!rpcauth_uptodatecred(task)) {
759 task->tk_action = call_refresh;
760 return;
761 }
762
763 task->tk_status = 0;
764 task->tk_action = call_reserveresult;
765 xprt_reserve(task);
766}
767
768/*
769 * 1b. Grok the result of xprt_reserve()
770 */
771static void
772call_reserveresult(struct rpc_task *task)
773{
774 int status = task->tk_status;
775
46121cf7 776 dprint_status(task);
1da177e4
LT
777
778 /*
779 * After a call to xprt_reserve(), we must have either
780 * a request slot or else an error status.
781 */
782 task->tk_status = 0;
783 if (status >= 0) {
784 if (task->tk_rqstp) {
785 task->tk_action = call_allocate;
786 return;
787 }
788
789 printk(KERN_ERR "%s: status=%d, but no request slot, exiting\n",
790 __FUNCTION__, status);
791 rpc_exit(task, -EIO);
792 return;
793 }
794
795 /*
796 * Even though there was an error, we may have acquired
797 * a request slot somehow. Make sure not to leak it.
798 */
799 if (task->tk_rqstp) {
800 printk(KERN_ERR "%s: status=%d, request allocated anyway\n",
801 __FUNCTION__, status);
802 xprt_release(task);
803 }
804
805 switch (status) {
806 case -EAGAIN: /* woken up; retry */
807 task->tk_action = call_reserve;
808 return;
809 case -EIO: /* probably a shutdown */
810 break;
811 default:
812 printk(KERN_ERR "%s: unrecognized error %d, exiting\n",
813 __FUNCTION__, status);
814 break;
815 }
816 rpc_exit(task, status);
817}
818
819/*
820 * 2. Allocate the buffer. For details, see sched.c:rpc_malloc.
02107148 821 * (Note: buffer memory is freed in xprt_release).
1da177e4
LT
822 */
823static void
824call_allocate(struct rpc_task *task)
825{
1be27f36 826 unsigned int slack = task->tk_msg.rpc_cred->cr_auth->au_cslack;
02107148
CL
827 struct rpc_rqst *req = task->tk_rqstp;
828 struct rpc_xprt *xprt = task->tk_xprt;
2bea90d4 829 struct rpc_procinfo *proc = task->tk_msg.rpc_proc;
1da177e4 830
46121cf7
CL
831 dprint_status(task);
832
2bea90d4 833 task->tk_status = 0;
1da177e4 834 task->tk_action = call_bind;
2bea90d4 835
02107148 836 if (req->rq_buffer)
1da177e4
LT
837 return;
838
2bea90d4
CL
839 if (proc->p_proc != 0) {
840 BUG_ON(proc->p_arglen == 0);
841 if (proc->p_decode != NULL)
842 BUG_ON(proc->p_replen == 0);
843 }
1da177e4 844
2bea90d4
CL
845 /*
846 * Calculate the size (in quads) of the RPC call
847 * and reply headers, and convert both values
848 * to byte sizes.
849 */
850 req->rq_callsize = RPC_CALLHDRSIZE + (slack << 1) + proc->p_arglen;
851 req->rq_callsize <<= 2;
852 req->rq_rcvsize = RPC_REPHDRSIZE + slack + proc->p_replen;
853 req->rq_rcvsize <<= 2;
854
c5a4dd8b
CL
855 req->rq_buffer = xprt->ops->buf_alloc(task,
856 req->rq_callsize + req->rq_rcvsize);
2bea90d4 857 if (req->rq_buffer != NULL)
1da177e4 858 return;
46121cf7
CL
859
860 dprintk("RPC: %5u rpc_buffer allocation failed\n", task->tk_pid);
1da177e4 861
14b218a8 862 if (RPC_IS_ASYNC(task) || !signalled()) {
b6e9c713 863 task->tk_action = call_allocate;
1da177e4
LT
864 rpc_delay(task, HZ>>4);
865 return;
866 }
867
868 rpc_exit(task, -ERESTARTSYS);
869}
870
940e3318
TM
871static inline int
872rpc_task_need_encode(struct rpc_task *task)
873{
874 return task->tk_rqstp->rq_snd_buf.len == 0;
875}
876
877static inline void
878rpc_task_force_reencode(struct rpc_task *task)
879{
880 task->tk_rqstp->rq_snd_buf.len = 0;
881}
882
2bea90d4
CL
883static inline void
884rpc_xdr_buf_init(struct xdr_buf *buf, void *start, size_t len)
885{
886 buf->head[0].iov_base = start;
887 buf->head[0].iov_len = len;
888 buf->tail[0].iov_len = 0;
889 buf->page_len = 0;
4f22ccc3 890 buf->flags = 0;
2bea90d4
CL
891 buf->len = 0;
892 buf->buflen = len;
893}
894
1da177e4
LT
895/*
896 * 3. Encode arguments of an RPC call
897 */
898static void
899call_encode(struct rpc_task *task)
900{
1da177e4 901 struct rpc_rqst *req = task->tk_rqstp;
1da177e4 902 kxdrproc_t encode;
d8ed029d 903 __be32 *p;
1da177e4 904
46121cf7 905 dprint_status(task);
1da177e4 906
2bea90d4
CL
907 rpc_xdr_buf_init(&req->rq_snd_buf,
908 req->rq_buffer,
909 req->rq_callsize);
910 rpc_xdr_buf_init(&req->rq_rcv_buf,
911 (char *)req->rq_buffer + req->rq_callsize,
912 req->rq_rcvsize);
1da177e4
LT
913
914 /* Encode header and provided arguments */
915 encode = task->tk_msg.rpc_proc->p_encode;
916 if (!(p = call_header(task))) {
917 printk(KERN_INFO "RPC: call_header failed, exit EIO\n");
918 rpc_exit(task, -EIO);
919 return;
920 }
f3680312
BF
921 if (encode == NULL)
922 return;
923
924 task->tk_status = rpcauth_wrap_req(task, encode, req, p,
925 task->tk_msg.rpc_argp);
926 if (task->tk_status == -ENOMEM) {
927 /* XXX: Is this sane? */
928 rpc_delay(task, 3*HZ);
929 task->tk_status = -EAGAIN;
930 }
1da177e4
LT
931}
932
933/*
934 * 4. Get the server port number if not yet set
935 */
936static void
937call_bind(struct rpc_task *task)
938{
ec739ef0 939 struct rpc_xprt *xprt = task->tk_xprt;
1da177e4 940
46121cf7 941 dprint_status(task);
1da177e4 942
da351878 943 task->tk_action = call_connect;
ec739ef0 944 if (!xprt_bound(xprt)) {
da351878 945 task->tk_action = call_bind_status;
ec739ef0 946 task->tk_timeout = xprt->bind_timeout;
bbf7c1dd 947 xprt->ops->rpcbind(task);
1da177e4
LT
948 }
949}
950
951/*
da351878
CL
952 * 4a. Sort out bind result
953 */
954static void
955call_bind_status(struct rpc_task *task)
956{
906462af 957 int status = -EIO;
da351878
CL
958
959 if (task->tk_status >= 0) {
46121cf7 960 dprint_status(task);
da351878
CL
961 task->tk_status = 0;
962 task->tk_action = call_connect;
963 return;
964 }
965
966 switch (task->tk_status) {
2429cbf6
CL
967 case -EAGAIN:
968 dprintk("RPC: %5u rpcbind waiting for another request "
969 "to finish\n", task->tk_pid);
970 /* avoid busy-waiting here -- could be a network outage. */
971 rpc_delay(task, 5*HZ);
972 goto retry_timeout;
da351878 973 case -EACCES:
46121cf7
CL
974 dprintk("RPC: %5u remote rpcbind: RPC program/version "
975 "unavailable\n", task->tk_pid);
b79dc8ce
CL
976 /* fail immediately if this is an RPC ping */
977 if (task->tk_msg.rpc_proc->p_proc == 0) {
978 status = -EOPNOTSUPP;
979 break;
980 }
ea635a51 981 rpc_delay(task, 3*HZ);
da45828e 982 goto retry_timeout;
da351878 983 case -ETIMEDOUT:
46121cf7 984 dprintk("RPC: %5u rpcbind request timed out\n",
da351878 985 task->tk_pid);
da45828e 986 goto retry_timeout;
da351878 987 case -EPFNOSUPPORT:
906462af 988 /* server doesn't support any rpcbind version we know of */
46121cf7 989 dprintk("RPC: %5u remote rpcbind service unavailable\n",
da351878
CL
990 task->tk_pid);
991 break;
992 case -EPROTONOSUPPORT:
00a6e7bb 993 dprintk("RPC: %5u remote rpcbind version unavailable, retrying\n",
da351878 994 task->tk_pid);
00a6e7bb
CL
995 task->tk_status = 0;
996 task->tk_action = call_bind;
997 return;
da351878 998 default:
46121cf7 999 dprintk("RPC: %5u unrecognized rpcbind error (%d)\n",
da351878 1000 task->tk_pid, -task->tk_status);
da351878
CL
1001 }
1002
1003 rpc_exit(task, status);
1004 return;
1005
da45828e
TM
1006retry_timeout:
1007 task->tk_action = call_timeout;
da351878
CL
1008}
1009
1010/*
1011 * 4b. Connect to the RPC server
1da177e4
LT
1012 */
1013static void
1014call_connect(struct rpc_task *task)
1015{
da351878 1016 struct rpc_xprt *xprt = task->tk_xprt;
1da177e4 1017
46121cf7 1018 dprintk("RPC: %5u call_connect xprt %p %s connected\n",
da351878
CL
1019 task->tk_pid, xprt,
1020 (xprt_connected(xprt) ? "is" : "is not"));
1da177e4 1021
da351878
CL
1022 task->tk_action = call_transmit;
1023 if (!xprt_connected(xprt)) {
1024 task->tk_action = call_connect_status;
1025 if (task->tk_status < 0)
1026 return;
1027 xprt_connect(task);
1da177e4 1028 }
1da177e4
LT
1029}
1030
1031/*
da351878 1032 * 4c. Sort out connect result
1da177e4
LT
1033 */
1034static void
1035call_connect_status(struct rpc_task *task)
1036{
1037 struct rpc_clnt *clnt = task->tk_client;
1038 int status = task->tk_status;
1039
46121cf7 1040 dprint_status(task);
da351878 1041
1da177e4
LT
1042 task->tk_status = 0;
1043 if (status >= 0) {
1044 clnt->cl_stats->netreconn++;
1045 task->tk_action = call_transmit;
1046 return;
1047 }
1048
da351878 1049 /* Something failed: remote service port may have changed */
35f5a422 1050 rpc_force_rebind(clnt);
da351878 1051
1da177e4
LT
1052 switch (status) {
1053 case -ENOTCONN:
1da177e4 1054 case -EAGAIN:
da351878 1055 task->tk_action = call_bind;
da45828e
TM
1056 if (!RPC_IS_SOFT(task))
1057 return;
1058 /* if soft mounted, test if we've timed out */
1059 case -ETIMEDOUT:
1060 task->tk_action = call_timeout;
1061 return;
1da177e4 1062 }
da45828e 1063 rpc_exit(task, -EIO);
1da177e4
LT
1064}
1065
1066/*
1067 * 5. Transmit the RPC request, and wait for reply
1068 */
1069static void
1070call_transmit(struct rpc_task *task)
1071{
46121cf7 1072 dprint_status(task);
1da177e4
LT
1073
1074 task->tk_action = call_status;
1075 if (task->tk_status < 0)
1076 return;
1077 task->tk_status = xprt_prepare_transmit(task);
1078 if (task->tk_status != 0)
1079 return;
e0ab53de 1080 task->tk_action = call_transmit_status;
1da177e4 1081 /* Encode here so that rpcsec_gss can use correct sequence number. */
940e3318 1082 if (rpc_task_need_encode(task)) {
e0ab53de 1083 BUG_ON(task->tk_rqstp->rq_bytes_sent != 0);
1da177e4 1084 call_encode(task);
5e5ce5be
TM
1085 /* Did the encode result in an error condition? */
1086 if (task->tk_status != 0)
e0ab53de 1087 return;
5e5ce5be 1088 }
1da177e4
LT
1089 xprt_transmit(task);
1090 if (task->tk_status < 0)
1091 return;
e0ab53de
TM
1092 /*
1093 * On success, ensure that we call xprt_end_transmit() before sleeping
1094 * in order to allow access to the socket to other RPC requests.
1095 */
1096 call_transmit_status(task);
1097 if (task->tk_msg.rpc_proc->p_decode != NULL)
1098 return;
1099 task->tk_action = rpc_exit_task;
1100 rpc_wake_up_task(task);
1101}
1102
1103/*
1104 * 5a. Handle cleanup after a transmission
1105 */
1106static void
1107call_transmit_status(struct rpc_task *task)
1108{
1109 task->tk_action = call_status;
1110 /*
1111 * Special case: if we've been waiting on the socket's write_space()
1112 * callback, then don't call xprt_end_transmit().
1113 */
1114 if (task->tk_status == -EAGAIN)
1115 return;
1116 xprt_end_transmit(task);
940e3318 1117 rpc_task_force_reencode(task);
1da177e4
LT
1118}
1119
1120/*
1121 * 6. Sort out the RPC call status
1122 */
1123static void
1124call_status(struct rpc_task *task)
1125{
1126 struct rpc_clnt *clnt = task->tk_client;
1127 struct rpc_rqst *req = task->tk_rqstp;
1128 int status;
1129
1130 if (req->rq_received > 0 && !req->rq_bytes_sent)
1131 task->tk_status = req->rq_received;
1132
46121cf7 1133 dprint_status(task);
1da177e4
LT
1134
1135 status = task->tk_status;
1136 if (status >= 0) {
1137 task->tk_action = call_decode;
1138 return;
1139 }
1140
1141 task->tk_status = 0;
1142 switch(status) {
76303992
TM
1143 case -EHOSTDOWN:
1144 case -EHOSTUNREACH:
1145 case -ENETUNREACH:
1146 /*
1147 * Delay any retries for 3 seconds, then handle as if it
1148 * were a timeout.
1149 */
1150 rpc_delay(task, 3*HZ);
1da177e4
LT
1151 case -ETIMEDOUT:
1152 task->tk_action = call_timeout;
241c39b9 1153 if (task->tk_client->cl_discrtry)
3ebb067d 1154 xprt_force_disconnect(task->tk_xprt);
1da177e4
LT
1155 break;
1156 case -ECONNREFUSED:
1157 case -ENOTCONN:
35f5a422 1158 rpc_force_rebind(clnt);
1da177e4
LT
1159 task->tk_action = call_bind;
1160 break;
1161 case -EAGAIN:
1162 task->tk_action = call_transmit;
1163 break;
1164 case -EIO:
1165 /* shutdown or soft timeout */
1166 rpc_exit(task, status);
1167 break;
1168 default:
f518e35a 1169 printk("%s: RPC call returned error %d\n",
1da177e4
LT
1170 clnt->cl_protname, -status);
1171 rpc_exit(task, status);
1da177e4
LT
1172 }
1173}
1174
1175/*
e0ab53de 1176 * 6a. Handle RPC timeout
1da177e4
LT
1177 * We do not release the request slot, so we keep using the
1178 * same XID for all retransmits.
1179 */
1180static void
1181call_timeout(struct rpc_task *task)
1182{
1183 struct rpc_clnt *clnt = task->tk_client;
1184
1185 if (xprt_adjust_timeout(task->tk_rqstp) == 0) {
46121cf7 1186 dprintk("RPC: %5u call_timeout (minor)\n", task->tk_pid);
1da177e4
LT
1187 goto retry;
1188 }
1189
46121cf7 1190 dprintk("RPC: %5u call_timeout (major)\n", task->tk_pid);
ef759a2e
CL
1191 task->tk_timeouts++;
1192
1da177e4 1193 if (RPC_IS_SOFT(task)) {
f518e35a 1194 printk(KERN_NOTICE "%s: server %s not responding, timed out\n",
1da177e4
LT
1195 clnt->cl_protname, clnt->cl_server);
1196 rpc_exit(task, -EIO);
1197 return;
1198 }
1199
f518e35a 1200 if (!(task->tk_flags & RPC_CALL_MAJORSEEN)) {
1da177e4
LT
1201 task->tk_flags |= RPC_CALL_MAJORSEEN;
1202 printk(KERN_NOTICE "%s: server %s not responding, still trying\n",
1203 clnt->cl_protname, clnt->cl_server);
1204 }
35f5a422 1205 rpc_force_rebind(clnt);
1da177e4
LT
1206
1207retry:
1208 clnt->cl_stats->rpcretrans++;
1209 task->tk_action = call_bind;
1210 task->tk_status = 0;
1211}
1212
1213/*
1214 * 7. Decode the RPC reply
1215 */
1216static void
1217call_decode(struct rpc_task *task)
1218{
1219 struct rpc_clnt *clnt = task->tk_client;
1220 struct rpc_rqst *req = task->tk_rqstp;
1221 kxdrproc_t decode = task->tk_msg.rpc_proc->p_decode;
d8ed029d 1222 __be32 *p;
1da177e4 1223
46121cf7
CL
1224 dprintk("RPC: %5u call_decode (status %d)\n",
1225 task->tk_pid, task->tk_status);
1da177e4 1226
f518e35a 1227 if (task->tk_flags & RPC_CALL_MAJORSEEN) {
1da177e4
LT
1228 printk(KERN_NOTICE "%s: server %s OK\n",
1229 clnt->cl_protname, clnt->cl_server);
1230 task->tk_flags &= ~RPC_CALL_MAJORSEEN;
1231 }
1232
1233 if (task->tk_status < 12) {
1234 if (!RPC_IS_SOFT(task)) {
1235 task->tk_action = call_bind;
1236 clnt->cl_stats->rpcretrans++;
1237 goto out_retry;
1238 }
46121cf7
CL
1239 dprintk("RPC: %s: too small RPC reply size (%d bytes)\n",
1240 clnt->cl_protname, task->tk_status);
da45828e
TM
1241 task->tk_action = call_timeout;
1242 goto out_retry;
1da177e4
LT
1243 }
1244
43ac3f29
TM
1245 /*
1246 * Ensure that we see all writes made by xprt_complete_rqst()
1247 * before it changed req->rq_received.
1248 */
1249 smp_rmb();
1da177e4
LT
1250 req->rq_rcv_buf.len = req->rq_private_buf.len;
1251
1252 /* Check that the softirq receive buffer is valid */
1253 WARN_ON(memcmp(&req->rq_rcv_buf, &req->rq_private_buf,
1254 sizeof(req->rq_rcv_buf)) != 0);
1255
1256 /* Verify the RPC header */
abbcf28f
TM
1257 p = call_verify(task);
1258 if (IS_ERR(p)) {
1259 if (p == ERR_PTR(-EAGAIN))
1260 goto out_retry;
1261 return;
1da177e4
LT
1262 }
1263
abbcf28f 1264 task->tk_action = rpc_exit_task;
1da177e4 1265
6d5fcb5a 1266 if (decode) {
1da177e4
LT
1267 task->tk_status = rpcauth_unwrap_resp(task, decode, req, p,
1268 task->tk_msg.rpc_resp);
6d5fcb5a 1269 }
46121cf7
CL
1270 dprintk("RPC: %5u call_decode result %d\n", task->tk_pid,
1271 task->tk_status);
1da177e4
LT
1272 return;
1273out_retry:
1274 req->rq_received = req->rq_private_buf.len = 0;
1275 task->tk_status = 0;
241c39b9 1276 if (task->tk_client->cl_discrtry)
3ebb067d 1277 xprt_force_disconnect(task->tk_xprt);
1da177e4
LT
1278}
1279
1280/*
1281 * 8. Refresh the credentials if rejected by the server
1282 */
1283static void
1284call_refresh(struct rpc_task *task)
1285{
46121cf7 1286 dprint_status(task);
1da177e4 1287
1da177e4
LT
1288 task->tk_action = call_refreshresult;
1289 task->tk_status = 0;
1290 task->tk_client->cl_stats->rpcauthrefresh++;
1291 rpcauth_refreshcred(task);
1292}
1293
1294/*
1295 * 8a. Process the results of a credential refresh
1296 */
1297static void
1298call_refreshresult(struct rpc_task *task)
1299{
1300 int status = task->tk_status;
46121cf7
CL
1301
1302 dprint_status(task);
1da177e4
LT
1303
1304 task->tk_status = 0;
1305 task->tk_action = call_reserve;
1306 if (status >= 0 && rpcauth_uptodatecred(task))
1307 return;
1308 if (status == -EACCES) {
1309 rpc_exit(task, -EACCES);
1310 return;
1311 }
1312 task->tk_action = call_refresh;
1313 if (status != -ETIMEDOUT)
1314 rpc_delay(task, 3*HZ);
1315 return;
1316}
1317
1318/*
1319 * Call header serialization
1320 */
d8ed029d 1321static __be32 *
1da177e4
LT
1322call_header(struct rpc_task *task)
1323{
1324 struct rpc_clnt *clnt = task->tk_client;
1da177e4 1325 struct rpc_rqst *req = task->tk_rqstp;
d8ed029d 1326 __be32 *p = req->rq_svec[0].iov_base;
1da177e4
LT
1327
1328 /* FIXME: check buffer size? */
808012fb
CL
1329
1330 p = xprt_skip_transport_header(task->tk_xprt, p);
1da177e4
LT
1331 *p++ = req->rq_xid; /* XID */
1332 *p++ = htonl(RPC_CALL); /* CALL */
1333 *p++ = htonl(RPC_VERSION); /* RPC version */
1334 *p++ = htonl(clnt->cl_prog); /* program number */
1335 *p++ = htonl(clnt->cl_vers); /* program version */
1336 *p++ = htonl(task->tk_msg.rpc_proc->p_proc); /* procedure */
334ccfd5
TM
1337 p = rpcauth_marshcred(task, p);
1338 req->rq_slen = xdr_adjust_iovec(&req->rq_svec[0], p);
1339 return p;
1da177e4
LT
1340}
1341
1342/*
1343 * Reply header verification
1344 */
d8ed029d 1345static __be32 *
1da177e4
LT
1346call_verify(struct rpc_task *task)
1347{
1348 struct kvec *iov = &task->tk_rqstp->rq_rcv_buf.head[0];
1349 int len = task->tk_rqstp->rq_rcv_buf.len >> 2;
d8ed029d
AD
1350 __be32 *p = iov->iov_base;
1351 u32 n;
1da177e4
LT
1352 int error = -EACCES;
1353
e8896495
DH
1354 if ((task->tk_rqstp->rq_rcv_buf.len & 3) != 0) {
1355 /* RFC-1014 says that the representation of XDR data must be a
1356 * multiple of four bytes
1357 * - if it isn't pointer subtraction in the NFS client may give
1358 * undefined results
1359 */
8a702bbb
TM
1360 dprintk("RPC: %5u %s: XDR representation not a multiple of"
1361 " 4 bytes: 0x%x\n", task->tk_pid, __FUNCTION__,
1362 task->tk_rqstp->rq_rcv_buf.len);
e8896495
DH
1363 goto out_eio;
1364 }
1da177e4
LT
1365 if ((len -= 3) < 0)
1366 goto out_overflow;
1367 p += 1; /* skip XID */
1368
1369 if ((n = ntohl(*p++)) != RPC_REPLY) {
8a702bbb
TM
1370 dprintk("RPC: %5u %s: not an RPC reply: %x\n",
1371 task->tk_pid, __FUNCTION__, n);
abbcf28f 1372 goto out_garbage;
1da177e4
LT
1373 }
1374 if ((n = ntohl(*p++)) != RPC_MSG_ACCEPTED) {
1375 if (--len < 0)
1376 goto out_overflow;
1377 switch ((n = ntohl(*p++))) {
1378 case RPC_AUTH_ERROR:
1379 break;
1380 case RPC_MISMATCH:
46121cf7
CL
1381 dprintk("RPC: %5u %s: RPC call version "
1382 "mismatch!\n",
1383 task->tk_pid, __FUNCTION__);
cdf47706
AG
1384 error = -EPROTONOSUPPORT;
1385 goto out_err;
1da177e4 1386 default:
46121cf7
CL
1387 dprintk("RPC: %5u %s: RPC call rejected, "
1388 "unknown error: %x\n",
1389 task->tk_pid, __FUNCTION__, n);
1da177e4
LT
1390 goto out_eio;
1391 }
1392 if (--len < 0)
1393 goto out_overflow;
1394 switch ((n = ntohl(*p++))) {
1395 case RPC_AUTH_REJECTEDCRED:
1396 case RPC_AUTH_REJECTEDVERF:
1397 case RPCSEC_GSS_CREDPROBLEM:
1398 case RPCSEC_GSS_CTXPROBLEM:
1399 if (!task->tk_cred_retry)
1400 break;
1401 task->tk_cred_retry--;
46121cf7
CL
1402 dprintk("RPC: %5u %s: retry stale creds\n",
1403 task->tk_pid, __FUNCTION__);
1da177e4 1404 rpcauth_invalcred(task);
220bcc2a
TM
1405 /* Ensure we obtain a new XID! */
1406 xprt_release(task);
1da177e4 1407 task->tk_action = call_refresh;
abbcf28f 1408 goto out_retry;
1da177e4
LT
1409 case RPC_AUTH_BADCRED:
1410 case RPC_AUTH_BADVERF:
1411 /* possibly garbled cred/verf? */
1412 if (!task->tk_garb_retry)
1413 break;
1414 task->tk_garb_retry--;
46121cf7
CL
1415 dprintk("RPC: %5u %s: retry garbled creds\n",
1416 task->tk_pid, __FUNCTION__);
1da177e4 1417 task->tk_action = call_bind;
abbcf28f 1418 goto out_retry;
1da177e4 1419 case RPC_AUTH_TOOWEAK:
1356b8c2
LS
1420 printk(KERN_NOTICE "call_verify: server %s requires stronger "
1421 "authentication.\n", task->tk_client->cl_server);
1da177e4
LT
1422 break;
1423 default:
8a702bbb
TM
1424 dprintk("RPC: %5u %s: unknown auth error: %x\n",
1425 task->tk_pid, __FUNCTION__, n);
1da177e4
LT
1426 error = -EIO;
1427 }
46121cf7
CL
1428 dprintk("RPC: %5u %s: call rejected %d\n",
1429 task->tk_pid, __FUNCTION__, n);
1da177e4
LT
1430 goto out_err;
1431 }
1432 if (!(p = rpcauth_checkverf(task, p))) {
8a702bbb
TM
1433 dprintk("RPC: %5u %s: auth check failed\n",
1434 task->tk_pid, __FUNCTION__);
abbcf28f 1435 goto out_garbage; /* bad verifier, retry */
1da177e4 1436 }
d8ed029d 1437 len = p - (__be32 *)iov->iov_base - 1;
1da177e4
LT
1438 if (len < 0)
1439 goto out_overflow;
1440 switch ((n = ntohl(*p++))) {
1441 case RPC_SUCCESS:
1442 return p;
1443 case RPC_PROG_UNAVAIL:
46121cf7
CL
1444 dprintk("RPC: %5u %s: program %u is unsupported by server %s\n",
1445 task->tk_pid, __FUNCTION__,
1da177e4
LT
1446 (unsigned int)task->tk_client->cl_prog,
1447 task->tk_client->cl_server);
cdf47706
AG
1448 error = -EPFNOSUPPORT;
1449 goto out_err;
1da177e4 1450 case RPC_PROG_MISMATCH:
46121cf7
CL
1451 dprintk("RPC: %5u %s: program %u, version %u unsupported by "
1452 "server %s\n", task->tk_pid, __FUNCTION__,
1da177e4
LT
1453 (unsigned int)task->tk_client->cl_prog,
1454 (unsigned int)task->tk_client->cl_vers,
1455 task->tk_client->cl_server);
cdf47706
AG
1456 error = -EPROTONOSUPPORT;
1457 goto out_err;
1da177e4 1458 case RPC_PROC_UNAVAIL:
46121cf7
CL
1459 dprintk("RPC: %5u %s: proc %p unsupported by program %u, "
1460 "version %u on server %s\n",
1461 task->tk_pid, __FUNCTION__,
1da177e4
LT
1462 task->tk_msg.rpc_proc,
1463 task->tk_client->cl_prog,
1464 task->tk_client->cl_vers,
1465 task->tk_client->cl_server);
cdf47706
AG
1466 error = -EOPNOTSUPP;
1467 goto out_err;
1da177e4 1468 case RPC_GARBAGE_ARGS:
46121cf7
CL
1469 dprintk("RPC: %5u %s: server saw garbage\n",
1470 task->tk_pid, __FUNCTION__);
1da177e4
LT
1471 break; /* retry */
1472 default:
8a702bbb
TM
1473 dprintk("RPC: %5u %s: server accept status: %x\n",
1474 task->tk_pid, __FUNCTION__, n);
1da177e4
LT
1475 /* Also retry */
1476 }
1477
abbcf28f 1478out_garbage:
1da177e4
LT
1479 task->tk_client->cl_stats->rpcgarbage++;
1480 if (task->tk_garb_retry) {
1481 task->tk_garb_retry--;
46121cf7
CL
1482 dprintk("RPC: %5u %s: retrying\n",
1483 task->tk_pid, __FUNCTION__);
1da177e4 1484 task->tk_action = call_bind;
abbcf28f
TM
1485out_retry:
1486 return ERR_PTR(-EAGAIN);
1da177e4 1487 }
1da177e4
LT
1488out_eio:
1489 error = -EIO;
1490out_err:
1491 rpc_exit(task, error);
8a702bbb
TM
1492 dprintk("RPC: %5u %s: call failed with error %d\n", task->tk_pid,
1493 __FUNCTION__, error);
abbcf28f 1494 return ERR_PTR(error);
1da177e4 1495out_overflow:
8a702bbb
TM
1496 dprintk("RPC: %5u %s: server reply was truncated.\n", task->tk_pid,
1497 __FUNCTION__);
abbcf28f 1498 goto out_garbage;
1da177e4 1499}
5ee0ed7d 1500
d8ed029d 1501static int rpcproc_encode_null(void *rqstp, __be32 *data, void *obj)
5ee0ed7d
TM
1502{
1503 return 0;
1504}
1505
d8ed029d 1506static int rpcproc_decode_null(void *rqstp, __be32 *data, void *obj)
5ee0ed7d
TM
1507{
1508 return 0;
1509}
1510
1511static struct rpc_procinfo rpcproc_null = {
1512 .p_encode = rpcproc_encode_null,
1513 .p_decode = rpcproc_decode_null,
1514};
1515
64c91a1f 1516static int rpc_ping(struct rpc_clnt *clnt, int flags)
5ee0ed7d
TM
1517{
1518 struct rpc_message msg = {
1519 .rpc_proc = &rpcproc_null,
1520 };
1521 int err;
1522 msg.rpc_cred = authnull_ops.lookup_cred(NULL, NULL, 0);
1523 err = rpc_call_sync(clnt, &msg, flags);
1524 put_rpccred(msg.rpc_cred);
1525 return err;
1526}
188fef11 1527
5e1550d6
TM
1528struct rpc_task *rpc_call_null(struct rpc_clnt *clnt, struct rpc_cred *cred, int flags)
1529{
1530 struct rpc_message msg = {
1531 .rpc_proc = &rpcproc_null,
1532 .rpc_cred = cred,
1533 };
84115e1c
TM
1534 struct rpc_task_setup task_setup_data = {
1535 .rpc_client = clnt,
1536 .rpc_message = &msg,
1537 .callback_ops = &rpc_default_ops,
1538 .flags = flags,
1539 };
c970aa85 1540 return rpc_run_task(&task_setup_data);
5e1550d6 1541}
e8914c65 1542EXPORT_SYMBOL_GPL(rpc_call_null);
5e1550d6 1543
188fef11
TM
1544#ifdef RPC_DEBUG
1545void rpc_show_tasks(void)
1546{
1547 struct rpc_clnt *clnt;
1548 struct rpc_task *t;
1549
1550 spin_lock(&rpc_client_lock);
1551 if (list_empty(&all_clients))
1552 goto out;
1553 printk("-pid- proc flgs status -client- -prog- --rqstp- -timeout "
1554 "-rpcwait -action- ---ops--\n");
1555 list_for_each_entry(clnt, &all_clients, cl_clients) {
1556 if (list_empty(&clnt->cl_tasks))
1557 continue;
1558 spin_lock(&clnt->cl_lock);
1559 list_for_each_entry(t, &clnt->cl_tasks, tk_task) {
1560 const char *rpc_waitq = "none";
d66968f2
CL
1561 int proc;
1562
1563 if (t->tk_msg.rpc_proc)
1564 proc = t->tk_msg.rpc_proc->p_proc;
1565 else
1566 proc = -1;
188fef11
TM
1567
1568 if (RPC_IS_QUEUED(t))
1569 rpc_waitq = rpc_qname(t->u.tk_wait.rpc_waitq);
1570
1571 printk("%5u %04d %04x %6d %8p %6d %8p %8ld %8s %8p %8p\n",
d66968f2 1572 t->tk_pid, proc,
188fef11
TM
1573 t->tk_flags, t->tk_status,
1574 t->tk_client,
1575 (t->tk_client ? t->tk_client->cl_prog : 0),
1576 t->tk_rqstp, t->tk_timeout,
1577 rpc_waitq,
1578 t->tk_action, t->tk_ops);
1579 }
1580 spin_unlock(&clnt->cl_lock);
1581 }
1582out:
1583 spin_unlock(&rpc_client_lock);
1584}
1585#endif