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