blob: 3c074de902ea2bd1844a9dc293407528f146c817 [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>
Jeff Layton59766872013-02-04 12:50:00 -050036#include <linux/sunrpc/addr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/sunrpc/rpc_pipe_fs.h>
Chuck Lever11c556b2006-03-20 13:44:22 -050038#include <linux/sunrpc/metrics.h>
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -040039#include <linux/sunrpc/bc_xprt.h>
Steve Dickson5753cba2012-02-06 10:08:08 -050040#include <trace/events/sunrpc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -040042#include "sunrpc.h"
Stanislav Kinsbursky70abc492012-01-12 22:07:51 +040043#include "netns.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#ifdef RPC_DEBUG
46# define RPCDBG_FACILITY RPCDBG_CALL
47#endif
48
Chuck Lever46121cf2007-01-31 12:14:08 -050049#define dprint_status(t) \
50 dprintk("RPC: %5u %s (status %d)\n", t->tk_pid, \
Harvey Harrison0dc47872008-03-05 20:47:47 -080051 __func__, t->tk_status)
Chuck Lever46121cf2007-01-31 12:14:08 -050052
Trond Myklebust188fef12007-06-16 14:18:40 -040053/*
54 * All RPC clients are linked into this list
55 */
Trond Myklebust188fef12007-06-16 14:18:40 -040056
Linus Torvalds1da177e2005-04-16 15:20:36 -070057static DECLARE_WAIT_QUEUE_HEAD(destroy_wait);
58
59
60static void call_start(struct rpc_task *task);
61static void call_reserve(struct rpc_task *task);
62static void call_reserveresult(struct rpc_task *task);
63static void call_allocate(struct rpc_task *task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064static void call_decode(struct rpc_task *task);
65static void call_bind(struct rpc_task *task);
Chuck Leverda351872005-08-11 16:25:11 -040066static void call_bind_status(struct rpc_task *task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067static void call_transmit(struct rpc_task *task);
Trond Myklebust9e00abc2011-07-13 19:20:49 -040068#if defined(CONFIG_SUNRPC_BACKCHANNEL)
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -040069static void call_bc_transmit(struct rpc_task *task);
Trond Myklebust9e00abc2011-07-13 19:20:49 -040070#endif /* CONFIG_SUNRPC_BACKCHANNEL */
Linus Torvalds1da177e2005-04-16 15:20:36 -070071static void call_status(struct rpc_task *task);
Trond Myklebust940e3312005-11-09 21:45:24 -050072static void call_transmit_status(struct rpc_task *task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073static void call_refresh(struct rpc_task *task);
74static void call_refreshresult(struct rpc_task *task);
75static void call_timeout(struct rpc_task *task);
76static void call_connect(struct rpc_task *task);
77static void call_connect_status(struct rpc_task *task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Chuck Leverb0e1c572008-05-21 17:09:19 -040079static __be32 *rpc_encode_header(struct rpc_task *task);
80static __be32 *rpc_verify_header(struct rpc_task *task);
Chuck Levercaabea82009-12-03 15:58:56 -050081static int rpc_ping(struct rpc_clnt *clnt);
Trond Myklebust64c91a12007-06-23 10:17:16 -040082
Trond Myklebust188fef12007-06-16 14:18:40 -040083static void rpc_register_client(struct rpc_clnt *clnt)
84{
Trond Myklebust2446ab62012-03-01 17:00:56 -050085 struct net *net = rpc_net_ns(clnt);
86 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
Stanislav Kinsbursky70abc492012-01-12 22:07:51 +040087
88 spin_lock(&sn->rpc_client_lock);
89 list_add(&clnt->cl_clients, &sn->all_clients);
90 spin_unlock(&sn->rpc_client_lock);
Trond Myklebust188fef12007-06-16 14:18:40 -040091}
92
93static void rpc_unregister_client(struct rpc_clnt *clnt)
94{
Trond Myklebust2446ab62012-03-01 17:00:56 -050095 struct net *net = rpc_net_ns(clnt);
96 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
Stanislav Kinsbursky70abc492012-01-12 22:07:51 +040097
98 spin_lock(&sn->rpc_client_lock);
Trond Myklebust188fef12007-06-16 14:18:40 -040099 list_del(&clnt->cl_clients);
Stanislav Kinsbursky70abc492012-01-12 22:07:51 +0400100 spin_unlock(&sn->rpc_client_lock);
Trond Myklebust188fef12007-06-16 14:18:40 -0400101}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400103static void __rpc_clnt_remove_pipedir(struct rpc_clnt *clnt)
104{
Trond Myklebust5f42b012013-08-26 16:47:20 -0400105 if (clnt->cl_dentry)
Trond Myklebust6739ffb2013-08-26 15:38:11 -0400106 rpc_remove_client_dir(clnt->cl_dentry, clnt);
Stanislav Kinsbursky30507f52012-01-11 19:18:42 +0400107 clnt->cl_dentry = NULL;
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400108}
109
110static void rpc_clnt_remove_pipedir(struct rpc_clnt *clnt)
111{
Trond Myklebust2446ab62012-03-01 17:00:56 -0500112 struct net *net = rpc_net_ns(clnt);
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400113 struct super_block *pipefs_sb;
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400114
Trond Myklebust2446ab62012-03-01 17:00:56 -0500115 pipefs_sb = rpc_get_sb_net(net);
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400116 if (pipefs_sb) {
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400117 __rpc_clnt_remove_pipedir(clnt);
Trond Myklebust2446ab62012-03-01 17:00:56 -0500118 rpc_put_sb_net(net);
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400119 }
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400120}
121
122static struct dentry *rpc_setup_pipedir_sb(struct super_block *sb,
Trond Myklebust41b6b4d2013-08-23 13:03:50 -0400123 struct rpc_clnt *clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
Trond Myklebustf1345852005-09-23 11:08:25 -0400125 static uint32_t clntid;
Trond Myklebust41b6b4d2013-08-23 13:03:50 -0400126 const char *dir_name = clnt->cl_program->pipe_dir_name;
Trond Myklebust23ac6582009-08-09 15:14:25 -0400127 char name[15];
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400128 struct dentry *dir, *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400130 dir = rpc_d_lookup_sb(sb, dir_name);
Weston Andros Adamson922eeac2012-10-23 10:43:25 -0400131 if (dir == NULL) {
132 pr_info("RPC: pipefs directory doesn't exist: %s\n", dir_name);
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400133 return dir;
Weston Andros Adamson922eeac2012-10-23 10:43:25 -0400134 }
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400135 for (;;) {
Al Viroa95e6912013-07-14 16:43:54 +0400136 snprintf(name, sizeof(name), "clnt%x", (unsigned int)clntid++);
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400137 name[sizeof(name) - 1] = '\0';
Al Viroa95e6912013-07-14 16:43:54 +0400138 dentry = rpc_create_client_dir(dir, name, clnt);
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400139 if (!IS_ERR(dentry))
140 break;
Al Viroa95e6912013-07-14 16:43:54 +0400141 if (dentry == ERR_PTR(-EEXIST))
142 continue;
143 printk(KERN_INFO "RPC: Couldn't create pipefs entry"
144 " %s/%s, error %ld\n",
145 dir_name, name, PTR_ERR(dentry));
146 break;
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400147 }
148 dput(dir);
149 return dentry;
150}
151
152static int
Trond Myklebust41b6b4d2013-08-23 13:03:50 -0400153rpc_setup_pipedir(struct super_block *pipefs_sb, struct rpc_clnt *clnt)
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400154{
Stanislav Kinsbursky30507f52012-01-11 19:18:42 +0400155 struct dentry *dentry;
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400156
Trond Myklebust41b6b4d2013-08-23 13:03:50 -0400157 if (clnt->cl_program->pipe_dir_name == NULL)
158 goto out;
Stanislav Kinsbursky30507f52012-01-11 19:18:42 +0400159 clnt->cl_dentry = NULL;
Trond Myklebust41b6b4d2013-08-23 13:03:50 -0400160 dentry = rpc_setup_pipedir_sb(pipefs_sb, clnt);
Stanislav Kinsbursky30507f52012-01-11 19:18:42 +0400161 if (IS_ERR(dentry))
162 return PTR_ERR(dentry);
163 clnt->cl_dentry = dentry;
Trond Myklebust41b6b4d2013-08-23 13:03:50 -0400164out:
Trond Myklebust23ac6582009-08-09 15:14:25 -0400165 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166}
167
Trond Myklebust41b6b4d2013-08-23 13:03:50 -0400168static int rpc_clnt_skip_event(struct rpc_clnt *clnt, unsigned long event)
Stanislav Kinsburskyea8cfa02012-04-27 13:00:17 +0400169{
Trond Myklebust41b6b4d2013-08-23 13:03:50 -0400170 if (clnt->cl_program->pipe_dir_name == NULL)
171 return 1;
172
Stanislav Kinsburskyea8cfa02012-04-27 13:00:17 +0400173 if (((event == RPC_PIPEFS_MOUNT) && clnt->cl_dentry) ||
174 ((event == RPC_PIPEFS_UMOUNT) && !clnt->cl_dentry))
175 return 1;
Stanislav Kinsbursky4f6bb242013-06-24 11:52:59 +0400176 if ((event == RPC_PIPEFS_MOUNT) && atomic_read(&clnt->cl_count) == 0)
177 return 1;
Stanislav Kinsburskyea8cfa02012-04-27 13:00:17 +0400178 return 0;
179}
180
181static int __rpc_clnt_handle_event(struct rpc_clnt *clnt, unsigned long event,
182 struct super_block *sb)
Stanislav Kinsbursky80df9d22012-01-11 19:18:17 +0400183{
184 struct dentry *dentry;
185 int err = 0;
186
187 switch (event) {
188 case RPC_PIPEFS_MOUNT:
Trond Myklebust41b6b4d2013-08-23 13:03:50 -0400189 dentry = rpc_setup_pipedir_sb(sb, clnt);
Weston Andros Adamson922eeac2012-10-23 10:43:25 -0400190 if (!dentry)
191 return -ENOENT;
Stanislav Kinsbursky80df9d22012-01-11 19:18:17 +0400192 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 break;
196 case RPC_PIPEFS_UMOUNT:
197 __rpc_clnt_remove_pipedir(clnt);
198 break;
199 default:
200 printk(KERN_ERR "%s: unknown event: %ld\n", __func__, event);
201 return -ENOTSUPP;
202 }
203 return err;
204}
205
Stanislav Kinsburskyea8cfa02012-04-27 13:00:17 +0400206static int __rpc_pipefs_event(struct rpc_clnt *clnt, unsigned long event,
207 struct super_block *sb)
208{
209 int error = 0;
210
211 for (;; clnt = clnt->cl_parent) {
212 if (!rpc_clnt_skip_event(clnt, event))
213 error = __rpc_clnt_handle_event(clnt, event, sb);
214 if (error || clnt == clnt->cl_parent)
215 break;
216 }
217 return error;
218}
219
Stanislav Kinsburskyda3b4622012-02-27 22:05:29 +0400220static struct rpc_clnt *rpc_get_client_for_event(struct net *net, int event)
221{
222 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
223 struct rpc_clnt *clnt;
224
225 spin_lock(&sn->rpc_client_lock);
226 list_for_each_entry(clnt, &sn->all_clients, cl_clients) {
Stanislav Kinsburskyea8cfa02012-04-27 13:00:17 +0400227 if (rpc_clnt_skip_event(clnt, event))
Stanislav Kinsburskyda3b4622012-02-27 22:05:29 +0400228 continue;
Stanislav Kinsburskyda3b4622012-02-27 22:05:29 +0400229 spin_unlock(&sn->rpc_client_lock);
230 return clnt;
231 }
232 spin_unlock(&sn->rpc_client_lock);
233 return NULL;
234}
235
Stanislav Kinsbursky80df9d22012-01-11 19:18:17 +0400236static int rpc_pipefs_event(struct notifier_block *nb, unsigned long event,
237 void *ptr)
238{
239 struct super_block *sb = ptr;
240 struct rpc_clnt *clnt;
241 int error = 0;
Stanislav Kinsbursky80df9d22012-01-11 19:18:17 +0400242
Stanislav Kinsburskyda3b4622012-02-27 22:05:29 +0400243 while ((clnt = rpc_get_client_for_event(sb->s_fs_info, event))) {
Stanislav Kinsbursky80df9d22012-01-11 19:18:17 +0400244 error = __rpc_pipefs_event(clnt, event, sb);
245 if (error)
246 break;
247 }
Stanislav Kinsbursky80df9d22012-01-11 19:18:17 +0400248 return error;
249}
250
251static struct notifier_block rpc_clients_block = {
252 .notifier_call = rpc_pipefs_event,
Stanislav Kinsburskyeee17322012-01-10 16:13:19 +0400253 .priority = SUNRPC_PIPEFS_RPC_PRIO,
Stanislav Kinsbursky80df9d22012-01-11 19:18:17 +0400254};
255
256int rpc_clients_notifier_register(void)
257{
258 return rpc_pipefs_notifier_register(&rpc_clients_block);
259}
260
261void rpc_clients_notifier_unregister(void)
262{
263 return rpc_pipefs_notifier_unregister(&rpc_clients_block);
264}
265
Trond Myklebustcbbb3442012-04-30 11:52:40 -0400266static void rpc_clnt_set_nodename(struct rpc_clnt *clnt, const char *nodename)
267{
268 clnt->cl_nodelen = strlen(nodename);
269 if (clnt->cl_nodelen > UNX_MAXNODENAME)
270 clnt->cl_nodelen = UNX_MAXNODENAME;
271 memcpy(clnt->cl_nodename, nodename, clnt->cl_nodelen);
272}
273
Stanislav Kinsburskye73f4cc2013-06-24 11:52:52 +0400274static int rpc_client_register(const struct rpc_create_args *args,
275 struct rpc_clnt *clnt)
276{
Trond Myklebustc2190662013-08-26 19:23:04 -0400277 struct rpc_auth_create_args auth_args = {
278 .pseudoflavor = args->authflavor,
279 .target_name = args->client_name,
280 };
Stanislav Kinsburskye73f4cc2013-06-24 11:52:52 +0400281 struct rpc_auth *auth;
282 struct net *net = rpc_net_ns(clnt);
283 struct super_block *pipefs_sb;
Trond Myklebusteeee2452013-07-10 15:33:01 -0400284 int err;
Stanislav Kinsburskye73f4cc2013-06-24 11:52:52 +0400285
286 pipefs_sb = rpc_get_sb_net(net);
287 if (pipefs_sb) {
Trond Myklebust41b6b4d2013-08-23 13:03:50 -0400288 err = rpc_setup_pipedir(pipefs_sb, clnt);
Stanislav Kinsburskye73f4cc2013-06-24 11:52:52 +0400289 if (err)
290 goto out;
291 }
292
Trond Myklebusteeee2452013-07-10 15:33:01 -0400293 rpc_register_client(clnt);
294 if (pipefs_sb)
295 rpc_put_sb_net(net);
296
Trond Myklebustc2190662013-08-26 19:23:04 -0400297 auth = rpcauth_create(&auth_args, clnt);
Stanislav Kinsburskye73f4cc2013-06-24 11:52:52 +0400298 if (IS_ERR(auth)) {
299 dprintk("RPC: Couldn't create auth handle (flavor %u)\n",
300 args->authflavor);
301 err = PTR_ERR(auth);
302 goto err_auth;
303 }
Trond Myklebusteeee2452013-07-10 15:33:01 -0400304 return 0;
305err_auth:
306 pipefs_sb = rpc_get_sb_net(net);
Trond Myklebust1540c5d2013-07-14 22:57:50 -0400307 rpc_unregister_client(clnt);
Trond Myklebusteeee2452013-07-10 15:33:01 -0400308 __rpc_clnt_remove_pipedir(clnt);
Stanislav Kinsburskye73f4cc2013-06-24 11:52:52 +0400309out:
310 if (pipefs_sb)
311 rpc_put_sb_net(net);
312 return err;
Stanislav Kinsburskye73f4cc2013-06-24 11:52:52 +0400313}
314
Trond Myklebust698b6d02007-12-20 16:03:53 -0500315static struct rpc_clnt * rpc_new_client(const struct rpc_create_args *args, struct rpc_xprt *xprt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316{
Trond Myklebusta613fa12012-01-20 13:53:56 -0500317 const struct rpc_program *program = args->program;
318 const struct rpc_version *version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 struct rpc_clnt *clnt = NULL;
320 int err;
Chuck Lever06b8d252007-09-11 18:00:20 -0400321
322 /* sanity check the name before trying to print it */
Chuck Lever46121cf2007-01-31 12:14:08 -0500323 dprintk("RPC: creating %s client for %s (xprt %p)\n",
Trond Myklebust698b6d02007-12-20 16:03:53 -0500324 program->name, args->servername, xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Trond Myklebust4ada5392007-06-14 17:26:17 -0400326 err = rpciod_up();
327 if (err)
328 goto out_no_rpciod;
Trond Myklebust698b6d02007-12-20 16:03:53 -0500329
Trond Myklebustf05c1242013-04-05 14:13:21 -0400330 err = -EINVAL;
Trond Myklebust698b6d02007-12-20 16:03:53 -0500331 if (args->version >= program->nrvers)
332 goto out_err;
333 version = program->version[args->version];
334 if (version == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 goto out_err;
336
337 err = -ENOMEM;
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700338 clnt = kzalloc(sizeof(*clnt), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 if (!clnt)
340 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 clnt->cl_parent = clnt;
342
Trond Myklebust2446ab62012-03-01 17:00:56 -0500343 rcu_assign_pointer(clnt->cl_xprt, xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 clnt->cl_procinfo = version->procs;
345 clnt->cl_maxproc = version->nrprocs;
Benny Halevyd5b337b2008-09-28 09:21:26 +0300346 clnt->cl_prog = args->prognumber ? : program->number;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 clnt->cl_vers = version->number;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 clnt->cl_stats = program->stats;
Chuck Lever11c556b2006-03-20 13:44:22 -0500349 clnt->cl_metrics = rpc_alloc_iostats(clnt);
Trond Myklebust6739ffb2013-08-26 15:38:11 -0400350 rpc_init_pipe_dir_head(&clnt->cl_pipedir_objects);
Trond Myklebust23bf85b2006-11-21 10:40:23 -0500351 err = -ENOMEM;
352 if (clnt->cl_metrics == NULL)
353 goto out_no_stats;
Trond Myklebust3e32a5d2006-11-16 11:37:27 -0500354 clnt->cl_program = program;
Trond Myklebust6529eba2007-06-14 16:40:14 -0400355 INIT_LIST_HEAD(&clnt->cl_tasks);
Trond Myklebust4bef61f2007-06-16 14:17:01 -0400356 spin_lock_init(&clnt->cl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
Trond Myklebust2446ab62012-03-01 17:00:56 -0500358 if (!xprt_bound(xprt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 clnt->cl_autobind = 1;
360
Trond Myklebustba7392b2007-12-20 16:03:55 -0500361 clnt->cl_timeout = xprt->timeout;
362 if (args->timeout != NULL) {
363 memcpy(&clnt->cl_timeout_default, args->timeout,
364 sizeof(clnt->cl_timeout_default));
365 clnt->cl_timeout = &clnt->cl_timeout_default;
366 }
367
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 clnt->cl_rtt = &clnt->cl_rtt_default;
Trond Myklebustba7392b2007-12-20 16:03:55 -0500369 rpc_init_rtt(&clnt->cl_rtt_default, clnt->cl_timeout->to_initval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Trond Myklebust006abe82010-09-12 19:55:25 -0400371 atomic_set(&clnt->cl_count, 1);
Trond Myklebust34f52e32007-06-14 16:40:31 -0400372
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 /* save the nodename */
Trond Myklebustcbbb3442012-04-30 11:52:40 -0400374 rpc_clnt_set_nodename(clnt, utsname()->nodename);
Stanislav Kinsburskye73f4cc2013-06-24 11:52:52 +0400375
376 err = rpc_client_register(args, clnt);
377 if (err)
378 goto out_no_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 return clnt;
380
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381out_no_path:
Trond Myklebust23bf85b2006-11-21 10:40:23 -0500382 rpc_free_iostats(clnt->cl_metrics);
383out_no_stats:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 kfree(clnt);
385out_err:
Trond Myklebust4ada5392007-06-14 17:26:17 -0400386 rpciod_down();
387out_no_rpciod:
Trond Myklebustf05c1242013-04-05 14:13:21 -0400388 xprt_put(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 return ERR_PTR(err);
390}
391
Ben Hutchings2c530402012-07-10 10:55:09 +0000392/**
Chuck Leverc2866762006-08-22 20:06:20 -0400393 * rpc_create - create an RPC client and transport with one call
394 * @args: rpc_clnt create argument structure
395 *
396 * Creates and initializes an RPC transport and an RPC client.
397 *
398 * It can ping the server in order to determine if it is up, and to see if
399 * it supports this program and version. RPC_CLNT_CREATE_NOPING disables
400 * this behavior so asynchronous tasks can also use rpc_create.
401 */
402struct rpc_clnt *rpc_create(struct rpc_create_args *args)
403{
404 struct rpc_xprt *xprt;
405 struct rpc_clnt *clnt;
\"Talpey, Thomas\3c341b0b2007-09-10 13:47:07 -0400406 struct xprt_create xprtargs = {
Pavel Emelyanov9a23e332010-09-29 16:05:12 +0400407 .net = args->net,
\"Talpey, Thomas\4fa016e2007-09-10 13:47:57 -0400408 .ident = args->protocol,
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +0200409 .srcaddr = args->saddress,
Frank van Maarseveen96802a02007-07-08 13:08:54 +0200410 .dstaddr = args->address,
411 .addrlen = args->addrsize,
Trond Myklebust4e0038b2012-03-01 17:01:05 -0500412 .servername = args->servername,
Alexandros Batsakisf300bab2009-09-10 17:33:30 +0300413 .bc_xprt = args->bc_xprt,
Frank van Maarseveen96802a02007-07-08 13:08:54 +0200414 };
Chuck Lever510deb02007-12-10 14:56:24 -0500415 char servername[48];
Chuck Leverc2866762006-08-22 20:06:20 -0400416
Trond Myklebustb7993ce2013-04-14 11:42:00 -0400417 if (args->flags & RPC_CLNT_CREATE_INFINITE_SLOTS)
418 xprtargs.flags |= XPRT_CREATE_INFINITE_SLOTS;
J. Bruce Fields33d90ac2013-04-11 15:06:36 -0400419 if (args->flags & RPC_CLNT_CREATE_NO_IDLE_TIMEOUT)
420 xprtargs.flags |= XPRT_CREATE_NO_IDLE_TIMEOUT;
Chuck Leverc2866762006-08-22 20:06:20 -0400421 /*
Chuck Lever43780b82007-07-01 12:13:22 -0400422 * If the caller chooses not to specify a hostname, whip
423 * up a string representation of the passed-in address.
424 */
Trond Myklebust4e0038b2012-03-01 17:01:05 -0500425 if (xprtargs.servername == NULL) {
Chuck Lever176e21e2011-05-09 15:22:44 -0400426 struct sockaddr_un *sun =
427 (struct sockaddr_un *)args->address;
Chuck Leverda09eb92011-05-09 15:22:25 -0400428 struct sockaddr_in *sin =
429 (struct sockaddr_in *)args->address;
430 struct sockaddr_in6 *sin6 =
431 (struct sockaddr_in6 *)args->address;
432
Chuck Lever510deb02007-12-10 14:56:24 -0500433 servername[0] = '\0';
434 switch (args->address->sa_family) {
Chuck Lever176e21e2011-05-09 15:22:44 -0400435 case AF_LOCAL:
436 snprintf(servername, sizeof(servername), "%s",
437 sun->sun_path);
438 break;
Chuck Leverda09eb92011-05-09 15:22:25 -0400439 case AF_INET:
Harvey Harrison21454aa2008-10-31 00:54:56 -0700440 snprintf(servername, sizeof(servername), "%pI4",
441 &sin->sin_addr.s_addr);
Chuck Lever510deb02007-12-10 14:56:24 -0500442 break;
Chuck Leverda09eb92011-05-09 15:22:25 -0400443 case AF_INET6:
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700444 snprintf(servername, sizeof(servername), "%pI6",
Chuck Leverda09eb92011-05-09 15:22:25 -0400445 &sin6->sin6_addr);
Chuck Lever510deb02007-12-10 14:56:24 -0500446 break;
Chuck Lever510deb02007-12-10 14:56:24 -0500447 default:
448 /* caller wants default server name, but
449 * address family isn't recognized. */
450 return ERR_PTR(-EINVAL);
451 }
Trond Myklebust4e0038b2012-03-01 17:01:05 -0500452 xprtargs.servername = servername;
Chuck Lever43780b82007-07-01 12:13:22 -0400453 }
454
Chuck Lever510deb02007-12-10 14:56:24 -0500455 xprt = xprt_create_transport(&xprtargs);
456 if (IS_ERR(xprt))
457 return (struct rpc_clnt *)xprt;
458
Chuck Lever43780b82007-07-01 12:13:22 -0400459 /*
Chuck Leverc2866762006-08-22 20:06:20 -0400460 * By default, kernel RPC client connects from a reserved port.
461 * CAP_NET_BIND_SERVICE will not be set for unprivileged requesters,
462 * but it is always enabled for rpciod, which handles the connect
463 * operation.
464 */
465 xprt->resvport = 1;
466 if (args->flags & RPC_CLNT_CREATE_NONPRIVPORT)
467 xprt->resvport = 0;
468
Trond Myklebust698b6d02007-12-20 16:03:53 -0500469 clnt = rpc_new_client(args, xprt);
Chuck Leverc2866762006-08-22 20:06:20 -0400470 if (IS_ERR(clnt))
471 return clnt;
472
473 if (!(args->flags & RPC_CLNT_CREATE_NOPING)) {
Chuck Levercaabea82009-12-03 15:58:56 -0500474 int err = rpc_ping(clnt);
Chuck Leverc2866762006-08-22 20:06:20 -0400475 if (err != 0) {
476 rpc_shutdown_client(clnt);
477 return ERR_PTR(err);
478 }
479 }
480
481 clnt->cl_softrtry = 1;
482 if (args->flags & RPC_CLNT_CREATE_HARDRTRY)
483 clnt->cl_softrtry = 0;
484
Chuck Leverc2866762006-08-22 20:06:20 -0400485 if (args->flags & RPC_CLNT_CREATE_AUTOBIND)
486 clnt->cl_autobind = 1;
Chuck Lever43d78ef2007-02-06 18:26:11 -0500487 if (args->flags & RPC_CLNT_CREATE_DISCRTRY)
488 clnt->cl_discrtry = 1;
Olga Kornievskaiab6b61522008-06-09 16:51:31 -0400489 if (!(args->flags & RPC_CLNT_CREATE_QUIET))
490 clnt->cl_chatty = 1;
Chuck Leverc2866762006-08-22 20:06:20 -0400491
492 return clnt;
493}
Chuck Leverb86acd52006-08-22 20:06:22 -0400494EXPORT_SYMBOL_GPL(rpc_create);
Chuck Leverc2866762006-08-22 20:06:20 -0400495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496/*
497 * This function clones the RPC client structure. It allows us to share the
498 * same transport while varying parameters such as the authentication
499 * flavour.
500 */
Chuck Lever1b63a752012-09-14 17:23:52 -0400501static struct rpc_clnt *__rpc_clone_client(struct rpc_create_args *args,
502 struct rpc_clnt *clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503{
Trond Myklebust2446ab62012-03-01 17:00:56 -0500504 struct rpc_xprt *xprt;
Chuck Lever1b63a752012-09-14 17:23:52 -0400505 struct rpc_clnt *new;
506 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
Chuck Lever1b63a752012-09-14 17:23:52 -0400508 err = -ENOMEM;
Trond Myklebust2446ab62012-03-01 17:00:56 -0500509 rcu_read_lock();
510 xprt = xprt_get(rcu_dereference(clnt->cl_xprt));
511 rcu_read_unlock();
512 if (xprt == NULL)
Chuck Lever1b63a752012-09-14 17:23:52 -0400513 goto out_err;
514 args->servername = xprt->servername;
515
516 new = rpc_new_client(args, xprt);
517 if (IS_ERR(new)) {
518 err = PTR_ERR(new);
Chuck Levera58e0be2013-03-22 12:52:59 -0400519 goto out_err;
Chuck Lever1b63a752012-09-14 17:23:52 -0400520 }
521
Trond Myklebust006abe82010-09-12 19:55:25 -0400522 atomic_inc(&clnt->cl_count);
Chuck Lever1b63a752012-09-14 17:23:52 -0400523 new->cl_parent = clnt;
524
525 /* Turn off autobind on clones */
526 new->cl_autobind = 0;
527 new->cl_softrtry = clnt->cl_softrtry;
528 new->cl_discrtry = clnt->cl_discrtry;
529 new->cl_chatty = clnt->cl_chatty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 return new;
Chuck Lever1b63a752012-09-14 17:23:52 -0400531
Chuck Lever1b63a752012-09-14 17:23:52 -0400532out_err:
Harvey Harrison0dc47872008-03-05 20:47:47 -0800533 dprintk("RPC: %s: returned error %d\n", __func__, err);
Trond Myklebust3e32a5d2006-11-16 11:37:27 -0500534 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535}
Chuck Lever1b63a752012-09-14 17:23:52 -0400536
537/**
538 * rpc_clone_client - Clone an RPC client structure
539 *
540 * @clnt: RPC client whose parameters are copied
541 *
542 * Returns a fresh RPC client or an ERR_PTR.
543 */
544struct rpc_clnt *rpc_clone_client(struct rpc_clnt *clnt)
545{
546 struct rpc_create_args args = {
547 .program = clnt->cl_program,
548 .prognumber = clnt->cl_prog,
549 .version = clnt->cl_vers,
550 .authflavor = clnt->cl_auth->au_flavor,
Chuck Lever1b63a752012-09-14 17:23:52 -0400551 };
552 return __rpc_clone_client(&args, clnt);
553}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400554EXPORT_SYMBOL_GPL(rpc_clone_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Chuck Leverba9b5842012-09-14 17:24:02 -0400556/**
557 * rpc_clone_client_set_auth - Clone an RPC client structure and set its auth
558 *
559 * @clnt: RPC client whose parameters are copied
Randy Dunlap7144bca2013-01-09 17:12:35 -0800560 * @flavor: security flavor for new client
Chuck Leverba9b5842012-09-14 17:24:02 -0400561 *
562 * Returns a fresh RPC client or an ERR_PTR.
563 */
564struct rpc_clnt *
565rpc_clone_client_set_auth(struct rpc_clnt *clnt, rpc_authflavor_t flavor)
566{
567 struct rpc_create_args args = {
568 .program = clnt->cl_program,
569 .prognumber = clnt->cl_prog,
570 .version = clnt->cl_vers,
571 .authflavor = flavor,
Chuck Leverba9b5842012-09-14 17:24:02 -0400572 };
573 return __rpc_clone_client(&args, clnt);
574}
575EXPORT_SYMBOL_GPL(rpc_clone_client_set_auth);
576
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577/*
Trond Myklebust58f96122010-07-31 14:29:08 -0400578 * Kill all tasks for the given client.
579 * XXX: kill their descendants as well?
580 */
581void rpc_killall_tasks(struct rpc_clnt *clnt)
582{
583 struct rpc_task *rovr;
584
585
586 if (list_empty(&clnt->cl_tasks))
587 return;
588 dprintk("RPC: killing all tasks for client %p\n", clnt);
589 /*
590 * Spin lock all_tasks to prevent changes...
591 */
592 spin_lock(&clnt->cl_lock);
593 list_for_each_entry(rovr, &clnt->cl_tasks, tk_task) {
594 if (!RPC_IS_ACTIVATED(rovr))
595 continue;
596 if (!(rovr->tk_flags & RPC_TASK_KILLED)) {
597 rovr->tk_flags |= RPC_TASK_KILLED;
598 rpc_exit(rovr, -EIO);
Stanislav Kinsbursky8e26de22011-03-17 18:54:23 +0300599 if (RPC_IS_QUEUED(rovr))
600 rpc_wake_up_queued_task(rovr->tk_waitqueue,
601 rovr);
Trond Myklebust58f96122010-07-31 14:29:08 -0400602 }
603 }
604 spin_unlock(&clnt->cl_lock);
605}
606EXPORT_SYMBOL_GPL(rpc_killall_tasks);
607
608/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 * Properly shut down an RPC client, terminating all outstanding
Trond Myklebust90c57552007-06-09 19:49:36 -0400610 * requests.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 */
Trond Myklebust4c402b42007-06-14 16:40:32 -0400612void rpc_shutdown_client(struct rpc_clnt *clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613{
Weston Andros Adamson168e4b32012-10-30 17:01:40 -0400614 might_sleep();
615
Trond Myklebust4e0038b2012-03-01 17:01:05 -0500616 dprintk_rcu("RPC: shutting down %s client for %s\n",
Trond Myklebust55909f22013-08-23 11:48:15 -0400617 clnt->cl_program->name,
Trond Myklebust4e0038b2012-03-01 17:01:05 -0500618 rcu_dereference(clnt->cl_xprt)->servername);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
Trond Myklebust34f52e32007-06-14 16:40:31 -0400620 while (!list_empty(&clnt->cl_tasks)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 rpc_killall_tasks(clnt);
Ingo Molnar532347e2006-01-09 20:52:53 -0800622 wait_event_timeout(destroy_wait,
Trond Myklebust34f52e32007-06-14 16:40:31 -0400623 list_empty(&clnt->cl_tasks), 1*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 }
625
Trond Myklebust4c402b42007-06-14 16:40:32 -0400626 rpc_release_client(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400628EXPORT_SYMBOL_GPL(rpc_shutdown_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
630/*
Trond Myklebust34f52e32007-06-14 16:40:31 -0400631 * Free an RPC client
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 */
Trond Myklebust34f52e32007-06-14 16:40:31 -0400633static void
Trond Myklebust006abe82010-09-12 19:55:25 -0400634rpc_free_client(struct rpc_clnt *clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635{
Trond Myklebust4e0038b2012-03-01 17:01:05 -0500636 dprintk_rcu("RPC: destroying %s client for %s\n",
Trond Myklebust55909f22013-08-23 11:48:15 -0400637 clnt->cl_program->name,
Trond Myklebust4e0038b2012-03-01 17:01:05 -0500638 rcu_dereference(clnt->cl_xprt)->servername);
Trond Myklebust6eac7d32012-01-20 13:53:37 -0500639 if (clnt->cl_parent != clnt)
Trond Myklebust8ad7c892007-06-14 16:40:32 -0400640 rpc_release_client(clnt->cl_parent);
Stanislav Kinsburskyf5131252012-01-11 19:18:26 +0400641 rpc_clnt_remove_pipedir(clnt);
Stanislav Kinsburskyadb6fa72013-06-26 10:15:14 +0400642 rpc_unregister_client(clnt);
Chuck Lever11c556b2006-03-20 13:44:22 -0500643 rpc_free_iostats(clnt->cl_metrics);
644 clnt->cl_metrics = NULL;
Trond Myklebust2446ab62012-03-01 17:00:56 -0500645 xprt_put(rcu_dereference_raw(clnt->cl_xprt));
Trond Myklebust4ada5392007-06-14 17:26:17 -0400646 rpciod_down();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 kfree(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648}
649
650/*
Trond Myklebust1dd17ec2007-06-26 16:57:41 -0400651 * Free an RPC client
652 */
653static void
Trond Myklebust006abe82010-09-12 19:55:25 -0400654rpc_free_auth(struct rpc_clnt *clnt)
Trond Myklebust1dd17ec2007-06-26 16:57:41 -0400655{
Trond Myklebust1dd17ec2007-06-26 16:57:41 -0400656 if (clnt->cl_auth == NULL) {
Trond Myklebust006abe82010-09-12 19:55:25 -0400657 rpc_free_client(clnt);
Trond Myklebust1dd17ec2007-06-26 16:57:41 -0400658 return;
659 }
660
661 /*
662 * Note: RPCSEC_GSS may need to send NULL RPC calls in order to
663 * release remaining GSS contexts. This mechanism ensures
664 * that it can do so safely.
665 */
Trond Myklebust006abe82010-09-12 19:55:25 -0400666 atomic_inc(&clnt->cl_count);
Trond Myklebust1dd17ec2007-06-26 16:57:41 -0400667 rpcauth_release(clnt->cl_auth);
668 clnt->cl_auth = NULL;
Trond Myklebust006abe82010-09-12 19:55:25 -0400669 if (atomic_dec_and_test(&clnt->cl_count))
670 rpc_free_client(clnt);
Trond Myklebust1dd17ec2007-06-26 16:57:41 -0400671}
672
673/*
Trond Myklebust34f52e32007-06-14 16:40:31 -0400674 * Release reference to the RPC client
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 */
676void
677rpc_release_client(struct rpc_clnt *clnt)
678{
Trond Myklebust34f52e32007-06-14 16:40:31 -0400679 dprintk("RPC: rpc_release_client(%p)\n", clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
Trond Myklebust34f52e32007-06-14 16:40:31 -0400681 if (list_empty(&clnt->cl_tasks))
682 wake_up(&destroy_wait);
Trond Myklebust006abe82010-09-12 19:55:25 -0400683 if (atomic_dec_and_test(&clnt->cl_count))
684 rpc_free_auth(clnt);
Trond Myklebust34f52e32007-06-14 16:40:31 -0400685}
Simo Sorce1d658332012-05-25 18:09:55 -0400686EXPORT_SYMBOL_GPL(rpc_release_client);
Trond Myklebust34f52e32007-06-14 16:40:31 -0400687
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000688/**
689 * rpc_bind_new_program - bind a new RPC program to an existing client
Randy Dunlap65b6e422008-02-13 15:03:23 -0800690 * @old: old rpc_client
691 * @program: rpc program to set
692 * @vers: rpc program version
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000693 *
694 * Clones the rpc client and sets up a new RPC program. This is mainly
695 * of use for enabling different RPC programs to share the same transport.
696 * The Sun NFSv2/v3 ACL protocol can do this.
697 */
698struct rpc_clnt *rpc_bind_new_program(struct rpc_clnt *old,
Trond Myklebusta613fa12012-01-20 13:53:56 -0500699 const struct rpc_program *program,
Chuck Lever89eb21c2007-09-11 18:00:09 -0400700 u32 vers)
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000701{
Trond Myklebustf994c432012-11-01 12:14:14 -0400702 struct rpc_create_args args = {
703 .program = program,
704 .prognumber = program->number,
705 .version = vers,
706 .authflavor = old->cl_auth->au_flavor,
Trond Myklebustf994c432012-11-01 12:14:14 -0400707 };
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000708 struct rpc_clnt *clnt;
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000709 int err;
710
Trond Myklebustf994c432012-11-01 12:14:14 -0400711 clnt = __rpc_clone_client(&args, old);
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000712 if (IS_ERR(clnt))
713 goto out;
Chuck Levercaabea82009-12-03 15:58:56 -0500714 err = rpc_ping(clnt);
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000715 if (err != 0) {
716 rpc_shutdown_client(clnt);
717 clnt = ERR_PTR(err);
718 }
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800719out:
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000720 return clnt;
721}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400722EXPORT_SYMBOL_GPL(rpc_bind_new_program);
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000723
Trond Myklebust58f96122010-07-31 14:29:08 -0400724void rpc_task_release_client(struct rpc_task *task)
725{
726 struct rpc_clnt *clnt = task->tk_client;
727
728 if (clnt != NULL) {
729 /* Remove from client task list */
730 spin_lock(&clnt->cl_lock);
731 list_del(&task->tk_task);
732 spin_unlock(&clnt->cl_lock);
733 task->tk_client = NULL;
734
735 rpc_release_client(clnt);
736 }
737}
738
739static
740void rpc_task_set_client(struct rpc_task *task, struct rpc_clnt *clnt)
741{
742 if (clnt != NULL) {
743 rpc_task_release_client(task);
744 task->tk_client = clnt;
Trond Myklebust006abe82010-09-12 19:55:25 -0400745 atomic_inc(&clnt->cl_count);
Trond Myklebust58f96122010-07-31 14:29:08 -0400746 if (clnt->cl_softrtry)
747 task->tk_flags |= RPC_TASK_SOFT;
Mel Gormana564b8f2012-07-31 16:45:12 -0700748 if (sk_memalloc_socks()) {
749 struct rpc_xprt *xprt;
750
751 rcu_read_lock();
752 xprt = rcu_dereference(clnt->cl_xprt);
753 if (xprt->swapper)
754 task->tk_flags |= RPC_TASK_SWAPPER;
755 rcu_read_unlock();
756 }
Trond Myklebust58f96122010-07-31 14:29:08 -0400757 /* Add to the client's list of all tasks */
758 spin_lock(&clnt->cl_lock);
759 list_add_tail(&task->tk_task, &clnt->cl_tasks);
760 spin_unlock(&clnt->cl_lock);
761 }
762}
763
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +0000764void rpc_task_reset_client(struct rpc_task *task, struct rpc_clnt *clnt)
765{
766 rpc_task_release_client(task);
767 rpc_task_set_client(task, clnt);
768}
769EXPORT_SYMBOL_GPL(rpc_task_reset_client);
770
771
Trond Myklebust58f96122010-07-31 14:29:08 -0400772static void
773rpc_task_set_rpc_message(struct rpc_task *task, const struct rpc_message *msg)
774{
775 if (msg != NULL) {
776 task->tk_msg.rpc_proc = msg->rpc_proc;
777 task->tk_msg.rpc_argp = msg->rpc_argp;
778 task->tk_msg.rpc_resp = msg->rpc_resp;
Trond Myklebusta17c2152010-07-31 14:29:08 -0400779 if (msg->rpc_cred != NULL)
780 task->tk_msg.rpc_cred = get_rpccred(msg->rpc_cred);
Trond Myklebust58f96122010-07-31 14:29:08 -0400781 }
782}
783
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784/*
785 * Default callback for async RPC calls
786 */
787static void
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100788rpc_default_callback(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789{
790}
791
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100792static const struct rpc_call_ops rpc_default_ops = {
793 .rpc_call_done = rpc_default_callback,
794};
795
Trond Myklebustc970aa82007-07-14 15:39:59 -0400796/**
797 * rpc_run_task - Allocate a new RPC task, then run rpc_execute against it
798 * @task_setup_data: pointer to task initialisation data
799 */
800struct rpc_task *rpc_run_task(const struct rpc_task_setup *task_setup_data)
Trond Myklebust6e5b70e2007-06-12 10:02:37 -0400801{
Trond Myklebust19445b92010-04-16 16:41:10 -0400802 struct rpc_task *task;
Trond Myklebust6e5b70e2007-06-12 10:02:37 -0400803
Trond Myklebust84115e12007-07-14 15:39:59 -0400804 task = rpc_new_task(task_setup_data);
Trond Myklebust19445b92010-04-16 16:41:10 -0400805 if (IS_ERR(task))
Trond Myklebust50859252007-10-25 18:19:37 -0400806 goto out;
Trond Myklebust6e5b70e2007-06-12 10:02:37 -0400807
Trond Myklebust58f96122010-07-31 14:29:08 -0400808 rpc_task_set_client(task, task_setup_data->rpc_client);
809 rpc_task_set_rpc_message(task, task_setup_data->rpc_message);
810
Trond Myklebust58f96122010-07-31 14:29:08 -0400811 if (task->tk_action == NULL)
812 rpc_call_start(task);
813
Trond Myklebust6e5b70e2007-06-12 10:02:37 -0400814 atomic_inc(&task->tk_count);
815 rpc_execute(task);
Trond Myklebust6e5b70e2007-06-12 10:02:37 -0400816out:
Trond Myklebust19445b92010-04-16 16:41:10 -0400817 return task;
Trond Myklebust6e5b70e2007-06-12 10:02:37 -0400818}
Trond Myklebustc970aa82007-07-14 15:39:59 -0400819EXPORT_SYMBOL_GPL(rpc_run_task);
Trond Myklebust6e5b70e2007-06-12 10:02:37 -0400820
821/**
822 * rpc_call_sync - Perform a synchronous RPC call
823 * @clnt: pointer to RPC client
824 * @msg: RPC call parameters
825 * @flags: RPC call flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 */
Trond Myklebustcbc20052008-02-14 11:11:30 -0500827int rpc_call_sync(struct rpc_clnt *clnt, const struct rpc_message *msg, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828{
829 struct rpc_task *task;
Trond Myklebust84115e12007-07-14 15:39:59 -0400830 struct rpc_task_setup task_setup_data = {
831 .rpc_client = clnt,
832 .rpc_message = msg,
833 .callback_ops = &rpc_default_ops,
834 .flags = flags,
835 };
Trond Myklebust6e5b70e2007-06-12 10:02:37 -0400836 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
Weston Andros Adamson50d2bdb2012-11-01 16:04:40 -0400838 WARN_ON_ONCE(flags & RPC_TASK_ASYNC);
839 if (flags & RPC_TASK_ASYNC) {
840 rpc_release_calldata(task_setup_data.callback_ops,
841 task_setup_data.callback_data);
842 return -EINVAL;
843 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
Trond Myklebustc970aa82007-07-14 15:39:59 -0400845 task = rpc_run_task(&task_setup_data);
Trond Myklebust6e5b70e2007-06-12 10:02:37 -0400846 if (IS_ERR(task))
847 return PTR_ERR(task);
Trond Myklebuste60859a2006-01-03 09:55:10 +0100848 status = task->tk_status;
Trond Myklebustbde8f002007-01-24 11:54:53 -0800849 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 return status;
851}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400852EXPORT_SYMBOL_GPL(rpc_call_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
Trond Myklebust6e5b70e2007-06-12 10:02:37 -0400854/**
855 * rpc_call_async - Perform an asynchronous RPC call
856 * @clnt: pointer to RPC client
857 * @msg: RPC call parameters
858 * @flags: RPC call flags
Randy Dunlap65b6e422008-02-13 15:03:23 -0800859 * @tk_ops: RPC call ops
Trond Myklebust6e5b70e2007-06-12 10:02:37 -0400860 * @data: user call data
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 */
862int
Trond Myklebustcbc20052008-02-14 11:11:30 -0500863rpc_call_async(struct rpc_clnt *clnt, const struct rpc_message *msg, int flags,
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100864 const struct rpc_call_ops *tk_ops, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865{
866 struct rpc_task *task;
Trond Myklebust84115e12007-07-14 15:39:59 -0400867 struct rpc_task_setup task_setup_data = {
868 .rpc_client = clnt,
869 .rpc_message = msg,
870 .callback_ops = tk_ops,
871 .callback_data = data,
872 .flags = flags|RPC_TASK_ASYNC,
873 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
Trond Myklebustc970aa82007-07-14 15:39:59 -0400875 task = rpc_run_task(&task_setup_data);
Trond Myklebust6e5b70e2007-06-12 10:02:37 -0400876 if (IS_ERR(task))
877 return PTR_ERR(task);
878 rpc_put_task(task);
879 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400881EXPORT_SYMBOL_GPL(rpc_call_async);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
Trond Myklebust9e00abc2011-07-13 19:20:49 -0400883#if defined(CONFIG_SUNRPC_BACKCHANNEL)
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -0400884/**
885 * rpc_run_bc_task - Allocate a new RPC task for backchannel use, then run
886 * rpc_execute against it
Jaswinder Singh Rajput7a73fdd2009-09-24 14:58:42 -0400887 * @req: RPC request
888 * @tk_ops: RPC call ops
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -0400889 */
890struct rpc_task *rpc_run_bc_task(struct rpc_rqst *req,
Jaswinder Singh Rajput7a73fdd2009-09-24 14:58:42 -0400891 const struct rpc_call_ops *tk_ops)
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -0400892{
893 struct rpc_task *task;
894 struct xdr_buf *xbufp = &req->rq_snd_buf;
895 struct rpc_task_setup task_setup_data = {
896 .callback_ops = tk_ops,
897 };
898
899 dprintk("RPC: rpc_run_bc_task req= %p\n", req);
900 /*
901 * Create an rpc_task to send the data
902 */
903 task = rpc_new_task(&task_setup_data);
Trond Myklebust19445b92010-04-16 16:41:10 -0400904 if (IS_ERR(task)) {
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -0400905 xprt_free_bc_request(req);
906 goto out;
907 }
908 task->tk_rqstp = req;
909
910 /*
911 * Set up the xdr_buf length.
912 * This also indicates that the buffer is XDR encoded already.
913 */
914 xbufp->len = xbufp->head[0].iov_len + xbufp->page_len +
915 xbufp->tail[0].iov_len;
916
917 task->tk_action = call_bc_transmit;
918 atomic_inc(&task->tk_count);
Weston Andros Adamson9a6478f2012-10-23 10:43:27 -0400919 WARN_ON_ONCE(atomic_read(&task->tk_count) != 2);
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -0400920 rpc_execute(task);
921
922out:
923 dprintk("RPC: rpc_run_bc_task: task= %p\n", task);
924 return task;
925}
Trond Myklebust9e00abc2011-07-13 19:20:49 -0400926#endif /* CONFIG_SUNRPC_BACKCHANNEL */
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -0400927
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928void
Trond Myklebust77de2c52007-10-25 18:40:21 -0400929rpc_call_start(struct rpc_task *task)
930{
931 task->tk_action = call_start;
932}
933EXPORT_SYMBOL_GPL(rpc_call_start);
934
Chuck Levered394402006-08-22 20:06:17 -0400935/**
936 * rpc_peeraddr - extract remote peer address from clnt's xprt
937 * @clnt: RPC client structure
938 * @buf: target buffer
Randy Dunlap65b6e422008-02-13 15:03:23 -0800939 * @bufsize: length of target buffer
Chuck Levered394402006-08-22 20:06:17 -0400940 *
941 * Returns the number of bytes that are actually in the stored address.
942 */
943size_t rpc_peeraddr(struct rpc_clnt *clnt, struct sockaddr *buf, size_t bufsize)
944{
945 size_t bytes;
Trond Myklebust2446ab62012-03-01 17:00:56 -0500946 struct rpc_xprt *xprt;
Chuck Levered394402006-08-22 20:06:17 -0400947
Trond Myklebust2446ab62012-03-01 17:00:56 -0500948 rcu_read_lock();
949 xprt = rcu_dereference(clnt->cl_xprt);
950
951 bytes = xprt->addrlen;
Chuck Levered394402006-08-22 20:06:17 -0400952 if (bytes > bufsize)
953 bytes = bufsize;
Trond Myklebust2446ab62012-03-01 17:00:56 -0500954 memcpy(buf, &xprt->addr, bytes);
955 rcu_read_unlock();
956
957 return bytes;
Chuck Levered394402006-08-22 20:06:17 -0400958}
Chuck Leverb86acd52006-08-22 20:06:22 -0400959EXPORT_SYMBOL_GPL(rpc_peeraddr);
Chuck Levered394402006-08-22 20:06:17 -0400960
Chuck Leverf425eba2006-08-22 20:06:18 -0400961/**
962 * rpc_peeraddr2str - return remote peer address in printable format
963 * @clnt: RPC client structure
964 * @format: address format
965 *
Trond Myklebust2446ab62012-03-01 17:00:56 -0500966 * NB: the lifetime of the memory referenced by the returned pointer is
967 * the same as the rpc_xprt itself. As long as the caller uses this
968 * pointer, it must hold the RCU read lock.
Chuck Leverf425eba2006-08-22 20:06:18 -0400969 */
Chuck Leverb454ae92008-01-07 18:34:48 -0500970const char *rpc_peeraddr2str(struct rpc_clnt *clnt,
971 enum rpc_display_format_t format)
Chuck Leverf425eba2006-08-22 20:06:18 -0400972{
Trond Myklebust2446ab62012-03-01 17:00:56 -0500973 struct rpc_xprt *xprt;
974
975 xprt = rcu_dereference(clnt->cl_xprt);
Chuck Lever7559c7a2006-12-05 16:35:37 -0500976
977 if (xprt->address_strings[format] != NULL)
978 return xprt->address_strings[format];
979 else
980 return "unprintable";
Chuck Leverf425eba2006-08-22 20:06:18 -0400981}
Chuck Leverb86acd52006-08-22 20:06:22 -0400982EXPORT_SYMBOL_GPL(rpc_peeraddr2str);
Chuck Leverf425eba2006-08-22 20:06:18 -0400983
Chuck Lever2e738fd2012-03-01 17:01:14 -0500984static const struct sockaddr_in rpc_inaddr_loopback = {
985 .sin_family = AF_INET,
986 .sin_addr.s_addr = htonl(INADDR_ANY),
987};
988
989static const struct sockaddr_in6 rpc_in6addr_loopback = {
990 .sin6_family = AF_INET6,
991 .sin6_addr = IN6ADDR_ANY_INIT,
992};
993
994/*
995 * Try a getsockname() on a connected datagram socket. Using a
996 * connected datagram socket prevents leaving a socket in TIME_WAIT.
997 * This conserves the ephemeral port number space.
998 *
999 * Returns zero and fills in "buf" if successful; otherwise, a
1000 * negative errno is returned.
1001 */
1002static int rpc_sockname(struct net *net, struct sockaddr *sap, size_t salen,
1003 struct sockaddr *buf, int buflen)
1004{
1005 struct socket *sock;
1006 int err;
1007
1008 err = __sock_create(net, sap->sa_family,
1009 SOCK_DGRAM, IPPROTO_UDP, &sock, 1);
1010 if (err < 0) {
1011 dprintk("RPC: can't create UDP socket (%d)\n", err);
1012 goto out;
1013 }
1014
1015 switch (sap->sa_family) {
1016 case AF_INET:
1017 err = kernel_bind(sock,
1018 (struct sockaddr *)&rpc_inaddr_loopback,
1019 sizeof(rpc_inaddr_loopback));
1020 break;
1021 case AF_INET6:
1022 err = kernel_bind(sock,
1023 (struct sockaddr *)&rpc_in6addr_loopback,
1024 sizeof(rpc_in6addr_loopback));
1025 break;
1026 default:
1027 err = -EAFNOSUPPORT;
1028 goto out;
1029 }
1030 if (err < 0) {
1031 dprintk("RPC: can't bind UDP socket (%d)\n", err);
1032 goto out_release;
1033 }
1034
1035 err = kernel_connect(sock, sap, salen, 0);
1036 if (err < 0) {
1037 dprintk("RPC: can't connect UDP socket (%d)\n", err);
1038 goto out_release;
1039 }
1040
1041 err = kernel_getsockname(sock, buf, &buflen);
1042 if (err < 0) {
1043 dprintk("RPC: getsockname failed (%d)\n", err);
1044 goto out_release;
1045 }
1046
1047 err = 0;
1048 if (buf->sa_family == AF_INET6) {
1049 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)buf;
1050 sin6->sin6_scope_id = 0;
1051 }
1052 dprintk("RPC: %s succeeded\n", __func__);
1053
1054out_release:
1055 sock_release(sock);
1056out:
1057 return err;
1058}
1059
1060/*
1061 * Scraping a connected socket failed, so we don't have a useable
1062 * local address. Fallback: generate an address that will prevent
1063 * the server from calling us back.
1064 *
1065 * Returns zero and fills in "buf" if successful; otherwise, a
1066 * negative errno is returned.
1067 */
1068static int rpc_anyaddr(int family, struct sockaddr *buf, size_t buflen)
1069{
1070 switch (family) {
1071 case AF_INET:
1072 if (buflen < sizeof(rpc_inaddr_loopback))
1073 return -EINVAL;
1074 memcpy(buf, &rpc_inaddr_loopback,
1075 sizeof(rpc_inaddr_loopback));
1076 break;
1077 case AF_INET6:
1078 if (buflen < sizeof(rpc_in6addr_loopback))
1079 return -EINVAL;
1080 memcpy(buf, &rpc_in6addr_loopback,
1081 sizeof(rpc_in6addr_loopback));
1082 default:
1083 dprintk("RPC: %s: address family not supported\n",
1084 __func__);
1085 return -EAFNOSUPPORT;
1086 }
1087 dprintk("RPC: %s: succeeded\n", __func__);
1088 return 0;
1089}
1090
1091/**
1092 * rpc_localaddr - discover local endpoint address for an RPC client
1093 * @clnt: RPC client structure
1094 * @buf: target buffer
1095 * @buflen: size of target buffer, in bytes
1096 *
1097 * Returns zero and fills in "buf" and "buflen" if successful;
1098 * otherwise, a negative errno is returned.
1099 *
1100 * This works even if the underlying transport is not currently connected,
1101 * or if the upper layer never previously provided a source address.
1102 *
1103 * The result of this function call is transient: multiple calls in
1104 * succession may give different results, depending on how local
1105 * networking configuration changes over time.
1106 */
1107int rpc_localaddr(struct rpc_clnt *clnt, struct sockaddr *buf, size_t buflen)
1108{
1109 struct sockaddr_storage address;
1110 struct sockaddr *sap = (struct sockaddr *)&address;
1111 struct rpc_xprt *xprt;
1112 struct net *net;
1113 size_t salen;
1114 int err;
1115
1116 rcu_read_lock();
1117 xprt = rcu_dereference(clnt->cl_xprt);
1118 salen = xprt->addrlen;
1119 memcpy(sap, &xprt->addr, salen);
1120 net = get_net(xprt->xprt_net);
1121 rcu_read_unlock();
1122
1123 rpc_set_port(sap, 0);
1124 err = rpc_sockname(net, sap, salen, buf, buflen);
1125 put_net(net);
1126 if (err != 0)
1127 /* Couldn't discover local address, return ANYADDR */
1128 return rpc_anyaddr(sap->sa_family, buf, buflen);
1129 return 0;
1130}
1131EXPORT_SYMBOL_GPL(rpc_localaddr);
1132
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133void
1134rpc_setbufsize(struct rpc_clnt *clnt, unsigned int sndsize, unsigned int rcvsize)
1135{
Trond Myklebust2446ab62012-03-01 17:00:56 -05001136 struct rpc_xprt *xprt;
1137
1138 rcu_read_lock();
1139 xprt = rcu_dereference(clnt->cl_xprt);
Chuck Lever470056c2005-08-25 16:25:56 -07001140 if (xprt->ops->set_buffer_size)
1141 xprt->ops->set_buffer_size(xprt, sndsize, rcvsize);
Trond Myklebust2446ab62012-03-01 17:00:56 -05001142 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143}
Trond Myklebuste8914c62007-07-14 15:39:59 -04001144EXPORT_SYMBOL_GPL(rpc_setbufsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
Trond Myklebust2446ab62012-03-01 17:00:56 -05001146/**
1147 * rpc_protocol - Get transport protocol number for an RPC client
1148 * @clnt: RPC client to query
1149 *
1150 */
1151int rpc_protocol(struct rpc_clnt *clnt)
1152{
1153 int protocol;
1154
1155 rcu_read_lock();
1156 protocol = rcu_dereference(clnt->cl_xprt)->prot;
1157 rcu_read_unlock();
1158 return protocol;
1159}
1160EXPORT_SYMBOL_GPL(rpc_protocol);
1161
1162/**
1163 * rpc_net_ns - Get the network namespace for this RPC client
1164 * @clnt: RPC client to query
1165 *
1166 */
1167struct net *rpc_net_ns(struct rpc_clnt *clnt)
1168{
1169 struct net *ret;
1170
1171 rcu_read_lock();
1172 ret = rcu_dereference(clnt->cl_xprt)->xprt_net;
1173 rcu_read_unlock();
1174 return ret;
1175}
1176EXPORT_SYMBOL_GPL(rpc_net_ns);
1177
1178/**
1179 * rpc_max_payload - Get maximum payload size for a transport, in bytes
1180 * @clnt: RPC client to query
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 *
1182 * For stream transports, this is one RPC record fragment (see RFC
1183 * 1831), as we don't support multi-record requests yet. For datagram
1184 * transports, this is the size of an IP packet minus the IP, UDP, and
1185 * RPC header sizes.
1186 */
1187size_t rpc_max_payload(struct rpc_clnt *clnt)
1188{
Trond Myklebust2446ab62012-03-01 17:00:56 -05001189 size_t ret;
1190
1191 rcu_read_lock();
1192 ret = rcu_dereference(clnt->cl_xprt)->max_payload;
1193 rcu_read_unlock();
1194 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195}
Chuck Leverb86acd52006-08-22 20:06:22 -04001196EXPORT_SYMBOL_GPL(rpc_max_payload);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
Chuck Lever35f5a422006-01-03 09:55:50 +01001198/**
Trond Myklebust512e4b22013-03-02 15:54:11 -08001199 * rpc_get_timeout - Get timeout for transport in units of HZ
Weston Andros Adamsonedddbb12013-02-28 20:30:09 -05001200 * @clnt: RPC client to query
1201 */
1202unsigned long rpc_get_timeout(struct rpc_clnt *clnt)
1203{
1204 unsigned long ret;
1205
1206 rcu_read_lock();
1207 ret = rcu_dereference(clnt->cl_xprt)->timeout->to_initval;
1208 rcu_read_unlock();
1209 return ret;
1210}
1211EXPORT_SYMBOL_GPL(rpc_get_timeout);
1212
1213/**
Chuck Lever35f5a422006-01-03 09:55:50 +01001214 * rpc_force_rebind - force transport to check that remote port is unchanged
1215 * @clnt: client to rebind
1216 *
1217 */
1218void rpc_force_rebind(struct rpc_clnt *clnt)
1219{
Trond Myklebust2446ab62012-03-01 17:00:56 -05001220 if (clnt->cl_autobind) {
1221 rcu_read_lock();
1222 xprt_clear_bound(rcu_dereference(clnt->cl_xprt));
1223 rcu_read_unlock();
1224 }
Chuck Lever35f5a422006-01-03 09:55:50 +01001225}
Chuck Leverb86acd52006-08-22 20:06:22 -04001226EXPORT_SYMBOL_GPL(rpc_force_rebind);
Chuck Lever35f5a422006-01-03 09:55:50 +01001227
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228/*
Andy Adamsonaae20062009-04-01 09:22:40 -04001229 * Restart an (async) RPC call from the call_prepare state.
1230 * Usually called from within the exit handler.
1231 */
Trond Myklebustf1f88fc2010-07-31 14:29:07 -04001232int
Andy Adamsonaae20062009-04-01 09:22:40 -04001233rpc_restart_call_prepare(struct rpc_task *task)
1234{
1235 if (RPC_ASSASSINATED(task))
Trond Myklebustf1f88fc2010-07-31 14:29:07 -04001236 return 0;
Trond Myklebustd00c5d42011-10-19 12:17:29 -07001237 task->tk_action = call_start;
1238 if (task->tk_ops->rpc_call_prepare != NULL)
1239 task->tk_action = rpc_prepare_task;
Trond Myklebustf1f88fc2010-07-31 14:29:07 -04001240 return 1;
Andy Adamsonaae20062009-04-01 09:22:40 -04001241}
1242EXPORT_SYMBOL_GPL(rpc_restart_call_prepare);
1243
1244/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 * Restart an (async) RPC call. Usually called from within the
1246 * exit handler.
1247 */
Trond Myklebustf1f88fc2010-07-31 14:29:07 -04001248int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249rpc_restart_call(struct rpc_task *task)
1250{
1251 if (RPC_ASSASSINATED(task))
Trond Myklebustf1f88fc2010-07-31 14:29:07 -04001252 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 task->tk_action = call_start;
Trond Myklebustf1f88fc2010-07-31 14:29:07 -04001254 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255}
Trond Myklebuste8914c62007-07-14 15:39:59 -04001256EXPORT_SYMBOL_GPL(rpc_restart_call);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
Chuck Lever3748f1e2008-05-21 17:09:12 -04001258#ifdef RPC_DEBUG
1259static const char *rpc_proc_name(const struct rpc_task *task)
1260{
1261 const struct rpc_procinfo *proc = task->tk_msg.rpc_proc;
1262
1263 if (proc) {
1264 if (proc->p_name)
1265 return proc->p_name;
1266 else
1267 return "NULL";
1268 } else
1269 return "no proc";
1270}
1271#endif
1272
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273/*
1274 * 0. Initial state
1275 *
1276 * Other FSM states can be visited zero or more times, but
1277 * this state is visited exactly once for each RPC.
1278 */
1279static void
1280call_start(struct rpc_task *task)
1281{
1282 struct rpc_clnt *clnt = task->tk_client;
1283
Chuck Lever3748f1e2008-05-21 17:09:12 -04001284 dprintk("RPC: %5u call_start %s%d proc %s (%s)\n", task->tk_pid,
Trond Myklebust55909f22013-08-23 11:48:15 -04001285 clnt->cl_program->name, clnt->cl_vers,
Chuck Lever3748f1e2008-05-21 17:09:12 -04001286 rpc_proc_name(task),
Chuck Lever46121cf2007-01-31 12:14:08 -05001287 (RPC_IS_ASYNC(task) ? "async" : "sync"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
1289 /* Increment call count */
1290 task->tk_msg.rpc_proc->p_count++;
1291 clnt->cl_stats->rpccnt++;
1292 task->tk_action = call_reserve;
1293}
1294
1295/*
1296 * 1. Reserve an RPC call slot
1297 */
1298static void
1299call_reserve(struct rpc_task *task)
1300{
Chuck Lever46121cf2007-01-31 12:14:08 -05001301 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 task->tk_status = 0;
1304 task->tk_action = call_reserveresult;
1305 xprt_reserve(task);
1306}
1307
Trond Myklebustba60eb22013-04-14 10:49:37 -04001308static void call_retry_reserve(struct rpc_task *task);
1309
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310/*
1311 * 1b. Grok the result of xprt_reserve()
1312 */
1313static void
1314call_reserveresult(struct rpc_task *task)
1315{
1316 int status = task->tk_status;
1317
Chuck Lever46121cf2007-01-31 12:14:08 -05001318 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
1320 /*
1321 * After a call to xprt_reserve(), we must have either
1322 * a request slot or else an error status.
1323 */
1324 task->tk_status = 0;
1325 if (status >= 0) {
1326 if (task->tk_rqstp) {
J. Bruce Fieldsf2d47d02010-09-12 19:55:25 -04001327 task->tk_action = call_refresh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 return;
1329 }
1330
1331 printk(KERN_ERR "%s: status=%d, but no request slot, exiting\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08001332 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 rpc_exit(task, -EIO);
1334 return;
1335 }
1336
1337 /*
1338 * Even though there was an error, we may have acquired
1339 * a request slot somehow. Make sure not to leak it.
1340 */
1341 if (task->tk_rqstp) {
1342 printk(KERN_ERR "%s: status=%d, request allocated anyway\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08001343 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 xprt_release(task);
1345 }
1346
1347 switch (status) {
Trond Myklebust1afeaf52012-05-19 12:12:53 -04001348 case -ENOMEM:
1349 rpc_delay(task, HZ >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 case -EAGAIN: /* woken up; retry */
Trond Myklebustba60eb22013-04-14 10:49:37 -04001351 task->tk_action = call_retry_reserve;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 return;
1353 case -EIO: /* probably a shutdown */
1354 break;
1355 default:
1356 printk(KERN_ERR "%s: unrecognized error %d, exiting\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08001357 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 break;
1359 }
1360 rpc_exit(task, status);
1361}
1362
1363/*
Trond Myklebustba60eb22013-04-14 10:49:37 -04001364 * 1c. Retry reserving an RPC call slot
1365 */
1366static void
1367call_retry_reserve(struct rpc_task *task)
1368{
1369 dprint_status(task);
1370
1371 task->tk_status = 0;
1372 task->tk_action = call_reserveresult;
1373 xprt_retry_reserve(task);
1374}
1375
1376/*
J. Bruce Fields55576242010-09-12 19:55:25 -04001377 * 2. Bind and/or refresh the credentials
1378 */
1379static void
1380call_refresh(struct rpc_task *task)
1381{
1382 dprint_status(task);
1383
1384 task->tk_action = call_refreshresult;
1385 task->tk_status = 0;
1386 task->tk_client->cl_stats->rpcauthrefresh++;
1387 rpcauth_refreshcred(task);
1388}
1389
1390/*
1391 * 2a. Process the results of a credential refresh
1392 */
1393static void
1394call_refreshresult(struct rpc_task *task)
1395{
1396 int status = task->tk_status;
1397
1398 dprint_status(task);
1399
1400 task->tk_status = 0;
Trond Myklebust5fc43972010-11-20 11:13:31 -05001401 task->tk_action = call_refresh;
J. Bruce Fields55576242010-09-12 19:55:25 -04001402 switch (status) {
Trond Myklebust5fc43972010-11-20 11:13:31 -05001403 case 0:
1404 if (rpcauth_uptodatecred(task))
1405 task->tk_action = call_allocate;
J. Bruce Fields55576242010-09-12 19:55:25 -04001406 return;
1407 case -ETIMEDOUT:
1408 rpc_delay(task, 3*HZ);
Andy Adamsoneb96d5c2012-11-27 10:34:19 -05001409 case -EKEYEXPIRED:
Trond Myklebust5fc43972010-11-20 11:13:31 -05001410 case -EAGAIN:
1411 status = -EACCES;
1412 if (!task->tk_cred_retry)
1413 break;
1414 task->tk_cred_retry--;
1415 dprintk("RPC: %5u %s: retry refresh creds\n",
1416 task->tk_pid, __func__);
1417 return;
J. Bruce Fields55576242010-09-12 19:55:25 -04001418 }
Trond Myklebust5fc43972010-11-20 11:13:31 -05001419 dprintk("RPC: %5u %s: refresh creds failed with error %d\n",
1420 task->tk_pid, __func__, status);
1421 rpc_exit(task, status);
J. Bruce Fields55576242010-09-12 19:55:25 -04001422}
1423
1424/*
1425 * 2b. Allocate the buffer. For details, see sched.c:rpc_malloc.
Chuck Lever02107142006-01-03 09:55:49 +01001426 * (Note: buffer memory is freed in xprt_release).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 */
1428static void
1429call_allocate(struct rpc_task *task)
1430{
J. Bruce Fieldsf2d47d02010-09-12 19:55:25 -04001431 unsigned int slack = task->tk_rqstp->rq_cred->cr_auth->au_cslack;
Chuck Lever02107142006-01-03 09:55:49 +01001432 struct rpc_rqst *req = task->tk_rqstp;
Trond Myklebusta4f08352013-01-08 09:10:21 -05001433 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Lever2bea90d2007-03-29 16:47:53 -04001434 struct rpc_procinfo *proc = task->tk_msg.rpc_proc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435
Chuck Lever46121cf2007-01-31 12:14:08 -05001436 dprint_status(task);
1437
Chuck Lever2bea90d2007-03-29 16:47:53 -04001438 task->tk_status = 0;
J. Bruce Fieldsf2d47d02010-09-12 19:55:25 -04001439 task->tk_action = call_bind;
Chuck Lever2bea90d2007-03-29 16:47:53 -04001440
Chuck Lever02107142006-01-03 09:55:49 +01001441 if (req->rq_buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 return;
1443
Chuck Lever2bea90d2007-03-29 16:47:53 -04001444 if (proc->p_proc != 0) {
1445 BUG_ON(proc->p_arglen == 0);
1446 if (proc->p_decode != NULL)
1447 BUG_ON(proc->p_replen == 0);
1448 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
Chuck Lever2bea90d2007-03-29 16:47:53 -04001450 /*
1451 * Calculate the size (in quads) of the RPC call
1452 * and reply headers, and convert both values
1453 * to byte sizes.
1454 */
1455 req->rq_callsize = RPC_CALLHDRSIZE + (slack << 1) + proc->p_arglen;
1456 req->rq_callsize <<= 2;
1457 req->rq_rcvsize = RPC_REPHDRSIZE + slack + proc->p_replen;
1458 req->rq_rcvsize <<= 2;
1459
Chuck Leverc5a4dd82007-03-29 16:47:58 -04001460 req->rq_buffer = xprt->ops->buf_alloc(task,
1461 req->rq_callsize + req->rq_rcvsize);
Chuck Lever2bea90d2007-03-29 16:47:53 -04001462 if (req->rq_buffer != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 return;
Chuck Lever46121cf2007-01-31 12:14:08 -05001464
1465 dprintk("RPC: %5u rpc_buffer allocation failed\n", task->tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466
Trond Myklebust5afa9132011-06-17 10:14:59 -04001467 if (RPC_IS_ASYNC(task) || !fatal_signal_pending(current)) {
Trond Myklebustb6e9c712007-10-01 12:06:44 -04001468 task->tk_action = call_allocate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 rpc_delay(task, HZ>>4);
1470 return;
1471 }
1472
1473 rpc_exit(task, -ERESTARTSYS);
1474}
1475
Trond Myklebust940e3312005-11-09 21:45:24 -05001476static inline int
1477rpc_task_need_encode(struct rpc_task *task)
1478{
1479 return task->tk_rqstp->rq_snd_buf.len == 0;
1480}
1481
1482static inline void
1483rpc_task_force_reencode(struct rpc_task *task)
1484{
1485 task->tk_rqstp->rq_snd_buf.len = 0;
Trond Myklebust2574cc92009-08-28 11:12:12 -04001486 task->tk_rqstp->rq_bytes_sent = 0;
Trond Myklebust940e3312005-11-09 21:45:24 -05001487}
1488
Chuck Lever2bea90d2007-03-29 16:47:53 -04001489static inline void
1490rpc_xdr_buf_init(struct xdr_buf *buf, void *start, size_t len)
1491{
1492 buf->head[0].iov_base = start;
1493 buf->head[0].iov_len = len;
1494 buf->tail[0].iov_len = 0;
1495 buf->page_len = 0;
\"Talpey, Thomas\4f22ccc2007-09-10 13:44:58 -04001496 buf->flags = 0;
Chuck Lever2bea90d2007-03-29 16:47:53 -04001497 buf->len = 0;
1498 buf->buflen = len;
1499}
1500
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501/*
1502 * 3. Encode arguments of an RPC call
1503 */
1504static void
Chuck Leverb0e1c572008-05-21 17:09:19 -04001505rpc_xdr_encode(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 struct rpc_rqst *req = task->tk_rqstp;
Chuck Lever9f06c712010-12-14 14:59:18 +00001508 kxdreproc_t encode;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001509 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
Chuck Lever46121cf2007-01-31 12:14:08 -05001511 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
Chuck Lever2bea90d2007-03-29 16:47:53 -04001513 rpc_xdr_buf_init(&req->rq_snd_buf,
1514 req->rq_buffer,
1515 req->rq_callsize);
1516 rpc_xdr_buf_init(&req->rq_rcv_buf,
1517 (char *)req->rq_buffer + req->rq_callsize,
1518 req->rq_rcvsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519
Chuck Leverb0e1c572008-05-21 17:09:19 -04001520 p = rpc_encode_header(task);
1521 if (p == NULL) {
1522 printk(KERN_INFO "RPC: couldn't encode RPC header, exit EIO\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 rpc_exit(task, -EIO);
1524 return;
1525 }
Chuck Leverb0e1c572008-05-21 17:09:19 -04001526
1527 encode = task->tk_msg.rpc_proc->p_encode;
J. Bruce Fieldsf3680312005-10-13 16:54:48 -04001528 if (encode == NULL)
1529 return;
1530
1531 task->tk_status = rpcauth_wrap_req(task, encode, req, p,
1532 task->tk_msg.rpc_argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533}
1534
1535/*
1536 * 4. Get the server port number if not yet set
1537 */
1538static void
1539call_bind(struct rpc_task *task)
1540{
Trond Myklebustad2368d2013-01-08 10:08:33 -05001541 struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542
Chuck Lever46121cf2007-01-31 12:14:08 -05001543 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544
Chuck Leverda351872005-08-11 16:25:11 -04001545 task->tk_action = call_connect;
Chuck Leverec739ef2006-08-22 20:06:15 -04001546 if (!xprt_bound(xprt)) {
Chuck Leverda351872005-08-11 16:25:11 -04001547 task->tk_action = call_bind_status;
Chuck Leverec739ef2006-08-22 20:06:15 -04001548 task->tk_timeout = xprt->bind_timeout;
Chuck Leverbbf7c1d2006-08-22 20:06:16 -04001549 xprt->ops->rpcbind(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 }
1551}
1552
1553/*
Chuck Leverda351872005-08-11 16:25:11 -04001554 * 4a. Sort out bind result
1555 */
1556static void
1557call_bind_status(struct rpc_task *task)
1558{
Chuck Lever906462a2007-09-11 18:00:47 -04001559 int status = -EIO;
Chuck Leverda351872005-08-11 16:25:11 -04001560
1561 if (task->tk_status >= 0) {
Chuck Lever46121cf2007-01-31 12:14:08 -05001562 dprint_status(task);
Chuck Leverda351872005-08-11 16:25:11 -04001563 task->tk_status = 0;
1564 task->tk_action = call_connect;
1565 return;
1566 }
1567
Steve Dickson5753cba2012-02-06 10:08:08 -05001568 trace_rpc_bind_status(task);
Chuck Leverda351872005-08-11 16:25:11 -04001569 switch (task->tk_status) {
Trond Myklebust381ba742008-07-07 12:18:53 -04001570 case -ENOMEM:
1571 dprintk("RPC: %5u rpcbind out of memory\n", task->tk_pid);
1572 rpc_delay(task, HZ >> 2);
Chuck Lever2429cbf2007-09-11 18:00:41 -04001573 goto retry_timeout;
Chuck Leverda351872005-08-11 16:25:11 -04001574 case -EACCES:
Chuck Lever46121cf2007-01-31 12:14:08 -05001575 dprintk("RPC: %5u remote rpcbind: RPC program/version "
1576 "unavailable\n", task->tk_pid);
Chuck Leverb79dc8c2007-09-11 18:00:52 -04001577 /* fail immediately if this is an RPC ping */
1578 if (task->tk_msg.rpc_proc->p_proc == 0) {
1579 status = -EOPNOTSUPP;
1580 break;
1581 }
Trond Myklebust0b760112011-05-31 15:15:34 -04001582 if (task->tk_rebind_retry == 0)
1583 break;
1584 task->tk_rebind_retry--;
Chuck Leverea635a52005-10-06 23:12:58 -04001585 rpc_delay(task, 3*HZ);
Trond Myklebustda458282006-08-31 15:44:52 -04001586 goto retry_timeout;
Chuck Leverda351872005-08-11 16:25:11 -04001587 case -ETIMEDOUT:
Chuck Lever46121cf2007-01-31 12:14:08 -05001588 dprintk("RPC: %5u rpcbind request timed out\n",
Chuck Leverda351872005-08-11 16:25:11 -04001589 task->tk_pid);
Trond Myklebustda458282006-08-31 15:44:52 -04001590 goto retry_timeout;
Chuck Leverda351872005-08-11 16:25:11 -04001591 case -EPFNOSUPPORT:
Chuck Lever906462a2007-09-11 18:00:47 -04001592 /* server doesn't support any rpcbind version we know of */
Chuck Lever012da152009-12-03 15:58:56 -05001593 dprintk("RPC: %5u unrecognized remote rpcbind service\n",
Chuck Leverda351872005-08-11 16:25:11 -04001594 task->tk_pid);
1595 break;
1596 case -EPROTONOSUPPORT:
Chuck Lever00a6e7b2007-03-29 16:48:33 -04001597 dprintk("RPC: %5u remote rpcbind version unavailable, retrying\n",
Chuck Leverda351872005-08-11 16:25:11 -04001598 task->tk_pid);
Chuck Lever00a6e7b2007-03-29 16:48:33 -04001599 task->tk_status = 0;
1600 task->tk_action = call_bind;
1601 return;
Chuck Lever012da152009-12-03 15:58:56 -05001602 case -ECONNREFUSED: /* connection problems */
1603 case -ECONNRESET:
1604 case -ENOTCONN:
1605 case -EHOSTDOWN:
1606 case -EHOSTUNREACH:
1607 case -ENETUNREACH:
1608 case -EPIPE:
1609 dprintk("RPC: %5u remote rpcbind unreachable: %d\n",
1610 task->tk_pid, task->tk_status);
1611 if (!RPC_IS_SOFTCONN(task)) {
1612 rpc_delay(task, 5*HZ);
1613 goto retry_timeout;
1614 }
1615 status = task->tk_status;
1616 break;
Chuck Leverda351872005-08-11 16:25:11 -04001617 default:
Chuck Lever46121cf2007-01-31 12:14:08 -05001618 dprintk("RPC: %5u unrecognized rpcbind error (%d)\n",
Chuck Leverda351872005-08-11 16:25:11 -04001619 task->tk_pid, -task->tk_status);
Chuck Leverda351872005-08-11 16:25:11 -04001620 }
1621
1622 rpc_exit(task, status);
1623 return;
1624
Trond Myklebustda458282006-08-31 15:44:52 -04001625retry_timeout:
1626 task->tk_action = call_timeout;
Chuck Leverda351872005-08-11 16:25:11 -04001627}
1628
1629/*
1630 * 4b. Connect to the RPC server
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 */
1632static void
1633call_connect(struct rpc_task *task)
1634{
Trond Myklebustad2368d2013-01-08 10:08:33 -05001635 struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636
Chuck Lever46121cf2007-01-31 12:14:08 -05001637 dprintk("RPC: %5u call_connect xprt %p %s connected\n",
Chuck Leverda351872005-08-11 16:25:11 -04001638 task->tk_pid, xprt,
1639 (xprt_connected(xprt) ? "is" : "is not"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640
Chuck Leverda351872005-08-11 16:25:11 -04001641 task->tk_action = call_transmit;
1642 if (!xprt_connected(xprt)) {
1643 task->tk_action = call_connect_status;
1644 if (task->tk_status < 0)
1645 return;
1646 xprt_connect(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648}
1649
1650/*
Chuck Leverda351872005-08-11 16:25:11 -04001651 * 4c. Sort out connect result
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 */
1653static void
1654call_connect_status(struct rpc_task *task)
1655{
1656 struct rpc_clnt *clnt = task->tk_client;
1657 int status = task->tk_status;
1658
Chuck Lever46121cf2007-01-31 12:14:08 -05001659 dprint_status(task);
Chuck Leverda351872005-08-11 16:25:11 -04001660
Steve Dickson5753cba2012-02-06 10:08:08 -05001661 trace_rpc_connect_status(task, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 switch (status) {
Trond Myklebustda458282006-08-31 15:44:52 -04001663 /* if soft mounted, test if we've timed out */
1664 case -ETIMEDOUT:
1665 task->tk_action = call_timeout;
Trond Myklebust3ed5e2a2013-03-04 17:29:33 -05001666 return;
1667 case -ECONNREFUSED:
1668 case -ECONNRESET:
1669 case -ENETUNREACH:
1670 if (RPC_IS_SOFTCONN(task))
1671 break;
1672 /* retry with existing socket, after a delay */
1673 case 0:
1674 case -EAGAIN:
1675 task->tk_status = 0;
1676 clnt->cl_stats->netreconn++;
1677 task->tk_action = call_transmit;
1678 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 }
Trond Myklebust3ed5e2a2013-03-04 17:29:33 -05001680 rpc_exit(task, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681}
1682
1683/*
1684 * 5. Transmit the RPC request, and wait for reply
1685 */
1686static void
1687call_transmit(struct rpc_task *task)
1688{
Chuck Lever46121cf2007-01-31 12:14:08 -05001689 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690
1691 task->tk_action = call_status;
1692 if (task->tk_status < 0)
1693 return;
1694 task->tk_status = xprt_prepare_transmit(task);
1695 if (task->tk_status != 0)
1696 return;
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04001697 task->tk_action = call_transmit_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 /* Encode here so that rpcsec_gss can use correct sequence number. */
Trond Myklebust940e3312005-11-09 21:45:24 -05001699 if (rpc_task_need_encode(task)) {
Chuck Leverb0e1c572008-05-21 17:09:19 -04001700 rpc_xdr_encode(task);
Trond Myklebust5e5ce5b2005-10-18 14:20:11 -07001701 /* Did the encode result in an error condition? */
Trond Myklebust8b39f2b2008-05-14 19:48:25 -07001702 if (task->tk_status != 0) {
1703 /* Was the error nonfatal? */
1704 if (task->tk_status == -EAGAIN)
1705 rpc_delay(task, HZ >> 4);
1706 else
1707 rpc_exit(task, task->tk_status);
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04001708 return;
Trond Myklebust8b39f2b2008-05-14 19:48:25 -07001709 }
Trond Myklebust5e5ce5b2005-10-18 14:20:11 -07001710 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 xprt_transmit(task);
1712 if (task->tk_status < 0)
1713 return;
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04001714 /*
1715 * On success, ensure that we call xprt_end_transmit() before sleeping
1716 * in order to allow access to the socket to other RPC requests.
1717 */
1718 call_transmit_status(task);
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001719 if (rpc_reply_expected(task))
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04001720 return;
1721 task->tk_action = rpc_exit_task;
Trond Myklebusta4f08352013-01-08 09:10:21 -05001722 rpc_wake_up_queued_task(&task->tk_rqstp->rq_xprt->pending, task);
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04001723}
1724
1725/*
1726 * 5a. Handle cleanup after a transmission
1727 */
1728static void
1729call_transmit_status(struct rpc_task *task)
1730{
1731 task->tk_action = call_status;
Chuck Lever206a1342009-12-03 15:58:56 -05001732
1733 /*
1734 * Common case: success. Force the compiler to put this
1735 * test first.
1736 */
1737 if (task->tk_status == 0) {
1738 xprt_end_transmit(task);
1739 rpc_task_force_reencode(task);
1740 return;
1741 }
1742
Trond Myklebust15f081c2009-03-11 14:37:57 -04001743 switch (task->tk_status) {
1744 case -EAGAIN:
1745 break;
1746 default:
Chuck Lever206a1342009-12-03 15:58:56 -05001747 dprint_status(task);
Trond Myklebust15f081c2009-03-11 14:37:57 -04001748 xprt_end_transmit(task);
Chuck Lever09a21c42009-12-03 15:58:56 -05001749 rpc_task_force_reencode(task);
1750 break;
Trond Myklebust15f081c2009-03-11 14:37:57 -04001751 /*
1752 * Special cases: if we've been waiting on the
1753 * socket's write_space() callback, or if the
1754 * socket just returned a connection error,
1755 * then hold onto the transport lock.
1756 */
1757 case -ECONNREFUSED:
Trond Myklebust15f081c2009-03-11 14:37:57 -04001758 case -EHOSTDOWN:
1759 case -EHOSTUNREACH:
1760 case -ENETUNREACH:
Chuck Lever09a21c42009-12-03 15:58:56 -05001761 if (RPC_IS_SOFTCONN(task)) {
1762 xprt_end_transmit(task);
1763 rpc_exit(task, task->tk_status);
1764 break;
1765 }
1766 case -ECONNRESET:
1767 case -ENOTCONN:
Trond Myklebustc8485e42009-03-11 14:37:59 -04001768 case -EPIPE:
Trond Myklebust15f081c2009-03-11 14:37:57 -04001769 rpc_task_force_reencode(task);
1770 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771}
1772
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001773#if defined(CONFIG_SUNRPC_BACKCHANNEL)
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001774/*
1775 * 5b. Send the backchannel RPC reply. On error, drop the reply. In
1776 * addition, disconnect on connectivity errors.
1777 */
1778static void
1779call_bc_transmit(struct rpc_task *task)
1780{
1781 struct rpc_rqst *req = task->tk_rqstp;
1782
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001783 task->tk_status = xprt_prepare_transmit(task);
1784 if (task->tk_status == -EAGAIN) {
1785 /*
1786 * Could not reserve the transport. Try again after the
1787 * transport is released.
1788 */
1789 task->tk_status = 0;
1790 task->tk_action = call_bc_transmit;
1791 return;
1792 }
1793
1794 task->tk_action = rpc_exit_task;
1795 if (task->tk_status < 0) {
1796 printk(KERN_NOTICE "RPC: Could not send backchannel reply "
1797 "error: %d\n", task->tk_status);
1798 return;
1799 }
1800
1801 xprt_transmit(task);
1802 xprt_end_transmit(task);
1803 dprint_status(task);
1804 switch (task->tk_status) {
1805 case 0:
1806 /* Success */
1807 break;
1808 case -EHOSTDOWN:
1809 case -EHOSTUNREACH:
1810 case -ENETUNREACH:
1811 case -ETIMEDOUT:
1812 /*
1813 * Problem reaching the server. Disconnect and let the
1814 * forechannel reestablish the connection. The server will
1815 * have to retransmit the backchannel request and we'll
1816 * reprocess it. Since these ops are idempotent, there's no
1817 * need to cache our reply at this time.
1818 */
1819 printk(KERN_NOTICE "RPC: Could not send backchannel reply "
1820 "error: %d\n", task->tk_status);
Trond Myklebusta4f08352013-01-08 09:10:21 -05001821 xprt_conditional_disconnect(req->rq_xprt,
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001822 req->rq_connect_cookie);
1823 break;
1824 default:
1825 /*
1826 * We were unable to reply and will have to drop the
1827 * request. The server should reconnect and retransmit.
1828 */
Weston Andros Adamson1facf4c2012-10-23 10:43:30 -04001829 WARN_ON_ONCE(task->tk_status == -EAGAIN);
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001830 printk(KERN_NOTICE "RPC: Could not send backchannel reply "
1831 "error: %d\n", task->tk_status);
1832 break;
1833 }
1834 rpc_wake_up_queued_task(&req->rq_xprt->pending, task);
1835}
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001836#endif /* CONFIG_SUNRPC_BACKCHANNEL */
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001837
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838/*
1839 * 6. Sort out the RPC call status
1840 */
1841static void
1842call_status(struct rpc_task *task)
1843{
1844 struct rpc_clnt *clnt = task->tk_client;
1845 struct rpc_rqst *req = task->tk_rqstp;
1846 int status;
1847
Ricardo Labiagadd2b63d2009-04-01 09:23:28 -04001848 if (req->rq_reply_bytes_recvd > 0 && !req->rq_bytes_sent)
1849 task->tk_status = req->rq_reply_bytes_recvd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850
Chuck Lever46121cf2007-01-31 12:14:08 -05001851 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852
1853 status = task->tk_status;
1854 if (status >= 0) {
1855 task->tk_action = call_decode;
1856 return;
1857 }
1858
Steve Dickson5753cba2012-02-06 10:08:08 -05001859 trace_rpc_call_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 task->tk_status = 0;
1861 switch(status) {
Trond Myklebust76303992006-08-30 14:32:49 -04001862 case -EHOSTDOWN:
1863 case -EHOSTUNREACH:
1864 case -ENETUNREACH:
1865 /*
1866 * Delay any retries for 3 seconds, then handle as if it
1867 * were a timeout.
1868 */
1869 rpc_delay(task, 3*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 case -ETIMEDOUT:
1871 task->tk_action = call_timeout;
Trond Myklebust241c39b2007-04-20 16:12:55 -04001872 if (task->tk_client->cl_discrtry)
Trond Myklebusta4f08352013-01-08 09:10:21 -05001873 xprt_conditional_disconnect(req->rq_xprt,
Trond Myklebust7c1d71c2008-04-17 16:52:57 -04001874 req->rq_connect_cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 break;
Trond Myklebustc8485e42009-03-11 14:37:59 -04001876 case -ECONNRESET:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 case -ECONNREFUSED:
Chuck Lever35f5a422006-01-03 09:55:50 +01001878 rpc_force_rebind(clnt);
Trond Myklebustc8485e42009-03-11 14:37:59 -04001879 rpc_delay(task, 3*HZ);
1880 case -EPIPE:
1881 case -ENOTCONN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 task->tk_action = call_bind;
1883 break;
1884 case -EAGAIN:
1885 task->tk_action = call_transmit;
1886 break;
1887 case -EIO:
1888 /* shutdown or soft timeout */
1889 rpc_exit(task, status);
1890 break;
1891 default:
Olga Kornievskaiab6b61522008-06-09 16:51:31 -04001892 if (clnt->cl_chatty)
1893 printk("%s: RPC call returned error %d\n",
Trond Myklebust55909f22013-08-23 11:48:15 -04001894 clnt->cl_program->name, -status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 rpc_exit(task, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 }
1897}
1898
1899/*
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04001900 * 6a. Handle RPC timeout
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 * We do not release the request slot, so we keep using the
1902 * same XID for all retransmits.
1903 */
1904static void
1905call_timeout(struct rpc_task *task)
1906{
1907 struct rpc_clnt *clnt = task->tk_client;
1908
1909 if (xprt_adjust_timeout(task->tk_rqstp) == 0) {
Chuck Lever46121cf2007-01-31 12:14:08 -05001910 dprintk("RPC: %5u call_timeout (minor)\n", task->tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 goto retry;
1912 }
1913
Chuck Lever46121cf2007-01-31 12:14:08 -05001914 dprintk("RPC: %5u call_timeout (major)\n", task->tk_pid);
Chuck Leveref759a22006-03-20 13:44:17 -05001915 task->tk_timeouts++;
1916
Chuck Lever3a28bec2009-12-03 15:58:56 -05001917 if (RPC_IS_SOFTCONN(task)) {
1918 rpc_exit(task, -ETIMEDOUT);
1919 return;
1920 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 if (RPC_IS_SOFT(task)) {
Joe Perchescac5d072012-07-18 11:17:11 -07001922 if (clnt->cl_chatty) {
Trond Myklebust4e0038b2012-03-01 17:01:05 -05001923 rcu_read_lock();
Olga Kornievskaiab6b61522008-06-09 16:51:31 -04001924 printk(KERN_NOTICE "%s: server %s not responding, timed out\n",
Trond Myklebust55909f22013-08-23 11:48:15 -04001925 clnt->cl_program->name,
Trond Myklebust4e0038b2012-03-01 17:01:05 -05001926 rcu_dereference(clnt->cl_xprt)->servername);
1927 rcu_read_unlock();
Joe Perchescac5d072012-07-18 11:17:11 -07001928 }
Trond Myklebust7494d002011-04-24 14:28:45 -04001929 if (task->tk_flags & RPC_TASK_TIMEOUT)
1930 rpc_exit(task, -ETIMEDOUT);
1931 else
1932 rpc_exit(task, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 return;
1934 }
1935
Chuck Leverf518e35a2006-01-03 09:55:52 +01001936 if (!(task->tk_flags & RPC_CALL_MAJORSEEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 task->tk_flags |= RPC_CALL_MAJORSEEN;
Trond Myklebust4e0038b2012-03-01 17:01:05 -05001938 if (clnt->cl_chatty) {
1939 rcu_read_lock();
Olga Kornievskaiab6b61522008-06-09 16:51:31 -04001940 printk(KERN_NOTICE "%s: server %s not responding, still trying\n",
Trond Myklebust55909f22013-08-23 11:48:15 -04001941 clnt->cl_program->name,
Trond Myklebust4e0038b2012-03-01 17:01:05 -05001942 rcu_dereference(clnt->cl_xprt)->servername);
1943 rcu_read_unlock();
1944 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 }
Chuck Lever35f5a422006-01-03 09:55:50 +01001946 rpc_force_rebind(clnt);
Trond Myklebustb48633b2008-04-22 16:47:55 -04001947 /*
1948 * Did our request time out due to an RPCSEC_GSS out-of-sequence
1949 * event? RFC2203 requires the server to drop all such requests.
1950 */
1951 rpcauth_invalcred(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952
1953retry:
1954 clnt->cl_stats->rpcretrans++;
1955 task->tk_action = call_bind;
1956 task->tk_status = 0;
1957}
1958
1959/*
1960 * 7. Decode the RPC reply
1961 */
1962static void
1963call_decode(struct rpc_task *task)
1964{
1965 struct rpc_clnt *clnt = task->tk_client;
1966 struct rpc_rqst *req = task->tk_rqstp;
Chuck Leverbf269552010-12-14 14:59:29 +00001967 kxdrdproc_t decode = task->tk_msg.rpc_proc->p_decode;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001968 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969
Vasily Averin726fd6a2011-06-01 16:23:59 +04001970 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971
Chuck Leverf518e35a2006-01-03 09:55:52 +01001972 if (task->tk_flags & RPC_CALL_MAJORSEEN) {
Trond Myklebust4e0038b2012-03-01 17:01:05 -05001973 if (clnt->cl_chatty) {
1974 rcu_read_lock();
Olga Kornievskaiab6b61522008-06-09 16:51:31 -04001975 printk(KERN_NOTICE "%s: server %s OK\n",
Trond Myklebust55909f22013-08-23 11:48:15 -04001976 clnt->cl_program->name,
Trond Myklebust4e0038b2012-03-01 17:01:05 -05001977 rcu_dereference(clnt->cl_xprt)->servername);
1978 rcu_read_unlock();
1979 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 task->tk_flags &= ~RPC_CALL_MAJORSEEN;
1981 }
1982
Trond Myklebust43ac3f22006-03-20 13:44:51 -05001983 /*
1984 * Ensure that we see all writes made by xprt_complete_rqst()
Ricardo Labiagadd2b63d2009-04-01 09:23:28 -04001985 * before it changed req->rq_reply_bytes_recvd.
Trond Myklebust43ac3f22006-03-20 13:44:51 -05001986 */
1987 smp_rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 req->rq_rcv_buf.len = req->rq_private_buf.len;
1989
1990 /* Check that the softirq receive buffer is valid */
1991 WARN_ON(memcmp(&req->rq_rcv_buf, &req->rq_private_buf,
1992 sizeof(req->rq_rcv_buf)) != 0);
1993
Trond Myklebust1e799b62008-03-21 16:19:41 -04001994 if (req->rq_rcv_buf.len < 12) {
1995 if (!RPC_IS_SOFT(task)) {
1996 task->tk_action = call_bind;
1997 clnt->cl_stats->rpcretrans++;
1998 goto out_retry;
1999 }
2000 dprintk("RPC: %s: too small RPC reply size (%d bytes)\n",
Trond Myklebust55909f22013-08-23 11:48:15 -04002001 clnt->cl_program->name, task->tk_status);
Trond Myklebust1e799b62008-03-21 16:19:41 -04002002 task->tk_action = call_timeout;
2003 goto out_retry;
2004 }
2005
Chuck Leverb0e1c572008-05-21 17:09:19 -04002006 p = rpc_verify_header(task);
Trond Myklebustabbcf282006-01-03 09:55:03 +01002007 if (IS_ERR(p)) {
2008 if (p == ERR_PTR(-EAGAIN))
2009 goto out_retry;
2010 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 }
2012
Trond Myklebustabbcf282006-01-03 09:55:03 +01002013 task->tk_action = rpc_exit_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014
Trond Myklebust6d5fcb52006-10-18 16:01:06 -04002015 if (decode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 task->tk_status = rpcauth_unwrap_resp(task, decode, req, p,
2017 task->tk_msg.rpc_resp);
Trond Myklebust6d5fcb52006-10-18 16:01:06 -04002018 }
Chuck Lever46121cf2007-01-31 12:14:08 -05002019 dprintk("RPC: %5u call_decode result %d\n", task->tk_pid,
2020 task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 return;
2022out_retry:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 task->tk_status = 0;
Chuck Leverb0e1c572008-05-21 17:09:19 -04002024 /* Note: rpc_verify_header() may have freed the RPC slot */
Trond Myklebust24b74bf2008-04-19 13:15:47 -04002025 if (task->tk_rqstp == req) {
Ricardo Labiagadd2b63d2009-04-01 09:23:28 -04002026 req->rq_reply_bytes_recvd = req->rq_rcv_buf.len = 0;
Trond Myklebust24b74bf2008-04-19 13:15:47 -04002027 if (task->tk_client->cl_discrtry)
Trond Myklebusta4f08352013-01-08 09:10:21 -05002028 xprt_conditional_disconnect(req->rq_xprt,
Trond Myklebust7c1d71c2008-04-17 16:52:57 -04002029 req->rq_connect_cookie);
Trond Myklebust24b74bf2008-04-19 13:15:47 -04002030 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031}
2032
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07002033static __be32 *
Chuck Leverb0e1c572008-05-21 17:09:19 -04002034rpc_encode_header(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035{
2036 struct rpc_clnt *clnt = task->tk_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 struct rpc_rqst *req = task->tk_rqstp;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07002038 __be32 *p = req->rq_svec[0].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039
2040 /* FIXME: check buffer size? */
Chuck Lever808012f2005-08-25 16:25:49 -07002041
Trond Myklebusta4f08352013-01-08 09:10:21 -05002042 p = xprt_skip_transport_header(req->rq_xprt, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 *p++ = req->rq_xid; /* XID */
2044 *p++ = htonl(RPC_CALL); /* CALL */
2045 *p++ = htonl(RPC_VERSION); /* RPC version */
2046 *p++ = htonl(clnt->cl_prog); /* program number */
2047 *p++ = htonl(clnt->cl_vers); /* program version */
2048 *p++ = htonl(task->tk_msg.rpc_proc->p_proc); /* procedure */
Trond Myklebust334ccfd2005-06-22 17:16:19 +00002049 p = rpcauth_marshcred(task, p);
2050 req->rq_slen = xdr_adjust_iovec(&req->rq_svec[0], p);
2051 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052}
2053
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07002054static __be32 *
Chuck Leverb0e1c572008-05-21 17:09:19 -04002055rpc_verify_header(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056{
Trond Myklebust4e0038b2012-03-01 17:01:05 -05002057 struct rpc_clnt *clnt = task->tk_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 struct kvec *iov = &task->tk_rqstp->rq_rcv_buf.head[0];
2059 int len = task->tk_rqstp->rq_rcv_buf.len >> 2;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07002060 __be32 *p = iov->iov_base;
2061 u32 n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 int error = -EACCES;
2063
David Howellse8896492006-08-24 15:44:19 -04002064 if ((task->tk_rqstp->rq_rcv_buf.len & 3) != 0) {
2065 /* RFC-1014 says that the representation of XDR data must be a
2066 * multiple of four bytes
2067 * - if it isn't pointer subtraction in the NFS client may give
2068 * undefined results
2069 */
Trond Myklebust8a702bb2007-06-27 18:30:26 -04002070 dprintk("RPC: %5u %s: XDR representation not a multiple of"
Harvey Harrison0dc47872008-03-05 20:47:47 -08002071 " 4 bytes: 0x%x\n", task->tk_pid, __func__,
Trond Myklebust8a702bb2007-06-27 18:30:26 -04002072 task->tk_rqstp->rq_rcv_buf.len);
David Howellse8896492006-08-24 15:44:19 -04002073 goto out_eio;
2074 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 if ((len -= 3) < 0)
2076 goto out_overflow;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04002078 p += 1; /* skip XID */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 if ((n = ntohl(*p++)) != RPC_REPLY) {
Trond Myklebust8a702bb2007-06-27 18:30:26 -04002080 dprintk("RPC: %5u %s: not an RPC reply: %x\n",
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04002081 task->tk_pid, __func__, n);
Trond Myklebustabbcf282006-01-03 09:55:03 +01002082 goto out_garbage;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 }
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04002084
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 if ((n = ntohl(*p++)) != RPC_MSG_ACCEPTED) {
2086 if (--len < 0)
2087 goto out_overflow;
2088 switch ((n = ntohl(*p++))) {
Joe Perches89f0e4f2011-07-01 09:43:12 +00002089 case RPC_AUTH_ERROR:
2090 break;
2091 case RPC_MISMATCH:
2092 dprintk("RPC: %5u %s: RPC call version mismatch!\n",
2093 task->tk_pid, __func__);
2094 error = -EPROTONOSUPPORT;
2095 goto out_err;
2096 default:
2097 dprintk("RPC: %5u %s: RPC call rejected, "
2098 "unknown error: %x\n",
2099 task->tk_pid, __func__, n);
2100 goto out_eio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 }
2102 if (--len < 0)
2103 goto out_overflow;
2104 switch ((n = ntohl(*p++))) {
2105 case RPC_AUTH_REJECTEDCRED:
2106 case RPC_AUTH_REJECTEDVERF:
2107 case RPCSEC_GSS_CREDPROBLEM:
2108 case RPCSEC_GSS_CTXPROBLEM:
2109 if (!task->tk_cred_retry)
2110 break;
2111 task->tk_cred_retry--;
Chuck Lever46121cf2007-01-31 12:14:08 -05002112 dprintk("RPC: %5u %s: retry stale creds\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08002113 task->tk_pid, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 rpcauth_invalcred(task);
Trond Myklebust220bcc22007-10-01 12:06:48 -04002115 /* Ensure we obtain a new XID! */
2116 xprt_release(task);
Trond Myklebust118df3d2010-10-24 17:17:31 -04002117 task->tk_action = call_reserve;
Trond Myklebustabbcf282006-01-03 09:55:03 +01002118 goto out_retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 case RPC_AUTH_BADCRED:
2120 case RPC_AUTH_BADVERF:
2121 /* possibly garbled cred/verf? */
2122 if (!task->tk_garb_retry)
2123 break;
2124 task->tk_garb_retry--;
Chuck Lever46121cf2007-01-31 12:14:08 -05002125 dprintk("RPC: %5u %s: retry garbled creds\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08002126 task->tk_pid, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 task->tk_action = call_bind;
Trond Myklebustabbcf282006-01-03 09:55:03 +01002128 goto out_retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 case RPC_AUTH_TOOWEAK:
Trond Myklebust4e0038b2012-03-01 17:01:05 -05002130 rcu_read_lock();
Chuck Leverb0e1c572008-05-21 17:09:19 -04002131 printk(KERN_NOTICE "RPC: server %s requires stronger "
Trond Myklebust4e0038b2012-03-01 17:01:05 -05002132 "authentication.\n",
2133 rcu_dereference(clnt->cl_xprt)->servername);
2134 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 break;
2136 default:
Trond Myklebust8a702bb2007-06-27 18:30:26 -04002137 dprintk("RPC: %5u %s: unknown auth error: %x\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08002138 task->tk_pid, __func__, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 error = -EIO;
2140 }
Chuck Lever46121cf2007-01-31 12:14:08 -05002141 dprintk("RPC: %5u %s: call rejected %d\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08002142 task->tk_pid, __func__, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 goto out_err;
2144 }
2145 if (!(p = rpcauth_checkverf(task, p))) {
Trond Myklebust8a702bb2007-06-27 18:30:26 -04002146 dprintk("RPC: %5u %s: auth check failed\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08002147 task->tk_pid, __func__);
Trond Myklebustabbcf282006-01-03 09:55:03 +01002148 goto out_garbage; /* bad verifier, retry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 }
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07002150 len = p - (__be32 *)iov->iov_base - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 if (len < 0)
2152 goto out_overflow;
2153 switch ((n = ntohl(*p++))) {
2154 case RPC_SUCCESS:
2155 return p;
2156 case RPC_PROG_UNAVAIL:
Trond Myklebust4e0038b2012-03-01 17:01:05 -05002157 dprintk_rcu("RPC: %5u %s: program %u is unsupported "
2158 "by server %s\n", task->tk_pid, __func__,
2159 (unsigned int)clnt->cl_prog,
2160 rcu_dereference(clnt->cl_xprt)->servername);
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00002161 error = -EPFNOSUPPORT;
2162 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 case RPC_PROG_MISMATCH:
Trond Myklebust4e0038b2012-03-01 17:01:05 -05002164 dprintk_rcu("RPC: %5u %s: program %u, version %u unsupported "
2165 "by server %s\n", task->tk_pid, __func__,
2166 (unsigned int)clnt->cl_prog,
2167 (unsigned int)clnt->cl_vers,
2168 rcu_dereference(clnt->cl_xprt)->servername);
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00002169 error = -EPROTONOSUPPORT;
2170 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 case RPC_PROC_UNAVAIL:
Trond Myklebust4e0038b2012-03-01 17:01:05 -05002172 dprintk_rcu("RPC: %5u %s: proc %s unsupported by program %u, "
Chuck Lever46121cf2007-01-31 12:14:08 -05002173 "version %u on server %s\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08002174 task->tk_pid, __func__,
Chuck Lever3748f1e2008-05-21 17:09:12 -04002175 rpc_proc_name(task),
Trond Myklebust4e0038b2012-03-01 17:01:05 -05002176 clnt->cl_prog, clnt->cl_vers,
2177 rcu_dereference(clnt->cl_xprt)->servername);
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00002178 error = -EOPNOTSUPP;
2179 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 case RPC_GARBAGE_ARGS:
Chuck Lever46121cf2007-01-31 12:14:08 -05002181 dprintk("RPC: %5u %s: server saw garbage\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08002182 task->tk_pid, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 break; /* retry */
2184 default:
Trond Myklebust8a702bb2007-06-27 18:30:26 -04002185 dprintk("RPC: %5u %s: server accept status: %x\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08002186 task->tk_pid, __func__, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 /* Also retry */
2188 }
2189
Trond Myklebustabbcf282006-01-03 09:55:03 +01002190out_garbage:
Trond Myklebust4e0038b2012-03-01 17:01:05 -05002191 clnt->cl_stats->rpcgarbage++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 if (task->tk_garb_retry) {
2193 task->tk_garb_retry--;
Chuck Lever46121cf2007-01-31 12:14:08 -05002194 dprintk("RPC: %5u %s: retrying\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08002195 task->tk_pid, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 task->tk_action = call_bind;
Trond Myklebustabbcf282006-01-03 09:55:03 +01002197out_retry:
2198 return ERR_PTR(-EAGAIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200out_eio:
2201 error = -EIO;
2202out_err:
2203 rpc_exit(task, error);
Trond Myklebust8a702bb2007-06-27 18:30:26 -04002204 dprintk("RPC: %5u %s: call failed with error %d\n", task->tk_pid,
Harvey Harrison0dc47872008-03-05 20:47:47 -08002205 __func__, error);
Trond Myklebustabbcf282006-01-03 09:55:03 +01002206 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207out_overflow:
Trond Myklebust8a702bb2007-06-27 18:30:26 -04002208 dprintk("RPC: %5u %s: server reply was truncated.\n", task->tk_pid,
Harvey Harrison0dc47872008-03-05 20:47:47 -08002209 __func__);
Trond Myklebustabbcf282006-01-03 09:55:03 +01002210 goto out_garbage;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211}
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00002212
Chuck Lever9f06c712010-12-14 14:59:18 +00002213static void rpcproc_encode_null(void *rqstp, struct xdr_stream *xdr, void *obj)
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00002214{
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00002215}
2216
Chuck Leverbf269552010-12-14 14:59:29 +00002217static int rpcproc_decode_null(void *rqstp, struct xdr_stream *xdr, void *obj)
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00002218{
2219 return 0;
2220}
2221
2222static struct rpc_procinfo rpcproc_null = {
2223 .p_encode = rpcproc_encode_null,
2224 .p_decode = rpcproc_decode_null,
2225};
2226
Chuck Levercaabea82009-12-03 15:58:56 -05002227static int rpc_ping(struct rpc_clnt *clnt)
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00002228{
2229 struct rpc_message msg = {
2230 .rpc_proc = &rpcproc_null,
2231 };
2232 int err;
2233 msg.rpc_cred = authnull_ops.lookup_cred(NULL, NULL, 0);
Chuck Levercaabea82009-12-03 15:58:56 -05002234 err = rpc_call_sync(clnt, &msg, RPC_TASK_SOFT | RPC_TASK_SOFTCONN);
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00002235 put_rpccred(msg.rpc_cred);
2236 return err;
2237}
Trond Myklebust188fef12007-06-16 14:18:40 -04002238
Trond Myklebust5e1550d2007-06-23 10:17:16 -04002239struct rpc_task *rpc_call_null(struct rpc_clnt *clnt, struct rpc_cred *cred, int flags)
2240{
2241 struct rpc_message msg = {
2242 .rpc_proc = &rpcproc_null,
2243 .rpc_cred = cred,
2244 };
Trond Myklebust84115e12007-07-14 15:39:59 -04002245 struct rpc_task_setup task_setup_data = {
2246 .rpc_client = clnt,
2247 .rpc_message = &msg,
2248 .callback_ops = &rpc_default_ops,
2249 .flags = flags,
2250 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002251 return rpc_run_task(&task_setup_data);
Trond Myklebust5e1550d2007-06-23 10:17:16 -04002252}
Trond Myklebuste8914c62007-07-14 15:39:59 -04002253EXPORT_SYMBOL_GPL(rpc_call_null);
Trond Myklebust5e1550d2007-06-23 10:17:16 -04002254
Trond Myklebust188fef12007-06-16 14:18:40 -04002255#ifdef RPC_DEBUG
Chuck Lever68a23ee2008-05-21 17:09:26 -04002256static void rpc_show_header(void)
2257{
Chuck Levercb3997b2008-05-21 17:09:41 -04002258 printk(KERN_INFO "-pid- flgs status -client- --rqstp- "
2259 "-timeout ---ops--\n");
Chuck Lever68a23ee2008-05-21 17:09:26 -04002260}
2261
Chuck Lever38e886e2008-05-21 17:09:33 -04002262static void rpc_show_task(const struct rpc_clnt *clnt,
2263 const struct rpc_task *task)
2264{
2265 const char *rpc_waitq = "none";
Chuck Lever38e886e2008-05-21 17:09:33 -04002266
2267 if (RPC_IS_QUEUED(task))
2268 rpc_waitq = rpc_qname(task->tk_waitqueue);
2269
Joe Perchesb3bceda2010-12-21 10:52:24 -05002270 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 -04002271 task->tk_pid, task->tk_flags, task->tk_status,
2272 clnt, task->tk_rqstp, task->tk_timeout, task->tk_ops,
Trond Myklebust55909f22013-08-23 11:48:15 -04002273 clnt->cl_program->name, clnt->cl_vers, rpc_proc_name(task),
Joe Perchesb3bceda2010-12-21 10:52:24 -05002274 task->tk_action, rpc_waitq);
Chuck Lever38e886e2008-05-21 17:09:33 -04002275}
2276
Stanislav Kinsbursky70abc492012-01-12 22:07:51 +04002277void rpc_show_tasks(struct net *net)
Trond Myklebust188fef12007-06-16 14:18:40 -04002278{
2279 struct rpc_clnt *clnt;
Chuck Lever38e886e2008-05-21 17:09:33 -04002280 struct rpc_task *task;
Chuck Lever68a23ee2008-05-21 17:09:26 -04002281 int header = 0;
Stanislav Kinsbursky70abc492012-01-12 22:07:51 +04002282 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
Trond Myklebust188fef12007-06-16 14:18:40 -04002283
Stanislav Kinsbursky70abc492012-01-12 22:07:51 +04002284 spin_lock(&sn->rpc_client_lock);
2285 list_for_each_entry(clnt, &sn->all_clients, cl_clients) {
Trond Myklebust188fef12007-06-16 14:18:40 -04002286 spin_lock(&clnt->cl_lock);
Chuck Lever38e886e2008-05-21 17:09:33 -04002287 list_for_each_entry(task, &clnt->cl_tasks, tk_task) {
Chuck Lever68a23ee2008-05-21 17:09:26 -04002288 if (!header) {
2289 rpc_show_header();
2290 header++;
2291 }
Chuck Lever38e886e2008-05-21 17:09:33 -04002292 rpc_show_task(clnt, task);
Trond Myklebust188fef12007-06-16 14:18:40 -04002293 }
2294 spin_unlock(&clnt->cl_lock);
2295 }
Stanislav Kinsbursky70abc492012-01-12 22:07:51 +04002296 spin_unlock(&sn->rpc_client_lock);
Trond Myklebust188fef12007-06-16 14:18:40 -04002297}
2298#endif