blob: 627a87a71f8ba2f90917cf5b7c0a191a476958e4 [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Chuck Lever55aa4f52005-08-11 16:25:47 -04003 * linux/net/sunrpc/clnt.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * This file contains the high-level RPC interface.
6 * It is modeled as a finite state machine to support both synchronous
7 * and asynchronous requests.
8 *
9 * - RPC header generation and argument serialization.
10 * - Credential refresh.
11 * - TCP connect handling.
12 * - Retry of operation when it is suspected the operation failed because
13 * of uid squashing on the server, or when the credentials were stale
14 * and need to be refreshed, or when a packet was damaged in transit.
15 * This may be have to be moved to the VFS layer.
16 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 * Copyright (C) 1992,1993 Rick Sladkey <jrs@world.std.com>
18 * Copyright (C) 1995,1996 Olaf Kirch <okir@monad.swb.de>
19 */
20
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
22#include <linux/module.h>
23#include <linux/types.h>
Chuck Levercb3997b2008-05-21 17:09:41 -040024#include <linux/kallsyms.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/mm.h>
Trond Myklebust23ac6582009-08-09 15:14:25 -040026#include <linux/namei.h>
27#include <linux/mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/slab.h>
Trond Myklebust40b00b6b2013-10-17 14:12:23 -040029#include <linux/rcupdate.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/utsname.h>
Chuck Lever11c556b2006-03-20 13:44:22 -050031#include <linux/workqueue.h>
Chuck Lever176e21e2011-05-09 15:22:44 -040032#include <linux/in.h>
Chuck Lever510deb02007-12-10 14:56:24 -050033#include <linux/in6.h>
Chuck Lever176e21e2011-05-09 15:22:44 -040034#include <linux/un.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36#include <linux/sunrpc/clnt.h>
Jeff Layton59766872013-02-04 12:50:00 -050037#include <linux/sunrpc/addr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/sunrpc/rpc_pipe_fs.h>
Chuck Lever11c556b2006-03-20 13:44:22 -050039#include <linux/sunrpc/metrics.h>
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -040040#include <linux/sunrpc/bc_xprt.h>
Steve Dickson5753cba2012-02-06 10:08:08 -050041#include <trace/events/sunrpc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -040043#include "sunrpc.h"
Stanislav Kinsbursky70abc492012-01-12 22:07:51 +040044#include "netns.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Jeff Laytonf895b252014-11-17 16:58:04 -050046#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
Linus Torvalds1da177e2005-04-16 15:20:36 -070047# define RPCDBG_FACILITY RPCDBG_CALL
48#endif
49
Chuck Lever46121cf2007-01-31 12:14:08 -050050#define dprint_status(t) \
51 dprintk("RPC: %5u %s (status %d)\n", t->tk_pid, \
Harvey Harrison0dc47872008-03-05 20:47:47 -080052 __func__, t->tk_status)
Chuck Lever46121cf2007-01-31 12:14:08 -050053
Trond Myklebust188fef12007-06-16 14:18:40 -040054/*
55 * All RPC clients are linked into this list
56 */
Trond Myklebust188fef12007-06-16 14:18:40 -040057
Linus Torvalds1da177e2005-04-16 15:20:36 -070058static DECLARE_WAIT_QUEUE_HEAD(destroy_wait);
59
60
61static void call_start(struct rpc_task *task);
62static void call_reserve(struct rpc_task *task);
63static void call_reserveresult(struct rpc_task *task);
64static void call_allocate(struct rpc_task *task);
Trond Myklebust762e4e62018-08-24 16:28:28 -040065static void call_encode(struct rpc_task *task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066static void call_decode(struct rpc_task *task);
67static void call_bind(struct rpc_task *task);
Chuck Leverda351872005-08-11 16:25:11 -040068static void call_bind_status(struct rpc_task *task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069static void call_transmit(struct rpc_task *task);
70static void call_status(struct rpc_task *task);
Trond Myklebust940e3312005-11-09 21:45:24 -050071static void call_transmit_status(struct rpc_task *task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072static void call_refresh(struct rpc_task *task);
73static void call_refreshresult(struct rpc_task *task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074static void call_connect(struct rpc_task *task);
75static void call_connect_status(struct rpc_task *task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Chuck Levere8680a22019-02-11 11:24:48 -050077static int rpc_encode_header(struct rpc_task *task,
78 struct xdr_stream *xdr);
Chuck Levera0584ee2019-02-11 11:24:58 -050079static int rpc_decode_header(struct rpc_task *task,
80 struct xdr_stream *xdr);
Chuck Levercaabea82009-12-03 15:58:56 -050081static int rpc_ping(struct rpc_clnt *clnt);
Trond Myklebust7b3fef82019-03-07 14:10:32 -050082static void rpc_check_timeout(struct rpc_task *task);
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
Trond Myklebust79caa5f2019-04-24 17:46:42 -0400398 clnt->cl_cred = get_cred(args->cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 clnt->cl_procinfo = version->procs;
400 clnt->cl_maxproc = version->nrprocs;
Benny Halevyd5b337b2008-09-28 09:21:26 +0300401 clnt->cl_prog = args->prognumber ? : program->number;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 clnt->cl_vers = version->number;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 clnt->cl_stats = program->stats;
Chuck Lever11c556b2006-03-20 13:44:22 -0500404 clnt->cl_metrics = rpc_alloc_iostats(clnt);
Trond Myklebust6739ffb2013-08-26 15:38:11 -0400405 rpc_init_pipe_dir_head(&clnt->cl_pipedir_objects);
Trond Myklebust23bf85b2006-11-21 10:40:23 -0500406 err = -ENOMEM;
407 if (clnt->cl_metrics == NULL)
408 goto out_no_stats;
Trond Myklebust3e32a5d2006-11-16 11:37:27 -0500409 clnt->cl_program = program;
Trond Myklebust6529eba2007-06-14 16:40:14 -0400410 INIT_LIST_HEAD(&clnt->cl_tasks);
Trond Myklebust4bef61f2007-06-16 14:17:01 -0400411 spin_lock_init(&clnt->cl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400413 timeout = xprt->timeout;
Trond Myklebustba7392b2007-12-20 16:03:55 -0500414 if (args->timeout != NULL) {
415 memcpy(&clnt->cl_timeout_default, args->timeout,
416 sizeof(clnt->cl_timeout_default));
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400417 timeout = &clnt->cl_timeout_default;
Trond Myklebustba7392b2007-12-20 16:03:55 -0500418 }
419
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400420 rpc_clnt_set_transport(clnt, xprt, timeout);
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500421 xprt_iter_init(&clnt->cl_xpi, xps);
422 xprt_switch_put(xps);
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400423
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 clnt->cl_rtt = &clnt->cl_rtt_default;
Trond Myklebustba7392b2007-12-20 16:03:55 -0500425 rpc_init_rtt(&clnt->cl_rtt_default, clnt->cl_timeout->to_initval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
Trond Myklebust006abe82010-09-12 19:55:25 -0400427 atomic_set(&clnt->cl_count, 1);
Trond Myklebust34f52e32007-06-14 16:40:31 -0400428
Trond Myklebust03a9a422015-01-30 18:12:28 -0500429 if (nodename == NULL)
430 nodename = utsname()->nodename;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 /* save the nodename */
Trond Myklebust03a9a422015-01-30 18:12:28 -0500432 rpc_clnt_set_nodename(clnt, nodename);
Stanislav Kinsburskye73f4cc2013-06-24 11:52:52 +0400433
Chuck Leverd746e542013-10-17 14:12:17 -0400434 err = rpc_client_register(clnt, args->authflavor, args->client_name);
Stanislav Kinsburskye73f4cc2013-06-24 11:52:52 +0400435 if (err)
436 goto out_no_path;
Trond Myklebust280ebcf2013-09-02 13:28:04 -0400437 if (parent)
438 atomic_inc(&parent->cl_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 return clnt;
440
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441out_no_path:
Trond Myklebust23bf85b2006-11-21 10:40:23 -0500442 rpc_free_iostats(clnt->cl_metrics);
443out_no_stats:
Trond Myklebust79caa5f2019-04-24 17:46:42 -0400444 put_cred(clnt->cl_cred);
Trond Myklebust2f048db2013-09-04 21:51:44 -0400445 rpc_free_clid(clnt);
446out_no_clid:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 kfree(clnt);
448out_err:
Trond Myklebust4ada5392007-06-14 17:26:17 -0400449 rpciod_down();
450out_no_rpciod:
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500451 xprt_switch_put(xps);
Trond Myklebustf05c1242013-04-05 14:13:21 -0400452 xprt_put(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 return ERR_PTR(err);
454}
455
J. Bruce Fieldsd50039e2016-05-16 17:03:42 -0400456static struct rpc_clnt *rpc_create_xprt(struct rpc_create_args *args,
Kinglong Mee83ddfeb2014-03-24 11:58:59 +0800457 struct rpc_xprt *xprt)
458{
459 struct rpc_clnt *clnt = NULL;
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500460 struct rpc_xprt_switch *xps;
Kinglong Mee83ddfeb2014-03-24 11:58:59 +0800461
J. Bruce Fields39a9beab2016-05-17 12:38:21 -0400462 if (args->bc_xprt && args->bc_xprt->xpt_bc_xps) {
Chuck Lever16590a22016-08-22 14:57:42 -0400463 WARN_ON_ONCE(!(args->protocol & XPRT_TRANSPORT_BC));
J. Bruce Fields39a9beab2016-05-17 12:38:21 -0400464 xps = args->bc_xprt->xpt_bc_xps;
465 xprt_switch_get(xps);
466 } else {
467 xps = xprt_switch_alloc(xprt, GFP_KERNEL);
468 if (xps == NULL) {
469 xprt_put(xprt);
470 return ERR_PTR(-ENOMEM);
471 }
472 if (xprt->bc_xprt) {
473 xprt_switch_get(xps);
474 xprt->bc_xprt->xpt_bc_xps = xps;
475 }
J. Bruce Fields1208fd52016-05-20 17:07:17 -0400476 }
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500477 clnt = rpc_new_client(args, xps, xprt, NULL);
Kinglong Mee83ddfeb2014-03-24 11:58:59 +0800478 if (IS_ERR(clnt))
479 return clnt;
480
481 if (!(args->flags & RPC_CLNT_CREATE_NOPING)) {
482 int err = rpc_ping(clnt);
483 if (err != 0) {
484 rpc_shutdown_client(clnt);
485 return ERR_PTR(err);
486 }
487 }
488
489 clnt->cl_softrtry = 1;
Trond Myklebustae6ec912019-04-07 13:58:58 -0400490 if (args->flags & (RPC_CLNT_CREATE_HARDRTRY|RPC_CLNT_CREATE_SOFTERR)) {
Kinglong Mee83ddfeb2014-03-24 11:58:59 +0800491 clnt->cl_softrtry = 0;
Trond Myklebustae6ec912019-04-07 13:58:58 -0400492 if (args->flags & RPC_CLNT_CREATE_SOFTERR)
493 clnt->cl_softerr = 1;
494 }
Kinglong Mee83ddfeb2014-03-24 11:58:59 +0800495
496 if (args->flags & RPC_CLNT_CREATE_AUTOBIND)
497 clnt->cl_autobind = 1;
Trond Myklebust2aca5b82014-09-24 22:35:58 -0400498 if (args->flags & RPC_CLNT_CREATE_NO_RETRANS_TIMEOUT)
499 clnt->cl_noretranstimeo = 1;
Kinglong Mee83ddfeb2014-03-24 11:58:59 +0800500 if (args->flags & RPC_CLNT_CREATE_DISCRTRY)
501 clnt->cl_discrtry = 1;
502 if (!(args->flags & RPC_CLNT_CREATE_QUIET))
503 clnt->cl_chatty = 1;
504
505 return clnt;
506}
Kinglong Mee83ddfeb2014-03-24 11:58:59 +0800507
Ben Hutchings2c530402012-07-10 10:55:09 +0000508/**
Chuck Leverc2866762006-08-22 20:06:20 -0400509 * rpc_create - create an RPC client and transport with one call
510 * @args: rpc_clnt create argument structure
511 *
512 * Creates and initializes an RPC transport and an RPC client.
513 *
514 * It can ping the server in order to determine if it is up, and to see if
515 * it supports this program and version. RPC_CLNT_CREATE_NOPING disables
516 * this behavior so asynchronous tasks can also use rpc_create.
517 */
518struct rpc_clnt *rpc_create(struct rpc_create_args *args)
519{
520 struct rpc_xprt *xprt;
\"Talpey, Thomas\3c341b0b2007-09-10 13:47:07 -0400521 struct xprt_create xprtargs = {
Pavel Emelyanov9a23e332010-09-29 16:05:12 +0400522 .net = args->net,
\"Talpey, Thomas\4fa016e2007-09-10 13:47:57 -0400523 .ident = args->protocol,
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +0200524 .srcaddr = args->saddress,
Frank van Maarseveen96802a02007-07-08 13:08:54 +0200525 .dstaddr = args->address,
526 .addrlen = args->addrsize,
Trond Myklebust4e0038b2012-03-01 17:01:05 -0500527 .servername = args->servername,
Alexandros Batsakisf300bab2009-09-10 17:33:30 +0300528 .bc_xprt = args->bc_xprt,
Frank van Maarseveen96802a02007-07-08 13:08:54 +0200529 };
Chuck Lever510deb02007-12-10 14:56:24 -0500530 char servername[48];
Chuck Leverc2866762006-08-22 20:06:20 -0400531
J. Bruce Fieldsd50039e2016-05-16 17:03:42 -0400532 if (args->bc_xprt) {
Chuck Lever16590a22016-08-22 14:57:42 -0400533 WARN_ON_ONCE(!(args->protocol & XPRT_TRANSPORT_BC));
J. Bruce Fieldsd50039e2016-05-16 17:03:42 -0400534 xprt = args->bc_xprt->xpt_bc_xprt;
535 if (xprt) {
536 xprt_get(xprt);
537 return rpc_create_xprt(args, xprt);
538 }
539 }
540
Trond Myklebustb7993ce2013-04-14 11:42:00 -0400541 if (args->flags & RPC_CLNT_CREATE_INFINITE_SLOTS)
542 xprtargs.flags |= XPRT_CREATE_INFINITE_SLOTS;
J. Bruce Fields33d90ac2013-04-11 15:06:36 -0400543 if (args->flags & RPC_CLNT_CREATE_NO_IDLE_TIMEOUT)
544 xprtargs.flags |= XPRT_CREATE_NO_IDLE_TIMEOUT;
Chuck Leverc2866762006-08-22 20:06:20 -0400545 /*
Chuck Lever43780b82007-07-01 12:13:22 -0400546 * If the caller chooses not to specify a hostname, whip
547 * up a string representation of the passed-in address.
548 */
Trond Myklebust4e0038b2012-03-01 17:01:05 -0500549 if (xprtargs.servername == NULL) {
Chuck Lever176e21e2011-05-09 15:22:44 -0400550 struct sockaddr_un *sun =
551 (struct sockaddr_un *)args->address;
Chuck Leverda09eb92011-05-09 15:22:25 -0400552 struct sockaddr_in *sin =
553 (struct sockaddr_in *)args->address;
554 struct sockaddr_in6 *sin6 =
555 (struct sockaddr_in6 *)args->address;
556
Chuck Lever510deb02007-12-10 14:56:24 -0500557 servername[0] = '\0';
558 switch (args->address->sa_family) {
Chuck Lever176e21e2011-05-09 15:22:44 -0400559 case AF_LOCAL:
560 snprintf(servername, sizeof(servername), "%s",
561 sun->sun_path);
562 break;
Chuck Leverda09eb92011-05-09 15:22:25 -0400563 case AF_INET:
Harvey Harrison21454aa2008-10-31 00:54:56 -0700564 snprintf(servername, sizeof(servername), "%pI4",
565 &sin->sin_addr.s_addr);
Chuck Lever510deb02007-12-10 14:56:24 -0500566 break;
Chuck Leverda09eb92011-05-09 15:22:25 -0400567 case AF_INET6:
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700568 snprintf(servername, sizeof(servername), "%pI6",
Chuck Leverda09eb92011-05-09 15:22:25 -0400569 &sin6->sin6_addr);
Chuck Lever510deb02007-12-10 14:56:24 -0500570 break;
Chuck Lever510deb02007-12-10 14:56:24 -0500571 default:
572 /* caller wants default server name, but
573 * address family isn't recognized. */
574 return ERR_PTR(-EINVAL);
575 }
Trond Myklebust4e0038b2012-03-01 17:01:05 -0500576 xprtargs.servername = servername;
Chuck Lever43780b82007-07-01 12:13:22 -0400577 }
578
Chuck Lever510deb02007-12-10 14:56:24 -0500579 xprt = xprt_create_transport(&xprtargs);
580 if (IS_ERR(xprt))
581 return (struct rpc_clnt *)xprt;
582
Chuck Lever43780b82007-07-01 12:13:22 -0400583 /*
Chuck Leverc2866762006-08-22 20:06:20 -0400584 * By default, kernel RPC client connects from a reserved port.
585 * CAP_NET_BIND_SERVICE will not be set for unprivileged requesters,
586 * but it is always enabled for rpciod, which handles the connect
587 * operation.
588 */
589 xprt->resvport = 1;
590 if (args->flags & RPC_CLNT_CREATE_NONPRIVPORT)
591 xprt->resvport = 0;
592
Kinglong Mee83ddfeb2014-03-24 11:58:59 +0800593 return rpc_create_xprt(args, xprt);
Chuck Leverc2866762006-08-22 20:06:20 -0400594}
Chuck Leverb86acd52006-08-22 20:06:22 -0400595EXPORT_SYMBOL_GPL(rpc_create);
Chuck Leverc2866762006-08-22 20:06:20 -0400596
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597/*
598 * This function clones the RPC client structure. It allows us to share the
599 * same transport while varying parameters such as the authentication
600 * flavour.
601 */
Chuck Lever1b63a752012-09-14 17:23:52 -0400602static struct rpc_clnt *__rpc_clone_client(struct rpc_create_args *args,
603 struct rpc_clnt *clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604{
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500605 struct rpc_xprt_switch *xps;
Trond Myklebust2446ab62012-03-01 17:00:56 -0500606 struct rpc_xprt *xprt;
Chuck Lever1b63a752012-09-14 17:23:52 -0400607 struct rpc_clnt *new;
608 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
Chuck Lever1b63a752012-09-14 17:23:52 -0400610 err = -ENOMEM;
Trond Myklebust2446ab62012-03-01 17:00:56 -0500611 rcu_read_lock();
612 xprt = xprt_get(rcu_dereference(clnt->cl_xprt));
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500613 xps = xprt_switch_get(rcu_dereference(clnt->cl_xpi.xpi_xpswitch));
Trond Myklebust2446ab62012-03-01 17:00:56 -0500614 rcu_read_unlock();
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500615 if (xprt == NULL || xps == NULL) {
616 xprt_put(xprt);
617 xprt_switch_put(xps);
Chuck Lever1b63a752012-09-14 17:23:52 -0400618 goto out_err;
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500619 }
Chuck Lever1b63a752012-09-14 17:23:52 -0400620 args->servername = xprt->servername;
Trond Myklebust03a9a422015-01-30 18:12:28 -0500621 args->nodename = clnt->cl_nodename;
Chuck Lever1b63a752012-09-14 17:23:52 -0400622
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500623 new = rpc_new_client(args, xps, xprt, clnt);
Chuck Lever1b63a752012-09-14 17:23:52 -0400624 if (IS_ERR(new)) {
625 err = PTR_ERR(new);
Chuck Levera58e0be2013-03-22 12:52:59 -0400626 goto out_err;
Chuck Lever1b63a752012-09-14 17:23:52 -0400627 }
628
Chuck Lever1b63a752012-09-14 17:23:52 -0400629 /* Turn off autobind on clones */
630 new->cl_autobind = 0;
631 new->cl_softrtry = clnt->cl_softrtry;
Trond Myklebustae6ec912019-04-07 13:58:58 -0400632 new->cl_softerr = clnt->cl_softerr;
Trond Myklebust2aca5b82014-09-24 22:35:58 -0400633 new->cl_noretranstimeo = clnt->cl_noretranstimeo;
Chuck Lever1b63a752012-09-14 17:23:52 -0400634 new->cl_discrtry = clnt->cl_discrtry;
635 new->cl_chatty = clnt->cl_chatty;
NeilBrown5e169232018-12-03 11:30:30 +1100636 new->cl_principal = clnt->cl_principal;
Trond Myklebust79caa5f2019-04-24 17:46:42 -0400637 new->cl_cred = get_cred(clnt->cl_cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 return new;
Chuck Lever1b63a752012-09-14 17:23:52 -0400639
Chuck Lever1b63a752012-09-14 17:23:52 -0400640out_err:
Harvey Harrison0dc47872008-03-05 20:47:47 -0800641 dprintk("RPC: %s: returned error %d\n", __func__, err);
Trond Myklebust3e32a5d2006-11-16 11:37:27 -0500642 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643}
Chuck Lever1b63a752012-09-14 17:23:52 -0400644
645/**
646 * rpc_clone_client - Clone an RPC client structure
647 *
648 * @clnt: RPC client whose parameters are copied
649 *
650 * Returns a fresh RPC client or an ERR_PTR.
651 */
652struct rpc_clnt *rpc_clone_client(struct rpc_clnt *clnt)
653{
654 struct rpc_create_args args = {
655 .program = clnt->cl_program,
656 .prognumber = clnt->cl_prog,
657 .version = clnt->cl_vers,
658 .authflavor = clnt->cl_auth->au_flavor,
Trond Myklebust79caa5f2019-04-24 17:46:42 -0400659 .cred = clnt->cl_cred,
Chuck Lever1b63a752012-09-14 17:23:52 -0400660 };
661 return __rpc_clone_client(&args, clnt);
662}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400663EXPORT_SYMBOL_GPL(rpc_clone_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
Chuck Leverba9b5842012-09-14 17:24:02 -0400665/**
666 * rpc_clone_client_set_auth - Clone an RPC client structure and set its auth
667 *
668 * @clnt: RPC client whose parameters are copied
Randy Dunlap7144bca2013-01-09 17:12:35 -0800669 * @flavor: security flavor for new client
Chuck Leverba9b5842012-09-14 17:24:02 -0400670 *
671 * Returns a fresh RPC client or an ERR_PTR.
672 */
673struct rpc_clnt *
674rpc_clone_client_set_auth(struct rpc_clnt *clnt, rpc_authflavor_t flavor)
675{
676 struct rpc_create_args args = {
677 .program = clnt->cl_program,
678 .prognumber = clnt->cl_prog,
679 .version = clnt->cl_vers,
680 .authflavor = flavor,
Trond Myklebust79caa5f2019-04-24 17:46:42 -0400681 .cred = clnt->cl_cred,
Chuck Leverba9b5842012-09-14 17:24:02 -0400682 };
683 return __rpc_clone_client(&args, clnt);
684}
685EXPORT_SYMBOL_GPL(rpc_clone_client_set_auth);
686
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400687/**
688 * rpc_switch_client_transport: switch the RPC transport on the fly
689 * @clnt: pointer to a struct rpc_clnt
690 * @args: pointer to the new transport arguments
691 * @timeout: pointer to the new timeout parameters
692 *
693 * This function allows the caller to switch the RPC transport for the
694 * rpc_clnt structure 'clnt' to allow it to connect to a mirrored NFS
695 * server, for instance. It assumes that the caller has ensured that
696 * there are no active RPC tasks by using some form of locking.
697 *
698 * Returns zero if "clnt" is now using the new xprt. Otherwise a
699 * negative errno is returned, and "clnt" continues to use the old
700 * xprt.
701 */
702int rpc_switch_client_transport(struct rpc_clnt *clnt,
703 struct xprt_create *args,
704 const struct rpc_timeout *timeout)
705{
706 const struct rpc_timeout *old_timeo;
707 rpc_authflavor_t pseudoflavor;
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500708 struct rpc_xprt_switch *xps, *oldxps;
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400709 struct rpc_xprt *xprt, *old;
710 struct rpc_clnt *parent;
711 int err;
712
713 xprt = xprt_create_transport(args);
714 if (IS_ERR(xprt)) {
715 dprintk("RPC: failed to create new xprt for clnt %p\n",
716 clnt);
717 return PTR_ERR(xprt);
718 }
719
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500720 xps = xprt_switch_alloc(xprt, GFP_KERNEL);
721 if (xps == NULL) {
722 xprt_put(xprt);
723 return -ENOMEM;
724 }
725
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400726 pseudoflavor = clnt->cl_auth->au_flavor;
727
728 old_timeo = clnt->cl_timeout;
729 old = rpc_clnt_set_transport(clnt, xprt, timeout);
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500730 oldxps = xprt_iter_xchg_switch(&clnt->cl_xpi, xps);
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400731
732 rpc_unregister_client(clnt);
733 __rpc_clnt_remove_pipedir(clnt);
Jeff Laytonb4b9d2c2014-11-26 14:44:43 -0500734 rpc_clnt_debugfs_unregister(clnt);
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400735
736 /*
737 * A new transport was created. "clnt" therefore
738 * becomes the root of a new cl_parent tree. clnt's
739 * children, if it has any, still point to the old xprt.
740 */
741 parent = clnt->cl_parent;
742 clnt->cl_parent = clnt;
743
744 /*
745 * The old rpc_auth cache cannot be re-used. GSS
746 * contexts in particular are between a single
747 * client and server.
748 */
749 err = rpc_client_register(clnt, pseudoflavor, NULL);
750 if (err)
751 goto out_revert;
752
753 synchronize_rcu();
754 if (parent != clnt)
755 rpc_release_client(parent);
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500756 xprt_switch_put(oldxps);
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400757 xprt_put(old);
758 dprintk("RPC: replaced xprt for clnt %p\n", clnt);
759 return 0;
760
761out_revert:
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500762 xps = xprt_iter_xchg_switch(&clnt->cl_xpi, oldxps);
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400763 rpc_clnt_set_transport(clnt, old, old_timeo);
764 clnt->cl_parent = parent;
765 rpc_client_register(clnt, pseudoflavor, NULL);
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500766 xprt_switch_put(xps);
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400767 xprt_put(xprt);
768 dprintk("RPC: failed to switch xprt for clnt %p\n", clnt);
769 return err;
770}
771EXPORT_SYMBOL_GPL(rpc_switch_client_transport);
772
Trond Myklebust32278862016-01-30 20:39:19 -0500773static
774int rpc_clnt_xprt_iter_init(struct rpc_clnt *clnt, struct rpc_xprt_iter *xpi)
775{
776 struct rpc_xprt_switch *xps;
777
778 rcu_read_lock();
779 xps = xprt_switch_get(rcu_dereference(clnt->cl_xpi.xpi_xpswitch));
780 rcu_read_unlock();
781 if (xps == NULL)
782 return -EAGAIN;
783 xprt_iter_init_listall(xpi, xps);
784 xprt_switch_put(xps);
785 return 0;
786}
787
788/**
789 * rpc_clnt_iterate_for_each_xprt - Apply a function to all transports
790 * @clnt: pointer to client
791 * @fn: function to apply
792 * @data: void pointer to function data
793 *
794 * Iterates through the list of RPC transports currently attached to the
795 * client and applies the function fn(clnt, xprt, data).
796 *
797 * On error, the iteration stops, and the function returns the error value.
798 */
799int rpc_clnt_iterate_for_each_xprt(struct rpc_clnt *clnt,
800 int (*fn)(struct rpc_clnt *, struct rpc_xprt *, void *),
801 void *data)
802{
803 struct rpc_xprt_iter xpi;
804 int ret;
805
806 ret = rpc_clnt_xprt_iter_init(clnt, &xpi);
807 if (ret)
808 return ret;
809 for (;;) {
810 struct rpc_xprt *xprt = xprt_iter_get_next(&xpi);
811
812 if (!xprt)
813 break;
814 ret = fn(clnt, xprt, data);
815 xprt_put(xprt);
816 if (ret < 0)
817 break;
818 }
819 xprt_iter_destroy(&xpi);
820 return ret;
821}
822EXPORT_SYMBOL_GPL(rpc_clnt_iterate_for_each_xprt);
823
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824/*
Trond Myklebust58f96122010-07-31 14:29:08 -0400825 * Kill all tasks for the given client.
826 * XXX: kill their descendants as well?
827 */
828void rpc_killall_tasks(struct rpc_clnt *clnt)
829{
830 struct rpc_task *rovr;
831
832
833 if (list_empty(&clnt->cl_tasks))
834 return;
835 dprintk("RPC: killing all tasks for client %p\n", clnt);
836 /*
837 * Spin lock all_tasks to prevent changes...
838 */
839 spin_lock(&clnt->cl_lock);
Trond Myklebustae67bd32019-04-07 13:58:44 -0400840 list_for_each_entry(rovr, &clnt->cl_tasks, tk_task)
841 rpc_signal_task(rovr);
Trond Myklebust58f96122010-07-31 14:29:08 -0400842 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 Myklebust79caa5f2019-04-24 17:46:42 -0400889 put_cred(clnt->cl_cred);
Trond Myklebust2f048db2013-09-04 21:51:44 -0400890 rpc_free_clid(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 kfree(clnt);
Trond Myklebustd07ba842013-11-12 17:24:36 -0500892 return parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893}
894
895/*
Trond Myklebust1dd17ec2007-06-26 16:57:41 -0400896 * Free an RPC client
897 */
Stephen Hemminger8fdee4c2018-07-24 12:29:15 -0700898static struct rpc_clnt *
Trond Myklebust006abe82010-09-12 19:55:25 -0400899rpc_free_auth(struct rpc_clnt *clnt)
Trond Myklebust1dd17ec2007-06-26 16:57:41 -0400900{
Trond Myklebustd07ba842013-11-12 17:24:36 -0500901 if (clnt->cl_auth == NULL)
902 return rpc_free_client(clnt);
Trond Myklebust1dd17ec2007-06-26 16:57:41 -0400903
904 /*
905 * Note: RPCSEC_GSS may need to send NULL RPC calls in order to
906 * release remaining GSS contexts. This mechanism ensures
907 * that it can do so safely.
908 */
Trond Myklebust006abe82010-09-12 19:55:25 -0400909 atomic_inc(&clnt->cl_count);
Trond Myklebust1dd17ec2007-06-26 16:57:41 -0400910 rpcauth_release(clnt->cl_auth);
911 clnt->cl_auth = NULL;
Trond Myklebust006abe82010-09-12 19:55:25 -0400912 if (atomic_dec_and_test(&clnt->cl_count))
Trond Myklebustd07ba842013-11-12 17:24:36 -0500913 return rpc_free_client(clnt);
914 return NULL;
Trond Myklebust1dd17ec2007-06-26 16:57:41 -0400915}
916
917/*
Trond Myklebust34f52e32007-06-14 16:40:31 -0400918 * Release reference to the RPC client
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 */
920void
921rpc_release_client(struct rpc_clnt *clnt)
922{
Trond Myklebust34f52e32007-06-14 16:40:31 -0400923 dprintk("RPC: rpc_release_client(%p)\n", clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
Trond Myklebustd07ba842013-11-12 17:24:36 -0500925 do {
926 if (list_empty(&clnt->cl_tasks))
927 wake_up(&destroy_wait);
928 if (!atomic_dec_and_test(&clnt->cl_count))
929 break;
930 clnt = rpc_free_auth(clnt);
931 } while (clnt != NULL);
Trond Myklebust34f52e32007-06-14 16:40:31 -0400932}
Simo Sorce1d658332012-05-25 18:09:55 -0400933EXPORT_SYMBOL_GPL(rpc_release_client);
Trond Myklebust34f52e32007-06-14 16:40:31 -0400934
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000935/**
936 * rpc_bind_new_program - bind a new RPC program to an existing client
Randy Dunlap65b6e422008-02-13 15:03:23 -0800937 * @old: old rpc_client
938 * @program: rpc program to set
939 * @vers: rpc program version
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000940 *
941 * Clones the rpc client and sets up a new RPC program. This is mainly
942 * of use for enabling different RPC programs to share the same transport.
943 * The Sun NFSv2/v3 ACL protocol can do this.
944 */
945struct rpc_clnt *rpc_bind_new_program(struct rpc_clnt *old,
Trond Myklebusta613fa12012-01-20 13:53:56 -0500946 const struct rpc_program *program,
Chuck Lever89eb21c2007-09-11 18:00:09 -0400947 u32 vers)
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000948{
Trond Myklebustf994c432012-11-01 12:14:14 -0400949 struct rpc_create_args args = {
950 .program = program,
951 .prognumber = program->number,
952 .version = vers,
953 .authflavor = old->cl_auth->au_flavor,
Trond Myklebust79caa5f2019-04-24 17:46:42 -0400954 .cred = old->cl_cred,
Trond Myklebustf994c432012-11-01 12:14:14 -0400955 };
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000956 struct rpc_clnt *clnt;
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000957 int err;
958
Trond Myklebustf994c432012-11-01 12:14:14 -0400959 clnt = __rpc_clone_client(&args, old);
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000960 if (IS_ERR(clnt))
961 goto out;
Chuck Levercaabea82009-12-03 15:58:56 -0500962 err = rpc_ping(clnt);
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000963 if (err != 0) {
964 rpc_shutdown_client(clnt);
965 clnt = ERR_PTR(err);
966 }
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800967out:
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000968 return clnt;
969}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400970EXPORT_SYMBOL_GPL(rpc_bind_new_program);
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000971
Bill Baker0f90be12018-06-19 16:24:58 -0500972void rpc_task_release_transport(struct rpc_task *task)
973{
974 struct rpc_xprt *xprt = task->tk_xprt;
975
976 if (xprt) {
977 task->tk_xprt = NULL;
978 xprt_put(xprt);
979 }
980}
981EXPORT_SYMBOL_GPL(rpc_task_release_transport);
982
Trond Myklebust58f96122010-07-31 14:29:08 -0400983void rpc_task_release_client(struct rpc_task *task)
984{
985 struct rpc_clnt *clnt = task->tk_client;
986
987 if (clnt != NULL) {
988 /* Remove from client task list */
989 spin_lock(&clnt->cl_lock);
990 list_del(&task->tk_task);
991 spin_unlock(&clnt->cl_lock);
992 task->tk_client = NULL;
993
994 rpc_release_client(clnt);
995 }
Bill Baker0f90be12018-06-19 16:24:58 -0500996 rpc_task_release_transport(task);
997}
Trond Myklebustfb43d172016-01-30 16:39:26 -0500998
Bill Baker0f90be12018-06-19 16:24:58 -0500999static
1000void rpc_task_set_transport(struct rpc_task *task, struct rpc_clnt *clnt)
1001{
1002 if (!task->tk_xprt)
1003 task->tk_xprt = xprt_iter_get_next(&clnt->cl_xpi);
Trond Myklebust58f96122010-07-31 14:29:08 -04001004}
1005
1006static
1007void rpc_task_set_client(struct rpc_task *task, struct rpc_clnt *clnt)
1008{
Trond Myklebustfb43d172016-01-30 16:39:26 -05001009
Trond Myklebust58f96122010-07-31 14:29:08 -04001010 if (clnt != NULL) {
Bill Baker0f90be12018-06-19 16:24:58 -05001011 rpc_task_set_transport(task, clnt);
Trond Myklebust58f96122010-07-31 14:29:08 -04001012 task->tk_client = clnt;
Trond Myklebust006abe82010-09-12 19:55:25 -04001013 atomic_inc(&clnt->cl_count);
Trond Myklebust58f96122010-07-31 14:29:08 -04001014 if (clnt->cl_softrtry)
1015 task->tk_flags |= RPC_TASK_SOFT;
Trond Myklebustae6ec912019-04-07 13:58:58 -04001016 if (clnt->cl_softerr)
1017 task->tk_flags |= RPC_TASK_TIMEOUT;
Trond Myklebust8a19a0b2013-09-24 12:00:27 -04001018 if (clnt->cl_noretranstimeo)
1019 task->tk_flags |= RPC_TASK_NO_RETRANS_TIMEOUT;
Jeff Layton3c87ef62015-06-03 16:14:25 -04001020 if (atomic_read(&clnt->cl_swapper))
1021 task->tk_flags |= RPC_TASK_SWAPPER;
Trond Myklebust58f96122010-07-31 14:29:08 -04001022 /* Add to the client's list of all tasks */
1023 spin_lock(&clnt->cl_lock);
1024 list_add_tail(&task->tk_task, &clnt->cl_tasks);
1025 spin_unlock(&clnt->cl_lock);
1026 }
1027}
1028
1029static void
1030rpc_task_set_rpc_message(struct rpc_task *task, const struct rpc_message *msg)
1031{
1032 if (msg != NULL) {
1033 task->tk_msg.rpc_proc = msg->rpc_proc;
1034 task->tk_msg.rpc_argp = msg->rpc_argp;
1035 task->tk_msg.rpc_resp = msg->rpc_resp;
Trond Myklebusta17c2152010-07-31 14:29:08 -04001036 if (msg->rpc_cred != NULL)
NeilBrowna52458b2018-12-03 11:30:31 +11001037 task->tk_msg.rpc_cred = get_cred(msg->rpc_cred);
Trond Myklebust58f96122010-07-31 14:29:08 -04001038 }
1039}
1040
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041/*
1042 * Default callback for async RPC calls
1043 */
1044static void
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001045rpc_default_callback(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046{
1047}
1048
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001049static const struct rpc_call_ops rpc_default_ops = {
1050 .rpc_call_done = rpc_default_callback,
1051};
1052
Trond Myklebustc970aa82007-07-14 15:39:59 -04001053/**
1054 * rpc_run_task - Allocate a new RPC task, then run rpc_execute against it
1055 * @task_setup_data: pointer to task initialisation data
1056 */
1057struct rpc_task *rpc_run_task(const struct rpc_task_setup *task_setup_data)
Trond Myklebust6e5b70e2007-06-12 10:02:37 -04001058{
Trond Myklebust19445b92010-04-16 16:41:10 -04001059 struct rpc_task *task;
Trond Myklebust6e5b70e2007-06-12 10:02:37 -04001060
Trond Myklebust84115e12007-07-14 15:39:59 -04001061 task = rpc_new_task(task_setup_data);
Trond Myklebust6e5b70e2007-06-12 10:02:37 -04001062
Trond Myklebust58f96122010-07-31 14:29:08 -04001063 rpc_task_set_client(task, task_setup_data->rpc_client);
1064 rpc_task_set_rpc_message(task, task_setup_data->rpc_message);
1065
Trond Myklebust58f96122010-07-31 14:29:08 -04001066 if (task->tk_action == NULL)
1067 rpc_call_start(task);
1068
Trond Myklebust6e5b70e2007-06-12 10:02:37 -04001069 atomic_inc(&task->tk_count);
1070 rpc_execute(task);
Trond Myklebust19445b92010-04-16 16:41:10 -04001071 return task;
Trond Myklebust6e5b70e2007-06-12 10:02:37 -04001072}
Trond Myklebustc970aa82007-07-14 15:39:59 -04001073EXPORT_SYMBOL_GPL(rpc_run_task);
Trond Myklebust6e5b70e2007-06-12 10:02:37 -04001074
1075/**
1076 * rpc_call_sync - Perform a synchronous RPC call
1077 * @clnt: pointer to RPC client
1078 * @msg: RPC call parameters
1079 * @flags: RPC call flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 */
Trond Myklebustcbc20052008-02-14 11:11:30 -05001081int rpc_call_sync(struct rpc_clnt *clnt, const struct rpc_message *msg, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082{
1083 struct rpc_task *task;
Trond Myklebust84115e12007-07-14 15:39:59 -04001084 struct rpc_task_setup task_setup_data = {
1085 .rpc_client = clnt,
1086 .rpc_message = msg,
1087 .callback_ops = &rpc_default_ops,
1088 .flags = flags,
1089 };
Trond Myklebust6e5b70e2007-06-12 10:02:37 -04001090 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
Weston Andros Adamson50d2bdb2012-11-01 16:04:40 -04001092 WARN_ON_ONCE(flags & RPC_TASK_ASYNC);
1093 if (flags & RPC_TASK_ASYNC) {
1094 rpc_release_calldata(task_setup_data.callback_ops,
1095 task_setup_data.callback_data);
1096 return -EINVAL;
1097 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
Trond Myklebustc970aa82007-07-14 15:39:59 -04001099 task = rpc_run_task(&task_setup_data);
Trond Myklebust6e5b70e2007-06-12 10:02:37 -04001100 if (IS_ERR(task))
1101 return PTR_ERR(task);
Trond Myklebuste60859a2006-01-03 09:55:10 +01001102 status = task->tk_status;
Trond Myklebustbde8f002007-01-24 11:54:53 -08001103 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 return status;
1105}
Trond Myklebuste8914c62007-07-14 15:39:59 -04001106EXPORT_SYMBOL_GPL(rpc_call_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
Trond Myklebust6e5b70e2007-06-12 10:02:37 -04001108/**
1109 * rpc_call_async - Perform an asynchronous RPC call
1110 * @clnt: pointer to RPC client
1111 * @msg: RPC call parameters
1112 * @flags: RPC call flags
Randy Dunlap65b6e422008-02-13 15:03:23 -08001113 * @tk_ops: RPC call ops
Trond Myklebust6e5b70e2007-06-12 10:02:37 -04001114 * @data: user call data
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 */
1116int
Trond Myklebustcbc20052008-02-14 11:11:30 -05001117rpc_call_async(struct rpc_clnt *clnt, const struct rpc_message *msg, int flags,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001118 const struct rpc_call_ops *tk_ops, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119{
1120 struct rpc_task *task;
Trond Myklebust84115e12007-07-14 15:39:59 -04001121 struct rpc_task_setup task_setup_data = {
1122 .rpc_client = clnt,
1123 .rpc_message = msg,
1124 .callback_ops = tk_ops,
1125 .callback_data = data,
1126 .flags = flags|RPC_TASK_ASYNC,
1127 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
Trond Myklebustc970aa82007-07-14 15:39:59 -04001129 task = rpc_run_task(&task_setup_data);
Trond Myklebust6e5b70e2007-06-12 10:02:37 -04001130 if (IS_ERR(task))
1131 return PTR_ERR(task);
1132 rpc_put_task(task);
1133 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134}
Trond Myklebuste8914c62007-07-14 15:39:59 -04001135EXPORT_SYMBOL_GPL(rpc_call_async);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001137#if defined(CONFIG_SUNRPC_BACKCHANNEL)
Trond Myklebust477687e2019-03-05 07:30:48 -05001138static void call_bc_encode(struct rpc_task *task);
1139
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001140/**
1141 * rpc_run_bc_task - Allocate a new RPC task for backchannel use, then run
1142 * rpc_execute against it
Jaswinder Singh Rajput7a73fdd2009-09-24 14:58:42 -04001143 * @req: RPC request
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001144 */
Trond Myklebust0f419792015-06-01 22:59:08 -04001145struct rpc_task *rpc_run_bc_task(struct rpc_rqst *req)
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001146{
1147 struct rpc_task *task;
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001148 struct rpc_task_setup task_setup_data = {
Trond Myklebust0f419792015-06-01 22:59:08 -04001149 .callback_ops = &rpc_default_ops,
Trond Myklebust762e4e62018-08-24 16:28:28 -04001150 .flags = RPC_TASK_SOFTCONN |
1151 RPC_TASK_NO_RETRANS_TIMEOUT,
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001152 };
1153
1154 dprintk("RPC: rpc_run_bc_task req= %p\n", req);
1155 /*
1156 * Create an rpc_task to send the data
1157 */
1158 task = rpc_new_task(&task_setup_data);
Trond Myklebust902c5882018-09-01 17:21:01 -04001159 xprt_init_bc_request(req, task);
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001160
Trond Myklebust477687e2019-03-05 07:30:48 -05001161 task->tk_action = call_bc_encode;
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001162 atomic_inc(&task->tk_count);
Weston Andros Adamson9a6478f2012-10-23 10:43:27 -04001163 WARN_ON_ONCE(atomic_read(&task->tk_count) != 2);
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001164 rpc_execute(task);
1165
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001166 dprintk("RPC: rpc_run_bc_task: task= %p\n", task);
1167 return task;
1168}
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001169#endif /* CONFIG_SUNRPC_BACKCHANNEL */
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001170
Chuck Levercf500ba2019-02-11 11:25:20 -05001171/**
1172 * rpc_prepare_reply_pages - Prepare to receive a reply data payload into pages
1173 * @req: RPC request to prepare
1174 * @pages: vector of struct page pointers
1175 * @base: offset in first page where receive should start, in bytes
1176 * @len: expected size of the upper layer data payload, in bytes
1177 * @hdrsize: expected size of upper layer reply header, in XDR words
1178 *
1179 */
1180void rpc_prepare_reply_pages(struct rpc_rqst *req, struct page **pages,
1181 unsigned int base, unsigned int len,
1182 unsigned int hdrsize)
1183{
Chuck Lever02ef04e2019-02-11 11:25:25 -05001184 /* Subtract one to force an extra word of buffer space for the
1185 * payload's XDR pad to fall into the rcv_buf's tail iovec.
1186 */
Chuck Lever35e77d22019-02-11 11:25:36 -05001187 hdrsize += RPC_REPHDRSIZE + req->rq_cred->cr_auth->au_ralign - 1;
Chuck Lever02ef04e2019-02-11 11:25:25 -05001188
Chuck Levercf500ba2019-02-11 11:25:20 -05001189 xdr_inline_pages(&req->rq_rcv_buf, hdrsize << 2, pages, base, len);
1190 trace_rpc_reply_pages(req);
1191}
1192EXPORT_SYMBOL_GPL(rpc_prepare_reply_pages);
1193
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194void
Trond Myklebust77de2c52007-10-25 18:40:21 -04001195rpc_call_start(struct rpc_task *task)
1196{
1197 task->tk_action = call_start;
1198}
1199EXPORT_SYMBOL_GPL(rpc_call_start);
1200
Chuck Levered394402006-08-22 20:06:17 -04001201/**
1202 * rpc_peeraddr - extract remote peer address from clnt's xprt
1203 * @clnt: RPC client structure
1204 * @buf: target buffer
Randy Dunlap65b6e422008-02-13 15:03:23 -08001205 * @bufsize: length of target buffer
Chuck Levered394402006-08-22 20:06:17 -04001206 *
1207 * Returns the number of bytes that are actually in the stored address.
1208 */
1209size_t rpc_peeraddr(struct rpc_clnt *clnt, struct sockaddr *buf, size_t bufsize)
1210{
1211 size_t bytes;
Trond Myklebust2446ab62012-03-01 17:00:56 -05001212 struct rpc_xprt *xprt;
Chuck Levered394402006-08-22 20:06:17 -04001213
Trond Myklebust2446ab62012-03-01 17:00:56 -05001214 rcu_read_lock();
1215 xprt = rcu_dereference(clnt->cl_xprt);
1216
1217 bytes = xprt->addrlen;
Chuck Levered394402006-08-22 20:06:17 -04001218 if (bytes > bufsize)
1219 bytes = bufsize;
Trond Myklebust2446ab62012-03-01 17:00:56 -05001220 memcpy(buf, &xprt->addr, bytes);
1221 rcu_read_unlock();
1222
1223 return bytes;
Chuck Levered394402006-08-22 20:06:17 -04001224}
Chuck Leverb86acd52006-08-22 20:06:22 -04001225EXPORT_SYMBOL_GPL(rpc_peeraddr);
Chuck Levered394402006-08-22 20:06:17 -04001226
Chuck Leverf425eba2006-08-22 20:06:18 -04001227/**
1228 * rpc_peeraddr2str - return remote peer address in printable format
1229 * @clnt: RPC client structure
1230 * @format: address format
1231 *
Trond Myklebust2446ab62012-03-01 17:00:56 -05001232 * NB: the lifetime of the memory referenced by the returned pointer is
1233 * the same as the rpc_xprt itself. As long as the caller uses this
1234 * pointer, it must hold the RCU read lock.
Chuck Leverf425eba2006-08-22 20:06:18 -04001235 */
Chuck Leverb454ae92008-01-07 18:34:48 -05001236const char *rpc_peeraddr2str(struct rpc_clnt *clnt,
1237 enum rpc_display_format_t format)
Chuck Leverf425eba2006-08-22 20:06:18 -04001238{
Trond Myklebust2446ab62012-03-01 17:00:56 -05001239 struct rpc_xprt *xprt;
1240
1241 xprt = rcu_dereference(clnt->cl_xprt);
Chuck Lever7559c7a2006-12-05 16:35:37 -05001242
1243 if (xprt->address_strings[format] != NULL)
1244 return xprt->address_strings[format];
1245 else
1246 return "unprintable";
Chuck Leverf425eba2006-08-22 20:06:18 -04001247}
Chuck Leverb86acd52006-08-22 20:06:22 -04001248EXPORT_SYMBOL_GPL(rpc_peeraddr2str);
Chuck Leverf425eba2006-08-22 20:06:18 -04001249
Chuck Lever2e738fd2012-03-01 17:01:14 -05001250static const struct sockaddr_in rpc_inaddr_loopback = {
1251 .sin_family = AF_INET,
1252 .sin_addr.s_addr = htonl(INADDR_ANY),
1253};
1254
1255static const struct sockaddr_in6 rpc_in6addr_loopback = {
1256 .sin6_family = AF_INET6,
1257 .sin6_addr = IN6ADDR_ANY_INIT,
1258};
1259
1260/*
1261 * Try a getsockname() on a connected datagram socket. Using a
1262 * connected datagram socket prevents leaving a socket in TIME_WAIT.
1263 * This conserves the ephemeral port number space.
1264 *
1265 * Returns zero and fills in "buf" if successful; otherwise, a
1266 * negative errno is returned.
1267 */
1268static int rpc_sockname(struct net *net, struct sockaddr *sap, size_t salen,
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001269 struct sockaddr *buf)
Chuck Lever2e738fd2012-03-01 17:01:14 -05001270{
1271 struct socket *sock;
1272 int err;
1273
1274 err = __sock_create(net, sap->sa_family,
1275 SOCK_DGRAM, IPPROTO_UDP, &sock, 1);
1276 if (err < 0) {
1277 dprintk("RPC: can't create UDP socket (%d)\n", err);
1278 goto out;
1279 }
1280
1281 switch (sap->sa_family) {
1282 case AF_INET:
1283 err = kernel_bind(sock,
1284 (struct sockaddr *)&rpc_inaddr_loopback,
1285 sizeof(rpc_inaddr_loopback));
1286 break;
1287 case AF_INET6:
1288 err = kernel_bind(sock,
1289 (struct sockaddr *)&rpc_in6addr_loopback,
1290 sizeof(rpc_in6addr_loopback));
1291 break;
1292 default:
1293 err = -EAFNOSUPPORT;
1294 goto out;
1295 }
1296 if (err < 0) {
1297 dprintk("RPC: can't bind UDP socket (%d)\n", err);
1298 goto out_release;
1299 }
1300
1301 err = kernel_connect(sock, sap, salen, 0);
1302 if (err < 0) {
1303 dprintk("RPC: can't connect UDP socket (%d)\n", err);
1304 goto out_release;
1305 }
1306
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001307 err = kernel_getsockname(sock, buf);
Chuck Lever2e738fd2012-03-01 17:01:14 -05001308 if (err < 0) {
1309 dprintk("RPC: getsockname failed (%d)\n", err);
1310 goto out_release;
1311 }
1312
1313 err = 0;
1314 if (buf->sa_family == AF_INET6) {
1315 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)buf;
1316 sin6->sin6_scope_id = 0;
1317 }
1318 dprintk("RPC: %s succeeded\n", __func__);
1319
1320out_release:
1321 sock_release(sock);
1322out:
1323 return err;
1324}
1325
1326/*
1327 * Scraping a connected socket failed, so we don't have a useable
1328 * local address. Fallback: generate an address that will prevent
1329 * the server from calling us back.
1330 *
1331 * Returns zero and fills in "buf" if successful; otherwise, a
1332 * negative errno is returned.
1333 */
1334static int rpc_anyaddr(int family, struct sockaddr *buf, size_t buflen)
1335{
1336 switch (family) {
1337 case AF_INET:
1338 if (buflen < sizeof(rpc_inaddr_loopback))
1339 return -EINVAL;
1340 memcpy(buf, &rpc_inaddr_loopback,
1341 sizeof(rpc_inaddr_loopback));
1342 break;
1343 case AF_INET6:
1344 if (buflen < sizeof(rpc_in6addr_loopback))
1345 return -EINVAL;
1346 memcpy(buf, &rpc_in6addr_loopback,
1347 sizeof(rpc_in6addr_loopback));
Trond Myklebust0b161e62015-12-30 18:14:06 -05001348 break;
Chuck Lever2e738fd2012-03-01 17:01:14 -05001349 default:
1350 dprintk("RPC: %s: address family not supported\n",
1351 __func__);
1352 return -EAFNOSUPPORT;
1353 }
1354 dprintk("RPC: %s: succeeded\n", __func__);
1355 return 0;
1356}
1357
1358/**
1359 * rpc_localaddr - discover local endpoint address for an RPC client
1360 * @clnt: RPC client structure
1361 * @buf: target buffer
1362 * @buflen: size of target buffer, in bytes
1363 *
1364 * Returns zero and fills in "buf" and "buflen" if successful;
1365 * otherwise, a negative errno is returned.
1366 *
1367 * This works even if the underlying transport is not currently connected,
1368 * or if the upper layer never previously provided a source address.
1369 *
1370 * The result of this function call is transient: multiple calls in
1371 * succession may give different results, depending on how local
1372 * networking configuration changes over time.
1373 */
1374int rpc_localaddr(struct rpc_clnt *clnt, struct sockaddr *buf, size_t buflen)
1375{
1376 struct sockaddr_storage address;
1377 struct sockaddr *sap = (struct sockaddr *)&address;
1378 struct rpc_xprt *xprt;
1379 struct net *net;
1380 size_t salen;
1381 int err;
1382
1383 rcu_read_lock();
1384 xprt = rcu_dereference(clnt->cl_xprt);
1385 salen = xprt->addrlen;
1386 memcpy(sap, &xprt->addr, salen);
1387 net = get_net(xprt->xprt_net);
1388 rcu_read_unlock();
1389
1390 rpc_set_port(sap, 0);
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001391 err = rpc_sockname(net, sap, salen, buf);
Chuck Lever2e738fd2012-03-01 17:01:14 -05001392 put_net(net);
1393 if (err != 0)
1394 /* Couldn't discover local address, return ANYADDR */
1395 return rpc_anyaddr(sap->sa_family, buf, buflen);
1396 return 0;
1397}
1398EXPORT_SYMBOL_GPL(rpc_localaddr);
1399
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400void
1401rpc_setbufsize(struct rpc_clnt *clnt, unsigned int sndsize, unsigned int rcvsize)
1402{
Trond Myklebust2446ab62012-03-01 17:00:56 -05001403 struct rpc_xprt *xprt;
1404
1405 rcu_read_lock();
1406 xprt = rcu_dereference(clnt->cl_xprt);
Chuck Lever470056c2005-08-25 16:25:56 -07001407 if (xprt->ops->set_buffer_size)
1408 xprt->ops->set_buffer_size(xprt, sndsize, rcvsize);
Trond Myklebust2446ab62012-03-01 17:00:56 -05001409 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410}
Trond Myklebuste8914c62007-07-14 15:39:59 -04001411EXPORT_SYMBOL_GPL(rpc_setbufsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
Trond Myklebust2446ab62012-03-01 17:00:56 -05001413/**
Trond Myklebust2446ab62012-03-01 17:00:56 -05001414 * rpc_net_ns - Get the network namespace for this RPC client
1415 * @clnt: RPC client to query
1416 *
1417 */
1418struct net *rpc_net_ns(struct rpc_clnt *clnt)
1419{
1420 struct net *ret;
1421
1422 rcu_read_lock();
1423 ret = rcu_dereference(clnt->cl_xprt)->xprt_net;
1424 rcu_read_unlock();
1425 return ret;
1426}
1427EXPORT_SYMBOL_GPL(rpc_net_ns);
1428
1429/**
1430 * rpc_max_payload - Get maximum payload size for a transport, in bytes
1431 * @clnt: RPC client to query
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 *
1433 * For stream transports, this is one RPC record fragment (see RFC
1434 * 1831), as we don't support multi-record requests yet. For datagram
1435 * transports, this is the size of an IP packet minus the IP, UDP, and
1436 * RPC header sizes.
1437 */
1438size_t rpc_max_payload(struct rpc_clnt *clnt)
1439{
Trond Myklebust2446ab62012-03-01 17:00:56 -05001440 size_t ret;
1441
1442 rcu_read_lock();
1443 ret = rcu_dereference(clnt->cl_xprt)->max_payload;
1444 rcu_read_unlock();
1445 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446}
Chuck Leverb86acd52006-08-22 20:06:22 -04001447EXPORT_SYMBOL_GPL(rpc_max_payload);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
Chuck Lever35f5a422006-01-03 09:55:50 +01001449/**
Chuck Lever6b26cc82016-05-02 14:40:40 -04001450 * rpc_max_bc_payload - Get maximum backchannel payload size, in bytes
1451 * @clnt: RPC client to query
1452 */
1453size_t rpc_max_bc_payload(struct rpc_clnt *clnt)
1454{
1455 struct rpc_xprt *xprt;
1456 size_t ret;
1457
1458 rcu_read_lock();
1459 xprt = rcu_dereference(clnt->cl_xprt);
1460 ret = xprt->ops->bc_maxpayload(xprt);
1461 rcu_read_unlock();
1462 return ret;
1463}
1464EXPORT_SYMBOL_GPL(rpc_max_bc_payload);
1465
1466/**
Chuck Lever35f5a422006-01-03 09:55:50 +01001467 * rpc_force_rebind - force transport to check that remote port is unchanged
1468 * @clnt: client to rebind
1469 *
1470 */
1471void rpc_force_rebind(struct rpc_clnt *clnt)
1472{
Trond Myklebust2446ab62012-03-01 17:00:56 -05001473 if (clnt->cl_autobind) {
1474 rcu_read_lock();
1475 xprt_clear_bound(rcu_dereference(clnt->cl_xprt));
1476 rcu_read_unlock();
1477 }
Chuck Lever35f5a422006-01-03 09:55:50 +01001478}
Chuck Leverb86acd52006-08-22 20:06:22 -04001479EXPORT_SYMBOL_GPL(rpc_force_rebind);
Chuck Lever35f5a422006-01-03 09:55:50 +01001480
Trond Myklebust9e6fa0b2019-04-07 13:58:45 -04001481static int
1482__rpc_restart_call(struct rpc_task *task, void (*action)(struct rpc_task *))
Andy Adamsonaae20062009-04-01 09:22:40 -04001483{
Trond Myklebust494314c2014-03-20 12:59:09 -04001484 task->tk_status = 0;
Trond Myklebust5ad64b32019-04-07 13:58:54 -04001485 task->tk_rpc_status = 0;
Trond Myklebust9e6fa0b2019-04-07 13:58:45 -04001486 task->tk_action = action;
Trond Myklebustf1f88fc2010-07-31 14:29:07 -04001487 return 1;
Andy Adamsonaae20062009-04-01 09:22:40 -04001488}
Andy Adamsonaae20062009-04-01 09:22:40 -04001489
1490/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 * Restart an (async) RPC call. Usually called from within the
1492 * exit handler.
1493 */
Trond Myklebustf1f88fc2010-07-31 14:29:07 -04001494int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495rpc_restart_call(struct rpc_task *task)
1496{
Trond Myklebust9e6fa0b2019-04-07 13:58:45 -04001497 return __rpc_restart_call(task, call_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498}
Trond Myklebuste8914c62007-07-14 15:39:59 -04001499EXPORT_SYMBOL_GPL(rpc_restart_call);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
Trond Myklebust9e6fa0b2019-04-07 13:58:45 -04001501/*
1502 * Restart an (async) RPC call from the call_prepare state.
1503 * Usually called from within the exit handler.
1504 */
1505int
1506rpc_restart_call_prepare(struct rpc_task *task)
1507{
1508 if (task->tk_ops->rpc_call_prepare != NULL)
1509 return __rpc_restart_call(task, rpc_prepare_task);
1510 return rpc_restart_call(task);
1511}
1512EXPORT_SYMBOL_GPL(rpc_restart_call_prepare);
1513
Jeff Laytonb4b9d2c2014-11-26 14:44:43 -05001514const char
1515*rpc_proc_name(const struct rpc_task *task)
Chuck Lever3748f1e2008-05-21 17:09:12 -04001516{
1517 const struct rpc_procinfo *proc = task->tk_msg.rpc_proc;
1518
1519 if (proc) {
1520 if (proc->p_name)
1521 return proc->p_name;
1522 else
1523 return "NULL";
1524 } else
1525 return "no proc";
1526}
Chuck Lever3748f1e2008-05-21 17:09:12 -04001527
Trond Myklebust5ad64b32019-04-07 13:58:54 -04001528static void
1529__rpc_call_rpcerror(struct rpc_task *task, int tk_status, int rpc_status)
1530{
1531 task->tk_rpc_status = rpc_status;
1532 rpc_exit(task, tk_status);
1533}
1534
1535static void
1536rpc_call_rpcerror(struct rpc_task *task, int status)
1537{
1538 __rpc_call_rpcerror(task, status, status);
1539}
1540
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541/*
1542 * 0. Initial state
1543 *
1544 * Other FSM states can be visited zero or more times, but
1545 * this state is visited exactly once for each RPC.
1546 */
1547static void
1548call_start(struct rpc_task *task)
1549{
1550 struct rpc_clnt *clnt = task->tk_client;
Christoph Hellwig1c5876d2017-05-08 23:27:10 +02001551 int idx = task->tk_msg.rpc_proc->p_statidx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552
Chuck Leverc435da62017-11-03 13:46:14 -04001553 trace_rpc_request(task);
Chuck Lever3748f1e2008-05-21 17:09:12 -04001554 dprintk("RPC: %5u call_start %s%d proc %s (%s)\n", task->tk_pid,
Trond Myklebust55909f22013-08-23 11:48:15 -04001555 clnt->cl_program->name, clnt->cl_vers,
Chuck Lever3748f1e2008-05-21 17:09:12 -04001556 rpc_proc_name(task),
Chuck Lever46121cf2007-01-31 12:14:08 -05001557 (RPC_IS_ASYNC(task) ? "async" : "sync"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558
Christoph Hellwig1c5876d2017-05-08 23:27:10 +02001559 /* Increment call count (version might not be valid for ping) */
1560 if (clnt->cl_program->version[clnt->cl_vers])
1561 clnt->cl_program->version[clnt->cl_vers]->counts[idx]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 clnt->cl_stats->rpccnt++;
1563 task->tk_action = call_reserve;
Bill Baker0f90be12018-06-19 16:24:58 -05001564 rpc_task_set_transport(task, clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565}
1566
1567/*
1568 * 1. Reserve an RPC call slot
1569 */
1570static void
1571call_reserve(struct rpc_task *task)
1572{
Chuck Lever46121cf2007-01-31 12:14:08 -05001573 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 task->tk_status = 0;
1576 task->tk_action = call_reserveresult;
1577 xprt_reserve(task);
1578}
1579
Trond Myklebustba60eb22013-04-14 10:49:37 -04001580static void call_retry_reserve(struct rpc_task *task);
1581
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582/*
1583 * 1b. Grok the result of xprt_reserve()
1584 */
1585static void
1586call_reserveresult(struct rpc_task *task)
1587{
1588 int status = task->tk_status;
1589
Chuck Lever46121cf2007-01-31 12:14:08 -05001590 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591
1592 /*
1593 * After a call to xprt_reserve(), we must have either
1594 * a request slot or else an error status.
1595 */
1596 task->tk_status = 0;
1597 if (status >= 0) {
1598 if (task->tk_rqstp) {
J. Bruce Fieldsf2d47d02010-09-12 19:55:25 -04001599 task->tk_action = call_refresh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 return;
1601 }
1602
1603 printk(KERN_ERR "%s: status=%d, but no request slot, exiting\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08001604 __func__, status);
Trond Myklebust5ad64b32019-04-07 13:58:54 -04001605 rpc_call_rpcerror(task, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 return;
1607 }
1608
1609 /*
1610 * Even though there was an error, we may have acquired
1611 * a request slot somehow. Make sure not to leak it.
1612 */
1613 if (task->tk_rqstp) {
1614 printk(KERN_ERR "%s: status=%d, request allocated anyway\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08001615 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 xprt_release(task);
1617 }
1618
1619 switch (status) {
Trond Myklebust1afeaf52012-05-19 12:12:53 -04001620 case -ENOMEM:
1621 rpc_delay(task, HZ >> 2);
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05001622 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 case -EAGAIN: /* woken up; retry */
Trond Myklebustba60eb22013-04-14 10:49:37 -04001624 task->tk_action = call_retry_reserve;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 return;
1626 case -EIO: /* probably a shutdown */
1627 break;
1628 default:
1629 printk(KERN_ERR "%s: unrecognized error %d, exiting\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08001630 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 break;
1632 }
Trond Myklebust5ad64b32019-04-07 13:58:54 -04001633 rpc_call_rpcerror(task, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634}
1635
1636/*
Trond Myklebustba60eb22013-04-14 10:49:37 -04001637 * 1c. Retry reserving an RPC call slot
1638 */
1639static void
1640call_retry_reserve(struct rpc_task *task)
1641{
1642 dprint_status(task);
1643
1644 task->tk_status = 0;
1645 task->tk_action = call_reserveresult;
1646 xprt_retry_reserve(task);
1647}
1648
1649/*
J. Bruce Fields55576242010-09-12 19:55:25 -04001650 * 2. Bind and/or refresh the credentials
1651 */
1652static void
1653call_refresh(struct rpc_task *task)
1654{
1655 dprint_status(task);
1656
1657 task->tk_action = call_refreshresult;
1658 task->tk_status = 0;
1659 task->tk_client->cl_stats->rpcauthrefresh++;
1660 rpcauth_refreshcred(task);
1661}
1662
1663/*
1664 * 2a. Process the results of a credential refresh
1665 */
1666static void
1667call_refreshresult(struct rpc_task *task)
1668{
1669 int status = task->tk_status;
1670
1671 dprint_status(task);
1672
1673 task->tk_status = 0;
Trond Myklebust5fc43972010-11-20 11:13:31 -05001674 task->tk_action = call_refresh;
J. Bruce Fields55576242010-09-12 19:55:25 -04001675 switch (status) {
Trond Myklebust5fc43972010-11-20 11:13:31 -05001676 case 0:
Weston Andros Adamson6ff33b72013-12-17 12:16:11 -05001677 if (rpcauth_uptodatecred(task)) {
Trond Myklebust5fc43972010-11-20 11:13:31 -05001678 task->tk_action = call_allocate;
Weston Andros Adamson6ff33b72013-12-17 12:16:11 -05001679 return;
1680 }
1681 /* Use rate-limiting and a max number of retries if refresh
1682 * had status 0 but failed to update the cred.
1683 */
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05001684 /* fall through */
J. Bruce Fields55576242010-09-12 19:55:25 -04001685 case -ETIMEDOUT:
1686 rpc_delay(task, 3*HZ);
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05001687 /* fall through */
Trond Myklebust5fc43972010-11-20 11:13:31 -05001688 case -EAGAIN:
1689 status = -EACCES;
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05001690 /* fall through */
Andy Adamsonf1ff0c22013-08-14 11:59:13 -04001691 case -EKEYEXPIRED:
Trond Myklebust5fc43972010-11-20 11:13:31 -05001692 if (!task->tk_cred_retry)
1693 break;
1694 task->tk_cred_retry--;
1695 dprintk("RPC: %5u %s: retry refresh creds\n",
1696 task->tk_pid, __func__);
1697 return;
J. Bruce Fields55576242010-09-12 19:55:25 -04001698 }
Trond Myklebust5fc43972010-11-20 11:13:31 -05001699 dprintk("RPC: %5u %s: refresh creds failed with error %d\n",
1700 task->tk_pid, __func__, status);
Trond Myklebust5ad64b32019-04-07 13:58:54 -04001701 rpc_call_rpcerror(task, status);
J. Bruce Fields55576242010-09-12 19:55:25 -04001702}
1703
1704/*
1705 * 2b. Allocate the buffer. For details, see sched.c:rpc_malloc.
Chuck Lever02107142006-01-03 09:55:49 +01001706 * (Note: buffer memory is freed in xprt_release).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 */
1708static void
1709call_allocate(struct rpc_task *task)
1710{
Chuck Lever2c94b8e2019-02-11 11:25:41 -05001711 const struct rpc_auth *auth = task->tk_rqstp->rq_cred->cr_auth;
Chuck Lever02107142006-01-03 09:55:49 +01001712 struct rpc_rqst *req = task->tk_rqstp;
Trond Myklebusta4f08352013-01-08 09:10:21 -05001713 struct rpc_xprt *xprt = req->rq_xprt;
Christoph Hellwig499b4982017-05-12 15:36:49 +02001714 const struct rpc_procinfo *proc = task->tk_msg.rpc_proc;
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04001715 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716
Chuck Lever46121cf2007-01-31 12:14:08 -05001717 dprint_status(task);
1718
Chuck Lever2bea90d2007-03-29 16:47:53 -04001719 task->tk_status = 0;
Trond Myklebust762e4e62018-08-24 16:28:28 -04001720 task->tk_action = call_encode;
Chuck Lever2bea90d2007-03-29 16:47:53 -04001721
Trond Myklebustaf6b61d2019-04-11 15:16:52 -04001722 if (req->rq_buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 return;
1724
Chuck Lever2bea90d2007-03-29 16:47:53 -04001725 if (proc->p_proc != 0) {
1726 BUG_ON(proc->p_arglen == 0);
1727 if (proc->p_decode != NULL)
1728 BUG_ON(proc->p_replen == 0);
1729 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730
Chuck Lever2bea90d2007-03-29 16:47:53 -04001731 /*
1732 * Calculate the size (in quads) of the RPC call
1733 * and reply headers, and convert both values
1734 * to byte sizes.
1735 */
Chuck Lever2c94b8e2019-02-11 11:25:41 -05001736 req->rq_callsize = RPC_CALLHDRSIZE + (auth->au_cslack << 1) +
1737 proc->p_arglen;
Chuck Lever2bea90d2007-03-29 16:47:53 -04001738 req->rq_callsize <<= 2;
Trond Myklebust51314962019-03-15 12:55:59 -04001739 /*
1740 * Note: the reply buffer must at minimum allocate enough space
1741 * for the 'struct accepted_reply' from RFC5531.
1742 */
1743 req->rq_rcvsize = RPC_REPHDRSIZE + auth->au_rslack + \
1744 max_t(size_t, proc->p_replen, 2);
Chuck Lever2bea90d2007-03-29 16:47:53 -04001745 req->rq_rcvsize <<= 2;
1746
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04001747 status = xprt->ops->buf_alloc(task);
Chuck Lever4a068252015-05-11 14:02:25 -04001748 xprt_inject_disconnect(xprt);
Trond Myklebustaf6b61d2019-04-11 15:16:52 -04001749 if (status == 0)
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04001750 return;
1751 if (status != -ENOMEM) {
Trond Myklebust5ad64b32019-04-07 13:58:54 -04001752 rpc_call_rpcerror(task, status);
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04001753 return;
1754 }
Chuck Lever46121cf2007-01-31 12:14:08 -05001755
1756 dprintk("RPC: %5u rpc_buffer allocation failed\n", task->tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757
Trond Myklebust5afa91332011-06-17 10:14:59 -04001758 if (RPC_IS_ASYNC(task) || !fatal_signal_pending(current)) {
Trond Myklebustb6e9c712007-10-01 12:06:44 -04001759 task->tk_action = call_allocate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 rpc_delay(task, HZ>>4);
1761 return;
1762 }
1763
1764 rpc_exit(task, -ERESTARTSYS);
1765}
1766
Trond Myklebust7ebbbc62018-08-28 09:00:27 -04001767static int
Trond Myklebust940e3312005-11-09 21:45:24 -05001768rpc_task_need_encode(struct rpc_task *task)
1769{
Trond Myklebust762e4e62018-08-24 16:28:28 -04001770 return test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate) == 0 &&
1771 (!(task->tk_flags & RPC_TASK_SENT) ||
1772 !(task->tk_flags & RPC_TASK_NO_RETRANS_TIMEOUT) ||
1773 xprt_request_need_retransmit(task));
Trond Myklebust940e3312005-11-09 21:45:24 -05001774}
1775
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776static void
Chuck Leverb0e1c572008-05-21 17:09:19 -04001777rpc_xdr_encode(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 struct rpc_rqst *req = task->tk_rqstp;
Chuck Levere8680a22019-02-11 11:24:48 -05001780 struct xdr_stream xdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781
Chuck Leverb9c5bc02016-09-15 10:55:12 -04001782 xdr_buf_init(&req->rq_snd_buf,
1783 req->rq_buffer,
1784 req->rq_callsize);
1785 xdr_buf_init(&req->rq_rcv_buf,
Chuck Lever68778942016-09-15 10:55:37 -04001786 req->rq_rbuffer,
Chuck Leverb9c5bc02016-09-15 10:55:12 -04001787 req->rq_rcvsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788
Chuck Levere8680a22019-02-11 11:24:48 -05001789 req->rq_snd_buf.head[0].iov_len = 0;
1790 xdr_init_encode(&xdr, &req->rq_snd_buf,
1791 req->rq_snd_buf.head[0].iov_base, req);
1792 if (rpc_encode_header(task, &xdr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 return;
Chuck Leverb0e1c572008-05-21 17:09:19 -04001794
Chuck Levere8680a22019-02-11 11:24:48 -05001795 task->tk_status = rpcauth_wrap_req(task, &xdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796}
1797
1798/*
Trond Myklebust762e4e62018-08-24 16:28:28 -04001799 * 3. Encode arguments of an RPC call
1800 */
1801static void
1802call_encode(struct rpc_task *task)
1803{
1804 if (!rpc_task_need_encode(task))
1805 goto out;
Chuck Levere8680a22019-02-11 11:24:48 -05001806 dprint_status(task);
Trond Myklebust762e4e62018-08-24 16:28:28 -04001807 /* Encode here so that rpcsec_gss can use correct sequence number. */
1808 rpc_xdr_encode(task);
1809 /* Did the encode result in an error condition? */
1810 if (task->tk_status != 0) {
1811 /* Was the error nonfatal? */
Trond Myklebust97b78ae2019-01-02 17:53:13 -05001812 switch (task->tk_status) {
1813 case -EAGAIN:
1814 case -ENOMEM:
Trond Myklebust762e4e62018-08-24 16:28:28 -04001815 rpc_delay(task, HZ >> 4);
Trond Myklebust97b78ae2019-01-02 17:53:13 -05001816 break;
1817 case -EKEYEXPIRED:
ZhangXiaoxu9c5948c2019-04-29 17:32:31 +08001818 if (!task->tk_cred_retry) {
1819 rpc_exit(task, task->tk_status);
1820 } else {
1821 task->tk_action = call_refresh;
1822 task->tk_cred_retry--;
1823 dprintk("RPC: %5u %s: retry refresh creds\n",
1824 task->tk_pid, __func__);
1825 }
Trond Myklebust97b78ae2019-01-02 17:53:13 -05001826 break;
1827 default:
Trond Myklebust5ad64b32019-04-07 13:58:54 -04001828 rpc_call_rpcerror(task, task->tk_status);
Trond Myklebust97b78ae2019-01-02 17:53:13 -05001829 }
Trond Myklebust762e4e62018-08-24 16:28:28 -04001830 return;
Chuck Levere8680a22019-02-11 11:24:48 -05001831 } else {
1832 xprt_request_prepare(task->tk_rqstp);
Trond Myklebust762e4e62018-08-24 16:28:28 -04001833 }
1834
1835 /* Add task to reply queue before transmission to avoid races */
1836 if (rpc_reply_expected(task))
1837 xprt_request_enqueue_receive(task);
1838 xprt_request_enqueue_transmit(task);
1839out:
Trond Myklebustaf6b61d2019-04-11 15:16:52 -04001840 task->tk_action = call_transmit;
1841 /* Check that the connection is OK */
1842 if (!xprt_bound(task->tk_xprt))
1843 task->tk_action = call_bind;
1844 else if (!xprt_connected(task->tk_xprt))
1845 task->tk_action = call_connect;
Trond Myklebust762e4e62018-08-24 16:28:28 -04001846}
1847
1848/*
Trond Myklebust03e51d32019-03-10 11:17:13 -04001849 * Helpers to check if the task was already transmitted, and
1850 * to take action when that is the case.
1851 */
1852static bool
1853rpc_task_transmitted(struct rpc_task *task)
1854{
1855 return !test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate);
1856}
1857
1858static void
1859rpc_task_handle_transmitted(struct rpc_task *task)
1860{
1861 xprt_end_transmit(task);
1862 task->tk_action = call_transmit_status;
Trond Myklebust03e51d32019-03-10 11:17:13 -04001863}
1864
1865/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 * 4. Get the server port number if not yet set
1867 */
1868static void
1869call_bind(struct rpc_task *task)
1870{
Trond Myklebustad2368d2013-01-08 10:08:33 -05001871 struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872
Trond Myklebust03e51d32019-03-10 11:17:13 -04001873 if (rpc_task_transmitted(task)) {
1874 rpc_task_handle_transmitted(task);
1875 return;
1876 }
1877
Trond Myklebust009a82f2019-03-09 12:07:17 -05001878 if (xprt_bound(xprt)) {
1879 task->tk_action = call_connect;
Trond Myklebust009a82f2019-03-09 12:07:17 -05001880 return;
1881 }
1882
Chuck Lever46121cf2007-01-31 12:14:08 -05001883 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884
Trond Myklebust009a82f2019-03-09 12:07:17 -05001885 task->tk_action = call_bind_status;
Trond Myklebust4d6c6712019-03-10 12:22:39 -04001886 if (!xprt_prepare_transmit(task))
1887 return;
1888
Trond Myklebust009a82f2019-03-09 12:07:17 -05001889 xprt->ops->rpcbind(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890}
1891
1892/*
Chuck Leverda351872005-08-11 16:25:11 -04001893 * 4a. Sort out bind result
1894 */
1895static void
1896call_bind_status(struct rpc_task *task)
1897{
Chuck Lever906462a2007-09-11 18:00:47 -04001898 int status = -EIO;
Chuck Leverda351872005-08-11 16:25:11 -04001899
Trond Myklebust03e51d32019-03-10 11:17:13 -04001900 if (rpc_task_transmitted(task)) {
1901 rpc_task_handle_transmitted(task);
1902 return;
1903 }
1904
Chuck Leverda351872005-08-11 16:25:11 -04001905 if (task->tk_status >= 0) {
Chuck Lever46121cf2007-01-31 12:14:08 -05001906 dprint_status(task);
Chuck Leverda351872005-08-11 16:25:11 -04001907 task->tk_status = 0;
1908 task->tk_action = call_connect;
1909 return;
1910 }
1911
Steve Dickson5753cba2012-02-06 10:08:08 -05001912 trace_rpc_bind_status(task);
Chuck Leverda351872005-08-11 16:25:11 -04001913 switch (task->tk_status) {
Trond Myklebust381ba742008-07-07 12:18:53 -04001914 case -ENOMEM:
1915 dprintk("RPC: %5u rpcbind out of memory\n", task->tk_pid);
1916 rpc_delay(task, HZ >> 2);
Chuck Lever2429cbf2007-09-11 18:00:41 -04001917 goto retry_timeout;
Chuck Leverda351872005-08-11 16:25:11 -04001918 case -EACCES:
Chuck Lever46121cf2007-01-31 12:14:08 -05001919 dprintk("RPC: %5u remote rpcbind: RPC program/version "
1920 "unavailable\n", task->tk_pid);
Chuck Leverb79dc8c2007-09-11 18:00:52 -04001921 /* fail immediately if this is an RPC ping */
1922 if (task->tk_msg.rpc_proc->p_proc == 0) {
1923 status = -EOPNOTSUPP;
1924 break;
1925 }
Trond Myklebust0b760112011-05-31 15:15:34 -04001926 if (task->tk_rebind_retry == 0)
1927 break;
1928 task->tk_rebind_retry--;
Chuck Leverea635a52005-10-06 23:12:58 -04001929 rpc_delay(task, 3*HZ);
Trond Myklebustda45828e2006-08-31 15:44:52 -04001930 goto retry_timeout;
Trond Myklebust4d6c6712019-03-10 12:22:39 -04001931 case -EAGAIN:
1932 goto retry_timeout;
Chuck Leverda351872005-08-11 16:25:11 -04001933 case -ETIMEDOUT:
Chuck Lever46121cf2007-01-31 12:14:08 -05001934 dprintk("RPC: %5u rpcbind request timed out\n",
Chuck Leverda351872005-08-11 16:25:11 -04001935 task->tk_pid);
Trond Myklebustda45828e2006-08-31 15:44:52 -04001936 goto retry_timeout;
Chuck Leverda351872005-08-11 16:25:11 -04001937 case -EPFNOSUPPORT:
Chuck Lever906462a2007-09-11 18:00:47 -04001938 /* server doesn't support any rpcbind version we know of */
Chuck Lever012da1582009-12-03 15:58:56 -05001939 dprintk("RPC: %5u unrecognized remote rpcbind service\n",
Chuck Leverda351872005-08-11 16:25:11 -04001940 task->tk_pid);
1941 break;
1942 case -EPROTONOSUPPORT:
Chuck Lever00a6e7b2007-03-29 16:48:33 -04001943 dprintk("RPC: %5u remote rpcbind version unavailable, retrying\n",
Chuck Leverda351872005-08-11 16:25:11 -04001944 task->tk_pid);
Trond Myklebustfdb63dc2014-03-17 12:57:31 -04001945 goto retry_timeout;
Chuck Lever012da1582009-12-03 15:58:56 -05001946 case -ECONNREFUSED: /* connection problems */
1947 case -ECONNRESET:
Trond Myklebustdf277272013-12-31 13:11:43 -05001948 case -ECONNABORTED:
Chuck Lever012da1582009-12-03 15:58:56 -05001949 case -ENOTCONN:
1950 case -EHOSTDOWN:
Trond Myklebusteb5b46f2017-11-30 07:21:33 -05001951 case -ENETDOWN:
Chuck Lever012da1582009-12-03 15:58:56 -05001952 case -EHOSTUNREACH:
1953 case -ENETUNREACH:
Trond Myklebust3601c4a2014-06-30 13:42:19 -04001954 case -ENOBUFS:
Chuck Lever012da1582009-12-03 15:58:56 -05001955 case -EPIPE:
1956 dprintk("RPC: %5u remote rpcbind unreachable: %d\n",
1957 task->tk_pid, task->tk_status);
1958 if (!RPC_IS_SOFTCONN(task)) {
1959 rpc_delay(task, 5*HZ);
1960 goto retry_timeout;
1961 }
1962 status = task->tk_status;
1963 break;
Chuck Leverda351872005-08-11 16:25:11 -04001964 default:
Chuck Lever46121cf2007-01-31 12:14:08 -05001965 dprintk("RPC: %5u unrecognized rpcbind error (%d)\n",
Chuck Leverda351872005-08-11 16:25:11 -04001966 task->tk_pid, -task->tk_status);
Chuck Leverda351872005-08-11 16:25:11 -04001967 }
1968
Trond Myklebust5ad64b32019-04-07 13:58:54 -04001969 rpc_call_rpcerror(task, status);
Chuck Leverda351872005-08-11 16:25:11 -04001970 return;
1971
Trond Myklebustda45828e2006-08-31 15:44:52 -04001972retry_timeout:
Trond Myklebustfdb63dc2014-03-17 12:57:31 -04001973 task->tk_status = 0;
Trond Myklebust4d6c6712019-03-10 12:22:39 -04001974 task->tk_action = call_bind;
Trond Myklebustcea57782019-03-09 16:06:47 -05001975 rpc_check_timeout(task);
Chuck Leverda351872005-08-11 16:25:11 -04001976}
1977
1978/*
1979 * 4b. Connect to the RPC server
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 */
1981static void
1982call_connect(struct rpc_task *task)
1983{
Trond Myklebustad2368d2013-01-08 10:08:33 -05001984 struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985
Trond Myklebust03e51d32019-03-10 11:17:13 -04001986 if (rpc_task_transmitted(task)) {
1987 rpc_task_handle_transmitted(task);
1988 return;
1989 }
1990
Trond Myklebust009a82f2019-03-09 12:07:17 -05001991 if (xprt_connected(xprt)) {
1992 task->tk_action = call_transmit;
Trond Myklebust009a82f2019-03-09 12:07:17 -05001993 return;
1994 }
1995
Chuck Lever46121cf2007-01-31 12:14:08 -05001996 dprintk("RPC: %5u call_connect xprt %p %s connected\n",
Chuck Leverda351872005-08-11 16:25:11 -04001997 task->tk_pid, xprt,
1998 (xprt_connected(xprt) ? "is" : "is not"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999
Trond Myklebust009a82f2019-03-09 12:07:17 -05002000 task->tk_action = call_connect_status;
2001 if (task->tk_status < 0)
2002 return;
2003 if (task->tk_flags & RPC_TASK_NOCONNECT) {
Trond Myklebust5ad64b32019-04-07 13:58:54 -04002004 rpc_call_rpcerror(task, -ENOTCONN);
Trond Myklebust009a82f2019-03-09 12:07:17 -05002005 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 }
Trond Myklebust4d6c6712019-03-10 12:22:39 -04002007 if (!xprt_prepare_transmit(task))
2008 return;
Trond Myklebust009a82f2019-03-09 12:07:17 -05002009 xprt_connect(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010}
2011
2012/*
Chuck Leverda351872005-08-11 16:25:11 -04002013 * 4c. Sort out connect result
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 */
2015static void
2016call_connect_status(struct rpc_task *task)
2017{
2018 struct rpc_clnt *clnt = task->tk_client;
2019 int status = task->tk_status;
2020
Trond Myklebust03e51d32019-03-10 11:17:13 -04002021 if (rpc_task_transmitted(task)) {
2022 rpc_task_handle_transmitted(task);
Trond Myklebust9bd11522018-11-30 12:48:47 -05002023 return;
2024 }
2025
Chuck Lever46121cf2007-01-31 12:14:08 -05002026 dprint_status(task);
Chuck Leverda351872005-08-11 16:25:11 -04002027
Chuck Levere671edb2018-03-16 10:33:44 -04002028 trace_rpc_connect_status(task);
Trond Myklebust561ec162013-09-26 15:22:45 -04002029 task->tk_status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 switch (status) {
Trond Myklebust3ed5e2a2013-03-04 17:29:33 -05002031 case -ECONNREFUSED:
NeilBrownfd01b252017-08-18 17:12:51 +10002032 /* A positive refusal suggests a rebind is needed. */
2033 if (RPC_IS_SOFTCONN(task))
2034 break;
2035 if (clnt->cl_autobind) {
2036 rpc_force_rebind(clnt);
Trond Myklebust7b3fef82019-03-07 14:10:32 -05002037 goto out_retry;
NeilBrownfd01b252017-08-18 17:12:51 +10002038 }
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05002039 /* fall through */
Trond Myklebust3ed5e2a2013-03-04 17:29:33 -05002040 case -ECONNRESET:
Trond Myklebustdf277272013-12-31 13:11:43 -05002041 case -ECONNABORTED:
Trond Myklebusteb5b46f2017-11-30 07:21:33 -05002042 case -ENETDOWN:
Trond Myklebust3ed5e2a2013-03-04 17:29:33 -05002043 case -ENETUNREACH:
Trond Myklebustdf277272013-12-31 13:11:43 -05002044 case -EHOSTUNREACH:
Trond Myklebust3913c782015-02-08 21:44:04 -05002045 case -EADDRINUSE:
Trond Myklebust3601c4a2014-06-30 13:42:19 -04002046 case -ENOBUFS:
Trond Myklebust2fc193c2014-07-03 00:02:57 -04002047 case -EPIPE:
NeilBrown2c2ee6d2016-11-23 14:44:58 +11002048 xprt_conditional_disconnect(task->tk_rqstp->rq_xprt,
2049 task->tk_rqstp->rq_connect_cookie);
Trond Myklebust3ed5e2a2013-03-04 17:29:33 -05002050 if (RPC_IS_SOFTCONN(task))
2051 break;
Steve Dickson1fa3e2e2014-03-20 11:23:03 -04002052 /* retry with existing socket, after a delay */
2053 rpc_delay(task, 3*HZ);
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05002054 /* fall through */
Trond Myklebust0445f922018-12-17 13:34:59 -05002055 case -ENOTCONN:
Trond Myklebust3ed5e2a2013-03-04 17:29:33 -05002056 case -EAGAIN:
Trond Myklebust485f2252014-03-17 12:51:44 -04002057 case -ETIMEDOUT:
Trond Myklebust7b3fef82019-03-07 14:10:32 -05002058 goto out_retry;
Trond Myklebust561ec162013-09-26 15:22:45 -04002059 case 0:
Trond Myklebust3ed5e2a2013-03-04 17:29:33 -05002060 clnt->cl_stats->netreconn++;
2061 task->tk_action = call_transmit;
2062 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 }
Trond Myklebust5ad64b32019-04-07 13:58:54 -04002064 rpc_call_rpcerror(task, status);
Trond Myklebust7b3fef82019-03-07 14:10:32 -05002065 return;
2066out_retry:
2067 /* Check for timeouts before looping back to call_bind */
2068 task->tk_action = call_bind;
2069 rpc_check_timeout(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070}
2071
2072/*
2073 * 5. Transmit the RPC request, and wait for reply
2074 */
2075static void
2076call_transmit(struct rpc_task *task)
2077{
Trond Myklebust03e51d32019-03-10 11:17:13 -04002078 if (rpc_task_transmitted(task)) {
2079 rpc_task_handle_transmitted(task);
2080 return;
2081 }
2082
Chuck Lever46121cf2007-01-31 12:14:08 -05002083 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084
Trond Myklebusted7dc972019-03-04 14:19:31 -05002085 task->tk_action = call_transmit_status;
Trond Myklebust009a82f2019-03-09 12:07:17 -05002086 if (!xprt_prepare_transmit(task))
2087 return;
2088 task->tk_status = 0;
Trond Myklebustc5445772018-09-03 23:39:27 -04002089 if (test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate)) {
Trond Myklebust009a82f2019-03-09 12:07:17 -05002090 if (!xprt_connected(task->tk_xprt)) {
2091 task->tk_status = -ENOTCONN;
Trond Myklebustc5445772018-09-03 23:39:27 -04002092 return;
Trond Myklebusted7dc972019-03-04 14:19:31 -05002093 }
Trond Myklebust009a82f2019-03-09 12:07:17 -05002094 xprt_transmit(task);
Trond Myklebustc5445772018-09-03 23:39:27 -04002095 }
Trond Myklebustc5445772018-09-03 23:39:27 -04002096 xprt_end_transmit(task);
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04002097}
2098
2099/*
2100 * 5a. Handle cleanup after a transmission
2101 */
2102static void
2103call_transmit_status(struct rpc_task *task)
2104{
2105 task->tk_action = call_status;
Chuck Lever206a1342009-12-03 15:58:56 -05002106
2107 /*
2108 * Common case: success. Force the compiler to put this
2109 * test first.
2110 */
Trond Myklebust009a82f2019-03-09 12:07:17 -05002111 if (rpc_task_transmitted(task)) {
Trond Myklebusta7b1a482019-04-15 11:54:13 -04002112 task->tk_status = 0;
2113 xprt_request_wait_receive(task);
Chuck Lever206a1342009-12-03 15:58:56 -05002114 return;
2115 }
2116
Trond Myklebust15f081c2009-03-11 14:37:57 -04002117 switch (task->tk_status) {
Trond Myklebust15f081c2009-03-11 14:37:57 -04002118 default:
Chuck Lever206a1342009-12-03 15:58:56 -05002119 dprint_status(task);
Trond Myklebust75891f52018-09-03 17:37:36 -04002120 break;
Trond Myklebust78b576c2018-08-28 09:20:10 -04002121 case -EBADMSG:
Trond Myklebust762e4e62018-08-24 16:28:28 -04002122 task->tk_status = 0;
2123 task->tk_action = call_encode;
Trond Myklebust78b576c2018-08-28 09:20:10 -04002124 break;
Trond Myklebust15f081c2009-03-11 14:37:57 -04002125 /*
2126 * Special cases: if we've been waiting on the
2127 * socket's write_space() callback, or if the
2128 * socket just returned a connection error,
2129 * then hold onto the transport lock.
2130 */
Trond Myklebust78b576c2018-08-28 09:20:10 -04002131 case -ENOBUFS:
2132 rpc_delay(task, HZ>>2);
2133 /* fall through */
Trond Myklebustc5445772018-09-03 23:39:27 -04002134 case -EBADSLT:
Trond Myklebust78b576c2018-08-28 09:20:10 -04002135 case -EAGAIN:
2136 task->tk_action = call_transmit;
2137 task->tk_status = 0;
2138 break;
Trond Myklebust15f081c2009-03-11 14:37:57 -04002139 case -ECONNREFUSED:
Trond Myklebust15f081c2009-03-11 14:37:57 -04002140 case -EHOSTDOWN:
Trond Myklebusteb5b46f2017-11-30 07:21:33 -05002141 case -ENETDOWN:
Trond Myklebust15f081c2009-03-11 14:37:57 -04002142 case -EHOSTUNREACH:
2143 case -ENETUNREACH:
Jason Baron3dedbb52014-09-24 18:08:04 +00002144 case -EPERM:
Chuck Lever09a21c42009-12-03 15:58:56 -05002145 if (RPC_IS_SOFTCONN(task)) {
Chuck Levera25a4cb2018-03-16 10:33:55 -04002146 if (!task->tk_msg.rpc_proc->p_proc)
2147 trace_xprt_ping(task->tk_xprt,
2148 task->tk_status);
Trond Myklebust5ad64b32019-04-07 13:58:54 -04002149 rpc_call_rpcerror(task, task->tk_status);
Trond Myklebust7b3fef82019-03-07 14:10:32 -05002150 return;
Chuck Lever09a21c42009-12-03 15:58:56 -05002151 }
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05002152 /* fall through */
Chuck Lever09a21c42009-12-03 15:58:56 -05002153 case -ECONNRESET:
Trond Myklebustdf277272013-12-31 13:11:43 -05002154 case -ECONNABORTED:
Trond Myklebust3913c782015-02-08 21:44:04 -05002155 case -EADDRINUSE:
Chuck Lever09a21c42009-12-03 15:58:56 -05002156 case -ENOTCONN:
Trond Myklebustc8485e42009-03-11 14:37:59 -04002157 case -EPIPE:
Trond Myklebusted7dc972019-03-04 14:19:31 -05002158 task->tk_action = call_bind;
2159 task->tk_status = 0;
Trond Myklebust7ebbbc62018-08-28 09:00:27 -04002160 break;
Trond Myklebust15f081c2009-03-11 14:37:57 -04002161 }
Trond Myklebust7b3fef82019-03-07 14:10:32 -05002162 rpc_check_timeout(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163}
2164
Trond Myklebust9e00abc2011-07-13 19:20:49 -04002165#if defined(CONFIG_SUNRPC_BACKCHANNEL)
Trond Myklebust477687e2019-03-05 07:30:48 -05002166static void call_bc_transmit(struct rpc_task *task);
2167static void call_bc_transmit_status(struct rpc_task *task);
2168
2169static void
2170call_bc_encode(struct rpc_task *task)
2171{
2172 xprt_request_enqueue_transmit(task);
2173 task->tk_action = call_bc_transmit;
2174}
2175
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04002176/*
2177 * 5b. Send the backchannel RPC reply. On error, drop the reply. In
2178 * addition, disconnect on connectivity errors.
2179 */
2180static void
2181call_bc_transmit(struct rpc_task *task)
2182{
Trond Myklebust477687e2019-03-05 07:30:48 -05002183 task->tk_action = call_bc_transmit_status;
2184 if (test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate)) {
2185 if (!xprt_prepare_transmit(task))
2186 return;
2187 task->tk_status = 0;
2188 xprt_transmit(task);
2189 }
2190 xprt_end_transmit(task);
2191}
2192
2193static void
2194call_bc_transmit_status(struct rpc_task *task)
2195{
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04002196 struct rpc_rqst *req = task->tk_rqstp;
2197
Trond Myklebusta7b1a482019-04-15 11:54:13 -04002198 if (rpc_task_transmitted(task))
2199 task->tk_status = 0;
2200
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04002201 dprint_status(task);
Trond Myklebust477687e2019-03-05 07:30:48 -05002202
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04002203 switch (task->tk_status) {
2204 case 0:
2205 /* Success */
Trond Myklebusteb5b46f2017-11-30 07:21:33 -05002206 case -ENETDOWN:
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04002207 case -EHOSTDOWN:
2208 case -EHOSTUNREACH:
2209 case -ENETUNREACH:
Trond Myklebust38325912015-06-19 13:04:13 -04002210 case -ECONNRESET:
2211 case -ECONNREFUSED:
2212 case -EADDRINUSE:
2213 case -ENOTCONN:
2214 case -EPIPE:
2215 break;
Trond Myklebust477687e2019-03-05 07:30:48 -05002216 case -ENOBUFS:
2217 rpc_delay(task, HZ>>2);
2218 /* fall through */
2219 case -EBADSLT:
Trond Myklebustc5445772018-09-03 23:39:27 -04002220 case -EAGAIN:
Trond Myklebust477687e2019-03-05 07:30:48 -05002221 task->tk_status = 0;
2222 task->tk_action = call_bc_transmit;
2223 return;
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04002224 case -ETIMEDOUT:
2225 /*
2226 * Problem reaching the server. Disconnect and let the
2227 * forechannel reestablish the connection. The server will
2228 * have to retransmit the backchannel request and we'll
2229 * reprocess it. Since these ops are idempotent, there's no
2230 * need to cache our reply at this time.
2231 */
2232 printk(KERN_NOTICE "RPC: Could not send backchannel reply "
2233 "error: %d\n", task->tk_status);
Trond Myklebusta4f08352013-01-08 09:10:21 -05002234 xprt_conditional_disconnect(req->rq_xprt,
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04002235 req->rq_connect_cookie);
2236 break;
2237 default:
2238 /*
2239 * We were unable to reply and will have to drop the
2240 * request. The server should reconnect and retransmit.
2241 */
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04002242 printk(KERN_NOTICE "RPC: Could not send backchannel reply "
2243 "error: %d\n", task->tk_status);
2244 break;
2245 }
Trond Myklebust1193d582015-06-02 11:53:21 -04002246 task->tk_action = rpc_exit_task;
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04002247}
Trond Myklebust9e00abc2011-07-13 19:20:49 -04002248#endif /* CONFIG_SUNRPC_BACKCHANNEL */
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04002249
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250/*
2251 * 6. Sort out the RPC call status
2252 */
2253static void
2254call_status(struct rpc_task *task)
2255{
2256 struct rpc_clnt *clnt = task->tk_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 int status;
2258
Chuck Levera25a4cb2018-03-16 10:33:55 -04002259 if (!task->tk_msg.rpc_proc->p_proc)
2260 trace_xprt_ping(task->tk_xprt, task->tk_status);
2261
Chuck Lever46121cf2007-01-31 12:14:08 -05002262 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263
2264 status = task->tk_status;
2265 if (status >= 0) {
2266 task->tk_action = call_decode;
2267 return;
2268 }
2269
Steve Dickson5753cba2012-02-06 10:08:08 -05002270 trace_rpc_call_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 task->tk_status = 0;
2272 switch(status) {
Trond Myklebust76303992006-08-30 14:32:49 -04002273 case -EHOSTDOWN:
Trond Myklebusteb5b46f2017-11-30 07:21:33 -05002274 case -ENETDOWN:
Trond Myklebust76303992006-08-30 14:32:49 -04002275 case -EHOSTUNREACH:
2276 case -ENETUNREACH:
Jason Baron3dedbb52014-09-24 18:08:04 +00002277 case -EPERM:
Trond Myklebustcea57782019-03-09 16:06:47 -05002278 if (RPC_IS_SOFTCONN(task))
2279 goto out_exit;
Trond Myklebust76303992006-08-30 14:32:49 -04002280 /*
2281 * Delay any retries for 3 seconds, then handle as if it
2282 * were a timeout.
2283 */
2284 rpc_delay(task, 3*HZ);
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05002285 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 case -ETIMEDOUT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 break;
2288 case -ECONNREFUSED:
Trond Myklebustdf277272013-12-31 13:11:43 -05002289 case -ECONNRESET:
2290 case -ECONNABORTED:
Olga Kornievskaiaec6017d2019-05-29 10:46:00 -04002291 case -ENOTCONN:
Chuck Lever35f5a422006-01-03 09:55:50 +01002292 rpc_force_rebind(clnt);
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05002293 /* fall through */
Trond Myklebust3913c782015-02-08 21:44:04 -05002294 case -EADDRINUSE:
Trond Myklebustc8485e42009-03-11 14:37:59 -04002295 rpc_delay(task, 3*HZ);
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05002296 /* fall through */
Trond Myklebustc8485e42009-03-11 14:37:59 -04002297 case -EPIPE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 case -EAGAIN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 break;
2300 case -EIO:
2301 /* shutdown or soft timeout */
Trond Myklebustcea57782019-03-09 16:06:47 -05002302 goto out_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 default:
Olga Kornievskaiab6b61522008-06-09 16:51:31 -04002304 if (clnt->cl_chatty)
2305 printk("%s: RPC call returned error %d\n",
Trond Myklebust55909f22013-08-23 11:48:15 -04002306 clnt->cl_program->name, -status);
Trond Myklebustcea57782019-03-09 16:06:47 -05002307 goto out_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 }
Trond Myklebustcea57782019-03-09 16:06:47 -05002309 task->tk_action = call_encode;
2310 rpc_check_timeout(task);
2311 return;
2312out_exit:
Trond Myklebust5ad64b32019-04-07 13:58:54 -04002313 rpc_call_rpcerror(task, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314}
2315
Trond Myklebustd84dd3f2019-03-19 11:24:54 -04002316static bool
2317rpc_check_connected(const struct rpc_rqst *req)
2318{
2319 /* No allocated request or transport? return true */
2320 if (!req || !req->rq_xprt)
2321 return true;
2322 return xprt_connected(req->rq_xprt);
2323}
2324
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325static void
Trond Myklebust7b3fef82019-03-07 14:10:32 -05002326rpc_check_timeout(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327{
2328 struct rpc_clnt *clnt = task->tk_client;
2329
Trond Myklebust7b3fef82019-03-07 14:10:32 -05002330 if (xprt_adjust_timeout(task->tk_rqstp) == 0)
2331 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332
Chuck Lever46121cf2007-01-31 12:14:08 -05002333 dprintk("RPC: %5u call_timeout (major)\n", task->tk_pid);
Chuck Leveref759a22006-03-20 13:44:17 -05002334 task->tk_timeouts++;
2335
Trond Myklebustd84dd3f2019-03-19 11:24:54 -04002336 if (RPC_IS_SOFTCONN(task) && !rpc_check_connected(task->tk_rqstp)) {
Trond Myklebust5ad64b32019-04-07 13:58:54 -04002337 rpc_call_rpcerror(task, -ETIMEDOUT);
Chuck Lever3a28bec2009-12-03 15:58:56 -05002338 return;
2339 }
Trond Myklebustd84dd3f2019-03-19 11:24:54 -04002340
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 if (RPC_IS_SOFT(task)) {
Trond Myklebuste4ec48d2019-04-07 13:58:55 -04002342 /*
2343 * Once a "no retrans timeout" soft tasks (a.k.a NFSv4) has
2344 * been sent, it should time out only if the transport
2345 * connection gets terminally broken.
2346 */
2347 if ((task->tk_flags & RPC_TASK_NO_RETRANS_TIMEOUT) &&
2348 rpc_check_connected(task->tk_rqstp))
2349 return;
2350
Joe Perchescac5d072012-07-18 11:17:11 -07002351 if (clnt->cl_chatty) {
Trond Myklebust0729d992019-04-07 13:58:57 -04002352 pr_notice_ratelimited(
2353 "%s: server %s not responding, timed out\n",
Trond Myklebust55909f22013-08-23 11:48:15 -04002354 clnt->cl_program->name,
Trond Myklebustfb43d172016-01-30 16:39:26 -05002355 task->tk_xprt->servername);
Joe Perchescac5d072012-07-18 11:17:11 -07002356 }
Trond Myklebust7494d002011-04-24 14:28:45 -04002357 if (task->tk_flags & RPC_TASK_TIMEOUT)
Trond Myklebust5ad64b32019-04-07 13:58:54 -04002358 rpc_call_rpcerror(task, -ETIMEDOUT);
Trond Myklebust7494d002011-04-24 14:28:45 -04002359 else
Trond Myklebust5ad64b32019-04-07 13:58:54 -04002360 __rpc_call_rpcerror(task, -EIO, -ETIMEDOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 return;
2362 }
2363
Chuck Leverf518e35a2006-01-03 09:55:52 +01002364 if (!(task->tk_flags & RPC_CALL_MAJORSEEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 task->tk_flags |= RPC_CALL_MAJORSEEN;
Trond Myklebust4e0038b2012-03-01 17:01:05 -05002366 if (clnt->cl_chatty) {
Trond Myklebust0729d992019-04-07 13:58:57 -04002367 pr_notice_ratelimited(
2368 "%s: server %s not responding, still trying\n",
2369 clnt->cl_program->name,
2370 task->tk_xprt->servername);
Trond Myklebust4e0038b2012-03-01 17:01:05 -05002371 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 }
Chuck Lever35f5a422006-01-03 09:55:50 +01002373 rpc_force_rebind(clnt);
Trond Myklebustb48633b2008-04-22 16:47:55 -04002374 /*
2375 * Did our request time out due to an RPCSEC_GSS out-of-sequence
2376 * event? RFC2203 requires the server to drop all such requests.
2377 */
2378 rpcauth_invalcred(task);
Trond Myklebust7b3fef82019-03-07 14:10:32 -05002379}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380
Trond Myklebust7b3fef82019-03-07 14:10:32 -05002381/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 * 7. Decode the RPC reply
2383 */
2384static void
2385call_decode(struct rpc_task *task)
2386{
2387 struct rpc_clnt *clnt = task->tk_client;
2388 struct rpc_rqst *req = task->tk_rqstp;
Chuck Levera0584ee2019-02-11 11:24:58 -05002389 struct xdr_stream xdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390
Vasily Averin726fd6a2011-06-01 16:23:59 +04002391 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392
Chuck Levera0584ee2019-02-11 11:24:58 -05002393 if (!task->tk_msg.rpc_proc->p_decode) {
Trond Myklebust9ee94d32018-08-28 16:27:31 -04002394 task->tk_action = rpc_exit_task;
2395 return;
2396 }
2397
Chuck Leverf518e35a2006-01-03 09:55:52 +01002398 if (task->tk_flags & RPC_CALL_MAJORSEEN) {
Trond Myklebust4e0038b2012-03-01 17:01:05 -05002399 if (clnt->cl_chatty) {
Trond Myklebust0729d992019-04-07 13:58:57 -04002400 pr_notice_ratelimited("%s: server %s OK\n",
Trond Myklebust55909f22013-08-23 11:48:15 -04002401 clnt->cl_program->name,
Trond Myklebustfb43d172016-01-30 16:39:26 -05002402 task->tk_xprt->servername);
Trond Myklebust4e0038b2012-03-01 17:01:05 -05002403 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 task->tk_flags &= ~RPC_CALL_MAJORSEEN;
2405 }
2406
Trond Myklebust43ac3f22006-03-20 13:44:51 -05002407 /*
2408 * Ensure that we see all writes made by xprt_complete_rqst()
Ricardo Labiagadd2b63d2009-04-01 09:23:28 -04002409 * before it changed req->rq_reply_bytes_recvd.
Trond Myklebust43ac3f22006-03-20 13:44:51 -05002410 */
2411 smp_rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 req->rq_rcv_buf.len = req->rq_private_buf.len;
2413
2414 /* Check that the softirq receive buffer is valid */
2415 WARN_ON(memcmp(&req->rq_rcv_buf, &req->rq_private_buf,
2416 sizeof(req->rq_rcv_buf)) != 0);
2417
Chuck Levera0584ee2019-02-11 11:24:58 -05002418 xdr_init_decode(&xdr, &req->rq_rcv_buf,
2419 req->rq_rcv_buf.head[0].iov_base, req);
2420 switch (rpc_decode_header(task, &xdr)) {
2421 case 0:
2422 task->tk_action = rpc_exit_task;
2423 task->tk_status = rpcauth_unwrap_resp(task, &xdr);
2424 dprintk("RPC: %5u %s result %d\n",
2425 task->tk_pid, __func__, task->tk_status);
Trond Myklebustabbcf282006-01-03 09:55:03 +01002426 return;
Chuck Levera0584ee2019-02-11 11:24:58 -05002427 case -EAGAIN:
Chuck Levera0584ee2019-02-11 11:24:58 -05002428 task->tk_status = 0;
Trond Myklebust7987b692019-05-29 12:49:52 -04002429 xdr_free_bvec(&req->rq_rcv_buf);
2430 req->rq_reply_bytes_recvd = 0;
2431 req->rq_rcv_buf.len = 0;
2432 if (task->tk_client->cl_discrtry)
2433 xprt_conditional_disconnect(req->rq_xprt,
2434 req->rq_connect_cookie);
Trond Myklebustcea57782019-03-09 16:06:47 -05002435 task->tk_action = call_encode;
2436 rpc_check_timeout(task);
Trond Myklebust7987b692019-05-29 12:49:52 -04002437 break;
2438 case -EKEYREJECTED:
2439 task->tk_action = call_reserve;
2440 rpc_check_timeout(task);
2441 rpcauth_invalcred(task);
2442 /* Ensure we obtain a new XID if we retry! */
2443 xprt_release(task);
Trond Myklebust24b74bf2008-04-19 13:15:47 -04002444 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445}
2446
Chuck Levere8680a22019-02-11 11:24:48 -05002447static int
2448rpc_encode_header(struct rpc_task *task, struct xdr_stream *xdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449{
2450 struct rpc_clnt *clnt = task->tk_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 struct rpc_rqst *req = task->tk_rqstp;
Chuck Levere8680a22019-02-11 11:24:48 -05002452 __be32 *p;
2453 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454
Chuck Levere8680a22019-02-11 11:24:48 -05002455 error = -EMSGSIZE;
2456 p = xdr_reserve_space(xdr, RPC_CALLHDRSIZE << 2);
2457 if (!p)
2458 goto out_fail;
2459 *p++ = req->rq_xid;
2460 *p++ = rpc_call;
2461 *p++ = cpu_to_be32(RPC_VERSION);
2462 *p++ = cpu_to_be32(clnt->cl_prog);
2463 *p++ = cpu_to_be32(clnt->cl_vers);
2464 *p = cpu_to_be32(task->tk_msg.rpc_proc->p_proc);
Chuck Lever808012f2005-08-25 16:25:49 -07002465
Chuck Levere8680a22019-02-11 11:24:48 -05002466 error = rpcauth_marshcred(task, xdr);
2467 if (error < 0)
2468 goto out_fail;
2469 return 0;
2470out_fail:
2471 trace_rpc_bad_callhdr(task);
2472 rpc_exit(task, error);
2473 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474}
2475
Chuck Levera0584ee2019-02-11 11:24:58 -05002476static noinline int
2477rpc_decode_header(struct rpc_task *task, struct xdr_stream *xdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478{
Trond Myklebust4e0038b2012-03-01 17:01:05 -05002479 struct rpc_clnt *clnt = task->tk_client;
Trond Myklebusteb90a162019-03-15 09:29:00 -04002480 int error;
Chuck Levera0584ee2019-02-11 11:24:58 -05002481 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482
Chuck Lever7f5667a52019-02-11 11:24:53 -05002483 /* RFC-1014 says that the representation of XDR data must be a
2484 * multiple of four bytes
2485 * - if it isn't pointer subtraction in the NFS client may give
2486 * undefined results
2487 */
2488 if (task->tk_rqstp->rq_rcv_buf.len & 3)
Trond Myklebusteb90a162019-03-15 09:29:00 -04002489 goto out_unparsable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490
Chuck Levera0584ee2019-02-11 11:24:58 -05002491 p = xdr_inline_decode(xdr, 3 * sizeof(*p));
2492 if (!p)
2493 goto out_unparsable;
Chuck Lever7f5667a52019-02-11 11:24:53 -05002494 p++; /* skip XID */
2495 if (*p++ != rpc_reply)
2496 goto out_unparsable;
2497 if (*p++ != rpc_msg_accepted)
2498 goto out_msg_denied;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04002499
Chuck Levera0584ee2019-02-11 11:24:58 -05002500 error = rpcauth_checkverf(task, xdr);
2501 if (error)
Chuck Lever7f5667a52019-02-11 11:24:53 -05002502 goto out_verifier;
2503
Chuck Levera0584ee2019-02-11 11:24:58 -05002504 p = xdr_inline_decode(xdr, sizeof(*p));
2505 if (!p)
Chuck Lever7f5667a52019-02-11 11:24:53 -05002506 goto out_unparsable;
Chuck Levera0584ee2019-02-11 11:24:58 -05002507 switch (*p) {
Chuck Lever7f5667a52019-02-11 11:24:53 -05002508 case rpc_success:
Chuck Levera0584ee2019-02-11 11:24:58 -05002509 return 0;
Chuck Lever7f5667a52019-02-11 11:24:53 -05002510 case rpc_prog_unavail:
2511 trace_rpc__prog_unavail(task);
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00002512 error = -EPFNOSUPPORT;
2513 goto out_err;
Chuck Lever7f5667a52019-02-11 11:24:53 -05002514 case rpc_prog_mismatch:
2515 trace_rpc__prog_mismatch(task);
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00002516 error = -EPROTONOSUPPORT;
2517 goto out_err;
Chuck Lever7f5667a52019-02-11 11:24:53 -05002518 case rpc_proc_unavail:
2519 trace_rpc__proc_unavail(task);
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00002520 error = -EOPNOTSUPP;
2521 goto out_err;
Chuck Lever7f5667a52019-02-11 11:24:53 -05002522 case rpc_garbage_args:
Trond Myklebust928d42f2019-03-15 10:12:30 -04002523 case rpc_system_err:
Chuck Lever7f5667a52019-02-11 11:24:53 -05002524 trace_rpc__garbage_args(task);
Trond Myklebusteb90a162019-03-15 09:29:00 -04002525 error = -EIO;
Chuck Lever7f5667a52019-02-11 11:24:53 -05002526 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 default:
Trond Myklebusteb90a162019-03-15 09:29:00 -04002528 goto out_unparsable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 }
2530
Trond Myklebustabbcf282006-01-03 09:55:03 +01002531out_garbage:
Trond Myklebust4e0038b2012-03-01 17:01:05 -05002532 clnt->cl_stats->rpcgarbage++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 if (task->tk_garb_retry) {
2534 task->tk_garb_retry--;
Trond Myklebust762e4e62018-08-24 16:28:28 -04002535 task->tk_action = call_encode;
Chuck Levera0584ee2019-02-11 11:24:58 -05002536 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538out_err:
2539 rpc_exit(task, error);
Chuck Levera0584ee2019-02-11 11:24:58 -05002540 return error;
Chuck Lever7f5667a52019-02-11 11:24:53 -05002541
Chuck Lever7f5667a52019-02-11 11:24:53 -05002542out_unparsable:
2543 trace_rpc__unparsable(task);
2544 error = -EIO;
Trond Myklebustabbcf282006-01-03 09:55:03 +01002545 goto out_garbage;
Chuck Lever7f5667a52019-02-11 11:24:53 -05002546
2547out_verifier:
2548 trace_rpc_bad_verifier(task);
Chuck Lever7f5667a52019-02-11 11:24:53 -05002549 goto out_garbage;
2550
2551out_msg_denied:
Trond Myklebusteb90a162019-03-15 09:29:00 -04002552 error = -EACCES;
Chuck Levera0584ee2019-02-11 11:24:58 -05002553 p = xdr_inline_decode(xdr, sizeof(*p));
2554 if (!p)
2555 goto out_unparsable;
Chuck Lever7f5667a52019-02-11 11:24:53 -05002556 switch (*p++) {
2557 case rpc_auth_error:
2558 break;
2559 case rpc_mismatch:
2560 trace_rpc__mismatch(task);
2561 error = -EPROTONOSUPPORT;
2562 goto out_err;
2563 default:
Trond Myklebusteb90a162019-03-15 09:29:00 -04002564 goto out_unparsable;
Chuck Lever7f5667a52019-02-11 11:24:53 -05002565 }
2566
Chuck Levera0584ee2019-02-11 11:24:58 -05002567 p = xdr_inline_decode(xdr, sizeof(*p));
2568 if (!p)
2569 goto out_unparsable;
Chuck Lever7f5667a52019-02-11 11:24:53 -05002570 switch (*p++) {
2571 case rpc_autherr_rejectedcred:
2572 case rpc_autherr_rejectedverf:
2573 case rpcsec_gsserr_credproblem:
2574 case rpcsec_gsserr_ctxproblem:
2575 if (!task->tk_cred_retry)
2576 break;
2577 task->tk_cred_retry--;
2578 trace_rpc__stale_creds(task);
Trond Myklebust7987b692019-05-29 12:49:52 -04002579 return -EKEYREJECTED;
Chuck Lever7f5667a52019-02-11 11:24:53 -05002580 case rpc_autherr_badcred:
2581 case rpc_autherr_badverf:
2582 /* possibly garbled cred/verf? */
2583 if (!task->tk_garb_retry)
2584 break;
2585 task->tk_garb_retry--;
2586 trace_rpc__bad_creds(task);
2587 task->tk_action = call_encode;
Chuck Levera0584ee2019-02-11 11:24:58 -05002588 return -EAGAIN;
Chuck Lever7f5667a52019-02-11 11:24:53 -05002589 case rpc_autherr_tooweak:
2590 trace_rpc__auth_tooweak(task);
2591 pr_warn("RPC: server %s requires stronger authentication.\n",
2592 task->tk_xprt->servername);
2593 break;
2594 default:
Trond Myklebusteb90a162019-03-15 09:29:00 -04002595 goto out_unparsable;
Chuck Lever7f5667a52019-02-11 11:24:53 -05002596 }
2597 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598}
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00002599
Christoph Hellwigc512f362017-05-08 09:31:19 +02002600static void rpcproc_encode_null(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
2601 const void *obj)
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00002602{
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00002603}
2604
Christoph Hellwig73c8dc12017-05-08 14:58:11 +02002605static int rpcproc_decode_null(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
2606 void *obj)
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00002607{
2608 return 0;
2609}
2610
Christoph Hellwig499b4982017-05-12 15:36:49 +02002611static const struct rpc_procinfo rpcproc_null = {
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00002612 .p_encode = rpcproc_encode_null,
2613 .p_decode = rpcproc_decode_null,
2614};
2615
Chuck Levercaabea82009-12-03 15:58:56 -05002616static int rpc_ping(struct rpc_clnt *clnt)
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00002617{
2618 struct rpc_message msg = {
2619 .rpc_proc = &rpcproc_null,
2620 };
2621 int err;
NeilBrowna68a72e2018-12-03 11:30:30 +11002622 err = rpc_call_sync(clnt, &msg, RPC_TASK_SOFT | RPC_TASK_SOFTCONN |
2623 RPC_TASK_NULLCREDS);
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00002624 return err;
2625}
Trond Myklebust188fef12007-06-16 14:18:40 -04002626
Trond Myklebust7f554892016-01-30 23:43:35 -05002627static
2628struct rpc_task *rpc_call_null_helper(struct rpc_clnt *clnt,
2629 struct rpc_xprt *xprt, struct rpc_cred *cred, int flags,
2630 const struct rpc_call_ops *ops, void *data)
Trond Myklebust5e1550d2007-06-23 10:17:16 -04002631{
2632 struct rpc_message msg = {
2633 .rpc_proc = &rpcproc_null,
Trond Myklebust5e1550d2007-06-23 10:17:16 -04002634 };
Trond Myklebust84115e12007-07-14 15:39:59 -04002635 struct rpc_task_setup task_setup_data = {
2636 .rpc_client = clnt,
Trond Myklebust7f554892016-01-30 23:43:35 -05002637 .rpc_xprt = xprt,
Trond Myklebust84115e12007-07-14 15:39:59 -04002638 .rpc_message = &msg,
NeilBrown1de7eea2018-12-03 11:30:30 +11002639 .rpc_op_cred = cred,
Trond Myklebust7f554892016-01-30 23:43:35 -05002640 .callback_ops = (ops != NULL) ? ops : &rpc_default_ops,
2641 .callback_data = data,
NeilBrowna52458b2018-12-03 11:30:31 +11002642 .flags = flags | RPC_TASK_NULLCREDS,
Trond Myklebust84115e12007-07-14 15:39:59 -04002643 };
Trond Myklebust7f554892016-01-30 23:43:35 -05002644
Trond Myklebustc970aa82007-07-14 15:39:59 -04002645 return rpc_run_task(&task_setup_data);
Trond Myklebust5e1550d2007-06-23 10:17:16 -04002646}
Trond Myklebust7f554892016-01-30 23:43:35 -05002647
2648struct rpc_task *rpc_call_null(struct rpc_clnt *clnt, struct rpc_cred *cred, int flags)
2649{
2650 return rpc_call_null_helper(clnt, NULL, cred, flags, NULL, NULL);
2651}
Trond Myklebuste8914c62007-07-14 15:39:59 -04002652EXPORT_SYMBOL_GPL(rpc_call_null);
Trond Myklebust5e1550d2007-06-23 10:17:16 -04002653
Trond Myklebust7f554892016-01-30 23:43:35 -05002654struct rpc_cb_add_xprt_calldata {
2655 struct rpc_xprt_switch *xps;
2656 struct rpc_xprt *xprt;
2657};
2658
2659static void rpc_cb_add_xprt_done(struct rpc_task *task, void *calldata)
2660{
2661 struct rpc_cb_add_xprt_calldata *data = calldata;
2662
2663 if (task->tk_status == 0)
2664 rpc_xprt_switch_add_xprt(data->xps, data->xprt);
2665}
2666
2667static void rpc_cb_add_xprt_release(void *calldata)
2668{
2669 struct rpc_cb_add_xprt_calldata *data = calldata;
2670
2671 xprt_put(data->xprt);
2672 xprt_switch_put(data->xps);
2673 kfree(data);
2674}
2675
Trond Myklebustce272302016-07-24 17:06:28 -04002676static const struct rpc_call_ops rpc_cb_add_xprt_call_ops = {
Trond Myklebust7f554892016-01-30 23:43:35 -05002677 .rpc_call_done = rpc_cb_add_xprt_done,
2678 .rpc_release = rpc_cb_add_xprt_release,
2679};
2680
2681/**
2682 * rpc_clnt_test_and_add_xprt - Test and add a new transport to a rpc_clnt
2683 * @clnt: pointer to struct rpc_clnt
2684 * @xps: pointer to struct rpc_xprt_switch,
2685 * @xprt: pointer struct rpc_xprt
2686 * @dummy: unused
2687 */
2688int rpc_clnt_test_and_add_xprt(struct rpc_clnt *clnt,
2689 struct rpc_xprt_switch *xps, struct rpc_xprt *xprt,
2690 void *dummy)
2691{
2692 struct rpc_cb_add_xprt_calldata *data;
Trond Myklebust7f554892016-01-30 23:43:35 -05002693 struct rpc_task *task;
2694
2695 data = kmalloc(sizeof(*data), GFP_NOFS);
2696 if (!data)
2697 return -ENOMEM;
2698 data->xps = xprt_switch_get(xps);
2699 data->xprt = xprt_get(xprt);
2700
NeilBrowna68a72e2018-12-03 11:30:30 +11002701 task = rpc_call_null_helper(clnt, xprt, NULL,
2702 RPC_TASK_SOFT|RPC_TASK_SOFTCONN|RPC_TASK_ASYNC|RPC_TASK_NULLCREDS,
Trond Myklebust7f554892016-01-30 23:43:35 -05002703 &rpc_cb_add_xprt_call_ops, data);
Trond Myklebust7f554892016-01-30 23:43:35 -05002704 if (IS_ERR(task))
2705 return PTR_ERR(task);
2706 rpc_put_task(task);
2707 return 1;
2708}
2709EXPORT_SYMBOL_GPL(rpc_clnt_test_and_add_xprt);
2710
2711/**
Andy Adamsonfda0ab42016-09-09 09:22:26 -04002712 * rpc_clnt_setup_test_and_add_xprt()
2713 *
2714 * This is an rpc_clnt_add_xprt setup() function which returns 1 so:
2715 * 1) caller of the test function must dereference the rpc_xprt_switch
2716 * and the rpc_xprt.
2717 * 2) test function must call rpc_xprt_switch_add_xprt, usually in
2718 * the rpc_call_done routine.
2719 *
2720 * Upon success (return of 1), the test function adds the new
2721 * transport to the rpc_clnt xprt switch
2722 *
2723 * @clnt: struct rpc_clnt to get the new transport
2724 * @xps: the rpc_xprt_switch to hold the new transport
2725 * @xprt: the rpc_xprt to test
2726 * @data: a struct rpc_add_xprt_test pointer that holds the test function
2727 * and test function call data
2728 */
2729int rpc_clnt_setup_test_and_add_xprt(struct rpc_clnt *clnt,
2730 struct rpc_xprt_switch *xps,
2731 struct rpc_xprt *xprt,
2732 void *data)
2733{
Andy Adamsonfda0ab42016-09-09 09:22:26 -04002734 struct rpc_task *task;
2735 struct rpc_add_xprt_test *xtest = (struct rpc_add_xprt_test *)data;
2736 int status = -EADDRINUSE;
2737
2738 xprt = xprt_get(xprt);
2739 xprt_switch_get(xps);
2740
2741 if (rpc_xprt_switch_has_addr(xps, (struct sockaddr *)&xprt->addr))
2742 goto out_err;
2743
2744 /* Test the connection */
NeilBrowna68a72e2018-12-03 11:30:30 +11002745 task = rpc_call_null_helper(clnt, xprt, NULL,
2746 RPC_TASK_SOFT | RPC_TASK_SOFTCONN | RPC_TASK_NULLCREDS,
Andy Adamsonfda0ab42016-09-09 09:22:26 -04002747 NULL, NULL);
Andy Adamsonfda0ab42016-09-09 09:22:26 -04002748 if (IS_ERR(task)) {
2749 status = PTR_ERR(task);
2750 goto out_err;
2751 }
2752 status = task->tk_status;
2753 rpc_put_task(task);
2754
2755 if (status < 0)
2756 goto out_err;
2757
2758 /* rpc_xprt_switch and rpc_xprt are deferrenced by add_xprt_test() */
2759 xtest->add_xprt_test(clnt, xprt, xtest->data);
2760
Santosh kumar pradhan10e037d2018-12-19 12:29:57 +05302761 xprt_put(xprt);
2762 xprt_switch_put(xps);
2763
Andy Adamsonfda0ab42016-09-09 09:22:26 -04002764 /* so that rpc_clnt_add_xprt does not call rpc_xprt_switch_add_xprt */
2765 return 1;
2766out_err:
2767 xprt_put(xprt);
2768 xprt_switch_put(xps);
2769 pr_info("RPC: rpc_clnt_test_xprt failed: %d addr %s not added\n",
2770 status, xprt->address_strings[RPC_DISPLAY_ADDR]);
2771 return status;
2772}
2773EXPORT_SYMBOL_GPL(rpc_clnt_setup_test_and_add_xprt);
2774
2775/**
Trond Myklebust7f554892016-01-30 23:43:35 -05002776 * rpc_clnt_add_xprt - Add a new transport to a rpc_clnt
2777 * @clnt: pointer to struct rpc_clnt
2778 * @xprtargs: pointer to struct xprt_create
2779 * @setup: callback to test and/or set up the connection
2780 * @data: pointer to setup function data
2781 *
2782 * Creates a new transport using the parameters set in args and
2783 * adds it to clnt.
2784 * If ping is set, then test that connectivity succeeds before
2785 * adding the new transport.
2786 *
2787 */
2788int rpc_clnt_add_xprt(struct rpc_clnt *clnt,
2789 struct xprt_create *xprtargs,
2790 int (*setup)(struct rpc_clnt *,
2791 struct rpc_xprt_switch *,
2792 struct rpc_xprt *,
2793 void *),
2794 void *data)
2795{
2796 struct rpc_xprt_switch *xps;
2797 struct rpc_xprt *xprt;
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002798 unsigned long connect_timeout;
Trond Myklebust3851f1c2016-08-04 00:08:45 -04002799 unsigned long reconnect_timeout;
Trond Myklebust7f554892016-01-30 23:43:35 -05002800 unsigned char resvport;
2801 int ret = 0;
2802
2803 rcu_read_lock();
2804 xps = xprt_switch_get(rcu_dereference(clnt->cl_xpi.xpi_xpswitch));
2805 xprt = xprt_iter_xprt(&clnt->cl_xpi);
2806 if (xps == NULL || xprt == NULL) {
2807 rcu_read_unlock();
2808 return -EAGAIN;
2809 }
2810 resvport = xprt->resvport;
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002811 connect_timeout = xprt->connect_timeout;
Trond Myklebust3851f1c2016-08-04 00:08:45 -04002812 reconnect_timeout = xprt->max_reconnect_timeout;
Trond Myklebust7f554892016-01-30 23:43:35 -05002813 rcu_read_unlock();
2814
2815 xprt = xprt_create_transport(xprtargs);
2816 if (IS_ERR(xprt)) {
2817 ret = PTR_ERR(xprt);
2818 goto out_put_switch;
2819 }
2820 xprt->resvport = resvport;
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002821 if (xprt->ops->set_connect_timeout != NULL)
2822 xprt->ops->set_connect_timeout(xprt,
2823 connect_timeout,
2824 reconnect_timeout);
Trond Myklebust7f554892016-01-30 23:43:35 -05002825
2826 rpc_xprt_switch_set_roundrobin(xps);
2827 if (setup) {
2828 ret = setup(clnt, xps, xprt, data);
2829 if (ret != 0)
2830 goto out_put_xprt;
2831 }
2832 rpc_xprt_switch_add_xprt(xps, xprt);
2833out_put_xprt:
2834 xprt_put(xprt);
2835out_put_switch:
2836 xprt_switch_put(xps);
2837 return ret;
2838}
2839EXPORT_SYMBOL_GPL(rpc_clnt_add_xprt);
2840
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002841struct connect_timeout_data {
2842 unsigned long connect_timeout;
2843 unsigned long reconnect_timeout;
2844};
2845
Trond Myklebust8d480322016-08-05 19:03:31 -04002846static int
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002847rpc_xprt_set_connect_timeout(struct rpc_clnt *clnt,
Trond Myklebust8d480322016-08-05 19:03:31 -04002848 struct rpc_xprt *xprt,
2849 void *data)
2850{
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002851 struct connect_timeout_data *timeo = data;
Trond Myklebust8d480322016-08-05 19:03:31 -04002852
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002853 if (xprt->ops->set_connect_timeout)
2854 xprt->ops->set_connect_timeout(xprt,
2855 timeo->connect_timeout,
2856 timeo->reconnect_timeout);
Trond Myklebust8d480322016-08-05 19:03:31 -04002857 return 0;
2858}
2859
2860void
Trond Myklebust26ae1022017-02-08 11:17:55 -05002861rpc_set_connect_timeout(struct rpc_clnt *clnt,
2862 unsigned long connect_timeout,
2863 unsigned long reconnect_timeout)
Trond Myklebust8d480322016-08-05 19:03:31 -04002864{
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002865 struct connect_timeout_data timeout = {
Trond Myklebust26ae1022017-02-08 11:17:55 -05002866 .connect_timeout = connect_timeout,
2867 .reconnect_timeout = reconnect_timeout,
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002868 };
Trond Myklebust8d480322016-08-05 19:03:31 -04002869 rpc_clnt_iterate_for_each_xprt(clnt,
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002870 rpc_xprt_set_connect_timeout,
2871 &timeout);
Trond Myklebust8d480322016-08-05 19:03:31 -04002872}
Trond Myklebust26ae1022017-02-08 11:17:55 -05002873EXPORT_SYMBOL_GPL(rpc_set_connect_timeout);
Trond Myklebust8d480322016-08-05 19:03:31 -04002874
Andy Adamson3b58a8a2016-09-09 09:22:23 -04002875void rpc_clnt_xprt_switch_put(struct rpc_clnt *clnt)
2876{
Anna Schumakerbb29dd82016-10-26 10:33:31 -04002877 rcu_read_lock();
Andy Adamson3b58a8a2016-09-09 09:22:23 -04002878 xprt_switch_put(rcu_dereference(clnt->cl_xpi.xpi_xpswitch));
Anna Schumakerbb29dd82016-10-26 10:33:31 -04002879 rcu_read_unlock();
Andy Adamson3b58a8a2016-09-09 09:22:23 -04002880}
2881EXPORT_SYMBOL_GPL(rpc_clnt_xprt_switch_put);
2882
Andy Adamsondd691712016-09-09 09:22:24 -04002883void rpc_clnt_xprt_switch_add_xprt(struct rpc_clnt *clnt, struct rpc_xprt *xprt)
2884{
Anna Schumakerbb29dd82016-10-26 10:33:31 -04002885 rcu_read_lock();
Andy Adamsondd691712016-09-09 09:22:24 -04002886 rpc_xprt_switch_add_xprt(rcu_dereference(clnt->cl_xpi.xpi_xpswitch),
2887 xprt);
Anna Schumakerbb29dd82016-10-26 10:33:31 -04002888 rcu_read_unlock();
Andy Adamsondd691712016-09-09 09:22:24 -04002889}
2890EXPORT_SYMBOL_GPL(rpc_clnt_xprt_switch_add_xprt);
2891
Andy Adamson39e5d2d2016-09-09 09:22:25 -04002892bool rpc_clnt_xprt_switch_has_addr(struct rpc_clnt *clnt,
2893 const struct sockaddr *sap)
2894{
2895 struct rpc_xprt_switch *xps;
2896 bool ret;
2897
Andy Adamson39e5d2d2016-09-09 09:22:25 -04002898 rcu_read_lock();
Anna Schumakerbb29dd82016-10-26 10:33:31 -04002899 xps = rcu_dereference(clnt->cl_xpi.xpi_xpswitch);
Andy Adamson39e5d2d2016-09-09 09:22:25 -04002900 ret = rpc_xprt_switch_has_addr(xps, sap);
2901 rcu_read_unlock();
2902 return ret;
2903}
2904EXPORT_SYMBOL_GPL(rpc_clnt_xprt_switch_has_addr);
2905
Jeff Laytonf895b252014-11-17 16:58:04 -05002906#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
Chuck Lever68a23ee2008-05-21 17:09:26 -04002907static void rpc_show_header(void)
2908{
Chuck Levercb3997b2008-05-21 17:09:41 -04002909 printk(KERN_INFO "-pid- flgs status -client- --rqstp- "
2910 "-timeout ---ops--\n");
Chuck Lever68a23ee2008-05-21 17:09:26 -04002911}
2912
Chuck Lever38e886e2008-05-21 17:09:33 -04002913static void rpc_show_task(const struct rpc_clnt *clnt,
2914 const struct rpc_task *task)
2915{
2916 const char *rpc_waitq = "none";
Chuck Lever38e886e2008-05-21 17:09:33 -04002917
2918 if (RPC_IS_QUEUED(task))
2919 rpc_waitq = rpc_qname(task->tk_waitqueue);
2920
Joe Perchesb3bceda2010-12-21 10:52:24 -05002921 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 -04002922 task->tk_pid, task->tk_flags, task->tk_status,
Trond Myklebust5efd1872019-04-07 13:58:50 -04002923 clnt, task->tk_rqstp, rpc_task_timeout(task), task->tk_ops,
Trond Myklebust55909f22013-08-23 11:48:15 -04002924 clnt->cl_program->name, clnt->cl_vers, rpc_proc_name(task),
Joe Perchesb3bceda2010-12-21 10:52:24 -05002925 task->tk_action, rpc_waitq);
Chuck Lever38e886e2008-05-21 17:09:33 -04002926}
2927
Stanislav Kinsbursky70abc492012-01-12 22:07:51 +04002928void rpc_show_tasks(struct net *net)
Trond Myklebust188fef12007-06-16 14:18:40 -04002929{
2930 struct rpc_clnt *clnt;
Chuck Lever38e886e2008-05-21 17:09:33 -04002931 struct rpc_task *task;
Chuck Lever68a23ee2008-05-21 17:09:26 -04002932 int header = 0;
Stanislav Kinsbursky70abc492012-01-12 22:07:51 +04002933 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
Trond Myklebust188fef12007-06-16 14:18:40 -04002934
Stanislav Kinsbursky70abc492012-01-12 22:07:51 +04002935 spin_lock(&sn->rpc_client_lock);
2936 list_for_each_entry(clnt, &sn->all_clients, cl_clients) {
Trond Myklebust188fef12007-06-16 14:18:40 -04002937 spin_lock(&clnt->cl_lock);
Chuck Lever38e886e2008-05-21 17:09:33 -04002938 list_for_each_entry(task, &clnt->cl_tasks, tk_task) {
Chuck Lever68a23ee2008-05-21 17:09:26 -04002939 if (!header) {
2940 rpc_show_header();
2941 header++;
2942 }
Chuck Lever38e886e2008-05-21 17:09:33 -04002943 rpc_show_task(clnt, task);
Trond Myklebust188fef12007-06-16 14:18:40 -04002944 }
2945 spin_unlock(&clnt->cl_lock);
2946 }
Stanislav Kinsbursky70abc492012-01-12 22:07:51 +04002947 spin_unlock(&sn->rpc_client_lock);
Trond Myklebust188fef12007-06-16 14:18:40 -04002948}
2949#endif
Jeff Layton3c87ef62015-06-03 16:14:25 -04002950
2951#if IS_ENABLED(CONFIG_SUNRPC_SWAP)
Trond Myklebust15001e52016-01-30 20:05:34 -05002952static int
2953rpc_clnt_swap_activate_callback(struct rpc_clnt *clnt,
2954 struct rpc_xprt *xprt,
2955 void *dummy)
2956{
2957 return xprt_enable_swap(xprt);
2958}
2959
Jeff Layton3c87ef62015-06-03 16:14:25 -04002960int
2961rpc_clnt_swap_activate(struct rpc_clnt *clnt)
2962{
Trond Myklebust15001e52016-01-30 20:05:34 -05002963 if (atomic_inc_return(&clnt->cl_swapper) == 1)
2964 return rpc_clnt_iterate_for_each_xprt(clnt,
2965 rpc_clnt_swap_activate_callback, NULL);
2966 return 0;
Jeff Layton3c87ef62015-06-03 16:14:25 -04002967}
2968EXPORT_SYMBOL_GPL(rpc_clnt_swap_activate);
2969
Trond Myklebust15001e52016-01-30 20:05:34 -05002970static int
2971rpc_clnt_swap_deactivate_callback(struct rpc_clnt *clnt,
2972 struct rpc_xprt *xprt,
2973 void *dummy)
2974{
2975 xprt_disable_swap(xprt);
2976 return 0;
2977}
2978
Jeff Layton3c87ef62015-06-03 16:14:25 -04002979void
2980rpc_clnt_swap_deactivate(struct rpc_clnt *clnt)
2981{
Trond Myklebust15001e52016-01-30 20:05:34 -05002982 if (atomic_dec_if_positive(&clnt->cl_swapper) == 0)
2983 rpc_clnt_iterate_for_each_xprt(clnt,
2984 rpc_clnt_swap_deactivate_callback, NULL);
Jeff Layton3c87ef62015-06-03 16:14:25 -04002985}
2986EXPORT_SYMBOL_GPL(rpc_clnt_swap_deactivate);
2987#endif /* CONFIG_SUNRPC_SWAP */