blob: dfeea4fea95a7f6dfb4ea038491aaba39ced48dc [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Chuck Lever55aa4f52005-08-11 16:25:47 -04002 * linux/net/sunrpc/clnt.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * This file contains the high-level RPC interface.
5 * It is modeled as a finite state machine to support both synchronous
6 * and asynchronous requests.
7 *
8 * - RPC header generation and argument serialization.
9 * - Credential refresh.
10 * - TCP connect handling.
11 * - Retry of operation when it is suspected the operation failed because
12 * of uid squashing on the server, or when the credentials were stale
13 * and need to be refreshed, or when a packet was damaged in transit.
14 * This may be have to be moved to the VFS layer.
15 *
16 * NB: BSD uses a more intelligent approach to guessing when a request
17 * or reply has been lost by keeping the RTO estimate for each procedure.
18 * We currently make do with a constant timeout value.
19 *
20 * Copyright (C) 1992,1993 Rick Sladkey <jrs@world.std.com>
21 * Copyright (C) 1995,1996 Olaf Kirch <okir@monad.swb.de>
22 */
23
24#include <asm/system.h>
25
26#include <linux/module.h>
27#include <linux/types.h>
28#include <linux/mm.h>
29#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/utsname.h>
Chuck Lever11c556b2006-03-20 13:44:22 -050031#include <linux/workqueue.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33#include <linux/sunrpc/clnt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/sunrpc/rpc_pipe_fs.h>
Chuck Lever11c556b2006-03-20 13:44:22 -050035#include <linux/sunrpc/metrics.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37
38#define RPC_SLACK_SPACE (1024) /* total overkill */
39
40#ifdef RPC_DEBUG
41# define RPCDBG_FACILITY RPCDBG_CALL
42#endif
43
44static DECLARE_WAIT_QUEUE_HEAD(destroy_wait);
45
46
47static void call_start(struct rpc_task *task);
48static void call_reserve(struct rpc_task *task);
49static void call_reserveresult(struct rpc_task *task);
50static void call_allocate(struct rpc_task *task);
51static void call_encode(struct rpc_task *task);
52static void call_decode(struct rpc_task *task);
53static void call_bind(struct rpc_task *task);
Chuck Leverda351872005-08-11 16:25:11 -040054static void call_bind_status(struct rpc_task *task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055static void call_transmit(struct rpc_task *task);
56static void call_status(struct rpc_task *task);
Trond Myklebust940e3312005-11-09 21:45:24 -050057static void call_transmit_status(struct rpc_task *task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058static void call_refresh(struct rpc_task *task);
59static void call_refreshresult(struct rpc_task *task);
60static void call_timeout(struct rpc_task *task);
61static void call_connect(struct rpc_task *task);
62static void call_connect_status(struct rpc_task *task);
Alexey Dobriyand8ed0292006-09-26 22:29:38 -070063static __be32 * call_header(struct rpc_task *task);
64static __be32 * call_verify(struct rpc_task *task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66
67static int
68rpc_setup_pipedir(struct rpc_clnt *clnt, char *dir_name)
69{
Trond Myklebustf1345852005-09-23 11:08:25 -040070 static uint32_t clntid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 int error;
72
Trond Myklebust54281542006-03-20 13:44:49 -050073 clnt->cl_vfsmnt = ERR_PTR(-ENOENT);
74 clnt->cl_dentry = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 if (dir_name == NULL)
76 return 0;
Trond Myklebust54281542006-03-20 13:44:49 -050077
78 clnt->cl_vfsmnt = rpc_get_mount();
79 if (IS_ERR(clnt->cl_vfsmnt))
80 return PTR_ERR(clnt->cl_vfsmnt);
81
Trond Myklebustf1345852005-09-23 11:08:25 -040082 for (;;) {
83 snprintf(clnt->cl_pathname, sizeof(clnt->cl_pathname),
84 "%s/clnt%x", dir_name,
85 (unsigned int)clntid++);
86 clnt->cl_pathname[sizeof(clnt->cl_pathname) - 1] = '\0';
87 clnt->cl_dentry = rpc_mkdir(clnt->cl_pathname, clnt);
88 if (!IS_ERR(clnt->cl_dentry))
89 return 0;
Christoph Hellwig278c9952005-07-24 23:53:01 +010090 error = PTR_ERR(clnt->cl_dentry);
Trond Myklebustf1345852005-09-23 11:08:25 -040091 if (error != -EEXIST) {
92 printk(KERN_INFO "RPC: Couldn't create pipefs entry %s, error %d\n",
93 clnt->cl_pathname, error);
Trond Myklebust54281542006-03-20 13:44:49 -050094 rpc_put_mount();
Trond Myklebustf1345852005-09-23 11:08:25 -040095 return error;
96 }
Christoph Hellwig278c9952005-07-24 23:53:01 +010097 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070098}
99
Chuck Leverff9aa5e2006-08-22 20:06:21 -0400100static struct rpc_clnt * rpc_new_client(struct rpc_xprt *xprt, char *servname, struct rpc_program *program, u32 vers, rpc_authflavor_t flavor)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101{
102 struct rpc_version *version;
103 struct rpc_clnt *clnt = NULL;
J. Bruce Fields6a192752005-06-22 17:16:23 +0000104 struct rpc_auth *auth;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 int err;
106 int len;
107
108 dprintk("RPC: creating %s client for %s (xprt %p)\n",
109 program->name, servname, xprt);
110
111 err = -EINVAL;
112 if (!xprt)
Adrian Bunk712917d2006-03-13 21:20:47 -0800113 goto out_no_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 if (vers >= program->nrvers || !(version = program->version[vers]))
115 goto out_err;
116
117 err = -ENOMEM;
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700118 clnt = kzalloc(sizeof(*clnt), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 if (!clnt)
120 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 atomic_set(&clnt->cl_users, 0);
122 atomic_set(&clnt->cl_count, 1);
123 clnt->cl_parent = clnt;
124
125 clnt->cl_server = clnt->cl_inline_name;
126 len = strlen(servname) + 1;
127 if (len > sizeof(clnt->cl_inline_name)) {
128 char *buf = kmalloc(len, GFP_KERNEL);
129 if (buf != 0)
130 clnt->cl_server = buf;
131 else
132 len = sizeof(clnt->cl_inline_name);
133 }
134 strlcpy(clnt->cl_server, servname, len);
135
136 clnt->cl_xprt = xprt;
137 clnt->cl_procinfo = version->procs;
138 clnt->cl_maxproc = version->nrprocs;
139 clnt->cl_protname = program->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 clnt->cl_prog = program->number;
141 clnt->cl_vers = version->number;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 clnt->cl_stats = program->stats;
Chuck Lever11c556b2006-03-20 13:44:22 -0500143 clnt->cl_metrics = rpc_alloc_iostats(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Chuck Leverec739ef2006-08-22 20:06:15 -0400145 if (!xprt_bound(clnt->cl_xprt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 clnt->cl_autobind = 1;
147
148 clnt->cl_rtt = &clnt->cl_rtt_default;
149 rpc_init_rtt(&clnt->cl_rtt_default, xprt->timeout.to_initval);
150
151 err = rpc_setup_pipedir(clnt, program->pipe_dir_name);
152 if (err < 0)
153 goto out_no_path;
154
J. Bruce Fields6a192752005-06-22 17:16:23 +0000155 auth = rpcauth_create(flavor, clnt);
156 if (IS_ERR(auth)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 printk(KERN_INFO "RPC: Couldn't create auth handle (flavor %u)\n",
158 flavor);
J. Bruce Fields6a192752005-06-22 17:16:23 +0000159 err = PTR_ERR(auth);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 goto out_no_auth;
161 }
162
163 /* save the nodename */
Serge E. Hallyne9ff3992006-10-02 02:18:11 -0700164 clnt->cl_nodelen = strlen(utsname()->nodename);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 if (clnt->cl_nodelen > UNX_MAXNODENAME)
166 clnt->cl_nodelen = UNX_MAXNODENAME;
Serge E. Hallyne9ff3992006-10-02 02:18:11 -0700167 memcpy(clnt->cl_nodename, utsname()->nodename, clnt->cl_nodelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 return clnt;
169
170out_no_auth:
Trond Myklebust54281542006-03-20 13:44:49 -0500171 if (!IS_ERR(clnt->cl_dentry)) {
Trond Myklebustdff02cc2006-07-31 14:17:18 -0700172 rpc_rmdir(clnt->cl_dentry);
Trond Myklebust54281542006-03-20 13:44:49 -0500173 rpc_put_mount();
174 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175out_no_path:
176 if (clnt->cl_server != clnt->cl_inline_name)
177 kfree(clnt->cl_server);
178 kfree(clnt);
179out_err:
Trond Myklebust6b6ca862006-09-05 12:55:57 -0400180 xprt_put(xprt);
Adrian Bunk712917d2006-03-13 21:20:47 -0800181out_no_xprt:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 return ERR_PTR(err);
183}
184
Chuck Leverc2866762006-08-22 20:06:20 -0400185/*
186 * rpc_create - create an RPC client and transport with one call
187 * @args: rpc_clnt create argument structure
188 *
189 * Creates and initializes an RPC transport and an RPC client.
190 *
191 * It can ping the server in order to determine if it is up, and to see if
192 * it supports this program and version. RPC_CLNT_CREATE_NOPING disables
193 * this behavior so asynchronous tasks can also use rpc_create.
194 */
195struct rpc_clnt *rpc_create(struct rpc_create_args *args)
196{
197 struct rpc_xprt *xprt;
198 struct rpc_clnt *clnt;
199
200 xprt = xprt_create_transport(args->protocol, args->address,
201 args->addrsize, args->timeout);
202 if (IS_ERR(xprt))
203 return (struct rpc_clnt *)xprt;
204
205 /*
206 * By default, kernel RPC client connects from a reserved port.
207 * CAP_NET_BIND_SERVICE will not be set for unprivileged requesters,
208 * but it is always enabled for rpciod, which handles the connect
209 * operation.
210 */
211 xprt->resvport = 1;
212 if (args->flags & RPC_CLNT_CREATE_NONPRIVPORT)
213 xprt->resvport = 0;
214
215 dprintk("RPC: creating %s client for %s (xprt %p)\n",
216 args->program->name, args->servername, xprt);
217
218 clnt = rpc_new_client(xprt, args->servername, args->program,
219 args->version, args->authflavor);
220 if (IS_ERR(clnt))
221 return clnt;
222
223 if (!(args->flags & RPC_CLNT_CREATE_NOPING)) {
224 int err = rpc_ping(clnt, RPC_TASK_SOFT|RPC_TASK_NOINTR);
225 if (err != 0) {
226 rpc_shutdown_client(clnt);
227 return ERR_PTR(err);
228 }
229 }
230
231 clnt->cl_softrtry = 1;
232 if (args->flags & RPC_CLNT_CREATE_HARDRTRY)
233 clnt->cl_softrtry = 0;
234
235 if (args->flags & RPC_CLNT_CREATE_INTR)
236 clnt->cl_intr = 1;
237 if (args->flags & RPC_CLNT_CREATE_AUTOBIND)
238 clnt->cl_autobind = 1;
239 if (args->flags & RPC_CLNT_CREATE_ONESHOT)
240 clnt->cl_oneshot = 1;
241
242 return clnt;
243}
Chuck Leverb86acd52006-08-22 20:06:22 -0400244EXPORT_SYMBOL_GPL(rpc_create);
Chuck Leverc2866762006-08-22 20:06:20 -0400245
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246/*
247 * This function clones the RPC client structure. It allows us to share the
248 * same transport while varying parameters such as the authentication
249 * flavour.
250 */
251struct rpc_clnt *
252rpc_clone_client(struct rpc_clnt *clnt)
253{
254 struct rpc_clnt *new;
255
Arnaldo Carvalho de Meloe69062b2006-11-21 01:21:34 -0200256 new = kmemdup(clnt, sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 if (!new)
258 goto out_no_clnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 atomic_set(&new->cl_count, 1);
260 atomic_set(&new->cl_users, 0);
261 new->cl_parent = clnt;
262 atomic_inc(&clnt->cl_count);
Trond Myklebust6b6ca862006-09-05 12:55:57 -0400263 new->cl_xprt = xprt_get(clnt->cl_xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 /* Turn off autobind on clones */
265 new->cl_autobind = 0;
266 new->cl_oneshot = 0;
267 new->cl_dead = 0;
Trond Myklebust8f8e7a52006-08-14 13:11:15 -0400268 if (!IS_ERR(new->cl_dentry))
Trond Myklebust54281542006-03-20 13:44:49 -0500269 dget(new->cl_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 rpc_init_rtt(&new->cl_rtt_default, clnt->cl_xprt->timeout.to_initval);
271 if (new->cl_auth)
272 atomic_inc(&new->cl_auth->au_count);
Chuck Lever4a681792006-08-22 20:06:15 -0400273 new->cl_metrics = rpc_alloc_iostats(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 return new;
275out_no_clnt:
276 printk(KERN_INFO "RPC: out of memory in %s\n", __FUNCTION__);
277 return ERR_PTR(-ENOMEM);
278}
279
280/*
281 * Properly shut down an RPC client, terminating all outstanding
282 * requests. Note that we must be certain that cl_oneshot and
283 * cl_dead are cleared, or else the client would be destroyed
284 * when the last task releases it.
285 */
286int
287rpc_shutdown_client(struct rpc_clnt *clnt)
288{
289 dprintk("RPC: shutting down %s client for %s, tasks=%d\n",
290 clnt->cl_protname, clnt->cl_server,
291 atomic_read(&clnt->cl_users));
292
293 while (atomic_read(&clnt->cl_users) > 0) {
294 /* Don't let rpc_release_client destroy us */
295 clnt->cl_oneshot = 0;
296 clnt->cl_dead = 0;
297 rpc_killall_tasks(clnt);
Ingo Molnar532347e2006-01-09 20:52:53 -0800298 wait_event_timeout(destroy_wait,
Linus Torvalds4f477072006-01-10 08:56:39 -0800299 !atomic_read(&clnt->cl_users), 1*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 }
301
302 if (atomic_read(&clnt->cl_users) < 0) {
303 printk(KERN_ERR "RPC: rpc_shutdown_client clnt %p tasks=%d\n",
304 clnt, atomic_read(&clnt->cl_users));
305#ifdef RPC_DEBUG
306 rpc_show_tasks();
307#endif
308 BUG();
309 }
310
311 return rpc_destroy_client(clnt);
312}
313
314/*
315 * Delete an RPC client
316 */
317int
318rpc_destroy_client(struct rpc_clnt *clnt)
319{
320 if (!atomic_dec_and_test(&clnt->cl_count))
321 return 1;
322 BUG_ON(atomic_read(&clnt->cl_users) != 0);
323
324 dprintk("RPC: destroying %s client for %s\n",
325 clnt->cl_protname, clnt->cl_server);
326 if (clnt->cl_auth) {
327 rpcauth_destroy(clnt->cl_auth);
328 clnt->cl_auth = NULL;
329 }
330 if (clnt->cl_parent != clnt) {
Trond Myklebust8f8e7a52006-08-14 13:11:15 -0400331 if (!IS_ERR(clnt->cl_dentry))
332 dput(clnt->cl_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 rpc_destroy_client(clnt->cl_parent);
334 goto out_free;
335 }
Trond Myklebust8f8e7a52006-08-14 13:11:15 -0400336 if (!IS_ERR(clnt->cl_dentry)) {
Trond Myklebustdff02cc2006-07-31 14:17:18 -0700337 rpc_rmdir(clnt->cl_dentry);
Trond Myklebust8f8e7a52006-08-14 13:11:15 -0400338 rpc_put_mount();
339 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 if (clnt->cl_server != clnt->cl_inline_name)
341 kfree(clnt->cl_server);
342out_free:
Chuck Lever11c556b2006-03-20 13:44:22 -0500343 rpc_free_iostats(clnt->cl_metrics);
344 clnt->cl_metrics = NULL;
Trond Myklebust6b6ca862006-09-05 12:55:57 -0400345 xprt_put(clnt->cl_xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 kfree(clnt);
347 return 0;
348}
349
350/*
351 * Release an RPC client
352 */
353void
354rpc_release_client(struct rpc_clnt *clnt)
355{
356 dprintk("RPC: rpc_release_client(%p, %d)\n",
357 clnt, atomic_read(&clnt->cl_users));
358
359 if (!atomic_dec_and_test(&clnt->cl_users))
360 return;
361 wake_up(&destroy_wait);
362 if (clnt->cl_oneshot || clnt->cl_dead)
363 rpc_destroy_client(clnt);
364}
365
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000366/**
367 * rpc_bind_new_program - bind a new RPC program to an existing client
368 * @old - old rpc_client
369 * @program - rpc program to set
370 * @vers - rpc program version
371 *
372 * Clones the rpc client and sets up a new RPC program. This is mainly
373 * of use for enabling different RPC programs to share the same transport.
374 * The Sun NFSv2/v3 ACL protocol can do this.
375 */
376struct rpc_clnt *rpc_bind_new_program(struct rpc_clnt *old,
377 struct rpc_program *program,
378 int vers)
379{
380 struct rpc_clnt *clnt;
381 struct rpc_version *version;
382 int err;
383
384 BUG_ON(vers >= program->nrvers || !program->version[vers]);
385 version = program->version[vers];
386 clnt = rpc_clone_client(old);
387 if (IS_ERR(clnt))
388 goto out;
389 clnt->cl_procinfo = version->procs;
390 clnt->cl_maxproc = version->nrprocs;
391 clnt->cl_protname = program->name;
392 clnt->cl_prog = program->number;
393 clnt->cl_vers = version->number;
394 clnt->cl_stats = program->stats;
395 err = rpc_ping(clnt, RPC_TASK_SOFT|RPC_TASK_NOINTR);
396 if (err != 0) {
397 rpc_shutdown_client(clnt);
398 clnt = ERR_PTR(err);
399 }
400out:
401 return clnt;
402}
403
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404/*
405 * Default callback for async RPC calls
406 */
407static void
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100408rpc_default_callback(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409{
410}
411
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100412static const struct rpc_call_ops rpc_default_ops = {
413 .rpc_call_done = rpc_default_callback,
414};
415
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416/*
Trond Myklebust14b218a2005-06-22 17:16:28 +0000417 * Export the signal mask handling for synchronous code that
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 * sleeps on RPC calls
419 */
Trond Myklebust2bd61572006-01-03 09:55:19 +0100420#define RPC_INTR_SIGNALS (sigmask(SIGHUP) | sigmask(SIGINT) | sigmask(SIGQUIT) | sigmask(SIGTERM))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
Trond Myklebust14b218a2005-06-22 17:16:28 +0000422static void rpc_save_sigmask(sigset_t *oldset, int intr)
423{
Trond Myklebust2bd61572006-01-03 09:55:19 +0100424 unsigned long sigallow = sigmask(SIGKILL);
Trond Myklebust14b218a2005-06-22 17:16:28 +0000425 sigset_t sigmask;
426
427 /* Block all signals except those listed in sigallow */
428 if (intr)
429 sigallow |= RPC_INTR_SIGNALS;
430 siginitsetinv(&sigmask, sigallow);
431 sigprocmask(SIG_BLOCK, &sigmask, oldset);
432}
433
434static inline void rpc_task_sigmask(struct rpc_task *task, sigset_t *oldset)
435{
436 rpc_save_sigmask(oldset, !RPC_TASK_UNINTERRUPTIBLE(task));
437}
438
439static inline void rpc_restore_sigmask(sigset_t *oldset)
440{
441 sigprocmask(SIG_SETMASK, oldset, NULL);
442}
443
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444void rpc_clnt_sigmask(struct rpc_clnt *clnt, sigset_t *oldset)
445{
Trond Myklebust14b218a2005-06-22 17:16:28 +0000446 rpc_save_sigmask(oldset, clnt->cl_intr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447}
448
449void rpc_clnt_sigunmask(struct rpc_clnt *clnt, sigset_t *oldset)
450{
Trond Myklebust14b218a2005-06-22 17:16:28 +0000451 rpc_restore_sigmask(oldset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452}
453
454/*
455 * New rpc_call implementation
456 */
457int rpc_call_sync(struct rpc_clnt *clnt, struct rpc_message *msg, int flags)
458{
459 struct rpc_task *task;
460 sigset_t oldset;
461 int status;
462
463 /* If this client is slain all further I/O fails */
464 if (clnt->cl_dead)
465 return -EIO;
466
467 BUG_ON(flags & RPC_TASK_ASYNC);
468
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 status = -ENOMEM;
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100470 task = rpc_new_task(clnt, flags, &rpc_default_ops, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 if (task == NULL)
472 goto out;
473
Trond Myklebust14b218a2005-06-22 17:16:28 +0000474 /* Mask signals on RPC calls _and_ GSS_AUTH upcalls */
475 rpc_task_sigmask(task, &oldset);
476
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 rpc_call_setup(task, msg, 0);
478
479 /* Set up the call info struct and execute the task */
Trond Myklebuste60859a2006-01-03 09:55:10 +0100480 status = task->tk_status;
481 if (status == 0) {
482 atomic_inc(&task->tk_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 status = rpc_execute(task);
Trond Myklebuste60859a2006-01-03 09:55:10 +0100484 if (status == 0)
485 status = task->tk_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 }
Trond Myklebust14b218a2005-06-22 17:16:28 +0000487 rpc_restore_sigmask(&oldset);
Trond Myklebuste60859a2006-01-03 09:55:10 +0100488 rpc_release_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 return status;
491}
492
493/*
494 * New rpc_call implementation
495 */
496int
497rpc_call_async(struct rpc_clnt *clnt, struct rpc_message *msg, int flags,
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100498 const struct rpc_call_ops *tk_ops, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499{
500 struct rpc_task *task;
501 sigset_t oldset;
502 int status;
503
504 /* If this client is slain all further I/O fails */
Trond Myklebust7a1218a2006-03-20 18:11:10 -0500505 status = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 if (clnt->cl_dead)
Trond Myklebust7a1218a2006-03-20 18:11:10 -0500507 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
509 flags |= RPC_TASK_ASYNC;
510
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 /* Create/initialize a new RPC task */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 status = -ENOMEM;
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100513 if (!(task = rpc_new_task(clnt, flags, tk_ops, data)))
Trond Myklebust7a1218a2006-03-20 18:11:10 -0500514 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
Trond Myklebust14b218a2005-06-22 17:16:28 +0000516 /* Mask signals on GSS_AUTH upcalls */
517 rpc_task_sigmask(task, &oldset);
518
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 rpc_call_setup(task, msg, 0);
520
521 /* Set up the call info struct and execute the task */
522 status = task->tk_status;
523 if (status == 0)
524 rpc_execute(task);
525 else
526 rpc_release_task(task);
527
Trond Myklebust14b218a2005-06-22 17:16:28 +0000528 rpc_restore_sigmask(&oldset);
Trond Myklebust7a1218a2006-03-20 18:11:10 -0500529 return status;
530out_release:
531 if (tk_ops->rpc_release != NULL)
532 tk_ops->rpc_release(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 return status;
534}
535
536
537void
538rpc_call_setup(struct rpc_task *task, struct rpc_message *msg, int flags)
539{
540 task->tk_msg = *msg;
541 task->tk_flags |= flags;
542 /* Bind the user cred */
543 if (task->tk_msg.rpc_cred != NULL)
544 rpcauth_holdcred(task);
545 else
546 rpcauth_bindcred(task);
547
548 if (task->tk_status == 0)
549 task->tk_action = call_start;
550 else
Trond Myklebustabbcf282006-01-03 09:55:03 +0100551 task->tk_action = rpc_exit_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552}
553
Chuck Levered394402006-08-22 20:06:17 -0400554/**
555 * rpc_peeraddr - extract remote peer address from clnt's xprt
556 * @clnt: RPC client structure
557 * @buf: target buffer
558 * @size: length of target buffer
559 *
560 * Returns the number of bytes that are actually in the stored address.
561 */
562size_t rpc_peeraddr(struct rpc_clnt *clnt, struct sockaddr *buf, size_t bufsize)
563{
564 size_t bytes;
565 struct rpc_xprt *xprt = clnt->cl_xprt;
566
567 bytes = sizeof(xprt->addr);
568 if (bytes > bufsize)
569 bytes = bufsize;
570 memcpy(buf, &clnt->cl_xprt->addr, bytes);
Chuck Leverc4efcb12006-08-22 20:06:19 -0400571 return xprt->addrlen;
Chuck Levered394402006-08-22 20:06:17 -0400572}
Chuck Leverb86acd52006-08-22 20:06:22 -0400573EXPORT_SYMBOL_GPL(rpc_peeraddr);
Chuck Levered394402006-08-22 20:06:17 -0400574
Chuck Leverf425eba2006-08-22 20:06:18 -0400575/**
576 * rpc_peeraddr2str - return remote peer address in printable format
577 * @clnt: RPC client structure
578 * @format: address format
579 *
580 */
581char *rpc_peeraddr2str(struct rpc_clnt *clnt, enum rpc_display_format_t format)
582{
583 struct rpc_xprt *xprt = clnt->cl_xprt;
584 return xprt->ops->print_addr(xprt, format);
585}
Chuck Leverb86acd52006-08-22 20:06:22 -0400586EXPORT_SYMBOL_GPL(rpc_peeraddr2str);
Chuck Leverf425eba2006-08-22 20:06:18 -0400587
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588void
589rpc_setbufsize(struct rpc_clnt *clnt, unsigned int sndsize, unsigned int rcvsize)
590{
591 struct rpc_xprt *xprt = clnt->cl_xprt;
Chuck Lever470056c2005-08-25 16:25:56 -0700592 if (xprt->ops->set_buffer_size)
593 xprt->ops->set_buffer_size(xprt, sndsize, rcvsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594}
595
596/*
597 * Return size of largest payload RPC client can support, in bytes
598 *
599 * For stream transports, this is one RPC record fragment (see RFC
600 * 1831), as we don't support multi-record requests yet. For datagram
601 * transports, this is the size of an IP packet minus the IP, UDP, and
602 * RPC header sizes.
603 */
604size_t rpc_max_payload(struct rpc_clnt *clnt)
605{
606 return clnt->cl_xprt->max_payload;
607}
Chuck Leverb86acd52006-08-22 20:06:22 -0400608EXPORT_SYMBOL_GPL(rpc_max_payload);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
Chuck Lever35f5a422006-01-03 09:55:50 +0100610/**
611 * rpc_force_rebind - force transport to check that remote port is unchanged
612 * @clnt: client to rebind
613 *
614 */
615void rpc_force_rebind(struct rpc_clnt *clnt)
616{
617 if (clnt->cl_autobind)
Chuck Leverec739ef2006-08-22 20:06:15 -0400618 xprt_clear_bound(clnt->cl_xprt);
Chuck Lever35f5a422006-01-03 09:55:50 +0100619}
Chuck Leverb86acd52006-08-22 20:06:22 -0400620EXPORT_SYMBOL_GPL(rpc_force_rebind);
Chuck Lever35f5a422006-01-03 09:55:50 +0100621
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622/*
623 * Restart an (async) RPC call. Usually called from within the
624 * exit handler.
625 */
626void
627rpc_restart_call(struct rpc_task *task)
628{
629 if (RPC_ASSASSINATED(task))
630 return;
631
632 task->tk_action = call_start;
633}
634
635/*
636 * 0. Initial state
637 *
638 * Other FSM states can be visited zero or more times, but
639 * this state is visited exactly once for each RPC.
640 */
641static void
642call_start(struct rpc_task *task)
643{
644 struct rpc_clnt *clnt = task->tk_client;
645
646 dprintk("RPC: %4d call_start %s%d proc %d (%s)\n", task->tk_pid,
647 clnt->cl_protname, clnt->cl_vers, task->tk_msg.rpc_proc->p_proc,
648 (RPC_IS_ASYNC(task) ? "async" : "sync"));
649
650 /* Increment call count */
651 task->tk_msg.rpc_proc->p_count++;
652 clnt->cl_stats->rpccnt++;
653 task->tk_action = call_reserve;
654}
655
656/*
657 * 1. Reserve an RPC call slot
658 */
659static void
660call_reserve(struct rpc_task *task)
661{
662 dprintk("RPC: %4d call_reserve\n", task->tk_pid);
663
664 if (!rpcauth_uptodatecred(task)) {
665 task->tk_action = call_refresh;
666 return;
667 }
668
669 task->tk_status = 0;
670 task->tk_action = call_reserveresult;
671 xprt_reserve(task);
672}
673
674/*
675 * 1b. Grok the result of xprt_reserve()
676 */
677static void
678call_reserveresult(struct rpc_task *task)
679{
680 int status = task->tk_status;
681
682 dprintk("RPC: %4d call_reserveresult (status %d)\n",
683 task->tk_pid, task->tk_status);
684
685 /*
686 * After a call to xprt_reserve(), we must have either
687 * a request slot or else an error status.
688 */
689 task->tk_status = 0;
690 if (status >= 0) {
691 if (task->tk_rqstp) {
692 task->tk_action = call_allocate;
693 return;
694 }
695
696 printk(KERN_ERR "%s: status=%d, but no request slot, exiting\n",
697 __FUNCTION__, status);
698 rpc_exit(task, -EIO);
699 return;
700 }
701
702 /*
703 * Even though there was an error, we may have acquired
704 * a request slot somehow. Make sure not to leak it.
705 */
706 if (task->tk_rqstp) {
707 printk(KERN_ERR "%s: status=%d, request allocated anyway\n",
708 __FUNCTION__, status);
709 xprt_release(task);
710 }
711
712 switch (status) {
713 case -EAGAIN: /* woken up; retry */
714 task->tk_action = call_reserve;
715 return;
716 case -EIO: /* probably a shutdown */
717 break;
718 default:
719 printk(KERN_ERR "%s: unrecognized error %d, exiting\n",
720 __FUNCTION__, status);
721 break;
722 }
723 rpc_exit(task, status);
724}
725
726/*
727 * 2. Allocate the buffer. For details, see sched.c:rpc_malloc.
Chuck Lever02107142006-01-03 09:55:49 +0100728 * (Note: buffer memory is freed in xprt_release).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 */
730static void
731call_allocate(struct rpc_task *task)
732{
Chuck Lever02107142006-01-03 09:55:49 +0100733 struct rpc_rqst *req = task->tk_rqstp;
734 struct rpc_xprt *xprt = task->tk_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 unsigned int bufsiz;
736
737 dprintk("RPC: %4d call_allocate (status %d)\n",
738 task->tk_pid, task->tk_status);
739 task->tk_action = call_bind;
Chuck Lever02107142006-01-03 09:55:49 +0100740 if (req->rq_buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 return;
742
743 /* FIXME: compute buffer requirements more exactly using
744 * auth->au_wslack */
745 bufsiz = task->tk_msg.rpc_proc->p_bufsiz + RPC_SLACK_SPACE;
746
Chuck Lever02107142006-01-03 09:55:49 +0100747 if (xprt->ops->buf_alloc(task, bufsiz << 1) != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 return;
749 printk(KERN_INFO "RPC: buffer allocation failed for task %p\n", task);
750
Trond Myklebust14b218a2005-06-22 17:16:28 +0000751 if (RPC_IS_ASYNC(task) || !signalled()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 xprt_release(task);
753 task->tk_action = call_reserve;
754 rpc_delay(task, HZ>>4);
755 return;
756 }
757
758 rpc_exit(task, -ERESTARTSYS);
759}
760
Trond Myklebust940e3312005-11-09 21:45:24 -0500761static inline int
762rpc_task_need_encode(struct rpc_task *task)
763{
764 return task->tk_rqstp->rq_snd_buf.len == 0;
765}
766
767static inline void
768rpc_task_force_reencode(struct rpc_task *task)
769{
770 task->tk_rqstp->rq_snd_buf.len = 0;
771}
772
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773/*
774 * 3. Encode arguments of an RPC call
775 */
776static void
777call_encode(struct rpc_task *task)
778{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 struct rpc_rqst *req = task->tk_rqstp;
780 struct xdr_buf *sndbuf = &req->rq_snd_buf;
781 struct xdr_buf *rcvbuf = &req->rq_rcv_buf;
782 unsigned int bufsiz;
783 kxdrproc_t encode;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700784 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
786 dprintk("RPC: %4d call_encode (status %d)\n",
787 task->tk_pid, task->tk_status);
788
789 /* Default buffer setup */
Chuck Lever02107142006-01-03 09:55:49 +0100790 bufsiz = req->rq_bufsize >> 1;
791 sndbuf->head[0].iov_base = (void *)req->rq_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 sndbuf->head[0].iov_len = bufsiz;
793 sndbuf->tail[0].iov_len = 0;
794 sndbuf->page_len = 0;
795 sndbuf->len = 0;
796 sndbuf->buflen = bufsiz;
Chuck Lever02107142006-01-03 09:55:49 +0100797 rcvbuf->head[0].iov_base = (void *)((char *)req->rq_buffer + bufsiz);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 rcvbuf->head[0].iov_len = bufsiz;
799 rcvbuf->tail[0].iov_len = 0;
800 rcvbuf->page_len = 0;
801 rcvbuf->len = 0;
802 rcvbuf->buflen = bufsiz;
803
804 /* Encode header and provided arguments */
805 encode = task->tk_msg.rpc_proc->p_encode;
806 if (!(p = call_header(task))) {
807 printk(KERN_INFO "RPC: call_header failed, exit EIO\n");
808 rpc_exit(task, -EIO);
809 return;
810 }
J. Bruce Fieldsf3680312005-10-13 16:54:48 -0400811 if (encode == NULL)
812 return;
813
814 task->tk_status = rpcauth_wrap_req(task, encode, req, p,
815 task->tk_msg.rpc_argp);
816 if (task->tk_status == -ENOMEM) {
817 /* XXX: Is this sane? */
818 rpc_delay(task, 3*HZ);
819 task->tk_status = -EAGAIN;
820 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821}
822
823/*
824 * 4. Get the server port number if not yet set
825 */
826static void
827call_bind(struct rpc_task *task)
828{
Chuck Leverec739ef2006-08-22 20:06:15 -0400829 struct rpc_xprt *xprt = task->tk_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
Chuck Leverda351872005-08-11 16:25:11 -0400831 dprintk("RPC: %4d call_bind (status %d)\n",
832 task->tk_pid, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
Chuck Leverda351872005-08-11 16:25:11 -0400834 task->tk_action = call_connect;
Chuck Leverec739ef2006-08-22 20:06:15 -0400835 if (!xprt_bound(xprt)) {
Chuck Leverda351872005-08-11 16:25:11 -0400836 task->tk_action = call_bind_status;
Chuck Leverec739ef2006-08-22 20:06:15 -0400837 task->tk_timeout = xprt->bind_timeout;
Chuck Leverbbf7c1d2006-08-22 20:06:16 -0400838 xprt->ops->rpcbind(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 }
840}
841
842/*
Chuck Leverda351872005-08-11 16:25:11 -0400843 * 4a. Sort out bind result
844 */
845static void
846call_bind_status(struct rpc_task *task)
847{
848 int status = -EACCES;
849
850 if (task->tk_status >= 0) {
851 dprintk("RPC: %4d call_bind_status (status %d)\n",
852 task->tk_pid, task->tk_status);
853 task->tk_status = 0;
854 task->tk_action = call_connect;
855 return;
856 }
857
858 switch (task->tk_status) {
859 case -EACCES:
860 dprintk("RPC: %4d remote rpcbind: RPC program/version unavailable\n",
861 task->tk_pid);
Chuck Leverea635a52005-10-06 23:12:58 -0400862 rpc_delay(task, 3*HZ);
Trond Myklebustda458282006-08-31 15:44:52 -0400863 goto retry_timeout;
Chuck Leverda351872005-08-11 16:25:11 -0400864 case -ETIMEDOUT:
865 dprintk("RPC: %4d rpcbind request timed out\n",
866 task->tk_pid);
Trond Myklebustda458282006-08-31 15:44:52 -0400867 goto retry_timeout;
Chuck Leverda351872005-08-11 16:25:11 -0400868 case -EPFNOSUPPORT:
869 dprintk("RPC: %4d remote rpcbind service unavailable\n",
870 task->tk_pid);
871 break;
872 case -EPROTONOSUPPORT:
873 dprintk("RPC: %4d remote rpcbind version 2 unavailable\n",
874 task->tk_pid);
875 break;
876 default:
877 dprintk("RPC: %4d unrecognized rpcbind error (%d)\n",
878 task->tk_pid, -task->tk_status);
879 status = -EIO;
Chuck Leverda351872005-08-11 16:25:11 -0400880 }
881
882 rpc_exit(task, status);
883 return;
884
Trond Myklebustda458282006-08-31 15:44:52 -0400885retry_timeout:
886 task->tk_action = call_timeout;
Chuck Leverda351872005-08-11 16:25:11 -0400887}
888
889/*
890 * 4b. Connect to the RPC server
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 */
892static void
893call_connect(struct rpc_task *task)
894{
Chuck Leverda351872005-08-11 16:25:11 -0400895 struct rpc_xprt *xprt = task->tk_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
Chuck Leverda351872005-08-11 16:25:11 -0400897 dprintk("RPC: %4d call_connect xprt %p %s connected\n",
898 task->tk_pid, xprt,
899 (xprt_connected(xprt) ? "is" : "is not"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
Chuck Leverda351872005-08-11 16:25:11 -0400901 task->tk_action = call_transmit;
902 if (!xprt_connected(xprt)) {
903 task->tk_action = call_connect_status;
904 if (task->tk_status < 0)
905 return;
906 xprt_connect(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908}
909
910/*
Chuck Leverda351872005-08-11 16:25:11 -0400911 * 4c. Sort out connect result
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 */
913static void
914call_connect_status(struct rpc_task *task)
915{
916 struct rpc_clnt *clnt = task->tk_client;
917 int status = task->tk_status;
918
Chuck Leverda351872005-08-11 16:25:11 -0400919 dprintk("RPC: %5u call_connect_status (status %d)\n",
920 task->tk_pid, task->tk_status);
921
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 task->tk_status = 0;
923 if (status >= 0) {
924 clnt->cl_stats->netreconn++;
925 task->tk_action = call_transmit;
926 return;
927 }
928
Chuck Leverda351872005-08-11 16:25:11 -0400929 /* Something failed: remote service port may have changed */
Chuck Lever35f5a422006-01-03 09:55:50 +0100930 rpc_force_rebind(clnt);
Chuck Leverda351872005-08-11 16:25:11 -0400931
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 switch (status) {
933 case -ENOTCONN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 case -EAGAIN:
Chuck Leverda351872005-08-11 16:25:11 -0400935 task->tk_action = call_bind;
Trond Myklebustda458282006-08-31 15:44:52 -0400936 if (!RPC_IS_SOFT(task))
937 return;
938 /* if soft mounted, test if we've timed out */
939 case -ETIMEDOUT:
940 task->tk_action = call_timeout;
941 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 }
Trond Myklebustda458282006-08-31 15:44:52 -0400943 rpc_exit(task, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944}
945
946/*
947 * 5. Transmit the RPC request, and wait for reply
948 */
949static void
950call_transmit(struct rpc_task *task)
951{
952 dprintk("RPC: %4d call_transmit (status %d)\n",
953 task->tk_pid, task->tk_status);
954
955 task->tk_action = call_status;
956 if (task->tk_status < 0)
957 return;
958 task->tk_status = xprt_prepare_transmit(task);
959 if (task->tk_status != 0)
960 return;
Trond Myklebuste0ab53d2006-07-27 17:22:50 -0400961 task->tk_action = call_transmit_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 /* Encode here so that rpcsec_gss can use correct sequence number. */
Trond Myklebust940e3312005-11-09 21:45:24 -0500963 if (rpc_task_need_encode(task)) {
Trond Myklebuste0ab53d2006-07-27 17:22:50 -0400964 BUG_ON(task->tk_rqstp->rq_bytes_sent != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 call_encode(task);
Trond Myklebust5e5ce5b2005-10-18 14:20:11 -0700966 /* Did the encode result in an error condition? */
967 if (task->tk_status != 0)
Trond Myklebuste0ab53d2006-07-27 17:22:50 -0400968 return;
Trond Myklebust5e5ce5b2005-10-18 14:20:11 -0700969 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 xprt_transmit(task);
971 if (task->tk_status < 0)
972 return;
Trond Myklebuste0ab53d2006-07-27 17:22:50 -0400973 /*
974 * On success, ensure that we call xprt_end_transmit() before sleeping
975 * in order to allow access to the socket to other RPC requests.
976 */
977 call_transmit_status(task);
978 if (task->tk_msg.rpc_proc->p_decode != NULL)
979 return;
980 task->tk_action = rpc_exit_task;
981 rpc_wake_up_task(task);
982}
983
984/*
985 * 5a. Handle cleanup after a transmission
986 */
987static void
988call_transmit_status(struct rpc_task *task)
989{
990 task->tk_action = call_status;
991 /*
992 * Special case: if we've been waiting on the socket's write_space()
993 * callback, then don't call xprt_end_transmit().
994 */
995 if (task->tk_status == -EAGAIN)
996 return;
997 xprt_end_transmit(task);
Trond Myklebust940e3312005-11-09 21:45:24 -0500998 rpc_task_force_reencode(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999}
1000
1001/*
1002 * 6. Sort out the RPC call status
1003 */
1004static void
1005call_status(struct rpc_task *task)
1006{
1007 struct rpc_clnt *clnt = task->tk_client;
1008 struct rpc_rqst *req = task->tk_rqstp;
1009 int status;
1010
1011 if (req->rq_received > 0 && !req->rq_bytes_sent)
1012 task->tk_status = req->rq_received;
1013
1014 dprintk("RPC: %4d call_status (status %d)\n",
1015 task->tk_pid, task->tk_status);
1016
1017 status = task->tk_status;
1018 if (status >= 0) {
1019 task->tk_action = call_decode;
1020 return;
1021 }
1022
1023 task->tk_status = 0;
1024 switch(status) {
Trond Myklebust76303992006-08-30 14:32:49 -04001025 case -EHOSTDOWN:
1026 case -EHOSTUNREACH:
1027 case -ENETUNREACH:
1028 /*
1029 * Delay any retries for 3 seconds, then handle as if it
1030 * were a timeout.
1031 */
1032 rpc_delay(task, 3*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 case -ETIMEDOUT:
1034 task->tk_action = call_timeout;
1035 break;
1036 case -ECONNREFUSED:
1037 case -ENOTCONN:
Chuck Lever35f5a422006-01-03 09:55:50 +01001038 rpc_force_rebind(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 task->tk_action = call_bind;
1040 break;
1041 case -EAGAIN:
1042 task->tk_action = call_transmit;
1043 break;
1044 case -EIO:
1045 /* shutdown or soft timeout */
1046 rpc_exit(task, status);
1047 break;
1048 default:
Chuck Leverf518e35a2006-01-03 09:55:52 +01001049 printk("%s: RPC call returned error %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 clnt->cl_protname, -status);
1051 rpc_exit(task, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 }
1053}
1054
1055/*
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04001056 * 6a. Handle RPC timeout
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 * We do not release the request slot, so we keep using the
1058 * same XID for all retransmits.
1059 */
1060static void
1061call_timeout(struct rpc_task *task)
1062{
1063 struct rpc_clnt *clnt = task->tk_client;
1064
1065 if (xprt_adjust_timeout(task->tk_rqstp) == 0) {
1066 dprintk("RPC: %4d call_timeout (minor)\n", task->tk_pid);
1067 goto retry;
1068 }
1069
1070 dprintk("RPC: %4d call_timeout (major)\n", task->tk_pid);
Chuck Leveref759a22006-03-20 13:44:17 -05001071 task->tk_timeouts++;
1072
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 if (RPC_IS_SOFT(task)) {
Chuck Leverf518e35a2006-01-03 09:55:52 +01001074 printk(KERN_NOTICE "%s: server %s not responding, timed out\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 clnt->cl_protname, clnt->cl_server);
1076 rpc_exit(task, -EIO);
1077 return;
1078 }
1079
Chuck Leverf518e35a2006-01-03 09:55:52 +01001080 if (!(task->tk_flags & RPC_CALL_MAJORSEEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 task->tk_flags |= RPC_CALL_MAJORSEEN;
1082 printk(KERN_NOTICE "%s: server %s not responding, still trying\n",
1083 clnt->cl_protname, clnt->cl_server);
1084 }
Chuck Lever35f5a422006-01-03 09:55:50 +01001085 rpc_force_rebind(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
1087retry:
1088 clnt->cl_stats->rpcretrans++;
1089 task->tk_action = call_bind;
1090 task->tk_status = 0;
1091}
1092
1093/*
1094 * 7. Decode the RPC reply
1095 */
1096static void
1097call_decode(struct rpc_task *task)
1098{
1099 struct rpc_clnt *clnt = task->tk_client;
1100 struct rpc_rqst *req = task->tk_rqstp;
1101 kxdrproc_t decode = task->tk_msg.rpc_proc->p_decode;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001102 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
1104 dprintk("RPC: %4d call_decode (status %d)\n",
1105 task->tk_pid, task->tk_status);
1106
Chuck Leverf518e35a2006-01-03 09:55:52 +01001107 if (task->tk_flags & RPC_CALL_MAJORSEEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 printk(KERN_NOTICE "%s: server %s OK\n",
1109 clnt->cl_protname, clnt->cl_server);
1110 task->tk_flags &= ~RPC_CALL_MAJORSEEN;
1111 }
1112
1113 if (task->tk_status < 12) {
1114 if (!RPC_IS_SOFT(task)) {
1115 task->tk_action = call_bind;
1116 clnt->cl_stats->rpcretrans++;
1117 goto out_retry;
1118 }
Trond Myklebustda458282006-08-31 15:44:52 -04001119 dprintk("%s: too small RPC reply size (%d bytes)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 clnt->cl_protname, task->tk_status);
Trond Myklebustda458282006-08-31 15:44:52 -04001121 task->tk_action = call_timeout;
1122 goto out_retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 }
1124
Trond Myklebust43ac3f22006-03-20 13:44:51 -05001125 /*
1126 * Ensure that we see all writes made by xprt_complete_rqst()
1127 * before it changed req->rq_received.
1128 */
1129 smp_rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 req->rq_rcv_buf.len = req->rq_private_buf.len;
1131
1132 /* Check that the softirq receive buffer is valid */
1133 WARN_ON(memcmp(&req->rq_rcv_buf, &req->rq_private_buf,
1134 sizeof(req->rq_rcv_buf)) != 0);
1135
1136 /* Verify the RPC header */
Trond Myklebustabbcf282006-01-03 09:55:03 +01001137 p = call_verify(task);
1138 if (IS_ERR(p)) {
1139 if (p == ERR_PTR(-EAGAIN))
1140 goto out_retry;
1141 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 }
1143
Trond Myklebustabbcf282006-01-03 09:55:03 +01001144 task->tk_action = rpc_exit_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
1146 if (decode)
1147 task->tk_status = rpcauth_unwrap_resp(task, decode, req, p,
1148 task->tk_msg.rpc_resp);
1149 dprintk("RPC: %4d call_decode result %d\n", task->tk_pid,
1150 task->tk_status);
1151 return;
1152out_retry:
1153 req->rq_received = req->rq_private_buf.len = 0;
1154 task->tk_status = 0;
1155}
1156
1157/*
1158 * 8. Refresh the credentials if rejected by the server
1159 */
1160static void
1161call_refresh(struct rpc_task *task)
1162{
1163 dprintk("RPC: %4d call_refresh\n", task->tk_pid);
1164
1165 xprt_release(task); /* Must do to obtain new XID */
1166 task->tk_action = call_refreshresult;
1167 task->tk_status = 0;
1168 task->tk_client->cl_stats->rpcauthrefresh++;
1169 rpcauth_refreshcred(task);
1170}
1171
1172/*
1173 * 8a. Process the results of a credential refresh
1174 */
1175static void
1176call_refreshresult(struct rpc_task *task)
1177{
1178 int status = task->tk_status;
1179 dprintk("RPC: %4d call_refreshresult (status %d)\n",
1180 task->tk_pid, task->tk_status);
1181
1182 task->tk_status = 0;
1183 task->tk_action = call_reserve;
1184 if (status >= 0 && rpcauth_uptodatecred(task))
1185 return;
1186 if (status == -EACCES) {
1187 rpc_exit(task, -EACCES);
1188 return;
1189 }
1190 task->tk_action = call_refresh;
1191 if (status != -ETIMEDOUT)
1192 rpc_delay(task, 3*HZ);
1193 return;
1194}
1195
1196/*
1197 * Call header serialization
1198 */
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001199static __be32 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200call_header(struct rpc_task *task)
1201{
1202 struct rpc_clnt *clnt = task->tk_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 struct rpc_rqst *req = task->tk_rqstp;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001204 __be32 *p = req->rq_svec[0].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
1206 /* FIXME: check buffer size? */
Chuck Lever808012f2005-08-25 16:25:49 -07001207
1208 p = xprt_skip_transport_header(task->tk_xprt, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 *p++ = req->rq_xid; /* XID */
1210 *p++ = htonl(RPC_CALL); /* CALL */
1211 *p++ = htonl(RPC_VERSION); /* RPC version */
1212 *p++ = htonl(clnt->cl_prog); /* program number */
1213 *p++ = htonl(clnt->cl_vers); /* program version */
1214 *p++ = htonl(task->tk_msg.rpc_proc->p_proc); /* procedure */
Trond Myklebust334ccfd2005-06-22 17:16:19 +00001215 p = rpcauth_marshcred(task, p);
1216 req->rq_slen = xdr_adjust_iovec(&req->rq_svec[0], p);
1217 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218}
1219
1220/*
1221 * Reply header verification
1222 */
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001223static __be32 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224call_verify(struct rpc_task *task)
1225{
1226 struct kvec *iov = &task->tk_rqstp->rq_rcv_buf.head[0];
1227 int len = task->tk_rqstp->rq_rcv_buf.len >> 2;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001228 __be32 *p = iov->iov_base;
1229 u32 n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 int error = -EACCES;
1231
David Howellse8896492006-08-24 15:44:19 -04001232 if ((task->tk_rqstp->rq_rcv_buf.len & 3) != 0) {
1233 /* RFC-1014 says that the representation of XDR data must be a
1234 * multiple of four bytes
1235 * - if it isn't pointer subtraction in the NFS client may give
1236 * undefined results
1237 */
1238 printk(KERN_WARNING
1239 "call_verify: XDR representation not a multiple of"
1240 " 4 bytes: 0x%x\n", task->tk_rqstp->rq_rcv_buf.len);
1241 goto out_eio;
1242 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 if ((len -= 3) < 0)
1244 goto out_overflow;
1245 p += 1; /* skip XID */
1246
1247 if ((n = ntohl(*p++)) != RPC_REPLY) {
1248 printk(KERN_WARNING "call_verify: not an RPC reply: %x\n", n);
Trond Myklebustabbcf282006-01-03 09:55:03 +01001249 goto out_garbage;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 }
1251 if ((n = ntohl(*p++)) != RPC_MSG_ACCEPTED) {
1252 if (--len < 0)
1253 goto out_overflow;
1254 switch ((n = ntohl(*p++))) {
1255 case RPC_AUTH_ERROR:
1256 break;
1257 case RPC_MISMATCH:
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00001258 dprintk("%s: RPC call version mismatch!\n", __FUNCTION__);
1259 error = -EPROTONOSUPPORT;
1260 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 default:
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00001262 dprintk("%s: RPC call rejected, unknown error: %x\n", __FUNCTION__, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 goto out_eio;
1264 }
1265 if (--len < 0)
1266 goto out_overflow;
1267 switch ((n = ntohl(*p++))) {
1268 case RPC_AUTH_REJECTEDCRED:
1269 case RPC_AUTH_REJECTEDVERF:
1270 case RPCSEC_GSS_CREDPROBLEM:
1271 case RPCSEC_GSS_CTXPROBLEM:
1272 if (!task->tk_cred_retry)
1273 break;
1274 task->tk_cred_retry--;
1275 dprintk("RPC: %4d call_verify: retry stale creds\n",
1276 task->tk_pid);
1277 rpcauth_invalcred(task);
1278 task->tk_action = call_refresh;
Trond Myklebustabbcf282006-01-03 09:55:03 +01001279 goto out_retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 case RPC_AUTH_BADCRED:
1281 case RPC_AUTH_BADVERF:
1282 /* possibly garbled cred/verf? */
1283 if (!task->tk_garb_retry)
1284 break;
1285 task->tk_garb_retry--;
1286 dprintk("RPC: %4d call_verify: retry garbled creds\n",
1287 task->tk_pid);
1288 task->tk_action = call_bind;
Trond Myklebustabbcf282006-01-03 09:55:03 +01001289 goto out_retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 case RPC_AUTH_TOOWEAK:
Levent Serinol1356b8c2006-03-20 13:44:11 -05001291 printk(KERN_NOTICE "call_verify: server %s requires stronger "
1292 "authentication.\n", task->tk_client->cl_server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 break;
1294 default:
1295 printk(KERN_WARNING "call_verify: unknown auth error: %x\n", n);
1296 error = -EIO;
1297 }
1298 dprintk("RPC: %4d call_verify: call rejected %d\n",
1299 task->tk_pid, n);
1300 goto out_err;
1301 }
1302 if (!(p = rpcauth_checkverf(task, p))) {
1303 printk(KERN_WARNING "call_verify: auth check failed\n");
Trond Myklebustabbcf282006-01-03 09:55:03 +01001304 goto out_garbage; /* bad verifier, retry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 }
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001306 len = p - (__be32 *)iov->iov_base - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 if (len < 0)
1308 goto out_overflow;
1309 switch ((n = ntohl(*p++))) {
1310 case RPC_SUCCESS:
1311 return p;
1312 case RPC_PROG_UNAVAIL:
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00001313 dprintk("RPC: call_verify: program %u is unsupported by server %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 (unsigned int)task->tk_client->cl_prog,
1315 task->tk_client->cl_server);
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00001316 error = -EPFNOSUPPORT;
1317 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 case RPC_PROG_MISMATCH:
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00001319 dprintk("RPC: call_verify: program %u, version %u unsupported by server %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 (unsigned int)task->tk_client->cl_prog,
1321 (unsigned int)task->tk_client->cl_vers,
1322 task->tk_client->cl_server);
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00001323 error = -EPROTONOSUPPORT;
1324 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 case RPC_PROC_UNAVAIL:
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00001326 dprintk("RPC: call_verify: proc %p unsupported by program %u, version %u on server %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 task->tk_msg.rpc_proc,
1328 task->tk_client->cl_prog,
1329 task->tk_client->cl_vers,
1330 task->tk_client->cl_server);
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00001331 error = -EOPNOTSUPP;
1332 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 case RPC_GARBAGE_ARGS:
1334 dprintk("RPC: %4d %s: server saw garbage\n", task->tk_pid, __FUNCTION__);
1335 break; /* retry */
1336 default:
1337 printk(KERN_WARNING "call_verify: server accept status: %x\n", n);
1338 /* Also retry */
1339 }
1340
Trond Myklebustabbcf282006-01-03 09:55:03 +01001341out_garbage:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 task->tk_client->cl_stats->rpcgarbage++;
1343 if (task->tk_garb_retry) {
1344 task->tk_garb_retry--;
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00001345 dprintk("RPC %s: retrying %4d\n", __FUNCTION__, task->tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 task->tk_action = call_bind;
Trond Myklebustabbcf282006-01-03 09:55:03 +01001347out_retry:
1348 return ERR_PTR(-EAGAIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 }
1350 printk(KERN_WARNING "RPC %s: retry failed, exit EIO\n", __FUNCTION__);
1351out_eio:
1352 error = -EIO;
1353out_err:
1354 rpc_exit(task, error);
Trond Myklebustabbcf282006-01-03 09:55:03 +01001355 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356out_overflow:
1357 printk(KERN_WARNING "RPC %s: server reply was truncated.\n", __FUNCTION__);
Trond Myklebustabbcf282006-01-03 09:55:03 +01001358 goto out_garbage;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359}
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00001360
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001361static int rpcproc_encode_null(void *rqstp, __be32 *data, void *obj)
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00001362{
1363 return 0;
1364}
1365
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001366static int rpcproc_decode_null(void *rqstp, __be32 *data, void *obj)
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00001367{
1368 return 0;
1369}
1370
1371static struct rpc_procinfo rpcproc_null = {
1372 .p_encode = rpcproc_encode_null,
1373 .p_decode = rpcproc_decode_null,
1374};
1375
1376int rpc_ping(struct rpc_clnt *clnt, int flags)
1377{
1378 struct rpc_message msg = {
1379 .rpc_proc = &rpcproc_null,
1380 };
1381 int err;
1382 msg.rpc_cred = authnull_ops.lookup_cred(NULL, NULL, 0);
1383 err = rpc_call_sync(clnt, &msg, flags);
1384 put_rpccred(msg.rpc_cred);
1385 return err;
1386}