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