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