Merge master.kernel.org:/pub/scm/linux/kernel/git/holtmann/bluetooth-2.6
[linux-2.6] / net / sunrpc / clnt.c
1 /*
2  *  linux/net/sunrpc/rpcclnt.c
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>
30 #include <linux/in.h>
31 #include <linux/utsname.h>
32
33 #include <linux/sunrpc/clnt.h>
34 #include <linux/workqueue.h>
35 #include <linux/sunrpc/rpc_pipe_fs.h>
36
37 #include <linux/nfs.h>
38
39
40 #define RPC_SLACK_SPACE         (1024)  /* total overkill */
41
42 #ifdef RPC_DEBUG
43 # define RPCDBG_FACILITY        RPCDBG_CALL
44 #endif
45
46 static DECLARE_WAIT_QUEUE_HEAD(destroy_wait);
47
48
49 static void     call_start(struct rpc_task *task);
50 static void     call_reserve(struct rpc_task *task);
51 static void     call_reserveresult(struct rpc_task *task);
52 static void     call_allocate(struct rpc_task *task);
53 static void     call_encode(struct rpc_task *task);
54 static void     call_decode(struct rpc_task *task);
55 static void     call_bind(struct rpc_task *task);
56 static void     call_transmit(struct rpc_task *task);
57 static void     call_status(struct rpc_task *task);
58 static void     call_refresh(struct rpc_task *task);
59 static void     call_refreshresult(struct rpc_task *task);
60 static void     call_timeout(struct rpc_task *task);
61 static void     call_connect(struct rpc_task *task);
62 static void     call_connect_status(struct rpc_task *task);
63 static u32 *    call_header(struct rpc_task *task);
64 static u32 *    call_verify(struct rpc_task *task);
65
66
67 static int
68 rpc_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  */
99 struct rpc_clnt *
100 rpc_new_client(struct rpc_xprt *xprt, char *servname,
101                   struct rpc_program *program, u32 vers,
102                   rpc_authflavor_t flavor)
103 {
104         struct rpc_version      *version;
105         struct rpc_clnt         *clnt = NULL;
106         struct rpc_auth         *auth;
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
161         auth = rpcauth_create(flavor, clnt);
162         if (IS_ERR(auth)) {
163                 printk(KERN_INFO "RPC: Couldn't create auth handle (flavor %u)\n",
164                                 flavor);
165                 err = PTR_ERR(auth);
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
176 out_no_auth:
177         rpc_rmdir(clnt->cl_pathname);
178 out_no_path:
179         if (clnt->cl_server != clnt->cl_inline_name)
180                 kfree(clnt->cl_server);
181         kfree(clnt);
182 out_err:
183         xprt_destroy(xprt);
184         return ERR_PTR(err);
185 }
186
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  */
201 struct 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
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  */
222 struct rpc_clnt *
223 rpc_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);
244         new->cl_pmap            = &new->cl_pmap_default;
245         rpc_init_wait_queue(&new->cl_pmap_default.pm_bindwait, "bindwait");
246         return new;
247 out_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  */
258 int
259 rpc_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  */
288 int
289 rpc_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);
313 out_free:
314         kfree(clnt);
315         return 0;
316 }
317
318 /*
319  * Release an RPC client
320  */
321 void
322 rpc_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
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  */
344 struct 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         }
368 out:    
369         return clnt;
370 }
371
372 /*
373  * Default callback for async RPC calls
374  */
375 static void
376 rpc_default_callback(struct rpc_task *task)
377 {
378 }
379
380 /*
381  *      Export the signal mask handling for synchronous code that
382  *      sleeps on RPC calls
383  */
384 #define RPC_INTR_SIGNALS (sigmask(SIGINT) | sigmask(SIGQUIT) | sigmask(SIGKILL))
385  
386 static 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
398 static inline void rpc_task_sigmask(struct rpc_task *task, sigset_t *oldset)
399 {
400         rpc_save_sigmask(oldset, !RPC_TASK_UNINTERRUPTIBLE(task));
401 }
402
403 static inline void rpc_restore_sigmask(sigset_t *oldset)
404 {
405         sigprocmask(SIG_SETMASK, oldset, NULL);
406 }
407
408 void rpc_clnt_sigmask(struct rpc_clnt *clnt, sigset_t *oldset)
409 {
410         rpc_save_sigmask(oldset, clnt->cl_intr);
411 }
412
413 void rpc_clnt_sigunmask(struct rpc_clnt *clnt, sigset_t *oldset)
414 {
415         rpc_restore_sigmask(oldset);
416 }
417
418 /*
419  * New rpc_call implementation
420  */
421 int 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
433         status = -ENOMEM;
434         task = rpc_new_task(clnt, NULL, flags);
435         if (task == NULL)
436                 goto out;
437
438         /* Mask signals on RPC calls _and_ GSS_AUTH upcalls */
439         rpc_task_sigmask(task, &oldset);
440
441         rpc_call_setup(task, msg, 0);
442
443         /* Set up the call info struct and execute the task */
444         if (task->tk_status == 0) {
445                 status = rpc_execute(task);
446         } else {
447                 status = task->tk_status;
448                 rpc_release_task(task);
449         }
450
451         rpc_restore_sigmask(&oldset);
452 out:
453         return status;
454 }
455
456 /*
457  * New rpc_call implementation
458  */
459 int
460 rpc_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
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
481         /* Mask signals on GSS_AUTH upcalls */
482         rpc_task_sigmask(task, &oldset);                
483
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
493         rpc_restore_sigmask(&oldset);           
494 out:
495         return status;
496 }
497
498
499 void
500 rpc_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
516 void
517 rpc_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;
527         if (xprt_connected(xprt))
528                 xprt_sock_setbufsize(xprt);
529 }
530
531 /*
532  * Return size of largest payload RPC client can support, in bytes
533  *
534  * For stream transports, this is one RPC record fragment (see RFC
535  * 1831), as we don't support multi-record requests yet.  For datagram
536  * transports, this is the size of an IP packet minus the IP, UDP, and
537  * RPC header sizes.
538  */
539 size_t rpc_max_payload(struct rpc_clnt *clnt)
540 {
541         return clnt->cl_xprt->max_payload;
542 }
543 EXPORT_SYMBOL(rpc_max_payload);
544
545 /*
546  * Restart an (async) RPC call. Usually called from within the
547  * exit handler.
548  */
549 void
550 rpc_restart_call(struct rpc_task *task)
551 {
552         if (RPC_ASSASSINATED(task))
553                 return;
554
555         task->tk_action = call_start;
556 }
557
558 /*
559  * 0.  Initial state
560  *
561  *     Other FSM states can be visited zero or more times, but
562  *     this state is visited exactly once for each RPC.
563  */
564 static void
565 call_start(struct rpc_task *task)
566 {
567         struct rpc_clnt *clnt = task->tk_client;
568
569         dprintk("RPC: %4d call_start %s%d proc %d (%s)\n", task->tk_pid,
570                 clnt->cl_protname, clnt->cl_vers, task->tk_msg.rpc_proc->p_proc,
571                 (RPC_IS_ASYNC(task) ? "async" : "sync"));
572
573         /* Increment call count */
574         task->tk_msg.rpc_proc->p_count++;
575         clnt->cl_stats->rpccnt++;
576         task->tk_action = call_reserve;
577 }
578
579 /*
580  * 1.   Reserve an RPC call slot
581  */
582 static void
583 call_reserve(struct rpc_task *task)
584 {
585         dprintk("RPC: %4d call_reserve\n", task->tk_pid);
586
587         if (!rpcauth_uptodatecred(task)) {
588                 task->tk_action = call_refresh;
589                 return;
590         }
591
592         task->tk_status  = 0;
593         task->tk_action  = call_reserveresult;
594         xprt_reserve(task);
595 }
596
597 /*
598  * 1b.  Grok the result of xprt_reserve()
599  */
600 static void
601 call_reserveresult(struct rpc_task *task)
602 {
603         int status = task->tk_status;
604
605         dprintk("RPC: %4d call_reserveresult (status %d)\n",
606                                 task->tk_pid, task->tk_status);
607
608         /*
609          * After a call to xprt_reserve(), we must have either
610          * a request slot or else an error status.
611          */
612         task->tk_status = 0;
613         if (status >= 0) {
614                 if (task->tk_rqstp) {
615                         task->tk_action = call_allocate;
616                         return;
617                 }
618
619                 printk(KERN_ERR "%s: status=%d, but no request slot, exiting\n",
620                                 __FUNCTION__, status);
621                 rpc_exit(task, -EIO);
622                 return;
623         }
624
625         /*
626          * Even though there was an error, we may have acquired
627          * a request slot somehow.  Make sure not to leak it.
628          */
629         if (task->tk_rqstp) {
630                 printk(KERN_ERR "%s: status=%d, request allocated anyway\n",
631                                 __FUNCTION__, status);
632                 xprt_release(task);
633         }
634
635         switch (status) {
636         case -EAGAIN:   /* woken up; retry */
637                 task->tk_action = call_reserve;
638                 return;
639         case -EIO:      /* probably a shutdown */
640                 break;
641         default:
642                 printk(KERN_ERR "%s: unrecognized error %d, exiting\n",
643                                 __FUNCTION__, status);
644                 break;
645         }
646         rpc_exit(task, status);
647 }
648
649 /*
650  * 2.   Allocate the buffer. For details, see sched.c:rpc_malloc.
651  *      (Note: buffer memory is freed in rpc_task_release).
652  */
653 static void
654 call_allocate(struct rpc_task *task)
655 {
656         unsigned int    bufsiz;
657
658         dprintk("RPC: %4d call_allocate (status %d)\n", 
659                                 task->tk_pid, task->tk_status);
660         task->tk_action = call_bind;
661         if (task->tk_buffer)
662                 return;
663
664         /* FIXME: compute buffer requirements more exactly using
665          * auth->au_wslack */
666         bufsiz = task->tk_msg.rpc_proc->p_bufsiz + RPC_SLACK_SPACE;
667
668         if (rpc_malloc(task, bufsiz << 1) != NULL)
669                 return;
670         printk(KERN_INFO "RPC: buffer allocation failed for task %p\n", task); 
671
672         if (RPC_IS_ASYNC(task) || !signalled()) {
673                 xprt_release(task);
674                 task->tk_action = call_reserve;
675                 rpc_delay(task, HZ>>4);
676                 return;
677         }
678
679         rpc_exit(task, -ERESTARTSYS);
680 }
681
682 /*
683  * 3.   Encode arguments of an RPC call
684  */
685 static void
686 call_encode(struct rpc_task *task)
687 {
688         struct rpc_clnt *clnt = task->tk_client;
689         struct rpc_rqst *req = task->tk_rqstp;
690         struct xdr_buf *sndbuf = &req->rq_snd_buf;
691         struct xdr_buf *rcvbuf = &req->rq_rcv_buf;
692         unsigned int    bufsiz;
693         kxdrproc_t      encode;
694         int             status;
695         u32             *p;
696
697         dprintk("RPC: %4d call_encode (status %d)\n", 
698                                 task->tk_pid, task->tk_status);
699
700         /* Default buffer setup */
701         bufsiz = task->tk_bufsize >> 1;
702         sndbuf->head[0].iov_base = (void *)task->tk_buffer;
703         sndbuf->head[0].iov_len  = bufsiz;
704         sndbuf->tail[0].iov_len  = 0;
705         sndbuf->page_len         = 0;
706         sndbuf->len              = 0;
707         sndbuf->buflen           = bufsiz;
708         rcvbuf->head[0].iov_base = (void *)((char *)task->tk_buffer + bufsiz);
709         rcvbuf->head[0].iov_len  = bufsiz;
710         rcvbuf->tail[0].iov_len  = 0;
711         rcvbuf->page_len         = 0;
712         rcvbuf->len              = 0;
713         rcvbuf->buflen           = bufsiz;
714
715         /* Encode header and provided arguments */
716         encode = task->tk_msg.rpc_proc->p_encode;
717         if (!(p = call_header(task))) {
718                 printk(KERN_INFO "RPC: call_header failed, exit EIO\n");
719                 rpc_exit(task, -EIO);
720                 return;
721         }
722         if (encode && (status = rpcauth_wrap_req(task, encode, req, p,
723                                                  task->tk_msg.rpc_argp)) < 0) {
724                 printk(KERN_WARNING "%s: can't encode arguments: %d\n",
725                                 clnt->cl_protname, -status);
726                 rpc_exit(task, status);
727         }
728 }
729
730 /*
731  * 4.   Get the server port number if not yet set
732  */
733 static void
734 call_bind(struct rpc_task *task)
735 {
736         struct rpc_clnt *clnt = task->tk_client;
737         struct rpc_xprt *xprt = clnt->cl_xprt;
738
739         dprintk("RPC: %4d call_bind xprt %p %s connected\n", task->tk_pid,
740                         xprt, (xprt_connected(xprt) ? "is" : "is not"));
741
742         task->tk_action = (xprt_connected(xprt)) ? call_transmit : call_connect;
743
744         if (!clnt->cl_port) {
745                 task->tk_action = call_connect;
746                 task->tk_timeout = RPC_CONNECT_TIMEOUT;
747                 rpc_getport(task, clnt);
748         }
749 }
750
751 /*
752  * 4a.  Connect to the RPC server (TCP case)
753  */
754 static void
755 call_connect(struct rpc_task *task)
756 {
757         struct rpc_clnt *clnt = task->tk_client;
758
759         dprintk("RPC: %4d call_connect status %d\n",
760                                 task->tk_pid, task->tk_status);
761
762         if (xprt_connected(clnt->cl_xprt)) {
763                 task->tk_action = call_transmit;
764                 return;
765         }
766         task->tk_action = call_connect_status;
767         if (task->tk_status < 0)
768                 return;
769         xprt_connect(task);
770 }
771
772 /*
773  * 4b. Sort out connect result
774  */
775 static void
776 call_connect_status(struct rpc_task *task)
777 {
778         struct rpc_clnt *clnt = task->tk_client;
779         int status = task->tk_status;
780
781         task->tk_status = 0;
782         if (status >= 0) {
783                 clnt->cl_stats->netreconn++;
784                 task->tk_action = call_transmit;
785                 return;
786         }
787
788         /* Something failed: we may have to rebind */
789         if (clnt->cl_autobind)
790                 clnt->cl_port = 0;
791         switch (status) {
792         case -ENOTCONN:
793         case -ETIMEDOUT:
794         case -EAGAIN:
795                 task->tk_action = (clnt->cl_port == 0) ? call_bind : call_connect;
796                 break;
797         default:
798                 rpc_exit(task, -EIO);
799         }
800 }
801
802 /*
803  * 5.   Transmit the RPC request, and wait for reply
804  */
805 static void
806 call_transmit(struct rpc_task *task)
807 {
808         dprintk("RPC: %4d call_transmit (status %d)\n", 
809                                 task->tk_pid, task->tk_status);
810
811         task->tk_action = call_status;
812         if (task->tk_status < 0)
813                 return;
814         task->tk_status = xprt_prepare_transmit(task);
815         if (task->tk_status != 0)
816                 return;
817         /* Encode here so that rpcsec_gss can use correct sequence number. */
818         if (!task->tk_rqstp->rq_bytes_sent)
819                 call_encode(task);
820         if (task->tk_status < 0)
821                 return;
822         xprt_transmit(task);
823         if (task->tk_status < 0)
824                 return;
825         if (!task->tk_msg.rpc_proc->p_decode) {
826                 task->tk_action = NULL;
827                 rpc_wake_up_task(task);
828         }
829 }
830
831 /*
832  * 6.   Sort out the RPC call status
833  */
834 static void
835 call_status(struct rpc_task *task)
836 {
837         struct rpc_clnt *clnt = task->tk_client;
838         struct rpc_rqst *req = task->tk_rqstp;
839         int             status;
840
841         if (req->rq_received > 0 && !req->rq_bytes_sent)
842                 task->tk_status = req->rq_received;
843
844         dprintk("RPC: %4d call_status (status %d)\n", 
845                                 task->tk_pid, task->tk_status);
846
847         status = task->tk_status;
848         if (status >= 0) {
849                 task->tk_action = call_decode;
850                 return;
851         }
852
853         task->tk_status = 0;
854         switch(status) {
855         case -ETIMEDOUT:
856                 task->tk_action = call_timeout;
857                 break;
858         case -ECONNREFUSED:
859         case -ENOTCONN:
860                 req->rq_bytes_sent = 0;
861                 if (clnt->cl_autobind)
862                         clnt->cl_port = 0;
863                 task->tk_action = call_bind;
864                 break;
865         case -EAGAIN:
866                 task->tk_action = call_transmit;
867                 break;
868         case -EIO:
869                 /* shutdown or soft timeout */
870                 rpc_exit(task, status);
871                 break;
872         default:
873                 if (clnt->cl_chatty)
874                         printk("%s: RPC call returned error %d\n",
875                                clnt->cl_protname, -status);
876                 rpc_exit(task, status);
877                 break;
878         }
879 }
880
881 /*
882  * 6a.  Handle RPC timeout
883  *      We do not release the request slot, so we keep using the
884  *      same XID for all retransmits.
885  */
886 static void
887 call_timeout(struct rpc_task *task)
888 {
889         struct rpc_clnt *clnt = task->tk_client;
890
891         if (xprt_adjust_timeout(task->tk_rqstp) == 0) {
892                 dprintk("RPC: %4d call_timeout (minor)\n", task->tk_pid);
893                 goto retry;
894         }
895
896         dprintk("RPC: %4d call_timeout (major)\n", task->tk_pid);
897         if (RPC_IS_SOFT(task)) {
898                 if (clnt->cl_chatty)
899                         printk(KERN_NOTICE "%s: server %s not responding, timed out\n",
900                                 clnt->cl_protname, clnt->cl_server);
901                 rpc_exit(task, -EIO);
902                 return;
903         }
904
905         if (clnt->cl_chatty && !(task->tk_flags & RPC_CALL_MAJORSEEN)) {
906                 task->tk_flags |= RPC_CALL_MAJORSEEN;
907                 printk(KERN_NOTICE "%s: server %s not responding, still trying\n",
908                         clnt->cl_protname, clnt->cl_server);
909         }
910         if (clnt->cl_autobind)
911                 clnt->cl_port = 0;
912
913 retry:
914         clnt->cl_stats->rpcretrans++;
915         task->tk_action = call_bind;
916         task->tk_status = 0;
917 }
918
919 /*
920  * 7.   Decode the RPC reply
921  */
922 static void
923 call_decode(struct rpc_task *task)
924 {
925         struct rpc_clnt *clnt = task->tk_client;
926         struct rpc_rqst *req = task->tk_rqstp;
927         kxdrproc_t      decode = task->tk_msg.rpc_proc->p_decode;
928         u32             *p;
929
930         dprintk("RPC: %4d call_decode (status %d)\n", 
931                                 task->tk_pid, task->tk_status);
932
933         if (clnt->cl_chatty && (task->tk_flags & RPC_CALL_MAJORSEEN)) {
934                 printk(KERN_NOTICE "%s: server %s OK\n",
935                         clnt->cl_protname, clnt->cl_server);
936                 task->tk_flags &= ~RPC_CALL_MAJORSEEN;
937         }
938
939         if (task->tk_status < 12) {
940                 if (!RPC_IS_SOFT(task)) {
941                         task->tk_action = call_bind;
942                         clnt->cl_stats->rpcretrans++;
943                         goto out_retry;
944                 }
945                 printk(KERN_WARNING "%s: too small RPC reply size (%d bytes)\n",
946                         clnt->cl_protname, task->tk_status);
947                 rpc_exit(task, -EIO);
948                 return;
949         }
950
951         req->rq_rcv_buf.len = req->rq_private_buf.len;
952
953         /* Check that the softirq receive buffer is valid */
954         WARN_ON(memcmp(&req->rq_rcv_buf, &req->rq_private_buf,
955                                 sizeof(req->rq_rcv_buf)) != 0);
956
957         /* Verify the RPC header */
958         if (!(p = call_verify(task))) {
959                 if (task->tk_action == NULL)
960                         return;
961                 goto out_retry;
962         }
963
964         task->tk_action = NULL;
965
966         if (decode)
967                 task->tk_status = rpcauth_unwrap_resp(task, decode, req, p,
968                                                       task->tk_msg.rpc_resp);
969         dprintk("RPC: %4d call_decode result %d\n", task->tk_pid,
970                                         task->tk_status);
971         return;
972 out_retry:
973         req->rq_received = req->rq_private_buf.len = 0;
974         task->tk_status = 0;
975 }
976
977 /*
978  * 8.   Refresh the credentials if rejected by the server
979  */
980 static void
981 call_refresh(struct rpc_task *task)
982 {
983         dprintk("RPC: %4d call_refresh\n", task->tk_pid);
984
985         xprt_release(task);     /* Must do to obtain new XID */
986         task->tk_action = call_refreshresult;
987         task->tk_status = 0;
988         task->tk_client->cl_stats->rpcauthrefresh++;
989         rpcauth_refreshcred(task);
990 }
991
992 /*
993  * 8a.  Process the results of a credential refresh
994  */
995 static void
996 call_refreshresult(struct rpc_task *task)
997 {
998         int status = task->tk_status;
999         dprintk("RPC: %4d call_refreshresult (status %d)\n", 
1000                                 task->tk_pid, task->tk_status);
1001
1002         task->tk_status = 0;
1003         task->tk_action = call_reserve;
1004         if (status >= 0 && rpcauth_uptodatecred(task))
1005                 return;
1006         if (status == -EACCES) {
1007                 rpc_exit(task, -EACCES);
1008                 return;
1009         }
1010         task->tk_action = call_refresh;
1011         if (status != -ETIMEDOUT)
1012                 rpc_delay(task, 3*HZ);
1013         return;
1014 }
1015
1016 /*
1017  * Call header serialization
1018  */
1019 static u32 *
1020 call_header(struct rpc_task *task)
1021 {
1022         struct rpc_clnt *clnt = task->tk_client;
1023         struct rpc_xprt *xprt = clnt->cl_xprt;
1024         struct rpc_rqst *req = task->tk_rqstp;
1025         u32             *p = req->rq_svec[0].iov_base;
1026
1027         /* FIXME: check buffer size? */
1028         if (xprt->stream)
1029                 *p++ = 0;               /* fill in later */
1030         *p++ = req->rq_xid;             /* XID */
1031         *p++ = htonl(RPC_CALL);         /* CALL */
1032         *p++ = htonl(RPC_VERSION);      /* RPC version */
1033         *p++ = htonl(clnt->cl_prog);    /* program number */
1034         *p++ = htonl(clnt->cl_vers);    /* program version */
1035         *p++ = htonl(task->tk_msg.rpc_proc->p_proc);    /* procedure */
1036         p = rpcauth_marshcred(task, p);
1037         req->rq_slen = xdr_adjust_iovec(&req->rq_svec[0], p);
1038         return p;
1039 }
1040
1041 /*
1042  * Reply header verification
1043  */
1044 static u32 *
1045 call_verify(struct rpc_task *task)
1046 {
1047         struct kvec *iov = &task->tk_rqstp->rq_rcv_buf.head[0];
1048         int len = task->tk_rqstp->rq_rcv_buf.len >> 2;
1049         u32     *p = iov->iov_base, n;
1050         int error = -EACCES;
1051
1052         if ((len -= 3) < 0)
1053                 goto out_overflow;
1054         p += 1; /* skip XID */
1055
1056         if ((n = ntohl(*p++)) != RPC_REPLY) {
1057                 printk(KERN_WARNING "call_verify: not an RPC reply: %x\n", n);
1058                 goto out_retry;
1059         }
1060         if ((n = ntohl(*p++)) != RPC_MSG_ACCEPTED) {
1061                 if (--len < 0)
1062                         goto out_overflow;
1063                 switch ((n = ntohl(*p++))) {
1064                         case RPC_AUTH_ERROR:
1065                                 break;
1066                         case RPC_MISMATCH:
1067                                 dprintk("%s: RPC call version mismatch!\n", __FUNCTION__);
1068                                 error = -EPROTONOSUPPORT;
1069                                 goto out_err;
1070                         default:
1071                                 dprintk("%s: RPC call rejected, unknown error: %x\n", __FUNCTION__, n);
1072                                 goto out_eio;
1073                 }
1074                 if (--len < 0)
1075                         goto out_overflow;
1076                 switch ((n = ntohl(*p++))) {
1077                 case RPC_AUTH_REJECTEDCRED:
1078                 case RPC_AUTH_REJECTEDVERF:
1079                 case RPCSEC_GSS_CREDPROBLEM:
1080                 case RPCSEC_GSS_CTXPROBLEM:
1081                         if (!task->tk_cred_retry)
1082                                 break;
1083                         task->tk_cred_retry--;
1084                         dprintk("RPC: %4d call_verify: retry stale creds\n",
1085                                                         task->tk_pid);
1086                         rpcauth_invalcred(task);
1087                         task->tk_action = call_refresh;
1088                         return NULL;
1089                 case RPC_AUTH_BADCRED:
1090                 case RPC_AUTH_BADVERF:
1091                         /* possibly garbled cred/verf? */
1092                         if (!task->tk_garb_retry)
1093                                 break;
1094                         task->tk_garb_retry--;
1095                         dprintk("RPC: %4d call_verify: retry garbled creds\n",
1096                                                         task->tk_pid);
1097                         task->tk_action = call_bind;
1098                         return NULL;
1099                 case RPC_AUTH_TOOWEAK:
1100                         printk(KERN_NOTICE "call_verify: server requires stronger "
1101                                "authentication.\n");
1102                         break;
1103                 default:
1104                         printk(KERN_WARNING "call_verify: unknown auth error: %x\n", n);
1105                         error = -EIO;
1106                 }
1107                 dprintk("RPC: %4d call_verify: call rejected %d\n",
1108                                                 task->tk_pid, n);
1109                 goto out_err;
1110         }
1111         if (!(p = rpcauth_checkverf(task, p))) {
1112                 printk(KERN_WARNING "call_verify: auth check failed\n");
1113                 goto out_retry;         /* bad verifier, retry */
1114         }
1115         len = p - (u32 *)iov->iov_base - 1;
1116         if (len < 0)
1117                 goto out_overflow;
1118         switch ((n = ntohl(*p++))) {
1119         case RPC_SUCCESS:
1120                 return p;
1121         case RPC_PROG_UNAVAIL:
1122                 dprintk("RPC: call_verify: program %u is unsupported by server %s\n",
1123                                 (unsigned int)task->tk_client->cl_prog,
1124                                 task->tk_client->cl_server);
1125                 error = -EPFNOSUPPORT;
1126                 goto out_err;
1127         case RPC_PROG_MISMATCH:
1128                 dprintk("RPC: call_verify: program %u, version %u unsupported by server %s\n",
1129                                 (unsigned int)task->tk_client->cl_prog,
1130                                 (unsigned int)task->tk_client->cl_vers,
1131                                 task->tk_client->cl_server);
1132                 error = -EPROTONOSUPPORT;
1133                 goto out_err;
1134         case RPC_PROC_UNAVAIL:
1135                 dprintk("RPC: call_verify: proc %p unsupported by program %u, version %u on server %s\n",
1136                                 task->tk_msg.rpc_proc,
1137                                 task->tk_client->cl_prog,
1138                                 task->tk_client->cl_vers,
1139                                 task->tk_client->cl_server);
1140                 error = -EOPNOTSUPP;
1141                 goto out_err;
1142         case RPC_GARBAGE_ARGS:
1143                 dprintk("RPC: %4d %s: server saw garbage\n", task->tk_pid, __FUNCTION__);
1144                 break;                  /* retry */
1145         default:
1146                 printk(KERN_WARNING "call_verify: server accept status: %x\n", n);
1147                 /* Also retry */
1148         }
1149
1150 out_retry:
1151         task->tk_client->cl_stats->rpcgarbage++;
1152         if (task->tk_garb_retry) {
1153                 task->tk_garb_retry--;
1154                 dprintk("RPC %s: retrying %4d\n", __FUNCTION__, task->tk_pid);
1155                 task->tk_action = call_bind;
1156                 return NULL;
1157         }
1158         printk(KERN_WARNING "RPC %s: retry failed, exit EIO\n", __FUNCTION__);
1159 out_eio:
1160         error = -EIO;
1161 out_err:
1162         rpc_exit(task, error);
1163         return NULL;
1164 out_overflow:
1165         printk(KERN_WARNING "RPC %s: server reply was truncated.\n", __FUNCTION__);
1166         goto out_retry;
1167 }
1168
1169 static int rpcproc_encode_null(void *rqstp, u32 *data, void *obj)
1170 {
1171         return 0;
1172 }
1173
1174 static int rpcproc_decode_null(void *rqstp, u32 *data, void *obj)
1175 {
1176         return 0;
1177 }
1178
1179 static struct rpc_procinfo rpcproc_null = {
1180         .p_encode = rpcproc_encode_null,
1181         .p_decode = rpcproc_decode_null,
1182 };
1183
1184 int rpc_ping(struct rpc_clnt *clnt, int flags)
1185 {
1186         struct rpc_message msg = {
1187                 .rpc_proc = &rpcproc_null,
1188         };
1189         int err;
1190         msg.rpc_cred = authnull_ops.lookup_cred(NULL, NULL, 0);
1191         err = rpc_call_sync(clnt, &msg, flags);
1192         put_rpccred(msg.rpc_cred);
1193         return err;
1194 }