blob: 0b8d03d0856112541e15bd5851f708363869035b [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
100/*
101 * Create an RPC client
102 * FIXME: This should also take a flags argument (as in task->tk_flags).
103 * It's called (among others) from pmap_create_client, which may in
104 * turn be called by an async task. In this case, rpciod should not be
105 * made to sleep too long.
106 */
107struct rpc_clnt *
Trond Myklebust5ee0ed72005-06-22 17:16:20 +0000108rpc_new_client(struct rpc_xprt *xprt, char *servname,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 struct rpc_program *program, u32 vers,
110 rpc_authflavor_t flavor)
111{
112 struct rpc_version *version;
113 struct rpc_clnt *clnt = NULL;
J. Bruce Fields6a192752005-06-22 17:16:23 +0000114 struct rpc_auth *auth;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 int err;
116 int len;
117
118 dprintk("RPC: creating %s client for %s (xprt %p)\n",
119 program->name, servname, xprt);
120
121 err = -EINVAL;
122 if (!xprt)
Adrian Bunk712917d2006-03-13 21:20:47 -0800123 goto out_no_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 if (vers >= program->nrvers || !(version = program->version[vers]))
125 goto out_err;
126
127 err = -ENOMEM;
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700128 clnt = kzalloc(sizeof(*clnt), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 if (!clnt)
130 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 atomic_set(&clnt->cl_users, 0);
132 atomic_set(&clnt->cl_count, 1);
133 clnt->cl_parent = clnt;
134
135 clnt->cl_server = clnt->cl_inline_name;
136 len = strlen(servname) + 1;
137 if (len > sizeof(clnt->cl_inline_name)) {
138 char *buf = kmalloc(len, GFP_KERNEL);
139 if (buf != 0)
140 clnt->cl_server = buf;
141 else
142 len = sizeof(clnt->cl_inline_name);
143 }
144 strlcpy(clnt->cl_server, servname, len);
145
146 clnt->cl_xprt = xprt;
147 clnt->cl_procinfo = version->procs;
148 clnt->cl_maxproc = version->nrprocs;
149 clnt->cl_protname = program->name;
150 clnt->cl_pmap = &clnt->cl_pmap_default;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 clnt->cl_prog = program->number;
152 clnt->cl_vers = version->number;
153 clnt->cl_prot = xprt->prot;
154 clnt->cl_stats = program->stats;
Chuck Lever11c556b2006-03-20 13:44:22 -0500155 clnt->cl_metrics = rpc_alloc_iostats(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 rpc_init_wait_queue(&clnt->cl_pmap_default.pm_bindwait, "bindwait");
157
Chuck Leverec739ef2006-08-22 20:06:15 -0400158 if (!xprt_bound(clnt->cl_xprt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 clnt->cl_autobind = 1;
160
161 clnt->cl_rtt = &clnt->cl_rtt_default;
162 rpc_init_rtt(&clnt->cl_rtt_default, xprt->timeout.to_initval);
163
164 err = rpc_setup_pipedir(clnt, program->pipe_dir_name);
165 if (err < 0)
166 goto out_no_path;
167
J. Bruce Fields6a192752005-06-22 17:16:23 +0000168 auth = rpcauth_create(flavor, clnt);
169 if (IS_ERR(auth)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 printk(KERN_INFO "RPC: Couldn't create auth handle (flavor %u)\n",
171 flavor);
J. Bruce Fields6a192752005-06-22 17:16:23 +0000172 err = PTR_ERR(auth);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 goto out_no_auth;
174 }
175
176 /* save the nodename */
177 clnt->cl_nodelen = strlen(system_utsname.nodename);
178 if (clnt->cl_nodelen > UNX_MAXNODENAME)
179 clnt->cl_nodelen = UNX_MAXNODENAME;
180 memcpy(clnt->cl_nodename, system_utsname.nodename, clnt->cl_nodelen);
181 return clnt;
182
183out_no_auth:
Trond Myklebust54281542006-03-20 13:44:49 -0500184 if (!IS_ERR(clnt->cl_dentry)) {
Trond Myklebustdff02cc2006-07-31 14:17:18 -0700185 rpc_rmdir(clnt->cl_dentry);
Trond Myklebust54281542006-03-20 13:44:49 -0500186 rpc_put_mount();
187 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188out_no_path:
189 if (clnt->cl_server != clnt->cl_inline_name)
190 kfree(clnt->cl_server);
191 kfree(clnt);
192out_err:
Trond Myklebust5b616f52005-06-22 17:16:20 +0000193 xprt_destroy(xprt);
Adrian Bunk712917d2006-03-13 21:20:47 -0800194out_no_xprt:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 return ERR_PTR(err);
196}
197
Trond Myklebust5ee0ed72005-06-22 17:16:20 +0000198/**
199 * Create an RPC client
200 * @xprt - pointer to xprt struct
201 * @servname - name of server
202 * @info - rpc_program
203 * @version - rpc_program version
204 * @authflavor - rpc_auth flavour to use
205 *
206 * Creates an RPC client structure, then pings the server in order to
207 * determine if it is up, and if it supports this program and version.
208 *
209 * This function should never be called by asynchronous tasks such as
210 * the portmapper.
211 */
212struct rpc_clnt *rpc_create_client(struct rpc_xprt *xprt, char *servname,
213 struct rpc_program *info, u32 version, rpc_authflavor_t authflavor)
214{
215 struct rpc_clnt *clnt;
216 int err;
217
218 clnt = rpc_new_client(xprt, servname, info, version, authflavor);
219 if (IS_ERR(clnt))
220 return clnt;
221 err = rpc_ping(clnt, RPC_TASK_SOFT|RPC_TASK_NOINTR);
222 if (err == 0)
223 return clnt;
224 rpc_shutdown_client(clnt);
225 return ERR_PTR(err);
226}
227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228/*
229 * This function clones the RPC client structure. It allows us to share the
230 * same transport while varying parameters such as the authentication
231 * flavour.
232 */
233struct rpc_clnt *
234rpc_clone_client(struct rpc_clnt *clnt)
235{
236 struct rpc_clnt *new;
237
Kris Katterjohn8b3a7002006-01-11 15:56:43 -0800238 new = kmalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 if (!new)
240 goto out_no_clnt;
241 memcpy(new, clnt, sizeof(*new));
242 atomic_set(&new->cl_count, 1);
243 atomic_set(&new->cl_users, 0);
244 new->cl_parent = clnt;
245 atomic_inc(&clnt->cl_count);
246 /* Duplicate portmapper */
247 rpc_init_wait_queue(&new->cl_pmap_default.pm_bindwait, "bindwait");
248 /* Turn off autobind on clones */
249 new->cl_autobind = 0;
250 new->cl_oneshot = 0;
251 new->cl_dead = 0;
Trond Myklebust8f8e7a52006-08-14 13:11:15 -0400252 if (!IS_ERR(new->cl_dentry))
Trond Myklebust54281542006-03-20 13:44:49 -0500253 dget(new->cl_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 rpc_init_rtt(&new->cl_rtt_default, clnt->cl_xprt->timeout.to_initval);
255 if (new->cl_auth)
256 atomic_inc(&new->cl_auth->au_count);
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000257 new->cl_pmap = &new->cl_pmap_default;
Chuck Lever11c556b2006-03-20 13:44:22 -0500258 new->cl_metrics = rpc_alloc_iostats(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 return new;
260out_no_clnt:
261 printk(KERN_INFO "RPC: out of memory in %s\n", __FUNCTION__);
262 return ERR_PTR(-ENOMEM);
263}
264
265/*
266 * Properly shut down an RPC client, terminating all outstanding
267 * requests. Note that we must be certain that cl_oneshot and
268 * cl_dead are cleared, or else the client would be destroyed
269 * when the last task releases it.
270 */
271int
272rpc_shutdown_client(struct rpc_clnt *clnt)
273{
274 dprintk("RPC: shutting down %s client for %s, tasks=%d\n",
275 clnt->cl_protname, clnt->cl_server,
276 atomic_read(&clnt->cl_users));
277
278 while (atomic_read(&clnt->cl_users) > 0) {
279 /* Don't let rpc_release_client destroy us */
280 clnt->cl_oneshot = 0;
281 clnt->cl_dead = 0;
282 rpc_killall_tasks(clnt);
Ingo Molnar532347e2006-01-09 20:52:53 -0800283 wait_event_timeout(destroy_wait,
Linus Torvalds4f477072006-01-10 08:56:39 -0800284 !atomic_read(&clnt->cl_users), 1*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 }
286
287 if (atomic_read(&clnt->cl_users) < 0) {
288 printk(KERN_ERR "RPC: rpc_shutdown_client clnt %p tasks=%d\n",
289 clnt, atomic_read(&clnt->cl_users));
290#ifdef RPC_DEBUG
291 rpc_show_tasks();
292#endif
293 BUG();
294 }
295
296 return rpc_destroy_client(clnt);
297}
298
299/*
300 * Delete an RPC client
301 */
302int
303rpc_destroy_client(struct rpc_clnt *clnt)
304{
305 if (!atomic_dec_and_test(&clnt->cl_count))
306 return 1;
307 BUG_ON(atomic_read(&clnt->cl_users) != 0);
308
309 dprintk("RPC: destroying %s client for %s\n",
310 clnt->cl_protname, clnt->cl_server);
311 if (clnt->cl_auth) {
312 rpcauth_destroy(clnt->cl_auth);
313 clnt->cl_auth = NULL;
314 }
315 if (clnt->cl_parent != clnt) {
Trond Myklebust8f8e7a52006-08-14 13:11:15 -0400316 if (!IS_ERR(clnt->cl_dentry))
317 dput(clnt->cl_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 rpc_destroy_client(clnt->cl_parent);
319 goto out_free;
320 }
Trond Myklebust8f8e7a52006-08-14 13:11:15 -0400321 if (!IS_ERR(clnt->cl_dentry)) {
Trond Myklebustdff02cc2006-07-31 14:17:18 -0700322 rpc_rmdir(clnt->cl_dentry);
Trond Myklebust8f8e7a52006-08-14 13:11:15 -0400323 rpc_put_mount();
324 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 if (clnt->cl_xprt) {
326 xprt_destroy(clnt->cl_xprt);
327 clnt->cl_xprt = NULL;
328 }
329 if (clnt->cl_server != clnt->cl_inline_name)
330 kfree(clnt->cl_server);
331out_free:
Chuck Lever11c556b2006-03-20 13:44:22 -0500332 rpc_free_iostats(clnt->cl_metrics);
333 clnt->cl_metrics = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 kfree(clnt);
335 return 0;
336}
337
338/*
339 * Release an RPC client
340 */
341void
342rpc_release_client(struct rpc_clnt *clnt)
343{
344 dprintk("RPC: rpc_release_client(%p, %d)\n",
345 clnt, atomic_read(&clnt->cl_users));
346
347 if (!atomic_dec_and_test(&clnt->cl_users))
348 return;
349 wake_up(&destroy_wait);
350 if (clnt->cl_oneshot || clnt->cl_dead)
351 rpc_destroy_client(clnt);
352}
353
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000354/**
355 * rpc_bind_new_program - bind a new RPC program to an existing client
356 * @old - old rpc_client
357 * @program - rpc program to set
358 * @vers - rpc program version
359 *
360 * Clones the rpc client and sets up a new RPC program. This is mainly
361 * of use for enabling different RPC programs to share the same transport.
362 * The Sun NFSv2/v3 ACL protocol can do this.
363 */
364struct rpc_clnt *rpc_bind_new_program(struct rpc_clnt *old,
365 struct rpc_program *program,
366 int vers)
367{
368 struct rpc_clnt *clnt;
369 struct rpc_version *version;
370 int err;
371
372 BUG_ON(vers >= program->nrvers || !program->version[vers]);
373 version = program->version[vers];
374 clnt = rpc_clone_client(old);
375 if (IS_ERR(clnt))
376 goto out;
377 clnt->cl_procinfo = version->procs;
378 clnt->cl_maxproc = version->nrprocs;
379 clnt->cl_protname = program->name;
380 clnt->cl_prog = program->number;
381 clnt->cl_vers = version->number;
382 clnt->cl_stats = program->stats;
383 err = rpc_ping(clnt, RPC_TASK_SOFT|RPC_TASK_NOINTR);
384 if (err != 0) {
385 rpc_shutdown_client(clnt);
386 clnt = ERR_PTR(err);
387 }
388out:
389 return clnt;
390}
391
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392/*
393 * Default callback for async RPC calls
394 */
395static void
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100396rpc_default_callback(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397{
398}
399
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100400static const struct rpc_call_ops rpc_default_ops = {
401 .rpc_call_done = rpc_default_callback,
402};
403
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404/*
Trond Myklebust14b218a2005-06-22 17:16:28 +0000405 * Export the signal mask handling for synchronous code that
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 * sleeps on RPC calls
407 */
Trond Myklebust2bd61572006-01-03 09:55:19 +0100408#define RPC_INTR_SIGNALS (sigmask(SIGHUP) | sigmask(SIGINT) | sigmask(SIGQUIT) | sigmask(SIGTERM))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
Trond Myklebust14b218a2005-06-22 17:16:28 +0000410static void rpc_save_sigmask(sigset_t *oldset, int intr)
411{
Trond Myklebust2bd61572006-01-03 09:55:19 +0100412 unsigned long sigallow = sigmask(SIGKILL);
Trond Myklebust14b218a2005-06-22 17:16:28 +0000413 sigset_t sigmask;
414
415 /* Block all signals except those listed in sigallow */
416 if (intr)
417 sigallow |= RPC_INTR_SIGNALS;
418 siginitsetinv(&sigmask, sigallow);
419 sigprocmask(SIG_BLOCK, &sigmask, oldset);
420}
421
422static inline void rpc_task_sigmask(struct rpc_task *task, sigset_t *oldset)
423{
424 rpc_save_sigmask(oldset, !RPC_TASK_UNINTERRUPTIBLE(task));
425}
426
427static inline void rpc_restore_sigmask(sigset_t *oldset)
428{
429 sigprocmask(SIG_SETMASK, oldset, NULL);
430}
431
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432void rpc_clnt_sigmask(struct rpc_clnt *clnt, sigset_t *oldset)
433{
Trond Myklebust14b218a2005-06-22 17:16:28 +0000434 rpc_save_sigmask(oldset, clnt->cl_intr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435}
436
437void rpc_clnt_sigunmask(struct rpc_clnt *clnt, sigset_t *oldset)
438{
Trond Myklebust14b218a2005-06-22 17:16:28 +0000439 rpc_restore_sigmask(oldset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440}
441
442/*
443 * New rpc_call implementation
444 */
445int rpc_call_sync(struct rpc_clnt *clnt, struct rpc_message *msg, int flags)
446{
447 struct rpc_task *task;
448 sigset_t oldset;
449 int status;
450
451 /* If this client is slain all further I/O fails */
452 if (clnt->cl_dead)
453 return -EIO;
454
455 BUG_ON(flags & RPC_TASK_ASYNC);
456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 status = -ENOMEM;
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100458 task = rpc_new_task(clnt, flags, &rpc_default_ops, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 if (task == NULL)
460 goto out;
461
Trond Myklebust14b218a2005-06-22 17:16:28 +0000462 /* Mask signals on RPC calls _and_ GSS_AUTH upcalls */
463 rpc_task_sigmask(task, &oldset);
464
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 rpc_call_setup(task, msg, 0);
466
467 /* Set up the call info struct and execute the task */
Trond Myklebuste60859a2006-01-03 09:55:10 +0100468 status = task->tk_status;
469 if (status == 0) {
470 atomic_inc(&task->tk_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 status = rpc_execute(task);
Trond Myklebuste60859a2006-01-03 09:55:10 +0100472 if (status == 0)
473 status = task->tk_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 }
Trond Myklebust14b218a2005-06-22 17:16:28 +0000475 rpc_restore_sigmask(&oldset);
Trond Myklebuste60859a2006-01-03 09:55:10 +0100476 rpc_release_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 return status;
479}
480
481/*
482 * New rpc_call implementation
483 */
484int
485rpc_call_async(struct rpc_clnt *clnt, struct rpc_message *msg, int flags,
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100486 const struct rpc_call_ops *tk_ops, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
488 struct rpc_task *task;
489 sigset_t oldset;
490 int status;
491
492 /* If this client is slain all further I/O fails */
Trond Myklebust7a1218a2006-03-20 18:11:10 -0500493 status = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 if (clnt->cl_dead)
Trond Myklebust7a1218a2006-03-20 18:11:10 -0500495 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
497 flags |= RPC_TASK_ASYNC;
498
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 /* Create/initialize a new RPC task */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 status = -ENOMEM;
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100501 if (!(task = rpc_new_task(clnt, flags, tk_ops, data)))
Trond Myklebust7a1218a2006-03-20 18:11:10 -0500502 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
Trond Myklebust14b218a2005-06-22 17:16:28 +0000504 /* Mask signals on GSS_AUTH upcalls */
505 rpc_task_sigmask(task, &oldset);
506
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 rpc_call_setup(task, msg, 0);
508
509 /* Set up the call info struct and execute the task */
510 status = task->tk_status;
511 if (status == 0)
512 rpc_execute(task);
513 else
514 rpc_release_task(task);
515
Trond Myklebust14b218a2005-06-22 17:16:28 +0000516 rpc_restore_sigmask(&oldset);
Trond Myklebust7a1218a2006-03-20 18:11:10 -0500517 return status;
518out_release:
519 if (tk_ops->rpc_release != NULL)
520 tk_ops->rpc_release(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 return status;
522}
523
524
525void
526rpc_call_setup(struct rpc_task *task, struct rpc_message *msg, int flags)
527{
528 task->tk_msg = *msg;
529 task->tk_flags |= flags;
530 /* Bind the user cred */
531 if (task->tk_msg.rpc_cred != NULL)
532 rpcauth_holdcred(task);
533 else
534 rpcauth_bindcred(task);
535
536 if (task->tk_status == 0)
537 task->tk_action = call_start;
538 else
Trond Myklebustabbcf282006-01-03 09:55:03 +0100539 task->tk_action = rpc_exit_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540}
541
542void
543rpc_setbufsize(struct rpc_clnt *clnt, unsigned int sndsize, unsigned int rcvsize)
544{
545 struct rpc_xprt *xprt = clnt->cl_xprt;
Chuck Lever470056c2005-08-25 16:25:56 -0700546 if (xprt->ops->set_buffer_size)
547 xprt->ops->set_buffer_size(xprt, sndsize, rcvsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548}
549
550/*
551 * Return size of largest payload RPC client can support, in bytes
552 *
553 * For stream transports, this is one RPC record fragment (see RFC
554 * 1831), as we don't support multi-record requests yet. For datagram
555 * transports, this is the size of an IP packet minus the IP, UDP, and
556 * RPC header sizes.
557 */
558size_t rpc_max_payload(struct rpc_clnt *clnt)
559{
560 return clnt->cl_xprt->max_payload;
561}
562EXPORT_SYMBOL(rpc_max_payload);
563
Chuck Lever35f5a422006-01-03 09:55:50 +0100564/**
565 * rpc_force_rebind - force transport to check that remote port is unchanged
566 * @clnt: client to rebind
567 *
568 */
569void rpc_force_rebind(struct rpc_clnt *clnt)
570{
571 if (clnt->cl_autobind)
Chuck Leverec739ef2006-08-22 20:06:15 -0400572 xprt_clear_bound(clnt->cl_xprt);
Chuck Lever35f5a422006-01-03 09:55:50 +0100573}
574EXPORT_SYMBOL(rpc_force_rebind);
575
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576/*
577 * Restart an (async) RPC call. Usually called from within the
578 * exit handler.
579 */
580void
581rpc_restart_call(struct rpc_task *task)
582{
583 if (RPC_ASSASSINATED(task))
584 return;
585
586 task->tk_action = call_start;
587}
588
589/*
590 * 0. Initial state
591 *
592 * Other FSM states can be visited zero or more times, but
593 * this state is visited exactly once for each RPC.
594 */
595static void
596call_start(struct rpc_task *task)
597{
598 struct rpc_clnt *clnt = task->tk_client;
599
600 dprintk("RPC: %4d call_start %s%d proc %d (%s)\n", task->tk_pid,
601 clnt->cl_protname, clnt->cl_vers, task->tk_msg.rpc_proc->p_proc,
602 (RPC_IS_ASYNC(task) ? "async" : "sync"));
603
604 /* Increment call count */
605 task->tk_msg.rpc_proc->p_count++;
606 clnt->cl_stats->rpccnt++;
607 task->tk_action = call_reserve;
608}
609
610/*
611 * 1. Reserve an RPC call slot
612 */
613static void
614call_reserve(struct rpc_task *task)
615{
616 dprintk("RPC: %4d call_reserve\n", task->tk_pid);
617
618 if (!rpcauth_uptodatecred(task)) {
619 task->tk_action = call_refresh;
620 return;
621 }
622
623 task->tk_status = 0;
624 task->tk_action = call_reserveresult;
625 xprt_reserve(task);
626}
627
628/*
629 * 1b. Grok the result of xprt_reserve()
630 */
631static void
632call_reserveresult(struct rpc_task *task)
633{
634 int status = task->tk_status;
635
636 dprintk("RPC: %4d call_reserveresult (status %d)\n",
637 task->tk_pid, task->tk_status);
638
639 /*
640 * After a call to xprt_reserve(), we must have either
641 * a request slot or else an error status.
642 */
643 task->tk_status = 0;
644 if (status >= 0) {
645 if (task->tk_rqstp) {
646 task->tk_action = call_allocate;
647 return;
648 }
649
650 printk(KERN_ERR "%s: status=%d, but no request slot, exiting\n",
651 __FUNCTION__, status);
652 rpc_exit(task, -EIO);
653 return;
654 }
655
656 /*
657 * Even though there was an error, we may have acquired
658 * a request slot somehow. Make sure not to leak it.
659 */
660 if (task->tk_rqstp) {
661 printk(KERN_ERR "%s: status=%d, request allocated anyway\n",
662 __FUNCTION__, status);
663 xprt_release(task);
664 }
665
666 switch (status) {
667 case -EAGAIN: /* woken up; retry */
668 task->tk_action = call_reserve;
669 return;
670 case -EIO: /* probably a shutdown */
671 break;
672 default:
673 printk(KERN_ERR "%s: unrecognized error %d, exiting\n",
674 __FUNCTION__, status);
675 break;
676 }
677 rpc_exit(task, status);
678}
679
680/*
681 * 2. Allocate the buffer. For details, see sched.c:rpc_malloc.
Chuck Lever02107142006-01-03 09:55:49 +0100682 * (Note: buffer memory is freed in xprt_release).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 */
684static void
685call_allocate(struct rpc_task *task)
686{
Chuck Lever02107142006-01-03 09:55:49 +0100687 struct rpc_rqst *req = task->tk_rqstp;
688 struct rpc_xprt *xprt = task->tk_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 unsigned int bufsiz;
690
691 dprintk("RPC: %4d call_allocate (status %d)\n",
692 task->tk_pid, task->tk_status);
693 task->tk_action = call_bind;
Chuck Lever02107142006-01-03 09:55:49 +0100694 if (req->rq_buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 return;
696
697 /* FIXME: compute buffer requirements more exactly using
698 * auth->au_wslack */
699 bufsiz = task->tk_msg.rpc_proc->p_bufsiz + RPC_SLACK_SPACE;
700
Chuck Lever02107142006-01-03 09:55:49 +0100701 if (xprt->ops->buf_alloc(task, bufsiz << 1) != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 return;
703 printk(KERN_INFO "RPC: buffer allocation failed for task %p\n", task);
704
Trond Myklebust14b218a2005-06-22 17:16:28 +0000705 if (RPC_IS_ASYNC(task) || !signalled()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 xprt_release(task);
707 task->tk_action = call_reserve;
708 rpc_delay(task, HZ>>4);
709 return;
710 }
711
712 rpc_exit(task, -ERESTARTSYS);
713}
714
Trond Myklebust940e3312005-11-09 21:45:24 -0500715static inline int
716rpc_task_need_encode(struct rpc_task *task)
717{
718 return task->tk_rqstp->rq_snd_buf.len == 0;
719}
720
721static inline void
722rpc_task_force_reencode(struct rpc_task *task)
723{
724 task->tk_rqstp->rq_snd_buf.len = 0;
725}
726
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727/*
728 * 3. Encode arguments of an RPC call
729 */
730static void
731call_encode(struct rpc_task *task)
732{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 struct rpc_rqst *req = task->tk_rqstp;
734 struct xdr_buf *sndbuf = &req->rq_snd_buf;
735 struct xdr_buf *rcvbuf = &req->rq_rcv_buf;
736 unsigned int bufsiz;
737 kxdrproc_t encode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 u32 *p;
739
740 dprintk("RPC: %4d call_encode (status %d)\n",
741 task->tk_pid, task->tk_status);
742
743 /* Default buffer setup */
Chuck Lever02107142006-01-03 09:55:49 +0100744 bufsiz = req->rq_bufsize >> 1;
745 sndbuf->head[0].iov_base = (void *)req->rq_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 sndbuf->head[0].iov_len = bufsiz;
747 sndbuf->tail[0].iov_len = 0;
748 sndbuf->page_len = 0;
749 sndbuf->len = 0;
750 sndbuf->buflen = bufsiz;
Chuck Lever02107142006-01-03 09:55:49 +0100751 rcvbuf->head[0].iov_base = (void *)((char *)req->rq_buffer + bufsiz);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 rcvbuf->head[0].iov_len = bufsiz;
753 rcvbuf->tail[0].iov_len = 0;
754 rcvbuf->page_len = 0;
755 rcvbuf->len = 0;
756 rcvbuf->buflen = bufsiz;
757
758 /* Encode header and provided arguments */
759 encode = task->tk_msg.rpc_proc->p_encode;
760 if (!(p = call_header(task))) {
761 printk(KERN_INFO "RPC: call_header failed, exit EIO\n");
762 rpc_exit(task, -EIO);
763 return;
764 }
J. Bruce Fieldsf3680312005-10-13 16:54:48 -0400765 if (encode == NULL)
766 return;
767
768 task->tk_status = rpcauth_wrap_req(task, encode, req, p,
769 task->tk_msg.rpc_argp);
770 if (task->tk_status == -ENOMEM) {
771 /* XXX: Is this sane? */
772 rpc_delay(task, 3*HZ);
773 task->tk_status = -EAGAIN;
774 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775}
776
777/*
778 * 4. Get the server port number if not yet set
779 */
780static void
781call_bind(struct rpc_task *task)
782{
783 struct rpc_clnt *clnt = task->tk_client;
Chuck Leverec739ef2006-08-22 20:06:15 -0400784 struct rpc_xprt *xprt = task->tk_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
Chuck Leverda351872005-08-11 16:25:11 -0400786 dprintk("RPC: %4d call_bind (status %d)\n",
787 task->tk_pid, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
Chuck Leverda351872005-08-11 16:25:11 -0400789 task->tk_action = call_connect;
Chuck Leverec739ef2006-08-22 20:06:15 -0400790 if (!xprt_bound(xprt)) {
Chuck Leverda351872005-08-11 16:25:11 -0400791 task->tk_action = call_bind_status;
Chuck Leverec739ef2006-08-22 20:06:15 -0400792 task->tk_timeout = xprt->bind_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 rpc_getport(task, clnt);
794 }
795}
796
797/*
Chuck Leverda351872005-08-11 16:25:11 -0400798 * 4a. Sort out bind result
799 */
800static void
801call_bind_status(struct rpc_task *task)
802{
803 int status = -EACCES;
804
805 if (task->tk_status >= 0) {
806 dprintk("RPC: %4d call_bind_status (status %d)\n",
807 task->tk_pid, task->tk_status);
808 task->tk_status = 0;
809 task->tk_action = call_connect;
810 return;
811 }
812
813 switch (task->tk_status) {
814 case -EACCES:
815 dprintk("RPC: %4d remote rpcbind: RPC program/version unavailable\n",
816 task->tk_pid);
Chuck Leverea635a52005-10-06 23:12:58 -0400817 rpc_delay(task, 3*HZ);
818 goto retry_bind;
Chuck Leverda351872005-08-11 16:25:11 -0400819 case -ETIMEDOUT:
820 dprintk("RPC: %4d rpcbind request timed out\n",
821 task->tk_pid);
822 if (RPC_IS_SOFT(task)) {
823 status = -EIO;
824 break;
825 }
826 goto retry_bind;
827 case -EPFNOSUPPORT:
828 dprintk("RPC: %4d remote rpcbind service unavailable\n",
829 task->tk_pid);
830 break;
831 case -EPROTONOSUPPORT:
832 dprintk("RPC: %4d remote rpcbind version 2 unavailable\n",
833 task->tk_pid);
834 break;
835 default:
836 dprintk("RPC: %4d unrecognized rpcbind error (%d)\n",
837 task->tk_pid, -task->tk_status);
838 status = -EIO;
839 break;
840 }
841
842 rpc_exit(task, status);
843 return;
844
845retry_bind:
846 task->tk_status = 0;
847 task->tk_action = call_bind;
848 return;
849}
850
851/*
852 * 4b. Connect to the RPC server
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 */
854static void
855call_connect(struct rpc_task *task)
856{
Chuck Leverda351872005-08-11 16:25:11 -0400857 struct rpc_xprt *xprt = task->tk_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
Chuck Leverda351872005-08-11 16:25:11 -0400859 dprintk("RPC: %4d call_connect xprt %p %s connected\n",
860 task->tk_pid, xprt,
861 (xprt_connected(xprt) ? "is" : "is not"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
Chuck Leverda351872005-08-11 16:25:11 -0400863 task->tk_action = call_transmit;
864 if (!xprt_connected(xprt)) {
865 task->tk_action = call_connect_status;
866 if (task->tk_status < 0)
867 return;
868 xprt_connect(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870}
871
872/*
Chuck Leverda351872005-08-11 16:25:11 -0400873 * 4c. Sort out connect result
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 */
875static void
876call_connect_status(struct rpc_task *task)
877{
878 struct rpc_clnt *clnt = task->tk_client;
879 int status = task->tk_status;
880
Chuck Leverda351872005-08-11 16:25:11 -0400881 dprintk("RPC: %5u call_connect_status (status %d)\n",
882 task->tk_pid, task->tk_status);
883
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 task->tk_status = 0;
885 if (status >= 0) {
886 clnt->cl_stats->netreconn++;
887 task->tk_action = call_transmit;
888 return;
889 }
890
Chuck Leverda351872005-08-11 16:25:11 -0400891 /* Something failed: remote service port may have changed */
Chuck Lever35f5a422006-01-03 09:55:50 +0100892 rpc_force_rebind(clnt);
Chuck Leverda351872005-08-11 16:25:11 -0400893
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 switch (status) {
895 case -ENOTCONN:
896 case -ETIMEDOUT:
897 case -EAGAIN:
Chuck Leverda351872005-08-11 16:25:11 -0400898 task->tk_action = call_bind;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 break;
900 default:
901 rpc_exit(task, -EIO);
Chuck Leverda351872005-08-11 16:25:11 -0400902 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 }
904}
905
906/*
907 * 5. Transmit the RPC request, and wait for reply
908 */
909static void
910call_transmit(struct rpc_task *task)
911{
912 dprintk("RPC: %4d call_transmit (status %d)\n",
913 task->tk_pid, task->tk_status);
914
915 task->tk_action = call_status;
916 if (task->tk_status < 0)
917 return;
918 task->tk_status = xprt_prepare_transmit(task);
919 if (task->tk_status != 0)
920 return;
Trond Myklebuste0ab53d2006-07-27 17:22:50 -0400921 task->tk_action = call_transmit_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 /* Encode here so that rpcsec_gss can use correct sequence number. */
Trond Myklebust940e3312005-11-09 21:45:24 -0500923 if (rpc_task_need_encode(task)) {
Trond Myklebuste0ab53d2006-07-27 17:22:50 -0400924 BUG_ON(task->tk_rqstp->rq_bytes_sent != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 call_encode(task);
Trond Myklebust5e5ce5b2005-10-18 14:20:11 -0700926 /* Did the encode result in an error condition? */
927 if (task->tk_status != 0)
Trond Myklebuste0ab53d2006-07-27 17:22:50 -0400928 return;
Trond Myklebust5e5ce5b2005-10-18 14:20:11 -0700929 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 xprt_transmit(task);
931 if (task->tk_status < 0)
932 return;
Trond Myklebuste0ab53d2006-07-27 17:22:50 -0400933 /*
934 * On success, ensure that we call xprt_end_transmit() before sleeping
935 * in order to allow access to the socket to other RPC requests.
936 */
937 call_transmit_status(task);
938 if (task->tk_msg.rpc_proc->p_decode != NULL)
939 return;
940 task->tk_action = rpc_exit_task;
941 rpc_wake_up_task(task);
942}
943
944/*
945 * 5a. Handle cleanup after a transmission
946 */
947static void
948call_transmit_status(struct rpc_task *task)
949{
950 task->tk_action = call_status;
951 /*
952 * Special case: if we've been waiting on the socket's write_space()
953 * callback, then don't call xprt_end_transmit().
954 */
955 if (task->tk_status == -EAGAIN)
956 return;
957 xprt_end_transmit(task);
Trond Myklebust940e3312005-11-09 21:45:24 -0500958 rpc_task_force_reencode(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959}
960
961/*
962 * 6. Sort out the RPC call status
963 */
964static void
965call_status(struct rpc_task *task)
966{
967 struct rpc_clnt *clnt = task->tk_client;
968 struct rpc_rqst *req = task->tk_rqstp;
969 int status;
970
971 if (req->rq_received > 0 && !req->rq_bytes_sent)
972 task->tk_status = req->rq_received;
973
974 dprintk("RPC: %4d call_status (status %d)\n",
975 task->tk_pid, task->tk_status);
976
977 status = task->tk_status;
978 if (status >= 0) {
979 task->tk_action = call_decode;
980 return;
981 }
982
983 task->tk_status = 0;
984 switch(status) {
985 case -ETIMEDOUT:
986 task->tk_action = call_timeout;
987 break;
988 case -ECONNREFUSED:
989 case -ENOTCONN:
Chuck Lever35f5a422006-01-03 09:55:50 +0100990 rpc_force_rebind(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 task->tk_action = call_bind;
992 break;
993 case -EAGAIN:
994 task->tk_action = call_transmit;
995 break;
996 case -EIO:
997 /* shutdown or soft timeout */
998 rpc_exit(task, status);
999 break;
1000 default:
Chuck Leverf518e35a2006-01-03 09:55:52 +01001001 printk("%s: RPC call returned error %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 clnt->cl_protname, -status);
1003 rpc_exit(task, status);
1004 break;
1005 }
1006}
1007
1008/*
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04001009 * 6a. Handle RPC timeout
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 * We do not release the request slot, so we keep using the
1011 * same XID for all retransmits.
1012 */
1013static void
1014call_timeout(struct rpc_task *task)
1015{
1016 struct rpc_clnt *clnt = task->tk_client;
1017
1018 if (xprt_adjust_timeout(task->tk_rqstp) == 0) {
1019 dprintk("RPC: %4d call_timeout (minor)\n", task->tk_pid);
1020 goto retry;
1021 }
1022
1023 dprintk("RPC: %4d call_timeout (major)\n", task->tk_pid);
Chuck Leveref759a22006-03-20 13:44:17 -05001024 task->tk_timeouts++;
1025
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 if (RPC_IS_SOFT(task)) {
Chuck Leverf518e35a2006-01-03 09:55:52 +01001027 printk(KERN_NOTICE "%s: server %s not responding, timed out\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 clnt->cl_protname, clnt->cl_server);
1029 rpc_exit(task, -EIO);
1030 return;
1031 }
1032
Chuck Leverf518e35a2006-01-03 09:55:52 +01001033 if (!(task->tk_flags & RPC_CALL_MAJORSEEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 task->tk_flags |= RPC_CALL_MAJORSEEN;
1035 printk(KERN_NOTICE "%s: server %s not responding, still trying\n",
1036 clnt->cl_protname, clnt->cl_server);
1037 }
Chuck Lever35f5a422006-01-03 09:55:50 +01001038 rpc_force_rebind(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
1040retry:
1041 clnt->cl_stats->rpcretrans++;
1042 task->tk_action = call_bind;
1043 task->tk_status = 0;
1044}
1045
1046/*
1047 * 7. Decode the RPC reply
1048 */
1049static void
1050call_decode(struct rpc_task *task)
1051{
1052 struct rpc_clnt *clnt = task->tk_client;
1053 struct rpc_rqst *req = task->tk_rqstp;
1054 kxdrproc_t decode = task->tk_msg.rpc_proc->p_decode;
1055 u32 *p;
1056
1057 dprintk("RPC: %4d call_decode (status %d)\n",
1058 task->tk_pid, task->tk_status);
1059
Chuck Leverf518e35a2006-01-03 09:55:52 +01001060 if (task->tk_flags & RPC_CALL_MAJORSEEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 printk(KERN_NOTICE "%s: server %s OK\n",
1062 clnt->cl_protname, clnt->cl_server);
1063 task->tk_flags &= ~RPC_CALL_MAJORSEEN;
1064 }
1065
1066 if (task->tk_status < 12) {
1067 if (!RPC_IS_SOFT(task)) {
1068 task->tk_action = call_bind;
1069 clnt->cl_stats->rpcretrans++;
1070 goto out_retry;
1071 }
1072 printk(KERN_WARNING "%s: too small RPC reply size (%d bytes)\n",
1073 clnt->cl_protname, task->tk_status);
1074 rpc_exit(task, -EIO);
1075 return;
1076 }
1077
Trond Myklebust43ac3f22006-03-20 13:44:51 -05001078 /*
1079 * Ensure that we see all writes made by xprt_complete_rqst()
1080 * before it changed req->rq_received.
1081 */
1082 smp_rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 req->rq_rcv_buf.len = req->rq_private_buf.len;
1084
1085 /* Check that the softirq receive buffer is valid */
1086 WARN_ON(memcmp(&req->rq_rcv_buf, &req->rq_private_buf,
1087 sizeof(req->rq_rcv_buf)) != 0);
1088
1089 /* Verify the RPC header */
Trond Myklebustabbcf282006-01-03 09:55:03 +01001090 p = call_verify(task);
1091 if (IS_ERR(p)) {
1092 if (p == ERR_PTR(-EAGAIN))
1093 goto out_retry;
1094 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 }
1096
Trond Myklebustabbcf282006-01-03 09:55:03 +01001097 task->tk_action = rpc_exit_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
1099 if (decode)
1100 task->tk_status = rpcauth_unwrap_resp(task, decode, req, p,
1101 task->tk_msg.rpc_resp);
1102 dprintk("RPC: %4d call_decode result %d\n", task->tk_pid,
1103 task->tk_status);
1104 return;
1105out_retry:
1106 req->rq_received = req->rq_private_buf.len = 0;
1107 task->tk_status = 0;
1108}
1109
1110/*
1111 * 8. Refresh the credentials if rejected by the server
1112 */
1113static void
1114call_refresh(struct rpc_task *task)
1115{
1116 dprintk("RPC: %4d call_refresh\n", task->tk_pid);
1117
1118 xprt_release(task); /* Must do to obtain new XID */
1119 task->tk_action = call_refreshresult;
1120 task->tk_status = 0;
1121 task->tk_client->cl_stats->rpcauthrefresh++;
1122 rpcauth_refreshcred(task);
1123}
1124
1125/*
1126 * 8a. Process the results of a credential refresh
1127 */
1128static void
1129call_refreshresult(struct rpc_task *task)
1130{
1131 int status = task->tk_status;
1132 dprintk("RPC: %4d call_refreshresult (status %d)\n",
1133 task->tk_pid, task->tk_status);
1134
1135 task->tk_status = 0;
1136 task->tk_action = call_reserve;
1137 if (status >= 0 && rpcauth_uptodatecred(task))
1138 return;
1139 if (status == -EACCES) {
1140 rpc_exit(task, -EACCES);
1141 return;
1142 }
1143 task->tk_action = call_refresh;
1144 if (status != -ETIMEDOUT)
1145 rpc_delay(task, 3*HZ);
1146 return;
1147}
1148
1149/*
1150 * Call header serialization
1151 */
1152static u32 *
1153call_header(struct rpc_task *task)
1154{
1155 struct rpc_clnt *clnt = task->tk_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 struct rpc_rqst *req = task->tk_rqstp;
1157 u32 *p = req->rq_svec[0].iov_base;
1158
1159 /* FIXME: check buffer size? */
Chuck Lever808012f2005-08-25 16:25:49 -07001160
1161 p = xprt_skip_transport_header(task->tk_xprt, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 *p++ = req->rq_xid; /* XID */
1163 *p++ = htonl(RPC_CALL); /* CALL */
1164 *p++ = htonl(RPC_VERSION); /* RPC version */
1165 *p++ = htonl(clnt->cl_prog); /* program number */
1166 *p++ = htonl(clnt->cl_vers); /* program version */
1167 *p++ = htonl(task->tk_msg.rpc_proc->p_proc); /* procedure */
Trond Myklebust334ccfd2005-06-22 17:16:19 +00001168 p = rpcauth_marshcred(task, p);
1169 req->rq_slen = xdr_adjust_iovec(&req->rq_svec[0], p);
1170 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171}
1172
1173/*
1174 * Reply header verification
1175 */
1176static u32 *
1177call_verify(struct rpc_task *task)
1178{
1179 struct kvec *iov = &task->tk_rqstp->rq_rcv_buf.head[0];
1180 int len = task->tk_rqstp->rq_rcv_buf.len >> 2;
1181 u32 *p = iov->iov_base, n;
1182 int error = -EACCES;
1183
David Howellse8896492006-08-24 15:44:19 -04001184 if ((task->tk_rqstp->rq_rcv_buf.len & 3) != 0) {
1185 /* RFC-1014 says that the representation of XDR data must be a
1186 * multiple of four bytes
1187 * - if it isn't pointer subtraction in the NFS client may give
1188 * undefined results
1189 */
1190 printk(KERN_WARNING
1191 "call_verify: XDR representation not a multiple of"
1192 " 4 bytes: 0x%x\n", task->tk_rqstp->rq_rcv_buf.len);
1193 goto out_eio;
1194 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 if ((len -= 3) < 0)
1196 goto out_overflow;
1197 p += 1; /* skip XID */
1198
1199 if ((n = ntohl(*p++)) != RPC_REPLY) {
1200 printk(KERN_WARNING "call_verify: not an RPC reply: %x\n", n);
Trond Myklebustabbcf282006-01-03 09:55:03 +01001201 goto out_garbage;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 }
1203 if ((n = ntohl(*p++)) != RPC_MSG_ACCEPTED) {
1204 if (--len < 0)
1205 goto out_overflow;
1206 switch ((n = ntohl(*p++))) {
1207 case RPC_AUTH_ERROR:
1208 break;
1209 case RPC_MISMATCH:
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00001210 dprintk("%s: RPC call version mismatch!\n", __FUNCTION__);
1211 error = -EPROTONOSUPPORT;
1212 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 default:
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00001214 dprintk("%s: RPC call rejected, unknown error: %x\n", __FUNCTION__, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 goto out_eio;
1216 }
1217 if (--len < 0)
1218 goto out_overflow;
1219 switch ((n = ntohl(*p++))) {
1220 case RPC_AUTH_REJECTEDCRED:
1221 case RPC_AUTH_REJECTEDVERF:
1222 case RPCSEC_GSS_CREDPROBLEM:
1223 case RPCSEC_GSS_CTXPROBLEM:
1224 if (!task->tk_cred_retry)
1225 break;
1226 task->tk_cred_retry--;
1227 dprintk("RPC: %4d call_verify: retry stale creds\n",
1228 task->tk_pid);
1229 rpcauth_invalcred(task);
1230 task->tk_action = call_refresh;
Trond Myklebustabbcf282006-01-03 09:55:03 +01001231 goto out_retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 case RPC_AUTH_BADCRED:
1233 case RPC_AUTH_BADVERF:
1234 /* possibly garbled cred/verf? */
1235 if (!task->tk_garb_retry)
1236 break;
1237 task->tk_garb_retry--;
1238 dprintk("RPC: %4d call_verify: retry garbled creds\n",
1239 task->tk_pid);
1240 task->tk_action = call_bind;
Trond Myklebustabbcf282006-01-03 09:55:03 +01001241 goto out_retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 case RPC_AUTH_TOOWEAK:
Levent Serinol1356b8c2006-03-20 13:44:11 -05001243 printk(KERN_NOTICE "call_verify: server %s requires stronger "
1244 "authentication.\n", task->tk_client->cl_server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 break;
1246 default:
1247 printk(KERN_WARNING "call_verify: unknown auth error: %x\n", n);
1248 error = -EIO;
1249 }
1250 dprintk("RPC: %4d call_verify: call rejected %d\n",
1251 task->tk_pid, n);
1252 goto out_err;
1253 }
1254 if (!(p = rpcauth_checkverf(task, p))) {
1255 printk(KERN_WARNING "call_verify: auth check failed\n");
Trond Myklebustabbcf282006-01-03 09:55:03 +01001256 goto out_garbage; /* bad verifier, retry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 }
1258 len = p - (u32 *)iov->iov_base - 1;
1259 if (len < 0)
1260 goto out_overflow;
1261 switch ((n = ntohl(*p++))) {
1262 case RPC_SUCCESS:
1263 return p;
1264 case RPC_PROG_UNAVAIL:
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00001265 dprintk("RPC: call_verify: program %u is unsupported by server %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 (unsigned int)task->tk_client->cl_prog,
1267 task->tk_client->cl_server);
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00001268 error = -EPFNOSUPPORT;
1269 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 case RPC_PROG_MISMATCH:
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00001271 dprintk("RPC: call_verify: program %u, version %u unsupported by server %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 (unsigned int)task->tk_client->cl_prog,
1273 (unsigned int)task->tk_client->cl_vers,
1274 task->tk_client->cl_server);
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00001275 error = -EPROTONOSUPPORT;
1276 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 case RPC_PROC_UNAVAIL:
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00001278 dprintk("RPC: call_verify: proc %p unsupported by program %u, version %u on server %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 task->tk_msg.rpc_proc,
1280 task->tk_client->cl_prog,
1281 task->tk_client->cl_vers,
1282 task->tk_client->cl_server);
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00001283 error = -EOPNOTSUPP;
1284 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 case RPC_GARBAGE_ARGS:
1286 dprintk("RPC: %4d %s: server saw garbage\n", task->tk_pid, __FUNCTION__);
1287 break; /* retry */
1288 default:
1289 printk(KERN_WARNING "call_verify: server accept status: %x\n", n);
1290 /* Also retry */
1291 }
1292
Trond Myklebustabbcf282006-01-03 09:55:03 +01001293out_garbage:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 task->tk_client->cl_stats->rpcgarbage++;
1295 if (task->tk_garb_retry) {
1296 task->tk_garb_retry--;
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00001297 dprintk("RPC %s: retrying %4d\n", __FUNCTION__, task->tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 task->tk_action = call_bind;
Trond Myklebustabbcf282006-01-03 09:55:03 +01001299out_retry:
1300 return ERR_PTR(-EAGAIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 }
1302 printk(KERN_WARNING "RPC %s: retry failed, exit EIO\n", __FUNCTION__);
1303out_eio:
1304 error = -EIO;
1305out_err:
1306 rpc_exit(task, error);
Trond Myklebustabbcf282006-01-03 09:55:03 +01001307 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308out_overflow:
1309 printk(KERN_WARNING "RPC %s: server reply was truncated.\n", __FUNCTION__);
Trond Myklebustabbcf282006-01-03 09:55:03 +01001310 goto out_garbage;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311}
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00001312
1313static int rpcproc_encode_null(void *rqstp, u32 *data, void *obj)
1314{
1315 return 0;
1316}
1317
1318static int rpcproc_decode_null(void *rqstp, u32 *data, void *obj)
1319{
1320 return 0;
1321}
1322
1323static struct rpc_procinfo rpcproc_null = {
1324 .p_encode = rpcproc_encode_null,
1325 .p_decode = rpcproc_decode_null,
1326};
1327
1328int rpc_ping(struct rpc_clnt *clnt, int flags)
1329{
1330 struct rpc_message msg = {
1331 .rpc_proc = &rpcproc_null,
1332 };
1333 int err;
1334 msg.rpc_cred = authnull_ops.lookup_cred(NULL, NULL, 0);
1335 err = rpc_call_sync(clnt, &msg, flags);
1336 put_rpccred(msg.rpc_cred);
1337 return err;
1338}