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