blob: d10ebc4310f71b422773b72d5cec261b065d8a01 [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 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 * Copyright (C) 1992,1993 Rick Sladkey <jrs@world.std.com>
17 * Copyright (C) 1995,1996 Olaf Kirch <okir@monad.swb.de>
18 */
19
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
21#include <linux/module.h>
22#include <linux/types.h>
Chuck Levercb3997b2008-05-21 17:09:41 -040023#include <linux/kallsyms.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/mm.h>
Trond Myklebust23ac6582009-08-09 15:14:25 -040025#include <linux/namei.h>
26#include <linux/mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/utsname.h>
Chuck Lever11c556b2006-03-20 13:44:22 -050029#include <linux/workqueue.h>
Chuck Lever176e21e2011-05-09 15:22:44 -040030#include <linux/in.h>
Chuck Lever510deb02007-12-10 14:56:24 -050031#include <linux/in6.h>
Chuck Lever176e21e2011-05-09 15:22:44 -040032#include <linux/un.h>
Trond Myklebust2446ab62012-03-01 17:00:56 -050033#include <linux/rcupdate.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>
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -040038#include <linux/sunrpc/bc_xprt.h>
Steve Dickson5753cba2012-02-06 10:08:08 -050039#include <trace/events/sunrpc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -040041#include "sunrpc.h"
Stanislav Kinsbursky70abc492012-01-12 22:07:51 +040042#include "netns.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#ifdef RPC_DEBUG
45# define RPCDBG_FACILITY RPCDBG_CALL
46#endif
47
Chuck Lever46121cf2007-01-31 12:14:08 -050048#define dprint_status(t) \
49 dprintk("RPC: %5u %s (status %d)\n", t->tk_pid, \
Harvey Harrison0dc47872008-03-05 20:47:47 -080050 __func__, t->tk_status)
Chuck Lever46121cf2007-01-31 12:14:08 -050051
Trond Myklebust188fef12007-06-16 14:18:40 -040052/*
53 * All RPC clients are linked into this list
54 */
Trond Myklebust188fef12007-06-16 14:18:40 -040055
Linus Torvalds1da177e2005-04-16 15:20:36 -070056static DECLARE_WAIT_QUEUE_HEAD(destroy_wait);
57
58
59static void call_start(struct rpc_task *task);
60static void call_reserve(struct rpc_task *task);
61static void call_reserveresult(struct rpc_task *task);
62static void call_allocate(struct rpc_task *task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063static void call_decode(struct rpc_task *task);
64static void call_bind(struct rpc_task *task);
Chuck Leverda351872005-08-11 16:25:11 -040065static void call_bind_status(struct rpc_task *task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066static void call_transmit(struct rpc_task *task);
Trond Myklebust9e00abc2011-07-13 19:20:49 -040067#if defined(CONFIG_SUNRPC_BACKCHANNEL)
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -040068static void call_bc_transmit(struct rpc_task *task);
Trond Myklebust9e00abc2011-07-13 19:20:49 -040069#endif /* CONFIG_SUNRPC_BACKCHANNEL */
Linus Torvalds1da177e2005-04-16 15:20:36 -070070static void call_status(struct rpc_task *task);
Trond Myklebust940e3312005-11-09 21:45:24 -050071static void call_transmit_status(struct rpc_task *task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072static void call_refresh(struct rpc_task *task);
73static void call_refreshresult(struct rpc_task *task);
74static void call_timeout(struct rpc_task *task);
75static void call_connect(struct rpc_task *task);
76static void call_connect_status(struct rpc_task *task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Chuck Leverb0e1c572008-05-21 17:09:19 -040078static __be32 *rpc_encode_header(struct rpc_task *task);
79static __be32 *rpc_verify_header(struct rpc_task *task);
Chuck Levercaabea82009-12-03 15:58:56 -050080static int rpc_ping(struct rpc_clnt *clnt);
Trond Myklebust64c91a12007-06-23 10:17:16 -040081
Trond Myklebust188fef12007-06-16 14:18:40 -040082static void rpc_register_client(struct rpc_clnt *clnt)
83{
Trond Myklebust2446ab62012-03-01 17:00:56 -050084 struct net *net = rpc_net_ns(clnt);
85 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
Stanislav Kinsbursky70abc492012-01-12 22:07:51 +040086
87 spin_lock(&sn->rpc_client_lock);
88 list_add(&clnt->cl_clients, &sn->all_clients);
89 spin_unlock(&sn->rpc_client_lock);
Trond Myklebust188fef12007-06-16 14:18:40 -040090}
91
92static void rpc_unregister_client(struct rpc_clnt *clnt)
93{
Trond Myklebust2446ab62012-03-01 17:00:56 -050094 struct net *net = rpc_net_ns(clnt);
95 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
Stanislav Kinsbursky70abc492012-01-12 22:07:51 +040096
97 spin_lock(&sn->rpc_client_lock);
Trond Myklebust188fef12007-06-16 14:18:40 -040098 list_del(&clnt->cl_clients);
Stanislav Kinsbursky70abc492012-01-12 22:07:51 +040099 spin_unlock(&sn->rpc_client_lock);
Trond Myklebust188fef12007-06-16 14:18:40 -0400100}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400102static void __rpc_clnt_remove_pipedir(struct rpc_clnt *clnt)
103{
Stanislav Kinsbursky30507f52012-01-11 19:18:42 +0400104 if (clnt->cl_dentry) {
Stanislav Kinsbursky80df9d22012-01-11 19:18:17 +0400105 if (clnt->cl_auth && clnt->cl_auth->au_ops->pipes_destroy)
106 clnt->cl_auth->au_ops->pipes_destroy(clnt->cl_auth);
Stanislav Kinsbursky30507f52012-01-11 19:18:42 +0400107 rpc_remove_client_dir(clnt->cl_dentry);
Stanislav Kinsbursky80df9d22012-01-11 19:18:17 +0400108 }
Stanislav Kinsbursky30507f52012-01-11 19:18:42 +0400109 clnt->cl_dentry = NULL;
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400110}
111
112static void rpc_clnt_remove_pipedir(struct rpc_clnt *clnt)
113{
Trond Myklebust2446ab62012-03-01 17:00:56 -0500114 struct net *net = rpc_net_ns(clnt);
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400115 struct super_block *pipefs_sb;
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400116
Trond Myklebust2446ab62012-03-01 17:00:56 -0500117 pipefs_sb = rpc_get_sb_net(net);
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400118 if (pipefs_sb) {
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400119 __rpc_clnt_remove_pipedir(clnt);
Trond Myklebust2446ab62012-03-01 17:00:56 -0500120 rpc_put_sb_net(net);
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400121 }
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400122}
123
124static struct dentry *rpc_setup_pipedir_sb(struct super_block *sb,
Trond Myklebust080b7942012-01-20 13:53:56 -0500125 struct rpc_clnt *clnt,
126 const char *dir_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127{
Trond Myklebustf1345852005-09-23 11:08:25 -0400128 static uint32_t clntid;
Trond Myklebust23ac6582009-08-09 15:14:25 -0400129 char name[15];
130 struct qstr q = {
131 .name = name,
132 };
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400133 struct dentry *dir, *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 int error;
135
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400136 dir = rpc_d_lookup_sb(sb, dir_name);
137 if (dir == NULL)
138 return dir;
139 for (;;) {
140 q.len = snprintf(name, sizeof(name), "clnt%x", (unsigned int)clntid++);
141 name[sizeof(name) - 1] = '\0';
142 q.hash = full_name_hash(q.name, q.len);
143 dentry = rpc_create_client_dir(dir, &q, clnt);
144 if (!IS_ERR(dentry))
145 break;
146 error = PTR_ERR(dentry);
147 if (error != -EEXIST) {
148 printk(KERN_INFO "RPC: Couldn't create pipefs entry"
149 " %s/%s, error %d\n",
150 dir_name, name, error);
151 break;
152 }
153 }
154 dput(dir);
155 return dentry;
156}
157
158static int
Trond Myklebust080b7942012-01-20 13:53:56 -0500159rpc_setup_pipedir(struct rpc_clnt *clnt, const char *dir_name)
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400160{
Trond Myklebust2446ab62012-03-01 17:00:56 -0500161 struct net *net = rpc_net_ns(clnt);
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400162 struct super_block *pipefs_sb;
Stanislav Kinsbursky30507f52012-01-11 19:18:42 +0400163 struct dentry *dentry;
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400164
Stanislav Kinsbursky30507f52012-01-11 19:18:42 +0400165 clnt->cl_dentry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 if (dir_name == NULL)
167 return 0;
Trond Myklebust2446ab62012-03-01 17:00:56 -0500168 pipefs_sb = rpc_get_sb_net(net);
Stanislav Kinsbursky70fe25b2012-01-11 19:18:34 +0400169 if (!pipefs_sb)
170 return 0;
Stanislav Kinsbursky30507f52012-01-11 19:18:42 +0400171 dentry = rpc_setup_pipedir_sb(pipefs_sb, clnt, dir_name);
Trond Myklebust2446ab62012-03-01 17:00:56 -0500172 rpc_put_sb_net(net);
Stanislav Kinsbursky30507f52012-01-11 19:18:42 +0400173 if (IS_ERR(dentry))
174 return PTR_ERR(dentry);
175 clnt->cl_dentry = dentry;
Trond Myklebust23ac6582009-08-09 15:14:25 -0400176 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177}
178
Stanislav Kinsbursky80df9d22012-01-11 19:18:17 +0400179static int __rpc_pipefs_event(struct rpc_clnt *clnt, unsigned long event,
180 struct super_block *sb)
181{
182 struct dentry *dentry;
183 int err = 0;
184
185 switch (event) {
186 case RPC_PIPEFS_MOUNT:
187 if (clnt->cl_program->pipe_dir_name == NULL)
188 break;
189 dentry = rpc_setup_pipedir_sb(sb, clnt,
190 clnt->cl_program->pipe_dir_name);
191 BUG_ON(dentry == NULL);
192 if (IS_ERR(dentry))
193 return PTR_ERR(dentry);
Stanislav Kinsbursky30507f52012-01-11 19:18:42 +0400194 clnt->cl_dentry = dentry;
Stanislav Kinsbursky80df9d22012-01-11 19:18:17 +0400195 if (clnt->cl_auth->au_ops->pipes_create) {
196 err = clnt->cl_auth->au_ops->pipes_create(clnt->cl_auth);
197 if (err)
198 __rpc_clnt_remove_pipedir(clnt);
199 }
200 break;
201 case RPC_PIPEFS_UMOUNT:
202 __rpc_clnt_remove_pipedir(clnt);
203 break;
204 default:
205 printk(KERN_ERR "%s: unknown event: %ld\n", __func__, event);
206 return -ENOTSUPP;
207 }
208 return err;
209}
210
Stanislav Kinsburskyda3b4622012-02-27 22:05:29 +0400211static struct rpc_clnt *rpc_get_client_for_event(struct net *net, int event)
212{
213 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
214 struct rpc_clnt *clnt;
215
216 spin_lock(&sn->rpc_client_lock);
217 list_for_each_entry(clnt, &sn->all_clients, cl_clients) {
218 if (((event == RPC_PIPEFS_MOUNT) && clnt->cl_dentry) ||
219 ((event == RPC_PIPEFS_UMOUNT) && !clnt->cl_dentry))
220 continue;
Stanislav Kinsburskya4dff1b2012-04-20 18:11:02 +0400221 if (atomic_inc_not_zero(&clnt->cl_count) == 0)
222 continue;
Stanislav Kinsburskyda3b4622012-02-27 22:05:29 +0400223 spin_unlock(&sn->rpc_client_lock);
224 return clnt;
225 }
226 spin_unlock(&sn->rpc_client_lock);
227 return NULL;
228}
229
Stanislav Kinsbursky80df9d22012-01-11 19:18:17 +0400230static int rpc_pipefs_event(struct notifier_block *nb, unsigned long event,
231 void *ptr)
232{
233 struct super_block *sb = ptr;
234 struct rpc_clnt *clnt;
235 int error = 0;
Stanislav Kinsbursky80df9d22012-01-11 19:18:17 +0400236
Stanislav Kinsburskyda3b4622012-02-27 22:05:29 +0400237 while ((clnt = rpc_get_client_for_event(sb->s_fs_info, event))) {
Stanislav Kinsbursky80df9d22012-01-11 19:18:17 +0400238 error = __rpc_pipefs_event(clnt, event, sb);
Stanislav Kinsburskyda3b4622012-02-27 22:05:29 +0400239 rpc_release_client(clnt);
Stanislav Kinsbursky80df9d22012-01-11 19:18:17 +0400240 if (error)
241 break;
242 }
Stanislav Kinsbursky80df9d22012-01-11 19:18:17 +0400243 return error;
244}
245
246static struct notifier_block rpc_clients_block = {
247 .notifier_call = rpc_pipefs_event,
Stanislav Kinsburskyeee17322012-01-10 16:13:19 +0400248 .priority = SUNRPC_PIPEFS_RPC_PRIO,
Stanislav Kinsbursky80df9d22012-01-11 19:18:17 +0400249};
250
251int rpc_clients_notifier_register(void)
252{
253 return rpc_pipefs_notifier_register(&rpc_clients_block);
254}
255
256void rpc_clients_notifier_unregister(void)
257{
258 return rpc_pipefs_notifier_unregister(&rpc_clients_block);
259}
260
Trond Myklebust698b6d02007-12-20 16:03:53 -0500261static struct rpc_clnt * rpc_new_client(const struct rpc_create_args *args, struct rpc_xprt *xprt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
Trond Myklebusta613fa12012-01-20 13:53:56 -0500263 const struct rpc_program *program = args->program;
264 const struct rpc_version *version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 struct rpc_clnt *clnt = NULL;
J. Bruce Fields6a192752005-06-22 17:16:23 +0000266 struct rpc_auth *auth;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 int err;
Chuck Lever06b8d252007-09-11 18:00:20 -0400268
269 /* sanity check the name before trying to print it */
Chuck Lever46121cf2007-01-31 12:14:08 -0500270 dprintk("RPC: creating %s client for %s (xprt %p)\n",
Trond Myklebust698b6d02007-12-20 16:03:53 -0500271 program->name, args->servername, xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Trond Myklebust4ada5392007-06-14 17:26:17 -0400273 err = rpciod_up();
274 if (err)
275 goto out_no_rpciod;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 err = -EINVAL;
277 if (!xprt)
Adrian Bunk712917d2006-03-13 21:20:47 -0800278 goto out_no_xprt;
Trond Myklebust698b6d02007-12-20 16:03:53 -0500279
280 if (args->version >= program->nrvers)
281 goto out_err;
282 version = program->version[args->version];
283 if (version == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 goto out_err;
285
286 err = -ENOMEM;
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700287 clnt = kzalloc(sizeof(*clnt), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 if (!clnt)
289 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 clnt->cl_parent = clnt;
291
Trond Myklebust2446ab62012-03-01 17:00:56 -0500292 rcu_assign_pointer(clnt->cl_xprt, xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 clnt->cl_procinfo = version->procs;
294 clnt->cl_maxproc = version->nrprocs;
295 clnt->cl_protname = program->name;
Benny Halevyd5b337b2008-09-28 09:21:26 +0300296 clnt->cl_prog = args->prognumber ? : program->number;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 clnt->cl_vers = version->number;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 clnt->cl_stats = program->stats;
Chuck Lever11c556b2006-03-20 13:44:22 -0500299 clnt->cl_metrics = rpc_alloc_iostats(clnt);
Trond Myklebust23bf85b2006-11-21 10:40:23 -0500300 err = -ENOMEM;
301 if (clnt->cl_metrics == NULL)
302 goto out_no_stats;
Trond Myklebust3e32a5d2006-11-16 11:37:27 -0500303 clnt->cl_program = program;
Trond Myklebust6529eba2007-06-14 16:40:14 -0400304 INIT_LIST_HEAD(&clnt->cl_tasks);
Trond Myklebust4bef61f2007-06-16 14:17:01 -0400305 spin_lock_init(&clnt->cl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
Trond Myklebust2446ab62012-03-01 17:00:56 -0500307 if (!xprt_bound(xprt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 clnt->cl_autobind = 1;
309
Trond Myklebustba7392b2007-12-20 16:03:55 -0500310 clnt->cl_timeout = xprt->timeout;
311 if (args->timeout != NULL) {
312 memcpy(&clnt->cl_timeout_default, args->timeout,
313 sizeof(clnt->cl_timeout_default));
314 clnt->cl_timeout = &clnt->cl_timeout_default;
315 }
316
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 clnt->cl_rtt = &clnt->cl_rtt_default;
Trond Myklebustba7392b2007-12-20 16:03:55 -0500318 rpc_init_rtt(&clnt->cl_rtt_default, clnt->cl_timeout->to_initval);
Olga Kornievskaia608207e2008-12-23 16:17:40 -0500319 clnt->cl_principal = NULL;
320 if (args->client_name) {
321 clnt->cl_principal = kstrdup(args->client_name, GFP_KERNEL);
322 if (!clnt->cl_principal)
323 goto out_no_principal;
324 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Trond Myklebust006abe82010-09-12 19:55:25 -0400326 atomic_set(&clnt->cl_count, 1);
Trond Myklebust34f52e32007-06-14 16:40:31 -0400327
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 err = rpc_setup_pipedir(clnt, program->pipe_dir_name);
329 if (err < 0)
330 goto out_no_path;
331
Trond Myklebust698b6d02007-12-20 16:03:53 -0500332 auth = rpcauth_create(args->authflavor, clnt);
J. Bruce Fields6a192752005-06-22 17:16:23 +0000333 if (IS_ERR(auth)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 printk(KERN_INFO "RPC: Couldn't create auth handle (flavor %u)\n",
Trond Myklebust698b6d02007-12-20 16:03:53 -0500335 args->authflavor);
J. Bruce Fields6a192752005-06-22 17:16:23 +0000336 err = PTR_ERR(auth);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 goto out_no_auth;
338 }
339
340 /* save the nodename */
Cedric Le Goater63ffc232008-10-03 23:41:51 -0400341 clnt->cl_nodelen = strlen(init_utsname()->nodename);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 if (clnt->cl_nodelen > UNX_MAXNODENAME)
343 clnt->cl_nodelen = UNX_MAXNODENAME;
Cedric Le Goater63ffc232008-10-03 23:41:51 -0400344 memcpy(clnt->cl_nodename, init_utsname()->nodename, clnt->cl_nodelen);
Trond Myklebust6529eba2007-06-14 16:40:14 -0400345 rpc_register_client(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 return clnt;
347
348out_no_auth:
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400349 rpc_clnt_remove_pipedir(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350out_no_path:
Olga Kornievskaia608207e2008-12-23 16:17:40 -0500351 kfree(clnt->cl_principal);
352out_no_principal:
Trond Myklebust23bf85b2006-11-21 10:40:23 -0500353 rpc_free_iostats(clnt->cl_metrics);
354out_no_stats:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 kfree(clnt);
356out_err:
Trond Myklebust6b6ca862006-09-05 12:55:57 -0400357 xprt_put(xprt);
Adrian Bunk712917d2006-03-13 21:20:47 -0800358out_no_xprt:
Trond Myklebust4ada5392007-06-14 17:26:17 -0400359 rpciod_down();
360out_no_rpciod:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 return ERR_PTR(err);
362}
363
Chuck Leverc2866762006-08-22 20:06:20 -0400364/*
365 * rpc_create - create an RPC client and transport with one call
366 * @args: rpc_clnt create argument structure
367 *
368 * Creates and initializes an RPC transport and an RPC client.
369 *
370 * It can ping the server in order to determine if it is up, and to see if
371 * it supports this program and version. RPC_CLNT_CREATE_NOPING disables
372 * this behavior so asynchronous tasks can also use rpc_create.
373 */
374struct rpc_clnt *rpc_create(struct rpc_create_args *args)
375{
376 struct rpc_xprt *xprt;
377 struct rpc_clnt *clnt;
\"Talpey, Thomas\3c341b0b2007-09-10 13:47:07 -0400378 struct xprt_create xprtargs = {
Pavel Emelyanov9a23e332010-09-29 16:05:12 +0400379 .net = args->net,
\"Talpey, Thomas\4fa016e2007-09-10 13:47:57 -0400380 .ident = args->protocol,
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +0200381 .srcaddr = args->saddress,
Frank van Maarseveen96802a02007-07-08 13:08:54 +0200382 .dstaddr = args->address,
383 .addrlen = args->addrsize,
Trond Myklebust4e0038b2012-03-01 17:01:05 -0500384 .servername = args->servername,
Alexandros Batsakisf300bab2009-09-10 17:33:30 +0300385 .bc_xprt = args->bc_xprt,
Frank van Maarseveen96802a02007-07-08 13:08:54 +0200386 };
Chuck Lever510deb02007-12-10 14:56:24 -0500387 char servername[48];
Chuck Leverc2866762006-08-22 20:06:20 -0400388
Chuck Leverc2866762006-08-22 20:06:20 -0400389 /*
Chuck Lever43780b82007-07-01 12:13:22 -0400390 * If the caller chooses not to specify a hostname, whip
391 * up a string representation of the passed-in address.
392 */
Trond Myklebust4e0038b2012-03-01 17:01:05 -0500393 if (xprtargs.servername == NULL) {
Chuck Lever176e21e2011-05-09 15:22:44 -0400394 struct sockaddr_un *sun =
395 (struct sockaddr_un *)args->address;
Chuck Leverda09eb92011-05-09 15:22:25 -0400396 struct sockaddr_in *sin =
397 (struct sockaddr_in *)args->address;
398 struct sockaddr_in6 *sin6 =
399 (struct sockaddr_in6 *)args->address;
400
Chuck Lever510deb02007-12-10 14:56:24 -0500401 servername[0] = '\0';
402 switch (args->address->sa_family) {
Chuck Lever176e21e2011-05-09 15:22:44 -0400403 case AF_LOCAL:
404 snprintf(servername, sizeof(servername), "%s",
405 sun->sun_path);
406 break;
Chuck Leverda09eb92011-05-09 15:22:25 -0400407 case AF_INET:
Harvey Harrison21454aa2008-10-31 00:54:56 -0700408 snprintf(servername, sizeof(servername), "%pI4",
409 &sin->sin_addr.s_addr);
Chuck Lever510deb02007-12-10 14:56:24 -0500410 break;
Chuck Leverda09eb92011-05-09 15:22:25 -0400411 case AF_INET6:
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700412 snprintf(servername, sizeof(servername), "%pI6",
Chuck Leverda09eb92011-05-09 15:22:25 -0400413 &sin6->sin6_addr);
Chuck Lever510deb02007-12-10 14:56:24 -0500414 break;
Chuck Lever510deb02007-12-10 14:56:24 -0500415 default:
416 /* caller wants default server name, but
417 * address family isn't recognized. */
418 return ERR_PTR(-EINVAL);
419 }
Trond Myklebust4e0038b2012-03-01 17:01:05 -0500420 xprtargs.servername = servername;
Chuck Lever43780b82007-07-01 12:13:22 -0400421 }
422
Chuck Lever510deb02007-12-10 14:56:24 -0500423 xprt = xprt_create_transport(&xprtargs);
424 if (IS_ERR(xprt))
425 return (struct rpc_clnt *)xprt;
426
Chuck Lever43780b82007-07-01 12:13:22 -0400427 /*
Chuck Leverc2866762006-08-22 20:06:20 -0400428 * By default, kernel RPC client connects from a reserved port.
429 * CAP_NET_BIND_SERVICE will not be set for unprivileged requesters,
430 * but it is always enabled for rpciod, which handles the connect
431 * operation.
432 */
433 xprt->resvport = 1;
434 if (args->flags & RPC_CLNT_CREATE_NONPRIVPORT)
435 xprt->resvport = 0;
436
Trond Myklebust698b6d02007-12-20 16:03:53 -0500437 clnt = rpc_new_client(args, xprt);
Chuck Leverc2866762006-08-22 20:06:20 -0400438 if (IS_ERR(clnt))
439 return clnt;
440
441 if (!(args->flags & RPC_CLNT_CREATE_NOPING)) {
Chuck Levercaabea82009-12-03 15:58:56 -0500442 int err = rpc_ping(clnt);
Chuck Leverc2866762006-08-22 20:06:20 -0400443 if (err != 0) {
444 rpc_shutdown_client(clnt);
445 return ERR_PTR(err);
446 }
447 }
448
449 clnt->cl_softrtry = 1;
450 if (args->flags & RPC_CLNT_CREATE_HARDRTRY)
451 clnt->cl_softrtry = 0;
452
Chuck Leverc2866762006-08-22 20:06:20 -0400453 if (args->flags & RPC_CLNT_CREATE_AUTOBIND)
454 clnt->cl_autobind = 1;
Chuck Lever43d78ef2007-02-06 18:26:11 -0500455 if (args->flags & RPC_CLNT_CREATE_DISCRTRY)
456 clnt->cl_discrtry = 1;
Olga Kornievskaiab6b61522008-06-09 16:51:31 -0400457 if (!(args->flags & RPC_CLNT_CREATE_QUIET))
458 clnt->cl_chatty = 1;
Chuck Leverc2866762006-08-22 20:06:20 -0400459
460 return clnt;
461}
Chuck Leverb86acd52006-08-22 20:06:22 -0400462EXPORT_SYMBOL_GPL(rpc_create);
Chuck Leverc2866762006-08-22 20:06:20 -0400463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464/*
465 * This function clones the RPC client structure. It allows us to share the
466 * same transport while varying parameters such as the authentication
467 * flavour.
468 */
469struct rpc_clnt *
470rpc_clone_client(struct rpc_clnt *clnt)
471{
472 struct rpc_clnt *new;
Trond Myklebust2446ab62012-03-01 17:00:56 -0500473 struct rpc_xprt *xprt;
Trond Myklebust3e32a5d2006-11-16 11:37:27 -0500474 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
Arnaldo Carvalho de Meloe69062b2006-11-21 01:21:34 -0200476 new = kmemdup(clnt, sizeof(*new), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 if (!new)
478 goto out_no_clnt;
Trond Myklebustd431a5552007-06-17 17:07:54 -0400479 new->cl_parent = clnt;
480 /* Turn off autobind on clones */
481 new->cl_autobind = 0;
482 INIT_LIST_HEAD(&new->cl_tasks);
483 spin_lock_init(&new->cl_lock);
Trond Myklebustba7392b2007-12-20 16:03:55 -0500484 rpc_init_rtt(&new->cl_rtt_default, clnt->cl_timeout->to_initval);
Trond Myklebust23bf85b2006-11-21 10:40:23 -0500485 new->cl_metrics = rpc_alloc_iostats(clnt);
486 if (new->cl_metrics == NULL)
487 goto out_no_stats;
Olga Kornievskaia608207e2008-12-23 16:17:40 -0500488 if (clnt->cl_principal) {
489 new->cl_principal = kstrdup(clnt->cl_principal, GFP_KERNEL);
490 if (new->cl_principal == NULL)
491 goto out_no_principal;
492 }
Trond Myklebust2446ab62012-03-01 17:00:56 -0500493 rcu_read_lock();
494 xprt = xprt_get(rcu_dereference(clnt->cl_xprt));
495 rcu_read_unlock();
496 if (xprt == NULL)
497 goto out_no_transport;
498 rcu_assign_pointer(new->cl_xprt, xprt);
Trond Myklebust006abe82010-09-12 19:55:25 -0400499 atomic_set(&new->cl_count, 1);
Trond Myklebust3e32a5d2006-11-16 11:37:27 -0500500 err = rpc_setup_pipedir(new, clnt->cl_program->pipe_dir_name);
501 if (err != 0)
502 goto out_no_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 if (new->cl_auth)
504 atomic_inc(&new->cl_auth->au_count);
Trond Myklebust006abe82010-09-12 19:55:25 -0400505 atomic_inc(&clnt->cl_count);
Trond Myklebust6529eba2007-06-14 16:40:14 -0400506 rpc_register_client(new);
Trond Myklebust4ada5392007-06-14 17:26:17 -0400507 rpciod_up();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 return new;
Trond Myklebust3e32a5d2006-11-16 11:37:27 -0500509out_no_path:
Trond Myklebust2446ab62012-03-01 17:00:56 -0500510 xprt_put(xprt);
511out_no_transport:
Olga Kornievskaia608207e2008-12-23 16:17:40 -0500512 kfree(new->cl_principal);
513out_no_principal:
Trond Myklebust3e32a5d2006-11-16 11:37:27 -0500514 rpc_free_iostats(new->cl_metrics);
Trond Myklebust23bf85b2006-11-21 10:40:23 -0500515out_no_stats:
516 kfree(new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517out_no_clnt:
Harvey Harrison0dc47872008-03-05 20:47:47 -0800518 dprintk("RPC: %s: returned error %d\n", __func__, err);
Trond Myklebust3e32a5d2006-11-16 11:37:27 -0500519 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400521EXPORT_SYMBOL_GPL(rpc_clone_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
523/*
Trond Myklebust58f96122010-07-31 14:29:08 -0400524 * Kill all tasks for the given client.
525 * XXX: kill their descendants as well?
526 */
527void rpc_killall_tasks(struct rpc_clnt *clnt)
528{
529 struct rpc_task *rovr;
530
531
532 if (list_empty(&clnt->cl_tasks))
533 return;
534 dprintk("RPC: killing all tasks for client %p\n", clnt);
535 /*
536 * Spin lock all_tasks to prevent changes...
537 */
538 spin_lock(&clnt->cl_lock);
539 list_for_each_entry(rovr, &clnt->cl_tasks, tk_task) {
540 if (!RPC_IS_ACTIVATED(rovr))
541 continue;
542 if (!(rovr->tk_flags & RPC_TASK_KILLED)) {
543 rovr->tk_flags |= RPC_TASK_KILLED;
544 rpc_exit(rovr, -EIO);
Stanislav Kinsbursky8e26de22011-03-17 18:54:23 +0300545 if (RPC_IS_QUEUED(rovr))
546 rpc_wake_up_queued_task(rovr->tk_waitqueue,
547 rovr);
Trond Myklebust58f96122010-07-31 14:29:08 -0400548 }
549 }
550 spin_unlock(&clnt->cl_lock);
551}
552EXPORT_SYMBOL_GPL(rpc_killall_tasks);
553
554/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 * Properly shut down an RPC client, terminating all outstanding
Trond Myklebust90c57552007-06-09 19:49:36 -0400556 * requests.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 */
Trond Myklebust4c402b42007-06-14 16:40:32 -0400558void rpc_shutdown_client(struct rpc_clnt *clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
Trond Myklebust4e0038b2012-03-01 17:01:05 -0500560 dprintk_rcu("RPC: shutting down %s client for %s\n",
561 clnt->cl_protname,
562 rcu_dereference(clnt->cl_xprt)->servername);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Trond Myklebust34f52e32007-06-14 16:40:31 -0400564 while (!list_empty(&clnt->cl_tasks)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 rpc_killall_tasks(clnt);
Ingo Molnar532347e2006-01-09 20:52:53 -0800566 wait_event_timeout(destroy_wait,
Trond Myklebust34f52e32007-06-14 16:40:31 -0400567 list_empty(&clnt->cl_tasks), 1*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 }
569
Trond Myklebust4c402b42007-06-14 16:40:32 -0400570 rpc_release_client(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400572EXPORT_SYMBOL_GPL(rpc_shutdown_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
574/*
Trond Myklebust34f52e32007-06-14 16:40:31 -0400575 * Free an RPC client
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 */
Trond Myklebust34f52e32007-06-14 16:40:31 -0400577static void
Trond Myklebust006abe82010-09-12 19:55:25 -0400578rpc_free_client(struct rpc_clnt *clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579{
Trond Myklebust4e0038b2012-03-01 17:01:05 -0500580 dprintk_rcu("RPC: destroying %s client for %s\n",
581 clnt->cl_protname,
582 rcu_dereference(clnt->cl_xprt)->servername);
Trond Myklebust6eac7d32012-01-20 13:53:37 -0500583 if (clnt->cl_parent != clnt)
Trond Myklebust8ad7c892007-06-14 16:40:32 -0400584 rpc_release_client(clnt->cl_parent);
Trond Myklebust6529eba2007-06-14 16:40:14 -0400585 rpc_unregister_client(clnt);
Stanislav Kinsburskyf5131252012-01-11 19:18:26 +0400586 rpc_clnt_remove_pipedir(clnt);
Chuck Lever11c556b2006-03-20 13:44:22 -0500587 rpc_free_iostats(clnt->cl_metrics);
Olga Kornievskaia608207e2008-12-23 16:17:40 -0500588 kfree(clnt->cl_principal);
Chuck Lever11c556b2006-03-20 13:44:22 -0500589 clnt->cl_metrics = NULL;
Trond Myklebust2446ab62012-03-01 17:00:56 -0500590 xprt_put(rcu_dereference_raw(clnt->cl_xprt));
Trond Myklebust4ada5392007-06-14 17:26:17 -0400591 rpciod_down();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 kfree(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593}
594
595/*
Trond Myklebust1dd17ec2007-06-26 16:57:41 -0400596 * Free an RPC client
597 */
598static void
Trond Myklebust006abe82010-09-12 19:55:25 -0400599rpc_free_auth(struct rpc_clnt *clnt)
Trond Myklebust1dd17ec2007-06-26 16:57:41 -0400600{
Trond Myklebust1dd17ec2007-06-26 16:57:41 -0400601 if (clnt->cl_auth == NULL) {
Trond Myklebust006abe82010-09-12 19:55:25 -0400602 rpc_free_client(clnt);
Trond Myklebust1dd17ec2007-06-26 16:57:41 -0400603 return;
604 }
605
606 /*
607 * Note: RPCSEC_GSS may need to send NULL RPC calls in order to
608 * release remaining GSS contexts. This mechanism ensures
609 * that it can do so safely.
610 */
Trond Myklebust006abe82010-09-12 19:55:25 -0400611 atomic_inc(&clnt->cl_count);
Trond Myklebust1dd17ec2007-06-26 16:57:41 -0400612 rpcauth_release(clnt->cl_auth);
613 clnt->cl_auth = NULL;
Trond Myklebust006abe82010-09-12 19:55:25 -0400614 if (atomic_dec_and_test(&clnt->cl_count))
615 rpc_free_client(clnt);
Trond Myklebust1dd17ec2007-06-26 16:57:41 -0400616}
617
618/*
Trond Myklebust34f52e32007-06-14 16:40:31 -0400619 * Release reference to the RPC client
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 */
621void
622rpc_release_client(struct rpc_clnt *clnt)
623{
Trond Myklebust34f52e32007-06-14 16:40:31 -0400624 dprintk("RPC: rpc_release_client(%p)\n", clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
Trond Myklebust34f52e32007-06-14 16:40:31 -0400626 if (list_empty(&clnt->cl_tasks))
627 wake_up(&destroy_wait);
Trond Myklebust006abe82010-09-12 19:55:25 -0400628 if (atomic_dec_and_test(&clnt->cl_count))
629 rpc_free_auth(clnt);
Trond Myklebust34f52e32007-06-14 16:40:31 -0400630}
631
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000632/**
633 * rpc_bind_new_program - bind a new RPC program to an existing client
Randy Dunlap65b6e422008-02-13 15:03:23 -0800634 * @old: old rpc_client
635 * @program: rpc program to set
636 * @vers: rpc program version
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000637 *
638 * Clones the rpc client and sets up a new RPC program. This is mainly
639 * of use for enabling different RPC programs to share the same transport.
640 * The Sun NFSv2/v3 ACL protocol can do this.
641 */
642struct rpc_clnt *rpc_bind_new_program(struct rpc_clnt *old,
Trond Myklebusta613fa12012-01-20 13:53:56 -0500643 const struct rpc_program *program,
Chuck Lever89eb21c2007-09-11 18:00:09 -0400644 u32 vers)
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000645{
646 struct rpc_clnt *clnt;
Trond Myklebusta613fa12012-01-20 13:53:56 -0500647 const struct rpc_version *version;
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000648 int err;
649
650 BUG_ON(vers >= program->nrvers || !program->version[vers]);
651 version = program->version[vers];
652 clnt = rpc_clone_client(old);
653 if (IS_ERR(clnt))
654 goto out;
655 clnt->cl_procinfo = version->procs;
656 clnt->cl_maxproc = version->nrprocs;
657 clnt->cl_protname = program->name;
658 clnt->cl_prog = program->number;
659 clnt->cl_vers = version->number;
660 clnt->cl_stats = program->stats;
Chuck Levercaabea82009-12-03 15:58:56 -0500661 err = rpc_ping(clnt);
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000662 if (err != 0) {
663 rpc_shutdown_client(clnt);
664 clnt = ERR_PTR(err);
665 }
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800666out:
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000667 return clnt;
668}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400669EXPORT_SYMBOL_GPL(rpc_bind_new_program);
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000670
Trond Myklebust58f96122010-07-31 14:29:08 -0400671void rpc_task_release_client(struct rpc_task *task)
672{
673 struct rpc_clnt *clnt = task->tk_client;
674
675 if (clnt != NULL) {
676 /* Remove from client task list */
677 spin_lock(&clnt->cl_lock);
678 list_del(&task->tk_task);
679 spin_unlock(&clnt->cl_lock);
680 task->tk_client = NULL;
681
682 rpc_release_client(clnt);
683 }
684}
685
686static
687void rpc_task_set_client(struct rpc_task *task, struct rpc_clnt *clnt)
688{
689 if (clnt != NULL) {
690 rpc_task_release_client(task);
691 task->tk_client = clnt;
Trond Myklebust006abe82010-09-12 19:55:25 -0400692 atomic_inc(&clnt->cl_count);
Trond Myklebust58f96122010-07-31 14:29:08 -0400693 if (clnt->cl_softrtry)
694 task->tk_flags |= RPC_TASK_SOFT;
695 /* Add to the client's list of all tasks */
696 spin_lock(&clnt->cl_lock);
697 list_add_tail(&task->tk_task, &clnt->cl_tasks);
698 spin_unlock(&clnt->cl_lock);
699 }
700}
701
Andy Adamsoncbdabc72011-03-01 01:34:20 +0000702void rpc_task_reset_client(struct rpc_task *task, struct rpc_clnt *clnt)
703{
704 rpc_task_release_client(task);
705 rpc_task_set_client(task, clnt);
706}
707EXPORT_SYMBOL_GPL(rpc_task_reset_client);
708
709
Trond Myklebust58f96122010-07-31 14:29:08 -0400710static void
711rpc_task_set_rpc_message(struct rpc_task *task, const struct rpc_message *msg)
712{
713 if (msg != NULL) {
714 task->tk_msg.rpc_proc = msg->rpc_proc;
715 task->tk_msg.rpc_argp = msg->rpc_argp;
716 task->tk_msg.rpc_resp = msg->rpc_resp;
Trond Myklebusta17c2152010-07-31 14:29:08 -0400717 if (msg->rpc_cred != NULL)
718 task->tk_msg.rpc_cred = get_rpccred(msg->rpc_cred);
Trond Myklebust58f96122010-07-31 14:29:08 -0400719 }
720}
721
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722/*
723 * Default callback for async RPC calls
724 */
725static void
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100726rpc_default_callback(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727{
728}
729
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100730static const struct rpc_call_ops rpc_default_ops = {
731 .rpc_call_done = rpc_default_callback,
732};
733
Trond Myklebustc970aa82007-07-14 15:39:59 -0400734/**
735 * rpc_run_task - Allocate a new RPC task, then run rpc_execute against it
736 * @task_setup_data: pointer to task initialisation data
737 */
738struct rpc_task *rpc_run_task(const struct rpc_task_setup *task_setup_data)
Trond Myklebust6e5b70e2007-06-12 10:02:37 -0400739{
Trond Myklebust19445b92010-04-16 16:41:10 -0400740 struct rpc_task *task;
Trond Myklebust6e5b70e2007-06-12 10:02:37 -0400741
Trond Myklebust84115e12007-07-14 15:39:59 -0400742 task = rpc_new_task(task_setup_data);
Trond Myklebust19445b92010-04-16 16:41:10 -0400743 if (IS_ERR(task))
Trond Myklebust50859252007-10-25 18:19:37 -0400744 goto out;
Trond Myklebust6e5b70e2007-06-12 10:02:37 -0400745
Trond Myklebust58f96122010-07-31 14:29:08 -0400746 rpc_task_set_client(task, task_setup_data->rpc_client);
747 rpc_task_set_rpc_message(task, task_setup_data->rpc_message);
748
Trond Myklebust58f96122010-07-31 14:29:08 -0400749 if (task->tk_action == NULL)
750 rpc_call_start(task);
751
Trond Myklebust6e5b70e2007-06-12 10:02:37 -0400752 atomic_inc(&task->tk_count);
753 rpc_execute(task);
Trond Myklebust6e5b70e2007-06-12 10:02:37 -0400754out:
Trond Myklebust19445b92010-04-16 16:41:10 -0400755 return task;
Trond Myklebust6e5b70e2007-06-12 10:02:37 -0400756}
Trond Myklebustc970aa82007-07-14 15:39:59 -0400757EXPORT_SYMBOL_GPL(rpc_run_task);
Trond Myklebust6e5b70e2007-06-12 10:02:37 -0400758
759/**
760 * rpc_call_sync - Perform a synchronous RPC call
761 * @clnt: pointer to RPC client
762 * @msg: RPC call parameters
763 * @flags: RPC call flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 */
Trond Myklebustcbc20052008-02-14 11:11:30 -0500765int rpc_call_sync(struct rpc_clnt *clnt, const struct rpc_message *msg, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766{
767 struct rpc_task *task;
Trond Myklebust84115e12007-07-14 15:39:59 -0400768 struct rpc_task_setup task_setup_data = {
769 .rpc_client = clnt,
770 .rpc_message = msg,
771 .callback_ops = &rpc_default_ops,
772 .flags = flags,
773 };
Trond Myklebust6e5b70e2007-06-12 10:02:37 -0400774 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 BUG_ON(flags & RPC_TASK_ASYNC);
777
Trond Myklebustc970aa82007-07-14 15:39:59 -0400778 task = rpc_run_task(&task_setup_data);
Trond Myklebust6e5b70e2007-06-12 10:02:37 -0400779 if (IS_ERR(task))
780 return PTR_ERR(task);
Trond Myklebuste60859a2006-01-03 09:55:10 +0100781 status = task->tk_status;
Trond Myklebustbde8f002007-01-24 11:54:53 -0800782 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 return status;
784}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400785EXPORT_SYMBOL_GPL(rpc_call_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
Trond Myklebust6e5b70e2007-06-12 10:02:37 -0400787/**
788 * rpc_call_async - Perform an asynchronous RPC call
789 * @clnt: pointer to RPC client
790 * @msg: RPC call parameters
791 * @flags: RPC call flags
Randy Dunlap65b6e422008-02-13 15:03:23 -0800792 * @tk_ops: RPC call ops
Trond Myklebust6e5b70e2007-06-12 10:02:37 -0400793 * @data: user call data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 */
795int
Trond Myklebustcbc20052008-02-14 11:11:30 -0500796rpc_call_async(struct rpc_clnt *clnt, const struct rpc_message *msg, int flags,
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100797 const struct rpc_call_ops *tk_ops, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798{
799 struct rpc_task *task;
Trond Myklebust84115e12007-07-14 15:39:59 -0400800 struct rpc_task_setup task_setup_data = {
801 .rpc_client = clnt,
802 .rpc_message = msg,
803 .callback_ops = tk_ops,
804 .callback_data = data,
805 .flags = flags|RPC_TASK_ASYNC,
806 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
Trond Myklebustc970aa82007-07-14 15:39:59 -0400808 task = rpc_run_task(&task_setup_data);
Trond Myklebust6e5b70e2007-06-12 10:02:37 -0400809 if (IS_ERR(task))
810 return PTR_ERR(task);
811 rpc_put_task(task);
812 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400814EXPORT_SYMBOL_GPL(rpc_call_async);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
Trond Myklebust9e00abc2011-07-13 19:20:49 -0400816#if defined(CONFIG_SUNRPC_BACKCHANNEL)
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -0400817/**
818 * rpc_run_bc_task - Allocate a new RPC task for backchannel use, then run
819 * rpc_execute against it
Jaswinder Singh Rajput7a73fdd2009-09-24 14:58:42 -0400820 * @req: RPC request
821 * @tk_ops: RPC call ops
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -0400822 */
823struct rpc_task *rpc_run_bc_task(struct rpc_rqst *req,
Jaswinder Singh Rajput7a73fdd2009-09-24 14:58:42 -0400824 const struct rpc_call_ops *tk_ops)
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -0400825{
826 struct rpc_task *task;
827 struct xdr_buf *xbufp = &req->rq_snd_buf;
828 struct rpc_task_setup task_setup_data = {
829 .callback_ops = tk_ops,
830 };
831
832 dprintk("RPC: rpc_run_bc_task req= %p\n", req);
833 /*
834 * Create an rpc_task to send the data
835 */
836 task = rpc_new_task(&task_setup_data);
Trond Myklebust19445b92010-04-16 16:41:10 -0400837 if (IS_ERR(task)) {
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -0400838 xprt_free_bc_request(req);
839 goto out;
840 }
841 task->tk_rqstp = req;
842
843 /*
844 * Set up the xdr_buf length.
845 * This also indicates that the buffer is XDR encoded already.
846 */
847 xbufp->len = xbufp->head[0].iov_len + xbufp->page_len +
848 xbufp->tail[0].iov_len;
849
850 task->tk_action = call_bc_transmit;
851 atomic_inc(&task->tk_count);
852 BUG_ON(atomic_read(&task->tk_count) != 2);
853 rpc_execute(task);
854
855out:
856 dprintk("RPC: rpc_run_bc_task: task= %p\n", task);
857 return task;
858}
Trond Myklebust9e00abc2011-07-13 19:20:49 -0400859#endif /* CONFIG_SUNRPC_BACKCHANNEL */
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -0400860
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861void
Trond Myklebust77de2c52007-10-25 18:40:21 -0400862rpc_call_start(struct rpc_task *task)
863{
864 task->tk_action = call_start;
865}
866EXPORT_SYMBOL_GPL(rpc_call_start);
867
Chuck Levered394402006-08-22 20:06:17 -0400868/**
869 * rpc_peeraddr - extract remote peer address from clnt's xprt
870 * @clnt: RPC client structure
871 * @buf: target buffer
Randy Dunlap65b6e422008-02-13 15:03:23 -0800872 * @bufsize: length of target buffer
Chuck Levered394402006-08-22 20:06:17 -0400873 *
874 * Returns the number of bytes that are actually in the stored address.
875 */
876size_t rpc_peeraddr(struct rpc_clnt *clnt, struct sockaddr *buf, size_t bufsize)
877{
878 size_t bytes;
Trond Myklebust2446ab62012-03-01 17:00:56 -0500879 struct rpc_xprt *xprt;
Chuck Levered394402006-08-22 20:06:17 -0400880
Trond Myklebust2446ab62012-03-01 17:00:56 -0500881 rcu_read_lock();
882 xprt = rcu_dereference(clnt->cl_xprt);
883
884 bytes = xprt->addrlen;
Chuck Levered394402006-08-22 20:06:17 -0400885 if (bytes > bufsize)
886 bytes = bufsize;
Trond Myklebust2446ab62012-03-01 17:00:56 -0500887 memcpy(buf, &xprt->addr, bytes);
888 rcu_read_unlock();
889
890 return bytes;
Chuck Levered394402006-08-22 20:06:17 -0400891}
Chuck Leverb86acd52006-08-22 20:06:22 -0400892EXPORT_SYMBOL_GPL(rpc_peeraddr);
Chuck Levered394402006-08-22 20:06:17 -0400893
Chuck Leverf425eba2006-08-22 20:06:18 -0400894/**
895 * rpc_peeraddr2str - return remote peer address in printable format
896 * @clnt: RPC client structure
897 * @format: address format
898 *
Trond Myklebust2446ab62012-03-01 17:00:56 -0500899 * NB: the lifetime of the memory referenced by the returned pointer is
900 * the same as the rpc_xprt itself. As long as the caller uses this
901 * pointer, it must hold the RCU read lock.
Chuck Leverf425eba2006-08-22 20:06:18 -0400902 */
Chuck Leverb454ae92008-01-07 18:34:48 -0500903const char *rpc_peeraddr2str(struct rpc_clnt *clnt,
904 enum rpc_display_format_t format)
Chuck Leverf425eba2006-08-22 20:06:18 -0400905{
Trond Myklebust2446ab62012-03-01 17:00:56 -0500906 struct rpc_xprt *xprt;
907
908 xprt = rcu_dereference(clnt->cl_xprt);
Chuck Lever7559c7a2006-12-05 16:35:37 -0500909
910 if (xprt->address_strings[format] != NULL)
911 return xprt->address_strings[format];
912 else
913 return "unprintable";
Chuck Leverf425eba2006-08-22 20:06:18 -0400914}
Chuck Leverb86acd52006-08-22 20:06:22 -0400915EXPORT_SYMBOL_GPL(rpc_peeraddr2str);
Chuck Leverf425eba2006-08-22 20:06:18 -0400916
Chuck Lever2e738fd2012-03-01 17:01:14 -0500917static const struct sockaddr_in rpc_inaddr_loopback = {
918 .sin_family = AF_INET,
919 .sin_addr.s_addr = htonl(INADDR_ANY),
920};
921
922static const struct sockaddr_in6 rpc_in6addr_loopback = {
923 .sin6_family = AF_INET6,
924 .sin6_addr = IN6ADDR_ANY_INIT,
925};
926
927/*
928 * Try a getsockname() on a connected datagram socket. Using a
929 * connected datagram socket prevents leaving a socket in TIME_WAIT.
930 * This conserves the ephemeral port number space.
931 *
932 * Returns zero and fills in "buf" if successful; otherwise, a
933 * negative errno is returned.
934 */
935static int rpc_sockname(struct net *net, struct sockaddr *sap, size_t salen,
936 struct sockaddr *buf, int buflen)
937{
938 struct socket *sock;
939 int err;
940
941 err = __sock_create(net, sap->sa_family,
942 SOCK_DGRAM, IPPROTO_UDP, &sock, 1);
943 if (err < 0) {
944 dprintk("RPC: can't create UDP socket (%d)\n", err);
945 goto out;
946 }
947
948 switch (sap->sa_family) {
949 case AF_INET:
950 err = kernel_bind(sock,
951 (struct sockaddr *)&rpc_inaddr_loopback,
952 sizeof(rpc_inaddr_loopback));
953 break;
954 case AF_INET6:
955 err = kernel_bind(sock,
956 (struct sockaddr *)&rpc_in6addr_loopback,
957 sizeof(rpc_in6addr_loopback));
958 break;
959 default:
960 err = -EAFNOSUPPORT;
961 goto out;
962 }
963 if (err < 0) {
964 dprintk("RPC: can't bind UDP socket (%d)\n", err);
965 goto out_release;
966 }
967
968 err = kernel_connect(sock, sap, salen, 0);
969 if (err < 0) {
970 dprintk("RPC: can't connect UDP socket (%d)\n", err);
971 goto out_release;
972 }
973
974 err = kernel_getsockname(sock, buf, &buflen);
975 if (err < 0) {
976 dprintk("RPC: getsockname failed (%d)\n", err);
977 goto out_release;
978 }
979
980 err = 0;
981 if (buf->sa_family == AF_INET6) {
982 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)buf;
983 sin6->sin6_scope_id = 0;
984 }
985 dprintk("RPC: %s succeeded\n", __func__);
986
987out_release:
988 sock_release(sock);
989out:
990 return err;
991}
992
993/*
994 * Scraping a connected socket failed, so we don't have a useable
995 * local address. Fallback: generate an address that will prevent
996 * the server from calling us back.
997 *
998 * Returns zero and fills in "buf" if successful; otherwise, a
999 * negative errno is returned.
1000 */
1001static int rpc_anyaddr(int family, struct sockaddr *buf, size_t buflen)
1002{
1003 switch (family) {
1004 case AF_INET:
1005 if (buflen < sizeof(rpc_inaddr_loopback))
1006 return -EINVAL;
1007 memcpy(buf, &rpc_inaddr_loopback,
1008 sizeof(rpc_inaddr_loopback));
1009 break;
1010 case AF_INET6:
1011 if (buflen < sizeof(rpc_in6addr_loopback))
1012 return -EINVAL;
1013 memcpy(buf, &rpc_in6addr_loopback,
1014 sizeof(rpc_in6addr_loopback));
1015 default:
1016 dprintk("RPC: %s: address family not supported\n",
1017 __func__);
1018 return -EAFNOSUPPORT;
1019 }
1020 dprintk("RPC: %s: succeeded\n", __func__);
1021 return 0;
1022}
1023
1024/**
1025 * rpc_localaddr - discover local endpoint address for an RPC client
1026 * @clnt: RPC client structure
1027 * @buf: target buffer
1028 * @buflen: size of target buffer, in bytes
1029 *
1030 * Returns zero and fills in "buf" and "buflen" if successful;
1031 * otherwise, a negative errno is returned.
1032 *
1033 * This works even if the underlying transport is not currently connected,
1034 * or if the upper layer never previously provided a source address.
1035 *
1036 * The result of this function call is transient: multiple calls in
1037 * succession may give different results, depending on how local
1038 * networking configuration changes over time.
1039 */
1040int rpc_localaddr(struct rpc_clnt *clnt, struct sockaddr *buf, size_t buflen)
1041{
1042 struct sockaddr_storage address;
1043 struct sockaddr *sap = (struct sockaddr *)&address;
1044 struct rpc_xprt *xprt;
1045 struct net *net;
1046 size_t salen;
1047 int err;
1048
1049 rcu_read_lock();
1050 xprt = rcu_dereference(clnt->cl_xprt);
1051 salen = xprt->addrlen;
1052 memcpy(sap, &xprt->addr, salen);
1053 net = get_net(xprt->xprt_net);
1054 rcu_read_unlock();
1055
1056 rpc_set_port(sap, 0);
1057 err = rpc_sockname(net, sap, salen, buf, buflen);
1058 put_net(net);
1059 if (err != 0)
1060 /* Couldn't discover local address, return ANYADDR */
1061 return rpc_anyaddr(sap->sa_family, buf, buflen);
1062 return 0;
1063}
1064EXPORT_SYMBOL_GPL(rpc_localaddr);
1065
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066void
1067rpc_setbufsize(struct rpc_clnt *clnt, unsigned int sndsize, unsigned int rcvsize)
1068{
Trond Myklebust2446ab62012-03-01 17:00:56 -05001069 struct rpc_xprt *xprt;
1070
1071 rcu_read_lock();
1072 xprt = rcu_dereference(clnt->cl_xprt);
Chuck Lever470056c2005-08-25 16:25:56 -07001073 if (xprt->ops->set_buffer_size)
1074 xprt->ops->set_buffer_size(xprt, sndsize, rcvsize);
Trond Myklebust2446ab62012-03-01 17:00:56 -05001075 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076}
Trond Myklebuste8914c62007-07-14 15:39:59 -04001077EXPORT_SYMBOL_GPL(rpc_setbufsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
Trond Myklebust2446ab62012-03-01 17:00:56 -05001079/**
1080 * rpc_protocol - Get transport protocol number for an RPC client
1081 * @clnt: RPC client to query
1082 *
1083 */
1084int rpc_protocol(struct rpc_clnt *clnt)
1085{
1086 int protocol;
1087
1088 rcu_read_lock();
1089 protocol = rcu_dereference(clnt->cl_xprt)->prot;
1090 rcu_read_unlock();
1091 return protocol;
1092}
1093EXPORT_SYMBOL_GPL(rpc_protocol);
1094
1095/**
1096 * rpc_net_ns - Get the network namespace for this RPC client
1097 * @clnt: RPC client to query
1098 *
1099 */
1100struct net *rpc_net_ns(struct rpc_clnt *clnt)
1101{
1102 struct net *ret;
1103
1104 rcu_read_lock();
1105 ret = rcu_dereference(clnt->cl_xprt)->xprt_net;
1106 rcu_read_unlock();
1107 return ret;
1108}
1109EXPORT_SYMBOL_GPL(rpc_net_ns);
1110
1111/**
1112 * rpc_max_payload - Get maximum payload size for a transport, in bytes
1113 * @clnt: RPC client to query
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 *
1115 * For stream transports, this is one RPC record fragment (see RFC
1116 * 1831), as we don't support multi-record requests yet. For datagram
1117 * transports, this is the size of an IP packet minus the IP, UDP, and
1118 * RPC header sizes.
1119 */
1120size_t rpc_max_payload(struct rpc_clnt *clnt)
1121{
Trond Myklebust2446ab62012-03-01 17:00:56 -05001122 size_t ret;
1123
1124 rcu_read_lock();
1125 ret = rcu_dereference(clnt->cl_xprt)->max_payload;
1126 rcu_read_unlock();
1127 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128}
Chuck Leverb86acd52006-08-22 20:06:22 -04001129EXPORT_SYMBOL_GPL(rpc_max_payload);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130
Chuck Lever35f5a422006-01-03 09:55:50 +01001131/**
1132 * rpc_force_rebind - force transport to check that remote port is unchanged
1133 * @clnt: client to rebind
1134 *
1135 */
1136void rpc_force_rebind(struct rpc_clnt *clnt)
1137{
Trond Myklebust2446ab62012-03-01 17:00:56 -05001138 if (clnt->cl_autobind) {
1139 rcu_read_lock();
1140 xprt_clear_bound(rcu_dereference(clnt->cl_xprt));
1141 rcu_read_unlock();
1142 }
Chuck Lever35f5a422006-01-03 09:55:50 +01001143}
Chuck Leverb86acd52006-08-22 20:06:22 -04001144EXPORT_SYMBOL_GPL(rpc_force_rebind);
Chuck Lever35f5a422006-01-03 09:55:50 +01001145
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146/*
Andy Adamsonaae20062009-04-01 09:22:40 -04001147 * Restart an (async) RPC call from the call_prepare state.
1148 * Usually called from within the exit handler.
1149 */
Trond Myklebustf1f88fc2010-07-31 14:29:07 -04001150int
Andy Adamsonaae20062009-04-01 09:22:40 -04001151rpc_restart_call_prepare(struct rpc_task *task)
1152{
1153 if (RPC_ASSASSINATED(task))
Trond Myklebustf1f88fc2010-07-31 14:29:07 -04001154 return 0;
Trond Myklebustd00c5d42011-10-19 12:17:29 -07001155 task->tk_action = call_start;
1156 if (task->tk_ops->rpc_call_prepare != NULL)
1157 task->tk_action = rpc_prepare_task;
Trond Myklebustf1f88fc2010-07-31 14:29:07 -04001158 return 1;
Andy Adamsonaae20062009-04-01 09:22:40 -04001159}
1160EXPORT_SYMBOL_GPL(rpc_restart_call_prepare);
1161
1162/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 * Restart an (async) RPC call. Usually called from within the
1164 * exit handler.
1165 */
Trond Myklebustf1f88fc2010-07-31 14:29:07 -04001166int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167rpc_restart_call(struct rpc_task *task)
1168{
1169 if (RPC_ASSASSINATED(task))
Trond Myklebustf1f88fc2010-07-31 14:29:07 -04001170 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 task->tk_action = call_start;
Trond Myklebustf1f88fc2010-07-31 14:29:07 -04001172 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173}
Trond Myklebuste8914c62007-07-14 15:39:59 -04001174EXPORT_SYMBOL_GPL(rpc_restart_call);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
Chuck Lever3748f1e2008-05-21 17:09:12 -04001176#ifdef RPC_DEBUG
1177static const char *rpc_proc_name(const struct rpc_task *task)
1178{
1179 const struct rpc_procinfo *proc = task->tk_msg.rpc_proc;
1180
1181 if (proc) {
1182 if (proc->p_name)
1183 return proc->p_name;
1184 else
1185 return "NULL";
1186 } else
1187 return "no proc";
1188}
1189#endif
1190
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191/*
1192 * 0. Initial state
1193 *
1194 * Other FSM states can be visited zero or more times, but
1195 * this state is visited exactly once for each RPC.
1196 */
1197static void
1198call_start(struct rpc_task *task)
1199{
1200 struct rpc_clnt *clnt = task->tk_client;
1201
Chuck Lever3748f1e2008-05-21 17:09:12 -04001202 dprintk("RPC: %5u call_start %s%d proc %s (%s)\n", task->tk_pid,
Chuck Lever46121cf2007-01-31 12:14:08 -05001203 clnt->cl_protname, clnt->cl_vers,
Chuck Lever3748f1e2008-05-21 17:09:12 -04001204 rpc_proc_name(task),
Chuck Lever46121cf2007-01-31 12:14:08 -05001205 (RPC_IS_ASYNC(task) ? "async" : "sync"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
1207 /* Increment call count */
1208 task->tk_msg.rpc_proc->p_count++;
1209 clnt->cl_stats->rpccnt++;
1210 task->tk_action = call_reserve;
1211}
1212
1213/*
1214 * 1. Reserve an RPC call slot
1215 */
1216static void
1217call_reserve(struct rpc_task *task)
1218{
Chuck Lever46121cf2007-01-31 12:14:08 -05001219 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 task->tk_status = 0;
1222 task->tk_action = call_reserveresult;
1223 xprt_reserve(task);
1224}
1225
1226/*
1227 * 1b. Grok the result of xprt_reserve()
1228 */
1229static void
1230call_reserveresult(struct rpc_task *task)
1231{
1232 int status = task->tk_status;
1233
Chuck Lever46121cf2007-01-31 12:14:08 -05001234 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
1236 /*
1237 * After a call to xprt_reserve(), we must have either
1238 * a request slot or else an error status.
1239 */
1240 task->tk_status = 0;
1241 if (status >= 0) {
1242 if (task->tk_rqstp) {
J. Bruce Fieldsf2d47d02010-09-12 19:55:25 -04001243 task->tk_action = call_refresh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 return;
1245 }
1246
1247 printk(KERN_ERR "%s: status=%d, but no request slot, exiting\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08001248 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 rpc_exit(task, -EIO);
1250 return;
1251 }
1252
1253 /*
1254 * Even though there was an error, we may have acquired
1255 * a request slot somehow. Make sure not to leak it.
1256 */
1257 if (task->tk_rqstp) {
1258 printk(KERN_ERR "%s: status=%d, request allocated anyway\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08001259 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 xprt_release(task);
1261 }
1262
1263 switch (status) {
1264 case -EAGAIN: /* woken up; retry */
1265 task->tk_action = call_reserve;
1266 return;
1267 case -EIO: /* probably a shutdown */
1268 break;
1269 default:
1270 printk(KERN_ERR "%s: unrecognized error %d, exiting\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08001271 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 break;
1273 }
1274 rpc_exit(task, status);
1275}
1276
1277/*
J. Bruce Fields55576242010-09-12 19:55:25 -04001278 * 2. Bind and/or refresh the credentials
1279 */
1280static void
1281call_refresh(struct rpc_task *task)
1282{
1283 dprint_status(task);
1284
1285 task->tk_action = call_refreshresult;
1286 task->tk_status = 0;
1287 task->tk_client->cl_stats->rpcauthrefresh++;
1288 rpcauth_refreshcred(task);
1289}
1290
1291/*
1292 * 2a. Process the results of a credential refresh
1293 */
1294static void
1295call_refreshresult(struct rpc_task *task)
1296{
1297 int status = task->tk_status;
1298
1299 dprint_status(task);
1300
1301 task->tk_status = 0;
Trond Myklebust5fc43972010-11-20 11:13:31 -05001302 task->tk_action = call_refresh;
J. Bruce Fields55576242010-09-12 19:55:25 -04001303 switch (status) {
Trond Myklebust5fc43972010-11-20 11:13:31 -05001304 case 0:
1305 if (rpcauth_uptodatecred(task))
1306 task->tk_action = call_allocate;
J. Bruce Fields55576242010-09-12 19:55:25 -04001307 return;
1308 case -ETIMEDOUT:
1309 rpc_delay(task, 3*HZ);
Trond Myklebust5fc43972010-11-20 11:13:31 -05001310 case -EAGAIN:
1311 status = -EACCES;
1312 if (!task->tk_cred_retry)
1313 break;
1314 task->tk_cred_retry--;
1315 dprintk("RPC: %5u %s: retry refresh creds\n",
1316 task->tk_pid, __func__);
1317 return;
J. Bruce Fields55576242010-09-12 19:55:25 -04001318 }
Trond Myklebust5fc43972010-11-20 11:13:31 -05001319 dprintk("RPC: %5u %s: refresh creds failed with error %d\n",
1320 task->tk_pid, __func__, status);
1321 rpc_exit(task, status);
J. Bruce Fields55576242010-09-12 19:55:25 -04001322}
1323
1324/*
1325 * 2b. Allocate the buffer. For details, see sched.c:rpc_malloc.
Chuck Lever02107142006-01-03 09:55:49 +01001326 * (Note: buffer memory is freed in xprt_release).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 */
1328static void
1329call_allocate(struct rpc_task *task)
1330{
J. Bruce Fieldsf2d47d02010-09-12 19:55:25 -04001331 unsigned int slack = task->tk_rqstp->rq_cred->cr_auth->au_cslack;
Chuck Lever02107142006-01-03 09:55:49 +01001332 struct rpc_rqst *req = task->tk_rqstp;
1333 struct rpc_xprt *xprt = task->tk_xprt;
Chuck Lever2bea90d2007-03-29 16:47:53 -04001334 struct rpc_procinfo *proc = task->tk_msg.rpc_proc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
Chuck Lever46121cf2007-01-31 12:14:08 -05001336 dprint_status(task);
1337
Chuck Lever2bea90d2007-03-29 16:47:53 -04001338 task->tk_status = 0;
J. Bruce Fieldsf2d47d02010-09-12 19:55:25 -04001339 task->tk_action = call_bind;
Chuck Lever2bea90d2007-03-29 16:47:53 -04001340
Chuck Lever02107142006-01-03 09:55:49 +01001341 if (req->rq_buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 return;
1343
Chuck Lever2bea90d2007-03-29 16:47:53 -04001344 if (proc->p_proc != 0) {
1345 BUG_ON(proc->p_arglen == 0);
1346 if (proc->p_decode != NULL)
1347 BUG_ON(proc->p_replen == 0);
1348 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
Chuck Lever2bea90d2007-03-29 16:47:53 -04001350 /*
1351 * Calculate the size (in quads) of the RPC call
1352 * and reply headers, and convert both values
1353 * to byte sizes.
1354 */
1355 req->rq_callsize = RPC_CALLHDRSIZE + (slack << 1) + proc->p_arglen;
1356 req->rq_callsize <<= 2;
1357 req->rq_rcvsize = RPC_REPHDRSIZE + slack + proc->p_replen;
1358 req->rq_rcvsize <<= 2;
1359
Chuck Leverc5a4dd82007-03-29 16:47:58 -04001360 req->rq_buffer = xprt->ops->buf_alloc(task,
1361 req->rq_callsize + req->rq_rcvsize);
Chuck Lever2bea90d2007-03-29 16:47:53 -04001362 if (req->rq_buffer != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 return;
Chuck Lever46121cf2007-01-31 12:14:08 -05001364
1365 dprintk("RPC: %5u rpc_buffer allocation failed\n", task->tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
Trond Myklebust5afa91332011-06-17 10:14:59 -04001367 if (RPC_IS_ASYNC(task) || !fatal_signal_pending(current)) {
Trond Myklebustb6e9c712007-10-01 12:06:44 -04001368 task->tk_action = call_allocate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 rpc_delay(task, HZ>>4);
1370 return;
1371 }
1372
1373 rpc_exit(task, -ERESTARTSYS);
1374}
1375
Trond Myklebust940e3312005-11-09 21:45:24 -05001376static inline int
1377rpc_task_need_encode(struct rpc_task *task)
1378{
1379 return task->tk_rqstp->rq_snd_buf.len == 0;
1380}
1381
1382static inline void
1383rpc_task_force_reencode(struct rpc_task *task)
1384{
1385 task->tk_rqstp->rq_snd_buf.len = 0;
Trond Myklebust2574cc92009-08-28 11:12:12 -04001386 task->tk_rqstp->rq_bytes_sent = 0;
Trond Myklebust940e3312005-11-09 21:45:24 -05001387}
1388
Chuck Lever2bea90d2007-03-29 16:47:53 -04001389static inline void
1390rpc_xdr_buf_init(struct xdr_buf *buf, void *start, size_t len)
1391{
1392 buf->head[0].iov_base = start;
1393 buf->head[0].iov_len = len;
1394 buf->tail[0].iov_len = 0;
1395 buf->page_len = 0;
\"Talpey, Thomas\4f22ccc2007-09-10 13:44:58 -04001396 buf->flags = 0;
Chuck Lever2bea90d2007-03-29 16:47:53 -04001397 buf->len = 0;
1398 buf->buflen = len;
1399}
1400
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401/*
1402 * 3. Encode arguments of an RPC call
1403 */
1404static void
Chuck Leverb0e1c572008-05-21 17:09:19 -04001405rpc_xdr_encode(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 struct rpc_rqst *req = task->tk_rqstp;
Chuck Lever9f06c712010-12-14 14:59:18 +00001408 kxdreproc_t encode;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001409 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
Chuck Lever46121cf2007-01-31 12:14:08 -05001411 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
Chuck Lever2bea90d2007-03-29 16:47:53 -04001413 rpc_xdr_buf_init(&req->rq_snd_buf,
1414 req->rq_buffer,
1415 req->rq_callsize);
1416 rpc_xdr_buf_init(&req->rq_rcv_buf,
1417 (char *)req->rq_buffer + req->rq_callsize,
1418 req->rq_rcvsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419
Chuck Leverb0e1c572008-05-21 17:09:19 -04001420 p = rpc_encode_header(task);
1421 if (p == NULL) {
1422 printk(KERN_INFO "RPC: couldn't encode RPC header, exit EIO\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 rpc_exit(task, -EIO);
1424 return;
1425 }
Chuck Leverb0e1c572008-05-21 17:09:19 -04001426
1427 encode = task->tk_msg.rpc_proc->p_encode;
J. Bruce Fieldsf3680312005-10-13 16:54:48 -04001428 if (encode == NULL)
1429 return;
1430
1431 task->tk_status = rpcauth_wrap_req(task, encode, req, p,
1432 task->tk_msg.rpc_argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433}
1434
1435/*
1436 * 4. Get the server port number if not yet set
1437 */
1438static void
1439call_bind(struct rpc_task *task)
1440{
Chuck Leverec739ef2006-08-22 20:06:15 -04001441 struct rpc_xprt *xprt = task->tk_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442
Chuck Lever46121cf2007-01-31 12:14:08 -05001443 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
Chuck Leverda351872005-08-11 16:25:11 -04001445 task->tk_action = call_connect;
Chuck Leverec739ef2006-08-22 20:06:15 -04001446 if (!xprt_bound(xprt)) {
Chuck Leverda351872005-08-11 16:25:11 -04001447 task->tk_action = call_bind_status;
Chuck Leverec739ef2006-08-22 20:06:15 -04001448 task->tk_timeout = xprt->bind_timeout;
Chuck Leverbbf7c1d2006-08-22 20:06:16 -04001449 xprt->ops->rpcbind(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 }
1451}
1452
1453/*
Chuck Leverda351872005-08-11 16:25:11 -04001454 * 4a. Sort out bind result
1455 */
1456static void
1457call_bind_status(struct rpc_task *task)
1458{
Chuck Lever906462a2007-09-11 18:00:47 -04001459 int status = -EIO;
Chuck Leverda351872005-08-11 16:25:11 -04001460
1461 if (task->tk_status >= 0) {
Chuck Lever46121cf2007-01-31 12:14:08 -05001462 dprint_status(task);
Chuck Leverda351872005-08-11 16:25:11 -04001463 task->tk_status = 0;
1464 task->tk_action = call_connect;
1465 return;
1466 }
1467
Steve Dickson5753cba2012-02-06 10:08:08 -05001468 trace_rpc_bind_status(task);
Chuck Leverda351872005-08-11 16:25:11 -04001469 switch (task->tk_status) {
Trond Myklebust381ba742008-07-07 12:18:53 -04001470 case -ENOMEM:
1471 dprintk("RPC: %5u rpcbind out of memory\n", task->tk_pid);
1472 rpc_delay(task, HZ >> 2);
Chuck Lever2429cbf2007-09-11 18:00:41 -04001473 goto retry_timeout;
Chuck Leverda351872005-08-11 16:25:11 -04001474 case -EACCES:
Chuck Lever46121cf2007-01-31 12:14:08 -05001475 dprintk("RPC: %5u remote rpcbind: RPC program/version "
1476 "unavailable\n", task->tk_pid);
Chuck Leverb79dc8c2007-09-11 18:00:52 -04001477 /* fail immediately if this is an RPC ping */
1478 if (task->tk_msg.rpc_proc->p_proc == 0) {
1479 status = -EOPNOTSUPP;
1480 break;
1481 }
Trond Myklebust0b760112011-05-31 15:15:34 -04001482 if (task->tk_rebind_retry == 0)
1483 break;
1484 task->tk_rebind_retry--;
Chuck Leverea635a52005-10-06 23:12:58 -04001485 rpc_delay(task, 3*HZ);
Trond Myklebustda458282006-08-31 15:44:52 -04001486 goto retry_timeout;
Chuck Leverda351872005-08-11 16:25:11 -04001487 case -ETIMEDOUT:
Chuck Lever46121cf2007-01-31 12:14:08 -05001488 dprintk("RPC: %5u rpcbind request timed out\n",
Chuck Leverda351872005-08-11 16:25:11 -04001489 task->tk_pid);
Trond Myklebustda458282006-08-31 15:44:52 -04001490 goto retry_timeout;
Chuck Leverda351872005-08-11 16:25:11 -04001491 case -EPFNOSUPPORT:
Chuck Lever906462a2007-09-11 18:00:47 -04001492 /* server doesn't support any rpcbind version we know of */
Chuck Lever012da152009-12-03 15:58:56 -05001493 dprintk("RPC: %5u unrecognized remote rpcbind service\n",
Chuck Leverda351872005-08-11 16:25:11 -04001494 task->tk_pid);
1495 break;
1496 case -EPROTONOSUPPORT:
Chuck Lever00a6e7b2007-03-29 16:48:33 -04001497 dprintk("RPC: %5u remote rpcbind version unavailable, retrying\n",
Chuck Leverda351872005-08-11 16:25:11 -04001498 task->tk_pid);
Chuck Lever00a6e7b2007-03-29 16:48:33 -04001499 task->tk_status = 0;
1500 task->tk_action = call_bind;
1501 return;
Chuck Lever012da152009-12-03 15:58:56 -05001502 case -ECONNREFUSED: /* connection problems */
1503 case -ECONNRESET:
1504 case -ENOTCONN:
1505 case -EHOSTDOWN:
1506 case -EHOSTUNREACH:
1507 case -ENETUNREACH:
1508 case -EPIPE:
1509 dprintk("RPC: %5u remote rpcbind unreachable: %d\n",
1510 task->tk_pid, task->tk_status);
1511 if (!RPC_IS_SOFTCONN(task)) {
1512 rpc_delay(task, 5*HZ);
1513 goto retry_timeout;
1514 }
1515 status = task->tk_status;
1516 break;
Chuck Leverda351872005-08-11 16:25:11 -04001517 default:
Chuck Lever46121cf2007-01-31 12:14:08 -05001518 dprintk("RPC: %5u unrecognized rpcbind error (%d)\n",
Chuck Leverda351872005-08-11 16:25:11 -04001519 task->tk_pid, -task->tk_status);
Chuck Leverda351872005-08-11 16:25:11 -04001520 }
1521
1522 rpc_exit(task, status);
1523 return;
1524
Trond Myklebustda458282006-08-31 15:44:52 -04001525retry_timeout:
1526 task->tk_action = call_timeout;
Chuck Leverda351872005-08-11 16:25:11 -04001527}
1528
1529/*
1530 * 4b. Connect to the RPC server
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 */
1532static void
1533call_connect(struct rpc_task *task)
1534{
Chuck Leverda351872005-08-11 16:25:11 -04001535 struct rpc_xprt *xprt = task->tk_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
Chuck Lever46121cf2007-01-31 12:14:08 -05001537 dprintk("RPC: %5u call_connect xprt %p %s connected\n",
Chuck Leverda351872005-08-11 16:25:11 -04001538 task->tk_pid, xprt,
1539 (xprt_connected(xprt) ? "is" : "is not"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540
Chuck Leverda351872005-08-11 16:25:11 -04001541 task->tk_action = call_transmit;
1542 if (!xprt_connected(xprt)) {
1543 task->tk_action = call_connect_status;
1544 if (task->tk_status < 0)
1545 return;
1546 xprt_connect(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548}
1549
1550/*
Chuck Leverda351872005-08-11 16:25:11 -04001551 * 4c. Sort out connect result
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 */
1553static void
1554call_connect_status(struct rpc_task *task)
1555{
1556 struct rpc_clnt *clnt = task->tk_client;
1557 int status = task->tk_status;
1558
Chuck Lever46121cf2007-01-31 12:14:08 -05001559 dprint_status(task);
Chuck Leverda351872005-08-11 16:25:11 -04001560
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 task->tk_status = 0;
Trond Myklebust2a491992009-03-11 14:38:00 -04001562 if (status >= 0 || status == -EAGAIN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 clnt->cl_stats->netreconn++;
1564 task->tk_action = call_transmit;
1565 return;
1566 }
1567
Steve Dickson5753cba2012-02-06 10:08:08 -05001568 trace_rpc_connect_status(task, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 switch (status) {
Trond Myklebustda458282006-08-31 15:44:52 -04001570 /* if soft mounted, test if we've timed out */
1571 case -ETIMEDOUT:
1572 task->tk_action = call_timeout;
Trond Myklebust2a491992009-03-11 14:38:00 -04001573 break;
1574 default:
1575 rpc_exit(task, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 }
1577}
1578
1579/*
1580 * 5. Transmit the RPC request, and wait for reply
1581 */
1582static void
1583call_transmit(struct rpc_task *task)
1584{
Chuck Lever46121cf2007-01-31 12:14:08 -05001585 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
1587 task->tk_action = call_status;
1588 if (task->tk_status < 0)
1589 return;
1590 task->tk_status = xprt_prepare_transmit(task);
1591 if (task->tk_status != 0)
1592 return;
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04001593 task->tk_action = call_transmit_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 /* Encode here so that rpcsec_gss can use correct sequence number. */
Trond Myklebust940e3312005-11-09 21:45:24 -05001595 if (rpc_task_need_encode(task)) {
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04001596 BUG_ON(task->tk_rqstp->rq_bytes_sent != 0);
Chuck Leverb0e1c572008-05-21 17:09:19 -04001597 rpc_xdr_encode(task);
Trond Myklebust5e5ce5b2005-10-18 14:20:11 -07001598 /* Did the encode result in an error condition? */
Trond Myklebust8b39f2b2008-05-14 19:48:25 -07001599 if (task->tk_status != 0) {
1600 /* Was the error nonfatal? */
1601 if (task->tk_status == -EAGAIN)
1602 rpc_delay(task, HZ >> 4);
1603 else
1604 rpc_exit(task, task->tk_status);
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04001605 return;
Trond Myklebust8b39f2b2008-05-14 19:48:25 -07001606 }
Trond Myklebust5e5ce5b2005-10-18 14:20:11 -07001607 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 xprt_transmit(task);
1609 if (task->tk_status < 0)
1610 return;
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04001611 /*
1612 * On success, ensure that we call xprt_end_transmit() before sleeping
1613 * in order to allow access to the socket to other RPC requests.
1614 */
1615 call_transmit_status(task);
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001616 if (rpc_reply_expected(task))
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04001617 return;
1618 task->tk_action = rpc_exit_task;
Trond Myklebustfda13932008-02-22 16:34:12 -05001619 rpc_wake_up_queued_task(&task->tk_xprt->pending, task);
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04001620}
1621
1622/*
1623 * 5a. Handle cleanup after a transmission
1624 */
1625static void
1626call_transmit_status(struct rpc_task *task)
1627{
1628 task->tk_action = call_status;
Chuck Lever206a1342009-12-03 15:58:56 -05001629
1630 /*
1631 * Common case: success. Force the compiler to put this
1632 * test first.
1633 */
1634 if (task->tk_status == 0) {
1635 xprt_end_transmit(task);
1636 rpc_task_force_reencode(task);
1637 return;
1638 }
1639
Trond Myklebust15f081c2009-03-11 14:37:57 -04001640 switch (task->tk_status) {
1641 case -EAGAIN:
1642 break;
1643 default:
Chuck Lever206a1342009-12-03 15:58:56 -05001644 dprint_status(task);
Trond Myklebust15f081c2009-03-11 14:37:57 -04001645 xprt_end_transmit(task);
Chuck Lever09a21c42009-12-03 15:58:56 -05001646 rpc_task_force_reencode(task);
1647 break;
Trond Myklebust15f081c2009-03-11 14:37:57 -04001648 /*
1649 * Special cases: if we've been waiting on the
1650 * socket's write_space() callback, or if the
1651 * socket just returned a connection error,
1652 * then hold onto the transport lock.
1653 */
1654 case -ECONNREFUSED:
Trond Myklebust15f081c2009-03-11 14:37:57 -04001655 case -EHOSTDOWN:
1656 case -EHOSTUNREACH:
1657 case -ENETUNREACH:
Chuck Lever09a21c42009-12-03 15:58:56 -05001658 if (RPC_IS_SOFTCONN(task)) {
1659 xprt_end_transmit(task);
1660 rpc_exit(task, task->tk_status);
1661 break;
1662 }
1663 case -ECONNRESET:
1664 case -ENOTCONN:
Trond Myklebustc8485e42009-03-11 14:37:59 -04001665 case -EPIPE:
Trond Myklebust15f081c2009-03-11 14:37:57 -04001666 rpc_task_force_reencode(task);
1667 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668}
1669
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001670#if defined(CONFIG_SUNRPC_BACKCHANNEL)
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001671/*
1672 * 5b. Send the backchannel RPC reply. On error, drop the reply. In
1673 * addition, disconnect on connectivity errors.
1674 */
1675static void
1676call_bc_transmit(struct rpc_task *task)
1677{
1678 struct rpc_rqst *req = task->tk_rqstp;
1679
1680 BUG_ON(task->tk_status != 0);
1681 task->tk_status = xprt_prepare_transmit(task);
1682 if (task->tk_status == -EAGAIN) {
1683 /*
1684 * Could not reserve the transport. Try again after the
1685 * transport is released.
1686 */
1687 task->tk_status = 0;
1688 task->tk_action = call_bc_transmit;
1689 return;
1690 }
1691
1692 task->tk_action = rpc_exit_task;
1693 if (task->tk_status < 0) {
1694 printk(KERN_NOTICE "RPC: Could not send backchannel reply "
1695 "error: %d\n", task->tk_status);
1696 return;
1697 }
1698
1699 xprt_transmit(task);
1700 xprt_end_transmit(task);
1701 dprint_status(task);
1702 switch (task->tk_status) {
1703 case 0:
1704 /* Success */
1705 break;
1706 case -EHOSTDOWN:
1707 case -EHOSTUNREACH:
1708 case -ENETUNREACH:
1709 case -ETIMEDOUT:
1710 /*
1711 * Problem reaching the server. Disconnect and let the
1712 * forechannel reestablish the connection. The server will
1713 * have to retransmit the backchannel request and we'll
1714 * reprocess it. Since these ops are idempotent, there's no
1715 * need to cache our reply at this time.
1716 */
1717 printk(KERN_NOTICE "RPC: Could not send backchannel reply "
1718 "error: %d\n", task->tk_status);
1719 xprt_conditional_disconnect(task->tk_xprt,
1720 req->rq_connect_cookie);
1721 break;
1722 default:
1723 /*
1724 * We were unable to reply and will have to drop the
1725 * request. The server should reconnect and retransmit.
1726 */
1727 BUG_ON(task->tk_status == -EAGAIN);
1728 printk(KERN_NOTICE "RPC: Could not send backchannel reply "
1729 "error: %d\n", task->tk_status);
1730 break;
1731 }
1732 rpc_wake_up_queued_task(&req->rq_xprt->pending, task);
1733}
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001734#endif /* CONFIG_SUNRPC_BACKCHANNEL */
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001735
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736/*
1737 * 6. Sort out the RPC call status
1738 */
1739static void
1740call_status(struct rpc_task *task)
1741{
1742 struct rpc_clnt *clnt = task->tk_client;
1743 struct rpc_rqst *req = task->tk_rqstp;
1744 int status;
1745
Ricardo Labiagadd2b63d2009-04-01 09:23:28 -04001746 if (req->rq_reply_bytes_recvd > 0 && !req->rq_bytes_sent)
1747 task->tk_status = req->rq_reply_bytes_recvd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748
Chuck Lever46121cf2007-01-31 12:14:08 -05001749 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
1751 status = task->tk_status;
1752 if (status >= 0) {
1753 task->tk_action = call_decode;
1754 return;
1755 }
1756
Steve Dickson5753cba2012-02-06 10:08:08 -05001757 trace_rpc_call_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 task->tk_status = 0;
1759 switch(status) {
Trond Myklebust76303992006-08-30 14:32:49 -04001760 case -EHOSTDOWN:
1761 case -EHOSTUNREACH:
1762 case -ENETUNREACH:
1763 /*
1764 * Delay any retries for 3 seconds, then handle as if it
1765 * were a timeout.
1766 */
1767 rpc_delay(task, 3*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 case -ETIMEDOUT:
1769 task->tk_action = call_timeout;
Trond Myklebust241c39b2007-04-20 16:12:55 -04001770 if (task->tk_client->cl_discrtry)
Trond Myklebust7c1d71c2008-04-17 16:52:57 -04001771 xprt_conditional_disconnect(task->tk_xprt,
1772 req->rq_connect_cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 break;
Trond Myklebustc8485e42009-03-11 14:37:59 -04001774 case -ECONNRESET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 case -ECONNREFUSED:
Chuck Lever35f5a422006-01-03 09:55:50 +01001776 rpc_force_rebind(clnt);
Trond Myklebustc8485e42009-03-11 14:37:59 -04001777 rpc_delay(task, 3*HZ);
1778 case -EPIPE:
1779 case -ENOTCONN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 task->tk_action = call_bind;
1781 break;
1782 case -EAGAIN:
1783 task->tk_action = call_transmit;
1784 break;
1785 case -EIO:
1786 /* shutdown or soft timeout */
1787 rpc_exit(task, status);
1788 break;
1789 default:
Olga Kornievskaiab6b61522008-06-09 16:51:31 -04001790 if (clnt->cl_chatty)
1791 printk("%s: RPC call returned error %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 clnt->cl_protname, -status);
1793 rpc_exit(task, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 }
1795}
1796
1797/*
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04001798 * 6a. Handle RPC timeout
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 * We do not release the request slot, so we keep using the
1800 * same XID for all retransmits.
1801 */
1802static void
1803call_timeout(struct rpc_task *task)
1804{
1805 struct rpc_clnt *clnt = task->tk_client;
1806
1807 if (xprt_adjust_timeout(task->tk_rqstp) == 0) {
Chuck Lever46121cf2007-01-31 12:14:08 -05001808 dprintk("RPC: %5u call_timeout (minor)\n", task->tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 goto retry;
1810 }
1811
Chuck Lever46121cf2007-01-31 12:14:08 -05001812 dprintk("RPC: %5u call_timeout (major)\n", task->tk_pid);
Chuck Leveref759a22006-03-20 13:44:17 -05001813 task->tk_timeouts++;
1814
Chuck Lever3a28bec2009-12-03 15:58:56 -05001815 if (RPC_IS_SOFTCONN(task)) {
1816 rpc_exit(task, -ETIMEDOUT);
1817 return;
1818 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 if (RPC_IS_SOFT(task)) {
Olga Kornievskaiab6b61522008-06-09 16:51:31 -04001820 if (clnt->cl_chatty)
Trond Myklebust4e0038b2012-03-01 17:01:05 -05001821 rcu_read_lock();
Olga Kornievskaiab6b61522008-06-09 16:51:31 -04001822 printk(KERN_NOTICE "%s: server %s not responding, timed out\n",
Trond Myklebust4e0038b2012-03-01 17:01:05 -05001823 clnt->cl_protname,
1824 rcu_dereference(clnt->cl_xprt)->servername);
1825 rcu_read_unlock();
Trond Myklebust7494d002011-04-24 14:28:45 -04001826 if (task->tk_flags & RPC_TASK_TIMEOUT)
1827 rpc_exit(task, -ETIMEDOUT);
1828 else
1829 rpc_exit(task, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 return;
1831 }
1832
Chuck Leverf518e35a2006-01-03 09:55:52 +01001833 if (!(task->tk_flags & RPC_CALL_MAJORSEEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 task->tk_flags |= RPC_CALL_MAJORSEEN;
Trond Myklebust4e0038b2012-03-01 17:01:05 -05001835 if (clnt->cl_chatty) {
1836 rcu_read_lock();
Olga Kornievskaiab6b61522008-06-09 16:51:31 -04001837 printk(KERN_NOTICE "%s: server %s not responding, still trying\n",
Trond Myklebust4e0038b2012-03-01 17:01:05 -05001838 clnt->cl_protname,
1839 rcu_dereference(clnt->cl_xprt)->servername);
1840 rcu_read_unlock();
1841 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 }
Chuck Lever35f5a422006-01-03 09:55:50 +01001843 rpc_force_rebind(clnt);
Trond Myklebustb48633b2008-04-22 16:47:55 -04001844 /*
1845 * Did our request time out due to an RPCSEC_GSS out-of-sequence
1846 * event? RFC2203 requires the server to drop all such requests.
1847 */
1848 rpcauth_invalcred(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849
1850retry:
1851 clnt->cl_stats->rpcretrans++;
1852 task->tk_action = call_bind;
1853 task->tk_status = 0;
1854}
1855
1856/*
1857 * 7. Decode the RPC reply
1858 */
1859static void
1860call_decode(struct rpc_task *task)
1861{
1862 struct rpc_clnt *clnt = task->tk_client;
1863 struct rpc_rqst *req = task->tk_rqstp;
Chuck Leverbf269552010-12-14 14:59:29 +00001864 kxdrdproc_t decode = task->tk_msg.rpc_proc->p_decode;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001865 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866
Vasily Averin726fd6a2011-06-01 16:23:59 +04001867 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868
Chuck Leverf518e35a2006-01-03 09:55:52 +01001869 if (task->tk_flags & RPC_CALL_MAJORSEEN) {
Trond Myklebust4e0038b2012-03-01 17:01:05 -05001870 if (clnt->cl_chatty) {
1871 rcu_read_lock();
Olga Kornievskaiab6b61522008-06-09 16:51:31 -04001872 printk(KERN_NOTICE "%s: server %s OK\n",
Trond Myklebust4e0038b2012-03-01 17:01:05 -05001873 clnt->cl_protname,
1874 rcu_dereference(clnt->cl_xprt)->servername);
1875 rcu_read_unlock();
1876 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 task->tk_flags &= ~RPC_CALL_MAJORSEEN;
1878 }
1879
Trond Myklebust43ac3f22006-03-20 13:44:51 -05001880 /*
1881 * Ensure that we see all writes made by xprt_complete_rqst()
Ricardo Labiagadd2b63d2009-04-01 09:23:28 -04001882 * before it changed req->rq_reply_bytes_recvd.
Trond Myklebust43ac3f22006-03-20 13:44:51 -05001883 */
1884 smp_rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 req->rq_rcv_buf.len = req->rq_private_buf.len;
1886
1887 /* Check that the softirq receive buffer is valid */
1888 WARN_ON(memcmp(&req->rq_rcv_buf, &req->rq_private_buf,
1889 sizeof(req->rq_rcv_buf)) != 0);
1890
Trond Myklebust1e799b62008-03-21 16:19:41 -04001891 if (req->rq_rcv_buf.len < 12) {
1892 if (!RPC_IS_SOFT(task)) {
1893 task->tk_action = call_bind;
1894 clnt->cl_stats->rpcretrans++;
1895 goto out_retry;
1896 }
1897 dprintk("RPC: %s: too small RPC reply size (%d bytes)\n",
1898 clnt->cl_protname, task->tk_status);
1899 task->tk_action = call_timeout;
1900 goto out_retry;
1901 }
1902
Chuck Leverb0e1c572008-05-21 17:09:19 -04001903 p = rpc_verify_header(task);
Trond Myklebustabbcf282006-01-03 09:55:03 +01001904 if (IS_ERR(p)) {
1905 if (p == ERR_PTR(-EAGAIN))
1906 goto out_retry;
1907 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 }
1909
Trond Myklebustabbcf282006-01-03 09:55:03 +01001910 task->tk_action = rpc_exit_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911
Trond Myklebust6d5fcb52006-10-18 16:01:06 -04001912 if (decode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 task->tk_status = rpcauth_unwrap_resp(task, decode, req, p,
1914 task->tk_msg.rpc_resp);
Trond Myklebust6d5fcb52006-10-18 16:01:06 -04001915 }
Chuck Lever46121cf2007-01-31 12:14:08 -05001916 dprintk("RPC: %5u call_decode result %d\n", task->tk_pid,
1917 task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 return;
1919out_retry:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 task->tk_status = 0;
Chuck Leverb0e1c572008-05-21 17:09:19 -04001921 /* Note: rpc_verify_header() may have freed the RPC slot */
Trond Myklebust24b74bf2008-04-19 13:15:47 -04001922 if (task->tk_rqstp == req) {
Ricardo Labiagadd2b63d2009-04-01 09:23:28 -04001923 req->rq_reply_bytes_recvd = req->rq_rcv_buf.len = 0;
Trond Myklebust24b74bf2008-04-19 13:15:47 -04001924 if (task->tk_client->cl_discrtry)
Trond Myklebust7c1d71c2008-04-17 16:52:57 -04001925 xprt_conditional_disconnect(task->tk_xprt,
1926 req->rq_connect_cookie);
Trond Myklebust24b74bf2008-04-19 13:15:47 -04001927 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928}
1929
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001930static __be32 *
Chuck Leverb0e1c572008-05-21 17:09:19 -04001931rpc_encode_header(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932{
1933 struct rpc_clnt *clnt = task->tk_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 struct rpc_rqst *req = task->tk_rqstp;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001935 __be32 *p = req->rq_svec[0].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936
1937 /* FIXME: check buffer size? */
Chuck Lever808012f2005-08-25 16:25:49 -07001938
1939 p = xprt_skip_transport_header(task->tk_xprt, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 *p++ = req->rq_xid; /* XID */
1941 *p++ = htonl(RPC_CALL); /* CALL */
1942 *p++ = htonl(RPC_VERSION); /* RPC version */
1943 *p++ = htonl(clnt->cl_prog); /* program number */
1944 *p++ = htonl(clnt->cl_vers); /* program version */
1945 *p++ = htonl(task->tk_msg.rpc_proc->p_proc); /* procedure */
Trond Myklebust334ccfd2005-06-22 17:16:19 +00001946 p = rpcauth_marshcred(task, p);
1947 req->rq_slen = xdr_adjust_iovec(&req->rq_svec[0], p);
1948 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949}
1950
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001951static __be32 *
Chuck Leverb0e1c572008-05-21 17:09:19 -04001952rpc_verify_header(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953{
Trond Myklebust4e0038b2012-03-01 17:01:05 -05001954 struct rpc_clnt *clnt = task->tk_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 struct kvec *iov = &task->tk_rqstp->rq_rcv_buf.head[0];
1956 int len = task->tk_rqstp->rq_rcv_buf.len >> 2;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001957 __be32 *p = iov->iov_base;
1958 u32 n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 int error = -EACCES;
1960
David Howellse8896492006-08-24 15:44:19 -04001961 if ((task->tk_rqstp->rq_rcv_buf.len & 3) != 0) {
1962 /* RFC-1014 says that the representation of XDR data must be a
1963 * multiple of four bytes
1964 * - if it isn't pointer subtraction in the NFS client may give
1965 * undefined results
1966 */
Trond Myklebust8a702bb2007-06-27 18:30:26 -04001967 dprintk("RPC: %5u %s: XDR representation not a multiple of"
Harvey Harrison0dc47872008-03-05 20:47:47 -08001968 " 4 bytes: 0x%x\n", task->tk_pid, __func__,
Trond Myklebust8a702bb2007-06-27 18:30:26 -04001969 task->tk_rqstp->rq_rcv_buf.len);
David Howellse8896492006-08-24 15:44:19 -04001970 goto out_eio;
1971 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 if ((len -= 3) < 0)
1973 goto out_overflow;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001975 p += 1; /* skip XID */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 if ((n = ntohl(*p++)) != RPC_REPLY) {
Trond Myklebust8a702bb2007-06-27 18:30:26 -04001977 dprintk("RPC: %5u %s: not an RPC reply: %x\n",
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001978 task->tk_pid, __func__, n);
Trond Myklebustabbcf282006-01-03 09:55:03 +01001979 goto out_garbage;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 }
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04001981
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 if ((n = ntohl(*p++)) != RPC_MSG_ACCEPTED) {
1983 if (--len < 0)
1984 goto out_overflow;
1985 switch ((n = ntohl(*p++))) {
Joe Perches89f0e4f2011-07-01 09:43:12 +00001986 case RPC_AUTH_ERROR:
1987 break;
1988 case RPC_MISMATCH:
1989 dprintk("RPC: %5u %s: RPC call version mismatch!\n",
1990 task->tk_pid, __func__);
1991 error = -EPROTONOSUPPORT;
1992 goto out_err;
1993 default:
1994 dprintk("RPC: %5u %s: RPC call rejected, "
1995 "unknown error: %x\n",
1996 task->tk_pid, __func__, n);
1997 goto out_eio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 }
1999 if (--len < 0)
2000 goto out_overflow;
2001 switch ((n = ntohl(*p++))) {
2002 case RPC_AUTH_REJECTEDCRED:
2003 case RPC_AUTH_REJECTEDVERF:
2004 case RPCSEC_GSS_CREDPROBLEM:
2005 case RPCSEC_GSS_CTXPROBLEM:
2006 if (!task->tk_cred_retry)
2007 break;
2008 task->tk_cred_retry--;
Chuck Lever46121cf2007-01-31 12:14:08 -05002009 dprintk("RPC: %5u %s: retry stale creds\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08002010 task->tk_pid, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 rpcauth_invalcred(task);
Trond Myklebust220bcc22007-10-01 12:06:48 -04002012 /* Ensure we obtain a new XID! */
2013 xprt_release(task);
Trond Myklebust118df3d2010-10-24 17:17:31 -04002014 task->tk_action = call_reserve;
Trond Myklebustabbcf282006-01-03 09:55:03 +01002015 goto out_retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 case RPC_AUTH_BADCRED:
2017 case RPC_AUTH_BADVERF:
2018 /* possibly garbled cred/verf? */
2019 if (!task->tk_garb_retry)
2020 break;
2021 task->tk_garb_retry--;
Chuck Lever46121cf2007-01-31 12:14:08 -05002022 dprintk("RPC: %5u %s: retry garbled creds\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08002023 task->tk_pid, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 task->tk_action = call_bind;
Trond Myklebustabbcf282006-01-03 09:55:03 +01002025 goto out_retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 case RPC_AUTH_TOOWEAK:
Trond Myklebust4e0038b2012-03-01 17:01:05 -05002027 rcu_read_lock();
Chuck Leverb0e1c572008-05-21 17:09:19 -04002028 printk(KERN_NOTICE "RPC: server %s requires stronger "
Trond Myklebust4e0038b2012-03-01 17:01:05 -05002029 "authentication.\n",
2030 rcu_dereference(clnt->cl_xprt)->servername);
2031 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 break;
2033 default:
Trond Myklebust8a702bb2007-06-27 18:30:26 -04002034 dprintk("RPC: %5u %s: unknown auth error: %x\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08002035 task->tk_pid, __func__, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 error = -EIO;
2037 }
Chuck Lever46121cf2007-01-31 12:14:08 -05002038 dprintk("RPC: %5u %s: call rejected %d\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08002039 task->tk_pid, __func__, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 goto out_err;
2041 }
2042 if (!(p = rpcauth_checkverf(task, p))) {
Trond Myklebust8a702bb2007-06-27 18:30:26 -04002043 dprintk("RPC: %5u %s: auth check failed\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08002044 task->tk_pid, __func__);
Trond Myklebustabbcf282006-01-03 09:55:03 +01002045 goto out_garbage; /* bad verifier, retry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 }
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07002047 len = p - (__be32 *)iov->iov_base - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 if (len < 0)
2049 goto out_overflow;
2050 switch ((n = ntohl(*p++))) {
2051 case RPC_SUCCESS:
2052 return p;
2053 case RPC_PROG_UNAVAIL:
Trond Myklebust4e0038b2012-03-01 17:01:05 -05002054 dprintk_rcu("RPC: %5u %s: program %u is unsupported "
2055 "by server %s\n", task->tk_pid, __func__,
2056 (unsigned int)clnt->cl_prog,
2057 rcu_dereference(clnt->cl_xprt)->servername);
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00002058 error = -EPFNOSUPPORT;
2059 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 case RPC_PROG_MISMATCH:
Trond Myklebust4e0038b2012-03-01 17:01:05 -05002061 dprintk_rcu("RPC: %5u %s: program %u, version %u unsupported "
2062 "by server %s\n", task->tk_pid, __func__,
2063 (unsigned int)clnt->cl_prog,
2064 (unsigned int)clnt->cl_vers,
2065 rcu_dereference(clnt->cl_xprt)->servername);
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00002066 error = -EPROTONOSUPPORT;
2067 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 case RPC_PROC_UNAVAIL:
Trond Myklebust4e0038b2012-03-01 17:01:05 -05002069 dprintk_rcu("RPC: %5u %s: proc %s unsupported by program %u, "
Chuck Lever46121cf2007-01-31 12:14:08 -05002070 "version %u on server %s\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08002071 task->tk_pid, __func__,
Chuck Lever3748f1e2008-05-21 17:09:12 -04002072 rpc_proc_name(task),
Trond Myklebust4e0038b2012-03-01 17:01:05 -05002073 clnt->cl_prog, clnt->cl_vers,
2074 rcu_dereference(clnt->cl_xprt)->servername);
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00002075 error = -EOPNOTSUPP;
2076 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 case RPC_GARBAGE_ARGS:
Chuck Lever46121cf2007-01-31 12:14:08 -05002078 dprintk("RPC: %5u %s: server saw garbage\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08002079 task->tk_pid, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 break; /* retry */
2081 default:
Trond Myklebust8a702bb2007-06-27 18:30:26 -04002082 dprintk("RPC: %5u %s: server accept status: %x\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08002083 task->tk_pid, __func__, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 /* Also retry */
2085 }
2086
Trond Myklebustabbcf282006-01-03 09:55:03 +01002087out_garbage:
Trond Myklebust4e0038b2012-03-01 17:01:05 -05002088 clnt->cl_stats->rpcgarbage++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 if (task->tk_garb_retry) {
2090 task->tk_garb_retry--;
Chuck Lever46121cf2007-01-31 12:14:08 -05002091 dprintk("RPC: %5u %s: retrying\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08002092 task->tk_pid, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 task->tk_action = call_bind;
Trond Myklebustabbcf282006-01-03 09:55:03 +01002094out_retry:
2095 return ERR_PTR(-EAGAIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097out_eio:
2098 error = -EIO;
2099out_err:
2100 rpc_exit(task, error);
Trond Myklebust8a702bb2007-06-27 18:30:26 -04002101 dprintk("RPC: %5u %s: call failed with error %d\n", task->tk_pid,
Harvey Harrison0dc47872008-03-05 20:47:47 -08002102 __func__, error);
Trond Myklebustabbcf282006-01-03 09:55:03 +01002103 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104out_overflow:
Trond Myklebust8a702bb2007-06-27 18:30:26 -04002105 dprintk("RPC: %5u %s: server reply was truncated.\n", task->tk_pid,
Harvey Harrison0dc47872008-03-05 20:47:47 -08002106 __func__);
Trond Myklebustabbcf282006-01-03 09:55:03 +01002107 goto out_garbage;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108}
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00002109
Chuck Lever9f06c712010-12-14 14:59:18 +00002110static void rpcproc_encode_null(void *rqstp, struct xdr_stream *xdr, void *obj)
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00002111{
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00002112}
2113
Chuck Leverbf269552010-12-14 14:59:29 +00002114static int rpcproc_decode_null(void *rqstp, struct xdr_stream *xdr, void *obj)
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00002115{
2116 return 0;
2117}
2118
2119static struct rpc_procinfo rpcproc_null = {
2120 .p_encode = rpcproc_encode_null,
2121 .p_decode = rpcproc_decode_null,
2122};
2123
Chuck Levercaabea82009-12-03 15:58:56 -05002124static int rpc_ping(struct rpc_clnt *clnt)
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00002125{
2126 struct rpc_message msg = {
2127 .rpc_proc = &rpcproc_null,
2128 };
2129 int err;
2130 msg.rpc_cred = authnull_ops.lookup_cred(NULL, NULL, 0);
Chuck Levercaabea82009-12-03 15:58:56 -05002131 err = rpc_call_sync(clnt, &msg, RPC_TASK_SOFT | RPC_TASK_SOFTCONN);
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00002132 put_rpccred(msg.rpc_cred);
2133 return err;
2134}
Trond Myklebust188fef12007-06-16 14:18:40 -04002135
Trond Myklebust5e1550d2007-06-23 10:17:16 -04002136struct rpc_task *rpc_call_null(struct rpc_clnt *clnt, struct rpc_cred *cred, int flags)
2137{
2138 struct rpc_message msg = {
2139 .rpc_proc = &rpcproc_null,
2140 .rpc_cred = cred,
2141 };
Trond Myklebust84115e12007-07-14 15:39:59 -04002142 struct rpc_task_setup task_setup_data = {
2143 .rpc_client = clnt,
2144 .rpc_message = &msg,
2145 .callback_ops = &rpc_default_ops,
2146 .flags = flags,
2147 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002148 return rpc_run_task(&task_setup_data);
Trond Myklebust5e1550d2007-06-23 10:17:16 -04002149}
Trond Myklebuste8914c62007-07-14 15:39:59 -04002150EXPORT_SYMBOL_GPL(rpc_call_null);
Trond Myklebust5e1550d2007-06-23 10:17:16 -04002151
Trond Myklebust188fef12007-06-16 14:18:40 -04002152#ifdef RPC_DEBUG
Chuck Lever68a23ee2008-05-21 17:09:26 -04002153static void rpc_show_header(void)
2154{
Chuck Levercb3997b2008-05-21 17:09:41 -04002155 printk(KERN_INFO "-pid- flgs status -client- --rqstp- "
2156 "-timeout ---ops--\n");
Chuck Lever68a23ee2008-05-21 17:09:26 -04002157}
2158
Chuck Lever38e886e2008-05-21 17:09:33 -04002159static void rpc_show_task(const struct rpc_clnt *clnt,
2160 const struct rpc_task *task)
2161{
2162 const char *rpc_waitq = "none";
Chuck Lever38e886e2008-05-21 17:09:33 -04002163
2164 if (RPC_IS_QUEUED(task))
2165 rpc_waitq = rpc_qname(task->tk_waitqueue);
2166
Joe Perchesb3bceda2010-12-21 10:52:24 -05002167 printk(KERN_INFO "%5u %04x %6d %8p %8p %8ld %8p %sv%u %s a:%ps q:%s\n",
Chuck Levercb3997b2008-05-21 17:09:41 -04002168 task->tk_pid, task->tk_flags, task->tk_status,
2169 clnt, task->tk_rqstp, task->tk_timeout, task->tk_ops,
2170 clnt->cl_protname, clnt->cl_vers, rpc_proc_name(task),
Joe Perchesb3bceda2010-12-21 10:52:24 -05002171 task->tk_action, rpc_waitq);
Chuck Lever38e886e2008-05-21 17:09:33 -04002172}
2173
Stanislav Kinsbursky70abc492012-01-12 22:07:51 +04002174void rpc_show_tasks(struct net *net)
Trond Myklebust188fef12007-06-16 14:18:40 -04002175{
2176 struct rpc_clnt *clnt;
Chuck Lever38e886e2008-05-21 17:09:33 -04002177 struct rpc_task *task;
Chuck Lever68a23ee2008-05-21 17:09:26 -04002178 int header = 0;
Stanislav Kinsbursky70abc492012-01-12 22:07:51 +04002179 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
Trond Myklebust188fef12007-06-16 14:18:40 -04002180
Stanislav Kinsbursky70abc492012-01-12 22:07:51 +04002181 spin_lock(&sn->rpc_client_lock);
2182 list_for_each_entry(clnt, &sn->all_clients, cl_clients) {
Trond Myklebust188fef12007-06-16 14:18:40 -04002183 spin_lock(&clnt->cl_lock);
Chuck Lever38e886e2008-05-21 17:09:33 -04002184 list_for_each_entry(task, &clnt->cl_tasks, tk_task) {
Chuck Lever68a23ee2008-05-21 17:09:26 -04002185 if (!header) {
2186 rpc_show_header();
2187 header++;
2188 }
Chuck Lever38e886e2008-05-21 17:09:33 -04002189 rpc_show_task(clnt, task);
Trond Myklebust188fef12007-06-16 14:18:40 -04002190 }
2191 spin_unlock(&clnt->cl_lock);
2192 }
Stanislav Kinsbursky70abc492012-01-12 22:07:51 +04002193 spin_unlock(&sn->rpc_client_lock);
Trond Myklebust188fef12007-06-16 14:18:40 -04002194}
2195#endif