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