blob: 8b78177e75751abed41c1d48460ca44c08e9ce42 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Chuck Lever55aa4f52005-08-11 16:25:47 -04002 * linux/net/sunrpc/clnt.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
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 *
16 * NB: BSD uses a more intelligent approach to guessing when a request
17 * or reply has been lost by keeping the RTO estimate for each procedure.
18 * We currently make do with a constant timeout value.
19 *
20 * Copyright (C) 1992,1993 Rick Sladkey <jrs@world.std.com>
21 * Copyright (C) 1995,1996 Olaf Kirch <okir@monad.swb.de>
22 */
23
24#include <asm/system.h>
25
26#include <linux/module.h>
27#include <linux/types.h>
28#include <linux/mm.h>
29#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/utsname.h>
Chuck Lever11c556b2006-03-20 13:44:22 -050031#include <linux/workqueue.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33#include <linux/sunrpc/clnt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/sunrpc/rpc_pipe_fs.h>
Chuck Lever11c556b2006-03-20 13:44:22 -050035#include <linux/sunrpc/metrics.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37
38#define RPC_SLACK_SPACE (1024) /* total overkill */
39
40#ifdef RPC_DEBUG
41# define RPCDBG_FACILITY RPCDBG_CALL
42#endif
43
44static DECLARE_WAIT_QUEUE_HEAD(destroy_wait);
45
46
47static void call_start(struct rpc_task *task);
48static void call_reserve(struct rpc_task *task);
49static void call_reserveresult(struct rpc_task *task);
50static void call_allocate(struct rpc_task *task);
51static void call_encode(struct rpc_task *task);
52static void call_decode(struct rpc_task *task);
53static void call_bind(struct rpc_task *task);
Chuck Leverda351872005-08-11 16:25:11 -040054static void call_bind_status(struct rpc_task *task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055static void call_transmit(struct rpc_task *task);
56static void call_status(struct rpc_task *task);
Trond Myklebust940e3312005-11-09 21:45:24 -050057static void call_transmit_status(struct rpc_task *task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058static void call_refresh(struct rpc_task *task);
59static void call_refreshresult(struct rpc_task *task);
60static void call_timeout(struct rpc_task *task);
61static void call_connect(struct rpc_task *task);
62static void call_connect_status(struct rpc_task *task);
Alexey Dobriyand8ed0292006-09-26 22:29:38 -070063static __be32 * call_header(struct rpc_task *task);
64static __be32 * call_verify(struct rpc_task *task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66
67static int
68rpc_setup_pipedir(struct rpc_clnt *clnt, char *dir_name)
69{
Trond Myklebustf1345852005-09-23 11:08:25 -040070 static uint32_t clntid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 int error;
72
Trond Myklebust54281542006-03-20 13:44:49 -050073 clnt->cl_vfsmnt = ERR_PTR(-ENOENT);
74 clnt->cl_dentry = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 if (dir_name == NULL)
76 return 0;
Trond Myklebust54281542006-03-20 13:44:49 -050077
78 clnt->cl_vfsmnt = rpc_get_mount();
79 if (IS_ERR(clnt->cl_vfsmnt))
80 return PTR_ERR(clnt->cl_vfsmnt);
81
Trond Myklebustf1345852005-09-23 11:08:25 -040082 for (;;) {
83 snprintf(clnt->cl_pathname, sizeof(clnt->cl_pathname),
84 "%s/clnt%x", dir_name,
85 (unsigned int)clntid++);
86 clnt->cl_pathname[sizeof(clnt->cl_pathname) - 1] = '\0';
87 clnt->cl_dentry = rpc_mkdir(clnt->cl_pathname, clnt);
88 if (!IS_ERR(clnt->cl_dentry))
89 return 0;
Christoph Hellwig278c9952005-07-24 23:53:01 +010090 error = PTR_ERR(clnt->cl_dentry);
Trond Myklebustf1345852005-09-23 11:08:25 -040091 if (error != -EEXIST) {
92 printk(KERN_INFO "RPC: Couldn't create pipefs entry %s, error %d\n",
93 clnt->cl_pathname, error);
Trond Myklebust54281542006-03-20 13:44:49 -050094 rpc_put_mount();
Trond Myklebustf1345852005-09-23 11:08:25 -040095 return error;
96 }
Christoph Hellwig278c9952005-07-24 23:53:01 +010097 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070098}
99
Chuck Leverff9aa5e2006-08-22 20:06:21 -0400100static struct rpc_clnt * rpc_new_client(struct rpc_xprt *xprt, char *servname, struct rpc_program *program, u32 vers, rpc_authflavor_t flavor)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101{
102 struct rpc_version *version;
103 struct rpc_clnt *clnt = NULL;
J. Bruce Fields6a192752005-06-22 17:16:23 +0000104 struct rpc_auth *auth;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 int err;
106 int len;
107
108 dprintk("RPC: creating %s client for %s (xprt %p)\n",
109 program->name, servname, xprt);
110
111 err = -EINVAL;
112 if (!xprt)
Adrian Bunk712917d2006-03-13 21:20:47 -0800113 goto out_no_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 if (vers >= program->nrvers || !(version = program->version[vers]))
115 goto out_err;
116
117 err = -ENOMEM;
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700118 clnt = kzalloc(sizeof(*clnt), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 if (!clnt)
120 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 atomic_set(&clnt->cl_users, 0);
122 atomic_set(&clnt->cl_count, 1);
123 clnt->cl_parent = clnt;
124
125 clnt->cl_server = clnt->cl_inline_name;
126 len = strlen(servname) + 1;
127 if (len > sizeof(clnt->cl_inline_name)) {
128 char *buf = kmalloc(len, GFP_KERNEL);
129 if (buf != 0)
130 clnt->cl_server = buf;
131 else
132 len = sizeof(clnt->cl_inline_name);
133 }
134 strlcpy(clnt->cl_server, servname, len);
135
136 clnt->cl_xprt = xprt;
137 clnt->cl_procinfo = version->procs;
138 clnt->cl_maxproc = version->nrprocs;
139 clnt->cl_protname = program->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 clnt->cl_prog = program->number;
141 clnt->cl_vers = version->number;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 clnt->cl_stats = program->stats;
Chuck Lever11c556b2006-03-20 13:44:22 -0500143 clnt->cl_metrics = rpc_alloc_iostats(clnt);
Trond Myklebust23bf85b2006-11-21 10:40:23 -0500144 err = -ENOMEM;
145 if (clnt->cl_metrics == NULL)
146 goto out_no_stats;
Trond Myklebust3e32a5d2006-11-16 11:37:27 -0500147 clnt->cl_program = program;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Chuck Leverec739ef2006-08-22 20:06:15 -0400149 if (!xprt_bound(clnt->cl_xprt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 clnt->cl_autobind = 1;
151
152 clnt->cl_rtt = &clnt->cl_rtt_default;
153 rpc_init_rtt(&clnt->cl_rtt_default, xprt->timeout.to_initval);
154
155 err = rpc_setup_pipedir(clnt, program->pipe_dir_name);
156 if (err < 0)
157 goto out_no_path;
158
J. Bruce Fields6a192752005-06-22 17:16:23 +0000159 auth = rpcauth_create(flavor, clnt);
160 if (IS_ERR(auth)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 printk(KERN_INFO "RPC: Couldn't create auth handle (flavor %u)\n",
162 flavor);
J. Bruce Fields6a192752005-06-22 17:16:23 +0000163 err = PTR_ERR(auth);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 goto out_no_auth;
165 }
166
167 /* save the nodename */
Serge E. Hallyne9ff3992006-10-02 02:18:11 -0700168 clnt->cl_nodelen = strlen(utsname()->nodename);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 if (clnt->cl_nodelen > UNX_MAXNODENAME)
170 clnt->cl_nodelen = UNX_MAXNODENAME;
Serge E. Hallyne9ff3992006-10-02 02:18:11 -0700171 memcpy(clnt->cl_nodename, utsname()->nodename, clnt->cl_nodelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 return clnt;
173
174out_no_auth:
Trond Myklebust54281542006-03-20 13:44:49 -0500175 if (!IS_ERR(clnt->cl_dentry)) {
Trond Myklebustdff02cc2006-07-31 14:17:18 -0700176 rpc_rmdir(clnt->cl_dentry);
Trond Myklebust54281542006-03-20 13:44:49 -0500177 rpc_put_mount();
178 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179out_no_path:
Trond Myklebust23bf85b2006-11-21 10:40:23 -0500180 rpc_free_iostats(clnt->cl_metrics);
181out_no_stats:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 if (clnt->cl_server != clnt->cl_inline_name)
183 kfree(clnt->cl_server);
184 kfree(clnt);
185out_err:
Trond Myklebust6b6ca862006-09-05 12:55:57 -0400186 xprt_put(xprt);
Adrian Bunk712917d2006-03-13 21:20:47 -0800187out_no_xprt:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 return ERR_PTR(err);
189}
190
Chuck Leverc2866762006-08-22 20:06:20 -0400191/*
192 * rpc_create - create an RPC client and transport with one call
193 * @args: rpc_clnt create argument structure
194 *
195 * Creates and initializes an RPC transport and an RPC client.
196 *
197 * It can ping the server in order to determine if it is up, and to see if
198 * it supports this program and version. RPC_CLNT_CREATE_NOPING disables
199 * this behavior so asynchronous tasks can also use rpc_create.
200 */
201struct rpc_clnt *rpc_create(struct rpc_create_args *args)
202{
203 struct rpc_xprt *xprt;
204 struct rpc_clnt *clnt;
205
206 xprt = xprt_create_transport(args->protocol, args->address,
207 args->addrsize, args->timeout);
208 if (IS_ERR(xprt))
209 return (struct rpc_clnt *)xprt;
210
211 /*
212 * By default, kernel RPC client connects from a reserved port.
213 * CAP_NET_BIND_SERVICE will not be set for unprivileged requesters,
214 * but it is always enabled for rpciod, which handles the connect
215 * operation.
216 */
217 xprt->resvport = 1;
218 if (args->flags & RPC_CLNT_CREATE_NONPRIVPORT)
219 xprt->resvport = 0;
220
221 dprintk("RPC: creating %s client for %s (xprt %p)\n",
222 args->program->name, args->servername, xprt);
223
224 clnt = rpc_new_client(xprt, args->servername, args->program,
225 args->version, args->authflavor);
226 if (IS_ERR(clnt))
227 return clnt;
228
229 if (!(args->flags & RPC_CLNT_CREATE_NOPING)) {
230 int err = rpc_ping(clnt, RPC_TASK_SOFT|RPC_TASK_NOINTR);
231 if (err != 0) {
232 rpc_shutdown_client(clnt);
233 return ERR_PTR(err);
234 }
235 }
236
237 clnt->cl_softrtry = 1;
238 if (args->flags & RPC_CLNT_CREATE_HARDRTRY)
239 clnt->cl_softrtry = 0;
240
241 if (args->flags & RPC_CLNT_CREATE_INTR)
242 clnt->cl_intr = 1;
243 if (args->flags & RPC_CLNT_CREATE_AUTOBIND)
244 clnt->cl_autobind = 1;
245 if (args->flags & RPC_CLNT_CREATE_ONESHOT)
246 clnt->cl_oneshot = 1;
247
248 return clnt;
249}
Chuck Leverb86acd52006-08-22 20:06:22 -0400250EXPORT_SYMBOL_GPL(rpc_create);
Chuck Leverc2866762006-08-22 20:06:20 -0400251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252/*
253 * This function clones the RPC client structure. It allows us to share the
254 * same transport while varying parameters such as the authentication
255 * flavour.
256 */
257struct rpc_clnt *
258rpc_clone_client(struct rpc_clnt *clnt)
259{
260 struct rpc_clnt *new;
Trond Myklebust3e32a5d2006-11-16 11:37:27 -0500261 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Arnaldo Carvalho de Meloe69062b2006-11-21 01:21:34 -0200263 new = kmemdup(clnt, sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 if (!new)
265 goto out_no_clnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 atomic_set(&new->cl_count, 1);
267 atomic_set(&new->cl_users, 0);
Trond Myklebust23bf85b2006-11-21 10:40:23 -0500268 new->cl_metrics = rpc_alloc_iostats(clnt);
269 if (new->cl_metrics == NULL)
270 goto out_no_stats;
Trond Myklebust3e32a5d2006-11-16 11:37:27 -0500271 err = rpc_setup_pipedir(new, clnt->cl_program->pipe_dir_name);
272 if (err != 0)
273 goto out_no_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 new->cl_parent = clnt;
275 atomic_inc(&clnt->cl_count);
Trond Myklebust6b6ca862006-09-05 12:55:57 -0400276 new->cl_xprt = xprt_get(clnt->cl_xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 /* Turn off autobind on clones */
278 new->cl_autobind = 0;
279 new->cl_oneshot = 0;
280 new->cl_dead = 0;
281 rpc_init_rtt(&new->cl_rtt_default, clnt->cl_xprt->timeout.to_initval);
282 if (new->cl_auth)
283 atomic_inc(&new->cl_auth->au_count);
284 return new;
Trond Myklebust3e32a5d2006-11-16 11:37:27 -0500285out_no_path:
286 rpc_free_iostats(new->cl_metrics);
Trond Myklebust23bf85b2006-11-21 10:40:23 -0500287out_no_stats:
288 kfree(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289out_no_clnt:
Trond Myklebust3e32a5d2006-11-16 11:37:27 -0500290 dprintk("RPC: %s returned error %d\n", __FUNCTION__, err);
291 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292}
293
294/*
295 * Properly shut down an RPC client, terminating all outstanding
296 * requests. Note that we must be certain that cl_oneshot and
297 * cl_dead are cleared, or else the client would be destroyed
298 * when the last task releases it.
299 */
300int
301rpc_shutdown_client(struct rpc_clnt *clnt)
302{
303 dprintk("RPC: shutting down %s client for %s, tasks=%d\n",
304 clnt->cl_protname, clnt->cl_server,
305 atomic_read(&clnt->cl_users));
306
307 while (atomic_read(&clnt->cl_users) > 0) {
308 /* Don't let rpc_release_client destroy us */
309 clnt->cl_oneshot = 0;
310 clnt->cl_dead = 0;
311 rpc_killall_tasks(clnt);
Ingo Molnar532347e2006-01-09 20:52:53 -0800312 wait_event_timeout(destroy_wait,
Linus Torvalds4f477072006-01-10 08:56:39 -0800313 !atomic_read(&clnt->cl_users), 1*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 }
315
316 if (atomic_read(&clnt->cl_users) < 0) {
317 printk(KERN_ERR "RPC: rpc_shutdown_client clnt %p tasks=%d\n",
318 clnt, atomic_read(&clnt->cl_users));
319#ifdef RPC_DEBUG
320 rpc_show_tasks();
321#endif
322 BUG();
323 }
324
325 return rpc_destroy_client(clnt);
326}
327
328/*
329 * Delete an RPC client
330 */
331int
332rpc_destroy_client(struct rpc_clnt *clnt)
333{
334 if (!atomic_dec_and_test(&clnt->cl_count))
335 return 1;
336 BUG_ON(atomic_read(&clnt->cl_users) != 0);
337
338 dprintk("RPC: destroying %s client for %s\n",
339 clnt->cl_protname, clnt->cl_server);
340 if (clnt->cl_auth) {
341 rpcauth_destroy(clnt->cl_auth);
342 clnt->cl_auth = NULL;
343 }
Trond Myklebust8f8e7a52006-08-14 13:11:15 -0400344 if (!IS_ERR(clnt->cl_dentry)) {
Trond Myklebustdff02cc2006-07-31 14:17:18 -0700345 rpc_rmdir(clnt->cl_dentry);
Trond Myklebust8f8e7a52006-08-14 13:11:15 -0400346 rpc_put_mount();
347 }
Trond Myklebust3e32a5d2006-11-16 11:37:27 -0500348 if (clnt->cl_parent != clnt) {
349 rpc_destroy_client(clnt->cl_parent);
350 goto out_free;
351 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 if (clnt->cl_server != clnt->cl_inline_name)
353 kfree(clnt->cl_server);
354out_free:
Chuck Lever11c556b2006-03-20 13:44:22 -0500355 rpc_free_iostats(clnt->cl_metrics);
356 clnt->cl_metrics = NULL;
Trond Myklebust6b6ca862006-09-05 12:55:57 -0400357 xprt_put(clnt->cl_xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 kfree(clnt);
359 return 0;
360}
361
362/*
363 * Release an RPC client
364 */
365void
366rpc_release_client(struct rpc_clnt *clnt)
367{
368 dprintk("RPC: rpc_release_client(%p, %d)\n",
369 clnt, atomic_read(&clnt->cl_users));
370
371 if (!atomic_dec_and_test(&clnt->cl_users))
372 return;
373 wake_up(&destroy_wait);
374 if (clnt->cl_oneshot || clnt->cl_dead)
375 rpc_destroy_client(clnt);
376}
377
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000378/**
379 * rpc_bind_new_program - bind a new RPC program to an existing client
380 * @old - old rpc_client
381 * @program - rpc program to set
382 * @vers - rpc program version
383 *
384 * Clones the rpc client and sets up a new RPC program. This is mainly
385 * of use for enabling different RPC programs to share the same transport.
386 * The Sun NFSv2/v3 ACL protocol can do this.
387 */
388struct rpc_clnt *rpc_bind_new_program(struct rpc_clnt *old,
389 struct rpc_program *program,
390 int vers)
391{
392 struct rpc_clnt *clnt;
393 struct rpc_version *version;
394 int err;
395
396 BUG_ON(vers >= program->nrvers || !program->version[vers]);
397 version = program->version[vers];
398 clnt = rpc_clone_client(old);
399 if (IS_ERR(clnt))
400 goto out;
401 clnt->cl_procinfo = version->procs;
402 clnt->cl_maxproc = version->nrprocs;
403 clnt->cl_protname = program->name;
404 clnt->cl_prog = program->number;
405 clnt->cl_vers = version->number;
406 clnt->cl_stats = program->stats;
407 err = rpc_ping(clnt, RPC_TASK_SOFT|RPC_TASK_NOINTR);
408 if (err != 0) {
409 rpc_shutdown_client(clnt);
410 clnt = ERR_PTR(err);
411 }
412out:
413 return clnt;
414}
415
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416/*
417 * Default callback for async RPC calls
418 */
419static void
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100420rpc_default_callback(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{
422}
423
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100424static const struct rpc_call_ops rpc_default_ops = {
425 .rpc_call_done = rpc_default_callback,
426};
427
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428/*
Trond Myklebust14b218a2005-06-22 17:16:28 +0000429 * Export the signal mask handling for synchronous code that
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 * sleeps on RPC calls
431 */
Trond Myklebust2bd61572006-01-03 09:55:19 +0100432#define RPC_INTR_SIGNALS (sigmask(SIGHUP) | sigmask(SIGINT) | sigmask(SIGQUIT) | sigmask(SIGTERM))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
Trond Myklebust14b218a2005-06-22 17:16:28 +0000434static void rpc_save_sigmask(sigset_t *oldset, int intr)
435{
Trond Myklebust2bd61572006-01-03 09:55:19 +0100436 unsigned long sigallow = sigmask(SIGKILL);
Trond Myklebust14b218a2005-06-22 17:16:28 +0000437 sigset_t sigmask;
438
439 /* Block all signals except those listed in sigallow */
440 if (intr)
441 sigallow |= RPC_INTR_SIGNALS;
442 siginitsetinv(&sigmask, sigallow);
443 sigprocmask(SIG_BLOCK, &sigmask, oldset);
444}
445
446static inline void rpc_task_sigmask(struct rpc_task *task, sigset_t *oldset)
447{
448 rpc_save_sigmask(oldset, !RPC_TASK_UNINTERRUPTIBLE(task));
449}
450
451static inline void rpc_restore_sigmask(sigset_t *oldset)
452{
453 sigprocmask(SIG_SETMASK, oldset, NULL);
454}
455
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456void rpc_clnt_sigmask(struct rpc_clnt *clnt, sigset_t *oldset)
457{
Trond Myklebust14b218a2005-06-22 17:16:28 +0000458 rpc_save_sigmask(oldset, clnt->cl_intr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459}
460
461void rpc_clnt_sigunmask(struct rpc_clnt *clnt, sigset_t *oldset)
462{
Trond Myklebust14b218a2005-06-22 17:16:28 +0000463 rpc_restore_sigmask(oldset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464}
465
466/*
467 * New rpc_call implementation
468 */
469int rpc_call_sync(struct rpc_clnt *clnt, struct rpc_message *msg, int flags)
470{
471 struct rpc_task *task;
472 sigset_t oldset;
473 int status;
474
475 /* If this client is slain all further I/O fails */
476 if (clnt->cl_dead)
477 return -EIO;
478
479 BUG_ON(flags & RPC_TASK_ASYNC);
480
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100481 task = rpc_new_task(clnt, flags, &rpc_default_ops, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 if (task == NULL)
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -0500483 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Trond Myklebust14b218a2005-06-22 17:16:28 +0000485 /* Mask signals on RPC calls _and_ GSS_AUTH upcalls */
486 rpc_task_sigmask(task, &oldset);
487
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 rpc_call_setup(task, msg, 0);
489
490 /* Set up the call info struct and execute the task */
Trond Myklebuste60859a2006-01-03 09:55:10 +0100491 status = task->tk_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -0500492 if (status != 0) {
493 rpc_release_task(task);
494 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 }
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -0500496 atomic_inc(&task->tk_count);
497 status = rpc_execute(task);
498 if (status == 0)
499 status = task->tk_status;
500 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -0500502 rpc_restore_sigmask(&oldset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 return status;
504}
505
506/*
507 * New rpc_call implementation
508 */
509int
510rpc_call_async(struct rpc_clnt *clnt, struct rpc_message *msg, int flags,
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100511 const struct rpc_call_ops *tk_ops, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512{
513 struct rpc_task *task;
514 sigset_t oldset;
515 int status;
516
517 /* If this client is slain all further I/O fails */
Trond Myklebust7a1218a2006-03-20 18:11:10 -0500518 status = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 if (clnt->cl_dead)
Trond Myklebust7a1218a2006-03-20 18:11:10 -0500520 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
522 flags |= RPC_TASK_ASYNC;
523
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 /* Create/initialize a new RPC task */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 status = -ENOMEM;
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100526 if (!(task = rpc_new_task(clnt, flags, tk_ops, data)))
Trond Myklebust7a1218a2006-03-20 18:11:10 -0500527 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
Trond Myklebust14b218a2005-06-22 17:16:28 +0000529 /* Mask signals on GSS_AUTH upcalls */
530 rpc_task_sigmask(task, &oldset);
531
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 rpc_call_setup(task, msg, 0);
533
534 /* Set up the call info struct and execute the task */
535 status = task->tk_status;
536 if (status == 0)
537 rpc_execute(task);
538 else
539 rpc_release_task(task);
540
Trond Myklebust14b218a2005-06-22 17:16:28 +0000541 rpc_restore_sigmask(&oldset);
Trond Myklebust7a1218a2006-03-20 18:11:10 -0500542 return status;
543out_release:
544 if (tk_ops->rpc_release != NULL)
545 tk_ops->rpc_release(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 return status;
547}
548
549
550void
551rpc_call_setup(struct rpc_task *task, struct rpc_message *msg, int flags)
552{
553 task->tk_msg = *msg;
554 task->tk_flags |= flags;
555 /* Bind the user cred */
556 if (task->tk_msg.rpc_cred != NULL)
557 rpcauth_holdcred(task);
558 else
559 rpcauth_bindcred(task);
560
561 if (task->tk_status == 0)
562 task->tk_action = call_start;
563 else
Trond Myklebustabbcf282006-01-03 09:55:03 +0100564 task->tk_action = rpc_exit_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565}
566
Chuck Levered394402006-08-22 20:06:17 -0400567/**
568 * rpc_peeraddr - extract remote peer address from clnt's xprt
569 * @clnt: RPC client structure
570 * @buf: target buffer
571 * @size: length of target buffer
572 *
573 * Returns the number of bytes that are actually in the stored address.
574 */
575size_t rpc_peeraddr(struct rpc_clnt *clnt, struct sockaddr *buf, size_t bufsize)
576{
577 size_t bytes;
578 struct rpc_xprt *xprt = clnt->cl_xprt;
579
580 bytes = sizeof(xprt->addr);
581 if (bytes > bufsize)
582 bytes = bufsize;
583 memcpy(buf, &clnt->cl_xprt->addr, bytes);
Chuck Leverc4efcb12006-08-22 20:06:19 -0400584 return xprt->addrlen;
Chuck Levered394402006-08-22 20:06:17 -0400585}
Chuck Leverb86acd52006-08-22 20:06:22 -0400586EXPORT_SYMBOL_GPL(rpc_peeraddr);
Chuck Levered394402006-08-22 20:06:17 -0400587
Chuck Leverf425eba2006-08-22 20:06:18 -0400588/**
589 * rpc_peeraddr2str - return remote peer address in printable format
590 * @clnt: RPC client structure
591 * @format: address format
592 *
593 */
594char *rpc_peeraddr2str(struct rpc_clnt *clnt, enum rpc_display_format_t format)
595{
596 struct rpc_xprt *xprt = clnt->cl_xprt;
597 return xprt->ops->print_addr(xprt, format);
598}
Chuck Leverb86acd52006-08-22 20:06:22 -0400599EXPORT_SYMBOL_GPL(rpc_peeraddr2str);
Chuck Leverf425eba2006-08-22 20:06:18 -0400600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601void
602rpc_setbufsize(struct rpc_clnt *clnt, unsigned int sndsize, unsigned int rcvsize)
603{
604 struct rpc_xprt *xprt = clnt->cl_xprt;
Chuck Lever470056c2005-08-25 16:25:56 -0700605 if (xprt->ops->set_buffer_size)
606 xprt->ops->set_buffer_size(xprt, sndsize, rcvsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607}
608
609/*
610 * Return size of largest payload RPC client can support, in bytes
611 *
612 * For stream transports, this is one RPC record fragment (see RFC
613 * 1831), as we don't support multi-record requests yet. For datagram
614 * transports, this is the size of an IP packet minus the IP, UDP, and
615 * RPC header sizes.
616 */
617size_t rpc_max_payload(struct rpc_clnt *clnt)
618{
619 return clnt->cl_xprt->max_payload;
620}
Chuck Leverb86acd52006-08-22 20:06:22 -0400621EXPORT_SYMBOL_GPL(rpc_max_payload);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Chuck Lever35f5a422006-01-03 09:55:50 +0100623/**
624 * rpc_force_rebind - force transport to check that remote port is unchanged
625 * @clnt: client to rebind
626 *
627 */
628void rpc_force_rebind(struct rpc_clnt *clnt)
629{
630 if (clnt->cl_autobind)
Chuck Leverec739ef2006-08-22 20:06:15 -0400631 xprt_clear_bound(clnt->cl_xprt);
Chuck Lever35f5a422006-01-03 09:55:50 +0100632}
Chuck Leverb86acd52006-08-22 20:06:22 -0400633EXPORT_SYMBOL_GPL(rpc_force_rebind);
Chuck Lever35f5a422006-01-03 09:55:50 +0100634
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635/*
636 * Restart an (async) RPC call. Usually called from within the
637 * exit handler.
638 */
639void
640rpc_restart_call(struct rpc_task *task)
641{
642 if (RPC_ASSASSINATED(task))
643 return;
644
645 task->tk_action = call_start;
646}
647
648/*
649 * 0. Initial state
650 *
651 * Other FSM states can be visited zero or more times, but
652 * this state is visited exactly once for each RPC.
653 */
654static void
655call_start(struct rpc_task *task)
656{
657 struct rpc_clnt *clnt = task->tk_client;
658
659 dprintk("RPC: %4d call_start %s%d proc %d (%s)\n", task->tk_pid,
660 clnt->cl_protname, clnt->cl_vers, task->tk_msg.rpc_proc->p_proc,
661 (RPC_IS_ASYNC(task) ? "async" : "sync"));
662
663 /* Increment call count */
664 task->tk_msg.rpc_proc->p_count++;
665 clnt->cl_stats->rpccnt++;
666 task->tk_action = call_reserve;
667}
668
669/*
670 * 1. Reserve an RPC call slot
671 */
672static void
673call_reserve(struct rpc_task *task)
674{
675 dprintk("RPC: %4d call_reserve\n", task->tk_pid);
676
677 if (!rpcauth_uptodatecred(task)) {
678 task->tk_action = call_refresh;
679 return;
680 }
681
682 task->tk_status = 0;
683 task->tk_action = call_reserveresult;
684 xprt_reserve(task);
685}
686
687/*
688 * 1b. Grok the result of xprt_reserve()
689 */
690static void
691call_reserveresult(struct rpc_task *task)
692{
693 int status = task->tk_status;
694
695 dprintk("RPC: %4d call_reserveresult (status %d)\n",
696 task->tk_pid, task->tk_status);
697
698 /*
699 * After a call to xprt_reserve(), we must have either
700 * a request slot or else an error status.
701 */
702 task->tk_status = 0;
703 if (status >= 0) {
704 if (task->tk_rqstp) {
705 task->tk_action = call_allocate;
706 return;
707 }
708
709 printk(KERN_ERR "%s: status=%d, but no request slot, exiting\n",
710 __FUNCTION__, status);
711 rpc_exit(task, -EIO);
712 return;
713 }
714
715 /*
716 * Even though there was an error, we may have acquired
717 * a request slot somehow. Make sure not to leak it.
718 */
719 if (task->tk_rqstp) {
720 printk(KERN_ERR "%s: status=%d, request allocated anyway\n",
721 __FUNCTION__, status);
722 xprt_release(task);
723 }
724
725 switch (status) {
726 case -EAGAIN: /* woken up; retry */
727 task->tk_action = call_reserve;
728 return;
729 case -EIO: /* probably a shutdown */
730 break;
731 default:
732 printk(KERN_ERR "%s: unrecognized error %d, exiting\n",
733 __FUNCTION__, status);
734 break;
735 }
736 rpc_exit(task, status);
737}
738
739/*
740 * 2. Allocate the buffer. For details, see sched.c:rpc_malloc.
Chuck Lever02107142006-01-03 09:55:49 +0100741 * (Note: buffer memory is freed in xprt_release).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 */
743static void
744call_allocate(struct rpc_task *task)
745{
Chuck Lever02107142006-01-03 09:55:49 +0100746 struct rpc_rqst *req = task->tk_rqstp;
747 struct rpc_xprt *xprt = task->tk_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 unsigned int bufsiz;
749
750 dprintk("RPC: %4d call_allocate (status %d)\n",
751 task->tk_pid, task->tk_status);
752 task->tk_action = call_bind;
Chuck Lever02107142006-01-03 09:55:49 +0100753 if (req->rq_buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 return;
755
756 /* FIXME: compute buffer requirements more exactly using
757 * auth->au_wslack */
758 bufsiz = task->tk_msg.rpc_proc->p_bufsiz + RPC_SLACK_SPACE;
759
Chuck Lever02107142006-01-03 09:55:49 +0100760 if (xprt->ops->buf_alloc(task, bufsiz << 1) != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 return;
762 printk(KERN_INFO "RPC: buffer allocation failed for task %p\n", task);
763
Trond Myklebust14b218a2005-06-22 17:16:28 +0000764 if (RPC_IS_ASYNC(task) || !signalled()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 xprt_release(task);
766 task->tk_action = call_reserve;
767 rpc_delay(task, HZ>>4);
768 return;
769 }
770
771 rpc_exit(task, -ERESTARTSYS);
772}
773
Trond Myklebust940e3312005-11-09 21:45:24 -0500774static inline int
775rpc_task_need_encode(struct rpc_task *task)
776{
777 return task->tk_rqstp->rq_snd_buf.len == 0;
778}
779
780static inline void
781rpc_task_force_reencode(struct rpc_task *task)
782{
783 task->tk_rqstp->rq_snd_buf.len = 0;
784}
785
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786/*
787 * 3. Encode arguments of an RPC call
788 */
789static void
790call_encode(struct rpc_task *task)
791{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 struct rpc_rqst *req = task->tk_rqstp;
793 struct xdr_buf *sndbuf = &req->rq_snd_buf;
794 struct xdr_buf *rcvbuf = &req->rq_rcv_buf;
795 unsigned int bufsiz;
796 kxdrproc_t encode;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700797 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
799 dprintk("RPC: %4d call_encode (status %d)\n",
800 task->tk_pid, task->tk_status);
801
802 /* Default buffer setup */
Chuck Lever02107142006-01-03 09:55:49 +0100803 bufsiz = req->rq_bufsize >> 1;
804 sndbuf->head[0].iov_base = (void *)req->rq_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 sndbuf->head[0].iov_len = bufsiz;
806 sndbuf->tail[0].iov_len = 0;
807 sndbuf->page_len = 0;
808 sndbuf->len = 0;
809 sndbuf->buflen = bufsiz;
Chuck Lever02107142006-01-03 09:55:49 +0100810 rcvbuf->head[0].iov_base = (void *)((char *)req->rq_buffer + bufsiz);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 rcvbuf->head[0].iov_len = bufsiz;
812 rcvbuf->tail[0].iov_len = 0;
813 rcvbuf->page_len = 0;
814 rcvbuf->len = 0;
815 rcvbuf->buflen = bufsiz;
816
817 /* Encode header and provided arguments */
818 encode = task->tk_msg.rpc_proc->p_encode;
819 if (!(p = call_header(task))) {
820 printk(KERN_INFO "RPC: call_header failed, exit EIO\n");
821 rpc_exit(task, -EIO);
822 return;
823 }
J. Bruce Fieldsf3680312005-10-13 16:54:48 -0400824 if (encode == NULL)
825 return;
826
827 task->tk_status = rpcauth_wrap_req(task, encode, req, p,
828 task->tk_msg.rpc_argp);
829 if (task->tk_status == -ENOMEM) {
830 /* XXX: Is this sane? */
831 rpc_delay(task, 3*HZ);
832 task->tk_status = -EAGAIN;
833 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834}
835
836/*
837 * 4. Get the server port number if not yet set
838 */
839static void
840call_bind(struct rpc_task *task)
841{
Chuck Leverec739ef2006-08-22 20:06:15 -0400842 struct rpc_xprt *xprt = task->tk_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Chuck Leverda351872005-08-11 16:25:11 -0400844 dprintk("RPC: %4d call_bind (status %d)\n",
845 task->tk_pid, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
Chuck Leverda351872005-08-11 16:25:11 -0400847 task->tk_action = call_connect;
Chuck Leverec739ef2006-08-22 20:06:15 -0400848 if (!xprt_bound(xprt)) {
Chuck Leverda351872005-08-11 16:25:11 -0400849 task->tk_action = call_bind_status;
Chuck Leverec739ef2006-08-22 20:06:15 -0400850 task->tk_timeout = xprt->bind_timeout;
Chuck Leverbbf7c1d2006-08-22 20:06:16 -0400851 xprt->ops->rpcbind(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 }
853}
854
855/*
Chuck Leverda351872005-08-11 16:25:11 -0400856 * 4a. Sort out bind result
857 */
858static void
859call_bind_status(struct rpc_task *task)
860{
861 int status = -EACCES;
862
863 if (task->tk_status >= 0) {
864 dprintk("RPC: %4d call_bind_status (status %d)\n",
865 task->tk_pid, task->tk_status);
866 task->tk_status = 0;
867 task->tk_action = call_connect;
868 return;
869 }
870
871 switch (task->tk_status) {
872 case -EACCES:
873 dprintk("RPC: %4d remote rpcbind: RPC program/version unavailable\n",
874 task->tk_pid);
Chuck Leverea635a52005-10-06 23:12:58 -0400875 rpc_delay(task, 3*HZ);
Trond Myklebustda458282006-08-31 15:44:52 -0400876 goto retry_timeout;
Chuck Leverda351872005-08-11 16:25:11 -0400877 case -ETIMEDOUT:
878 dprintk("RPC: %4d rpcbind request timed out\n",
879 task->tk_pid);
Trond Myklebustda458282006-08-31 15:44:52 -0400880 goto retry_timeout;
Chuck Leverda351872005-08-11 16:25:11 -0400881 case -EPFNOSUPPORT:
882 dprintk("RPC: %4d remote rpcbind service unavailable\n",
883 task->tk_pid);
884 break;
885 case -EPROTONOSUPPORT:
886 dprintk("RPC: %4d remote rpcbind version 2 unavailable\n",
887 task->tk_pid);
888 break;
889 default:
890 dprintk("RPC: %4d unrecognized rpcbind error (%d)\n",
891 task->tk_pid, -task->tk_status);
892 status = -EIO;
Chuck Leverda351872005-08-11 16:25:11 -0400893 }
894
895 rpc_exit(task, status);
896 return;
897
Trond Myklebustda458282006-08-31 15:44:52 -0400898retry_timeout:
899 task->tk_action = call_timeout;
Chuck Leverda351872005-08-11 16:25:11 -0400900}
901
902/*
903 * 4b. Connect to the RPC server
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 */
905static void
906call_connect(struct rpc_task *task)
907{
Chuck Leverda351872005-08-11 16:25:11 -0400908 struct rpc_xprt *xprt = task->tk_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
Chuck Leverda351872005-08-11 16:25:11 -0400910 dprintk("RPC: %4d call_connect xprt %p %s connected\n",
911 task->tk_pid, xprt,
912 (xprt_connected(xprt) ? "is" : "is not"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
Chuck Leverda351872005-08-11 16:25:11 -0400914 task->tk_action = call_transmit;
915 if (!xprt_connected(xprt)) {
916 task->tk_action = call_connect_status;
917 if (task->tk_status < 0)
918 return;
919 xprt_connect(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921}
922
923/*
Chuck Leverda351872005-08-11 16:25:11 -0400924 * 4c. Sort out connect result
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 */
926static void
927call_connect_status(struct rpc_task *task)
928{
929 struct rpc_clnt *clnt = task->tk_client;
930 int status = task->tk_status;
931
Chuck Leverda351872005-08-11 16:25:11 -0400932 dprintk("RPC: %5u call_connect_status (status %d)\n",
933 task->tk_pid, task->tk_status);
934
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 task->tk_status = 0;
936 if (status >= 0) {
937 clnt->cl_stats->netreconn++;
938 task->tk_action = call_transmit;
939 return;
940 }
941
Chuck Leverda351872005-08-11 16:25:11 -0400942 /* Something failed: remote service port may have changed */
Chuck Lever35f5a422006-01-03 09:55:50 +0100943 rpc_force_rebind(clnt);
Chuck Leverda351872005-08-11 16:25:11 -0400944
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 switch (status) {
946 case -ENOTCONN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 case -EAGAIN:
Chuck Leverda351872005-08-11 16:25:11 -0400948 task->tk_action = call_bind;
Trond Myklebustda458282006-08-31 15:44:52 -0400949 if (!RPC_IS_SOFT(task))
950 return;
951 /* if soft mounted, test if we've timed out */
952 case -ETIMEDOUT:
953 task->tk_action = call_timeout;
954 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 }
Trond Myklebustda458282006-08-31 15:44:52 -0400956 rpc_exit(task, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957}
958
959/*
960 * 5. Transmit the RPC request, and wait for reply
961 */
962static void
963call_transmit(struct rpc_task *task)
964{
965 dprintk("RPC: %4d call_transmit (status %d)\n",
966 task->tk_pid, task->tk_status);
967
968 task->tk_action = call_status;
969 if (task->tk_status < 0)
970 return;
971 task->tk_status = xprt_prepare_transmit(task);
972 if (task->tk_status != 0)
973 return;
Trond Myklebuste0ab53d2006-07-27 17:22:50 -0400974 task->tk_action = call_transmit_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 /* Encode here so that rpcsec_gss can use correct sequence number. */
Trond Myklebust940e3312005-11-09 21:45:24 -0500976 if (rpc_task_need_encode(task)) {
Trond Myklebuste0ab53d2006-07-27 17:22:50 -0400977 BUG_ON(task->tk_rqstp->rq_bytes_sent != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 call_encode(task);
Trond Myklebust5e5ce5b2005-10-18 14:20:11 -0700979 /* Did the encode result in an error condition? */
980 if (task->tk_status != 0)
Trond Myklebuste0ab53d2006-07-27 17:22:50 -0400981 return;
Trond Myklebust5e5ce5b2005-10-18 14:20:11 -0700982 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 xprt_transmit(task);
984 if (task->tk_status < 0)
985 return;
Trond Myklebuste0ab53d2006-07-27 17:22:50 -0400986 /*
987 * On success, ensure that we call xprt_end_transmit() before sleeping
988 * in order to allow access to the socket to other RPC requests.
989 */
990 call_transmit_status(task);
991 if (task->tk_msg.rpc_proc->p_decode != NULL)
992 return;
993 task->tk_action = rpc_exit_task;
994 rpc_wake_up_task(task);
995}
996
997/*
998 * 5a. Handle cleanup after a transmission
999 */
1000static void
1001call_transmit_status(struct rpc_task *task)
1002{
1003 task->tk_action = call_status;
1004 /*
1005 * Special case: if we've been waiting on the socket's write_space()
1006 * callback, then don't call xprt_end_transmit().
1007 */
1008 if (task->tk_status == -EAGAIN)
1009 return;
1010 xprt_end_transmit(task);
Trond Myklebust940e3312005-11-09 21:45:24 -05001011 rpc_task_force_reencode(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012}
1013
1014/*
1015 * 6. Sort out the RPC call status
1016 */
1017static void
1018call_status(struct rpc_task *task)
1019{
1020 struct rpc_clnt *clnt = task->tk_client;
1021 struct rpc_rqst *req = task->tk_rqstp;
1022 int status;
1023
1024 if (req->rq_received > 0 && !req->rq_bytes_sent)
1025 task->tk_status = req->rq_received;
1026
1027 dprintk("RPC: %4d call_status (status %d)\n",
1028 task->tk_pid, task->tk_status);
1029
1030 status = task->tk_status;
1031 if (status >= 0) {
1032 task->tk_action = call_decode;
1033 return;
1034 }
1035
1036 task->tk_status = 0;
1037 switch(status) {
Trond Myklebust76303992006-08-30 14:32:49 -04001038 case -EHOSTDOWN:
1039 case -EHOSTUNREACH:
1040 case -ENETUNREACH:
1041 /*
1042 * Delay any retries for 3 seconds, then handle as if it
1043 * were a timeout.
1044 */
1045 rpc_delay(task, 3*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 case -ETIMEDOUT:
1047 task->tk_action = call_timeout;
1048 break;
1049 case -ECONNREFUSED:
1050 case -ENOTCONN:
Chuck Lever35f5a422006-01-03 09:55:50 +01001051 rpc_force_rebind(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 task->tk_action = call_bind;
1053 break;
1054 case -EAGAIN:
1055 task->tk_action = call_transmit;
1056 break;
1057 case -EIO:
1058 /* shutdown or soft timeout */
1059 rpc_exit(task, status);
1060 break;
1061 default:
Chuck Leverf518e352006-01-03 09:55:52 +01001062 printk("%s: RPC call returned error %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 clnt->cl_protname, -status);
1064 rpc_exit(task, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 }
1066}
1067
1068/*
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04001069 * 6a. Handle RPC timeout
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 * We do not release the request slot, so we keep using the
1071 * same XID for all retransmits.
1072 */
1073static void
1074call_timeout(struct rpc_task *task)
1075{
1076 struct rpc_clnt *clnt = task->tk_client;
1077
1078 if (xprt_adjust_timeout(task->tk_rqstp) == 0) {
1079 dprintk("RPC: %4d call_timeout (minor)\n", task->tk_pid);
1080 goto retry;
1081 }
1082
1083 dprintk("RPC: %4d call_timeout (major)\n", task->tk_pid);
Chuck Leveref759a22006-03-20 13:44:17 -05001084 task->tk_timeouts++;
1085
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 if (RPC_IS_SOFT(task)) {
Chuck Leverf518e352006-01-03 09:55:52 +01001087 printk(KERN_NOTICE "%s: server %s not responding, timed out\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 clnt->cl_protname, clnt->cl_server);
1089 rpc_exit(task, -EIO);
1090 return;
1091 }
1092
Chuck Leverf518e352006-01-03 09:55:52 +01001093 if (!(task->tk_flags & RPC_CALL_MAJORSEEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 task->tk_flags |= RPC_CALL_MAJORSEEN;
1095 printk(KERN_NOTICE "%s: server %s not responding, still trying\n",
1096 clnt->cl_protname, clnt->cl_server);
1097 }
Chuck Lever35f5a422006-01-03 09:55:50 +01001098 rpc_force_rebind(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
1100retry:
1101 clnt->cl_stats->rpcretrans++;
1102 task->tk_action = call_bind;
1103 task->tk_status = 0;
1104}
1105
1106/*
1107 * 7. Decode the RPC reply
1108 */
1109static void
1110call_decode(struct rpc_task *task)
1111{
1112 struct rpc_clnt *clnt = task->tk_client;
1113 struct rpc_rqst *req = task->tk_rqstp;
1114 kxdrproc_t decode = task->tk_msg.rpc_proc->p_decode;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001115 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
1117 dprintk("RPC: %4d call_decode (status %d)\n",
1118 task->tk_pid, task->tk_status);
1119
Chuck Leverf518e352006-01-03 09:55:52 +01001120 if (task->tk_flags & RPC_CALL_MAJORSEEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 printk(KERN_NOTICE "%s: server %s OK\n",
1122 clnt->cl_protname, clnt->cl_server);
1123 task->tk_flags &= ~RPC_CALL_MAJORSEEN;
1124 }
1125
1126 if (task->tk_status < 12) {
1127 if (!RPC_IS_SOFT(task)) {
1128 task->tk_action = call_bind;
1129 clnt->cl_stats->rpcretrans++;
1130 goto out_retry;
1131 }
Trond Myklebustda458282006-08-31 15:44:52 -04001132 dprintk("%s: too small RPC reply size (%d bytes)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 clnt->cl_protname, task->tk_status);
Trond Myklebustda458282006-08-31 15:44:52 -04001134 task->tk_action = call_timeout;
1135 goto out_retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 }
1137
Trond Myklebust43ac3f22006-03-20 13:44:51 -05001138 /*
1139 * Ensure that we see all writes made by xprt_complete_rqst()
1140 * before it changed req->rq_received.
1141 */
1142 smp_rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 req->rq_rcv_buf.len = req->rq_private_buf.len;
1144
1145 /* Check that the softirq receive buffer is valid */
1146 WARN_ON(memcmp(&req->rq_rcv_buf, &req->rq_private_buf,
1147 sizeof(req->rq_rcv_buf)) != 0);
1148
1149 /* Verify the RPC header */
Trond Myklebustabbcf282006-01-03 09:55:03 +01001150 p = call_verify(task);
1151 if (IS_ERR(p)) {
1152 if (p == ERR_PTR(-EAGAIN))
1153 goto out_retry;
1154 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 }
1156
Trond Myklebustabbcf282006-01-03 09:55:03 +01001157 task->tk_action = rpc_exit_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
1159 if (decode)
1160 task->tk_status = rpcauth_unwrap_resp(task, decode, req, p,
1161 task->tk_msg.rpc_resp);
1162 dprintk("RPC: %4d call_decode result %d\n", task->tk_pid,
1163 task->tk_status);
1164 return;
1165out_retry:
1166 req->rq_received = req->rq_private_buf.len = 0;
1167 task->tk_status = 0;
1168}
1169
1170/*
1171 * 8. Refresh the credentials if rejected by the server
1172 */
1173static void
1174call_refresh(struct rpc_task *task)
1175{
1176 dprintk("RPC: %4d call_refresh\n", task->tk_pid);
1177
1178 xprt_release(task); /* Must do to obtain new XID */
1179 task->tk_action = call_refreshresult;
1180 task->tk_status = 0;
1181 task->tk_client->cl_stats->rpcauthrefresh++;
1182 rpcauth_refreshcred(task);
1183}
1184
1185/*
1186 * 8a. Process the results of a credential refresh
1187 */
1188static void
1189call_refreshresult(struct rpc_task *task)
1190{
1191 int status = task->tk_status;
1192 dprintk("RPC: %4d call_refreshresult (status %d)\n",
1193 task->tk_pid, task->tk_status);
1194
1195 task->tk_status = 0;
1196 task->tk_action = call_reserve;
1197 if (status >= 0 && rpcauth_uptodatecred(task))
1198 return;
1199 if (status == -EACCES) {
1200 rpc_exit(task, -EACCES);
1201 return;
1202 }
1203 task->tk_action = call_refresh;
1204 if (status != -ETIMEDOUT)
1205 rpc_delay(task, 3*HZ);
1206 return;
1207}
1208
1209/*
1210 * Call header serialization
1211 */
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001212static __be32 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213call_header(struct rpc_task *task)
1214{
1215 struct rpc_clnt *clnt = task->tk_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 struct rpc_rqst *req = task->tk_rqstp;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001217 __be32 *p = req->rq_svec[0].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
1219 /* FIXME: check buffer size? */
Chuck Lever808012f2005-08-25 16:25:49 -07001220
1221 p = xprt_skip_transport_header(task->tk_xprt, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 *p++ = req->rq_xid; /* XID */
1223 *p++ = htonl(RPC_CALL); /* CALL */
1224 *p++ = htonl(RPC_VERSION); /* RPC version */
1225 *p++ = htonl(clnt->cl_prog); /* program number */
1226 *p++ = htonl(clnt->cl_vers); /* program version */
1227 *p++ = htonl(task->tk_msg.rpc_proc->p_proc); /* procedure */
Trond Myklebust334ccfd2005-06-22 17:16:19 +00001228 p = rpcauth_marshcred(task, p);
1229 req->rq_slen = xdr_adjust_iovec(&req->rq_svec[0], p);
1230 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231}
1232
1233/*
1234 * Reply header verification
1235 */
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001236static __be32 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237call_verify(struct rpc_task *task)
1238{
1239 struct kvec *iov = &task->tk_rqstp->rq_rcv_buf.head[0];
1240 int len = task->tk_rqstp->rq_rcv_buf.len >> 2;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001241 __be32 *p = iov->iov_base;
1242 u32 n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 int error = -EACCES;
1244
David Howellse8896492006-08-24 15:44:19 -04001245 if ((task->tk_rqstp->rq_rcv_buf.len & 3) != 0) {
1246 /* RFC-1014 says that the representation of XDR data must be a
1247 * multiple of four bytes
1248 * - if it isn't pointer subtraction in the NFS client may give
1249 * undefined results
1250 */
1251 printk(KERN_WARNING
1252 "call_verify: XDR representation not a multiple of"
1253 " 4 bytes: 0x%x\n", task->tk_rqstp->rq_rcv_buf.len);
1254 goto out_eio;
1255 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 if ((len -= 3) < 0)
1257 goto out_overflow;
1258 p += 1; /* skip XID */
1259
1260 if ((n = ntohl(*p++)) != RPC_REPLY) {
1261 printk(KERN_WARNING "call_verify: not an RPC reply: %x\n", n);
Trond Myklebustabbcf282006-01-03 09:55:03 +01001262 goto out_garbage;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 }
1264 if ((n = ntohl(*p++)) != RPC_MSG_ACCEPTED) {
1265 if (--len < 0)
1266 goto out_overflow;
1267 switch ((n = ntohl(*p++))) {
1268 case RPC_AUTH_ERROR:
1269 break;
1270 case RPC_MISMATCH:
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00001271 dprintk("%s: RPC call version mismatch!\n", __FUNCTION__);
1272 error = -EPROTONOSUPPORT;
1273 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 default:
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00001275 dprintk("%s: RPC call rejected, unknown error: %x\n", __FUNCTION__, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 goto out_eio;
1277 }
1278 if (--len < 0)
1279 goto out_overflow;
1280 switch ((n = ntohl(*p++))) {
1281 case RPC_AUTH_REJECTEDCRED:
1282 case RPC_AUTH_REJECTEDVERF:
1283 case RPCSEC_GSS_CREDPROBLEM:
1284 case RPCSEC_GSS_CTXPROBLEM:
1285 if (!task->tk_cred_retry)
1286 break;
1287 task->tk_cred_retry--;
1288 dprintk("RPC: %4d call_verify: retry stale creds\n",
1289 task->tk_pid);
1290 rpcauth_invalcred(task);
1291 task->tk_action = call_refresh;
Trond Myklebustabbcf282006-01-03 09:55:03 +01001292 goto out_retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 case RPC_AUTH_BADCRED:
1294 case RPC_AUTH_BADVERF:
1295 /* possibly garbled cred/verf? */
1296 if (!task->tk_garb_retry)
1297 break;
1298 task->tk_garb_retry--;
1299 dprintk("RPC: %4d call_verify: retry garbled creds\n",
1300 task->tk_pid);
1301 task->tk_action = call_bind;
Trond Myklebustabbcf282006-01-03 09:55:03 +01001302 goto out_retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 case RPC_AUTH_TOOWEAK:
Levent Serinol1356b8c2006-03-20 13:44:11 -05001304 printk(KERN_NOTICE "call_verify: server %s requires stronger "
1305 "authentication.\n", task->tk_client->cl_server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 break;
1307 default:
1308 printk(KERN_WARNING "call_verify: unknown auth error: %x\n", n);
1309 error = -EIO;
1310 }
1311 dprintk("RPC: %4d call_verify: call rejected %d\n",
1312 task->tk_pid, n);
1313 goto out_err;
1314 }
1315 if (!(p = rpcauth_checkverf(task, p))) {
1316 printk(KERN_WARNING "call_verify: auth check failed\n");
Trond Myklebustabbcf282006-01-03 09:55:03 +01001317 goto out_garbage; /* bad verifier, retry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 }
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001319 len = p - (__be32 *)iov->iov_base - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 if (len < 0)
1321 goto out_overflow;
1322 switch ((n = ntohl(*p++))) {
1323 case RPC_SUCCESS:
1324 return p;
1325 case RPC_PROG_UNAVAIL:
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00001326 dprintk("RPC: call_verify: program %u is unsupported by server %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 (unsigned int)task->tk_client->cl_prog,
1328 task->tk_client->cl_server);
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00001329 error = -EPFNOSUPPORT;
1330 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 case RPC_PROG_MISMATCH:
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00001332 dprintk("RPC: call_verify: program %u, version %u unsupported by server %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 (unsigned int)task->tk_client->cl_prog,
1334 (unsigned int)task->tk_client->cl_vers,
1335 task->tk_client->cl_server);
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00001336 error = -EPROTONOSUPPORT;
1337 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 case RPC_PROC_UNAVAIL:
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00001339 dprintk("RPC: call_verify: proc %p unsupported by program %u, version %u on server %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 task->tk_msg.rpc_proc,
1341 task->tk_client->cl_prog,
1342 task->tk_client->cl_vers,
1343 task->tk_client->cl_server);
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00001344 error = -EOPNOTSUPP;
1345 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 case RPC_GARBAGE_ARGS:
1347 dprintk("RPC: %4d %s: server saw garbage\n", task->tk_pid, __FUNCTION__);
1348 break; /* retry */
1349 default:
1350 printk(KERN_WARNING "call_verify: server accept status: %x\n", n);
1351 /* Also retry */
1352 }
1353
Trond Myklebustabbcf282006-01-03 09:55:03 +01001354out_garbage:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 task->tk_client->cl_stats->rpcgarbage++;
1356 if (task->tk_garb_retry) {
1357 task->tk_garb_retry--;
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00001358 dprintk("RPC %s: retrying %4d\n", __FUNCTION__, task->tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 task->tk_action = call_bind;
Trond Myklebustabbcf282006-01-03 09:55:03 +01001360out_retry:
1361 return ERR_PTR(-EAGAIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 }
1363 printk(KERN_WARNING "RPC %s: retry failed, exit EIO\n", __FUNCTION__);
1364out_eio:
1365 error = -EIO;
1366out_err:
1367 rpc_exit(task, error);
Trond Myklebustabbcf282006-01-03 09:55:03 +01001368 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369out_overflow:
1370 printk(KERN_WARNING "RPC %s: server reply was truncated.\n", __FUNCTION__);
Trond Myklebustabbcf282006-01-03 09:55:03 +01001371 goto out_garbage;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372}
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00001373
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001374static int rpcproc_encode_null(void *rqstp, __be32 *data, void *obj)
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00001375{
1376 return 0;
1377}
1378
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001379static int rpcproc_decode_null(void *rqstp, __be32 *data, void *obj)
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00001380{
1381 return 0;
1382}
1383
1384static struct rpc_procinfo rpcproc_null = {
1385 .p_encode = rpcproc_encode_null,
1386 .p_decode = rpcproc_decode_null,
1387};
1388
1389int rpc_ping(struct rpc_clnt *clnt, int flags)
1390{
1391 struct rpc_message msg = {
1392 .rpc_proc = &rpcproc_null,
1393 };
1394 int err;
1395 msg.rpc_cred = authnull_ops.lookup_cred(NULL, NULL, 0);
1396 err = rpc_call_sync(clnt, &msg, flags);
1397 put_rpccred(msg.rpc_cred);
1398 return err;
1399}