blob: 76eef19cf995f1cf61efc83af0916629a9a5ffde [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 Myklebust188fef12007-06-16 14:18:40 -040075static void rpc_register_client(struct rpc_clnt *clnt)
76{
77 spin_lock(&rpc_client_lock);
78 list_add(&clnt->cl_clients, &all_clients);
79 spin_unlock(&rpc_client_lock);
80}
81
82static void rpc_unregister_client(struct rpc_clnt *clnt)
83{
84 spin_lock(&rpc_client_lock);
85 list_del(&clnt->cl_clients);
86 spin_unlock(&rpc_client_lock);
87}
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
89static int
90rpc_setup_pipedir(struct rpc_clnt *clnt, char *dir_name)
91{
Trond Myklebustf1345852005-09-23 11:08:25 -040092 static uint32_t clntid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 int error;
94
Trond Myklebust54281542006-03-20 13:44:49 -050095 clnt->cl_vfsmnt = ERR_PTR(-ENOENT);
96 clnt->cl_dentry = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 if (dir_name == NULL)
98 return 0;
Trond Myklebust54281542006-03-20 13:44:49 -050099
100 clnt->cl_vfsmnt = rpc_get_mount();
101 if (IS_ERR(clnt->cl_vfsmnt))
102 return PTR_ERR(clnt->cl_vfsmnt);
103
Trond Myklebustf1345852005-09-23 11:08:25 -0400104 for (;;) {
105 snprintf(clnt->cl_pathname, sizeof(clnt->cl_pathname),
106 "%s/clnt%x", dir_name,
107 (unsigned int)clntid++);
108 clnt->cl_pathname[sizeof(clnt->cl_pathname) - 1] = '\0';
109 clnt->cl_dentry = rpc_mkdir(clnt->cl_pathname, clnt);
110 if (!IS_ERR(clnt->cl_dentry))
111 return 0;
Christoph Hellwig278c9952005-07-24 23:53:01 +0100112 error = PTR_ERR(clnt->cl_dentry);
Trond Myklebustf1345852005-09-23 11:08:25 -0400113 if (error != -EEXIST) {
114 printk(KERN_INFO "RPC: Couldn't create pipefs entry %s, error %d\n",
115 clnt->cl_pathname, error);
Trond Myklebust54281542006-03-20 13:44:49 -0500116 rpc_put_mount();
Trond Myklebustf1345852005-09-23 11:08:25 -0400117 return error;
118 }
Christoph Hellwig278c9952005-07-24 23:53:01 +0100119 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120}
121
Chuck Leverff9aa5e2006-08-22 20:06:21 -0400122static 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 -0700123{
124 struct rpc_version *version;
125 struct rpc_clnt *clnt = NULL;
J. Bruce Fields6a192752005-06-22 17:16:23 +0000126 struct rpc_auth *auth;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 int err;
128 int len;
129
Chuck Lever46121cf2007-01-31 12:14:08 -0500130 dprintk("RPC: creating %s client for %s (xprt %p)\n",
131 program->name, servname, xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Trond Myklebust4ada5392007-06-14 17:26:17 -0400133 err = rpciod_up();
134 if (err)
135 goto out_no_rpciod;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 err = -EINVAL;
137 if (!xprt)
Adrian Bunk712917d2006-03-13 21:20:47 -0800138 goto out_no_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 if (vers >= program->nrvers || !(version = program->version[vers]))
140 goto out_err;
141
142 err = -ENOMEM;
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700143 clnt = kzalloc(sizeof(*clnt), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 if (!clnt)
145 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 clnt->cl_parent = clnt;
147
148 clnt->cl_server = clnt->cl_inline_name;
149 len = strlen(servname) + 1;
150 if (len > sizeof(clnt->cl_inline_name)) {
151 char *buf = kmalloc(len, GFP_KERNEL);
152 if (buf != 0)
153 clnt->cl_server = buf;
154 else
155 len = sizeof(clnt->cl_inline_name);
156 }
157 strlcpy(clnt->cl_server, servname, len);
158
159 clnt->cl_xprt = xprt;
160 clnt->cl_procinfo = version->procs;
161 clnt->cl_maxproc = version->nrprocs;
162 clnt->cl_protname = program->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 clnt->cl_prog = program->number;
164 clnt->cl_vers = version->number;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 clnt->cl_stats = program->stats;
Chuck Lever11c556b2006-03-20 13:44:22 -0500166 clnt->cl_metrics = rpc_alloc_iostats(clnt);
Trond Myklebust23bf85b2006-11-21 10:40:23 -0500167 err = -ENOMEM;
168 if (clnt->cl_metrics == NULL)
169 goto out_no_stats;
Trond Myklebust3e32a5d2006-11-16 11:37:27 -0500170 clnt->cl_program = program;
Trond Myklebust6529eba2007-06-14 16:40:14 -0400171 INIT_LIST_HEAD(&clnt->cl_tasks);
Trond Myklebust4bef61f2007-06-16 14:17:01 -0400172 spin_lock_init(&clnt->cl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Chuck Leverec739ef2006-08-22 20:06:15 -0400174 if (!xprt_bound(clnt->cl_xprt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 clnt->cl_autobind = 1;
176
177 clnt->cl_rtt = &clnt->cl_rtt_default;
178 rpc_init_rtt(&clnt->cl_rtt_default, xprt->timeout.to_initval);
179
Trond Myklebust34f52e32007-06-14 16:40:31 -0400180 kref_init(&clnt->cl_kref);
181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 err = rpc_setup_pipedir(clnt, program->pipe_dir_name);
183 if (err < 0)
184 goto out_no_path;
185
J. Bruce Fields6a192752005-06-22 17:16:23 +0000186 auth = rpcauth_create(flavor, clnt);
187 if (IS_ERR(auth)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 printk(KERN_INFO "RPC: Couldn't create auth handle (flavor %u)\n",
189 flavor);
J. Bruce Fields6a192752005-06-22 17:16:23 +0000190 err = PTR_ERR(auth);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 goto out_no_auth;
192 }
193
194 /* save the nodename */
Serge E. Hallyne9ff3992006-10-02 02:18:11 -0700195 clnt->cl_nodelen = strlen(utsname()->nodename);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 if (clnt->cl_nodelen > UNX_MAXNODENAME)
197 clnt->cl_nodelen = UNX_MAXNODENAME;
Serge E. Hallyne9ff3992006-10-02 02:18:11 -0700198 memcpy(clnt->cl_nodename, utsname()->nodename, clnt->cl_nodelen);
Trond Myklebust6529eba2007-06-14 16:40:14 -0400199 rpc_register_client(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 return clnt;
201
202out_no_auth:
Trond Myklebust54281542006-03-20 13:44:49 -0500203 if (!IS_ERR(clnt->cl_dentry)) {
Trond Myklebustdff02cc2006-07-31 14:17:18 -0700204 rpc_rmdir(clnt->cl_dentry);
Trond Myklebust54281542006-03-20 13:44:49 -0500205 rpc_put_mount();
206 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207out_no_path:
Trond Myklebust23bf85b2006-11-21 10:40:23 -0500208 rpc_free_iostats(clnt->cl_metrics);
209out_no_stats:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 if (clnt->cl_server != clnt->cl_inline_name)
211 kfree(clnt->cl_server);
212 kfree(clnt);
213out_err:
Trond Myklebust6b6ca862006-09-05 12:55:57 -0400214 xprt_put(xprt);
Adrian Bunk712917d2006-03-13 21:20:47 -0800215out_no_xprt:
Trond Myklebust4ada5392007-06-14 17:26:17 -0400216 rpciod_down();
217out_no_rpciod:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 return ERR_PTR(err);
219}
220
Chuck Leverc2866762006-08-22 20:06:20 -0400221/*
222 * rpc_create - create an RPC client and transport with one call
223 * @args: rpc_clnt create argument structure
224 *
225 * Creates and initializes an RPC transport and an RPC client.
226 *
227 * It can ping the server in order to determine if it is up, and to see if
228 * it supports this program and version. RPC_CLNT_CREATE_NOPING disables
229 * this behavior so asynchronous tasks can also use rpc_create.
230 */
231struct rpc_clnt *rpc_create(struct rpc_create_args *args)
232{
233 struct rpc_xprt *xprt;
234 struct rpc_clnt *clnt;
235
236 xprt = xprt_create_transport(args->protocol, args->address,
237 args->addrsize, args->timeout);
238 if (IS_ERR(xprt))
239 return (struct rpc_clnt *)xprt;
240
241 /*
242 * By default, kernel RPC client connects from a reserved port.
243 * CAP_NET_BIND_SERVICE will not be set for unprivileged requesters,
244 * but it is always enabled for rpciod, which handles the connect
245 * operation.
246 */
247 xprt->resvport = 1;
248 if (args->flags & RPC_CLNT_CREATE_NONPRIVPORT)
249 xprt->resvport = 0;
250
251 dprintk("RPC: creating %s client for %s (xprt %p)\n",
Chuck Lever46121cf2007-01-31 12:14:08 -0500252 args->program->name, args->servername, xprt);
Chuck Leverc2866762006-08-22 20:06:20 -0400253
254 clnt = rpc_new_client(xprt, args->servername, args->program,
255 args->version, args->authflavor);
256 if (IS_ERR(clnt))
257 return clnt;
258
259 if (!(args->flags & RPC_CLNT_CREATE_NOPING)) {
260 int err = rpc_ping(clnt, RPC_TASK_SOFT|RPC_TASK_NOINTR);
261 if (err != 0) {
262 rpc_shutdown_client(clnt);
263 return ERR_PTR(err);
264 }
265 }
266
267 clnt->cl_softrtry = 1;
268 if (args->flags & RPC_CLNT_CREATE_HARDRTRY)
269 clnt->cl_softrtry = 0;
270
271 if (args->flags & RPC_CLNT_CREATE_INTR)
272 clnt->cl_intr = 1;
273 if (args->flags & RPC_CLNT_CREATE_AUTOBIND)
274 clnt->cl_autobind = 1;
Chuck Lever43d78ef2007-02-06 18:26:11 -0500275 if (args->flags & RPC_CLNT_CREATE_DISCRTRY)
276 clnt->cl_discrtry = 1;
Chuck Leverc2866762006-08-22 20:06:20 -0400277
278 return clnt;
279}
Chuck Leverb86acd52006-08-22 20:06:22 -0400280EXPORT_SYMBOL_GPL(rpc_create);
Chuck Leverc2866762006-08-22 20:06:20 -0400281
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282/*
283 * This function clones the RPC client structure. It allows us to share the
284 * same transport while varying parameters such as the authentication
285 * flavour.
286 */
287struct rpc_clnt *
288rpc_clone_client(struct rpc_clnt *clnt)
289{
290 struct rpc_clnt *new;
Trond Myklebust3e32a5d2006-11-16 11:37:27 -0500291 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
Arnaldo Carvalho de Meloe69062b2006-11-21 01:21:34 -0200293 new = kmemdup(clnt, sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 if (!new)
295 goto out_no_clnt;
Trond Myklebustd431a5552007-06-17 17:07:54 -0400296 new->cl_parent = clnt;
297 /* Turn off autobind on clones */
298 new->cl_autobind = 0;
299 INIT_LIST_HEAD(&new->cl_tasks);
300 spin_lock_init(&new->cl_lock);
301 rpc_init_rtt(&new->cl_rtt_default, clnt->cl_xprt->timeout.to_initval);
Trond Myklebust23bf85b2006-11-21 10:40:23 -0500302 new->cl_metrics = rpc_alloc_iostats(clnt);
303 if (new->cl_metrics == NULL)
304 goto out_no_stats;
Trond Myklebust34f52e32007-06-14 16:40:31 -0400305 kref_init(&new->cl_kref);
Trond Myklebust3e32a5d2006-11-16 11:37:27 -0500306 err = rpc_setup_pipedir(new, clnt->cl_program->pipe_dir_name);
307 if (err != 0)
308 goto out_no_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 if (new->cl_auth)
310 atomic_inc(&new->cl_auth->au_count);
Trond Myklebustd431a5552007-06-17 17:07:54 -0400311 xprt_get(clnt->cl_xprt);
312 kref_get(&clnt->cl_kref);
Trond Myklebust6529eba2007-06-14 16:40:14 -0400313 rpc_register_client(new);
Trond Myklebust4ada5392007-06-14 17:26:17 -0400314 rpciod_up();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 return new;
Trond Myklebust3e32a5d2006-11-16 11:37:27 -0500316out_no_path:
317 rpc_free_iostats(new->cl_metrics);
Trond Myklebust23bf85b2006-11-21 10:40:23 -0500318out_no_stats:
319 kfree(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320out_no_clnt:
Chuck Lever46121cf2007-01-31 12:14:08 -0500321 dprintk("RPC: %s: returned error %d\n", __FUNCTION__, err);
Trond Myklebust3e32a5d2006-11-16 11:37:27 -0500322 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323}
324
325/*
326 * Properly shut down an RPC client, terminating all outstanding
Trond Myklebust90c57552007-06-09 19:49:36 -0400327 * requests.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 */
Trond Myklebust4c402b42007-06-14 16:40:32 -0400329void rpc_shutdown_client(struct rpc_clnt *clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330{
Trond Myklebust34f52e32007-06-14 16:40:31 -0400331 dprintk("RPC: shutting down %s client for %s\n",
332 clnt->cl_protname, clnt->cl_server);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
Trond Myklebust34f52e32007-06-14 16:40:31 -0400334 while (!list_empty(&clnt->cl_tasks)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 rpc_killall_tasks(clnt);
Ingo Molnar532347e2006-01-09 20:52:53 -0800336 wait_event_timeout(destroy_wait,
Trond Myklebust34f52e32007-06-14 16:40:31 -0400337 list_empty(&clnt->cl_tasks), 1*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 }
339
Trond Myklebust4c402b42007-06-14 16:40:32 -0400340 rpc_release_client(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341}
342
343/*
Trond Myklebust34f52e32007-06-14 16:40:31 -0400344 * Free an RPC client
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 */
Trond Myklebust34f52e32007-06-14 16:40:31 -0400346static void
347rpc_free_client(struct kref *kref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348{
Trond Myklebust34f52e32007-06-14 16:40:31 -0400349 struct rpc_clnt *clnt = container_of(kref, struct rpc_clnt, cl_kref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Chuck Lever46121cf2007-01-31 12:14:08 -0500351 dprintk("RPC: destroying %s client for %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 clnt->cl_protname, clnt->cl_server);
353 if (clnt->cl_auth) {
354 rpcauth_destroy(clnt->cl_auth);
355 clnt->cl_auth = NULL;
356 }
Trond Myklebust8f8e7a52006-08-14 13:11:15 -0400357 if (!IS_ERR(clnt->cl_dentry)) {
Trond Myklebustdff02cc2006-07-31 14:17:18 -0700358 rpc_rmdir(clnt->cl_dentry);
Trond Myklebust8f8e7a52006-08-14 13:11:15 -0400359 rpc_put_mount();
360 }
Trond Myklebust3e32a5d2006-11-16 11:37:27 -0500361 if (clnt->cl_parent != clnt) {
Trond Myklebust8ad7c892007-06-14 16:40:32 -0400362 rpc_release_client(clnt->cl_parent);
Trond Myklebust3e32a5d2006-11-16 11:37:27 -0500363 goto out_free;
364 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 if (clnt->cl_server != clnt->cl_inline_name)
366 kfree(clnt->cl_server);
367out_free:
Trond Myklebust6529eba2007-06-14 16:40:14 -0400368 rpc_unregister_client(clnt);
Chuck Lever11c556b2006-03-20 13:44:22 -0500369 rpc_free_iostats(clnt->cl_metrics);
370 clnt->cl_metrics = NULL;
Trond Myklebust6b6ca862006-09-05 12:55:57 -0400371 xprt_put(clnt->cl_xprt);
Trond Myklebust4ada5392007-06-14 17:26:17 -0400372 rpciod_down();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 kfree(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374}
375
376/*
Trond Myklebust34f52e32007-06-14 16:40:31 -0400377 * Release reference to the RPC client
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 */
379void
380rpc_release_client(struct rpc_clnt *clnt)
381{
Trond Myklebust34f52e32007-06-14 16:40:31 -0400382 dprintk("RPC: rpc_release_client(%p)\n", clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Trond Myklebust34f52e32007-06-14 16:40:31 -0400384 if (list_empty(&clnt->cl_tasks))
385 wake_up(&destroy_wait);
Trond Myklebust34f52e32007-06-14 16:40:31 -0400386 kref_put(&clnt->cl_kref, rpc_free_client);
387}
388
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000389/**
390 * rpc_bind_new_program - bind a new RPC program to an existing client
391 * @old - old rpc_client
392 * @program - rpc program to set
393 * @vers - rpc program version
394 *
395 * Clones the rpc client and sets up a new RPC program. This is mainly
396 * of use for enabling different RPC programs to share the same transport.
397 * The Sun NFSv2/v3 ACL protocol can do this.
398 */
399struct rpc_clnt *rpc_bind_new_program(struct rpc_clnt *old,
400 struct rpc_program *program,
401 int vers)
402{
403 struct rpc_clnt *clnt;
404 struct rpc_version *version;
405 int err;
406
407 BUG_ON(vers >= program->nrvers || !program->version[vers]);
408 version = program->version[vers];
409 clnt = rpc_clone_client(old);
410 if (IS_ERR(clnt))
411 goto out;
412 clnt->cl_procinfo = version->procs;
413 clnt->cl_maxproc = version->nrprocs;
414 clnt->cl_protname = program->name;
415 clnt->cl_prog = program->number;
416 clnt->cl_vers = version->number;
417 clnt->cl_stats = program->stats;
418 err = rpc_ping(clnt, RPC_TASK_SOFT|RPC_TASK_NOINTR);
419 if (err != 0) {
420 rpc_shutdown_client(clnt);
421 clnt = ERR_PTR(err);
422 }
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800423out:
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000424 return clnt;
425}
426
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427/*
428 * Default callback for async RPC calls
429 */
430static void
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100431rpc_default_callback(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432{
433}
434
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100435static const struct rpc_call_ops rpc_default_ops = {
436 .rpc_call_done = rpc_default_callback,
437};
438
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439/*
Trond Myklebust14b218a2005-06-22 17:16:28 +0000440 * Export the signal mask handling for synchronous code that
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 * sleeps on RPC calls
442 */
Trond Myklebust2bd61572006-01-03 09:55:19 +0100443#define RPC_INTR_SIGNALS (sigmask(SIGHUP) | sigmask(SIGINT) | sigmask(SIGQUIT) | sigmask(SIGTERM))
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800444
Trond Myklebust14b218a2005-06-22 17:16:28 +0000445static void rpc_save_sigmask(sigset_t *oldset, int intr)
446{
Trond Myklebust2bd61572006-01-03 09:55:19 +0100447 unsigned long sigallow = sigmask(SIGKILL);
Trond Myklebust14b218a2005-06-22 17:16:28 +0000448 sigset_t sigmask;
449
450 /* Block all signals except those listed in sigallow */
451 if (intr)
452 sigallow |= RPC_INTR_SIGNALS;
453 siginitsetinv(&sigmask, sigallow);
454 sigprocmask(SIG_BLOCK, &sigmask, oldset);
455}
456
457static inline void rpc_task_sigmask(struct rpc_task *task, sigset_t *oldset)
458{
459 rpc_save_sigmask(oldset, !RPC_TASK_UNINTERRUPTIBLE(task));
460}
461
462static inline void rpc_restore_sigmask(sigset_t *oldset)
463{
464 sigprocmask(SIG_SETMASK, oldset, NULL);
465}
466
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467void rpc_clnt_sigmask(struct rpc_clnt *clnt, sigset_t *oldset)
468{
Trond Myklebust14b218a2005-06-22 17:16:28 +0000469 rpc_save_sigmask(oldset, clnt->cl_intr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470}
471
472void rpc_clnt_sigunmask(struct rpc_clnt *clnt, sigset_t *oldset)
473{
Trond Myklebust14b218a2005-06-22 17:16:28 +0000474 rpc_restore_sigmask(oldset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475}
476
Trond Myklebust6e5b70e2007-06-12 10:02:37 -0400477static
478struct rpc_task *rpc_do_run_task(struct rpc_clnt *clnt,
479 struct rpc_message *msg,
480 int flags,
481 const struct rpc_call_ops *ops,
482 void *data)
483{
484 struct rpc_task *task, *ret;
485 sigset_t oldset;
486
487 task = rpc_new_task(clnt, flags, ops, data);
488 if (task == NULL) {
489 rpc_release_calldata(ops, data);
490 return ERR_PTR(-ENOMEM);
491 }
492
493 /* Mask signals on synchronous RPC calls and RPCSEC_GSS upcalls */
494 rpc_task_sigmask(task, &oldset);
495 if (msg != NULL) {
496 rpc_call_setup(task, msg, 0);
497 if (task->tk_status != 0) {
498 ret = ERR_PTR(task->tk_status);
499 rpc_put_task(task);
500 goto out;
501 }
502 }
503 atomic_inc(&task->tk_count);
504 rpc_execute(task);
505 ret = task;
506out:
507 rpc_restore_sigmask(&oldset);
508 return ret;
509}
510
511/**
512 * rpc_call_sync - Perform a synchronous RPC call
513 * @clnt: pointer to RPC client
514 * @msg: RPC call parameters
515 * @flags: RPC call flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 */
517int rpc_call_sync(struct rpc_clnt *clnt, struct rpc_message *msg, int flags)
518{
519 struct rpc_task *task;
Trond Myklebust6e5b70e2007-06-12 10:02:37 -0400520 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 BUG_ON(flags & RPC_TASK_ASYNC);
523
Trond Myklebust6e5b70e2007-06-12 10:02:37 -0400524 task = rpc_do_run_task(clnt, msg, flags, &rpc_default_ops, NULL);
525 if (IS_ERR(task))
526 return PTR_ERR(task);
Trond Myklebuste60859a2006-01-03 09:55:10 +0100527 status = task->tk_status;
Trond Myklebustbde8f002007-01-24 11:54:53 -0800528 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 return status;
530}
531
Trond Myklebust6e5b70e2007-06-12 10:02:37 -0400532/**
533 * rpc_call_async - Perform an asynchronous RPC call
534 * @clnt: pointer to RPC client
535 * @msg: RPC call parameters
536 * @flags: RPC call flags
537 * @ops: RPC call ops
538 * @data: user call data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 */
540int
541rpc_call_async(struct rpc_clnt *clnt, struct rpc_message *msg, int flags,
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100542 const struct rpc_call_ops *tk_ops, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543{
544 struct rpc_task *task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
Trond Myklebust6e5b70e2007-06-12 10:02:37 -0400546 task = rpc_do_run_task(clnt, msg, flags|RPC_TASK_ASYNC, tk_ops, data);
547 if (IS_ERR(task))
548 return PTR_ERR(task);
549 rpc_put_task(task);
550 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551}
552
Trond Myklebust6e5b70e2007-06-12 10:02:37 -0400553/**
554 * rpc_run_task - Allocate a new RPC task, then run rpc_execute against it
555 * @clnt: pointer to RPC client
556 * @flags: RPC flags
557 * @ops: RPC call ops
558 * @data: user call data
559 */
560struct rpc_task *rpc_run_task(struct rpc_clnt *clnt, int flags,
561 const struct rpc_call_ops *tk_ops,
562 void *data)
563{
564 return rpc_do_run_task(clnt, NULL, flags, tk_ops, data);
565}
566EXPORT_SYMBOL(rpc_run_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
568void
569rpc_call_setup(struct rpc_task *task, struct rpc_message *msg, int flags)
570{
571 task->tk_msg = *msg;
572 task->tk_flags |= flags;
573 /* Bind the user cred */
574 if (task->tk_msg.rpc_cred != NULL)
575 rpcauth_holdcred(task);
576 else
577 rpcauth_bindcred(task);
578
579 if (task->tk_status == 0)
580 task->tk_action = call_start;
581 else
Trond Myklebustabbcf282006-01-03 09:55:03 +0100582 task->tk_action = rpc_exit_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583}
584
Chuck Levered394402006-08-22 20:06:17 -0400585/**
586 * rpc_peeraddr - extract remote peer address from clnt's xprt
587 * @clnt: RPC client structure
588 * @buf: target buffer
589 * @size: length of target buffer
590 *
591 * Returns the number of bytes that are actually in the stored address.
592 */
593size_t rpc_peeraddr(struct rpc_clnt *clnt, struct sockaddr *buf, size_t bufsize)
594{
595 size_t bytes;
596 struct rpc_xprt *xprt = clnt->cl_xprt;
597
598 bytes = sizeof(xprt->addr);
599 if (bytes > bufsize)
600 bytes = bufsize;
601 memcpy(buf, &clnt->cl_xprt->addr, bytes);
Chuck Leverc4efcb12006-08-22 20:06:19 -0400602 return xprt->addrlen;
Chuck Levered394402006-08-22 20:06:17 -0400603}
Chuck Leverb86acd52006-08-22 20:06:22 -0400604EXPORT_SYMBOL_GPL(rpc_peeraddr);
Chuck Levered394402006-08-22 20:06:17 -0400605
Chuck Leverf425eba2006-08-22 20:06:18 -0400606/**
607 * rpc_peeraddr2str - return remote peer address in printable format
608 * @clnt: RPC client structure
609 * @format: address format
610 *
611 */
612char *rpc_peeraddr2str(struct rpc_clnt *clnt, enum rpc_display_format_t format)
613{
614 struct rpc_xprt *xprt = clnt->cl_xprt;
Chuck Lever7559c7a2006-12-05 16:35:37 -0500615
616 if (xprt->address_strings[format] != NULL)
617 return xprt->address_strings[format];
618 else
619 return "unprintable";
Chuck Leverf425eba2006-08-22 20:06:18 -0400620}
Chuck Leverb86acd52006-08-22 20:06:22 -0400621EXPORT_SYMBOL_GPL(rpc_peeraddr2str);
Chuck Leverf425eba2006-08-22 20:06:18 -0400622
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623void
624rpc_setbufsize(struct rpc_clnt *clnt, unsigned int sndsize, unsigned int rcvsize)
625{
626 struct rpc_xprt *xprt = clnt->cl_xprt;
Chuck Lever470056c2005-08-25 16:25:56 -0700627 if (xprt->ops->set_buffer_size)
628 xprt->ops->set_buffer_size(xprt, sndsize, rcvsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629}
630
631/*
632 * Return size of largest payload RPC client can support, in bytes
633 *
634 * For stream transports, this is one RPC record fragment (see RFC
635 * 1831), as we don't support multi-record requests yet. For datagram
636 * transports, this is the size of an IP packet minus the IP, UDP, and
637 * RPC header sizes.
638 */
639size_t rpc_max_payload(struct rpc_clnt *clnt)
640{
641 return clnt->cl_xprt->max_payload;
642}
Chuck Leverb86acd52006-08-22 20:06:22 -0400643EXPORT_SYMBOL_GPL(rpc_max_payload);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
Chuck Lever35f5a422006-01-03 09:55:50 +0100645/**
646 * rpc_force_rebind - force transport to check that remote port is unchanged
647 * @clnt: client to rebind
648 *
649 */
650void rpc_force_rebind(struct rpc_clnt *clnt)
651{
652 if (clnt->cl_autobind)
Chuck Leverec739ef2006-08-22 20:06:15 -0400653 xprt_clear_bound(clnt->cl_xprt);
Chuck Lever35f5a422006-01-03 09:55:50 +0100654}
Chuck Leverb86acd52006-08-22 20:06:22 -0400655EXPORT_SYMBOL_GPL(rpc_force_rebind);
Chuck Lever35f5a422006-01-03 09:55:50 +0100656
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657/*
658 * Restart an (async) RPC call. Usually called from within the
659 * exit handler.
660 */
661void
662rpc_restart_call(struct rpc_task *task)
663{
664 if (RPC_ASSASSINATED(task))
665 return;
666
667 task->tk_action = call_start;
668}
669
670/*
671 * 0. Initial state
672 *
673 * Other FSM states can be visited zero or more times, but
674 * this state is visited exactly once for each RPC.
675 */
676static void
677call_start(struct rpc_task *task)
678{
679 struct rpc_clnt *clnt = task->tk_client;
680
Chuck Lever46121cf2007-01-31 12:14:08 -0500681 dprintk("RPC: %5u call_start %s%d proc %d (%s)\n", task->tk_pid,
682 clnt->cl_protname, clnt->cl_vers,
683 task->tk_msg.rpc_proc->p_proc,
684 (RPC_IS_ASYNC(task) ? "async" : "sync"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
686 /* Increment call count */
687 task->tk_msg.rpc_proc->p_count++;
688 clnt->cl_stats->rpccnt++;
689 task->tk_action = call_reserve;
690}
691
692/*
693 * 1. Reserve an RPC call slot
694 */
695static void
696call_reserve(struct rpc_task *task)
697{
Chuck Lever46121cf2007-01-31 12:14:08 -0500698 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
700 if (!rpcauth_uptodatecred(task)) {
701 task->tk_action = call_refresh;
702 return;
703 }
704
705 task->tk_status = 0;
706 task->tk_action = call_reserveresult;
707 xprt_reserve(task);
708}
709
710/*
711 * 1b. Grok the result of xprt_reserve()
712 */
713static void
714call_reserveresult(struct rpc_task *task)
715{
716 int status = task->tk_status;
717
Chuck Lever46121cf2007-01-31 12:14:08 -0500718 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
720 /*
721 * After a call to xprt_reserve(), we must have either
722 * a request slot or else an error status.
723 */
724 task->tk_status = 0;
725 if (status >= 0) {
726 if (task->tk_rqstp) {
727 task->tk_action = call_allocate;
728 return;
729 }
730
731 printk(KERN_ERR "%s: status=%d, but no request slot, exiting\n",
732 __FUNCTION__, status);
733 rpc_exit(task, -EIO);
734 return;
735 }
736
737 /*
738 * Even though there was an error, we may have acquired
739 * a request slot somehow. Make sure not to leak it.
740 */
741 if (task->tk_rqstp) {
742 printk(KERN_ERR "%s: status=%d, request allocated anyway\n",
743 __FUNCTION__, status);
744 xprt_release(task);
745 }
746
747 switch (status) {
748 case -EAGAIN: /* woken up; retry */
749 task->tk_action = call_reserve;
750 return;
751 case -EIO: /* probably a shutdown */
752 break;
753 default:
754 printk(KERN_ERR "%s: unrecognized error %d, exiting\n",
755 __FUNCTION__, status);
756 break;
757 }
758 rpc_exit(task, status);
759}
760
761/*
762 * 2. Allocate the buffer. For details, see sched.c:rpc_malloc.
Chuck Lever02107142006-01-03 09:55:49 +0100763 * (Note: buffer memory is freed in xprt_release).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 */
765static void
766call_allocate(struct rpc_task *task)
767{
Chuck Lever2bea90d2007-03-29 16:47:53 -0400768 unsigned int slack = task->tk_auth->au_cslack;
Chuck Lever02107142006-01-03 09:55:49 +0100769 struct rpc_rqst *req = task->tk_rqstp;
770 struct rpc_xprt *xprt = task->tk_xprt;
Chuck Lever2bea90d2007-03-29 16:47:53 -0400771 struct rpc_procinfo *proc = task->tk_msg.rpc_proc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
Chuck Lever46121cf2007-01-31 12:14:08 -0500773 dprint_status(task);
774
Chuck Lever2bea90d2007-03-29 16:47:53 -0400775 task->tk_status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 task->tk_action = call_bind;
Chuck Lever2bea90d2007-03-29 16:47:53 -0400777
Chuck Lever02107142006-01-03 09:55:49 +0100778 if (req->rq_buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 return;
780
Chuck Lever2bea90d2007-03-29 16:47:53 -0400781 if (proc->p_proc != 0) {
782 BUG_ON(proc->p_arglen == 0);
783 if (proc->p_decode != NULL)
784 BUG_ON(proc->p_replen == 0);
785 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
Chuck Lever2bea90d2007-03-29 16:47:53 -0400787 /*
788 * Calculate the size (in quads) of the RPC call
789 * and reply headers, and convert both values
790 * to byte sizes.
791 */
792 req->rq_callsize = RPC_CALLHDRSIZE + (slack << 1) + proc->p_arglen;
793 req->rq_callsize <<= 2;
794 req->rq_rcvsize = RPC_REPHDRSIZE + slack + proc->p_replen;
795 req->rq_rcvsize <<= 2;
796
Chuck Leverc5a4dd82007-03-29 16:47:58 -0400797 req->rq_buffer = xprt->ops->buf_alloc(task,
798 req->rq_callsize + req->rq_rcvsize);
Chuck Lever2bea90d2007-03-29 16:47:53 -0400799 if (req->rq_buffer != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 return;
Chuck Lever46121cf2007-01-31 12:14:08 -0500801
802 dprintk("RPC: %5u rpc_buffer allocation failed\n", task->tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
Trond Myklebust14b218a2005-06-22 17:16:28 +0000804 if (RPC_IS_ASYNC(task) || !signalled()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 xprt_release(task);
806 task->tk_action = call_reserve;
807 rpc_delay(task, HZ>>4);
808 return;
809 }
810
811 rpc_exit(task, -ERESTARTSYS);
812}
813
Trond Myklebust940e3312005-11-09 21:45:24 -0500814static inline int
815rpc_task_need_encode(struct rpc_task *task)
816{
817 return task->tk_rqstp->rq_snd_buf.len == 0;
818}
819
820static inline void
821rpc_task_force_reencode(struct rpc_task *task)
822{
823 task->tk_rqstp->rq_snd_buf.len = 0;
824}
825
Chuck Lever2bea90d2007-03-29 16:47:53 -0400826static inline void
827rpc_xdr_buf_init(struct xdr_buf *buf, void *start, size_t len)
828{
829 buf->head[0].iov_base = start;
830 buf->head[0].iov_len = len;
831 buf->tail[0].iov_len = 0;
832 buf->page_len = 0;
833 buf->len = 0;
834 buf->buflen = len;
835}
836
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837/*
838 * 3. Encode arguments of an RPC call
839 */
840static void
841call_encode(struct rpc_task *task)
842{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 struct rpc_rqst *req = task->tk_rqstp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 kxdrproc_t encode;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700845 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
Chuck Lever46121cf2007-01-31 12:14:08 -0500847 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
Chuck Lever2bea90d2007-03-29 16:47:53 -0400849 rpc_xdr_buf_init(&req->rq_snd_buf,
850 req->rq_buffer,
851 req->rq_callsize);
852 rpc_xdr_buf_init(&req->rq_rcv_buf,
853 (char *)req->rq_buffer + req->rq_callsize,
854 req->rq_rcvsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
856 /* Encode header and provided arguments */
857 encode = task->tk_msg.rpc_proc->p_encode;
858 if (!(p = call_header(task))) {
859 printk(KERN_INFO "RPC: call_header failed, exit EIO\n");
860 rpc_exit(task, -EIO);
861 return;
862 }
J. Bruce Fieldsf3680312005-10-13 16:54:48 -0400863 if (encode == NULL)
864 return;
865
Trond Myklebust6d5fcb52006-10-18 16:01:06 -0400866 lock_kernel();
J. Bruce Fieldsf3680312005-10-13 16:54:48 -0400867 task->tk_status = rpcauth_wrap_req(task, encode, req, p,
868 task->tk_msg.rpc_argp);
Trond Myklebust6d5fcb52006-10-18 16:01:06 -0400869 unlock_kernel();
J. Bruce Fieldsf3680312005-10-13 16:54:48 -0400870 if (task->tk_status == -ENOMEM) {
871 /* XXX: Is this sane? */
872 rpc_delay(task, 3*HZ);
873 task->tk_status = -EAGAIN;
874 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875}
876
877/*
878 * 4. Get the server port number if not yet set
879 */
880static void
881call_bind(struct rpc_task *task)
882{
Chuck Leverec739ef2006-08-22 20:06:15 -0400883 struct rpc_xprt *xprt = task->tk_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
Chuck Lever46121cf2007-01-31 12:14:08 -0500885 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
Chuck Leverda351872005-08-11 16:25:11 -0400887 task->tk_action = call_connect;
Chuck Leverec739ef2006-08-22 20:06:15 -0400888 if (!xprt_bound(xprt)) {
Chuck Leverda351872005-08-11 16:25:11 -0400889 task->tk_action = call_bind_status;
Chuck Leverec739ef2006-08-22 20:06:15 -0400890 task->tk_timeout = xprt->bind_timeout;
Chuck Leverbbf7c1d2006-08-22 20:06:16 -0400891 xprt->ops->rpcbind(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 }
893}
894
895/*
Chuck Leverda351872005-08-11 16:25:11 -0400896 * 4a. Sort out bind result
897 */
898static void
899call_bind_status(struct rpc_task *task)
900{
901 int status = -EACCES;
902
903 if (task->tk_status >= 0) {
Chuck Lever46121cf2007-01-31 12:14:08 -0500904 dprint_status(task);
Chuck Leverda351872005-08-11 16:25:11 -0400905 task->tk_status = 0;
906 task->tk_action = call_connect;
907 return;
908 }
909
910 switch (task->tk_status) {
911 case -EACCES:
Chuck Lever46121cf2007-01-31 12:14:08 -0500912 dprintk("RPC: %5u remote rpcbind: RPC program/version "
913 "unavailable\n", task->tk_pid);
Chuck Leverea635a52005-10-06 23:12:58 -0400914 rpc_delay(task, 3*HZ);
Trond Myklebustda458282006-08-31 15:44:52 -0400915 goto retry_timeout;
Chuck Leverda351872005-08-11 16:25:11 -0400916 case -ETIMEDOUT:
Chuck Lever46121cf2007-01-31 12:14:08 -0500917 dprintk("RPC: %5u rpcbind request timed out\n",
Chuck Leverda351872005-08-11 16:25:11 -0400918 task->tk_pid);
Trond Myklebustda458282006-08-31 15:44:52 -0400919 goto retry_timeout;
Chuck Leverda351872005-08-11 16:25:11 -0400920 case -EPFNOSUPPORT:
Chuck Lever46121cf2007-01-31 12:14:08 -0500921 dprintk("RPC: %5u remote rpcbind service unavailable\n",
Chuck Leverda351872005-08-11 16:25:11 -0400922 task->tk_pid);
923 break;
924 case -EPROTONOSUPPORT:
Chuck Lever00a6e7b2007-03-29 16:48:33 -0400925 dprintk("RPC: %5u remote rpcbind version unavailable, retrying\n",
Chuck Leverda351872005-08-11 16:25:11 -0400926 task->tk_pid);
Chuck Lever00a6e7b2007-03-29 16:48:33 -0400927 task->tk_status = 0;
928 task->tk_action = call_bind;
929 return;
Chuck Leverda351872005-08-11 16:25:11 -0400930 default:
Chuck Lever46121cf2007-01-31 12:14:08 -0500931 dprintk("RPC: %5u unrecognized rpcbind error (%d)\n",
Chuck Leverda351872005-08-11 16:25:11 -0400932 task->tk_pid, -task->tk_status);
933 status = -EIO;
Chuck Leverda351872005-08-11 16:25:11 -0400934 }
935
936 rpc_exit(task, status);
937 return;
938
Trond Myklebustda458282006-08-31 15:44:52 -0400939retry_timeout:
940 task->tk_action = call_timeout;
Chuck Leverda351872005-08-11 16:25:11 -0400941}
942
943/*
944 * 4b. Connect to the RPC server
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 */
946static void
947call_connect(struct rpc_task *task)
948{
Chuck Leverda351872005-08-11 16:25:11 -0400949 struct rpc_xprt *xprt = task->tk_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
Chuck Lever46121cf2007-01-31 12:14:08 -0500951 dprintk("RPC: %5u call_connect xprt %p %s connected\n",
Chuck Leverda351872005-08-11 16:25:11 -0400952 task->tk_pid, xprt,
953 (xprt_connected(xprt) ? "is" : "is not"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
Chuck Leverda351872005-08-11 16:25:11 -0400955 task->tk_action = call_transmit;
956 if (!xprt_connected(xprt)) {
957 task->tk_action = call_connect_status;
958 if (task->tk_status < 0)
959 return;
960 xprt_connect(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962}
963
964/*
Chuck Leverda351872005-08-11 16:25:11 -0400965 * 4c. Sort out connect result
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 */
967static void
968call_connect_status(struct rpc_task *task)
969{
970 struct rpc_clnt *clnt = task->tk_client;
971 int status = task->tk_status;
972
Chuck Lever46121cf2007-01-31 12:14:08 -0500973 dprint_status(task);
Chuck Leverda351872005-08-11 16:25:11 -0400974
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 task->tk_status = 0;
976 if (status >= 0) {
977 clnt->cl_stats->netreconn++;
978 task->tk_action = call_transmit;
979 return;
980 }
981
Chuck Leverda351872005-08-11 16:25:11 -0400982 /* Something failed: remote service port may have changed */
Chuck Lever35f5a422006-01-03 09:55:50 +0100983 rpc_force_rebind(clnt);
Chuck Leverda351872005-08-11 16:25:11 -0400984
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 switch (status) {
986 case -ENOTCONN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 case -EAGAIN:
Chuck Leverda351872005-08-11 16:25:11 -0400988 task->tk_action = call_bind;
Trond Myklebustda458282006-08-31 15:44:52 -0400989 if (!RPC_IS_SOFT(task))
990 return;
991 /* if soft mounted, test if we've timed out */
992 case -ETIMEDOUT:
993 task->tk_action = call_timeout;
994 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 }
Trond Myklebustda458282006-08-31 15:44:52 -0400996 rpc_exit(task, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997}
998
999/*
1000 * 5. Transmit the RPC request, and wait for reply
1001 */
1002static void
1003call_transmit(struct rpc_task *task)
1004{
Chuck Lever46121cf2007-01-31 12:14:08 -05001005 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
1007 task->tk_action = call_status;
1008 if (task->tk_status < 0)
1009 return;
1010 task->tk_status = xprt_prepare_transmit(task);
1011 if (task->tk_status != 0)
1012 return;
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04001013 task->tk_action = call_transmit_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 /* Encode here so that rpcsec_gss can use correct sequence number. */
Trond Myklebust940e3312005-11-09 21:45:24 -05001015 if (rpc_task_need_encode(task)) {
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04001016 BUG_ON(task->tk_rqstp->rq_bytes_sent != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 call_encode(task);
Trond Myklebust5e5ce5b2005-10-18 14:20:11 -07001018 /* Did the encode result in an error condition? */
1019 if (task->tk_status != 0)
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04001020 return;
Trond Myklebust5e5ce5b2005-10-18 14:20:11 -07001021 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 xprt_transmit(task);
1023 if (task->tk_status < 0)
1024 return;
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04001025 /*
1026 * On success, ensure that we call xprt_end_transmit() before sleeping
1027 * in order to allow access to the socket to other RPC requests.
1028 */
1029 call_transmit_status(task);
1030 if (task->tk_msg.rpc_proc->p_decode != NULL)
1031 return;
1032 task->tk_action = rpc_exit_task;
1033 rpc_wake_up_task(task);
1034}
1035
1036/*
1037 * 5a. Handle cleanup after a transmission
1038 */
1039static void
1040call_transmit_status(struct rpc_task *task)
1041{
1042 task->tk_action = call_status;
1043 /*
1044 * Special case: if we've been waiting on the socket's write_space()
1045 * callback, then don't call xprt_end_transmit().
1046 */
1047 if (task->tk_status == -EAGAIN)
1048 return;
1049 xprt_end_transmit(task);
Trond Myklebust940e3312005-11-09 21:45:24 -05001050 rpc_task_force_reencode(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051}
1052
1053/*
1054 * 6. Sort out the RPC call status
1055 */
1056static void
1057call_status(struct rpc_task *task)
1058{
1059 struct rpc_clnt *clnt = task->tk_client;
1060 struct rpc_rqst *req = task->tk_rqstp;
1061 int status;
1062
1063 if (req->rq_received > 0 && !req->rq_bytes_sent)
1064 task->tk_status = req->rq_received;
1065
Chuck Lever46121cf2007-01-31 12:14:08 -05001066 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
1068 status = task->tk_status;
1069 if (status >= 0) {
1070 task->tk_action = call_decode;
1071 return;
1072 }
1073
1074 task->tk_status = 0;
1075 switch(status) {
Trond Myklebust76303992006-08-30 14:32:49 -04001076 case -EHOSTDOWN:
1077 case -EHOSTUNREACH:
1078 case -ENETUNREACH:
1079 /*
1080 * Delay any retries for 3 seconds, then handle as if it
1081 * were a timeout.
1082 */
1083 rpc_delay(task, 3*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 case -ETIMEDOUT:
1085 task->tk_action = call_timeout;
Trond Myklebust241c39b2007-04-20 16:12:55 -04001086 if (task->tk_client->cl_discrtry)
1087 xprt_disconnect(task->tk_xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 break;
1089 case -ECONNREFUSED:
1090 case -ENOTCONN:
Chuck Lever35f5a422006-01-03 09:55:50 +01001091 rpc_force_rebind(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 task->tk_action = call_bind;
1093 break;
1094 case -EAGAIN:
1095 task->tk_action = call_transmit;
1096 break;
1097 case -EIO:
1098 /* shutdown or soft timeout */
1099 rpc_exit(task, status);
1100 break;
1101 default:
Chuck Leverf518e35a2006-01-03 09:55:52 +01001102 printk("%s: RPC call returned error %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 clnt->cl_protname, -status);
1104 rpc_exit(task, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 }
1106}
1107
1108/*
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04001109 * 6a. Handle RPC timeout
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 * We do not release the request slot, so we keep using the
1111 * same XID for all retransmits.
1112 */
1113static void
1114call_timeout(struct rpc_task *task)
1115{
1116 struct rpc_clnt *clnt = task->tk_client;
1117
1118 if (xprt_adjust_timeout(task->tk_rqstp) == 0) {
Chuck Lever46121cf2007-01-31 12:14:08 -05001119 dprintk("RPC: %5u call_timeout (minor)\n", task->tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 goto retry;
1121 }
1122
Chuck Lever46121cf2007-01-31 12:14:08 -05001123 dprintk("RPC: %5u call_timeout (major)\n", task->tk_pid);
Chuck Leveref759a22006-03-20 13:44:17 -05001124 task->tk_timeouts++;
1125
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 if (RPC_IS_SOFT(task)) {
Chuck Leverf518e35a2006-01-03 09:55:52 +01001127 printk(KERN_NOTICE "%s: server %s not responding, timed out\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 clnt->cl_protname, clnt->cl_server);
1129 rpc_exit(task, -EIO);
1130 return;
1131 }
1132
Chuck Leverf518e35a2006-01-03 09:55:52 +01001133 if (!(task->tk_flags & RPC_CALL_MAJORSEEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 task->tk_flags |= RPC_CALL_MAJORSEEN;
1135 printk(KERN_NOTICE "%s: server %s not responding, still trying\n",
1136 clnt->cl_protname, clnt->cl_server);
1137 }
Chuck Lever35f5a422006-01-03 09:55:50 +01001138 rpc_force_rebind(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
1140retry:
1141 clnt->cl_stats->rpcretrans++;
1142 task->tk_action = call_bind;
1143 task->tk_status = 0;
1144}
1145
1146/*
1147 * 7. Decode the RPC reply
1148 */
1149static void
1150call_decode(struct rpc_task *task)
1151{
1152 struct rpc_clnt *clnt = task->tk_client;
1153 struct rpc_rqst *req = task->tk_rqstp;
1154 kxdrproc_t decode = task->tk_msg.rpc_proc->p_decode;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001155 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
Chuck Lever46121cf2007-01-31 12:14:08 -05001157 dprintk("RPC: %5u call_decode (status %d)\n",
1158 task->tk_pid, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
Chuck Leverf518e35a2006-01-03 09:55:52 +01001160 if (task->tk_flags & RPC_CALL_MAJORSEEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 printk(KERN_NOTICE "%s: server %s OK\n",
1162 clnt->cl_protname, clnt->cl_server);
1163 task->tk_flags &= ~RPC_CALL_MAJORSEEN;
1164 }
1165
1166 if (task->tk_status < 12) {
1167 if (!RPC_IS_SOFT(task)) {
1168 task->tk_action = call_bind;
1169 clnt->cl_stats->rpcretrans++;
1170 goto out_retry;
1171 }
Chuck Lever46121cf2007-01-31 12:14:08 -05001172 dprintk("RPC: %s: too small RPC reply size (%d bytes)\n",
1173 clnt->cl_protname, task->tk_status);
Trond Myklebustda458282006-08-31 15:44:52 -04001174 task->tk_action = call_timeout;
1175 goto out_retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 }
1177
Trond Myklebust43ac3f22006-03-20 13:44:51 -05001178 /*
1179 * Ensure that we see all writes made by xprt_complete_rqst()
1180 * before it changed req->rq_received.
1181 */
1182 smp_rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 req->rq_rcv_buf.len = req->rq_private_buf.len;
1184
1185 /* Check that the softirq receive buffer is valid */
1186 WARN_ON(memcmp(&req->rq_rcv_buf, &req->rq_private_buf,
1187 sizeof(req->rq_rcv_buf)) != 0);
1188
1189 /* Verify the RPC header */
Trond Myklebustabbcf282006-01-03 09:55:03 +01001190 p = call_verify(task);
1191 if (IS_ERR(p)) {
1192 if (p == ERR_PTR(-EAGAIN))
1193 goto out_retry;
1194 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 }
1196
Trond Myklebustabbcf282006-01-03 09:55:03 +01001197 task->tk_action = rpc_exit_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
Trond Myklebust6d5fcb52006-10-18 16:01:06 -04001199 if (decode) {
1200 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 task->tk_status = rpcauth_unwrap_resp(task, decode, req, p,
1202 task->tk_msg.rpc_resp);
Trond Myklebust6d5fcb52006-10-18 16:01:06 -04001203 unlock_kernel();
1204 }
Chuck Lever46121cf2007-01-31 12:14:08 -05001205 dprintk("RPC: %5u call_decode result %d\n", task->tk_pid,
1206 task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 return;
1208out_retry:
1209 req->rq_received = req->rq_private_buf.len = 0;
1210 task->tk_status = 0;
Trond Myklebust241c39b2007-04-20 16:12:55 -04001211 if (task->tk_client->cl_discrtry)
1212 xprt_disconnect(task->tk_xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213}
1214
1215/*
1216 * 8. Refresh the credentials if rejected by the server
1217 */
1218static void
1219call_refresh(struct rpc_task *task)
1220{
Chuck Lever46121cf2007-01-31 12:14:08 -05001221 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
1223 xprt_release(task); /* Must do to obtain new XID */
1224 task->tk_action = call_refreshresult;
1225 task->tk_status = 0;
1226 task->tk_client->cl_stats->rpcauthrefresh++;
1227 rpcauth_refreshcred(task);
1228}
1229
1230/*
1231 * 8a. Process the results of a credential refresh
1232 */
1233static void
1234call_refreshresult(struct rpc_task *task)
1235{
1236 int status = task->tk_status;
Chuck Lever46121cf2007-01-31 12:14:08 -05001237
1238 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
1240 task->tk_status = 0;
1241 task->tk_action = call_reserve;
1242 if (status >= 0 && rpcauth_uptodatecred(task))
1243 return;
1244 if (status == -EACCES) {
1245 rpc_exit(task, -EACCES);
1246 return;
1247 }
1248 task->tk_action = call_refresh;
1249 if (status != -ETIMEDOUT)
1250 rpc_delay(task, 3*HZ);
1251 return;
1252}
1253
1254/*
1255 * Call header serialization
1256 */
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001257static __be32 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258call_header(struct rpc_task *task)
1259{
1260 struct rpc_clnt *clnt = task->tk_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 struct rpc_rqst *req = task->tk_rqstp;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001262 __be32 *p = req->rq_svec[0].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
1264 /* FIXME: check buffer size? */
Chuck Lever808012f2005-08-25 16:25:49 -07001265
1266 p = xprt_skip_transport_header(task->tk_xprt, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 *p++ = req->rq_xid; /* XID */
1268 *p++ = htonl(RPC_CALL); /* CALL */
1269 *p++ = htonl(RPC_VERSION); /* RPC version */
1270 *p++ = htonl(clnt->cl_prog); /* program number */
1271 *p++ = htonl(clnt->cl_vers); /* program version */
1272 *p++ = htonl(task->tk_msg.rpc_proc->p_proc); /* procedure */
Trond Myklebust334ccfd2005-06-22 17:16:19 +00001273 p = rpcauth_marshcred(task, p);
1274 req->rq_slen = xdr_adjust_iovec(&req->rq_svec[0], p);
1275 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276}
1277
1278/*
1279 * Reply header verification
1280 */
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001281static __be32 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282call_verify(struct rpc_task *task)
1283{
1284 struct kvec *iov = &task->tk_rqstp->rq_rcv_buf.head[0];
1285 int len = task->tk_rqstp->rq_rcv_buf.len >> 2;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001286 __be32 *p = iov->iov_base;
1287 u32 n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 int error = -EACCES;
1289
David Howellse8896492006-08-24 15:44:19 -04001290 if ((task->tk_rqstp->rq_rcv_buf.len & 3) != 0) {
1291 /* RFC-1014 says that the representation of XDR data must be a
1292 * multiple of four bytes
1293 * - if it isn't pointer subtraction in the NFS client may give
1294 * undefined results
1295 */
1296 printk(KERN_WARNING
1297 "call_verify: XDR representation not a multiple of"
1298 " 4 bytes: 0x%x\n", task->tk_rqstp->rq_rcv_buf.len);
1299 goto out_eio;
1300 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 if ((len -= 3) < 0)
1302 goto out_overflow;
1303 p += 1; /* skip XID */
1304
1305 if ((n = ntohl(*p++)) != RPC_REPLY) {
1306 printk(KERN_WARNING "call_verify: not an RPC reply: %x\n", n);
Trond Myklebustabbcf282006-01-03 09:55:03 +01001307 goto out_garbage;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 }
1309 if ((n = ntohl(*p++)) != RPC_MSG_ACCEPTED) {
1310 if (--len < 0)
1311 goto out_overflow;
1312 switch ((n = ntohl(*p++))) {
1313 case RPC_AUTH_ERROR:
1314 break;
1315 case RPC_MISMATCH:
Chuck Lever46121cf2007-01-31 12:14:08 -05001316 dprintk("RPC: %5u %s: RPC call version "
1317 "mismatch!\n",
1318 task->tk_pid, __FUNCTION__);
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00001319 error = -EPROTONOSUPPORT;
1320 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 default:
Chuck Lever46121cf2007-01-31 12:14:08 -05001322 dprintk("RPC: %5u %s: RPC call rejected, "
1323 "unknown error: %x\n",
1324 task->tk_pid, __FUNCTION__, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 goto out_eio;
1326 }
1327 if (--len < 0)
1328 goto out_overflow;
1329 switch ((n = ntohl(*p++))) {
1330 case RPC_AUTH_REJECTEDCRED:
1331 case RPC_AUTH_REJECTEDVERF:
1332 case RPCSEC_GSS_CREDPROBLEM:
1333 case RPCSEC_GSS_CTXPROBLEM:
1334 if (!task->tk_cred_retry)
1335 break;
1336 task->tk_cred_retry--;
Chuck Lever46121cf2007-01-31 12:14:08 -05001337 dprintk("RPC: %5u %s: retry stale creds\n",
1338 task->tk_pid, __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 rpcauth_invalcred(task);
1340 task->tk_action = call_refresh;
Trond Myklebustabbcf282006-01-03 09:55:03 +01001341 goto out_retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 case RPC_AUTH_BADCRED:
1343 case RPC_AUTH_BADVERF:
1344 /* possibly garbled cred/verf? */
1345 if (!task->tk_garb_retry)
1346 break;
1347 task->tk_garb_retry--;
Chuck Lever46121cf2007-01-31 12:14:08 -05001348 dprintk("RPC: %5u %s: retry garbled creds\n",
1349 task->tk_pid, __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 task->tk_action = call_bind;
Trond Myklebustabbcf282006-01-03 09:55:03 +01001351 goto out_retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 case RPC_AUTH_TOOWEAK:
Levent Serinol1356b8c2006-03-20 13:44:11 -05001353 printk(KERN_NOTICE "call_verify: server %s requires stronger "
1354 "authentication.\n", task->tk_client->cl_server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 break;
1356 default:
1357 printk(KERN_WARNING "call_verify: unknown auth error: %x\n", n);
1358 error = -EIO;
1359 }
Chuck Lever46121cf2007-01-31 12:14:08 -05001360 dprintk("RPC: %5u %s: call rejected %d\n",
1361 task->tk_pid, __FUNCTION__, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 goto out_err;
1363 }
1364 if (!(p = rpcauth_checkverf(task, p))) {
1365 printk(KERN_WARNING "call_verify: auth check failed\n");
Trond Myklebustabbcf282006-01-03 09:55:03 +01001366 goto out_garbage; /* bad verifier, retry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 }
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001368 len = p - (__be32 *)iov->iov_base - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 if (len < 0)
1370 goto out_overflow;
1371 switch ((n = ntohl(*p++))) {
1372 case RPC_SUCCESS:
1373 return p;
1374 case RPC_PROG_UNAVAIL:
Chuck Lever46121cf2007-01-31 12:14:08 -05001375 dprintk("RPC: %5u %s: program %u is unsupported by server %s\n",
1376 task->tk_pid, __FUNCTION__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 (unsigned int)task->tk_client->cl_prog,
1378 task->tk_client->cl_server);
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00001379 error = -EPFNOSUPPORT;
1380 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 case RPC_PROG_MISMATCH:
Chuck Lever46121cf2007-01-31 12:14:08 -05001382 dprintk("RPC: %5u %s: program %u, version %u unsupported by "
1383 "server %s\n", task->tk_pid, __FUNCTION__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 (unsigned int)task->tk_client->cl_prog,
1385 (unsigned int)task->tk_client->cl_vers,
1386 task->tk_client->cl_server);
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00001387 error = -EPROTONOSUPPORT;
1388 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 case RPC_PROC_UNAVAIL:
Chuck Lever46121cf2007-01-31 12:14:08 -05001390 dprintk("RPC: %5u %s: proc %p unsupported by program %u, "
1391 "version %u on server %s\n",
1392 task->tk_pid, __FUNCTION__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 task->tk_msg.rpc_proc,
1394 task->tk_client->cl_prog,
1395 task->tk_client->cl_vers,
1396 task->tk_client->cl_server);
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00001397 error = -EOPNOTSUPP;
1398 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 case RPC_GARBAGE_ARGS:
Chuck Lever46121cf2007-01-31 12:14:08 -05001400 dprintk("RPC: %5u %s: server saw garbage\n",
1401 task->tk_pid, __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 break; /* retry */
1403 default:
1404 printk(KERN_WARNING "call_verify: server accept status: %x\n", n);
1405 /* Also retry */
1406 }
1407
Trond Myklebustabbcf282006-01-03 09:55:03 +01001408out_garbage:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 task->tk_client->cl_stats->rpcgarbage++;
1410 if (task->tk_garb_retry) {
1411 task->tk_garb_retry--;
Chuck Lever46121cf2007-01-31 12:14:08 -05001412 dprintk("RPC: %5u %s: retrying\n",
1413 task->tk_pid, __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 task->tk_action = call_bind;
Trond Myklebustabbcf282006-01-03 09:55:03 +01001415out_retry:
1416 return ERR_PTR(-EAGAIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 }
1418 printk(KERN_WARNING "RPC %s: retry failed, exit EIO\n", __FUNCTION__);
1419out_eio:
1420 error = -EIO;
1421out_err:
1422 rpc_exit(task, error);
Trond Myklebustabbcf282006-01-03 09:55:03 +01001423 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424out_overflow:
1425 printk(KERN_WARNING "RPC %s: server reply was truncated.\n", __FUNCTION__);
Trond Myklebustabbcf282006-01-03 09:55:03 +01001426 goto out_garbage;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427}
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00001428
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001429static int rpcproc_encode_null(void *rqstp, __be32 *data, void *obj)
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00001430{
1431 return 0;
1432}
1433
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001434static int rpcproc_decode_null(void *rqstp, __be32 *data, void *obj)
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00001435{
1436 return 0;
1437}
1438
1439static struct rpc_procinfo rpcproc_null = {
1440 .p_encode = rpcproc_encode_null,
1441 .p_decode = rpcproc_decode_null,
1442};
1443
1444int rpc_ping(struct rpc_clnt *clnt, int flags)
1445{
1446 struct rpc_message msg = {
1447 .rpc_proc = &rpcproc_null,
1448 };
1449 int err;
1450 msg.rpc_cred = authnull_ops.lookup_cred(NULL, NULL, 0);
1451 err = rpc_call_sync(clnt, &msg, flags);
1452 put_rpccred(msg.rpc_cred);
1453 return err;
1454}
Trond Myklebust188fef12007-06-16 14:18:40 -04001455
1456#ifdef RPC_DEBUG
1457void rpc_show_tasks(void)
1458{
1459 struct rpc_clnt *clnt;
1460 struct rpc_task *t;
1461
1462 spin_lock(&rpc_client_lock);
1463 if (list_empty(&all_clients))
1464 goto out;
1465 printk("-pid- proc flgs status -client- -prog- --rqstp- -timeout "
1466 "-rpcwait -action- ---ops--\n");
1467 list_for_each_entry(clnt, &all_clients, cl_clients) {
1468 if (list_empty(&clnt->cl_tasks))
1469 continue;
1470 spin_lock(&clnt->cl_lock);
1471 list_for_each_entry(t, &clnt->cl_tasks, tk_task) {
1472 const char *rpc_waitq = "none";
1473
1474 if (RPC_IS_QUEUED(t))
1475 rpc_waitq = rpc_qname(t->u.tk_wait.rpc_waitq);
1476
1477 printk("%5u %04d %04x %6d %8p %6d %8p %8ld %8s %8p %8p\n",
1478 t->tk_pid,
1479 (t->tk_msg.rpc_proc ? t->tk_msg.rpc_proc->p_proc : -1),
1480 t->tk_flags, t->tk_status,
1481 t->tk_client,
1482 (t->tk_client ? t->tk_client->cl_prog : 0),
1483 t->tk_rqstp, t->tk_timeout,
1484 rpc_waitq,
1485 t->tk_action, t->tk_ops);
1486 }
1487 spin_unlock(&clnt->cl_lock);
1488 }
1489out:
1490 spin_unlock(&rpc_client_lock);
1491}
1492#endif