blob: 8d68efd2026feb12751fdfcae906a775248e5b34 [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>
Trond Myklebust40b00b6b2013-10-17 14:12:23 -040028#include <linux/rcupdate.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/utsname.h>
Chuck Lever11c556b2006-03-20 13:44:22 -050030#include <linux/workqueue.h>
Chuck Lever176e21e2011-05-09 15:22:44 -040031#include <linux/in.h>
Chuck Lever510deb02007-12-10 14:56:24 -050032#include <linux/in6.h>
Chuck Lever176e21e2011-05-09 15:22:44 -040033#include <linux/un.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
Jeff Laytonf895b252014-11-17 16:58:04 -050045#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046# 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 Myklebustc36dcfe12013-08-26 17:44:26 -0400105 rpc_remove_client_dir(clnt);
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400106}
107
108static void rpc_clnt_remove_pipedir(struct rpc_clnt *clnt)
109{
Trond Myklebust2446ab62012-03-01 17:00:56 -0500110 struct net *net = rpc_net_ns(clnt);
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400111 struct super_block *pipefs_sb;
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400112
Trond Myklebust2446ab62012-03-01 17:00:56 -0500113 pipefs_sb = rpc_get_sb_net(net);
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400114 if (pipefs_sb) {
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400115 __rpc_clnt_remove_pipedir(clnt);
Trond Myklebust2446ab62012-03-01 17:00:56 -0500116 rpc_put_sb_net(net);
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400117 }
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400118}
119
120static struct dentry *rpc_setup_pipedir_sb(struct super_block *sb,
Trond Myklebust41b6b4d2013-08-23 13:03:50 -0400121 struct rpc_clnt *clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
Trond Myklebustf1345852005-09-23 11:08:25 -0400123 static uint32_t clntid;
Trond Myklebust41b6b4d2013-08-23 13:03:50 -0400124 const char *dir_name = clnt->cl_program->pipe_dir_name;
Trond Myklebust23ac6582009-08-09 15:14:25 -0400125 char name[15];
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400126 struct dentry *dir, *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400128 dir = rpc_d_lookup_sb(sb, dir_name);
Weston Andros Adamson922eeac2012-10-23 10:43:25 -0400129 if (dir == NULL) {
130 pr_info("RPC: pipefs directory doesn't exist: %s\n", dir_name);
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400131 return dir;
Weston Andros Adamson922eeac2012-10-23 10:43:25 -0400132 }
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400133 for (;;) {
Al Viroa95e6912013-07-14 16:43:54 +0400134 snprintf(name, sizeof(name), "clnt%x", (unsigned int)clntid++);
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400135 name[sizeof(name) - 1] = '\0';
Al Viroa95e6912013-07-14 16:43:54 +0400136 dentry = rpc_create_client_dir(dir, name, clnt);
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400137 if (!IS_ERR(dentry))
138 break;
Al Viroa95e6912013-07-14 16:43:54 +0400139 if (dentry == ERR_PTR(-EEXIST))
140 continue;
141 printk(KERN_INFO "RPC: Couldn't create pipefs entry"
142 " %s/%s, error %ld\n",
143 dir_name, name, PTR_ERR(dentry));
144 break;
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400145 }
146 dput(dir);
147 return dentry;
148}
149
150static int
Trond Myklebust41b6b4d2013-08-23 13:03:50 -0400151rpc_setup_pipedir(struct super_block *pipefs_sb, struct rpc_clnt *clnt)
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400152{
Stanislav Kinsbursky30507f52012-01-11 19:18:42 +0400153 struct dentry *dentry;
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400154
Trond Myklebustc36dcfe12013-08-26 17:44:26 -0400155 if (clnt->cl_program->pipe_dir_name != NULL) {
156 dentry = rpc_setup_pipedir_sb(pipefs_sb, clnt);
157 if (IS_ERR(dentry))
158 return PTR_ERR(dentry);
159 }
Trond Myklebust23ac6582009-08-09 15:14:25 -0400160 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161}
162
Trond Myklebust41b6b4d2013-08-23 13:03:50 -0400163static int rpc_clnt_skip_event(struct rpc_clnt *clnt, unsigned long event)
Stanislav Kinsburskyea8cfa02012-04-27 13:00:17 +0400164{
Trond Myklebust41b6b4d2013-08-23 13:03:50 -0400165 if (clnt->cl_program->pipe_dir_name == NULL)
Stanislav Kinsburskyea8cfa02012-04-27 13:00:17 +0400166 return 1;
Trond Myklebust41b6b4d2013-08-23 13:03:50 -0400167
Trond Myklebustc36dcfe12013-08-26 17:44:26 -0400168 switch (event) {
169 case RPC_PIPEFS_MOUNT:
170 if (clnt->cl_pipedir_objects.pdh_dentry != NULL)
171 return 1;
172 if (atomic_read(&clnt->cl_count) == 0)
173 return 1;
174 break;
175 case RPC_PIPEFS_UMOUNT:
176 if (clnt->cl_pipedir_objects.pdh_dentry == NULL)
177 return 1;
178 break;
179 }
Stanislav Kinsburskyea8cfa02012-04-27 13:00:17 +0400180 return 0;
181}
182
183static int __rpc_clnt_handle_event(struct rpc_clnt *clnt, unsigned long event,
184 struct super_block *sb)
Stanislav Kinsbursky80df9d22012-01-11 19:18:17 +0400185{
186 struct dentry *dentry;
Stanislav Kinsbursky80df9d22012-01-11 19:18:17 +0400187
188 switch (event) {
189 case RPC_PIPEFS_MOUNT:
Trond Myklebust41b6b4d2013-08-23 13:03:50 -0400190 dentry = rpc_setup_pipedir_sb(sb, clnt);
Weston Andros Adamson922eeac2012-10-23 10:43:25 -0400191 if (!dentry)
192 return -ENOENT;
Stanislav Kinsbursky80df9d22012-01-11 19:18:17 +0400193 if (IS_ERR(dentry))
194 return PTR_ERR(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 }
Amitoj Kaur Chawla2813b622016-08-08 14:43:49 +0530203 return 0;
Stanislav Kinsbursky80df9d22012-01-11 19:18:17 +0400204}
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 Myklebust40b00b6b2013-10-17 14:12:23 -0400266static struct rpc_xprt *rpc_clnt_set_transport(struct rpc_clnt *clnt,
267 struct rpc_xprt *xprt,
268 const struct rpc_timeout *timeout)
269{
270 struct rpc_xprt *old;
271
272 spin_lock(&clnt->cl_lock);
Trond Myklebust34751b92013-10-28 16:42:44 -0400273 old = rcu_dereference_protected(clnt->cl_xprt,
274 lockdep_is_held(&clnt->cl_lock));
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400275
276 if (!xprt_bound(xprt))
277 clnt->cl_autobind = 1;
278
279 clnt->cl_timeout = timeout;
280 rcu_assign_pointer(clnt->cl_xprt, xprt);
281 spin_unlock(&clnt->cl_lock);
282
283 return old;
284}
285
Trond Myklebustcbbb3442012-04-30 11:52:40 -0400286static void rpc_clnt_set_nodename(struct rpc_clnt *clnt, const char *nodename)
287{
Trond Myklebust03a9a422015-01-30 18:12:28 -0500288 clnt->cl_nodelen = strlcpy(clnt->cl_nodename,
289 nodename, sizeof(clnt->cl_nodename));
Trond Myklebustcbbb3442012-04-30 11:52:40 -0400290}
291
Chuck Leverd746e542013-10-17 14:12:17 -0400292static int rpc_client_register(struct rpc_clnt *clnt,
293 rpc_authflavor_t pseudoflavor,
294 const char *client_name)
Stanislav Kinsburskye73f4cc2013-06-24 11:52:52 +0400295{
Trond Myklebustc2190662013-08-26 19:23:04 -0400296 struct rpc_auth_create_args auth_args = {
Chuck Leverd746e542013-10-17 14:12:17 -0400297 .pseudoflavor = pseudoflavor,
298 .target_name = client_name,
Trond Myklebustc2190662013-08-26 19:23:04 -0400299 };
Stanislav Kinsburskye73f4cc2013-06-24 11:52:52 +0400300 struct rpc_auth *auth;
301 struct net *net = rpc_net_ns(clnt);
302 struct super_block *pipefs_sb;
Trond Myklebusteeee2452013-07-10 15:33:01 -0400303 int err;
Stanislav Kinsburskye73f4cc2013-06-24 11:52:52 +0400304
Jeff Laytonf9c72d12015-03-31 12:03:28 -0400305 rpc_clnt_debugfs_register(clnt);
Jeff Laytonb4b9d2c2014-11-26 14:44:43 -0500306
Stanislav Kinsburskye73f4cc2013-06-24 11:52:52 +0400307 pipefs_sb = rpc_get_sb_net(net);
308 if (pipefs_sb) {
Trond Myklebust41b6b4d2013-08-23 13:03:50 -0400309 err = rpc_setup_pipedir(pipefs_sb, clnt);
Stanislav Kinsburskye73f4cc2013-06-24 11:52:52 +0400310 if (err)
311 goto out;
312 }
313
Trond Myklebusteeee2452013-07-10 15:33:01 -0400314 rpc_register_client(clnt);
315 if (pipefs_sb)
316 rpc_put_sb_net(net);
317
Trond Myklebustc2190662013-08-26 19:23:04 -0400318 auth = rpcauth_create(&auth_args, clnt);
Stanislav Kinsburskye73f4cc2013-06-24 11:52:52 +0400319 if (IS_ERR(auth)) {
320 dprintk("RPC: Couldn't create auth handle (flavor %u)\n",
Chuck Leverd746e542013-10-17 14:12:17 -0400321 pseudoflavor);
Stanislav Kinsburskye73f4cc2013-06-24 11:52:52 +0400322 err = PTR_ERR(auth);
323 goto err_auth;
324 }
Trond Myklebusteeee2452013-07-10 15:33:01 -0400325 return 0;
326err_auth:
327 pipefs_sb = rpc_get_sb_net(net);
Trond Myklebust1540c5d2013-07-14 22:57:50 -0400328 rpc_unregister_client(clnt);
Trond Myklebusteeee2452013-07-10 15:33:01 -0400329 __rpc_clnt_remove_pipedir(clnt);
Stanislav Kinsburskye73f4cc2013-06-24 11:52:52 +0400330out:
331 if (pipefs_sb)
332 rpc_put_sb_net(net);
Jeff Laytonb4b9d2c2014-11-26 14:44:43 -0500333 rpc_clnt_debugfs_unregister(clnt);
Stanislav Kinsburskye73f4cc2013-06-24 11:52:52 +0400334 return err;
Stanislav Kinsburskye73f4cc2013-06-24 11:52:52 +0400335}
336
Trond Myklebust2f048db2013-09-04 21:51:44 -0400337static DEFINE_IDA(rpc_clids);
338
339static int rpc_alloc_clid(struct rpc_clnt *clnt)
340{
341 int clid;
342
343 clid = ida_simple_get(&rpc_clids, 0, 0, GFP_KERNEL);
344 if (clid < 0)
345 return clid;
346 clnt->cl_clid = clid;
347 return 0;
348}
349
350static void rpc_free_clid(struct rpc_clnt *clnt)
351{
352 ida_simple_remove(&rpc_clids, clnt->cl_clid);
353}
354
Trond Myklebust280ebcf2013-09-02 13:28:04 -0400355static struct rpc_clnt * rpc_new_client(const struct rpc_create_args *args,
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500356 struct rpc_xprt_switch *xps,
Trond Myklebust280ebcf2013-09-02 13:28:04 -0400357 struct rpc_xprt *xprt,
358 struct rpc_clnt *parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359{
Trond Myklebusta613fa12012-01-20 13:53:56 -0500360 const struct rpc_program *program = args->program;
361 const struct rpc_version *version;
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400362 struct rpc_clnt *clnt = NULL;
363 const struct rpc_timeout *timeout;
Trond Myklebust03a9a422015-01-30 18:12:28 -0500364 const char *nodename = args->nodename;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 int err;
Chuck Lever06b8d252007-09-11 18:00:20 -0400366
367 /* sanity check the name before trying to print it */
Chuck Lever46121cf2007-01-31 12:14:08 -0500368 dprintk("RPC: creating %s client for %s (xprt %p)\n",
Trond Myklebust698b6d02007-12-20 16:03:53 -0500369 program->name, args->servername, xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Trond Myklebust4ada5392007-06-14 17:26:17 -0400371 err = rpciod_up();
372 if (err)
373 goto out_no_rpciod;
Trond Myklebust698b6d02007-12-20 16:03:53 -0500374
Trond Myklebustf05c1242013-04-05 14:13:21 -0400375 err = -EINVAL;
Trond Myklebust698b6d02007-12-20 16:03:53 -0500376 if (args->version >= program->nrvers)
377 goto out_err;
378 version = program->version[args->version];
379 if (version == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 goto out_err;
381
382 err = -ENOMEM;
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700383 clnt = kzalloc(sizeof(*clnt), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 if (!clnt)
385 goto out_err;
Trond Myklebust280ebcf2013-09-02 13:28:04 -0400386 clnt->cl_parent = parent ? : clnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
Trond Myklebust2f048db2013-09-04 21:51:44 -0400388 err = rpc_alloc_clid(clnt);
389 if (err)
390 goto out_no_clid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 clnt->cl_procinfo = version->procs;
393 clnt->cl_maxproc = version->nrprocs;
Benny Halevyd5b337b2008-09-28 09:21:26 +0300394 clnt->cl_prog = args->prognumber ? : program->number;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 clnt->cl_vers = version->number;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 clnt->cl_stats = program->stats;
Chuck Lever11c556b2006-03-20 13:44:22 -0500397 clnt->cl_metrics = rpc_alloc_iostats(clnt);
Trond Myklebust6739ffb2013-08-26 15:38:11 -0400398 rpc_init_pipe_dir_head(&clnt->cl_pipedir_objects);
Trond Myklebust23bf85b2006-11-21 10:40:23 -0500399 err = -ENOMEM;
400 if (clnt->cl_metrics == NULL)
401 goto out_no_stats;
Trond Myklebust3e32a5d2006-11-16 11:37:27 -0500402 clnt->cl_program = program;
Trond Myklebust6529eba2007-06-14 16:40:14 -0400403 INIT_LIST_HEAD(&clnt->cl_tasks);
Trond Myklebust4bef61f2007-06-16 14:17:01 -0400404 spin_lock_init(&clnt->cl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400406 timeout = xprt->timeout;
Trond Myklebustba7392b2007-12-20 16:03:55 -0500407 if (args->timeout != NULL) {
408 memcpy(&clnt->cl_timeout_default, args->timeout,
409 sizeof(clnt->cl_timeout_default));
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400410 timeout = &clnt->cl_timeout_default;
Trond Myklebustba7392b2007-12-20 16:03:55 -0500411 }
412
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400413 rpc_clnt_set_transport(clnt, xprt, timeout);
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500414 xprt_iter_init(&clnt->cl_xpi, xps);
415 xprt_switch_put(xps);
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400416
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 clnt->cl_rtt = &clnt->cl_rtt_default;
Trond Myklebustba7392b2007-12-20 16:03:55 -0500418 rpc_init_rtt(&clnt->cl_rtt_default, clnt->cl_timeout->to_initval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Trond Myklebust006abe82010-09-12 19:55:25 -0400420 atomic_set(&clnt->cl_count, 1);
Trond Myklebust34f52e32007-06-14 16:40:31 -0400421
Trond Myklebust03a9a422015-01-30 18:12:28 -0500422 if (nodename == NULL)
423 nodename = utsname()->nodename;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 /* save the nodename */
Trond Myklebust03a9a422015-01-30 18:12:28 -0500425 rpc_clnt_set_nodename(clnt, nodename);
Stanislav Kinsburskye73f4cc2013-06-24 11:52:52 +0400426
Chuck Leverd746e542013-10-17 14:12:17 -0400427 err = rpc_client_register(clnt, args->authflavor, args->client_name);
Stanislav Kinsburskye73f4cc2013-06-24 11:52:52 +0400428 if (err)
429 goto out_no_path;
Trond Myklebust280ebcf2013-09-02 13:28:04 -0400430 if (parent)
431 atomic_inc(&parent->cl_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 return clnt;
433
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434out_no_path:
Trond Myklebust23bf85b2006-11-21 10:40:23 -0500435 rpc_free_iostats(clnt->cl_metrics);
436out_no_stats:
Trond Myklebust2f048db2013-09-04 21:51:44 -0400437 rpc_free_clid(clnt);
438out_no_clid:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 kfree(clnt);
440out_err:
Trond Myklebust4ada5392007-06-14 17:26:17 -0400441 rpciod_down();
442out_no_rpciod:
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500443 xprt_switch_put(xps);
Trond Myklebustf05c1242013-04-05 14:13:21 -0400444 xprt_put(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 return ERR_PTR(err);
446}
447
J. Bruce Fieldsd50039e2016-05-16 17:03:42 -0400448static struct rpc_clnt *rpc_create_xprt(struct rpc_create_args *args,
Kinglong Mee83ddfeb2014-03-24 11:58:59 +0800449 struct rpc_xprt *xprt)
450{
451 struct rpc_clnt *clnt = NULL;
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500452 struct rpc_xprt_switch *xps;
Kinglong Mee83ddfeb2014-03-24 11:58:59 +0800453
J. Bruce Fields39a9bea2016-05-17 12:38:21 -0400454 if (args->bc_xprt && args->bc_xprt->xpt_bc_xps) {
Chuck Lever16590a22016-08-22 14:57:42 -0400455 WARN_ON_ONCE(!(args->protocol & XPRT_TRANSPORT_BC));
J. Bruce Fields39a9bea2016-05-17 12:38:21 -0400456 xps = args->bc_xprt->xpt_bc_xps;
457 xprt_switch_get(xps);
458 } else {
459 xps = xprt_switch_alloc(xprt, GFP_KERNEL);
460 if (xps == NULL) {
461 xprt_put(xprt);
462 return ERR_PTR(-ENOMEM);
463 }
464 if (xprt->bc_xprt) {
465 xprt_switch_get(xps);
466 xprt->bc_xprt->xpt_bc_xps = xps;
467 }
J. Bruce Fields1208fd52016-05-20 17:07:17 -0400468 }
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500469 clnt = rpc_new_client(args, xps, xprt, NULL);
Kinglong Mee83ddfeb2014-03-24 11:58:59 +0800470 if (IS_ERR(clnt))
471 return clnt;
472
473 if (!(args->flags & RPC_CLNT_CREATE_NOPING)) {
474 int err = rpc_ping(clnt);
475 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
485 if (args->flags & RPC_CLNT_CREATE_AUTOBIND)
486 clnt->cl_autobind = 1;
Trond Myklebust2aca5b82014-09-24 22:35:58 -0400487 if (args->flags & RPC_CLNT_CREATE_NO_RETRANS_TIMEOUT)
488 clnt->cl_noretranstimeo = 1;
Kinglong Mee83ddfeb2014-03-24 11:58:59 +0800489 if (args->flags & RPC_CLNT_CREATE_DISCRTRY)
490 clnt->cl_discrtry = 1;
491 if (!(args->flags & RPC_CLNT_CREATE_QUIET))
492 clnt->cl_chatty = 1;
493
494 return clnt;
495}
Kinglong Mee83ddfeb2014-03-24 11:58:59 +0800496
Ben Hutchings2c530402012-07-10 10:55:09 +0000497/**
Chuck Leverc2866762006-08-22 20:06:20 -0400498 * rpc_create - create an RPC client and transport with one call
499 * @args: rpc_clnt create argument structure
500 *
501 * Creates and initializes an RPC transport and an RPC client.
502 *
503 * It can ping the server in order to determine if it is up, and to see if
504 * it supports this program and version. RPC_CLNT_CREATE_NOPING disables
505 * this behavior so asynchronous tasks can also use rpc_create.
506 */
507struct rpc_clnt *rpc_create(struct rpc_create_args *args)
508{
509 struct rpc_xprt *xprt;
\"Talpey, Thomas\3c341b0b2007-09-10 13:47:07 -0400510 struct xprt_create xprtargs = {
Pavel Emelyanov9a23e332010-09-29 16:05:12 +0400511 .net = args->net,
\"Talpey, Thomas\4fa016e2007-09-10 13:47:57 -0400512 .ident = args->protocol,
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +0200513 .srcaddr = args->saddress,
Frank van Maarseveen96802a02007-07-08 13:08:54 +0200514 .dstaddr = args->address,
515 .addrlen = args->addrsize,
Trond Myklebust4e0038b2012-03-01 17:01:05 -0500516 .servername = args->servername,
Alexandros Batsakisf300bab2009-09-10 17:33:30 +0300517 .bc_xprt = args->bc_xprt,
Frank van Maarseveen96802a02007-07-08 13:08:54 +0200518 };
Chuck Lever510deb02007-12-10 14:56:24 -0500519 char servername[48];
Chuck Leverc2866762006-08-22 20:06:20 -0400520
J. Bruce Fieldsd50039e2016-05-16 17:03:42 -0400521 if (args->bc_xprt) {
Chuck Lever16590a22016-08-22 14:57:42 -0400522 WARN_ON_ONCE(!(args->protocol & XPRT_TRANSPORT_BC));
J. Bruce Fieldsd50039e2016-05-16 17:03:42 -0400523 xprt = args->bc_xprt->xpt_bc_xprt;
524 if (xprt) {
525 xprt_get(xprt);
526 return rpc_create_xprt(args, xprt);
527 }
528 }
529
Trond Myklebustb7993ce2013-04-14 11:42:00 -0400530 if (args->flags & RPC_CLNT_CREATE_INFINITE_SLOTS)
531 xprtargs.flags |= XPRT_CREATE_INFINITE_SLOTS;
J. Bruce Fields33d90ac2013-04-11 15:06:36 -0400532 if (args->flags & RPC_CLNT_CREATE_NO_IDLE_TIMEOUT)
533 xprtargs.flags |= XPRT_CREATE_NO_IDLE_TIMEOUT;
Chuck Leverc2866762006-08-22 20:06:20 -0400534 /*
Chuck Lever43780b82007-07-01 12:13:22 -0400535 * If the caller chooses not to specify a hostname, whip
536 * up a string representation of the passed-in address.
537 */
Trond Myklebust4e0038b2012-03-01 17:01:05 -0500538 if (xprtargs.servername == NULL) {
Chuck Lever176e21e2011-05-09 15:22:44 -0400539 struct sockaddr_un *sun =
540 (struct sockaddr_un *)args->address;
Chuck Leverda09eb92011-05-09 15:22:25 -0400541 struct sockaddr_in *sin =
542 (struct sockaddr_in *)args->address;
543 struct sockaddr_in6 *sin6 =
544 (struct sockaddr_in6 *)args->address;
545
Chuck Lever510deb02007-12-10 14:56:24 -0500546 servername[0] = '\0';
547 switch (args->address->sa_family) {
Chuck Lever176e21e2011-05-09 15:22:44 -0400548 case AF_LOCAL:
549 snprintf(servername, sizeof(servername), "%s",
550 sun->sun_path);
551 break;
Chuck Leverda09eb92011-05-09 15:22:25 -0400552 case AF_INET:
Harvey Harrison21454aa2008-10-31 00:54:56 -0700553 snprintf(servername, sizeof(servername), "%pI4",
554 &sin->sin_addr.s_addr);
Chuck Lever510deb02007-12-10 14:56:24 -0500555 break;
Chuck Leverda09eb92011-05-09 15:22:25 -0400556 case AF_INET6:
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700557 snprintf(servername, sizeof(servername), "%pI6",
Chuck Leverda09eb92011-05-09 15:22:25 -0400558 &sin6->sin6_addr);
Chuck Lever510deb02007-12-10 14:56:24 -0500559 break;
Chuck Lever510deb02007-12-10 14:56:24 -0500560 default:
561 /* caller wants default server name, but
562 * address family isn't recognized. */
563 return ERR_PTR(-EINVAL);
564 }
Trond Myklebust4e0038b2012-03-01 17:01:05 -0500565 xprtargs.servername = servername;
Chuck Lever43780b82007-07-01 12:13:22 -0400566 }
567
Chuck Lever510deb02007-12-10 14:56:24 -0500568 xprt = xprt_create_transport(&xprtargs);
569 if (IS_ERR(xprt))
570 return (struct rpc_clnt *)xprt;
571
Chuck Lever43780b82007-07-01 12:13:22 -0400572 /*
Chuck Leverc2866762006-08-22 20:06:20 -0400573 * By default, kernel RPC client connects from a reserved port.
574 * CAP_NET_BIND_SERVICE will not be set for unprivileged requesters,
575 * but it is always enabled for rpciod, which handles the connect
576 * operation.
577 */
578 xprt->resvport = 1;
579 if (args->flags & RPC_CLNT_CREATE_NONPRIVPORT)
580 xprt->resvport = 0;
581
Kinglong Mee83ddfeb2014-03-24 11:58:59 +0800582 return rpc_create_xprt(args, xprt);
Chuck Leverc2866762006-08-22 20:06:20 -0400583}
Chuck Leverb86acd52006-08-22 20:06:22 -0400584EXPORT_SYMBOL_GPL(rpc_create);
Chuck Leverc2866762006-08-22 20:06:20 -0400585
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586/*
587 * This function clones the RPC client structure. It allows us to share the
588 * same transport while varying parameters such as the authentication
589 * flavour.
590 */
Chuck Lever1b63a752012-09-14 17:23:52 -0400591static struct rpc_clnt *__rpc_clone_client(struct rpc_create_args *args,
592 struct rpc_clnt *clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593{
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500594 struct rpc_xprt_switch *xps;
Trond Myklebust2446ab62012-03-01 17:00:56 -0500595 struct rpc_xprt *xprt;
Chuck Lever1b63a752012-09-14 17:23:52 -0400596 struct rpc_clnt *new;
597 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
Chuck Lever1b63a752012-09-14 17:23:52 -0400599 err = -ENOMEM;
Trond Myklebust2446ab62012-03-01 17:00:56 -0500600 rcu_read_lock();
601 xprt = xprt_get(rcu_dereference(clnt->cl_xprt));
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500602 xps = xprt_switch_get(rcu_dereference(clnt->cl_xpi.xpi_xpswitch));
Trond Myklebust2446ab62012-03-01 17:00:56 -0500603 rcu_read_unlock();
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500604 if (xprt == NULL || xps == NULL) {
605 xprt_put(xprt);
606 xprt_switch_put(xps);
Chuck Lever1b63a752012-09-14 17:23:52 -0400607 goto out_err;
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500608 }
Chuck Lever1b63a752012-09-14 17:23:52 -0400609 args->servername = xprt->servername;
Trond Myklebust03a9a422015-01-30 18:12:28 -0500610 args->nodename = clnt->cl_nodename;
Chuck Lever1b63a752012-09-14 17:23:52 -0400611
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500612 new = rpc_new_client(args, xps, xprt, clnt);
Chuck Lever1b63a752012-09-14 17:23:52 -0400613 if (IS_ERR(new)) {
614 err = PTR_ERR(new);
Chuck Levera58e0be2013-03-22 12:52:59 -0400615 goto out_err;
Chuck Lever1b63a752012-09-14 17:23:52 -0400616 }
617
Chuck Lever1b63a752012-09-14 17:23:52 -0400618 /* Turn off autobind on clones */
619 new->cl_autobind = 0;
620 new->cl_softrtry = clnt->cl_softrtry;
Trond Myklebust2aca5b82014-09-24 22:35:58 -0400621 new->cl_noretranstimeo = clnt->cl_noretranstimeo;
Chuck Lever1b63a752012-09-14 17:23:52 -0400622 new->cl_discrtry = clnt->cl_discrtry;
623 new->cl_chatty = clnt->cl_chatty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 return new;
Chuck Lever1b63a752012-09-14 17:23:52 -0400625
Chuck Lever1b63a752012-09-14 17:23:52 -0400626out_err:
Harvey Harrison0dc47872008-03-05 20:47:47 -0800627 dprintk("RPC: %s: returned error %d\n", __func__, err);
Trond Myklebust3e32a5d2006-11-16 11:37:27 -0500628 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629}
Chuck Lever1b63a752012-09-14 17:23:52 -0400630
631/**
632 * rpc_clone_client - Clone an RPC client structure
633 *
634 * @clnt: RPC client whose parameters are copied
635 *
636 * Returns a fresh RPC client or an ERR_PTR.
637 */
638struct rpc_clnt *rpc_clone_client(struct rpc_clnt *clnt)
639{
640 struct rpc_create_args args = {
641 .program = clnt->cl_program,
642 .prognumber = clnt->cl_prog,
643 .version = clnt->cl_vers,
644 .authflavor = clnt->cl_auth->au_flavor,
Chuck Lever1b63a752012-09-14 17:23:52 -0400645 };
646 return __rpc_clone_client(&args, clnt);
647}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400648EXPORT_SYMBOL_GPL(rpc_clone_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
Chuck Leverba9b5842012-09-14 17:24:02 -0400650/**
651 * rpc_clone_client_set_auth - Clone an RPC client structure and set its auth
652 *
653 * @clnt: RPC client whose parameters are copied
Randy Dunlap7144bca2013-01-09 17:12:35 -0800654 * @flavor: security flavor for new client
Chuck Leverba9b5842012-09-14 17:24:02 -0400655 *
656 * Returns a fresh RPC client or an ERR_PTR.
657 */
658struct rpc_clnt *
659rpc_clone_client_set_auth(struct rpc_clnt *clnt, rpc_authflavor_t flavor)
660{
661 struct rpc_create_args args = {
662 .program = clnt->cl_program,
663 .prognumber = clnt->cl_prog,
664 .version = clnt->cl_vers,
665 .authflavor = flavor,
Chuck Leverba9b5842012-09-14 17:24:02 -0400666 };
667 return __rpc_clone_client(&args, clnt);
668}
669EXPORT_SYMBOL_GPL(rpc_clone_client_set_auth);
670
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400671/**
672 * rpc_switch_client_transport: switch the RPC transport on the fly
673 * @clnt: pointer to a struct rpc_clnt
674 * @args: pointer to the new transport arguments
675 * @timeout: pointer to the new timeout parameters
676 *
677 * This function allows the caller to switch the RPC transport for the
678 * rpc_clnt structure 'clnt' to allow it to connect to a mirrored NFS
679 * server, for instance. It assumes that the caller has ensured that
680 * there are no active RPC tasks by using some form of locking.
681 *
682 * Returns zero if "clnt" is now using the new xprt. Otherwise a
683 * negative errno is returned, and "clnt" continues to use the old
684 * xprt.
685 */
686int rpc_switch_client_transport(struct rpc_clnt *clnt,
687 struct xprt_create *args,
688 const struct rpc_timeout *timeout)
689{
690 const struct rpc_timeout *old_timeo;
691 rpc_authflavor_t pseudoflavor;
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500692 struct rpc_xprt_switch *xps, *oldxps;
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400693 struct rpc_xprt *xprt, *old;
694 struct rpc_clnt *parent;
695 int err;
696
697 xprt = xprt_create_transport(args);
698 if (IS_ERR(xprt)) {
699 dprintk("RPC: failed to create new xprt for clnt %p\n",
700 clnt);
701 return PTR_ERR(xprt);
702 }
703
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500704 xps = xprt_switch_alloc(xprt, GFP_KERNEL);
705 if (xps == NULL) {
706 xprt_put(xprt);
707 return -ENOMEM;
708 }
709
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400710 pseudoflavor = clnt->cl_auth->au_flavor;
711
712 old_timeo = clnt->cl_timeout;
713 old = rpc_clnt_set_transport(clnt, xprt, timeout);
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500714 oldxps = xprt_iter_xchg_switch(&clnt->cl_xpi, xps);
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400715
716 rpc_unregister_client(clnt);
717 __rpc_clnt_remove_pipedir(clnt);
Jeff Laytonb4b9d2c2014-11-26 14:44:43 -0500718 rpc_clnt_debugfs_unregister(clnt);
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400719
720 /*
721 * A new transport was created. "clnt" therefore
722 * becomes the root of a new cl_parent tree. clnt's
723 * children, if it has any, still point to the old xprt.
724 */
725 parent = clnt->cl_parent;
726 clnt->cl_parent = clnt;
727
728 /*
729 * The old rpc_auth cache cannot be re-used. GSS
730 * contexts in particular are between a single
731 * client and server.
732 */
733 err = rpc_client_register(clnt, pseudoflavor, NULL);
734 if (err)
735 goto out_revert;
736
737 synchronize_rcu();
738 if (parent != clnt)
739 rpc_release_client(parent);
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500740 xprt_switch_put(oldxps);
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400741 xprt_put(old);
742 dprintk("RPC: replaced xprt for clnt %p\n", clnt);
743 return 0;
744
745out_revert:
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500746 xps = xprt_iter_xchg_switch(&clnt->cl_xpi, oldxps);
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400747 rpc_clnt_set_transport(clnt, old, old_timeo);
748 clnt->cl_parent = parent;
749 rpc_client_register(clnt, pseudoflavor, NULL);
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500750 xprt_switch_put(xps);
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400751 xprt_put(xprt);
752 dprintk("RPC: failed to switch xprt for clnt %p\n", clnt);
753 return err;
754}
755EXPORT_SYMBOL_GPL(rpc_switch_client_transport);
756
Trond Myklebust32278862016-01-30 20:39:19 -0500757static
758int rpc_clnt_xprt_iter_init(struct rpc_clnt *clnt, struct rpc_xprt_iter *xpi)
759{
760 struct rpc_xprt_switch *xps;
761
762 rcu_read_lock();
763 xps = xprt_switch_get(rcu_dereference(clnt->cl_xpi.xpi_xpswitch));
764 rcu_read_unlock();
765 if (xps == NULL)
766 return -EAGAIN;
767 xprt_iter_init_listall(xpi, xps);
768 xprt_switch_put(xps);
769 return 0;
770}
771
772/**
773 * rpc_clnt_iterate_for_each_xprt - Apply a function to all transports
774 * @clnt: pointer to client
775 * @fn: function to apply
776 * @data: void pointer to function data
777 *
778 * Iterates through the list of RPC transports currently attached to the
779 * client and applies the function fn(clnt, xprt, data).
780 *
781 * On error, the iteration stops, and the function returns the error value.
782 */
783int rpc_clnt_iterate_for_each_xprt(struct rpc_clnt *clnt,
784 int (*fn)(struct rpc_clnt *, struct rpc_xprt *, void *),
785 void *data)
786{
787 struct rpc_xprt_iter xpi;
788 int ret;
789
790 ret = rpc_clnt_xprt_iter_init(clnt, &xpi);
791 if (ret)
792 return ret;
793 for (;;) {
794 struct rpc_xprt *xprt = xprt_iter_get_next(&xpi);
795
796 if (!xprt)
797 break;
798 ret = fn(clnt, xprt, data);
799 xprt_put(xprt);
800 if (ret < 0)
801 break;
802 }
803 xprt_iter_destroy(&xpi);
804 return ret;
805}
806EXPORT_SYMBOL_GPL(rpc_clnt_iterate_for_each_xprt);
807
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808/*
Trond Myklebust58f96122010-07-31 14:29:08 -0400809 * Kill all tasks for the given client.
810 * XXX: kill their descendants as well?
811 */
812void rpc_killall_tasks(struct rpc_clnt *clnt)
813{
814 struct rpc_task *rovr;
815
816
817 if (list_empty(&clnt->cl_tasks))
818 return;
819 dprintk("RPC: killing all tasks for client %p\n", clnt);
820 /*
821 * Spin lock all_tasks to prevent changes...
822 */
823 spin_lock(&clnt->cl_lock);
824 list_for_each_entry(rovr, &clnt->cl_tasks, tk_task) {
825 if (!RPC_IS_ACTIVATED(rovr))
826 continue;
827 if (!(rovr->tk_flags & RPC_TASK_KILLED)) {
828 rovr->tk_flags |= RPC_TASK_KILLED;
829 rpc_exit(rovr, -EIO);
Stanislav Kinsbursky8e26de22011-03-17 18:54:23 +0300830 if (RPC_IS_QUEUED(rovr))
831 rpc_wake_up_queued_task(rovr->tk_waitqueue,
832 rovr);
Trond Myklebust58f96122010-07-31 14:29:08 -0400833 }
834 }
835 spin_unlock(&clnt->cl_lock);
836}
837EXPORT_SYMBOL_GPL(rpc_killall_tasks);
838
839/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 * Properly shut down an RPC client, terminating all outstanding
Trond Myklebust90c57552007-06-09 19:49:36 -0400841 * requests.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 */
Trond Myklebust4c402b42007-06-14 16:40:32 -0400843void rpc_shutdown_client(struct rpc_clnt *clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844{
Weston Andros Adamson168e4b32012-10-30 17:01:40 -0400845 might_sleep();
846
Trond Myklebust4e0038b2012-03-01 17:01:05 -0500847 dprintk_rcu("RPC: shutting down %s client for %s\n",
Trond Myklebust55909f22013-08-23 11:48:15 -0400848 clnt->cl_program->name,
Trond Myklebust4e0038b2012-03-01 17:01:05 -0500849 rcu_dereference(clnt->cl_xprt)->servername);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
Trond Myklebust34f52e32007-06-14 16:40:31 -0400851 while (!list_empty(&clnt->cl_tasks)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 rpc_killall_tasks(clnt);
Ingo Molnar532347e2006-01-09 20:52:53 -0800853 wait_event_timeout(destroy_wait,
Trond Myklebust34f52e32007-06-14 16:40:31 -0400854 list_empty(&clnt->cl_tasks), 1*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 }
856
Trond Myklebust4c402b42007-06-14 16:40:32 -0400857 rpc_release_client(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400859EXPORT_SYMBOL_GPL(rpc_shutdown_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
861/*
Trond Myklebust34f52e32007-06-14 16:40:31 -0400862 * Free an RPC client
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 */
Trond Myklebustd07ba842013-11-12 17:24:36 -0500864static struct rpc_clnt *
Trond Myklebust006abe82010-09-12 19:55:25 -0400865rpc_free_client(struct rpc_clnt *clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866{
Trond Myklebustd07ba842013-11-12 17:24:36 -0500867 struct rpc_clnt *parent = NULL;
868
Trond Myklebust4e0038b2012-03-01 17:01:05 -0500869 dprintk_rcu("RPC: destroying %s client for %s\n",
Trond Myklebust55909f22013-08-23 11:48:15 -0400870 clnt->cl_program->name,
Trond Myklebust4e0038b2012-03-01 17:01:05 -0500871 rcu_dereference(clnt->cl_xprt)->servername);
Trond Myklebust6eac7d32012-01-20 13:53:37 -0500872 if (clnt->cl_parent != clnt)
Trond Myklebustd07ba842013-11-12 17:24:36 -0500873 parent = clnt->cl_parent;
Jeff Laytonb4b9d2c2014-11-26 14:44:43 -0500874 rpc_clnt_debugfs_unregister(clnt);
Stanislav Kinsburskyf5131252012-01-11 19:18:26 +0400875 rpc_clnt_remove_pipedir(clnt);
Stanislav Kinsburskyadb6fa72013-06-26 10:15:14 +0400876 rpc_unregister_client(clnt);
Chuck Lever11c556b2006-03-20 13:44:22 -0500877 rpc_free_iostats(clnt->cl_metrics);
878 clnt->cl_metrics = NULL;
Trond Myklebust2446ab62012-03-01 17:00:56 -0500879 xprt_put(rcu_dereference_raw(clnt->cl_xprt));
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500880 xprt_iter_destroy(&clnt->cl_xpi);
Trond Myklebust4ada5392007-06-14 17:26:17 -0400881 rpciod_down();
Trond Myklebust2f048db2013-09-04 21:51:44 -0400882 rpc_free_clid(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 kfree(clnt);
Trond Myklebustd07ba842013-11-12 17:24:36 -0500884 return parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885}
886
887/*
Trond Myklebust1dd17ec2007-06-26 16:57:41 -0400888 * Free an RPC client
889 */
Trond Myklebustd07ba842013-11-12 17:24:36 -0500890static struct rpc_clnt *
Trond Myklebust006abe82010-09-12 19:55:25 -0400891rpc_free_auth(struct rpc_clnt *clnt)
Trond Myklebust1dd17ec2007-06-26 16:57:41 -0400892{
Trond Myklebustd07ba842013-11-12 17:24:36 -0500893 if (clnt->cl_auth == NULL)
894 return rpc_free_client(clnt);
Trond Myklebust1dd17ec2007-06-26 16:57:41 -0400895
896 /*
897 * Note: RPCSEC_GSS may need to send NULL RPC calls in order to
898 * release remaining GSS contexts. This mechanism ensures
899 * that it can do so safely.
900 */
Trond Myklebust006abe82010-09-12 19:55:25 -0400901 atomic_inc(&clnt->cl_count);
Trond Myklebust1dd17ec2007-06-26 16:57:41 -0400902 rpcauth_release(clnt->cl_auth);
903 clnt->cl_auth = NULL;
Trond Myklebust006abe82010-09-12 19:55:25 -0400904 if (atomic_dec_and_test(&clnt->cl_count))
Trond Myklebustd07ba842013-11-12 17:24:36 -0500905 return rpc_free_client(clnt);
906 return NULL;
Trond Myklebust1dd17ec2007-06-26 16:57:41 -0400907}
908
909/*
Trond Myklebust34f52e32007-06-14 16:40:31 -0400910 * Release reference to the RPC client
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 */
912void
913rpc_release_client(struct rpc_clnt *clnt)
914{
Trond Myklebust34f52e32007-06-14 16:40:31 -0400915 dprintk("RPC: rpc_release_client(%p)\n", clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
Trond Myklebustd07ba842013-11-12 17:24:36 -0500917 do {
918 if (list_empty(&clnt->cl_tasks))
919 wake_up(&destroy_wait);
920 if (!atomic_dec_and_test(&clnt->cl_count))
921 break;
922 clnt = rpc_free_auth(clnt);
923 } while (clnt != NULL);
Trond Myklebust34f52e32007-06-14 16:40:31 -0400924}
Simo Sorce1d658332012-05-25 18:09:55 -0400925EXPORT_SYMBOL_GPL(rpc_release_client);
Trond Myklebust34f52e32007-06-14 16:40:31 -0400926
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000927/**
928 * rpc_bind_new_program - bind a new RPC program to an existing client
Randy Dunlap65b6e422008-02-13 15:03:23 -0800929 * @old: old rpc_client
930 * @program: rpc program to set
931 * @vers: rpc program version
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000932 *
933 * Clones the rpc client and sets up a new RPC program. This is mainly
934 * of use for enabling different RPC programs to share the same transport.
935 * The Sun NFSv2/v3 ACL protocol can do this.
936 */
937struct rpc_clnt *rpc_bind_new_program(struct rpc_clnt *old,
Trond Myklebusta613fa12012-01-20 13:53:56 -0500938 const struct rpc_program *program,
Chuck Lever89eb21c2007-09-11 18:00:09 -0400939 u32 vers)
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000940{
Trond Myklebustf994c432012-11-01 12:14:14 -0400941 struct rpc_create_args args = {
942 .program = program,
943 .prognumber = program->number,
944 .version = vers,
945 .authflavor = old->cl_auth->au_flavor,
Trond Myklebustf994c432012-11-01 12:14:14 -0400946 };
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000947 struct rpc_clnt *clnt;
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000948 int err;
949
Trond Myklebustf994c432012-11-01 12:14:14 -0400950 clnt = __rpc_clone_client(&args, old);
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000951 if (IS_ERR(clnt))
952 goto out;
Chuck Levercaabea82009-12-03 15:58:56 -0500953 err = rpc_ping(clnt);
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000954 if (err != 0) {
955 rpc_shutdown_client(clnt);
956 clnt = ERR_PTR(err);
957 }
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800958out:
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000959 return clnt;
960}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400961EXPORT_SYMBOL_GPL(rpc_bind_new_program);
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000962
Trond Myklebust58f96122010-07-31 14:29:08 -0400963void rpc_task_release_client(struct rpc_task *task)
964{
965 struct rpc_clnt *clnt = task->tk_client;
Trond Myklebustfb43d172016-01-30 16:39:26 -0500966 struct rpc_xprt *xprt = task->tk_xprt;
Trond Myklebust58f96122010-07-31 14:29:08 -0400967
968 if (clnt != NULL) {
969 /* Remove from client task list */
970 spin_lock(&clnt->cl_lock);
971 list_del(&task->tk_task);
972 spin_unlock(&clnt->cl_lock);
973 task->tk_client = NULL;
974
975 rpc_release_client(clnt);
976 }
Trond Myklebustfb43d172016-01-30 16:39:26 -0500977
978 if (xprt != NULL) {
979 task->tk_xprt = NULL;
980
981 xprt_put(xprt);
982 }
Trond Myklebust58f96122010-07-31 14:29:08 -0400983}
984
985static
986void rpc_task_set_client(struct rpc_task *task, struct rpc_clnt *clnt)
987{
Trond Myklebustfb43d172016-01-30 16:39:26 -0500988
Trond Myklebust58f96122010-07-31 14:29:08 -0400989 if (clnt != NULL) {
Trond Myklebustfb43d172016-01-30 16:39:26 -0500990 if (task->tk_xprt == NULL)
991 task->tk_xprt = xprt_iter_get_next(&clnt->cl_xpi);
Trond Myklebust58f96122010-07-31 14:29:08 -0400992 task->tk_client = clnt;
Trond Myklebust006abe82010-09-12 19:55:25 -0400993 atomic_inc(&clnt->cl_count);
Trond Myklebust58f96122010-07-31 14:29:08 -0400994 if (clnt->cl_softrtry)
995 task->tk_flags |= RPC_TASK_SOFT;
Trond Myklebust8a19a0b2013-09-24 12:00:27 -0400996 if (clnt->cl_noretranstimeo)
997 task->tk_flags |= RPC_TASK_NO_RETRANS_TIMEOUT;
Jeff Layton3c87ef62015-06-03 16:14:25 -0400998 if (atomic_read(&clnt->cl_swapper))
999 task->tk_flags |= RPC_TASK_SWAPPER;
Trond Myklebust58f96122010-07-31 14:29:08 -04001000 /* Add to the client's list of all tasks */
1001 spin_lock(&clnt->cl_lock);
1002 list_add_tail(&task->tk_task, &clnt->cl_tasks);
1003 spin_unlock(&clnt->cl_lock);
1004 }
1005}
1006
1007static void
1008rpc_task_set_rpc_message(struct rpc_task *task, const struct rpc_message *msg)
1009{
1010 if (msg != NULL) {
1011 task->tk_msg.rpc_proc = msg->rpc_proc;
1012 task->tk_msg.rpc_argp = msg->rpc_argp;
1013 task->tk_msg.rpc_resp = msg->rpc_resp;
Trond Myklebusta17c2152010-07-31 14:29:08 -04001014 if (msg->rpc_cred != NULL)
1015 task->tk_msg.rpc_cred = get_rpccred(msg->rpc_cred);
Trond Myklebust58f96122010-07-31 14:29:08 -04001016 }
1017}
1018
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019/*
1020 * Default callback for async RPC calls
1021 */
1022static void
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001023rpc_default_callback(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024{
1025}
1026
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001027static const struct rpc_call_ops rpc_default_ops = {
1028 .rpc_call_done = rpc_default_callback,
1029};
1030
Trond Myklebustc970aa82007-07-14 15:39:59 -04001031/**
1032 * rpc_run_task - Allocate a new RPC task, then run rpc_execute against it
1033 * @task_setup_data: pointer to task initialisation data
1034 */
1035struct rpc_task *rpc_run_task(const struct rpc_task_setup *task_setup_data)
Trond Myklebust6e5b70e2007-06-12 10:02:37 -04001036{
Trond Myklebust19445b92010-04-16 16:41:10 -04001037 struct rpc_task *task;
Trond Myklebust6e5b70e2007-06-12 10:02:37 -04001038
Trond Myklebust84115e12007-07-14 15:39:59 -04001039 task = rpc_new_task(task_setup_data);
Trond Myklebust19445b92010-04-16 16:41:10 -04001040 if (IS_ERR(task))
Trond Myklebust50859252007-10-25 18:19:37 -04001041 goto out;
Trond Myklebust6e5b70e2007-06-12 10:02:37 -04001042
Trond Myklebust58f96122010-07-31 14:29:08 -04001043 rpc_task_set_client(task, task_setup_data->rpc_client);
1044 rpc_task_set_rpc_message(task, task_setup_data->rpc_message);
1045
Trond Myklebust58f96122010-07-31 14:29:08 -04001046 if (task->tk_action == NULL)
1047 rpc_call_start(task);
1048
Trond Myklebust6e5b70e2007-06-12 10:02:37 -04001049 atomic_inc(&task->tk_count);
1050 rpc_execute(task);
Trond Myklebust6e5b70e2007-06-12 10:02:37 -04001051out:
Trond Myklebust19445b92010-04-16 16:41:10 -04001052 return task;
Trond Myklebust6e5b70e2007-06-12 10:02:37 -04001053}
Trond Myklebustc970aa82007-07-14 15:39:59 -04001054EXPORT_SYMBOL_GPL(rpc_run_task);
Trond Myklebust6e5b70e2007-06-12 10:02:37 -04001055
1056/**
1057 * rpc_call_sync - Perform a synchronous RPC call
1058 * @clnt: pointer to RPC client
1059 * @msg: RPC call parameters
1060 * @flags: RPC call flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 */
Trond Myklebustcbc20052008-02-14 11:11:30 -05001062int rpc_call_sync(struct rpc_clnt *clnt, const struct rpc_message *msg, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063{
1064 struct rpc_task *task;
Trond Myklebust84115e12007-07-14 15:39:59 -04001065 struct rpc_task_setup task_setup_data = {
1066 .rpc_client = clnt,
1067 .rpc_message = msg,
1068 .callback_ops = &rpc_default_ops,
1069 .flags = flags,
1070 };
Trond Myklebust6e5b70e2007-06-12 10:02:37 -04001071 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
Weston Andros Adamson50d2bdb2012-11-01 16:04:40 -04001073 WARN_ON_ONCE(flags & RPC_TASK_ASYNC);
1074 if (flags & RPC_TASK_ASYNC) {
1075 rpc_release_calldata(task_setup_data.callback_ops,
1076 task_setup_data.callback_data);
1077 return -EINVAL;
1078 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
Trond Myklebustc970aa82007-07-14 15:39:59 -04001080 task = rpc_run_task(&task_setup_data);
Trond Myklebust6e5b70e2007-06-12 10:02:37 -04001081 if (IS_ERR(task))
1082 return PTR_ERR(task);
Trond Myklebuste60859a2006-01-03 09:55:10 +01001083 status = task->tk_status;
Trond Myklebustbde8f002007-01-24 11:54:53 -08001084 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 return status;
1086}
Trond Myklebuste8914c62007-07-14 15:39:59 -04001087EXPORT_SYMBOL_GPL(rpc_call_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
Trond Myklebust6e5b70e2007-06-12 10:02:37 -04001089/**
1090 * rpc_call_async - Perform an asynchronous RPC call
1091 * @clnt: pointer to RPC client
1092 * @msg: RPC call parameters
1093 * @flags: RPC call flags
Randy Dunlap65b6e422008-02-13 15:03:23 -08001094 * @tk_ops: RPC call ops
Trond Myklebust6e5b70e2007-06-12 10:02:37 -04001095 * @data: user call data
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 */
1097int
Trond Myklebustcbc20052008-02-14 11:11:30 -05001098rpc_call_async(struct rpc_clnt *clnt, const struct rpc_message *msg, int flags,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001099 const struct rpc_call_ops *tk_ops, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100{
1101 struct rpc_task *task;
Trond Myklebust84115e12007-07-14 15:39:59 -04001102 struct rpc_task_setup task_setup_data = {
1103 .rpc_client = clnt,
1104 .rpc_message = msg,
1105 .callback_ops = tk_ops,
1106 .callback_data = data,
1107 .flags = flags|RPC_TASK_ASYNC,
1108 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
Trond Myklebustc970aa82007-07-14 15:39:59 -04001110 task = rpc_run_task(&task_setup_data);
Trond Myklebust6e5b70e2007-06-12 10:02:37 -04001111 if (IS_ERR(task))
1112 return PTR_ERR(task);
1113 rpc_put_task(task);
1114 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115}
Trond Myklebuste8914c62007-07-14 15:39:59 -04001116EXPORT_SYMBOL_GPL(rpc_call_async);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001118#if defined(CONFIG_SUNRPC_BACKCHANNEL)
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001119/**
1120 * rpc_run_bc_task - Allocate a new RPC task for backchannel use, then run
1121 * rpc_execute against it
Jaswinder Singh Rajput7a73fdd2009-09-24 14:58:42 -04001122 * @req: RPC request
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001123 */
Trond Myklebust0f419792015-06-01 22:59:08 -04001124struct rpc_task *rpc_run_bc_task(struct rpc_rqst *req)
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001125{
1126 struct rpc_task *task;
1127 struct xdr_buf *xbufp = &req->rq_snd_buf;
1128 struct rpc_task_setup task_setup_data = {
Trond Myklebust0f419792015-06-01 22:59:08 -04001129 .callback_ops = &rpc_default_ops,
Trond Myklebust38325912015-06-19 13:04:13 -04001130 .flags = RPC_TASK_SOFTCONN,
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001131 };
1132
1133 dprintk("RPC: rpc_run_bc_task req= %p\n", req);
1134 /*
1135 * Create an rpc_task to send the data
1136 */
1137 task = rpc_new_task(&task_setup_data);
Trond Myklebust19445b92010-04-16 16:41:10 -04001138 if (IS_ERR(task)) {
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001139 xprt_free_bc_request(req);
1140 goto out;
1141 }
1142 task->tk_rqstp = req;
1143
1144 /*
1145 * Set up the xdr_buf length.
1146 * This also indicates that the buffer is XDR encoded already.
1147 */
1148 xbufp->len = xbufp->head[0].iov_len + xbufp->page_len +
1149 xbufp->tail[0].iov_len;
1150
1151 task->tk_action = call_bc_transmit;
1152 atomic_inc(&task->tk_count);
Weston Andros Adamson9a6478f2012-10-23 10:43:27 -04001153 WARN_ON_ONCE(atomic_read(&task->tk_count) != 2);
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001154 rpc_execute(task);
1155
1156out:
1157 dprintk("RPC: rpc_run_bc_task: task= %p\n", task);
1158 return task;
1159}
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001160#endif /* CONFIG_SUNRPC_BACKCHANNEL */
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001161
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162void
Trond Myklebust77de2c52007-10-25 18:40:21 -04001163rpc_call_start(struct rpc_task *task)
1164{
1165 task->tk_action = call_start;
1166}
1167EXPORT_SYMBOL_GPL(rpc_call_start);
1168
Chuck Levered394402006-08-22 20:06:17 -04001169/**
1170 * rpc_peeraddr - extract remote peer address from clnt's xprt
1171 * @clnt: RPC client structure
1172 * @buf: target buffer
Randy Dunlap65b6e422008-02-13 15:03:23 -08001173 * @bufsize: length of target buffer
Chuck Levered394402006-08-22 20:06:17 -04001174 *
1175 * Returns the number of bytes that are actually in the stored address.
1176 */
1177size_t rpc_peeraddr(struct rpc_clnt *clnt, struct sockaddr *buf, size_t bufsize)
1178{
1179 size_t bytes;
Trond Myklebust2446ab62012-03-01 17:00:56 -05001180 struct rpc_xprt *xprt;
Chuck Levered394402006-08-22 20:06:17 -04001181
Trond Myklebust2446ab62012-03-01 17:00:56 -05001182 rcu_read_lock();
1183 xprt = rcu_dereference(clnt->cl_xprt);
1184
1185 bytes = xprt->addrlen;
Chuck Levered394402006-08-22 20:06:17 -04001186 if (bytes > bufsize)
1187 bytes = bufsize;
Trond Myklebust2446ab62012-03-01 17:00:56 -05001188 memcpy(buf, &xprt->addr, bytes);
1189 rcu_read_unlock();
1190
1191 return bytes;
Chuck Levered394402006-08-22 20:06:17 -04001192}
Chuck Leverb86acd52006-08-22 20:06:22 -04001193EXPORT_SYMBOL_GPL(rpc_peeraddr);
Chuck Levered394402006-08-22 20:06:17 -04001194
Chuck Leverf425eba2006-08-22 20:06:18 -04001195/**
1196 * rpc_peeraddr2str - return remote peer address in printable format
1197 * @clnt: RPC client structure
1198 * @format: address format
1199 *
Trond Myklebust2446ab62012-03-01 17:00:56 -05001200 * NB: the lifetime of the memory referenced by the returned pointer is
1201 * the same as the rpc_xprt itself. As long as the caller uses this
1202 * pointer, it must hold the RCU read lock.
Chuck Leverf425eba2006-08-22 20:06:18 -04001203 */
Chuck Leverb454ae92008-01-07 18:34:48 -05001204const char *rpc_peeraddr2str(struct rpc_clnt *clnt,
1205 enum rpc_display_format_t format)
Chuck Leverf425eba2006-08-22 20:06:18 -04001206{
Trond Myklebust2446ab62012-03-01 17:00:56 -05001207 struct rpc_xprt *xprt;
1208
1209 xprt = rcu_dereference(clnt->cl_xprt);
Chuck Lever7559c7a2006-12-05 16:35:37 -05001210
1211 if (xprt->address_strings[format] != NULL)
1212 return xprt->address_strings[format];
1213 else
1214 return "unprintable";
Chuck Leverf425eba2006-08-22 20:06:18 -04001215}
Chuck Leverb86acd52006-08-22 20:06:22 -04001216EXPORT_SYMBOL_GPL(rpc_peeraddr2str);
Chuck Leverf425eba2006-08-22 20:06:18 -04001217
Chuck Lever2e738fd2012-03-01 17:01:14 -05001218static const struct sockaddr_in rpc_inaddr_loopback = {
1219 .sin_family = AF_INET,
1220 .sin_addr.s_addr = htonl(INADDR_ANY),
1221};
1222
1223static const struct sockaddr_in6 rpc_in6addr_loopback = {
1224 .sin6_family = AF_INET6,
1225 .sin6_addr = IN6ADDR_ANY_INIT,
1226};
1227
1228/*
1229 * Try a getsockname() on a connected datagram socket. Using a
1230 * connected datagram socket prevents leaving a socket in TIME_WAIT.
1231 * This conserves the ephemeral port number space.
1232 *
1233 * Returns zero and fills in "buf" if successful; otherwise, a
1234 * negative errno is returned.
1235 */
1236static int rpc_sockname(struct net *net, struct sockaddr *sap, size_t salen,
1237 struct sockaddr *buf, int buflen)
1238{
1239 struct socket *sock;
1240 int err;
1241
1242 err = __sock_create(net, sap->sa_family,
1243 SOCK_DGRAM, IPPROTO_UDP, &sock, 1);
1244 if (err < 0) {
1245 dprintk("RPC: can't create UDP socket (%d)\n", err);
1246 goto out;
1247 }
1248
1249 switch (sap->sa_family) {
1250 case AF_INET:
1251 err = kernel_bind(sock,
1252 (struct sockaddr *)&rpc_inaddr_loopback,
1253 sizeof(rpc_inaddr_loopback));
1254 break;
1255 case AF_INET6:
1256 err = kernel_bind(sock,
1257 (struct sockaddr *)&rpc_in6addr_loopback,
1258 sizeof(rpc_in6addr_loopback));
1259 break;
1260 default:
1261 err = -EAFNOSUPPORT;
1262 goto out;
1263 }
1264 if (err < 0) {
1265 dprintk("RPC: can't bind UDP socket (%d)\n", err);
1266 goto out_release;
1267 }
1268
1269 err = kernel_connect(sock, sap, salen, 0);
1270 if (err < 0) {
1271 dprintk("RPC: can't connect UDP socket (%d)\n", err);
1272 goto out_release;
1273 }
1274
1275 err = kernel_getsockname(sock, buf, &buflen);
1276 if (err < 0) {
1277 dprintk("RPC: getsockname failed (%d)\n", err);
1278 goto out_release;
1279 }
1280
1281 err = 0;
1282 if (buf->sa_family == AF_INET6) {
1283 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)buf;
1284 sin6->sin6_scope_id = 0;
1285 }
1286 dprintk("RPC: %s succeeded\n", __func__);
1287
1288out_release:
1289 sock_release(sock);
1290out:
1291 return err;
1292}
1293
1294/*
1295 * Scraping a connected socket failed, so we don't have a useable
1296 * local address. Fallback: generate an address that will prevent
1297 * the server from calling us back.
1298 *
1299 * Returns zero and fills in "buf" if successful; otherwise, a
1300 * negative errno is returned.
1301 */
1302static int rpc_anyaddr(int family, struct sockaddr *buf, size_t buflen)
1303{
1304 switch (family) {
1305 case AF_INET:
1306 if (buflen < sizeof(rpc_inaddr_loopback))
1307 return -EINVAL;
1308 memcpy(buf, &rpc_inaddr_loopback,
1309 sizeof(rpc_inaddr_loopback));
1310 break;
1311 case AF_INET6:
1312 if (buflen < sizeof(rpc_in6addr_loopback))
1313 return -EINVAL;
1314 memcpy(buf, &rpc_in6addr_loopback,
1315 sizeof(rpc_in6addr_loopback));
Trond Myklebust0b161e62015-12-30 18:14:06 -05001316 break;
Chuck Lever2e738fd2012-03-01 17:01:14 -05001317 default:
1318 dprintk("RPC: %s: address family not supported\n",
1319 __func__);
1320 return -EAFNOSUPPORT;
1321 }
1322 dprintk("RPC: %s: succeeded\n", __func__);
1323 return 0;
1324}
1325
1326/**
1327 * rpc_localaddr - discover local endpoint address for an RPC client
1328 * @clnt: RPC client structure
1329 * @buf: target buffer
1330 * @buflen: size of target buffer, in bytes
1331 *
1332 * Returns zero and fills in "buf" and "buflen" if successful;
1333 * otherwise, a negative errno is returned.
1334 *
1335 * This works even if the underlying transport is not currently connected,
1336 * or if the upper layer never previously provided a source address.
1337 *
1338 * The result of this function call is transient: multiple calls in
1339 * succession may give different results, depending on how local
1340 * networking configuration changes over time.
1341 */
1342int rpc_localaddr(struct rpc_clnt *clnt, struct sockaddr *buf, size_t buflen)
1343{
1344 struct sockaddr_storage address;
1345 struct sockaddr *sap = (struct sockaddr *)&address;
1346 struct rpc_xprt *xprt;
1347 struct net *net;
1348 size_t salen;
1349 int err;
1350
1351 rcu_read_lock();
1352 xprt = rcu_dereference(clnt->cl_xprt);
1353 salen = xprt->addrlen;
1354 memcpy(sap, &xprt->addr, salen);
1355 net = get_net(xprt->xprt_net);
1356 rcu_read_unlock();
1357
1358 rpc_set_port(sap, 0);
1359 err = rpc_sockname(net, sap, salen, buf, buflen);
1360 put_net(net);
1361 if (err != 0)
1362 /* Couldn't discover local address, return ANYADDR */
1363 return rpc_anyaddr(sap->sa_family, buf, buflen);
1364 return 0;
1365}
1366EXPORT_SYMBOL_GPL(rpc_localaddr);
1367
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368void
1369rpc_setbufsize(struct rpc_clnt *clnt, unsigned int sndsize, unsigned int rcvsize)
1370{
Trond Myklebust2446ab62012-03-01 17:00:56 -05001371 struct rpc_xprt *xprt;
1372
1373 rcu_read_lock();
1374 xprt = rcu_dereference(clnt->cl_xprt);
Chuck Lever470056c2005-08-25 16:25:56 -07001375 if (xprt->ops->set_buffer_size)
1376 xprt->ops->set_buffer_size(xprt, sndsize, rcvsize);
Trond Myklebust2446ab62012-03-01 17:00:56 -05001377 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378}
Trond Myklebuste8914c62007-07-14 15:39:59 -04001379EXPORT_SYMBOL_GPL(rpc_setbufsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
Trond Myklebust2446ab62012-03-01 17:00:56 -05001381/**
1382 * rpc_protocol - Get transport protocol number for an RPC client
1383 * @clnt: RPC client to query
1384 *
1385 */
1386int rpc_protocol(struct rpc_clnt *clnt)
1387{
1388 int protocol;
1389
1390 rcu_read_lock();
1391 protocol = rcu_dereference(clnt->cl_xprt)->prot;
1392 rcu_read_unlock();
1393 return protocol;
1394}
1395EXPORT_SYMBOL_GPL(rpc_protocol);
1396
1397/**
1398 * rpc_net_ns - Get the network namespace for this RPC client
1399 * @clnt: RPC client to query
1400 *
1401 */
1402struct net *rpc_net_ns(struct rpc_clnt *clnt)
1403{
1404 struct net *ret;
1405
1406 rcu_read_lock();
1407 ret = rcu_dereference(clnt->cl_xprt)->xprt_net;
1408 rcu_read_unlock();
1409 return ret;
1410}
1411EXPORT_SYMBOL_GPL(rpc_net_ns);
1412
1413/**
1414 * rpc_max_payload - Get maximum payload size for a transport, in bytes
1415 * @clnt: RPC client to query
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 *
1417 * For stream transports, this is one RPC record fragment (see RFC
1418 * 1831), as we don't support multi-record requests yet. For datagram
1419 * transports, this is the size of an IP packet minus the IP, UDP, and
1420 * RPC header sizes.
1421 */
1422size_t rpc_max_payload(struct rpc_clnt *clnt)
1423{
Trond Myklebust2446ab62012-03-01 17:00:56 -05001424 size_t ret;
1425
1426 rcu_read_lock();
1427 ret = rcu_dereference(clnt->cl_xprt)->max_payload;
1428 rcu_read_unlock();
1429 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430}
Chuck Leverb86acd52006-08-22 20:06:22 -04001431EXPORT_SYMBOL_GPL(rpc_max_payload);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
Chuck Lever35f5a422006-01-03 09:55:50 +01001433/**
Chuck Lever6b26cc82016-05-02 14:40:40 -04001434 * rpc_max_bc_payload - Get maximum backchannel payload size, in bytes
1435 * @clnt: RPC client to query
1436 */
1437size_t rpc_max_bc_payload(struct rpc_clnt *clnt)
1438{
1439 struct rpc_xprt *xprt;
1440 size_t ret;
1441
1442 rcu_read_lock();
1443 xprt = rcu_dereference(clnt->cl_xprt);
1444 ret = xprt->ops->bc_maxpayload(xprt);
1445 rcu_read_unlock();
1446 return ret;
1447}
1448EXPORT_SYMBOL_GPL(rpc_max_bc_payload);
1449
1450/**
Trond Myklebust512e4b22013-03-02 15:54:11 -08001451 * rpc_get_timeout - Get timeout for transport in units of HZ
Weston Andros Adamsonedddbb12013-02-28 20:30:09 -05001452 * @clnt: RPC client to query
1453 */
1454unsigned long rpc_get_timeout(struct rpc_clnt *clnt)
1455{
1456 unsigned long ret;
1457
1458 rcu_read_lock();
1459 ret = rcu_dereference(clnt->cl_xprt)->timeout->to_initval;
1460 rcu_read_unlock();
1461 return ret;
1462}
1463EXPORT_SYMBOL_GPL(rpc_get_timeout);
1464
1465/**
Chuck Lever35f5a422006-01-03 09:55:50 +01001466 * rpc_force_rebind - force transport to check that remote port is unchanged
1467 * @clnt: client to rebind
1468 *
1469 */
1470void rpc_force_rebind(struct rpc_clnt *clnt)
1471{
Trond Myklebust2446ab62012-03-01 17:00:56 -05001472 if (clnt->cl_autobind) {
1473 rcu_read_lock();
1474 xprt_clear_bound(rcu_dereference(clnt->cl_xprt));
1475 rcu_read_unlock();
1476 }
Chuck Lever35f5a422006-01-03 09:55:50 +01001477}
Chuck Leverb86acd52006-08-22 20:06:22 -04001478EXPORT_SYMBOL_GPL(rpc_force_rebind);
Chuck Lever35f5a422006-01-03 09:55:50 +01001479
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480/*
Andy Adamsonaae20062009-04-01 09:22:40 -04001481 * Restart an (async) RPC call from the call_prepare state.
1482 * Usually called from within the exit handler.
1483 */
Trond Myklebustf1f88fc2010-07-31 14:29:07 -04001484int
Andy Adamsonaae20062009-04-01 09:22:40 -04001485rpc_restart_call_prepare(struct rpc_task *task)
1486{
1487 if (RPC_ASSASSINATED(task))
Trond Myklebustf1f88fc2010-07-31 14:29:07 -04001488 return 0;
Trond Myklebustd00c5d42011-10-19 12:17:29 -07001489 task->tk_action = call_start;
Trond Myklebust494314c2014-03-20 12:59:09 -04001490 task->tk_status = 0;
Trond Myklebustd00c5d42011-10-19 12:17:29 -07001491 if (task->tk_ops->rpc_call_prepare != NULL)
1492 task->tk_action = rpc_prepare_task;
Trond Myklebustf1f88fc2010-07-31 14:29:07 -04001493 return 1;
Andy Adamsonaae20062009-04-01 09:22:40 -04001494}
1495EXPORT_SYMBOL_GPL(rpc_restart_call_prepare);
1496
1497/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 * Restart an (async) RPC call. Usually called from within the
1499 * exit handler.
1500 */
Trond Myklebustf1f88fc2010-07-31 14:29:07 -04001501int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502rpc_restart_call(struct rpc_task *task)
1503{
1504 if (RPC_ASSASSINATED(task))
Trond Myklebustf1f88fc2010-07-31 14:29:07 -04001505 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 task->tk_action = call_start;
Trond Myklebust494314c2014-03-20 12:59:09 -04001507 task->tk_status = 0;
Trond Myklebustf1f88fc2010-07-31 14:29:07 -04001508 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509}
Trond Myklebuste8914c62007-07-14 15:39:59 -04001510EXPORT_SYMBOL_GPL(rpc_restart_call);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
Jeff Laytonf895b252014-11-17 16:58:04 -05001512#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
Jeff Laytonb4b9d2c2014-11-26 14:44:43 -05001513const char
1514*rpc_proc_name(const struct rpc_task *task)
Chuck Lever3748f1e2008-05-21 17:09:12 -04001515{
1516 const struct rpc_procinfo *proc = task->tk_msg.rpc_proc;
1517
1518 if (proc) {
1519 if (proc->p_name)
1520 return proc->p_name;
1521 else
1522 return "NULL";
1523 } else
1524 return "no proc";
1525}
1526#endif
1527
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528/*
1529 * 0. Initial state
1530 *
1531 * Other FSM states can be visited zero or more times, but
1532 * this state is visited exactly once for each RPC.
1533 */
1534static void
1535call_start(struct rpc_task *task)
1536{
1537 struct rpc_clnt *clnt = task->tk_client;
1538
Chuck Lever3748f1e2008-05-21 17:09:12 -04001539 dprintk("RPC: %5u call_start %s%d proc %s (%s)\n", task->tk_pid,
Trond Myklebust55909f22013-08-23 11:48:15 -04001540 clnt->cl_program->name, clnt->cl_vers,
Chuck Lever3748f1e2008-05-21 17:09:12 -04001541 rpc_proc_name(task),
Chuck Lever46121cf2007-01-31 12:14:08 -05001542 (RPC_IS_ASYNC(task) ? "async" : "sync"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
1544 /* Increment call count */
1545 task->tk_msg.rpc_proc->p_count++;
1546 clnt->cl_stats->rpccnt++;
1547 task->tk_action = call_reserve;
1548}
1549
1550/*
1551 * 1. Reserve an RPC call slot
1552 */
1553static void
1554call_reserve(struct rpc_task *task)
1555{
Chuck Lever46121cf2007-01-31 12:14:08 -05001556 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 task->tk_status = 0;
1559 task->tk_action = call_reserveresult;
1560 xprt_reserve(task);
1561}
1562
Trond Myklebustba60eb22013-04-14 10:49:37 -04001563static void call_retry_reserve(struct rpc_task *task);
1564
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565/*
1566 * 1b. Grok the result of xprt_reserve()
1567 */
1568static void
1569call_reserveresult(struct rpc_task *task)
1570{
1571 int status = task->tk_status;
1572
Chuck Lever46121cf2007-01-31 12:14:08 -05001573 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574
1575 /*
1576 * After a call to xprt_reserve(), we must have either
1577 * a request slot or else an error status.
1578 */
1579 task->tk_status = 0;
1580 if (status >= 0) {
1581 if (task->tk_rqstp) {
J. Bruce Fieldsf2d47d02010-09-12 19:55:25 -04001582 task->tk_action = call_refresh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 return;
1584 }
1585
1586 printk(KERN_ERR "%s: status=%d, but no request slot, exiting\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08001587 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 rpc_exit(task, -EIO);
1589 return;
1590 }
1591
1592 /*
1593 * Even though there was an error, we may have acquired
1594 * a request slot somehow. Make sure not to leak it.
1595 */
1596 if (task->tk_rqstp) {
1597 printk(KERN_ERR "%s: status=%d, request allocated anyway\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08001598 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 xprt_release(task);
1600 }
1601
1602 switch (status) {
Trond Myklebust1afeaf52012-05-19 12:12:53 -04001603 case -ENOMEM:
1604 rpc_delay(task, HZ >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 case -EAGAIN: /* woken up; retry */
Trond Myklebustba60eb22013-04-14 10:49:37 -04001606 task->tk_action = call_retry_reserve;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 return;
1608 case -EIO: /* probably a shutdown */
1609 break;
1610 default:
1611 printk(KERN_ERR "%s: unrecognized error %d, exiting\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08001612 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 break;
1614 }
1615 rpc_exit(task, status);
1616}
1617
1618/*
Trond Myklebustba60eb22013-04-14 10:49:37 -04001619 * 1c. Retry reserving an RPC call slot
1620 */
1621static void
1622call_retry_reserve(struct rpc_task *task)
1623{
1624 dprint_status(task);
1625
1626 task->tk_status = 0;
1627 task->tk_action = call_reserveresult;
1628 xprt_retry_reserve(task);
1629}
1630
1631/*
J. Bruce Fields55576242010-09-12 19:55:25 -04001632 * 2. Bind and/or refresh the credentials
1633 */
1634static void
1635call_refresh(struct rpc_task *task)
1636{
1637 dprint_status(task);
1638
1639 task->tk_action = call_refreshresult;
1640 task->tk_status = 0;
1641 task->tk_client->cl_stats->rpcauthrefresh++;
1642 rpcauth_refreshcred(task);
1643}
1644
1645/*
1646 * 2a. Process the results of a credential refresh
1647 */
1648static void
1649call_refreshresult(struct rpc_task *task)
1650{
1651 int status = task->tk_status;
1652
1653 dprint_status(task);
1654
1655 task->tk_status = 0;
Trond Myklebust5fc43972010-11-20 11:13:31 -05001656 task->tk_action = call_refresh;
J. Bruce Fields55576242010-09-12 19:55:25 -04001657 switch (status) {
Trond Myklebust5fc43972010-11-20 11:13:31 -05001658 case 0:
Weston Andros Adamson6ff33b72013-12-17 12:16:11 -05001659 if (rpcauth_uptodatecred(task)) {
Trond Myklebust5fc43972010-11-20 11:13:31 -05001660 task->tk_action = call_allocate;
Weston Andros Adamson6ff33b72013-12-17 12:16:11 -05001661 return;
1662 }
1663 /* Use rate-limiting and a max number of retries if refresh
1664 * had status 0 but failed to update the cred.
1665 */
J. Bruce Fields55576242010-09-12 19:55:25 -04001666 case -ETIMEDOUT:
1667 rpc_delay(task, 3*HZ);
Trond Myklebust5fc43972010-11-20 11:13:31 -05001668 case -EAGAIN:
1669 status = -EACCES;
Andy Adamsonf1ff0c22013-08-14 11:59:13 -04001670 case -EKEYEXPIRED:
Trond Myklebust5fc43972010-11-20 11:13:31 -05001671 if (!task->tk_cred_retry)
1672 break;
1673 task->tk_cred_retry--;
1674 dprintk("RPC: %5u %s: retry refresh creds\n",
1675 task->tk_pid, __func__);
1676 return;
J. Bruce Fields55576242010-09-12 19:55:25 -04001677 }
Trond Myklebust5fc43972010-11-20 11:13:31 -05001678 dprintk("RPC: %5u %s: refresh creds failed with error %d\n",
1679 task->tk_pid, __func__, status);
1680 rpc_exit(task, status);
J. Bruce Fields55576242010-09-12 19:55:25 -04001681}
1682
1683/*
1684 * 2b. Allocate the buffer. For details, see sched.c:rpc_malloc.
Chuck Lever02107142006-01-03 09:55:49 +01001685 * (Note: buffer memory is freed in xprt_release).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 */
1687static void
1688call_allocate(struct rpc_task *task)
1689{
J. Bruce Fieldsf2d47d02010-09-12 19:55:25 -04001690 unsigned int slack = task->tk_rqstp->rq_cred->cr_auth->au_cslack;
Chuck Lever02107142006-01-03 09:55:49 +01001691 struct rpc_rqst *req = task->tk_rqstp;
Trond Myklebusta4f08352013-01-08 09:10:21 -05001692 struct rpc_xprt *xprt = req->rq_xprt;
Chuck Lever2bea90d2007-03-29 16:47:53 -04001693 struct rpc_procinfo *proc = task->tk_msg.rpc_proc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694
Chuck Lever46121cf2007-01-31 12:14:08 -05001695 dprint_status(task);
1696
Chuck Lever2bea90d2007-03-29 16:47:53 -04001697 task->tk_status = 0;
J. Bruce Fieldsf2d47d02010-09-12 19:55:25 -04001698 task->tk_action = call_bind;
Chuck Lever2bea90d2007-03-29 16:47:53 -04001699
Chuck Lever02107142006-01-03 09:55:49 +01001700 if (req->rq_buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 return;
1702
Chuck Lever2bea90d2007-03-29 16:47:53 -04001703 if (proc->p_proc != 0) {
1704 BUG_ON(proc->p_arglen == 0);
1705 if (proc->p_decode != NULL)
1706 BUG_ON(proc->p_replen == 0);
1707 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708
Chuck Lever2bea90d2007-03-29 16:47:53 -04001709 /*
1710 * Calculate the size (in quads) of the RPC call
1711 * and reply headers, and convert both values
1712 * to byte sizes.
1713 */
1714 req->rq_callsize = RPC_CALLHDRSIZE + (slack << 1) + proc->p_arglen;
1715 req->rq_callsize <<= 2;
1716 req->rq_rcvsize = RPC_REPHDRSIZE + slack + proc->p_replen;
1717 req->rq_rcvsize <<= 2;
1718
Chuck Leverc5a4dd82007-03-29 16:47:58 -04001719 req->rq_buffer = xprt->ops->buf_alloc(task,
1720 req->rq_callsize + req->rq_rcvsize);
Chuck Lever2bea90d2007-03-29 16:47:53 -04001721 if (req->rq_buffer != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 return;
Chuck Lever4a068252015-05-11 14:02:25 -04001723 xprt_inject_disconnect(xprt);
Chuck Lever46121cf2007-01-31 12:14:08 -05001724
1725 dprintk("RPC: %5u rpc_buffer allocation failed\n", task->tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726
Trond Myklebust5afa9132011-06-17 10:14:59 -04001727 if (RPC_IS_ASYNC(task) || !fatal_signal_pending(current)) {
Trond Myklebustb6e9c712007-10-01 12:06:44 -04001728 task->tk_action = call_allocate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 rpc_delay(task, HZ>>4);
1730 return;
1731 }
1732
1733 rpc_exit(task, -ERESTARTSYS);
1734}
1735
Trond Myklebust940e3312005-11-09 21:45:24 -05001736static inline int
1737rpc_task_need_encode(struct rpc_task *task)
1738{
1739 return task->tk_rqstp->rq_snd_buf.len == 0;
1740}
1741
1742static inline void
1743rpc_task_force_reencode(struct rpc_task *task)
1744{
1745 task->tk_rqstp->rq_snd_buf.len = 0;
Trond Myklebust2574cc92009-08-28 11:12:12 -04001746 task->tk_rqstp->rq_bytes_sent = 0;
Trond Myklebust940e3312005-11-09 21:45:24 -05001747}
1748
Chuck Lever2bea90d2007-03-29 16:47:53 -04001749static inline void
1750rpc_xdr_buf_init(struct xdr_buf *buf, void *start, size_t len)
1751{
1752 buf->head[0].iov_base = start;
1753 buf->head[0].iov_len = len;
1754 buf->tail[0].iov_len = 0;
1755 buf->page_len = 0;
\"Talpey, Thomas\4f22ccc2007-09-10 13:44:58 -04001756 buf->flags = 0;
Chuck Lever2bea90d2007-03-29 16:47:53 -04001757 buf->len = 0;
1758 buf->buflen = len;
1759}
1760
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761/*
1762 * 3. Encode arguments of an RPC call
1763 */
1764static void
Chuck Leverb0e1c572008-05-21 17:09:19 -04001765rpc_xdr_encode(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 struct rpc_rqst *req = task->tk_rqstp;
Chuck Lever9f06c712010-12-14 14:59:18 +00001768 kxdreproc_t encode;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001769 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770
Chuck Lever46121cf2007-01-31 12:14:08 -05001771 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772
Chuck Lever2bea90d2007-03-29 16:47:53 -04001773 rpc_xdr_buf_init(&req->rq_snd_buf,
1774 req->rq_buffer,
1775 req->rq_callsize);
1776 rpc_xdr_buf_init(&req->rq_rcv_buf,
1777 (char *)req->rq_buffer + req->rq_callsize,
1778 req->rq_rcvsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779
Chuck Leverb0e1c572008-05-21 17:09:19 -04001780 p = rpc_encode_header(task);
1781 if (p == NULL) {
1782 printk(KERN_INFO "RPC: couldn't encode RPC header, exit EIO\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 rpc_exit(task, -EIO);
1784 return;
1785 }
Chuck Leverb0e1c572008-05-21 17:09:19 -04001786
1787 encode = task->tk_msg.rpc_proc->p_encode;
J. Bruce Fieldsf3680312005-10-13 16:54:48 -04001788 if (encode == NULL)
1789 return;
1790
1791 task->tk_status = rpcauth_wrap_req(task, encode, req, p,
1792 task->tk_msg.rpc_argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793}
1794
1795/*
1796 * 4. Get the server port number if not yet set
1797 */
1798static void
1799call_bind(struct rpc_task *task)
1800{
Trond Myklebustad2368d2013-01-08 10:08:33 -05001801 struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802
Chuck Lever46121cf2007-01-31 12:14:08 -05001803 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804
Chuck Leverda351872005-08-11 16:25:11 -04001805 task->tk_action = call_connect;
Chuck Leverec739ef2006-08-22 20:06:15 -04001806 if (!xprt_bound(xprt)) {
Chuck Leverda351872005-08-11 16:25:11 -04001807 task->tk_action = call_bind_status;
Chuck Leverec739ef2006-08-22 20:06:15 -04001808 task->tk_timeout = xprt->bind_timeout;
Chuck Leverbbf7c1d2006-08-22 20:06:16 -04001809 xprt->ops->rpcbind(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 }
1811}
1812
1813/*
Chuck Leverda351872005-08-11 16:25:11 -04001814 * 4a. Sort out bind result
1815 */
1816static void
1817call_bind_status(struct rpc_task *task)
1818{
Chuck Lever906462a2007-09-11 18:00:47 -04001819 int status = -EIO;
Chuck Leverda351872005-08-11 16:25:11 -04001820
1821 if (task->tk_status >= 0) {
Chuck Lever46121cf2007-01-31 12:14:08 -05001822 dprint_status(task);
Chuck Leverda351872005-08-11 16:25:11 -04001823 task->tk_status = 0;
1824 task->tk_action = call_connect;
1825 return;
1826 }
1827
Steve Dickson5753cba2012-02-06 10:08:08 -05001828 trace_rpc_bind_status(task);
Chuck Leverda351872005-08-11 16:25:11 -04001829 switch (task->tk_status) {
Trond Myklebust381ba742008-07-07 12:18:53 -04001830 case -ENOMEM:
1831 dprintk("RPC: %5u rpcbind out of memory\n", task->tk_pid);
1832 rpc_delay(task, HZ >> 2);
Chuck Lever2429cbf2007-09-11 18:00:41 -04001833 goto retry_timeout;
Chuck Leverda351872005-08-11 16:25:11 -04001834 case -EACCES:
Chuck Lever46121cf2007-01-31 12:14:08 -05001835 dprintk("RPC: %5u remote rpcbind: RPC program/version "
1836 "unavailable\n", task->tk_pid);
Chuck Leverb79dc8c2007-09-11 18:00:52 -04001837 /* fail immediately if this is an RPC ping */
1838 if (task->tk_msg.rpc_proc->p_proc == 0) {
1839 status = -EOPNOTSUPP;
1840 break;
1841 }
Trond Myklebust0b760112011-05-31 15:15:34 -04001842 if (task->tk_rebind_retry == 0)
1843 break;
1844 task->tk_rebind_retry--;
Chuck Leverea635a52005-10-06 23:12:58 -04001845 rpc_delay(task, 3*HZ);
Trond Myklebustda458282006-08-31 15:44:52 -04001846 goto retry_timeout;
Chuck Leverda351872005-08-11 16:25:11 -04001847 case -ETIMEDOUT:
Chuck Lever46121cf2007-01-31 12:14:08 -05001848 dprintk("RPC: %5u rpcbind request timed out\n",
Chuck Leverda351872005-08-11 16:25:11 -04001849 task->tk_pid);
Trond Myklebustda458282006-08-31 15:44:52 -04001850 goto retry_timeout;
Chuck Leverda351872005-08-11 16:25:11 -04001851 case -EPFNOSUPPORT:
Chuck Lever906462a2007-09-11 18:00:47 -04001852 /* server doesn't support any rpcbind version we know of */
Chuck Lever012da152009-12-03 15:58:56 -05001853 dprintk("RPC: %5u unrecognized remote rpcbind service\n",
Chuck Leverda351872005-08-11 16:25:11 -04001854 task->tk_pid);
1855 break;
1856 case -EPROTONOSUPPORT:
Chuck Lever00a6e7b2007-03-29 16:48:33 -04001857 dprintk("RPC: %5u remote rpcbind version unavailable, retrying\n",
Chuck Leverda351872005-08-11 16:25:11 -04001858 task->tk_pid);
Trond Myklebustfdb63dc2014-03-17 12:57:31 -04001859 goto retry_timeout;
Chuck Lever012da152009-12-03 15:58:56 -05001860 case -ECONNREFUSED: /* connection problems */
1861 case -ECONNRESET:
Trond Myklebustdf277272013-12-31 13:11:43 -05001862 case -ECONNABORTED:
Chuck Lever012da152009-12-03 15:58:56 -05001863 case -ENOTCONN:
1864 case -EHOSTDOWN:
1865 case -EHOSTUNREACH:
1866 case -ENETUNREACH:
Trond Myklebust3601c4a2014-06-30 13:42:19 -04001867 case -ENOBUFS:
Chuck Lever012da152009-12-03 15:58:56 -05001868 case -EPIPE:
1869 dprintk("RPC: %5u remote rpcbind unreachable: %d\n",
1870 task->tk_pid, task->tk_status);
1871 if (!RPC_IS_SOFTCONN(task)) {
1872 rpc_delay(task, 5*HZ);
1873 goto retry_timeout;
1874 }
1875 status = task->tk_status;
1876 break;
Chuck Leverda351872005-08-11 16:25:11 -04001877 default:
Chuck Lever46121cf2007-01-31 12:14:08 -05001878 dprintk("RPC: %5u unrecognized rpcbind error (%d)\n",
Chuck Leverda351872005-08-11 16:25:11 -04001879 task->tk_pid, -task->tk_status);
Chuck Leverda351872005-08-11 16:25:11 -04001880 }
1881
1882 rpc_exit(task, status);
1883 return;
1884
Trond Myklebustda458282006-08-31 15:44:52 -04001885retry_timeout:
Trond Myklebustfdb63dc2014-03-17 12:57:31 -04001886 task->tk_status = 0;
Trond Myklebustda458282006-08-31 15:44:52 -04001887 task->tk_action = call_timeout;
Chuck Leverda351872005-08-11 16:25:11 -04001888}
1889
1890/*
1891 * 4b. Connect to the RPC server
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 */
1893static void
1894call_connect(struct rpc_task *task)
1895{
Trond Myklebustad2368d2013-01-08 10:08:33 -05001896 struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897
Chuck Lever46121cf2007-01-31 12:14:08 -05001898 dprintk("RPC: %5u call_connect xprt %p %s connected\n",
Chuck Leverda351872005-08-11 16:25:11 -04001899 task->tk_pid, xprt,
1900 (xprt_connected(xprt) ? "is" : "is not"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901
Chuck Leverda351872005-08-11 16:25:11 -04001902 task->tk_action = call_transmit;
1903 if (!xprt_connected(xprt)) {
1904 task->tk_action = call_connect_status;
1905 if (task->tk_status < 0)
1906 return;
Trond Myklebust786615b2013-08-05 16:04:47 -04001907 if (task->tk_flags & RPC_TASK_NOCONNECT) {
1908 rpc_exit(task, -ENOTCONN);
1909 return;
1910 }
Chuck Leverda351872005-08-11 16:25:11 -04001911 xprt_connect(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913}
1914
1915/*
Chuck Leverda351872005-08-11 16:25:11 -04001916 * 4c. Sort out connect result
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 */
1918static void
1919call_connect_status(struct rpc_task *task)
1920{
1921 struct rpc_clnt *clnt = task->tk_client;
1922 int status = task->tk_status;
1923
Chuck Lever46121cf2007-01-31 12:14:08 -05001924 dprint_status(task);
Chuck Leverda351872005-08-11 16:25:11 -04001925
Steve Dickson5753cba2012-02-06 10:08:08 -05001926 trace_rpc_connect_status(task, status);
Trond Myklebust561ec162013-09-26 15:22:45 -04001927 task->tk_status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 switch (status) {
Trond Myklebust3ed5e2a2013-03-04 17:29:33 -05001929 case -ECONNREFUSED:
1930 case -ECONNRESET:
Trond Myklebustdf277272013-12-31 13:11:43 -05001931 case -ECONNABORTED:
Trond Myklebust3ed5e2a2013-03-04 17:29:33 -05001932 case -ENETUNREACH:
Trond Myklebustdf277272013-12-31 13:11:43 -05001933 case -EHOSTUNREACH:
Trond Myklebust3913c782015-02-08 21:44:04 -05001934 case -EADDRINUSE:
Trond Myklebust3601c4a2014-06-30 13:42:19 -04001935 case -ENOBUFS:
Trond Myklebust2fc193c2014-07-03 00:02:57 -04001936 case -EPIPE:
Trond Myklebust3ed5e2a2013-03-04 17:29:33 -05001937 if (RPC_IS_SOFTCONN(task))
1938 break;
Steve Dickson1fa3e2e2014-03-20 11:23:03 -04001939 /* retry with existing socket, after a delay */
1940 rpc_delay(task, 3*HZ);
Trond Myklebust3ed5e2a2013-03-04 17:29:33 -05001941 case -EAGAIN:
Trond Myklebust485f2252014-03-17 12:51:44 -04001942 /* Check for timeouts before looping back to call_bind */
1943 case -ETIMEDOUT:
1944 task->tk_action = call_timeout;
Trond Myklebust561ec162013-09-26 15:22:45 -04001945 return;
1946 case 0:
Trond Myklebust3ed5e2a2013-03-04 17:29:33 -05001947 clnt->cl_stats->netreconn++;
1948 task->tk_action = call_transmit;
1949 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 }
Trond Myklebust3ed5e2a2013-03-04 17:29:33 -05001951 rpc_exit(task, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952}
1953
1954/*
1955 * 5. Transmit the RPC request, and wait for reply
1956 */
1957static void
1958call_transmit(struct rpc_task *task)
1959{
Trond Myklebustca7f33a2013-09-25 13:39:45 -04001960 int is_retrans = RPC_WAS_SENT(task);
1961
Chuck Lever46121cf2007-01-31 12:14:08 -05001962 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963
1964 task->tk_action = call_status;
1965 if (task->tk_status < 0)
1966 return;
Trond Myklebust90051ea2013-09-25 12:17:18 -04001967 if (!xprt_prepare_transmit(task))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 return;
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04001969 task->tk_action = call_transmit_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 /* Encode here so that rpcsec_gss can use correct sequence number. */
Trond Myklebust940e3312005-11-09 21:45:24 -05001971 if (rpc_task_need_encode(task)) {
Chuck Leverb0e1c572008-05-21 17:09:19 -04001972 rpc_xdr_encode(task);
Trond Myklebust5e5ce5b2005-10-18 14:20:11 -07001973 /* Did the encode result in an error condition? */
Trond Myklebust8b39f2b2008-05-14 19:48:25 -07001974 if (task->tk_status != 0) {
1975 /* Was the error nonfatal? */
1976 if (task->tk_status == -EAGAIN)
1977 rpc_delay(task, HZ >> 4);
1978 else
1979 rpc_exit(task, task->tk_status);
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04001980 return;
Trond Myklebust8b39f2b2008-05-14 19:48:25 -07001981 }
Trond Myklebust5e5ce5b2005-10-18 14:20:11 -07001982 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 xprt_transmit(task);
1984 if (task->tk_status < 0)
1985 return;
Trond Myklebustca7f33a2013-09-25 13:39:45 -04001986 if (is_retrans)
1987 task->tk_client->cl_stats->rpcretrans++;
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04001988 /*
1989 * On success, ensure that we call xprt_end_transmit() before sleeping
1990 * in order to allow access to the socket to other RPC requests.
1991 */
1992 call_transmit_status(task);
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001993 if (rpc_reply_expected(task))
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04001994 return;
1995 task->tk_action = rpc_exit_task;
Trond Myklebusta4f08352013-01-08 09:10:21 -05001996 rpc_wake_up_queued_task(&task->tk_rqstp->rq_xprt->pending, task);
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04001997}
1998
1999/*
2000 * 5a. Handle cleanup after a transmission
2001 */
2002static void
2003call_transmit_status(struct rpc_task *task)
2004{
2005 task->tk_action = call_status;
Chuck Lever206a1342009-12-03 15:58:56 -05002006
2007 /*
2008 * Common case: success. Force the compiler to put this
2009 * test first.
2010 */
2011 if (task->tk_status == 0) {
2012 xprt_end_transmit(task);
2013 rpc_task_force_reencode(task);
2014 return;
2015 }
2016
Trond Myklebust15f081c2009-03-11 14:37:57 -04002017 switch (task->tk_status) {
2018 case -EAGAIN:
Trond Myklebust93aa6c72015-07-03 09:28:09 -04002019 case -ENOBUFS:
Trond Myklebust15f081c2009-03-11 14:37:57 -04002020 break;
2021 default:
Chuck Lever206a1342009-12-03 15:58:56 -05002022 dprint_status(task);
Trond Myklebust15f081c2009-03-11 14:37:57 -04002023 xprt_end_transmit(task);
Chuck Lever09a21c42009-12-03 15:58:56 -05002024 rpc_task_force_reencode(task);
2025 break;
Trond Myklebust15f081c2009-03-11 14:37:57 -04002026 /*
2027 * Special cases: if we've been waiting on the
2028 * socket's write_space() callback, or if the
2029 * socket just returned a connection error,
2030 * then hold onto the transport lock.
2031 */
2032 case -ECONNREFUSED:
Trond Myklebust15f081c2009-03-11 14:37:57 -04002033 case -EHOSTDOWN:
2034 case -EHOSTUNREACH:
2035 case -ENETUNREACH:
Jason Baron3dedbb52014-09-24 18:08:04 +00002036 case -EPERM:
Chuck Lever09a21c42009-12-03 15:58:56 -05002037 if (RPC_IS_SOFTCONN(task)) {
2038 xprt_end_transmit(task);
2039 rpc_exit(task, task->tk_status);
2040 break;
2041 }
2042 case -ECONNRESET:
Trond Myklebustdf277272013-12-31 13:11:43 -05002043 case -ECONNABORTED:
Trond Myklebust3913c782015-02-08 21:44:04 -05002044 case -EADDRINUSE:
Chuck Lever09a21c42009-12-03 15:58:56 -05002045 case -ENOTCONN:
Trond Myklebustc8485e42009-03-11 14:37:59 -04002046 case -EPIPE:
Trond Myklebust15f081c2009-03-11 14:37:57 -04002047 rpc_task_force_reencode(task);
2048 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049}
2050
Trond Myklebust9e00abc2011-07-13 19:20:49 -04002051#if defined(CONFIG_SUNRPC_BACKCHANNEL)
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04002052/*
2053 * 5b. Send the backchannel RPC reply. On error, drop the reply. In
2054 * addition, disconnect on connectivity errors.
2055 */
2056static void
2057call_bc_transmit(struct rpc_task *task)
2058{
2059 struct rpc_rqst *req = task->tk_rqstp;
2060
Trond Myklebust1193d582015-06-02 11:53:21 -04002061 if (!xprt_prepare_transmit(task))
2062 goto out_retry;
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04002063
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04002064 if (task->tk_status < 0) {
2065 printk(KERN_NOTICE "RPC: Could not send backchannel reply "
2066 "error: %d\n", task->tk_status);
Trond Myklebust1193d582015-06-02 11:53:21 -04002067 goto out_done;
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04002068 }
Trond Myklebust1193d582015-06-02 11:53:21 -04002069 if (req->rq_connect_cookie != req->rq_xprt->connect_cookie)
2070 req->rq_bytes_sent = 0;
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04002071
2072 xprt_transmit(task);
Trond Myklebust1193d582015-06-02 11:53:21 -04002073
2074 if (task->tk_status == -EAGAIN)
2075 goto out_nospace;
2076
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04002077 xprt_end_transmit(task);
2078 dprint_status(task);
2079 switch (task->tk_status) {
2080 case 0:
2081 /* Success */
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04002082 case -EHOSTDOWN:
2083 case -EHOSTUNREACH:
2084 case -ENETUNREACH:
Trond Myklebust38325912015-06-19 13:04:13 -04002085 case -ECONNRESET:
2086 case -ECONNREFUSED:
2087 case -EADDRINUSE:
2088 case -ENOTCONN:
2089 case -EPIPE:
2090 break;
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04002091 case -ETIMEDOUT:
2092 /*
2093 * Problem reaching the server. Disconnect and let the
2094 * forechannel reestablish the connection. The server will
2095 * have to retransmit the backchannel request and we'll
2096 * reprocess it. Since these ops are idempotent, there's no
2097 * need to cache our reply at this time.
2098 */
2099 printk(KERN_NOTICE "RPC: Could not send backchannel reply "
2100 "error: %d\n", task->tk_status);
Trond Myklebusta4f08352013-01-08 09:10:21 -05002101 xprt_conditional_disconnect(req->rq_xprt,
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04002102 req->rq_connect_cookie);
2103 break;
2104 default:
2105 /*
2106 * We were unable to reply and will have to drop the
2107 * request. The server should reconnect and retransmit.
2108 */
Weston Andros Adamson1facf4c2012-10-23 10:43:30 -04002109 WARN_ON_ONCE(task->tk_status == -EAGAIN);
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04002110 printk(KERN_NOTICE "RPC: Could not send backchannel reply "
2111 "error: %d\n", task->tk_status);
2112 break;
2113 }
2114 rpc_wake_up_queued_task(&req->rq_xprt->pending, task);
Trond Myklebust1193d582015-06-02 11:53:21 -04002115out_done:
2116 task->tk_action = rpc_exit_task;
2117 return;
2118out_nospace:
2119 req->rq_connect_cookie = req->rq_xprt->connect_cookie;
2120out_retry:
2121 task->tk_status = 0;
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04002122}
Trond Myklebust9e00abc2011-07-13 19:20:49 -04002123#endif /* CONFIG_SUNRPC_BACKCHANNEL */
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04002124
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125/*
2126 * 6. Sort out the RPC call status
2127 */
2128static void
2129call_status(struct rpc_task *task)
2130{
2131 struct rpc_clnt *clnt = task->tk_client;
2132 struct rpc_rqst *req = task->tk_rqstp;
2133 int status;
2134
Ricardo Labiagadd2b63d2009-04-01 09:23:28 -04002135 if (req->rq_reply_bytes_recvd > 0 && !req->rq_bytes_sent)
2136 task->tk_status = req->rq_reply_bytes_recvd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137
Chuck Lever46121cf2007-01-31 12:14:08 -05002138 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139
2140 status = task->tk_status;
2141 if (status >= 0) {
2142 task->tk_action = call_decode;
2143 return;
2144 }
2145
Steve Dickson5753cba2012-02-06 10:08:08 -05002146 trace_rpc_call_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 task->tk_status = 0;
2148 switch(status) {
Trond Myklebust76303992006-08-30 14:32:49 -04002149 case -EHOSTDOWN:
2150 case -EHOSTUNREACH:
2151 case -ENETUNREACH:
Jason Baron3dedbb52014-09-24 18:08:04 +00002152 case -EPERM:
Trond Myklebust9455e3f2014-03-19 13:25:43 -04002153 if (RPC_IS_SOFTCONN(task)) {
2154 rpc_exit(task, status);
2155 break;
2156 }
Trond Myklebust76303992006-08-30 14:32:49 -04002157 /*
2158 * Delay any retries for 3 seconds, then handle as if it
2159 * were a timeout.
2160 */
2161 rpc_delay(task, 3*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 case -ETIMEDOUT:
2163 task->tk_action = call_timeout;
Trond Myklebust8a19a0b2013-09-24 12:00:27 -04002164 if (!(task->tk_flags & RPC_TASK_NO_RETRANS_TIMEOUT)
2165 && task->tk_client->cl_discrtry)
Trond Myklebusta4f08352013-01-08 09:10:21 -05002166 xprt_conditional_disconnect(req->rq_xprt,
Trond Myklebust7c1d71c2008-04-17 16:52:57 -04002167 req->rq_connect_cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 break;
2169 case -ECONNREFUSED:
Trond Myklebustdf277272013-12-31 13:11:43 -05002170 case -ECONNRESET:
2171 case -ECONNABORTED:
Chuck Lever35f5a422006-01-03 09:55:50 +01002172 rpc_force_rebind(clnt);
Trond Myklebust3913c782015-02-08 21:44:04 -05002173 case -EADDRINUSE:
Trond Myklebustc8485e42009-03-11 14:37:59 -04002174 rpc_delay(task, 3*HZ);
2175 case -EPIPE:
2176 case -ENOTCONN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 task->tk_action = call_bind;
2178 break;
Trond Myklebust93aa6c72015-07-03 09:28:09 -04002179 case -ENOBUFS:
2180 rpc_delay(task, HZ>>2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 case -EAGAIN:
2182 task->tk_action = call_transmit;
2183 break;
2184 case -EIO:
2185 /* shutdown or soft timeout */
2186 rpc_exit(task, status);
2187 break;
2188 default:
Olga Kornievskaiab6b61522008-06-09 16:51:31 -04002189 if (clnt->cl_chatty)
2190 printk("%s: RPC call returned error %d\n",
Trond Myklebust55909f22013-08-23 11:48:15 -04002191 clnt->cl_program->name, -status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 rpc_exit(task, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 }
2194}
2195
2196/*
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04002197 * 6a. Handle RPC timeout
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 * We do not release the request slot, so we keep using the
2199 * same XID for all retransmits.
2200 */
2201static void
2202call_timeout(struct rpc_task *task)
2203{
2204 struct rpc_clnt *clnt = task->tk_client;
2205
2206 if (xprt_adjust_timeout(task->tk_rqstp) == 0) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002207 dprintk("RPC: %5u call_timeout (minor)\n", task->tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 goto retry;
2209 }
2210
Chuck Lever46121cf2007-01-31 12:14:08 -05002211 dprintk("RPC: %5u call_timeout (major)\n", task->tk_pid);
Chuck Leveref759a22006-03-20 13:44:17 -05002212 task->tk_timeouts++;
2213
Chuck Lever3a28bec2009-12-03 15:58:56 -05002214 if (RPC_IS_SOFTCONN(task)) {
2215 rpc_exit(task, -ETIMEDOUT);
2216 return;
2217 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 if (RPC_IS_SOFT(task)) {
Joe Perchescac5d072012-07-18 11:17:11 -07002219 if (clnt->cl_chatty) {
Olga Kornievskaiab6b61522008-06-09 16:51:31 -04002220 printk(KERN_NOTICE "%s: server %s not responding, timed out\n",
Trond Myklebust55909f22013-08-23 11:48:15 -04002221 clnt->cl_program->name,
Trond Myklebustfb43d172016-01-30 16:39:26 -05002222 task->tk_xprt->servername);
Joe Perchescac5d072012-07-18 11:17:11 -07002223 }
Trond Myklebust7494d002011-04-24 14:28:45 -04002224 if (task->tk_flags & RPC_TASK_TIMEOUT)
2225 rpc_exit(task, -ETIMEDOUT);
2226 else
2227 rpc_exit(task, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 return;
2229 }
2230
Chuck Leverf518e35a2006-01-03 09:55:52 +01002231 if (!(task->tk_flags & RPC_CALL_MAJORSEEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 task->tk_flags |= RPC_CALL_MAJORSEEN;
Trond Myklebust4e0038b2012-03-01 17:01:05 -05002233 if (clnt->cl_chatty) {
Olga Kornievskaiab6b61522008-06-09 16:51:31 -04002234 printk(KERN_NOTICE "%s: server %s not responding, still trying\n",
Trond Myklebust55909f22013-08-23 11:48:15 -04002235 clnt->cl_program->name,
Trond Myklebustfb43d172016-01-30 16:39:26 -05002236 task->tk_xprt->servername);
Trond Myklebust4e0038b2012-03-01 17:01:05 -05002237 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 }
Chuck Lever35f5a422006-01-03 09:55:50 +01002239 rpc_force_rebind(clnt);
Trond Myklebustb48633b2008-04-22 16:47:55 -04002240 /*
2241 * Did our request time out due to an RPCSEC_GSS out-of-sequence
2242 * event? RFC2203 requires the server to drop all such requests.
2243 */
2244 rpcauth_invalcred(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245
2246retry:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 task->tk_action = call_bind;
2248 task->tk_status = 0;
2249}
2250
2251/*
2252 * 7. Decode the RPC reply
2253 */
2254static void
2255call_decode(struct rpc_task *task)
2256{
2257 struct rpc_clnt *clnt = task->tk_client;
2258 struct rpc_rqst *req = task->tk_rqstp;
Chuck Leverbf269552010-12-14 14:59:29 +00002259 kxdrdproc_t decode = task->tk_msg.rpc_proc->p_decode;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07002260 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261
Vasily Averin726fd6a2011-06-01 16:23:59 +04002262 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263
Chuck Leverf518e35a2006-01-03 09:55:52 +01002264 if (task->tk_flags & RPC_CALL_MAJORSEEN) {
Trond Myklebust4e0038b2012-03-01 17:01:05 -05002265 if (clnt->cl_chatty) {
Olga Kornievskaiab6b61522008-06-09 16:51:31 -04002266 printk(KERN_NOTICE "%s: server %s OK\n",
Trond Myklebust55909f22013-08-23 11:48:15 -04002267 clnt->cl_program->name,
Trond Myklebustfb43d172016-01-30 16:39:26 -05002268 task->tk_xprt->servername);
Trond Myklebust4e0038b2012-03-01 17:01:05 -05002269 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 task->tk_flags &= ~RPC_CALL_MAJORSEEN;
2271 }
2272
Trond Myklebust43ac3f22006-03-20 13:44:51 -05002273 /*
2274 * Ensure that we see all writes made by xprt_complete_rqst()
Ricardo Labiagadd2b63d2009-04-01 09:23:28 -04002275 * before it changed req->rq_reply_bytes_recvd.
Trond Myklebust43ac3f22006-03-20 13:44:51 -05002276 */
2277 smp_rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 req->rq_rcv_buf.len = req->rq_private_buf.len;
2279
2280 /* Check that the softirq receive buffer is valid */
2281 WARN_ON(memcmp(&req->rq_rcv_buf, &req->rq_private_buf,
2282 sizeof(req->rq_rcv_buf)) != 0);
2283
Trond Myklebust1e799b62008-03-21 16:19:41 -04002284 if (req->rq_rcv_buf.len < 12) {
2285 if (!RPC_IS_SOFT(task)) {
2286 task->tk_action = call_bind;
Trond Myklebust1e799b62008-03-21 16:19:41 -04002287 goto out_retry;
2288 }
2289 dprintk("RPC: %s: too small RPC reply size (%d bytes)\n",
Trond Myklebust55909f22013-08-23 11:48:15 -04002290 clnt->cl_program->name, task->tk_status);
Trond Myklebust1e799b62008-03-21 16:19:41 -04002291 task->tk_action = call_timeout;
2292 goto out_retry;
2293 }
2294
Chuck Leverb0e1c572008-05-21 17:09:19 -04002295 p = rpc_verify_header(task);
Trond Myklebustabbcf282006-01-03 09:55:03 +01002296 if (IS_ERR(p)) {
2297 if (p == ERR_PTR(-EAGAIN))
2298 goto out_retry;
2299 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 }
2301
Trond Myklebustabbcf282006-01-03 09:55:03 +01002302 task->tk_action = rpc_exit_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303
Trond Myklebust6d5fcb52006-10-18 16:01:06 -04002304 if (decode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 task->tk_status = rpcauth_unwrap_resp(task, decode, req, p,
2306 task->tk_msg.rpc_resp);
Trond Myklebust6d5fcb52006-10-18 16:01:06 -04002307 }
Chuck Lever46121cf2007-01-31 12:14:08 -05002308 dprintk("RPC: %5u call_decode result %d\n", task->tk_pid,
2309 task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 return;
2311out_retry:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 task->tk_status = 0;
Chuck Leverb0e1c572008-05-21 17:09:19 -04002313 /* Note: rpc_verify_header() may have freed the RPC slot */
Trond Myklebust24b74bf2008-04-19 13:15:47 -04002314 if (task->tk_rqstp == req) {
Ricardo Labiagadd2b63d2009-04-01 09:23:28 -04002315 req->rq_reply_bytes_recvd = req->rq_rcv_buf.len = 0;
Trond Myklebust24b74bf2008-04-19 13:15:47 -04002316 if (task->tk_client->cl_discrtry)
Trond Myklebusta4f08352013-01-08 09:10:21 -05002317 xprt_conditional_disconnect(req->rq_xprt,
Trond Myklebust7c1d71c2008-04-17 16:52:57 -04002318 req->rq_connect_cookie);
Trond Myklebust24b74bf2008-04-19 13:15:47 -04002319 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320}
2321
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07002322static __be32 *
Chuck Leverb0e1c572008-05-21 17:09:19 -04002323rpc_encode_header(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324{
2325 struct rpc_clnt *clnt = task->tk_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 struct rpc_rqst *req = task->tk_rqstp;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07002327 __be32 *p = req->rq_svec[0].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328
2329 /* FIXME: check buffer size? */
Chuck Lever808012f2005-08-25 16:25:49 -07002330
Trond Myklebusta4f08352013-01-08 09:10:21 -05002331 p = xprt_skip_transport_header(req->rq_xprt, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 *p++ = req->rq_xid; /* XID */
2333 *p++ = htonl(RPC_CALL); /* CALL */
2334 *p++ = htonl(RPC_VERSION); /* RPC version */
2335 *p++ = htonl(clnt->cl_prog); /* program number */
2336 *p++ = htonl(clnt->cl_vers); /* program version */
2337 *p++ = htonl(task->tk_msg.rpc_proc->p_proc); /* procedure */
Trond Myklebust334ccfd2005-06-22 17:16:19 +00002338 p = rpcauth_marshcred(task, p);
2339 req->rq_slen = xdr_adjust_iovec(&req->rq_svec[0], p);
2340 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341}
2342
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07002343static __be32 *
Chuck Leverb0e1c572008-05-21 17:09:19 -04002344rpc_verify_header(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345{
Trond Myklebust4e0038b2012-03-01 17:01:05 -05002346 struct rpc_clnt *clnt = task->tk_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 struct kvec *iov = &task->tk_rqstp->rq_rcv_buf.head[0];
2348 int len = task->tk_rqstp->rq_rcv_buf.len >> 2;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07002349 __be32 *p = iov->iov_base;
2350 u32 n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 int error = -EACCES;
2352
David Howellse8896492006-08-24 15:44:19 -04002353 if ((task->tk_rqstp->rq_rcv_buf.len & 3) != 0) {
2354 /* RFC-1014 says that the representation of XDR data must be a
2355 * multiple of four bytes
2356 * - if it isn't pointer subtraction in the NFS client may give
2357 * undefined results
2358 */
Trond Myklebust8a702bb2007-06-27 18:30:26 -04002359 dprintk("RPC: %5u %s: XDR representation not a multiple of"
Harvey Harrison0dc47872008-03-05 20:47:47 -08002360 " 4 bytes: 0x%x\n", task->tk_pid, __func__,
Trond Myklebust8a702bb2007-06-27 18:30:26 -04002361 task->tk_rqstp->rq_rcv_buf.len);
Andy Adamson35fa5f72013-08-14 11:59:17 -04002362 error = -EIO;
2363 goto out_err;
David Howellse8896492006-08-24 15:44:19 -04002364 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 if ((len -= 3) < 0)
2366 goto out_overflow;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04002368 p += 1; /* skip XID */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 if ((n = ntohl(*p++)) != RPC_REPLY) {
Trond Myklebust8a702bb2007-06-27 18:30:26 -04002370 dprintk("RPC: %5u %s: not an RPC reply: %x\n",
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04002371 task->tk_pid, __func__, n);
Andy Adamson35fa5f72013-08-14 11:59:17 -04002372 error = -EIO;
Trond Myklebustabbcf282006-01-03 09:55:03 +01002373 goto out_garbage;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 }
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04002375
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 if ((n = ntohl(*p++)) != RPC_MSG_ACCEPTED) {
2377 if (--len < 0)
2378 goto out_overflow;
2379 switch ((n = ntohl(*p++))) {
Joe Perches89f0e4f2011-07-01 09:43:12 +00002380 case RPC_AUTH_ERROR:
2381 break;
2382 case RPC_MISMATCH:
2383 dprintk("RPC: %5u %s: RPC call version mismatch!\n",
2384 task->tk_pid, __func__);
2385 error = -EPROTONOSUPPORT;
2386 goto out_err;
2387 default:
2388 dprintk("RPC: %5u %s: RPC call rejected, "
2389 "unknown error: %x\n",
2390 task->tk_pid, __func__, n);
Andy Adamson35fa5f72013-08-14 11:59:17 -04002391 error = -EIO;
2392 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 }
2394 if (--len < 0)
2395 goto out_overflow;
2396 switch ((n = ntohl(*p++))) {
2397 case RPC_AUTH_REJECTEDCRED:
2398 case RPC_AUTH_REJECTEDVERF:
2399 case RPCSEC_GSS_CREDPROBLEM:
2400 case RPCSEC_GSS_CTXPROBLEM:
2401 if (!task->tk_cred_retry)
2402 break;
2403 task->tk_cred_retry--;
Chuck Lever46121cf2007-01-31 12:14:08 -05002404 dprintk("RPC: %5u %s: retry stale creds\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08002405 task->tk_pid, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 rpcauth_invalcred(task);
Trond Myklebust220bcc22007-10-01 12:06:48 -04002407 /* Ensure we obtain a new XID! */
2408 xprt_release(task);
Trond Myklebust118df3d2010-10-24 17:17:31 -04002409 task->tk_action = call_reserve;
Trond Myklebustabbcf282006-01-03 09:55:03 +01002410 goto out_retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 case RPC_AUTH_BADCRED:
2412 case RPC_AUTH_BADVERF:
2413 /* possibly garbled cred/verf? */
2414 if (!task->tk_garb_retry)
2415 break;
2416 task->tk_garb_retry--;
Chuck Lever46121cf2007-01-31 12:14:08 -05002417 dprintk("RPC: %5u %s: retry garbled creds\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08002418 task->tk_pid, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 task->tk_action = call_bind;
Trond Myklebustabbcf282006-01-03 09:55:03 +01002420 goto out_retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 case RPC_AUTH_TOOWEAK:
Chuck Leverb0e1c572008-05-21 17:09:19 -04002422 printk(KERN_NOTICE "RPC: server %s requires stronger "
Trond Myklebust4e0038b2012-03-01 17:01:05 -05002423 "authentication.\n",
Trond Myklebustfb43d172016-01-30 16:39:26 -05002424 task->tk_xprt->servername);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 break;
2426 default:
Trond Myklebust8a702bb2007-06-27 18:30:26 -04002427 dprintk("RPC: %5u %s: unknown auth error: %x\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08002428 task->tk_pid, __func__, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 error = -EIO;
2430 }
Chuck Lever46121cf2007-01-31 12:14:08 -05002431 dprintk("RPC: %5u %s: call rejected %d\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08002432 task->tk_pid, __func__, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 goto out_err;
2434 }
Andy Adamson35fa5f72013-08-14 11:59:17 -04002435 p = rpcauth_checkverf(task, p);
2436 if (IS_ERR(p)) {
2437 error = PTR_ERR(p);
2438 dprintk("RPC: %5u %s: auth check failed with %d\n",
2439 task->tk_pid, __func__, error);
Trond Myklebustabbcf282006-01-03 09:55:03 +01002440 goto out_garbage; /* bad verifier, retry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 }
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07002442 len = p - (__be32 *)iov->iov_base - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 if (len < 0)
2444 goto out_overflow;
2445 switch ((n = ntohl(*p++))) {
2446 case RPC_SUCCESS:
2447 return p;
2448 case RPC_PROG_UNAVAIL:
Trond Myklebustfb43d172016-01-30 16:39:26 -05002449 dprintk("RPC: %5u %s: program %u is unsupported "
Trond Myklebust4e0038b2012-03-01 17:01:05 -05002450 "by server %s\n", task->tk_pid, __func__,
2451 (unsigned int)clnt->cl_prog,
Trond Myklebustfb43d172016-01-30 16:39:26 -05002452 task->tk_xprt->servername);
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00002453 error = -EPFNOSUPPORT;
2454 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 case RPC_PROG_MISMATCH:
Trond Myklebustfb43d172016-01-30 16:39:26 -05002456 dprintk("RPC: %5u %s: program %u, version %u unsupported "
Trond Myklebust4e0038b2012-03-01 17:01:05 -05002457 "by server %s\n", task->tk_pid, __func__,
2458 (unsigned int)clnt->cl_prog,
2459 (unsigned int)clnt->cl_vers,
Trond Myklebustfb43d172016-01-30 16:39:26 -05002460 task->tk_xprt->servername);
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00002461 error = -EPROTONOSUPPORT;
2462 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 case RPC_PROC_UNAVAIL:
Trond Myklebustfb43d172016-01-30 16:39:26 -05002464 dprintk("RPC: %5u %s: proc %s unsupported by program %u, "
Chuck Lever46121cf2007-01-31 12:14:08 -05002465 "version %u on server %s\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08002466 task->tk_pid, __func__,
Chuck Lever3748f1e2008-05-21 17:09:12 -04002467 rpc_proc_name(task),
Trond Myklebust4e0038b2012-03-01 17:01:05 -05002468 clnt->cl_prog, clnt->cl_vers,
Trond Myklebustfb43d172016-01-30 16:39:26 -05002469 task->tk_xprt->servername);
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00002470 error = -EOPNOTSUPP;
2471 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 case RPC_GARBAGE_ARGS:
Chuck Lever46121cf2007-01-31 12:14:08 -05002473 dprintk("RPC: %5u %s: server saw garbage\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08002474 task->tk_pid, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 break; /* retry */
2476 default:
Trond Myklebust8a702bb2007-06-27 18:30:26 -04002477 dprintk("RPC: %5u %s: server accept status: %x\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08002478 task->tk_pid, __func__, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 /* Also retry */
2480 }
2481
Trond Myklebustabbcf282006-01-03 09:55:03 +01002482out_garbage:
Trond Myklebust4e0038b2012-03-01 17:01:05 -05002483 clnt->cl_stats->rpcgarbage++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 if (task->tk_garb_retry) {
2485 task->tk_garb_retry--;
Chuck Lever46121cf2007-01-31 12:14:08 -05002486 dprintk("RPC: %5u %s: retrying\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08002487 task->tk_pid, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 task->tk_action = call_bind;
Trond Myklebustabbcf282006-01-03 09:55:03 +01002489out_retry:
2490 return ERR_PTR(-EAGAIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492out_err:
2493 rpc_exit(task, error);
Trond Myklebust8a702bb2007-06-27 18:30:26 -04002494 dprintk("RPC: %5u %s: call failed with error %d\n", task->tk_pid,
Harvey Harrison0dc47872008-03-05 20:47:47 -08002495 __func__, error);
Trond Myklebustabbcf282006-01-03 09:55:03 +01002496 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497out_overflow:
Trond Myklebust8a702bb2007-06-27 18:30:26 -04002498 dprintk("RPC: %5u %s: server reply was truncated.\n", task->tk_pid,
Harvey Harrison0dc47872008-03-05 20:47:47 -08002499 __func__);
Trond Myklebustabbcf282006-01-03 09:55:03 +01002500 goto out_garbage;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501}
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00002502
Chuck Lever9f06c712010-12-14 14:59:18 +00002503static void rpcproc_encode_null(void *rqstp, struct xdr_stream *xdr, void *obj)
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00002504{
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00002505}
2506
Chuck Leverbf269552010-12-14 14:59:29 +00002507static int rpcproc_decode_null(void *rqstp, struct xdr_stream *xdr, void *obj)
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00002508{
2509 return 0;
2510}
2511
2512static struct rpc_procinfo rpcproc_null = {
2513 .p_encode = rpcproc_encode_null,
2514 .p_decode = rpcproc_decode_null,
2515};
2516
Chuck Levercaabea82009-12-03 15:58:56 -05002517static int rpc_ping(struct rpc_clnt *clnt)
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00002518{
2519 struct rpc_message msg = {
2520 .rpc_proc = &rpcproc_null,
2521 };
2522 int err;
2523 msg.rpc_cred = authnull_ops.lookup_cred(NULL, NULL, 0);
Chuck Levercaabea82009-12-03 15:58:56 -05002524 err = rpc_call_sync(clnt, &msg, RPC_TASK_SOFT | RPC_TASK_SOFTCONN);
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00002525 put_rpccred(msg.rpc_cred);
2526 return err;
2527}
Trond Myklebust188fef12007-06-16 14:18:40 -04002528
Trond Myklebust7f554892016-01-30 23:43:35 -05002529static
2530struct rpc_task *rpc_call_null_helper(struct rpc_clnt *clnt,
2531 struct rpc_xprt *xprt, struct rpc_cred *cred, int flags,
2532 const struct rpc_call_ops *ops, void *data)
Trond Myklebust5e1550d2007-06-23 10:17:16 -04002533{
2534 struct rpc_message msg = {
2535 .rpc_proc = &rpcproc_null,
2536 .rpc_cred = cred,
2537 };
Trond Myklebust84115e12007-07-14 15:39:59 -04002538 struct rpc_task_setup task_setup_data = {
2539 .rpc_client = clnt,
Trond Myklebust7f554892016-01-30 23:43:35 -05002540 .rpc_xprt = xprt,
Trond Myklebust84115e12007-07-14 15:39:59 -04002541 .rpc_message = &msg,
Trond Myklebust7f554892016-01-30 23:43:35 -05002542 .callback_ops = (ops != NULL) ? ops : &rpc_default_ops,
2543 .callback_data = data,
Trond Myklebust84115e12007-07-14 15:39:59 -04002544 .flags = flags,
2545 };
Trond Myklebust7f554892016-01-30 23:43:35 -05002546
Trond Myklebustc970aa82007-07-14 15:39:59 -04002547 return rpc_run_task(&task_setup_data);
Trond Myklebust5e1550d2007-06-23 10:17:16 -04002548}
Trond Myklebust7f554892016-01-30 23:43:35 -05002549
2550struct rpc_task *rpc_call_null(struct rpc_clnt *clnt, struct rpc_cred *cred, int flags)
2551{
2552 return rpc_call_null_helper(clnt, NULL, cred, flags, NULL, NULL);
2553}
Trond Myklebuste8914c62007-07-14 15:39:59 -04002554EXPORT_SYMBOL_GPL(rpc_call_null);
Trond Myklebust5e1550d2007-06-23 10:17:16 -04002555
Trond Myklebust7f554892016-01-30 23:43:35 -05002556struct rpc_cb_add_xprt_calldata {
2557 struct rpc_xprt_switch *xps;
2558 struct rpc_xprt *xprt;
2559};
2560
2561static void rpc_cb_add_xprt_done(struct rpc_task *task, void *calldata)
2562{
2563 struct rpc_cb_add_xprt_calldata *data = calldata;
2564
2565 if (task->tk_status == 0)
2566 rpc_xprt_switch_add_xprt(data->xps, data->xprt);
2567}
2568
2569static void rpc_cb_add_xprt_release(void *calldata)
2570{
2571 struct rpc_cb_add_xprt_calldata *data = calldata;
2572
2573 xprt_put(data->xprt);
2574 xprt_switch_put(data->xps);
2575 kfree(data);
2576}
2577
Trond Myklebustce272302016-07-24 17:06:28 -04002578static const struct rpc_call_ops rpc_cb_add_xprt_call_ops = {
Trond Myklebust7f554892016-01-30 23:43:35 -05002579 .rpc_call_done = rpc_cb_add_xprt_done,
2580 .rpc_release = rpc_cb_add_xprt_release,
2581};
2582
2583/**
2584 * rpc_clnt_test_and_add_xprt - Test and add a new transport to a rpc_clnt
2585 * @clnt: pointer to struct rpc_clnt
2586 * @xps: pointer to struct rpc_xprt_switch,
2587 * @xprt: pointer struct rpc_xprt
2588 * @dummy: unused
2589 */
2590int rpc_clnt_test_and_add_xprt(struct rpc_clnt *clnt,
2591 struct rpc_xprt_switch *xps, struct rpc_xprt *xprt,
2592 void *dummy)
2593{
2594 struct rpc_cb_add_xprt_calldata *data;
2595 struct rpc_cred *cred;
2596 struct rpc_task *task;
2597
2598 data = kmalloc(sizeof(*data), GFP_NOFS);
2599 if (!data)
2600 return -ENOMEM;
2601 data->xps = xprt_switch_get(xps);
2602 data->xprt = xprt_get(xprt);
2603
2604 cred = authnull_ops.lookup_cred(NULL, NULL, 0);
2605 task = rpc_call_null_helper(clnt, xprt, cred,
2606 RPC_TASK_SOFT|RPC_TASK_SOFTCONN|RPC_TASK_ASYNC,
2607 &rpc_cb_add_xprt_call_ops, data);
2608 put_rpccred(cred);
2609 if (IS_ERR(task))
2610 return PTR_ERR(task);
2611 rpc_put_task(task);
2612 return 1;
2613}
2614EXPORT_SYMBOL_GPL(rpc_clnt_test_and_add_xprt);
2615
2616/**
2617 * rpc_clnt_add_xprt - Add a new transport to a rpc_clnt
2618 * @clnt: pointer to struct rpc_clnt
2619 * @xprtargs: pointer to struct xprt_create
2620 * @setup: callback to test and/or set up the connection
2621 * @data: pointer to setup function data
2622 *
2623 * Creates a new transport using the parameters set in args and
2624 * adds it to clnt.
2625 * If ping is set, then test that connectivity succeeds before
2626 * adding the new transport.
2627 *
2628 */
2629int rpc_clnt_add_xprt(struct rpc_clnt *clnt,
2630 struct xprt_create *xprtargs,
2631 int (*setup)(struct rpc_clnt *,
2632 struct rpc_xprt_switch *,
2633 struct rpc_xprt *,
2634 void *),
2635 void *data)
2636{
2637 struct rpc_xprt_switch *xps;
2638 struct rpc_xprt *xprt;
Trond Myklebust3851f1c2016-08-04 00:08:45 -04002639 unsigned long reconnect_timeout;
Trond Myklebust7f554892016-01-30 23:43:35 -05002640 unsigned char resvport;
2641 int ret = 0;
2642
2643 rcu_read_lock();
2644 xps = xprt_switch_get(rcu_dereference(clnt->cl_xpi.xpi_xpswitch));
2645 xprt = xprt_iter_xprt(&clnt->cl_xpi);
2646 if (xps == NULL || xprt == NULL) {
2647 rcu_read_unlock();
2648 return -EAGAIN;
2649 }
2650 resvport = xprt->resvport;
Trond Myklebust3851f1c2016-08-04 00:08:45 -04002651 reconnect_timeout = xprt->max_reconnect_timeout;
Trond Myklebust7f554892016-01-30 23:43:35 -05002652 rcu_read_unlock();
2653
2654 xprt = xprt_create_transport(xprtargs);
2655 if (IS_ERR(xprt)) {
2656 ret = PTR_ERR(xprt);
2657 goto out_put_switch;
2658 }
2659 xprt->resvport = resvport;
Trond Myklebust3851f1c2016-08-04 00:08:45 -04002660 xprt->max_reconnect_timeout = reconnect_timeout;
Trond Myklebust7f554892016-01-30 23:43:35 -05002661
2662 rpc_xprt_switch_set_roundrobin(xps);
2663 if (setup) {
2664 ret = setup(clnt, xps, xprt, data);
2665 if (ret != 0)
2666 goto out_put_xprt;
2667 }
2668 rpc_xprt_switch_add_xprt(xps, xprt);
2669out_put_xprt:
2670 xprt_put(xprt);
2671out_put_switch:
2672 xprt_switch_put(xps);
2673 return ret;
2674}
2675EXPORT_SYMBOL_GPL(rpc_clnt_add_xprt);
2676
Trond Myklebust8d480322016-08-05 19:03:31 -04002677static int
2678rpc_xprt_cap_max_reconnect_timeout(struct rpc_clnt *clnt,
2679 struct rpc_xprt *xprt,
2680 void *data)
2681{
2682 unsigned long timeout = *((unsigned long *)data);
2683
2684 if (timeout < xprt->max_reconnect_timeout)
2685 xprt->max_reconnect_timeout = timeout;
2686 return 0;
2687}
2688
2689void
2690rpc_cap_max_reconnect_timeout(struct rpc_clnt *clnt, unsigned long timeo)
2691{
2692 rpc_clnt_iterate_for_each_xprt(clnt,
2693 rpc_xprt_cap_max_reconnect_timeout,
2694 &timeo);
2695}
2696EXPORT_SYMBOL_GPL(rpc_cap_max_reconnect_timeout);
2697
Andy Adamson3b58a8a2016-09-09 09:22:23 -04002698void rpc_clnt_xprt_switch_put(struct rpc_clnt *clnt)
2699{
2700 xprt_switch_put(rcu_dereference(clnt->cl_xpi.xpi_xpswitch));
2701}
2702EXPORT_SYMBOL_GPL(rpc_clnt_xprt_switch_put);
2703
Andy Adamsondd691712016-09-09 09:22:24 -04002704void rpc_clnt_xprt_switch_add_xprt(struct rpc_clnt *clnt, struct rpc_xprt *xprt)
2705{
2706 rpc_xprt_switch_add_xprt(rcu_dereference(clnt->cl_xpi.xpi_xpswitch),
2707 xprt);
2708}
2709EXPORT_SYMBOL_GPL(rpc_clnt_xprt_switch_add_xprt);
2710
Andy Adamson39e5d2d2016-09-09 09:22:25 -04002711bool rpc_clnt_xprt_switch_has_addr(struct rpc_clnt *clnt,
2712 const struct sockaddr *sap)
2713{
2714 struct rpc_xprt_switch *xps;
2715 bool ret;
2716
2717 xps = rcu_dereference(clnt->cl_xpi.xpi_xpswitch);
2718
2719 rcu_read_lock();
2720 ret = rpc_xprt_switch_has_addr(xps, sap);
2721 rcu_read_unlock();
2722 return ret;
2723}
2724EXPORT_SYMBOL_GPL(rpc_clnt_xprt_switch_has_addr);
2725
Jeff Laytonf895b252014-11-17 16:58:04 -05002726#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
Chuck Lever68a23ee2008-05-21 17:09:26 -04002727static void rpc_show_header(void)
2728{
Chuck Levercb3997b2008-05-21 17:09:41 -04002729 printk(KERN_INFO "-pid- flgs status -client- --rqstp- "
2730 "-timeout ---ops--\n");
Chuck Lever68a23ee2008-05-21 17:09:26 -04002731}
2732
Chuck Lever38e886e2008-05-21 17:09:33 -04002733static void rpc_show_task(const struct rpc_clnt *clnt,
2734 const struct rpc_task *task)
2735{
2736 const char *rpc_waitq = "none";
Chuck Lever38e886e2008-05-21 17:09:33 -04002737
2738 if (RPC_IS_QUEUED(task))
2739 rpc_waitq = rpc_qname(task->tk_waitqueue);
2740
Joe Perchesb3bceda2010-12-21 10:52:24 -05002741 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 -04002742 task->tk_pid, task->tk_flags, task->tk_status,
2743 clnt, task->tk_rqstp, task->tk_timeout, task->tk_ops,
Trond Myklebust55909f22013-08-23 11:48:15 -04002744 clnt->cl_program->name, clnt->cl_vers, rpc_proc_name(task),
Joe Perchesb3bceda2010-12-21 10:52:24 -05002745 task->tk_action, rpc_waitq);
Chuck Lever38e886e2008-05-21 17:09:33 -04002746}
2747
Stanislav Kinsbursky70abc492012-01-12 22:07:51 +04002748void rpc_show_tasks(struct net *net)
Trond Myklebust188fef12007-06-16 14:18:40 -04002749{
2750 struct rpc_clnt *clnt;
Chuck Lever38e886e2008-05-21 17:09:33 -04002751 struct rpc_task *task;
Chuck Lever68a23ee2008-05-21 17:09:26 -04002752 int header = 0;
Stanislav Kinsbursky70abc492012-01-12 22:07:51 +04002753 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
Trond Myklebust188fef12007-06-16 14:18:40 -04002754
Stanislav Kinsbursky70abc492012-01-12 22:07:51 +04002755 spin_lock(&sn->rpc_client_lock);
2756 list_for_each_entry(clnt, &sn->all_clients, cl_clients) {
Trond Myklebust188fef12007-06-16 14:18:40 -04002757 spin_lock(&clnt->cl_lock);
Chuck Lever38e886e2008-05-21 17:09:33 -04002758 list_for_each_entry(task, &clnt->cl_tasks, tk_task) {
Chuck Lever68a23ee2008-05-21 17:09:26 -04002759 if (!header) {
2760 rpc_show_header();
2761 header++;
2762 }
Chuck Lever38e886e2008-05-21 17:09:33 -04002763 rpc_show_task(clnt, task);
Trond Myklebust188fef12007-06-16 14:18:40 -04002764 }
2765 spin_unlock(&clnt->cl_lock);
2766 }
Stanislav Kinsbursky70abc492012-01-12 22:07:51 +04002767 spin_unlock(&sn->rpc_client_lock);
Trond Myklebust188fef12007-06-16 14:18:40 -04002768}
2769#endif
Jeff Layton3c87ef62015-06-03 16:14:25 -04002770
2771#if IS_ENABLED(CONFIG_SUNRPC_SWAP)
Trond Myklebust15001e52016-01-30 20:05:34 -05002772static int
2773rpc_clnt_swap_activate_callback(struct rpc_clnt *clnt,
2774 struct rpc_xprt *xprt,
2775 void *dummy)
2776{
2777 return xprt_enable_swap(xprt);
2778}
2779
Jeff Layton3c87ef62015-06-03 16:14:25 -04002780int
2781rpc_clnt_swap_activate(struct rpc_clnt *clnt)
2782{
Trond Myklebust15001e52016-01-30 20:05:34 -05002783 if (atomic_inc_return(&clnt->cl_swapper) == 1)
2784 return rpc_clnt_iterate_for_each_xprt(clnt,
2785 rpc_clnt_swap_activate_callback, NULL);
2786 return 0;
Jeff Layton3c87ef62015-06-03 16:14:25 -04002787}
2788EXPORT_SYMBOL_GPL(rpc_clnt_swap_activate);
2789
Trond Myklebust15001e52016-01-30 20:05:34 -05002790static int
2791rpc_clnt_swap_deactivate_callback(struct rpc_clnt *clnt,
2792 struct rpc_xprt *xprt,
2793 void *dummy)
2794{
2795 xprt_disable_swap(xprt);
2796 return 0;
2797}
2798
Jeff Layton3c87ef62015-06-03 16:14:25 -04002799void
2800rpc_clnt_swap_deactivate(struct rpc_clnt *clnt)
2801{
Trond Myklebust15001e52016-01-30 20:05:34 -05002802 if (atomic_dec_if_positive(&clnt->cl_swapper) == 0)
2803 rpc_clnt_iterate_for_each_xprt(clnt,
2804 rpc_clnt_swap_deactivate_callback, NULL);
Jeff Layton3c87ef62015-06-03 16:14:25 -04002805}
2806EXPORT_SYMBOL_GPL(rpc_clnt_swap_deactivate);
2807#endif /* CONFIG_SUNRPC_SWAP */