SUNRPC: Remove obsolete comment
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / sunrpc / clnt.c
blob08ed49629b865e9e0a7d03b6837f782d184b1370
1 /*
2 * linux/net/sunrpc/clnt.c
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.
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.
16 * Copyright (C) 1992,1993 Rick Sladkey <jrs@world.std.com>
17 * Copyright (C) 1995,1996 Olaf Kirch <okir@monad.swb.de>
20 #include <asm/system.h>
22 #include <linux/module.h>
23 #include <linux/types.h>
24 #include <linux/kallsyms.h>
25 #include <linux/mm.h>
26 #include <linux/namei.h>
27 #include <linux/mount.h>
28 #include <linux/slab.h>
29 #include <linux/utsname.h>
30 #include <linux/workqueue.h>
31 #include <linux/in6.h>
33 #include <linux/sunrpc/clnt.h>
34 #include <linux/sunrpc/rpc_pipe_fs.h>
35 #include <linux/sunrpc/metrics.h>
36 #include <linux/sunrpc/bc_xprt.h>
38 #include "sunrpc.h"
40 #ifdef RPC_DEBUG
41 # define RPCDBG_FACILITY RPCDBG_CALL
42 #endif
44 #define dprint_status(t) \
45 dprintk("RPC: %5u %s (status %d)\n", t->tk_pid, \
46 __func__, t->tk_status)
49 * All RPC clients are linked into this list
51 static LIST_HEAD(all_clients);
52 static DEFINE_SPINLOCK(rpc_client_lock);
54 static DECLARE_WAIT_QUEUE_HEAD(destroy_wait);
57 static void call_start(struct rpc_task *task);
58 static void call_reserve(struct rpc_task *task);
59 static void call_reserveresult(struct rpc_task *task);
60 static void call_allocate(struct rpc_task *task);
61 static void call_decode(struct rpc_task *task);
62 static void call_bind(struct rpc_task *task);
63 static void call_bind_status(struct rpc_task *task);
64 static void call_transmit(struct rpc_task *task);
65 #if defined(CONFIG_NFS_V4_1)
66 static void call_bc_transmit(struct rpc_task *task);
67 #endif /* CONFIG_NFS_V4_1 */
68 static void call_status(struct rpc_task *task);
69 static void call_transmit_status(struct rpc_task *task);
70 static void call_refresh(struct rpc_task *task);
71 static void call_refreshresult(struct rpc_task *task);
72 static void call_timeout(struct rpc_task *task);
73 static void call_connect(struct rpc_task *task);
74 static void call_connect_status(struct rpc_task *task);
76 static __be32 *rpc_encode_header(struct rpc_task *task);
77 static __be32 *rpc_verify_header(struct rpc_task *task);
78 static int rpc_ping(struct rpc_clnt *clnt);
80 static void rpc_register_client(struct rpc_clnt *clnt)
82 spin_lock(&rpc_client_lock);
83 list_add(&clnt->cl_clients, &all_clients);
84 spin_unlock(&rpc_client_lock);
87 static void rpc_unregister_client(struct rpc_clnt *clnt)
89 spin_lock(&rpc_client_lock);
90 list_del(&clnt->cl_clients);
91 spin_unlock(&rpc_client_lock);
94 static int
95 rpc_setup_pipedir(struct rpc_clnt *clnt, char *dir_name)
97 static uint32_t clntid;
98 struct nameidata nd;
99 struct path path;
100 char name[15];
101 struct qstr q = {
102 .name = name,
104 int error;
106 clnt->cl_path.mnt = ERR_PTR(-ENOENT);
107 clnt->cl_path.dentry = ERR_PTR(-ENOENT);
108 if (dir_name == NULL)
109 return 0;
111 path.mnt = rpc_get_mount();
112 if (IS_ERR(path.mnt))
113 return PTR_ERR(path.mnt);
114 error = vfs_path_lookup(path.mnt->mnt_root, path.mnt, dir_name, 0, &nd);
115 if (error)
116 goto err;
118 for (;;) {
119 q.len = snprintf(name, sizeof(name), "clnt%x", (unsigned int)clntid++);
120 name[sizeof(name) - 1] = '\0';
121 q.hash = full_name_hash(q.name, q.len);
122 path.dentry = rpc_create_client_dir(nd.path.dentry, &q, clnt);
123 if (!IS_ERR(path.dentry))
124 break;
125 error = PTR_ERR(path.dentry);
126 if (error != -EEXIST) {
127 printk(KERN_INFO "RPC: Couldn't create pipefs entry"
128 " %s/%s, error %d\n",
129 dir_name, name, error);
130 goto err_path_put;
133 path_put(&nd.path);
134 clnt->cl_path = path;
135 return 0;
136 err_path_put:
137 path_put(&nd.path);
138 err:
139 rpc_put_mount();
140 return error;
143 static struct rpc_clnt * rpc_new_client(const struct rpc_create_args *args, struct rpc_xprt *xprt)
145 struct rpc_program *program = args->program;
146 struct rpc_version *version;
147 struct rpc_clnt *clnt = NULL;
148 struct rpc_auth *auth;
149 int err;
150 size_t len;
152 /* sanity check the name before trying to print it */
153 err = -EINVAL;
154 len = strlen(args->servername);
155 if (len > RPC_MAXNETNAMELEN)
156 goto out_no_rpciod;
157 len++;
159 dprintk("RPC: creating %s client for %s (xprt %p)\n",
160 program->name, args->servername, xprt);
162 err = rpciod_up();
163 if (err)
164 goto out_no_rpciod;
165 err = -EINVAL;
166 if (!xprt)
167 goto out_no_xprt;
169 if (args->version >= program->nrvers)
170 goto out_err;
171 version = program->version[args->version];
172 if (version == NULL)
173 goto out_err;
175 err = -ENOMEM;
176 clnt = kzalloc(sizeof(*clnt), GFP_KERNEL);
177 if (!clnt)
178 goto out_err;
179 clnt->cl_parent = clnt;
181 clnt->cl_server = clnt->cl_inline_name;
182 if (len > sizeof(clnt->cl_inline_name)) {
183 char *buf = kmalloc(len, GFP_KERNEL);
184 if (buf != NULL)
185 clnt->cl_server = buf;
186 else
187 len = sizeof(clnt->cl_inline_name);
189 strlcpy(clnt->cl_server, args->servername, len);
191 clnt->cl_xprt = xprt;
192 clnt->cl_procinfo = version->procs;
193 clnt->cl_maxproc = version->nrprocs;
194 clnt->cl_protname = program->name;
195 clnt->cl_prog = args->prognumber ? : program->number;
196 clnt->cl_vers = version->number;
197 clnt->cl_stats = program->stats;
198 clnt->cl_metrics = rpc_alloc_iostats(clnt);
199 err = -ENOMEM;
200 if (clnt->cl_metrics == NULL)
201 goto out_no_stats;
202 clnt->cl_program = program;
203 INIT_LIST_HEAD(&clnt->cl_tasks);
204 spin_lock_init(&clnt->cl_lock);
206 if (!xprt_bound(clnt->cl_xprt))
207 clnt->cl_autobind = 1;
209 clnt->cl_timeout = xprt->timeout;
210 if (args->timeout != NULL) {
211 memcpy(&clnt->cl_timeout_default, args->timeout,
212 sizeof(clnt->cl_timeout_default));
213 clnt->cl_timeout = &clnt->cl_timeout_default;
216 clnt->cl_rtt = &clnt->cl_rtt_default;
217 rpc_init_rtt(&clnt->cl_rtt_default, clnt->cl_timeout->to_initval);
218 clnt->cl_principal = NULL;
219 if (args->client_name) {
220 clnt->cl_principal = kstrdup(args->client_name, GFP_KERNEL);
221 if (!clnt->cl_principal)
222 goto out_no_principal;
225 atomic_set(&clnt->cl_count, 1);
227 err = rpc_setup_pipedir(clnt, program->pipe_dir_name);
228 if (err < 0)
229 goto out_no_path;
231 auth = rpcauth_create(args->authflavor, clnt);
232 if (IS_ERR(auth)) {
233 printk(KERN_INFO "RPC: Couldn't create auth handle (flavor %u)\n",
234 args->authflavor);
235 err = PTR_ERR(auth);
236 goto out_no_auth;
239 /* save the nodename */
240 clnt->cl_nodelen = strlen(init_utsname()->nodename);
241 if (clnt->cl_nodelen > UNX_MAXNODENAME)
242 clnt->cl_nodelen = UNX_MAXNODENAME;
243 memcpy(clnt->cl_nodename, init_utsname()->nodename, clnt->cl_nodelen);
244 rpc_register_client(clnt);
245 return clnt;
247 out_no_auth:
248 if (!IS_ERR(clnt->cl_path.dentry)) {
249 rpc_remove_client_dir(clnt->cl_path.dentry);
250 rpc_put_mount();
252 out_no_path:
253 kfree(clnt->cl_principal);
254 out_no_principal:
255 rpc_free_iostats(clnt->cl_metrics);
256 out_no_stats:
257 if (clnt->cl_server != clnt->cl_inline_name)
258 kfree(clnt->cl_server);
259 kfree(clnt);
260 out_err:
261 xprt_put(xprt);
262 out_no_xprt:
263 rpciod_down();
264 out_no_rpciod:
265 return ERR_PTR(err);
269 * rpc_create - create an RPC client and transport with one call
270 * @args: rpc_clnt create argument structure
272 * Creates and initializes an RPC transport and an RPC client.
274 * It can ping the server in order to determine if it is up, and to see if
275 * it supports this program and version. RPC_CLNT_CREATE_NOPING disables
276 * this behavior so asynchronous tasks can also use rpc_create.
278 struct rpc_clnt *rpc_create(struct rpc_create_args *args)
280 struct rpc_xprt *xprt;
281 struct rpc_clnt *clnt;
282 struct xprt_create xprtargs = {
283 .net = args->net,
284 .ident = args->protocol,
285 .srcaddr = args->saddress,
286 .dstaddr = args->address,
287 .addrlen = args->addrsize,
288 .bc_xprt = args->bc_xprt,
290 char servername[48];
293 * If the caller chooses not to specify a hostname, whip
294 * up a string representation of the passed-in address.
296 if (args->servername == NULL) {
297 struct sockaddr_in *sin =
298 (struct sockaddr_in *)args->address;
299 struct sockaddr_in6 *sin6 =
300 (struct sockaddr_in6 *)args->address;
302 servername[0] = '\0';
303 switch (args->address->sa_family) {
304 case AF_INET:
305 snprintf(servername, sizeof(servername), "%pI4",
306 &sin->sin_addr.s_addr);
307 break;
308 case AF_INET6:
309 snprintf(servername, sizeof(servername), "%pI6",
310 &sin6->sin6_addr);
311 break;
312 default:
313 /* caller wants default server name, but
314 * address family isn't recognized. */
315 return ERR_PTR(-EINVAL);
317 args->servername = servername;
320 xprt = xprt_create_transport(&xprtargs);
321 if (IS_ERR(xprt))
322 return (struct rpc_clnt *)xprt;
325 * By default, kernel RPC client connects from a reserved port.
326 * CAP_NET_BIND_SERVICE will not be set for unprivileged requesters,
327 * but it is always enabled for rpciod, which handles the connect
328 * operation.
330 xprt->resvport = 1;
331 if (args->flags & RPC_CLNT_CREATE_NONPRIVPORT)
332 xprt->resvport = 0;
334 clnt = rpc_new_client(args, xprt);
335 if (IS_ERR(clnt))
336 return clnt;
338 if (!(args->flags & RPC_CLNT_CREATE_NOPING)) {
339 int err = rpc_ping(clnt);
340 if (err != 0) {
341 rpc_shutdown_client(clnt);
342 return ERR_PTR(err);
346 clnt->cl_softrtry = 1;
347 if (args->flags & RPC_CLNT_CREATE_HARDRTRY)
348 clnt->cl_softrtry = 0;
350 if (args->flags & RPC_CLNT_CREATE_AUTOBIND)
351 clnt->cl_autobind = 1;
352 if (args->flags & RPC_CLNT_CREATE_DISCRTRY)
353 clnt->cl_discrtry = 1;
354 if (!(args->flags & RPC_CLNT_CREATE_QUIET))
355 clnt->cl_chatty = 1;
357 return clnt;
359 EXPORT_SYMBOL_GPL(rpc_create);
362 * This function clones the RPC client structure. It allows us to share the
363 * same transport while varying parameters such as the authentication
364 * flavour.
366 struct rpc_clnt *
367 rpc_clone_client(struct rpc_clnt *clnt)
369 struct rpc_clnt *new;
370 int err = -ENOMEM;
372 new = kmemdup(clnt, sizeof(*new), GFP_KERNEL);
373 if (!new)
374 goto out_no_clnt;
375 new->cl_parent = clnt;
376 /* Turn off autobind on clones */
377 new->cl_autobind = 0;
378 INIT_LIST_HEAD(&new->cl_tasks);
379 spin_lock_init(&new->cl_lock);
380 rpc_init_rtt(&new->cl_rtt_default, clnt->cl_timeout->to_initval);
381 new->cl_metrics = rpc_alloc_iostats(clnt);
382 if (new->cl_metrics == NULL)
383 goto out_no_stats;
384 if (clnt->cl_principal) {
385 new->cl_principal = kstrdup(clnt->cl_principal, GFP_KERNEL);
386 if (new->cl_principal == NULL)
387 goto out_no_principal;
389 atomic_set(&new->cl_count, 1);
390 err = rpc_setup_pipedir(new, clnt->cl_program->pipe_dir_name);
391 if (err != 0)
392 goto out_no_path;
393 if (new->cl_auth)
394 atomic_inc(&new->cl_auth->au_count);
395 xprt_get(clnt->cl_xprt);
396 atomic_inc(&clnt->cl_count);
397 rpc_register_client(new);
398 rpciod_up();
399 return new;
400 out_no_path:
401 kfree(new->cl_principal);
402 out_no_principal:
403 rpc_free_iostats(new->cl_metrics);
404 out_no_stats:
405 kfree(new);
406 out_no_clnt:
407 dprintk("RPC: %s: returned error %d\n", __func__, err);
408 return ERR_PTR(err);
410 EXPORT_SYMBOL_GPL(rpc_clone_client);
413 * Kill all tasks for the given client.
414 * XXX: kill their descendants as well?
416 void rpc_killall_tasks(struct rpc_clnt *clnt)
418 struct rpc_task *rovr;
421 if (list_empty(&clnt->cl_tasks))
422 return;
423 dprintk("RPC: killing all tasks for client %p\n", clnt);
425 * Spin lock all_tasks to prevent changes...
427 spin_lock(&clnt->cl_lock);
428 list_for_each_entry(rovr, &clnt->cl_tasks, tk_task) {
429 if (!RPC_IS_ACTIVATED(rovr))
430 continue;
431 if (!(rovr->tk_flags & RPC_TASK_KILLED)) {
432 rovr->tk_flags |= RPC_TASK_KILLED;
433 rpc_exit(rovr, -EIO);
434 if (RPC_IS_QUEUED(rovr))
435 rpc_wake_up_queued_task(rovr->tk_waitqueue,
436 rovr);
439 spin_unlock(&clnt->cl_lock);
441 EXPORT_SYMBOL_GPL(rpc_killall_tasks);
444 * Properly shut down an RPC client, terminating all outstanding
445 * requests.
447 void rpc_shutdown_client(struct rpc_clnt *clnt)
449 dprintk("RPC: shutting down %s client for %s\n",
450 clnt->cl_protname, clnt->cl_server);
452 while (!list_empty(&clnt->cl_tasks)) {
453 rpc_killall_tasks(clnt);
454 wait_event_timeout(destroy_wait,
455 list_empty(&clnt->cl_tasks), 1*HZ);
458 rpc_release_client(clnt);
460 EXPORT_SYMBOL_GPL(rpc_shutdown_client);
463 * Free an RPC client
465 static void
466 rpc_free_client(struct rpc_clnt *clnt)
468 dprintk("RPC: destroying %s client for %s\n",
469 clnt->cl_protname, clnt->cl_server);
470 if (!IS_ERR(clnt->cl_path.dentry)) {
471 rpc_remove_client_dir(clnt->cl_path.dentry);
472 rpc_put_mount();
474 if (clnt->cl_parent != clnt) {
475 rpc_release_client(clnt->cl_parent);
476 goto out_free;
478 if (clnt->cl_server != clnt->cl_inline_name)
479 kfree(clnt->cl_server);
480 out_free:
481 rpc_unregister_client(clnt);
482 rpc_free_iostats(clnt->cl_metrics);
483 kfree(clnt->cl_principal);
484 clnt->cl_metrics = NULL;
485 xprt_put(clnt->cl_xprt);
486 rpciod_down();
487 kfree(clnt);
491 * Free an RPC client
493 static void
494 rpc_free_auth(struct rpc_clnt *clnt)
496 if (clnt->cl_auth == NULL) {
497 rpc_free_client(clnt);
498 return;
502 * Note: RPCSEC_GSS may need to send NULL RPC calls in order to
503 * release remaining GSS contexts. This mechanism ensures
504 * that it can do so safely.
506 atomic_inc(&clnt->cl_count);
507 rpcauth_release(clnt->cl_auth);
508 clnt->cl_auth = NULL;
509 if (atomic_dec_and_test(&clnt->cl_count))
510 rpc_free_client(clnt);
514 * Release reference to the RPC client
516 void
517 rpc_release_client(struct rpc_clnt *clnt)
519 dprintk("RPC: rpc_release_client(%p)\n", clnt);
521 if (list_empty(&clnt->cl_tasks))
522 wake_up(&destroy_wait);
523 if (atomic_dec_and_test(&clnt->cl_count))
524 rpc_free_auth(clnt);
528 * rpc_bind_new_program - bind a new RPC program to an existing client
529 * @old: old rpc_client
530 * @program: rpc program to set
531 * @vers: rpc program version
533 * Clones the rpc client and sets up a new RPC program. This is mainly
534 * of use for enabling different RPC programs to share the same transport.
535 * The Sun NFSv2/v3 ACL protocol can do this.
537 struct rpc_clnt *rpc_bind_new_program(struct rpc_clnt *old,
538 struct rpc_program *program,
539 u32 vers)
541 struct rpc_clnt *clnt;
542 struct rpc_version *version;
543 int err;
545 BUG_ON(vers >= program->nrvers || !program->version[vers]);
546 version = program->version[vers];
547 clnt = rpc_clone_client(old);
548 if (IS_ERR(clnt))
549 goto out;
550 clnt->cl_procinfo = version->procs;
551 clnt->cl_maxproc = version->nrprocs;
552 clnt->cl_protname = program->name;
553 clnt->cl_prog = program->number;
554 clnt->cl_vers = version->number;
555 clnt->cl_stats = program->stats;
556 err = rpc_ping(clnt);
557 if (err != 0) {
558 rpc_shutdown_client(clnt);
559 clnt = ERR_PTR(err);
561 out:
562 return clnt;
564 EXPORT_SYMBOL_GPL(rpc_bind_new_program);
566 void rpc_task_release_client(struct rpc_task *task)
568 struct rpc_clnt *clnt = task->tk_client;
570 if (clnt != NULL) {
571 /* Remove from client task list */
572 spin_lock(&clnt->cl_lock);
573 list_del(&task->tk_task);
574 spin_unlock(&clnt->cl_lock);
575 task->tk_client = NULL;
577 rpc_release_client(clnt);
581 static
582 void rpc_task_set_client(struct rpc_task *task, struct rpc_clnt *clnt)
584 if (clnt != NULL) {
585 rpc_task_release_client(task);
586 task->tk_client = clnt;
587 atomic_inc(&clnt->cl_count);
588 if (clnt->cl_softrtry)
589 task->tk_flags |= RPC_TASK_SOFT;
590 /* Add to the client's list of all tasks */
591 spin_lock(&clnt->cl_lock);
592 list_add_tail(&task->tk_task, &clnt->cl_tasks);
593 spin_unlock(&clnt->cl_lock);
597 void rpc_task_reset_client(struct rpc_task *task, struct rpc_clnt *clnt)
599 rpc_task_release_client(task);
600 rpc_task_set_client(task, clnt);
602 EXPORT_SYMBOL_GPL(rpc_task_reset_client);
605 static void
606 rpc_task_set_rpc_message(struct rpc_task *task, const struct rpc_message *msg)
608 if (msg != NULL) {
609 task->tk_msg.rpc_proc = msg->rpc_proc;
610 task->tk_msg.rpc_argp = msg->rpc_argp;
611 task->tk_msg.rpc_resp = msg->rpc_resp;
612 if (msg->rpc_cred != NULL)
613 task->tk_msg.rpc_cred = get_rpccred(msg->rpc_cred);
618 * Default callback for async RPC calls
620 static void
621 rpc_default_callback(struct rpc_task *task, void *data)
625 static const struct rpc_call_ops rpc_default_ops = {
626 .rpc_call_done = rpc_default_callback,
630 * rpc_run_task - Allocate a new RPC task, then run rpc_execute against it
631 * @task_setup_data: pointer to task initialisation data
633 struct rpc_task *rpc_run_task(const struct rpc_task_setup *task_setup_data)
635 struct rpc_task *task;
637 task = rpc_new_task(task_setup_data);
638 if (IS_ERR(task))
639 goto out;
641 rpc_task_set_client(task, task_setup_data->rpc_client);
642 rpc_task_set_rpc_message(task, task_setup_data->rpc_message);
644 if (task->tk_action == NULL)
645 rpc_call_start(task);
647 atomic_inc(&task->tk_count);
648 rpc_execute(task);
649 out:
650 return task;
652 EXPORT_SYMBOL_GPL(rpc_run_task);
655 * rpc_call_sync - Perform a synchronous RPC call
656 * @clnt: pointer to RPC client
657 * @msg: RPC call parameters
658 * @flags: RPC call flags
660 int rpc_call_sync(struct rpc_clnt *clnt, const struct rpc_message *msg, int flags)
662 struct rpc_task *task;
663 struct rpc_task_setup task_setup_data = {
664 .rpc_client = clnt,
665 .rpc_message = msg,
666 .callback_ops = &rpc_default_ops,
667 .flags = flags,
669 int status;
671 BUG_ON(flags & RPC_TASK_ASYNC);
673 task = rpc_run_task(&task_setup_data);
674 if (IS_ERR(task))
675 return PTR_ERR(task);
676 status = task->tk_status;
677 rpc_put_task(task);
678 return status;
680 EXPORT_SYMBOL_GPL(rpc_call_sync);
683 * rpc_call_async - Perform an asynchronous RPC call
684 * @clnt: pointer to RPC client
685 * @msg: RPC call parameters
686 * @flags: RPC call flags
687 * @tk_ops: RPC call ops
688 * @data: user call data
691 rpc_call_async(struct rpc_clnt *clnt, const struct rpc_message *msg, int flags,
692 const struct rpc_call_ops *tk_ops, void *data)
694 struct rpc_task *task;
695 struct rpc_task_setup task_setup_data = {
696 .rpc_client = clnt,
697 .rpc_message = msg,
698 .callback_ops = tk_ops,
699 .callback_data = data,
700 .flags = flags|RPC_TASK_ASYNC,
703 task = rpc_run_task(&task_setup_data);
704 if (IS_ERR(task))
705 return PTR_ERR(task);
706 rpc_put_task(task);
707 return 0;
709 EXPORT_SYMBOL_GPL(rpc_call_async);
711 #if defined(CONFIG_NFS_V4_1)
713 * rpc_run_bc_task - Allocate a new RPC task for backchannel use, then run
714 * rpc_execute against it
715 * @req: RPC request
716 * @tk_ops: RPC call ops
718 struct rpc_task *rpc_run_bc_task(struct rpc_rqst *req,
719 const struct rpc_call_ops *tk_ops)
721 struct rpc_task *task;
722 struct xdr_buf *xbufp = &req->rq_snd_buf;
723 struct rpc_task_setup task_setup_data = {
724 .callback_ops = tk_ops,
727 dprintk("RPC: rpc_run_bc_task req= %p\n", req);
729 * Create an rpc_task to send the data
731 task = rpc_new_task(&task_setup_data);
732 if (IS_ERR(task)) {
733 xprt_free_bc_request(req);
734 goto out;
736 task->tk_rqstp = req;
739 * Set up the xdr_buf length.
740 * This also indicates that the buffer is XDR encoded already.
742 xbufp->len = xbufp->head[0].iov_len + xbufp->page_len +
743 xbufp->tail[0].iov_len;
745 task->tk_action = call_bc_transmit;
746 atomic_inc(&task->tk_count);
747 BUG_ON(atomic_read(&task->tk_count) != 2);
748 rpc_execute(task);
750 out:
751 dprintk("RPC: rpc_run_bc_task: task= %p\n", task);
752 return task;
754 #endif /* CONFIG_NFS_V4_1 */
756 void
757 rpc_call_start(struct rpc_task *task)
759 task->tk_action = call_start;
761 EXPORT_SYMBOL_GPL(rpc_call_start);
764 * rpc_peeraddr - extract remote peer address from clnt's xprt
765 * @clnt: RPC client structure
766 * @buf: target buffer
767 * @bufsize: length of target buffer
769 * Returns the number of bytes that are actually in the stored address.
771 size_t rpc_peeraddr(struct rpc_clnt *clnt, struct sockaddr *buf, size_t bufsize)
773 size_t bytes;
774 struct rpc_xprt *xprt = clnt->cl_xprt;
776 bytes = sizeof(xprt->addr);
777 if (bytes > bufsize)
778 bytes = bufsize;
779 memcpy(buf, &clnt->cl_xprt->addr, bytes);
780 return xprt->addrlen;
782 EXPORT_SYMBOL_GPL(rpc_peeraddr);
785 * rpc_peeraddr2str - return remote peer address in printable format
786 * @clnt: RPC client structure
787 * @format: address format
790 const char *rpc_peeraddr2str(struct rpc_clnt *clnt,
791 enum rpc_display_format_t format)
793 struct rpc_xprt *xprt = clnt->cl_xprt;
795 if (xprt->address_strings[format] != NULL)
796 return xprt->address_strings[format];
797 else
798 return "unprintable";
800 EXPORT_SYMBOL_GPL(rpc_peeraddr2str);
802 void
803 rpc_setbufsize(struct rpc_clnt *clnt, unsigned int sndsize, unsigned int rcvsize)
805 struct rpc_xprt *xprt = clnt->cl_xprt;
806 if (xprt->ops->set_buffer_size)
807 xprt->ops->set_buffer_size(xprt, sndsize, rcvsize);
809 EXPORT_SYMBOL_GPL(rpc_setbufsize);
812 * Return size of largest payload RPC client can support, in bytes
814 * For stream transports, this is one RPC record fragment (see RFC
815 * 1831), as we don't support multi-record requests yet. For datagram
816 * transports, this is the size of an IP packet minus the IP, UDP, and
817 * RPC header sizes.
819 size_t rpc_max_payload(struct rpc_clnt *clnt)
821 return clnt->cl_xprt->max_payload;
823 EXPORT_SYMBOL_GPL(rpc_max_payload);
826 * rpc_force_rebind - force transport to check that remote port is unchanged
827 * @clnt: client to rebind
830 void rpc_force_rebind(struct rpc_clnt *clnt)
832 if (clnt->cl_autobind)
833 xprt_clear_bound(clnt->cl_xprt);
835 EXPORT_SYMBOL_GPL(rpc_force_rebind);
838 * Restart an (async) RPC call from the call_prepare state.
839 * Usually called from within the exit handler.
842 rpc_restart_call_prepare(struct rpc_task *task)
844 if (RPC_ASSASSINATED(task))
845 return 0;
846 task->tk_action = rpc_prepare_task;
847 return 1;
849 EXPORT_SYMBOL_GPL(rpc_restart_call_prepare);
852 * Restart an (async) RPC call. Usually called from within the
853 * exit handler.
856 rpc_restart_call(struct rpc_task *task)
858 if (RPC_ASSASSINATED(task))
859 return 0;
860 task->tk_action = call_start;
861 return 1;
863 EXPORT_SYMBOL_GPL(rpc_restart_call);
865 #ifdef RPC_DEBUG
866 static const char *rpc_proc_name(const struct rpc_task *task)
868 const struct rpc_procinfo *proc = task->tk_msg.rpc_proc;
870 if (proc) {
871 if (proc->p_name)
872 return proc->p_name;
873 else
874 return "NULL";
875 } else
876 return "no proc";
878 #endif
881 * 0. Initial state
883 * Other FSM states can be visited zero or more times, but
884 * this state is visited exactly once for each RPC.
886 static void
887 call_start(struct rpc_task *task)
889 struct rpc_clnt *clnt = task->tk_client;
891 dprintk("RPC: %5u call_start %s%d proc %s (%s)\n", task->tk_pid,
892 clnt->cl_protname, clnt->cl_vers,
893 rpc_proc_name(task),
894 (RPC_IS_ASYNC(task) ? "async" : "sync"));
896 /* Increment call count */
897 task->tk_msg.rpc_proc->p_count++;
898 clnt->cl_stats->rpccnt++;
899 task->tk_action = call_reserve;
903 * 1. Reserve an RPC call slot
905 static void
906 call_reserve(struct rpc_task *task)
908 dprint_status(task);
910 task->tk_status = 0;
911 task->tk_action = call_reserveresult;
912 xprt_reserve(task);
916 * 1b. Grok the result of xprt_reserve()
918 static void
919 call_reserveresult(struct rpc_task *task)
921 int status = task->tk_status;
923 dprint_status(task);
926 * After a call to xprt_reserve(), we must have either
927 * a request slot or else an error status.
929 task->tk_status = 0;
930 if (status >= 0) {
931 if (task->tk_rqstp) {
932 task->tk_action = call_refresh;
933 return;
936 printk(KERN_ERR "%s: status=%d, but no request slot, exiting\n",
937 __func__, status);
938 rpc_exit(task, -EIO);
939 return;
943 * Even though there was an error, we may have acquired
944 * a request slot somehow. Make sure not to leak it.
946 if (task->tk_rqstp) {
947 printk(KERN_ERR "%s: status=%d, request allocated anyway\n",
948 __func__, status);
949 xprt_release(task);
952 switch (status) {
953 case -EAGAIN: /* woken up; retry */
954 task->tk_action = call_reserve;
955 return;
956 case -EIO: /* probably a shutdown */
957 break;
958 default:
959 printk(KERN_ERR "%s: unrecognized error %d, exiting\n",
960 __func__, status);
961 break;
963 rpc_exit(task, status);
967 * 2. Bind and/or refresh the credentials
969 static void
970 call_refresh(struct rpc_task *task)
972 dprint_status(task);
974 task->tk_action = call_refreshresult;
975 task->tk_status = 0;
976 task->tk_client->cl_stats->rpcauthrefresh++;
977 rpcauth_refreshcred(task);
981 * 2a. Process the results of a credential refresh
983 static void
984 call_refreshresult(struct rpc_task *task)
986 int status = task->tk_status;
988 dprint_status(task);
990 task->tk_status = 0;
991 task->tk_action = call_refresh;
992 switch (status) {
993 case 0:
994 if (rpcauth_uptodatecred(task))
995 task->tk_action = call_allocate;
996 return;
997 case -ETIMEDOUT:
998 rpc_delay(task, 3*HZ);
999 case -EAGAIN:
1000 status = -EACCES;
1001 if (!task->tk_cred_retry)
1002 break;
1003 task->tk_cred_retry--;
1004 dprintk("RPC: %5u %s: retry refresh creds\n",
1005 task->tk_pid, __func__);
1006 return;
1008 dprintk("RPC: %5u %s: refresh creds failed with error %d\n",
1009 task->tk_pid, __func__, status);
1010 rpc_exit(task, status);
1014 * 2b. Allocate the buffer. For details, see sched.c:rpc_malloc.
1015 * (Note: buffer memory is freed in xprt_release).
1017 static void
1018 call_allocate(struct rpc_task *task)
1020 unsigned int slack = task->tk_rqstp->rq_cred->cr_auth->au_cslack;
1021 struct rpc_rqst *req = task->tk_rqstp;
1022 struct rpc_xprt *xprt = task->tk_xprt;
1023 struct rpc_procinfo *proc = task->tk_msg.rpc_proc;
1025 dprint_status(task);
1027 task->tk_status = 0;
1028 task->tk_action = call_bind;
1030 if (req->rq_buffer)
1031 return;
1033 if (proc->p_proc != 0) {
1034 BUG_ON(proc->p_arglen == 0);
1035 if (proc->p_decode != NULL)
1036 BUG_ON(proc->p_replen == 0);
1040 * Calculate the size (in quads) of the RPC call
1041 * and reply headers, and convert both values
1042 * to byte sizes.
1044 req->rq_callsize = RPC_CALLHDRSIZE + (slack << 1) + proc->p_arglen;
1045 req->rq_callsize <<= 2;
1046 req->rq_rcvsize = RPC_REPHDRSIZE + slack + proc->p_replen;
1047 req->rq_rcvsize <<= 2;
1049 req->rq_buffer = xprt->ops->buf_alloc(task,
1050 req->rq_callsize + req->rq_rcvsize);
1051 if (req->rq_buffer != NULL)
1052 return;
1054 dprintk("RPC: %5u rpc_buffer allocation failed\n", task->tk_pid);
1056 if (RPC_IS_ASYNC(task) || !signalled()) {
1057 task->tk_action = call_allocate;
1058 rpc_delay(task, HZ>>4);
1059 return;
1062 rpc_exit(task, -ERESTARTSYS);
1065 static inline int
1066 rpc_task_need_encode(struct rpc_task *task)
1068 return task->tk_rqstp->rq_snd_buf.len == 0;
1071 static inline void
1072 rpc_task_force_reencode(struct rpc_task *task)
1074 task->tk_rqstp->rq_snd_buf.len = 0;
1075 task->tk_rqstp->rq_bytes_sent = 0;
1078 static inline void
1079 rpc_xdr_buf_init(struct xdr_buf *buf, void *start, size_t len)
1081 buf->head[0].iov_base = start;
1082 buf->head[0].iov_len = len;
1083 buf->tail[0].iov_len = 0;
1084 buf->page_len = 0;
1085 buf->flags = 0;
1086 buf->len = 0;
1087 buf->buflen = len;
1091 * 3. Encode arguments of an RPC call
1093 static void
1094 rpc_xdr_encode(struct rpc_task *task)
1096 struct rpc_rqst *req = task->tk_rqstp;
1097 kxdreproc_t encode;
1098 __be32 *p;
1100 dprint_status(task);
1102 rpc_xdr_buf_init(&req->rq_snd_buf,
1103 req->rq_buffer,
1104 req->rq_callsize);
1105 rpc_xdr_buf_init(&req->rq_rcv_buf,
1106 (char *)req->rq_buffer + req->rq_callsize,
1107 req->rq_rcvsize);
1109 p = rpc_encode_header(task);
1110 if (p == NULL) {
1111 printk(KERN_INFO "RPC: couldn't encode RPC header, exit EIO\n");
1112 rpc_exit(task, -EIO);
1113 return;
1116 encode = task->tk_msg.rpc_proc->p_encode;
1117 if (encode == NULL)
1118 return;
1120 task->tk_status = rpcauth_wrap_req(task, encode, req, p,
1121 task->tk_msg.rpc_argp);
1125 * 4. Get the server port number if not yet set
1127 static void
1128 call_bind(struct rpc_task *task)
1130 struct rpc_xprt *xprt = task->tk_xprt;
1132 dprint_status(task);
1134 task->tk_action = call_connect;
1135 if (!xprt_bound(xprt)) {
1136 task->tk_action = call_bind_status;
1137 task->tk_timeout = xprt->bind_timeout;
1138 xprt->ops->rpcbind(task);
1143 * 4a. Sort out bind result
1145 static void
1146 call_bind_status(struct rpc_task *task)
1148 int status = -EIO;
1150 if (task->tk_status >= 0) {
1151 dprint_status(task);
1152 task->tk_status = 0;
1153 task->tk_action = call_connect;
1154 return;
1157 switch (task->tk_status) {
1158 case -ENOMEM:
1159 dprintk("RPC: %5u rpcbind out of memory\n", task->tk_pid);
1160 rpc_delay(task, HZ >> 2);
1161 goto retry_timeout;
1162 case -EACCES:
1163 dprintk("RPC: %5u remote rpcbind: RPC program/version "
1164 "unavailable\n", task->tk_pid);
1165 /* fail immediately if this is an RPC ping */
1166 if (task->tk_msg.rpc_proc->p_proc == 0) {
1167 status = -EOPNOTSUPP;
1168 break;
1170 rpc_delay(task, 3*HZ);
1171 goto retry_timeout;
1172 case -ETIMEDOUT:
1173 dprintk("RPC: %5u rpcbind request timed out\n",
1174 task->tk_pid);
1175 goto retry_timeout;
1176 case -EPFNOSUPPORT:
1177 /* server doesn't support any rpcbind version we know of */
1178 dprintk("RPC: %5u unrecognized remote rpcbind service\n",
1179 task->tk_pid);
1180 break;
1181 case -EPROTONOSUPPORT:
1182 dprintk("RPC: %5u remote rpcbind version unavailable, retrying\n",
1183 task->tk_pid);
1184 task->tk_status = 0;
1185 task->tk_action = call_bind;
1186 return;
1187 case -ECONNREFUSED: /* connection problems */
1188 case -ECONNRESET:
1189 case -ENOTCONN:
1190 case -EHOSTDOWN:
1191 case -EHOSTUNREACH:
1192 case -ENETUNREACH:
1193 case -EPIPE:
1194 dprintk("RPC: %5u remote rpcbind unreachable: %d\n",
1195 task->tk_pid, task->tk_status);
1196 if (!RPC_IS_SOFTCONN(task)) {
1197 rpc_delay(task, 5*HZ);
1198 goto retry_timeout;
1200 status = task->tk_status;
1201 break;
1202 default:
1203 dprintk("RPC: %5u unrecognized rpcbind error (%d)\n",
1204 task->tk_pid, -task->tk_status);
1207 rpc_exit(task, status);
1208 return;
1210 retry_timeout:
1211 task->tk_action = call_timeout;
1215 * 4b. Connect to the RPC server
1217 static void
1218 call_connect(struct rpc_task *task)
1220 struct rpc_xprt *xprt = task->tk_xprt;
1222 dprintk("RPC: %5u call_connect xprt %p %s connected\n",
1223 task->tk_pid, xprt,
1224 (xprt_connected(xprt) ? "is" : "is not"));
1226 task->tk_action = call_transmit;
1227 if (!xprt_connected(xprt)) {
1228 task->tk_action = call_connect_status;
1229 if (task->tk_status < 0)
1230 return;
1231 xprt_connect(task);
1236 * 4c. Sort out connect result
1238 static void
1239 call_connect_status(struct rpc_task *task)
1241 struct rpc_clnt *clnt = task->tk_client;
1242 int status = task->tk_status;
1244 dprint_status(task);
1246 task->tk_status = 0;
1247 if (status >= 0 || status == -EAGAIN) {
1248 clnt->cl_stats->netreconn++;
1249 task->tk_action = call_transmit;
1250 return;
1253 switch (status) {
1254 /* if soft mounted, test if we've timed out */
1255 case -ETIMEDOUT:
1256 task->tk_action = call_timeout;
1257 break;
1258 default:
1259 rpc_exit(task, -EIO);
1264 * 5. Transmit the RPC request, and wait for reply
1266 static void
1267 call_transmit(struct rpc_task *task)
1269 dprint_status(task);
1271 task->tk_action = call_status;
1272 if (task->tk_status < 0)
1273 return;
1274 task->tk_status = xprt_prepare_transmit(task);
1275 if (task->tk_status != 0)
1276 return;
1277 task->tk_action = call_transmit_status;
1278 /* Encode here so that rpcsec_gss can use correct sequence number. */
1279 if (rpc_task_need_encode(task)) {
1280 BUG_ON(task->tk_rqstp->rq_bytes_sent != 0);
1281 rpc_xdr_encode(task);
1282 /* Did the encode result in an error condition? */
1283 if (task->tk_status != 0) {
1284 /* Was the error nonfatal? */
1285 if (task->tk_status == -EAGAIN)
1286 rpc_delay(task, HZ >> 4);
1287 else
1288 rpc_exit(task, task->tk_status);
1289 return;
1292 xprt_transmit(task);
1293 if (task->tk_status < 0)
1294 return;
1296 * On success, ensure that we call xprt_end_transmit() before sleeping
1297 * in order to allow access to the socket to other RPC requests.
1299 call_transmit_status(task);
1300 if (rpc_reply_expected(task))
1301 return;
1302 task->tk_action = rpc_exit_task;
1303 rpc_wake_up_queued_task(&task->tk_xprt->pending, task);
1307 * 5a. Handle cleanup after a transmission
1309 static void
1310 call_transmit_status(struct rpc_task *task)
1312 task->tk_action = call_status;
1315 * Common case: success. Force the compiler to put this
1316 * test first.
1318 if (task->tk_status == 0) {
1319 xprt_end_transmit(task);
1320 rpc_task_force_reencode(task);
1321 return;
1324 switch (task->tk_status) {
1325 case -EAGAIN:
1326 break;
1327 default:
1328 dprint_status(task);
1329 xprt_end_transmit(task);
1330 rpc_task_force_reencode(task);
1331 break;
1333 * Special cases: if we've been waiting on the
1334 * socket's write_space() callback, or if the
1335 * socket just returned a connection error,
1336 * then hold onto the transport lock.
1338 case -ECONNREFUSED:
1339 case -EHOSTDOWN:
1340 case -EHOSTUNREACH:
1341 case -ENETUNREACH:
1342 if (RPC_IS_SOFTCONN(task)) {
1343 xprt_end_transmit(task);
1344 rpc_exit(task, task->tk_status);
1345 break;
1347 case -ECONNRESET:
1348 case -ENOTCONN:
1349 case -EPIPE:
1350 rpc_task_force_reencode(task);
1354 #if defined(CONFIG_NFS_V4_1)
1356 * 5b. Send the backchannel RPC reply. On error, drop the reply. In
1357 * addition, disconnect on connectivity errors.
1359 static void
1360 call_bc_transmit(struct rpc_task *task)
1362 struct rpc_rqst *req = task->tk_rqstp;
1364 BUG_ON(task->tk_status != 0);
1365 task->tk_status = xprt_prepare_transmit(task);
1366 if (task->tk_status == -EAGAIN) {
1368 * Could not reserve the transport. Try again after the
1369 * transport is released.
1371 task->tk_status = 0;
1372 task->tk_action = call_bc_transmit;
1373 return;
1376 task->tk_action = rpc_exit_task;
1377 if (task->tk_status < 0) {
1378 printk(KERN_NOTICE "RPC: Could not send backchannel reply "
1379 "error: %d\n", task->tk_status);
1380 return;
1383 xprt_transmit(task);
1384 xprt_end_transmit(task);
1385 dprint_status(task);
1386 switch (task->tk_status) {
1387 case 0:
1388 /* Success */
1389 break;
1390 case -EHOSTDOWN:
1391 case -EHOSTUNREACH:
1392 case -ENETUNREACH:
1393 case -ETIMEDOUT:
1395 * Problem reaching the server. Disconnect and let the
1396 * forechannel reestablish the connection. The server will
1397 * have to retransmit the backchannel request and we'll
1398 * reprocess it. Since these ops are idempotent, there's no
1399 * need to cache our reply at this time.
1401 printk(KERN_NOTICE "RPC: Could not send backchannel reply "
1402 "error: %d\n", task->tk_status);
1403 xprt_conditional_disconnect(task->tk_xprt,
1404 req->rq_connect_cookie);
1405 break;
1406 default:
1408 * We were unable to reply and will have to drop the
1409 * request. The server should reconnect and retransmit.
1411 BUG_ON(task->tk_status == -EAGAIN);
1412 printk(KERN_NOTICE "RPC: Could not send backchannel reply "
1413 "error: %d\n", task->tk_status);
1414 break;
1416 rpc_wake_up_queued_task(&req->rq_xprt->pending, task);
1418 #endif /* CONFIG_NFS_V4_1 */
1421 * 6. Sort out the RPC call status
1423 static void
1424 call_status(struct rpc_task *task)
1426 struct rpc_clnt *clnt = task->tk_client;
1427 struct rpc_rqst *req = task->tk_rqstp;
1428 int status;
1430 if (req->rq_reply_bytes_recvd > 0 && !req->rq_bytes_sent)
1431 task->tk_status = req->rq_reply_bytes_recvd;
1433 dprint_status(task);
1435 status = task->tk_status;
1436 if (status >= 0) {
1437 task->tk_action = call_decode;
1438 return;
1441 task->tk_status = 0;
1442 switch(status) {
1443 case -EHOSTDOWN:
1444 case -EHOSTUNREACH:
1445 case -ENETUNREACH:
1447 * Delay any retries for 3 seconds, then handle as if it
1448 * were a timeout.
1450 rpc_delay(task, 3*HZ);
1451 case -ETIMEDOUT:
1452 task->tk_action = call_timeout;
1453 if (task->tk_client->cl_discrtry)
1454 xprt_conditional_disconnect(task->tk_xprt,
1455 req->rq_connect_cookie);
1456 break;
1457 case -ECONNRESET:
1458 case -ECONNREFUSED:
1459 rpc_force_rebind(clnt);
1460 rpc_delay(task, 3*HZ);
1461 case -EPIPE:
1462 case -ENOTCONN:
1463 task->tk_action = call_bind;
1464 break;
1465 case -EAGAIN:
1466 task->tk_action = call_transmit;
1467 break;
1468 case -EIO:
1469 /* shutdown or soft timeout */
1470 rpc_exit(task, status);
1471 break;
1472 default:
1473 if (clnt->cl_chatty)
1474 printk("%s: RPC call returned error %d\n",
1475 clnt->cl_protname, -status);
1476 rpc_exit(task, status);
1481 * 6a. Handle RPC timeout
1482 * We do not release the request slot, so we keep using the
1483 * same XID for all retransmits.
1485 static void
1486 call_timeout(struct rpc_task *task)
1488 struct rpc_clnt *clnt = task->tk_client;
1490 if (xprt_adjust_timeout(task->tk_rqstp) == 0) {
1491 dprintk("RPC: %5u call_timeout (minor)\n", task->tk_pid);
1492 goto retry;
1495 dprintk("RPC: %5u call_timeout (major)\n", task->tk_pid);
1496 task->tk_timeouts++;
1498 if (RPC_IS_SOFTCONN(task)) {
1499 rpc_exit(task, -ETIMEDOUT);
1500 return;
1502 if (RPC_IS_SOFT(task)) {
1503 if (clnt->cl_chatty)
1504 printk(KERN_NOTICE "%s: server %s not responding, timed out\n",
1505 clnt->cl_protname, clnt->cl_server);
1506 if (task->tk_flags & RPC_TASK_TIMEOUT)
1507 rpc_exit(task, -ETIMEDOUT);
1508 else
1509 rpc_exit(task, -EIO);
1510 return;
1513 if (!(task->tk_flags & RPC_CALL_MAJORSEEN)) {
1514 task->tk_flags |= RPC_CALL_MAJORSEEN;
1515 if (clnt->cl_chatty)
1516 printk(KERN_NOTICE "%s: server %s not responding, still trying\n",
1517 clnt->cl_protname, clnt->cl_server);
1519 rpc_force_rebind(clnt);
1521 * Did our request time out due to an RPCSEC_GSS out-of-sequence
1522 * event? RFC2203 requires the server to drop all such requests.
1524 rpcauth_invalcred(task);
1526 retry:
1527 clnt->cl_stats->rpcretrans++;
1528 task->tk_action = call_bind;
1529 task->tk_status = 0;
1533 * 7. Decode the RPC reply
1535 static void
1536 call_decode(struct rpc_task *task)
1538 struct rpc_clnt *clnt = task->tk_client;
1539 struct rpc_rqst *req = task->tk_rqstp;
1540 kxdrdproc_t decode = task->tk_msg.rpc_proc->p_decode;
1541 __be32 *p;
1543 dprintk("RPC: %5u call_decode (status %d)\n",
1544 task->tk_pid, task->tk_status);
1546 if (task->tk_flags & RPC_CALL_MAJORSEEN) {
1547 if (clnt->cl_chatty)
1548 printk(KERN_NOTICE "%s: server %s OK\n",
1549 clnt->cl_protname, clnt->cl_server);
1550 task->tk_flags &= ~RPC_CALL_MAJORSEEN;
1554 * Ensure that we see all writes made by xprt_complete_rqst()
1555 * before it changed req->rq_reply_bytes_recvd.
1557 smp_rmb();
1558 req->rq_rcv_buf.len = req->rq_private_buf.len;
1560 /* Check that the softirq receive buffer is valid */
1561 WARN_ON(memcmp(&req->rq_rcv_buf, &req->rq_private_buf,
1562 sizeof(req->rq_rcv_buf)) != 0);
1564 if (req->rq_rcv_buf.len < 12) {
1565 if (!RPC_IS_SOFT(task)) {
1566 task->tk_action = call_bind;
1567 clnt->cl_stats->rpcretrans++;
1568 goto out_retry;
1570 dprintk("RPC: %s: too small RPC reply size (%d bytes)\n",
1571 clnt->cl_protname, task->tk_status);
1572 task->tk_action = call_timeout;
1573 goto out_retry;
1576 p = rpc_verify_header(task);
1577 if (IS_ERR(p)) {
1578 if (p == ERR_PTR(-EAGAIN))
1579 goto out_retry;
1580 return;
1583 task->tk_action = rpc_exit_task;
1585 if (decode) {
1586 task->tk_status = rpcauth_unwrap_resp(task, decode, req, p,
1587 task->tk_msg.rpc_resp);
1589 dprintk("RPC: %5u call_decode result %d\n", task->tk_pid,
1590 task->tk_status);
1591 return;
1592 out_retry:
1593 task->tk_status = 0;
1594 /* Note: rpc_verify_header() may have freed the RPC slot */
1595 if (task->tk_rqstp == req) {
1596 req->rq_reply_bytes_recvd = req->rq_rcv_buf.len = 0;
1597 if (task->tk_client->cl_discrtry)
1598 xprt_conditional_disconnect(task->tk_xprt,
1599 req->rq_connect_cookie);
1603 static __be32 *
1604 rpc_encode_header(struct rpc_task *task)
1606 struct rpc_clnt *clnt = task->tk_client;
1607 struct rpc_rqst *req = task->tk_rqstp;
1608 __be32 *p = req->rq_svec[0].iov_base;
1610 /* FIXME: check buffer size? */
1612 p = xprt_skip_transport_header(task->tk_xprt, p);
1613 *p++ = req->rq_xid; /* XID */
1614 *p++ = htonl(RPC_CALL); /* CALL */
1615 *p++ = htonl(RPC_VERSION); /* RPC version */
1616 *p++ = htonl(clnt->cl_prog); /* program number */
1617 *p++ = htonl(clnt->cl_vers); /* program version */
1618 *p++ = htonl(task->tk_msg.rpc_proc->p_proc); /* procedure */
1619 p = rpcauth_marshcred(task, p);
1620 req->rq_slen = xdr_adjust_iovec(&req->rq_svec[0], p);
1621 return p;
1624 static __be32 *
1625 rpc_verify_header(struct rpc_task *task)
1627 struct kvec *iov = &task->tk_rqstp->rq_rcv_buf.head[0];
1628 int len = task->tk_rqstp->rq_rcv_buf.len >> 2;
1629 __be32 *p = iov->iov_base;
1630 u32 n;
1631 int error = -EACCES;
1633 if ((task->tk_rqstp->rq_rcv_buf.len & 3) != 0) {
1634 /* RFC-1014 says that the representation of XDR data must be a
1635 * multiple of four bytes
1636 * - if it isn't pointer subtraction in the NFS client may give
1637 * undefined results
1639 dprintk("RPC: %5u %s: XDR representation not a multiple of"
1640 " 4 bytes: 0x%x\n", task->tk_pid, __func__,
1641 task->tk_rqstp->rq_rcv_buf.len);
1642 goto out_eio;
1644 if ((len -= 3) < 0)
1645 goto out_overflow;
1647 p += 1; /* skip XID */
1648 if ((n = ntohl(*p++)) != RPC_REPLY) {
1649 dprintk("RPC: %5u %s: not an RPC reply: %x\n",
1650 task->tk_pid, __func__, n);
1651 goto out_garbage;
1654 if ((n = ntohl(*p++)) != RPC_MSG_ACCEPTED) {
1655 if (--len < 0)
1656 goto out_overflow;
1657 switch ((n = ntohl(*p++))) {
1658 case RPC_AUTH_ERROR:
1659 break;
1660 case RPC_MISMATCH:
1661 dprintk("RPC: %5u %s: RPC call version "
1662 "mismatch!\n",
1663 task->tk_pid, __func__);
1664 error = -EPROTONOSUPPORT;
1665 goto out_err;
1666 default:
1667 dprintk("RPC: %5u %s: RPC call rejected, "
1668 "unknown error: %x\n",
1669 task->tk_pid, __func__, n);
1670 goto out_eio;
1672 if (--len < 0)
1673 goto out_overflow;
1674 switch ((n = ntohl(*p++))) {
1675 case RPC_AUTH_REJECTEDCRED:
1676 case RPC_AUTH_REJECTEDVERF:
1677 case RPCSEC_GSS_CREDPROBLEM:
1678 case RPCSEC_GSS_CTXPROBLEM:
1679 if (!task->tk_cred_retry)
1680 break;
1681 task->tk_cred_retry--;
1682 dprintk("RPC: %5u %s: retry stale creds\n",
1683 task->tk_pid, __func__);
1684 rpcauth_invalcred(task);
1685 /* Ensure we obtain a new XID! */
1686 xprt_release(task);
1687 task->tk_action = call_reserve;
1688 goto out_retry;
1689 case RPC_AUTH_BADCRED:
1690 case RPC_AUTH_BADVERF:
1691 /* possibly garbled cred/verf? */
1692 if (!task->tk_garb_retry)
1693 break;
1694 task->tk_garb_retry--;
1695 dprintk("RPC: %5u %s: retry garbled creds\n",
1696 task->tk_pid, __func__);
1697 task->tk_action = call_bind;
1698 goto out_retry;
1699 case RPC_AUTH_TOOWEAK:
1700 printk(KERN_NOTICE "RPC: server %s requires stronger "
1701 "authentication.\n", task->tk_client->cl_server);
1702 break;
1703 default:
1704 dprintk("RPC: %5u %s: unknown auth error: %x\n",
1705 task->tk_pid, __func__, n);
1706 error = -EIO;
1708 dprintk("RPC: %5u %s: call rejected %d\n",
1709 task->tk_pid, __func__, n);
1710 goto out_err;
1712 if (!(p = rpcauth_checkverf(task, p))) {
1713 dprintk("RPC: %5u %s: auth check failed\n",
1714 task->tk_pid, __func__);
1715 goto out_garbage; /* bad verifier, retry */
1717 len = p - (__be32 *)iov->iov_base - 1;
1718 if (len < 0)
1719 goto out_overflow;
1720 switch ((n = ntohl(*p++))) {
1721 case RPC_SUCCESS:
1722 return p;
1723 case RPC_PROG_UNAVAIL:
1724 dprintk("RPC: %5u %s: program %u is unsupported by server %s\n",
1725 task->tk_pid, __func__,
1726 (unsigned int)task->tk_client->cl_prog,
1727 task->tk_client->cl_server);
1728 error = -EPFNOSUPPORT;
1729 goto out_err;
1730 case RPC_PROG_MISMATCH:
1731 dprintk("RPC: %5u %s: program %u, version %u unsupported by "
1732 "server %s\n", task->tk_pid, __func__,
1733 (unsigned int)task->tk_client->cl_prog,
1734 (unsigned int)task->tk_client->cl_vers,
1735 task->tk_client->cl_server);
1736 error = -EPROTONOSUPPORT;
1737 goto out_err;
1738 case RPC_PROC_UNAVAIL:
1739 dprintk("RPC: %5u %s: proc %s unsupported by program %u, "
1740 "version %u on server %s\n",
1741 task->tk_pid, __func__,
1742 rpc_proc_name(task),
1743 task->tk_client->cl_prog,
1744 task->tk_client->cl_vers,
1745 task->tk_client->cl_server);
1746 error = -EOPNOTSUPP;
1747 goto out_err;
1748 case RPC_GARBAGE_ARGS:
1749 dprintk("RPC: %5u %s: server saw garbage\n",
1750 task->tk_pid, __func__);
1751 break; /* retry */
1752 default:
1753 dprintk("RPC: %5u %s: server accept status: %x\n",
1754 task->tk_pid, __func__, n);
1755 /* Also retry */
1758 out_garbage:
1759 task->tk_client->cl_stats->rpcgarbage++;
1760 if (task->tk_garb_retry) {
1761 task->tk_garb_retry--;
1762 dprintk("RPC: %5u %s: retrying\n",
1763 task->tk_pid, __func__);
1764 task->tk_action = call_bind;
1765 out_retry:
1766 return ERR_PTR(-EAGAIN);
1768 out_eio:
1769 error = -EIO;
1770 out_err:
1771 rpc_exit(task, error);
1772 dprintk("RPC: %5u %s: call failed with error %d\n", task->tk_pid,
1773 __func__, error);
1774 return ERR_PTR(error);
1775 out_overflow:
1776 dprintk("RPC: %5u %s: server reply was truncated.\n", task->tk_pid,
1777 __func__);
1778 goto out_garbage;
1781 static void rpcproc_encode_null(void *rqstp, struct xdr_stream *xdr, void *obj)
1785 static int rpcproc_decode_null(void *rqstp, struct xdr_stream *xdr, void *obj)
1787 return 0;
1790 static struct rpc_procinfo rpcproc_null = {
1791 .p_encode = rpcproc_encode_null,
1792 .p_decode = rpcproc_decode_null,
1795 static int rpc_ping(struct rpc_clnt *clnt)
1797 struct rpc_message msg = {
1798 .rpc_proc = &rpcproc_null,
1800 int err;
1801 msg.rpc_cred = authnull_ops.lookup_cred(NULL, NULL, 0);
1802 err = rpc_call_sync(clnt, &msg, RPC_TASK_SOFT | RPC_TASK_SOFTCONN);
1803 put_rpccred(msg.rpc_cred);
1804 return err;
1807 struct rpc_task *rpc_call_null(struct rpc_clnt *clnt, struct rpc_cred *cred, int flags)
1809 struct rpc_message msg = {
1810 .rpc_proc = &rpcproc_null,
1811 .rpc_cred = cred,
1813 struct rpc_task_setup task_setup_data = {
1814 .rpc_client = clnt,
1815 .rpc_message = &msg,
1816 .callback_ops = &rpc_default_ops,
1817 .flags = flags,
1819 return rpc_run_task(&task_setup_data);
1821 EXPORT_SYMBOL_GPL(rpc_call_null);
1823 #ifdef RPC_DEBUG
1824 static void rpc_show_header(void)
1826 printk(KERN_INFO "-pid- flgs status -client- --rqstp- "
1827 "-timeout ---ops--\n");
1830 static void rpc_show_task(const struct rpc_clnt *clnt,
1831 const struct rpc_task *task)
1833 const char *rpc_waitq = "none";
1835 if (RPC_IS_QUEUED(task))
1836 rpc_waitq = rpc_qname(task->tk_waitqueue);
1838 printk(KERN_INFO "%5u %04x %6d %8p %8p %8ld %8p %sv%u %s a:%ps q:%s\n",
1839 task->tk_pid, task->tk_flags, task->tk_status,
1840 clnt, task->tk_rqstp, task->tk_timeout, task->tk_ops,
1841 clnt->cl_protname, clnt->cl_vers, rpc_proc_name(task),
1842 task->tk_action, rpc_waitq);
1845 void rpc_show_tasks(void)
1847 struct rpc_clnt *clnt;
1848 struct rpc_task *task;
1849 int header = 0;
1851 spin_lock(&rpc_client_lock);
1852 list_for_each_entry(clnt, &all_clients, cl_clients) {
1853 spin_lock(&clnt->cl_lock);
1854 list_for_each_entry(task, &clnt->cl_tasks, tk_task) {
1855 if (!header) {
1856 rpc_show_header();
1857 header++;
1859 rpc_show_task(clnt, task);
1861 spin_unlock(&clnt->cl_lock);
1863 spin_unlock(&rpc_client_lock);
1865 #endif