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