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