blob: fe838e996ee330f6e52584be261345ed8ad12d9a [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
Trond Myklebust4ada5392007-06-14 17:26:17 -0400114 err = rpciod_up();
115 if (err)
116 goto out_no_rpciod;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 err = -EINVAL;
118 if (!xprt)
Adrian Bunk712917d2006-03-13 21:20:47 -0800119 goto out_no_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 if (vers >= program->nrvers || !(version = program->version[vers]))
121 goto out_err;
122
123 err = -ENOMEM;
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700124 clnt = kzalloc(sizeof(*clnt), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 if (!clnt)
126 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 clnt->cl_parent = clnt;
128
129 clnt->cl_server = clnt->cl_inline_name;
130 len = strlen(servname) + 1;
131 if (len > sizeof(clnt->cl_inline_name)) {
132 char *buf = kmalloc(len, GFP_KERNEL);
133 if (buf != 0)
134 clnt->cl_server = buf;
135 else
136 len = sizeof(clnt->cl_inline_name);
137 }
138 strlcpy(clnt->cl_server, servname, len);
139
140 clnt->cl_xprt = xprt;
141 clnt->cl_procinfo = version->procs;
142 clnt->cl_maxproc = version->nrprocs;
143 clnt->cl_protname = program->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 clnt->cl_prog = program->number;
145 clnt->cl_vers = version->number;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 clnt->cl_stats = program->stats;
Chuck Lever11c556b2006-03-20 13:44:22 -0500147 clnt->cl_metrics = rpc_alloc_iostats(clnt);
Trond Myklebust23bf85b2006-11-21 10:40:23 -0500148 err = -ENOMEM;
149 if (clnt->cl_metrics == NULL)
150 goto out_no_stats;
Trond Myklebust3e32a5d2006-11-16 11:37:27 -0500151 clnt->cl_program = program;
Trond Myklebust6529eba2007-06-14 16:40:14 -0400152 INIT_LIST_HEAD(&clnt->cl_tasks);
Trond Myklebust4bef61f2007-06-16 14:17:01 -0400153 spin_lock_init(&clnt->cl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Chuck Leverec739ef2006-08-22 20:06:15 -0400155 if (!xprt_bound(clnt->cl_xprt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 clnt->cl_autobind = 1;
157
158 clnt->cl_rtt = &clnt->cl_rtt_default;
159 rpc_init_rtt(&clnt->cl_rtt_default, xprt->timeout.to_initval);
160
Trond Myklebust34f52e32007-06-14 16:40:31 -0400161 kref_init(&clnt->cl_kref);
162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 err = rpc_setup_pipedir(clnt, program->pipe_dir_name);
164 if (err < 0)
165 goto out_no_path;
166
J. Bruce Fields6a192752005-06-22 17:16:23 +0000167 auth = rpcauth_create(flavor, clnt);
168 if (IS_ERR(auth)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 printk(KERN_INFO "RPC: Couldn't create auth handle (flavor %u)\n",
170 flavor);
J. Bruce Fields6a192752005-06-22 17:16:23 +0000171 err = PTR_ERR(auth);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 goto out_no_auth;
173 }
174
175 /* save the nodename */
Serge E. Hallyne9ff3992006-10-02 02:18:11 -0700176 clnt->cl_nodelen = strlen(utsname()->nodename);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 if (clnt->cl_nodelen > UNX_MAXNODENAME)
178 clnt->cl_nodelen = UNX_MAXNODENAME;
Serge E. Hallyne9ff3992006-10-02 02:18:11 -0700179 memcpy(clnt->cl_nodename, utsname()->nodename, clnt->cl_nodelen);
Trond Myklebust6529eba2007-06-14 16:40:14 -0400180 rpc_register_client(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 return clnt;
182
183out_no_auth:
Trond Myklebust54281542006-03-20 13:44:49 -0500184 if (!IS_ERR(clnt->cl_dentry)) {
Trond Myklebustdff02cc2006-07-31 14:17:18 -0700185 rpc_rmdir(clnt->cl_dentry);
Trond Myklebust54281542006-03-20 13:44:49 -0500186 rpc_put_mount();
187 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188out_no_path:
Trond Myklebust23bf85b2006-11-21 10:40:23 -0500189 rpc_free_iostats(clnt->cl_metrics);
190out_no_stats:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 if (clnt->cl_server != clnt->cl_inline_name)
192 kfree(clnt->cl_server);
193 kfree(clnt);
194out_err:
Trond Myklebust6b6ca862006-09-05 12:55:57 -0400195 xprt_put(xprt);
Adrian Bunk712917d2006-03-13 21:20:47 -0800196out_no_xprt:
Trond Myklebust4ada5392007-06-14 17:26:17 -0400197 rpciod_down();
198out_no_rpciod:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 return ERR_PTR(err);
200}
201
Chuck Leverc2866762006-08-22 20:06:20 -0400202/*
203 * rpc_create - create an RPC client and transport with one call
204 * @args: rpc_clnt create argument structure
205 *
206 * Creates and initializes an RPC transport and an RPC client.
207 *
208 * It can ping the server in order to determine if it is up, and to see if
209 * it supports this program and version. RPC_CLNT_CREATE_NOPING disables
210 * this behavior so asynchronous tasks can also use rpc_create.
211 */
212struct rpc_clnt *rpc_create(struct rpc_create_args *args)
213{
214 struct rpc_xprt *xprt;
215 struct rpc_clnt *clnt;
216
217 xprt = xprt_create_transport(args->protocol, args->address,
218 args->addrsize, args->timeout);
219 if (IS_ERR(xprt))
220 return (struct rpc_clnt *)xprt;
221
222 /*
223 * By default, kernel RPC client connects from a reserved port.
224 * CAP_NET_BIND_SERVICE will not be set for unprivileged requesters,
225 * but it is always enabled for rpciod, which handles the connect
226 * operation.
227 */
228 xprt->resvport = 1;
229 if (args->flags & RPC_CLNT_CREATE_NONPRIVPORT)
230 xprt->resvport = 0;
231
232 dprintk("RPC: creating %s client for %s (xprt %p)\n",
Chuck Lever46121cf2007-01-31 12:14:08 -0500233 args->program->name, args->servername, xprt);
Chuck Leverc2866762006-08-22 20:06:20 -0400234
235 clnt = rpc_new_client(xprt, args->servername, args->program,
236 args->version, args->authflavor);
237 if (IS_ERR(clnt))
238 return clnt;
239
240 if (!(args->flags & RPC_CLNT_CREATE_NOPING)) {
241 int err = rpc_ping(clnt, RPC_TASK_SOFT|RPC_TASK_NOINTR);
242 if (err != 0) {
243 rpc_shutdown_client(clnt);
244 return ERR_PTR(err);
245 }
246 }
247
248 clnt->cl_softrtry = 1;
249 if (args->flags & RPC_CLNT_CREATE_HARDRTRY)
250 clnt->cl_softrtry = 0;
251
252 if (args->flags & RPC_CLNT_CREATE_INTR)
253 clnt->cl_intr = 1;
254 if (args->flags & RPC_CLNT_CREATE_AUTOBIND)
255 clnt->cl_autobind = 1;
Chuck Lever43d78ef2007-02-06 18:26:11 -0500256 if (args->flags & RPC_CLNT_CREATE_DISCRTRY)
257 clnt->cl_discrtry = 1;
Chuck Leverc2866762006-08-22 20:06:20 -0400258
259 return clnt;
260}
Chuck Leverb86acd52006-08-22 20:06:22 -0400261EXPORT_SYMBOL_GPL(rpc_create);
Chuck Leverc2866762006-08-22 20:06:20 -0400262
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263/*
264 * This function clones the RPC client structure. It allows us to share the
265 * same transport while varying parameters such as the authentication
266 * flavour.
267 */
268struct rpc_clnt *
269rpc_clone_client(struct rpc_clnt *clnt)
270{
271 struct rpc_clnt *new;
Trond Myklebust3e32a5d2006-11-16 11:37:27 -0500272 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Arnaldo Carvalho de Meloe69062b2006-11-21 01:21:34 -0200274 new = kmemdup(clnt, sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 if (!new)
276 goto out_no_clnt;
Trond Myklebustd431a5552007-06-17 17:07:54 -0400277 new->cl_parent = clnt;
278 /* Turn off autobind on clones */
279 new->cl_autobind = 0;
280 INIT_LIST_HEAD(&new->cl_tasks);
281 spin_lock_init(&new->cl_lock);
282 rpc_init_rtt(&new->cl_rtt_default, clnt->cl_xprt->timeout.to_initval);
Trond Myklebust23bf85b2006-11-21 10:40:23 -0500283 new->cl_metrics = rpc_alloc_iostats(clnt);
284 if (new->cl_metrics == NULL)
285 goto out_no_stats;
Trond Myklebust34f52e32007-06-14 16:40:31 -0400286 kref_init(&new->cl_kref);
Trond Myklebust3e32a5d2006-11-16 11:37:27 -0500287 err = rpc_setup_pipedir(new, clnt->cl_program->pipe_dir_name);
288 if (err != 0)
289 goto out_no_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 if (new->cl_auth)
291 atomic_inc(&new->cl_auth->au_count);
Trond Myklebustd431a5552007-06-17 17:07:54 -0400292 xprt_get(clnt->cl_xprt);
293 kref_get(&clnt->cl_kref);
Trond Myklebust6529eba2007-06-14 16:40:14 -0400294 rpc_register_client(new);
Trond Myklebust4ada5392007-06-14 17:26:17 -0400295 rpciod_up();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 return new;
Trond Myklebust3e32a5d2006-11-16 11:37:27 -0500297out_no_path:
298 rpc_free_iostats(new->cl_metrics);
Trond Myklebust23bf85b2006-11-21 10:40:23 -0500299out_no_stats:
300 kfree(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301out_no_clnt:
Chuck Lever46121cf2007-01-31 12:14:08 -0500302 dprintk("RPC: %s: returned error %d\n", __FUNCTION__, err);
Trond Myklebust3e32a5d2006-11-16 11:37:27 -0500303 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304}
305
306/*
307 * Properly shut down an RPC client, terminating all outstanding
Trond Myklebust90c57552007-06-09 19:49:36 -0400308 * requests.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 */
Trond Myklebust4c402b42007-06-14 16:40:32 -0400310void rpc_shutdown_client(struct rpc_clnt *clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
Trond Myklebust34f52e32007-06-14 16:40:31 -0400312 dprintk("RPC: shutting down %s client for %s\n",
313 clnt->cl_protname, clnt->cl_server);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
Trond Myklebust34f52e32007-06-14 16:40:31 -0400315 while (!list_empty(&clnt->cl_tasks)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 rpc_killall_tasks(clnt);
Ingo Molnar532347e2006-01-09 20:52:53 -0800317 wait_event_timeout(destroy_wait,
Trond Myklebust34f52e32007-06-14 16:40:31 -0400318 list_empty(&clnt->cl_tasks), 1*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 }
320
Trond Myklebust4c402b42007-06-14 16:40:32 -0400321 rpc_release_client(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322}
323
324/*
Trond Myklebust34f52e32007-06-14 16:40:31 -0400325 * Free an RPC client
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 */
Trond Myklebust34f52e32007-06-14 16:40:31 -0400327static void
328rpc_free_client(struct kref *kref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329{
Trond Myklebust34f52e32007-06-14 16:40:31 -0400330 struct rpc_clnt *clnt = container_of(kref, struct rpc_clnt, cl_kref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
Chuck Lever46121cf2007-01-31 12:14:08 -0500332 dprintk("RPC: destroying %s client for %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 clnt->cl_protname, clnt->cl_server);
334 if (clnt->cl_auth) {
335 rpcauth_destroy(clnt->cl_auth);
336 clnt->cl_auth = NULL;
337 }
Trond Myklebust8f8e7a52006-08-14 13:11:15 -0400338 if (!IS_ERR(clnt->cl_dentry)) {
Trond Myklebustdff02cc2006-07-31 14:17:18 -0700339 rpc_rmdir(clnt->cl_dentry);
Trond Myklebust8f8e7a52006-08-14 13:11:15 -0400340 rpc_put_mount();
341 }
Trond Myklebust3e32a5d2006-11-16 11:37:27 -0500342 if (clnt->cl_parent != clnt) {
Trond Myklebust8ad7c892007-06-14 16:40:32 -0400343 rpc_release_client(clnt->cl_parent);
Trond Myklebust3e32a5d2006-11-16 11:37:27 -0500344 goto out_free;
345 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 if (clnt->cl_server != clnt->cl_inline_name)
347 kfree(clnt->cl_server);
348out_free:
Trond Myklebust6529eba2007-06-14 16:40:14 -0400349 rpc_unregister_client(clnt);
Chuck Lever11c556b2006-03-20 13:44:22 -0500350 rpc_free_iostats(clnt->cl_metrics);
351 clnt->cl_metrics = NULL;
Trond Myklebust6b6ca862006-09-05 12:55:57 -0400352 xprt_put(clnt->cl_xprt);
Trond Myklebust4ada5392007-06-14 17:26:17 -0400353 rpciod_down();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 kfree(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355}
356
357/*
Trond Myklebust34f52e32007-06-14 16:40:31 -0400358 * Release reference to the RPC client
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 */
360void
361rpc_release_client(struct rpc_clnt *clnt)
362{
Trond Myklebust34f52e32007-06-14 16:40:31 -0400363 dprintk("RPC: rpc_release_client(%p)\n", clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
Trond Myklebust34f52e32007-06-14 16:40:31 -0400365 if (list_empty(&clnt->cl_tasks))
366 wake_up(&destroy_wait);
Trond Myklebust34f52e32007-06-14 16:40:31 -0400367 kref_put(&clnt->cl_kref, rpc_free_client);
368}
369
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000370/**
371 * rpc_bind_new_program - bind a new RPC program to an existing client
372 * @old - old rpc_client
373 * @program - rpc program to set
374 * @vers - rpc program version
375 *
376 * Clones the rpc client and sets up a new RPC program. This is mainly
377 * of use for enabling different RPC programs to share the same transport.
378 * The Sun NFSv2/v3 ACL protocol can do this.
379 */
380struct rpc_clnt *rpc_bind_new_program(struct rpc_clnt *old,
381 struct rpc_program *program,
382 int vers)
383{
384 struct rpc_clnt *clnt;
385 struct rpc_version *version;
386 int err;
387
388 BUG_ON(vers >= program->nrvers || !program->version[vers]);
389 version = program->version[vers];
390 clnt = rpc_clone_client(old);
391 if (IS_ERR(clnt))
392 goto out;
393 clnt->cl_procinfo = version->procs;
394 clnt->cl_maxproc = version->nrprocs;
395 clnt->cl_protname = program->name;
396 clnt->cl_prog = program->number;
397 clnt->cl_vers = version->number;
398 clnt->cl_stats = program->stats;
399 err = rpc_ping(clnt, RPC_TASK_SOFT|RPC_TASK_NOINTR);
400 if (err != 0) {
401 rpc_shutdown_client(clnt);
402 clnt = ERR_PTR(err);
403 }
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800404out:
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000405 return clnt;
406}
407
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408/*
409 * Default callback for async RPC calls
410 */
411static void
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100412rpc_default_callback(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413{
414}
415
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100416static const struct rpc_call_ops rpc_default_ops = {
417 .rpc_call_done = rpc_default_callback,
418};
419
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420/*
Trond Myklebust14b218a2005-06-22 17:16:28 +0000421 * Export the signal mask handling for synchronous code that
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 * sleeps on RPC calls
423 */
Trond Myklebust2bd61572006-01-03 09:55:19 +0100424#define RPC_INTR_SIGNALS (sigmask(SIGHUP) | sigmask(SIGINT) | sigmask(SIGQUIT) | sigmask(SIGTERM))
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800425
Trond Myklebust14b218a2005-06-22 17:16:28 +0000426static void rpc_save_sigmask(sigset_t *oldset, int intr)
427{
Trond Myklebust2bd61572006-01-03 09:55:19 +0100428 unsigned long sigallow = sigmask(SIGKILL);
Trond Myklebust14b218a2005-06-22 17:16:28 +0000429 sigset_t sigmask;
430
431 /* Block all signals except those listed in sigallow */
432 if (intr)
433 sigallow |= RPC_INTR_SIGNALS;
434 siginitsetinv(&sigmask, sigallow);
435 sigprocmask(SIG_BLOCK, &sigmask, oldset);
436}
437
438static inline void rpc_task_sigmask(struct rpc_task *task, sigset_t *oldset)
439{
440 rpc_save_sigmask(oldset, !RPC_TASK_UNINTERRUPTIBLE(task));
441}
442
443static inline void rpc_restore_sigmask(sigset_t *oldset)
444{
445 sigprocmask(SIG_SETMASK, oldset, NULL);
446}
447
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448void rpc_clnt_sigmask(struct rpc_clnt *clnt, sigset_t *oldset)
449{
Trond Myklebust14b218a2005-06-22 17:16:28 +0000450 rpc_save_sigmask(oldset, clnt->cl_intr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451}
452
453void rpc_clnt_sigunmask(struct rpc_clnt *clnt, sigset_t *oldset)
454{
Trond Myklebust14b218a2005-06-22 17:16:28 +0000455 rpc_restore_sigmask(oldset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456}
457
458/*
459 * New rpc_call implementation
460 */
461int rpc_call_sync(struct rpc_clnt *clnt, struct rpc_message *msg, int flags)
462{
463 struct rpc_task *task;
464 sigset_t oldset;
465 int status;
466
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 BUG_ON(flags & RPC_TASK_ASYNC);
468
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100469 task = rpc_new_task(clnt, flags, &rpc_default_ops, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 if (task == NULL)
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -0500471 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
Trond Myklebust14b218a2005-06-22 17:16:28 +0000473 /* Mask signals on RPC calls _and_ GSS_AUTH upcalls */
474 rpc_task_sigmask(task, &oldset);
475
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 /* Set up the call info struct and execute the task */
Trond Myklebust2efef832007-02-03 13:38:41 -0800477 rpc_call_setup(task, msg, 0);
478 if (task->tk_status == 0) {
479 atomic_inc(&task->tk_count);
480 rpc_execute(task);
481 }
Trond Myklebuste60859a2006-01-03 09:55:10 +0100482 status = task->tk_status;
Trond Myklebustbde8f002007-01-24 11:54:53 -0800483 rpc_put_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -0500484 rpc_restore_sigmask(&oldset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 return status;
486}
487
488/*
489 * New rpc_call implementation
490 */
491int
492rpc_call_async(struct rpc_clnt *clnt, struct rpc_message *msg, int flags,
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100493 const struct rpc_call_ops *tk_ops, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
495 struct rpc_task *task;
496 sigset_t oldset;
497 int status;
498
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 flags |= RPC_TASK_ASYNC;
500
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 /* Create/initialize a new RPC task */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 status = -ENOMEM;
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100503 if (!(task = rpc_new_task(clnt, flags, tk_ops, data)))
Trond Myklebust7a1218a2006-03-20 18:11:10 -0500504 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
Trond Myklebust14b218a2005-06-22 17:16:28 +0000506 /* Mask signals on GSS_AUTH upcalls */
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800507 rpc_task_sigmask(task, &oldset);
Trond Myklebust14b218a2005-06-22 17:16:28 +0000508
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 rpc_call_setup(task, msg, 0);
510
511 /* Set up the call info struct and execute the task */
512 status = task->tk_status;
513 if (status == 0)
514 rpc_execute(task);
515 else
Trond Myklebustbde8f002007-01-24 11:54:53 -0800516 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800518 rpc_restore_sigmask(&oldset);
Trond Myklebust7a1218a2006-03-20 18:11:10 -0500519 return status;
520out_release:
Trond Myklebustbbd5a1f2006-10-18 16:01:05 -0400521 rpc_release_calldata(tk_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 return status;
523}
524
525
526void
527rpc_call_setup(struct rpc_task *task, struct rpc_message *msg, int flags)
528{
529 task->tk_msg = *msg;
530 task->tk_flags |= flags;
531 /* Bind the user cred */
532 if (task->tk_msg.rpc_cred != NULL)
533 rpcauth_holdcred(task);
534 else
535 rpcauth_bindcred(task);
536
537 if (task->tk_status == 0)
538 task->tk_action = call_start;
539 else
Trond Myklebustabbcf282006-01-03 09:55:03 +0100540 task->tk_action = rpc_exit_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541}
542
Chuck Levered394402006-08-22 20:06:17 -0400543/**
544 * rpc_peeraddr - extract remote peer address from clnt's xprt
545 * @clnt: RPC client structure
546 * @buf: target buffer
547 * @size: length of target buffer
548 *
549 * Returns the number of bytes that are actually in the stored address.
550 */
551size_t rpc_peeraddr(struct rpc_clnt *clnt, struct sockaddr *buf, size_t bufsize)
552{
553 size_t bytes;
554 struct rpc_xprt *xprt = clnt->cl_xprt;
555
556 bytes = sizeof(xprt->addr);
557 if (bytes > bufsize)
558 bytes = bufsize;
559 memcpy(buf, &clnt->cl_xprt->addr, bytes);
Chuck Leverc4efcb12006-08-22 20:06:19 -0400560 return xprt->addrlen;
Chuck Levered394402006-08-22 20:06:17 -0400561}
Chuck Leverb86acd52006-08-22 20:06:22 -0400562EXPORT_SYMBOL_GPL(rpc_peeraddr);
Chuck Levered394402006-08-22 20:06:17 -0400563
Chuck Leverf425eba2006-08-22 20:06:18 -0400564/**
565 * rpc_peeraddr2str - return remote peer address in printable format
566 * @clnt: RPC client structure
567 * @format: address format
568 *
569 */
570char *rpc_peeraddr2str(struct rpc_clnt *clnt, enum rpc_display_format_t format)
571{
572 struct rpc_xprt *xprt = clnt->cl_xprt;
Chuck Lever7559c7a2006-12-05 16:35:37 -0500573
574 if (xprt->address_strings[format] != NULL)
575 return xprt->address_strings[format];
576 else
577 return "unprintable";
Chuck Leverf425eba2006-08-22 20:06:18 -0400578}
Chuck Leverb86acd52006-08-22 20:06:22 -0400579EXPORT_SYMBOL_GPL(rpc_peeraddr2str);
Chuck Leverf425eba2006-08-22 20:06:18 -0400580
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581void
582rpc_setbufsize(struct rpc_clnt *clnt, unsigned int sndsize, unsigned int rcvsize)
583{
584 struct rpc_xprt *xprt = clnt->cl_xprt;
Chuck Lever470056c2005-08-25 16:25:56 -0700585 if (xprt->ops->set_buffer_size)
586 xprt->ops->set_buffer_size(xprt, sndsize, rcvsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587}
588
589/*
590 * Return size of largest payload RPC client can support, in bytes
591 *
592 * For stream transports, this is one RPC record fragment (see RFC
593 * 1831), as we don't support multi-record requests yet. For datagram
594 * transports, this is the size of an IP packet minus the IP, UDP, and
595 * RPC header sizes.
596 */
597size_t rpc_max_payload(struct rpc_clnt *clnt)
598{
599 return clnt->cl_xprt->max_payload;
600}
Chuck Leverb86acd52006-08-22 20:06:22 -0400601EXPORT_SYMBOL_GPL(rpc_max_payload);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
Chuck Lever35f5a422006-01-03 09:55:50 +0100603/**
604 * rpc_force_rebind - force transport to check that remote port is unchanged
605 * @clnt: client to rebind
606 *
607 */
608void rpc_force_rebind(struct rpc_clnt *clnt)
609{
610 if (clnt->cl_autobind)
Chuck Leverec739ef2006-08-22 20:06:15 -0400611 xprt_clear_bound(clnt->cl_xprt);
Chuck Lever35f5a422006-01-03 09:55:50 +0100612}
Chuck Leverb86acd52006-08-22 20:06:22 -0400613EXPORT_SYMBOL_GPL(rpc_force_rebind);
Chuck Lever35f5a422006-01-03 09:55:50 +0100614
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615/*
616 * Restart an (async) RPC call. Usually called from within the
617 * exit handler.
618 */
619void
620rpc_restart_call(struct rpc_task *task)
621{
622 if (RPC_ASSASSINATED(task))
623 return;
624
625 task->tk_action = call_start;
626}
627
628/*
629 * 0. Initial state
630 *
631 * Other FSM states can be visited zero or more times, but
632 * this state is visited exactly once for each RPC.
633 */
634static void
635call_start(struct rpc_task *task)
636{
637 struct rpc_clnt *clnt = task->tk_client;
638
Chuck Lever46121cf2007-01-31 12:14:08 -0500639 dprintk("RPC: %5u call_start %s%d proc %d (%s)\n", task->tk_pid,
640 clnt->cl_protname, clnt->cl_vers,
641 task->tk_msg.rpc_proc->p_proc,
642 (RPC_IS_ASYNC(task) ? "async" : "sync"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
644 /* Increment call count */
645 task->tk_msg.rpc_proc->p_count++;
646 clnt->cl_stats->rpccnt++;
647 task->tk_action = call_reserve;
648}
649
650/*
651 * 1. Reserve an RPC call slot
652 */
653static void
654call_reserve(struct rpc_task *task)
655{
Chuck Lever46121cf2007-01-31 12:14:08 -0500656 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
658 if (!rpcauth_uptodatecred(task)) {
659 task->tk_action = call_refresh;
660 return;
661 }
662
663 task->tk_status = 0;
664 task->tk_action = call_reserveresult;
665 xprt_reserve(task);
666}
667
668/*
669 * 1b. Grok the result of xprt_reserve()
670 */
671static void
672call_reserveresult(struct rpc_task *task)
673{
674 int status = task->tk_status;
675
Chuck Lever46121cf2007-01-31 12:14:08 -0500676 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
678 /*
679 * After a call to xprt_reserve(), we must have either
680 * a request slot or else an error status.
681 */
682 task->tk_status = 0;
683 if (status >= 0) {
684 if (task->tk_rqstp) {
685 task->tk_action = call_allocate;
686 return;
687 }
688
689 printk(KERN_ERR "%s: status=%d, but no request slot, exiting\n",
690 __FUNCTION__, status);
691 rpc_exit(task, -EIO);
692 return;
693 }
694
695 /*
696 * Even though there was an error, we may have acquired
697 * a request slot somehow. Make sure not to leak it.
698 */
699 if (task->tk_rqstp) {
700 printk(KERN_ERR "%s: status=%d, request allocated anyway\n",
701 __FUNCTION__, status);
702 xprt_release(task);
703 }
704
705 switch (status) {
706 case -EAGAIN: /* woken up; retry */
707 task->tk_action = call_reserve;
708 return;
709 case -EIO: /* probably a shutdown */
710 break;
711 default:
712 printk(KERN_ERR "%s: unrecognized error %d, exiting\n",
713 __FUNCTION__, status);
714 break;
715 }
716 rpc_exit(task, status);
717}
718
719/*
720 * 2. Allocate the buffer. For details, see sched.c:rpc_malloc.
Chuck Lever02107142006-01-03 09:55:49 +0100721 * (Note: buffer memory is freed in xprt_release).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 */
723static void
724call_allocate(struct rpc_task *task)
725{
Chuck Lever2bea90d2007-03-29 16:47:53 -0400726 unsigned int slack = task->tk_auth->au_cslack;
Chuck Lever02107142006-01-03 09:55:49 +0100727 struct rpc_rqst *req = task->tk_rqstp;
728 struct rpc_xprt *xprt = task->tk_xprt;
Chuck Lever2bea90d2007-03-29 16:47:53 -0400729 struct rpc_procinfo *proc = task->tk_msg.rpc_proc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
Chuck Lever46121cf2007-01-31 12:14:08 -0500731 dprint_status(task);
732
Chuck Lever2bea90d2007-03-29 16:47:53 -0400733 task->tk_status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 task->tk_action = call_bind;
Chuck Lever2bea90d2007-03-29 16:47:53 -0400735
Chuck Lever02107142006-01-03 09:55:49 +0100736 if (req->rq_buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 return;
738
Chuck Lever2bea90d2007-03-29 16:47:53 -0400739 if (proc->p_proc != 0) {
740 BUG_ON(proc->p_arglen == 0);
741 if (proc->p_decode != NULL)
742 BUG_ON(proc->p_replen == 0);
743 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
Chuck Lever2bea90d2007-03-29 16:47:53 -0400745 /*
746 * Calculate the size (in quads) of the RPC call
747 * and reply headers, and convert both values
748 * to byte sizes.
749 */
750 req->rq_callsize = RPC_CALLHDRSIZE + (slack << 1) + proc->p_arglen;
751 req->rq_callsize <<= 2;
752 req->rq_rcvsize = RPC_REPHDRSIZE + slack + proc->p_replen;
753 req->rq_rcvsize <<= 2;
754
Chuck Leverc5a4dd82007-03-29 16:47:58 -0400755 req->rq_buffer = xprt->ops->buf_alloc(task,
756 req->rq_callsize + req->rq_rcvsize);
Chuck Lever2bea90d2007-03-29 16:47:53 -0400757 if (req->rq_buffer != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 return;
Chuck Lever46121cf2007-01-31 12:14:08 -0500759
760 dprintk("RPC: %5u rpc_buffer allocation failed\n", task->tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
Trond Myklebust14b218a2005-06-22 17:16:28 +0000762 if (RPC_IS_ASYNC(task) || !signalled()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 xprt_release(task);
764 task->tk_action = call_reserve;
765 rpc_delay(task, HZ>>4);
766 return;
767 }
768
769 rpc_exit(task, -ERESTARTSYS);
770}
771
Trond Myklebust940e3312005-11-09 21:45:24 -0500772static inline int
773rpc_task_need_encode(struct rpc_task *task)
774{
775 return task->tk_rqstp->rq_snd_buf.len == 0;
776}
777
778static inline void
779rpc_task_force_reencode(struct rpc_task *task)
780{
781 task->tk_rqstp->rq_snd_buf.len = 0;
782}
783
Chuck Lever2bea90d2007-03-29 16:47:53 -0400784static inline void
785rpc_xdr_buf_init(struct xdr_buf *buf, void *start, size_t len)
786{
787 buf->head[0].iov_base = start;
788 buf->head[0].iov_len = len;
789 buf->tail[0].iov_len = 0;
790 buf->page_len = 0;
791 buf->len = 0;
792 buf->buflen = len;
793}
794
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795/*
796 * 3. Encode arguments of an RPC call
797 */
798static void
799call_encode(struct rpc_task *task)
800{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 struct rpc_rqst *req = task->tk_rqstp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 kxdrproc_t encode;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700803 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
Chuck Lever46121cf2007-01-31 12:14:08 -0500805 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
Chuck Lever2bea90d2007-03-29 16:47:53 -0400807 rpc_xdr_buf_init(&req->rq_snd_buf,
808 req->rq_buffer,
809 req->rq_callsize);
810 rpc_xdr_buf_init(&req->rq_rcv_buf,
811 (char *)req->rq_buffer + req->rq_callsize,
812 req->rq_rcvsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
814 /* Encode header and provided arguments */
815 encode = task->tk_msg.rpc_proc->p_encode;
816 if (!(p = call_header(task))) {
817 printk(KERN_INFO "RPC: call_header failed, exit EIO\n");
818 rpc_exit(task, -EIO);
819 return;
820 }
J. Bruce Fieldsf3680312005-10-13 16:54:48 -0400821 if (encode == NULL)
822 return;
823
Trond Myklebust6d5fcb52006-10-18 16:01:06 -0400824 lock_kernel();
J. Bruce Fieldsf3680312005-10-13 16:54:48 -0400825 task->tk_status = rpcauth_wrap_req(task, encode, req, p,
826 task->tk_msg.rpc_argp);
Trond Myklebust6d5fcb52006-10-18 16:01:06 -0400827 unlock_kernel();
J. Bruce Fieldsf3680312005-10-13 16:54:48 -0400828 if (task->tk_status == -ENOMEM) {
829 /* XXX: Is this sane? */
830 rpc_delay(task, 3*HZ);
831 task->tk_status = -EAGAIN;
832 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833}
834
835/*
836 * 4. Get the server port number if not yet set
837 */
838static void
839call_bind(struct rpc_task *task)
840{
Chuck Leverec739ef2006-08-22 20:06:15 -0400841 struct rpc_xprt *xprt = task->tk_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
Chuck Lever46121cf2007-01-31 12:14:08 -0500843 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
Chuck Leverda351872005-08-11 16:25:11 -0400845 task->tk_action = call_connect;
Chuck Leverec739ef2006-08-22 20:06:15 -0400846 if (!xprt_bound(xprt)) {
Chuck Leverda351872005-08-11 16:25:11 -0400847 task->tk_action = call_bind_status;
Chuck Leverec739ef2006-08-22 20:06:15 -0400848 task->tk_timeout = xprt->bind_timeout;
Chuck Leverbbf7c1d2006-08-22 20:06:16 -0400849 xprt->ops->rpcbind(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 }
851}
852
853/*
Chuck Leverda351872005-08-11 16:25:11 -0400854 * 4a. Sort out bind result
855 */
856static void
857call_bind_status(struct rpc_task *task)
858{
859 int status = -EACCES;
860
861 if (task->tk_status >= 0) {
Chuck Lever46121cf2007-01-31 12:14:08 -0500862 dprint_status(task);
Chuck Leverda351872005-08-11 16:25:11 -0400863 task->tk_status = 0;
864 task->tk_action = call_connect;
865 return;
866 }
867
868 switch (task->tk_status) {
869 case -EACCES:
Chuck Lever46121cf2007-01-31 12:14:08 -0500870 dprintk("RPC: %5u remote rpcbind: RPC program/version "
871 "unavailable\n", task->tk_pid);
Chuck Leverea635a52005-10-06 23:12:58 -0400872 rpc_delay(task, 3*HZ);
Trond Myklebustda458282006-08-31 15:44:52 -0400873 goto retry_timeout;
Chuck Leverda351872005-08-11 16:25:11 -0400874 case -ETIMEDOUT:
Chuck Lever46121cf2007-01-31 12:14:08 -0500875 dprintk("RPC: %5u rpcbind request timed out\n",
Chuck Leverda351872005-08-11 16:25:11 -0400876 task->tk_pid);
Trond Myklebustda458282006-08-31 15:44:52 -0400877 goto retry_timeout;
Chuck Leverda351872005-08-11 16:25:11 -0400878 case -EPFNOSUPPORT:
Chuck Lever46121cf2007-01-31 12:14:08 -0500879 dprintk("RPC: %5u remote rpcbind service unavailable\n",
Chuck Leverda351872005-08-11 16:25:11 -0400880 task->tk_pid);
881 break;
882 case -EPROTONOSUPPORT:
Chuck Lever00a6e7b2007-03-29 16:48:33 -0400883 dprintk("RPC: %5u remote rpcbind version unavailable, retrying\n",
Chuck Leverda351872005-08-11 16:25:11 -0400884 task->tk_pid);
Chuck Lever00a6e7b2007-03-29 16:48:33 -0400885 task->tk_status = 0;
886 task->tk_action = call_bind;
887 return;
Chuck Leverda351872005-08-11 16:25:11 -0400888 default:
Chuck Lever46121cf2007-01-31 12:14:08 -0500889 dprintk("RPC: %5u unrecognized rpcbind error (%d)\n",
Chuck Leverda351872005-08-11 16:25:11 -0400890 task->tk_pid, -task->tk_status);
891 status = -EIO;
Chuck Leverda351872005-08-11 16:25:11 -0400892 }
893
894 rpc_exit(task, status);
895 return;
896
Trond Myklebustda458282006-08-31 15:44:52 -0400897retry_timeout:
898 task->tk_action = call_timeout;
Chuck Leverda351872005-08-11 16:25:11 -0400899}
900
901/*
902 * 4b. Connect to the RPC server
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 */
904static void
905call_connect(struct rpc_task *task)
906{
Chuck Leverda351872005-08-11 16:25:11 -0400907 struct rpc_xprt *xprt = task->tk_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
Chuck Lever46121cf2007-01-31 12:14:08 -0500909 dprintk("RPC: %5u call_connect xprt %p %s connected\n",
Chuck Leverda351872005-08-11 16:25:11 -0400910 task->tk_pid, xprt,
911 (xprt_connected(xprt) ? "is" : "is not"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
Chuck Leverda351872005-08-11 16:25:11 -0400913 task->tk_action = call_transmit;
914 if (!xprt_connected(xprt)) {
915 task->tk_action = call_connect_status;
916 if (task->tk_status < 0)
917 return;
918 xprt_connect(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920}
921
922/*
Chuck Leverda351872005-08-11 16:25:11 -0400923 * 4c. Sort out connect result
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 */
925static void
926call_connect_status(struct rpc_task *task)
927{
928 struct rpc_clnt *clnt = task->tk_client;
929 int status = task->tk_status;
930
Chuck Lever46121cf2007-01-31 12:14:08 -0500931 dprint_status(task);
Chuck Leverda351872005-08-11 16:25:11 -0400932
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 task->tk_status = 0;
934 if (status >= 0) {
935 clnt->cl_stats->netreconn++;
936 task->tk_action = call_transmit;
937 return;
938 }
939
Chuck Leverda351872005-08-11 16:25:11 -0400940 /* Something failed: remote service port may have changed */
Chuck Lever35f5a422006-01-03 09:55:50 +0100941 rpc_force_rebind(clnt);
Chuck Leverda351872005-08-11 16:25:11 -0400942
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 switch (status) {
944 case -ENOTCONN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 case -EAGAIN:
Chuck Leverda351872005-08-11 16:25:11 -0400946 task->tk_action = call_bind;
Trond Myklebustda458282006-08-31 15:44:52 -0400947 if (!RPC_IS_SOFT(task))
948 return;
949 /* if soft mounted, test if we've timed out */
950 case -ETIMEDOUT:
951 task->tk_action = call_timeout;
952 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 }
Trond Myklebustda458282006-08-31 15:44:52 -0400954 rpc_exit(task, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955}
956
957/*
958 * 5. Transmit the RPC request, and wait for reply
959 */
960static void
961call_transmit(struct rpc_task *task)
962{
Chuck Lever46121cf2007-01-31 12:14:08 -0500963 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
965 task->tk_action = call_status;
966 if (task->tk_status < 0)
967 return;
968 task->tk_status = xprt_prepare_transmit(task);
969 if (task->tk_status != 0)
970 return;
Trond Myklebuste0ab53d2006-07-27 17:22:50 -0400971 task->tk_action = call_transmit_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 /* Encode here so that rpcsec_gss can use correct sequence number. */
Trond Myklebust940e3312005-11-09 21:45:24 -0500973 if (rpc_task_need_encode(task)) {
Trond Myklebuste0ab53d2006-07-27 17:22:50 -0400974 BUG_ON(task->tk_rqstp->rq_bytes_sent != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 call_encode(task);
Trond Myklebust5e5ce5b2005-10-18 14:20:11 -0700976 /* Did the encode result in an error condition? */
977 if (task->tk_status != 0)
Trond Myklebuste0ab53d2006-07-27 17:22:50 -0400978 return;
Trond Myklebust5e5ce5b2005-10-18 14:20:11 -0700979 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 xprt_transmit(task);
981 if (task->tk_status < 0)
982 return;
Trond Myklebuste0ab53d2006-07-27 17:22:50 -0400983 /*
984 * On success, ensure that we call xprt_end_transmit() before sleeping
985 * in order to allow access to the socket to other RPC requests.
986 */
987 call_transmit_status(task);
988 if (task->tk_msg.rpc_proc->p_decode != NULL)
989 return;
990 task->tk_action = rpc_exit_task;
991 rpc_wake_up_task(task);
992}
993
994/*
995 * 5a. Handle cleanup after a transmission
996 */
997static void
998call_transmit_status(struct rpc_task *task)
999{
1000 task->tk_action = call_status;
1001 /*
1002 * Special case: if we've been waiting on the socket's write_space()
1003 * callback, then don't call xprt_end_transmit().
1004 */
1005 if (task->tk_status == -EAGAIN)
1006 return;
1007 xprt_end_transmit(task);
Trond Myklebust940e3312005-11-09 21:45:24 -05001008 rpc_task_force_reencode(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009}
1010
1011/*
1012 * 6. Sort out the RPC call status
1013 */
1014static void
1015call_status(struct rpc_task *task)
1016{
1017 struct rpc_clnt *clnt = task->tk_client;
1018 struct rpc_rqst *req = task->tk_rqstp;
1019 int status;
1020
1021 if (req->rq_received > 0 && !req->rq_bytes_sent)
1022 task->tk_status = req->rq_received;
1023
Chuck Lever46121cf2007-01-31 12:14:08 -05001024 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
1026 status = task->tk_status;
1027 if (status >= 0) {
1028 task->tk_action = call_decode;
1029 return;
1030 }
1031
1032 task->tk_status = 0;
1033 switch(status) {
Trond Myklebust76303992006-08-30 14:32:49 -04001034 case -EHOSTDOWN:
1035 case -EHOSTUNREACH:
1036 case -ENETUNREACH:
1037 /*
1038 * Delay any retries for 3 seconds, then handle as if it
1039 * were a timeout.
1040 */
1041 rpc_delay(task, 3*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 case -ETIMEDOUT:
1043 task->tk_action = call_timeout;
Trond Myklebust241c39b2007-04-20 16:12:55 -04001044 if (task->tk_client->cl_discrtry)
1045 xprt_disconnect(task->tk_xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 break;
1047 case -ECONNREFUSED:
1048 case -ENOTCONN:
Chuck Lever35f5a422006-01-03 09:55:50 +01001049 rpc_force_rebind(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 task->tk_action = call_bind;
1051 break;
1052 case -EAGAIN:
1053 task->tk_action = call_transmit;
1054 break;
1055 case -EIO:
1056 /* shutdown or soft timeout */
1057 rpc_exit(task, status);
1058 break;
1059 default:
Chuck Leverf518e352006-01-03 09:55:52 +01001060 printk("%s: RPC call returned error %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 clnt->cl_protname, -status);
1062 rpc_exit(task, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 }
1064}
1065
1066/*
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04001067 * 6a. Handle RPC timeout
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 * We do not release the request slot, so we keep using the
1069 * same XID for all retransmits.
1070 */
1071static void
1072call_timeout(struct rpc_task *task)
1073{
1074 struct rpc_clnt *clnt = task->tk_client;
1075
1076 if (xprt_adjust_timeout(task->tk_rqstp) == 0) {
Chuck Lever46121cf2007-01-31 12:14:08 -05001077 dprintk("RPC: %5u call_timeout (minor)\n", task->tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 goto retry;
1079 }
1080
Chuck Lever46121cf2007-01-31 12:14:08 -05001081 dprintk("RPC: %5u call_timeout (major)\n", task->tk_pid);
Chuck Leveref759a22006-03-20 13:44:17 -05001082 task->tk_timeouts++;
1083
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 if (RPC_IS_SOFT(task)) {
Chuck Leverf518e352006-01-03 09:55:52 +01001085 printk(KERN_NOTICE "%s: server %s not responding, timed out\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 clnt->cl_protname, clnt->cl_server);
1087 rpc_exit(task, -EIO);
1088 return;
1089 }
1090
Chuck Leverf518e352006-01-03 09:55:52 +01001091 if (!(task->tk_flags & RPC_CALL_MAJORSEEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 task->tk_flags |= RPC_CALL_MAJORSEEN;
1093 printk(KERN_NOTICE "%s: server %s not responding, still trying\n",
1094 clnt->cl_protname, clnt->cl_server);
1095 }
Chuck Lever35f5a422006-01-03 09:55:50 +01001096 rpc_force_rebind(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
1098retry:
1099 clnt->cl_stats->rpcretrans++;
1100 task->tk_action = call_bind;
1101 task->tk_status = 0;
1102}
1103
1104/*
1105 * 7. Decode the RPC reply
1106 */
1107static void
1108call_decode(struct rpc_task *task)
1109{
1110 struct rpc_clnt *clnt = task->tk_client;
1111 struct rpc_rqst *req = task->tk_rqstp;
1112 kxdrproc_t decode = task->tk_msg.rpc_proc->p_decode;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001113 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
Chuck Lever46121cf2007-01-31 12:14:08 -05001115 dprintk("RPC: %5u call_decode (status %d)\n",
1116 task->tk_pid, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
Chuck Leverf518e352006-01-03 09:55:52 +01001118 if (task->tk_flags & RPC_CALL_MAJORSEEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 printk(KERN_NOTICE "%s: server %s OK\n",
1120 clnt->cl_protname, clnt->cl_server);
1121 task->tk_flags &= ~RPC_CALL_MAJORSEEN;
1122 }
1123
1124 if (task->tk_status < 12) {
1125 if (!RPC_IS_SOFT(task)) {
1126 task->tk_action = call_bind;
1127 clnt->cl_stats->rpcretrans++;
1128 goto out_retry;
1129 }
Chuck Lever46121cf2007-01-31 12:14:08 -05001130 dprintk("RPC: %s: too small RPC reply size (%d bytes)\n",
1131 clnt->cl_protname, task->tk_status);
Trond Myklebustda458282006-08-31 15:44:52 -04001132 task->tk_action = call_timeout;
1133 goto out_retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 }
1135
Trond Myklebust43ac3f22006-03-20 13:44:51 -05001136 /*
1137 * Ensure that we see all writes made by xprt_complete_rqst()
1138 * before it changed req->rq_received.
1139 */
1140 smp_rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 req->rq_rcv_buf.len = req->rq_private_buf.len;
1142
1143 /* Check that the softirq receive buffer is valid */
1144 WARN_ON(memcmp(&req->rq_rcv_buf, &req->rq_private_buf,
1145 sizeof(req->rq_rcv_buf)) != 0);
1146
1147 /* Verify the RPC header */
Trond Myklebustabbcf282006-01-03 09:55:03 +01001148 p = call_verify(task);
1149 if (IS_ERR(p)) {
1150 if (p == ERR_PTR(-EAGAIN))
1151 goto out_retry;
1152 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 }
1154
Trond Myklebustabbcf282006-01-03 09:55:03 +01001155 task->tk_action = rpc_exit_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
Trond Myklebust6d5fcb52006-10-18 16:01:06 -04001157 if (decode) {
1158 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 task->tk_status = rpcauth_unwrap_resp(task, decode, req, p,
1160 task->tk_msg.rpc_resp);
Trond Myklebust6d5fcb52006-10-18 16:01:06 -04001161 unlock_kernel();
1162 }
Chuck Lever46121cf2007-01-31 12:14:08 -05001163 dprintk("RPC: %5u call_decode result %d\n", task->tk_pid,
1164 task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 return;
1166out_retry:
1167 req->rq_received = req->rq_private_buf.len = 0;
1168 task->tk_status = 0;
Trond Myklebust241c39b2007-04-20 16:12:55 -04001169 if (task->tk_client->cl_discrtry)
1170 xprt_disconnect(task->tk_xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171}
1172
1173/*
1174 * 8. Refresh the credentials if rejected by the server
1175 */
1176static void
1177call_refresh(struct rpc_task *task)
1178{
Chuck Lever46121cf2007-01-31 12:14:08 -05001179 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
1181 xprt_release(task); /* Must do to obtain new XID */
1182 task->tk_action = call_refreshresult;
1183 task->tk_status = 0;
1184 task->tk_client->cl_stats->rpcauthrefresh++;
1185 rpcauth_refreshcred(task);
1186}
1187
1188/*
1189 * 8a. Process the results of a credential refresh
1190 */
1191static void
1192call_refreshresult(struct rpc_task *task)
1193{
1194 int status = task->tk_status;
Chuck Lever46121cf2007-01-31 12:14:08 -05001195
1196 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
1198 task->tk_status = 0;
1199 task->tk_action = call_reserve;
1200 if (status >= 0 && rpcauth_uptodatecred(task))
1201 return;
1202 if (status == -EACCES) {
1203 rpc_exit(task, -EACCES);
1204 return;
1205 }
1206 task->tk_action = call_refresh;
1207 if (status != -ETIMEDOUT)
1208 rpc_delay(task, 3*HZ);
1209 return;
1210}
1211
1212/*
1213 * Call header serialization
1214 */
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001215static __be32 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216call_header(struct rpc_task *task)
1217{
1218 struct rpc_clnt *clnt = task->tk_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 struct rpc_rqst *req = task->tk_rqstp;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001220 __be32 *p = req->rq_svec[0].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
1222 /* FIXME: check buffer size? */
Chuck Lever808012f2005-08-25 16:25:49 -07001223
1224 p = xprt_skip_transport_header(task->tk_xprt, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 *p++ = req->rq_xid; /* XID */
1226 *p++ = htonl(RPC_CALL); /* CALL */
1227 *p++ = htonl(RPC_VERSION); /* RPC version */
1228 *p++ = htonl(clnt->cl_prog); /* program number */
1229 *p++ = htonl(clnt->cl_vers); /* program version */
1230 *p++ = htonl(task->tk_msg.rpc_proc->p_proc); /* procedure */
Trond Myklebust334ccfd2005-06-22 17:16:19 +00001231 p = rpcauth_marshcred(task, p);
1232 req->rq_slen = xdr_adjust_iovec(&req->rq_svec[0], p);
1233 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234}
1235
1236/*
1237 * Reply header verification
1238 */
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001239static __be32 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240call_verify(struct rpc_task *task)
1241{
1242 struct kvec *iov = &task->tk_rqstp->rq_rcv_buf.head[0];
1243 int len = task->tk_rqstp->rq_rcv_buf.len >> 2;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001244 __be32 *p = iov->iov_base;
1245 u32 n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 int error = -EACCES;
1247
David Howellse8896492006-08-24 15:44:19 -04001248 if ((task->tk_rqstp->rq_rcv_buf.len & 3) != 0) {
1249 /* RFC-1014 says that the representation of XDR data must be a
1250 * multiple of four bytes
1251 * - if it isn't pointer subtraction in the NFS client may give
1252 * undefined results
1253 */
1254 printk(KERN_WARNING
1255 "call_verify: XDR representation not a multiple of"
1256 " 4 bytes: 0x%x\n", task->tk_rqstp->rq_rcv_buf.len);
1257 goto out_eio;
1258 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 if ((len -= 3) < 0)
1260 goto out_overflow;
1261 p += 1; /* skip XID */
1262
1263 if ((n = ntohl(*p++)) != RPC_REPLY) {
1264 printk(KERN_WARNING "call_verify: not an RPC reply: %x\n", n);
Trond Myklebustabbcf282006-01-03 09:55:03 +01001265 goto out_garbage;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 }
1267 if ((n = ntohl(*p++)) != RPC_MSG_ACCEPTED) {
1268 if (--len < 0)
1269 goto out_overflow;
1270 switch ((n = ntohl(*p++))) {
1271 case RPC_AUTH_ERROR:
1272 break;
1273 case RPC_MISMATCH:
Chuck Lever46121cf2007-01-31 12:14:08 -05001274 dprintk("RPC: %5u %s: RPC call version "
1275 "mismatch!\n",
1276 task->tk_pid, __FUNCTION__);
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00001277 error = -EPROTONOSUPPORT;
1278 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 default:
Chuck Lever46121cf2007-01-31 12:14:08 -05001280 dprintk("RPC: %5u %s: RPC call rejected, "
1281 "unknown error: %x\n",
1282 task->tk_pid, __FUNCTION__, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 goto out_eio;
1284 }
1285 if (--len < 0)
1286 goto out_overflow;
1287 switch ((n = ntohl(*p++))) {
1288 case RPC_AUTH_REJECTEDCRED:
1289 case RPC_AUTH_REJECTEDVERF:
1290 case RPCSEC_GSS_CREDPROBLEM:
1291 case RPCSEC_GSS_CTXPROBLEM:
1292 if (!task->tk_cred_retry)
1293 break;
1294 task->tk_cred_retry--;
Chuck Lever46121cf2007-01-31 12:14:08 -05001295 dprintk("RPC: %5u %s: retry stale creds\n",
1296 task->tk_pid, __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 rpcauth_invalcred(task);
1298 task->tk_action = call_refresh;
Trond Myklebustabbcf282006-01-03 09:55:03 +01001299 goto out_retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 case RPC_AUTH_BADCRED:
1301 case RPC_AUTH_BADVERF:
1302 /* possibly garbled cred/verf? */
1303 if (!task->tk_garb_retry)
1304 break;
1305 task->tk_garb_retry--;
Chuck Lever46121cf2007-01-31 12:14:08 -05001306 dprintk("RPC: %5u %s: retry garbled creds\n",
1307 task->tk_pid, __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 task->tk_action = call_bind;
Trond Myklebustabbcf282006-01-03 09:55:03 +01001309 goto out_retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 case RPC_AUTH_TOOWEAK:
Levent Serinol1356b8c2006-03-20 13:44:11 -05001311 printk(KERN_NOTICE "call_verify: server %s requires stronger "
1312 "authentication.\n", task->tk_client->cl_server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 break;
1314 default:
1315 printk(KERN_WARNING "call_verify: unknown auth error: %x\n", n);
1316 error = -EIO;
1317 }
Chuck Lever46121cf2007-01-31 12:14:08 -05001318 dprintk("RPC: %5u %s: call rejected %d\n",
1319 task->tk_pid, __FUNCTION__, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 goto out_err;
1321 }
1322 if (!(p = rpcauth_checkverf(task, p))) {
1323 printk(KERN_WARNING "call_verify: auth check failed\n");
Trond Myklebustabbcf282006-01-03 09:55:03 +01001324 goto out_garbage; /* bad verifier, retry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 }
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001326 len = p - (__be32 *)iov->iov_base - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 if (len < 0)
1328 goto out_overflow;
1329 switch ((n = ntohl(*p++))) {
1330 case RPC_SUCCESS:
1331 return p;
1332 case RPC_PROG_UNAVAIL:
Chuck Lever46121cf2007-01-31 12:14:08 -05001333 dprintk("RPC: %5u %s: program %u is unsupported by server %s\n",
1334 task->tk_pid, __FUNCTION__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 (unsigned int)task->tk_client->cl_prog,
1336 task->tk_client->cl_server);
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00001337 error = -EPFNOSUPPORT;
1338 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 case RPC_PROG_MISMATCH:
Chuck Lever46121cf2007-01-31 12:14:08 -05001340 dprintk("RPC: %5u %s: program %u, version %u unsupported by "
1341 "server %s\n", task->tk_pid, __FUNCTION__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 (unsigned int)task->tk_client->cl_prog,
1343 (unsigned int)task->tk_client->cl_vers,
1344 task->tk_client->cl_server);
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00001345 error = -EPROTONOSUPPORT;
1346 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 case RPC_PROC_UNAVAIL:
Chuck Lever46121cf2007-01-31 12:14:08 -05001348 dprintk("RPC: %5u %s: proc %p unsupported by program %u, "
1349 "version %u on server %s\n",
1350 task->tk_pid, __FUNCTION__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 task->tk_msg.rpc_proc,
1352 task->tk_client->cl_prog,
1353 task->tk_client->cl_vers,
1354 task->tk_client->cl_server);
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00001355 error = -EOPNOTSUPP;
1356 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 case RPC_GARBAGE_ARGS:
Chuck Lever46121cf2007-01-31 12:14:08 -05001358 dprintk("RPC: %5u %s: server saw garbage\n",
1359 task->tk_pid, __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 break; /* retry */
1361 default:
1362 printk(KERN_WARNING "call_verify: server accept status: %x\n", n);
1363 /* Also retry */
1364 }
1365
Trond Myklebustabbcf282006-01-03 09:55:03 +01001366out_garbage:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 task->tk_client->cl_stats->rpcgarbage++;
1368 if (task->tk_garb_retry) {
1369 task->tk_garb_retry--;
Chuck Lever46121cf2007-01-31 12:14:08 -05001370 dprintk("RPC: %5u %s: retrying\n",
1371 task->tk_pid, __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 task->tk_action = call_bind;
Trond Myklebustabbcf282006-01-03 09:55:03 +01001373out_retry:
1374 return ERR_PTR(-EAGAIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 }
1376 printk(KERN_WARNING "RPC %s: retry failed, exit EIO\n", __FUNCTION__);
1377out_eio:
1378 error = -EIO;
1379out_err:
1380 rpc_exit(task, error);
Trond Myklebustabbcf282006-01-03 09:55:03 +01001381 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382out_overflow:
1383 printk(KERN_WARNING "RPC %s: server reply was truncated.\n", __FUNCTION__);
Trond Myklebustabbcf282006-01-03 09:55:03 +01001384 goto out_garbage;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385}
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00001386
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001387static int rpcproc_encode_null(void *rqstp, __be32 *data, void *obj)
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00001388{
1389 return 0;
1390}
1391
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001392static int rpcproc_decode_null(void *rqstp, __be32 *data, void *obj)
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00001393{
1394 return 0;
1395}
1396
1397static struct rpc_procinfo rpcproc_null = {
1398 .p_encode = rpcproc_encode_null,
1399 .p_decode = rpcproc_decode_null,
1400};
1401
1402int rpc_ping(struct rpc_clnt *clnt, int flags)
1403{
1404 struct rpc_message msg = {
1405 .rpc_proc = &rpcproc_null,
1406 };
1407 int err;
1408 msg.rpc_cred = authnull_ops.lookup_cred(NULL, NULL, 0);
1409 err = rpc_call_sync(clnt, &msg, flags);
1410 put_rpccred(msg.rpc_cred);
1411 return err;
1412}