blob: a312ea2bc4405917e6e9ec6b41974a32e931f729 [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"
Olga Kornievskaiac5a382e2021-06-08 15:59:12 -040044#include "sysfs.h"
Stanislav Kinsbursky70abc492012-01-12 22:07:51 +040045#include "netns.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Jeff Laytonf895b252014-11-17 16:58:04 -050047#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048# define RPCDBG_FACILITY RPCDBG_CALL
49#endif
50
Trond Myklebust188fef12007-06-16 14:18:40 -040051/*
52 * All RPC clients are linked into this list
53 */
Trond Myklebust188fef12007-06-16 14:18:40 -040054
Linus Torvalds1da177e2005-04-16 15:20:36 -070055static DECLARE_WAIT_QUEUE_HEAD(destroy_wait);
56
57
58static void call_start(struct rpc_task *task);
59static void call_reserve(struct rpc_task *task);
60static void call_reserveresult(struct rpc_task *task);
61static void call_allocate(struct rpc_task *task);
Trond Myklebust762e4e62018-08-24 16:28:28 -040062static void call_encode(struct rpc_task *task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063static void call_decode(struct rpc_task *task);
64static void call_bind(struct rpc_task *task);
Chuck Leverda351872005-08-11 16:25:11 -040065static void call_bind_status(struct rpc_task *task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066static void call_transmit(struct rpc_task *task);
67static void call_status(struct rpc_task *task);
Trond Myklebust940e3312005-11-09 21:45:24 -050068static void call_transmit_status(struct rpc_task *task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069static void call_refresh(struct rpc_task *task);
70static void call_refreshresult(struct rpc_task *task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071static void call_connect(struct rpc_task *task);
72static void call_connect_status(struct rpc_task *task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Chuck Levere8680a22019-02-11 11:24:48 -050074static int rpc_encode_header(struct rpc_task *task,
75 struct xdr_stream *xdr);
Chuck Levera0584ee2019-02-11 11:24:58 -050076static int rpc_decode_header(struct rpc_task *task,
77 struct xdr_stream *xdr);
Chuck Levercaabea82009-12-03 15:58:56 -050078static int rpc_ping(struct rpc_clnt *clnt);
Trond Myklebust7b3fef82019-03-07 14:10:32 -050079static void rpc_check_timeout(struct rpc_task *task);
Trond Myklebust64c91a12007-06-23 10:17:16 -040080
Trond Myklebust188fef12007-06-16 14:18:40 -040081static void rpc_register_client(struct rpc_clnt *clnt)
82{
Trond Myklebust2446ab62012-03-01 17:00:56 -050083 struct net *net = rpc_net_ns(clnt);
84 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
Stanislav Kinsbursky70abc492012-01-12 22:07:51 +040085
86 spin_lock(&sn->rpc_client_lock);
87 list_add(&clnt->cl_clients, &sn->all_clients);
88 spin_unlock(&sn->rpc_client_lock);
Trond Myklebust188fef12007-06-16 14:18:40 -040089}
90
91static void rpc_unregister_client(struct rpc_clnt *clnt)
92{
Trond Myklebust2446ab62012-03-01 17:00:56 -050093 struct net *net = rpc_net_ns(clnt);
94 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
Stanislav Kinsbursky70abc492012-01-12 22:07:51 +040095
96 spin_lock(&sn->rpc_client_lock);
Trond Myklebust188fef12007-06-16 14:18:40 -040097 list_del(&clnt->cl_clients);
Stanislav Kinsbursky70abc492012-01-12 22:07:51 +040098 spin_unlock(&sn->rpc_client_lock);
Trond Myklebust188fef12007-06-16 14:18:40 -040099}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400101static void __rpc_clnt_remove_pipedir(struct rpc_clnt *clnt)
102{
Trond Myklebustc36dcfe12013-08-26 17:44:26 -0400103 rpc_remove_client_dir(clnt);
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400104}
105
106static void rpc_clnt_remove_pipedir(struct rpc_clnt *clnt)
107{
Trond Myklebust2446ab62012-03-01 17:00:56 -0500108 struct net *net = rpc_net_ns(clnt);
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400109 struct super_block *pipefs_sb;
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400110
Trond Myklebust2446ab62012-03-01 17:00:56 -0500111 pipefs_sb = rpc_get_sb_net(net);
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400112 if (pipefs_sb) {
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400113 __rpc_clnt_remove_pipedir(clnt);
Trond Myklebust2446ab62012-03-01 17:00:56 -0500114 rpc_put_sb_net(net);
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400115 }
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400116}
117
118static struct dentry *rpc_setup_pipedir_sb(struct super_block *sb,
Trond Myklebust41b6b4d2013-08-23 13:03:50 -0400119 struct rpc_clnt *clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120{
Trond Myklebustf1345852005-09-23 11:08:25 -0400121 static uint32_t clntid;
Trond Myklebust41b6b4d2013-08-23 13:03:50 -0400122 const char *dir_name = clnt->cl_program->pipe_dir_name;
Trond Myklebust23ac6582009-08-09 15:14:25 -0400123 char name[15];
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400124 struct dentry *dir, *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400126 dir = rpc_d_lookup_sb(sb, dir_name);
Weston Andros Adamson922eeac2012-10-23 10:43:25 -0400127 if (dir == NULL) {
128 pr_info("RPC: pipefs directory doesn't exist: %s\n", dir_name);
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400129 return dir;
Weston Andros Adamson922eeac2012-10-23 10:43:25 -0400130 }
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400131 for (;;) {
Al Viroa95e6912013-07-14 16:43:54 +0400132 snprintf(name, sizeof(name), "clnt%x", (unsigned int)clntid++);
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400133 name[sizeof(name) - 1] = '\0';
Al Viroa95e6912013-07-14 16:43:54 +0400134 dentry = rpc_create_client_dir(dir, name, clnt);
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400135 if (!IS_ERR(dentry))
136 break;
Al Viroa95e6912013-07-14 16:43:54 +0400137 if (dentry == ERR_PTR(-EEXIST))
138 continue;
139 printk(KERN_INFO "RPC: Couldn't create pipefs entry"
140 " %s/%s, error %ld\n",
141 dir_name, name, PTR_ERR(dentry));
142 break;
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400143 }
144 dput(dir);
145 return dentry;
146}
147
148static int
Trond Myklebust41b6b4d2013-08-23 13:03:50 -0400149rpc_setup_pipedir(struct super_block *pipefs_sb, struct rpc_clnt *clnt)
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400150{
Stanislav Kinsbursky30507f52012-01-11 19:18:42 +0400151 struct dentry *dentry;
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400152
Trond Myklebustc36dcfe12013-08-26 17:44:26 -0400153 if (clnt->cl_program->pipe_dir_name != NULL) {
154 dentry = rpc_setup_pipedir_sb(pipefs_sb, clnt);
155 if (IS_ERR(dentry))
156 return PTR_ERR(dentry);
157 }
Trond Myklebust23ac6582009-08-09 15:14:25 -0400158 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159}
160
Trond Myklebust41b6b4d2013-08-23 13:03:50 -0400161static int rpc_clnt_skip_event(struct rpc_clnt *clnt, unsigned long event)
Stanislav Kinsburskyea8cfa02012-04-27 13:00:17 +0400162{
Trond Myklebust41b6b4d2013-08-23 13:03:50 -0400163 if (clnt->cl_program->pipe_dir_name == NULL)
Stanislav Kinsburskyea8cfa02012-04-27 13:00:17 +0400164 return 1;
Trond Myklebust41b6b4d2013-08-23 13:03:50 -0400165
Trond Myklebustc36dcfe12013-08-26 17:44:26 -0400166 switch (event) {
167 case RPC_PIPEFS_MOUNT:
168 if (clnt->cl_pipedir_objects.pdh_dentry != NULL)
169 return 1;
Trond Myklebust71d3d0e2021-07-26 08:01:27 -0400170 if (refcount_read(&clnt->cl_count) == 0)
Trond Myklebustc36dcfe12013-08-26 17:44:26 -0400171 return 1;
172 break;
173 case RPC_PIPEFS_UMOUNT:
174 if (clnt->cl_pipedir_objects.pdh_dentry == NULL)
175 return 1;
176 break;
177 }
Stanislav Kinsburskyea8cfa02012-04-27 13:00:17 +0400178 return 0;
179}
180
181static int __rpc_clnt_handle_event(struct rpc_clnt *clnt, unsigned long event,
182 struct super_block *sb)
Stanislav Kinsbursky80df9d22012-01-11 19:18:17 +0400183{
184 struct dentry *dentry;
Stanislav Kinsbursky80df9d22012-01-11 19:18:17 +0400185
186 switch (event) {
187 case RPC_PIPEFS_MOUNT:
Trond Myklebust41b6b4d2013-08-23 13:03:50 -0400188 dentry = rpc_setup_pipedir_sb(sb, clnt);
Weston Andros Adamson922eeac2012-10-23 10:43:25 -0400189 if (!dentry)
190 return -ENOENT;
Stanislav Kinsbursky80df9d22012-01-11 19:18:17 +0400191 if (IS_ERR(dentry))
192 return PTR_ERR(dentry);
Stanislav Kinsbursky80df9d22012-01-11 19:18:17 +0400193 break;
194 case RPC_PIPEFS_UMOUNT:
195 __rpc_clnt_remove_pipedir(clnt);
196 break;
197 default:
198 printk(KERN_ERR "%s: unknown event: %ld\n", __func__, event);
199 return -ENOTSUPP;
200 }
Amitoj Kaur Chawla2813b622016-08-08 14:43:49 +0530201 return 0;
Stanislav Kinsbursky80df9d22012-01-11 19:18:17 +0400202}
203
Stanislav Kinsburskyea8cfa02012-04-27 13:00:17 +0400204static int __rpc_pipefs_event(struct rpc_clnt *clnt, unsigned long event,
205 struct super_block *sb)
206{
207 int error = 0;
208
209 for (;; clnt = clnt->cl_parent) {
210 if (!rpc_clnt_skip_event(clnt, event))
211 error = __rpc_clnt_handle_event(clnt, event, sb);
212 if (error || clnt == clnt->cl_parent)
213 break;
214 }
215 return error;
216}
217
Stanislav Kinsburskyda3b4622012-02-27 22:05:29 +0400218static struct rpc_clnt *rpc_get_client_for_event(struct net *net, int event)
219{
220 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
221 struct rpc_clnt *clnt;
222
223 spin_lock(&sn->rpc_client_lock);
224 list_for_each_entry(clnt, &sn->all_clients, cl_clients) {
Stanislav Kinsburskyea8cfa02012-04-27 13:00:17 +0400225 if (rpc_clnt_skip_event(clnt, event))
Stanislav Kinsburskyda3b4622012-02-27 22:05:29 +0400226 continue;
Stanislav Kinsburskyda3b4622012-02-27 22:05:29 +0400227 spin_unlock(&sn->rpc_client_lock);
228 return clnt;
229 }
230 spin_unlock(&sn->rpc_client_lock);
231 return NULL;
232}
233
Stanislav Kinsbursky80df9d22012-01-11 19:18:17 +0400234static int rpc_pipefs_event(struct notifier_block *nb, unsigned long event,
235 void *ptr)
236{
237 struct super_block *sb = ptr;
238 struct rpc_clnt *clnt;
239 int error = 0;
Stanislav Kinsbursky80df9d22012-01-11 19:18:17 +0400240
Stanislav Kinsburskyda3b4622012-02-27 22:05:29 +0400241 while ((clnt = rpc_get_client_for_event(sb->s_fs_info, event))) {
Stanislav Kinsbursky80df9d22012-01-11 19:18:17 +0400242 error = __rpc_pipefs_event(clnt, event, sb);
243 if (error)
244 break;
245 }
Stanislav Kinsbursky80df9d22012-01-11 19:18:17 +0400246 return error;
247}
248
249static struct notifier_block rpc_clients_block = {
250 .notifier_call = rpc_pipefs_event,
Stanislav Kinsburskyeee173252012-01-10 16:13:19 +0400251 .priority = SUNRPC_PIPEFS_RPC_PRIO,
Stanislav Kinsbursky80df9d22012-01-11 19:18:17 +0400252};
253
254int rpc_clients_notifier_register(void)
255{
256 return rpc_pipefs_notifier_register(&rpc_clients_block);
257}
258
259void rpc_clients_notifier_unregister(void)
260{
261 return rpc_pipefs_notifier_unregister(&rpc_clients_block);
262}
263
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400264static struct rpc_xprt *rpc_clnt_set_transport(struct rpc_clnt *clnt,
265 struct rpc_xprt *xprt,
266 const struct rpc_timeout *timeout)
267{
268 struct rpc_xprt *old;
269
270 spin_lock(&clnt->cl_lock);
Trond Myklebust34751b92013-10-28 16:42:44 -0400271 old = rcu_dereference_protected(clnt->cl_xprt,
272 lockdep_is_held(&clnt->cl_lock));
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400273
274 if (!xprt_bound(xprt))
275 clnt->cl_autobind = 1;
276
277 clnt->cl_timeout = timeout;
278 rcu_assign_pointer(clnt->cl_xprt, xprt);
279 spin_unlock(&clnt->cl_lock);
280
281 return old;
282}
283
Trond Myklebustcbbb3442012-04-30 11:52:40 -0400284static void rpc_clnt_set_nodename(struct rpc_clnt *clnt, const char *nodename)
285{
Trond Myklebust03a9a422015-01-30 18:12:28 -0500286 clnt->cl_nodelen = strlcpy(clnt->cl_nodename,
287 nodename, sizeof(clnt->cl_nodename));
Trond Myklebustcbbb3442012-04-30 11:52:40 -0400288}
289
Chuck Leverd746e542013-10-17 14:12:17 -0400290static int rpc_client_register(struct rpc_clnt *clnt,
291 rpc_authflavor_t pseudoflavor,
292 const char *client_name)
Stanislav Kinsburskye73f4cc2013-06-24 11:52:52 +0400293{
Trond Myklebustc2190662013-08-26 19:23:04 -0400294 struct rpc_auth_create_args auth_args = {
Chuck Leverd746e542013-10-17 14:12:17 -0400295 .pseudoflavor = pseudoflavor,
296 .target_name = client_name,
Trond Myklebustc2190662013-08-26 19:23:04 -0400297 };
Stanislav Kinsburskye73f4cc2013-06-24 11:52:52 +0400298 struct rpc_auth *auth;
299 struct net *net = rpc_net_ns(clnt);
300 struct super_block *pipefs_sb;
Trond Myklebusteeee2452013-07-10 15:33:01 -0400301 int err;
Stanislav Kinsburskye73f4cc2013-06-24 11:52:52 +0400302
Jeff Laytonf9c72d12015-03-31 12:03:28 -0400303 rpc_clnt_debugfs_register(clnt);
Jeff Laytonb4b9d2c2014-11-26 14:44:43 -0500304
Stanislav Kinsburskye73f4cc2013-06-24 11:52:52 +0400305 pipefs_sb = rpc_get_sb_net(net);
306 if (pipefs_sb) {
Trond Myklebust41b6b4d2013-08-23 13:03:50 -0400307 err = rpc_setup_pipedir(pipefs_sb, clnt);
Stanislav Kinsburskye73f4cc2013-06-24 11:52:52 +0400308 if (err)
309 goto out;
310 }
311
Trond Myklebusteeee2452013-07-10 15:33:01 -0400312 rpc_register_client(clnt);
313 if (pipefs_sb)
314 rpc_put_sb_net(net);
315
Trond Myklebustc2190662013-08-26 19:23:04 -0400316 auth = rpcauth_create(&auth_args, clnt);
Stanislav Kinsburskye73f4cc2013-06-24 11:52:52 +0400317 if (IS_ERR(auth)) {
318 dprintk("RPC: Couldn't create auth handle (flavor %u)\n",
Chuck Leverd746e542013-10-17 14:12:17 -0400319 pseudoflavor);
Stanislav Kinsburskye73f4cc2013-06-24 11:52:52 +0400320 err = PTR_ERR(auth);
321 goto err_auth;
322 }
Trond Myklebusteeee2452013-07-10 15:33:01 -0400323 return 0;
324err_auth:
325 pipefs_sb = rpc_get_sb_net(net);
Trond Myklebust1540c5d2013-07-14 22:57:50 -0400326 rpc_unregister_client(clnt);
Trond Myklebusteeee2452013-07-10 15:33:01 -0400327 __rpc_clnt_remove_pipedir(clnt);
Stanislav Kinsburskye73f4cc2013-06-24 11:52:52 +0400328out:
329 if (pipefs_sb)
330 rpc_put_sb_net(net);
Olga Kornievskaiac5a382e2021-06-08 15:59:12 -0400331 rpc_sysfs_client_destroy(clnt);
Jeff Laytonb4b9d2c2014-11-26 14:44:43 -0500332 rpc_clnt_debugfs_unregister(clnt);
Stanislav Kinsburskye73f4cc2013-06-24 11:52:52 +0400333 return err;
Stanislav Kinsburskye73f4cc2013-06-24 11:52:52 +0400334}
335
Trond Myklebust2f048db2013-09-04 21:51:44 -0400336static DEFINE_IDA(rpc_clids);
337
Kinglong Meec929ea02017-01-20 16:48:39 +0800338void rpc_cleanup_clids(void)
339{
340 ida_destroy(&rpc_clids);
341}
342
Trond Myklebust2f048db2013-09-04 21:51:44 -0400343static int rpc_alloc_clid(struct rpc_clnt *clnt)
344{
345 int clid;
346
347 clid = ida_simple_get(&rpc_clids, 0, 0, GFP_KERNEL);
348 if (clid < 0)
349 return clid;
350 clnt->cl_clid = clid;
351 return 0;
352}
353
354static void rpc_free_clid(struct rpc_clnt *clnt)
355{
356 ida_simple_remove(&rpc_clids, clnt->cl_clid);
357}
358
Trond Myklebust280ebcf2013-09-02 13:28:04 -0400359static struct rpc_clnt * rpc_new_client(const struct rpc_create_args *args,
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500360 struct rpc_xprt_switch *xps,
Trond Myklebust280ebcf2013-09-02 13:28:04 -0400361 struct rpc_xprt *xprt,
362 struct rpc_clnt *parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363{
Trond Myklebusta613fa12012-01-20 13:53:56 -0500364 const struct rpc_program *program = args->program;
365 const struct rpc_version *version;
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400366 struct rpc_clnt *clnt = NULL;
367 const struct rpc_timeout *timeout;
Trond Myklebust03a9a422015-01-30 18:12:28 -0500368 const char *nodename = args->nodename;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 int err;
Chuck Lever06b8d252007-09-11 18:00:20 -0400370
Trond Myklebust4ada5392007-06-14 17:26:17 -0400371 err = rpciod_up();
372 if (err)
373 goto out_no_rpciod;
Trond Myklebust698b6d02007-12-20 16:03:53 -0500374
Trond Myklebustf05c1242013-04-05 14:13:21 -0400375 err = -EINVAL;
Trond Myklebust698b6d02007-12-20 16:03:53 -0500376 if (args->version >= program->nrvers)
377 goto out_err;
378 version = program->version[args->version];
379 if (version == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 goto out_err;
381
382 err = -ENOMEM;
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700383 clnt = kzalloc(sizeof(*clnt), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 if (!clnt)
385 goto out_err;
Trond Myklebust280ebcf2013-09-02 13:28:04 -0400386 clnt->cl_parent = parent ? : clnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
Trond Myklebust2f048db2013-09-04 21:51:44 -0400388 err = rpc_alloc_clid(clnt);
389 if (err)
390 goto out_no_clid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
Trond Myklebust79caa5f2019-04-24 17:46:42 -0400392 clnt->cl_cred = get_cred(args->cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 clnt->cl_procinfo = version->procs;
394 clnt->cl_maxproc = version->nrprocs;
Benny Halevyd5b337b2008-09-28 09:21:26 +0300395 clnt->cl_prog = args->prognumber ? : program->number;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 clnt->cl_vers = version->number;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 clnt->cl_stats = program->stats;
Chuck Lever11c556b2006-03-20 13:44:22 -0500398 clnt->cl_metrics = rpc_alloc_iostats(clnt);
Trond Myklebust6739ffb2013-08-26 15:38:11 -0400399 rpc_init_pipe_dir_head(&clnt->cl_pipedir_objects);
Trond Myklebust23bf85b2006-11-21 10:40:23 -0500400 err = -ENOMEM;
401 if (clnt->cl_metrics == NULL)
402 goto out_no_stats;
Trond Myklebust3e32a5d2006-11-16 11:37:27 -0500403 clnt->cl_program = program;
Trond Myklebust6529eba2007-06-14 16:40:14 -0400404 INIT_LIST_HEAD(&clnt->cl_tasks);
Trond Myklebust4bef61f2007-06-16 14:17:01 -0400405 spin_lock_init(&clnt->cl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400407 timeout = xprt->timeout;
Trond Myklebustba7392b2007-12-20 16:03:55 -0500408 if (args->timeout != NULL) {
409 memcpy(&clnt->cl_timeout_default, args->timeout,
410 sizeof(clnt->cl_timeout_default));
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400411 timeout = &clnt->cl_timeout_default;
Trond Myklebustba7392b2007-12-20 16:03:55 -0500412 }
413
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400414 rpc_clnt_set_transport(clnt, xprt, timeout);
Olga Kornievskaiae0918532021-06-23 23:28:46 -0400415 xprt->main = true;
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500416 xprt_iter_init(&clnt->cl_xpi, xps);
417 xprt_switch_put(xps);
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400418
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 clnt->cl_rtt = &clnt->cl_rtt_default;
Trond Myklebustba7392b2007-12-20 16:03:55 -0500420 rpc_init_rtt(&clnt->cl_rtt_default, clnt->cl_timeout->to_initval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
Trond Myklebust71d3d0e2021-07-26 08:01:27 -0400422 refcount_set(&clnt->cl_count, 1);
Trond Myklebust34f52e32007-06-14 16:40:31 -0400423
Trond Myklebust03a9a422015-01-30 18:12:28 -0500424 if (nodename == NULL)
425 nodename = utsname()->nodename;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 /* save the nodename */
Trond Myklebust03a9a422015-01-30 18:12:28 -0500427 rpc_clnt_set_nodename(clnt, nodename);
Stanislav Kinsburskye73f4cc2013-06-24 11:52:52 +0400428
Olga Kornievskaia2a338a52021-06-08 15:59:17 -0400429 rpc_sysfs_client_setup(clnt, xps, rpc_net_ns(clnt));
Chuck Leverd746e542013-10-17 14:12:17 -0400430 err = rpc_client_register(clnt, args->authflavor, args->client_name);
Stanislav Kinsburskye73f4cc2013-06-24 11:52:52 +0400431 if (err)
432 goto out_no_path;
Trond Myklebust280ebcf2013-09-02 13:28:04 -0400433 if (parent)
Trond Myklebust71d3d0e2021-07-26 08:01:27 -0400434 refcount_inc(&parent->cl_count);
Chuck Lever42aad0d2020-05-12 17:13:39 -0400435
436 trace_rpc_clnt_new(clnt, xprt, program->name, args->servername);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 return clnt;
438
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439out_no_path:
Trond Myklebust23bf85b2006-11-21 10:40:23 -0500440 rpc_free_iostats(clnt->cl_metrics);
441out_no_stats:
Trond Myklebust79caa5f2019-04-24 17:46:42 -0400442 put_cred(clnt->cl_cred);
Trond Myklebust2f048db2013-09-04 21:51:44 -0400443 rpc_free_clid(clnt);
444out_no_clid:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 kfree(clnt);
446out_err:
Trond Myklebust4ada5392007-06-14 17:26:17 -0400447 rpciod_down();
448out_no_rpciod:
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500449 xprt_switch_put(xps);
Trond Myklebustf05c1242013-04-05 14:13:21 -0400450 xprt_put(xprt);
Chuck Lever42aad0d2020-05-12 17:13:39 -0400451 trace_rpc_clnt_new_err(program->name, args->servername, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 return ERR_PTR(err);
453}
454
J. Bruce Fieldsd50039e2016-05-16 17:03:42 -0400455static struct rpc_clnt *rpc_create_xprt(struct rpc_create_args *args,
Kinglong Mee83ddfeb2014-03-24 11:58:59 +0800456 struct rpc_xprt *xprt)
457{
458 struct rpc_clnt *clnt = NULL;
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500459 struct rpc_xprt_switch *xps;
Kinglong Mee83ddfeb2014-03-24 11:58:59 +0800460
J. Bruce Fields39a9bea2016-05-17 12:38:21 -0400461 if (args->bc_xprt && args->bc_xprt->xpt_bc_xps) {
Chuck Lever16590a22016-08-22 14:57:42 -0400462 WARN_ON_ONCE(!(args->protocol & XPRT_TRANSPORT_BC));
J. Bruce Fields39a9bea2016-05-17 12:38:21 -0400463 xps = args->bc_xprt->xpt_bc_xps;
464 xprt_switch_get(xps);
465 } else {
466 xps = xprt_switch_alloc(xprt, GFP_KERNEL);
467 if (xps == NULL) {
468 xprt_put(xprt);
469 return ERR_PTR(-ENOMEM);
470 }
471 if (xprt->bc_xprt) {
472 xprt_switch_get(xps);
473 xprt->bc_xprt->xpt_bc_xps = xps;
474 }
J. Bruce Fields1208fd52016-05-20 17:07:17 -0400475 }
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500476 clnt = rpc_new_client(args, xps, xprt, NULL);
Kinglong Mee83ddfeb2014-03-24 11:58:59 +0800477 if (IS_ERR(clnt))
478 return clnt;
479
480 if (!(args->flags & RPC_CLNT_CREATE_NOPING)) {
481 int err = rpc_ping(clnt);
482 if (err != 0) {
483 rpc_shutdown_client(clnt);
484 return ERR_PTR(err);
485 }
486 }
487
488 clnt->cl_softrtry = 1;
Trond Myklebustae6ec912019-04-07 13:58:58 -0400489 if (args->flags & (RPC_CLNT_CREATE_HARDRTRY|RPC_CLNT_CREATE_SOFTERR)) {
Kinglong Mee83ddfeb2014-03-24 11:58:59 +0800490 clnt->cl_softrtry = 0;
Trond Myklebustae6ec912019-04-07 13:58:58 -0400491 if (args->flags & RPC_CLNT_CREATE_SOFTERR)
492 clnt->cl_softerr = 1;
493 }
Kinglong Mee83ddfeb2014-03-24 11:58:59 +0800494
495 if (args->flags & RPC_CLNT_CREATE_AUTOBIND)
496 clnt->cl_autobind = 1;
Trond Myklebust2aca5b82014-09-24 22:35:58 -0400497 if (args->flags & RPC_CLNT_CREATE_NO_RETRANS_TIMEOUT)
498 clnt->cl_noretranstimeo = 1;
Kinglong Mee83ddfeb2014-03-24 11:58:59 +0800499 if (args->flags & RPC_CLNT_CREATE_DISCRTRY)
500 clnt->cl_discrtry = 1;
501 if (!(args->flags & RPC_CLNT_CREATE_QUIET))
502 clnt->cl_chatty = 1;
503
504 return clnt;
505}
Kinglong Mee83ddfeb2014-03-24 11:58:59 +0800506
Ben Hutchings2c530402012-07-10 10:55:09 +0000507/**
Chuck Leverc2866762006-08-22 20:06:20 -0400508 * rpc_create - create an RPC client and transport with one call
509 * @args: rpc_clnt create argument structure
510 *
511 * Creates and initializes an RPC transport and an RPC client.
512 *
513 * It can ping the server in order to determine if it is up, and to see if
514 * it supports this program and version. RPC_CLNT_CREATE_NOPING disables
515 * this behavior so asynchronous tasks can also use rpc_create.
516 */
517struct rpc_clnt *rpc_create(struct rpc_create_args *args)
518{
519 struct rpc_xprt *xprt;
\"Talpey, Thomas\3c341b0b2007-09-10 13:47:07 -0400520 struct xprt_create xprtargs = {
Pavel Emelyanov9a23e332010-09-29 16:05:12 +0400521 .net = args->net,
\"Talpey, Thomas\4fa016e2007-09-10 13:47:57 -0400522 .ident = args->protocol,
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +0200523 .srcaddr = args->saddress,
Frank van Maarseveen96802a02007-07-08 13:08:54 +0200524 .dstaddr = args->address,
525 .addrlen = args->addrsize,
Trond Myklebust4e0038b2012-03-01 17:01:05 -0500526 .servername = args->servername,
Alexandros Batsakisf300bab2009-09-10 17:33:30 +0300527 .bc_xprt = args->bc_xprt,
Frank van Maarseveen96802a02007-07-08 13:08:54 +0200528 };
Chuck Lever510deb02007-12-10 14:56:24 -0500529 char servername[48];
Trond Myklebust612b41f2017-04-27 08:50:51 -0400530 struct rpc_clnt *clnt;
531 int i;
Chuck Leverc2866762006-08-22 20:06:20 -0400532
J. Bruce Fieldsd50039e2016-05-16 17:03:42 -0400533 if (args->bc_xprt) {
Chuck Lever16590a22016-08-22 14:57:42 -0400534 WARN_ON_ONCE(!(args->protocol & XPRT_TRANSPORT_BC));
J. Bruce Fieldsd50039e2016-05-16 17:03:42 -0400535 xprt = args->bc_xprt->xpt_bc_xprt;
536 if (xprt) {
537 xprt_get(xprt);
538 return rpc_create_xprt(args, xprt);
539 }
540 }
541
Trond Myklebustb7993ce2013-04-14 11:42:00 -0400542 if (args->flags & RPC_CLNT_CREATE_INFINITE_SLOTS)
543 xprtargs.flags |= XPRT_CREATE_INFINITE_SLOTS;
J. Bruce Fields33d90ac2013-04-11 15:06:36 -0400544 if (args->flags & RPC_CLNT_CREATE_NO_IDLE_TIMEOUT)
545 xprtargs.flags |= XPRT_CREATE_NO_IDLE_TIMEOUT;
Chuck Leverc2866762006-08-22 20:06:20 -0400546 /*
Chuck Lever43780b82007-07-01 12:13:22 -0400547 * If the caller chooses not to specify a hostname, whip
548 * up a string representation of the passed-in address.
549 */
Trond Myklebust4e0038b2012-03-01 17:01:05 -0500550 if (xprtargs.servername == NULL) {
Chuck Lever176e21e2011-05-09 15:22:44 -0400551 struct sockaddr_un *sun =
552 (struct sockaddr_un *)args->address;
Chuck Leverda09eb92011-05-09 15:22:25 -0400553 struct sockaddr_in *sin =
554 (struct sockaddr_in *)args->address;
555 struct sockaddr_in6 *sin6 =
556 (struct sockaddr_in6 *)args->address;
557
Chuck Lever510deb02007-12-10 14:56:24 -0500558 servername[0] = '\0';
559 switch (args->address->sa_family) {
Chuck Lever176e21e2011-05-09 15:22:44 -0400560 case AF_LOCAL:
561 snprintf(servername, sizeof(servername), "%s",
562 sun->sun_path);
563 break;
Chuck Leverda09eb92011-05-09 15:22:25 -0400564 case AF_INET:
Harvey Harrison21454aa2008-10-31 00:54:56 -0700565 snprintf(servername, sizeof(servername), "%pI4",
566 &sin->sin_addr.s_addr);
Chuck Lever510deb02007-12-10 14:56:24 -0500567 break;
Chuck Leverda09eb92011-05-09 15:22:25 -0400568 case AF_INET6:
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700569 snprintf(servername, sizeof(servername), "%pI6",
Chuck Leverda09eb92011-05-09 15:22:25 -0400570 &sin6->sin6_addr);
Chuck Lever510deb02007-12-10 14:56:24 -0500571 break;
Chuck Lever510deb02007-12-10 14:56:24 -0500572 default:
573 /* caller wants default server name, but
574 * address family isn't recognized. */
575 return ERR_PTR(-EINVAL);
576 }
Trond Myklebust4e0038b2012-03-01 17:01:05 -0500577 xprtargs.servername = servername;
Chuck Lever43780b82007-07-01 12:13:22 -0400578 }
579
Chuck Lever510deb02007-12-10 14:56:24 -0500580 xprt = xprt_create_transport(&xprtargs);
581 if (IS_ERR(xprt))
582 return (struct rpc_clnt *)xprt;
583
Chuck Lever43780b82007-07-01 12:13:22 -0400584 /*
Chuck Leverc2866762006-08-22 20:06:20 -0400585 * By default, kernel RPC client connects from a reserved port.
586 * CAP_NET_BIND_SERVICE will not be set for unprivileged requesters,
587 * but it is always enabled for rpciod, which handles the connect
588 * operation.
589 */
590 xprt->resvport = 1;
591 if (args->flags & RPC_CLNT_CREATE_NONPRIVPORT)
592 xprt->resvport = 0;
Trond Myklebuste6237b62019-10-17 11:13:54 -0400593 xprt->reuseport = 0;
594 if (args->flags & RPC_CLNT_CREATE_REUSEPORT)
595 xprt->reuseport = 1;
Chuck Leverc2866762006-08-22 20:06:20 -0400596
Trond Myklebust612b41f2017-04-27 08:50:51 -0400597 clnt = rpc_create_xprt(args, xprt);
598 if (IS_ERR(clnt) || args->nconnect <= 1)
599 return clnt;
600
601 for (i = 0; i < args->nconnect - 1; i++) {
602 if (rpc_clnt_add_xprt(clnt, &xprtargs, NULL, NULL) < 0)
603 break;
604 }
605 return clnt;
Chuck Leverc2866762006-08-22 20:06:20 -0400606}
Chuck Leverb86acd52006-08-22 20:06:22 -0400607EXPORT_SYMBOL_GPL(rpc_create);
Chuck Leverc2866762006-08-22 20:06:20 -0400608
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609/*
610 * This function clones the RPC client structure. It allows us to share the
611 * same transport while varying parameters such as the authentication
612 * flavour.
613 */
Chuck Lever1b63a752012-09-14 17:23:52 -0400614static struct rpc_clnt *__rpc_clone_client(struct rpc_create_args *args,
615 struct rpc_clnt *clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616{
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500617 struct rpc_xprt_switch *xps;
Trond Myklebust2446ab62012-03-01 17:00:56 -0500618 struct rpc_xprt *xprt;
Chuck Lever1b63a752012-09-14 17:23:52 -0400619 struct rpc_clnt *new;
620 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
Chuck Lever1b63a752012-09-14 17:23:52 -0400622 err = -ENOMEM;
Trond Myklebust2446ab62012-03-01 17:00:56 -0500623 rcu_read_lock();
624 xprt = xprt_get(rcu_dereference(clnt->cl_xprt));
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500625 xps = xprt_switch_get(rcu_dereference(clnt->cl_xpi.xpi_xpswitch));
Trond Myklebust2446ab62012-03-01 17:00:56 -0500626 rcu_read_unlock();
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500627 if (xprt == NULL || xps == NULL) {
628 xprt_put(xprt);
629 xprt_switch_put(xps);
Chuck Lever1b63a752012-09-14 17:23:52 -0400630 goto out_err;
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500631 }
Chuck Lever1b63a752012-09-14 17:23:52 -0400632 args->servername = xprt->servername;
Trond Myklebust03a9a422015-01-30 18:12:28 -0500633 args->nodename = clnt->cl_nodename;
Chuck Lever1b63a752012-09-14 17:23:52 -0400634
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500635 new = rpc_new_client(args, xps, xprt, clnt);
Chuck Lever42aad0d2020-05-12 17:13:39 -0400636 if (IS_ERR(new))
637 return new;
Chuck Lever1b63a752012-09-14 17:23:52 -0400638
Chuck Lever1b63a752012-09-14 17:23:52 -0400639 /* Turn off autobind on clones */
640 new->cl_autobind = 0;
641 new->cl_softrtry = clnt->cl_softrtry;
Trond Myklebustae6ec912019-04-07 13:58:58 -0400642 new->cl_softerr = clnt->cl_softerr;
Trond Myklebust2aca5b82014-09-24 22:35:58 -0400643 new->cl_noretranstimeo = clnt->cl_noretranstimeo;
Chuck Lever1b63a752012-09-14 17:23:52 -0400644 new->cl_discrtry = clnt->cl_discrtry;
645 new->cl_chatty = clnt->cl_chatty;
NeilBrown5e169232018-12-03 11:30:30 +1100646 new->cl_principal = clnt->cl_principal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 return new;
Chuck Lever1b63a752012-09-14 17:23:52 -0400648
Chuck Lever1b63a752012-09-14 17:23:52 -0400649out_err:
Chuck Lever42aad0d2020-05-12 17:13:39 -0400650 trace_rpc_clnt_clone_err(clnt, err);
Trond Myklebust3e32a5d2006-11-16 11:37:27 -0500651 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652}
Chuck Lever1b63a752012-09-14 17:23:52 -0400653
654/**
655 * rpc_clone_client - Clone an RPC client structure
656 *
657 * @clnt: RPC client whose parameters are copied
658 *
659 * Returns a fresh RPC client or an ERR_PTR.
660 */
661struct rpc_clnt *rpc_clone_client(struct rpc_clnt *clnt)
662{
663 struct rpc_create_args args = {
664 .program = clnt->cl_program,
665 .prognumber = clnt->cl_prog,
666 .version = clnt->cl_vers,
667 .authflavor = clnt->cl_auth->au_flavor,
Trond Myklebust79caa5f2019-04-24 17:46:42 -0400668 .cred = clnt->cl_cred,
Chuck Lever1b63a752012-09-14 17:23:52 -0400669 };
670 return __rpc_clone_client(&args, clnt);
671}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400672EXPORT_SYMBOL_GPL(rpc_clone_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
Chuck Leverba9b5842012-09-14 17:24:02 -0400674/**
675 * rpc_clone_client_set_auth - Clone an RPC client structure and set its auth
676 *
677 * @clnt: RPC client whose parameters are copied
Randy Dunlap7144bca2013-01-09 17:12:35 -0800678 * @flavor: security flavor for new client
Chuck Leverba9b5842012-09-14 17:24:02 -0400679 *
680 * Returns a fresh RPC client or an ERR_PTR.
681 */
682struct rpc_clnt *
683rpc_clone_client_set_auth(struct rpc_clnt *clnt, rpc_authflavor_t flavor)
684{
685 struct rpc_create_args args = {
686 .program = clnt->cl_program,
687 .prognumber = clnt->cl_prog,
688 .version = clnt->cl_vers,
689 .authflavor = flavor,
Trond Myklebust79caa5f2019-04-24 17:46:42 -0400690 .cred = clnt->cl_cred,
Chuck Leverba9b5842012-09-14 17:24:02 -0400691 };
692 return __rpc_clone_client(&args, clnt);
693}
694EXPORT_SYMBOL_GPL(rpc_clone_client_set_auth);
695
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400696/**
697 * rpc_switch_client_transport: switch the RPC transport on the fly
698 * @clnt: pointer to a struct rpc_clnt
699 * @args: pointer to the new transport arguments
700 * @timeout: pointer to the new timeout parameters
701 *
702 * This function allows the caller to switch the RPC transport for the
703 * rpc_clnt structure 'clnt' to allow it to connect to a mirrored NFS
704 * server, for instance. It assumes that the caller has ensured that
705 * there are no active RPC tasks by using some form of locking.
706 *
707 * Returns zero if "clnt" is now using the new xprt. Otherwise a
708 * negative errno is returned, and "clnt" continues to use the old
709 * xprt.
710 */
711int rpc_switch_client_transport(struct rpc_clnt *clnt,
712 struct xprt_create *args,
713 const struct rpc_timeout *timeout)
714{
715 const struct rpc_timeout *old_timeo;
716 rpc_authflavor_t pseudoflavor;
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500717 struct rpc_xprt_switch *xps, *oldxps;
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400718 struct rpc_xprt *xprt, *old;
719 struct rpc_clnt *parent;
720 int err;
721
722 xprt = xprt_create_transport(args);
Chuck Lever42aad0d2020-05-12 17:13:39 -0400723 if (IS_ERR(xprt))
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400724 return PTR_ERR(xprt);
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400725
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500726 xps = xprt_switch_alloc(xprt, GFP_KERNEL);
727 if (xps == NULL) {
728 xprt_put(xprt);
729 return -ENOMEM;
730 }
731
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400732 pseudoflavor = clnt->cl_auth->au_flavor;
733
734 old_timeo = clnt->cl_timeout;
735 old = rpc_clnt_set_transport(clnt, xprt, timeout);
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500736 oldxps = xprt_iter_xchg_switch(&clnt->cl_xpi, xps);
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400737
738 rpc_unregister_client(clnt);
739 __rpc_clnt_remove_pipedir(clnt);
Olga Kornievskaiac5a382e2021-06-08 15:59:12 -0400740 rpc_sysfs_client_destroy(clnt);
Jeff Laytonb4b9d2c2014-11-26 14:44:43 -0500741 rpc_clnt_debugfs_unregister(clnt);
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400742
743 /*
744 * A new transport was created. "clnt" therefore
745 * becomes the root of a new cl_parent tree. clnt's
746 * children, if it has any, still point to the old xprt.
747 */
748 parent = clnt->cl_parent;
749 clnt->cl_parent = clnt;
750
751 /*
752 * The old rpc_auth cache cannot be re-used. GSS
753 * contexts in particular are between a single
754 * client and server.
755 */
756 err = rpc_client_register(clnt, pseudoflavor, NULL);
757 if (err)
758 goto out_revert;
759
760 synchronize_rcu();
761 if (parent != clnt)
762 rpc_release_client(parent);
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500763 xprt_switch_put(oldxps);
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400764 xprt_put(old);
Chuck Lever42aad0d2020-05-12 17:13:39 -0400765 trace_rpc_clnt_replace_xprt(clnt);
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400766 return 0;
767
768out_revert:
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500769 xps = xprt_iter_xchg_switch(&clnt->cl_xpi, oldxps);
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400770 rpc_clnt_set_transport(clnt, old, old_timeo);
771 clnt->cl_parent = parent;
772 rpc_client_register(clnt, pseudoflavor, NULL);
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500773 xprt_switch_put(xps);
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400774 xprt_put(xprt);
Chuck Lever42aad0d2020-05-12 17:13:39 -0400775 trace_rpc_clnt_replace_xprt_err(clnt);
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400776 return err;
777}
778EXPORT_SYMBOL_GPL(rpc_switch_client_transport);
779
Trond Myklebust32278862016-01-30 20:39:19 -0500780static
781int rpc_clnt_xprt_iter_init(struct rpc_clnt *clnt, struct rpc_xprt_iter *xpi)
782{
783 struct rpc_xprt_switch *xps;
784
785 rcu_read_lock();
786 xps = xprt_switch_get(rcu_dereference(clnt->cl_xpi.xpi_xpswitch));
787 rcu_read_unlock();
788 if (xps == NULL)
789 return -EAGAIN;
790 xprt_iter_init_listall(xpi, xps);
791 xprt_switch_put(xps);
792 return 0;
793}
794
795/**
796 * rpc_clnt_iterate_for_each_xprt - Apply a function to all transports
797 * @clnt: pointer to client
798 * @fn: function to apply
799 * @data: void pointer to function data
800 *
801 * Iterates through the list of RPC transports currently attached to the
802 * client and applies the function fn(clnt, xprt, data).
803 *
804 * On error, the iteration stops, and the function returns the error value.
805 */
806int rpc_clnt_iterate_for_each_xprt(struct rpc_clnt *clnt,
807 int (*fn)(struct rpc_clnt *, struct rpc_xprt *, void *),
808 void *data)
809{
810 struct rpc_xprt_iter xpi;
811 int ret;
812
813 ret = rpc_clnt_xprt_iter_init(clnt, &xpi);
814 if (ret)
815 return ret;
816 for (;;) {
817 struct rpc_xprt *xprt = xprt_iter_get_next(&xpi);
818
819 if (!xprt)
820 break;
821 ret = fn(clnt, xprt, data);
822 xprt_put(xprt);
823 if (ret < 0)
824 break;
825 }
826 xprt_iter_destroy(&xpi);
827 return ret;
828}
829EXPORT_SYMBOL_GPL(rpc_clnt_iterate_for_each_xprt);
830
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831/*
Trond Myklebust58f96122010-07-31 14:29:08 -0400832 * Kill all tasks for the given client.
833 * XXX: kill their descendants as well?
834 */
835void rpc_killall_tasks(struct rpc_clnt *clnt)
836{
837 struct rpc_task *rovr;
838
839
840 if (list_empty(&clnt->cl_tasks))
841 return;
Chuck Lever42aad0d2020-05-12 17:13:39 -0400842
Trond Myklebust58f96122010-07-31 14:29:08 -0400843 /*
844 * Spin lock all_tasks to prevent changes...
845 */
Chuck Lever42aad0d2020-05-12 17:13:39 -0400846 trace_rpc_clnt_killall(clnt);
Trond Myklebust58f96122010-07-31 14:29:08 -0400847 spin_lock(&clnt->cl_lock);
Trond Myklebustae67bd32019-04-07 13:58:44 -0400848 list_for_each_entry(rovr, &clnt->cl_tasks, tk_task)
849 rpc_signal_task(rovr);
Trond Myklebust58f96122010-07-31 14:29:08 -0400850 spin_unlock(&clnt->cl_lock);
851}
852EXPORT_SYMBOL_GPL(rpc_killall_tasks);
853
854/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 * Properly shut down an RPC client, terminating all outstanding
Trond Myklebust90c57552007-06-09 19:49:36 -0400856 * requests.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 */
Trond Myklebust4c402b42007-06-14 16:40:32 -0400858void rpc_shutdown_client(struct rpc_clnt *clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859{
Weston Andros Adamson168e4b32012-10-30 17:01:40 -0400860 might_sleep();
861
Chuck Lever42aad0d2020-05-12 17:13:39 -0400862 trace_rpc_clnt_shutdown(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
Trond Myklebust34f52e32007-06-14 16:40:31 -0400864 while (!list_empty(&clnt->cl_tasks)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 rpc_killall_tasks(clnt);
Ingo Molnar532347e2006-01-09 20:52:53 -0800866 wait_event_timeout(destroy_wait,
Trond Myklebust34f52e32007-06-14 16:40:31 -0400867 list_empty(&clnt->cl_tasks), 1*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 }
869
Trond Myklebust4c402b42007-06-14 16:40:32 -0400870 rpc_release_client(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400872EXPORT_SYMBOL_GPL(rpc_shutdown_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
874/*
Trond Myklebust34f52e32007-06-14 16:40:31 -0400875 * Free an RPC client
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 */
NeilBrown7c4310f2020-04-03 14:33:41 +1100877static void rpc_free_client_work(struct work_struct *work)
878{
879 struct rpc_clnt *clnt = container_of(work, struct rpc_clnt, cl_work);
880
Chuck Lever42aad0d2020-05-12 17:13:39 -0400881 trace_rpc_clnt_free(clnt);
882
NeilBrown7c4310f2020-04-03 14:33:41 +1100883 /* These might block on processes that might allocate memory,
884 * so they cannot be called in rpciod, so they are handled separately
885 * here.
886 */
Olga Kornievskaiac5a382e2021-06-08 15:59:12 -0400887 rpc_sysfs_client_destroy(clnt);
NeilBrown7c4310f2020-04-03 14:33:41 +1100888 rpc_clnt_debugfs_unregister(clnt);
J. Bruce Fields933496e2020-05-13 18:38:40 -0400889 rpc_free_clid(clnt);
NeilBrown7c4310f2020-04-03 14:33:41 +1100890 rpc_clnt_remove_pipedir(clnt);
NeilBrown31e9a7f2020-05-09 07:25:11 +1000891 xprt_put(rcu_dereference_raw(clnt->cl_xprt));
NeilBrown7c4310f2020-04-03 14:33:41 +1100892
893 kfree(clnt);
894 rpciod_down();
895}
Trond Myklebustd07ba842013-11-12 17:24:36 -0500896static struct rpc_clnt *
Trond Myklebust006abe82010-09-12 19:55:25 -0400897rpc_free_client(struct rpc_clnt *clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898{
Trond Myklebustd07ba842013-11-12 17:24:36 -0500899 struct rpc_clnt *parent = NULL;
900
Chuck Lever42aad0d2020-05-12 17:13:39 -0400901 trace_rpc_clnt_release(clnt);
Trond Myklebust6eac7d32012-01-20 13:53:37 -0500902 if (clnt->cl_parent != clnt)
Trond Myklebustd07ba842013-11-12 17:24:36 -0500903 parent = clnt->cl_parent;
Stanislav Kinsburskyadb6fa72013-06-26 10:15:14 +0400904 rpc_unregister_client(clnt);
Chuck Lever11c556b2006-03-20 13:44:22 -0500905 rpc_free_iostats(clnt->cl_metrics);
906 clnt->cl_metrics = NULL;
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500907 xprt_iter_destroy(&clnt->cl_xpi);
Trond Myklebust79caa5f2019-04-24 17:46:42 -0400908 put_cred(clnt->cl_cred);
NeilBrown7c4310f2020-04-03 14:33:41 +1100909
910 INIT_WORK(&clnt->cl_work, rpc_free_client_work);
911 schedule_work(&clnt->cl_work);
Trond Myklebustd07ba842013-11-12 17:24:36 -0500912 return parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913}
914
915/*
Trond Myklebust1dd17ec2007-06-26 16:57:41 -0400916 * Free an RPC client
917 */
Stephen Hemminger8fdee4c2018-07-24 12:29:15 -0700918static struct rpc_clnt *
Trond Myklebust006abe82010-09-12 19:55:25 -0400919rpc_free_auth(struct rpc_clnt *clnt)
Trond Myklebust1dd17ec2007-06-26 16:57:41 -0400920{
Trond Myklebust1dd17ec2007-06-26 16:57:41 -0400921 /*
922 * Note: RPCSEC_GSS may need to send NULL RPC calls in order to
923 * release remaining GSS contexts. This mechanism ensures
924 * that it can do so safely.
925 */
Trond Myklebust71d3d0e2021-07-26 08:01:27 -0400926 if (clnt->cl_auth != NULL) {
927 rpcauth_release(clnt->cl_auth);
928 clnt->cl_auth = NULL;
929 }
930 if (refcount_dec_and_test(&clnt->cl_count))
Trond Myklebustd07ba842013-11-12 17:24:36 -0500931 return rpc_free_client(clnt);
932 return NULL;
Trond Myklebust1dd17ec2007-06-26 16:57:41 -0400933}
934
935/*
Trond Myklebust34f52e32007-06-14 16:40:31 -0400936 * Release reference to the RPC client
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 */
938void
939rpc_release_client(struct rpc_clnt *clnt)
940{
Trond Myklebustd07ba842013-11-12 17:24:36 -0500941 do {
942 if (list_empty(&clnt->cl_tasks))
943 wake_up(&destroy_wait);
Trond Myklebust71d3d0e2021-07-26 08:01:27 -0400944 if (refcount_dec_not_one(&clnt->cl_count))
Trond Myklebustd07ba842013-11-12 17:24:36 -0500945 break;
946 clnt = rpc_free_auth(clnt);
947 } while (clnt != NULL);
Trond Myklebust34f52e32007-06-14 16:40:31 -0400948}
Simo Sorce1d658332012-05-25 18:09:55 -0400949EXPORT_SYMBOL_GPL(rpc_release_client);
Trond Myklebust34f52e32007-06-14 16:40:31 -0400950
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000951/**
952 * rpc_bind_new_program - bind a new RPC program to an existing client
Randy Dunlap65b6e422008-02-13 15:03:23 -0800953 * @old: old rpc_client
954 * @program: rpc program to set
955 * @vers: rpc program version
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000956 *
957 * Clones the rpc client and sets up a new RPC program. This is mainly
958 * of use for enabling different RPC programs to share the same transport.
959 * The Sun NFSv2/v3 ACL protocol can do this.
960 */
961struct rpc_clnt *rpc_bind_new_program(struct rpc_clnt *old,
Trond Myklebusta613fa12012-01-20 13:53:56 -0500962 const struct rpc_program *program,
Chuck Lever89eb21c2007-09-11 18:00:09 -0400963 u32 vers)
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000964{
Trond Myklebustf994c432012-11-01 12:14:14 -0400965 struct rpc_create_args args = {
966 .program = program,
967 .prognumber = program->number,
968 .version = vers,
969 .authflavor = old->cl_auth->au_flavor,
Trond Myklebust79caa5f2019-04-24 17:46:42 -0400970 .cred = old->cl_cred,
Trond Myklebustf994c432012-11-01 12:14:14 -0400971 };
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000972 struct rpc_clnt *clnt;
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000973 int err;
974
Trond Myklebustf994c432012-11-01 12:14:14 -0400975 clnt = __rpc_clone_client(&args, old);
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000976 if (IS_ERR(clnt))
977 goto out;
Chuck Levercaabea82009-12-03 15:58:56 -0500978 err = rpc_ping(clnt);
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000979 if (err != 0) {
980 rpc_shutdown_client(clnt);
981 clnt = ERR_PTR(err);
982 }
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800983out:
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000984 return clnt;
985}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400986EXPORT_SYMBOL_GPL(rpc_bind_new_program);
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000987
Trond Myklebusta101b042019-07-11 16:33:12 -0400988struct rpc_xprt *
989rpc_task_get_xprt(struct rpc_clnt *clnt, struct rpc_xprt *xprt)
Trond Myklebust21f0ffa2017-04-28 10:52:42 -0400990{
991 struct rpc_xprt_switch *xps;
Trond Myklebust21f0ffa2017-04-28 10:52:42 -0400992
993 if (!xprt)
994 return NULL;
995 rcu_read_lock();
996 xps = rcu_dereference(clnt->cl_xpi.xpi_xpswitch);
997 atomic_long_inc(&xps->xps_queuelen);
998 rcu_read_unlock();
999 atomic_long_inc(&xprt->queuelen);
1000
1001 return xprt;
1002}
1003
1004static void
1005rpc_task_release_xprt(struct rpc_clnt *clnt, struct rpc_xprt *xprt)
1006{
1007 struct rpc_xprt_switch *xps;
1008
1009 atomic_long_dec(&xprt->queuelen);
1010 rcu_read_lock();
1011 xps = rcu_dereference(clnt->cl_xpi.xpi_xpswitch);
1012 atomic_long_dec(&xps->xps_queuelen);
1013 rcu_read_unlock();
1014
1015 xprt_put(xprt);
1016}
1017
Bill Baker0f90be12018-06-19 16:24:58 -05001018void rpc_task_release_transport(struct rpc_task *task)
1019{
1020 struct rpc_xprt *xprt = task->tk_xprt;
1021
1022 if (xprt) {
1023 task->tk_xprt = NULL;
Trond Myklebust21f0ffa2017-04-28 10:52:42 -04001024 if (task->tk_client)
1025 rpc_task_release_xprt(task->tk_client, xprt);
1026 else
1027 xprt_put(xprt);
Bill Baker0f90be12018-06-19 16:24:58 -05001028 }
1029}
1030EXPORT_SYMBOL_GPL(rpc_task_release_transport);
1031
Trond Myklebust58f96122010-07-31 14:29:08 -04001032void rpc_task_release_client(struct rpc_task *task)
1033{
1034 struct rpc_clnt *clnt = task->tk_client;
1035
Trond Myklebust21f0ffa2017-04-28 10:52:42 -04001036 rpc_task_release_transport(task);
Trond Myklebust58f96122010-07-31 14:29:08 -04001037 if (clnt != NULL) {
1038 /* Remove from client task list */
1039 spin_lock(&clnt->cl_lock);
1040 list_del(&task->tk_task);
1041 spin_unlock(&clnt->cl_lock);
1042 task->tk_client = NULL;
1043
1044 rpc_release_client(clnt);
1045 }
Bill Baker0f90be12018-06-19 16:24:58 -05001046}
Trond Myklebustfb43d172016-01-30 16:39:26 -05001047
Trond Myklebusta101b042019-07-11 16:33:12 -04001048static struct rpc_xprt *
1049rpc_task_get_first_xprt(struct rpc_clnt *clnt)
1050{
1051 struct rpc_xprt *xprt;
1052
1053 rcu_read_lock();
1054 xprt = xprt_get(rcu_dereference(clnt->cl_xprt));
1055 rcu_read_unlock();
1056 return rpc_task_get_xprt(clnt, xprt);
1057}
1058
1059static struct rpc_xprt *
1060rpc_task_get_next_xprt(struct rpc_clnt *clnt)
1061{
1062 return rpc_task_get_xprt(clnt, xprt_iter_get_next(&clnt->cl_xpi));
1063}
1064
Bill Baker0f90be12018-06-19 16:24:58 -05001065static
1066void rpc_task_set_transport(struct rpc_task *task, struct rpc_clnt *clnt)
1067{
NeilBrown5a0c2572019-05-30 10:41:28 +10001068 if (task->tk_xprt)
1069 return;
1070 if (task->tk_flags & RPC_TASK_NO_ROUND_ROBIN)
1071 task->tk_xprt = rpc_task_get_first_xprt(clnt);
1072 else
Trond Myklebusta101b042019-07-11 16:33:12 -04001073 task->tk_xprt = rpc_task_get_next_xprt(clnt);
Trond Myklebust58f96122010-07-31 14:29:08 -04001074}
1075
1076static
1077void rpc_task_set_client(struct rpc_task *task, struct rpc_clnt *clnt)
1078{
Thiago Rafael Becker023859c2021-10-20 18:04:28 -03001079 rpc_task_set_transport(task, clnt);
1080 task->tk_client = clnt;
1081 refcount_inc(&clnt->cl_count);
1082 if (clnt->cl_softrtry)
1083 task->tk_flags |= RPC_TASK_SOFT;
1084 if (clnt->cl_softerr)
1085 task->tk_flags |= RPC_TASK_TIMEOUT;
1086 if (clnt->cl_noretranstimeo)
1087 task->tk_flags |= RPC_TASK_NO_RETRANS_TIMEOUT;
1088 if (atomic_read(&clnt->cl_swapper))
1089 task->tk_flags |= RPC_TASK_SWAPPER;
1090 /* Add to the client's list of all tasks */
1091 spin_lock(&clnt->cl_lock);
1092 list_add_tail(&task->tk_task, &clnt->cl_tasks);
1093 spin_unlock(&clnt->cl_lock);
Trond Myklebust58f96122010-07-31 14:29:08 -04001094}
1095
1096static void
1097rpc_task_set_rpc_message(struct rpc_task *task, const struct rpc_message *msg)
1098{
1099 if (msg != NULL) {
1100 task->tk_msg.rpc_proc = msg->rpc_proc;
1101 task->tk_msg.rpc_argp = msg->rpc_argp;
1102 task->tk_msg.rpc_resp = msg->rpc_resp;
Trond Myklebust7eac5262020-02-07 19:11:12 -05001103 task->tk_msg.rpc_cred = msg->rpc_cred;
1104 if (!(task->tk_flags & RPC_TASK_CRED_NOREF))
1105 get_cred(task->tk_msg.rpc_cred);
Trond Myklebust58f96122010-07-31 14:29:08 -04001106 }
1107}
1108
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109/*
1110 * Default callback for async RPC calls
1111 */
1112static void
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001113rpc_default_callback(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114{
1115}
1116
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001117static const struct rpc_call_ops rpc_default_ops = {
1118 .rpc_call_done = rpc_default_callback,
1119};
1120
Trond Myklebustc970aa82007-07-14 15:39:59 -04001121/**
1122 * rpc_run_task - Allocate a new RPC task, then run rpc_execute against it
1123 * @task_setup_data: pointer to task initialisation data
1124 */
1125struct rpc_task *rpc_run_task(const struct rpc_task_setup *task_setup_data)
Trond Myklebust6e5b70e2007-06-12 10:02:37 -04001126{
Trond Myklebust19445b92010-04-16 16:41:10 -04001127 struct rpc_task *task;
Trond Myklebust6e5b70e2007-06-12 10:02:37 -04001128
Trond Myklebust84115e12007-07-14 15:39:59 -04001129 task = rpc_new_task(task_setup_data);
Trond Myklebust6e5b70e2007-06-12 10:02:37 -04001130
Trond Myklebust263fb9c2020-02-07 19:16:34 -05001131 if (!RPC_IS_ASYNC(task))
1132 task->tk_flags |= RPC_TASK_CRED_NOREF;
1133
Trond Myklebust58f96122010-07-31 14:29:08 -04001134 rpc_task_set_client(task, task_setup_data->rpc_client);
1135 rpc_task_set_rpc_message(task, task_setup_data->rpc_message);
1136
Trond Myklebust58f96122010-07-31 14:29:08 -04001137 if (task->tk_action == NULL)
1138 rpc_call_start(task);
1139
Trond Myklebust6e5b70e2007-06-12 10:02:37 -04001140 atomic_inc(&task->tk_count);
1141 rpc_execute(task);
Trond Myklebust19445b92010-04-16 16:41:10 -04001142 return task;
Trond Myklebust6e5b70e2007-06-12 10:02:37 -04001143}
Trond Myklebustc970aa82007-07-14 15:39:59 -04001144EXPORT_SYMBOL_GPL(rpc_run_task);
Trond Myklebust6e5b70e2007-06-12 10:02:37 -04001145
1146/**
1147 * rpc_call_sync - Perform a synchronous RPC call
1148 * @clnt: pointer to RPC client
1149 * @msg: RPC call parameters
1150 * @flags: RPC call flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 */
Trond Myklebustcbc20052008-02-14 11:11:30 -05001152int rpc_call_sync(struct rpc_clnt *clnt, const struct rpc_message *msg, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153{
1154 struct rpc_task *task;
Trond Myklebust84115e12007-07-14 15:39:59 -04001155 struct rpc_task_setup task_setup_data = {
1156 .rpc_client = clnt,
1157 .rpc_message = msg,
1158 .callback_ops = &rpc_default_ops,
1159 .flags = flags,
1160 };
Trond Myklebust6e5b70e2007-06-12 10:02:37 -04001161 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
Weston Andros Adamson50d2bdb2012-11-01 16:04:40 -04001163 WARN_ON_ONCE(flags & RPC_TASK_ASYNC);
1164 if (flags & RPC_TASK_ASYNC) {
1165 rpc_release_calldata(task_setup_data.callback_ops,
1166 task_setup_data.callback_data);
1167 return -EINVAL;
1168 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
Trond Myklebustc970aa82007-07-14 15:39:59 -04001170 task = rpc_run_task(&task_setup_data);
Trond Myklebust6e5b70e2007-06-12 10:02:37 -04001171 if (IS_ERR(task))
1172 return PTR_ERR(task);
Trond Myklebuste60859a2006-01-03 09:55:10 +01001173 status = task->tk_status;
Trond Myklebustbde8f002007-01-24 11:54:53 -08001174 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 return status;
1176}
Trond Myklebuste8914c62007-07-14 15:39:59 -04001177EXPORT_SYMBOL_GPL(rpc_call_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
Trond Myklebust6e5b70e2007-06-12 10:02:37 -04001179/**
1180 * rpc_call_async - Perform an asynchronous RPC call
1181 * @clnt: pointer to RPC client
1182 * @msg: RPC call parameters
1183 * @flags: RPC call flags
Randy Dunlap65b6e422008-02-13 15:03:23 -08001184 * @tk_ops: RPC call ops
Trond Myklebust6e5b70e2007-06-12 10:02:37 -04001185 * @data: user call data
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 */
1187int
Trond Myklebustcbc20052008-02-14 11:11:30 -05001188rpc_call_async(struct rpc_clnt *clnt, const struct rpc_message *msg, int flags,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001189 const struct rpc_call_ops *tk_ops, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190{
1191 struct rpc_task *task;
Trond Myklebust84115e12007-07-14 15:39:59 -04001192 struct rpc_task_setup task_setup_data = {
1193 .rpc_client = clnt,
1194 .rpc_message = msg,
1195 .callback_ops = tk_ops,
1196 .callback_data = data,
1197 .flags = flags|RPC_TASK_ASYNC,
1198 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
Trond Myklebustc970aa82007-07-14 15:39:59 -04001200 task = rpc_run_task(&task_setup_data);
Trond Myklebust6e5b70e2007-06-12 10:02:37 -04001201 if (IS_ERR(task))
1202 return PTR_ERR(task);
1203 rpc_put_task(task);
1204 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205}
Trond Myklebuste8914c62007-07-14 15:39:59 -04001206EXPORT_SYMBOL_GPL(rpc_call_async);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001208#if defined(CONFIG_SUNRPC_BACKCHANNEL)
Trond Myklebust477687e2019-03-05 07:30:48 -05001209static void call_bc_encode(struct rpc_task *task);
1210
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001211/**
1212 * rpc_run_bc_task - Allocate a new RPC task for backchannel use, then run
1213 * rpc_execute against it
Jaswinder Singh Rajput7a73fdd2009-09-24 14:58:42 -04001214 * @req: RPC request
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001215 */
Trond Myklebust0f419792015-06-01 22:59:08 -04001216struct rpc_task *rpc_run_bc_task(struct rpc_rqst *req)
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001217{
1218 struct rpc_task *task;
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001219 struct rpc_task_setup task_setup_data = {
Trond Myklebust0f419792015-06-01 22:59:08 -04001220 .callback_ops = &rpc_default_ops,
Trond Myklebust762e4e62018-08-24 16:28:28 -04001221 .flags = RPC_TASK_SOFTCONN |
1222 RPC_TASK_NO_RETRANS_TIMEOUT,
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001223 };
1224
1225 dprintk("RPC: rpc_run_bc_task req= %p\n", req);
1226 /*
1227 * Create an rpc_task to send the data
1228 */
1229 task = rpc_new_task(&task_setup_data);
Trond Myklebust902c5882018-09-01 17:21:01 -04001230 xprt_init_bc_request(req, task);
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001231
Trond Myklebust477687e2019-03-05 07:30:48 -05001232 task->tk_action = call_bc_encode;
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001233 atomic_inc(&task->tk_count);
Weston Andros Adamson9a6478f2012-10-23 10:43:27 -04001234 WARN_ON_ONCE(atomic_read(&task->tk_count) != 2);
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001235 rpc_execute(task);
1236
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001237 dprintk("RPC: rpc_run_bc_task: task= %p\n", task);
1238 return task;
1239}
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001240#endif /* CONFIG_SUNRPC_BACKCHANNEL */
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001241
Chuck Levercf500ba2019-02-11 11:25:20 -05001242/**
1243 * rpc_prepare_reply_pages - Prepare to receive a reply data payload into pages
1244 * @req: RPC request to prepare
1245 * @pages: vector of struct page pointers
1246 * @base: offset in first page where receive should start, in bytes
1247 * @len: expected size of the upper layer data payload, in bytes
1248 * @hdrsize: expected size of upper layer reply header, in XDR words
1249 *
1250 */
1251void rpc_prepare_reply_pages(struct rpc_rqst *req, struct page **pages,
1252 unsigned int base, unsigned int len,
1253 unsigned int hdrsize)
1254{
Trond Myklebust9ed5af262020-11-21 20:46:18 -05001255 hdrsize += RPC_REPHDRSIZE + req->rq_cred->cr_auth->au_ralign;
Chuck Lever02ef04e2019-02-11 11:25:25 -05001256
Chuck Levercf500ba2019-02-11 11:25:20 -05001257 xdr_inline_pages(&req->rq_rcv_buf, hdrsize << 2, pages, base, len);
Chuck Leverc509f15a2020-05-12 17:13:28 -04001258 trace_rpc_xdr_reply_pages(req->rq_task, &req->rq_rcv_buf);
Chuck Levercf500ba2019-02-11 11:25:20 -05001259}
1260EXPORT_SYMBOL_GPL(rpc_prepare_reply_pages);
1261
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262void
Trond Myklebust77de2c52007-10-25 18:40:21 -04001263rpc_call_start(struct rpc_task *task)
1264{
1265 task->tk_action = call_start;
1266}
1267EXPORT_SYMBOL_GPL(rpc_call_start);
1268
Chuck Levered394402006-08-22 20:06:17 -04001269/**
1270 * rpc_peeraddr - extract remote peer address from clnt's xprt
1271 * @clnt: RPC client structure
1272 * @buf: target buffer
Randy Dunlap65b6e422008-02-13 15:03:23 -08001273 * @bufsize: length of target buffer
Chuck Levered394402006-08-22 20:06:17 -04001274 *
1275 * Returns the number of bytes that are actually in the stored address.
1276 */
1277size_t rpc_peeraddr(struct rpc_clnt *clnt, struct sockaddr *buf, size_t bufsize)
1278{
1279 size_t bytes;
Trond Myklebust2446ab62012-03-01 17:00:56 -05001280 struct rpc_xprt *xprt;
Chuck Levered394402006-08-22 20:06:17 -04001281
Trond Myklebust2446ab62012-03-01 17:00:56 -05001282 rcu_read_lock();
1283 xprt = rcu_dereference(clnt->cl_xprt);
1284
1285 bytes = xprt->addrlen;
Chuck Levered394402006-08-22 20:06:17 -04001286 if (bytes > bufsize)
1287 bytes = bufsize;
Trond Myklebust2446ab62012-03-01 17:00:56 -05001288 memcpy(buf, &xprt->addr, bytes);
1289 rcu_read_unlock();
1290
1291 return bytes;
Chuck Levered394402006-08-22 20:06:17 -04001292}
Chuck Leverb86acd52006-08-22 20:06:22 -04001293EXPORT_SYMBOL_GPL(rpc_peeraddr);
Chuck Levered394402006-08-22 20:06:17 -04001294
Chuck Leverf425eba2006-08-22 20:06:18 -04001295/**
1296 * rpc_peeraddr2str - return remote peer address in printable format
1297 * @clnt: RPC client structure
1298 * @format: address format
1299 *
Trond Myklebust2446ab62012-03-01 17:00:56 -05001300 * NB: the lifetime of the memory referenced by the returned pointer is
1301 * the same as the rpc_xprt itself. As long as the caller uses this
1302 * pointer, it must hold the RCU read lock.
Chuck Leverf425eba2006-08-22 20:06:18 -04001303 */
Chuck Leverb454ae92008-01-07 18:34:48 -05001304const char *rpc_peeraddr2str(struct rpc_clnt *clnt,
1305 enum rpc_display_format_t format)
Chuck Leverf425eba2006-08-22 20:06:18 -04001306{
Trond Myklebust2446ab62012-03-01 17:00:56 -05001307 struct rpc_xprt *xprt;
1308
1309 xprt = rcu_dereference(clnt->cl_xprt);
Chuck Lever7559c7a2006-12-05 16:35:37 -05001310
1311 if (xprt->address_strings[format] != NULL)
1312 return xprt->address_strings[format];
1313 else
1314 return "unprintable";
Chuck Leverf425eba2006-08-22 20:06:18 -04001315}
Chuck Leverb86acd52006-08-22 20:06:22 -04001316EXPORT_SYMBOL_GPL(rpc_peeraddr2str);
Chuck Leverf425eba2006-08-22 20:06:18 -04001317
Chuck Lever2e738fd2012-03-01 17:01:14 -05001318static const struct sockaddr_in rpc_inaddr_loopback = {
1319 .sin_family = AF_INET,
1320 .sin_addr.s_addr = htonl(INADDR_ANY),
1321};
1322
1323static const struct sockaddr_in6 rpc_in6addr_loopback = {
1324 .sin6_family = AF_INET6,
1325 .sin6_addr = IN6ADDR_ANY_INIT,
1326};
1327
1328/*
1329 * Try a getsockname() on a connected datagram socket. Using a
1330 * connected datagram socket prevents leaving a socket in TIME_WAIT.
1331 * This conserves the ephemeral port number space.
1332 *
1333 * Returns zero and fills in "buf" if successful; otherwise, a
1334 * negative errno is returned.
1335 */
1336static int rpc_sockname(struct net *net, struct sockaddr *sap, size_t salen,
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001337 struct sockaddr *buf)
Chuck Lever2e738fd2012-03-01 17:01:14 -05001338{
1339 struct socket *sock;
1340 int err;
1341
1342 err = __sock_create(net, sap->sa_family,
1343 SOCK_DGRAM, IPPROTO_UDP, &sock, 1);
1344 if (err < 0) {
1345 dprintk("RPC: can't create UDP socket (%d)\n", err);
1346 goto out;
1347 }
1348
1349 switch (sap->sa_family) {
1350 case AF_INET:
1351 err = kernel_bind(sock,
1352 (struct sockaddr *)&rpc_inaddr_loopback,
1353 sizeof(rpc_inaddr_loopback));
1354 break;
1355 case AF_INET6:
1356 err = kernel_bind(sock,
1357 (struct sockaddr *)&rpc_in6addr_loopback,
1358 sizeof(rpc_in6addr_loopback));
1359 break;
1360 default:
1361 err = -EAFNOSUPPORT;
1362 goto out;
1363 }
1364 if (err < 0) {
1365 dprintk("RPC: can't bind UDP socket (%d)\n", err);
1366 goto out_release;
1367 }
1368
1369 err = kernel_connect(sock, sap, salen, 0);
1370 if (err < 0) {
1371 dprintk("RPC: can't connect UDP socket (%d)\n", err);
1372 goto out_release;
1373 }
1374
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001375 err = kernel_getsockname(sock, buf);
Chuck Lever2e738fd2012-03-01 17:01:14 -05001376 if (err < 0) {
1377 dprintk("RPC: getsockname failed (%d)\n", err);
1378 goto out_release;
1379 }
1380
1381 err = 0;
1382 if (buf->sa_family == AF_INET6) {
1383 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)buf;
1384 sin6->sin6_scope_id = 0;
1385 }
1386 dprintk("RPC: %s succeeded\n", __func__);
1387
1388out_release:
1389 sock_release(sock);
1390out:
1391 return err;
1392}
1393
1394/*
1395 * Scraping a connected socket failed, so we don't have a useable
1396 * local address. Fallback: generate an address that will prevent
1397 * the server from calling us back.
1398 *
1399 * Returns zero and fills in "buf" if successful; otherwise, a
1400 * negative errno is returned.
1401 */
1402static int rpc_anyaddr(int family, struct sockaddr *buf, size_t buflen)
1403{
1404 switch (family) {
1405 case AF_INET:
1406 if (buflen < sizeof(rpc_inaddr_loopback))
1407 return -EINVAL;
1408 memcpy(buf, &rpc_inaddr_loopback,
1409 sizeof(rpc_inaddr_loopback));
1410 break;
1411 case AF_INET6:
1412 if (buflen < sizeof(rpc_in6addr_loopback))
1413 return -EINVAL;
1414 memcpy(buf, &rpc_in6addr_loopback,
1415 sizeof(rpc_in6addr_loopback));
Trond Myklebust0b161e62015-12-30 18:14:06 -05001416 break;
Chuck Lever2e738fd2012-03-01 17:01:14 -05001417 default:
1418 dprintk("RPC: %s: address family not supported\n",
1419 __func__);
1420 return -EAFNOSUPPORT;
1421 }
1422 dprintk("RPC: %s: succeeded\n", __func__);
1423 return 0;
1424}
1425
1426/**
1427 * rpc_localaddr - discover local endpoint address for an RPC client
1428 * @clnt: RPC client structure
1429 * @buf: target buffer
1430 * @buflen: size of target buffer, in bytes
1431 *
1432 * Returns zero and fills in "buf" and "buflen" if successful;
1433 * otherwise, a negative errno is returned.
1434 *
1435 * This works even if the underlying transport is not currently connected,
1436 * or if the upper layer never previously provided a source address.
1437 *
1438 * The result of this function call is transient: multiple calls in
1439 * succession may give different results, depending on how local
1440 * networking configuration changes over time.
1441 */
1442int rpc_localaddr(struct rpc_clnt *clnt, struct sockaddr *buf, size_t buflen)
1443{
1444 struct sockaddr_storage address;
1445 struct sockaddr *sap = (struct sockaddr *)&address;
1446 struct rpc_xprt *xprt;
1447 struct net *net;
1448 size_t salen;
1449 int err;
1450
1451 rcu_read_lock();
1452 xprt = rcu_dereference(clnt->cl_xprt);
1453 salen = xprt->addrlen;
1454 memcpy(sap, &xprt->addr, salen);
1455 net = get_net(xprt->xprt_net);
1456 rcu_read_unlock();
1457
1458 rpc_set_port(sap, 0);
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001459 err = rpc_sockname(net, sap, salen, buf);
Chuck Lever2e738fd2012-03-01 17:01:14 -05001460 put_net(net);
1461 if (err != 0)
1462 /* Couldn't discover local address, return ANYADDR */
1463 return rpc_anyaddr(sap->sa_family, buf, buflen);
1464 return 0;
1465}
1466EXPORT_SYMBOL_GPL(rpc_localaddr);
1467
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468void
1469rpc_setbufsize(struct rpc_clnt *clnt, unsigned int sndsize, unsigned int rcvsize)
1470{
Trond Myklebust2446ab62012-03-01 17:00:56 -05001471 struct rpc_xprt *xprt;
1472
1473 rcu_read_lock();
1474 xprt = rcu_dereference(clnt->cl_xprt);
Chuck Lever470056c2005-08-25 16:25:56 -07001475 if (xprt->ops->set_buffer_size)
1476 xprt->ops->set_buffer_size(xprt, sndsize, rcvsize);
Trond Myklebust2446ab62012-03-01 17:00:56 -05001477 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478}
Trond Myklebuste8914c62007-07-14 15:39:59 -04001479EXPORT_SYMBOL_GPL(rpc_setbufsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
Trond Myklebust2446ab62012-03-01 17:00:56 -05001481/**
Trond Myklebust2446ab62012-03-01 17:00:56 -05001482 * rpc_net_ns - Get the network namespace for this RPC client
1483 * @clnt: RPC client to query
1484 *
1485 */
1486struct net *rpc_net_ns(struct rpc_clnt *clnt)
1487{
1488 struct net *ret;
1489
1490 rcu_read_lock();
1491 ret = rcu_dereference(clnt->cl_xprt)->xprt_net;
1492 rcu_read_unlock();
1493 return ret;
1494}
1495EXPORT_SYMBOL_GPL(rpc_net_ns);
1496
1497/**
1498 * rpc_max_payload - Get maximum payload size for a transport, in bytes
1499 * @clnt: RPC client to query
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 *
1501 * For stream transports, this is one RPC record fragment (see RFC
1502 * 1831), as we don't support multi-record requests yet. For datagram
1503 * transports, this is the size of an IP packet minus the IP, UDP, and
1504 * RPC header sizes.
1505 */
1506size_t rpc_max_payload(struct rpc_clnt *clnt)
1507{
Trond Myklebust2446ab62012-03-01 17:00:56 -05001508 size_t ret;
1509
1510 rcu_read_lock();
1511 ret = rcu_dereference(clnt->cl_xprt)->max_payload;
1512 rcu_read_unlock();
1513 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514}
Chuck Leverb86acd52006-08-22 20:06:22 -04001515EXPORT_SYMBOL_GPL(rpc_max_payload);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516
Chuck Lever35f5a422006-01-03 09:55:50 +01001517/**
Chuck Lever6b26cc82016-05-02 14:40:40 -04001518 * rpc_max_bc_payload - Get maximum backchannel payload size, in bytes
1519 * @clnt: RPC client to query
1520 */
1521size_t rpc_max_bc_payload(struct rpc_clnt *clnt)
1522{
1523 struct rpc_xprt *xprt;
1524 size_t ret;
1525
1526 rcu_read_lock();
1527 xprt = rcu_dereference(clnt->cl_xprt);
1528 ret = xprt->ops->bc_maxpayload(xprt);
1529 rcu_read_unlock();
1530 return ret;
1531}
1532EXPORT_SYMBOL_GPL(rpc_max_bc_payload);
1533
Trond Myklebust7402a4f2019-07-16 13:51:29 -04001534unsigned int rpc_num_bc_slots(struct rpc_clnt *clnt)
1535{
1536 struct rpc_xprt *xprt;
1537 unsigned int ret;
1538
1539 rcu_read_lock();
1540 xprt = rcu_dereference(clnt->cl_xprt);
1541 ret = xprt->ops->bc_num_slots(xprt);
1542 rcu_read_unlock();
1543 return ret;
1544}
1545EXPORT_SYMBOL_GPL(rpc_num_bc_slots);
1546
Chuck Lever6b26cc82016-05-02 14:40:40 -04001547/**
Chuck Lever35f5a422006-01-03 09:55:50 +01001548 * rpc_force_rebind - force transport to check that remote port is unchanged
1549 * @clnt: client to rebind
1550 *
1551 */
1552void rpc_force_rebind(struct rpc_clnt *clnt)
1553{
Trond Myklebust2446ab62012-03-01 17:00:56 -05001554 if (clnt->cl_autobind) {
1555 rcu_read_lock();
1556 xprt_clear_bound(rcu_dereference(clnt->cl_xprt));
1557 rcu_read_unlock();
1558 }
Chuck Lever35f5a422006-01-03 09:55:50 +01001559}
Chuck Leverb86acd52006-08-22 20:06:22 -04001560EXPORT_SYMBOL_GPL(rpc_force_rebind);
Chuck Lever35f5a422006-01-03 09:55:50 +01001561
Trond Myklebust9e6fa0b2019-04-07 13:58:45 -04001562static int
1563__rpc_restart_call(struct rpc_task *task, void (*action)(struct rpc_task *))
Andy Adamsonaae20062009-04-01 09:22:40 -04001564{
Trond Myklebust494314c2014-03-20 12:59:09 -04001565 task->tk_status = 0;
Trond Myklebust5ad64b32019-04-07 13:58:54 -04001566 task->tk_rpc_status = 0;
Trond Myklebust9e6fa0b2019-04-07 13:58:45 -04001567 task->tk_action = action;
Trond Myklebustf1f88fc2010-07-31 14:29:07 -04001568 return 1;
Andy Adamsonaae20062009-04-01 09:22:40 -04001569}
Andy Adamsonaae20062009-04-01 09:22:40 -04001570
1571/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 * Restart an (async) RPC call. Usually called from within the
1573 * exit handler.
1574 */
Trond Myklebustf1f88fc2010-07-31 14:29:07 -04001575int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576rpc_restart_call(struct rpc_task *task)
1577{
Trond Myklebust9e6fa0b2019-04-07 13:58:45 -04001578 return __rpc_restart_call(task, call_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579}
Trond Myklebuste8914c62007-07-14 15:39:59 -04001580EXPORT_SYMBOL_GPL(rpc_restart_call);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581
Trond Myklebust9e6fa0b2019-04-07 13:58:45 -04001582/*
1583 * Restart an (async) RPC call from the call_prepare state.
1584 * Usually called from within the exit handler.
1585 */
1586int
1587rpc_restart_call_prepare(struct rpc_task *task)
1588{
1589 if (task->tk_ops->rpc_call_prepare != NULL)
1590 return __rpc_restart_call(task, rpc_prepare_task);
1591 return rpc_restart_call(task);
1592}
1593EXPORT_SYMBOL_GPL(rpc_restart_call_prepare);
1594
Jeff Laytonb4b9d2c2014-11-26 14:44:43 -05001595const char
1596*rpc_proc_name(const struct rpc_task *task)
Chuck Lever3748f1e2008-05-21 17:09:12 -04001597{
1598 const struct rpc_procinfo *proc = task->tk_msg.rpc_proc;
1599
1600 if (proc) {
1601 if (proc->p_name)
1602 return proc->p_name;
1603 else
1604 return "NULL";
1605 } else
1606 return "no proc";
1607}
Chuck Lever3748f1e2008-05-21 17:09:12 -04001608
Trond Myklebust5ad64b32019-04-07 13:58:54 -04001609static void
1610__rpc_call_rpcerror(struct rpc_task *task, int tk_status, int rpc_status)
1611{
Chuck Lever0125ecb2020-05-12 17:13:23 -04001612 trace_rpc_call_rpcerror(task, tk_status, rpc_status);
Trond Myklebust5ad64b32019-04-07 13:58:54 -04001613 task->tk_rpc_status = rpc_status;
1614 rpc_exit(task, tk_status);
1615}
1616
1617static void
1618rpc_call_rpcerror(struct rpc_task *task, int status)
1619{
1620 __rpc_call_rpcerror(task, status, status);
1621}
1622
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623/*
1624 * 0. Initial state
1625 *
1626 * Other FSM states can be visited zero or more times, but
1627 * this state is visited exactly once for each RPC.
1628 */
1629static void
1630call_start(struct rpc_task *task)
1631{
1632 struct rpc_clnt *clnt = task->tk_client;
Christoph Hellwig1c5876d2017-05-08 23:27:10 +02001633 int idx = task->tk_msg.rpc_proc->p_statidx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634
Chuck Leverc435da62017-11-03 13:46:14 -04001635 trace_rpc_request(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636
Christoph Hellwig1c5876d2017-05-08 23:27:10 +02001637 /* Increment call count (version might not be valid for ping) */
1638 if (clnt->cl_program->version[clnt->cl_vers])
1639 clnt->cl_program->version[clnt->cl_vers]->counts[idx]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 clnt->cl_stats->rpccnt++;
1641 task->tk_action = call_reserve;
Bill Baker0f90be12018-06-19 16:24:58 -05001642 rpc_task_set_transport(task, clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643}
1644
1645/*
1646 * 1. Reserve an RPC call slot
1647 */
1648static void
1649call_reserve(struct rpc_task *task)
1650{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 task->tk_status = 0;
1652 task->tk_action = call_reserveresult;
1653 xprt_reserve(task);
1654}
1655
Trond Myklebustba60eb22013-04-14 10:49:37 -04001656static void call_retry_reserve(struct rpc_task *task);
1657
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658/*
1659 * 1b. Grok the result of xprt_reserve()
1660 */
1661static void
1662call_reserveresult(struct rpc_task *task)
1663{
1664 int status = task->tk_status;
1665
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 /*
1667 * After a call to xprt_reserve(), we must have either
1668 * a request slot or else an error status.
1669 */
1670 task->tk_status = 0;
1671 if (status >= 0) {
1672 if (task->tk_rqstp) {
J. Bruce Fieldsf2d47d02010-09-12 19:55:25 -04001673 task->tk_action = call_refresh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 return;
1675 }
1676
Trond Myklebust5ad64b32019-04-07 13:58:54 -04001677 rpc_call_rpcerror(task, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 return;
1679 }
1680
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 switch (status) {
Trond Myklebust1afeaf52012-05-19 12:12:53 -04001682 case -ENOMEM:
1683 rpc_delay(task, HZ >> 2);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001684 fallthrough;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 case -EAGAIN: /* woken up; retry */
Trond Myklebustba60eb22013-04-14 10:49:37 -04001686 task->tk_action = call_retry_reserve;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 default:
Chuck Lever5cd8b0d2019-10-09 12:58:08 -04001689 rpc_call_rpcerror(task, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691}
1692
1693/*
Trond Myklebustba60eb22013-04-14 10:49:37 -04001694 * 1c. Retry reserving an RPC call slot
1695 */
1696static void
1697call_retry_reserve(struct rpc_task *task)
1698{
Trond Myklebustba60eb22013-04-14 10:49:37 -04001699 task->tk_status = 0;
1700 task->tk_action = call_reserveresult;
1701 xprt_retry_reserve(task);
1702}
1703
1704/*
J. Bruce Fields55576242010-09-12 19:55:25 -04001705 * 2. Bind and/or refresh the credentials
1706 */
1707static void
1708call_refresh(struct rpc_task *task)
1709{
J. Bruce Fields55576242010-09-12 19:55:25 -04001710 task->tk_action = call_refreshresult;
1711 task->tk_status = 0;
1712 task->tk_client->cl_stats->rpcauthrefresh++;
1713 rpcauth_refreshcred(task);
1714}
1715
1716/*
1717 * 2a. Process the results of a credential refresh
1718 */
1719static void
1720call_refreshresult(struct rpc_task *task)
1721{
1722 int status = task->tk_status;
1723
J. Bruce Fields55576242010-09-12 19:55:25 -04001724 task->tk_status = 0;
Trond Myklebust5fc43972010-11-20 11:13:31 -05001725 task->tk_action = call_refresh;
J. Bruce Fields55576242010-09-12 19:55:25 -04001726 switch (status) {
Trond Myklebust5fc43972010-11-20 11:13:31 -05001727 case 0:
Weston Andros Adamson6ff33b72013-12-17 12:16:11 -05001728 if (rpcauth_uptodatecred(task)) {
Trond Myklebust5fc43972010-11-20 11:13:31 -05001729 task->tk_action = call_allocate;
Weston Andros Adamson6ff33b72013-12-17 12:16:11 -05001730 return;
1731 }
1732 /* Use rate-limiting and a max number of retries if refresh
1733 * had status 0 but failed to update the cred.
1734 */
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001735 fallthrough;
J. Bruce Fields55576242010-09-12 19:55:25 -04001736 case -ETIMEDOUT:
1737 rpc_delay(task, 3*HZ);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001738 fallthrough;
Trond Myklebust5fc43972010-11-20 11:13:31 -05001739 case -EAGAIN:
1740 status = -EACCES;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001741 fallthrough;
Andy Adamsonf1ff0c22013-08-14 11:59:13 -04001742 case -EKEYEXPIRED:
Trond Myklebust5fc43972010-11-20 11:13:31 -05001743 if (!task->tk_cred_retry)
1744 break;
1745 task->tk_cred_retry--;
Chuck Lever7c8099f2020-07-08 16:10:03 -04001746 trace_rpc_retry_refresh_status(task);
Trond Myklebust5fc43972010-11-20 11:13:31 -05001747 return;
J. Bruce Fields55576242010-09-12 19:55:25 -04001748 }
Chuck Lever7c8099f2020-07-08 16:10:03 -04001749 trace_rpc_refresh_status(task);
Trond Myklebust5ad64b32019-04-07 13:58:54 -04001750 rpc_call_rpcerror(task, status);
J. Bruce Fields55576242010-09-12 19:55:25 -04001751}
1752
1753/*
1754 * 2b. Allocate the buffer. For details, see sched.c:rpc_malloc.
Chuck Lever02107142006-01-03 09:55:49 +01001755 * (Note: buffer memory is freed in xprt_release).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 */
1757static void
1758call_allocate(struct rpc_task *task)
1759{
Chuck Lever2c94b8e2019-02-11 11:25:41 -05001760 const struct rpc_auth *auth = task->tk_rqstp->rq_cred->cr_auth;
Chuck Lever02107142006-01-03 09:55:49 +01001761 struct rpc_rqst *req = task->tk_rqstp;
Trond Myklebusta4f08352013-01-08 09:10:21 -05001762 struct rpc_xprt *xprt = req->rq_xprt;
Christoph Hellwig499b4982017-05-12 15:36:49 +02001763 const struct rpc_procinfo *proc = task->tk_msg.rpc_proc;
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04001764 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765
Chuck Lever2bea90d2007-03-29 16:47:53 -04001766 task->tk_status = 0;
Trond Myklebust762e4e62018-08-24 16:28:28 -04001767 task->tk_action = call_encode;
Chuck Lever2bea90d2007-03-29 16:47:53 -04001768
Trond Myklebustaf6b61d2019-04-11 15:16:52 -04001769 if (req->rq_buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 return;
1771
Chuck Lever2bea90d2007-03-29 16:47:53 -04001772 if (proc->p_proc != 0) {
1773 BUG_ON(proc->p_arglen == 0);
1774 if (proc->p_decode != NULL)
1775 BUG_ON(proc->p_replen == 0);
1776 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777
Chuck Lever2bea90d2007-03-29 16:47:53 -04001778 /*
1779 * Calculate the size (in quads) of the RPC call
1780 * and reply headers, and convert both values
1781 * to byte sizes.
1782 */
Chuck Lever2c94b8e2019-02-11 11:25:41 -05001783 req->rq_callsize = RPC_CALLHDRSIZE + (auth->au_cslack << 1) +
1784 proc->p_arglen;
Chuck Lever2bea90d2007-03-29 16:47:53 -04001785 req->rq_callsize <<= 2;
Trond Myklebust51314962019-03-15 12:55:59 -04001786 /*
1787 * Note: the reply buffer must at minimum allocate enough space
1788 * for the 'struct accepted_reply' from RFC5531.
1789 */
1790 req->rq_rcvsize = RPC_REPHDRSIZE + auth->au_rslack + \
1791 max_t(size_t, proc->p_replen, 2);
Chuck Lever2bea90d2007-03-29 16:47:53 -04001792 req->rq_rcvsize <<= 2;
1793
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04001794 status = xprt->ops->buf_alloc(task);
Chuck Lever06e234c2020-07-08 16:09:11 -04001795 trace_rpc_buf_alloc(task, status);
Trond Myklebustaf6b61d2019-04-11 15:16:52 -04001796 if (status == 0)
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04001797 return;
1798 if (status != -ENOMEM) {
Trond Myklebust5ad64b32019-04-07 13:58:54 -04001799 rpc_call_rpcerror(task, status);
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04001800 return;
1801 }
Chuck Lever46121cf2007-01-31 12:14:08 -05001802
Trond Myklebust5afa91332011-06-17 10:14:59 -04001803 if (RPC_IS_ASYNC(task) || !fatal_signal_pending(current)) {
Trond Myklebustb6e9c712007-10-01 12:06:44 -04001804 task->tk_action = call_allocate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 rpc_delay(task, HZ>>4);
1806 return;
1807 }
1808
Trond Myklebust714fbc72019-09-12 08:06:51 -04001809 rpc_call_rpcerror(task, -ERESTARTSYS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810}
1811
Trond Myklebust7ebbbc62018-08-28 09:00:27 -04001812static int
Trond Myklebust940e3312005-11-09 21:45:24 -05001813rpc_task_need_encode(struct rpc_task *task)
1814{
Trond Myklebust762e4e62018-08-24 16:28:28 -04001815 return test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate) == 0 &&
1816 (!(task->tk_flags & RPC_TASK_SENT) ||
1817 !(task->tk_flags & RPC_TASK_NO_RETRANS_TIMEOUT) ||
1818 xprt_request_need_retransmit(task));
Trond Myklebust940e3312005-11-09 21:45:24 -05001819}
1820
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821static void
Chuck Leverb0e1c572008-05-21 17:09:19 -04001822rpc_xdr_encode(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 struct rpc_rqst *req = task->tk_rqstp;
Chuck Levere8680a22019-02-11 11:24:48 -05001825 struct xdr_stream xdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826
Chuck Leverb9c5bc02016-09-15 10:55:12 -04001827 xdr_buf_init(&req->rq_snd_buf,
1828 req->rq_buffer,
1829 req->rq_callsize);
1830 xdr_buf_init(&req->rq_rcv_buf,
Chuck Lever68778942016-09-15 10:55:37 -04001831 req->rq_rbuffer,
Chuck Leverb9c5bc02016-09-15 10:55:12 -04001832 req->rq_rcvsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833
Trond Myklebustcc204d02019-09-10 13:01:35 -04001834 req->rq_reply_bytes_recvd = 0;
Chuck Levere8680a22019-02-11 11:24:48 -05001835 req->rq_snd_buf.head[0].iov_len = 0;
1836 xdr_init_encode(&xdr, &req->rq_snd_buf,
1837 req->rq_snd_buf.head[0].iov_base, req);
Trond Myklebust75369082019-07-17 21:22:38 -04001838 xdr_free_bvec(&req->rq_snd_buf);
Chuck Levere8680a22019-02-11 11:24:48 -05001839 if (rpc_encode_header(task, &xdr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 return;
Chuck Leverb0e1c572008-05-21 17:09:19 -04001841
Chuck Levere8680a22019-02-11 11:24:48 -05001842 task->tk_status = rpcauth_wrap_req(task, &xdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843}
1844
1845/*
Trond Myklebust762e4e62018-08-24 16:28:28 -04001846 * 3. Encode arguments of an RPC call
1847 */
1848static void
1849call_encode(struct rpc_task *task)
1850{
1851 if (!rpc_task_need_encode(task))
1852 goto out;
Chuck Lever63870392020-07-08 16:09:37 -04001853
Trond Myklebustcc204d02019-09-10 13:01:35 -04001854 /* Dequeue task from the receive queue while we're encoding */
1855 xprt_request_dequeue_xprt(task);
Trond Myklebust762e4e62018-08-24 16:28:28 -04001856 /* Encode here so that rpcsec_gss can use correct sequence number. */
1857 rpc_xdr_encode(task);
1858 /* Did the encode result in an error condition? */
1859 if (task->tk_status != 0) {
1860 /* Was the error nonfatal? */
Trond Myklebust97b78ae2019-01-02 17:53:13 -05001861 switch (task->tk_status) {
1862 case -EAGAIN:
1863 case -ENOMEM:
Trond Myklebust762e4e62018-08-24 16:28:28 -04001864 rpc_delay(task, HZ >> 4);
Trond Myklebust97b78ae2019-01-02 17:53:13 -05001865 break;
1866 case -EKEYEXPIRED:
ZhangXiaoxu9c5948c2019-04-29 17:32:31 +08001867 if (!task->tk_cred_retry) {
1868 rpc_exit(task, task->tk_status);
1869 } else {
1870 task->tk_action = call_refresh;
1871 task->tk_cred_retry--;
Chuck Lever7c8099f2020-07-08 16:10:03 -04001872 trace_rpc_retry_refresh_status(task);
ZhangXiaoxu9c5948c2019-04-29 17:32:31 +08001873 }
Trond Myklebust97b78ae2019-01-02 17:53:13 -05001874 break;
1875 default:
Trond Myklebust5ad64b32019-04-07 13:58:54 -04001876 rpc_call_rpcerror(task, task->tk_status);
Trond Myklebust97b78ae2019-01-02 17:53:13 -05001877 }
Trond Myklebust762e4e62018-08-24 16:28:28 -04001878 return;
1879 }
1880
1881 /* Add task to reply queue before transmission to avoid races */
1882 if (rpc_reply_expected(task))
1883 xprt_request_enqueue_receive(task);
1884 xprt_request_enqueue_transmit(task);
1885out:
Trond Myklebustaf6b61d2019-04-11 15:16:52 -04001886 task->tk_action = call_transmit;
1887 /* Check that the connection is OK */
1888 if (!xprt_bound(task->tk_xprt))
1889 task->tk_action = call_bind;
1890 else if (!xprt_connected(task->tk_xprt))
1891 task->tk_action = call_connect;
Trond Myklebust762e4e62018-08-24 16:28:28 -04001892}
1893
1894/*
Trond Myklebust03e51d32019-03-10 11:17:13 -04001895 * Helpers to check if the task was already transmitted, and
1896 * to take action when that is the case.
1897 */
1898static bool
1899rpc_task_transmitted(struct rpc_task *task)
1900{
1901 return !test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate);
1902}
1903
1904static void
1905rpc_task_handle_transmitted(struct rpc_task *task)
1906{
1907 xprt_end_transmit(task);
1908 task->tk_action = call_transmit_status;
Trond Myklebust03e51d32019-03-10 11:17:13 -04001909}
1910
1911/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 * 4. Get the server port number if not yet set
1913 */
1914static void
1915call_bind(struct rpc_task *task)
1916{
Trond Myklebustad2368d2013-01-08 10:08:33 -05001917 struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918
Trond Myklebust03e51d32019-03-10 11:17:13 -04001919 if (rpc_task_transmitted(task)) {
1920 rpc_task_handle_transmitted(task);
1921 return;
1922 }
1923
Trond Myklebust009a82f2019-03-09 12:07:17 -05001924 if (xprt_bound(xprt)) {
1925 task->tk_action = call_connect;
Trond Myklebust009a82f2019-03-09 12:07:17 -05001926 return;
1927 }
1928
Trond Myklebust009a82f2019-03-09 12:07:17 -05001929 task->tk_action = call_bind_status;
Trond Myklebust4d6c6712019-03-10 12:22:39 -04001930 if (!xprt_prepare_transmit(task))
1931 return;
1932
Trond Myklebust009a82f2019-03-09 12:07:17 -05001933 xprt->ops->rpcbind(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934}
1935
1936/*
Chuck Leverda351872005-08-11 16:25:11 -04001937 * 4a. Sort out bind result
1938 */
1939static void
1940call_bind_status(struct rpc_task *task)
1941{
Trond Myklebustbd736ed2019-08-15 17:26:17 -04001942 struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
Chuck Lever906462a2007-09-11 18:00:47 -04001943 int status = -EIO;
Chuck Leverda351872005-08-11 16:25:11 -04001944
Trond Myklebust03e51d32019-03-10 11:17:13 -04001945 if (rpc_task_transmitted(task)) {
1946 rpc_task_handle_transmitted(task);
1947 return;
1948 }
1949
Trond Myklebustbd736ed2019-08-15 17:26:17 -04001950 if (task->tk_status >= 0)
1951 goto out_next;
1952 if (xprt_bound(xprt)) {
Chuck Leverda351872005-08-11 16:25:11 -04001953 task->tk_status = 0;
Trond Myklebustbd736ed2019-08-15 17:26:17 -04001954 goto out_next;
Chuck Leverda351872005-08-11 16:25:11 -04001955 }
1956
1957 switch (task->tk_status) {
Trond Myklebust381ba742008-07-07 12:18:53 -04001958 case -ENOMEM:
Trond Myklebust381ba742008-07-07 12:18:53 -04001959 rpc_delay(task, HZ >> 2);
Chuck Lever2429cbf2007-09-11 18:00:41 -04001960 goto retry_timeout;
Chuck Leverda351872005-08-11 16:25:11 -04001961 case -EACCES:
Chuck Lever42ebfc22020-07-08 16:10:14 -04001962 trace_rpcb_prog_unavail_err(task);
Chuck Leverb79dc8c2007-09-11 18:00:52 -04001963 /* fail immediately if this is an RPC ping */
1964 if (task->tk_msg.rpc_proc->p_proc == 0) {
1965 status = -EOPNOTSUPP;
1966 break;
1967 }
Trond Myklebust0b760112011-05-31 15:15:34 -04001968 if (task->tk_rebind_retry == 0)
1969 break;
1970 task->tk_rebind_retry--;
Chuck Leverea635a52005-10-06 23:12:58 -04001971 rpc_delay(task, 3*HZ);
Trond Myklebustda45828e2006-08-31 15:44:52 -04001972 goto retry_timeout;
Trond Myklebust80f455d2019-08-15 22:55:19 -04001973 case -ENOBUFS:
1974 rpc_delay(task, HZ >> 2);
1975 goto retry_timeout;
Trond Myklebust4d6c6712019-03-10 12:22:39 -04001976 case -EAGAIN:
1977 goto retry_timeout;
Chuck Leverda351872005-08-11 16:25:11 -04001978 case -ETIMEDOUT:
Chuck Lever42ebfc22020-07-08 16:10:14 -04001979 trace_rpcb_timeout_err(task);
Trond Myklebustda45828e2006-08-31 15:44:52 -04001980 goto retry_timeout;
Chuck Leverda351872005-08-11 16:25:11 -04001981 case -EPFNOSUPPORT:
Chuck Lever906462a2007-09-11 18:00:47 -04001982 /* server doesn't support any rpcbind version we know of */
Chuck Lever42ebfc22020-07-08 16:10:14 -04001983 trace_rpcb_bind_version_err(task);
Chuck Leverda351872005-08-11 16:25:11 -04001984 break;
1985 case -EPROTONOSUPPORT:
Chuck Lever42ebfc22020-07-08 16:10:14 -04001986 trace_rpcb_bind_version_err(task);
Trond Myklebustfdb63dc2014-03-17 12:57:31 -04001987 goto retry_timeout;
Chuck Lever012da152009-12-03 15:58:56 -05001988 case -ECONNREFUSED: /* connection problems */
1989 case -ECONNRESET:
Trond Myklebustdf277272013-12-31 13:11:43 -05001990 case -ECONNABORTED:
Chuck Lever012da152009-12-03 15:58:56 -05001991 case -ENOTCONN:
1992 case -EHOSTDOWN:
Trond Myklebusteb5b46f2017-11-30 07:21:33 -05001993 case -ENETDOWN:
Chuck Lever012da152009-12-03 15:58:56 -05001994 case -EHOSTUNREACH:
1995 case -ENETUNREACH:
1996 case -EPIPE:
Chuck Lever42ebfc22020-07-08 16:10:14 -04001997 trace_rpcb_unreachable_err(task);
Chuck Lever012da152009-12-03 15:58:56 -05001998 if (!RPC_IS_SOFTCONN(task)) {
1999 rpc_delay(task, 5*HZ);
2000 goto retry_timeout;
2001 }
2002 status = task->tk_status;
2003 break;
Chuck Leverda351872005-08-11 16:25:11 -04002004 default:
Chuck Lever42ebfc22020-07-08 16:10:14 -04002005 trace_rpcb_unrecognized_err(task);
Chuck Leverda351872005-08-11 16:25:11 -04002006 }
2007
Trond Myklebust5ad64b32019-04-07 13:58:54 -04002008 rpc_call_rpcerror(task, status);
Chuck Leverda351872005-08-11 16:25:11 -04002009 return;
Trond Myklebustbd736ed2019-08-15 17:26:17 -04002010out_next:
2011 task->tk_action = call_connect;
2012 return;
Trond Myklebustda45828e2006-08-31 15:44:52 -04002013retry_timeout:
Trond Myklebustfdb63dc2014-03-17 12:57:31 -04002014 task->tk_status = 0;
Trond Myklebust4d6c6712019-03-10 12:22:39 -04002015 task->tk_action = call_bind;
Trond Myklebustcea57782019-03-09 16:06:47 -05002016 rpc_check_timeout(task);
Chuck Leverda351872005-08-11 16:25:11 -04002017}
2018
2019/*
2020 * 4b. Connect to the RPC server
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 */
2022static void
2023call_connect(struct rpc_task *task)
2024{
Trond Myklebustad2368d2013-01-08 10:08:33 -05002025 struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026
Trond Myklebust03e51d32019-03-10 11:17:13 -04002027 if (rpc_task_transmitted(task)) {
2028 rpc_task_handle_transmitted(task);
2029 return;
2030 }
2031
Trond Myklebust009a82f2019-03-09 12:07:17 -05002032 if (xprt_connected(xprt)) {
2033 task->tk_action = call_transmit;
Trond Myklebust009a82f2019-03-09 12:07:17 -05002034 return;
2035 }
2036
Trond Myklebust009a82f2019-03-09 12:07:17 -05002037 task->tk_action = call_connect_status;
2038 if (task->tk_status < 0)
2039 return;
2040 if (task->tk_flags & RPC_TASK_NOCONNECT) {
Trond Myklebust5ad64b32019-04-07 13:58:54 -04002041 rpc_call_rpcerror(task, -ENOTCONN);
Trond Myklebust009a82f2019-03-09 12:07:17 -05002042 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 }
Trond Myklebust4d6c6712019-03-10 12:22:39 -04002044 if (!xprt_prepare_transmit(task))
2045 return;
Trond Myklebust009a82f2019-03-09 12:07:17 -05002046 xprt_connect(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047}
2048
2049/*
Chuck Leverda351872005-08-11 16:25:11 -04002050 * 4c. Sort out connect result
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 */
2052static void
2053call_connect_status(struct rpc_task *task)
2054{
Trond Myklebustbd736ed2019-08-15 17:26:17 -04002055 struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 struct rpc_clnt *clnt = task->tk_client;
2057 int status = task->tk_status;
2058
Trond Myklebust03e51d32019-03-10 11:17:13 -04002059 if (rpc_task_transmitted(task)) {
2060 rpc_task_handle_transmitted(task);
Trond Myklebust9bd11522018-11-30 12:48:47 -05002061 return;
2062 }
2063
Chuck Levere671edb2018-03-16 10:33:44 -04002064 trace_rpc_connect_status(task);
Trond Myklebustbd736ed2019-08-15 17:26:17 -04002065
2066 if (task->tk_status == 0) {
2067 clnt->cl_stats->netreconn++;
2068 goto out_next;
2069 }
2070 if (xprt_connected(xprt)) {
2071 task->tk_status = 0;
2072 goto out_next;
2073 }
2074
Trond Myklebust561ec162013-09-26 15:22:45 -04002075 task->tk_status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 switch (status) {
Trond Myklebust3ed5e2a2013-03-04 17:29:33 -05002077 case -ECONNREFUSED:
NeilBrownfd01b252017-08-18 17:12:51 +10002078 /* A positive refusal suggests a rebind is needed. */
2079 if (RPC_IS_SOFTCONN(task))
2080 break;
2081 if (clnt->cl_autobind) {
2082 rpc_force_rebind(clnt);
Trond Myklebust7b3fef82019-03-07 14:10:32 -05002083 goto out_retry;
NeilBrownfd01b252017-08-18 17:12:51 +10002084 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002085 fallthrough;
Trond Myklebust3ed5e2a2013-03-04 17:29:33 -05002086 case -ECONNRESET:
Trond Myklebustdf277272013-12-31 13:11:43 -05002087 case -ECONNABORTED:
Trond Myklebusteb5b46f2017-11-30 07:21:33 -05002088 case -ENETDOWN:
Trond Myklebust3ed5e2a2013-03-04 17:29:33 -05002089 case -ENETUNREACH:
Trond Myklebustdf277272013-12-31 13:11:43 -05002090 case -EHOSTUNREACH:
Trond Myklebust2fc193c2014-07-03 00:02:57 -04002091 case -EPIPE:
Chuck Leverb8457602019-12-23 10:28:44 -05002092 case -EPROTO:
NeilBrown2c2ee6d2016-11-23 14:44:58 +11002093 xprt_conditional_disconnect(task->tk_rqstp->rq_xprt,
2094 task->tk_rqstp->rq_connect_cookie);
Trond Myklebust3ed5e2a2013-03-04 17:29:33 -05002095 if (RPC_IS_SOFTCONN(task))
2096 break;
Steve Dickson1fa3e2e2014-03-20 11:23:03 -04002097 /* retry with existing socket, after a delay */
2098 rpc_delay(task, 3*HZ);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002099 fallthrough;
Trond Myklebust80f455d2019-08-15 22:55:19 -04002100 case -EADDRINUSE:
Trond Myklebust0445f922018-12-17 13:34:59 -05002101 case -ENOTCONN:
Trond Myklebust3ed5e2a2013-03-04 17:29:33 -05002102 case -EAGAIN:
Trond Myklebust485f2252014-03-17 12:51:44 -04002103 case -ETIMEDOUT:
Olga Kornievskaia6f0816932021-06-23 23:28:53 -04002104 if (!(task->tk_flags & RPC_TASK_NO_ROUND_ROBIN) &&
2105 (task->tk_flags & RPC_TASK_MOVEABLE) &&
2106 test_bit(XPRT_REMOVE, &xprt->state)) {
2107 struct rpc_xprt *saved = task->tk_xprt;
2108 struct rpc_xprt_switch *xps;
2109
2110 rcu_read_lock();
2111 xps = xprt_switch_get(rcu_dereference(clnt->cl_xpi.xpi_xpswitch));
2112 rcu_read_unlock();
2113 if (xps->xps_nxprts > 1) {
2114 long value;
2115
2116 xprt_release(task);
2117 value = atomic_long_dec_return(&xprt->queuelen);
2118 if (value == 0)
2119 rpc_xprt_switch_remove_xprt(xps, saved);
2120 xprt_put(saved);
2121 task->tk_xprt = NULL;
2122 task->tk_action = call_start;
2123 }
2124 xprt_switch_put(xps);
2125 if (!task->tk_xprt)
2126 return;
2127 }
Trond Myklebust7b3fef82019-03-07 14:10:32 -05002128 goto out_retry;
Trond Myklebust80f455d2019-08-15 22:55:19 -04002129 case -ENOBUFS:
2130 rpc_delay(task, HZ >> 2);
2131 goto out_retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 }
Trond Myklebust5ad64b32019-04-07 13:58:54 -04002133 rpc_call_rpcerror(task, status);
Trond Myklebust7b3fef82019-03-07 14:10:32 -05002134 return;
Trond Myklebustbd736ed2019-08-15 17:26:17 -04002135out_next:
2136 task->tk_action = call_transmit;
2137 return;
Trond Myklebust7b3fef82019-03-07 14:10:32 -05002138out_retry:
2139 /* Check for timeouts before looping back to call_bind */
2140 task->tk_action = call_bind;
2141 rpc_check_timeout(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142}
2143
2144/*
2145 * 5. Transmit the RPC request, and wait for reply
2146 */
2147static void
2148call_transmit(struct rpc_task *task)
2149{
Trond Myklebust03e51d32019-03-10 11:17:13 -04002150 if (rpc_task_transmitted(task)) {
2151 rpc_task_handle_transmitted(task);
2152 return;
2153 }
2154
Trond Myklebusted7dc972019-03-04 14:19:31 -05002155 task->tk_action = call_transmit_status;
Trond Myklebust009a82f2019-03-09 12:07:17 -05002156 if (!xprt_prepare_transmit(task))
2157 return;
2158 task->tk_status = 0;
Trond Myklebustc5445772018-09-03 23:39:27 -04002159 if (test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate)) {
Trond Myklebust009a82f2019-03-09 12:07:17 -05002160 if (!xprt_connected(task->tk_xprt)) {
2161 task->tk_status = -ENOTCONN;
Trond Myklebustc5445772018-09-03 23:39:27 -04002162 return;
Trond Myklebusted7dc972019-03-04 14:19:31 -05002163 }
Trond Myklebust009a82f2019-03-09 12:07:17 -05002164 xprt_transmit(task);
Trond Myklebustc5445772018-09-03 23:39:27 -04002165 }
Trond Myklebustc5445772018-09-03 23:39:27 -04002166 xprt_end_transmit(task);
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04002167}
2168
2169/*
2170 * 5a. Handle cleanup after a transmission
2171 */
2172static void
2173call_transmit_status(struct rpc_task *task)
2174{
2175 task->tk_action = call_status;
Chuck Lever206a1342009-12-03 15:58:56 -05002176
2177 /*
2178 * Common case: success. Force the compiler to put this
2179 * test first.
2180 */
Trond Myklebust009a82f2019-03-09 12:07:17 -05002181 if (rpc_task_transmitted(task)) {
Trond Myklebusta7b1a482019-04-15 11:54:13 -04002182 task->tk_status = 0;
2183 xprt_request_wait_receive(task);
Chuck Lever206a1342009-12-03 15:58:56 -05002184 return;
2185 }
2186
Trond Myklebust15f081c2009-03-11 14:37:57 -04002187 switch (task->tk_status) {
Trond Myklebust15f081c2009-03-11 14:37:57 -04002188 default:
Trond Myklebust75891f52018-09-03 17:37:36 -04002189 break;
Trond Myklebust78b576c2018-08-28 09:20:10 -04002190 case -EBADMSG:
Trond Myklebust762e4e62018-08-24 16:28:28 -04002191 task->tk_status = 0;
2192 task->tk_action = call_encode;
Trond Myklebust78b576c2018-08-28 09:20:10 -04002193 break;
Trond Myklebust15f081c2009-03-11 14:37:57 -04002194 /*
2195 * Special cases: if we've been waiting on the
2196 * socket's write_space() callback, or if the
2197 * socket just returned a connection error,
2198 * then hold onto the transport lock.
2199 */
Trond Myklebust78b576c2018-08-28 09:20:10 -04002200 case -ENOBUFS:
2201 rpc_delay(task, HZ>>2);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002202 fallthrough;
Trond Myklebustc5445772018-09-03 23:39:27 -04002203 case -EBADSLT:
Trond Myklebust78b576c2018-08-28 09:20:10 -04002204 case -EAGAIN:
2205 task->tk_action = call_transmit;
2206 task->tk_status = 0;
2207 break;
Trond Myklebust15f081c2009-03-11 14:37:57 -04002208 case -ECONNREFUSED:
Trond Myklebust15f081c2009-03-11 14:37:57 -04002209 case -EHOSTDOWN:
Trond Myklebusteb5b46f2017-11-30 07:21:33 -05002210 case -ENETDOWN:
Trond Myklebust15f081c2009-03-11 14:37:57 -04002211 case -EHOSTUNREACH:
2212 case -ENETUNREACH:
Jason Baron3dedbb52014-09-24 18:08:04 +00002213 case -EPERM:
Chuck Lever09a21c42009-12-03 15:58:56 -05002214 if (RPC_IS_SOFTCONN(task)) {
Chuck Levera25a4cb2018-03-16 10:33:55 -04002215 if (!task->tk_msg.rpc_proc->p_proc)
2216 trace_xprt_ping(task->tk_xprt,
2217 task->tk_status);
Trond Myklebust5ad64b32019-04-07 13:58:54 -04002218 rpc_call_rpcerror(task, task->tk_status);
Trond Myklebust7b3fef82019-03-07 14:10:32 -05002219 return;
Chuck Lever09a21c42009-12-03 15:58:56 -05002220 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002221 fallthrough;
Chuck Lever09a21c42009-12-03 15:58:56 -05002222 case -ECONNRESET:
Trond Myklebustdf277272013-12-31 13:11:43 -05002223 case -ECONNABORTED:
Trond Myklebust3913c782015-02-08 21:44:04 -05002224 case -EADDRINUSE:
Chuck Lever09a21c42009-12-03 15:58:56 -05002225 case -ENOTCONN:
Trond Myklebustc8485e42009-03-11 14:37:59 -04002226 case -EPIPE:
Trond Myklebusted7dc972019-03-04 14:19:31 -05002227 task->tk_action = call_bind;
2228 task->tk_status = 0;
Trond Myklebust7ebbbc62018-08-28 09:00:27 -04002229 break;
Trond Myklebust15f081c2009-03-11 14:37:57 -04002230 }
Trond Myklebust7b3fef82019-03-07 14:10:32 -05002231 rpc_check_timeout(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232}
2233
Trond Myklebust9e00abc2011-07-13 19:20:49 -04002234#if defined(CONFIG_SUNRPC_BACKCHANNEL)
Trond Myklebust477687e2019-03-05 07:30:48 -05002235static void call_bc_transmit(struct rpc_task *task);
2236static void call_bc_transmit_status(struct rpc_task *task);
2237
2238static void
2239call_bc_encode(struct rpc_task *task)
2240{
2241 xprt_request_enqueue_transmit(task);
2242 task->tk_action = call_bc_transmit;
2243}
2244
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04002245/*
2246 * 5b. Send the backchannel RPC reply. On error, drop the reply. In
2247 * addition, disconnect on connectivity errors.
2248 */
2249static void
2250call_bc_transmit(struct rpc_task *task)
2251{
Trond Myklebust477687e2019-03-05 07:30:48 -05002252 task->tk_action = call_bc_transmit_status;
2253 if (test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate)) {
2254 if (!xprt_prepare_transmit(task))
2255 return;
2256 task->tk_status = 0;
2257 xprt_transmit(task);
2258 }
2259 xprt_end_transmit(task);
2260}
2261
2262static void
2263call_bc_transmit_status(struct rpc_task *task)
2264{
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04002265 struct rpc_rqst *req = task->tk_rqstp;
2266
Trond Myklebusta7b1a482019-04-15 11:54:13 -04002267 if (rpc_task_transmitted(task))
2268 task->tk_status = 0;
2269
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04002270 switch (task->tk_status) {
2271 case 0:
2272 /* Success */
Trond Myklebusteb5b46f2017-11-30 07:21:33 -05002273 case -ENETDOWN:
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04002274 case -EHOSTDOWN:
2275 case -EHOSTUNREACH:
2276 case -ENETUNREACH:
Trond Myklebust38325912015-06-19 13:04:13 -04002277 case -ECONNRESET:
2278 case -ECONNREFUSED:
2279 case -EADDRINUSE:
2280 case -ENOTCONN:
2281 case -EPIPE:
2282 break;
Trond Myklebust477687e2019-03-05 07:30:48 -05002283 case -ENOBUFS:
2284 rpc_delay(task, HZ>>2);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002285 fallthrough;
Trond Myklebust477687e2019-03-05 07:30:48 -05002286 case -EBADSLT:
Trond Myklebustc5445772018-09-03 23:39:27 -04002287 case -EAGAIN:
Trond Myklebust477687e2019-03-05 07:30:48 -05002288 task->tk_status = 0;
2289 task->tk_action = call_bc_transmit;
2290 return;
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04002291 case -ETIMEDOUT:
2292 /*
2293 * Problem reaching the server. Disconnect and let the
2294 * forechannel reestablish the connection. The server will
2295 * have to retransmit the backchannel request and we'll
2296 * reprocess it. Since these ops are idempotent, there's no
2297 * need to cache our reply at this time.
2298 */
2299 printk(KERN_NOTICE "RPC: Could not send backchannel reply "
2300 "error: %d\n", task->tk_status);
Trond Myklebusta4f08352013-01-08 09:10:21 -05002301 xprt_conditional_disconnect(req->rq_xprt,
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04002302 req->rq_connect_cookie);
2303 break;
2304 default:
2305 /*
2306 * We were unable to reply and will have to drop the
2307 * request. The server should reconnect and retransmit.
2308 */
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04002309 printk(KERN_NOTICE "RPC: Could not send backchannel reply "
2310 "error: %d\n", task->tk_status);
2311 break;
2312 }
Trond Myklebust1193d582015-06-02 11:53:21 -04002313 task->tk_action = rpc_exit_task;
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04002314}
Trond Myklebust9e00abc2011-07-13 19:20:49 -04002315#endif /* CONFIG_SUNRPC_BACKCHANNEL */
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04002316
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317/*
2318 * 6. Sort out the RPC call status
2319 */
2320static void
2321call_status(struct rpc_task *task)
2322{
2323 struct rpc_clnt *clnt = task->tk_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 int status;
2325
Chuck Levera25a4cb2018-03-16 10:33:55 -04002326 if (!task->tk_msg.rpc_proc->p_proc)
2327 trace_xprt_ping(task->tk_xprt, task->tk_status);
2328
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 status = task->tk_status;
2330 if (status >= 0) {
2331 task->tk_action = call_decode;
2332 return;
2333 }
2334
Steve Dickson5753cba2012-02-06 10:08:08 -05002335 trace_rpc_call_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 task->tk_status = 0;
2337 switch(status) {
Trond Myklebust76303992006-08-30 14:32:49 -04002338 case -EHOSTDOWN:
Trond Myklebusteb5b46f2017-11-30 07:21:33 -05002339 case -ENETDOWN:
Trond Myklebust76303992006-08-30 14:32:49 -04002340 case -EHOSTUNREACH:
2341 case -ENETUNREACH:
Jason Baron3dedbb52014-09-24 18:08:04 +00002342 case -EPERM:
Trond Myklebustcea57782019-03-09 16:06:47 -05002343 if (RPC_IS_SOFTCONN(task))
2344 goto out_exit;
Trond Myklebust76303992006-08-30 14:32:49 -04002345 /*
2346 * Delay any retries for 3 seconds, then handle as if it
2347 * were a timeout.
2348 */
2349 rpc_delay(task, 3*HZ);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002350 fallthrough;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 case -ETIMEDOUT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 break;
2353 case -ECONNREFUSED:
Trond Myklebustdf277272013-12-31 13:11:43 -05002354 case -ECONNRESET:
2355 case -ECONNABORTED:
Olga Kornievskaiaec6017d2019-05-29 10:46:00 -04002356 case -ENOTCONN:
Chuck Lever35f5a422006-01-03 09:55:50 +01002357 rpc_force_rebind(clnt);
Trond Myklebustc82e5472019-08-16 08:58:48 -04002358 break;
Trond Myklebust3913c782015-02-08 21:44:04 -05002359 case -EADDRINUSE:
Trond Myklebustc8485e42009-03-11 14:37:59 -04002360 rpc_delay(task, 3*HZ);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002361 fallthrough;
Trond Myklebustc8485e42009-03-11 14:37:59 -04002362 case -EPIPE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 case -EAGAIN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 break;
2365 case -EIO:
2366 /* shutdown or soft timeout */
Trond Myklebustcea57782019-03-09 16:06:47 -05002367 goto out_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 default:
Olga Kornievskaiab6b61522008-06-09 16:51:31 -04002369 if (clnt->cl_chatty)
2370 printk("%s: RPC call returned error %d\n",
Trond Myklebust55909f22013-08-23 11:48:15 -04002371 clnt->cl_program->name, -status);
Trond Myklebustcea57782019-03-09 16:06:47 -05002372 goto out_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 }
Trond Myklebustcea57782019-03-09 16:06:47 -05002374 task->tk_action = call_encode;
Olga Kornievskaia88428cc42020-06-23 11:24:09 -04002375 if (status != -ECONNRESET && status != -ECONNABORTED)
2376 rpc_check_timeout(task);
Trond Myklebustcea57782019-03-09 16:06:47 -05002377 return;
2378out_exit:
Trond Myklebust5ad64b32019-04-07 13:58:54 -04002379 rpc_call_rpcerror(task, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380}
2381
Trond Myklebustd84dd3f2019-03-19 11:24:54 -04002382static bool
2383rpc_check_connected(const struct rpc_rqst *req)
2384{
2385 /* No allocated request or transport? return true */
2386 if (!req || !req->rq_xprt)
2387 return true;
2388 return xprt_connected(req->rq_xprt);
2389}
2390
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391static void
Trond Myklebust7b3fef82019-03-07 14:10:32 -05002392rpc_check_timeout(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393{
2394 struct rpc_clnt *clnt = task->tk_client;
2395
Chuck Leverce99aa62020-05-09 14:07:13 -04002396 if (RPC_SIGNALLED(task)) {
2397 rpc_call_rpcerror(task, -ERESTARTSYS);
2398 return;
2399 }
2400
Trond Myklebust7b3fef82019-03-07 14:10:32 -05002401 if (xprt_adjust_timeout(task->tk_rqstp) == 0)
2402 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403
Chuck Lever914cdcc2020-07-08 16:09:58 -04002404 trace_rpc_timeout_status(task);
Chuck Leveref759a22006-03-20 13:44:17 -05002405 task->tk_timeouts++;
2406
Trond Myklebustd84dd3f2019-03-19 11:24:54 -04002407 if (RPC_IS_SOFTCONN(task) && !rpc_check_connected(task->tk_rqstp)) {
Trond Myklebust5ad64b32019-04-07 13:58:54 -04002408 rpc_call_rpcerror(task, -ETIMEDOUT);
Chuck Lever3a28bec2009-12-03 15:58:56 -05002409 return;
2410 }
Trond Myklebustd84dd3f2019-03-19 11:24:54 -04002411
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 if (RPC_IS_SOFT(task)) {
Trond Myklebuste4ec48d2019-04-07 13:58:55 -04002413 /*
2414 * Once a "no retrans timeout" soft tasks (a.k.a NFSv4) has
2415 * been sent, it should time out only if the transport
2416 * connection gets terminally broken.
2417 */
2418 if ((task->tk_flags & RPC_TASK_NO_RETRANS_TIMEOUT) &&
2419 rpc_check_connected(task->tk_rqstp))
2420 return;
2421
Joe Perchescac5d072012-07-18 11:17:11 -07002422 if (clnt->cl_chatty) {
Trond Myklebust0729d992019-04-07 13:58:57 -04002423 pr_notice_ratelimited(
2424 "%s: server %s not responding, timed out\n",
Trond Myklebust55909f22013-08-23 11:48:15 -04002425 clnt->cl_program->name,
Trond Myklebustfb43d172016-01-30 16:39:26 -05002426 task->tk_xprt->servername);
Joe Perchescac5d072012-07-18 11:17:11 -07002427 }
Trond Myklebust7494d002011-04-24 14:28:45 -04002428 if (task->tk_flags & RPC_TASK_TIMEOUT)
Trond Myklebust5ad64b32019-04-07 13:58:54 -04002429 rpc_call_rpcerror(task, -ETIMEDOUT);
Trond Myklebust7494d002011-04-24 14:28:45 -04002430 else
Trond Myklebust5ad64b32019-04-07 13:58:54 -04002431 __rpc_call_rpcerror(task, -EIO, -ETIMEDOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 return;
2433 }
2434
Chuck Leverf518e35a2006-01-03 09:55:52 +01002435 if (!(task->tk_flags & RPC_CALL_MAJORSEEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 task->tk_flags |= RPC_CALL_MAJORSEEN;
Trond Myklebust4e0038b2012-03-01 17:01:05 -05002437 if (clnt->cl_chatty) {
Trond Myklebust0729d992019-04-07 13:58:57 -04002438 pr_notice_ratelimited(
2439 "%s: server %s not responding, still trying\n",
2440 clnt->cl_program->name,
2441 task->tk_xprt->servername);
Trond Myklebust4e0038b2012-03-01 17:01:05 -05002442 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 }
Chuck Lever35f5a422006-01-03 09:55:50 +01002444 rpc_force_rebind(clnt);
Trond Myklebustb48633b2008-04-22 16:47:55 -04002445 /*
2446 * Did our request time out due to an RPCSEC_GSS out-of-sequence
2447 * event? RFC2203 requires the server to drop all such requests.
2448 */
2449 rpcauth_invalcred(task);
Trond Myklebust7b3fef82019-03-07 14:10:32 -05002450}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451
Trond Myklebust7b3fef82019-03-07 14:10:32 -05002452/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 * 7. Decode the RPC reply
2454 */
2455static void
2456call_decode(struct rpc_task *task)
2457{
2458 struct rpc_clnt *clnt = task->tk_client;
2459 struct rpc_rqst *req = task->tk_rqstp;
Chuck Levera0584ee2019-02-11 11:24:58 -05002460 struct xdr_stream xdr;
Trond Myklebust9ba82882019-09-16 09:12:19 -04002461 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462
Chuck Levera0584ee2019-02-11 11:24:58 -05002463 if (!task->tk_msg.rpc_proc->p_decode) {
Trond Myklebust9ee94d32018-08-28 16:27:31 -04002464 task->tk_action = rpc_exit_task;
2465 return;
2466 }
2467
Chuck Leverf518e35a2006-01-03 09:55:52 +01002468 if (task->tk_flags & RPC_CALL_MAJORSEEN) {
Trond Myklebust4e0038b2012-03-01 17:01:05 -05002469 if (clnt->cl_chatty) {
Trond Myklebust0729d992019-04-07 13:58:57 -04002470 pr_notice_ratelimited("%s: server %s OK\n",
Trond Myklebust55909f22013-08-23 11:48:15 -04002471 clnt->cl_program->name,
Trond Myklebustfb43d172016-01-30 16:39:26 -05002472 task->tk_xprt->servername);
Trond Myklebust4e0038b2012-03-01 17:01:05 -05002473 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 task->tk_flags &= ~RPC_CALL_MAJORSEEN;
2475 }
2476
Trond Myklebust43ac3f22006-03-20 13:44:51 -05002477 /*
Trond Myklebust9ba82882019-09-16 09:12:19 -04002478 * Did we ever call xprt_complete_rqst()? If not, we should assume
2479 * the message is incomplete.
2480 */
2481 err = -EAGAIN;
2482 if (!req->rq_reply_bytes_recvd)
2483 goto out;
2484
Baptiste Lepersf8f7e0f2021-05-01 14:10:51 +10002485 /* Ensure that we see all writes made by xprt_complete_rqst()
2486 * before it changed req->rq_reply_bytes_recvd.
2487 */
2488 smp_rmb();
2489
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 req->rq_rcv_buf.len = req->rq_private_buf.len;
Chuck Leverc509f15a2020-05-12 17:13:28 -04002491 trace_rpc_xdr_recvfrom(task, &req->rq_rcv_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492
2493 /* Check that the softirq receive buffer is valid */
2494 WARN_ON(memcmp(&req->rq_rcv_buf, &req->rq_private_buf,
2495 sizeof(req->rq_rcv_buf)) != 0);
2496
Chuck Levera0584ee2019-02-11 11:24:58 -05002497 xdr_init_decode(&xdr, &req->rq_rcv_buf,
2498 req->rq_rcv_buf.head[0].iov_base, req);
Trond Myklebust9ba82882019-09-16 09:12:19 -04002499 err = rpc_decode_header(task, &xdr);
2500out:
2501 switch (err) {
Chuck Levera0584ee2019-02-11 11:24:58 -05002502 case 0:
2503 task->tk_action = rpc_exit_task;
2504 task->tk_status = rpcauth_unwrap_resp(task, &xdr);
Trond Myklebustabbcf282006-01-03 09:55:03 +01002505 return;
Chuck Levera0584ee2019-02-11 11:24:58 -05002506 case -EAGAIN:
Chuck Levera0584ee2019-02-11 11:24:58 -05002507 task->tk_status = 0;
Trond Myklebust7987b692019-05-29 12:49:52 -04002508 if (task->tk_client->cl_discrtry)
2509 xprt_conditional_disconnect(req->rq_xprt,
2510 req->rq_connect_cookie);
Trond Myklebustcea57782019-03-09 16:06:47 -05002511 task->tk_action = call_encode;
2512 rpc_check_timeout(task);
Trond Myklebust7987b692019-05-29 12:49:52 -04002513 break;
2514 case -EKEYREJECTED:
2515 task->tk_action = call_reserve;
2516 rpc_check_timeout(task);
2517 rpcauth_invalcred(task);
2518 /* Ensure we obtain a new XID if we retry! */
2519 xprt_release(task);
Trond Myklebust24b74bf2008-04-19 13:15:47 -04002520 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521}
2522
Chuck Levere8680a22019-02-11 11:24:48 -05002523static int
2524rpc_encode_header(struct rpc_task *task, struct xdr_stream *xdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525{
2526 struct rpc_clnt *clnt = task->tk_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 struct rpc_rqst *req = task->tk_rqstp;
Chuck Levere8680a22019-02-11 11:24:48 -05002528 __be32 *p;
2529 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530
Chuck Levere8680a22019-02-11 11:24:48 -05002531 error = -EMSGSIZE;
2532 p = xdr_reserve_space(xdr, RPC_CALLHDRSIZE << 2);
2533 if (!p)
2534 goto out_fail;
2535 *p++ = req->rq_xid;
2536 *p++ = rpc_call;
2537 *p++ = cpu_to_be32(RPC_VERSION);
2538 *p++ = cpu_to_be32(clnt->cl_prog);
2539 *p++ = cpu_to_be32(clnt->cl_vers);
2540 *p = cpu_to_be32(task->tk_msg.rpc_proc->p_proc);
Chuck Lever808012f2005-08-25 16:25:49 -07002541
Chuck Levere8680a22019-02-11 11:24:48 -05002542 error = rpcauth_marshcred(task, xdr);
2543 if (error < 0)
2544 goto out_fail;
2545 return 0;
2546out_fail:
2547 trace_rpc_bad_callhdr(task);
Trond Myklebust714fbc72019-09-12 08:06:51 -04002548 rpc_call_rpcerror(task, error);
Chuck Levere8680a22019-02-11 11:24:48 -05002549 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550}
2551
Chuck Levera0584ee2019-02-11 11:24:58 -05002552static noinline int
2553rpc_decode_header(struct rpc_task *task, struct xdr_stream *xdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554{
Trond Myklebust4e0038b2012-03-01 17:01:05 -05002555 struct rpc_clnt *clnt = task->tk_client;
Trond Myklebusteb90a162019-03-15 09:29:00 -04002556 int error;
Chuck Levera0584ee2019-02-11 11:24:58 -05002557 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558
Chuck Lever7f5667a2019-02-11 11:24:53 -05002559 /* RFC-1014 says that the representation of XDR data must be a
2560 * multiple of four bytes
2561 * - if it isn't pointer subtraction in the NFS client may give
2562 * undefined results
2563 */
2564 if (task->tk_rqstp->rq_rcv_buf.len & 3)
Trond Myklebusteb90a162019-03-15 09:29:00 -04002565 goto out_unparsable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566
Chuck Levera0584ee2019-02-11 11:24:58 -05002567 p = xdr_inline_decode(xdr, 3 * sizeof(*p));
2568 if (!p)
2569 goto out_unparsable;
Chuck Lever7f5667a2019-02-11 11:24:53 -05002570 p++; /* skip XID */
2571 if (*p++ != rpc_reply)
2572 goto out_unparsable;
2573 if (*p++ != rpc_msg_accepted)
2574 goto out_msg_denied;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04002575
Chuck Levera0584ee2019-02-11 11:24:58 -05002576 error = rpcauth_checkverf(task, xdr);
2577 if (error)
Chuck Lever7f5667a2019-02-11 11:24:53 -05002578 goto out_verifier;
2579
Chuck Levera0584ee2019-02-11 11:24:58 -05002580 p = xdr_inline_decode(xdr, sizeof(*p));
2581 if (!p)
Chuck Lever7f5667a2019-02-11 11:24:53 -05002582 goto out_unparsable;
Chuck Levera0584ee2019-02-11 11:24:58 -05002583 switch (*p) {
Chuck Lever7f5667a2019-02-11 11:24:53 -05002584 case rpc_success:
Chuck Levera0584ee2019-02-11 11:24:58 -05002585 return 0;
Chuck Lever7f5667a2019-02-11 11:24:53 -05002586 case rpc_prog_unavail:
2587 trace_rpc__prog_unavail(task);
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00002588 error = -EPFNOSUPPORT;
2589 goto out_err;
Chuck Lever7f5667a2019-02-11 11:24:53 -05002590 case rpc_prog_mismatch:
2591 trace_rpc__prog_mismatch(task);
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00002592 error = -EPROTONOSUPPORT;
2593 goto out_err;
Chuck Lever7f5667a2019-02-11 11:24:53 -05002594 case rpc_proc_unavail:
2595 trace_rpc__proc_unavail(task);
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00002596 error = -EOPNOTSUPP;
2597 goto out_err;
Chuck Lever7f5667a2019-02-11 11:24:53 -05002598 case rpc_garbage_args:
Trond Myklebust928d42f2019-03-15 10:12:30 -04002599 case rpc_system_err:
Chuck Lever7f5667a2019-02-11 11:24:53 -05002600 trace_rpc__garbage_args(task);
Trond Myklebusteb90a162019-03-15 09:29:00 -04002601 error = -EIO;
Chuck Lever7f5667a2019-02-11 11:24:53 -05002602 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 default:
Trond Myklebusteb90a162019-03-15 09:29:00 -04002604 goto out_unparsable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 }
2606
Trond Myklebustabbcf282006-01-03 09:55:03 +01002607out_garbage:
Trond Myklebust4e0038b2012-03-01 17:01:05 -05002608 clnt->cl_stats->rpcgarbage++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 if (task->tk_garb_retry) {
2610 task->tk_garb_retry--;
Trond Myklebust762e4e62018-08-24 16:28:28 -04002611 task->tk_action = call_encode;
Chuck Levera0584ee2019-02-11 11:24:58 -05002612 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614out_err:
Trond Myklebust714fbc72019-09-12 08:06:51 -04002615 rpc_call_rpcerror(task, error);
Chuck Levera0584ee2019-02-11 11:24:58 -05002616 return error;
Chuck Lever7f5667a2019-02-11 11:24:53 -05002617
Chuck Lever7f5667a2019-02-11 11:24:53 -05002618out_unparsable:
2619 trace_rpc__unparsable(task);
2620 error = -EIO;
Trond Myklebustabbcf282006-01-03 09:55:03 +01002621 goto out_garbage;
Chuck Lever7f5667a2019-02-11 11:24:53 -05002622
2623out_verifier:
2624 trace_rpc_bad_verifier(task);
Chuck Lever7f5667a2019-02-11 11:24:53 -05002625 goto out_garbage;
2626
2627out_msg_denied:
Trond Myklebusteb90a162019-03-15 09:29:00 -04002628 error = -EACCES;
Chuck Levera0584ee2019-02-11 11:24:58 -05002629 p = xdr_inline_decode(xdr, sizeof(*p));
2630 if (!p)
2631 goto out_unparsable;
Chuck Lever7f5667a2019-02-11 11:24:53 -05002632 switch (*p++) {
2633 case rpc_auth_error:
2634 break;
2635 case rpc_mismatch:
2636 trace_rpc__mismatch(task);
2637 error = -EPROTONOSUPPORT;
2638 goto out_err;
2639 default:
Trond Myklebusteb90a162019-03-15 09:29:00 -04002640 goto out_unparsable;
Chuck Lever7f5667a2019-02-11 11:24:53 -05002641 }
2642
Chuck Levera0584ee2019-02-11 11:24:58 -05002643 p = xdr_inline_decode(xdr, sizeof(*p));
2644 if (!p)
2645 goto out_unparsable;
Chuck Lever7f5667a2019-02-11 11:24:53 -05002646 switch (*p++) {
2647 case rpc_autherr_rejectedcred:
2648 case rpc_autherr_rejectedverf:
2649 case rpcsec_gsserr_credproblem:
2650 case rpcsec_gsserr_ctxproblem:
2651 if (!task->tk_cred_retry)
2652 break;
2653 task->tk_cred_retry--;
2654 trace_rpc__stale_creds(task);
Trond Myklebust7987b692019-05-29 12:49:52 -04002655 return -EKEYREJECTED;
Chuck Lever7f5667a2019-02-11 11:24:53 -05002656 case rpc_autherr_badcred:
2657 case rpc_autherr_badverf:
2658 /* possibly garbled cred/verf? */
2659 if (!task->tk_garb_retry)
2660 break;
2661 task->tk_garb_retry--;
2662 trace_rpc__bad_creds(task);
2663 task->tk_action = call_encode;
Chuck Levera0584ee2019-02-11 11:24:58 -05002664 return -EAGAIN;
Chuck Lever7f5667a2019-02-11 11:24:53 -05002665 case rpc_autherr_tooweak:
2666 trace_rpc__auth_tooweak(task);
2667 pr_warn("RPC: server %s requires stronger authentication.\n",
2668 task->tk_xprt->servername);
2669 break;
2670 default:
Trond Myklebusteb90a162019-03-15 09:29:00 -04002671 goto out_unparsable;
Chuck Lever7f5667a2019-02-11 11:24:53 -05002672 }
2673 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674}
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00002675
Christoph Hellwigc512f362017-05-08 09:31:19 +02002676static void rpcproc_encode_null(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
2677 const void *obj)
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00002678{
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00002679}
2680
Christoph Hellwig73c8dc12017-05-08 14:58:11 +02002681static int rpcproc_decode_null(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
2682 void *obj)
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00002683{
2684 return 0;
2685}
2686
Christoph Hellwig499b4982017-05-12 15:36:49 +02002687static const struct rpc_procinfo rpcproc_null = {
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00002688 .p_encode = rpcproc_encode_null,
2689 .p_decode = rpcproc_decode_null,
2690};
2691
Chuck Lever823c73d02021-07-19 10:48:10 -04002692static void
2693rpc_null_call_prepare(struct rpc_task *task, void *data)
2694{
2695 task->tk_flags &= ~RPC_TASK_NO_RETRANS_TIMEOUT;
2696 rpc_call_start(task);
2697}
2698
2699static const struct rpc_call_ops rpc_null_ops = {
2700 .rpc_call_prepare = rpc_null_call_prepare,
2701 .rpc_call_done = rpc_default_callback,
2702};
2703
Trond Myklebust7f554892016-01-30 23:43:35 -05002704static
2705struct rpc_task *rpc_call_null_helper(struct rpc_clnt *clnt,
2706 struct rpc_xprt *xprt, struct rpc_cred *cred, int flags,
2707 const struct rpc_call_ops *ops, void *data)
Trond Myklebust5e1550d2007-06-23 10:17:16 -04002708{
2709 struct rpc_message msg = {
2710 .rpc_proc = &rpcproc_null,
Trond Myklebust5e1550d2007-06-23 10:17:16 -04002711 };
Trond Myklebust84115e12007-07-14 15:39:59 -04002712 struct rpc_task_setup task_setup_data = {
2713 .rpc_client = clnt,
Trond Myklebust7f554892016-01-30 23:43:35 -05002714 .rpc_xprt = xprt,
Trond Myklebust84115e12007-07-14 15:39:59 -04002715 .rpc_message = &msg,
NeilBrown1de7eea2018-12-03 11:30:30 +11002716 .rpc_op_cred = cred,
Chuck Lever823c73d02021-07-19 10:48:10 -04002717 .callback_ops = ops ?: &rpc_null_ops,
Trond Myklebust7f554892016-01-30 23:43:35 -05002718 .callback_data = data,
Chuck Lever841a2ed2020-05-12 17:13:55 -04002719 .flags = flags | RPC_TASK_SOFT | RPC_TASK_SOFTCONN |
2720 RPC_TASK_NULLCREDS,
Trond Myklebust84115e12007-07-14 15:39:59 -04002721 };
Trond Myklebust7f554892016-01-30 23:43:35 -05002722
Trond Myklebustc970aa82007-07-14 15:39:59 -04002723 return rpc_run_task(&task_setup_data);
Trond Myklebust5e1550d2007-06-23 10:17:16 -04002724}
Trond Myklebust7f554892016-01-30 23:43:35 -05002725
2726struct rpc_task *rpc_call_null(struct rpc_clnt *clnt, struct rpc_cred *cred, int flags)
2727{
2728 return rpc_call_null_helper(clnt, NULL, cred, flags, NULL, NULL);
2729}
Trond Myklebuste8914c62007-07-14 15:39:59 -04002730EXPORT_SYMBOL_GPL(rpc_call_null);
Trond Myklebust5e1550d2007-06-23 10:17:16 -04002731
Chuck Leveraede5172021-07-19 10:48:04 -04002732static int rpc_ping(struct rpc_clnt *clnt)
2733{
2734 struct rpc_task *task;
2735 int status;
2736
2737 task = rpc_call_null_helper(clnt, NULL, NULL, 0, NULL, NULL);
2738 if (IS_ERR(task))
2739 return PTR_ERR(task);
2740 status = task->tk_status;
2741 rpc_put_task(task);
2742 return status;
2743}
2744
Trond Myklebust7f554892016-01-30 23:43:35 -05002745struct rpc_cb_add_xprt_calldata {
2746 struct rpc_xprt_switch *xps;
2747 struct rpc_xprt *xprt;
2748};
2749
2750static void rpc_cb_add_xprt_done(struct rpc_task *task, void *calldata)
2751{
2752 struct rpc_cb_add_xprt_calldata *data = calldata;
2753
2754 if (task->tk_status == 0)
2755 rpc_xprt_switch_add_xprt(data->xps, data->xprt);
2756}
2757
2758static void rpc_cb_add_xprt_release(void *calldata)
2759{
2760 struct rpc_cb_add_xprt_calldata *data = calldata;
2761
2762 xprt_put(data->xprt);
2763 xprt_switch_put(data->xps);
2764 kfree(data);
2765}
2766
Trond Myklebustce272302016-07-24 17:06:28 -04002767static const struct rpc_call_ops rpc_cb_add_xprt_call_ops = {
Chuck Lever823c73d02021-07-19 10:48:10 -04002768 .rpc_call_prepare = rpc_null_call_prepare,
Trond Myklebust7f554892016-01-30 23:43:35 -05002769 .rpc_call_done = rpc_cb_add_xprt_done,
2770 .rpc_release = rpc_cb_add_xprt_release,
2771};
2772
2773/**
2774 * rpc_clnt_test_and_add_xprt - Test and add a new transport to a rpc_clnt
2775 * @clnt: pointer to struct rpc_clnt
2776 * @xps: pointer to struct rpc_xprt_switch,
2777 * @xprt: pointer struct rpc_xprt
2778 * @dummy: unused
2779 */
2780int rpc_clnt_test_and_add_xprt(struct rpc_clnt *clnt,
2781 struct rpc_xprt_switch *xps, struct rpc_xprt *xprt,
2782 void *dummy)
2783{
2784 struct rpc_cb_add_xprt_calldata *data;
Trond Myklebust7f554892016-01-30 23:43:35 -05002785 struct rpc_task *task;
2786
Olga Kornievskaiadc48e0a2021-08-27 14:37:18 -04002787 if (xps->xps_nunique_destaddr_xprts + 1 > clnt->cl_max_connect) {
2788 rcu_read_lock();
2789 pr_warn("SUNRPC: reached max allowed number (%d) did not add "
2790 "transport to server: %s\n", clnt->cl_max_connect,
2791 rpc_peeraddr2str(clnt, RPC_DISPLAY_ADDR));
2792 rcu_read_unlock();
2793 return -EINVAL;
2794 }
2795
Trond Myklebust7f554892016-01-30 23:43:35 -05002796 data = kmalloc(sizeof(*data), GFP_NOFS);
2797 if (!data)
2798 return -ENOMEM;
2799 data->xps = xprt_switch_get(xps);
2800 data->xprt = xprt_get(xprt);
Trond Myklebust612b41f2017-04-27 08:50:51 -04002801 if (rpc_xprt_switch_has_addr(data->xps, (struct sockaddr *)&xprt->addr)) {
2802 rpc_cb_add_xprt_release(data);
2803 goto success;
2804 }
Trond Myklebust7f554892016-01-30 23:43:35 -05002805
Chuck Lever841a2ed2020-05-12 17:13:55 -04002806 task = rpc_call_null_helper(clnt, xprt, NULL, RPC_TASK_ASYNC,
Trond Myklebust7f554892016-01-30 23:43:35 -05002807 &rpc_cb_add_xprt_call_ops, data);
Olga Kornievskaia3a3f9762021-08-27 14:37:14 -04002808 data->xps->xps_nunique_destaddr_xprts++;
Trond Myklebust7f554892016-01-30 23:43:35 -05002809 rpc_put_task(task);
Trond Myklebust612b41f2017-04-27 08:50:51 -04002810success:
Trond Myklebust7f554892016-01-30 23:43:35 -05002811 return 1;
2812}
2813EXPORT_SYMBOL_GPL(rpc_clnt_test_and_add_xprt);
2814
2815/**
Andy Adamsonfda0ab42016-09-09 09:22:26 -04002816 * rpc_clnt_setup_test_and_add_xprt()
2817 *
2818 * This is an rpc_clnt_add_xprt setup() function which returns 1 so:
2819 * 1) caller of the test function must dereference the rpc_xprt_switch
2820 * and the rpc_xprt.
2821 * 2) test function must call rpc_xprt_switch_add_xprt, usually in
2822 * the rpc_call_done routine.
2823 *
2824 * Upon success (return of 1), the test function adds the new
2825 * transport to the rpc_clnt xprt switch
2826 *
2827 * @clnt: struct rpc_clnt to get the new transport
2828 * @xps: the rpc_xprt_switch to hold the new transport
2829 * @xprt: the rpc_xprt to test
2830 * @data: a struct rpc_add_xprt_test pointer that holds the test function
2831 * and test function call data
2832 */
2833int rpc_clnt_setup_test_and_add_xprt(struct rpc_clnt *clnt,
2834 struct rpc_xprt_switch *xps,
2835 struct rpc_xprt *xprt,
2836 void *data)
2837{
Andy Adamsonfda0ab42016-09-09 09:22:26 -04002838 struct rpc_task *task;
2839 struct rpc_add_xprt_test *xtest = (struct rpc_add_xprt_test *)data;
2840 int status = -EADDRINUSE;
2841
2842 xprt = xprt_get(xprt);
2843 xprt_switch_get(xps);
2844
2845 if (rpc_xprt_switch_has_addr(xps, (struct sockaddr *)&xprt->addr))
2846 goto out_err;
2847
2848 /* Test the connection */
Chuck Lever841a2ed2020-05-12 17:13:55 -04002849 task = rpc_call_null_helper(clnt, xprt, NULL, 0, NULL, NULL);
Andy Adamsonfda0ab42016-09-09 09:22:26 -04002850 if (IS_ERR(task)) {
2851 status = PTR_ERR(task);
2852 goto out_err;
2853 }
2854 status = task->tk_status;
2855 rpc_put_task(task);
2856
2857 if (status < 0)
2858 goto out_err;
2859
2860 /* rpc_xprt_switch and rpc_xprt are deferrenced by add_xprt_test() */
2861 xtest->add_xprt_test(clnt, xprt, xtest->data);
2862
Santosh kumar pradhan10e037d2018-12-19 12:29:57 +05302863 xprt_put(xprt);
2864 xprt_switch_put(xps);
2865
Andy Adamsonfda0ab42016-09-09 09:22:26 -04002866 /* so that rpc_clnt_add_xprt does not call rpc_xprt_switch_add_xprt */
2867 return 1;
2868out_err:
2869 xprt_put(xprt);
2870 xprt_switch_put(xps);
2871 pr_info("RPC: rpc_clnt_test_xprt failed: %d addr %s not added\n",
2872 status, xprt->address_strings[RPC_DISPLAY_ADDR]);
2873 return status;
2874}
2875EXPORT_SYMBOL_GPL(rpc_clnt_setup_test_and_add_xprt);
2876
2877/**
Trond Myklebust7f554892016-01-30 23:43:35 -05002878 * rpc_clnt_add_xprt - Add a new transport to a rpc_clnt
2879 * @clnt: pointer to struct rpc_clnt
2880 * @xprtargs: pointer to struct xprt_create
2881 * @setup: callback to test and/or set up the connection
2882 * @data: pointer to setup function data
2883 *
2884 * Creates a new transport using the parameters set in args and
2885 * adds it to clnt.
2886 * If ping is set, then test that connectivity succeeds before
2887 * adding the new transport.
2888 *
2889 */
2890int rpc_clnt_add_xprt(struct rpc_clnt *clnt,
2891 struct xprt_create *xprtargs,
2892 int (*setup)(struct rpc_clnt *,
2893 struct rpc_xprt_switch *,
2894 struct rpc_xprt *,
2895 void *),
2896 void *data)
2897{
2898 struct rpc_xprt_switch *xps;
2899 struct rpc_xprt *xprt;
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002900 unsigned long connect_timeout;
Trond Myklebust3851f1c2016-08-04 00:08:45 -04002901 unsigned long reconnect_timeout;
Trond Myklebuste6237b62019-10-17 11:13:54 -04002902 unsigned char resvport, reuseport;
Trond Myklebust7f554892016-01-30 23:43:35 -05002903 int ret = 0;
2904
2905 rcu_read_lock();
2906 xps = xprt_switch_get(rcu_dereference(clnt->cl_xpi.xpi_xpswitch));
2907 xprt = xprt_iter_xprt(&clnt->cl_xpi);
2908 if (xps == NULL || xprt == NULL) {
2909 rcu_read_unlock();
Lin Yib9622612019-06-10 10:16:56 +08002910 xprt_switch_put(xps);
Trond Myklebust7f554892016-01-30 23:43:35 -05002911 return -EAGAIN;
2912 }
2913 resvport = xprt->resvport;
Trond Myklebuste6237b62019-10-17 11:13:54 -04002914 reuseport = xprt->reuseport;
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002915 connect_timeout = xprt->connect_timeout;
Trond Myklebust3851f1c2016-08-04 00:08:45 -04002916 reconnect_timeout = xprt->max_reconnect_timeout;
Trond Myklebust7f554892016-01-30 23:43:35 -05002917 rcu_read_unlock();
2918
2919 xprt = xprt_create_transport(xprtargs);
2920 if (IS_ERR(xprt)) {
2921 ret = PTR_ERR(xprt);
2922 goto out_put_switch;
2923 }
2924 xprt->resvport = resvport;
Trond Myklebuste6237b62019-10-17 11:13:54 -04002925 xprt->reuseport = reuseport;
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002926 if (xprt->ops->set_connect_timeout != NULL)
2927 xprt->ops->set_connect_timeout(xprt,
2928 connect_timeout,
2929 reconnect_timeout);
Trond Myklebust7f554892016-01-30 23:43:35 -05002930
2931 rpc_xprt_switch_set_roundrobin(xps);
2932 if (setup) {
2933 ret = setup(clnt, xps, xprt, data);
2934 if (ret != 0)
2935 goto out_put_xprt;
2936 }
2937 rpc_xprt_switch_add_xprt(xps, xprt);
2938out_put_xprt:
2939 xprt_put(xprt);
2940out_put_switch:
2941 xprt_switch_put(xps);
2942 return ret;
2943}
2944EXPORT_SYMBOL_GPL(rpc_clnt_add_xprt);
2945
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002946struct connect_timeout_data {
2947 unsigned long connect_timeout;
2948 unsigned long reconnect_timeout;
2949};
2950
Trond Myklebust8d480322016-08-05 19:03:31 -04002951static int
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002952rpc_xprt_set_connect_timeout(struct rpc_clnt *clnt,
Trond Myklebust8d480322016-08-05 19:03:31 -04002953 struct rpc_xprt *xprt,
2954 void *data)
2955{
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002956 struct connect_timeout_data *timeo = data;
Trond Myklebust8d480322016-08-05 19:03:31 -04002957
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002958 if (xprt->ops->set_connect_timeout)
2959 xprt->ops->set_connect_timeout(xprt,
2960 timeo->connect_timeout,
2961 timeo->reconnect_timeout);
Trond Myklebust8d480322016-08-05 19:03:31 -04002962 return 0;
2963}
2964
2965void
Trond Myklebust26ae1022017-02-08 11:17:55 -05002966rpc_set_connect_timeout(struct rpc_clnt *clnt,
2967 unsigned long connect_timeout,
2968 unsigned long reconnect_timeout)
Trond Myklebust8d480322016-08-05 19:03:31 -04002969{
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002970 struct connect_timeout_data timeout = {
Trond Myklebust26ae1022017-02-08 11:17:55 -05002971 .connect_timeout = connect_timeout,
2972 .reconnect_timeout = reconnect_timeout,
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002973 };
Trond Myklebust8d480322016-08-05 19:03:31 -04002974 rpc_clnt_iterate_for_each_xprt(clnt,
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002975 rpc_xprt_set_connect_timeout,
2976 &timeout);
Trond Myklebust8d480322016-08-05 19:03:31 -04002977}
Trond Myklebust26ae1022017-02-08 11:17:55 -05002978EXPORT_SYMBOL_GPL(rpc_set_connect_timeout);
Trond Myklebust8d480322016-08-05 19:03:31 -04002979
Andy Adamson3b58a8a2016-09-09 09:22:23 -04002980void rpc_clnt_xprt_switch_put(struct rpc_clnt *clnt)
2981{
Anna Schumakerbb29dd82016-10-26 10:33:31 -04002982 rcu_read_lock();
Andy Adamson3b58a8a2016-09-09 09:22:23 -04002983 xprt_switch_put(rcu_dereference(clnt->cl_xpi.xpi_xpswitch));
Anna Schumakerbb29dd82016-10-26 10:33:31 -04002984 rcu_read_unlock();
Andy Adamson3b58a8a2016-09-09 09:22:23 -04002985}
2986EXPORT_SYMBOL_GPL(rpc_clnt_xprt_switch_put);
2987
Andy Adamsondd691712016-09-09 09:22:24 -04002988void rpc_clnt_xprt_switch_add_xprt(struct rpc_clnt *clnt, struct rpc_xprt *xprt)
2989{
Anna Schumakerbb29dd82016-10-26 10:33:31 -04002990 rcu_read_lock();
Andy Adamsondd691712016-09-09 09:22:24 -04002991 rpc_xprt_switch_add_xprt(rcu_dereference(clnt->cl_xpi.xpi_xpswitch),
2992 xprt);
Anna Schumakerbb29dd82016-10-26 10:33:31 -04002993 rcu_read_unlock();
Andy Adamsondd691712016-09-09 09:22:24 -04002994}
2995EXPORT_SYMBOL_GPL(rpc_clnt_xprt_switch_add_xprt);
2996
Andy Adamson39e5d2d2016-09-09 09:22:25 -04002997bool rpc_clnt_xprt_switch_has_addr(struct rpc_clnt *clnt,
2998 const struct sockaddr *sap)
2999{
3000 struct rpc_xprt_switch *xps;
3001 bool ret;
3002
Andy Adamson39e5d2d2016-09-09 09:22:25 -04003003 rcu_read_lock();
Anna Schumakerbb29dd82016-10-26 10:33:31 -04003004 xps = rcu_dereference(clnt->cl_xpi.xpi_xpswitch);
Andy Adamson39e5d2d2016-09-09 09:22:25 -04003005 ret = rpc_xprt_switch_has_addr(xps, sap);
3006 rcu_read_unlock();
3007 return ret;
3008}
3009EXPORT_SYMBOL_GPL(rpc_clnt_xprt_switch_has_addr);
3010
Jeff Laytonf895b252014-11-17 16:58:04 -05003011#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
Chuck Lever68a23ee2008-05-21 17:09:26 -04003012static void rpc_show_header(void)
3013{
Chuck Levercb3997b2008-05-21 17:09:41 -04003014 printk(KERN_INFO "-pid- flgs status -client- --rqstp- "
3015 "-timeout ---ops--\n");
Chuck Lever68a23ee2008-05-21 17:09:26 -04003016}
3017
Chuck Lever38e886e2008-05-21 17:09:33 -04003018static void rpc_show_task(const struct rpc_clnt *clnt,
3019 const struct rpc_task *task)
3020{
3021 const char *rpc_waitq = "none";
Chuck Lever38e886e2008-05-21 17:09:33 -04003022
3023 if (RPC_IS_QUEUED(task))
3024 rpc_waitq = rpc_qname(task->tk_waitqueue);
3025
Joe Perchesb3bceda2010-12-21 10:52:24 -05003026 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 -04003027 task->tk_pid, task->tk_flags, task->tk_status,
Trond Myklebust5efd1872019-04-07 13:58:50 -04003028 clnt, task->tk_rqstp, rpc_task_timeout(task), task->tk_ops,
Trond Myklebust55909f22013-08-23 11:48:15 -04003029 clnt->cl_program->name, clnt->cl_vers, rpc_proc_name(task),
Joe Perchesb3bceda2010-12-21 10:52:24 -05003030 task->tk_action, rpc_waitq);
Chuck Lever38e886e2008-05-21 17:09:33 -04003031}
3032
Stanislav Kinsbursky70abc492012-01-12 22:07:51 +04003033void rpc_show_tasks(struct net *net)
Trond Myklebust188fef12007-06-16 14:18:40 -04003034{
3035 struct rpc_clnt *clnt;
Chuck Lever38e886e2008-05-21 17:09:33 -04003036 struct rpc_task *task;
Chuck Lever68a23ee2008-05-21 17:09:26 -04003037 int header = 0;
Stanislav Kinsbursky70abc492012-01-12 22:07:51 +04003038 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
Trond Myklebust188fef12007-06-16 14:18:40 -04003039
Stanislav Kinsbursky70abc492012-01-12 22:07:51 +04003040 spin_lock(&sn->rpc_client_lock);
3041 list_for_each_entry(clnt, &sn->all_clients, cl_clients) {
Trond Myklebust188fef12007-06-16 14:18:40 -04003042 spin_lock(&clnt->cl_lock);
Chuck Lever38e886e2008-05-21 17:09:33 -04003043 list_for_each_entry(task, &clnt->cl_tasks, tk_task) {
Chuck Lever68a23ee2008-05-21 17:09:26 -04003044 if (!header) {
3045 rpc_show_header();
3046 header++;
3047 }
Chuck Lever38e886e2008-05-21 17:09:33 -04003048 rpc_show_task(clnt, task);
Trond Myklebust188fef12007-06-16 14:18:40 -04003049 }
3050 spin_unlock(&clnt->cl_lock);
3051 }
Stanislav Kinsbursky70abc492012-01-12 22:07:51 +04003052 spin_unlock(&sn->rpc_client_lock);
Trond Myklebust188fef12007-06-16 14:18:40 -04003053}
3054#endif
Jeff Layton3c87ef62015-06-03 16:14:25 -04003055
3056#if IS_ENABLED(CONFIG_SUNRPC_SWAP)
Trond Myklebust15001e52016-01-30 20:05:34 -05003057static int
3058rpc_clnt_swap_activate_callback(struct rpc_clnt *clnt,
3059 struct rpc_xprt *xprt,
3060 void *dummy)
3061{
3062 return xprt_enable_swap(xprt);
3063}
3064
Jeff Layton3c87ef62015-06-03 16:14:25 -04003065int
3066rpc_clnt_swap_activate(struct rpc_clnt *clnt)
3067{
Trond Myklebust15001e52016-01-30 20:05:34 -05003068 if (atomic_inc_return(&clnt->cl_swapper) == 1)
3069 return rpc_clnt_iterate_for_each_xprt(clnt,
3070 rpc_clnt_swap_activate_callback, NULL);
3071 return 0;
Jeff Layton3c87ef62015-06-03 16:14:25 -04003072}
3073EXPORT_SYMBOL_GPL(rpc_clnt_swap_activate);
3074
Trond Myklebust15001e52016-01-30 20:05:34 -05003075static int
3076rpc_clnt_swap_deactivate_callback(struct rpc_clnt *clnt,
3077 struct rpc_xprt *xprt,
3078 void *dummy)
3079{
3080 xprt_disable_swap(xprt);
3081 return 0;
3082}
3083
Jeff Layton3c87ef62015-06-03 16:14:25 -04003084void
3085rpc_clnt_swap_deactivate(struct rpc_clnt *clnt)
3086{
Trond Myklebust15001e52016-01-30 20:05:34 -05003087 if (atomic_dec_if_positive(&clnt->cl_swapper) == 0)
3088 rpc_clnt_iterate_for_each_xprt(clnt,
3089 rpc_clnt_swap_deactivate_callback, NULL);
Jeff Layton3c87ef62015-06-03 16:14:25 -04003090}
3091EXPORT_SYMBOL_GPL(rpc_clnt_swap_deactivate);
3092#endif /* CONFIG_SUNRPC_SWAP */