blob: c3d571abcded265d23d0079705d4cdfa96d9345c [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,
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +0200239 .srcaddr = args->saddress,
Frank van Maarseveen96802a02007-07-08 13:08:54 +0200240 .dstaddr = args->address,
241 .addrlen = args->addrsize,
242 .timeout = args->timeout
243 };
Chuck Lever43780b82007-07-01 12:13:22 -0400244 char servername[20];
Chuck Leverc2866762006-08-22 20:06:20 -0400245
Frank van Maarseveen96802a02007-07-08 13:08:54 +0200246 xprt = xprt_create_transport(&xprtargs);
Chuck Leverc2866762006-08-22 20:06:20 -0400247 if (IS_ERR(xprt))
248 return (struct rpc_clnt *)xprt;
249
250 /*
Chuck Lever43780b82007-07-01 12:13:22 -0400251 * If the caller chooses not to specify a hostname, whip
252 * up a string representation of the passed-in address.
253 */
254 if (args->servername == NULL) {
255 struct sockaddr_in *addr =
256 (struct sockaddr_in *) &args->address;
257 snprintf(servername, sizeof(servername), NIPQUAD_FMT,
258 NIPQUAD(addr->sin_addr.s_addr));
259 args->servername = servername;
260 }
261
262 /*
Chuck Leverc2866762006-08-22 20:06:20 -0400263 * By default, kernel RPC client connects from a reserved port.
264 * CAP_NET_BIND_SERVICE will not be set for unprivileged requesters,
265 * but it is always enabled for rpciod, which handles the connect
266 * operation.
267 */
268 xprt->resvport = 1;
269 if (args->flags & RPC_CLNT_CREATE_NONPRIVPORT)
270 xprt->resvport = 0;
271
Chuck Leverc2866762006-08-22 20:06:20 -0400272 clnt = rpc_new_client(xprt, args->servername, args->program,
273 args->version, args->authflavor);
274 if (IS_ERR(clnt))
275 return clnt;
276
277 if (!(args->flags & RPC_CLNT_CREATE_NOPING)) {
278 int err = rpc_ping(clnt, RPC_TASK_SOFT|RPC_TASK_NOINTR);
279 if (err != 0) {
280 rpc_shutdown_client(clnt);
281 return ERR_PTR(err);
282 }
283 }
284
285 clnt->cl_softrtry = 1;
286 if (args->flags & RPC_CLNT_CREATE_HARDRTRY)
287 clnt->cl_softrtry = 0;
288
289 if (args->flags & RPC_CLNT_CREATE_INTR)
290 clnt->cl_intr = 1;
291 if (args->flags & RPC_CLNT_CREATE_AUTOBIND)
292 clnt->cl_autobind = 1;
Chuck Lever43d78ef2007-02-06 18:26:11 -0500293 if (args->flags & RPC_CLNT_CREATE_DISCRTRY)
294 clnt->cl_discrtry = 1;
Chuck Leverc2866762006-08-22 20:06:20 -0400295
296 return clnt;
297}
Chuck Leverb86acd52006-08-22 20:06:22 -0400298EXPORT_SYMBOL_GPL(rpc_create);
Chuck Leverc2866762006-08-22 20:06:20 -0400299
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300/*
301 * This function clones the RPC client structure. It allows us to share the
302 * same transport while varying parameters such as the authentication
303 * flavour.
304 */
305struct rpc_clnt *
306rpc_clone_client(struct rpc_clnt *clnt)
307{
308 struct rpc_clnt *new;
Trond Myklebust3e32a5d2006-11-16 11:37:27 -0500309 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Arnaldo Carvalho de Meloe69062b2006-11-21 01:21:34 -0200311 new = kmemdup(clnt, sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 if (!new)
313 goto out_no_clnt;
Trond Myklebustd431a5552007-06-17 17:07:54 -0400314 new->cl_parent = clnt;
315 /* Turn off autobind on clones */
316 new->cl_autobind = 0;
317 INIT_LIST_HEAD(&new->cl_tasks);
318 spin_lock_init(&new->cl_lock);
319 rpc_init_rtt(&new->cl_rtt_default, clnt->cl_xprt->timeout.to_initval);
Trond Myklebust23bf85b2006-11-21 10:40:23 -0500320 new->cl_metrics = rpc_alloc_iostats(clnt);
321 if (new->cl_metrics == NULL)
322 goto out_no_stats;
Trond Myklebust34f52e32007-06-14 16:40:31 -0400323 kref_init(&new->cl_kref);
Trond Myklebust3e32a5d2006-11-16 11:37:27 -0500324 err = rpc_setup_pipedir(new, clnt->cl_program->pipe_dir_name);
325 if (err != 0)
326 goto out_no_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 if (new->cl_auth)
328 atomic_inc(&new->cl_auth->au_count);
Trond Myklebustd431a5552007-06-17 17:07:54 -0400329 xprt_get(clnt->cl_xprt);
330 kref_get(&clnt->cl_kref);
Trond Myklebust6529eba2007-06-14 16:40:14 -0400331 rpc_register_client(new);
Trond Myklebust4ada5392007-06-14 17:26:17 -0400332 rpciod_up();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 return new;
Trond Myklebust3e32a5d2006-11-16 11:37:27 -0500334out_no_path:
335 rpc_free_iostats(new->cl_metrics);
Trond Myklebust23bf85b2006-11-21 10:40:23 -0500336out_no_stats:
337 kfree(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338out_no_clnt:
Chuck Lever46121cf2007-01-31 12:14:08 -0500339 dprintk("RPC: %s: returned error %d\n", __FUNCTION__, err);
Trond Myklebust3e32a5d2006-11-16 11:37:27 -0500340 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341}
342
343/*
344 * Properly shut down an RPC client, terminating all outstanding
Trond Myklebust90c57552007-06-09 19:49:36 -0400345 * requests.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 */
Trond Myklebust4c402b42007-06-14 16:40:32 -0400347void rpc_shutdown_client(struct rpc_clnt *clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348{
Trond Myklebust34f52e32007-06-14 16:40:31 -0400349 dprintk("RPC: shutting down %s client for %s\n",
350 clnt->cl_protname, clnt->cl_server);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
Trond Myklebust34f52e32007-06-14 16:40:31 -0400352 while (!list_empty(&clnt->cl_tasks)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 rpc_killall_tasks(clnt);
Ingo Molnar532347e2006-01-09 20:52:53 -0800354 wait_event_timeout(destroy_wait,
Trond Myklebust34f52e32007-06-14 16:40:31 -0400355 list_empty(&clnt->cl_tasks), 1*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 }
357
Trond Myklebust4c402b42007-06-14 16:40:32 -0400358 rpc_release_client(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359}
360
361/*
Trond Myklebust34f52e32007-06-14 16:40:31 -0400362 * Free an RPC client
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 */
Trond Myklebust34f52e32007-06-14 16:40:31 -0400364static void
365rpc_free_client(struct kref *kref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366{
Trond Myklebust34f52e32007-06-14 16:40:31 -0400367 struct rpc_clnt *clnt = container_of(kref, struct rpc_clnt, cl_kref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
Chuck Lever46121cf2007-01-31 12:14:08 -0500369 dprintk("RPC: destroying %s client for %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 clnt->cl_protname, clnt->cl_server);
Trond Myklebust8f8e7a52006-08-14 13:11:15 -0400371 if (!IS_ERR(clnt->cl_dentry)) {
Trond Myklebustdff02cc2006-07-31 14:17:18 -0700372 rpc_rmdir(clnt->cl_dentry);
Trond Myklebust8f8e7a52006-08-14 13:11:15 -0400373 rpc_put_mount();
374 }
Trond Myklebust3e32a5d2006-11-16 11:37:27 -0500375 if (clnt->cl_parent != clnt) {
Trond Myklebust8ad7c892007-06-14 16:40:32 -0400376 rpc_release_client(clnt->cl_parent);
Trond Myklebust3e32a5d2006-11-16 11:37:27 -0500377 goto out_free;
378 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 if (clnt->cl_server != clnt->cl_inline_name)
380 kfree(clnt->cl_server);
381out_free:
Trond Myklebust6529eba2007-06-14 16:40:14 -0400382 rpc_unregister_client(clnt);
Chuck Lever11c556b2006-03-20 13:44:22 -0500383 rpc_free_iostats(clnt->cl_metrics);
384 clnt->cl_metrics = NULL;
Trond Myklebust6b6ca862006-09-05 12:55:57 -0400385 xprt_put(clnt->cl_xprt);
Trond Myklebust4ada5392007-06-14 17:26:17 -0400386 rpciod_down();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 kfree(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388}
389
390/*
Trond Myklebust1dd17ec2007-06-26 16:57:41 -0400391 * Free an RPC client
392 */
393static void
394rpc_free_auth(struct kref *kref)
395{
396 struct rpc_clnt *clnt = container_of(kref, struct rpc_clnt, cl_kref);
397
398 if (clnt->cl_auth == NULL) {
399 rpc_free_client(kref);
400 return;
401 }
402
403 /*
404 * Note: RPCSEC_GSS may need to send NULL RPC calls in order to
405 * release remaining GSS contexts. This mechanism ensures
406 * that it can do so safely.
407 */
408 kref_init(kref);
409 rpcauth_release(clnt->cl_auth);
410 clnt->cl_auth = NULL;
411 kref_put(kref, rpc_free_client);
412}
413
414/*
Trond Myklebust34f52e32007-06-14 16:40:31 -0400415 * Release reference to the RPC client
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 */
417void
418rpc_release_client(struct rpc_clnt *clnt)
419{
Trond Myklebust34f52e32007-06-14 16:40:31 -0400420 dprintk("RPC: rpc_release_client(%p)\n", clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
Trond Myklebust34f52e32007-06-14 16:40:31 -0400422 if (list_empty(&clnt->cl_tasks))
423 wake_up(&destroy_wait);
Trond Myklebust1dd17ec2007-06-26 16:57:41 -0400424 kref_put(&clnt->cl_kref, rpc_free_auth);
Trond Myklebust34f52e32007-06-14 16:40:31 -0400425}
426
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000427/**
428 * rpc_bind_new_program - bind a new RPC program to an existing client
429 * @old - old rpc_client
430 * @program - rpc program to set
431 * @vers - rpc program version
432 *
433 * Clones the rpc client and sets up a new RPC program. This is mainly
434 * of use for enabling different RPC programs to share the same transport.
435 * The Sun NFSv2/v3 ACL protocol can do this.
436 */
437struct rpc_clnt *rpc_bind_new_program(struct rpc_clnt *old,
438 struct rpc_program *program,
Chuck Lever89eb21c2007-09-11 18:00:09 -0400439 u32 vers)
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000440{
441 struct rpc_clnt *clnt;
442 struct rpc_version *version;
443 int err;
444
445 BUG_ON(vers >= program->nrvers || !program->version[vers]);
446 version = program->version[vers];
447 clnt = rpc_clone_client(old);
448 if (IS_ERR(clnt))
449 goto out;
450 clnt->cl_procinfo = version->procs;
451 clnt->cl_maxproc = version->nrprocs;
452 clnt->cl_protname = program->name;
453 clnt->cl_prog = program->number;
454 clnt->cl_vers = version->number;
455 clnt->cl_stats = program->stats;
456 err = rpc_ping(clnt, RPC_TASK_SOFT|RPC_TASK_NOINTR);
457 if (err != 0) {
458 rpc_shutdown_client(clnt);
459 clnt = ERR_PTR(err);
460 }
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800461out:
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000462 return clnt;
463}
464
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465/*
466 * Default callback for async RPC calls
467 */
468static void
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100469rpc_default_callback(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470{
471}
472
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100473static const struct rpc_call_ops rpc_default_ops = {
474 .rpc_call_done = rpc_default_callback,
475};
476
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477/*
Trond Myklebust14b218a2005-06-22 17:16:28 +0000478 * Export the signal mask handling for synchronous code that
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 * sleeps on RPC calls
480 */
Trond Myklebust2bd61572006-01-03 09:55:19 +0100481#define RPC_INTR_SIGNALS (sigmask(SIGHUP) | sigmask(SIGINT) | sigmask(SIGQUIT) | sigmask(SIGTERM))
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800482
Trond Myklebust14b218a2005-06-22 17:16:28 +0000483static void rpc_save_sigmask(sigset_t *oldset, int intr)
484{
Trond Myklebust2bd61572006-01-03 09:55:19 +0100485 unsigned long sigallow = sigmask(SIGKILL);
Trond Myklebust14b218a2005-06-22 17:16:28 +0000486 sigset_t sigmask;
487
488 /* Block all signals except those listed in sigallow */
489 if (intr)
490 sigallow |= RPC_INTR_SIGNALS;
491 siginitsetinv(&sigmask, sigallow);
492 sigprocmask(SIG_BLOCK, &sigmask, oldset);
493}
494
495static inline void rpc_task_sigmask(struct rpc_task *task, sigset_t *oldset)
496{
497 rpc_save_sigmask(oldset, !RPC_TASK_UNINTERRUPTIBLE(task));
498}
499
500static inline void rpc_restore_sigmask(sigset_t *oldset)
501{
502 sigprocmask(SIG_SETMASK, oldset, NULL);
503}
504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505void rpc_clnt_sigmask(struct rpc_clnt *clnt, sigset_t *oldset)
506{
Trond Myklebust14b218a2005-06-22 17:16:28 +0000507 rpc_save_sigmask(oldset, clnt->cl_intr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508}
509
510void rpc_clnt_sigunmask(struct rpc_clnt *clnt, sigset_t *oldset)
511{
Trond Myklebust14b218a2005-06-22 17:16:28 +0000512 rpc_restore_sigmask(oldset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513}
514
Trond Myklebust6e5b70e2007-06-12 10:02:37 -0400515static
516struct rpc_task *rpc_do_run_task(struct rpc_clnt *clnt,
517 struct rpc_message *msg,
518 int flags,
519 const struct rpc_call_ops *ops,
520 void *data)
521{
522 struct rpc_task *task, *ret;
523 sigset_t oldset;
524
525 task = rpc_new_task(clnt, flags, ops, data);
526 if (task == NULL) {
527 rpc_release_calldata(ops, data);
528 return ERR_PTR(-ENOMEM);
529 }
530
531 /* Mask signals on synchronous RPC calls and RPCSEC_GSS upcalls */
532 rpc_task_sigmask(task, &oldset);
533 if (msg != NULL) {
534 rpc_call_setup(task, msg, 0);
535 if (task->tk_status != 0) {
536 ret = ERR_PTR(task->tk_status);
537 rpc_put_task(task);
538 goto out;
539 }
540 }
541 atomic_inc(&task->tk_count);
542 rpc_execute(task);
543 ret = task;
544out:
545 rpc_restore_sigmask(&oldset);
546 return ret;
547}
548
549/**
550 * rpc_call_sync - Perform a synchronous RPC call
551 * @clnt: pointer to RPC client
552 * @msg: RPC call parameters
553 * @flags: RPC call flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 */
555int rpc_call_sync(struct rpc_clnt *clnt, struct rpc_message *msg, int flags)
556{
557 struct rpc_task *task;
Trond Myklebust6e5b70e2007-06-12 10:02:37 -0400558 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 BUG_ON(flags & RPC_TASK_ASYNC);
561
Trond Myklebust6e5b70e2007-06-12 10:02:37 -0400562 task = rpc_do_run_task(clnt, msg, flags, &rpc_default_ops, NULL);
563 if (IS_ERR(task))
564 return PTR_ERR(task);
Trond Myklebuste60859a2006-01-03 09:55:10 +0100565 status = task->tk_status;
Trond Myklebustbde8f002007-01-24 11:54:53 -0800566 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 return status;
568}
569
Trond Myklebust6e5b70e2007-06-12 10:02:37 -0400570/**
571 * rpc_call_async - Perform an asynchronous RPC call
572 * @clnt: pointer to RPC client
573 * @msg: RPC call parameters
574 * @flags: RPC call flags
575 * @ops: RPC call ops
576 * @data: user call data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 */
578int
579rpc_call_async(struct rpc_clnt *clnt, struct rpc_message *msg, int flags,
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100580 const struct rpc_call_ops *tk_ops, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
582 struct rpc_task *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
Trond Myklebust6e5b70e2007-06-12 10:02:37 -0400584 task = rpc_do_run_task(clnt, msg, flags|RPC_TASK_ASYNC, tk_ops, data);
585 if (IS_ERR(task))
586 return PTR_ERR(task);
587 rpc_put_task(task);
588 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589}
590
Trond Myklebust6e5b70e2007-06-12 10:02:37 -0400591/**
592 * rpc_run_task - Allocate a new RPC task, then run rpc_execute against it
593 * @clnt: pointer to RPC client
594 * @flags: RPC flags
595 * @ops: RPC call ops
596 * @data: user call data
597 */
598struct rpc_task *rpc_run_task(struct rpc_clnt *clnt, int flags,
599 const struct rpc_call_ops *tk_ops,
600 void *data)
601{
602 return rpc_do_run_task(clnt, NULL, flags, tk_ops, data);
603}
604EXPORT_SYMBOL(rpc_run_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
606void
607rpc_call_setup(struct rpc_task *task, struct rpc_message *msg, int flags)
608{
609 task->tk_msg = *msg;
610 task->tk_flags |= flags;
611 /* Bind the user cred */
612 if (task->tk_msg.rpc_cred != NULL)
613 rpcauth_holdcred(task);
614 else
615 rpcauth_bindcred(task);
616
617 if (task->tk_status == 0)
618 task->tk_action = call_start;
619 else
Trond Myklebustabbcf282006-01-03 09:55:03 +0100620 task->tk_action = rpc_exit_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621}
622
Chuck Levered394402006-08-22 20:06:17 -0400623/**
624 * rpc_peeraddr - extract remote peer address from clnt's xprt
625 * @clnt: RPC client structure
626 * @buf: target buffer
627 * @size: length of target buffer
628 *
629 * Returns the number of bytes that are actually in the stored address.
630 */
631size_t rpc_peeraddr(struct rpc_clnt *clnt, struct sockaddr *buf, size_t bufsize)
632{
633 size_t bytes;
634 struct rpc_xprt *xprt = clnt->cl_xprt;
635
636 bytes = sizeof(xprt->addr);
637 if (bytes > bufsize)
638 bytes = bufsize;
639 memcpy(buf, &clnt->cl_xprt->addr, bytes);
Chuck Leverc4efcb12006-08-22 20:06:19 -0400640 return xprt->addrlen;
Chuck Levered394402006-08-22 20:06:17 -0400641}
Chuck Leverb86acd52006-08-22 20:06:22 -0400642EXPORT_SYMBOL_GPL(rpc_peeraddr);
Chuck Levered394402006-08-22 20:06:17 -0400643
Chuck Leverf425eba2006-08-22 20:06:18 -0400644/**
645 * rpc_peeraddr2str - return remote peer address in printable format
646 * @clnt: RPC client structure
647 * @format: address format
648 *
649 */
650char *rpc_peeraddr2str(struct rpc_clnt *clnt, enum rpc_display_format_t format)
651{
652 struct rpc_xprt *xprt = clnt->cl_xprt;
Chuck Lever7559c7a2006-12-05 16:35:37 -0500653
654 if (xprt->address_strings[format] != NULL)
655 return xprt->address_strings[format];
656 else
657 return "unprintable";
Chuck Leverf425eba2006-08-22 20:06:18 -0400658}
Chuck Leverb86acd52006-08-22 20:06:22 -0400659EXPORT_SYMBOL_GPL(rpc_peeraddr2str);
Chuck Leverf425eba2006-08-22 20:06:18 -0400660
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661void
662rpc_setbufsize(struct rpc_clnt *clnt, unsigned int sndsize, unsigned int rcvsize)
663{
664 struct rpc_xprt *xprt = clnt->cl_xprt;
Chuck Lever470056c22005-08-25 16:25:56 -0700665 if (xprt->ops->set_buffer_size)
666 xprt->ops->set_buffer_size(xprt, sndsize, rcvsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667}
668
669/*
670 * Return size of largest payload RPC client can support, in bytes
671 *
672 * For stream transports, this is one RPC record fragment (see RFC
673 * 1831), as we don't support multi-record requests yet. For datagram
674 * transports, this is the size of an IP packet minus the IP, UDP, and
675 * RPC header sizes.
676 */
677size_t rpc_max_payload(struct rpc_clnt *clnt)
678{
679 return clnt->cl_xprt->max_payload;
680}
Chuck Leverb86acd52006-08-22 20:06:22 -0400681EXPORT_SYMBOL_GPL(rpc_max_payload);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
Chuck Lever35f5a422006-01-03 09:55:50 +0100683/**
684 * rpc_force_rebind - force transport to check that remote port is unchanged
685 * @clnt: client to rebind
686 *
687 */
688void rpc_force_rebind(struct rpc_clnt *clnt)
689{
690 if (clnt->cl_autobind)
Chuck Leverec739ef2006-08-22 20:06:15 -0400691 xprt_clear_bound(clnt->cl_xprt);
Chuck Lever35f5a422006-01-03 09:55:50 +0100692}
Chuck Leverb86acd52006-08-22 20:06:22 -0400693EXPORT_SYMBOL_GPL(rpc_force_rebind);
Chuck Lever35f5a422006-01-03 09:55:50 +0100694
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695/*
696 * Restart an (async) RPC call. Usually called from within the
697 * exit handler.
698 */
699void
700rpc_restart_call(struct rpc_task *task)
701{
702 if (RPC_ASSASSINATED(task))
703 return;
704
705 task->tk_action = call_start;
706}
707
708/*
709 * 0. Initial state
710 *
711 * Other FSM states can be visited zero or more times, but
712 * this state is visited exactly once for each RPC.
713 */
714static void
715call_start(struct rpc_task *task)
716{
717 struct rpc_clnt *clnt = task->tk_client;
718
Chuck Lever46121cf2007-01-31 12:14:08 -0500719 dprintk("RPC: %5u call_start %s%d proc %d (%s)\n", task->tk_pid,
720 clnt->cl_protname, clnt->cl_vers,
721 task->tk_msg.rpc_proc->p_proc,
722 (RPC_IS_ASYNC(task) ? "async" : "sync"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
724 /* Increment call count */
725 task->tk_msg.rpc_proc->p_count++;
726 clnt->cl_stats->rpccnt++;
727 task->tk_action = call_reserve;
728}
729
730/*
731 * 1. Reserve an RPC call slot
732 */
733static void
734call_reserve(struct rpc_task *task)
735{
Chuck Lever46121cf2007-01-31 12:14:08 -0500736 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
738 if (!rpcauth_uptodatecred(task)) {
739 task->tk_action = call_refresh;
740 return;
741 }
742
743 task->tk_status = 0;
744 task->tk_action = call_reserveresult;
745 xprt_reserve(task);
746}
747
748/*
749 * 1b. Grok the result of xprt_reserve()
750 */
751static void
752call_reserveresult(struct rpc_task *task)
753{
754 int status = task->tk_status;
755
Chuck Lever46121cf2007-01-31 12:14:08 -0500756 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
758 /*
759 * After a call to xprt_reserve(), we must have either
760 * a request slot or else an error status.
761 */
762 task->tk_status = 0;
763 if (status >= 0) {
764 if (task->tk_rqstp) {
765 task->tk_action = call_allocate;
766 return;
767 }
768
769 printk(KERN_ERR "%s: status=%d, but no request slot, exiting\n",
770 __FUNCTION__, status);
771 rpc_exit(task, -EIO);
772 return;
773 }
774
775 /*
776 * Even though there was an error, we may have acquired
777 * a request slot somehow. Make sure not to leak it.
778 */
779 if (task->tk_rqstp) {
780 printk(KERN_ERR "%s: status=%d, request allocated anyway\n",
781 __FUNCTION__, status);
782 xprt_release(task);
783 }
784
785 switch (status) {
786 case -EAGAIN: /* woken up; retry */
787 task->tk_action = call_reserve;
788 return;
789 case -EIO: /* probably a shutdown */
790 break;
791 default:
792 printk(KERN_ERR "%s: unrecognized error %d, exiting\n",
793 __FUNCTION__, status);
794 break;
795 }
796 rpc_exit(task, status);
797}
798
799/*
800 * 2. Allocate the buffer. For details, see sched.c:rpc_malloc.
Chuck Lever02107142006-01-03 09:55:49 +0100801 * (Note: buffer memory is freed in xprt_release).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 */
803static void
804call_allocate(struct rpc_task *task)
805{
Trond Myklebust1be27f32007-06-27 14:29:04 -0400806 unsigned int slack = task->tk_msg.rpc_cred->cr_auth->au_cslack;
Chuck Lever02107142006-01-03 09:55:49 +0100807 struct rpc_rqst *req = task->tk_rqstp;
808 struct rpc_xprt *xprt = task->tk_xprt;
Chuck Lever2bea90d2007-03-29 16:47:53 -0400809 struct rpc_procinfo *proc = task->tk_msg.rpc_proc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
Chuck Lever46121cf2007-01-31 12:14:08 -0500811 dprint_status(task);
812
Chuck Lever2bea90d2007-03-29 16:47:53 -0400813 task->tk_status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 task->tk_action = call_bind;
Chuck Lever2bea90d2007-03-29 16:47:53 -0400815
Chuck Lever02107142006-01-03 09:55:49 +0100816 if (req->rq_buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 return;
818
Chuck Lever2bea90d2007-03-29 16:47:53 -0400819 if (proc->p_proc != 0) {
820 BUG_ON(proc->p_arglen == 0);
821 if (proc->p_decode != NULL)
822 BUG_ON(proc->p_replen == 0);
823 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
Chuck Lever2bea90d2007-03-29 16:47:53 -0400825 /*
826 * Calculate the size (in quads) of the RPC call
827 * and reply headers, and convert both values
828 * to byte sizes.
829 */
830 req->rq_callsize = RPC_CALLHDRSIZE + (slack << 1) + proc->p_arglen;
831 req->rq_callsize <<= 2;
832 req->rq_rcvsize = RPC_REPHDRSIZE + slack + proc->p_replen;
833 req->rq_rcvsize <<= 2;
834
Chuck Leverc5a4dd82007-03-29 16:47:58 -0400835 req->rq_buffer = xprt->ops->buf_alloc(task,
836 req->rq_callsize + req->rq_rcvsize);
Chuck Lever2bea90d2007-03-29 16:47:53 -0400837 if (req->rq_buffer != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 return;
Chuck Lever46121cf2007-01-31 12:14:08 -0500839
840 dprintk("RPC: %5u rpc_buffer allocation failed\n", task->tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
Trond Myklebust14b218a2005-06-22 17:16:28 +0000842 if (RPC_IS_ASYNC(task) || !signalled()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 xprt_release(task);
844 task->tk_action = call_reserve;
845 rpc_delay(task, HZ>>4);
846 return;
847 }
848
849 rpc_exit(task, -ERESTARTSYS);
850}
851
Trond Myklebust940e3312005-11-09 21:45:24 -0500852static inline int
853rpc_task_need_encode(struct rpc_task *task)
854{
855 return task->tk_rqstp->rq_snd_buf.len == 0;
856}
857
858static inline void
859rpc_task_force_reencode(struct rpc_task *task)
860{
861 task->tk_rqstp->rq_snd_buf.len = 0;
862}
863
Chuck Lever2bea90d2007-03-29 16:47:53 -0400864static inline void
865rpc_xdr_buf_init(struct xdr_buf *buf, void *start, size_t len)
866{
867 buf->head[0].iov_base = start;
868 buf->head[0].iov_len = len;
869 buf->tail[0].iov_len = 0;
870 buf->page_len = 0;
871 buf->len = 0;
872 buf->buflen = len;
873}
874
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875/*
876 * 3. Encode arguments of an RPC call
877 */
878static void
879call_encode(struct rpc_task *task)
880{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 struct rpc_rqst *req = task->tk_rqstp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 kxdrproc_t encode;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700883 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
Chuck Lever46121cf2007-01-31 12:14:08 -0500885 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
Chuck Lever2bea90d2007-03-29 16:47:53 -0400887 rpc_xdr_buf_init(&req->rq_snd_buf,
888 req->rq_buffer,
889 req->rq_callsize);
890 rpc_xdr_buf_init(&req->rq_rcv_buf,
891 (char *)req->rq_buffer + req->rq_callsize,
892 req->rq_rcvsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
894 /* Encode header and provided arguments */
895 encode = task->tk_msg.rpc_proc->p_encode;
896 if (!(p = call_header(task))) {
897 printk(KERN_INFO "RPC: call_header failed, exit EIO\n");
898 rpc_exit(task, -EIO);
899 return;
900 }
J. Bruce Fieldsf3680312005-10-13 16:54:48 -0400901 if (encode == NULL)
902 return;
903
904 task->tk_status = rpcauth_wrap_req(task, encode, req, p,
905 task->tk_msg.rpc_argp);
906 if (task->tk_status == -ENOMEM) {
907 /* XXX: Is this sane? */
908 rpc_delay(task, 3*HZ);
909 task->tk_status = -EAGAIN;
910 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911}
912
913/*
914 * 4. Get the server port number if not yet set
915 */
916static void
917call_bind(struct rpc_task *task)
918{
Chuck Leverec739ef2006-08-22 20:06:15 -0400919 struct rpc_xprt *xprt = task->tk_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
Chuck Lever46121cf2007-01-31 12:14:08 -0500921 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
Chuck Leverda351872005-08-11 16:25:11 -0400923 task->tk_action = call_connect;
Chuck Leverec739ef2006-08-22 20:06:15 -0400924 if (!xprt_bound(xprt)) {
Chuck Leverda351872005-08-11 16:25:11 -0400925 task->tk_action = call_bind_status;
Chuck Leverec739ef2006-08-22 20:06:15 -0400926 task->tk_timeout = xprt->bind_timeout;
Chuck Leverbbf7c1d2006-08-22 20:06:16 -0400927 xprt->ops->rpcbind(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 }
929}
930
931/*
Chuck Leverda351872005-08-11 16:25:11 -0400932 * 4a. Sort out bind result
933 */
934static void
935call_bind_status(struct rpc_task *task)
936{
937 int status = -EACCES;
938
939 if (task->tk_status >= 0) {
Chuck Lever46121cf2007-01-31 12:14:08 -0500940 dprint_status(task);
Chuck Leverda351872005-08-11 16:25:11 -0400941 task->tk_status = 0;
942 task->tk_action = call_connect;
943 return;
944 }
945
946 switch (task->tk_status) {
947 case -EACCES:
Chuck Lever46121cf2007-01-31 12:14:08 -0500948 dprintk("RPC: %5u remote rpcbind: RPC program/version "
949 "unavailable\n", task->tk_pid);
Chuck Leverea635a52005-10-06 23:12:58 -0400950 rpc_delay(task, 3*HZ);
Trond Myklebustda458282006-08-31 15:44:52 -0400951 goto retry_timeout;
Chuck Leverda351872005-08-11 16:25:11 -0400952 case -ETIMEDOUT:
Chuck Lever46121cf2007-01-31 12:14:08 -0500953 dprintk("RPC: %5u rpcbind request timed out\n",
Chuck Leverda351872005-08-11 16:25:11 -0400954 task->tk_pid);
Trond Myklebustda458282006-08-31 15:44:52 -0400955 goto retry_timeout;
Chuck Leverda351872005-08-11 16:25:11 -0400956 case -EPFNOSUPPORT:
Chuck Lever46121cf2007-01-31 12:14:08 -0500957 dprintk("RPC: %5u remote rpcbind service unavailable\n",
Chuck Leverda351872005-08-11 16:25:11 -0400958 task->tk_pid);
959 break;
960 case -EPROTONOSUPPORT:
Chuck Lever00a6e7b2007-03-29 16:48:33 -0400961 dprintk("RPC: %5u remote rpcbind version unavailable, retrying\n",
Chuck Leverda351872005-08-11 16:25:11 -0400962 task->tk_pid);
Chuck Lever00a6e7b2007-03-29 16:48:33 -0400963 task->tk_status = 0;
964 task->tk_action = call_bind;
965 return;
Chuck Leverda351872005-08-11 16:25:11 -0400966 default:
Chuck Lever46121cf2007-01-31 12:14:08 -0500967 dprintk("RPC: %5u unrecognized rpcbind error (%d)\n",
Chuck Leverda351872005-08-11 16:25:11 -0400968 task->tk_pid, -task->tk_status);
969 status = -EIO;
Chuck Leverda351872005-08-11 16:25:11 -0400970 }
971
972 rpc_exit(task, status);
973 return;
974
Trond Myklebustda458282006-08-31 15:44:52 -0400975retry_timeout:
976 task->tk_action = call_timeout;
Chuck Leverda351872005-08-11 16:25:11 -0400977}
978
979/*
980 * 4b. Connect to the RPC server
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 */
982static void
983call_connect(struct rpc_task *task)
984{
Chuck Leverda351872005-08-11 16:25:11 -0400985 struct rpc_xprt *xprt = task->tk_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
Chuck Lever46121cf2007-01-31 12:14:08 -0500987 dprintk("RPC: %5u call_connect xprt %p %s connected\n",
Chuck Leverda351872005-08-11 16:25:11 -0400988 task->tk_pid, xprt,
989 (xprt_connected(xprt) ? "is" : "is not"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
Chuck Leverda351872005-08-11 16:25:11 -0400991 task->tk_action = call_transmit;
992 if (!xprt_connected(xprt)) {
993 task->tk_action = call_connect_status;
994 if (task->tk_status < 0)
995 return;
996 xprt_connect(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998}
999
1000/*
Chuck Leverda351872005-08-11 16:25:11 -04001001 * 4c. Sort out connect result
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 */
1003static void
1004call_connect_status(struct rpc_task *task)
1005{
1006 struct rpc_clnt *clnt = task->tk_client;
1007 int status = task->tk_status;
1008
Chuck Lever46121cf2007-01-31 12:14:08 -05001009 dprint_status(task);
Chuck Leverda351872005-08-11 16:25:11 -04001010
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 task->tk_status = 0;
1012 if (status >= 0) {
1013 clnt->cl_stats->netreconn++;
1014 task->tk_action = call_transmit;
1015 return;
1016 }
1017
Chuck Leverda351872005-08-11 16:25:11 -04001018 /* Something failed: remote service port may have changed */
Chuck Lever35f5a422006-01-03 09:55:50 +01001019 rpc_force_rebind(clnt);
Chuck Leverda351872005-08-11 16:25:11 -04001020
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 switch (status) {
1022 case -ENOTCONN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 case -EAGAIN:
Chuck Leverda351872005-08-11 16:25:11 -04001024 task->tk_action = call_bind;
Trond Myklebustda458282006-08-31 15:44:52 -04001025 if (!RPC_IS_SOFT(task))
1026 return;
1027 /* if soft mounted, test if we've timed out */
1028 case -ETIMEDOUT:
1029 task->tk_action = call_timeout;
1030 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 }
Trond Myklebustda458282006-08-31 15:44:52 -04001032 rpc_exit(task, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033}
1034
1035/*
1036 * 5. Transmit the RPC request, and wait for reply
1037 */
1038static void
1039call_transmit(struct rpc_task *task)
1040{
Chuck Lever46121cf2007-01-31 12:14:08 -05001041 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
1043 task->tk_action = call_status;
1044 if (task->tk_status < 0)
1045 return;
1046 task->tk_status = xprt_prepare_transmit(task);
1047 if (task->tk_status != 0)
1048 return;
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04001049 task->tk_action = call_transmit_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 /* Encode here so that rpcsec_gss can use correct sequence number. */
Trond Myklebust940e3312005-11-09 21:45:24 -05001051 if (rpc_task_need_encode(task)) {
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04001052 BUG_ON(task->tk_rqstp->rq_bytes_sent != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 call_encode(task);
Trond Myklebust5e5ce5b2005-10-18 14:20:11 -07001054 /* Did the encode result in an error condition? */
1055 if (task->tk_status != 0)
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04001056 return;
Trond Myklebust5e5ce5b2005-10-18 14:20:11 -07001057 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 xprt_transmit(task);
1059 if (task->tk_status < 0)
1060 return;
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04001061 /*
1062 * On success, ensure that we call xprt_end_transmit() before sleeping
1063 * in order to allow access to the socket to other RPC requests.
1064 */
1065 call_transmit_status(task);
1066 if (task->tk_msg.rpc_proc->p_decode != NULL)
1067 return;
1068 task->tk_action = rpc_exit_task;
1069 rpc_wake_up_task(task);
1070}
1071
1072/*
1073 * 5a. Handle cleanup after a transmission
1074 */
1075static void
1076call_transmit_status(struct rpc_task *task)
1077{
1078 task->tk_action = call_status;
1079 /*
1080 * Special case: if we've been waiting on the socket's write_space()
1081 * callback, then don't call xprt_end_transmit().
1082 */
1083 if (task->tk_status == -EAGAIN)
1084 return;
1085 xprt_end_transmit(task);
Trond Myklebust940e3312005-11-09 21:45:24 -05001086 rpc_task_force_reencode(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087}
1088
1089/*
1090 * 6. Sort out the RPC call status
1091 */
1092static void
1093call_status(struct rpc_task *task)
1094{
1095 struct rpc_clnt *clnt = task->tk_client;
1096 struct rpc_rqst *req = task->tk_rqstp;
1097 int status;
1098
1099 if (req->rq_received > 0 && !req->rq_bytes_sent)
1100 task->tk_status = req->rq_received;
1101
Chuck Lever46121cf2007-01-31 12:14:08 -05001102 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
1104 status = task->tk_status;
1105 if (status >= 0) {
1106 task->tk_action = call_decode;
1107 return;
1108 }
1109
1110 task->tk_status = 0;
1111 switch(status) {
Trond Myklebust76303992006-08-30 14:32:49 -04001112 case -EHOSTDOWN:
1113 case -EHOSTUNREACH:
1114 case -ENETUNREACH:
1115 /*
1116 * Delay any retries for 3 seconds, then handle as if it
1117 * were a timeout.
1118 */
1119 rpc_delay(task, 3*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 case -ETIMEDOUT:
1121 task->tk_action = call_timeout;
Trond Myklebust241c39b2007-04-20 16:12:55 -04001122 if (task->tk_client->cl_discrtry)
1123 xprt_disconnect(task->tk_xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 break;
1125 case -ECONNREFUSED:
1126 case -ENOTCONN:
Chuck Lever35f5a422006-01-03 09:55:50 +01001127 rpc_force_rebind(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 task->tk_action = call_bind;
1129 break;
1130 case -EAGAIN:
1131 task->tk_action = call_transmit;
1132 break;
1133 case -EIO:
1134 /* shutdown or soft timeout */
1135 rpc_exit(task, status);
1136 break;
1137 default:
Chuck Leverf518e352006-01-03 09:55:52 +01001138 printk("%s: RPC call returned error %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 clnt->cl_protname, -status);
1140 rpc_exit(task, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 }
1142}
1143
1144/*
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04001145 * 6a. Handle RPC timeout
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 * We do not release the request slot, so we keep using the
1147 * same XID for all retransmits.
1148 */
1149static void
1150call_timeout(struct rpc_task *task)
1151{
1152 struct rpc_clnt *clnt = task->tk_client;
1153
1154 if (xprt_adjust_timeout(task->tk_rqstp) == 0) {
Chuck Lever46121cf2007-01-31 12:14:08 -05001155 dprintk("RPC: %5u call_timeout (minor)\n", task->tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 goto retry;
1157 }
1158
Chuck Lever46121cf2007-01-31 12:14:08 -05001159 dprintk("RPC: %5u call_timeout (major)\n", task->tk_pid);
Chuck Leveref759a22006-03-20 13:44:17 -05001160 task->tk_timeouts++;
1161
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 if (RPC_IS_SOFT(task)) {
Chuck Leverf518e352006-01-03 09:55:52 +01001163 printk(KERN_NOTICE "%s: server %s not responding, timed out\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 clnt->cl_protname, clnt->cl_server);
1165 rpc_exit(task, -EIO);
1166 return;
1167 }
1168
Chuck Leverf518e352006-01-03 09:55:52 +01001169 if (!(task->tk_flags & RPC_CALL_MAJORSEEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 task->tk_flags |= RPC_CALL_MAJORSEEN;
1171 printk(KERN_NOTICE "%s: server %s not responding, still trying\n",
1172 clnt->cl_protname, clnt->cl_server);
1173 }
Chuck Lever35f5a422006-01-03 09:55:50 +01001174 rpc_force_rebind(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
1176retry:
1177 clnt->cl_stats->rpcretrans++;
1178 task->tk_action = call_bind;
1179 task->tk_status = 0;
1180}
1181
1182/*
1183 * 7. Decode the RPC reply
1184 */
1185static void
1186call_decode(struct rpc_task *task)
1187{
1188 struct rpc_clnt *clnt = task->tk_client;
1189 struct rpc_rqst *req = task->tk_rqstp;
1190 kxdrproc_t decode = task->tk_msg.rpc_proc->p_decode;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001191 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
Chuck Lever46121cf2007-01-31 12:14:08 -05001193 dprintk("RPC: %5u call_decode (status %d)\n",
1194 task->tk_pid, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
Chuck Leverf518e352006-01-03 09:55:52 +01001196 if (task->tk_flags & RPC_CALL_MAJORSEEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 printk(KERN_NOTICE "%s: server %s OK\n",
1198 clnt->cl_protname, clnt->cl_server);
1199 task->tk_flags &= ~RPC_CALL_MAJORSEEN;
1200 }
1201
1202 if (task->tk_status < 12) {
1203 if (!RPC_IS_SOFT(task)) {
1204 task->tk_action = call_bind;
1205 clnt->cl_stats->rpcretrans++;
1206 goto out_retry;
1207 }
Chuck Lever46121cf2007-01-31 12:14:08 -05001208 dprintk("RPC: %s: too small RPC reply size (%d bytes)\n",
1209 clnt->cl_protname, task->tk_status);
Trond Myklebustda458282006-08-31 15:44:52 -04001210 task->tk_action = call_timeout;
1211 goto out_retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 }
1213
Trond Myklebust43ac3f22006-03-20 13:44:51 -05001214 /*
1215 * Ensure that we see all writes made by xprt_complete_rqst()
1216 * before it changed req->rq_received.
1217 */
1218 smp_rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 req->rq_rcv_buf.len = req->rq_private_buf.len;
1220
1221 /* Check that the softirq receive buffer is valid */
1222 WARN_ON(memcmp(&req->rq_rcv_buf, &req->rq_private_buf,
1223 sizeof(req->rq_rcv_buf)) != 0);
1224
1225 /* Verify the RPC header */
Trond Myklebustabbcf282006-01-03 09:55:03 +01001226 p = call_verify(task);
1227 if (IS_ERR(p)) {
1228 if (p == ERR_PTR(-EAGAIN))
1229 goto out_retry;
1230 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 }
1232
Trond Myklebustabbcf282006-01-03 09:55:03 +01001233 task->tk_action = rpc_exit_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
Trond Myklebust6d5fcb52006-10-18 16:01:06 -04001235 if (decode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 task->tk_status = rpcauth_unwrap_resp(task, decode, req, p,
1237 task->tk_msg.rpc_resp);
Trond Myklebust6d5fcb52006-10-18 16:01:06 -04001238 }
Chuck Lever46121cf2007-01-31 12:14:08 -05001239 dprintk("RPC: %5u call_decode result %d\n", task->tk_pid,
1240 task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 return;
1242out_retry:
1243 req->rq_received = req->rq_private_buf.len = 0;
1244 task->tk_status = 0;
Trond Myklebust241c39b2007-04-20 16:12:55 -04001245 if (task->tk_client->cl_discrtry)
1246 xprt_disconnect(task->tk_xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247}
1248
1249/*
1250 * 8. Refresh the credentials if rejected by the server
1251 */
1252static void
1253call_refresh(struct rpc_task *task)
1254{
Chuck Lever46121cf2007-01-31 12:14:08 -05001255 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
1257 xprt_release(task); /* Must do to obtain new XID */
1258 task->tk_action = call_refreshresult;
1259 task->tk_status = 0;
1260 task->tk_client->cl_stats->rpcauthrefresh++;
1261 rpcauth_refreshcred(task);
1262}
1263
1264/*
1265 * 8a. Process the results of a credential refresh
1266 */
1267static void
1268call_refreshresult(struct rpc_task *task)
1269{
1270 int status = task->tk_status;
Chuck Lever46121cf2007-01-31 12:14:08 -05001271
1272 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
1274 task->tk_status = 0;
1275 task->tk_action = call_reserve;
1276 if (status >= 0 && rpcauth_uptodatecred(task))
1277 return;
1278 if (status == -EACCES) {
1279 rpc_exit(task, -EACCES);
1280 return;
1281 }
1282 task->tk_action = call_refresh;
1283 if (status != -ETIMEDOUT)
1284 rpc_delay(task, 3*HZ);
1285 return;
1286}
1287
1288/*
1289 * Call header serialization
1290 */
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001291static __be32 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292call_header(struct rpc_task *task)
1293{
1294 struct rpc_clnt *clnt = task->tk_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 struct rpc_rqst *req = task->tk_rqstp;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001296 __be32 *p = req->rq_svec[0].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
1298 /* FIXME: check buffer size? */
Chuck Lever808012f2005-08-25 16:25:49 -07001299
1300 p = xprt_skip_transport_header(task->tk_xprt, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 *p++ = req->rq_xid; /* XID */
1302 *p++ = htonl(RPC_CALL); /* CALL */
1303 *p++ = htonl(RPC_VERSION); /* RPC version */
1304 *p++ = htonl(clnt->cl_prog); /* program number */
1305 *p++ = htonl(clnt->cl_vers); /* program version */
1306 *p++ = htonl(task->tk_msg.rpc_proc->p_proc); /* procedure */
Trond Myklebust334ccfd2005-06-22 17:16:19 +00001307 p = rpcauth_marshcred(task, p);
1308 req->rq_slen = xdr_adjust_iovec(&req->rq_svec[0], p);
1309 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310}
1311
1312/*
1313 * Reply header verification
1314 */
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001315static __be32 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316call_verify(struct rpc_task *task)
1317{
1318 struct kvec *iov = &task->tk_rqstp->rq_rcv_buf.head[0];
1319 int len = task->tk_rqstp->rq_rcv_buf.len >> 2;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001320 __be32 *p = iov->iov_base;
1321 u32 n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 int error = -EACCES;
1323
David Howellse8896492006-08-24 15:44:19 -04001324 if ((task->tk_rqstp->rq_rcv_buf.len & 3) != 0) {
1325 /* RFC-1014 says that the representation of XDR data must be a
1326 * multiple of four bytes
1327 * - if it isn't pointer subtraction in the NFS client may give
1328 * undefined results
1329 */
Trond Myklebust8a702bb2007-06-27 18:30:26 -04001330 dprintk("RPC: %5u %s: XDR representation not a multiple of"
1331 " 4 bytes: 0x%x\n", task->tk_pid, __FUNCTION__,
1332 task->tk_rqstp->rq_rcv_buf.len);
David Howellse8896492006-08-24 15:44:19 -04001333 goto out_eio;
1334 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 if ((len -= 3) < 0)
1336 goto out_overflow;
1337 p += 1; /* skip XID */
1338
1339 if ((n = ntohl(*p++)) != RPC_REPLY) {
Trond Myklebust8a702bb2007-06-27 18:30:26 -04001340 dprintk("RPC: %5u %s: not an RPC reply: %x\n",
1341 task->tk_pid, __FUNCTION__, n);
Trond Myklebustabbcf282006-01-03 09:55:03 +01001342 goto out_garbage;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 }
1344 if ((n = ntohl(*p++)) != RPC_MSG_ACCEPTED) {
1345 if (--len < 0)
1346 goto out_overflow;
1347 switch ((n = ntohl(*p++))) {
1348 case RPC_AUTH_ERROR:
1349 break;
1350 case RPC_MISMATCH:
Chuck Lever46121cf2007-01-31 12:14:08 -05001351 dprintk("RPC: %5u %s: RPC call version "
1352 "mismatch!\n",
1353 task->tk_pid, __FUNCTION__);
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00001354 error = -EPROTONOSUPPORT;
1355 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 default:
Chuck Lever46121cf2007-01-31 12:14:08 -05001357 dprintk("RPC: %5u %s: RPC call rejected, "
1358 "unknown error: %x\n",
1359 task->tk_pid, __FUNCTION__, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 goto out_eio;
1361 }
1362 if (--len < 0)
1363 goto out_overflow;
1364 switch ((n = ntohl(*p++))) {
1365 case RPC_AUTH_REJECTEDCRED:
1366 case RPC_AUTH_REJECTEDVERF:
1367 case RPCSEC_GSS_CREDPROBLEM:
1368 case RPCSEC_GSS_CTXPROBLEM:
1369 if (!task->tk_cred_retry)
1370 break;
1371 task->tk_cred_retry--;
Chuck Lever46121cf2007-01-31 12:14:08 -05001372 dprintk("RPC: %5u %s: retry stale creds\n",
1373 task->tk_pid, __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 rpcauth_invalcred(task);
1375 task->tk_action = call_refresh;
Trond Myklebustabbcf282006-01-03 09:55:03 +01001376 goto out_retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 case RPC_AUTH_BADCRED:
1378 case RPC_AUTH_BADVERF:
1379 /* possibly garbled cred/verf? */
1380 if (!task->tk_garb_retry)
1381 break;
1382 task->tk_garb_retry--;
Chuck Lever46121cf2007-01-31 12:14:08 -05001383 dprintk("RPC: %5u %s: retry garbled creds\n",
1384 task->tk_pid, __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 task->tk_action = call_bind;
Trond Myklebustabbcf282006-01-03 09:55:03 +01001386 goto out_retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 case RPC_AUTH_TOOWEAK:
Levent Serinol1356b8c2006-03-20 13:44:11 -05001388 printk(KERN_NOTICE "call_verify: server %s requires stronger "
1389 "authentication.\n", task->tk_client->cl_server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 break;
1391 default:
Trond Myklebust8a702bb2007-06-27 18:30:26 -04001392 dprintk("RPC: %5u %s: unknown auth error: %x\n",
1393 task->tk_pid, __FUNCTION__, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 error = -EIO;
1395 }
Chuck Lever46121cf2007-01-31 12:14:08 -05001396 dprintk("RPC: %5u %s: call rejected %d\n",
1397 task->tk_pid, __FUNCTION__, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 goto out_err;
1399 }
1400 if (!(p = rpcauth_checkverf(task, p))) {
Trond Myklebust8a702bb2007-06-27 18:30:26 -04001401 dprintk("RPC: %5u %s: auth check failed\n",
1402 task->tk_pid, __FUNCTION__);
Trond Myklebustabbcf282006-01-03 09:55:03 +01001403 goto out_garbage; /* bad verifier, retry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 }
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001405 len = p - (__be32 *)iov->iov_base - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 if (len < 0)
1407 goto out_overflow;
1408 switch ((n = ntohl(*p++))) {
1409 case RPC_SUCCESS:
1410 return p;
1411 case RPC_PROG_UNAVAIL:
Chuck Lever46121cf2007-01-31 12:14:08 -05001412 dprintk("RPC: %5u %s: program %u is unsupported by server %s\n",
1413 task->tk_pid, __FUNCTION__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 (unsigned int)task->tk_client->cl_prog,
1415 task->tk_client->cl_server);
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00001416 error = -EPFNOSUPPORT;
1417 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 case RPC_PROG_MISMATCH:
Chuck Lever46121cf2007-01-31 12:14:08 -05001419 dprintk("RPC: %5u %s: program %u, version %u unsupported by "
1420 "server %s\n", task->tk_pid, __FUNCTION__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 (unsigned int)task->tk_client->cl_prog,
1422 (unsigned int)task->tk_client->cl_vers,
1423 task->tk_client->cl_server);
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00001424 error = -EPROTONOSUPPORT;
1425 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 case RPC_PROC_UNAVAIL:
Chuck Lever46121cf2007-01-31 12:14:08 -05001427 dprintk("RPC: %5u %s: proc %p unsupported by program %u, "
1428 "version %u on server %s\n",
1429 task->tk_pid, __FUNCTION__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 task->tk_msg.rpc_proc,
1431 task->tk_client->cl_prog,
1432 task->tk_client->cl_vers,
1433 task->tk_client->cl_server);
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00001434 error = -EOPNOTSUPP;
1435 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 case RPC_GARBAGE_ARGS:
Chuck Lever46121cf2007-01-31 12:14:08 -05001437 dprintk("RPC: %5u %s: server saw garbage\n",
1438 task->tk_pid, __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 break; /* retry */
1440 default:
Trond Myklebust8a702bb2007-06-27 18:30:26 -04001441 dprintk("RPC: %5u %s: server accept status: %x\n",
1442 task->tk_pid, __FUNCTION__, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 /* Also retry */
1444 }
1445
Trond Myklebustabbcf282006-01-03 09:55:03 +01001446out_garbage:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 task->tk_client->cl_stats->rpcgarbage++;
1448 if (task->tk_garb_retry) {
1449 task->tk_garb_retry--;
Chuck Lever46121cf2007-01-31 12:14:08 -05001450 dprintk("RPC: %5u %s: retrying\n",
1451 task->tk_pid, __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 task->tk_action = call_bind;
Trond Myklebustabbcf282006-01-03 09:55:03 +01001453out_retry:
1454 return ERR_PTR(-EAGAIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456out_eio:
1457 error = -EIO;
1458out_err:
1459 rpc_exit(task, error);
Trond Myklebust8a702bb2007-06-27 18:30:26 -04001460 dprintk("RPC: %5u %s: call failed with error %d\n", task->tk_pid,
1461 __FUNCTION__, error);
Trond Myklebustabbcf282006-01-03 09:55:03 +01001462 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463out_overflow:
Trond Myklebust8a702bb2007-06-27 18:30:26 -04001464 dprintk("RPC: %5u %s: server reply was truncated.\n", task->tk_pid,
1465 __FUNCTION__);
Trond Myklebustabbcf282006-01-03 09:55:03 +01001466 goto out_garbage;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467}
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00001468
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001469static int rpcproc_encode_null(void *rqstp, __be32 *data, void *obj)
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00001470{
1471 return 0;
1472}
1473
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001474static int rpcproc_decode_null(void *rqstp, __be32 *data, void *obj)
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00001475{
1476 return 0;
1477}
1478
1479static struct rpc_procinfo rpcproc_null = {
1480 .p_encode = rpcproc_encode_null,
1481 .p_decode = rpcproc_decode_null,
1482};
1483
Trond Myklebust64c91a12007-06-23 10:17:16 -04001484static int rpc_ping(struct rpc_clnt *clnt, int flags)
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00001485{
1486 struct rpc_message msg = {
1487 .rpc_proc = &rpcproc_null,
1488 };
1489 int err;
1490 msg.rpc_cred = authnull_ops.lookup_cred(NULL, NULL, 0);
1491 err = rpc_call_sync(clnt, &msg, flags);
1492 put_rpccred(msg.rpc_cred);
1493 return err;
1494}
Trond Myklebust188fef12007-06-16 14:18:40 -04001495
Trond Myklebust5e1550d2007-06-23 10:17:16 -04001496struct rpc_task *rpc_call_null(struct rpc_clnt *clnt, struct rpc_cred *cred, int flags)
1497{
1498 struct rpc_message msg = {
1499 .rpc_proc = &rpcproc_null,
1500 .rpc_cred = cred,
1501 };
1502 return rpc_do_run_task(clnt, &msg, flags, &rpc_default_ops, NULL);
1503}
1504EXPORT_SYMBOL(rpc_call_null);
1505
Trond Myklebust188fef12007-06-16 14:18:40 -04001506#ifdef RPC_DEBUG
1507void rpc_show_tasks(void)
1508{
1509 struct rpc_clnt *clnt;
1510 struct rpc_task *t;
1511
1512 spin_lock(&rpc_client_lock);
1513 if (list_empty(&all_clients))
1514 goto out;
1515 printk("-pid- proc flgs status -client- -prog- --rqstp- -timeout "
1516 "-rpcwait -action- ---ops--\n");
1517 list_for_each_entry(clnt, &all_clients, cl_clients) {
1518 if (list_empty(&clnt->cl_tasks))
1519 continue;
1520 spin_lock(&clnt->cl_lock);
1521 list_for_each_entry(t, &clnt->cl_tasks, tk_task) {
1522 const char *rpc_waitq = "none";
1523
1524 if (RPC_IS_QUEUED(t))
1525 rpc_waitq = rpc_qname(t->u.tk_wait.rpc_waitq);
1526
1527 printk("%5u %04d %04x %6d %8p %6d %8p %8ld %8s %8p %8p\n",
1528 t->tk_pid,
1529 (t->tk_msg.rpc_proc ? t->tk_msg.rpc_proc->p_proc : -1),
1530 t->tk_flags, t->tk_status,
1531 t->tk_client,
1532 (t->tk_client ? t->tk_client->cl_prog : 0),
1533 t->tk_rqstp, t->tk_timeout,
1534 rpc_waitq,
1535 t->tk_action, t->tk_ops);
1536 }
1537 spin_unlock(&clnt->cl_lock);
1538 }
1539out:
1540 spin_unlock(&rpc_client_lock);
1541}
1542#endif