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