SUNRPC: Make sure server name is reasonable before trying to print it
[linux-2.6] / 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/smp_lock.h>
31 #include <linux/utsname.h>
32 #include <linux/workqueue.h>
33
34 #include <linux/sunrpc/clnt.h>
35 #include <linux/sunrpc/rpc_pipe_fs.h>
36 #include <linux/sunrpc/metrics.h>
37
38
39 #ifdef RPC_DEBUG
40 # define RPCDBG_FACILITY        RPCDBG_CALL
41 #endif
42
43 #define dprint_status(t)                                        \
44         dprintk("RPC: %5u %s (status %d)\n", t->tk_pid,         \
45                         __FUNCTION__, t->tk_status)
46
47 /*
48  * All RPC clients are linked into this list
49  */
50 static LIST_HEAD(all_clients);
51 static DEFINE_SPINLOCK(rpc_client_lock);
52
53 static DECLARE_WAIT_QUEUE_HEAD(destroy_wait);
54
55
56 static void     call_start(struct rpc_task *task);
57 static void     call_reserve(struct rpc_task *task);
58 static void     call_reserveresult(struct rpc_task *task);
59 static void     call_allocate(struct rpc_task *task);
60 static void     call_encode(struct rpc_task *task);
61 static void     call_decode(struct rpc_task *task);
62 static void     call_bind(struct rpc_task *task);
63 static void     call_bind_status(struct rpc_task *task);
64 static void     call_transmit(struct rpc_task *task);
65 static void     call_status(struct rpc_task *task);
66 static void     call_transmit_status(struct rpc_task *task);
67 static void     call_refresh(struct rpc_task *task);
68 static void     call_refreshresult(struct rpc_task *task);
69 static void     call_timeout(struct rpc_task *task);
70 static void     call_connect(struct rpc_task *task);
71 static void     call_connect_status(struct rpc_task *task);
72 static __be32 * call_header(struct rpc_task *task);
73 static __be32 * call_verify(struct rpc_task *task);
74
75 static int      rpc_ping(struct rpc_clnt *clnt, int flags);
76
77 static void rpc_register_client(struct rpc_clnt *clnt)
78 {
79         spin_lock(&rpc_client_lock);
80         list_add(&clnt->cl_clients, &all_clients);
81         spin_unlock(&rpc_client_lock);
82 }
83
84 static void rpc_unregister_client(struct rpc_clnt *clnt)
85 {
86         spin_lock(&rpc_client_lock);
87         list_del(&clnt->cl_clients);
88         spin_unlock(&rpc_client_lock);
89 }
90
91 static int
92 rpc_setup_pipedir(struct rpc_clnt *clnt, char *dir_name)
93 {
94         static uint32_t clntid;
95         int error;
96
97         clnt->cl_vfsmnt = ERR_PTR(-ENOENT);
98         clnt->cl_dentry = ERR_PTR(-ENOENT);
99         if (dir_name == NULL)
100                 return 0;
101
102         clnt->cl_vfsmnt = rpc_get_mount();
103         if (IS_ERR(clnt->cl_vfsmnt))
104                 return PTR_ERR(clnt->cl_vfsmnt);
105
106         for (;;) {
107                 snprintf(clnt->cl_pathname, sizeof(clnt->cl_pathname),
108                                 "%s/clnt%x", dir_name,
109                                 (unsigned int)clntid++);
110                 clnt->cl_pathname[sizeof(clnt->cl_pathname) - 1] = '\0';
111                 clnt->cl_dentry = rpc_mkdir(clnt->cl_pathname, clnt);
112                 if (!IS_ERR(clnt->cl_dentry))
113                         return 0;
114                 error = PTR_ERR(clnt->cl_dentry);
115                 if (error != -EEXIST) {
116                         printk(KERN_INFO "RPC: Couldn't create pipefs entry %s, error %d\n",
117                                         clnt->cl_pathname, error);
118                         rpc_put_mount();
119                         return error;
120                 }
121         }
122 }
123
124 static struct rpc_clnt * rpc_new_client(struct rpc_xprt *xprt, char *servname, struct rpc_program *program, u32 vers, rpc_authflavor_t flavor)
125 {
126         struct rpc_version      *version;
127         struct rpc_clnt         *clnt = NULL;
128         struct rpc_auth         *auth;
129         int err;
130         size_t len;
131
132         /* sanity check the name before trying to print it */
133         err = -EINVAL;
134         len = strlen(servname);
135         if (len > RPC_MAXNETNAMELEN)
136                 goto out_no_rpciod;
137         len++;
138
139         dprintk("RPC:       creating %s client for %s (xprt %p)\n",
140                         program->name, servname, xprt);
141
142         err = rpciod_up();
143         if (err)
144                 goto out_no_rpciod;
145         err = -EINVAL;
146         if (!xprt)
147                 goto out_no_xprt;
148         if (vers >= program->nrvers || !(version = program->version[vers]))
149                 goto out_err;
150
151         err = -ENOMEM;
152         clnt = kzalloc(sizeof(*clnt), GFP_KERNEL);
153         if (!clnt)
154                 goto out_err;
155         clnt->cl_parent = clnt;
156
157         clnt->cl_server = clnt->cl_inline_name;
158         if (len > sizeof(clnt->cl_inline_name)) {
159                 char *buf = kmalloc(len, GFP_KERNEL);
160                 if (buf != 0)
161                         clnt->cl_server = buf;
162                 else
163                         len = sizeof(clnt->cl_inline_name);
164         }
165         strlcpy(clnt->cl_server, servname, len);
166
167         clnt->cl_xprt     = xprt;
168         clnt->cl_procinfo = version->procs;
169         clnt->cl_maxproc  = version->nrprocs;
170         clnt->cl_protname = program->name;
171         clnt->cl_prog     = program->number;
172         clnt->cl_vers     = version->number;
173         clnt->cl_stats    = program->stats;
174         clnt->cl_metrics  = rpc_alloc_iostats(clnt);
175         err = -ENOMEM;
176         if (clnt->cl_metrics == NULL)
177                 goto out_no_stats;
178         clnt->cl_program  = program;
179         INIT_LIST_HEAD(&clnt->cl_tasks);
180         spin_lock_init(&clnt->cl_lock);
181
182         if (!xprt_bound(clnt->cl_xprt))
183                 clnt->cl_autobind = 1;
184
185         clnt->cl_rtt = &clnt->cl_rtt_default;
186         rpc_init_rtt(&clnt->cl_rtt_default, xprt->timeout.to_initval);
187
188         kref_init(&clnt->cl_kref);
189
190         err = rpc_setup_pipedir(clnt, program->pipe_dir_name);
191         if (err < 0)
192                 goto out_no_path;
193
194         auth = rpcauth_create(flavor, clnt);
195         if (IS_ERR(auth)) {
196                 printk(KERN_INFO "RPC: Couldn't create auth handle (flavor %u)\n",
197                                 flavor);
198                 err = PTR_ERR(auth);
199                 goto out_no_auth;
200         }
201
202         /* save the nodename */
203         clnt->cl_nodelen = strlen(utsname()->nodename);
204         if (clnt->cl_nodelen > UNX_MAXNODENAME)
205                 clnt->cl_nodelen = UNX_MAXNODENAME;
206         memcpy(clnt->cl_nodename, utsname()->nodename, clnt->cl_nodelen);
207         rpc_register_client(clnt);
208         return clnt;
209
210 out_no_auth:
211         if (!IS_ERR(clnt->cl_dentry)) {
212                 rpc_rmdir(clnt->cl_dentry);
213                 rpc_put_mount();
214         }
215 out_no_path:
216         rpc_free_iostats(clnt->cl_metrics);
217 out_no_stats:
218         if (clnt->cl_server != clnt->cl_inline_name)
219                 kfree(clnt->cl_server);
220         kfree(clnt);
221 out_err:
222         xprt_put(xprt);
223 out_no_xprt:
224         rpciod_down();
225 out_no_rpciod:
226         return ERR_PTR(err);
227 }
228
229 /*
230  * rpc_create - create an RPC client and transport with one call
231  * @args: rpc_clnt create argument structure
232  *
233  * Creates and initializes an RPC transport and an RPC client.
234  *
235  * It can ping the server in order to determine if it is up, and to see if
236  * it supports this program and version.  RPC_CLNT_CREATE_NOPING disables
237  * this behavior so asynchronous tasks can also use rpc_create.
238  */
239 struct rpc_clnt *rpc_create(struct rpc_create_args *args)
240 {
241         struct rpc_xprt *xprt;
242         struct rpc_clnt *clnt;
243         struct rpc_xprtsock_create xprtargs = {
244                 .proto = args->protocol,
245                 .srcaddr = args->saddress,
246                 .dstaddr = args->address,
247                 .addrlen = args->addrsize,
248                 .timeout = args->timeout
249         };
250         char servername[20];
251
252         xprt = xprt_create_transport(&xprtargs);
253         if (IS_ERR(xprt))
254                 return (struct rpc_clnt *)xprt;
255
256         /*
257          * If the caller chooses not to specify a hostname, whip
258          * up a string representation of the passed-in address.
259          */
260         if (args->servername == NULL) {
261                 struct sockaddr_in *addr =
262                                         (struct sockaddr_in *) &args->address;
263                 snprintf(servername, sizeof(servername), NIPQUAD_FMT,
264                         NIPQUAD(addr->sin_addr.s_addr));
265                 args->servername = servername;
266         }
267
268         /*
269          * By default, kernel RPC client connects from a reserved port.
270          * CAP_NET_BIND_SERVICE will not be set for unprivileged requesters,
271          * but it is always enabled for rpciod, which handles the connect
272          * operation.
273          */
274         xprt->resvport = 1;
275         if (args->flags & RPC_CLNT_CREATE_NONPRIVPORT)
276                 xprt->resvport = 0;
277
278         clnt = rpc_new_client(xprt, args->servername, args->program,
279                                 args->version, args->authflavor);
280         if (IS_ERR(clnt))
281                 return clnt;
282
283         if (!(args->flags & RPC_CLNT_CREATE_NOPING)) {
284                 int err = rpc_ping(clnt, RPC_TASK_SOFT|RPC_TASK_NOINTR);
285                 if (err != 0) {
286                         rpc_shutdown_client(clnt);
287                         return ERR_PTR(err);
288                 }
289         }
290
291         clnt->cl_softrtry = 1;
292         if (args->flags & RPC_CLNT_CREATE_HARDRTRY)
293                 clnt->cl_softrtry = 0;
294
295         if (args->flags & RPC_CLNT_CREATE_INTR)
296                 clnt->cl_intr = 1;
297         if (args->flags & RPC_CLNT_CREATE_AUTOBIND)
298                 clnt->cl_autobind = 1;
299         if (args->flags & RPC_CLNT_CREATE_DISCRTRY)
300                 clnt->cl_discrtry = 1;
301
302         return clnt;
303 }
304 EXPORT_SYMBOL_GPL(rpc_create);
305
306 /*
307  * This function clones the RPC client structure. It allows us to share the
308  * same transport while varying parameters such as the authentication
309  * flavour.
310  */
311 struct rpc_clnt *
312 rpc_clone_client(struct rpc_clnt *clnt)
313 {
314         struct rpc_clnt *new;
315         int err = -ENOMEM;
316
317         new = kmemdup(clnt, sizeof(*new), GFP_KERNEL);
318         if (!new)
319                 goto out_no_clnt;
320         new->cl_parent = clnt;
321         /* Turn off autobind on clones */
322         new->cl_autobind = 0;
323         INIT_LIST_HEAD(&new->cl_tasks);
324         spin_lock_init(&new->cl_lock);
325         rpc_init_rtt(&new->cl_rtt_default, clnt->cl_xprt->timeout.to_initval);
326         new->cl_metrics = rpc_alloc_iostats(clnt);
327         if (new->cl_metrics == NULL)
328                 goto out_no_stats;
329         kref_init(&new->cl_kref);
330         err = rpc_setup_pipedir(new, clnt->cl_program->pipe_dir_name);
331         if (err != 0)
332                 goto out_no_path;
333         if (new->cl_auth)
334                 atomic_inc(&new->cl_auth->au_count);
335         xprt_get(clnt->cl_xprt);
336         kref_get(&clnt->cl_kref);
337         rpc_register_client(new);
338         rpciod_up();
339         return new;
340 out_no_path:
341         rpc_free_iostats(new->cl_metrics);
342 out_no_stats:
343         kfree(new);
344 out_no_clnt:
345         dprintk("RPC:       %s: returned error %d\n", __FUNCTION__, err);
346         return ERR_PTR(err);
347 }
348
349 /*
350  * Properly shut down an RPC client, terminating all outstanding
351  * requests.
352  */
353 void rpc_shutdown_client(struct rpc_clnt *clnt)
354 {
355         dprintk("RPC:       shutting down %s client for %s\n",
356                         clnt->cl_protname, clnt->cl_server);
357
358         while (!list_empty(&clnt->cl_tasks)) {
359                 rpc_killall_tasks(clnt);
360                 wait_event_timeout(destroy_wait,
361                         list_empty(&clnt->cl_tasks), 1*HZ);
362         }
363
364         rpc_release_client(clnt);
365 }
366
367 /*
368  * Free an RPC client
369  */
370 static void
371 rpc_free_client(struct kref *kref)
372 {
373         struct rpc_clnt *clnt = container_of(kref, struct rpc_clnt, cl_kref);
374
375         dprintk("RPC:       destroying %s client for %s\n",
376                         clnt->cl_protname, clnt->cl_server);
377         if (!IS_ERR(clnt->cl_dentry)) {
378                 rpc_rmdir(clnt->cl_dentry);
379                 rpc_put_mount();
380         }
381         if (clnt->cl_parent != clnt) {
382                 rpc_release_client(clnt->cl_parent);
383                 goto out_free;
384         }
385         if (clnt->cl_server != clnt->cl_inline_name)
386                 kfree(clnt->cl_server);
387 out_free:
388         rpc_unregister_client(clnt);
389         rpc_free_iostats(clnt->cl_metrics);
390         clnt->cl_metrics = NULL;
391         xprt_put(clnt->cl_xprt);
392         rpciod_down();
393         kfree(clnt);
394 }
395
396 /*
397  * Free an RPC client
398  */
399 static void
400 rpc_free_auth(struct kref *kref)
401 {
402         struct rpc_clnt *clnt = container_of(kref, struct rpc_clnt, cl_kref);
403
404         if (clnt->cl_auth == NULL) {
405                 rpc_free_client(kref);
406                 return;
407         }
408
409         /*
410          * Note: RPCSEC_GSS may need to send NULL RPC calls in order to
411          *       release remaining GSS contexts. This mechanism ensures
412          *       that it can do so safely.
413          */
414         kref_init(kref);
415         rpcauth_release(clnt->cl_auth);
416         clnt->cl_auth = NULL;
417         kref_put(kref, rpc_free_client);
418 }
419
420 /*
421  * Release reference to the RPC client
422  */
423 void
424 rpc_release_client(struct rpc_clnt *clnt)
425 {
426         dprintk("RPC:       rpc_release_client(%p)\n", clnt);
427
428         if (list_empty(&clnt->cl_tasks))
429                 wake_up(&destroy_wait);
430         kref_put(&clnt->cl_kref, rpc_free_auth);
431 }
432
433 /**
434  * rpc_bind_new_program - bind a new RPC program to an existing client
435  * @old - old rpc_client
436  * @program - rpc program to set
437  * @vers - rpc program version
438  *
439  * Clones the rpc client and sets up a new RPC program. This is mainly
440  * of use for enabling different RPC programs to share the same transport.
441  * The Sun NFSv2/v3 ACL protocol can do this.
442  */
443 struct rpc_clnt *rpc_bind_new_program(struct rpc_clnt *old,
444                                       struct rpc_program *program,
445                                       u32 vers)
446 {
447         struct rpc_clnt *clnt;
448         struct rpc_version *version;
449         int err;
450
451         BUG_ON(vers >= program->nrvers || !program->version[vers]);
452         version = program->version[vers];
453         clnt = rpc_clone_client(old);
454         if (IS_ERR(clnt))
455                 goto out;
456         clnt->cl_procinfo = version->procs;
457         clnt->cl_maxproc  = version->nrprocs;
458         clnt->cl_protname = program->name;
459         clnt->cl_prog     = program->number;
460         clnt->cl_vers     = version->number;
461         clnt->cl_stats    = program->stats;
462         err = rpc_ping(clnt, RPC_TASK_SOFT|RPC_TASK_NOINTR);
463         if (err != 0) {
464                 rpc_shutdown_client(clnt);
465                 clnt = ERR_PTR(err);
466         }
467 out:
468         return clnt;
469 }
470
471 /*
472  * Default callback for async RPC calls
473  */
474 static void
475 rpc_default_callback(struct rpc_task *task, void *data)
476 {
477 }
478
479 static const struct rpc_call_ops rpc_default_ops = {
480         .rpc_call_done = rpc_default_callback,
481 };
482
483 /*
484  *      Export the signal mask handling for synchronous code that
485  *      sleeps on RPC calls
486  */
487 #define RPC_INTR_SIGNALS (sigmask(SIGHUP) | sigmask(SIGINT) | sigmask(SIGQUIT) | sigmask(SIGTERM))
488
489 static void rpc_save_sigmask(sigset_t *oldset, int intr)
490 {
491         unsigned long   sigallow = sigmask(SIGKILL);
492         sigset_t sigmask;
493
494         /* Block all signals except those listed in sigallow */
495         if (intr)
496                 sigallow |= RPC_INTR_SIGNALS;
497         siginitsetinv(&sigmask, sigallow);
498         sigprocmask(SIG_BLOCK, &sigmask, oldset);
499 }
500
501 static inline void rpc_task_sigmask(struct rpc_task *task, sigset_t *oldset)
502 {
503         rpc_save_sigmask(oldset, !RPC_TASK_UNINTERRUPTIBLE(task));
504 }
505
506 static inline void rpc_restore_sigmask(sigset_t *oldset)
507 {
508         sigprocmask(SIG_SETMASK, oldset, NULL);
509 }
510
511 void rpc_clnt_sigmask(struct rpc_clnt *clnt, sigset_t *oldset)
512 {
513         rpc_save_sigmask(oldset, clnt->cl_intr);
514 }
515
516 void rpc_clnt_sigunmask(struct rpc_clnt *clnt, sigset_t *oldset)
517 {
518         rpc_restore_sigmask(oldset);
519 }
520
521 static
522 struct rpc_task *rpc_do_run_task(struct rpc_clnt *clnt,
523                 struct rpc_message *msg,
524                 int flags,
525                 const struct rpc_call_ops *ops,
526                 void *data)
527 {
528         struct rpc_task *task, *ret;
529         sigset_t oldset;
530
531         task = rpc_new_task(clnt, flags, ops, data);
532         if (task == NULL) {
533                 rpc_release_calldata(ops, data);
534                 return ERR_PTR(-ENOMEM);
535         }
536
537         /* Mask signals on synchronous RPC calls and RPCSEC_GSS upcalls */
538         rpc_task_sigmask(task, &oldset);
539         if (msg != NULL) {
540                 rpc_call_setup(task, msg, 0);
541                 if (task->tk_status != 0) {
542                         ret = ERR_PTR(task->tk_status);
543                         rpc_put_task(task);
544                         goto out;
545                 }
546         }
547         atomic_inc(&task->tk_count);
548         rpc_execute(task);
549         ret = task;
550 out:
551         rpc_restore_sigmask(&oldset);
552         return ret;
553 }
554
555 /**
556  * rpc_call_sync - Perform a synchronous RPC call
557  * @clnt: pointer to RPC client
558  * @msg: RPC call parameters
559  * @flags: RPC call flags
560  */
561 int rpc_call_sync(struct rpc_clnt *clnt, struct rpc_message *msg, int flags)
562 {
563         struct rpc_task *task;
564         int status;
565
566         BUG_ON(flags & RPC_TASK_ASYNC);
567
568         task = rpc_do_run_task(clnt, msg, flags, &rpc_default_ops, NULL);
569         if (IS_ERR(task))
570                 return PTR_ERR(task);
571         status = task->tk_status;
572         rpc_put_task(task);
573         return status;
574 }
575
576 /**
577  * rpc_call_async - Perform an asynchronous RPC call
578  * @clnt: pointer to RPC client
579  * @msg: RPC call parameters
580  * @flags: RPC call flags
581  * @ops: RPC call ops
582  * @data: user call data
583  */
584 int
585 rpc_call_async(struct rpc_clnt *clnt, struct rpc_message *msg, int flags,
586                const struct rpc_call_ops *tk_ops, void *data)
587 {
588         struct rpc_task *task;
589
590         task = rpc_do_run_task(clnt, msg, flags|RPC_TASK_ASYNC, tk_ops, data);
591         if (IS_ERR(task))
592                 return PTR_ERR(task);
593         rpc_put_task(task);
594         return 0;
595 }
596
597 /**
598  * rpc_run_task - Allocate a new RPC task, then run rpc_execute against it
599  * @clnt: pointer to RPC client
600  * @flags: RPC flags
601  * @ops: RPC call ops
602  * @data: user call data
603  */
604 struct rpc_task *rpc_run_task(struct rpc_clnt *clnt, int flags,
605                                         const struct rpc_call_ops *tk_ops,
606                                         void *data)
607 {
608         return rpc_do_run_task(clnt, NULL, flags, tk_ops, data);
609 }
610 EXPORT_SYMBOL(rpc_run_task);
611
612 void
613 rpc_call_setup(struct rpc_task *task, struct rpc_message *msg, int flags)
614 {
615         task->tk_msg   = *msg;
616         task->tk_flags |= flags;
617         /* Bind the user cred */
618         if (task->tk_msg.rpc_cred != NULL)
619                 rpcauth_holdcred(task);
620         else
621                 rpcauth_bindcred(task);
622
623         if (task->tk_status == 0)
624                 task->tk_action = call_start;
625         else
626                 task->tk_action = rpc_exit_task;
627 }
628
629 /**
630  * rpc_peeraddr - extract remote peer address from clnt's xprt
631  * @clnt: RPC client structure
632  * @buf: target buffer
633  * @size: length of target buffer
634  *
635  * Returns the number of bytes that are actually in the stored address.
636  */
637 size_t rpc_peeraddr(struct rpc_clnt *clnt, struct sockaddr *buf, size_t bufsize)
638 {
639         size_t bytes;
640         struct rpc_xprt *xprt = clnt->cl_xprt;
641
642         bytes = sizeof(xprt->addr);
643         if (bytes > bufsize)
644                 bytes = bufsize;
645         memcpy(buf, &clnt->cl_xprt->addr, bytes);
646         return xprt->addrlen;
647 }
648 EXPORT_SYMBOL_GPL(rpc_peeraddr);
649
650 /**
651  * rpc_peeraddr2str - return remote peer address in printable format
652  * @clnt: RPC client structure
653  * @format: address format
654  *
655  */
656 char *rpc_peeraddr2str(struct rpc_clnt *clnt, enum rpc_display_format_t format)
657 {
658         struct rpc_xprt *xprt = clnt->cl_xprt;
659
660         if (xprt->address_strings[format] != NULL)
661                 return xprt->address_strings[format];
662         else
663                 return "unprintable";
664 }
665 EXPORT_SYMBOL_GPL(rpc_peeraddr2str);
666
667 void
668 rpc_setbufsize(struct rpc_clnt *clnt, unsigned int sndsize, unsigned int rcvsize)
669 {
670         struct rpc_xprt *xprt = clnt->cl_xprt;
671         if (xprt->ops->set_buffer_size)
672                 xprt->ops->set_buffer_size(xprt, sndsize, rcvsize);
673 }
674
675 /*
676  * Return size of largest payload RPC client can support, in bytes
677  *
678  * For stream transports, this is one RPC record fragment (see RFC
679  * 1831), as we don't support multi-record requests yet.  For datagram
680  * transports, this is the size of an IP packet minus the IP, UDP, and
681  * RPC header sizes.
682  */
683 size_t rpc_max_payload(struct rpc_clnt *clnt)
684 {
685         return clnt->cl_xprt->max_payload;
686 }
687 EXPORT_SYMBOL_GPL(rpc_max_payload);
688
689 /**
690  * rpc_force_rebind - force transport to check that remote port is unchanged
691  * @clnt: client to rebind
692  *
693  */
694 void rpc_force_rebind(struct rpc_clnt *clnt)
695 {
696         if (clnt->cl_autobind)
697                 xprt_clear_bound(clnt->cl_xprt);
698 }
699 EXPORT_SYMBOL_GPL(rpc_force_rebind);
700
701 /*
702  * Restart an (async) RPC call. Usually called from within the
703  * exit handler.
704  */
705 void
706 rpc_restart_call(struct rpc_task *task)
707 {
708         if (RPC_ASSASSINATED(task))
709                 return;
710
711         task->tk_action = call_start;
712 }
713
714 /*
715  * 0.  Initial state
716  *
717  *     Other FSM states can be visited zero or more times, but
718  *     this state is visited exactly once for each RPC.
719  */
720 static void
721 call_start(struct rpc_task *task)
722 {
723         struct rpc_clnt *clnt = task->tk_client;
724
725         dprintk("RPC: %5u call_start %s%d proc %d (%s)\n", task->tk_pid,
726                         clnt->cl_protname, clnt->cl_vers,
727                         task->tk_msg.rpc_proc->p_proc,
728                         (RPC_IS_ASYNC(task) ? "async" : "sync"));
729
730         /* Increment call count */
731         task->tk_msg.rpc_proc->p_count++;
732         clnt->cl_stats->rpccnt++;
733         task->tk_action = call_reserve;
734 }
735
736 /*
737  * 1.   Reserve an RPC call slot
738  */
739 static void
740 call_reserve(struct rpc_task *task)
741 {
742         dprint_status(task);
743
744         if (!rpcauth_uptodatecred(task)) {
745                 task->tk_action = call_refresh;
746                 return;
747         }
748
749         task->tk_status  = 0;
750         task->tk_action  = call_reserveresult;
751         xprt_reserve(task);
752 }
753
754 /*
755  * 1b.  Grok the result of xprt_reserve()
756  */
757 static void
758 call_reserveresult(struct rpc_task *task)
759 {
760         int status = task->tk_status;
761
762         dprint_status(task);
763
764         /*
765          * After a call to xprt_reserve(), we must have either
766          * a request slot or else an error status.
767          */
768         task->tk_status = 0;
769         if (status >= 0) {
770                 if (task->tk_rqstp) {
771                         task->tk_action = call_allocate;
772                         return;
773                 }
774
775                 printk(KERN_ERR "%s: status=%d, but no request slot, exiting\n",
776                                 __FUNCTION__, status);
777                 rpc_exit(task, -EIO);
778                 return;
779         }
780
781         /*
782          * Even though there was an error, we may have acquired
783          * a request slot somehow.  Make sure not to leak it.
784          */
785         if (task->tk_rqstp) {
786                 printk(KERN_ERR "%s: status=%d, request allocated anyway\n",
787                                 __FUNCTION__, status);
788                 xprt_release(task);
789         }
790
791         switch (status) {
792         case -EAGAIN:   /* woken up; retry */
793                 task->tk_action = call_reserve;
794                 return;
795         case -EIO:      /* probably a shutdown */
796                 break;
797         default:
798                 printk(KERN_ERR "%s: unrecognized error %d, exiting\n",
799                                 __FUNCTION__, status);
800                 break;
801         }
802         rpc_exit(task, status);
803 }
804
805 /*
806  * 2.   Allocate the buffer. For details, see sched.c:rpc_malloc.
807  *      (Note: buffer memory is freed in xprt_release).
808  */
809 static void
810 call_allocate(struct rpc_task *task)
811 {
812         unsigned int slack = task->tk_msg.rpc_cred->cr_auth->au_cslack;
813         struct rpc_rqst *req = task->tk_rqstp;
814         struct rpc_xprt *xprt = task->tk_xprt;
815         struct rpc_procinfo *proc = task->tk_msg.rpc_proc;
816
817         dprint_status(task);
818
819         task->tk_status = 0;
820         task->tk_action = call_bind;
821
822         if (req->rq_buffer)
823                 return;
824
825         if (proc->p_proc != 0) {
826                 BUG_ON(proc->p_arglen == 0);
827                 if (proc->p_decode != NULL)
828                         BUG_ON(proc->p_replen == 0);
829         }
830
831         /*
832          * Calculate the size (in quads) of the RPC call
833          * and reply headers, and convert both values
834          * to byte sizes.
835          */
836         req->rq_callsize = RPC_CALLHDRSIZE + (slack << 1) + proc->p_arglen;
837         req->rq_callsize <<= 2;
838         req->rq_rcvsize = RPC_REPHDRSIZE + slack + proc->p_replen;
839         req->rq_rcvsize <<= 2;
840
841         req->rq_buffer = xprt->ops->buf_alloc(task,
842                                         req->rq_callsize + req->rq_rcvsize);
843         if (req->rq_buffer != NULL)
844                 return;
845
846         dprintk("RPC: %5u rpc_buffer allocation failed\n", task->tk_pid);
847
848         if (RPC_IS_ASYNC(task) || !signalled()) {
849                 xprt_release(task);
850                 task->tk_action = call_reserve;
851                 rpc_delay(task, HZ>>4);
852                 return;
853         }
854
855         rpc_exit(task, -ERESTARTSYS);
856 }
857
858 static inline int
859 rpc_task_need_encode(struct rpc_task *task)
860 {
861         return task->tk_rqstp->rq_snd_buf.len == 0;
862 }
863
864 static inline void
865 rpc_task_force_reencode(struct rpc_task *task)
866 {
867         task->tk_rqstp->rq_snd_buf.len = 0;
868 }
869
870 static inline void
871 rpc_xdr_buf_init(struct xdr_buf *buf, void *start, size_t len)
872 {
873         buf->head[0].iov_base = start;
874         buf->head[0].iov_len = len;
875         buf->tail[0].iov_len = 0;
876         buf->page_len = 0;
877         buf->len = 0;
878         buf->buflen = len;
879 }
880
881 /*
882  * 3.   Encode arguments of an RPC call
883  */
884 static void
885 call_encode(struct rpc_task *task)
886 {
887         struct rpc_rqst *req = task->tk_rqstp;
888         kxdrproc_t      encode;
889         __be32          *p;
890
891         dprint_status(task);
892
893         rpc_xdr_buf_init(&req->rq_snd_buf,
894                          req->rq_buffer,
895                          req->rq_callsize);
896         rpc_xdr_buf_init(&req->rq_rcv_buf,
897                          (char *)req->rq_buffer + req->rq_callsize,
898                          req->rq_rcvsize);
899
900         /* Encode header and provided arguments */
901         encode = task->tk_msg.rpc_proc->p_encode;
902         if (!(p = call_header(task))) {
903                 printk(KERN_INFO "RPC: call_header failed, exit EIO\n");
904                 rpc_exit(task, -EIO);
905                 return;
906         }
907         if (encode == NULL)
908                 return;
909
910         task->tk_status = rpcauth_wrap_req(task, encode, req, p,
911                         task->tk_msg.rpc_argp);
912         if (task->tk_status == -ENOMEM) {
913                 /* XXX: Is this sane? */
914                 rpc_delay(task, 3*HZ);
915                 task->tk_status = -EAGAIN;
916         }
917 }
918
919 /*
920  * 4.   Get the server port number if not yet set
921  */
922 static void
923 call_bind(struct rpc_task *task)
924 {
925         struct rpc_xprt *xprt = task->tk_xprt;
926
927         dprint_status(task);
928
929         task->tk_action = call_connect;
930         if (!xprt_bound(xprt)) {
931                 task->tk_action = call_bind_status;
932                 task->tk_timeout = xprt->bind_timeout;
933                 xprt->ops->rpcbind(task);
934         }
935 }
936
937 /*
938  * 4a.  Sort out bind result
939  */
940 static void
941 call_bind_status(struct rpc_task *task)
942 {
943         int status = -EACCES;
944
945         if (task->tk_status >= 0) {
946                 dprint_status(task);
947                 task->tk_status = 0;
948                 task->tk_action = call_connect;
949                 return;
950         }
951
952         switch (task->tk_status) {
953         case -EACCES:
954                 dprintk("RPC: %5u remote rpcbind: RPC program/version "
955                                 "unavailable\n", task->tk_pid);
956                 rpc_delay(task, 3*HZ);
957                 goto retry_timeout;
958         case -ETIMEDOUT:
959                 dprintk("RPC: %5u rpcbind request timed out\n",
960                                 task->tk_pid);
961                 goto retry_timeout;
962         case -EPFNOSUPPORT:
963                 dprintk("RPC: %5u remote rpcbind service unavailable\n",
964                                 task->tk_pid);
965                 break;
966         case -EPROTONOSUPPORT:
967                 dprintk("RPC: %5u remote rpcbind version unavailable, retrying\n",
968                                 task->tk_pid);
969                 task->tk_status = 0;
970                 task->tk_action = call_bind;
971                 return;
972         default:
973                 dprintk("RPC: %5u unrecognized rpcbind error (%d)\n",
974                                 task->tk_pid, -task->tk_status);
975                 status = -EIO;
976         }
977
978         rpc_exit(task, status);
979         return;
980
981 retry_timeout:
982         task->tk_action = call_timeout;
983 }
984
985 /*
986  * 4b.  Connect to the RPC server
987  */
988 static void
989 call_connect(struct rpc_task *task)
990 {
991         struct rpc_xprt *xprt = task->tk_xprt;
992
993         dprintk("RPC: %5u call_connect xprt %p %s connected\n",
994                         task->tk_pid, xprt,
995                         (xprt_connected(xprt) ? "is" : "is not"));
996
997         task->tk_action = call_transmit;
998         if (!xprt_connected(xprt)) {
999                 task->tk_action = call_connect_status;
1000                 if (task->tk_status < 0)
1001                         return;
1002                 xprt_connect(task);
1003         }
1004 }
1005
1006 /*
1007  * 4c.  Sort out connect result
1008  */
1009 static void
1010 call_connect_status(struct rpc_task *task)
1011 {
1012         struct rpc_clnt *clnt = task->tk_client;
1013         int status = task->tk_status;
1014
1015         dprint_status(task);
1016
1017         task->tk_status = 0;
1018         if (status >= 0) {
1019                 clnt->cl_stats->netreconn++;
1020                 task->tk_action = call_transmit;
1021                 return;
1022         }
1023
1024         /* Something failed: remote service port may have changed */
1025         rpc_force_rebind(clnt);
1026
1027         switch (status) {
1028         case -ENOTCONN:
1029         case -EAGAIN:
1030                 task->tk_action = call_bind;
1031                 if (!RPC_IS_SOFT(task))
1032                         return;
1033                 /* if soft mounted, test if we've timed out */
1034         case -ETIMEDOUT:
1035                 task->tk_action = call_timeout;
1036                 return;
1037         }
1038         rpc_exit(task, -EIO);
1039 }
1040
1041 /*
1042  * 5.   Transmit the RPC request, and wait for reply
1043  */
1044 static void
1045 call_transmit(struct rpc_task *task)
1046 {
1047         dprint_status(task);
1048
1049         task->tk_action = call_status;
1050         if (task->tk_status < 0)
1051                 return;
1052         task->tk_status = xprt_prepare_transmit(task);
1053         if (task->tk_status != 0)
1054                 return;
1055         task->tk_action = call_transmit_status;
1056         /* Encode here so that rpcsec_gss can use correct sequence number. */
1057         if (rpc_task_need_encode(task)) {
1058                 BUG_ON(task->tk_rqstp->rq_bytes_sent != 0);
1059                 call_encode(task);
1060                 /* Did the encode result in an error condition? */
1061                 if (task->tk_status != 0)
1062                         return;
1063         }
1064         xprt_transmit(task);
1065         if (task->tk_status < 0)
1066                 return;
1067         /*
1068          * On success, ensure that we call xprt_end_transmit() before sleeping
1069          * in order to allow access to the socket to other RPC requests.
1070          */
1071         call_transmit_status(task);
1072         if (task->tk_msg.rpc_proc->p_decode != NULL)
1073                 return;
1074         task->tk_action = rpc_exit_task;
1075         rpc_wake_up_task(task);
1076 }
1077
1078 /*
1079  * 5a.  Handle cleanup after a transmission
1080  */
1081 static void
1082 call_transmit_status(struct rpc_task *task)
1083 {
1084         task->tk_action = call_status;
1085         /*
1086          * Special case: if we've been waiting on the socket's write_space()
1087          * callback, then don't call xprt_end_transmit().
1088          */
1089         if (task->tk_status == -EAGAIN)
1090                 return;
1091         xprt_end_transmit(task);
1092         rpc_task_force_reencode(task);
1093 }
1094
1095 /*
1096  * 6.   Sort out the RPC call status
1097  */
1098 static void
1099 call_status(struct rpc_task *task)
1100 {
1101         struct rpc_clnt *clnt = task->tk_client;
1102         struct rpc_rqst *req = task->tk_rqstp;
1103         int             status;
1104
1105         if (req->rq_received > 0 && !req->rq_bytes_sent)
1106                 task->tk_status = req->rq_received;
1107
1108         dprint_status(task);
1109
1110         status = task->tk_status;
1111         if (status >= 0) {
1112                 task->tk_action = call_decode;
1113                 return;
1114         }
1115
1116         task->tk_status = 0;
1117         switch(status) {
1118         case -EHOSTDOWN:
1119         case -EHOSTUNREACH:
1120         case -ENETUNREACH:
1121                 /*
1122                  * Delay any retries for 3 seconds, then handle as if it
1123                  * were a timeout.
1124                  */
1125                 rpc_delay(task, 3*HZ);
1126         case -ETIMEDOUT:
1127                 task->tk_action = call_timeout;
1128                 if (task->tk_client->cl_discrtry)
1129                         xprt_disconnect(task->tk_xprt);
1130                 break;
1131         case -ECONNREFUSED:
1132         case -ENOTCONN:
1133                 rpc_force_rebind(clnt);
1134                 task->tk_action = call_bind;
1135                 break;
1136         case -EAGAIN:
1137                 task->tk_action = call_transmit;
1138                 break;
1139         case -EIO:
1140                 /* shutdown or soft timeout */
1141                 rpc_exit(task, status);
1142                 break;
1143         default:
1144                 printk("%s: RPC call returned error %d\n",
1145                                clnt->cl_protname, -status);
1146                 rpc_exit(task, status);
1147         }
1148 }
1149
1150 /*
1151  * 6a.  Handle RPC timeout
1152  *      We do not release the request slot, so we keep using the
1153  *      same XID for all retransmits.
1154  */
1155 static void
1156 call_timeout(struct rpc_task *task)
1157 {
1158         struct rpc_clnt *clnt = task->tk_client;
1159
1160         if (xprt_adjust_timeout(task->tk_rqstp) == 0) {
1161                 dprintk("RPC: %5u call_timeout (minor)\n", task->tk_pid);
1162                 goto retry;
1163         }
1164
1165         dprintk("RPC: %5u call_timeout (major)\n", task->tk_pid);
1166         task->tk_timeouts++;
1167
1168         if (RPC_IS_SOFT(task)) {
1169                 printk(KERN_NOTICE "%s: server %s not responding, timed out\n",
1170                                 clnt->cl_protname, clnt->cl_server);
1171                 rpc_exit(task, -EIO);
1172                 return;
1173         }
1174
1175         if (!(task->tk_flags & RPC_CALL_MAJORSEEN)) {
1176                 task->tk_flags |= RPC_CALL_MAJORSEEN;
1177                 printk(KERN_NOTICE "%s: server %s not responding, still trying\n",
1178                         clnt->cl_protname, clnt->cl_server);
1179         }
1180         rpc_force_rebind(clnt);
1181
1182 retry:
1183         clnt->cl_stats->rpcretrans++;
1184         task->tk_action = call_bind;
1185         task->tk_status = 0;
1186 }
1187
1188 /*
1189  * 7.   Decode the RPC reply
1190  */
1191 static void
1192 call_decode(struct rpc_task *task)
1193 {
1194         struct rpc_clnt *clnt = task->tk_client;
1195         struct rpc_rqst *req = task->tk_rqstp;
1196         kxdrproc_t      decode = task->tk_msg.rpc_proc->p_decode;
1197         __be32          *p;
1198
1199         dprintk("RPC: %5u call_decode (status %d)\n",
1200                         task->tk_pid, task->tk_status);
1201
1202         if (task->tk_flags & RPC_CALL_MAJORSEEN) {
1203                 printk(KERN_NOTICE "%s: server %s OK\n",
1204                         clnt->cl_protname, clnt->cl_server);
1205                 task->tk_flags &= ~RPC_CALL_MAJORSEEN;
1206         }
1207
1208         if (task->tk_status < 12) {
1209                 if (!RPC_IS_SOFT(task)) {
1210                         task->tk_action = call_bind;
1211                         clnt->cl_stats->rpcretrans++;
1212                         goto out_retry;
1213                 }
1214                 dprintk("RPC:       %s: too small RPC reply size (%d bytes)\n",
1215                                 clnt->cl_protname, task->tk_status);
1216                 task->tk_action = call_timeout;
1217                 goto out_retry;
1218         }
1219
1220         /*
1221          * Ensure that we see all writes made by xprt_complete_rqst()
1222          * before it changed req->rq_received.
1223          */
1224         smp_rmb();
1225         req->rq_rcv_buf.len = req->rq_private_buf.len;
1226
1227         /* Check that the softirq receive buffer is valid */
1228         WARN_ON(memcmp(&req->rq_rcv_buf, &req->rq_private_buf,
1229                                 sizeof(req->rq_rcv_buf)) != 0);
1230
1231         /* Verify the RPC header */
1232         p = call_verify(task);
1233         if (IS_ERR(p)) {
1234                 if (p == ERR_PTR(-EAGAIN))
1235                         goto out_retry;
1236                 return;
1237         }
1238
1239         task->tk_action = rpc_exit_task;
1240
1241         if (decode) {
1242                 task->tk_status = rpcauth_unwrap_resp(task, decode, req, p,
1243                                                       task->tk_msg.rpc_resp);
1244         }
1245         dprintk("RPC: %5u call_decode result %d\n", task->tk_pid,
1246                         task->tk_status);
1247         return;
1248 out_retry:
1249         req->rq_received = req->rq_private_buf.len = 0;
1250         task->tk_status = 0;
1251         if (task->tk_client->cl_discrtry)
1252                 xprt_disconnect(task->tk_xprt);
1253 }
1254
1255 /*
1256  * 8.   Refresh the credentials if rejected by the server
1257  */
1258 static void
1259 call_refresh(struct rpc_task *task)
1260 {
1261         dprint_status(task);
1262
1263         xprt_release(task);     /* Must do to obtain new XID */
1264         task->tk_action = call_refreshresult;
1265         task->tk_status = 0;
1266         task->tk_client->cl_stats->rpcauthrefresh++;
1267         rpcauth_refreshcred(task);
1268 }
1269
1270 /*
1271  * 8a.  Process the results of a credential refresh
1272  */
1273 static void
1274 call_refreshresult(struct rpc_task *task)
1275 {
1276         int status = task->tk_status;
1277
1278         dprint_status(task);
1279
1280         task->tk_status = 0;
1281         task->tk_action = call_reserve;
1282         if (status >= 0 && rpcauth_uptodatecred(task))
1283                 return;
1284         if (status == -EACCES) {
1285                 rpc_exit(task, -EACCES);
1286                 return;
1287         }
1288         task->tk_action = call_refresh;
1289         if (status != -ETIMEDOUT)
1290                 rpc_delay(task, 3*HZ);
1291         return;
1292 }
1293
1294 /*
1295  * Call header serialization
1296  */
1297 static __be32 *
1298 call_header(struct rpc_task *task)
1299 {
1300         struct rpc_clnt *clnt = task->tk_client;
1301         struct rpc_rqst *req = task->tk_rqstp;
1302         __be32          *p = req->rq_svec[0].iov_base;
1303
1304         /* FIXME: check buffer size? */
1305
1306         p = xprt_skip_transport_header(task->tk_xprt, p);
1307         *p++ = req->rq_xid;             /* XID */
1308         *p++ = htonl(RPC_CALL);         /* CALL */
1309         *p++ = htonl(RPC_VERSION);      /* RPC version */
1310         *p++ = htonl(clnt->cl_prog);    /* program number */
1311         *p++ = htonl(clnt->cl_vers);    /* program version */
1312         *p++ = htonl(task->tk_msg.rpc_proc->p_proc);    /* procedure */
1313         p = rpcauth_marshcred(task, p);
1314         req->rq_slen = xdr_adjust_iovec(&req->rq_svec[0], p);
1315         return p;
1316 }
1317
1318 /*
1319  * Reply header verification
1320  */
1321 static __be32 *
1322 call_verify(struct rpc_task *task)
1323 {
1324         struct kvec *iov = &task->tk_rqstp->rq_rcv_buf.head[0];
1325         int len = task->tk_rqstp->rq_rcv_buf.len >> 2;
1326         __be32  *p = iov->iov_base;
1327         u32 n;
1328         int error = -EACCES;
1329
1330         if ((task->tk_rqstp->rq_rcv_buf.len & 3) != 0) {
1331                 /* RFC-1014 says that the representation of XDR data must be a
1332                  * multiple of four bytes
1333                  * - if it isn't pointer subtraction in the NFS client may give
1334                  *   undefined results
1335                  */
1336                 dprintk("RPC: %5u %s: XDR representation not a multiple of"
1337                        " 4 bytes: 0x%x\n", task->tk_pid, __FUNCTION__,
1338                        task->tk_rqstp->rq_rcv_buf.len);
1339                 goto out_eio;
1340         }
1341         if ((len -= 3) < 0)
1342                 goto out_overflow;
1343         p += 1; /* skip XID */
1344
1345         if ((n = ntohl(*p++)) != RPC_REPLY) {
1346                 dprintk("RPC: %5u %s: not an RPC reply: %x\n",
1347                                 task->tk_pid, __FUNCTION__, n);
1348                 goto out_garbage;
1349         }
1350         if ((n = ntohl(*p++)) != RPC_MSG_ACCEPTED) {
1351                 if (--len < 0)
1352                         goto out_overflow;
1353                 switch ((n = ntohl(*p++))) {
1354                         case RPC_AUTH_ERROR:
1355                                 break;
1356                         case RPC_MISMATCH:
1357                                 dprintk("RPC: %5u %s: RPC call version "
1358                                                 "mismatch!\n",
1359                                                 task->tk_pid, __FUNCTION__);
1360                                 error = -EPROTONOSUPPORT;
1361                                 goto out_err;
1362                         default:
1363                                 dprintk("RPC: %5u %s: RPC call rejected, "
1364                                                 "unknown error: %x\n",
1365                                                 task->tk_pid, __FUNCTION__, n);
1366                                 goto out_eio;
1367                 }
1368                 if (--len < 0)
1369                         goto out_overflow;
1370                 switch ((n = ntohl(*p++))) {
1371                 case RPC_AUTH_REJECTEDCRED:
1372                 case RPC_AUTH_REJECTEDVERF:
1373                 case RPCSEC_GSS_CREDPROBLEM:
1374                 case RPCSEC_GSS_CTXPROBLEM:
1375                         if (!task->tk_cred_retry)
1376                                 break;
1377                         task->tk_cred_retry--;
1378                         dprintk("RPC: %5u %s: retry stale creds\n",
1379                                         task->tk_pid, __FUNCTION__);
1380                         rpcauth_invalcred(task);
1381                         task->tk_action = call_refresh;
1382                         goto out_retry;
1383                 case RPC_AUTH_BADCRED:
1384                 case RPC_AUTH_BADVERF:
1385                         /* possibly garbled cred/verf? */
1386                         if (!task->tk_garb_retry)
1387                                 break;
1388                         task->tk_garb_retry--;
1389                         dprintk("RPC: %5u %s: retry garbled creds\n",
1390                                         task->tk_pid, __FUNCTION__);
1391                         task->tk_action = call_bind;
1392                         goto out_retry;
1393                 case RPC_AUTH_TOOWEAK:
1394                         printk(KERN_NOTICE "call_verify: server %s requires stronger "
1395                                "authentication.\n", task->tk_client->cl_server);
1396                         break;
1397                 default:
1398                         dprintk("RPC: %5u %s: unknown auth error: %x\n",
1399                                         task->tk_pid, __FUNCTION__, n);
1400                         error = -EIO;
1401                 }
1402                 dprintk("RPC: %5u %s: call rejected %d\n",
1403                                 task->tk_pid, __FUNCTION__, n);
1404                 goto out_err;
1405         }
1406         if (!(p = rpcauth_checkverf(task, p))) {
1407                 dprintk("RPC: %5u %s: auth check failed\n",
1408                                 task->tk_pid, __FUNCTION__);
1409                 goto out_garbage;               /* bad verifier, retry */
1410         }
1411         len = p - (__be32 *)iov->iov_base - 1;
1412         if (len < 0)
1413                 goto out_overflow;
1414         switch ((n = ntohl(*p++))) {
1415         case RPC_SUCCESS:
1416                 return p;
1417         case RPC_PROG_UNAVAIL:
1418                 dprintk("RPC: %5u %s: program %u is unsupported by server %s\n",
1419                                 task->tk_pid, __FUNCTION__,
1420                                 (unsigned int)task->tk_client->cl_prog,
1421                                 task->tk_client->cl_server);
1422                 error = -EPFNOSUPPORT;
1423                 goto out_err;
1424         case RPC_PROG_MISMATCH:
1425                 dprintk("RPC: %5u %s: program %u, version %u unsupported by "
1426                                 "server %s\n", task->tk_pid, __FUNCTION__,
1427                                 (unsigned int)task->tk_client->cl_prog,
1428                                 (unsigned int)task->tk_client->cl_vers,
1429                                 task->tk_client->cl_server);
1430                 error = -EPROTONOSUPPORT;
1431                 goto out_err;
1432         case RPC_PROC_UNAVAIL:
1433                 dprintk("RPC: %5u %s: proc %p unsupported by program %u, "
1434                                 "version %u on server %s\n",
1435                                 task->tk_pid, __FUNCTION__,
1436                                 task->tk_msg.rpc_proc,
1437                                 task->tk_client->cl_prog,
1438                                 task->tk_client->cl_vers,
1439                                 task->tk_client->cl_server);
1440                 error = -EOPNOTSUPP;
1441                 goto out_err;
1442         case RPC_GARBAGE_ARGS:
1443                 dprintk("RPC: %5u %s: server saw garbage\n",
1444                                 task->tk_pid, __FUNCTION__);
1445                 break;                  /* retry */
1446         default:
1447                 dprintk("RPC: %5u %s: server accept status: %x\n",
1448                                 task->tk_pid, __FUNCTION__, n);
1449                 /* Also retry */
1450         }
1451
1452 out_garbage:
1453         task->tk_client->cl_stats->rpcgarbage++;
1454         if (task->tk_garb_retry) {
1455                 task->tk_garb_retry--;
1456                 dprintk("RPC: %5u %s: retrying\n",
1457                                 task->tk_pid, __FUNCTION__);
1458                 task->tk_action = call_bind;
1459 out_retry:
1460                 return ERR_PTR(-EAGAIN);
1461         }
1462 out_eio:
1463         error = -EIO;
1464 out_err:
1465         rpc_exit(task, error);
1466         dprintk("RPC: %5u %s: call failed with error %d\n", task->tk_pid,
1467                         __FUNCTION__, error);
1468         return ERR_PTR(error);
1469 out_overflow:
1470         dprintk("RPC: %5u %s: server reply was truncated.\n", task->tk_pid,
1471                         __FUNCTION__);
1472         goto out_garbage;
1473 }
1474
1475 static int rpcproc_encode_null(void *rqstp, __be32 *data, void *obj)
1476 {
1477         return 0;
1478 }
1479
1480 static int rpcproc_decode_null(void *rqstp, __be32 *data, void *obj)
1481 {
1482         return 0;
1483 }
1484
1485 static struct rpc_procinfo rpcproc_null = {
1486         .p_encode = rpcproc_encode_null,
1487         .p_decode = rpcproc_decode_null,
1488 };
1489
1490 static int rpc_ping(struct rpc_clnt *clnt, int flags)
1491 {
1492         struct rpc_message msg = {
1493                 .rpc_proc = &rpcproc_null,
1494         };
1495         int err;
1496         msg.rpc_cred = authnull_ops.lookup_cred(NULL, NULL, 0);
1497         err = rpc_call_sync(clnt, &msg, flags);
1498         put_rpccred(msg.rpc_cred);
1499         return err;
1500 }
1501
1502 struct rpc_task *rpc_call_null(struct rpc_clnt *clnt, struct rpc_cred *cred, int flags)
1503 {
1504         struct rpc_message msg = {
1505                 .rpc_proc = &rpcproc_null,
1506                 .rpc_cred = cred,
1507         };
1508         return rpc_do_run_task(clnt, &msg, flags, &rpc_default_ops, NULL);
1509 }
1510 EXPORT_SYMBOL(rpc_call_null);
1511
1512 #ifdef RPC_DEBUG
1513 void rpc_show_tasks(void)
1514 {
1515         struct rpc_clnt *clnt;
1516         struct rpc_task *t;
1517
1518         spin_lock(&rpc_client_lock);
1519         if (list_empty(&all_clients))
1520                 goto out;
1521         printk("-pid- proc flgs status -client- -prog- --rqstp- -timeout "
1522                 "-rpcwait -action- ---ops--\n");
1523         list_for_each_entry(clnt, &all_clients, cl_clients) {
1524                 if (list_empty(&clnt->cl_tasks))
1525                         continue;
1526                 spin_lock(&clnt->cl_lock);
1527                 list_for_each_entry(t, &clnt->cl_tasks, tk_task) {
1528                         const char *rpc_waitq = "none";
1529
1530                         if (RPC_IS_QUEUED(t))
1531                                 rpc_waitq = rpc_qname(t->u.tk_wait.rpc_waitq);
1532
1533                         printk("%5u %04d %04x %6d %8p %6d %8p %8ld %8s %8p %8p\n",
1534                                 t->tk_pid,
1535                                 (t->tk_msg.rpc_proc ? t->tk_msg.rpc_proc->p_proc : -1),
1536                                 t->tk_flags, t->tk_status,
1537                                 t->tk_client,
1538                                 (t->tk_client ? t->tk_client->cl_prog : 0),
1539                                 t->tk_rqstp, t->tk_timeout,
1540                                 rpc_waitq,
1541                                 t->tk_action, t->tk_ops);
1542                 }
1543                 spin_unlock(&clnt->cl_lock);
1544         }
1545 out:
1546         spin_unlock(&rpc_client_lock);
1547 }
1548 #endif