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>
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>
32 #include <linux/in6.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>
45 #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
46 # define RPCDBG_FACILITY RPCDBG_CALL
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
);
115 __rpc_clnt_remove_pipedir(clnt
);
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
;
126 struct dentry
*dir
, *dentry
;
128 dir
= rpc_d_lookup_sb(sb
, dir_name
);
130 pr_info("RPC: pipefs directory doesn't exist: %s\n", dir_name
);
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
);
139 if (dentry
== ERR_PTR(-EEXIST
))
141 printk(KERN_INFO
"RPC: Couldn't create pipefs entry"
142 " %s/%s, error %ld\n",
143 dir_name
, name
, PTR_ERR(dentry
));
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
);
158 return PTR_ERR(dentry
);
163 static int rpc_clnt_skip_event(struct rpc_clnt
*clnt
, unsigned long event
)
165 if (clnt
->cl_program
->pipe_dir_name
== NULL
)
169 case RPC_PIPEFS_MOUNT
:
170 if (clnt
->cl_pipedir_objects
.pdh_dentry
!= NULL
)
172 if (atomic_read(&clnt
->cl_count
) == 0)
175 case RPC_PIPEFS_UMOUNT
:
176 if (clnt
->cl_pipedir_objects
.pdh_dentry
== NULL
)
183 static int __rpc_clnt_handle_event(struct rpc_clnt
*clnt
, unsigned long event
,
184 struct super_block
*sb
)
186 struct dentry
*dentry
;
189 case RPC_PIPEFS_MOUNT
:
190 dentry
= rpc_setup_pipedir_sb(sb
, clnt
);
194 return PTR_ERR(dentry
);
196 case RPC_PIPEFS_UMOUNT
:
197 __rpc_clnt_remove_pipedir(clnt
);
200 printk(KERN_ERR
"%s: unknown event: %ld\n", __func__
, event
);
206 static int __rpc_pipefs_event(struct rpc_clnt
*clnt
, unsigned long event
,
207 struct super_block
*sb
)
211 for (;; clnt
= clnt
->cl_parent
) {
212 if (!rpc_clnt_skip_event(clnt
, event
))
213 error
= __rpc_clnt_handle_event(clnt
, event
, sb
);
214 if (error
|| clnt
== clnt
->cl_parent
)
220 static struct rpc_clnt
*rpc_get_client_for_event(struct net
*net
, int event
)
222 struct sunrpc_net
*sn
= net_generic(net
, sunrpc_net_id
);
223 struct rpc_clnt
*clnt
;
225 spin_lock(&sn
->rpc_client_lock
);
226 list_for_each_entry(clnt
, &sn
->all_clients
, cl_clients
) {
227 if (rpc_clnt_skip_event(clnt
, event
))
229 spin_unlock(&sn
->rpc_client_lock
);
232 spin_unlock(&sn
->rpc_client_lock
);
236 static int rpc_pipefs_event(struct notifier_block
*nb
, unsigned long event
,
239 struct super_block
*sb
= ptr
;
240 struct rpc_clnt
*clnt
;
243 while ((clnt
= rpc_get_client_for_event(sb
->s_fs_info
, event
))) {
244 error
= __rpc_pipefs_event(clnt
, event
, sb
);
251 static struct notifier_block rpc_clients_block
= {
252 .notifier_call
= rpc_pipefs_event
,
253 .priority
= SUNRPC_PIPEFS_RPC_PRIO
,
256 int rpc_clients_notifier_register(void)
258 return rpc_pipefs_notifier_register(&rpc_clients_block
);
261 void rpc_clients_notifier_unregister(void)
263 return rpc_pipefs_notifier_unregister(&rpc_clients_block
);
266 static struct rpc_xprt
*rpc_clnt_set_transport(struct rpc_clnt
*clnt
,
267 struct rpc_xprt
*xprt
,
268 const struct rpc_timeout
*timeout
)
270 struct rpc_xprt
*old
;
272 spin_lock(&clnt
->cl_lock
);
273 old
= rcu_dereference_protected(clnt
->cl_xprt
,
274 lockdep_is_held(&clnt
->cl_lock
));
276 if (!xprt_bound(xprt
))
277 clnt
->cl_autobind
= 1;
279 clnt
->cl_timeout
= timeout
;
280 rcu_assign_pointer(clnt
->cl_xprt
, xprt
);
281 spin_unlock(&clnt
->cl_lock
);
286 static void rpc_clnt_set_nodename(struct rpc_clnt
*clnt
, const char *nodename
)
288 clnt
->cl_nodelen
= strlcpy(clnt
->cl_nodename
,
289 nodename
, sizeof(clnt
->cl_nodename
));
292 static int rpc_client_register(struct rpc_clnt
*clnt
,
293 rpc_authflavor_t pseudoflavor
,
294 const char *client_name
)
296 struct rpc_auth_create_args auth_args
= {
297 .pseudoflavor
= pseudoflavor
,
298 .target_name
= client_name
,
300 struct rpc_auth
*auth
;
301 struct net
*net
= rpc_net_ns(clnt
);
302 struct super_block
*pipefs_sb
;
305 rpc_clnt_debugfs_register(clnt
);
307 pipefs_sb
= rpc_get_sb_net(net
);
309 err
= rpc_setup_pipedir(pipefs_sb
, clnt
);
314 rpc_register_client(clnt
);
318 auth
= rpcauth_create(&auth_args
, clnt
);
320 dprintk("RPC: Couldn't create auth handle (flavor %u)\n",
327 pipefs_sb
= rpc_get_sb_net(net
);
328 rpc_unregister_client(clnt
);
329 __rpc_clnt_remove_pipedir(clnt
);
333 rpc_clnt_debugfs_unregister(clnt
);
337 static DEFINE_IDA(rpc_clids
);
339 static int rpc_alloc_clid(struct rpc_clnt
*clnt
)
343 clid
= ida_simple_get(&rpc_clids
, 0, 0, GFP_KERNEL
);
346 clnt
->cl_clid
= clid
;
350 static void rpc_free_clid(struct rpc_clnt
*clnt
)
352 ida_simple_remove(&rpc_clids
, clnt
->cl_clid
);
355 static struct rpc_clnt
* rpc_new_client(const struct rpc_create_args
*args
,
356 struct rpc_xprt_switch
*xps
,
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
;
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
);
376 if (args
->version
>= program
->nrvers
)
378 version
= program
->version
[args
->version
];
383 clnt
= kzalloc(sizeof(*clnt
), GFP_KERNEL
);
386 clnt
->cl_parent
= parent
? : clnt
;
388 err
= rpc_alloc_clid(clnt
);
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
);
400 if (clnt
->cl_metrics
== NULL
)
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
);
414 xprt_iter_init(&clnt
->cl_xpi
, xps
);
415 xprt_switch_put(xps
);
417 clnt
->cl_rtt
= &clnt
->cl_rtt_default
;
418 rpc_init_rtt(&clnt
->cl_rtt_default
, clnt
->cl_timeout
->to_initval
);
420 atomic_set(&clnt
->cl_count
, 1);
422 if (nodename
== NULL
)
423 nodename
= utsname()->nodename
;
424 /* save the nodename */
425 rpc_clnt_set_nodename(clnt
, nodename
);
427 err
= rpc_client_register(clnt
, args
->authflavor
, args
->client_name
);
431 atomic_inc(&parent
->cl_count
);
435 rpc_free_iostats(clnt
->cl_metrics
);
443 xprt_switch_put(xps
);
448 static struct rpc_clnt
*rpc_create_xprt(struct rpc_create_args
*args
,
449 struct rpc_xprt
*xprt
)
451 struct rpc_clnt
*clnt
= NULL
;
452 struct rpc_xprt_switch
*xps
;
454 if (args
->bc_xprt
&& args
->bc_xprt
->xpt_bc_xps
) {
455 WARN_ON_ONCE(!(args
->protocol
& XPRT_TRANSPORT_BC
));
456 xps
= args
->bc_xprt
->xpt_bc_xps
;
457 xprt_switch_get(xps
);
459 xps
= xprt_switch_alloc(xprt
, GFP_KERNEL
);
462 return ERR_PTR(-ENOMEM
);
465 xprt_switch_get(xps
);
466 xprt
->bc_xprt
->xpt_bc_xps
= xps
;
469 clnt
= rpc_new_client(args
, xps
, xprt
, NULL
);
473 if (!(args
->flags
& RPC_CLNT_CREATE_NOPING
)) {
474 int err
= rpc_ping(clnt
);
476 rpc_shutdown_client(clnt
);
481 clnt
->cl_softrtry
= 1;
482 if (args
->flags
& RPC_CLNT_CREATE_HARDRTRY
)
483 clnt
->cl_softrtry
= 0;
485 if (args
->flags
& RPC_CLNT_CREATE_AUTOBIND
)
486 clnt
->cl_autobind
= 1;
487 if (args
->flags
& RPC_CLNT_CREATE_NO_RETRANS_TIMEOUT
)
488 clnt
->cl_noretranstimeo
= 1;
489 if (args
->flags
& RPC_CLNT_CREATE_DISCRTRY
)
490 clnt
->cl_discrtry
= 1;
491 if (!(args
->flags
& RPC_CLNT_CREATE_QUIET
))
498 * rpc_create - create an RPC client and transport with one call
499 * @args: rpc_clnt create argument structure
501 * Creates and initializes an RPC transport and an RPC client.
503 * It can ping the server in order to determine if it is up, and to see if
504 * it supports this program and version. RPC_CLNT_CREATE_NOPING disables
505 * this behavior so asynchronous tasks can also use rpc_create.
507 struct rpc_clnt
*rpc_create(struct rpc_create_args
*args
)
509 struct rpc_xprt
*xprt
;
510 struct xprt_create xprtargs
= {
512 .ident
= args
->protocol
,
513 .srcaddr
= args
->saddress
,
514 .dstaddr
= args
->address
,
515 .addrlen
= args
->addrsize
,
516 .servername
= args
->servername
,
517 .bc_xprt
= args
->bc_xprt
,
522 WARN_ON_ONCE(!(args
->protocol
& XPRT_TRANSPORT_BC
));
523 xprt
= args
->bc_xprt
->xpt_bc_xprt
;
526 return rpc_create_xprt(args
, xprt
);
530 if (args
->flags
& RPC_CLNT_CREATE_INFINITE_SLOTS
)
531 xprtargs
.flags
|= XPRT_CREATE_INFINITE_SLOTS
;
532 if (args
->flags
& RPC_CLNT_CREATE_NO_IDLE_TIMEOUT
)
533 xprtargs
.flags
|= XPRT_CREATE_NO_IDLE_TIMEOUT
;
535 * If the caller chooses not to specify a hostname, whip
536 * up a string representation of the passed-in address.
538 if (xprtargs
.servername
== NULL
) {
539 struct sockaddr_un
*sun
=
540 (struct sockaddr_un
*)args
->address
;
541 struct sockaddr_in
*sin
=
542 (struct sockaddr_in
*)args
->address
;
543 struct sockaddr_in6
*sin6
=
544 (struct sockaddr_in6
*)args
->address
;
546 servername
[0] = '\0';
547 switch (args
->address
->sa_family
) {
549 snprintf(servername
, sizeof(servername
), "%s",
553 snprintf(servername
, sizeof(servername
), "%pI4",
554 &sin
->sin_addr
.s_addr
);
557 snprintf(servername
, sizeof(servername
), "%pI6",
561 /* caller wants default server name, but
562 * address family isn't recognized. */
563 return ERR_PTR(-EINVAL
);
565 xprtargs
.servername
= servername
;
568 xprt
= xprt_create_transport(&xprtargs
);
570 return (struct rpc_clnt
*)xprt
;
573 * By default, kernel RPC client connects from a reserved port.
574 * CAP_NET_BIND_SERVICE will not be set for unprivileged requesters,
575 * but it is always enabled for rpciod, which handles the connect
579 if (args
->flags
& RPC_CLNT_CREATE_NONPRIVPORT
)
582 return rpc_create_xprt(args
, xprt
);
584 EXPORT_SYMBOL_GPL(rpc_create
);
587 * This function clones the RPC client structure. It allows us to share the
588 * same transport while varying parameters such as the authentication
591 static struct rpc_clnt
*__rpc_clone_client(struct rpc_create_args
*args
,
592 struct rpc_clnt
*clnt
)
594 struct rpc_xprt_switch
*xps
;
595 struct rpc_xprt
*xprt
;
596 struct rpc_clnt
*new;
601 xprt
= xprt_get(rcu_dereference(clnt
->cl_xprt
));
602 xps
= xprt_switch_get(rcu_dereference(clnt
->cl_xpi
.xpi_xpswitch
));
604 if (xprt
== NULL
|| xps
== NULL
) {
606 xprt_switch_put(xps
);
609 args
->servername
= xprt
->servername
;
610 args
->nodename
= clnt
->cl_nodename
;
612 new = rpc_new_client(args
, xps
, xprt
, clnt
);
618 /* Turn off autobind on clones */
619 new->cl_autobind
= 0;
620 new->cl_softrtry
= clnt
->cl_softrtry
;
621 new->cl_noretranstimeo
= clnt
->cl_noretranstimeo
;
622 new->cl_discrtry
= clnt
->cl_discrtry
;
623 new->cl_chatty
= clnt
->cl_chatty
;
627 dprintk("RPC: %s: returned error %d\n", __func__
, err
);
632 * rpc_clone_client - Clone an RPC client structure
634 * @clnt: RPC client whose parameters are copied
636 * Returns a fresh RPC client or an ERR_PTR.
638 struct rpc_clnt
*rpc_clone_client(struct rpc_clnt
*clnt
)
640 struct rpc_create_args args
= {
641 .program
= clnt
->cl_program
,
642 .prognumber
= clnt
->cl_prog
,
643 .version
= clnt
->cl_vers
,
644 .authflavor
= clnt
->cl_auth
->au_flavor
,
646 return __rpc_clone_client(&args
, clnt
);
648 EXPORT_SYMBOL_GPL(rpc_clone_client
);
651 * rpc_clone_client_set_auth - Clone an RPC client structure and set its auth
653 * @clnt: RPC client whose parameters are copied
654 * @flavor: security flavor for new client
656 * Returns a fresh RPC client or an ERR_PTR.
659 rpc_clone_client_set_auth(struct rpc_clnt
*clnt
, rpc_authflavor_t flavor
)
661 struct rpc_create_args args
= {
662 .program
= clnt
->cl_program
,
663 .prognumber
= clnt
->cl_prog
,
664 .version
= clnt
->cl_vers
,
665 .authflavor
= flavor
,
667 return __rpc_clone_client(&args
, clnt
);
669 EXPORT_SYMBOL_GPL(rpc_clone_client_set_auth
);
672 * rpc_switch_client_transport: switch the RPC transport on the fly
673 * @clnt: pointer to a struct rpc_clnt
674 * @args: pointer to the new transport arguments
675 * @timeout: pointer to the new timeout parameters
677 * This function allows the caller to switch the RPC transport for the
678 * rpc_clnt structure 'clnt' to allow it to connect to a mirrored NFS
679 * server, for instance. It assumes that the caller has ensured that
680 * there are no active RPC tasks by using some form of locking.
682 * Returns zero if "clnt" is now using the new xprt. Otherwise a
683 * negative errno is returned, and "clnt" continues to use the old
686 int rpc_switch_client_transport(struct rpc_clnt
*clnt
,
687 struct xprt_create
*args
,
688 const struct rpc_timeout
*timeout
)
690 const struct rpc_timeout
*old_timeo
;
691 rpc_authflavor_t pseudoflavor
;
692 struct rpc_xprt_switch
*xps
, *oldxps
;
693 struct rpc_xprt
*xprt
, *old
;
694 struct rpc_clnt
*parent
;
697 xprt
= xprt_create_transport(args
);
699 dprintk("RPC: failed to create new xprt for clnt %p\n",
701 return PTR_ERR(xprt
);
704 xps
= xprt_switch_alloc(xprt
, GFP_KERNEL
);
710 pseudoflavor
= clnt
->cl_auth
->au_flavor
;
712 old_timeo
= clnt
->cl_timeout
;
713 old
= rpc_clnt_set_transport(clnt
, xprt
, timeout
);
714 oldxps
= xprt_iter_xchg_switch(&clnt
->cl_xpi
, xps
);
716 rpc_unregister_client(clnt
);
717 __rpc_clnt_remove_pipedir(clnt
);
718 rpc_clnt_debugfs_unregister(clnt
);
721 * A new transport was created. "clnt" therefore
722 * becomes the root of a new cl_parent tree. clnt's
723 * children, if it has any, still point to the old xprt.
725 parent
= clnt
->cl_parent
;
726 clnt
->cl_parent
= clnt
;
729 * The old rpc_auth cache cannot be re-used. GSS
730 * contexts in particular are between a single
733 err
= rpc_client_register(clnt
, pseudoflavor
, NULL
);
739 rpc_release_client(parent
);
740 xprt_switch_put(oldxps
);
742 dprintk("RPC: replaced xprt for clnt %p\n", clnt
);
746 xps
= xprt_iter_xchg_switch(&clnt
->cl_xpi
, oldxps
);
747 rpc_clnt_set_transport(clnt
, old
, old_timeo
);
748 clnt
->cl_parent
= parent
;
749 rpc_client_register(clnt
, pseudoflavor
, NULL
);
750 xprt_switch_put(xps
);
752 dprintk("RPC: failed to switch xprt for clnt %p\n", clnt
);
755 EXPORT_SYMBOL_GPL(rpc_switch_client_transport
);
758 int rpc_clnt_xprt_iter_init(struct rpc_clnt
*clnt
, struct rpc_xprt_iter
*xpi
)
760 struct rpc_xprt_switch
*xps
;
763 xps
= xprt_switch_get(rcu_dereference(clnt
->cl_xpi
.xpi_xpswitch
));
767 xprt_iter_init_listall(xpi
, xps
);
768 xprt_switch_put(xps
);
773 * rpc_clnt_iterate_for_each_xprt - Apply a function to all transports
774 * @clnt: pointer to client
775 * @fn: function to apply
776 * @data: void pointer to function data
778 * Iterates through the list of RPC transports currently attached to the
779 * client and applies the function fn(clnt, xprt, data).
781 * On error, the iteration stops, and the function returns the error value.
783 int rpc_clnt_iterate_for_each_xprt(struct rpc_clnt
*clnt
,
784 int (*fn
)(struct rpc_clnt
*, struct rpc_xprt
*, void *),
787 struct rpc_xprt_iter xpi
;
790 ret
= rpc_clnt_xprt_iter_init(clnt
, &xpi
);
794 struct rpc_xprt
*xprt
= xprt_iter_get_next(&xpi
);
798 ret
= fn(clnt
, xprt
, data
);
803 xprt_iter_destroy(&xpi
);
806 EXPORT_SYMBOL_GPL(rpc_clnt_iterate_for_each_xprt
);
809 * Kill all tasks for the given client.
810 * XXX: kill their descendants as well?
812 void rpc_killall_tasks(struct rpc_clnt
*clnt
)
814 struct rpc_task
*rovr
;
817 if (list_empty(&clnt
->cl_tasks
))
819 dprintk("RPC: killing all tasks for client %p\n", clnt
);
821 * Spin lock all_tasks to prevent changes...
823 spin_lock(&clnt
->cl_lock
);
824 list_for_each_entry(rovr
, &clnt
->cl_tasks
, tk_task
) {
825 if (!RPC_IS_ACTIVATED(rovr
))
827 if (!(rovr
->tk_flags
& RPC_TASK_KILLED
)) {
828 rovr
->tk_flags
|= RPC_TASK_KILLED
;
829 rpc_exit(rovr
, -EIO
);
830 if (RPC_IS_QUEUED(rovr
))
831 rpc_wake_up_queued_task(rovr
->tk_waitqueue
,
835 spin_unlock(&clnt
->cl_lock
);
837 EXPORT_SYMBOL_GPL(rpc_killall_tasks
);
840 * Properly shut down an RPC client, terminating all outstanding
843 void rpc_shutdown_client(struct rpc_clnt
*clnt
)
847 dprintk_rcu("RPC: shutting down %s client for %s\n",
848 clnt
->cl_program
->name
,
849 rcu_dereference(clnt
->cl_xprt
)->servername
);
851 while (!list_empty(&clnt
->cl_tasks
)) {
852 rpc_killall_tasks(clnt
);
853 wait_event_timeout(destroy_wait
,
854 list_empty(&clnt
->cl_tasks
), 1*HZ
);
857 rpc_release_client(clnt
);
859 EXPORT_SYMBOL_GPL(rpc_shutdown_client
);
864 static struct rpc_clnt
*
865 rpc_free_client(struct rpc_clnt
*clnt
)
867 struct rpc_clnt
*parent
= NULL
;
869 dprintk_rcu("RPC: destroying %s client for %s\n",
870 clnt
->cl_program
->name
,
871 rcu_dereference(clnt
->cl_xprt
)->servername
);
872 if (clnt
->cl_parent
!= clnt
)
873 parent
= clnt
->cl_parent
;
874 rpc_clnt_debugfs_unregister(clnt
);
875 rpc_clnt_remove_pipedir(clnt
);
876 rpc_unregister_client(clnt
);
877 rpc_free_iostats(clnt
->cl_metrics
);
878 clnt
->cl_metrics
= NULL
;
879 xprt_put(rcu_dereference_raw(clnt
->cl_xprt
));
880 xprt_iter_destroy(&clnt
->cl_xpi
);
890 static struct rpc_clnt
*
891 rpc_free_auth(struct rpc_clnt
*clnt
)
893 if (clnt
->cl_auth
== NULL
)
894 return rpc_free_client(clnt
);
897 * Note: RPCSEC_GSS may need to send NULL RPC calls in order to
898 * release remaining GSS contexts. This mechanism ensures
899 * that it can do so safely.
901 atomic_inc(&clnt
->cl_count
);
902 rpcauth_release(clnt
->cl_auth
);
903 clnt
->cl_auth
= NULL
;
904 if (atomic_dec_and_test(&clnt
->cl_count
))
905 return rpc_free_client(clnt
);
910 * Release reference to the RPC client
913 rpc_release_client(struct rpc_clnt
*clnt
)
915 dprintk("RPC: rpc_release_client(%p)\n", clnt
);
918 if (list_empty(&clnt
->cl_tasks
))
919 wake_up(&destroy_wait
);
920 if (!atomic_dec_and_test(&clnt
->cl_count
))
922 clnt
= rpc_free_auth(clnt
);
923 } while (clnt
!= NULL
);
925 EXPORT_SYMBOL_GPL(rpc_release_client
);
928 * rpc_bind_new_program - bind a new RPC program to an existing client
929 * @old: old rpc_client
930 * @program: rpc program to set
931 * @vers: rpc program version
933 * Clones the rpc client and sets up a new RPC program. This is mainly
934 * of use for enabling different RPC programs to share the same transport.
935 * The Sun NFSv2/v3 ACL protocol can do this.
937 struct rpc_clnt
*rpc_bind_new_program(struct rpc_clnt
*old
,
938 const struct rpc_program
*program
,
941 struct rpc_create_args args
= {
943 .prognumber
= program
->number
,
945 .authflavor
= old
->cl_auth
->au_flavor
,
947 struct rpc_clnt
*clnt
;
950 clnt
= __rpc_clone_client(&args
, old
);
953 err
= rpc_ping(clnt
);
955 rpc_shutdown_client(clnt
);
961 EXPORT_SYMBOL_GPL(rpc_bind_new_program
);
963 void rpc_task_release_client(struct rpc_task
*task
)
965 struct rpc_clnt
*clnt
= task
->tk_client
;
966 struct rpc_xprt
*xprt
= task
->tk_xprt
;
969 /* Remove from client task list */
970 spin_lock(&clnt
->cl_lock
);
971 list_del(&task
->tk_task
);
972 spin_unlock(&clnt
->cl_lock
);
973 task
->tk_client
= NULL
;
975 rpc_release_client(clnt
);
979 task
->tk_xprt
= NULL
;
986 void rpc_task_set_client(struct rpc_task
*task
, struct rpc_clnt
*clnt
)
990 if (task
->tk_xprt
== NULL
)
991 task
->tk_xprt
= xprt_iter_get_next(&clnt
->cl_xpi
);
992 task
->tk_client
= clnt
;
993 atomic_inc(&clnt
->cl_count
);
994 if (clnt
->cl_softrtry
)
995 task
->tk_flags
|= RPC_TASK_SOFT
;
996 if (clnt
->cl_noretranstimeo
)
997 task
->tk_flags
|= RPC_TASK_NO_RETRANS_TIMEOUT
;
998 if (atomic_read(&clnt
->cl_swapper
))
999 task
->tk_flags
|= RPC_TASK_SWAPPER
;
1000 /* Add to the client's list of all tasks */
1001 spin_lock(&clnt
->cl_lock
);
1002 list_add_tail(&task
->tk_task
, &clnt
->cl_tasks
);
1003 spin_unlock(&clnt
->cl_lock
);
1008 rpc_task_set_rpc_message(struct rpc_task
*task
, const struct rpc_message
*msg
)
1011 task
->tk_msg
.rpc_proc
= msg
->rpc_proc
;
1012 task
->tk_msg
.rpc_argp
= msg
->rpc_argp
;
1013 task
->tk_msg
.rpc_resp
= msg
->rpc_resp
;
1014 if (msg
->rpc_cred
!= NULL
)
1015 task
->tk_msg
.rpc_cred
= get_rpccred(msg
->rpc_cred
);
1020 * Default callback for async RPC calls
1023 rpc_default_callback(struct rpc_task
*task
, void *data
)
1027 static const struct rpc_call_ops rpc_default_ops
= {
1028 .rpc_call_done
= rpc_default_callback
,
1032 * rpc_run_task - Allocate a new RPC task, then run rpc_execute against it
1033 * @task_setup_data: pointer to task initialisation data
1035 struct rpc_task
*rpc_run_task(const struct rpc_task_setup
*task_setup_data
)
1037 struct rpc_task
*task
;
1039 task
= rpc_new_task(task_setup_data
);
1043 rpc_task_set_client(task
, task_setup_data
->rpc_client
);
1044 rpc_task_set_rpc_message(task
, task_setup_data
->rpc_message
);
1046 if (task
->tk_action
== NULL
)
1047 rpc_call_start(task
);
1049 atomic_inc(&task
->tk_count
);
1054 EXPORT_SYMBOL_GPL(rpc_run_task
);
1057 * rpc_call_sync - Perform a synchronous RPC call
1058 * @clnt: pointer to RPC client
1059 * @msg: RPC call parameters
1060 * @flags: RPC call flags
1062 int rpc_call_sync(struct rpc_clnt
*clnt
, const struct rpc_message
*msg
, int flags
)
1064 struct rpc_task
*task
;
1065 struct rpc_task_setup task_setup_data
= {
1068 .callback_ops
= &rpc_default_ops
,
1073 WARN_ON_ONCE(flags
& RPC_TASK_ASYNC
);
1074 if (flags
& RPC_TASK_ASYNC
) {
1075 rpc_release_calldata(task_setup_data
.callback_ops
,
1076 task_setup_data
.callback_data
);
1080 task
= rpc_run_task(&task_setup_data
);
1082 return PTR_ERR(task
);
1083 status
= task
->tk_status
;
1087 EXPORT_SYMBOL_GPL(rpc_call_sync
);
1090 * rpc_call_async - Perform an asynchronous RPC call
1091 * @clnt: pointer to RPC client
1092 * @msg: RPC call parameters
1093 * @flags: RPC call flags
1094 * @tk_ops: RPC call ops
1095 * @data: user call data
1098 rpc_call_async(struct rpc_clnt
*clnt
, const struct rpc_message
*msg
, int flags
,
1099 const struct rpc_call_ops
*tk_ops
, void *data
)
1101 struct rpc_task
*task
;
1102 struct rpc_task_setup task_setup_data
= {
1105 .callback_ops
= tk_ops
,
1106 .callback_data
= data
,
1107 .flags
= flags
|RPC_TASK_ASYNC
,
1110 task
= rpc_run_task(&task_setup_data
);
1112 return PTR_ERR(task
);
1116 EXPORT_SYMBOL_GPL(rpc_call_async
);
1118 #if defined(CONFIG_SUNRPC_BACKCHANNEL)
1120 * rpc_run_bc_task - Allocate a new RPC task for backchannel use, then run
1121 * rpc_execute against it
1124 struct rpc_task
*rpc_run_bc_task(struct rpc_rqst
*req
)
1126 struct rpc_task
*task
;
1127 struct xdr_buf
*xbufp
= &req
->rq_snd_buf
;
1128 struct rpc_task_setup task_setup_data
= {
1129 .callback_ops
= &rpc_default_ops
,
1130 .flags
= RPC_TASK_SOFTCONN
,
1133 dprintk("RPC: rpc_run_bc_task req= %p\n", req
);
1135 * Create an rpc_task to send the data
1137 task
= rpc_new_task(&task_setup_data
);
1139 xprt_free_bc_request(req
);
1142 task
->tk_rqstp
= req
;
1145 * Set up the xdr_buf length.
1146 * This also indicates that the buffer is XDR encoded already.
1148 xbufp
->len
= xbufp
->head
[0].iov_len
+ xbufp
->page_len
+
1149 xbufp
->tail
[0].iov_len
;
1151 task
->tk_action
= call_bc_transmit
;
1152 atomic_inc(&task
->tk_count
);
1153 WARN_ON_ONCE(atomic_read(&task
->tk_count
) != 2);
1157 dprintk("RPC: rpc_run_bc_task: task= %p\n", task
);
1160 #endif /* CONFIG_SUNRPC_BACKCHANNEL */
1163 rpc_call_start(struct rpc_task
*task
)
1165 task
->tk_action
= call_start
;
1167 EXPORT_SYMBOL_GPL(rpc_call_start
);
1170 * rpc_peeraddr - extract remote peer address from clnt's xprt
1171 * @clnt: RPC client structure
1172 * @buf: target buffer
1173 * @bufsize: length of target buffer
1175 * Returns the number of bytes that are actually in the stored address.
1177 size_t rpc_peeraddr(struct rpc_clnt
*clnt
, struct sockaddr
*buf
, size_t bufsize
)
1180 struct rpc_xprt
*xprt
;
1183 xprt
= rcu_dereference(clnt
->cl_xprt
);
1185 bytes
= xprt
->addrlen
;
1186 if (bytes
> bufsize
)
1188 memcpy(buf
, &xprt
->addr
, bytes
);
1193 EXPORT_SYMBOL_GPL(rpc_peeraddr
);
1196 * rpc_peeraddr2str - return remote peer address in printable format
1197 * @clnt: RPC client structure
1198 * @format: address format
1200 * NB: the lifetime of the memory referenced by the returned pointer is
1201 * the same as the rpc_xprt itself. As long as the caller uses this
1202 * pointer, it must hold the RCU read lock.
1204 const char *rpc_peeraddr2str(struct rpc_clnt
*clnt
,
1205 enum rpc_display_format_t format
)
1207 struct rpc_xprt
*xprt
;
1209 xprt
= rcu_dereference(clnt
->cl_xprt
);
1211 if (xprt
->address_strings
[format
] != NULL
)
1212 return xprt
->address_strings
[format
];
1214 return "unprintable";
1216 EXPORT_SYMBOL_GPL(rpc_peeraddr2str
);
1218 static const struct sockaddr_in rpc_inaddr_loopback
= {
1219 .sin_family
= AF_INET
,
1220 .sin_addr
.s_addr
= htonl(INADDR_ANY
),
1223 static const struct sockaddr_in6 rpc_in6addr_loopback
= {
1224 .sin6_family
= AF_INET6
,
1225 .sin6_addr
= IN6ADDR_ANY_INIT
,
1229 * Try a getsockname() on a connected datagram socket. Using a
1230 * connected datagram socket prevents leaving a socket in TIME_WAIT.
1231 * This conserves the ephemeral port number space.
1233 * Returns zero and fills in "buf" if successful; otherwise, a
1234 * negative errno is returned.
1236 static int rpc_sockname(struct net
*net
, struct sockaddr
*sap
, size_t salen
,
1237 struct sockaddr
*buf
, int buflen
)
1239 struct socket
*sock
;
1242 err
= __sock_create(net
, sap
->sa_family
,
1243 SOCK_DGRAM
, IPPROTO_UDP
, &sock
, 1);
1245 dprintk("RPC: can't create UDP socket (%d)\n", err
);
1249 switch (sap
->sa_family
) {
1251 err
= kernel_bind(sock
,
1252 (struct sockaddr
*)&rpc_inaddr_loopback
,
1253 sizeof(rpc_inaddr_loopback
));
1256 err
= kernel_bind(sock
,
1257 (struct sockaddr
*)&rpc_in6addr_loopback
,
1258 sizeof(rpc_in6addr_loopback
));
1261 err
= -EAFNOSUPPORT
;
1265 dprintk("RPC: can't bind UDP socket (%d)\n", err
);
1269 err
= kernel_connect(sock
, sap
, salen
, 0);
1271 dprintk("RPC: can't connect UDP socket (%d)\n", err
);
1275 err
= kernel_getsockname(sock
, buf
, &buflen
);
1277 dprintk("RPC: getsockname failed (%d)\n", err
);
1282 if (buf
->sa_family
== AF_INET6
) {
1283 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)buf
;
1284 sin6
->sin6_scope_id
= 0;
1286 dprintk("RPC: %s succeeded\n", __func__
);
1295 * Scraping a connected socket failed, so we don't have a useable
1296 * local address. Fallback: generate an address that will prevent
1297 * the server from calling us back.
1299 * Returns zero and fills in "buf" if successful; otherwise, a
1300 * negative errno is returned.
1302 static int rpc_anyaddr(int family
, struct sockaddr
*buf
, size_t buflen
)
1306 if (buflen
< sizeof(rpc_inaddr_loopback
))
1308 memcpy(buf
, &rpc_inaddr_loopback
,
1309 sizeof(rpc_inaddr_loopback
));
1312 if (buflen
< sizeof(rpc_in6addr_loopback
))
1314 memcpy(buf
, &rpc_in6addr_loopback
,
1315 sizeof(rpc_in6addr_loopback
));
1318 dprintk("RPC: %s: address family not supported\n",
1320 return -EAFNOSUPPORT
;
1322 dprintk("RPC: %s: succeeded\n", __func__
);
1327 * rpc_localaddr - discover local endpoint address for an RPC client
1328 * @clnt: RPC client structure
1329 * @buf: target buffer
1330 * @buflen: size of target buffer, in bytes
1332 * Returns zero and fills in "buf" and "buflen" if successful;
1333 * otherwise, a negative errno is returned.
1335 * This works even if the underlying transport is not currently connected,
1336 * or if the upper layer never previously provided a source address.
1338 * The result of this function call is transient: multiple calls in
1339 * succession may give different results, depending on how local
1340 * networking configuration changes over time.
1342 int rpc_localaddr(struct rpc_clnt
*clnt
, struct sockaddr
*buf
, size_t buflen
)
1344 struct sockaddr_storage address
;
1345 struct sockaddr
*sap
= (struct sockaddr
*)&address
;
1346 struct rpc_xprt
*xprt
;
1352 xprt
= rcu_dereference(clnt
->cl_xprt
);
1353 salen
= xprt
->addrlen
;
1354 memcpy(sap
, &xprt
->addr
, salen
);
1355 net
= get_net(xprt
->xprt_net
);
1358 rpc_set_port(sap
, 0);
1359 err
= rpc_sockname(net
, sap
, salen
, buf
, buflen
);
1362 /* Couldn't discover local address, return ANYADDR */
1363 return rpc_anyaddr(sap
->sa_family
, buf
, buflen
);
1366 EXPORT_SYMBOL_GPL(rpc_localaddr
);
1369 rpc_setbufsize(struct rpc_clnt
*clnt
, unsigned int sndsize
, unsigned int rcvsize
)
1371 struct rpc_xprt
*xprt
;
1374 xprt
= rcu_dereference(clnt
->cl_xprt
);
1375 if (xprt
->ops
->set_buffer_size
)
1376 xprt
->ops
->set_buffer_size(xprt
, sndsize
, rcvsize
);
1379 EXPORT_SYMBOL_GPL(rpc_setbufsize
);
1382 * rpc_protocol - Get transport protocol number for an RPC client
1383 * @clnt: RPC client to query
1386 int rpc_protocol(struct rpc_clnt
*clnt
)
1391 protocol
= rcu_dereference(clnt
->cl_xprt
)->prot
;
1395 EXPORT_SYMBOL_GPL(rpc_protocol
);
1398 * rpc_net_ns - Get the network namespace for this RPC client
1399 * @clnt: RPC client to query
1402 struct net
*rpc_net_ns(struct rpc_clnt
*clnt
)
1407 ret
= rcu_dereference(clnt
->cl_xprt
)->xprt_net
;
1411 EXPORT_SYMBOL_GPL(rpc_net_ns
);
1414 * rpc_max_payload - Get maximum payload size for a transport, in bytes
1415 * @clnt: RPC client to query
1417 * For stream transports, this is one RPC record fragment (see RFC
1418 * 1831), as we don't support multi-record requests yet. For datagram
1419 * transports, this is the size of an IP packet minus the IP, UDP, and
1422 size_t rpc_max_payload(struct rpc_clnt
*clnt
)
1427 ret
= rcu_dereference(clnt
->cl_xprt
)->max_payload
;
1431 EXPORT_SYMBOL_GPL(rpc_max_payload
);
1434 * rpc_max_bc_payload - Get maximum backchannel payload size, in bytes
1435 * @clnt: RPC client to query
1437 size_t rpc_max_bc_payload(struct rpc_clnt
*clnt
)
1439 struct rpc_xprt
*xprt
;
1443 xprt
= rcu_dereference(clnt
->cl_xprt
);
1444 ret
= xprt
->ops
->bc_maxpayload(xprt
);
1448 EXPORT_SYMBOL_GPL(rpc_max_bc_payload
);
1451 * rpc_get_timeout - Get timeout for transport in units of HZ
1452 * @clnt: RPC client to query
1454 unsigned long rpc_get_timeout(struct rpc_clnt
*clnt
)
1459 ret
= rcu_dereference(clnt
->cl_xprt
)->timeout
->to_initval
;
1463 EXPORT_SYMBOL_GPL(rpc_get_timeout
);
1466 * rpc_force_rebind - force transport to check that remote port is unchanged
1467 * @clnt: client to rebind
1470 void rpc_force_rebind(struct rpc_clnt
*clnt
)
1472 if (clnt
->cl_autobind
) {
1474 xprt_clear_bound(rcu_dereference(clnt
->cl_xprt
));
1478 EXPORT_SYMBOL_GPL(rpc_force_rebind
);
1481 * Restart an (async) RPC call from the call_prepare state.
1482 * Usually called from within the exit handler.
1485 rpc_restart_call_prepare(struct rpc_task
*task
)
1487 if (RPC_ASSASSINATED(task
))
1489 task
->tk_action
= call_start
;
1490 task
->tk_status
= 0;
1491 if (task
->tk_ops
->rpc_call_prepare
!= NULL
)
1492 task
->tk_action
= rpc_prepare_task
;
1495 EXPORT_SYMBOL_GPL(rpc_restart_call_prepare
);
1498 * Restart an (async) RPC call. Usually called from within the
1502 rpc_restart_call(struct rpc_task
*task
)
1504 if (RPC_ASSASSINATED(task
))
1506 task
->tk_action
= call_start
;
1507 task
->tk_status
= 0;
1510 EXPORT_SYMBOL_GPL(rpc_restart_call
);
1512 #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
1514 *rpc_proc_name(const struct rpc_task
*task
)
1516 const struct rpc_procinfo
*proc
= task
->tk_msg
.rpc_proc
;
1520 return proc
->p_name
;
1531 * Other FSM states can be visited zero or more times, but
1532 * this state is visited exactly once for each RPC.
1535 call_start(struct rpc_task
*task
)
1537 struct rpc_clnt
*clnt
= task
->tk_client
;
1539 dprintk("RPC: %5u call_start %s%d proc %s (%s)\n", task
->tk_pid
,
1540 clnt
->cl_program
->name
, clnt
->cl_vers
,
1541 rpc_proc_name(task
),
1542 (RPC_IS_ASYNC(task
) ? "async" : "sync"));
1544 /* Increment call count */
1545 task
->tk_msg
.rpc_proc
->p_count
++;
1546 clnt
->cl_stats
->rpccnt
++;
1547 task
->tk_action
= call_reserve
;
1551 * 1. Reserve an RPC call slot
1554 call_reserve(struct rpc_task
*task
)
1556 dprint_status(task
);
1558 task
->tk_status
= 0;
1559 task
->tk_action
= call_reserveresult
;
1563 static void call_retry_reserve(struct rpc_task
*task
);
1566 * 1b. Grok the result of xprt_reserve()
1569 call_reserveresult(struct rpc_task
*task
)
1571 int status
= task
->tk_status
;
1573 dprint_status(task
);
1576 * After a call to xprt_reserve(), we must have either
1577 * a request slot or else an error status.
1579 task
->tk_status
= 0;
1581 if (task
->tk_rqstp
) {
1582 task
->tk_action
= call_refresh
;
1586 printk(KERN_ERR
"%s: status=%d, but no request slot, exiting\n",
1588 rpc_exit(task
, -EIO
);
1593 * Even though there was an error, we may have acquired
1594 * a request slot somehow. Make sure not to leak it.
1596 if (task
->tk_rqstp
) {
1597 printk(KERN_ERR
"%s: status=%d, request allocated anyway\n",
1604 rpc_delay(task
, HZ
>> 2);
1605 case -EAGAIN
: /* woken up; retry */
1606 task
->tk_action
= call_retry_reserve
;
1608 case -EIO
: /* probably a shutdown */
1611 printk(KERN_ERR
"%s: unrecognized error %d, exiting\n",
1615 rpc_exit(task
, status
);
1619 * 1c. Retry reserving an RPC call slot
1622 call_retry_reserve(struct rpc_task
*task
)
1624 dprint_status(task
);
1626 task
->tk_status
= 0;
1627 task
->tk_action
= call_reserveresult
;
1628 xprt_retry_reserve(task
);
1632 * 2. Bind and/or refresh the credentials
1635 call_refresh(struct rpc_task
*task
)
1637 dprint_status(task
);
1639 task
->tk_action
= call_refreshresult
;
1640 task
->tk_status
= 0;
1641 task
->tk_client
->cl_stats
->rpcauthrefresh
++;
1642 rpcauth_refreshcred(task
);
1646 * 2a. Process the results of a credential refresh
1649 call_refreshresult(struct rpc_task
*task
)
1651 int status
= task
->tk_status
;
1653 dprint_status(task
);
1655 task
->tk_status
= 0;
1656 task
->tk_action
= call_refresh
;
1659 if (rpcauth_uptodatecred(task
)) {
1660 task
->tk_action
= call_allocate
;
1663 /* Use rate-limiting and a max number of retries if refresh
1664 * had status 0 but failed to update the cred.
1667 rpc_delay(task
, 3*HZ
);
1671 if (!task
->tk_cred_retry
)
1673 task
->tk_cred_retry
--;
1674 dprintk("RPC: %5u %s: retry refresh creds\n",
1675 task
->tk_pid
, __func__
);
1678 dprintk("RPC: %5u %s: refresh creds failed with error %d\n",
1679 task
->tk_pid
, __func__
, status
);
1680 rpc_exit(task
, status
);
1684 * 2b. Allocate the buffer. For details, see sched.c:rpc_malloc.
1685 * (Note: buffer memory is freed in xprt_release).
1688 call_allocate(struct rpc_task
*task
)
1690 unsigned int slack
= task
->tk_rqstp
->rq_cred
->cr_auth
->au_cslack
;
1691 struct rpc_rqst
*req
= task
->tk_rqstp
;
1692 struct rpc_xprt
*xprt
= req
->rq_xprt
;
1693 struct rpc_procinfo
*proc
= task
->tk_msg
.rpc_proc
;
1696 dprint_status(task
);
1698 task
->tk_status
= 0;
1699 task
->tk_action
= call_bind
;
1704 if (proc
->p_proc
!= 0) {
1705 BUG_ON(proc
->p_arglen
== 0);
1706 if (proc
->p_decode
!= NULL
)
1707 BUG_ON(proc
->p_replen
== 0);
1711 * Calculate the size (in quads) of the RPC call
1712 * and reply headers, and convert both values
1715 req
->rq_callsize
= RPC_CALLHDRSIZE
+ (slack
<< 1) + proc
->p_arglen
;
1716 req
->rq_callsize
<<= 2;
1717 req
->rq_rcvsize
= RPC_REPHDRSIZE
+ slack
+ proc
->p_replen
;
1718 req
->rq_rcvsize
<<= 2;
1720 status
= xprt
->ops
->buf_alloc(task
);
1721 xprt_inject_disconnect(xprt
);
1724 if (status
!= -ENOMEM
) {
1725 rpc_exit(task
, status
);
1729 dprintk("RPC: %5u rpc_buffer allocation failed\n", task
->tk_pid
);
1731 if (RPC_IS_ASYNC(task
) || !fatal_signal_pending(current
)) {
1732 task
->tk_action
= call_allocate
;
1733 rpc_delay(task
, HZ
>>4);
1737 rpc_exit(task
, -ERESTARTSYS
);
1741 rpc_task_need_encode(struct rpc_task
*task
)
1743 return task
->tk_rqstp
->rq_snd_buf
.len
== 0;
1747 rpc_task_force_reencode(struct rpc_task
*task
)
1749 task
->tk_rqstp
->rq_snd_buf
.len
= 0;
1750 task
->tk_rqstp
->rq_bytes_sent
= 0;
1754 * 3. Encode arguments of an RPC call
1757 rpc_xdr_encode(struct rpc_task
*task
)
1759 struct rpc_rqst
*req
= task
->tk_rqstp
;
1763 dprint_status(task
);
1765 xdr_buf_init(&req
->rq_snd_buf
,
1768 xdr_buf_init(&req
->rq_rcv_buf
,
1772 p
= rpc_encode_header(task
);
1774 printk(KERN_INFO
"RPC: couldn't encode RPC header, exit EIO\n");
1775 rpc_exit(task
, -EIO
);
1779 encode
= task
->tk_msg
.rpc_proc
->p_encode
;
1783 task
->tk_status
= rpcauth_wrap_req(task
, encode
, req
, p
,
1784 task
->tk_msg
.rpc_argp
);
1788 * 4. Get the server port number if not yet set
1791 call_bind(struct rpc_task
*task
)
1793 struct rpc_xprt
*xprt
= task
->tk_rqstp
->rq_xprt
;
1795 dprint_status(task
);
1797 task
->tk_action
= call_connect
;
1798 if (!xprt_bound(xprt
)) {
1799 task
->tk_action
= call_bind_status
;
1800 task
->tk_timeout
= xprt
->bind_timeout
;
1801 xprt
->ops
->rpcbind(task
);
1806 * 4a. Sort out bind result
1809 call_bind_status(struct rpc_task
*task
)
1813 if (task
->tk_status
>= 0) {
1814 dprint_status(task
);
1815 task
->tk_status
= 0;
1816 task
->tk_action
= call_connect
;
1820 trace_rpc_bind_status(task
);
1821 switch (task
->tk_status
) {
1823 dprintk("RPC: %5u rpcbind out of memory\n", task
->tk_pid
);
1824 rpc_delay(task
, HZ
>> 2);
1827 dprintk("RPC: %5u remote rpcbind: RPC program/version "
1828 "unavailable\n", task
->tk_pid
);
1829 /* fail immediately if this is an RPC ping */
1830 if (task
->tk_msg
.rpc_proc
->p_proc
== 0) {
1831 status
= -EOPNOTSUPP
;
1834 if (task
->tk_rebind_retry
== 0)
1836 task
->tk_rebind_retry
--;
1837 rpc_delay(task
, 3*HZ
);
1840 dprintk("RPC: %5u rpcbind request timed out\n",
1844 /* server doesn't support any rpcbind version we know of */
1845 dprintk("RPC: %5u unrecognized remote rpcbind service\n",
1848 case -EPROTONOSUPPORT
:
1849 dprintk("RPC: %5u remote rpcbind version unavailable, retrying\n",
1852 case -ECONNREFUSED
: /* connection problems */
1861 dprintk("RPC: %5u remote rpcbind unreachable: %d\n",
1862 task
->tk_pid
, task
->tk_status
);
1863 if (!RPC_IS_SOFTCONN(task
)) {
1864 rpc_delay(task
, 5*HZ
);
1867 status
= task
->tk_status
;
1870 dprintk("RPC: %5u unrecognized rpcbind error (%d)\n",
1871 task
->tk_pid
, -task
->tk_status
);
1874 rpc_exit(task
, status
);
1878 task
->tk_status
= 0;
1879 task
->tk_action
= call_timeout
;
1883 * 4b. Connect to the RPC server
1886 call_connect(struct rpc_task
*task
)
1888 struct rpc_xprt
*xprt
= task
->tk_rqstp
->rq_xprt
;
1890 dprintk("RPC: %5u call_connect xprt %p %s connected\n",
1892 (xprt_connected(xprt
) ? "is" : "is not"));
1894 task
->tk_action
= call_transmit
;
1895 if (!xprt_connected(xprt
)) {
1896 task
->tk_action
= call_connect_status
;
1897 if (task
->tk_status
< 0)
1899 if (task
->tk_flags
& RPC_TASK_NOCONNECT
) {
1900 rpc_exit(task
, -ENOTCONN
);
1908 * 4c. Sort out connect result
1911 call_connect_status(struct rpc_task
*task
)
1913 struct rpc_clnt
*clnt
= task
->tk_client
;
1914 int status
= task
->tk_status
;
1916 dprint_status(task
);
1918 trace_rpc_connect_status(task
, status
);
1919 task
->tk_status
= 0;
1929 if (RPC_IS_SOFTCONN(task
))
1931 /* retry with existing socket, after a delay */
1932 rpc_delay(task
, 3*HZ
);
1934 /* Check for timeouts before looping back to call_bind */
1936 task
->tk_action
= call_timeout
;
1939 clnt
->cl_stats
->netreconn
++;
1940 task
->tk_action
= call_transmit
;
1943 rpc_exit(task
, status
);
1947 * 5. Transmit the RPC request, and wait for reply
1950 call_transmit(struct rpc_task
*task
)
1952 int is_retrans
= RPC_WAS_SENT(task
);
1954 dprint_status(task
);
1956 task
->tk_action
= call_status
;
1957 if (task
->tk_status
< 0)
1959 if (!xprt_prepare_transmit(task
))
1961 task
->tk_action
= call_transmit_status
;
1962 /* Encode here so that rpcsec_gss can use correct sequence number. */
1963 if (rpc_task_need_encode(task
)) {
1964 rpc_xdr_encode(task
);
1965 /* Did the encode result in an error condition? */
1966 if (task
->tk_status
!= 0) {
1967 /* Was the error nonfatal? */
1968 if (task
->tk_status
== -EAGAIN
)
1969 rpc_delay(task
, HZ
>> 4);
1971 rpc_exit(task
, task
->tk_status
);
1975 xprt_transmit(task
);
1976 if (task
->tk_status
< 0)
1979 task
->tk_client
->cl_stats
->rpcretrans
++;
1981 * On success, ensure that we call xprt_end_transmit() before sleeping
1982 * in order to allow access to the socket to other RPC requests.
1984 call_transmit_status(task
);
1985 if (rpc_reply_expected(task
))
1987 task
->tk_action
= rpc_exit_task
;
1988 rpc_wake_up_queued_task(&task
->tk_rqstp
->rq_xprt
->pending
, task
);
1992 * 5a. Handle cleanup after a transmission
1995 call_transmit_status(struct rpc_task
*task
)
1997 task
->tk_action
= call_status
;
2000 * Common case: success. Force the compiler to put this
2003 if (task
->tk_status
== 0) {
2004 xprt_end_transmit(task
);
2005 rpc_task_force_reencode(task
);
2009 switch (task
->tk_status
) {
2014 dprint_status(task
);
2015 xprt_end_transmit(task
);
2016 rpc_task_force_reencode(task
);
2019 * Special cases: if we've been waiting on the
2020 * socket's write_space() callback, or if the
2021 * socket just returned a connection error,
2022 * then hold onto the transport lock.
2029 if (RPC_IS_SOFTCONN(task
)) {
2030 xprt_end_transmit(task
);
2031 rpc_exit(task
, task
->tk_status
);
2039 rpc_task_force_reencode(task
);
2043 #if defined(CONFIG_SUNRPC_BACKCHANNEL)
2045 * 5b. Send the backchannel RPC reply. On error, drop the reply. In
2046 * addition, disconnect on connectivity errors.
2049 call_bc_transmit(struct rpc_task
*task
)
2051 struct rpc_rqst
*req
= task
->tk_rqstp
;
2053 if (!xprt_prepare_transmit(task
))
2056 if (task
->tk_status
< 0) {
2057 printk(KERN_NOTICE
"RPC: Could not send backchannel reply "
2058 "error: %d\n", task
->tk_status
);
2061 if (req
->rq_connect_cookie
!= req
->rq_xprt
->connect_cookie
)
2062 req
->rq_bytes_sent
= 0;
2064 xprt_transmit(task
);
2066 if (task
->tk_status
== -EAGAIN
)
2069 xprt_end_transmit(task
);
2070 dprint_status(task
);
2071 switch (task
->tk_status
) {
2085 * Problem reaching the server. Disconnect and let the
2086 * forechannel reestablish the connection. The server will
2087 * have to retransmit the backchannel request and we'll
2088 * reprocess it. Since these ops are idempotent, there's no
2089 * need to cache our reply at this time.
2091 printk(KERN_NOTICE
"RPC: Could not send backchannel reply "
2092 "error: %d\n", task
->tk_status
);
2093 xprt_conditional_disconnect(req
->rq_xprt
,
2094 req
->rq_connect_cookie
);
2098 * We were unable to reply and will have to drop the
2099 * request. The server should reconnect and retransmit.
2101 WARN_ON_ONCE(task
->tk_status
== -EAGAIN
);
2102 printk(KERN_NOTICE
"RPC: Could not send backchannel reply "
2103 "error: %d\n", task
->tk_status
);
2106 rpc_wake_up_queued_task(&req
->rq_xprt
->pending
, task
);
2108 task
->tk_action
= rpc_exit_task
;
2111 req
->rq_connect_cookie
= req
->rq_xprt
->connect_cookie
;
2113 task
->tk_status
= 0;
2115 #endif /* CONFIG_SUNRPC_BACKCHANNEL */
2118 * 6. Sort out the RPC call status
2121 call_status(struct rpc_task
*task
)
2123 struct rpc_clnt
*clnt
= task
->tk_client
;
2124 struct rpc_rqst
*req
= task
->tk_rqstp
;
2127 if (req
->rq_reply_bytes_recvd
> 0 && !req
->rq_bytes_sent
)
2128 task
->tk_status
= req
->rq_reply_bytes_recvd
;
2130 dprint_status(task
);
2132 status
= task
->tk_status
;
2134 task
->tk_action
= call_decode
;
2138 trace_rpc_call_status(task
);
2139 task
->tk_status
= 0;
2145 if (RPC_IS_SOFTCONN(task
)) {
2146 rpc_exit(task
, status
);
2150 * Delay any retries for 3 seconds, then handle as if it
2153 rpc_delay(task
, 3*HZ
);
2155 task
->tk_action
= call_timeout
;
2156 if (!(task
->tk_flags
& RPC_TASK_NO_RETRANS_TIMEOUT
)
2157 && task
->tk_client
->cl_discrtry
)
2158 xprt_conditional_disconnect(req
->rq_xprt
,
2159 req
->rq_connect_cookie
);
2164 rpc_force_rebind(clnt
);
2166 rpc_delay(task
, 3*HZ
);
2169 task
->tk_action
= call_bind
;
2172 rpc_delay(task
, HZ
>>2);
2174 task
->tk_action
= call_transmit
;
2177 /* shutdown or soft timeout */
2178 rpc_exit(task
, status
);
2181 if (clnt
->cl_chatty
)
2182 printk("%s: RPC call returned error %d\n",
2183 clnt
->cl_program
->name
, -status
);
2184 rpc_exit(task
, status
);
2189 * 6a. Handle RPC timeout
2190 * We do not release the request slot, so we keep using the
2191 * same XID for all retransmits.
2194 call_timeout(struct rpc_task
*task
)
2196 struct rpc_clnt
*clnt
= task
->tk_client
;
2198 if (xprt_adjust_timeout(task
->tk_rqstp
) == 0) {
2199 dprintk("RPC: %5u call_timeout (minor)\n", task
->tk_pid
);
2203 dprintk("RPC: %5u call_timeout (major)\n", task
->tk_pid
);
2204 task
->tk_timeouts
++;
2206 if (RPC_IS_SOFTCONN(task
)) {
2207 rpc_exit(task
, -ETIMEDOUT
);
2210 if (RPC_IS_SOFT(task
)) {
2211 if (clnt
->cl_chatty
) {
2212 printk(KERN_NOTICE
"%s: server %s not responding, timed out\n",
2213 clnt
->cl_program
->name
,
2214 task
->tk_xprt
->servername
);
2216 if (task
->tk_flags
& RPC_TASK_TIMEOUT
)
2217 rpc_exit(task
, -ETIMEDOUT
);
2219 rpc_exit(task
, -EIO
);
2223 if (!(task
->tk_flags
& RPC_CALL_MAJORSEEN
)) {
2224 task
->tk_flags
|= RPC_CALL_MAJORSEEN
;
2225 if (clnt
->cl_chatty
) {
2226 printk(KERN_NOTICE
"%s: server %s not responding, still trying\n",
2227 clnt
->cl_program
->name
,
2228 task
->tk_xprt
->servername
);
2231 rpc_force_rebind(clnt
);
2233 * Did our request time out due to an RPCSEC_GSS out-of-sequence
2234 * event? RFC2203 requires the server to drop all such requests.
2236 rpcauth_invalcred(task
);
2239 task
->tk_action
= call_bind
;
2240 task
->tk_status
= 0;
2244 * 7. Decode the RPC reply
2247 call_decode(struct rpc_task
*task
)
2249 struct rpc_clnt
*clnt
= task
->tk_client
;
2250 struct rpc_rqst
*req
= task
->tk_rqstp
;
2251 kxdrdproc_t decode
= task
->tk_msg
.rpc_proc
->p_decode
;
2254 dprint_status(task
);
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.
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_bind
;
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
;
2287 p
= rpc_verify_header(task
);
2289 if (p
== ERR_PTR(-EAGAIN
))
2294 task
->tk_action
= rpc_exit_task
;
2297 task
->tk_status
= rpcauth_unwrap_resp(task
, decode
, req
, p
,
2298 task
->tk_msg
.rpc_resp
);
2300 dprintk("RPC: %5u call_decode result %d\n", task
->tk_pid
,
2304 task
->tk_status
= 0;
2305 /* Note: rpc_verify_header() may have freed the RPC slot */
2306 if (task
->tk_rqstp
== req
) {
2307 req
->rq_reply_bytes_recvd
= req
->rq_rcv_buf
.len
= 0;
2308 if (task
->tk_client
->cl_discrtry
)
2309 xprt_conditional_disconnect(req
->rq_xprt
,
2310 req
->rq_connect_cookie
);
2315 rpc_encode_header(struct rpc_task
*task
)
2317 struct rpc_clnt
*clnt
= task
->tk_client
;
2318 struct rpc_rqst
*req
= task
->tk_rqstp
;
2319 __be32
*p
= req
->rq_svec
[0].iov_base
;
2321 /* FIXME: check buffer size? */
2323 p
= xprt_skip_transport_header(req
->rq_xprt
, p
);
2324 *p
++ = req
->rq_xid
; /* XID */
2325 *p
++ = htonl(RPC_CALL
); /* CALL */
2326 *p
++ = htonl(RPC_VERSION
); /* RPC version */
2327 *p
++ = htonl(clnt
->cl_prog
); /* program number */
2328 *p
++ = htonl(clnt
->cl_vers
); /* program version */
2329 *p
++ = htonl(task
->tk_msg
.rpc_proc
->p_proc
); /* procedure */
2330 p
= rpcauth_marshcred(task
, p
);
2331 req
->rq_slen
= xdr_adjust_iovec(&req
->rq_svec
[0], p
);
2336 rpc_verify_header(struct rpc_task
*task
)
2338 struct rpc_clnt
*clnt
= task
->tk_client
;
2339 struct kvec
*iov
= &task
->tk_rqstp
->rq_rcv_buf
.head
[0];
2340 int len
= task
->tk_rqstp
->rq_rcv_buf
.len
>> 2;
2341 __be32
*p
= iov
->iov_base
;
2343 int error
= -EACCES
;
2345 if ((task
->tk_rqstp
->rq_rcv_buf
.len
& 3) != 0) {
2346 /* RFC-1014 says that the representation of XDR data must be a
2347 * multiple of four bytes
2348 * - if it isn't pointer subtraction in the NFS client may give
2351 dprintk("RPC: %5u %s: XDR representation not a multiple of"
2352 " 4 bytes: 0x%x\n", task
->tk_pid
, __func__
,
2353 task
->tk_rqstp
->rq_rcv_buf
.len
);
2360 p
+= 1; /* skip XID */
2361 if ((n
= ntohl(*p
++)) != RPC_REPLY
) {
2362 dprintk("RPC: %5u %s: not an RPC reply: %x\n",
2363 task
->tk_pid
, __func__
, n
);
2368 if ((n
= ntohl(*p
++)) != RPC_MSG_ACCEPTED
) {
2371 switch ((n
= ntohl(*p
++))) {
2372 case RPC_AUTH_ERROR
:
2375 dprintk("RPC: %5u %s: RPC call version mismatch!\n",
2376 task
->tk_pid
, __func__
);
2377 error
= -EPROTONOSUPPORT
;
2380 dprintk("RPC: %5u %s: RPC call rejected, "
2381 "unknown error: %x\n",
2382 task
->tk_pid
, __func__
, n
);
2388 switch ((n
= ntohl(*p
++))) {
2389 case RPC_AUTH_REJECTEDCRED
:
2390 case RPC_AUTH_REJECTEDVERF
:
2391 case RPCSEC_GSS_CREDPROBLEM
:
2392 case RPCSEC_GSS_CTXPROBLEM
:
2393 if (!task
->tk_cred_retry
)
2395 task
->tk_cred_retry
--;
2396 dprintk("RPC: %5u %s: retry stale creds\n",
2397 task
->tk_pid
, __func__
);
2398 rpcauth_invalcred(task
);
2399 /* Ensure we obtain a new XID! */
2401 task
->tk_action
= call_reserve
;
2403 case RPC_AUTH_BADCRED
:
2404 case RPC_AUTH_BADVERF
:
2405 /* possibly garbled cred/verf? */
2406 if (!task
->tk_garb_retry
)
2408 task
->tk_garb_retry
--;
2409 dprintk("RPC: %5u %s: retry garbled creds\n",
2410 task
->tk_pid
, __func__
);
2411 task
->tk_action
= call_bind
;
2413 case RPC_AUTH_TOOWEAK
:
2414 printk(KERN_NOTICE
"RPC: server %s requires stronger "
2415 "authentication.\n",
2416 task
->tk_xprt
->servername
);
2419 dprintk("RPC: %5u %s: unknown auth error: %x\n",
2420 task
->tk_pid
, __func__
, n
);
2423 dprintk("RPC: %5u %s: call rejected %d\n",
2424 task
->tk_pid
, __func__
, n
);
2427 p
= rpcauth_checkverf(task
, p
);
2430 dprintk("RPC: %5u %s: auth check failed with %d\n",
2431 task
->tk_pid
, __func__
, error
);
2432 goto out_garbage
; /* bad verifier, retry */
2434 len
= p
- (__be32
*)iov
->iov_base
- 1;
2437 switch ((n
= ntohl(*p
++))) {
2440 case RPC_PROG_UNAVAIL
:
2441 dprintk("RPC: %5u %s: program %u is unsupported "
2442 "by server %s\n", task
->tk_pid
, __func__
,
2443 (unsigned int)clnt
->cl_prog
,
2444 task
->tk_xprt
->servername
);
2445 error
= -EPFNOSUPPORT
;
2447 case RPC_PROG_MISMATCH
:
2448 dprintk("RPC: %5u %s: program %u, version %u unsupported "
2449 "by server %s\n", task
->tk_pid
, __func__
,
2450 (unsigned int)clnt
->cl_prog
,
2451 (unsigned int)clnt
->cl_vers
,
2452 task
->tk_xprt
->servername
);
2453 error
= -EPROTONOSUPPORT
;
2455 case RPC_PROC_UNAVAIL
:
2456 dprintk("RPC: %5u %s: proc %s unsupported by program %u, "
2457 "version %u on server %s\n",
2458 task
->tk_pid
, __func__
,
2459 rpc_proc_name(task
),
2460 clnt
->cl_prog
, clnt
->cl_vers
,
2461 task
->tk_xprt
->servername
);
2462 error
= -EOPNOTSUPP
;
2464 case RPC_GARBAGE_ARGS
:
2465 dprintk("RPC: %5u %s: server saw garbage\n",
2466 task
->tk_pid
, __func__
);
2469 dprintk("RPC: %5u %s: server accept status: %x\n",
2470 task
->tk_pid
, __func__
, n
);
2475 clnt
->cl_stats
->rpcgarbage
++;
2476 if (task
->tk_garb_retry
) {
2477 task
->tk_garb_retry
--;
2478 dprintk("RPC: %5u %s: retrying\n",
2479 task
->tk_pid
, __func__
);
2480 task
->tk_action
= call_bind
;
2482 return ERR_PTR(-EAGAIN
);
2485 rpc_exit(task
, error
);
2486 dprintk("RPC: %5u %s: call failed with error %d\n", task
->tk_pid
,
2488 return ERR_PTR(error
);
2490 dprintk("RPC: %5u %s: server reply was truncated.\n", task
->tk_pid
,
2495 static void rpcproc_encode_null(void *rqstp
, struct xdr_stream
*xdr
, void *obj
)
2499 static int rpcproc_decode_null(void *rqstp
, struct xdr_stream
*xdr
, void *obj
)
2504 static 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
,
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
);
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
,
2530 struct rpc_task_setup task_setup_data
= {
2533 .rpc_message
= &msg
,
2534 .callback_ops
= (ops
!= NULL
) ? ops
: &rpc_default_ops
,
2535 .callback_data
= data
,
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
);
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
2582 int rpc_clnt_test_and_add_xprt(struct rpc_clnt
*clnt
,
2583 struct rpc_xprt_switch
*xps
, struct rpc_xprt
*xprt
,
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
);
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
);
2602 return PTR_ERR(task
);
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
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
,
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
))
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
,
2649 status
= PTR_ERR(task
);
2652 status
= task
->tk_status
;
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 */
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
]);
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
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
*,
2693 struct rpc_xprt_switch
*xps
;
2694 struct rpc_xprt
*xprt
;
2695 unsigned long reconnect_timeout
;
2696 unsigned char resvport
;
2700 xps
= xprt_switch_get(rcu_dereference(clnt
->cl_xpi
.xpi_xpswitch
));
2701 xprt
= xprt_iter_xprt(&clnt
->cl_xpi
);
2702 if (xps
== NULL
|| xprt
== NULL
) {
2706 resvport
= xprt
->resvport
;
2707 reconnect_timeout
= xprt
->max_reconnect_timeout
;
2710 xprt
= xprt_create_transport(xprtargs
);
2712 ret
= PTR_ERR(xprt
);
2713 goto out_put_switch
;
2715 xprt
->resvport
= resvport
;
2716 xprt
->max_reconnect_timeout
= reconnect_timeout
;
2718 rpc_xprt_switch_set_roundrobin(xps
);
2720 ret
= setup(clnt
, xps
, xprt
, data
);
2724 rpc_xprt_switch_add_xprt(xps
, xprt
);
2728 xprt_switch_put(xps
);
2731 EXPORT_SYMBOL_GPL(rpc_clnt_add_xprt
);
2734 rpc_xprt_cap_max_reconnect_timeout(struct rpc_clnt
*clnt
,
2735 struct rpc_xprt
*xprt
,
2738 unsigned long timeout
= *((unsigned long *)data
);
2740 if (timeout
< xprt
->max_reconnect_timeout
)
2741 xprt
->max_reconnect_timeout
= timeout
;
2746 rpc_cap_max_reconnect_timeout(struct rpc_clnt
*clnt
, unsigned long timeo
)
2748 rpc_clnt_iterate_for_each_xprt(clnt
,
2749 rpc_xprt_cap_max_reconnect_timeout
,
2752 EXPORT_SYMBOL_GPL(rpc_cap_max_reconnect_timeout
);
2754 void rpc_clnt_xprt_switch_put(struct rpc_clnt
*clnt
)
2756 xprt_switch_put(rcu_dereference(clnt
->cl_xpi
.xpi_xpswitch
));
2758 EXPORT_SYMBOL_GPL(rpc_clnt_xprt_switch_put
);
2760 void rpc_clnt_xprt_switch_add_xprt(struct rpc_clnt
*clnt
, struct rpc_xprt
*xprt
)
2762 rpc_xprt_switch_add_xprt(rcu_dereference(clnt
->cl_xpi
.xpi_xpswitch
),
2765 EXPORT_SYMBOL_GPL(rpc_clnt_xprt_switch_add_xprt
);
2767 bool rpc_clnt_xprt_switch_has_addr(struct rpc_clnt
*clnt
,
2768 const struct sockaddr
*sap
)
2770 struct rpc_xprt_switch
*xps
;
2773 xps
= rcu_dereference(clnt
->cl_xpi
.xpi_xpswitch
);
2776 ret
= rpc_xprt_switch_has_addr(xps
, sap
);
2780 EXPORT_SYMBOL_GPL(rpc_clnt_xprt_switch_has_addr
);
2782 #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
2783 static void rpc_show_header(void)
2785 printk(KERN_INFO
"-pid- flgs status -client- --rqstp- "
2786 "-timeout ---ops--\n");
2789 static void rpc_show_task(const struct rpc_clnt
*clnt
,
2790 const struct rpc_task
*task
)
2792 const char *rpc_waitq
= "none";
2794 if (RPC_IS_QUEUED(task
))
2795 rpc_waitq
= rpc_qname(task
->tk_waitqueue
);
2797 printk(KERN_INFO
"%5u %04x %6d %8p %8p %8ld %8p %sv%u %s a:%ps q:%s\n",
2798 task
->tk_pid
, task
->tk_flags
, task
->tk_status
,
2799 clnt
, task
->tk_rqstp
, task
->tk_timeout
, task
->tk_ops
,
2800 clnt
->cl_program
->name
, clnt
->cl_vers
, rpc_proc_name(task
),
2801 task
->tk_action
, rpc_waitq
);
2804 void rpc_show_tasks(struct net
*net
)
2806 struct rpc_clnt
*clnt
;
2807 struct rpc_task
*task
;
2809 struct sunrpc_net
*sn
= net_generic(net
, sunrpc_net_id
);
2811 spin_lock(&sn
->rpc_client_lock
);
2812 list_for_each_entry(clnt
, &sn
->all_clients
, cl_clients
) {
2813 spin_lock(&clnt
->cl_lock
);
2814 list_for_each_entry(task
, &clnt
->cl_tasks
, tk_task
) {
2819 rpc_show_task(clnt
, task
);
2821 spin_unlock(&clnt
->cl_lock
);
2823 spin_unlock(&sn
->rpc_client_lock
);
2827 #if IS_ENABLED(CONFIG_SUNRPC_SWAP)
2829 rpc_clnt_swap_activate_callback(struct rpc_clnt
*clnt
,
2830 struct rpc_xprt
*xprt
,
2833 return xprt_enable_swap(xprt
);
2837 rpc_clnt_swap_activate(struct rpc_clnt
*clnt
)
2839 if (atomic_inc_return(&clnt
->cl_swapper
) == 1)
2840 return rpc_clnt_iterate_for_each_xprt(clnt
,
2841 rpc_clnt_swap_activate_callback
, NULL
);
2844 EXPORT_SYMBOL_GPL(rpc_clnt_swap_activate
);
2847 rpc_clnt_swap_deactivate_callback(struct rpc_clnt
*clnt
,
2848 struct rpc_xprt
*xprt
,
2851 xprt_disable_swap(xprt
);
2856 rpc_clnt_swap_deactivate(struct rpc_clnt
*clnt
)
2858 if (atomic_dec_if_positive(&clnt
->cl_swapper
) == 0)
2859 rpc_clnt_iterate_for_each_xprt(clnt
,
2860 rpc_clnt_swap_deactivate_callback
, NULL
);
2862 EXPORT_SYMBOL_GPL(rpc_clnt_swap_deactivate
);
2863 #endif /* CONFIG_SUNRPC_SWAP */