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_encode(struct rpc_task
*task
);
65 static void call_decode(struct rpc_task
*task
);
66 static void call_bind(struct rpc_task
*task
);
67 static void call_bind_status(struct rpc_task
*task
);
68 static void call_transmit(struct rpc_task
*task
);
69 #if defined(CONFIG_SUNRPC_BACKCHANNEL)
70 static void call_bc_transmit(struct rpc_task
*task
);
71 #endif /* CONFIG_SUNRPC_BACKCHANNEL */
72 static void call_status(struct rpc_task
*task
);
73 static void call_transmit_status(struct rpc_task
*task
);
74 static void call_refresh(struct rpc_task
*task
);
75 static void call_refreshresult(struct rpc_task
*task
);
76 static void call_timeout(struct rpc_task
*task
);
77 static void call_connect(struct rpc_task
*task
);
78 static void call_connect_status(struct rpc_task
*task
);
80 static __be32
*rpc_encode_header(struct rpc_task
*task
);
81 static __be32
*rpc_verify_header(struct rpc_task
*task
);
82 static int rpc_ping(struct rpc_clnt
*clnt
);
84 static void rpc_register_client(struct rpc_clnt
*clnt
)
86 struct net
*net
= rpc_net_ns(clnt
);
87 struct sunrpc_net
*sn
= net_generic(net
, sunrpc_net_id
);
89 spin_lock(&sn
->rpc_client_lock
);
90 list_add(&clnt
->cl_clients
, &sn
->all_clients
);
91 spin_unlock(&sn
->rpc_client_lock
);
94 static void rpc_unregister_client(struct rpc_clnt
*clnt
)
96 struct net
*net
= rpc_net_ns(clnt
);
97 struct sunrpc_net
*sn
= net_generic(net
, sunrpc_net_id
);
99 spin_lock(&sn
->rpc_client_lock
);
100 list_del(&clnt
->cl_clients
);
101 spin_unlock(&sn
->rpc_client_lock
);
104 static void __rpc_clnt_remove_pipedir(struct rpc_clnt
*clnt
)
106 rpc_remove_client_dir(clnt
);
109 static void rpc_clnt_remove_pipedir(struct rpc_clnt
*clnt
)
111 struct net
*net
= rpc_net_ns(clnt
);
112 struct super_block
*pipefs_sb
;
114 pipefs_sb
= rpc_get_sb_net(net
);
116 __rpc_clnt_remove_pipedir(clnt
);
121 static struct dentry
*rpc_setup_pipedir_sb(struct super_block
*sb
,
122 struct rpc_clnt
*clnt
)
124 static uint32_t clntid
;
125 const char *dir_name
= clnt
->cl_program
->pipe_dir_name
;
127 struct dentry
*dir
, *dentry
;
129 dir
= rpc_d_lookup_sb(sb
, dir_name
);
131 pr_info("RPC: pipefs directory doesn't exist: %s\n", dir_name
);
135 snprintf(name
, sizeof(name
), "clnt%x", (unsigned int)clntid
++);
136 name
[sizeof(name
) - 1] = '\0';
137 dentry
= rpc_create_client_dir(dir
, name
, clnt
);
140 if (dentry
== ERR_PTR(-EEXIST
))
142 printk(KERN_INFO
"RPC: Couldn't create pipefs entry"
143 " %s/%s, error %ld\n",
144 dir_name
, name
, PTR_ERR(dentry
));
152 rpc_setup_pipedir(struct super_block
*pipefs_sb
, struct rpc_clnt
*clnt
)
154 struct dentry
*dentry
;
156 if (clnt
->cl_program
->pipe_dir_name
!= NULL
) {
157 dentry
= rpc_setup_pipedir_sb(pipefs_sb
, clnt
);
159 return PTR_ERR(dentry
);
164 static int rpc_clnt_skip_event(struct rpc_clnt
*clnt
, unsigned long event
)
166 if (clnt
->cl_program
->pipe_dir_name
== NULL
)
170 case RPC_PIPEFS_MOUNT
:
171 if (clnt
->cl_pipedir_objects
.pdh_dentry
!= NULL
)
173 if (atomic_read(&clnt
->cl_count
) == 0)
176 case RPC_PIPEFS_UMOUNT
:
177 if (clnt
->cl_pipedir_objects
.pdh_dentry
== NULL
)
184 static int __rpc_clnt_handle_event(struct rpc_clnt
*clnt
, unsigned long event
,
185 struct super_block
*sb
)
187 struct dentry
*dentry
;
190 case RPC_PIPEFS_MOUNT
:
191 dentry
= rpc_setup_pipedir_sb(sb
, clnt
);
195 return PTR_ERR(dentry
);
197 case RPC_PIPEFS_UMOUNT
:
198 __rpc_clnt_remove_pipedir(clnt
);
201 printk(KERN_ERR
"%s: unknown event: %ld\n", __func__
, event
);
207 static int __rpc_pipefs_event(struct rpc_clnt
*clnt
, unsigned long event
,
208 struct super_block
*sb
)
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
)
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
))
230 spin_unlock(&sn
->rpc_client_lock
);
233 spin_unlock(&sn
->rpc_client_lock
);
237 static int rpc_pipefs_event(struct notifier_block
*nb
, unsigned long event
,
240 struct super_block
*sb
= ptr
;
241 struct rpc_clnt
*clnt
;
244 while ((clnt
= rpc_get_client_for_event(sb
->s_fs_info
, event
))) {
245 error
= __rpc_pipefs_event(clnt
, event
, sb
);
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
);
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
;
306 rpc_clnt_debugfs_register(clnt
);
308 pipefs_sb
= rpc_get_sb_net(net
);
310 err
= rpc_setup_pipedir(pipefs_sb
, clnt
);
315 rpc_register_client(clnt
);
319 auth
= rpcauth_create(&auth_args
, clnt
);
321 dprintk("RPC: Couldn't create auth handle (flavor %u)\n",
328 pipefs_sb
= rpc_get_sb_net(net
);
329 rpc_unregister_client(clnt
);
330 __rpc_clnt_remove_pipedir(clnt
);
334 rpc_clnt_debugfs_unregister(clnt
);
338 static DEFINE_IDA(rpc_clids
);
340 void rpc_cleanup_clids(void)
342 ida_destroy(&rpc_clids
);
345 static int rpc_alloc_clid(struct rpc_clnt
*clnt
)
349 clid
= ida_simple_get(&rpc_clids
, 0, 0, GFP_KERNEL
);
352 clnt
->cl_clid
= clid
;
356 static void rpc_free_clid(struct rpc_clnt
*clnt
)
358 ida_simple_remove(&rpc_clids
, clnt
->cl_clid
);
361 static struct rpc_clnt
* rpc_new_client(const struct rpc_create_args
*args
,
362 struct rpc_xprt_switch
*xps
,
363 struct rpc_xprt
*xprt
,
364 struct rpc_clnt
*parent
)
366 const struct rpc_program
*program
= args
->program
;
367 const struct rpc_version
*version
;
368 struct rpc_clnt
*clnt
= NULL
;
369 const struct rpc_timeout
*timeout
;
370 const char *nodename
= args
->nodename
;
373 /* sanity check the name before trying to print it */
374 dprintk("RPC: creating %s client for %s (xprt %p)\n",
375 program
->name
, args
->servername
, xprt
);
382 if (args
->version
>= program
->nrvers
)
384 version
= program
->version
[args
->version
];
389 clnt
= kzalloc(sizeof(*clnt
), GFP_KERNEL
);
392 clnt
->cl_parent
= parent
? : clnt
;
394 err
= rpc_alloc_clid(clnt
);
398 clnt
->cl_procinfo
= version
->procs
;
399 clnt
->cl_maxproc
= version
->nrprocs
;
400 clnt
->cl_prog
= args
->prognumber
? : program
->number
;
401 clnt
->cl_vers
= version
->number
;
402 clnt
->cl_stats
= program
->stats
;
403 clnt
->cl_metrics
= rpc_alloc_iostats(clnt
);
404 rpc_init_pipe_dir_head(&clnt
->cl_pipedir_objects
);
406 if (clnt
->cl_metrics
== NULL
)
408 clnt
->cl_program
= program
;
409 INIT_LIST_HEAD(&clnt
->cl_tasks
);
410 spin_lock_init(&clnt
->cl_lock
);
412 timeout
= xprt
->timeout
;
413 if (args
->timeout
!= NULL
) {
414 memcpy(&clnt
->cl_timeout_default
, args
->timeout
,
415 sizeof(clnt
->cl_timeout_default
));
416 timeout
= &clnt
->cl_timeout_default
;
419 rpc_clnt_set_transport(clnt
, xprt
, timeout
);
420 xprt_iter_init(&clnt
->cl_xpi
, xps
);
421 xprt_switch_put(xps
);
423 clnt
->cl_rtt
= &clnt
->cl_rtt_default
;
424 rpc_init_rtt(&clnt
->cl_rtt_default
, clnt
->cl_timeout
->to_initval
);
426 atomic_set(&clnt
->cl_count
, 1);
428 if (nodename
== NULL
)
429 nodename
= utsname()->nodename
;
430 /* save the nodename */
431 rpc_clnt_set_nodename(clnt
, nodename
);
433 err
= rpc_client_register(clnt
, args
->authflavor
, args
->client_name
);
437 atomic_inc(&parent
->cl_count
);
441 rpc_free_iostats(clnt
->cl_metrics
);
449 xprt_switch_put(xps
);
454 static struct rpc_clnt
*rpc_create_xprt(struct rpc_create_args
*args
,
455 struct rpc_xprt
*xprt
)
457 struct rpc_clnt
*clnt
= NULL
;
458 struct rpc_xprt_switch
*xps
;
460 if (args
->bc_xprt
&& args
->bc_xprt
->xpt_bc_xps
) {
461 WARN_ON_ONCE(!(args
->protocol
& XPRT_TRANSPORT_BC
));
462 xps
= args
->bc_xprt
->xpt_bc_xps
;
463 xprt_switch_get(xps
);
465 xps
= xprt_switch_alloc(xprt
, GFP_KERNEL
);
468 return ERR_PTR(-ENOMEM
);
471 xprt_switch_get(xps
);
472 xprt
->bc_xprt
->xpt_bc_xps
= xps
;
475 clnt
= rpc_new_client(args
, xps
, xprt
, NULL
);
479 if (!(args
->flags
& RPC_CLNT_CREATE_NOPING
)) {
480 int err
= rpc_ping(clnt
);
482 rpc_shutdown_client(clnt
);
487 clnt
->cl_softrtry
= 1;
488 if (args
->flags
& RPC_CLNT_CREATE_HARDRTRY
)
489 clnt
->cl_softrtry
= 0;
491 if (args
->flags
& RPC_CLNT_CREATE_AUTOBIND
)
492 clnt
->cl_autobind
= 1;
493 if (args
->flags
& RPC_CLNT_CREATE_NO_RETRANS_TIMEOUT
)
494 clnt
->cl_noretranstimeo
= 1;
495 if (args
->flags
& RPC_CLNT_CREATE_DISCRTRY
)
496 clnt
->cl_discrtry
= 1;
497 if (!(args
->flags
& RPC_CLNT_CREATE_QUIET
))
504 * rpc_create - create an RPC client and transport with one call
505 * @args: rpc_clnt create argument structure
507 * Creates and initializes an RPC transport and an RPC client.
509 * It can ping the server in order to determine if it is up, and to see if
510 * it supports this program and version. RPC_CLNT_CREATE_NOPING disables
511 * this behavior so asynchronous tasks can also use rpc_create.
513 struct rpc_clnt
*rpc_create(struct rpc_create_args
*args
)
515 struct rpc_xprt
*xprt
;
516 struct xprt_create xprtargs
= {
518 .ident
= args
->protocol
,
519 .srcaddr
= args
->saddress
,
520 .dstaddr
= args
->address
,
521 .addrlen
= args
->addrsize
,
522 .servername
= args
->servername
,
523 .bc_xprt
= args
->bc_xprt
,
528 WARN_ON_ONCE(!(args
->protocol
& XPRT_TRANSPORT_BC
));
529 xprt
= args
->bc_xprt
->xpt_bc_xprt
;
532 return rpc_create_xprt(args
, xprt
);
536 if (args
->flags
& RPC_CLNT_CREATE_INFINITE_SLOTS
)
537 xprtargs
.flags
|= XPRT_CREATE_INFINITE_SLOTS
;
538 if (args
->flags
& RPC_CLNT_CREATE_NO_IDLE_TIMEOUT
)
539 xprtargs
.flags
|= XPRT_CREATE_NO_IDLE_TIMEOUT
;
541 * If the caller chooses not to specify a hostname, whip
542 * up a string representation of the passed-in address.
544 if (xprtargs
.servername
== NULL
) {
545 struct sockaddr_un
*sun
=
546 (struct sockaddr_un
*)args
->address
;
547 struct sockaddr_in
*sin
=
548 (struct sockaddr_in
*)args
->address
;
549 struct sockaddr_in6
*sin6
=
550 (struct sockaddr_in6
*)args
->address
;
552 servername
[0] = '\0';
553 switch (args
->address
->sa_family
) {
555 snprintf(servername
, sizeof(servername
), "%s",
559 snprintf(servername
, sizeof(servername
), "%pI4",
560 &sin
->sin_addr
.s_addr
);
563 snprintf(servername
, sizeof(servername
), "%pI6",
567 /* caller wants default server name, but
568 * address family isn't recognized. */
569 return ERR_PTR(-EINVAL
);
571 xprtargs
.servername
= servername
;
574 xprt
= xprt_create_transport(&xprtargs
);
576 return (struct rpc_clnt
*)xprt
;
579 * By default, kernel RPC client connects from a reserved port.
580 * CAP_NET_BIND_SERVICE will not be set for unprivileged requesters,
581 * but it is always enabled for rpciod, which handles the connect
585 if (args
->flags
& RPC_CLNT_CREATE_NONPRIVPORT
)
588 return rpc_create_xprt(args
, xprt
);
590 EXPORT_SYMBOL_GPL(rpc_create
);
593 * This function clones the RPC client structure. It allows us to share the
594 * same transport while varying parameters such as the authentication
597 static struct rpc_clnt
*__rpc_clone_client(struct rpc_create_args
*args
,
598 struct rpc_clnt
*clnt
)
600 struct rpc_xprt_switch
*xps
;
601 struct rpc_xprt
*xprt
;
602 struct rpc_clnt
*new;
607 xprt
= xprt_get(rcu_dereference(clnt
->cl_xprt
));
608 xps
= xprt_switch_get(rcu_dereference(clnt
->cl_xpi
.xpi_xpswitch
));
610 if (xprt
== NULL
|| xps
== NULL
) {
612 xprt_switch_put(xps
);
615 args
->servername
= xprt
->servername
;
616 args
->nodename
= clnt
->cl_nodename
;
618 new = rpc_new_client(args
, xps
, xprt
, clnt
);
624 /* Turn off autobind on clones */
625 new->cl_autobind
= 0;
626 new->cl_softrtry
= clnt
->cl_softrtry
;
627 new->cl_noretranstimeo
= clnt
->cl_noretranstimeo
;
628 new->cl_discrtry
= clnt
->cl_discrtry
;
629 new->cl_chatty
= clnt
->cl_chatty
;
633 dprintk("RPC: %s: returned error %d\n", __func__
, err
);
638 * rpc_clone_client - Clone an RPC client structure
640 * @clnt: RPC client whose parameters are copied
642 * Returns a fresh RPC client or an ERR_PTR.
644 struct rpc_clnt
*rpc_clone_client(struct rpc_clnt
*clnt
)
646 struct rpc_create_args args
= {
647 .program
= clnt
->cl_program
,
648 .prognumber
= clnt
->cl_prog
,
649 .version
= clnt
->cl_vers
,
650 .authflavor
= clnt
->cl_auth
->au_flavor
,
652 return __rpc_clone_client(&args
, clnt
);
654 EXPORT_SYMBOL_GPL(rpc_clone_client
);
657 * rpc_clone_client_set_auth - Clone an RPC client structure and set its auth
659 * @clnt: RPC client whose parameters are copied
660 * @flavor: security flavor for new client
662 * Returns a fresh RPC client or an ERR_PTR.
665 rpc_clone_client_set_auth(struct rpc_clnt
*clnt
, rpc_authflavor_t flavor
)
667 struct rpc_create_args args
= {
668 .program
= clnt
->cl_program
,
669 .prognumber
= clnt
->cl_prog
,
670 .version
= clnt
->cl_vers
,
671 .authflavor
= flavor
,
673 return __rpc_clone_client(&args
, clnt
);
675 EXPORT_SYMBOL_GPL(rpc_clone_client_set_auth
);
678 * rpc_switch_client_transport: switch the RPC transport on the fly
679 * @clnt: pointer to a struct rpc_clnt
680 * @args: pointer to the new transport arguments
681 * @timeout: pointer to the new timeout parameters
683 * This function allows the caller to switch the RPC transport for the
684 * rpc_clnt structure 'clnt' to allow it to connect to a mirrored NFS
685 * server, for instance. It assumes that the caller has ensured that
686 * there are no active RPC tasks by using some form of locking.
688 * Returns zero if "clnt" is now using the new xprt. Otherwise a
689 * negative errno is returned, and "clnt" continues to use the old
692 int rpc_switch_client_transport(struct rpc_clnt
*clnt
,
693 struct xprt_create
*args
,
694 const struct rpc_timeout
*timeout
)
696 const struct rpc_timeout
*old_timeo
;
697 rpc_authflavor_t pseudoflavor
;
698 struct rpc_xprt_switch
*xps
, *oldxps
;
699 struct rpc_xprt
*xprt
, *old
;
700 struct rpc_clnt
*parent
;
703 xprt
= xprt_create_transport(args
);
705 dprintk("RPC: failed to create new xprt for clnt %p\n",
707 return PTR_ERR(xprt
);
710 xps
= xprt_switch_alloc(xprt
, GFP_KERNEL
);
716 pseudoflavor
= clnt
->cl_auth
->au_flavor
;
718 old_timeo
= clnt
->cl_timeout
;
719 old
= rpc_clnt_set_transport(clnt
, xprt
, timeout
);
720 oldxps
= xprt_iter_xchg_switch(&clnt
->cl_xpi
, xps
);
722 rpc_unregister_client(clnt
);
723 __rpc_clnt_remove_pipedir(clnt
);
724 rpc_clnt_debugfs_unregister(clnt
);
727 * A new transport was created. "clnt" therefore
728 * becomes the root of a new cl_parent tree. clnt's
729 * children, if it has any, still point to the old xprt.
731 parent
= clnt
->cl_parent
;
732 clnt
->cl_parent
= clnt
;
735 * The old rpc_auth cache cannot be re-used. GSS
736 * contexts in particular are between a single
739 err
= rpc_client_register(clnt
, pseudoflavor
, NULL
);
745 rpc_release_client(parent
);
746 xprt_switch_put(oldxps
);
748 dprintk("RPC: replaced xprt for clnt %p\n", clnt
);
752 xps
= xprt_iter_xchg_switch(&clnt
->cl_xpi
, oldxps
);
753 rpc_clnt_set_transport(clnt
, old
, old_timeo
);
754 clnt
->cl_parent
= parent
;
755 rpc_client_register(clnt
, pseudoflavor
, NULL
);
756 xprt_switch_put(xps
);
758 dprintk("RPC: failed to switch xprt for clnt %p\n", clnt
);
761 EXPORT_SYMBOL_GPL(rpc_switch_client_transport
);
764 int rpc_clnt_xprt_iter_init(struct rpc_clnt
*clnt
, struct rpc_xprt_iter
*xpi
)
766 struct rpc_xprt_switch
*xps
;
769 xps
= xprt_switch_get(rcu_dereference(clnt
->cl_xpi
.xpi_xpswitch
));
773 xprt_iter_init_listall(xpi
, xps
);
774 xprt_switch_put(xps
);
779 * rpc_clnt_iterate_for_each_xprt - Apply a function to all transports
780 * @clnt: pointer to client
781 * @fn: function to apply
782 * @data: void pointer to function data
784 * Iterates through the list of RPC transports currently attached to the
785 * client and applies the function fn(clnt, xprt, data).
787 * On error, the iteration stops, and the function returns the error value.
789 int rpc_clnt_iterate_for_each_xprt(struct rpc_clnt
*clnt
,
790 int (*fn
)(struct rpc_clnt
*, struct rpc_xprt
*, void *),
793 struct rpc_xprt_iter xpi
;
796 ret
= rpc_clnt_xprt_iter_init(clnt
, &xpi
);
800 struct rpc_xprt
*xprt
= xprt_iter_get_next(&xpi
);
804 ret
= fn(clnt
, xprt
, data
);
809 xprt_iter_destroy(&xpi
);
812 EXPORT_SYMBOL_GPL(rpc_clnt_iterate_for_each_xprt
);
815 * Kill all tasks for the given client.
816 * XXX: kill their descendants as well?
818 void rpc_killall_tasks(struct rpc_clnt
*clnt
)
820 struct rpc_task
*rovr
;
823 if (list_empty(&clnt
->cl_tasks
))
825 dprintk("RPC: killing all tasks for client %p\n", clnt
);
827 * Spin lock all_tasks to prevent changes...
829 spin_lock(&clnt
->cl_lock
);
830 list_for_each_entry(rovr
, &clnt
->cl_tasks
, tk_task
) {
831 if (!RPC_IS_ACTIVATED(rovr
))
833 if (!(rovr
->tk_flags
& RPC_TASK_KILLED
)) {
834 rovr
->tk_flags
|= RPC_TASK_KILLED
;
835 rpc_exit(rovr
, -EIO
);
836 if (RPC_IS_QUEUED(rovr
))
837 rpc_wake_up_queued_task(rovr
->tk_waitqueue
,
841 spin_unlock(&clnt
->cl_lock
);
843 EXPORT_SYMBOL_GPL(rpc_killall_tasks
);
846 * Properly shut down an RPC client, terminating all outstanding
849 void rpc_shutdown_client(struct rpc_clnt
*clnt
)
853 dprintk_rcu("RPC: shutting down %s client for %s\n",
854 clnt
->cl_program
->name
,
855 rcu_dereference(clnt
->cl_xprt
)->servername
);
857 while (!list_empty(&clnt
->cl_tasks
)) {
858 rpc_killall_tasks(clnt
);
859 wait_event_timeout(destroy_wait
,
860 list_empty(&clnt
->cl_tasks
), 1*HZ
);
863 rpc_release_client(clnt
);
865 EXPORT_SYMBOL_GPL(rpc_shutdown_client
);
870 static struct rpc_clnt
*
871 rpc_free_client(struct rpc_clnt
*clnt
)
873 struct rpc_clnt
*parent
= NULL
;
875 dprintk_rcu("RPC: destroying %s client for %s\n",
876 clnt
->cl_program
->name
,
877 rcu_dereference(clnt
->cl_xprt
)->servername
);
878 if (clnt
->cl_parent
!= clnt
)
879 parent
= clnt
->cl_parent
;
880 rpc_clnt_debugfs_unregister(clnt
);
881 rpc_clnt_remove_pipedir(clnt
);
882 rpc_unregister_client(clnt
);
883 rpc_free_iostats(clnt
->cl_metrics
);
884 clnt
->cl_metrics
= NULL
;
885 xprt_put(rcu_dereference_raw(clnt
->cl_xprt
));
886 xprt_iter_destroy(&clnt
->cl_xpi
);
896 static struct rpc_clnt
*
897 rpc_free_auth(struct rpc_clnt
*clnt
)
899 if (clnt
->cl_auth
== NULL
)
900 return rpc_free_client(clnt
);
903 * Note: RPCSEC_GSS may need to send NULL RPC calls in order to
904 * release remaining GSS contexts. This mechanism ensures
905 * that it can do so safely.
907 atomic_inc(&clnt
->cl_count
);
908 rpcauth_release(clnt
->cl_auth
);
909 clnt
->cl_auth
= NULL
;
910 if (atomic_dec_and_test(&clnt
->cl_count
))
911 return rpc_free_client(clnt
);
916 * Release reference to the RPC client
919 rpc_release_client(struct rpc_clnt
*clnt
)
921 dprintk("RPC: rpc_release_client(%p)\n", clnt
);
924 if (list_empty(&clnt
->cl_tasks
))
925 wake_up(&destroy_wait
);
926 if (!atomic_dec_and_test(&clnt
->cl_count
))
928 clnt
= rpc_free_auth(clnt
);
929 } while (clnt
!= NULL
);
931 EXPORT_SYMBOL_GPL(rpc_release_client
);
934 * rpc_bind_new_program - bind a new RPC program to an existing client
935 * @old: old rpc_client
936 * @program: rpc program to set
937 * @vers: rpc program version
939 * Clones the rpc client and sets up a new RPC program. This is mainly
940 * of use for enabling different RPC programs to share the same transport.
941 * The Sun NFSv2/v3 ACL protocol can do this.
943 struct rpc_clnt
*rpc_bind_new_program(struct rpc_clnt
*old
,
944 const struct rpc_program
*program
,
947 struct rpc_create_args args
= {
949 .prognumber
= program
->number
,
951 .authflavor
= old
->cl_auth
->au_flavor
,
953 struct rpc_clnt
*clnt
;
956 clnt
= __rpc_clone_client(&args
, old
);
959 err
= rpc_ping(clnt
);
961 rpc_shutdown_client(clnt
);
967 EXPORT_SYMBOL_GPL(rpc_bind_new_program
);
969 void rpc_task_release_transport(struct rpc_task
*task
)
971 struct rpc_xprt
*xprt
= task
->tk_xprt
;
974 task
->tk_xprt
= NULL
;
978 EXPORT_SYMBOL_GPL(rpc_task_release_transport
);
980 void rpc_task_release_client(struct rpc_task
*task
)
982 struct rpc_clnt
*clnt
= task
->tk_client
;
985 /* Remove from client task list */
986 spin_lock(&clnt
->cl_lock
);
987 list_del(&task
->tk_task
);
988 spin_unlock(&clnt
->cl_lock
);
989 task
->tk_client
= NULL
;
991 rpc_release_client(clnt
);
993 rpc_task_release_transport(task
);
997 void rpc_task_set_transport(struct rpc_task
*task
, struct rpc_clnt
*clnt
)
1000 task
->tk_xprt
= xprt_iter_get_next(&clnt
->cl_xpi
);
1004 void rpc_task_set_client(struct rpc_task
*task
, struct rpc_clnt
*clnt
)
1008 rpc_task_set_transport(task
, clnt
);
1009 task
->tk_client
= clnt
;
1010 atomic_inc(&clnt
->cl_count
);
1011 if (clnt
->cl_softrtry
)
1012 task
->tk_flags
|= RPC_TASK_SOFT
;
1013 if (clnt
->cl_noretranstimeo
)
1014 task
->tk_flags
|= RPC_TASK_NO_RETRANS_TIMEOUT
;
1015 if (atomic_read(&clnt
->cl_swapper
))
1016 task
->tk_flags
|= RPC_TASK_SWAPPER
;
1017 /* Add to the client's list of all tasks */
1018 spin_lock(&clnt
->cl_lock
);
1019 list_add_tail(&task
->tk_task
, &clnt
->cl_tasks
);
1020 spin_unlock(&clnt
->cl_lock
);
1025 rpc_task_set_rpc_message(struct rpc_task
*task
, const struct rpc_message
*msg
)
1028 task
->tk_msg
.rpc_proc
= msg
->rpc_proc
;
1029 task
->tk_msg
.rpc_argp
= msg
->rpc_argp
;
1030 task
->tk_msg
.rpc_resp
= msg
->rpc_resp
;
1031 if (msg
->rpc_cred
!= NULL
)
1032 task
->tk_msg
.rpc_cred
= get_rpccred(msg
->rpc_cred
);
1037 * Default callback for async RPC calls
1040 rpc_default_callback(struct rpc_task
*task
, void *data
)
1044 static const struct rpc_call_ops rpc_default_ops
= {
1045 .rpc_call_done
= rpc_default_callback
,
1049 * rpc_run_task - Allocate a new RPC task, then run rpc_execute against it
1050 * @task_setup_data: pointer to task initialisation data
1052 struct rpc_task
*rpc_run_task(const struct rpc_task_setup
*task_setup_data
)
1054 struct rpc_task
*task
;
1056 task
= rpc_new_task(task_setup_data
);
1058 rpc_task_set_client(task
, task_setup_data
->rpc_client
);
1059 rpc_task_set_rpc_message(task
, task_setup_data
->rpc_message
);
1061 if (task
->tk_action
== NULL
)
1062 rpc_call_start(task
);
1064 atomic_inc(&task
->tk_count
);
1068 EXPORT_SYMBOL_GPL(rpc_run_task
);
1071 * rpc_call_sync - Perform a synchronous RPC call
1072 * @clnt: pointer to RPC client
1073 * @msg: RPC call parameters
1074 * @flags: RPC call flags
1076 int rpc_call_sync(struct rpc_clnt
*clnt
, const struct rpc_message
*msg
, int flags
)
1078 struct rpc_task
*task
;
1079 struct rpc_task_setup task_setup_data
= {
1082 .callback_ops
= &rpc_default_ops
,
1087 WARN_ON_ONCE(flags
& RPC_TASK_ASYNC
);
1088 if (flags
& RPC_TASK_ASYNC
) {
1089 rpc_release_calldata(task_setup_data
.callback_ops
,
1090 task_setup_data
.callback_data
);
1094 task
= rpc_run_task(&task_setup_data
);
1096 return PTR_ERR(task
);
1097 status
= task
->tk_status
;
1101 EXPORT_SYMBOL_GPL(rpc_call_sync
);
1104 * rpc_call_async - Perform an asynchronous RPC call
1105 * @clnt: pointer to RPC client
1106 * @msg: RPC call parameters
1107 * @flags: RPC call flags
1108 * @tk_ops: RPC call ops
1109 * @data: user call data
1112 rpc_call_async(struct rpc_clnt
*clnt
, const struct rpc_message
*msg
, int flags
,
1113 const struct rpc_call_ops
*tk_ops
, void *data
)
1115 struct rpc_task
*task
;
1116 struct rpc_task_setup task_setup_data
= {
1119 .callback_ops
= tk_ops
,
1120 .callback_data
= data
,
1121 .flags
= flags
|RPC_TASK_ASYNC
,
1124 task
= rpc_run_task(&task_setup_data
);
1126 return PTR_ERR(task
);
1130 EXPORT_SYMBOL_GPL(rpc_call_async
);
1132 #if defined(CONFIG_SUNRPC_BACKCHANNEL)
1134 * rpc_run_bc_task - Allocate a new RPC task for backchannel use, then run
1135 * rpc_execute against it
1138 struct rpc_task
*rpc_run_bc_task(struct rpc_rqst
*req
)
1140 struct rpc_task
*task
;
1141 struct rpc_task_setup task_setup_data
= {
1142 .callback_ops
= &rpc_default_ops
,
1143 .flags
= RPC_TASK_SOFTCONN
|
1144 RPC_TASK_NO_RETRANS_TIMEOUT
,
1147 dprintk("RPC: rpc_run_bc_task req= %p\n", req
);
1149 * Create an rpc_task to send the data
1151 task
= rpc_new_task(&task_setup_data
);
1152 xprt_init_bc_request(req
, task
);
1154 task
->tk_action
= call_bc_transmit
;
1155 atomic_inc(&task
->tk_count
);
1156 WARN_ON_ONCE(atomic_read(&task
->tk_count
) != 2);
1159 dprintk("RPC: rpc_run_bc_task: task= %p\n", task
);
1162 #endif /* CONFIG_SUNRPC_BACKCHANNEL */
1165 rpc_call_start(struct rpc_task
*task
)
1167 task
->tk_action
= call_start
;
1169 EXPORT_SYMBOL_GPL(rpc_call_start
);
1172 * rpc_peeraddr - extract remote peer address from clnt's xprt
1173 * @clnt: RPC client structure
1174 * @buf: target buffer
1175 * @bufsize: length of target buffer
1177 * Returns the number of bytes that are actually in the stored address.
1179 size_t rpc_peeraddr(struct rpc_clnt
*clnt
, struct sockaddr
*buf
, size_t bufsize
)
1182 struct rpc_xprt
*xprt
;
1185 xprt
= rcu_dereference(clnt
->cl_xprt
);
1187 bytes
= xprt
->addrlen
;
1188 if (bytes
> bufsize
)
1190 memcpy(buf
, &xprt
->addr
, bytes
);
1195 EXPORT_SYMBOL_GPL(rpc_peeraddr
);
1198 * rpc_peeraddr2str - return remote peer address in printable format
1199 * @clnt: RPC client structure
1200 * @format: address format
1202 * NB: the lifetime of the memory referenced by the returned pointer is
1203 * the same as the rpc_xprt itself. As long as the caller uses this
1204 * pointer, it must hold the RCU read lock.
1206 const char *rpc_peeraddr2str(struct rpc_clnt
*clnt
,
1207 enum rpc_display_format_t format
)
1209 struct rpc_xprt
*xprt
;
1211 xprt
= rcu_dereference(clnt
->cl_xprt
);
1213 if (xprt
->address_strings
[format
] != NULL
)
1214 return xprt
->address_strings
[format
];
1216 return "unprintable";
1218 EXPORT_SYMBOL_GPL(rpc_peeraddr2str
);
1220 static const struct sockaddr_in rpc_inaddr_loopback
= {
1221 .sin_family
= AF_INET
,
1222 .sin_addr
.s_addr
= htonl(INADDR_ANY
),
1225 static const struct sockaddr_in6 rpc_in6addr_loopback
= {
1226 .sin6_family
= AF_INET6
,
1227 .sin6_addr
= IN6ADDR_ANY_INIT
,
1231 * Try a getsockname() on a connected datagram socket. Using a
1232 * connected datagram socket prevents leaving a socket in TIME_WAIT.
1233 * This conserves the ephemeral port number space.
1235 * Returns zero and fills in "buf" if successful; otherwise, a
1236 * negative errno is returned.
1238 static int rpc_sockname(struct net
*net
, struct sockaddr
*sap
, size_t salen
,
1239 struct sockaddr
*buf
)
1241 struct socket
*sock
;
1244 err
= __sock_create(net
, sap
->sa_family
,
1245 SOCK_DGRAM
, IPPROTO_UDP
, &sock
, 1);
1247 dprintk("RPC: can't create UDP socket (%d)\n", err
);
1251 switch (sap
->sa_family
) {
1253 err
= kernel_bind(sock
,
1254 (struct sockaddr
*)&rpc_inaddr_loopback
,
1255 sizeof(rpc_inaddr_loopback
));
1258 err
= kernel_bind(sock
,
1259 (struct sockaddr
*)&rpc_in6addr_loopback
,
1260 sizeof(rpc_in6addr_loopback
));
1263 err
= -EAFNOSUPPORT
;
1267 dprintk("RPC: can't bind UDP socket (%d)\n", err
);
1271 err
= kernel_connect(sock
, sap
, salen
, 0);
1273 dprintk("RPC: can't connect UDP socket (%d)\n", err
);
1277 err
= kernel_getsockname(sock
, buf
);
1279 dprintk("RPC: getsockname failed (%d)\n", err
);
1284 if (buf
->sa_family
== AF_INET6
) {
1285 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*)buf
;
1286 sin6
->sin6_scope_id
= 0;
1288 dprintk("RPC: %s succeeded\n", __func__
);
1297 * Scraping a connected socket failed, so we don't have a useable
1298 * local address. Fallback: generate an address that will prevent
1299 * the server from calling us back.
1301 * Returns zero and fills in "buf" if successful; otherwise, a
1302 * negative errno is returned.
1304 static int rpc_anyaddr(int family
, struct sockaddr
*buf
, size_t buflen
)
1308 if (buflen
< sizeof(rpc_inaddr_loopback
))
1310 memcpy(buf
, &rpc_inaddr_loopback
,
1311 sizeof(rpc_inaddr_loopback
));
1314 if (buflen
< sizeof(rpc_in6addr_loopback
))
1316 memcpy(buf
, &rpc_in6addr_loopback
,
1317 sizeof(rpc_in6addr_loopback
));
1320 dprintk("RPC: %s: address family not supported\n",
1322 return -EAFNOSUPPORT
;
1324 dprintk("RPC: %s: succeeded\n", __func__
);
1329 * rpc_localaddr - discover local endpoint address for an RPC client
1330 * @clnt: RPC client structure
1331 * @buf: target buffer
1332 * @buflen: size of target buffer, in bytes
1334 * Returns zero and fills in "buf" and "buflen" if successful;
1335 * otherwise, a negative errno is returned.
1337 * This works even if the underlying transport is not currently connected,
1338 * or if the upper layer never previously provided a source address.
1340 * The result of this function call is transient: multiple calls in
1341 * succession may give different results, depending on how local
1342 * networking configuration changes over time.
1344 int rpc_localaddr(struct rpc_clnt
*clnt
, struct sockaddr
*buf
, size_t buflen
)
1346 struct sockaddr_storage address
;
1347 struct sockaddr
*sap
= (struct sockaddr
*)&address
;
1348 struct rpc_xprt
*xprt
;
1354 xprt
= rcu_dereference(clnt
->cl_xprt
);
1355 salen
= xprt
->addrlen
;
1356 memcpy(sap
, &xprt
->addr
, salen
);
1357 net
= get_net(xprt
->xprt_net
);
1360 rpc_set_port(sap
, 0);
1361 err
= rpc_sockname(net
, sap
, salen
, buf
);
1364 /* Couldn't discover local address, return ANYADDR */
1365 return rpc_anyaddr(sap
->sa_family
, buf
, buflen
);
1368 EXPORT_SYMBOL_GPL(rpc_localaddr
);
1371 rpc_setbufsize(struct rpc_clnt
*clnt
, unsigned int sndsize
, unsigned int rcvsize
)
1373 struct rpc_xprt
*xprt
;
1376 xprt
= rcu_dereference(clnt
->cl_xprt
);
1377 if (xprt
->ops
->set_buffer_size
)
1378 xprt
->ops
->set_buffer_size(xprt
, sndsize
, rcvsize
);
1381 EXPORT_SYMBOL_GPL(rpc_setbufsize
);
1384 * rpc_net_ns - Get the network namespace for this RPC client
1385 * @clnt: RPC client to query
1388 struct net
*rpc_net_ns(struct rpc_clnt
*clnt
)
1393 ret
= rcu_dereference(clnt
->cl_xprt
)->xprt_net
;
1397 EXPORT_SYMBOL_GPL(rpc_net_ns
);
1400 * rpc_max_payload - Get maximum payload size for a transport, in bytes
1401 * @clnt: RPC client to query
1403 * For stream transports, this is one RPC record fragment (see RFC
1404 * 1831), as we don't support multi-record requests yet. For datagram
1405 * transports, this is the size of an IP packet minus the IP, UDP, and
1408 size_t rpc_max_payload(struct rpc_clnt
*clnt
)
1413 ret
= rcu_dereference(clnt
->cl_xprt
)->max_payload
;
1417 EXPORT_SYMBOL_GPL(rpc_max_payload
);
1420 * rpc_max_bc_payload - Get maximum backchannel payload size, in bytes
1421 * @clnt: RPC client to query
1423 size_t rpc_max_bc_payload(struct rpc_clnt
*clnt
)
1425 struct rpc_xprt
*xprt
;
1429 xprt
= rcu_dereference(clnt
->cl_xprt
);
1430 ret
= xprt
->ops
->bc_maxpayload(xprt
);
1434 EXPORT_SYMBOL_GPL(rpc_max_bc_payload
);
1437 * rpc_force_rebind - force transport to check that remote port is unchanged
1438 * @clnt: client to rebind
1441 void rpc_force_rebind(struct rpc_clnt
*clnt
)
1443 if (clnt
->cl_autobind
) {
1445 xprt_clear_bound(rcu_dereference(clnt
->cl_xprt
));
1449 EXPORT_SYMBOL_GPL(rpc_force_rebind
);
1452 * Restart an (async) RPC call from the call_prepare state.
1453 * Usually called from within the exit handler.
1456 rpc_restart_call_prepare(struct rpc_task
*task
)
1458 if (RPC_ASSASSINATED(task
))
1460 task
->tk_action
= call_start
;
1461 task
->tk_status
= 0;
1462 if (task
->tk_ops
->rpc_call_prepare
!= NULL
)
1463 task
->tk_action
= rpc_prepare_task
;
1466 EXPORT_SYMBOL_GPL(rpc_restart_call_prepare
);
1469 * Restart an (async) RPC call. Usually called from within the
1473 rpc_restart_call(struct rpc_task
*task
)
1475 if (RPC_ASSASSINATED(task
))
1477 task
->tk_action
= call_start
;
1478 task
->tk_status
= 0;
1481 EXPORT_SYMBOL_GPL(rpc_restart_call
);
1484 *rpc_proc_name(const struct rpc_task
*task
)
1486 const struct rpc_procinfo
*proc
= task
->tk_msg
.rpc_proc
;
1490 return proc
->p_name
;
1500 * Other FSM states can be visited zero or more times, but
1501 * this state is visited exactly once for each RPC.
1504 call_start(struct rpc_task
*task
)
1506 struct rpc_clnt
*clnt
= task
->tk_client
;
1507 int idx
= task
->tk_msg
.rpc_proc
->p_statidx
;
1509 trace_rpc_request(task
);
1510 dprintk("RPC: %5u call_start %s%d proc %s (%s)\n", task
->tk_pid
,
1511 clnt
->cl_program
->name
, clnt
->cl_vers
,
1512 rpc_proc_name(task
),
1513 (RPC_IS_ASYNC(task
) ? "async" : "sync"));
1515 /* Increment call count (version might not be valid for ping) */
1516 if (clnt
->cl_program
->version
[clnt
->cl_vers
])
1517 clnt
->cl_program
->version
[clnt
->cl_vers
]->counts
[idx
]++;
1518 clnt
->cl_stats
->rpccnt
++;
1519 task
->tk_action
= call_reserve
;
1520 rpc_task_set_transport(task
, clnt
);
1524 * 1. Reserve an RPC call slot
1527 call_reserve(struct rpc_task
*task
)
1529 dprint_status(task
);
1531 task
->tk_status
= 0;
1532 task
->tk_action
= call_reserveresult
;
1536 static void call_retry_reserve(struct rpc_task
*task
);
1539 * 1b. Grok the result of xprt_reserve()
1542 call_reserveresult(struct rpc_task
*task
)
1544 int status
= task
->tk_status
;
1546 dprint_status(task
);
1549 * After a call to xprt_reserve(), we must have either
1550 * a request slot or else an error status.
1552 task
->tk_status
= 0;
1554 if (task
->tk_rqstp
) {
1555 task
->tk_action
= call_refresh
;
1559 printk(KERN_ERR
"%s: status=%d, but no request slot, exiting\n",
1561 rpc_exit(task
, -EIO
);
1566 * Even though there was an error, we may have acquired
1567 * a request slot somehow. Make sure not to leak it.
1569 if (task
->tk_rqstp
) {
1570 printk(KERN_ERR
"%s: status=%d, request allocated anyway\n",
1577 rpc_delay(task
, HZ
>> 2);
1579 case -EAGAIN
: /* woken up; retry */
1580 task
->tk_action
= call_retry_reserve
;
1582 case -EIO
: /* probably a shutdown */
1585 printk(KERN_ERR
"%s: unrecognized error %d, exiting\n",
1589 rpc_exit(task
, status
);
1593 * 1c. Retry reserving an RPC call slot
1596 call_retry_reserve(struct rpc_task
*task
)
1598 dprint_status(task
);
1600 task
->tk_status
= 0;
1601 task
->tk_action
= call_reserveresult
;
1602 xprt_retry_reserve(task
);
1606 * 2. Bind and/or refresh the credentials
1609 call_refresh(struct rpc_task
*task
)
1611 dprint_status(task
);
1613 task
->tk_action
= call_refreshresult
;
1614 task
->tk_status
= 0;
1615 task
->tk_client
->cl_stats
->rpcauthrefresh
++;
1616 rpcauth_refreshcred(task
);
1620 * 2a. Process the results of a credential refresh
1623 call_refreshresult(struct rpc_task
*task
)
1625 int status
= task
->tk_status
;
1627 dprint_status(task
);
1629 task
->tk_status
= 0;
1630 task
->tk_action
= call_refresh
;
1633 if (rpcauth_uptodatecred(task
)) {
1634 task
->tk_action
= call_allocate
;
1637 /* Use rate-limiting and a max number of retries if refresh
1638 * had status 0 but failed to update the cred.
1642 rpc_delay(task
, 3*HZ
);
1648 if (!task
->tk_cred_retry
)
1650 task
->tk_cred_retry
--;
1651 dprintk("RPC: %5u %s: retry refresh creds\n",
1652 task
->tk_pid
, __func__
);
1655 dprintk("RPC: %5u %s: refresh creds failed with error %d\n",
1656 task
->tk_pid
, __func__
, status
);
1657 rpc_exit(task
, status
);
1661 * 2b. Allocate the buffer. For details, see sched.c:rpc_malloc.
1662 * (Note: buffer memory is freed in xprt_release).
1665 call_allocate(struct rpc_task
*task
)
1667 unsigned int slack
= task
->tk_rqstp
->rq_cred
->cr_auth
->au_cslack
;
1668 struct rpc_rqst
*req
= task
->tk_rqstp
;
1669 struct rpc_xprt
*xprt
= req
->rq_xprt
;
1670 const struct rpc_procinfo
*proc
= task
->tk_msg
.rpc_proc
;
1673 dprint_status(task
);
1675 task
->tk_status
= 0;
1676 task
->tk_action
= call_encode
;
1681 if (proc
->p_proc
!= 0) {
1682 BUG_ON(proc
->p_arglen
== 0);
1683 if (proc
->p_decode
!= NULL
)
1684 BUG_ON(proc
->p_replen
== 0);
1688 * Calculate the size (in quads) of the RPC call
1689 * and reply headers, and convert both values
1692 req
->rq_callsize
= RPC_CALLHDRSIZE
+ (slack
<< 1) + proc
->p_arglen
;
1693 req
->rq_callsize
<<= 2;
1694 req
->rq_rcvsize
= RPC_REPHDRSIZE
+ slack
+ proc
->p_replen
;
1695 req
->rq_rcvsize
<<= 2;
1697 status
= xprt
->ops
->buf_alloc(task
);
1698 xprt_inject_disconnect(xprt
);
1701 if (status
!= -ENOMEM
) {
1702 rpc_exit(task
, status
);
1706 dprintk("RPC: %5u rpc_buffer allocation failed\n", task
->tk_pid
);
1708 if (RPC_IS_ASYNC(task
) || !fatal_signal_pending(current
)) {
1709 task
->tk_action
= call_allocate
;
1710 rpc_delay(task
, HZ
>>4);
1714 rpc_exit(task
, -ERESTARTSYS
);
1718 rpc_task_need_encode(struct rpc_task
*task
)
1720 return test_bit(RPC_TASK_NEED_XMIT
, &task
->tk_runstate
) == 0 &&
1721 (!(task
->tk_flags
& RPC_TASK_SENT
) ||
1722 !(task
->tk_flags
& RPC_TASK_NO_RETRANS_TIMEOUT
) ||
1723 xprt_request_need_retransmit(task
));
1727 rpc_xdr_encode(struct rpc_task
*task
)
1729 struct rpc_rqst
*req
= task
->tk_rqstp
;
1733 dprint_status(task
);
1735 xdr_buf_init(&req
->rq_snd_buf
,
1738 xdr_buf_init(&req
->rq_rcv_buf
,
1741 req
->rq_bytes_sent
= 0;
1743 p
= rpc_encode_header(task
);
1745 printk(KERN_INFO
"RPC: couldn't encode RPC header, exit EIO\n");
1746 rpc_exit(task
, -EIO
);
1750 encode
= task
->tk_msg
.rpc_proc
->p_encode
;
1754 task
->tk_status
= rpcauth_wrap_req(task
, encode
, req
, p
,
1755 task
->tk_msg
.rpc_argp
);
1756 if (task
->tk_status
== 0)
1757 xprt_request_prepare(req
);
1761 * 3. Encode arguments of an RPC call
1764 call_encode(struct rpc_task
*task
)
1766 if (!rpc_task_need_encode(task
))
1768 /* Encode here so that rpcsec_gss can use correct sequence number. */
1769 rpc_xdr_encode(task
);
1770 /* Did the encode result in an error condition? */
1771 if (task
->tk_status
!= 0) {
1772 /* Was the error nonfatal? */
1773 if (task
->tk_status
== -EAGAIN
|| task
->tk_status
== -ENOMEM
)
1774 rpc_delay(task
, HZ
>> 4);
1776 rpc_exit(task
, task
->tk_status
);
1780 /* Add task to reply queue before transmission to avoid races */
1781 if (rpc_reply_expected(task
))
1782 xprt_request_enqueue_receive(task
);
1783 xprt_request_enqueue_transmit(task
);
1785 task
->tk_action
= call_bind
;
1789 * 4. Get the server port number if not yet set
1792 call_bind(struct rpc_task
*task
)
1794 struct rpc_xprt
*xprt
= task
->tk_rqstp
->rq_xprt
;
1796 dprint_status(task
);
1798 task
->tk_action
= call_connect
;
1799 if (!xprt_bound(xprt
)) {
1800 task
->tk_action
= call_bind_status
;
1801 task
->tk_timeout
= xprt
->bind_timeout
;
1802 xprt
->ops
->rpcbind(task
);
1807 * 4a. Sort out bind result
1810 call_bind_status(struct rpc_task
*task
)
1814 if (task
->tk_status
>= 0) {
1815 dprint_status(task
);
1816 task
->tk_status
= 0;
1817 task
->tk_action
= call_connect
;
1821 trace_rpc_bind_status(task
);
1822 switch (task
->tk_status
) {
1824 dprintk("RPC: %5u rpcbind out of memory\n", task
->tk_pid
);
1825 rpc_delay(task
, HZ
>> 2);
1828 dprintk("RPC: %5u remote rpcbind: RPC program/version "
1829 "unavailable\n", task
->tk_pid
);
1830 /* fail immediately if this is an RPC ping */
1831 if (task
->tk_msg
.rpc_proc
->p_proc
== 0) {
1832 status
= -EOPNOTSUPP
;
1835 if (task
->tk_rebind_retry
== 0)
1837 task
->tk_rebind_retry
--;
1838 rpc_delay(task
, 3*HZ
);
1841 dprintk("RPC: %5u rpcbind request timed out\n",
1845 /* server doesn't support any rpcbind version we know of */
1846 dprintk("RPC: %5u unrecognized remote rpcbind service\n",
1849 case -EPROTONOSUPPORT
:
1850 dprintk("RPC: %5u remote rpcbind version unavailable, retrying\n",
1853 case -ECONNREFUSED
: /* connection problems */
1863 dprintk("RPC: %5u remote rpcbind unreachable: %d\n",
1864 task
->tk_pid
, task
->tk_status
);
1865 if (!RPC_IS_SOFTCONN(task
)) {
1866 rpc_delay(task
, 5*HZ
);
1869 status
= task
->tk_status
;
1872 dprintk("RPC: %5u unrecognized rpcbind error (%d)\n",
1873 task
->tk_pid
, -task
->tk_status
);
1876 rpc_exit(task
, status
);
1880 task
->tk_status
= 0;
1881 task
->tk_action
= call_timeout
;
1885 * 4b. Connect to the RPC server
1888 call_connect(struct rpc_task
*task
)
1890 struct rpc_xprt
*xprt
= task
->tk_rqstp
->rq_xprt
;
1892 dprintk("RPC: %5u call_connect xprt %p %s connected\n",
1894 (xprt_connected(xprt
) ? "is" : "is not"));
1896 task
->tk_action
= call_transmit
;
1897 if (!xprt_connected(xprt
)) {
1898 task
->tk_action
= call_connect_status
;
1899 if (task
->tk_status
< 0)
1901 if (task
->tk_flags
& RPC_TASK_NOCONNECT
) {
1902 rpc_exit(task
, -ENOTCONN
);
1910 * 4c. Sort out connect result
1913 call_connect_status(struct rpc_task
*task
)
1915 struct rpc_clnt
*clnt
= task
->tk_client
;
1916 int status
= task
->tk_status
;
1918 dprint_status(task
);
1920 trace_rpc_connect_status(task
);
1921 task
->tk_status
= 0;
1924 /* A positive refusal suggests a rebind is needed. */
1925 if (RPC_IS_SOFTCONN(task
))
1927 if (clnt
->cl_autobind
) {
1928 rpc_force_rebind(clnt
);
1929 task
->tk_action
= call_bind
;
1941 xprt_conditional_disconnect(task
->tk_rqstp
->rq_xprt
,
1942 task
->tk_rqstp
->rq_connect_cookie
);
1943 if (RPC_IS_SOFTCONN(task
))
1945 /* retry with existing socket, after a delay */
1946 rpc_delay(task
, 3*HZ
);
1949 /* Check for timeouts before looping back to call_bind */
1951 task
->tk_action
= call_timeout
;
1954 clnt
->cl_stats
->netreconn
++;
1955 task
->tk_action
= call_transmit
;
1958 rpc_exit(task
, status
);
1962 * 5. Transmit the RPC request, and wait for reply
1965 call_transmit(struct rpc_task
*task
)
1967 dprint_status(task
);
1969 task
->tk_status
= 0;
1970 if (test_bit(RPC_TASK_NEED_XMIT
, &task
->tk_runstate
)) {
1971 if (!xprt_prepare_transmit(task
))
1973 xprt_transmit(task
);
1975 task
->tk_action
= call_transmit_status
;
1976 xprt_end_transmit(task
);
1980 * 5a. Handle cleanup after a transmission
1983 call_transmit_status(struct rpc_task
*task
)
1985 task
->tk_action
= call_status
;
1988 * Common case: success. Force the compiler to put this
1991 if (task
->tk_status
== 0) {
1992 xprt_request_wait_receive(task
);
1996 switch (task
->tk_status
) {
1998 dprint_status(task
);
2001 task
->tk_status
= 0;
2002 task
->tk_action
= call_encode
;
2005 * Special cases: if we've been waiting on the
2006 * socket's write_space() callback, or if the
2007 * socket just returned a connection error,
2008 * then hold onto the transport lock.
2011 rpc_delay(task
, HZ
>>2);
2015 task
->tk_action
= call_transmit
;
2016 task
->tk_status
= 0;
2024 if (RPC_IS_SOFTCONN(task
)) {
2025 if (!task
->tk_msg
.rpc_proc
->p_proc
)
2026 trace_xprt_ping(task
->tk_xprt
,
2028 rpc_exit(task
, task
->tk_status
);
2041 #if defined(CONFIG_SUNRPC_BACKCHANNEL)
2043 * 5b. Send the backchannel RPC reply. On error, drop the reply. In
2044 * addition, disconnect on connectivity errors.
2047 call_bc_transmit(struct rpc_task
*task
)
2049 struct rpc_rqst
*req
= task
->tk_rqstp
;
2051 if (rpc_task_need_encode(task
))
2052 xprt_request_enqueue_transmit(task
);
2053 if (!test_bit(RPC_TASK_NEED_XMIT
, &task
->tk_runstate
))
2056 if (!xprt_prepare_transmit(task
))
2059 if (task
->tk_status
< 0) {
2060 printk(KERN_NOTICE
"RPC: Could not send backchannel reply "
2061 "error: %d\n", task
->tk_status
);
2065 xprt_transmit(task
);
2067 xprt_end_transmit(task
);
2068 dprint_status(task
);
2069 switch (task
->tk_status
) {
2086 * Problem reaching the server. Disconnect and let the
2087 * forechannel reestablish the connection. The server will
2088 * have to retransmit the backchannel request and we'll
2089 * reprocess it. Since these ops are idempotent, there's no
2090 * need to cache our reply at this time.
2092 printk(KERN_NOTICE
"RPC: Could not send backchannel reply "
2093 "error: %d\n", task
->tk_status
);
2094 xprt_conditional_disconnect(req
->rq_xprt
,
2095 req
->rq_connect_cookie
);
2099 * We were unable to reply and will have to drop the
2100 * request. The server should reconnect and retransmit.
2102 WARN_ON_ONCE(task
->tk_status
== -EAGAIN
);
2103 printk(KERN_NOTICE
"RPC: Could not send backchannel reply "
2104 "error: %d\n", task
->tk_status
);
2108 rpc_wake_up_queued_task(&req
->rq_xprt
->pending
, task
);
2110 task
->tk_action
= rpc_exit_task
;
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
;
2126 if (!task
->tk_msg
.rpc_proc
->p_proc
)
2127 trace_xprt_ping(task
->tk_xprt
, task
->tk_status
);
2129 dprint_status(task
);
2131 status
= task
->tk_status
;
2133 task
->tk_action
= call_decode
;
2137 trace_rpc_call_status(task
);
2138 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
);
2156 task
->tk_action
= call_timeout
;
2161 rpc_force_rebind(clnt
);
2164 rpc_delay(task
, 3*HZ
);
2169 task
->tk_action
= call_encode
;
2172 /* shutdown or soft timeout */
2173 rpc_exit(task
, status
);
2176 if (clnt
->cl_chatty
)
2177 printk("%s: RPC call returned error %d\n",
2178 clnt
->cl_program
->name
, -status
);
2179 rpc_exit(task
, status
);
2184 * 6a. Handle RPC timeout
2185 * We do not release the request slot, so we keep using the
2186 * same XID for all retransmits.
2189 call_timeout(struct rpc_task
*task
)
2191 struct rpc_clnt
*clnt
= task
->tk_client
;
2193 if (xprt_adjust_timeout(task
->tk_rqstp
) == 0) {
2194 dprintk("RPC: %5u call_timeout (minor)\n", task
->tk_pid
);
2198 dprintk("RPC: %5u call_timeout (major)\n", task
->tk_pid
);
2199 task
->tk_timeouts
++;
2201 if (RPC_IS_SOFTCONN(task
)) {
2202 rpc_exit(task
, -ETIMEDOUT
);
2205 if (RPC_IS_SOFT(task
)) {
2206 if (clnt
->cl_chatty
) {
2207 printk(KERN_NOTICE
"%s: server %s not responding, timed out\n",
2208 clnt
->cl_program
->name
,
2209 task
->tk_xprt
->servername
);
2211 if (task
->tk_flags
& RPC_TASK_TIMEOUT
)
2212 rpc_exit(task
, -ETIMEDOUT
);
2214 rpc_exit(task
, -EIO
);
2218 if (!(task
->tk_flags
& RPC_CALL_MAJORSEEN
)) {
2219 task
->tk_flags
|= RPC_CALL_MAJORSEEN
;
2220 if (clnt
->cl_chatty
) {
2221 printk(KERN_NOTICE
"%s: server %s not responding, still trying\n",
2222 clnt
->cl_program
->name
,
2223 task
->tk_xprt
->servername
);
2226 rpc_force_rebind(clnt
);
2228 * Did our request time out due to an RPCSEC_GSS out-of-sequence
2229 * event? RFC2203 requires the server to drop all such requests.
2231 rpcauth_invalcred(task
);
2234 task
->tk_action
= call_encode
;
2235 task
->tk_status
= 0;
2239 * 7. Decode the RPC reply
2242 call_decode(struct rpc_task
*task
)
2244 struct rpc_clnt
*clnt
= task
->tk_client
;
2245 struct rpc_rqst
*req
= task
->tk_rqstp
;
2246 kxdrdproc_t decode
= task
->tk_msg
.rpc_proc
->p_decode
;
2249 dprint_status(task
);
2252 task
->tk_action
= rpc_exit_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_encode
;
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
))
2293 task
->tk_action
= rpc_exit_task
;
2295 task
->tk_status
= rpcauth_unwrap_resp(task
, decode
, req
, p
,
2296 task
->tk_msg
.rpc_resp
);
2298 dprintk("RPC: %5u call_decode result %d\n", task
->tk_pid
,
2302 task
->tk_status
= 0;
2303 /* Note: rpc_verify_header() may have freed the RPC slot */
2304 if (task
->tk_rqstp
== req
) {
2305 req
->rq_reply_bytes_recvd
= req
->rq_rcv_buf
.len
= 0;
2306 if (task
->tk_client
->cl_discrtry
)
2307 xprt_conditional_disconnect(req
->rq_xprt
,
2308 req
->rq_connect_cookie
);
2313 rpc_encode_header(struct rpc_task
*task
)
2315 struct rpc_clnt
*clnt
= task
->tk_client
;
2316 struct rpc_rqst
*req
= task
->tk_rqstp
;
2317 __be32
*p
= req
->rq_svec
[0].iov_base
;
2319 /* FIXME: check buffer size? */
2321 p
= xprt_skip_transport_header(req
->rq_xprt
, p
);
2322 *p
++ = req
->rq_xid
; /* XID */
2323 *p
++ = htonl(RPC_CALL
); /* CALL */
2324 *p
++ = htonl(RPC_VERSION
); /* RPC version */
2325 *p
++ = htonl(clnt
->cl_prog
); /* program number */
2326 *p
++ = htonl(clnt
->cl_vers
); /* program version */
2327 *p
++ = htonl(task
->tk_msg
.rpc_proc
->p_proc
); /* procedure */
2328 p
= rpcauth_marshcred(task
, p
);
2329 req
->rq_slen
= xdr_adjust_iovec(&req
->rq_svec
[0], p
);
2334 rpc_verify_header(struct rpc_task
*task
)
2336 struct rpc_clnt
*clnt
= task
->tk_client
;
2337 struct kvec
*iov
= &task
->tk_rqstp
->rq_rcv_buf
.head
[0];
2338 int len
= task
->tk_rqstp
->rq_rcv_buf
.len
>> 2;
2339 __be32
*p
= iov
->iov_base
;
2341 int error
= -EACCES
;
2343 if ((task
->tk_rqstp
->rq_rcv_buf
.len
& 3) != 0) {
2344 /* RFC-1014 says that the representation of XDR data must be a
2345 * multiple of four bytes
2346 * - if it isn't pointer subtraction in the NFS client may give
2349 dprintk("RPC: %5u %s: XDR representation not a multiple of"
2350 " 4 bytes: 0x%x\n", task
->tk_pid
, __func__
,
2351 task
->tk_rqstp
->rq_rcv_buf
.len
);
2358 p
+= 1; /* skip XID */
2359 if ((n
= ntohl(*p
++)) != RPC_REPLY
) {
2360 dprintk("RPC: %5u %s: not an RPC reply: %x\n",
2361 task
->tk_pid
, __func__
, n
);
2366 if ((n
= ntohl(*p
++)) != RPC_MSG_ACCEPTED
) {
2369 switch ((n
= ntohl(*p
++))) {
2370 case RPC_AUTH_ERROR
:
2373 dprintk("RPC: %5u %s: RPC call version mismatch!\n",
2374 task
->tk_pid
, __func__
);
2375 error
= -EPROTONOSUPPORT
;
2378 dprintk("RPC: %5u %s: RPC call rejected, "
2379 "unknown error: %x\n",
2380 task
->tk_pid
, __func__
, n
);
2386 switch ((n
= ntohl(*p
++))) {
2387 case RPC_AUTH_REJECTEDCRED
:
2388 case RPC_AUTH_REJECTEDVERF
:
2389 case RPCSEC_GSS_CREDPROBLEM
:
2390 case RPCSEC_GSS_CTXPROBLEM
:
2391 if (!task
->tk_cred_retry
)
2393 task
->tk_cred_retry
--;
2394 dprintk("RPC: %5u %s: retry stale creds\n",
2395 task
->tk_pid
, __func__
);
2396 rpcauth_invalcred(task
);
2397 /* Ensure we obtain a new XID! */
2399 task
->tk_action
= call_reserve
;
2401 case RPC_AUTH_BADCRED
:
2402 case RPC_AUTH_BADVERF
:
2403 /* possibly garbled cred/verf? */
2404 if (!task
->tk_garb_retry
)
2406 task
->tk_garb_retry
--;
2407 dprintk("RPC: %5u %s: retry garbled creds\n",
2408 task
->tk_pid
, __func__
);
2409 task
->tk_action
= call_encode
;
2411 case RPC_AUTH_TOOWEAK
:
2412 printk(KERN_NOTICE
"RPC: server %s requires stronger "
2413 "authentication.\n",
2414 task
->tk_xprt
->servername
);
2417 dprintk("RPC: %5u %s: unknown auth error: %x\n",
2418 task
->tk_pid
, __func__
, n
);
2421 dprintk("RPC: %5u %s: call rejected %d\n",
2422 task
->tk_pid
, __func__
, n
);
2425 p
= rpcauth_checkverf(task
, p
);
2428 dprintk("RPC: %5u %s: auth check failed with %d\n",
2429 task
->tk_pid
, __func__
, error
);
2430 goto out_garbage
; /* bad verifier, retry */
2432 len
= p
- (__be32
*)iov
->iov_base
- 1;
2435 switch ((n
= ntohl(*p
++))) {
2438 case RPC_PROG_UNAVAIL
:
2439 dprintk("RPC: %5u %s: program %u is unsupported "
2440 "by server %s\n", task
->tk_pid
, __func__
,
2441 (unsigned int)clnt
->cl_prog
,
2442 task
->tk_xprt
->servername
);
2443 error
= -EPFNOSUPPORT
;
2445 case RPC_PROG_MISMATCH
:
2446 dprintk("RPC: %5u %s: program %u, version %u unsupported "
2447 "by server %s\n", task
->tk_pid
, __func__
,
2448 (unsigned int)clnt
->cl_prog
,
2449 (unsigned int)clnt
->cl_vers
,
2450 task
->tk_xprt
->servername
);
2451 error
= -EPROTONOSUPPORT
;
2453 case RPC_PROC_UNAVAIL
:
2454 dprintk("RPC: %5u %s: proc %s unsupported by program %u, "
2455 "version %u on server %s\n",
2456 task
->tk_pid
, __func__
,
2457 rpc_proc_name(task
),
2458 clnt
->cl_prog
, clnt
->cl_vers
,
2459 task
->tk_xprt
->servername
);
2460 error
= -EOPNOTSUPP
;
2462 case RPC_GARBAGE_ARGS
:
2463 dprintk("RPC: %5u %s: server saw garbage\n",
2464 task
->tk_pid
, __func__
);
2467 dprintk("RPC: %5u %s: server accept status: %x\n",
2468 task
->tk_pid
, __func__
, n
);
2473 clnt
->cl_stats
->rpcgarbage
++;
2474 if (task
->tk_garb_retry
) {
2475 task
->tk_garb_retry
--;
2476 dprintk("RPC: %5u %s: retrying\n",
2477 task
->tk_pid
, __func__
);
2478 task
->tk_action
= call_encode
;
2480 return ERR_PTR(-EAGAIN
);
2483 rpc_exit(task
, error
);
2484 dprintk("RPC: %5u %s: call failed with error %d\n", task
->tk_pid
,
2486 return ERR_PTR(error
);
2488 dprintk("RPC: %5u %s: server reply was truncated.\n", task
->tk_pid
,
2493 static void rpcproc_encode_null(struct rpc_rqst
*rqstp
, struct xdr_stream
*xdr
,
2498 static int rpcproc_decode_null(struct rpc_rqst
*rqstp
, struct xdr_stream
*xdr
,
2504 static const struct rpc_procinfo rpcproc_null
= {
2505 .p_encode
= rpcproc_encode_null
,
2506 .p_decode
= rpcproc_decode_null
,
2509 static int rpc_ping(struct rpc_clnt
*clnt
)
2511 struct rpc_message msg
= {
2512 .rpc_proc
= &rpcproc_null
,
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 connect_timeout
;
2696 unsigned long reconnect_timeout
;
2697 unsigned char resvport
;
2701 xps
= xprt_switch_get(rcu_dereference(clnt
->cl_xpi
.xpi_xpswitch
));
2702 xprt
= xprt_iter_xprt(&clnt
->cl_xpi
);
2703 if (xps
== NULL
|| xprt
== NULL
) {
2707 resvport
= xprt
->resvport
;
2708 connect_timeout
= xprt
->connect_timeout
;
2709 reconnect_timeout
= xprt
->max_reconnect_timeout
;
2712 xprt
= xprt_create_transport(xprtargs
);
2714 ret
= PTR_ERR(xprt
);
2715 goto out_put_switch
;
2717 xprt
->resvport
= resvport
;
2718 if (xprt
->ops
->set_connect_timeout
!= NULL
)
2719 xprt
->ops
->set_connect_timeout(xprt
,
2723 rpc_xprt_switch_set_roundrobin(xps
);
2725 ret
= setup(clnt
, xps
, xprt
, data
);
2729 rpc_xprt_switch_add_xprt(xps
, xprt
);
2733 xprt_switch_put(xps
);
2736 EXPORT_SYMBOL_GPL(rpc_clnt_add_xprt
);
2738 struct connect_timeout_data
{
2739 unsigned long connect_timeout
;
2740 unsigned long reconnect_timeout
;
2744 rpc_xprt_set_connect_timeout(struct rpc_clnt
*clnt
,
2745 struct rpc_xprt
*xprt
,
2748 struct connect_timeout_data
*timeo
= data
;
2750 if (xprt
->ops
->set_connect_timeout
)
2751 xprt
->ops
->set_connect_timeout(xprt
,
2752 timeo
->connect_timeout
,
2753 timeo
->reconnect_timeout
);
2758 rpc_set_connect_timeout(struct rpc_clnt
*clnt
,
2759 unsigned long connect_timeout
,
2760 unsigned long reconnect_timeout
)
2762 struct connect_timeout_data timeout
= {
2763 .connect_timeout
= connect_timeout
,
2764 .reconnect_timeout
= reconnect_timeout
,
2766 rpc_clnt_iterate_for_each_xprt(clnt
,
2767 rpc_xprt_set_connect_timeout
,
2770 EXPORT_SYMBOL_GPL(rpc_set_connect_timeout
);
2772 void rpc_clnt_xprt_switch_put(struct rpc_clnt
*clnt
)
2775 xprt_switch_put(rcu_dereference(clnt
->cl_xpi
.xpi_xpswitch
));
2778 EXPORT_SYMBOL_GPL(rpc_clnt_xprt_switch_put
);
2780 void rpc_clnt_xprt_switch_add_xprt(struct rpc_clnt
*clnt
, struct rpc_xprt
*xprt
)
2783 rpc_xprt_switch_add_xprt(rcu_dereference(clnt
->cl_xpi
.xpi_xpswitch
),
2787 EXPORT_SYMBOL_GPL(rpc_clnt_xprt_switch_add_xprt
);
2789 bool rpc_clnt_xprt_switch_has_addr(struct rpc_clnt
*clnt
,
2790 const struct sockaddr
*sap
)
2792 struct rpc_xprt_switch
*xps
;
2796 xps
= rcu_dereference(clnt
->cl_xpi
.xpi_xpswitch
);
2797 ret
= rpc_xprt_switch_has_addr(xps
, sap
);
2801 EXPORT_SYMBOL_GPL(rpc_clnt_xprt_switch_has_addr
);
2803 #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
2804 static void rpc_show_header(void)
2806 printk(KERN_INFO
"-pid- flgs status -client- --rqstp- "
2807 "-timeout ---ops--\n");
2810 static void rpc_show_task(const struct rpc_clnt
*clnt
,
2811 const struct rpc_task
*task
)
2813 const char *rpc_waitq
= "none";
2815 if (RPC_IS_QUEUED(task
))
2816 rpc_waitq
= rpc_qname(task
->tk_waitqueue
);
2818 printk(KERN_INFO
"%5u %04x %6d %8p %8p %8ld %8p %sv%u %s a:%ps q:%s\n",
2819 task
->tk_pid
, task
->tk_flags
, task
->tk_status
,
2820 clnt
, task
->tk_rqstp
, task
->tk_timeout
, task
->tk_ops
,
2821 clnt
->cl_program
->name
, clnt
->cl_vers
, rpc_proc_name(task
),
2822 task
->tk_action
, rpc_waitq
);
2825 void rpc_show_tasks(struct net
*net
)
2827 struct rpc_clnt
*clnt
;
2828 struct rpc_task
*task
;
2830 struct sunrpc_net
*sn
= net_generic(net
, sunrpc_net_id
);
2832 spin_lock(&sn
->rpc_client_lock
);
2833 list_for_each_entry(clnt
, &sn
->all_clients
, cl_clients
) {
2834 spin_lock(&clnt
->cl_lock
);
2835 list_for_each_entry(task
, &clnt
->cl_tasks
, tk_task
) {
2840 rpc_show_task(clnt
, task
);
2842 spin_unlock(&clnt
->cl_lock
);
2844 spin_unlock(&sn
->rpc_client_lock
);
2848 #if IS_ENABLED(CONFIG_SUNRPC_SWAP)
2850 rpc_clnt_swap_activate_callback(struct rpc_clnt
*clnt
,
2851 struct rpc_xprt
*xprt
,
2854 return xprt_enable_swap(xprt
);
2858 rpc_clnt_swap_activate(struct rpc_clnt
*clnt
)
2860 if (atomic_inc_return(&clnt
->cl_swapper
) == 1)
2861 return rpc_clnt_iterate_for_each_xprt(clnt
,
2862 rpc_clnt_swap_activate_callback
, NULL
);
2865 EXPORT_SYMBOL_GPL(rpc_clnt_swap_activate
);
2868 rpc_clnt_swap_deactivate_callback(struct rpc_clnt
*clnt
,
2869 struct rpc_xprt
*xprt
,
2872 xprt_disable_swap(xprt
);
2877 rpc_clnt_swap_deactivate(struct rpc_clnt
*clnt
)
2879 if (atomic_dec_if_positive(&clnt
->cl_swapper
) == 0)
2880 rpc_clnt_iterate_for_each_xprt(clnt
,
2881 rpc_clnt_swap_deactivate_callback
, NULL
);
2883 EXPORT_SYMBOL_GPL(rpc_clnt_swap_deactivate
);
2884 #endif /* CONFIG_SUNRPC_SWAP */