cleanup
[linux-2.4.21-pre4.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 reconnect handling (when finished).
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/types.h>
27 #include <linux/mm.h>
28 #include <linux/slab.h>
29 #include <linux/in.h>
30 #include <linux/utsname.h>
31
32 #include <linux/sunrpc/clnt.h>
33
34 #include <linux/nfs.h>
35
36
37 #define RPC_SLACK_SPACE         512     /* total overkill */
38
39 #ifdef RPC_DEBUG
40 # define RPCDBG_FACILITY        RPCDBG_CALL
41 #endif
42
43 static DECLARE_WAIT_QUEUE_HEAD(destroy_wait);
44
45
46 static void     call_start(struct rpc_task *task);
47 static void     call_reserve(struct rpc_task *task);
48 static void     call_reserveresult(struct rpc_task *task);
49 static void     call_allocate(struct rpc_task *task);
50 static void     call_encode(struct rpc_task *task);
51 static void     call_decode(struct rpc_task *task);
52 static void     call_bind(struct rpc_task *task);
53 static void     call_transmit(struct rpc_task *task);
54 static void     call_status(struct rpc_task *task);
55 static void     call_refresh(struct rpc_task *task);
56 static void     call_refreshresult(struct rpc_task *task);
57 static void     call_timeout(struct rpc_task *task);
58 static void     call_reconnect(struct rpc_task *task);
59 static void     child_reconnect(struct rpc_task *);
60 static void     child_reconnect_status(struct rpc_task *);
61 static u32 *    call_header(struct rpc_task *task);
62 static u32 *    call_verify(struct rpc_task *task);
63
64
65 /*
66  * Create an RPC client
67  * FIXME: This should also take a flags argument (as in task->tk_flags).
68  * It's called (among others) from pmap_create_client, which may in
69  * turn be called by an async task. In this case, rpciod should not be
70  * made to sleep too long.
71  */
72 struct rpc_clnt *
73 rpc_create_client(struct rpc_xprt *xprt, char *servname,
74                   struct rpc_program *program, u32 vers, int flavor)
75 {
76         struct rpc_version      *version;
77         struct rpc_clnt         *clnt = NULL;
78
79         dprintk("RPC: creating %s client for %s (xprt %p)\n",
80                 program->name, servname, xprt);
81
82         if (!xprt)
83                 goto out;
84         if (vers >= program->nrvers || !(version = program->version[vers]))
85                 goto out;
86
87         clnt = (struct rpc_clnt *) rpc_allocate(0, sizeof(*clnt));
88         if (!clnt)
89                 goto out_no_clnt;
90         memset(clnt, 0, sizeof(*clnt));
91         atomic_set(&clnt->cl_users, 0);
92
93         clnt->cl_xprt     = xprt;
94         clnt->cl_procinfo = version->procs;
95         clnt->cl_maxproc  = version->nrprocs;
96         clnt->cl_server   = servname;
97         clnt->cl_protname = program->name;
98         clnt->cl_port     = xprt->addr.sin_port;
99         clnt->cl_prog     = program->number;
100         clnt->cl_vers     = version->number;
101         clnt->cl_prot     = xprt->prot;
102         clnt->cl_stats    = program->stats;
103         INIT_RPC_WAITQ(&clnt->cl_bindwait, "bindwait");
104
105         if (!clnt->cl_port)
106                 clnt->cl_autobind = 1;
107
108         rpc_init_rtt(&clnt->cl_rtt, xprt->timeout.to_initval);
109
110         if (!rpcauth_create(flavor, clnt))
111                 goto out_no_auth;
112
113         /* save the nodename */
114         clnt->cl_nodelen = strlen(system_utsname.nodename);
115         if (clnt->cl_nodelen > UNX_MAXNODENAME)
116                 clnt->cl_nodelen = UNX_MAXNODENAME;
117         memcpy(clnt->cl_nodename, system_utsname.nodename, clnt->cl_nodelen);
118 out:
119         return clnt;
120
121 out_no_clnt:
122         printk(KERN_INFO "RPC: out of memory in rpc_create_client\n");
123         goto out;
124 out_no_auth:
125         printk(KERN_INFO "RPC: Couldn't create auth handle (flavor %d)\n",
126                 flavor);
127         rpc_free(clnt);
128         clnt = NULL;
129         goto out;
130 }
131
132 /*
133  * Properly shut down an RPC client, terminating all outstanding
134  * requests. Note that we must be certain that cl_oneshot and
135  * cl_dead are cleared, or else the client would be destroyed
136  * when the last task releases it.
137  */
138 int
139 rpc_shutdown_client(struct rpc_clnt *clnt)
140 {
141         dprintk("RPC: shutting down %s client for %s\n",
142                 clnt->cl_protname, clnt->cl_server);
143         while (atomic_read(&clnt->cl_users)) {
144 #ifdef RPC_DEBUG
145                 dprintk("RPC: rpc_shutdown_client: client %s, tasks=%d\n",
146                         clnt->cl_protname, atomic_read(&clnt->cl_users));
147 #endif
148                 /* Don't let rpc_release_client destroy us */
149                 clnt->cl_oneshot = 0;
150                 clnt->cl_dead = 0;
151                 rpc_killall_tasks(clnt);
152                 sleep_on_timeout(&destroy_wait, 1*HZ);
153         }
154         return rpc_destroy_client(clnt);
155 }
156
157 /*
158  * Delete an RPC client
159  */
160 int
161 rpc_destroy_client(struct rpc_clnt *clnt)
162 {
163         dprintk("RPC: destroying %s client for %s\n",
164                         clnt->cl_protname, clnt->cl_server);
165
166         if (clnt->cl_auth) {
167                 rpcauth_destroy(clnt->cl_auth);
168                 clnt->cl_auth = NULL;
169         }
170         if (clnt->cl_xprt) {
171                 xprt_destroy(clnt->cl_xprt);
172                 clnt->cl_xprt = NULL;
173         }
174         rpc_free(clnt);
175         return 0;
176 }
177
178 /*
179  * Release an RPC client
180  */
181 void
182 rpc_release_client(struct rpc_clnt *clnt)
183 {
184         dprintk("RPC:      rpc_release_client(%p, %d)\n",
185                                 clnt, atomic_read(&clnt->cl_users));
186
187         if (!atomic_dec_and_test(&clnt->cl_users))
188                 return;
189         wake_up(&destroy_wait);
190         if (clnt->cl_oneshot || clnt->cl_dead)
191                 rpc_destroy_client(clnt);
192 }
193
194 /*
195  * Default callback for async RPC calls
196  */
197 static void
198 rpc_default_callback(struct rpc_task *task)
199 {
200 }
201
202 /*
203  *      Export the signal mask handling for aysnchronous code that
204  *      sleeps on RPC calls
205  */
206  
207 void rpc_clnt_sigmask(struct rpc_clnt *clnt, sigset_t *oldset)
208 {
209         unsigned long   sigallow = sigmask(SIGKILL);
210         unsigned long   irqflags;
211         
212         /* Turn off various signals */
213         if (clnt->cl_intr) {
214                 struct k_sigaction *action = current->sig->action;
215                 if (action[SIGINT-1].sa.sa_handler == SIG_DFL)
216                         sigallow |= sigmask(SIGINT);
217                 if (action[SIGQUIT-1].sa.sa_handler == SIG_DFL)
218                         sigallow |= sigmask(SIGQUIT);
219         }
220         spin_lock_irqsave(&current->sigmask_lock, irqflags);
221         *oldset = current->blocked;
222         siginitsetinv(&current->blocked, sigallow & ~oldset->sig[0]);
223         recalc_sigpending(current);
224         spin_unlock_irqrestore(&current->sigmask_lock, irqflags);
225 }
226
227 void rpc_clnt_sigunmask(struct rpc_clnt *clnt, sigset_t *oldset)
228 {
229         unsigned long   irqflags;
230         
231         spin_lock_irqsave(&current->sigmask_lock, irqflags);
232         current->blocked = *oldset;
233         recalc_sigpending(current);
234         spin_unlock_irqrestore(&current->sigmask_lock, irqflags);
235 }
236
237 /*
238  * New rpc_call implementation
239  */
240 int rpc_call_sync(struct rpc_clnt *clnt, struct rpc_message *msg, int flags)
241 {
242         struct rpc_task my_task, *task = &my_task;
243         sigset_t        oldset;
244         int             status;
245
246         /* If this client is slain all further I/O fails */
247         if (clnt->cl_dead) 
248                 return -EIO;
249
250         if (flags & RPC_TASK_ASYNC) {
251                 printk("rpc_call_sync: Illegal flag combination for synchronous task\n");
252                 flags &= ~RPC_TASK_ASYNC;
253         }
254
255         rpc_clnt_sigmask(clnt, &oldset);                
256
257         /* Create/initialize a new RPC task */
258         rpc_init_task(task, clnt, NULL, flags);
259         rpc_call_setup(task, msg, 0);
260
261         /* Set up the call info struct and execute the task */
262         if (task->tk_status == 0)
263                 status = rpc_execute(task);
264         else {
265                 status = task->tk_status;
266                 rpc_release_task(task);
267         }
268
269         rpc_clnt_sigunmask(clnt, &oldset);              
270
271         return status;
272 }
273
274 /*
275  * New rpc_call implementation
276  */
277 int
278 rpc_call_async(struct rpc_clnt *clnt, struct rpc_message *msg, int flags,
279                rpc_action callback, void *data)
280 {
281         struct rpc_task *task;
282         sigset_t        oldset;
283         int             status;
284
285         /* If this client is slain all further I/O fails */
286         if (clnt->cl_dead) 
287                 return -EIO;
288
289         flags |= RPC_TASK_ASYNC;
290
291         rpc_clnt_sigmask(clnt, &oldset);                
292
293         /* Create/initialize a new RPC task */
294         if (!callback)
295                 callback = rpc_default_callback;
296         status = -ENOMEM;
297         if (!(task = rpc_new_task(clnt, callback, flags)))
298                 goto out;
299         task->tk_calldata = data;
300
301         rpc_call_setup(task, msg, 0);
302
303         /* Set up the call info struct and execute the task */
304         if (task->tk_status == 0)
305                 status = rpc_execute(task);
306         else {
307                 status = task->tk_status;
308                 rpc_release_task(task);
309         }
310
311 out:
312         rpc_clnt_sigunmask(clnt, &oldset);              
313
314         return status;
315 }
316
317
318 void
319 rpc_call_setup(struct rpc_task *task, struct rpc_message *msg, int flags)
320 {
321         task->tk_msg   = *msg;
322         task->tk_flags |= flags;
323         /* Bind the user cred */
324         if (task->tk_msg.rpc_cred != NULL) {
325                 rpcauth_holdcred(task);
326         } else
327                 rpcauth_bindcred(task);
328
329         if (task->tk_status == 0)
330                 task->tk_action = call_start;
331         else
332                 task->tk_action = NULL;
333 }
334
335 void
336 rpc_setbufsize(struct rpc_clnt *clnt, unsigned int sndsize, unsigned int rcvsize)
337 {
338         struct rpc_xprt *xprt = clnt->cl_xprt;
339
340         xprt->sndsize = 0;
341         if (sndsize)
342                 xprt->sndsize = sndsize + RPC_SLACK_SPACE;
343         xprt->rcvsize = 0;
344         if (rcvsize)
345                 xprt->rcvsize = rcvsize + RPC_SLACK_SPACE;
346         xprt_sock_setbufsize(xprt);
347 }
348
349 /*
350  * Restart an (async) RPC call. Usually called from within the
351  * exit handler.
352  */
353 void
354 rpc_restart_call(struct rpc_task *task)
355 {
356         if (RPC_ASSASSINATED(task))
357                 return;
358
359         task->tk_action = call_start;
360 }
361
362 /*
363  * 0.  Initial state
364  *
365  *     Other FSM states can be visited zero or more times, but
366  *     this state is visited exactly once for each RPC.
367  */
368 static void
369 call_start(struct rpc_task *task)
370 {
371         struct rpc_clnt *clnt = task->tk_client;
372
373         if (task->tk_msg.rpc_proc > clnt->cl_maxproc) {
374                 printk(KERN_ERR "%s (vers %d): bad procedure number %d\n",
375                                 clnt->cl_protname, clnt->cl_vers,
376                                 task->tk_msg.rpc_proc);
377                 rpc_exit(task, -EIO);
378                 return;
379         }
380
381         dprintk("RPC: %4d call_start %s%d proc %d (%s)\n", task->tk_pid,
382                 clnt->cl_protname, clnt->cl_vers, task->tk_msg.rpc_proc,
383                 (RPC_IS_ASYNC(task) ? "async" : "sync"));
384
385         /* Increment call count */
386         rpcproc_count(clnt, task->tk_msg.rpc_proc)++;
387         clnt->cl_stats->rpccnt++;
388         task->tk_action = call_reserve;
389 }
390
391 /*
392  * 1.   Reserve an RPC call slot
393  */
394 static void
395 call_reserve(struct rpc_task *task)
396 {
397         struct rpc_clnt *clnt = task->tk_client;
398
399         dprintk("RPC: %4d call_reserve\n", task->tk_pid);
400
401         if (!rpcauth_uptodatecred(task)) {
402                 task->tk_action = call_refresh;
403                 return;
404         }
405
406         task->tk_status  = 0;
407         task->tk_action  = call_reserveresult;
408         task->tk_timeout = clnt->cl_timeout.to_resrvval;
409         xprt_reserve(task);
410 }
411
412 /*
413  * 1b.  Grok the result of xprt_reserve()
414  */
415 static void
416 call_reserveresult(struct rpc_task *task)
417 {
418         int status = task->tk_status;
419
420         dprintk("RPC: %4d call_reserveresult (status %d)\n",
421                                 task->tk_pid, task->tk_status);
422         /*
423          * After a call to xprt_reserve(), we must have either
424          * a request slot or else an error status.
425          */
426         if ((task->tk_status >= 0 && !task->tk_rqstp) ||
427             (task->tk_status < 0 && task->tk_rqstp))
428                 printk(KERN_ERR "call_reserveresult: status=%d, request=%p??\n",
429                  task->tk_status, task->tk_rqstp);
430
431         if (task->tk_status >= 0) {
432                 task->tk_action = call_allocate;
433                 return;
434         }
435
436         task->tk_status = 0;
437         switch (status) {
438         case -EAGAIN:
439         case -ENOBUFS:
440                 task->tk_timeout = task->tk_client->cl_timeout.to_resrvval;
441                 task->tk_action = call_reserve;
442                 break;
443         case -ETIMEDOUT:
444                 dprintk("RPC: task timed out\n");
445                 task->tk_action = call_timeout;
446                 break;
447         default:
448                 if (!task->tk_rqstp) {
449                         printk(KERN_INFO "RPC: task has no request, exit EIO\n");
450                         rpc_exit(task, -EIO);
451                 } else
452                         rpc_exit(task, status);
453         }
454 }
455
456 /*
457  * 2.   Allocate the buffer. For details, see sched.c:rpc_malloc.
458  *      (Note: buffer memory is freed in rpc_task_release).
459  */
460 static void
461 call_allocate(struct rpc_task *task)
462 {
463         struct rpc_clnt *clnt = task->tk_client;
464         unsigned int    bufsiz;
465
466         dprintk("RPC: %4d call_allocate (status %d)\n", 
467                                 task->tk_pid, task->tk_status);
468         task->tk_action = call_encode;
469         if (task->tk_buffer)
470                 return;
471
472         /* FIXME: compute buffer requirements more exactly using
473          * auth->au_wslack */
474         bufsiz = rpcproc_bufsiz(clnt, task->tk_msg.rpc_proc) + RPC_SLACK_SPACE;
475
476         if ((task->tk_buffer = rpc_malloc(task, bufsiz << 1)) != NULL)
477                 return;
478         printk(KERN_INFO "RPC: buffer allocation failed for task %p\n", task); 
479
480         if (RPC_IS_ASYNC(task) || !(task->tk_client->cl_intr && signalled())) {
481                 xprt_release(task);
482                 task->tk_action = call_reserve;
483                 rpc_delay(task, HZ>>4);
484                 return;
485         }
486
487         rpc_exit(task, -ERESTARTSYS);
488 }
489
490 /*
491  * 3.   Encode arguments of an RPC call
492  */
493 static void
494 call_encode(struct rpc_task *task)
495 {
496         struct rpc_clnt *clnt = task->tk_client;
497         struct rpc_rqst *req = task->tk_rqstp;
498         struct xdr_buf *sndbuf = &req->rq_snd_buf;
499         struct xdr_buf *rcvbuf = &req->rq_rcv_buf;
500         unsigned int    bufsiz;
501         kxdrproc_t      encode;
502         int             status;
503         u32             *p;
504
505         dprintk("RPC: %4d call_encode (status %d)\n", 
506                                 task->tk_pid, task->tk_status);
507
508         task->tk_action = call_bind;
509
510         /* Default buffer setup */
511         bufsiz = rpcproc_bufsiz(clnt, task->tk_msg.rpc_proc)+RPC_SLACK_SPACE;
512         sndbuf->head[0].iov_base = (void *)task->tk_buffer;
513         sndbuf->head[0].iov_len  = bufsiz;
514         sndbuf->tail[0].iov_len  = 0;
515         sndbuf->page_len         = 0;
516         sndbuf->len              = 0;
517         rcvbuf->head[0].iov_base = (void *)((char *)task->tk_buffer + bufsiz);
518         rcvbuf->head[0].iov_len  = bufsiz;
519         rcvbuf->tail[0].iov_len  = 0;
520         rcvbuf->page_len         = 0;
521         rcvbuf->len              = bufsiz;
522
523         /* Zero buffer so we have automatic zero-padding of opaque & string */
524         memset(task->tk_buffer, 0, bufsiz);
525
526         /* Encode header and provided arguments */
527         encode = rpcproc_encode(clnt, task->tk_msg.rpc_proc);
528         if (!(p = call_header(task))) {
529                 printk(KERN_INFO "RPC: call_header failed, exit EIO\n");
530                 rpc_exit(task, -EIO);
531         } else
532         if (encode && (status = encode(req, p, task->tk_msg.rpc_argp)) < 0) {
533                 printk(KERN_WARNING "%s: can't encode arguments: %d\n",
534                                 clnt->cl_protname, -status);
535                 rpc_exit(task, status);
536         }
537 }
538
539 /*
540  * 4.   Get the server port number if not yet set
541  */
542 static void
543 call_bind(struct rpc_task *task)
544 {
545         struct rpc_clnt *clnt = task->tk_client;
546         struct rpc_xprt *xprt = clnt->cl_xprt;
547
548         task->tk_action = (xprt_connected(xprt)) ? call_transmit : call_reconnect;
549
550         if (!clnt->cl_port) {
551                 task->tk_action = call_reconnect;
552                 task->tk_timeout = clnt->cl_timeout.to_maxval;
553                 rpc_getport(task, clnt);
554         }
555 }
556
557 /*
558  * 4a.  Reconnect to the RPC server (TCP case)
559  */
560 static void
561 call_reconnect(struct rpc_task *task)
562 {
563         struct rpc_clnt *clnt = task->tk_client;
564         struct rpc_task *child;
565
566         dprintk("RPC: %4d call_reconnect status %d\n",
567                                 task->tk_pid, task->tk_status);
568
569         task->tk_action = call_transmit;
570         if (task->tk_status < 0 || !clnt->cl_xprt->stream)
571                 return;
572
573         /* Run as a child to ensure it runs as an rpciod task */
574         child = rpc_new_child(clnt, task);
575         if (child) {
576                 child->tk_action = child_reconnect;
577                 rpc_run_child(task, child, NULL);
578         }
579 }
580
581 static void child_reconnect(struct rpc_task *task)
582 {
583         task->tk_client->cl_stats->netreconn++;
584         task->tk_status = 0;
585         task->tk_action = child_reconnect_status;
586         xprt_reconnect(task);
587 }
588
589 static void child_reconnect_status(struct rpc_task *task)
590 {
591         if (task->tk_status == -EAGAIN)
592                 task->tk_action = child_reconnect;
593         else
594                 task->tk_action = NULL;
595 }
596
597 /*
598  * 5.   Transmit the RPC request, and wait for reply
599  */
600 static void
601 call_transmit(struct rpc_task *task)
602 {
603         struct rpc_clnt *clnt = task->tk_client;
604
605         dprintk("RPC: %4d call_transmit (status %d)\n", 
606                                 task->tk_pid, task->tk_status);
607
608         task->tk_action = call_status;
609         if (task->tk_status < 0)
610                 return;
611         xprt_transmit(task);
612         if (!rpcproc_decode(clnt, task->tk_msg.rpc_proc) && task->tk_status >= 0) {
613                 task->tk_action = NULL;
614                 rpc_wake_up_task(task);
615         }
616 }
617
618 /*
619  * 6.   Sort out the RPC call status
620  */
621 static void
622 call_status(struct rpc_task *task)
623 {
624         struct rpc_clnt *clnt = task->tk_client;
625         struct rpc_xprt *xprt = clnt->cl_xprt;
626         struct rpc_rqst *req = task->tk_rqstp;
627         int             status;
628
629         if (req->rq_received != 0)
630                 task->tk_status = req->rq_received;
631
632         dprintk("RPC: %4d call_status (status %d)\n", 
633                                 task->tk_pid, task->tk_status);
634
635         status = task->tk_status;
636         if (status >= 0) {
637                 task->tk_action = call_decode;
638                 return;
639         }
640
641         task->tk_status = 0;
642         switch(status) {
643         case -ETIMEDOUT:
644                 task->tk_action = call_timeout;
645                 break;
646         case -ECONNREFUSED:
647         case -ENOTCONN:
648                 req->rq_bytes_sent = 0;
649                 if (clnt->cl_autobind || !clnt->cl_port) {
650                         clnt->cl_port = 0;
651                         task->tk_action = call_bind;
652                         break;
653                 }
654                 if (xprt->stream) {
655                         task->tk_action = call_reconnect;
656                         break;
657                 }
658                 /*
659                  * Sleep and dream of an open connection
660                  */
661                 task->tk_timeout = 5 * HZ;
662                 rpc_sleep_on(&xprt->sending, task, NULL, NULL);
663         case -ENOMEM:
664         case -EAGAIN:
665                 task->tk_action = call_transmit;
666                 break;
667         default:
668                 if (clnt->cl_chatty)
669                         printk("%s: RPC call returned error %d\n",
670                                clnt->cl_protname, -status);
671                 rpc_exit(task, status);
672         }
673 }
674
675 /*
676  * 6a.  Handle RPC timeout
677  *      We do not release the request slot, so we keep using the
678  *      same XID for all retransmits.
679  */
680 static void
681 call_timeout(struct rpc_task *task)
682 {
683         struct rpc_clnt *clnt = task->tk_client;
684         struct rpc_rqst *req = task->tk_rqstp;
685
686         if (req) {
687                 struct rpc_timeout *to = &req->rq_timeout;
688
689                 if (xprt_adjust_timeout(to)) {
690                         dprintk("RPC: %4d call_timeout (minor timeo)\n",
691                                 task->tk_pid);
692                         goto minor_timeout;
693                 }
694                 to->to_retries = clnt->cl_timeout.to_retries;
695         }
696
697         dprintk("RPC: %4d call_timeout (major timeo)\n", task->tk_pid);
698         if (clnt->cl_softrtry) {
699                 if (clnt->cl_chatty && !task->tk_exit)
700                         printk(KERN_NOTICE "%s: server %s not responding, timed out\n",
701                                 clnt->cl_protname, clnt->cl_server);
702                 rpc_exit(task, -EIO);
703                 return;
704         }
705         if (clnt->cl_chatty && !(task->tk_flags & RPC_CALL_MAJORSEEN) && rpc_ntimeo(&clnt->cl_rtt) > 7) {
706                 task->tk_flags |= RPC_CALL_MAJORSEEN;
707                 if (req)
708                         printk(KERN_NOTICE "%s: server %s not responding, still trying\n",
709                                 clnt->cl_protname, clnt->cl_server);
710 #ifdef RPC_DEBUG                                
711                 else
712                         printk(KERN_NOTICE "%s: task %d can't get a request slot\n",
713                                 clnt->cl_protname, task->tk_pid);
714 #endif                          
715         }
716         if (clnt->cl_autobind)
717                 clnt->cl_port = 0;
718
719 minor_timeout:
720         if (!req)
721                 task->tk_action = call_reserve;
722         else if (!clnt->cl_port) {
723                 task->tk_action = call_bind;
724                 clnt->cl_stats->rpcretrans++;
725         } else if (!xprt_connected(clnt->cl_xprt)) {
726                 task->tk_action = call_reconnect;
727                 clnt->cl_stats->rpcretrans++;
728         } else {
729                 task->tk_action = call_transmit;
730                 clnt->cl_stats->rpcretrans++;
731         }
732         task->tk_status = 0;
733 }
734
735 /*
736  * 7.   Decode the RPC reply
737  */
738 static void
739 call_decode(struct rpc_task *task)
740 {
741         struct rpc_clnt *clnt = task->tk_client;
742         struct rpc_rqst *req = task->tk_rqstp;
743         kxdrproc_t      decode = rpcproc_decode(clnt, task->tk_msg.rpc_proc);
744         u32             *p;
745
746         dprintk("RPC: %4d call_decode (status %d)\n", 
747                                 task->tk_pid, task->tk_status);
748
749         if (clnt->cl_chatty && (task->tk_flags & RPC_CALL_MAJORSEEN)) {
750                 printk(KERN_NOTICE "%s: server %s OK\n",
751                         clnt->cl_protname, clnt->cl_server);
752                 task->tk_flags &= ~RPC_CALL_MAJORSEEN;
753         }
754
755         if (task->tk_status < 12) {
756                 if (!clnt->cl_softrtry) {
757                         task->tk_action = call_transmit;
758                         clnt->cl_stats->rpcretrans++;
759                 } else {
760                         printk(KERN_WARNING "%s: too small RPC reply size (%d bytes)\n",
761                                 clnt->cl_protname, task->tk_status);
762                         rpc_exit(task, -EIO);
763                 }
764                 return;
765         }
766
767         /* Verify the RPC header */
768         if (!(p = call_verify(task)))
769                 return;
770
771         /*
772          * The following is an NFS-specific hack to cater for setuid
773          * processes whose uid is mapped to nobody on the server.
774          */
775         if (task->tk_client->cl_droppriv && 
776             (ntohl(*p) == NFSERR_ACCES || ntohl(*p) == NFSERR_PERM)) {
777                 if (RPC_IS_SETUID(task) && task->tk_suid_retry) {
778                         dprintk("RPC: %4d retry squashed uid\n", task->tk_pid);
779                         task->tk_flags ^= RPC_CALL_REALUID;
780                         task->tk_action = call_encode;
781                         task->tk_suid_retry--;
782                         return;
783                 }
784         }
785
786         task->tk_action = NULL;
787
788         if (decode)
789                 task->tk_status = decode(req, p, task->tk_msg.rpc_resp);
790         dprintk("RPC: %4d call_decode result %d\n", task->tk_pid,
791                                         task->tk_status);
792 }
793
794 /*
795  * 8.   Refresh the credentials if rejected by the server
796  */
797 static void
798 call_refresh(struct rpc_task *task)
799 {
800         dprintk("RPC: %4d call_refresh\n", task->tk_pid);
801
802         xprt_release(task);     /* Must do to obtain new XID */
803         task->tk_action = call_refreshresult;
804         task->tk_status = 0;
805         task->tk_client->cl_stats->rpcauthrefresh++;
806         rpcauth_refreshcred(task);
807 }
808
809 /*
810  * 8a.  Process the results of a credential refresh
811  */
812 static void
813 call_refreshresult(struct rpc_task *task)
814 {
815         dprintk("RPC: %4d call_refreshresult (status %d)\n", 
816                                 task->tk_pid, task->tk_status);
817
818         if (task->tk_status < 0)
819                 rpc_exit(task, -EACCES);
820         else
821                 task->tk_action = call_reserve;
822 }
823
824 /*
825  * Call header serialization
826  */
827 static u32 *
828 call_header(struct rpc_task *task)
829 {
830         struct rpc_clnt *clnt = task->tk_client;
831         struct rpc_xprt *xprt = clnt->cl_xprt;
832         struct rpc_rqst *req = task->tk_rqstp;
833         u32             *p = req->rq_svec[0].iov_base;
834
835         /* FIXME: check buffer size? */
836         if (xprt->stream)
837                 *p++ = 0;               /* fill in later */
838         *p++ = req->rq_xid;             /* XID */
839         *p++ = htonl(RPC_CALL);         /* CALL */
840         *p++ = htonl(RPC_VERSION);      /* RPC version */
841         *p++ = htonl(clnt->cl_prog);    /* program number */
842         *p++ = htonl(clnt->cl_vers);    /* program version */
843         *p++ = htonl(task->tk_msg.rpc_proc);    /* procedure */
844         return rpcauth_marshcred(task, p);
845 }
846
847 /*
848  * Reply header verification
849  */
850 static u32 *
851 call_verify(struct rpc_task *task)
852 {
853         u32     *p = task->tk_rqstp->rq_rvec[0].iov_base, n;
854
855         p += 1; /* skip XID */
856
857         if ((n = ntohl(*p++)) != RPC_REPLY) {
858                 printk(KERN_WARNING "call_verify: not an RPC reply: %x\n", n);
859                 goto garbage;
860         }
861         if ((n = ntohl(*p++)) != RPC_MSG_ACCEPTED) {
862                 int     error = -EACCES;
863
864                 if ((n = ntohl(*p++)) != RPC_AUTH_ERROR) {
865                         printk(KERN_WARNING "call_verify: RPC call rejected: %x\n", n);
866                 } else
867                 switch ((n = ntohl(*p++))) {
868                 case RPC_AUTH_REJECTEDCRED:
869                 case RPC_AUTH_REJECTEDVERF:
870                         if (!task->tk_cred_retry)
871                                 break;
872                         task->tk_cred_retry--;
873                         dprintk("RPC: %4d call_verify: retry stale creds\n",
874                                                         task->tk_pid);
875                         rpcauth_invalcred(task);
876                         task->tk_action = call_refresh;
877                         return NULL;
878                 case RPC_AUTH_BADCRED:
879                 case RPC_AUTH_BADVERF:
880                         /* possibly garbled cred/verf? */
881                         if (!task->tk_garb_retry)
882                                 break;
883                         task->tk_garb_retry--;
884                         dprintk("RPC: %4d call_verify: retry garbled creds\n",
885                                                         task->tk_pid);
886                         task->tk_action = call_encode;
887                         return NULL;
888                 case RPC_AUTH_TOOWEAK:
889                         printk(KERN_NOTICE "call_verify: server requires stronger "
890                                "authentication.\n");
891                         break;
892                 default:
893                         printk(KERN_WARNING "call_verify: unknown auth error: %x\n", n);
894                         error = -EIO;
895                 }
896                 dprintk("RPC: %4d call_verify: call rejected %d\n",
897                                                 task->tk_pid, n);
898                 rpc_exit(task, error);
899                 return NULL;
900         }
901         if (!(p = rpcauth_checkverf(task, p))) {
902                 printk(KERN_WARNING "call_verify: auth check failed\n");
903                 goto garbage;           /* bad verifier, retry */
904         }
905         switch ((n = ntohl(*p++))) {
906         case RPC_SUCCESS:
907                 return p;
908         case RPC_GARBAGE_ARGS:
909                 break;                  /* retry */
910         default:
911                 printk(KERN_WARNING "call_verify: server accept status: %x\n", n);
912                 /* Also retry */
913         }
914
915 garbage:
916         dprintk("RPC: %4d call_verify: server saw garbage\n", task->tk_pid);
917         task->tk_client->cl_stats->rpcgarbage++;
918         if (task->tk_garb_retry) {
919                 task->tk_garb_retry--;
920                 dprintk(KERN_WARNING "RPC: garbage, retrying %4d\n", task->tk_pid);
921                 task->tk_action = call_encode;
922                 return NULL;
923         }
924         printk(KERN_WARNING "RPC: garbage, exit EIO\n");
925         rpc_exit(task, -EIO);
926         return NULL;
927 }