blob: de8bbfcd503043ea0bc64b5cab606597591c73af [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);
Trond Myklebust23bf85b2006-11-21 10:40:23 -0500144 err = -ENOMEM;
145 if (clnt->cl_metrics == NULL)
146 goto out_no_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Chuck Leverec739ef2006-08-22 20:06:15 -0400148 if (!xprt_bound(clnt->cl_xprt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 clnt->cl_autobind = 1;
150
151 clnt->cl_rtt = &clnt->cl_rtt_default;
152 rpc_init_rtt(&clnt->cl_rtt_default, xprt->timeout.to_initval);
153
154 err = rpc_setup_pipedir(clnt, program->pipe_dir_name);
155 if (err < 0)
156 goto out_no_path;
157
J. Bruce Fields6a192752005-06-22 17:16:23 +0000158 auth = rpcauth_create(flavor, clnt);
159 if (IS_ERR(auth)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 printk(KERN_INFO "RPC: Couldn't create auth handle (flavor %u)\n",
161 flavor);
J. Bruce Fields6a192752005-06-22 17:16:23 +0000162 err = PTR_ERR(auth);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 goto out_no_auth;
164 }
165
166 /* save the nodename */
Serge E. Hallyne9ff3992006-10-02 02:18:11 -0700167 clnt->cl_nodelen = strlen(utsname()->nodename);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 if (clnt->cl_nodelen > UNX_MAXNODENAME)
169 clnt->cl_nodelen = UNX_MAXNODENAME;
Serge E. Hallyne9ff3992006-10-02 02:18:11 -0700170 memcpy(clnt->cl_nodename, utsname()->nodename, clnt->cl_nodelen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 return clnt;
172
173out_no_auth:
Trond Myklebust54281542006-03-20 13:44:49 -0500174 if (!IS_ERR(clnt->cl_dentry)) {
Trond Myklebustdff02cc2006-07-31 14:17:18 -0700175 rpc_rmdir(clnt->cl_dentry);
Trond Myklebust54281542006-03-20 13:44:49 -0500176 rpc_put_mount();
177 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178out_no_path:
Trond Myklebust23bf85b2006-11-21 10:40:23 -0500179 rpc_free_iostats(clnt->cl_metrics);
180out_no_stats:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 if (clnt->cl_server != clnt->cl_inline_name)
182 kfree(clnt->cl_server);
183 kfree(clnt);
184out_err:
Trond Myklebust6b6ca862006-09-05 12:55:57 -0400185 xprt_put(xprt);
Adrian Bunk712917d2006-03-13 21:20:47 -0800186out_no_xprt:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 return ERR_PTR(err);
188}
189
Chuck Leverc2866762006-08-22 20:06:20 -0400190/*
191 * rpc_create - create an RPC client and transport with one call
192 * @args: rpc_clnt create argument structure
193 *
194 * Creates and initializes an RPC transport and an RPC client.
195 *
196 * It can ping the server in order to determine if it is up, and to see if
197 * it supports this program and version. RPC_CLNT_CREATE_NOPING disables
198 * this behavior so asynchronous tasks can also use rpc_create.
199 */
200struct rpc_clnt *rpc_create(struct rpc_create_args *args)
201{
202 struct rpc_xprt *xprt;
203 struct rpc_clnt *clnt;
204
205 xprt = xprt_create_transport(args->protocol, args->address,
206 args->addrsize, args->timeout);
207 if (IS_ERR(xprt))
208 return (struct rpc_clnt *)xprt;
209
210 /*
211 * By default, kernel RPC client connects from a reserved port.
212 * CAP_NET_BIND_SERVICE will not be set for unprivileged requesters,
213 * but it is always enabled for rpciod, which handles the connect
214 * operation.
215 */
216 xprt->resvport = 1;
217 if (args->flags & RPC_CLNT_CREATE_NONPRIVPORT)
218 xprt->resvport = 0;
219
220 dprintk("RPC: creating %s client for %s (xprt %p)\n",
221 args->program->name, args->servername, xprt);
222
223 clnt = rpc_new_client(xprt, args->servername, args->program,
224 args->version, args->authflavor);
225 if (IS_ERR(clnt))
226 return clnt;
227
228 if (!(args->flags & RPC_CLNT_CREATE_NOPING)) {
229 int err = rpc_ping(clnt, RPC_TASK_SOFT|RPC_TASK_NOINTR);
230 if (err != 0) {
231 rpc_shutdown_client(clnt);
232 return ERR_PTR(err);
233 }
234 }
235
236 clnt->cl_softrtry = 1;
237 if (args->flags & RPC_CLNT_CREATE_HARDRTRY)
238 clnt->cl_softrtry = 0;
239
240 if (args->flags & RPC_CLNT_CREATE_INTR)
241 clnt->cl_intr = 1;
242 if (args->flags & RPC_CLNT_CREATE_AUTOBIND)
243 clnt->cl_autobind = 1;
244 if (args->flags & RPC_CLNT_CREATE_ONESHOT)
245 clnt->cl_oneshot = 1;
246
247 return clnt;
248}
Chuck Leverb86acd52006-08-22 20:06:22 -0400249EXPORT_SYMBOL_GPL(rpc_create);
Chuck Leverc2866762006-08-22 20:06:20 -0400250
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251/*
252 * This function clones the RPC client structure. It allows us to share the
253 * same transport while varying parameters such as the authentication
254 * flavour.
255 */
256struct rpc_clnt *
257rpc_clone_client(struct rpc_clnt *clnt)
258{
259 struct rpc_clnt *new;
260
Arnaldo Carvalho de Meloe69062b2006-11-21 01:21:34 -0200261 new = kmemdup(clnt, sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 if (!new)
263 goto out_no_clnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 atomic_set(&new->cl_count, 1);
265 atomic_set(&new->cl_users, 0);
Trond Myklebust23bf85b2006-11-21 10:40:23 -0500266 new->cl_metrics = rpc_alloc_iostats(clnt);
267 if (new->cl_metrics == NULL)
268 goto out_no_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 new->cl_parent = clnt;
270 atomic_inc(&clnt->cl_count);
Trond Myklebust6b6ca862006-09-05 12:55:57 -0400271 new->cl_xprt = xprt_get(clnt->cl_xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 /* Turn off autobind on clones */
273 new->cl_autobind = 0;
274 new->cl_oneshot = 0;
275 new->cl_dead = 0;
Trond Myklebust8f8e7a52006-08-14 13:11:15 -0400276 if (!IS_ERR(new->cl_dentry))
Trond Myklebust54281542006-03-20 13:44:49 -0500277 dget(new->cl_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 rpc_init_rtt(&new->cl_rtt_default, clnt->cl_xprt->timeout.to_initval);
279 if (new->cl_auth)
280 atomic_inc(&new->cl_auth->au_count);
281 return new;
Trond Myklebust23bf85b2006-11-21 10:40:23 -0500282out_no_stats:
283 kfree(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284out_no_clnt:
285 printk(KERN_INFO "RPC: out of memory in %s\n", __FUNCTION__);
286 return ERR_PTR(-ENOMEM);
287}
288
289/*
290 * Properly shut down an RPC client, terminating all outstanding
291 * requests. Note that we must be certain that cl_oneshot and
292 * cl_dead are cleared, or else the client would be destroyed
293 * when the last task releases it.
294 */
295int
296rpc_shutdown_client(struct rpc_clnt *clnt)
297{
298 dprintk("RPC: shutting down %s client for %s, tasks=%d\n",
299 clnt->cl_protname, clnt->cl_server,
300 atomic_read(&clnt->cl_users));
301
302 while (atomic_read(&clnt->cl_users) > 0) {
303 /* Don't let rpc_release_client destroy us */
304 clnt->cl_oneshot = 0;
305 clnt->cl_dead = 0;
306 rpc_killall_tasks(clnt);
Ingo Molnar532347e2006-01-09 20:52:53 -0800307 wait_event_timeout(destroy_wait,
Linus Torvalds4f477072006-01-10 08:56:39 -0800308 !atomic_read(&clnt->cl_users), 1*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 }
310
311 if (atomic_read(&clnt->cl_users) < 0) {
312 printk(KERN_ERR "RPC: rpc_shutdown_client clnt %p tasks=%d\n",
313 clnt, atomic_read(&clnt->cl_users));
314#ifdef RPC_DEBUG
315 rpc_show_tasks();
316#endif
317 BUG();
318 }
319
320 return rpc_destroy_client(clnt);
321}
322
323/*
324 * Delete an RPC client
325 */
326int
327rpc_destroy_client(struct rpc_clnt *clnt)
328{
329 if (!atomic_dec_and_test(&clnt->cl_count))
330 return 1;
331 BUG_ON(atomic_read(&clnt->cl_users) != 0);
332
333 dprintk("RPC: destroying %s client for %s\n",
334 clnt->cl_protname, clnt->cl_server);
335 if (clnt->cl_auth) {
336 rpcauth_destroy(clnt->cl_auth);
337 clnt->cl_auth = NULL;
338 }
339 if (clnt->cl_parent != clnt) {
Trond Myklebust8f8e7a52006-08-14 13:11:15 -0400340 if (!IS_ERR(clnt->cl_dentry))
341 dput(clnt->cl_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 rpc_destroy_client(clnt->cl_parent);
343 goto out_free;
344 }
Trond Myklebust8f8e7a52006-08-14 13:11:15 -0400345 if (!IS_ERR(clnt->cl_dentry)) {
Trond Myklebustdff02cc2006-07-31 14:17:18 -0700346 rpc_rmdir(clnt->cl_dentry);
Trond Myklebust8f8e7a52006-08-14 13:11:15 -0400347 rpc_put_mount();
348 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 if (clnt->cl_server != clnt->cl_inline_name)
350 kfree(clnt->cl_server);
351out_free:
Chuck Lever11c556b2006-03-20 13:44:22 -0500352 rpc_free_iostats(clnt->cl_metrics);
353 clnt->cl_metrics = NULL;
Trond Myklebust6b6ca862006-09-05 12:55:57 -0400354 xprt_put(clnt->cl_xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 kfree(clnt);
356 return 0;
357}
358
359/*
360 * Release an RPC client
361 */
362void
363rpc_release_client(struct rpc_clnt *clnt)
364{
365 dprintk("RPC: rpc_release_client(%p, %d)\n",
366 clnt, atomic_read(&clnt->cl_users));
367
368 if (!atomic_dec_and_test(&clnt->cl_users))
369 return;
370 wake_up(&destroy_wait);
371 if (clnt->cl_oneshot || clnt->cl_dead)
372 rpc_destroy_client(clnt);
373}
374
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000375/**
376 * rpc_bind_new_program - bind a new RPC program to an existing client
377 * @old - old rpc_client
378 * @program - rpc program to set
379 * @vers - rpc program version
380 *
381 * Clones the rpc client and sets up a new RPC program. This is mainly
382 * of use for enabling different RPC programs to share the same transport.
383 * The Sun NFSv2/v3 ACL protocol can do this.
384 */
385struct rpc_clnt *rpc_bind_new_program(struct rpc_clnt *old,
386 struct rpc_program *program,
387 int vers)
388{
389 struct rpc_clnt *clnt;
390 struct rpc_version *version;
391 int err;
392
393 BUG_ON(vers >= program->nrvers || !program->version[vers]);
394 version = program->version[vers];
395 clnt = rpc_clone_client(old);
396 if (IS_ERR(clnt))
397 goto out;
398 clnt->cl_procinfo = version->procs;
399 clnt->cl_maxproc = version->nrprocs;
400 clnt->cl_protname = program->name;
401 clnt->cl_prog = program->number;
402 clnt->cl_vers = version->number;
403 clnt->cl_stats = program->stats;
404 err = rpc_ping(clnt, RPC_TASK_SOFT|RPC_TASK_NOINTR);
405 if (err != 0) {
406 rpc_shutdown_client(clnt);
407 clnt = ERR_PTR(err);
408 }
409out:
410 return clnt;
411}
412
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413/*
414 * Default callback for async RPC calls
415 */
416static void
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100417rpc_default_callback(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418{
419}
420
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100421static const struct rpc_call_ops rpc_default_ops = {
422 .rpc_call_done = rpc_default_callback,
423};
424
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425/*
Trond Myklebust14b218a2005-06-22 17:16:28 +0000426 * Export the signal mask handling for synchronous code that
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 * sleeps on RPC calls
428 */
Trond Myklebust2bd61572006-01-03 09:55:19 +0100429#define RPC_INTR_SIGNALS (sigmask(SIGHUP) | sigmask(SIGINT) | sigmask(SIGQUIT) | sigmask(SIGTERM))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Trond Myklebust14b218a2005-06-22 17:16:28 +0000431static void rpc_save_sigmask(sigset_t *oldset, int intr)
432{
Trond Myklebust2bd61572006-01-03 09:55:19 +0100433 unsigned long sigallow = sigmask(SIGKILL);
Trond Myklebust14b218a2005-06-22 17:16:28 +0000434 sigset_t sigmask;
435
436 /* Block all signals except those listed in sigallow */
437 if (intr)
438 sigallow |= RPC_INTR_SIGNALS;
439 siginitsetinv(&sigmask, sigallow);
440 sigprocmask(SIG_BLOCK, &sigmask, oldset);
441}
442
443static inline void rpc_task_sigmask(struct rpc_task *task, sigset_t *oldset)
444{
445 rpc_save_sigmask(oldset, !RPC_TASK_UNINTERRUPTIBLE(task));
446}
447
448static inline void rpc_restore_sigmask(sigset_t *oldset)
449{
450 sigprocmask(SIG_SETMASK, oldset, NULL);
451}
452
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453void rpc_clnt_sigmask(struct rpc_clnt *clnt, sigset_t *oldset)
454{
Trond Myklebust14b218a2005-06-22 17:16:28 +0000455 rpc_save_sigmask(oldset, clnt->cl_intr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456}
457
458void rpc_clnt_sigunmask(struct rpc_clnt *clnt, sigset_t *oldset)
459{
Trond Myklebust14b218a2005-06-22 17:16:28 +0000460 rpc_restore_sigmask(oldset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461}
462
463/*
464 * New rpc_call implementation
465 */
466int rpc_call_sync(struct rpc_clnt *clnt, struct rpc_message *msg, int flags)
467{
468 struct rpc_task *task;
469 sigset_t oldset;
470 int status;
471
472 /* If this client is slain all further I/O fails */
473 if (clnt->cl_dead)
474 return -EIO;
475
476 BUG_ON(flags & RPC_TASK_ASYNC);
477
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100478 task = rpc_new_task(clnt, flags, &rpc_default_ops, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 if (task == NULL)
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -0500480 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
Trond Myklebust14b218a2005-06-22 17:16:28 +0000482 /* Mask signals on RPC calls _and_ GSS_AUTH upcalls */
483 rpc_task_sigmask(task, &oldset);
484
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 rpc_call_setup(task, msg, 0);
486
487 /* Set up the call info struct and execute the task */
Trond Myklebuste60859a2006-01-03 09:55:10 +0100488 status = task->tk_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -0500489 if (status != 0) {
490 rpc_release_task(task);
491 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 }
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -0500493 atomic_inc(&task->tk_count);
494 status = rpc_execute(task);
495 if (status == 0)
496 status = task->tk_status;
497 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -0500499 rpc_restore_sigmask(&oldset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 return status;
501}
502
503/*
504 * New rpc_call implementation
505 */
506int
507rpc_call_async(struct rpc_clnt *clnt, struct rpc_message *msg, int flags,
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100508 const struct rpc_call_ops *tk_ops, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509{
510 struct rpc_task *task;
511 sigset_t oldset;
512 int status;
513
514 /* If this client is slain all further I/O fails */
Trond Myklebust7a1218a2006-03-20 18:11:10 -0500515 status = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 if (clnt->cl_dead)
Trond Myklebust7a1218a2006-03-20 18:11:10 -0500517 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
519 flags |= RPC_TASK_ASYNC;
520
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 /* Create/initialize a new RPC task */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 status = -ENOMEM;
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100523 if (!(task = rpc_new_task(clnt, flags, tk_ops, data)))
Trond Myklebust7a1218a2006-03-20 18:11:10 -0500524 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Trond Myklebust14b218a2005-06-22 17:16:28 +0000526 /* Mask signals on GSS_AUTH upcalls */
527 rpc_task_sigmask(task, &oldset);
528
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 rpc_call_setup(task, msg, 0);
530
531 /* Set up the call info struct and execute the task */
532 status = task->tk_status;
533 if (status == 0)
534 rpc_execute(task);
535 else
536 rpc_release_task(task);
537
Trond Myklebust14b218a2005-06-22 17:16:28 +0000538 rpc_restore_sigmask(&oldset);
Trond Myklebust7a1218a2006-03-20 18:11:10 -0500539 return status;
540out_release:
541 if (tk_ops->rpc_release != NULL)
542 tk_ops->rpc_release(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 return status;
544}
545
546
547void
548rpc_call_setup(struct rpc_task *task, struct rpc_message *msg, int flags)
549{
550 task->tk_msg = *msg;
551 task->tk_flags |= flags;
552 /* Bind the user cred */
553 if (task->tk_msg.rpc_cred != NULL)
554 rpcauth_holdcred(task);
555 else
556 rpcauth_bindcred(task);
557
558 if (task->tk_status == 0)
559 task->tk_action = call_start;
560 else
Trond Myklebustabbcf282006-01-03 09:55:03 +0100561 task->tk_action = rpc_exit_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562}
563
Chuck Levered394402006-08-22 20:06:17 -0400564/**
565 * rpc_peeraddr - extract remote peer address from clnt's xprt
566 * @clnt: RPC client structure
567 * @buf: target buffer
568 * @size: length of target buffer
569 *
570 * Returns the number of bytes that are actually in the stored address.
571 */
572size_t rpc_peeraddr(struct rpc_clnt *clnt, struct sockaddr *buf, size_t bufsize)
573{
574 size_t bytes;
575 struct rpc_xprt *xprt = clnt->cl_xprt;
576
577 bytes = sizeof(xprt->addr);
578 if (bytes > bufsize)
579 bytes = bufsize;
580 memcpy(buf, &clnt->cl_xprt->addr, bytes);
Chuck Leverc4efcb12006-08-22 20:06:19 -0400581 return xprt->addrlen;
Chuck Levered394402006-08-22 20:06:17 -0400582}
Chuck Leverb86acd52006-08-22 20:06:22 -0400583EXPORT_SYMBOL_GPL(rpc_peeraddr);
Chuck Levered394402006-08-22 20:06:17 -0400584
Chuck Leverf425eba2006-08-22 20:06:18 -0400585/**
586 * rpc_peeraddr2str - return remote peer address in printable format
587 * @clnt: RPC client structure
588 * @format: address format
589 *
590 */
591char *rpc_peeraddr2str(struct rpc_clnt *clnt, enum rpc_display_format_t format)
592{
593 struct rpc_xprt *xprt = clnt->cl_xprt;
594 return xprt->ops->print_addr(xprt, format);
595}
Chuck Leverb86acd52006-08-22 20:06:22 -0400596EXPORT_SYMBOL_GPL(rpc_peeraddr2str);
Chuck Leverf425eba2006-08-22 20:06:18 -0400597
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598void
599rpc_setbufsize(struct rpc_clnt *clnt, unsigned int sndsize, unsigned int rcvsize)
600{
601 struct rpc_xprt *xprt = clnt->cl_xprt;
Chuck Lever470056c2005-08-25 16:25:56 -0700602 if (xprt->ops->set_buffer_size)
603 xprt->ops->set_buffer_size(xprt, sndsize, rcvsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604}
605
606/*
607 * Return size of largest payload RPC client can support, in bytes
608 *
609 * For stream transports, this is one RPC record fragment (see RFC
610 * 1831), as we don't support multi-record requests yet. For datagram
611 * transports, this is the size of an IP packet minus the IP, UDP, and
612 * RPC header sizes.
613 */
614size_t rpc_max_payload(struct rpc_clnt *clnt)
615{
616 return clnt->cl_xprt->max_payload;
617}
Chuck Leverb86acd52006-08-22 20:06:22 -0400618EXPORT_SYMBOL_GPL(rpc_max_payload);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
Chuck Lever35f5a422006-01-03 09:55:50 +0100620/**
621 * rpc_force_rebind - force transport to check that remote port is unchanged
622 * @clnt: client to rebind
623 *
624 */
625void rpc_force_rebind(struct rpc_clnt *clnt)
626{
627 if (clnt->cl_autobind)
Chuck Leverec739ef2006-08-22 20:06:15 -0400628 xprt_clear_bound(clnt->cl_xprt);
Chuck Lever35f5a422006-01-03 09:55:50 +0100629}
Chuck Leverb86acd52006-08-22 20:06:22 -0400630EXPORT_SYMBOL_GPL(rpc_force_rebind);
Chuck Lever35f5a422006-01-03 09:55:50 +0100631
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632/*
633 * Restart an (async) RPC call. Usually called from within the
634 * exit handler.
635 */
636void
637rpc_restart_call(struct rpc_task *task)
638{
639 if (RPC_ASSASSINATED(task))
640 return;
641
642 task->tk_action = call_start;
643}
644
645/*
646 * 0. Initial state
647 *
648 * Other FSM states can be visited zero or more times, but
649 * this state is visited exactly once for each RPC.
650 */
651static void
652call_start(struct rpc_task *task)
653{
654 struct rpc_clnt *clnt = task->tk_client;
655
656 dprintk("RPC: %4d call_start %s%d proc %d (%s)\n", task->tk_pid,
657 clnt->cl_protname, clnt->cl_vers, task->tk_msg.rpc_proc->p_proc,
658 (RPC_IS_ASYNC(task) ? "async" : "sync"));
659
660 /* Increment call count */
661 task->tk_msg.rpc_proc->p_count++;
662 clnt->cl_stats->rpccnt++;
663 task->tk_action = call_reserve;
664}
665
666/*
667 * 1. Reserve an RPC call slot
668 */
669static void
670call_reserve(struct rpc_task *task)
671{
672 dprintk("RPC: %4d call_reserve\n", task->tk_pid);
673
674 if (!rpcauth_uptodatecred(task)) {
675 task->tk_action = call_refresh;
676 return;
677 }
678
679 task->tk_status = 0;
680 task->tk_action = call_reserveresult;
681 xprt_reserve(task);
682}
683
684/*
685 * 1b. Grok the result of xprt_reserve()
686 */
687static void
688call_reserveresult(struct rpc_task *task)
689{
690 int status = task->tk_status;
691
692 dprintk("RPC: %4d call_reserveresult (status %d)\n",
693 task->tk_pid, task->tk_status);
694
695 /*
696 * After a call to xprt_reserve(), we must have either
697 * a request slot or else an error status.
698 */
699 task->tk_status = 0;
700 if (status >= 0) {
701 if (task->tk_rqstp) {
702 task->tk_action = call_allocate;
703 return;
704 }
705
706 printk(KERN_ERR "%s: status=%d, but no request slot, exiting\n",
707 __FUNCTION__, status);
708 rpc_exit(task, -EIO);
709 return;
710 }
711
712 /*
713 * Even though there was an error, we may have acquired
714 * a request slot somehow. Make sure not to leak it.
715 */
716 if (task->tk_rqstp) {
717 printk(KERN_ERR "%s: status=%d, request allocated anyway\n",
718 __FUNCTION__, status);
719 xprt_release(task);
720 }
721
722 switch (status) {
723 case -EAGAIN: /* woken up; retry */
724 task->tk_action = call_reserve;
725 return;
726 case -EIO: /* probably a shutdown */
727 break;
728 default:
729 printk(KERN_ERR "%s: unrecognized error %d, exiting\n",
730 __FUNCTION__, status);
731 break;
732 }
733 rpc_exit(task, status);
734}
735
736/*
737 * 2. Allocate the buffer. For details, see sched.c:rpc_malloc.
Chuck Lever02107142006-01-03 09:55:49 +0100738 * (Note: buffer memory is freed in xprt_release).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 */
740static void
741call_allocate(struct rpc_task *task)
742{
Chuck Lever02107142006-01-03 09:55:49 +0100743 struct rpc_rqst *req = task->tk_rqstp;
744 struct rpc_xprt *xprt = task->tk_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 unsigned int bufsiz;
746
747 dprintk("RPC: %4d call_allocate (status %d)\n",
748 task->tk_pid, task->tk_status);
749 task->tk_action = call_bind;
Chuck Lever02107142006-01-03 09:55:49 +0100750 if (req->rq_buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 return;
752
753 /* FIXME: compute buffer requirements more exactly using
754 * auth->au_wslack */
755 bufsiz = task->tk_msg.rpc_proc->p_bufsiz + RPC_SLACK_SPACE;
756
Chuck Lever02107142006-01-03 09:55:49 +0100757 if (xprt->ops->buf_alloc(task, bufsiz << 1) != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 return;
759 printk(KERN_INFO "RPC: buffer allocation failed for task %p\n", task);
760
Trond Myklebust14b218a2005-06-22 17:16:28 +0000761 if (RPC_IS_ASYNC(task) || !signalled()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 xprt_release(task);
763 task->tk_action = call_reserve;
764 rpc_delay(task, HZ>>4);
765 return;
766 }
767
768 rpc_exit(task, -ERESTARTSYS);
769}
770
Trond Myklebust940e3312005-11-09 21:45:24 -0500771static inline int
772rpc_task_need_encode(struct rpc_task *task)
773{
774 return task->tk_rqstp->rq_snd_buf.len == 0;
775}
776
777static inline void
778rpc_task_force_reencode(struct rpc_task *task)
779{
780 task->tk_rqstp->rq_snd_buf.len = 0;
781}
782
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783/*
784 * 3. Encode arguments of an RPC call
785 */
786static void
787call_encode(struct rpc_task *task)
788{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 struct rpc_rqst *req = task->tk_rqstp;
790 struct xdr_buf *sndbuf = &req->rq_snd_buf;
791 struct xdr_buf *rcvbuf = &req->rq_rcv_buf;
792 unsigned int bufsiz;
793 kxdrproc_t encode;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700794 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
796 dprintk("RPC: %4d call_encode (status %d)\n",
797 task->tk_pid, task->tk_status);
798
799 /* Default buffer setup */
Chuck Lever02107142006-01-03 09:55:49 +0100800 bufsiz = req->rq_bufsize >> 1;
801 sndbuf->head[0].iov_base = (void *)req->rq_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 sndbuf->head[0].iov_len = bufsiz;
803 sndbuf->tail[0].iov_len = 0;
804 sndbuf->page_len = 0;
805 sndbuf->len = 0;
806 sndbuf->buflen = bufsiz;
Chuck Lever02107142006-01-03 09:55:49 +0100807 rcvbuf->head[0].iov_base = (void *)((char *)req->rq_buffer + bufsiz);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 rcvbuf->head[0].iov_len = bufsiz;
809 rcvbuf->tail[0].iov_len = 0;
810 rcvbuf->page_len = 0;
811 rcvbuf->len = 0;
812 rcvbuf->buflen = bufsiz;
813
814 /* Encode header and provided arguments */
815 encode = task->tk_msg.rpc_proc->p_encode;
816 if (!(p = call_header(task))) {
817 printk(KERN_INFO "RPC: call_header failed, exit EIO\n");
818 rpc_exit(task, -EIO);
819 return;
820 }
J. Bruce Fieldsf3680312005-10-13 16:54:48 -0400821 if (encode == NULL)
822 return;
823
824 task->tk_status = rpcauth_wrap_req(task, encode, req, p,
825 task->tk_msg.rpc_argp);
826 if (task->tk_status == -ENOMEM) {
827 /* XXX: Is this sane? */
828 rpc_delay(task, 3*HZ);
829 task->tk_status = -EAGAIN;
830 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831}
832
833/*
834 * 4. Get the server port number if not yet set
835 */
836static void
837call_bind(struct rpc_task *task)
838{
Chuck Leverec739ef2006-08-22 20:06:15 -0400839 struct rpc_xprt *xprt = task->tk_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
Chuck Leverda351872005-08-11 16:25:11 -0400841 dprintk("RPC: %4d call_bind (status %d)\n",
842 task->tk_pid, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Chuck Leverda351872005-08-11 16:25:11 -0400844 task->tk_action = call_connect;
Chuck Leverec739ef2006-08-22 20:06:15 -0400845 if (!xprt_bound(xprt)) {
Chuck Leverda351872005-08-11 16:25:11 -0400846 task->tk_action = call_bind_status;
Chuck Leverec739ef2006-08-22 20:06:15 -0400847 task->tk_timeout = xprt->bind_timeout;
Chuck Leverbbf7c1d2006-08-22 20:06:16 -0400848 xprt->ops->rpcbind(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 }
850}
851
852/*
Chuck Leverda351872005-08-11 16:25:11 -0400853 * 4a. Sort out bind result
854 */
855static void
856call_bind_status(struct rpc_task *task)
857{
858 int status = -EACCES;
859
860 if (task->tk_status >= 0) {
861 dprintk("RPC: %4d call_bind_status (status %d)\n",
862 task->tk_pid, task->tk_status);
863 task->tk_status = 0;
864 task->tk_action = call_connect;
865 return;
866 }
867
868 switch (task->tk_status) {
869 case -EACCES:
870 dprintk("RPC: %4d remote rpcbind: RPC program/version unavailable\n",
871 task->tk_pid);
Chuck Leverea635a52005-10-06 23:12:58 -0400872 rpc_delay(task, 3*HZ);
Trond Myklebustda458282006-08-31 15:44:52 -0400873 goto retry_timeout;
Chuck Leverda351872005-08-11 16:25:11 -0400874 case -ETIMEDOUT:
875 dprintk("RPC: %4d rpcbind request timed out\n",
876 task->tk_pid);
Trond Myklebustda458282006-08-31 15:44:52 -0400877 goto retry_timeout;
Chuck Leverda351872005-08-11 16:25:11 -0400878 case -EPFNOSUPPORT:
879 dprintk("RPC: %4d remote rpcbind service unavailable\n",
880 task->tk_pid);
881 break;
882 case -EPROTONOSUPPORT:
883 dprintk("RPC: %4d remote rpcbind version 2 unavailable\n",
884 task->tk_pid);
885 break;
886 default:
887 dprintk("RPC: %4d unrecognized rpcbind error (%d)\n",
888 task->tk_pid, -task->tk_status);
889 status = -EIO;
Chuck Leverda351872005-08-11 16:25:11 -0400890 }
891
892 rpc_exit(task, status);
893 return;
894
Trond Myklebustda458282006-08-31 15:44:52 -0400895retry_timeout:
896 task->tk_action = call_timeout;
Chuck Leverda351872005-08-11 16:25:11 -0400897}
898
899/*
900 * 4b. Connect to the RPC server
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 */
902static void
903call_connect(struct rpc_task *task)
904{
Chuck Leverda351872005-08-11 16:25:11 -0400905 struct rpc_xprt *xprt = task->tk_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
Chuck Leverda351872005-08-11 16:25:11 -0400907 dprintk("RPC: %4d call_connect xprt %p %s connected\n",
908 task->tk_pid, xprt,
909 (xprt_connected(xprt) ? "is" : "is not"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
Chuck Leverda351872005-08-11 16:25:11 -0400911 task->tk_action = call_transmit;
912 if (!xprt_connected(xprt)) {
913 task->tk_action = call_connect_status;
914 if (task->tk_status < 0)
915 return;
916 xprt_connect(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918}
919
920/*
Chuck Leverda351872005-08-11 16:25:11 -0400921 * 4c. Sort out connect result
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 */
923static void
924call_connect_status(struct rpc_task *task)
925{
926 struct rpc_clnt *clnt = task->tk_client;
927 int status = task->tk_status;
928
Chuck Leverda351872005-08-11 16:25:11 -0400929 dprintk("RPC: %5u call_connect_status (status %d)\n",
930 task->tk_pid, task->tk_status);
931
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 task->tk_status = 0;
933 if (status >= 0) {
934 clnt->cl_stats->netreconn++;
935 task->tk_action = call_transmit;
936 return;
937 }
938
Chuck Leverda351872005-08-11 16:25:11 -0400939 /* Something failed: remote service port may have changed */
Chuck Lever35f5a422006-01-03 09:55:50 +0100940 rpc_force_rebind(clnt);
Chuck Leverda351872005-08-11 16:25:11 -0400941
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 switch (status) {
943 case -ENOTCONN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 case -EAGAIN:
Chuck Leverda351872005-08-11 16:25:11 -0400945 task->tk_action = call_bind;
Trond Myklebustda458282006-08-31 15:44:52 -0400946 if (!RPC_IS_SOFT(task))
947 return;
948 /* if soft mounted, test if we've timed out */
949 case -ETIMEDOUT:
950 task->tk_action = call_timeout;
951 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 }
Trond Myklebustda458282006-08-31 15:44:52 -0400953 rpc_exit(task, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954}
955
956/*
957 * 5. Transmit the RPC request, and wait for reply
958 */
959static void
960call_transmit(struct rpc_task *task)
961{
962 dprintk("RPC: %4d call_transmit (status %d)\n",
963 task->tk_pid, task->tk_status);
964
965 task->tk_action = call_status;
966 if (task->tk_status < 0)
967 return;
968 task->tk_status = xprt_prepare_transmit(task);
969 if (task->tk_status != 0)
970 return;
Trond Myklebuste0ab53d2006-07-27 17:22:50 -0400971 task->tk_action = call_transmit_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 /* Encode here so that rpcsec_gss can use correct sequence number. */
Trond Myklebust940e3312005-11-09 21:45:24 -0500973 if (rpc_task_need_encode(task)) {
Trond Myklebuste0ab53d2006-07-27 17:22:50 -0400974 BUG_ON(task->tk_rqstp->rq_bytes_sent != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 call_encode(task);
Trond Myklebust5e5ce5b2005-10-18 14:20:11 -0700976 /* Did the encode result in an error condition? */
977 if (task->tk_status != 0)
Trond Myklebuste0ab53d2006-07-27 17:22:50 -0400978 return;
Trond Myklebust5e5ce5b2005-10-18 14:20:11 -0700979 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 xprt_transmit(task);
981 if (task->tk_status < 0)
982 return;
Trond Myklebuste0ab53d2006-07-27 17:22:50 -0400983 /*
984 * On success, ensure that we call xprt_end_transmit() before sleeping
985 * in order to allow access to the socket to other RPC requests.
986 */
987 call_transmit_status(task);
988 if (task->tk_msg.rpc_proc->p_decode != NULL)
989 return;
990 task->tk_action = rpc_exit_task;
991 rpc_wake_up_task(task);
992}
993
994/*
995 * 5a. Handle cleanup after a transmission
996 */
997static void
998call_transmit_status(struct rpc_task *task)
999{
1000 task->tk_action = call_status;
1001 /*
1002 * Special case: if we've been waiting on the socket's write_space()
1003 * callback, then don't call xprt_end_transmit().
1004 */
1005 if (task->tk_status == -EAGAIN)
1006 return;
1007 xprt_end_transmit(task);
Trond Myklebust940e3312005-11-09 21:45:24 -05001008 rpc_task_force_reencode(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009}
1010
1011/*
1012 * 6. Sort out the RPC call status
1013 */
1014static void
1015call_status(struct rpc_task *task)
1016{
1017 struct rpc_clnt *clnt = task->tk_client;
1018 struct rpc_rqst *req = task->tk_rqstp;
1019 int status;
1020
1021 if (req->rq_received > 0 && !req->rq_bytes_sent)
1022 task->tk_status = req->rq_received;
1023
1024 dprintk("RPC: %4d call_status (status %d)\n",
1025 task->tk_pid, task->tk_status);
1026
1027 status = task->tk_status;
1028 if (status >= 0) {
1029 task->tk_action = call_decode;
1030 return;
1031 }
1032
1033 task->tk_status = 0;
1034 switch(status) {
Trond Myklebust76303992006-08-30 14:32:49 -04001035 case -EHOSTDOWN:
1036 case -EHOSTUNREACH:
1037 case -ENETUNREACH:
1038 /*
1039 * Delay any retries for 3 seconds, then handle as if it
1040 * were a timeout.
1041 */
1042 rpc_delay(task, 3*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 case -ETIMEDOUT:
1044 task->tk_action = call_timeout;
1045 break;
1046 case -ECONNREFUSED:
1047 case -ENOTCONN:
Chuck Lever35f5a422006-01-03 09:55:50 +01001048 rpc_force_rebind(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 task->tk_action = call_bind;
1050 break;
1051 case -EAGAIN:
1052 task->tk_action = call_transmit;
1053 break;
1054 case -EIO:
1055 /* shutdown or soft timeout */
1056 rpc_exit(task, status);
1057 break;
1058 default:
Chuck Leverf518e35a2006-01-03 09:55:52 +01001059 printk("%s: RPC call returned error %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 clnt->cl_protname, -status);
1061 rpc_exit(task, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 }
1063}
1064
1065/*
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04001066 * 6a. Handle RPC timeout
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 * We do not release the request slot, so we keep using the
1068 * same XID for all retransmits.
1069 */
1070static void
1071call_timeout(struct rpc_task *task)
1072{
1073 struct rpc_clnt *clnt = task->tk_client;
1074
1075 if (xprt_adjust_timeout(task->tk_rqstp) == 0) {
1076 dprintk("RPC: %4d call_timeout (minor)\n", task->tk_pid);
1077 goto retry;
1078 }
1079
1080 dprintk("RPC: %4d call_timeout (major)\n", task->tk_pid);
Chuck Leveref759a22006-03-20 13:44:17 -05001081 task->tk_timeouts++;
1082
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 if (RPC_IS_SOFT(task)) {
Chuck Leverf518e35a2006-01-03 09:55:52 +01001084 printk(KERN_NOTICE "%s: server %s not responding, timed out\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 clnt->cl_protname, clnt->cl_server);
1086 rpc_exit(task, -EIO);
1087 return;
1088 }
1089
Chuck Leverf518e35a2006-01-03 09:55:52 +01001090 if (!(task->tk_flags & RPC_CALL_MAJORSEEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 task->tk_flags |= RPC_CALL_MAJORSEEN;
1092 printk(KERN_NOTICE "%s: server %s not responding, still trying\n",
1093 clnt->cl_protname, clnt->cl_server);
1094 }
Chuck Lever35f5a422006-01-03 09:55:50 +01001095 rpc_force_rebind(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
1097retry:
1098 clnt->cl_stats->rpcretrans++;
1099 task->tk_action = call_bind;
1100 task->tk_status = 0;
1101}
1102
1103/*
1104 * 7. Decode the RPC reply
1105 */
1106static void
1107call_decode(struct rpc_task *task)
1108{
1109 struct rpc_clnt *clnt = task->tk_client;
1110 struct rpc_rqst *req = task->tk_rqstp;
1111 kxdrproc_t decode = task->tk_msg.rpc_proc->p_decode;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001112 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
1114 dprintk("RPC: %4d call_decode (status %d)\n",
1115 task->tk_pid, task->tk_status);
1116
Chuck Leverf518e35a2006-01-03 09:55:52 +01001117 if (task->tk_flags & RPC_CALL_MAJORSEEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 printk(KERN_NOTICE "%s: server %s OK\n",
1119 clnt->cl_protname, clnt->cl_server);
1120 task->tk_flags &= ~RPC_CALL_MAJORSEEN;
1121 }
1122
1123 if (task->tk_status < 12) {
1124 if (!RPC_IS_SOFT(task)) {
1125 task->tk_action = call_bind;
1126 clnt->cl_stats->rpcretrans++;
1127 goto out_retry;
1128 }
Trond Myklebustda458282006-08-31 15:44:52 -04001129 dprintk("%s: too small RPC reply size (%d bytes)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 clnt->cl_protname, task->tk_status);
Trond Myklebustda458282006-08-31 15:44:52 -04001131 task->tk_action = call_timeout;
1132 goto out_retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 }
1134
Trond Myklebust43ac3f22006-03-20 13:44:51 -05001135 /*
1136 * Ensure that we see all writes made by xprt_complete_rqst()
1137 * before it changed req->rq_received.
1138 */
1139 smp_rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 req->rq_rcv_buf.len = req->rq_private_buf.len;
1141
1142 /* Check that the softirq receive buffer is valid */
1143 WARN_ON(memcmp(&req->rq_rcv_buf, &req->rq_private_buf,
1144 sizeof(req->rq_rcv_buf)) != 0);
1145
1146 /* Verify the RPC header */
Trond Myklebustabbcf282006-01-03 09:55:03 +01001147 p = call_verify(task);
1148 if (IS_ERR(p)) {
1149 if (p == ERR_PTR(-EAGAIN))
1150 goto out_retry;
1151 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 }
1153
Trond Myklebustabbcf282006-01-03 09:55:03 +01001154 task->tk_action = rpc_exit_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
1156 if (decode)
1157 task->tk_status = rpcauth_unwrap_resp(task, decode, req, p,
1158 task->tk_msg.rpc_resp);
1159 dprintk("RPC: %4d call_decode result %d\n", task->tk_pid,
1160 task->tk_status);
1161 return;
1162out_retry:
1163 req->rq_received = req->rq_private_buf.len = 0;
1164 task->tk_status = 0;
1165}
1166
1167/*
1168 * 8. Refresh the credentials if rejected by the server
1169 */
1170static void
1171call_refresh(struct rpc_task *task)
1172{
1173 dprintk("RPC: %4d call_refresh\n", task->tk_pid);
1174
1175 xprt_release(task); /* Must do to obtain new XID */
1176 task->tk_action = call_refreshresult;
1177 task->tk_status = 0;
1178 task->tk_client->cl_stats->rpcauthrefresh++;
1179 rpcauth_refreshcred(task);
1180}
1181
1182/*
1183 * 8a. Process the results of a credential refresh
1184 */
1185static void
1186call_refreshresult(struct rpc_task *task)
1187{
1188 int status = task->tk_status;
1189 dprintk("RPC: %4d call_refreshresult (status %d)\n",
1190 task->tk_pid, task->tk_status);
1191
1192 task->tk_status = 0;
1193 task->tk_action = call_reserve;
1194 if (status >= 0 && rpcauth_uptodatecred(task))
1195 return;
1196 if (status == -EACCES) {
1197 rpc_exit(task, -EACCES);
1198 return;
1199 }
1200 task->tk_action = call_refresh;
1201 if (status != -ETIMEDOUT)
1202 rpc_delay(task, 3*HZ);
1203 return;
1204}
1205
1206/*
1207 * Call header serialization
1208 */
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001209static __be32 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210call_header(struct rpc_task *task)
1211{
1212 struct rpc_clnt *clnt = task->tk_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 struct rpc_rqst *req = task->tk_rqstp;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001214 __be32 *p = req->rq_svec[0].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
1216 /* FIXME: check buffer size? */
Chuck Lever808012f2005-08-25 16:25:49 -07001217
1218 p = xprt_skip_transport_header(task->tk_xprt, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 *p++ = req->rq_xid; /* XID */
1220 *p++ = htonl(RPC_CALL); /* CALL */
1221 *p++ = htonl(RPC_VERSION); /* RPC version */
1222 *p++ = htonl(clnt->cl_prog); /* program number */
1223 *p++ = htonl(clnt->cl_vers); /* program version */
1224 *p++ = htonl(task->tk_msg.rpc_proc->p_proc); /* procedure */
Trond Myklebust334ccfd2005-06-22 17:16:19 +00001225 p = rpcauth_marshcred(task, p);
1226 req->rq_slen = xdr_adjust_iovec(&req->rq_svec[0], p);
1227 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228}
1229
1230/*
1231 * Reply header verification
1232 */
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001233static __be32 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234call_verify(struct rpc_task *task)
1235{
1236 struct kvec *iov = &task->tk_rqstp->rq_rcv_buf.head[0];
1237 int len = task->tk_rqstp->rq_rcv_buf.len >> 2;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001238 __be32 *p = iov->iov_base;
1239 u32 n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 int error = -EACCES;
1241
David Howellse8896492006-08-24 15:44:19 -04001242 if ((task->tk_rqstp->rq_rcv_buf.len & 3) != 0) {
1243 /* RFC-1014 says that the representation of XDR data must be a
1244 * multiple of four bytes
1245 * - if it isn't pointer subtraction in the NFS client may give
1246 * undefined results
1247 */
1248 printk(KERN_WARNING
1249 "call_verify: XDR representation not a multiple of"
1250 " 4 bytes: 0x%x\n", task->tk_rqstp->rq_rcv_buf.len);
1251 goto out_eio;
1252 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 if ((len -= 3) < 0)
1254 goto out_overflow;
1255 p += 1; /* skip XID */
1256
1257 if ((n = ntohl(*p++)) != RPC_REPLY) {
1258 printk(KERN_WARNING "call_verify: not an RPC reply: %x\n", n);
Trond Myklebustabbcf282006-01-03 09:55:03 +01001259 goto out_garbage;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 }
1261 if ((n = ntohl(*p++)) != RPC_MSG_ACCEPTED) {
1262 if (--len < 0)
1263 goto out_overflow;
1264 switch ((n = ntohl(*p++))) {
1265 case RPC_AUTH_ERROR:
1266 break;
1267 case RPC_MISMATCH:
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00001268 dprintk("%s: RPC call version mismatch!\n", __FUNCTION__);
1269 error = -EPROTONOSUPPORT;
1270 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 default:
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00001272 dprintk("%s: RPC call rejected, unknown error: %x\n", __FUNCTION__, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 goto out_eio;
1274 }
1275 if (--len < 0)
1276 goto out_overflow;
1277 switch ((n = ntohl(*p++))) {
1278 case RPC_AUTH_REJECTEDCRED:
1279 case RPC_AUTH_REJECTEDVERF:
1280 case RPCSEC_GSS_CREDPROBLEM:
1281 case RPCSEC_GSS_CTXPROBLEM:
1282 if (!task->tk_cred_retry)
1283 break;
1284 task->tk_cred_retry--;
1285 dprintk("RPC: %4d call_verify: retry stale creds\n",
1286 task->tk_pid);
1287 rpcauth_invalcred(task);
1288 task->tk_action = call_refresh;
Trond Myklebustabbcf282006-01-03 09:55:03 +01001289 goto out_retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 case RPC_AUTH_BADCRED:
1291 case RPC_AUTH_BADVERF:
1292 /* possibly garbled cred/verf? */
1293 if (!task->tk_garb_retry)
1294 break;
1295 task->tk_garb_retry--;
1296 dprintk("RPC: %4d call_verify: retry garbled creds\n",
1297 task->tk_pid);
1298 task->tk_action = call_bind;
Trond Myklebustabbcf282006-01-03 09:55:03 +01001299 goto out_retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 case RPC_AUTH_TOOWEAK:
Levent Serinol1356b8c2006-03-20 13:44:11 -05001301 printk(KERN_NOTICE "call_verify: server %s requires stronger "
1302 "authentication.\n", task->tk_client->cl_server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 break;
1304 default:
1305 printk(KERN_WARNING "call_verify: unknown auth error: %x\n", n);
1306 error = -EIO;
1307 }
1308 dprintk("RPC: %4d call_verify: call rejected %d\n",
1309 task->tk_pid, n);
1310 goto out_err;
1311 }
1312 if (!(p = rpcauth_checkverf(task, p))) {
1313 printk(KERN_WARNING "call_verify: auth check failed\n");
Trond Myklebustabbcf282006-01-03 09:55:03 +01001314 goto out_garbage; /* bad verifier, retry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 }
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001316 len = p - (__be32 *)iov->iov_base - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 if (len < 0)
1318 goto out_overflow;
1319 switch ((n = ntohl(*p++))) {
1320 case RPC_SUCCESS:
1321 return p;
1322 case RPC_PROG_UNAVAIL:
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00001323 dprintk("RPC: call_verify: program %u is unsupported by server %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 (unsigned int)task->tk_client->cl_prog,
1325 task->tk_client->cl_server);
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00001326 error = -EPFNOSUPPORT;
1327 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 case RPC_PROG_MISMATCH:
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00001329 dprintk("RPC: call_verify: program %u, version %u unsupported by server %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 (unsigned int)task->tk_client->cl_prog,
1331 (unsigned int)task->tk_client->cl_vers,
1332 task->tk_client->cl_server);
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00001333 error = -EPROTONOSUPPORT;
1334 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 case RPC_PROC_UNAVAIL:
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00001336 dprintk("RPC: call_verify: proc %p unsupported by program %u, version %u on server %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 task->tk_msg.rpc_proc,
1338 task->tk_client->cl_prog,
1339 task->tk_client->cl_vers,
1340 task->tk_client->cl_server);
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00001341 error = -EOPNOTSUPP;
1342 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 case RPC_GARBAGE_ARGS:
1344 dprintk("RPC: %4d %s: server saw garbage\n", task->tk_pid, __FUNCTION__);
1345 break; /* retry */
1346 default:
1347 printk(KERN_WARNING "call_verify: server accept status: %x\n", n);
1348 /* Also retry */
1349 }
1350
Trond Myklebustabbcf282006-01-03 09:55:03 +01001351out_garbage:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 task->tk_client->cl_stats->rpcgarbage++;
1353 if (task->tk_garb_retry) {
1354 task->tk_garb_retry--;
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00001355 dprintk("RPC %s: retrying %4d\n", __FUNCTION__, task->tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 task->tk_action = call_bind;
Trond Myklebustabbcf282006-01-03 09:55:03 +01001357out_retry:
1358 return ERR_PTR(-EAGAIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 }
1360 printk(KERN_WARNING "RPC %s: retry failed, exit EIO\n", __FUNCTION__);
1361out_eio:
1362 error = -EIO;
1363out_err:
1364 rpc_exit(task, error);
Trond Myklebustabbcf282006-01-03 09:55:03 +01001365 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366out_overflow:
1367 printk(KERN_WARNING "RPC %s: server reply was truncated.\n", __FUNCTION__);
Trond Myklebustabbcf282006-01-03 09:55:03 +01001368 goto out_garbage;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369}
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00001370
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001371static int rpcproc_encode_null(void *rqstp, __be32 *data, void *obj)
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00001372{
1373 return 0;
1374}
1375
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001376static int rpcproc_decode_null(void *rqstp, __be32 *data, void *obj)
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00001377{
1378 return 0;
1379}
1380
1381static struct rpc_procinfo rpcproc_null = {
1382 .p_encode = rpcproc_encode_null,
1383 .p_decode = rpcproc_decode_null,
1384};
1385
1386int rpc_ping(struct rpc_clnt *clnt, int flags)
1387{
1388 struct rpc_message msg = {
1389 .rpc_proc = &rpcproc_null,
1390 };
1391 int err;
1392 msg.rpc_cred = authnull_ops.lookup_cred(NULL, NULL, 0);
1393 err = rpc_call_sync(clnt, &msg, flags);
1394 put_rpccred(msg.rpc_cred);
1395 return err;
1396}