blob: 6a6b96e8dcb27499d05a587898b7bf4e3995ca0e [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>
Chuck Lever510deb02007-12-10 14:56:24 -050033#include <linux/in6.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
35#include <linux/sunrpc/clnt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/sunrpc/rpc_pipe_fs.h>
Chuck Lever11c556b2006-03-20 13:44:22 -050037#include <linux/sunrpc/metrics.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#ifdef RPC_DEBUG
41# define RPCDBG_FACILITY RPCDBG_CALL
42#endif
43
Chuck Lever46121cf2007-01-31 12:14:08 -050044#define dprint_status(t) \
45 dprintk("RPC: %5u %s (status %d)\n", t->tk_pid, \
46 __FUNCTION__, t->tk_status)
47
Trond Myklebust188fef12007-06-16 14:18:40 -040048/*
49 * All RPC clients are linked into this list
50 */
51static LIST_HEAD(all_clients);
52static DEFINE_SPINLOCK(rpc_client_lock);
53
Linus Torvalds1da177e2005-04-16 15:20:36 -070054static DECLARE_WAIT_QUEUE_HEAD(destroy_wait);
55
56
57static void call_start(struct rpc_task *task);
58static void call_reserve(struct rpc_task *task);
59static void call_reserveresult(struct rpc_task *task);
60static void call_allocate(struct rpc_task *task);
61static void call_encode(struct rpc_task *task);
62static void call_decode(struct rpc_task *task);
63static void call_bind(struct rpc_task *task);
Chuck Leverda351872005-08-11 16:25:11 -040064static void call_bind_status(struct rpc_task *task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065static void call_transmit(struct rpc_task *task);
66static void call_status(struct rpc_task *task);
Trond Myklebust940e3312005-11-09 21:45:24 -050067static void call_transmit_status(struct rpc_task *task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068static void call_refresh(struct rpc_task *task);
69static void call_refreshresult(struct rpc_task *task);
70static void call_timeout(struct rpc_task *task);
71static void call_connect(struct rpc_task *task);
72static void call_connect_status(struct rpc_task *task);
Alexey Dobriyand8ed0292006-09-26 22:29:38 -070073static __be32 * call_header(struct rpc_task *task);
74static __be32 * call_verify(struct rpc_task *task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Trond Myklebust64c91a12007-06-23 10:17:16 -040076static int rpc_ping(struct rpc_clnt *clnt, int flags);
77
Trond Myklebust188fef12007-06-16 14:18:40 -040078static void rpc_register_client(struct rpc_clnt *clnt)
79{
80 spin_lock(&rpc_client_lock);
81 list_add(&clnt->cl_clients, &all_clients);
82 spin_unlock(&rpc_client_lock);
83}
84
85static void rpc_unregister_client(struct rpc_clnt *clnt)
86{
87 spin_lock(&rpc_client_lock);
88 list_del(&clnt->cl_clients);
89 spin_unlock(&rpc_client_lock);
90}
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92static int
93rpc_setup_pipedir(struct rpc_clnt *clnt, char *dir_name)
94{
Trond Myklebustf1345852005-09-23 11:08:25 -040095 static uint32_t clntid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 int error;
97
Trond Myklebust54281542006-03-20 13:44:49 -050098 clnt->cl_vfsmnt = ERR_PTR(-ENOENT);
99 clnt->cl_dentry = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 if (dir_name == NULL)
101 return 0;
Trond Myklebust54281542006-03-20 13:44:49 -0500102
103 clnt->cl_vfsmnt = rpc_get_mount();
104 if (IS_ERR(clnt->cl_vfsmnt))
105 return PTR_ERR(clnt->cl_vfsmnt);
106
Trond Myklebustf1345852005-09-23 11:08:25 -0400107 for (;;) {
108 snprintf(clnt->cl_pathname, sizeof(clnt->cl_pathname),
109 "%s/clnt%x", dir_name,
110 (unsigned int)clntid++);
111 clnt->cl_pathname[sizeof(clnt->cl_pathname) - 1] = '\0';
112 clnt->cl_dentry = rpc_mkdir(clnt->cl_pathname, clnt);
113 if (!IS_ERR(clnt->cl_dentry))
114 return 0;
Christoph Hellwig278c9952005-07-24 23:53:01 +0100115 error = PTR_ERR(clnt->cl_dentry);
Trond Myklebustf1345852005-09-23 11:08:25 -0400116 if (error != -EEXIST) {
117 printk(KERN_INFO "RPC: Couldn't create pipefs entry %s, error %d\n",
118 clnt->cl_pathname, error);
Trond Myklebust54281542006-03-20 13:44:49 -0500119 rpc_put_mount();
Trond Myklebustf1345852005-09-23 11:08:25 -0400120 return error;
121 }
Christoph Hellwig278c9952005-07-24 23:53:01 +0100122 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123}
124
Trond Myklebust698b6d02007-12-20 16:03:53 -0500125static struct rpc_clnt * rpc_new_client(const struct rpc_create_args *args, struct rpc_xprt *xprt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126{
Trond Myklebust698b6d02007-12-20 16:03:53 -0500127 struct rpc_program *program = args->program;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 struct rpc_version *version;
129 struct rpc_clnt *clnt = NULL;
J. Bruce Fields6a192752005-06-22 17:16:23 +0000130 struct rpc_auth *auth;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 int err;
Chuck Lever06b8d252007-09-11 18:00:20 -0400132 size_t len;
133
134 /* sanity check the name before trying to print it */
135 err = -EINVAL;
Trond Myklebust698b6d02007-12-20 16:03:53 -0500136 len = strlen(args->servername);
Chuck Lever06b8d252007-09-11 18:00:20 -0400137 if (len > RPC_MAXNETNAMELEN)
138 goto out_no_rpciod;
139 len++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Chuck Lever46121cf2007-01-31 12:14:08 -0500141 dprintk("RPC: creating %s client for %s (xprt %p)\n",
Trond Myklebust698b6d02007-12-20 16:03:53 -0500142 program->name, args->servername, xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Trond Myklebust4ada5392007-06-14 17:26:17 -0400144 err = rpciod_up();
145 if (err)
146 goto out_no_rpciod;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 err = -EINVAL;
148 if (!xprt)
Adrian Bunk712917d2006-03-13 21:20:47 -0800149 goto out_no_xprt;
Trond Myklebust698b6d02007-12-20 16:03:53 -0500150
151 if (args->version >= program->nrvers)
152 goto out_err;
153 version = program->version[args->version];
154 if (version == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 goto out_err;
156
157 err = -ENOMEM;
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700158 clnt = kzalloc(sizeof(*clnt), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 if (!clnt)
160 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 clnt->cl_parent = clnt;
162
163 clnt->cl_server = clnt->cl_inline_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 if (len > sizeof(clnt->cl_inline_name)) {
165 char *buf = kmalloc(len, GFP_KERNEL);
Trond Myklebust698b6d02007-12-20 16:03:53 -0500166 if (buf != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 clnt->cl_server = buf;
168 else
169 len = sizeof(clnt->cl_inline_name);
170 }
Trond Myklebust698b6d02007-12-20 16:03:53 -0500171 strlcpy(clnt->cl_server, args->servername, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173 clnt->cl_xprt = xprt;
174 clnt->cl_procinfo = version->procs;
175 clnt->cl_maxproc = version->nrprocs;
176 clnt->cl_protname = program->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 clnt->cl_prog = program->number;
178 clnt->cl_vers = version->number;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 clnt->cl_stats = program->stats;
Chuck Lever11c556b2006-03-20 13:44:22 -0500180 clnt->cl_metrics = rpc_alloc_iostats(clnt);
Trond Myklebust23bf85b2006-11-21 10:40:23 -0500181 err = -ENOMEM;
182 if (clnt->cl_metrics == NULL)
183 goto out_no_stats;
Trond Myklebust3e32a5d2006-11-16 11:37:27 -0500184 clnt->cl_program = program;
Trond Myklebust6529eba2007-06-14 16:40:14 -0400185 INIT_LIST_HEAD(&clnt->cl_tasks);
Trond Myklebust4bef61f2007-06-16 14:17:01 -0400186 spin_lock_init(&clnt->cl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Chuck Leverec739ef2006-08-22 20:06:15 -0400188 if (!xprt_bound(clnt->cl_xprt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 clnt->cl_autobind = 1;
190
191 clnt->cl_rtt = &clnt->cl_rtt_default;
192 rpc_init_rtt(&clnt->cl_rtt_default, xprt->timeout.to_initval);
193
Trond Myklebust34f52e32007-06-14 16:40:31 -0400194 kref_init(&clnt->cl_kref);
195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 err = rpc_setup_pipedir(clnt, program->pipe_dir_name);
197 if (err < 0)
198 goto out_no_path;
199
Trond Myklebust698b6d02007-12-20 16:03:53 -0500200 auth = rpcauth_create(args->authflavor, clnt);
J. Bruce Fields6a192752005-06-22 17:16:23 +0000201 if (IS_ERR(auth)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 printk(KERN_INFO "RPC: Couldn't create auth handle (flavor %u)\n",
Trond Myklebust698b6d02007-12-20 16:03:53 -0500203 args->authflavor);
J. Bruce Fields6a192752005-06-22 17:16:23 +0000204 err = PTR_ERR(auth);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 goto out_no_auth;
206 }
207
208 /* save the nodename */
Serge E. Hallyne9ff3992006-10-02 02:18:11 -0700209 clnt->cl_nodelen = strlen(utsname()->nodename);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 if (clnt->cl_nodelen > UNX_MAXNODENAME)
211 clnt->cl_nodelen = UNX_MAXNODENAME;
Serge E. Hallyne9ff3992006-10-02 02:18:11 -0700212 memcpy(clnt->cl_nodename, utsname()->nodename, clnt->cl_nodelen);
Trond Myklebust6529eba2007-06-14 16:40:14 -0400213 rpc_register_client(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 return clnt;
215
216out_no_auth:
Trond Myklebust54281542006-03-20 13:44:49 -0500217 if (!IS_ERR(clnt->cl_dentry)) {
Trond Myklebustdff02cc2006-07-31 14:17:18 -0700218 rpc_rmdir(clnt->cl_dentry);
Trond Myklebust54281542006-03-20 13:44:49 -0500219 rpc_put_mount();
220 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221out_no_path:
Trond Myklebust23bf85b2006-11-21 10:40:23 -0500222 rpc_free_iostats(clnt->cl_metrics);
223out_no_stats:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 if (clnt->cl_server != clnt->cl_inline_name)
225 kfree(clnt->cl_server);
226 kfree(clnt);
227out_err:
Trond Myklebust6b6ca862006-09-05 12:55:57 -0400228 xprt_put(xprt);
Adrian Bunk712917d2006-03-13 21:20:47 -0800229out_no_xprt:
Trond Myklebust4ada5392007-06-14 17:26:17 -0400230 rpciod_down();
231out_no_rpciod:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 return ERR_PTR(err);
233}
234
Chuck Leverc2866762006-08-22 20:06:20 -0400235/*
236 * rpc_create - create an RPC client and transport with one call
237 * @args: rpc_clnt create argument structure
238 *
239 * Creates and initializes an RPC transport and an RPC client.
240 *
241 * It can ping the server in order to determine if it is up, and to see if
242 * it supports this program and version. RPC_CLNT_CREATE_NOPING disables
243 * this behavior so asynchronous tasks can also use rpc_create.
244 */
245struct rpc_clnt *rpc_create(struct rpc_create_args *args)
246{
247 struct rpc_xprt *xprt;
248 struct rpc_clnt *clnt;
\"Talpey, Thomas\3c341b02007-09-10 13:47:07 -0400249 struct xprt_create xprtargs = {
\"Talpey, Thomas\4fa016e2007-09-10 13:47:57 -0400250 .ident = args->protocol,
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +0200251 .srcaddr = args->saddress,
Frank van Maarseveen96802a02007-07-08 13:08:54 +0200252 .dstaddr = args->address,
253 .addrlen = args->addrsize,
254 .timeout = args->timeout
255 };
Chuck Lever510deb02007-12-10 14:56:24 -0500256 char servername[48];
Chuck Leverc2866762006-08-22 20:06:20 -0400257
Frank van Maarseveen96802a02007-07-08 13:08:54 +0200258 xprt = xprt_create_transport(&xprtargs);
Chuck Leverc2866762006-08-22 20:06:20 -0400259 if (IS_ERR(xprt))
260 return (struct rpc_clnt *)xprt;
261
262 /*
Chuck Lever43780b82007-07-01 12:13:22 -0400263 * If the caller chooses not to specify a hostname, whip
264 * up a string representation of the passed-in address.
265 */
266 if (args->servername == NULL) {
Chuck Lever510deb02007-12-10 14:56:24 -0500267 servername[0] = '\0';
268 switch (args->address->sa_family) {
269 case AF_INET: {
270 struct sockaddr_in *sin =
271 (struct sockaddr_in *)args->address;
272 snprintf(servername, sizeof(servername), NIPQUAD_FMT,
273 NIPQUAD(sin->sin_addr.s_addr));
274 break;
275 }
276 case AF_INET6: {
277 struct sockaddr_in6 *sin =
278 (struct sockaddr_in6 *)args->address;
279 snprintf(servername, sizeof(servername), NIP6_FMT,
280 NIP6(sin->sin6_addr));
281 break;
282 }
283 default:
284 /* caller wants default server name, but
285 * address family isn't recognized. */
286 return ERR_PTR(-EINVAL);
287 }
Chuck Lever43780b82007-07-01 12:13:22 -0400288 args->servername = servername;
289 }
290
Chuck Lever510deb02007-12-10 14:56:24 -0500291 xprt = xprt_create_transport(&xprtargs);
292 if (IS_ERR(xprt))
293 return (struct rpc_clnt *)xprt;
294
Chuck Lever43780b82007-07-01 12:13:22 -0400295 /*
Chuck Leverc2866762006-08-22 20:06:20 -0400296 * By default, kernel RPC client connects from a reserved port.
297 * CAP_NET_BIND_SERVICE will not be set for unprivileged requesters,
298 * but it is always enabled for rpciod, which handles the connect
299 * operation.
300 */
301 xprt->resvport = 1;
302 if (args->flags & RPC_CLNT_CREATE_NONPRIVPORT)
303 xprt->resvport = 0;
304
Trond Myklebust698b6d02007-12-20 16:03:53 -0500305 clnt = rpc_new_client(args, xprt);
Chuck Leverc2866762006-08-22 20:06:20 -0400306 if (IS_ERR(clnt))
307 return clnt;
308
309 if (!(args->flags & RPC_CLNT_CREATE_NOPING)) {
310 int err = rpc_ping(clnt, RPC_TASK_SOFT|RPC_TASK_NOINTR);
311 if (err != 0) {
312 rpc_shutdown_client(clnt);
313 return ERR_PTR(err);
314 }
315 }
316
317 clnt->cl_softrtry = 1;
318 if (args->flags & RPC_CLNT_CREATE_HARDRTRY)
319 clnt->cl_softrtry = 0;
320
321 if (args->flags & RPC_CLNT_CREATE_INTR)
322 clnt->cl_intr = 1;
323 if (args->flags & RPC_CLNT_CREATE_AUTOBIND)
324 clnt->cl_autobind = 1;
Chuck Lever43d78ef2007-02-06 18:26:11 -0500325 if (args->flags & RPC_CLNT_CREATE_DISCRTRY)
326 clnt->cl_discrtry = 1;
Chuck Leverc2866762006-08-22 20:06:20 -0400327
328 return clnt;
329}
Chuck Leverb86acd52006-08-22 20:06:22 -0400330EXPORT_SYMBOL_GPL(rpc_create);
Chuck Leverc2866762006-08-22 20:06:20 -0400331
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332/*
333 * This function clones the RPC client structure. It allows us to share the
334 * same transport while varying parameters such as the authentication
335 * flavour.
336 */
337struct rpc_clnt *
338rpc_clone_client(struct rpc_clnt *clnt)
339{
340 struct rpc_clnt *new;
Trond Myklebust3e32a5d2006-11-16 11:37:27 -0500341 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
Arnaldo Carvalho de Meloe69062b2006-11-21 01:21:34 -0200343 new = kmemdup(clnt, sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 if (!new)
345 goto out_no_clnt;
Trond Myklebustd431a5552007-06-17 17:07:54 -0400346 new->cl_parent = clnt;
347 /* Turn off autobind on clones */
348 new->cl_autobind = 0;
349 INIT_LIST_HEAD(&new->cl_tasks);
350 spin_lock_init(&new->cl_lock);
351 rpc_init_rtt(&new->cl_rtt_default, clnt->cl_xprt->timeout.to_initval);
Trond Myklebust23bf85b2006-11-21 10:40:23 -0500352 new->cl_metrics = rpc_alloc_iostats(clnt);
353 if (new->cl_metrics == NULL)
354 goto out_no_stats;
Trond Myklebust34f52e32007-06-14 16:40:31 -0400355 kref_init(&new->cl_kref);
Trond Myklebust3e32a5d2006-11-16 11:37:27 -0500356 err = rpc_setup_pipedir(new, clnt->cl_program->pipe_dir_name);
357 if (err != 0)
358 goto out_no_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 if (new->cl_auth)
360 atomic_inc(&new->cl_auth->au_count);
Trond Myklebustd431a5552007-06-17 17:07:54 -0400361 xprt_get(clnt->cl_xprt);
362 kref_get(&clnt->cl_kref);
Trond Myklebust6529eba2007-06-14 16:40:14 -0400363 rpc_register_client(new);
Trond Myklebust4ada5392007-06-14 17:26:17 -0400364 rpciod_up();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 return new;
Trond Myklebust3e32a5d2006-11-16 11:37:27 -0500366out_no_path:
367 rpc_free_iostats(new->cl_metrics);
Trond Myklebust23bf85b2006-11-21 10:40:23 -0500368out_no_stats:
369 kfree(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370out_no_clnt:
Chuck Lever46121cf2007-01-31 12:14:08 -0500371 dprintk("RPC: %s: returned error %d\n", __FUNCTION__, err);
Trond Myklebust3e32a5d2006-11-16 11:37:27 -0500372 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400374EXPORT_SYMBOL_GPL(rpc_clone_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
376/*
377 * Properly shut down an RPC client, terminating all outstanding
Trond Myklebust90c57552007-06-09 19:49:36 -0400378 * requests.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 */
Trond Myklebust4c402b42007-06-14 16:40:32 -0400380void rpc_shutdown_client(struct rpc_clnt *clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381{
Trond Myklebust34f52e32007-06-14 16:40:31 -0400382 dprintk("RPC: shutting down %s client for %s\n",
383 clnt->cl_protname, clnt->cl_server);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
Trond Myklebust34f52e32007-06-14 16:40:31 -0400385 while (!list_empty(&clnt->cl_tasks)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 rpc_killall_tasks(clnt);
Ingo Molnar532347e2006-01-09 20:52:53 -0800387 wait_event_timeout(destroy_wait,
Trond Myklebust34f52e32007-06-14 16:40:31 -0400388 list_empty(&clnt->cl_tasks), 1*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 }
390
Trond Myklebust4c402b42007-06-14 16:40:32 -0400391 rpc_release_client(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400393EXPORT_SYMBOL_GPL(rpc_shutdown_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
395/*
Trond Myklebust34f52e32007-06-14 16:40:31 -0400396 * Free an RPC client
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 */
Trond Myklebust34f52e32007-06-14 16:40:31 -0400398static void
399rpc_free_client(struct kref *kref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400{
Trond Myklebust34f52e32007-06-14 16:40:31 -0400401 struct rpc_clnt *clnt = container_of(kref, struct rpc_clnt, cl_kref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Chuck Lever46121cf2007-01-31 12:14:08 -0500403 dprintk("RPC: destroying %s client for %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 clnt->cl_protname, clnt->cl_server);
Trond Myklebust8f8e7a52006-08-14 13:11:15 -0400405 if (!IS_ERR(clnt->cl_dentry)) {
Trond Myklebustdff02cc2006-07-31 14:17:18 -0700406 rpc_rmdir(clnt->cl_dentry);
Trond Myklebust8f8e7a52006-08-14 13:11:15 -0400407 rpc_put_mount();
408 }
Trond Myklebust3e32a5d2006-11-16 11:37:27 -0500409 if (clnt->cl_parent != clnt) {
Trond Myklebust8ad7c892007-06-14 16:40:32 -0400410 rpc_release_client(clnt->cl_parent);
Trond Myklebust3e32a5d2006-11-16 11:37:27 -0500411 goto out_free;
412 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 if (clnt->cl_server != clnt->cl_inline_name)
414 kfree(clnt->cl_server);
415out_free:
Trond Myklebust6529eba2007-06-14 16:40:14 -0400416 rpc_unregister_client(clnt);
Chuck Lever11c556b2006-03-20 13:44:22 -0500417 rpc_free_iostats(clnt->cl_metrics);
418 clnt->cl_metrics = NULL;
Trond Myklebust6b6ca862006-09-05 12:55:57 -0400419 xprt_put(clnt->cl_xprt);
Trond Myklebust4ada5392007-06-14 17:26:17 -0400420 rpciod_down();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 kfree(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422}
423
424/*
Trond Myklebust1dd17ec2007-06-26 16:57:41 -0400425 * Free an RPC client
426 */
427static void
428rpc_free_auth(struct kref *kref)
429{
430 struct rpc_clnt *clnt = container_of(kref, struct rpc_clnt, cl_kref);
431
432 if (clnt->cl_auth == NULL) {
433 rpc_free_client(kref);
434 return;
435 }
436
437 /*
438 * Note: RPCSEC_GSS may need to send NULL RPC calls in order to
439 * release remaining GSS contexts. This mechanism ensures
440 * that it can do so safely.
441 */
442 kref_init(kref);
443 rpcauth_release(clnt->cl_auth);
444 clnt->cl_auth = NULL;
445 kref_put(kref, rpc_free_client);
446}
447
448/*
Trond Myklebust34f52e32007-06-14 16:40:31 -0400449 * Release reference to the RPC client
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 */
451void
452rpc_release_client(struct rpc_clnt *clnt)
453{
Trond Myklebust34f52e32007-06-14 16:40:31 -0400454 dprintk("RPC: rpc_release_client(%p)\n", clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Trond Myklebust34f52e32007-06-14 16:40:31 -0400456 if (list_empty(&clnt->cl_tasks))
457 wake_up(&destroy_wait);
Trond Myklebust1dd17ec2007-06-26 16:57:41 -0400458 kref_put(&clnt->cl_kref, rpc_free_auth);
Trond Myklebust34f52e32007-06-14 16:40:31 -0400459}
460
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000461/**
462 * rpc_bind_new_program - bind a new RPC program to an existing client
463 * @old - old rpc_client
464 * @program - rpc program to set
465 * @vers - rpc program version
466 *
467 * Clones the rpc client and sets up a new RPC program. This is mainly
468 * of use for enabling different RPC programs to share the same transport.
469 * The Sun NFSv2/v3 ACL protocol can do this.
470 */
471struct rpc_clnt *rpc_bind_new_program(struct rpc_clnt *old,
472 struct rpc_program *program,
Chuck Lever89eb21c2007-09-11 18:00:09 -0400473 u32 vers)
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000474{
475 struct rpc_clnt *clnt;
476 struct rpc_version *version;
477 int err;
478
479 BUG_ON(vers >= program->nrvers || !program->version[vers]);
480 version = program->version[vers];
481 clnt = rpc_clone_client(old);
482 if (IS_ERR(clnt))
483 goto out;
484 clnt->cl_procinfo = version->procs;
485 clnt->cl_maxproc = version->nrprocs;
486 clnt->cl_protname = program->name;
487 clnt->cl_prog = program->number;
488 clnt->cl_vers = version->number;
489 clnt->cl_stats = program->stats;
490 err = rpc_ping(clnt, RPC_TASK_SOFT|RPC_TASK_NOINTR);
491 if (err != 0) {
492 rpc_shutdown_client(clnt);
493 clnt = ERR_PTR(err);
494 }
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800495out:
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000496 return clnt;
497}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400498EXPORT_SYMBOL_GPL(rpc_bind_new_program);
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000499
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500/*
501 * Default callback for async RPC calls
502 */
503static void
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100504rpc_default_callback(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505{
506}
507
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100508static const struct rpc_call_ops rpc_default_ops = {
509 .rpc_call_done = rpc_default_callback,
510};
511
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512/*
Trond Myklebust14b218a2005-06-22 17:16:28 +0000513 * Export the signal mask handling for synchronous code that
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 * sleeps on RPC calls
515 */
Trond Myklebust2bd61572006-01-03 09:55:19 +0100516#define RPC_INTR_SIGNALS (sigmask(SIGHUP) | sigmask(SIGINT) | sigmask(SIGQUIT) | sigmask(SIGTERM))
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800517
Trond Myklebust14b218a2005-06-22 17:16:28 +0000518static void rpc_save_sigmask(sigset_t *oldset, int intr)
519{
Trond Myklebust2bd61572006-01-03 09:55:19 +0100520 unsigned long sigallow = sigmask(SIGKILL);
Trond Myklebust14b218a2005-06-22 17:16:28 +0000521 sigset_t sigmask;
522
523 /* Block all signals except those listed in sigallow */
524 if (intr)
525 sigallow |= RPC_INTR_SIGNALS;
526 siginitsetinv(&sigmask, sigallow);
527 sigprocmask(SIG_BLOCK, &sigmask, oldset);
528}
529
Trond Myklebust50859252007-10-25 18:19:37 -0400530static void rpc_task_sigmask(struct rpc_task *task, sigset_t *oldset)
Trond Myklebust14b218a2005-06-22 17:16:28 +0000531{
532 rpc_save_sigmask(oldset, !RPC_TASK_UNINTERRUPTIBLE(task));
533}
534
Trond Myklebust50859252007-10-25 18:19:37 -0400535static void rpc_restore_sigmask(sigset_t *oldset)
Trond Myklebust14b218a2005-06-22 17:16:28 +0000536{
537 sigprocmask(SIG_SETMASK, oldset, NULL);
538}
539
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540void rpc_clnt_sigmask(struct rpc_clnt *clnt, sigset_t *oldset)
541{
Trond Myklebust14b218a2005-06-22 17:16:28 +0000542 rpc_save_sigmask(oldset, clnt->cl_intr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400544EXPORT_SYMBOL_GPL(rpc_clnt_sigmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
546void rpc_clnt_sigunmask(struct rpc_clnt *clnt, sigset_t *oldset)
547{
Trond Myklebust14b218a2005-06-22 17:16:28 +0000548 rpc_restore_sigmask(oldset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400550EXPORT_SYMBOL_GPL(rpc_clnt_sigunmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
Trond Myklebustc970aa82007-07-14 15:39:59 -0400552/**
553 * rpc_run_task - Allocate a new RPC task, then run rpc_execute against it
554 * @task_setup_data: pointer to task initialisation data
555 */
556struct rpc_task *rpc_run_task(const struct rpc_task_setup *task_setup_data)
Trond Myklebust6e5b70e2007-06-12 10:02:37 -0400557{
558 struct rpc_task *task, *ret;
559 sigset_t oldset;
560
Trond Myklebust84115e12007-07-14 15:39:59 -0400561 task = rpc_new_task(task_setup_data);
Trond Myklebust6e5b70e2007-06-12 10:02:37 -0400562 if (task == NULL) {
Trond Myklebust84115e12007-07-14 15:39:59 -0400563 rpc_release_calldata(task_setup_data->callback_ops,
564 task_setup_data->callback_data);
Trond Myklebust50859252007-10-25 18:19:37 -0400565 ret = ERR_PTR(-ENOMEM);
566 goto out;
Trond Myklebust6e5b70e2007-06-12 10:02:37 -0400567 }
568
Trond Myklebustb3ef8b32007-10-25 18:32:34 -0400569 if (task->tk_status != 0) {
570 ret = ERR_PTR(task->tk_status);
571 rpc_put_task(task);
572 goto out;
Trond Myklebust6e5b70e2007-06-12 10:02:37 -0400573 }
574 atomic_inc(&task->tk_count);
Trond Myklebust50859252007-10-25 18:19:37 -0400575 /* Mask signals on synchronous RPC calls and RPCSEC_GSS upcalls */
576 rpc_task_sigmask(task, &oldset);
Trond Myklebust6e5b70e2007-06-12 10:02:37 -0400577 rpc_execute(task);
Trond Myklebust50859252007-10-25 18:19:37 -0400578 rpc_restore_sigmask(&oldset);
Trond Myklebust6e5b70e2007-06-12 10:02:37 -0400579 ret = task;
580out:
Trond Myklebust6e5b70e2007-06-12 10:02:37 -0400581 return ret;
582}
Trond Myklebustc970aa82007-07-14 15:39:59 -0400583EXPORT_SYMBOL_GPL(rpc_run_task);
Trond Myklebust6e5b70e2007-06-12 10:02:37 -0400584
585/**
586 * rpc_call_sync - Perform a synchronous RPC call
587 * @clnt: pointer to RPC client
588 * @msg: RPC call parameters
589 * @flags: RPC call flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 */
591int rpc_call_sync(struct rpc_clnt *clnt, struct rpc_message *msg, int flags)
592{
593 struct rpc_task *task;
Trond Myklebust84115e12007-07-14 15:39:59 -0400594 struct rpc_task_setup task_setup_data = {
595 .rpc_client = clnt,
596 .rpc_message = msg,
597 .callback_ops = &rpc_default_ops,
598 .flags = flags,
599 };
Trond Myklebust6e5b70e2007-06-12 10:02:37 -0400600 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 BUG_ON(flags & RPC_TASK_ASYNC);
603
Trond Myklebustc970aa82007-07-14 15:39:59 -0400604 task = rpc_run_task(&task_setup_data);
Trond Myklebust6e5b70e2007-06-12 10:02:37 -0400605 if (IS_ERR(task))
606 return PTR_ERR(task);
Trond Myklebuste60859a2006-01-03 09:55:10 +0100607 status = task->tk_status;
Trond Myklebustbde8f002007-01-24 11:54:53 -0800608 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 return status;
610}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400611EXPORT_SYMBOL_GPL(rpc_call_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
Trond Myklebust6e5b70e2007-06-12 10:02:37 -0400613/**
614 * rpc_call_async - Perform an asynchronous RPC call
615 * @clnt: pointer to RPC client
616 * @msg: RPC call parameters
617 * @flags: RPC call flags
618 * @ops: RPC call ops
619 * @data: user call data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 */
621int
622rpc_call_async(struct rpc_clnt *clnt, struct rpc_message *msg, int flags,
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100623 const struct rpc_call_ops *tk_ops, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624{
625 struct rpc_task *task;
Trond Myklebust84115e12007-07-14 15:39:59 -0400626 struct rpc_task_setup task_setup_data = {
627 .rpc_client = clnt,
628 .rpc_message = msg,
629 .callback_ops = tk_ops,
630 .callback_data = data,
631 .flags = flags|RPC_TASK_ASYNC,
632 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
Trond Myklebustc970aa82007-07-14 15:39:59 -0400634 task = rpc_run_task(&task_setup_data);
Trond Myklebust6e5b70e2007-06-12 10:02:37 -0400635 if (IS_ERR(task))
636 return PTR_ERR(task);
637 rpc_put_task(task);
638 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400640EXPORT_SYMBOL_GPL(rpc_call_async);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642void
Trond Myklebust77de2c52007-10-25 18:40:21 -0400643rpc_call_start(struct rpc_task *task)
644{
645 task->tk_action = call_start;
646}
647EXPORT_SYMBOL_GPL(rpc_call_start);
648
Chuck Levered394402006-08-22 20:06:17 -0400649/**
650 * rpc_peeraddr - extract remote peer address from clnt's xprt
651 * @clnt: RPC client structure
652 * @buf: target buffer
653 * @size: length of target buffer
654 *
655 * Returns the number of bytes that are actually in the stored address.
656 */
657size_t rpc_peeraddr(struct rpc_clnt *clnt, struct sockaddr *buf, size_t bufsize)
658{
659 size_t bytes;
660 struct rpc_xprt *xprt = clnt->cl_xprt;
661
662 bytes = sizeof(xprt->addr);
663 if (bytes > bufsize)
664 bytes = bufsize;
665 memcpy(buf, &clnt->cl_xprt->addr, bytes);
Chuck Leverc4efcb12006-08-22 20:06:19 -0400666 return xprt->addrlen;
Chuck Levered394402006-08-22 20:06:17 -0400667}
Chuck Leverb86acd52006-08-22 20:06:22 -0400668EXPORT_SYMBOL_GPL(rpc_peeraddr);
Chuck Levered394402006-08-22 20:06:17 -0400669
Chuck Leverf425eba2006-08-22 20:06:18 -0400670/**
671 * rpc_peeraddr2str - return remote peer address in printable format
672 * @clnt: RPC client structure
673 * @format: address format
674 *
675 */
676char *rpc_peeraddr2str(struct rpc_clnt *clnt, enum rpc_display_format_t format)
677{
678 struct rpc_xprt *xprt = clnt->cl_xprt;
Chuck Lever7559c7a2006-12-05 16:35:37 -0500679
680 if (xprt->address_strings[format] != NULL)
681 return xprt->address_strings[format];
682 else
683 return "unprintable";
Chuck Leverf425eba2006-08-22 20:06:18 -0400684}
Chuck Leverb86acd52006-08-22 20:06:22 -0400685EXPORT_SYMBOL_GPL(rpc_peeraddr2str);
Chuck Leverf425eba2006-08-22 20:06:18 -0400686
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687void
688rpc_setbufsize(struct rpc_clnt *clnt, unsigned int sndsize, unsigned int rcvsize)
689{
690 struct rpc_xprt *xprt = clnt->cl_xprt;
Chuck Lever470056c2005-08-25 16:25:56 -0700691 if (xprt->ops->set_buffer_size)
692 xprt->ops->set_buffer_size(xprt, sndsize, rcvsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400694EXPORT_SYMBOL_GPL(rpc_setbufsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
696/*
697 * Return size of largest payload RPC client can support, in bytes
698 *
699 * For stream transports, this is one RPC record fragment (see RFC
700 * 1831), as we don't support multi-record requests yet. For datagram
701 * transports, this is the size of an IP packet minus the IP, UDP, and
702 * RPC header sizes.
703 */
704size_t rpc_max_payload(struct rpc_clnt *clnt)
705{
706 return clnt->cl_xprt->max_payload;
707}
Chuck Leverb86acd52006-08-22 20:06:22 -0400708EXPORT_SYMBOL_GPL(rpc_max_payload);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
Chuck Lever35f5a422006-01-03 09:55:50 +0100710/**
711 * rpc_force_rebind - force transport to check that remote port is unchanged
712 * @clnt: client to rebind
713 *
714 */
715void rpc_force_rebind(struct rpc_clnt *clnt)
716{
717 if (clnt->cl_autobind)
Chuck Leverec739ef2006-08-22 20:06:15 -0400718 xprt_clear_bound(clnt->cl_xprt);
Chuck Lever35f5a422006-01-03 09:55:50 +0100719}
Chuck Leverb86acd52006-08-22 20:06:22 -0400720EXPORT_SYMBOL_GPL(rpc_force_rebind);
Chuck Lever35f5a422006-01-03 09:55:50 +0100721
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722/*
723 * Restart an (async) RPC call. Usually called from within the
724 * exit handler.
725 */
726void
727rpc_restart_call(struct rpc_task *task)
728{
729 if (RPC_ASSASSINATED(task))
730 return;
731
732 task->tk_action = call_start;
733}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400734EXPORT_SYMBOL_GPL(rpc_restart_call);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
736/*
737 * 0. Initial state
738 *
739 * Other FSM states can be visited zero or more times, but
740 * this state is visited exactly once for each RPC.
741 */
742static void
743call_start(struct rpc_task *task)
744{
745 struct rpc_clnt *clnt = task->tk_client;
746
Chuck Lever46121cf2007-01-31 12:14:08 -0500747 dprintk("RPC: %5u call_start %s%d proc %d (%s)\n", task->tk_pid,
748 clnt->cl_protname, clnt->cl_vers,
749 task->tk_msg.rpc_proc->p_proc,
750 (RPC_IS_ASYNC(task) ? "async" : "sync"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
752 /* Increment call count */
753 task->tk_msg.rpc_proc->p_count++;
754 clnt->cl_stats->rpccnt++;
755 task->tk_action = call_reserve;
756}
757
758/*
759 * 1. Reserve an RPC call slot
760 */
761static void
762call_reserve(struct rpc_task *task)
763{
Chuck Lever46121cf2007-01-31 12:14:08 -0500764 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
766 if (!rpcauth_uptodatecred(task)) {
767 task->tk_action = call_refresh;
768 return;
769 }
770
771 task->tk_status = 0;
772 task->tk_action = call_reserveresult;
773 xprt_reserve(task);
774}
775
776/*
777 * 1b. Grok the result of xprt_reserve()
778 */
779static void
780call_reserveresult(struct rpc_task *task)
781{
782 int status = task->tk_status;
783
Chuck Lever46121cf2007-01-31 12:14:08 -0500784 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
786 /*
787 * After a call to xprt_reserve(), we must have either
788 * a request slot or else an error status.
789 */
790 task->tk_status = 0;
791 if (status >= 0) {
792 if (task->tk_rqstp) {
793 task->tk_action = call_allocate;
794 return;
795 }
796
797 printk(KERN_ERR "%s: status=%d, but no request slot, exiting\n",
798 __FUNCTION__, status);
799 rpc_exit(task, -EIO);
800 return;
801 }
802
803 /*
804 * Even though there was an error, we may have acquired
805 * a request slot somehow. Make sure not to leak it.
806 */
807 if (task->tk_rqstp) {
808 printk(KERN_ERR "%s: status=%d, request allocated anyway\n",
809 __FUNCTION__, status);
810 xprt_release(task);
811 }
812
813 switch (status) {
814 case -EAGAIN: /* woken up; retry */
815 task->tk_action = call_reserve;
816 return;
817 case -EIO: /* probably a shutdown */
818 break;
819 default:
820 printk(KERN_ERR "%s: unrecognized error %d, exiting\n",
821 __FUNCTION__, status);
822 break;
823 }
824 rpc_exit(task, status);
825}
826
827/*
828 * 2. Allocate the buffer. For details, see sched.c:rpc_malloc.
Chuck Lever02107142006-01-03 09:55:49 +0100829 * (Note: buffer memory is freed in xprt_release).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 */
831static void
832call_allocate(struct rpc_task *task)
833{
Trond Myklebust1be27f32007-06-27 14:29:04 -0400834 unsigned int slack = task->tk_msg.rpc_cred->cr_auth->au_cslack;
Chuck Lever02107142006-01-03 09:55:49 +0100835 struct rpc_rqst *req = task->tk_rqstp;
836 struct rpc_xprt *xprt = task->tk_xprt;
Chuck Lever2bea90d2007-03-29 16:47:53 -0400837 struct rpc_procinfo *proc = task->tk_msg.rpc_proc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
Chuck Lever46121cf2007-01-31 12:14:08 -0500839 dprint_status(task);
840
Chuck Lever2bea90d2007-03-29 16:47:53 -0400841 task->tk_status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 task->tk_action = call_bind;
Chuck Lever2bea90d2007-03-29 16:47:53 -0400843
Chuck Lever02107142006-01-03 09:55:49 +0100844 if (req->rq_buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 return;
846
Chuck Lever2bea90d2007-03-29 16:47:53 -0400847 if (proc->p_proc != 0) {
848 BUG_ON(proc->p_arglen == 0);
849 if (proc->p_decode != NULL)
850 BUG_ON(proc->p_replen == 0);
851 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
Chuck Lever2bea90d2007-03-29 16:47:53 -0400853 /*
854 * Calculate the size (in quads) of the RPC call
855 * and reply headers, and convert both values
856 * to byte sizes.
857 */
858 req->rq_callsize = RPC_CALLHDRSIZE + (slack << 1) + proc->p_arglen;
859 req->rq_callsize <<= 2;
860 req->rq_rcvsize = RPC_REPHDRSIZE + slack + proc->p_replen;
861 req->rq_rcvsize <<= 2;
862
Chuck Leverc5a4dd82007-03-29 16:47:58 -0400863 req->rq_buffer = xprt->ops->buf_alloc(task,
864 req->rq_callsize + req->rq_rcvsize);
Chuck Lever2bea90d2007-03-29 16:47:53 -0400865 if (req->rq_buffer != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 return;
Chuck Lever46121cf2007-01-31 12:14:08 -0500867
868 dprintk("RPC: %5u rpc_buffer allocation failed\n", task->tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
Trond Myklebust14b218a2005-06-22 17:16:28 +0000870 if (RPC_IS_ASYNC(task) || !signalled()) {
Trond Myklebustb6e9c712007-10-01 12:06:44 -0400871 task->tk_action = call_allocate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 rpc_delay(task, HZ>>4);
873 return;
874 }
875
876 rpc_exit(task, -ERESTARTSYS);
877}
878
Trond Myklebust940e3312005-11-09 21:45:24 -0500879static inline int
880rpc_task_need_encode(struct rpc_task *task)
881{
882 return task->tk_rqstp->rq_snd_buf.len == 0;
883}
884
885static inline void
886rpc_task_force_reencode(struct rpc_task *task)
887{
888 task->tk_rqstp->rq_snd_buf.len = 0;
889}
890
Chuck Lever2bea90d2007-03-29 16:47:53 -0400891static inline void
892rpc_xdr_buf_init(struct xdr_buf *buf, void *start, size_t len)
893{
894 buf->head[0].iov_base = start;
895 buf->head[0].iov_len = len;
896 buf->tail[0].iov_len = 0;
897 buf->page_len = 0;
\"Talpey, Thomas\4f22ccc2007-09-10 13:44:58 -0400898 buf->flags = 0;
Chuck Lever2bea90d2007-03-29 16:47:53 -0400899 buf->len = 0;
900 buf->buflen = len;
901}
902
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903/*
904 * 3. Encode arguments of an RPC call
905 */
906static void
907call_encode(struct rpc_task *task)
908{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 struct rpc_rqst *req = task->tk_rqstp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 kxdrproc_t encode;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700911 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
Chuck Lever46121cf2007-01-31 12:14:08 -0500913 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
Chuck Lever2bea90d2007-03-29 16:47:53 -0400915 rpc_xdr_buf_init(&req->rq_snd_buf,
916 req->rq_buffer,
917 req->rq_callsize);
918 rpc_xdr_buf_init(&req->rq_rcv_buf,
919 (char *)req->rq_buffer + req->rq_callsize,
920 req->rq_rcvsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
922 /* Encode header and provided arguments */
923 encode = task->tk_msg.rpc_proc->p_encode;
924 if (!(p = call_header(task))) {
925 printk(KERN_INFO "RPC: call_header failed, exit EIO\n");
926 rpc_exit(task, -EIO);
927 return;
928 }
J. Bruce Fieldsf3680312005-10-13 16:54:48 -0400929 if (encode == NULL)
930 return;
931
932 task->tk_status = rpcauth_wrap_req(task, encode, req, p,
933 task->tk_msg.rpc_argp);
934 if (task->tk_status == -ENOMEM) {
935 /* XXX: Is this sane? */
936 rpc_delay(task, 3*HZ);
937 task->tk_status = -EAGAIN;
938 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939}
940
941/*
942 * 4. Get the server port number if not yet set
943 */
944static void
945call_bind(struct rpc_task *task)
946{
Chuck Leverec739ef2006-08-22 20:06:15 -0400947 struct rpc_xprt *xprt = task->tk_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
Chuck Lever46121cf2007-01-31 12:14:08 -0500949 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
Chuck Leverda351872005-08-11 16:25:11 -0400951 task->tk_action = call_connect;
Chuck Leverec739ef2006-08-22 20:06:15 -0400952 if (!xprt_bound(xprt)) {
Chuck Leverda351872005-08-11 16:25:11 -0400953 task->tk_action = call_bind_status;
Chuck Leverec739ef2006-08-22 20:06:15 -0400954 task->tk_timeout = xprt->bind_timeout;
Chuck Leverbbf7c1d2006-08-22 20:06:16 -0400955 xprt->ops->rpcbind(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 }
957}
958
959/*
Chuck Leverda351872005-08-11 16:25:11 -0400960 * 4a. Sort out bind result
961 */
962static void
963call_bind_status(struct rpc_task *task)
964{
Chuck Lever906462a2007-09-11 18:00:47 -0400965 int status = -EIO;
Chuck Leverda351872005-08-11 16:25:11 -0400966
967 if (task->tk_status >= 0) {
Chuck Lever46121cf2007-01-31 12:14:08 -0500968 dprint_status(task);
Chuck Leverda351872005-08-11 16:25:11 -0400969 task->tk_status = 0;
970 task->tk_action = call_connect;
971 return;
972 }
973
974 switch (task->tk_status) {
Chuck Lever2429cbf2007-09-11 18:00:41 -0400975 case -EAGAIN:
976 dprintk("RPC: %5u rpcbind waiting for another request "
977 "to finish\n", task->tk_pid);
978 /* avoid busy-waiting here -- could be a network outage. */
979 rpc_delay(task, 5*HZ);
980 goto retry_timeout;
Chuck Leverda351872005-08-11 16:25:11 -0400981 case -EACCES:
Chuck Lever46121cf2007-01-31 12:14:08 -0500982 dprintk("RPC: %5u remote rpcbind: RPC program/version "
983 "unavailable\n", task->tk_pid);
Chuck Leverb79dc8c2007-09-11 18:00:52 -0400984 /* fail immediately if this is an RPC ping */
985 if (task->tk_msg.rpc_proc->p_proc == 0) {
986 status = -EOPNOTSUPP;
987 break;
988 }
Chuck Leverea635a52005-10-06 23:12:58 -0400989 rpc_delay(task, 3*HZ);
Trond Myklebustda458282006-08-31 15:44:52 -0400990 goto retry_timeout;
Chuck Leverda351872005-08-11 16:25:11 -0400991 case -ETIMEDOUT:
Chuck Lever46121cf2007-01-31 12:14:08 -0500992 dprintk("RPC: %5u rpcbind request timed out\n",
Chuck Leverda351872005-08-11 16:25:11 -0400993 task->tk_pid);
Trond Myklebustda458282006-08-31 15:44:52 -0400994 goto retry_timeout;
Chuck Leverda351872005-08-11 16:25:11 -0400995 case -EPFNOSUPPORT:
Chuck Lever906462a2007-09-11 18:00:47 -0400996 /* server doesn't support any rpcbind version we know of */
Chuck Lever46121cf2007-01-31 12:14:08 -0500997 dprintk("RPC: %5u remote rpcbind service unavailable\n",
Chuck Leverda351872005-08-11 16:25:11 -0400998 task->tk_pid);
999 break;
1000 case -EPROTONOSUPPORT:
Chuck Lever00a6e7b2007-03-29 16:48:33 -04001001 dprintk("RPC: %5u remote rpcbind version unavailable, retrying\n",
Chuck Leverda351872005-08-11 16:25:11 -04001002 task->tk_pid);
Chuck Lever00a6e7b2007-03-29 16:48:33 -04001003 task->tk_status = 0;
1004 task->tk_action = call_bind;
1005 return;
Chuck Leverda351872005-08-11 16:25:11 -04001006 default:
Chuck Lever46121cf2007-01-31 12:14:08 -05001007 dprintk("RPC: %5u unrecognized rpcbind error (%d)\n",
Chuck Leverda351872005-08-11 16:25:11 -04001008 task->tk_pid, -task->tk_status);
Chuck Leverda351872005-08-11 16:25:11 -04001009 }
1010
1011 rpc_exit(task, status);
1012 return;
1013
Trond Myklebustda458282006-08-31 15:44:52 -04001014retry_timeout:
1015 task->tk_action = call_timeout;
Chuck Leverda351872005-08-11 16:25:11 -04001016}
1017
1018/*
1019 * 4b. Connect to the RPC server
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 */
1021static void
1022call_connect(struct rpc_task *task)
1023{
Chuck Leverda351872005-08-11 16:25:11 -04001024 struct rpc_xprt *xprt = task->tk_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
Chuck Lever46121cf2007-01-31 12:14:08 -05001026 dprintk("RPC: %5u call_connect xprt %p %s connected\n",
Chuck Leverda351872005-08-11 16:25:11 -04001027 task->tk_pid, xprt,
1028 (xprt_connected(xprt) ? "is" : "is not"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
Chuck Leverda351872005-08-11 16:25:11 -04001030 task->tk_action = call_transmit;
1031 if (!xprt_connected(xprt)) {
1032 task->tk_action = call_connect_status;
1033 if (task->tk_status < 0)
1034 return;
1035 xprt_connect(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037}
1038
1039/*
Chuck Leverda351872005-08-11 16:25:11 -04001040 * 4c. Sort out connect result
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 */
1042static void
1043call_connect_status(struct rpc_task *task)
1044{
1045 struct rpc_clnt *clnt = task->tk_client;
1046 int status = task->tk_status;
1047
Chuck Lever46121cf2007-01-31 12:14:08 -05001048 dprint_status(task);
Chuck Leverda351872005-08-11 16:25:11 -04001049
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 task->tk_status = 0;
1051 if (status >= 0) {
1052 clnt->cl_stats->netreconn++;
1053 task->tk_action = call_transmit;
1054 return;
1055 }
1056
Chuck Leverda351872005-08-11 16:25:11 -04001057 /* Something failed: remote service port may have changed */
Chuck Lever35f5a422006-01-03 09:55:50 +01001058 rpc_force_rebind(clnt);
Chuck Leverda351872005-08-11 16:25:11 -04001059
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 switch (status) {
1061 case -ENOTCONN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 case -EAGAIN:
Chuck Leverda351872005-08-11 16:25:11 -04001063 task->tk_action = call_bind;
Trond Myklebustda458282006-08-31 15:44:52 -04001064 if (!RPC_IS_SOFT(task))
1065 return;
1066 /* if soft mounted, test if we've timed out */
1067 case -ETIMEDOUT:
1068 task->tk_action = call_timeout;
1069 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 }
Trond Myklebustda458282006-08-31 15:44:52 -04001071 rpc_exit(task, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072}
1073
1074/*
1075 * 5. Transmit the RPC request, and wait for reply
1076 */
1077static void
1078call_transmit(struct rpc_task *task)
1079{
Chuck Lever46121cf2007-01-31 12:14:08 -05001080 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
1082 task->tk_action = call_status;
1083 if (task->tk_status < 0)
1084 return;
1085 task->tk_status = xprt_prepare_transmit(task);
1086 if (task->tk_status != 0)
1087 return;
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04001088 task->tk_action = call_transmit_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 /* Encode here so that rpcsec_gss can use correct sequence number. */
Trond Myklebust940e3312005-11-09 21:45:24 -05001090 if (rpc_task_need_encode(task)) {
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04001091 BUG_ON(task->tk_rqstp->rq_bytes_sent != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 call_encode(task);
Trond Myklebust5e5ce5b2005-10-18 14:20:11 -07001093 /* Did the encode result in an error condition? */
1094 if (task->tk_status != 0)
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04001095 return;
Trond Myklebust5e5ce5b2005-10-18 14:20:11 -07001096 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 xprt_transmit(task);
1098 if (task->tk_status < 0)
1099 return;
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04001100 /*
1101 * On success, ensure that we call xprt_end_transmit() before sleeping
1102 * in order to allow access to the socket to other RPC requests.
1103 */
1104 call_transmit_status(task);
1105 if (task->tk_msg.rpc_proc->p_decode != NULL)
1106 return;
1107 task->tk_action = rpc_exit_task;
1108 rpc_wake_up_task(task);
1109}
1110
1111/*
1112 * 5a. Handle cleanup after a transmission
1113 */
1114static void
1115call_transmit_status(struct rpc_task *task)
1116{
1117 task->tk_action = call_status;
1118 /*
1119 * Special case: if we've been waiting on the socket's write_space()
1120 * callback, then don't call xprt_end_transmit().
1121 */
1122 if (task->tk_status == -EAGAIN)
1123 return;
1124 xprt_end_transmit(task);
Trond Myklebust940e3312005-11-09 21:45:24 -05001125 rpc_task_force_reencode(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126}
1127
1128/*
1129 * 6. Sort out the RPC call status
1130 */
1131static void
1132call_status(struct rpc_task *task)
1133{
1134 struct rpc_clnt *clnt = task->tk_client;
1135 struct rpc_rqst *req = task->tk_rqstp;
1136 int status;
1137
1138 if (req->rq_received > 0 && !req->rq_bytes_sent)
1139 task->tk_status = req->rq_received;
1140
Chuck Lever46121cf2007-01-31 12:14:08 -05001141 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142
1143 status = task->tk_status;
1144 if (status >= 0) {
1145 task->tk_action = call_decode;
1146 return;
1147 }
1148
1149 task->tk_status = 0;
1150 switch(status) {
Trond Myklebust76303992006-08-30 14:32:49 -04001151 case -EHOSTDOWN:
1152 case -EHOSTUNREACH:
1153 case -ENETUNREACH:
1154 /*
1155 * Delay any retries for 3 seconds, then handle as if it
1156 * were a timeout.
1157 */
1158 rpc_delay(task, 3*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 case -ETIMEDOUT:
1160 task->tk_action = call_timeout;
Trond Myklebust241c39b2007-04-20 16:12:55 -04001161 if (task->tk_client->cl_discrtry)
Trond Myklebust3ebb0672007-11-06 18:40:12 -05001162 xprt_force_disconnect(task->tk_xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 break;
1164 case -ECONNREFUSED:
1165 case -ENOTCONN:
Chuck Lever35f5a422006-01-03 09:55:50 +01001166 rpc_force_rebind(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 task->tk_action = call_bind;
1168 break;
1169 case -EAGAIN:
1170 task->tk_action = call_transmit;
1171 break;
1172 case -EIO:
1173 /* shutdown or soft timeout */
1174 rpc_exit(task, status);
1175 break;
1176 default:
Chuck Leverf518e352006-01-03 09:55:52 +01001177 printk("%s: RPC call returned error %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 clnt->cl_protname, -status);
1179 rpc_exit(task, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 }
1181}
1182
1183/*
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04001184 * 6a. Handle RPC timeout
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 * We do not release the request slot, so we keep using the
1186 * same XID for all retransmits.
1187 */
1188static void
1189call_timeout(struct rpc_task *task)
1190{
1191 struct rpc_clnt *clnt = task->tk_client;
1192
1193 if (xprt_adjust_timeout(task->tk_rqstp) == 0) {
Chuck Lever46121cf2007-01-31 12:14:08 -05001194 dprintk("RPC: %5u call_timeout (minor)\n", task->tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 goto retry;
1196 }
1197
Chuck Lever46121cf2007-01-31 12:14:08 -05001198 dprintk("RPC: %5u call_timeout (major)\n", task->tk_pid);
Chuck Leveref759a22006-03-20 13:44:17 -05001199 task->tk_timeouts++;
1200
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 if (RPC_IS_SOFT(task)) {
Chuck Leverf518e352006-01-03 09:55:52 +01001202 printk(KERN_NOTICE "%s: server %s not responding, timed out\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 clnt->cl_protname, clnt->cl_server);
1204 rpc_exit(task, -EIO);
1205 return;
1206 }
1207
Chuck Leverf518e352006-01-03 09:55:52 +01001208 if (!(task->tk_flags & RPC_CALL_MAJORSEEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 task->tk_flags |= RPC_CALL_MAJORSEEN;
1210 printk(KERN_NOTICE "%s: server %s not responding, still trying\n",
1211 clnt->cl_protname, clnt->cl_server);
1212 }
Chuck Lever35f5a422006-01-03 09:55:50 +01001213 rpc_force_rebind(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
1215retry:
1216 clnt->cl_stats->rpcretrans++;
1217 task->tk_action = call_bind;
1218 task->tk_status = 0;
1219}
1220
1221/*
1222 * 7. Decode the RPC reply
1223 */
1224static void
1225call_decode(struct rpc_task *task)
1226{
1227 struct rpc_clnt *clnt = task->tk_client;
1228 struct rpc_rqst *req = task->tk_rqstp;
1229 kxdrproc_t decode = task->tk_msg.rpc_proc->p_decode;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001230 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
Chuck Lever46121cf2007-01-31 12:14:08 -05001232 dprintk("RPC: %5u call_decode (status %d)\n",
1233 task->tk_pid, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
Chuck Leverf518e352006-01-03 09:55:52 +01001235 if (task->tk_flags & RPC_CALL_MAJORSEEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 printk(KERN_NOTICE "%s: server %s OK\n",
1237 clnt->cl_protname, clnt->cl_server);
1238 task->tk_flags &= ~RPC_CALL_MAJORSEEN;
1239 }
1240
1241 if (task->tk_status < 12) {
1242 if (!RPC_IS_SOFT(task)) {
1243 task->tk_action = call_bind;
1244 clnt->cl_stats->rpcretrans++;
1245 goto out_retry;
1246 }
Chuck Lever46121cf2007-01-31 12:14:08 -05001247 dprintk("RPC: %s: too small RPC reply size (%d bytes)\n",
1248 clnt->cl_protname, task->tk_status);
Trond Myklebustda458282006-08-31 15:44:52 -04001249 task->tk_action = call_timeout;
1250 goto out_retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 }
1252
Trond Myklebust43ac3f22006-03-20 13:44:51 -05001253 /*
1254 * Ensure that we see all writes made by xprt_complete_rqst()
1255 * before it changed req->rq_received.
1256 */
1257 smp_rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 req->rq_rcv_buf.len = req->rq_private_buf.len;
1259
1260 /* Check that the softirq receive buffer is valid */
1261 WARN_ON(memcmp(&req->rq_rcv_buf, &req->rq_private_buf,
1262 sizeof(req->rq_rcv_buf)) != 0);
1263
1264 /* Verify the RPC header */
Trond Myklebustabbcf282006-01-03 09:55:03 +01001265 p = call_verify(task);
1266 if (IS_ERR(p)) {
1267 if (p == ERR_PTR(-EAGAIN))
1268 goto out_retry;
1269 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 }
1271
Trond Myklebustabbcf282006-01-03 09:55:03 +01001272 task->tk_action = rpc_exit_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
Trond Myklebust6d5fcb52006-10-18 16:01:06 -04001274 if (decode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 task->tk_status = rpcauth_unwrap_resp(task, decode, req, p,
1276 task->tk_msg.rpc_resp);
Trond Myklebust6d5fcb52006-10-18 16:01:06 -04001277 }
Chuck Lever46121cf2007-01-31 12:14:08 -05001278 dprintk("RPC: %5u call_decode result %d\n", task->tk_pid,
1279 task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 return;
1281out_retry:
1282 req->rq_received = req->rq_private_buf.len = 0;
1283 task->tk_status = 0;
Trond Myklebust241c39b2007-04-20 16:12:55 -04001284 if (task->tk_client->cl_discrtry)
Trond Myklebust3ebb0672007-11-06 18:40:12 -05001285 xprt_force_disconnect(task->tk_xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286}
1287
1288/*
1289 * 8. Refresh the credentials if rejected by the server
1290 */
1291static void
1292call_refresh(struct rpc_task *task)
1293{
Chuck Lever46121cf2007-01-31 12:14:08 -05001294 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 task->tk_action = call_refreshresult;
1297 task->tk_status = 0;
1298 task->tk_client->cl_stats->rpcauthrefresh++;
1299 rpcauth_refreshcred(task);
1300}
1301
1302/*
1303 * 8a. Process the results of a credential refresh
1304 */
1305static void
1306call_refreshresult(struct rpc_task *task)
1307{
1308 int status = task->tk_status;
Chuck Lever46121cf2007-01-31 12:14:08 -05001309
1310 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311
1312 task->tk_status = 0;
1313 task->tk_action = call_reserve;
1314 if (status >= 0 && rpcauth_uptodatecred(task))
1315 return;
1316 if (status == -EACCES) {
1317 rpc_exit(task, -EACCES);
1318 return;
1319 }
1320 task->tk_action = call_refresh;
1321 if (status != -ETIMEDOUT)
1322 rpc_delay(task, 3*HZ);
1323 return;
1324}
1325
1326/*
1327 * Call header serialization
1328 */
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001329static __be32 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330call_header(struct rpc_task *task)
1331{
1332 struct rpc_clnt *clnt = task->tk_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 struct rpc_rqst *req = task->tk_rqstp;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001334 __be32 *p = req->rq_svec[0].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
1336 /* FIXME: check buffer size? */
Chuck Lever808012f2005-08-25 16:25:49 -07001337
1338 p = xprt_skip_transport_header(task->tk_xprt, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 *p++ = req->rq_xid; /* XID */
1340 *p++ = htonl(RPC_CALL); /* CALL */
1341 *p++ = htonl(RPC_VERSION); /* RPC version */
1342 *p++ = htonl(clnt->cl_prog); /* program number */
1343 *p++ = htonl(clnt->cl_vers); /* program version */
1344 *p++ = htonl(task->tk_msg.rpc_proc->p_proc); /* procedure */
Trond Myklebust334ccfd2005-06-22 17:16:19 +00001345 p = rpcauth_marshcred(task, p);
1346 req->rq_slen = xdr_adjust_iovec(&req->rq_svec[0], p);
1347 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348}
1349
1350/*
1351 * Reply header verification
1352 */
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001353static __be32 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354call_verify(struct rpc_task *task)
1355{
1356 struct kvec *iov = &task->tk_rqstp->rq_rcv_buf.head[0];
1357 int len = task->tk_rqstp->rq_rcv_buf.len >> 2;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001358 __be32 *p = iov->iov_base;
1359 u32 n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 int error = -EACCES;
1361
David Howellse8896492006-08-24 15:44:19 -04001362 if ((task->tk_rqstp->rq_rcv_buf.len & 3) != 0) {
1363 /* RFC-1014 says that the representation of XDR data must be a
1364 * multiple of four bytes
1365 * - if it isn't pointer subtraction in the NFS client may give
1366 * undefined results
1367 */
Trond Myklebust8a702bb2007-06-27 18:30:26 -04001368 dprintk("RPC: %5u %s: XDR representation not a multiple of"
1369 " 4 bytes: 0x%x\n", task->tk_pid, __FUNCTION__,
1370 task->tk_rqstp->rq_rcv_buf.len);
David Howellse8896492006-08-24 15:44:19 -04001371 goto out_eio;
1372 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 if ((len -= 3) < 0)
1374 goto out_overflow;
1375 p += 1; /* skip XID */
1376
1377 if ((n = ntohl(*p++)) != RPC_REPLY) {
Trond Myklebust8a702bb2007-06-27 18:30:26 -04001378 dprintk("RPC: %5u %s: not an RPC reply: %x\n",
1379 task->tk_pid, __FUNCTION__, n);
Trond Myklebustabbcf282006-01-03 09:55:03 +01001380 goto out_garbage;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 }
1382 if ((n = ntohl(*p++)) != RPC_MSG_ACCEPTED) {
1383 if (--len < 0)
1384 goto out_overflow;
1385 switch ((n = ntohl(*p++))) {
1386 case RPC_AUTH_ERROR:
1387 break;
1388 case RPC_MISMATCH:
Chuck Lever46121cf2007-01-31 12:14:08 -05001389 dprintk("RPC: %5u %s: RPC call version "
1390 "mismatch!\n",
1391 task->tk_pid, __FUNCTION__);
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00001392 error = -EPROTONOSUPPORT;
1393 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 default:
Chuck Lever46121cf2007-01-31 12:14:08 -05001395 dprintk("RPC: %5u %s: RPC call rejected, "
1396 "unknown error: %x\n",
1397 task->tk_pid, __FUNCTION__, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 goto out_eio;
1399 }
1400 if (--len < 0)
1401 goto out_overflow;
1402 switch ((n = ntohl(*p++))) {
1403 case RPC_AUTH_REJECTEDCRED:
1404 case RPC_AUTH_REJECTEDVERF:
1405 case RPCSEC_GSS_CREDPROBLEM:
1406 case RPCSEC_GSS_CTXPROBLEM:
1407 if (!task->tk_cred_retry)
1408 break;
1409 task->tk_cred_retry--;
Chuck Lever46121cf2007-01-31 12:14:08 -05001410 dprintk("RPC: %5u %s: retry stale creds\n",
1411 task->tk_pid, __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 rpcauth_invalcred(task);
Trond Myklebust220bcc22007-10-01 12:06:48 -04001413 /* Ensure we obtain a new XID! */
1414 xprt_release(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 task->tk_action = call_refresh;
Trond Myklebustabbcf282006-01-03 09:55:03 +01001416 goto out_retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 case RPC_AUTH_BADCRED:
1418 case RPC_AUTH_BADVERF:
1419 /* possibly garbled cred/verf? */
1420 if (!task->tk_garb_retry)
1421 break;
1422 task->tk_garb_retry--;
Chuck Lever46121cf2007-01-31 12:14:08 -05001423 dprintk("RPC: %5u %s: retry garbled creds\n",
1424 task->tk_pid, __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 task->tk_action = call_bind;
Trond Myklebustabbcf282006-01-03 09:55:03 +01001426 goto out_retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 case RPC_AUTH_TOOWEAK:
Levent Serinol1356b8c2006-03-20 13:44:11 -05001428 printk(KERN_NOTICE "call_verify: server %s requires stronger "
1429 "authentication.\n", task->tk_client->cl_server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 break;
1431 default:
Trond Myklebust8a702bb2007-06-27 18:30:26 -04001432 dprintk("RPC: %5u %s: unknown auth error: %x\n",
1433 task->tk_pid, __FUNCTION__, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 error = -EIO;
1435 }
Chuck Lever46121cf2007-01-31 12:14:08 -05001436 dprintk("RPC: %5u %s: call rejected %d\n",
1437 task->tk_pid, __FUNCTION__, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 goto out_err;
1439 }
1440 if (!(p = rpcauth_checkverf(task, p))) {
Trond Myklebust8a702bb2007-06-27 18:30:26 -04001441 dprintk("RPC: %5u %s: auth check failed\n",
1442 task->tk_pid, __FUNCTION__);
Trond Myklebustabbcf282006-01-03 09:55:03 +01001443 goto out_garbage; /* bad verifier, retry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 }
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001445 len = p - (__be32 *)iov->iov_base - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 if (len < 0)
1447 goto out_overflow;
1448 switch ((n = ntohl(*p++))) {
1449 case RPC_SUCCESS:
1450 return p;
1451 case RPC_PROG_UNAVAIL:
Chuck Lever46121cf2007-01-31 12:14:08 -05001452 dprintk("RPC: %5u %s: program %u is unsupported by server %s\n",
1453 task->tk_pid, __FUNCTION__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 (unsigned int)task->tk_client->cl_prog,
1455 task->tk_client->cl_server);
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00001456 error = -EPFNOSUPPORT;
1457 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 case RPC_PROG_MISMATCH:
Chuck Lever46121cf2007-01-31 12:14:08 -05001459 dprintk("RPC: %5u %s: program %u, version %u unsupported by "
1460 "server %s\n", task->tk_pid, __FUNCTION__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 (unsigned int)task->tk_client->cl_prog,
1462 (unsigned int)task->tk_client->cl_vers,
1463 task->tk_client->cl_server);
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00001464 error = -EPROTONOSUPPORT;
1465 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 case RPC_PROC_UNAVAIL:
Chuck Lever46121cf2007-01-31 12:14:08 -05001467 dprintk("RPC: %5u %s: proc %p unsupported by program %u, "
1468 "version %u on server %s\n",
1469 task->tk_pid, __FUNCTION__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 task->tk_msg.rpc_proc,
1471 task->tk_client->cl_prog,
1472 task->tk_client->cl_vers,
1473 task->tk_client->cl_server);
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00001474 error = -EOPNOTSUPP;
1475 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 case RPC_GARBAGE_ARGS:
Chuck Lever46121cf2007-01-31 12:14:08 -05001477 dprintk("RPC: %5u %s: server saw garbage\n",
1478 task->tk_pid, __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 break; /* retry */
1480 default:
Trond Myklebust8a702bb2007-06-27 18:30:26 -04001481 dprintk("RPC: %5u %s: server accept status: %x\n",
1482 task->tk_pid, __FUNCTION__, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 /* Also retry */
1484 }
1485
Trond Myklebustabbcf282006-01-03 09:55:03 +01001486out_garbage:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 task->tk_client->cl_stats->rpcgarbage++;
1488 if (task->tk_garb_retry) {
1489 task->tk_garb_retry--;
Chuck Lever46121cf2007-01-31 12:14:08 -05001490 dprintk("RPC: %5u %s: retrying\n",
1491 task->tk_pid, __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 task->tk_action = call_bind;
Trond Myklebustabbcf282006-01-03 09:55:03 +01001493out_retry:
1494 return ERR_PTR(-EAGAIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496out_eio:
1497 error = -EIO;
1498out_err:
1499 rpc_exit(task, error);
Trond Myklebust8a702bb2007-06-27 18:30:26 -04001500 dprintk("RPC: %5u %s: call failed with error %d\n", task->tk_pid,
1501 __FUNCTION__, error);
Trond Myklebustabbcf282006-01-03 09:55:03 +01001502 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503out_overflow:
Trond Myklebust8a702bb2007-06-27 18:30:26 -04001504 dprintk("RPC: %5u %s: server reply was truncated.\n", task->tk_pid,
1505 __FUNCTION__);
Trond Myklebustabbcf282006-01-03 09:55:03 +01001506 goto out_garbage;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507}
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00001508
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001509static int rpcproc_encode_null(void *rqstp, __be32 *data, void *obj)
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00001510{
1511 return 0;
1512}
1513
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001514static int rpcproc_decode_null(void *rqstp, __be32 *data, void *obj)
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00001515{
1516 return 0;
1517}
1518
1519static struct rpc_procinfo rpcproc_null = {
1520 .p_encode = rpcproc_encode_null,
1521 .p_decode = rpcproc_decode_null,
1522};
1523
Trond Myklebust64c91a12007-06-23 10:17:16 -04001524static int rpc_ping(struct rpc_clnt *clnt, int flags)
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00001525{
1526 struct rpc_message msg = {
1527 .rpc_proc = &rpcproc_null,
1528 };
1529 int err;
1530 msg.rpc_cred = authnull_ops.lookup_cred(NULL, NULL, 0);
1531 err = rpc_call_sync(clnt, &msg, flags);
1532 put_rpccred(msg.rpc_cred);
1533 return err;
1534}
Trond Myklebust188fef12007-06-16 14:18:40 -04001535
Trond Myklebust5e1550d2007-06-23 10:17:16 -04001536struct rpc_task *rpc_call_null(struct rpc_clnt *clnt, struct rpc_cred *cred, int flags)
1537{
1538 struct rpc_message msg = {
1539 .rpc_proc = &rpcproc_null,
1540 .rpc_cred = cred,
1541 };
Trond Myklebust84115e12007-07-14 15:39:59 -04001542 struct rpc_task_setup task_setup_data = {
1543 .rpc_client = clnt,
1544 .rpc_message = &msg,
1545 .callback_ops = &rpc_default_ops,
1546 .flags = flags,
1547 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001548 return rpc_run_task(&task_setup_data);
Trond Myklebust5e1550d2007-06-23 10:17:16 -04001549}
Trond Myklebuste8914c62007-07-14 15:39:59 -04001550EXPORT_SYMBOL_GPL(rpc_call_null);
Trond Myklebust5e1550d2007-06-23 10:17:16 -04001551
Trond Myklebust188fef12007-06-16 14:18:40 -04001552#ifdef RPC_DEBUG
1553void rpc_show_tasks(void)
1554{
1555 struct rpc_clnt *clnt;
1556 struct rpc_task *t;
1557
1558 spin_lock(&rpc_client_lock);
1559 if (list_empty(&all_clients))
1560 goto out;
1561 printk("-pid- proc flgs status -client- -prog- --rqstp- -timeout "
1562 "-rpcwait -action- ---ops--\n");
1563 list_for_each_entry(clnt, &all_clients, cl_clients) {
1564 if (list_empty(&clnt->cl_tasks))
1565 continue;
1566 spin_lock(&clnt->cl_lock);
1567 list_for_each_entry(t, &clnt->cl_tasks, tk_task) {
1568 const char *rpc_waitq = "none";
Chuck Leverd66968f2007-09-11 18:00:25 -04001569 int proc;
1570
1571 if (t->tk_msg.rpc_proc)
1572 proc = t->tk_msg.rpc_proc->p_proc;
1573 else
1574 proc = -1;
Trond Myklebust188fef12007-06-16 14:18:40 -04001575
1576 if (RPC_IS_QUEUED(t))
1577 rpc_waitq = rpc_qname(t->u.tk_wait.rpc_waitq);
1578
1579 printk("%5u %04d %04x %6d %8p %6d %8p %8ld %8s %8p %8p\n",
Chuck Leverd66968f2007-09-11 18:00:25 -04001580 t->tk_pid, proc,
Trond Myklebust188fef12007-06-16 14:18:40 -04001581 t->tk_flags, t->tk_status,
1582 t->tk_client,
1583 (t->tk_client ? t->tk_client->cl_prog : 0),
1584 t->tk_rqstp, t->tk_timeout,
1585 rpc_waitq,
1586 t->tk_action, t->tk_ops);
1587 }
1588 spin_unlock(&clnt->cl_lock);
1589 }
1590out:
1591 spin_unlock(&rpc_client_lock);
1592}
1593#endif