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