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