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