blob: d7ec6132c046ec409db057d6ace332ab620fa88b [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);
Trond Myklebust762e4e62018-08-24 16:28:28 -040064static void call_encode(struct rpc_task *task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065static void call_decode(struct rpc_task *task);
66static void call_bind(struct rpc_task *task);
Chuck Leverda351872005-08-11 16:25:11 -040067static void call_bind_status(struct rpc_task *task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068static void call_transmit(struct rpc_task *task);
Trond Myklebust9e00abc2011-07-13 19:20:49 -040069#if defined(CONFIG_SUNRPC_BACKCHANNEL)
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -040070static void call_bc_transmit(struct rpc_task *task);
Trond Myklebust9e00abc2011-07-13 19:20:49 -040071#endif /* CONFIG_SUNRPC_BACKCHANNEL */
Linus Torvalds1da177e2005-04-16 15:20:36 -070072static void call_status(struct rpc_task *task);
Trond Myklebust940e3312005-11-09 21:45:24 -050073static void call_transmit_status(struct rpc_task *task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074static void call_refresh(struct rpc_task *task);
75static void call_refreshresult(struct rpc_task *task);
76static void call_timeout(struct rpc_task *task);
77static void call_connect(struct rpc_task *task);
78static void call_connect_status(struct rpc_task *task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Chuck Leverb0e1c572008-05-21 17:09:19 -040080static __be32 *rpc_encode_header(struct rpc_task *task);
81static __be32 *rpc_verify_header(struct rpc_task *task);
Chuck Levercaabea82009-12-03 15:58:56 -050082static int rpc_ping(struct rpc_clnt *clnt);
Trond Myklebust64c91a12007-06-23 10:17:16 -040083
Trond Myklebust188fef12007-06-16 14:18:40 -040084static void rpc_register_client(struct rpc_clnt *clnt)
85{
Trond Myklebust2446ab62012-03-01 17:00:56 -050086 struct net *net = rpc_net_ns(clnt);
87 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
Stanislav Kinsbursky70abc492012-01-12 22:07:51 +040088
89 spin_lock(&sn->rpc_client_lock);
90 list_add(&clnt->cl_clients, &sn->all_clients);
91 spin_unlock(&sn->rpc_client_lock);
Trond Myklebust188fef12007-06-16 14:18:40 -040092}
93
94static void rpc_unregister_client(struct rpc_clnt *clnt)
95{
Trond Myklebust2446ab62012-03-01 17:00:56 -050096 struct net *net = rpc_net_ns(clnt);
97 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
Stanislav Kinsbursky70abc492012-01-12 22:07:51 +040098
99 spin_lock(&sn->rpc_client_lock);
Trond Myklebust188fef12007-06-16 14:18:40 -0400100 list_del(&clnt->cl_clients);
Stanislav Kinsbursky70abc492012-01-12 22:07:51 +0400101 spin_unlock(&sn->rpc_client_lock);
Trond Myklebust188fef12007-06-16 14:18:40 -0400102}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400104static void __rpc_clnt_remove_pipedir(struct rpc_clnt *clnt)
105{
Trond Myklebustc36dcfe12013-08-26 17:44:26 -0400106 rpc_remove_client_dir(clnt);
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400107}
108
109static void rpc_clnt_remove_pipedir(struct rpc_clnt *clnt)
110{
Trond Myklebust2446ab62012-03-01 17:00:56 -0500111 struct net *net = rpc_net_ns(clnt);
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400112 struct super_block *pipefs_sb;
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400113
Trond Myklebust2446ab62012-03-01 17:00:56 -0500114 pipefs_sb = rpc_get_sb_net(net);
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400115 if (pipefs_sb) {
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400116 __rpc_clnt_remove_pipedir(clnt);
Trond Myklebust2446ab62012-03-01 17:00:56 -0500117 rpc_put_sb_net(net);
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400118 }
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400119}
120
121static struct dentry *rpc_setup_pipedir_sb(struct super_block *sb,
Trond Myklebust41b6b4d2013-08-23 13:03:50 -0400122 struct rpc_clnt *clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
Trond Myklebustf1345852005-09-23 11:08:25 -0400124 static uint32_t clntid;
Trond Myklebust41b6b4d2013-08-23 13:03:50 -0400125 const char *dir_name = clnt->cl_program->pipe_dir_name;
Trond Myklebust23ac6582009-08-09 15:14:25 -0400126 char name[15];
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400127 struct dentry *dir, *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400129 dir = rpc_d_lookup_sb(sb, dir_name);
Weston Andros Adamson922eeac2012-10-23 10:43:25 -0400130 if (dir == NULL) {
131 pr_info("RPC: pipefs directory doesn't exist: %s\n", dir_name);
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400132 return dir;
Weston Andros Adamson922eeac2012-10-23 10:43:25 -0400133 }
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400134 for (;;) {
Al Viroa95e6912013-07-14 16:43:54 +0400135 snprintf(name, sizeof(name), "clnt%x", (unsigned int)clntid++);
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400136 name[sizeof(name) - 1] = '\0';
Al Viroa95e6912013-07-14 16:43:54 +0400137 dentry = rpc_create_client_dir(dir, name, clnt);
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400138 if (!IS_ERR(dentry))
139 break;
Al Viroa95e6912013-07-14 16:43:54 +0400140 if (dentry == ERR_PTR(-EEXIST))
141 continue;
142 printk(KERN_INFO "RPC: Couldn't create pipefs entry"
143 " %s/%s, error %ld\n",
144 dir_name, name, PTR_ERR(dentry));
145 break;
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400146 }
147 dput(dir);
148 return dentry;
149}
150
151static int
Trond Myklebust41b6b4d2013-08-23 13:03:50 -0400152rpc_setup_pipedir(struct super_block *pipefs_sb, struct rpc_clnt *clnt)
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400153{
Stanislav Kinsbursky30507f52012-01-11 19:18:42 +0400154 struct dentry *dentry;
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400155
Trond Myklebustc36dcfe12013-08-26 17:44:26 -0400156 if (clnt->cl_program->pipe_dir_name != NULL) {
157 dentry = rpc_setup_pipedir_sb(pipefs_sb, clnt);
158 if (IS_ERR(dentry))
159 return PTR_ERR(dentry);
160 }
Trond Myklebust23ac6582009-08-09 15:14:25 -0400161 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162}
163
Trond Myklebust41b6b4d2013-08-23 13:03:50 -0400164static int rpc_clnt_skip_event(struct rpc_clnt *clnt, unsigned long event)
Stanislav Kinsburskyea8cfa02012-04-27 13:00:17 +0400165{
Trond Myklebust41b6b4d2013-08-23 13:03:50 -0400166 if (clnt->cl_program->pipe_dir_name == NULL)
Stanislav Kinsburskyea8cfa02012-04-27 13:00:17 +0400167 return 1;
Trond Myklebust41b6b4d2013-08-23 13:03:50 -0400168
Trond Myklebustc36dcfe12013-08-26 17:44:26 -0400169 switch (event) {
170 case RPC_PIPEFS_MOUNT:
171 if (clnt->cl_pipedir_objects.pdh_dentry != NULL)
172 return 1;
173 if (atomic_read(&clnt->cl_count) == 0)
174 return 1;
175 break;
176 case RPC_PIPEFS_UMOUNT:
177 if (clnt->cl_pipedir_objects.pdh_dentry == NULL)
178 return 1;
179 break;
180 }
Stanislav Kinsburskyea8cfa02012-04-27 13:00:17 +0400181 return 0;
182}
183
184static int __rpc_clnt_handle_event(struct rpc_clnt *clnt, unsigned long event,
185 struct super_block *sb)
Stanislav Kinsbursky80df9d22012-01-11 19:18:17 +0400186{
187 struct dentry *dentry;
Stanislav Kinsbursky80df9d22012-01-11 19:18:17 +0400188
189 switch (event) {
190 case RPC_PIPEFS_MOUNT:
Trond Myklebust41b6b4d2013-08-23 13:03:50 -0400191 dentry = rpc_setup_pipedir_sb(sb, clnt);
Weston Andros Adamson922eeac2012-10-23 10:43:25 -0400192 if (!dentry)
193 return -ENOENT;
Stanislav Kinsbursky80df9d22012-01-11 19:18:17 +0400194 if (IS_ERR(dentry))
195 return PTR_ERR(dentry);
Stanislav Kinsbursky80df9d22012-01-11 19:18:17 +0400196 break;
197 case RPC_PIPEFS_UMOUNT:
198 __rpc_clnt_remove_pipedir(clnt);
199 break;
200 default:
201 printk(KERN_ERR "%s: unknown event: %ld\n", __func__, event);
202 return -ENOTSUPP;
203 }
Amitoj Kaur Chawla2813b622016-08-08 14:43:49 +0530204 return 0;
Stanislav Kinsbursky80df9d22012-01-11 19:18:17 +0400205}
206
Stanislav Kinsburskyea8cfa02012-04-27 13:00:17 +0400207static int __rpc_pipefs_event(struct rpc_clnt *clnt, unsigned long event,
208 struct super_block *sb)
209{
210 int error = 0;
211
212 for (;; clnt = clnt->cl_parent) {
213 if (!rpc_clnt_skip_event(clnt, event))
214 error = __rpc_clnt_handle_event(clnt, event, sb);
215 if (error || clnt == clnt->cl_parent)
216 break;
217 }
218 return error;
219}
220
Stanislav Kinsburskyda3b4622012-02-27 22:05:29 +0400221static struct rpc_clnt *rpc_get_client_for_event(struct net *net, int event)
222{
223 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
224 struct rpc_clnt *clnt;
225
226 spin_lock(&sn->rpc_client_lock);
227 list_for_each_entry(clnt, &sn->all_clients, cl_clients) {
Stanislav Kinsburskyea8cfa02012-04-27 13:00:17 +0400228 if (rpc_clnt_skip_event(clnt, event))
Stanislav Kinsburskyda3b4622012-02-27 22:05:29 +0400229 continue;
Stanislav Kinsburskyda3b4622012-02-27 22:05:29 +0400230 spin_unlock(&sn->rpc_client_lock);
231 return clnt;
232 }
233 spin_unlock(&sn->rpc_client_lock);
234 return NULL;
235}
236
Stanislav Kinsbursky80df9d22012-01-11 19:18:17 +0400237static int rpc_pipefs_event(struct notifier_block *nb, unsigned long event,
238 void *ptr)
239{
240 struct super_block *sb = ptr;
241 struct rpc_clnt *clnt;
242 int error = 0;
Stanislav Kinsbursky80df9d22012-01-11 19:18:17 +0400243
Stanislav Kinsburskyda3b4622012-02-27 22:05:29 +0400244 while ((clnt = rpc_get_client_for_event(sb->s_fs_info, event))) {
Stanislav Kinsbursky80df9d22012-01-11 19:18:17 +0400245 error = __rpc_pipefs_event(clnt, event, sb);
246 if (error)
247 break;
248 }
Stanislav Kinsbursky80df9d22012-01-11 19:18:17 +0400249 return error;
250}
251
252static struct notifier_block rpc_clients_block = {
253 .notifier_call = rpc_pipefs_event,
Stanislav Kinsburskyeee173252012-01-10 16:13:19 +0400254 .priority = SUNRPC_PIPEFS_RPC_PRIO,
Stanislav Kinsbursky80df9d22012-01-11 19:18:17 +0400255};
256
257int rpc_clients_notifier_register(void)
258{
259 return rpc_pipefs_notifier_register(&rpc_clients_block);
260}
261
262void rpc_clients_notifier_unregister(void)
263{
264 return rpc_pipefs_notifier_unregister(&rpc_clients_block);
265}
266
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400267static struct rpc_xprt *rpc_clnt_set_transport(struct rpc_clnt *clnt,
268 struct rpc_xprt *xprt,
269 const struct rpc_timeout *timeout)
270{
271 struct rpc_xprt *old;
272
273 spin_lock(&clnt->cl_lock);
Trond Myklebust34751b92013-10-28 16:42:44 -0400274 old = rcu_dereference_protected(clnt->cl_xprt,
275 lockdep_is_held(&clnt->cl_lock));
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400276
277 if (!xprt_bound(xprt))
278 clnt->cl_autobind = 1;
279
280 clnt->cl_timeout = timeout;
281 rcu_assign_pointer(clnt->cl_xprt, xprt);
282 spin_unlock(&clnt->cl_lock);
283
284 return old;
285}
286
Trond Myklebustcbbb3442012-04-30 11:52:40 -0400287static void rpc_clnt_set_nodename(struct rpc_clnt *clnt, const char *nodename)
288{
Trond Myklebust03a9a422015-01-30 18:12:28 -0500289 clnt->cl_nodelen = strlcpy(clnt->cl_nodename,
290 nodename, sizeof(clnt->cl_nodename));
Trond Myklebustcbbb3442012-04-30 11:52:40 -0400291}
292
Chuck Leverd746e542013-10-17 14:12:17 -0400293static int rpc_client_register(struct rpc_clnt *clnt,
294 rpc_authflavor_t pseudoflavor,
295 const char *client_name)
Stanislav Kinsburskye73f4cc2013-06-24 11:52:52 +0400296{
Trond Myklebustc2190662013-08-26 19:23:04 -0400297 struct rpc_auth_create_args auth_args = {
Chuck Leverd746e542013-10-17 14:12:17 -0400298 .pseudoflavor = pseudoflavor,
299 .target_name = client_name,
Trond Myklebustc2190662013-08-26 19:23:04 -0400300 };
Stanislav Kinsburskye73f4cc2013-06-24 11:52:52 +0400301 struct rpc_auth *auth;
302 struct net *net = rpc_net_ns(clnt);
303 struct super_block *pipefs_sb;
Trond Myklebusteeee2452013-07-10 15:33:01 -0400304 int err;
Stanislav Kinsburskye73f4cc2013-06-24 11:52:52 +0400305
Jeff Laytonf9c72d12015-03-31 12:03:28 -0400306 rpc_clnt_debugfs_register(clnt);
Jeff Laytonb4b9d2c2014-11-26 14:44:43 -0500307
Stanislav Kinsburskye73f4cc2013-06-24 11:52:52 +0400308 pipefs_sb = rpc_get_sb_net(net);
309 if (pipefs_sb) {
Trond Myklebust41b6b4d2013-08-23 13:03:50 -0400310 err = rpc_setup_pipedir(pipefs_sb, clnt);
Stanislav Kinsburskye73f4cc2013-06-24 11:52:52 +0400311 if (err)
312 goto out;
313 }
314
Trond Myklebusteeee2452013-07-10 15:33:01 -0400315 rpc_register_client(clnt);
316 if (pipefs_sb)
317 rpc_put_sb_net(net);
318
Trond Myklebustc2190662013-08-26 19:23:04 -0400319 auth = rpcauth_create(&auth_args, clnt);
Stanislav Kinsburskye73f4cc2013-06-24 11:52:52 +0400320 if (IS_ERR(auth)) {
321 dprintk("RPC: Couldn't create auth handle (flavor %u)\n",
Chuck Leverd746e542013-10-17 14:12:17 -0400322 pseudoflavor);
Stanislav Kinsburskye73f4cc2013-06-24 11:52:52 +0400323 err = PTR_ERR(auth);
324 goto err_auth;
325 }
Trond Myklebusteeee2452013-07-10 15:33:01 -0400326 return 0;
327err_auth:
328 pipefs_sb = rpc_get_sb_net(net);
Trond Myklebust1540c5d2013-07-14 22:57:50 -0400329 rpc_unregister_client(clnt);
Trond Myklebusteeee2452013-07-10 15:33:01 -0400330 __rpc_clnt_remove_pipedir(clnt);
Stanislav Kinsburskye73f4cc2013-06-24 11:52:52 +0400331out:
332 if (pipefs_sb)
333 rpc_put_sb_net(net);
Jeff Laytonb4b9d2c2014-11-26 14:44:43 -0500334 rpc_clnt_debugfs_unregister(clnt);
Stanislav Kinsburskye73f4cc2013-06-24 11:52:52 +0400335 return err;
Stanislav Kinsburskye73f4cc2013-06-24 11:52:52 +0400336}
337
Trond Myklebust2f048db2013-09-04 21:51:44 -0400338static DEFINE_IDA(rpc_clids);
339
Kinglong Meec929ea02017-01-20 16:48:39 +0800340void rpc_cleanup_clids(void)
341{
342 ida_destroy(&rpc_clids);
343}
344
Trond Myklebust2f048db2013-09-04 21:51:44 -0400345static int rpc_alloc_clid(struct rpc_clnt *clnt)
346{
347 int clid;
348
349 clid = ida_simple_get(&rpc_clids, 0, 0, GFP_KERNEL);
350 if (clid < 0)
351 return clid;
352 clnt->cl_clid = clid;
353 return 0;
354}
355
356static void rpc_free_clid(struct rpc_clnt *clnt)
357{
358 ida_simple_remove(&rpc_clids, clnt->cl_clid);
359}
360
Trond Myklebust280ebcf2013-09-02 13:28:04 -0400361static struct rpc_clnt * rpc_new_client(const struct rpc_create_args *args,
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500362 struct rpc_xprt_switch *xps,
Trond Myklebust280ebcf2013-09-02 13:28:04 -0400363 struct rpc_xprt *xprt,
364 struct rpc_clnt *parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365{
Trond Myklebusta613fa12012-01-20 13:53:56 -0500366 const struct rpc_program *program = args->program;
367 const struct rpc_version *version;
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400368 struct rpc_clnt *clnt = NULL;
369 const struct rpc_timeout *timeout;
Trond Myklebust03a9a422015-01-30 18:12:28 -0500370 const char *nodename = args->nodename;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 int err;
Chuck Lever06b8d252007-09-11 18:00:20 -0400372
373 /* sanity check the name before trying to print it */
Chuck Lever46121cf2007-01-31 12:14:08 -0500374 dprintk("RPC: creating %s client for %s (xprt %p)\n",
Trond Myklebust698b6d02007-12-20 16:03:53 -0500375 program->name, args->servername, xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Trond Myklebust4ada5392007-06-14 17:26:17 -0400377 err = rpciod_up();
378 if (err)
379 goto out_no_rpciod;
Trond Myklebust698b6d02007-12-20 16:03:53 -0500380
Trond Myklebustf05c1242013-04-05 14:13:21 -0400381 err = -EINVAL;
Trond Myklebust698b6d02007-12-20 16:03:53 -0500382 if (args->version >= program->nrvers)
383 goto out_err;
384 version = program->version[args->version];
385 if (version == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 goto out_err;
387
388 err = -ENOMEM;
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700389 clnt = kzalloc(sizeof(*clnt), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 if (!clnt)
391 goto out_err;
Trond Myklebust280ebcf2013-09-02 13:28:04 -0400392 clnt->cl_parent = parent ? : clnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Trond Myklebust2f048db2013-09-04 21:51:44 -0400394 err = rpc_alloc_clid(clnt);
395 if (err)
396 goto out_no_clid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 clnt->cl_procinfo = version->procs;
399 clnt->cl_maxproc = version->nrprocs;
Benny Halevyd5b337b2008-09-28 09:21:26 +0300400 clnt->cl_prog = args->prognumber ? : program->number;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 clnt->cl_vers = version->number;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 clnt->cl_stats = program->stats;
Chuck Lever11c556b2006-03-20 13:44:22 -0500403 clnt->cl_metrics = rpc_alloc_iostats(clnt);
Trond Myklebust6739ffb2013-08-26 15:38:11 -0400404 rpc_init_pipe_dir_head(&clnt->cl_pipedir_objects);
Trond Myklebust23bf85b2006-11-21 10:40:23 -0500405 err = -ENOMEM;
406 if (clnt->cl_metrics == NULL)
407 goto out_no_stats;
Trond Myklebust3e32a5d2006-11-16 11:37:27 -0500408 clnt->cl_program = program;
Trond Myklebust6529eba2007-06-14 16:40:14 -0400409 INIT_LIST_HEAD(&clnt->cl_tasks);
Trond Myklebust4bef61f2007-06-16 14:17:01 -0400410 spin_lock_init(&clnt->cl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400412 timeout = xprt->timeout;
Trond Myklebustba7392b2007-12-20 16:03:55 -0500413 if (args->timeout != NULL) {
414 memcpy(&clnt->cl_timeout_default, args->timeout,
415 sizeof(clnt->cl_timeout_default));
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400416 timeout = &clnt->cl_timeout_default;
Trond Myklebustba7392b2007-12-20 16:03:55 -0500417 }
418
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400419 rpc_clnt_set_transport(clnt, xprt, timeout);
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500420 xprt_iter_init(&clnt->cl_xpi, xps);
421 xprt_switch_put(xps);
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400422
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 clnt->cl_rtt = &clnt->cl_rtt_default;
Trond Myklebustba7392b2007-12-20 16:03:55 -0500424 rpc_init_rtt(&clnt->cl_rtt_default, clnt->cl_timeout->to_initval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Trond Myklebust006abe82010-09-12 19:55:25 -0400426 atomic_set(&clnt->cl_count, 1);
Trond Myklebust34f52e32007-06-14 16:40:31 -0400427
Trond Myklebust03a9a422015-01-30 18:12:28 -0500428 if (nodename == NULL)
429 nodename = utsname()->nodename;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 /* save the nodename */
Trond Myklebust03a9a422015-01-30 18:12:28 -0500431 rpc_clnt_set_nodename(clnt, nodename);
Stanislav Kinsburskye73f4cc2013-06-24 11:52:52 +0400432
Chuck Leverd746e542013-10-17 14:12:17 -0400433 err = rpc_client_register(clnt, args->authflavor, args->client_name);
Stanislav Kinsburskye73f4cc2013-06-24 11:52:52 +0400434 if (err)
435 goto out_no_path;
Trond Myklebust280ebcf2013-09-02 13:28:04 -0400436 if (parent)
437 atomic_inc(&parent->cl_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 return clnt;
439
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440out_no_path:
Trond Myklebust23bf85b2006-11-21 10:40:23 -0500441 rpc_free_iostats(clnt->cl_metrics);
442out_no_stats:
Trond Myklebust2f048db2013-09-04 21:51:44 -0400443 rpc_free_clid(clnt);
444out_no_clid:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 kfree(clnt);
446out_err:
Trond Myklebust4ada5392007-06-14 17:26:17 -0400447 rpciod_down();
448out_no_rpciod:
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500449 xprt_switch_put(xps);
Trond Myklebustf05c1242013-04-05 14:13:21 -0400450 xprt_put(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 return ERR_PTR(err);
452}
453
J. Bruce Fieldsd50039e2016-05-16 17:03:42 -0400454static struct rpc_clnt *rpc_create_xprt(struct rpc_create_args *args,
Kinglong Mee83ddfeb2014-03-24 11:58:59 +0800455 struct rpc_xprt *xprt)
456{
457 struct rpc_clnt *clnt = NULL;
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500458 struct rpc_xprt_switch *xps;
Kinglong Mee83ddfeb2014-03-24 11:58:59 +0800459
J. Bruce Fields39a9bea2016-05-17 12:38:21 -0400460 if (args->bc_xprt && args->bc_xprt->xpt_bc_xps) {
Chuck Lever16590a22016-08-22 14:57:42 -0400461 WARN_ON_ONCE(!(args->protocol & XPRT_TRANSPORT_BC));
J. Bruce Fields39a9bea2016-05-17 12:38:21 -0400462 xps = args->bc_xprt->xpt_bc_xps;
463 xprt_switch_get(xps);
464 } else {
465 xps = xprt_switch_alloc(xprt, GFP_KERNEL);
466 if (xps == NULL) {
467 xprt_put(xprt);
468 return ERR_PTR(-ENOMEM);
469 }
470 if (xprt->bc_xprt) {
471 xprt_switch_get(xps);
472 xprt->bc_xprt->xpt_bc_xps = xps;
473 }
J. Bruce Fields1208fd52016-05-20 17:07:17 -0400474 }
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500475 clnt = rpc_new_client(args, xps, xprt, NULL);
Kinglong Mee83ddfeb2014-03-24 11:58:59 +0800476 if (IS_ERR(clnt))
477 return clnt;
478
479 if (!(args->flags & RPC_CLNT_CREATE_NOPING)) {
480 int err = rpc_ping(clnt);
481 if (err != 0) {
482 rpc_shutdown_client(clnt);
483 return ERR_PTR(err);
484 }
485 }
486
487 clnt->cl_softrtry = 1;
488 if (args->flags & RPC_CLNT_CREATE_HARDRTRY)
489 clnt->cl_softrtry = 0;
490
491 if (args->flags & RPC_CLNT_CREATE_AUTOBIND)
492 clnt->cl_autobind = 1;
Trond Myklebust2aca5b82014-09-24 22:35:58 -0400493 if (args->flags & RPC_CLNT_CREATE_NO_RETRANS_TIMEOUT)
494 clnt->cl_noretranstimeo = 1;
Kinglong Mee83ddfeb2014-03-24 11:58:59 +0800495 if (args->flags & RPC_CLNT_CREATE_DISCRTRY)
496 clnt->cl_discrtry = 1;
497 if (!(args->flags & RPC_CLNT_CREATE_QUIET))
498 clnt->cl_chatty = 1;
499
500 return clnt;
501}
Kinglong Mee83ddfeb2014-03-24 11:58:59 +0800502
Ben Hutchings2c530402012-07-10 10:55:09 +0000503/**
Chuck Leverc2866762006-08-22 20:06:20 -0400504 * rpc_create - create an RPC client and transport with one call
505 * @args: rpc_clnt create argument structure
506 *
507 * Creates and initializes an RPC transport and an RPC client.
508 *
509 * It can ping the server in order to determine if it is up, and to see if
510 * it supports this program and version. RPC_CLNT_CREATE_NOPING disables
511 * this behavior so asynchronous tasks can also use rpc_create.
512 */
513struct rpc_clnt *rpc_create(struct rpc_create_args *args)
514{
515 struct rpc_xprt *xprt;
\"Talpey, Thomas\3c341b0b2007-09-10 13:47:07 -0400516 struct xprt_create xprtargs = {
Pavel Emelyanov9a23e332010-09-29 16:05:12 +0400517 .net = args->net,
\"Talpey, Thomas\4fa016e2007-09-10 13:47:57 -0400518 .ident = args->protocol,
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +0200519 .srcaddr = args->saddress,
Frank van Maarseveen96802a02007-07-08 13:08:54 +0200520 .dstaddr = args->address,
521 .addrlen = args->addrsize,
Trond Myklebust4e0038b2012-03-01 17:01:05 -0500522 .servername = args->servername,
Alexandros Batsakisf300bab2009-09-10 17:33:30 +0300523 .bc_xprt = args->bc_xprt,
Frank van Maarseveen96802a02007-07-08 13:08:54 +0200524 };
Chuck Lever510deb02007-12-10 14:56:24 -0500525 char servername[48];
Chuck Leverc2866762006-08-22 20:06:20 -0400526
J. Bruce Fieldsd50039e2016-05-16 17:03:42 -0400527 if (args->bc_xprt) {
Chuck Lever16590a22016-08-22 14:57:42 -0400528 WARN_ON_ONCE(!(args->protocol & XPRT_TRANSPORT_BC));
J. Bruce Fieldsd50039e2016-05-16 17:03:42 -0400529 xprt = args->bc_xprt->xpt_bc_xprt;
530 if (xprt) {
531 xprt_get(xprt);
532 return rpc_create_xprt(args, xprt);
533 }
534 }
535
Trond Myklebustb7993ce2013-04-14 11:42:00 -0400536 if (args->flags & RPC_CLNT_CREATE_INFINITE_SLOTS)
537 xprtargs.flags |= XPRT_CREATE_INFINITE_SLOTS;
J. Bruce Fields33d90ac2013-04-11 15:06:36 -0400538 if (args->flags & RPC_CLNT_CREATE_NO_IDLE_TIMEOUT)
539 xprtargs.flags |= XPRT_CREATE_NO_IDLE_TIMEOUT;
Chuck Leverc2866762006-08-22 20:06:20 -0400540 /*
Chuck Lever43780b82007-07-01 12:13:22 -0400541 * If the caller chooses not to specify a hostname, whip
542 * up a string representation of the passed-in address.
543 */
Trond Myklebust4e0038b2012-03-01 17:01:05 -0500544 if (xprtargs.servername == NULL) {
Chuck Lever176e21e2011-05-09 15:22:44 -0400545 struct sockaddr_un *sun =
546 (struct sockaddr_un *)args->address;
Chuck Leverda09eb92011-05-09 15:22:25 -0400547 struct sockaddr_in *sin =
548 (struct sockaddr_in *)args->address;
549 struct sockaddr_in6 *sin6 =
550 (struct sockaddr_in6 *)args->address;
551
Chuck Lever510deb02007-12-10 14:56:24 -0500552 servername[0] = '\0';
553 switch (args->address->sa_family) {
Chuck Lever176e21e2011-05-09 15:22:44 -0400554 case AF_LOCAL:
555 snprintf(servername, sizeof(servername), "%s",
556 sun->sun_path);
557 break;
Chuck Leverda09eb92011-05-09 15:22:25 -0400558 case AF_INET:
Harvey Harrison21454aa2008-10-31 00:54:56 -0700559 snprintf(servername, sizeof(servername), "%pI4",
560 &sin->sin_addr.s_addr);
Chuck Lever510deb02007-12-10 14:56:24 -0500561 break;
Chuck Leverda09eb92011-05-09 15:22:25 -0400562 case AF_INET6:
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700563 snprintf(servername, sizeof(servername), "%pI6",
Chuck Leverda09eb92011-05-09 15:22:25 -0400564 &sin6->sin6_addr);
Chuck Lever510deb02007-12-10 14:56:24 -0500565 break;
Chuck Lever510deb02007-12-10 14:56:24 -0500566 default:
567 /* caller wants default server name, but
568 * address family isn't recognized. */
569 return ERR_PTR(-EINVAL);
570 }
Trond Myklebust4e0038b2012-03-01 17:01:05 -0500571 xprtargs.servername = servername;
Chuck Lever43780b82007-07-01 12:13:22 -0400572 }
573
Chuck Lever510deb02007-12-10 14:56:24 -0500574 xprt = xprt_create_transport(&xprtargs);
575 if (IS_ERR(xprt))
576 return (struct rpc_clnt *)xprt;
577
Chuck Lever43780b82007-07-01 12:13:22 -0400578 /*
Chuck Leverc2866762006-08-22 20:06:20 -0400579 * By default, kernel RPC client connects from a reserved port.
580 * CAP_NET_BIND_SERVICE will not be set for unprivileged requesters,
581 * but it is always enabled for rpciod, which handles the connect
582 * operation.
583 */
584 xprt->resvport = 1;
585 if (args->flags & RPC_CLNT_CREATE_NONPRIVPORT)
586 xprt->resvport = 0;
587
Kinglong Mee83ddfeb2014-03-24 11:58:59 +0800588 return rpc_create_xprt(args, xprt);
Chuck Leverc2866762006-08-22 20:06:20 -0400589}
Chuck Leverb86acd52006-08-22 20:06:22 -0400590EXPORT_SYMBOL_GPL(rpc_create);
Chuck Leverc2866762006-08-22 20:06:20 -0400591
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592/*
593 * This function clones the RPC client structure. It allows us to share the
594 * same transport while varying parameters such as the authentication
595 * flavour.
596 */
Chuck Lever1b63a752012-09-14 17:23:52 -0400597static struct rpc_clnt *__rpc_clone_client(struct rpc_create_args *args,
598 struct rpc_clnt *clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599{
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500600 struct rpc_xprt_switch *xps;
Trond Myklebust2446ab62012-03-01 17:00:56 -0500601 struct rpc_xprt *xprt;
Chuck Lever1b63a752012-09-14 17:23:52 -0400602 struct rpc_clnt *new;
603 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Chuck Lever1b63a752012-09-14 17:23:52 -0400605 err = -ENOMEM;
Trond Myklebust2446ab62012-03-01 17:00:56 -0500606 rcu_read_lock();
607 xprt = xprt_get(rcu_dereference(clnt->cl_xprt));
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500608 xps = xprt_switch_get(rcu_dereference(clnt->cl_xpi.xpi_xpswitch));
Trond Myklebust2446ab62012-03-01 17:00:56 -0500609 rcu_read_unlock();
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500610 if (xprt == NULL || xps == NULL) {
611 xprt_put(xprt);
612 xprt_switch_put(xps);
Chuck Lever1b63a752012-09-14 17:23:52 -0400613 goto out_err;
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500614 }
Chuck Lever1b63a752012-09-14 17:23:52 -0400615 args->servername = xprt->servername;
Trond Myklebust03a9a422015-01-30 18:12:28 -0500616 args->nodename = clnt->cl_nodename;
Chuck Lever1b63a752012-09-14 17:23:52 -0400617
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500618 new = rpc_new_client(args, xps, xprt, clnt);
Chuck Lever1b63a752012-09-14 17:23:52 -0400619 if (IS_ERR(new)) {
620 err = PTR_ERR(new);
Chuck Levera58e0be2013-03-22 12:52:59 -0400621 goto out_err;
Chuck Lever1b63a752012-09-14 17:23:52 -0400622 }
623
Chuck Lever1b63a752012-09-14 17:23:52 -0400624 /* Turn off autobind on clones */
625 new->cl_autobind = 0;
626 new->cl_softrtry = clnt->cl_softrtry;
Trond Myklebust2aca5b82014-09-24 22:35:58 -0400627 new->cl_noretranstimeo = clnt->cl_noretranstimeo;
Chuck Lever1b63a752012-09-14 17:23:52 -0400628 new->cl_discrtry = clnt->cl_discrtry;
629 new->cl_chatty = clnt->cl_chatty;
NeilBrown5e169232018-12-03 11:30:30 +1100630 new->cl_principal = clnt->cl_principal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 return new;
Chuck Lever1b63a752012-09-14 17:23:52 -0400632
Chuck Lever1b63a752012-09-14 17:23:52 -0400633out_err:
Harvey Harrison0dc47872008-03-05 20:47:47 -0800634 dprintk("RPC: %s: returned error %d\n", __func__, err);
Trond Myklebust3e32a5d2006-11-16 11:37:27 -0500635 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636}
Chuck Lever1b63a752012-09-14 17:23:52 -0400637
638/**
639 * rpc_clone_client - Clone an RPC client structure
640 *
641 * @clnt: RPC client whose parameters are copied
642 *
643 * Returns a fresh RPC client or an ERR_PTR.
644 */
645struct rpc_clnt *rpc_clone_client(struct rpc_clnt *clnt)
646{
647 struct rpc_create_args args = {
648 .program = clnt->cl_program,
649 .prognumber = clnt->cl_prog,
650 .version = clnt->cl_vers,
651 .authflavor = clnt->cl_auth->au_flavor,
Chuck Lever1b63a752012-09-14 17:23:52 -0400652 };
653 return __rpc_clone_client(&args, clnt);
654}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400655EXPORT_SYMBOL_GPL(rpc_clone_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
Chuck Leverba9b5842012-09-14 17:24:02 -0400657/**
658 * rpc_clone_client_set_auth - Clone an RPC client structure and set its auth
659 *
660 * @clnt: RPC client whose parameters are copied
Randy Dunlap7144bca2013-01-09 17:12:35 -0800661 * @flavor: security flavor for new client
Chuck Leverba9b5842012-09-14 17:24:02 -0400662 *
663 * Returns a fresh RPC client or an ERR_PTR.
664 */
665struct rpc_clnt *
666rpc_clone_client_set_auth(struct rpc_clnt *clnt, rpc_authflavor_t flavor)
667{
668 struct rpc_create_args args = {
669 .program = clnt->cl_program,
670 .prognumber = clnt->cl_prog,
671 .version = clnt->cl_vers,
672 .authflavor = flavor,
Chuck Leverba9b5842012-09-14 17:24:02 -0400673 };
674 return __rpc_clone_client(&args, clnt);
675}
676EXPORT_SYMBOL_GPL(rpc_clone_client_set_auth);
677
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400678/**
679 * rpc_switch_client_transport: switch the RPC transport on the fly
680 * @clnt: pointer to a struct rpc_clnt
681 * @args: pointer to the new transport arguments
682 * @timeout: pointer to the new timeout parameters
683 *
684 * This function allows the caller to switch the RPC transport for the
685 * rpc_clnt structure 'clnt' to allow it to connect to a mirrored NFS
686 * server, for instance. It assumes that the caller has ensured that
687 * there are no active RPC tasks by using some form of locking.
688 *
689 * Returns zero if "clnt" is now using the new xprt. Otherwise a
690 * negative errno is returned, and "clnt" continues to use the old
691 * xprt.
692 */
693int rpc_switch_client_transport(struct rpc_clnt *clnt,
694 struct xprt_create *args,
695 const struct rpc_timeout *timeout)
696{
697 const struct rpc_timeout *old_timeo;
698 rpc_authflavor_t pseudoflavor;
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500699 struct rpc_xprt_switch *xps, *oldxps;
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400700 struct rpc_xprt *xprt, *old;
701 struct rpc_clnt *parent;
702 int err;
703
704 xprt = xprt_create_transport(args);
705 if (IS_ERR(xprt)) {
706 dprintk("RPC: failed to create new xprt for clnt %p\n",
707 clnt);
708 return PTR_ERR(xprt);
709 }
710
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500711 xps = xprt_switch_alloc(xprt, GFP_KERNEL);
712 if (xps == NULL) {
713 xprt_put(xprt);
714 return -ENOMEM;
715 }
716
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400717 pseudoflavor = clnt->cl_auth->au_flavor;
718
719 old_timeo = clnt->cl_timeout;
720 old = rpc_clnt_set_transport(clnt, xprt, timeout);
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500721 oldxps = xprt_iter_xchg_switch(&clnt->cl_xpi, xps);
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400722
723 rpc_unregister_client(clnt);
724 __rpc_clnt_remove_pipedir(clnt);
Jeff Laytonb4b9d2c2014-11-26 14:44:43 -0500725 rpc_clnt_debugfs_unregister(clnt);
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400726
727 /*
728 * A new transport was created. "clnt" therefore
729 * becomes the root of a new cl_parent tree. clnt's
730 * children, if it has any, still point to the old xprt.
731 */
732 parent = clnt->cl_parent;
733 clnt->cl_parent = clnt;
734
735 /*
736 * The old rpc_auth cache cannot be re-used. GSS
737 * contexts in particular are between a single
738 * client and server.
739 */
740 err = rpc_client_register(clnt, pseudoflavor, NULL);
741 if (err)
742 goto out_revert;
743
744 synchronize_rcu();
745 if (parent != clnt)
746 rpc_release_client(parent);
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500747 xprt_switch_put(oldxps);
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400748 xprt_put(old);
749 dprintk("RPC: replaced xprt for clnt %p\n", clnt);
750 return 0;
751
752out_revert:
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500753 xps = xprt_iter_xchg_switch(&clnt->cl_xpi, oldxps);
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400754 rpc_clnt_set_transport(clnt, old, old_timeo);
755 clnt->cl_parent = parent;
756 rpc_client_register(clnt, pseudoflavor, NULL);
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500757 xprt_switch_put(xps);
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400758 xprt_put(xprt);
759 dprintk("RPC: failed to switch xprt for clnt %p\n", clnt);
760 return err;
761}
762EXPORT_SYMBOL_GPL(rpc_switch_client_transport);
763
Trond Myklebust32278862016-01-30 20:39:19 -0500764static
765int rpc_clnt_xprt_iter_init(struct rpc_clnt *clnt, struct rpc_xprt_iter *xpi)
766{
767 struct rpc_xprt_switch *xps;
768
769 rcu_read_lock();
770 xps = xprt_switch_get(rcu_dereference(clnt->cl_xpi.xpi_xpswitch));
771 rcu_read_unlock();
772 if (xps == NULL)
773 return -EAGAIN;
774 xprt_iter_init_listall(xpi, xps);
775 xprt_switch_put(xps);
776 return 0;
777}
778
779/**
780 * rpc_clnt_iterate_for_each_xprt - Apply a function to all transports
781 * @clnt: pointer to client
782 * @fn: function to apply
783 * @data: void pointer to function data
784 *
785 * Iterates through the list of RPC transports currently attached to the
786 * client and applies the function fn(clnt, xprt, data).
787 *
788 * On error, the iteration stops, and the function returns the error value.
789 */
790int rpc_clnt_iterate_for_each_xprt(struct rpc_clnt *clnt,
791 int (*fn)(struct rpc_clnt *, struct rpc_xprt *, void *),
792 void *data)
793{
794 struct rpc_xprt_iter xpi;
795 int ret;
796
797 ret = rpc_clnt_xprt_iter_init(clnt, &xpi);
798 if (ret)
799 return ret;
800 for (;;) {
801 struct rpc_xprt *xprt = xprt_iter_get_next(&xpi);
802
803 if (!xprt)
804 break;
805 ret = fn(clnt, xprt, data);
806 xprt_put(xprt);
807 if (ret < 0)
808 break;
809 }
810 xprt_iter_destroy(&xpi);
811 return ret;
812}
813EXPORT_SYMBOL_GPL(rpc_clnt_iterate_for_each_xprt);
814
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815/*
Trond Myklebust58f96122010-07-31 14:29:08 -0400816 * Kill all tasks for the given client.
817 * XXX: kill their descendants as well?
818 */
819void rpc_killall_tasks(struct rpc_clnt *clnt)
820{
821 struct rpc_task *rovr;
822
823
824 if (list_empty(&clnt->cl_tasks))
825 return;
826 dprintk("RPC: killing all tasks for client %p\n", clnt);
827 /*
828 * Spin lock all_tasks to prevent changes...
829 */
830 spin_lock(&clnt->cl_lock);
831 list_for_each_entry(rovr, &clnt->cl_tasks, tk_task) {
832 if (!RPC_IS_ACTIVATED(rovr))
833 continue;
834 if (!(rovr->tk_flags & RPC_TASK_KILLED)) {
835 rovr->tk_flags |= RPC_TASK_KILLED;
836 rpc_exit(rovr, -EIO);
Stanislav Kinsbursky8e26de22011-03-17 18:54:23 +0300837 if (RPC_IS_QUEUED(rovr))
838 rpc_wake_up_queued_task(rovr->tk_waitqueue,
839 rovr);
Trond Myklebust58f96122010-07-31 14:29:08 -0400840 }
841 }
842 spin_unlock(&clnt->cl_lock);
843}
844EXPORT_SYMBOL_GPL(rpc_killall_tasks);
845
846/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 * Properly shut down an RPC client, terminating all outstanding
Trond Myklebust90c57552007-06-09 19:49:36 -0400848 * requests.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 */
Trond Myklebust4c402b42007-06-14 16:40:32 -0400850void rpc_shutdown_client(struct rpc_clnt *clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851{
Weston Andros Adamson168e4b32012-10-30 17:01:40 -0400852 might_sleep();
853
Trond Myklebust4e0038b2012-03-01 17:01:05 -0500854 dprintk_rcu("RPC: shutting down %s client for %s\n",
Trond Myklebust55909f22013-08-23 11:48:15 -0400855 clnt->cl_program->name,
Trond Myklebust4e0038b2012-03-01 17:01:05 -0500856 rcu_dereference(clnt->cl_xprt)->servername);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
Trond Myklebust34f52e32007-06-14 16:40:31 -0400858 while (!list_empty(&clnt->cl_tasks)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 rpc_killall_tasks(clnt);
Ingo Molnar532347e2006-01-09 20:52:53 -0800860 wait_event_timeout(destroy_wait,
Trond Myklebust34f52e32007-06-14 16:40:31 -0400861 list_empty(&clnt->cl_tasks), 1*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 }
863
Trond Myklebust4c402b42007-06-14 16:40:32 -0400864 rpc_release_client(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400866EXPORT_SYMBOL_GPL(rpc_shutdown_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
868/*
Trond Myklebust34f52e32007-06-14 16:40:31 -0400869 * Free an RPC client
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 */
Trond Myklebustd07ba842013-11-12 17:24:36 -0500871static struct rpc_clnt *
Trond Myklebust006abe82010-09-12 19:55:25 -0400872rpc_free_client(struct rpc_clnt *clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873{
Trond Myklebustd07ba842013-11-12 17:24:36 -0500874 struct rpc_clnt *parent = NULL;
875
Trond Myklebust4e0038b2012-03-01 17:01:05 -0500876 dprintk_rcu("RPC: destroying %s client for %s\n",
Trond Myklebust55909f22013-08-23 11:48:15 -0400877 clnt->cl_program->name,
Trond Myklebust4e0038b2012-03-01 17:01:05 -0500878 rcu_dereference(clnt->cl_xprt)->servername);
Trond Myklebust6eac7d32012-01-20 13:53:37 -0500879 if (clnt->cl_parent != clnt)
Trond Myklebustd07ba842013-11-12 17:24:36 -0500880 parent = clnt->cl_parent;
Jeff Laytonb4b9d2c2014-11-26 14:44:43 -0500881 rpc_clnt_debugfs_unregister(clnt);
Stanislav Kinsburskyf5131252012-01-11 19:18:26 +0400882 rpc_clnt_remove_pipedir(clnt);
Stanislav Kinsburskyadb6fa72013-06-26 10:15:14 +0400883 rpc_unregister_client(clnt);
Chuck Lever11c556b2006-03-20 13:44:22 -0500884 rpc_free_iostats(clnt->cl_metrics);
885 clnt->cl_metrics = NULL;
Trond Myklebust2446ab62012-03-01 17:00:56 -0500886 xprt_put(rcu_dereference_raw(clnt->cl_xprt));
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500887 xprt_iter_destroy(&clnt->cl_xpi);
Trond Myklebust4ada5392007-06-14 17:26:17 -0400888 rpciod_down();
Trond Myklebust2f048db2013-09-04 21:51:44 -0400889 rpc_free_clid(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 kfree(clnt);
Trond Myklebustd07ba842013-11-12 17:24:36 -0500891 return parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892}
893
894/*
Trond Myklebust1dd17ec2007-06-26 16:57:41 -0400895 * Free an RPC client
896 */
Stephen Hemminger8fdee4c2018-07-24 12:29:15 -0700897static struct rpc_clnt *
Trond Myklebust006abe82010-09-12 19:55:25 -0400898rpc_free_auth(struct rpc_clnt *clnt)
Trond Myklebust1dd17ec2007-06-26 16:57:41 -0400899{
Trond Myklebustd07ba842013-11-12 17:24:36 -0500900 if (clnt->cl_auth == NULL)
901 return rpc_free_client(clnt);
Trond Myklebust1dd17ec2007-06-26 16:57:41 -0400902
903 /*
904 * Note: RPCSEC_GSS may need to send NULL RPC calls in order to
905 * release remaining GSS contexts. This mechanism ensures
906 * that it can do so safely.
907 */
Trond Myklebust006abe82010-09-12 19:55:25 -0400908 atomic_inc(&clnt->cl_count);
Trond Myklebust1dd17ec2007-06-26 16:57:41 -0400909 rpcauth_release(clnt->cl_auth);
910 clnt->cl_auth = NULL;
Trond Myklebust006abe82010-09-12 19:55:25 -0400911 if (atomic_dec_and_test(&clnt->cl_count))
Trond Myklebustd07ba842013-11-12 17:24:36 -0500912 return rpc_free_client(clnt);
913 return NULL;
Trond Myklebust1dd17ec2007-06-26 16:57:41 -0400914}
915
916/*
Trond Myklebust34f52e32007-06-14 16:40:31 -0400917 * Release reference to the RPC client
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 */
919void
920rpc_release_client(struct rpc_clnt *clnt)
921{
Trond Myklebust34f52e32007-06-14 16:40:31 -0400922 dprintk("RPC: rpc_release_client(%p)\n", clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
Trond Myklebustd07ba842013-11-12 17:24:36 -0500924 do {
925 if (list_empty(&clnt->cl_tasks))
926 wake_up(&destroy_wait);
927 if (!atomic_dec_and_test(&clnt->cl_count))
928 break;
929 clnt = rpc_free_auth(clnt);
930 } while (clnt != NULL);
Trond Myklebust34f52e32007-06-14 16:40:31 -0400931}
Simo Sorce1d658332012-05-25 18:09:55 -0400932EXPORT_SYMBOL_GPL(rpc_release_client);
Trond Myklebust34f52e32007-06-14 16:40:31 -0400933
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000934/**
935 * rpc_bind_new_program - bind a new RPC program to an existing client
Randy Dunlap65b6e422008-02-13 15:03:23 -0800936 * @old: old rpc_client
937 * @program: rpc program to set
938 * @vers: rpc program version
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000939 *
940 * Clones the rpc client and sets up a new RPC program. This is mainly
941 * of use for enabling different RPC programs to share the same transport.
942 * The Sun NFSv2/v3 ACL protocol can do this.
943 */
944struct rpc_clnt *rpc_bind_new_program(struct rpc_clnt *old,
Trond Myklebusta613fa12012-01-20 13:53:56 -0500945 const struct rpc_program *program,
Chuck Lever89eb21c2007-09-11 18:00:09 -0400946 u32 vers)
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000947{
Trond Myklebustf994c432012-11-01 12:14:14 -0400948 struct rpc_create_args args = {
949 .program = program,
950 .prognumber = program->number,
951 .version = vers,
952 .authflavor = old->cl_auth->au_flavor,
Trond Myklebustf994c432012-11-01 12:14:14 -0400953 };
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000954 struct rpc_clnt *clnt;
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000955 int err;
956
Trond Myklebustf994c432012-11-01 12:14:14 -0400957 clnt = __rpc_clone_client(&args, old);
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000958 if (IS_ERR(clnt))
959 goto out;
Chuck Levercaabea82009-12-03 15:58:56 -0500960 err = rpc_ping(clnt);
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000961 if (err != 0) {
962 rpc_shutdown_client(clnt);
963 clnt = ERR_PTR(err);
964 }
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800965out:
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000966 return clnt;
967}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400968EXPORT_SYMBOL_GPL(rpc_bind_new_program);
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000969
Bill Baker0f90be12018-06-19 16:24:58 -0500970void rpc_task_release_transport(struct rpc_task *task)
971{
972 struct rpc_xprt *xprt = task->tk_xprt;
973
974 if (xprt) {
975 task->tk_xprt = NULL;
976 xprt_put(xprt);
977 }
978}
979EXPORT_SYMBOL_GPL(rpc_task_release_transport);
980
Trond Myklebust58f96122010-07-31 14:29:08 -0400981void rpc_task_release_client(struct rpc_task *task)
982{
983 struct rpc_clnt *clnt = task->tk_client;
984
985 if (clnt != NULL) {
986 /* Remove from client task list */
987 spin_lock(&clnt->cl_lock);
988 list_del(&task->tk_task);
989 spin_unlock(&clnt->cl_lock);
990 task->tk_client = NULL;
991
992 rpc_release_client(clnt);
993 }
Bill Baker0f90be12018-06-19 16:24:58 -0500994 rpc_task_release_transport(task);
995}
Trond Myklebustfb43d172016-01-30 16:39:26 -0500996
Bill Baker0f90be12018-06-19 16:24:58 -0500997static
998void rpc_task_set_transport(struct rpc_task *task, struct rpc_clnt *clnt)
999{
1000 if (!task->tk_xprt)
1001 task->tk_xprt = xprt_iter_get_next(&clnt->cl_xpi);
Trond Myklebust58f96122010-07-31 14:29:08 -04001002}
1003
1004static
1005void rpc_task_set_client(struct rpc_task *task, struct rpc_clnt *clnt)
1006{
Trond Myklebustfb43d172016-01-30 16:39:26 -05001007
Trond Myklebust58f96122010-07-31 14:29:08 -04001008 if (clnt != NULL) {
Bill Baker0f90be12018-06-19 16:24:58 -05001009 rpc_task_set_transport(task, clnt);
Trond Myklebust58f96122010-07-31 14:29:08 -04001010 task->tk_client = clnt;
Trond Myklebust006abe82010-09-12 19:55:25 -04001011 atomic_inc(&clnt->cl_count);
Trond Myklebust58f96122010-07-31 14:29:08 -04001012 if (clnt->cl_softrtry)
1013 task->tk_flags |= RPC_TASK_SOFT;
Trond Myklebust8a19a0b2013-09-24 12:00:27 -04001014 if (clnt->cl_noretranstimeo)
1015 task->tk_flags |= RPC_TASK_NO_RETRANS_TIMEOUT;
Jeff Layton3c87ef62015-06-03 16:14:25 -04001016 if (atomic_read(&clnt->cl_swapper))
1017 task->tk_flags |= RPC_TASK_SWAPPER;
Trond Myklebust58f96122010-07-31 14:29:08 -04001018 /* Add to the client's list of all tasks */
1019 spin_lock(&clnt->cl_lock);
1020 list_add_tail(&task->tk_task, &clnt->cl_tasks);
1021 spin_unlock(&clnt->cl_lock);
1022 }
1023}
1024
1025static void
1026rpc_task_set_rpc_message(struct rpc_task *task, const struct rpc_message *msg)
1027{
1028 if (msg != NULL) {
1029 task->tk_msg.rpc_proc = msg->rpc_proc;
1030 task->tk_msg.rpc_argp = msg->rpc_argp;
1031 task->tk_msg.rpc_resp = msg->rpc_resp;
Trond Myklebusta17c2152010-07-31 14:29:08 -04001032 if (msg->rpc_cred != NULL)
NeilBrowna52458b2018-12-03 11:30:31 +11001033 task->tk_msg.rpc_cred = get_cred(msg->rpc_cred);
Trond Myklebust58f96122010-07-31 14:29:08 -04001034 }
1035}
1036
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037/*
1038 * Default callback for async RPC calls
1039 */
1040static void
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001041rpc_default_callback(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042{
1043}
1044
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001045static const struct rpc_call_ops rpc_default_ops = {
1046 .rpc_call_done = rpc_default_callback,
1047};
1048
Trond Myklebustc970aa82007-07-14 15:39:59 -04001049/**
1050 * rpc_run_task - Allocate a new RPC task, then run rpc_execute against it
1051 * @task_setup_data: pointer to task initialisation data
1052 */
1053struct rpc_task *rpc_run_task(const struct rpc_task_setup *task_setup_data)
Trond Myklebust6e5b70e2007-06-12 10:02:37 -04001054{
Trond Myklebust19445b92010-04-16 16:41:10 -04001055 struct rpc_task *task;
Trond Myklebust6e5b70e2007-06-12 10:02:37 -04001056
Trond Myklebust84115e12007-07-14 15:39:59 -04001057 task = rpc_new_task(task_setup_data);
Trond Myklebust6e5b70e2007-06-12 10:02:37 -04001058
Trond Myklebust58f96122010-07-31 14:29:08 -04001059 rpc_task_set_client(task, task_setup_data->rpc_client);
1060 rpc_task_set_rpc_message(task, task_setup_data->rpc_message);
1061
Trond Myklebust58f96122010-07-31 14:29:08 -04001062 if (task->tk_action == NULL)
1063 rpc_call_start(task);
1064
Trond Myklebust6e5b70e2007-06-12 10:02:37 -04001065 atomic_inc(&task->tk_count);
1066 rpc_execute(task);
Trond Myklebust19445b92010-04-16 16:41:10 -04001067 return task;
Trond Myklebust6e5b70e2007-06-12 10:02:37 -04001068}
Trond Myklebustc970aa82007-07-14 15:39:59 -04001069EXPORT_SYMBOL_GPL(rpc_run_task);
Trond Myklebust6e5b70e2007-06-12 10:02:37 -04001070
1071/**
1072 * rpc_call_sync - Perform a synchronous RPC call
1073 * @clnt: pointer to RPC client
1074 * @msg: RPC call parameters
1075 * @flags: RPC call flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 */
Trond Myklebustcbc20052008-02-14 11:11:30 -05001077int rpc_call_sync(struct rpc_clnt *clnt, const struct rpc_message *msg, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078{
1079 struct rpc_task *task;
Trond Myklebust84115e12007-07-14 15:39:59 -04001080 struct rpc_task_setup task_setup_data = {
1081 .rpc_client = clnt,
1082 .rpc_message = msg,
1083 .callback_ops = &rpc_default_ops,
1084 .flags = flags,
1085 };
Trond Myklebust6e5b70e2007-06-12 10:02:37 -04001086 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
Weston Andros Adamson50d2bdb2012-11-01 16:04:40 -04001088 WARN_ON_ONCE(flags & RPC_TASK_ASYNC);
1089 if (flags & RPC_TASK_ASYNC) {
1090 rpc_release_calldata(task_setup_data.callback_ops,
1091 task_setup_data.callback_data);
1092 return -EINVAL;
1093 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
Trond Myklebustc970aa82007-07-14 15:39:59 -04001095 task = rpc_run_task(&task_setup_data);
Trond Myklebust6e5b70e2007-06-12 10:02:37 -04001096 if (IS_ERR(task))
1097 return PTR_ERR(task);
Trond Myklebuste60859a2006-01-03 09:55:10 +01001098 status = task->tk_status;
Trond Myklebustbde8f002007-01-24 11:54:53 -08001099 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 return status;
1101}
Trond Myklebuste8914c62007-07-14 15:39:59 -04001102EXPORT_SYMBOL_GPL(rpc_call_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
Trond Myklebust6e5b70e2007-06-12 10:02:37 -04001104/**
1105 * rpc_call_async - Perform an asynchronous RPC call
1106 * @clnt: pointer to RPC client
1107 * @msg: RPC call parameters
1108 * @flags: RPC call flags
Randy Dunlap65b6e422008-02-13 15:03:23 -08001109 * @tk_ops: RPC call ops
Trond Myklebust6e5b70e2007-06-12 10:02:37 -04001110 * @data: user call data
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 */
1112int
Trond Myklebustcbc20052008-02-14 11:11:30 -05001113rpc_call_async(struct rpc_clnt *clnt, const struct rpc_message *msg, int flags,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001114 const struct rpc_call_ops *tk_ops, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115{
1116 struct rpc_task *task;
Trond Myklebust84115e12007-07-14 15:39:59 -04001117 struct rpc_task_setup task_setup_data = {
1118 .rpc_client = clnt,
1119 .rpc_message = msg,
1120 .callback_ops = tk_ops,
1121 .callback_data = data,
1122 .flags = flags|RPC_TASK_ASYNC,
1123 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
Trond Myklebustc970aa82007-07-14 15:39:59 -04001125 task = rpc_run_task(&task_setup_data);
Trond Myklebust6e5b70e2007-06-12 10:02:37 -04001126 if (IS_ERR(task))
1127 return PTR_ERR(task);
1128 rpc_put_task(task);
1129 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130}
Trond Myklebuste8914c62007-07-14 15:39:59 -04001131EXPORT_SYMBOL_GPL(rpc_call_async);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001133#if defined(CONFIG_SUNRPC_BACKCHANNEL)
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001134/**
1135 * rpc_run_bc_task - Allocate a new RPC task for backchannel use, then run
1136 * rpc_execute against it
Jaswinder Singh Rajput7a73fdd2009-09-24 14:58:42 -04001137 * @req: RPC request
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001138 */
Trond Myklebust0f419792015-06-01 22:59:08 -04001139struct rpc_task *rpc_run_bc_task(struct rpc_rqst *req)
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001140{
1141 struct rpc_task *task;
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001142 struct rpc_task_setup task_setup_data = {
Trond Myklebust0f419792015-06-01 22:59:08 -04001143 .callback_ops = &rpc_default_ops,
Trond Myklebust762e4e62018-08-24 16:28:28 -04001144 .flags = RPC_TASK_SOFTCONN |
1145 RPC_TASK_NO_RETRANS_TIMEOUT,
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001146 };
1147
1148 dprintk("RPC: rpc_run_bc_task req= %p\n", req);
1149 /*
1150 * Create an rpc_task to send the data
1151 */
1152 task = rpc_new_task(&task_setup_data);
Trond Myklebust902c5882018-09-01 17:21:01 -04001153 xprt_init_bc_request(req, task);
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001154
1155 task->tk_action = call_bc_transmit;
1156 atomic_inc(&task->tk_count);
Weston Andros Adamson9a6478f2012-10-23 10:43:27 -04001157 WARN_ON_ONCE(atomic_read(&task->tk_count) != 2);
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001158 rpc_execute(task);
1159
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001160 dprintk("RPC: rpc_run_bc_task: task= %p\n", task);
1161 return task;
1162}
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001163#endif /* CONFIG_SUNRPC_BACKCHANNEL */
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001164
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165void
Trond Myklebust77de2c52007-10-25 18:40:21 -04001166rpc_call_start(struct rpc_task *task)
1167{
1168 task->tk_action = call_start;
1169}
1170EXPORT_SYMBOL_GPL(rpc_call_start);
1171
Chuck Levered394402006-08-22 20:06:17 -04001172/**
1173 * rpc_peeraddr - extract remote peer address from clnt's xprt
1174 * @clnt: RPC client structure
1175 * @buf: target buffer
Randy Dunlap65b6e422008-02-13 15:03:23 -08001176 * @bufsize: length of target buffer
Chuck Levered394402006-08-22 20:06:17 -04001177 *
1178 * Returns the number of bytes that are actually in the stored address.
1179 */
1180size_t rpc_peeraddr(struct rpc_clnt *clnt, struct sockaddr *buf, size_t bufsize)
1181{
1182 size_t bytes;
Trond Myklebust2446ab62012-03-01 17:00:56 -05001183 struct rpc_xprt *xprt;
Chuck Levered394402006-08-22 20:06:17 -04001184
Trond Myklebust2446ab62012-03-01 17:00:56 -05001185 rcu_read_lock();
1186 xprt = rcu_dereference(clnt->cl_xprt);
1187
1188 bytes = xprt->addrlen;
Chuck Levered394402006-08-22 20:06:17 -04001189 if (bytes > bufsize)
1190 bytes = bufsize;
Trond Myklebust2446ab62012-03-01 17:00:56 -05001191 memcpy(buf, &xprt->addr, bytes);
1192 rcu_read_unlock();
1193
1194 return bytes;
Chuck Levered394402006-08-22 20:06:17 -04001195}
Chuck Leverb86acd52006-08-22 20:06:22 -04001196EXPORT_SYMBOL_GPL(rpc_peeraddr);
Chuck Levered394402006-08-22 20:06:17 -04001197
Chuck Leverf425eba2006-08-22 20:06:18 -04001198/**
1199 * rpc_peeraddr2str - return remote peer address in printable format
1200 * @clnt: RPC client structure
1201 * @format: address format
1202 *
Trond Myklebust2446ab62012-03-01 17:00:56 -05001203 * NB: the lifetime of the memory referenced by the returned pointer is
1204 * the same as the rpc_xprt itself. As long as the caller uses this
1205 * pointer, it must hold the RCU read lock.
Chuck Leverf425eba2006-08-22 20:06:18 -04001206 */
Chuck Leverb454ae92008-01-07 18:34:48 -05001207const char *rpc_peeraddr2str(struct rpc_clnt *clnt,
1208 enum rpc_display_format_t format)
Chuck Leverf425eba2006-08-22 20:06:18 -04001209{
Trond Myklebust2446ab62012-03-01 17:00:56 -05001210 struct rpc_xprt *xprt;
1211
1212 xprt = rcu_dereference(clnt->cl_xprt);
Chuck Lever7559c7a2006-12-05 16:35:37 -05001213
1214 if (xprt->address_strings[format] != NULL)
1215 return xprt->address_strings[format];
1216 else
1217 return "unprintable";
Chuck Leverf425eba2006-08-22 20:06:18 -04001218}
Chuck Leverb86acd52006-08-22 20:06:22 -04001219EXPORT_SYMBOL_GPL(rpc_peeraddr2str);
Chuck Leverf425eba2006-08-22 20:06:18 -04001220
Chuck Lever2e738fd2012-03-01 17:01:14 -05001221static const struct sockaddr_in rpc_inaddr_loopback = {
1222 .sin_family = AF_INET,
1223 .sin_addr.s_addr = htonl(INADDR_ANY),
1224};
1225
1226static const struct sockaddr_in6 rpc_in6addr_loopback = {
1227 .sin6_family = AF_INET6,
1228 .sin6_addr = IN6ADDR_ANY_INIT,
1229};
1230
1231/*
1232 * Try a getsockname() on a connected datagram socket. Using a
1233 * connected datagram socket prevents leaving a socket in TIME_WAIT.
1234 * This conserves the ephemeral port number space.
1235 *
1236 * Returns zero and fills in "buf" if successful; otherwise, a
1237 * negative errno is returned.
1238 */
1239static int rpc_sockname(struct net *net, struct sockaddr *sap, size_t salen,
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001240 struct sockaddr *buf)
Chuck Lever2e738fd2012-03-01 17:01:14 -05001241{
1242 struct socket *sock;
1243 int err;
1244
1245 err = __sock_create(net, sap->sa_family,
1246 SOCK_DGRAM, IPPROTO_UDP, &sock, 1);
1247 if (err < 0) {
1248 dprintk("RPC: can't create UDP socket (%d)\n", err);
1249 goto out;
1250 }
1251
1252 switch (sap->sa_family) {
1253 case AF_INET:
1254 err = kernel_bind(sock,
1255 (struct sockaddr *)&rpc_inaddr_loopback,
1256 sizeof(rpc_inaddr_loopback));
1257 break;
1258 case AF_INET6:
1259 err = kernel_bind(sock,
1260 (struct sockaddr *)&rpc_in6addr_loopback,
1261 sizeof(rpc_in6addr_loopback));
1262 break;
1263 default:
1264 err = -EAFNOSUPPORT;
1265 goto out;
1266 }
1267 if (err < 0) {
1268 dprintk("RPC: can't bind UDP socket (%d)\n", err);
1269 goto out_release;
1270 }
1271
1272 err = kernel_connect(sock, sap, salen, 0);
1273 if (err < 0) {
1274 dprintk("RPC: can't connect UDP socket (%d)\n", err);
1275 goto out_release;
1276 }
1277
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001278 err = kernel_getsockname(sock, buf);
Chuck Lever2e738fd2012-03-01 17:01:14 -05001279 if (err < 0) {
1280 dprintk("RPC: getsockname failed (%d)\n", err);
1281 goto out_release;
1282 }
1283
1284 err = 0;
1285 if (buf->sa_family == AF_INET6) {
1286 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)buf;
1287 sin6->sin6_scope_id = 0;
1288 }
1289 dprintk("RPC: %s succeeded\n", __func__);
1290
1291out_release:
1292 sock_release(sock);
1293out:
1294 return err;
1295}
1296
1297/*
1298 * Scraping a connected socket failed, so we don't have a useable
1299 * local address. Fallback: generate an address that will prevent
1300 * the server from calling us back.
1301 *
1302 * Returns zero and fills in "buf" if successful; otherwise, a
1303 * negative errno is returned.
1304 */
1305static int rpc_anyaddr(int family, struct sockaddr *buf, size_t buflen)
1306{
1307 switch (family) {
1308 case AF_INET:
1309 if (buflen < sizeof(rpc_inaddr_loopback))
1310 return -EINVAL;
1311 memcpy(buf, &rpc_inaddr_loopback,
1312 sizeof(rpc_inaddr_loopback));
1313 break;
1314 case AF_INET6:
1315 if (buflen < sizeof(rpc_in6addr_loopback))
1316 return -EINVAL;
1317 memcpy(buf, &rpc_in6addr_loopback,
1318 sizeof(rpc_in6addr_loopback));
Trond Myklebust0b161e62015-12-30 18:14:06 -05001319 break;
Chuck Lever2e738fd2012-03-01 17:01:14 -05001320 default:
1321 dprintk("RPC: %s: address family not supported\n",
1322 __func__);
1323 return -EAFNOSUPPORT;
1324 }
1325 dprintk("RPC: %s: succeeded\n", __func__);
1326 return 0;
1327}
1328
1329/**
1330 * rpc_localaddr - discover local endpoint address for an RPC client
1331 * @clnt: RPC client structure
1332 * @buf: target buffer
1333 * @buflen: size of target buffer, in bytes
1334 *
1335 * Returns zero and fills in "buf" and "buflen" if successful;
1336 * otherwise, a negative errno is returned.
1337 *
1338 * This works even if the underlying transport is not currently connected,
1339 * or if the upper layer never previously provided a source address.
1340 *
1341 * The result of this function call is transient: multiple calls in
1342 * succession may give different results, depending on how local
1343 * networking configuration changes over time.
1344 */
1345int rpc_localaddr(struct rpc_clnt *clnt, struct sockaddr *buf, size_t buflen)
1346{
1347 struct sockaddr_storage address;
1348 struct sockaddr *sap = (struct sockaddr *)&address;
1349 struct rpc_xprt *xprt;
1350 struct net *net;
1351 size_t salen;
1352 int err;
1353
1354 rcu_read_lock();
1355 xprt = rcu_dereference(clnt->cl_xprt);
1356 salen = xprt->addrlen;
1357 memcpy(sap, &xprt->addr, salen);
1358 net = get_net(xprt->xprt_net);
1359 rcu_read_unlock();
1360
1361 rpc_set_port(sap, 0);
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001362 err = rpc_sockname(net, sap, salen, buf);
Chuck Lever2e738fd2012-03-01 17:01:14 -05001363 put_net(net);
1364 if (err != 0)
1365 /* Couldn't discover local address, return ANYADDR */
1366 return rpc_anyaddr(sap->sa_family, buf, buflen);
1367 return 0;
1368}
1369EXPORT_SYMBOL_GPL(rpc_localaddr);
1370
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371void
1372rpc_setbufsize(struct rpc_clnt *clnt, unsigned int sndsize, unsigned int rcvsize)
1373{
Trond Myklebust2446ab62012-03-01 17:00:56 -05001374 struct rpc_xprt *xprt;
1375
1376 rcu_read_lock();
1377 xprt = rcu_dereference(clnt->cl_xprt);
Chuck Lever470056c2005-08-25 16:25:56 -07001378 if (xprt->ops->set_buffer_size)
1379 xprt->ops->set_buffer_size(xprt, sndsize, rcvsize);
Trond Myklebust2446ab62012-03-01 17:00:56 -05001380 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381}
Trond Myklebuste8914c62007-07-14 15:39:59 -04001382EXPORT_SYMBOL_GPL(rpc_setbufsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
Trond Myklebust2446ab62012-03-01 17:00:56 -05001384/**
Trond Myklebust2446ab62012-03-01 17:00:56 -05001385 * rpc_net_ns - Get the network namespace for this RPC client
1386 * @clnt: RPC client to query
1387 *
1388 */
1389struct net *rpc_net_ns(struct rpc_clnt *clnt)
1390{
1391 struct net *ret;
1392
1393 rcu_read_lock();
1394 ret = rcu_dereference(clnt->cl_xprt)->xprt_net;
1395 rcu_read_unlock();
1396 return ret;
1397}
1398EXPORT_SYMBOL_GPL(rpc_net_ns);
1399
1400/**
1401 * rpc_max_payload - Get maximum payload size for a transport, in bytes
1402 * @clnt: RPC client to query
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 *
1404 * For stream transports, this is one RPC record fragment (see RFC
1405 * 1831), as we don't support multi-record requests yet. For datagram
1406 * transports, this is the size of an IP packet minus the IP, UDP, and
1407 * RPC header sizes.
1408 */
1409size_t rpc_max_payload(struct rpc_clnt *clnt)
1410{
Trond Myklebust2446ab62012-03-01 17:00:56 -05001411 size_t ret;
1412
1413 rcu_read_lock();
1414 ret = rcu_dereference(clnt->cl_xprt)->max_payload;
1415 rcu_read_unlock();
1416 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417}
Chuck Leverb86acd52006-08-22 20:06:22 -04001418EXPORT_SYMBOL_GPL(rpc_max_payload);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419
Chuck Lever35f5a422006-01-03 09:55:50 +01001420/**
Chuck Lever6b26cc82016-05-02 14:40:40 -04001421 * rpc_max_bc_payload - Get maximum backchannel payload size, in bytes
1422 * @clnt: RPC client to query
1423 */
1424size_t rpc_max_bc_payload(struct rpc_clnt *clnt)
1425{
1426 struct rpc_xprt *xprt;
1427 size_t ret;
1428
1429 rcu_read_lock();
1430 xprt = rcu_dereference(clnt->cl_xprt);
1431 ret = xprt->ops->bc_maxpayload(xprt);
1432 rcu_read_unlock();
1433 return ret;
1434}
1435EXPORT_SYMBOL_GPL(rpc_max_bc_payload);
1436
1437/**
Chuck Lever35f5a422006-01-03 09:55:50 +01001438 * rpc_force_rebind - force transport to check that remote port is unchanged
1439 * @clnt: client to rebind
1440 *
1441 */
1442void rpc_force_rebind(struct rpc_clnt *clnt)
1443{
Trond Myklebust2446ab62012-03-01 17:00:56 -05001444 if (clnt->cl_autobind) {
1445 rcu_read_lock();
1446 xprt_clear_bound(rcu_dereference(clnt->cl_xprt));
1447 rcu_read_unlock();
1448 }
Chuck Lever35f5a422006-01-03 09:55:50 +01001449}
Chuck Leverb86acd52006-08-22 20:06:22 -04001450EXPORT_SYMBOL_GPL(rpc_force_rebind);
Chuck Lever35f5a422006-01-03 09:55:50 +01001451
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452/*
Andy Adamsonaae20062009-04-01 09:22:40 -04001453 * Restart an (async) RPC call from the call_prepare state.
1454 * Usually called from within the exit handler.
1455 */
Trond Myklebustf1f88fc2010-07-31 14:29:07 -04001456int
Andy Adamsonaae20062009-04-01 09:22:40 -04001457rpc_restart_call_prepare(struct rpc_task *task)
1458{
1459 if (RPC_ASSASSINATED(task))
Trond Myklebustf1f88fc2010-07-31 14:29:07 -04001460 return 0;
Trond Myklebustd00c5d42011-10-19 12:17:29 -07001461 task->tk_action = call_start;
Trond Myklebust494314c2014-03-20 12:59:09 -04001462 task->tk_status = 0;
Trond Myklebustd00c5d42011-10-19 12:17:29 -07001463 if (task->tk_ops->rpc_call_prepare != NULL)
1464 task->tk_action = rpc_prepare_task;
Trond Myklebustf1f88fc2010-07-31 14:29:07 -04001465 return 1;
Andy Adamsonaae20062009-04-01 09:22:40 -04001466}
1467EXPORT_SYMBOL_GPL(rpc_restart_call_prepare);
1468
1469/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 * Restart an (async) RPC call. Usually called from within the
1471 * exit handler.
1472 */
Trond Myklebustf1f88fc2010-07-31 14:29:07 -04001473int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474rpc_restart_call(struct rpc_task *task)
1475{
1476 if (RPC_ASSASSINATED(task))
Trond Myklebustf1f88fc2010-07-31 14:29:07 -04001477 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 task->tk_action = call_start;
Trond Myklebust494314c2014-03-20 12:59:09 -04001479 task->tk_status = 0;
Trond Myklebustf1f88fc2010-07-31 14:29:07 -04001480 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481}
Trond Myklebuste8914c62007-07-14 15:39:59 -04001482EXPORT_SYMBOL_GPL(rpc_restart_call);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483
Jeff Laytonb4b9d2c2014-11-26 14:44:43 -05001484const char
1485*rpc_proc_name(const struct rpc_task *task)
Chuck Lever3748f1e2008-05-21 17:09:12 -04001486{
1487 const struct rpc_procinfo *proc = task->tk_msg.rpc_proc;
1488
1489 if (proc) {
1490 if (proc->p_name)
1491 return proc->p_name;
1492 else
1493 return "NULL";
1494 } else
1495 return "no proc";
1496}
Chuck Lever3748f1e2008-05-21 17:09:12 -04001497
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498/*
1499 * 0. Initial state
1500 *
1501 * Other FSM states can be visited zero or more times, but
1502 * this state is visited exactly once for each RPC.
1503 */
1504static void
1505call_start(struct rpc_task *task)
1506{
1507 struct rpc_clnt *clnt = task->tk_client;
Christoph Hellwig1c5876d2017-05-08 23:27:10 +02001508 int idx = task->tk_msg.rpc_proc->p_statidx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509
Chuck Leverc435da62017-11-03 13:46:14 -04001510 trace_rpc_request(task);
Chuck Lever3748f1e2008-05-21 17:09:12 -04001511 dprintk("RPC: %5u call_start %s%d proc %s (%s)\n", task->tk_pid,
Trond Myklebust55909f22013-08-23 11:48:15 -04001512 clnt->cl_program->name, clnt->cl_vers,
Chuck Lever3748f1e2008-05-21 17:09:12 -04001513 rpc_proc_name(task),
Chuck Lever46121cf2007-01-31 12:14:08 -05001514 (RPC_IS_ASYNC(task) ? "async" : "sync"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515
Christoph Hellwig1c5876d2017-05-08 23:27:10 +02001516 /* Increment call count (version might not be valid for ping) */
1517 if (clnt->cl_program->version[clnt->cl_vers])
1518 clnt->cl_program->version[clnt->cl_vers]->counts[idx]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 clnt->cl_stats->rpccnt++;
1520 task->tk_action = call_reserve;
Bill Baker0f90be12018-06-19 16:24:58 -05001521 rpc_task_set_transport(task, clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522}
1523
1524/*
1525 * 1. Reserve an RPC call slot
1526 */
1527static void
1528call_reserve(struct rpc_task *task)
1529{
Chuck Lever46121cf2007-01-31 12:14:08 -05001530 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 task->tk_status = 0;
1533 task->tk_action = call_reserveresult;
1534 xprt_reserve(task);
1535}
1536
Trond Myklebustba60eb22013-04-14 10:49:37 -04001537static void call_retry_reserve(struct rpc_task *task);
1538
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539/*
1540 * 1b. Grok the result of xprt_reserve()
1541 */
1542static void
1543call_reserveresult(struct rpc_task *task)
1544{
1545 int status = task->tk_status;
1546
Chuck Lever46121cf2007-01-31 12:14:08 -05001547 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548
1549 /*
1550 * After a call to xprt_reserve(), we must have either
1551 * a request slot or else an error status.
1552 */
1553 task->tk_status = 0;
1554 if (status >= 0) {
1555 if (task->tk_rqstp) {
J. Bruce Fieldsf2d47d02010-09-12 19:55:25 -04001556 task->tk_action = call_refresh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 return;
1558 }
1559
1560 printk(KERN_ERR "%s: status=%d, but no request slot, exiting\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08001561 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 rpc_exit(task, -EIO);
1563 return;
1564 }
1565
1566 /*
1567 * Even though there was an error, we may have acquired
1568 * a request slot somehow. Make sure not to leak it.
1569 */
1570 if (task->tk_rqstp) {
1571 printk(KERN_ERR "%s: status=%d, request allocated anyway\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08001572 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 xprt_release(task);
1574 }
1575
1576 switch (status) {
Trond Myklebust1afeaf52012-05-19 12:12:53 -04001577 case -ENOMEM:
1578 rpc_delay(task, HZ >> 2);
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05001579 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 case -EAGAIN: /* woken up; retry */
Trond Myklebustba60eb22013-04-14 10:49:37 -04001581 task->tk_action = call_retry_reserve;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 return;
1583 case -EIO: /* probably a shutdown */
1584 break;
1585 default:
1586 printk(KERN_ERR "%s: unrecognized error %d, exiting\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08001587 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 break;
1589 }
1590 rpc_exit(task, status);
1591}
1592
1593/*
Trond Myklebustba60eb22013-04-14 10:49:37 -04001594 * 1c. Retry reserving an RPC call slot
1595 */
1596static void
1597call_retry_reserve(struct rpc_task *task)
1598{
1599 dprint_status(task);
1600
1601 task->tk_status = 0;
1602 task->tk_action = call_reserveresult;
1603 xprt_retry_reserve(task);
1604}
1605
1606/*
J. Bruce Fields55576242010-09-12 19:55:25 -04001607 * 2. Bind and/or refresh the credentials
1608 */
1609static void
1610call_refresh(struct rpc_task *task)
1611{
1612 dprint_status(task);
1613
1614 task->tk_action = call_refreshresult;
1615 task->tk_status = 0;
1616 task->tk_client->cl_stats->rpcauthrefresh++;
1617 rpcauth_refreshcred(task);
1618}
1619
1620/*
1621 * 2a. Process the results of a credential refresh
1622 */
1623static void
1624call_refreshresult(struct rpc_task *task)
1625{
1626 int status = task->tk_status;
1627
1628 dprint_status(task);
1629
1630 task->tk_status = 0;
Trond Myklebust5fc43972010-11-20 11:13:31 -05001631 task->tk_action = call_refresh;
J. Bruce Fields55576242010-09-12 19:55:25 -04001632 switch (status) {
Trond Myklebust5fc43972010-11-20 11:13:31 -05001633 case 0:
Weston Andros Adamson6ff33b72013-12-17 12:16:11 -05001634 if (rpcauth_uptodatecred(task)) {
Trond Myklebust5fc43972010-11-20 11:13:31 -05001635 task->tk_action = call_allocate;
Weston Andros Adamson6ff33b72013-12-17 12:16:11 -05001636 return;
1637 }
1638 /* Use rate-limiting and a max number of retries if refresh
1639 * had status 0 but failed to update the cred.
1640 */
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05001641 /* fall through */
J. Bruce Fields55576242010-09-12 19:55:25 -04001642 case -ETIMEDOUT:
1643 rpc_delay(task, 3*HZ);
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05001644 /* fall through */
Trond Myklebust5fc43972010-11-20 11:13:31 -05001645 case -EAGAIN:
1646 status = -EACCES;
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05001647 /* fall through */
Andy Adamsonf1ff0c22013-08-14 11:59:13 -04001648 case -EKEYEXPIRED:
Trond Myklebust5fc43972010-11-20 11:13:31 -05001649 if (!task->tk_cred_retry)
1650 break;
1651 task->tk_cred_retry--;
1652 dprintk("RPC: %5u %s: retry refresh creds\n",
1653 task->tk_pid, __func__);
1654 return;
J. Bruce Fields55576242010-09-12 19:55:25 -04001655 }
Trond Myklebust5fc43972010-11-20 11:13:31 -05001656 dprintk("RPC: %5u %s: refresh creds failed with error %d\n",
1657 task->tk_pid, __func__, status);
1658 rpc_exit(task, status);
J. Bruce Fields55576242010-09-12 19:55:25 -04001659}
1660
1661/*
1662 * 2b. Allocate the buffer. For details, see sched.c:rpc_malloc.
Chuck Lever02107142006-01-03 09:55:49 +01001663 * (Note: buffer memory is freed in xprt_release).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 */
1665static void
1666call_allocate(struct rpc_task *task)
1667{
J. Bruce Fieldsf2d47d02010-09-12 19:55:25 -04001668 unsigned int slack = task->tk_rqstp->rq_cred->cr_auth->au_cslack;
Chuck Lever02107142006-01-03 09:55:49 +01001669 struct rpc_rqst *req = task->tk_rqstp;
Trond Myklebusta4f08352013-01-08 09:10:21 -05001670 struct rpc_xprt *xprt = req->rq_xprt;
Christoph Hellwig499b4982017-05-12 15:36:49 +02001671 const struct rpc_procinfo *proc = task->tk_msg.rpc_proc;
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04001672 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
Chuck Lever46121cf2007-01-31 12:14:08 -05001674 dprint_status(task);
1675
Chuck Lever2bea90d2007-03-29 16:47:53 -04001676 task->tk_status = 0;
Trond Myklebust762e4e62018-08-24 16:28:28 -04001677 task->tk_action = call_encode;
Chuck Lever2bea90d2007-03-29 16:47:53 -04001678
Chuck Lever02107142006-01-03 09:55:49 +01001679 if (req->rq_buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 return;
1681
Chuck Lever2bea90d2007-03-29 16:47:53 -04001682 if (proc->p_proc != 0) {
1683 BUG_ON(proc->p_arglen == 0);
1684 if (proc->p_decode != NULL)
1685 BUG_ON(proc->p_replen == 0);
1686 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687
Chuck Lever2bea90d2007-03-29 16:47:53 -04001688 /*
1689 * Calculate the size (in quads) of the RPC call
1690 * and reply headers, and convert both values
1691 * to byte sizes.
1692 */
1693 req->rq_callsize = RPC_CALLHDRSIZE + (slack << 1) + proc->p_arglen;
1694 req->rq_callsize <<= 2;
1695 req->rq_rcvsize = RPC_REPHDRSIZE + slack + proc->p_replen;
1696 req->rq_rcvsize <<= 2;
1697
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04001698 status = xprt->ops->buf_alloc(task);
Chuck Lever4a068252015-05-11 14:02:25 -04001699 xprt_inject_disconnect(xprt);
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04001700 if (status == 0)
1701 return;
1702 if (status != -ENOMEM) {
1703 rpc_exit(task, status);
1704 return;
1705 }
Chuck Lever46121cf2007-01-31 12:14:08 -05001706
1707 dprintk("RPC: %5u rpc_buffer allocation failed\n", task->tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708
Trond Myklebust5afa91332011-06-17 10:14:59 -04001709 if (RPC_IS_ASYNC(task) || !fatal_signal_pending(current)) {
Trond Myklebustb6e9c712007-10-01 12:06:44 -04001710 task->tk_action = call_allocate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 rpc_delay(task, HZ>>4);
1712 return;
1713 }
1714
1715 rpc_exit(task, -ERESTARTSYS);
1716}
1717
Trond Myklebust7ebbbc62018-08-28 09:00:27 -04001718static int
Trond Myklebust940e3312005-11-09 21:45:24 -05001719rpc_task_need_encode(struct rpc_task *task)
1720{
Trond Myklebust762e4e62018-08-24 16:28:28 -04001721 return test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate) == 0 &&
1722 (!(task->tk_flags & RPC_TASK_SENT) ||
1723 !(task->tk_flags & RPC_TASK_NO_RETRANS_TIMEOUT) ||
1724 xprt_request_need_retransmit(task));
Trond Myklebust940e3312005-11-09 21:45:24 -05001725}
1726
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727static void
Chuck Leverb0e1c572008-05-21 17:09:19 -04001728rpc_xdr_encode(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 struct rpc_rqst *req = task->tk_rqstp;
Chuck Lever9f06c712010-12-14 14:59:18 +00001731 kxdreproc_t encode;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07001732 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733
Chuck Lever46121cf2007-01-31 12:14:08 -05001734 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735
Chuck Leverb9c5bc02016-09-15 10:55:12 -04001736 xdr_buf_init(&req->rq_snd_buf,
1737 req->rq_buffer,
1738 req->rq_callsize);
1739 xdr_buf_init(&req->rq_rcv_buf,
Chuck Lever68778942016-09-15 10:55:37 -04001740 req->rq_rbuffer,
Chuck Leverb9c5bc02016-09-15 10:55:12 -04001741 req->rq_rcvsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742
Chuck Leverb0e1c572008-05-21 17:09:19 -04001743 p = rpc_encode_header(task);
Trond Myklebust97b78ae2019-01-02 17:53:13 -05001744 if (p == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 return;
Chuck Leverb0e1c572008-05-21 17:09:19 -04001746
1747 encode = task->tk_msg.rpc_proc->p_encode;
J. Bruce Fieldsf3680312005-10-13 16:54:48 -04001748 if (encode == NULL)
1749 return;
1750
1751 task->tk_status = rpcauth_wrap_req(task, encode, req, p,
1752 task->tk_msg.rpc_argp);
Trond Myklebust9d96acb2018-09-13 12:22:04 -04001753 if (task->tk_status == 0)
1754 xprt_request_prepare(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755}
1756
1757/*
Trond Myklebust762e4e62018-08-24 16:28:28 -04001758 * 3. Encode arguments of an RPC call
1759 */
1760static void
1761call_encode(struct rpc_task *task)
1762{
1763 if (!rpc_task_need_encode(task))
1764 goto out;
1765 /* Encode here so that rpcsec_gss can use correct sequence number. */
1766 rpc_xdr_encode(task);
1767 /* Did the encode result in an error condition? */
1768 if (task->tk_status != 0) {
1769 /* Was the error nonfatal? */
Trond Myklebust97b78ae2019-01-02 17:53:13 -05001770 switch (task->tk_status) {
1771 case -EAGAIN:
1772 case -ENOMEM:
Trond Myklebust762e4e62018-08-24 16:28:28 -04001773 rpc_delay(task, HZ >> 4);
Trond Myklebust97b78ae2019-01-02 17:53:13 -05001774 break;
1775 case -EKEYEXPIRED:
1776 task->tk_action = call_refresh;
1777 break;
1778 default:
Trond Myklebust762e4e62018-08-24 16:28:28 -04001779 rpc_exit(task, task->tk_status);
Trond Myklebust97b78ae2019-01-02 17:53:13 -05001780 }
Trond Myklebust762e4e62018-08-24 16:28:28 -04001781 return;
1782 }
1783
1784 /* Add task to reply queue before transmission to avoid races */
1785 if (rpc_reply_expected(task))
1786 xprt_request_enqueue_receive(task);
1787 xprt_request_enqueue_transmit(task);
1788out:
1789 task->tk_action = call_bind;
1790}
1791
1792/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 * 4. Get the server port number if not yet set
1794 */
1795static void
1796call_bind(struct rpc_task *task)
1797{
Trond Myklebustad2368d2013-01-08 10:08:33 -05001798 struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799
Chuck Lever46121cf2007-01-31 12:14:08 -05001800 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801
Chuck Leverda351872005-08-11 16:25:11 -04001802 task->tk_action = call_connect;
Chuck Leverec739ef2006-08-22 20:06:15 -04001803 if (!xprt_bound(xprt)) {
Chuck Leverda351872005-08-11 16:25:11 -04001804 task->tk_action = call_bind_status;
Chuck Leverec739ef2006-08-22 20:06:15 -04001805 task->tk_timeout = xprt->bind_timeout;
Chuck Leverbbf7c1d2006-08-22 20:06:16 -04001806 xprt->ops->rpcbind(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 }
1808}
1809
1810/*
Chuck Leverda351872005-08-11 16:25:11 -04001811 * 4a. Sort out bind result
1812 */
1813static void
1814call_bind_status(struct rpc_task *task)
1815{
Chuck Lever906462a2007-09-11 18:00:47 -04001816 int status = -EIO;
Chuck Leverda351872005-08-11 16:25:11 -04001817
1818 if (task->tk_status >= 0) {
Chuck Lever46121cf2007-01-31 12:14:08 -05001819 dprint_status(task);
Chuck Leverda351872005-08-11 16:25:11 -04001820 task->tk_status = 0;
1821 task->tk_action = call_connect;
1822 return;
1823 }
1824
Steve Dickson5753cba2012-02-06 10:08:08 -05001825 trace_rpc_bind_status(task);
Chuck Leverda351872005-08-11 16:25:11 -04001826 switch (task->tk_status) {
Trond Myklebust381ba742008-07-07 12:18:53 -04001827 case -ENOMEM:
1828 dprintk("RPC: %5u rpcbind out of memory\n", task->tk_pid);
1829 rpc_delay(task, HZ >> 2);
Chuck Lever2429cbf2007-09-11 18:00:41 -04001830 goto retry_timeout;
Chuck Leverda351872005-08-11 16:25:11 -04001831 case -EACCES:
Chuck Lever46121cf2007-01-31 12:14:08 -05001832 dprintk("RPC: %5u remote rpcbind: RPC program/version "
1833 "unavailable\n", task->tk_pid);
Chuck Leverb79dc8c2007-09-11 18:00:52 -04001834 /* fail immediately if this is an RPC ping */
1835 if (task->tk_msg.rpc_proc->p_proc == 0) {
1836 status = -EOPNOTSUPP;
1837 break;
1838 }
Trond Myklebust0b760112011-05-31 15:15:34 -04001839 if (task->tk_rebind_retry == 0)
1840 break;
1841 task->tk_rebind_retry--;
Chuck Leverea635a52005-10-06 23:12:58 -04001842 rpc_delay(task, 3*HZ);
Trond Myklebustda45828e2006-08-31 15:44:52 -04001843 goto retry_timeout;
Chuck Leverda351872005-08-11 16:25:11 -04001844 case -ETIMEDOUT:
Chuck Lever46121cf2007-01-31 12:14:08 -05001845 dprintk("RPC: %5u rpcbind request timed out\n",
Chuck Leverda351872005-08-11 16:25:11 -04001846 task->tk_pid);
Trond Myklebustda45828e2006-08-31 15:44:52 -04001847 goto retry_timeout;
Chuck Leverda351872005-08-11 16:25:11 -04001848 case -EPFNOSUPPORT:
Chuck Lever906462a2007-09-11 18:00:47 -04001849 /* server doesn't support any rpcbind version we know of */
Chuck Lever012da152009-12-03 15:58:56 -05001850 dprintk("RPC: %5u unrecognized remote rpcbind service\n",
Chuck Leverda351872005-08-11 16:25:11 -04001851 task->tk_pid);
1852 break;
1853 case -EPROTONOSUPPORT:
Chuck Lever00a6e7b2007-03-29 16:48:33 -04001854 dprintk("RPC: %5u remote rpcbind version unavailable, retrying\n",
Chuck Leverda351872005-08-11 16:25:11 -04001855 task->tk_pid);
Trond Myklebustfdb63dc2014-03-17 12:57:31 -04001856 goto retry_timeout;
Chuck Lever012da152009-12-03 15:58:56 -05001857 case -ECONNREFUSED: /* connection problems */
1858 case -ECONNRESET:
Trond Myklebustdf277272013-12-31 13:11:43 -05001859 case -ECONNABORTED:
Chuck Lever012da152009-12-03 15:58:56 -05001860 case -ENOTCONN:
1861 case -EHOSTDOWN:
Trond Myklebusteb5b46f2017-11-30 07:21:33 -05001862 case -ENETDOWN:
Chuck Lever012da152009-12-03 15:58:56 -05001863 case -EHOSTUNREACH:
1864 case -ENETUNREACH:
Trond Myklebust3601c4a2014-06-30 13:42:19 -04001865 case -ENOBUFS:
Chuck Lever012da152009-12-03 15:58:56 -05001866 case -EPIPE:
1867 dprintk("RPC: %5u remote rpcbind unreachable: %d\n",
1868 task->tk_pid, task->tk_status);
1869 if (!RPC_IS_SOFTCONN(task)) {
1870 rpc_delay(task, 5*HZ);
1871 goto retry_timeout;
1872 }
1873 status = task->tk_status;
1874 break;
Chuck Leverda351872005-08-11 16:25:11 -04001875 default:
Chuck Lever46121cf2007-01-31 12:14:08 -05001876 dprintk("RPC: %5u unrecognized rpcbind error (%d)\n",
Chuck Leverda351872005-08-11 16:25:11 -04001877 task->tk_pid, -task->tk_status);
Chuck Leverda351872005-08-11 16:25:11 -04001878 }
1879
1880 rpc_exit(task, status);
1881 return;
1882
Trond Myklebustda45828e2006-08-31 15:44:52 -04001883retry_timeout:
Trond Myklebustfdb63dc2014-03-17 12:57:31 -04001884 task->tk_status = 0;
Trond Myklebustda45828e2006-08-31 15:44:52 -04001885 task->tk_action = call_timeout;
Chuck Leverda351872005-08-11 16:25:11 -04001886}
1887
1888/*
1889 * 4b. Connect to the RPC server
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 */
1891static void
1892call_connect(struct rpc_task *task)
1893{
Trond Myklebustad2368d2013-01-08 10:08:33 -05001894 struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895
Chuck Lever46121cf2007-01-31 12:14:08 -05001896 dprintk("RPC: %5u call_connect xprt %p %s connected\n",
Chuck Leverda351872005-08-11 16:25:11 -04001897 task->tk_pid, xprt,
1898 (xprt_connected(xprt) ? "is" : "is not"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899
Chuck Leverda351872005-08-11 16:25:11 -04001900 task->tk_action = call_transmit;
1901 if (!xprt_connected(xprt)) {
1902 task->tk_action = call_connect_status;
1903 if (task->tk_status < 0)
1904 return;
Trond Myklebust786615b2013-08-05 16:04:47 -04001905 if (task->tk_flags & RPC_TASK_NOCONNECT) {
1906 rpc_exit(task, -ENOTCONN);
1907 return;
1908 }
Chuck Leverda351872005-08-11 16:25:11 -04001909 xprt_connect(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911}
1912
1913/*
Chuck Leverda351872005-08-11 16:25:11 -04001914 * 4c. Sort out connect result
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 */
1916static void
1917call_connect_status(struct rpc_task *task)
1918{
1919 struct rpc_clnt *clnt = task->tk_client;
1920 int status = task->tk_status;
1921
Trond Myklebust9bd11522018-11-30 12:48:47 -05001922 /* Check if the task was already transmitted */
1923 if (!test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate)) {
1924 xprt_end_transmit(task);
1925 task->tk_action = call_transmit_status;
1926 return;
1927 }
1928
Chuck Lever46121cf2007-01-31 12:14:08 -05001929 dprint_status(task);
Chuck Leverda351872005-08-11 16:25:11 -04001930
Chuck Levere671edb2018-03-16 10:33:44 -04001931 trace_rpc_connect_status(task);
Trond Myklebust561ec162013-09-26 15:22:45 -04001932 task->tk_status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 switch (status) {
Trond Myklebust3ed5e2a2013-03-04 17:29:33 -05001934 case -ECONNREFUSED:
NeilBrownfd01b252017-08-18 17:12:51 +10001935 /* A positive refusal suggests a rebind is needed. */
1936 if (RPC_IS_SOFTCONN(task))
1937 break;
1938 if (clnt->cl_autobind) {
1939 rpc_force_rebind(clnt);
1940 task->tk_action = call_bind;
1941 return;
1942 }
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05001943 /* fall through */
Trond Myklebust3ed5e2a2013-03-04 17:29:33 -05001944 case -ECONNRESET:
Trond Myklebustdf277272013-12-31 13:11:43 -05001945 case -ECONNABORTED:
Trond Myklebusteb5b46f2017-11-30 07:21:33 -05001946 case -ENETDOWN:
Trond Myklebust3ed5e2a2013-03-04 17:29:33 -05001947 case -ENETUNREACH:
Trond Myklebustdf277272013-12-31 13:11:43 -05001948 case -EHOSTUNREACH:
Trond Myklebust3913c782015-02-08 21:44:04 -05001949 case -EADDRINUSE:
Trond Myklebust3601c4a2014-06-30 13:42:19 -04001950 case -ENOBUFS:
Trond Myklebust2fc193c2014-07-03 00:02:57 -04001951 case -EPIPE:
NeilBrown2c2ee6d2016-11-23 14:44:58 +11001952 xprt_conditional_disconnect(task->tk_rqstp->rq_xprt,
1953 task->tk_rqstp->rq_connect_cookie);
Trond Myklebust3ed5e2a2013-03-04 17:29:33 -05001954 if (RPC_IS_SOFTCONN(task))
1955 break;
Steve Dickson1fa3e2e2014-03-20 11:23:03 -04001956 /* retry with existing socket, after a delay */
1957 rpc_delay(task, 3*HZ);
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05001958 /* fall through */
Trond Myklebust0445f922018-12-17 13:34:59 -05001959 case -ENOTCONN:
Trond Myklebust3ed5e2a2013-03-04 17:29:33 -05001960 case -EAGAIN:
Trond Myklebust485f2252014-03-17 12:51:44 -04001961 /* Check for timeouts before looping back to call_bind */
1962 case -ETIMEDOUT:
1963 task->tk_action = call_timeout;
Trond Myklebust561ec162013-09-26 15:22:45 -04001964 return;
1965 case 0:
Trond Myklebust3ed5e2a2013-03-04 17:29:33 -05001966 clnt->cl_stats->netreconn++;
1967 task->tk_action = call_transmit;
1968 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 }
Trond Myklebust3ed5e2a2013-03-04 17:29:33 -05001970 rpc_exit(task, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971}
1972
1973/*
1974 * 5. Transmit the RPC request, and wait for reply
1975 */
1976static void
1977call_transmit(struct rpc_task *task)
1978{
Chuck Lever46121cf2007-01-31 12:14:08 -05001979 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980
Trond Myklebustc5445772018-09-03 23:39:27 -04001981 task->tk_status = 0;
1982 if (test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate)) {
1983 if (!xprt_prepare_transmit(task))
1984 return;
1985 xprt_transmit(task);
1986 }
Trond Myklebust78b576c2018-08-28 09:20:10 -04001987 task->tk_action = call_transmit_status;
Trond Myklebustc5445772018-09-03 23:39:27 -04001988 xprt_end_transmit(task);
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04001989}
1990
1991/*
1992 * 5a. Handle cleanup after a transmission
1993 */
1994static void
1995call_transmit_status(struct rpc_task *task)
1996{
1997 task->tk_action = call_status;
Chuck Lever206a1342009-12-03 15:58:56 -05001998
1999 /*
2000 * Common case: success. Force the compiler to put this
2001 * test first.
2002 */
2003 if (task->tk_status == 0) {
Trond Myklebust7f3a1d12018-08-23 00:03:43 -04002004 xprt_request_wait_receive(task);
Chuck Lever206a1342009-12-03 15:58:56 -05002005 return;
2006 }
2007
Trond Myklebust15f081c2009-03-11 14:37:57 -04002008 switch (task->tk_status) {
Trond Myklebust15f081c2009-03-11 14:37:57 -04002009 default:
Chuck Lever206a1342009-12-03 15:58:56 -05002010 dprint_status(task);
Trond Myklebust75891f52018-09-03 17:37:36 -04002011 break;
Trond Myklebust78b576c2018-08-28 09:20:10 -04002012 case -EBADMSG:
Trond Myklebust762e4e62018-08-24 16:28:28 -04002013 task->tk_status = 0;
2014 task->tk_action = call_encode;
Trond Myklebust78b576c2018-08-28 09:20:10 -04002015 break;
Trond Myklebust15f081c2009-03-11 14:37:57 -04002016 /*
2017 * Special cases: if we've been waiting on the
2018 * socket's write_space() callback, or if the
2019 * socket just returned a connection error,
2020 * then hold onto the transport lock.
2021 */
Trond Myklebust78b576c2018-08-28 09:20:10 -04002022 case -ENOBUFS:
2023 rpc_delay(task, HZ>>2);
2024 /* fall through */
Trond Myklebustc5445772018-09-03 23:39:27 -04002025 case -EBADSLT:
Trond Myklebust78b576c2018-08-28 09:20:10 -04002026 case -EAGAIN:
2027 task->tk_action = call_transmit;
2028 task->tk_status = 0;
2029 break;
Trond Myklebust15f081c2009-03-11 14:37:57 -04002030 case -ECONNREFUSED:
Trond Myklebust15f081c2009-03-11 14:37:57 -04002031 case -EHOSTDOWN:
Trond Myklebusteb5b46f2017-11-30 07:21:33 -05002032 case -ENETDOWN:
Trond Myklebust15f081c2009-03-11 14:37:57 -04002033 case -EHOSTUNREACH:
2034 case -ENETUNREACH:
Jason Baron3dedbb52014-09-24 18:08:04 +00002035 case -EPERM:
Chuck Lever09a21c42009-12-03 15:58:56 -05002036 if (RPC_IS_SOFTCONN(task)) {
Chuck Levera25a4cb2018-03-16 10:33:55 -04002037 if (!task->tk_msg.rpc_proc->p_proc)
2038 trace_xprt_ping(task->tk_xprt,
2039 task->tk_status);
Chuck Lever09a21c42009-12-03 15:58:56 -05002040 rpc_exit(task, task->tk_status);
2041 break;
2042 }
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05002043 /* fall through */
Chuck Lever09a21c42009-12-03 15:58:56 -05002044 case -ECONNRESET:
Trond Myklebustdf277272013-12-31 13:11:43 -05002045 case -ECONNABORTED:
Trond Myklebust3913c782015-02-08 21:44:04 -05002046 case -EADDRINUSE:
Chuck Lever09a21c42009-12-03 15:58:56 -05002047 case -ENOTCONN:
Trond Myklebustc8485e42009-03-11 14:37:59 -04002048 case -EPIPE:
Trond Myklebust7ebbbc62018-08-28 09:00:27 -04002049 break;
Trond Myklebust15f081c2009-03-11 14:37:57 -04002050 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051}
2052
Trond Myklebust9e00abc2011-07-13 19:20:49 -04002053#if defined(CONFIG_SUNRPC_BACKCHANNEL)
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04002054/*
2055 * 5b. Send the backchannel RPC reply. On error, drop the reply. In
2056 * addition, disconnect on connectivity errors.
2057 */
2058static void
2059call_bc_transmit(struct rpc_task *task)
2060{
2061 struct rpc_rqst *req = task->tk_rqstp;
2062
Trond Myklebust762e4e62018-08-24 16:28:28 -04002063 if (rpc_task_need_encode(task))
2064 xprt_request_enqueue_transmit(task);
Trond Myklebust902c5882018-09-01 17:21:01 -04002065 if (!test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate))
2066 goto out_wakeup;
Trond Myklebust762e4e62018-08-24 16:28:28 -04002067
Trond Myklebust1193d582015-06-02 11:53:21 -04002068 if (!xprt_prepare_transmit(task))
2069 goto out_retry;
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04002070
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04002071 if (task->tk_status < 0) {
2072 printk(KERN_NOTICE "RPC: Could not send backchannel reply "
2073 "error: %d\n", task->tk_status);
Trond Myklebust1193d582015-06-02 11:53:21 -04002074 goto out_done;
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04002075 }
2076
2077 xprt_transmit(task);
Trond Myklebust1193d582015-06-02 11:53:21 -04002078
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04002079 xprt_end_transmit(task);
2080 dprint_status(task);
2081 switch (task->tk_status) {
2082 case 0:
2083 /* Success */
Trond Myklebusteb5b46f2017-11-30 07:21:33 -05002084 case -ENETDOWN:
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04002085 case -EHOSTDOWN:
2086 case -EHOSTUNREACH:
2087 case -ENETUNREACH:
Trond Myklebust38325912015-06-19 13:04:13 -04002088 case -ECONNRESET:
2089 case -ECONNREFUSED:
2090 case -EADDRINUSE:
2091 case -ENOTCONN:
2092 case -EPIPE:
2093 break;
Trond Myklebustc5445772018-09-03 23:39:27 -04002094 case -EAGAIN:
2095 goto out_retry;
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04002096 case -ETIMEDOUT:
2097 /*
2098 * Problem reaching the server. Disconnect and let the
2099 * forechannel reestablish the connection. The server will
2100 * have to retransmit the backchannel request and we'll
2101 * reprocess it. Since these ops are idempotent, there's no
2102 * need to cache our reply at this time.
2103 */
2104 printk(KERN_NOTICE "RPC: Could not send backchannel reply "
2105 "error: %d\n", task->tk_status);
Trond Myklebusta4f08352013-01-08 09:10:21 -05002106 xprt_conditional_disconnect(req->rq_xprt,
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04002107 req->rq_connect_cookie);
2108 break;
2109 default:
2110 /*
2111 * We were unable to reply and will have to drop the
2112 * request. The server should reconnect and retransmit.
2113 */
Weston Andros Adamson1facf4c2012-10-23 10:43:30 -04002114 WARN_ON_ONCE(task->tk_status == -EAGAIN);
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04002115 printk(KERN_NOTICE "RPC: Could not send backchannel reply "
2116 "error: %d\n", task->tk_status);
2117 break;
2118 }
Trond Myklebust902c5882018-09-01 17:21:01 -04002119out_wakeup:
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04002120 rpc_wake_up_queued_task(&req->rq_xprt->pending, task);
Trond Myklebust1193d582015-06-02 11:53:21 -04002121out_done:
2122 task->tk_action = rpc_exit_task;
2123 return;
Trond Myklebust1193d582015-06-02 11:53:21 -04002124out_retry:
2125 task->tk_status = 0;
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04002126}
Trond Myklebust9e00abc2011-07-13 19:20:49 -04002127#endif /* CONFIG_SUNRPC_BACKCHANNEL */
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04002128
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129/*
2130 * 6. Sort out the RPC call status
2131 */
2132static void
2133call_status(struct rpc_task *task)
2134{
2135 struct rpc_clnt *clnt = task->tk_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 int status;
2137
Chuck Levera25a4cb2018-03-16 10:33:55 -04002138 if (!task->tk_msg.rpc_proc->p_proc)
2139 trace_xprt_ping(task->tk_xprt, task->tk_status);
2140
Chuck Lever46121cf2007-01-31 12:14:08 -05002141 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142
2143 status = task->tk_status;
2144 if (status >= 0) {
2145 task->tk_action = call_decode;
2146 return;
2147 }
2148
Steve Dickson5753cba2012-02-06 10:08:08 -05002149 trace_rpc_call_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 task->tk_status = 0;
2151 switch(status) {
Trond Myklebust76303992006-08-30 14:32:49 -04002152 case -EHOSTDOWN:
Trond Myklebusteb5b46f2017-11-30 07:21:33 -05002153 case -ENETDOWN:
Trond Myklebust76303992006-08-30 14:32:49 -04002154 case -EHOSTUNREACH:
2155 case -ENETUNREACH:
Jason Baron3dedbb52014-09-24 18:08:04 +00002156 case -EPERM:
Trond Myklebust9455e3f2014-03-19 13:25:43 -04002157 if (RPC_IS_SOFTCONN(task)) {
2158 rpc_exit(task, status);
2159 break;
2160 }
Trond Myklebust76303992006-08-30 14:32:49 -04002161 /*
2162 * Delay any retries for 3 seconds, then handle as if it
2163 * were a timeout.
2164 */
2165 rpc_delay(task, 3*HZ);
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05002166 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 case -ETIMEDOUT:
2168 task->tk_action = call_timeout;
2169 break;
2170 case -ECONNREFUSED:
Trond Myklebustdf277272013-12-31 13:11:43 -05002171 case -ECONNRESET:
2172 case -ECONNABORTED:
Chuck Lever35f5a422006-01-03 09:55:50 +01002173 rpc_force_rebind(clnt);
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05002174 /* fall through */
Trond Myklebust3913c782015-02-08 21:44:04 -05002175 case -EADDRINUSE:
Trond Myklebustc8485e42009-03-11 14:37:59 -04002176 rpc_delay(task, 3*HZ);
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05002177 /* fall through */
Trond Myklebustc8485e42009-03-11 14:37:59 -04002178 case -EPIPE:
2179 case -ENOTCONN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 case -EAGAIN:
Trond Myklebust762e4e62018-08-24 16:28:28 -04002181 task->tk_action = call_encode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 break;
2183 case -EIO:
2184 /* shutdown or soft timeout */
2185 rpc_exit(task, status);
2186 break;
2187 default:
Olga Kornievskaiab6b61522008-06-09 16:51:31 -04002188 if (clnt->cl_chatty)
2189 printk("%s: RPC call returned error %d\n",
Trond Myklebust55909f22013-08-23 11:48:15 -04002190 clnt->cl_program->name, -status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 rpc_exit(task, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 }
2193}
2194
2195/*
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04002196 * 6a. Handle RPC timeout
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 * We do not release the request slot, so we keep using the
2198 * same XID for all retransmits.
2199 */
2200static void
2201call_timeout(struct rpc_task *task)
2202{
2203 struct rpc_clnt *clnt = task->tk_client;
2204
2205 if (xprt_adjust_timeout(task->tk_rqstp) == 0) {
Chuck Lever46121cf2007-01-31 12:14:08 -05002206 dprintk("RPC: %5u call_timeout (minor)\n", task->tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 goto retry;
2208 }
2209
Chuck Lever46121cf2007-01-31 12:14:08 -05002210 dprintk("RPC: %5u call_timeout (major)\n", task->tk_pid);
Chuck Leveref759a22006-03-20 13:44:17 -05002211 task->tk_timeouts++;
2212
Chuck Lever3a28bec2009-12-03 15:58:56 -05002213 if (RPC_IS_SOFTCONN(task)) {
2214 rpc_exit(task, -ETIMEDOUT);
2215 return;
2216 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 if (RPC_IS_SOFT(task)) {
Joe Perchescac5d072012-07-18 11:17:11 -07002218 if (clnt->cl_chatty) {
Olga Kornievskaiab6b61522008-06-09 16:51:31 -04002219 printk(KERN_NOTICE "%s: server %s not responding, timed out\n",
Trond Myklebust55909f22013-08-23 11:48:15 -04002220 clnt->cl_program->name,
Trond Myklebustfb43d172016-01-30 16:39:26 -05002221 task->tk_xprt->servername);
Joe Perchescac5d072012-07-18 11:17:11 -07002222 }
Trond Myklebust7494d002011-04-24 14:28:45 -04002223 if (task->tk_flags & RPC_TASK_TIMEOUT)
2224 rpc_exit(task, -ETIMEDOUT);
2225 else
2226 rpc_exit(task, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 return;
2228 }
2229
Chuck Leverf518e35a2006-01-03 09:55:52 +01002230 if (!(task->tk_flags & RPC_CALL_MAJORSEEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 task->tk_flags |= RPC_CALL_MAJORSEEN;
Trond Myklebust4e0038b2012-03-01 17:01:05 -05002232 if (clnt->cl_chatty) {
Olga Kornievskaiab6b61522008-06-09 16:51:31 -04002233 printk(KERN_NOTICE "%s: server %s not responding, still trying\n",
Trond Myklebust55909f22013-08-23 11:48:15 -04002234 clnt->cl_program->name,
Trond Myklebustfb43d172016-01-30 16:39:26 -05002235 task->tk_xprt->servername);
Trond Myklebust4e0038b2012-03-01 17:01:05 -05002236 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 }
Chuck Lever35f5a422006-01-03 09:55:50 +01002238 rpc_force_rebind(clnt);
Trond Myklebustb48633b2008-04-22 16:47:55 -04002239 /*
2240 * Did our request time out due to an RPCSEC_GSS out-of-sequence
2241 * event? RFC2203 requires the server to drop all such requests.
2242 */
2243 rpcauth_invalcred(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244
2245retry:
Trond Myklebust762e4e62018-08-24 16:28:28 -04002246 task->tk_action = call_encode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 task->tk_status = 0;
2248}
2249
2250/*
2251 * 7. Decode the RPC reply
2252 */
2253static void
2254call_decode(struct rpc_task *task)
2255{
2256 struct rpc_clnt *clnt = task->tk_client;
2257 struct rpc_rqst *req = task->tk_rqstp;
Chuck Leverbf269552010-12-14 14:59:29 +00002258 kxdrdproc_t decode = task->tk_msg.rpc_proc->p_decode;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07002259 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260
Vasily Averin726fd6a2011-06-01 16:23:59 +04002261 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262
Trond Myklebust9ee94d32018-08-28 16:27:31 -04002263 if (!decode) {
2264 task->tk_action = rpc_exit_task;
2265 return;
2266 }
2267
Chuck Leverf518e35a2006-01-03 09:55:52 +01002268 if (task->tk_flags & RPC_CALL_MAJORSEEN) {
Trond Myklebust4e0038b2012-03-01 17:01:05 -05002269 if (clnt->cl_chatty) {
Olga Kornievskaiab6b61522008-06-09 16:51:31 -04002270 printk(KERN_NOTICE "%s: server %s OK\n",
Trond Myklebust55909f22013-08-23 11:48:15 -04002271 clnt->cl_program->name,
Trond Myklebustfb43d172016-01-30 16:39:26 -05002272 task->tk_xprt->servername);
Trond Myklebust4e0038b2012-03-01 17:01:05 -05002273 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 task->tk_flags &= ~RPC_CALL_MAJORSEEN;
2275 }
2276
Trond Myklebust43ac3f22006-03-20 13:44:51 -05002277 /*
2278 * Ensure that we see all writes made by xprt_complete_rqst()
Ricardo Labiagadd2b63d2009-04-01 09:23:28 -04002279 * before it changed req->rq_reply_bytes_recvd.
Trond Myklebust43ac3f22006-03-20 13:44:51 -05002280 */
2281 smp_rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 req->rq_rcv_buf.len = req->rq_private_buf.len;
2283
2284 /* Check that the softirq receive buffer is valid */
2285 WARN_ON(memcmp(&req->rq_rcv_buf, &req->rq_private_buf,
2286 sizeof(req->rq_rcv_buf)) != 0);
2287
Trond Myklebust1e799b62008-03-21 16:19:41 -04002288 if (req->rq_rcv_buf.len < 12) {
2289 if (!RPC_IS_SOFT(task)) {
Trond Myklebust762e4e62018-08-24 16:28:28 -04002290 task->tk_action = call_encode;
Trond Myklebust1e799b62008-03-21 16:19:41 -04002291 goto out_retry;
2292 }
2293 dprintk("RPC: %s: too small RPC reply size (%d bytes)\n",
Trond Myklebust55909f22013-08-23 11:48:15 -04002294 clnt->cl_program->name, task->tk_status);
Trond Myklebust1e799b62008-03-21 16:19:41 -04002295 task->tk_action = call_timeout;
2296 goto out_retry;
2297 }
2298
Chuck Leverb0e1c572008-05-21 17:09:19 -04002299 p = rpc_verify_header(task);
Trond Myklebustabbcf282006-01-03 09:55:03 +01002300 if (IS_ERR(p)) {
2301 if (p == ERR_PTR(-EAGAIN))
2302 goto out_retry;
2303 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 }
Trond Myklebustabbcf282006-01-03 09:55:03 +01002305 task->tk_action = rpc_exit_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306
Trond Myklebust9ee94d32018-08-28 16:27:31 -04002307 task->tk_status = rpcauth_unwrap_resp(task, decode, req, p,
2308 task->tk_msg.rpc_resp);
2309
Chuck Lever46121cf2007-01-31 12:14:08 -05002310 dprintk("RPC: %5u call_decode result %d\n", task->tk_pid,
2311 task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 return;
2313out_retry:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 task->tk_status = 0;
Chuck Leverb0e1c572008-05-21 17:09:19 -04002315 /* Note: rpc_verify_header() may have freed the RPC slot */
Trond Myklebust24b74bf2008-04-19 13:15:47 -04002316 if (task->tk_rqstp == req) {
Trond Myklebust71700bb2018-11-30 16:11:15 -05002317 xdr_free_bvec(&req->rq_rcv_buf);
Ricardo Labiagadd2b63d2009-04-01 09:23:28 -04002318 req->rq_reply_bytes_recvd = req->rq_rcv_buf.len = 0;
Trond Myklebust24b74bf2008-04-19 13:15:47 -04002319 if (task->tk_client->cl_discrtry)
Trond Myklebusta4f08352013-01-08 09:10:21 -05002320 xprt_conditional_disconnect(req->rq_xprt,
Trond Myklebust7c1d71c2008-04-17 16:52:57 -04002321 req->rq_connect_cookie);
Trond Myklebust24b74bf2008-04-19 13:15:47 -04002322 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323}
2324
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07002325static __be32 *
Chuck Leverb0e1c572008-05-21 17:09:19 -04002326rpc_encode_header(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327{
2328 struct rpc_clnt *clnt = task->tk_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 struct rpc_rqst *req = task->tk_rqstp;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07002330 __be32 *p = req->rq_svec[0].iov_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331
2332 /* FIXME: check buffer size? */
Chuck Lever808012f2005-08-25 16:25:49 -07002333
Trond Myklebusta4f08352013-01-08 09:10:21 -05002334 p = xprt_skip_transport_header(req->rq_xprt, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 *p++ = req->rq_xid; /* XID */
2336 *p++ = htonl(RPC_CALL); /* CALL */
2337 *p++ = htonl(RPC_VERSION); /* RPC version */
2338 *p++ = htonl(clnt->cl_prog); /* program number */
2339 *p++ = htonl(clnt->cl_vers); /* program version */
2340 *p++ = htonl(task->tk_msg.rpc_proc->p_proc); /* procedure */
Trond Myklebust334ccfd2005-06-22 17:16:19 +00002341 p = rpcauth_marshcred(task, p);
Trond Myklebust97b78ae2019-01-02 17:53:13 -05002342 if (p)
2343 req->rq_slen = xdr_adjust_iovec(&req->rq_svec[0], p);
Trond Myklebust334ccfd2005-06-22 17:16:19 +00002344 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345}
2346
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07002347static __be32 *
Chuck Leverb0e1c572008-05-21 17:09:19 -04002348rpc_verify_header(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349{
Trond Myklebust4e0038b2012-03-01 17:01:05 -05002350 struct rpc_clnt *clnt = task->tk_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 struct kvec *iov = &task->tk_rqstp->rq_rcv_buf.head[0];
2352 int len = task->tk_rqstp->rq_rcv_buf.len >> 2;
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07002353 __be32 *p = iov->iov_base;
2354 u32 n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 int error = -EACCES;
2356
David Howellse8896492006-08-24 15:44:19 -04002357 if ((task->tk_rqstp->rq_rcv_buf.len & 3) != 0) {
2358 /* RFC-1014 says that the representation of XDR data must be a
2359 * multiple of four bytes
2360 * - if it isn't pointer subtraction in the NFS client may give
2361 * undefined results
2362 */
Trond Myklebust8a702bb2007-06-27 18:30:26 -04002363 dprintk("RPC: %5u %s: XDR representation not a multiple of"
Harvey Harrison0dc47872008-03-05 20:47:47 -08002364 " 4 bytes: 0x%x\n", task->tk_pid, __func__,
Trond Myklebust8a702bb2007-06-27 18:30:26 -04002365 task->tk_rqstp->rq_rcv_buf.len);
Andy Adamson35fa5f72013-08-14 11:59:17 -04002366 error = -EIO;
2367 goto out_err;
David Howellse8896492006-08-24 15:44:19 -04002368 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 if ((len -= 3) < 0)
2370 goto out_overflow;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04002372 p += 1; /* skip XID */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 if ((n = ntohl(*p++)) != RPC_REPLY) {
Trond Myklebust8a702bb2007-06-27 18:30:26 -04002374 dprintk("RPC: %5u %s: not an RPC reply: %x\n",
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04002375 task->tk_pid, __func__, n);
Andy Adamson35fa5f72013-08-14 11:59:17 -04002376 error = -EIO;
Trond Myklebustabbcf282006-01-03 09:55:03 +01002377 goto out_garbage;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 }
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04002379
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 if ((n = ntohl(*p++)) != RPC_MSG_ACCEPTED) {
2381 if (--len < 0)
2382 goto out_overflow;
2383 switch ((n = ntohl(*p++))) {
Joe Perches89f0e4f2011-07-01 09:43:12 +00002384 case RPC_AUTH_ERROR:
2385 break;
2386 case RPC_MISMATCH:
2387 dprintk("RPC: %5u %s: RPC call version mismatch!\n",
2388 task->tk_pid, __func__);
2389 error = -EPROTONOSUPPORT;
2390 goto out_err;
2391 default:
2392 dprintk("RPC: %5u %s: RPC call rejected, "
2393 "unknown error: %x\n",
2394 task->tk_pid, __func__, n);
Andy Adamson35fa5f72013-08-14 11:59:17 -04002395 error = -EIO;
2396 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 }
2398 if (--len < 0)
2399 goto out_overflow;
2400 switch ((n = ntohl(*p++))) {
2401 case RPC_AUTH_REJECTEDCRED:
2402 case RPC_AUTH_REJECTEDVERF:
2403 case RPCSEC_GSS_CREDPROBLEM:
2404 case RPCSEC_GSS_CTXPROBLEM:
2405 if (!task->tk_cred_retry)
2406 break;
2407 task->tk_cred_retry--;
Chuck Lever46121cf2007-01-31 12:14:08 -05002408 dprintk("RPC: %5u %s: retry stale creds\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08002409 task->tk_pid, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 rpcauth_invalcred(task);
Trond Myklebust220bcc22007-10-01 12:06:48 -04002411 /* Ensure we obtain a new XID! */
2412 xprt_release(task);
Trond Myklebust118df3d2010-10-24 17:17:31 -04002413 task->tk_action = call_reserve;
Trond Myklebustabbcf282006-01-03 09:55:03 +01002414 goto out_retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 case RPC_AUTH_BADCRED:
2416 case RPC_AUTH_BADVERF:
2417 /* possibly garbled cred/verf? */
2418 if (!task->tk_garb_retry)
2419 break;
2420 task->tk_garb_retry--;
Chuck Lever46121cf2007-01-31 12:14:08 -05002421 dprintk("RPC: %5u %s: retry garbled creds\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08002422 task->tk_pid, __func__);
Trond Myklebust762e4e62018-08-24 16:28:28 -04002423 task->tk_action = call_encode;
Trond Myklebustabbcf282006-01-03 09:55:03 +01002424 goto out_retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 case RPC_AUTH_TOOWEAK:
Chuck Leverb0e1c572008-05-21 17:09:19 -04002426 printk(KERN_NOTICE "RPC: server %s requires stronger "
Trond Myklebust4e0038b2012-03-01 17:01:05 -05002427 "authentication.\n",
Trond Myklebustfb43d172016-01-30 16:39:26 -05002428 task->tk_xprt->servername);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 break;
2430 default:
Trond Myklebust8a702bb2007-06-27 18:30:26 -04002431 dprintk("RPC: %5u %s: unknown auth error: %x\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08002432 task->tk_pid, __func__, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 error = -EIO;
2434 }
Chuck Lever46121cf2007-01-31 12:14:08 -05002435 dprintk("RPC: %5u %s: call rejected %d\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08002436 task->tk_pid, __func__, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 goto out_err;
2438 }
Andy Adamson35fa5f72013-08-14 11:59:17 -04002439 p = rpcauth_checkverf(task, p);
2440 if (IS_ERR(p)) {
2441 error = PTR_ERR(p);
2442 dprintk("RPC: %5u %s: auth check failed with %d\n",
2443 task->tk_pid, __func__, error);
Trond Myklebustabbcf282006-01-03 09:55:03 +01002444 goto out_garbage; /* bad verifier, retry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 }
Alexey Dobriyand8ed0292006-09-26 22:29:38 -07002446 len = p - (__be32 *)iov->iov_base - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 if (len < 0)
2448 goto out_overflow;
2449 switch ((n = ntohl(*p++))) {
2450 case RPC_SUCCESS:
2451 return p;
2452 case RPC_PROG_UNAVAIL:
Trond Myklebustfb43d172016-01-30 16:39:26 -05002453 dprintk("RPC: %5u %s: program %u is unsupported "
Trond Myklebust4e0038b2012-03-01 17:01:05 -05002454 "by server %s\n", task->tk_pid, __func__,
2455 (unsigned int)clnt->cl_prog,
Trond Myklebustfb43d172016-01-30 16:39:26 -05002456 task->tk_xprt->servername);
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00002457 error = -EPFNOSUPPORT;
2458 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 case RPC_PROG_MISMATCH:
Trond Myklebustfb43d172016-01-30 16:39:26 -05002460 dprintk("RPC: %5u %s: program %u, version %u unsupported "
Trond Myklebust4e0038b2012-03-01 17:01:05 -05002461 "by server %s\n", task->tk_pid, __func__,
2462 (unsigned int)clnt->cl_prog,
2463 (unsigned int)clnt->cl_vers,
Trond Myklebustfb43d172016-01-30 16:39:26 -05002464 task->tk_xprt->servername);
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00002465 error = -EPROTONOSUPPORT;
2466 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 case RPC_PROC_UNAVAIL:
Trond Myklebustfb43d172016-01-30 16:39:26 -05002468 dprintk("RPC: %5u %s: proc %s unsupported by program %u, "
Chuck Lever46121cf2007-01-31 12:14:08 -05002469 "version %u on server %s\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08002470 task->tk_pid, __func__,
Chuck Lever3748f1e2008-05-21 17:09:12 -04002471 rpc_proc_name(task),
Trond Myklebust4e0038b2012-03-01 17:01:05 -05002472 clnt->cl_prog, clnt->cl_vers,
Trond Myklebustfb43d172016-01-30 16:39:26 -05002473 task->tk_xprt->servername);
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00002474 error = -EOPNOTSUPP;
2475 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 case RPC_GARBAGE_ARGS:
Chuck Lever46121cf2007-01-31 12:14:08 -05002477 dprintk("RPC: %5u %s: server saw garbage\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08002478 task->tk_pid, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 break; /* retry */
2480 default:
Trond Myklebust8a702bb2007-06-27 18:30:26 -04002481 dprintk("RPC: %5u %s: server accept status: %x\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08002482 task->tk_pid, __func__, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 /* Also retry */
2484 }
2485
Trond Myklebustabbcf282006-01-03 09:55:03 +01002486out_garbage:
Trond Myklebust4e0038b2012-03-01 17:01:05 -05002487 clnt->cl_stats->rpcgarbage++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 if (task->tk_garb_retry) {
2489 task->tk_garb_retry--;
Chuck Lever46121cf2007-01-31 12:14:08 -05002490 dprintk("RPC: %5u %s: retrying\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08002491 task->tk_pid, __func__);
Trond Myklebust762e4e62018-08-24 16:28:28 -04002492 task->tk_action = call_encode;
Trond Myklebustabbcf282006-01-03 09:55:03 +01002493out_retry:
2494 return ERR_PTR(-EAGAIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496out_err:
2497 rpc_exit(task, error);
Trond Myklebust8a702bb2007-06-27 18:30:26 -04002498 dprintk("RPC: %5u %s: call failed with error %d\n", task->tk_pid,
Harvey Harrison0dc47872008-03-05 20:47:47 -08002499 __func__, error);
Trond Myklebustabbcf282006-01-03 09:55:03 +01002500 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501out_overflow:
Trond Myklebust8a702bb2007-06-27 18:30:26 -04002502 dprintk("RPC: %5u %s: server reply was truncated.\n", task->tk_pid,
Harvey Harrison0dc47872008-03-05 20:47:47 -08002503 __func__);
Trond Myklebustabbcf282006-01-03 09:55:03 +01002504 goto out_garbage;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505}
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00002506
Christoph Hellwigc512f362017-05-08 09:31:19 +02002507static void rpcproc_encode_null(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
2508 const void *obj)
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00002509{
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00002510}
2511
Christoph Hellwig73c8dc12017-05-08 14:58:11 +02002512static int rpcproc_decode_null(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
2513 void *obj)
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00002514{
2515 return 0;
2516}
2517
Christoph Hellwig499b4982017-05-12 15:36:49 +02002518static const struct rpc_procinfo rpcproc_null = {
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00002519 .p_encode = rpcproc_encode_null,
2520 .p_decode = rpcproc_decode_null,
2521};
2522
Chuck Levercaabea82009-12-03 15:58:56 -05002523static int rpc_ping(struct rpc_clnt *clnt)
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00002524{
2525 struct rpc_message msg = {
2526 .rpc_proc = &rpcproc_null,
2527 };
2528 int err;
NeilBrowna68a72e2018-12-03 11:30:30 +11002529 err = rpc_call_sync(clnt, &msg, RPC_TASK_SOFT | RPC_TASK_SOFTCONN |
2530 RPC_TASK_NULLCREDS);
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00002531 return err;
2532}
Trond Myklebust188fef12007-06-16 14:18:40 -04002533
Trond Myklebust7f554892016-01-30 23:43:35 -05002534static
2535struct rpc_task *rpc_call_null_helper(struct rpc_clnt *clnt,
2536 struct rpc_xprt *xprt, struct rpc_cred *cred, int flags,
2537 const struct rpc_call_ops *ops, void *data)
Trond Myklebust5e1550d2007-06-23 10:17:16 -04002538{
2539 struct rpc_message msg = {
2540 .rpc_proc = &rpcproc_null,
Trond Myklebust5e1550d2007-06-23 10:17:16 -04002541 };
Trond Myklebust84115e12007-07-14 15:39:59 -04002542 struct rpc_task_setup task_setup_data = {
2543 .rpc_client = clnt,
Trond Myklebust7f554892016-01-30 23:43:35 -05002544 .rpc_xprt = xprt,
Trond Myklebust84115e12007-07-14 15:39:59 -04002545 .rpc_message = &msg,
NeilBrown1de7eea2018-12-03 11:30:30 +11002546 .rpc_op_cred = cred,
Trond Myklebust7f554892016-01-30 23:43:35 -05002547 .callback_ops = (ops != NULL) ? ops : &rpc_default_ops,
2548 .callback_data = data,
NeilBrowna52458b2018-12-03 11:30:31 +11002549 .flags = flags | RPC_TASK_NULLCREDS,
Trond Myklebust84115e12007-07-14 15:39:59 -04002550 };
Trond Myklebust7f554892016-01-30 23:43:35 -05002551
Trond Myklebustc970aa82007-07-14 15:39:59 -04002552 return rpc_run_task(&task_setup_data);
Trond Myklebust5e1550d2007-06-23 10:17:16 -04002553}
Trond Myklebust7f554892016-01-30 23:43:35 -05002554
2555struct rpc_task *rpc_call_null(struct rpc_clnt *clnt, struct rpc_cred *cred, int flags)
2556{
2557 return rpc_call_null_helper(clnt, NULL, cred, flags, NULL, NULL);
2558}
Trond Myklebuste8914c62007-07-14 15:39:59 -04002559EXPORT_SYMBOL_GPL(rpc_call_null);
Trond Myklebust5e1550d2007-06-23 10:17:16 -04002560
Trond Myklebust7f554892016-01-30 23:43:35 -05002561struct rpc_cb_add_xprt_calldata {
2562 struct rpc_xprt_switch *xps;
2563 struct rpc_xprt *xprt;
2564};
2565
2566static void rpc_cb_add_xprt_done(struct rpc_task *task, void *calldata)
2567{
2568 struct rpc_cb_add_xprt_calldata *data = calldata;
2569
2570 if (task->tk_status == 0)
2571 rpc_xprt_switch_add_xprt(data->xps, data->xprt);
2572}
2573
2574static void rpc_cb_add_xprt_release(void *calldata)
2575{
2576 struct rpc_cb_add_xprt_calldata *data = calldata;
2577
2578 xprt_put(data->xprt);
2579 xprt_switch_put(data->xps);
2580 kfree(data);
2581}
2582
Trond Myklebustce272302016-07-24 17:06:28 -04002583static const struct rpc_call_ops rpc_cb_add_xprt_call_ops = {
Trond Myklebust7f554892016-01-30 23:43:35 -05002584 .rpc_call_done = rpc_cb_add_xprt_done,
2585 .rpc_release = rpc_cb_add_xprt_release,
2586};
2587
2588/**
2589 * rpc_clnt_test_and_add_xprt - Test and add a new transport to a rpc_clnt
2590 * @clnt: pointer to struct rpc_clnt
2591 * @xps: pointer to struct rpc_xprt_switch,
2592 * @xprt: pointer struct rpc_xprt
2593 * @dummy: unused
2594 */
2595int rpc_clnt_test_and_add_xprt(struct rpc_clnt *clnt,
2596 struct rpc_xprt_switch *xps, struct rpc_xprt *xprt,
2597 void *dummy)
2598{
2599 struct rpc_cb_add_xprt_calldata *data;
Trond Myklebust7f554892016-01-30 23:43:35 -05002600 struct rpc_task *task;
2601
2602 data = kmalloc(sizeof(*data), GFP_NOFS);
2603 if (!data)
2604 return -ENOMEM;
2605 data->xps = xprt_switch_get(xps);
2606 data->xprt = xprt_get(xprt);
2607
NeilBrowna68a72e2018-12-03 11:30:30 +11002608 task = rpc_call_null_helper(clnt, xprt, NULL,
2609 RPC_TASK_SOFT|RPC_TASK_SOFTCONN|RPC_TASK_ASYNC|RPC_TASK_NULLCREDS,
Trond Myklebust7f554892016-01-30 23:43:35 -05002610 &rpc_cb_add_xprt_call_ops, data);
Trond Myklebust7f554892016-01-30 23:43:35 -05002611 if (IS_ERR(task))
2612 return PTR_ERR(task);
2613 rpc_put_task(task);
2614 return 1;
2615}
2616EXPORT_SYMBOL_GPL(rpc_clnt_test_and_add_xprt);
2617
2618/**
Andy Adamsonfda0ab42016-09-09 09:22:26 -04002619 * rpc_clnt_setup_test_and_add_xprt()
2620 *
2621 * This is an rpc_clnt_add_xprt setup() function which returns 1 so:
2622 * 1) caller of the test function must dereference the rpc_xprt_switch
2623 * and the rpc_xprt.
2624 * 2) test function must call rpc_xprt_switch_add_xprt, usually in
2625 * the rpc_call_done routine.
2626 *
2627 * Upon success (return of 1), the test function adds the new
2628 * transport to the rpc_clnt xprt switch
2629 *
2630 * @clnt: struct rpc_clnt to get the new transport
2631 * @xps: the rpc_xprt_switch to hold the new transport
2632 * @xprt: the rpc_xprt to test
2633 * @data: a struct rpc_add_xprt_test pointer that holds the test function
2634 * and test function call data
2635 */
2636int rpc_clnt_setup_test_and_add_xprt(struct rpc_clnt *clnt,
2637 struct rpc_xprt_switch *xps,
2638 struct rpc_xprt *xprt,
2639 void *data)
2640{
Andy Adamsonfda0ab42016-09-09 09:22:26 -04002641 struct rpc_task *task;
2642 struct rpc_add_xprt_test *xtest = (struct rpc_add_xprt_test *)data;
2643 int status = -EADDRINUSE;
2644
2645 xprt = xprt_get(xprt);
2646 xprt_switch_get(xps);
2647
2648 if (rpc_xprt_switch_has_addr(xps, (struct sockaddr *)&xprt->addr))
2649 goto out_err;
2650
2651 /* Test the connection */
NeilBrowna68a72e2018-12-03 11:30:30 +11002652 task = rpc_call_null_helper(clnt, xprt, NULL,
2653 RPC_TASK_SOFT | RPC_TASK_SOFTCONN | RPC_TASK_NULLCREDS,
Andy Adamsonfda0ab42016-09-09 09:22:26 -04002654 NULL, NULL);
Andy Adamsonfda0ab42016-09-09 09:22:26 -04002655 if (IS_ERR(task)) {
2656 status = PTR_ERR(task);
2657 goto out_err;
2658 }
2659 status = task->tk_status;
2660 rpc_put_task(task);
2661
2662 if (status < 0)
2663 goto out_err;
2664
2665 /* rpc_xprt_switch and rpc_xprt are deferrenced by add_xprt_test() */
2666 xtest->add_xprt_test(clnt, xprt, xtest->data);
2667
Santosh kumar pradhan10e037d2018-12-19 12:29:57 +05302668 xprt_put(xprt);
2669 xprt_switch_put(xps);
2670
Andy Adamsonfda0ab42016-09-09 09:22:26 -04002671 /* so that rpc_clnt_add_xprt does not call rpc_xprt_switch_add_xprt */
2672 return 1;
2673out_err:
2674 xprt_put(xprt);
2675 xprt_switch_put(xps);
2676 pr_info("RPC: rpc_clnt_test_xprt failed: %d addr %s not added\n",
2677 status, xprt->address_strings[RPC_DISPLAY_ADDR]);
2678 return status;
2679}
2680EXPORT_SYMBOL_GPL(rpc_clnt_setup_test_and_add_xprt);
2681
2682/**
Trond Myklebust7f554892016-01-30 23:43:35 -05002683 * rpc_clnt_add_xprt - Add a new transport to a rpc_clnt
2684 * @clnt: pointer to struct rpc_clnt
2685 * @xprtargs: pointer to struct xprt_create
2686 * @setup: callback to test and/or set up the connection
2687 * @data: pointer to setup function data
2688 *
2689 * Creates a new transport using the parameters set in args and
2690 * adds it to clnt.
2691 * If ping is set, then test that connectivity succeeds before
2692 * adding the new transport.
2693 *
2694 */
2695int rpc_clnt_add_xprt(struct rpc_clnt *clnt,
2696 struct xprt_create *xprtargs,
2697 int (*setup)(struct rpc_clnt *,
2698 struct rpc_xprt_switch *,
2699 struct rpc_xprt *,
2700 void *),
2701 void *data)
2702{
2703 struct rpc_xprt_switch *xps;
2704 struct rpc_xprt *xprt;
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002705 unsigned long connect_timeout;
Trond Myklebust3851f1c2016-08-04 00:08:45 -04002706 unsigned long reconnect_timeout;
Trond Myklebust7f554892016-01-30 23:43:35 -05002707 unsigned char resvport;
2708 int ret = 0;
2709
2710 rcu_read_lock();
2711 xps = xprt_switch_get(rcu_dereference(clnt->cl_xpi.xpi_xpswitch));
2712 xprt = xprt_iter_xprt(&clnt->cl_xpi);
2713 if (xps == NULL || xprt == NULL) {
2714 rcu_read_unlock();
2715 return -EAGAIN;
2716 }
2717 resvport = xprt->resvport;
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002718 connect_timeout = xprt->connect_timeout;
Trond Myklebust3851f1c2016-08-04 00:08:45 -04002719 reconnect_timeout = xprt->max_reconnect_timeout;
Trond Myklebust7f554892016-01-30 23:43:35 -05002720 rcu_read_unlock();
2721
2722 xprt = xprt_create_transport(xprtargs);
2723 if (IS_ERR(xprt)) {
2724 ret = PTR_ERR(xprt);
2725 goto out_put_switch;
2726 }
2727 xprt->resvport = resvport;
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002728 if (xprt->ops->set_connect_timeout != NULL)
2729 xprt->ops->set_connect_timeout(xprt,
2730 connect_timeout,
2731 reconnect_timeout);
Trond Myklebust7f554892016-01-30 23:43:35 -05002732
2733 rpc_xprt_switch_set_roundrobin(xps);
2734 if (setup) {
2735 ret = setup(clnt, xps, xprt, data);
2736 if (ret != 0)
2737 goto out_put_xprt;
2738 }
2739 rpc_xprt_switch_add_xprt(xps, xprt);
2740out_put_xprt:
2741 xprt_put(xprt);
2742out_put_switch:
2743 xprt_switch_put(xps);
2744 return ret;
2745}
2746EXPORT_SYMBOL_GPL(rpc_clnt_add_xprt);
2747
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002748struct connect_timeout_data {
2749 unsigned long connect_timeout;
2750 unsigned long reconnect_timeout;
2751};
2752
Trond Myklebust8d480322016-08-05 19:03:31 -04002753static int
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002754rpc_xprt_set_connect_timeout(struct rpc_clnt *clnt,
Trond Myklebust8d480322016-08-05 19:03:31 -04002755 struct rpc_xprt *xprt,
2756 void *data)
2757{
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002758 struct connect_timeout_data *timeo = data;
Trond Myklebust8d480322016-08-05 19:03:31 -04002759
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002760 if (xprt->ops->set_connect_timeout)
2761 xprt->ops->set_connect_timeout(xprt,
2762 timeo->connect_timeout,
2763 timeo->reconnect_timeout);
Trond Myklebust8d480322016-08-05 19:03:31 -04002764 return 0;
2765}
2766
2767void
Trond Myklebust26ae1022017-02-08 11:17:55 -05002768rpc_set_connect_timeout(struct rpc_clnt *clnt,
2769 unsigned long connect_timeout,
2770 unsigned long reconnect_timeout)
Trond Myklebust8d480322016-08-05 19:03:31 -04002771{
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002772 struct connect_timeout_data timeout = {
Trond Myklebust26ae1022017-02-08 11:17:55 -05002773 .connect_timeout = connect_timeout,
2774 .reconnect_timeout = reconnect_timeout,
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002775 };
Trond Myklebust8d480322016-08-05 19:03:31 -04002776 rpc_clnt_iterate_for_each_xprt(clnt,
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002777 rpc_xprt_set_connect_timeout,
2778 &timeout);
Trond Myklebust8d480322016-08-05 19:03:31 -04002779}
Trond Myklebust26ae1022017-02-08 11:17:55 -05002780EXPORT_SYMBOL_GPL(rpc_set_connect_timeout);
Trond Myklebust8d480322016-08-05 19:03:31 -04002781
Andy Adamson3b58a8a2016-09-09 09:22:23 -04002782void rpc_clnt_xprt_switch_put(struct rpc_clnt *clnt)
2783{
Anna Schumakerbb29dd82016-10-26 10:33:31 -04002784 rcu_read_lock();
Andy Adamson3b58a8a2016-09-09 09:22:23 -04002785 xprt_switch_put(rcu_dereference(clnt->cl_xpi.xpi_xpswitch));
Anna Schumakerbb29dd82016-10-26 10:33:31 -04002786 rcu_read_unlock();
Andy Adamson3b58a8a2016-09-09 09:22:23 -04002787}
2788EXPORT_SYMBOL_GPL(rpc_clnt_xprt_switch_put);
2789
Andy Adamsondd691712016-09-09 09:22:24 -04002790void rpc_clnt_xprt_switch_add_xprt(struct rpc_clnt *clnt, struct rpc_xprt *xprt)
2791{
Anna Schumakerbb29dd82016-10-26 10:33:31 -04002792 rcu_read_lock();
Andy Adamsondd691712016-09-09 09:22:24 -04002793 rpc_xprt_switch_add_xprt(rcu_dereference(clnt->cl_xpi.xpi_xpswitch),
2794 xprt);
Anna Schumakerbb29dd82016-10-26 10:33:31 -04002795 rcu_read_unlock();
Andy Adamsondd691712016-09-09 09:22:24 -04002796}
2797EXPORT_SYMBOL_GPL(rpc_clnt_xprt_switch_add_xprt);
2798
Andy Adamson39e5d2d2016-09-09 09:22:25 -04002799bool rpc_clnt_xprt_switch_has_addr(struct rpc_clnt *clnt,
2800 const struct sockaddr *sap)
2801{
2802 struct rpc_xprt_switch *xps;
2803 bool ret;
2804
Andy Adamson39e5d2d2016-09-09 09:22:25 -04002805 rcu_read_lock();
Anna Schumakerbb29dd82016-10-26 10:33:31 -04002806 xps = rcu_dereference(clnt->cl_xpi.xpi_xpswitch);
Andy Adamson39e5d2d2016-09-09 09:22:25 -04002807 ret = rpc_xprt_switch_has_addr(xps, sap);
2808 rcu_read_unlock();
2809 return ret;
2810}
2811EXPORT_SYMBOL_GPL(rpc_clnt_xprt_switch_has_addr);
2812
Jeff Laytonf895b252014-11-17 16:58:04 -05002813#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
Chuck Lever68a23ee2008-05-21 17:09:26 -04002814static void rpc_show_header(void)
2815{
Chuck Levercb3997b2008-05-21 17:09:41 -04002816 printk(KERN_INFO "-pid- flgs status -client- --rqstp- "
2817 "-timeout ---ops--\n");
Chuck Lever68a23ee2008-05-21 17:09:26 -04002818}
2819
Chuck Lever38e886e2008-05-21 17:09:33 -04002820static void rpc_show_task(const struct rpc_clnt *clnt,
2821 const struct rpc_task *task)
2822{
2823 const char *rpc_waitq = "none";
Chuck Lever38e886e2008-05-21 17:09:33 -04002824
2825 if (RPC_IS_QUEUED(task))
2826 rpc_waitq = rpc_qname(task->tk_waitqueue);
2827
Joe Perchesb3bceda2010-12-21 10:52:24 -05002828 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 -04002829 task->tk_pid, task->tk_flags, task->tk_status,
2830 clnt, task->tk_rqstp, task->tk_timeout, task->tk_ops,
Trond Myklebust55909f22013-08-23 11:48:15 -04002831 clnt->cl_program->name, clnt->cl_vers, rpc_proc_name(task),
Joe Perchesb3bceda2010-12-21 10:52:24 -05002832 task->tk_action, rpc_waitq);
Chuck Lever38e886e2008-05-21 17:09:33 -04002833}
2834
Stanislav Kinsbursky70abc492012-01-12 22:07:51 +04002835void rpc_show_tasks(struct net *net)
Trond Myklebust188fef12007-06-16 14:18:40 -04002836{
2837 struct rpc_clnt *clnt;
Chuck Lever38e886e2008-05-21 17:09:33 -04002838 struct rpc_task *task;
Chuck Lever68a23ee2008-05-21 17:09:26 -04002839 int header = 0;
Stanislav Kinsbursky70abc492012-01-12 22:07:51 +04002840 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
Trond Myklebust188fef12007-06-16 14:18:40 -04002841
Stanislav Kinsbursky70abc492012-01-12 22:07:51 +04002842 spin_lock(&sn->rpc_client_lock);
2843 list_for_each_entry(clnt, &sn->all_clients, cl_clients) {
Trond Myklebust188fef12007-06-16 14:18:40 -04002844 spin_lock(&clnt->cl_lock);
Chuck Lever38e886e2008-05-21 17:09:33 -04002845 list_for_each_entry(task, &clnt->cl_tasks, tk_task) {
Chuck Lever68a23ee2008-05-21 17:09:26 -04002846 if (!header) {
2847 rpc_show_header();
2848 header++;
2849 }
Chuck Lever38e886e2008-05-21 17:09:33 -04002850 rpc_show_task(clnt, task);
Trond Myklebust188fef12007-06-16 14:18:40 -04002851 }
2852 spin_unlock(&clnt->cl_lock);
2853 }
Stanislav Kinsbursky70abc492012-01-12 22:07:51 +04002854 spin_unlock(&sn->rpc_client_lock);
Trond Myklebust188fef12007-06-16 14:18:40 -04002855}
2856#endif
Jeff Layton3c87ef62015-06-03 16:14:25 -04002857
2858#if IS_ENABLED(CONFIG_SUNRPC_SWAP)
Trond Myklebust15001e52016-01-30 20:05:34 -05002859static int
2860rpc_clnt_swap_activate_callback(struct rpc_clnt *clnt,
2861 struct rpc_xprt *xprt,
2862 void *dummy)
2863{
2864 return xprt_enable_swap(xprt);
2865}
2866
Jeff Layton3c87ef62015-06-03 16:14:25 -04002867int
2868rpc_clnt_swap_activate(struct rpc_clnt *clnt)
2869{
Trond Myklebust15001e52016-01-30 20:05:34 -05002870 if (atomic_inc_return(&clnt->cl_swapper) == 1)
2871 return rpc_clnt_iterate_for_each_xprt(clnt,
2872 rpc_clnt_swap_activate_callback, NULL);
2873 return 0;
Jeff Layton3c87ef62015-06-03 16:14:25 -04002874}
2875EXPORT_SYMBOL_GPL(rpc_clnt_swap_activate);
2876
Trond Myklebust15001e52016-01-30 20:05:34 -05002877static int
2878rpc_clnt_swap_deactivate_callback(struct rpc_clnt *clnt,
2879 struct rpc_xprt *xprt,
2880 void *dummy)
2881{
2882 xprt_disable_swap(xprt);
2883 return 0;
2884}
2885
Jeff Layton3c87ef62015-06-03 16:14:25 -04002886void
2887rpc_clnt_swap_deactivate(struct rpc_clnt *clnt)
2888{
Trond Myklebust15001e52016-01-30 20:05:34 -05002889 if (atomic_dec_if_positive(&clnt->cl_swapper) == 0)
2890 rpc_clnt_iterate_for_each_xprt(clnt,
2891 rpc_clnt_swap_deactivate_callback, NULL);
Jeff Layton3c87ef62015-06-03 16:14:25 -04002892}
2893EXPORT_SYMBOL_GPL(rpc_clnt_swap_deactivate);
2894#endif /* CONFIG_SUNRPC_SWAP */