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