SUNRPC: rpcauth_create needs to know about rpc_clnt clone status
[pandora-kernel.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  *  Copyright (C) 1992,1993 Rick Sladkey <jrs@world.std.com>
17  *  Copyright (C) 1995,1996 Olaf Kirch <okir@monad.swb.de>
18  */
19
20
21 #include <linux/module.h>
22 #include <linux/types.h>
23 #include <linux/kallsyms.h>
24 #include <linux/mm.h>
25 #include <linux/namei.h>
26 #include <linux/mount.h>
27 #include <linux/slab.h>
28 #include <linux/utsname.h>
29 #include <linux/workqueue.h>
30 #include <linux/in.h>
31 #include <linux/in6.h>
32 #include <linux/un.h>
33 #include <linux/rcupdate.h>
34
35 #include <linux/sunrpc/clnt.h>
36 #include <linux/sunrpc/addr.h>
37 #include <linux/sunrpc/rpc_pipe_fs.h>
38 #include <linux/sunrpc/metrics.h>
39 #include <linux/sunrpc/bc_xprt.h>
40 #include <trace/events/sunrpc.h>
41
42 #include "sunrpc.h"
43 #include "netns.h"
44
45 #ifdef RPC_DEBUG
46 # define RPCDBG_FACILITY        RPCDBG_CALL
47 #endif
48
49 #define dprint_status(t)                                        \
50         dprintk("RPC: %5u %s (status %d)\n", t->tk_pid,         \
51                         __func__, t->tk_status)
52
53 /*
54  * All RPC clients are linked into this list
55  */
56
57 static DECLARE_WAIT_QUEUE_HEAD(destroy_wait);
58
59
60 static void     call_start(struct rpc_task *task);
61 static void     call_reserve(struct rpc_task *task);
62 static void     call_reserveresult(struct rpc_task *task);
63 static void     call_allocate(struct rpc_task *task);
64 static void     call_decode(struct rpc_task *task);
65 static void     call_bind(struct rpc_task *task);
66 static void     call_bind_status(struct rpc_task *task);
67 static void     call_transmit(struct rpc_task *task);
68 #if defined(CONFIG_SUNRPC_BACKCHANNEL)
69 static void     call_bc_transmit(struct rpc_task *task);
70 #endif /* CONFIG_SUNRPC_BACKCHANNEL */
71 static void     call_status(struct rpc_task *task);
72 static void     call_transmit_status(struct rpc_task *task);
73 static void     call_refresh(struct rpc_task *task);
74 static void     call_refreshresult(struct rpc_task *task);
75 static void     call_timeout(struct rpc_task *task);
76 static void     call_connect(struct rpc_task *task);
77 static void     call_connect_status(struct rpc_task *task);
78
79 static __be32   *rpc_encode_header(struct rpc_task *task);
80 static __be32   *rpc_verify_header(struct rpc_task *task);
81 static int      rpc_ping(struct rpc_clnt *clnt);
82
83 static void rpc_register_client(struct rpc_clnt *clnt)
84 {
85         struct net *net = rpc_net_ns(clnt);
86         struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
87
88         spin_lock(&sn->rpc_client_lock);
89         list_add(&clnt->cl_clients, &sn->all_clients);
90         spin_unlock(&sn->rpc_client_lock);
91 }
92
93 static void rpc_unregister_client(struct rpc_clnt *clnt)
94 {
95         struct net *net = rpc_net_ns(clnt);
96         struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
97
98         spin_lock(&sn->rpc_client_lock);
99         list_del(&clnt->cl_clients);
100         spin_unlock(&sn->rpc_client_lock);
101 }
102
103 static void __rpc_clnt_remove_pipedir(struct rpc_clnt *clnt)
104 {
105         rpc_remove_client_dir(clnt);
106 }
107
108 static void rpc_clnt_remove_pipedir(struct rpc_clnt *clnt)
109 {
110         struct net *net = rpc_net_ns(clnt);
111         struct super_block *pipefs_sb;
112
113         pipefs_sb = rpc_get_sb_net(net);
114         if (pipefs_sb) {
115                 __rpc_clnt_remove_pipedir(clnt);
116                 rpc_put_sb_net(net);
117         }
118 }
119
120 static struct dentry *rpc_setup_pipedir_sb(struct super_block *sb,
121                                     struct rpc_clnt *clnt)
122 {
123         static uint32_t clntid;
124         const char *dir_name = clnt->cl_program->pipe_dir_name;
125         char name[15];
126         struct dentry *dir, *dentry;
127
128         dir = rpc_d_lookup_sb(sb, dir_name);
129         if (dir == NULL) {
130                 pr_info("RPC: pipefs directory doesn't exist: %s\n", dir_name);
131                 return dir;
132         }
133         for (;;) {
134                 snprintf(name, sizeof(name), "clnt%x", (unsigned int)clntid++);
135                 name[sizeof(name) - 1] = '\0';
136                 dentry = rpc_create_client_dir(dir, name, clnt);
137                 if (!IS_ERR(dentry))
138                         break;
139                 if (dentry == ERR_PTR(-EEXIST))
140                         continue;
141                 printk(KERN_INFO "RPC: Couldn't create pipefs entry"
142                                 " %s/%s, error %ld\n",
143                                 dir_name, name, PTR_ERR(dentry));
144                 break;
145         }
146         dput(dir);
147         return dentry;
148 }
149
150 static int
151 rpc_setup_pipedir(struct super_block *pipefs_sb, struct rpc_clnt *clnt)
152 {
153         struct dentry *dentry;
154
155         if (clnt->cl_program->pipe_dir_name != NULL) {
156                 dentry = rpc_setup_pipedir_sb(pipefs_sb, clnt);
157                 if (IS_ERR(dentry))
158                         return PTR_ERR(dentry);
159         }
160         return 0;
161 }
162
163 static int rpc_clnt_skip_event(struct rpc_clnt *clnt, unsigned long event)
164 {
165         if (clnt->cl_program->pipe_dir_name == NULL)
166                 return 1;
167
168         switch (event) {
169         case RPC_PIPEFS_MOUNT:
170                 if (clnt->cl_pipedir_objects.pdh_dentry != NULL)
171                         return 1;
172                 if (atomic_read(&clnt->cl_count) == 0)
173                         return 1;
174                 break;
175         case RPC_PIPEFS_UMOUNT:
176                 if (clnt->cl_pipedir_objects.pdh_dentry == NULL)
177                         return 1;
178                 break;
179         }
180         return 0;
181 }
182
183 static int __rpc_clnt_handle_event(struct rpc_clnt *clnt, unsigned long event,
184                                    struct super_block *sb)
185 {
186         struct dentry *dentry;
187         int err = 0;
188
189         switch (event) {
190         case RPC_PIPEFS_MOUNT:
191                 dentry = rpc_setup_pipedir_sb(sb, clnt);
192                 if (!dentry)
193                         return -ENOENT;
194                 if (IS_ERR(dentry))
195                         return PTR_ERR(dentry);
196                 break;
197         case RPC_PIPEFS_UMOUNT:
198                 __rpc_clnt_remove_pipedir(clnt);
199                 break;
200         default:
201                 printk(KERN_ERR "%s: unknown event: %ld\n", __func__, event);
202                 return -ENOTSUPP;
203         }
204         return err;
205 }
206
207 static int __rpc_pipefs_event(struct rpc_clnt *clnt, unsigned long event,
208                                 struct super_block *sb)
209 {
210         int error = 0;
211
212         for (;; clnt = clnt->cl_parent) {
213                 if (!rpc_clnt_skip_event(clnt, event))
214                         error = __rpc_clnt_handle_event(clnt, event, sb);
215                 if (error || clnt == clnt->cl_parent)
216                         break;
217         }
218         return error;
219 }
220
221 static struct rpc_clnt *rpc_get_client_for_event(struct net *net, int event)
222 {
223         struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
224         struct rpc_clnt *clnt;
225
226         spin_lock(&sn->rpc_client_lock);
227         list_for_each_entry(clnt, &sn->all_clients, cl_clients) {
228                 if (rpc_clnt_skip_event(clnt, event))
229                         continue;
230                 spin_unlock(&sn->rpc_client_lock);
231                 return clnt;
232         }
233         spin_unlock(&sn->rpc_client_lock);
234         return NULL;
235 }
236
237 static int rpc_pipefs_event(struct notifier_block *nb, unsigned long event,
238                             void *ptr)
239 {
240         struct super_block *sb = ptr;
241         struct rpc_clnt *clnt;
242         int error = 0;
243
244         while ((clnt = rpc_get_client_for_event(sb->s_fs_info, event))) {
245                 error = __rpc_pipefs_event(clnt, event, sb);
246                 if (error)
247                         break;
248         }
249         return error;
250 }
251
252 static struct notifier_block rpc_clients_block = {
253         .notifier_call  = rpc_pipefs_event,
254         .priority       = SUNRPC_PIPEFS_RPC_PRIO,
255 };
256
257 int rpc_clients_notifier_register(void)
258 {
259         return rpc_pipefs_notifier_register(&rpc_clients_block);
260 }
261
262 void rpc_clients_notifier_unregister(void)
263 {
264         return rpc_pipefs_notifier_unregister(&rpc_clients_block);
265 }
266
267 static void rpc_clnt_set_nodename(struct rpc_clnt *clnt, const char *nodename)
268 {
269         clnt->cl_nodelen = strlen(nodename);
270         if (clnt->cl_nodelen > UNX_MAXNODENAME)
271                 clnt->cl_nodelen = UNX_MAXNODENAME;
272         memcpy(clnt->cl_nodename, nodename, clnt->cl_nodelen);
273 }
274
275 static int rpc_client_register(const struct rpc_create_args *args,
276                                struct rpc_clnt *clnt)
277 {
278         struct rpc_auth_create_args auth_args = {
279                 .pseudoflavor = args->authflavor,
280                 .target_name = args->client_name,
281         };
282         struct rpc_auth *auth;
283         struct net *net = rpc_net_ns(clnt);
284         struct super_block *pipefs_sb;
285         int err;
286
287         pipefs_sb = rpc_get_sb_net(net);
288         if (pipefs_sb) {
289                 err = rpc_setup_pipedir(pipefs_sb, clnt);
290                 if (err)
291                         goto out;
292         }
293
294         rpc_register_client(clnt);
295         if (pipefs_sb)
296                 rpc_put_sb_net(net);
297
298         auth = rpcauth_create(&auth_args, clnt);
299         if (IS_ERR(auth)) {
300                 dprintk("RPC:       Couldn't create auth handle (flavor %u)\n",
301                                 args->authflavor);
302                 err = PTR_ERR(auth);
303                 goto err_auth;
304         }
305         return 0;
306 err_auth:
307         pipefs_sb = rpc_get_sb_net(net);
308         rpc_unregister_client(clnt);
309         __rpc_clnt_remove_pipedir(clnt);
310 out:
311         if (pipefs_sb)
312                 rpc_put_sb_net(net);
313         return err;
314 }
315
316 static struct rpc_clnt * rpc_new_client(const struct rpc_create_args *args,
317                 struct rpc_xprt *xprt,
318                 struct rpc_clnt *parent)
319 {
320         const struct rpc_program *program = args->program;
321         const struct rpc_version *version;
322         struct rpc_clnt         *clnt = NULL;
323         int err;
324
325         /* sanity check the name before trying to print it */
326         dprintk("RPC:       creating %s client for %s (xprt %p)\n",
327                         program->name, args->servername, xprt);
328
329         err = rpciod_up();
330         if (err)
331                 goto out_no_rpciod;
332
333         err = -EINVAL;
334         if (args->version >= program->nrvers)
335                 goto out_err;
336         version = program->version[args->version];
337         if (version == NULL)
338                 goto out_err;
339
340         err = -ENOMEM;
341         clnt = kzalloc(sizeof(*clnt), GFP_KERNEL);
342         if (!clnt)
343                 goto out_err;
344         clnt->cl_parent = parent ? : clnt;
345
346         rcu_assign_pointer(clnt->cl_xprt, xprt);
347         clnt->cl_procinfo = version->procs;
348         clnt->cl_maxproc  = version->nrprocs;
349         clnt->cl_prog     = args->prognumber ? : program->number;
350         clnt->cl_vers     = version->number;
351         clnt->cl_stats    = program->stats;
352         clnt->cl_metrics  = rpc_alloc_iostats(clnt);
353         rpc_init_pipe_dir_head(&clnt->cl_pipedir_objects);
354         err = -ENOMEM;
355         if (clnt->cl_metrics == NULL)
356                 goto out_no_stats;
357         clnt->cl_program  = program;
358         INIT_LIST_HEAD(&clnt->cl_tasks);
359         spin_lock_init(&clnt->cl_lock);
360
361         if (!xprt_bound(xprt))
362                 clnt->cl_autobind = 1;
363
364         clnt->cl_timeout = xprt->timeout;
365         if (args->timeout != NULL) {
366                 memcpy(&clnt->cl_timeout_default, args->timeout,
367                                 sizeof(clnt->cl_timeout_default));
368                 clnt->cl_timeout = &clnt->cl_timeout_default;
369         }
370
371         clnt->cl_rtt = &clnt->cl_rtt_default;
372         rpc_init_rtt(&clnt->cl_rtt_default, clnt->cl_timeout->to_initval);
373
374         atomic_set(&clnt->cl_count, 1);
375
376         /* save the nodename */
377         rpc_clnt_set_nodename(clnt, utsname()->nodename);
378
379         err = rpc_client_register(args, clnt);
380         if (err)
381                 goto out_no_path;
382         if (parent)
383                 atomic_inc(&parent->cl_count);
384         return clnt;
385
386 out_no_path:
387         rpc_free_iostats(clnt->cl_metrics);
388 out_no_stats:
389         kfree(clnt);
390 out_err:
391         rpciod_down();
392 out_no_rpciod:
393         xprt_put(xprt);
394         return ERR_PTR(err);
395 }
396
397 /**
398  * rpc_create - create an RPC client and transport with one call
399  * @args: rpc_clnt create argument structure
400  *
401  * Creates and initializes an RPC transport and an RPC client.
402  *
403  * It can ping the server in order to determine if it is up, and to see if
404  * it supports this program and version.  RPC_CLNT_CREATE_NOPING disables
405  * this behavior so asynchronous tasks can also use rpc_create.
406  */
407 struct rpc_clnt *rpc_create(struct rpc_create_args *args)
408 {
409         struct rpc_xprt *xprt;
410         struct rpc_clnt *clnt;
411         struct xprt_create xprtargs = {
412                 .net = args->net,
413                 .ident = args->protocol,
414                 .srcaddr = args->saddress,
415                 .dstaddr = args->address,
416                 .addrlen = args->addrsize,
417                 .servername = args->servername,
418                 .bc_xprt = args->bc_xprt,
419         };
420         char servername[48];
421
422         if (args->flags & RPC_CLNT_CREATE_INFINITE_SLOTS)
423                 xprtargs.flags |= XPRT_CREATE_INFINITE_SLOTS;
424         if (args->flags & RPC_CLNT_CREATE_NO_IDLE_TIMEOUT)
425                 xprtargs.flags |= XPRT_CREATE_NO_IDLE_TIMEOUT;
426         /*
427          * If the caller chooses not to specify a hostname, whip
428          * up a string representation of the passed-in address.
429          */
430         if (xprtargs.servername == NULL) {
431                 struct sockaddr_un *sun =
432                                 (struct sockaddr_un *)args->address;
433                 struct sockaddr_in *sin =
434                                 (struct sockaddr_in *)args->address;
435                 struct sockaddr_in6 *sin6 =
436                                 (struct sockaddr_in6 *)args->address;
437
438                 servername[0] = '\0';
439                 switch (args->address->sa_family) {
440                 case AF_LOCAL:
441                         snprintf(servername, sizeof(servername), "%s",
442                                  sun->sun_path);
443                         break;
444                 case AF_INET:
445                         snprintf(servername, sizeof(servername), "%pI4",
446                                  &sin->sin_addr.s_addr);
447                         break;
448                 case AF_INET6:
449                         snprintf(servername, sizeof(servername), "%pI6",
450                                  &sin6->sin6_addr);
451                         break;
452                 default:
453                         /* caller wants default server name, but
454                          * address family isn't recognized. */
455                         return ERR_PTR(-EINVAL);
456                 }
457                 xprtargs.servername = servername;
458         }
459
460         xprt = xprt_create_transport(&xprtargs);
461         if (IS_ERR(xprt))
462                 return (struct rpc_clnt *)xprt;
463
464         /*
465          * By default, kernel RPC client connects from a reserved port.
466          * CAP_NET_BIND_SERVICE will not be set for unprivileged requesters,
467          * but it is always enabled for rpciod, which handles the connect
468          * operation.
469          */
470         xprt->resvport = 1;
471         if (args->flags & RPC_CLNT_CREATE_NONPRIVPORT)
472                 xprt->resvport = 0;
473
474         clnt = rpc_new_client(args, xprt, NULL);
475         if (IS_ERR(clnt))
476                 return clnt;
477
478         if (!(args->flags & RPC_CLNT_CREATE_NOPING)) {
479                 int err = rpc_ping(clnt);
480                 if (err != 0) {
481                         rpc_shutdown_client(clnt);
482                         return ERR_PTR(err);
483                 }
484         }
485
486         clnt->cl_softrtry = 1;
487         if (args->flags & RPC_CLNT_CREATE_HARDRTRY)
488                 clnt->cl_softrtry = 0;
489
490         if (args->flags & RPC_CLNT_CREATE_AUTOBIND)
491                 clnt->cl_autobind = 1;
492         if (args->flags & RPC_CLNT_CREATE_DISCRTRY)
493                 clnt->cl_discrtry = 1;
494         if (!(args->flags & RPC_CLNT_CREATE_QUIET))
495                 clnt->cl_chatty = 1;
496
497         return clnt;
498 }
499 EXPORT_SYMBOL_GPL(rpc_create);
500
501 /*
502  * This function clones the RPC client structure. It allows us to share the
503  * same transport while varying parameters such as the authentication
504  * flavour.
505  */
506 static struct rpc_clnt *__rpc_clone_client(struct rpc_create_args *args,
507                                            struct rpc_clnt *clnt)
508 {
509         struct rpc_xprt *xprt;
510         struct rpc_clnt *new;
511         int err;
512
513         err = -ENOMEM;
514         rcu_read_lock();
515         xprt = xprt_get(rcu_dereference(clnt->cl_xprt));
516         rcu_read_unlock();
517         if (xprt == NULL)
518                 goto out_err;
519         args->servername = xprt->servername;
520
521         new = rpc_new_client(args, xprt, clnt);
522         if (IS_ERR(new)) {
523                 err = PTR_ERR(new);
524                 goto out_err;
525         }
526
527         /* Turn off autobind on clones */
528         new->cl_autobind = 0;
529         new->cl_softrtry = clnt->cl_softrtry;
530         new->cl_discrtry = clnt->cl_discrtry;
531         new->cl_chatty = clnt->cl_chatty;
532         return new;
533
534 out_err:
535         dprintk("RPC:       %s: returned error %d\n", __func__, err);
536         return ERR_PTR(err);
537 }
538
539 /**
540  * rpc_clone_client - Clone an RPC client structure
541  *
542  * @clnt: RPC client whose parameters are copied
543  *
544  * Returns a fresh RPC client or an ERR_PTR.
545  */
546 struct rpc_clnt *rpc_clone_client(struct rpc_clnt *clnt)
547 {
548         struct rpc_create_args args = {
549                 .program        = clnt->cl_program,
550                 .prognumber     = clnt->cl_prog,
551                 .version        = clnt->cl_vers,
552                 .authflavor     = clnt->cl_auth->au_flavor,
553         };
554         return __rpc_clone_client(&args, clnt);
555 }
556 EXPORT_SYMBOL_GPL(rpc_clone_client);
557
558 /**
559  * rpc_clone_client_set_auth - Clone an RPC client structure and set its auth
560  *
561  * @clnt: RPC client whose parameters are copied
562  * @flavor: security flavor for new client
563  *
564  * Returns a fresh RPC client or an ERR_PTR.
565  */
566 struct rpc_clnt *
567 rpc_clone_client_set_auth(struct rpc_clnt *clnt, rpc_authflavor_t flavor)
568 {
569         struct rpc_create_args args = {
570                 .program        = clnt->cl_program,
571                 .prognumber     = clnt->cl_prog,
572                 .version        = clnt->cl_vers,
573                 .authflavor     = flavor,
574         };
575         return __rpc_clone_client(&args, clnt);
576 }
577 EXPORT_SYMBOL_GPL(rpc_clone_client_set_auth);
578
579 /*
580  * Kill all tasks for the given client.
581  * XXX: kill their descendants as well?
582  */
583 void rpc_killall_tasks(struct rpc_clnt *clnt)
584 {
585         struct rpc_task *rovr;
586
587
588         if (list_empty(&clnt->cl_tasks))
589                 return;
590         dprintk("RPC:       killing all tasks for client %p\n", clnt);
591         /*
592          * Spin lock all_tasks to prevent changes...
593          */
594         spin_lock(&clnt->cl_lock);
595         list_for_each_entry(rovr, &clnt->cl_tasks, tk_task) {
596                 if (!RPC_IS_ACTIVATED(rovr))
597                         continue;
598                 if (!(rovr->tk_flags & RPC_TASK_KILLED)) {
599                         rovr->tk_flags |= RPC_TASK_KILLED;
600                         rpc_exit(rovr, -EIO);
601                         if (RPC_IS_QUEUED(rovr))
602                                 rpc_wake_up_queued_task(rovr->tk_waitqueue,
603                                                         rovr);
604                 }
605         }
606         spin_unlock(&clnt->cl_lock);
607 }
608 EXPORT_SYMBOL_GPL(rpc_killall_tasks);
609
610 /*
611  * Properly shut down an RPC client, terminating all outstanding
612  * requests.
613  */
614 void rpc_shutdown_client(struct rpc_clnt *clnt)
615 {
616         might_sleep();
617
618         dprintk_rcu("RPC:       shutting down %s client for %s\n",
619                         clnt->cl_program->name,
620                         rcu_dereference(clnt->cl_xprt)->servername);
621
622         while (!list_empty(&clnt->cl_tasks)) {
623                 rpc_killall_tasks(clnt);
624                 wait_event_timeout(destroy_wait,
625                         list_empty(&clnt->cl_tasks), 1*HZ);
626         }
627
628         rpc_release_client(clnt);
629 }
630 EXPORT_SYMBOL_GPL(rpc_shutdown_client);
631
632 /*
633  * Free an RPC client
634  */
635 static void
636 rpc_free_client(struct rpc_clnt *clnt)
637 {
638         dprintk_rcu("RPC:       destroying %s client for %s\n",
639                         clnt->cl_program->name,
640                         rcu_dereference(clnt->cl_xprt)->servername);
641         if (clnt->cl_parent != clnt)
642                 rpc_release_client(clnt->cl_parent);
643         rpc_clnt_remove_pipedir(clnt);
644         rpc_unregister_client(clnt);
645         rpc_free_iostats(clnt->cl_metrics);
646         clnt->cl_metrics = NULL;
647         xprt_put(rcu_dereference_raw(clnt->cl_xprt));
648         rpciod_down();
649         kfree(clnt);
650 }
651
652 /*
653  * Free an RPC client
654  */
655 static void
656 rpc_free_auth(struct rpc_clnt *clnt)
657 {
658         if (clnt->cl_auth == NULL) {
659                 rpc_free_client(clnt);
660                 return;
661         }
662
663         /*
664          * Note: RPCSEC_GSS may need to send NULL RPC calls in order to
665          *       release remaining GSS contexts. This mechanism ensures
666          *       that it can do so safely.
667          */
668         atomic_inc(&clnt->cl_count);
669         rpcauth_release(clnt->cl_auth);
670         clnt->cl_auth = NULL;
671         if (atomic_dec_and_test(&clnt->cl_count))
672                 rpc_free_client(clnt);
673 }
674
675 /*
676  * Release reference to the RPC client
677  */
678 void
679 rpc_release_client(struct rpc_clnt *clnt)
680 {
681         dprintk("RPC:       rpc_release_client(%p)\n", clnt);
682
683         if (list_empty(&clnt->cl_tasks))
684                 wake_up(&destroy_wait);
685         if (atomic_dec_and_test(&clnt->cl_count))
686                 rpc_free_auth(clnt);
687 }
688 EXPORT_SYMBOL_GPL(rpc_release_client);
689
690 /**
691  * rpc_bind_new_program - bind a new RPC program to an existing client
692  * @old: old rpc_client
693  * @program: rpc program to set
694  * @vers: rpc program version
695  *
696  * Clones the rpc client and sets up a new RPC program. This is mainly
697  * of use for enabling different RPC programs to share the same transport.
698  * The Sun NFSv2/v3 ACL protocol can do this.
699  */
700 struct rpc_clnt *rpc_bind_new_program(struct rpc_clnt *old,
701                                       const struct rpc_program *program,
702                                       u32 vers)
703 {
704         struct rpc_create_args args = {
705                 .program        = program,
706                 .prognumber     = program->number,
707                 .version        = vers,
708                 .authflavor     = old->cl_auth->au_flavor,
709         };
710         struct rpc_clnt *clnt;
711         int err;
712
713         clnt = __rpc_clone_client(&args, old);
714         if (IS_ERR(clnt))
715                 goto out;
716         err = rpc_ping(clnt);
717         if (err != 0) {
718                 rpc_shutdown_client(clnt);
719                 clnt = ERR_PTR(err);
720         }
721 out:
722         return clnt;
723 }
724 EXPORT_SYMBOL_GPL(rpc_bind_new_program);
725
726 void rpc_task_release_client(struct rpc_task *task)
727 {
728         struct rpc_clnt *clnt = task->tk_client;
729
730         if (clnt != NULL) {
731                 /* Remove from client task list */
732                 spin_lock(&clnt->cl_lock);
733                 list_del(&task->tk_task);
734                 spin_unlock(&clnt->cl_lock);
735                 task->tk_client = NULL;
736
737                 rpc_release_client(clnt);
738         }
739 }
740
741 static
742 void rpc_task_set_client(struct rpc_task *task, struct rpc_clnt *clnt)
743 {
744         if (clnt != NULL) {
745                 rpc_task_release_client(task);
746                 task->tk_client = clnt;
747                 atomic_inc(&clnt->cl_count);
748                 if (clnt->cl_softrtry)
749                         task->tk_flags |= RPC_TASK_SOFT;
750                 if (sk_memalloc_socks()) {
751                         struct rpc_xprt *xprt;
752
753                         rcu_read_lock();
754                         xprt = rcu_dereference(clnt->cl_xprt);
755                         if (xprt->swapper)
756                                 task->tk_flags |= RPC_TASK_SWAPPER;
757                         rcu_read_unlock();
758                 }
759                 /* Add to the client's list of all tasks */
760                 spin_lock(&clnt->cl_lock);
761                 list_add_tail(&task->tk_task, &clnt->cl_tasks);
762                 spin_unlock(&clnt->cl_lock);
763         }
764 }
765
766 void rpc_task_reset_client(struct rpc_task *task, struct rpc_clnt *clnt)
767 {
768         rpc_task_release_client(task);
769         rpc_task_set_client(task, clnt);
770 }
771 EXPORT_SYMBOL_GPL(rpc_task_reset_client);
772
773
774 static void
775 rpc_task_set_rpc_message(struct rpc_task *task, const struct rpc_message *msg)
776 {
777         if (msg != NULL) {
778                 task->tk_msg.rpc_proc = msg->rpc_proc;
779                 task->tk_msg.rpc_argp = msg->rpc_argp;
780                 task->tk_msg.rpc_resp = msg->rpc_resp;
781                 if (msg->rpc_cred != NULL)
782                         task->tk_msg.rpc_cred = get_rpccred(msg->rpc_cred);
783         }
784 }
785
786 /*
787  * Default callback for async RPC calls
788  */
789 static void
790 rpc_default_callback(struct rpc_task *task, void *data)
791 {
792 }
793
794 static const struct rpc_call_ops rpc_default_ops = {
795         .rpc_call_done = rpc_default_callback,
796 };
797
798 /**
799  * rpc_run_task - Allocate a new RPC task, then run rpc_execute against it
800  * @task_setup_data: pointer to task initialisation data
801  */
802 struct rpc_task *rpc_run_task(const struct rpc_task_setup *task_setup_data)
803 {
804         struct rpc_task *task;
805
806         task = rpc_new_task(task_setup_data);
807         if (IS_ERR(task))
808                 goto out;
809
810         rpc_task_set_client(task, task_setup_data->rpc_client);
811         rpc_task_set_rpc_message(task, task_setup_data->rpc_message);
812
813         if (task->tk_action == NULL)
814                 rpc_call_start(task);
815
816         atomic_inc(&task->tk_count);
817         rpc_execute(task);
818 out:
819         return task;
820 }
821 EXPORT_SYMBOL_GPL(rpc_run_task);
822
823 /**
824  * rpc_call_sync - Perform a synchronous RPC call
825  * @clnt: pointer to RPC client
826  * @msg: RPC call parameters
827  * @flags: RPC call flags
828  */
829 int rpc_call_sync(struct rpc_clnt *clnt, const struct rpc_message *msg, int flags)
830 {
831         struct rpc_task *task;
832         struct rpc_task_setup task_setup_data = {
833                 .rpc_client = clnt,
834                 .rpc_message = msg,
835                 .callback_ops = &rpc_default_ops,
836                 .flags = flags,
837         };
838         int status;
839
840         WARN_ON_ONCE(flags & RPC_TASK_ASYNC);
841         if (flags & RPC_TASK_ASYNC) {
842                 rpc_release_calldata(task_setup_data.callback_ops,
843                         task_setup_data.callback_data);
844                 return -EINVAL;
845         }
846
847         task = rpc_run_task(&task_setup_data);
848         if (IS_ERR(task))
849                 return PTR_ERR(task);
850         status = task->tk_status;
851         rpc_put_task(task);
852         return status;
853 }
854 EXPORT_SYMBOL_GPL(rpc_call_sync);
855
856 /**
857  * rpc_call_async - Perform an asynchronous RPC call
858  * @clnt: pointer to RPC client
859  * @msg: RPC call parameters
860  * @flags: RPC call flags
861  * @tk_ops: RPC call ops
862  * @data: user call data
863  */
864 int
865 rpc_call_async(struct rpc_clnt *clnt, const struct rpc_message *msg, int flags,
866                const struct rpc_call_ops *tk_ops, void *data)
867 {
868         struct rpc_task *task;
869         struct rpc_task_setup task_setup_data = {
870                 .rpc_client = clnt,
871                 .rpc_message = msg,
872                 .callback_ops = tk_ops,
873                 .callback_data = data,
874                 .flags = flags|RPC_TASK_ASYNC,
875         };
876
877         task = rpc_run_task(&task_setup_data);
878         if (IS_ERR(task))
879                 return PTR_ERR(task);
880         rpc_put_task(task);
881         return 0;
882 }
883 EXPORT_SYMBOL_GPL(rpc_call_async);
884
885 #if defined(CONFIG_SUNRPC_BACKCHANNEL)
886 /**
887  * rpc_run_bc_task - Allocate a new RPC task for backchannel use, then run
888  * rpc_execute against it
889  * @req: RPC request
890  * @tk_ops: RPC call ops
891  */
892 struct rpc_task *rpc_run_bc_task(struct rpc_rqst *req,
893                                 const struct rpc_call_ops *tk_ops)
894 {
895         struct rpc_task *task;
896         struct xdr_buf *xbufp = &req->rq_snd_buf;
897         struct rpc_task_setup task_setup_data = {
898                 .callback_ops = tk_ops,
899         };
900
901         dprintk("RPC: rpc_run_bc_task req= %p\n", req);
902         /*
903          * Create an rpc_task to send the data
904          */
905         task = rpc_new_task(&task_setup_data);
906         if (IS_ERR(task)) {
907                 xprt_free_bc_request(req);
908                 goto out;
909         }
910         task->tk_rqstp = req;
911
912         /*
913          * Set up the xdr_buf length.
914          * This also indicates that the buffer is XDR encoded already.
915          */
916         xbufp->len = xbufp->head[0].iov_len + xbufp->page_len +
917                         xbufp->tail[0].iov_len;
918
919         task->tk_action = call_bc_transmit;
920         atomic_inc(&task->tk_count);
921         WARN_ON_ONCE(atomic_read(&task->tk_count) != 2);
922         rpc_execute(task);
923
924 out:
925         dprintk("RPC: rpc_run_bc_task: task= %p\n", task);
926         return task;
927 }
928 #endif /* CONFIG_SUNRPC_BACKCHANNEL */
929
930 void
931 rpc_call_start(struct rpc_task *task)
932 {
933         task->tk_action = call_start;
934 }
935 EXPORT_SYMBOL_GPL(rpc_call_start);
936
937 /**
938  * rpc_peeraddr - extract remote peer address from clnt's xprt
939  * @clnt: RPC client structure
940  * @buf: target buffer
941  * @bufsize: length of target buffer
942  *
943  * Returns the number of bytes that are actually in the stored address.
944  */
945 size_t rpc_peeraddr(struct rpc_clnt *clnt, struct sockaddr *buf, size_t bufsize)
946 {
947         size_t bytes;
948         struct rpc_xprt *xprt;
949
950         rcu_read_lock();
951         xprt = rcu_dereference(clnt->cl_xprt);
952
953         bytes = xprt->addrlen;
954         if (bytes > bufsize)
955                 bytes = bufsize;
956         memcpy(buf, &xprt->addr, bytes);
957         rcu_read_unlock();
958
959         return bytes;
960 }
961 EXPORT_SYMBOL_GPL(rpc_peeraddr);
962
963 /**
964  * rpc_peeraddr2str - return remote peer address in printable format
965  * @clnt: RPC client structure
966  * @format: address format
967  *
968  * NB: the lifetime of the memory referenced by the returned pointer is
969  * the same as the rpc_xprt itself.  As long as the caller uses this
970  * pointer, it must hold the RCU read lock.
971  */
972 const char *rpc_peeraddr2str(struct rpc_clnt *clnt,
973                              enum rpc_display_format_t format)
974 {
975         struct rpc_xprt *xprt;
976
977         xprt = rcu_dereference(clnt->cl_xprt);
978
979         if (xprt->address_strings[format] != NULL)
980                 return xprt->address_strings[format];
981         else
982                 return "unprintable";
983 }
984 EXPORT_SYMBOL_GPL(rpc_peeraddr2str);
985
986 static const struct sockaddr_in rpc_inaddr_loopback = {
987         .sin_family             = AF_INET,
988         .sin_addr.s_addr        = htonl(INADDR_ANY),
989 };
990
991 static const struct sockaddr_in6 rpc_in6addr_loopback = {
992         .sin6_family            = AF_INET6,
993         .sin6_addr              = IN6ADDR_ANY_INIT,
994 };
995
996 /*
997  * Try a getsockname() on a connected datagram socket.  Using a
998  * connected datagram socket prevents leaving a socket in TIME_WAIT.
999  * This conserves the ephemeral port number space.
1000  *
1001  * Returns zero and fills in "buf" if successful; otherwise, a
1002  * negative errno is returned.
1003  */
1004 static int rpc_sockname(struct net *net, struct sockaddr *sap, size_t salen,
1005                         struct sockaddr *buf, int buflen)
1006 {
1007         struct socket *sock;
1008         int err;
1009
1010         err = __sock_create(net, sap->sa_family,
1011                                 SOCK_DGRAM, IPPROTO_UDP, &sock, 1);
1012         if (err < 0) {
1013                 dprintk("RPC:       can't create UDP socket (%d)\n", err);
1014                 goto out;
1015         }
1016
1017         switch (sap->sa_family) {
1018         case AF_INET:
1019                 err = kernel_bind(sock,
1020                                 (struct sockaddr *)&rpc_inaddr_loopback,
1021                                 sizeof(rpc_inaddr_loopback));
1022                 break;
1023         case AF_INET6:
1024                 err = kernel_bind(sock,
1025                                 (struct sockaddr *)&rpc_in6addr_loopback,
1026                                 sizeof(rpc_in6addr_loopback));
1027                 break;
1028         default:
1029                 err = -EAFNOSUPPORT;
1030                 goto out;
1031         }
1032         if (err < 0) {
1033                 dprintk("RPC:       can't bind UDP socket (%d)\n", err);
1034                 goto out_release;
1035         }
1036
1037         err = kernel_connect(sock, sap, salen, 0);
1038         if (err < 0) {
1039                 dprintk("RPC:       can't connect UDP socket (%d)\n", err);
1040                 goto out_release;
1041         }
1042
1043         err = kernel_getsockname(sock, buf, &buflen);
1044         if (err < 0) {
1045                 dprintk("RPC:       getsockname failed (%d)\n", err);
1046                 goto out_release;
1047         }
1048
1049         err = 0;
1050         if (buf->sa_family == AF_INET6) {
1051                 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)buf;
1052                 sin6->sin6_scope_id = 0;
1053         }
1054         dprintk("RPC:       %s succeeded\n", __func__);
1055
1056 out_release:
1057         sock_release(sock);
1058 out:
1059         return err;
1060 }
1061
1062 /*
1063  * Scraping a connected socket failed, so we don't have a useable
1064  * local address.  Fallback: generate an address that will prevent
1065  * the server from calling us back.
1066  *
1067  * Returns zero and fills in "buf" if successful; otherwise, a
1068  * negative errno is returned.
1069  */
1070 static int rpc_anyaddr(int family, struct sockaddr *buf, size_t buflen)
1071 {
1072         switch (family) {
1073         case AF_INET:
1074                 if (buflen < sizeof(rpc_inaddr_loopback))
1075                         return -EINVAL;
1076                 memcpy(buf, &rpc_inaddr_loopback,
1077                                 sizeof(rpc_inaddr_loopback));
1078                 break;
1079         case AF_INET6:
1080                 if (buflen < sizeof(rpc_in6addr_loopback))
1081                         return -EINVAL;
1082                 memcpy(buf, &rpc_in6addr_loopback,
1083                                 sizeof(rpc_in6addr_loopback));
1084         default:
1085                 dprintk("RPC:       %s: address family not supported\n",
1086                         __func__);
1087                 return -EAFNOSUPPORT;
1088         }
1089         dprintk("RPC:       %s: succeeded\n", __func__);
1090         return 0;
1091 }
1092
1093 /**
1094  * rpc_localaddr - discover local endpoint address for an RPC client
1095  * @clnt: RPC client structure
1096  * @buf: target buffer
1097  * @buflen: size of target buffer, in bytes
1098  *
1099  * Returns zero and fills in "buf" and "buflen" if successful;
1100  * otherwise, a negative errno is returned.
1101  *
1102  * This works even if the underlying transport is not currently connected,
1103  * or if the upper layer never previously provided a source address.
1104  *
1105  * The result of this function call is transient: multiple calls in
1106  * succession may give different results, depending on how local
1107  * networking configuration changes over time.
1108  */
1109 int rpc_localaddr(struct rpc_clnt *clnt, struct sockaddr *buf, size_t buflen)
1110 {
1111         struct sockaddr_storage address;
1112         struct sockaddr *sap = (struct sockaddr *)&address;
1113         struct rpc_xprt *xprt;
1114         struct net *net;
1115         size_t salen;
1116         int err;
1117
1118         rcu_read_lock();
1119         xprt = rcu_dereference(clnt->cl_xprt);
1120         salen = xprt->addrlen;
1121         memcpy(sap, &xprt->addr, salen);
1122         net = get_net(xprt->xprt_net);
1123         rcu_read_unlock();
1124
1125         rpc_set_port(sap, 0);
1126         err = rpc_sockname(net, sap, salen, buf, buflen);
1127         put_net(net);
1128         if (err != 0)
1129                 /* Couldn't discover local address, return ANYADDR */
1130                 return rpc_anyaddr(sap->sa_family, buf, buflen);
1131         return 0;
1132 }
1133 EXPORT_SYMBOL_GPL(rpc_localaddr);
1134
1135 void
1136 rpc_setbufsize(struct rpc_clnt *clnt, unsigned int sndsize, unsigned int rcvsize)
1137 {
1138         struct rpc_xprt *xprt;
1139
1140         rcu_read_lock();
1141         xprt = rcu_dereference(clnt->cl_xprt);
1142         if (xprt->ops->set_buffer_size)
1143                 xprt->ops->set_buffer_size(xprt, sndsize, rcvsize);
1144         rcu_read_unlock();
1145 }
1146 EXPORT_SYMBOL_GPL(rpc_setbufsize);
1147
1148 /**
1149  * rpc_protocol - Get transport protocol number for an RPC client
1150  * @clnt: RPC client to query
1151  *
1152  */
1153 int rpc_protocol(struct rpc_clnt *clnt)
1154 {
1155         int protocol;
1156
1157         rcu_read_lock();
1158         protocol = rcu_dereference(clnt->cl_xprt)->prot;
1159         rcu_read_unlock();
1160         return protocol;
1161 }
1162 EXPORT_SYMBOL_GPL(rpc_protocol);
1163
1164 /**
1165  * rpc_net_ns - Get the network namespace for this RPC client
1166  * @clnt: RPC client to query
1167  *
1168  */
1169 struct net *rpc_net_ns(struct rpc_clnt *clnt)
1170 {
1171         struct net *ret;
1172
1173         rcu_read_lock();
1174         ret = rcu_dereference(clnt->cl_xprt)->xprt_net;
1175         rcu_read_unlock();
1176         return ret;
1177 }
1178 EXPORT_SYMBOL_GPL(rpc_net_ns);
1179
1180 /**
1181  * rpc_max_payload - Get maximum payload size for a transport, in bytes
1182  * @clnt: RPC client to query
1183  *
1184  * For stream transports, this is one RPC record fragment (see RFC
1185  * 1831), as we don't support multi-record requests yet.  For datagram
1186  * transports, this is the size of an IP packet minus the IP, UDP, and
1187  * RPC header sizes.
1188  */
1189 size_t rpc_max_payload(struct rpc_clnt *clnt)
1190 {
1191         size_t ret;
1192
1193         rcu_read_lock();
1194         ret = rcu_dereference(clnt->cl_xprt)->max_payload;
1195         rcu_read_unlock();
1196         return ret;
1197 }
1198 EXPORT_SYMBOL_GPL(rpc_max_payload);
1199
1200 /**
1201  * rpc_get_timeout - Get timeout for transport in units of HZ
1202  * @clnt: RPC client to query
1203  */
1204 unsigned long rpc_get_timeout(struct rpc_clnt *clnt)
1205 {
1206         unsigned long ret;
1207
1208         rcu_read_lock();
1209         ret = rcu_dereference(clnt->cl_xprt)->timeout->to_initval;
1210         rcu_read_unlock();
1211         return ret;
1212 }
1213 EXPORT_SYMBOL_GPL(rpc_get_timeout);
1214
1215 /**
1216  * rpc_force_rebind - force transport to check that remote port is unchanged
1217  * @clnt: client to rebind
1218  *
1219  */
1220 void rpc_force_rebind(struct rpc_clnt *clnt)
1221 {
1222         if (clnt->cl_autobind) {
1223                 rcu_read_lock();
1224                 xprt_clear_bound(rcu_dereference(clnt->cl_xprt));
1225                 rcu_read_unlock();
1226         }
1227 }
1228 EXPORT_SYMBOL_GPL(rpc_force_rebind);
1229
1230 /*
1231  * Restart an (async) RPC call from the call_prepare state.
1232  * Usually called from within the exit handler.
1233  */
1234 int
1235 rpc_restart_call_prepare(struct rpc_task *task)
1236 {
1237         if (RPC_ASSASSINATED(task))
1238                 return 0;
1239         task->tk_action = call_start;
1240         if (task->tk_ops->rpc_call_prepare != NULL)
1241                 task->tk_action = rpc_prepare_task;
1242         return 1;
1243 }
1244 EXPORT_SYMBOL_GPL(rpc_restart_call_prepare);
1245
1246 /*
1247  * Restart an (async) RPC call. Usually called from within the
1248  * exit handler.
1249  */
1250 int
1251 rpc_restart_call(struct rpc_task *task)
1252 {
1253         if (RPC_ASSASSINATED(task))
1254                 return 0;
1255         task->tk_action = call_start;
1256         return 1;
1257 }
1258 EXPORT_SYMBOL_GPL(rpc_restart_call);
1259
1260 #ifdef RPC_DEBUG
1261 static const char *rpc_proc_name(const struct rpc_task *task)
1262 {
1263         const struct rpc_procinfo *proc = task->tk_msg.rpc_proc;
1264
1265         if (proc) {
1266                 if (proc->p_name)
1267                         return proc->p_name;
1268                 else
1269                         return "NULL";
1270         } else
1271                 return "no proc";
1272 }
1273 #endif
1274
1275 /*
1276  * 0.  Initial state
1277  *
1278  *     Other FSM states can be visited zero or more times, but
1279  *     this state is visited exactly once for each RPC.
1280  */
1281 static void
1282 call_start(struct rpc_task *task)
1283 {
1284         struct rpc_clnt *clnt = task->tk_client;
1285
1286         dprintk("RPC: %5u call_start %s%d proc %s (%s)\n", task->tk_pid,
1287                         clnt->cl_program->name, clnt->cl_vers,
1288                         rpc_proc_name(task),
1289                         (RPC_IS_ASYNC(task) ? "async" : "sync"));
1290
1291         /* Increment call count */
1292         task->tk_msg.rpc_proc->p_count++;
1293         clnt->cl_stats->rpccnt++;
1294         task->tk_action = call_reserve;
1295 }
1296
1297 /*
1298  * 1.   Reserve an RPC call slot
1299  */
1300 static void
1301 call_reserve(struct rpc_task *task)
1302 {
1303         dprint_status(task);
1304
1305         task->tk_status  = 0;
1306         task->tk_action  = call_reserveresult;
1307         xprt_reserve(task);
1308 }
1309
1310 static void call_retry_reserve(struct rpc_task *task);
1311
1312 /*
1313  * 1b.  Grok the result of xprt_reserve()
1314  */
1315 static void
1316 call_reserveresult(struct rpc_task *task)
1317 {
1318         int status = task->tk_status;
1319
1320         dprint_status(task);
1321
1322         /*
1323          * After a call to xprt_reserve(), we must have either
1324          * a request slot or else an error status.
1325          */
1326         task->tk_status = 0;
1327         if (status >= 0) {
1328                 if (task->tk_rqstp) {
1329                         task->tk_action = call_refresh;
1330                         return;
1331                 }
1332
1333                 printk(KERN_ERR "%s: status=%d, but no request slot, exiting\n",
1334                                 __func__, status);
1335                 rpc_exit(task, -EIO);
1336                 return;
1337         }
1338
1339         /*
1340          * Even though there was an error, we may have acquired
1341          * a request slot somehow.  Make sure not to leak it.
1342          */
1343         if (task->tk_rqstp) {
1344                 printk(KERN_ERR "%s: status=%d, request allocated anyway\n",
1345                                 __func__, status);
1346                 xprt_release(task);
1347         }
1348
1349         switch (status) {
1350         case -ENOMEM:
1351                 rpc_delay(task, HZ >> 2);
1352         case -EAGAIN:   /* woken up; retry */
1353                 task->tk_action = call_retry_reserve;
1354                 return;
1355         case -EIO:      /* probably a shutdown */
1356                 break;
1357         default:
1358                 printk(KERN_ERR "%s: unrecognized error %d, exiting\n",
1359                                 __func__, status);
1360                 break;
1361         }
1362         rpc_exit(task, status);
1363 }
1364
1365 /*
1366  * 1c.  Retry reserving an RPC call slot
1367  */
1368 static void
1369 call_retry_reserve(struct rpc_task *task)
1370 {
1371         dprint_status(task);
1372
1373         task->tk_status  = 0;
1374         task->tk_action  = call_reserveresult;
1375         xprt_retry_reserve(task);
1376 }
1377
1378 /*
1379  * 2.   Bind and/or refresh the credentials
1380  */
1381 static void
1382 call_refresh(struct rpc_task *task)
1383 {
1384         dprint_status(task);
1385
1386         task->tk_action = call_refreshresult;
1387         task->tk_status = 0;
1388         task->tk_client->cl_stats->rpcauthrefresh++;
1389         rpcauth_refreshcred(task);
1390 }
1391
1392 /*
1393  * 2a.  Process the results of a credential refresh
1394  */
1395 static void
1396 call_refreshresult(struct rpc_task *task)
1397 {
1398         int status = task->tk_status;
1399
1400         dprint_status(task);
1401
1402         task->tk_status = 0;
1403         task->tk_action = call_refresh;
1404         switch (status) {
1405         case 0:
1406                 if (rpcauth_uptodatecred(task))
1407                         task->tk_action = call_allocate;
1408                 return;
1409         case -ETIMEDOUT:
1410                 rpc_delay(task, 3*HZ);
1411         case -EKEYEXPIRED:
1412         case -EAGAIN:
1413                 status = -EACCES;
1414                 if (!task->tk_cred_retry)
1415                         break;
1416                 task->tk_cred_retry--;
1417                 dprintk("RPC: %5u %s: retry refresh creds\n",
1418                                 task->tk_pid, __func__);
1419                 return;
1420         }
1421         dprintk("RPC: %5u %s: refresh creds failed with error %d\n",
1422                                 task->tk_pid, __func__, status);
1423         rpc_exit(task, status);
1424 }
1425
1426 /*
1427  * 2b.  Allocate the buffer. For details, see sched.c:rpc_malloc.
1428  *      (Note: buffer memory is freed in xprt_release).
1429  */
1430 static void
1431 call_allocate(struct rpc_task *task)
1432 {
1433         unsigned int slack = task->tk_rqstp->rq_cred->cr_auth->au_cslack;
1434         struct rpc_rqst *req = task->tk_rqstp;
1435         struct rpc_xprt *xprt = req->rq_xprt;
1436         struct rpc_procinfo *proc = task->tk_msg.rpc_proc;
1437
1438         dprint_status(task);
1439
1440         task->tk_status = 0;
1441         task->tk_action = call_bind;
1442
1443         if (req->rq_buffer)
1444                 return;
1445
1446         if (proc->p_proc != 0) {
1447                 BUG_ON(proc->p_arglen == 0);
1448                 if (proc->p_decode != NULL)
1449                         BUG_ON(proc->p_replen == 0);
1450         }
1451
1452         /*
1453          * Calculate the size (in quads) of the RPC call
1454          * and reply headers, and convert both values
1455          * to byte sizes.
1456          */
1457         req->rq_callsize = RPC_CALLHDRSIZE + (slack << 1) + proc->p_arglen;
1458         req->rq_callsize <<= 2;
1459         req->rq_rcvsize = RPC_REPHDRSIZE + slack + proc->p_replen;
1460         req->rq_rcvsize <<= 2;
1461
1462         req->rq_buffer = xprt->ops->buf_alloc(task,
1463                                         req->rq_callsize + req->rq_rcvsize);
1464         if (req->rq_buffer != NULL)
1465                 return;
1466
1467         dprintk("RPC: %5u rpc_buffer allocation failed\n", task->tk_pid);
1468
1469         if (RPC_IS_ASYNC(task) || !fatal_signal_pending(current)) {
1470                 task->tk_action = call_allocate;
1471                 rpc_delay(task, HZ>>4);
1472                 return;
1473         }
1474
1475         rpc_exit(task, -ERESTARTSYS);
1476 }
1477
1478 static inline int
1479 rpc_task_need_encode(struct rpc_task *task)
1480 {
1481         return task->tk_rqstp->rq_snd_buf.len == 0;
1482 }
1483
1484 static inline void
1485 rpc_task_force_reencode(struct rpc_task *task)
1486 {
1487         task->tk_rqstp->rq_snd_buf.len = 0;
1488         task->tk_rqstp->rq_bytes_sent = 0;
1489 }
1490
1491 static inline void
1492 rpc_xdr_buf_init(struct xdr_buf *buf, void *start, size_t len)
1493 {
1494         buf->head[0].iov_base = start;
1495         buf->head[0].iov_len = len;
1496         buf->tail[0].iov_len = 0;
1497         buf->page_len = 0;
1498         buf->flags = 0;
1499         buf->len = 0;
1500         buf->buflen = len;
1501 }
1502
1503 /*
1504  * 3.   Encode arguments of an RPC call
1505  */
1506 static void
1507 rpc_xdr_encode(struct rpc_task *task)
1508 {
1509         struct rpc_rqst *req = task->tk_rqstp;
1510         kxdreproc_t     encode;
1511         __be32          *p;
1512
1513         dprint_status(task);
1514
1515         rpc_xdr_buf_init(&req->rq_snd_buf,
1516                          req->rq_buffer,
1517                          req->rq_callsize);
1518         rpc_xdr_buf_init(&req->rq_rcv_buf,
1519                          (char *)req->rq_buffer + req->rq_callsize,
1520                          req->rq_rcvsize);
1521
1522         p = rpc_encode_header(task);
1523         if (p == NULL) {
1524                 printk(KERN_INFO "RPC: couldn't encode RPC header, exit EIO\n");
1525                 rpc_exit(task, -EIO);
1526                 return;
1527         }
1528
1529         encode = task->tk_msg.rpc_proc->p_encode;
1530         if (encode == NULL)
1531                 return;
1532
1533         task->tk_status = rpcauth_wrap_req(task, encode, req, p,
1534                         task->tk_msg.rpc_argp);
1535 }
1536
1537 /*
1538  * 4.   Get the server port number if not yet set
1539  */
1540 static void
1541 call_bind(struct rpc_task *task)
1542 {
1543         struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
1544
1545         dprint_status(task);
1546
1547         task->tk_action = call_connect;
1548         if (!xprt_bound(xprt)) {
1549                 task->tk_action = call_bind_status;
1550                 task->tk_timeout = xprt->bind_timeout;
1551                 xprt->ops->rpcbind(task);
1552         }
1553 }
1554
1555 /*
1556  * 4a.  Sort out bind result
1557  */
1558 static void
1559 call_bind_status(struct rpc_task *task)
1560 {
1561         int status = -EIO;
1562
1563         if (task->tk_status >= 0) {
1564                 dprint_status(task);
1565                 task->tk_status = 0;
1566                 task->tk_action = call_connect;
1567                 return;
1568         }
1569
1570         trace_rpc_bind_status(task);
1571         switch (task->tk_status) {
1572         case -ENOMEM:
1573                 dprintk("RPC: %5u rpcbind out of memory\n", task->tk_pid);
1574                 rpc_delay(task, HZ >> 2);
1575                 goto retry_timeout;
1576         case -EACCES:
1577                 dprintk("RPC: %5u remote rpcbind: RPC program/version "
1578                                 "unavailable\n", task->tk_pid);
1579                 /* fail immediately if this is an RPC ping */
1580                 if (task->tk_msg.rpc_proc->p_proc == 0) {
1581                         status = -EOPNOTSUPP;
1582                         break;
1583                 }
1584                 if (task->tk_rebind_retry == 0)
1585                         break;
1586                 task->tk_rebind_retry--;
1587                 rpc_delay(task, 3*HZ);
1588                 goto retry_timeout;
1589         case -ETIMEDOUT:
1590                 dprintk("RPC: %5u rpcbind request timed out\n",
1591                                 task->tk_pid);
1592                 goto retry_timeout;
1593         case -EPFNOSUPPORT:
1594                 /* server doesn't support any rpcbind version we know of */
1595                 dprintk("RPC: %5u unrecognized remote rpcbind service\n",
1596                                 task->tk_pid);
1597                 break;
1598         case -EPROTONOSUPPORT:
1599                 dprintk("RPC: %5u remote rpcbind version unavailable, retrying\n",
1600                                 task->tk_pid);
1601                 task->tk_status = 0;
1602                 task->tk_action = call_bind;
1603                 return;
1604         case -ECONNREFUSED:             /* connection problems */
1605         case -ECONNRESET:
1606         case -ENOTCONN:
1607         case -EHOSTDOWN:
1608         case -EHOSTUNREACH:
1609         case -ENETUNREACH:
1610         case -EPIPE:
1611                 dprintk("RPC: %5u remote rpcbind unreachable: %d\n",
1612                                 task->tk_pid, task->tk_status);
1613                 if (!RPC_IS_SOFTCONN(task)) {
1614                         rpc_delay(task, 5*HZ);
1615                         goto retry_timeout;
1616                 }
1617                 status = task->tk_status;
1618                 break;
1619         default:
1620                 dprintk("RPC: %5u unrecognized rpcbind error (%d)\n",
1621                                 task->tk_pid, -task->tk_status);
1622         }
1623
1624         rpc_exit(task, status);
1625         return;
1626
1627 retry_timeout:
1628         task->tk_action = call_timeout;
1629 }
1630
1631 /*
1632  * 4b.  Connect to the RPC server
1633  */
1634 static void
1635 call_connect(struct rpc_task *task)
1636 {
1637         struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
1638
1639         dprintk("RPC: %5u call_connect xprt %p %s connected\n",
1640                         task->tk_pid, xprt,
1641                         (xprt_connected(xprt) ? "is" : "is not"));
1642
1643         task->tk_action = call_transmit;
1644         if (!xprt_connected(xprt)) {
1645                 task->tk_action = call_connect_status;
1646                 if (task->tk_status < 0)
1647                         return;
1648                 xprt_connect(task);
1649         }
1650 }
1651
1652 /*
1653  * 4c.  Sort out connect result
1654  */
1655 static void
1656 call_connect_status(struct rpc_task *task)
1657 {
1658         struct rpc_clnt *clnt = task->tk_client;
1659         int status = task->tk_status;
1660
1661         dprint_status(task);
1662
1663         trace_rpc_connect_status(task, status);
1664         switch (status) {
1665                 /* if soft mounted, test if we've timed out */
1666         case -ETIMEDOUT:
1667                 task->tk_action = call_timeout;
1668                 return;
1669         case -ECONNREFUSED:
1670         case -ECONNRESET:
1671         case -ENETUNREACH:
1672                 if (RPC_IS_SOFTCONN(task))
1673                         break;
1674                 /* retry with existing socket, after a delay */
1675         case 0:
1676         case -EAGAIN:
1677                 task->tk_status = 0;
1678                 clnt->cl_stats->netreconn++;
1679                 task->tk_action = call_transmit;
1680                 return;
1681         }
1682         rpc_exit(task, status);
1683 }
1684
1685 /*
1686  * 5.   Transmit the RPC request, and wait for reply
1687  */
1688 static void
1689 call_transmit(struct rpc_task *task)
1690 {
1691         dprint_status(task);
1692
1693         task->tk_action = call_status;
1694         if (task->tk_status < 0)
1695                 return;
1696         task->tk_status = xprt_prepare_transmit(task);
1697         if (task->tk_status != 0)
1698                 return;
1699         task->tk_action = call_transmit_status;
1700         /* Encode here so that rpcsec_gss can use correct sequence number. */
1701         if (rpc_task_need_encode(task)) {
1702                 rpc_xdr_encode(task);
1703                 /* Did the encode result in an error condition? */
1704                 if (task->tk_status != 0) {
1705                         /* Was the error nonfatal? */
1706                         if (task->tk_status == -EAGAIN)
1707                                 rpc_delay(task, HZ >> 4);
1708                         else
1709                                 rpc_exit(task, task->tk_status);
1710                         return;
1711                 }
1712         }
1713         xprt_transmit(task);
1714         if (task->tk_status < 0)
1715                 return;
1716         /*
1717          * On success, ensure that we call xprt_end_transmit() before sleeping
1718          * in order to allow access to the socket to other RPC requests.
1719          */
1720         call_transmit_status(task);
1721         if (rpc_reply_expected(task))
1722                 return;
1723         task->tk_action = rpc_exit_task;
1724         rpc_wake_up_queued_task(&task->tk_rqstp->rq_xprt->pending, task);
1725 }
1726
1727 /*
1728  * 5a.  Handle cleanup after a transmission
1729  */
1730 static void
1731 call_transmit_status(struct rpc_task *task)
1732 {
1733         task->tk_action = call_status;
1734
1735         /*
1736          * Common case: success.  Force the compiler to put this
1737          * test first.
1738          */
1739         if (task->tk_status == 0) {
1740                 xprt_end_transmit(task);
1741                 rpc_task_force_reencode(task);
1742                 return;
1743         }
1744
1745         switch (task->tk_status) {
1746         case -EAGAIN:
1747                 break;
1748         default:
1749                 dprint_status(task);
1750                 xprt_end_transmit(task);
1751                 rpc_task_force_reencode(task);
1752                 break;
1753                 /*
1754                  * Special cases: if we've been waiting on the
1755                  * socket's write_space() callback, or if the
1756                  * socket just returned a connection error,
1757                  * then hold onto the transport lock.
1758                  */
1759         case -ECONNREFUSED:
1760         case -EHOSTDOWN:
1761         case -EHOSTUNREACH:
1762         case -ENETUNREACH:
1763                 if (RPC_IS_SOFTCONN(task)) {
1764                         xprt_end_transmit(task);
1765                         rpc_exit(task, task->tk_status);
1766                         break;
1767                 }
1768         case -ECONNRESET:
1769         case -ENOTCONN:
1770         case -EPIPE:
1771                 rpc_task_force_reencode(task);
1772         }
1773 }
1774
1775 #if defined(CONFIG_SUNRPC_BACKCHANNEL)
1776 /*
1777  * 5b.  Send the backchannel RPC reply.  On error, drop the reply.  In
1778  * addition, disconnect on connectivity errors.
1779  */
1780 static void
1781 call_bc_transmit(struct rpc_task *task)
1782 {
1783         struct rpc_rqst *req = task->tk_rqstp;
1784
1785         task->tk_status = xprt_prepare_transmit(task);
1786         if (task->tk_status == -EAGAIN) {
1787                 /*
1788                  * Could not reserve the transport. Try again after the
1789                  * transport is released.
1790                  */
1791                 task->tk_status = 0;
1792                 task->tk_action = call_bc_transmit;
1793                 return;
1794         }
1795
1796         task->tk_action = rpc_exit_task;
1797         if (task->tk_status < 0) {
1798                 printk(KERN_NOTICE "RPC: Could not send backchannel reply "
1799                         "error: %d\n", task->tk_status);
1800                 return;
1801         }
1802
1803         xprt_transmit(task);
1804         xprt_end_transmit(task);
1805         dprint_status(task);
1806         switch (task->tk_status) {
1807         case 0:
1808                 /* Success */
1809                 break;
1810         case -EHOSTDOWN:
1811         case -EHOSTUNREACH:
1812         case -ENETUNREACH:
1813         case -ETIMEDOUT:
1814                 /*
1815                  * Problem reaching the server.  Disconnect and let the
1816                  * forechannel reestablish the connection.  The server will
1817                  * have to retransmit the backchannel request and we'll
1818                  * reprocess it.  Since these ops are idempotent, there's no
1819                  * need to cache our reply at this time.
1820                  */
1821                 printk(KERN_NOTICE "RPC: Could not send backchannel reply "
1822                         "error: %d\n", task->tk_status);
1823                 xprt_conditional_disconnect(req->rq_xprt,
1824                         req->rq_connect_cookie);
1825                 break;
1826         default:
1827                 /*
1828                  * We were unable to reply and will have to drop the
1829                  * request.  The server should reconnect and retransmit.
1830                  */
1831                 WARN_ON_ONCE(task->tk_status == -EAGAIN);
1832                 printk(KERN_NOTICE "RPC: Could not send backchannel reply "
1833                         "error: %d\n", task->tk_status);
1834                 break;
1835         }
1836         rpc_wake_up_queued_task(&req->rq_xprt->pending, task);
1837 }
1838 #endif /* CONFIG_SUNRPC_BACKCHANNEL */
1839
1840 /*
1841  * 6.   Sort out the RPC call status
1842  */
1843 static void
1844 call_status(struct rpc_task *task)
1845 {
1846         struct rpc_clnt *clnt = task->tk_client;
1847         struct rpc_rqst *req = task->tk_rqstp;
1848         int             status;
1849
1850         if (req->rq_reply_bytes_recvd > 0 && !req->rq_bytes_sent)
1851                 task->tk_status = req->rq_reply_bytes_recvd;
1852
1853         dprint_status(task);
1854
1855         status = task->tk_status;
1856         if (status >= 0) {
1857                 task->tk_action = call_decode;
1858                 return;
1859         }
1860
1861         trace_rpc_call_status(task);
1862         task->tk_status = 0;
1863         switch(status) {
1864         case -EHOSTDOWN:
1865         case -EHOSTUNREACH:
1866         case -ENETUNREACH:
1867                 /*
1868                  * Delay any retries for 3 seconds, then handle as if it
1869                  * were a timeout.
1870                  */
1871                 rpc_delay(task, 3*HZ);
1872         case -ETIMEDOUT:
1873                 task->tk_action = call_timeout;
1874                 if (task->tk_client->cl_discrtry)
1875                         xprt_conditional_disconnect(req->rq_xprt,
1876                                         req->rq_connect_cookie);
1877                 break;
1878         case -ECONNRESET:
1879         case -ECONNREFUSED:
1880                 rpc_force_rebind(clnt);
1881                 rpc_delay(task, 3*HZ);
1882         case -EPIPE:
1883         case -ENOTCONN:
1884                 task->tk_action = call_bind;
1885                 break;
1886         case -EAGAIN:
1887                 task->tk_action = call_transmit;
1888                 break;
1889         case -EIO:
1890                 /* shutdown or soft timeout */
1891                 rpc_exit(task, status);
1892                 break;
1893         default:
1894                 if (clnt->cl_chatty)
1895                         printk("%s: RPC call returned error %d\n",
1896                                clnt->cl_program->name, -status);
1897                 rpc_exit(task, status);
1898         }
1899 }
1900
1901 /*
1902  * 6a.  Handle RPC timeout
1903  *      We do not release the request slot, so we keep using the
1904  *      same XID for all retransmits.
1905  */
1906 static void
1907 call_timeout(struct rpc_task *task)
1908 {
1909         struct rpc_clnt *clnt = task->tk_client;
1910
1911         if (xprt_adjust_timeout(task->tk_rqstp) == 0) {
1912                 dprintk("RPC: %5u call_timeout (minor)\n", task->tk_pid);
1913                 goto retry;
1914         }
1915
1916         dprintk("RPC: %5u call_timeout (major)\n", task->tk_pid);
1917         task->tk_timeouts++;
1918
1919         if (RPC_IS_SOFTCONN(task)) {
1920                 rpc_exit(task, -ETIMEDOUT);
1921                 return;
1922         }
1923         if (RPC_IS_SOFT(task)) {
1924                 if (clnt->cl_chatty) {
1925                         rcu_read_lock();
1926                         printk(KERN_NOTICE "%s: server %s not responding, timed out\n",
1927                                 clnt->cl_program->name,
1928                                 rcu_dereference(clnt->cl_xprt)->servername);
1929                         rcu_read_unlock();
1930                 }
1931                 if (task->tk_flags & RPC_TASK_TIMEOUT)
1932                         rpc_exit(task, -ETIMEDOUT);
1933                 else
1934                         rpc_exit(task, -EIO);
1935                 return;
1936         }
1937
1938         if (!(task->tk_flags & RPC_CALL_MAJORSEEN)) {
1939                 task->tk_flags |= RPC_CALL_MAJORSEEN;
1940                 if (clnt->cl_chatty) {
1941                         rcu_read_lock();
1942                         printk(KERN_NOTICE "%s: server %s not responding, still trying\n",
1943                         clnt->cl_program->name,
1944                         rcu_dereference(clnt->cl_xprt)->servername);
1945                         rcu_read_unlock();
1946                 }
1947         }
1948         rpc_force_rebind(clnt);
1949         /*
1950          * Did our request time out due to an RPCSEC_GSS out-of-sequence
1951          * event? RFC2203 requires the server to drop all such requests.
1952          */
1953         rpcauth_invalcred(task);
1954
1955 retry:
1956         clnt->cl_stats->rpcretrans++;
1957         task->tk_action = call_bind;
1958         task->tk_status = 0;
1959 }
1960
1961 /*
1962  * 7.   Decode the RPC reply
1963  */
1964 static void
1965 call_decode(struct rpc_task *task)
1966 {
1967         struct rpc_clnt *clnt = task->tk_client;
1968         struct rpc_rqst *req = task->tk_rqstp;
1969         kxdrdproc_t     decode = task->tk_msg.rpc_proc->p_decode;
1970         __be32          *p;
1971
1972         dprint_status(task);
1973
1974         if (task->tk_flags & RPC_CALL_MAJORSEEN) {
1975                 if (clnt->cl_chatty) {
1976                         rcu_read_lock();
1977                         printk(KERN_NOTICE "%s: server %s OK\n",
1978                                 clnt->cl_program->name,
1979                                 rcu_dereference(clnt->cl_xprt)->servername);
1980                         rcu_read_unlock();
1981                 }
1982                 task->tk_flags &= ~RPC_CALL_MAJORSEEN;
1983         }
1984
1985         /*
1986          * Ensure that we see all writes made by xprt_complete_rqst()
1987          * before it changed req->rq_reply_bytes_recvd.
1988          */
1989         smp_rmb();
1990         req->rq_rcv_buf.len = req->rq_private_buf.len;
1991
1992         /* Check that the softirq receive buffer is valid */
1993         WARN_ON(memcmp(&req->rq_rcv_buf, &req->rq_private_buf,
1994                                 sizeof(req->rq_rcv_buf)) != 0);
1995
1996         if (req->rq_rcv_buf.len < 12) {
1997                 if (!RPC_IS_SOFT(task)) {
1998                         task->tk_action = call_bind;
1999                         clnt->cl_stats->rpcretrans++;
2000                         goto out_retry;
2001                 }
2002                 dprintk("RPC:       %s: too small RPC reply size (%d bytes)\n",
2003                                 clnt->cl_program->name, task->tk_status);
2004                 task->tk_action = call_timeout;
2005                 goto out_retry;
2006         }
2007
2008         p = rpc_verify_header(task);
2009         if (IS_ERR(p)) {
2010                 if (p == ERR_PTR(-EAGAIN))
2011                         goto out_retry;
2012                 return;
2013         }
2014
2015         task->tk_action = rpc_exit_task;
2016
2017         if (decode) {
2018                 task->tk_status = rpcauth_unwrap_resp(task, decode, req, p,
2019                                                       task->tk_msg.rpc_resp);
2020         }
2021         dprintk("RPC: %5u call_decode result %d\n", task->tk_pid,
2022                         task->tk_status);
2023         return;
2024 out_retry:
2025         task->tk_status = 0;
2026         /* Note: rpc_verify_header() may have freed the RPC slot */
2027         if (task->tk_rqstp == req) {
2028                 req->rq_reply_bytes_recvd = req->rq_rcv_buf.len = 0;
2029                 if (task->tk_client->cl_discrtry)
2030                         xprt_conditional_disconnect(req->rq_xprt,
2031                                         req->rq_connect_cookie);
2032         }
2033 }
2034
2035 static __be32 *
2036 rpc_encode_header(struct rpc_task *task)
2037 {
2038         struct rpc_clnt *clnt = task->tk_client;
2039         struct rpc_rqst *req = task->tk_rqstp;
2040         __be32          *p = req->rq_svec[0].iov_base;
2041
2042         /* FIXME: check buffer size? */
2043
2044         p = xprt_skip_transport_header(req->rq_xprt, p);
2045         *p++ = req->rq_xid;             /* XID */
2046         *p++ = htonl(RPC_CALL);         /* CALL */
2047         *p++ = htonl(RPC_VERSION);      /* RPC version */
2048         *p++ = htonl(clnt->cl_prog);    /* program number */
2049         *p++ = htonl(clnt->cl_vers);    /* program version */
2050         *p++ = htonl(task->tk_msg.rpc_proc->p_proc);    /* procedure */
2051         p = rpcauth_marshcred(task, p);
2052         req->rq_slen = xdr_adjust_iovec(&req->rq_svec[0], p);
2053         return p;
2054 }
2055
2056 static __be32 *
2057 rpc_verify_header(struct rpc_task *task)
2058 {
2059         struct rpc_clnt *clnt = task->tk_client;
2060         struct kvec *iov = &task->tk_rqstp->rq_rcv_buf.head[0];
2061         int len = task->tk_rqstp->rq_rcv_buf.len >> 2;
2062         __be32  *p = iov->iov_base;
2063         u32 n;
2064         int error = -EACCES;
2065
2066         if ((task->tk_rqstp->rq_rcv_buf.len & 3) != 0) {
2067                 /* RFC-1014 says that the representation of XDR data must be a
2068                  * multiple of four bytes
2069                  * - if it isn't pointer subtraction in the NFS client may give
2070                  *   undefined results
2071                  */
2072                 dprintk("RPC: %5u %s: XDR representation not a multiple of"
2073                        " 4 bytes: 0x%x\n", task->tk_pid, __func__,
2074                        task->tk_rqstp->rq_rcv_buf.len);
2075                 goto out_eio;
2076         }
2077         if ((len -= 3) < 0)
2078                 goto out_overflow;
2079
2080         p += 1; /* skip XID */
2081         if ((n = ntohl(*p++)) != RPC_REPLY) {
2082                 dprintk("RPC: %5u %s: not an RPC reply: %x\n",
2083                         task->tk_pid, __func__, n);
2084                 goto out_garbage;
2085         }
2086
2087         if ((n = ntohl(*p++)) != RPC_MSG_ACCEPTED) {
2088                 if (--len < 0)
2089                         goto out_overflow;
2090                 switch ((n = ntohl(*p++))) {
2091                 case RPC_AUTH_ERROR:
2092                         break;
2093                 case RPC_MISMATCH:
2094                         dprintk("RPC: %5u %s: RPC call version mismatch!\n",
2095                                 task->tk_pid, __func__);
2096                         error = -EPROTONOSUPPORT;
2097                         goto out_err;
2098                 default:
2099                         dprintk("RPC: %5u %s: RPC call rejected, "
2100                                 "unknown error: %x\n",
2101                                 task->tk_pid, __func__, n);
2102                         goto out_eio;
2103                 }
2104                 if (--len < 0)
2105                         goto out_overflow;
2106                 switch ((n = ntohl(*p++))) {
2107                 case RPC_AUTH_REJECTEDCRED:
2108                 case RPC_AUTH_REJECTEDVERF:
2109                 case RPCSEC_GSS_CREDPROBLEM:
2110                 case RPCSEC_GSS_CTXPROBLEM:
2111                         if (!task->tk_cred_retry)
2112                                 break;
2113                         task->tk_cred_retry--;
2114                         dprintk("RPC: %5u %s: retry stale creds\n",
2115                                         task->tk_pid, __func__);
2116                         rpcauth_invalcred(task);
2117                         /* Ensure we obtain a new XID! */
2118                         xprt_release(task);
2119                         task->tk_action = call_reserve;
2120                         goto out_retry;
2121                 case RPC_AUTH_BADCRED:
2122                 case RPC_AUTH_BADVERF:
2123                         /* possibly garbled cred/verf? */
2124                         if (!task->tk_garb_retry)
2125                                 break;
2126                         task->tk_garb_retry--;
2127                         dprintk("RPC: %5u %s: retry garbled creds\n",
2128                                         task->tk_pid, __func__);
2129                         task->tk_action = call_bind;
2130                         goto out_retry;
2131                 case RPC_AUTH_TOOWEAK:
2132                         rcu_read_lock();
2133                         printk(KERN_NOTICE "RPC: server %s requires stronger "
2134                                "authentication.\n",
2135                                rcu_dereference(clnt->cl_xprt)->servername);
2136                         rcu_read_unlock();
2137                         break;
2138                 default:
2139                         dprintk("RPC: %5u %s: unknown auth error: %x\n",
2140                                         task->tk_pid, __func__, n);
2141                         error = -EIO;
2142                 }
2143                 dprintk("RPC: %5u %s: call rejected %d\n",
2144                                 task->tk_pid, __func__, n);
2145                 goto out_err;
2146         }
2147         if (!(p = rpcauth_checkverf(task, p))) {
2148                 dprintk("RPC: %5u %s: auth check failed\n",
2149                                 task->tk_pid, __func__);
2150                 goto out_garbage;               /* bad verifier, retry */
2151         }
2152         len = p - (__be32 *)iov->iov_base - 1;
2153         if (len < 0)
2154                 goto out_overflow;
2155         switch ((n = ntohl(*p++))) {
2156         case RPC_SUCCESS:
2157                 return p;
2158         case RPC_PROG_UNAVAIL:
2159                 dprintk_rcu("RPC: %5u %s: program %u is unsupported "
2160                                 "by server %s\n", task->tk_pid, __func__,
2161                                 (unsigned int)clnt->cl_prog,
2162                                 rcu_dereference(clnt->cl_xprt)->servername);
2163                 error = -EPFNOSUPPORT;
2164                 goto out_err;
2165         case RPC_PROG_MISMATCH:
2166                 dprintk_rcu("RPC: %5u %s: program %u, version %u unsupported "
2167                                 "by server %s\n", task->tk_pid, __func__,
2168                                 (unsigned int)clnt->cl_prog,
2169                                 (unsigned int)clnt->cl_vers,
2170                                 rcu_dereference(clnt->cl_xprt)->servername);
2171                 error = -EPROTONOSUPPORT;
2172                 goto out_err;
2173         case RPC_PROC_UNAVAIL:
2174                 dprintk_rcu("RPC: %5u %s: proc %s unsupported by program %u, "
2175                                 "version %u on server %s\n",
2176                                 task->tk_pid, __func__,
2177                                 rpc_proc_name(task),
2178                                 clnt->cl_prog, clnt->cl_vers,
2179                                 rcu_dereference(clnt->cl_xprt)->servername);
2180                 error = -EOPNOTSUPP;
2181                 goto out_err;
2182         case RPC_GARBAGE_ARGS:
2183                 dprintk("RPC: %5u %s: server saw garbage\n",
2184                                 task->tk_pid, __func__);
2185                 break;                  /* retry */
2186         default:
2187                 dprintk("RPC: %5u %s: server accept status: %x\n",
2188                                 task->tk_pid, __func__, n);
2189                 /* Also retry */
2190         }
2191
2192 out_garbage:
2193         clnt->cl_stats->rpcgarbage++;
2194         if (task->tk_garb_retry) {
2195                 task->tk_garb_retry--;
2196                 dprintk("RPC: %5u %s: retrying\n",
2197                                 task->tk_pid, __func__);
2198                 task->tk_action = call_bind;
2199 out_retry:
2200                 return ERR_PTR(-EAGAIN);
2201         }
2202 out_eio:
2203         error = -EIO;
2204 out_err:
2205         rpc_exit(task, error);
2206         dprintk("RPC: %5u %s: call failed with error %d\n", task->tk_pid,
2207                         __func__, error);
2208         return ERR_PTR(error);
2209 out_overflow:
2210         dprintk("RPC: %5u %s: server reply was truncated.\n", task->tk_pid,
2211                         __func__);
2212         goto out_garbage;
2213 }
2214
2215 static void rpcproc_encode_null(void *rqstp, struct xdr_stream *xdr, void *obj)
2216 {
2217 }
2218
2219 static int rpcproc_decode_null(void *rqstp, struct xdr_stream *xdr, void *obj)
2220 {
2221         return 0;
2222 }
2223
2224 static struct rpc_procinfo rpcproc_null = {
2225         .p_encode = rpcproc_encode_null,
2226         .p_decode = rpcproc_decode_null,
2227 };
2228
2229 static int rpc_ping(struct rpc_clnt *clnt)
2230 {
2231         struct rpc_message msg = {
2232                 .rpc_proc = &rpcproc_null,
2233         };
2234         int err;
2235         msg.rpc_cred = authnull_ops.lookup_cred(NULL, NULL, 0);
2236         err = rpc_call_sync(clnt, &msg, RPC_TASK_SOFT | RPC_TASK_SOFTCONN);
2237         put_rpccred(msg.rpc_cred);
2238         return err;
2239 }
2240
2241 struct rpc_task *rpc_call_null(struct rpc_clnt *clnt, struct rpc_cred *cred, int flags)
2242 {
2243         struct rpc_message msg = {
2244                 .rpc_proc = &rpcproc_null,
2245                 .rpc_cred = cred,
2246         };
2247         struct rpc_task_setup task_setup_data = {
2248                 .rpc_client = clnt,
2249                 .rpc_message = &msg,
2250                 .callback_ops = &rpc_default_ops,
2251                 .flags = flags,
2252         };
2253         return rpc_run_task(&task_setup_data);
2254 }
2255 EXPORT_SYMBOL_GPL(rpc_call_null);
2256
2257 #ifdef RPC_DEBUG
2258 static void rpc_show_header(void)
2259 {
2260         printk(KERN_INFO "-pid- flgs status -client- --rqstp- "
2261                 "-timeout ---ops--\n");
2262 }
2263
2264 static void rpc_show_task(const struct rpc_clnt *clnt,
2265                           const struct rpc_task *task)
2266 {
2267         const char *rpc_waitq = "none";
2268
2269         if (RPC_IS_QUEUED(task))
2270                 rpc_waitq = rpc_qname(task->tk_waitqueue);
2271
2272         printk(KERN_INFO "%5u %04x %6d %8p %8p %8ld %8p %sv%u %s a:%ps q:%s\n",
2273                 task->tk_pid, task->tk_flags, task->tk_status,
2274                 clnt, task->tk_rqstp, task->tk_timeout, task->tk_ops,
2275                 clnt->cl_program->name, clnt->cl_vers, rpc_proc_name(task),
2276                 task->tk_action, rpc_waitq);
2277 }
2278
2279 void rpc_show_tasks(struct net *net)
2280 {
2281         struct rpc_clnt *clnt;
2282         struct rpc_task *task;
2283         int header = 0;
2284         struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
2285
2286         spin_lock(&sn->rpc_client_lock);
2287         list_for_each_entry(clnt, &sn->all_clients, cl_clients) {
2288                 spin_lock(&clnt->cl_lock);
2289                 list_for_each_entry(task, &clnt->cl_tasks, tk_task) {
2290                         if (!header) {
2291                                 rpc_show_header();
2292                                 header++;
2293                         }
2294                         rpc_show_task(clnt, task);
2295                 }
2296                 spin_unlock(&clnt->cl_lock);
2297         }
2298         spin_unlock(&sn->rpc_client_lock);
2299 }
2300 #endif