perf tools: Don't clone maps from parent when synthesizing forks
[linux/fpc-iii.git] / net / sunrpc / clnt.c
blobae3b8145da35a236cb24a7aff544b3f99d67547d
1 /*
2 * linux/net/sunrpc/clnt.c
4 * This file contains the high-level RPC interface.
5 * It is modeled as a finite state machine to support both synchronous
6 * and asynchronous requests.
8 * - RPC header generation and argument serialization.
9 * - Credential refresh.
10 * - TCP connect handling.
11 * - Retry of operation when it is suspected the operation failed because
12 * of uid squashing on the server, or when the credentials were stale
13 * and need to be refreshed, or when a packet was damaged in transit.
14 * This may be have to be moved to the VFS layer.
16 * Copyright (C) 1992,1993 Rick Sladkey <jrs@world.std.com>
17 * Copyright (C) 1995,1996 Olaf Kirch <okir@monad.swb.de>
21 #include <linux/module.h>
22 #include <linux/types.h>
23 #include <linux/kallsyms.h>
24 #include <linux/mm.h>
25 #include <linux/namei.h>
26 #include <linux/mount.h>
27 #include <linux/slab.h>
28 #include <linux/rcupdate.h>
29 #include <linux/utsname.h>
30 #include <linux/workqueue.h>
31 #include <linux/in.h>
32 #include <linux/in6.h>
33 #include <linux/un.h>
35 #include <linux/sunrpc/clnt.h>
36 #include <linux/sunrpc/addr.h>
37 #include <linux/sunrpc/rpc_pipe_fs.h>
38 #include <linux/sunrpc/metrics.h>
39 #include <linux/sunrpc/bc_xprt.h>
40 #include <trace/events/sunrpc.h>
42 #include "sunrpc.h"
43 #include "netns.h"
45 #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
46 # define RPCDBG_FACILITY RPCDBG_CALL
47 #endif
49 #define dprint_status(t) \
50 dprintk("RPC: %5u %s (status %d)\n", t->tk_pid, \
51 __func__, t->tk_status)
54 * All RPC clients are linked into this list
57 static DECLARE_WAIT_QUEUE_HEAD(destroy_wait);
60 static void call_start(struct rpc_task *task);
61 static void call_reserve(struct rpc_task *task);
62 static void call_reserveresult(struct rpc_task *task);
63 static void call_allocate(struct rpc_task *task);
64 static void call_encode(struct rpc_task *task);
65 static void call_decode(struct rpc_task *task);
66 static void call_bind(struct rpc_task *task);
67 static void call_bind_status(struct rpc_task *task);
68 static void call_transmit(struct rpc_task *task);
69 #if defined(CONFIG_SUNRPC_BACKCHANNEL)
70 static void call_bc_transmit(struct rpc_task *task);
71 #endif /* CONFIG_SUNRPC_BACKCHANNEL */
72 static void call_status(struct rpc_task *task);
73 static void call_transmit_status(struct rpc_task *task);
74 static void call_refresh(struct rpc_task *task);
75 static void call_refreshresult(struct rpc_task *task);
76 static void call_timeout(struct rpc_task *task);
77 static void call_connect(struct rpc_task *task);
78 static void call_connect_status(struct rpc_task *task);
80 static __be32 *rpc_encode_header(struct rpc_task *task);
81 static __be32 *rpc_verify_header(struct rpc_task *task);
82 static int rpc_ping(struct rpc_clnt *clnt);
84 static void rpc_register_client(struct rpc_clnt *clnt)
86 struct net *net = rpc_net_ns(clnt);
87 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
89 spin_lock(&sn->rpc_client_lock);
90 list_add(&clnt->cl_clients, &sn->all_clients);
91 spin_unlock(&sn->rpc_client_lock);
94 static void rpc_unregister_client(struct rpc_clnt *clnt)
96 struct net *net = rpc_net_ns(clnt);
97 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
99 spin_lock(&sn->rpc_client_lock);
100 list_del(&clnt->cl_clients);
101 spin_unlock(&sn->rpc_client_lock);
104 static void __rpc_clnt_remove_pipedir(struct rpc_clnt *clnt)
106 rpc_remove_client_dir(clnt);
109 static void rpc_clnt_remove_pipedir(struct rpc_clnt *clnt)
111 struct net *net = rpc_net_ns(clnt);
112 struct super_block *pipefs_sb;
114 pipefs_sb = rpc_get_sb_net(net);
115 if (pipefs_sb) {
116 __rpc_clnt_remove_pipedir(clnt);
117 rpc_put_sb_net(net);
121 static struct dentry *rpc_setup_pipedir_sb(struct super_block *sb,
122 struct rpc_clnt *clnt)
124 static uint32_t clntid;
125 const char *dir_name = clnt->cl_program->pipe_dir_name;
126 char name[15];
127 struct dentry *dir, *dentry;
129 dir = rpc_d_lookup_sb(sb, dir_name);
130 if (dir == NULL) {
131 pr_info("RPC: pipefs directory doesn't exist: %s\n", dir_name);
132 return dir;
134 for (;;) {
135 snprintf(name, sizeof(name), "clnt%x", (unsigned int)clntid++);
136 name[sizeof(name) - 1] = '\0';
137 dentry = rpc_create_client_dir(dir, name, clnt);
138 if (!IS_ERR(dentry))
139 break;
140 if (dentry == ERR_PTR(-EEXIST))
141 continue;
142 printk(KERN_INFO "RPC: Couldn't create pipefs entry"
143 " %s/%s, error %ld\n",
144 dir_name, name, PTR_ERR(dentry));
145 break;
147 dput(dir);
148 return dentry;
151 static int
152 rpc_setup_pipedir(struct super_block *pipefs_sb, struct rpc_clnt *clnt)
154 struct dentry *dentry;
156 if (clnt->cl_program->pipe_dir_name != NULL) {
157 dentry = rpc_setup_pipedir_sb(pipefs_sb, clnt);
158 if (IS_ERR(dentry))
159 return PTR_ERR(dentry);
161 return 0;
164 static int rpc_clnt_skip_event(struct rpc_clnt *clnt, unsigned long event)
166 if (clnt->cl_program->pipe_dir_name == NULL)
167 return 1;
169 switch (event) {
170 case RPC_PIPEFS_MOUNT:
171 if (clnt->cl_pipedir_objects.pdh_dentry != NULL)
172 return 1;
173 if (atomic_read(&clnt->cl_count) == 0)
174 return 1;
175 break;
176 case RPC_PIPEFS_UMOUNT:
177 if (clnt->cl_pipedir_objects.pdh_dentry == NULL)
178 return 1;
179 break;
181 return 0;
184 static int __rpc_clnt_handle_event(struct rpc_clnt *clnt, unsigned long event,
185 struct super_block *sb)
187 struct dentry *dentry;
189 switch (event) {
190 case RPC_PIPEFS_MOUNT:
191 dentry = rpc_setup_pipedir_sb(sb, clnt);
192 if (!dentry)
193 return -ENOENT;
194 if (IS_ERR(dentry))
195 return PTR_ERR(dentry);
196 break;
197 case RPC_PIPEFS_UMOUNT:
198 __rpc_clnt_remove_pipedir(clnt);
199 break;
200 default:
201 printk(KERN_ERR "%s: unknown event: %ld\n", __func__, event);
202 return -ENOTSUPP;
204 return 0;
207 static int __rpc_pipefs_event(struct rpc_clnt *clnt, unsigned long event,
208 struct super_block *sb)
210 int error = 0;
212 for (;; clnt = clnt->cl_parent) {
213 if (!rpc_clnt_skip_event(clnt, event))
214 error = __rpc_clnt_handle_event(clnt, event, sb);
215 if (error || clnt == clnt->cl_parent)
216 break;
218 return error;
221 static struct rpc_clnt *rpc_get_client_for_event(struct net *net, int event)
223 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
224 struct rpc_clnt *clnt;
226 spin_lock(&sn->rpc_client_lock);
227 list_for_each_entry(clnt, &sn->all_clients, cl_clients) {
228 if (rpc_clnt_skip_event(clnt, event))
229 continue;
230 spin_unlock(&sn->rpc_client_lock);
231 return clnt;
233 spin_unlock(&sn->rpc_client_lock);
234 return NULL;
237 static int rpc_pipefs_event(struct notifier_block *nb, unsigned long event,
238 void *ptr)
240 struct super_block *sb = ptr;
241 struct rpc_clnt *clnt;
242 int error = 0;
244 while ((clnt = rpc_get_client_for_event(sb->s_fs_info, event))) {
245 error = __rpc_pipefs_event(clnt, event, sb);
246 if (error)
247 break;
249 return error;
252 static struct notifier_block rpc_clients_block = {
253 .notifier_call = rpc_pipefs_event,
254 .priority = SUNRPC_PIPEFS_RPC_PRIO,
257 int rpc_clients_notifier_register(void)
259 return rpc_pipefs_notifier_register(&rpc_clients_block);
262 void rpc_clients_notifier_unregister(void)
264 return rpc_pipefs_notifier_unregister(&rpc_clients_block);
267 static struct rpc_xprt *rpc_clnt_set_transport(struct rpc_clnt *clnt,
268 struct rpc_xprt *xprt,
269 const struct rpc_timeout *timeout)
271 struct rpc_xprt *old;
273 spin_lock(&clnt->cl_lock);
274 old = rcu_dereference_protected(clnt->cl_xprt,
275 lockdep_is_held(&clnt->cl_lock));
277 if (!xprt_bound(xprt))
278 clnt->cl_autobind = 1;
280 clnt->cl_timeout = timeout;
281 rcu_assign_pointer(clnt->cl_xprt, xprt);
282 spin_unlock(&clnt->cl_lock);
284 return old;
287 static void rpc_clnt_set_nodename(struct rpc_clnt *clnt, const char *nodename)
289 clnt->cl_nodelen = strlcpy(clnt->cl_nodename,
290 nodename, sizeof(clnt->cl_nodename));
293 static int rpc_client_register(struct rpc_clnt *clnt,
294 rpc_authflavor_t pseudoflavor,
295 const char *client_name)
297 struct rpc_auth_create_args auth_args = {
298 .pseudoflavor = pseudoflavor,
299 .target_name = client_name,
301 struct rpc_auth *auth;
302 struct net *net = rpc_net_ns(clnt);
303 struct super_block *pipefs_sb;
304 int err;
306 rpc_clnt_debugfs_register(clnt);
308 pipefs_sb = rpc_get_sb_net(net);
309 if (pipefs_sb) {
310 err = rpc_setup_pipedir(pipefs_sb, clnt);
311 if (err)
312 goto out;
315 rpc_register_client(clnt);
316 if (pipefs_sb)
317 rpc_put_sb_net(net);
319 auth = rpcauth_create(&auth_args, clnt);
320 if (IS_ERR(auth)) {
321 dprintk("RPC: Couldn't create auth handle (flavor %u)\n",
322 pseudoflavor);
323 err = PTR_ERR(auth);
324 goto err_auth;
326 return 0;
327 err_auth:
328 pipefs_sb = rpc_get_sb_net(net);
329 rpc_unregister_client(clnt);
330 __rpc_clnt_remove_pipedir(clnt);
331 out:
332 if (pipefs_sb)
333 rpc_put_sb_net(net);
334 rpc_clnt_debugfs_unregister(clnt);
335 return err;
338 static DEFINE_IDA(rpc_clids);
340 void rpc_cleanup_clids(void)
342 ida_destroy(&rpc_clids);
345 static int rpc_alloc_clid(struct rpc_clnt *clnt)
347 int clid;
349 clid = ida_simple_get(&rpc_clids, 0, 0, GFP_KERNEL);
350 if (clid < 0)
351 return clid;
352 clnt->cl_clid = clid;
353 return 0;
356 static void rpc_free_clid(struct rpc_clnt *clnt)
358 ida_simple_remove(&rpc_clids, clnt->cl_clid);
361 static struct rpc_clnt * rpc_new_client(const struct rpc_create_args *args,
362 struct rpc_xprt_switch *xps,
363 struct rpc_xprt *xprt,
364 struct rpc_clnt *parent)
366 const struct rpc_program *program = args->program;
367 const struct rpc_version *version;
368 struct rpc_clnt *clnt = NULL;
369 const struct rpc_timeout *timeout;
370 const char *nodename = args->nodename;
371 int err;
373 /* sanity check the name before trying to print it */
374 dprintk("RPC: creating %s client for %s (xprt %p)\n",
375 program->name, args->servername, xprt);
377 err = rpciod_up();
378 if (err)
379 goto out_no_rpciod;
381 err = -EINVAL;
382 if (args->version >= program->nrvers)
383 goto out_err;
384 version = program->version[args->version];
385 if (version == NULL)
386 goto out_err;
388 err = -ENOMEM;
389 clnt = kzalloc(sizeof(*clnt), GFP_KERNEL);
390 if (!clnt)
391 goto out_err;
392 clnt->cl_parent = parent ? : clnt;
394 err = rpc_alloc_clid(clnt);
395 if (err)
396 goto out_no_clid;
398 clnt->cl_procinfo = version->procs;
399 clnt->cl_maxproc = version->nrprocs;
400 clnt->cl_prog = args->prognumber ? : program->number;
401 clnt->cl_vers = version->number;
402 clnt->cl_stats = program->stats;
403 clnt->cl_metrics = rpc_alloc_iostats(clnt);
404 rpc_init_pipe_dir_head(&clnt->cl_pipedir_objects);
405 err = -ENOMEM;
406 if (clnt->cl_metrics == NULL)
407 goto out_no_stats;
408 clnt->cl_program = program;
409 INIT_LIST_HEAD(&clnt->cl_tasks);
410 spin_lock_init(&clnt->cl_lock);
412 timeout = xprt->timeout;
413 if (args->timeout != NULL) {
414 memcpy(&clnt->cl_timeout_default, args->timeout,
415 sizeof(clnt->cl_timeout_default));
416 timeout = &clnt->cl_timeout_default;
419 rpc_clnt_set_transport(clnt, xprt, timeout);
420 xprt_iter_init(&clnt->cl_xpi, xps);
421 xprt_switch_put(xps);
423 clnt->cl_rtt = &clnt->cl_rtt_default;
424 rpc_init_rtt(&clnt->cl_rtt_default, clnt->cl_timeout->to_initval);
426 atomic_set(&clnt->cl_count, 1);
428 if (nodename == NULL)
429 nodename = utsname()->nodename;
430 /* save the nodename */
431 rpc_clnt_set_nodename(clnt, nodename);
433 err = rpc_client_register(clnt, args->authflavor, args->client_name);
434 if (err)
435 goto out_no_path;
436 if (parent)
437 atomic_inc(&parent->cl_count);
438 return clnt;
440 out_no_path:
441 rpc_free_iostats(clnt->cl_metrics);
442 out_no_stats:
443 rpc_free_clid(clnt);
444 out_no_clid:
445 kfree(clnt);
446 out_err:
447 rpciod_down();
448 out_no_rpciod:
449 xprt_switch_put(xps);
450 xprt_put(xprt);
451 return ERR_PTR(err);
454 static struct rpc_clnt *rpc_create_xprt(struct rpc_create_args *args,
455 struct rpc_xprt *xprt)
457 struct rpc_clnt *clnt = NULL;
458 struct rpc_xprt_switch *xps;
460 if (args->bc_xprt && args->bc_xprt->xpt_bc_xps) {
461 WARN_ON_ONCE(!(args->protocol & XPRT_TRANSPORT_BC));
462 xps = args->bc_xprt->xpt_bc_xps;
463 xprt_switch_get(xps);
464 } else {
465 xps = xprt_switch_alloc(xprt, GFP_KERNEL);
466 if (xps == NULL) {
467 xprt_put(xprt);
468 return ERR_PTR(-ENOMEM);
470 if (xprt->bc_xprt) {
471 xprt_switch_get(xps);
472 xprt->bc_xprt->xpt_bc_xps = xps;
475 clnt = rpc_new_client(args, xps, xprt, NULL);
476 if (IS_ERR(clnt))
477 return clnt;
479 if (!(args->flags & RPC_CLNT_CREATE_NOPING)) {
480 int err = rpc_ping(clnt);
481 if (err != 0) {
482 rpc_shutdown_client(clnt);
483 return ERR_PTR(err);
487 clnt->cl_softrtry = 1;
488 if (args->flags & RPC_CLNT_CREATE_HARDRTRY)
489 clnt->cl_softrtry = 0;
491 if (args->flags & RPC_CLNT_CREATE_AUTOBIND)
492 clnt->cl_autobind = 1;
493 if (args->flags & RPC_CLNT_CREATE_NO_RETRANS_TIMEOUT)
494 clnt->cl_noretranstimeo = 1;
495 if (args->flags & RPC_CLNT_CREATE_DISCRTRY)
496 clnt->cl_discrtry = 1;
497 if (!(args->flags & RPC_CLNT_CREATE_QUIET))
498 clnt->cl_chatty = 1;
500 return clnt;
504 * rpc_create - create an RPC client and transport with one call
505 * @args: rpc_clnt create argument structure
507 * Creates and initializes an RPC transport and an RPC client.
509 * It can ping the server in order to determine if it is up, and to see if
510 * it supports this program and version. RPC_CLNT_CREATE_NOPING disables
511 * this behavior so asynchronous tasks can also use rpc_create.
513 struct rpc_clnt *rpc_create(struct rpc_create_args *args)
515 struct rpc_xprt *xprt;
516 struct xprt_create xprtargs = {
517 .net = args->net,
518 .ident = args->protocol,
519 .srcaddr = args->saddress,
520 .dstaddr = args->address,
521 .addrlen = args->addrsize,
522 .servername = args->servername,
523 .bc_xprt = args->bc_xprt,
525 char servername[48];
527 if (args->bc_xprt) {
528 WARN_ON_ONCE(!(args->protocol & XPRT_TRANSPORT_BC));
529 xprt = args->bc_xprt->xpt_bc_xprt;
530 if (xprt) {
531 xprt_get(xprt);
532 return rpc_create_xprt(args, xprt);
536 if (args->flags & RPC_CLNT_CREATE_INFINITE_SLOTS)
537 xprtargs.flags |= XPRT_CREATE_INFINITE_SLOTS;
538 if (args->flags & RPC_CLNT_CREATE_NO_IDLE_TIMEOUT)
539 xprtargs.flags |= XPRT_CREATE_NO_IDLE_TIMEOUT;
541 * If the caller chooses not to specify a hostname, whip
542 * up a string representation of the passed-in address.
544 if (xprtargs.servername == NULL) {
545 struct sockaddr_un *sun =
546 (struct sockaddr_un *)args->address;
547 struct sockaddr_in *sin =
548 (struct sockaddr_in *)args->address;
549 struct sockaddr_in6 *sin6 =
550 (struct sockaddr_in6 *)args->address;
552 servername[0] = '\0';
553 switch (args->address->sa_family) {
554 case AF_LOCAL:
555 snprintf(servername, sizeof(servername), "%s",
556 sun->sun_path);
557 break;
558 case AF_INET:
559 snprintf(servername, sizeof(servername), "%pI4",
560 &sin->sin_addr.s_addr);
561 break;
562 case AF_INET6:
563 snprintf(servername, sizeof(servername), "%pI6",
564 &sin6->sin6_addr);
565 break;
566 default:
567 /* caller wants default server name, but
568 * address family isn't recognized. */
569 return ERR_PTR(-EINVAL);
571 xprtargs.servername = servername;
574 xprt = xprt_create_transport(&xprtargs);
575 if (IS_ERR(xprt))
576 return (struct rpc_clnt *)xprt;
579 * By default, kernel RPC client connects from a reserved port.
580 * CAP_NET_BIND_SERVICE will not be set for unprivileged requesters,
581 * but it is always enabled for rpciod, which handles the connect
582 * operation.
584 xprt->resvport = 1;
585 if (args->flags & RPC_CLNT_CREATE_NONPRIVPORT)
586 xprt->resvport = 0;
588 return rpc_create_xprt(args, xprt);
590 EXPORT_SYMBOL_GPL(rpc_create);
593 * This function clones the RPC client structure. It allows us to share the
594 * same transport while varying parameters such as the authentication
595 * flavour.
597 static struct rpc_clnt *__rpc_clone_client(struct rpc_create_args *args,
598 struct rpc_clnt *clnt)
600 struct rpc_xprt_switch *xps;
601 struct rpc_xprt *xprt;
602 struct rpc_clnt *new;
603 int err;
605 err = -ENOMEM;
606 rcu_read_lock();
607 xprt = xprt_get(rcu_dereference(clnt->cl_xprt));
608 xps = xprt_switch_get(rcu_dereference(clnt->cl_xpi.xpi_xpswitch));
609 rcu_read_unlock();
610 if (xprt == NULL || xps == NULL) {
611 xprt_put(xprt);
612 xprt_switch_put(xps);
613 goto out_err;
615 args->servername = xprt->servername;
616 args->nodename = clnt->cl_nodename;
618 new = rpc_new_client(args, xps, xprt, clnt);
619 if (IS_ERR(new)) {
620 err = PTR_ERR(new);
621 goto out_err;
624 /* Turn off autobind on clones */
625 new->cl_autobind = 0;
626 new->cl_softrtry = clnt->cl_softrtry;
627 new->cl_noretranstimeo = clnt->cl_noretranstimeo;
628 new->cl_discrtry = clnt->cl_discrtry;
629 new->cl_chatty = clnt->cl_chatty;
630 return new;
632 out_err:
633 dprintk("RPC: %s: returned error %d\n", __func__, err);
634 return ERR_PTR(err);
638 * rpc_clone_client - Clone an RPC client structure
640 * @clnt: RPC client whose parameters are copied
642 * Returns a fresh RPC client or an ERR_PTR.
644 struct rpc_clnt *rpc_clone_client(struct rpc_clnt *clnt)
646 struct rpc_create_args args = {
647 .program = clnt->cl_program,
648 .prognumber = clnt->cl_prog,
649 .version = clnt->cl_vers,
650 .authflavor = clnt->cl_auth->au_flavor,
652 return __rpc_clone_client(&args, clnt);
654 EXPORT_SYMBOL_GPL(rpc_clone_client);
657 * rpc_clone_client_set_auth - Clone an RPC client structure and set its auth
659 * @clnt: RPC client whose parameters are copied
660 * @flavor: security flavor for new client
662 * Returns a fresh RPC client or an ERR_PTR.
664 struct rpc_clnt *
665 rpc_clone_client_set_auth(struct rpc_clnt *clnt, rpc_authflavor_t flavor)
667 struct rpc_create_args args = {
668 .program = clnt->cl_program,
669 .prognumber = clnt->cl_prog,
670 .version = clnt->cl_vers,
671 .authflavor = flavor,
673 return __rpc_clone_client(&args, clnt);
675 EXPORT_SYMBOL_GPL(rpc_clone_client_set_auth);
678 * rpc_switch_client_transport: switch the RPC transport on the fly
679 * @clnt: pointer to a struct rpc_clnt
680 * @args: pointer to the new transport arguments
681 * @timeout: pointer to the new timeout parameters
683 * This function allows the caller to switch the RPC transport for the
684 * rpc_clnt structure 'clnt' to allow it to connect to a mirrored NFS
685 * server, for instance. It assumes that the caller has ensured that
686 * there are no active RPC tasks by using some form of locking.
688 * Returns zero if "clnt" is now using the new xprt. Otherwise a
689 * negative errno is returned, and "clnt" continues to use the old
690 * xprt.
692 int rpc_switch_client_transport(struct rpc_clnt *clnt,
693 struct xprt_create *args,
694 const struct rpc_timeout *timeout)
696 const struct rpc_timeout *old_timeo;
697 rpc_authflavor_t pseudoflavor;
698 struct rpc_xprt_switch *xps, *oldxps;
699 struct rpc_xprt *xprt, *old;
700 struct rpc_clnt *parent;
701 int err;
703 xprt = xprt_create_transport(args);
704 if (IS_ERR(xprt)) {
705 dprintk("RPC: failed to create new xprt for clnt %p\n",
706 clnt);
707 return PTR_ERR(xprt);
710 xps = xprt_switch_alloc(xprt, GFP_KERNEL);
711 if (xps == NULL) {
712 xprt_put(xprt);
713 return -ENOMEM;
716 pseudoflavor = clnt->cl_auth->au_flavor;
718 old_timeo = clnt->cl_timeout;
719 old = rpc_clnt_set_transport(clnt, xprt, timeout);
720 oldxps = xprt_iter_xchg_switch(&clnt->cl_xpi, xps);
722 rpc_unregister_client(clnt);
723 __rpc_clnt_remove_pipedir(clnt);
724 rpc_clnt_debugfs_unregister(clnt);
727 * A new transport was created. "clnt" therefore
728 * becomes the root of a new cl_parent tree. clnt's
729 * children, if it has any, still point to the old xprt.
731 parent = clnt->cl_parent;
732 clnt->cl_parent = clnt;
735 * The old rpc_auth cache cannot be re-used. GSS
736 * contexts in particular are between a single
737 * client and server.
739 err = rpc_client_register(clnt, pseudoflavor, NULL);
740 if (err)
741 goto out_revert;
743 synchronize_rcu();
744 if (parent != clnt)
745 rpc_release_client(parent);
746 xprt_switch_put(oldxps);
747 xprt_put(old);
748 dprintk("RPC: replaced xprt for clnt %p\n", clnt);
749 return 0;
751 out_revert:
752 xps = xprt_iter_xchg_switch(&clnt->cl_xpi, oldxps);
753 rpc_clnt_set_transport(clnt, old, old_timeo);
754 clnt->cl_parent = parent;
755 rpc_client_register(clnt, pseudoflavor, NULL);
756 xprt_switch_put(xps);
757 xprt_put(xprt);
758 dprintk("RPC: failed to switch xprt for clnt %p\n", clnt);
759 return err;
761 EXPORT_SYMBOL_GPL(rpc_switch_client_transport);
763 static
764 int rpc_clnt_xprt_iter_init(struct rpc_clnt *clnt, struct rpc_xprt_iter *xpi)
766 struct rpc_xprt_switch *xps;
768 rcu_read_lock();
769 xps = xprt_switch_get(rcu_dereference(clnt->cl_xpi.xpi_xpswitch));
770 rcu_read_unlock();
771 if (xps == NULL)
772 return -EAGAIN;
773 xprt_iter_init_listall(xpi, xps);
774 xprt_switch_put(xps);
775 return 0;
779 * rpc_clnt_iterate_for_each_xprt - Apply a function to all transports
780 * @clnt: pointer to client
781 * @fn: function to apply
782 * @data: void pointer to function data
784 * Iterates through the list of RPC transports currently attached to the
785 * client and applies the function fn(clnt, xprt, data).
787 * On error, the iteration stops, and the function returns the error value.
789 int rpc_clnt_iterate_for_each_xprt(struct rpc_clnt *clnt,
790 int (*fn)(struct rpc_clnt *, struct rpc_xprt *, void *),
791 void *data)
793 struct rpc_xprt_iter xpi;
794 int ret;
796 ret = rpc_clnt_xprt_iter_init(clnt, &xpi);
797 if (ret)
798 return ret;
799 for (;;) {
800 struct rpc_xprt *xprt = xprt_iter_get_next(&xpi);
802 if (!xprt)
803 break;
804 ret = fn(clnt, xprt, data);
805 xprt_put(xprt);
806 if (ret < 0)
807 break;
809 xprt_iter_destroy(&xpi);
810 return ret;
812 EXPORT_SYMBOL_GPL(rpc_clnt_iterate_for_each_xprt);
815 * Kill all tasks for the given client.
816 * XXX: kill their descendants as well?
818 void rpc_killall_tasks(struct rpc_clnt *clnt)
820 struct rpc_task *rovr;
823 if (list_empty(&clnt->cl_tasks))
824 return;
825 dprintk("RPC: killing all tasks for client %p\n", clnt);
827 * Spin lock all_tasks to prevent changes...
829 spin_lock(&clnt->cl_lock);
830 list_for_each_entry(rovr, &clnt->cl_tasks, tk_task) {
831 if (!RPC_IS_ACTIVATED(rovr))
832 continue;
833 if (!(rovr->tk_flags & RPC_TASK_KILLED)) {
834 rovr->tk_flags |= RPC_TASK_KILLED;
835 rpc_exit(rovr, -EIO);
836 if (RPC_IS_QUEUED(rovr))
837 rpc_wake_up_queued_task(rovr->tk_waitqueue,
838 rovr);
841 spin_unlock(&clnt->cl_lock);
843 EXPORT_SYMBOL_GPL(rpc_killall_tasks);
846 * Properly shut down an RPC client, terminating all outstanding
847 * requests.
849 void rpc_shutdown_client(struct rpc_clnt *clnt)
851 might_sleep();
853 dprintk_rcu("RPC: shutting down %s client for %s\n",
854 clnt->cl_program->name,
855 rcu_dereference(clnt->cl_xprt)->servername);
857 while (!list_empty(&clnt->cl_tasks)) {
858 rpc_killall_tasks(clnt);
859 wait_event_timeout(destroy_wait,
860 list_empty(&clnt->cl_tasks), 1*HZ);
863 rpc_release_client(clnt);
865 EXPORT_SYMBOL_GPL(rpc_shutdown_client);
868 * Free an RPC client
870 static struct rpc_clnt *
871 rpc_free_client(struct rpc_clnt *clnt)
873 struct rpc_clnt *parent = NULL;
875 dprintk_rcu("RPC: destroying %s client for %s\n",
876 clnt->cl_program->name,
877 rcu_dereference(clnt->cl_xprt)->servername);
878 if (clnt->cl_parent != clnt)
879 parent = clnt->cl_parent;
880 rpc_clnt_debugfs_unregister(clnt);
881 rpc_clnt_remove_pipedir(clnt);
882 rpc_unregister_client(clnt);
883 rpc_free_iostats(clnt->cl_metrics);
884 clnt->cl_metrics = NULL;
885 xprt_put(rcu_dereference_raw(clnt->cl_xprt));
886 xprt_iter_destroy(&clnt->cl_xpi);
887 rpciod_down();
888 rpc_free_clid(clnt);
889 kfree(clnt);
890 return parent;
894 * Free an RPC client
896 static struct rpc_clnt *
897 rpc_free_auth(struct rpc_clnt *clnt)
899 if (clnt->cl_auth == NULL)
900 return rpc_free_client(clnt);
903 * Note: RPCSEC_GSS may need to send NULL RPC calls in order to
904 * release remaining GSS contexts. This mechanism ensures
905 * that it can do so safely.
907 atomic_inc(&clnt->cl_count);
908 rpcauth_release(clnt->cl_auth);
909 clnt->cl_auth = NULL;
910 if (atomic_dec_and_test(&clnt->cl_count))
911 return rpc_free_client(clnt);
912 return NULL;
916 * Release reference to the RPC client
918 void
919 rpc_release_client(struct rpc_clnt *clnt)
921 dprintk("RPC: rpc_release_client(%p)\n", clnt);
923 do {
924 if (list_empty(&clnt->cl_tasks))
925 wake_up(&destroy_wait);
926 if (!atomic_dec_and_test(&clnt->cl_count))
927 break;
928 clnt = rpc_free_auth(clnt);
929 } while (clnt != NULL);
931 EXPORT_SYMBOL_GPL(rpc_release_client);
934 * rpc_bind_new_program - bind a new RPC program to an existing client
935 * @old: old rpc_client
936 * @program: rpc program to set
937 * @vers: rpc program version
939 * Clones the rpc client and sets up a new RPC program. This is mainly
940 * of use for enabling different RPC programs to share the same transport.
941 * The Sun NFSv2/v3 ACL protocol can do this.
943 struct rpc_clnt *rpc_bind_new_program(struct rpc_clnt *old,
944 const struct rpc_program *program,
945 u32 vers)
947 struct rpc_create_args args = {
948 .program = program,
949 .prognumber = program->number,
950 .version = vers,
951 .authflavor = old->cl_auth->au_flavor,
953 struct rpc_clnt *clnt;
954 int err;
956 clnt = __rpc_clone_client(&args, old);
957 if (IS_ERR(clnt))
958 goto out;
959 err = rpc_ping(clnt);
960 if (err != 0) {
961 rpc_shutdown_client(clnt);
962 clnt = ERR_PTR(err);
964 out:
965 return clnt;
967 EXPORT_SYMBOL_GPL(rpc_bind_new_program);
969 void rpc_task_release_transport(struct rpc_task *task)
971 struct rpc_xprt *xprt = task->tk_xprt;
973 if (xprt) {
974 task->tk_xprt = NULL;
975 xprt_put(xprt);
978 EXPORT_SYMBOL_GPL(rpc_task_release_transport);
980 void rpc_task_release_client(struct rpc_task *task)
982 struct rpc_clnt *clnt = task->tk_client;
984 if (clnt != NULL) {
985 /* Remove from client task list */
986 spin_lock(&clnt->cl_lock);
987 list_del(&task->tk_task);
988 spin_unlock(&clnt->cl_lock);
989 task->tk_client = NULL;
991 rpc_release_client(clnt);
993 rpc_task_release_transport(task);
996 static
997 void rpc_task_set_transport(struct rpc_task *task, struct rpc_clnt *clnt)
999 if (!task->tk_xprt)
1000 task->tk_xprt = xprt_iter_get_next(&clnt->cl_xpi);
1003 static
1004 void rpc_task_set_client(struct rpc_task *task, struct rpc_clnt *clnt)
1007 if (clnt != NULL) {
1008 rpc_task_set_transport(task, clnt);
1009 task->tk_client = clnt;
1010 atomic_inc(&clnt->cl_count);
1011 if (clnt->cl_softrtry)
1012 task->tk_flags |= RPC_TASK_SOFT;
1013 if (clnt->cl_noretranstimeo)
1014 task->tk_flags |= RPC_TASK_NO_RETRANS_TIMEOUT;
1015 if (atomic_read(&clnt->cl_swapper))
1016 task->tk_flags |= RPC_TASK_SWAPPER;
1017 /* Add to the client's list of all tasks */
1018 spin_lock(&clnt->cl_lock);
1019 list_add_tail(&task->tk_task, &clnt->cl_tasks);
1020 spin_unlock(&clnt->cl_lock);
1024 static void
1025 rpc_task_set_rpc_message(struct rpc_task *task, const struct rpc_message *msg)
1027 if (msg != NULL) {
1028 task->tk_msg.rpc_proc = msg->rpc_proc;
1029 task->tk_msg.rpc_argp = msg->rpc_argp;
1030 task->tk_msg.rpc_resp = msg->rpc_resp;
1031 if (msg->rpc_cred != NULL)
1032 task->tk_msg.rpc_cred = get_rpccred(msg->rpc_cred);
1037 * Default callback for async RPC calls
1039 static void
1040 rpc_default_callback(struct rpc_task *task, void *data)
1044 static const struct rpc_call_ops rpc_default_ops = {
1045 .rpc_call_done = rpc_default_callback,
1049 * rpc_run_task - Allocate a new RPC task, then run rpc_execute against it
1050 * @task_setup_data: pointer to task initialisation data
1052 struct rpc_task *rpc_run_task(const struct rpc_task_setup *task_setup_data)
1054 struct rpc_task *task;
1056 task = rpc_new_task(task_setup_data);
1058 rpc_task_set_client(task, task_setup_data->rpc_client);
1059 rpc_task_set_rpc_message(task, task_setup_data->rpc_message);
1061 if (task->tk_action == NULL)
1062 rpc_call_start(task);
1064 atomic_inc(&task->tk_count);
1065 rpc_execute(task);
1066 return task;
1068 EXPORT_SYMBOL_GPL(rpc_run_task);
1071 * rpc_call_sync - Perform a synchronous RPC call
1072 * @clnt: pointer to RPC client
1073 * @msg: RPC call parameters
1074 * @flags: RPC call flags
1076 int rpc_call_sync(struct rpc_clnt *clnt, const struct rpc_message *msg, int flags)
1078 struct rpc_task *task;
1079 struct rpc_task_setup task_setup_data = {
1080 .rpc_client = clnt,
1081 .rpc_message = msg,
1082 .callback_ops = &rpc_default_ops,
1083 .flags = flags,
1085 int status;
1087 WARN_ON_ONCE(flags & RPC_TASK_ASYNC);
1088 if (flags & RPC_TASK_ASYNC) {
1089 rpc_release_calldata(task_setup_data.callback_ops,
1090 task_setup_data.callback_data);
1091 return -EINVAL;
1094 task = rpc_run_task(&task_setup_data);
1095 if (IS_ERR(task))
1096 return PTR_ERR(task);
1097 status = task->tk_status;
1098 rpc_put_task(task);
1099 return status;
1101 EXPORT_SYMBOL_GPL(rpc_call_sync);
1104 * rpc_call_async - Perform an asynchronous RPC call
1105 * @clnt: pointer to RPC client
1106 * @msg: RPC call parameters
1107 * @flags: RPC call flags
1108 * @tk_ops: RPC call ops
1109 * @data: user call data
1112 rpc_call_async(struct rpc_clnt *clnt, const struct rpc_message *msg, int flags,
1113 const struct rpc_call_ops *tk_ops, void *data)
1115 struct rpc_task *task;
1116 struct rpc_task_setup task_setup_data = {
1117 .rpc_client = clnt,
1118 .rpc_message = msg,
1119 .callback_ops = tk_ops,
1120 .callback_data = data,
1121 .flags = flags|RPC_TASK_ASYNC,
1124 task = rpc_run_task(&task_setup_data);
1125 if (IS_ERR(task))
1126 return PTR_ERR(task);
1127 rpc_put_task(task);
1128 return 0;
1130 EXPORT_SYMBOL_GPL(rpc_call_async);
1132 #if defined(CONFIG_SUNRPC_BACKCHANNEL)
1134 * rpc_run_bc_task - Allocate a new RPC task for backchannel use, then run
1135 * rpc_execute against it
1136 * @req: RPC request
1138 struct rpc_task *rpc_run_bc_task(struct rpc_rqst *req)
1140 struct rpc_task *task;
1141 struct rpc_task_setup task_setup_data = {
1142 .callback_ops = &rpc_default_ops,
1143 .flags = RPC_TASK_SOFTCONN |
1144 RPC_TASK_NO_RETRANS_TIMEOUT,
1147 dprintk("RPC: rpc_run_bc_task req= %p\n", req);
1149 * Create an rpc_task to send the data
1151 task = rpc_new_task(&task_setup_data);
1152 xprt_init_bc_request(req, task);
1154 task->tk_action = call_bc_transmit;
1155 atomic_inc(&task->tk_count);
1156 WARN_ON_ONCE(atomic_read(&task->tk_count) != 2);
1157 rpc_execute(task);
1159 dprintk("RPC: rpc_run_bc_task: task= %p\n", task);
1160 return task;
1162 #endif /* CONFIG_SUNRPC_BACKCHANNEL */
1164 void
1165 rpc_call_start(struct rpc_task *task)
1167 task->tk_action = call_start;
1169 EXPORT_SYMBOL_GPL(rpc_call_start);
1172 * rpc_peeraddr - extract remote peer address from clnt's xprt
1173 * @clnt: RPC client structure
1174 * @buf: target buffer
1175 * @bufsize: length of target buffer
1177 * Returns the number of bytes that are actually in the stored address.
1179 size_t rpc_peeraddr(struct rpc_clnt *clnt, struct sockaddr *buf, size_t bufsize)
1181 size_t bytes;
1182 struct rpc_xprt *xprt;
1184 rcu_read_lock();
1185 xprt = rcu_dereference(clnt->cl_xprt);
1187 bytes = xprt->addrlen;
1188 if (bytes > bufsize)
1189 bytes = bufsize;
1190 memcpy(buf, &xprt->addr, bytes);
1191 rcu_read_unlock();
1193 return bytes;
1195 EXPORT_SYMBOL_GPL(rpc_peeraddr);
1198 * rpc_peeraddr2str - return remote peer address in printable format
1199 * @clnt: RPC client structure
1200 * @format: address format
1202 * NB: the lifetime of the memory referenced by the returned pointer is
1203 * the same as the rpc_xprt itself. As long as the caller uses this
1204 * pointer, it must hold the RCU read lock.
1206 const char *rpc_peeraddr2str(struct rpc_clnt *clnt,
1207 enum rpc_display_format_t format)
1209 struct rpc_xprt *xprt;
1211 xprt = rcu_dereference(clnt->cl_xprt);
1213 if (xprt->address_strings[format] != NULL)
1214 return xprt->address_strings[format];
1215 else
1216 return "unprintable";
1218 EXPORT_SYMBOL_GPL(rpc_peeraddr2str);
1220 static const struct sockaddr_in rpc_inaddr_loopback = {
1221 .sin_family = AF_INET,
1222 .sin_addr.s_addr = htonl(INADDR_ANY),
1225 static const struct sockaddr_in6 rpc_in6addr_loopback = {
1226 .sin6_family = AF_INET6,
1227 .sin6_addr = IN6ADDR_ANY_INIT,
1231 * Try a getsockname() on a connected datagram socket. Using a
1232 * connected datagram socket prevents leaving a socket in TIME_WAIT.
1233 * This conserves the ephemeral port number space.
1235 * Returns zero and fills in "buf" if successful; otherwise, a
1236 * negative errno is returned.
1238 static int rpc_sockname(struct net *net, struct sockaddr *sap, size_t salen,
1239 struct sockaddr *buf)
1241 struct socket *sock;
1242 int err;
1244 err = __sock_create(net, sap->sa_family,
1245 SOCK_DGRAM, IPPROTO_UDP, &sock, 1);
1246 if (err < 0) {
1247 dprintk("RPC: can't create UDP socket (%d)\n", err);
1248 goto out;
1251 switch (sap->sa_family) {
1252 case AF_INET:
1253 err = kernel_bind(sock,
1254 (struct sockaddr *)&rpc_inaddr_loopback,
1255 sizeof(rpc_inaddr_loopback));
1256 break;
1257 case AF_INET6:
1258 err = kernel_bind(sock,
1259 (struct sockaddr *)&rpc_in6addr_loopback,
1260 sizeof(rpc_in6addr_loopback));
1261 break;
1262 default:
1263 err = -EAFNOSUPPORT;
1264 goto out;
1266 if (err < 0) {
1267 dprintk("RPC: can't bind UDP socket (%d)\n", err);
1268 goto out_release;
1271 err = kernel_connect(sock, sap, salen, 0);
1272 if (err < 0) {
1273 dprintk("RPC: can't connect UDP socket (%d)\n", err);
1274 goto out_release;
1277 err = kernel_getsockname(sock, buf);
1278 if (err < 0) {
1279 dprintk("RPC: getsockname failed (%d)\n", err);
1280 goto out_release;
1283 err = 0;
1284 if (buf->sa_family == AF_INET6) {
1285 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)buf;
1286 sin6->sin6_scope_id = 0;
1288 dprintk("RPC: %s succeeded\n", __func__);
1290 out_release:
1291 sock_release(sock);
1292 out:
1293 return err;
1297 * Scraping a connected socket failed, so we don't have a useable
1298 * local address. Fallback: generate an address that will prevent
1299 * the server from calling us back.
1301 * Returns zero and fills in "buf" if successful; otherwise, a
1302 * negative errno is returned.
1304 static int rpc_anyaddr(int family, struct sockaddr *buf, size_t buflen)
1306 switch (family) {
1307 case AF_INET:
1308 if (buflen < sizeof(rpc_inaddr_loopback))
1309 return -EINVAL;
1310 memcpy(buf, &rpc_inaddr_loopback,
1311 sizeof(rpc_inaddr_loopback));
1312 break;
1313 case AF_INET6:
1314 if (buflen < sizeof(rpc_in6addr_loopback))
1315 return -EINVAL;
1316 memcpy(buf, &rpc_in6addr_loopback,
1317 sizeof(rpc_in6addr_loopback));
1318 break;
1319 default:
1320 dprintk("RPC: %s: address family not supported\n",
1321 __func__);
1322 return -EAFNOSUPPORT;
1324 dprintk("RPC: %s: succeeded\n", __func__);
1325 return 0;
1329 * rpc_localaddr - discover local endpoint address for an RPC client
1330 * @clnt: RPC client structure
1331 * @buf: target buffer
1332 * @buflen: size of target buffer, in bytes
1334 * Returns zero and fills in "buf" and "buflen" if successful;
1335 * otherwise, a negative errno is returned.
1337 * This works even if the underlying transport is not currently connected,
1338 * or if the upper layer never previously provided a source address.
1340 * The result of this function call is transient: multiple calls in
1341 * succession may give different results, depending on how local
1342 * networking configuration changes over time.
1344 int rpc_localaddr(struct rpc_clnt *clnt, struct sockaddr *buf, size_t buflen)
1346 struct sockaddr_storage address;
1347 struct sockaddr *sap = (struct sockaddr *)&address;
1348 struct rpc_xprt *xprt;
1349 struct net *net;
1350 size_t salen;
1351 int err;
1353 rcu_read_lock();
1354 xprt = rcu_dereference(clnt->cl_xprt);
1355 salen = xprt->addrlen;
1356 memcpy(sap, &xprt->addr, salen);
1357 net = get_net(xprt->xprt_net);
1358 rcu_read_unlock();
1360 rpc_set_port(sap, 0);
1361 err = rpc_sockname(net, sap, salen, buf);
1362 put_net(net);
1363 if (err != 0)
1364 /* Couldn't discover local address, return ANYADDR */
1365 return rpc_anyaddr(sap->sa_family, buf, buflen);
1366 return 0;
1368 EXPORT_SYMBOL_GPL(rpc_localaddr);
1370 void
1371 rpc_setbufsize(struct rpc_clnt *clnt, unsigned int sndsize, unsigned int rcvsize)
1373 struct rpc_xprt *xprt;
1375 rcu_read_lock();
1376 xprt = rcu_dereference(clnt->cl_xprt);
1377 if (xprt->ops->set_buffer_size)
1378 xprt->ops->set_buffer_size(xprt, sndsize, rcvsize);
1379 rcu_read_unlock();
1381 EXPORT_SYMBOL_GPL(rpc_setbufsize);
1384 * rpc_net_ns - Get the network namespace for this RPC client
1385 * @clnt: RPC client to query
1388 struct net *rpc_net_ns(struct rpc_clnt *clnt)
1390 struct net *ret;
1392 rcu_read_lock();
1393 ret = rcu_dereference(clnt->cl_xprt)->xprt_net;
1394 rcu_read_unlock();
1395 return ret;
1397 EXPORT_SYMBOL_GPL(rpc_net_ns);
1400 * rpc_max_payload - Get maximum payload size for a transport, in bytes
1401 * @clnt: RPC client to query
1403 * For stream transports, this is one RPC record fragment (see RFC
1404 * 1831), as we don't support multi-record requests yet. For datagram
1405 * transports, this is the size of an IP packet minus the IP, UDP, and
1406 * RPC header sizes.
1408 size_t rpc_max_payload(struct rpc_clnt *clnt)
1410 size_t ret;
1412 rcu_read_lock();
1413 ret = rcu_dereference(clnt->cl_xprt)->max_payload;
1414 rcu_read_unlock();
1415 return ret;
1417 EXPORT_SYMBOL_GPL(rpc_max_payload);
1420 * rpc_max_bc_payload - Get maximum backchannel payload size, in bytes
1421 * @clnt: RPC client to query
1423 size_t rpc_max_bc_payload(struct rpc_clnt *clnt)
1425 struct rpc_xprt *xprt;
1426 size_t ret;
1428 rcu_read_lock();
1429 xprt = rcu_dereference(clnt->cl_xprt);
1430 ret = xprt->ops->bc_maxpayload(xprt);
1431 rcu_read_unlock();
1432 return ret;
1434 EXPORT_SYMBOL_GPL(rpc_max_bc_payload);
1437 * rpc_force_rebind - force transport to check that remote port is unchanged
1438 * @clnt: client to rebind
1441 void rpc_force_rebind(struct rpc_clnt *clnt)
1443 if (clnt->cl_autobind) {
1444 rcu_read_lock();
1445 xprt_clear_bound(rcu_dereference(clnt->cl_xprt));
1446 rcu_read_unlock();
1449 EXPORT_SYMBOL_GPL(rpc_force_rebind);
1452 * Restart an (async) RPC call from the call_prepare state.
1453 * Usually called from within the exit handler.
1456 rpc_restart_call_prepare(struct rpc_task *task)
1458 if (RPC_ASSASSINATED(task))
1459 return 0;
1460 task->tk_action = call_start;
1461 task->tk_status = 0;
1462 if (task->tk_ops->rpc_call_prepare != NULL)
1463 task->tk_action = rpc_prepare_task;
1464 return 1;
1466 EXPORT_SYMBOL_GPL(rpc_restart_call_prepare);
1469 * Restart an (async) RPC call. Usually called from within the
1470 * exit handler.
1473 rpc_restart_call(struct rpc_task *task)
1475 if (RPC_ASSASSINATED(task))
1476 return 0;
1477 task->tk_action = call_start;
1478 task->tk_status = 0;
1479 return 1;
1481 EXPORT_SYMBOL_GPL(rpc_restart_call);
1483 const char
1484 *rpc_proc_name(const struct rpc_task *task)
1486 const struct rpc_procinfo *proc = task->tk_msg.rpc_proc;
1488 if (proc) {
1489 if (proc->p_name)
1490 return proc->p_name;
1491 else
1492 return "NULL";
1493 } else
1494 return "no proc";
1498 * 0. Initial state
1500 * Other FSM states can be visited zero or more times, but
1501 * this state is visited exactly once for each RPC.
1503 static void
1504 call_start(struct rpc_task *task)
1506 struct rpc_clnt *clnt = task->tk_client;
1507 int idx = task->tk_msg.rpc_proc->p_statidx;
1509 trace_rpc_request(task);
1510 dprintk("RPC: %5u call_start %s%d proc %s (%s)\n", task->tk_pid,
1511 clnt->cl_program->name, clnt->cl_vers,
1512 rpc_proc_name(task),
1513 (RPC_IS_ASYNC(task) ? "async" : "sync"));
1515 /* Increment call count (version might not be valid for ping) */
1516 if (clnt->cl_program->version[clnt->cl_vers])
1517 clnt->cl_program->version[clnt->cl_vers]->counts[idx]++;
1518 clnt->cl_stats->rpccnt++;
1519 task->tk_action = call_reserve;
1520 rpc_task_set_transport(task, clnt);
1524 * 1. Reserve an RPC call slot
1526 static void
1527 call_reserve(struct rpc_task *task)
1529 dprint_status(task);
1531 task->tk_status = 0;
1532 task->tk_action = call_reserveresult;
1533 xprt_reserve(task);
1536 static void call_retry_reserve(struct rpc_task *task);
1539 * 1b. Grok the result of xprt_reserve()
1541 static void
1542 call_reserveresult(struct rpc_task *task)
1544 int status = task->tk_status;
1546 dprint_status(task);
1549 * After a call to xprt_reserve(), we must have either
1550 * a request slot or else an error status.
1552 task->tk_status = 0;
1553 if (status >= 0) {
1554 if (task->tk_rqstp) {
1555 task->tk_action = call_refresh;
1556 return;
1559 printk(KERN_ERR "%s: status=%d, but no request slot, exiting\n",
1560 __func__, status);
1561 rpc_exit(task, -EIO);
1562 return;
1566 * Even though there was an error, we may have acquired
1567 * a request slot somehow. Make sure not to leak it.
1569 if (task->tk_rqstp) {
1570 printk(KERN_ERR "%s: status=%d, request allocated anyway\n",
1571 __func__, status);
1572 xprt_release(task);
1575 switch (status) {
1576 case -ENOMEM:
1577 rpc_delay(task, HZ >> 2);
1578 /* fall through */
1579 case -EAGAIN: /* woken up; retry */
1580 task->tk_action = call_retry_reserve;
1581 return;
1582 case -EIO: /* probably a shutdown */
1583 break;
1584 default:
1585 printk(KERN_ERR "%s: unrecognized error %d, exiting\n",
1586 __func__, status);
1587 break;
1589 rpc_exit(task, status);
1593 * 1c. Retry reserving an RPC call slot
1595 static void
1596 call_retry_reserve(struct rpc_task *task)
1598 dprint_status(task);
1600 task->tk_status = 0;
1601 task->tk_action = call_reserveresult;
1602 xprt_retry_reserve(task);
1606 * 2. Bind and/or refresh the credentials
1608 static void
1609 call_refresh(struct rpc_task *task)
1611 dprint_status(task);
1613 task->tk_action = call_refreshresult;
1614 task->tk_status = 0;
1615 task->tk_client->cl_stats->rpcauthrefresh++;
1616 rpcauth_refreshcred(task);
1620 * 2a. Process the results of a credential refresh
1622 static void
1623 call_refreshresult(struct rpc_task *task)
1625 int status = task->tk_status;
1627 dprint_status(task);
1629 task->tk_status = 0;
1630 task->tk_action = call_refresh;
1631 switch (status) {
1632 case 0:
1633 if (rpcauth_uptodatecred(task)) {
1634 task->tk_action = call_allocate;
1635 return;
1637 /* Use rate-limiting and a max number of retries if refresh
1638 * had status 0 but failed to update the cred.
1640 /* fall through */
1641 case -ETIMEDOUT:
1642 rpc_delay(task, 3*HZ);
1643 /* fall through */
1644 case -EAGAIN:
1645 status = -EACCES;
1646 /* fall through */
1647 case -EKEYEXPIRED:
1648 if (!task->tk_cred_retry)
1649 break;
1650 task->tk_cred_retry--;
1651 dprintk("RPC: %5u %s: retry refresh creds\n",
1652 task->tk_pid, __func__);
1653 return;
1655 dprintk("RPC: %5u %s: refresh creds failed with error %d\n",
1656 task->tk_pid, __func__, status);
1657 rpc_exit(task, status);
1661 * 2b. Allocate the buffer. For details, see sched.c:rpc_malloc.
1662 * (Note: buffer memory is freed in xprt_release).
1664 static void
1665 call_allocate(struct rpc_task *task)
1667 unsigned int slack = task->tk_rqstp->rq_cred->cr_auth->au_cslack;
1668 struct rpc_rqst *req = task->tk_rqstp;
1669 struct rpc_xprt *xprt = req->rq_xprt;
1670 const struct rpc_procinfo *proc = task->tk_msg.rpc_proc;
1671 int status;
1673 dprint_status(task);
1675 task->tk_status = 0;
1676 task->tk_action = call_encode;
1678 if (req->rq_buffer)
1679 return;
1681 if (proc->p_proc != 0) {
1682 BUG_ON(proc->p_arglen == 0);
1683 if (proc->p_decode != NULL)
1684 BUG_ON(proc->p_replen == 0);
1688 * Calculate the size (in quads) of the RPC call
1689 * and reply headers, and convert both values
1690 * to byte sizes.
1692 req->rq_callsize = RPC_CALLHDRSIZE + (slack << 1) + proc->p_arglen;
1693 req->rq_callsize <<= 2;
1694 req->rq_rcvsize = RPC_REPHDRSIZE + slack + proc->p_replen;
1695 req->rq_rcvsize <<= 2;
1697 status = xprt->ops->buf_alloc(task);
1698 xprt_inject_disconnect(xprt);
1699 if (status == 0)
1700 return;
1701 if (status != -ENOMEM) {
1702 rpc_exit(task, status);
1703 return;
1706 dprintk("RPC: %5u rpc_buffer allocation failed\n", task->tk_pid);
1708 if (RPC_IS_ASYNC(task) || !fatal_signal_pending(current)) {
1709 task->tk_action = call_allocate;
1710 rpc_delay(task, HZ>>4);
1711 return;
1714 rpc_exit(task, -ERESTARTSYS);
1717 static int
1718 rpc_task_need_encode(struct rpc_task *task)
1720 return test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate) == 0 &&
1721 (!(task->tk_flags & RPC_TASK_SENT) ||
1722 !(task->tk_flags & RPC_TASK_NO_RETRANS_TIMEOUT) ||
1723 xprt_request_need_retransmit(task));
1726 static void
1727 rpc_xdr_encode(struct rpc_task *task)
1729 struct rpc_rqst *req = task->tk_rqstp;
1730 kxdreproc_t encode;
1731 __be32 *p;
1733 dprint_status(task);
1735 xdr_buf_init(&req->rq_snd_buf,
1736 req->rq_buffer,
1737 req->rq_callsize);
1738 xdr_buf_init(&req->rq_rcv_buf,
1739 req->rq_rbuffer,
1740 req->rq_rcvsize);
1741 req->rq_bytes_sent = 0;
1743 p = rpc_encode_header(task);
1744 if (p == NULL) {
1745 printk(KERN_INFO "RPC: couldn't encode RPC header, exit EIO\n");
1746 rpc_exit(task, -EIO);
1747 return;
1750 encode = task->tk_msg.rpc_proc->p_encode;
1751 if (encode == NULL)
1752 return;
1754 task->tk_status = rpcauth_wrap_req(task, encode, req, p,
1755 task->tk_msg.rpc_argp);
1756 if (task->tk_status == 0)
1757 xprt_request_prepare(req);
1761 * 3. Encode arguments of an RPC call
1763 static void
1764 call_encode(struct rpc_task *task)
1766 if (!rpc_task_need_encode(task))
1767 goto out;
1768 /* Encode here so that rpcsec_gss can use correct sequence number. */
1769 rpc_xdr_encode(task);
1770 /* Did the encode result in an error condition? */
1771 if (task->tk_status != 0) {
1772 /* Was the error nonfatal? */
1773 if (task->tk_status == -EAGAIN || task->tk_status == -ENOMEM)
1774 rpc_delay(task, HZ >> 4);
1775 else
1776 rpc_exit(task, task->tk_status);
1777 return;
1780 /* Add task to reply queue before transmission to avoid races */
1781 if (rpc_reply_expected(task))
1782 xprt_request_enqueue_receive(task);
1783 xprt_request_enqueue_transmit(task);
1784 out:
1785 task->tk_action = call_bind;
1789 * 4. Get the server port number if not yet set
1791 static void
1792 call_bind(struct rpc_task *task)
1794 struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
1796 dprint_status(task);
1798 task->tk_action = call_connect;
1799 if (!xprt_bound(xprt)) {
1800 task->tk_action = call_bind_status;
1801 task->tk_timeout = xprt->bind_timeout;
1802 xprt->ops->rpcbind(task);
1807 * 4a. Sort out bind result
1809 static void
1810 call_bind_status(struct rpc_task *task)
1812 int status = -EIO;
1814 if (task->tk_status >= 0) {
1815 dprint_status(task);
1816 task->tk_status = 0;
1817 task->tk_action = call_connect;
1818 return;
1821 trace_rpc_bind_status(task);
1822 switch (task->tk_status) {
1823 case -ENOMEM:
1824 dprintk("RPC: %5u rpcbind out of memory\n", task->tk_pid);
1825 rpc_delay(task, HZ >> 2);
1826 goto retry_timeout;
1827 case -EACCES:
1828 dprintk("RPC: %5u remote rpcbind: RPC program/version "
1829 "unavailable\n", task->tk_pid);
1830 /* fail immediately if this is an RPC ping */
1831 if (task->tk_msg.rpc_proc->p_proc == 0) {
1832 status = -EOPNOTSUPP;
1833 break;
1835 if (task->tk_rebind_retry == 0)
1836 break;
1837 task->tk_rebind_retry--;
1838 rpc_delay(task, 3*HZ);
1839 goto retry_timeout;
1840 case -ETIMEDOUT:
1841 dprintk("RPC: %5u rpcbind request timed out\n",
1842 task->tk_pid);
1843 goto retry_timeout;
1844 case -EPFNOSUPPORT:
1845 /* server doesn't support any rpcbind version we know of */
1846 dprintk("RPC: %5u unrecognized remote rpcbind service\n",
1847 task->tk_pid);
1848 break;
1849 case -EPROTONOSUPPORT:
1850 dprintk("RPC: %5u remote rpcbind version unavailable, retrying\n",
1851 task->tk_pid);
1852 goto retry_timeout;
1853 case -ECONNREFUSED: /* connection problems */
1854 case -ECONNRESET:
1855 case -ECONNABORTED:
1856 case -ENOTCONN:
1857 case -EHOSTDOWN:
1858 case -ENETDOWN:
1859 case -EHOSTUNREACH:
1860 case -ENETUNREACH:
1861 case -ENOBUFS:
1862 case -EPIPE:
1863 dprintk("RPC: %5u remote rpcbind unreachable: %d\n",
1864 task->tk_pid, task->tk_status);
1865 if (!RPC_IS_SOFTCONN(task)) {
1866 rpc_delay(task, 5*HZ);
1867 goto retry_timeout;
1869 status = task->tk_status;
1870 break;
1871 default:
1872 dprintk("RPC: %5u unrecognized rpcbind error (%d)\n",
1873 task->tk_pid, -task->tk_status);
1876 rpc_exit(task, status);
1877 return;
1879 retry_timeout:
1880 task->tk_status = 0;
1881 task->tk_action = call_timeout;
1885 * 4b. Connect to the RPC server
1887 static void
1888 call_connect(struct rpc_task *task)
1890 struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
1892 dprintk("RPC: %5u call_connect xprt %p %s connected\n",
1893 task->tk_pid, xprt,
1894 (xprt_connected(xprt) ? "is" : "is not"));
1896 task->tk_action = call_transmit;
1897 if (!xprt_connected(xprt)) {
1898 task->tk_action = call_connect_status;
1899 if (task->tk_status < 0)
1900 return;
1901 if (task->tk_flags & RPC_TASK_NOCONNECT) {
1902 rpc_exit(task, -ENOTCONN);
1903 return;
1905 xprt_connect(task);
1910 * 4c. Sort out connect result
1912 static void
1913 call_connect_status(struct rpc_task *task)
1915 struct rpc_clnt *clnt = task->tk_client;
1916 int status = task->tk_status;
1918 dprint_status(task);
1920 trace_rpc_connect_status(task);
1921 task->tk_status = 0;
1922 switch (status) {
1923 case -ECONNREFUSED:
1924 /* A positive refusal suggests a rebind is needed. */
1925 if (RPC_IS_SOFTCONN(task))
1926 break;
1927 if (clnt->cl_autobind) {
1928 rpc_force_rebind(clnt);
1929 task->tk_action = call_bind;
1930 return;
1932 /* fall through */
1933 case -ECONNRESET:
1934 case -ECONNABORTED:
1935 case -ENETDOWN:
1936 case -ENETUNREACH:
1937 case -EHOSTUNREACH:
1938 case -EADDRINUSE:
1939 case -ENOBUFS:
1940 case -EPIPE:
1941 xprt_conditional_disconnect(task->tk_rqstp->rq_xprt,
1942 task->tk_rqstp->rq_connect_cookie);
1943 if (RPC_IS_SOFTCONN(task))
1944 break;
1945 /* retry with existing socket, after a delay */
1946 rpc_delay(task, 3*HZ);
1947 /* fall through */
1948 case -EAGAIN:
1949 /* Check for timeouts before looping back to call_bind */
1950 case -ETIMEDOUT:
1951 task->tk_action = call_timeout;
1952 return;
1953 case 0:
1954 clnt->cl_stats->netreconn++;
1955 task->tk_action = call_transmit;
1956 return;
1958 rpc_exit(task, status);
1962 * 5. Transmit the RPC request, and wait for reply
1964 static void
1965 call_transmit(struct rpc_task *task)
1967 dprint_status(task);
1969 task->tk_status = 0;
1970 if (test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate)) {
1971 if (!xprt_prepare_transmit(task))
1972 return;
1973 xprt_transmit(task);
1975 task->tk_action = call_transmit_status;
1976 xprt_end_transmit(task);
1980 * 5a. Handle cleanup after a transmission
1982 static void
1983 call_transmit_status(struct rpc_task *task)
1985 task->tk_action = call_status;
1988 * Common case: success. Force the compiler to put this
1989 * test first.
1991 if (task->tk_status == 0) {
1992 xprt_request_wait_receive(task);
1993 return;
1996 switch (task->tk_status) {
1997 default:
1998 dprint_status(task);
1999 break;
2000 case -EBADMSG:
2001 task->tk_status = 0;
2002 task->tk_action = call_encode;
2003 break;
2005 * Special cases: if we've been waiting on the
2006 * socket's write_space() callback, or if the
2007 * socket just returned a connection error,
2008 * then hold onto the transport lock.
2010 case -ENOBUFS:
2011 rpc_delay(task, HZ>>2);
2012 /* fall through */
2013 case -EBADSLT:
2014 case -EAGAIN:
2015 task->tk_action = call_transmit;
2016 task->tk_status = 0;
2017 break;
2018 case -ECONNREFUSED:
2019 case -EHOSTDOWN:
2020 case -ENETDOWN:
2021 case -EHOSTUNREACH:
2022 case -ENETUNREACH:
2023 case -EPERM:
2024 if (RPC_IS_SOFTCONN(task)) {
2025 if (!task->tk_msg.rpc_proc->p_proc)
2026 trace_xprt_ping(task->tk_xprt,
2027 task->tk_status);
2028 rpc_exit(task, task->tk_status);
2029 break;
2031 /* fall through */
2032 case -ECONNRESET:
2033 case -ECONNABORTED:
2034 case -EADDRINUSE:
2035 case -ENOTCONN:
2036 case -EPIPE:
2037 break;
2041 #if defined(CONFIG_SUNRPC_BACKCHANNEL)
2043 * 5b. Send the backchannel RPC reply. On error, drop the reply. In
2044 * addition, disconnect on connectivity errors.
2046 static void
2047 call_bc_transmit(struct rpc_task *task)
2049 struct rpc_rqst *req = task->tk_rqstp;
2051 if (rpc_task_need_encode(task))
2052 xprt_request_enqueue_transmit(task);
2053 if (!test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate))
2054 goto out_wakeup;
2056 if (!xprt_prepare_transmit(task))
2057 goto out_retry;
2059 if (task->tk_status < 0) {
2060 printk(KERN_NOTICE "RPC: Could not send backchannel reply "
2061 "error: %d\n", task->tk_status);
2062 goto out_done;
2065 xprt_transmit(task);
2067 xprt_end_transmit(task);
2068 dprint_status(task);
2069 switch (task->tk_status) {
2070 case 0:
2071 /* Success */
2072 case -ENETDOWN:
2073 case -EHOSTDOWN:
2074 case -EHOSTUNREACH:
2075 case -ENETUNREACH:
2076 case -ECONNRESET:
2077 case -ECONNREFUSED:
2078 case -EADDRINUSE:
2079 case -ENOTCONN:
2080 case -EPIPE:
2081 break;
2082 case -EAGAIN:
2083 goto out_retry;
2084 case -ETIMEDOUT:
2086 * Problem reaching the server. Disconnect and let the
2087 * forechannel reestablish the connection. The server will
2088 * have to retransmit the backchannel request and we'll
2089 * reprocess it. Since these ops are idempotent, there's no
2090 * need to cache our reply at this time.
2092 printk(KERN_NOTICE "RPC: Could not send backchannel reply "
2093 "error: %d\n", task->tk_status);
2094 xprt_conditional_disconnect(req->rq_xprt,
2095 req->rq_connect_cookie);
2096 break;
2097 default:
2099 * We were unable to reply and will have to drop the
2100 * request. The server should reconnect and retransmit.
2102 WARN_ON_ONCE(task->tk_status == -EAGAIN);
2103 printk(KERN_NOTICE "RPC: Could not send backchannel reply "
2104 "error: %d\n", task->tk_status);
2105 break;
2107 out_wakeup:
2108 rpc_wake_up_queued_task(&req->rq_xprt->pending, task);
2109 out_done:
2110 task->tk_action = rpc_exit_task;
2111 return;
2112 out_retry:
2113 task->tk_status = 0;
2115 #endif /* CONFIG_SUNRPC_BACKCHANNEL */
2118 * 6. Sort out the RPC call status
2120 static void
2121 call_status(struct rpc_task *task)
2123 struct rpc_clnt *clnt = task->tk_client;
2124 int status;
2126 if (!task->tk_msg.rpc_proc->p_proc)
2127 trace_xprt_ping(task->tk_xprt, task->tk_status);
2129 dprint_status(task);
2131 status = task->tk_status;
2132 if (status >= 0) {
2133 task->tk_action = call_decode;
2134 return;
2137 trace_rpc_call_status(task);
2138 task->tk_status = 0;
2139 switch(status) {
2140 case -EHOSTDOWN:
2141 case -ENETDOWN:
2142 case -EHOSTUNREACH:
2143 case -ENETUNREACH:
2144 case -EPERM:
2145 if (RPC_IS_SOFTCONN(task)) {
2146 rpc_exit(task, status);
2147 break;
2150 * Delay any retries for 3 seconds, then handle as if it
2151 * were a timeout.
2153 rpc_delay(task, 3*HZ);
2154 /* fall through */
2155 case -ETIMEDOUT:
2156 task->tk_action = call_timeout;
2157 break;
2158 case -ECONNREFUSED:
2159 case -ECONNRESET:
2160 case -ECONNABORTED:
2161 rpc_force_rebind(clnt);
2162 /* fall through */
2163 case -EADDRINUSE:
2164 rpc_delay(task, 3*HZ);
2165 /* fall through */
2166 case -EPIPE:
2167 case -ENOTCONN:
2168 case -EAGAIN:
2169 task->tk_action = call_encode;
2170 break;
2171 case -EIO:
2172 /* shutdown or soft timeout */
2173 rpc_exit(task, status);
2174 break;
2175 default:
2176 if (clnt->cl_chatty)
2177 printk("%s: RPC call returned error %d\n",
2178 clnt->cl_program->name, -status);
2179 rpc_exit(task, status);
2184 * 6a. Handle RPC timeout
2185 * We do not release the request slot, so we keep using the
2186 * same XID for all retransmits.
2188 static void
2189 call_timeout(struct rpc_task *task)
2191 struct rpc_clnt *clnt = task->tk_client;
2193 if (xprt_adjust_timeout(task->tk_rqstp) == 0) {
2194 dprintk("RPC: %5u call_timeout (minor)\n", task->tk_pid);
2195 goto retry;
2198 dprintk("RPC: %5u call_timeout (major)\n", task->tk_pid);
2199 task->tk_timeouts++;
2201 if (RPC_IS_SOFTCONN(task)) {
2202 rpc_exit(task, -ETIMEDOUT);
2203 return;
2205 if (RPC_IS_SOFT(task)) {
2206 if (clnt->cl_chatty) {
2207 printk(KERN_NOTICE "%s: server %s not responding, timed out\n",
2208 clnt->cl_program->name,
2209 task->tk_xprt->servername);
2211 if (task->tk_flags & RPC_TASK_TIMEOUT)
2212 rpc_exit(task, -ETIMEDOUT);
2213 else
2214 rpc_exit(task, -EIO);
2215 return;
2218 if (!(task->tk_flags & RPC_CALL_MAJORSEEN)) {
2219 task->tk_flags |= RPC_CALL_MAJORSEEN;
2220 if (clnt->cl_chatty) {
2221 printk(KERN_NOTICE "%s: server %s not responding, still trying\n",
2222 clnt->cl_program->name,
2223 task->tk_xprt->servername);
2226 rpc_force_rebind(clnt);
2228 * Did our request time out due to an RPCSEC_GSS out-of-sequence
2229 * event? RFC2203 requires the server to drop all such requests.
2231 rpcauth_invalcred(task);
2233 retry:
2234 task->tk_action = call_encode;
2235 task->tk_status = 0;
2239 * 7. Decode the RPC reply
2241 static void
2242 call_decode(struct rpc_task *task)
2244 struct rpc_clnt *clnt = task->tk_client;
2245 struct rpc_rqst *req = task->tk_rqstp;
2246 kxdrdproc_t decode = task->tk_msg.rpc_proc->p_decode;
2247 __be32 *p;
2249 dprint_status(task);
2251 if (!decode) {
2252 task->tk_action = rpc_exit_task;
2253 return;
2256 if (task->tk_flags & RPC_CALL_MAJORSEEN) {
2257 if (clnt->cl_chatty) {
2258 printk(KERN_NOTICE "%s: server %s OK\n",
2259 clnt->cl_program->name,
2260 task->tk_xprt->servername);
2262 task->tk_flags &= ~RPC_CALL_MAJORSEEN;
2266 * Ensure that we see all writes made by xprt_complete_rqst()
2267 * before it changed req->rq_reply_bytes_recvd.
2269 smp_rmb();
2270 req->rq_rcv_buf.len = req->rq_private_buf.len;
2272 /* Check that the softirq receive buffer is valid */
2273 WARN_ON(memcmp(&req->rq_rcv_buf, &req->rq_private_buf,
2274 sizeof(req->rq_rcv_buf)) != 0);
2276 if (req->rq_rcv_buf.len < 12) {
2277 if (!RPC_IS_SOFT(task)) {
2278 task->tk_action = call_encode;
2279 goto out_retry;
2281 dprintk("RPC: %s: too small RPC reply size (%d bytes)\n",
2282 clnt->cl_program->name, task->tk_status);
2283 task->tk_action = call_timeout;
2284 goto out_retry;
2287 p = rpc_verify_header(task);
2288 if (IS_ERR(p)) {
2289 if (p == ERR_PTR(-EAGAIN))
2290 goto out_retry;
2291 return;
2293 task->tk_action = rpc_exit_task;
2295 task->tk_status = rpcauth_unwrap_resp(task, decode, req, p,
2296 task->tk_msg.rpc_resp);
2298 dprintk("RPC: %5u call_decode result %d\n", task->tk_pid,
2299 task->tk_status);
2300 return;
2301 out_retry:
2302 task->tk_status = 0;
2303 /* Note: rpc_verify_header() may have freed the RPC slot */
2304 if (task->tk_rqstp == req) {
2305 req->rq_reply_bytes_recvd = req->rq_rcv_buf.len = 0;
2306 if (task->tk_client->cl_discrtry)
2307 xprt_conditional_disconnect(req->rq_xprt,
2308 req->rq_connect_cookie);
2312 static __be32 *
2313 rpc_encode_header(struct rpc_task *task)
2315 struct rpc_clnt *clnt = task->tk_client;
2316 struct rpc_rqst *req = task->tk_rqstp;
2317 __be32 *p = req->rq_svec[0].iov_base;
2319 /* FIXME: check buffer size? */
2321 p = xprt_skip_transport_header(req->rq_xprt, p);
2322 *p++ = req->rq_xid; /* XID */
2323 *p++ = htonl(RPC_CALL); /* CALL */
2324 *p++ = htonl(RPC_VERSION); /* RPC version */
2325 *p++ = htonl(clnt->cl_prog); /* program number */
2326 *p++ = htonl(clnt->cl_vers); /* program version */
2327 *p++ = htonl(task->tk_msg.rpc_proc->p_proc); /* procedure */
2328 p = rpcauth_marshcred(task, p);
2329 req->rq_slen = xdr_adjust_iovec(&req->rq_svec[0], p);
2330 return p;
2333 static __be32 *
2334 rpc_verify_header(struct rpc_task *task)
2336 struct rpc_clnt *clnt = task->tk_client;
2337 struct kvec *iov = &task->tk_rqstp->rq_rcv_buf.head[0];
2338 int len = task->tk_rqstp->rq_rcv_buf.len >> 2;
2339 __be32 *p = iov->iov_base;
2340 u32 n;
2341 int error = -EACCES;
2343 if ((task->tk_rqstp->rq_rcv_buf.len & 3) != 0) {
2344 /* RFC-1014 says that the representation of XDR data must be a
2345 * multiple of four bytes
2346 * - if it isn't pointer subtraction in the NFS client may give
2347 * undefined results
2349 dprintk("RPC: %5u %s: XDR representation not a multiple of"
2350 " 4 bytes: 0x%x\n", task->tk_pid, __func__,
2351 task->tk_rqstp->rq_rcv_buf.len);
2352 error = -EIO;
2353 goto out_err;
2355 if ((len -= 3) < 0)
2356 goto out_overflow;
2358 p += 1; /* skip XID */
2359 if ((n = ntohl(*p++)) != RPC_REPLY) {
2360 dprintk("RPC: %5u %s: not an RPC reply: %x\n",
2361 task->tk_pid, __func__, n);
2362 error = -EIO;
2363 goto out_garbage;
2366 if ((n = ntohl(*p++)) != RPC_MSG_ACCEPTED) {
2367 if (--len < 0)
2368 goto out_overflow;
2369 switch ((n = ntohl(*p++))) {
2370 case RPC_AUTH_ERROR:
2371 break;
2372 case RPC_MISMATCH:
2373 dprintk("RPC: %5u %s: RPC call version mismatch!\n",
2374 task->tk_pid, __func__);
2375 error = -EPROTONOSUPPORT;
2376 goto out_err;
2377 default:
2378 dprintk("RPC: %5u %s: RPC call rejected, "
2379 "unknown error: %x\n",
2380 task->tk_pid, __func__, n);
2381 error = -EIO;
2382 goto out_err;
2384 if (--len < 0)
2385 goto out_overflow;
2386 switch ((n = ntohl(*p++))) {
2387 case RPC_AUTH_REJECTEDCRED:
2388 case RPC_AUTH_REJECTEDVERF:
2389 case RPCSEC_GSS_CREDPROBLEM:
2390 case RPCSEC_GSS_CTXPROBLEM:
2391 if (!task->tk_cred_retry)
2392 break;
2393 task->tk_cred_retry--;
2394 dprintk("RPC: %5u %s: retry stale creds\n",
2395 task->tk_pid, __func__);
2396 rpcauth_invalcred(task);
2397 /* Ensure we obtain a new XID! */
2398 xprt_release(task);
2399 task->tk_action = call_reserve;
2400 goto out_retry;
2401 case RPC_AUTH_BADCRED:
2402 case RPC_AUTH_BADVERF:
2403 /* possibly garbled cred/verf? */
2404 if (!task->tk_garb_retry)
2405 break;
2406 task->tk_garb_retry--;
2407 dprintk("RPC: %5u %s: retry garbled creds\n",
2408 task->tk_pid, __func__);
2409 task->tk_action = call_encode;
2410 goto out_retry;
2411 case RPC_AUTH_TOOWEAK:
2412 printk(KERN_NOTICE "RPC: server %s requires stronger "
2413 "authentication.\n",
2414 task->tk_xprt->servername);
2415 break;
2416 default:
2417 dprintk("RPC: %5u %s: unknown auth error: %x\n",
2418 task->tk_pid, __func__, n);
2419 error = -EIO;
2421 dprintk("RPC: %5u %s: call rejected %d\n",
2422 task->tk_pid, __func__, n);
2423 goto out_err;
2425 p = rpcauth_checkverf(task, p);
2426 if (IS_ERR(p)) {
2427 error = PTR_ERR(p);
2428 dprintk("RPC: %5u %s: auth check failed with %d\n",
2429 task->tk_pid, __func__, error);
2430 goto out_garbage; /* bad verifier, retry */
2432 len = p - (__be32 *)iov->iov_base - 1;
2433 if (len < 0)
2434 goto out_overflow;
2435 switch ((n = ntohl(*p++))) {
2436 case RPC_SUCCESS:
2437 return p;
2438 case RPC_PROG_UNAVAIL:
2439 dprintk("RPC: %5u %s: program %u is unsupported "
2440 "by server %s\n", task->tk_pid, __func__,
2441 (unsigned int)clnt->cl_prog,
2442 task->tk_xprt->servername);
2443 error = -EPFNOSUPPORT;
2444 goto out_err;
2445 case RPC_PROG_MISMATCH:
2446 dprintk("RPC: %5u %s: program %u, version %u unsupported "
2447 "by server %s\n", task->tk_pid, __func__,
2448 (unsigned int)clnt->cl_prog,
2449 (unsigned int)clnt->cl_vers,
2450 task->tk_xprt->servername);
2451 error = -EPROTONOSUPPORT;
2452 goto out_err;
2453 case RPC_PROC_UNAVAIL:
2454 dprintk("RPC: %5u %s: proc %s unsupported by program %u, "
2455 "version %u on server %s\n",
2456 task->tk_pid, __func__,
2457 rpc_proc_name(task),
2458 clnt->cl_prog, clnt->cl_vers,
2459 task->tk_xprt->servername);
2460 error = -EOPNOTSUPP;
2461 goto out_err;
2462 case RPC_GARBAGE_ARGS:
2463 dprintk("RPC: %5u %s: server saw garbage\n",
2464 task->tk_pid, __func__);
2465 break; /* retry */
2466 default:
2467 dprintk("RPC: %5u %s: server accept status: %x\n",
2468 task->tk_pid, __func__, n);
2469 /* Also retry */
2472 out_garbage:
2473 clnt->cl_stats->rpcgarbage++;
2474 if (task->tk_garb_retry) {
2475 task->tk_garb_retry--;
2476 dprintk("RPC: %5u %s: retrying\n",
2477 task->tk_pid, __func__);
2478 task->tk_action = call_encode;
2479 out_retry:
2480 return ERR_PTR(-EAGAIN);
2482 out_err:
2483 rpc_exit(task, error);
2484 dprintk("RPC: %5u %s: call failed with error %d\n", task->tk_pid,
2485 __func__, error);
2486 return ERR_PTR(error);
2487 out_overflow:
2488 dprintk("RPC: %5u %s: server reply was truncated.\n", task->tk_pid,
2489 __func__);
2490 goto out_garbage;
2493 static void rpcproc_encode_null(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
2494 const void *obj)
2498 static int rpcproc_decode_null(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
2499 void *obj)
2501 return 0;
2504 static const struct rpc_procinfo rpcproc_null = {
2505 .p_encode = rpcproc_encode_null,
2506 .p_decode = rpcproc_decode_null,
2509 static int rpc_ping(struct rpc_clnt *clnt)
2511 struct rpc_message msg = {
2512 .rpc_proc = &rpcproc_null,
2514 int err;
2515 msg.rpc_cred = authnull_ops.lookup_cred(NULL, NULL, 0);
2516 err = rpc_call_sync(clnt, &msg, RPC_TASK_SOFT | RPC_TASK_SOFTCONN);
2517 put_rpccred(msg.rpc_cred);
2518 return err;
2521 static
2522 struct rpc_task *rpc_call_null_helper(struct rpc_clnt *clnt,
2523 struct rpc_xprt *xprt, struct rpc_cred *cred, int flags,
2524 const struct rpc_call_ops *ops, void *data)
2526 struct rpc_message msg = {
2527 .rpc_proc = &rpcproc_null,
2528 .rpc_cred = cred,
2530 struct rpc_task_setup task_setup_data = {
2531 .rpc_client = clnt,
2532 .rpc_xprt = xprt,
2533 .rpc_message = &msg,
2534 .callback_ops = (ops != NULL) ? ops : &rpc_default_ops,
2535 .callback_data = data,
2536 .flags = flags,
2539 return rpc_run_task(&task_setup_data);
2542 struct rpc_task *rpc_call_null(struct rpc_clnt *clnt, struct rpc_cred *cred, int flags)
2544 return rpc_call_null_helper(clnt, NULL, cred, flags, NULL, NULL);
2546 EXPORT_SYMBOL_GPL(rpc_call_null);
2548 struct rpc_cb_add_xprt_calldata {
2549 struct rpc_xprt_switch *xps;
2550 struct rpc_xprt *xprt;
2553 static void rpc_cb_add_xprt_done(struct rpc_task *task, void *calldata)
2555 struct rpc_cb_add_xprt_calldata *data = calldata;
2557 if (task->tk_status == 0)
2558 rpc_xprt_switch_add_xprt(data->xps, data->xprt);
2561 static void rpc_cb_add_xprt_release(void *calldata)
2563 struct rpc_cb_add_xprt_calldata *data = calldata;
2565 xprt_put(data->xprt);
2566 xprt_switch_put(data->xps);
2567 kfree(data);
2570 static const struct rpc_call_ops rpc_cb_add_xprt_call_ops = {
2571 .rpc_call_done = rpc_cb_add_xprt_done,
2572 .rpc_release = rpc_cb_add_xprt_release,
2576 * rpc_clnt_test_and_add_xprt - Test and add a new transport to a rpc_clnt
2577 * @clnt: pointer to struct rpc_clnt
2578 * @xps: pointer to struct rpc_xprt_switch,
2579 * @xprt: pointer struct rpc_xprt
2580 * @dummy: unused
2582 int rpc_clnt_test_and_add_xprt(struct rpc_clnt *clnt,
2583 struct rpc_xprt_switch *xps, struct rpc_xprt *xprt,
2584 void *dummy)
2586 struct rpc_cb_add_xprt_calldata *data;
2587 struct rpc_cred *cred;
2588 struct rpc_task *task;
2590 data = kmalloc(sizeof(*data), GFP_NOFS);
2591 if (!data)
2592 return -ENOMEM;
2593 data->xps = xprt_switch_get(xps);
2594 data->xprt = xprt_get(xprt);
2596 cred = authnull_ops.lookup_cred(NULL, NULL, 0);
2597 task = rpc_call_null_helper(clnt, xprt, cred,
2598 RPC_TASK_SOFT|RPC_TASK_SOFTCONN|RPC_TASK_ASYNC,
2599 &rpc_cb_add_xprt_call_ops, data);
2600 put_rpccred(cred);
2601 if (IS_ERR(task))
2602 return PTR_ERR(task);
2603 rpc_put_task(task);
2604 return 1;
2606 EXPORT_SYMBOL_GPL(rpc_clnt_test_and_add_xprt);
2609 * rpc_clnt_setup_test_and_add_xprt()
2611 * This is an rpc_clnt_add_xprt setup() function which returns 1 so:
2612 * 1) caller of the test function must dereference the rpc_xprt_switch
2613 * and the rpc_xprt.
2614 * 2) test function must call rpc_xprt_switch_add_xprt, usually in
2615 * the rpc_call_done routine.
2617 * Upon success (return of 1), the test function adds the new
2618 * transport to the rpc_clnt xprt switch
2620 * @clnt: struct rpc_clnt to get the new transport
2621 * @xps: the rpc_xprt_switch to hold the new transport
2622 * @xprt: the rpc_xprt to test
2623 * @data: a struct rpc_add_xprt_test pointer that holds the test function
2624 * and test function call data
2626 int rpc_clnt_setup_test_and_add_xprt(struct rpc_clnt *clnt,
2627 struct rpc_xprt_switch *xps,
2628 struct rpc_xprt *xprt,
2629 void *data)
2631 struct rpc_cred *cred;
2632 struct rpc_task *task;
2633 struct rpc_add_xprt_test *xtest = (struct rpc_add_xprt_test *)data;
2634 int status = -EADDRINUSE;
2636 xprt = xprt_get(xprt);
2637 xprt_switch_get(xps);
2639 if (rpc_xprt_switch_has_addr(xps, (struct sockaddr *)&xprt->addr))
2640 goto out_err;
2642 /* Test the connection */
2643 cred = authnull_ops.lookup_cred(NULL, NULL, 0);
2644 task = rpc_call_null_helper(clnt, xprt, cred,
2645 RPC_TASK_SOFT | RPC_TASK_SOFTCONN,
2646 NULL, NULL);
2647 put_rpccred(cred);
2648 if (IS_ERR(task)) {
2649 status = PTR_ERR(task);
2650 goto out_err;
2652 status = task->tk_status;
2653 rpc_put_task(task);
2655 if (status < 0)
2656 goto out_err;
2658 /* rpc_xprt_switch and rpc_xprt are deferrenced by add_xprt_test() */
2659 xtest->add_xprt_test(clnt, xprt, xtest->data);
2661 /* so that rpc_clnt_add_xprt does not call rpc_xprt_switch_add_xprt */
2662 return 1;
2663 out_err:
2664 xprt_put(xprt);
2665 xprt_switch_put(xps);
2666 pr_info("RPC: rpc_clnt_test_xprt failed: %d addr %s not added\n",
2667 status, xprt->address_strings[RPC_DISPLAY_ADDR]);
2668 return status;
2670 EXPORT_SYMBOL_GPL(rpc_clnt_setup_test_and_add_xprt);
2673 * rpc_clnt_add_xprt - Add a new transport to a rpc_clnt
2674 * @clnt: pointer to struct rpc_clnt
2675 * @xprtargs: pointer to struct xprt_create
2676 * @setup: callback to test and/or set up the connection
2677 * @data: pointer to setup function data
2679 * Creates a new transport using the parameters set in args and
2680 * adds it to clnt.
2681 * If ping is set, then test that connectivity succeeds before
2682 * adding the new transport.
2685 int rpc_clnt_add_xprt(struct rpc_clnt *clnt,
2686 struct xprt_create *xprtargs,
2687 int (*setup)(struct rpc_clnt *,
2688 struct rpc_xprt_switch *,
2689 struct rpc_xprt *,
2690 void *),
2691 void *data)
2693 struct rpc_xprt_switch *xps;
2694 struct rpc_xprt *xprt;
2695 unsigned long connect_timeout;
2696 unsigned long reconnect_timeout;
2697 unsigned char resvport;
2698 int ret = 0;
2700 rcu_read_lock();
2701 xps = xprt_switch_get(rcu_dereference(clnt->cl_xpi.xpi_xpswitch));
2702 xprt = xprt_iter_xprt(&clnt->cl_xpi);
2703 if (xps == NULL || xprt == NULL) {
2704 rcu_read_unlock();
2705 return -EAGAIN;
2707 resvport = xprt->resvport;
2708 connect_timeout = xprt->connect_timeout;
2709 reconnect_timeout = xprt->max_reconnect_timeout;
2710 rcu_read_unlock();
2712 xprt = xprt_create_transport(xprtargs);
2713 if (IS_ERR(xprt)) {
2714 ret = PTR_ERR(xprt);
2715 goto out_put_switch;
2717 xprt->resvport = resvport;
2718 if (xprt->ops->set_connect_timeout != NULL)
2719 xprt->ops->set_connect_timeout(xprt,
2720 connect_timeout,
2721 reconnect_timeout);
2723 rpc_xprt_switch_set_roundrobin(xps);
2724 if (setup) {
2725 ret = setup(clnt, xps, xprt, data);
2726 if (ret != 0)
2727 goto out_put_xprt;
2729 rpc_xprt_switch_add_xprt(xps, xprt);
2730 out_put_xprt:
2731 xprt_put(xprt);
2732 out_put_switch:
2733 xprt_switch_put(xps);
2734 return ret;
2736 EXPORT_SYMBOL_GPL(rpc_clnt_add_xprt);
2738 struct connect_timeout_data {
2739 unsigned long connect_timeout;
2740 unsigned long reconnect_timeout;
2743 static int
2744 rpc_xprt_set_connect_timeout(struct rpc_clnt *clnt,
2745 struct rpc_xprt *xprt,
2746 void *data)
2748 struct connect_timeout_data *timeo = data;
2750 if (xprt->ops->set_connect_timeout)
2751 xprt->ops->set_connect_timeout(xprt,
2752 timeo->connect_timeout,
2753 timeo->reconnect_timeout);
2754 return 0;
2757 void
2758 rpc_set_connect_timeout(struct rpc_clnt *clnt,
2759 unsigned long connect_timeout,
2760 unsigned long reconnect_timeout)
2762 struct connect_timeout_data timeout = {
2763 .connect_timeout = connect_timeout,
2764 .reconnect_timeout = reconnect_timeout,
2766 rpc_clnt_iterate_for_each_xprt(clnt,
2767 rpc_xprt_set_connect_timeout,
2768 &timeout);
2770 EXPORT_SYMBOL_GPL(rpc_set_connect_timeout);
2772 void rpc_clnt_xprt_switch_put(struct rpc_clnt *clnt)
2774 rcu_read_lock();
2775 xprt_switch_put(rcu_dereference(clnt->cl_xpi.xpi_xpswitch));
2776 rcu_read_unlock();
2778 EXPORT_SYMBOL_GPL(rpc_clnt_xprt_switch_put);
2780 void rpc_clnt_xprt_switch_add_xprt(struct rpc_clnt *clnt, struct rpc_xprt *xprt)
2782 rcu_read_lock();
2783 rpc_xprt_switch_add_xprt(rcu_dereference(clnt->cl_xpi.xpi_xpswitch),
2784 xprt);
2785 rcu_read_unlock();
2787 EXPORT_SYMBOL_GPL(rpc_clnt_xprt_switch_add_xprt);
2789 bool rpc_clnt_xprt_switch_has_addr(struct rpc_clnt *clnt,
2790 const struct sockaddr *sap)
2792 struct rpc_xprt_switch *xps;
2793 bool ret;
2795 rcu_read_lock();
2796 xps = rcu_dereference(clnt->cl_xpi.xpi_xpswitch);
2797 ret = rpc_xprt_switch_has_addr(xps, sap);
2798 rcu_read_unlock();
2799 return ret;
2801 EXPORT_SYMBOL_GPL(rpc_clnt_xprt_switch_has_addr);
2803 #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
2804 static void rpc_show_header(void)
2806 printk(KERN_INFO "-pid- flgs status -client- --rqstp- "
2807 "-timeout ---ops--\n");
2810 static void rpc_show_task(const struct rpc_clnt *clnt,
2811 const struct rpc_task *task)
2813 const char *rpc_waitq = "none";
2815 if (RPC_IS_QUEUED(task))
2816 rpc_waitq = rpc_qname(task->tk_waitqueue);
2818 printk(KERN_INFO "%5u %04x %6d %8p %8p %8ld %8p %sv%u %s a:%ps q:%s\n",
2819 task->tk_pid, task->tk_flags, task->tk_status,
2820 clnt, task->tk_rqstp, task->tk_timeout, task->tk_ops,
2821 clnt->cl_program->name, clnt->cl_vers, rpc_proc_name(task),
2822 task->tk_action, rpc_waitq);
2825 void rpc_show_tasks(struct net *net)
2827 struct rpc_clnt *clnt;
2828 struct rpc_task *task;
2829 int header = 0;
2830 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
2832 spin_lock(&sn->rpc_client_lock);
2833 list_for_each_entry(clnt, &sn->all_clients, cl_clients) {
2834 spin_lock(&clnt->cl_lock);
2835 list_for_each_entry(task, &clnt->cl_tasks, tk_task) {
2836 if (!header) {
2837 rpc_show_header();
2838 header++;
2840 rpc_show_task(clnt, task);
2842 spin_unlock(&clnt->cl_lock);
2844 spin_unlock(&sn->rpc_client_lock);
2846 #endif
2848 #if IS_ENABLED(CONFIG_SUNRPC_SWAP)
2849 static int
2850 rpc_clnt_swap_activate_callback(struct rpc_clnt *clnt,
2851 struct rpc_xprt *xprt,
2852 void *dummy)
2854 return xprt_enable_swap(xprt);
2858 rpc_clnt_swap_activate(struct rpc_clnt *clnt)
2860 if (atomic_inc_return(&clnt->cl_swapper) == 1)
2861 return rpc_clnt_iterate_for_each_xprt(clnt,
2862 rpc_clnt_swap_activate_callback, NULL);
2863 return 0;
2865 EXPORT_SYMBOL_GPL(rpc_clnt_swap_activate);
2867 static int
2868 rpc_clnt_swap_deactivate_callback(struct rpc_clnt *clnt,
2869 struct rpc_xprt *xprt,
2870 void *dummy)
2872 xprt_disable_swap(xprt);
2873 return 0;
2876 void
2877 rpc_clnt_swap_deactivate(struct rpc_clnt *clnt)
2879 if (atomic_dec_if_positive(&clnt->cl_swapper) == 0)
2880 rpc_clnt_iterate_for_each_xprt(clnt,
2881 rpc_clnt_swap_deactivate_callback, NULL);
2883 EXPORT_SYMBOL_GPL(rpc_clnt_swap_deactivate);
2884 #endif /* CONFIG_SUNRPC_SWAP */