SUNRPC: Ensure that the gssproxy client can start in a connected state
[platform/kernel/linux-rpi.git] / net / sunrpc / clnt.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  linux/net/sunrpc/clnt.c
4  *
5  *  This file contains the high-level RPC interface.
6  *  It is modeled as a finite state machine to support both synchronous
7  *  and asynchronous requests.
8  *
9  *  -   RPC header generation and argument serialization.
10  *  -   Credential refresh.
11  *  -   TCP connect handling.
12  *  -   Retry of operation when it is suspected the operation failed because
13  *      of uid squashing on the server, or when the credentials were stale
14  *      and need to be refreshed, or when a packet was damaged in transit.
15  *      This may be have to be moved to the VFS layer.
16  *
17  *  Copyright (C) 1992,1993 Rick Sladkey <jrs@world.std.com>
18  *  Copyright (C) 1995,1996 Olaf Kirch <okir@monad.swb.de>
19  */
20
21
22 #include <linux/module.h>
23 #include <linux/types.h>
24 #include <linux/kallsyms.h>
25 #include <linux/mm.h>
26 #include <linux/namei.h>
27 #include <linux/mount.h>
28 #include <linux/slab.h>
29 #include <linux/rcupdate.h>
30 #include <linux/utsname.h>
31 #include <linux/workqueue.h>
32 #include <linux/in.h>
33 #include <linux/in6.h>
34 #include <linux/un.h>
35
36 #include <linux/sunrpc/clnt.h>
37 #include <linux/sunrpc/addr.h>
38 #include <linux/sunrpc/rpc_pipe_fs.h>
39 #include <linux/sunrpc/metrics.h>
40 #include <linux/sunrpc/bc_xprt.h>
41 #include <trace/events/sunrpc.h>
42
43 #include "sunrpc.h"
44 #include "sysfs.h"
45 #include "netns.h"
46
47 #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
48 # define RPCDBG_FACILITY        RPCDBG_CALL
49 #endif
50
51 /*
52  * All RPC clients are linked into this list
53  */
54
55 static DECLARE_WAIT_QUEUE_HEAD(destroy_wait);
56
57
58 static void     call_start(struct rpc_task *task);
59 static void     call_reserve(struct rpc_task *task);
60 static void     call_reserveresult(struct rpc_task *task);
61 static void     call_allocate(struct rpc_task *task);
62 static void     call_encode(struct rpc_task *task);
63 static void     call_decode(struct rpc_task *task);
64 static void     call_bind(struct rpc_task *task);
65 static void     call_bind_status(struct rpc_task *task);
66 static void     call_transmit(struct rpc_task *task);
67 static void     call_status(struct rpc_task *task);
68 static void     call_transmit_status(struct rpc_task *task);
69 static void     call_refresh(struct rpc_task *task);
70 static void     call_refreshresult(struct rpc_task *task);
71 static void     call_connect(struct rpc_task *task);
72 static void     call_connect_status(struct rpc_task *task);
73
74 static int      rpc_encode_header(struct rpc_task *task,
75                                   struct xdr_stream *xdr);
76 static int      rpc_decode_header(struct rpc_task *task,
77                                   struct xdr_stream *xdr);
78 static int      rpc_ping(struct rpc_clnt *clnt);
79 static int      rpc_ping_noreply(struct rpc_clnt *clnt);
80 static void     rpc_check_timeout(struct rpc_task *task);
81
82 static void rpc_register_client(struct rpc_clnt *clnt)
83 {
84         struct net *net = rpc_net_ns(clnt);
85         struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
86
87         spin_lock(&sn->rpc_client_lock);
88         list_add(&clnt->cl_clients, &sn->all_clients);
89         spin_unlock(&sn->rpc_client_lock);
90 }
91
92 static void rpc_unregister_client(struct rpc_clnt *clnt)
93 {
94         struct net *net = rpc_net_ns(clnt);
95         struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
96
97         spin_lock(&sn->rpc_client_lock);
98         list_del(&clnt->cl_clients);
99         spin_unlock(&sn->rpc_client_lock);
100 }
101
102 static void __rpc_clnt_remove_pipedir(struct rpc_clnt *clnt)
103 {
104         rpc_remove_client_dir(clnt);
105 }
106
107 static void rpc_clnt_remove_pipedir(struct rpc_clnt *clnt)
108 {
109         struct net *net = rpc_net_ns(clnt);
110         struct super_block *pipefs_sb;
111
112         pipefs_sb = rpc_get_sb_net(net);
113         if (pipefs_sb) {
114                 __rpc_clnt_remove_pipedir(clnt);
115                 rpc_put_sb_net(net);
116         }
117 }
118
119 static struct dentry *rpc_setup_pipedir_sb(struct super_block *sb,
120                                     struct rpc_clnt *clnt)
121 {
122         static uint32_t clntid;
123         const char *dir_name = clnt->cl_program->pipe_dir_name;
124         char name[15];
125         struct dentry *dir, *dentry;
126
127         dir = rpc_d_lookup_sb(sb, dir_name);
128         if (dir == NULL) {
129                 pr_info("RPC: pipefs directory doesn't exist: %s\n", dir_name);
130                 return dir;
131         }
132         for (;;) {
133                 snprintf(name, sizeof(name), "clnt%x", (unsigned int)clntid++);
134                 name[sizeof(name) - 1] = '\0';
135                 dentry = rpc_create_client_dir(dir, name, clnt);
136                 if (!IS_ERR(dentry))
137                         break;
138                 if (dentry == ERR_PTR(-EEXIST))
139                         continue;
140                 printk(KERN_INFO "RPC: Couldn't create pipefs entry"
141                                 " %s/%s, error %ld\n",
142                                 dir_name, name, PTR_ERR(dentry));
143                 break;
144         }
145         dput(dir);
146         return dentry;
147 }
148
149 static int
150 rpc_setup_pipedir(struct super_block *pipefs_sb, struct rpc_clnt *clnt)
151 {
152         struct dentry *dentry;
153
154         if (clnt->cl_program->pipe_dir_name != NULL) {
155                 dentry = rpc_setup_pipedir_sb(pipefs_sb, clnt);
156                 if (IS_ERR(dentry))
157                         return PTR_ERR(dentry);
158         }
159         return 0;
160 }
161
162 static int rpc_clnt_skip_event(struct rpc_clnt *clnt, unsigned long event)
163 {
164         if (clnt->cl_program->pipe_dir_name == NULL)
165                 return 1;
166
167         switch (event) {
168         case RPC_PIPEFS_MOUNT:
169                 if (clnt->cl_pipedir_objects.pdh_dentry != NULL)
170                         return 1;
171                 if (refcount_read(&clnt->cl_count) == 0)
172                         return 1;
173                 break;
174         case RPC_PIPEFS_UMOUNT:
175                 if (clnt->cl_pipedir_objects.pdh_dentry == NULL)
176                         return 1;
177                 break;
178         }
179         return 0;
180 }
181
182 static int __rpc_clnt_handle_event(struct rpc_clnt *clnt, unsigned long event,
183                                    struct super_block *sb)
184 {
185         struct dentry *dentry;
186
187         switch (event) {
188         case RPC_PIPEFS_MOUNT:
189                 dentry = rpc_setup_pipedir_sb(sb, clnt);
190                 if (!dentry)
191                         return -ENOENT;
192                 if (IS_ERR(dentry))
193                         return PTR_ERR(dentry);
194                 break;
195         case RPC_PIPEFS_UMOUNT:
196                 __rpc_clnt_remove_pipedir(clnt);
197                 break;
198         default:
199                 printk(KERN_ERR "%s: unknown event: %ld\n", __func__, event);
200                 return -ENOTSUPP;
201         }
202         return 0;
203 }
204
205 static int __rpc_pipefs_event(struct rpc_clnt *clnt, unsigned long event,
206                                 struct super_block *sb)
207 {
208         int error = 0;
209
210         for (;; clnt = clnt->cl_parent) {
211                 if (!rpc_clnt_skip_event(clnt, event))
212                         error = __rpc_clnt_handle_event(clnt, event, sb);
213                 if (error || clnt == clnt->cl_parent)
214                         break;
215         }
216         return error;
217 }
218
219 static struct rpc_clnt *rpc_get_client_for_event(struct net *net, int event)
220 {
221         struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
222         struct rpc_clnt *clnt;
223
224         spin_lock(&sn->rpc_client_lock);
225         list_for_each_entry(clnt, &sn->all_clients, cl_clients) {
226                 if (rpc_clnt_skip_event(clnt, event))
227                         continue;
228                 spin_unlock(&sn->rpc_client_lock);
229                 return clnt;
230         }
231         spin_unlock(&sn->rpc_client_lock);
232         return NULL;
233 }
234
235 static int rpc_pipefs_event(struct notifier_block *nb, unsigned long event,
236                             void *ptr)
237 {
238         struct super_block *sb = ptr;
239         struct rpc_clnt *clnt;
240         int error = 0;
241
242         while ((clnt = rpc_get_client_for_event(sb->s_fs_info, event))) {
243                 error = __rpc_pipefs_event(clnt, event, sb);
244                 if (error)
245                         break;
246         }
247         return error;
248 }
249
250 static struct notifier_block rpc_clients_block = {
251         .notifier_call  = rpc_pipefs_event,
252         .priority       = SUNRPC_PIPEFS_RPC_PRIO,
253 };
254
255 int rpc_clients_notifier_register(void)
256 {
257         return rpc_pipefs_notifier_register(&rpc_clients_block);
258 }
259
260 void rpc_clients_notifier_unregister(void)
261 {
262         return rpc_pipefs_notifier_unregister(&rpc_clients_block);
263 }
264
265 static struct rpc_xprt *rpc_clnt_set_transport(struct rpc_clnt *clnt,
266                 struct rpc_xprt *xprt,
267                 const struct rpc_timeout *timeout)
268 {
269         struct rpc_xprt *old;
270
271         spin_lock(&clnt->cl_lock);
272         old = rcu_dereference_protected(clnt->cl_xprt,
273                         lockdep_is_held(&clnt->cl_lock));
274
275         if (!xprt_bound(xprt))
276                 clnt->cl_autobind = 1;
277
278         clnt->cl_timeout = timeout;
279         rcu_assign_pointer(clnt->cl_xprt, xprt);
280         spin_unlock(&clnt->cl_lock);
281
282         return old;
283 }
284
285 static void rpc_clnt_set_nodename(struct rpc_clnt *clnt, const char *nodename)
286 {
287         clnt->cl_nodelen = strlcpy(clnt->cl_nodename,
288                         nodename, sizeof(clnt->cl_nodename));
289 }
290
291 static int rpc_client_register(struct rpc_clnt *clnt,
292                                rpc_authflavor_t pseudoflavor,
293                                const char *client_name)
294 {
295         struct rpc_auth_create_args auth_args = {
296                 .pseudoflavor = pseudoflavor,
297                 .target_name = client_name,
298         };
299         struct rpc_auth *auth;
300         struct net *net = rpc_net_ns(clnt);
301         struct super_block *pipefs_sb;
302         int err;
303
304         rpc_clnt_debugfs_register(clnt);
305
306         pipefs_sb = rpc_get_sb_net(net);
307         if (pipefs_sb) {
308                 err = rpc_setup_pipedir(pipefs_sb, clnt);
309                 if (err)
310                         goto out;
311         }
312
313         rpc_register_client(clnt);
314         if (pipefs_sb)
315                 rpc_put_sb_net(net);
316
317         auth = rpcauth_create(&auth_args, clnt);
318         if (IS_ERR(auth)) {
319                 dprintk("RPC:       Couldn't create auth handle (flavor %u)\n",
320                                 pseudoflavor);
321                 err = PTR_ERR(auth);
322                 goto err_auth;
323         }
324         return 0;
325 err_auth:
326         pipefs_sb = rpc_get_sb_net(net);
327         rpc_unregister_client(clnt);
328         __rpc_clnt_remove_pipedir(clnt);
329 out:
330         if (pipefs_sb)
331                 rpc_put_sb_net(net);
332         rpc_sysfs_client_destroy(clnt);
333         rpc_clnt_debugfs_unregister(clnt);
334         return err;
335 }
336
337 static DEFINE_IDA(rpc_clids);
338
339 void rpc_cleanup_clids(void)
340 {
341         ida_destroy(&rpc_clids);
342 }
343
344 static int rpc_alloc_clid(struct rpc_clnt *clnt)
345 {
346         int clid;
347
348         clid = ida_simple_get(&rpc_clids, 0, 0, GFP_KERNEL);
349         if (clid < 0)
350                 return clid;
351         clnt->cl_clid = clid;
352         return 0;
353 }
354
355 static void rpc_free_clid(struct rpc_clnt *clnt)
356 {
357         ida_simple_remove(&rpc_clids, clnt->cl_clid);
358 }
359
360 static struct rpc_clnt * rpc_new_client(const struct rpc_create_args *args,
361                 struct rpc_xprt_switch *xps,
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         err = rpciod_up();
373         if (err)
374                 goto out_no_rpciod;
375
376         err = -EINVAL;
377         if (args->version >= program->nrvers)
378                 goto out_err;
379         version = program->version[args->version];
380         if (version == NULL)
381                 goto out_err;
382
383         err = -ENOMEM;
384         clnt = kzalloc(sizeof(*clnt), GFP_KERNEL);
385         if (!clnt)
386                 goto out_err;
387         clnt->cl_parent = parent ? : clnt;
388
389         err = rpc_alloc_clid(clnt);
390         if (err)
391                 goto out_no_clid;
392
393         clnt->cl_cred     = get_cred(args->cred);
394         clnt->cl_procinfo = version->procs;
395         clnt->cl_maxproc  = version->nrprocs;
396         clnt->cl_prog     = args->prognumber ? : program->number;
397         clnt->cl_vers     = version->number;
398         clnt->cl_stats    = program->stats;
399         clnt->cl_metrics  = rpc_alloc_iostats(clnt);
400         rpc_init_pipe_dir_head(&clnt->cl_pipedir_objects);
401         err = -ENOMEM;
402         if (clnt->cl_metrics == NULL)
403                 goto out_no_stats;
404         clnt->cl_program  = program;
405         INIT_LIST_HEAD(&clnt->cl_tasks);
406         spin_lock_init(&clnt->cl_lock);
407
408         timeout = xprt->timeout;
409         if (args->timeout != NULL) {
410                 memcpy(&clnt->cl_timeout_default, args->timeout,
411                                 sizeof(clnt->cl_timeout_default));
412                 timeout = &clnt->cl_timeout_default;
413         }
414
415         rpc_clnt_set_transport(clnt, xprt, timeout);
416         xprt->main = true;
417         xprt_iter_init(&clnt->cl_xpi, xps);
418         xprt_switch_put(xps);
419
420         clnt->cl_rtt = &clnt->cl_rtt_default;
421         rpc_init_rtt(&clnt->cl_rtt_default, clnt->cl_timeout->to_initval);
422
423         refcount_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         rpc_sysfs_client_setup(clnt, xps, rpc_net_ns(clnt));
431         err = rpc_client_register(clnt, args->authflavor, args->client_name);
432         if (err)
433                 goto out_no_path;
434         if (parent)
435                 refcount_inc(&parent->cl_count);
436
437         trace_rpc_clnt_new(clnt, xprt, program->name, args->servername);
438         return clnt;
439
440 out_no_path:
441         rpc_free_iostats(clnt->cl_metrics);
442 out_no_stats:
443         put_cred(clnt->cl_cred);
444         rpc_free_clid(clnt);
445 out_no_clid:
446         kfree(clnt);
447 out_err:
448         rpciod_down();
449 out_no_rpciod:
450         xprt_switch_put(xps);
451         xprt_put(xprt);
452         trace_rpc_clnt_new_err(program->name, args->servername, err);
453         return ERR_PTR(err);
454 }
455
456 static struct rpc_clnt *rpc_create_xprt(struct rpc_create_args *args,
457                                         struct rpc_xprt *xprt)
458 {
459         struct rpc_clnt *clnt = NULL;
460         struct rpc_xprt_switch *xps;
461
462         if (args->bc_xprt && args->bc_xprt->xpt_bc_xps) {
463                 WARN_ON_ONCE(!(args->protocol & XPRT_TRANSPORT_BC));
464                 xps = args->bc_xprt->xpt_bc_xps;
465                 xprt_switch_get(xps);
466         } else {
467                 xps = xprt_switch_alloc(xprt, GFP_KERNEL);
468                 if (xps == NULL) {
469                         xprt_put(xprt);
470                         return ERR_PTR(-ENOMEM);
471                 }
472                 if (xprt->bc_xprt) {
473                         xprt_switch_get(xps);
474                         xprt->bc_xprt->xpt_bc_xps = xps;
475                 }
476         }
477         clnt = rpc_new_client(args, xps, xprt, NULL);
478         if (IS_ERR(clnt))
479                 return clnt;
480
481         if (!(args->flags & RPC_CLNT_CREATE_NOPING)) {
482                 int err = rpc_ping(clnt);
483                 if (err != 0) {
484                         rpc_shutdown_client(clnt);
485                         return ERR_PTR(err);
486                 }
487         } else if (args->flags & RPC_CLNT_CREATE_CONNECTED) {
488                 int err = rpc_ping_noreply(clnt);
489                 if (err != 0) {
490                         rpc_shutdown_client(clnt);
491                         return ERR_PTR(err);
492                 }
493         }
494
495         clnt->cl_softrtry = 1;
496         if (args->flags & (RPC_CLNT_CREATE_HARDRTRY|RPC_CLNT_CREATE_SOFTERR)) {
497                 clnt->cl_softrtry = 0;
498                 if (args->flags & RPC_CLNT_CREATE_SOFTERR)
499                         clnt->cl_softerr = 1;
500         }
501
502         if (args->flags & RPC_CLNT_CREATE_AUTOBIND)
503                 clnt->cl_autobind = 1;
504         if (args->flags & RPC_CLNT_CREATE_NO_RETRANS_TIMEOUT)
505                 clnt->cl_noretranstimeo = 1;
506         if (args->flags & RPC_CLNT_CREATE_DISCRTRY)
507                 clnt->cl_discrtry = 1;
508         if (!(args->flags & RPC_CLNT_CREATE_QUIET))
509                 clnt->cl_chatty = 1;
510
511         return clnt;
512 }
513
514 /**
515  * rpc_create - create an RPC client and transport with one call
516  * @args: rpc_clnt create argument structure
517  *
518  * Creates and initializes an RPC transport and an RPC client.
519  *
520  * It can ping the server in order to determine if it is up, and to see if
521  * it supports this program and version.  RPC_CLNT_CREATE_NOPING disables
522  * this behavior so asynchronous tasks can also use rpc_create.
523  */
524 struct rpc_clnt *rpc_create(struct rpc_create_args *args)
525 {
526         struct rpc_xprt *xprt;
527         struct xprt_create xprtargs = {
528                 .net = args->net,
529                 .ident = args->protocol,
530                 .srcaddr = args->saddress,
531                 .dstaddr = args->address,
532                 .addrlen = args->addrsize,
533                 .servername = args->servername,
534                 .bc_xprt = args->bc_xprt,
535         };
536         char servername[48];
537         struct rpc_clnt *clnt;
538         int i;
539
540         if (args->bc_xprt) {
541                 WARN_ON_ONCE(!(args->protocol & XPRT_TRANSPORT_BC));
542                 xprt = args->bc_xprt->xpt_bc_xprt;
543                 if (xprt) {
544                         xprt_get(xprt);
545                         return rpc_create_xprt(args, xprt);
546                 }
547         }
548
549         if (args->flags & RPC_CLNT_CREATE_INFINITE_SLOTS)
550                 xprtargs.flags |= XPRT_CREATE_INFINITE_SLOTS;
551         if (args->flags & RPC_CLNT_CREATE_NO_IDLE_TIMEOUT)
552                 xprtargs.flags |= XPRT_CREATE_NO_IDLE_TIMEOUT;
553         /*
554          * If the caller chooses not to specify a hostname, whip
555          * up a string representation of the passed-in address.
556          */
557         if (xprtargs.servername == NULL) {
558                 struct sockaddr_un *sun =
559                                 (struct sockaddr_un *)args->address;
560                 struct sockaddr_in *sin =
561                                 (struct sockaddr_in *)args->address;
562                 struct sockaddr_in6 *sin6 =
563                                 (struct sockaddr_in6 *)args->address;
564
565                 servername[0] = '\0';
566                 switch (args->address->sa_family) {
567                 case AF_LOCAL:
568                         snprintf(servername, sizeof(servername), "%s",
569                                  sun->sun_path);
570                         break;
571                 case AF_INET:
572                         snprintf(servername, sizeof(servername), "%pI4",
573                                  &sin->sin_addr.s_addr);
574                         break;
575                 case AF_INET6:
576                         snprintf(servername, sizeof(servername), "%pI6",
577                                  &sin6->sin6_addr);
578                         break;
579                 default:
580                         /* caller wants default server name, but
581                          * address family isn't recognized. */
582                         return ERR_PTR(-EINVAL);
583                 }
584                 xprtargs.servername = servername;
585         }
586
587         xprt = xprt_create_transport(&xprtargs);
588         if (IS_ERR(xprt))
589                 return (struct rpc_clnt *)xprt;
590
591         /*
592          * By default, kernel RPC client connects from a reserved port.
593          * CAP_NET_BIND_SERVICE will not be set for unprivileged requesters,
594          * but it is always enabled for rpciod, which handles the connect
595          * operation.
596          */
597         xprt->resvport = 1;
598         if (args->flags & RPC_CLNT_CREATE_NONPRIVPORT)
599                 xprt->resvport = 0;
600         xprt->reuseport = 0;
601         if (args->flags & RPC_CLNT_CREATE_REUSEPORT)
602                 xprt->reuseport = 1;
603
604         clnt = rpc_create_xprt(args, xprt);
605         if (IS_ERR(clnt) || args->nconnect <= 1)
606                 return clnt;
607
608         for (i = 0; i < args->nconnect - 1; i++) {
609                 if (rpc_clnt_add_xprt(clnt, &xprtargs, NULL, NULL) < 0)
610                         break;
611         }
612         return clnt;
613 }
614 EXPORT_SYMBOL_GPL(rpc_create);
615
616 /*
617  * This function clones the RPC client structure. It allows us to share the
618  * same transport while varying parameters such as the authentication
619  * flavour.
620  */
621 static struct rpc_clnt *__rpc_clone_client(struct rpc_create_args *args,
622                                            struct rpc_clnt *clnt)
623 {
624         struct rpc_xprt_switch *xps;
625         struct rpc_xprt *xprt;
626         struct rpc_clnt *new;
627         int err;
628
629         err = -ENOMEM;
630         rcu_read_lock();
631         xprt = xprt_get(rcu_dereference(clnt->cl_xprt));
632         xps = xprt_switch_get(rcu_dereference(clnt->cl_xpi.xpi_xpswitch));
633         rcu_read_unlock();
634         if (xprt == NULL || xps == NULL) {
635                 xprt_put(xprt);
636                 xprt_switch_put(xps);
637                 goto out_err;
638         }
639         args->servername = xprt->servername;
640         args->nodename = clnt->cl_nodename;
641
642         new = rpc_new_client(args, xps, xprt, clnt);
643         if (IS_ERR(new))
644                 return new;
645
646         /* Turn off autobind on clones */
647         new->cl_autobind = 0;
648         new->cl_softrtry = clnt->cl_softrtry;
649         new->cl_softerr = clnt->cl_softerr;
650         new->cl_noretranstimeo = clnt->cl_noretranstimeo;
651         new->cl_discrtry = clnt->cl_discrtry;
652         new->cl_chatty = clnt->cl_chatty;
653         new->cl_principal = clnt->cl_principal;
654         return new;
655
656 out_err:
657         trace_rpc_clnt_clone_err(clnt, err);
658         return ERR_PTR(err);
659 }
660
661 /**
662  * rpc_clone_client - Clone an RPC client structure
663  *
664  * @clnt: RPC client whose parameters are copied
665  *
666  * Returns a fresh RPC client or an ERR_PTR.
667  */
668 struct rpc_clnt *rpc_clone_client(struct rpc_clnt *clnt)
669 {
670         struct rpc_create_args args = {
671                 .program        = clnt->cl_program,
672                 .prognumber     = clnt->cl_prog,
673                 .version        = clnt->cl_vers,
674                 .authflavor     = clnt->cl_auth->au_flavor,
675                 .cred           = clnt->cl_cred,
676         };
677         return __rpc_clone_client(&args, clnt);
678 }
679 EXPORT_SYMBOL_GPL(rpc_clone_client);
680
681 /**
682  * rpc_clone_client_set_auth - Clone an RPC client structure and set its auth
683  *
684  * @clnt: RPC client whose parameters are copied
685  * @flavor: security flavor for new client
686  *
687  * Returns a fresh RPC client or an ERR_PTR.
688  */
689 struct rpc_clnt *
690 rpc_clone_client_set_auth(struct rpc_clnt *clnt, rpc_authflavor_t flavor)
691 {
692         struct rpc_create_args args = {
693                 .program        = clnt->cl_program,
694                 .prognumber     = clnt->cl_prog,
695                 .version        = clnt->cl_vers,
696                 .authflavor     = flavor,
697                 .cred           = clnt->cl_cred,
698         };
699         return __rpc_clone_client(&args, clnt);
700 }
701 EXPORT_SYMBOL_GPL(rpc_clone_client_set_auth);
702
703 /**
704  * rpc_switch_client_transport: switch the RPC transport on the fly
705  * @clnt: pointer to a struct rpc_clnt
706  * @args: pointer to the new transport arguments
707  * @timeout: pointer to the new timeout parameters
708  *
709  * This function allows the caller to switch the RPC transport for the
710  * rpc_clnt structure 'clnt' to allow it to connect to a mirrored NFS
711  * server, for instance.  It assumes that the caller has ensured that
712  * there are no active RPC tasks by using some form of locking.
713  *
714  * Returns zero if "clnt" is now using the new xprt.  Otherwise a
715  * negative errno is returned, and "clnt" continues to use the old
716  * xprt.
717  */
718 int rpc_switch_client_transport(struct rpc_clnt *clnt,
719                 struct xprt_create *args,
720                 const struct rpc_timeout *timeout)
721 {
722         const struct rpc_timeout *old_timeo;
723         rpc_authflavor_t pseudoflavor;
724         struct rpc_xprt_switch *xps, *oldxps;
725         struct rpc_xprt *xprt, *old;
726         struct rpc_clnt *parent;
727         int err;
728
729         xprt = xprt_create_transport(args);
730         if (IS_ERR(xprt))
731                 return PTR_ERR(xprt);
732
733         xps = xprt_switch_alloc(xprt, GFP_KERNEL);
734         if (xps == NULL) {
735                 xprt_put(xprt);
736                 return -ENOMEM;
737         }
738
739         pseudoflavor = clnt->cl_auth->au_flavor;
740
741         old_timeo = clnt->cl_timeout;
742         old = rpc_clnt_set_transport(clnt, xprt, timeout);
743         oldxps = xprt_iter_xchg_switch(&clnt->cl_xpi, xps);
744
745         rpc_unregister_client(clnt);
746         __rpc_clnt_remove_pipedir(clnt);
747         rpc_sysfs_client_destroy(clnt);
748         rpc_clnt_debugfs_unregister(clnt);
749
750         /*
751          * A new transport was created.  "clnt" therefore
752          * becomes the root of a new cl_parent tree.  clnt's
753          * children, if it has any, still point to the old xprt.
754          */
755         parent = clnt->cl_parent;
756         clnt->cl_parent = clnt;
757
758         /*
759          * The old rpc_auth cache cannot be re-used.  GSS
760          * contexts in particular are between a single
761          * client and server.
762          */
763         err = rpc_client_register(clnt, pseudoflavor, NULL);
764         if (err)
765                 goto out_revert;
766
767         synchronize_rcu();
768         if (parent != clnt)
769                 rpc_release_client(parent);
770         xprt_switch_put(oldxps);
771         xprt_put(old);
772         trace_rpc_clnt_replace_xprt(clnt);
773         return 0;
774
775 out_revert:
776         xps = xprt_iter_xchg_switch(&clnt->cl_xpi, oldxps);
777         rpc_clnt_set_transport(clnt, old, old_timeo);
778         clnt->cl_parent = parent;
779         rpc_client_register(clnt, pseudoflavor, NULL);
780         xprt_switch_put(xps);
781         xprt_put(xprt);
782         trace_rpc_clnt_replace_xprt_err(clnt);
783         return err;
784 }
785 EXPORT_SYMBOL_GPL(rpc_switch_client_transport);
786
787 static
788 int rpc_clnt_xprt_iter_init(struct rpc_clnt *clnt, struct rpc_xprt_iter *xpi)
789 {
790         struct rpc_xprt_switch *xps;
791
792         rcu_read_lock();
793         xps = xprt_switch_get(rcu_dereference(clnt->cl_xpi.xpi_xpswitch));
794         rcu_read_unlock();
795         if (xps == NULL)
796                 return -EAGAIN;
797         xprt_iter_init_listall(xpi, xps);
798         xprt_switch_put(xps);
799         return 0;
800 }
801
802 /**
803  * rpc_clnt_iterate_for_each_xprt - Apply a function to all transports
804  * @clnt: pointer to client
805  * @fn: function to apply
806  * @data: void pointer to function data
807  *
808  * Iterates through the list of RPC transports currently attached to the
809  * client and applies the function fn(clnt, xprt, data).
810  *
811  * On error, the iteration stops, and the function returns the error value.
812  */
813 int rpc_clnt_iterate_for_each_xprt(struct rpc_clnt *clnt,
814                 int (*fn)(struct rpc_clnt *, struct rpc_xprt *, void *),
815                 void *data)
816 {
817         struct rpc_xprt_iter xpi;
818         int ret;
819
820         ret = rpc_clnt_xprt_iter_init(clnt, &xpi);
821         if (ret)
822                 return ret;
823         for (;;) {
824                 struct rpc_xprt *xprt = xprt_iter_get_next(&xpi);
825
826                 if (!xprt)
827                         break;
828                 ret = fn(clnt, xprt, data);
829                 xprt_put(xprt);
830                 if (ret < 0)
831                         break;
832         }
833         xprt_iter_destroy(&xpi);
834         return ret;
835 }
836 EXPORT_SYMBOL_GPL(rpc_clnt_iterate_for_each_xprt);
837
838 /*
839  * Kill all tasks for the given client.
840  * XXX: kill their descendants as well?
841  */
842 void rpc_killall_tasks(struct rpc_clnt *clnt)
843 {
844         struct rpc_task *rovr;
845
846
847         if (list_empty(&clnt->cl_tasks))
848                 return;
849
850         /*
851          * Spin lock all_tasks to prevent changes...
852          */
853         trace_rpc_clnt_killall(clnt);
854         spin_lock(&clnt->cl_lock);
855         list_for_each_entry(rovr, &clnt->cl_tasks, tk_task)
856                 rpc_signal_task(rovr);
857         spin_unlock(&clnt->cl_lock);
858 }
859 EXPORT_SYMBOL_GPL(rpc_killall_tasks);
860
861 /*
862  * Properly shut down an RPC client, terminating all outstanding
863  * requests.
864  */
865 void rpc_shutdown_client(struct rpc_clnt *clnt)
866 {
867         might_sleep();
868
869         trace_rpc_clnt_shutdown(clnt);
870
871         while (!list_empty(&clnt->cl_tasks)) {
872                 rpc_killall_tasks(clnt);
873                 wait_event_timeout(destroy_wait,
874                         list_empty(&clnt->cl_tasks), 1*HZ);
875         }
876
877         rpc_release_client(clnt);
878 }
879 EXPORT_SYMBOL_GPL(rpc_shutdown_client);
880
881 /*
882  * Free an RPC client
883  */
884 static void rpc_free_client_work(struct work_struct *work)
885 {
886         struct rpc_clnt *clnt = container_of(work, struct rpc_clnt, cl_work);
887
888         trace_rpc_clnt_free(clnt);
889
890         /* These might block on processes that might allocate memory,
891          * so they cannot be called in rpciod, so they are handled separately
892          * here.
893          */
894         rpc_sysfs_client_destroy(clnt);
895         rpc_clnt_debugfs_unregister(clnt);
896         rpc_free_clid(clnt);
897         rpc_clnt_remove_pipedir(clnt);
898         xprt_put(rcu_dereference_raw(clnt->cl_xprt));
899
900         kfree(clnt);
901         rpciod_down();
902 }
903 static struct rpc_clnt *
904 rpc_free_client(struct rpc_clnt *clnt)
905 {
906         struct rpc_clnt *parent = NULL;
907
908         trace_rpc_clnt_release(clnt);
909         if (clnt->cl_parent != clnt)
910                 parent = clnt->cl_parent;
911         rpc_unregister_client(clnt);
912         rpc_free_iostats(clnt->cl_metrics);
913         clnt->cl_metrics = NULL;
914         xprt_iter_destroy(&clnt->cl_xpi);
915         put_cred(clnt->cl_cred);
916
917         INIT_WORK(&clnt->cl_work, rpc_free_client_work);
918         schedule_work(&clnt->cl_work);
919         return parent;
920 }
921
922 /*
923  * Free an RPC client
924  */
925 static struct rpc_clnt *
926 rpc_free_auth(struct rpc_clnt *clnt)
927 {
928         /*
929          * Note: RPCSEC_GSS may need to send NULL RPC calls in order to
930          *       release remaining GSS contexts. This mechanism ensures
931          *       that it can do so safely.
932          */
933         if (clnt->cl_auth != NULL) {
934                 rpcauth_release(clnt->cl_auth);
935                 clnt->cl_auth = NULL;
936         }
937         if (refcount_dec_and_test(&clnt->cl_count))
938                 return rpc_free_client(clnt);
939         return NULL;
940 }
941
942 /*
943  * Release reference to the RPC client
944  */
945 void
946 rpc_release_client(struct rpc_clnt *clnt)
947 {
948         do {
949                 if (list_empty(&clnt->cl_tasks))
950                         wake_up(&destroy_wait);
951                 if (refcount_dec_not_one(&clnt->cl_count))
952                         break;
953                 clnt = rpc_free_auth(clnt);
954         } while (clnt != NULL);
955 }
956 EXPORT_SYMBOL_GPL(rpc_release_client);
957
958 /**
959  * rpc_bind_new_program - bind a new RPC program to an existing client
960  * @old: old rpc_client
961  * @program: rpc program to set
962  * @vers: rpc program version
963  *
964  * Clones the rpc client and sets up a new RPC program. This is mainly
965  * of use for enabling different RPC programs to share the same transport.
966  * The Sun NFSv2/v3 ACL protocol can do this.
967  */
968 struct rpc_clnt *rpc_bind_new_program(struct rpc_clnt *old,
969                                       const struct rpc_program *program,
970                                       u32 vers)
971 {
972         struct rpc_create_args args = {
973                 .program        = program,
974                 .prognumber     = program->number,
975                 .version        = vers,
976                 .authflavor     = old->cl_auth->au_flavor,
977                 .cred           = old->cl_cred,
978         };
979         struct rpc_clnt *clnt;
980         int err;
981
982         clnt = __rpc_clone_client(&args, old);
983         if (IS_ERR(clnt))
984                 goto out;
985         err = rpc_ping(clnt);
986         if (err != 0) {
987                 rpc_shutdown_client(clnt);
988                 clnt = ERR_PTR(err);
989         }
990 out:
991         return clnt;
992 }
993 EXPORT_SYMBOL_GPL(rpc_bind_new_program);
994
995 struct rpc_xprt *
996 rpc_task_get_xprt(struct rpc_clnt *clnt, struct rpc_xprt *xprt)
997 {
998         struct rpc_xprt_switch *xps;
999
1000         if (!xprt)
1001                 return NULL;
1002         rcu_read_lock();
1003         xps = rcu_dereference(clnt->cl_xpi.xpi_xpswitch);
1004         atomic_long_inc(&xps->xps_queuelen);
1005         rcu_read_unlock();
1006         atomic_long_inc(&xprt->queuelen);
1007
1008         return xprt;
1009 }
1010
1011 static void
1012 rpc_task_release_xprt(struct rpc_clnt *clnt, struct rpc_xprt *xprt)
1013 {
1014         struct rpc_xprt_switch *xps;
1015
1016         atomic_long_dec(&xprt->queuelen);
1017         rcu_read_lock();
1018         xps = rcu_dereference(clnt->cl_xpi.xpi_xpswitch);
1019         atomic_long_dec(&xps->xps_queuelen);
1020         rcu_read_unlock();
1021
1022         xprt_put(xprt);
1023 }
1024
1025 void rpc_task_release_transport(struct rpc_task *task)
1026 {
1027         struct rpc_xprt *xprt = task->tk_xprt;
1028
1029         if (xprt) {
1030                 task->tk_xprt = NULL;
1031                 if (task->tk_client)
1032                         rpc_task_release_xprt(task->tk_client, xprt);
1033                 else
1034                         xprt_put(xprt);
1035         }
1036 }
1037 EXPORT_SYMBOL_GPL(rpc_task_release_transport);
1038
1039 void rpc_task_release_client(struct rpc_task *task)
1040 {
1041         struct rpc_clnt *clnt = task->tk_client;
1042
1043         rpc_task_release_transport(task);
1044         if (clnt != NULL) {
1045                 /* Remove from client task list */
1046                 spin_lock(&clnt->cl_lock);
1047                 list_del(&task->tk_task);
1048                 spin_unlock(&clnt->cl_lock);
1049                 task->tk_client = NULL;
1050
1051                 rpc_release_client(clnt);
1052         }
1053 }
1054
1055 static struct rpc_xprt *
1056 rpc_task_get_first_xprt(struct rpc_clnt *clnt)
1057 {
1058         struct rpc_xprt *xprt;
1059
1060         rcu_read_lock();
1061         xprt = xprt_get(rcu_dereference(clnt->cl_xprt));
1062         rcu_read_unlock();
1063         return rpc_task_get_xprt(clnt, xprt);
1064 }
1065
1066 static struct rpc_xprt *
1067 rpc_task_get_next_xprt(struct rpc_clnt *clnt)
1068 {
1069         return rpc_task_get_xprt(clnt, xprt_iter_get_next(&clnt->cl_xpi));
1070 }
1071
1072 static
1073 void rpc_task_set_transport(struct rpc_task *task, struct rpc_clnt *clnt)
1074 {
1075         if (task->tk_xprt) {
1076                 if (!(test_bit(XPRT_OFFLINE, &task->tk_xprt->state) &&
1077                       (task->tk_flags & RPC_TASK_MOVEABLE)))
1078                         return;
1079                 xprt_release(task);
1080                 xprt_put(task->tk_xprt);
1081         }
1082         if (task->tk_flags & RPC_TASK_NO_ROUND_ROBIN)
1083                 task->tk_xprt = rpc_task_get_first_xprt(clnt);
1084         else
1085                 task->tk_xprt = rpc_task_get_next_xprt(clnt);
1086 }
1087
1088 static
1089 void rpc_task_set_client(struct rpc_task *task, struct rpc_clnt *clnt)
1090 {
1091
1092         if (clnt != NULL) {
1093                 rpc_task_set_transport(task, clnt);
1094                 task->tk_client = clnt;
1095                 refcount_inc(&clnt->cl_count);
1096                 if (clnt->cl_softrtry)
1097                         task->tk_flags |= RPC_TASK_SOFT;
1098                 if (clnt->cl_softerr)
1099                         task->tk_flags |= RPC_TASK_TIMEOUT;
1100                 if (clnt->cl_noretranstimeo)
1101                         task->tk_flags |= RPC_TASK_NO_RETRANS_TIMEOUT;
1102                 if (atomic_read(&clnt->cl_swapper))
1103                         task->tk_flags |= RPC_TASK_SWAPPER;
1104                 /* Add to the client's list of all tasks */
1105                 spin_lock(&clnt->cl_lock);
1106                 list_add_tail(&task->tk_task, &clnt->cl_tasks);
1107                 spin_unlock(&clnt->cl_lock);
1108         }
1109 }
1110
1111 static void
1112 rpc_task_set_rpc_message(struct rpc_task *task, const struct rpc_message *msg)
1113 {
1114         if (msg != NULL) {
1115                 task->tk_msg.rpc_proc = msg->rpc_proc;
1116                 task->tk_msg.rpc_argp = msg->rpc_argp;
1117                 task->tk_msg.rpc_resp = msg->rpc_resp;
1118                 task->tk_msg.rpc_cred = msg->rpc_cred;
1119                 if (!(task->tk_flags & RPC_TASK_CRED_NOREF))
1120                         get_cred(task->tk_msg.rpc_cred);
1121         }
1122 }
1123
1124 /*
1125  * Default callback for async RPC calls
1126  */
1127 static void
1128 rpc_default_callback(struct rpc_task *task, void *data)
1129 {
1130 }
1131
1132 static const struct rpc_call_ops rpc_default_ops = {
1133         .rpc_call_done = rpc_default_callback,
1134 };
1135
1136 /**
1137  * rpc_run_task - Allocate a new RPC task, then run rpc_execute against it
1138  * @task_setup_data: pointer to task initialisation data
1139  */
1140 struct rpc_task *rpc_run_task(const struct rpc_task_setup *task_setup_data)
1141 {
1142         struct rpc_task *task;
1143
1144         task = rpc_new_task(task_setup_data);
1145
1146         if (!RPC_IS_ASYNC(task))
1147                 task->tk_flags |= RPC_TASK_CRED_NOREF;
1148
1149         rpc_task_set_client(task, task_setup_data->rpc_client);
1150         rpc_task_set_rpc_message(task, task_setup_data->rpc_message);
1151
1152         if (task->tk_action == NULL)
1153                 rpc_call_start(task);
1154
1155         atomic_inc(&task->tk_count);
1156         rpc_execute(task);
1157         return task;
1158 }
1159 EXPORT_SYMBOL_GPL(rpc_run_task);
1160
1161 /**
1162  * rpc_call_sync - Perform a synchronous RPC call
1163  * @clnt: pointer to RPC client
1164  * @msg: RPC call parameters
1165  * @flags: RPC call flags
1166  */
1167 int rpc_call_sync(struct rpc_clnt *clnt, const struct rpc_message *msg, int flags)
1168 {
1169         struct rpc_task *task;
1170         struct rpc_task_setup task_setup_data = {
1171                 .rpc_client = clnt,
1172                 .rpc_message = msg,
1173                 .callback_ops = &rpc_default_ops,
1174                 .flags = flags,
1175         };
1176         int status;
1177
1178         WARN_ON_ONCE(flags & RPC_TASK_ASYNC);
1179         if (flags & RPC_TASK_ASYNC) {
1180                 rpc_release_calldata(task_setup_data.callback_ops,
1181                         task_setup_data.callback_data);
1182                 return -EINVAL;
1183         }
1184
1185         task = rpc_run_task(&task_setup_data);
1186         if (IS_ERR(task))
1187                 return PTR_ERR(task);
1188         status = task->tk_status;
1189         rpc_put_task(task);
1190         return status;
1191 }
1192 EXPORT_SYMBOL_GPL(rpc_call_sync);
1193
1194 /**
1195  * rpc_call_async - Perform an asynchronous RPC call
1196  * @clnt: pointer to RPC client
1197  * @msg: RPC call parameters
1198  * @flags: RPC call flags
1199  * @tk_ops: RPC call ops
1200  * @data: user call data
1201  */
1202 int
1203 rpc_call_async(struct rpc_clnt *clnt, const struct rpc_message *msg, int flags,
1204                const struct rpc_call_ops *tk_ops, void *data)
1205 {
1206         struct rpc_task *task;
1207         struct rpc_task_setup task_setup_data = {
1208                 .rpc_client = clnt,
1209                 .rpc_message = msg,
1210                 .callback_ops = tk_ops,
1211                 .callback_data = data,
1212                 .flags = flags|RPC_TASK_ASYNC,
1213         };
1214
1215         task = rpc_run_task(&task_setup_data);
1216         if (IS_ERR(task))
1217                 return PTR_ERR(task);
1218         rpc_put_task(task);
1219         return 0;
1220 }
1221 EXPORT_SYMBOL_GPL(rpc_call_async);
1222
1223 #if defined(CONFIG_SUNRPC_BACKCHANNEL)
1224 static void call_bc_encode(struct rpc_task *task);
1225
1226 /**
1227  * rpc_run_bc_task - Allocate a new RPC task for backchannel use, then run
1228  * rpc_execute against it
1229  * @req: RPC request
1230  */
1231 struct rpc_task *rpc_run_bc_task(struct rpc_rqst *req)
1232 {
1233         struct rpc_task *task;
1234         struct rpc_task_setup task_setup_data = {
1235                 .callback_ops = &rpc_default_ops,
1236                 .flags = RPC_TASK_SOFTCONN |
1237                         RPC_TASK_NO_RETRANS_TIMEOUT,
1238         };
1239
1240         dprintk("RPC: rpc_run_bc_task req= %p\n", req);
1241         /*
1242          * Create an rpc_task to send the data
1243          */
1244         task = rpc_new_task(&task_setup_data);
1245         xprt_init_bc_request(req, task);
1246
1247         task->tk_action = call_bc_encode;
1248         atomic_inc(&task->tk_count);
1249         WARN_ON_ONCE(atomic_read(&task->tk_count) != 2);
1250         rpc_execute(task);
1251
1252         dprintk("RPC: rpc_run_bc_task: task= %p\n", task);
1253         return task;
1254 }
1255 #endif /* CONFIG_SUNRPC_BACKCHANNEL */
1256
1257 /**
1258  * rpc_prepare_reply_pages - Prepare to receive a reply data payload into pages
1259  * @req: RPC request to prepare
1260  * @pages: vector of struct page pointers
1261  * @base: offset in first page where receive should start, in bytes
1262  * @len: expected size of the upper layer data payload, in bytes
1263  * @hdrsize: expected size of upper layer reply header, in XDR words
1264  *
1265  */
1266 void rpc_prepare_reply_pages(struct rpc_rqst *req, struct page **pages,
1267                              unsigned int base, unsigned int len,
1268                              unsigned int hdrsize)
1269 {
1270         hdrsize += RPC_REPHDRSIZE + req->rq_cred->cr_auth->au_ralign;
1271
1272         xdr_inline_pages(&req->rq_rcv_buf, hdrsize << 2, pages, base, len);
1273         trace_rpc_xdr_reply_pages(req->rq_task, &req->rq_rcv_buf);
1274 }
1275 EXPORT_SYMBOL_GPL(rpc_prepare_reply_pages);
1276
1277 void
1278 rpc_call_start(struct rpc_task *task)
1279 {
1280         task->tk_action = call_start;
1281 }
1282 EXPORT_SYMBOL_GPL(rpc_call_start);
1283
1284 /**
1285  * rpc_peeraddr - extract remote peer address from clnt's xprt
1286  * @clnt: RPC client structure
1287  * @buf: target buffer
1288  * @bufsize: length of target buffer
1289  *
1290  * Returns the number of bytes that are actually in the stored address.
1291  */
1292 size_t rpc_peeraddr(struct rpc_clnt *clnt, struct sockaddr *buf, size_t bufsize)
1293 {
1294         size_t bytes;
1295         struct rpc_xprt *xprt;
1296
1297         rcu_read_lock();
1298         xprt = rcu_dereference(clnt->cl_xprt);
1299
1300         bytes = xprt->addrlen;
1301         if (bytes > bufsize)
1302                 bytes = bufsize;
1303         memcpy(buf, &xprt->addr, bytes);
1304         rcu_read_unlock();
1305
1306         return bytes;
1307 }
1308 EXPORT_SYMBOL_GPL(rpc_peeraddr);
1309
1310 /**
1311  * rpc_peeraddr2str - return remote peer address in printable format
1312  * @clnt: RPC client structure
1313  * @format: address format
1314  *
1315  * NB: the lifetime of the memory referenced by the returned pointer is
1316  * the same as the rpc_xprt itself.  As long as the caller uses this
1317  * pointer, it must hold the RCU read lock.
1318  */
1319 const char *rpc_peeraddr2str(struct rpc_clnt *clnt,
1320                              enum rpc_display_format_t format)
1321 {
1322         struct rpc_xprt *xprt;
1323
1324         xprt = rcu_dereference(clnt->cl_xprt);
1325
1326         if (xprt->address_strings[format] != NULL)
1327                 return xprt->address_strings[format];
1328         else
1329                 return "unprintable";
1330 }
1331 EXPORT_SYMBOL_GPL(rpc_peeraddr2str);
1332
1333 static const struct sockaddr_in rpc_inaddr_loopback = {
1334         .sin_family             = AF_INET,
1335         .sin_addr.s_addr        = htonl(INADDR_ANY),
1336 };
1337
1338 static const struct sockaddr_in6 rpc_in6addr_loopback = {
1339         .sin6_family            = AF_INET6,
1340         .sin6_addr              = IN6ADDR_ANY_INIT,
1341 };
1342
1343 /*
1344  * Try a getsockname() on a connected datagram socket.  Using a
1345  * connected datagram socket prevents leaving a socket in TIME_WAIT.
1346  * This conserves the ephemeral port number space.
1347  *
1348  * Returns zero and fills in "buf" if successful; otherwise, a
1349  * negative errno is returned.
1350  */
1351 static int rpc_sockname(struct net *net, struct sockaddr *sap, size_t salen,
1352                         struct sockaddr *buf)
1353 {
1354         struct socket *sock;
1355         int err;
1356
1357         err = __sock_create(net, sap->sa_family,
1358                                 SOCK_DGRAM, IPPROTO_UDP, &sock, 1);
1359         if (err < 0) {
1360                 dprintk("RPC:       can't create UDP socket (%d)\n", err);
1361                 goto out;
1362         }
1363
1364         switch (sap->sa_family) {
1365         case AF_INET:
1366                 err = kernel_bind(sock,
1367                                 (struct sockaddr *)&rpc_inaddr_loopback,
1368                                 sizeof(rpc_inaddr_loopback));
1369                 break;
1370         case AF_INET6:
1371                 err = kernel_bind(sock,
1372                                 (struct sockaddr *)&rpc_in6addr_loopback,
1373                                 sizeof(rpc_in6addr_loopback));
1374                 break;
1375         default:
1376                 err = -EAFNOSUPPORT;
1377                 goto out;
1378         }
1379         if (err < 0) {
1380                 dprintk("RPC:       can't bind UDP socket (%d)\n", err);
1381                 goto out_release;
1382         }
1383
1384         err = kernel_connect(sock, sap, salen, 0);
1385         if (err < 0) {
1386                 dprintk("RPC:       can't connect UDP socket (%d)\n", err);
1387                 goto out_release;
1388         }
1389
1390         err = kernel_getsockname(sock, buf);
1391         if (err < 0) {
1392                 dprintk("RPC:       getsockname failed (%d)\n", err);
1393                 goto out_release;
1394         }
1395
1396         err = 0;
1397         if (buf->sa_family == AF_INET6) {
1398                 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)buf;
1399                 sin6->sin6_scope_id = 0;
1400         }
1401         dprintk("RPC:       %s succeeded\n", __func__);
1402
1403 out_release:
1404         sock_release(sock);
1405 out:
1406         return err;
1407 }
1408
1409 /*
1410  * Scraping a connected socket failed, so we don't have a useable
1411  * local address.  Fallback: generate an address that will prevent
1412  * the server from calling us back.
1413  *
1414  * Returns zero and fills in "buf" if successful; otherwise, a
1415  * negative errno is returned.
1416  */
1417 static int rpc_anyaddr(int family, struct sockaddr *buf, size_t buflen)
1418 {
1419         switch (family) {
1420         case AF_INET:
1421                 if (buflen < sizeof(rpc_inaddr_loopback))
1422                         return -EINVAL;
1423                 memcpy(buf, &rpc_inaddr_loopback,
1424                                 sizeof(rpc_inaddr_loopback));
1425                 break;
1426         case AF_INET6:
1427                 if (buflen < sizeof(rpc_in6addr_loopback))
1428                         return -EINVAL;
1429                 memcpy(buf, &rpc_in6addr_loopback,
1430                                 sizeof(rpc_in6addr_loopback));
1431                 break;
1432         default:
1433                 dprintk("RPC:       %s: address family not supported\n",
1434                         __func__);
1435                 return -EAFNOSUPPORT;
1436         }
1437         dprintk("RPC:       %s: succeeded\n", __func__);
1438         return 0;
1439 }
1440
1441 /**
1442  * rpc_localaddr - discover local endpoint address for an RPC client
1443  * @clnt: RPC client structure
1444  * @buf: target buffer
1445  * @buflen: size of target buffer, in bytes
1446  *
1447  * Returns zero and fills in "buf" and "buflen" if successful;
1448  * otherwise, a negative errno is returned.
1449  *
1450  * This works even if the underlying transport is not currently connected,
1451  * or if the upper layer never previously provided a source address.
1452  *
1453  * The result of this function call is transient: multiple calls in
1454  * succession may give different results, depending on how local
1455  * networking configuration changes over time.
1456  */
1457 int rpc_localaddr(struct rpc_clnt *clnt, struct sockaddr *buf, size_t buflen)
1458 {
1459         struct sockaddr_storage address;
1460         struct sockaddr *sap = (struct sockaddr *)&address;
1461         struct rpc_xprt *xprt;
1462         struct net *net;
1463         size_t salen;
1464         int err;
1465
1466         rcu_read_lock();
1467         xprt = rcu_dereference(clnt->cl_xprt);
1468         salen = xprt->addrlen;
1469         memcpy(sap, &xprt->addr, salen);
1470         net = get_net(xprt->xprt_net);
1471         rcu_read_unlock();
1472
1473         rpc_set_port(sap, 0);
1474         err = rpc_sockname(net, sap, salen, buf);
1475         put_net(net);
1476         if (err != 0)
1477                 /* Couldn't discover local address, return ANYADDR */
1478                 return rpc_anyaddr(sap->sa_family, buf, buflen);
1479         return 0;
1480 }
1481 EXPORT_SYMBOL_GPL(rpc_localaddr);
1482
1483 void
1484 rpc_setbufsize(struct rpc_clnt *clnt, unsigned int sndsize, unsigned int rcvsize)
1485 {
1486         struct rpc_xprt *xprt;
1487
1488         rcu_read_lock();
1489         xprt = rcu_dereference(clnt->cl_xprt);
1490         if (xprt->ops->set_buffer_size)
1491                 xprt->ops->set_buffer_size(xprt, sndsize, rcvsize);
1492         rcu_read_unlock();
1493 }
1494 EXPORT_SYMBOL_GPL(rpc_setbufsize);
1495
1496 /**
1497  * rpc_net_ns - Get the network namespace for this RPC client
1498  * @clnt: RPC client to query
1499  *
1500  */
1501 struct net *rpc_net_ns(struct rpc_clnt *clnt)
1502 {
1503         struct net *ret;
1504
1505         rcu_read_lock();
1506         ret = rcu_dereference(clnt->cl_xprt)->xprt_net;
1507         rcu_read_unlock();
1508         return ret;
1509 }
1510 EXPORT_SYMBOL_GPL(rpc_net_ns);
1511
1512 /**
1513  * rpc_max_payload - Get maximum payload size for a transport, in bytes
1514  * @clnt: RPC client to query
1515  *
1516  * For stream transports, this is one RPC record fragment (see RFC
1517  * 1831), as we don't support multi-record requests yet.  For datagram
1518  * transports, this is the size of an IP packet minus the IP, UDP, and
1519  * RPC header sizes.
1520  */
1521 size_t rpc_max_payload(struct rpc_clnt *clnt)
1522 {
1523         size_t ret;
1524
1525         rcu_read_lock();
1526         ret = rcu_dereference(clnt->cl_xprt)->max_payload;
1527         rcu_read_unlock();
1528         return ret;
1529 }
1530 EXPORT_SYMBOL_GPL(rpc_max_payload);
1531
1532 /**
1533  * rpc_max_bc_payload - Get maximum backchannel payload size, in bytes
1534  * @clnt: RPC client to query
1535  */
1536 size_t rpc_max_bc_payload(struct rpc_clnt *clnt)
1537 {
1538         struct rpc_xprt *xprt;
1539         size_t ret;
1540
1541         rcu_read_lock();
1542         xprt = rcu_dereference(clnt->cl_xprt);
1543         ret = xprt->ops->bc_maxpayload(xprt);
1544         rcu_read_unlock();
1545         return ret;
1546 }
1547 EXPORT_SYMBOL_GPL(rpc_max_bc_payload);
1548
1549 unsigned int rpc_num_bc_slots(struct rpc_clnt *clnt)
1550 {
1551         struct rpc_xprt *xprt;
1552         unsigned int ret;
1553
1554         rcu_read_lock();
1555         xprt = rcu_dereference(clnt->cl_xprt);
1556         ret = xprt->ops->bc_num_slots(xprt);
1557         rcu_read_unlock();
1558         return ret;
1559 }
1560 EXPORT_SYMBOL_GPL(rpc_num_bc_slots);
1561
1562 /**
1563  * rpc_force_rebind - force transport to check that remote port is unchanged
1564  * @clnt: client to rebind
1565  *
1566  */
1567 void rpc_force_rebind(struct rpc_clnt *clnt)
1568 {
1569         if (clnt->cl_autobind) {
1570                 rcu_read_lock();
1571                 xprt_clear_bound(rcu_dereference(clnt->cl_xprt));
1572                 rcu_read_unlock();
1573         }
1574 }
1575 EXPORT_SYMBOL_GPL(rpc_force_rebind);
1576
1577 static int
1578 __rpc_restart_call(struct rpc_task *task, void (*action)(struct rpc_task *))
1579 {
1580         task->tk_status = 0;
1581         task->tk_rpc_status = 0;
1582         task->tk_action = action;
1583         return 1;
1584 }
1585
1586 /*
1587  * Restart an (async) RPC call. Usually called from within the
1588  * exit handler.
1589  */
1590 int
1591 rpc_restart_call(struct rpc_task *task)
1592 {
1593         return __rpc_restart_call(task, call_start);
1594 }
1595 EXPORT_SYMBOL_GPL(rpc_restart_call);
1596
1597 /*
1598  * Restart an (async) RPC call from the call_prepare state.
1599  * Usually called from within the exit handler.
1600  */
1601 int
1602 rpc_restart_call_prepare(struct rpc_task *task)
1603 {
1604         if (task->tk_ops->rpc_call_prepare != NULL)
1605                 return __rpc_restart_call(task, rpc_prepare_task);
1606         return rpc_restart_call(task);
1607 }
1608 EXPORT_SYMBOL_GPL(rpc_restart_call_prepare);
1609
1610 const char
1611 *rpc_proc_name(const struct rpc_task *task)
1612 {
1613         const struct rpc_procinfo *proc = task->tk_msg.rpc_proc;
1614
1615         if (proc) {
1616                 if (proc->p_name)
1617                         return proc->p_name;
1618                 else
1619                         return "NULL";
1620         } else
1621                 return "no proc";
1622 }
1623
1624 static void
1625 __rpc_call_rpcerror(struct rpc_task *task, int tk_status, int rpc_status)
1626 {
1627         trace_rpc_call_rpcerror(task, tk_status, rpc_status);
1628         task->tk_rpc_status = rpc_status;
1629         rpc_exit(task, tk_status);
1630 }
1631
1632 static void
1633 rpc_call_rpcerror(struct rpc_task *task, int status)
1634 {
1635         __rpc_call_rpcerror(task, status, status);
1636 }
1637
1638 /*
1639  * 0.  Initial state
1640  *
1641  *     Other FSM states can be visited zero or more times, but
1642  *     this state is visited exactly once for each RPC.
1643  */
1644 static void
1645 call_start(struct rpc_task *task)
1646 {
1647         struct rpc_clnt *clnt = task->tk_client;
1648         int idx = task->tk_msg.rpc_proc->p_statidx;
1649
1650         trace_rpc_request(task);
1651
1652         /* Increment call count (version might not be valid for ping) */
1653         if (clnt->cl_program->version[clnt->cl_vers])
1654                 clnt->cl_program->version[clnt->cl_vers]->counts[idx]++;
1655         clnt->cl_stats->rpccnt++;
1656         task->tk_action = call_reserve;
1657         rpc_task_set_transport(task, clnt);
1658 }
1659
1660 /*
1661  * 1.   Reserve an RPC call slot
1662  */
1663 static void
1664 call_reserve(struct rpc_task *task)
1665 {
1666         task->tk_status  = 0;
1667         task->tk_action  = call_reserveresult;
1668         xprt_reserve(task);
1669 }
1670
1671 static void call_retry_reserve(struct rpc_task *task);
1672
1673 /*
1674  * 1b.  Grok the result of xprt_reserve()
1675  */
1676 static void
1677 call_reserveresult(struct rpc_task *task)
1678 {
1679         int status = task->tk_status;
1680
1681         /*
1682          * After a call to xprt_reserve(), we must have either
1683          * a request slot or else an error status.
1684          */
1685         task->tk_status = 0;
1686         if (status >= 0) {
1687                 if (task->tk_rqstp) {
1688                         task->tk_action = call_refresh;
1689                         return;
1690                 }
1691
1692                 rpc_call_rpcerror(task, -EIO);
1693                 return;
1694         }
1695
1696         switch (status) {
1697         case -ENOMEM:
1698                 rpc_delay(task, HZ >> 2);
1699                 fallthrough;
1700         case -EAGAIN:   /* woken up; retry */
1701                 task->tk_action = call_retry_reserve;
1702                 return;
1703         default:
1704                 rpc_call_rpcerror(task, status);
1705         }
1706 }
1707
1708 /*
1709  * 1c.  Retry reserving an RPC call slot
1710  */
1711 static void
1712 call_retry_reserve(struct rpc_task *task)
1713 {
1714         task->tk_status  = 0;
1715         task->tk_action  = call_reserveresult;
1716         xprt_retry_reserve(task);
1717 }
1718
1719 /*
1720  * 2.   Bind and/or refresh the credentials
1721  */
1722 static void
1723 call_refresh(struct rpc_task *task)
1724 {
1725         task->tk_action = call_refreshresult;
1726         task->tk_status = 0;
1727         task->tk_client->cl_stats->rpcauthrefresh++;
1728         rpcauth_refreshcred(task);
1729 }
1730
1731 /*
1732  * 2a.  Process the results of a credential refresh
1733  */
1734 static void
1735 call_refreshresult(struct rpc_task *task)
1736 {
1737         int status = task->tk_status;
1738
1739         task->tk_status = 0;
1740         task->tk_action = call_refresh;
1741         switch (status) {
1742         case 0:
1743                 if (rpcauth_uptodatecred(task)) {
1744                         task->tk_action = call_allocate;
1745                         return;
1746                 }
1747                 /* Use rate-limiting and a max number of retries if refresh
1748                  * had status 0 but failed to update the cred.
1749                  */
1750                 fallthrough;
1751         case -ETIMEDOUT:
1752                 rpc_delay(task, 3*HZ);
1753                 fallthrough;
1754         case -EAGAIN:
1755                 status = -EACCES;
1756                 fallthrough;
1757         case -EKEYEXPIRED:
1758                 if (!task->tk_cred_retry)
1759                         break;
1760                 task->tk_cred_retry--;
1761                 trace_rpc_retry_refresh_status(task);
1762                 return;
1763         }
1764         trace_rpc_refresh_status(task);
1765         rpc_call_rpcerror(task, status);
1766 }
1767
1768 /*
1769  * 2b.  Allocate the buffer. For details, see sched.c:rpc_malloc.
1770  *      (Note: buffer memory is freed in xprt_release).
1771  */
1772 static void
1773 call_allocate(struct rpc_task *task)
1774 {
1775         const struct rpc_auth *auth = task->tk_rqstp->rq_cred->cr_auth;
1776         struct rpc_rqst *req = task->tk_rqstp;
1777         struct rpc_xprt *xprt = req->rq_xprt;
1778         const struct rpc_procinfo *proc = task->tk_msg.rpc_proc;
1779         int status;
1780
1781         task->tk_status = 0;
1782         task->tk_action = call_encode;
1783
1784         if (req->rq_buffer)
1785                 return;
1786
1787         if (proc->p_proc != 0) {
1788                 BUG_ON(proc->p_arglen == 0);
1789                 if (proc->p_decode != NULL)
1790                         BUG_ON(proc->p_replen == 0);
1791         }
1792
1793         /*
1794          * Calculate the size (in quads) of the RPC call
1795          * and reply headers, and convert both values
1796          * to byte sizes.
1797          */
1798         req->rq_callsize = RPC_CALLHDRSIZE + (auth->au_cslack << 1) +
1799                            proc->p_arglen;
1800         req->rq_callsize <<= 2;
1801         /*
1802          * Note: the reply buffer must at minimum allocate enough space
1803          * for the 'struct accepted_reply' from RFC5531.
1804          */
1805         req->rq_rcvsize = RPC_REPHDRSIZE + auth->au_rslack + \
1806                         max_t(size_t, proc->p_replen, 2);
1807         req->rq_rcvsize <<= 2;
1808
1809         status = xprt->ops->buf_alloc(task);
1810         trace_rpc_buf_alloc(task, status);
1811         if (status == 0)
1812                 return;
1813         if (status != -ENOMEM) {
1814                 rpc_call_rpcerror(task, status);
1815                 return;
1816         }
1817
1818         if (RPC_IS_ASYNC(task) || !fatal_signal_pending(current)) {
1819                 task->tk_action = call_allocate;
1820                 rpc_delay(task, HZ>>4);
1821                 return;
1822         }
1823
1824         rpc_call_rpcerror(task, -ERESTARTSYS);
1825 }
1826
1827 static int
1828 rpc_task_need_encode(struct rpc_task *task)
1829 {
1830         return test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate) == 0 &&
1831                 (!(task->tk_flags & RPC_TASK_SENT) ||
1832                  !(task->tk_flags & RPC_TASK_NO_RETRANS_TIMEOUT) ||
1833                  xprt_request_need_retransmit(task));
1834 }
1835
1836 static void
1837 rpc_xdr_encode(struct rpc_task *task)
1838 {
1839         struct rpc_rqst *req = task->tk_rqstp;
1840         struct xdr_stream xdr;
1841
1842         xdr_buf_init(&req->rq_snd_buf,
1843                      req->rq_buffer,
1844                      req->rq_callsize);
1845         xdr_buf_init(&req->rq_rcv_buf,
1846                      req->rq_rbuffer,
1847                      req->rq_rcvsize);
1848
1849         req->rq_reply_bytes_recvd = 0;
1850         req->rq_snd_buf.head[0].iov_len = 0;
1851         xdr_init_encode(&xdr, &req->rq_snd_buf,
1852                         req->rq_snd_buf.head[0].iov_base, req);
1853         xdr_free_bvec(&req->rq_snd_buf);
1854         if (rpc_encode_header(task, &xdr))
1855                 return;
1856
1857         task->tk_status = rpcauth_wrap_req(task, &xdr);
1858 }
1859
1860 /*
1861  * 3.   Encode arguments of an RPC call
1862  */
1863 static void
1864 call_encode(struct rpc_task *task)
1865 {
1866         if (!rpc_task_need_encode(task))
1867                 goto out;
1868
1869         /* Dequeue task from the receive queue while we're encoding */
1870         xprt_request_dequeue_xprt(task);
1871         /* Encode here so that rpcsec_gss can use correct sequence number. */
1872         rpc_xdr_encode(task);
1873         /* Did the encode result in an error condition? */
1874         if (task->tk_status != 0) {
1875                 /* Was the error nonfatal? */
1876                 switch (task->tk_status) {
1877                 case -EAGAIN:
1878                 case -ENOMEM:
1879                         rpc_delay(task, HZ >> 4);
1880                         break;
1881                 case -EKEYEXPIRED:
1882                         if (!task->tk_cred_retry) {
1883                                 rpc_exit(task, task->tk_status);
1884                         } else {
1885                                 task->tk_action = call_refresh;
1886                                 task->tk_cred_retry--;
1887                                 trace_rpc_retry_refresh_status(task);
1888                         }
1889                         break;
1890                 default:
1891                         rpc_call_rpcerror(task, task->tk_status);
1892                 }
1893                 return;
1894         }
1895
1896         /* Add task to reply queue before transmission to avoid races */
1897         if (rpc_reply_expected(task))
1898                 xprt_request_enqueue_receive(task);
1899         xprt_request_enqueue_transmit(task);
1900 out:
1901         task->tk_action = call_transmit;
1902         /* Check that the connection is OK */
1903         if (!xprt_bound(task->tk_xprt))
1904                 task->tk_action = call_bind;
1905         else if (!xprt_connected(task->tk_xprt))
1906                 task->tk_action = call_connect;
1907 }
1908
1909 /*
1910  * Helpers to check if the task was already transmitted, and
1911  * to take action when that is the case.
1912  */
1913 static bool
1914 rpc_task_transmitted(struct rpc_task *task)
1915 {
1916         return !test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate);
1917 }
1918
1919 static void
1920 rpc_task_handle_transmitted(struct rpc_task *task)
1921 {
1922         xprt_end_transmit(task);
1923         task->tk_action = call_transmit_status;
1924 }
1925
1926 /*
1927  * 4.   Get the server port number if not yet set
1928  */
1929 static void
1930 call_bind(struct rpc_task *task)
1931 {
1932         struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
1933
1934         if (rpc_task_transmitted(task)) {
1935                 rpc_task_handle_transmitted(task);
1936                 return;
1937         }
1938
1939         if (xprt_bound(xprt)) {
1940                 task->tk_action = call_connect;
1941                 return;
1942         }
1943
1944         task->tk_action = call_bind_status;
1945         if (!xprt_prepare_transmit(task))
1946                 return;
1947
1948         xprt->ops->rpcbind(task);
1949 }
1950
1951 /*
1952  * 4a.  Sort out bind result
1953  */
1954 static void
1955 call_bind_status(struct rpc_task *task)
1956 {
1957         struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
1958         int status = -EIO;
1959
1960         if (rpc_task_transmitted(task)) {
1961                 rpc_task_handle_transmitted(task);
1962                 return;
1963         }
1964
1965         if (task->tk_status >= 0)
1966                 goto out_next;
1967         if (xprt_bound(xprt)) {
1968                 task->tk_status = 0;
1969                 goto out_next;
1970         }
1971
1972         switch (task->tk_status) {
1973         case -ENOMEM:
1974                 rpc_delay(task, HZ >> 2);
1975                 goto retry_timeout;
1976         case -EACCES:
1977                 trace_rpcb_prog_unavail_err(task);
1978                 /* fail immediately if this is an RPC ping */
1979                 if (task->tk_msg.rpc_proc->p_proc == 0) {
1980                         status = -EOPNOTSUPP;
1981                         break;
1982                 }
1983                 if (task->tk_rebind_retry == 0)
1984                         break;
1985                 task->tk_rebind_retry--;
1986                 rpc_delay(task, 3*HZ);
1987                 goto retry_timeout;
1988         case -ENOBUFS:
1989                 rpc_delay(task, HZ >> 2);
1990                 goto retry_timeout;
1991         case -EAGAIN:
1992                 goto retry_timeout;
1993         case -ETIMEDOUT:
1994                 trace_rpcb_timeout_err(task);
1995                 goto retry_timeout;
1996         case -EPFNOSUPPORT:
1997                 /* server doesn't support any rpcbind version we know of */
1998                 trace_rpcb_bind_version_err(task);
1999                 break;
2000         case -EPROTONOSUPPORT:
2001                 trace_rpcb_bind_version_err(task);
2002                 goto retry_timeout;
2003         case -ECONNREFUSED:             /* connection problems */
2004         case -ECONNRESET:
2005         case -ECONNABORTED:
2006         case -ENOTCONN:
2007         case -EHOSTDOWN:
2008         case -ENETDOWN:
2009         case -EHOSTUNREACH:
2010         case -ENETUNREACH:
2011         case -EPIPE:
2012                 trace_rpcb_unreachable_err(task);
2013                 if (!RPC_IS_SOFTCONN(task)) {
2014                         rpc_delay(task, 5*HZ);
2015                         goto retry_timeout;
2016                 }
2017                 status = task->tk_status;
2018                 break;
2019         default:
2020                 trace_rpcb_unrecognized_err(task);
2021         }
2022
2023         rpc_call_rpcerror(task, status);
2024         return;
2025 out_next:
2026         task->tk_action = call_connect;
2027         return;
2028 retry_timeout:
2029         task->tk_status = 0;
2030         task->tk_action = call_bind;
2031         rpc_check_timeout(task);
2032 }
2033
2034 /*
2035  * 4b.  Connect to the RPC server
2036  */
2037 static void
2038 call_connect(struct rpc_task *task)
2039 {
2040         struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
2041
2042         if (rpc_task_transmitted(task)) {
2043                 rpc_task_handle_transmitted(task);
2044                 return;
2045         }
2046
2047         if (xprt_connected(xprt)) {
2048                 task->tk_action = call_transmit;
2049                 return;
2050         }
2051
2052         task->tk_action = call_connect_status;
2053         if (task->tk_status < 0)
2054                 return;
2055         if (task->tk_flags & RPC_TASK_NOCONNECT) {
2056                 rpc_call_rpcerror(task, -ENOTCONN);
2057                 return;
2058         }
2059         if (!xprt_prepare_transmit(task))
2060                 return;
2061         xprt_connect(task);
2062 }
2063
2064 /*
2065  * 4c.  Sort out connect result
2066  */
2067 static void
2068 call_connect_status(struct rpc_task *task)
2069 {
2070         struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
2071         struct rpc_clnt *clnt = task->tk_client;
2072         int status = task->tk_status;
2073
2074         if (rpc_task_transmitted(task)) {
2075                 rpc_task_handle_transmitted(task);
2076                 return;
2077         }
2078
2079         trace_rpc_connect_status(task);
2080
2081         if (task->tk_status == 0) {
2082                 clnt->cl_stats->netreconn++;
2083                 goto out_next;
2084         }
2085         if (xprt_connected(xprt)) {
2086                 task->tk_status = 0;
2087                 goto out_next;
2088         }
2089
2090         task->tk_status = 0;
2091         switch (status) {
2092         case -ECONNREFUSED:
2093                 /* A positive refusal suggests a rebind is needed. */
2094                 if (RPC_IS_SOFTCONN(task))
2095                         break;
2096                 if (clnt->cl_autobind) {
2097                         rpc_force_rebind(clnt);
2098                         goto out_retry;
2099                 }
2100                 fallthrough;
2101         case -ECONNRESET:
2102         case -ECONNABORTED:
2103         case -ENETDOWN:
2104         case -ENETUNREACH:
2105         case -EHOSTUNREACH:
2106         case -EPIPE:
2107         case -EPROTO:
2108                 xprt_conditional_disconnect(task->tk_rqstp->rq_xprt,
2109                                             task->tk_rqstp->rq_connect_cookie);
2110                 if (RPC_IS_SOFTCONN(task))
2111                         break;
2112                 /* retry with existing socket, after a delay */
2113                 rpc_delay(task, 3*HZ);
2114                 fallthrough;
2115         case -EADDRINUSE:
2116         case -ENOTCONN:
2117         case -EAGAIN:
2118         case -ETIMEDOUT:
2119                 if (!(task->tk_flags & RPC_TASK_NO_ROUND_ROBIN) &&
2120                     (task->tk_flags & RPC_TASK_MOVEABLE) &&
2121                     test_bit(XPRT_REMOVE, &xprt->state)) {
2122                         struct rpc_xprt *saved = task->tk_xprt;
2123                         struct rpc_xprt_switch *xps;
2124
2125                         rcu_read_lock();
2126                         xps = xprt_switch_get(rcu_dereference(clnt->cl_xpi.xpi_xpswitch));
2127                         rcu_read_unlock();
2128                         if (xps->xps_nxprts > 1) {
2129                                 long value;
2130
2131                                 xprt_release(task);
2132                                 value = atomic_long_dec_return(&xprt->queuelen);
2133                                 if (value == 0)
2134                                         rpc_xprt_switch_remove_xprt(xps, saved);
2135                                 xprt_put(saved);
2136                                 task->tk_xprt = NULL;
2137                                 task->tk_action = call_start;
2138                         }
2139                         xprt_switch_put(xps);
2140                         if (!task->tk_xprt)
2141                                 return;
2142                 }
2143                 goto out_retry;
2144         case -ENOBUFS:
2145                 rpc_delay(task, HZ >> 2);
2146                 goto out_retry;
2147         }
2148         rpc_call_rpcerror(task, status);
2149         return;
2150 out_next:
2151         task->tk_action = call_transmit;
2152         return;
2153 out_retry:
2154         /* Check for timeouts before looping back to call_bind */
2155         task->tk_action = call_bind;
2156         rpc_check_timeout(task);
2157 }
2158
2159 /*
2160  * 5.   Transmit the RPC request, and wait for reply
2161  */
2162 static void
2163 call_transmit(struct rpc_task *task)
2164 {
2165         if (rpc_task_transmitted(task)) {
2166                 rpc_task_handle_transmitted(task);
2167                 return;
2168         }
2169
2170         task->tk_action = call_transmit_status;
2171         if (!xprt_prepare_transmit(task))
2172                 return;
2173         task->tk_status = 0;
2174         if (test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate)) {
2175                 if (!xprt_connected(task->tk_xprt)) {
2176                         task->tk_status = -ENOTCONN;
2177                         return;
2178                 }
2179                 xprt_transmit(task);
2180         }
2181         xprt_end_transmit(task);
2182 }
2183
2184 /*
2185  * 5a.  Handle cleanup after a transmission
2186  */
2187 static void
2188 call_transmit_status(struct rpc_task *task)
2189 {
2190         task->tk_action = call_status;
2191
2192         /*
2193          * Common case: success.  Force the compiler to put this
2194          * test first.
2195          */
2196         if (rpc_task_transmitted(task)) {
2197                 task->tk_status = 0;
2198                 xprt_request_wait_receive(task);
2199                 return;
2200         }
2201
2202         switch (task->tk_status) {
2203         default:
2204                 break;
2205         case -EBADMSG:
2206                 task->tk_status = 0;
2207                 task->tk_action = call_encode;
2208                 break;
2209                 /*
2210                  * Special cases: if we've been waiting on the
2211                  * socket's write_space() callback, or if the
2212                  * socket just returned a connection error,
2213                  * then hold onto the transport lock.
2214                  */
2215         case -ENOMEM:
2216         case -ENOBUFS:
2217                 rpc_delay(task, HZ>>2);
2218                 fallthrough;
2219         case -EBADSLT:
2220         case -EAGAIN:
2221                 task->tk_action = call_transmit;
2222                 task->tk_status = 0;
2223                 break;
2224         case -ECONNREFUSED:
2225         case -EHOSTDOWN:
2226         case -ENETDOWN:
2227         case -EHOSTUNREACH:
2228         case -ENETUNREACH:
2229         case -EPERM:
2230                 if (RPC_IS_SOFTCONN(task)) {
2231                         if (!task->tk_msg.rpc_proc->p_proc)
2232                                 trace_xprt_ping(task->tk_xprt,
2233                                                 task->tk_status);
2234                         rpc_call_rpcerror(task, task->tk_status);
2235                         return;
2236                 }
2237                 fallthrough;
2238         case -ECONNRESET:
2239         case -ECONNABORTED:
2240         case -EADDRINUSE:
2241         case -ENOTCONN:
2242         case -EPIPE:
2243                 task->tk_action = call_bind;
2244                 task->tk_status = 0;
2245                 break;
2246         }
2247         rpc_check_timeout(task);
2248 }
2249
2250 #if defined(CONFIG_SUNRPC_BACKCHANNEL)
2251 static void call_bc_transmit(struct rpc_task *task);
2252 static void call_bc_transmit_status(struct rpc_task *task);
2253
2254 static void
2255 call_bc_encode(struct rpc_task *task)
2256 {
2257         xprt_request_enqueue_transmit(task);
2258         task->tk_action = call_bc_transmit;
2259 }
2260
2261 /*
2262  * 5b.  Send the backchannel RPC reply.  On error, drop the reply.  In
2263  * addition, disconnect on connectivity errors.
2264  */
2265 static void
2266 call_bc_transmit(struct rpc_task *task)
2267 {
2268         task->tk_action = call_bc_transmit_status;
2269         if (test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate)) {
2270                 if (!xprt_prepare_transmit(task))
2271                         return;
2272                 task->tk_status = 0;
2273                 xprt_transmit(task);
2274         }
2275         xprt_end_transmit(task);
2276 }
2277
2278 static void
2279 call_bc_transmit_status(struct rpc_task *task)
2280 {
2281         struct rpc_rqst *req = task->tk_rqstp;
2282
2283         if (rpc_task_transmitted(task))
2284                 task->tk_status = 0;
2285
2286         switch (task->tk_status) {
2287         case 0:
2288                 /* Success */
2289         case -ENETDOWN:
2290         case -EHOSTDOWN:
2291         case -EHOSTUNREACH:
2292         case -ENETUNREACH:
2293         case -ECONNRESET:
2294         case -ECONNREFUSED:
2295         case -EADDRINUSE:
2296         case -ENOTCONN:
2297         case -EPIPE:
2298                 break;
2299         case -ENOMEM:
2300         case -ENOBUFS:
2301                 rpc_delay(task, HZ>>2);
2302                 fallthrough;
2303         case -EBADSLT:
2304         case -EAGAIN:
2305                 task->tk_status = 0;
2306                 task->tk_action = call_bc_transmit;
2307                 return;
2308         case -ETIMEDOUT:
2309                 /*
2310                  * Problem reaching the server.  Disconnect and let the
2311                  * forechannel reestablish the connection.  The server will
2312                  * have to retransmit the backchannel request and we'll
2313                  * reprocess it.  Since these ops are idempotent, there's no
2314                  * need to cache our reply at this time.
2315                  */
2316                 printk(KERN_NOTICE "RPC: Could not send backchannel reply "
2317                         "error: %d\n", task->tk_status);
2318                 xprt_conditional_disconnect(req->rq_xprt,
2319                         req->rq_connect_cookie);
2320                 break;
2321         default:
2322                 /*
2323                  * We were unable to reply and will have to drop the
2324                  * request.  The server should reconnect and retransmit.
2325                  */
2326                 printk(KERN_NOTICE "RPC: Could not send backchannel reply "
2327                         "error: %d\n", task->tk_status);
2328                 break;
2329         }
2330         task->tk_action = rpc_exit_task;
2331 }
2332 #endif /* CONFIG_SUNRPC_BACKCHANNEL */
2333
2334 /*
2335  * 6.   Sort out the RPC call status
2336  */
2337 static void
2338 call_status(struct rpc_task *task)
2339 {
2340         struct rpc_clnt *clnt = task->tk_client;
2341         int             status;
2342
2343         if (!task->tk_msg.rpc_proc->p_proc)
2344                 trace_xprt_ping(task->tk_xprt, task->tk_status);
2345
2346         status = task->tk_status;
2347         if (status >= 0) {
2348                 task->tk_action = call_decode;
2349                 return;
2350         }
2351
2352         trace_rpc_call_status(task);
2353         task->tk_status = 0;
2354         switch(status) {
2355         case -EHOSTDOWN:
2356         case -ENETDOWN:
2357         case -EHOSTUNREACH:
2358         case -ENETUNREACH:
2359         case -EPERM:
2360                 if (RPC_IS_SOFTCONN(task))
2361                         goto out_exit;
2362                 /*
2363                  * Delay any retries for 3 seconds, then handle as if it
2364                  * were a timeout.
2365                  */
2366                 rpc_delay(task, 3*HZ);
2367                 fallthrough;
2368         case -ETIMEDOUT:
2369                 break;
2370         case -ECONNREFUSED:
2371         case -ECONNRESET:
2372         case -ECONNABORTED:
2373         case -ENOTCONN:
2374                 rpc_force_rebind(clnt);
2375                 break;
2376         case -EADDRINUSE:
2377                 rpc_delay(task, 3*HZ);
2378                 fallthrough;
2379         case -EPIPE:
2380         case -EAGAIN:
2381                 break;
2382         case -ENFILE:
2383         case -ENOBUFS:
2384         case -ENOMEM:
2385                 rpc_delay(task, HZ>>2);
2386                 break;
2387         case -EIO:
2388                 /* shutdown or soft timeout */
2389                 goto out_exit;
2390         default:
2391                 if (clnt->cl_chatty)
2392                         printk("%s: RPC call returned error %d\n",
2393                                clnt->cl_program->name, -status);
2394                 goto out_exit;
2395         }
2396         task->tk_action = call_encode;
2397         if (status != -ECONNRESET && status != -ECONNABORTED)
2398                 rpc_check_timeout(task);
2399         return;
2400 out_exit:
2401         rpc_call_rpcerror(task, status);
2402 }
2403
2404 static bool
2405 rpc_check_connected(const struct rpc_rqst *req)
2406 {
2407         /* No allocated request or transport? return true */
2408         if (!req || !req->rq_xprt)
2409                 return true;
2410         return xprt_connected(req->rq_xprt);
2411 }
2412
2413 static void
2414 rpc_check_timeout(struct rpc_task *task)
2415 {
2416         struct rpc_clnt *clnt = task->tk_client;
2417
2418         if (RPC_SIGNALLED(task)) {
2419                 rpc_call_rpcerror(task, -ERESTARTSYS);
2420                 return;
2421         }
2422
2423         if (xprt_adjust_timeout(task->tk_rqstp) == 0)
2424                 return;
2425
2426         trace_rpc_timeout_status(task);
2427         task->tk_timeouts++;
2428
2429         if (RPC_IS_SOFTCONN(task) && !rpc_check_connected(task->tk_rqstp)) {
2430                 rpc_call_rpcerror(task, -ETIMEDOUT);
2431                 return;
2432         }
2433
2434         if (RPC_IS_SOFT(task)) {
2435                 /*
2436                  * Once a "no retrans timeout" soft tasks (a.k.a NFSv4) has
2437                  * been sent, it should time out only if the transport
2438                  * connection gets terminally broken.
2439                  */
2440                 if ((task->tk_flags & RPC_TASK_NO_RETRANS_TIMEOUT) &&
2441                     rpc_check_connected(task->tk_rqstp))
2442                         return;
2443
2444                 if (clnt->cl_chatty) {
2445                         pr_notice_ratelimited(
2446                                 "%s: server %s not responding, timed out\n",
2447                                 clnt->cl_program->name,
2448                                 task->tk_xprt->servername);
2449                 }
2450                 if (task->tk_flags & RPC_TASK_TIMEOUT)
2451                         rpc_call_rpcerror(task, -ETIMEDOUT);
2452                 else
2453                         __rpc_call_rpcerror(task, -EIO, -ETIMEDOUT);
2454                 return;
2455         }
2456
2457         if (!(task->tk_flags & RPC_CALL_MAJORSEEN)) {
2458                 task->tk_flags |= RPC_CALL_MAJORSEEN;
2459                 if (clnt->cl_chatty) {
2460                         pr_notice_ratelimited(
2461                                 "%s: server %s not responding, still trying\n",
2462                                 clnt->cl_program->name,
2463                                 task->tk_xprt->servername);
2464                 }
2465         }
2466         rpc_force_rebind(clnt);
2467         /*
2468          * Did our request time out due to an RPCSEC_GSS out-of-sequence
2469          * event? RFC2203 requires the server to drop all such requests.
2470          */
2471         rpcauth_invalcred(task);
2472 }
2473
2474 /*
2475  * 7.   Decode the RPC reply
2476  */
2477 static void
2478 call_decode(struct rpc_task *task)
2479 {
2480         struct rpc_clnt *clnt = task->tk_client;
2481         struct rpc_rqst *req = task->tk_rqstp;
2482         struct xdr_stream xdr;
2483         int err;
2484
2485         if (!task->tk_msg.rpc_proc->p_decode) {
2486                 task->tk_action = rpc_exit_task;
2487                 return;
2488         }
2489
2490         if (task->tk_flags & RPC_CALL_MAJORSEEN) {
2491                 if (clnt->cl_chatty) {
2492                         pr_notice_ratelimited("%s: server %s OK\n",
2493                                 clnt->cl_program->name,
2494                                 task->tk_xprt->servername);
2495                 }
2496                 task->tk_flags &= ~RPC_CALL_MAJORSEEN;
2497         }
2498
2499         /*
2500          * Did we ever call xprt_complete_rqst()? If not, we should assume
2501          * the message is incomplete.
2502          */
2503         err = -EAGAIN;
2504         if (!req->rq_reply_bytes_recvd)
2505                 goto out;
2506
2507         /* Ensure that we see all writes made by xprt_complete_rqst()
2508          * before it changed req->rq_reply_bytes_recvd.
2509          */
2510         smp_rmb();
2511
2512         req->rq_rcv_buf.len = req->rq_private_buf.len;
2513         trace_rpc_xdr_recvfrom(task, &req->rq_rcv_buf);
2514
2515         /* Check that the softirq receive buffer is valid */
2516         WARN_ON(memcmp(&req->rq_rcv_buf, &req->rq_private_buf,
2517                                 sizeof(req->rq_rcv_buf)) != 0);
2518
2519         xdr_init_decode(&xdr, &req->rq_rcv_buf,
2520                         req->rq_rcv_buf.head[0].iov_base, req);
2521         err = rpc_decode_header(task, &xdr);
2522 out:
2523         switch (err) {
2524         case 0:
2525                 task->tk_action = rpc_exit_task;
2526                 task->tk_status = rpcauth_unwrap_resp(task, &xdr);
2527                 return;
2528         case -EAGAIN:
2529                 task->tk_status = 0;
2530                 if (task->tk_client->cl_discrtry)
2531                         xprt_conditional_disconnect(req->rq_xprt,
2532                                                     req->rq_connect_cookie);
2533                 task->tk_action = call_encode;
2534                 rpc_check_timeout(task);
2535                 break;
2536         case -EKEYREJECTED:
2537                 task->tk_action = call_reserve;
2538                 rpc_check_timeout(task);
2539                 rpcauth_invalcred(task);
2540                 /* Ensure we obtain a new XID if we retry! */
2541                 xprt_release(task);
2542         }
2543 }
2544
2545 static int
2546 rpc_encode_header(struct rpc_task *task, struct xdr_stream *xdr)
2547 {
2548         struct rpc_clnt *clnt = task->tk_client;
2549         struct rpc_rqst *req = task->tk_rqstp;
2550         __be32 *p;
2551         int error;
2552
2553         error = -EMSGSIZE;
2554         p = xdr_reserve_space(xdr, RPC_CALLHDRSIZE << 2);
2555         if (!p)
2556                 goto out_fail;
2557         *p++ = req->rq_xid;
2558         *p++ = rpc_call;
2559         *p++ = cpu_to_be32(RPC_VERSION);
2560         *p++ = cpu_to_be32(clnt->cl_prog);
2561         *p++ = cpu_to_be32(clnt->cl_vers);
2562         *p   = cpu_to_be32(task->tk_msg.rpc_proc->p_proc);
2563
2564         error = rpcauth_marshcred(task, xdr);
2565         if (error < 0)
2566                 goto out_fail;
2567         return 0;
2568 out_fail:
2569         trace_rpc_bad_callhdr(task);
2570         rpc_call_rpcerror(task, error);
2571         return error;
2572 }
2573
2574 static noinline int
2575 rpc_decode_header(struct rpc_task *task, struct xdr_stream *xdr)
2576 {
2577         struct rpc_clnt *clnt = task->tk_client;
2578         int error;
2579         __be32 *p;
2580
2581         /* RFC-1014 says that the representation of XDR data must be a
2582          * multiple of four bytes
2583          * - if it isn't pointer subtraction in the NFS client may give
2584          *   undefined results
2585          */
2586         if (task->tk_rqstp->rq_rcv_buf.len & 3)
2587                 goto out_unparsable;
2588
2589         p = xdr_inline_decode(xdr, 3 * sizeof(*p));
2590         if (!p)
2591                 goto out_unparsable;
2592         p++;    /* skip XID */
2593         if (*p++ != rpc_reply)
2594                 goto out_unparsable;
2595         if (*p++ != rpc_msg_accepted)
2596                 goto out_msg_denied;
2597
2598         error = rpcauth_checkverf(task, xdr);
2599         if (error)
2600                 goto out_verifier;
2601
2602         p = xdr_inline_decode(xdr, sizeof(*p));
2603         if (!p)
2604                 goto out_unparsable;
2605         switch (*p) {
2606         case rpc_success:
2607                 return 0;
2608         case rpc_prog_unavail:
2609                 trace_rpc__prog_unavail(task);
2610                 error = -EPFNOSUPPORT;
2611                 goto out_err;
2612         case rpc_prog_mismatch:
2613                 trace_rpc__prog_mismatch(task);
2614                 error = -EPROTONOSUPPORT;
2615                 goto out_err;
2616         case rpc_proc_unavail:
2617                 trace_rpc__proc_unavail(task);
2618                 error = -EOPNOTSUPP;
2619                 goto out_err;
2620         case rpc_garbage_args:
2621         case rpc_system_err:
2622                 trace_rpc__garbage_args(task);
2623                 error = -EIO;
2624                 break;
2625         default:
2626                 goto out_unparsable;
2627         }
2628
2629 out_garbage:
2630         clnt->cl_stats->rpcgarbage++;
2631         if (task->tk_garb_retry) {
2632                 task->tk_garb_retry--;
2633                 task->tk_action = call_encode;
2634                 return -EAGAIN;
2635         }
2636 out_err:
2637         rpc_call_rpcerror(task, error);
2638         return error;
2639
2640 out_unparsable:
2641         trace_rpc__unparsable(task);
2642         error = -EIO;
2643         goto out_garbage;
2644
2645 out_verifier:
2646         trace_rpc_bad_verifier(task);
2647         goto out_garbage;
2648
2649 out_msg_denied:
2650         error = -EACCES;
2651         p = xdr_inline_decode(xdr, sizeof(*p));
2652         if (!p)
2653                 goto out_unparsable;
2654         switch (*p++) {
2655         case rpc_auth_error:
2656                 break;
2657         case rpc_mismatch:
2658                 trace_rpc__mismatch(task);
2659                 error = -EPROTONOSUPPORT;
2660                 goto out_err;
2661         default:
2662                 goto out_unparsable;
2663         }
2664
2665         p = xdr_inline_decode(xdr, sizeof(*p));
2666         if (!p)
2667                 goto out_unparsable;
2668         switch (*p++) {
2669         case rpc_autherr_rejectedcred:
2670         case rpc_autherr_rejectedverf:
2671         case rpcsec_gsserr_credproblem:
2672         case rpcsec_gsserr_ctxproblem:
2673                 if (!task->tk_cred_retry)
2674                         break;
2675                 task->tk_cred_retry--;
2676                 trace_rpc__stale_creds(task);
2677                 return -EKEYREJECTED;
2678         case rpc_autherr_badcred:
2679         case rpc_autherr_badverf:
2680                 /* possibly garbled cred/verf? */
2681                 if (!task->tk_garb_retry)
2682                         break;
2683                 task->tk_garb_retry--;
2684                 trace_rpc__bad_creds(task);
2685                 task->tk_action = call_encode;
2686                 return -EAGAIN;
2687         case rpc_autherr_tooweak:
2688                 trace_rpc__auth_tooweak(task);
2689                 pr_warn("RPC: server %s requires stronger authentication.\n",
2690                         task->tk_xprt->servername);
2691                 break;
2692         default:
2693                 goto out_unparsable;
2694         }
2695         goto out_err;
2696 }
2697
2698 static void rpcproc_encode_null(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
2699                 const void *obj)
2700 {
2701 }
2702
2703 static int rpcproc_decode_null(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
2704                 void *obj)
2705 {
2706         return 0;
2707 }
2708
2709 static const struct rpc_procinfo rpcproc_null = {
2710         .p_encode = rpcproc_encode_null,
2711         .p_decode = rpcproc_decode_null,
2712 };
2713
2714 static const struct rpc_procinfo rpcproc_null_noreply = {
2715         .p_encode = rpcproc_encode_null,
2716 };
2717
2718 static void
2719 rpc_null_call_prepare(struct rpc_task *task, void *data)
2720 {
2721         task->tk_flags &= ~RPC_TASK_NO_RETRANS_TIMEOUT;
2722         rpc_call_start(task);
2723 }
2724
2725 static const struct rpc_call_ops rpc_null_ops = {
2726         .rpc_call_prepare = rpc_null_call_prepare,
2727         .rpc_call_done = rpc_default_callback,
2728 };
2729
2730 static
2731 struct rpc_task *rpc_call_null_helper(struct rpc_clnt *clnt,
2732                 struct rpc_xprt *xprt, struct rpc_cred *cred, int flags,
2733                 const struct rpc_call_ops *ops, void *data)
2734 {
2735         struct rpc_message msg = {
2736                 .rpc_proc = &rpcproc_null,
2737         };
2738         struct rpc_task_setup task_setup_data = {
2739                 .rpc_client = clnt,
2740                 .rpc_xprt = xprt,
2741                 .rpc_message = &msg,
2742                 .rpc_op_cred = cred,
2743                 .callback_ops = ops ?: &rpc_null_ops,
2744                 .callback_data = data,
2745                 .flags = flags | RPC_TASK_SOFT | RPC_TASK_SOFTCONN |
2746                          RPC_TASK_NULLCREDS,
2747         };
2748
2749         return rpc_run_task(&task_setup_data);
2750 }
2751
2752 struct rpc_task *rpc_call_null(struct rpc_clnt *clnt, struct rpc_cred *cred, int flags)
2753 {
2754         return rpc_call_null_helper(clnt, NULL, cred, flags, NULL, NULL);
2755 }
2756 EXPORT_SYMBOL_GPL(rpc_call_null);
2757
2758 static int rpc_ping(struct rpc_clnt *clnt)
2759 {
2760         struct rpc_task *task;
2761         int status;
2762
2763         task = rpc_call_null_helper(clnt, NULL, NULL, 0, NULL, NULL);
2764         if (IS_ERR(task))
2765                 return PTR_ERR(task);
2766         status = task->tk_status;
2767         rpc_put_task(task);
2768         return status;
2769 }
2770
2771 static int rpc_ping_noreply(struct rpc_clnt *clnt)
2772 {
2773         struct rpc_message msg = {
2774                 .rpc_proc = &rpcproc_null_noreply,
2775         };
2776         struct rpc_task_setup task_setup_data = {
2777                 .rpc_client = clnt,
2778                 .rpc_message = &msg,
2779                 .callback_ops = &rpc_null_ops,
2780                 .flags = RPC_TASK_SOFT | RPC_TASK_SOFTCONN | RPC_TASK_NULLCREDS,
2781         };
2782         struct rpc_task *task;
2783         int status;
2784
2785         task = rpc_run_task(&task_setup_data);
2786         if (IS_ERR(task))
2787                 return PTR_ERR(task);
2788         status = task->tk_status;
2789         rpc_put_task(task);
2790         return status;
2791 }
2792
2793 struct rpc_cb_add_xprt_calldata {
2794         struct rpc_xprt_switch *xps;
2795         struct rpc_xprt *xprt;
2796 };
2797
2798 static void rpc_cb_add_xprt_done(struct rpc_task *task, void *calldata)
2799 {
2800         struct rpc_cb_add_xprt_calldata *data = calldata;
2801
2802         if (task->tk_status == 0)
2803                 rpc_xprt_switch_add_xprt(data->xps, data->xprt);
2804 }
2805
2806 static void rpc_cb_add_xprt_release(void *calldata)
2807 {
2808         struct rpc_cb_add_xprt_calldata *data = calldata;
2809
2810         xprt_put(data->xprt);
2811         xprt_switch_put(data->xps);
2812         kfree(data);
2813 }
2814
2815 static const struct rpc_call_ops rpc_cb_add_xprt_call_ops = {
2816         .rpc_call_prepare = rpc_null_call_prepare,
2817         .rpc_call_done = rpc_cb_add_xprt_done,
2818         .rpc_release = rpc_cb_add_xprt_release,
2819 };
2820
2821 /**
2822  * rpc_clnt_test_and_add_xprt - Test and add a new transport to a rpc_clnt
2823  * @clnt: pointer to struct rpc_clnt
2824  * @xps: pointer to struct rpc_xprt_switch,
2825  * @xprt: pointer struct rpc_xprt
2826  * @dummy: unused
2827  */
2828 int rpc_clnt_test_and_add_xprt(struct rpc_clnt *clnt,
2829                 struct rpc_xprt_switch *xps, struct rpc_xprt *xprt,
2830                 void *dummy)
2831 {
2832         struct rpc_cb_add_xprt_calldata *data;
2833         struct rpc_task *task;
2834
2835         if (xps->xps_nunique_destaddr_xprts + 1 > clnt->cl_max_connect) {
2836                 rcu_read_lock();
2837                 pr_warn("SUNRPC: reached max allowed number (%d) did not add "
2838                         "transport to server: %s\n", clnt->cl_max_connect,
2839                         rpc_peeraddr2str(clnt, RPC_DISPLAY_ADDR));
2840                 rcu_read_unlock();
2841                 return -EINVAL;
2842         }
2843
2844         data = kmalloc(sizeof(*data), GFP_NOFS);
2845         if (!data)
2846                 return -ENOMEM;
2847         data->xps = xprt_switch_get(xps);
2848         data->xprt = xprt_get(xprt);
2849         if (rpc_xprt_switch_has_addr(data->xps, (struct sockaddr *)&xprt->addr)) {
2850                 rpc_cb_add_xprt_release(data);
2851                 goto success;
2852         }
2853
2854         task = rpc_call_null_helper(clnt, xprt, NULL, RPC_TASK_ASYNC,
2855                         &rpc_cb_add_xprt_call_ops, data);
2856         data->xps->xps_nunique_destaddr_xprts++;
2857         rpc_put_task(task);
2858 success:
2859         return 1;
2860 }
2861 EXPORT_SYMBOL_GPL(rpc_clnt_test_and_add_xprt);
2862
2863 /**
2864  * rpc_clnt_setup_test_and_add_xprt()
2865  *
2866  * This is an rpc_clnt_add_xprt setup() function which returns 1 so:
2867  *   1) caller of the test function must dereference the rpc_xprt_switch
2868  *   and the rpc_xprt.
2869  *   2) test function must call rpc_xprt_switch_add_xprt, usually in
2870  *   the rpc_call_done routine.
2871  *
2872  * Upon success (return of 1), the test function adds the new
2873  * transport to the rpc_clnt xprt switch
2874  *
2875  * @clnt: struct rpc_clnt to get the new transport
2876  * @xps:  the rpc_xprt_switch to hold the new transport
2877  * @xprt: the rpc_xprt to test
2878  * @data: a struct rpc_add_xprt_test pointer that holds the test function
2879  *        and test function call data
2880  */
2881 int rpc_clnt_setup_test_and_add_xprt(struct rpc_clnt *clnt,
2882                                      struct rpc_xprt_switch *xps,
2883                                      struct rpc_xprt *xprt,
2884                                      void *data)
2885 {
2886         struct rpc_task *task;
2887         struct rpc_add_xprt_test *xtest = (struct rpc_add_xprt_test *)data;
2888         int status = -EADDRINUSE;
2889
2890         xprt = xprt_get(xprt);
2891         xprt_switch_get(xps);
2892
2893         if (rpc_xprt_switch_has_addr(xps, (struct sockaddr *)&xprt->addr))
2894                 goto out_err;
2895
2896         /* Test the connection */
2897         task = rpc_call_null_helper(clnt, xprt, NULL, 0, NULL, NULL);
2898         if (IS_ERR(task)) {
2899                 status = PTR_ERR(task);
2900                 goto out_err;
2901         }
2902         status = task->tk_status;
2903         rpc_put_task(task);
2904
2905         if (status < 0)
2906                 goto out_err;
2907
2908         /* rpc_xprt_switch and rpc_xprt are deferrenced by add_xprt_test() */
2909         xtest->add_xprt_test(clnt, xprt, xtest->data);
2910
2911         xprt_put(xprt);
2912         xprt_switch_put(xps);
2913
2914         /* so that rpc_clnt_add_xprt does not call rpc_xprt_switch_add_xprt */
2915         return 1;
2916 out_err:
2917         xprt_put(xprt);
2918         xprt_switch_put(xps);
2919         pr_info("RPC:   rpc_clnt_test_xprt failed: %d addr %s not added\n",
2920                 status, xprt->address_strings[RPC_DISPLAY_ADDR]);
2921         return status;
2922 }
2923 EXPORT_SYMBOL_GPL(rpc_clnt_setup_test_and_add_xprt);
2924
2925 /**
2926  * rpc_clnt_add_xprt - Add a new transport to a rpc_clnt
2927  * @clnt: pointer to struct rpc_clnt
2928  * @xprtargs: pointer to struct xprt_create
2929  * @setup: callback to test and/or set up the connection
2930  * @data: pointer to setup function data
2931  *
2932  * Creates a new transport using the parameters set in args and
2933  * adds it to clnt.
2934  * If ping is set, then test that connectivity succeeds before
2935  * adding the new transport.
2936  *
2937  */
2938 int rpc_clnt_add_xprt(struct rpc_clnt *clnt,
2939                 struct xprt_create *xprtargs,
2940                 int (*setup)(struct rpc_clnt *,
2941                         struct rpc_xprt_switch *,
2942                         struct rpc_xprt *,
2943                         void *),
2944                 void *data)
2945 {
2946         struct rpc_xprt_switch *xps;
2947         struct rpc_xprt *xprt;
2948         unsigned long connect_timeout;
2949         unsigned long reconnect_timeout;
2950         unsigned char resvport, reuseport;
2951         int ret = 0, ident;
2952
2953         rcu_read_lock();
2954         xps = xprt_switch_get(rcu_dereference(clnt->cl_xpi.xpi_xpswitch));
2955         xprt = xprt_iter_xprt(&clnt->cl_xpi);
2956         if (xps == NULL || xprt == NULL) {
2957                 rcu_read_unlock();
2958                 xprt_switch_put(xps);
2959                 return -EAGAIN;
2960         }
2961         resvport = xprt->resvport;
2962         reuseport = xprt->reuseport;
2963         connect_timeout = xprt->connect_timeout;
2964         reconnect_timeout = xprt->max_reconnect_timeout;
2965         ident = xprt->xprt_class->ident;
2966         rcu_read_unlock();
2967
2968         if (!xprtargs->ident)
2969                 xprtargs->ident = ident;
2970         xprt = xprt_create_transport(xprtargs);
2971         if (IS_ERR(xprt)) {
2972                 ret = PTR_ERR(xprt);
2973                 goto out_put_switch;
2974         }
2975         xprt->resvport = resvport;
2976         xprt->reuseport = reuseport;
2977         if (xprt->ops->set_connect_timeout != NULL)
2978                 xprt->ops->set_connect_timeout(xprt,
2979                                 connect_timeout,
2980                                 reconnect_timeout);
2981
2982         rpc_xprt_switch_set_roundrobin(xps);
2983         if (setup) {
2984                 ret = setup(clnt, xps, xprt, data);
2985                 if (ret != 0)
2986                         goto out_put_xprt;
2987         }
2988         rpc_xprt_switch_add_xprt(xps, xprt);
2989 out_put_xprt:
2990         xprt_put(xprt);
2991 out_put_switch:
2992         xprt_switch_put(xps);
2993         return ret;
2994 }
2995 EXPORT_SYMBOL_GPL(rpc_clnt_add_xprt);
2996
2997 struct connect_timeout_data {
2998         unsigned long connect_timeout;
2999         unsigned long reconnect_timeout;
3000 };
3001
3002 static int
3003 rpc_xprt_set_connect_timeout(struct rpc_clnt *clnt,
3004                 struct rpc_xprt *xprt,
3005                 void *data)
3006 {
3007         struct connect_timeout_data *timeo = data;
3008
3009         if (xprt->ops->set_connect_timeout)
3010                 xprt->ops->set_connect_timeout(xprt,
3011                                 timeo->connect_timeout,
3012                                 timeo->reconnect_timeout);
3013         return 0;
3014 }
3015
3016 void
3017 rpc_set_connect_timeout(struct rpc_clnt *clnt,
3018                 unsigned long connect_timeout,
3019                 unsigned long reconnect_timeout)
3020 {
3021         struct connect_timeout_data timeout = {
3022                 .connect_timeout = connect_timeout,
3023                 .reconnect_timeout = reconnect_timeout,
3024         };
3025         rpc_clnt_iterate_for_each_xprt(clnt,
3026                         rpc_xprt_set_connect_timeout,
3027                         &timeout);
3028 }
3029 EXPORT_SYMBOL_GPL(rpc_set_connect_timeout);
3030
3031 void rpc_clnt_xprt_switch_put(struct rpc_clnt *clnt)
3032 {
3033         rcu_read_lock();
3034         xprt_switch_put(rcu_dereference(clnt->cl_xpi.xpi_xpswitch));
3035         rcu_read_unlock();
3036 }
3037 EXPORT_SYMBOL_GPL(rpc_clnt_xprt_switch_put);
3038
3039 void rpc_clnt_xprt_switch_add_xprt(struct rpc_clnt *clnt, struct rpc_xprt *xprt)
3040 {
3041         rcu_read_lock();
3042         rpc_xprt_switch_add_xprt(rcu_dereference(clnt->cl_xpi.xpi_xpswitch),
3043                                  xprt);
3044         rcu_read_unlock();
3045 }
3046 EXPORT_SYMBOL_GPL(rpc_clnt_xprt_switch_add_xprt);
3047
3048 bool rpc_clnt_xprt_switch_has_addr(struct rpc_clnt *clnt,
3049                                    const struct sockaddr *sap)
3050 {
3051         struct rpc_xprt_switch *xps;
3052         bool ret;
3053
3054         rcu_read_lock();
3055         xps = rcu_dereference(clnt->cl_xpi.xpi_xpswitch);
3056         ret = rpc_xprt_switch_has_addr(xps, sap);
3057         rcu_read_unlock();
3058         return ret;
3059 }
3060 EXPORT_SYMBOL_GPL(rpc_clnt_xprt_switch_has_addr);
3061
3062 #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
3063 static void rpc_show_header(void)
3064 {
3065         printk(KERN_INFO "-pid- flgs status -client- --rqstp- "
3066                 "-timeout ---ops--\n");
3067 }
3068
3069 static void rpc_show_task(const struct rpc_clnt *clnt,
3070                           const struct rpc_task *task)
3071 {
3072         const char *rpc_waitq = "none";
3073
3074         if (RPC_IS_QUEUED(task))
3075                 rpc_waitq = rpc_qname(task->tk_waitqueue);
3076
3077         printk(KERN_INFO "%5u %04x %6d %8p %8p %8ld %8p %sv%u %s a:%ps q:%s\n",
3078                 task->tk_pid, task->tk_flags, task->tk_status,
3079                 clnt, task->tk_rqstp, rpc_task_timeout(task), task->tk_ops,
3080                 clnt->cl_program->name, clnt->cl_vers, rpc_proc_name(task),
3081                 task->tk_action, rpc_waitq);
3082 }
3083
3084 void rpc_show_tasks(struct net *net)
3085 {
3086         struct rpc_clnt *clnt;
3087         struct rpc_task *task;
3088         int header = 0;
3089         struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
3090
3091         spin_lock(&sn->rpc_client_lock);
3092         list_for_each_entry(clnt, &sn->all_clients, cl_clients) {
3093                 spin_lock(&clnt->cl_lock);
3094                 list_for_each_entry(task, &clnt->cl_tasks, tk_task) {
3095                         if (!header) {
3096                                 rpc_show_header();
3097                                 header++;
3098                         }
3099                         rpc_show_task(clnt, task);
3100                 }
3101                 spin_unlock(&clnt->cl_lock);
3102         }
3103         spin_unlock(&sn->rpc_client_lock);
3104 }
3105 #endif
3106
3107 #if IS_ENABLED(CONFIG_SUNRPC_SWAP)
3108 static int
3109 rpc_clnt_swap_activate_callback(struct rpc_clnt *clnt,
3110                 struct rpc_xprt *xprt,
3111                 void *dummy)
3112 {
3113         return xprt_enable_swap(xprt);
3114 }
3115
3116 int
3117 rpc_clnt_swap_activate(struct rpc_clnt *clnt)
3118 {
3119         if (atomic_inc_return(&clnt->cl_swapper) == 1)
3120                 return rpc_clnt_iterate_for_each_xprt(clnt,
3121                                 rpc_clnt_swap_activate_callback, NULL);
3122         return 0;
3123 }
3124 EXPORT_SYMBOL_GPL(rpc_clnt_swap_activate);
3125
3126 static int
3127 rpc_clnt_swap_deactivate_callback(struct rpc_clnt *clnt,
3128                 struct rpc_xprt *xprt,
3129                 void *dummy)
3130 {
3131         xprt_disable_swap(xprt);
3132         return 0;
3133 }
3134
3135 void
3136 rpc_clnt_swap_deactivate(struct rpc_clnt *clnt)
3137 {
3138         if (atomic_dec_if_positive(&clnt->cl_swapper) == 0)
3139                 rpc_clnt_iterate_for_each_xprt(clnt,
3140                                 rpc_clnt_swap_deactivate_callback, NULL);
3141 }
3142 EXPORT_SYMBOL_GPL(rpc_clnt_swap_deactivate);
3143 #endif /* CONFIG_SUNRPC_SWAP */