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