blob: 25bbf2d7f603c6c5c3c4a89ccf1936de49c305e2 [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>
Trond Myklebust6d5fcb52006-10-18 16:01:06 -040030#include <linux/smp_lock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/utsname.h>
Chuck Lever11c556b2006-03-20 13:44:22 -050032#include <linux/workqueue.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34#include <linux/sunrpc/clnt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/sunrpc/rpc_pipe_fs.h>
Chuck Lever11c556b2006-03-20 13:44:22 -050036#include <linux/sunrpc/metrics.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#ifdef RPC_DEBUG
40# define RPCDBG_FACILITY RPCDBG_CALL
41#endif
42
Chuck Lever46121cf2007-01-31 12:14:08 -050043#define dprint_status(t) \
44 dprintk("RPC: %5u %s (status %d)\n", t->tk_pid, \
45 __FUNCTION__, t->tk_status)
46
Trond Myklebust188fef12007-06-16 14:18:40 -040047/*
48 * All RPC clients are linked into this list
49 */
50static LIST_HEAD(all_clients);
51static DEFINE_SPINLOCK(rpc_client_lock);
52
Linus Torvalds1da177e2005-04-16 15:20:36 -070053static DECLARE_WAIT_QUEUE_HEAD(destroy_wait);
54
55
56static void call_start(struct rpc_task *task);
57static void call_reserve(struct rpc_task *task);
58static void call_reserveresult(struct rpc_task *task);
59static void call_allocate(struct rpc_task *task);
60static void call_encode(struct rpc_task *task);
61static void call_decode(struct rpc_task *task);
62static void call_bind(struct rpc_task *task);
Chuck Leverda351872005-08-11 16:25:11 -040063static void call_bind_status(struct rpc_task *task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064static void call_transmit(struct rpc_task *task);
65static void call_status(struct rpc_task *task);
Trond Myklebust940e3312005-11-09 21:45:24 -050066static void call_transmit_status(struct rpc_task *task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067static void call_refresh(struct rpc_task *task);
68static void call_refreshresult(struct rpc_task *task);
69static void call_timeout(struct rpc_task *task);
70static void call_connect(struct rpc_task *task);
71static void call_connect_status(struct rpc_task *task);
Alexey Dobriyand8ed0292006-09-26 22:29:38 -070072static __be32 * call_header(struct rpc_task *task);
73static __be32 * call_verify(struct rpc_task *task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Trond Myklebust64c91a12007-06-23 10:17:16 -040075static int rpc_ping(struct rpc_clnt *clnt, int flags);
76
Trond Myklebust188fef12007-06-16 14:18:40 -040077static void rpc_register_client(struct rpc_clnt *clnt)
78{
79 spin_lock(&rpc_client_lock);
80 list_add(&clnt->cl_clients, &all_clients);
81 spin_unlock(&rpc_client_lock);
82}
83
84static void rpc_unregister_client(struct rpc_clnt *clnt)
85{
86 spin_lock(&rpc_client_lock);
87 list_del(&clnt->cl_clients);
88 spin_unlock(&rpc_client_lock);
89}
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
91static int
92rpc_setup_pipedir(struct rpc_clnt *clnt, char *dir_name)
93{
Trond Myklebustf1345852005-09-23 11:08:25 -040094 static uint32_t clntid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 int error;
96
Trond Myklebust54281542006-03-20 13:44:49 -050097 clnt->cl_vfsmnt = ERR_PTR(-ENOENT);
98 clnt->cl_dentry = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 if (dir_name == NULL)
100 return 0;
Trond Myklebust54281542006-03-20 13:44:49 -0500101
102 clnt->cl_vfsmnt = rpc_get_mount();
103 if (IS_ERR(clnt->cl_vfsmnt))
104 return PTR_ERR(clnt->cl_vfsmnt);
105
Trond Myklebustf1345852005-09-23 11:08:25 -0400106 for (;;) {
107 snprintf(clnt->cl_pathname, sizeof(clnt->cl_pathname),
108 "%s/clnt%x", dir_name,
109 (unsigned int)clntid++);
110 clnt->cl_pathname[sizeof(clnt->cl_pathname) - 1] = '\0';
111 clnt->cl_dentry = rpc_mkdir(clnt->cl_pathname, clnt);
112 if (!IS_ERR(clnt->cl_dentry))
113 return 0;
Christoph Hellwig278c9952005-07-24 23:53:01 +0100114 error = PTR_ERR(clnt->cl_dentry);
Trond Myklebustf1345852005-09-23 11:08:25 -0400115 if (error != -EEXIST) {
116 printk(KERN_INFO "RPC: Couldn't create pipefs entry %s, error %d\n",
117 clnt->cl_pathname, error);
Trond Myklebust54281542006-03-20 13:44:49 -0500118 rpc_put_mount();
Trond Myklebustf1345852005-09-23 11:08:25 -0400119 return error;
120 }
Christoph Hellwig278c9952005-07-24 23:53:01 +0100121 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122}
123
Chuck Leverff9aa5e2006-08-22 20:06:21 -0400124static 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 -0700125{
126 struct rpc_version *version;
127 struct rpc_clnt *clnt = NULL;
J. Bruce Fields6a192752005-06-22 17:16:23 +0000128 struct rpc_auth *auth;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 int err;
130 int len;
131
Chuck Lever46121cf2007-01-31 12:14:08 -0500132 dprintk("RPC: creating %s client for %s (xprt %p)\n",
133 program->name, servname, xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Trond Myklebust4ada5392007-06-14 17:26:17 -0400135 err = rpciod_up();
136 if (err)
137 goto out_no_rpciod;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 err = -EINVAL;
139 if (!xprt)
Adrian Bunk712917d2006-03-13 21:20:47 -0800140 goto out_no_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 if (vers >= program->nrvers || !(version = program->version[vers]))
142 goto out_err;
143
144 err = -ENOMEM;
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700145 clnt = kzalloc(sizeof(*clnt), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 if (!clnt)
147 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 clnt->cl_parent = clnt;
149
150 clnt->cl_server = clnt->cl_inline_name;
151 len = strlen(servname) + 1;
152 if (len > sizeof(clnt->cl_inline_name)) {
153 char *buf = kmalloc(len, GFP_KERNEL);
154 if (buf != 0)
155 clnt->cl_server = buf;
156 else
157 len = sizeof(clnt->cl_inline_name);
158 }
159 strlcpy(clnt->cl_server, servname, len);
160
161 clnt->cl_xprt = xprt;
162 clnt->cl_procinfo = version->procs;
163 clnt->cl_maxproc = version->nrprocs;
164 clnt->cl_protname = program->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 clnt->cl_prog = program->number;
166 clnt->cl_vers = version->number;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 clnt->cl_stats = program->stats;
Chuck Lever11c556b2006-03-20 13:44:22 -0500168 clnt->cl_metrics = rpc_alloc_iostats(clnt);
Trond Myklebust23bf85b2006-11-21 10:40:23 -0500169 err = -ENOMEM;
170 if (clnt->cl_metrics == NULL)
171 goto out_no_stats;
Trond Myklebust3e32a5d2006-11-16 11:37:27 -0500172 clnt->cl_program = program;
Trond Myklebust6529eba2007-06-14 16:40:14 -0400173 INIT_LIST_HEAD(&clnt->cl_tasks);
Trond Myklebust4bef61f2007-06-16 14:17:01 -0400174 spin_lock_init(&clnt->cl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Chuck Leverec739ef2006-08-22 20:06:15 -0400176 if (!xprt_bound(clnt->cl_xprt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 clnt->cl_autobind = 1;
178
179 clnt->cl_rtt = &clnt->cl_rtt_default;
180 rpc_init_rtt(&clnt->cl_rtt_default, xprt->timeout.to_initval);
181
Trond Myklebust34f52e32007-06-14 16:40:31 -0400182 kref_init(&clnt->cl_kref);
183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 err = rpc_setup_pipedir(clnt, program->pipe_dir_name);
185 if (err < 0)
186 goto out_no_path;
187
J. Bruce Fields6a192752005-06-22 17:16:23 +0000188 auth = rpcauth_create(flavor, clnt);
189 if (IS_ERR(auth)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 printk(KERN_INFO "RPC: Couldn't create auth handle (flavor %u)\n",
191 flavor);
J. Bruce Fields6a192752005-06-22 17:16:23 +0000192 err = PTR_ERR(auth);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 goto out_no_auth;
194 }
195
196 /* save the nodename */
Serge E. Hallyne9ff3992006-10-02 02:18:11 -0700197 clnt->cl_nodelen = strlen(utsname()->nodename);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 if (clnt->cl_nodelen > UNX_MAXNODENAME)
199 clnt->cl_nodelen = UNX_MAXNODENAME;
Serge E. Hallyne9ff3992006-10-02 02:18:11 -0700200 memcpy(clnt->cl_nodename, utsname()->nodename, clnt->cl_nodelen);
Trond Myklebust6529eba2007-06-14 16:40:14 -0400201 rpc_register_client(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 return clnt;
203
204out_no_auth:
Trond Myklebust54281542006-03-20 13:44:49 -0500205 if (!IS_ERR(clnt->cl_dentry)) {
Trond Myklebustdff02cc2006-07-31 14:17:18 -0700206 rpc_rmdir(clnt->cl_dentry);
Trond Myklebust54281542006-03-20 13:44:49 -0500207 rpc_put_mount();
208 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209out_no_path:
Trond Myklebust23bf85b2006-11-21 10:40:23 -0500210 rpc_free_iostats(clnt->cl_metrics);
211out_no_stats:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 if (clnt->cl_server != clnt->cl_inline_name)
213 kfree(clnt->cl_server);
214 kfree(clnt);
215out_err:
Trond Myklebust6b6ca862006-09-05 12:55:57 -0400216 xprt_put(xprt);
Adrian Bunk712917d2006-03-13 21:20:47 -0800217out_no_xprt:
Trond Myklebust4ada5392007-06-14 17:26:17 -0400218 rpciod_down();
219out_no_rpciod:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 return ERR_PTR(err);
221}
222
Chuck Leverc2866762006-08-22 20:06:20 -0400223/*
224 * rpc_create - create an RPC client and transport with one call
225 * @args: rpc_clnt create argument structure
226 *
227 * Creates and initializes an RPC transport and an RPC client.
228 *
229 * It can ping the server in order to determine if it is up, and to see if
230 * it supports this program and version. RPC_CLNT_CREATE_NOPING disables
231 * this behavior so asynchronous tasks can also use rpc_create.
232 */
233struct rpc_clnt *rpc_create(struct rpc_create_args *args)
234{
235 struct rpc_xprt *xprt;
236 struct rpc_clnt *clnt;
Frank van Maarseveen96802a02007-07-08 13:08:54 +0200237 struct rpc_xprtsock_create xprtargs = {
238 .proto = args->protocol,
239 .dstaddr = args->address,
240 .addrlen = args->addrsize,
241 .timeout = args->timeout
242 };
Chuck Lever43780b82007-07-01 12:13:22 -0400243 char servername[20];
Chuck Leverc2866762006-08-22 20:06:20 -0400244
Frank van Maarseveen96802a02007-07-08 13:08:54 +0200245 xprt = xprt_create_transport(&xprtargs);
Chuck Leverc2866762006-08-22 20:06:20 -0400246 if (IS_ERR(xprt))
247 return (struct rpc_clnt *)xprt;
248
249 /*
Chuck Lever43780b82007-07-01 12:13:22 -0400250 * If the caller chooses not to specify a hostname, whip
251 * up a string representation of the passed-in address.
252 */
253 if (args->servername == NULL) {
254 struct sockaddr_in *addr =
255 (struct sockaddr_in *) &args->address;
256 snprintf(servername, sizeof(servername), NIPQUAD_FMT,
257 NIPQUAD(addr->sin_addr.s_addr));
258 args->servername = servername;
259 }
260
261 /*
Chuck Leverc2866762006-08-22 20:06:20 -0400262 * By default, kernel RPC client connects from a reserved port.
263 * CAP_NET_BIND_SERVICE will not be set for unprivileged requesters,
264 * but it is always enabled for rpciod, which handles the connect
265 * operation.
266 */
267 xprt->resvport = 1;
268 if (args->flags & RPC_CLNT_CREATE_NONPRIVPORT)
269 xprt->resvport = 0;
270
271 dprintk("RPC: creating %s client for %s (xprt %p)\n",
Chuck Lever46121cf2007-01-31 12:14:08 -0500272 args->program->name, args->servername, xprt);
Chuck Leverc2866762006-08-22 20:06:20 -0400273
274 clnt = rpc_new_client(xprt, args->servername, args->program,
275 args->version, args->authflavor);
276 if (IS_ERR(clnt))
277 return clnt;
278
279 if (!(args->flags & RPC_CLNT_CREATE_NOPING)) {
280 int err = rpc_ping(clnt, RPC_TASK_SOFT|RPC_TASK_NOINTR);
281 if (err != 0) {
282 rpc_shutdown_client(clnt);
283 return ERR_PTR(err);
284 }
285 }
286
287 clnt->cl_softrtry = 1;
288 if (args->flags & RPC_CLNT_CREATE_HARDRTRY)
289 clnt->cl_softrtry = 0;
290
291 if (args->flags & RPC_CLNT_CREATE_INTR)
292 clnt->cl_intr = 1;
293 if (args->flags & RPC_CLNT_CREATE_AUTOBIND)
294 clnt->cl_autobind = 1;
Chuck Lever43d78ef2007-02-06 18:26:11 -0500295 if (args->flags & RPC_CLNT_CREATE_DISCRTRY)
296 clnt->cl_discrtry = 1;
Chuck Leverc2866762006-08-22 20:06:20 -0400297
298 return clnt;
299}
Chuck Leverb86acd52006-08-22 20:06:22 -0400300EXPORT_SYMBOL_GPL(rpc_create);
Chuck Leverc2866762006-08-22 20:06:20 -0400301
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302/*
303 * This function clones the RPC client structure. It allows us to share the
304 * same transport while varying parameters such as the authentication
305 * flavour.
306 */
307struct rpc_clnt *
308rpc_clone_client(struct rpc_clnt *clnt)
309{
310 struct rpc_clnt *new;
Trond Myklebust3e32a5d2006-11-16 11:37:27 -0500311 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Arnaldo Carvalho de Meloe69062b2006-11-21 01:21:34 -0200313 new = kmemdup(clnt, sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 if (!new)
315 goto out_no_clnt;
Trond Myklebustd431a5552007-06-17 17:07:54 -0400316 new->cl_parent = clnt;
317 /* Turn off autobind on clones */
318 new->cl_autobind = 0;
319 INIT_LIST_HEAD(&new->cl_tasks);
320 spin_lock_init(&new->cl_lock);
321 rpc_init_rtt(&new->cl_rtt_default, clnt->cl_xprt->timeout.to_initval);
Trond Myklebust23bf85b2006-11-21 10:40:23 -0500322 new->cl_metrics = rpc_alloc_iostats(clnt);
323 if (new->cl_metrics == NULL)
324 goto out_no_stats;
Trond Myklebust34f52e32007-06-14 16:40:31 -0400325 kref_init(&new->cl_kref);
Trond Myklebust3e32a5d2006-11-16 11:37:27 -0500326 err = rpc_setup_pipedir(new, clnt->cl_program->pipe_dir_name);
327 if (err != 0)
328 goto out_no_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 if (new->cl_auth)
330 atomic_inc(&new->cl_auth->au_count);
Trond Myklebustd431a5552007-06-17 17:07:54 -0400331 xprt_get(clnt->cl_xprt);
332 kref_get(&clnt->cl_kref);
Trond Myklebust6529eba2007-06-14 16:40:14 -0400333 rpc_register_client(new);
Trond Myklebust4ada5392007-06-14 17:26:17 -0400334 rpciod_up();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 return new;
Trond Myklebust3e32a5d2006-11-16 11:37:27 -0500336out_no_path:
337 rpc_free_iostats(new->cl_metrics);
Trond Myklebust23bf85b2006-11-21 10:40:23 -0500338out_no_stats:
339 kfree(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340out_no_clnt:
Chuck Lever46121cf2007-01-31 12:14:08 -0500341 dprintk("RPC: %s: returned error %d\n", __FUNCTION__, err);
Trond Myklebust3e32a5d2006-11-16 11:37:27 -0500342 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343}
344
345/*
346 * Properly shut down an RPC client, terminating all outstanding
Trond Myklebust90c57552007-06-09 19:49:36 -0400347 * requests.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 */
Trond Myklebust4c402b42007-06-14 16:40:32 -0400349void rpc_shutdown_client(struct rpc_clnt *clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350{
Trond Myklebust34f52e32007-06-14 16:40:31 -0400351 dprintk("RPC: shutting down %s client for %s\n",
352 clnt->cl_protname, clnt->cl_server);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Trond Myklebust34f52e32007-06-14 16:40:31 -0400354 while (!list_empty(&clnt->cl_tasks)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 rpc_killall_tasks(clnt);
Ingo Molnar532347e2006-01-09 20:52:53 -0800356 wait_event_timeout(destroy_wait,
Trond Myklebust34f52e32007-06-14 16:40:31 -0400357 list_empty(&clnt->cl_tasks), 1*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 }
359
Trond Myklebust4c402b42007-06-14 16:40:32 -0400360 rpc_release_client(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361}
362
363/*
Trond Myklebust34f52e32007-06-14 16:40:31 -0400364 * Free an RPC client
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 */
Trond Myklebust34f52e32007-06-14 16:40:31 -0400366static void
367rpc_free_client(struct kref *kref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
Trond Myklebust34f52e32007-06-14 16:40:31 -0400369 struct rpc_clnt *clnt = container_of(kref, struct rpc_clnt, cl_kref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Chuck Lever46121cf2007-01-31 12:14:08 -0500371 dprintk("RPC: destroying %s client for %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 clnt->cl_protname, clnt->cl_server);
Trond Myklebust8f8e7a52006-08-14 13:11:15 -0400373 if (!IS_ERR(clnt->cl_dentry)) {
Trond Myklebustdff02cc2006-07-31 14:17:18 -0700374 rpc_rmdir(clnt->cl_dentry);
Trond Myklebust8f8e7a52006-08-14 13:11:15 -0400375 rpc_put_mount();
376 }
Trond Myklebust3e32a5d2006-11-16 11:37:27 -0500377 if (clnt->cl_parent != clnt) {
Trond Myklebust8ad7c892007-06-14 16:40:32 -0400378 rpc_release_client(clnt->cl_parent);
Trond Myklebust3e32a5d2006-11-16 11:37:27 -0500379 goto out_free;
380 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 if (clnt->cl_server != clnt->cl_inline_name)
382 kfree(clnt->cl_server);
383out_free:
Trond Myklebust6529eba2007-06-14 16:40:14 -0400384 rpc_unregister_client(clnt);
Chuck Lever11c556b2006-03-20 13:44:22 -0500385 rpc_free_iostats(clnt->cl_metrics);
386 clnt->cl_metrics = NULL;
Trond Myklebust6b6ca862006-09-05 12:55:57 -0400387 xprt_put(clnt->cl_xprt);
Trond Myklebust4ada5392007-06-14 17:26:17 -0400388 rpciod_down();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 kfree(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390}
391
392/*
Trond Myklebust1dd17ec2007-06-26 16:57:41 -0400393 * Free an RPC client
394 */
395static void
396rpc_free_auth(struct kref *kref)
397{
398 struct rpc_clnt *clnt = container_of(kref, struct rpc_clnt, cl_kref);
399
400 if (clnt->cl_auth == NULL) {
401 rpc_free_client(kref);
402 return;
403 }
404
405 /*
406 * Note: RPCSEC_GSS may need to send NULL RPC calls in order to
407 * release remaining GSS contexts. This mechanism ensures
408 * that it can do so safely.
409 */
410 kref_init(kref);
411 rpcauth_release(clnt->cl_auth);
412 clnt->cl_auth = NULL;
413 kref_put(kref, rpc_free_client);
414}
415
416/*
Trond Myklebust34f52e32007-06-14 16:40:31 -0400417 * Release reference to the RPC client
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 */
419void
420rpc_release_client(struct rpc_clnt *clnt)
421{
Trond Myklebust34f52e32007-06-14 16:40:31 -0400422 dprintk("RPC: rpc_release_client(%p)\n", clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
Trond Myklebust34f52e32007-06-14 16:40:31 -0400424 if (list_empty(&clnt->cl_tasks))
425 wake_up(&destroy_wait);
Trond Myklebust1dd17ec2007-06-26 16:57:41 -0400426 kref_put(&clnt->cl_kref, rpc_free_auth);
Trond Myklebust34f52e32007-06-14 16:40:31 -0400427}
428
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000429/**
430 * rpc_bind_new_program - bind a new RPC program to an existing client
431 * @old - old rpc_client
432 * @program - rpc program to set
433 * @vers - rpc program version
434 *
435 * Clones the rpc client and sets up a new RPC program. This is mainly
436 * of use for enabling different RPC programs to share the same transport.
437 * The Sun NFSv2/v3 ACL protocol can do this.
438 */
439struct rpc_clnt *rpc_bind_new_program(struct rpc_clnt *old,
440 struct rpc_program *program,
441 int vers)
442{
443 struct rpc_clnt *clnt;
444 struct rpc_version *version;
445 int err;
446
447 BUG_ON(vers >= program->nrvers || !program->version[vers]);
448 version = program->version[vers];
449 clnt = rpc_clone_client(old);
450 if (IS_ERR(clnt))
451 goto out;
452 clnt->cl_procinfo = version->procs;
453 clnt->cl_maxproc = version->nrprocs;
454 clnt->cl_protname = program->name;
455 clnt->cl_prog = program->number;
456 clnt->cl_vers = version->number;
457 clnt->cl_stats = program->stats;
458 err = rpc_ping(clnt, RPC_TASK_SOFT|RPC_TASK_NOINTR);
459 if (err != 0) {
460 rpc_shutdown_client(clnt);
461 clnt = ERR_PTR(err);
462 }
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800463out:
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000464 return clnt;
465}
466
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467/*
468 * Default callback for async RPC calls
469 */
470static void
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100471rpc_default_callback(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472{
473}
474
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100475static const struct rpc_call_ops rpc_default_ops = {
476 .rpc_call_done = rpc_default_callback,
477};
478
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479/*
Trond Myklebust14b218a2005-06-22 17:16:28 +0000480 * Export the signal mask handling for synchronous code that
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 * sleeps on RPC calls
482 */
Trond Myklebust2bd61572006-01-03 09:55:19 +0100483#define RPC_INTR_SIGNALS (sigmask(SIGHUP) | sigmask(SIGINT) | sigmask(SIGQUIT) | sigmask(SIGTERM))
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800484
Trond Myklebust14b218a2005-06-22 17:16:28 +0000485static void rpc_save_sigmask(sigset_t *oldset, int intr)
486{
Trond Myklebust2bd61572006-01-03 09:55:19 +0100487 unsigned long sigallow = sigmask(SIGKILL);
Trond Myklebust14b218a2005-06-22 17:16:28 +0000488 sigset_t sigmask;
489
490 /* Block all signals except those listed in sigallow */
491 if (intr)
492 sigallow |= RPC_INTR_SIGNALS;
493 siginitsetinv(&sigmask, sigallow);
494 sigprocmask(SIG_BLOCK, &sigmask, oldset);
495}
496
497static inline void rpc_task_sigmask(struct rpc_task *task, sigset_t *oldset)
498{
499 rpc_save_sigmask(oldset, !RPC_TASK_UNINTERRUPTIBLE(task));
500}
501
502static inline void rpc_restore_sigmask(sigset_t *oldset)
503{
504 sigprocmask(SIG_SETMASK, oldset, NULL);
505}
506
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507void rpc_clnt_sigmask(struct rpc_clnt *clnt, sigset_t *oldset)
508{
Trond Myklebust14b218a2005-06-22 17:16:28 +0000509 rpc_save_sigmask(oldset, clnt->cl_intr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510}
511
512void rpc_clnt_sigunmask(struct rpc_clnt *clnt, sigset_t *oldset)
513{
Trond Myklebust14b218a2005-06-22 17:16:28 +0000514 rpc_restore_sigmask(oldset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515}
516
Trond Myklebust6e5b70e2007-06-12 10:02:37 -0400517static
518struct rpc_task *rpc_do_run_task(struct rpc_clnt *clnt,
519 struct rpc_message *msg,
520 int flags,
521 const struct rpc_call_ops *ops,
522 void *data)
523{
524 struct rpc_task *task, *ret;
525 sigset_t oldset;
526
527 task = rpc_new_task(clnt, flags, ops, data);
528 if (task == NULL) {
529 rpc_release_calldata(ops, data);
530 return ERR_PTR(-ENOMEM);
531 }
532
533 /* Mask signals on synchronous RPC calls and RPCSEC_GSS upcalls */
534 rpc_task_sigmask(task, &oldset);
535 if (msg != NULL) {
536 rpc_call_setup(task, msg, 0);
537 if (task->tk_status != 0) {
538 ret = ERR_PTR(task->tk_status);
539 rpc_put_task(task);
540 goto out;
541 }
542 }
543 atomic_inc(&task->tk_count);
544 rpc_execute(task);
545 ret = task;
546out:
547 rpc_restore_sigmask(&oldset);
548 return ret;
549}
550
551/**
552 * rpc_call_sync - Perform a synchronous RPC call
553 * @clnt: pointer to RPC client
554 * @msg: RPC call parameters
555 * @flags: RPC call flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 */
557int rpc_call_sync(struct rpc_clnt *clnt, struct rpc_message *msg, int flags)
558{
559 struct rpc_task *task;
Trond Myklebust6e5b70e2007-06-12 10:02:37 -0400560 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 BUG_ON(flags & RPC_TASK_ASYNC);
563
Trond Myklebust6e5b70e2007-06-12 10:02:37 -0400564 task = rpc_do_run_task(clnt, msg, flags, &rpc_default_ops, NULL);
565 if (IS_ERR(task))
566 return PTR_ERR(task);
Trond Myklebuste60859a2006-01-03 09:55:10 +0100567 status = task->tk_status;
Trond Myklebustbde8f002007-01-24 11:54:53 -0800568 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 return status;
570}
571
Trond Myklebust6e5b70e2007-06-12 10:02:37 -0400572/**
573 * rpc_call_async - Perform an asynchronous RPC call
574 * @clnt: pointer to RPC client
575 * @msg: RPC call parameters
576 * @flags: RPC call flags
577 * @ops: RPC call ops
578 * @data: user call data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 */
580int
581rpc_call_async(struct rpc_clnt *clnt, struct rpc_message *msg, int flags,
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100582 const struct rpc_call_ops *tk_ops, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583{
584 struct rpc_task *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
Trond Myklebust6e5b70e2007-06-12 10:02:37 -0400586 task = rpc_do_run_task(clnt, msg, flags|RPC_TASK_ASYNC, tk_ops, data);
587 if (IS_ERR(task))
588 return PTR_ERR(task);
589 rpc_put_task(task);
590 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591}
592
Trond Myklebust6e5b70e2007-06-12 10:02:37 -0400593/**
594 * rpc_run_task - Allocate a new RPC task, then run rpc_execute against it
595 * @clnt: pointer to RPC client
596 * @flags: RPC flags
597 * @ops: RPC call ops
598 * @data: user call data
599 */
600struct rpc_task *rpc_run_task(struct rpc_clnt *clnt, int flags,
601 const struct rpc_call_ops *tk_ops,
602 void *data)
603{
604 return rpc_do_run_task(clnt, NULL, flags, tk_ops, data);
605}
606EXPORT_SYMBOL(rpc_run_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
608void
609rpc_call_setup(struct rpc_task *task, struct rpc_message *msg, int flags)
610{
611 task->tk_msg = *msg;
612 task->tk_flags |= flags;
613 /* Bind the user cred */
614 if (task->tk_msg.rpc_cred != NULL)
615 rpcauth_holdcred(task);
616 else
617 rpcauth_bindcred(task);
618
619 if (task->tk_status == 0)
620 task->tk_action = call_start;
621 else
Trond Myklebustabbcf282006-01-03 09:55:03 +0100622 task->tk_action = rpc_exit_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623}
624
Chuck Levered394402006-08-22 20:06:17 -0400625/**
626 * rpc_peeraddr - extract remote peer address from clnt's xprt
627 * @clnt: RPC client structure
628 * @buf: target buffer
629 * @size: length of target buffer
630 *
631 * Returns the number of bytes that are actually in the stored address.
632 */
633size_t rpc_peeraddr(struct rpc_clnt *clnt, struct sockaddr *buf, size_t bufsize)
634{
635 size_t bytes;
636 struct rpc_xprt *xprt = clnt->cl_xprt;
637
638 bytes = sizeof(xprt->addr);
639 if (bytes > bufsize)
640 bytes = bufsize;
641 memcpy(buf, &clnt->cl_xprt->addr, bytes);
Chuck Leverc4efcb12006-08-22 20:06:19 -0400642 return xprt->addrlen;
Chuck Levered394402006-08-22 20:06:17 -0400643}
Chuck Leverb86acd52006-08-22 20:06:22 -0400644EXPORT_SYMBOL_GPL(rpc_peeraddr);
Chuck Levered394402006-08-22 20:06:17 -0400645
Chuck Leverf425eba2006-08-22 20:06:18 -0400646/**
647 * rpc_peeraddr2str - return remote peer address in printable format
648 * @clnt: RPC client structure
649 * @format: address format
650 *
651 */
652char *rpc_peeraddr2str(struct rpc_clnt *clnt, enum rpc_display_format_t format)
653{
654 struct rpc_xprt *xprt = clnt->cl_xprt;
Chuck Lever7559c7a2006-12-05 16:35:37 -0500655
656 if (xprt->address_strings[format] != NULL)
657 return xprt->address_strings[format];
658 else
659 return "unprintable";
Chuck Leverf425eba2006-08-22 20:06:18 -0400660}
Chuck Leverb86acd52006-08-22 20:06:22 -0400661EXPORT_SYMBOL_GPL(rpc_peeraddr2str);
Chuck Leverf425eba2006-08-22 20:06:18 -0400662
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663void
664rpc_setbufsize(struct rpc_clnt *clnt, unsigned int sndsize, unsigned int rcvsize)
665{
666 struct rpc_xprt *xprt = clnt->cl_xprt;
Chuck Lever470056c2005-08-25 16:25:56 -0700667 if (xprt->ops->set_buffer_size)
668 xprt->ops->set_buffer_size(xprt, sndsize, rcvsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669}
670
671/*
672 * Return size of largest payload RPC client can support, in bytes
673 *
674 * For stream transports, this is one RPC record fragment (see RFC
675 * 1831), as we don't support multi-record requests yet. For datagram
676 * transports, this is the size of an IP packet minus the IP, UDP, and
677 * RPC header sizes.
678 */
679size_t rpc_max_payload(struct rpc_clnt *clnt)
680{
681 return clnt->cl_xprt->max_payload;
682}
Chuck Leverb86acd52006-08-22 20:06:22 -0400683EXPORT_SYMBOL_GPL(rpc_max_payload);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
Chuck Lever35f5a422006-01-03 09:55:50 +0100685/**
686 * rpc_force_rebind - force transport to check that remote port is unchanged
687 * @clnt: client to rebind
688 *
689 */
690void rpc_force_rebind(struct rpc_clnt *clnt)
691{
692 if (clnt->cl_autobind)
Chuck Leverec739ef2006-08-22 20:06:15 -0400693 xprt_clear_bound(clnt->cl_xprt);
Chuck Lever35f5a422006-01-03 09:55:50 +0100694}
Chuck Leverb86acd52006-08-22 20:06:22 -0400695EXPORT_SYMBOL_GPL(rpc_force_rebind);
Chuck Lever35f5a422006-01-03 09:55:50 +0100696
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697/*
698 * Restart an (async) RPC call. Usually called from within the
699 * exit handler.
700 */
701void
702rpc_restart_call(struct rpc_task *task)
703{
704 if (RPC_ASSASSINATED(task))
705 return;
706
707 task->tk_action = call_start;
708}
709
710/*
711 * 0. Initial state
712 *
713 * Other FSM states can be visited zero or more times, but
714 * this state is visited exactly once for each RPC.
715 */
716static void
717call_start(struct rpc_task *task)
718{
719 struct rpc_clnt *clnt = task->tk_client;
720
Chuck Lever46121cf2007-01-31 12:14:08 -0500721 dprintk("RPC: %5u call_start %s%d proc %d (%s)\n", task->tk_pid,
722 clnt->cl_protname, clnt->cl_vers,
723 task->tk_msg.rpc_proc->p_proc,
724 (RPC_IS_ASYNC(task) ? "async" : "sync"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
726 /* Increment call count */
727 task->tk_msg.rpc_proc->p_count++;
728 clnt->cl_stats->rpccnt++;
729 task->tk_action = call_reserve;
730}
731
732/*
733 * 1. Reserve an RPC call slot
734 */
735static void
736call_reserve(struct rpc_task *task)
737{
Chuck Lever46121cf2007-01-31 12:14:08 -0500738 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
740 if (!rpcauth_uptodatecred(task)) {
741 task->tk_action = call_refresh;
742 return;
743 }
744
745 task->tk_status = 0;
746 task->tk_action = call_reserveresult;
747 xprt_reserve(task);
748}
749
750/*
751 * 1b. Grok the result of xprt_reserve()
752 */
753static void
754call_reserveresult(struct rpc_task *task)
755{
756 int status = task->tk_status;
757
Chuck Lever46121cf2007-01-31 12:14:08 -0500758 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
760 /*
761 * After a call to xprt_reserve(), we must have either
762 * a request slot or else an error status.
763 */
764 task->tk_status = 0;
765 if (status >= 0) {
766 if (task->tk_rqstp) {
767 task->tk_action = call_allocate;
768 return;
769 }
770
771 printk(KERN_ERR "%s: status=%d, but no request slot, exiting\n",
772 __FUNCTION__, status);
773 rpc_exit(task, -EIO);
774 return;
775 }
776
777 /*
778 * Even though there was an error, we may have acquired
779 * a request slot somehow. Make sure not to leak it.
780 */
781 if (task->tk_rqstp) {
782 printk(KERN_ERR "%s: status=%d, request allocated anyway\n",
783 __FUNCTION__, status);
784 xprt_release(task);
785 }
786
787 switch (status) {
788 case -EAGAIN: /* woken up; retry */
789 task->tk_action = call_reserve;
790 return;
791 case -EIO: /* probably a shutdown */
792 break;
793 default:
794 printk(KERN_ERR "%s: unrecognized error %d, exiting\n",
795 __FUNCTION__, status);
796 break;
797 }
798 rpc_exit(task, status);
799}
800
801/*
802 * 2. Allocate the buffer. For details, see sched.c:rpc_malloc.
Chuck Lever02107142006-01-03 09:55:49 +0100803 * (Note: buffer memory is freed in xprt_release).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 */
805static void
806call_allocate(struct rpc_task *task)
807{
Trond Myklebust1be27f32007-06-27 14:29:04 -0400808 unsigned int slack = task->tk_msg.rpc_cred->cr_auth->au_cslack;
Chuck Lever02107142006-01-03 09:55:49 +0100809 struct rpc_rqst *req = task->tk_rqstp;
810 struct rpc_xprt *xprt = task->tk_xprt;
Chuck Lever2bea90d2007-03-29 16:47:53 -0400811 struct rpc_procinfo *proc = task->tk_msg.rpc_proc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
Chuck Lever46121cf2007-01-31 12:14:08 -0500813 dprint_status(task);
814
Chuck Lever2bea90d2007-03-29 16:47:53 -0400815 task->tk_status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 task->tk_action = call_bind;
Chuck Lever2bea90d2007-03-29 16:47:53 -0400817
Chuck Lever02107142006-01-03 09:55:49 +0100818 if (req->rq_buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 return;
820
Chuck Lever2bea90d2007-03-29 16:47:53 -0400821 if (proc->p_proc != 0) {
822 BUG_ON(proc->p_arglen == 0);
823 if (proc->p_decode != NULL)
824 BUG_ON(proc->p_replen == 0);
825 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
Chuck Lever2bea90d2007-03-29 16:47:53 -0400827 /*
828 * Calculate the size (in quads) of the RPC call
829 * and reply headers, and convert both values
830 * to byte sizes.
831 */
832 req->rq_callsize = RPC_CALLHDRSIZE + (slack << 1) + proc->p_arglen;
833 req->rq_callsize <<= 2;
834 req->rq_rcvsize = RPC_REPHDRSIZE + slack + proc->p_replen;
835 req->rq_rcvsize <<= 2;
836
Chuck Leverc5a4dd82007-03-29 16:47:58 -0400837 req->rq_buffer = xprt->ops->buf_alloc(task,
838 req->rq_callsize + req->rq_rcvsize);
Chuck Lever2bea90d2007-03-29 16:47:53 -0400839 if (req->rq_buffer != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 return;
Chuck Lever46121cf2007-01-31 12:14:08 -0500841
842 dprintk("RPC: %5u rpc_buffer allocation failed\n", task->tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Trond Myklebust14b218a2005-06-22 17:16:28 +0000844 if (RPC_IS_ASYNC(task) || !signalled()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 xprt_release(task);
846 task->tk_action = call_reserve;
847 rpc_delay(task, HZ>>4);
848 return;
849 }
850
851 rpc_exit(task, -ERESTARTSYS);
852}
853
Trond Myklebust940e3312005-11-09 21:45:24 -0500854static inline int
855rpc_task_need_encode(struct rpc_task *task)
856{
857 return task->tk_rqstp->rq_snd_buf.len == 0;
858}
859
860static inline void
861rpc_task_force_reencode(struct rpc_task *task)
862{
863 task->tk_rqstp->rq_snd_buf.len = 0;
864}
865
Chuck Lever2bea90d2007-03-29 16:47:53 -0400866static inline void
867rpc_xdr_buf_init(struct xdr_buf *buf, void *start, size_t len)
868{
869 buf->head[0].iov_base = start;
870 buf->head[0].iov_len = len;
871 buf->tail[0].iov_len = 0;
872 buf->page_len = 0;
873 buf->len = 0;
874 buf->buflen = len;
875}
876
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877/*
878 * 3. Encode arguments of an RPC call
879 */
880static void
881call_encode(struct rpc_task *task)
882{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 struct rpc_rqst *req = task->tk_rqstp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 kxdrproc_t encode;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700885 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
Chuck Lever46121cf2007-01-31 12:14:08 -0500887 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
Chuck Lever2bea90d2007-03-29 16:47:53 -0400889 rpc_xdr_buf_init(&req->rq_snd_buf,
890 req->rq_buffer,
891 req->rq_callsize);
892 rpc_xdr_buf_init(&req->rq_rcv_buf,
893 (char *)req->rq_buffer + req->rq_callsize,
894 req->rq_rcvsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
896 /* Encode header and provided arguments */
897 encode = task->tk_msg.rpc_proc->p_encode;
898 if (!(p = call_header(task))) {
899 printk(KERN_INFO "RPC: call_header failed, exit EIO\n");
900 rpc_exit(task, -EIO);
901 return;
902 }
J. Bruce Fieldsf3680312005-10-13 16:54:48 -0400903 if (encode == NULL)
904 return;
905
Trond Myklebust6d5fcb52006-10-18 16:01:06 -0400906 lock_kernel();
J. Bruce Fieldsf3680312005-10-13 16:54:48 -0400907 task->tk_status = rpcauth_wrap_req(task, encode, req, p,
908 task->tk_msg.rpc_argp);
Trond Myklebust6d5fcb52006-10-18 16:01:06 -0400909 unlock_kernel();
J. Bruce Fieldsf3680312005-10-13 16:54:48 -0400910 if (task->tk_status == -ENOMEM) {
911 /* XXX: Is this sane? */
912 rpc_delay(task, 3*HZ);
913 task->tk_status = -EAGAIN;
914 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915}
916
917/*
918 * 4. Get the server port number if not yet set
919 */
920static void
921call_bind(struct rpc_task *task)
922{
Chuck Leverec739ef2006-08-22 20:06:15 -0400923 struct rpc_xprt *xprt = task->tk_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
Chuck Lever46121cf2007-01-31 12:14:08 -0500925 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
Chuck Leverda351872005-08-11 16:25:11 -0400927 task->tk_action = call_connect;
Chuck Leverec739ef2006-08-22 20:06:15 -0400928 if (!xprt_bound(xprt)) {
Chuck Leverda351872005-08-11 16:25:11 -0400929 task->tk_action = call_bind_status;
Chuck Leverec739ef2006-08-22 20:06:15 -0400930 task->tk_timeout = xprt->bind_timeout;
Chuck Leverbbf7c1d2006-08-22 20:06:16 -0400931 xprt->ops->rpcbind(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 }
933}
934
935/*
Chuck Leverda351872005-08-11 16:25:11 -0400936 * 4a. Sort out bind result
937 */
938static void
939call_bind_status(struct rpc_task *task)
940{
941 int status = -EACCES;
942
943 if (task->tk_status >= 0) {
Chuck Lever46121cf2007-01-31 12:14:08 -0500944 dprint_status(task);
Chuck Leverda351872005-08-11 16:25:11 -0400945 task->tk_status = 0;
946 task->tk_action = call_connect;
947 return;
948 }
949
950 switch (task->tk_status) {
951 case -EACCES:
Chuck Lever46121cf2007-01-31 12:14:08 -0500952 dprintk("RPC: %5u remote rpcbind: RPC program/version "
953 "unavailable\n", task->tk_pid);
Chuck Leverea635a52005-10-06 23:12:58 -0400954 rpc_delay(task, 3*HZ);
Trond Myklebustda458282006-08-31 15:44:52 -0400955 goto retry_timeout;
Chuck Leverda351872005-08-11 16:25:11 -0400956 case -ETIMEDOUT:
Chuck Lever46121cf2007-01-31 12:14:08 -0500957 dprintk("RPC: %5u rpcbind request timed out\n",
Chuck Leverda351872005-08-11 16:25:11 -0400958 task->tk_pid);
Trond Myklebustda458282006-08-31 15:44:52 -0400959 goto retry_timeout;
Chuck Leverda351872005-08-11 16:25:11 -0400960 case -EPFNOSUPPORT:
Chuck Lever46121cf2007-01-31 12:14:08 -0500961 dprintk("RPC: %5u remote rpcbind service unavailable\n",
Chuck Leverda351872005-08-11 16:25:11 -0400962 task->tk_pid);
963 break;
964 case -EPROTONOSUPPORT:
Chuck Lever00a6e7b2007-03-29 16:48:33 -0400965 dprintk("RPC: %5u remote rpcbind version unavailable, retrying\n",
Chuck Leverda351872005-08-11 16:25:11 -0400966 task->tk_pid);
Chuck Lever00a6e7b2007-03-29 16:48:33 -0400967 task->tk_status = 0;
968 task->tk_action = call_bind;
969 return;
Chuck Leverda351872005-08-11 16:25:11 -0400970 default:
Chuck Lever46121cf2007-01-31 12:14:08 -0500971 dprintk("RPC: %5u unrecognized rpcbind error (%d)\n",
Chuck Leverda351872005-08-11 16:25:11 -0400972 task->tk_pid, -task->tk_status);
973 status = -EIO;
Chuck Leverda351872005-08-11 16:25:11 -0400974 }
975
976 rpc_exit(task, status);
977 return;
978
Trond Myklebustda458282006-08-31 15:44:52 -0400979retry_timeout:
980 task->tk_action = call_timeout;
Chuck Leverda351872005-08-11 16:25:11 -0400981}
982
983/*
984 * 4b. Connect to the RPC server
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 */
986static void
987call_connect(struct rpc_task *task)
988{
Chuck Leverda351872005-08-11 16:25:11 -0400989 struct rpc_xprt *xprt = task->tk_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
Chuck Lever46121cf2007-01-31 12:14:08 -0500991 dprintk("RPC: %5u call_connect xprt %p %s connected\n",
Chuck Leverda351872005-08-11 16:25:11 -0400992 task->tk_pid, xprt,
993 (xprt_connected(xprt) ? "is" : "is not"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
Chuck Leverda351872005-08-11 16:25:11 -0400995 task->tk_action = call_transmit;
996 if (!xprt_connected(xprt)) {
997 task->tk_action = call_connect_status;
998 if (task->tk_status < 0)
999 return;
1000 xprt_connect(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002}
1003
1004/*
Chuck Leverda351872005-08-11 16:25:11 -04001005 * 4c. Sort out connect result
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 */
1007static void
1008call_connect_status(struct rpc_task *task)
1009{
1010 struct rpc_clnt *clnt = task->tk_client;
1011 int status = task->tk_status;
1012
Chuck Lever46121cf2007-01-31 12:14:08 -05001013 dprint_status(task);
Chuck Leverda351872005-08-11 16:25:11 -04001014
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 task->tk_status = 0;
1016 if (status >= 0) {
1017 clnt->cl_stats->netreconn++;
1018 task->tk_action = call_transmit;
1019 return;
1020 }
1021
Chuck Leverda351872005-08-11 16:25:11 -04001022 /* Something failed: remote service port may have changed */
Chuck Lever35f5a422006-01-03 09:55:50 +01001023 rpc_force_rebind(clnt);
Chuck Leverda351872005-08-11 16:25:11 -04001024
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 switch (status) {
1026 case -ENOTCONN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 case -EAGAIN:
Chuck Leverda351872005-08-11 16:25:11 -04001028 task->tk_action = call_bind;
Trond Myklebustda458282006-08-31 15:44:52 -04001029 if (!RPC_IS_SOFT(task))
1030 return;
1031 /* if soft mounted, test if we've timed out */
1032 case -ETIMEDOUT:
1033 task->tk_action = call_timeout;
1034 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 }
Trond Myklebustda458282006-08-31 15:44:52 -04001036 rpc_exit(task, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037}
1038
1039/*
1040 * 5. Transmit the RPC request, and wait for reply
1041 */
1042static void
1043call_transmit(struct rpc_task *task)
1044{
Chuck Lever46121cf2007-01-31 12:14:08 -05001045 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
1047 task->tk_action = call_status;
1048 if (task->tk_status < 0)
1049 return;
1050 task->tk_status = xprt_prepare_transmit(task);
1051 if (task->tk_status != 0)
1052 return;
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04001053 task->tk_action = call_transmit_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 /* Encode here so that rpcsec_gss can use correct sequence number. */
Trond Myklebust940e3312005-11-09 21:45:24 -05001055 if (rpc_task_need_encode(task)) {
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04001056 BUG_ON(task->tk_rqstp->rq_bytes_sent != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 call_encode(task);
Trond Myklebust5e5ce5b2005-10-18 14:20:11 -07001058 /* Did the encode result in an error condition? */
1059 if (task->tk_status != 0)
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04001060 return;
Trond Myklebust5e5ce5b2005-10-18 14:20:11 -07001061 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 xprt_transmit(task);
1063 if (task->tk_status < 0)
1064 return;
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04001065 /*
1066 * On success, ensure that we call xprt_end_transmit() before sleeping
1067 * in order to allow access to the socket to other RPC requests.
1068 */
1069 call_transmit_status(task);
1070 if (task->tk_msg.rpc_proc->p_decode != NULL)
1071 return;
1072 task->tk_action = rpc_exit_task;
1073 rpc_wake_up_task(task);
1074}
1075
1076/*
1077 * 5a. Handle cleanup after a transmission
1078 */
1079static void
1080call_transmit_status(struct rpc_task *task)
1081{
1082 task->tk_action = call_status;
1083 /*
1084 * Special case: if we've been waiting on the socket's write_space()
1085 * callback, then don't call xprt_end_transmit().
1086 */
1087 if (task->tk_status == -EAGAIN)
1088 return;
1089 xprt_end_transmit(task);
Trond Myklebust940e3312005-11-09 21:45:24 -05001090 rpc_task_force_reencode(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091}
1092
1093/*
1094 * 6. Sort out the RPC call status
1095 */
1096static void
1097call_status(struct rpc_task *task)
1098{
1099 struct rpc_clnt *clnt = task->tk_client;
1100 struct rpc_rqst *req = task->tk_rqstp;
1101 int status;
1102
1103 if (req->rq_received > 0 && !req->rq_bytes_sent)
1104 task->tk_status = req->rq_received;
1105
Chuck Lever46121cf2007-01-31 12:14:08 -05001106 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
1108 status = task->tk_status;
1109 if (status >= 0) {
1110 task->tk_action = call_decode;
1111 return;
1112 }
1113
1114 task->tk_status = 0;
1115 switch(status) {
Trond Myklebust76303992006-08-30 14:32:49 -04001116 case -EHOSTDOWN:
1117 case -EHOSTUNREACH:
1118 case -ENETUNREACH:
1119 /*
1120 * Delay any retries for 3 seconds, then handle as if it
1121 * were a timeout.
1122 */
1123 rpc_delay(task, 3*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 case -ETIMEDOUT:
1125 task->tk_action = call_timeout;
Trond Myklebust241c39b2007-04-20 16:12:55 -04001126 if (task->tk_client->cl_discrtry)
1127 xprt_disconnect(task->tk_xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 break;
1129 case -ECONNREFUSED:
1130 case -ENOTCONN:
Chuck Lever35f5a422006-01-03 09:55:50 +01001131 rpc_force_rebind(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 task->tk_action = call_bind;
1133 break;
1134 case -EAGAIN:
1135 task->tk_action = call_transmit;
1136 break;
1137 case -EIO:
1138 /* shutdown or soft timeout */
1139 rpc_exit(task, status);
1140 break;
1141 default:
Chuck Leverf518e35a2006-01-03 09:55:52 +01001142 printk("%s: RPC call returned error %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 clnt->cl_protname, -status);
1144 rpc_exit(task, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 }
1146}
1147
1148/*
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04001149 * 6a. Handle RPC timeout
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 * We do not release the request slot, so we keep using the
1151 * same XID for all retransmits.
1152 */
1153static void
1154call_timeout(struct rpc_task *task)
1155{
1156 struct rpc_clnt *clnt = task->tk_client;
1157
1158 if (xprt_adjust_timeout(task->tk_rqstp) == 0) {
Chuck Lever46121cf2007-01-31 12:14:08 -05001159 dprintk("RPC: %5u call_timeout (minor)\n", task->tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 goto retry;
1161 }
1162
Chuck Lever46121cf2007-01-31 12:14:08 -05001163 dprintk("RPC: %5u call_timeout (major)\n", task->tk_pid);
Chuck Leveref759a22006-03-20 13:44:17 -05001164 task->tk_timeouts++;
1165
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 if (RPC_IS_SOFT(task)) {
Chuck Leverf518e35a2006-01-03 09:55:52 +01001167 printk(KERN_NOTICE "%s: server %s not responding, timed out\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 clnt->cl_protname, clnt->cl_server);
1169 rpc_exit(task, -EIO);
1170 return;
1171 }
1172
Chuck Leverf518e35a2006-01-03 09:55:52 +01001173 if (!(task->tk_flags & RPC_CALL_MAJORSEEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 task->tk_flags |= RPC_CALL_MAJORSEEN;
1175 printk(KERN_NOTICE "%s: server %s not responding, still trying\n",
1176 clnt->cl_protname, clnt->cl_server);
1177 }
Chuck Lever35f5a422006-01-03 09:55:50 +01001178 rpc_force_rebind(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
1180retry:
1181 clnt->cl_stats->rpcretrans++;
1182 task->tk_action = call_bind;
1183 task->tk_status = 0;
1184}
1185
1186/*
1187 * 7. Decode the RPC reply
1188 */
1189static void
1190call_decode(struct rpc_task *task)
1191{
1192 struct rpc_clnt *clnt = task->tk_client;
1193 struct rpc_rqst *req = task->tk_rqstp;
1194 kxdrproc_t decode = task->tk_msg.rpc_proc->p_decode;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001195 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
Chuck Lever46121cf2007-01-31 12:14:08 -05001197 dprintk("RPC: %5u call_decode (status %d)\n",
1198 task->tk_pid, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
Chuck Leverf518e35a2006-01-03 09:55:52 +01001200 if (task->tk_flags & RPC_CALL_MAJORSEEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 printk(KERN_NOTICE "%s: server %s OK\n",
1202 clnt->cl_protname, clnt->cl_server);
1203 task->tk_flags &= ~RPC_CALL_MAJORSEEN;
1204 }
1205
1206 if (task->tk_status < 12) {
1207 if (!RPC_IS_SOFT(task)) {
1208 task->tk_action = call_bind;
1209 clnt->cl_stats->rpcretrans++;
1210 goto out_retry;
1211 }
Chuck Lever46121cf2007-01-31 12:14:08 -05001212 dprintk("RPC: %s: too small RPC reply size (%d bytes)\n",
1213 clnt->cl_protname, task->tk_status);
Trond Myklebustda458282006-08-31 15:44:52 -04001214 task->tk_action = call_timeout;
1215 goto out_retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 }
1217
Trond Myklebust43ac3f22006-03-20 13:44:51 -05001218 /*
1219 * Ensure that we see all writes made by xprt_complete_rqst()
1220 * before it changed req->rq_received.
1221 */
1222 smp_rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 req->rq_rcv_buf.len = req->rq_private_buf.len;
1224
1225 /* Check that the softirq receive buffer is valid */
1226 WARN_ON(memcmp(&req->rq_rcv_buf, &req->rq_private_buf,
1227 sizeof(req->rq_rcv_buf)) != 0);
1228
1229 /* Verify the RPC header */
Trond Myklebustabbcf282006-01-03 09:55:03 +01001230 p = call_verify(task);
1231 if (IS_ERR(p)) {
1232 if (p == ERR_PTR(-EAGAIN))
1233 goto out_retry;
1234 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 }
1236
Trond Myklebustabbcf282006-01-03 09:55:03 +01001237 task->tk_action = rpc_exit_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
Trond Myklebust6d5fcb52006-10-18 16:01:06 -04001239 if (decode) {
1240 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 task->tk_status = rpcauth_unwrap_resp(task, decode, req, p,
1242 task->tk_msg.rpc_resp);
Trond Myklebust6d5fcb52006-10-18 16:01:06 -04001243 unlock_kernel();
1244 }
Chuck Lever46121cf2007-01-31 12:14:08 -05001245 dprintk("RPC: %5u call_decode result %d\n", task->tk_pid,
1246 task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 return;
1248out_retry:
1249 req->rq_received = req->rq_private_buf.len = 0;
1250 task->tk_status = 0;
Trond Myklebust241c39b2007-04-20 16:12:55 -04001251 if (task->tk_client->cl_discrtry)
1252 xprt_disconnect(task->tk_xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253}
1254
1255/*
1256 * 8. Refresh the credentials if rejected by the server
1257 */
1258static void
1259call_refresh(struct rpc_task *task)
1260{
Chuck Lever46121cf2007-01-31 12:14:08 -05001261 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
1263 xprt_release(task); /* Must do to obtain new XID */
1264 task->tk_action = call_refreshresult;
1265 task->tk_status = 0;
1266 task->tk_client->cl_stats->rpcauthrefresh++;
1267 rpcauth_refreshcred(task);
1268}
1269
1270/*
1271 * 8a. Process the results of a credential refresh
1272 */
1273static void
1274call_refreshresult(struct rpc_task *task)
1275{
1276 int status = task->tk_status;
Chuck Lever46121cf2007-01-31 12:14:08 -05001277
1278 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
1280 task->tk_status = 0;
1281 task->tk_action = call_reserve;
1282 if (status >= 0 && rpcauth_uptodatecred(task))
1283 return;
1284 if (status == -EACCES) {
1285 rpc_exit(task, -EACCES);
1286 return;
1287 }
1288 task->tk_action = call_refresh;
1289 if (status != -ETIMEDOUT)
1290 rpc_delay(task, 3*HZ);
1291 return;
1292}
1293
1294/*
1295 * Call header serialization
1296 */
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001297static __be32 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298call_header(struct rpc_task *task)
1299{
1300 struct rpc_clnt *clnt = task->tk_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 struct rpc_rqst *req = task->tk_rqstp;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001302 __be32 *p = req->rq_svec[0].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
1304 /* FIXME: check buffer size? */
Chuck Lever808012f2005-08-25 16:25:49 -07001305
1306 p = xprt_skip_transport_header(task->tk_xprt, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 *p++ = req->rq_xid; /* XID */
1308 *p++ = htonl(RPC_CALL); /* CALL */
1309 *p++ = htonl(RPC_VERSION); /* RPC version */
1310 *p++ = htonl(clnt->cl_prog); /* program number */
1311 *p++ = htonl(clnt->cl_vers); /* program version */
1312 *p++ = htonl(task->tk_msg.rpc_proc->p_proc); /* procedure */
Trond Myklebust334ccfd2005-06-22 17:16:19 +00001313 p = rpcauth_marshcred(task, p);
1314 req->rq_slen = xdr_adjust_iovec(&req->rq_svec[0], p);
1315 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316}
1317
1318/*
1319 * Reply header verification
1320 */
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001321static __be32 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322call_verify(struct rpc_task *task)
1323{
1324 struct kvec *iov = &task->tk_rqstp->rq_rcv_buf.head[0];
1325 int len = task->tk_rqstp->rq_rcv_buf.len >> 2;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001326 __be32 *p = iov->iov_base;
1327 u32 n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 int error = -EACCES;
1329
David Howellse8896492006-08-24 15:44:19 -04001330 if ((task->tk_rqstp->rq_rcv_buf.len & 3) != 0) {
1331 /* RFC-1014 says that the representation of XDR data must be a
1332 * multiple of four bytes
1333 * - if it isn't pointer subtraction in the NFS client may give
1334 * undefined results
1335 */
Trond Myklebust8a702bb2007-06-27 18:30:26 -04001336 dprintk("RPC: %5u %s: XDR representation not a multiple of"
1337 " 4 bytes: 0x%x\n", task->tk_pid, __FUNCTION__,
1338 task->tk_rqstp->rq_rcv_buf.len);
David Howellse8896492006-08-24 15:44:19 -04001339 goto out_eio;
1340 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 if ((len -= 3) < 0)
1342 goto out_overflow;
1343 p += 1; /* skip XID */
1344
1345 if ((n = ntohl(*p++)) != RPC_REPLY) {
Trond Myklebust8a702bb2007-06-27 18:30:26 -04001346 dprintk("RPC: %5u %s: not an RPC reply: %x\n",
1347 task->tk_pid, __FUNCTION__, n);
Trond Myklebustabbcf282006-01-03 09:55:03 +01001348 goto out_garbage;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 }
1350 if ((n = ntohl(*p++)) != RPC_MSG_ACCEPTED) {
1351 if (--len < 0)
1352 goto out_overflow;
1353 switch ((n = ntohl(*p++))) {
1354 case RPC_AUTH_ERROR:
1355 break;
1356 case RPC_MISMATCH:
Chuck Lever46121cf2007-01-31 12:14:08 -05001357 dprintk("RPC: %5u %s: RPC call version "
1358 "mismatch!\n",
1359 task->tk_pid, __FUNCTION__);
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00001360 error = -EPROTONOSUPPORT;
1361 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 default:
Chuck Lever46121cf2007-01-31 12:14:08 -05001363 dprintk("RPC: %5u %s: RPC call rejected, "
1364 "unknown error: %x\n",
1365 task->tk_pid, __FUNCTION__, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 goto out_eio;
1367 }
1368 if (--len < 0)
1369 goto out_overflow;
1370 switch ((n = ntohl(*p++))) {
1371 case RPC_AUTH_REJECTEDCRED:
1372 case RPC_AUTH_REJECTEDVERF:
1373 case RPCSEC_GSS_CREDPROBLEM:
1374 case RPCSEC_GSS_CTXPROBLEM:
1375 if (!task->tk_cred_retry)
1376 break;
1377 task->tk_cred_retry--;
Chuck Lever46121cf2007-01-31 12:14:08 -05001378 dprintk("RPC: %5u %s: retry stale creds\n",
1379 task->tk_pid, __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 rpcauth_invalcred(task);
1381 task->tk_action = call_refresh;
Trond Myklebustabbcf282006-01-03 09:55:03 +01001382 goto out_retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 case RPC_AUTH_BADCRED:
1384 case RPC_AUTH_BADVERF:
1385 /* possibly garbled cred/verf? */
1386 if (!task->tk_garb_retry)
1387 break;
1388 task->tk_garb_retry--;
Chuck Lever46121cf2007-01-31 12:14:08 -05001389 dprintk("RPC: %5u %s: retry garbled creds\n",
1390 task->tk_pid, __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 task->tk_action = call_bind;
Trond Myklebustabbcf282006-01-03 09:55:03 +01001392 goto out_retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 case RPC_AUTH_TOOWEAK:
Levent Serinol1356b8c2006-03-20 13:44:11 -05001394 printk(KERN_NOTICE "call_verify: server %s requires stronger "
1395 "authentication.\n", task->tk_client->cl_server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 break;
1397 default:
Trond Myklebust8a702bb2007-06-27 18:30:26 -04001398 dprintk("RPC: %5u %s: unknown auth error: %x\n",
1399 task->tk_pid, __FUNCTION__, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 error = -EIO;
1401 }
Chuck Lever46121cf2007-01-31 12:14:08 -05001402 dprintk("RPC: %5u %s: call rejected %d\n",
1403 task->tk_pid, __FUNCTION__, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 goto out_err;
1405 }
1406 if (!(p = rpcauth_checkverf(task, p))) {
Trond Myklebust8a702bb2007-06-27 18:30:26 -04001407 dprintk("RPC: %5u %s: auth check failed\n",
1408 task->tk_pid, __FUNCTION__);
Trond Myklebustabbcf282006-01-03 09:55:03 +01001409 goto out_garbage; /* bad verifier, retry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 }
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001411 len = p - (__be32 *)iov->iov_base - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 if (len < 0)
1413 goto out_overflow;
1414 switch ((n = ntohl(*p++))) {
1415 case RPC_SUCCESS:
1416 return p;
1417 case RPC_PROG_UNAVAIL:
Chuck Lever46121cf2007-01-31 12:14:08 -05001418 dprintk("RPC: %5u %s: program %u is unsupported by server %s\n",
1419 task->tk_pid, __FUNCTION__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 (unsigned int)task->tk_client->cl_prog,
1421 task->tk_client->cl_server);
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00001422 error = -EPFNOSUPPORT;
1423 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 case RPC_PROG_MISMATCH:
Chuck Lever46121cf2007-01-31 12:14:08 -05001425 dprintk("RPC: %5u %s: program %u, version %u unsupported by "
1426 "server %s\n", task->tk_pid, __FUNCTION__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 (unsigned int)task->tk_client->cl_prog,
1428 (unsigned int)task->tk_client->cl_vers,
1429 task->tk_client->cl_server);
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00001430 error = -EPROTONOSUPPORT;
1431 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 case RPC_PROC_UNAVAIL:
Chuck Lever46121cf2007-01-31 12:14:08 -05001433 dprintk("RPC: %5u %s: proc %p unsupported by program %u, "
1434 "version %u on server %s\n",
1435 task->tk_pid, __FUNCTION__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 task->tk_msg.rpc_proc,
1437 task->tk_client->cl_prog,
1438 task->tk_client->cl_vers,
1439 task->tk_client->cl_server);
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00001440 error = -EOPNOTSUPP;
1441 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 case RPC_GARBAGE_ARGS:
Chuck Lever46121cf2007-01-31 12:14:08 -05001443 dprintk("RPC: %5u %s: server saw garbage\n",
1444 task->tk_pid, __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 break; /* retry */
1446 default:
Trond Myklebust8a702bb2007-06-27 18:30:26 -04001447 dprintk("RPC: %5u %s: server accept status: %x\n",
1448 task->tk_pid, __FUNCTION__, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 /* Also retry */
1450 }
1451
Trond Myklebustabbcf282006-01-03 09:55:03 +01001452out_garbage:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 task->tk_client->cl_stats->rpcgarbage++;
1454 if (task->tk_garb_retry) {
1455 task->tk_garb_retry--;
Chuck Lever46121cf2007-01-31 12:14:08 -05001456 dprintk("RPC: %5u %s: retrying\n",
1457 task->tk_pid, __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 task->tk_action = call_bind;
Trond Myklebustabbcf282006-01-03 09:55:03 +01001459out_retry:
1460 return ERR_PTR(-EAGAIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462out_eio:
1463 error = -EIO;
1464out_err:
1465 rpc_exit(task, error);
Trond Myklebust8a702bb2007-06-27 18:30:26 -04001466 dprintk("RPC: %5u %s: call failed with error %d\n", task->tk_pid,
1467 __FUNCTION__, error);
Trond Myklebustabbcf282006-01-03 09:55:03 +01001468 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469out_overflow:
Trond Myklebust8a702bb2007-06-27 18:30:26 -04001470 dprintk("RPC: %5u %s: server reply was truncated.\n", task->tk_pid,
1471 __FUNCTION__);
Trond Myklebustabbcf282006-01-03 09:55:03 +01001472 goto out_garbage;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473}
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00001474
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001475static int rpcproc_encode_null(void *rqstp, __be32 *data, void *obj)
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00001476{
1477 return 0;
1478}
1479
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001480static int rpcproc_decode_null(void *rqstp, __be32 *data, void *obj)
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00001481{
1482 return 0;
1483}
1484
1485static struct rpc_procinfo rpcproc_null = {
1486 .p_encode = rpcproc_encode_null,
1487 .p_decode = rpcproc_decode_null,
1488};
1489
Trond Myklebust64c91a12007-06-23 10:17:16 -04001490static int rpc_ping(struct rpc_clnt *clnt, int flags)
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00001491{
1492 struct rpc_message msg = {
1493 .rpc_proc = &rpcproc_null,
1494 };
1495 int err;
1496 msg.rpc_cred = authnull_ops.lookup_cred(NULL, NULL, 0);
1497 err = rpc_call_sync(clnt, &msg, flags);
1498 put_rpccred(msg.rpc_cred);
1499 return err;
1500}
Trond Myklebust188fef12007-06-16 14:18:40 -04001501
Trond Myklebust5e1550d2007-06-23 10:17:16 -04001502struct rpc_task *rpc_call_null(struct rpc_clnt *clnt, struct rpc_cred *cred, int flags)
1503{
1504 struct rpc_message msg = {
1505 .rpc_proc = &rpcproc_null,
1506 .rpc_cred = cred,
1507 };
1508 return rpc_do_run_task(clnt, &msg, flags, &rpc_default_ops, NULL);
1509}
1510EXPORT_SYMBOL(rpc_call_null);
1511
Trond Myklebust188fef12007-06-16 14:18:40 -04001512#ifdef RPC_DEBUG
1513void rpc_show_tasks(void)
1514{
1515 struct rpc_clnt *clnt;
1516 struct rpc_task *t;
1517
1518 spin_lock(&rpc_client_lock);
1519 if (list_empty(&all_clients))
1520 goto out;
1521 printk("-pid- proc flgs status -client- -prog- --rqstp- -timeout "
1522 "-rpcwait -action- ---ops--\n");
1523 list_for_each_entry(clnt, &all_clients, cl_clients) {
1524 if (list_empty(&clnt->cl_tasks))
1525 continue;
1526 spin_lock(&clnt->cl_lock);
1527 list_for_each_entry(t, &clnt->cl_tasks, tk_task) {
1528 const char *rpc_waitq = "none";
1529
1530 if (RPC_IS_QUEUED(t))
1531 rpc_waitq = rpc_qname(t->u.tk_wait.rpc_waitq);
1532
1533 printk("%5u %04d %04x %6d %8p %6d %8p %8ld %8s %8p %8p\n",
1534 t->tk_pid,
1535 (t->tk_msg.rpc_proc ? t->tk_msg.rpc_proc->p_proc : -1),
1536 t->tk_flags, t->tk_status,
1537 t->tk_client,
1538 (t->tk_client ? t->tk_client->cl_prog : 0),
1539 t->tk_rqstp, t->tk_timeout,
1540 rpc_waitq,
1541 t->tk_action, t->tk_ops);
1542 }
1543 spin_unlock(&clnt->cl_lock);
1544 }
1545out:
1546 spin_unlock(&rpc_client_lock);
1547}
1548#endif