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