xref: /linux/net/sunrpc/clnt.c (revision 1d658336b05f8697d6445834f8867f8ad5e4f735)
1 /*
2  *  linux/net/sunrpc/clnt.c
3  *
4  *  This file contains the high-level RPC interface.
5  *  It is modeled as a finite state machine to support both synchronous
6  *  and asynchronous requests.
7  *
8  *  -	RPC header generation and argument serialization.
9  *  -	Credential refresh.
10  *  -	TCP connect handling.
11  *  -	Retry of operation when it is suspected the operation failed because
12  *	of uid squashing on the server, or when the credentials were stale
13  *	and need to be refreshed, or when a packet was damaged in transit.
14  *	This may be have to be moved to the VFS layer.
15  *
16  *  Copyright (C) 1992,1993 Rick Sladkey <jrs@world.std.com>
17  *  Copyright (C) 1995,1996 Olaf Kirch <okir@monad.swb.de>
18  */
19 
20 
21 #include <linux/module.h>
22 #include <linux/types.h>
23 #include <linux/kallsyms.h>
24 #include <linux/mm.h>
25 #include <linux/namei.h>
26 #include <linux/mount.h>
27 #include <linux/slab.h>
28 #include <linux/utsname.h>
29 #include <linux/workqueue.h>
30 #include <linux/in.h>
31 #include <linux/in6.h>
32 #include <linux/un.h>
33 #include <linux/rcupdate.h>
34 
35 #include <linux/sunrpc/clnt.h>
36 #include <linux/sunrpc/addr.h>
37 #include <linux/sunrpc/rpc_pipe_fs.h>
38 #include <linux/sunrpc/metrics.h>
39 #include <linux/sunrpc/bc_xprt.h>
40 #include <trace/events/sunrpc.h>
41 
42 #include "sunrpc.h"
43 #include "netns.h"
44 
45 #ifdef RPC_DEBUG
46 # define RPCDBG_FACILITY	RPCDBG_CALL
47 #endif
48 
49 #define dprint_status(t)					\
50 	dprintk("RPC: %5u %s (status %d)\n", t->tk_pid,		\
51 			__func__, t->tk_status)
52 
53 /*
54  * All RPC clients are linked into this list
55  */
56 
57 static DECLARE_WAIT_QUEUE_HEAD(destroy_wait);
58 
59 
60 static void	call_start(struct rpc_task *task);
61 static void	call_reserve(struct rpc_task *task);
62 static void	call_reserveresult(struct rpc_task *task);
63 static void	call_allocate(struct rpc_task *task);
64 static void	call_decode(struct rpc_task *task);
65 static void	call_bind(struct rpc_task *task);
66 static void	call_bind_status(struct rpc_task *task);
67 static void	call_transmit(struct rpc_task *task);
68 #if defined(CONFIG_SUNRPC_BACKCHANNEL)
69 static void	call_bc_transmit(struct rpc_task *task);
70 #endif /* CONFIG_SUNRPC_BACKCHANNEL */
71 static void	call_status(struct rpc_task *task);
72 static void	call_transmit_status(struct rpc_task *task);
73 static void	call_refresh(struct rpc_task *task);
74 static void	call_refreshresult(struct rpc_task *task);
75 static void	call_timeout(struct rpc_task *task);
76 static void	call_connect(struct rpc_task *task);
77 static void	call_connect_status(struct rpc_task *task);
78 
79 static __be32	*rpc_encode_header(struct rpc_task *task);
80 static __be32	*rpc_verify_header(struct rpc_task *task);
81 static int	rpc_ping(struct rpc_clnt *clnt);
82 
83 static void rpc_register_client(struct rpc_clnt *clnt)
84 {
85 	struct net *net = rpc_net_ns(clnt);
86 	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
87 
88 	spin_lock(&sn->rpc_client_lock);
89 	list_add(&clnt->cl_clients, &sn->all_clients);
90 	spin_unlock(&sn->rpc_client_lock);
91 }
92 
93 static void rpc_unregister_client(struct rpc_clnt *clnt)
94 {
95 	struct net *net = rpc_net_ns(clnt);
96 	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
97 
98 	spin_lock(&sn->rpc_client_lock);
99 	list_del(&clnt->cl_clients);
100 	spin_unlock(&sn->rpc_client_lock);
101 }
102 
103 static void __rpc_clnt_remove_pipedir(struct rpc_clnt *clnt)
104 {
105 	if (clnt->cl_dentry) {
106 		if (clnt->cl_auth && clnt->cl_auth->au_ops->pipes_destroy)
107 			clnt->cl_auth->au_ops->pipes_destroy(clnt->cl_auth);
108 		rpc_remove_client_dir(clnt->cl_dentry);
109 	}
110 	clnt->cl_dentry = NULL;
111 }
112 
113 static void rpc_clnt_remove_pipedir(struct rpc_clnt *clnt)
114 {
115 	struct net *net = rpc_net_ns(clnt);
116 	struct super_block *pipefs_sb;
117 
118 	pipefs_sb = rpc_get_sb_net(net);
119 	if (pipefs_sb) {
120 		__rpc_clnt_remove_pipedir(clnt);
121 		rpc_put_sb_net(net);
122 	}
123 }
124 
125 static struct dentry *rpc_setup_pipedir_sb(struct super_block *sb,
126 				    struct rpc_clnt *clnt,
127 				    const char *dir_name)
128 {
129 	static uint32_t clntid;
130 	char name[15];
131 	struct qstr q = { .name = name };
132 	struct dentry *dir, *dentry;
133 	int error;
134 
135 	dir = rpc_d_lookup_sb(sb, dir_name);
136 	if (dir == NULL) {
137 		pr_info("RPC: pipefs directory doesn't exist: %s\n", dir_name);
138 		return dir;
139 	}
140 	for (;;) {
141 		q.len = snprintf(name, sizeof(name), "clnt%x", (unsigned int)clntid++);
142 		name[sizeof(name) - 1] = '\0';
143 		q.hash = full_name_hash(q.name, q.len);
144 		dentry = rpc_create_client_dir(dir, &q, clnt);
145 		if (!IS_ERR(dentry))
146 			break;
147 		error = PTR_ERR(dentry);
148 		if (error != -EEXIST) {
149 			printk(KERN_INFO "RPC: Couldn't create pipefs entry"
150 					" %s/%s, error %d\n",
151 					dir_name, name, error);
152 			break;
153 		}
154 	}
155 	dput(dir);
156 	return dentry;
157 }
158 
159 static int
160 rpc_setup_pipedir(struct rpc_clnt *clnt, const char *dir_name)
161 {
162 	struct net *net = rpc_net_ns(clnt);
163 	struct super_block *pipefs_sb;
164 	struct dentry *dentry;
165 
166 	clnt->cl_dentry = NULL;
167 	if (dir_name == NULL)
168 		return 0;
169 	pipefs_sb = rpc_get_sb_net(net);
170 	if (!pipefs_sb)
171 		return 0;
172 	dentry = rpc_setup_pipedir_sb(pipefs_sb, clnt, dir_name);
173 	rpc_put_sb_net(net);
174 	if (IS_ERR(dentry))
175 		return PTR_ERR(dentry);
176 	clnt->cl_dentry = dentry;
177 	return 0;
178 }
179 
180 static inline int rpc_clnt_skip_event(struct rpc_clnt *clnt, unsigned long event)
181 {
182 	if (((event == RPC_PIPEFS_MOUNT) && clnt->cl_dentry) ||
183 	    ((event == RPC_PIPEFS_UMOUNT) && !clnt->cl_dentry))
184 		return 1;
185 	return 0;
186 }
187 
188 static int __rpc_clnt_handle_event(struct rpc_clnt *clnt, unsigned long event,
189 				   struct super_block *sb)
190 {
191 	struct dentry *dentry;
192 	int err = 0;
193 
194 	switch (event) {
195 	case RPC_PIPEFS_MOUNT:
196 		dentry = rpc_setup_pipedir_sb(sb, clnt,
197 					      clnt->cl_program->pipe_dir_name);
198 		if (!dentry)
199 			return -ENOENT;
200 		if (IS_ERR(dentry))
201 			return PTR_ERR(dentry);
202 		clnt->cl_dentry = dentry;
203 		if (clnt->cl_auth->au_ops->pipes_create) {
204 			err = clnt->cl_auth->au_ops->pipes_create(clnt->cl_auth);
205 			if (err)
206 				__rpc_clnt_remove_pipedir(clnt);
207 		}
208 		break;
209 	case RPC_PIPEFS_UMOUNT:
210 		__rpc_clnt_remove_pipedir(clnt);
211 		break;
212 	default:
213 		printk(KERN_ERR "%s: unknown event: %ld\n", __func__, event);
214 		return -ENOTSUPP;
215 	}
216 	return err;
217 }
218 
219 static int __rpc_pipefs_event(struct rpc_clnt *clnt, unsigned long event,
220 				struct super_block *sb)
221 {
222 	int error = 0;
223 
224 	for (;; clnt = clnt->cl_parent) {
225 		if (!rpc_clnt_skip_event(clnt, event))
226 			error = __rpc_clnt_handle_event(clnt, event, sb);
227 		if (error || clnt == clnt->cl_parent)
228 			break;
229 	}
230 	return error;
231 }
232 
233 static struct rpc_clnt *rpc_get_client_for_event(struct net *net, int event)
234 {
235 	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
236 	struct rpc_clnt *clnt;
237 
238 	spin_lock(&sn->rpc_client_lock);
239 	list_for_each_entry(clnt, &sn->all_clients, cl_clients) {
240 		if (clnt->cl_program->pipe_dir_name == NULL)
241 			continue;
242 		if (rpc_clnt_skip_event(clnt, event))
243 			continue;
244 		if (atomic_inc_not_zero(&clnt->cl_count) == 0)
245 			continue;
246 		spin_unlock(&sn->rpc_client_lock);
247 		return clnt;
248 	}
249 	spin_unlock(&sn->rpc_client_lock);
250 	return NULL;
251 }
252 
253 static int rpc_pipefs_event(struct notifier_block *nb, unsigned long event,
254 			    void *ptr)
255 {
256 	struct super_block *sb = ptr;
257 	struct rpc_clnt *clnt;
258 	int error = 0;
259 
260 	while ((clnt = rpc_get_client_for_event(sb->s_fs_info, event))) {
261 		error = __rpc_pipefs_event(clnt, event, sb);
262 		rpc_release_client(clnt);
263 		if (error)
264 			break;
265 	}
266 	return error;
267 }
268 
269 static struct notifier_block rpc_clients_block = {
270 	.notifier_call	= rpc_pipefs_event,
271 	.priority	= SUNRPC_PIPEFS_RPC_PRIO,
272 };
273 
274 int rpc_clients_notifier_register(void)
275 {
276 	return rpc_pipefs_notifier_register(&rpc_clients_block);
277 }
278 
279 void rpc_clients_notifier_unregister(void)
280 {
281 	return rpc_pipefs_notifier_unregister(&rpc_clients_block);
282 }
283 
284 static void rpc_clnt_set_nodename(struct rpc_clnt *clnt, const char *nodename)
285 {
286 	clnt->cl_nodelen = strlen(nodename);
287 	if (clnt->cl_nodelen > UNX_MAXNODENAME)
288 		clnt->cl_nodelen = UNX_MAXNODENAME;
289 	memcpy(clnt->cl_nodename, nodename, clnt->cl_nodelen);
290 }
291 
292 static struct rpc_clnt * rpc_new_client(const struct rpc_create_args *args, struct rpc_xprt *xprt)
293 {
294 	const struct rpc_program *program = args->program;
295 	const struct rpc_version *version;
296 	struct rpc_clnt		*clnt = NULL;
297 	struct rpc_auth		*auth;
298 	int err;
299 
300 	/* sanity check the name before trying to print it */
301 	dprintk("RPC:       creating %s client for %s (xprt %p)\n",
302 			program->name, args->servername, xprt);
303 
304 	err = rpciod_up();
305 	if (err)
306 		goto out_no_rpciod;
307 	err = -EINVAL;
308 	if (!xprt)
309 		goto out_no_xprt;
310 
311 	if (args->version >= program->nrvers)
312 		goto out_err;
313 	version = program->version[args->version];
314 	if (version == NULL)
315 		goto out_err;
316 
317 	err = -ENOMEM;
318 	clnt = kzalloc(sizeof(*clnt), GFP_KERNEL);
319 	if (!clnt)
320 		goto out_err;
321 	clnt->cl_parent = clnt;
322 
323 	rcu_assign_pointer(clnt->cl_xprt, xprt);
324 	clnt->cl_procinfo = version->procs;
325 	clnt->cl_maxproc  = version->nrprocs;
326 	clnt->cl_protname = program->name;
327 	clnt->cl_prog     = args->prognumber ? : program->number;
328 	clnt->cl_vers     = version->number;
329 	clnt->cl_stats    = program->stats;
330 	clnt->cl_metrics  = rpc_alloc_iostats(clnt);
331 	err = -ENOMEM;
332 	if (clnt->cl_metrics == NULL)
333 		goto out_no_stats;
334 	clnt->cl_program  = program;
335 	INIT_LIST_HEAD(&clnt->cl_tasks);
336 	spin_lock_init(&clnt->cl_lock);
337 
338 	if (!xprt_bound(xprt))
339 		clnt->cl_autobind = 1;
340 
341 	clnt->cl_timeout = xprt->timeout;
342 	if (args->timeout != NULL) {
343 		memcpy(&clnt->cl_timeout_default, args->timeout,
344 				sizeof(clnt->cl_timeout_default));
345 		clnt->cl_timeout = &clnt->cl_timeout_default;
346 	}
347 
348 	clnt->cl_rtt = &clnt->cl_rtt_default;
349 	rpc_init_rtt(&clnt->cl_rtt_default, clnt->cl_timeout->to_initval);
350 	clnt->cl_principal = NULL;
351 	if (args->client_name) {
352 		clnt->cl_principal = kstrdup(args->client_name, GFP_KERNEL);
353 		if (!clnt->cl_principal)
354 			goto out_no_principal;
355 	}
356 
357 	atomic_set(&clnt->cl_count, 1);
358 
359 	err = rpc_setup_pipedir(clnt, program->pipe_dir_name);
360 	if (err < 0)
361 		goto out_no_path;
362 
363 	auth = rpcauth_create(args->authflavor, clnt);
364 	if (IS_ERR(auth)) {
365 		printk(KERN_INFO "RPC: Couldn't create auth handle (flavor %u)\n",
366 				args->authflavor);
367 		err = PTR_ERR(auth);
368 		goto out_no_auth;
369 	}
370 
371 	/* save the nodename */
372 	rpc_clnt_set_nodename(clnt, utsname()->nodename);
373 	rpc_register_client(clnt);
374 	return clnt;
375 
376 out_no_auth:
377 	rpc_clnt_remove_pipedir(clnt);
378 out_no_path:
379 	kfree(clnt->cl_principal);
380 out_no_principal:
381 	rpc_free_iostats(clnt->cl_metrics);
382 out_no_stats:
383 	kfree(clnt);
384 out_err:
385 	xprt_put(xprt);
386 out_no_xprt:
387 	rpciod_down();
388 out_no_rpciod:
389 	return ERR_PTR(err);
390 }
391 
392 /**
393  * rpc_create - create an RPC client and transport with one call
394  * @args: rpc_clnt create argument structure
395  *
396  * Creates and initializes an RPC transport and an RPC client.
397  *
398  * It can ping the server in order to determine if it is up, and to see if
399  * it supports this program and version.  RPC_CLNT_CREATE_NOPING disables
400  * this behavior so asynchronous tasks can also use rpc_create.
401  */
402 struct rpc_clnt *rpc_create(struct rpc_create_args *args)
403 {
404 	struct rpc_xprt *xprt;
405 	struct rpc_clnt *clnt;
406 	struct xprt_create xprtargs = {
407 		.net = args->net,
408 		.ident = args->protocol,
409 		.srcaddr = args->saddress,
410 		.dstaddr = args->address,
411 		.addrlen = args->addrsize,
412 		.servername = args->servername,
413 		.bc_xprt = args->bc_xprt,
414 	};
415 	char servername[48];
416 
417 	if (args->flags & RPC_CLNT_CREATE_INFINITE_SLOTS)
418 		xprtargs.flags |= XPRT_CREATE_INFINITE_SLOTS;
419 	if (args->flags & RPC_CLNT_CREATE_NO_IDLE_TIMEOUT)
420 		xprtargs.flags |= XPRT_CREATE_NO_IDLE_TIMEOUT;
421 	/*
422 	 * If the caller chooses not to specify a hostname, whip
423 	 * up a string representation of the passed-in address.
424 	 */
425 	if (xprtargs.servername == NULL) {
426 		struct sockaddr_un *sun =
427 				(struct sockaddr_un *)args->address;
428 		struct sockaddr_in *sin =
429 				(struct sockaddr_in *)args->address;
430 		struct sockaddr_in6 *sin6 =
431 				(struct sockaddr_in6 *)args->address;
432 
433 		servername[0] = '\0';
434 		switch (args->address->sa_family) {
435 		case AF_LOCAL:
436 			snprintf(servername, sizeof(servername), "%s",
437 				 sun->sun_path);
438 			break;
439 		case AF_INET:
440 			snprintf(servername, sizeof(servername), "%pI4",
441 				 &sin->sin_addr.s_addr);
442 			break;
443 		case AF_INET6:
444 			snprintf(servername, sizeof(servername), "%pI6",
445 				 &sin6->sin6_addr);
446 			break;
447 		default:
448 			/* caller wants default server name, but
449 			 * address family isn't recognized. */
450 			return ERR_PTR(-EINVAL);
451 		}
452 		xprtargs.servername = servername;
453 	}
454 
455 	xprt = xprt_create_transport(&xprtargs);
456 	if (IS_ERR(xprt))
457 		return (struct rpc_clnt *)xprt;
458 
459 	/*
460 	 * By default, kernel RPC client connects from a reserved port.
461 	 * CAP_NET_BIND_SERVICE will not be set for unprivileged requesters,
462 	 * but it is always enabled for rpciod, which handles the connect
463 	 * operation.
464 	 */
465 	xprt->resvport = 1;
466 	if (args->flags & RPC_CLNT_CREATE_NONPRIVPORT)
467 		xprt->resvport = 0;
468 
469 	clnt = rpc_new_client(args, xprt);
470 	if (IS_ERR(clnt))
471 		return clnt;
472 
473 	if (!(args->flags & RPC_CLNT_CREATE_NOPING)) {
474 		int err = rpc_ping(clnt);
475 		if (err != 0) {
476 			rpc_shutdown_client(clnt);
477 			return ERR_PTR(err);
478 		}
479 	}
480 
481 	clnt->cl_softrtry = 1;
482 	if (args->flags & RPC_CLNT_CREATE_HARDRTRY)
483 		clnt->cl_softrtry = 0;
484 
485 	if (args->flags & RPC_CLNT_CREATE_AUTOBIND)
486 		clnt->cl_autobind = 1;
487 	if (args->flags & RPC_CLNT_CREATE_DISCRTRY)
488 		clnt->cl_discrtry = 1;
489 	if (!(args->flags & RPC_CLNT_CREATE_QUIET))
490 		clnt->cl_chatty = 1;
491 
492 	return clnt;
493 }
494 EXPORT_SYMBOL_GPL(rpc_create);
495 
496 /*
497  * This function clones the RPC client structure. It allows us to share the
498  * same transport while varying parameters such as the authentication
499  * flavour.
500  */
501 static struct rpc_clnt *__rpc_clone_client(struct rpc_create_args *args,
502 					   struct rpc_clnt *clnt)
503 {
504 	struct rpc_xprt *xprt;
505 	struct rpc_clnt *new;
506 	int err;
507 
508 	err = -ENOMEM;
509 	rcu_read_lock();
510 	xprt = xprt_get(rcu_dereference(clnt->cl_xprt));
511 	rcu_read_unlock();
512 	if (xprt == NULL)
513 		goto out_err;
514 	args->servername = xprt->servername;
515 
516 	new = rpc_new_client(args, xprt);
517 	if (IS_ERR(new)) {
518 		err = PTR_ERR(new);
519 		goto out_put;
520 	}
521 
522 	atomic_inc(&clnt->cl_count);
523 	new->cl_parent = clnt;
524 
525 	/* Turn off autobind on clones */
526 	new->cl_autobind = 0;
527 	new->cl_softrtry = clnt->cl_softrtry;
528 	new->cl_discrtry = clnt->cl_discrtry;
529 	new->cl_chatty = clnt->cl_chatty;
530 	return new;
531 
532 out_put:
533 	xprt_put(xprt);
534 out_err:
535 	dprintk("RPC:       %s: returned error %d\n", __func__, err);
536 	return ERR_PTR(err);
537 }
538 
539 /**
540  * rpc_clone_client - Clone an RPC client structure
541  *
542  * @clnt: RPC client whose parameters are copied
543  *
544  * Returns a fresh RPC client or an ERR_PTR.
545  */
546 struct rpc_clnt *rpc_clone_client(struct rpc_clnt *clnt)
547 {
548 	struct rpc_create_args args = {
549 		.program	= clnt->cl_program,
550 		.prognumber	= clnt->cl_prog,
551 		.version	= clnt->cl_vers,
552 		.authflavor	= clnt->cl_auth->au_flavor,
553 		.client_name	= clnt->cl_principal,
554 	};
555 	return __rpc_clone_client(&args, clnt);
556 }
557 EXPORT_SYMBOL_GPL(rpc_clone_client);
558 
559 /**
560  * rpc_clone_client_set_auth - Clone an RPC client structure and set its auth
561  *
562  * @clnt: RPC client whose parameters are copied
563  * @flavor: security flavor for new client
564  *
565  * Returns a fresh RPC client or an ERR_PTR.
566  */
567 struct rpc_clnt *
568 rpc_clone_client_set_auth(struct rpc_clnt *clnt, rpc_authflavor_t flavor)
569 {
570 	struct rpc_create_args args = {
571 		.program	= clnt->cl_program,
572 		.prognumber	= clnt->cl_prog,
573 		.version	= clnt->cl_vers,
574 		.authflavor	= flavor,
575 		.client_name	= clnt->cl_principal,
576 	};
577 	return __rpc_clone_client(&args, clnt);
578 }
579 EXPORT_SYMBOL_GPL(rpc_clone_client_set_auth);
580 
581 /*
582  * Kill all tasks for the given client.
583  * XXX: kill their descendants as well?
584  */
585 void rpc_killall_tasks(struct rpc_clnt *clnt)
586 {
587 	struct rpc_task	*rovr;
588 
589 
590 	if (list_empty(&clnt->cl_tasks))
591 		return;
592 	dprintk("RPC:       killing all tasks for client %p\n", clnt);
593 	/*
594 	 * Spin lock all_tasks to prevent changes...
595 	 */
596 	spin_lock(&clnt->cl_lock);
597 	list_for_each_entry(rovr, &clnt->cl_tasks, tk_task) {
598 		if (!RPC_IS_ACTIVATED(rovr))
599 			continue;
600 		if (!(rovr->tk_flags & RPC_TASK_KILLED)) {
601 			rovr->tk_flags |= RPC_TASK_KILLED;
602 			rpc_exit(rovr, -EIO);
603 			if (RPC_IS_QUEUED(rovr))
604 				rpc_wake_up_queued_task(rovr->tk_waitqueue,
605 							rovr);
606 		}
607 	}
608 	spin_unlock(&clnt->cl_lock);
609 }
610 EXPORT_SYMBOL_GPL(rpc_killall_tasks);
611 
612 /*
613  * Properly shut down an RPC client, terminating all outstanding
614  * requests.
615  */
616 void rpc_shutdown_client(struct rpc_clnt *clnt)
617 {
618 	might_sleep();
619 
620 	dprintk_rcu("RPC:       shutting down %s client for %s\n",
621 			clnt->cl_protname,
622 			rcu_dereference(clnt->cl_xprt)->servername);
623 
624 	while (!list_empty(&clnt->cl_tasks)) {
625 		rpc_killall_tasks(clnt);
626 		wait_event_timeout(destroy_wait,
627 			list_empty(&clnt->cl_tasks), 1*HZ);
628 	}
629 
630 	rpc_release_client(clnt);
631 }
632 EXPORT_SYMBOL_GPL(rpc_shutdown_client);
633 
634 /*
635  * Free an RPC client
636  */
637 static void
638 rpc_free_client(struct rpc_clnt *clnt)
639 {
640 	dprintk_rcu("RPC:       destroying %s client for %s\n",
641 			clnt->cl_protname,
642 			rcu_dereference(clnt->cl_xprt)->servername);
643 	if (clnt->cl_parent != clnt)
644 		rpc_release_client(clnt->cl_parent);
645 	rpc_unregister_client(clnt);
646 	rpc_clnt_remove_pipedir(clnt);
647 	rpc_free_iostats(clnt->cl_metrics);
648 	kfree(clnt->cl_principal);
649 	clnt->cl_metrics = NULL;
650 	xprt_put(rcu_dereference_raw(clnt->cl_xprt));
651 	rpciod_down();
652 	kfree(clnt);
653 }
654 
655 /*
656  * Free an RPC client
657  */
658 static void
659 rpc_free_auth(struct rpc_clnt *clnt)
660 {
661 	if (clnt->cl_auth == NULL) {
662 		rpc_free_client(clnt);
663 		return;
664 	}
665 
666 	/*
667 	 * Note: RPCSEC_GSS may need to send NULL RPC calls in order to
668 	 *       release remaining GSS contexts. This mechanism ensures
669 	 *       that it can do so safely.
670 	 */
671 	atomic_inc(&clnt->cl_count);
672 	rpcauth_release(clnt->cl_auth);
673 	clnt->cl_auth = NULL;
674 	if (atomic_dec_and_test(&clnt->cl_count))
675 		rpc_free_client(clnt);
676 }
677 
678 /*
679  * Release reference to the RPC client
680  */
681 void
682 rpc_release_client(struct rpc_clnt *clnt)
683 {
684 	dprintk("RPC:       rpc_release_client(%p)\n", clnt);
685 
686 	if (list_empty(&clnt->cl_tasks))
687 		wake_up(&destroy_wait);
688 	if (atomic_dec_and_test(&clnt->cl_count))
689 		rpc_free_auth(clnt);
690 }
691 EXPORT_SYMBOL_GPL(rpc_release_client);
692 
693 /**
694  * rpc_bind_new_program - bind a new RPC program to an existing client
695  * @old: old rpc_client
696  * @program: rpc program to set
697  * @vers: rpc program version
698  *
699  * Clones the rpc client and sets up a new RPC program. This is mainly
700  * of use for enabling different RPC programs to share the same transport.
701  * The Sun NFSv2/v3 ACL protocol can do this.
702  */
703 struct rpc_clnt *rpc_bind_new_program(struct rpc_clnt *old,
704 				      const struct rpc_program *program,
705 				      u32 vers)
706 {
707 	struct rpc_create_args args = {
708 		.program	= program,
709 		.prognumber	= program->number,
710 		.version	= vers,
711 		.authflavor	= old->cl_auth->au_flavor,
712 		.client_name	= old->cl_principal,
713 	};
714 	struct rpc_clnt *clnt;
715 	int err;
716 
717 	clnt = __rpc_clone_client(&args, old);
718 	if (IS_ERR(clnt))
719 		goto out;
720 	err = rpc_ping(clnt);
721 	if (err != 0) {
722 		rpc_shutdown_client(clnt);
723 		clnt = ERR_PTR(err);
724 	}
725 out:
726 	return clnt;
727 }
728 EXPORT_SYMBOL_GPL(rpc_bind_new_program);
729 
730 void rpc_task_release_client(struct rpc_task *task)
731 {
732 	struct rpc_clnt *clnt = task->tk_client;
733 
734 	if (clnt != NULL) {
735 		/* Remove from client task list */
736 		spin_lock(&clnt->cl_lock);
737 		list_del(&task->tk_task);
738 		spin_unlock(&clnt->cl_lock);
739 		task->tk_client = NULL;
740 
741 		rpc_release_client(clnt);
742 	}
743 }
744 
745 static
746 void rpc_task_set_client(struct rpc_task *task, struct rpc_clnt *clnt)
747 {
748 	if (clnt != NULL) {
749 		rpc_task_release_client(task);
750 		task->tk_client = clnt;
751 		atomic_inc(&clnt->cl_count);
752 		if (clnt->cl_softrtry)
753 			task->tk_flags |= RPC_TASK_SOFT;
754 		if (sk_memalloc_socks()) {
755 			struct rpc_xprt *xprt;
756 
757 			rcu_read_lock();
758 			xprt = rcu_dereference(clnt->cl_xprt);
759 			if (xprt->swapper)
760 				task->tk_flags |= RPC_TASK_SWAPPER;
761 			rcu_read_unlock();
762 		}
763 		/* Add to the client's list of all tasks */
764 		spin_lock(&clnt->cl_lock);
765 		list_add_tail(&task->tk_task, &clnt->cl_tasks);
766 		spin_unlock(&clnt->cl_lock);
767 	}
768 }
769 
770 void rpc_task_reset_client(struct rpc_task *task, struct rpc_clnt *clnt)
771 {
772 	rpc_task_release_client(task);
773 	rpc_task_set_client(task, clnt);
774 }
775 EXPORT_SYMBOL_GPL(rpc_task_reset_client);
776 
777 
778 static void
779 rpc_task_set_rpc_message(struct rpc_task *task, const struct rpc_message *msg)
780 {
781 	if (msg != NULL) {
782 		task->tk_msg.rpc_proc = msg->rpc_proc;
783 		task->tk_msg.rpc_argp = msg->rpc_argp;
784 		task->tk_msg.rpc_resp = msg->rpc_resp;
785 		if (msg->rpc_cred != NULL)
786 			task->tk_msg.rpc_cred = get_rpccred(msg->rpc_cred);
787 	}
788 }
789 
790 /*
791  * Default callback for async RPC calls
792  */
793 static void
794 rpc_default_callback(struct rpc_task *task, void *data)
795 {
796 }
797 
798 static const struct rpc_call_ops rpc_default_ops = {
799 	.rpc_call_done = rpc_default_callback,
800 };
801 
802 /**
803  * rpc_run_task - Allocate a new RPC task, then run rpc_execute against it
804  * @task_setup_data: pointer to task initialisation data
805  */
806 struct rpc_task *rpc_run_task(const struct rpc_task_setup *task_setup_data)
807 {
808 	struct rpc_task *task;
809 
810 	task = rpc_new_task(task_setup_data);
811 	if (IS_ERR(task))
812 		goto out;
813 
814 	rpc_task_set_client(task, task_setup_data->rpc_client);
815 	rpc_task_set_rpc_message(task, task_setup_data->rpc_message);
816 
817 	if (task->tk_action == NULL)
818 		rpc_call_start(task);
819 
820 	atomic_inc(&task->tk_count);
821 	rpc_execute(task);
822 out:
823 	return task;
824 }
825 EXPORT_SYMBOL_GPL(rpc_run_task);
826 
827 /**
828  * rpc_call_sync - Perform a synchronous RPC call
829  * @clnt: pointer to RPC client
830  * @msg: RPC call parameters
831  * @flags: RPC call flags
832  */
833 int rpc_call_sync(struct rpc_clnt *clnt, const struct rpc_message *msg, int flags)
834 {
835 	struct rpc_task	*task;
836 	struct rpc_task_setup task_setup_data = {
837 		.rpc_client = clnt,
838 		.rpc_message = msg,
839 		.callback_ops = &rpc_default_ops,
840 		.flags = flags,
841 	};
842 	int status;
843 
844 	WARN_ON_ONCE(flags & RPC_TASK_ASYNC);
845 	if (flags & RPC_TASK_ASYNC) {
846 		rpc_release_calldata(task_setup_data.callback_ops,
847 			task_setup_data.callback_data);
848 		return -EINVAL;
849 	}
850 
851 	task = rpc_run_task(&task_setup_data);
852 	if (IS_ERR(task))
853 		return PTR_ERR(task);
854 	status = task->tk_status;
855 	rpc_put_task(task);
856 	return status;
857 }
858 EXPORT_SYMBOL_GPL(rpc_call_sync);
859 
860 /**
861  * rpc_call_async - Perform an asynchronous RPC call
862  * @clnt: pointer to RPC client
863  * @msg: RPC call parameters
864  * @flags: RPC call flags
865  * @tk_ops: RPC call ops
866  * @data: user call data
867  */
868 int
869 rpc_call_async(struct rpc_clnt *clnt, const struct rpc_message *msg, int flags,
870 	       const struct rpc_call_ops *tk_ops, void *data)
871 {
872 	struct rpc_task	*task;
873 	struct rpc_task_setup task_setup_data = {
874 		.rpc_client = clnt,
875 		.rpc_message = msg,
876 		.callback_ops = tk_ops,
877 		.callback_data = data,
878 		.flags = flags|RPC_TASK_ASYNC,
879 	};
880 
881 	task = rpc_run_task(&task_setup_data);
882 	if (IS_ERR(task))
883 		return PTR_ERR(task);
884 	rpc_put_task(task);
885 	return 0;
886 }
887 EXPORT_SYMBOL_GPL(rpc_call_async);
888 
889 #if defined(CONFIG_SUNRPC_BACKCHANNEL)
890 /**
891  * rpc_run_bc_task - Allocate a new RPC task for backchannel use, then run
892  * rpc_execute against it
893  * @req: RPC request
894  * @tk_ops: RPC call ops
895  */
896 struct rpc_task *rpc_run_bc_task(struct rpc_rqst *req,
897 				const struct rpc_call_ops *tk_ops)
898 {
899 	struct rpc_task *task;
900 	struct xdr_buf *xbufp = &req->rq_snd_buf;
901 	struct rpc_task_setup task_setup_data = {
902 		.callback_ops = tk_ops,
903 	};
904 
905 	dprintk("RPC: rpc_run_bc_task req= %p\n", req);
906 	/*
907 	 * Create an rpc_task to send the data
908 	 */
909 	task = rpc_new_task(&task_setup_data);
910 	if (IS_ERR(task)) {
911 		xprt_free_bc_request(req);
912 		goto out;
913 	}
914 	task->tk_rqstp = req;
915 
916 	/*
917 	 * Set up the xdr_buf length.
918 	 * This also indicates that the buffer is XDR encoded already.
919 	 */
920 	xbufp->len = xbufp->head[0].iov_len + xbufp->page_len +
921 			xbufp->tail[0].iov_len;
922 
923 	task->tk_action = call_bc_transmit;
924 	atomic_inc(&task->tk_count);
925 	WARN_ON_ONCE(atomic_read(&task->tk_count) != 2);
926 	rpc_execute(task);
927 
928 out:
929 	dprintk("RPC: rpc_run_bc_task: task= %p\n", task);
930 	return task;
931 }
932 #endif /* CONFIG_SUNRPC_BACKCHANNEL */
933 
934 void
935 rpc_call_start(struct rpc_task *task)
936 {
937 	task->tk_action = call_start;
938 }
939 EXPORT_SYMBOL_GPL(rpc_call_start);
940 
941 /**
942  * rpc_peeraddr - extract remote peer address from clnt's xprt
943  * @clnt: RPC client structure
944  * @buf: target buffer
945  * @bufsize: length of target buffer
946  *
947  * Returns the number of bytes that are actually in the stored address.
948  */
949 size_t rpc_peeraddr(struct rpc_clnt *clnt, struct sockaddr *buf, size_t bufsize)
950 {
951 	size_t bytes;
952 	struct rpc_xprt *xprt;
953 
954 	rcu_read_lock();
955 	xprt = rcu_dereference(clnt->cl_xprt);
956 
957 	bytes = xprt->addrlen;
958 	if (bytes > bufsize)
959 		bytes = bufsize;
960 	memcpy(buf, &xprt->addr, bytes);
961 	rcu_read_unlock();
962 
963 	return bytes;
964 }
965 EXPORT_SYMBOL_GPL(rpc_peeraddr);
966 
967 /**
968  * rpc_peeraddr2str - return remote peer address in printable format
969  * @clnt: RPC client structure
970  * @format: address format
971  *
972  * NB: the lifetime of the memory referenced by the returned pointer is
973  * the same as the rpc_xprt itself.  As long as the caller uses this
974  * pointer, it must hold the RCU read lock.
975  */
976 const char *rpc_peeraddr2str(struct rpc_clnt *clnt,
977 			     enum rpc_display_format_t format)
978 {
979 	struct rpc_xprt *xprt;
980 
981 	xprt = rcu_dereference(clnt->cl_xprt);
982 
983 	if (xprt->address_strings[format] != NULL)
984 		return xprt->address_strings[format];
985 	else
986 		return "unprintable";
987 }
988 EXPORT_SYMBOL_GPL(rpc_peeraddr2str);
989 
990 static const struct sockaddr_in rpc_inaddr_loopback = {
991 	.sin_family		= AF_INET,
992 	.sin_addr.s_addr	= htonl(INADDR_ANY),
993 };
994 
995 static const struct sockaddr_in6 rpc_in6addr_loopback = {
996 	.sin6_family		= AF_INET6,
997 	.sin6_addr		= IN6ADDR_ANY_INIT,
998 };
999 
1000 /*
1001  * Try a getsockname() on a connected datagram socket.  Using a
1002  * connected datagram socket prevents leaving a socket in TIME_WAIT.
1003  * This conserves the ephemeral port number space.
1004  *
1005  * Returns zero and fills in "buf" if successful; otherwise, a
1006  * negative errno is returned.
1007  */
1008 static int rpc_sockname(struct net *net, struct sockaddr *sap, size_t salen,
1009 			struct sockaddr *buf, int buflen)
1010 {
1011 	struct socket *sock;
1012 	int err;
1013 
1014 	err = __sock_create(net, sap->sa_family,
1015 				SOCK_DGRAM, IPPROTO_UDP, &sock, 1);
1016 	if (err < 0) {
1017 		dprintk("RPC:       can't create UDP socket (%d)\n", err);
1018 		goto out;
1019 	}
1020 
1021 	switch (sap->sa_family) {
1022 	case AF_INET:
1023 		err = kernel_bind(sock,
1024 				(struct sockaddr *)&rpc_inaddr_loopback,
1025 				sizeof(rpc_inaddr_loopback));
1026 		break;
1027 	case AF_INET6:
1028 		err = kernel_bind(sock,
1029 				(struct sockaddr *)&rpc_in6addr_loopback,
1030 				sizeof(rpc_in6addr_loopback));
1031 		break;
1032 	default:
1033 		err = -EAFNOSUPPORT;
1034 		goto out;
1035 	}
1036 	if (err < 0) {
1037 		dprintk("RPC:       can't bind UDP socket (%d)\n", err);
1038 		goto out_release;
1039 	}
1040 
1041 	err = kernel_connect(sock, sap, salen, 0);
1042 	if (err < 0) {
1043 		dprintk("RPC:       can't connect UDP socket (%d)\n", err);
1044 		goto out_release;
1045 	}
1046 
1047 	err = kernel_getsockname(sock, buf, &buflen);
1048 	if (err < 0) {
1049 		dprintk("RPC:       getsockname failed (%d)\n", err);
1050 		goto out_release;
1051 	}
1052 
1053 	err = 0;
1054 	if (buf->sa_family == AF_INET6) {
1055 		struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)buf;
1056 		sin6->sin6_scope_id = 0;
1057 	}
1058 	dprintk("RPC:       %s succeeded\n", __func__);
1059 
1060 out_release:
1061 	sock_release(sock);
1062 out:
1063 	return err;
1064 }
1065 
1066 /*
1067  * Scraping a connected socket failed, so we don't have a useable
1068  * local address.  Fallback: generate an address that will prevent
1069  * the server from calling us back.
1070  *
1071  * Returns zero and fills in "buf" if successful; otherwise, a
1072  * negative errno is returned.
1073  */
1074 static int rpc_anyaddr(int family, struct sockaddr *buf, size_t buflen)
1075 {
1076 	switch (family) {
1077 	case AF_INET:
1078 		if (buflen < sizeof(rpc_inaddr_loopback))
1079 			return -EINVAL;
1080 		memcpy(buf, &rpc_inaddr_loopback,
1081 				sizeof(rpc_inaddr_loopback));
1082 		break;
1083 	case AF_INET6:
1084 		if (buflen < sizeof(rpc_in6addr_loopback))
1085 			return -EINVAL;
1086 		memcpy(buf, &rpc_in6addr_loopback,
1087 				sizeof(rpc_in6addr_loopback));
1088 	default:
1089 		dprintk("RPC:       %s: address family not supported\n",
1090 			__func__);
1091 		return -EAFNOSUPPORT;
1092 	}
1093 	dprintk("RPC:       %s: succeeded\n", __func__);
1094 	return 0;
1095 }
1096 
1097 /**
1098  * rpc_localaddr - discover local endpoint address for an RPC client
1099  * @clnt: RPC client structure
1100  * @buf: target buffer
1101  * @buflen: size of target buffer, in bytes
1102  *
1103  * Returns zero and fills in "buf" and "buflen" if successful;
1104  * otherwise, a negative errno is returned.
1105  *
1106  * This works even if the underlying transport is not currently connected,
1107  * or if the upper layer never previously provided a source address.
1108  *
1109  * The result of this function call is transient: multiple calls in
1110  * succession may give different results, depending on how local
1111  * networking configuration changes over time.
1112  */
1113 int rpc_localaddr(struct rpc_clnt *clnt, struct sockaddr *buf, size_t buflen)
1114 {
1115 	struct sockaddr_storage address;
1116 	struct sockaddr *sap = (struct sockaddr *)&address;
1117 	struct rpc_xprt *xprt;
1118 	struct net *net;
1119 	size_t salen;
1120 	int err;
1121 
1122 	rcu_read_lock();
1123 	xprt = rcu_dereference(clnt->cl_xprt);
1124 	salen = xprt->addrlen;
1125 	memcpy(sap, &xprt->addr, salen);
1126 	net = get_net(xprt->xprt_net);
1127 	rcu_read_unlock();
1128 
1129 	rpc_set_port(sap, 0);
1130 	err = rpc_sockname(net, sap, salen, buf, buflen);
1131 	put_net(net);
1132 	if (err != 0)
1133 		/* Couldn't discover local address, return ANYADDR */
1134 		return rpc_anyaddr(sap->sa_family, buf, buflen);
1135 	return 0;
1136 }
1137 EXPORT_SYMBOL_GPL(rpc_localaddr);
1138 
1139 void
1140 rpc_setbufsize(struct rpc_clnt *clnt, unsigned int sndsize, unsigned int rcvsize)
1141 {
1142 	struct rpc_xprt *xprt;
1143 
1144 	rcu_read_lock();
1145 	xprt = rcu_dereference(clnt->cl_xprt);
1146 	if (xprt->ops->set_buffer_size)
1147 		xprt->ops->set_buffer_size(xprt, sndsize, rcvsize);
1148 	rcu_read_unlock();
1149 }
1150 EXPORT_SYMBOL_GPL(rpc_setbufsize);
1151 
1152 /**
1153  * rpc_protocol - Get transport protocol number for an RPC client
1154  * @clnt: RPC client to query
1155  *
1156  */
1157 int rpc_protocol(struct rpc_clnt *clnt)
1158 {
1159 	int protocol;
1160 
1161 	rcu_read_lock();
1162 	protocol = rcu_dereference(clnt->cl_xprt)->prot;
1163 	rcu_read_unlock();
1164 	return protocol;
1165 }
1166 EXPORT_SYMBOL_GPL(rpc_protocol);
1167 
1168 /**
1169  * rpc_net_ns - Get the network namespace for this RPC client
1170  * @clnt: RPC client to query
1171  *
1172  */
1173 struct net *rpc_net_ns(struct rpc_clnt *clnt)
1174 {
1175 	struct net *ret;
1176 
1177 	rcu_read_lock();
1178 	ret = rcu_dereference(clnt->cl_xprt)->xprt_net;
1179 	rcu_read_unlock();
1180 	return ret;
1181 }
1182 EXPORT_SYMBOL_GPL(rpc_net_ns);
1183 
1184 /**
1185  * rpc_max_payload - Get maximum payload size for a transport, in bytes
1186  * @clnt: RPC client to query
1187  *
1188  * For stream transports, this is one RPC record fragment (see RFC
1189  * 1831), as we don't support multi-record requests yet.  For datagram
1190  * transports, this is the size of an IP packet minus the IP, UDP, and
1191  * RPC header sizes.
1192  */
1193 size_t rpc_max_payload(struct rpc_clnt *clnt)
1194 {
1195 	size_t ret;
1196 
1197 	rcu_read_lock();
1198 	ret = rcu_dereference(clnt->cl_xprt)->max_payload;
1199 	rcu_read_unlock();
1200 	return ret;
1201 }
1202 EXPORT_SYMBOL_GPL(rpc_max_payload);
1203 
1204 /**
1205  * rpc_get_timeout - Get timeout for transport in units of HZ
1206  * @clnt: RPC client to query
1207  */
1208 unsigned long rpc_get_timeout(struct rpc_clnt *clnt)
1209 {
1210 	unsigned long ret;
1211 
1212 	rcu_read_lock();
1213 	ret = rcu_dereference(clnt->cl_xprt)->timeout->to_initval;
1214 	rcu_read_unlock();
1215 	return ret;
1216 }
1217 EXPORT_SYMBOL_GPL(rpc_get_timeout);
1218 
1219 /**
1220  * rpc_force_rebind - force transport to check that remote port is unchanged
1221  * @clnt: client to rebind
1222  *
1223  */
1224 void rpc_force_rebind(struct rpc_clnt *clnt)
1225 {
1226 	if (clnt->cl_autobind) {
1227 		rcu_read_lock();
1228 		xprt_clear_bound(rcu_dereference(clnt->cl_xprt));
1229 		rcu_read_unlock();
1230 	}
1231 }
1232 EXPORT_SYMBOL_GPL(rpc_force_rebind);
1233 
1234 /*
1235  * Restart an (async) RPC call from the call_prepare state.
1236  * Usually called from within the exit handler.
1237  */
1238 int
1239 rpc_restart_call_prepare(struct rpc_task *task)
1240 {
1241 	if (RPC_ASSASSINATED(task))
1242 		return 0;
1243 	task->tk_action = call_start;
1244 	if (task->tk_ops->rpc_call_prepare != NULL)
1245 		task->tk_action = rpc_prepare_task;
1246 	return 1;
1247 }
1248 EXPORT_SYMBOL_GPL(rpc_restart_call_prepare);
1249 
1250 /*
1251  * Restart an (async) RPC call. Usually called from within the
1252  * exit handler.
1253  */
1254 int
1255 rpc_restart_call(struct rpc_task *task)
1256 {
1257 	if (RPC_ASSASSINATED(task))
1258 		return 0;
1259 	task->tk_action = call_start;
1260 	return 1;
1261 }
1262 EXPORT_SYMBOL_GPL(rpc_restart_call);
1263 
1264 #ifdef RPC_DEBUG
1265 static const char *rpc_proc_name(const struct rpc_task *task)
1266 {
1267 	const struct rpc_procinfo *proc = task->tk_msg.rpc_proc;
1268 
1269 	if (proc) {
1270 		if (proc->p_name)
1271 			return proc->p_name;
1272 		else
1273 			return "NULL";
1274 	} else
1275 		return "no proc";
1276 }
1277 #endif
1278 
1279 /*
1280  * 0.  Initial state
1281  *
1282  *     Other FSM states can be visited zero or more times, but
1283  *     this state is visited exactly once for each RPC.
1284  */
1285 static void
1286 call_start(struct rpc_task *task)
1287 {
1288 	struct rpc_clnt	*clnt = task->tk_client;
1289 
1290 	dprintk("RPC: %5u call_start %s%d proc %s (%s)\n", task->tk_pid,
1291 			clnt->cl_protname, clnt->cl_vers,
1292 			rpc_proc_name(task),
1293 			(RPC_IS_ASYNC(task) ? "async" : "sync"));
1294 
1295 	/* Increment call count */
1296 	task->tk_msg.rpc_proc->p_count++;
1297 	clnt->cl_stats->rpccnt++;
1298 	task->tk_action = call_reserve;
1299 }
1300 
1301 /*
1302  * 1.	Reserve an RPC call slot
1303  */
1304 static void
1305 call_reserve(struct rpc_task *task)
1306 {
1307 	dprint_status(task);
1308 
1309 	task->tk_status  = 0;
1310 	task->tk_action  = call_reserveresult;
1311 	xprt_reserve(task);
1312 }
1313 
1314 static void call_retry_reserve(struct rpc_task *task);
1315 
1316 /*
1317  * 1b.	Grok the result of xprt_reserve()
1318  */
1319 static void
1320 call_reserveresult(struct rpc_task *task)
1321 {
1322 	int status = task->tk_status;
1323 
1324 	dprint_status(task);
1325 
1326 	/*
1327 	 * After a call to xprt_reserve(), we must have either
1328 	 * a request slot or else an error status.
1329 	 */
1330 	task->tk_status = 0;
1331 	if (status >= 0) {
1332 		if (task->tk_rqstp) {
1333 			task->tk_action = call_refresh;
1334 			return;
1335 		}
1336 
1337 		printk(KERN_ERR "%s: status=%d, but no request slot, exiting\n",
1338 				__func__, status);
1339 		rpc_exit(task, -EIO);
1340 		return;
1341 	}
1342 
1343 	/*
1344 	 * Even though there was an error, we may have acquired
1345 	 * a request slot somehow.  Make sure not to leak it.
1346 	 */
1347 	if (task->tk_rqstp) {
1348 		printk(KERN_ERR "%s: status=%d, request allocated anyway\n",
1349 				__func__, status);
1350 		xprt_release(task);
1351 	}
1352 
1353 	switch (status) {
1354 	case -ENOMEM:
1355 		rpc_delay(task, HZ >> 2);
1356 	case -EAGAIN:	/* woken up; retry */
1357 		task->tk_action = call_retry_reserve;
1358 		return;
1359 	case -EIO:	/* probably a shutdown */
1360 		break;
1361 	default:
1362 		printk(KERN_ERR "%s: unrecognized error %d, exiting\n",
1363 				__func__, status);
1364 		break;
1365 	}
1366 	rpc_exit(task, status);
1367 }
1368 
1369 /*
1370  * 1c.	Retry reserving an RPC call slot
1371  */
1372 static void
1373 call_retry_reserve(struct rpc_task *task)
1374 {
1375 	dprint_status(task);
1376 
1377 	task->tk_status  = 0;
1378 	task->tk_action  = call_reserveresult;
1379 	xprt_retry_reserve(task);
1380 }
1381 
1382 /*
1383  * 2.	Bind and/or refresh the credentials
1384  */
1385 static void
1386 call_refresh(struct rpc_task *task)
1387 {
1388 	dprint_status(task);
1389 
1390 	task->tk_action = call_refreshresult;
1391 	task->tk_status = 0;
1392 	task->tk_client->cl_stats->rpcauthrefresh++;
1393 	rpcauth_refreshcred(task);
1394 }
1395 
1396 /*
1397  * 2a.	Process the results of a credential refresh
1398  */
1399 static void
1400 call_refreshresult(struct rpc_task *task)
1401 {
1402 	int status = task->tk_status;
1403 
1404 	dprint_status(task);
1405 
1406 	task->tk_status = 0;
1407 	task->tk_action = call_refresh;
1408 	switch (status) {
1409 	case 0:
1410 		if (rpcauth_uptodatecred(task))
1411 			task->tk_action = call_allocate;
1412 		return;
1413 	case -ETIMEDOUT:
1414 		rpc_delay(task, 3*HZ);
1415 	case -EKEYEXPIRED:
1416 	case -EAGAIN:
1417 		status = -EACCES;
1418 		if (!task->tk_cred_retry)
1419 			break;
1420 		task->tk_cred_retry--;
1421 		dprintk("RPC: %5u %s: retry refresh creds\n",
1422 				task->tk_pid, __func__);
1423 		return;
1424 	}
1425 	dprintk("RPC: %5u %s: refresh creds failed with error %d\n",
1426 				task->tk_pid, __func__, status);
1427 	rpc_exit(task, status);
1428 }
1429 
1430 /*
1431  * 2b.	Allocate the buffer. For details, see sched.c:rpc_malloc.
1432  *	(Note: buffer memory is freed in xprt_release).
1433  */
1434 static void
1435 call_allocate(struct rpc_task *task)
1436 {
1437 	unsigned int slack = task->tk_rqstp->rq_cred->cr_auth->au_cslack;
1438 	struct rpc_rqst *req = task->tk_rqstp;
1439 	struct rpc_xprt *xprt = req->rq_xprt;
1440 	struct rpc_procinfo *proc = task->tk_msg.rpc_proc;
1441 
1442 	dprint_status(task);
1443 
1444 	task->tk_status = 0;
1445 	task->tk_action = call_bind;
1446 
1447 	if (req->rq_buffer)
1448 		return;
1449 
1450 	if (proc->p_proc != 0) {
1451 		BUG_ON(proc->p_arglen == 0);
1452 		if (proc->p_decode != NULL)
1453 			BUG_ON(proc->p_replen == 0);
1454 	}
1455 
1456 	/*
1457 	 * Calculate the size (in quads) of the RPC call
1458 	 * and reply headers, and convert both values
1459 	 * to byte sizes.
1460 	 */
1461 	req->rq_callsize = RPC_CALLHDRSIZE + (slack << 1) + proc->p_arglen;
1462 	req->rq_callsize <<= 2;
1463 	req->rq_rcvsize = RPC_REPHDRSIZE + slack + proc->p_replen;
1464 	req->rq_rcvsize <<= 2;
1465 
1466 	req->rq_buffer = xprt->ops->buf_alloc(task,
1467 					req->rq_callsize + req->rq_rcvsize);
1468 	if (req->rq_buffer != NULL)
1469 		return;
1470 
1471 	dprintk("RPC: %5u rpc_buffer allocation failed\n", task->tk_pid);
1472 
1473 	if (RPC_IS_ASYNC(task) || !fatal_signal_pending(current)) {
1474 		task->tk_action = call_allocate;
1475 		rpc_delay(task, HZ>>4);
1476 		return;
1477 	}
1478 
1479 	rpc_exit(task, -ERESTARTSYS);
1480 }
1481 
1482 static inline int
1483 rpc_task_need_encode(struct rpc_task *task)
1484 {
1485 	return task->tk_rqstp->rq_snd_buf.len == 0;
1486 }
1487 
1488 static inline void
1489 rpc_task_force_reencode(struct rpc_task *task)
1490 {
1491 	task->tk_rqstp->rq_snd_buf.len = 0;
1492 	task->tk_rqstp->rq_bytes_sent = 0;
1493 }
1494 
1495 static inline void
1496 rpc_xdr_buf_init(struct xdr_buf *buf, void *start, size_t len)
1497 {
1498 	buf->head[0].iov_base = start;
1499 	buf->head[0].iov_len = len;
1500 	buf->tail[0].iov_len = 0;
1501 	buf->page_len = 0;
1502 	buf->flags = 0;
1503 	buf->len = 0;
1504 	buf->buflen = len;
1505 }
1506 
1507 /*
1508  * 3.	Encode arguments of an RPC call
1509  */
1510 static void
1511 rpc_xdr_encode(struct rpc_task *task)
1512 {
1513 	struct rpc_rqst	*req = task->tk_rqstp;
1514 	kxdreproc_t	encode;
1515 	__be32		*p;
1516 
1517 	dprint_status(task);
1518 
1519 	rpc_xdr_buf_init(&req->rq_snd_buf,
1520 			 req->rq_buffer,
1521 			 req->rq_callsize);
1522 	rpc_xdr_buf_init(&req->rq_rcv_buf,
1523 			 (char *)req->rq_buffer + req->rq_callsize,
1524 			 req->rq_rcvsize);
1525 
1526 	p = rpc_encode_header(task);
1527 	if (p == NULL) {
1528 		printk(KERN_INFO "RPC: couldn't encode RPC header, exit EIO\n");
1529 		rpc_exit(task, -EIO);
1530 		return;
1531 	}
1532 
1533 	encode = task->tk_msg.rpc_proc->p_encode;
1534 	if (encode == NULL)
1535 		return;
1536 
1537 	task->tk_status = rpcauth_wrap_req(task, encode, req, p,
1538 			task->tk_msg.rpc_argp);
1539 }
1540 
1541 /*
1542  * 4.	Get the server port number if not yet set
1543  */
1544 static void
1545 call_bind(struct rpc_task *task)
1546 {
1547 	struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
1548 
1549 	dprint_status(task);
1550 
1551 	task->tk_action = call_connect;
1552 	if (!xprt_bound(xprt)) {
1553 		task->tk_action = call_bind_status;
1554 		task->tk_timeout = xprt->bind_timeout;
1555 		xprt->ops->rpcbind(task);
1556 	}
1557 }
1558 
1559 /*
1560  * 4a.	Sort out bind result
1561  */
1562 static void
1563 call_bind_status(struct rpc_task *task)
1564 {
1565 	int status = -EIO;
1566 
1567 	if (task->tk_status >= 0) {
1568 		dprint_status(task);
1569 		task->tk_status = 0;
1570 		task->tk_action = call_connect;
1571 		return;
1572 	}
1573 
1574 	trace_rpc_bind_status(task);
1575 	switch (task->tk_status) {
1576 	case -ENOMEM:
1577 		dprintk("RPC: %5u rpcbind out of memory\n", task->tk_pid);
1578 		rpc_delay(task, HZ >> 2);
1579 		goto retry_timeout;
1580 	case -EACCES:
1581 		dprintk("RPC: %5u remote rpcbind: RPC program/version "
1582 				"unavailable\n", task->tk_pid);
1583 		/* fail immediately if this is an RPC ping */
1584 		if (task->tk_msg.rpc_proc->p_proc == 0) {
1585 			status = -EOPNOTSUPP;
1586 			break;
1587 		}
1588 		if (task->tk_rebind_retry == 0)
1589 			break;
1590 		task->tk_rebind_retry--;
1591 		rpc_delay(task, 3*HZ);
1592 		goto retry_timeout;
1593 	case -ETIMEDOUT:
1594 		dprintk("RPC: %5u rpcbind request timed out\n",
1595 				task->tk_pid);
1596 		goto retry_timeout;
1597 	case -EPFNOSUPPORT:
1598 		/* server doesn't support any rpcbind version we know of */
1599 		dprintk("RPC: %5u unrecognized remote rpcbind service\n",
1600 				task->tk_pid);
1601 		break;
1602 	case -EPROTONOSUPPORT:
1603 		dprintk("RPC: %5u remote rpcbind version unavailable, retrying\n",
1604 				task->tk_pid);
1605 		task->tk_status = 0;
1606 		task->tk_action = call_bind;
1607 		return;
1608 	case -ECONNREFUSED:		/* connection problems */
1609 	case -ECONNRESET:
1610 	case -ENOTCONN:
1611 	case -EHOSTDOWN:
1612 	case -EHOSTUNREACH:
1613 	case -ENETUNREACH:
1614 	case -EPIPE:
1615 		dprintk("RPC: %5u remote rpcbind unreachable: %d\n",
1616 				task->tk_pid, task->tk_status);
1617 		if (!RPC_IS_SOFTCONN(task)) {
1618 			rpc_delay(task, 5*HZ);
1619 			goto retry_timeout;
1620 		}
1621 		status = task->tk_status;
1622 		break;
1623 	default:
1624 		dprintk("RPC: %5u unrecognized rpcbind error (%d)\n",
1625 				task->tk_pid, -task->tk_status);
1626 	}
1627 
1628 	rpc_exit(task, status);
1629 	return;
1630 
1631 retry_timeout:
1632 	task->tk_action = call_timeout;
1633 }
1634 
1635 /*
1636  * 4b.	Connect to the RPC server
1637  */
1638 static void
1639 call_connect(struct rpc_task *task)
1640 {
1641 	struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
1642 
1643 	dprintk("RPC: %5u call_connect xprt %p %s connected\n",
1644 			task->tk_pid, xprt,
1645 			(xprt_connected(xprt) ? "is" : "is not"));
1646 
1647 	task->tk_action = call_transmit;
1648 	if (!xprt_connected(xprt)) {
1649 		task->tk_action = call_connect_status;
1650 		if (task->tk_status < 0)
1651 			return;
1652 		xprt_connect(task);
1653 	}
1654 }
1655 
1656 /*
1657  * 4c.	Sort out connect result
1658  */
1659 static void
1660 call_connect_status(struct rpc_task *task)
1661 {
1662 	struct rpc_clnt *clnt = task->tk_client;
1663 	int status = task->tk_status;
1664 
1665 	dprint_status(task);
1666 
1667 	trace_rpc_connect_status(task, status);
1668 	switch (status) {
1669 		/* if soft mounted, test if we've timed out */
1670 	case -ETIMEDOUT:
1671 		task->tk_action = call_timeout;
1672 		return;
1673 	case -ECONNREFUSED:
1674 	case -ECONNRESET:
1675 	case -ENETUNREACH:
1676 		if (RPC_IS_SOFTCONN(task))
1677 			break;
1678 		/* retry with existing socket, after a delay */
1679 	case 0:
1680 	case -EAGAIN:
1681 		task->tk_status = 0;
1682 		clnt->cl_stats->netreconn++;
1683 		task->tk_action = call_transmit;
1684 		return;
1685 	}
1686 	rpc_exit(task, status);
1687 }
1688 
1689 /*
1690  * 5.	Transmit the RPC request, and wait for reply
1691  */
1692 static void
1693 call_transmit(struct rpc_task *task)
1694 {
1695 	dprint_status(task);
1696 
1697 	task->tk_action = call_status;
1698 	if (task->tk_status < 0)
1699 		return;
1700 	task->tk_status = xprt_prepare_transmit(task);
1701 	if (task->tk_status != 0)
1702 		return;
1703 	task->tk_action = call_transmit_status;
1704 	/* Encode here so that rpcsec_gss can use correct sequence number. */
1705 	if (rpc_task_need_encode(task)) {
1706 		rpc_xdr_encode(task);
1707 		/* Did the encode result in an error condition? */
1708 		if (task->tk_status != 0) {
1709 			/* Was the error nonfatal? */
1710 			if (task->tk_status == -EAGAIN)
1711 				rpc_delay(task, HZ >> 4);
1712 			else
1713 				rpc_exit(task, task->tk_status);
1714 			return;
1715 		}
1716 	}
1717 	xprt_transmit(task);
1718 	if (task->tk_status < 0)
1719 		return;
1720 	/*
1721 	 * On success, ensure that we call xprt_end_transmit() before sleeping
1722 	 * in order to allow access to the socket to other RPC requests.
1723 	 */
1724 	call_transmit_status(task);
1725 	if (rpc_reply_expected(task))
1726 		return;
1727 	task->tk_action = rpc_exit_task;
1728 	rpc_wake_up_queued_task(&task->tk_rqstp->rq_xprt->pending, task);
1729 }
1730 
1731 /*
1732  * 5a.	Handle cleanup after a transmission
1733  */
1734 static void
1735 call_transmit_status(struct rpc_task *task)
1736 {
1737 	task->tk_action = call_status;
1738 
1739 	/*
1740 	 * Common case: success.  Force the compiler to put this
1741 	 * test first.
1742 	 */
1743 	if (task->tk_status == 0) {
1744 		xprt_end_transmit(task);
1745 		rpc_task_force_reencode(task);
1746 		return;
1747 	}
1748 
1749 	switch (task->tk_status) {
1750 	case -EAGAIN:
1751 		break;
1752 	default:
1753 		dprint_status(task);
1754 		xprt_end_transmit(task);
1755 		rpc_task_force_reencode(task);
1756 		break;
1757 		/*
1758 		 * Special cases: if we've been waiting on the
1759 		 * socket's write_space() callback, or if the
1760 		 * socket just returned a connection error,
1761 		 * then hold onto the transport lock.
1762 		 */
1763 	case -ECONNREFUSED:
1764 	case -EHOSTDOWN:
1765 	case -EHOSTUNREACH:
1766 	case -ENETUNREACH:
1767 		if (RPC_IS_SOFTCONN(task)) {
1768 			xprt_end_transmit(task);
1769 			rpc_exit(task, task->tk_status);
1770 			break;
1771 		}
1772 	case -ECONNRESET:
1773 	case -ENOTCONN:
1774 	case -EPIPE:
1775 		rpc_task_force_reencode(task);
1776 	}
1777 }
1778 
1779 #if defined(CONFIG_SUNRPC_BACKCHANNEL)
1780 /*
1781  * 5b.	Send the backchannel RPC reply.  On error, drop the reply.  In
1782  * addition, disconnect on connectivity errors.
1783  */
1784 static void
1785 call_bc_transmit(struct rpc_task *task)
1786 {
1787 	struct rpc_rqst *req = task->tk_rqstp;
1788 
1789 	task->tk_status = xprt_prepare_transmit(task);
1790 	if (task->tk_status == -EAGAIN) {
1791 		/*
1792 		 * Could not reserve the transport. Try again after the
1793 		 * transport is released.
1794 		 */
1795 		task->tk_status = 0;
1796 		task->tk_action = call_bc_transmit;
1797 		return;
1798 	}
1799 
1800 	task->tk_action = rpc_exit_task;
1801 	if (task->tk_status < 0) {
1802 		printk(KERN_NOTICE "RPC: Could not send backchannel reply "
1803 			"error: %d\n", task->tk_status);
1804 		return;
1805 	}
1806 
1807 	xprt_transmit(task);
1808 	xprt_end_transmit(task);
1809 	dprint_status(task);
1810 	switch (task->tk_status) {
1811 	case 0:
1812 		/* Success */
1813 		break;
1814 	case -EHOSTDOWN:
1815 	case -EHOSTUNREACH:
1816 	case -ENETUNREACH:
1817 	case -ETIMEDOUT:
1818 		/*
1819 		 * Problem reaching the server.  Disconnect and let the
1820 		 * forechannel reestablish the connection.  The server will
1821 		 * have to retransmit the backchannel request and we'll
1822 		 * reprocess it.  Since these ops are idempotent, there's no
1823 		 * need to cache our reply at this time.
1824 		 */
1825 		printk(KERN_NOTICE "RPC: Could not send backchannel reply "
1826 			"error: %d\n", task->tk_status);
1827 		xprt_conditional_disconnect(req->rq_xprt,
1828 			req->rq_connect_cookie);
1829 		break;
1830 	default:
1831 		/*
1832 		 * We were unable to reply and will have to drop the
1833 		 * request.  The server should reconnect and retransmit.
1834 		 */
1835 		WARN_ON_ONCE(task->tk_status == -EAGAIN);
1836 		printk(KERN_NOTICE "RPC: Could not send backchannel reply "
1837 			"error: %d\n", task->tk_status);
1838 		break;
1839 	}
1840 	rpc_wake_up_queued_task(&req->rq_xprt->pending, task);
1841 }
1842 #endif /* CONFIG_SUNRPC_BACKCHANNEL */
1843 
1844 /*
1845  * 6.	Sort out the RPC call status
1846  */
1847 static void
1848 call_status(struct rpc_task *task)
1849 {
1850 	struct rpc_clnt	*clnt = task->tk_client;
1851 	struct rpc_rqst	*req = task->tk_rqstp;
1852 	int		status;
1853 
1854 	if (req->rq_reply_bytes_recvd > 0 && !req->rq_bytes_sent)
1855 		task->tk_status = req->rq_reply_bytes_recvd;
1856 
1857 	dprint_status(task);
1858 
1859 	status = task->tk_status;
1860 	if (status >= 0) {
1861 		task->tk_action = call_decode;
1862 		return;
1863 	}
1864 
1865 	trace_rpc_call_status(task);
1866 	task->tk_status = 0;
1867 	switch(status) {
1868 	case -EHOSTDOWN:
1869 	case -EHOSTUNREACH:
1870 	case -ENETUNREACH:
1871 		/*
1872 		 * Delay any retries for 3 seconds, then handle as if it
1873 		 * were a timeout.
1874 		 */
1875 		rpc_delay(task, 3*HZ);
1876 	case -ETIMEDOUT:
1877 		task->tk_action = call_timeout;
1878 		if (task->tk_client->cl_discrtry)
1879 			xprt_conditional_disconnect(req->rq_xprt,
1880 					req->rq_connect_cookie);
1881 		break;
1882 	case -ECONNRESET:
1883 	case -ECONNREFUSED:
1884 		rpc_force_rebind(clnt);
1885 		rpc_delay(task, 3*HZ);
1886 	case -EPIPE:
1887 	case -ENOTCONN:
1888 		task->tk_action = call_bind;
1889 		break;
1890 	case -EAGAIN:
1891 		task->tk_action = call_transmit;
1892 		break;
1893 	case -EIO:
1894 		/* shutdown or soft timeout */
1895 		rpc_exit(task, status);
1896 		break;
1897 	default:
1898 		if (clnt->cl_chatty)
1899 			printk("%s: RPC call returned error %d\n",
1900 			       clnt->cl_protname, -status);
1901 		rpc_exit(task, status);
1902 	}
1903 }
1904 
1905 /*
1906  * 6a.	Handle RPC timeout
1907  * 	We do not release the request slot, so we keep using the
1908  *	same XID for all retransmits.
1909  */
1910 static void
1911 call_timeout(struct rpc_task *task)
1912 {
1913 	struct rpc_clnt	*clnt = task->tk_client;
1914 
1915 	if (xprt_adjust_timeout(task->tk_rqstp) == 0) {
1916 		dprintk("RPC: %5u call_timeout (minor)\n", task->tk_pid);
1917 		goto retry;
1918 	}
1919 
1920 	dprintk("RPC: %5u call_timeout (major)\n", task->tk_pid);
1921 	task->tk_timeouts++;
1922 
1923 	if (RPC_IS_SOFTCONN(task)) {
1924 		rpc_exit(task, -ETIMEDOUT);
1925 		return;
1926 	}
1927 	if (RPC_IS_SOFT(task)) {
1928 		if (clnt->cl_chatty) {
1929 			rcu_read_lock();
1930 			printk(KERN_NOTICE "%s: server %s not responding, timed out\n",
1931 				clnt->cl_protname,
1932 				rcu_dereference(clnt->cl_xprt)->servername);
1933 			rcu_read_unlock();
1934 		}
1935 		if (task->tk_flags & RPC_TASK_TIMEOUT)
1936 			rpc_exit(task, -ETIMEDOUT);
1937 		else
1938 			rpc_exit(task, -EIO);
1939 		return;
1940 	}
1941 
1942 	if (!(task->tk_flags & RPC_CALL_MAJORSEEN)) {
1943 		task->tk_flags |= RPC_CALL_MAJORSEEN;
1944 		if (clnt->cl_chatty) {
1945 			rcu_read_lock();
1946 			printk(KERN_NOTICE "%s: server %s not responding, still trying\n",
1947 			clnt->cl_protname,
1948 			rcu_dereference(clnt->cl_xprt)->servername);
1949 			rcu_read_unlock();
1950 		}
1951 	}
1952 	rpc_force_rebind(clnt);
1953 	/*
1954 	 * Did our request time out due to an RPCSEC_GSS out-of-sequence
1955 	 * event? RFC2203 requires the server to drop all such requests.
1956 	 */
1957 	rpcauth_invalcred(task);
1958 
1959 retry:
1960 	clnt->cl_stats->rpcretrans++;
1961 	task->tk_action = call_bind;
1962 	task->tk_status = 0;
1963 }
1964 
1965 /*
1966  * 7.	Decode the RPC reply
1967  */
1968 static void
1969 call_decode(struct rpc_task *task)
1970 {
1971 	struct rpc_clnt	*clnt = task->tk_client;
1972 	struct rpc_rqst	*req = task->tk_rqstp;
1973 	kxdrdproc_t	decode = task->tk_msg.rpc_proc->p_decode;
1974 	__be32		*p;
1975 
1976 	dprint_status(task);
1977 
1978 	if (task->tk_flags & RPC_CALL_MAJORSEEN) {
1979 		if (clnt->cl_chatty) {
1980 			rcu_read_lock();
1981 			printk(KERN_NOTICE "%s: server %s OK\n",
1982 				clnt->cl_protname,
1983 				rcu_dereference(clnt->cl_xprt)->servername);
1984 			rcu_read_unlock();
1985 		}
1986 		task->tk_flags &= ~RPC_CALL_MAJORSEEN;
1987 	}
1988 
1989 	/*
1990 	 * Ensure that we see all writes made by xprt_complete_rqst()
1991 	 * before it changed req->rq_reply_bytes_recvd.
1992 	 */
1993 	smp_rmb();
1994 	req->rq_rcv_buf.len = req->rq_private_buf.len;
1995 
1996 	/* Check that the softirq receive buffer is valid */
1997 	WARN_ON(memcmp(&req->rq_rcv_buf, &req->rq_private_buf,
1998 				sizeof(req->rq_rcv_buf)) != 0);
1999 
2000 	if (req->rq_rcv_buf.len < 12) {
2001 		if (!RPC_IS_SOFT(task)) {
2002 			task->tk_action = call_bind;
2003 			clnt->cl_stats->rpcretrans++;
2004 			goto out_retry;
2005 		}
2006 		dprintk("RPC:       %s: too small RPC reply size (%d bytes)\n",
2007 				clnt->cl_protname, task->tk_status);
2008 		task->tk_action = call_timeout;
2009 		goto out_retry;
2010 	}
2011 
2012 	p = rpc_verify_header(task);
2013 	if (IS_ERR(p)) {
2014 		if (p == ERR_PTR(-EAGAIN))
2015 			goto out_retry;
2016 		return;
2017 	}
2018 
2019 	task->tk_action = rpc_exit_task;
2020 
2021 	if (decode) {
2022 		task->tk_status = rpcauth_unwrap_resp(task, decode, req, p,
2023 						      task->tk_msg.rpc_resp);
2024 	}
2025 	dprintk("RPC: %5u call_decode result %d\n", task->tk_pid,
2026 			task->tk_status);
2027 	return;
2028 out_retry:
2029 	task->tk_status = 0;
2030 	/* Note: rpc_verify_header() may have freed the RPC slot */
2031 	if (task->tk_rqstp == req) {
2032 		req->rq_reply_bytes_recvd = req->rq_rcv_buf.len = 0;
2033 		if (task->tk_client->cl_discrtry)
2034 			xprt_conditional_disconnect(req->rq_xprt,
2035 					req->rq_connect_cookie);
2036 	}
2037 }
2038 
2039 static __be32 *
2040 rpc_encode_header(struct rpc_task *task)
2041 {
2042 	struct rpc_clnt *clnt = task->tk_client;
2043 	struct rpc_rqst	*req = task->tk_rqstp;
2044 	__be32		*p = req->rq_svec[0].iov_base;
2045 
2046 	/* FIXME: check buffer size? */
2047 
2048 	p = xprt_skip_transport_header(req->rq_xprt, p);
2049 	*p++ = req->rq_xid;		/* XID */
2050 	*p++ = htonl(RPC_CALL);		/* CALL */
2051 	*p++ = htonl(RPC_VERSION);	/* RPC version */
2052 	*p++ = htonl(clnt->cl_prog);	/* program number */
2053 	*p++ = htonl(clnt->cl_vers);	/* program version */
2054 	*p++ = htonl(task->tk_msg.rpc_proc->p_proc);	/* procedure */
2055 	p = rpcauth_marshcred(task, p);
2056 	req->rq_slen = xdr_adjust_iovec(&req->rq_svec[0], p);
2057 	return p;
2058 }
2059 
2060 static __be32 *
2061 rpc_verify_header(struct rpc_task *task)
2062 {
2063 	struct rpc_clnt *clnt = task->tk_client;
2064 	struct kvec *iov = &task->tk_rqstp->rq_rcv_buf.head[0];
2065 	int len = task->tk_rqstp->rq_rcv_buf.len >> 2;
2066 	__be32	*p = iov->iov_base;
2067 	u32 n;
2068 	int error = -EACCES;
2069 
2070 	if ((task->tk_rqstp->rq_rcv_buf.len & 3) != 0) {
2071 		/* RFC-1014 says that the representation of XDR data must be a
2072 		 * multiple of four bytes
2073 		 * - if it isn't pointer subtraction in the NFS client may give
2074 		 *   undefined results
2075 		 */
2076 		dprintk("RPC: %5u %s: XDR representation not a multiple of"
2077 		       " 4 bytes: 0x%x\n", task->tk_pid, __func__,
2078 		       task->tk_rqstp->rq_rcv_buf.len);
2079 		goto out_eio;
2080 	}
2081 	if ((len -= 3) < 0)
2082 		goto out_overflow;
2083 
2084 	p += 1; /* skip XID */
2085 	if ((n = ntohl(*p++)) != RPC_REPLY) {
2086 		dprintk("RPC: %5u %s: not an RPC reply: %x\n",
2087 			task->tk_pid, __func__, n);
2088 		goto out_garbage;
2089 	}
2090 
2091 	if ((n = ntohl(*p++)) != RPC_MSG_ACCEPTED) {
2092 		if (--len < 0)
2093 			goto out_overflow;
2094 		switch ((n = ntohl(*p++))) {
2095 		case RPC_AUTH_ERROR:
2096 			break;
2097 		case RPC_MISMATCH:
2098 			dprintk("RPC: %5u %s: RPC call version mismatch!\n",
2099 				task->tk_pid, __func__);
2100 			error = -EPROTONOSUPPORT;
2101 			goto out_err;
2102 		default:
2103 			dprintk("RPC: %5u %s: RPC call rejected, "
2104 				"unknown error: %x\n",
2105 				task->tk_pid, __func__, n);
2106 			goto out_eio;
2107 		}
2108 		if (--len < 0)
2109 			goto out_overflow;
2110 		switch ((n = ntohl(*p++))) {
2111 		case RPC_AUTH_REJECTEDCRED:
2112 		case RPC_AUTH_REJECTEDVERF:
2113 		case RPCSEC_GSS_CREDPROBLEM:
2114 		case RPCSEC_GSS_CTXPROBLEM:
2115 			if (!task->tk_cred_retry)
2116 				break;
2117 			task->tk_cred_retry--;
2118 			dprintk("RPC: %5u %s: retry stale creds\n",
2119 					task->tk_pid, __func__);
2120 			rpcauth_invalcred(task);
2121 			/* Ensure we obtain a new XID! */
2122 			xprt_release(task);
2123 			task->tk_action = call_reserve;
2124 			goto out_retry;
2125 		case RPC_AUTH_BADCRED:
2126 		case RPC_AUTH_BADVERF:
2127 			/* possibly garbled cred/verf? */
2128 			if (!task->tk_garb_retry)
2129 				break;
2130 			task->tk_garb_retry--;
2131 			dprintk("RPC: %5u %s: retry garbled creds\n",
2132 					task->tk_pid, __func__);
2133 			task->tk_action = call_bind;
2134 			goto out_retry;
2135 		case RPC_AUTH_TOOWEAK:
2136 			rcu_read_lock();
2137 			printk(KERN_NOTICE "RPC: server %s requires stronger "
2138 			       "authentication.\n",
2139 			       rcu_dereference(clnt->cl_xprt)->servername);
2140 			rcu_read_unlock();
2141 			break;
2142 		default:
2143 			dprintk("RPC: %5u %s: unknown auth error: %x\n",
2144 					task->tk_pid, __func__, n);
2145 			error = -EIO;
2146 		}
2147 		dprintk("RPC: %5u %s: call rejected %d\n",
2148 				task->tk_pid, __func__, n);
2149 		goto out_err;
2150 	}
2151 	if (!(p = rpcauth_checkverf(task, p))) {
2152 		dprintk("RPC: %5u %s: auth check failed\n",
2153 				task->tk_pid, __func__);
2154 		goto out_garbage;		/* bad verifier, retry */
2155 	}
2156 	len = p - (__be32 *)iov->iov_base - 1;
2157 	if (len < 0)
2158 		goto out_overflow;
2159 	switch ((n = ntohl(*p++))) {
2160 	case RPC_SUCCESS:
2161 		return p;
2162 	case RPC_PROG_UNAVAIL:
2163 		dprintk_rcu("RPC: %5u %s: program %u is unsupported "
2164 				"by server %s\n", task->tk_pid, __func__,
2165 				(unsigned int)clnt->cl_prog,
2166 				rcu_dereference(clnt->cl_xprt)->servername);
2167 		error = -EPFNOSUPPORT;
2168 		goto out_err;
2169 	case RPC_PROG_MISMATCH:
2170 		dprintk_rcu("RPC: %5u %s: program %u, version %u unsupported "
2171 				"by server %s\n", task->tk_pid, __func__,
2172 				(unsigned int)clnt->cl_prog,
2173 				(unsigned int)clnt->cl_vers,
2174 				rcu_dereference(clnt->cl_xprt)->servername);
2175 		error = -EPROTONOSUPPORT;
2176 		goto out_err;
2177 	case RPC_PROC_UNAVAIL:
2178 		dprintk_rcu("RPC: %5u %s: proc %s unsupported by program %u, "
2179 				"version %u on server %s\n",
2180 				task->tk_pid, __func__,
2181 				rpc_proc_name(task),
2182 				clnt->cl_prog, clnt->cl_vers,
2183 				rcu_dereference(clnt->cl_xprt)->servername);
2184 		error = -EOPNOTSUPP;
2185 		goto out_err;
2186 	case RPC_GARBAGE_ARGS:
2187 		dprintk("RPC: %5u %s: server saw garbage\n",
2188 				task->tk_pid, __func__);
2189 		break;			/* retry */
2190 	default:
2191 		dprintk("RPC: %5u %s: server accept status: %x\n",
2192 				task->tk_pid, __func__, n);
2193 		/* Also retry */
2194 	}
2195 
2196 out_garbage:
2197 	clnt->cl_stats->rpcgarbage++;
2198 	if (task->tk_garb_retry) {
2199 		task->tk_garb_retry--;
2200 		dprintk("RPC: %5u %s: retrying\n",
2201 				task->tk_pid, __func__);
2202 		task->tk_action = call_bind;
2203 out_retry:
2204 		return ERR_PTR(-EAGAIN);
2205 	}
2206 out_eio:
2207 	error = -EIO;
2208 out_err:
2209 	rpc_exit(task, error);
2210 	dprintk("RPC: %5u %s: call failed with error %d\n", task->tk_pid,
2211 			__func__, error);
2212 	return ERR_PTR(error);
2213 out_overflow:
2214 	dprintk("RPC: %5u %s: server reply was truncated.\n", task->tk_pid,
2215 			__func__);
2216 	goto out_garbage;
2217 }
2218 
2219 static void rpcproc_encode_null(void *rqstp, struct xdr_stream *xdr, void *obj)
2220 {
2221 }
2222 
2223 static int rpcproc_decode_null(void *rqstp, struct xdr_stream *xdr, void *obj)
2224 {
2225 	return 0;
2226 }
2227 
2228 static struct rpc_procinfo rpcproc_null = {
2229 	.p_encode = rpcproc_encode_null,
2230 	.p_decode = rpcproc_decode_null,
2231 };
2232 
2233 static int rpc_ping(struct rpc_clnt *clnt)
2234 {
2235 	struct rpc_message msg = {
2236 		.rpc_proc = &rpcproc_null,
2237 	};
2238 	int err;
2239 	msg.rpc_cred = authnull_ops.lookup_cred(NULL, NULL, 0);
2240 	err = rpc_call_sync(clnt, &msg, RPC_TASK_SOFT | RPC_TASK_SOFTCONN);
2241 	put_rpccred(msg.rpc_cred);
2242 	return err;
2243 }
2244 
2245 struct rpc_task *rpc_call_null(struct rpc_clnt *clnt, struct rpc_cred *cred, int flags)
2246 {
2247 	struct rpc_message msg = {
2248 		.rpc_proc = &rpcproc_null,
2249 		.rpc_cred = cred,
2250 	};
2251 	struct rpc_task_setup task_setup_data = {
2252 		.rpc_client = clnt,
2253 		.rpc_message = &msg,
2254 		.callback_ops = &rpc_default_ops,
2255 		.flags = flags,
2256 	};
2257 	return rpc_run_task(&task_setup_data);
2258 }
2259 EXPORT_SYMBOL_GPL(rpc_call_null);
2260 
2261 #ifdef RPC_DEBUG
2262 static void rpc_show_header(void)
2263 {
2264 	printk(KERN_INFO "-pid- flgs status -client- --rqstp- "
2265 		"-timeout ---ops--\n");
2266 }
2267 
2268 static void rpc_show_task(const struct rpc_clnt *clnt,
2269 			  const struct rpc_task *task)
2270 {
2271 	const char *rpc_waitq = "none";
2272 
2273 	if (RPC_IS_QUEUED(task))
2274 		rpc_waitq = rpc_qname(task->tk_waitqueue);
2275 
2276 	printk(KERN_INFO "%5u %04x %6d %8p %8p %8ld %8p %sv%u %s a:%ps q:%s\n",
2277 		task->tk_pid, task->tk_flags, task->tk_status,
2278 		clnt, task->tk_rqstp, task->tk_timeout, task->tk_ops,
2279 		clnt->cl_protname, clnt->cl_vers, rpc_proc_name(task),
2280 		task->tk_action, rpc_waitq);
2281 }
2282 
2283 void rpc_show_tasks(struct net *net)
2284 {
2285 	struct rpc_clnt *clnt;
2286 	struct rpc_task *task;
2287 	int header = 0;
2288 	struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
2289 
2290 	spin_lock(&sn->rpc_client_lock);
2291 	list_for_each_entry(clnt, &sn->all_clients, cl_clients) {
2292 		spin_lock(&clnt->cl_lock);
2293 		list_for_each_entry(task, &clnt->cl_tasks, tk_task) {
2294 			if (!header) {
2295 				rpc_show_header();
2296 				header++;
2297 			}
2298 			rpc_show_task(clnt, task);
2299 		}
2300 		spin_unlock(&clnt->cl_lock);
2301 	}
2302 	spin_unlock(&sn->rpc_client_lock);
2303 }
2304 #endif
2305