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