gro: Allow tunnel stacking in the case of FOU/GUE
[linux/fpc-iii.git] / net / sunrpc / clnt.c
blob16e831dcfde04295146e40eb8a0dd58b78519250
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_decode(struct rpc_task *task);
65 static void call_bind(struct rpc_task *task);
66 static void call_bind_status(struct rpc_task *task);
67 static void call_transmit(struct rpc_task *task);
68 #if defined(CONFIG_SUNRPC_BACKCHANNEL)
69 static void call_bc_transmit(struct rpc_task *task);
70 #endif /* CONFIG_SUNRPC_BACKCHANNEL */
71 static void call_status(struct rpc_task *task);
72 static void call_transmit_status(struct rpc_task *task);
73 static void call_refresh(struct rpc_task *task);
74 static void call_refreshresult(struct rpc_task *task);
75 static void call_timeout(struct rpc_task *task);
76 static void call_connect(struct rpc_task *task);
77 static void call_connect_status(struct rpc_task *task);
79 static __be32 *rpc_encode_header(struct rpc_task *task);
80 static __be32 *rpc_verify_header(struct rpc_task *task);
81 static int rpc_ping(struct rpc_clnt *clnt);
83 static void rpc_register_client(struct rpc_clnt *clnt)
85 struct net *net = rpc_net_ns(clnt);
86 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
88 spin_lock(&sn->rpc_client_lock);
89 list_add(&clnt->cl_clients, &sn->all_clients);
90 spin_unlock(&sn->rpc_client_lock);
93 static void rpc_unregister_client(struct rpc_clnt *clnt)
95 struct net *net = rpc_net_ns(clnt);
96 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
98 spin_lock(&sn->rpc_client_lock);
99 list_del(&clnt->cl_clients);
100 spin_unlock(&sn->rpc_client_lock);
103 static void __rpc_clnt_remove_pipedir(struct rpc_clnt *clnt)
105 rpc_remove_client_dir(clnt);
108 static void rpc_clnt_remove_pipedir(struct rpc_clnt *clnt)
110 struct net *net = rpc_net_ns(clnt);
111 struct super_block *pipefs_sb;
113 pipefs_sb = rpc_get_sb_net(net);
114 if (pipefs_sb) {
115 __rpc_clnt_remove_pipedir(clnt);
116 rpc_put_sb_net(net);
120 static struct dentry *rpc_setup_pipedir_sb(struct super_block *sb,
121 struct rpc_clnt *clnt)
123 static uint32_t clntid;
124 const char *dir_name = clnt->cl_program->pipe_dir_name;
125 char name[15];
126 struct dentry *dir, *dentry;
128 dir = rpc_d_lookup_sb(sb, dir_name);
129 if (dir == NULL) {
130 pr_info("RPC: pipefs directory doesn't exist: %s\n", dir_name);
131 return dir;
133 for (;;) {
134 snprintf(name, sizeof(name), "clnt%x", (unsigned int)clntid++);
135 name[sizeof(name) - 1] = '\0';
136 dentry = rpc_create_client_dir(dir, name, clnt);
137 if (!IS_ERR(dentry))
138 break;
139 if (dentry == ERR_PTR(-EEXIST))
140 continue;
141 printk(KERN_INFO "RPC: Couldn't create pipefs entry"
142 " %s/%s, error %ld\n",
143 dir_name, name, PTR_ERR(dentry));
144 break;
146 dput(dir);
147 return dentry;
150 static int
151 rpc_setup_pipedir(struct super_block *pipefs_sb, struct rpc_clnt *clnt)
153 struct dentry *dentry;
155 if (clnt->cl_program->pipe_dir_name != NULL) {
156 dentry = rpc_setup_pipedir_sb(pipefs_sb, clnt);
157 if (IS_ERR(dentry))
158 return PTR_ERR(dentry);
160 return 0;
163 static int rpc_clnt_skip_event(struct rpc_clnt *clnt, unsigned long event)
165 if (clnt->cl_program->pipe_dir_name == NULL)
166 return 1;
168 switch (event) {
169 case RPC_PIPEFS_MOUNT:
170 if (clnt->cl_pipedir_objects.pdh_dentry != NULL)
171 return 1;
172 if (atomic_read(&clnt->cl_count) == 0)
173 return 1;
174 break;
175 case RPC_PIPEFS_UMOUNT:
176 if (clnt->cl_pipedir_objects.pdh_dentry == NULL)
177 return 1;
178 break;
180 return 0;
183 static int __rpc_clnt_handle_event(struct rpc_clnt *clnt, unsigned long event,
184 struct super_block *sb)
186 struct dentry *dentry;
187 int err = 0;
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 err;
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 static int rpc_alloc_clid(struct rpc_clnt *clnt)
342 int clid;
344 clid = ida_simple_get(&rpc_clids, 0, 0, GFP_KERNEL);
345 if (clid < 0)
346 return clid;
347 clnt->cl_clid = clid;
348 return 0;
351 static void rpc_free_clid(struct rpc_clnt *clnt)
353 ida_simple_remove(&rpc_clids, clnt->cl_clid);
356 static struct rpc_clnt * rpc_new_client(const struct rpc_create_args *args,
357 struct rpc_xprt *xprt,
358 struct rpc_clnt *parent)
360 const struct rpc_program *program = args->program;
361 const struct rpc_version *version;
362 struct rpc_clnt *clnt = NULL;
363 const struct rpc_timeout *timeout;
364 const char *nodename = args->nodename;
365 int err;
367 /* sanity check the name before trying to print it */
368 dprintk("RPC: creating %s client for %s (xprt %p)\n",
369 program->name, args->servername, xprt);
371 err = rpciod_up();
372 if (err)
373 goto out_no_rpciod;
375 err = -EINVAL;
376 if (args->version >= program->nrvers)
377 goto out_err;
378 version = program->version[args->version];
379 if (version == NULL)
380 goto out_err;
382 err = -ENOMEM;
383 clnt = kzalloc(sizeof(*clnt), GFP_KERNEL);
384 if (!clnt)
385 goto out_err;
386 clnt->cl_parent = parent ? : clnt;
388 err = rpc_alloc_clid(clnt);
389 if (err)
390 goto out_no_clid;
392 clnt->cl_procinfo = version->procs;
393 clnt->cl_maxproc = version->nrprocs;
394 clnt->cl_prog = args->prognumber ? : program->number;
395 clnt->cl_vers = version->number;
396 clnt->cl_stats = program->stats;
397 clnt->cl_metrics = rpc_alloc_iostats(clnt);
398 rpc_init_pipe_dir_head(&clnt->cl_pipedir_objects);
399 err = -ENOMEM;
400 if (clnt->cl_metrics == NULL)
401 goto out_no_stats;
402 clnt->cl_program = program;
403 INIT_LIST_HEAD(&clnt->cl_tasks);
404 spin_lock_init(&clnt->cl_lock);
406 timeout = xprt->timeout;
407 if (args->timeout != NULL) {
408 memcpy(&clnt->cl_timeout_default, args->timeout,
409 sizeof(clnt->cl_timeout_default));
410 timeout = &clnt->cl_timeout_default;
413 rpc_clnt_set_transport(clnt, xprt, timeout);
415 clnt->cl_rtt = &clnt->cl_rtt_default;
416 rpc_init_rtt(&clnt->cl_rtt_default, clnt->cl_timeout->to_initval);
418 atomic_set(&clnt->cl_count, 1);
420 if (nodename == NULL)
421 nodename = utsname()->nodename;
422 /* save the nodename */
423 rpc_clnt_set_nodename(clnt, nodename);
425 err = rpc_client_register(clnt, args->authflavor, args->client_name);
426 if (err)
427 goto out_no_path;
428 if (parent)
429 atomic_inc(&parent->cl_count);
430 return clnt;
432 out_no_path:
433 rpc_free_iostats(clnt->cl_metrics);
434 out_no_stats:
435 rpc_free_clid(clnt);
436 out_no_clid:
437 kfree(clnt);
438 out_err:
439 rpciod_down();
440 out_no_rpciod:
441 xprt_put(xprt);
442 return ERR_PTR(err);
445 static struct rpc_clnt *rpc_create_xprt(struct rpc_create_args *args,
446 struct rpc_xprt *xprt)
448 struct rpc_clnt *clnt = NULL;
450 clnt = rpc_new_client(args, xprt, NULL);
451 if (IS_ERR(clnt))
452 return clnt;
454 if (!(args->flags & RPC_CLNT_CREATE_NOPING)) {
455 int err = rpc_ping(clnt);
456 if (err != 0) {
457 rpc_shutdown_client(clnt);
458 return ERR_PTR(err);
462 clnt->cl_softrtry = 1;
463 if (args->flags & RPC_CLNT_CREATE_HARDRTRY)
464 clnt->cl_softrtry = 0;
466 if (args->flags & RPC_CLNT_CREATE_AUTOBIND)
467 clnt->cl_autobind = 1;
468 if (args->flags & RPC_CLNT_CREATE_NO_RETRANS_TIMEOUT)
469 clnt->cl_noretranstimeo = 1;
470 if (args->flags & RPC_CLNT_CREATE_DISCRTRY)
471 clnt->cl_discrtry = 1;
472 if (!(args->flags & RPC_CLNT_CREATE_QUIET))
473 clnt->cl_chatty = 1;
475 return clnt;
479 * rpc_create - create an RPC client and transport with one call
480 * @args: rpc_clnt create argument structure
482 * Creates and initializes an RPC transport and an RPC client.
484 * It can ping the server in order to determine if it is up, and to see if
485 * it supports this program and version. RPC_CLNT_CREATE_NOPING disables
486 * this behavior so asynchronous tasks can also use rpc_create.
488 struct rpc_clnt *rpc_create(struct rpc_create_args *args)
490 struct rpc_xprt *xprt;
491 struct xprt_create xprtargs = {
492 .net = args->net,
493 .ident = args->protocol,
494 .srcaddr = args->saddress,
495 .dstaddr = args->address,
496 .addrlen = args->addrsize,
497 .servername = args->servername,
498 .bc_xprt = args->bc_xprt,
500 char servername[48];
502 if (args->bc_xprt) {
503 WARN_ON(args->protocol != XPRT_TRANSPORT_BC_TCP);
504 xprt = args->bc_xprt->xpt_bc_xprt;
505 if (xprt) {
506 xprt_get(xprt);
507 return rpc_create_xprt(args, xprt);
511 if (args->flags & RPC_CLNT_CREATE_INFINITE_SLOTS)
512 xprtargs.flags |= XPRT_CREATE_INFINITE_SLOTS;
513 if (args->flags & RPC_CLNT_CREATE_NO_IDLE_TIMEOUT)
514 xprtargs.flags |= XPRT_CREATE_NO_IDLE_TIMEOUT;
516 * If the caller chooses not to specify a hostname, whip
517 * up a string representation of the passed-in address.
519 if (xprtargs.servername == NULL) {
520 struct sockaddr_un *sun =
521 (struct sockaddr_un *)args->address;
522 struct sockaddr_in *sin =
523 (struct sockaddr_in *)args->address;
524 struct sockaddr_in6 *sin6 =
525 (struct sockaddr_in6 *)args->address;
527 servername[0] = '\0';
528 switch (args->address->sa_family) {
529 case AF_LOCAL:
530 snprintf(servername, sizeof(servername), "%s",
531 sun->sun_path);
532 break;
533 case AF_INET:
534 snprintf(servername, sizeof(servername), "%pI4",
535 &sin->sin_addr.s_addr);
536 break;
537 case AF_INET6:
538 snprintf(servername, sizeof(servername), "%pI6",
539 &sin6->sin6_addr);
540 break;
541 default:
542 /* caller wants default server name, but
543 * address family isn't recognized. */
544 return ERR_PTR(-EINVAL);
546 xprtargs.servername = servername;
549 xprt = xprt_create_transport(&xprtargs);
550 if (IS_ERR(xprt))
551 return (struct rpc_clnt *)xprt;
554 * By default, kernel RPC client connects from a reserved port.
555 * CAP_NET_BIND_SERVICE will not be set for unprivileged requesters,
556 * but it is always enabled for rpciod, which handles the connect
557 * operation.
559 xprt->resvport = 1;
560 if (args->flags & RPC_CLNT_CREATE_NONPRIVPORT)
561 xprt->resvport = 0;
563 return rpc_create_xprt(args, xprt);
565 EXPORT_SYMBOL_GPL(rpc_create);
568 * This function clones the RPC client structure. It allows us to share the
569 * same transport while varying parameters such as the authentication
570 * flavour.
572 static struct rpc_clnt *__rpc_clone_client(struct rpc_create_args *args,
573 struct rpc_clnt *clnt)
575 struct rpc_xprt *xprt;
576 struct rpc_clnt *new;
577 int err;
579 err = -ENOMEM;
580 rcu_read_lock();
581 xprt = xprt_get(rcu_dereference(clnt->cl_xprt));
582 rcu_read_unlock();
583 if (xprt == NULL)
584 goto out_err;
585 args->servername = xprt->servername;
586 args->nodename = clnt->cl_nodename;
588 new = rpc_new_client(args, xprt, clnt);
589 if (IS_ERR(new)) {
590 err = PTR_ERR(new);
591 goto out_err;
594 /* Turn off autobind on clones */
595 new->cl_autobind = 0;
596 new->cl_softrtry = clnt->cl_softrtry;
597 new->cl_noretranstimeo = clnt->cl_noretranstimeo;
598 new->cl_discrtry = clnt->cl_discrtry;
599 new->cl_chatty = clnt->cl_chatty;
600 return new;
602 out_err:
603 dprintk("RPC: %s: returned error %d\n", __func__, err);
604 return ERR_PTR(err);
608 * rpc_clone_client - Clone an RPC client structure
610 * @clnt: RPC client whose parameters are copied
612 * Returns a fresh RPC client or an ERR_PTR.
614 struct rpc_clnt *rpc_clone_client(struct rpc_clnt *clnt)
616 struct rpc_create_args args = {
617 .program = clnt->cl_program,
618 .prognumber = clnt->cl_prog,
619 .version = clnt->cl_vers,
620 .authflavor = clnt->cl_auth->au_flavor,
622 return __rpc_clone_client(&args, clnt);
624 EXPORT_SYMBOL_GPL(rpc_clone_client);
627 * rpc_clone_client_set_auth - Clone an RPC client structure and set its auth
629 * @clnt: RPC client whose parameters are copied
630 * @flavor: security flavor for new client
632 * Returns a fresh RPC client or an ERR_PTR.
634 struct rpc_clnt *
635 rpc_clone_client_set_auth(struct rpc_clnt *clnt, rpc_authflavor_t flavor)
637 struct rpc_create_args args = {
638 .program = clnt->cl_program,
639 .prognumber = clnt->cl_prog,
640 .version = clnt->cl_vers,
641 .authflavor = flavor,
643 return __rpc_clone_client(&args, clnt);
645 EXPORT_SYMBOL_GPL(rpc_clone_client_set_auth);
648 * rpc_switch_client_transport: switch the RPC transport on the fly
649 * @clnt: pointer to a struct rpc_clnt
650 * @args: pointer to the new transport arguments
651 * @timeout: pointer to the new timeout parameters
653 * This function allows the caller to switch the RPC transport for the
654 * rpc_clnt structure 'clnt' to allow it to connect to a mirrored NFS
655 * server, for instance. It assumes that the caller has ensured that
656 * there are no active RPC tasks by using some form of locking.
658 * Returns zero if "clnt" is now using the new xprt. Otherwise a
659 * negative errno is returned, and "clnt" continues to use the old
660 * xprt.
662 int rpc_switch_client_transport(struct rpc_clnt *clnt,
663 struct xprt_create *args,
664 const struct rpc_timeout *timeout)
666 const struct rpc_timeout *old_timeo;
667 rpc_authflavor_t pseudoflavor;
668 struct rpc_xprt *xprt, *old;
669 struct rpc_clnt *parent;
670 int err;
672 xprt = xprt_create_transport(args);
673 if (IS_ERR(xprt)) {
674 dprintk("RPC: failed to create new xprt for clnt %p\n",
675 clnt);
676 return PTR_ERR(xprt);
679 pseudoflavor = clnt->cl_auth->au_flavor;
681 old_timeo = clnt->cl_timeout;
682 old = rpc_clnt_set_transport(clnt, xprt, timeout);
684 rpc_unregister_client(clnt);
685 __rpc_clnt_remove_pipedir(clnt);
686 rpc_clnt_debugfs_unregister(clnt);
689 * A new transport was created. "clnt" therefore
690 * becomes the root of a new cl_parent tree. clnt's
691 * children, if it has any, still point to the old xprt.
693 parent = clnt->cl_parent;
694 clnt->cl_parent = clnt;
697 * The old rpc_auth cache cannot be re-used. GSS
698 * contexts in particular are between a single
699 * client and server.
701 err = rpc_client_register(clnt, pseudoflavor, NULL);
702 if (err)
703 goto out_revert;
705 synchronize_rcu();
706 if (parent != clnt)
707 rpc_release_client(parent);
708 xprt_put(old);
709 dprintk("RPC: replaced xprt for clnt %p\n", clnt);
710 return 0;
712 out_revert:
713 rpc_clnt_set_transport(clnt, old, old_timeo);
714 clnt->cl_parent = parent;
715 rpc_client_register(clnt, pseudoflavor, NULL);
716 xprt_put(xprt);
717 dprintk("RPC: failed to switch xprt for clnt %p\n", clnt);
718 return err;
720 EXPORT_SYMBOL_GPL(rpc_switch_client_transport);
723 * Kill all tasks for the given client.
724 * XXX: kill their descendants as well?
726 void rpc_killall_tasks(struct rpc_clnt *clnt)
728 struct rpc_task *rovr;
731 if (list_empty(&clnt->cl_tasks))
732 return;
733 dprintk("RPC: killing all tasks for client %p\n", clnt);
735 * Spin lock all_tasks to prevent changes...
737 spin_lock(&clnt->cl_lock);
738 list_for_each_entry(rovr, &clnt->cl_tasks, tk_task) {
739 if (!RPC_IS_ACTIVATED(rovr))
740 continue;
741 if (!(rovr->tk_flags & RPC_TASK_KILLED)) {
742 rovr->tk_flags |= RPC_TASK_KILLED;
743 rpc_exit(rovr, -EIO);
744 if (RPC_IS_QUEUED(rovr))
745 rpc_wake_up_queued_task(rovr->tk_waitqueue,
746 rovr);
749 spin_unlock(&clnt->cl_lock);
751 EXPORT_SYMBOL_GPL(rpc_killall_tasks);
754 * Properly shut down an RPC client, terminating all outstanding
755 * requests.
757 void rpc_shutdown_client(struct rpc_clnt *clnt)
759 might_sleep();
761 dprintk_rcu("RPC: shutting down %s client for %s\n",
762 clnt->cl_program->name,
763 rcu_dereference(clnt->cl_xprt)->servername);
765 while (!list_empty(&clnt->cl_tasks)) {
766 rpc_killall_tasks(clnt);
767 wait_event_timeout(destroy_wait,
768 list_empty(&clnt->cl_tasks), 1*HZ);
771 rpc_release_client(clnt);
773 EXPORT_SYMBOL_GPL(rpc_shutdown_client);
776 * Free an RPC client
778 static struct rpc_clnt *
779 rpc_free_client(struct rpc_clnt *clnt)
781 struct rpc_clnt *parent = NULL;
783 dprintk_rcu("RPC: destroying %s client for %s\n",
784 clnt->cl_program->name,
785 rcu_dereference(clnt->cl_xprt)->servername);
786 if (clnt->cl_parent != clnt)
787 parent = clnt->cl_parent;
788 rpc_clnt_debugfs_unregister(clnt);
789 rpc_clnt_remove_pipedir(clnt);
790 rpc_unregister_client(clnt);
791 rpc_free_iostats(clnt->cl_metrics);
792 clnt->cl_metrics = NULL;
793 xprt_put(rcu_dereference_raw(clnt->cl_xprt));
794 rpciod_down();
795 rpc_free_clid(clnt);
796 kfree(clnt);
797 return parent;
801 * Free an RPC client
803 static struct rpc_clnt *
804 rpc_free_auth(struct rpc_clnt *clnt)
806 if (clnt->cl_auth == NULL)
807 return rpc_free_client(clnt);
810 * Note: RPCSEC_GSS may need to send NULL RPC calls in order to
811 * release remaining GSS contexts. This mechanism ensures
812 * that it can do so safely.
814 atomic_inc(&clnt->cl_count);
815 rpcauth_release(clnt->cl_auth);
816 clnt->cl_auth = NULL;
817 if (atomic_dec_and_test(&clnt->cl_count))
818 return rpc_free_client(clnt);
819 return NULL;
823 * Release reference to the RPC client
825 void
826 rpc_release_client(struct rpc_clnt *clnt)
828 dprintk("RPC: rpc_release_client(%p)\n", clnt);
830 do {
831 if (list_empty(&clnt->cl_tasks))
832 wake_up(&destroy_wait);
833 if (!atomic_dec_and_test(&clnt->cl_count))
834 break;
835 clnt = rpc_free_auth(clnt);
836 } while (clnt != NULL);
838 EXPORT_SYMBOL_GPL(rpc_release_client);
841 * rpc_bind_new_program - bind a new RPC program to an existing client
842 * @old: old rpc_client
843 * @program: rpc program to set
844 * @vers: rpc program version
846 * Clones the rpc client and sets up a new RPC program. This is mainly
847 * of use for enabling different RPC programs to share the same transport.
848 * The Sun NFSv2/v3 ACL protocol can do this.
850 struct rpc_clnt *rpc_bind_new_program(struct rpc_clnt *old,
851 const struct rpc_program *program,
852 u32 vers)
854 struct rpc_create_args args = {
855 .program = program,
856 .prognumber = program->number,
857 .version = vers,
858 .authflavor = old->cl_auth->au_flavor,
860 struct rpc_clnt *clnt;
861 int err;
863 clnt = __rpc_clone_client(&args, old);
864 if (IS_ERR(clnt))
865 goto out;
866 err = rpc_ping(clnt);
867 if (err != 0) {
868 rpc_shutdown_client(clnt);
869 clnt = ERR_PTR(err);
871 out:
872 return clnt;
874 EXPORT_SYMBOL_GPL(rpc_bind_new_program);
876 void rpc_task_release_client(struct rpc_task *task)
878 struct rpc_clnt *clnt = task->tk_client;
880 if (clnt != NULL) {
881 /* Remove from client task list */
882 spin_lock(&clnt->cl_lock);
883 list_del(&task->tk_task);
884 spin_unlock(&clnt->cl_lock);
885 task->tk_client = NULL;
887 rpc_release_client(clnt);
891 static
892 void rpc_task_set_client(struct rpc_task *task, struct rpc_clnt *clnt)
894 if (clnt != NULL) {
895 rpc_task_release_client(task);
896 task->tk_client = clnt;
897 atomic_inc(&clnt->cl_count);
898 if (clnt->cl_softrtry)
899 task->tk_flags |= RPC_TASK_SOFT;
900 if (clnt->cl_noretranstimeo)
901 task->tk_flags |= RPC_TASK_NO_RETRANS_TIMEOUT;
902 if (sk_memalloc_socks()) {
903 struct rpc_xprt *xprt;
905 rcu_read_lock();
906 xprt = rcu_dereference(clnt->cl_xprt);
907 if (xprt->swapper)
908 task->tk_flags |= RPC_TASK_SWAPPER;
909 rcu_read_unlock();
911 /* Add to the client's list of all tasks */
912 spin_lock(&clnt->cl_lock);
913 list_add_tail(&task->tk_task, &clnt->cl_tasks);
914 spin_unlock(&clnt->cl_lock);
918 void rpc_task_reset_client(struct rpc_task *task, struct rpc_clnt *clnt)
920 rpc_task_release_client(task);
921 rpc_task_set_client(task, clnt);
923 EXPORT_SYMBOL_GPL(rpc_task_reset_client);
926 static void
927 rpc_task_set_rpc_message(struct rpc_task *task, const struct rpc_message *msg)
929 if (msg != NULL) {
930 task->tk_msg.rpc_proc = msg->rpc_proc;
931 task->tk_msg.rpc_argp = msg->rpc_argp;
932 task->tk_msg.rpc_resp = msg->rpc_resp;
933 if (msg->rpc_cred != NULL)
934 task->tk_msg.rpc_cred = get_rpccred(msg->rpc_cred);
939 * Default callback for async RPC calls
941 static void
942 rpc_default_callback(struct rpc_task *task, void *data)
946 static const struct rpc_call_ops rpc_default_ops = {
947 .rpc_call_done = rpc_default_callback,
951 * rpc_run_task - Allocate a new RPC task, then run rpc_execute against it
952 * @task_setup_data: pointer to task initialisation data
954 struct rpc_task *rpc_run_task(const struct rpc_task_setup *task_setup_data)
956 struct rpc_task *task;
958 task = rpc_new_task(task_setup_data);
959 if (IS_ERR(task))
960 goto out;
962 rpc_task_set_client(task, task_setup_data->rpc_client);
963 rpc_task_set_rpc_message(task, task_setup_data->rpc_message);
965 if (task->tk_action == NULL)
966 rpc_call_start(task);
968 atomic_inc(&task->tk_count);
969 rpc_execute(task);
970 out:
971 return task;
973 EXPORT_SYMBOL_GPL(rpc_run_task);
976 * rpc_call_sync - Perform a synchronous RPC call
977 * @clnt: pointer to RPC client
978 * @msg: RPC call parameters
979 * @flags: RPC call flags
981 int rpc_call_sync(struct rpc_clnt *clnt, const struct rpc_message *msg, int flags)
983 struct rpc_task *task;
984 struct rpc_task_setup task_setup_data = {
985 .rpc_client = clnt,
986 .rpc_message = msg,
987 .callback_ops = &rpc_default_ops,
988 .flags = flags,
990 int status;
992 WARN_ON_ONCE(flags & RPC_TASK_ASYNC);
993 if (flags & RPC_TASK_ASYNC) {
994 rpc_release_calldata(task_setup_data.callback_ops,
995 task_setup_data.callback_data);
996 return -EINVAL;
999 task = rpc_run_task(&task_setup_data);
1000 if (IS_ERR(task))
1001 return PTR_ERR(task);
1002 status = task->tk_status;
1003 rpc_put_task(task);
1004 return status;
1006 EXPORT_SYMBOL_GPL(rpc_call_sync);
1009 * rpc_call_async - Perform an asynchronous RPC call
1010 * @clnt: pointer to RPC client
1011 * @msg: RPC call parameters
1012 * @flags: RPC call flags
1013 * @tk_ops: RPC call ops
1014 * @data: user call data
1017 rpc_call_async(struct rpc_clnt *clnt, const struct rpc_message *msg, int flags,
1018 const struct rpc_call_ops *tk_ops, void *data)
1020 struct rpc_task *task;
1021 struct rpc_task_setup task_setup_data = {
1022 .rpc_client = clnt,
1023 .rpc_message = msg,
1024 .callback_ops = tk_ops,
1025 .callback_data = data,
1026 .flags = flags|RPC_TASK_ASYNC,
1029 task = rpc_run_task(&task_setup_data);
1030 if (IS_ERR(task))
1031 return PTR_ERR(task);
1032 rpc_put_task(task);
1033 return 0;
1035 EXPORT_SYMBOL_GPL(rpc_call_async);
1037 #if defined(CONFIG_SUNRPC_BACKCHANNEL)
1039 * rpc_run_bc_task - Allocate a new RPC task for backchannel use, then run
1040 * rpc_execute against it
1041 * @req: RPC request
1042 * @tk_ops: RPC call ops
1044 struct rpc_task *rpc_run_bc_task(struct rpc_rqst *req,
1045 const struct rpc_call_ops *tk_ops)
1047 struct rpc_task *task;
1048 struct xdr_buf *xbufp = &req->rq_snd_buf;
1049 struct rpc_task_setup task_setup_data = {
1050 .callback_ops = tk_ops,
1053 dprintk("RPC: rpc_run_bc_task req= %p\n", req);
1055 * Create an rpc_task to send the data
1057 task = rpc_new_task(&task_setup_data);
1058 if (IS_ERR(task)) {
1059 xprt_free_bc_request(req);
1060 goto out;
1062 task->tk_rqstp = req;
1065 * Set up the xdr_buf length.
1066 * This also indicates that the buffer is XDR encoded already.
1068 xbufp->len = xbufp->head[0].iov_len + xbufp->page_len +
1069 xbufp->tail[0].iov_len;
1071 task->tk_action = call_bc_transmit;
1072 atomic_inc(&task->tk_count);
1073 WARN_ON_ONCE(atomic_read(&task->tk_count) != 2);
1074 rpc_execute(task);
1076 out:
1077 dprintk("RPC: rpc_run_bc_task: task= %p\n", task);
1078 return task;
1080 #endif /* CONFIG_SUNRPC_BACKCHANNEL */
1082 void
1083 rpc_call_start(struct rpc_task *task)
1085 task->tk_action = call_start;
1087 EXPORT_SYMBOL_GPL(rpc_call_start);
1090 * rpc_peeraddr - extract remote peer address from clnt's xprt
1091 * @clnt: RPC client structure
1092 * @buf: target buffer
1093 * @bufsize: length of target buffer
1095 * Returns the number of bytes that are actually in the stored address.
1097 size_t rpc_peeraddr(struct rpc_clnt *clnt, struct sockaddr *buf, size_t bufsize)
1099 size_t bytes;
1100 struct rpc_xprt *xprt;
1102 rcu_read_lock();
1103 xprt = rcu_dereference(clnt->cl_xprt);
1105 bytes = xprt->addrlen;
1106 if (bytes > bufsize)
1107 bytes = bufsize;
1108 memcpy(buf, &xprt->addr, bytes);
1109 rcu_read_unlock();
1111 return bytes;
1113 EXPORT_SYMBOL_GPL(rpc_peeraddr);
1116 * rpc_peeraddr2str - return remote peer address in printable format
1117 * @clnt: RPC client structure
1118 * @format: address format
1120 * NB: the lifetime of the memory referenced by the returned pointer is
1121 * the same as the rpc_xprt itself. As long as the caller uses this
1122 * pointer, it must hold the RCU read lock.
1124 const char *rpc_peeraddr2str(struct rpc_clnt *clnt,
1125 enum rpc_display_format_t format)
1127 struct rpc_xprt *xprt;
1129 xprt = rcu_dereference(clnt->cl_xprt);
1131 if (xprt->address_strings[format] != NULL)
1132 return xprt->address_strings[format];
1133 else
1134 return "unprintable";
1136 EXPORT_SYMBOL_GPL(rpc_peeraddr2str);
1138 static const struct sockaddr_in rpc_inaddr_loopback = {
1139 .sin_family = AF_INET,
1140 .sin_addr.s_addr = htonl(INADDR_ANY),
1143 static const struct sockaddr_in6 rpc_in6addr_loopback = {
1144 .sin6_family = AF_INET6,
1145 .sin6_addr = IN6ADDR_ANY_INIT,
1149 * Try a getsockname() on a connected datagram socket. Using a
1150 * connected datagram socket prevents leaving a socket in TIME_WAIT.
1151 * This conserves the ephemeral port number space.
1153 * Returns zero and fills in "buf" if successful; otherwise, a
1154 * negative errno is returned.
1156 static int rpc_sockname(struct net *net, struct sockaddr *sap, size_t salen,
1157 struct sockaddr *buf, int buflen)
1159 struct socket *sock;
1160 int err;
1162 err = __sock_create(net, sap->sa_family,
1163 SOCK_DGRAM, IPPROTO_UDP, &sock, 1);
1164 if (err < 0) {
1165 dprintk("RPC: can't create UDP socket (%d)\n", err);
1166 goto out;
1169 switch (sap->sa_family) {
1170 case AF_INET:
1171 err = kernel_bind(sock,
1172 (struct sockaddr *)&rpc_inaddr_loopback,
1173 sizeof(rpc_inaddr_loopback));
1174 break;
1175 case AF_INET6:
1176 err = kernel_bind(sock,
1177 (struct sockaddr *)&rpc_in6addr_loopback,
1178 sizeof(rpc_in6addr_loopback));
1179 break;
1180 default:
1181 err = -EAFNOSUPPORT;
1182 goto out;
1184 if (err < 0) {
1185 dprintk("RPC: can't bind UDP socket (%d)\n", err);
1186 goto out_release;
1189 err = kernel_connect(sock, sap, salen, 0);
1190 if (err < 0) {
1191 dprintk("RPC: can't connect UDP socket (%d)\n", err);
1192 goto out_release;
1195 err = kernel_getsockname(sock, buf, &buflen);
1196 if (err < 0) {
1197 dprintk("RPC: getsockname failed (%d)\n", err);
1198 goto out_release;
1201 err = 0;
1202 if (buf->sa_family == AF_INET6) {
1203 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)buf;
1204 sin6->sin6_scope_id = 0;
1206 dprintk("RPC: %s succeeded\n", __func__);
1208 out_release:
1209 sock_release(sock);
1210 out:
1211 return err;
1215 * Scraping a connected socket failed, so we don't have a useable
1216 * local address. Fallback: generate an address that will prevent
1217 * the server from calling us back.
1219 * Returns zero and fills in "buf" if successful; otherwise, a
1220 * negative errno is returned.
1222 static int rpc_anyaddr(int family, struct sockaddr *buf, size_t buflen)
1224 switch (family) {
1225 case AF_INET:
1226 if (buflen < sizeof(rpc_inaddr_loopback))
1227 return -EINVAL;
1228 memcpy(buf, &rpc_inaddr_loopback,
1229 sizeof(rpc_inaddr_loopback));
1230 break;
1231 case AF_INET6:
1232 if (buflen < sizeof(rpc_in6addr_loopback))
1233 return -EINVAL;
1234 memcpy(buf, &rpc_in6addr_loopback,
1235 sizeof(rpc_in6addr_loopback));
1236 default:
1237 dprintk("RPC: %s: address family not supported\n",
1238 __func__);
1239 return -EAFNOSUPPORT;
1241 dprintk("RPC: %s: succeeded\n", __func__);
1242 return 0;
1246 * rpc_localaddr - discover local endpoint address for an RPC client
1247 * @clnt: RPC client structure
1248 * @buf: target buffer
1249 * @buflen: size of target buffer, in bytes
1251 * Returns zero and fills in "buf" and "buflen" if successful;
1252 * otherwise, a negative errno is returned.
1254 * This works even if the underlying transport is not currently connected,
1255 * or if the upper layer never previously provided a source address.
1257 * The result of this function call is transient: multiple calls in
1258 * succession may give different results, depending on how local
1259 * networking configuration changes over time.
1261 int rpc_localaddr(struct rpc_clnt *clnt, struct sockaddr *buf, size_t buflen)
1263 struct sockaddr_storage address;
1264 struct sockaddr *sap = (struct sockaddr *)&address;
1265 struct rpc_xprt *xprt;
1266 struct net *net;
1267 size_t salen;
1268 int err;
1270 rcu_read_lock();
1271 xprt = rcu_dereference(clnt->cl_xprt);
1272 salen = xprt->addrlen;
1273 memcpy(sap, &xprt->addr, salen);
1274 net = get_net(xprt->xprt_net);
1275 rcu_read_unlock();
1277 rpc_set_port(sap, 0);
1278 err = rpc_sockname(net, sap, salen, buf, buflen);
1279 put_net(net);
1280 if (err != 0)
1281 /* Couldn't discover local address, return ANYADDR */
1282 return rpc_anyaddr(sap->sa_family, buf, buflen);
1283 return 0;
1285 EXPORT_SYMBOL_GPL(rpc_localaddr);
1287 void
1288 rpc_setbufsize(struct rpc_clnt *clnt, unsigned int sndsize, unsigned int rcvsize)
1290 struct rpc_xprt *xprt;
1292 rcu_read_lock();
1293 xprt = rcu_dereference(clnt->cl_xprt);
1294 if (xprt->ops->set_buffer_size)
1295 xprt->ops->set_buffer_size(xprt, sndsize, rcvsize);
1296 rcu_read_unlock();
1298 EXPORT_SYMBOL_GPL(rpc_setbufsize);
1301 * rpc_protocol - Get transport protocol number for an RPC client
1302 * @clnt: RPC client to query
1305 int rpc_protocol(struct rpc_clnt *clnt)
1307 int protocol;
1309 rcu_read_lock();
1310 protocol = rcu_dereference(clnt->cl_xprt)->prot;
1311 rcu_read_unlock();
1312 return protocol;
1314 EXPORT_SYMBOL_GPL(rpc_protocol);
1317 * rpc_net_ns - Get the network namespace for this RPC client
1318 * @clnt: RPC client to query
1321 struct net *rpc_net_ns(struct rpc_clnt *clnt)
1323 struct net *ret;
1325 rcu_read_lock();
1326 ret = rcu_dereference(clnt->cl_xprt)->xprt_net;
1327 rcu_read_unlock();
1328 return ret;
1330 EXPORT_SYMBOL_GPL(rpc_net_ns);
1333 * rpc_max_payload - Get maximum payload size for a transport, in bytes
1334 * @clnt: RPC client to query
1336 * For stream transports, this is one RPC record fragment (see RFC
1337 * 1831), as we don't support multi-record requests yet. For datagram
1338 * transports, this is the size of an IP packet minus the IP, UDP, and
1339 * RPC header sizes.
1341 size_t rpc_max_payload(struct rpc_clnt *clnt)
1343 size_t ret;
1345 rcu_read_lock();
1346 ret = rcu_dereference(clnt->cl_xprt)->max_payload;
1347 rcu_read_unlock();
1348 return ret;
1350 EXPORT_SYMBOL_GPL(rpc_max_payload);
1353 * rpc_get_timeout - Get timeout for transport in units of HZ
1354 * @clnt: RPC client to query
1356 unsigned long rpc_get_timeout(struct rpc_clnt *clnt)
1358 unsigned long ret;
1360 rcu_read_lock();
1361 ret = rcu_dereference(clnt->cl_xprt)->timeout->to_initval;
1362 rcu_read_unlock();
1363 return ret;
1365 EXPORT_SYMBOL_GPL(rpc_get_timeout);
1368 * rpc_force_rebind - force transport to check that remote port is unchanged
1369 * @clnt: client to rebind
1372 void rpc_force_rebind(struct rpc_clnt *clnt)
1374 if (clnt->cl_autobind) {
1375 rcu_read_lock();
1376 xprt_clear_bound(rcu_dereference(clnt->cl_xprt));
1377 rcu_read_unlock();
1380 EXPORT_SYMBOL_GPL(rpc_force_rebind);
1383 * Restart an (async) RPC call from the call_prepare state.
1384 * Usually called from within the exit handler.
1387 rpc_restart_call_prepare(struct rpc_task *task)
1389 if (RPC_ASSASSINATED(task))
1390 return 0;
1391 task->tk_action = call_start;
1392 task->tk_status = 0;
1393 if (task->tk_ops->rpc_call_prepare != NULL)
1394 task->tk_action = rpc_prepare_task;
1395 return 1;
1397 EXPORT_SYMBOL_GPL(rpc_restart_call_prepare);
1400 * Restart an (async) RPC call. Usually called from within the
1401 * exit handler.
1404 rpc_restart_call(struct rpc_task *task)
1406 if (RPC_ASSASSINATED(task))
1407 return 0;
1408 task->tk_action = call_start;
1409 task->tk_status = 0;
1410 return 1;
1412 EXPORT_SYMBOL_GPL(rpc_restart_call);
1414 #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
1415 const char
1416 *rpc_proc_name(const struct rpc_task *task)
1418 const struct rpc_procinfo *proc = task->tk_msg.rpc_proc;
1420 if (proc) {
1421 if (proc->p_name)
1422 return proc->p_name;
1423 else
1424 return "NULL";
1425 } else
1426 return "no proc";
1428 #endif
1431 * 0. Initial state
1433 * Other FSM states can be visited zero or more times, but
1434 * this state is visited exactly once for each RPC.
1436 static void
1437 call_start(struct rpc_task *task)
1439 struct rpc_clnt *clnt = task->tk_client;
1441 dprintk("RPC: %5u call_start %s%d proc %s (%s)\n", task->tk_pid,
1442 clnt->cl_program->name, clnt->cl_vers,
1443 rpc_proc_name(task),
1444 (RPC_IS_ASYNC(task) ? "async" : "sync"));
1446 /* Increment call count */
1447 task->tk_msg.rpc_proc->p_count++;
1448 clnt->cl_stats->rpccnt++;
1449 task->tk_action = call_reserve;
1453 * 1. Reserve an RPC call slot
1455 static void
1456 call_reserve(struct rpc_task *task)
1458 dprint_status(task);
1460 task->tk_status = 0;
1461 task->tk_action = call_reserveresult;
1462 xprt_reserve(task);
1465 static void call_retry_reserve(struct rpc_task *task);
1468 * 1b. Grok the result of xprt_reserve()
1470 static void
1471 call_reserveresult(struct rpc_task *task)
1473 int status = task->tk_status;
1475 dprint_status(task);
1478 * After a call to xprt_reserve(), we must have either
1479 * a request slot or else an error status.
1481 task->tk_status = 0;
1482 if (status >= 0) {
1483 if (task->tk_rqstp) {
1484 task->tk_action = call_refresh;
1485 return;
1488 printk(KERN_ERR "%s: status=%d, but no request slot, exiting\n",
1489 __func__, status);
1490 rpc_exit(task, -EIO);
1491 return;
1495 * Even though there was an error, we may have acquired
1496 * a request slot somehow. Make sure not to leak it.
1498 if (task->tk_rqstp) {
1499 printk(KERN_ERR "%s: status=%d, request allocated anyway\n",
1500 __func__, status);
1501 xprt_release(task);
1504 switch (status) {
1505 case -ENOMEM:
1506 rpc_delay(task, HZ >> 2);
1507 case -EAGAIN: /* woken up; retry */
1508 task->tk_action = call_retry_reserve;
1509 return;
1510 case -EIO: /* probably a shutdown */
1511 break;
1512 default:
1513 printk(KERN_ERR "%s: unrecognized error %d, exiting\n",
1514 __func__, status);
1515 break;
1517 rpc_exit(task, status);
1521 * 1c. Retry reserving an RPC call slot
1523 static void
1524 call_retry_reserve(struct rpc_task *task)
1526 dprint_status(task);
1528 task->tk_status = 0;
1529 task->tk_action = call_reserveresult;
1530 xprt_retry_reserve(task);
1534 * 2. Bind and/or refresh the credentials
1536 static void
1537 call_refresh(struct rpc_task *task)
1539 dprint_status(task);
1541 task->tk_action = call_refreshresult;
1542 task->tk_status = 0;
1543 task->tk_client->cl_stats->rpcauthrefresh++;
1544 rpcauth_refreshcred(task);
1548 * 2a. Process the results of a credential refresh
1550 static void
1551 call_refreshresult(struct rpc_task *task)
1553 int status = task->tk_status;
1555 dprint_status(task);
1557 task->tk_status = 0;
1558 task->tk_action = call_refresh;
1559 switch (status) {
1560 case 0:
1561 if (rpcauth_uptodatecred(task)) {
1562 task->tk_action = call_allocate;
1563 return;
1565 /* Use rate-limiting and a max number of retries if refresh
1566 * had status 0 but failed to update the cred.
1568 case -ETIMEDOUT:
1569 rpc_delay(task, 3*HZ);
1570 case -EAGAIN:
1571 status = -EACCES;
1572 case -EKEYEXPIRED:
1573 if (!task->tk_cred_retry)
1574 break;
1575 task->tk_cred_retry--;
1576 dprintk("RPC: %5u %s: retry refresh creds\n",
1577 task->tk_pid, __func__);
1578 return;
1580 dprintk("RPC: %5u %s: refresh creds failed with error %d\n",
1581 task->tk_pid, __func__, status);
1582 rpc_exit(task, status);
1586 * 2b. Allocate the buffer. For details, see sched.c:rpc_malloc.
1587 * (Note: buffer memory is freed in xprt_release).
1589 static void
1590 call_allocate(struct rpc_task *task)
1592 unsigned int slack = task->tk_rqstp->rq_cred->cr_auth->au_cslack;
1593 struct rpc_rqst *req = task->tk_rqstp;
1594 struct rpc_xprt *xprt = req->rq_xprt;
1595 struct rpc_procinfo *proc = task->tk_msg.rpc_proc;
1597 dprint_status(task);
1599 task->tk_status = 0;
1600 task->tk_action = call_bind;
1602 if (req->rq_buffer)
1603 return;
1605 if (proc->p_proc != 0) {
1606 BUG_ON(proc->p_arglen == 0);
1607 if (proc->p_decode != NULL)
1608 BUG_ON(proc->p_replen == 0);
1612 * Calculate the size (in quads) of the RPC call
1613 * and reply headers, and convert both values
1614 * to byte sizes.
1616 req->rq_callsize = RPC_CALLHDRSIZE + (slack << 1) + proc->p_arglen;
1617 req->rq_callsize <<= 2;
1618 req->rq_rcvsize = RPC_REPHDRSIZE + slack + proc->p_replen;
1619 req->rq_rcvsize <<= 2;
1621 req->rq_buffer = xprt->ops->buf_alloc(task,
1622 req->rq_callsize + req->rq_rcvsize);
1623 if (req->rq_buffer != NULL)
1624 return;
1626 dprintk("RPC: %5u rpc_buffer allocation failed\n", task->tk_pid);
1628 if (RPC_IS_ASYNC(task) || !fatal_signal_pending(current)) {
1629 task->tk_action = call_allocate;
1630 rpc_delay(task, HZ>>4);
1631 return;
1634 rpc_exit(task, -ERESTARTSYS);
1637 static inline int
1638 rpc_task_need_encode(struct rpc_task *task)
1640 return task->tk_rqstp->rq_snd_buf.len == 0;
1643 static inline void
1644 rpc_task_force_reencode(struct rpc_task *task)
1646 task->tk_rqstp->rq_snd_buf.len = 0;
1647 task->tk_rqstp->rq_bytes_sent = 0;
1650 static inline void
1651 rpc_xdr_buf_init(struct xdr_buf *buf, void *start, size_t len)
1653 buf->head[0].iov_base = start;
1654 buf->head[0].iov_len = len;
1655 buf->tail[0].iov_len = 0;
1656 buf->page_len = 0;
1657 buf->flags = 0;
1658 buf->len = 0;
1659 buf->buflen = len;
1663 * 3. Encode arguments of an RPC call
1665 static void
1666 rpc_xdr_encode(struct rpc_task *task)
1668 struct rpc_rqst *req = task->tk_rqstp;
1669 kxdreproc_t encode;
1670 __be32 *p;
1672 dprint_status(task);
1674 rpc_xdr_buf_init(&req->rq_snd_buf,
1675 req->rq_buffer,
1676 req->rq_callsize);
1677 rpc_xdr_buf_init(&req->rq_rcv_buf,
1678 (char *)req->rq_buffer + req->rq_callsize,
1679 req->rq_rcvsize);
1681 p = rpc_encode_header(task);
1682 if (p == NULL) {
1683 printk(KERN_INFO "RPC: couldn't encode RPC header, exit EIO\n");
1684 rpc_exit(task, -EIO);
1685 return;
1688 encode = task->tk_msg.rpc_proc->p_encode;
1689 if (encode == NULL)
1690 return;
1692 task->tk_status = rpcauth_wrap_req(task, encode, req, p,
1693 task->tk_msg.rpc_argp);
1697 * 4. Get the server port number if not yet set
1699 static void
1700 call_bind(struct rpc_task *task)
1702 struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
1704 dprint_status(task);
1706 task->tk_action = call_connect;
1707 if (!xprt_bound(xprt)) {
1708 task->tk_action = call_bind_status;
1709 task->tk_timeout = xprt->bind_timeout;
1710 xprt->ops->rpcbind(task);
1715 * 4a. Sort out bind result
1717 static void
1718 call_bind_status(struct rpc_task *task)
1720 int status = -EIO;
1722 if (task->tk_status >= 0) {
1723 dprint_status(task);
1724 task->tk_status = 0;
1725 task->tk_action = call_connect;
1726 return;
1729 trace_rpc_bind_status(task);
1730 switch (task->tk_status) {
1731 case -ENOMEM:
1732 dprintk("RPC: %5u rpcbind out of memory\n", task->tk_pid);
1733 rpc_delay(task, HZ >> 2);
1734 goto retry_timeout;
1735 case -EACCES:
1736 dprintk("RPC: %5u remote rpcbind: RPC program/version "
1737 "unavailable\n", task->tk_pid);
1738 /* fail immediately if this is an RPC ping */
1739 if (task->tk_msg.rpc_proc->p_proc == 0) {
1740 status = -EOPNOTSUPP;
1741 break;
1743 if (task->tk_rebind_retry == 0)
1744 break;
1745 task->tk_rebind_retry--;
1746 rpc_delay(task, 3*HZ);
1747 goto retry_timeout;
1748 case -ETIMEDOUT:
1749 dprintk("RPC: %5u rpcbind request timed out\n",
1750 task->tk_pid);
1751 goto retry_timeout;
1752 case -EPFNOSUPPORT:
1753 /* server doesn't support any rpcbind version we know of */
1754 dprintk("RPC: %5u unrecognized remote rpcbind service\n",
1755 task->tk_pid);
1756 break;
1757 case -EPROTONOSUPPORT:
1758 dprintk("RPC: %5u remote rpcbind version unavailable, retrying\n",
1759 task->tk_pid);
1760 goto retry_timeout;
1761 case -ECONNREFUSED: /* connection problems */
1762 case -ECONNRESET:
1763 case -ECONNABORTED:
1764 case -ENOTCONN:
1765 case -EHOSTDOWN:
1766 case -EHOSTUNREACH:
1767 case -ENETUNREACH:
1768 case -ENOBUFS:
1769 case -EPIPE:
1770 dprintk("RPC: %5u remote rpcbind unreachable: %d\n",
1771 task->tk_pid, task->tk_status);
1772 if (!RPC_IS_SOFTCONN(task)) {
1773 rpc_delay(task, 5*HZ);
1774 goto retry_timeout;
1776 status = task->tk_status;
1777 break;
1778 default:
1779 dprintk("RPC: %5u unrecognized rpcbind error (%d)\n",
1780 task->tk_pid, -task->tk_status);
1783 rpc_exit(task, status);
1784 return;
1786 retry_timeout:
1787 task->tk_status = 0;
1788 task->tk_action = call_timeout;
1792 * 4b. Connect to the RPC server
1794 static void
1795 call_connect(struct rpc_task *task)
1797 struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
1799 dprintk("RPC: %5u call_connect xprt %p %s connected\n",
1800 task->tk_pid, xprt,
1801 (xprt_connected(xprt) ? "is" : "is not"));
1803 task->tk_action = call_transmit;
1804 if (!xprt_connected(xprt)) {
1805 task->tk_action = call_connect_status;
1806 if (task->tk_status < 0)
1807 return;
1808 if (task->tk_flags & RPC_TASK_NOCONNECT) {
1809 rpc_exit(task, -ENOTCONN);
1810 return;
1812 xprt_connect(task);
1817 * 4c. Sort out connect result
1819 static void
1820 call_connect_status(struct rpc_task *task)
1822 struct rpc_clnt *clnt = task->tk_client;
1823 int status = task->tk_status;
1825 dprint_status(task);
1827 trace_rpc_connect_status(task, status);
1828 task->tk_status = 0;
1829 switch (status) {
1830 case -ECONNREFUSED:
1831 case -ECONNRESET:
1832 case -ECONNABORTED:
1833 case -ENETUNREACH:
1834 case -EHOSTUNREACH:
1835 case -EADDRINUSE:
1836 case -ENOBUFS:
1837 case -EPIPE:
1838 if (RPC_IS_SOFTCONN(task))
1839 break;
1840 /* retry with existing socket, after a delay */
1841 rpc_delay(task, 3*HZ);
1842 case -EAGAIN:
1843 /* Check for timeouts before looping back to call_bind */
1844 case -ETIMEDOUT:
1845 task->tk_action = call_timeout;
1846 return;
1847 case 0:
1848 clnt->cl_stats->netreconn++;
1849 task->tk_action = call_transmit;
1850 return;
1852 rpc_exit(task, status);
1856 * 5. Transmit the RPC request, and wait for reply
1858 static void
1859 call_transmit(struct rpc_task *task)
1861 int is_retrans = RPC_WAS_SENT(task);
1863 dprint_status(task);
1865 task->tk_action = call_status;
1866 if (task->tk_status < 0)
1867 return;
1868 if (!xprt_prepare_transmit(task))
1869 return;
1870 task->tk_action = call_transmit_status;
1871 /* Encode here so that rpcsec_gss can use correct sequence number. */
1872 if (rpc_task_need_encode(task)) {
1873 rpc_xdr_encode(task);
1874 /* Did the encode result in an error condition? */
1875 if (task->tk_status != 0) {
1876 /* Was the error nonfatal? */
1877 if (task->tk_status == -EAGAIN)
1878 rpc_delay(task, HZ >> 4);
1879 else
1880 rpc_exit(task, task->tk_status);
1881 return;
1884 xprt_transmit(task);
1885 if (task->tk_status < 0)
1886 return;
1887 if (is_retrans)
1888 task->tk_client->cl_stats->rpcretrans++;
1890 * On success, ensure that we call xprt_end_transmit() before sleeping
1891 * in order to allow access to the socket to other RPC requests.
1893 call_transmit_status(task);
1894 if (rpc_reply_expected(task))
1895 return;
1896 task->tk_action = rpc_exit_task;
1897 rpc_wake_up_queued_task(&task->tk_rqstp->rq_xprt->pending, task);
1901 * 5a. Handle cleanup after a transmission
1903 static void
1904 call_transmit_status(struct rpc_task *task)
1906 task->tk_action = call_status;
1909 * Common case: success. Force the compiler to put this
1910 * test first.
1912 if (task->tk_status == 0) {
1913 xprt_end_transmit(task);
1914 rpc_task_force_reencode(task);
1915 return;
1918 switch (task->tk_status) {
1919 case -EAGAIN:
1920 break;
1921 default:
1922 dprint_status(task);
1923 xprt_end_transmit(task);
1924 rpc_task_force_reencode(task);
1925 break;
1927 * Special cases: if we've been waiting on the
1928 * socket's write_space() callback, or if the
1929 * socket just returned a connection error,
1930 * then hold onto the transport lock.
1932 case -ECONNREFUSED:
1933 case -EHOSTDOWN:
1934 case -EHOSTUNREACH:
1935 case -ENETUNREACH:
1936 case -EPERM:
1937 if (RPC_IS_SOFTCONN(task)) {
1938 xprt_end_transmit(task);
1939 rpc_exit(task, task->tk_status);
1940 break;
1942 case -ECONNRESET:
1943 case -ECONNABORTED:
1944 case -EADDRINUSE:
1945 case -ENOTCONN:
1946 case -ENOBUFS:
1947 case -EPIPE:
1948 rpc_task_force_reencode(task);
1952 #if defined(CONFIG_SUNRPC_BACKCHANNEL)
1954 * 5b. Send the backchannel RPC reply. On error, drop the reply. In
1955 * addition, disconnect on connectivity errors.
1957 static void
1958 call_bc_transmit(struct rpc_task *task)
1960 struct rpc_rqst *req = task->tk_rqstp;
1962 if (!xprt_prepare_transmit(task)) {
1964 * Could not reserve the transport. Try again after the
1965 * transport is released.
1967 task->tk_status = 0;
1968 task->tk_action = call_bc_transmit;
1969 return;
1972 task->tk_action = rpc_exit_task;
1973 if (task->tk_status < 0) {
1974 printk(KERN_NOTICE "RPC: Could not send backchannel reply "
1975 "error: %d\n", task->tk_status);
1976 return;
1979 xprt_transmit(task);
1980 xprt_end_transmit(task);
1981 dprint_status(task);
1982 switch (task->tk_status) {
1983 case 0:
1984 /* Success */
1985 break;
1986 case -EHOSTDOWN:
1987 case -EHOSTUNREACH:
1988 case -ENETUNREACH:
1989 case -ETIMEDOUT:
1991 * Problem reaching the server. Disconnect and let the
1992 * forechannel reestablish the connection. The server will
1993 * have to retransmit the backchannel request and we'll
1994 * reprocess it. Since these ops are idempotent, there's no
1995 * need to cache our reply at this time.
1997 printk(KERN_NOTICE "RPC: Could not send backchannel reply "
1998 "error: %d\n", task->tk_status);
1999 xprt_conditional_disconnect(req->rq_xprt,
2000 req->rq_connect_cookie);
2001 break;
2002 default:
2004 * We were unable to reply and will have to drop the
2005 * request. The server should reconnect and retransmit.
2007 WARN_ON_ONCE(task->tk_status == -EAGAIN);
2008 printk(KERN_NOTICE "RPC: Could not send backchannel reply "
2009 "error: %d\n", task->tk_status);
2010 break;
2012 rpc_wake_up_queued_task(&req->rq_xprt->pending, task);
2014 #endif /* CONFIG_SUNRPC_BACKCHANNEL */
2017 * 6. Sort out the RPC call status
2019 static void
2020 call_status(struct rpc_task *task)
2022 struct rpc_clnt *clnt = task->tk_client;
2023 struct rpc_rqst *req = task->tk_rqstp;
2024 int status;
2026 if (req->rq_reply_bytes_recvd > 0 && !req->rq_bytes_sent)
2027 task->tk_status = req->rq_reply_bytes_recvd;
2029 dprint_status(task);
2031 status = task->tk_status;
2032 if (status >= 0) {
2033 task->tk_action = call_decode;
2034 return;
2037 trace_rpc_call_status(task);
2038 task->tk_status = 0;
2039 switch(status) {
2040 case -EHOSTDOWN:
2041 case -EHOSTUNREACH:
2042 case -ENETUNREACH:
2043 case -EPERM:
2044 if (RPC_IS_SOFTCONN(task)) {
2045 rpc_exit(task, status);
2046 break;
2049 * Delay any retries for 3 seconds, then handle as if it
2050 * were a timeout.
2052 rpc_delay(task, 3*HZ);
2053 case -ETIMEDOUT:
2054 task->tk_action = call_timeout;
2055 if (!(task->tk_flags & RPC_TASK_NO_RETRANS_TIMEOUT)
2056 && task->tk_client->cl_discrtry)
2057 xprt_conditional_disconnect(req->rq_xprt,
2058 req->rq_connect_cookie);
2059 break;
2060 case -ECONNREFUSED:
2061 case -ECONNRESET:
2062 case -ECONNABORTED:
2063 rpc_force_rebind(clnt);
2064 case -EADDRINUSE:
2065 case -ENOBUFS:
2066 rpc_delay(task, 3*HZ);
2067 case -EPIPE:
2068 case -ENOTCONN:
2069 task->tk_action = call_bind;
2070 break;
2071 case -EAGAIN:
2072 task->tk_action = call_transmit;
2073 break;
2074 case -EIO:
2075 /* shutdown or soft timeout */
2076 rpc_exit(task, status);
2077 break;
2078 default:
2079 if (clnt->cl_chatty)
2080 printk("%s: RPC call returned error %d\n",
2081 clnt->cl_program->name, -status);
2082 rpc_exit(task, status);
2087 * 6a. Handle RPC timeout
2088 * We do not release the request slot, so we keep using the
2089 * same XID for all retransmits.
2091 static void
2092 call_timeout(struct rpc_task *task)
2094 struct rpc_clnt *clnt = task->tk_client;
2096 if (xprt_adjust_timeout(task->tk_rqstp) == 0) {
2097 dprintk("RPC: %5u call_timeout (minor)\n", task->tk_pid);
2098 goto retry;
2101 dprintk("RPC: %5u call_timeout (major)\n", task->tk_pid);
2102 task->tk_timeouts++;
2104 if (RPC_IS_SOFTCONN(task)) {
2105 rpc_exit(task, -ETIMEDOUT);
2106 return;
2108 if (RPC_IS_SOFT(task)) {
2109 if (clnt->cl_chatty) {
2110 rcu_read_lock();
2111 printk(KERN_NOTICE "%s: server %s not responding, timed out\n",
2112 clnt->cl_program->name,
2113 rcu_dereference(clnt->cl_xprt)->servername);
2114 rcu_read_unlock();
2116 if (task->tk_flags & RPC_TASK_TIMEOUT)
2117 rpc_exit(task, -ETIMEDOUT);
2118 else
2119 rpc_exit(task, -EIO);
2120 return;
2123 if (!(task->tk_flags & RPC_CALL_MAJORSEEN)) {
2124 task->tk_flags |= RPC_CALL_MAJORSEEN;
2125 if (clnt->cl_chatty) {
2126 rcu_read_lock();
2127 printk(KERN_NOTICE "%s: server %s not responding, still trying\n",
2128 clnt->cl_program->name,
2129 rcu_dereference(clnt->cl_xprt)->servername);
2130 rcu_read_unlock();
2133 rpc_force_rebind(clnt);
2135 * Did our request time out due to an RPCSEC_GSS out-of-sequence
2136 * event? RFC2203 requires the server to drop all such requests.
2138 rpcauth_invalcred(task);
2140 retry:
2141 task->tk_action = call_bind;
2142 task->tk_status = 0;
2146 * 7. Decode the RPC reply
2148 static void
2149 call_decode(struct rpc_task *task)
2151 struct rpc_clnt *clnt = task->tk_client;
2152 struct rpc_rqst *req = task->tk_rqstp;
2153 kxdrdproc_t decode = task->tk_msg.rpc_proc->p_decode;
2154 __be32 *p;
2156 dprint_status(task);
2158 if (task->tk_flags & RPC_CALL_MAJORSEEN) {
2159 if (clnt->cl_chatty) {
2160 rcu_read_lock();
2161 printk(KERN_NOTICE "%s: server %s OK\n",
2162 clnt->cl_program->name,
2163 rcu_dereference(clnt->cl_xprt)->servername);
2164 rcu_read_unlock();
2166 task->tk_flags &= ~RPC_CALL_MAJORSEEN;
2170 * Ensure that we see all writes made by xprt_complete_rqst()
2171 * before it changed req->rq_reply_bytes_recvd.
2173 smp_rmb();
2174 req->rq_rcv_buf.len = req->rq_private_buf.len;
2176 /* Check that the softirq receive buffer is valid */
2177 WARN_ON(memcmp(&req->rq_rcv_buf, &req->rq_private_buf,
2178 sizeof(req->rq_rcv_buf)) != 0);
2180 if (req->rq_rcv_buf.len < 12) {
2181 if (!RPC_IS_SOFT(task)) {
2182 task->tk_action = call_bind;
2183 goto out_retry;
2185 dprintk("RPC: %s: too small RPC reply size (%d bytes)\n",
2186 clnt->cl_program->name, task->tk_status);
2187 task->tk_action = call_timeout;
2188 goto out_retry;
2191 p = rpc_verify_header(task);
2192 if (IS_ERR(p)) {
2193 if (p == ERR_PTR(-EAGAIN))
2194 goto out_retry;
2195 return;
2198 task->tk_action = rpc_exit_task;
2200 if (decode) {
2201 task->tk_status = rpcauth_unwrap_resp(task, decode, req, p,
2202 task->tk_msg.rpc_resp);
2204 dprintk("RPC: %5u call_decode result %d\n", task->tk_pid,
2205 task->tk_status);
2206 return;
2207 out_retry:
2208 task->tk_status = 0;
2209 /* Note: rpc_verify_header() may have freed the RPC slot */
2210 if (task->tk_rqstp == req) {
2211 req->rq_reply_bytes_recvd = req->rq_rcv_buf.len = 0;
2212 if (task->tk_client->cl_discrtry)
2213 xprt_conditional_disconnect(req->rq_xprt,
2214 req->rq_connect_cookie);
2218 static __be32 *
2219 rpc_encode_header(struct rpc_task *task)
2221 struct rpc_clnt *clnt = task->tk_client;
2222 struct rpc_rqst *req = task->tk_rqstp;
2223 __be32 *p = req->rq_svec[0].iov_base;
2225 /* FIXME: check buffer size? */
2227 p = xprt_skip_transport_header(req->rq_xprt, p);
2228 *p++ = req->rq_xid; /* XID */
2229 *p++ = htonl(RPC_CALL); /* CALL */
2230 *p++ = htonl(RPC_VERSION); /* RPC version */
2231 *p++ = htonl(clnt->cl_prog); /* program number */
2232 *p++ = htonl(clnt->cl_vers); /* program version */
2233 *p++ = htonl(task->tk_msg.rpc_proc->p_proc); /* procedure */
2234 p = rpcauth_marshcred(task, p);
2235 req->rq_slen = xdr_adjust_iovec(&req->rq_svec[0], p);
2236 return p;
2239 static __be32 *
2240 rpc_verify_header(struct rpc_task *task)
2242 struct rpc_clnt *clnt = task->tk_client;
2243 struct kvec *iov = &task->tk_rqstp->rq_rcv_buf.head[0];
2244 int len = task->tk_rqstp->rq_rcv_buf.len >> 2;
2245 __be32 *p = iov->iov_base;
2246 u32 n;
2247 int error = -EACCES;
2249 if ((task->tk_rqstp->rq_rcv_buf.len & 3) != 0) {
2250 /* RFC-1014 says that the representation of XDR data must be a
2251 * multiple of four bytes
2252 * - if it isn't pointer subtraction in the NFS client may give
2253 * undefined results
2255 dprintk("RPC: %5u %s: XDR representation not a multiple of"
2256 " 4 bytes: 0x%x\n", task->tk_pid, __func__,
2257 task->tk_rqstp->rq_rcv_buf.len);
2258 error = -EIO;
2259 goto out_err;
2261 if ((len -= 3) < 0)
2262 goto out_overflow;
2264 p += 1; /* skip XID */
2265 if ((n = ntohl(*p++)) != RPC_REPLY) {
2266 dprintk("RPC: %5u %s: not an RPC reply: %x\n",
2267 task->tk_pid, __func__, n);
2268 error = -EIO;
2269 goto out_garbage;
2272 if ((n = ntohl(*p++)) != RPC_MSG_ACCEPTED) {
2273 if (--len < 0)
2274 goto out_overflow;
2275 switch ((n = ntohl(*p++))) {
2276 case RPC_AUTH_ERROR:
2277 break;
2278 case RPC_MISMATCH:
2279 dprintk("RPC: %5u %s: RPC call version mismatch!\n",
2280 task->tk_pid, __func__);
2281 error = -EPROTONOSUPPORT;
2282 goto out_err;
2283 default:
2284 dprintk("RPC: %5u %s: RPC call rejected, "
2285 "unknown error: %x\n",
2286 task->tk_pid, __func__, n);
2287 error = -EIO;
2288 goto out_err;
2290 if (--len < 0)
2291 goto out_overflow;
2292 switch ((n = ntohl(*p++))) {
2293 case RPC_AUTH_REJECTEDCRED:
2294 case RPC_AUTH_REJECTEDVERF:
2295 case RPCSEC_GSS_CREDPROBLEM:
2296 case RPCSEC_GSS_CTXPROBLEM:
2297 if (!task->tk_cred_retry)
2298 break;
2299 task->tk_cred_retry--;
2300 dprintk("RPC: %5u %s: retry stale creds\n",
2301 task->tk_pid, __func__);
2302 rpcauth_invalcred(task);
2303 /* Ensure we obtain a new XID! */
2304 xprt_release(task);
2305 task->tk_action = call_reserve;
2306 goto out_retry;
2307 case RPC_AUTH_BADCRED:
2308 case RPC_AUTH_BADVERF:
2309 /* possibly garbled cred/verf? */
2310 if (!task->tk_garb_retry)
2311 break;
2312 task->tk_garb_retry--;
2313 dprintk("RPC: %5u %s: retry garbled creds\n",
2314 task->tk_pid, __func__);
2315 task->tk_action = call_bind;
2316 goto out_retry;
2317 case RPC_AUTH_TOOWEAK:
2318 rcu_read_lock();
2319 printk(KERN_NOTICE "RPC: server %s requires stronger "
2320 "authentication.\n",
2321 rcu_dereference(clnt->cl_xprt)->servername);
2322 rcu_read_unlock();
2323 break;
2324 default:
2325 dprintk("RPC: %5u %s: unknown auth error: %x\n",
2326 task->tk_pid, __func__, n);
2327 error = -EIO;
2329 dprintk("RPC: %5u %s: call rejected %d\n",
2330 task->tk_pid, __func__, n);
2331 goto out_err;
2333 p = rpcauth_checkverf(task, p);
2334 if (IS_ERR(p)) {
2335 error = PTR_ERR(p);
2336 dprintk("RPC: %5u %s: auth check failed with %d\n",
2337 task->tk_pid, __func__, error);
2338 goto out_garbage; /* bad verifier, retry */
2340 len = p - (__be32 *)iov->iov_base - 1;
2341 if (len < 0)
2342 goto out_overflow;
2343 switch ((n = ntohl(*p++))) {
2344 case RPC_SUCCESS:
2345 return p;
2346 case RPC_PROG_UNAVAIL:
2347 dprintk_rcu("RPC: %5u %s: program %u is unsupported "
2348 "by server %s\n", task->tk_pid, __func__,
2349 (unsigned int)clnt->cl_prog,
2350 rcu_dereference(clnt->cl_xprt)->servername);
2351 error = -EPFNOSUPPORT;
2352 goto out_err;
2353 case RPC_PROG_MISMATCH:
2354 dprintk_rcu("RPC: %5u %s: program %u, version %u unsupported "
2355 "by server %s\n", task->tk_pid, __func__,
2356 (unsigned int)clnt->cl_prog,
2357 (unsigned int)clnt->cl_vers,
2358 rcu_dereference(clnt->cl_xprt)->servername);
2359 error = -EPROTONOSUPPORT;
2360 goto out_err;
2361 case RPC_PROC_UNAVAIL:
2362 dprintk_rcu("RPC: %5u %s: proc %s unsupported by program %u, "
2363 "version %u on server %s\n",
2364 task->tk_pid, __func__,
2365 rpc_proc_name(task),
2366 clnt->cl_prog, clnt->cl_vers,
2367 rcu_dereference(clnt->cl_xprt)->servername);
2368 error = -EOPNOTSUPP;
2369 goto out_err;
2370 case RPC_GARBAGE_ARGS:
2371 dprintk("RPC: %5u %s: server saw garbage\n",
2372 task->tk_pid, __func__);
2373 break; /* retry */
2374 default:
2375 dprintk("RPC: %5u %s: server accept status: %x\n",
2376 task->tk_pid, __func__, n);
2377 /* Also retry */
2380 out_garbage:
2381 clnt->cl_stats->rpcgarbage++;
2382 if (task->tk_garb_retry) {
2383 task->tk_garb_retry--;
2384 dprintk("RPC: %5u %s: retrying\n",
2385 task->tk_pid, __func__);
2386 task->tk_action = call_bind;
2387 out_retry:
2388 return ERR_PTR(-EAGAIN);
2390 out_err:
2391 rpc_exit(task, error);
2392 dprintk("RPC: %5u %s: call failed with error %d\n", task->tk_pid,
2393 __func__, error);
2394 return ERR_PTR(error);
2395 out_overflow:
2396 dprintk("RPC: %5u %s: server reply was truncated.\n", task->tk_pid,
2397 __func__);
2398 goto out_garbage;
2401 static void rpcproc_encode_null(void *rqstp, struct xdr_stream *xdr, void *obj)
2405 static int rpcproc_decode_null(void *rqstp, struct xdr_stream *xdr, void *obj)
2407 return 0;
2410 static struct rpc_procinfo rpcproc_null = {
2411 .p_encode = rpcproc_encode_null,
2412 .p_decode = rpcproc_decode_null,
2415 static int rpc_ping(struct rpc_clnt *clnt)
2417 struct rpc_message msg = {
2418 .rpc_proc = &rpcproc_null,
2420 int err;
2421 msg.rpc_cred = authnull_ops.lookup_cred(NULL, NULL, 0);
2422 err = rpc_call_sync(clnt, &msg, RPC_TASK_SOFT | RPC_TASK_SOFTCONN);
2423 put_rpccred(msg.rpc_cred);
2424 return err;
2427 struct rpc_task *rpc_call_null(struct rpc_clnt *clnt, struct rpc_cred *cred, int flags)
2429 struct rpc_message msg = {
2430 .rpc_proc = &rpcproc_null,
2431 .rpc_cred = cred,
2433 struct rpc_task_setup task_setup_data = {
2434 .rpc_client = clnt,
2435 .rpc_message = &msg,
2436 .callback_ops = &rpc_default_ops,
2437 .flags = flags,
2439 return rpc_run_task(&task_setup_data);
2441 EXPORT_SYMBOL_GPL(rpc_call_null);
2443 #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
2444 static void rpc_show_header(void)
2446 printk(KERN_INFO "-pid- flgs status -client- --rqstp- "
2447 "-timeout ---ops--\n");
2450 static void rpc_show_task(const struct rpc_clnt *clnt,
2451 const struct rpc_task *task)
2453 const char *rpc_waitq = "none";
2455 if (RPC_IS_QUEUED(task))
2456 rpc_waitq = rpc_qname(task->tk_waitqueue);
2458 printk(KERN_INFO "%5u %04x %6d %8p %8p %8ld %8p %sv%u %s a:%ps q:%s\n",
2459 task->tk_pid, task->tk_flags, task->tk_status,
2460 clnt, task->tk_rqstp, task->tk_timeout, task->tk_ops,
2461 clnt->cl_program->name, clnt->cl_vers, rpc_proc_name(task),
2462 task->tk_action, rpc_waitq);
2465 void rpc_show_tasks(struct net *net)
2467 struct rpc_clnt *clnt;
2468 struct rpc_task *task;
2469 int header = 0;
2470 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
2472 spin_lock(&sn->rpc_client_lock);
2473 list_for_each_entry(clnt, &sn->all_clients, cl_clients) {
2474 spin_lock(&clnt->cl_lock);
2475 list_for_each_entry(task, &clnt->cl_tasks, tk_task) {
2476 if (!header) {
2477 rpc_show_header();
2478 header++;
2480 rpc_show_task(clnt, task);
2482 spin_unlock(&clnt->cl_lock);
2484 spin_unlock(&sn->rpc_client_lock);
2486 #endif