blob: 0d9b5275fac3af56eb6d5504c3d0a8c9e7cb12be [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;
Chuck Lever43780b82007-07-01 12:13:22 -0400237 char servername[20];
Chuck Leverc2866762006-08-22 20:06:20 -0400238
239 xprt = xprt_create_transport(args->protocol, args->address,
240 args->addrsize, args->timeout);
241 if (IS_ERR(xprt))
242 return (struct rpc_clnt *)xprt;
243
244 /*
Chuck Lever43780b82007-07-01 12:13:22 -0400245 * If the caller chooses not to specify a hostname, whip
246 * up a string representation of the passed-in address.
247 */
248 if (args->servername == NULL) {
249 struct sockaddr_in *addr =
250 (struct sockaddr_in *) &args->address;
251 snprintf(servername, sizeof(servername), NIPQUAD_FMT,
252 NIPQUAD(addr->sin_addr.s_addr));
253 args->servername = servername;
254 }
255
256 /*
Chuck Leverc2866762006-08-22 20:06:20 -0400257 * By default, kernel RPC client connects from a reserved port.
258 * CAP_NET_BIND_SERVICE will not be set for unprivileged requesters,
259 * but it is always enabled for rpciod, which handles the connect
260 * operation.
261 */
262 xprt->resvport = 1;
263 if (args->flags & RPC_CLNT_CREATE_NONPRIVPORT)
264 xprt->resvport = 0;
265
266 dprintk("RPC: creating %s client for %s (xprt %p)\n",
Chuck Lever46121cf2007-01-31 12:14:08 -0500267 args->program->name, args->servername, xprt);
Chuck Leverc2866762006-08-22 20:06:20 -0400268
269 clnt = rpc_new_client(xprt, args->servername, args->program,
270 args->version, args->authflavor);
271 if (IS_ERR(clnt))
272 return clnt;
273
274 if (!(args->flags & RPC_CLNT_CREATE_NOPING)) {
275 int err = rpc_ping(clnt, RPC_TASK_SOFT|RPC_TASK_NOINTR);
276 if (err != 0) {
277 rpc_shutdown_client(clnt);
278 return ERR_PTR(err);
279 }
280 }
281
282 clnt->cl_softrtry = 1;
283 if (args->flags & RPC_CLNT_CREATE_HARDRTRY)
284 clnt->cl_softrtry = 0;
285
286 if (args->flags & RPC_CLNT_CREATE_INTR)
287 clnt->cl_intr = 1;
288 if (args->flags & RPC_CLNT_CREATE_AUTOBIND)
289 clnt->cl_autobind = 1;
Chuck Lever43d78ef2007-02-06 18:26:11 -0500290 if (args->flags & RPC_CLNT_CREATE_DISCRTRY)
291 clnt->cl_discrtry = 1;
Chuck Leverc2866762006-08-22 20:06:20 -0400292
293 return clnt;
294}
Chuck Leverb86acd52006-08-22 20:06:22 -0400295EXPORT_SYMBOL_GPL(rpc_create);
Chuck Leverc2866762006-08-22 20:06:20 -0400296
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297/*
298 * This function clones the RPC client structure. It allows us to share the
299 * same transport while varying parameters such as the authentication
300 * flavour.
301 */
302struct rpc_clnt *
303rpc_clone_client(struct rpc_clnt *clnt)
304{
305 struct rpc_clnt *new;
Trond Myklebust3e32a5d2006-11-16 11:37:27 -0500306 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Arnaldo Carvalho de Meloe69062b2006-11-21 01:21:34 -0200308 new = kmemdup(clnt, sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 if (!new)
310 goto out_no_clnt;
Trond Myklebustd431a5552007-06-17 17:07:54 -0400311 new->cl_parent = clnt;
312 /* Turn off autobind on clones */
313 new->cl_autobind = 0;
314 INIT_LIST_HEAD(&new->cl_tasks);
315 spin_lock_init(&new->cl_lock);
316 rpc_init_rtt(&new->cl_rtt_default, clnt->cl_xprt->timeout.to_initval);
Trond Myklebust23bf85b2006-11-21 10:40:23 -0500317 new->cl_metrics = rpc_alloc_iostats(clnt);
318 if (new->cl_metrics == NULL)
319 goto out_no_stats;
Trond Myklebust34f52e32007-06-14 16:40:31 -0400320 kref_init(&new->cl_kref);
Trond Myklebust3e32a5d2006-11-16 11:37:27 -0500321 err = rpc_setup_pipedir(new, clnt->cl_program->pipe_dir_name);
322 if (err != 0)
323 goto out_no_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 if (new->cl_auth)
325 atomic_inc(&new->cl_auth->au_count);
Trond Myklebustd431a5552007-06-17 17:07:54 -0400326 xprt_get(clnt->cl_xprt);
327 kref_get(&clnt->cl_kref);
Trond Myklebust6529eba2007-06-14 16:40:14 -0400328 rpc_register_client(new);
Trond Myklebust4ada5392007-06-14 17:26:17 -0400329 rpciod_up();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 return new;
Trond Myklebust3e32a5d2006-11-16 11:37:27 -0500331out_no_path:
332 rpc_free_iostats(new->cl_metrics);
Trond Myklebust23bf85b2006-11-21 10:40:23 -0500333out_no_stats:
334 kfree(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335out_no_clnt:
Chuck Lever46121cf2007-01-31 12:14:08 -0500336 dprintk("RPC: %s: returned error %d\n", __FUNCTION__, err);
Trond Myklebust3e32a5d2006-11-16 11:37:27 -0500337 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338}
339
340/*
341 * Properly shut down an RPC client, terminating all outstanding
Trond Myklebust90c57552007-06-09 19:49:36 -0400342 * requests.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 */
Trond Myklebust4c402b42007-06-14 16:40:32 -0400344void rpc_shutdown_client(struct rpc_clnt *clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
Trond Myklebust34f52e32007-06-14 16:40:31 -0400346 dprintk("RPC: shutting down %s client for %s\n",
347 clnt->cl_protname, clnt->cl_server);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Trond Myklebust34f52e32007-06-14 16:40:31 -0400349 while (!list_empty(&clnt->cl_tasks)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 rpc_killall_tasks(clnt);
Ingo Molnar532347e2006-01-09 20:52:53 -0800351 wait_event_timeout(destroy_wait,
Trond Myklebust34f52e32007-06-14 16:40:31 -0400352 list_empty(&clnt->cl_tasks), 1*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 }
354
Trond Myklebust4c402b42007-06-14 16:40:32 -0400355 rpc_release_client(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356}
357
358/*
Trond Myklebust34f52e32007-06-14 16:40:31 -0400359 * Free an RPC client
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 */
Trond Myklebust34f52e32007-06-14 16:40:31 -0400361static void
362rpc_free_client(struct kref *kref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363{
Trond Myklebust34f52e32007-06-14 16:40:31 -0400364 struct rpc_clnt *clnt = container_of(kref, struct rpc_clnt, cl_kref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Chuck Lever46121cf2007-01-31 12:14:08 -0500366 dprintk("RPC: destroying %s client for %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 clnt->cl_protname, clnt->cl_server);
Trond Myklebust8f8e7a52006-08-14 13:11:15 -0400368 if (!IS_ERR(clnt->cl_dentry)) {
Trond Myklebustdff02cc2006-07-31 14:17:18 -0700369 rpc_rmdir(clnt->cl_dentry);
Trond Myklebust8f8e7a52006-08-14 13:11:15 -0400370 rpc_put_mount();
371 }
Trond Myklebust3e32a5d2006-11-16 11:37:27 -0500372 if (clnt->cl_parent != clnt) {
Trond Myklebust8ad7c892007-06-14 16:40:32 -0400373 rpc_release_client(clnt->cl_parent);
Trond Myklebust3e32a5d2006-11-16 11:37:27 -0500374 goto out_free;
375 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 if (clnt->cl_server != clnt->cl_inline_name)
377 kfree(clnt->cl_server);
378out_free:
Trond Myklebust6529eba2007-06-14 16:40:14 -0400379 rpc_unregister_client(clnt);
Chuck Lever11c556b2006-03-20 13:44:22 -0500380 rpc_free_iostats(clnt->cl_metrics);
381 clnt->cl_metrics = NULL;
Trond Myklebust6b6ca862006-09-05 12:55:57 -0400382 xprt_put(clnt->cl_xprt);
Trond Myklebust4ada5392007-06-14 17:26:17 -0400383 rpciod_down();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 kfree(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385}
386
387/*
Trond Myklebust1dd17ec2007-06-26 16:57:41 -0400388 * Free an RPC client
389 */
390static void
391rpc_free_auth(struct kref *kref)
392{
393 struct rpc_clnt *clnt = container_of(kref, struct rpc_clnt, cl_kref);
394
395 if (clnt->cl_auth == NULL) {
396 rpc_free_client(kref);
397 return;
398 }
399
400 /*
401 * Note: RPCSEC_GSS may need to send NULL RPC calls in order to
402 * release remaining GSS contexts. This mechanism ensures
403 * that it can do so safely.
404 */
405 kref_init(kref);
406 rpcauth_release(clnt->cl_auth);
407 clnt->cl_auth = NULL;
408 kref_put(kref, rpc_free_client);
409}
410
411/*
Trond Myklebust34f52e32007-06-14 16:40:31 -0400412 * Release reference to the RPC client
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 */
414void
415rpc_release_client(struct rpc_clnt *clnt)
416{
Trond Myklebust34f52e32007-06-14 16:40:31 -0400417 dprintk("RPC: rpc_release_client(%p)\n", clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Trond Myklebust34f52e32007-06-14 16:40:31 -0400419 if (list_empty(&clnt->cl_tasks))
420 wake_up(&destroy_wait);
Trond Myklebust1dd17ec2007-06-26 16:57:41 -0400421 kref_put(&clnt->cl_kref, rpc_free_auth);
Trond Myklebust34f52e32007-06-14 16:40:31 -0400422}
423
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000424/**
425 * rpc_bind_new_program - bind a new RPC program to an existing client
426 * @old - old rpc_client
427 * @program - rpc program to set
428 * @vers - rpc program version
429 *
430 * Clones the rpc client and sets up a new RPC program. This is mainly
431 * of use for enabling different RPC programs to share the same transport.
432 * The Sun NFSv2/v3 ACL protocol can do this.
433 */
434struct rpc_clnt *rpc_bind_new_program(struct rpc_clnt *old,
435 struct rpc_program *program,
436 int vers)
437{
438 struct rpc_clnt *clnt;
439 struct rpc_version *version;
440 int err;
441
442 BUG_ON(vers >= program->nrvers || !program->version[vers]);
443 version = program->version[vers];
444 clnt = rpc_clone_client(old);
445 if (IS_ERR(clnt))
446 goto out;
447 clnt->cl_procinfo = version->procs;
448 clnt->cl_maxproc = version->nrprocs;
449 clnt->cl_protname = program->name;
450 clnt->cl_prog = program->number;
451 clnt->cl_vers = version->number;
452 clnt->cl_stats = program->stats;
453 err = rpc_ping(clnt, RPC_TASK_SOFT|RPC_TASK_NOINTR);
454 if (err != 0) {
455 rpc_shutdown_client(clnt);
456 clnt = ERR_PTR(err);
457 }
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800458out:
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000459 return clnt;
460}
461
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462/*
463 * Default callback for async RPC calls
464 */
465static void
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100466rpc_default_callback(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
468}
469
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100470static const struct rpc_call_ops rpc_default_ops = {
471 .rpc_call_done = rpc_default_callback,
472};
473
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474/*
Trond Myklebust14b218a2005-06-22 17:16:28 +0000475 * Export the signal mask handling for synchronous code that
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 * sleeps on RPC calls
477 */
Trond Myklebust2bd61572006-01-03 09:55:19 +0100478#define RPC_INTR_SIGNALS (sigmask(SIGHUP) | sigmask(SIGINT) | sigmask(SIGQUIT) | sigmask(SIGTERM))
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800479
Trond Myklebust14b218a2005-06-22 17:16:28 +0000480static void rpc_save_sigmask(sigset_t *oldset, int intr)
481{
Trond Myklebust2bd61572006-01-03 09:55:19 +0100482 unsigned long sigallow = sigmask(SIGKILL);
Trond Myklebust14b218a2005-06-22 17:16:28 +0000483 sigset_t sigmask;
484
485 /* Block all signals except those listed in sigallow */
486 if (intr)
487 sigallow |= RPC_INTR_SIGNALS;
488 siginitsetinv(&sigmask, sigallow);
489 sigprocmask(SIG_BLOCK, &sigmask, oldset);
490}
491
492static inline void rpc_task_sigmask(struct rpc_task *task, sigset_t *oldset)
493{
494 rpc_save_sigmask(oldset, !RPC_TASK_UNINTERRUPTIBLE(task));
495}
496
497static inline void rpc_restore_sigmask(sigset_t *oldset)
498{
499 sigprocmask(SIG_SETMASK, oldset, NULL);
500}
501
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502void rpc_clnt_sigmask(struct rpc_clnt *clnt, sigset_t *oldset)
503{
Trond Myklebust14b218a2005-06-22 17:16:28 +0000504 rpc_save_sigmask(oldset, clnt->cl_intr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505}
506
507void rpc_clnt_sigunmask(struct rpc_clnt *clnt, sigset_t *oldset)
508{
Trond Myklebust14b218a2005-06-22 17:16:28 +0000509 rpc_restore_sigmask(oldset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510}
511
Trond Myklebust6e5b70e2007-06-12 10:02:37 -0400512static
513struct rpc_task *rpc_do_run_task(struct rpc_clnt *clnt,
514 struct rpc_message *msg,
515 int flags,
516 const struct rpc_call_ops *ops,
517 void *data)
518{
519 struct rpc_task *task, *ret;
520 sigset_t oldset;
521
522 task = rpc_new_task(clnt, flags, ops, data);
523 if (task == NULL) {
524 rpc_release_calldata(ops, data);
525 return ERR_PTR(-ENOMEM);
526 }
527
528 /* Mask signals on synchronous RPC calls and RPCSEC_GSS upcalls */
529 rpc_task_sigmask(task, &oldset);
530 if (msg != NULL) {
531 rpc_call_setup(task, msg, 0);
532 if (task->tk_status != 0) {
533 ret = ERR_PTR(task->tk_status);
534 rpc_put_task(task);
535 goto out;
536 }
537 }
538 atomic_inc(&task->tk_count);
539 rpc_execute(task);
540 ret = task;
541out:
542 rpc_restore_sigmask(&oldset);
543 return ret;
544}
545
546/**
547 * rpc_call_sync - Perform a synchronous RPC call
548 * @clnt: pointer to RPC client
549 * @msg: RPC call parameters
550 * @flags: RPC call flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 */
552int rpc_call_sync(struct rpc_clnt *clnt, struct rpc_message *msg, int flags)
553{
554 struct rpc_task *task;
Trond Myklebust6e5b70e2007-06-12 10:02:37 -0400555 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 BUG_ON(flags & RPC_TASK_ASYNC);
558
Trond Myklebust6e5b70e2007-06-12 10:02:37 -0400559 task = rpc_do_run_task(clnt, msg, flags, &rpc_default_ops, NULL);
560 if (IS_ERR(task))
561 return PTR_ERR(task);
Trond Myklebuste60859a2006-01-03 09:55:10 +0100562 status = task->tk_status;
Trond Myklebustbde8f002007-01-24 11:54:53 -0800563 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 return status;
565}
566
Trond Myklebust6e5b70e2007-06-12 10:02:37 -0400567/**
568 * rpc_call_async - Perform an asynchronous RPC call
569 * @clnt: pointer to RPC client
570 * @msg: RPC call parameters
571 * @flags: RPC call flags
572 * @ops: RPC call ops
573 * @data: user call data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 */
575int
576rpc_call_async(struct rpc_clnt *clnt, struct rpc_message *msg, int flags,
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100577 const struct rpc_call_ops *tk_ops, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578{
579 struct rpc_task *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
Trond Myklebust6e5b70e2007-06-12 10:02:37 -0400581 task = rpc_do_run_task(clnt, msg, flags|RPC_TASK_ASYNC, tk_ops, data);
582 if (IS_ERR(task))
583 return PTR_ERR(task);
584 rpc_put_task(task);
585 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586}
587
Trond Myklebust6e5b70e2007-06-12 10:02:37 -0400588/**
589 * rpc_run_task - Allocate a new RPC task, then run rpc_execute against it
590 * @clnt: pointer to RPC client
591 * @flags: RPC flags
592 * @ops: RPC call ops
593 * @data: user call data
594 */
595struct rpc_task *rpc_run_task(struct rpc_clnt *clnt, int flags,
596 const struct rpc_call_ops *tk_ops,
597 void *data)
598{
599 return rpc_do_run_task(clnt, NULL, flags, tk_ops, data);
600}
601EXPORT_SYMBOL(rpc_run_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
603void
604rpc_call_setup(struct rpc_task *task, struct rpc_message *msg, int flags)
605{
606 task->tk_msg = *msg;
607 task->tk_flags |= flags;
608 /* Bind the user cred */
609 if (task->tk_msg.rpc_cred != NULL)
610 rpcauth_holdcred(task);
611 else
612 rpcauth_bindcred(task);
613
614 if (task->tk_status == 0)
615 task->tk_action = call_start;
616 else
Trond Myklebustabbcf282006-01-03 09:55:03 +0100617 task->tk_action = rpc_exit_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618}
619
Chuck Levered394402006-08-22 20:06:17 -0400620/**
621 * rpc_peeraddr - extract remote peer address from clnt's xprt
622 * @clnt: RPC client structure
623 * @buf: target buffer
624 * @size: length of target buffer
625 *
626 * Returns the number of bytes that are actually in the stored address.
627 */
628size_t rpc_peeraddr(struct rpc_clnt *clnt, struct sockaddr *buf, size_t bufsize)
629{
630 size_t bytes;
631 struct rpc_xprt *xprt = clnt->cl_xprt;
632
633 bytes = sizeof(xprt->addr);
634 if (bytes > bufsize)
635 bytes = bufsize;
636 memcpy(buf, &clnt->cl_xprt->addr, bytes);
Chuck Leverc4efcb12006-08-22 20:06:19 -0400637 return xprt->addrlen;
Chuck Levered394402006-08-22 20:06:17 -0400638}
Chuck Leverb86acd52006-08-22 20:06:22 -0400639EXPORT_SYMBOL_GPL(rpc_peeraddr);
Chuck Levered394402006-08-22 20:06:17 -0400640
Chuck Leverf425eba2006-08-22 20:06:18 -0400641/**
642 * rpc_peeraddr2str - return remote peer address in printable format
643 * @clnt: RPC client structure
644 * @format: address format
645 *
646 */
647char *rpc_peeraddr2str(struct rpc_clnt *clnt, enum rpc_display_format_t format)
648{
649 struct rpc_xprt *xprt = clnt->cl_xprt;
Chuck Lever7559c7a2006-12-05 16:35:37 -0500650
651 if (xprt->address_strings[format] != NULL)
652 return xprt->address_strings[format];
653 else
654 return "unprintable";
Chuck Leverf425eba2006-08-22 20:06:18 -0400655}
Chuck Leverb86acd52006-08-22 20:06:22 -0400656EXPORT_SYMBOL_GPL(rpc_peeraddr2str);
Chuck Leverf425eba2006-08-22 20:06:18 -0400657
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658void
659rpc_setbufsize(struct rpc_clnt *clnt, unsigned int sndsize, unsigned int rcvsize)
660{
661 struct rpc_xprt *xprt = clnt->cl_xprt;
Chuck Lever470056c2005-08-25 16:25:56 -0700662 if (xprt->ops->set_buffer_size)
663 xprt->ops->set_buffer_size(xprt, sndsize, rcvsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664}
665
666/*
667 * Return size of largest payload RPC client can support, in bytes
668 *
669 * For stream transports, this is one RPC record fragment (see RFC
670 * 1831), as we don't support multi-record requests yet. For datagram
671 * transports, this is the size of an IP packet minus the IP, UDP, and
672 * RPC header sizes.
673 */
674size_t rpc_max_payload(struct rpc_clnt *clnt)
675{
676 return clnt->cl_xprt->max_payload;
677}
Chuck Leverb86acd52006-08-22 20:06:22 -0400678EXPORT_SYMBOL_GPL(rpc_max_payload);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
Chuck Lever35f5a422006-01-03 09:55:50 +0100680/**
681 * rpc_force_rebind - force transport to check that remote port is unchanged
682 * @clnt: client to rebind
683 *
684 */
685void rpc_force_rebind(struct rpc_clnt *clnt)
686{
687 if (clnt->cl_autobind)
Chuck Leverec739ef2006-08-22 20:06:15 -0400688 xprt_clear_bound(clnt->cl_xprt);
Chuck Lever35f5a422006-01-03 09:55:50 +0100689}
Chuck Leverb86acd52006-08-22 20:06:22 -0400690EXPORT_SYMBOL_GPL(rpc_force_rebind);
Chuck Lever35f5a422006-01-03 09:55:50 +0100691
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692/*
693 * Restart an (async) RPC call. Usually called from within the
694 * exit handler.
695 */
696void
697rpc_restart_call(struct rpc_task *task)
698{
699 if (RPC_ASSASSINATED(task))
700 return;
701
702 task->tk_action = call_start;
703}
704
705/*
706 * 0. Initial state
707 *
708 * Other FSM states can be visited zero or more times, but
709 * this state is visited exactly once for each RPC.
710 */
711static void
712call_start(struct rpc_task *task)
713{
714 struct rpc_clnt *clnt = task->tk_client;
715
Chuck Lever46121cf2007-01-31 12:14:08 -0500716 dprintk("RPC: %5u call_start %s%d proc %d (%s)\n", task->tk_pid,
717 clnt->cl_protname, clnt->cl_vers,
718 task->tk_msg.rpc_proc->p_proc,
719 (RPC_IS_ASYNC(task) ? "async" : "sync"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
721 /* Increment call count */
722 task->tk_msg.rpc_proc->p_count++;
723 clnt->cl_stats->rpccnt++;
724 task->tk_action = call_reserve;
725}
726
727/*
728 * 1. Reserve an RPC call slot
729 */
730static void
731call_reserve(struct rpc_task *task)
732{
Chuck Lever46121cf2007-01-31 12:14:08 -0500733 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
735 if (!rpcauth_uptodatecred(task)) {
736 task->tk_action = call_refresh;
737 return;
738 }
739
740 task->tk_status = 0;
741 task->tk_action = call_reserveresult;
742 xprt_reserve(task);
743}
744
745/*
746 * 1b. Grok the result of xprt_reserve()
747 */
748static void
749call_reserveresult(struct rpc_task *task)
750{
751 int status = task->tk_status;
752
Chuck Lever46121cf2007-01-31 12:14:08 -0500753 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
755 /*
756 * After a call to xprt_reserve(), we must have either
757 * a request slot or else an error status.
758 */
759 task->tk_status = 0;
760 if (status >= 0) {
761 if (task->tk_rqstp) {
762 task->tk_action = call_allocate;
763 return;
764 }
765
766 printk(KERN_ERR "%s: status=%d, but no request slot, exiting\n",
767 __FUNCTION__, status);
768 rpc_exit(task, -EIO);
769 return;
770 }
771
772 /*
773 * Even though there was an error, we may have acquired
774 * a request slot somehow. Make sure not to leak it.
775 */
776 if (task->tk_rqstp) {
777 printk(KERN_ERR "%s: status=%d, request allocated anyway\n",
778 __FUNCTION__, status);
779 xprt_release(task);
780 }
781
782 switch (status) {
783 case -EAGAIN: /* woken up; retry */
784 task->tk_action = call_reserve;
785 return;
786 case -EIO: /* probably a shutdown */
787 break;
788 default:
789 printk(KERN_ERR "%s: unrecognized error %d, exiting\n",
790 __FUNCTION__, status);
791 break;
792 }
793 rpc_exit(task, status);
794}
795
796/*
797 * 2. Allocate the buffer. For details, see sched.c:rpc_malloc.
Chuck Lever02107142006-01-03 09:55:49 +0100798 * (Note: buffer memory is freed in xprt_release).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 */
800static void
801call_allocate(struct rpc_task *task)
802{
Trond Myklebust1be27f32007-06-27 14:29:04 -0400803 unsigned int slack = task->tk_msg.rpc_cred->cr_auth->au_cslack;
Chuck Lever02107142006-01-03 09:55:49 +0100804 struct rpc_rqst *req = task->tk_rqstp;
805 struct rpc_xprt *xprt = task->tk_xprt;
Chuck Lever2bea90d2007-03-29 16:47:53 -0400806 struct rpc_procinfo *proc = task->tk_msg.rpc_proc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
Chuck Lever46121cf2007-01-31 12:14:08 -0500808 dprint_status(task);
809
Chuck Lever2bea90d2007-03-29 16:47:53 -0400810 task->tk_status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 task->tk_action = call_bind;
Chuck Lever2bea90d2007-03-29 16:47:53 -0400812
Chuck Lever02107142006-01-03 09:55:49 +0100813 if (req->rq_buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 return;
815
Chuck Lever2bea90d2007-03-29 16:47:53 -0400816 if (proc->p_proc != 0) {
817 BUG_ON(proc->p_arglen == 0);
818 if (proc->p_decode != NULL)
819 BUG_ON(proc->p_replen == 0);
820 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
Chuck Lever2bea90d2007-03-29 16:47:53 -0400822 /*
823 * Calculate the size (in quads) of the RPC call
824 * and reply headers, and convert both values
825 * to byte sizes.
826 */
827 req->rq_callsize = RPC_CALLHDRSIZE + (slack << 1) + proc->p_arglen;
828 req->rq_callsize <<= 2;
829 req->rq_rcvsize = RPC_REPHDRSIZE + slack + proc->p_replen;
830 req->rq_rcvsize <<= 2;
831
Chuck Leverc5a4dd82007-03-29 16:47:58 -0400832 req->rq_buffer = xprt->ops->buf_alloc(task,
833 req->rq_callsize + req->rq_rcvsize);
Chuck Lever2bea90d2007-03-29 16:47:53 -0400834 if (req->rq_buffer != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 return;
Chuck Lever46121cf2007-01-31 12:14:08 -0500836
837 dprintk("RPC: %5u rpc_buffer allocation failed\n", task->tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
Trond Myklebust14b218a2005-06-22 17:16:28 +0000839 if (RPC_IS_ASYNC(task) || !signalled()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 xprt_release(task);
841 task->tk_action = call_reserve;
842 rpc_delay(task, HZ>>4);
843 return;
844 }
845
846 rpc_exit(task, -ERESTARTSYS);
847}
848
Trond Myklebust940e3312005-11-09 21:45:24 -0500849static inline int
850rpc_task_need_encode(struct rpc_task *task)
851{
852 return task->tk_rqstp->rq_snd_buf.len == 0;
853}
854
855static inline void
856rpc_task_force_reencode(struct rpc_task *task)
857{
858 task->tk_rqstp->rq_snd_buf.len = 0;
859}
860
Chuck Lever2bea90d2007-03-29 16:47:53 -0400861static inline void
862rpc_xdr_buf_init(struct xdr_buf *buf, void *start, size_t len)
863{
864 buf->head[0].iov_base = start;
865 buf->head[0].iov_len = len;
866 buf->tail[0].iov_len = 0;
867 buf->page_len = 0;
868 buf->len = 0;
869 buf->buflen = len;
870}
871
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872/*
873 * 3. Encode arguments of an RPC call
874 */
875static void
876call_encode(struct rpc_task *task)
877{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 struct rpc_rqst *req = task->tk_rqstp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 kxdrproc_t encode;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700880 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
Chuck Lever46121cf2007-01-31 12:14:08 -0500882 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
Chuck Lever2bea90d2007-03-29 16:47:53 -0400884 rpc_xdr_buf_init(&req->rq_snd_buf,
885 req->rq_buffer,
886 req->rq_callsize);
887 rpc_xdr_buf_init(&req->rq_rcv_buf,
888 (char *)req->rq_buffer + req->rq_callsize,
889 req->rq_rcvsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
891 /* Encode header and provided arguments */
892 encode = task->tk_msg.rpc_proc->p_encode;
893 if (!(p = call_header(task))) {
894 printk(KERN_INFO "RPC: call_header failed, exit EIO\n");
895 rpc_exit(task, -EIO);
896 return;
897 }
J. Bruce Fieldsf3680312005-10-13 16:54:48 -0400898 if (encode == NULL)
899 return;
900
Trond Myklebust6d5fcb52006-10-18 16:01:06 -0400901 lock_kernel();
J. Bruce Fieldsf3680312005-10-13 16:54:48 -0400902 task->tk_status = rpcauth_wrap_req(task, encode, req, p,
903 task->tk_msg.rpc_argp);
Trond Myklebust6d5fcb52006-10-18 16:01:06 -0400904 unlock_kernel();
J. Bruce Fieldsf3680312005-10-13 16:54:48 -0400905 if (task->tk_status == -ENOMEM) {
906 /* XXX: Is this sane? */
907 rpc_delay(task, 3*HZ);
908 task->tk_status = -EAGAIN;
909 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910}
911
912/*
913 * 4. Get the server port number if not yet set
914 */
915static void
916call_bind(struct rpc_task *task)
917{
Chuck Leverec739ef2006-08-22 20:06:15 -0400918 struct rpc_xprt *xprt = task->tk_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
Chuck Lever46121cf2007-01-31 12:14:08 -0500920 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
Chuck Leverda351872005-08-11 16:25:11 -0400922 task->tk_action = call_connect;
Chuck Leverec739ef2006-08-22 20:06:15 -0400923 if (!xprt_bound(xprt)) {
Chuck Leverda351872005-08-11 16:25:11 -0400924 task->tk_action = call_bind_status;
Chuck Leverec739ef2006-08-22 20:06:15 -0400925 task->tk_timeout = xprt->bind_timeout;
Chuck Leverbbf7c1d2006-08-22 20:06:16 -0400926 xprt->ops->rpcbind(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 }
928}
929
930/*
Chuck Leverda351872005-08-11 16:25:11 -0400931 * 4a. Sort out bind result
932 */
933static void
934call_bind_status(struct rpc_task *task)
935{
936 int status = -EACCES;
937
938 if (task->tk_status >= 0) {
Chuck Lever46121cf2007-01-31 12:14:08 -0500939 dprint_status(task);
Chuck Leverda351872005-08-11 16:25:11 -0400940 task->tk_status = 0;
941 task->tk_action = call_connect;
942 return;
943 }
944
945 switch (task->tk_status) {
946 case -EACCES:
Chuck Lever46121cf2007-01-31 12:14:08 -0500947 dprintk("RPC: %5u remote rpcbind: RPC program/version "
948 "unavailable\n", task->tk_pid);
Chuck Leverea635a52005-10-06 23:12:58 -0400949 rpc_delay(task, 3*HZ);
Trond Myklebustda458282006-08-31 15:44:52 -0400950 goto retry_timeout;
Chuck Leverda351872005-08-11 16:25:11 -0400951 case -ETIMEDOUT:
Chuck Lever46121cf2007-01-31 12:14:08 -0500952 dprintk("RPC: %5u rpcbind request timed out\n",
Chuck Leverda351872005-08-11 16:25:11 -0400953 task->tk_pid);
Trond Myklebustda458282006-08-31 15:44:52 -0400954 goto retry_timeout;
Chuck Leverda351872005-08-11 16:25:11 -0400955 case -EPFNOSUPPORT:
Chuck Lever46121cf2007-01-31 12:14:08 -0500956 dprintk("RPC: %5u remote rpcbind service unavailable\n",
Chuck Leverda351872005-08-11 16:25:11 -0400957 task->tk_pid);
958 break;
959 case -EPROTONOSUPPORT:
Chuck Lever00a6e7b2007-03-29 16:48:33 -0400960 dprintk("RPC: %5u remote rpcbind version unavailable, retrying\n",
Chuck Leverda351872005-08-11 16:25:11 -0400961 task->tk_pid);
Chuck Lever00a6e7b2007-03-29 16:48:33 -0400962 task->tk_status = 0;
963 task->tk_action = call_bind;
964 return;
Chuck Leverda351872005-08-11 16:25:11 -0400965 default:
Chuck Lever46121cf2007-01-31 12:14:08 -0500966 dprintk("RPC: %5u unrecognized rpcbind error (%d)\n",
Chuck Leverda351872005-08-11 16:25:11 -0400967 task->tk_pid, -task->tk_status);
968 status = -EIO;
Chuck Leverda351872005-08-11 16:25:11 -0400969 }
970
971 rpc_exit(task, status);
972 return;
973
Trond Myklebustda458282006-08-31 15:44:52 -0400974retry_timeout:
975 task->tk_action = call_timeout;
Chuck Leverda351872005-08-11 16:25:11 -0400976}
977
978/*
979 * 4b. Connect to the RPC server
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 */
981static void
982call_connect(struct rpc_task *task)
983{
Chuck Leverda351872005-08-11 16:25:11 -0400984 struct rpc_xprt *xprt = task->tk_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
Chuck Lever46121cf2007-01-31 12:14:08 -0500986 dprintk("RPC: %5u call_connect xprt %p %s connected\n",
Chuck Leverda351872005-08-11 16:25:11 -0400987 task->tk_pid, xprt,
988 (xprt_connected(xprt) ? "is" : "is not"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
Chuck Leverda351872005-08-11 16:25:11 -0400990 task->tk_action = call_transmit;
991 if (!xprt_connected(xprt)) {
992 task->tk_action = call_connect_status;
993 if (task->tk_status < 0)
994 return;
995 xprt_connect(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997}
998
999/*
Chuck Leverda351872005-08-11 16:25:11 -04001000 * 4c. Sort out connect result
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 */
1002static void
1003call_connect_status(struct rpc_task *task)
1004{
1005 struct rpc_clnt *clnt = task->tk_client;
1006 int status = task->tk_status;
1007
Chuck Lever46121cf2007-01-31 12:14:08 -05001008 dprint_status(task);
Chuck Leverda351872005-08-11 16:25:11 -04001009
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 task->tk_status = 0;
1011 if (status >= 0) {
1012 clnt->cl_stats->netreconn++;
1013 task->tk_action = call_transmit;
1014 return;
1015 }
1016
Chuck Leverda351872005-08-11 16:25:11 -04001017 /* Something failed: remote service port may have changed */
Chuck Lever35f5a422006-01-03 09:55:50 +01001018 rpc_force_rebind(clnt);
Chuck Leverda351872005-08-11 16:25:11 -04001019
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 switch (status) {
1021 case -ENOTCONN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 case -EAGAIN:
Chuck Leverda351872005-08-11 16:25:11 -04001023 task->tk_action = call_bind;
Trond Myklebustda458282006-08-31 15:44:52 -04001024 if (!RPC_IS_SOFT(task))
1025 return;
1026 /* if soft mounted, test if we've timed out */
1027 case -ETIMEDOUT:
1028 task->tk_action = call_timeout;
1029 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 }
Trond Myklebustda458282006-08-31 15:44:52 -04001031 rpc_exit(task, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032}
1033
1034/*
1035 * 5. Transmit the RPC request, and wait for reply
1036 */
1037static void
1038call_transmit(struct rpc_task *task)
1039{
Chuck Lever46121cf2007-01-31 12:14:08 -05001040 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
1042 task->tk_action = call_status;
1043 if (task->tk_status < 0)
1044 return;
1045 task->tk_status = xprt_prepare_transmit(task);
1046 if (task->tk_status != 0)
1047 return;
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04001048 task->tk_action = call_transmit_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 /* Encode here so that rpcsec_gss can use correct sequence number. */
Trond Myklebust940e3312005-11-09 21:45:24 -05001050 if (rpc_task_need_encode(task)) {
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04001051 BUG_ON(task->tk_rqstp->rq_bytes_sent != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 call_encode(task);
Trond Myklebust5e5ce5b2005-10-18 14:20:11 -07001053 /* Did the encode result in an error condition? */
1054 if (task->tk_status != 0)
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04001055 return;
Trond Myklebust5e5ce5b2005-10-18 14:20:11 -07001056 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 xprt_transmit(task);
1058 if (task->tk_status < 0)
1059 return;
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04001060 /*
1061 * On success, ensure that we call xprt_end_transmit() before sleeping
1062 * in order to allow access to the socket to other RPC requests.
1063 */
1064 call_transmit_status(task);
1065 if (task->tk_msg.rpc_proc->p_decode != NULL)
1066 return;
1067 task->tk_action = rpc_exit_task;
1068 rpc_wake_up_task(task);
1069}
1070
1071/*
1072 * 5a. Handle cleanup after a transmission
1073 */
1074static void
1075call_transmit_status(struct rpc_task *task)
1076{
1077 task->tk_action = call_status;
1078 /*
1079 * Special case: if we've been waiting on the socket's write_space()
1080 * callback, then don't call xprt_end_transmit().
1081 */
1082 if (task->tk_status == -EAGAIN)
1083 return;
1084 xprt_end_transmit(task);
Trond Myklebust940e3312005-11-09 21:45:24 -05001085 rpc_task_force_reencode(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086}
1087
1088/*
1089 * 6. Sort out the RPC call status
1090 */
1091static void
1092call_status(struct rpc_task *task)
1093{
1094 struct rpc_clnt *clnt = task->tk_client;
1095 struct rpc_rqst *req = task->tk_rqstp;
1096 int status;
1097
1098 if (req->rq_received > 0 && !req->rq_bytes_sent)
1099 task->tk_status = req->rq_received;
1100
Chuck Lever46121cf2007-01-31 12:14:08 -05001101 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
1103 status = task->tk_status;
1104 if (status >= 0) {
1105 task->tk_action = call_decode;
1106 return;
1107 }
1108
1109 task->tk_status = 0;
1110 switch(status) {
Trond Myklebust76303992006-08-30 14:32:49 -04001111 case -EHOSTDOWN:
1112 case -EHOSTUNREACH:
1113 case -ENETUNREACH:
1114 /*
1115 * Delay any retries for 3 seconds, then handle as if it
1116 * were a timeout.
1117 */
1118 rpc_delay(task, 3*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 case -ETIMEDOUT:
1120 task->tk_action = call_timeout;
Trond Myklebust241c39b2007-04-20 16:12:55 -04001121 if (task->tk_client->cl_discrtry)
1122 xprt_disconnect(task->tk_xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 break;
1124 case -ECONNREFUSED:
1125 case -ENOTCONN:
Chuck Lever35f5a422006-01-03 09:55:50 +01001126 rpc_force_rebind(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 task->tk_action = call_bind;
1128 break;
1129 case -EAGAIN:
1130 task->tk_action = call_transmit;
1131 break;
1132 case -EIO:
1133 /* shutdown or soft timeout */
1134 rpc_exit(task, status);
1135 break;
1136 default:
Chuck Leverf518e35a2006-01-03 09:55:52 +01001137 printk("%s: RPC call returned error %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 clnt->cl_protname, -status);
1139 rpc_exit(task, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 }
1141}
1142
1143/*
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04001144 * 6a. Handle RPC timeout
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 * We do not release the request slot, so we keep using the
1146 * same XID for all retransmits.
1147 */
1148static void
1149call_timeout(struct rpc_task *task)
1150{
1151 struct rpc_clnt *clnt = task->tk_client;
1152
1153 if (xprt_adjust_timeout(task->tk_rqstp) == 0) {
Chuck Lever46121cf2007-01-31 12:14:08 -05001154 dprintk("RPC: %5u call_timeout (minor)\n", task->tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 goto retry;
1156 }
1157
Chuck Lever46121cf2007-01-31 12:14:08 -05001158 dprintk("RPC: %5u call_timeout (major)\n", task->tk_pid);
Chuck Leveref759a22006-03-20 13:44:17 -05001159 task->tk_timeouts++;
1160
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 if (RPC_IS_SOFT(task)) {
Chuck Leverf518e35a2006-01-03 09:55:52 +01001162 printk(KERN_NOTICE "%s: server %s not responding, timed out\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 clnt->cl_protname, clnt->cl_server);
1164 rpc_exit(task, -EIO);
1165 return;
1166 }
1167
Chuck Leverf518e35a2006-01-03 09:55:52 +01001168 if (!(task->tk_flags & RPC_CALL_MAJORSEEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 task->tk_flags |= RPC_CALL_MAJORSEEN;
1170 printk(KERN_NOTICE "%s: server %s not responding, still trying\n",
1171 clnt->cl_protname, clnt->cl_server);
1172 }
Chuck Lever35f5a422006-01-03 09:55:50 +01001173 rpc_force_rebind(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
1175retry:
1176 clnt->cl_stats->rpcretrans++;
1177 task->tk_action = call_bind;
1178 task->tk_status = 0;
1179}
1180
1181/*
1182 * 7. Decode the RPC reply
1183 */
1184static void
1185call_decode(struct rpc_task *task)
1186{
1187 struct rpc_clnt *clnt = task->tk_client;
1188 struct rpc_rqst *req = task->tk_rqstp;
1189 kxdrproc_t decode = task->tk_msg.rpc_proc->p_decode;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001190 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
Chuck Lever46121cf2007-01-31 12:14:08 -05001192 dprintk("RPC: %5u call_decode (status %d)\n",
1193 task->tk_pid, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
Chuck Leverf518e35a2006-01-03 09:55:52 +01001195 if (task->tk_flags & RPC_CALL_MAJORSEEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 printk(KERN_NOTICE "%s: server %s OK\n",
1197 clnt->cl_protname, clnt->cl_server);
1198 task->tk_flags &= ~RPC_CALL_MAJORSEEN;
1199 }
1200
1201 if (task->tk_status < 12) {
1202 if (!RPC_IS_SOFT(task)) {
1203 task->tk_action = call_bind;
1204 clnt->cl_stats->rpcretrans++;
1205 goto out_retry;
1206 }
Chuck Lever46121cf2007-01-31 12:14:08 -05001207 dprintk("RPC: %s: too small RPC reply size (%d bytes)\n",
1208 clnt->cl_protname, task->tk_status);
Trond Myklebustda458282006-08-31 15:44:52 -04001209 task->tk_action = call_timeout;
1210 goto out_retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 }
1212
Trond Myklebust43ac3f22006-03-20 13:44:51 -05001213 /*
1214 * Ensure that we see all writes made by xprt_complete_rqst()
1215 * before it changed req->rq_received.
1216 */
1217 smp_rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 req->rq_rcv_buf.len = req->rq_private_buf.len;
1219
1220 /* Check that the softirq receive buffer is valid */
1221 WARN_ON(memcmp(&req->rq_rcv_buf, &req->rq_private_buf,
1222 sizeof(req->rq_rcv_buf)) != 0);
1223
1224 /* Verify the RPC header */
Trond Myklebustabbcf282006-01-03 09:55:03 +01001225 p = call_verify(task);
1226 if (IS_ERR(p)) {
1227 if (p == ERR_PTR(-EAGAIN))
1228 goto out_retry;
1229 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 }
1231
Trond Myklebustabbcf282006-01-03 09:55:03 +01001232 task->tk_action = rpc_exit_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
Trond Myklebust6d5fcb52006-10-18 16:01:06 -04001234 if (decode) {
1235 lock_kernel();
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 unlock_kernel();
1239 }
Chuck Lever46121cf2007-01-31 12:14:08 -05001240 dprintk("RPC: %5u call_decode result %d\n", task->tk_pid,
1241 task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 return;
1243out_retry:
1244 req->rq_received = req->rq_private_buf.len = 0;
1245 task->tk_status = 0;
Trond Myklebust241c39b2007-04-20 16:12:55 -04001246 if (task->tk_client->cl_discrtry)
1247 xprt_disconnect(task->tk_xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248}
1249
1250/*
1251 * 8. Refresh the credentials if rejected by the server
1252 */
1253static void
1254call_refresh(struct rpc_task *task)
1255{
Chuck Lever46121cf2007-01-31 12:14:08 -05001256 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
1258 xprt_release(task); /* Must do to obtain new XID */
1259 task->tk_action = call_refreshresult;
1260 task->tk_status = 0;
1261 task->tk_client->cl_stats->rpcauthrefresh++;
1262 rpcauth_refreshcred(task);
1263}
1264
1265/*
1266 * 8a. Process the results of a credential refresh
1267 */
1268static void
1269call_refreshresult(struct rpc_task *task)
1270{
1271 int status = task->tk_status;
Chuck Lever46121cf2007-01-31 12:14:08 -05001272
1273 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
1275 task->tk_status = 0;
1276 task->tk_action = call_reserve;
1277 if (status >= 0 && rpcauth_uptodatecred(task))
1278 return;
1279 if (status == -EACCES) {
1280 rpc_exit(task, -EACCES);
1281 return;
1282 }
1283 task->tk_action = call_refresh;
1284 if (status != -ETIMEDOUT)
1285 rpc_delay(task, 3*HZ);
1286 return;
1287}
1288
1289/*
1290 * Call header serialization
1291 */
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001292static __be32 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293call_header(struct rpc_task *task)
1294{
1295 struct rpc_clnt *clnt = task->tk_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 struct rpc_rqst *req = task->tk_rqstp;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001297 __be32 *p = req->rq_svec[0].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
1299 /* FIXME: check buffer size? */
Chuck Lever808012f2005-08-25 16:25:49 -07001300
1301 p = xprt_skip_transport_header(task->tk_xprt, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 *p++ = req->rq_xid; /* XID */
1303 *p++ = htonl(RPC_CALL); /* CALL */
1304 *p++ = htonl(RPC_VERSION); /* RPC version */
1305 *p++ = htonl(clnt->cl_prog); /* program number */
1306 *p++ = htonl(clnt->cl_vers); /* program version */
1307 *p++ = htonl(task->tk_msg.rpc_proc->p_proc); /* procedure */
Trond Myklebust334ccfd2005-06-22 17:16:19 +00001308 p = rpcauth_marshcred(task, p);
1309 req->rq_slen = xdr_adjust_iovec(&req->rq_svec[0], p);
1310 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311}
1312
1313/*
1314 * Reply header verification
1315 */
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001316static __be32 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317call_verify(struct rpc_task *task)
1318{
1319 struct kvec *iov = &task->tk_rqstp->rq_rcv_buf.head[0];
1320 int len = task->tk_rqstp->rq_rcv_buf.len >> 2;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001321 __be32 *p = iov->iov_base;
1322 u32 n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 int error = -EACCES;
1324
David Howellse8896492006-08-24 15:44:19 -04001325 if ((task->tk_rqstp->rq_rcv_buf.len & 3) != 0) {
1326 /* RFC-1014 says that the representation of XDR data must be a
1327 * multiple of four bytes
1328 * - if it isn't pointer subtraction in the NFS client may give
1329 * undefined results
1330 */
Trond Myklebust8a702bb2007-06-27 18:30:26 -04001331 dprintk("RPC: %5u %s: XDR representation not a multiple of"
1332 " 4 bytes: 0x%x\n", task->tk_pid, __FUNCTION__,
1333 task->tk_rqstp->rq_rcv_buf.len);
David Howellse8896492006-08-24 15:44:19 -04001334 goto out_eio;
1335 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 if ((len -= 3) < 0)
1337 goto out_overflow;
1338 p += 1; /* skip XID */
1339
1340 if ((n = ntohl(*p++)) != RPC_REPLY) {
Trond Myklebust8a702bb2007-06-27 18:30:26 -04001341 dprintk("RPC: %5u %s: not an RPC reply: %x\n",
1342 task->tk_pid, __FUNCTION__, n);
Trond Myklebustabbcf282006-01-03 09:55:03 +01001343 goto out_garbage;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 }
1345 if ((n = ntohl(*p++)) != RPC_MSG_ACCEPTED) {
1346 if (--len < 0)
1347 goto out_overflow;
1348 switch ((n = ntohl(*p++))) {
1349 case RPC_AUTH_ERROR:
1350 break;
1351 case RPC_MISMATCH:
Chuck Lever46121cf2007-01-31 12:14:08 -05001352 dprintk("RPC: %5u %s: RPC call version "
1353 "mismatch!\n",
1354 task->tk_pid, __FUNCTION__);
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00001355 error = -EPROTONOSUPPORT;
1356 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 default:
Chuck Lever46121cf2007-01-31 12:14:08 -05001358 dprintk("RPC: %5u %s: RPC call rejected, "
1359 "unknown error: %x\n",
1360 task->tk_pid, __FUNCTION__, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 goto out_eio;
1362 }
1363 if (--len < 0)
1364 goto out_overflow;
1365 switch ((n = ntohl(*p++))) {
1366 case RPC_AUTH_REJECTEDCRED:
1367 case RPC_AUTH_REJECTEDVERF:
1368 case RPCSEC_GSS_CREDPROBLEM:
1369 case RPCSEC_GSS_CTXPROBLEM:
1370 if (!task->tk_cred_retry)
1371 break;
1372 task->tk_cred_retry--;
Chuck Lever46121cf2007-01-31 12:14:08 -05001373 dprintk("RPC: %5u %s: retry stale creds\n",
1374 task->tk_pid, __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 rpcauth_invalcred(task);
1376 task->tk_action = call_refresh;
Trond Myklebustabbcf282006-01-03 09:55:03 +01001377 goto out_retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 case RPC_AUTH_BADCRED:
1379 case RPC_AUTH_BADVERF:
1380 /* possibly garbled cred/verf? */
1381 if (!task->tk_garb_retry)
1382 break;
1383 task->tk_garb_retry--;
Chuck Lever46121cf2007-01-31 12:14:08 -05001384 dprintk("RPC: %5u %s: retry garbled creds\n",
1385 task->tk_pid, __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 task->tk_action = call_bind;
Trond Myklebustabbcf282006-01-03 09:55:03 +01001387 goto out_retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 case RPC_AUTH_TOOWEAK:
Levent Serinol1356b8c2006-03-20 13:44:11 -05001389 printk(KERN_NOTICE "call_verify: server %s requires stronger "
1390 "authentication.\n", task->tk_client->cl_server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 break;
1392 default:
Trond Myklebust8a702bb2007-06-27 18:30:26 -04001393 dprintk("RPC: %5u %s: unknown auth error: %x\n",
1394 task->tk_pid, __FUNCTION__, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 error = -EIO;
1396 }
Chuck Lever46121cf2007-01-31 12:14:08 -05001397 dprintk("RPC: %5u %s: call rejected %d\n",
1398 task->tk_pid, __FUNCTION__, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 goto out_err;
1400 }
1401 if (!(p = rpcauth_checkverf(task, p))) {
Trond Myklebust8a702bb2007-06-27 18:30:26 -04001402 dprintk("RPC: %5u %s: auth check failed\n",
1403 task->tk_pid, __FUNCTION__);
Trond Myklebustabbcf282006-01-03 09:55:03 +01001404 goto out_garbage; /* bad verifier, retry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 }
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001406 len = p - (__be32 *)iov->iov_base - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 if (len < 0)
1408 goto out_overflow;
1409 switch ((n = ntohl(*p++))) {
1410 case RPC_SUCCESS:
1411 return p;
1412 case RPC_PROG_UNAVAIL:
Chuck Lever46121cf2007-01-31 12:14:08 -05001413 dprintk("RPC: %5u %s: program %u is unsupported by server %s\n",
1414 task->tk_pid, __FUNCTION__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 (unsigned int)task->tk_client->cl_prog,
1416 task->tk_client->cl_server);
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00001417 error = -EPFNOSUPPORT;
1418 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 case RPC_PROG_MISMATCH:
Chuck Lever46121cf2007-01-31 12:14:08 -05001420 dprintk("RPC: %5u %s: program %u, version %u unsupported by "
1421 "server %s\n", task->tk_pid, __FUNCTION__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 (unsigned int)task->tk_client->cl_prog,
1423 (unsigned int)task->tk_client->cl_vers,
1424 task->tk_client->cl_server);
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00001425 error = -EPROTONOSUPPORT;
1426 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 case RPC_PROC_UNAVAIL:
Chuck Lever46121cf2007-01-31 12:14:08 -05001428 dprintk("RPC: %5u %s: proc %p unsupported by program %u, "
1429 "version %u on server %s\n",
1430 task->tk_pid, __FUNCTION__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 task->tk_msg.rpc_proc,
1432 task->tk_client->cl_prog,
1433 task->tk_client->cl_vers,
1434 task->tk_client->cl_server);
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00001435 error = -EOPNOTSUPP;
1436 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 case RPC_GARBAGE_ARGS:
Chuck Lever46121cf2007-01-31 12:14:08 -05001438 dprintk("RPC: %5u %s: server saw garbage\n",
1439 task->tk_pid, __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 break; /* retry */
1441 default:
Trond Myklebust8a702bb2007-06-27 18:30:26 -04001442 dprintk("RPC: %5u %s: server accept status: %x\n",
1443 task->tk_pid, __FUNCTION__, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 /* Also retry */
1445 }
1446
Trond Myklebustabbcf282006-01-03 09:55:03 +01001447out_garbage:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 task->tk_client->cl_stats->rpcgarbage++;
1449 if (task->tk_garb_retry) {
1450 task->tk_garb_retry--;
Chuck Lever46121cf2007-01-31 12:14:08 -05001451 dprintk("RPC: %5u %s: retrying\n",
1452 task->tk_pid, __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 task->tk_action = call_bind;
Trond Myklebustabbcf282006-01-03 09:55:03 +01001454out_retry:
1455 return ERR_PTR(-EAGAIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457out_eio:
1458 error = -EIO;
1459out_err:
1460 rpc_exit(task, error);
Trond Myklebust8a702bb2007-06-27 18:30:26 -04001461 dprintk("RPC: %5u %s: call failed with error %d\n", task->tk_pid,
1462 __FUNCTION__, error);
Trond Myklebustabbcf282006-01-03 09:55:03 +01001463 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464out_overflow:
Trond Myklebust8a702bb2007-06-27 18:30:26 -04001465 dprintk("RPC: %5u %s: server reply was truncated.\n", task->tk_pid,
1466 __FUNCTION__);
Trond Myklebustabbcf282006-01-03 09:55:03 +01001467 goto out_garbage;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468}
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00001469
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001470static int rpcproc_encode_null(void *rqstp, __be32 *data, void *obj)
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00001471{
1472 return 0;
1473}
1474
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001475static int rpcproc_decode_null(void *rqstp, __be32 *data, void *obj)
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00001476{
1477 return 0;
1478}
1479
1480static struct rpc_procinfo rpcproc_null = {
1481 .p_encode = rpcproc_encode_null,
1482 .p_decode = rpcproc_decode_null,
1483};
1484
Trond Myklebust64c91a12007-06-23 10:17:16 -04001485static int rpc_ping(struct rpc_clnt *clnt, int flags)
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00001486{
1487 struct rpc_message msg = {
1488 .rpc_proc = &rpcproc_null,
1489 };
1490 int err;
1491 msg.rpc_cred = authnull_ops.lookup_cred(NULL, NULL, 0);
1492 err = rpc_call_sync(clnt, &msg, flags);
1493 put_rpccred(msg.rpc_cred);
1494 return err;
1495}
Trond Myklebust188fef12007-06-16 14:18:40 -04001496
Trond Myklebust5e1550d2007-06-23 10:17:16 -04001497struct rpc_task *rpc_call_null(struct rpc_clnt *clnt, struct rpc_cred *cred, int flags)
1498{
1499 struct rpc_message msg = {
1500 .rpc_proc = &rpcproc_null,
1501 .rpc_cred = cred,
1502 };
1503 return rpc_do_run_task(clnt, &msg, flags, &rpc_default_ops, NULL);
1504}
1505EXPORT_SYMBOL(rpc_call_null);
1506
Trond Myklebust188fef12007-06-16 14:18:40 -04001507#ifdef RPC_DEBUG
1508void rpc_show_tasks(void)
1509{
1510 struct rpc_clnt *clnt;
1511 struct rpc_task *t;
1512
1513 spin_lock(&rpc_client_lock);
1514 if (list_empty(&all_clients))
1515 goto out;
1516 printk("-pid- proc flgs status -client- -prog- --rqstp- -timeout "
1517 "-rpcwait -action- ---ops--\n");
1518 list_for_each_entry(clnt, &all_clients, cl_clients) {
1519 if (list_empty(&clnt->cl_tasks))
1520 continue;
1521 spin_lock(&clnt->cl_lock);
1522 list_for_each_entry(t, &clnt->cl_tasks, tk_task) {
1523 const char *rpc_waitq = "none";
1524
1525 if (RPC_IS_QUEUED(t))
1526 rpc_waitq = rpc_qname(t->u.tk_wait.rpc_waitq);
1527
1528 printk("%5u %04d %04x %6d %8p %6d %8p %8ld %8s %8p %8p\n",
1529 t->tk_pid,
1530 (t->tk_msg.rpc_proc ? t->tk_msg.rpc_proc->p_proc : -1),
1531 t->tk_flags, t->tk_status,
1532 t->tk_client,
1533 (t->tk_client ? t->tk_client->cl_prog : 0),
1534 t->tk_rqstp, t->tk_timeout,
1535 rpc_waitq,
1536 t->tk_action, t->tk_ops);
1537 }
1538 spin_unlock(&clnt->cl_lock);
1539 }
1540out:
1541 spin_unlock(&rpc_client_lock);
1542}
1543#endif