]> git.ipfire.org Git - thirdparty/kernel/linux.git/blame - net/sunrpc/clnt.c
SUNRPC rpc_clnt_xprt_switch_put
[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);
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;
1da177e4 1694
46121cf7
CL
1695 dprint_status(task);
1696
2bea90d4 1697 task->tk_status = 0;
f2d47d02 1698 task->tk_action = call_bind;
2bea90d4 1699
02107148 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 */
1714 req->rq_callsize = RPC_CALLHDRSIZE + (slack << 1) + proc->p_arglen;
1715 req->rq_callsize <<= 2;
1716 req->rq_rcvsize = RPC_REPHDRSIZE + slack + proc->p_replen;
1717 req->rq_rcvsize <<= 2;
1718
c5a4dd8b
CL
1719 req->rq_buffer = xprt->ops->buf_alloc(task,
1720 req->rq_callsize + req->rq_rcvsize);
2bea90d4 1721 if (req->rq_buffer != NULL)
1da177e4 1722 return;
4a068258 1723 xprt_inject_disconnect(xprt);
46121cf7
CL
1724
1725 dprintk("RPC: %5u rpc_buffer allocation failed\n", task->tk_pid);
1da177e4 1726
5afa9133 1727 if (RPC_IS_ASYNC(task) || !fatal_signal_pending(current)) {
b6e9c713 1728 task->tk_action = call_allocate;
1da177e4
LT
1729 rpc_delay(task, HZ>>4);
1730 return;
1731 }
1732
1733 rpc_exit(task, -ERESTARTSYS);
1734}
1735
940e3318
TM
1736static inline int
1737rpc_task_need_encode(struct rpc_task *task)
1738{
1739 return task->tk_rqstp->rq_snd_buf.len == 0;
1740}
1741
1742static inline void
1743rpc_task_force_reencode(struct rpc_task *task)
1744{
1745 task->tk_rqstp->rq_snd_buf.len = 0;
2574cc9f 1746 task->tk_rqstp->rq_bytes_sent = 0;
940e3318
TM
1747}
1748
2bea90d4
CL
1749static inline void
1750rpc_xdr_buf_init(struct xdr_buf *buf, void *start, size_t len)
1751{
1752 buf->head[0].iov_base = start;
1753 buf->head[0].iov_len = len;
1754 buf->tail[0].iov_len = 0;
1755 buf->page_len = 0;
4f22ccc3 1756 buf->flags = 0;
2bea90d4
CL
1757 buf->len = 0;
1758 buf->buflen = len;
1759}
1760
1da177e4
LT
1761/*
1762 * 3. Encode arguments of an RPC call
1763 */
1764static void
b0e1c57e 1765rpc_xdr_encode(struct rpc_task *task)
1da177e4 1766{
1da177e4 1767 struct rpc_rqst *req = task->tk_rqstp;
9f06c719 1768 kxdreproc_t encode;
d8ed029d 1769 __be32 *p;
1da177e4 1770
46121cf7 1771 dprint_status(task);
1da177e4 1772
2bea90d4
CL
1773 rpc_xdr_buf_init(&req->rq_snd_buf,
1774 req->rq_buffer,
1775 req->rq_callsize);
1776 rpc_xdr_buf_init(&req->rq_rcv_buf,
1777 (char *)req->rq_buffer + req->rq_callsize,
1778 req->rq_rcvsize);
1da177e4 1779
b0e1c57e
CL
1780 p = rpc_encode_header(task);
1781 if (p == NULL) {
1782 printk(KERN_INFO "RPC: couldn't encode RPC header, exit EIO\n");
1da177e4
LT
1783 rpc_exit(task, -EIO);
1784 return;
1785 }
b0e1c57e
CL
1786
1787 encode = task->tk_msg.rpc_proc->p_encode;
f3680312
BF
1788 if (encode == NULL)
1789 return;
1790
1791 task->tk_status = rpcauth_wrap_req(task, encode, req, p,
1792 task->tk_msg.rpc_argp);
1da177e4
LT
1793}
1794
1795/*
1796 * 4. Get the server port number if not yet set
1797 */
1798static void
1799call_bind(struct rpc_task *task)
1800{
ad2368d6 1801 struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
1da177e4 1802
46121cf7 1803 dprint_status(task);
1da177e4 1804
da351878 1805 task->tk_action = call_connect;
ec739ef0 1806 if (!xprt_bound(xprt)) {
da351878 1807 task->tk_action = call_bind_status;
ec739ef0 1808 task->tk_timeout = xprt->bind_timeout;
bbf7c1dd 1809 xprt->ops->rpcbind(task);
1da177e4
LT
1810 }
1811}
1812
1813/*
da351878
CL
1814 * 4a. Sort out bind result
1815 */
1816static void
1817call_bind_status(struct rpc_task *task)
1818{
906462af 1819 int status = -EIO;
da351878
CL
1820
1821 if (task->tk_status >= 0) {
46121cf7 1822 dprint_status(task);
da351878
CL
1823 task->tk_status = 0;
1824 task->tk_action = call_connect;
1825 return;
1826 }
1827
5753cba1 1828 trace_rpc_bind_status(task);
da351878 1829 switch (task->tk_status) {
381ba74a
TM
1830 case -ENOMEM:
1831 dprintk("RPC: %5u rpcbind out of memory\n", task->tk_pid);
1832 rpc_delay(task, HZ >> 2);
2429cbf6 1833 goto retry_timeout;
da351878 1834 case -EACCES:
46121cf7
CL
1835 dprintk("RPC: %5u remote rpcbind: RPC program/version "
1836 "unavailable\n", task->tk_pid);
b79dc8ce
CL
1837 /* fail immediately if this is an RPC ping */
1838 if (task->tk_msg.rpc_proc->p_proc == 0) {
1839 status = -EOPNOTSUPP;
1840 break;
1841 }
0b760113
TM
1842 if (task->tk_rebind_retry == 0)
1843 break;
1844 task->tk_rebind_retry--;
ea635a51 1845 rpc_delay(task, 3*HZ);
da45828e 1846 goto retry_timeout;
da351878 1847 case -ETIMEDOUT:
46121cf7 1848 dprintk("RPC: %5u rpcbind request timed out\n",
da351878 1849 task->tk_pid);
da45828e 1850 goto retry_timeout;
da351878 1851 case -EPFNOSUPPORT:
906462af 1852 /* server doesn't support any rpcbind version we know of */
012da158 1853 dprintk("RPC: %5u unrecognized remote rpcbind service\n",
da351878
CL
1854 task->tk_pid);
1855 break;
1856 case -EPROTONOSUPPORT:
00a6e7bb 1857 dprintk("RPC: %5u remote rpcbind version unavailable, retrying\n",
da351878 1858 task->tk_pid);
fdb63dcd 1859 goto retry_timeout;
012da158
CL
1860 case -ECONNREFUSED: /* connection problems */
1861 case -ECONNRESET:
df277270 1862 case -ECONNABORTED:
012da158
CL
1863 case -ENOTCONN:
1864 case -EHOSTDOWN:
1865 case -EHOSTUNREACH:
1866 case -ENETUNREACH:
3601c4a9 1867 case -ENOBUFS:
012da158
CL
1868 case -EPIPE:
1869 dprintk("RPC: %5u remote rpcbind unreachable: %d\n",
1870 task->tk_pid, task->tk_status);
1871 if (!RPC_IS_SOFTCONN(task)) {
1872 rpc_delay(task, 5*HZ);
1873 goto retry_timeout;
1874 }
1875 status = task->tk_status;
1876 break;
da351878 1877 default:
46121cf7 1878 dprintk("RPC: %5u unrecognized rpcbind error (%d)\n",
da351878 1879 task->tk_pid, -task->tk_status);
da351878
CL
1880 }
1881
1882 rpc_exit(task, status);
1883 return;
1884
da45828e 1885retry_timeout:
fdb63dcd 1886 task->tk_status = 0;
da45828e 1887 task->tk_action = call_timeout;
da351878
CL
1888}
1889
1890/*
1891 * 4b. Connect to the RPC server
1da177e4
LT
1892 */
1893static void
1894call_connect(struct rpc_task *task)
1895{
ad2368d6 1896 struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
1da177e4 1897
46121cf7 1898 dprintk("RPC: %5u call_connect xprt %p %s connected\n",
da351878
CL
1899 task->tk_pid, xprt,
1900 (xprt_connected(xprt) ? "is" : "is not"));
1da177e4 1901
da351878
CL
1902 task->tk_action = call_transmit;
1903 if (!xprt_connected(xprt)) {
1904 task->tk_action = call_connect_status;
1905 if (task->tk_status < 0)
1906 return;
786615bc
TM
1907 if (task->tk_flags & RPC_TASK_NOCONNECT) {
1908 rpc_exit(task, -ENOTCONN);
1909 return;
1910 }
da351878 1911 xprt_connect(task);
1da177e4 1912 }
1da177e4
LT
1913}
1914
1915/*
da351878 1916 * 4c. Sort out connect result
1da177e4
LT
1917 */
1918static void
1919call_connect_status(struct rpc_task *task)
1920{
1921 struct rpc_clnt *clnt = task->tk_client;
1922 int status = task->tk_status;
1923
46121cf7 1924 dprint_status(task);
da351878 1925
5753cba1 1926 trace_rpc_connect_status(task, status);
561ec160 1927 task->tk_status = 0;
1da177e4 1928 switch (status) {
3ed5e2a2
TM
1929 case -ECONNREFUSED:
1930 case -ECONNRESET:
df277270 1931 case -ECONNABORTED:
3ed5e2a2 1932 case -ENETUNREACH:
df277270 1933 case -EHOSTUNREACH:
3913c78c 1934 case -EADDRINUSE:
3601c4a9 1935 case -ENOBUFS:
2fc193cf 1936 case -EPIPE:
3ed5e2a2
TM
1937 if (RPC_IS_SOFTCONN(task))
1938 break;
1fa3e2eb
SD
1939 /* retry with existing socket, after a delay */
1940 rpc_delay(task, 3*HZ);
3ed5e2a2 1941 case -EAGAIN:
485f2251
TM
1942 /* Check for timeouts before looping back to call_bind */
1943 case -ETIMEDOUT:
1944 task->tk_action = call_timeout;
561ec160
TM
1945 return;
1946 case 0:
3ed5e2a2
TM
1947 clnt->cl_stats->netreconn++;
1948 task->tk_action = call_transmit;
1949 return;
1da177e4 1950 }
3ed5e2a2 1951 rpc_exit(task, status);
1da177e4
LT
1952}
1953
1954/*
1955 * 5. Transmit the RPC request, and wait for reply
1956 */
1957static void
1958call_transmit(struct rpc_task *task)
1959{
ca7f33aa
TM
1960 int is_retrans = RPC_WAS_SENT(task);
1961
46121cf7 1962 dprint_status(task);
1da177e4
LT
1963
1964 task->tk_action = call_status;
1965 if (task->tk_status < 0)
1966 return;
90051ea7 1967 if (!xprt_prepare_transmit(task))
1da177e4 1968 return;
e0ab53de 1969 task->tk_action = call_transmit_status;
1da177e4 1970 /* Encode here so that rpcsec_gss can use correct sequence number. */
940e3318 1971 if (rpc_task_need_encode(task)) {
b0e1c57e 1972 rpc_xdr_encode(task);
5e5ce5be 1973 /* Did the encode result in an error condition? */
8b39f2b4
TM
1974 if (task->tk_status != 0) {
1975 /* Was the error nonfatal? */
1976 if (task->tk_status == -EAGAIN)
1977 rpc_delay(task, HZ >> 4);
1978 else
1979 rpc_exit(task, task->tk_status);
e0ab53de 1980 return;
8b39f2b4 1981 }
5e5ce5be 1982 }
1da177e4
LT
1983 xprt_transmit(task);
1984 if (task->tk_status < 0)
1985 return;
ca7f33aa
TM
1986 if (is_retrans)
1987 task->tk_client->cl_stats->rpcretrans++;
e0ab53de
TM
1988 /*
1989 * On success, ensure that we call xprt_end_transmit() before sleeping
1990 * in order to allow access to the socket to other RPC requests.
1991 */
1992 call_transmit_status(task);
55ae1aab 1993 if (rpc_reply_expected(task))
e0ab53de
TM
1994 return;
1995 task->tk_action = rpc_exit_task;
a4f0835c 1996 rpc_wake_up_queued_task(&task->tk_rqstp->rq_xprt->pending, task);
e0ab53de
TM
1997}
1998
1999/*
2000 * 5a. Handle cleanup after a transmission
2001 */
2002static void
2003call_transmit_status(struct rpc_task *task)
2004{
2005 task->tk_action = call_status;
206a134b
CL
2006
2007 /*
2008 * Common case: success. Force the compiler to put this
2009 * test first.
2010 */
2011 if (task->tk_status == 0) {
2012 xprt_end_transmit(task);
2013 rpc_task_force_reencode(task);
2014 return;
2015 }
2016
15f081ca
TM
2017 switch (task->tk_status) {
2018 case -EAGAIN:
93aa6c7b 2019 case -ENOBUFS:
15f081ca
TM
2020 break;
2021 default:
206a134b 2022 dprint_status(task);
15f081ca 2023 xprt_end_transmit(task);
09a21c41
CL
2024 rpc_task_force_reencode(task);
2025 break;
15f081ca
TM
2026 /*
2027 * Special cases: if we've been waiting on the
2028 * socket's write_space() callback, or if the
2029 * socket just returned a connection error,
2030 * then hold onto the transport lock.
2031 */
2032 case -ECONNREFUSED:
15f081ca
TM
2033 case -EHOSTDOWN:
2034 case -EHOSTUNREACH:
2035 case -ENETUNREACH:
3dedbb5c 2036 case -EPERM:
09a21c41
CL
2037 if (RPC_IS_SOFTCONN(task)) {
2038 xprt_end_transmit(task);
2039 rpc_exit(task, task->tk_status);
2040 break;
2041 }
2042 case -ECONNRESET:
df277270 2043 case -ECONNABORTED:
3913c78c 2044 case -EADDRINUSE:
09a21c41 2045 case -ENOTCONN:
c8485e4d 2046 case -EPIPE:
15f081ca
TM
2047 rpc_task_force_reencode(task);
2048 }
1da177e4
LT
2049}
2050
9e00abc3 2051#if defined(CONFIG_SUNRPC_BACKCHANNEL)
55ae1aab
RL
2052/*
2053 * 5b. Send the backchannel RPC reply. On error, drop the reply. In
2054 * addition, disconnect on connectivity errors.
2055 */
2056static void
2057call_bc_transmit(struct rpc_task *task)
2058{
2059 struct rpc_rqst *req = task->tk_rqstp;
2060
1193d58f
TM
2061 if (!xprt_prepare_transmit(task))
2062 goto out_retry;
55ae1aab 2063
55ae1aab
RL
2064 if (task->tk_status < 0) {
2065 printk(KERN_NOTICE "RPC: Could not send backchannel reply "
2066 "error: %d\n", task->tk_status);
1193d58f 2067 goto out_done;
55ae1aab 2068 }
1193d58f
TM
2069 if (req->rq_connect_cookie != req->rq_xprt->connect_cookie)
2070 req->rq_bytes_sent = 0;
55ae1aab
RL
2071
2072 xprt_transmit(task);
1193d58f
TM
2073
2074 if (task->tk_status == -EAGAIN)
2075 goto out_nospace;
2076
55ae1aab
RL
2077 xprt_end_transmit(task);
2078 dprint_status(task);
2079 switch (task->tk_status) {
2080 case 0:
2081 /* Success */
55ae1aab
RL
2082 case -EHOSTDOWN:
2083 case -EHOSTUNREACH:
2084 case -ENETUNREACH:
3832591e
TM
2085 case -ECONNRESET:
2086 case -ECONNREFUSED:
2087 case -EADDRINUSE:
2088 case -ENOTCONN:
2089 case -EPIPE:
2090 break;
55ae1aab
RL
2091 case -ETIMEDOUT:
2092 /*
2093 * Problem reaching the server. Disconnect and let the
2094 * forechannel reestablish the connection. The server will
2095 * have to retransmit the backchannel request and we'll
2096 * reprocess it. Since these ops are idempotent, there's no
2097 * need to cache our reply at this time.
2098 */
2099 printk(KERN_NOTICE "RPC: Could not send backchannel reply "
2100 "error: %d\n", task->tk_status);
a4f0835c 2101 xprt_conditional_disconnect(req->rq_xprt,
55ae1aab
RL
2102 req->rq_connect_cookie);
2103 break;
2104 default:
2105 /*
2106 * We were unable to reply and will have to drop the
2107 * request. The server should reconnect and retransmit.
2108 */
1facf4c4 2109 WARN_ON_ONCE(task->tk_status == -EAGAIN);
55ae1aab
RL
2110 printk(KERN_NOTICE "RPC: Could not send backchannel reply "
2111 "error: %d\n", task->tk_status);
2112 break;
2113 }
2114 rpc_wake_up_queued_task(&req->rq_xprt->pending, task);
1193d58f
TM
2115out_done:
2116 task->tk_action = rpc_exit_task;
2117 return;
2118out_nospace:
2119 req->rq_connect_cookie = req->rq_xprt->connect_cookie;
2120out_retry:
2121 task->tk_status = 0;
55ae1aab 2122}
9e00abc3 2123#endif /* CONFIG_SUNRPC_BACKCHANNEL */
55ae1aab 2124
1da177e4
LT
2125/*
2126 * 6. Sort out the RPC call status
2127 */
2128static void
2129call_status(struct rpc_task *task)
2130{
2131 struct rpc_clnt *clnt = task->tk_client;
2132 struct rpc_rqst *req = task->tk_rqstp;
2133 int status;
2134
dd2b63d0
RL
2135 if (req->rq_reply_bytes_recvd > 0 && !req->rq_bytes_sent)
2136 task->tk_status = req->rq_reply_bytes_recvd;
1da177e4 2137
46121cf7 2138 dprint_status(task);
1da177e4
LT
2139
2140 status = task->tk_status;
2141 if (status >= 0) {
2142 task->tk_action = call_decode;
2143 return;
2144 }
2145
5753cba1 2146 trace_rpc_call_status(task);
1da177e4
LT
2147 task->tk_status = 0;
2148 switch(status) {
76303992
TM
2149 case -EHOSTDOWN:
2150 case -EHOSTUNREACH:
2151 case -ENETUNREACH:
3dedbb5c 2152 case -EPERM:
9455e3f4
TM
2153 if (RPC_IS_SOFTCONN(task)) {
2154 rpc_exit(task, status);
2155 break;
2156 }
76303992
TM
2157 /*
2158 * Delay any retries for 3 seconds, then handle as if it
2159 * were a timeout.
2160 */
2161 rpc_delay(task, 3*HZ);
1da177e4
LT
2162 case -ETIMEDOUT:
2163 task->tk_action = call_timeout;
8a19a0b6
TM
2164 if (!(task->tk_flags & RPC_TASK_NO_RETRANS_TIMEOUT)
2165 && task->tk_client->cl_discrtry)
a4f0835c 2166 xprt_conditional_disconnect(req->rq_xprt,
7c1d71cf 2167 req->rq_connect_cookie);
1da177e4
LT
2168 break;
2169 case -ECONNREFUSED:
df277270
TM
2170 case -ECONNRESET:
2171 case -ECONNABORTED:
35f5a422 2172 rpc_force_rebind(clnt);
3913c78c 2173 case -EADDRINUSE:
c8485e4d
TM
2174 rpc_delay(task, 3*HZ);
2175 case -EPIPE:
2176 case -ENOTCONN:
1da177e4
LT
2177 task->tk_action = call_bind;
2178 break;
93aa6c7b
TM
2179 case -ENOBUFS:
2180 rpc_delay(task, HZ>>2);
1da177e4
LT
2181 case -EAGAIN:
2182 task->tk_action = call_transmit;
2183 break;
2184 case -EIO:
2185 /* shutdown or soft timeout */
2186 rpc_exit(task, status);
2187 break;
2188 default:
b6b6152c
OK
2189 if (clnt->cl_chatty)
2190 printk("%s: RPC call returned error %d\n",
55909f21 2191 clnt->cl_program->name, -status);
1da177e4 2192 rpc_exit(task, status);
1da177e4
LT
2193 }
2194}
2195
2196/*
e0ab53de 2197 * 6a. Handle RPC timeout
1da177e4
LT
2198 * We do not release the request slot, so we keep using the
2199 * same XID for all retransmits.
2200 */
2201static void
2202call_timeout(struct rpc_task *task)
2203{
2204 struct rpc_clnt *clnt = task->tk_client;
2205
2206 if (xprt_adjust_timeout(task->tk_rqstp) == 0) {
46121cf7 2207 dprintk("RPC: %5u call_timeout (minor)\n", task->tk_pid);
1da177e4
LT
2208 goto retry;
2209 }
2210
46121cf7 2211 dprintk("RPC: %5u call_timeout (major)\n", task->tk_pid);
ef759a2e
CL
2212 task->tk_timeouts++;
2213
3a28becc
CL
2214 if (RPC_IS_SOFTCONN(task)) {
2215 rpc_exit(task, -ETIMEDOUT);
2216 return;
2217 }
1da177e4 2218 if (RPC_IS_SOFT(task)) {
cac5d07e 2219 if (clnt->cl_chatty) {
b6b6152c 2220 printk(KERN_NOTICE "%s: server %s not responding, timed out\n",
55909f21 2221 clnt->cl_program->name,
fb43d172 2222 task->tk_xprt->servername);
cac5d07e 2223 }
7494d00c
TM
2224 if (task->tk_flags & RPC_TASK_TIMEOUT)
2225 rpc_exit(task, -ETIMEDOUT);
2226 else
2227 rpc_exit(task, -EIO);
1da177e4
LT
2228 return;
2229 }
2230
f518e35a 2231 if (!(task->tk_flags & RPC_CALL_MAJORSEEN)) {
1da177e4 2232 task->tk_flags |= RPC_CALL_MAJORSEEN;
4e0038b6 2233 if (clnt->cl_chatty) {
b6b6152c 2234 printk(KERN_NOTICE "%s: server %s not responding, still trying\n",
55909f21 2235 clnt->cl_program->name,
fb43d172 2236 task->tk_xprt->servername);
4e0038b6 2237 }
1da177e4 2238 }
35f5a422 2239 rpc_force_rebind(clnt);
b48633bd
TM
2240 /*
2241 * Did our request time out due to an RPCSEC_GSS out-of-sequence
2242 * event? RFC2203 requires the server to drop all such requests.
2243 */
2244 rpcauth_invalcred(task);
1da177e4
LT
2245
2246retry:
1da177e4
LT
2247 task->tk_action = call_bind;
2248 task->tk_status = 0;
2249}
2250
2251/*
2252 * 7. Decode the RPC reply
2253 */
2254static void
2255call_decode(struct rpc_task *task)
2256{
2257 struct rpc_clnt *clnt = task->tk_client;
2258 struct rpc_rqst *req = task->tk_rqstp;
bf269551 2259 kxdrdproc_t decode = task->tk_msg.rpc_proc->p_decode;
d8ed029d 2260 __be32 *p;
1da177e4 2261
726fd6ad 2262 dprint_status(task);
1da177e4 2263
f518e35a 2264 if (task->tk_flags & RPC_CALL_MAJORSEEN) {
4e0038b6 2265 if (clnt->cl_chatty) {
b6b6152c 2266 printk(KERN_NOTICE "%s: server %s OK\n",
55909f21 2267 clnt->cl_program->name,
fb43d172 2268 task->tk_xprt->servername);
4e0038b6 2269 }
1da177e4
LT
2270 task->tk_flags &= ~RPC_CALL_MAJORSEEN;
2271 }
2272
43ac3f29
TM
2273 /*
2274 * Ensure that we see all writes made by xprt_complete_rqst()
dd2b63d0 2275 * before it changed req->rq_reply_bytes_recvd.
43ac3f29
TM
2276 */
2277 smp_rmb();
1da177e4
LT
2278 req->rq_rcv_buf.len = req->rq_private_buf.len;
2279
2280 /* Check that the softirq receive buffer is valid */
2281 WARN_ON(memcmp(&req->rq_rcv_buf, &req->rq_private_buf,
2282 sizeof(req->rq_rcv_buf)) != 0);
2283
1e799b67
TM
2284 if (req->rq_rcv_buf.len < 12) {
2285 if (!RPC_IS_SOFT(task)) {
2286 task->tk_action = call_bind;
1e799b67
TM
2287 goto out_retry;
2288 }
2289 dprintk("RPC: %s: too small RPC reply size (%d bytes)\n",
55909f21 2290 clnt->cl_program->name, task->tk_status);
1e799b67
TM
2291 task->tk_action = call_timeout;
2292 goto out_retry;
2293 }
2294
b0e1c57e 2295 p = rpc_verify_header(task);
abbcf28f
TM
2296 if (IS_ERR(p)) {
2297 if (p == ERR_PTR(-EAGAIN))
2298 goto out_retry;
2299 return;
1da177e4
LT
2300 }
2301
abbcf28f 2302 task->tk_action = rpc_exit_task;
1da177e4 2303
6d5fcb5a 2304 if (decode) {
1da177e4
LT
2305 task->tk_status = rpcauth_unwrap_resp(task, decode, req, p,
2306 task->tk_msg.rpc_resp);
6d5fcb5a 2307 }
46121cf7
CL
2308 dprintk("RPC: %5u call_decode result %d\n", task->tk_pid,
2309 task->tk_status);
1da177e4
LT
2310 return;
2311out_retry:
1da177e4 2312 task->tk_status = 0;
b0e1c57e 2313 /* Note: rpc_verify_header() may have freed the RPC slot */
24b74bf0 2314 if (task->tk_rqstp == req) {
dd2b63d0 2315 req->rq_reply_bytes_recvd = req->rq_rcv_buf.len = 0;
24b74bf0 2316 if (task->tk_client->cl_discrtry)
a4f0835c 2317 xprt_conditional_disconnect(req->rq_xprt,
7c1d71cf 2318 req->rq_connect_cookie);
24b74bf0 2319 }
1da177e4
LT
2320}
2321
d8ed029d 2322static __be32 *
b0e1c57e 2323rpc_encode_header(struct rpc_task *task)
1da177e4
LT
2324{
2325 struct rpc_clnt *clnt = task->tk_client;
1da177e4 2326 struct rpc_rqst *req = task->tk_rqstp;
d8ed029d 2327 __be32 *p = req->rq_svec[0].iov_base;
1da177e4
LT
2328
2329 /* FIXME: check buffer size? */
808012fb 2330
a4f0835c 2331 p = xprt_skip_transport_header(req->rq_xprt, p);
1da177e4
LT
2332 *p++ = req->rq_xid; /* XID */
2333 *p++ = htonl(RPC_CALL); /* CALL */
2334 *p++ = htonl(RPC_VERSION); /* RPC version */
2335 *p++ = htonl(clnt->cl_prog); /* program number */
2336 *p++ = htonl(clnt->cl_vers); /* program version */
2337 *p++ = htonl(task->tk_msg.rpc_proc->p_proc); /* procedure */
334ccfd5
TM
2338 p = rpcauth_marshcred(task, p);
2339 req->rq_slen = xdr_adjust_iovec(&req->rq_svec[0], p);
2340 return p;
1da177e4
LT
2341}
2342
d8ed029d 2343static __be32 *
b0e1c57e 2344rpc_verify_header(struct rpc_task *task)
1da177e4 2345{
4e0038b6 2346 struct rpc_clnt *clnt = task->tk_client;
1da177e4
LT
2347 struct kvec *iov = &task->tk_rqstp->rq_rcv_buf.head[0];
2348 int len = task->tk_rqstp->rq_rcv_buf.len >> 2;
d8ed029d
AD
2349 __be32 *p = iov->iov_base;
2350 u32 n;
1da177e4
LT
2351 int error = -EACCES;
2352
e8896495
DH
2353 if ((task->tk_rqstp->rq_rcv_buf.len & 3) != 0) {
2354 /* RFC-1014 says that the representation of XDR data must be a
2355 * multiple of four bytes
2356 * - if it isn't pointer subtraction in the NFS client may give
2357 * undefined results
2358 */
8a702bbb 2359 dprintk("RPC: %5u %s: XDR representation not a multiple of"
0dc47877 2360 " 4 bytes: 0x%x\n", task->tk_pid, __func__,
8a702bbb 2361 task->tk_rqstp->rq_rcv_buf.len);
35fa5f7b
AA
2362 error = -EIO;
2363 goto out_err;
e8896495 2364 }
1da177e4
LT
2365 if ((len -= 3) < 0)
2366 goto out_overflow;
1da177e4 2367
f4a2e418 2368 p += 1; /* skip XID */
1da177e4 2369 if ((n = ntohl(*p++)) != RPC_REPLY) {
8a702bbb 2370 dprintk("RPC: %5u %s: not an RPC reply: %x\n",
f4a2e418 2371 task->tk_pid, __func__, n);
35fa5f7b 2372 error = -EIO;
abbcf28f 2373 goto out_garbage;
1da177e4 2374 }
f4a2e418 2375
1da177e4
LT
2376 if ((n = ntohl(*p++)) != RPC_MSG_ACCEPTED) {
2377 if (--len < 0)
2378 goto out_overflow;
2379 switch ((n = ntohl(*p++))) {
89f0e4fe
JP
2380 case RPC_AUTH_ERROR:
2381 break;
2382 case RPC_MISMATCH:
2383 dprintk("RPC: %5u %s: RPC call version mismatch!\n",
2384 task->tk_pid, __func__);
2385 error = -EPROTONOSUPPORT;
2386 goto out_err;
2387 default:
2388 dprintk("RPC: %5u %s: RPC call rejected, "
2389 "unknown error: %x\n",
2390 task->tk_pid, __func__, n);
35fa5f7b
AA
2391 error = -EIO;
2392 goto out_err;
1da177e4
LT
2393 }
2394 if (--len < 0)
2395 goto out_overflow;
2396 switch ((n = ntohl(*p++))) {
2397 case RPC_AUTH_REJECTEDCRED:
2398 case RPC_AUTH_REJECTEDVERF:
2399 case RPCSEC_GSS_CREDPROBLEM:
2400 case RPCSEC_GSS_CTXPROBLEM:
2401 if (!task->tk_cred_retry)
2402 break;
2403 task->tk_cred_retry--;
46121cf7 2404 dprintk("RPC: %5u %s: retry stale creds\n",
0dc47877 2405 task->tk_pid, __func__);
1da177e4 2406 rpcauth_invalcred(task);
220bcc2a
TM
2407 /* Ensure we obtain a new XID! */
2408 xprt_release(task);
118df3d1 2409 task->tk_action = call_reserve;
abbcf28f 2410 goto out_retry;
1da177e4
LT
2411 case RPC_AUTH_BADCRED:
2412 case RPC_AUTH_BADVERF:
2413 /* possibly garbled cred/verf? */
2414 if (!task->tk_garb_retry)
2415 break;
2416 task->tk_garb_retry--;
46121cf7 2417 dprintk("RPC: %5u %s: retry garbled creds\n",
0dc47877 2418 task->tk_pid, __func__);
1da177e4 2419 task->tk_action = call_bind;
abbcf28f 2420 goto out_retry;
1da177e4 2421 case RPC_AUTH_TOOWEAK:
b0e1c57e 2422 printk(KERN_NOTICE "RPC: server %s requires stronger "
4e0038b6 2423 "authentication.\n",
fb43d172 2424 task->tk_xprt->servername);
1da177e4
LT
2425 break;
2426 default:
8a702bbb 2427 dprintk("RPC: %5u %s: unknown auth error: %x\n",
0dc47877 2428 task->tk_pid, __func__, n);
1da177e4
LT
2429 error = -EIO;
2430 }
46121cf7 2431 dprintk("RPC: %5u %s: call rejected %d\n",
0dc47877 2432 task->tk_pid, __func__, n);
1da177e4
LT
2433 goto out_err;
2434 }
35fa5f7b
AA
2435 p = rpcauth_checkverf(task, p);
2436 if (IS_ERR(p)) {
2437 error = PTR_ERR(p);
2438 dprintk("RPC: %5u %s: auth check failed with %d\n",
2439 task->tk_pid, __func__, error);
abbcf28f 2440 goto out_garbage; /* bad verifier, retry */
1da177e4 2441 }
d8ed029d 2442 len = p - (__be32 *)iov->iov_base - 1;
1da177e4
LT
2443 if (len < 0)
2444 goto out_overflow;
2445 switch ((n = ntohl(*p++))) {
2446 case RPC_SUCCESS:
2447 return p;
2448 case RPC_PROG_UNAVAIL:
fb43d172 2449 dprintk("RPC: %5u %s: program %u is unsupported "
4e0038b6
TM
2450 "by server %s\n", task->tk_pid, __func__,
2451 (unsigned int)clnt->cl_prog,
fb43d172 2452 task->tk_xprt->servername);
cdf47706
AG
2453 error = -EPFNOSUPPORT;
2454 goto out_err;
1da177e4 2455 case RPC_PROG_MISMATCH:
fb43d172 2456 dprintk("RPC: %5u %s: program %u, version %u unsupported "
4e0038b6
TM
2457 "by server %s\n", task->tk_pid, __func__,
2458 (unsigned int)clnt->cl_prog,
2459 (unsigned int)clnt->cl_vers,
fb43d172 2460 task->tk_xprt->servername);
cdf47706
AG
2461 error = -EPROTONOSUPPORT;
2462 goto out_err;
1da177e4 2463 case RPC_PROC_UNAVAIL:
fb43d172 2464 dprintk("RPC: %5u %s: proc %s unsupported by program %u, "
46121cf7 2465 "version %u on server %s\n",
0dc47877 2466 task->tk_pid, __func__,
3748f1e4 2467 rpc_proc_name(task),
4e0038b6 2468 clnt->cl_prog, clnt->cl_vers,
fb43d172 2469 task->tk_xprt->servername);
cdf47706
AG
2470 error = -EOPNOTSUPP;
2471 goto out_err;
1da177e4 2472 case RPC_GARBAGE_ARGS:
46121cf7 2473 dprintk("RPC: %5u %s: server saw garbage\n",
0dc47877 2474 task->tk_pid, __func__);
1da177e4
LT
2475 break; /* retry */
2476 default:
8a702bbb 2477 dprintk("RPC: %5u %s: server accept status: %x\n",
0dc47877 2478 task->tk_pid, __func__, n);
1da177e4
LT
2479 /* Also retry */
2480 }
2481
abbcf28f 2482out_garbage:
4e0038b6 2483 clnt->cl_stats->rpcgarbage++;
1da177e4
LT
2484 if (task->tk_garb_retry) {
2485 task->tk_garb_retry--;
46121cf7 2486 dprintk("RPC: %5u %s: retrying\n",
0dc47877 2487 task->tk_pid, __func__);
1da177e4 2488 task->tk_action = call_bind;
abbcf28f
TM
2489out_retry:
2490 return ERR_PTR(-EAGAIN);
1da177e4 2491 }
1da177e4
LT
2492out_err:
2493 rpc_exit(task, error);
8a702bbb 2494 dprintk("RPC: %5u %s: call failed with error %d\n", task->tk_pid,
0dc47877 2495 __func__, error);
abbcf28f 2496 return ERR_PTR(error);
1da177e4 2497out_overflow:
8a702bbb 2498 dprintk("RPC: %5u %s: server reply was truncated.\n", task->tk_pid,
0dc47877 2499 __func__);
abbcf28f 2500 goto out_garbage;
1da177e4 2501}
5ee0ed7d 2502
9f06c719 2503static void rpcproc_encode_null(void *rqstp, struct xdr_stream *xdr, void *obj)
5ee0ed7d 2504{
5ee0ed7d
TM
2505}
2506
bf269551 2507static int rpcproc_decode_null(void *rqstp, struct xdr_stream *xdr, void *obj)
5ee0ed7d
TM
2508{
2509 return 0;
2510}
2511
2512static struct rpc_procinfo rpcproc_null = {
2513 .p_encode = rpcproc_encode_null,
2514 .p_decode = rpcproc_decode_null,
2515};
2516
caabea8a 2517static int rpc_ping(struct rpc_clnt *clnt)
5ee0ed7d
TM
2518{
2519 struct rpc_message msg = {
2520 .rpc_proc = &rpcproc_null,
2521 };
2522 int err;
2523 msg.rpc_cred = authnull_ops.lookup_cred(NULL, NULL, 0);
caabea8a 2524 err = rpc_call_sync(clnt, &msg, RPC_TASK_SOFT | RPC_TASK_SOFTCONN);
5ee0ed7d
TM
2525 put_rpccred(msg.rpc_cred);
2526 return err;
2527}
188fef11 2528
7f554890
TM
2529static
2530struct rpc_task *rpc_call_null_helper(struct rpc_clnt *clnt,
2531 struct rpc_xprt *xprt, struct rpc_cred *cred, int flags,
2532 const struct rpc_call_ops *ops, void *data)
5e1550d6
TM
2533{
2534 struct rpc_message msg = {
2535 .rpc_proc = &rpcproc_null,
2536 .rpc_cred = cred,
2537 };
84115e1c
TM
2538 struct rpc_task_setup task_setup_data = {
2539 .rpc_client = clnt,
7f554890 2540 .rpc_xprt = xprt,
84115e1c 2541 .rpc_message = &msg,
7f554890
TM
2542 .callback_ops = (ops != NULL) ? ops : &rpc_default_ops,
2543 .callback_data = data,
84115e1c
TM
2544 .flags = flags,
2545 };
7f554890 2546
c970aa85 2547 return rpc_run_task(&task_setup_data);
5e1550d6 2548}
7f554890
TM
2549
2550struct rpc_task *rpc_call_null(struct rpc_clnt *clnt, struct rpc_cred *cred, int flags)
2551{
2552 return rpc_call_null_helper(clnt, NULL, cred, flags, NULL, NULL);
2553}
e8914c65 2554EXPORT_SYMBOL_GPL(rpc_call_null);
5e1550d6 2555
7f554890
TM
2556struct rpc_cb_add_xprt_calldata {
2557 struct rpc_xprt_switch *xps;
2558 struct rpc_xprt *xprt;
2559};
2560
2561static void rpc_cb_add_xprt_done(struct rpc_task *task, void *calldata)
2562{
2563 struct rpc_cb_add_xprt_calldata *data = calldata;
2564
2565 if (task->tk_status == 0)
2566 rpc_xprt_switch_add_xprt(data->xps, data->xprt);
2567}
2568
2569static void rpc_cb_add_xprt_release(void *calldata)
2570{
2571 struct rpc_cb_add_xprt_calldata *data = calldata;
2572
2573 xprt_put(data->xprt);
2574 xprt_switch_put(data->xps);
2575 kfree(data);
2576}
2577
ce272302 2578static const struct rpc_call_ops rpc_cb_add_xprt_call_ops = {
7f554890
TM
2579 .rpc_call_done = rpc_cb_add_xprt_done,
2580 .rpc_release = rpc_cb_add_xprt_release,
2581};
2582
2583/**
2584 * rpc_clnt_test_and_add_xprt - Test and add a new transport to a rpc_clnt
2585 * @clnt: pointer to struct rpc_clnt
2586 * @xps: pointer to struct rpc_xprt_switch,
2587 * @xprt: pointer struct rpc_xprt
2588 * @dummy: unused
2589 */
2590int rpc_clnt_test_and_add_xprt(struct rpc_clnt *clnt,
2591 struct rpc_xprt_switch *xps, struct rpc_xprt *xprt,
2592 void *dummy)
2593{
2594 struct rpc_cb_add_xprt_calldata *data;
2595 struct rpc_cred *cred;
2596 struct rpc_task *task;
2597
2598 data = kmalloc(sizeof(*data), GFP_NOFS);
2599 if (!data)
2600 return -ENOMEM;
2601 data->xps = xprt_switch_get(xps);
2602 data->xprt = xprt_get(xprt);
2603
2604 cred = authnull_ops.lookup_cred(NULL, NULL, 0);
2605 task = rpc_call_null_helper(clnt, xprt, cred,
2606 RPC_TASK_SOFT|RPC_TASK_SOFTCONN|RPC_TASK_ASYNC,
2607 &rpc_cb_add_xprt_call_ops, data);
2608 put_rpccred(cred);
2609 if (IS_ERR(task))
2610 return PTR_ERR(task);
2611 rpc_put_task(task);
2612 return 1;
2613}
2614EXPORT_SYMBOL_GPL(rpc_clnt_test_and_add_xprt);
2615
2616/**
2617 * rpc_clnt_add_xprt - Add a new transport to a rpc_clnt
2618 * @clnt: pointer to struct rpc_clnt
2619 * @xprtargs: pointer to struct xprt_create
2620 * @setup: callback to test and/or set up the connection
2621 * @data: pointer to setup function data
2622 *
2623 * Creates a new transport using the parameters set in args and
2624 * adds it to clnt.
2625 * If ping is set, then test that connectivity succeeds before
2626 * adding the new transport.
2627 *
2628 */
2629int rpc_clnt_add_xprt(struct rpc_clnt *clnt,
2630 struct xprt_create *xprtargs,
2631 int (*setup)(struct rpc_clnt *,
2632 struct rpc_xprt_switch *,
2633 struct rpc_xprt *,
2634 void *),
2635 void *data)
2636{
2637 struct rpc_xprt_switch *xps;
2638 struct rpc_xprt *xprt;
3851f1cd 2639 unsigned long reconnect_timeout;
7f554890
TM
2640 unsigned char resvport;
2641 int ret = 0;
2642
2643 rcu_read_lock();
2644 xps = xprt_switch_get(rcu_dereference(clnt->cl_xpi.xpi_xpswitch));
2645 xprt = xprt_iter_xprt(&clnt->cl_xpi);
2646 if (xps == NULL || xprt == NULL) {
2647 rcu_read_unlock();
2648 return -EAGAIN;
2649 }
2650 resvport = xprt->resvport;
3851f1cd 2651 reconnect_timeout = xprt->max_reconnect_timeout;
7f554890
TM
2652 rcu_read_unlock();
2653
2654 xprt = xprt_create_transport(xprtargs);
2655 if (IS_ERR(xprt)) {
2656 ret = PTR_ERR(xprt);
2657 goto out_put_switch;
2658 }
2659 xprt->resvport = resvport;
3851f1cd 2660 xprt->max_reconnect_timeout = reconnect_timeout;
7f554890
TM
2661
2662 rpc_xprt_switch_set_roundrobin(xps);
2663 if (setup) {
2664 ret = setup(clnt, xps, xprt, data);
2665 if (ret != 0)
2666 goto out_put_xprt;
2667 }
2668 rpc_xprt_switch_add_xprt(xps, xprt);
2669out_put_xprt:
2670 xprt_put(xprt);
2671out_put_switch:
2672 xprt_switch_put(xps);
2673 return ret;
2674}
2675EXPORT_SYMBOL_GPL(rpc_clnt_add_xprt);
2676
8d480326
TM
2677static int
2678rpc_xprt_cap_max_reconnect_timeout(struct rpc_clnt *clnt,
2679 struct rpc_xprt *xprt,
2680 void *data)
2681{
2682 unsigned long timeout = *((unsigned long *)data);
2683
2684 if (timeout < xprt->max_reconnect_timeout)
2685 xprt->max_reconnect_timeout = timeout;
2686 return 0;
2687}
2688
2689void
2690rpc_cap_max_reconnect_timeout(struct rpc_clnt *clnt, unsigned long timeo)
2691{
2692 rpc_clnt_iterate_for_each_xprt(clnt,
2693 rpc_xprt_cap_max_reconnect_timeout,
2694 &timeo);
2695}
2696EXPORT_SYMBOL_GPL(rpc_cap_max_reconnect_timeout);
2697
3b58a8a9
AA
2698void rpc_clnt_xprt_switch_put(struct rpc_clnt *clnt)
2699{
2700 xprt_switch_put(rcu_dereference(clnt->cl_xpi.xpi_xpswitch));
2701}
2702EXPORT_SYMBOL_GPL(rpc_clnt_xprt_switch_put);
2703
f895b252 2704#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
68a23ee9
CL
2705static void rpc_show_header(void)
2706{
cb3997b5
CL
2707 printk(KERN_INFO "-pid- flgs status -client- --rqstp- "
2708 "-timeout ---ops--\n");
68a23ee9
CL
2709}
2710
38e886e0
CL
2711static void rpc_show_task(const struct rpc_clnt *clnt,
2712 const struct rpc_task *task)
2713{
2714 const char *rpc_waitq = "none";
38e886e0
CL
2715
2716 if (RPC_IS_QUEUED(task))
2717 rpc_waitq = rpc_qname(task->tk_waitqueue);
2718
b3bcedad 2719 printk(KERN_INFO "%5u %04x %6d %8p %8p %8ld %8p %sv%u %s a:%ps q:%s\n",
cb3997b5
CL
2720 task->tk_pid, task->tk_flags, task->tk_status,
2721 clnt, task->tk_rqstp, task->tk_timeout, task->tk_ops,
55909f21 2722 clnt->cl_program->name, clnt->cl_vers, rpc_proc_name(task),
b3bcedad 2723 task->tk_action, rpc_waitq);
38e886e0
CL
2724}
2725
70abc49b 2726void rpc_show_tasks(struct net *net)
188fef11
TM
2727{
2728 struct rpc_clnt *clnt;
38e886e0 2729 struct rpc_task *task;
68a23ee9 2730 int header = 0;
70abc49b 2731 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
188fef11 2732
70abc49b
SK
2733 spin_lock(&sn->rpc_client_lock);
2734 list_for_each_entry(clnt, &sn->all_clients, cl_clients) {
188fef11 2735 spin_lock(&clnt->cl_lock);
38e886e0 2736 list_for_each_entry(task, &clnt->cl_tasks, tk_task) {
68a23ee9
CL
2737 if (!header) {
2738 rpc_show_header();
2739 header++;
2740 }
38e886e0 2741 rpc_show_task(clnt, task);
188fef11
TM
2742 }
2743 spin_unlock(&clnt->cl_lock);
2744 }
70abc49b 2745 spin_unlock(&sn->rpc_client_lock);
188fef11
TM
2746}
2747#endif
3c87ef6e
JL
2748
2749#if IS_ENABLED(CONFIG_SUNRPC_SWAP)
15001e5a
TM
2750static int
2751rpc_clnt_swap_activate_callback(struct rpc_clnt *clnt,
2752 struct rpc_xprt *xprt,
2753 void *dummy)
2754{
2755 return xprt_enable_swap(xprt);
2756}
2757
3c87ef6e
JL
2758int
2759rpc_clnt_swap_activate(struct rpc_clnt *clnt)
2760{
15001e5a
TM
2761 if (atomic_inc_return(&clnt->cl_swapper) == 1)
2762 return rpc_clnt_iterate_for_each_xprt(clnt,
2763 rpc_clnt_swap_activate_callback, NULL);
2764 return 0;
3c87ef6e
JL
2765}
2766EXPORT_SYMBOL_GPL(rpc_clnt_swap_activate);
2767
15001e5a
TM
2768static int
2769rpc_clnt_swap_deactivate_callback(struct rpc_clnt *clnt,
2770 struct rpc_xprt *xprt,
2771 void *dummy)
2772{
2773 xprt_disable_swap(xprt);
2774 return 0;
2775}
2776
3c87ef6e
JL
2777void
2778rpc_clnt_swap_deactivate(struct rpc_clnt *clnt)
2779{
15001e5a
TM
2780 if (atomic_dec_if_positive(&clnt->cl_swapper) == 0)
2781 rpc_clnt_iterate_for_each_xprt(clnt,
2782 rpc_clnt_swap_deactivate_callback, NULL);
3c87ef6e
JL
2783}
2784EXPORT_SYMBOL_GPL(rpc_clnt_swap_deactivate);
2785#endif /* CONFIG_SUNRPC_SWAP */