blob: 7aeffeebf42d44138fd9d048a199020692876860 [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;
Chuck Lever06b8d252007-09-11 18:00:20 -0400130 size_t len;
131
132 /* sanity check the name before trying to print it */
133 err = -EINVAL;
134 len = strlen(servname);
135 if (len > RPC_MAXNETNAMELEN)
136 goto out_no_rpciod;
137 len++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Chuck Lever46121cf2007-01-31 12:14:08 -0500139 dprintk("RPC: creating %s client for %s (xprt %p)\n",
140 program->name, servname, xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Trond Myklebust4ada5392007-06-14 17:26:17 -0400142 err = rpciod_up();
143 if (err)
144 goto out_no_rpciod;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 err = -EINVAL;
146 if (!xprt)
Adrian Bunk712917d2006-03-13 21:20:47 -0800147 goto out_no_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 if (vers >= program->nrvers || !(version = program->version[vers]))
149 goto out_err;
150
151 err = -ENOMEM;
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700152 clnt = kzalloc(sizeof(*clnt), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 if (!clnt)
154 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 clnt->cl_parent = clnt;
156
157 clnt->cl_server = clnt->cl_inline_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 if (len > sizeof(clnt->cl_inline_name)) {
159 char *buf = kmalloc(len, GFP_KERNEL);
160 if (buf != 0)
161 clnt->cl_server = buf;
162 else
163 len = sizeof(clnt->cl_inline_name);
164 }
165 strlcpy(clnt->cl_server, servname, len);
166
167 clnt->cl_xprt = xprt;
168 clnt->cl_procinfo = version->procs;
169 clnt->cl_maxproc = version->nrprocs;
170 clnt->cl_protname = program->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 clnt->cl_prog = program->number;
172 clnt->cl_vers = version->number;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 clnt->cl_stats = program->stats;
Chuck Lever11c556b2006-03-20 13:44:22 -0500174 clnt->cl_metrics = rpc_alloc_iostats(clnt);
Trond Myklebust23bf85b2006-11-21 10:40:23 -0500175 err = -ENOMEM;
176 if (clnt->cl_metrics == NULL)
177 goto out_no_stats;
Trond Myklebust3e32a5d2006-11-16 11:37:27 -0500178 clnt->cl_program = program;
Trond Myklebust6529eba2007-06-14 16:40:14 -0400179 INIT_LIST_HEAD(&clnt->cl_tasks);
Trond Myklebust4bef61f2007-06-16 14:17:01 -0400180 spin_lock_init(&clnt->cl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
Chuck Leverec739ef2006-08-22 20:06:15 -0400182 if (!xprt_bound(clnt->cl_xprt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 clnt->cl_autobind = 1;
184
185 clnt->cl_rtt = &clnt->cl_rtt_default;
186 rpc_init_rtt(&clnt->cl_rtt_default, xprt->timeout.to_initval);
187
Trond Myklebust34f52e32007-06-14 16:40:31 -0400188 kref_init(&clnt->cl_kref);
189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 err = rpc_setup_pipedir(clnt, program->pipe_dir_name);
191 if (err < 0)
192 goto out_no_path;
193
J. Bruce Fields6a192752005-06-22 17:16:23 +0000194 auth = rpcauth_create(flavor, clnt);
195 if (IS_ERR(auth)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 printk(KERN_INFO "RPC: Couldn't create auth handle (flavor %u)\n",
197 flavor);
J. Bruce Fields6a192752005-06-22 17:16:23 +0000198 err = PTR_ERR(auth);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 goto out_no_auth;
200 }
201
202 /* save the nodename */
Serge E. Hallyne9ff3992006-10-02 02:18:11 -0700203 clnt->cl_nodelen = strlen(utsname()->nodename);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 if (clnt->cl_nodelen > UNX_MAXNODENAME)
205 clnt->cl_nodelen = UNX_MAXNODENAME;
Serge E. Hallyne9ff3992006-10-02 02:18:11 -0700206 memcpy(clnt->cl_nodename, utsname()->nodename, clnt->cl_nodelen);
Trond Myklebust6529eba2007-06-14 16:40:14 -0400207 rpc_register_client(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 return clnt;
209
210out_no_auth:
Trond Myklebust54281542006-03-20 13:44:49 -0500211 if (!IS_ERR(clnt->cl_dentry)) {
Trond Myklebustdff02cc2006-07-31 14:17:18 -0700212 rpc_rmdir(clnt->cl_dentry);
Trond Myklebust54281542006-03-20 13:44:49 -0500213 rpc_put_mount();
214 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215out_no_path:
Trond Myklebust23bf85b2006-11-21 10:40:23 -0500216 rpc_free_iostats(clnt->cl_metrics);
217out_no_stats:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 if (clnt->cl_server != clnt->cl_inline_name)
219 kfree(clnt->cl_server);
220 kfree(clnt);
221out_err:
Trond Myklebust6b6ca862006-09-05 12:55:57 -0400222 xprt_put(xprt);
Adrian Bunk712917d2006-03-13 21:20:47 -0800223out_no_xprt:
Trond Myklebust4ada5392007-06-14 17:26:17 -0400224 rpciod_down();
225out_no_rpciod:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 return ERR_PTR(err);
227}
228
Chuck Leverc2866762006-08-22 20:06:20 -0400229/*
230 * rpc_create - create an RPC client and transport with one call
231 * @args: rpc_clnt create argument structure
232 *
233 * Creates and initializes an RPC transport and an RPC client.
234 *
235 * It can ping the server in order to determine if it is up, and to see if
236 * it supports this program and version. RPC_CLNT_CREATE_NOPING disables
237 * this behavior so asynchronous tasks can also use rpc_create.
238 */
239struct rpc_clnt *rpc_create(struct rpc_create_args *args)
240{
241 struct rpc_xprt *xprt;
242 struct rpc_clnt *clnt;
\"Talpey, Thomas\3c341b0b2007-09-10 13:47:07 -0400243 struct xprt_create xprtargs = {
\"Talpey, Thomas\4fa016e2007-09-10 13:47:57 -0400244 .ident = args->protocol,
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +0200245 .srcaddr = args->saddress,
Frank van Maarseveen96802a02007-07-08 13:08:54 +0200246 .dstaddr = args->address,
247 .addrlen = args->addrsize,
248 .timeout = args->timeout
249 };
Chuck Lever43780b82007-07-01 12:13:22 -0400250 char servername[20];
Chuck Leverc2866762006-08-22 20:06:20 -0400251
Frank van Maarseveen96802a02007-07-08 13:08:54 +0200252 xprt = xprt_create_transport(&xprtargs);
Chuck Leverc2866762006-08-22 20:06:20 -0400253 if (IS_ERR(xprt))
254 return (struct rpc_clnt *)xprt;
255
256 /*
Chuck Lever43780b82007-07-01 12:13:22 -0400257 * If the caller chooses not to specify a hostname, whip
258 * up a string representation of the passed-in address.
259 */
260 if (args->servername == NULL) {
261 struct sockaddr_in *addr =
J. Bruce Fieldsafde94f2007-09-26 14:38:08 -0400262 (struct sockaddr_in *) args->address;
Chuck Lever43780b82007-07-01 12:13:22 -0400263 snprintf(servername, sizeof(servername), NIPQUAD_FMT,
264 NIPQUAD(addr->sin_addr.s_addr));
265 args->servername = servername;
266 }
267
268 /*
Chuck Leverc2866762006-08-22 20:06:20 -0400269 * By default, kernel RPC client connects from a reserved port.
270 * CAP_NET_BIND_SERVICE will not be set for unprivileged requesters,
271 * but it is always enabled for rpciod, which handles the connect
272 * operation.
273 */
274 xprt->resvport = 1;
275 if (args->flags & RPC_CLNT_CREATE_NONPRIVPORT)
276 xprt->resvport = 0;
277
Chuck Leverc2866762006-08-22 20:06:20 -0400278 clnt = rpc_new_client(xprt, args->servername, args->program,
279 args->version, args->authflavor);
280 if (IS_ERR(clnt))
281 return clnt;
282
283 if (!(args->flags & RPC_CLNT_CREATE_NOPING)) {
284 int err = rpc_ping(clnt, RPC_TASK_SOFT|RPC_TASK_NOINTR);
285 if (err != 0) {
286 rpc_shutdown_client(clnt);
287 return ERR_PTR(err);
288 }
289 }
290
291 clnt->cl_softrtry = 1;
292 if (args->flags & RPC_CLNT_CREATE_HARDRTRY)
293 clnt->cl_softrtry = 0;
294
295 if (args->flags & RPC_CLNT_CREATE_INTR)
296 clnt->cl_intr = 1;
297 if (args->flags & RPC_CLNT_CREATE_AUTOBIND)
298 clnt->cl_autobind = 1;
Chuck Lever43d78ef2007-02-06 18:26:11 -0500299 if (args->flags & RPC_CLNT_CREATE_DISCRTRY)
300 clnt->cl_discrtry = 1;
Chuck Leverc2866762006-08-22 20:06:20 -0400301
302 return clnt;
303}
Chuck Leverb86acd52006-08-22 20:06:22 -0400304EXPORT_SYMBOL_GPL(rpc_create);
Chuck Leverc2866762006-08-22 20:06:20 -0400305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306/*
307 * This function clones the RPC client structure. It allows us to share the
308 * same transport while varying parameters such as the authentication
309 * flavour.
310 */
311struct rpc_clnt *
312rpc_clone_client(struct rpc_clnt *clnt)
313{
314 struct rpc_clnt *new;
Trond Myklebust3e32a5d2006-11-16 11:37:27 -0500315 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Arnaldo Carvalho de Meloe69062b2006-11-21 01:21:34 -0200317 new = kmemdup(clnt, sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 if (!new)
319 goto out_no_clnt;
Trond Myklebustd431a5552007-06-17 17:07:54 -0400320 new->cl_parent = clnt;
321 /* Turn off autobind on clones */
322 new->cl_autobind = 0;
323 INIT_LIST_HEAD(&new->cl_tasks);
324 spin_lock_init(&new->cl_lock);
325 rpc_init_rtt(&new->cl_rtt_default, clnt->cl_xprt->timeout.to_initval);
Trond Myklebust23bf85b2006-11-21 10:40:23 -0500326 new->cl_metrics = rpc_alloc_iostats(clnt);
327 if (new->cl_metrics == NULL)
328 goto out_no_stats;
Trond Myklebust34f52e32007-06-14 16:40:31 -0400329 kref_init(&new->cl_kref);
Trond Myklebust3e32a5d2006-11-16 11:37:27 -0500330 err = rpc_setup_pipedir(new, clnt->cl_program->pipe_dir_name);
331 if (err != 0)
332 goto out_no_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 if (new->cl_auth)
334 atomic_inc(&new->cl_auth->au_count);
Trond Myklebustd431a5552007-06-17 17:07:54 -0400335 xprt_get(clnt->cl_xprt);
336 kref_get(&clnt->cl_kref);
Trond Myklebust6529eba2007-06-14 16:40:14 -0400337 rpc_register_client(new);
Trond Myklebust4ada5392007-06-14 17:26:17 -0400338 rpciod_up();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 return new;
Trond Myklebust3e32a5d2006-11-16 11:37:27 -0500340out_no_path:
341 rpc_free_iostats(new->cl_metrics);
Trond Myklebust23bf85b2006-11-21 10:40:23 -0500342out_no_stats:
343 kfree(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344out_no_clnt:
Chuck Lever46121cf2007-01-31 12:14:08 -0500345 dprintk("RPC: %s: returned error %d\n", __FUNCTION__, err);
Trond Myklebust3e32a5d2006-11-16 11:37:27 -0500346 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400348EXPORT_SYMBOL_GPL(rpc_clone_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
350/*
351 * Properly shut down an RPC client, terminating all outstanding
Trond Myklebust90c57552007-06-09 19:49:36 -0400352 * requests.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 */
Trond Myklebust4c402b42007-06-14 16:40:32 -0400354void rpc_shutdown_client(struct rpc_clnt *clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355{
Trond Myklebust34f52e32007-06-14 16:40:31 -0400356 dprintk("RPC: shutting down %s client for %s\n",
357 clnt->cl_protname, clnt->cl_server);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Trond Myklebust34f52e32007-06-14 16:40:31 -0400359 while (!list_empty(&clnt->cl_tasks)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 rpc_killall_tasks(clnt);
Ingo Molnar532347e2006-01-09 20:52:53 -0800361 wait_event_timeout(destroy_wait,
Trond Myklebust34f52e32007-06-14 16:40:31 -0400362 list_empty(&clnt->cl_tasks), 1*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 }
364
Trond Myklebust4c402b42007-06-14 16:40:32 -0400365 rpc_release_client(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400367EXPORT_SYMBOL_GPL(rpc_shutdown_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
369/*
Trond Myklebust34f52e32007-06-14 16:40:31 -0400370 * Free an RPC client
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 */
Trond Myklebust34f52e32007-06-14 16:40:31 -0400372static void
373rpc_free_client(struct kref *kref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374{
Trond Myklebust34f52e32007-06-14 16:40:31 -0400375 struct rpc_clnt *clnt = container_of(kref, struct rpc_clnt, cl_kref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Chuck Lever46121cf2007-01-31 12:14:08 -0500377 dprintk("RPC: destroying %s client for %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 clnt->cl_protname, clnt->cl_server);
Trond Myklebust8f8e7a52006-08-14 13:11:15 -0400379 if (!IS_ERR(clnt->cl_dentry)) {
Trond Myklebustdff02cc2006-07-31 14:17:18 -0700380 rpc_rmdir(clnt->cl_dentry);
Trond Myklebust8f8e7a52006-08-14 13:11:15 -0400381 rpc_put_mount();
382 }
Trond Myklebust3e32a5d2006-11-16 11:37:27 -0500383 if (clnt->cl_parent != clnt) {
Trond Myklebust8ad7c892007-06-14 16:40:32 -0400384 rpc_release_client(clnt->cl_parent);
Trond Myklebust3e32a5d2006-11-16 11:37:27 -0500385 goto out_free;
386 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 if (clnt->cl_server != clnt->cl_inline_name)
388 kfree(clnt->cl_server);
389out_free:
Trond Myklebust6529eba2007-06-14 16:40:14 -0400390 rpc_unregister_client(clnt);
Chuck Lever11c556b2006-03-20 13:44:22 -0500391 rpc_free_iostats(clnt->cl_metrics);
392 clnt->cl_metrics = NULL;
Trond Myklebust6b6ca862006-09-05 12:55:57 -0400393 xprt_put(clnt->cl_xprt);
Trond Myklebust4ada5392007-06-14 17:26:17 -0400394 rpciod_down();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 kfree(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396}
397
398/*
Trond Myklebust1dd17ec2007-06-26 16:57:41 -0400399 * Free an RPC client
400 */
401static void
402rpc_free_auth(struct kref *kref)
403{
404 struct rpc_clnt *clnt = container_of(kref, struct rpc_clnt, cl_kref);
405
406 if (clnt->cl_auth == NULL) {
407 rpc_free_client(kref);
408 return;
409 }
410
411 /*
412 * Note: RPCSEC_GSS may need to send NULL RPC calls in order to
413 * release remaining GSS contexts. This mechanism ensures
414 * that it can do so safely.
415 */
416 kref_init(kref);
417 rpcauth_release(clnt->cl_auth);
418 clnt->cl_auth = NULL;
419 kref_put(kref, rpc_free_client);
420}
421
422/*
Trond Myklebust34f52e32007-06-14 16:40:31 -0400423 * Release reference to the RPC client
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 */
425void
426rpc_release_client(struct rpc_clnt *clnt)
427{
Trond Myklebust34f52e32007-06-14 16:40:31 -0400428 dprintk("RPC: rpc_release_client(%p)\n", clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
Trond Myklebust34f52e32007-06-14 16:40:31 -0400430 if (list_empty(&clnt->cl_tasks))
431 wake_up(&destroy_wait);
Trond Myklebust1dd17ec2007-06-26 16:57:41 -0400432 kref_put(&clnt->cl_kref, rpc_free_auth);
Trond Myklebust34f52e32007-06-14 16:40:31 -0400433}
434
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000435/**
436 * rpc_bind_new_program - bind a new RPC program to an existing client
437 * @old - old rpc_client
438 * @program - rpc program to set
439 * @vers - rpc program version
440 *
441 * Clones the rpc client and sets up a new RPC program. This is mainly
442 * of use for enabling different RPC programs to share the same transport.
443 * The Sun NFSv2/v3 ACL protocol can do this.
444 */
445struct rpc_clnt *rpc_bind_new_program(struct rpc_clnt *old,
446 struct rpc_program *program,
Chuck Lever89eb21c2007-09-11 18:00:09 -0400447 u32 vers)
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000448{
449 struct rpc_clnt *clnt;
450 struct rpc_version *version;
451 int err;
452
453 BUG_ON(vers >= program->nrvers || !program->version[vers]);
454 version = program->version[vers];
455 clnt = rpc_clone_client(old);
456 if (IS_ERR(clnt))
457 goto out;
458 clnt->cl_procinfo = version->procs;
459 clnt->cl_maxproc = version->nrprocs;
460 clnt->cl_protname = program->name;
461 clnt->cl_prog = program->number;
462 clnt->cl_vers = version->number;
463 clnt->cl_stats = program->stats;
464 err = rpc_ping(clnt, RPC_TASK_SOFT|RPC_TASK_NOINTR);
465 if (err != 0) {
466 rpc_shutdown_client(clnt);
467 clnt = ERR_PTR(err);
468 }
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800469out:
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000470 return clnt;
471}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400472EXPORT_SYMBOL_GPL(rpc_bind_new_program);
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000473
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474/*
475 * Default callback for async RPC calls
476 */
477static void
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100478rpc_default_callback(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479{
480}
481
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100482static const struct rpc_call_ops rpc_default_ops = {
483 .rpc_call_done = rpc_default_callback,
484};
485
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486/*
Trond Myklebust14b218a2005-06-22 17:16:28 +0000487 * Export the signal mask handling for synchronous code that
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 * sleeps on RPC calls
489 */
Trond Myklebust2bd61572006-01-03 09:55:19 +0100490#define RPC_INTR_SIGNALS (sigmask(SIGHUP) | sigmask(SIGINT) | sigmask(SIGQUIT) | sigmask(SIGTERM))
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800491
Trond Myklebust14b218a2005-06-22 17:16:28 +0000492static void rpc_save_sigmask(sigset_t *oldset, int intr)
493{
Trond Myklebust2bd61572006-01-03 09:55:19 +0100494 unsigned long sigallow = sigmask(SIGKILL);
Trond Myklebust14b218a2005-06-22 17:16:28 +0000495 sigset_t sigmask;
496
497 /* Block all signals except those listed in sigallow */
498 if (intr)
499 sigallow |= RPC_INTR_SIGNALS;
500 siginitsetinv(&sigmask, sigallow);
501 sigprocmask(SIG_BLOCK, &sigmask, oldset);
502}
503
Trond Myklebust50859252007-10-25 18:19:37 -0400504static void rpc_task_sigmask(struct rpc_task *task, sigset_t *oldset)
Trond Myklebust14b218a2005-06-22 17:16:28 +0000505{
506 rpc_save_sigmask(oldset, !RPC_TASK_UNINTERRUPTIBLE(task));
507}
508
Trond Myklebust50859252007-10-25 18:19:37 -0400509static void rpc_restore_sigmask(sigset_t *oldset)
Trond Myklebust14b218a2005-06-22 17:16:28 +0000510{
511 sigprocmask(SIG_SETMASK, oldset, NULL);
512}
513
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514void rpc_clnt_sigmask(struct rpc_clnt *clnt, sigset_t *oldset)
515{
Trond Myklebust14b218a2005-06-22 17:16:28 +0000516 rpc_save_sigmask(oldset, clnt->cl_intr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400518EXPORT_SYMBOL_GPL(rpc_clnt_sigmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
520void rpc_clnt_sigunmask(struct rpc_clnt *clnt, sigset_t *oldset)
521{
Trond Myklebust14b218a2005-06-22 17:16:28 +0000522 rpc_restore_sigmask(oldset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400524EXPORT_SYMBOL_GPL(rpc_clnt_sigunmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Trond Myklebustc970aa82007-07-14 15:39:59 -0400526/**
527 * rpc_run_task - Allocate a new RPC task, then run rpc_execute against it
528 * @task_setup_data: pointer to task initialisation data
529 */
530struct rpc_task *rpc_run_task(const struct rpc_task_setup *task_setup_data)
Trond Myklebust6e5b70e2007-06-12 10:02:37 -0400531{
532 struct rpc_task *task, *ret;
533 sigset_t oldset;
534
Trond Myklebust84115e12007-07-14 15:39:59 -0400535 task = rpc_new_task(task_setup_data);
Trond Myklebust6e5b70e2007-06-12 10:02:37 -0400536 if (task == NULL) {
Trond Myklebust84115e12007-07-14 15:39:59 -0400537 rpc_release_calldata(task_setup_data->callback_ops,
538 task_setup_data->callback_data);
Trond Myklebust50859252007-10-25 18:19:37 -0400539 ret = ERR_PTR(-ENOMEM);
540 goto out;
Trond Myklebust6e5b70e2007-06-12 10:02:37 -0400541 }
542
Trond Myklebust84115e12007-07-14 15:39:59 -0400543 if (task_setup_data->rpc_message != NULL) {
544 rpc_call_setup(task, task_setup_data->rpc_message, 0);
Trond Myklebust6e5b70e2007-06-12 10:02:37 -0400545 if (task->tk_status != 0) {
546 ret = ERR_PTR(task->tk_status);
547 rpc_put_task(task);
548 goto out;
549 }
550 }
551 atomic_inc(&task->tk_count);
Trond Myklebust50859252007-10-25 18:19:37 -0400552 /* Mask signals on synchronous RPC calls and RPCSEC_GSS upcalls */
553 rpc_task_sigmask(task, &oldset);
Trond Myklebust6e5b70e2007-06-12 10:02:37 -0400554 rpc_execute(task);
Trond Myklebust50859252007-10-25 18:19:37 -0400555 rpc_restore_sigmask(&oldset);
Trond Myklebust6e5b70e2007-06-12 10:02:37 -0400556 ret = task;
557out:
Trond Myklebust6e5b70e2007-06-12 10:02:37 -0400558 return ret;
559}
Trond Myklebustc970aa82007-07-14 15:39:59 -0400560EXPORT_SYMBOL_GPL(rpc_run_task);
Trond Myklebust6e5b70e2007-06-12 10:02:37 -0400561
562/**
563 * rpc_call_sync - Perform a synchronous RPC call
564 * @clnt: pointer to RPC client
565 * @msg: RPC call parameters
566 * @flags: RPC call flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 */
568int rpc_call_sync(struct rpc_clnt *clnt, struct rpc_message *msg, int flags)
569{
570 struct rpc_task *task;
Trond Myklebust84115e12007-07-14 15:39:59 -0400571 struct rpc_task_setup task_setup_data = {
572 .rpc_client = clnt,
573 .rpc_message = msg,
574 .callback_ops = &rpc_default_ops,
575 .flags = flags,
576 };
Trond Myklebust6e5b70e2007-06-12 10:02:37 -0400577 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 BUG_ON(flags & RPC_TASK_ASYNC);
580
Trond Myklebustc970aa82007-07-14 15:39:59 -0400581 task = rpc_run_task(&task_setup_data);
Trond Myklebust6e5b70e2007-06-12 10:02:37 -0400582 if (IS_ERR(task))
583 return PTR_ERR(task);
Trond Myklebuste60859a2006-01-03 09:55:10 +0100584 status = task->tk_status;
Trond Myklebustbde8f002007-01-24 11:54:53 -0800585 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 return status;
587}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400588EXPORT_SYMBOL_GPL(rpc_call_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
Trond Myklebust6e5b70e2007-06-12 10:02:37 -0400590/**
591 * rpc_call_async - Perform an asynchronous RPC call
592 * @clnt: pointer to RPC client
593 * @msg: RPC call parameters
594 * @flags: RPC call flags
595 * @ops: RPC call ops
596 * @data: user call data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 */
598int
599rpc_call_async(struct rpc_clnt *clnt, struct rpc_message *msg, int flags,
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100600 const struct rpc_call_ops *tk_ops, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601{
602 struct rpc_task *task;
Trond Myklebust84115e12007-07-14 15:39:59 -0400603 struct rpc_task_setup task_setup_data = {
604 .rpc_client = clnt,
605 .rpc_message = msg,
606 .callback_ops = tk_ops,
607 .callback_data = data,
608 .flags = flags|RPC_TASK_ASYNC,
609 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
Trond Myklebustc970aa82007-07-14 15:39:59 -0400611 task = rpc_run_task(&task_setup_data);
Trond Myklebust6e5b70e2007-06-12 10:02:37 -0400612 if (IS_ERR(task))
613 return PTR_ERR(task);
614 rpc_put_task(task);
615 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400617EXPORT_SYMBOL_GPL(rpc_call_async);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619void
Trond Myklebust84115e12007-07-14 15:39:59 -0400620rpc_call_setup(struct rpc_task *task, const struct rpc_message *msg, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621{
622 task->tk_msg = *msg;
623 task->tk_flags |= flags;
624 /* Bind the user cred */
625 if (task->tk_msg.rpc_cred != NULL)
626 rpcauth_holdcred(task);
627 else
628 rpcauth_bindcred(task);
629
630 if (task->tk_status == 0)
631 task->tk_action = call_start;
632 else
Trond Myklebustabbcf282006-01-03 09:55:03 +0100633 task->tk_action = rpc_exit_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400635EXPORT_SYMBOL_GPL(rpc_call_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
Trond Myklebust77de2c52007-10-25 18:40:21 -0400637void
638rpc_call_start(struct rpc_task *task)
639{
640 task->tk_action = call_start;
641}
642EXPORT_SYMBOL_GPL(rpc_call_start);
643
Chuck Levered394402006-08-22 20:06:17 -0400644/**
645 * rpc_peeraddr - extract remote peer address from clnt's xprt
646 * @clnt: RPC client structure
647 * @buf: target buffer
648 * @size: length of target buffer
649 *
650 * Returns the number of bytes that are actually in the stored address.
651 */
652size_t rpc_peeraddr(struct rpc_clnt *clnt, struct sockaddr *buf, size_t bufsize)
653{
654 size_t bytes;
655 struct rpc_xprt *xprt = clnt->cl_xprt;
656
657 bytes = sizeof(xprt->addr);
658 if (bytes > bufsize)
659 bytes = bufsize;
660 memcpy(buf, &clnt->cl_xprt->addr, bytes);
Chuck Leverc4efcb12006-08-22 20:06:19 -0400661 return xprt->addrlen;
Chuck Levered394402006-08-22 20:06:17 -0400662}
Chuck Leverb86acd52006-08-22 20:06:22 -0400663EXPORT_SYMBOL_GPL(rpc_peeraddr);
Chuck Levered394402006-08-22 20:06:17 -0400664
Chuck Leverf425eba2006-08-22 20:06:18 -0400665/**
666 * rpc_peeraddr2str - return remote peer address in printable format
667 * @clnt: RPC client structure
668 * @format: address format
669 *
670 */
671char *rpc_peeraddr2str(struct rpc_clnt *clnt, enum rpc_display_format_t format)
672{
673 struct rpc_xprt *xprt = clnt->cl_xprt;
Chuck Lever7559c7a2006-12-05 16:35:37 -0500674
675 if (xprt->address_strings[format] != NULL)
676 return xprt->address_strings[format];
677 else
678 return "unprintable";
Chuck Leverf425eba2006-08-22 20:06:18 -0400679}
Chuck Leverb86acd52006-08-22 20:06:22 -0400680EXPORT_SYMBOL_GPL(rpc_peeraddr2str);
Chuck Leverf425eba2006-08-22 20:06:18 -0400681
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682void
683rpc_setbufsize(struct rpc_clnt *clnt, unsigned int sndsize, unsigned int rcvsize)
684{
685 struct rpc_xprt *xprt = clnt->cl_xprt;
Chuck Lever470056c2005-08-25 16:25:56 -0700686 if (xprt->ops->set_buffer_size)
687 xprt->ops->set_buffer_size(xprt, sndsize, rcvsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400689EXPORT_SYMBOL_GPL(rpc_setbufsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
691/*
692 * Return size of largest payload RPC client can support, in bytes
693 *
694 * For stream transports, this is one RPC record fragment (see RFC
695 * 1831), as we don't support multi-record requests yet. For datagram
696 * transports, this is the size of an IP packet minus the IP, UDP, and
697 * RPC header sizes.
698 */
699size_t rpc_max_payload(struct rpc_clnt *clnt)
700{
701 return clnt->cl_xprt->max_payload;
702}
Chuck Leverb86acd52006-08-22 20:06:22 -0400703EXPORT_SYMBOL_GPL(rpc_max_payload);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
Chuck Lever35f5a422006-01-03 09:55:50 +0100705/**
706 * rpc_force_rebind - force transport to check that remote port is unchanged
707 * @clnt: client to rebind
708 *
709 */
710void rpc_force_rebind(struct rpc_clnt *clnt)
711{
712 if (clnt->cl_autobind)
Chuck Leverec739ef2006-08-22 20:06:15 -0400713 xprt_clear_bound(clnt->cl_xprt);
Chuck Lever35f5a422006-01-03 09:55:50 +0100714}
Chuck Leverb86acd52006-08-22 20:06:22 -0400715EXPORT_SYMBOL_GPL(rpc_force_rebind);
Chuck Lever35f5a422006-01-03 09:55:50 +0100716
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717/*
718 * Restart an (async) RPC call. Usually called from within the
719 * exit handler.
720 */
721void
722rpc_restart_call(struct rpc_task *task)
723{
724 if (RPC_ASSASSINATED(task))
725 return;
726
727 task->tk_action = call_start;
728}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400729EXPORT_SYMBOL_GPL(rpc_restart_call);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
731/*
732 * 0. Initial state
733 *
734 * Other FSM states can be visited zero or more times, but
735 * this state is visited exactly once for each RPC.
736 */
737static void
738call_start(struct rpc_task *task)
739{
740 struct rpc_clnt *clnt = task->tk_client;
741
Chuck Lever46121cf2007-01-31 12:14:08 -0500742 dprintk("RPC: %5u call_start %s%d proc %d (%s)\n", task->tk_pid,
743 clnt->cl_protname, clnt->cl_vers,
744 task->tk_msg.rpc_proc->p_proc,
745 (RPC_IS_ASYNC(task) ? "async" : "sync"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
747 /* Increment call count */
748 task->tk_msg.rpc_proc->p_count++;
749 clnt->cl_stats->rpccnt++;
750 task->tk_action = call_reserve;
751}
752
753/*
754 * 1. Reserve an RPC call slot
755 */
756static void
757call_reserve(struct rpc_task *task)
758{
Chuck Lever46121cf2007-01-31 12:14:08 -0500759 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
761 if (!rpcauth_uptodatecred(task)) {
762 task->tk_action = call_refresh;
763 return;
764 }
765
766 task->tk_status = 0;
767 task->tk_action = call_reserveresult;
768 xprt_reserve(task);
769}
770
771/*
772 * 1b. Grok the result of xprt_reserve()
773 */
774static void
775call_reserveresult(struct rpc_task *task)
776{
777 int status = task->tk_status;
778
Chuck Lever46121cf2007-01-31 12:14:08 -0500779 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
781 /*
782 * After a call to xprt_reserve(), we must have either
783 * a request slot or else an error status.
784 */
785 task->tk_status = 0;
786 if (status >= 0) {
787 if (task->tk_rqstp) {
788 task->tk_action = call_allocate;
789 return;
790 }
791
792 printk(KERN_ERR "%s: status=%d, but no request slot, exiting\n",
793 __FUNCTION__, status);
794 rpc_exit(task, -EIO);
795 return;
796 }
797
798 /*
799 * Even though there was an error, we may have acquired
800 * a request slot somehow. Make sure not to leak it.
801 */
802 if (task->tk_rqstp) {
803 printk(KERN_ERR "%s: status=%d, request allocated anyway\n",
804 __FUNCTION__, status);
805 xprt_release(task);
806 }
807
808 switch (status) {
809 case -EAGAIN: /* woken up; retry */
810 task->tk_action = call_reserve;
811 return;
812 case -EIO: /* probably a shutdown */
813 break;
814 default:
815 printk(KERN_ERR "%s: unrecognized error %d, exiting\n",
816 __FUNCTION__, status);
817 break;
818 }
819 rpc_exit(task, status);
820}
821
822/*
823 * 2. Allocate the buffer. For details, see sched.c:rpc_malloc.
Chuck Lever02107142006-01-03 09:55:49 +0100824 * (Note: buffer memory is freed in xprt_release).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 */
826static void
827call_allocate(struct rpc_task *task)
828{
Trond Myklebust1be27f32007-06-27 14:29:04 -0400829 unsigned int slack = task->tk_msg.rpc_cred->cr_auth->au_cslack;
Chuck Lever02107142006-01-03 09:55:49 +0100830 struct rpc_rqst *req = task->tk_rqstp;
831 struct rpc_xprt *xprt = task->tk_xprt;
Chuck Lever2bea90d2007-03-29 16:47:53 -0400832 struct rpc_procinfo *proc = task->tk_msg.rpc_proc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
Chuck Lever46121cf2007-01-31 12:14:08 -0500834 dprint_status(task);
835
Chuck Lever2bea90d2007-03-29 16:47:53 -0400836 task->tk_status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 task->tk_action = call_bind;
Chuck Lever2bea90d2007-03-29 16:47:53 -0400838
Chuck Lever02107142006-01-03 09:55:49 +0100839 if (req->rq_buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 return;
841
Chuck Lever2bea90d2007-03-29 16:47:53 -0400842 if (proc->p_proc != 0) {
843 BUG_ON(proc->p_arglen == 0);
844 if (proc->p_decode != NULL)
845 BUG_ON(proc->p_replen == 0);
846 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
Chuck Lever2bea90d2007-03-29 16:47:53 -0400848 /*
849 * Calculate the size (in quads) of the RPC call
850 * and reply headers, and convert both values
851 * to byte sizes.
852 */
853 req->rq_callsize = RPC_CALLHDRSIZE + (slack << 1) + proc->p_arglen;
854 req->rq_callsize <<= 2;
855 req->rq_rcvsize = RPC_REPHDRSIZE + slack + proc->p_replen;
856 req->rq_rcvsize <<= 2;
857
Chuck Leverc5a4dd82007-03-29 16:47:58 -0400858 req->rq_buffer = xprt->ops->buf_alloc(task,
859 req->rq_callsize + req->rq_rcvsize);
Chuck Lever2bea90d2007-03-29 16:47:53 -0400860 if (req->rq_buffer != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 return;
Chuck Lever46121cf2007-01-31 12:14:08 -0500862
863 dprintk("RPC: %5u rpc_buffer allocation failed\n", task->tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
Trond Myklebust14b218a2005-06-22 17:16:28 +0000865 if (RPC_IS_ASYNC(task) || !signalled()) {
Trond Myklebustb6e9c712007-10-01 12:06:44 -0400866 task->tk_action = call_allocate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 rpc_delay(task, HZ>>4);
868 return;
869 }
870
871 rpc_exit(task, -ERESTARTSYS);
872}
873
Trond Myklebust940e3312005-11-09 21:45:24 -0500874static inline int
875rpc_task_need_encode(struct rpc_task *task)
876{
877 return task->tk_rqstp->rq_snd_buf.len == 0;
878}
879
880static inline void
881rpc_task_force_reencode(struct rpc_task *task)
882{
883 task->tk_rqstp->rq_snd_buf.len = 0;
884}
885
Chuck Lever2bea90d2007-03-29 16:47:53 -0400886static inline void
887rpc_xdr_buf_init(struct xdr_buf *buf, void *start, size_t len)
888{
889 buf->head[0].iov_base = start;
890 buf->head[0].iov_len = len;
891 buf->tail[0].iov_len = 0;
892 buf->page_len = 0;
\"Talpey, Thomas\4f22ccc2007-09-10 13:44:58 -0400893 buf->flags = 0;
Chuck Lever2bea90d2007-03-29 16:47:53 -0400894 buf->len = 0;
895 buf->buflen = len;
896}
897
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898/*
899 * 3. Encode arguments of an RPC call
900 */
901static void
902call_encode(struct rpc_task *task)
903{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 struct rpc_rqst *req = task->tk_rqstp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 kxdrproc_t encode;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700906 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
Chuck Lever46121cf2007-01-31 12:14:08 -0500908 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
Chuck Lever2bea90d2007-03-29 16:47:53 -0400910 rpc_xdr_buf_init(&req->rq_snd_buf,
911 req->rq_buffer,
912 req->rq_callsize);
913 rpc_xdr_buf_init(&req->rq_rcv_buf,
914 (char *)req->rq_buffer + req->rq_callsize,
915 req->rq_rcvsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
917 /* Encode header and provided arguments */
918 encode = task->tk_msg.rpc_proc->p_encode;
919 if (!(p = call_header(task))) {
920 printk(KERN_INFO "RPC: call_header failed, exit EIO\n");
921 rpc_exit(task, -EIO);
922 return;
923 }
J. Bruce Fieldsf3680312005-10-13 16:54:48 -0400924 if (encode == NULL)
925 return;
926
927 task->tk_status = rpcauth_wrap_req(task, encode, req, p,
928 task->tk_msg.rpc_argp);
929 if (task->tk_status == -ENOMEM) {
930 /* XXX: Is this sane? */
931 rpc_delay(task, 3*HZ);
932 task->tk_status = -EAGAIN;
933 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934}
935
936/*
937 * 4. Get the server port number if not yet set
938 */
939static void
940call_bind(struct rpc_task *task)
941{
Chuck Leverec739ef2006-08-22 20:06:15 -0400942 struct rpc_xprt *xprt = task->tk_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
Chuck Lever46121cf2007-01-31 12:14:08 -0500944 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
Chuck Leverda351872005-08-11 16:25:11 -0400946 task->tk_action = call_connect;
Chuck Leverec739ef2006-08-22 20:06:15 -0400947 if (!xprt_bound(xprt)) {
Chuck Leverda351872005-08-11 16:25:11 -0400948 task->tk_action = call_bind_status;
Chuck Leverec739ef2006-08-22 20:06:15 -0400949 task->tk_timeout = xprt->bind_timeout;
Chuck Leverbbf7c1d2006-08-22 20:06:16 -0400950 xprt->ops->rpcbind(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 }
952}
953
954/*
Chuck Leverda351872005-08-11 16:25:11 -0400955 * 4a. Sort out bind result
956 */
957static void
958call_bind_status(struct rpc_task *task)
959{
Chuck Lever906462a2007-09-11 18:00:47 -0400960 int status = -EIO;
Chuck Leverda351872005-08-11 16:25:11 -0400961
962 if (task->tk_status >= 0) {
Chuck Lever46121cf2007-01-31 12:14:08 -0500963 dprint_status(task);
Chuck Leverda351872005-08-11 16:25:11 -0400964 task->tk_status = 0;
965 task->tk_action = call_connect;
966 return;
967 }
968
969 switch (task->tk_status) {
Chuck Lever2429cbf2007-09-11 18:00:41 -0400970 case -EAGAIN:
971 dprintk("RPC: %5u rpcbind waiting for another request "
972 "to finish\n", task->tk_pid);
973 /* avoid busy-waiting here -- could be a network outage. */
974 rpc_delay(task, 5*HZ);
975 goto retry_timeout;
Chuck Leverda351872005-08-11 16:25:11 -0400976 case -EACCES:
Chuck Lever46121cf2007-01-31 12:14:08 -0500977 dprintk("RPC: %5u remote rpcbind: RPC program/version "
978 "unavailable\n", task->tk_pid);
Chuck Leverb79dc8c2007-09-11 18:00:52 -0400979 /* fail immediately if this is an RPC ping */
980 if (task->tk_msg.rpc_proc->p_proc == 0) {
981 status = -EOPNOTSUPP;
982 break;
983 }
Chuck Leverea635a52005-10-06 23:12:58 -0400984 rpc_delay(task, 3*HZ);
Trond Myklebustda458282006-08-31 15:44:52 -0400985 goto retry_timeout;
Chuck Leverda351872005-08-11 16:25:11 -0400986 case -ETIMEDOUT:
Chuck Lever46121cf2007-01-31 12:14:08 -0500987 dprintk("RPC: %5u rpcbind request timed out\n",
Chuck Leverda351872005-08-11 16:25:11 -0400988 task->tk_pid);
Trond Myklebustda458282006-08-31 15:44:52 -0400989 goto retry_timeout;
Chuck Leverda351872005-08-11 16:25:11 -0400990 case -EPFNOSUPPORT:
Chuck Lever906462a2007-09-11 18:00:47 -0400991 /* server doesn't support any rpcbind version we know of */
Chuck Lever46121cf2007-01-31 12:14:08 -0500992 dprintk("RPC: %5u remote rpcbind service unavailable\n",
Chuck Leverda351872005-08-11 16:25:11 -0400993 task->tk_pid);
994 break;
995 case -EPROTONOSUPPORT:
Chuck Lever00a6e7b2007-03-29 16:48:33 -0400996 dprintk("RPC: %5u remote rpcbind version unavailable, retrying\n",
Chuck Leverda351872005-08-11 16:25:11 -0400997 task->tk_pid);
Chuck Lever00a6e7b2007-03-29 16:48:33 -0400998 task->tk_status = 0;
999 task->tk_action = call_bind;
1000 return;
Chuck Leverda351872005-08-11 16:25:11 -04001001 default:
Chuck Lever46121cf2007-01-31 12:14:08 -05001002 dprintk("RPC: %5u unrecognized rpcbind error (%d)\n",
Chuck Leverda351872005-08-11 16:25:11 -04001003 task->tk_pid, -task->tk_status);
Chuck Leverda351872005-08-11 16:25:11 -04001004 }
1005
1006 rpc_exit(task, status);
1007 return;
1008
Trond Myklebustda458282006-08-31 15:44:52 -04001009retry_timeout:
1010 task->tk_action = call_timeout;
Chuck Leverda351872005-08-11 16:25:11 -04001011}
1012
1013/*
1014 * 4b. Connect to the RPC server
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 */
1016static void
1017call_connect(struct rpc_task *task)
1018{
Chuck Leverda351872005-08-11 16:25:11 -04001019 struct rpc_xprt *xprt = task->tk_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
Chuck Lever46121cf2007-01-31 12:14:08 -05001021 dprintk("RPC: %5u call_connect xprt %p %s connected\n",
Chuck Leverda351872005-08-11 16:25:11 -04001022 task->tk_pid, xprt,
1023 (xprt_connected(xprt) ? "is" : "is not"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
Chuck Leverda351872005-08-11 16:25:11 -04001025 task->tk_action = call_transmit;
1026 if (!xprt_connected(xprt)) {
1027 task->tk_action = call_connect_status;
1028 if (task->tk_status < 0)
1029 return;
1030 xprt_connect(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032}
1033
1034/*
Chuck Leverda351872005-08-11 16:25:11 -04001035 * 4c. Sort out connect result
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 */
1037static void
1038call_connect_status(struct rpc_task *task)
1039{
1040 struct rpc_clnt *clnt = task->tk_client;
1041 int status = task->tk_status;
1042
Chuck Lever46121cf2007-01-31 12:14:08 -05001043 dprint_status(task);
Chuck Leverda351872005-08-11 16:25:11 -04001044
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 task->tk_status = 0;
1046 if (status >= 0) {
1047 clnt->cl_stats->netreconn++;
1048 task->tk_action = call_transmit;
1049 return;
1050 }
1051
Chuck Leverda351872005-08-11 16:25:11 -04001052 /* Something failed: remote service port may have changed */
Chuck Lever35f5a422006-01-03 09:55:50 +01001053 rpc_force_rebind(clnt);
Chuck Leverda351872005-08-11 16:25:11 -04001054
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 switch (status) {
1056 case -ENOTCONN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 case -EAGAIN:
Chuck Leverda351872005-08-11 16:25:11 -04001058 task->tk_action = call_bind;
Trond Myklebustda458282006-08-31 15:44:52 -04001059 if (!RPC_IS_SOFT(task))
1060 return;
1061 /* if soft mounted, test if we've timed out */
1062 case -ETIMEDOUT:
1063 task->tk_action = call_timeout;
1064 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 }
Trond Myklebustda458282006-08-31 15:44:52 -04001066 rpc_exit(task, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067}
1068
1069/*
1070 * 5. Transmit the RPC request, and wait for reply
1071 */
1072static void
1073call_transmit(struct rpc_task *task)
1074{
Chuck Lever46121cf2007-01-31 12:14:08 -05001075 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
1077 task->tk_action = call_status;
1078 if (task->tk_status < 0)
1079 return;
1080 task->tk_status = xprt_prepare_transmit(task);
1081 if (task->tk_status != 0)
1082 return;
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04001083 task->tk_action = call_transmit_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 /* Encode here so that rpcsec_gss can use correct sequence number. */
Trond Myklebust940e3312005-11-09 21:45:24 -05001085 if (rpc_task_need_encode(task)) {
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04001086 BUG_ON(task->tk_rqstp->rq_bytes_sent != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 call_encode(task);
Trond Myklebust5e5ce5b2005-10-18 14:20:11 -07001088 /* Did the encode result in an error condition? */
1089 if (task->tk_status != 0)
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04001090 return;
Trond Myklebust5e5ce5b2005-10-18 14:20:11 -07001091 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 xprt_transmit(task);
1093 if (task->tk_status < 0)
1094 return;
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04001095 /*
1096 * On success, ensure that we call xprt_end_transmit() before sleeping
1097 * in order to allow access to the socket to other RPC requests.
1098 */
1099 call_transmit_status(task);
1100 if (task->tk_msg.rpc_proc->p_decode != NULL)
1101 return;
1102 task->tk_action = rpc_exit_task;
1103 rpc_wake_up_task(task);
1104}
1105
1106/*
1107 * 5a. Handle cleanup after a transmission
1108 */
1109static void
1110call_transmit_status(struct rpc_task *task)
1111{
1112 task->tk_action = call_status;
1113 /*
1114 * Special case: if we've been waiting on the socket's write_space()
1115 * callback, then don't call xprt_end_transmit().
1116 */
1117 if (task->tk_status == -EAGAIN)
1118 return;
1119 xprt_end_transmit(task);
Trond Myklebust940e3312005-11-09 21:45:24 -05001120 rpc_task_force_reencode(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121}
1122
1123/*
1124 * 6. Sort out the RPC call status
1125 */
1126static void
1127call_status(struct rpc_task *task)
1128{
1129 struct rpc_clnt *clnt = task->tk_client;
1130 struct rpc_rqst *req = task->tk_rqstp;
1131 int status;
1132
1133 if (req->rq_received > 0 && !req->rq_bytes_sent)
1134 task->tk_status = req->rq_received;
1135
Chuck Lever46121cf2007-01-31 12:14:08 -05001136 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
1138 status = task->tk_status;
1139 if (status >= 0) {
1140 task->tk_action = call_decode;
1141 return;
1142 }
1143
1144 task->tk_status = 0;
1145 switch(status) {
Trond Myklebust76303992006-08-30 14:32:49 -04001146 case -EHOSTDOWN:
1147 case -EHOSTUNREACH:
1148 case -ENETUNREACH:
1149 /*
1150 * Delay any retries for 3 seconds, then handle as if it
1151 * were a timeout.
1152 */
1153 rpc_delay(task, 3*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 case -ETIMEDOUT:
1155 task->tk_action = call_timeout;
Trond Myklebust241c39b2007-04-20 16:12:55 -04001156 if (task->tk_client->cl_discrtry)
Trond Myklebust3ebb0672007-11-06 18:40:12 -05001157 xprt_force_disconnect(task->tk_xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 break;
1159 case -ECONNREFUSED:
1160 case -ENOTCONN:
Chuck Lever35f5a422006-01-03 09:55:50 +01001161 rpc_force_rebind(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 task->tk_action = call_bind;
1163 break;
1164 case -EAGAIN:
1165 task->tk_action = call_transmit;
1166 break;
1167 case -EIO:
1168 /* shutdown or soft timeout */
1169 rpc_exit(task, status);
1170 break;
1171 default:
Chuck Leverf518e35a2006-01-03 09:55:52 +01001172 printk("%s: RPC call returned error %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 clnt->cl_protname, -status);
1174 rpc_exit(task, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 }
1176}
1177
1178/*
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04001179 * 6a. Handle RPC timeout
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 * We do not release the request slot, so we keep using the
1181 * same XID for all retransmits.
1182 */
1183static void
1184call_timeout(struct rpc_task *task)
1185{
1186 struct rpc_clnt *clnt = task->tk_client;
1187
1188 if (xprt_adjust_timeout(task->tk_rqstp) == 0) {
Chuck Lever46121cf2007-01-31 12:14:08 -05001189 dprintk("RPC: %5u call_timeout (minor)\n", task->tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 goto retry;
1191 }
1192
Chuck Lever46121cf2007-01-31 12:14:08 -05001193 dprintk("RPC: %5u call_timeout (major)\n", task->tk_pid);
Chuck Leveref759a22006-03-20 13:44:17 -05001194 task->tk_timeouts++;
1195
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 if (RPC_IS_SOFT(task)) {
Chuck Leverf518e35a2006-01-03 09:55:52 +01001197 printk(KERN_NOTICE "%s: server %s not responding, timed out\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 clnt->cl_protname, clnt->cl_server);
1199 rpc_exit(task, -EIO);
1200 return;
1201 }
1202
Chuck Leverf518e35a2006-01-03 09:55:52 +01001203 if (!(task->tk_flags & RPC_CALL_MAJORSEEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 task->tk_flags |= RPC_CALL_MAJORSEEN;
1205 printk(KERN_NOTICE "%s: server %s not responding, still trying\n",
1206 clnt->cl_protname, clnt->cl_server);
1207 }
Chuck Lever35f5a422006-01-03 09:55:50 +01001208 rpc_force_rebind(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
1210retry:
1211 clnt->cl_stats->rpcretrans++;
1212 task->tk_action = call_bind;
1213 task->tk_status = 0;
1214}
1215
1216/*
1217 * 7. Decode the RPC reply
1218 */
1219static void
1220call_decode(struct rpc_task *task)
1221{
1222 struct rpc_clnt *clnt = task->tk_client;
1223 struct rpc_rqst *req = task->tk_rqstp;
1224 kxdrproc_t decode = task->tk_msg.rpc_proc->p_decode;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001225 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
Chuck Lever46121cf2007-01-31 12:14:08 -05001227 dprintk("RPC: %5u call_decode (status %d)\n",
1228 task->tk_pid, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
Chuck Leverf518e35a2006-01-03 09:55:52 +01001230 if (task->tk_flags & RPC_CALL_MAJORSEEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 printk(KERN_NOTICE "%s: server %s OK\n",
1232 clnt->cl_protname, clnt->cl_server);
1233 task->tk_flags &= ~RPC_CALL_MAJORSEEN;
1234 }
1235
1236 if (task->tk_status < 12) {
1237 if (!RPC_IS_SOFT(task)) {
1238 task->tk_action = call_bind;
1239 clnt->cl_stats->rpcretrans++;
1240 goto out_retry;
1241 }
Chuck Lever46121cf2007-01-31 12:14:08 -05001242 dprintk("RPC: %s: too small RPC reply size (%d bytes)\n",
1243 clnt->cl_protname, task->tk_status);
Trond Myklebustda458282006-08-31 15:44:52 -04001244 task->tk_action = call_timeout;
1245 goto out_retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 }
1247
Trond Myklebust43ac3f22006-03-20 13:44:51 -05001248 /*
1249 * Ensure that we see all writes made by xprt_complete_rqst()
1250 * before it changed req->rq_received.
1251 */
1252 smp_rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 req->rq_rcv_buf.len = req->rq_private_buf.len;
1254
1255 /* Check that the softirq receive buffer is valid */
1256 WARN_ON(memcmp(&req->rq_rcv_buf, &req->rq_private_buf,
1257 sizeof(req->rq_rcv_buf)) != 0);
1258
1259 /* Verify the RPC header */
Trond Myklebustabbcf282006-01-03 09:55:03 +01001260 p = call_verify(task);
1261 if (IS_ERR(p)) {
1262 if (p == ERR_PTR(-EAGAIN))
1263 goto out_retry;
1264 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 }
1266
Trond Myklebustabbcf282006-01-03 09:55:03 +01001267 task->tk_action = rpc_exit_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
Trond Myklebust6d5fcb52006-10-18 16:01:06 -04001269 if (decode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 task->tk_status = rpcauth_unwrap_resp(task, decode, req, p,
1271 task->tk_msg.rpc_resp);
Trond Myklebust6d5fcb52006-10-18 16:01:06 -04001272 }
Chuck Lever46121cf2007-01-31 12:14:08 -05001273 dprintk("RPC: %5u call_decode result %d\n", task->tk_pid,
1274 task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 return;
1276out_retry:
1277 req->rq_received = req->rq_private_buf.len = 0;
1278 task->tk_status = 0;
Trond Myklebust241c39b2007-04-20 16:12:55 -04001279 if (task->tk_client->cl_discrtry)
Trond Myklebust3ebb0672007-11-06 18:40:12 -05001280 xprt_force_disconnect(task->tk_xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281}
1282
1283/*
1284 * 8. Refresh the credentials if rejected by the server
1285 */
1286static void
1287call_refresh(struct rpc_task *task)
1288{
Chuck Lever46121cf2007-01-31 12:14:08 -05001289 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 task->tk_action = call_refreshresult;
1292 task->tk_status = 0;
1293 task->tk_client->cl_stats->rpcauthrefresh++;
1294 rpcauth_refreshcred(task);
1295}
1296
1297/*
1298 * 8a. Process the results of a credential refresh
1299 */
1300static void
1301call_refreshresult(struct rpc_task *task)
1302{
1303 int status = task->tk_status;
Chuck Lever46121cf2007-01-31 12:14:08 -05001304
1305 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
1307 task->tk_status = 0;
1308 task->tk_action = call_reserve;
1309 if (status >= 0 && rpcauth_uptodatecred(task))
1310 return;
1311 if (status == -EACCES) {
1312 rpc_exit(task, -EACCES);
1313 return;
1314 }
1315 task->tk_action = call_refresh;
1316 if (status != -ETIMEDOUT)
1317 rpc_delay(task, 3*HZ);
1318 return;
1319}
1320
1321/*
1322 * Call header serialization
1323 */
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001324static __be32 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325call_header(struct rpc_task *task)
1326{
1327 struct rpc_clnt *clnt = task->tk_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 struct rpc_rqst *req = task->tk_rqstp;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001329 __be32 *p = req->rq_svec[0].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
1331 /* FIXME: check buffer size? */
Chuck Lever808012f2005-08-25 16:25:49 -07001332
1333 p = xprt_skip_transport_header(task->tk_xprt, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 *p++ = req->rq_xid; /* XID */
1335 *p++ = htonl(RPC_CALL); /* CALL */
1336 *p++ = htonl(RPC_VERSION); /* RPC version */
1337 *p++ = htonl(clnt->cl_prog); /* program number */
1338 *p++ = htonl(clnt->cl_vers); /* program version */
1339 *p++ = htonl(task->tk_msg.rpc_proc->p_proc); /* procedure */
Trond Myklebust334ccfd2005-06-22 17:16:19 +00001340 p = rpcauth_marshcred(task, p);
1341 req->rq_slen = xdr_adjust_iovec(&req->rq_svec[0], p);
1342 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343}
1344
1345/*
1346 * Reply header verification
1347 */
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001348static __be32 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349call_verify(struct rpc_task *task)
1350{
1351 struct kvec *iov = &task->tk_rqstp->rq_rcv_buf.head[0];
1352 int len = task->tk_rqstp->rq_rcv_buf.len >> 2;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001353 __be32 *p = iov->iov_base;
1354 u32 n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 int error = -EACCES;
1356
David Howellse8896492006-08-24 15:44:19 -04001357 if ((task->tk_rqstp->rq_rcv_buf.len & 3) != 0) {
1358 /* RFC-1014 says that the representation of XDR data must be a
1359 * multiple of four bytes
1360 * - if it isn't pointer subtraction in the NFS client may give
1361 * undefined results
1362 */
Trond Myklebust8a702bb2007-06-27 18:30:26 -04001363 dprintk("RPC: %5u %s: XDR representation not a multiple of"
1364 " 4 bytes: 0x%x\n", task->tk_pid, __FUNCTION__,
1365 task->tk_rqstp->rq_rcv_buf.len);
David Howellse8896492006-08-24 15:44:19 -04001366 goto out_eio;
1367 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 if ((len -= 3) < 0)
1369 goto out_overflow;
1370 p += 1; /* skip XID */
1371
1372 if ((n = ntohl(*p++)) != RPC_REPLY) {
Trond Myklebust8a702bb2007-06-27 18:30:26 -04001373 dprintk("RPC: %5u %s: not an RPC reply: %x\n",
1374 task->tk_pid, __FUNCTION__, n);
Trond Myklebustabbcf282006-01-03 09:55:03 +01001375 goto out_garbage;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 }
1377 if ((n = ntohl(*p++)) != RPC_MSG_ACCEPTED) {
1378 if (--len < 0)
1379 goto out_overflow;
1380 switch ((n = ntohl(*p++))) {
1381 case RPC_AUTH_ERROR:
1382 break;
1383 case RPC_MISMATCH:
Chuck Lever46121cf2007-01-31 12:14:08 -05001384 dprintk("RPC: %5u %s: RPC call version "
1385 "mismatch!\n",
1386 task->tk_pid, __FUNCTION__);
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00001387 error = -EPROTONOSUPPORT;
1388 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 default:
Chuck Lever46121cf2007-01-31 12:14:08 -05001390 dprintk("RPC: %5u %s: RPC call rejected, "
1391 "unknown error: %x\n",
1392 task->tk_pid, __FUNCTION__, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 goto out_eio;
1394 }
1395 if (--len < 0)
1396 goto out_overflow;
1397 switch ((n = ntohl(*p++))) {
1398 case RPC_AUTH_REJECTEDCRED:
1399 case RPC_AUTH_REJECTEDVERF:
1400 case RPCSEC_GSS_CREDPROBLEM:
1401 case RPCSEC_GSS_CTXPROBLEM:
1402 if (!task->tk_cred_retry)
1403 break;
1404 task->tk_cred_retry--;
Chuck Lever46121cf2007-01-31 12:14:08 -05001405 dprintk("RPC: %5u %s: retry stale creds\n",
1406 task->tk_pid, __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 rpcauth_invalcred(task);
Trond Myklebust220bcc22007-10-01 12:06:48 -04001408 /* Ensure we obtain a new XID! */
1409 xprt_release(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 task->tk_action = call_refresh;
Trond Myklebustabbcf282006-01-03 09:55:03 +01001411 goto out_retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 case RPC_AUTH_BADCRED:
1413 case RPC_AUTH_BADVERF:
1414 /* possibly garbled cred/verf? */
1415 if (!task->tk_garb_retry)
1416 break;
1417 task->tk_garb_retry--;
Chuck Lever46121cf2007-01-31 12:14:08 -05001418 dprintk("RPC: %5u %s: retry garbled creds\n",
1419 task->tk_pid, __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 task->tk_action = call_bind;
Trond Myklebustabbcf282006-01-03 09:55:03 +01001421 goto out_retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 case RPC_AUTH_TOOWEAK:
Levent Serinol1356b8c2006-03-20 13:44:11 -05001423 printk(KERN_NOTICE "call_verify: server %s requires stronger "
1424 "authentication.\n", task->tk_client->cl_server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 break;
1426 default:
Trond Myklebust8a702bb2007-06-27 18:30:26 -04001427 dprintk("RPC: %5u %s: unknown auth error: %x\n",
1428 task->tk_pid, __FUNCTION__, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 error = -EIO;
1430 }
Chuck Lever46121cf2007-01-31 12:14:08 -05001431 dprintk("RPC: %5u %s: call rejected %d\n",
1432 task->tk_pid, __FUNCTION__, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 goto out_err;
1434 }
1435 if (!(p = rpcauth_checkverf(task, p))) {
Trond Myklebust8a702bb2007-06-27 18:30:26 -04001436 dprintk("RPC: %5u %s: auth check failed\n",
1437 task->tk_pid, __FUNCTION__);
Trond Myklebustabbcf282006-01-03 09:55:03 +01001438 goto out_garbage; /* bad verifier, retry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 }
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001440 len = p - (__be32 *)iov->iov_base - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 if (len < 0)
1442 goto out_overflow;
1443 switch ((n = ntohl(*p++))) {
1444 case RPC_SUCCESS:
1445 return p;
1446 case RPC_PROG_UNAVAIL:
Chuck Lever46121cf2007-01-31 12:14:08 -05001447 dprintk("RPC: %5u %s: program %u is unsupported by server %s\n",
1448 task->tk_pid, __FUNCTION__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 (unsigned int)task->tk_client->cl_prog,
1450 task->tk_client->cl_server);
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00001451 error = -EPFNOSUPPORT;
1452 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 case RPC_PROG_MISMATCH:
Chuck Lever46121cf2007-01-31 12:14:08 -05001454 dprintk("RPC: %5u %s: program %u, version %u unsupported by "
1455 "server %s\n", task->tk_pid, __FUNCTION__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 (unsigned int)task->tk_client->cl_prog,
1457 (unsigned int)task->tk_client->cl_vers,
1458 task->tk_client->cl_server);
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00001459 error = -EPROTONOSUPPORT;
1460 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 case RPC_PROC_UNAVAIL:
Chuck Lever46121cf2007-01-31 12:14:08 -05001462 dprintk("RPC: %5u %s: proc %p unsupported by program %u, "
1463 "version %u on server %s\n",
1464 task->tk_pid, __FUNCTION__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 task->tk_msg.rpc_proc,
1466 task->tk_client->cl_prog,
1467 task->tk_client->cl_vers,
1468 task->tk_client->cl_server);
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00001469 error = -EOPNOTSUPP;
1470 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 case RPC_GARBAGE_ARGS:
Chuck Lever46121cf2007-01-31 12:14:08 -05001472 dprintk("RPC: %5u %s: server saw garbage\n",
1473 task->tk_pid, __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 break; /* retry */
1475 default:
Trond Myklebust8a702bb2007-06-27 18:30:26 -04001476 dprintk("RPC: %5u %s: server accept status: %x\n",
1477 task->tk_pid, __FUNCTION__, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 /* Also retry */
1479 }
1480
Trond Myklebustabbcf282006-01-03 09:55:03 +01001481out_garbage:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 task->tk_client->cl_stats->rpcgarbage++;
1483 if (task->tk_garb_retry) {
1484 task->tk_garb_retry--;
Chuck Lever46121cf2007-01-31 12:14:08 -05001485 dprintk("RPC: %5u %s: retrying\n",
1486 task->tk_pid, __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 task->tk_action = call_bind;
Trond Myklebustabbcf282006-01-03 09:55:03 +01001488out_retry:
1489 return ERR_PTR(-EAGAIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491out_eio:
1492 error = -EIO;
1493out_err:
1494 rpc_exit(task, error);
Trond Myklebust8a702bb2007-06-27 18:30:26 -04001495 dprintk("RPC: %5u %s: call failed with error %d\n", task->tk_pid,
1496 __FUNCTION__, error);
Trond Myklebustabbcf282006-01-03 09:55:03 +01001497 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498out_overflow:
Trond Myklebust8a702bb2007-06-27 18:30:26 -04001499 dprintk("RPC: %5u %s: server reply was truncated.\n", task->tk_pid,
1500 __FUNCTION__);
Trond Myklebustabbcf282006-01-03 09:55:03 +01001501 goto out_garbage;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502}
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00001503
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001504static int rpcproc_encode_null(void *rqstp, __be32 *data, void *obj)
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00001505{
1506 return 0;
1507}
1508
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001509static int rpcproc_decode_null(void *rqstp, __be32 *data, void *obj)
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00001510{
1511 return 0;
1512}
1513
1514static struct rpc_procinfo rpcproc_null = {
1515 .p_encode = rpcproc_encode_null,
1516 .p_decode = rpcproc_decode_null,
1517};
1518
Trond Myklebust64c91a12007-06-23 10:17:16 -04001519static int rpc_ping(struct rpc_clnt *clnt, int flags)
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00001520{
1521 struct rpc_message msg = {
1522 .rpc_proc = &rpcproc_null,
1523 };
1524 int err;
1525 msg.rpc_cred = authnull_ops.lookup_cred(NULL, NULL, 0);
1526 err = rpc_call_sync(clnt, &msg, flags);
1527 put_rpccred(msg.rpc_cred);
1528 return err;
1529}
Trond Myklebust188fef12007-06-16 14:18:40 -04001530
Trond Myklebust5e1550d2007-06-23 10:17:16 -04001531struct rpc_task *rpc_call_null(struct rpc_clnt *clnt, struct rpc_cred *cred, int flags)
1532{
1533 struct rpc_message msg = {
1534 .rpc_proc = &rpcproc_null,
1535 .rpc_cred = cred,
1536 };
Trond Myklebust84115e12007-07-14 15:39:59 -04001537 struct rpc_task_setup task_setup_data = {
1538 .rpc_client = clnt,
1539 .rpc_message = &msg,
1540 .callback_ops = &rpc_default_ops,
1541 .flags = flags,
1542 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001543 return rpc_run_task(&task_setup_data);
Trond Myklebust5e1550d2007-06-23 10:17:16 -04001544}
Trond Myklebuste8914c62007-07-14 15:39:59 -04001545EXPORT_SYMBOL_GPL(rpc_call_null);
Trond Myklebust5e1550d2007-06-23 10:17:16 -04001546
Trond Myklebust188fef12007-06-16 14:18:40 -04001547#ifdef RPC_DEBUG
1548void rpc_show_tasks(void)
1549{
1550 struct rpc_clnt *clnt;
1551 struct rpc_task *t;
1552
1553 spin_lock(&rpc_client_lock);
1554 if (list_empty(&all_clients))
1555 goto out;
1556 printk("-pid- proc flgs status -client- -prog- --rqstp- -timeout "
1557 "-rpcwait -action- ---ops--\n");
1558 list_for_each_entry(clnt, &all_clients, cl_clients) {
1559 if (list_empty(&clnt->cl_tasks))
1560 continue;
1561 spin_lock(&clnt->cl_lock);
1562 list_for_each_entry(t, &clnt->cl_tasks, tk_task) {
1563 const char *rpc_waitq = "none";
Chuck Leverd66968f2007-09-11 18:00:25 -04001564 int proc;
1565
1566 if (t->tk_msg.rpc_proc)
1567 proc = t->tk_msg.rpc_proc->p_proc;
1568 else
1569 proc = -1;
Trond Myklebust188fef12007-06-16 14:18:40 -04001570
1571 if (RPC_IS_QUEUED(t))
1572 rpc_waitq = rpc_qname(t->u.tk_wait.rpc_waitq);
1573
1574 printk("%5u %04d %04x %6d %8p %6d %8p %8ld %8s %8p %8p\n",
Chuck Leverd66968f2007-09-11 18:00:25 -04001575 t->tk_pid, proc,
Trond Myklebust188fef12007-06-16 14:18:40 -04001576 t->tk_flags, t->tk_status,
1577 t->tk_client,
1578 (t->tk_client ? t->tk_client->cl_prog : 0),
1579 t->tk_rqstp, t->tk_timeout,
1580 rpc_waitq,
1581 t->tk_action, t->tk_ops);
1582 }
1583 spin_unlock(&clnt->cl_lock);
1584 }
1585out:
1586 spin_unlock(&rpc_client_lock);
1587}
1588#endif