blob: 76739e928d0da5ff99b748ac61538e2bfc03fbf4 [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>
Chuck Levercb3997b2008-05-21 17:09:41 -040028#include <linux/kallsyms.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/mm.h>
30#include <linux/slab.h>
Trond Myklebust6d5fcb52006-10-18 16:01:06 -040031#include <linux/smp_lock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/utsname.h>
Chuck Lever11c556b2006-03-20 13:44:22 -050033#include <linux/workqueue.h>
Chuck Lever510deb02007-12-10 14:56:24 -050034#include <linux/in6.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36#include <linux/sunrpc/clnt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/sunrpc/rpc_pipe_fs.h>
Chuck Lever11c556b2006-03-20 13:44:22 -050038#include <linux/sunrpc/metrics.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#ifdef RPC_DEBUG
42# define RPCDBG_FACILITY RPCDBG_CALL
43#endif
44
Chuck Lever46121cf2007-01-31 12:14:08 -050045#define dprint_status(t) \
46 dprintk("RPC: %5u %s (status %d)\n", t->tk_pid, \
Harvey Harrison0dc47872008-03-05 20:47:47 -080047 __func__, t->tk_status)
Chuck Lever46121cf2007-01-31 12:14:08 -050048
Trond Myklebust188fef12007-06-16 14:18:40 -040049/*
50 * All RPC clients are linked into this list
51 */
52static LIST_HEAD(all_clients);
53static DEFINE_SPINLOCK(rpc_client_lock);
54
Linus Torvalds1da177e2005-04-16 15:20:36 -070055static DECLARE_WAIT_QUEUE_HEAD(destroy_wait);
56
57
58static void call_start(struct rpc_task *task);
59static void call_reserve(struct rpc_task *task);
60static void call_reserveresult(struct rpc_task *task);
61static void call_allocate(struct rpc_task *task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062static 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Chuck Leverb0e1c572008-05-21 17:09:19 -040074static __be32 *rpc_encode_header(struct rpc_task *task);
75static __be32 *rpc_verify_header(struct rpc_task *task);
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
Trond Myklebustba7392b2007-12-20 16:03:55 -0500191 clnt->cl_timeout = xprt->timeout;
192 if (args->timeout != NULL) {
193 memcpy(&clnt->cl_timeout_default, args->timeout,
194 sizeof(clnt->cl_timeout_default));
195 clnt->cl_timeout = &clnt->cl_timeout_default;
196 }
197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 clnt->cl_rtt = &clnt->cl_rtt_default;
Trond Myklebustba7392b2007-12-20 16:03:55 -0500199 rpc_init_rtt(&clnt->cl_rtt_default, clnt->cl_timeout->to_initval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Trond Myklebust34f52e32007-06-14 16:40:31 -0400201 kref_init(&clnt->cl_kref);
202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 err = rpc_setup_pipedir(clnt, program->pipe_dir_name);
204 if (err < 0)
205 goto out_no_path;
206
Trond Myklebust698b6d02007-12-20 16:03:53 -0500207 auth = rpcauth_create(args->authflavor, clnt);
J. Bruce Fields6a192752005-06-22 17:16:23 +0000208 if (IS_ERR(auth)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 printk(KERN_INFO "RPC: Couldn't create auth handle (flavor %u)\n",
Trond Myklebust698b6d02007-12-20 16:03:53 -0500210 args->authflavor);
J. Bruce Fields6a192752005-06-22 17:16:23 +0000211 err = PTR_ERR(auth);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 goto out_no_auth;
213 }
214
215 /* save the nodename */
Serge E. Hallyne9ff3992006-10-02 02:18:11 -0700216 clnt->cl_nodelen = strlen(utsname()->nodename);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 if (clnt->cl_nodelen > UNX_MAXNODENAME)
218 clnt->cl_nodelen = UNX_MAXNODENAME;
Serge E. Hallyne9ff3992006-10-02 02:18:11 -0700219 memcpy(clnt->cl_nodename, utsname()->nodename, clnt->cl_nodelen);
Trond Myklebust6529eba2007-06-14 16:40:14 -0400220 rpc_register_client(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 return clnt;
222
223out_no_auth:
Trond Myklebust54281542006-03-20 13:44:49 -0500224 if (!IS_ERR(clnt->cl_dentry)) {
Trond Myklebustdff02cc2006-07-31 14:17:18 -0700225 rpc_rmdir(clnt->cl_dentry);
Trond Myklebust54281542006-03-20 13:44:49 -0500226 rpc_put_mount();
227 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228out_no_path:
Trond Myklebust23bf85b2006-11-21 10:40:23 -0500229 rpc_free_iostats(clnt->cl_metrics);
230out_no_stats:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 if (clnt->cl_server != clnt->cl_inline_name)
232 kfree(clnt->cl_server);
233 kfree(clnt);
234out_err:
Trond Myklebust6b6ca862006-09-05 12:55:57 -0400235 xprt_put(xprt);
Adrian Bunk712917d2006-03-13 21:20:47 -0800236out_no_xprt:
Trond Myklebust4ada5392007-06-14 17:26:17 -0400237 rpciod_down();
238out_no_rpciod:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 return ERR_PTR(err);
240}
241
Chuck Leverc2866762006-08-22 20:06:20 -0400242/*
243 * rpc_create - create an RPC client and transport with one call
244 * @args: rpc_clnt create argument structure
245 *
246 * Creates and initializes an RPC transport and an RPC client.
247 *
248 * It can ping the server in order to determine if it is up, and to see if
249 * it supports this program and version. RPC_CLNT_CREATE_NOPING disables
250 * this behavior so asynchronous tasks can also use rpc_create.
251 */
252struct rpc_clnt *rpc_create(struct rpc_create_args *args)
253{
254 struct rpc_xprt *xprt;
255 struct rpc_clnt *clnt;
\"Talpey, Thomas\3c341b02007-09-10 13:47:07 -0400256 struct xprt_create xprtargs = {
\"Talpey, Thomas\4fa016e2007-09-10 13:47:57 -0400257 .ident = args->protocol,
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +0200258 .srcaddr = args->saddress,
Frank van Maarseveen96802a02007-07-08 13:08:54 +0200259 .dstaddr = args->address,
260 .addrlen = args->addrsize,
Frank van Maarseveen96802a02007-07-08 13:08:54 +0200261 };
Chuck Lever510deb02007-12-10 14:56:24 -0500262 char servername[48];
Chuck Leverc2866762006-08-22 20:06:20 -0400263
Chuck Leverc2866762006-08-22 20:06:20 -0400264 /*
Chuck Lever43780b82007-07-01 12:13:22 -0400265 * If the caller chooses not to specify a hostname, whip
266 * up a string representation of the passed-in address.
267 */
268 if (args->servername == NULL) {
Chuck Lever510deb02007-12-10 14:56:24 -0500269 servername[0] = '\0';
270 switch (args->address->sa_family) {
271 case AF_INET: {
272 struct sockaddr_in *sin =
273 (struct sockaddr_in *)args->address;
274 snprintf(servername, sizeof(servername), NIPQUAD_FMT,
275 NIPQUAD(sin->sin_addr.s_addr));
276 break;
277 }
278 case AF_INET6: {
279 struct sockaddr_in6 *sin =
280 (struct sockaddr_in6 *)args->address;
281 snprintf(servername, sizeof(servername), NIP6_FMT,
282 NIP6(sin->sin6_addr));
283 break;
284 }
285 default:
286 /* caller wants default server name, but
287 * address family isn't recognized. */
288 return ERR_PTR(-EINVAL);
289 }
Chuck Lever43780b82007-07-01 12:13:22 -0400290 args->servername = servername;
291 }
292
Chuck Lever510deb02007-12-10 14:56:24 -0500293 xprt = xprt_create_transport(&xprtargs);
294 if (IS_ERR(xprt))
295 return (struct rpc_clnt *)xprt;
296
Chuck Lever43780b82007-07-01 12:13:22 -0400297 /*
Chuck Leverc2866762006-08-22 20:06:20 -0400298 * By default, kernel RPC client connects from a reserved port.
299 * CAP_NET_BIND_SERVICE will not be set for unprivileged requesters,
300 * but it is always enabled for rpciod, which handles the connect
301 * operation.
302 */
303 xprt->resvport = 1;
304 if (args->flags & RPC_CLNT_CREATE_NONPRIVPORT)
305 xprt->resvport = 0;
306
Trond Myklebust698b6d02007-12-20 16:03:53 -0500307 clnt = rpc_new_client(args, xprt);
Chuck Leverc2866762006-08-22 20:06:20 -0400308 if (IS_ERR(clnt))
309 return clnt;
310
311 if (!(args->flags & RPC_CLNT_CREATE_NOPING)) {
Matthew Wilcox150030b2007-12-06 16:24:39 -0500312 int err = rpc_ping(clnt, RPC_TASK_SOFT);
Chuck Leverc2866762006-08-22 20:06:20 -0400313 if (err != 0) {
314 rpc_shutdown_client(clnt);
315 return ERR_PTR(err);
316 }
317 }
318
319 clnt->cl_softrtry = 1;
320 if (args->flags & RPC_CLNT_CREATE_HARDRTRY)
321 clnt->cl_softrtry = 0;
322
Chuck Leverc2866762006-08-22 20:06:20 -0400323 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;
Olga Kornievskaiab6b61522008-06-09 16:51:31 -0400327 if (!(args->flags & RPC_CLNT_CREATE_QUIET))
328 clnt->cl_chatty = 1;
Chuck Leverc2866762006-08-22 20:06:20 -0400329
330 return clnt;
331}
Chuck Leverb86acd52006-08-22 20:06:22 -0400332EXPORT_SYMBOL_GPL(rpc_create);
Chuck Leverc2866762006-08-22 20:06:20 -0400333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334/*
335 * This function clones the RPC client structure. It allows us to share the
336 * same transport while varying parameters such as the authentication
337 * flavour.
338 */
339struct rpc_clnt *
340rpc_clone_client(struct rpc_clnt *clnt)
341{
342 struct rpc_clnt *new;
Trond Myklebust3e32a5d2006-11-16 11:37:27 -0500343 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
Arnaldo Carvalho de Meloe69062b2006-11-21 01:21:34 -0200345 new = kmemdup(clnt, sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 if (!new)
347 goto out_no_clnt;
Trond Myklebustd431a5552007-06-17 17:07:54 -0400348 new->cl_parent = clnt;
349 /* Turn off autobind on clones */
350 new->cl_autobind = 0;
351 INIT_LIST_HEAD(&new->cl_tasks);
352 spin_lock_init(&new->cl_lock);
Trond Myklebustba7392b2007-12-20 16:03:55 -0500353 rpc_init_rtt(&new->cl_rtt_default, clnt->cl_timeout->to_initval);
Trond Myklebust23bf85b2006-11-21 10:40:23 -0500354 new->cl_metrics = rpc_alloc_iostats(clnt);
355 if (new->cl_metrics == NULL)
356 goto out_no_stats;
Trond Myklebust34f52e32007-06-14 16:40:31 -0400357 kref_init(&new->cl_kref);
Trond Myklebust3e32a5d2006-11-16 11:37:27 -0500358 err = rpc_setup_pipedir(new, clnt->cl_program->pipe_dir_name);
359 if (err != 0)
360 goto out_no_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 if (new->cl_auth)
362 atomic_inc(&new->cl_auth->au_count);
Trond Myklebustd431a5552007-06-17 17:07:54 -0400363 xprt_get(clnt->cl_xprt);
364 kref_get(&clnt->cl_kref);
Trond Myklebust6529eba2007-06-14 16:40:14 -0400365 rpc_register_client(new);
Trond Myklebust4ada5392007-06-14 17:26:17 -0400366 rpciod_up();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 return new;
Trond Myklebust3e32a5d2006-11-16 11:37:27 -0500368out_no_path:
369 rpc_free_iostats(new->cl_metrics);
Trond Myklebust23bf85b2006-11-21 10:40:23 -0500370out_no_stats:
371 kfree(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372out_no_clnt:
Harvey Harrison0dc47872008-03-05 20:47:47 -0800373 dprintk("RPC: %s: returned error %d\n", __func__, err);
Trond Myklebust3e32a5d2006-11-16 11:37:27 -0500374 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400376EXPORT_SYMBOL_GPL(rpc_clone_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
378/*
379 * Properly shut down an RPC client, terminating all outstanding
Trond Myklebust90c57552007-06-09 19:49:36 -0400380 * requests.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 */
Trond Myklebust4c402b42007-06-14 16:40:32 -0400382void rpc_shutdown_client(struct rpc_clnt *clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383{
Trond Myklebust34f52e32007-06-14 16:40:31 -0400384 dprintk("RPC: shutting down %s client for %s\n",
385 clnt->cl_protname, clnt->cl_server);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Trond Myklebust34f52e32007-06-14 16:40:31 -0400387 while (!list_empty(&clnt->cl_tasks)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 rpc_killall_tasks(clnt);
Ingo Molnar532347e2006-01-09 20:52:53 -0800389 wait_event_timeout(destroy_wait,
Trond Myklebust34f52e32007-06-14 16:40:31 -0400390 list_empty(&clnt->cl_tasks), 1*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 }
392
Trond Myklebust4c402b42007-06-14 16:40:32 -0400393 rpc_release_client(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400395EXPORT_SYMBOL_GPL(rpc_shutdown_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
397/*
Trond Myklebust34f52e32007-06-14 16:40:31 -0400398 * Free an RPC client
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 */
Trond Myklebust34f52e32007-06-14 16:40:31 -0400400static void
401rpc_free_client(struct kref *kref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402{
Trond Myklebust34f52e32007-06-14 16:40:31 -0400403 struct rpc_clnt *clnt = container_of(kref, struct rpc_clnt, cl_kref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
Chuck Lever46121cf2007-01-31 12:14:08 -0500405 dprintk("RPC: destroying %s client for %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 clnt->cl_protname, clnt->cl_server);
Trond Myklebust8f8e7a52006-08-14 13:11:15 -0400407 if (!IS_ERR(clnt->cl_dentry)) {
Trond Myklebustdff02cc2006-07-31 14:17:18 -0700408 rpc_rmdir(clnt->cl_dentry);
Trond Myklebust8f8e7a52006-08-14 13:11:15 -0400409 rpc_put_mount();
410 }
Trond Myklebust3e32a5d2006-11-16 11:37:27 -0500411 if (clnt->cl_parent != clnt) {
Trond Myklebust8ad7c892007-06-14 16:40:32 -0400412 rpc_release_client(clnt->cl_parent);
Trond Myklebust3e32a5d2006-11-16 11:37:27 -0500413 goto out_free;
414 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 if (clnt->cl_server != clnt->cl_inline_name)
416 kfree(clnt->cl_server);
417out_free:
Trond Myklebust6529eba2007-06-14 16:40:14 -0400418 rpc_unregister_client(clnt);
Chuck Lever11c556b2006-03-20 13:44:22 -0500419 rpc_free_iostats(clnt->cl_metrics);
420 clnt->cl_metrics = NULL;
Trond Myklebust6b6ca862006-09-05 12:55:57 -0400421 xprt_put(clnt->cl_xprt);
Trond Myklebust4ada5392007-06-14 17:26:17 -0400422 rpciod_down();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 kfree(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424}
425
426/*
Trond Myklebust1dd17ec2007-06-26 16:57:41 -0400427 * Free an RPC client
428 */
429static void
430rpc_free_auth(struct kref *kref)
431{
432 struct rpc_clnt *clnt = container_of(kref, struct rpc_clnt, cl_kref);
433
434 if (clnt->cl_auth == NULL) {
435 rpc_free_client(kref);
436 return;
437 }
438
439 /*
440 * Note: RPCSEC_GSS may need to send NULL RPC calls in order to
441 * release remaining GSS contexts. This mechanism ensures
442 * that it can do so safely.
443 */
444 kref_init(kref);
445 rpcauth_release(clnt->cl_auth);
446 clnt->cl_auth = NULL;
447 kref_put(kref, rpc_free_client);
448}
449
450/*
Trond Myklebust34f52e32007-06-14 16:40:31 -0400451 * Release reference to the RPC client
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 */
453void
454rpc_release_client(struct rpc_clnt *clnt)
455{
Trond Myklebust34f52e32007-06-14 16:40:31 -0400456 dprintk("RPC: rpc_release_client(%p)\n", clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
Trond Myklebust34f52e32007-06-14 16:40:31 -0400458 if (list_empty(&clnt->cl_tasks))
459 wake_up(&destroy_wait);
Trond Myklebust1dd17ec2007-06-26 16:57:41 -0400460 kref_put(&clnt->cl_kref, rpc_free_auth);
Trond Myklebust34f52e32007-06-14 16:40:31 -0400461}
462
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000463/**
464 * rpc_bind_new_program - bind a new RPC program to an existing client
Randy Dunlap65b6e422008-02-13 15:03:23 -0800465 * @old: old rpc_client
466 * @program: rpc program to set
467 * @vers: rpc program version
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000468 *
469 * Clones the rpc client and sets up a new RPC program. This is mainly
470 * of use for enabling different RPC programs to share the same transport.
471 * The Sun NFSv2/v3 ACL protocol can do this.
472 */
473struct rpc_clnt *rpc_bind_new_program(struct rpc_clnt *old,
474 struct rpc_program *program,
Chuck Lever89eb21c2007-09-11 18:00:09 -0400475 u32 vers)
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000476{
477 struct rpc_clnt *clnt;
478 struct rpc_version *version;
479 int err;
480
481 BUG_ON(vers >= program->nrvers || !program->version[vers]);
482 version = program->version[vers];
483 clnt = rpc_clone_client(old);
484 if (IS_ERR(clnt))
485 goto out;
486 clnt->cl_procinfo = version->procs;
487 clnt->cl_maxproc = version->nrprocs;
488 clnt->cl_protname = program->name;
489 clnt->cl_prog = program->number;
490 clnt->cl_vers = version->number;
491 clnt->cl_stats = program->stats;
Matthew Wilcox150030b2007-12-06 16:24:39 -0500492 err = rpc_ping(clnt, RPC_TASK_SOFT);
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000493 if (err != 0) {
494 rpc_shutdown_client(clnt);
495 clnt = ERR_PTR(err);
496 }
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800497out:
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000498 return clnt;
499}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400500EXPORT_SYMBOL_GPL(rpc_bind_new_program);
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000501
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502/*
503 * Default callback for async RPC calls
504 */
505static void
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100506rpc_default_callback(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507{
508}
509
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100510static const struct rpc_call_ops rpc_default_ops = {
511 .rpc_call_done = rpc_default_callback,
512};
513
Trond Myklebustc970aa82007-07-14 15:39:59 -0400514/**
515 * rpc_run_task - Allocate a new RPC task, then run rpc_execute against it
516 * @task_setup_data: pointer to task initialisation data
517 */
518struct rpc_task *rpc_run_task(const struct rpc_task_setup *task_setup_data)
Trond Myklebust6e5b70e2007-06-12 10:02:37 -0400519{
520 struct rpc_task *task, *ret;
Trond Myklebust6e5b70e2007-06-12 10:02:37 -0400521
Trond Myklebust84115e12007-07-14 15:39:59 -0400522 task = rpc_new_task(task_setup_data);
Trond Myklebust6e5b70e2007-06-12 10:02:37 -0400523 if (task == NULL) {
Trond Myklebust84115e12007-07-14 15:39:59 -0400524 rpc_release_calldata(task_setup_data->callback_ops,
525 task_setup_data->callback_data);
Trond Myklebust50859252007-10-25 18:19:37 -0400526 ret = ERR_PTR(-ENOMEM);
527 goto out;
Trond Myklebust6e5b70e2007-06-12 10:02:37 -0400528 }
529
Trond Myklebustb3ef8b32007-10-25 18:32:34 -0400530 if (task->tk_status != 0) {
531 ret = ERR_PTR(task->tk_status);
532 rpc_put_task(task);
533 goto out;
Trond Myklebust6e5b70e2007-06-12 10:02:37 -0400534 }
535 atomic_inc(&task->tk_count);
536 rpc_execute(task);
537 ret = task;
538out:
Trond Myklebust6e5b70e2007-06-12 10:02:37 -0400539 return ret;
540}
Trond Myklebustc970aa82007-07-14 15:39:59 -0400541EXPORT_SYMBOL_GPL(rpc_run_task);
Trond Myklebust6e5b70e2007-06-12 10:02:37 -0400542
543/**
544 * rpc_call_sync - Perform a synchronous RPC call
545 * @clnt: pointer to RPC client
546 * @msg: RPC call parameters
547 * @flags: RPC call flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 */
Trond Myklebustcbc20052008-02-14 11:11:30 -0500549int rpc_call_sync(struct rpc_clnt *clnt, const struct rpc_message *msg, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550{
551 struct rpc_task *task;
Trond Myklebust84115e12007-07-14 15:39:59 -0400552 struct rpc_task_setup task_setup_data = {
553 .rpc_client = clnt,
554 .rpc_message = msg,
555 .callback_ops = &rpc_default_ops,
556 .flags = flags,
557 };
Trond Myklebust6e5b70e2007-06-12 10:02:37 -0400558 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 BUG_ON(flags & RPC_TASK_ASYNC);
561
Trond Myklebustc970aa82007-07-14 15:39:59 -0400562 task = rpc_run_task(&task_setup_data);
Trond Myklebust6e5b70e2007-06-12 10:02:37 -0400563 if (IS_ERR(task))
564 return PTR_ERR(task);
Trond Myklebuste60859a2006-01-03 09:55:10 +0100565 status = task->tk_status;
Trond Myklebustbde8f002007-01-24 11:54:53 -0800566 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 return status;
568}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400569EXPORT_SYMBOL_GPL(rpc_call_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
Trond Myklebust6e5b70e2007-06-12 10:02:37 -0400571/**
572 * rpc_call_async - Perform an asynchronous RPC call
573 * @clnt: pointer to RPC client
574 * @msg: RPC call parameters
575 * @flags: RPC call flags
Randy Dunlap65b6e422008-02-13 15:03:23 -0800576 * @tk_ops: RPC call ops
Trond Myklebust6e5b70e2007-06-12 10:02:37 -0400577 * @data: user call data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 */
579int
Trond Myklebustcbc20052008-02-14 11:11:30 -0500580rpc_call_async(struct rpc_clnt *clnt, const struct rpc_message *msg, int flags,
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100581 const struct rpc_call_ops *tk_ops, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582{
583 struct rpc_task *task;
Trond Myklebust84115e12007-07-14 15:39:59 -0400584 struct rpc_task_setup task_setup_data = {
585 .rpc_client = clnt,
586 .rpc_message = msg,
587 .callback_ops = tk_ops,
588 .callback_data = data,
589 .flags = flags|RPC_TASK_ASYNC,
590 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
Trond Myklebustc970aa82007-07-14 15:39:59 -0400592 task = rpc_run_task(&task_setup_data);
Trond Myklebust6e5b70e2007-06-12 10:02:37 -0400593 if (IS_ERR(task))
594 return PTR_ERR(task);
595 rpc_put_task(task);
596 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400598EXPORT_SYMBOL_GPL(rpc_call_async);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600void
Trond Myklebust77de2c52007-10-25 18:40:21 -0400601rpc_call_start(struct rpc_task *task)
602{
603 task->tk_action = call_start;
604}
605EXPORT_SYMBOL_GPL(rpc_call_start);
606
Chuck Levered394402006-08-22 20:06:17 -0400607/**
608 * rpc_peeraddr - extract remote peer address from clnt's xprt
609 * @clnt: RPC client structure
610 * @buf: target buffer
Randy Dunlap65b6e422008-02-13 15:03:23 -0800611 * @bufsize: length of target buffer
Chuck Levered394402006-08-22 20:06:17 -0400612 *
613 * Returns the number of bytes that are actually in the stored address.
614 */
615size_t rpc_peeraddr(struct rpc_clnt *clnt, struct sockaddr *buf, size_t bufsize)
616{
617 size_t bytes;
618 struct rpc_xprt *xprt = clnt->cl_xprt;
619
620 bytes = sizeof(xprt->addr);
621 if (bytes > bufsize)
622 bytes = bufsize;
623 memcpy(buf, &clnt->cl_xprt->addr, bytes);
Chuck Leverc4efcb12006-08-22 20:06:19 -0400624 return xprt->addrlen;
Chuck Levered394402006-08-22 20:06:17 -0400625}
Chuck Leverb86acd52006-08-22 20:06:22 -0400626EXPORT_SYMBOL_GPL(rpc_peeraddr);
Chuck Levered394402006-08-22 20:06:17 -0400627
Chuck Leverf425eba2006-08-22 20:06:18 -0400628/**
629 * rpc_peeraddr2str - return remote peer address in printable format
630 * @clnt: RPC client structure
631 * @format: address format
632 *
633 */
Chuck Leverb454ae92008-01-07 18:34:48 -0500634const char *rpc_peeraddr2str(struct rpc_clnt *clnt,
635 enum rpc_display_format_t format)
Chuck Leverf425eba2006-08-22 20:06:18 -0400636{
637 struct rpc_xprt *xprt = clnt->cl_xprt;
Chuck Lever7559c7a2006-12-05 16:35:37 -0500638
639 if (xprt->address_strings[format] != NULL)
640 return xprt->address_strings[format];
641 else
642 return "unprintable";
Chuck Leverf425eba2006-08-22 20:06:18 -0400643}
Chuck Leverb86acd52006-08-22 20:06:22 -0400644EXPORT_SYMBOL_GPL(rpc_peeraddr2str);
Chuck Leverf425eba2006-08-22 20:06:18 -0400645
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646void
647rpc_setbufsize(struct rpc_clnt *clnt, unsigned int sndsize, unsigned int rcvsize)
648{
649 struct rpc_xprt *xprt = clnt->cl_xprt;
Chuck Lever470056c2005-08-25 16:25:56 -0700650 if (xprt->ops->set_buffer_size)
651 xprt->ops->set_buffer_size(xprt, sndsize, rcvsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400653EXPORT_SYMBOL_GPL(rpc_setbufsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
655/*
656 * Return size of largest payload RPC client can support, in bytes
657 *
658 * For stream transports, this is one RPC record fragment (see RFC
659 * 1831), as we don't support multi-record requests yet. For datagram
660 * transports, this is the size of an IP packet minus the IP, UDP, and
661 * RPC header sizes.
662 */
663size_t rpc_max_payload(struct rpc_clnt *clnt)
664{
665 return clnt->cl_xprt->max_payload;
666}
Chuck Leverb86acd52006-08-22 20:06:22 -0400667EXPORT_SYMBOL_GPL(rpc_max_payload);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
Chuck Lever35f5a422006-01-03 09:55:50 +0100669/**
670 * rpc_force_rebind - force transport to check that remote port is unchanged
671 * @clnt: client to rebind
672 *
673 */
674void rpc_force_rebind(struct rpc_clnt *clnt)
675{
676 if (clnt->cl_autobind)
Chuck Leverec739ef2006-08-22 20:06:15 -0400677 xprt_clear_bound(clnt->cl_xprt);
Chuck Lever35f5a422006-01-03 09:55:50 +0100678}
Chuck Leverb86acd52006-08-22 20:06:22 -0400679EXPORT_SYMBOL_GPL(rpc_force_rebind);
Chuck Lever35f5a422006-01-03 09:55:50 +0100680
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681/*
682 * Restart an (async) RPC call. Usually called from within the
683 * exit handler.
684 */
685void
686rpc_restart_call(struct rpc_task *task)
687{
688 if (RPC_ASSASSINATED(task))
689 return;
690
691 task->tk_action = call_start;
692}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400693EXPORT_SYMBOL_GPL(rpc_restart_call);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
Chuck Lever3748f1e2008-05-21 17:09:12 -0400695#ifdef RPC_DEBUG
696static const char *rpc_proc_name(const struct rpc_task *task)
697{
698 const struct rpc_procinfo *proc = task->tk_msg.rpc_proc;
699
700 if (proc) {
701 if (proc->p_name)
702 return proc->p_name;
703 else
704 return "NULL";
705 } else
706 return "no proc";
707}
708#endif
709
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710/*
711 * 0. Initial state
712 *
713 * Other FSM states can be visited zero or more times, but
714 * this state is visited exactly once for each RPC.
715 */
716static void
717call_start(struct rpc_task *task)
718{
719 struct rpc_clnt *clnt = task->tk_client;
720
Chuck Lever3748f1e2008-05-21 17:09:12 -0400721 dprintk("RPC: %5u call_start %s%d proc %s (%s)\n", task->tk_pid,
Chuck Lever46121cf2007-01-31 12:14:08 -0500722 clnt->cl_protname, clnt->cl_vers,
Chuck Lever3748f1e2008-05-21 17:09:12 -0400723 rpc_proc_name(task),
Chuck Lever46121cf2007-01-31 12:14:08 -0500724 (RPC_IS_ASYNC(task) ? "async" : "sync"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
726 /* Increment call count */
727 task->tk_msg.rpc_proc->p_count++;
728 clnt->cl_stats->rpccnt++;
729 task->tk_action = call_reserve;
730}
731
732/*
733 * 1. Reserve an RPC call slot
734 */
735static void
736call_reserve(struct rpc_task *task)
737{
Chuck Lever46121cf2007-01-31 12:14:08 -0500738 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
740 if (!rpcauth_uptodatecred(task)) {
741 task->tk_action = call_refresh;
742 return;
743 }
744
745 task->tk_status = 0;
746 task->tk_action = call_reserveresult;
747 xprt_reserve(task);
748}
749
750/*
751 * 1b. Grok the result of xprt_reserve()
752 */
753static void
754call_reserveresult(struct rpc_task *task)
755{
756 int status = task->tk_status;
757
Chuck Lever46121cf2007-01-31 12:14:08 -0500758 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
760 /*
761 * After a call to xprt_reserve(), we must have either
762 * a request slot or else an error status.
763 */
764 task->tk_status = 0;
765 if (status >= 0) {
766 if (task->tk_rqstp) {
767 task->tk_action = call_allocate;
768 return;
769 }
770
771 printk(KERN_ERR "%s: status=%d, but no request slot, exiting\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800772 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 rpc_exit(task, -EIO);
774 return;
775 }
776
777 /*
778 * Even though there was an error, we may have acquired
779 * a request slot somehow. Make sure not to leak it.
780 */
781 if (task->tk_rqstp) {
782 printk(KERN_ERR "%s: status=%d, request allocated anyway\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800783 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 xprt_release(task);
785 }
786
787 switch (status) {
788 case -EAGAIN: /* woken up; retry */
789 task->tk_action = call_reserve;
790 return;
791 case -EIO: /* probably a shutdown */
792 break;
793 default:
794 printk(KERN_ERR "%s: unrecognized error %d, exiting\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800795 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 break;
797 }
798 rpc_exit(task, status);
799}
800
801/*
802 * 2. Allocate the buffer. For details, see sched.c:rpc_malloc.
Chuck Lever02107142006-01-03 09:55:49 +0100803 * (Note: buffer memory is freed in xprt_release).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 */
805static void
806call_allocate(struct rpc_task *task)
807{
Trond Myklebust1be27f32007-06-27 14:29:04 -0400808 unsigned int slack = task->tk_msg.rpc_cred->cr_auth->au_cslack;
Chuck Lever02107142006-01-03 09:55:49 +0100809 struct rpc_rqst *req = task->tk_rqstp;
810 struct rpc_xprt *xprt = task->tk_xprt;
Chuck Lever2bea90d2007-03-29 16:47:53 -0400811 struct rpc_procinfo *proc = task->tk_msg.rpc_proc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
Chuck Lever46121cf2007-01-31 12:14:08 -0500813 dprint_status(task);
814
Chuck Lever2bea90d2007-03-29 16:47:53 -0400815 task->tk_status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 task->tk_action = call_bind;
Chuck Lever2bea90d2007-03-29 16:47:53 -0400817
Chuck Lever02107142006-01-03 09:55:49 +0100818 if (req->rq_buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 return;
820
Chuck Lever2bea90d2007-03-29 16:47:53 -0400821 if (proc->p_proc != 0) {
822 BUG_ON(proc->p_arglen == 0);
823 if (proc->p_decode != NULL)
824 BUG_ON(proc->p_replen == 0);
825 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
Chuck Lever2bea90d2007-03-29 16:47:53 -0400827 /*
828 * Calculate the size (in quads) of the RPC call
829 * and reply headers, and convert both values
830 * to byte sizes.
831 */
832 req->rq_callsize = RPC_CALLHDRSIZE + (slack << 1) + proc->p_arglen;
833 req->rq_callsize <<= 2;
834 req->rq_rcvsize = RPC_REPHDRSIZE + slack + proc->p_replen;
835 req->rq_rcvsize <<= 2;
836
Chuck Leverc5a4dd82007-03-29 16:47:58 -0400837 req->rq_buffer = xprt->ops->buf_alloc(task,
838 req->rq_callsize + req->rq_rcvsize);
Chuck Lever2bea90d2007-03-29 16:47:53 -0400839 if (req->rq_buffer != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 return;
Chuck Lever46121cf2007-01-31 12:14:08 -0500841
842 dprintk("RPC: %5u rpc_buffer allocation failed\n", task->tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Trond Myklebust14b218a2005-06-22 17:16:28 +0000844 if (RPC_IS_ASYNC(task) || !signalled()) {
Trond Myklebustb6e9c712007-10-01 12:06:44 -0400845 task->tk_action = call_allocate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 rpc_delay(task, HZ>>4);
847 return;
848 }
849
850 rpc_exit(task, -ERESTARTSYS);
851}
852
Trond Myklebust940e3312005-11-09 21:45:24 -0500853static inline int
854rpc_task_need_encode(struct rpc_task *task)
855{
856 return task->tk_rqstp->rq_snd_buf.len == 0;
857}
858
859static inline void
860rpc_task_force_reencode(struct rpc_task *task)
861{
862 task->tk_rqstp->rq_snd_buf.len = 0;
863}
864
Chuck Lever2bea90d2007-03-29 16:47:53 -0400865static inline void
866rpc_xdr_buf_init(struct xdr_buf *buf, void *start, size_t len)
867{
868 buf->head[0].iov_base = start;
869 buf->head[0].iov_len = len;
870 buf->tail[0].iov_len = 0;
871 buf->page_len = 0;
\"Talpey, Thomas\4f22ccc2007-09-10 13:44:58 -0400872 buf->flags = 0;
Chuck Lever2bea90d2007-03-29 16:47:53 -0400873 buf->len = 0;
874 buf->buflen = len;
875}
876
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877/*
878 * 3. Encode arguments of an RPC call
879 */
880static void
Chuck Leverb0e1c572008-05-21 17:09:19 -0400881rpc_xdr_encode(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 struct rpc_rqst *req = task->tk_rqstp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 kxdrproc_t encode;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700885 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
Chuck Lever46121cf2007-01-31 12:14:08 -0500887 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
Chuck Lever2bea90d2007-03-29 16:47:53 -0400889 rpc_xdr_buf_init(&req->rq_snd_buf,
890 req->rq_buffer,
891 req->rq_callsize);
892 rpc_xdr_buf_init(&req->rq_rcv_buf,
893 (char *)req->rq_buffer + req->rq_callsize,
894 req->rq_rcvsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
Chuck Leverb0e1c572008-05-21 17:09:19 -0400896 p = rpc_encode_header(task);
897 if (p == NULL) {
898 printk(KERN_INFO "RPC: couldn't encode RPC header, exit EIO\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 rpc_exit(task, -EIO);
900 return;
901 }
Chuck Leverb0e1c572008-05-21 17:09:19 -0400902
903 encode = task->tk_msg.rpc_proc->p_encode;
J. Bruce Fieldsf3680312005-10-13 16:54:48 -0400904 if (encode == NULL)
905 return;
906
907 task->tk_status = rpcauth_wrap_req(task, encode, req, p,
908 task->tk_msg.rpc_argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909}
910
911/*
912 * 4. Get the server port number if not yet set
913 */
914static void
915call_bind(struct rpc_task *task)
916{
Chuck Leverec739ef2006-08-22 20:06:15 -0400917 struct rpc_xprt *xprt = task->tk_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
Chuck Lever46121cf2007-01-31 12:14:08 -0500919 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
Chuck Leverda351872005-08-11 16:25:11 -0400921 task->tk_action = call_connect;
Chuck Leverec739ef2006-08-22 20:06:15 -0400922 if (!xprt_bound(xprt)) {
Chuck Leverda351872005-08-11 16:25:11 -0400923 task->tk_action = call_bind_status;
Chuck Leverec739ef2006-08-22 20:06:15 -0400924 task->tk_timeout = xprt->bind_timeout;
Chuck Leverbbf7c1d2006-08-22 20:06:16 -0400925 xprt->ops->rpcbind(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 }
927}
928
929/*
Chuck Leverda351872005-08-11 16:25:11 -0400930 * 4a. Sort out bind result
931 */
932static void
933call_bind_status(struct rpc_task *task)
934{
Chuck Lever906462a2007-09-11 18:00:47 -0400935 int status = -EIO;
Chuck Leverda351872005-08-11 16:25:11 -0400936
937 if (task->tk_status >= 0) {
Chuck Lever46121cf2007-01-31 12:14:08 -0500938 dprint_status(task);
Chuck Leverda351872005-08-11 16:25:11 -0400939 task->tk_status = 0;
940 task->tk_action = call_connect;
941 return;
942 }
943
944 switch (task->tk_status) {
Trond Myklebust381ba742008-07-07 12:18:53 -0400945 case -ENOMEM:
946 dprintk("RPC: %5u rpcbind out of memory\n", task->tk_pid);
947 rpc_delay(task, HZ >> 2);
Chuck Lever2429cbf2007-09-11 18:00:41 -0400948 goto retry_timeout;
Chuck Leverda351872005-08-11 16:25:11 -0400949 case -EACCES:
Chuck Lever46121cf2007-01-31 12:14:08 -0500950 dprintk("RPC: %5u remote rpcbind: RPC program/version "
951 "unavailable\n", task->tk_pid);
Chuck Leverb79dc8c2007-09-11 18:00:52 -0400952 /* fail immediately if this is an RPC ping */
953 if (task->tk_msg.rpc_proc->p_proc == 0) {
954 status = -EOPNOTSUPP;
955 break;
956 }
Chuck Leverea635a52005-10-06 23:12:58 -0400957 rpc_delay(task, 3*HZ);
Trond Myklebustda458282006-08-31 15:44:52 -0400958 goto retry_timeout;
Chuck Leverda351872005-08-11 16:25:11 -0400959 case -ETIMEDOUT:
Chuck Lever46121cf2007-01-31 12:14:08 -0500960 dprintk("RPC: %5u rpcbind request timed out\n",
Chuck Leverda351872005-08-11 16:25:11 -0400961 task->tk_pid);
Trond Myklebustda458282006-08-31 15:44:52 -0400962 goto retry_timeout;
Chuck Leverda351872005-08-11 16:25:11 -0400963 case -EPFNOSUPPORT:
Chuck Lever906462a2007-09-11 18:00:47 -0400964 /* server doesn't support any rpcbind version we know of */
Chuck Lever46121cf2007-01-31 12:14:08 -0500965 dprintk("RPC: %5u remote rpcbind service unavailable\n",
Chuck Leverda351872005-08-11 16:25:11 -0400966 task->tk_pid);
967 break;
968 case -EPROTONOSUPPORT:
Chuck Lever00a6e7b2007-03-29 16:48:33 -0400969 dprintk("RPC: %5u remote rpcbind version unavailable, retrying\n",
Chuck Leverda351872005-08-11 16:25:11 -0400970 task->tk_pid);
Chuck Lever00a6e7b2007-03-29 16:48:33 -0400971 task->tk_status = 0;
972 task->tk_action = call_bind;
973 return;
Chuck Leverda351872005-08-11 16:25:11 -0400974 default:
Chuck Lever46121cf2007-01-31 12:14:08 -0500975 dprintk("RPC: %5u unrecognized rpcbind error (%d)\n",
Chuck Leverda351872005-08-11 16:25:11 -0400976 task->tk_pid, -task->tk_status);
Chuck Leverda351872005-08-11 16:25:11 -0400977 }
978
979 rpc_exit(task, status);
980 return;
981
Trond Myklebustda458282006-08-31 15:44:52 -0400982retry_timeout:
983 task->tk_action = call_timeout;
Chuck Leverda351872005-08-11 16:25:11 -0400984}
985
986/*
987 * 4b. Connect to the RPC server
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 */
989static void
990call_connect(struct rpc_task *task)
991{
Chuck Leverda351872005-08-11 16:25:11 -0400992 struct rpc_xprt *xprt = task->tk_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
Chuck Lever46121cf2007-01-31 12:14:08 -0500994 dprintk("RPC: %5u call_connect xprt %p %s connected\n",
Chuck Leverda351872005-08-11 16:25:11 -0400995 task->tk_pid, xprt,
996 (xprt_connected(xprt) ? "is" : "is not"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
Chuck Leverda351872005-08-11 16:25:11 -0400998 task->tk_action = call_transmit;
999 if (!xprt_connected(xprt)) {
1000 task->tk_action = call_connect_status;
1001 if (task->tk_status < 0)
1002 return;
1003 xprt_connect(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005}
1006
1007/*
Chuck Leverda351872005-08-11 16:25:11 -04001008 * 4c. Sort out connect result
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 */
1010static void
1011call_connect_status(struct rpc_task *task)
1012{
1013 struct rpc_clnt *clnt = task->tk_client;
1014 int status = task->tk_status;
1015
Chuck Lever46121cf2007-01-31 12:14:08 -05001016 dprint_status(task);
Chuck Leverda351872005-08-11 16:25:11 -04001017
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 task->tk_status = 0;
1019 if (status >= 0) {
1020 clnt->cl_stats->netreconn++;
1021 task->tk_action = call_transmit;
1022 return;
1023 }
1024
Chuck Leverda351872005-08-11 16:25:11 -04001025 /* Something failed: remote service port may have changed */
Chuck Lever35f5a422006-01-03 09:55:50 +01001026 rpc_force_rebind(clnt);
Chuck Leverda351872005-08-11 16:25:11 -04001027
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 switch (status) {
1029 case -ENOTCONN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 case -EAGAIN:
Chuck Leverda351872005-08-11 16:25:11 -04001031 task->tk_action = call_bind;
Trond Myklebustda458282006-08-31 15:44:52 -04001032 if (!RPC_IS_SOFT(task))
1033 return;
1034 /* if soft mounted, test if we've timed out */
1035 case -ETIMEDOUT:
1036 task->tk_action = call_timeout;
1037 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 }
Trond Myklebustda458282006-08-31 15:44:52 -04001039 rpc_exit(task, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040}
1041
1042/*
1043 * 5. Transmit the RPC request, and wait for reply
1044 */
1045static void
1046call_transmit(struct rpc_task *task)
1047{
Chuck Lever46121cf2007-01-31 12:14:08 -05001048 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
1050 task->tk_action = call_status;
1051 if (task->tk_status < 0)
1052 return;
1053 task->tk_status = xprt_prepare_transmit(task);
1054 if (task->tk_status != 0)
1055 return;
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04001056 task->tk_action = call_transmit_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 /* Encode here so that rpcsec_gss can use correct sequence number. */
Trond Myklebust940e3312005-11-09 21:45:24 -05001058 if (rpc_task_need_encode(task)) {
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04001059 BUG_ON(task->tk_rqstp->rq_bytes_sent != 0);
Chuck Leverb0e1c572008-05-21 17:09:19 -04001060 rpc_xdr_encode(task);
Trond Myklebust5e5ce5b2005-10-18 14:20:11 -07001061 /* Did the encode result in an error condition? */
Trond Myklebust8b39f2b2008-05-14 19:48:25 -07001062 if (task->tk_status != 0) {
1063 /* Was the error nonfatal? */
1064 if (task->tk_status == -EAGAIN)
1065 rpc_delay(task, HZ >> 4);
1066 else
1067 rpc_exit(task, task->tk_status);
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04001068 return;
Trond Myklebust8b39f2b2008-05-14 19:48:25 -07001069 }
Trond Myklebust5e5ce5b2005-10-18 14:20:11 -07001070 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 xprt_transmit(task);
1072 if (task->tk_status < 0)
1073 return;
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04001074 /*
1075 * On success, ensure that we call xprt_end_transmit() before sleeping
1076 * in order to allow access to the socket to other RPC requests.
1077 */
1078 call_transmit_status(task);
1079 if (task->tk_msg.rpc_proc->p_decode != NULL)
1080 return;
1081 task->tk_action = rpc_exit_task;
Trond Myklebustfda13932008-02-22 16:34:12 -05001082 rpc_wake_up_queued_task(&task->tk_xprt->pending, task);
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04001083}
1084
1085/*
1086 * 5a. Handle cleanup after a transmission
1087 */
1088static void
1089call_transmit_status(struct rpc_task *task)
1090{
1091 task->tk_action = call_status;
1092 /*
1093 * Special case: if we've been waiting on the socket's write_space()
1094 * callback, then don't call xprt_end_transmit().
1095 */
1096 if (task->tk_status == -EAGAIN)
1097 return;
1098 xprt_end_transmit(task);
Trond Myklebust940e3312005-11-09 21:45:24 -05001099 rpc_task_force_reencode(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100}
1101
1102/*
1103 * 6. Sort out the RPC call status
1104 */
1105static void
1106call_status(struct rpc_task *task)
1107{
1108 struct rpc_clnt *clnt = task->tk_client;
1109 struct rpc_rqst *req = task->tk_rqstp;
1110 int status;
1111
1112 if (req->rq_received > 0 && !req->rq_bytes_sent)
1113 task->tk_status = req->rq_received;
1114
Chuck Lever46121cf2007-01-31 12:14:08 -05001115 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
1117 status = task->tk_status;
1118 if (status >= 0) {
1119 task->tk_action = call_decode;
1120 return;
1121 }
1122
1123 task->tk_status = 0;
1124 switch(status) {
Trond Myklebust76303992006-08-30 14:32:49 -04001125 case -EHOSTDOWN:
1126 case -EHOSTUNREACH:
1127 case -ENETUNREACH:
1128 /*
1129 * Delay any retries for 3 seconds, then handle as if it
1130 * were a timeout.
1131 */
1132 rpc_delay(task, 3*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 case -ETIMEDOUT:
1134 task->tk_action = call_timeout;
Trond Myklebust241c39b2007-04-20 16:12:55 -04001135 if (task->tk_client->cl_discrtry)
Trond Myklebust7c1d71c2008-04-17 16:52:57 -04001136 xprt_conditional_disconnect(task->tk_xprt,
1137 req->rq_connect_cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 break;
1139 case -ECONNREFUSED:
1140 case -ENOTCONN:
Chuck Lever35f5a422006-01-03 09:55:50 +01001141 rpc_force_rebind(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 task->tk_action = call_bind;
1143 break;
1144 case -EAGAIN:
1145 task->tk_action = call_transmit;
1146 break;
1147 case -EIO:
1148 /* shutdown or soft timeout */
1149 rpc_exit(task, status);
1150 break;
1151 default:
Olga Kornievskaiab6b61522008-06-09 16:51:31 -04001152 if (clnt->cl_chatty)
1153 printk("%s: RPC call returned error %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 clnt->cl_protname, -status);
1155 rpc_exit(task, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 }
1157}
1158
1159/*
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04001160 * 6a. Handle RPC timeout
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 * We do not release the request slot, so we keep using the
1162 * same XID for all retransmits.
1163 */
1164static void
1165call_timeout(struct rpc_task *task)
1166{
1167 struct rpc_clnt *clnt = task->tk_client;
1168
1169 if (xprt_adjust_timeout(task->tk_rqstp) == 0) {
Chuck Lever46121cf2007-01-31 12:14:08 -05001170 dprintk("RPC: %5u call_timeout (minor)\n", task->tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 goto retry;
1172 }
1173
Chuck Lever46121cf2007-01-31 12:14:08 -05001174 dprintk("RPC: %5u call_timeout (major)\n", task->tk_pid);
Chuck Leveref759a22006-03-20 13:44:17 -05001175 task->tk_timeouts++;
1176
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 if (RPC_IS_SOFT(task)) {
Olga Kornievskaiab6b61522008-06-09 16:51:31 -04001178 if (clnt->cl_chatty)
1179 printk(KERN_NOTICE "%s: server %s not responding, timed out\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 clnt->cl_protname, clnt->cl_server);
1181 rpc_exit(task, -EIO);
1182 return;
1183 }
1184
Chuck Leverf518e352006-01-03 09:55:52 +01001185 if (!(task->tk_flags & RPC_CALL_MAJORSEEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 task->tk_flags |= RPC_CALL_MAJORSEEN;
Olga Kornievskaiab6b61522008-06-09 16:51:31 -04001187 if (clnt->cl_chatty)
1188 printk(KERN_NOTICE "%s: server %s not responding, still trying\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 clnt->cl_protname, clnt->cl_server);
1190 }
Chuck Lever35f5a422006-01-03 09:55:50 +01001191 rpc_force_rebind(clnt);
Trond Myklebustb48633b2008-04-22 16:47:55 -04001192 /*
1193 * Did our request time out due to an RPCSEC_GSS out-of-sequence
1194 * event? RFC2203 requires the server to drop all such requests.
1195 */
1196 rpcauth_invalcred(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
1198retry:
1199 clnt->cl_stats->rpcretrans++;
1200 task->tk_action = call_bind;
1201 task->tk_status = 0;
1202}
1203
1204/*
1205 * 7. Decode the RPC reply
1206 */
1207static void
1208call_decode(struct rpc_task *task)
1209{
1210 struct rpc_clnt *clnt = task->tk_client;
1211 struct rpc_rqst *req = task->tk_rqstp;
1212 kxdrproc_t decode = task->tk_msg.rpc_proc->p_decode;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001213 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
Chuck Lever46121cf2007-01-31 12:14:08 -05001215 dprintk("RPC: %5u call_decode (status %d)\n",
1216 task->tk_pid, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
Chuck Leverf518e352006-01-03 09:55:52 +01001218 if (task->tk_flags & RPC_CALL_MAJORSEEN) {
Olga Kornievskaiab6b61522008-06-09 16:51:31 -04001219 if (clnt->cl_chatty)
1220 printk(KERN_NOTICE "%s: server %s OK\n",
1221 clnt->cl_protname, clnt->cl_server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 task->tk_flags &= ~RPC_CALL_MAJORSEEN;
1223 }
1224
Trond Myklebust43ac3f22006-03-20 13:44:51 -05001225 /*
1226 * Ensure that we see all writes made by xprt_complete_rqst()
1227 * before it changed req->rq_received.
1228 */
1229 smp_rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 req->rq_rcv_buf.len = req->rq_private_buf.len;
1231
1232 /* Check that the softirq receive buffer is valid */
1233 WARN_ON(memcmp(&req->rq_rcv_buf, &req->rq_private_buf,
1234 sizeof(req->rq_rcv_buf)) != 0);
1235
Trond Myklebust1e799b62008-03-21 16:19:41 -04001236 if (req->rq_rcv_buf.len < 12) {
1237 if (!RPC_IS_SOFT(task)) {
1238 task->tk_action = call_bind;
1239 clnt->cl_stats->rpcretrans++;
1240 goto out_retry;
1241 }
1242 dprintk("RPC: %s: too small RPC reply size (%d bytes)\n",
1243 clnt->cl_protname, task->tk_status);
1244 task->tk_action = call_timeout;
1245 goto out_retry;
1246 }
1247
Chuck Leverb0e1c572008-05-21 17:09:19 -04001248 p = rpc_verify_header(task);
Trond Myklebustabbcf282006-01-03 09:55:03 +01001249 if (IS_ERR(p)) {
1250 if (p == ERR_PTR(-EAGAIN))
1251 goto out_retry;
1252 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 }
1254
Trond Myklebustabbcf282006-01-03 09:55:03 +01001255 task->tk_action = rpc_exit_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
Trond Myklebust6d5fcb52006-10-18 16:01:06 -04001257 if (decode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 task->tk_status = rpcauth_unwrap_resp(task, decode, req, p,
1259 task->tk_msg.rpc_resp);
Trond Myklebust6d5fcb52006-10-18 16:01:06 -04001260 }
Chuck Lever46121cf2007-01-31 12:14:08 -05001261 dprintk("RPC: %5u call_decode result %d\n", task->tk_pid,
1262 task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 return;
1264out_retry:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 task->tk_status = 0;
Chuck Leverb0e1c572008-05-21 17:09:19 -04001266 /* Note: rpc_verify_header() may have freed the RPC slot */
Trond Myklebust24b74bf2008-04-19 13:15:47 -04001267 if (task->tk_rqstp == req) {
Trond Myklebust1e799b62008-03-21 16:19:41 -04001268 req->rq_received = req->rq_rcv_buf.len = 0;
Trond Myklebust24b74bf2008-04-19 13:15:47 -04001269 if (task->tk_client->cl_discrtry)
Trond Myklebust7c1d71c2008-04-17 16:52:57 -04001270 xprt_conditional_disconnect(task->tk_xprt,
1271 req->rq_connect_cookie);
Trond Myklebust24b74bf2008-04-19 13:15:47 -04001272 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273}
1274
1275/*
1276 * 8. Refresh the credentials if rejected by the server
1277 */
1278static void
1279call_refresh(struct rpc_task *task)
1280{
Chuck Lever46121cf2007-01-31 12:14:08 -05001281 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 task->tk_action = call_refreshresult;
1284 task->tk_status = 0;
1285 task->tk_client->cl_stats->rpcauthrefresh++;
1286 rpcauth_refreshcred(task);
1287}
1288
1289/*
1290 * 8a. Process the results of a credential refresh
1291 */
1292static void
1293call_refreshresult(struct rpc_task *task)
1294{
1295 int status = task->tk_status;
Chuck Lever46121cf2007-01-31 12:14:08 -05001296
1297 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
1299 task->tk_status = 0;
1300 task->tk_action = call_reserve;
1301 if (status >= 0 && rpcauth_uptodatecred(task))
1302 return;
1303 if (status == -EACCES) {
1304 rpc_exit(task, -EACCES);
1305 return;
1306 }
1307 task->tk_action = call_refresh;
1308 if (status != -ETIMEDOUT)
1309 rpc_delay(task, 3*HZ);
1310 return;
1311}
1312
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001313static __be32 *
Chuck Leverb0e1c572008-05-21 17:09:19 -04001314rpc_encode_header(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315{
1316 struct rpc_clnt *clnt = task->tk_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 struct rpc_rqst *req = task->tk_rqstp;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001318 __be32 *p = req->rq_svec[0].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
1320 /* FIXME: check buffer size? */
Chuck Lever808012f2005-08-25 16:25:49 -07001321
1322 p = xprt_skip_transport_header(task->tk_xprt, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 *p++ = req->rq_xid; /* XID */
1324 *p++ = htonl(RPC_CALL); /* CALL */
1325 *p++ = htonl(RPC_VERSION); /* RPC version */
1326 *p++ = htonl(clnt->cl_prog); /* program number */
1327 *p++ = htonl(clnt->cl_vers); /* program version */
1328 *p++ = htonl(task->tk_msg.rpc_proc->p_proc); /* procedure */
Trond Myklebust334ccfd2005-06-22 17:16:19 +00001329 p = rpcauth_marshcred(task, p);
1330 req->rq_slen = xdr_adjust_iovec(&req->rq_svec[0], p);
1331 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332}
1333
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001334static __be32 *
Chuck Leverb0e1c572008-05-21 17:09:19 -04001335rpc_verify_header(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336{
1337 struct kvec *iov = &task->tk_rqstp->rq_rcv_buf.head[0];
1338 int len = task->tk_rqstp->rq_rcv_buf.len >> 2;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001339 __be32 *p = iov->iov_base;
1340 u32 n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 int error = -EACCES;
1342
David Howellse8896492006-08-24 15:44:19 -04001343 if ((task->tk_rqstp->rq_rcv_buf.len & 3) != 0) {
1344 /* RFC-1014 says that the representation of XDR data must be a
1345 * multiple of four bytes
1346 * - if it isn't pointer subtraction in the NFS client may give
1347 * undefined results
1348 */
Trond Myklebust8a702bb2007-06-27 18:30:26 -04001349 dprintk("RPC: %5u %s: XDR representation not a multiple of"
Harvey Harrison0dc47872008-03-05 20:47:47 -08001350 " 4 bytes: 0x%x\n", task->tk_pid, __func__,
Trond Myklebust8a702bb2007-06-27 18:30:26 -04001351 task->tk_rqstp->rq_rcv_buf.len);
David Howellse8896492006-08-24 15:44:19 -04001352 goto out_eio;
1353 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 if ((len -= 3) < 0)
1355 goto out_overflow;
1356 p += 1; /* skip XID */
1357
1358 if ((n = ntohl(*p++)) != RPC_REPLY) {
Trond Myklebust8a702bb2007-06-27 18:30:26 -04001359 dprintk("RPC: %5u %s: not an RPC reply: %x\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08001360 task->tk_pid, __func__, n);
Trond Myklebustabbcf282006-01-03 09:55:03 +01001361 goto out_garbage;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 }
1363 if ((n = ntohl(*p++)) != RPC_MSG_ACCEPTED) {
1364 if (--len < 0)
1365 goto out_overflow;
1366 switch ((n = ntohl(*p++))) {
1367 case RPC_AUTH_ERROR:
1368 break;
1369 case RPC_MISMATCH:
Chuck Lever46121cf2007-01-31 12:14:08 -05001370 dprintk("RPC: %5u %s: RPC call version "
1371 "mismatch!\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08001372 task->tk_pid, __func__);
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00001373 error = -EPROTONOSUPPORT;
1374 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 default:
Chuck Lever46121cf2007-01-31 12:14:08 -05001376 dprintk("RPC: %5u %s: RPC call rejected, "
1377 "unknown error: %x\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08001378 task->tk_pid, __func__, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 goto out_eio;
1380 }
1381 if (--len < 0)
1382 goto out_overflow;
1383 switch ((n = ntohl(*p++))) {
1384 case RPC_AUTH_REJECTEDCRED:
1385 case RPC_AUTH_REJECTEDVERF:
1386 case RPCSEC_GSS_CREDPROBLEM:
1387 case RPCSEC_GSS_CTXPROBLEM:
1388 if (!task->tk_cred_retry)
1389 break;
1390 task->tk_cred_retry--;
Chuck Lever46121cf2007-01-31 12:14:08 -05001391 dprintk("RPC: %5u %s: retry stale creds\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08001392 task->tk_pid, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 rpcauth_invalcred(task);
Trond Myklebust220bcc22007-10-01 12:06:48 -04001394 /* Ensure we obtain a new XID! */
1395 xprt_release(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 task->tk_action = call_refresh;
Trond Myklebustabbcf282006-01-03 09:55:03 +01001397 goto out_retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 case RPC_AUTH_BADCRED:
1399 case RPC_AUTH_BADVERF:
1400 /* possibly garbled cred/verf? */
1401 if (!task->tk_garb_retry)
1402 break;
1403 task->tk_garb_retry--;
Chuck Lever46121cf2007-01-31 12:14:08 -05001404 dprintk("RPC: %5u %s: retry garbled creds\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08001405 task->tk_pid, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 task->tk_action = call_bind;
Trond Myklebustabbcf282006-01-03 09:55:03 +01001407 goto out_retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 case RPC_AUTH_TOOWEAK:
Chuck Leverb0e1c572008-05-21 17:09:19 -04001409 printk(KERN_NOTICE "RPC: server %s requires stronger "
Levent Serinol1356b8c2006-03-20 13:44:11 -05001410 "authentication.\n", task->tk_client->cl_server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 break;
1412 default:
Trond Myklebust8a702bb2007-06-27 18:30:26 -04001413 dprintk("RPC: %5u %s: unknown auth error: %x\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08001414 task->tk_pid, __func__, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 error = -EIO;
1416 }
Chuck Lever46121cf2007-01-31 12:14:08 -05001417 dprintk("RPC: %5u %s: call rejected %d\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08001418 task->tk_pid, __func__, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 goto out_err;
1420 }
1421 if (!(p = rpcauth_checkverf(task, p))) {
Trond Myklebust8a702bb2007-06-27 18:30:26 -04001422 dprintk("RPC: %5u %s: auth check failed\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08001423 task->tk_pid, __func__);
Trond Myklebustabbcf282006-01-03 09:55:03 +01001424 goto out_garbage; /* bad verifier, retry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 }
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001426 len = p - (__be32 *)iov->iov_base - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 if (len < 0)
1428 goto out_overflow;
1429 switch ((n = ntohl(*p++))) {
1430 case RPC_SUCCESS:
1431 return p;
1432 case RPC_PROG_UNAVAIL:
Chuck Lever46121cf2007-01-31 12:14:08 -05001433 dprintk("RPC: %5u %s: program %u is unsupported by server %s\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08001434 task->tk_pid, __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 (unsigned int)task->tk_client->cl_prog,
1436 task->tk_client->cl_server);
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00001437 error = -EPFNOSUPPORT;
1438 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 case RPC_PROG_MISMATCH:
Chuck Lever46121cf2007-01-31 12:14:08 -05001440 dprintk("RPC: %5u %s: program %u, version %u unsupported by "
Harvey Harrison0dc47872008-03-05 20:47:47 -08001441 "server %s\n", task->tk_pid, __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 (unsigned int)task->tk_client->cl_prog,
1443 (unsigned int)task->tk_client->cl_vers,
1444 task->tk_client->cl_server);
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00001445 error = -EPROTONOSUPPORT;
1446 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 case RPC_PROC_UNAVAIL:
Chuck Lever3748f1e2008-05-21 17:09:12 -04001448 dprintk("RPC: %5u %s: proc %s unsupported by program %u, "
Chuck Lever46121cf2007-01-31 12:14:08 -05001449 "version %u on server %s\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08001450 task->tk_pid, __func__,
Chuck Lever3748f1e2008-05-21 17:09:12 -04001451 rpc_proc_name(task),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 task->tk_client->cl_prog,
1453 task->tk_client->cl_vers,
1454 task->tk_client->cl_server);
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00001455 error = -EOPNOTSUPP;
1456 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 case RPC_GARBAGE_ARGS:
Chuck Lever46121cf2007-01-31 12:14:08 -05001458 dprintk("RPC: %5u %s: server saw garbage\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08001459 task->tk_pid, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 break; /* retry */
1461 default:
Trond Myklebust8a702bb2007-06-27 18:30:26 -04001462 dprintk("RPC: %5u %s: server accept status: %x\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08001463 task->tk_pid, __func__, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 /* Also retry */
1465 }
1466
Trond Myklebustabbcf282006-01-03 09:55:03 +01001467out_garbage:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 task->tk_client->cl_stats->rpcgarbage++;
1469 if (task->tk_garb_retry) {
1470 task->tk_garb_retry--;
Chuck Lever46121cf2007-01-31 12:14:08 -05001471 dprintk("RPC: %5u %s: retrying\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08001472 task->tk_pid, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 task->tk_action = call_bind;
Trond Myklebustabbcf282006-01-03 09:55:03 +01001474out_retry:
1475 return ERR_PTR(-EAGAIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477out_eio:
1478 error = -EIO;
1479out_err:
1480 rpc_exit(task, error);
Trond Myklebust8a702bb2007-06-27 18:30:26 -04001481 dprintk("RPC: %5u %s: call failed with error %d\n", task->tk_pid,
Harvey Harrison0dc47872008-03-05 20:47:47 -08001482 __func__, error);
Trond Myklebustabbcf282006-01-03 09:55:03 +01001483 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484out_overflow:
Trond Myklebust8a702bb2007-06-27 18:30:26 -04001485 dprintk("RPC: %5u %s: server reply was truncated.\n", task->tk_pid,
Harvey Harrison0dc47872008-03-05 20:47:47 -08001486 __func__);
Trond Myklebustabbcf282006-01-03 09:55:03 +01001487 goto out_garbage;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488}
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00001489
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001490static int rpcproc_encode_null(void *rqstp, __be32 *data, void *obj)
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00001491{
1492 return 0;
1493}
1494
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001495static int rpcproc_decode_null(void *rqstp, __be32 *data, void *obj)
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00001496{
1497 return 0;
1498}
1499
1500static struct rpc_procinfo rpcproc_null = {
1501 .p_encode = rpcproc_encode_null,
1502 .p_decode = rpcproc_decode_null,
1503};
1504
Trond Myklebust64c91a12007-06-23 10:17:16 -04001505static int rpc_ping(struct rpc_clnt *clnt, int flags)
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00001506{
1507 struct rpc_message msg = {
1508 .rpc_proc = &rpcproc_null,
1509 };
1510 int err;
1511 msg.rpc_cred = authnull_ops.lookup_cred(NULL, NULL, 0);
1512 err = rpc_call_sync(clnt, &msg, flags);
1513 put_rpccred(msg.rpc_cred);
1514 return err;
1515}
Trond Myklebust188fef12007-06-16 14:18:40 -04001516
Trond Myklebust5e1550d2007-06-23 10:17:16 -04001517struct rpc_task *rpc_call_null(struct rpc_clnt *clnt, struct rpc_cred *cred, int flags)
1518{
1519 struct rpc_message msg = {
1520 .rpc_proc = &rpcproc_null,
1521 .rpc_cred = cred,
1522 };
Trond Myklebust84115e12007-07-14 15:39:59 -04001523 struct rpc_task_setup task_setup_data = {
1524 .rpc_client = clnt,
1525 .rpc_message = &msg,
1526 .callback_ops = &rpc_default_ops,
1527 .flags = flags,
1528 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001529 return rpc_run_task(&task_setup_data);
Trond Myklebust5e1550d2007-06-23 10:17:16 -04001530}
Trond Myklebuste8914c62007-07-14 15:39:59 -04001531EXPORT_SYMBOL_GPL(rpc_call_null);
Trond Myklebust5e1550d2007-06-23 10:17:16 -04001532
Trond Myklebust188fef12007-06-16 14:18:40 -04001533#ifdef RPC_DEBUG
Chuck Lever68a23ee2008-05-21 17:09:26 -04001534static void rpc_show_header(void)
1535{
Chuck Levercb3997b2008-05-21 17:09:41 -04001536 printk(KERN_INFO "-pid- flgs status -client- --rqstp- "
1537 "-timeout ---ops--\n");
Chuck Lever68a23ee2008-05-21 17:09:26 -04001538}
1539
Chuck Lever38e886e2008-05-21 17:09:33 -04001540static void rpc_show_task(const struct rpc_clnt *clnt,
1541 const struct rpc_task *task)
1542{
1543 const char *rpc_waitq = "none";
Chuck Levercb3997b2008-05-21 17:09:41 -04001544 char *p, action[KSYM_SYMBOL_LEN];
Chuck Lever38e886e2008-05-21 17:09:33 -04001545
1546 if (RPC_IS_QUEUED(task))
1547 rpc_waitq = rpc_qname(task->tk_waitqueue);
1548
Chuck Levercb3997b2008-05-21 17:09:41 -04001549 /* map tk_action pointer to a function name; then trim off
1550 * the "+0x0 [sunrpc]" */
1551 sprint_symbol(action, (unsigned long)task->tk_action);
1552 p = strchr(action, '+');
1553 if (p)
1554 *p = '\0';
1555
1556 printk(KERN_INFO "%5u %04x %6d %8p %8p %8ld %8p %sv%u %s a:%s q:%s\n",
1557 task->tk_pid, task->tk_flags, task->tk_status,
1558 clnt, task->tk_rqstp, task->tk_timeout, task->tk_ops,
1559 clnt->cl_protname, clnt->cl_vers, rpc_proc_name(task),
1560 action, rpc_waitq);
Chuck Lever38e886e2008-05-21 17:09:33 -04001561}
1562
Trond Myklebust188fef12007-06-16 14:18:40 -04001563void rpc_show_tasks(void)
1564{
1565 struct rpc_clnt *clnt;
Chuck Lever38e886e2008-05-21 17:09:33 -04001566 struct rpc_task *task;
Chuck Lever68a23ee2008-05-21 17:09:26 -04001567 int header = 0;
Trond Myklebust188fef12007-06-16 14:18:40 -04001568
1569 spin_lock(&rpc_client_lock);
Trond Myklebust188fef12007-06-16 14:18:40 -04001570 list_for_each_entry(clnt, &all_clients, cl_clients) {
Trond Myklebust188fef12007-06-16 14:18:40 -04001571 spin_lock(&clnt->cl_lock);
Chuck Lever38e886e2008-05-21 17:09:33 -04001572 list_for_each_entry(task, &clnt->cl_tasks, tk_task) {
Chuck Lever68a23ee2008-05-21 17:09:26 -04001573 if (!header) {
1574 rpc_show_header();
1575 header++;
1576 }
Chuck Lever38e886e2008-05-21 17:09:33 -04001577 rpc_show_task(clnt, task);
Trond Myklebust188fef12007-06-16 14:18:40 -04001578 }
1579 spin_unlock(&clnt->cl_lock);
1580 }
Trond Myklebust188fef12007-06-16 14:18:40 -04001581 spin_unlock(&rpc_client_lock);
1582}
1583#endif