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