SUNRPC: Cleanup of rpc_task initialisation
[linux-2.6.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/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 xprt_create xprtargs = {
244                 .ident = 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 EXPORT_SYMBOL_GPL(rpc_clone_client);
349
350 /*
351  * Properly shut down an RPC client, terminating all outstanding
352  * requests.
353  */
354 void rpc_shutdown_client(struct rpc_clnt *clnt)
355 {
356         dprintk("RPC:       shutting down %s client for %s\n",
357                         clnt->cl_protname, clnt->cl_server);
358
359         while (!list_empty(&clnt->cl_tasks)) {
360                 rpc_killall_tasks(clnt);
361                 wait_event_timeout(destroy_wait,
362                         list_empty(&clnt->cl_tasks), 1*HZ);
363         }
364
365         rpc_release_client(clnt);
366 }
367 EXPORT_SYMBOL_GPL(rpc_shutdown_client);
368
369 /*
370  * Free an RPC client
371  */
372 static void
373 rpc_free_client(struct kref *kref)
374 {
375         struct rpc_clnt *clnt = container_of(kref, struct rpc_clnt, cl_kref);
376
377         dprintk("RPC:       destroying %s client for %s\n",
378                         clnt->cl_protname, clnt->cl_server);
379         if (!IS_ERR(clnt->cl_dentry)) {
380                 rpc_rmdir(clnt->cl_dentry);
381                 rpc_put_mount();
382         }
383         if (clnt->cl_parent != clnt) {
384                 rpc_release_client(clnt->cl_parent);
385                 goto out_free;
386         }
387         if (clnt->cl_server != clnt->cl_inline_name)
388                 kfree(clnt->cl_server);
389 out_free:
390         rpc_unregister_client(clnt);
391         rpc_free_iostats(clnt->cl_metrics);
392         clnt->cl_metrics = NULL;
393         xprt_put(clnt->cl_xprt);
394         rpciod_down();
395         kfree(clnt);
396 }
397
398 /*
399  * Free an RPC client
400  */
401 static void
402 rpc_free_auth(struct kref *kref)
403 {
404         struct rpc_clnt *clnt = container_of(kref, struct rpc_clnt, cl_kref);
405
406         if (clnt->cl_auth == NULL) {
407                 rpc_free_client(kref);
408                 return;
409         }
410
411         /*
412          * Note: RPCSEC_GSS may need to send NULL RPC calls in order to
413          *       release remaining GSS contexts. This mechanism ensures
414          *       that it can do so safely.
415          */
416         kref_init(kref);
417         rpcauth_release(clnt->cl_auth);
418         clnt->cl_auth = NULL;
419         kref_put(kref, rpc_free_client);
420 }
421
422 /*
423  * Release reference to the RPC client
424  */
425 void
426 rpc_release_client(struct rpc_clnt *clnt)
427 {
428         dprintk("RPC:       rpc_release_client(%p)\n", clnt);
429
430         if (list_empty(&clnt->cl_tasks))
431                 wake_up(&destroy_wait);
432         kref_put(&clnt->cl_kref, rpc_free_auth);
433 }
434
435 /**
436  * rpc_bind_new_program - bind a new RPC program to an existing client
437  * @old - old rpc_client
438  * @program - rpc program to set
439  * @vers - rpc program version
440  *
441  * Clones the rpc client and sets up a new RPC program. This is mainly
442  * of use for enabling different RPC programs to share the same transport.
443  * The Sun NFSv2/v3 ACL protocol can do this.
444  */
445 struct rpc_clnt *rpc_bind_new_program(struct rpc_clnt *old,
446                                       struct rpc_program *program,
447                                       u32 vers)
448 {
449         struct rpc_clnt *clnt;
450         struct rpc_version *version;
451         int err;
452
453         BUG_ON(vers >= program->nrvers || !program->version[vers]);
454         version = program->version[vers];
455         clnt = rpc_clone_client(old);
456         if (IS_ERR(clnt))
457                 goto out;
458         clnt->cl_procinfo = version->procs;
459         clnt->cl_maxproc  = version->nrprocs;
460         clnt->cl_protname = program->name;
461         clnt->cl_prog     = program->number;
462         clnt->cl_vers     = version->number;
463         clnt->cl_stats    = program->stats;
464         err = rpc_ping(clnt, RPC_TASK_SOFT|RPC_TASK_NOINTR);
465         if (err != 0) {
466                 rpc_shutdown_client(clnt);
467                 clnt = ERR_PTR(err);
468         }
469 out:
470         return clnt;
471 }
472 EXPORT_SYMBOL_GPL(rpc_bind_new_program);
473
474 /*
475  * Default callback for async RPC calls
476  */
477 static void
478 rpc_default_callback(struct rpc_task *task, void *data)
479 {
480 }
481
482 static const struct rpc_call_ops rpc_default_ops = {
483         .rpc_call_done = rpc_default_callback,
484 };
485
486 /*
487  *      Export the signal mask handling for synchronous code that
488  *      sleeps on RPC calls
489  */
490 #define RPC_INTR_SIGNALS (sigmask(SIGHUP) | sigmask(SIGINT) | sigmask(SIGQUIT) | sigmask(SIGTERM))
491
492 static void rpc_save_sigmask(sigset_t *oldset, int intr)
493 {
494         unsigned long   sigallow = sigmask(SIGKILL);
495         sigset_t sigmask;
496
497         /* Block all signals except those listed in sigallow */
498         if (intr)
499                 sigallow |= RPC_INTR_SIGNALS;
500         siginitsetinv(&sigmask, sigallow);
501         sigprocmask(SIG_BLOCK, &sigmask, oldset);
502 }
503
504 static inline void rpc_task_sigmask(struct rpc_task *task, sigset_t *oldset)
505 {
506         rpc_save_sigmask(oldset, !RPC_TASK_UNINTERRUPTIBLE(task));
507 }
508
509 static inline void rpc_restore_sigmask(sigset_t *oldset)
510 {
511         sigprocmask(SIG_SETMASK, oldset, NULL);
512 }
513
514 void rpc_clnt_sigmask(struct rpc_clnt *clnt, sigset_t *oldset)
515 {
516         rpc_save_sigmask(oldset, clnt->cl_intr);
517 }
518 EXPORT_SYMBOL_GPL(rpc_clnt_sigmask);
519
520 void rpc_clnt_sigunmask(struct rpc_clnt *clnt, sigset_t *oldset)
521 {
522         rpc_restore_sigmask(oldset);
523 }
524 EXPORT_SYMBOL_GPL(rpc_clnt_sigunmask);
525
526 static
527 struct rpc_task *rpc_do_run_task(const struct rpc_task_setup *task_setup_data)
528 {
529         struct rpc_task *task, *ret;
530         sigset_t oldset;
531
532         task = rpc_new_task(task_setup_data);
533         if (task == NULL) {
534                 rpc_release_calldata(task_setup_data->callback_ops,
535                                 task_setup_data->callback_data);
536                 return ERR_PTR(-ENOMEM);
537         }
538
539         /* Mask signals on synchronous RPC calls and RPCSEC_GSS upcalls */
540         rpc_task_sigmask(task, &oldset);
541         if (task_setup_data->rpc_message != NULL) {
542                 rpc_call_setup(task, task_setup_data->rpc_message, 0);
543                 if (task->tk_status != 0) {
544                         ret = ERR_PTR(task->tk_status);
545                         rpc_put_task(task);
546                         goto out;
547                 }
548         }
549         atomic_inc(&task->tk_count);
550         rpc_execute(task);
551         ret = task;
552 out:
553         rpc_restore_sigmask(&oldset);
554         return ret;
555 }
556
557 /**
558  * rpc_call_sync - Perform a synchronous RPC call
559  * @clnt: pointer to RPC client
560  * @msg: RPC call parameters
561  * @flags: RPC call flags
562  */
563 int rpc_call_sync(struct rpc_clnt *clnt, struct rpc_message *msg, int flags)
564 {
565         struct rpc_task *task;
566         struct rpc_task_setup task_setup_data = {
567                 .rpc_client = clnt,
568                 .rpc_message = msg,
569                 .callback_ops = &rpc_default_ops,
570                 .flags = flags,
571         };
572         int status;
573
574         BUG_ON(flags & RPC_TASK_ASYNC);
575
576         task = rpc_do_run_task(&task_setup_data);
577         if (IS_ERR(task))
578                 return PTR_ERR(task);
579         status = task->tk_status;
580         rpc_put_task(task);
581         return status;
582 }
583 EXPORT_SYMBOL_GPL(rpc_call_sync);
584
585 /**
586  * rpc_call_async - Perform an asynchronous RPC call
587  * @clnt: pointer to RPC client
588  * @msg: RPC call parameters
589  * @flags: RPC call flags
590  * @ops: RPC call ops
591  * @data: user call data
592  */
593 int
594 rpc_call_async(struct rpc_clnt *clnt, struct rpc_message *msg, int flags,
595                const struct rpc_call_ops *tk_ops, void *data)
596 {
597         struct rpc_task *task;
598         struct rpc_task_setup task_setup_data = {
599                 .rpc_client = clnt,
600                 .rpc_message = msg,
601                 .callback_ops = tk_ops,
602                 .callback_data = data,
603                 .flags = flags|RPC_TASK_ASYNC,
604         };
605
606         task = rpc_do_run_task(&task_setup_data);
607         if (IS_ERR(task))
608                 return PTR_ERR(task);
609         rpc_put_task(task);
610         return 0;
611 }
612 EXPORT_SYMBOL_GPL(rpc_call_async);
613
614 /**
615  * rpc_run_task - Allocate a new RPC task, then run rpc_execute against it
616  * @clnt: pointer to RPC client
617  * @flags: RPC flags
618  * @ops: RPC call ops
619  * @data: user call data
620  */
621 struct rpc_task *rpc_run_task(struct rpc_clnt *clnt, int flags,
622                                         const struct rpc_call_ops *tk_ops,
623                                         void *data)
624 {
625         struct rpc_task_setup task_setup_data = {
626                 .rpc_client = clnt,
627                 .callback_ops = tk_ops,
628                 .callback_data = data,
629                 .flags = flags,
630         };
631
632         return rpc_do_run_task(&task_setup_data);
633 }
634 EXPORT_SYMBOL_GPL(rpc_run_task);
635
636 void
637 rpc_call_setup(struct rpc_task *task, const struct rpc_message *msg, int flags)
638 {
639         task->tk_msg   = *msg;
640         task->tk_flags |= flags;
641         /* Bind the user cred */
642         if (task->tk_msg.rpc_cred != NULL)
643                 rpcauth_holdcred(task);
644         else
645                 rpcauth_bindcred(task);
646
647         if (task->tk_status == 0)
648                 task->tk_action = call_start;
649         else
650                 task->tk_action = rpc_exit_task;
651 }
652 EXPORT_SYMBOL_GPL(rpc_call_setup);
653
654 /**
655  * rpc_peeraddr - extract remote peer address from clnt's xprt
656  * @clnt: RPC client structure
657  * @buf: target buffer
658  * @size: length of target buffer
659  *
660  * Returns the number of bytes that are actually in the stored address.
661  */
662 size_t rpc_peeraddr(struct rpc_clnt *clnt, struct sockaddr *buf, size_t bufsize)
663 {
664         size_t bytes;
665         struct rpc_xprt *xprt = clnt->cl_xprt;
666
667         bytes = sizeof(xprt->addr);
668         if (bytes > bufsize)
669                 bytes = bufsize;
670         memcpy(buf, &clnt->cl_xprt->addr, bytes);
671         return xprt->addrlen;
672 }
673 EXPORT_SYMBOL_GPL(rpc_peeraddr);
674
675 /**
676  * rpc_peeraddr2str - return remote peer address in printable format
677  * @clnt: RPC client structure
678  * @format: address format
679  *
680  */
681 char *rpc_peeraddr2str(struct rpc_clnt *clnt, enum rpc_display_format_t format)
682 {
683         struct rpc_xprt *xprt = clnt->cl_xprt;
684
685         if (xprt->address_strings[format] != NULL)
686                 return xprt->address_strings[format];
687         else
688                 return "unprintable";
689 }
690 EXPORT_SYMBOL_GPL(rpc_peeraddr2str);
691
692 void
693 rpc_setbufsize(struct rpc_clnt *clnt, unsigned int sndsize, unsigned int rcvsize)
694 {
695         struct rpc_xprt *xprt = clnt->cl_xprt;
696         if (xprt->ops->set_buffer_size)
697                 xprt->ops->set_buffer_size(xprt, sndsize, rcvsize);
698 }
699 EXPORT_SYMBOL_GPL(rpc_setbufsize);
700
701 /*
702  * Return size of largest payload RPC client can support, in bytes
703  *
704  * For stream transports, this is one RPC record fragment (see RFC
705  * 1831), as we don't support multi-record requests yet.  For datagram
706  * transports, this is the size of an IP packet minus the IP, UDP, and
707  * RPC header sizes.
708  */
709 size_t rpc_max_payload(struct rpc_clnt *clnt)
710 {
711         return clnt->cl_xprt->max_payload;
712 }
713 EXPORT_SYMBOL_GPL(rpc_max_payload);
714
715 /**
716  * rpc_force_rebind - force transport to check that remote port is unchanged
717  * @clnt: client to rebind
718  *
719  */
720 void rpc_force_rebind(struct rpc_clnt *clnt)
721 {
722         if (clnt->cl_autobind)
723                 xprt_clear_bound(clnt->cl_xprt);
724 }
725 EXPORT_SYMBOL_GPL(rpc_force_rebind);
726
727 /*
728  * Restart an (async) RPC call. Usually called from within the
729  * exit handler.
730  */
731 void
732 rpc_restart_call(struct rpc_task *task)
733 {
734         if (RPC_ASSASSINATED(task))
735                 return;
736
737         task->tk_action = call_start;
738 }
739 EXPORT_SYMBOL_GPL(rpc_restart_call);
740
741 /*
742  * 0.  Initial state
743  *
744  *     Other FSM states can be visited zero or more times, but
745  *     this state is visited exactly once for each RPC.
746  */
747 static void
748 call_start(struct rpc_task *task)
749 {
750         struct rpc_clnt *clnt = task->tk_client;
751
752         dprintk("RPC: %5u call_start %s%d proc %d (%s)\n", task->tk_pid,
753                         clnt->cl_protname, clnt->cl_vers,
754                         task->tk_msg.rpc_proc->p_proc,
755                         (RPC_IS_ASYNC(task) ? "async" : "sync"));
756
757         /* Increment call count */
758         task->tk_msg.rpc_proc->p_count++;
759         clnt->cl_stats->rpccnt++;
760         task->tk_action = call_reserve;
761 }
762
763 /*
764  * 1.   Reserve an RPC call slot
765  */
766 static void
767 call_reserve(struct rpc_task *task)
768 {
769         dprint_status(task);
770
771         if (!rpcauth_uptodatecred(task)) {
772                 task->tk_action = call_refresh;
773                 return;
774         }
775
776         task->tk_status  = 0;
777         task->tk_action  = call_reserveresult;
778         xprt_reserve(task);
779 }
780
781 /*
782  * 1b.  Grok the result of xprt_reserve()
783  */
784 static void
785 call_reserveresult(struct rpc_task *task)
786 {
787         int status = task->tk_status;
788
789         dprint_status(task);
790
791         /*
792          * After a call to xprt_reserve(), we must have either
793          * a request slot or else an error status.
794          */
795         task->tk_status = 0;
796         if (status >= 0) {
797                 if (task->tk_rqstp) {
798                         task->tk_action = call_allocate;
799                         return;
800                 }
801
802                 printk(KERN_ERR "%s: status=%d, but no request slot, exiting\n",
803                                 __FUNCTION__, status);
804                 rpc_exit(task, -EIO);
805                 return;
806         }
807
808         /*
809          * Even though there was an error, we may have acquired
810          * a request slot somehow.  Make sure not to leak it.
811          */
812         if (task->tk_rqstp) {
813                 printk(KERN_ERR "%s: status=%d, request allocated anyway\n",
814                                 __FUNCTION__, status);
815                 xprt_release(task);
816         }
817
818         switch (status) {
819         case -EAGAIN:   /* woken up; retry */
820                 task->tk_action = call_reserve;
821                 return;
822         case -EIO:      /* probably a shutdown */
823                 break;
824         default:
825                 printk(KERN_ERR "%s: unrecognized error %d, exiting\n",
826                                 __FUNCTION__, status);
827                 break;
828         }
829         rpc_exit(task, status);
830 }
831
832 /*
833  * 2.   Allocate the buffer. For details, see sched.c:rpc_malloc.
834  *      (Note: buffer memory is freed in xprt_release).
835  */
836 static void
837 call_allocate(struct rpc_task *task)
838 {
839         unsigned int slack = task->tk_msg.rpc_cred->cr_auth->au_cslack;
840         struct rpc_rqst *req = task->tk_rqstp;
841         struct rpc_xprt *xprt = task->tk_xprt;
842         struct rpc_procinfo *proc = task->tk_msg.rpc_proc;
843
844         dprint_status(task);
845
846         task->tk_status = 0;
847         task->tk_action = call_bind;
848
849         if (req->rq_buffer)
850                 return;
851
852         if (proc->p_proc != 0) {
853                 BUG_ON(proc->p_arglen == 0);
854                 if (proc->p_decode != NULL)
855                         BUG_ON(proc->p_replen == 0);
856         }
857
858         /*
859          * Calculate the size (in quads) of the RPC call
860          * and reply headers, and convert both values
861          * to byte sizes.
862          */
863         req->rq_callsize = RPC_CALLHDRSIZE + (slack << 1) + proc->p_arglen;
864         req->rq_callsize <<= 2;
865         req->rq_rcvsize = RPC_REPHDRSIZE + slack + proc->p_replen;
866         req->rq_rcvsize <<= 2;
867
868         req->rq_buffer = xprt->ops->buf_alloc(task,
869                                         req->rq_callsize + req->rq_rcvsize);
870         if (req->rq_buffer != NULL)
871                 return;
872
873         dprintk("RPC: %5u rpc_buffer allocation failed\n", task->tk_pid);
874
875         if (RPC_IS_ASYNC(task) || !signalled()) {
876                 task->tk_action = call_allocate;
877                 rpc_delay(task, HZ>>4);
878                 return;
879         }
880
881         rpc_exit(task, -ERESTARTSYS);
882 }
883
884 static inline int
885 rpc_task_need_encode(struct rpc_task *task)
886 {
887         return task->tk_rqstp->rq_snd_buf.len == 0;
888 }
889
890 static inline void
891 rpc_task_force_reencode(struct rpc_task *task)
892 {
893         task->tk_rqstp->rq_snd_buf.len = 0;
894 }
895
896 static inline void
897 rpc_xdr_buf_init(struct xdr_buf *buf, void *start, size_t len)
898 {
899         buf->head[0].iov_base = start;
900         buf->head[0].iov_len = len;
901         buf->tail[0].iov_len = 0;
902         buf->page_len = 0;
903         buf->flags = 0;
904         buf->len = 0;
905         buf->buflen = len;
906 }
907
908 /*
909  * 3.   Encode arguments of an RPC call
910  */
911 static void
912 call_encode(struct rpc_task *task)
913 {
914         struct rpc_rqst *req = task->tk_rqstp;
915         kxdrproc_t      encode;
916         __be32          *p;
917
918         dprint_status(task);
919
920         rpc_xdr_buf_init(&req->rq_snd_buf,
921                          req->rq_buffer,
922                          req->rq_callsize);
923         rpc_xdr_buf_init(&req->rq_rcv_buf,
924                          (char *)req->rq_buffer + req->rq_callsize,
925                          req->rq_rcvsize);
926
927         /* Encode header and provided arguments */
928         encode = task->tk_msg.rpc_proc->p_encode;
929         if (!(p = call_header(task))) {
930                 printk(KERN_INFO "RPC: call_header failed, exit EIO\n");
931                 rpc_exit(task, -EIO);
932                 return;
933         }
934         if (encode == NULL)
935                 return;
936
937         task->tk_status = rpcauth_wrap_req(task, encode, req, p,
938                         task->tk_msg.rpc_argp);
939         if (task->tk_status == -ENOMEM) {
940                 /* XXX: Is this sane? */
941                 rpc_delay(task, 3*HZ);
942                 task->tk_status = -EAGAIN;
943         }
944 }
945
946 /*
947  * 4.   Get the server port number if not yet set
948  */
949 static void
950 call_bind(struct rpc_task *task)
951 {
952         struct rpc_xprt *xprt = task->tk_xprt;
953
954         dprint_status(task);
955
956         task->tk_action = call_connect;
957         if (!xprt_bound(xprt)) {
958                 task->tk_action = call_bind_status;
959                 task->tk_timeout = xprt->bind_timeout;
960                 xprt->ops->rpcbind(task);
961         }
962 }
963
964 /*
965  * 4a.  Sort out bind result
966  */
967 static void
968 call_bind_status(struct rpc_task *task)
969 {
970         int status = -EIO;
971
972         if (task->tk_status >= 0) {
973                 dprint_status(task);
974                 task->tk_status = 0;
975                 task->tk_action = call_connect;
976                 return;
977         }
978
979         switch (task->tk_status) {
980         case -EAGAIN:
981                 dprintk("RPC: %5u rpcbind waiting for another request "
982                                 "to finish\n", task->tk_pid);
983                 /* avoid busy-waiting here -- could be a network outage. */
984                 rpc_delay(task, 5*HZ);
985                 goto retry_timeout;
986         case -EACCES:
987                 dprintk("RPC: %5u remote rpcbind: RPC program/version "
988                                 "unavailable\n", task->tk_pid);
989                 /* fail immediately if this is an RPC ping */
990                 if (task->tk_msg.rpc_proc->p_proc == 0) {
991                         status = -EOPNOTSUPP;
992                         break;
993                 }
994                 rpc_delay(task, 3*HZ);
995                 goto retry_timeout;
996         case -ETIMEDOUT:
997                 dprintk("RPC: %5u rpcbind request timed out\n",
998                                 task->tk_pid);
999                 goto retry_timeout;
1000         case -EPFNOSUPPORT:
1001                 /* server doesn't support any rpcbind version we know of */
1002                 dprintk("RPC: %5u remote rpcbind service unavailable\n",
1003                                 task->tk_pid);
1004                 break;
1005         case -EPROTONOSUPPORT:
1006                 dprintk("RPC: %5u remote rpcbind version unavailable, retrying\n",
1007                                 task->tk_pid);
1008                 task->tk_status = 0;
1009                 task->tk_action = call_bind;
1010                 return;
1011         default:
1012                 dprintk("RPC: %5u unrecognized rpcbind error (%d)\n",
1013                                 task->tk_pid, -task->tk_status);
1014         }
1015
1016         rpc_exit(task, status);
1017         return;
1018
1019 retry_timeout:
1020         task->tk_action = call_timeout;
1021 }
1022
1023 /*
1024  * 4b.  Connect to the RPC server
1025  */
1026 static void
1027 call_connect(struct rpc_task *task)
1028 {
1029         struct rpc_xprt *xprt = task->tk_xprt;
1030
1031         dprintk("RPC: %5u call_connect xprt %p %s connected\n",
1032                         task->tk_pid, xprt,
1033                         (xprt_connected(xprt) ? "is" : "is not"));
1034
1035         task->tk_action = call_transmit;
1036         if (!xprt_connected(xprt)) {
1037                 task->tk_action = call_connect_status;
1038                 if (task->tk_status < 0)
1039                         return;
1040                 xprt_connect(task);
1041         }
1042 }
1043
1044 /*
1045  * 4c.  Sort out connect result
1046  */
1047 static void
1048 call_connect_status(struct rpc_task *task)
1049 {
1050         struct rpc_clnt *clnt = task->tk_client;
1051         int status = task->tk_status;
1052
1053         dprint_status(task);
1054
1055         task->tk_status = 0;
1056         if (status >= 0) {
1057                 clnt->cl_stats->netreconn++;
1058                 task->tk_action = call_transmit;
1059                 return;
1060         }
1061
1062         /* Something failed: remote service port may have changed */
1063         rpc_force_rebind(clnt);
1064
1065         switch (status) {
1066         case -ENOTCONN:
1067         case -EAGAIN:
1068                 task->tk_action = call_bind;
1069                 if (!RPC_IS_SOFT(task))
1070                         return;
1071                 /* if soft mounted, test if we've timed out */
1072         case -ETIMEDOUT:
1073                 task->tk_action = call_timeout;
1074                 return;
1075         }
1076         rpc_exit(task, -EIO);
1077 }
1078
1079 /*
1080  * 5.   Transmit the RPC request, and wait for reply
1081  */
1082 static void
1083 call_transmit(struct rpc_task *task)
1084 {
1085         dprint_status(task);
1086
1087         task->tk_action = call_status;
1088         if (task->tk_status < 0)
1089                 return;
1090         task->tk_status = xprt_prepare_transmit(task);
1091         if (task->tk_status != 0)
1092                 return;
1093         task->tk_action = call_transmit_status;
1094         /* Encode here so that rpcsec_gss can use correct sequence number. */
1095         if (rpc_task_need_encode(task)) {
1096                 BUG_ON(task->tk_rqstp->rq_bytes_sent != 0);
1097                 call_encode(task);
1098                 /* Did the encode result in an error condition? */
1099                 if (task->tk_status != 0)
1100                         return;
1101         }
1102         xprt_transmit(task);
1103         if (task->tk_status < 0)
1104                 return;
1105         /*
1106          * On success, ensure that we call xprt_end_transmit() before sleeping
1107          * in order to allow access to the socket to other RPC requests.
1108          */
1109         call_transmit_status(task);
1110         if (task->tk_msg.rpc_proc->p_decode != NULL)
1111                 return;
1112         task->tk_action = rpc_exit_task;
1113         rpc_wake_up_task(task);
1114 }
1115
1116 /*
1117  * 5a.  Handle cleanup after a transmission
1118  */
1119 static void
1120 call_transmit_status(struct rpc_task *task)
1121 {
1122         task->tk_action = call_status;
1123         /*
1124          * Special case: if we've been waiting on the socket's write_space()
1125          * callback, then don't call xprt_end_transmit().
1126          */
1127         if (task->tk_status == -EAGAIN)
1128                 return;
1129         xprt_end_transmit(task);
1130         rpc_task_force_reencode(task);
1131 }
1132
1133 /*
1134  * 6.   Sort out the RPC call status
1135  */
1136 static void
1137 call_status(struct rpc_task *task)
1138 {
1139         struct rpc_clnt *clnt = task->tk_client;
1140         struct rpc_rqst *req = task->tk_rqstp;
1141         int             status;
1142
1143         if (req->rq_received > 0 && !req->rq_bytes_sent)
1144                 task->tk_status = req->rq_received;
1145
1146         dprint_status(task);
1147
1148         status = task->tk_status;
1149         if (status >= 0) {
1150                 task->tk_action = call_decode;
1151                 return;
1152         }
1153
1154         task->tk_status = 0;
1155         switch(status) {
1156         case -EHOSTDOWN:
1157         case -EHOSTUNREACH:
1158         case -ENETUNREACH:
1159                 /*
1160                  * Delay any retries for 3 seconds, then handle as if it
1161                  * were a timeout.
1162                  */
1163                 rpc_delay(task, 3*HZ);
1164         case -ETIMEDOUT:
1165                 task->tk_action = call_timeout;
1166                 if (task->tk_client->cl_discrtry)
1167                         xprt_force_disconnect(task->tk_xprt);
1168                 break;
1169         case -ECONNREFUSED:
1170         case -ENOTCONN:
1171                 rpc_force_rebind(clnt);
1172                 task->tk_action = call_bind;
1173                 break;
1174         case -EAGAIN:
1175                 task->tk_action = call_transmit;
1176                 break;
1177         case -EIO:
1178                 /* shutdown or soft timeout */
1179                 rpc_exit(task, status);
1180                 break;
1181         default:
1182                 printk("%s: RPC call returned error %d\n",
1183                                clnt->cl_protname, -status);
1184                 rpc_exit(task, status);
1185         }
1186 }
1187
1188 /*
1189  * 6a.  Handle RPC timeout
1190  *      We do not release the request slot, so we keep using the
1191  *      same XID for all retransmits.
1192  */
1193 static void
1194 call_timeout(struct rpc_task *task)
1195 {
1196         struct rpc_clnt *clnt = task->tk_client;
1197
1198         if (xprt_adjust_timeout(task->tk_rqstp) == 0) {
1199                 dprintk("RPC: %5u call_timeout (minor)\n", task->tk_pid);
1200                 goto retry;
1201         }
1202
1203         dprintk("RPC: %5u call_timeout (major)\n", task->tk_pid);
1204         task->tk_timeouts++;
1205
1206         if (RPC_IS_SOFT(task)) {
1207                 printk(KERN_NOTICE "%s: server %s not responding, timed out\n",
1208                                 clnt->cl_protname, clnt->cl_server);
1209                 rpc_exit(task, -EIO);
1210                 return;
1211         }
1212
1213         if (!(task->tk_flags & RPC_CALL_MAJORSEEN)) {
1214                 task->tk_flags |= RPC_CALL_MAJORSEEN;
1215                 printk(KERN_NOTICE "%s: server %s not responding, still trying\n",
1216                         clnt->cl_protname, clnt->cl_server);
1217         }
1218         rpc_force_rebind(clnt);
1219
1220 retry:
1221         clnt->cl_stats->rpcretrans++;
1222         task->tk_action = call_bind;
1223         task->tk_status = 0;
1224 }
1225
1226 /*
1227  * 7.   Decode the RPC reply
1228  */
1229 static void
1230 call_decode(struct rpc_task *task)
1231 {
1232         struct rpc_clnt *clnt = task->tk_client;
1233         struct rpc_rqst *req = task->tk_rqstp;
1234         kxdrproc_t      decode = task->tk_msg.rpc_proc->p_decode;
1235         __be32          *p;
1236
1237         dprintk("RPC: %5u call_decode (status %d)\n",
1238                         task->tk_pid, task->tk_status);
1239
1240         if (task->tk_flags & RPC_CALL_MAJORSEEN) {
1241                 printk(KERN_NOTICE "%s: server %s OK\n",
1242                         clnt->cl_protname, clnt->cl_server);
1243                 task->tk_flags &= ~RPC_CALL_MAJORSEEN;
1244         }
1245
1246         if (task->tk_status < 12) {
1247                 if (!RPC_IS_SOFT(task)) {
1248                         task->tk_action = call_bind;
1249                         clnt->cl_stats->rpcretrans++;
1250                         goto out_retry;
1251                 }
1252                 dprintk("RPC:       %s: too small RPC reply size (%d bytes)\n",
1253                                 clnt->cl_protname, task->tk_status);
1254                 task->tk_action = call_timeout;
1255                 goto out_retry;
1256         }
1257
1258         /*
1259          * Ensure that we see all writes made by xprt_complete_rqst()
1260          * before it changed req->rq_received.
1261          */
1262         smp_rmb();
1263         req->rq_rcv_buf.len = req->rq_private_buf.len;
1264
1265         /* Check that the softirq receive buffer is valid */
1266         WARN_ON(memcmp(&req->rq_rcv_buf, &req->rq_private_buf,
1267                                 sizeof(req->rq_rcv_buf)) != 0);
1268
1269         /* Verify the RPC header */
1270         p = call_verify(task);
1271         if (IS_ERR(p)) {
1272                 if (p == ERR_PTR(-EAGAIN))
1273                         goto out_retry;
1274                 return;
1275         }
1276
1277         task->tk_action = rpc_exit_task;
1278
1279         if (decode) {
1280                 task->tk_status = rpcauth_unwrap_resp(task, decode, req, p,
1281                                                       task->tk_msg.rpc_resp);
1282         }
1283         dprintk("RPC: %5u call_decode result %d\n", task->tk_pid,
1284                         task->tk_status);
1285         return;
1286 out_retry:
1287         req->rq_received = req->rq_private_buf.len = 0;
1288         task->tk_status = 0;
1289         if (task->tk_client->cl_discrtry)
1290                 xprt_force_disconnect(task->tk_xprt);
1291 }
1292
1293 /*
1294  * 8.   Refresh the credentials if rejected by the server
1295  */
1296 static void
1297 call_refresh(struct rpc_task *task)
1298 {
1299         dprint_status(task);
1300
1301         task->tk_action = call_refreshresult;
1302         task->tk_status = 0;
1303         task->tk_client->cl_stats->rpcauthrefresh++;
1304         rpcauth_refreshcred(task);
1305 }
1306
1307 /*
1308  * 8a.  Process the results of a credential refresh
1309  */
1310 static void
1311 call_refreshresult(struct rpc_task *task)
1312 {
1313         int status = task->tk_status;
1314
1315         dprint_status(task);
1316
1317         task->tk_status = 0;
1318         task->tk_action = call_reserve;
1319         if (status >= 0 && rpcauth_uptodatecred(task))
1320                 return;
1321         if (status == -EACCES) {
1322                 rpc_exit(task, -EACCES);
1323                 return;
1324         }
1325         task->tk_action = call_refresh;
1326         if (status != -ETIMEDOUT)
1327                 rpc_delay(task, 3*HZ);
1328         return;
1329 }
1330
1331 /*
1332  * Call header serialization
1333  */
1334 static __be32 *
1335 call_header(struct rpc_task *task)
1336 {
1337         struct rpc_clnt *clnt = task->tk_client;
1338         struct rpc_rqst *req = task->tk_rqstp;
1339         __be32          *p = req->rq_svec[0].iov_base;
1340
1341         /* FIXME: check buffer size? */
1342
1343         p = xprt_skip_transport_header(task->tk_xprt, p);
1344         *p++ = req->rq_xid;             /* XID */
1345         *p++ = htonl(RPC_CALL);         /* CALL */
1346         *p++ = htonl(RPC_VERSION);      /* RPC version */
1347         *p++ = htonl(clnt->cl_prog);    /* program number */
1348         *p++ = htonl(clnt->cl_vers);    /* program version */
1349         *p++ = htonl(task->tk_msg.rpc_proc->p_proc);    /* procedure */
1350         p = rpcauth_marshcred(task, p);
1351         req->rq_slen = xdr_adjust_iovec(&req->rq_svec[0], p);
1352         return p;
1353 }
1354
1355 /*
1356  * Reply header verification
1357  */
1358 static __be32 *
1359 call_verify(struct rpc_task *task)
1360 {
1361         struct kvec *iov = &task->tk_rqstp->rq_rcv_buf.head[0];
1362         int len = task->tk_rqstp->rq_rcv_buf.len >> 2;
1363         __be32  *p = iov->iov_base;
1364         u32 n;
1365         int error = -EACCES;
1366
1367         if ((task->tk_rqstp->rq_rcv_buf.len & 3) != 0) {
1368                 /* RFC-1014 says that the representation of XDR data must be a
1369                  * multiple of four bytes
1370                  * - if it isn't pointer subtraction in the NFS client may give
1371                  *   undefined results
1372                  */
1373                 dprintk("RPC: %5u %s: XDR representation not a multiple of"
1374                        " 4 bytes: 0x%x\n", task->tk_pid, __FUNCTION__,
1375                        task->tk_rqstp->rq_rcv_buf.len);
1376                 goto out_eio;
1377         }
1378         if ((len -= 3) < 0)
1379                 goto out_overflow;
1380         p += 1; /* skip XID */
1381
1382         if ((n = ntohl(*p++)) != RPC_REPLY) {
1383                 dprintk("RPC: %5u %s: not an RPC reply: %x\n",
1384                                 task->tk_pid, __FUNCTION__, n);
1385                 goto out_garbage;
1386         }
1387         if ((n = ntohl(*p++)) != RPC_MSG_ACCEPTED) {
1388                 if (--len < 0)
1389                         goto out_overflow;
1390                 switch ((n = ntohl(*p++))) {
1391                         case RPC_AUTH_ERROR:
1392                                 break;
1393                         case RPC_MISMATCH:
1394                                 dprintk("RPC: %5u %s: RPC call version "
1395                                                 "mismatch!\n",
1396                                                 task->tk_pid, __FUNCTION__);
1397                                 error = -EPROTONOSUPPORT;
1398                                 goto out_err;
1399                         default:
1400                                 dprintk("RPC: %5u %s: RPC call rejected, "
1401                                                 "unknown error: %x\n",
1402                                                 task->tk_pid, __FUNCTION__, n);
1403                                 goto out_eio;
1404                 }
1405                 if (--len < 0)
1406                         goto out_overflow;
1407                 switch ((n = ntohl(*p++))) {
1408                 case RPC_AUTH_REJECTEDCRED:
1409                 case RPC_AUTH_REJECTEDVERF:
1410                 case RPCSEC_GSS_CREDPROBLEM:
1411                 case RPCSEC_GSS_CTXPROBLEM:
1412                         if (!task->tk_cred_retry)
1413                                 break;
1414                         task->tk_cred_retry--;
1415                         dprintk("RPC: %5u %s: retry stale creds\n",
1416                                         task->tk_pid, __FUNCTION__);
1417                         rpcauth_invalcred(task);
1418                         /* Ensure we obtain a new XID! */
1419                         xprt_release(task);
1420                         task->tk_action = call_refresh;
1421                         goto out_retry;
1422                 case RPC_AUTH_BADCRED:
1423                 case RPC_AUTH_BADVERF:
1424                         /* possibly garbled cred/verf? */
1425                         if (!task->tk_garb_retry)
1426                                 break;
1427                         task->tk_garb_retry--;
1428                         dprintk("RPC: %5u %s: retry garbled creds\n",
1429                                         task->tk_pid, __FUNCTION__);
1430                         task->tk_action = call_bind;
1431                         goto out_retry;
1432                 case RPC_AUTH_TOOWEAK:
1433                         printk(KERN_NOTICE "call_verify: server %s requires stronger "
1434                                "authentication.\n", task->tk_client->cl_server);
1435                         break;
1436                 default:
1437                         dprintk("RPC: %5u %s: unknown auth error: %x\n",
1438                                         task->tk_pid, __FUNCTION__, n);
1439                         error = -EIO;
1440                 }
1441                 dprintk("RPC: %5u %s: call rejected %d\n",
1442                                 task->tk_pid, __FUNCTION__, n);
1443                 goto out_err;
1444         }
1445         if (!(p = rpcauth_checkverf(task, p))) {
1446                 dprintk("RPC: %5u %s: auth check failed\n",
1447                                 task->tk_pid, __FUNCTION__);
1448                 goto out_garbage;               /* bad verifier, retry */
1449         }
1450         len = p - (__be32 *)iov->iov_base - 1;
1451         if (len < 0)
1452                 goto out_overflow;
1453         switch ((n = ntohl(*p++))) {
1454         case RPC_SUCCESS:
1455                 return p;
1456         case RPC_PROG_UNAVAIL:
1457                 dprintk("RPC: %5u %s: program %u is unsupported by server %s\n",
1458                                 task->tk_pid, __FUNCTION__,
1459                                 (unsigned int)task->tk_client->cl_prog,
1460                                 task->tk_client->cl_server);
1461                 error = -EPFNOSUPPORT;
1462                 goto out_err;
1463         case RPC_PROG_MISMATCH:
1464                 dprintk("RPC: %5u %s: program %u, version %u unsupported by "
1465                                 "server %s\n", task->tk_pid, __FUNCTION__,
1466                                 (unsigned int)task->tk_client->cl_prog,
1467                                 (unsigned int)task->tk_client->cl_vers,
1468                                 task->tk_client->cl_server);
1469                 error = -EPROTONOSUPPORT;
1470                 goto out_err;
1471         case RPC_PROC_UNAVAIL:
1472                 dprintk("RPC: %5u %s: proc %p unsupported by program %u, "
1473                                 "version %u on server %s\n",
1474                                 task->tk_pid, __FUNCTION__,
1475                                 task->tk_msg.rpc_proc,
1476                                 task->tk_client->cl_prog,
1477                                 task->tk_client->cl_vers,
1478                                 task->tk_client->cl_server);
1479                 error = -EOPNOTSUPP;
1480                 goto out_err;
1481         case RPC_GARBAGE_ARGS:
1482                 dprintk("RPC: %5u %s: server saw garbage\n",
1483                                 task->tk_pid, __FUNCTION__);
1484                 break;                  /* retry */
1485         default:
1486                 dprintk("RPC: %5u %s: server accept status: %x\n",
1487                                 task->tk_pid, __FUNCTION__, n);
1488                 /* Also retry */
1489         }
1490
1491 out_garbage:
1492         task->tk_client->cl_stats->rpcgarbage++;
1493         if (task->tk_garb_retry) {
1494                 task->tk_garb_retry--;
1495                 dprintk("RPC: %5u %s: retrying\n",
1496                                 task->tk_pid, __FUNCTION__);
1497                 task->tk_action = call_bind;
1498 out_retry:
1499                 return ERR_PTR(-EAGAIN);
1500         }
1501 out_eio:
1502         error = -EIO;
1503 out_err:
1504         rpc_exit(task, error);
1505         dprintk("RPC: %5u %s: call failed with error %d\n", task->tk_pid,
1506                         __FUNCTION__, error);
1507         return ERR_PTR(error);
1508 out_overflow:
1509         dprintk("RPC: %5u %s: server reply was truncated.\n", task->tk_pid,
1510                         __FUNCTION__);
1511         goto out_garbage;
1512 }
1513
1514 static int rpcproc_encode_null(void *rqstp, __be32 *data, void *obj)
1515 {
1516         return 0;
1517 }
1518
1519 static int rpcproc_decode_null(void *rqstp, __be32 *data, void *obj)
1520 {
1521         return 0;
1522 }
1523
1524 static struct rpc_procinfo rpcproc_null = {
1525         .p_encode = rpcproc_encode_null,
1526         .p_decode = rpcproc_decode_null,
1527 };
1528
1529 static int rpc_ping(struct rpc_clnt *clnt, int flags)
1530 {
1531         struct rpc_message msg = {
1532                 .rpc_proc = &rpcproc_null,
1533         };
1534         int err;
1535         msg.rpc_cred = authnull_ops.lookup_cred(NULL, NULL, 0);
1536         err = rpc_call_sync(clnt, &msg, flags);
1537         put_rpccred(msg.rpc_cred);
1538         return err;
1539 }
1540
1541 struct rpc_task *rpc_call_null(struct rpc_clnt *clnt, struct rpc_cred *cred, int flags)
1542 {
1543         struct rpc_message msg = {
1544                 .rpc_proc = &rpcproc_null,
1545                 .rpc_cred = cred,
1546         };
1547         struct rpc_task_setup task_setup_data = {
1548                 .rpc_client = clnt,
1549                 .rpc_message = &msg,
1550                 .callback_ops = &rpc_default_ops,
1551                 .flags = flags,
1552         };
1553         return rpc_do_run_task(&task_setup_data);
1554 }
1555 EXPORT_SYMBOL_GPL(rpc_call_null);
1556
1557 #ifdef RPC_DEBUG
1558 void rpc_show_tasks(void)
1559 {
1560         struct rpc_clnt *clnt;
1561         struct rpc_task *t;
1562
1563         spin_lock(&rpc_client_lock);
1564         if (list_empty(&all_clients))
1565                 goto out;
1566         printk("-pid- proc flgs status -client- -prog- --rqstp- -timeout "
1567                 "-rpcwait -action- ---ops--\n");
1568         list_for_each_entry(clnt, &all_clients, cl_clients) {
1569                 if (list_empty(&clnt->cl_tasks))
1570                         continue;
1571                 spin_lock(&clnt->cl_lock);
1572                 list_for_each_entry(t, &clnt->cl_tasks, tk_task) {
1573                         const char *rpc_waitq = "none";
1574                         int proc;
1575
1576                         if (t->tk_msg.rpc_proc)
1577                                 proc = t->tk_msg.rpc_proc->p_proc;
1578                         else
1579                                 proc = -1;
1580
1581                         if (RPC_IS_QUEUED(t))
1582                                 rpc_waitq = rpc_qname(t->u.tk_wait.rpc_waitq);
1583
1584                         printk("%5u %04d %04x %6d %8p %6d %8p %8ld %8s %8p %8p\n",
1585                                 t->tk_pid, proc,
1586                                 t->tk_flags, t->tk_status,
1587                                 t->tk_client,
1588                                 (t->tk_client ? t->tk_client->cl_prog : 0),
1589                                 t->tk_rqstp, t->tk_timeout,
1590                                 rpc_waitq,
1591                                 t->tk_action, t->tk_ops);
1592                 }
1593                 spin_unlock(&clnt->cl_lock);
1594         }
1595 out:
1596         spin_unlock(&rpc_client_lock);
1597 }
1598 #endif