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