]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - net/sunrpc/clnt.c
Merge remote-tracking branch 'mkp-scsi/fixes' into fixes
[mirror_ubuntu-artful-kernel.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:
2c2ee6d2
N
1929 xprt_conditional_disconnect(task->tk_rqstp->rq_xprt,
1930 task->tk_rqstp->rq_connect_cookie);
3ed5e2a2
TM
1931 if (RPC_IS_SOFTCONN(task))
1932 break;
1fa3e2eb
SD
1933 /* retry with existing socket, after a delay */
1934 rpc_delay(task, 3*HZ);
3ed5e2a2 1935 case -EAGAIN:
485f2251
TM
1936 /* Check for timeouts before looping back to call_bind */
1937 case -ETIMEDOUT:
1938 task->tk_action = call_timeout;
561ec160
TM
1939 return;
1940 case 0:
3ed5e2a2
TM
1941 clnt->cl_stats->netreconn++;
1942 task->tk_action = call_transmit;
1943 return;
1da177e4 1944 }
3ed5e2a2 1945 rpc_exit(task, status);
1da177e4
LT
1946}
1947
1948/*
1949 * 5. Transmit the RPC request, and wait for reply
1950 */
1951static void
1952call_transmit(struct rpc_task *task)
1953{
ca7f33aa
TM
1954 int is_retrans = RPC_WAS_SENT(task);
1955
46121cf7 1956 dprint_status(task);
1da177e4
LT
1957
1958 task->tk_action = call_status;
1959 if (task->tk_status < 0)
1960 return;
90051ea7 1961 if (!xprt_prepare_transmit(task))
1da177e4 1962 return;
e0ab53de 1963 task->tk_action = call_transmit_status;
1da177e4 1964 /* Encode here so that rpcsec_gss can use correct sequence number. */
940e3318 1965 if (rpc_task_need_encode(task)) {
b0e1c57e 1966 rpc_xdr_encode(task);
5e5ce5be 1967 /* Did the encode result in an error condition? */
8b39f2b4
TM
1968 if (task->tk_status != 0) {
1969 /* Was the error nonfatal? */
1970 if (task->tk_status == -EAGAIN)
1971 rpc_delay(task, HZ >> 4);
1972 else
1973 rpc_exit(task, task->tk_status);
e0ab53de 1974 return;
8b39f2b4 1975 }
5e5ce5be 1976 }
1da177e4
LT
1977 xprt_transmit(task);
1978 if (task->tk_status < 0)
1979 return;
ca7f33aa
TM
1980 if (is_retrans)
1981 task->tk_client->cl_stats->rpcretrans++;
e0ab53de
TM
1982 /*
1983 * On success, ensure that we call xprt_end_transmit() before sleeping
1984 * in order to allow access to the socket to other RPC requests.
1985 */
1986 call_transmit_status(task);
55ae1aab 1987 if (rpc_reply_expected(task))
e0ab53de
TM
1988 return;
1989 task->tk_action = rpc_exit_task;
a4f0835c 1990 rpc_wake_up_queued_task(&task->tk_rqstp->rq_xprt->pending, task);
e0ab53de
TM
1991}
1992
1993/*
1994 * 5a. Handle cleanup after a transmission
1995 */
1996static void
1997call_transmit_status(struct rpc_task *task)
1998{
1999 task->tk_action = call_status;
206a134b
CL
2000
2001 /*
2002 * Common case: success. Force the compiler to put this
2003 * test first.
2004 */
2005 if (task->tk_status == 0) {
2006 xprt_end_transmit(task);
2007 rpc_task_force_reencode(task);
2008 return;
2009 }
2010
15f081ca
TM
2011 switch (task->tk_status) {
2012 case -EAGAIN:
93aa6c7b 2013 case -ENOBUFS:
15f081ca
TM
2014 break;
2015 default:
206a134b 2016 dprint_status(task);
15f081ca 2017 xprt_end_transmit(task);
09a21c41
CL
2018 rpc_task_force_reencode(task);
2019 break;
15f081ca
TM
2020 /*
2021 * Special cases: if we've been waiting on the
2022 * socket's write_space() callback, or if the
2023 * socket just returned a connection error,
2024 * then hold onto the transport lock.
2025 */
2026 case -ECONNREFUSED:
15f081ca
TM
2027 case -EHOSTDOWN:
2028 case -EHOSTUNREACH:
2029 case -ENETUNREACH:
3dedbb5c 2030 case -EPERM:
09a21c41
CL
2031 if (RPC_IS_SOFTCONN(task)) {
2032 xprt_end_transmit(task);
2033 rpc_exit(task, task->tk_status);
2034 break;
2035 }
2036 case -ECONNRESET:
df277270 2037 case -ECONNABORTED:
3913c78c 2038 case -EADDRINUSE:
09a21c41 2039 case -ENOTCONN:
c8485e4d 2040 case -EPIPE:
15f081ca
TM
2041 rpc_task_force_reencode(task);
2042 }
1da177e4
LT
2043}
2044
9e00abc3 2045#if defined(CONFIG_SUNRPC_BACKCHANNEL)
55ae1aab
RL
2046/*
2047 * 5b. Send the backchannel RPC reply. On error, drop the reply. In
2048 * addition, disconnect on connectivity errors.
2049 */
2050static void
2051call_bc_transmit(struct rpc_task *task)
2052{
2053 struct rpc_rqst *req = task->tk_rqstp;
2054
1193d58f
TM
2055 if (!xprt_prepare_transmit(task))
2056 goto out_retry;
55ae1aab 2057
55ae1aab
RL
2058 if (task->tk_status < 0) {
2059 printk(KERN_NOTICE "RPC: Could not send backchannel reply "
2060 "error: %d\n", task->tk_status);
1193d58f 2061 goto out_done;
55ae1aab 2062 }
1193d58f
TM
2063 if (req->rq_connect_cookie != req->rq_xprt->connect_cookie)
2064 req->rq_bytes_sent = 0;
55ae1aab
RL
2065
2066 xprt_transmit(task);
1193d58f
TM
2067
2068 if (task->tk_status == -EAGAIN)
2069 goto out_nospace;
2070
55ae1aab
RL
2071 xprt_end_transmit(task);
2072 dprint_status(task);
2073 switch (task->tk_status) {
2074 case 0:
2075 /* Success */
55ae1aab
RL
2076 case -EHOSTDOWN:
2077 case -EHOSTUNREACH:
2078 case -ENETUNREACH:
3832591e
TM
2079 case -ECONNRESET:
2080 case -ECONNREFUSED:
2081 case -EADDRINUSE:
2082 case -ENOTCONN:
2083 case -EPIPE:
2084 break;
55ae1aab
RL
2085 case -ETIMEDOUT:
2086 /*
2087 * Problem reaching the server. Disconnect and let the
2088 * forechannel reestablish the connection. The server will
2089 * have to retransmit the backchannel request and we'll
2090 * reprocess it. Since these ops are idempotent, there's no
2091 * need to cache our reply at this time.
2092 */
2093 printk(KERN_NOTICE "RPC: Could not send backchannel reply "
2094 "error: %d\n", task->tk_status);
a4f0835c 2095 xprt_conditional_disconnect(req->rq_xprt,
55ae1aab
RL
2096 req->rq_connect_cookie);
2097 break;
2098 default:
2099 /*
2100 * We were unable to reply and will have to drop the
2101 * request. The server should reconnect and retransmit.
2102 */
1facf4c4 2103 WARN_ON_ONCE(task->tk_status == -EAGAIN);
55ae1aab
RL
2104 printk(KERN_NOTICE "RPC: Could not send backchannel reply "
2105 "error: %d\n", task->tk_status);
2106 break;
2107 }
2108 rpc_wake_up_queued_task(&req->rq_xprt->pending, task);
1193d58f
TM
2109out_done:
2110 task->tk_action = rpc_exit_task;
2111 return;
2112out_nospace:
2113 req->rq_connect_cookie = req->rq_xprt->connect_cookie;
2114out_retry:
2115 task->tk_status = 0;
55ae1aab 2116}
9e00abc3 2117#endif /* CONFIG_SUNRPC_BACKCHANNEL */
55ae1aab 2118
1da177e4
LT
2119/*
2120 * 6. Sort out the RPC call status
2121 */
2122static void
2123call_status(struct rpc_task *task)
2124{
2125 struct rpc_clnt *clnt = task->tk_client;
2126 struct rpc_rqst *req = task->tk_rqstp;
2127 int status;
2128
dd2b63d0
RL
2129 if (req->rq_reply_bytes_recvd > 0 && !req->rq_bytes_sent)
2130 task->tk_status = req->rq_reply_bytes_recvd;
1da177e4 2131
46121cf7 2132 dprint_status(task);
1da177e4
LT
2133
2134 status = task->tk_status;
2135 if (status >= 0) {
2136 task->tk_action = call_decode;
2137 return;
2138 }
2139
5753cba1 2140 trace_rpc_call_status(task);
1da177e4
LT
2141 task->tk_status = 0;
2142 switch(status) {
76303992
TM
2143 case -EHOSTDOWN:
2144 case -EHOSTUNREACH:
2145 case -ENETUNREACH:
3dedbb5c 2146 case -EPERM:
9455e3f4
TM
2147 if (RPC_IS_SOFTCONN(task)) {
2148 rpc_exit(task, status);
2149 break;
2150 }
76303992
TM
2151 /*
2152 * Delay any retries for 3 seconds, then handle as if it
2153 * were a timeout.
2154 */
2155 rpc_delay(task, 3*HZ);
1da177e4
LT
2156 case -ETIMEDOUT:
2157 task->tk_action = call_timeout;
8a19a0b6
TM
2158 if (!(task->tk_flags & RPC_TASK_NO_RETRANS_TIMEOUT)
2159 && task->tk_client->cl_discrtry)
a4f0835c 2160 xprt_conditional_disconnect(req->rq_xprt,
7c1d71cf 2161 req->rq_connect_cookie);
1da177e4
LT
2162 break;
2163 case -ECONNREFUSED:
df277270
TM
2164 case -ECONNRESET:
2165 case -ECONNABORTED:
35f5a422 2166 rpc_force_rebind(clnt);
3913c78c 2167 case -EADDRINUSE:
c8485e4d
TM
2168 rpc_delay(task, 3*HZ);
2169 case -EPIPE:
2170 case -ENOTCONN:
1da177e4
LT
2171 task->tk_action = call_bind;
2172 break;
93aa6c7b
TM
2173 case -ENOBUFS:
2174 rpc_delay(task, HZ>>2);
1da177e4
LT
2175 case -EAGAIN:
2176 task->tk_action = call_transmit;
2177 break;
2178 case -EIO:
2179 /* shutdown or soft timeout */
2180 rpc_exit(task, status);
2181 break;
2182 default:
b6b6152c
OK
2183 if (clnt->cl_chatty)
2184 printk("%s: RPC call returned error %d\n",
55909f21 2185 clnt->cl_program->name, -status);
1da177e4 2186 rpc_exit(task, status);
1da177e4
LT
2187 }
2188}
2189
2190/*
e0ab53de 2191 * 6a. Handle RPC timeout
1da177e4
LT
2192 * We do not release the request slot, so we keep using the
2193 * same XID for all retransmits.
2194 */
2195static void
2196call_timeout(struct rpc_task *task)
2197{
2198 struct rpc_clnt *clnt = task->tk_client;
2199
2200 if (xprt_adjust_timeout(task->tk_rqstp) == 0) {
46121cf7 2201 dprintk("RPC: %5u call_timeout (minor)\n", task->tk_pid);
1da177e4
LT
2202 goto retry;
2203 }
2204
46121cf7 2205 dprintk("RPC: %5u call_timeout (major)\n", task->tk_pid);
ef759a2e
CL
2206 task->tk_timeouts++;
2207
3a28becc
CL
2208 if (RPC_IS_SOFTCONN(task)) {
2209 rpc_exit(task, -ETIMEDOUT);
2210 return;
2211 }
1da177e4 2212 if (RPC_IS_SOFT(task)) {
cac5d07e 2213 if (clnt->cl_chatty) {
b6b6152c 2214 printk(KERN_NOTICE "%s: server %s not responding, timed out\n",
55909f21 2215 clnt->cl_program->name,
fb43d172 2216 task->tk_xprt->servername);
cac5d07e 2217 }
7494d00c
TM
2218 if (task->tk_flags & RPC_TASK_TIMEOUT)
2219 rpc_exit(task, -ETIMEDOUT);
2220 else
2221 rpc_exit(task, -EIO);
1da177e4
LT
2222 return;
2223 }
2224
f518e35a 2225 if (!(task->tk_flags & RPC_CALL_MAJORSEEN)) {
1da177e4 2226 task->tk_flags |= RPC_CALL_MAJORSEEN;
4e0038b6 2227 if (clnt->cl_chatty) {
b6b6152c 2228 printk(KERN_NOTICE "%s: server %s not responding, still trying\n",
55909f21 2229 clnt->cl_program->name,
fb43d172 2230 task->tk_xprt->servername);
4e0038b6 2231 }
1da177e4 2232 }
35f5a422 2233 rpc_force_rebind(clnt);
b48633bd
TM
2234 /*
2235 * Did our request time out due to an RPCSEC_GSS out-of-sequence
2236 * event? RFC2203 requires the server to drop all such requests.
2237 */
2238 rpcauth_invalcred(task);
1da177e4
LT
2239
2240retry:
1da177e4
LT
2241 task->tk_action = call_bind;
2242 task->tk_status = 0;
2243}
2244
2245/*
2246 * 7. Decode the RPC reply
2247 */
2248static void
2249call_decode(struct rpc_task *task)
2250{
2251 struct rpc_clnt *clnt = task->tk_client;
2252 struct rpc_rqst *req = task->tk_rqstp;
bf269551 2253 kxdrdproc_t decode = task->tk_msg.rpc_proc->p_decode;
d8ed029d 2254 __be32 *p;
1da177e4 2255
726fd6ad 2256 dprint_status(task);
1da177e4 2257
f518e35a 2258 if (task->tk_flags & RPC_CALL_MAJORSEEN) {
4e0038b6 2259 if (clnt->cl_chatty) {
b6b6152c 2260 printk(KERN_NOTICE "%s: server %s OK\n",
55909f21 2261 clnt->cl_program->name,
fb43d172 2262 task->tk_xprt->servername);
4e0038b6 2263 }
1da177e4
LT
2264 task->tk_flags &= ~RPC_CALL_MAJORSEEN;
2265 }
2266
43ac3f29
TM
2267 /*
2268 * Ensure that we see all writes made by xprt_complete_rqst()
dd2b63d0 2269 * before it changed req->rq_reply_bytes_recvd.
43ac3f29
TM
2270 */
2271 smp_rmb();
1da177e4
LT
2272 req->rq_rcv_buf.len = req->rq_private_buf.len;
2273
2274 /* Check that the softirq receive buffer is valid */
2275 WARN_ON(memcmp(&req->rq_rcv_buf, &req->rq_private_buf,
2276 sizeof(req->rq_rcv_buf)) != 0);
2277
1e799b67
TM
2278 if (req->rq_rcv_buf.len < 12) {
2279 if (!RPC_IS_SOFT(task)) {
2280 task->tk_action = call_bind;
1e799b67
TM
2281 goto out_retry;
2282 }
2283 dprintk("RPC: %s: too small RPC reply size (%d bytes)\n",
55909f21 2284 clnt->cl_program->name, task->tk_status);
1e799b67
TM
2285 task->tk_action = call_timeout;
2286 goto out_retry;
2287 }
2288
b0e1c57e 2289 p = rpc_verify_header(task);
abbcf28f
TM
2290 if (IS_ERR(p)) {
2291 if (p == ERR_PTR(-EAGAIN))
2292 goto out_retry;
2293 return;
1da177e4
LT
2294 }
2295
abbcf28f 2296 task->tk_action = rpc_exit_task;
1da177e4 2297
6d5fcb5a 2298 if (decode) {
1da177e4
LT
2299 task->tk_status = rpcauth_unwrap_resp(task, decode, req, p,
2300 task->tk_msg.rpc_resp);
6d5fcb5a 2301 }
46121cf7
CL
2302 dprintk("RPC: %5u call_decode result %d\n", task->tk_pid,
2303 task->tk_status);
1da177e4
LT
2304 return;
2305out_retry:
1da177e4 2306 task->tk_status = 0;
b0e1c57e 2307 /* Note: rpc_verify_header() may have freed the RPC slot */
24b74bf0 2308 if (task->tk_rqstp == req) {
dd2b63d0 2309 req->rq_reply_bytes_recvd = req->rq_rcv_buf.len = 0;
24b74bf0 2310 if (task->tk_client->cl_discrtry)
a4f0835c 2311 xprt_conditional_disconnect(req->rq_xprt,
7c1d71cf 2312 req->rq_connect_cookie);
24b74bf0 2313 }
1da177e4
LT
2314}
2315
d8ed029d 2316static __be32 *
b0e1c57e 2317rpc_encode_header(struct rpc_task *task)
1da177e4
LT
2318{
2319 struct rpc_clnt *clnt = task->tk_client;
1da177e4 2320 struct rpc_rqst *req = task->tk_rqstp;
d8ed029d 2321 __be32 *p = req->rq_svec[0].iov_base;
1da177e4
LT
2322
2323 /* FIXME: check buffer size? */
808012fb 2324
a4f0835c 2325 p = xprt_skip_transport_header(req->rq_xprt, p);
1da177e4
LT
2326 *p++ = req->rq_xid; /* XID */
2327 *p++ = htonl(RPC_CALL); /* CALL */
2328 *p++ = htonl(RPC_VERSION); /* RPC version */
2329 *p++ = htonl(clnt->cl_prog); /* program number */
2330 *p++ = htonl(clnt->cl_vers); /* program version */
2331 *p++ = htonl(task->tk_msg.rpc_proc->p_proc); /* procedure */
334ccfd5
TM
2332 p = rpcauth_marshcred(task, p);
2333 req->rq_slen = xdr_adjust_iovec(&req->rq_svec[0], p);
2334 return p;
1da177e4
LT
2335}
2336
d8ed029d 2337static __be32 *
b0e1c57e 2338rpc_verify_header(struct rpc_task *task)
1da177e4 2339{
4e0038b6 2340 struct rpc_clnt *clnt = task->tk_client;
1da177e4
LT
2341 struct kvec *iov = &task->tk_rqstp->rq_rcv_buf.head[0];
2342 int len = task->tk_rqstp->rq_rcv_buf.len >> 2;
d8ed029d
AD
2343 __be32 *p = iov->iov_base;
2344 u32 n;
1da177e4
LT
2345 int error = -EACCES;
2346
e8896495
DH
2347 if ((task->tk_rqstp->rq_rcv_buf.len & 3) != 0) {
2348 /* RFC-1014 says that the representation of XDR data must be a
2349 * multiple of four bytes
2350 * - if it isn't pointer subtraction in the NFS client may give
2351 * undefined results
2352 */
8a702bbb 2353 dprintk("RPC: %5u %s: XDR representation not a multiple of"
0dc47877 2354 " 4 bytes: 0x%x\n", task->tk_pid, __func__,
8a702bbb 2355 task->tk_rqstp->rq_rcv_buf.len);
35fa5f7b
AA
2356 error = -EIO;
2357 goto out_err;
e8896495 2358 }
1da177e4
LT
2359 if ((len -= 3) < 0)
2360 goto out_overflow;
1da177e4 2361
f4a2e418 2362 p += 1; /* skip XID */
1da177e4 2363 if ((n = ntohl(*p++)) != RPC_REPLY) {
8a702bbb 2364 dprintk("RPC: %5u %s: not an RPC reply: %x\n",
f4a2e418 2365 task->tk_pid, __func__, n);
35fa5f7b 2366 error = -EIO;
abbcf28f 2367 goto out_garbage;
1da177e4 2368 }
f4a2e418 2369
1da177e4
LT
2370 if ((n = ntohl(*p++)) != RPC_MSG_ACCEPTED) {
2371 if (--len < 0)
2372 goto out_overflow;
2373 switch ((n = ntohl(*p++))) {
89f0e4fe
JP
2374 case RPC_AUTH_ERROR:
2375 break;
2376 case RPC_MISMATCH:
2377 dprintk("RPC: %5u %s: RPC call version mismatch!\n",
2378 task->tk_pid, __func__);
2379 error = -EPROTONOSUPPORT;
2380 goto out_err;
2381 default:
2382 dprintk("RPC: %5u %s: RPC call rejected, "
2383 "unknown error: %x\n",
2384 task->tk_pid, __func__, n);
35fa5f7b
AA
2385 error = -EIO;
2386 goto out_err;
1da177e4
LT
2387 }
2388 if (--len < 0)
2389 goto out_overflow;
2390 switch ((n = ntohl(*p++))) {
2391 case RPC_AUTH_REJECTEDCRED:
2392 case RPC_AUTH_REJECTEDVERF:
2393 case RPCSEC_GSS_CREDPROBLEM:
2394 case RPCSEC_GSS_CTXPROBLEM:
2395 if (!task->tk_cred_retry)
2396 break;
2397 task->tk_cred_retry--;
46121cf7 2398 dprintk("RPC: %5u %s: retry stale creds\n",
0dc47877 2399 task->tk_pid, __func__);
1da177e4 2400 rpcauth_invalcred(task);
220bcc2a
TM
2401 /* Ensure we obtain a new XID! */
2402 xprt_release(task);
118df3d1 2403 task->tk_action = call_reserve;
abbcf28f 2404 goto out_retry;
1da177e4
LT
2405 case RPC_AUTH_BADCRED:
2406 case RPC_AUTH_BADVERF:
2407 /* possibly garbled cred/verf? */
2408 if (!task->tk_garb_retry)
2409 break;
2410 task->tk_garb_retry--;
46121cf7 2411 dprintk("RPC: %5u %s: retry garbled creds\n",
0dc47877 2412 task->tk_pid, __func__);
1da177e4 2413 task->tk_action = call_bind;
abbcf28f 2414 goto out_retry;
1da177e4 2415 case RPC_AUTH_TOOWEAK:
b0e1c57e 2416 printk(KERN_NOTICE "RPC: server %s requires stronger "
4e0038b6 2417 "authentication.\n",
fb43d172 2418 task->tk_xprt->servername);
1da177e4
LT
2419 break;
2420 default:
8a702bbb 2421 dprintk("RPC: %5u %s: unknown auth error: %x\n",
0dc47877 2422 task->tk_pid, __func__, n);
1da177e4
LT
2423 error = -EIO;
2424 }
46121cf7 2425 dprintk("RPC: %5u %s: call rejected %d\n",
0dc47877 2426 task->tk_pid, __func__, n);
1da177e4
LT
2427 goto out_err;
2428 }
35fa5f7b
AA
2429 p = rpcauth_checkverf(task, p);
2430 if (IS_ERR(p)) {
2431 error = PTR_ERR(p);
2432 dprintk("RPC: %5u %s: auth check failed with %d\n",
2433 task->tk_pid, __func__, error);
abbcf28f 2434 goto out_garbage; /* bad verifier, retry */
1da177e4 2435 }
d8ed029d 2436 len = p - (__be32 *)iov->iov_base - 1;
1da177e4
LT
2437 if (len < 0)
2438 goto out_overflow;
2439 switch ((n = ntohl(*p++))) {
2440 case RPC_SUCCESS:
2441 return p;
2442 case RPC_PROG_UNAVAIL:
fb43d172 2443 dprintk("RPC: %5u %s: program %u is unsupported "
4e0038b6
TM
2444 "by server %s\n", task->tk_pid, __func__,
2445 (unsigned int)clnt->cl_prog,
fb43d172 2446 task->tk_xprt->servername);
cdf47706
AG
2447 error = -EPFNOSUPPORT;
2448 goto out_err;
1da177e4 2449 case RPC_PROG_MISMATCH:
fb43d172 2450 dprintk("RPC: %5u %s: program %u, version %u unsupported "
4e0038b6
TM
2451 "by server %s\n", task->tk_pid, __func__,
2452 (unsigned int)clnt->cl_prog,
2453 (unsigned int)clnt->cl_vers,
fb43d172 2454 task->tk_xprt->servername);
cdf47706
AG
2455 error = -EPROTONOSUPPORT;
2456 goto out_err;
1da177e4 2457 case RPC_PROC_UNAVAIL:
fb43d172 2458 dprintk("RPC: %5u %s: proc %s unsupported by program %u, "
46121cf7 2459 "version %u on server %s\n",
0dc47877 2460 task->tk_pid, __func__,
3748f1e4 2461 rpc_proc_name(task),
4e0038b6 2462 clnt->cl_prog, clnt->cl_vers,
fb43d172 2463 task->tk_xprt->servername);
cdf47706
AG
2464 error = -EOPNOTSUPP;
2465 goto out_err;
1da177e4 2466 case RPC_GARBAGE_ARGS:
46121cf7 2467 dprintk("RPC: %5u %s: server saw garbage\n",
0dc47877 2468 task->tk_pid, __func__);
1da177e4
LT
2469 break; /* retry */
2470 default:
8a702bbb 2471 dprintk("RPC: %5u %s: server accept status: %x\n",
0dc47877 2472 task->tk_pid, __func__, n);
1da177e4
LT
2473 /* Also retry */
2474 }
2475
abbcf28f 2476out_garbage:
4e0038b6 2477 clnt->cl_stats->rpcgarbage++;
1da177e4
LT
2478 if (task->tk_garb_retry) {
2479 task->tk_garb_retry--;
46121cf7 2480 dprintk("RPC: %5u %s: retrying\n",
0dc47877 2481 task->tk_pid, __func__);
1da177e4 2482 task->tk_action = call_bind;
abbcf28f
TM
2483out_retry:
2484 return ERR_PTR(-EAGAIN);
1da177e4 2485 }
1da177e4
LT
2486out_err:
2487 rpc_exit(task, error);
8a702bbb 2488 dprintk("RPC: %5u %s: call failed with error %d\n", task->tk_pid,
0dc47877 2489 __func__, error);
abbcf28f 2490 return ERR_PTR(error);
1da177e4 2491out_overflow:
8a702bbb 2492 dprintk("RPC: %5u %s: server reply was truncated.\n", task->tk_pid,
0dc47877 2493 __func__);
abbcf28f 2494 goto out_garbage;
1da177e4 2495}
5ee0ed7d 2496
9f06c719 2497static void rpcproc_encode_null(void *rqstp, struct xdr_stream *xdr, void *obj)
5ee0ed7d 2498{
5ee0ed7d
TM
2499}
2500
bf269551 2501static int rpcproc_decode_null(void *rqstp, struct xdr_stream *xdr, void *obj)
5ee0ed7d
TM
2502{
2503 return 0;
2504}
2505
2506static struct rpc_procinfo rpcproc_null = {
2507 .p_encode = rpcproc_encode_null,
2508 .p_decode = rpcproc_decode_null,
2509};
2510
caabea8a 2511static int rpc_ping(struct rpc_clnt *clnt)
5ee0ed7d
TM
2512{
2513 struct rpc_message msg = {
2514 .rpc_proc = &rpcproc_null,
2515 };
2516 int err;
2517 msg.rpc_cred = authnull_ops.lookup_cred(NULL, NULL, 0);
caabea8a 2518 err = rpc_call_sync(clnt, &msg, RPC_TASK_SOFT | RPC_TASK_SOFTCONN);
5ee0ed7d
TM
2519 put_rpccred(msg.rpc_cred);
2520 return err;
2521}
188fef11 2522
7f554890
TM
2523static
2524struct rpc_task *rpc_call_null_helper(struct rpc_clnt *clnt,
2525 struct rpc_xprt *xprt, struct rpc_cred *cred, int flags,
2526 const struct rpc_call_ops *ops, void *data)
5e1550d6
TM
2527{
2528 struct rpc_message msg = {
2529 .rpc_proc = &rpcproc_null,
2530 .rpc_cred = cred,
2531 };
84115e1c
TM
2532 struct rpc_task_setup task_setup_data = {
2533 .rpc_client = clnt,
7f554890 2534 .rpc_xprt = xprt,
84115e1c 2535 .rpc_message = &msg,
7f554890
TM
2536 .callback_ops = (ops != NULL) ? ops : &rpc_default_ops,
2537 .callback_data = data,
84115e1c
TM
2538 .flags = flags,
2539 };
7f554890 2540
c970aa85 2541 return rpc_run_task(&task_setup_data);
5e1550d6 2542}
7f554890
TM
2543
2544struct rpc_task *rpc_call_null(struct rpc_clnt *clnt, struct rpc_cred *cred, int flags)
2545{
2546 return rpc_call_null_helper(clnt, NULL, cred, flags, NULL, NULL);
2547}
e8914c65 2548EXPORT_SYMBOL_GPL(rpc_call_null);
5e1550d6 2549
7f554890
TM
2550struct rpc_cb_add_xprt_calldata {
2551 struct rpc_xprt_switch *xps;
2552 struct rpc_xprt *xprt;
2553};
2554
2555static void rpc_cb_add_xprt_done(struct rpc_task *task, void *calldata)
2556{
2557 struct rpc_cb_add_xprt_calldata *data = calldata;
2558
2559 if (task->tk_status == 0)
2560 rpc_xprt_switch_add_xprt(data->xps, data->xprt);
2561}
2562
2563static void rpc_cb_add_xprt_release(void *calldata)
2564{
2565 struct rpc_cb_add_xprt_calldata *data = calldata;
2566
2567 xprt_put(data->xprt);
2568 xprt_switch_put(data->xps);
2569 kfree(data);
2570}
2571
ce272302 2572static const struct rpc_call_ops rpc_cb_add_xprt_call_ops = {
7f554890
TM
2573 .rpc_call_done = rpc_cb_add_xprt_done,
2574 .rpc_release = rpc_cb_add_xprt_release,
2575};
2576
2577/**
2578 * rpc_clnt_test_and_add_xprt - Test and add a new transport to a rpc_clnt
2579 * @clnt: pointer to struct rpc_clnt
2580 * @xps: pointer to struct rpc_xprt_switch,
2581 * @xprt: pointer struct rpc_xprt
2582 * @dummy: unused
2583 */
2584int rpc_clnt_test_and_add_xprt(struct rpc_clnt *clnt,
2585 struct rpc_xprt_switch *xps, struct rpc_xprt *xprt,
2586 void *dummy)
2587{
2588 struct rpc_cb_add_xprt_calldata *data;
2589 struct rpc_cred *cred;
2590 struct rpc_task *task;
2591
2592 data = kmalloc(sizeof(*data), GFP_NOFS);
2593 if (!data)
2594 return -ENOMEM;
2595 data->xps = xprt_switch_get(xps);
2596 data->xprt = xprt_get(xprt);
2597
2598 cred = authnull_ops.lookup_cred(NULL, NULL, 0);
2599 task = rpc_call_null_helper(clnt, xprt, cred,
2600 RPC_TASK_SOFT|RPC_TASK_SOFTCONN|RPC_TASK_ASYNC,
2601 &rpc_cb_add_xprt_call_ops, data);
2602 put_rpccred(cred);
2603 if (IS_ERR(task))
2604 return PTR_ERR(task);
2605 rpc_put_task(task);
2606 return 1;
2607}
2608EXPORT_SYMBOL_GPL(rpc_clnt_test_and_add_xprt);
2609
fda0ab41
AA
2610/**
2611 * rpc_clnt_setup_test_and_add_xprt()
2612 *
2613 * This is an rpc_clnt_add_xprt setup() function which returns 1 so:
2614 * 1) caller of the test function must dereference the rpc_xprt_switch
2615 * and the rpc_xprt.
2616 * 2) test function must call rpc_xprt_switch_add_xprt, usually in
2617 * the rpc_call_done routine.
2618 *
2619 * Upon success (return of 1), the test function adds the new
2620 * transport to the rpc_clnt xprt switch
2621 *
2622 * @clnt: struct rpc_clnt to get the new transport
2623 * @xps: the rpc_xprt_switch to hold the new transport
2624 * @xprt: the rpc_xprt to test
2625 * @data: a struct rpc_add_xprt_test pointer that holds the test function
2626 * and test function call data
2627 */
2628int rpc_clnt_setup_test_and_add_xprt(struct rpc_clnt *clnt,
2629 struct rpc_xprt_switch *xps,
2630 struct rpc_xprt *xprt,
2631 void *data)
2632{
2633 struct rpc_cred *cred;
2634 struct rpc_task *task;
2635 struct rpc_add_xprt_test *xtest = (struct rpc_add_xprt_test *)data;
2636 int status = -EADDRINUSE;
2637
2638 xprt = xprt_get(xprt);
2639 xprt_switch_get(xps);
2640
2641 if (rpc_xprt_switch_has_addr(xps, (struct sockaddr *)&xprt->addr))
2642 goto out_err;
2643
2644 /* Test the connection */
2645 cred = authnull_ops.lookup_cred(NULL, NULL, 0);
2646 task = rpc_call_null_helper(clnt, xprt, cred,
2647 RPC_TASK_SOFT | RPC_TASK_SOFTCONN,
2648 NULL, NULL);
2649 put_rpccred(cred);
2650 if (IS_ERR(task)) {
2651 status = PTR_ERR(task);
2652 goto out_err;
2653 }
2654 status = task->tk_status;
2655 rpc_put_task(task);
2656
2657 if (status < 0)
2658 goto out_err;
2659
2660 /* rpc_xprt_switch and rpc_xprt are deferrenced by add_xprt_test() */
2661 xtest->add_xprt_test(clnt, xprt, xtest->data);
2662
2663 /* so that rpc_clnt_add_xprt does not call rpc_xprt_switch_add_xprt */
2664 return 1;
2665out_err:
2666 xprt_put(xprt);
2667 xprt_switch_put(xps);
2668 pr_info("RPC: rpc_clnt_test_xprt failed: %d addr %s not added\n",
2669 status, xprt->address_strings[RPC_DISPLAY_ADDR]);
2670 return status;
2671}
2672EXPORT_SYMBOL_GPL(rpc_clnt_setup_test_and_add_xprt);
2673
7f554890
TM
2674/**
2675 * rpc_clnt_add_xprt - Add a new transport to a rpc_clnt
2676 * @clnt: pointer to struct rpc_clnt
2677 * @xprtargs: pointer to struct xprt_create
2678 * @setup: callback to test and/or set up the connection
2679 * @data: pointer to setup function data
2680 *
2681 * Creates a new transport using the parameters set in args and
2682 * adds it to clnt.
2683 * If ping is set, then test that connectivity succeeds before
2684 * adding the new transport.
2685 *
2686 */
2687int rpc_clnt_add_xprt(struct rpc_clnt *clnt,
2688 struct xprt_create *xprtargs,
2689 int (*setup)(struct rpc_clnt *,
2690 struct rpc_xprt_switch *,
2691 struct rpc_xprt *,
2692 void *),
2693 void *data)
2694{
2695 struct rpc_xprt_switch *xps;
2696 struct rpc_xprt *xprt;
3851f1cd 2697 unsigned long reconnect_timeout;
7f554890
TM
2698 unsigned char resvport;
2699 int ret = 0;
2700
2701 rcu_read_lock();
2702 xps = xprt_switch_get(rcu_dereference(clnt->cl_xpi.xpi_xpswitch));
2703 xprt = xprt_iter_xprt(&clnt->cl_xpi);
2704 if (xps == NULL || xprt == NULL) {
2705 rcu_read_unlock();
2706 return -EAGAIN;
2707 }
2708 resvport = xprt->resvport;
3851f1cd 2709 reconnect_timeout = xprt->max_reconnect_timeout;
7f554890
TM
2710 rcu_read_unlock();
2711
2712 xprt = xprt_create_transport(xprtargs);
2713 if (IS_ERR(xprt)) {
2714 ret = PTR_ERR(xprt);
2715 goto out_put_switch;
2716 }
2717 xprt->resvport = resvport;
3851f1cd 2718 xprt->max_reconnect_timeout = reconnect_timeout;
7f554890
TM
2719
2720 rpc_xprt_switch_set_roundrobin(xps);
2721 if (setup) {
2722 ret = setup(clnt, xps, xprt, data);
2723 if (ret != 0)
2724 goto out_put_xprt;
2725 }
2726 rpc_xprt_switch_add_xprt(xps, xprt);
2727out_put_xprt:
2728 xprt_put(xprt);
2729out_put_switch:
2730 xprt_switch_put(xps);
2731 return ret;
2732}
2733EXPORT_SYMBOL_GPL(rpc_clnt_add_xprt);
2734
8d480326
TM
2735static int
2736rpc_xprt_cap_max_reconnect_timeout(struct rpc_clnt *clnt,
2737 struct rpc_xprt *xprt,
2738 void *data)
2739{
2740 unsigned long timeout = *((unsigned long *)data);
2741
2742 if (timeout < xprt->max_reconnect_timeout)
2743 xprt->max_reconnect_timeout = timeout;
2744 return 0;
2745}
2746
2747void
2748rpc_cap_max_reconnect_timeout(struct rpc_clnt *clnt, unsigned long timeo)
2749{
2750 rpc_clnt_iterate_for_each_xprt(clnt,
2751 rpc_xprt_cap_max_reconnect_timeout,
2752 &timeo);
2753}
2754EXPORT_SYMBOL_GPL(rpc_cap_max_reconnect_timeout);
2755
3b58a8a9
AA
2756void rpc_clnt_xprt_switch_put(struct rpc_clnt *clnt)
2757{
bb29dd84 2758 rcu_read_lock();
3b58a8a9 2759 xprt_switch_put(rcu_dereference(clnt->cl_xpi.xpi_xpswitch));
bb29dd84 2760 rcu_read_unlock();
3b58a8a9
AA
2761}
2762EXPORT_SYMBOL_GPL(rpc_clnt_xprt_switch_put);
2763
dd691717
AA
2764void rpc_clnt_xprt_switch_add_xprt(struct rpc_clnt *clnt, struct rpc_xprt *xprt)
2765{
bb29dd84 2766 rcu_read_lock();
dd691717
AA
2767 rpc_xprt_switch_add_xprt(rcu_dereference(clnt->cl_xpi.xpi_xpswitch),
2768 xprt);
bb29dd84 2769 rcu_read_unlock();
dd691717
AA
2770}
2771EXPORT_SYMBOL_GPL(rpc_clnt_xprt_switch_add_xprt);
2772
39e5d2df
AA
2773bool rpc_clnt_xprt_switch_has_addr(struct rpc_clnt *clnt,
2774 const struct sockaddr *sap)
2775{
2776 struct rpc_xprt_switch *xps;
2777 bool ret;
2778
39e5d2df 2779 rcu_read_lock();
bb29dd84 2780 xps = rcu_dereference(clnt->cl_xpi.xpi_xpswitch);
39e5d2df
AA
2781 ret = rpc_xprt_switch_has_addr(xps, sap);
2782 rcu_read_unlock();
2783 return ret;
2784}
2785EXPORT_SYMBOL_GPL(rpc_clnt_xprt_switch_has_addr);
2786
f895b252 2787#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
68a23ee9
CL
2788static void rpc_show_header(void)
2789{
cb3997b5
CL
2790 printk(KERN_INFO "-pid- flgs status -client- --rqstp- "
2791 "-timeout ---ops--\n");
68a23ee9
CL
2792}
2793
38e886e0
CL
2794static void rpc_show_task(const struct rpc_clnt *clnt,
2795 const struct rpc_task *task)
2796{
2797 const char *rpc_waitq = "none";
38e886e0
CL
2798
2799 if (RPC_IS_QUEUED(task))
2800 rpc_waitq = rpc_qname(task->tk_waitqueue);
2801
b3bcedad 2802 printk(KERN_INFO "%5u %04x %6d %8p %8p %8ld %8p %sv%u %s a:%ps q:%s\n",
cb3997b5
CL
2803 task->tk_pid, task->tk_flags, task->tk_status,
2804 clnt, task->tk_rqstp, task->tk_timeout, task->tk_ops,
55909f21 2805 clnt->cl_program->name, clnt->cl_vers, rpc_proc_name(task),
b3bcedad 2806 task->tk_action, rpc_waitq);
38e886e0
CL
2807}
2808
70abc49b 2809void rpc_show_tasks(struct net *net)
188fef11
TM
2810{
2811 struct rpc_clnt *clnt;
38e886e0 2812 struct rpc_task *task;
68a23ee9 2813 int header = 0;
70abc49b 2814 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
188fef11 2815
70abc49b
SK
2816 spin_lock(&sn->rpc_client_lock);
2817 list_for_each_entry(clnt, &sn->all_clients, cl_clients) {
188fef11 2818 spin_lock(&clnt->cl_lock);
38e886e0 2819 list_for_each_entry(task, &clnt->cl_tasks, tk_task) {
68a23ee9
CL
2820 if (!header) {
2821 rpc_show_header();
2822 header++;
2823 }
38e886e0 2824 rpc_show_task(clnt, task);
188fef11
TM
2825 }
2826 spin_unlock(&clnt->cl_lock);
2827 }
70abc49b 2828 spin_unlock(&sn->rpc_client_lock);
188fef11
TM
2829}
2830#endif
3c87ef6e
JL
2831
2832#if IS_ENABLED(CONFIG_SUNRPC_SWAP)
15001e5a
TM
2833static int
2834rpc_clnt_swap_activate_callback(struct rpc_clnt *clnt,
2835 struct rpc_xprt *xprt,
2836 void *dummy)
2837{
2838 return xprt_enable_swap(xprt);
2839}
2840
3c87ef6e
JL
2841int
2842rpc_clnt_swap_activate(struct rpc_clnt *clnt)
2843{
15001e5a
TM
2844 if (atomic_inc_return(&clnt->cl_swapper) == 1)
2845 return rpc_clnt_iterate_for_each_xprt(clnt,
2846 rpc_clnt_swap_activate_callback, NULL);
2847 return 0;
3c87ef6e
JL
2848}
2849EXPORT_SYMBOL_GPL(rpc_clnt_swap_activate);
2850
15001e5a
TM
2851static int
2852rpc_clnt_swap_deactivate_callback(struct rpc_clnt *clnt,
2853 struct rpc_xprt *xprt,
2854 void *dummy)
2855{
2856 xprt_disable_swap(xprt);
2857 return 0;
2858}
2859
3c87ef6e
JL
2860void
2861rpc_clnt_swap_deactivate(struct rpc_clnt *clnt)
2862{
15001e5a
TM
2863 if (atomic_dec_if_positive(&clnt->cl_swapper) == 0)
2864 rpc_clnt_iterate_for_each_xprt(clnt,
2865 rpc_clnt_swap_deactivate_callback, NULL);
3c87ef6e
JL
2866}
2867EXPORT_SYMBOL_GPL(rpc_clnt_swap_deactivate);
2868#endif /* CONFIG_SUNRPC_SWAP */