blob: cee504162a3fb47d54ebfc79f80bb3bfaadefd4e [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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 clnt->cl_prog = program->number;
151 clnt->cl_vers = version->number;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 clnt->cl_stats = program->stats;
Chuck Lever11c556b2006-03-20 13:44:22 -0500153 clnt->cl_metrics = rpc_alloc_iostats(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Chuck Leverec739ef2006-08-22 20:06:15 -0400155 if (!xprt_bound(clnt->cl_xprt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 clnt->cl_autobind = 1;
157
158 clnt->cl_rtt = &clnt->cl_rtt_default;
159 rpc_init_rtt(&clnt->cl_rtt_default, xprt->timeout.to_initval);
160
161 err = rpc_setup_pipedir(clnt, program->pipe_dir_name);
162 if (err < 0)
163 goto out_no_path;
164
J. Bruce Fields6a192752005-06-22 17:16:23 +0000165 auth = rpcauth_create(flavor, clnt);
166 if (IS_ERR(auth)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 printk(KERN_INFO "RPC: Couldn't create auth handle (flavor %u)\n",
168 flavor);
J. Bruce Fields6a192752005-06-22 17:16:23 +0000169 err = PTR_ERR(auth);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 goto out_no_auth;
171 }
172
173 /* save the nodename */
174 clnt->cl_nodelen = strlen(system_utsname.nodename);
175 if (clnt->cl_nodelen > UNX_MAXNODENAME)
176 clnt->cl_nodelen = UNX_MAXNODENAME;
177 memcpy(clnt->cl_nodename, system_utsname.nodename, clnt->cl_nodelen);
178 return clnt;
179
180out_no_auth:
Trond Myklebust54281542006-03-20 13:44:49 -0500181 if (!IS_ERR(clnt->cl_dentry)) {
Trond Myklebustdff02cc2006-07-31 14:17:18 -0700182 rpc_rmdir(clnt->cl_dentry);
Trond Myklebust54281542006-03-20 13:44:49 -0500183 rpc_put_mount();
184 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185out_no_path:
186 if (clnt->cl_server != clnt->cl_inline_name)
187 kfree(clnt->cl_server);
188 kfree(clnt);
189out_err:
Trond Myklebust5b616f52005-06-22 17:16:20 +0000190 xprt_destroy(xprt);
Adrian Bunk712917d2006-03-13 21:20:47 -0800191out_no_xprt:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 return ERR_PTR(err);
193}
194
Trond Myklebust5ee0ed72005-06-22 17:16:20 +0000195/**
196 * Create an RPC client
197 * @xprt - pointer to xprt struct
198 * @servname - name of server
199 * @info - rpc_program
200 * @version - rpc_program version
201 * @authflavor - rpc_auth flavour to use
202 *
203 * Creates an RPC client structure, then pings the server in order to
204 * determine if it is up, and if it supports this program and version.
205 *
206 * This function should never be called by asynchronous tasks such as
207 * the portmapper.
208 */
209struct rpc_clnt *rpc_create_client(struct rpc_xprt *xprt, char *servname,
210 struct rpc_program *info, u32 version, rpc_authflavor_t authflavor)
211{
212 struct rpc_clnt *clnt;
213 int err;
214
215 clnt = rpc_new_client(xprt, servname, info, version, authflavor);
216 if (IS_ERR(clnt))
217 return clnt;
218 err = rpc_ping(clnt, RPC_TASK_SOFT|RPC_TASK_NOINTR);
219 if (err == 0)
220 return clnt;
221 rpc_shutdown_client(clnt);
222 return ERR_PTR(err);
223}
224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225/*
226 * This function clones the RPC client structure. It allows us to share the
227 * same transport while varying parameters such as the authentication
228 * flavour.
229 */
230struct rpc_clnt *
231rpc_clone_client(struct rpc_clnt *clnt)
232{
233 struct rpc_clnt *new;
234
Kris Katterjohn8b3a7002006-01-11 15:56:43 -0800235 new = kmalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 if (!new)
237 goto out_no_clnt;
238 memcpy(new, clnt, sizeof(*new));
239 atomic_set(&new->cl_count, 1);
240 atomic_set(&new->cl_users, 0);
241 new->cl_parent = clnt;
242 atomic_inc(&clnt->cl_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 /* Turn off autobind on clones */
244 new->cl_autobind = 0;
245 new->cl_oneshot = 0;
246 new->cl_dead = 0;
Trond Myklebust8f8e7a52006-08-14 13:11:15 -0400247 if (!IS_ERR(new->cl_dentry))
Trond Myklebust54281542006-03-20 13:44:49 -0500248 dget(new->cl_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 rpc_init_rtt(&new->cl_rtt_default, clnt->cl_xprt->timeout.to_initval);
250 if (new->cl_auth)
251 atomic_inc(&new->cl_auth->au_count);
Chuck Lever4a681792006-08-22 20:06:15 -0400252 new->cl_metrics = rpc_alloc_iostats(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 return new;
254out_no_clnt:
255 printk(KERN_INFO "RPC: out of memory in %s\n", __FUNCTION__);
256 return ERR_PTR(-ENOMEM);
257}
258
259/*
260 * Properly shut down an RPC client, terminating all outstanding
261 * requests. Note that we must be certain that cl_oneshot and
262 * cl_dead are cleared, or else the client would be destroyed
263 * when the last task releases it.
264 */
265int
266rpc_shutdown_client(struct rpc_clnt *clnt)
267{
268 dprintk("RPC: shutting down %s client for %s, tasks=%d\n",
269 clnt->cl_protname, clnt->cl_server,
270 atomic_read(&clnt->cl_users));
271
272 while (atomic_read(&clnt->cl_users) > 0) {
273 /* Don't let rpc_release_client destroy us */
274 clnt->cl_oneshot = 0;
275 clnt->cl_dead = 0;
276 rpc_killall_tasks(clnt);
Ingo Molnar532347e2006-01-09 20:52:53 -0800277 wait_event_timeout(destroy_wait,
Linus Torvalds4f477072006-01-10 08:56:39 -0800278 !atomic_read(&clnt->cl_users), 1*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 }
280
281 if (atomic_read(&clnt->cl_users) < 0) {
282 printk(KERN_ERR "RPC: rpc_shutdown_client clnt %p tasks=%d\n",
283 clnt, atomic_read(&clnt->cl_users));
284#ifdef RPC_DEBUG
285 rpc_show_tasks();
286#endif
287 BUG();
288 }
289
290 return rpc_destroy_client(clnt);
291}
292
293/*
294 * Delete an RPC client
295 */
296int
297rpc_destroy_client(struct rpc_clnt *clnt)
298{
299 if (!atomic_dec_and_test(&clnt->cl_count))
300 return 1;
301 BUG_ON(atomic_read(&clnt->cl_users) != 0);
302
303 dprintk("RPC: destroying %s client for %s\n",
304 clnt->cl_protname, clnt->cl_server);
305 if (clnt->cl_auth) {
306 rpcauth_destroy(clnt->cl_auth);
307 clnt->cl_auth = NULL;
308 }
309 if (clnt->cl_parent != clnt) {
Trond Myklebust8f8e7a52006-08-14 13:11:15 -0400310 if (!IS_ERR(clnt->cl_dentry))
311 dput(clnt->cl_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 rpc_destroy_client(clnt->cl_parent);
313 goto out_free;
314 }
Trond Myklebust8f8e7a52006-08-14 13:11:15 -0400315 if (!IS_ERR(clnt->cl_dentry)) {
Trond Myklebustdff02cc2006-07-31 14:17:18 -0700316 rpc_rmdir(clnt->cl_dentry);
Trond Myklebust8f8e7a52006-08-14 13:11:15 -0400317 rpc_put_mount();
318 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 if (clnt->cl_xprt) {
320 xprt_destroy(clnt->cl_xprt);
321 clnt->cl_xprt = NULL;
322 }
323 if (clnt->cl_server != clnt->cl_inline_name)
324 kfree(clnt->cl_server);
325out_free:
Chuck Lever11c556b2006-03-20 13:44:22 -0500326 rpc_free_iostats(clnt->cl_metrics);
327 clnt->cl_metrics = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 kfree(clnt);
329 return 0;
330}
331
332/*
333 * Release an RPC client
334 */
335void
336rpc_release_client(struct rpc_clnt *clnt)
337{
338 dprintk("RPC: rpc_release_client(%p, %d)\n",
339 clnt, atomic_read(&clnt->cl_users));
340
341 if (!atomic_dec_and_test(&clnt->cl_users))
342 return;
343 wake_up(&destroy_wait);
344 if (clnt->cl_oneshot || clnt->cl_dead)
345 rpc_destroy_client(clnt);
346}
347
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000348/**
349 * rpc_bind_new_program - bind a new RPC program to an existing client
350 * @old - old rpc_client
351 * @program - rpc program to set
352 * @vers - rpc program version
353 *
354 * Clones the rpc client and sets up a new RPC program. This is mainly
355 * of use for enabling different RPC programs to share the same transport.
356 * The Sun NFSv2/v3 ACL protocol can do this.
357 */
358struct rpc_clnt *rpc_bind_new_program(struct rpc_clnt *old,
359 struct rpc_program *program,
360 int vers)
361{
362 struct rpc_clnt *clnt;
363 struct rpc_version *version;
364 int err;
365
366 BUG_ON(vers >= program->nrvers || !program->version[vers]);
367 version = program->version[vers];
368 clnt = rpc_clone_client(old);
369 if (IS_ERR(clnt))
370 goto out;
371 clnt->cl_procinfo = version->procs;
372 clnt->cl_maxproc = version->nrprocs;
373 clnt->cl_protname = program->name;
374 clnt->cl_prog = program->number;
375 clnt->cl_vers = version->number;
376 clnt->cl_stats = program->stats;
377 err = rpc_ping(clnt, RPC_TASK_SOFT|RPC_TASK_NOINTR);
378 if (err != 0) {
379 rpc_shutdown_client(clnt);
380 clnt = ERR_PTR(err);
381 }
382out:
383 return clnt;
384}
385
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386/*
387 * Default callback for async RPC calls
388 */
389static void
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100390rpc_default_callback(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391{
392}
393
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100394static const struct rpc_call_ops rpc_default_ops = {
395 .rpc_call_done = rpc_default_callback,
396};
397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398/*
Trond Myklebust14b218a2005-06-22 17:16:28 +0000399 * Export the signal mask handling for synchronous code that
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 * sleeps on RPC calls
401 */
Trond Myklebust2bd61572006-01-03 09:55:19 +0100402#define RPC_INTR_SIGNALS (sigmask(SIGHUP) | sigmask(SIGINT) | sigmask(SIGQUIT) | sigmask(SIGTERM))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Trond Myklebust14b218a2005-06-22 17:16:28 +0000404static void rpc_save_sigmask(sigset_t *oldset, int intr)
405{
Trond Myklebust2bd61572006-01-03 09:55:19 +0100406 unsigned long sigallow = sigmask(SIGKILL);
Trond Myklebust14b218a2005-06-22 17:16:28 +0000407 sigset_t sigmask;
408
409 /* Block all signals except those listed in sigallow */
410 if (intr)
411 sigallow |= RPC_INTR_SIGNALS;
412 siginitsetinv(&sigmask, sigallow);
413 sigprocmask(SIG_BLOCK, &sigmask, oldset);
414}
415
416static inline void rpc_task_sigmask(struct rpc_task *task, sigset_t *oldset)
417{
418 rpc_save_sigmask(oldset, !RPC_TASK_UNINTERRUPTIBLE(task));
419}
420
421static inline void rpc_restore_sigmask(sigset_t *oldset)
422{
423 sigprocmask(SIG_SETMASK, oldset, NULL);
424}
425
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426void rpc_clnt_sigmask(struct rpc_clnt *clnt, sigset_t *oldset)
427{
Trond Myklebust14b218a2005-06-22 17:16:28 +0000428 rpc_save_sigmask(oldset, clnt->cl_intr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429}
430
431void rpc_clnt_sigunmask(struct rpc_clnt *clnt, sigset_t *oldset)
432{
Trond Myklebust14b218a2005-06-22 17:16:28 +0000433 rpc_restore_sigmask(oldset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434}
435
436/*
437 * New rpc_call implementation
438 */
439int rpc_call_sync(struct rpc_clnt *clnt, struct rpc_message *msg, int flags)
440{
441 struct rpc_task *task;
442 sigset_t oldset;
443 int status;
444
445 /* If this client is slain all further I/O fails */
446 if (clnt->cl_dead)
447 return -EIO;
448
449 BUG_ON(flags & RPC_TASK_ASYNC);
450
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 status = -ENOMEM;
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100452 task = rpc_new_task(clnt, flags, &rpc_default_ops, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 if (task == NULL)
454 goto out;
455
Trond Myklebust14b218a2005-06-22 17:16:28 +0000456 /* Mask signals on RPC calls _and_ GSS_AUTH upcalls */
457 rpc_task_sigmask(task, &oldset);
458
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 rpc_call_setup(task, msg, 0);
460
461 /* Set up the call info struct and execute the task */
Trond Myklebuste60859a2006-01-03 09:55:10 +0100462 status = task->tk_status;
463 if (status == 0) {
464 atomic_inc(&task->tk_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 status = rpc_execute(task);
Trond Myklebuste60859a2006-01-03 09:55:10 +0100466 if (status == 0)
467 status = task->tk_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 }
Trond Myklebust14b218a2005-06-22 17:16:28 +0000469 rpc_restore_sigmask(&oldset);
Trond Myklebuste60859a2006-01-03 09:55:10 +0100470 rpc_release_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 return status;
473}
474
475/*
476 * New rpc_call implementation
477 */
478int
479rpc_call_async(struct rpc_clnt *clnt, struct rpc_message *msg, int flags,
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100480 const struct rpc_call_ops *tk_ops, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481{
482 struct rpc_task *task;
483 sigset_t oldset;
484 int status;
485
486 /* If this client is slain all further I/O fails */
Trond Myklebust7a1218a2006-03-20 18:11:10 -0500487 status = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 if (clnt->cl_dead)
Trond Myklebust7a1218a2006-03-20 18:11:10 -0500489 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
491 flags |= RPC_TASK_ASYNC;
492
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 /* Create/initialize a new RPC task */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 status = -ENOMEM;
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100495 if (!(task = rpc_new_task(clnt, flags, tk_ops, data)))
Trond Myklebust7a1218a2006-03-20 18:11:10 -0500496 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
Trond Myklebust14b218a2005-06-22 17:16:28 +0000498 /* Mask signals on GSS_AUTH upcalls */
499 rpc_task_sigmask(task, &oldset);
500
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 rpc_call_setup(task, msg, 0);
502
503 /* Set up the call info struct and execute the task */
504 status = task->tk_status;
505 if (status == 0)
506 rpc_execute(task);
507 else
508 rpc_release_task(task);
509
Trond Myklebust14b218a2005-06-22 17:16:28 +0000510 rpc_restore_sigmask(&oldset);
Trond Myklebust7a1218a2006-03-20 18:11:10 -0500511 return status;
512out_release:
513 if (tk_ops->rpc_release != NULL)
514 tk_ops->rpc_release(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 return status;
516}
517
518
519void
520rpc_call_setup(struct rpc_task *task, struct rpc_message *msg, int flags)
521{
522 task->tk_msg = *msg;
523 task->tk_flags |= flags;
524 /* Bind the user cred */
525 if (task->tk_msg.rpc_cred != NULL)
526 rpcauth_holdcred(task);
527 else
528 rpcauth_bindcred(task);
529
530 if (task->tk_status == 0)
531 task->tk_action = call_start;
532 else
Trond Myklebustabbcf282006-01-03 09:55:03 +0100533 task->tk_action = rpc_exit_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534}
535
536void
537rpc_setbufsize(struct rpc_clnt *clnt, unsigned int sndsize, unsigned int rcvsize)
538{
539 struct rpc_xprt *xprt = clnt->cl_xprt;
Chuck Lever470056c2005-08-25 16:25:56 -0700540 if (xprt->ops->set_buffer_size)
541 xprt->ops->set_buffer_size(xprt, sndsize, rcvsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542}
543
544/*
545 * Return size of largest payload RPC client can support, in bytes
546 *
547 * For stream transports, this is one RPC record fragment (see RFC
548 * 1831), as we don't support multi-record requests yet. For datagram
549 * transports, this is the size of an IP packet minus the IP, UDP, and
550 * RPC header sizes.
551 */
552size_t rpc_max_payload(struct rpc_clnt *clnt)
553{
554 return clnt->cl_xprt->max_payload;
555}
556EXPORT_SYMBOL(rpc_max_payload);
557
Chuck Lever35f5a422006-01-03 09:55:50 +0100558/**
559 * rpc_force_rebind - force transport to check that remote port is unchanged
560 * @clnt: client to rebind
561 *
562 */
563void rpc_force_rebind(struct rpc_clnt *clnt)
564{
565 if (clnt->cl_autobind)
Chuck Leverec739ef2006-08-22 20:06:15 -0400566 xprt_clear_bound(clnt->cl_xprt);
Chuck Lever35f5a422006-01-03 09:55:50 +0100567}
568EXPORT_SYMBOL(rpc_force_rebind);
569
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570/*
571 * Restart an (async) RPC call. Usually called from within the
572 * exit handler.
573 */
574void
575rpc_restart_call(struct rpc_task *task)
576{
577 if (RPC_ASSASSINATED(task))
578 return;
579
580 task->tk_action = call_start;
581}
582
583/*
584 * 0. Initial state
585 *
586 * Other FSM states can be visited zero or more times, but
587 * this state is visited exactly once for each RPC.
588 */
589static void
590call_start(struct rpc_task *task)
591{
592 struct rpc_clnt *clnt = task->tk_client;
593
594 dprintk("RPC: %4d call_start %s%d proc %d (%s)\n", task->tk_pid,
595 clnt->cl_protname, clnt->cl_vers, task->tk_msg.rpc_proc->p_proc,
596 (RPC_IS_ASYNC(task) ? "async" : "sync"));
597
598 /* Increment call count */
599 task->tk_msg.rpc_proc->p_count++;
600 clnt->cl_stats->rpccnt++;
601 task->tk_action = call_reserve;
602}
603
604/*
605 * 1. Reserve an RPC call slot
606 */
607static void
608call_reserve(struct rpc_task *task)
609{
610 dprintk("RPC: %4d call_reserve\n", task->tk_pid);
611
612 if (!rpcauth_uptodatecred(task)) {
613 task->tk_action = call_refresh;
614 return;
615 }
616
617 task->tk_status = 0;
618 task->tk_action = call_reserveresult;
619 xprt_reserve(task);
620}
621
622/*
623 * 1b. Grok the result of xprt_reserve()
624 */
625static void
626call_reserveresult(struct rpc_task *task)
627{
628 int status = task->tk_status;
629
630 dprintk("RPC: %4d call_reserveresult (status %d)\n",
631 task->tk_pid, task->tk_status);
632
633 /*
634 * After a call to xprt_reserve(), we must have either
635 * a request slot or else an error status.
636 */
637 task->tk_status = 0;
638 if (status >= 0) {
639 if (task->tk_rqstp) {
640 task->tk_action = call_allocate;
641 return;
642 }
643
644 printk(KERN_ERR "%s: status=%d, but no request slot, exiting\n",
645 __FUNCTION__, status);
646 rpc_exit(task, -EIO);
647 return;
648 }
649
650 /*
651 * Even though there was an error, we may have acquired
652 * a request slot somehow. Make sure not to leak it.
653 */
654 if (task->tk_rqstp) {
655 printk(KERN_ERR "%s: status=%d, request allocated anyway\n",
656 __FUNCTION__, status);
657 xprt_release(task);
658 }
659
660 switch (status) {
661 case -EAGAIN: /* woken up; retry */
662 task->tk_action = call_reserve;
663 return;
664 case -EIO: /* probably a shutdown */
665 break;
666 default:
667 printk(KERN_ERR "%s: unrecognized error %d, exiting\n",
668 __FUNCTION__, status);
669 break;
670 }
671 rpc_exit(task, status);
672}
673
674/*
675 * 2. Allocate the buffer. For details, see sched.c:rpc_malloc.
Chuck Lever02107142006-01-03 09:55:49 +0100676 * (Note: buffer memory is freed in xprt_release).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 */
678static void
679call_allocate(struct rpc_task *task)
680{
Chuck Lever02107142006-01-03 09:55:49 +0100681 struct rpc_rqst *req = task->tk_rqstp;
682 struct rpc_xprt *xprt = task->tk_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 unsigned int bufsiz;
684
685 dprintk("RPC: %4d call_allocate (status %d)\n",
686 task->tk_pid, task->tk_status);
687 task->tk_action = call_bind;
Chuck Lever02107142006-01-03 09:55:49 +0100688 if (req->rq_buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 return;
690
691 /* FIXME: compute buffer requirements more exactly using
692 * auth->au_wslack */
693 bufsiz = task->tk_msg.rpc_proc->p_bufsiz + RPC_SLACK_SPACE;
694
Chuck Lever02107142006-01-03 09:55:49 +0100695 if (xprt->ops->buf_alloc(task, bufsiz << 1) != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 return;
697 printk(KERN_INFO "RPC: buffer allocation failed for task %p\n", task);
698
Trond Myklebust14b218a2005-06-22 17:16:28 +0000699 if (RPC_IS_ASYNC(task) || !signalled()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 xprt_release(task);
701 task->tk_action = call_reserve;
702 rpc_delay(task, HZ>>4);
703 return;
704 }
705
706 rpc_exit(task, -ERESTARTSYS);
707}
708
Trond Myklebust940e3312005-11-09 21:45:24 -0500709static inline int
710rpc_task_need_encode(struct rpc_task *task)
711{
712 return task->tk_rqstp->rq_snd_buf.len == 0;
713}
714
715static inline void
716rpc_task_force_reencode(struct rpc_task *task)
717{
718 task->tk_rqstp->rq_snd_buf.len = 0;
719}
720
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721/*
722 * 3. Encode arguments of an RPC call
723 */
724static void
725call_encode(struct rpc_task *task)
726{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 struct rpc_rqst *req = task->tk_rqstp;
728 struct xdr_buf *sndbuf = &req->rq_snd_buf;
729 struct xdr_buf *rcvbuf = &req->rq_rcv_buf;
730 unsigned int bufsiz;
731 kxdrproc_t encode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 u32 *p;
733
734 dprintk("RPC: %4d call_encode (status %d)\n",
735 task->tk_pid, task->tk_status);
736
737 /* Default buffer setup */
Chuck Lever02107142006-01-03 09:55:49 +0100738 bufsiz = req->rq_bufsize >> 1;
739 sndbuf->head[0].iov_base = (void *)req->rq_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 sndbuf->head[0].iov_len = bufsiz;
741 sndbuf->tail[0].iov_len = 0;
742 sndbuf->page_len = 0;
743 sndbuf->len = 0;
744 sndbuf->buflen = bufsiz;
Chuck Lever02107142006-01-03 09:55:49 +0100745 rcvbuf->head[0].iov_base = (void *)((char *)req->rq_buffer + bufsiz);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 rcvbuf->head[0].iov_len = bufsiz;
747 rcvbuf->tail[0].iov_len = 0;
748 rcvbuf->page_len = 0;
749 rcvbuf->len = 0;
750 rcvbuf->buflen = bufsiz;
751
752 /* Encode header and provided arguments */
753 encode = task->tk_msg.rpc_proc->p_encode;
754 if (!(p = call_header(task))) {
755 printk(KERN_INFO "RPC: call_header failed, exit EIO\n");
756 rpc_exit(task, -EIO);
757 return;
758 }
J. Bruce Fieldsf3680312005-10-13 16:54:48 -0400759 if (encode == NULL)
760 return;
761
762 task->tk_status = rpcauth_wrap_req(task, encode, req, p,
763 task->tk_msg.rpc_argp);
764 if (task->tk_status == -ENOMEM) {
765 /* XXX: Is this sane? */
766 rpc_delay(task, 3*HZ);
767 task->tk_status = -EAGAIN;
768 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769}
770
771/*
772 * 4. Get the server port number if not yet set
773 */
774static void
775call_bind(struct rpc_task *task)
776{
777 struct rpc_clnt *clnt = task->tk_client;
Chuck Leverec739ef2006-08-22 20:06:15 -0400778 struct rpc_xprt *xprt = task->tk_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
Chuck Leverda351872005-08-11 16:25:11 -0400780 dprintk("RPC: %4d call_bind (status %d)\n",
781 task->tk_pid, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
Chuck Leverda351872005-08-11 16:25:11 -0400783 task->tk_action = call_connect;
Chuck Leverec739ef2006-08-22 20:06:15 -0400784 if (!xprt_bound(xprt)) {
Chuck Leverda351872005-08-11 16:25:11 -0400785 task->tk_action = call_bind_status;
Chuck Leverec739ef2006-08-22 20:06:15 -0400786 task->tk_timeout = xprt->bind_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 rpc_getport(task, clnt);
788 }
789}
790
791/*
Chuck Leverda351872005-08-11 16:25:11 -0400792 * 4a. Sort out bind result
793 */
794static void
795call_bind_status(struct rpc_task *task)
796{
797 int status = -EACCES;
798
799 if (task->tk_status >= 0) {
800 dprintk("RPC: %4d call_bind_status (status %d)\n",
801 task->tk_pid, task->tk_status);
802 task->tk_status = 0;
803 task->tk_action = call_connect;
804 return;
805 }
806
807 switch (task->tk_status) {
808 case -EACCES:
809 dprintk("RPC: %4d remote rpcbind: RPC program/version unavailable\n",
810 task->tk_pid);
Chuck Leverea635a52005-10-06 23:12:58 -0400811 rpc_delay(task, 3*HZ);
812 goto retry_bind;
Chuck Leverda351872005-08-11 16:25:11 -0400813 case -ETIMEDOUT:
814 dprintk("RPC: %4d rpcbind request timed out\n",
815 task->tk_pid);
816 if (RPC_IS_SOFT(task)) {
817 status = -EIO;
818 break;
819 }
820 goto retry_bind;
821 case -EPFNOSUPPORT:
822 dprintk("RPC: %4d remote rpcbind service unavailable\n",
823 task->tk_pid);
824 break;
825 case -EPROTONOSUPPORT:
826 dprintk("RPC: %4d remote rpcbind version 2 unavailable\n",
827 task->tk_pid);
828 break;
829 default:
830 dprintk("RPC: %4d unrecognized rpcbind error (%d)\n",
831 task->tk_pid, -task->tk_status);
832 status = -EIO;
833 break;
834 }
835
836 rpc_exit(task, status);
837 return;
838
839retry_bind:
840 task->tk_status = 0;
841 task->tk_action = call_bind;
842 return;
843}
844
845/*
846 * 4b. Connect to the RPC server
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 */
848static void
849call_connect(struct rpc_task *task)
850{
Chuck Leverda351872005-08-11 16:25:11 -0400851 struct rpc_xprt *xprt = task->tk_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
Chuck Leverda351872005-08-11 16:25:11 -0400853 dprintk("RPC: %4d call_connect xprt %p %s connected\n",
854 task->tk_pid, xprt,
855 (xprt_connected(xprt) ? "is" : "is not"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
Chuck Leverda351872005-08-11 16:25:11 -0400857 task->tk_action = call_transmit;
858 if (!xprt_connected(xprt)) {
859 task->tk_action = call_connect_status;
860 if (task->tk_status < 0)
861 return;
862 xprt_connect(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864}
865
866/*
Chuck Leverda351872005-08-11 16:25:11 -0400867 * 4c. Sort out connect result
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 */
869static void
870call_connect_status(struct rpc_task *task)
871{
872 struct rpc_clnt *clnt = task->tk_client;
873 int status = task->tk_status;
874
Chuck Leverda351872005-08-11 16:25:11 -0400875 dprintk("RPC: %5u call_connect_status (status %d)\n",
876 task->tk_pid, task->tk_status);
877
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 task->tk_status = 0;
879 if (status >= 0) {
880 clnt->cl_stats->netreconn++;
881 task->tk_action = call_transmit;
882 return;
883 }
884
Chuck Leverda351872005-08-11 16:25:11 -0400885 /* Something failed: remote service port may have changed */
Chuck Lever35f5a422006-01-03 09:55:50 +0100886 rpc_force_rebind(clnt);
Chuck Leverda351872005-08-11 16:25:11 -0400887
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 switch (status) {
889 case -ENOTCONN:
890 case -ETIMEDOUT:
891 case -EAGAIN:
Chuck Leverda351872005-08-11 16:25:11 -0400892 task->tk_action = call_bind;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 break;
894 default:
895 rpc_exit(task, -EIO);
Chuck Leverda351872005-08-11 16:25:11 -0400896 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 }
898}
899
900/*
901 * 5. Transmit the RPC request, and wait for reply
902 */
903static void
904call_transmit(struct rpc_task *task)
905{
906 dprintk("RPC: %4d call_transmit (status %d)\n",
907 task->tk_pid, task->tk_status);
908
909 task->tk_action = call_status;
910 if (task->tk_status < 0)
911 return;
912 task->tk_status = xprt_prepare_transmit(task);
913 if (task->tk_status != 0)
914 return;
Trond Myklebuste0ab53d2006-07-27 17:22:50 -0400915 task->tk_action = call_transmit_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 /* Encode here so that rpcsec_gss can use correct sequence number. */
Trond Myklebust940e3312005-11-09 21:45:24 -0500917 if (rpc_task_need_encode(task)) {
Trond Myklebuste0ab53d2006-07-27 17:22:50 -0400918 BUG_ON(task->tk_rqstp->rq_bytes_sent != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 call_encode(task);
Trond Myklebust5e5ce5b2005-10-18 14:20:11 -0700920 /* Did the encode result in an error condition? */
921 if (task->tk_status != 0)
Trond Myklebuste0ab53d2006-07-27 17:22:50 -0400922 return;
Trond Myklebust5e5ce5b2005-10-18 14:20:11 -0700923 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 xprt_transmit(task);
925 if (task->tk_status < 0)
926 return;
Trond Myklebuste0ab53d2006-07-27 17:22:50 -0400927 /*
928 * On success, ensure that we call xprt_end_transmit() before sleeping
929 * in order to allow access to the socket to other RPC requests.
930 */
931 call_transmit_status(task);
932 if (task->tk_msg.rpc_proc->p_decode != NULL)
933 return;
934 task->tk_action = rpc_exit_task;
935 rpc_wake_up_task(task);
936}
937
938/*
939 * 5a. Handle cleanup after a transmission
940 */
941static void
942call_transmit_status(struct rpc_task *task)
943{
944 task->tk_action = call_status;
945 /*
946 * Special case: if we've been waiting on the socket's write_space()
947 * callback, then don't call xprt_end_transmit().
948 */
949 if (task->tk_status == -EAGAIN)
950 return;
951 xprt_end_transmit(task);
Trond Myklebust940e3312005-11-09 21:45:24 -0500952 rpc_task_force_reencode(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953}
954
955/*
956 * 6. Sort out the RPC call status
957 */
958static void
959call_status(struct rpc_task *task)
960{
961 struct rpc_clnt *clnt = task->tk_client;
962 struct rpc_rqst *req = task->tk_rqstp;
963 int status;
964
965 if (req->rq_received > 0 && !req->rq_bytes_sent)
966 task->tk_status = req->rq_received;
967
968 dprintk("RPC: %4d call_status (status %d)\n",
969 task->tk_pid, task->tk_status);
970
971 status = task->tk_status;
972 if (status >= 0) {
973 task->tk_action = call_decode;
974 return;
975 }
976
977 task->tk_status = 0;
978 switch(status) {
979 case -ETIMEDOUT:
980 task->tk_action = call_timeout;
981 break;
982 case -ECONNREFUSED:
983 case -ENOTCONN:
Chuck Lever35f5a422006-01-03 09:55:50 +0100984 rpc_force_rebind(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 task->tk_action = call_bind;
986 break;
987 case -EAGAIN:
988 task->tk_action = call_transmit;
989 break;
990 case -EIO:
991 /* shutdown or soft timeout */
992 rpc_exit(task, status);
993 break;
994 default:
Chuck Leverf518e35a2006-01-03 09:55:52 +0100995 printk("%s: RPC call returned error %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 clnt->cl_protname, -status);
997 rpc_exit(task, status);
998 break;
999 }
1000}
1001
1002/*
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04001003 * 6a. Handle RPC timeout
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 * We do not release the request slot, so we keep using the
1005 * same XID for all retransmits.
1006 */
1007static void
1008call_timeout(struct rpc_task *task)
1009{
1010 struct rpc_clnt *clnt = task->tk_client;
1011
1012 if (xprt_adjust_timeout(task->tk_rqstp) == 0) {
1013 dprintk("RPC: %4d call_timeout (minor)\n", task->tk_pid);
1014 goto retry;
1015 }
1016
1017 dprintk("RPC: %4d call_timeout (major)\n", task->tk_pid);
Chuck Leveref759a22006-03-20 13:44:17 -05001018 task->tk_timeouts++;
1019
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 if (RPC_IS_SOFT(task)) {
Chuck Leverf518e35a2006-01-03 09:55:52 +01001021 printk(KERN_NOTICE "%s: server %s not responding, timed out\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 clnt->cl_protname, clnt->cl_server);
1023 rpc_exit(task, -EIO);
1024 return;
1025 }
1026
Chuck Leverf518e35a2006-01-03 09:55:52 +01001027 if (!(task->tk_flags & RPC_CALL_MAJORSEEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 task->tk_flags |= RPC_CALL_MAJORSEEN;
1029 printk(KERN_NOTICE "%s: server %s not responding, still trying\n",
1030 clnt->cl_protname, clnt->cl_server);
1031 }
Chuck Lever35f5a422006-01-03 09:55:50 +01001032 rpc_force_rebind(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
1034retry:
1035 clnt->cl_stats->rpcretrans++;
1036 task->tk_action = call_bind;
1037 task->tk_status = 0;
1038}
1039
1040/*
1041 * 7. Decode the RPC reply
1042 */
1043static void
1044call_decode(struct rpc_task *task)
1045{
1046 struct rpc_clnt *clnt = task->tk_client;
1047 struct rpc_rqst *req = task->tk_rqstp;
1048 kxdrproc_t decode = task->tk_msg.rpc_proc->p_decode;
1049 u32 *p;
1050
1051 dprintk("RPC: %4d call_decode (status %d)\n",
1052 task->tk_pid, task->tk_status);
1053
Chuck Leverf518e35a2006-01-03 09:55:52 +01001054 if (task->tk_flags & RPC_CALL_MAJORSEEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 printk(KERN_NOTICE "%s: server %s OK\n",
1056 clnt->cl_protname, clnt->cl_server);
1057 task->tk_flags &= ~RPC_CALL_MAJORSEEN;
1058 }
1059
1060 if (task->tk_status < 12) {
1061 if (!RPC_IS_SOFT(task)) {
1062 task->tk_action = call_bind;
1063 clnt->cl_stats->rpcretrans++;
1064 goto out_retry;
1065 }
1066 printk(KERN_WARNING "%s: too small RPC reply size (%d bytes)\n",
1067 clnt->cl_protname, task->tk_status);
1068 rpc_exit(task, -EIO);
1069 return;
1070 }
1071
Trond Myklebust43ac3f22006-03-20 13:44:51 -05001072 /*
1073 * Ensure that we see all writes made by xprt_complete_rqst()
1074 * before it changed req->rq_received.
1075 */
1076 smp_rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 req->rq_rcv_buf.len = req->rq_private_buf.len;
1078
1079 /* Check that the softirq receive buffer is valid */
1080 WARN_ON(memcmp(&req->rq_rcv_buf, &req->rq_private_buf,
1081 sizeof(req->rq_rcv_buf)) != 0);
1082
1083 /* Verify the RPC header */
Trond Myklebustabbcf282006-01-03 09:55:03 +01001084 p = call_verify(task);
1085 if (IS_ERR(p)) {
1086 if (p == ERR_PTR(-EAGAIN))
1087 goto out_retry;
1088 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 }
1090
Trond Myklebustabbcf282006-01-03 09:55:03 +01001091 task->tk_action = rpc_exit_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
1093 if (decode)
1094 task->tk_status = rpcauth_unwrap_resp(task, decode, req, p,
1095 task->tk_msg.rpc_resp);
1096 dprintk("RPC: %4d call_decode result %d\n", task->tk_pid,
1097 task->tk_status);
1098 return;
1099out_retry:
1100 req->rq_received = req->rq_private_buf.len = 0;
1101 task->tk_status = 0;
1102}
1103
1104/*
1105 * 8. Refresh the credentials if rejected by the server
1106 */
1107static void
1108call_refresh(struct rpc_task *task)
1109{
1110 dprintk("RPC: %4d call_refresh\n", task->tk_pid);
1111
1112 xprt_release(task); /* Must do to obtain new XID */
1113 task->tk_action = call_refreshresult;
1114 task->tk_status = 0;
1115 task->tk_client->cl_stats->rpcauthrefresh++;
1116 rpcauth_refreshcred(task);
1117}
1118
1119/*
1120 * 8a. Process the results of a credential refresh
1121 */
1122static void
1123call_refreshresult(struct rpc_task *task)
1124{
1125 int status = task->tk_status;
1126 dprintk("RPC: %4d call_refreshresult (status %d)\n",
1127 task->tk_pid, task->tk_status);
1128
1129 task->tk_status = 0;
1130 task->tk_action = call_reserve;
1131 if (status >= 0 && rpcauth_uptodatecred(task))
1132 return;
1133 if (status == -EACCES) {
1134 rpc_exit(task, -EACCES);
1135 return;
1136 }
1137 task->tk_action = call_refresh;
1138 if (status != -ETIMEDOUT)
1139 rpc_delay(task, 3*HZ);
1140 return;
1141}
1142
1143/*
1144 * Call header serialization
1145 */
1146static u32 *
1147call_header(struct rpc_task *task)
1148{
1149 struct rpc_clnt *clnt = task->tk_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 struct rpc_rqst *req = task->tk_rqstp;
1151 u32 *p = req->rq_svec[0].iov_base;
1152
1153 /* FIXME: check buffer size? */
Chuck Lever808012f2005-08-25 16:25:49 -07001154
1155 p = xprt_skip_transport_header(task->tk_xprt, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 *p++ = req->rq_xid; /* XID */
1157 *p++ = htonl(RPC_CALL); /* CALL */
1158 *p++ = htonl(RPC_VERSION); /* RPC version */
1159 *p++ = htonl(clnt->cl_prog); /* program number */
1160 *p++ = htonl(clnt->cl_vers); /* program version */
1161 *p++ = htonl(task->tk_msg.rpc_proc->p_proc); /* procedure */
Trond Myklebust334ccfd2005-06-22 17:16:19 +00001162 p = rpcauth_marshcred(task, p);
1163 req->rq_slen = xdr_adjust_iovec(&req->rq_svec[0], p);
1164 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165}
1166
1167/*
1168 * Reply header verification
1169 */
1170static u32 *
1171call_verify(struct rpc_task *task)
1172{
1173 struct kvec *iov = &task->tk_rqstp->rq_rcv_buf.head[0];
1174 int len = task->tk_rqstp->rq_rcv_buf.len >> 2;
1175 u32 *p = iov->iov_base, n;
1176 int error = -EACCES;
1177
David Howellse8896492006-08-24 15:44:19 -04001178 if ((task->tk_rqstp->rq_rcv_buf.len & 3) != 0) {
1179 /* RFC-1014 says that the representation of XDR data must be a
1180 * multiple of four bytes
1181 * - if it isn't pointer subtraction in the NFS client may give
1182 * undefined results
1183 */
1184 printk(KERN_WARNING
1185 "call_verify: XDR representation not a multiple of"
1186 " 4 bytes: 0x%x\n", task->tk_rqstp->rq_rcv_buf.len);
1187 goto out_eio;
1188 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 if ((len -= 3) < 0)
1190 goto out_overflow;
1191 p += 1; /* skip XID */
1192
1193 if ((n = ntohl(*p++)) != RPC_REPLY) {
1194 printk(KERN_WARNING "call_verify: not an RPC reply: %x\n", n);
Trond Myklebustabbcf282006-01-03 09:55:03 +01001195 goto out_garbage;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 }
1197 if ((n = ntohl(*p++)) != RPC_MSG_ACCEPTED) {
1198 if (--len < 0)
1199 goto out_overflow;
1200 switch ((n = ntohl(*p++))) {
1201 case RPC_AUTH_ERROR:
1202 break;
1203 case RPC_MISMATCH:
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00001204 dprintk("%s: RPC call version mismatch!\n", __FUNCTION__);
1205 error = -EPROTONOSUPPORT;
1206 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 default:
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00001208 dprintk("%s: RPC call rejected, unknown error: %x\n", __FUNCTION__, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 goto out_eio;
1210 }
1211 if (--len < 0)
1212 goto out_overflow;
1213 switch ((n = ntohl(*p++))) {
1214 case RPC_AUTH_REJECTEDCRED:
1215 case RPC_AUTH_REJECTEDVERF:
1216 case RPCSEC_GSS_CREDPROBLEM:
1217 case RPCSEC_GSS_CTXPROBLEM:
1218 if (!task->tk_cred_retry)
1219 break;
1220 task->tk_cred_retry--;
1221 dprintk("RPC: %4d call_verify: retry stale creds\n",
1222 task->tk_pid);
1223 rpcauth_invalcred(task);
1224 task->tk_action = call_refresh;
Trond Myklebustabbcf282006-01-03 09:55:03 +01001225 goto out_retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 case RPC_AUTH_BADCRED:
1227 case RPC_AUTH_BADVERF:
1228 /* possibly garbled cred/verf? */
1229 if (!task->tk_garb_retry)
1230 break;
1231 task->tk_garb_retry--;
1232 dprintk("RPC: %4d call_verify: retry garbled creds\n",
1233 task->tk_pid);
1234 task->tk_action = call_bind;
Trond Myklebustabbcf282006-01-03 09:55:03 +01001235 goto out_retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 case RPC_AUTH_TOOWEAK:
Levent Serinol1356b8c2006-03-20 13:44:11 -05001237 printk(KERN_NOTICE "call_verify: server %s requires stronger "
1238 "authentication.\n", task->tk_client->cl_server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 break;
1240 default:
1241 printk(KERN_WARNING "call_verify: unknown auth error: %x\n", n);
1242 error = -EIO;
1243 }
1244 dprintk("RPC: %4d call_verify: call rejected %d\n",
1245 task->tk_pid, n);
1246 goto out_err;
1247 }
1248 if (!(p = rpcauth_checkverf(task, p))) {
1249 printk(KERN_WARNING "call_verify: auth check failed\n");
Trond Myklebustabbcf282006-01-03 09:55:03 +01001250 goto out_garbage; /* bad verifier, retry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 }
1252 len = p - (u32 *)iov->iov_base - 1;
1253 if (len < 0)
1254 goto out_overflow;
1255 switch ((n = ntohl(*p++))) {
1256 case RPC_SUCCESS:
1257 return p;
1258 case RPC_PROG_UNAVAIL:
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00001259 dprintk("RPC: call_verify: program %u is unsupported by server %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 (unsigned int)task->tk_client->cl_prog,
1261 task->tk_client->cl_server);
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00001262 error = -EPFNOSUPPORT;
1263 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 case RPC_PROG_MISMATCH:
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00001265 dprintk("RPC: call_verify: program %u, version %u unsupported by server %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 (unsigned int)task->tk_client->cl_prog,
1267 (unsigned int)task->tk_client->cl_vers,
1268 task->tk_client->cl_server);
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00001269 error = -EPROTONOSUPPORT;
1270 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 case RPC_PROC_UNAVAIL:
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00001272 dprintk("RPC: call_verify: proc %p unsupported by program %u, version %u on server %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 task->tk_msg.rpc_proc,
1274 task->tk_client->cl_prog,
1275 task->tk_client->cl_vers,
1276 task->tk_client->cl_server);
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00001277 error = -EOPNOTSUPP;
1278 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 case RPC_GARBAGE_ARGS:
1280 dprintk("RPC: %4d %s: server saw garbage\n", task->tk_pid, __FUNCTION__);
1281 break; /* retry */
1282 default:
1283 printk(KERN_WARNING "call_verify: server accept status: %x\n", n);
1284 /* Also retry */
1285 }
1286
Trond Myklebustabbcf282006-01-03 09:55:03 +01001287out_garbage:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 task->tk_client->cl_stats->rpcgarbage++;
1289 if (task->tk_garb_retry) {
1290 task->tk_garb_retry--;
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00001291 dprintk("RPC %s: retrying %4d\n", __FUNCTION__, task->tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 task->tk_action = call_bind;
Trond Myklebustabbcf282006-01-03 09:55:03 +01001293out_retry:
1294 return ERR_PTR(-EAGAIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 }
1296 printk(KERN_WARNING "RPC %s: retry failed, exit EIO\n", __FUNCTION__);
1297out_eio:
1298 error = -EIO;
1299out_err:
1300 rpc_exit(task, error);
Trond Myklebustabbcf282006-01-03 09:55:03 +01001301 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302out_overflow:
1303 printk(KERN_WARNING "RPC %s: server reply was truncated.\n", __FUNCTION__);
Trond Myklebustabbcf282006-01-03 09:55:03 +01001304 goto out_garbage;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305}
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00001306
1307static int rpcproc_encode_null(void *rqstp, u32 *data, void *obj)
1308{
1309 return 0;
1310}
1311
1312static int rpcproc_decode_null(void *rqstp, u32 *data, void *obj)
1313{
1314 return 0;
1315}
1316
1317static struct rpc_procinfo rpcproc_null = {
1318 .p_encode = rpcproc_encode_null,
1319 .p_decode = rpcproc_decode_null,
1320};
1321
1322int rpc_ping(struct rpc_clnt *clnt, int flags)
1323{
1324 struct rpc_message msg = {
1325 .rpc_proc = &rpcproc_null,
1326 };
1327 int err;
1328 msg.rpc_cred = authnull_ops.lookup_cred(NULL, NULL, 0);
1329 err = rpc_call_sync(clnt, &msg, flags);
1330 put_rpccred(msg.rpc_cred);
1331 return err;
1332}