blob: 613c10e4ac31f0a4710a290e18e0a0cdedf0685c [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070047static DECLARE_WAIT_QUEUE_HEAD(destroy_wait);
48
49
50static void call_start(struct rpc_task *task);
51static void call_reserve(struct rpc_task *task);
52static void call_reserveresult(struct rpc_task *task);
53static void call_allocate(struct rpc_task *task);
54static void call_encode(struct rpc_task *task);
55static void call_decode(struct rpc_task *task);
56static void call_bind(struct rpc_task *task);
Chuck Leverda351872005-08-11 16:25:11 -040057static void call_bind_status(struct rpc_task *task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058static void call_transmit(struct rpc_task *task);
59static void call_status(struct rpc_task *task);
Trond Myklebust940e3312005-11-09 21:45:24 -050060static void call_transmit_status(struct rpc_task *task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061static void call_refresh(struct rpc_task *task);
62static void call_refreshresult(struct rpc_task *task);
63static void call_timeout(struct rpc_task *task);
64static void call_connect(struct rpc_task *task);
65static void call_connect_status(struct rpc_task *task);
Alexey Dobriyand8ed0292006-09-26 22:29:38 -070066static __be32 * call_header(struct rpc_task *task);
67static __be32 * call_verify(struct rpc_task *task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69
70static int
71rpc_setup_pipedir(struct rpc_clnt *clnt, char *dir_name)
72{
Trond Myklebustf1345852005-09-23 11:08:25 -040073 static uint32_t clntid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 int error;
75
Trond Myklebust54281542006-03-20 13:44:49 -050076 clnt->cl_vfsmnt = ERR_PTR(-ENOENT);
77 clnt->cl_dentry = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 if (dir_name == NULL)
79 return 0;
Trond Myklebust54281542006-03-20 13:44:49 -050080
81 clnt->cl_vfsmnt = rpc_get_mount();
82 if (IS_ERR(clnt->cl_vfsmnt))
83 return PTR_ERR(clnt->cl_vfsmnt);
84
Trond Myklebustf1345852005-09-23 11:08:25 -040085 for (;;) {
86 snprintf(clnt->cl_pathname, sizeof(clnt->cl_pathname),
87 "%s/clnt%x", dir_name,
88 (unsigned int)clntid++);
89 clnt->cl_pathname[sizeof(clnt->cl_pathname) - 1] = '\0';
90 clnt->cl_dentry = rpc_mkdir(clnt->cl_pathname, clnt);
91 if (!IS_ERR(clnt->cl_dentry))
92 return 0;
Christoph Hellwig278c9952005-07-24 23:53:01 +010093 error = PTR_ERR(clnt->cl_dentry);
Trond Myklebustf1345852005-09-23 11:08:25 -040094 if (error != -EEXIST) {
95 printk(KERN_INFO "RPC: Couldn't create pipefs entry %s, error %d\n",
96 clnt->cl_pathname, error);
Trond Myklebust54281542006-03-20 13:44:49 -050097 rpc_put_mount();
Trond Myklebustf1345852005-09-23 11:08:25 -040098 return error;
99 }
Christoph Hellwig278c9952005-07-24 23:53:01 +0100100 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101}
102
Chuck Leverff9aa5e2006-08-22 20:06:21 -0400103static 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 -0700104{
105 struct rpc_version *version;
106 struct rpc_clnt *clnt = NULL;
J. Bruce Fields6a192752005-06-22 17:16:23 +0000107 struct rpc_auth *auth;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 int err;
109 int len;
110
Chuck Lever46121cf2007-01-31 12:14:08 -0500111 dprintk("RPC: creating %s client for %s (xprt %p)\n",
112 program->name, servname, xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114 err = -EINVAL;
115 if (!xprt)
Adrian Bunk712917d2006-03-13 21:20:47 -0800116 goto out_no_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 if (vers >= program->nrvers || !(version = program->version[vers]))
118 goto out_err;
119
120 err = -ENOMEM;
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700121 clnt = kzalloc(sizeof(*clnt), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 if (!clnt)
123 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 atomic_set(&clnt->cl_count, 1);
125 clnt->cl_parent = clnt;
126
127 clnt->cl_server = clnt->cl_inline_name;
128 len = strlen(servname) + 1;
129 if (len > sizeof(clnt->cl_inline_name)) {
130 char *buf = kmalloc(len, GFP_KERNEL);
131 if (buf != 0)
132 clnt->cl_server = buf;
133 else
134 len = sizeof(clnt->cl_inline_name);
135 }
136 strlcpy(clnt->cl_server, servname, len);
137
138 clnt->cl_xprt = xprt;
139 clnt->cl_procinfo = version->procs;
140 clnt->cl_maxproc = version->nrprocs;
141 clnt->cl_protname = program->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 clnt->cl_prog = program->number;
143 clnt->cl_vers = version->number;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 clnt->cl_stats = program->stats;
Chuck Lever11c556b2006-03-20 13:44:22 -0500145 clnt->cl_metrics = rpc_alloc_iostats(clnt);
Trond Myklebust23bf85b2006-11-21 10:40:23 -0500146 err = -ENOMEM;
147 if (clnt->cl_metrics == NULL)
148 goto out_no_stats;
Trond Myklebust3e32a5d2006-11-16 11:37:27 -0500149 clnt->cl_program = program;
Trond Myklebust6529eba2007-06-14 16:40:14 -0400150 INIT_LIST_HEAD(&clnt->cl_tasks);
Trond Myklebust4bef61f2007-06-16 14:17:01 -0400151 spin_lock_init(&clnt->cl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Chuck Leverec739ef2006-08-22 20:06:15 -0400153 if (!xprt_bound(clnt->cl_xprt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 clnt->cl_autobind = 1;
155
156 clnt->cl_rtt = &clnt->cl_rtt_default;
157 rpc_init_rtt(&clnt->cl_rtt_default, xprt->timeout.to_initval);
158
Trond Myklebust34f52e32007-06-14 16:40:31 -0400159 kref_init(&clnt->cl_kref);
160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 err = rpc_setup_pipedir(clnt, program->pipe_dir_name);
162 if (err < 0)
163 goto out_no_path;
164
J. Bruce Fields6a192752005-06-22 17:16:23 +0000165 auth = rpcauth_create(flavor, clnt);
166 if (IS_ERR(auth)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 printk(KERN_INFO "RPC: Couldn't create auth handle (flavor %u)\n",
168 flavor);
J. Bruce Fields6a192752005-06-22 17:16:23 +0000169 err = PTR_ERR(auth);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 goto out_no_auth;
171 }
172
173 /* save the nodename */
Serge E. Hallyne9ff3992006-10-02 02:18:11 -0700174 clnt->cl_nodelen = strlen(utsname()->nodename);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 if (clnt->cl_nodelen > UNX_MAXNODENAME)
176 clnt->cl_nodelen = UNX_MAXNODENAME;
Serge E. Hallyne9ff3992006-10-02 02:18:11 -0700177 memcpy(clnt->cl_nodename, utsname()->nodename, clnt->cl_nodelen);
Trond Myklebust6529eba2007-06-14 16:40:14 -0400178 rpc_register_client(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 return clnt;
180
181out_no_auth:
Trond Myklebust54281542006-03-20 13:44:49 -0500182 if (!IS_ERR(clnt->cl_dentry)) {
Trond Myklebustdff02cc2006-07-31 14:17:18 -0700183 rpc_rmdir(clnt->cl_dentry);
Trond Myklebust54281542006-03-20 13:44:49 -0500184 rpc_put_mount();
185 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186out_no_path:
Trond Myklebust23bf85b2006-11-21 10:40:23 -0500187 rpc_free_iostats(clnt->cl_metrics);
188out_no_stats:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 if (clnt->cl_server != clnt->cl_inline_name)
190 kfree(clnt->cl_server);
191 kfree(clnt);
192out_err:
Trond Myklebust6b6ca862006-09-05 12:55:57 -0400193 xprt_put(xprt);
Adrian Bunk712917d2006-03-13 21:20:47 -0800194out_no_xprt:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 return ERR_PTR(err);
196}
197
Chuck Leverc2866762006-08-22 20:06:20 -0400198/*
199 * rpc_create - create an RPC client and transport with one call
200 * @args: rpc_clnt create argument structure
201 *
202 * Creates and initializes an RPC transport and an RPC client.
203 *
204 * It can ping the server in order to determine if it is up, and to see if
205 * it supports this program and version. RPC_CLNT_CREATE_NOPING disables
206 * this behavior so asynchronous tasks can also use rpc_create.
207 */
208struct rpc_clnt *rpc_create(struct rpc_create_args *args)
209{
210 struct rpc_xprt *xprt;
211 struct rpc_clnt *clnt;
212
213 xprt = xprt_create_transport(args->protocol, args->address,
214 args->addrsize, args->timeout);
215 if (IS_ERR(xprt))
216 return (struct rpc_clnt *)xprt;
217
218 /*
219 * By default, kernel RPC client connects from a reserved port.
220 * CAP_NET_BIND_SERVICE will not be set for unprivileged requesters,
221 * but it is always enabled for rpciod, which handles the connect
222 * operation.
223 */
224 xprt->resvport = 1;
225 if (args->flags & RPC_CLNT_CREATE_NONPRIVPORT)
226 xprt->resvport = 0;
227
228 dprintk("RPC: creating %s client for %s (xprt %p)\n",
Chuck Lever46121cf2007-01-31 12:14:08 -0500229 args->program->name, args->servername, xprt);
Chuck Leverc2866762006-08-22 20:06:20 -0400230
231 clnt = rpc_new_client(xprt, args->servername, args->program,
232 args->version, args->authflavor);
233 if (IS_ERR(clnt))
234 return clnt;
235
236 if (!(args->flags & RPC_CLNT_CREATE_NOPING)) {
237 int err = rpc_ping(clnt, RPC_TASK_SOFT|RPC_TASK_NOINTR);
238 if (err != 0) {
239 rpc_shutdown_client(clnt);
240 return ERR_PTR(err);
241 }
242 }
243
244 clnt->cl_softrtry = 1;
245 if (args->flags & RPC_CLNT_CREATE_HARDRTRY)
246 clnt->cl_softrtry = 0;
247
248 if (args->flags & RPC_CLNT_CREATE_INTR)
249 clnt->cl_intr = 1;
250 if (args->flags & RPC_CLNT_CREATE_AUTOBIND)
251 clnt->cl_autobind = 1;
Chuck Lever43d78ef2007-02-06 18:26:11 -0500252 if (args->flags & RPC_CLNT_CREATE_DISCRTRY)
253 clnt->cl_discrtry = 1;
Chuck Leverc2866762006-08-22 20:06:20 -0400254
255 return clnt;
256}
Chuck Leverb86acd52006-08-22 20:06:22 -0400257EXPORT_SYMBOL_GPL(rpc_create);
Chuck Leverc2866762006-08-22 20:06:20 -0400258
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259/*
260 * This function clones the RPC client structure. It allows us to share the
261 * same transport while varying parameters such as the authentication
262 * flavour.
263 */
264struct rpc_clnt *
265rpc_clone_client(struct rpc_clnt *clnt)
266{
267 struct rpc_clnt *new;
Trond Myklebust3e32a5d2006-11-16 11:37:27 -0500268 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Arnaldo Carvalho de Meloe69062b2006-11-21 01:21:34 -0200270 new = kmemdup(clnt, sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 if (!new)
272 goto out_no_clnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 atomic_set(&new->cl_count, 1);
Trond Myklebust23bf85b2006-11-21 10:40:23 -0500274 new->cl_metrics = rpc_alloc_iostats(clnt);
275 if (new->cl_metrics == NULL)
276 goto out_no_stats;
Trond Myklebust34f52e32007-06-14 16:40:31 -0400277 kref_init(&new->cl_kref);
Trond Myklebust3e32a5d2006-11-16 11:37:27 -0500278 err = rpc_setup_pipedir(new, clnt->cl_program->pipe_dir_name);
279 if (err != 0)
280 goto out_no_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 new->cl_parent = clnt;
Trond Myklebust8ad7c892007-06-14 16:40:32 -0400282 kref_get(&clnt->cl_kref);
Trond Myklebust6b6ca862006-09-05 12:55:57 -0400283 new->cl_xprt = xprt_get(clnt->cl_xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 /* Turn off autobind on clones */
285 new->cl_autobind = 0;
Trond Myklebust6529eba2007-06-14 16:40:14 -0400286 INIT_LIST_HEAD(&new->cl_tasks);
Trond Myklebust4bef61f2007-06-16 14:17:01 -0400287 spin_lock_init(&new->cl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 rpc_init_rtt(&new->cl_rtt_default, clnt->cl_xprt->timeout.to_initval);
289 if (new->cl_auth)
290 atomic_inc(&new->cl_auth->au_count);
Trond Myklebust6529eba2007-06-14 16:40:14 -0400291 rpc_register_client(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 return new;
Trond Myklebust3e32a5d2006-11-16 11:37:27 -0500293out_no_path:
294 rpc_free_iostats(new->cl_metrics);
Trond Myklebust23bf85b2006-11-21 10:40:23 -0500295out_no_stats:
296 kfree(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297out_no_clnt:
Chuck Lever46121cf2007-01-31 12:14:08 -0500298 dprintk("RPC: %s: returned error %d\n", __FUNCTION__, err);
Trond Myklebust3e32a5d2006-11-16 11:37:27 -0500299 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300}
301
302/*
303 * Properly shut down an RPC client, terminating all outstanding
Trond Myklebust90c57552007-06-09 19:49:36 -0400304 * requests.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 */
306int
307rpc_shutdown_client(struct rpc_clnt *clnt)
308{
Trond Myklebust34f52e32007-06-14 16:40:31 -0400309 dprintk("RPC: shutting down %s client for %s\n",
310 clnt->cl_protname, clnt->cl_server);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
Trond Myklebust34f52e32007-06-14 16:40:31 -0400312 while (!list_empty(&clnt->cl_tasks)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 rpc_killall_tasks(clnt);
Ingo Molnar532347e2006-01-09 20:52:53 -0800314 wait_event_timeout(destroy_wait,
Trond Myklebust34f52e32007-06-14 16:40:31 -0400315 list_empty(&clnt->cl_tasks), 1*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 }
317
318 return rpc_destroy_client(clnt);
319}
320
321/*
Trond Myklebust34f52e32007-06-14 16:40:31 -0400322 * Free an RPC client
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 */
Trond Myklebust34f52e32007-06-14 16:40:31 -0400324static void
325rpc_free_client(struct kref *kref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
Trond Myklebust34f52e32007-06-14 16:40:31 -0400327 struct rpc_clnt *clnt = container_of(kref, struct rpc_clnt, cl_kref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Chuck Lever46121cf2007-01-31 12:14:08 -0500329 dprintk("RPC: destroying %s client for %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 clnt->cl_protname, clnt->cl_server);
331 if (clnt->cl_auth) {
332 rpcauth_destroy(clnt->cl_auth);
333 clnt->cl_auth = NULL;
334 }
Trond Myklebust8f8e7a52006-08-14 13:11:15 -0400335 if (!IS_ERR(clnt->cl_dentry)) {
Trond Myklebustdff02cc2006-07-31 14:17:18 -0700336 rpc_rmdir(clnt->cl_dentry);
Trond Myklebust8f8e7a52006-08-14 13:11:15 -0400337 rpc_put_mount();
338 }
Trond Myklebust3e32a5d2006-11-16 11:37:27 -0500339 if (clnt->cl_parent != clnt) {
Trond Myklebust8ad7c892007-06-14 16:40:32 -0400340 rpc_release_client(clnt->cl_parent);
Trond Myklebust3e32a5d2006-11-16 11:37:27 -0500341 goto out_free;
342 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 if (clnt->cl_server != clnt->cl_inline_name)
344 kfree(clnt->cl_server);
345out_free:
Trond Myklebust6529eba2007-06-14 16:40:14 -0400346 rpc_unregister_client(clnt);
Chuck Lever11c556b2006-03-20 13:44:22 -0500347 rpc_free_iostats(clnt->cl_metrics);
348 clnt->cl_metrics = NULL;
Trond Myklebust6b6ca862006-09-05 12:55:57 -0400349 xprt_put(clnt->cl_xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 kfree(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351}
352
353/*
Trond Myklebust34f52e32007-06-14 16:40:31 -0400354 * Release reference to the RPC client
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 */
356void
357rpc_release_client(struct rpc_clnt *clnt)
358{
Trond Myklebust34f52e32007-06-14 16:40:31 -0400359 dprintk("RPC: rpc_release_client(%p)\n", clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
Trond Myklebust34f52e32007-06-14 16:40:31 -0400361 if (list_empty(&clnt->cl_tasks))
362 wake_up(&destroy_wait);
Trond Myklebust34f52e32007-06-14 16:40:31 -0400363 kref_put(&clnt->cl_kref, rpc_free_client);
364}
365
366/*
367 * Delete an RPC client
368 */
369int
370rpc_destroy_client(struct rpc_clnt *clnt)
371{
372 if (!atomic_dec_and_test(&clnt->cl_count))
373 return 1;
374 kref_put(&clnt->cl_kref, rpc_free_client);
375 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376}
377
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000378/**
379 * rpc_bind_new_program - bind a new RPC program to an existing client
380 * @old - old rpc_client
381 * @program - rpc program to set
382 * @vers - rpc program version
383 *
384 * Clones the rpc client and sets up a new RPC program. This is mainly
385 * of use for enabling different RPC programs to share the same transport.
386 * The Sun NFSv2/v3 ACL protocol can do this.
387 */
388struct rpc_clnt *rpc_bind_new_program(struct rpc_clnt *old,
389 struct rpc_program *program,
390 int vers)
391{
392 struct rpc_clnt *clnt;
393 struct rpc_version *version;
394 int err;
395
396 BUG_ON(vers >= program->nrvers || !program->version[vers]);
397 version = program->version[vers];
398 clnt = rpc_clone_client(old);
399 if (IS_ERR(clnt))
400 goto out;
401 clnt->cl_procinfo = version->procs;
402 clnt->cl_maxproc = version->nrprocs;
403 clnt->cl_protname = program->name;
404 clnt->cl_prog = program->number;
405 clnt->cl_vers = version->number;
406 clnt->cl_stats = program->stats;
407 err = rpc_ping(clnt, RPC_TASK_SOFT|RPC_TASK_NOINTR);
408 if (err != 0) {
409 rpc_shutdown_client(clnt);
410 clnt = ERR_PTR(err);
411 }
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800412out:
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000413 return clnt;
414}
415
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416/*
417 * Default callback for async RPC calls
418 */
419static void
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100420rpc_default_callback(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{
422}
423
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100424static const struct rpc_call_ops rpc_default_ops = {
425 .rpc_call_done = rpc_default_callback,
426};
427
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428/*
Trond Myklebust14b218a2005-06-22 17:16:28 +0000429 * Export the signal mask handling for synchronous code that
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 * sleeps on RPC calls
431 */
Trond Myklebust2bd61572006-01-03 09:55:19 +0100432#define RPC_INTR_SIGNALS (sigmask(SIGHUP) | sigmask(SIGINT) | sigmask(SIGQUIT) | sigmask(SIGTERM))
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800433
Trond Myklebust14b218a2005-06-22 17:16:28 +0000434static void rpc_save_sigmask(sigset_t *oldset, int intr)
435{
Trond Myklebust2bd61572006-01-03 09:55:19 +0100436 unsigned long sigallow = sigmask(SIGKILL);
Trond Myklebust14b218a2005-06-22 17:16:28 +0000437 sigset_t sigmask;
438
439 /* Block all signals except those listed in sigallow */
440 if (intr)
441 sigallow |= RPC_INTR_SIGNALS;
442 siginitsetinv(&sigmask, sigallow);
443 sigprocmask(SIG_BLOCK, &sigmask, oldset);
444}
445
446static inline void rpc_task_sigmask(struct rpc_task *task, sigset_t *oldset)
447{
448 rpc_save_sigmask(oldset, !RPC_TASK_UNINTERRUPTIBLE(task));
449}
450
451static inline void rpc_restore_sigmask(sigset_t *oldset)
452{
453 sigprocmask(SIG_SETMASK, oldset, NULL);
454}
455
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456void rpc_clnt_sigmask(struct rpc_clnt *clnt, sigset_t *oldset)
457{
Trond Myklebust14b218a2005-06-22 17:16:28 +0000458 rpc_save_sigmask(oldset, clnt->cl_intr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459}
460
461void rpc_clnt_sigunmask(struct rpc_clnt *clnt, sigset_t *oldset)
462{
Trond Myklebust14b218a2005-06-22 17:16:28 +0000463 rpc_restore_sigmask(oldset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464}
465
466/*
467 * New rpc_call implementation
468 */
469int rpc_call_sync(struct rpc_clnt *clnt, struct rpc_message *msg, int flags)
470{
471 struct rpc_task *task;
472 sigset_t oldset;
473 int status;
474
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 BUG_ON(flags & RPC_TASK_ASYNC);
476
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100477 task = rpc_new_task(clnt, flags, &rpc_default_ops, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 if (task == NULL)
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -0500479 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
Trond Myklebust14b218a2005-06-22 17:16:28 +0000481 /* Mask signals on RPC calls _and_ GSS_AUTH upcalls */
482 rpc_task_sigmask(task, &oldset);
483
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 /* Set up the call info struct and execute the task */
Trond Myklebust2efef832007-02-03 13:38:41 -0800485 rpc_call_setup(task, msg, 0);
486 if (task->tk_status == 0) {
487 atomic_inc(&task->tk_count);
488 rpc_execute(task);
489 }
Trond Myklebuste60859a2006-01-03 09:55:10 +0100490 status = task->tk_status;
Trond Myklebustbde8f002007-01-24 11:54:53 -0800491 rpc_put_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -0500492 rpc_restore_sigmask(&oldset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 return status;
494}
495
496/*
497 * New rpc_call implementation
498 */
499int
500rpc_call_async(struct rpc_clnt *clnt, struct rpc_message *msg, int flags,
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100501 const struct rpc_call_ops *tk_ops, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502{
503 struct rpc_task *task;
504 sigset_t oldset;
505 int status;
506
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 flags |= RPC_TASK_ASYNC;
508
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 /* Create/initialize a new RPC task */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 status = -ENOMEM;
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100511 if (!(task = rpc_new_task(clnt, flags, tk_ops, data)))
Trond Myklebust7a1218a2006-03-20 18:11:10 -0500512 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
Trond Myklebust14b218a2005-06-22 17:16:28 +0000514 /* Mask signals on GSS_AUTH upcalls */
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800515 rpc_task_sigmask(task, &oldset);
Trond Myklebust14b218a2005-06-22 17:16:28 +0000516
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 rpc_call_setup(task, msg, 0);
518
519 /* Set up the call info struct and execute the task */
520 status = task->tk_status;
521 if (status == 0)
522 rpc_execute(task);
523 else
Trond Myklebustbde8f002007-01-24 11:54:53 -0800524 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800526 rpc_restore_sigmask(&oldset);
Trond Myklebust7a1218a2006-03-20 18:11:10 -0500527 return status;
528out_release:
Trond Myklebustbbd5a1f2006-10-18 16:01:05 -0400529 rpc_release_calldata(tk_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 return status;
531}
532
533
534void
535rpc_call_setup(struct rpc_task *task, struct rpc_message *msg, int flags)
536{
537 task->tk_msg = *msg;
538 task->tk_flags |= flags;
539 /* Bind the user cred */
540 if (task->tk_msg.rpc_cred != NULL)
541 rpcauth_holdcred(task);
542 else
543 rpcauth_bindcred(task);
544
545 if (task->tk_status == 0)
546 task->tk_action = call_start;
547 else
Trond Myklebustabbcf282006-01-03 09:55:03 +0100548 task->tk_action = rpc_exit_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549}
550
Chuck Levered394402006-08-22 20:06:17 -0400551/**
552 * rpc_peeraddr - extract remote peer address from clnt's xprt
553 * @clnt: RPC client structure
554 * @buf: target buffer
555 * @size: length of target buffer
556 *
557 * Returns the number of bytes that are actually in the stored address.
558 */
559size_t rpc_peeraddr(struct rpc_clnt *clnt, struct sockaddr *buf, size_t bufsize)
560{
561 size_t bytes;
562 struct rpc_xprt *xprt = clnt->cl_xprt;
563
564 bytes = sizeof(xprt->addr);
565 if (bytes > bufsize)
566 bytes = bufsize;
567 memcpy(buf, &clnt->cl_xprt->addr, bytes);
Chuck Leverc4efcb12006-08-22 20:06:19 -0400568 return xprt->addrlen;
Chuck Levered394402006-08-22 20:06:17 -0400569}
Chuck Leverb86acd52006-08-22 20:06:22 -0400570EXPORT_SYMBOL_GPL(rpc_peeraddr);
Chuck Levered394402006-08-22 20:06:17 -0400571
Chuck Leverf425eba2006-08-22 20:06:18 -0400572/**
573 * rpc_peeraddr2str - return remote peer address in printable format
574 * @clnt: RPC client structure
575 * @format: address format
576 *
577 */
578char *rpc_peeraddr2str(struct rpc_clnt *clnt, enum rpc_display_format_t format)
579{
580 struct rpc_xprt *xprt = clnt->cl_xprt;
Chuck Lever7559c7a2006-12-05 16:35:37 -0500581
582 if (xprt->address_strings[format] != NULL)
583 return xprt->address_strings[format];
584 else
585 return "unprintable";
Chuck Leverf425eba2006-08-22 20:06:18 -0400586}
Chuck Leverb86acd52006-08-22 20:06:22 -0400587EXPORT_SYMBOL_GPL(rpc_peeraddr2str);
Chuck Leverf425eba2006-08-22 20:06:18 -0400588
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589void
590rpc_setbufsize(struct rpc_clnt *clnt, unsigned int sndsize, unsigned int rcvsize)
591{
592 struct rpc_xprt *xprt = clnt->cl_xprt;
Chuck Lever470056c2005-08-25 16:25:56 -0700593 if (xprt->ops->set_buffer_size)
594 xprt->ops->set_buffer_size(xprt, sndsize, rcvsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595}
596
597/*
598 * Return size of largest payload RPC client can support, in bytes
599 *
600 * For stream transports, this is one RPC record fragment (see RFC
601 * 1831), as we don't support multi-record requests yet. For datagram
602 * transports, this is the size of an IP packet minus the IP, UDP, and
603 * RPC header sizes.
604 */
605size_t rpc_max_payload(struct rpc_clnt *clnt)
606{
607 return clnt->cl_xprt->max_payload;
608}
Chuck Leverb86acd52006-08-22 20:06:22 -0400609EXPORT_SYMBOL_GPL(rpc_max_payload);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
Chuck Lever35f5a422006-01-03 09:55:50 +0100611/**
612 * rpc_force_rebind - force transport to check that remote port is unchanged
613 * @clnt: client to rebind
614 *
615 */
616void rpc_force_rebind(struct rpc_clnt *clnt)
617{
618 if (clnt->cl_autobind)
Chuck Leverec739ef2006-08-22 20:06:15 -0400619 xprt_clear_bound(clnt->cl_xprt);
Chuck Lever35f5a422006-01-03 09:55:50 +0100620}
Chuck Leverb86acd52006-08-22 20:06:22 -0400621EXPORT_SYMBOL_GPL(rpc_force_rebind);
Chuck Lever35f5a422006-01-03 09:55:50 +0100622
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623/*
624 * Restart an (async) RPC call. Usually called from within the
625 * exit handler.
626 */
627void
628rpc_restart_call(struct rpc_task *task)
629{
630 if (RPC_ASSASSINATED(task))
631 return;
632
633 task->tk_action = call_start;
634}
635
636/*
637 * 0. Initial state
638 *
639 * Other FSM states can be visited zero or more times, but
640 * this state is visited exactly once for each RPC.
641 */
642static void
643call_start(struct rpc_task *task)
644{
645 struct rpc_clnt *clnt = task->tk_client;
646
Chuck Lever46121cf2007-01-31 12:14:08 -0500647 dprintk("RPC: %5u call_start %s%d proc %d (%s)\n", task->tk_pid,
648 clnt->cl_protname, clnt->cl_vers,
649 task->tk_msg.rpc_proc->p_proc,
650 (RPC_IS_ASYNC(task) ? "async" : "sync"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
652 /* Increment call count */
653 task->tk_msg.rpc_proc->p_count++;
654 clnt->cl_stats->rpccnt++;
655 task->tk_action = call_reserve;
656}
657
658/*
659 * 1. Reserve an RPC call slot
660 */
661static void
662call_reserve(struct rpc_task *task)
663{
Chuck Lever46121cf2007-01-31 12:14:08 -0500664 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
666 if (!rpcauth_uptodatecred(task)) {
667 task->tk_action = call_refresh;
668 return;
669 }
670
671 task->tk_status = 0;
672 task->tk_action = call_reserveresult;
673 xprt_reserve(task);
674}
675
676/*
677 * 1b. Grok the result of xprt_reserve()
678 */
679static void
680call_reserveresult(struct rpc_task *task)
681{
682 int status = task->tk_status;
683
Chuck Lever46121cf2007-01-31 12:14:08 -0500684 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
686 /*
687 * After a call to xprt_reserve(), we must have either
688 * a request slot or else an error status.
689 */
690 task->tk_status = 0;
691 if (status >= 0) {
692 if (task->tk_rqstp) {
693 task->tk_action = call_allocate;
694 return;
695 }
696
697 printk(KERN_ERR "%s: status=%d, but no request slot, exiting\n",
698 __FUNCTION__, status);
699 rpc_exit(task, -EIO);
700 return;
701 }
702
703 /*
704 * Even though there was an error, we may have acquired
705 * a request slot somehow. Make sure not to leak it.
706 */
707 if (task->tk_rqstp) {
708 printk(KERN_ERR "%s: status=%d, request allocated anyway\n",
709 __FUNCTION__, status);
710 xprt_release(task);
711 }
712
713 switch (status) {
714 case -EAGAIN: /* woken up; retry */
715 task->tk_action = call_reserve;
716 return;
717 case -EIO: /* probably a shutdown */
718 break;
719 default:
720 printk(KERN_ERR "%s: unrecognized error %d, exiting\n",
721 __FUNCTION__, status);
722 break;
723 }
724 rpc_exit(task, status);
725}
726
727/*
728 * 2. Allocate the buffer. For details, see sched.c:rpc_malloc.
Chuck Lever02107142006-01-03 09:55:49 +0100729 * (Note: buffer memory is freed in xprt_release).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 */
731static void
732call_allocate(struct rpc_task *task)
733{
Chuck Lever2bea90d2007-03-29 16:47:53 -0400734 unsigned int slack = task->tk_auth->au_cslack;
Chuck Lever02107142006-01-03 09:55:49 +0100735 struct rpc_rqst *req = task->tk_rqstp;
736 struct rpc_xprt *xprt = task->tk_xprt;
Chuck Lever2bea90d2007-03-29 16:47:53 -0400737 struct rpc_procinfo *proc = task->tk_msg.rpc_proc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
Chuck Lever46121cf2007-01-31 12:14:08 -0500739 dprint_status(task);
740
Chuck Lever2bea90d2007-03-29 16:47:53 -0400741 task->tk_status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 task->tk_action = call_bind;
Chuck Lever2bea90d2007-03-29 16:47:53 -0400743
Chuck Lever02107142006-01-03 09:55:49 +0100744 if (req->rq_buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 return;
746
Chuck Lever2bea90d2007-03-29 16:47:53 -0400747 if (proc->p_proc != 0) {
748 BUG_ON(proc->p_arglen == 0);
749 if (proc->p_decode != NULL)
750 BUG_ON(proc->p_replen == 0);
751 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
Chuck Lever2bea90d2007-03-29 16:47:53 -0400753 /*
754 * Calculate the size (in quads) of the RPC call
755 * and reply headers, and convert both values
756 * to byte sizes.
757 */
758 req->rq_callsize = RPC_CALLHDRSIZE + (slack << 1) + proc->p_arglen;
759 req->rq_callsize <<= 2;
760 req->rq_rcvsize = RPC_REPHDRSIZE + slack + proc->p_replen;
761 req->rq_rcvsize <<= 2;
762
Chuck Leverc5a4dd82007-03-29 16:47:58 -0400763 req->rq_buffer = xprt->ops->buf_alloc(task,
764 req->rq_callsize + req->rq_rcvsize);
Chuck Lever2bea90d2007-03-29 16:47:53 -0400765 if (req->rq_buffer != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 return;
Chuck Lever46121cf2007-01-31 12:14:08 -0500767
768 dprintk("RPC: %5u rpc_buffer allocation failed\n", task->tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
Trond Myklebust14b218a2005-06-22 17:16:28 +0000770 if (RPC_IS_ASYNC(task) || !signalled()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 xprt_release(task);
772 task->tk_action = call_reserve;
773 rpc_delay(task, HZ>>4);
774 return;
775 }
776
777 rpc_exit(task, -ERESTARTSYS);
778}
779
Trond Myklebust940e3312005-11-09 21:45:24 -0500780static inline int
781rpc_task_need_encode(struct rpc_task *task)
782{
783 return task->tk_rqstp->rq_snd_buf.len == 0;
784}
785
786static inline void
787rpc_task_force_reencode(struct rpc_task *task)
788{
789 task->tk_rqstp->rq_snd_buf.len = 0;
790}
791
Chuck Lever2bea90d2007-03-29 16:47:53 -0400792static inline void
793rpc_xdr_buf_init(struct xdr_buf *buf, void *start, size_t len)
794{
795 buf->head[0].iov_base = start;
796 buf->head[0].iov_len = len;
797 buf->tail[0].iov_len = 0;
798 buf->page_len = 0;
799 buf->len = 0;
800 buf->buflen = len;
801}
802
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803/*
804 * 3. Encode arguments of an RPC call
805 */
806static void
807call_encode(struct rpc_task *task)
808{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 struct rpc_rqst *req = task->tk_rqstp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 kxdrproc_t encode;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700811 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
Chuck Lever46121cf2007-01-31 12:14:08 -0500813 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
Chuck Lever2bea90d2007-03-29 16:47:53 -0400815 rpc_xdr_buf_init(&req->rq_snd_buf,
816 req->rq_buffer,
817 req->rq_callsize);
818 rpc_xdr_buf_init(&req->rq_rcv_buf,
819 (char *)req->rq_buffer + req->rq_callsize,
820 req->rq_rcvsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
822 /* Encode header and provided arguments */
823 encode = task->tk_msg.rpc_proc->p_encode;
824 if (!(p = call_header(task))) {
825 printk(KERN_INFO "RPC: call_header failed, exit EIO\n");
826 rpc_exit(task, -EIO);
827 return;
828 }
J. Bruce Fieldsf3680312005-10-13 16:54:48 -0400829 if (encode == NULL)
830 return;
831
Trond Myklebust6d5fcb52006-10-18 16:01:06 -0400832 lock_kernel();
J. Bruce Fieldsf3680312005-10-13 16:54:48 -0400833 task->tk_status = rpcauth_wrap_req(task, encode, req, p,
834 task->tk_msg.rpc_argp);
Trond Myklebust6d5fcb52006-10-18 16:01:06 -0400835 unlock_kernel();
J. Bruce Fieldsf3680312005-10-13 16:54:48 -0400836 if (task->tk_status == -ENOMEM) {
837 /* XXX: Is this sane? */
838 rpc_delay(task, 3*HZ);
839 task->tk_status = -EAGAIN;
840 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841}
842
843/*
844 * 4. Get the server port number if not yet set
845 */
846static void
847call_bind(struct rpc_task *task)
848{
Chuck Leverec739ef2006-08-22 20:06:15 -0400849 struct rpc_xprt *xprt = task->tk_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
Chuck Lever46121cf2007-01-31 12:14:08 -0500851 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
Chuck Leverda351872005-08-11 16:25:11 -0400853 task->tk_action = call_connect;
Chuck Leverec739ef2006-08-22 20:06:15 -0400854 if (!xprt_bound(xprt)) {
Chuck Leverda351872005-08-11 16:25:11 -0400855 task->tk_action = call_bind_status;
Chuck Leverec739ef2006-08-22 20:06:15 -0400856 task->tk_timeout = xprt->bind_timeout;
Chuck Leverbbf7c1d2006-08-22 20:06:16 -0400857 xprt->ops->rpcbind(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 }
859}
860
861/*
Chuck Leverda351872005-08-11 16:25:11 -0400862 * 4a. Sort out bind result
863 */
864static void
865call_bind_status(struct rpc_task *task)
866{
867 int status = -EACCES;
868
869 if (task->tk_status >= 0) {
Chuck Lever46121cf2007-01-31 12:14:08 -0500870 dprint_status(task);
Chuck Leverda351872005-08-11 16:25:11 -0400871 task->tk_status = 0;
872 task->tk_action = call_connect;
873 return;
874 }
875
876 switch (task->tk_status) {
877 case -EACCES:
Chuck Lever46121cf2007-01-31 12:14:08 -0500878 dprintk("RPC: %5u remote rpcbind: RPC program/version "
879 "unavailable\n", task->tk_pid);
Chuck Leverea635a52005-10-06 23:12:58 -0400880 rpc_delay(task, 3*HZ);
Trond Myklebustda458282006-08-31 15:44:52 -0400881 goto retry_timeout;
Chuck Leverda351872005-08-11 16:25:11 -0400882 case -ETIMEDOUT:
Chuck Lever46121cf2007-01-31 12:14:08 -0500883 dprintk("RPC: %5u rpcbind request timed out\n",
Chuck Leverda351872005-08-11 16:25:11 -0400884 task->tk_pid);
Trond Myklebustda458282006-08-31 15:44:52 -0400885 goto retry_timeout;
Chuck Leverda351872005-08-11 16:25:11 -0400886 case -EPFNOSUPPORT:
Chuck Lever46121cf2007-01-31 12:14:08 -0500887 dprintk("RPC: %5u remote rpcbind service unavailable\n",
Chuck Leverda351872005-08-11 16:25:11 -0400888 task->tk_pid);
889 break;
890 case -EPROTONOSUPPORT:
Chuck Lever00a6e7b2007-03-29 16:48:33 -0400891 dprintk("RPC: %5u remote rpcbind version unavailable, retrying\n",
Chuck Leverda351872005-08-11 16:25:11 -0400892 task->tk_pid);
Chuck Lever00a6e7b2007-03-29 16:48:33 -0400893 task->tk_status = 0;
894 task->tk_action = call_bind;
895 return;
Chuck Leverda351872005-08-11 16:25:11 -0400896 default:
Chuck Lever46121cf2007-01-31 12:14:08 -0500897 dprintk("RPC: %5u unrecognized rpcbind error (%d)\n",
Chuck Leverda351872005-08-11 16:25:11 -0400898 task->tk_pid, -task->tk_status);
899 status = -EIO;
Chuck Leverda351872005-08-11 16:25:11 -0400900 }
901
902 rpc_exit(task, status);
903 return;
904
Trond Myklebustda458282006-08-31 15:44:52 -0400905retry_timeout:
906 task->tk_action = call_timeout;
Chuck Leverda351872005-08-11 16:25:11 -0400907}
908
909/*
910 * 4b. Connect to the RPC server
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 */
912static void
913call_connect(struct rpc_task *task)
914{
Chuck Leverda351872005-08-11 16:25:11 -0400915 struct rpc_xprt *xprt = task->tk_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
Chuck Lever46121cf2007-01-31 12:14:08 -0500917 dprintk("RPC: %5u call_connect xprt %p %s connected\n",
Chuck Leverda351872005-08-11 16:25:11 -0400918 task->tk_pid, xprt,
919 (xprt_connected(xprt) ? "is" : "is not"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
Chuck Leverda351872005-08-11 16:25:11 -0400921 task->tk_action = call_transmit;
922 if (!xprt_connected(xprt)) {
923 task->tk_action = call_connect_status;
924 if (task->tk_status < 0)
925 return;
926 xprt_connect(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928}
929
930/*
Chuck Leverda351872005-08-11 16:25:11 -0400931 * 4c. Sort out connect result
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 */
933static void
934call_connect_status(struct rpc_task *task)
935{
936 struct rpc_clnt *clnt = task->tk_client;
937 int status = task->tk_status;
938
Chuck Lever46121cf2007-01-31 12:14:08 -0500939 dprint_status(task);
Chuck Leverda351872005-08-11 16:25:11 -0400940
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 task->tk_status = 0;
942 if (status >= 0) {
943 clnt->cl_stats->netreconn++;
944 task->tk_action = call_transmit;
945 return;
946 }
947
Chuck Leverda351872005-08-11 16:25:11 -0400948 /* Something failed: remote service port may have changed */
Chuck Lever35f5a422006-01-03 09:55:50 +0100949 rpc_force_rebind(clnt);
Chuck Leverda351872005-08-11 16:25:11 -0400950
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 switch (status) {
952 case -ENOTCONN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 case -EAGAIN:
Chuck Leverda351872005-08-11 16:25:11 -0400954 task->tk_action = call_bind;
Trond Myklebustda458282006-08-31 15:44:52 -0400955 if (!RPC_IS_SOFT(task))
956 return;
957 /* if soft mounted, test if we've timed out */
958 case -ETIMEDOUT:
959 task->tk_action = call_timeout;
960 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 }
Trond Myklebustda458282006-08-31 15:44:52 -0400962 rpc_exit(task, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963}
964
965/*
966 * 5. Transmit the RPC request, and wait for reply
967 */
968static void
969call_transmit(struct rpc_task *task)
970{
Chuck Lever46121cf2007-01-31 12:14:08 -0500971 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
973 task->tk_action = call_status;
974 if (task->tk_status < 0)
975 return;
976 task->tk_status = xprt_prepare_transmit(task);
977 if (task->tk_status != 0)
978 return;
Trond Myklebuste0ab53d2006-07-27 17:22:50 -0400979 task->tk_action = call_transmit_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 /* Encode here so that rpcsec_gss can use correct sequence number. */
Trond Myklebust940e3312005-11-09 21:45:24 -0500981 if (rpc_task_need_encode(task)) {
Trond Myklebuste0ab53d2006-07-27 17:22:50 -0400982 BUG_ON(task->tk_rqstp->rq_bytes_sent != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 call_encode(task);
Trond Myklebust5e5ce5b2005-10-18 14:20:11 -0700984 /* Did the encode result in an error condition? */
985 if (task->tk_status != 0)
Trond Myklebuste0ab53d2006-07-27 17:22:50 -0400986 return;
Trond Myklebust5e5ce5b2005-10-18 14:20:11 -0700987 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 xprt_transmit(task);
989 if (task->tk_status < 0)
990 return;
Trond Myklebuste0ab53d2006-07-27 17:22:50 -0400991 /*
992 * On success, ensure that we call xprt_end_transmit() before sleeping
993 * in order to allow access to the socket to other RPC requests.
994 */
995 call_transmit_status(task);
996 if (task->tk_msg.rpc_proc->p_decode != NULL)
997 return;
998 task->tk_action = rpc_exit_task;
999 rpc_wake_up_task(task);
1000}
1001
1002/*
1003 * 5a. Handle cleanup after a transmission
1004 */
1005static void
1006call_transmit_status(struct rpc_task *task)
1007{
1008 task->tk_action = call_status;
1009 /*
1010 * Special case: if we've been waiting on the socket's write_space()
1011 * callback, then don't call xprt_end_transmit().
1012 */
1013 if (task->tk_status == -EAGAIN)
1014 return;
1015 xprt_end_transmit(task);
Trond Myklebust940e3312005-11-09 21:45:24 -05001016 rpc_task_force_reencode(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017}
1018
1019/*
1020 * 6. Sort out the RPC call status
1021 */
1022static void
1023call_status(struct rpc_task *task)
1024{
1025 struct rpc_clnt *clnt = task->tk_client;
1026 struct rpc_rqst *req = task->tk_rqstp;
1027 int status;
1028
1029 if (req->rq_received > 0 && !req->rq_bytes_sent)
1030 task->tk_status = req->rq_received;
1031
Chuck Lever46121cf2007-01-31 12:14:08 -05001032 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
1034 status = task->tk_status;
1035 if (status >= 0) {
1036 task->tk_action = call_decode;
1037 return;
1038 }
1039
1040 task->tk_status = 0;
1041 switch(status) {
Trond Myklebust76303992006-08-30 14:32:49 -04001042 case -EHOSTDOWN:
1043 case -EHOSTUNREACH:
1044 case -ENETUNREACH:
1045 /*
1046 * Delay any retries for 3 seconds, then handle as if it
1047 * were a timeout.
1048 */
1049 rpc_delay(task, 3*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 case -ETIMEDOUT:
1051 task->tk_action = call_timeout;
Trond Myklebust241c39b2007-04-20 16:12:55 -04001052 if (task->tk_client->cl_discrtry)
1053 xprt_disconnect(task->tk_xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 break;
1055 case -ECONNREFUSED:
1056 case -ENOTCONN:
Chuck Lever35f5a422006-01-03 09:55:50 +01001057 rpc_force_rebind(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 task->tk_action = call_bind;
1059 break;
1060 case -EAGAIN:
1061 task->tk_action = call_transmit;
1062 break;
1063 case -EIO:
1064 /* shutdown or soft timeout */
1065 rpc_exit(task, status);
1066 break;
1067 default:
Chuck Leverf518e35a2006-01-03 09:55:52 +01001068 printk("%s: RPC call returned error %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 clnt->cl_protname, -status);
1070 rpc_exit(task, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 }
1072}
1073
1074/*
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04001075 * 6a. Handle RPC timeout
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 * We do not release the request slot, so we keep using the
1077 * same XID for all retransmits.
1078 */
1079static void
1080call_timeout(struct rpc_task *task)
1081{
1082 struct rpc_clnt *clnt = task->tk_client;
1083
1084 if (xprt_adjust_timeout(task->tk_rqstp) == 0) {
Chuck Lever46121cf2007-01-31 12:14:08 -05001085 dprintk("RPC: %5u call_timeout (minor)\n", task->tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 goto retry;
1087 }
1088
Chuck Lever46121cf2007-01-31 12:14:08 -05001089 dprintk("RPC: %5u call_timeout (major)\n", task->tk_pid);
Chuck Leveref759a22006-03-20 13:44:17 -05001090 task->tk_timeouts++;
1091
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 if (RPC_IS_SOFT(task)) {
Chuck Leverf518e35a2006-01-03 09:55:52 +01001093 printk(KERN_NOTICE "%s: server %s not responding, timed out\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 clnt->cl_protname, clnt->cl_server);
1095 rpc_exit(task, -EIO);
1096 return;
1097 }
1098
Chuck Leverf518e35a2006-01-03 09:55:52 +01001099 if (!(task->tk_flags & RPC_CALL_MAJORSEEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 task->tk_flags |= RPC_CALL_MAJORSEEN;
1101 printk(KERN_NOTICE "%s: server %s not responding, still trying\n",
1102 clnt->cl_protname, clnt->cl_server);
1103 }
Chuck Lever35f5a422006-01-03 09:55:50 +01001104 rpc_force_rebind(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
1106retry:
1107 clnt->cl_stats->rpcretrans++;
1108 task->tk_action = call_bind;
1109 task->tk_status = 0;
1110}
1111
1112/*
1113 * 7. Decode the RPC reply
1114 */
1115static void
1116call_decode(struct rpc_task *task)
1117{
1118 struct rpc_clnt *clnt = task->tk_client;
1119 struct rpc_rqst *req = task->tk_rqstp;
1120 kxdrproc_t decode = task->tk_msg.rpc_proc->p_decode;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001121 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
Chuck Lever46121cf2007-01-31 12:14:08 -05001123 dprintk("RPC: %5u call_decode (status %d)\n",
1124 task->tk_pid, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
Chuck Leverf518e35a2006-01-03 09:55:52 +01001126 if (task->tk_flags & RPC_CALL_MAJORSEEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 printk(KERN_NOTICE "%s: server %s OK\n",
1128 clnt->cl_protname, clnt->cl_server);
1129 task->tk_flags &= ~RPC_CALL_MAJORSEEN;
1130 }
1131
1132 if (task->tk_status < 12) {
1133 if (!RPC_IS_SOFT(task)) {
1134 task->tk_action = call_bind;
1135 clnt->cl_stats->rpcretrans++;
1136 goto out_retry;
1137 }
Chuck Lever46121cf2007-01-31 12:14:08 -05001138 dprintk("RPC: %s: too small RPC reply size (%d bytes)\n",
1139 clnt->cl_protname, task->tk_status);
Trond Myklebustda458282006-08-31 15:44:52 -04001140 task->tk_action = call_timeout;
1141 goto out_retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 }
1143
Trond Myklebust43ac3f22006-03-20 13:44:51 -05001144 /*
1145 * Ensure that we see all writes made by xprt_complete_rqst()
1146 * before it changed req->rq_received.
1147 */
1148 smp_rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 req->rq_rcv_buf.len = req->rq_private_buf.len;
1150
1151 /* Check that the softirq receive buffer is valid */
1152 WARN_ON(memcmp(&req->rq_rcv_buf, &req->rq_private_buf,
1153 sizeof(req->rq_rcv_buf)) != 0);
1154
1155 /* Verify the RPC header */
Trond Myklebustabbcf282006-01-03 09:55:03 +01001156 p = call_verify(task);
1157 if (IS_ERR(p)) {
1158 if (p == ERR_PTR(-EAGAIN))
1159 goto out_retry;
1160 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 }
1162
Trond Myklebustabbcf282006-01-03 09:55:03 +01001163 task->tk_action = rpc_exit_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
Trond Myklebust6d5fcb52006-10-18 16:01:06 -04001165 if (decode) {
1166 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 task->tk_status = rpcauth_unwrap_resp(task, decode, req, p,
1168 task->tk_msg.rpc_resp);
Trond Myklebust6d5fcb52006-10-18 16:01:06 -04001169 unlock_kernel();
1170 }
Chuck Lever46121cf2007-01-31 12:14:08 -05001171 dprintk("RPC: %5u call_decode result %d\n", task->tk_pid,
1172 task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 return;
1174out_retry:
1175 req->rq_received = req->rq_private_buf.len = 0;
1176 task->tk_status = 0;
Trond Myklebust241c39b2007-04-20 16:12:55 -04001177 if (task->tk_client->cl_discrtry)
1178 xprt_disconnect(task->tk_xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179}
1180
1181/*
1182 * 8. Refresh the credentials if rejected by the server
1183 */
1184static void
1185call_refresh(struct rpc_task *task)
1186{
Chuck Lever46121cf2007-01-31 12:14:08 -05001187 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
1189 xprt_release(task); /* Must do to obtain new XID */
1190 task->tk_action = call_refreshresult;
1191 task->tk_status = 0;
1192 task->tk_client->cl_stats->rpcauthrefresh++;
1193 rpcauth_refreshcred(task);
1194}
1195
1196/*
1197 * 8a. Process the results of a credential refresh
1198 */
1199static void
1200call_refreshresult(struct rpc_task *task)
1201{
1202 int status = task->tk_status;
Chuck Lever46121cf2007-01-31 12:14:08 -05001203
1204 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
1206 task->tk_status = 0;
1207 task->tk_action = call_reserve;
1208 if (status >= 0 && rpcauth_uptodatecred(task))
1209 return;
1210 if (status == -EACCES) {
1211 rpc_exit(task, -EACCES);
1212 return;
1213 }
1214 task->tk_action = call_refresh;
1215 if (status != -ETIMEDOUT)
1216 rpc_delay(task, 3*HZ);
1217 return;
1218}
1219
1220/*
1221 * Call header serialization
1222 */
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001223static __be32 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224call_header(struct rpc_task *task)
1225{
1226 struct rpc_clnt *clnt = task->tk_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 struct rpc_rqst *req = task->tk_rqstp;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001228 __be32 *p = req->rq_svec[0].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
1230 /* FIXME: check buffer size? */
Chuck Lever808012f2005-08-25 16:25:49 -07001231
1232 p = xprt_skip_transport_header(task->tk_xprt, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 *p++ = req->rq_xid; /* XID */
1234 *p++ = htonl(RPC_CALL); /* CALL */
1235 *p++ = htonl(RPC_VERSION); /* RPC version */
1236 *p++ = htonl(clnt->cl_prog); /* program number */
1237 *p++ = htonl(clnt->cl_vers); /* program version */
1238 *p++ = htonl(task->tk_msg.rpc_proc->p_proc); /* procedure */
Trond Myklebust334ccfd2005-06-22 17:16:19 +00001239 p = rpcauth_marshcred(task, p);
1240 req->rq_slen = xdr_adjust_iovec(&req->rq_svec[0], p);
1241 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242}
1243
1244/*
1245 * Reply header verification
1246 */
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001247static __be32 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248call_verify(struct rpc_task *task)
1249{
1250 struct kvec *iov = &task->tk_rqstp->rq_rcv_buf.head[0];
1251 int len = task->tk_rqstp->rq_rcv_buf.len >> 2;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001252 __be32 *p = iov->iov_base;
1253 u32 n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 int error = -EACCES;
1255
David Howellse8896492006-08-24 15:44:19 -04001256 if ((task->tk_rqstp->rq_rcv_buf.len & 3) != 0) {
1257 /* RFC-1014 says that the representation of XDR data must be a
1258 * multiple of four bytes
1259 * - if it isn't pointer subtraction in the NFS client may give
1260 * undefined results
1261 */
1262 printk(KERN_WARNING
1263 "call_verify: XDR representation not a multiple of"
1264 " 4 bytes: 0x%x\n", task->tk_rqstp->rq_rcv_buf.len);
1265 goto out_eio;
1266 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 if ((len -= 3) < 0)
1268 goto out_overflow;
1269 p += 1; /* skip XID */
1270
1271 if ((n = ntohl(*p++)) != RPC_REPLY) {
1272 printk(KERN_WARNING "call_verify: not an RPC reply: %x\n", n);
Trond Myklebustabbcf282006-01-03 09:55:03 +01001273 goto out_garbage;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 }
1275 if ((n = ntohl(*p++)) != RPC_MSG_ACCEPTED) {
1276 if (--len < 0)
1277 goto out_overflow;
1278 switch ((n = ntohl(*p++))) {
1279 case RPC_AUTH_ERROR:
1280 break;
1281 case RPC_MISMATCH:
Chuck Lever46121cf2007-01-31 12:14:08 -05001282 dprintk("RPC: %5u %s: RPC call version "
1283 "mismatch!\n",
1284 task->tk_pid, __FUNCTION__);
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00001285 error = -EPROTONOSUPPORT;
1286 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 default:
Chuck Lever46121cf2007-01-31 12:14:08 -05001288 dprintk("RPC: %5u %s: RPC call rejected, "
1289 "unknown error: %x\n",
1290 task->tk_pid, __FUNCTION__, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 goto out_eio;
1292 }
1293 if (--len < 0)
1294 goto out_overflow;
1295 switch ((n = ntohl(*p++))) {
1296 case RPC_AUTH_REJECTEDCRED:
1297 case RPC_AUTH_REJECTEDVERF:
1298 case RPCSEC_GSS_CREDPROBLEM:
1299 case RPCSEC_GSS_CTXPROBLEM:
1300 if (!task->tk_cred_retry)
1301 break;
1302 task->tk_cred_retry--;
Chuck Lever46121cf2007-01-31 12:14:08 -05001303 dprintk("RPC: %5u %s: retry stale creds\n",
1304 task->tk_pid, __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 rpcauth_invalcred(task);
1306 task->tk_action = call_refresh;
Trond Myklebustabbcf282006-01-03 09:55:03 +01001307 goto out_retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 case RPC_AUTH_BADCRED:
1309 case RPC_AUTH_BADVERF:
1310 /* possibly garbled cred/verf? */
1311 if (!task->tk_garb_retry)
1312 break;
1313 task->tk_garb_retry--;
Chuck Lever46121cf2007-01-31 12:14:08 -05001314 dprintk("RPC: %5u %s: retry garbled creds\n",
1315 task->tk_pid, __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 task->tk_action = call_bind;
Trond Myklebustabbcf282006-01-03 09:55:03 +01001317 goto out_retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 case RPC_AUTH_TOOWEAK:
Levent Serinol1356b8c2006-03-20 13:44:11 -05001319 printk(KERN_NOTICE "call_verify: server %s requires stronger "
1320 "authentication.\n", task->tk_client->cl_server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 break;
1322 default:
1323 printk(KERN_WARNING "call_verify: unknown auth error: %x\n", n);
1324 error = -EIO;
1325 }
Chuck Lever46121cf2007-01-31 12:14:08 -05001326 dprintk("RPC: %5u %s: call rejected %d\n",
1327 task->tk_pid, __FUNCTION__, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 goto out_err;
1329 }
1330 if (!(p = rpcauth_checkverf(task, p))) {
1331 printk(KERN_WARNING "call_verify: auth check failed\n");
Trond Myklebustabbcf282006-01-03 09:55:03 +01001332 goto out_garbage; /* bad verifier, retry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 }
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001334 len = p - (__be32 *)iov->iov_base - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 if (len < 0)
1336 goto out_overflow;
1337 switch ((n = ntohl(*p++))) {
1338 case RPC_SUCCESS:
1339 return p;
1340 case RPC_PROG_UNAVAIL:
Chuck Lever46121cf2007-01-31 12:14:08 -05001341 dprintk("RPC: %5u %s: program %u is unsupported by server %s\n",
1342 task->tk_pid, __FUNCTION__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 (unsigned int)task->tk_client->cl_prog,
1344 task->tk_client->cl_server);
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00001345 error = -EPFNOSUPPORT;
1346 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 case RPC_PROG_MISMATCH:
Chuck Lever46121cf2007-01-31 12:14:08 -05001348 dprintk("RPC: %5u %s: program %u, version %u unsupported by "
1349 "server %s\n", task->tk_pid, __FUNCTION__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 (unsigned int)task->tk_client->cl_prog,
1351 (unsigned int)task->tk_client->cl_vers,
1352 task->tk_client->cl_server);
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00001353 error = -EPROTONOSUPPORT;
1354 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 case RPC_PROC_UNAVAIL:
Chuck Lever46121cf2007-01-31 12:14:08 -05001356 dprintk("RPC: %5u %s: proc %p unsupported by program %u, "
1357 "version %u on server %s\n",
1358 task->tk_pid, __FUNCTION__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 task->tk_msg.rpc_proc,
1360 task->tk_client->cl_prog,
1361 task->tk_client->cl_vers,
1362 task->tk_client->cl_server);
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00001363 error = -EOPNOTSUPP;
1364 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 case RPC_GARBAGE_ARGS:
Chuck Lever46121cf2007-01-31 12:14:08 -05001366 dprintk("RPC: %5u %s: server saw garbage\n",
1367 task->tk_pid, __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 break; /* retry */
1369 default:
1370 printk(KERN_WARNING "call_verify: server accept status: %x\n", n);
1371 /* Also retry */
1372 }
1373
Trond Myklebustabbcf282006-01-03 09:55:03 +01001374out_garbage:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 task->tk_client->cl_stats->rpcgarbage++;
1376 if (task->tk_garb_retry) {
1377 task->tk_garb_retry--;
Chuck Lever46121cf2007-01-31 12:14:08 -05001378 dprintk("RPC: %5u %s: retrying\n",
1379 task->tk_pid, __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 task->tk_action = call_bind;
Trond Myklebustabbcf282006-01-03 09:55:03 +01001381out_retry:
1382 return ERR_PTR(-EAGAIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 }
1384 printk(KERN_WARNING "RPC %s: retry failed, exit EIO\n", __FUNCTION__);
1385out_eio:
1386 error = -EIO;
1387out_err:
1388 rpc_exit(task, error);
Trond Myklebustabbcf282006-01-03 09:55:03 +01001389 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390out_overflow:
1391 printk(KERN_WARNING "RPC %s: server reply was truncated.\n", __FUNCTION__);
Trond Myklebustabbcf282006-01-03 09:55:03 +01001392 goto out_garbage;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393}
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00001394
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001395static int rpcproc_encode_null(void *rqstp, __be32 *data, void *obj)
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00001396{
1397 return 0;
1398}
1399
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001400static int rpcproc_decode_null(void *rqstp, __be32 *data, void *obj)
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00001401{
1402 return 0;
1403}
1404
1405static struct rpc_procinfo rpcproc_null = {
1406 .p_encode = rpcproc_encode_null,
1407 .p_decode = rpcproc_decode_null,
1408};
1409
1410int rpc_ping(struct rpc_clnt *clnt, int flags)
1411{
1412 struct rpc_message msg = {
1413 .rpc_proc = &rpcproc_null,
1414 };
1415 int err;
1416 msg.rpc_cred = authnull_ops.lookup_cred(NULL, NULL, 0);
1417 err = rpc_call_sync(clnt, &msg, flags);
1418 put_rpccred(msg.rpc_cred);
1419 return err;
1420}