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