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