blob: ceadb728f0da2f2a8acbd145994a799575539e66 [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);
63static u32 * call_header(struct rpc_task *task);
64static u32 * call_verify(struct rpc_task *task);
65
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Chuck Leverec739ef2006-08-22 20:06:15 -0400145 if (!xprt_bound(clnt->cl_xprt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 clnt->cl_autobind = 1;
147
148 clnt->cl_rtt = &clnt->cl_rtt_default;
149 rpc_init_rtt(&clnt->cl_rtt_default, xprt->timeout.to_initval);
150
151 err = rpc_setup_pipedir(clnt, program->pipe_dir_name);
152 if (err < 0)
153 goto out_no_path;
154
J. Bruce Fields6a192752005-06-22 17:16:23 +0000155 auth = rpcauth_create(flavor, clnt);
156 if (IS_ERR(auth)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 printk(KERN_INFO "RPC: Couldn't create auth handle (flavor %u)\n",
158 flavor);
J. Bruce Fields6a192752005-06-22 17:16:23 +0000159 err = PTR_ERR(auth);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 goto out_no_auth;
161 }
162
163 /* save the nodename */
164 clnt->cl_nodelen = strlen(system_utsname.nodename);
165 if (clnt->cl_nodelen > UNX_MAXNODENAME)
166 clnt->cl_nodelen = UNX_MAXNODENAME;
167 memcpy(clnt->cl_nodename, system_utsname.nodename, clnt->cl_nodelen);
168 return clnt;
169
170out_no_auth:
Trond Myklebust54281542006-03-20 13:44:49 -0500171 if (!IS_ERR(clnt->cl_dentry)) {
Trond Myklebustdff02cc2006-07-31 14:17:18 -0700172 rpc_rmdir(clnt->cl_dentry);
Trond Myklebust54281542006-03-20 13:44:49 -0500173 rpc_put_mount();
174 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175out_no_path:
176 if (clnt->cl_server != clnt->cl_inline_name)
177 kfree(clnt->cl_server);
178 kfree(clnt);
179out_err:
Trond Myklebust5b616f52005-06-22 17:16:20 +0000180 xprt_destroy(xprt);
Adrian Bunk712917d2006-03-13 21:20:47 -0800181out_no_xprt:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 return ERR_PTR(err);
183}
184
Chuck Leverc2866762006-08-22 20:06:20 -0400185/*
186 * rpc_create - create an RPC client and transport with one call
187 * @args: rpc_clnt create argument structure
188 *
189 * Creates and initializes an RPC transport and an RPC client.
190 *
191 * It can ping the server in order to determine if it is up, and to see if
192 * it supports this program and version. RPC_CLNT_CREATE_NOPING disables
193 * this behavior so asynchronous tasks can also use rpc_create.
194 */
195struct rpc_clnt *rpc_create(struct rpc_create_args *args)
196{
197 struct rpc_xprt *xprt;
198 struct rpc_clnt *clnt;
199
200 xprt = xprt_create_transport(args->protocol, args->address,
201 args->addrsize, args->timeout);
202 if (IS_ERR(xprt))
203 return (struct rpc_clnt *)xprt;
204
205 /*
206 * By default, kernel RPC client connects from a reserved port.
207 * CAP_NET_BIND_SERVICE will not be set for unprivileged requesters,
208 * but it is always enabled for rpciod, which handles the connect
209 * operation.
210 */
211 xprt->resvport = 1;
212 if (args->flags & RPC_CLNT_CREATE_NONPRIVPORT)
213 xprt->resvport = 0;
214
215 dprintk("RPC: creating %s client for %s (xprt %p)\n",
216 args->program->name, args->servername, xprt);
217
218 clnt = rpc_new_client(xprt, args->servername, args->program,
219 args->version, args->authflavor);
220 if (IS_ERR(clnt))
221 return clnt;
222
223 if (!(args->flags & RPC_CLNT_CREATE_NOPING)) {
224 int err = rpc_ping(clnt, RPC_TASK_SOFT|RPC_TASK_NOINTR);
225 if (err != 0) {
226 rpc_shutdown_client(clnt);
227 return ERR_PTR(err);
228 }
229 }
230
231 clnt->cl_softrtry = 1;
232 if (args->flags & RPC_CLNT_CREATE_HARDRTRY)
233 clnt->cl_softrtry = 0;
234
235 if (args->flags & RPC_CLNT_CREATE_INTR)
236 clnt->cl_intr = 1;
237 if (args->flags & RPC_CLNT_CREATE_AUTOBIND)
238 clnt->cl_autobind = 1;
239 if (args->flags & RPC_CLNT_CREATE_ONESHOT)
240 clnt->cl_oneshot = 1;
241
242 return clnt;
243}
Chuck Leverb86acd52006-08-22 20:06:22 -0400244EXPORT_SYMBOL_GPL(rpc_create);
Chuck Leverc2866762006-08-22 20:06:20 -0400245
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246/*
247 * This function clones the RPC client structure. It allows us to share the
248 * same transport while varying parameters such as the authentication
249 * flavour.
250 */
251struct rpc_clnt *
252rpc_clone_client(struct rpc_clnt *clnt)
253{
254 struct rpc_clnt *new;
255
Kris Katterjohn8b3a7002006-01-11 15:56:43 -0800256 new = kmalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 if (!new)
258 goto out_no_clnt;
259 memcpy(new, clnt, sizeof(*new));
260 atomic_set(&new->cl_count, 1);
261 atomic_set(&new->cl_users, 0);
262 new->cl_parent = clnt;
263 atomic_inc(&clnt->cl_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 /* Turn off autobind on clones */
265 new->cl_autobind = 0;
266 new->cl_oneshot = 0;
267 new->cl_dead = 0;
Trond Myklebust8f8e7a52006-08-14 13:11:15 -0400268 if (!IS_ERR(new->cl_dentry))
Trond Myklebust54281542006-03-20 13:44:49 -0500269 dget(new->cl_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 rpc_init_rtt(&new->cl_rtt_default, clnt->cl_xprt->timeout.to_initval);
271 if (new->cl_auth)
272 atomic_inc(&new->cl_auth->au_count);
Chuck Lever4a681792006-08-22 20:06:15 -0400273 new->cl_metrics = rpc_alloc_iostats(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 return new;
275out_no_clnt:
276 printk(KERN_INFO "RPC: out of memory in %s\n", __FUNCTION__);
277 return ERR_PTR(-ENOMEM);
278}
279
280/*
281 * Properly shut down an RPC client, terminating all outstanding
282 * requests. Note that we must be certain that cl_oneshot and
283 * cl_dead are cleared, or else the client would be destroyed
284 * when the last task releases it.
285 */
286int
287rpc_shutdown_client(struct rpc_clnt *clnt)
288{
289 dprintk("RPC: shutting down %s client for %s, tasks=%d\n",
290 clnt->cl_protname, clnt->cl_server,
291 atomic_read(&clnt->cl_users));
292
293 while (atomic_read(&clnt->cl_users) > 0) {
294 /* Don't let rpc_release_client destroy us */
295 clnt->cl_oneshot = 0;
296 clnt->cl_dead = 0;
297 rpc_killall_tasks(clnt);
Ingo Molnar532347e2006-01-09 20:52:53 -0800298 wait_event_timeout(destroy_wait,
Linus Torvalds4f477072006-01-10 08:56:39 -0800299 !atomic_read(&clnt->cl_users), 1*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 }
301
302 if (atomic_read(&clnt->cl_users) < 0) {
303 printk(KERN_ERR "RPC: rpc_shutdown_client clnt %p tasks=%d\n",
304 clnt, atomic_read(&clnt->cl_users));
305#ifdef RPC_DEBUG
306 rpc_show_tasks();
307#endif
308 BUG();
309 }
310
311 return rpc_destroy_client(clnt);
312}
313
314/*
315 * Delete an RPC client
316 */
317int
318rpc_destroy_client(struct rpc_clnt *clnt)
319{
320 if (!atomic_dec_and_test(&clnt->cl_count))
321 return 1;
322 BUG_ON(atomic_read(&clnt->cl_users) != 0);
323
324 dprintk("RPC: destroying %s client for %s\n",
325 clnt->cl_protname, clnt->cl_server);
326 if (clnt->cl_auth) {
327 rpcauth_destroy(clnt->cl_auth);
328 clnt->cl_auth = NULL;
329 }
330 if (clnt->cl_parent != clnt) {
Trond Myklebust8f8e7a52006-08-14 13:11:15 -0400331 if (!IS_ERR(clnt->cl_dentry))
332 dput(clnt->cl_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 rpc_destroy_client(clnt->cl_parent);
334 goto out_free;
335 }
Trond Myklebust8f8e7a52006-08-14 13:11:15 -0400336 if (!IS_ERR(clnt->cl_dentry)) {
Trond Myklebustdff02cc2006-07-31 14:17:18 -0700337 rpc_rmdir(clnt->cl_dentry);
Trond Myklebust8f8e7a52006-08-14 13:11:15 -0400338 rpc_put_mount();
339 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 if (clnt->cl_xprt) {
341 xprt_destroy(clnt->cl_xprt);
342 clnt->cl_xprt = NULL;
343 }
344 if (clnt->cl_server != clnt->cl_inline_name)
345 kfree(clnt->cl_server);
346out_free:
Chuck Lever11c556b2006-03-20 13:44:22 -0500347 rpc_free_iostats(clnt->cl_metrics);
348 clnt->cl_metrics = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 kfree(clnt);
350 return 0;
351}
352
353/*
354 * Release an RPC client
355 */
356void
357rpc_release_client(struct rpc_clnt *clnt)
358{
359 dprintk("RPC: rpc_release_client(%p, %d)\n",
360 clnt, atomic_read(&clnt->cl_users));
361
362 if (!atomic_dec_and_test(&clnt->cl_users))
363 return;
364 wake_up(&destroy_wait);
365 if (clnt->cl_oneshot || clnt->cl_dead)
366 rpc_destroy_client(clnt);
367}
368
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000369/**
370 * rpc_bind_new_program - bind a new RPC program to an existing client
371 * @old - old rpc_client
372 * @program - rpc program to set
373 * @vers - rpc program version
374 *
375 * Clones the rpc client and sets up a new RPC program. This is mainly
376 * of use for enabling different RPC programs to share the same transport.
377 * The Sun NFSv2/v3 ACL protocol can do this.
378 */
379struct rpc_clnt *rpc_bind_new_program(struct rpc_clnt *old,
380 struct rpc_program *program,
381 int vers)
382{
383 struct rpc_clnt *clnt;
384 struct rpc_version *version;
385 int err;
386
387 BUG_ON(vers >= program->nrvers || !program->version[vers]);
388 version = program->version[vers];
389 clnt = rpc_clone_client(old);
390 if (IS_ERR(clnt))
391 goto out;
392 clnt->cl_procinfo = version->procs;
393 clnt->cl_maxproc = version->nrprocs;
394 clnt->cl_protname = program->name;
395 clnt->cl_prog = program->number;
396 clnt->cl_vers = version->number;
397 clnt->cl_stats = program->stats;
398 err = rpc_ping(clnt, RPC_TASK_SOFT|RPC_TASK_NOINTR);
399 if (err != 0) {
400 rpc_shutdown_client(clnt);
401 clnt = ERR_PTR(err);
402 }
403out:
404 return clnt;
405}
406
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407/*
408 * Default callback for async RPC calls
409 */
410static void
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100411rpc_default_callback(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412{
413}
414
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100415static const struct rpc_call_ops rpc_default_ops = {
416 .rpc_call_done = rpc_default_callback,
417};
418
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419/*
Trond Myklebust14b218a2005-06-22 17:16:28 +0000420 * Export the signal mask handling for synchronous code that
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 * sleeps on RPC calls
422 */
Trond Myklebust2bd61572006-01-03 09:55:19 +0100423#define RPC_INTR_SIGNALS (sigmask(SIGHUP) | sigmask(SIGINT) | sigmask(SIGQUIT) | sigmask(SIGTERM))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
Trond Myklebust14b218a2005-06-22 17:16:28 +0000425static void rpc_save_sigmask(sigset_t *oldset, int intr)
426{
Trond Myklebust2bd61572006-01-03 09:55:19 +0100427 unsigned long sigallow = sigmask(SIGKILL);
Trond Myklebust14b218a2005-06-22 17:16:28 +0000428 sigset_t sigmask;
429
430 /* Block all signals except those listed in sigallow */
431 if (intr)
432 sigallow |= RPC_INTR_SIGNALS;
433 siginitsetinv(&sigmask, sigallow);
434 sigprocmask(SIG_BLOCK, &sigmask, oldset);
435}
436
437static inline void rpc_task_sigmask(struct rpc_task *task, sigset_t *oldset)
438{
439 rpc_save_sigmask(oldset, !RPC_TASK_UNINTERRUPTIBLE(task));
440}
441
442static inline void rpc_restore_sigmask(sigset_t *oldset)
443{
444 sigprocmask(SIG_SETMASK, oldset, NULL);
445}
446
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447void rpc_clnt_sigmask(struct rpc_clnt *clnt, sigset_t *oldset)
448{
Trond Myklebust14b218a2005-06-22 17:16:28 +0000449 rpc_save_sigmask(oldset, clnt->cl_intr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450}
451
452void rpc_clnt_sigunmask(struct rpc_clnt *clnt, sigset_t *oldset)
453{
Trond Myklebust14b218a2005-06-22 17:16:28 +0000454 rpc_restore_sigmask(oldset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455}
456
457/*
458 * New rpc_call implementation
459 */
460int rpc_call_sync(struct rpc_clnt *clnt, struct rpc_message *msg, int flags)
461{
462 struct rpc_task *task;
463 sigset_t oldset;
464 int status;
465
466 /* If this client is slain all further I/O fails */
467 if (clnt->cl_dead)
468 return -EIO;
469
470 BUG_ON(flags & RPC_TASK_ASYNC);
471
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 status = -ENOMEM;
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100473 task = rpc_new_task(clnt, flags, &rpc_default_ops, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 if (task == NULL)
475 goto out;
476
Trond Myklebust14b218a2005-06-22 17:16:28 +0000477 /* Mask signals on RPC calls _and_ GSS_AUTH upcalls */
478 rpc_task_sigmask(task, &oldset);
479
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 rpc_call_setup(task, msg, 0);
481
482 /* Set up the call info struct and execute the task */
Trond Myklebuste60859a2006-01-03 09:55:10 +0100483 status = task->tk_status;
484 if (status == 0) {
485 atomic_inc(&task->tk_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 status = rpc_execute(task);
Trond Myklebuste60859a2006-01-03 09:55:10 +0100487 if (status == 0)
488 status = task->tk_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 }
Trond Myklebust14b218a2005-06-22 17:16:28 +0000490 rpc_restore_sigmask(&oldset);
Trond Myklebuste60859a2006-01-03 09:55:10 +0100491 rpc_release_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 return status;
494}
495
496/*
497 * New rpc_call implementation
498 */
499int
500rpc_call_async(struct rpc_clnt *clnt, struct rpc_message *msg, int flags,
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100501 const struct rpc_call_ops *tk_ops, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502{
503 struct rpc_task *task;
504 sigset_t oldset;
505 int status;
506
507 /* If this client is slain all further I/O fails */
Trond Myklebust7a1218a2006-03-20 18:11:10 -0500508 status = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 if (clnt->cl_dead)
Trond Myklebust7a1218a2006-03-20 18:11:10 -0500510 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
512 flags |= RPC_TASK_ASYNC;
513
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 /* Create/initialize a new RPC task */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 status = -ENOMEM;
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100516 if (!(task = rpc_new_task(clnt, flags, tk_ops, data)))
Trond Myklebust7a1218a2006-03-20 18:11:10 -0500517 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Trond Myklebust14b218a2005-06-22 17:16:28 +0000519 /* Mask signals on GSS_AUTH upcalls */
520 rpc_task_sigmask(task, &oldset);
521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 rpc_call_setup(task, msg, 0);
523
524 /* Set up the call info struct and execute the task */
525 status = task->tk_status;
526 if (status == 0)
527 rpc_execute(task);
528 else
529 rpc_release_task(task);
530
Trond Myklebust14b218a2005-06-22 17:16:28 +0000531 rpc_restore_sigmask(&oldset);
Trond Myklebust7a1218a2006-03-20 18:11:10 -0500532 return status;
533out_release:
534 if (tk_ops->rpc_release != NULL)
535 tk_ops->rpc_release(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 return status;
537}
538
539
540void
541rpc_call_setup(struct rpc_task *task, struct rpc_message *msg, int flags)
542{
543 task->tk_msg = *msg;
544 task->tk_flags |= flags;
545 /* Bind the user cred */
546 if (task->tk_msg.rpc_cred != NULL)
547 rpcauth_holdcred(task);
548 else
549 rpcauth_bindcred(task);
550
551 if (task->tk_status == 0)
552 task->tk_action = call_start;
553 else
Trond Myklebustabbcf282006-01-03 09:55:03 +0100554 task->tk_action = rpc_exit_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555}
556
Chuck Levered394402006-08-22 20:06:17 -0400557/**
558 * rpc_peeraddr - extract remote peer address from clnt's xprt
559 * @clnt: RPC client structure
560 * @buf: target buffer
561 * @size: length of target buffer
562 *
563 * Returns the number of bytes that are actually in the stored address.
564 */
565size_t rpc_peeraddr(struct rpc_clnt *clnt, struct sockaddr *buf, size_t bufsize)
566{
567 size_t bytes;
568 struct rpc_xprt *xprt = clnt->cl_xprt;
569
570 bytes = sizeof(xprt->addr);
571 if (bytes > bufsize)
572 bytes = bufsize;
573 memcpy(buf, &clnt->cl_xprt->addr, bytes);
Chuck Leverc4efcb12006-08-22 20:06:19 -0400574 return xprt->addrlen;
Chuck Levered394402006-08-22 20:06:17 -0400575}
Chuck Leverb86acd52006-08-22 20:06:22 -0400576EXPORT_SYMBOL_GPL(rpc_peeraddr);
Chuck Levered394402006-08-22 20:06:17 -0400577
Chuck Leverf425eba2006-08-22 20:06:18 -0400578/**
579 * rpc_peeraddr2str - return remote peer address in printable format
580 * @clnt: RPC client structure
581 * @format: address format
582 *
583 */
584char *rpc_peeraddr2str(struct rpc_clnt *clnt, enum rpc_display_format_t format)
585{
586 struct rpc_xprt *xprt = clnt->cl_xprt;
587 return xprt->ops->print_addr(xprt, format);
588}
Chuck Leverb86acd52006-08-22 20:06:22 -0400589EXPORT_SYMBOL_GPL(rpc_peeraddr2str);
Chuck Leverf425eba2006-08-22 20:06:18 -0400590
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591void
592rpc_setbufsize(struct rpc_clnt *clnt, unsigned int sndsize, unsigned int rcvsize)
593{
594 struct rpc_xprt *xprt = clnt->cl_xprt;
Chuck Lever470056c2005-08-25 16:25:56 -0700595 if (xprt->ops->set_buffer_size)
596 xprt->ops->set_buffer_size(xprt, sndsize, rcvsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597}
598
599/*
600 * Return size of largest payload RPC client can support, in bytes
601 *
602 * For stream transports, this is one RPC record fragment (see RFC
603 * 1831), as we don't support multi-record requests yet. For datagram
604 * transports, this is the size of an IP packet minus the IP, UDP, and
605 * RPC header sizes.
606 */
607size_t rpc_max_payload(struct rpc_clnt *clnt)
608{
609 return clnt->cl_xprt->max_payload;
610}
Chuck Leverb86acd52006-08-22 20:06:22 -0400611EXPORT_SYMBOL_GPL(rpc_max_payload);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
Chuck Lever35f5a422006-01-03 09:55:50 +0100613/**
614 * rpc_force_rebind - force transport to check that remote port is unchanged
615 * @clnt: client to rebind
616 *
617 */
618void rpc_force_rebind(struct rpc_clnt *clnt)
619{
620 if (clnt->cl_autobind)
Chuck Leverec739ef2006-08-22 20:06:15 -0400621 xprt_clear_bound(clnt->cl_xprt);
Chuck Lever35f5a422006-01-03 09:55:50 +0100622}
Chuck Leverb86acd52006-08-22 20:06:22 -0400623EXPORT_SYMBOL_GPL(rpc_force_rebind);
Chuck Lever35f5a422006-01-03 09:55:50 +0100624
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625/*
626 * Restart an (async) RPC call. Usually called from within the
627 * exit handler.
628 */
629void
630rpc_restart_call(struct rpc_task *task)
631{
632 if (RPC_ASSASSINATED(task))
633 return;
634
635 task->tk_action = call_start;
636}
637
638/*
639 * 0. Initial state
640 *
641 * Other FSM states can be visited zero or more times, but
642 * this state is visited exactly once for each RPC.
643 */
644static void
645call_start(struct rpc_task *task)
646{
647 struct rpc_clnt *clnt = task->tk_client;
648
649 dprintk("RPC: %4d call_start %s%d proc %d (%s)\n", task->tk_pid,
650 clnt->cl_protname, clnt->cl_vers, task->tk_msg.rpc_proc->p_proc,
651 (RPC_IS_ASYNC(task) ? "async" : "sync"));
652
653 /* Increment call count */
654 task->tk_msg.rpc_proc->p_count++;
655 clnt->cl_stats->rpccnt++;
656 task->tk_action = call_reserve;
657}
658
659/*
660 * 1. Reserve an RPC call slot
661 */
662static void
663call_reserve(struct rpc_task *task)
664{
665 dprintk("RPC: %4d call_reserve\n", task->tk_pid);
666
667 if (!rpcauth_uptodatecred(task)) {
668 task->tk_action = call_refresh;
669 return;
670 }
671
672 task->tk_status = 0;
673 task->tk_action = call_reserveresult;
674 xprt_reserve(task);
675}
676
677/*
678 * 1b. Grok the result of xprt_reserve()
679 */
680static void
681call_reserveresult(struct rpc_task *task)
682{
683 int status = task->tk_status;
684
685 dprintk("RPC: %4d call_reserveresult (status %d)\n",
686 task->tk_pid, task->tk_status);
687
688 /*
689 * After a call to xprt_reserve(), we must have either
690 * a request slot or else an error status.
691 */
692 task->tk_status = 0;
693 if (status >= 0) {
694 if (task->tk_rqstp) {
695 task->tk_action = call_allocate;
696 return;
697 }
698
699 printk(KERN_ERR "%s: status=%d, but no request slot, exiting\n",
700 __FUNCTION__, status);
701 rpc_exit(task, -EIO);
702 return;
703 }
704
705 /*
706 * Even though there was an error, we may have acquired
707 * a request slot somehow. Make sure not to leak it.
708 */
709 if (task->tk_rqstp) {
710 printk(KERN_ERR "%s: status=%d, request allocated anyway\n",
711 __FUNCTION__, status);
712 xprt_release(task);
713 }
714
715 switch (status) {
716 case -EAGAIN: /* woken up; retry */
717 task->tk_action = call_reserve;
718 return;
719 case -EIO: /* probably a shutdown */
720 break;
721 default:
722 printk(KERN_ERR "%s: unrecognized error %d, exiting\n",
723 __FUNCTION__, status);
724 break;
725 }
726 rpc_exit(task, status);
727}
728
729/*
730 * 2. Allocate the buffer. For details, see sched.c:rpc_malloc.
Chuck Lever02107142006-01-03 09:55:49 +0100731 * (Note: buffer memory is freed in xprt_release).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 */
733static void
734call_allocate(struct rpc_task *task)
735{
Chuck Lever02107142006-01-03 09:55:49 +0100736 struct rpc_rqst *req = task->tk_rqstp;
737 struct rpc_xprt *xprt = task->tk_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 unsigned int bufsiz;
739
740 dprintk("RPC: %4d call_allocate (status %d)\n",
741 task->tk_pid, task->tk_status);
742 task->tk_action = call_bind;
Chuck Lever02107142006-01-03 09:55:49 +0100743 if (req->rq_buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 return;
745
746 /* FIXME: compute buffer requirements more exactly using
747 * auth->au_wslack */
748 bufsiz = task->tk_msg.rpc_proc->p_bufsiz + RPC_SLACK_SPACE;
749
Chuck Lever02107142006-01-03 09:55:49 +0100750 if (xprt->ops->buf_alloc(task, bufsiz << 1) != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 return;
752 printk(KERN_INFO "RPC: buffer allocation failed for task %p\n", task);
753
Trond Myklebust14b218a2005-06-22 17:16:28 +0000754 if (RPC_IS_ASYNC(task) || !signalled()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 xprt_release(task);
756 task->tk_action = call_reserve;
757 rpc_delay(task, HZ>>4);
758 return;
759 }
760
761 rpc_exit(task, -ERESTARTSYS);
762}
763
Trond Myklebust940e3312005-11-09 21:45:24 -0500764static inline int
765rpc_task_need_encode(struct rpc_task *task)
766{
767 return task->tk_rqstp->rq_snd_buf.len == 0;
768}
769
770static inline void
771rpc_task_force_reencode(struct rpc_task *task)
772{
773 task->tk_rqstp->rq_snd_buf.len = 0;
774}
775
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776/*
777 * 3. Encode arguments of an RPC call
778 */
779static void
780call_encode(struct rpc_task *task)
781{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 struct rpc_rqst *req = task->tk_rqstp;
783 struct xdr_buf *sndbuf = &req->rq_snd_buf;
784 struct xdr_buf *rcvbuf = &req->rq_rcv_buf;
785 unsigned int bufsiz;
786 kxdrproc_t encode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 u32 *p;
788
789 dprintk("RPC: %4d call_encode (status %d)\n",
790 task->tk_pid, task->tk_status);
791
792 /* Default buffer setup */
Chuck Lever02107142006-01-03 09:55:49 +0100793 bufsiz = req->rq_bufsize >> 1;
794 sndbuf->head[0].iov_base = (void *)req->rq_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 sndbuf->head[0].iov_len = bufsiz;
796 sndbuf->tail[0].iov_len = 0;
797 sndbuf->page_len = 0;
798 sndbuf->len = 0;
799 sndbuf->buflen = bufsiz;
Chuck Lever02107142006-01-03 09:55:49 +0100800 rcvbuf->head[0].iov_base = (void *)((char *)req->rq_buffer + bufsiz);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 rcvbuf->head[0].iov_len = bufsiz;
802 rcvbuf->tail[0].iov_len = 0;
803 rcvbuf->page_len = 0;
804 rcvbuf->len = 0;
805 rcvbuf->buflen = bufsiz;
806
807 /* Encode header and provided arguments */
808 encode = task->tk_msg.rpc_proc->p_encode;
809 if (!(p = call_header(task))) {
810 printk(KERN_INFO "RPC: call_header failed, exit EIO\n");
811 rpc_exit(task, -EIO);
812 return;
813 }
J. Bruce Fieldsf3680312005-10-13 16:54:48 -0400814 if (encode == NULL)
815 return;
816
817 task->tk_status = rpcauth_wrap_req(task, encode, req, p,
818 task->tk_msg.rpc_argp);
819 if (task->tk_status == -ENOMEM) {
820 /* XXX: Is this sane? */
821 rpc_delay(task, 3*HZ);
822 task->tk_status = -EAGAIN;
823 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824}
825
826/*
827 * 4. Get the server port number if not yet set
828 */
829static void
830call_bind(struct rpc_task *task)
831{
Chuck Leverec739ef2006-08-22 20:06:15 -0400832 struct rpc_xprt *xprt = task->tk_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
Chuck Leverda351872005-08-11 16:25:11 -0400834 dprintk("RPC: %4d call_bind (status %d)\n",
835 task->tk_pid, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
Chuck Leverda351872005-08-11 16:25:11 -0400837 task->tk_action = call_connect;
Chuck Leverec739ef2006-08-22 20:06:15 -0400838 if (!xprt_bound(xprt)) {
Chuck Leverda351872005-08-11 16:25:11 -0400839 task->tk_action = call_bind_status;
Chuck Leverec739ef2006-08-22 20:06:15 -0400840 task->tk_timeout = xprt->bind_timeout;
Chuck Leverbbf7c1d2006-08-22 20:06:16 -0400841 xprt->ops->rpcbind(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 }
843}
844
845/*
Chuck Leverda351872005-08-11 16:25:11 -0400846 * 4a. Sort out bind result
847 */
848static void
849call_bind_status(struct rpc_task *task)
850{
851 int status = -EACCES;
852
853 if (task->tk_status >= 0) {
854 dprintk("RPC: %4d call_bind_status (status %d)\n",
855 task->tk_pid, task->tk_status);
856 task->tk_status = 0;
857 task->tk_action = call_connect;
858 return;
859 }
860
861 switch (task->tk_status) {
862 case -EACCES:
863 dprintk("RPC: %4d remote rpcbind: RPC program/version unavailable\n",
864 task->tk_pid);
Chuck Leverea635a52005-10-06 23:12:58 -0400865 rpc_delay(task, 3*HZ);
Trond Myklebustda458282006-08-31 15:44:52 -0400866 goto retry_timeout;
Chuck Leverda351872005-08-11 16:25:11 -0400867 case -ETIMEDOUT:
868 dprintk("RPC: %4d rpcbind request timed out\n",
869 task->tk_pid);
Trond Myklebustda458282006-08-31 15:44:52 -0400870 goto retry_timeout;
Chuck Leverda351872005-08-11 16:25:11 -0400871 case -EPFNOSUPPORT:
872 dprintk("RPC: %4d remote rpcbind service unavailable\n",
873 task->tk_pid);
874 break;
875 case -EPROTONOSUPPORT:
876 dprintk("RPC: %4d remote rpcbind version 2 unavailable\n",
877 task->tk_pid);
878 break;
879 default:
880 dprintk("RPC: %4d unrecognized rpcbind error (%d)\n",
881 task->tk_pid, -task->tk_status);
882 status = -EIO;
Chuck Leverda351872005-08-11 16:25:11 -0400883 }
884
885 rpc_exit(task, status);
886 return;
887
Trond Myklebustda458282006-08-31 15:44:52 -0400888retry_timeout:
889 task->tk_action = call_timeout;
Chuck Leverda351872005-08-11 16:25:11 -0400890}
891
892/*
893 * 4b. Connect to the RPC server
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 */
895static void
896call_connect(struct rpc_task *task)
897{
Chuck Leverda351872005-08-11 16:25:11 -0400898 struct rpc_xprt *xprt = task->tk_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
Chuck Leverda351872005-08-11 16:25:11 -0400900 dprintk("RPC: %4d call_connect xprt %p %s connected\n",
901 task->tk_pid, xprt,
902 (xprt_connected(xprt) ? "is" : "is not"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
Chuck Leverda351872005-08-11 16:25:11 -0400904 task->tk_action = call_transmit;
905 if (!xprt_connected(xprt)) {
906 task->tk_action = call_connect_status;
907 if (task->tk_status < 0)
908 return;
909 xprt_connect(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911}
912
913/*
Chuck Leverda351872005-08-11 16:25:11 -0400914 * 4c. Sort out connect result
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 */
916static void
917call_connect_status(struct rpc_task *task)
918{
919 struct rpc_clnt *clnt = task->tk_client;
920 int status = task->tk_status;
921
Chuck Leverda351872005-08-11 16:25:11 -0400922 dprintk("RPC: %5u call_connect_status (status %d)\n",
923 task->tk_pid, task->tk_status);
924
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 task->tk_status = 0;
926 if (status >= 0) {
927 clnt->cl_stats->netreconn++;
928 task->tk_action = call_transmit;
929 return;
930 }
931
Chuck Leverda351872005-08-11 16:25:11 -0400932 /* Something failed: remote service port may have changed */
Chuck Lever35f5a422006-01-03 09:55:50 +0100933 rpc_force_rebind(clnt);
Chuck Leverda351872005-08-11 16:25:11 -0400934
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 switch (status) {
936 case -ENOTCONN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 case -EAGAIN:
Chuck Leverda351872005-08-11 16:25:11 -0400938 task->tk_action = call_bind;
Trond Myklebustda458282006-08-31 15:44:52 -0400939 if (!RPC_IS_SOFT(task))
940 return;
941 /* if soft mounted, test if we've timed out */
942 case -ETIMEDOUT:
943 task->tk_action = call_timeout;
944 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 }
Trond Myklebustda458282006-08-31 15:44:52 -0400946 rpc_exit(task, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947}
948
949/*
950 * 5. Transmit the RPC request, and wait for reply
951 */
952static void
953call_transmit(struct rpc_task *task)
954{
955 dprintk("RPC: %4d call_transmit (status %d)\n",
956 task->tk_pid, task->tk_status);
957
958 task->tk_action = call_status;
959 if (task->tk_status < 0)
960 return;
961 task->tk_status = xprt_prepare_transmit(task);
962 if (task->tk_status != 0)
963 return;
Trond Myklebuste0ab53d2006-07-27 17:22:50 -0400964 task->tk_action = call_transmit_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 /* Encode here so that rpcsec_gss can use correct sequence number. */
Trond Myklebust940e3312005-11-09 21:45:24 -0500966 if (rpc_task_need_encode(task)) {
Trond Myklebuste0ab53d2006-07-27 17:22:50 -0400967 BUG_ON(task->tk_rqstp->rq_bytes_sent != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 call_encode(task);
Trond Myklebust5e5ce5b2005-10-18 14:20:11 -0700969 /* Did the encode result in an error condition? */
970 if (task->tk_status != 0)
Trond Myklebuste0ab53d2006-07-27 17:22:50 -0400971 return;
Trond Myklebust5e5ce5b2005-10-18 14:20:11 -0700972 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 xprt_transmit(task);
974 if (task->tk_status < 0)
975 return;
Trond Myklebuste0ab53d2006-07-27 17:22:50 -0400976 /*
977 * On success, ensure that we call xprt_end_transmit() before sleeping
978 * in order to allow access to the socket to other RPC requests.
979 */
980 call_transmit_status(task);
981 if (task->tk_msg.rpc_proc->p_decode != NULL)
982 return;
983 task->tk_action = rpc_exit_task;
984 rpc_wake_up_task(task);
985}
986
987/*
988 * 5a. Handle cleanup after a transmission
989 */
990static void
991call_transmit_status(struct rpc_task *task)
992{
993 task->tk_action = call_status;
994 /*
995 * Special case: if we've been waiting on the socket's write_space()
996 * callback, then don't call xprt_end_transmit().
997 */
998 if (task->tk_status == -EAGAIN)
999 return;
1000 xprt_end_transmit(task);
Trond Myklebust940e3312005-11-09 21:45:24 -05001001 rpc_task_force_reencode(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002}
1003
1004/*
1005 * 6. Sort out the RPC call status
1006 */
1007static void
1008call_status(struct rpc_task *task)
1009{
1010 struct rpc_clnt *clnt = task->tk_client;
1011 struct rpc_rqst *req = task->tk_rqstp;
1012 int status;
1013
1014 if (req->rq_received > 0 && !req->rq_bytes_sent)
1015 task->tk_status = req->rq_received;
1016
1017 dprintk("RPC: %4d call_status (status %d)\n",
1018 task->tk_pid, task->tk_status);
1019
1020 status = task->tk_status;
1021 if (status >= 0) {
1022 task->tk_action = call_decode;
1023 return;
1024 }
1025
1026 task->tk_status = 0;
1027 switch(status) {
Trond Myklebust76303992006-08-30 14:32:49 -04001028 case -EHOSTDOWN:
1029 case -EHOSTUNREACH:
1030 case -ENETUNREACH:
1031 /*
1032 * Delay any retries for 3 seconds, then handle as if it
1033 * were a timeout.
1034 */
1035 rpc_delay(task, 3*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 case -ETIMEDOUT:
1037 task->tk_action = call_timeout;
1038 break;
1039 case -ECONNREFUSED:
1040 case -ENOTCONN:
Chuck Lever35f5a422006-01-03 09:55:50 +01001041 rpc_force_rebind(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 task->tk_action = call_bind;
1043 break;
1044 case -EAGAIN:
1045 task->tk_action = call_transmit;
1046 break;
1047 case -EIO:
1048 /* shutdown or soft timeout */
1049 rpc_exit(task, status);
1050 break;
1051 default:
Chuck Leverf518e35a2006-01-03 09:55:52 +01001052 printk("%s: RPC call returned error %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 clnt->cl_protname, -status);
1054 rpc_exit(task, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 }
1056}
1057
1058/*
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04001059 * 6a. Handle RPC timeout
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 * We do not release the request slot, so we keep using the
1061 * same XID for all retransmits.
1062 */
1063static void
1064call_timeout(struct rpc_task *task)
1065{
1066 struct rpc_clnt *clnt = task->tk_client;
1067
1068 if (xprt_adjust_timeout(task->tk_rqstp) == 0) {
1069 dprintk("RPC: %4d call_timeout (minor)\n", task->tk_pid);
1070 goto retry;
1071 }
1072
1073 dprintk("RPC: %4d call_timeout (major)\n", task->tk_pid);
Chuck Leveref759a22006-03-20 13:44:17 -05001074 task->tk_timeouts++;
1075
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 if (RPC_IS_SOFT(task)) {
Chuck Leverf518e35a2006-01-03 09:55:52 +01001077 printk(KERN_NOTICE "%s: server %s not responding, timed out\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 clnt->cl_protname, clnt->cl_server);
1079 rpc_exit(task, -EIO);
1080 return;
1081 }
1082
Chuck Leverf518e35a2006-01-03 09:55:52 +01001083 if (!(task->tk_flags & RPC_CALL_MAJORSEEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 task->tk_flags |= RPC_CALL_MAJORSEEN;
1085 printk(KERN_NOTICE "%s: server %s not responding, still trying\n",
1086 clnt->cl_protname, clnt->cl_server);
1087 }
Chuck Lever35f5a422006-01-03 09:55:50 +01001088 rpc_force_rebind(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
1090retry:
1091 clnt->cl_stats->rpcretrans++;
1092 task->tk_action = call_bind;
1093 task->tk_status = 0;
1094}
1095
1096/*
1097 * 7. Decode the RPC reply
1098 */
1099static void
1100call_decode(struct rpc_task *task)
1101{
1102 struct rpc_clnt *clnt = task->tk_client;
1103 struct rpc_rqst *req = task->tk_rqstp;
1104 kxdrproc_t decode = task->tk_msg.rpc_proc->p_decode;
1105 u32 *p;
1106
1107 dprintk("RPC: %4d call_decode (status %d)\n",
1108 task->tk_pid, task->tk_status);
1109
Chuck Leverf518e35a2006-01-03 09:55:52 +01001110 if (task->tk_flags & RPC_CALL_MAJORSEEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 printk(KERN_NOTICE "%s: server %s OK\n",
1112 clnt->cl_protname, clnt->cl_server);
1113 task->tk_flags &= ~RPC_CALL_MAJORSEEN;
1114 }
1115
1116 if (task->tk_status < 12) {
1117 if (!RPC_IS_SOFT(task)) {
1118 task->tk_action = call_bind;
1119 clnt->cl_stats->rpcretrans++;
1120 goto out_retry;
1121 }
Trond Myklebustda458282006-08-31 15:44:52 -04001122 dprintk("%s: too small RPC reply size (%d bytes)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 clnt->cl_protname, task->tk_status);
Trond Myklebustda458282006-08-31 15:44:52 -04001124 task->tk_action = call_timeout;
1125 goto out_retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 }
1127
Trond Myklebust43ac3f22006-03-20 13:44:51 -05001128 /*
1129 * Ensure that we see all writes made by xprt_complete_rqst()
1130 * before it changed req->rq_received.
1131 */
1132 smp_rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 req->rq_rcv_buf.len = req->rq_private_buf.len;
1134
1135 /* Check that the softirq receive buffer is valid */
1136 WARN_ON(memcmp(&req->rq_rcv_buf, &req->rq_private_buf,
1137 sizeof(req->rq_rcv_buf)) != 0);
1138
1139 /* Verify the RPC header */
Trond Myklebustabbcf282006-01-03 09:55:03 +01001140 p = call_verify(task);
1141 if (IS_ERR(p)) {
1142 if (p == ERR_PTR(-EAGAIN))
1143 goto out_retry;
1144 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 }
1146
Trond Myklebustabbcf282006-01-03 09:55:03 +01001147 task->tk_action = rpc_exit_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
1149 if (decode)
1150 task->tk_status = rpcauth_unwrap_resp(task, decode, req, p,
1151 task->tk_msg.rpc_resp);
1152 dprintk("RPC: %4d call_decode result %d\n", task->tk_pid,
1153 task->tk_status);
1154 return;
1155out_retry:
1156 req->rq_received = req->rq_private_buf.len = 0;
1157 task->tk_status = 0;
1158}
1159
1160/*
1161 * 8. Refresh the credentials if rejected by the server
1162 */
1163static void
1164call_refresh(struct rpc_task *task)
1165{
1166 dprintk("RPC: %4d call_refresh\n", task->tk_pid);
1167
1168 xprt_release(task); /* Must do to obtain new XID */
1169 task->tk_action = call_refreshresult;
1170 task->tk_status = 0;
1171 task->tk_client->cl_stats->rpcauthrefresh++;
1172 rpcauth_refreshcred(task);
1173}
1174
1175/*
1176 * 8a. Process the results of a credential refresh
1177 */
1178static void
1179call_refreshresult(struct rpc_task *task)
1180{
1181 int status = task->tk_status;
1182 dprintk("RPC: %4d call_refreshresult (status %d)\n",
1183 task->tk_pid, task->tk_status);
1184
1185 task->tk_status = 0;
1186 task->tk_action = call_reserve;
1187 if (status >= 0 && rpcauth_uptodatecred(task))
1188 return;
1189 if (status == -EACCES) {
1190 rpc_exit(task, -EACCES);
1191 return;
1192 }
1193 task->tk_action = call_refresh;
1194 if (status != -ETIMEDOUT)
1195 rpc_delay(task, 3*HZ);
1196 return;
1197}
1198
1199/*
1200 * Call header serialization
1201 */
1202static u32 *
1203call_header(struct rpc_task *task)
1204{
1205 struct rpc_clnt *clnt = task->tk_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 struct rpc_rqst *req = task->tk_rqstp;
1207 u32 *p = req->rq_svec[0].iov_base;
1208
1209 /* FIXME: check buffer size? */
Chuck Lever808012f2005-08-25 16:25:49 -07001210
1211 p = xprt_skip_transport_header(task->tk_xprt, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 *p++ = req->rq_xid; /* XID */
1213 *p++ = htonl(RPC_CALL); /* CALL */
1214 *p++ = htonl(RPC_VERSION); /* RPC version */
1215 *p++ = htonl(clnt->cl_prog); /* program number */
1216 *p++ = htonl(clnt->cl_vers); /* program version */
1217 *p++ = htonl(task->tk_msg.rpc_proc->p_proc); /* procedure */
Trond Myklebust334ccfd2005-06-22 17:16:19 +00001218 p = rpcauth_marshcred(task, p);
1219 req->rq_slen = xdr_adjust_iovec(&req->rq_svec[0], p);
1220 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221}
1222
1223/*
1224 * Reply header verification
1225 */
1226static u32 *
1227call_verify(struct rpc_task *task)
1228{
1229 struct kvec *iov = &task->tk_rqstp->rq_rcv_buf.head[0];
1230 int len = task->tk_rqstp->rq_rcv_buf.len >> 2;
1231 u32 *p = iov->iov_base, n;
1232 int error = -EACCES;
1233
David Howellse8896492006-08-24 15:44:19 -04001234 if ((task->tk_rqstp->rq_rcv_buf.len & 3) != 0) {
1235 /* RFC-1014 says that the representation of XDR data must be a
1236 * multiple of four bytes
1237 * - if it isn't pointer subtraction in the NFS client may give
1238 * undefined results
1239 */
1240 printk(KERN_WARNING
1241 "call_verify: XDR representation not a multiple of"
1242 " 4 bytes: 0x%x\n", task->tk_rqstp->rq_rcv_buf.len);
1243 goto out_eio;
1244 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 if ((len -= 3) < 0)
1246 goto out_overflow;
1247 p += 1; /* skip XID */
1248
1249 if ((n = ntohl(*p++)) != RPC_REPLY) {
1250 printk(KERN_WARNING "call_verify: not an RPC reply: %x\n", n);
Trond Myklebustabbcf282006-01-03 09:55:03 +01001251 goto out_garbage;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 }
1253 if ((n = ntohl(*p++)) != RPC_MSG_ACCEPTED) {
1254 if (--len < 0)
1255 goto out_overflow;
1256 switch ((n = ntohl(*p++))) {
1257 case RPC_AUTH_ERROR:
1258 break;
1259 case RPC_MISMATCH:
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00001260 dprintk("%s: RPC call version mismatch!\n", __FUNCTION__);
1261 error = -EPROTONOSUPPORT;
1262 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 default:
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00001264 dprintk("%s: RPC call rejected, unknown error: %x\n", __FUNCTION__, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 goto out_eio;
1266 }
1267 if (--len < 0)
1268 goto out_overflow;
1269 switch ((n = ntohl(*p++))) {
1270 case RPC_AUTH_REJECTEDCRED:
1271 case RPC_AUTH_REJECTEDVERF:
1272 case RPCSEC_GSS_CREDPROBLEM:
1273 case RPCSEC_GSS_CTXPROBLEM:
1274 if (!task->tk_cred_retry)
1275 break;
1276 task->tk_cred_retry--;
1277 dprintk("RPC: %4d call_verify: retry stale creds\n",
1278 task->tk_pid);
1279 rpcauth_invalcred(task);
1280 task->tk_action = call_refresh;
Trond Myklebustabbcf282006-01-03 09:55:03 +01001281 goto out_retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 case RPC_AUTH_BADCRED:
1283 case RPC_AUTH_BADVERF:
1284 /* possibly garbled cred/verf? */
1285 if (!task->tk_garb_retry)
1286 break;
1287 task->tk_garb_retry--;
1288 dprintk("RPC: %4d call_verify: retry garbled creds\n",
1289 task->tk_pid);
1290 task->tk_action = call_bind;
Trond Myklebustabbcf282006-01-03 09:55:03 +01001291 goto out_retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 case RPC_AUTH_TOOWEAK:
Levent Serinol1356b8c2006-03-20 13:44:11 -05001293 printk(KERN_NOTICE "call_verify: server %s requires stronger "
1294 "authentication.\n", task->tk_client->cl_server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 break;
1296 default:
1297 printk(KERN_WARNING "call_verify: unknown auth error: %x\n", n);
1298 error = -EIO;
1299 }
1300 dprintk("RPC: %4d call_verify: call rejected %d\n",
1301 task->tk_pid, n);
1302 goto out_err;
1303 }
1304 if (!(p = rpcauth_checkverf(task, p))) {
1305 printk(KERN_WARNING "call_verify: auth check failed\n");
Trond Myklebustabbcf282006-01-03 09:55:03 +01001306 goto out_garbage; /* bad verifier, retry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 }
1308 len = p - (u32 *)iov->iov_base - 1;
1309 if (len < 0)
1310 goto out_overflow;
1311 switch ((n = ntohl(*p++))) {
1312 case RPC_SUCCESS:
1313 return p;
1314 case RPC_PROG_UNAVAIL:
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00001315 dprintk("RPC: call_verify: program %u is unsupported by server %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 (unsigned int)task->tk_client->cl_prog,
1317 task->tk_client->cl_server);
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00001318 error = -EPFNOSUPPORT;
1319 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 case RPC_PROG_MISMATCH:
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00001321 dprintk("RPC: call_verify: program %u, version %u unsupported by server %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 (unsigned int)task->tk_client->cl_prog,
1323 (unsigned int)task->tk_client->cl_vers,
1324 task->tk_client->cl_server);
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00001325 error = -EPROTONOSUPPORT;
1326 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 case RPC_PROC_UNAVAIL:
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00001328 dprintk("RPC: call_verify: proc %p unsupported by program %u, version %u on server %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 task->tk_msg.rpc_proc,
1330 task->tk_client->cl_prog,
1331 task->tk_client->cl_vers,
1332 task->tk_client->cl_server);
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00001333 error = -EOPNOTSUPP;
1334 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 case RPC_GARBAGE_ARGS:
1336 dprintk("RPC: %4d %s: server saw garbage\n", task->tk_pid, __FUNCTION__);
1337 break; /* retry */
1338 default:
1339 printk(KERN_WARNING "call_verify: server accept status: %x\n", n);
1340 /* Also retry */
1341 }
1342
Trond Myklebustabbcf282006-01-03 09:55:03 +01001343out_garbage:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 task->tk_client->cl_stats->rpcgarbage++;
1345 if (task->tk_garb_retry) {
1346 task->tk_garb_retry--;
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00001347 dprintk("RPC %s: retrying %4d\n", __FUNCTION__, task->tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 task->tk_action = call_bind;
Trond Myklebustabbcf282006-01-03 09:55:03 +01001349out_retry:
1350 return ERR_PTR(-EAGAIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 }
1352 printk(KERN_WARNING "RPC %s: retry failed, exit EIO\n", __FUNCTION__);
1353out_eio:
1354 error = -EIO;
1355out_err:
1356 rpc_exit(task, error);
Trond Myklebustabbcf282006-01-03 09:55:03 +01001357 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358out_overflow:
1359 printk(KERN_WARNING "RPC %s: server reply was truncated.\n", __FUNCTION__);
Trond Myklebustabbcf282006-01-03 09:55:03 +01001360 goto out_garbage;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361}
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00001362
1363static int rpcproc_encode_null(void *rqstp, u32 *data, void *obj)
1364{
1365 return 0;
1366}
1367
1368static int rpcproc_decode_null(void *rqstp, u32 *data, void *obj)
1369{
1370 return 0;
1371}
1372
1373static struct rpc_procinfo rpcproc_null = {
1374 .p_encode = rpcproc_encode_null,
1375 .p_decode = rpcproc_decode_null,
1376};
1377
1378int rpc_ping(struct rpc_clnt *clnt, int flags)
1379{
1380 struct rpc_message msg = {
1381 .rpc_proc = &rpcproc_null,
1382 };
1383 int err;
1384 msg.rpc_cred = authnull_ops.lookup_cred(NULL, NULL, 0);
1385 err = rpc_call_sync(clnt, &msg, flags);
1386 put_rpccred(msg.rpc_cred);
1387 return err;
1388}