NFSv4: Convert idmapper to use the new framework for pipefs dentries
[deliverable/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>
1da177e4 28#include <linux/utsname.h>
11c556b3 29#include <linux/workqueue.h>
176e21ee 30#include <linux/in.h>
510deb0d 31#include <linux/in6.h>
176e21ee 32#include <linux/un.h>
2446ab60 33#include <linux/rcupdate.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
1da177e4
LT
45#ifdef RPC_DEBUG
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{
5f42b016 105 if (clnt->cl_dentry)
6739ffb7 106 rpc_remove_client_dir(clnt->cl_dentry, clnt);
30507f58 107 clnt->cl_dentry = NULL;
0157d021
SK
108}
109
110static void rpc_clnt_remove_pipedir(struct rpc_clnt *clnt)
111{
2446ab60 112 struct net *net = rpc_net_ns(clnt);
0157d021 113 struct super_block *pipefs_sb;
0157d021 114
2446ab60 115 pipefs_sb = rpc_get_sb_net(net);
0157d021 116 if (pipefs_sb) {
0157d021 117 __rpc_clnt_remove_pipedir(clnt);
2446ab60 118 rpc_put_sb_net(net);
0157d021 119 }
0157d021
SK
120}
121
122static struct dentry *rpc_setup_pipedir_sb(struct super_block *sb,
41b6b4d0 123 struct rpc_clnt *clnt)
1da177e4 124{
f134585a 125 static uint32_t clntid;
41b6b4d0 126 const char *dir_name = clnt->cl_program->pipe_dir_name;
23ac6581 127 char name[15];
0157d021 128 struct dentry *dir, *dentry;
1da177e4 129
0157d021 130 dir = rpc_d_lookup_sb(sb, dir_name);
922eeac3
WAA
131 if (dir == NULL) {
132 pr_info("RPC: pipefs directory doesn't exist: %s\n", dir_name);
0157d021 133 return dir;
922eeac3 134 }
f134585a 135 for (;;) {
a95e691f 136 snprintf(name, sizeof(name), "clnt%x", (unsigned int)clntid++);
23ac6581 137 name[sizeof(name) - 1] = '\0';
a95e691f 138 dentry = rpc_create_client_dir(dir, name, clnt);
0157d021 139 if (!IS_ERR(dentry))
23ac6581 140 break;
a95e691f
AV
141 if (dentry == ERR_PTR(-EEXIST))
142 continue;
143 printk(KERN_INFO "RPC: Couldn't create pipefs entry"
144 " %s/%s, error %ld\n",
145 dir_name, name, PTR_ERR(dentry));
146 break;
1da177e4 147 }
0157d021
SK
148 dput(dir);
149 return dentry;
150}
151
152static int
41b6b4d0 153rpc_setup_pipedir(struct super_block *pipefs_sb, struct rpc_clnt *clnt)
0157d021 154{
30507f58 155 struct dentry *dentry;
0157d021 156
41b6b4d0
TM
157 if (clnt->cl_program->pipe_dir_name == NULL)
158 goto out;
30507f58 159 clnt->cl_dentry = NULL;
41b6b4d0 160 dentry = rpc_setup_pipedir_sb(pipefs_sb, clnt);
30507f58
SK
161 if (IS_ERR(dentry))
162 return PTR_ERR(dentry);
163 clnt->cl_dentry = dentry;
41b6b4d0 164out:
23ac6581 165 return 0;
1da177e4
LT
166}
167
41b6b4d0 168static int rpc_clnt_skip_event(struct rpc_clnt *clnt, unsigned long event)
ea8cfa06 169{
41b6b4d0
TM
170 if (clnt->cl_program->pipe_dir_name == NULL)
171 return 1;
172
ea8cfa06
SK
173 if (((event == RPC_PIPEFS_MOUNT) && clnt->cl_dentry) ||
174 ((event == RPC_PIPEFS_UMOUNT) && !clnt->cl_dentry))
175 return 1;
4f6bb246
SK
176 if ((event == RPC_PIPEFS_MOUNT) && atomic_read(&clnt->cl_count) == 0)
177 return 1;
ea8cfa06
SK
178 return 0;
179}
180
181static int __rpc_clnt_handle_event(struct rpc_clnt *clnt, unsigned long event,
182 struct super_block *sb)
80df9d20
SK
183{
184 struct dentry *dentry;
185 int err = 0;
186
187 switch (event) {
188 case RPC_PIPEFS_MOUNT:
41b6b4d0 189 dentry = rpc_setup_pipedir_sb(sb, clnt);
922eeac3
WAA
190 if (!dentry)
191 return -ENOENT;
80df9d20
SK
192 if (IS_ERR(dentry))
193 return PTR_ERR(dentry);
30507f58 194 clnt->cl_dentry = 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 }
203 return err;
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
cbbb3449
TM
266static void rpc_clnt_set_nodename(struct rpc_clnt *clnt, const char *nodename)
267{
268 clnt->cl_nodelen = strlen(nodename);
269 if (clnt->cl_nodelen > UNX_MAXNODENAME)
270 clnt->cl_nodelen = UNX_MAXNODENAME;
271 memcpy(clnt->cl_nodename, nodename, clnt->cl_nodelen);
272}
273
e73f4cc0
SK
274static int rpc_client_register(const struct rpc_create_args *args,
275 struct rpc_clnt *clnt)
276{
c2190661
TM
277 struct rpc_auth_create_args auth_args = {
278 .pseudoflavor = args->authflavor,
279 .target_name = args->client_name,
280 };
e73f4cc0
SK
281 struct rpc_auth *auth;
282 struct net *net = rpc_net_ns(clnt);
283 struct super_block *pipefs_sb;
eeee2452 284 int err;
e73f4cc0
SK
285
286 pipefs_sb = rpc_get_sb_net(net);
287 if (pipefs_sb) {
41b6b4d0 288 err = rpc_setup_pipedir(pipefs_sb, clnt);
e73f4cc0
SK
289 if (err)
290 goto out;
291 }
292
eeee2452
TM
293 rpc_register_client(clnt);
294 if (pipefs_sb)
295 rpc_put_sb_net(net);
296
c2190661 297 auth = rpcauth_create(&auth_args, clnt);
e73f4cc0
SK
298 if (IS_ERR(auth)) {
299 dprintk("RPC: Couldn't create auth handle (flavor %u)\n",
300 args->authflavor);
301 err = PTR_ERR(auth);
302 goto err_auth;
303 }
eeee2452
TM
304 return 0;
305err_auth:
306 pipefs_sb = rpc_get_sb_net(net);
1540c5d3 307 rpc_unregister_client(clnt);
eeee2452 308 __rpc_clnt_remove_pipedir(clnt);
e73f4cc0
SK
309out:
310 if (pipefs_sb)
311 rpc_put_sb_net(net);
312 return err;
e73f4cc0
SK
313}
314
698b6d08 315static struct rpc_clnt * rpc_new_client(const struct rpc_create_args *args, struct rpc_xprt *xprt)
1da177e4 316{
a613fa16
TM
317 const struct rpc_program *program = args->program;
318 const struct rpc_version *version;
1da177e4
LT
319 struct rpc_clnt *clnt = NULL;
320 int err;
06b8d255
CL
321
322 /* sanity check the name before trying to print it */
46121cf7 323 dprintk("RPC: creating %s client for %s (xprt %p)\n",
698b6d08 324 program->name, args->servername, xprt);
1da177e4 325
4ada539e
TM
326 err = rpciod_up();
327 if (err)
328 goto out_no_rpciod;
698b6d08 329
f05c124a 330 err = -EINVAL;
698b6d08
TM
331 if (args->version >= program->nrvers)
332 goto out_err;
333 version = program->version[args->version];
334 if (version == NULL)
1da177e4
LT
335 goto out_err;
336
337 err = -ENOMEM;
0da974f4 338 clnt = kzalloc(sizeof(*clnt), GFP_KERNEL);
1da177e4
LT
339 if (!clnt)
340 goto out_err;
1da177e4
LT
341 clnt->cl_parent = clnt;
342
2446ab60 343 rcu_assign_pointer(clnt->cl_xprt, xprt);
1da177e4
LT
344 clnt->cl_procinfo = version->procs;
345 clnt->cl_maxproc = version->nrprocs;
d5b337b4 346 clnt->cl_prog = args->prognumber ? : program->number;
1da177e4 347 clnt->cl_vers = version->number;
1da177e4 348 clnt->cl_stats = program->stats;
11c556b3 349 clnt->cl_metrics = rpc_alloc_iostats(clnt);
6739ffb7 350 rpc_init_pipe_dir_head(&clnt->cl_pipedir_objects);
23bf85ba
TM
351 err = -ENOMEM;
352 if (clnt->cl_metrics == NULL)
353 goto out_no_stats;
3e32a5d9 354 clnt->cl_program = program;
6529eba0 355 INIT_LIST_HEAD(&clnt->cl_tasks);
4bef61ff 356 spin_lock_init(&clnt->cl_lock);
1da177e4 357
2446ab60 358 if (!xprt_bound(xprt))
1da177e4
LT
359 clnt->cl_autobind = 1;
360
ba7392bb
TM
361 clnt->cl_timeout = xprt->timeout;
362 if (args->timeout != NULL) {
363 memcpy(&clnt->cl_timeout_default, args->timeout,
364 sizeof(clnt->cl_timeout_default));
365 clnt->cl_timeout = &clnt->cl_timeout_default;
366 }
367
1da177e4 368 clnt->cl_rtt = &clnt->cl_rtt_default;
ba7392bb 369 rpc_init_rtt(&clnt->cl_rtt_default, clnt->cl_timeout->to_initval);
1da177e4 370
006abe88 371 atomic_set(&clnt->cl_count, 1);
34f52e35 372
1da177e4 373 /* save the nodename */
cbbb3449 374 rpc_clnt_set_nodename(clnt, utsname()->nodename);
e73f4cc0
SK
375
376 err = rpc_client_register(args, clnt);
377 if (err)
378 goto out_no_path;
1da177e4
LT
379 return clnt;
380
1da177e4 381out_no_path:
23bf85ba
TM
382 rpc_free_iostats(clnt->cl_metrics);
383out_no_stats:
1da177e4
LT
384 kfree(clnt);
385out_err:
4ada539e
TM
386 rpciod_down();
387out_no_rpciod:
f05c124a 388 xprt_put(xprt);
1da177e4
LT
389 return ERR_PTR(err);
390}
391
2c53040f 392/**
c2866763
CL
393 * rpc_create - create an RPC client and transport with one call
394 * @args: rpc_clnt create argument structure
395 *
396 * Creates and initializes an RPC transport and an RPC client.
397 *
398 * It can ping the server in order to determine if it is up, and to see if
399 * it supports this program and version. RPC_CLNT_CREATE_NOPING disables
400 * this behavior so asynchronous tasks can also use rpc_create.
401 */
402struct rpc_clnt *rpc_create(struct rpc_create_args *args)
403{
404 struct rpc_xprt *xprt;
405 struct rpc_clnt *clnt;
3c341b0b 406 struct xprt_create xprtargs = {
9a23e332 407 .net = args->net,
4fa016eb 408 .ident = args->protocol,
d3bc9a1d 409 .srcaddr = args->saddress,
96802a09
FM
410 .dstaddr = args->address,
411 .addrlen = args->addrsize,
4e0038b6 412 .servername = args->servername,
f300baba 413 .bc_xprt = args->bc_xprt,
96802a09 414 };
510deb0d 415 char servername[48];
c2866763 416
b7993ceb
TM
417 if (args->flags & RPC_CLNT_CREATE_INFINITE_SLOTS)
418 xprtargs.flags |= XPRT_CREATE_INFINITE_SLOTS;
33d90ac0
BF
419 if (args->flags & RPC_CLNT_CREATE_NO_IDLE_TIMEOUT)
420 xprtargs.flags |= XPRT_CREATE_NO_IDLE_TIMEOUT;
43780b87
CL
421 /*
422 * If the caller chooses not to specify a hostname, whip
423 * up a string representation of the passed-in address.
424 */
4e0038b6 425 if (xprtargs.servername == NULL) {
176e21ee
CL
426 struct sockaddr_un *sun =
427 (struct sockaddr_un *)args->address;
da09eb93
CL
428 struct sockaddr_in *sin =
429 (struct sockaddr_in *)args->address;
430 struct sockaddr_in6 *sin6 =
431 (struct sockaddr_in6 *)args->address;
432
510deb0d
CL
433 servername[0] = '\0';
434 switch (args->address->sa_family) {
176e21ee
CL
435 case AF_LOCAL:
436 snprintf(servername, sizeof(servername), "%s",
437 sun->sun_path);
438 break;
da09eb93 439 case AF_INET:
21454aaa
HH
440 snprintf(servername, sizeof(servername), "%pI4",
441 &sin->sin_addr.s_addr);
510deb0d 442 break;
da09eb93 443 case AF_INET6:
5b095d98 444 snprintf(servername, sizeof(servername), "%pI6",
da09eb93 445 &sin6->sin6_addr);
510deb0d 446 break;
510deb0d
CL
447 default:
448 /* caller wants default server name, but
449 * address family isn't recognized. */
450 return ERR_PTR(-EINVAL);
451 }
4e0038b6 452 xprtargs.servername = servername;
43780b87
CL
453 }
454
510deb0d
CL
455 xprt = xprt_create_transport(&xprtargs);
456 if (IS_ERR(xprt))
457 return (struct rpc_clnt *)xprt;
458
c2866763
CL
459 /*
460 * By default, kernel RPC client connects from a reserved port.
461 * CAP_NET_BIND_SERVICE will not be set for unprivileged requesters,
462 * but it is always enabled for rpciod, which handles the connect
463 * operation.
464 */
465 xprt->resvport = 1;
466 if (args->flags & RPC_CLNT_CREATE_NONPRIVPORT)
467 xprt->resvport = 0;
468
698b6d08 469 clnt = rpc_new_client(args, xprt);
c2866763
CL
470 if (IS_ERR(clnt))
471 return clnt;
472
473 if (!(args->flags & RPC_CLNT_CREATE_NOPING)) {
caabea8a 474 int err = rpc_ping(clnt);
c2866763
CL
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
c2866763
CL
485 if (args->flags & RPC_CLNT_CREATE_AUTOBIND)
486 clnt->cl_autobind = 1;
43d78ef2
CL
487 if (args->flags & RPC_CLNT_CREATE_DISCRTRY)
488 clnt->cl_discrtry = 1;
b6b6152c
OK
489 if (!(args->flags & RPC_CLNT_CREATE_QUIET))
490 clnt->cl_chatty = 1;
c2866763
CL
491
492 return clnt;
493}
b86acd50 494EXPORT_SYMBOL_GPL(rpc_create);
c2866763 495
1da177e4
LT
496/*
497 * This function clones the RPC client structure. It allows us to share the
498 * same transport while varying parameters such as the authentication
499 * flavour.
500 */
1b63a751
CL
501static struct rpc_clnt *__rpc_clone_client(struct rpc_create_args *args,
502 struct rpc_clnt *clnt)
1da177e4 503{
2446ab60 504 struct rpc_xprt *xprt;
1b63a751
CL
505 struct rpc_clnt *new;
506 int err;
1da177e4 507
1b63a751 508 err = -ENOMEM;
2446ab60
TM
509 rcu_read_lock();
510 xprt = xprt_get(rcu_dereference(clnt->cl_xprt));
511 rcu_read_unlock();
512 if (xprt == NULL)
1b63a751
CL
513 goto out_err;
514 args->servername = xprt->servername;
515
516 new = rpc_new_client(args, xprt);
517 if (IS_ERR(new)) {
518 err = PTR_ERR(new);
a58e0be6 519 goto out_err;
1b63a751
CL
520 }
521
006abe88 522 atomic_inc(&clnt->cl_count);
1b63a751
CL
523 new->cl_parent = clnt;
524
525 /* Turn off autobind on clones */
526 new->cl_autobind = 0;
527 new->cl_softrtry = clnt->cl_softrtry;
528 new->cl_discrtry = clnt->cl_discrtry;
529 new->cl_chatty = clnt->cl_chatty;
1da177e4 530 return new;
1b63a751 531
1b63a751 532out_err:
0dc47877 533 dprintk("RPC: %s: returned error %d\n", __func__, err);
3e32a5d9 534 return ERR_PTR(err);
1da177e4 535}
1b63a751
CL
536
537/**
538 * rpc_clone_client - Clone an RPC client structure
539 *
540 * @clnt: RPC client whose parameters are copied
541 *
542 * Returns a fresh RPC client or an ERR_PTR.
543 */
544struct rpc_clnt *rpc_clone_client(struct rpc_clnt *clnt)
545{
546 struct rpc_create_args args = {
547 .program = clnt->cl_program,
548 .prognumber = clnt->cl_prog,
549 .version = clnt->cl_vers,
550 .authflavor = clnt->cl_auth->au_flavor,
1b63a751
CL
551 };
552 return __rpc_clone_client(&args, clnt);
553}
e8914c65 554EXPORT_SYMBOL_GPL(rpc_clone_client);
1da177e4 555
ba9b584c
CL
556/**
557 * rpc_clone_client_set_auth - Clone an RPC client structure and set its auth
558 *
559 * @clnt: RPC client whose parameters are copied
7144bca6 560 * @flavor: security flavor for new client
ba9b584c
CL
561 *
562 * Returns a fresh RPC client or an ERR_PTR.
563 */
564struct rpc_clnt *
565rpc_clone_client_set_auth(struct rpc_clnt *clnt, rpc_authflavor_t flavor)
566{
567 struct rpc_create_args args = {
568 .program = clnt->cl_program,
569 .prognumber = clnt->cl_prog,
570 .version = clnt->cl_vers,
571 .authflavor = flavor,
ba9b584c
CL
572 };
573 return __rpc_clone_client(&args, clnt);
574}
575EXPORT_SYMBOL_GPL(rpc_clone_client_set_auth);
576
58f9612c
TM
577/*
578 * Kill all tasks for the given client.
579 * XXX: kill their descendants as well?
580 */
581void rpc_killall_tasks(struct rpc_clnt *clnt)
582{
583 struct rpc_task *rovr;
584
585
586 if (list_empty(&clnt->cl_tasks))
587 return;
588 dprintk("RPC: killing all tasks for client %p\n", clnt);
589 /*
590 * Spin lock all_tasks to prevent changes...
591 */
592 spin_lock(&clnt->cl_lock);
593 list_for_each_entry(rovr, &clnt->cl_tasks, tk_task) {
594 if (!RPC_IS_ACTIVATED(rovr))
595 continue;
596 if (!(rovr->tk_flags & RPC_TASK_KILLED)) {
597 rovr->tk_flags |= RPC_TASK_KILLED;
598 rpc_exit(rovr, -EIO);
8e26de23
SK
599 if (RPC_IS_QUEUED(rovr))
600 rpc_wake_up_queued_task(rovr->tk_waitqueue,
601 rovr);
58f9612c
TM
602 }
603 }
604 spin_unlock(&clnt->cl_lock);
605}
606EXPORT_SYMBOL_GPL(rpc_killall_tasks);
607
1da177e4
LT
608/*
609 * Properly shut down an RPC client, terminating all outstanding
90c5755f 610 * requests.
1da177e4 611 */
4c402b40 612void rpc_shutdown_client(struct rpc_clnt *clnt)
1da177e4 613{
168e4b39
WAA
614 might_sleep();
615
4e0038b6 616 dprintk_rcu("RPC: shutting down %s client for %s\n",
55909f21 617 clnt->cl_program->name,
4e0038b6 618 rcu_dereference(clnt->cl_xprt)->servername);
1da177e4 619
34f52e35 620 while (!list_empty(&clnt->cl_tasks)) {
1da177e4 621 rpc_killall_tasks(clnt);
532347e2 622 wait_event_timeout(destroy_wait,
34f52e35 623 list_empty(&clnt->cl_tasks), 1*HZ);
1da177e4
LT
624 }
625
4c402b40 626 rpc_release_client(clnt);
1da177e4 627}
e8914c65 628EXPORT_SYMBOL_GPL(rpc_shutdown_client);
1da177e4
LT
629
630/*
34f52e35 631 * Free an RPC client
1da177e4 632 */
34f52e35 633static void
006abe88 634rpc_free_client(struct rpc_clnt *clnt)
1da177e4 635{
4e0038b6 636 dprintk_rcu("RPC: destroying %s client for %s\n",
55909f21 637 clnt->cl_program->name,
4e0038b6 638 rcu_dereference(clnt->cl_xprt)->servername);
6eac7d3f 639 if (clnt->cl_parent != clnt)
8ad7c892 640 rpc_release_client(clnt->cl_parent);
f5131257 641 rpc_clnt_remove_pipedir(clnt);
adb6fa7f 642 rpc_unregister_client(clnt);
11c556b3
CL
643 rpc_free_iostats(clnt->cl_metrics);
644 clnt->cl_metrics = NULL;
2446ab60 645 xprt_put(rcu_dereference_raw(clnt->cl_xprt));
4ada539e 646 rpciod_down();
1da177e4 647 kfree(clnt);
1da177e4
LT
648}
649
1dd17ec6
TM
650/*
651 * Free an RPC client
652 */
653static void
006abe88 654rpc_free_auth(struct rpc_clnt *clnt)
1dd17ec6 655{
1dd17ec6 656 if (clnt->cl_auth == NULL) {
006abe88 657 rpc_free_client(clnt);
1dd17ec6
TM
658 return;
659 }
660
661 /*
662 * Note: RPCSEC_GSS may need to send NULL RPC calls in order to
663 * release remaining GSS contexts. This mechanism ensures
664 * that it can do so safely.
665 */
006abe88 666 atomic_inc(&clnt->cl_count);
1dd17ec6
TM
667 rpcauth_release(clnt->cl_auth);
668 clnt->cl_auth = NULL;
006abe88
TM
669 if (atomic_dec_and_test(&clnt->cl_count))
670 rpc_free_client(clnt);
1dd17ec6
TM
671}
672
1da177e4 673/*
34f52e35 674 * Release reference to the RPC client
1da177e4
LT
675 */
676void
677rpc_release_client(struct rpc_clnt *clnt)
678{
34f52e35 679 dprintk("RPC: rpc_release_client(%p)\n", clnt);
1da177e4 680
34f52e35
TM
681 if (list_empty(&clnt->cl_tasks))
682 wake_up(&destroy_wait);
006abe88
TM
683 if (atomic_dec_and_test(&clnt->cl_count))
684 rpc_free_auth(clnt);
34f52e35 685}
1d658336 686EXPORT_SYMBOL_GPL(rpc_release_client);
34f52e35 687
007e251f
AG
688/**
689 * rpc_bind_new_program - bind a new RPC program to an existing client
65b6e42c
RD
690 * @old: old rpc_client
691 * @program: rpc program to set
692 * @vers: rpc program version
007e251f
AG
693 *
694 * Clones the rpc client and sets up a new RPC program. This is mainly
695 * of use for enabling different RPC programs to share the same transport.
696 * The Sun NFSv2/v3 ACL protocol can do this.
697 */
698struct rpc_clnt *rpc_bind_new_program(struct rpc_clnt *old,
a613fa16 699 const struct rpc_program *program,
89eb21c3 700 u32 vers)
007e251f 701{
f994c43d
TM
702 struct rpc_create_args args = {
703 .program = program,
704 .prognumber = program->number,
705 .version = vers,
706 .authflavor = old->cl_auth->au_flavor,
f994c43d 707 };
007e251f 708 struct rpc_clnt *clnt;
007e251f
AG
709 int err;
710
f994c43d 711 clnt = __rpc_clone_client(&args, old);
007e251f
AG
712 if (IS_ERR(clnt))
713 goto out;
caabea8a 714 err = rpc_ping(clnt);
007e251f
AG
715 if (err != 0) {
716 rpc_shutdown_client(clnt);
717 clnt = ERR_PTR(err);
718 }
cca5172a 719out:
007e251f
AG
720 return clnt;
721}
e8914c65 722EXPORT_SYMBOL_GPL(rpc_bind_new_program);
007e251f 723
58f9612c
TM
724void rpc_task_release_client(struct rpc_task *task)
725{
726 struct rpc_clnt *clnt = task->tk_client;
727
728 if (clnt != NULL) {
729 /* Remove from client task list */
730 spin_lock(&clnt->cl_lock);
731 list_del(&task->tk_task);
732 spin_unlock(&clnt->cl_lock);
733 task->tk_client = NULL;
734
735 rpc_release_client(clnt);
736 }
737}
738
739static
740void rpc_task_set_client(struct rpc_task *task, struct rpc_clnt *clnt)
741{
742 if (clnt != NULL) {
743 rpc_task_release_client(task);
744 task->tk_client = clnt;
006abe88 745 atomic_inc(&clnt->cl_count);
58f9612c
TM
746 if (clnt->cl_softrtry)
747 task->tk_flags |= RPC_TASK_SOFT;
a564b8f0
MG
748 if (sk_memalloc_socks()) {
749 struct rpc_xprt *xprt;
750
751 rcu_read_lock();
752 xprt = rcu_dereference(clnt->cl_xprt);
753 if (xprt->swapper)
754 task->tk_flags |= RPC_TASK_SWAPPER;
755 rcu_read_unlock();
756 }
58f9612c
TM
757 /* Add to the client's list of all tasks */
758 spin_lock(&clnt->cl_lock);
759 list_add_tail(&task->tk_task, &clnt->cl_tasks);
760 spin_unlock(&clnt->cl_lock);
761 }
762}
763
cbdabc7f
AA
764void rpc_task_reset_client(struct rpc_task *task, struct rpc_clnt *clnt)
765{
766 rpc_task_release_client(task);
767 rpc_task_set_client(task, clnt);
768}
769EXPORT_SYMBOL_GPL(rpc_task_reset_client);
770
771
58f9612c
TM
772static void
773rpc_task_set_rpc_message(struct rpc_task *task, const struct rpc_message *msg)
774{
775 if (msg != NULL) {
776 task->tk_msg.rpc_proc = msg->rpc_proc;
777 task->tk_msg.rpc_argp = msg->rpc_argp;
778 task->tk_msg.rpc_resp = msg->rpc_resp;
a17c2153
TM
779 if (msg->rpc_cred != NULL)
780 task->tk_msg.rpc_cred = get_rpccred(msg->rpc_cred);
58f9612c
TM
781 }
782}
783
1da177e4
LT
784/*
785 * Default callback for async RPC calls
786 */
787static void
963d8fe5 788rpc_default_callback(struct rpc_task *task, void *data)
1da177e4
LT
789{
790}
791
963d8fe5
TM
792static const struct rpc_call_ops rpc_default_ops = {
793 .rpc_call_done = rpc_default_callback,
794};
795
c970aa85
TM
796/**
797 * rpc_run_task - Allocate a new RPC task, then run rpc_execute against it
798 * @task_setup_data: pointer to task initialisation data
799 */
800struct rpc_task *rpc_run_task(const struct rpc_task_setup *task_setup_data)
6e5b70e9 801{
19445b99 802 struct rpc_task *task;
6e5b70e9 803
84115e1c 804 task = rpc_new_task(task_setup_data);
19445b99 805 if (IS_ERR(task))
50859259 806 goto out;
6e5b70e9 807
58f9612c
TM
808 rpc_task_set_client(task, task_setup_data->rpc_client);
809 rpc_task_set_rpc_message(task, task_setup_data->rpc_message);
810
58f9612c
TM
811 if (task->tk_action == NULL)
812 rpc_call_start(task);
813
6e5b70e9
TM
814 atomic_inc(&task->tk_count);
815 rpc_execute(task);
6e5b70e9 816out:
19445b99 817 return task;
6e5b70e9 818}
c970aa85 819EXPORT_SYMBOL_GPL(rpc_run_task);
6e5b70e9
TM
820
821/**
822 * rpc_call_sync - Perform a synchronous RPC call
823 * @clnt: pointer to RPC client
824 * @msg: RPC call parameters
825 * @flags: RPC call flags
1da177e4 826 */
cbc20059 827int rpc_call_sync(struct rpc_clnt *clnt, const struct rpc_message *msg, int flags)
1da177e4
LT
828{
829 struct rpc_task *task;
84115e1c
TM
830 struct rpc_task_setup task_setup_data = {
831 .rpc_client = clnt,
832 .rpc_message = msg,
833 .callback_ops = &rpc_default_ops,
834 .flags = flags,
835 };
6e5b70e9 836 int status;
1da177e4 837
50d2bdb1
WAA
838 WARN_ON_ONCE(flags & RPC_TASK_ASYNC);
839 if (flags & RPC_TASK_ASYNC) {
840 rpc_release_calldata(task_setup_data.callback_ops,
841 task_setup_data.callback_data);
842 return -EINVAL;
843 }
1da177e4 844
c970aa85 845 task = rpc_run_task(&task_setup_data);
6e5b70e9
TM
846 if (IS_ERR(task))
847 return PTR_ERR(task);
e60859ac 848 status = task->tk_status;
bde8f00c 849 rpc_put_task(task);
1da177e4
LT
850 return status;
851}
e8914c65 852EXPORT_SYMBOL_GPL(rpc_call_sync);
1da177e4 853
6e5b70e9
TM
854/**
855 * rpc_call_async - Perform an asynchronous RPC call
856 * @clnt: pointer to RPC client
857 * @msg: RPC call parameters
858 * @flags: RPC call flags
65b6e42c 859 * @tk_ops: RPC call ops
6e5b70e9 860 * @data: user call data
1da177e4
LT
861 */
862int
cbc20059 863rpc_call_async(struct rpc_clnt *clnt, const struct rpc_message *msg, int flags,
963d8fe5 864 const struct rpc_call_ops *tk_ops, void *data)
1da177e4
LT
865{
866 struct rpc_task *task;
84115e1c
TM
867 struct rpc_task_setup task_setup_data = {
868 .rpc_client = clnt,
869 .rpc_message = msg,
870 .callback_ops = tk_ops,
871 .callback_data = data,
872 .flags = flags|RPC_TASK_ASYNC,
873 };
1da177e4 874
c970aa85 875 task = rpc_run_task(&task_setup_data);
6e5b70e9
TM
876 if (IS_ERR(task))
877 return PTR_ERR(task);
878 rpc_put_task(task);
879 return 0;
1da177e4 880}
e8914c65 881EXPORT_SYMBOL_GPL(rpc_call_async);
1da177e4 882
9e00abc3 883#if defined(CONFIG_SUNRPC_BACKCHANNEL)
55ae1aab
RL
884/**
885 * rpc_run_bc_task - Allocate a new RPC task for backchannel use, then run
886 * rpc_execute against it
7a73fdde
JSR
887 * @req: RPC request
888 * @tk_ops: RPC call ops
55ae1aab
RL
889 */
890struct rpc_task *rpc_run_bc_task(struct rpc_rqst *req,
7a73fdde 891 const struct rpc_call_ops *tk_ops)
55ae1aab
RL
892{
893 struct rpc_task *task;
894 struct xdr_buf *xbufp = &req->rq_snd_buf;
895 struct rpc_task_setup task_setup_data = {
896 .callback_ops = tk_ops,
897 };
898
899 dprintk("RPC: rpc_run_bc_task req= %p\n", req);
900 /*
901 * Create an rpc_task to send the data
902 */
903 task = rpc_new_task(&task_setup_data);
19445b99 904 if (IS_ERR(task)) {
55ae1aab
RL
905 xprt_free_bc_request(req);
906 goto out;
907 }
908 task->tk_rqstp = req;
909
910 /*
911 * Set up the xdr_buf length.
912 * This also indicates that the buffer is XDR encoded already.
913 */
914 xbufp->len = xbufp->head[0].iov_len + xbufp->page_len +
915 xbufp->tail[0].iov_len;
916
917 task->tk_action = call_bc_transmit;
918 atomic_inc(&task->tk_count);
9a6478f6 919 WARN_ON_ONCE(atomic_read(&task->tk_count) != 2);
55ae1aab
RL
920 rpc_execute(task);
921
922out:
923 dprintk("RPC: rpc_run_bc_task: task= %p\n", task);
924 return task;
925}
9e00abc3 926#endif /* CONFIG_SUNRPC_BACKCHANNEL */
55ae1aab 927
77de2c59
TM
928void
929rpc_call_start(struct rpc_task *task)
930{
931 task->tk_action = call_start;
932}
933EXPORT_SYMBOL_GPL(rpc_call_start);
934
ed39440a
CL
935/**
936 * rpc_peeraddr - extract remote peer address from clnt's xprt
937 * @clnt: RPC client structure
938 * @buf: target buffer
65b6e42c 939 * @bufsize: length of target buffer
ed39440a
CL
940 *
941 * Returns the number of bytes that are actually in the stored address.
942 */
943size_t rpc_peeraddr(struct rpc_clnt *clnt, struct sockaddr *buf, size_t bufsize)
944{
945 size_t bytes;
2446ab60
TM
946 struct rpc_xprt *xprt;
947
948 rcu_read_lock();
949 xprt = rcu_dereference(clnt->cl_xprt);
ed39440a 950
2446ab60 951 bytes = xprt->addrlen;
ed39440a
CL
952 if (bytes > bufsize)
953 bytes = bufsize;
2446ab60
TM
954 memcpy(buf, &xprt->addr, bytes);
955 rcu_read_unlock();
956
957 return bytes;
ed39440a 958}
b86acd50 959EXPORT_SYMBOL_GPL(rpc_peeraddr);
ed39440a 960
f425eba4
CL
961/**
962 * rpc_peeraddr2str - return remote peer address in printable format
963 * @clnt: RPC client structure
964 * @format: address format
965 *
2446ab60
TM
966 * NB: the lifetime of the memory referenced by the returned pointer is
967 * the same as the rpc_xprt itself. As long as the caller uses this
968 * pointer, it must hold the RCU read lock.
f425eba4 969 */
b454ae90
CL
970const char *rpc_peeraddr2str(struct rpc_clnt *clnt,
971 enum rpc_display_format_t format)
f425eba4 972{
2446ab60
TM
973 struct rpc_xprt *xprt;
974
975 xprt = rcu_dereference(clnt->cl_xprt);
7559c7a2
CL
976
977 if (xprt->address_strings[format] != NULL)
978 return xprt->address_strings[format];
979 else
980 return "unprintable";
f425eba4 981}
b86acd50 982EXPORT_SYMBOL_GPL(rpc_peeraddr2str);
f425eba4 983
2e738fdc
CL
984static const struct sockaddr_in rpc_inaddr_loopback = {
985 .sin_family = AF_INET,
986 .sin_addr.s_addr = htonl(INADDR_ANY),
987};
988
989static const struct sockaddr_in6 rpc_in6addr_loopback = {
990 .sin6_family = AF_INET6,
991 .sin6_addr = IN6ADDR_ANY_INIT,
992};
993
994/*
995 * Try a getsockname() on a connected datagram socket. Using a
996 * connected datagram socket prevents leaving a socket in TIME_WAIT.
997 * This conserves the ephemeral port number space.
998 *
999 * Returns zero and fills in "buf" if successful; otherwise, a
1000 * negative errno is returned.
1001 */
1002static int rpc_sockname(struct net *net, struct sockaddr *sap, size_t salen,
1003 struct sockaddr *buf, int buflen)
1004{
1005 struct socket *sock;
1006 int err;
1007
1008 err = __sock_create(net, sap->sa_family,
1009 SOCK_DGRAM, IPPROTO_UDP, &sock, 1);
1010 if (err < 0) {
1011 dprintk("RPC: can't create UDP socket (%d)\n", err);
1012 goto out;
1013 }
1014
1015 switch (sap->sa_family) {
1016 case AF_INET:
1017 err = kernel_bind(sock,
1018 (struct sockaddr *)&rpc_inaddr_loopback,
1019 sizeof(rpc_inaddr_loopback));
1020 break;
1021 case AF_INET6:
1022 err = kernel_bind(sock,
1023 (struct sockaddr *)&rpc_in6addr_loopback,
1024 sizeof(rpc_in6addr_loopback));
1025 break;
1026 default:
1027 err = -EAFNOSUPPORT;
1028 goto out;
1029 }
1030 if (err < 0) {
1031 dprintk("RPC: can't bind UDP socket (%d)\n", err);
1032 goto out_release;
1033 }
1034
1035 err = kernel_connect(sock, sap, salen, 0);
1036 if (err < 0) {
1037 dprintk("RPC: can't connect UDP socket (%d)\n", err);
1038 goto out_release;
1039 }
1040
1041 err = kernel_getsockname(sock, buf, &buflen);
1042 if (err < 0) {
1043 dprintk("RPC: getsockname failed (%d)\n", err);
1044 goto out_release;
1045 }
1046
1047 err = 0;
1048 if (buf->sa_family == AF_INET6) {
1049 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)buf;
1050 sin6->sin6_scope_id = 0;
1051 }
1052 dprintk("RPC: %s succeeded\n", __func__);
1053
1054out_release:
1055 sock_release(sock);
1056out:
1057 return err;
1058}
1059
1060/*
1061 * Scraping a connected socket failed, so we don't have a useable
1062 * local address. Fallback: generate an address that will prevent
1063 * the server from calling us back.
1064 *
1065 * Returns zero and fills in "buf" if successful; otherwise, a
1066 * negative errno is returned.
1067 */
1068static int rpc_anyaddr(int family, struct sockaddr *buf, size_t buflen)
1069{
1070 switch (family) {
1071 case AF_INET:
1072 if (buflen < sizeof(rpc_inaddr_loopback))
1073 return -EINVAL;
1074 memcpy(buf, &rpc_inaddr_loopback,
1075 sizeof(rpc_inaddr_loopback));
1076 break;
1077 case AF_INET6:
1078 if (buflen < sizeof(rpc_in6addr_loopback))
1079 return -EINVAL;
1080 memcpy(buf, &rpc_in6addr_loopback,
1081 sizeof(rpc_in6addr_loopback));
1082 default:
1083 dprintk("RPC: %s: address family not supported\n",
1084 __func__);
1085 return -EAFNOSUPPORT;
1086 }
1087 dprintk("RPC: %s: succeeded\n", __func__);
1088 return 0;
1089}
1090
1091/**
1092 * rpc_localaddr - discover local endpoint address for an RPC client
1093 * @clnt: RPC client structure
1094 * @buf: target buffer
1095 * @buflen: size of target buffer, in bytes
1096 *
1097 * Returns zero and fills in "buf" and "buflen" if successful;
1098 * otherwise, a negative errno is returned.
1099 *
1100 * This works even if the underlying transport is not currently connected,
1101 * or if the upper layer never previously provided a source address.
1102 *
1103 * The result of this function call is transient: multiple calls in
1104 * succession may give different results, depending on how local
1105 * networking configuration changes over time.
1106 */
1107int rpc_localaddr(struct rpc_clnt *clnt, struct sockaddr *buf, size_t buflen)
1108{
1109 struct sockaddr_storage address;
1110 struct sockaddr *sap = (struct sockaddr *)&address;
1111 struct rpc_xprt *xprt;
1112 struct net *net;
1113 size_t salen;
1114 int err;
1115
1116 rcu_read_lock();
1117 xprt = rcu_dereference(clnt->cl_xprt);
1118 salen = xprt->addrlen;
1119 memcpy(sap, &xprt->addr, salen);
1120 net = get_net(xprt->xprt_net);
1121 rcu_read_unlock();
1122
1123 rpc_set_port(sap, 0);
1124 err = rpc_sockname(net, sap, salen, buf, buflen);
1125 put_net(net);
1126 if (err != 0)
1127 /* Couldn't discover local address, return ANYADDR */
1128 return rpc_anyaddr(sap->sa_family, buf, buflen);
1129 return 0;
1130}
1131EXPORT_SYMBOL_GPL(rpc_localaddr);
1132
1da177e4
LT
1133void
1134rpc_setbufsize(struct rpc_clnt *clnt, unsigned int sndsize, unsigned int rcvsize)
1135{
2446ab60
TM
1136 struct rpc_xprt *xprt;
1137
1138 rcu_read_lock();
1139 xprt = rcu_dereference(clnt->cl_xprt);
470056c2
CL
1140 if (xprt->ops->set_buffer_size)
1141 xprt->ops->set_buffer_size(xprt, sndsize, rcvsize);
2446ab60 1142 rcu_read_unlock();
1da177e4 1143}
e8914c65 1144EXPORT_SYMBOL_GPL(rpc_setbufsize);
1da177e4 1145
2446ab60
TM
1146/**
1147 * rpc_protocol - Get transport protocol number for an RPC client
1148 * @clnt: RPC client to query
1149 *
1150 */
1151int rpc_protocol(struct rpc_clnt *clnt)
1152{
1153 int protocol;
1154
1155 rcu_read_lock();
1156 protocol = rcu_dereference(clnt->cl_xprt)->prot;
1157 rcu_read_unlock();
1158 return protocol;
1159}
1160EXPORT_SYMBOL_GPL(rpc_protocol);
1161
1162/**
1163 * rpc_net_ns - Get the network namespace for this RPC client
1164 * @clnt: RPC client to query
1165 *
1166 */
1167struct net *rpc_net_ns(struct rpc_clnt *clnt)
1168{
1169 struct net *ret;
1170
1171 rcu_read_lock();
1172 ret = rcu_dereference(clnt->cl_xprt)->xprt_net;
1173 rcu_read_unlock();
1174 return ret;
1175}
1176EXPORT_SYMBOL_GPL(rpc_net_ns);
1177
1178/**
1179 * rpc_max_payload - Get maximum payload size for a transport, in bytes
1180 * @clnt: RPC client to query
1da177e4
LT
1181 *
1182 * For stream transports, this is one RPC record fragment (see RFC
1183 * 1831), as we don't support multi-record requests yet. For datagram
1184 * transports, this is the size of an IP packet minus the IP, UDP, and
1185 * RPC header sizes.
1186 */
1187size_t rpc_max_payload(struct rpc_clnt *clnt)
1188{
2446ab60
TM
1189 size_t ret;
1190
1191 rcu_read_lock();
1192 ret = rcu_dereference(clnt->cl_xprt)->max_payload;
1193 rcu_read_unlock();
1194 return ret;
1da177e4 1195}
b86acd50 1196EXPORT_SYMBOL_GPL(rpc_max_payload);
1da177e4 1197
edddbb1e 1198/**
512e4b29 1199 * rpc_get_timeout - Get timeout for transport in units of HZ
edddbb1e
WAA
1200 * @clnt: RPC client to query
1201 */
1202unsigned long rpc_get_timeout(struct rpc_clnt *clnt)
1203{
1204 unsigned long ret;
1205
1206 rcu_read_lock();
1207 ret = rcu_dereference(clnt->cl_xprt)->timeout->to_initval;
1208 rcu_read_unlock();
1209 return ret;
1210}
1211EXPORT_SYMBOL_GPL(rpc_get_timeout);
1212
35f5a422
CL
1213/**
1214 * rpc_force_rebind - force transport to check that remote port is unchanged
1215 * @clnt: client to rebind
1216 *
1217 */
1218void rpc_force_rebind(struct rpc_clnt *clnt)
1219{
2446ab60
TM
1220 if (clnt->cl_autobind) {
1221 rcu_read_lock();
1222 xprt_clear_bound(rcu_dereference(clnt->cl_xprt));
1223 rcu_read_unlock();
1224 }
35f5a422 1225}
b86acd50 1226EXPORT_SYMBOL_GPL(rpc_force_rebind);
35f5a422 1227
aae2006e
AA
1228/*
1229 * Restart an (async) RPC call from the call_prepare state.
1230 * Usually called from within the exit handler.
1231 */
f1f88fc7 1232int
aae2006e
AA
1233rpc_restart_call_prepare(struct rpc_task *task)
1234{
1235 if (RPC_ASSASSINATED(task))
f1f88fc7 1236 return 0;
d00c5d43
TM
1237 task->tk_action = call_start;
1238 if (task->tk_ops->rpc_call_prepare != NULL)
1239 task->tk_action = rpc_prepare_task;
f1f88fc7 1240 return 1;
aae2006e
AA
1241}
1242EXPORT_SYMBOL_GPL(rpc_restart_call_prepare);
1243
1da177e4
LT
1244/*
1245 * Restart an (async) RPC call. Usually called from within the
1246 * exit handler.
1247 */
f1f88fc7 1248int
1da177e4
LT
1249rpc_restart_call(struct rpc_task *task)
1250{
1251 if (RPC_ASSASSINATED(task))
f1f88fc7 1252 return 0;
1da177e4 1253 task->tk_action = call_start;
f1f88fc7 1254 return 1;
1da177e4 1255}
e8914c65 1256EXPORT_SYMBOL_GPL(rpc_restart_call);
1da177e4 1257
3748f1e4
CL
1258#ifdef RPC_DEBUG
1259static const char *rpc_proc_name(const struct rpc_task *task)
1260{
1261 const struct rpc_procinfo *proc = task->tk_msg.rpc_proc;
1262
1263 if (proc) {
1264 if (proc->p_name)
1265 return proc->p_name;
1266 else
1267 return "NULL";
1268 } else
1269 return "no proc";
1270}
1271#endif
1272
1da177e4
LT
1273/*
1274 * 0. Initial state
1275 *
1276 * Other FSM states can be visited zero or more times, but
1277 * this state is visited exactly once for each RPC.
1278 */
1279static void
1280call_start(struct rpc_task *task)
1281{
1282 struct rpc_clnt *clnt = task->tk_client;
1283
3748f1e4 1284 dprintk("RPC: %5u call_start %s%d proc %s (%s)\n", task->tk_pid,
55909f21 1285 clnt->cl_program->name, clnt->cl_vers,
3748f1e4 1286 rpc_proc_name(task),
46121cf7 1287 (RPC_IS_ASYNC(task) ? "async" : "sync"));
1da177e4
LT
1288
1289 /* Increment call count */
1290 task->tk_msg.rpc_proc->p_count++;
1291 clnt->cl_stats->rpccnt++;
1292 task->tk_action = call_reserve;
1293}
1294
1295/*
1296 * 1. Reserve an RPC call slot
1297 */
1298static void
1299call_reserve(struct rpc_task *task)
1300{
46121cf7 1301 dprint_status(task);
1da177e4 1302
1da177e4
LT
1303 task->tk_status = 0;
1304 task->tk_action = call_reserveresult;
1305 xprt_reserve(task);
1306}
1307
ba60eb25
TM
1308static void call_retry_reserve(struct rpc_task *task);
1309
1da177e4
LT
1310/*
1311 * 1b. Grok the result of xprt_reserve()
1312 */
1313static void
1314call_reserveresult(struct rpc_task *task)
1315{
1316 int status = task->tk_status;
1317
46121cf7 1318 dprint_status(task);
1da177e4
LT
1319
1320 /*
1321 * After a call to xprt_reserve(), we must have either
1322 * a request slot or else an error status.
1323 */
1324 task->tk_status = 0;
1325 if (status >= 0) {
1326 if (task->tk_rqstp) {
f2d47d02 1327 task->tk_action = call_refresh;
1da177e4
LT
1328 return;
1329 }
1330
1331 printk(KERN_ERR "%s: status=%d, but no request slot, exiting\n",
0dc47877 1332 __func__, status);
1da177e4
LT
1333 rpc_exit(task, -EIO);
1334 return;
1335 }
1336
1337 /*
1338 * Even though there was an error, we may have acquired
1339 * a request slot somehow. Make sure not to leak it.
1340 */
1341 if (task->tk_rqstp) {
1342 printk(KERN_ERR "%s: status=%d, request allocated anyway\n",
0dc47877 1343 __func__, status);
1da177e4
LT
1344 xprt_release(task);
1345 }
1346
1347 switch (status) {
1afeaf5c
TM
1348 case -ENOMEM:
1349 rpc_delay(task, HZ >> 2);
1da177e4 1350 case -EAGAIN: /* woken up; retry */
ba60eb25 1351 task->tk_action = call_retry_reserve;
1da177e4
LT
1352 return;
1353 case -EIO: /* probably a shutdown */
1354 break;
1355 default:
1356 printk(KERN_ERR "%s: unrecognized error %d, exiting\n",
0dc47877 1357 __func__, status);
1da177e4
LT
1358 break;
1359 }
1360 rpc_exit(task, status);
1361}
1362
ba60eb25
TM
1363/*
1364 * 1c. Retry reserving an RPC call slot
1365 */
1366static void
1367call_retry_reserve(struct rpc_task *task)
1368{
1369 dprint_status(task);
1370
1371 task->tk_status = 0;
1372 task->tk_action = call_reserveresult;
1373 xprt_retry_reserve(task);
1374}
1375
1da177e4 1376/*
55576244
BF
1377 * 2. Bind and/or refresh the credentials
1378 */
1379static void
1380call_refresh(struct rpc_task *task)
1381{
1382 dprint_status(task);
1383
1384 task->tk_action = call_refreshresult;
1385 task->tk_status = 0;
1386 task->tk_client->cl_stats->rpcauthrefresh++;
1387 rpcauth_refreshcred(task);
1388}
1389
1390/*
1391 * 2a. Process the results of a credential refresh
1392 */
1393static void
1394call_refreshresult(struct rpc_task *task)
1395{
1396 int status = task->tk_status;
1397
1398 dprint_status(task);
1399
1400 task->tk_status = 0;
5fc43978 1401 task->tk_action = call_refresh;
55576244 1402 switch (status) {
5fc43978
TM
1403 case 0:
1404 if (rpcauth_uptodatecred(task))
1405 task->tk_action = call_allocate;
55576244
BF
1406 return;
1407 case -ETIMEDOUT:
1408 rpc_delay(task, 3*HZ);
eb96d5c9 1409 case -EKEYEXPIRED:
5fc43978
TM
1410 case -EAGAIN:
1411 status = -EACCES;
1412 if (!task->tk_cred_retry)
1413 break;
1414 task->tk_cred_retry--;
1415 dprintk("RPC: %5u %s: retry refresh creds\n",
1416 task->tk_pid, __func__);
1417 return;
55576244 1418 }
5fc43978
TM
1419 dprintk("RPC: %5u %s: refresh creds failed with error %d\n",
1420 task->tk_pid, __func__, status);
1421 rpc_exit(task, status);
55576244
BF
1422}
1423
1424/*
1425 * 2b. Allocate the buffer. For details, see sched.c:rpc_malloc.
02107148 1426 * (Note: buffer memory is freed in xprt_release).
1da177e4
LT
1427 */
1428static void
1429call_allocate(struct rpc_task *task)
1430{
f2d47d02 1431 unsigned int slack = task->tk_rqstp->rq_cred->cr_auth->au_cslack;
02107148 1432 struct rpc_rqst *req = task->tk_rqstp;
a4f0835c 1433 struct rpc_xprt *xprt = req->rq_xprt;
2bea90d4 1434 struct rpc_procinfo *proc = task->tk_msg.rpc_proc;
1da177e4 1435
46121cf7
CL
1436 dprint_status(task);
1437
2bea90d4 1438 task->tk_status = 0;
f2d47d02 1439 task->tk_action = call_bind;
2bea90d4 1440
02107148 1441 if (req->rq_buffer)
1da177e4
LT
1442 return;
1443
2bea90d4
CL
1444 if (proc->p_proc != 0) {
1445 BUG_ON(proc->p_arglen == 0);
1446 if (proc->p_decode != NULL)
1447 BUG_ON(proc->p_replen == 0);
1448 }
1da177e4 1449
2bea90d4
CL
1450 /*
1451 * Calculate the size (in quads) of the RPC call
1452 * and reply headers, and convert both values
1453 * to byte sizes.
1454 */
1455 req->rq_callsize = RPC_CALLHDRSIZE + (slack << 1) + proc->p_arglen;
1456 req->rq_callsize <<= 2;
1457 req->rq_rcvsize = RPC_REPHDRSIZE + slack + proc->p_replen;
1458 req->rq_rcvsize <<= 2;
1459
c5a4dd8b
CL
1460 req->rq_buffer = xprt->ops->buf_alloc(task,
1461 req->rq_callsize + req->rq_rcvsize);
2bea90d4 1462 if (req->rq_buffer != NULL)
1da177e4 1463 return;
46121cf7
CL
1464
1465 dprintk("RPC: %5u rpc_buffer allocation failed\n", task->tk_pid);
1da177e4 1466
5afa9133 1467 if (RPC_IS_ASYNC(task) || !fatal_signal_pending(current)) {
b6e9c713 1468 task->tk_action = call_allocate;
1da177e4
LT
1469 rpc_delay(task, HZ>>4);
1470 return;
1471 }
1472
1473 rpc_exit(task, -ERESTARTSYS);
1474}
1475
940e3318
TM
1476static inline int
1477rpc_task_need_encode(struct rpc_task *task)
1478{
1479 return task->tk_rqstp->rq_snd_buf.len == 0;
1480}
1481
1482static inline void
1483rpc_task_force_reencode(struct rpc_task *task)
1484{
1485 task->tk_rqstp->rq_snd_buf.len = 0;
2574cc9f 1486 task->tk_rqstp->rq_bytes_sent = 0;
940e3318
TM
1487}
1488
2bea90d4
CL
1489static inline void
1490rpc_xdr_buf_init(struct xdr_buf *buf, void *start, size_t len)
1491{
1492 buf->head[0].iov_base = start;
1493 buf->head[0].iov_len = len;
1494 buf->tail[0].iov_len = 0;
1495 buf->page_len = 0;
4f22ccc3 1496 buf->flags = 0;
2bea90d4
CL
1497 buf->len = 0;
1498 buf->buflen = len;
1499}
1500
1da177e4
LT
1501/*
1502 * 3. Encode arguments of an RPC call
1503 */
1504static void
b0e1c57e 1505rpc_xdr_encode(struct rpc_task *task)
1da177e4 1506{
1da177e4 1507 struct rpc_rqst *req = task->tk_rqstp;
9f06c719 1508 kxdreproc_t encode;
d8ed029d 1509 __be32 *p;
1da177e4 1510
46121cf7 1511 dprint_status(task);
1da177e4 1512
2bea90d4
CL
1513 rpc_xdr_buf_init(&req->rq_snd_buf,
1514 req->rq_buffer,
1515 req->rq_callsize);
1516 rpc_xdr_buf_init(&req->rq_rcv_buf,
1517 (char *)req->rq_buffer + req->rq_callsize,
1518 req->rq_rcvsize);
1da177e4 1519
b0e1c57e
CL
1520 p = rpc_encode_header(task);
1521 if (p == NULL) {
1522 printk(KERN_INFO "RPC: couldn't encode RPC header, exit EIO\n");
1da177e4
LT
1523 rpc_exit(task, -EIO);
1524 return;
1525 }
b0e1c57e
CL
1526
1527 encode = task->tk_msg.rpc_proc->p_encode;
f3680312
BF
1528 if (encode == NULL)
1529 return;
1530
1531 task->tk_status = rpcauth_wrap_req(task, encode, req, p,
1532 task->tk_msg.rpc_argp);
1da177e4
LT
1533}
1534
1535/*
1536 * 4. Get the server port number if not yet set
1537 */
1538static void
1539call_bind(struct rpc_task *task)
1540{
ad2368d6 1541 struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
1da177e4 1542
46121cf7 1543 dprint_status(task);
1da177e4 1544
da351878 1545 task->tk_action = call_connect;
ec739ef0 1546 if (!xprt_bound(xprt)) {
da351878 1547 task->tk_action = call_bind_status;
ec739ef0 1548 task->tk_timeout = xprt->bind_timeout;
bbf7c1dd 1549 xprt->ops->rpcbind(task);
1da177e4
LT
1550 }
1551}
1552
1553/*
da351878
CL
1554 * 4a. Sort out bind result
1555 */
1556static void
1557call_bind_status(struct rpc_task *task)
1558{
906462af 1559 int status = -EIO;
da351878
CL
1560
1561 if (task->tk_status >= 0) {
46121cf7 1562 dprint_status(task);
da351878
CL
1563 task->tk_status = 0;
1564 task->tk_action = call_connect;
1565 return;
1566 }
1567
5753cba1 1568 trace_rpc_bind_status(task);
da351878 1569 switch (task->tk_status) {
381ba74a
TM
1570 case -ENOMEM:
1571 dprintk("RPC: %5u rpcbind out of memory\n", task->tk_pid);
1572 rpc_delay(task, HZ >> 2);
2429cbf6 1573 goto retry_timeout;
da351878 1574 case -EACCES:
46121cf7
CL
1575 dprintk("RPC: %5u remote rpcbind: RPC program/version "
1576 "unavailable\n", task->tk_pid);
b79dc8ce
CL
1577 /* fail immediately if this is an RPC ping */
1578 if (task->tk_msg.rpc_proc->p_proc == 0) {
1579 status = -EOPNOTSUPP;
1580 break;
1581 }
0b760113
TM
1582 if (task->tk_rebind_retry == 0)
1583 break;
1584 task->tk_rebind_retry--;
ea635a51 1585 rpc_delay(task, 3*HZ);
da45828e 1586 goto retry_timeout;
da351878 1587 case -ETIMEDOUT:
46121cf7 1588 dprintk("RPC: %5u rpcbind request timed out\n",
da351878 1589 task->tk_pid);
da45828e 1590 goto retry_timeout;
da351878 1591 case -EPFNOSUPPORT:
906462af 1592 /* server doesn't support any rpcbind version we know of */
012da158 1593 dprintk("RPC: %5u unrecognized remote rpcbind service\n",
da351878
CL
1594 task->tk_pid);
1595 break;
1596 case -EPROTONOSUPPORT:
00a6e7bb 1597 dprintk("RPC: %5u remote rpcbind version unavailable, retrying\n",
da351878 1598 task->tk_pid);
00a6e7bb
CL
1599 task->tk_status = 0;
1600 task->tk_action = call_bind;
1601 return;
012da158
CL
1602 case -ECONNREFUSED: /* connection problems */
1603 case -ECONNRESET:
1604 case -ENOTCONN:
1605 case -EHOSTDOWN:
1606 case -EHOSTUNREACH:
1607 case -ENETUNREACH:
1608 case -EPIPE:
1609 dprintk("RPC: %5u remote rpcbind unreachable: %d\n",
1610 task->tk_pid, task->tk_status);
1611 if (!RPC_IS_SOFTCONN(task)) {
1612 rpc_delay(task, 5*HZ);
1613 goto retry_timeout;
1614 }
1615 status = task->tk_status;
1616 break;
da351878 1617 default:
46121cf7 1618 dprintk("RPC: %5u unrecognized rpcbind error (%d)\n",
da351878 1619 task->tk_pid, -task->tk_status);
da351878
CL
1620 }
1621
1622 rpc_exit(task, status);
1623 return;
1624
da45828e
TM
1625retry_timeout:
1626 task->tk_action = call_timeout;
da351878
CL
1627}
1628
1629/*
1630 * 4b. Connect to the RPC server
1da177e4
LT
1631 */
1632static void
1633call_connect(struct rpc_task *task)
1634{
ad2368d6 1635 struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
1da177e4 1636
46121cf7 1637 dprintk("RPC: %5u call_connect xprt %p %s connected\n",
da351878
CL
1638 task->tk_pid, xprt,
1639 (xprt_connected(xprt) ? "is" : "is not"));
1da177e4 1640
da351878
CL
1641 task->tk_action = call_transmit;
1642 if (!xprt_connected(xprt)) {
1643 task->tk_action = call_connect_status;
1644 if (task->tk_status < 0)
1645 return;
1646 xprt_connect(task);
1da177e4 1647 }
1da177e4
LT
1648}
1649
1650/*
da351878 1651 * 4c. Sort out connect result
1da177e4
LT
1652 */
1653static void
1654call_connect_status(struct rpc_task *task)
1655{
1656 struct rpc_clnt *clnt = task->tk_client;
1657 int status = task->tk_status;
1658
46121cf7 1659 dprint_status(task);
da351878 1660
5753cba1 1661 trace_rpc_connect_status(task, status);
1da177e4 1662 switch (status) {
da45828e
TM
1663 /* if soft mounted, test if we've timed out */
1664 case -ETIMEDOUT:
1665 task->tk_action = call_timeout;
3ed5e2a2
TM
1666 return;
1667 case -ECONNREFUSED:
1668 case -ECONNRESET:
1669 case -ENETUNREACH:
1670 if (RPC_IS_SOFTCONN(task))
1671 break;
1672 /* retry with existing socket, after a delay */
1673 case 0:
1674 case -EAGAIN:
1675 task->tk_status = 0;
1676 clnt->cl_stats->netreconn++;
1677 task->tk_action = call_transmit;
1678 return;
1da177e4 1679 }
3ed5e2a2 1680 rpc_exit(task, status);
1da177e4
LT
1681}
1682
1683/*
1684 * 5. Transmit the RPC request, and wait for reply
1685 */
1686static void
1687call_transmit(struct rpc_task *task)
1688{
46121cf7 1689 dprint_status(task);
1da177e4
LT
1690
1691 task->tk_action = call_status;
1692 if (task->tk_status < 0)
1693 return;
1694 task->tk_status = xprt_prepare_transmit(task);
1695 if (task->tk_status != 0)
1696 return;
e0ab53de 1697 task->tk_action = call_transmit_status;
1da177e4 1698 /* Encode here so that rpcsec_gss can use correct sequence number. */
940e3318 1699 if (rpc_task_need_encode(task)) {
b0e1c57e 1700 rpc_xdr_encode(task);
5e5ce5be 1701 /* Did the encode result in an error condition? */
8b39f2b4
TM
1702 if (task->tk_status != 0) {
1703 /* Was the error nonfatal? */
1704 if (task->tk_status == -EAGAIN)
1705 rpc_delay(task, HZ >> 4);
1706 else
1707 rpc_exit(task, task->tk_status);
e0ab53de 1708 return;
8b39f2b4 1709 }
5e5ce5be 1710 }
1da177e4
LT
1711 xprt_transmit(task);
1712 if (task->tk_status < 0)
1713 return;
e0ab53de
TM
1714 /*
1715 * On success, ensure that we call xprt_end_transmit() before sleeping
1716 * in order to allow access to the socket to other RPC requests.
1717 */
1718 call_transmit_status(task);
55ae1aab 1719 if (rpc_reply_expected(task))
e0ab53de
TM
1720 return;
1721 task->tk_action = rpc_exit_task;
a4f0835c 1722 rpc_wake_up_queued_task(&task->tk_rqstp->rq_xprt->pending, task);
e0ab53de
TM
1723}
1724
1725/*
1726 * 5a. Handle cleanup after a transmission
1727 */
1728static void
1729call_transmit_status(struct rpc_task *task)
1730{
1731 task->tk_action = call_status;
206a134b
CL
1732
1733 /*
1734 * Common case: success. Force the compiler to put this
1735 * test first.
1736 */
1737 if (task->tk_status == 0) {
1738 xprt_end_transmit(task);
1739 rpc_task_force_reencode(task);
1740 return;
1741 }
1742
15f081ca
TM
1743 switch (task->tk_status) {
1744 case -EAGAIN:
1745 break;
1746 default:
206a134b 1747 dprint_status(task);
15f081ca 1748 xprt_end_transmit(task);
09a21c41
CL
1749 rpc_task_force_reencode(task);
1750 break;
15f081ca
TM
1751 /*
1752 * Special cases: if we've been waiting on the
1753 * socket's write_space() callback, or if the
1754 * socket just returned a connection error,
1755 * then hold onto the transport lock.
1756 */
1757 case -ECONNREFUSED:
15f081ca
TM
1758 case -EHOSTDOWN:
1759 case -EHOSTUNREACH:
1760 case -ENETUNREACH:
09a21c41
CL
1761 if (RPC_IS_SOFTCONN(task)) {
1762 xprt_end_transmit(task);
1763 rpc_exit(task, task->tk_status);
1764 break;
1765 }
1766 case -ECONNRESET:
1767 case -ENOTCONN:
c8485e4d 1768 case -EPIPE:
15f081ca
TM
1769 rpc_task_force_reencode(task);
1770 }
1da177e4
LT
1771}
1772
9e00abc3 1773#if defined(CONFIG_SUNRPC_BACKCHANNEL)
55ae1aab
RL
1774/*
1775 * 5b. Send the backchannel RPC reply. On error, drop the reply. In
1776 * addition, disconnect on connectivity errors.
1777 */
1778static void
1779call_bc_transmit(struct rpc_task *task)
1780{
1781 struct rpc_rqst *req = task->tk_rqstp;
1782
55ae1aab
RL
1783 task->tk_status = xprt_prepare_transmit(task);
1784 if (task->tk_status == -EAGAIN) {
1785 /*
1786 * Could not reserve the transport. Try again after the
1787 * transport is released.
1788 */
1789 task->tk_status = 0;
1790 task->tk_action = call_bc_transmit;
1791 return;
1792 }
1793
1794 task->tk_action = rpc_exit_task;
1795 if (task->tk_status < 0) {
1796 printk(KERN_NOTICE "RPC: Could not send backchannel reply "
1797 "error: %d\n", task->tk_status);
1798 return;
1799 }
1800
1801 xprt_transmit(task);
1802 xprt_end_transmit(task);
1803 dprint_status(task);
1804 switch (task->tk_status) {
1805 case 0:
1806 /* Success */
1807 break;
1808 case -EHOSTDOWN:
1809 case -EHOSTUNREACH:
1810 case -ENETUNREACH:
1811 case -ETIMEDOUT:
1812 /*
1813 * Problem reaching the server. Disconnect and let the
1814 * forechannel reestablish the connection. The server will
1815 * have to retransmit the backchannel request and we'll
1816 * reprocess it. Since these ops are idempotent, there's no
1817 * need to cache our reply at this time.
1818 */
1819 printk(KERN_NOTICE "RPC: Could not send backchannel reply "
1820 "error: %d\n", task->tk_status);
a4f0835c 1821 xprt_conditional_disconnect(req->rq_xprt,
55ae1aab
RL
1822 req->rq_connect_cookie);
1823 break;
1824 default:
1825 /*
1826 * We were unable to reply and will have to drop the
1827 * request. The server should reconnect and retransmit.
1828 */
1facf4c4 1829 WARN_ON_ONCE(task->tk_status == -EAGAIN);
55ae1aab
RL
1830 printk(KERN_NOTICE "RPC: Could not send backchannel reply "
1831 "error: %d\n", task->tk_status);
1832 break;
1833 }
1834 rpc_wake_up_queued_task(&req->rq_xprt->pending, task);
1835}
9e00abc3 1836#endif /* CONFIG_SUNRPC_BACKCHANNEL */
55ae1aab 1837
1da177e4
LT
1838/*
1839 * 6. Sort out the RPC call status
1840 */
1841static void
1842call_status(struct rpc_task *task)
1843{
1844 struct rpc_clnt *clnt = task->tk_client;
1845 struct rpc_rqst *req = task->tk_rqstp;
1846 int status;
1847
dd2b63d0
RL
1848 if (req->rq_reply_bytes_recvd > 0 && !req->rq_bytes_sent)
1849 task->tk_status = req->rq_reply_bytes_recvd;
1da177e4 1850
46121cf7 1851 dprint_status(task);
1da177e4
LT
1852
1853 status = task->tk_status;
1854 if (status >= 0) {
1855 task->tk_action = call_decode;
1856 return;
1857 }
1858
5753cba1 1859 trace_rpc_call_status(task);
1da177e4
LT
1860 task->tk_status = 0;
1861 switch(status) {
76303992
TM
1862 case -EHOSTDOWN:
1863 case -EHOSTUNREACH:
1864 case -ENETUNREACH:
1865 /*
1866 * Delay any retries for 3 seconds, then handle as if it
1867 * were a timeout.
1868 */
1869 rpc_delay(task, 3*HZ);
1da177e4
LT
1870 case -ETIMEDOUT:
1871 task->tk_action = call_timeout;
241c39b9 1872 if (task->tk_client->cl_discrtry)
a4f0835c 1873 xprt_conditional_disconnect(req->rq_xprt,
7c1d71cf 1874 req->rq_connect_cookie);
1da177e4 1875 break;
c8485e4d 1876 case -ECONNRESET:
1da177e4 1877 case -ECONNREFUSED:
35f5a422 1878 rpc_force_rebind(clnt);
c8485e4d
TM
1879 rpc_delay(task, 3*HZ);
1880 case -EPIPE:
1881 case -ENOTCONN:
1da177e4
LT
1882 task->tk_action = call_bind;
1883 break;
1884 case -EAGAIN:
1885 task->tk_action = call_transmit;
1886 break;
1887 case -EIO:
1888 /* shutdown or soft timeout */
1889 rpc_exit(task, status);
1890 break;
1891 default:
b6b6152c
OK
1892 if (clnt->cl_chatty)
1893 printk("%s: RPC call returned error %d\n",
55909f21 1894 clnt->cl_program->name, -status);
1da177e4 1895 rpc_exit(task, status);
1da177e4
LT
1896 }
1897}
1898
1899/*
e0ab53de 1900 * 6a. Handle RPC timeout
1da177e4
LT
1901 * We do not release the request slot, so we keep using the
1902 * same XID for all retransmits.
1903 */
1904static void
1905call_timeout(struct rpc_task *task)
1906{
1907 struct rpc_clnt *clnt = task->tk_client;
1908
1909 if (xprt_adjust_timeout(task->tk_rqstp) == 0) {
46121cf7 1910 dprintk("RPC: %5u call_timeout (minor)\n", task->tk_pid);
1da177e4
LT
1911 goto retry;
1912 }
1913
46121cf7 1914 dprintk("RPC: %5u call_timeout (major)\n", task->tk_pid);
ef759a2e
CL
1915 task->tk_timeouts++;
1916
3a28becc
CL
1917 if (RPC_IS_SOFTCONN(task)) {
1918 rpc_exit(task, -ETIMEDOUT);
1919 return;
1920 }
1da177e4 1921 if (RPC_IS_SOFT(task)) {
cac5d07e 1922 if (clnt->cl_chatty) {
4e0038b6 1923 rcu_read_lock();
b6b6152c 1924 printk(KERN_NOTICE "%s: server %s not responding, timed out\n",
55909f21 1925 clnt->cl_program->name,
4e0038b6
TM
1926 rcu_dereference(clnt->cl_xprt)->servername);
1927 rcu_read_unlock();
cac5d07e 1928 }
7494d00c
TM
1929 if (task->tk_flags & RPC_TASK_TIMEOUT)
1930 rpc_exit(task, -ETIMEDOUT);
1931 else
1932 rpc_exit(task, -EIO);
1da177e4
LT
1933 return;
1934 }
1935
f518e35a 1936 if (!(task->tk_flags & RPC_CALL_MAJORSEEN)) {
1da177e4 1937 task->tk_flags |= RPC_CALL_MAJORSEEN;
4e0038b6
TM
1938 if (clnt->cl_chatty) {
1939 rcu_read_lock();
b6b6152c 1940 printk(KERN_NOTICE "%s: server %s not responding, still trying\n",
55909f21 1941 clnt->cl_program->name,
4e0038b6
TM
1942 rcu_dereference(clnt->cl_xprt)->servername);
1943 rcu_read_unlock();
1944 }
1da177e4 1945 }
35f5a422 1946 rpc_force_rebind(clnt);
b48633bd
TM
1947 /*
1948 * Did our request time out due to an RPCSEC_GSS out-of-sequence
1949 * event? RFC2203 requires the server to drop all such requests.
1950 */
1951 rpcauth_invalcred(task);
1da177e4
LT
1952
1953retry:
1954 clnt->cl_stats->rpcretrans++;
1955 task->tk_action = call_bind;
1956 task->tk_status = 0;
1957}
1958
1959/*
1960 * 7. Decode the RPC reply
1961 */
1962static void
1963call_decode(struct rpc_task *task)
1964{
1965 struct rpc_clnt *clnt = task->tk_client;
1966 struct rpc_rqst *req = task->tk_rqstp;
bf269551 1967 kxdrdproc_t decode = task->tk_msg.rpc_proc->p_decode;
d8ed029d 1968 __be32 *p;
1da177e4 1969
726fd6ad 1970 dprint_status(task);
1da177e4 1971
f518e35a 1972 if (task->tk_flags & RPC_CALL_MAJORSEEN) {
4e0038b6
TM
1973 if (clnt->cl_chatty) {
1974 rcu_read_lock();
b6b6152c 1975 printk(KERN_NOTICE "%s: server %s OK\n",
55909f21 1976 clnt->cl_program->name,
4e0038b6
TM
1977 rcu_dereference(clnt->cl_xprt)->servername);
1978 rcu_read_unlock();
1979 }
1da177e4
LT
1980 task->tk_flags &= ~RPC_CALL_MAJORSEEN;
1981 }
1982
43ac3f29
TM
1983 /*
1984 * Ensure that we see all writes made by xprt_complete_rqst()
dd2b63d0 1985 * before it changed req->rq_reply_bytes_recvd.
43ac3f29
TM
1986 */
1987 smp_rmb();
1da177e4
LT
1988 req->rq_rcv_buf.len = req->rq_private_buf.len;
1989
1990 /* Check that the softirq receive buffer is valid */
1991 WARN_ON(memcmp(&req->rq_rcv_buf, &req->rq_private_buf,
1992 sizeof(req->rq_rcv_buf)) != 0);
1993
1e799b67
TM
1994 if (req->rq_rcv_buf.len < 12) {
1995 if (!RPC_IS_SOFT(task)) {
1996 task->tk_action = call_bind;
1997 clnt->cl_stats->rpcretrans++;
1998 goto out_retry;
1999 }
2000 dprintk("RPC: %s: too small RPC reply size (%d bytes)\n",
55909f21 2001 clnt->cl_program->name, task->tk_status);
1e799b67
TM
2002 task->tk_action = call_timeout;
2003 goto out_retry;
2004 }
2005
b0e1c57e 2006 p = rpc_verify_header(task);
abbcf28f
TM
2007 if (IS_ERR(p)) {
2008 if (p == ERR_PTR(-EAGAIN))
2009 goto out_retry;
2010 return;
1da177e4
LT
2011 }
2012
abbcf28f 2013 task->tk_action = rpc_exit_task;
1da177e4 2014
6d5fcb5a 2015 if (decode) {
1da177e4
LT
2016 task->tk_status = rpcauth_unwrap_resp(task, decode, req, p,
2017 task->tk_msg.rpc_resp);
6d5fcb5a 2018 }
46121cf7
CL
2019 dprintk("RPC: %5u call_decode result %d\n", task->tk_pid,
2020 task->tk_status);
1da177e4
LT
2021 return;
2022out_retry:
1da177e4 2023 task->tk_status = 0;
b0e1c57e 2024 /* Note: rpc_verify_header() may have freed the RPC slot */
24b74bf0 2025 if (task->tk_rqstp == req) {
dd2b63d0 2026 req->rq_reply_bytes_recvd = req->rq_rcv_buf.len = 0;
24b74bf0 2027 if (task->tk_client->cl_discrtry)
a4f0835c 2028 xprt_conditional_disconnect(req->rq_xprt,
7c1d71cf 2029 req->rq_connect_cookie);
24b74bf0 2030 }
1da177e4
LT
2031}
2032
d8ed029d 2033static __be32 *
b0e1c57e 2034rpc_encode_header(struct rpc_task *task)
1da177e4
LT
2035{
2036 struct rpc_clnt *clnt = task->tk_client;
1da177e4 2037 struct rpc_rqst *req = task->tk_rqstp;
d8ed029d 2038 __be32 *p = req->rq_svec[0].iov_base;
1da177e4
LT
2039
2040 /* FIXME: check buffer size? */
808012fb 2041
a4f0835c 2042 p = xprt_skip_transport_header(req->rq_xprt, p);
1da177e4
LT
2043 *p++ = req->rq_xid; /* XID */
2044 *p++ = htonl(RPC_CALL); /* CALL */
2045 *p++ = htonl(RPC_VERSION); /* RPC version */
2046 *p++ = htonl(clnt->cl_prog); /* program number */
2047 *p++ = htonl(clnt->cl_vers); /* program version */
2048 *p++ = htonl(task->tk_msg.rpc_proc->p_proc); /* procedure */
334ccfd5
TM
2049 p = rpcauth_marshcred(task, p);
2050 req->rq_slen = xdr_adjust_iovec(&req->rq_svec[0], p);
2051 return p;
1da177e4
LT
2052}
2053
d8ed029d 2054static __be32 *
b0e1c57e 2055rpc_verify_header(struct rpc_task *task)
1da177e4 2056{
4e0038b6 2057 struct rpc_clnt *clnt = task->tk_client;
1da177e4
LT
2058 struct kvec *iov = &task->tk_rqstp->rq_rcv_buf.head[0];
2059 int len = task->tk_rqstp->rq_rcv_buf.len >> 2;
d8ed029d
AD
2060 __be32 *p = iov->iov_base;
2061 u32 n;
1da177e4
LT
2062 int error = -EACCES;
2063
e8896495
DH
2064 if ((task->tk_rqstp->rq_rcv_buf.len & 3) != 0) {
2065 /* RFC-1014 says that the representation of XDR data must be a
2066 * multiple of four bytes
2067 * - if it isn't pointer subtraction in the NFS client may give
2068 * undefined results
2069 */
8a702bbb 2070 dprintk("RPC: %5u %s: XDR representation not a multiple of"
0dc47877 2071 " 4 bytes: 0x%x\n", task->tk_pid, __func__,
8a702bbb 2072 task->tk_rqstp->rq_rcv_buf.len);
e8896495
DH
2073 goto out_eio;
2074 }
1da177e4
LT
2075 if ((len -= 3) < 0)
2076 goto out_overflow;
1da177e4 2077
f4a2e418 2078 p += 1; /* skip XID */
1da177e4 2079 if ((n = ntohl(*p++)) != RPC_REPLY) {
8a702bbb 2080 dprintk("RPC: %5u %s: not an RPC reply: %x\n",
f4a2e418 2081 task->tk_pid, __func__, n);
abbcf28f 2082 goto out_garbage;
1da177e4 2083 }
f4a2e418 2084
1da177e4
LT
2085 if ((n = ntohl(*p++)) != RPC_MSG_ACCEPTED) {
2086 if (--len < 0)
2087 goto out_overflow;
2088 switch ((n = ntohl(*p++))) {
89f0e4fe
JP
2089 case RPC_AUTH_ERROR:
2090 break;
2091 case RPC_MISMATCH:
2092 dprintk("RPC: %5u %s: RPC call version mismatch!\n",
2093 task->tk_pid, __func__);
2094 error = -EPROTONOSUPPORT;
2095 goto out_err;
2096 default:
2097 dprintk("RPC: %5u %s: RPC call rejected, "
2098 "unknown error: %x\n",
2099 task->tk_pid, __func__, n);
2100 goto out_eio;
1da177e4
LT
2101 }
2102 if (--len < 0)
2103 goto out_overflow;
2104 switch ((n = ntohl(*p++))) {
2105 case RPC_AUTH_REJECTEDCRED:
2106 case RPC_AUTH_REJECTEDVERF:
2107 case RPCSEC_GSS_CREDPROBLEM:
2108 case RPCSEC_GSS_CTXPROBLEM:
2109 if (!task->tk_cred_retry)
2110 break;
2111 task->tk_cred_retry--;
46121cf7 2112 dprintk("RPC: %5u %s: retry stale creds\n",
0dc47877 2113 task->tk_pid, __func__);
1da177e4 2114 rpcauth_invalcred(task);
220bcc2a
TM
2115 /* Ensure we obtain a new XID! */
2116 xprt_release(task);
118df3d1 2117 task->tk_action = call_reserve;
abbcf28f 2118 goto out_retry;
1da177e4
LT
2119 case RPC_AUTH_BADCRED:
2120 case RPC_AUTH_BADVERF:
2121 /* possibly garbled cred/verf? */
2122 if (!task->tk_garb_retry)
2123 break;
2124 task->tk_garb_retry--;
46121cf7 2125 dprintk("RPC: %5u %s: retry garbled creds\n",
0dc47877 2126 task->tk_pid, __func__);
1da177e4 2127 task->tk_action = call_bind;
abbcf28f 2128 goto out_retry;
1da177e4 2129 case RPC_AUTH_TOOWEAK:
4e0038b6 2130 rcu_read_lock();
b0e1c57e 2131 printk(KERN_NOTICE "RPC: server %s requires stronger "
4e0038b6
TM
2132 "authentication.\n",
2133 rcu_dereference(clnt->cl_xprt)->servername);
2134 rcu_read_unlock();
1da177e4
LT
2135 break;
2136 default:
8a702bbb 2137 dprintk("RPC: %5u %s: unknown auth error: %x\n",
0dc47877 2138 task->tk_pid, __func__, n);
1da177e4
LT
2139 error = -EIO;
2140 }
46121cf7 2141 dprintk("RPC: %5u %s: call rejected %d\n",
0dc47877 2142 task->tk_pid, __func__, n);
1da177e4
LT
2143 goto out_err;
2144 }
2145 if (!(p = rpcauth_checkverf(task, p))) {
8a702bbb 2146 dprintk("RPC: %5u %s: auth check failed\n",
0dc47877 2147 task->tk_pid, __func__);
abbcf28f 2148 goto out_garbage; /* bad verifier, retry */
1da177e4 2149 }
d8ed029d 2150 len = p - (__be32 *)iov->iov_base - 1;
1da177e4
LT
2151 if (len < 0)
2152 goto out_overflow;
2153 switch ((n = ntohl(*p++))) {
2154 case RPC_SUCCESS:
2155 return p;
2156 case RPC_PROG_UNAVAIL:
4e0038b6
TM
2157 dprintk_rcu("RPC: %5u %s: program %u is unsupported "
2158 "by server %s\n", task->tk_pid, __func__,
2159 (unsigned int)clnt->cl_prog,
2160 rcu_dereference(clnt->cl_xprt)->servername);
cdf47706
AG
2161 error = -EPFNOSUPPORT;
2162 goto out_err;
1da177e4 2163 case RPC_PROG_MISMATCH:
4e0038b6
TM
2164 dprintk_rcu("RPC: %5u %s: program %u, version %u unsupported "
2165 "by server %s\n", task->tk_pid, __func__,
2166 (unsigned int)clnt->cl_prog,
2167 (unsigned int)clnt->cl_vers,
2168 rcu_dereference(clnt->cl_xprt)->servername);
cdf47706
AG
2169 error = -EPROTONOSUPPORT;
2170 goto out_err;
1da177e4 2171 case RPC_PROC_UNAVAIL:
4e0038b6 2172 dprintk_rcu("RPC: %5u %s: proc %s unsupported by program %u, "
46121cf7 2173 "version %u on server %s\n",
0dc47877 2174 task->tk_pid, __func__,
3748f1e4 2175 rpc_proc_name(task),
4e0038b6
TM
2176 clnt->cl_prog, clnt->cl_vers,
2177 rcu_dereference(clnt->cl_xprt)->servername);
cdf47706
AG
2178 error = -EOPNOTSUPP;
2179 goto out_err;
1da177e4 2180 case RPC_GARBAGE_ARGS:
46121cf7 2181 dprintk("RPC: %5u %s: server saw garbage\n",
0dc47877 2182 task->tk_pid, __func__);
1da177e4
LT
2183 break; /* retry */
2184 default:
8a702bbb 2185 dprintk("RPC: %5u %s: server accept status: %x\n",
0dc47877 2186 task->tk_pid, __func__, n);
1da177e4
LT
2187 /* Also retry */
2188 }
2189
abbcf28f 2190out_garbage:
4e0038b6 2191 clnt->cl_stats->rpcgarbage++;
1da177e4
LT
2192 if (task->tk_garb_retry) {
2193 task->tk_garb_retry--;
46121cf7 2194 dprintk("RPC: %5u %s: retrying\n",
0dc47877 2195 task->tk_pid, __func__);
1da177e4 2196 task->tk_action = call_bind;
abbcf28f
TM
2197out_retry:
2198 return ERR_PTR(-EAGAIN);
1da177e4 2199 }
1da177e4
LT
2200out_eio:
2201 error = -EIO;
2202out_err:
2203 rpc_exit(task, error);
8a702bbb 2204 dprintk("RPC: %5u %s: call failed with error %d\n", task->tk_pid,
0dc47877 2205 __func__, error);
abbcf28f 2206 return ERR_PTR(error);
1da177e4 2207out_overflow:
8a702bbb 2208 dprintk("RPC: %5u %s: server reply was truncated.\n", task->tk_pid,
0dc47877 2209 __func__);
abbcf28f 2210 goto out_garbage;
1da177e4 2211}
5ee0ed7d 2212
9f06c719 2213static void rpcproc_encode_null(void *rqstp, struct xdr_stream *xdr, void *obj)
5ee0ed7d 2214{
5ee0ed7d
TM
2215}
2216
bf269551 2217static int rpcproc_decode_null(void *rqstp, struct xdr_stream *xdr, void *obj)
5ee0ed7d
TM
2218{
2219 return 0;
2220}
2221
2222static struct rpc_procinfo rpcproc_null = {
2223 .p_encode = rpcproc_encode_null,
2224 .p_decode = rpcproc_decode_null,
2225};
2226
caabea8a 2227static int rpc_ping(struct rpc_clnt *clnt)
5ee0ed7d
TM
2228{
2229 struct rpc_message msg = {
2230 .rpc_proc = &rpcproc_null,
2231 };
2232 int err;
2233 msg.rpc_cred = authnull_ops.lookup_cred(NULL, NULL, 0);
caabea8a 2234 err = rpc_call_sync(clnt, &msg, RPC_TASK_SOFT | RPC_TASK_SOFTCONN);
5ee0ed7d
TM
2235 put_rpccred(msg.rpc_cred);
2236 return err;
2237}
188fef11 2238
5e1550d6
TM
2239struct rpc_task *rpc_call_null(struct rpc_clnt *clnt, struct rpc_cred *cred, int flags)
2240{
2241 struct rpc_message msg = {
2242 .rpc_proc = &rpcproc_null,
2243 .rpc_cred = cred,
2244 };
84115e1c
TM
2245 struct rpc_task_setup task_setup_data = {
2246 .rpc_client = clnt,
2247 .rpc_message = &msg,
2248 .callback_ops = &rpc_default_ops,
2249 .flags = flags,
2250 };
c970aa85 2251 return rpc_run_task(&task_setup_data);
5e1550d6 2252}
e8914c65 2253EXPORT_SYMBOL_GPL(rpc_call_null);
5e1550d6 2254
188fef11 2255#ifdef RPC_DEBUG
68a23ee9
CL
2256static void rpc_show_header(void)
2257{
cb3997b5
CL
2258 printk(KERN_INFO "-pid- flgs status -client- --rqstp- "
2259 "-timeout ---ops--\n");
68a23ee9
CL
2260}
2261
38e886e0
CL
2262static void rpc_show_task(const struct rpc_clnt *clnt,
2263 const struct rpc_task *task)
2264{
2265 const char *rpc_waitq = "none";
38e886e0
CL
2266
2267 if (RPC_IS_QUEUED(task))
2268 rpc_waitq = rpc_qname(task->tk_waitqueue);
2269
b3bcedad 2270 printk(KERN_INFO "%5u %04x %6d %8p %8p %8ld %8p %sv%u %s a:%ps q:%s\n",
cb3997b5
CL
2271 task->tk_pid, task->tk_flags, task->tk_status,
2272 clnt, task->tk_rqstp, task->tk_timeout, task->tk_ops,
55909f21 2273 clnt->cl_program->name, clnt->cl_vers, rpc_proc_name(task),
b3bcedad 2274 task->tk_action, rpc_waitq);
38e886e0
CL
2275}
2276
70abc49b 2277void rpc_show_tasks(struct net *net)
188fef11
TM
2278{
2279 struct rpc_clnt *clnt;
38e886e0 2280 struct rpc_task *task;
68a23ee9 2281 int header = 0;
70abc49b 2282 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
188fef11 2283
70abc49b
SK
2284 spin_lock(&sn->rpc_client_lock);
2285 list_for_each_entry(clnt, &sn->all_clients, cl_clients) {
188fef11 2286 spin_lock(&clnt->cl_lock);
38e886e0 2287 list_for_each_entry(task, &clnt->cl_tasks, tk_task) {
68a23ee9
CL
2288 if (!header) {
2289 rpc_show_header();
2290 header++;
2291 }
38e886e0 2292 rpc_show_task(clnt, task);
188fef11
TM
2293 }
2294 spin_unlock(&clnt->cl_lock);
2295 }
70abc49b 2296 spin_unlock(&sn->rpc_client_lock);
188fef11
TM
2297}
2298#endif
This page took 0.992164 seconds and 5 git commands to generate.