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