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