blob: 4ba271f892c8791714e635f91f32d50eb1dbd32d [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;
151 clnt->cl_port = xprt->addr.sin_port;
152 clnt->cl_prog = program->number;
153 clnt->cl_vers = version->number;
154 clnt->cl_prot = xprt->prot;
155 clnt->cl_stats = program->stats;
Chuck Lever11c556b2006-03-20 13:44:22 -0500156 clnt->cl_metrics = rpc_alloc_iostats(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 rpc_init_wait_queue(&clnt->cl_pmap_default.pm_bindwait, "bindwait");
158
159 if (!clnt->cl_port)
160 clnt->cl_autobind = 1;
161
162 clnt->cl_rtt = &clnt->cl_rtt_default;
163 rpc_init_rtt(&clnt->cl_rtt_default, xprt->timeout.to_initval);
164
165 err = rpc_setup_pipedir(clnt, program->pipe_dir_name);
166 if (err < 0)
167 goto out_no_path;
168
J. Bruce Fields6a192752005-06-22 17:16:23 +0000169 auth = rpcauth_create(flavor, clnt);
170 if (IS_ERR(auth)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 printk(KERN_INFO "RPC: Couldn't create auth handle (flavor %u)\n",
172 flavor);
J. Bruce Fields6a192752005-06-22 17:16:23 +0000173 err = PTR_ERR(auth);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 goto out_no_auth;
175 }
176
177 /* save the nodename */
178 clnt->cl_nodelen = strlen(system_utsname.nodename);
179 if (clnt->cl_nodelen > UNX_MAXNODENAME)
180 clnt->cl_nodelen = UNX_MAXNODENAME;
181 memcpy(clnt->cl_nodename, system_utsname.nodename, clnt->cl_nodelen);
182 return clnt;
183
184out_no_auth:
Trond Myklebust54281542006-03-20 13:44:49 -0500185 if (!IS_ERR(clnt->cl_dentry)) {
186 rpc_rmdir(clnt->cl_pathname);
187 dput(clnt->cl_dentry);
188 rpc_put_mount();
189 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190out_no_path:
191 if (clnt->cl_server != clnt->cl_inline_name)
192 kfree(clnt->cl_server);
193 kfree(clnt);
194out_err:
Trond Myklebust5b616f52005-06-22 17:16:20 +0000195 xprt_destroy(xprt);
Adrian Bunk712917d2006-03-13 21:20:47 -0800196out_no_xprt:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 return ERR_PTR(err);
198}
199
Trond Myklebust5ee0ed72005-06-22 17:16:20 +0000200/**
201 * Create an RPC client
202 * @xprt - pointer to xprt struct
203 * @servname - name of server
204 * @info - rpc_program
205 * @version - rpc_program version
206 * @authflavor - rpc_auth flavour to use
207 *
208 * Creates an RPC client structure, then pings the server in order to
209 * determine if it is up, and if it supports this program and version.
210 *
211 * This function should never be called by asynchronous tasks such as
212 * the portmapper.
213 */
214struct rpc_clnt *rpc_create_client(struct rpc_xprt *xprt, char *servname,
215 struct rpc_program *info, u32 version, rpc_authflavor_t authflavor)
216{
217 struct rpc_clnt *clnt;
218 int err;
219
220 clnt = rpc_new_client(xprt, servname, info, version, authflavor);
221 if (IS_ERR(clnt))
222 return clnt;
223 err = rpc_ping(clnt, RPC_TASK_SOFT|RPC_TASK_NOINTR);
224 if (err == 0)
225 return clnt;
226 rpc_shutdown_client(clnt);
227 return ERR_PTR(err);
228}
229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230/*
231 * This function clones the RPC client structure. It allows us to share the
232 * same transport while varying parameters such as the authentication
233 * flavour.
234 */
235struct rpc_clnt *
236rpc_clone_client(struct rpc_clnt *clnt)
237{
238 struct rpc_clnt *new;
239
Kris Katterjohn8b3a7002006-01-11 15:56:43 -0800240 new = kmalloc(sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 if (!new)
242 goto out_no_clnt;
243 memcpy(new, clnt, sizeof(*new));
244 atomic_set(&new->cl_count, 1);
245 atomic_set(&new->cl_users, 0);
246 new->cl_parent = clnt;
247 atomic_inc(&clnt->cl_count);
248 /* Duplicate portmapper */
249 rpc_init_wait_queue(&new->cl_pmap_default.pm_bindwait, "bindwait");
250 /* Turn off autobind on clones */
251 new->cl_autobind = 0;
252 new->cl_oneshot = 0;
253 new->cl_dead = 0;
Trond Myklebust54281542006-03-20 13:44:49 -0500254 if (!IS_ERR(new->cl_dentry)) {
255 dget(new->cl_dentry);
256 rpc_get_mount();
257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 rpc_init_rtt(&new->cl_rtt_default, clnt->cl_xprt->timeout.to_initval);
259 if (new->cl_auth)
260 atomic_inc(&new->cl_auth->au_count);
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000261 new->cl_pmap = &new->cl_pmap_default;
Chuck Lever11c556b2006-03-20 13:44:22 -0500262 new->cl_metrics = rpc_alloc_iostats(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 return new;
264out_no_clnt:
265 printk(KERN_INFO "RPC: out of memory in %s\n", __FUNCTION__);
266 return ERR_PTR(-ENOMEM);
267}
268
269/*
270 * Properly shut down an RPC client, terminating all outstanding
271 * requests. Note that we must be certain that cl_oneshot and
272 * cl_dead are cleared, or else the client would be destroyed
273 * when the last task releases it.
274 */
275int
276rpc_shutdown_client(struct rpc_clnt *clnt)
277{
278 dprintk("RPC: shutting down %s client for %s, tasks=%d\n",
279 clnt->cl_protname, clnt->cl_server,
280 atomic_read(&clnt->cl_users));
281
282 while (atomic_read(&clnt->cl_users) > 0) {
283 /* Don't let rpc_release_client destroy us */
284 clnt->cl_oneshot = 0;
285 clnt->cl_dead = 0;
286 rpc_killall_tasks(clnt);
Ingo Molnar532347e2006-01-09 20:52:53 -0800287 wait_event_timeout(destroy_wait,
Linus Torvalds4f477072006-01-10 08:56:39 -0800288 !atomic_read(&clnt->cl_users), 1*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 }
290
291 if (atomic_read(&clnt->cl_users) < 0) {
292 printk(KERN_ERR "RPC: rpc_shutdown_client clnt %p tasks=%d\n",
293 clnt, atomic_read(&clnt->cl_users));
294#ifdef RPC_DEBUG
295 rpc_show_tasks();
296#endif
297 BUG();
298 }
299
300 return rpc_destroy_client(clnt);
301}
302
303/*
304 * Delete an RPC client
305 */
306int
307rpc_destroy_client(struct rpc_clnt *clnt)
308{
309 if (!atomic_dec_and_test(&clnt->cl_count))
310 return 1;
311 BUG_ON(atomic_read(&clnt->cl_users) != 0);
312
313 dprintk("RPC: destroying %s client for %s\n",
314 clnt->cl_protname, clnt->cl_server);
315 if (clnt->cl_auth) {
316 rpcauth_destroy(clnt->cl_auth);
317 clnt->cl_auth = NULL;
318 }
319 if (clnt->cl_parent != clnt) {
320 rpc_destroy_client(clnt->cl_parent);
321 goto out_free;
322 }
Trond Myklebustf1345852005-09-23 11:08:25 -0400323 if (clnt->cl_pathname[0])
324 rpc_rmdir(clnt->cl_pathname);
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;
Trond Myklebust54281542006-03-20 13:44:49 -0500334 if (!IS_ERR(clnt->cl_dentry)) {
Trond Myklebust12de3b32006-03-20 13:44:09 -0500335 dput(clnt->cl_dentry);
Trond Myklebust54281542006-03-20 13:44:49 -0500336 rpc_put_mount();
337 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 kfree(clnt);
339 return 0;
340}
341
342/*
343 * Release an RPC client
344 */
345void
346rpc_release_client(struct rpc_clnt *clnt)
347{
348 dprintk("RPC: rpc_release_client(%p, %d)\n",
349 clnt, atomic_read(&clnt->cl_users));
350
351 if (!atomic_dec_and_test(&clnt->cl_users))
352 return;
353 wake_up(&destroy_wait);
354 if (clnt->cl_oneshot || clnt->cl_dead)
355 rpc_destroy_client(clnt);
356}
357
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000358/**
359 * rpc_bind_new_program - bind a new RPC program to an existing client
360 * @old - old rpc_client
361 * @program - rpc program to set
362 * @vers - rpc program version
363 *
364 * Clones the rpc client and sets up a new RPC program. This is mainly
365 * of use for enabling different RPC programs to share the same transport.
366 * The Sun NFSv2/v3 ACL protocol can do this.
367 */
368struct rpc_clnt *rpc_bind_new_program(struct rpc_clnt *old,
369 struct rpc_program *program,
370 int vers)
371{
372 struct rpc_clnt *clnt;
373 struct rpc_version *version;
374 int err;
375
376 BUG_ON(vers >= program->nrvers || !program->version[vers]);
377 version = program->version[vers];
378 clnt = rpc_clone_client(old);
379 if (IS_ERR(clnt))
380 goto out;
381 clnt->cl_procinfo = version->procs;
382 clnt->cl_maxproc = version->nrprocs;
383 clnt->cl_protname = program->name;
384 clnt->cl_prog = program->number;
385 clnt->cl_vers = version->number;
386 clnt->cl_stats = program->stats;
387 err = rpc_ping(clnt, RPC_TASK_SOFT|RPC_TASK_NOINTR);
388 if (err != 0) {
389 rpc_shutdown_client(clnt);
390 clnt = ERR_PTR(err);
391 }
392out:
393 return clnt;
394}
395
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396/*
397 * Default callback for async RPC calls
398 */
399static void
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100400rpc_default_callback(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401{
402}
403
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100404static const struct rpc_call_ops rpc_default_ops = {
405 .rpc_call_done = rpc_default_callback,
406};
407
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408/*
Trond Myklebust14b218a2005-06-22 17:16:28 +0000409 * Export the signal mask handling for synchronous code that
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 * sleeps on RPC calls
411 */
Trond Myklebust2bd61572006-01-03 09:55:19 +0100412#define RPC_INTR_SIGNALS (sigmask(SIGHUP) | sigmask(SIGINT) | sigmask(SIGQUIT) | sigmask(SIGTERM))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Trond Myklebust14b218a2005-06-22 17:16:28 +0000414static void rpc_save_sigmask(sigset_t *oldset, int intr)
415{
Trond Myklebust2bd61572006-01-03 09:55:19 +0100416 unsigned long sigallow = sigmask(SIGKILL);
Trond Myklebust14b218a2005-06-22 17:16:28 +0000417 sigset_t sigmask;
418
419 /* Block all signals except those listed in sigallow */
420 if (intr)
421 sigallow |= RPC_INTR_SIGNALS;
422 siginitsetinv(&sigmask, sigallow);
423 sigprocmask(SIG_BLOCK, &sigmask, oldset);
424}
425
426static inline void rpc_task_sigmask(struct rpc_task *task, sigset_t *oldset)
427{
428 rpc_save_sigmask(oldset, !RPC_TASK_UNINTERRUPTIBLE(task));
429}
430
431static inline void rpc_restore_sigmask(sigset_t *oldset)
432{
433 sigprocmask(SIG_SETMASK, oldset, NULL);
434}
435
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436void rpc_clnt_sigmask(struct rpc_clnt *clnt, sigset_t *oldset)
437{
Trond Myklebust14b218a2005-06-22 17:16:28 +0000438 rpc_save_sigmask(oldset, clnt->cl_intr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439}
440
441void rpc_clnt_sigunmask(struct rpc_clnt *clnt, sigset_t *oldset)
442{
Trond Myklebust14b218a2005-06-22 17:16:28 +0000443 rpc_restore_sigmask(oldset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444}
445
446/*
447 * New rpc_call implementation
448 */
449int rpc_call_sync(struct rpc_clnt *clnt, struct rpc_message *msg, int flags)
450{
451 struct rpc_task *task;
452 sigset_t oldset;
453 int status;
454
455 /* If this client is slain all further I/O fails */
456 if (clnt->cl_dead)
457 return -EIO;
458
459 BUG_ON(flags & RPC_TASK_ASYNC);
460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 status = -ENOMEM;
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100462 task = rpc_new_task(clnt, flags, &rpc_default_ops, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 if (task == NULL)
464 goto out;
465
Trond Myklebust14b218a2005-06-22 17:16:28 +0000466 /* Mask signals on RPC calls _and_ GSS_AUTH upcalls */
467 rpc_task_sigmask(task, &oldset);
468
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 rpc_call_setup(task, msg, 0);
470
471 /* Set up the call info struct and execute the task */
Trond Myklebuste60859a2006-01-03 09:55:10 +0100472 status = task->tk_status;
473 if (status == 0) {
474 atomic_inc(&task->tk_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 status = rpc_execute(task);
Trond Myklebuste60859a2006-01-03 09:55:10 +0100476 if (status == 0)
477 status = task->tk_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 }
Trond Myklebust14b218a2005-06-22 17:16:28 +0000479 rpc_restore_sigmask(&oldset);
Trond Myklebuste60859a2006-01-03 09:55:10 +0100480 rpc_release_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 return status;
483}
484
485/*
486 * New rpc_call implementation
487 */
488int
489rpc_call_async(struct rpc_clnt *clnt, struct rpc_message *msg, int flags,
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100490 const struct rpc_call_ops *tk_ops, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491{
492 struct rpc_task *task;
493 sigset_t oldset;
494 int status;
495
496 /* If this client is slain all further I/O fails */
Trond Myklebust7a1218a2006-03-20 18:11:10 -0500497 status = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 if (clnt->cl_dead)
Trond Myklebust7a1218a2006-03-20 18:11:10 -0500499 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
501 flags |= RPC_TASK_ASYNC;
502
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 /* Create/initialize a new RPC task */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 status = -ENOMEM;
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100505 if (!(task = rpc_new_task(clnt, flags, tk_ops, data)))
Trond Myklebust7a1218a2006-03-20 18:11:10 -0500506 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
Trond Myklebust14b218a2005-06-22 17:16:28 +0000508 /* Mask signals on GSS_AUTH upcalls */
509 rpc_task_sigmask(task, &oldset);
510
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 rpc_call_setup(task, msg, 0);
512
513 /* Set up the call info struct and execute the task */
514 status = task->tk_status;
515 if (status == 0)
516 rpc_execute(task);
517 else
518 rpc_release_task(task);
519
Trond Myklebust14b218a2005-06-22 17:16:28 +0000520 rpc_restore_sigmask(&oldset);
Trond Myklebust7a1218a2006-03-20 18:11:10 -0500521 return status;
522out_release:
523 if (tk_ops->rpc_release != NULL)
524 tk_ops->rpc_release(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 return status;
526}
527
528
529void
530rpc_call_setup(struct rpc_task *task, struct rpc_message *msg, int flags)
531{
532 task->tk_msg = *msg;
533 task->tk_flags |= flags;
534 /* Bind the user cred */
535 if (task->tk_msg.rpc_cred != NULL)
536 rpcauth_holdcred(task);
537 else
538 rpcauth_bindcred(task);
539
540 if (task->tk_status == 0)
541 task->tk_action = call_start;
542 else
Trond Myklebustabbcf282006-01-03 09:55:03 +0100543 task->tk_action = rpc_exit_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544}
545
546void
547rpc_setbufsize(struct rpc_clnt *clnt, unsigned int sndsize, unsigned int rcvsize)
548{
549 struct rpc_xprt *xprt = clnt->cl_xprt;
Chuck Lever470056c2005-08-25 16:25:56 -0700550 if (xprt->ops->set_buffer_size)
551 xprt->ops->set_buffer_size(xprt, sndsize, rcvsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552}
553
554/*
555 * Return size of largest payload RPC client can support, in bytes
556 *
557 * For stream transports, this is one RPC record fragment (see RFC
558 * 1831), as we don't support multi-record requests yet. For datagram
559 * transports, this is the size of an IP packet minus the IP, UDP, and
560 * RPC header sizes.
561 */
562size_t rpc_max_payload(struct rpc_clnt *clnt)
563{
564 return clnt->cl_xprt->max_payload;
565}
566EXPORT_SYMBOL(rpc_max_payload);
567
Chuck Lever35f5a422006-01-03 09:55:50 +0100568/**
569 * rpc_force_rebind - force transport to check that remote port is unchanged
570 * @clnt: client to rebind
571 *
572 */
573void rpc_force_rebind(struct rpc_clnt *clnt)
574{
575 if (clnt->cl_autobind)
576 clnt->cl_port = 0;
577}
578EXPORT_SYMBOL(rpc_force_rebind);
579
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580/*
581 * Restart an (async) RPC call. Usually called from within the
582 * exit handler.
583 */
584void
585rpc_restart_call(struct rpc_task *task)
586{
587 if (RPC_ASSASSINATED(task))
588 return;
589
590 task->tk_action = call_start;
591}
592
593/*
594 * 0. Initial state
595 *
596 * Other FSM states can be visited zero or more times, but
597 * this state is visited exactly once for each RPC.
598 */
599static void
600call_start(struct rpc_task *task)
601{
602 struct rpc_clnt *clnt = task->tk_client;
603
604 dprintk("RPC: %4d call_start %s%d proc %d (%s)\n", task->tk_pid,
605 clnt->cl_protname, clnt->cl_vers, task->tk_msg.rpc_proc->p_proc,
606 (RPC_IS_ASYNC(task) ? "async" : "sync"));
607
608 /* Increment call count */
609 task->tk_msg.rpc_proc->p_count++;
610 clnt->cl_stats->rpccnt++;
611 task->tk_action = call_reserve;
612}
613
614/*
615 * 1. Reserve an RPC call slot
616 */
617static void
618call_reserve(struct rpc_task *task)
619{
620 dprintk("RPC: %4d call_reserve\n", task->tk_pid);
621
622 if (!rpcauth_uptodatecred(task)) {
623 task->tk_action = call_refresh;
624 return;
625 }
626
627 task->tk_status = 0;
628 task->tk_action = call_reserveresult;
629 xprt_reserve(task);
630}
631
632/*
633 * 1b. Grok the result of xprt_reserve()
634 */
635static void
636call_reserveresult(struct rpc_task *task)
637{
638 int status = task->tk_status;
639
640 dprintk("RPC: %4d call_reserveresult (status %d)\n",
641 task->tk_pid, task->tk_status);
642
643 /*
644 * After a call to xprt_reserve(), we must have either
645 * a request slot or else an error status.
646 */
647 task->tk_status = 0;
648 if (status >= 0) {
649 if (task->tk_rqstp) {
650 task->tk_action = call_allocate;
651 return;
652 }
653
654 printk(KERN_ERR "%s: status=%d, but no request slot, exiting\n",
655 __FUNCTION__, status);
656 rpc_exit(task, -EIO);
657 return;
658 }
659
660 /*
661 * Even though there was an error, we may have acquired
662 * a request slot somehow. Make sure not to leak it.
663 */
664 if (task->tk_rqstp) {
665 printk(KERN_ERR "%s: status=%d, request allocated anyway\n",
666 __FUNCTION__, status);
667 xprt_release(task);
668 }
669
670 switch (status) {
671 case -EAGAIN: /* woken up; retry */
672 task->tk_action = call_reserve;
673 return;
674 case -EIO: /* probably a shutdown */
675 break;
676 default:
677 printk(KERN_ERR "%s: unrecognized error %d, exiting\n",
678 __FUNCTION__, status);
679 break;
680 }
681 rpc_exit(task, status);
682}
683
684/*
685 * 2. Allocate the buffer. For details, see sched.c:rpc_malloc.
Chuck Lever02107142006-01-03 09:55:49 +0100686 * (Note: buffer memory is freed in xprt_release).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 */
688static void
689call_allocate(struct rpc_task *task)
690{
Chuck Lever02107142006-01-03 09:55:49 +0100691 struct rpc_rqst *req = task->tk_rqstp;
692 struct rpc_xprt *xprt = task->tk_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 unsigned int bufsiz;
694
695 dprintk("RPC: %4d call_allocate (status %d)\n",
696 task->tk_pid, task->tk_status);
697 task->tk_action = call_bind;
Chuck Lever02107142006-01-03 09:55:49 +0100698 if (req->rq_buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 return;
700
701 /* FIXME: compute buffer requirements more exactly using
702 * auth->au_wslack */
703 bufsiz = task->tk_msg.rpc_proc->p_bufsiz + RPC_SLACK_SPACE;
704
Chuck Lever02107142006-01-03 09:55:49 +0100705 if (xprt->ops->buf_alloc(task, bufsiz << 1) != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 return;
707 printk(KERN_INFO "RPC: buffer allocation failed for task %p\n", task);
708
Trond Myklebust14b218a2005-06-22 17:16:28 +0000709 if (RPC_IS_ASYNC(task) || !signalled()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 xprt_release(task);
711 task->tk_action = call_reserve;
712 rpc_delay(task, HZ>>4);
713 return;
714 }
715
716 rpc_exit(task, -ERESTARTSYS);
717}
718
Trond Myklebust940e3312005-11-09 21:45:24 -0500719static inline int
720rpc_task_need_encode(struct rpc_task *task)
721{
722 return task->tk_rqstp->rq_snd_buf.len == 0;
723}
724
725static inline void
726rpc_task_force_reencode(struct rpc_task *task)
727{
728 task->tk_rqstp->rq_snd_buf.len = 0;
729}
730
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731/*
732 * 3. Encode arguments of an RPC call
733 */
734static void
735call_encode(struct rpc_task *task)
736{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 struct rpc_rqst *req = task->tk_rqstp;
738 struct xdr_buf *sndbuf = &req->rq_snd_buf;
739 struct xdr_buf *rcvbuf = &req->rq_rcv_buf;
740 unsigned int bufsiz;
741 kxdrproc_t encode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 u32 *p;
743
744 dprintk("RPC: %4d call_encode (status %d)\n",
745 task->tk_pid, task->tk_status);
746
747 /* Default buffer setup */
Chuck Lever02107142006-01-03 09:55:49 +0100748 bufsiz = req->rq_bufsize >> 1;
749 sndbuf->head[0].iov_base = (void *)req->rq_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 sndbuf->head[0].iov_len = bufsiz;
751 sndbuf->tail[0].iov_len = 0;
752 sndbuf->page_len = 0;
753 sndbuf->len = 0;
754 sndbuf->buflen = bufsiz;
Chuck Lever02107142006-01-03 09:55:49 +0100755 rcvbuf->head[0].iov_base = (void *)((char *)req->rq_buffer + bufsiz);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 rcvbuf->head[0].iov_len = bufsiz;
757 rcvbuf->tail[0].iov_len = 0;
758 rcvbuf->page_len = 0;
759 rcvbuf->len = 0;
760 rcvbuf->buflen = bufsiz;
761
762 /* Encode header and provided arguments */
763 encode = task->tk_msg.rpc_proc->p_encode;
764 if (!(p = call_header(task))) {
765 printk(KERN_INFO "RPC: call_header failed, exit EIO\n");
766 rpc_exit(task, -EIO);
767 return;
768 }
J. Bruce Fieldsf3680312005-10-13 16:54:48 -0400769 if (encode == NULL)
770 return;
771
772 task->tk_status = rpcauth_wrap_req(task, encode, req, p,
773 task->tk_msg.rpc_argp);
774 if (task->tk_status == -ENOMEM) {
775 /* XXX: Is this sane? */
776 rpc_delay(task, 3*HZ);
777 task->tk_status = -EAGAIN;
778 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779}
780
781/*
782 * 4. Get the server port number if not yet set
783 */
784static void
785call_bind(struct rpc_task *task)
786{
787 struct rpc_clnt *clnt = task->tk_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
Chuck Leverda351872005-08-11 16:25:11 -0400789 dprintk("RPC: %4d call_bind (status %d)\n",
790 task->tk_pid, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
Chuck Leverda351872005-08-11 16:25:11 -0400792 task->tk_action = call_connect;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 if (!clnt->cl_port) {
Chuck Leverda351872005-08-11 16:25:11 -0400794 task->tk_action = call_bind_status;
Chuck Lever03bf4b72005-08-25 16:25:55 -0700795 task->tk_timeout = task->tk_xprt->bind_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 rpc_getport(task, clnt);
797 }
798}
799
800/*
Chuck Leverda351872005-08-11 16:25:11 -0400801 * 4a. Sort out bind result
802 */
803static void
804call_bind_status(struct rpc_task *task)
805{
806 int status = -EACCES;
807
808 if (task->tk_status >= 0) {
809 dprintk("RPC: %4d call_bind_status (status %d)\n",
810 task->tk_pid, task->tk_status);
811 task->tk_status = 0;
812 task->tk_action = call_connect;
813 return;
814 }
815
816 switch (task->tk_status) {
817 case -EACCES:
818 dprintk("RPC: %4d remote rpcbind: RPC program/version unavailable\n",
819 task->tk_pid);
Chuck Leverea635a52005-10-06 23:12:58 -0400820 rpc_delay(task, 3*HZ);
821 goto retry_bind;
Chuck Leverda351872005-08-11 16:25:11 -0400822 case -ETIMEDOUT:
823 dprintk("RPC: %4d rpcbind request timed out\n",
824 task->tk_pid);
825 if (RPC_IS_SOFT(task)) {
826 status = -EIO;
827 break;
828 }
829 goto retry_bind;
830 case -EPFNOSUPPORT:
831 dprintk("RPC: %4d remote rpcbind service unavailable\n",
832 task->tk_pid);
833 break;
834 case -EPROTONOSUPPORT:
835 dprintk("RPC: %4d remote rpcbind version 2 unavailable\n",
836 task->tk_pid);
837 break;
838 default:
839 dprintk("RPC: %4d unrecognized rpcbind error (%d)\n",
840 task->tk_pid, -task->tk_status);
841 status = -EIO;
842 break;
843 }
844
845 rpc_exit(task, status);
846 return;
847
848retry_bind:
849 task->tk_status = 0;
850 task->tk_action = call_bind;
851 return;
852}
853
854/*
855 * 4b. Connect to the RPC server
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 */
857static void
858call_connect(struct rpc_task *task)
859{
Chuck Leverda351872005-08-11 16:25:11 -0400860 struct rpc_xprt *xprt = task->tk_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
Chuck Leverda351872005-08-11 16:25:11 -0400862 dprintk("RPC: %4d call_connect xprt %p %s connected\n",
863 task->tk_pid, xprt,
864 (xprt_connected(xprt) ? "is" : "is not"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
Chuck Leverda351872005-08-11 16:25:11 -0400866 task->tk_action = call_transmit;
867 if (!xprt_connected(xprt)) {
868 task->tk_action = call_connect_status;
869 if (task->tk_status < 0)
870 return;
871 xprt_connect(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873}
874
875/*
Chuck Leverda351872005-08-11 16:25:11 -0400876 * 4c. Sort out connect result
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 */
878static void
879call_connect_status(struct rpc_task *task)
880{
881 struct rpc_clnt *clnt = task->tk_client;
882 int status = task->tk_status;
883
Chuck Leverda351872005-08-11 16:25:11 -0400884 dprintk("RPC: %5u call_connect_status (status %d)\n",
885 task->tk_pid, task->tk_status);
886
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 task->tk_status = 0;
888 if (status >= 0) {
889 clnt->cl_stats->netreconn++;
890 task->tk_action = call_transmit;
891 return;
892 }
893
Chuck Leverda351872005-08-11 16:25:11 -0400894 /* Something failed: remote service port may have changed */
Chuck Lever35f5a422006-01-03 09:55:50 +0100895 rpc_force_rebind(clnt);
Chuck Leverda351872005-08-11 16:25:11 -0400896
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 switch (status) {
898 case -ENOTCONN:
899 case -ETIMEDOUT:
900 case -EAGAIN:
Chuck Leverda351872005-08-11 16:25:11 -0400901 task->tk_action = call_bind;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 break;
903 default:
904 rpc_exit(task, -EIO);
Chuck Leverda351872005-08-11 16:25:11 -0400905 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 }
907}
908
909/*
910 * 5. Transmit the RPC request, and wait for reply
911 */
912static void
913call_transmit(struct rpc_task *task)
914{
915 dprintk("RPC: %4d call_transmit (status %d)\n",
916 task->tk_pid, task->tk_status);
917
918 task->tk_action = call_status;
919 if (task->tk_status < 0)
920 return;
921 task->tk_status = xprt_prepare_transmit(task);
922 if (task->tk_status != 0)
923 return;
924 /* Encode here so that rpcsec_gss can use correct sequence number. */
Trond Myklebust940e3312005-11-09 21:45:24 -0500925 if (rpc_task_need_encode(task)) {
926 task->tk_rqstp->rq_bytes_sent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 call_encode(task);
Trond Myklebust5e5ce5b2005-10-18 14:20:11 -0700928 /* Did the encode result in an error condition? */
929 if (task->tk_status != 0)
930 goto out_nosend;
931 }
Trond Myklebust940e3312005-11-09 21:45:24 -0500932 task->tk_action = call_transmit_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 xprt_transmit(task);
934 if (task->tk_status < 0)
935 return;
936 if (!task->tk_msg.rpc_proc->p_decode) {
Trond Myklebustabbcf282006-01-03 09:55:03 +0100937 task->tk_action = rpc_exit_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 rpc_wake_up_task(task);
939 }
Trond Myklebust5e5ce5b2005-10-18 14:20:11 -0700940 return;
941out_nosend:
942 /* release socket write lock before attempting to handle error */
943 xprt_abort_transmit(task);
Trond Myklebust940e3312005-11-09 21:45:24 -0500944 rpc_task_force_reencode(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945}
946
947/*
948 * 6. Sort out the RPC call status
949 */
950static void
951call_status(struct rpc_task *task)
952{
953 struct rpc_clnt *clnt = task->tk_client;
954 struct rpc_rqst *req = task->tk_rqstp;
955 int status;
956
957 if (req->rq_received > 0 && !req->rq_bytes_sent)
958 task->tk_status = req->rq_received;
959
960 dprintk("RPC: %4d call_status (status %d)\n",
961 task->tk_pid, task->tk_status);
962
963 status = task->tk_status;
964 if (status >= 0) {
965 task->tk_action = call_decode;
966 return;
967 }
968
969 task->tk_status = 0;
970 switch(status) {
971 case -ETIMEDOUT:
972 task->tk_action = call_timeout;
973 break;
974 case -ECONNREFUSED:
975 case -ENOTCONN:
Chuck Lever35f5a422006-01-03 09:55:50 +0100976 rpc_force_rebind(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 task->tk_action = call_bind;
978 break;
979 case -EAGAIN:
980 task->tk_action = call_transmit;
981 break;
982 case -EIO:
983 /* shutdown or soft timeout */
984 rpc_exit(task, status);
985 break;
986 default:
Chuck Leverf518e35a2006-01-03 09:55:52 +0100987 printk("%s: RPC call returned error %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 clnt->cl_protname, -status);
989 rpc_exit(task, status);
990 break;
991 }
992}
993
994/*
Trond Myklebust940e3312005-11-09 21:45:24 -0500995 * 6a. Handle transmission errors.
996 */
997static void
998call_transmit_status(struct rpc_task *task)
999{
1000 if (task->tk_status != -EAGAIN)
1001 rpc_task_force_reencode(task);
1002 call_status(task);
1003}
1004
1005/*
1006 * 6b. Handle RPC timeout
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 * We do not release the request slot, so we keep using the
1008 * same XID for all retransmits.
1009 */
1010static void
1011call_timeout(struct rpc_task *task)
1012{
1013 struct rpc_clnt *clnt = task->tk_client;
1014
1015 if (xprt_adjust_timeout(task->tk_rqstp) == 0) {
1016 dprintk("RPC: %4d call_timeout (minor)\n", task->tk_pid);
1017 goto retry;
1018 }
1019
1020 dprintk("RPC: %4d call_timeout (major)\n", task->tk_pid);
Chuck Leveref759a22006-03-20 13:44:17 -05001021 task->tk_timeouts++;
1022
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 if (RPC_IS_SOFT(task)) {
Chuck Leverf518e35a2006-01-03 09:55:52 +01001024 printk(KERN_NOTICE "%s: server %s not responding, timed out\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 clnt->cl_protname, clnt->cl_server);
1026 rpc_exit(task, -EIO);
1027 return;
1028 }
1029
Chuck Leverf518e35a2006-01-03 09:55:52 +01001030 if (!(task->tk_flags & RPC_CALL_MAJORSEEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 task->tk_flags |= RPC_CALL_MAJORSEEN;
1032 printk(KERN_NOTICE "%s: server %s not responding, still trying\n",
1033 clnt->cl_protname, clnt->cl_server);
1034 }
Chuck Lever35f5a422006-01-03 09:55:50 +01001035 rpc_force_rebind(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
1037retry:
1038 clnt->cl_stats->rpcretrans++;
1039 task->tk_action = call_bind;
1040 task->tk_status = 0;
1041}
1042
1043/*
1044 * 7. Decode the RPC reply
1045 */
1046static void
1047call_decode(struct rpc_task *task)
1048{
1049 struct rpc_clnt *clnt = task->tk_client;
1050 struct rpc_rqst *req = task->tk_rqstp;
1051 kxdrproc_t decode = task->tk_msg.rpc_proc->p_decode;
1052 u32 *p;
1053
1054 dprintk("RPC: %4d call_decode (status %d)\n",
1055 task->tk_pid, task->tk_status);
1056
Chuck Leverf518e35a2006-01-03 09:55:52 +01001057 if (task->tk_flags & RPC_CALL_MAJORSEEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 printk(KERN_NOTICE "%s: server %s OK\n",
1059 clnt->cl_protname, clnt->cl_server);
1060 task->tk_flags &= ~RPC_CALL_MAJORSEEN;
1061 }
1062
1063 if (task->tk_status < 12) {
1064 if (!RPC_IS_SOFT(task)) {
1065 task->tk_action = call_bind;
1066 clnt->cl_stats->rpcretrans++;
1067 goto out_retry;
1068 }
1069 printk(KERN_WARNING "%s: too small RPC reply size (%d bytes)\n",
1070 clnt->cl_protname, task->tk_status);
1071 rpc_exit(task, -EIO);
1072 return;
1073 }
1074
Trond Myklebust43ac3f22006-03-20 13:44:51 -05001075 /*
1076 * Ensure that we see all writes made by xprt_complete_rqst()
1077 * before it changed req->rq_received.
1078 */
1079 smp_rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 req->rq_rcv_buf.len = req->rq_private_buf.len;
1081
1082 /* Check that the softirq receive buffer is valid */
1083 WARN_ON(memcmp(&req->rq_rcv_buf, &req->rq_private_buf,
1084 sizeof(req->rq_rcv_buf)) != 0);
1085
1086 /* Verify the RPC header */
Trond Myklebustabbcf282006-01-03 09:55:03 +01001087 p = call_verify(task);
1088 if (IS_ERR(p)) {
1089 if (p == ERR_PTR(-EAGAIN))
1090 goto out_retry;
1091 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 }
1093
Trond Myklebustabbcf282006-01-03 09:55:03 +01001094 task->tk_action = rpc_exit_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
1096 if (decode)
1097 task->tk_status = rpcauth_unwrap_resp(task, decode, req, p,
1098 task->tk_msg.rpc_resp);
1099 dprintk("RPC: %4d call_decode result %d\n", task->tk_pid,
1100 task->tk_status);
1101 return;
1102out_retry:
1103 req->rq_received = req->rq_private_buf.len = 0;
1104 task->tk_status = 0;
1105}
1106
1107/*
1108 * 8. Refresh the credentials if rejected by the server
1109 */
1110static void
1111call_refresh(struct rpc_task *task)
1112{
1113 dprintk("RPC: %4d call_refresh\n", task->tk_pid);
1114
1115 xprt_release(task); /* Must do to obtain new XID */
1116 task->tk_action = call_refreshresult;
1117 task->tk_status = 0;
1118 task->tk_client->cl_stats->rpcauthrefresh++;
1119 rpcauth_refreshcred(task);
1120}
1121
1122/*
1123 * 8a. Process the results of a credential refresh
1124 */
1125static void
1126call_refreshresult(struct rpc_task *task)
1127{
1128 int status = task->tk_status;
1129 dprintk("RPC: %4d call_refreshresult (status %d)\n",
1130 task->tk_pid, task->tk_status);
1131
1132 task->tk_status = 0;
1133 task->tk_action = call_reserve;
1134 if (status >= 0 && rpcauth_uptodatecred(task))
1135 return;
1136 if (status == -EACCES) {
1137 rpc_exit(task, -EACCES);
1138 return;
1139 }
1140 task->tk_action = call_refresh;
1141 if (status != -ETIMEDOUT)
1142 rpc_delay(task, 3*HZ);
1143 return;
1144}
1145
1146/*
1147 * Call header serialization
1148 */
1149static u32 *
1150call_header(struct rpc_task *task)
1151{
1152 struct rpc_clnt *clnt = task->tk_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 struct rpc_rqst *req = task->tk_rqstp;
1154 u32 *p = req->rq_svec[0].iov_base;
1155
1156 /* FIXME: check buffer size? */
Chuck Lever808012f2005-08-25 16:25:49 -07001157
1158 p = xprt_skip_transport_header(task->tk_xprt, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 *p++ = req->rq_xid; /* XID */
1160 *p++ = htonl(RPC_CALL); /* CALL */
1161 *p++ = htonl(RPC_VERSION); /* RPC version */
1162 *p++ = htonl(clnt->cl_prog); /* program number */
1163 *p++ = htonl(clnt->cl_vers); /* program version */
1164 *p++ = htonl(task->tk_msg.rpc_proc->p_proc); /* procedure */
Trond Myklebust334ccfd2005-06-22 17:16:19 +00001165 p = rpcauth_marshcred(task, p);
1166 req->rq_slen = xdr_adjust_iovec(&req->rq_svec[0], p);
1167 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168}
1169
1170/*
1171 * Reply header verification
1172 */
1173static u32 *
1174call_verify(struct rpc_task *task)
1175{
1176 struct kvec *iov = &task->tk_rqstp->rq_rcv_buf.head[0];
1177 int len = task->tk_rqstp->rq_rcv_buf.len >> 2;
1178 u32 *p = iov->iov_base, n;
1179 int error = -EACCES;
1180
1181 if ((len -= 3) < 0)
1182 goto out_overflow;
1183 p += 1; /* skip XID */
1184
1185 if ((n = ntohl(*p++)) != RPC_REPLY) {
1186 printk(KERN_WARNING "call_verify: not an RPC reply: %x\n", n);
Trond Myklebustabbcf282006-01-03 09:55:03 +01001187 goto out_garbage;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 }
1189 if ((n = ntohl(*p++)) != RPC_MSG_ACCEPTED) {
1190 if (--len < 0)
1191 goto out_overflow;
1192 switch ((n = ntohl(*p++))) {
1193 case RPC_AUTH_ERROR:
1194 break;
1195 case RPC_MISMATCH:
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00001196 dprintk("%s: RPC call version mismatch!\n", __FUNCTION__);
1197 error = -EPROTONOSUPPORT;
1198 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 default:
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00001200 dprintk("%s: RPC call rejected, unknown error: %x\n", __FUNCTION__, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 goto out_eio;
1202 }
1203 if (--len < 0)
1204 goto out_overflow;
1205 switch ((n = ntohl(*p++))) {
1206 case RPC_AUTH_REJECTEDCRED:
1207 case RPC_AUTH_REJECTEDVERF:
1208 case RPCSEC_GSS_CREDPROBLEM:
1209 case RPCSEC_GSS_CTXPROBLEM:
1210 if (!task->tk_cred_retry)
1211 break;
1212 task->tk_cred_retry--;
1213 dprintk("RPC: %4d call_verify: retry stale creds\n",
1214 task->tk_pid);
1215 rpcauth_invalcred(task);
1216 task->tk_action = call_refresh;
Trond Myklebustabbcf282006-01-03 09:55:03 +01001217 goto out_retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 case RPC_AUTH_BADCRED:
1219 case RPC_AUTH_BADVERF:
1220 /* possibly garbled cred/verf? */
1221 if (!task->tk_garb_retry)
1222 break;
1223 task->tk_garb_retry--;
1224 dprintk("RPC: %4d call_verify: retry garbled creds\n",
1225 task->tk_pid);
1226 task->tk_action = call_bind;
Trond Myklebustabbcf282006-01-03 09:55:03 +01001227 goto out_retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 case RPC_AUTH_TOOWEAK:
Levent Serinol1356b8c2006-03-20 13:44:11 -05001229 printk(KERN_NOTICE "call_verify: server %s requires stronger "
1230 "authentication.\n", task->tk_client->cl_server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 break;
1232 default:
1233 printk(KERN_WARNING "call_verify: unknown auth error: %x\n", n);
1234 error = -EIO;
1235 }
1236 dprintk("RPC: %4d call_verify: call rejected %d\n",
1237 task->tk_pid, n);
1238 goto out_err;
1239 }
1240 if (!(p = rpcauth_checkverf(task, p))) {
1241 printk(KERN_WARNING "call_verify: auth check failed\n");
Trond Myklebustabbcf282006-01-03 09:55:03 +01001242 goto out_garbage; /* bad verifier, retry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 }
1244 len = p - (u32 *)iov->iov_base - 1;
1245 if (len < 0)
1246 goto out_overflow;
1247 switch ((n = ntohl(*p++))) {
1248 case RPC_SUCCESS:
1249 return p;
1250 case RPC_PROG_UNAVAIL:
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00001251 dprintk("RPC: call_verify: program %u is unsupported by server %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 (unsigned int)task->tk_client->cl_prog,
1253 task->tk_client->cl_server);
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00001254 error = -EPFNOSUPPORT;
1255 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 case RPC_PROG_MISMATCH:
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00001257 dprintk("RPC: call_verify: program %u, version %u unsupported by server %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 (unsigned int)task->tk_client->cl_prog,
1259 (unsigned int)task->tk_client->cl_vers,
1260 task->tk_client->cl_server);
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00001261 error = -EPROTONOSUPPORT;
1262 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 case RPC_PROC_UNAVAIL:
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00001264 dprintk("RPC: call_verify: proc %p unsupported by program %u, version %u on server %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 task->tk_msg.rpc_proc,
1266 task->tk_client->cl_prog,
1267 task->tk_client->cl_vers,
1268 task->tk_client->cl_server);
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00001269 error = -EOPNOTSUPP;
1270 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 case RPC_GARBAGE_ARGS:
1272 dprintk("RPC: %4d %s: server saw garbage\n", task->tk_pid, __FUNCTION__);
1273 break; /* retry */
1274 default:
1275 printk(KERN_WARNING "call_verify: server accept status: %x\n", n);
1276 /* Also retry */
1277 }
1278
Trond Myklebustabbcf282006-01-03 09:55:03 +01001279out_garbage:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 task->tk_client->cl_stats->rpcgarbage++;
1281 if (task->tk_garb_retry) {
1282 task->tk_garb_retry--;
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00001283 dprintk("RPC %s: retrying %4d\n", __FUNCTION__, task->tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 task->tk_action = call_bind;
Trond Myklebustabbcf282006-01-03 09:55:03 +01001285out_retry:
1286 return ERR_PTR(-EAGAIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 }
1288 printk(KERN_WARNING "RPC %s: retry failed, exit EIO\n", __FUNCTION__);
1289out_eio:
1290 error = -EIO;
1291out_err:
1292 rpc_exit(task, error);
Trond Myklebustabbcf282006-01-03 09:55:03 +01001293 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294out_overflow:
1295 printk(KERN_WARNING "RPC %s: server reply was truncated.\n", __FUNCTION__);
Trond Myklebustabbcf282006-01-03 09:55:03 +01001296 goto out_garbage;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297}
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00001298
1299static int rpcproc_encode_null(void *rqstp, u32 *data, void *obj)
1300{
1301 return 0;
1302}
1303
1304static int rpcproc_decode_null(void *rqstp, u32 *data, void *obj)
1305{
1306 return 0;
1307}
1308
1309static struct rpc_procinfo rpcproc_null = {
1310 .p_encode = rpcproc_encode_null,
1311 .p_decode = rpcproc_decode_null,
1312};
1313
1314int rpc_ping(struct rpc_clnt *clnt, int flags)
1315{
1316 struct rpc_message msg = {
1317 .rpc_proc = &rpcproc_null,
1318 };
1319 int err;
1320 msg.rpc_cred = authnull_ops.lookup_cred(NULL, NULL, 0);
1321 err = rpc_call_sync(clnt, &msg, flags);
1322 put_rpccred(msg.rpc_cred);
1323 return err;
1324}