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