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