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