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