blob: 9bf820bad84c088d70ed4b75e0f4ec54c444a1d5 [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;
170 if (atomic_read(&clnt->cl_count) == 0)
171 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);
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500415 xprt_iter_init(&clnt->cl_xpi, xps);
416 xprt_switch_put(xps);
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400417
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 clnt->cl_rtt = &clnt->cl_rtt_default;
Trond Myklebustba7392b2007-12-20 16:03:55 -0500419 rpc_init_rtt(&clnt->cl_rtt_default, clnt->cl_timeout->to_initval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
Trond Myklebust006abe82010-09-12 19:55:25 -0400421 atomic_set(&clnt->cl_count, 1);
Trond Myklebust34f52e32007-06-14 16:40:31 -0400422
Trond Myklebust03a9a422015-01-30 18:12:28 -0500423 if (nodename == NULL)
424 nodename = utsname()->nodename;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 /* save the nodename */
Trond Myklebust03a9a422015-01-30 18:12:28 -0500426 rpc_clnt_set_nodename(clnt, nodename);
Stanislav Kinsburskye73f4cc2013-06-24 11:52:52 +0400427
Olga Kornievskaia2a338a52021-06-08 15:59:17 -0400428 rpc_sysfs_client_setup(clnt, xps, rpc_net_ns(clnt));
Chuck Leverd746e542013-10-17 14:12:17 -0400429 err = rpc_client_register(clnt, args->authflavor, args->client_name);
Stanislav Kinsburskye73f4cc2013-06-24 11:52:52 +0400430 if (err)
431 goto out_no_path;
Trond Myklebust280ebcf2013-09-02 13:28:04 -0400432 if (parent)
433 atomic_inc(&parent->cl_count);
Chuck Lever42aad0d2020-05-12 17:13:39 -0400434
435 trace_rpc_clnt_new(clnt, xprt, program->name, args->servername);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 return clnt;
437
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438out_no_path:
Trond Myklebust23bf85b2006-11-21 10:40:23 -0500439 rpc_free_iostats(clnt->cl_metrics);
440out_no_stats:
Trond Myklebust79caa5f2019-04-24 17:46:42 -0400441 put_cred(clnt->cl_cred);
Trond Myklebust2f048db2013-09-04 21:51:44 -0400442 rpc_free_clid(clnt);
443out_no_clid:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 kfree(clnt);
445out_err:
Trond Myklebust4ada5392007-06-14 17:26:17 -0400446 rpciod_down();
447out_no_rpciod:
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500448 xprt_switch_put(xps);
Trond Myklebustf05c1242013-04-05 14:13:21 -0400449 xprt_put(xprt);
Chuck Lever42aad0d2020-05-12 17:13:39 -0400450 trace_rpc_clnt_new_err(program->name, args->servername, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 return ERR_PTR(err);
452}
453
J. Bruce Fieldsd50039e2016-05-16 17:03:42 -0400454static struct rpc_clnt *rpc_create_xprt(struct rpc_create_args *args,
Kinglong Mee83ddfeb2014-03-24 11:58:59 +0800455 struct rpc_xprt *xprt)
456{
457 struct rpc_clnt *clnt = NULL;
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500458 struct rpc_xprt_switch *xps;
Kinglong Mee83ddfeb2014-03-24 11:58:59 +0800459
J. Bruce Fields39a9bea2016-05-17 12:38:21 -0400460 if (args->bc_xprt && args->bc_xprt->xpt_bc_xps) {
Chuck Lever16590a22016-08-22 14:57:42 -0400461 WARN_ON_ONCE(!(args->protocol & XPRT_TRANSPORT_BC));
J. Bruce Fields39a9bea2016-05-17 12:38:21 -0400462 xps = args->bc_xprt->xpt_bc_xps;
463 xprt_switch_get(xps);
464 } else {
465 xps = xprt_switch_alloc(xprt, GFP_KERNEL);
466 if (xps == NULL) {
467 xprt_put(xprt);
468 return ERR_PTR(-ENOMEM);
469 }
470 if (xprt->bc_xprt) {
471 xprt_switch_get(xps);
472 xprt->bc_xprt->xpt_bc_xps = xps;
473 }
J. Bruce Fields1208fd52016-05-20 17:07:17 -0400474 }
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500475 clnt = rpc_new_client(args, xps, xprt, NULL);
Kinglong Mee83ddfeb2014-03-24 11:58:59 +0800476 if (IS_ERR(clnt))
477 return clnt;
478
479 if (!(args->flags & RPC_CLNT_CREATE_NOPING)) {
480 int err = rpc_ping(clnt);
481 if (err != 0) {
482 rpc_shutdown_client(clnt);
483 return ERR_PTR(err);
484 }
485 }
486
487 clnt->cl_softrtry = 1;
Trond Myklebustae6ec912019-04-07 13:58:58 -0400488 if (args->flags & (RPC_CLNT_CREATE_HARDRTRY|RPC_CLNT_CREATE_SOFTERR)) {
Kinglong Mee83ddfeb2014-03-24 11:58:59 +0800489 clnt->cl_softrtry = 0;
Trond Myklebustae6ec912019-04-07 13:58:58 -0400490 if (args->flags & RPC_CLNT_CREATE_SOFTERR)
491 clnt->cl_softerr = 1;
492 }
Kinglong Mee83ddfeb2014-03-24 11:58:59 +0800493
494 if (args->flags & RPC_CLNT_CREATE_AUTOBIND)
495 clnt->cl_autobind = 1;
Trond Myklebust2aca5b82014-09-24 22:35:58 -0400496 if (args->flags & RPC_CLNT_CREATE_NO_RETRANS_TIMEOUT)
497 clnt->cl_noretranstimeo = 1;
Kinglong Mee83ddfeb2014-03-24 11:58:59 +0800498 if (args->flags & RPC_CLNT_CREATE_DISCRTRY)
499 clnt->cl_discrtry = 1;
500 if (!(args->flags & RPC_CLNT_CREATE_QUIET))
501 clnt->cl_chatty = 1;
502
503 return clnt;
504}
Kinglong Mee83ddfeb2014-03-24 11:58:59 +0800505
Ben Hutchings2c530402012-07-10 10:55:09 +0000506/**
Chuck Leverc2866762006-08-22 20:06:20 -0400507 * rpc_create - create an RPC client and transport with one call
508 * @args: rpc_clnt create argument structure
509 *
510 * Creates and initializes an RPC transport and an RPC client.
511 *
512 * It can ping the server in order to determine if it is up, and to see if
513 * it supports this program and version. RPC_CLNT_CREATE_NOPING disables
514 * this behavior so asynchronous tasks can also use rpc_create.
515 */
516struct rpc_clnt *rpc_create(struct rpc_create_args *args)
517{
518 struct rpc_xprt *xprt;
\"Talpey, Thomas\3c341b0b2007-09-10 13:47:07 -0400519 struct xprt_create xprtargs = {
Pavel Emelyanov9a23e332010-09-29 16:05:12 +0400520 .net = args->net,
\"Talpey, Thomas\4fa016e2007-09-10 13:47:57 -0400521 .ident = args->protocol,
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +0200522 .srcaddr = args->saddress,
Frank van Maarseveen96802a02007-07-08 13:08:54 +0200523 .dstaddr = args->address,
524 .addrlen = args->addrsize,
Trond Myklebust4e0038b2012-03-01 17:01:05 -0500525 .servername = args->servername,
Alexandros Batsakisf300bab2009-09-10 17:33:30 +0300526 .bc_xprt = args->bc_xprt,
Frank van Maarseveen96802a02007-07-08 13:08:54 +0200527 };
Chuck Lever510deb02007-12-10 14:56:24 -0500528 char servername[48];
Trond Myklebust612b41f2017-04-27 08:50:51 -0400529 struct rpc_clnt *clnt;
530 int i;
Chuck Leverc2866762006-08-22 20:06:20 -0400531
J. Bruce Fieldsd50039e2016-05-16 17:03:42 -0400532 if (args->bc_xprt) {
Chuck Lever16590a22016-08-22 14:57:42 -0400533 WARN_ON_ONCE(!(args->protocol & XPRT_TRANSPORT_BC));
J. Bruce Fieldsd50039e2016-05-16 17:03:42 -0400534 xprt = args->bc_xprt->xpt_bc_xprt;
535 if (xprt) {
536 xprt_get(xprt);
537 return rpc_create_xprt(args, xprt);
538 }
539 }
540
Trond Myklebustb7993ce2013-04-14 11:42:00 -0400541 if (args->flags & RPC_CLNT_CREATE_INFINITE_SLOTS)
542 xprtargs.flags |= XPRT_CREATE_INFINITE_SLOTS;
J. Bruce Fields33d90ac2013-04-11 15:06:36 -0400543 if (args->flags & RPC_CLNT_CREATE_NO_IDLE_TIMEOUT)
544 xprtargs.flags |= XPRT_CREATE_NO_IDLE_TIMEOUT;
Chuck Leverc2866762006-08-22 20:06:20 -0400545 /*
Chuck Lever43780b82007-07-01 12:13:22 -0400546 * If the caller chooses not to specify a hostname, whip
547 * up a string representation of the passed-in address.
548 */
Trond Myklebust4e0038b2012-03-01 17:01:05 -0500549 if (xprtargs.servername == NULL) {
Chuck Lever176e21e2011-05-09 15:22:44 -0400550 struct sockaddr_un *sun =
551 (struct sockaddr_un *)args->address;
Chuck Leverda09eb92011-05-09 15:22:25 -0400552 struct sockaddr_in *sin =
553 (struct sockaddr_in *)args->address;
554 struct sockaddr_in6 *sin6 =
555 (struct sockaddr_in6 *)args->address;
556
Chuck Lever510deb02007-12-10 14:56:24 -0500557 servername[0] = '\0';
558 switch (args->address->sa_family) {
Chuck Lever176e21e2011-05-09 15:22:44 -0400559 case AF_LOCAL:
560 snprintf(servername, sizeof(servername), "%s",
561 sun->sun_path);
562 break;
Chuck Leverda09eb92011-05-09 15:22:25 -0400563 case AF_INET:
Harvey Harrison21454aa2008-10-31 00:54:56 -0700564 snprintf(servername, sizeof(servername), "%pI4",
565 &sin->sin_addr.s_addr);
Chuck Lever510deb02007-12-10 14:56:24 -0500566 break;
Chuck Leverda09eb92011-05-09 15:22:25 -0400567 case AF_INET6:
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700568 snprintf(servername, sizeof(servername), "%pI6",
Chuck Leverda09eb92011-05-09 15:22:25 -0400569 &sin6->sin6_addr);
Chuck Lever510deb02007-12-10 14:56:24 -0500570 break;
Chuck Lever510deb02007-12-10 14:56:24 -0500571 default:
572 /* caller wants default server name, but
573 * address family isn't recognized. */
574 return ERR_PTR(-EINVAL);
575 }
Trond Myklebust4e0038b2012-03-01 17:01:05 -0500576 xprtargs.servername = servername;
Chuck Lever43780b82007-07-01 12:13:22 -0400577 }
578
Chuck Lever510deb02007-12-10 14:56:24 -0500579 xprt = xprt_create_transport(&xprtargs);
580 if (IS_ERR(xprt))
581 return (struct rpc_clnt *)xprt;
582
Chuck Lever43780b82007-07-01 12:13:22 -0400583 /*
Chuck Leverc2866762006-08-22 20:06:20 -0400584 * By default, kernel RPC client connects from a reserved port.
585 * CAP_NET_BIND_SERVICE will not be set for unprivileged requesters,
586 * but it is always enabled for rpciod, which handles the connect
587 * operation.
588 */
589 xprt->resvport = 1;
590 if (args->flags & RPC_CLNT_CREATE_NONPRIVPORT)
591 xprt->resvport = 0;
Trond Myklebuste6237b62019-10-17 11:13:54 -0400592 xprt->reuseport = 0;
593 if (args->flags & RPC_CLNT_CREATE_REUSEPORT)
594 xprt->reuseport = 1;
Chuck Leverc2866762006-08-22 20:06:20 -0400595
Trond Myklebust612b41f2017-04-27 08:50:51 -0400596 clnt = rpc_create_xprt(args, xprt);
597 if (IS_ERR(clnt) || args->nconnect <= 1)
598 return clnt;
599
600 for (i = 0; i < args->nconnect - 1; i++) {
601 if (rpc_clnt_add_xprt(clnt, &xprtargs, NULL, NULL) < 0)
602 break;
603 }
604 return clnt;
Chuck Leverc2866762006-08-22 20:06:20 -0400605}
Chuck Leverb86acd52006-08-22 20:06:22 -0400606EXPORT_SYMBOL_GPL(rpc_create);
Chuck Leverc2866762006-08-22 20:06:20 -0400607
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608/*
609 * This function clones the RPC client structure. It allows us to share the
610 * same transport while varying parameters such as the authentication
611 * flavour.
612 */
Chuck Lever1b63a752012-09-14 17:23:52 -0400613static struct rpc_clnt *__rpc_clone_client(struct rpc_create_args *args,
614 struct rpc_clnt *clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615{
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500616 struct rpc_xprt_switch *xps;
Trond Myklebust2446ab62012-03-01 17:00:56 -0500617 struct rpc_xprt *xprt;
Chuck Lever1b63a752012-09-14 17:23:52 -0400618 struct rpc_clnt *new;
619 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
Chuck Lever1b63a752012-09-14 17:23:52 -0400621 err = -ENOMEM;
Trond Myklebust2446ab62012-03-01 17:00:56 -0500622 rcu_read_lock();
623 xprt = xprt_get(rcu_dereference(clnt->cl_xprt));
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500624 xps = xprt_switch_get(rcu_dereference(clnt->cl_xpi.xpi_xpswitch));
Trond Myklebust2446ab62012-03-01 17:00:56 -0500625 rcu_read_unlock();
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500626 if (xprt == NULL || xps == NULL) {
627 xprt_put(xprt);
628 xprt_switch_put(xps);
Chuck Lever1b63a752012-09-14 17:23:52 -0400629 goto out_err;
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500630 }
Chuck Lever1b63a752012-09-14 17:23:52 -0400631 args->servername = xprt->servername;
Trond Myklebust03a9a422015-01-30 18:12:28 -0500632 args->nodename = clnt->cl_nodename;
Chuck Lever1b63a752012-09-14 17:23:52 -0400633
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500634 new = rpc_new_client(args, xps, xprt, clnt);
Chuck Lever42aad0d2020-05-12 17:13:39 -0400635 if (IS_ERR(new))
636 return new;
Chuck Lever1b63a752012-09-14 17:23:52 -0400637
Chuck Lever1b63a752012-09-14 17:23:52 -0400638 /* Turn off autobind on clones */
639 new->cl_autobind = 0;
640 new->cl_softrtry = clnt->cl_softrtry;
Trond Myklebustae6ec912019-04-07 13:58:58 -0400641 new->cl_softerr = clnt->cl_softerr;
Trond Myklebust2aca5b82014-09-24 22:35:58 -0400642 new->cl_noretranstimeo = clnt->cl_noretranstimeo;
Chuck Lever1b63a752012-09-14 17:23:52 -0400643 new->cl_discrtry = clnt->cl_discrtry;
644 new->cl_chatty = clnt->cl_chatty;
NeilBrown5e169232018-12-03 11:30:30 +1100645 new->cl_principal = clnt->cl_principal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 return new;
Chuck Lever1b63a752012-09-14 17:23:52 -0400647
Chuck Lever1b63a752012-09-14 17:23:52 -0400648out_err:
Chuck Lever42aad0d2020-05-12 17:13:39 -0400649 trace_rpc_clnt_clone_err(clnt, err);
Trond Myklebust3e32a5d2006-11-16 11:37:27 -0500650 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651}
Chuck Lever1b63a752012-09-14 17:23:52 -0400652
653/**
654 * rpc_clone_client - Clone an RPC client structure
655 *
656 * @clnt: RPC client whose parameters are copied
657 *
658 * Returns a fresh RPC client or an ERR_PTR.
659 */
660struct rpc_clnt *rpc_clone_client(struct rpc_clnt *clnt)
661{
662 struct rpc_create_args args = {
663 .program = clnt->cl_program,
664 .prognumber = clnt->cl_prog,
665 .version = clnt->cl_vers,
666 .authflavor = clnt->cl_auth->au_flavor,
Trond Myklebust79caa5f2019-04-24 17:46:42 -0400667 .cred = clnt->cl_cred,
Chuck Lever1b63a752012-09-14 17:23:52 -0400668 };
669 return __rpc_clone_client(&args, clnt);
670}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400671EXPORT_SYMBOL_GPL(rpc_clone_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
Chuck Leverba9b5842012-09-14 17:24:02 -0400673/**
674 * rpc_clone_client_set_auth - Clone an RPC client structure and set its auth
675 *
676 * @clnt: RPC client whose parameters are copied
Randy Dunlap7144bca2013-01-09 17:12:35 -0800677 * @flavor: security flavor for new client
Chuck Leverba9b5842012-09-14 17:24:02 -0400678 *
679 * Returns a fresh RPC client or an ERR_PTR.
680 */
681struct rpc_clnt *
682rpc_clone_client_set_auth(struct rpc_clnt *clnt, rpc_authflavor_t flavor)
683{
684 struct rpc_create_args args = {
685 .program = clnt->cl_program,
686 .prognumber = clnt->cl_prog,
687 .version = clnt->cl_vers,
688 .authflavor = flavor,
Trond Myklebust79caa5f2019-04-24 17:46:42 -0400689 .cred = clnt->cl_cred,
Chuck Leverba9b5842012-09-14 17:24:02 -0400690 };
691 return __rpc_clone_client(&args, clnt);
692}
693EXPORT_SYMBOL_GPL(rpc_clone_client_set_auth);
694
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400695/**
696 * rpc_switch_client_transport: switch the RPC transport on the fly
697 * @clnt: pointer to a struct rpc_clnt
698 * @args: pointer to the new transport arguments
699 * @timeout: pointer to the new timeout parameters
700 *
701 * This function allows the caller to switch the RPC transport for the
702 * rpc_clnt structure 'clnt' to allow it to connect to a mirrored NFS
703 * server, for instance. It assumes that the caller has ensured that
704 * there are no active RPC tasks by using some form of locking.
705 *
706 * Returns zero if "clnt" is now using the new xprt. Otherwise a
707 * negative errno is returned, and "clnt" continues to use the old
708 * xprt.
709 */
710int rpc_switch_client_transport(struct rpc_clnt *clnt,
711 struct xprt_create *args,
712 const struct rpc_timeout *timeout)
713{
714 const struct rpc_timeout *old_timeo;
715 rpc_authflavor_t pseudoflavor;
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500716 struct rpc_xprt_switch *xps, *oldxps;
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400717 struct rpc_xprt *xprt, *old;
718 struct rpc_clnt *parent;
719 int err;
720
721 xprt = xprt_create_transport(args);
Chuck Lever42aad0d2020-05-12 17:13:39 -0400722 if (IS_ERR(xprt))
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400723 return PTR_ERR(xprt);
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400724
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500725 xps = xprt_switch_alloc(xprt, GFP_KERNEL);
726 if (xps == NULL) {
727 xprt_put(xprt);
728 return -ENOMEM;
729 }
730
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400731 pseudoflavor = clnt->cl_auth->au_flavor;
732
733 old_timeo = clnt->cl_timeout;
734 old = rpc_clnt_set_transport(clnt, xprt, timeout);
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500735 oldxps = xprt_iter_xchg_switch(&clnt->cl_xpi, xps);
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400736
737 rpc_unregister_client(clnt);
738 __rpc_clnt_remove_pipedir(clnt);
Olga Kornievskaiac5a382e2021-06-08 15:59:12 -0400739 rpc_sysfs_client_destroy(clnt);
Jeff Laytonb4b9d2c2014-11-26 14:44:43 -0500740 rpc_clnt_debugfs_unregister(clnt);
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400741
742 /*
743 * A new transport was created. "clnt" therefore
744 * becomes the root of a new cl_parent tree. clnt's
745 * children, if it has any, still point to the old xprt.
746 */
747 parent = clnt->cl_parent;
748 clnt->cl_parent = clnt;
749
750 /*
751 * The old rpc_auth cache cannot be re-used. GSS
752 * contexts in particular are between a single
753 * client and server.
754 */
755 err = rpc_client_register(clnt, pseudoflavor, NULL);
756 if (err)
757 goto out_revert;
758
759 synchronize_rcu();
760 if (parent != clnt)
761 rpc_release_client(parent);
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500762 xprt_switch_put(oldxps);
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400763 xprt_put(old);
Chuck Lever42aad0d2020-05-12 17:13:39 -0400764 trace_rpc_clnt_replace_xprt(clnt);
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400765 return 0;
766
767out_revert:
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500768 xps = xprt_iter_xchg_switch(&clnt->cl_xpi, oldxps);
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400769 rpc_clnt_set_transport(clnt, old, old_timeo);
770 clnt->cl_parent = parent;
771 rpc_client_register(clnt, pseudoflavor, NULL);
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500772 xprt_switch_put(xps);
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400773 xprt_put(xprt);
Chuck Lever42aad0d2020-05-12 17:13:39 -0400774 trace_rpc_clnt_replace_xprt_err(clnt);
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400775 return err;
776}
777EXPORT_SYMBOL_GPL(rpc_switch_client_transport);
778
Trond Myklebust32278862016-01-30 20:39:19 -0500779static
780int rpc_clnt_xprt_iter_init(struct rpc_clnt *clnt, struct rpc_xprt_iter *xpi)
781{
782 struct rpc_xprt_switch *xps;
783
784 rcu_read_lock();
785 xps = xprt_switch_get(rcu_dereference(clnt->cl_xpi.xpi_xpswitch));
786 rcu_read_unlock();
787 if (xps == NULL)
788 return -EAGAIN;
789 xprt_iter_init_listall(xpi, xps);
790 xprt_switch_put(xps);
791 return 0;
792}
793
794/**
795 * rpc_clnt_iterate_for_each_xprt - Apply a function to all transports
796 * @clnt: pointer to client
797 * @fn: function to apply
798 * @data: void pointer to function data
799 *
800 * Iterates through the list of RPC transports currently attached to the
801 * client and applies the function fn(clnt, xprt, data).
802 *
803 * On error, the iteration stops, and the function returns the error value.
804 */
805int rpc_clnt_iterate_for_each_xprt(struct rpc_clnt *clnt,
806 int (*fn)(struct rpc_clnt *, struct rpc_xprt *, void *),
807 void *data)
808{
809 struct rpc_xprt_iter xpi;
810 int ret;
811
812 ret = rpc_clnt_xprt_iter_init(clnt, &xpi);
813 if (ret)
814 return ret;
815 for (;;) {
816 struct rpc_xprt *xprt = xprt_iter_get_next(&xpi);
817
818 if (!xprt)
819 break;
820 ret = fn(clnt, xprt, data);
821 xprt_put(xprt);
822 if (ret < 0)
823 break;
824 }
825 xprt_iter_destroy(&xpi);
826 return ret;
827}
828EXPORT_SYMBOL_GPL(rpc_clnt_iterate_for_each_xprt);
829
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830/*
Trond Myklebust58f96122010-07-31 14:29:08 -0400831 * Kill all tasks for the given client.
832 * XXX: kill their descendants as well?
833 */
834void rpc_killall_tasks(struct rpc_clnt *clnt)
835{
836 struct rpc_task *rovr;
837
838
839 if (list_empty(&clnt->cl_tasks))
840 return;
Chuck Lever42aad0d2020-05-12 17:13:39 -0400841
Trond Myklebust58f96122010-07-31 14:29:08 -0400842 /*
843 * Spin lock all_tasks to prevent changes...
844 */
Chuck Lever42aad0d2020-05-12 17:13:39 -0400845 trace_rpc_clnt_killall(clnt);
Trond Myklebust58f96122010-07-31 14:29:08 -0400846 spin_lock(&clnt->cl_lock);
Trond Myklebustae67bd32019-04-07 13:58:44 -0400847 list_for_each_entry(rovr, &clnt->cl_tasks, tk_task)
848 rpc_signal_task(rovr);
Trond Myklebust58f96122010-07-31 14:29:08 -0400849 spin_unlock(&clnt->cl_lock);
850}
851EXPORT_SYMBOL_GPL(rpc_killall_tasks);
852
853/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 * Properly shut down an RPC client, terminating all outstanding
Trond Myklebust90c57552007-06-09 19:49:36 -0400855 * requests.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 */
Trond Myklebust4c402b42007-06-14 16:40:32 -0400857void rpc_shutdown_client(struct rpc_clnt *clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858{
Weston Andros Adamson168e4b32012-10-30 17:01:40 -0400859 might_sleep();
860
Chuck Lever42aad0d2020-05-12 17:13:39 -0400861 trace_rpc_clnt_shutdown(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
Trond Myklebust34f52e32007-06-14 16:40:31 -0400863 while (!list_empty(&clnt->cl_tasks)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 rpc_killall_tasks(clnt);
Ingo Molnar532347e2006-01-09 20:52:53 -0800865 wait_event_timeout(destroy_wait,
Trond Myklebust34f52e32007-06-14 16:40:31 -0400866 list_empty(&clnt->cl_tasks), 1*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 }
868
Trond Myklebust4c402b42007-06-14 16:40:32 -0400869 rpc_release_client(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400871EXPORT_SYMBOL_GPL(rpc_shutdown_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
873/*
Trond Myklebust34f52e32007-06-14 16:40:31 -0400874 * Free an RPC client
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 */
NeilBrown7c4310f2020-04-03 14:33:41 +1100876static void rpc_free_client_work(struct work_struct *work)
877{
878 struct rpc_clnt *clnt = container_of(work, struct rpc_clnt, cl_work);
879
Chuck Lever42aad0d2020-05-12 17:13:39 -0400880 trace_rpc_clnt_free(clnt);
881
NeilBrown7c4310f2020-04-03 14:33:41 +1100882 /* These might block on processes that might allocate memory,
883 * so they cannot be called in rpciod, so they are handled separately
884 * here.
885 */
Olga Kornievskaiac5a382e2021-06-08 15:59:12 -0400886 rpc_sysfs_client_destroy(clnt);
NeilBrown7c4310f2020-04-03 14:33:41 +1100887 rpc_clnt_debugfs_unregister(clnt);
J. Bruce Fields933496e2020-05-13 18:38:40 -0400888 rpc_free_clid(clnt);
NeilBrown7c4310f2020-04-03 14:33:41 +1100889 rpc_clnt_remove_pipedir(clnt);
NeilBrown31e9a7f2020-05-09 07:25:11 +1000890 xprt_put(rcu_dereference_raw(clnt->cl_xprt));
NeilBrown7c4310f2020-04-03 14:33:41 +1100891
892 kfree(clnt);
893 rpciod_down();
894}
Trond Myklebustd07ba842013-11-12 17:24:36 -0500895static struct rpc_clnt *
Trond Myklebust006abe82010-09-12 19:55:25 -0400896rpc_free_client(struct rpc_clnt *clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897{
Trond Myklebustd07ba842013-11-12 17:24:36 -0500898 struct rpc_clnt *parent = NULL;
899
Chuck Lever42aad0d2020-05-12 17:13:39 -0400900 trace_rpc_clnt_release(clnt);
Trond Myklebust6eac7d32012-01-20 13:53:37 -0500901 if (clnt->cl_parent != clnt)
Trond Myklebustd07ba842013-11-12 17:24:36 -0500902 parent = clnt->cl_parent;
Stanislav Kinsburskyadb6fa72013-06-26 10:15:14 +0400903 rpc_unregister_client(clnt);
Chuck Lever11c556b2006-03-20 13:44:22 -0500904 rpc_free_iostats(clnt->cl_metrics);
905 clnt->cl_metrics = NULL;
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500906 xprt_iter_destroy(&clnt->cl_xpi);
Trond Myklebust79caa5f2019-04-24 17:46:42 -0400907 put_cred(clnt->cl_cred);
NeilBrown7c4310f2020-04-03 14:33:41 +1100908
909 INIT_WORK(&clnt->cl_work, rpc_free_client_work);
910 schedule_work(&clnt->cl_work);
Trond Myklebustd07ba842013-11-12 17:24:36 -0500911 return parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912}
913
914/*
Trond Myklebust1dd17ec2007-06-26 16:57:41 -0400915 * Free an RPC client
916 */
Stephen Hemminger8fdee4c2018-07-24 12:29:15 -0700917static struct rpc_clnt *
Trond Myklebust006abe82010-09-12 19:55:25 -0400918rpc_free_auth(struct rpc_clnt *clnt)
Trond Myklebust1dd17ec2007-06-26 16:57:41 -0400919{
Trond Myklebustd07ba842013-11-12 17:24:36 -0500920 if (clnt->cl_auth == NULL)
921 return rpc_free_client(clnt);
Trond Myklebust1dd17ec2007-06-26 16:57:41 -0400922
923 /*
924 * Note: RPCSEC_GSS may need to send NULL RPC calls in order to
925 * release remaining GSS contexts. This mechanism ensures
926 * that it can do so safely.
927 */
Trond Myklebust006abe82010-09-12 19:55:25 -0400928 atomic_inc(&clnt->cl_count);
Trond Myklebust1dd17ec2007-06-26 16:57:41 -0400929 rpcauth_release(clnt->cl_auth);
930 clnt->cl_auth = NULL;
Trond Myklebust006abe82010-09-12 19:55:25 -0400931 if (atomic_dec_and_test(&clnt->cl_count))
Trond Myklebustd07ba842013-11-12 17:24:36 -0500932 return rpc_free_client(clnt);
933 return NULL;
Trond Myklebust1dd17ec2007-06-26 16:57:41 -0400934}
935
936/*
Trond Myklebust34f52e32007-06-14 16:40:31 -0400937 * Release reference to the RPC client
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 */
939void
940rpc_release_client(struct rpc_clnt *clnt)
941{
Trond Myklebustd07ba842013-11-12 17:24:36 -0500942 do {
943 if (list_empty(&clnt->cl_tasks))
944 wake_up(&destroy_wait);
945 if (!atomic_dec_and_test(&clnt->cl_count))
946 break;
947 clnt = rpc_free_auth(clnt);
948 } while (clnt != NULL);
Trond Myklebust34f52e32007-06-14 16:40:31 -0400949}
Simo Sorce1d658332012-05-25 18:09:55 -0400950EXPORT_SYMBOL_GPL(rpc_release_client);
Trond Myklebust34f52e32007-06-14 16:40:31 -0400951
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000952/**
953 * rpc_bind_new_program - bind a new RPC program to an existing client
Randy Dunlap65b6e422008-02-13 15:03:23 -0800954 * @old: old rpc_client
955 * @program: rpc program to set
956 * @vers: rpc program version
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000957 *
958 * Clones the rpc client and sets up a new RPC program. This is mainly
959 * of use for enabling different RPC programs to share the same transport.
960 * The Sun NFSv2/v3 ACL protocol can do this.
961 */
962struct rpc_clnt *rpc_bind_new_program(struct rpc_clnt *old,
Trond Myklebusta613fa12012-01-20 13:53:56 -0500963 const struct rpc_program *program,
Chuck Lever89eb21c2007-09-11 18:00:09 -0400964 u32 vers)
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000965{
Trond Myklebustf994c432012-11-01 12:14:14 -0400966 struct rpc_create_args args = {
967 .program = program,
968 .prognumber = program->number,
969 .version = vers,
970 .authflavor = old->cl_auth->au_flavor,
Trond Myklebust79caa5f2019-04-24 17:46:42 -0400971 .cred = old->cl_cred,
Trond Myklebustf994c432012-11-01 12:14:14 -0400972 };
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000973 struct rpc_clnt *clnt;
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000974 int err;
975
Trond Myklebustf994c432012-11-01 12:14:14 -0400976 clnt = __rpc_clone_client(&args, old);
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000977 if (IS_ERR(clnt))
978 goto out;
Chuck Levercaabea82009-12-03 15:58:56 -0500979 err = rpc_ping(clnt);
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000980 if (err != 0) {
981 rpc_shutdown_client(clnt);
982 clnt = ERR_PTR(err);
983 }
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800984out:
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000985 return clnt;
986}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400987EXPORT_SYMBOL_GPL(rpc_bind_new_program);
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000988
Trond Myklebusta101b042019-07-11 16:33:12 -0400989struct rpc_xprt *
990rpc_task_get_xprt(struct rpc_clnt *clnt, struct rpc_xprt *xprt)
Trond Myklebust21f0ffa2017-04-28 10:52:42 -0400991{
992 struct rpc_xprt_switch *xps;
Trond Myklebust21f0ffa2017-04-28 10:52:42 -0400993
994 if (!xprt)
995 return NULL;
996 rcu_read_lock();
997 xps = rcu_dereference(clnt->cl_xpi.xpi_xpswitch);
998 atomic_long_inc(&xps->xps_queuelen);
999 rcu_read_unlock();
1000 atomic_long_inc(&xprt->queuelen);
1001
1002 return xprt;
1003}
1004
1005static void
1006rpc_task_release_xprt(struct rpc_clnt *clnt, struct rpc_xprt *xprt)
1007{
1008 struct rpc_xprt_switch *xps;
1009
1010 atomic_long_dec(&xprt->queuelen);
1011 rcu_read_lock();
1012 xps = rcu_dereference(clnt->cl_xpi.xpi_xpswitch);
1013 atomic_long_dec(&xps->xps_queuelen);
1014 rcu_read_unlock();
1015
1016 xprt_put(xprt);
1017}
1018
Bill Baker0f90be12018-06-19 16:24:58 -05001019void rpc_task_release_transport(struct rpc_task *task)
1020{
1021 struct rpc_xprt *xprt = task->tk_xprt;
1022
1023 if (xprt) {
1024 task->tk_xprt = NULL;
Trond Myklebust21f0ffa2017-04-28 10:52:42 -04001025 if (task->tk_client)
1026 rpc_task_release_xprt(task->tk_client, xprt);
1027 else
1028 xprt_put(xprt);
Bill Baker0f90be12018-06-19 16:24:58 -05001029 }
1030}
1031EXPORT_SYMBOL_GPL(rpc_task_release_transport);
1032
Trond Myklebust58f96122010-07-31 14:29:08 -04001033void rpc_task_release_client(struct rpc_task *task)
1034{
1035 struct rpc_clnt *clnt = task->tk_client;
1036
Trond Myklebust21f0ffa2017-04-28 10:52:42 -04001037 rpc_task_release_transport(task);
Trond Myklebust58f96122010-07-31 14:29:08 -04001038 if (clnt != NULL) {
1039 /* Remove from client task list */
1040 spin_lock(&clnt->cl_lock);
1041 list_del(&task->tk_task);
1042 spin_unlock(&clnt->cl_lock);
1043 task->tk_client = NULL;
1044
1045 rpc_release_client(clnt);
1046 }
Bill Baker0f90be12018-06-19 16:24:58 -05001047}
Trond Myklebustfb43d172016-01-30 16:39:26 -05001048
Trond Myklebusta101b042019-07-11 16:33:12 -04001049static struct rpc_xprt *
1050rpc_task_get_first_xprt(struct rpc_clnt *clnt)
1051{
1052 struct rpc_xprt *xprt;
1053
1054 rcu_read_lock();
1055 xprt = xprt_get(rcu_dereference(clnt->cl_xprt));
1056 rcu_read_unlock();
1057 return rpc_task_get_xprt(clnt, xprt);
1058}
1059
1060static struct rpc_xprt *
1061rpc_task_get_next_xprt(struct rpc_clnt *clnt)
1062{
1063 return rpc_task_get_xprt(clnt, xprt_iter_get_next(&clnt->cl_xpi));
1064}
1065
Bill Baker0f90be12018-06-19 16:24:58 -05001066static
1067void rpc_task_set_transport(struct rpc_task *task, struct rpc_clnt *clnt)
1068{
NeilBrown5a0c2572019-05-30 10:41:28 +10001069 if (task->tk_xprt)
1070 return;
1071 if (task->tk_flags & RPC_TASK_NO_ROUND_ROBIN)
1072 task->tk_xprt = rpc_task_get_first_xprt(clnt);
1073 else
Trond Myklebusta101b042019-07-11 16:33:12 -04001074 task->tk_xprt = rpc_task_get_next_xprt(clnt);
Trond Myklebust58f96122010-07-31 14:29:08 -04001075}
1076
1077static
1078void rpc_task_set_client(struct rpc_task *task, struct rpc_clnt *clnt)
1079{
Trond Myklebustfb43d172016-01-30 16:39:26 -05001080
Trond Myklebust58f96122010-07-31 14:29:08 -04001081 if (clnt != NULL) {
Bill Baker0f90be12018-06-19 16:24:58 -05001082 rpc_task_set_transport(task, clnt);
Trond Myklebust58f96122010-07-31 14:29:08 -04001083 task->tk_client = clnt;
Trond Myklebust006abe82010-09-12 19:55:25 -04001084 atomic_inc(&clnt->cl_count);
Trond Myklebust58f96122010-07-31 14:29:08 -04001085 if (clnt->cl_softrtry)
1086 task->tk_flags |= RPC_TASK_SOFT;
Trond Myklebustae6ec912019-04-07 13:58:58 -04001087 if (clnt->cl_softerr)
1088 task->tk_flags |= RPC_TASK_TIMEOUT;
Trond Myklebust8a19a0b2013-09-24 12:00:27 -04001089 if (clnt->cl_noretranstimeo)
1090 task->tk_flags |= RPC_TASK_NO_RETRANS_TIMEOUT;
Jeff Layton3c87ef62015-06-03 16:14:25 -04001091 if (atomic_read(&clnt->cl_swapper))
1092 task->tk_flags |= RPC_TASK_SWAPPER;
Trond Myklebust58f96122010-07-31 14:29:08 -04001093 /* Add to the client's list of all tasks */
1094 spin_lock(&clnt->cl_lock);
1095 list_add_tail(&task->tk_task, &clnt->cl_tasks);
1096 spin_unlock(&clnt->cl_lock);
1097 }
1098}
1099
1100static void
1101rpc_task_set_rpc_message(struct rpc_task *task, const struct rpc_message *msg)
1102{
1103 if (msg != NULL) {
1104 task->tk_msg.rpc_proc = msg->rpc_proc;
1105 task->tk_msg.rpc_argp = msg->rpc_argp;
1106 task->tk_msg.rpc_resp = msg->rpc_resp;
Trond Myklebust7eac5262020-02-07 19:11:12 -05001107 task->tk_msg.rpc_cred = msg->rpc_cred;
1108 if (!(task->tk_flags & RPC_TASK_CRED_NOREF))
1109 get_cred(task->tk_msg.rpc_cred);
Trond Myklebust58f96122010-07-31 14:29:08 -04001110 }
1111}
1112
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113/*
1114 * Default callback for async RPC calls
1115 */
1116static void
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001117rpc_default_callback(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118{
1119}
1120
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001121static const struct rpc_call_ops rpc_default_ops = {
1122 .rpc_call_done = rpc_default_callback,
1123};
1124
Trond Myklebustc970aa82007-07-14 15:39:59 -04001125/**
1126 * rpc_run_task - Allocate a new RPC task, then run rpc_execute against it
1127 * @task_setup_data: pointer to task initialisation data
1128 */
1129struct rpc_task *rpc_run_task(const struct rpc_task_setup *task_setup_data)
Trond Myklebust6e5b70e2007-06-12 10:02:37 -04001130{
Trond Myklebust19445b92010-04-16 16:41:10 -04001131 struct rpc_task *task;
Trond Myklebust6e5b70e2007-06-12 10:02:37 -04001132
Trond Myklebust84115e12007-07-14 15:39:59 -04001133 task = rpc_new_task(task_setup_data);
Trond Myklebust6e5b70e2007-06-12 10:02:37 -04001134
Trond Myklebust263fb9c2020-02-07 19:16:34 -05001135 if (!RPC_IS_ASYNC(task))
1136 task->tk_flags |= RPC_TASK_CRED_NOREF;
1137
Trond Myklebust58f96122010-07-31 14:29:08 -04001138 rpc_task_set_client(task, task_setup_data->rpc_client);
1139 rpc_task_set_rpc_message(task, task_setup_data->rpc_message);
1140
Trond Myklebust58f96122010-07-31 14:29:08 -04001141 if (task->tk_action == NULL)
1142 rpc_call_start(task);
1143
Trond Myklebust6e5b70e2007-06-12 10:02:37 -04001144 atomic_inc(&task->tk_count);
1145 rpc_execute(task);
Trond Myklebust19445b92010-04-16 16:41:10 -04001146 return task;
Trond Myklebust6e5b70e2007-06-12 10:02:37 -04001147}
Trond Myklebustc970aa82007-07-14 15:39:59 -04001148EXPORT_SYMBOL_GPL(rpc_run_task);
Trond Myklebust6e5b70e2007-06-12 10:02:37 -04001149
1150/**
1151 * rpc_call_sync - Perform a synchronous RPC call
1152 * @clnt: pointer to RPC client
1153 * @msg: RPC call parameters
1154 * @flags: RPC call flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 */
Trond Myklebustcbc20052008-02-14 11:11:30 -05001156int rpc_call_sync(struct rpc_clnt *clnt, const struct rpc_message *msg, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157{
1158 struct rpc_task *task;
Trond Myklebust84115e12007-07-14 15:39:59 -04001159 struct rpc_task_setup task_setup_data = {
1160 .rpc_client = clnt,
1161 .rpc_message = msg,
1162 .callback_ops = &rpc_default_ops,
1163 .flags = flags,
1164 };
Trond Myklebust6e5b70e2007-06-12 10:02:37 -04001165 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
Weston Andros Adamson50d2bdb2012-11-01 16:04:40 -04001167 WARN_ON_ONCE(flags & RPC_TASK_ASYNC);
1168 if (flags & RPC_TASK_ASYNC) {
1169 rpc_release_calldata(task_setup_data.callback_ops,
1170 task_setup_data.callback_data);
1171 return -EINVAL;
1172 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
Trond Myklebustc970aa82007-07-14 15:39:59 -04001174 task = rpc_run_task(&task_setup_data);
Trond Myklebust6e5b70e2007-06-12 10:02:37 -04001175 if (IS_ERR(task))
1176 return PTR_ERR(task);
Trond Myklebuste60859a2006-01-03 09:55:10 +01001177 status = task->tk_status;
Trond Myklebustbde8f002007-01-24 11:54:53 -08001178 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 return status;
1180}
Trond Myklebuste8914c62007-07-14 15:39:59 -04001181EXPORT_SYMBOL_GPL(rpc_call_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182
Trond Myklebust6e5b70e2007-06-12 10:02:37 -04001183/**
1184 * rpc_call_async - Perform an asynchronous RPC call
1185 * @clnt: pointer to RPC client
1186 * @msg: RPC call parameters
1187 * @flags: RPC call flags
Randy Dunlap65b6e422008-02-13 15:03:23 -08001188 * @tk_ops: RPC call ops
Trond Myklebust6e5b70e2007-06-12 10:02:37 -04001189 * @data: user call data
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 */
1191int
Trond Myklebustcbc20052008-02-14 11:11:30 -05001192rpc_call_async(struct rpc_clnt *clnt, const struct rpc_message *msg, int flags,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001193 const struct rpc_call_ops *tk_ops, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194{
1195 struct rpc_task *task;
Trond Myklebust84115e12007-07-14 15:39:59 -04001196 struct rpc_task_setup task_setup_data = {
1197 .rpc_client = clnt,
1198 .rpc_message = msg,
1199 .callback_ops = tk_ops,
1200 .callback_data = data,
1201 .flags = flags|RPC_TASK_ASYNC,
1202 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
Trond Myklebustc970aa82007-07-14 15:39:59 -04001204 task = rpc_run_task(&task_setup_data);
Trond Myklebust6e5b70e2007-06-12 10:02:37 -04001205 if (IS_ERR(task))
1206 return PTR_ERR(task);
1207 rpc_put_task(task);
1208 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209}
Trond Myklebuste8914c62007-07-14 15:39:59 -04001210EXPORT_SYMBOL_GPL(rpc_call_async);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001212#if defined(CONFIG_SUNRPC_BACKCHANNEL)
Trond Myklebust477687e2019-03-05 07:30:48 -05001213static void call_bc_encode(struct rpc_task *task);
1214
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001215/**
1216 * rpc_run_bc_task - Allocate a new RPC task for backchannel use, then run
1217 * rpc_execute against it
Jaswinder Singh Rajput7a73fdd2009-09-24 14:58:42 -04001218 * @req: RPC request
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001219 */
Trond Myklebust0f419792015-06-01 22:59:08 -04001220struct rpc_task *rpc_run_bc_task(struct rpc_rqst *req)
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001221{
1222 struct rpc_task *task;
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001223 struct rpc_task_setup task_setup_data = {
Trond Myklebust0f419792015-06-01 22:59:08 -04001224 .callback_ops = &rpc_default_ops,
Trond Myklebust762e4e62018-08-24 16:28:28 -04001225 .flags = RPC_TASK_SOFTCONN |
1226 RPC_TASK_NO_RETRANS_TIMEOUT,
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001227 };
1228
1229 dprintk("RPC: rpc_run_bc_task req= %p\n", req);
1230 /*
1231 * Create an rpc_task to send the data
1232 */
1233 task = rpc_new_task(&task_setup_data);
Trond Myklebust902c5882018-09-01 17:21:01 -04001234 xprt_init_bc_request(req, task);
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001235
Trond Myklebust477687e2019-03-05 07:30:48 -05001236 task->tk_action = call_bc_encode;
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001237 atomic_inc(&task->tk_count);
Weston Andros Adamson9a6478f2012-10-23 10:43:27 -04001238 WARN_ON_ONCE(atomic_read(&task->tk_count) != 2);
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001239 rpc_execute(task);
1240
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001241 dprintk("RPC: rpc_run_bc_task: task= %p\n", task);
1242 return task;
1243}
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001244#endif /* CONFIG_SUNRPC_BACKCHANNEL */
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001245
Chuck Levercf500ba2019-02-11 11:25:20 -05001246/**
1247 * rpc_prepare_reply_pages - Prepare to receive a reply data payload into pages
1248 * @req: RPC request to prepare
1249 * @pages: vector of struct page pointers
1250 * @base: offset in first page where receive should start, in bytes
1251 * @len: expected size of the upper layer data payload, in bytes
1252 * @hdrsize: expected size of upper layer reply header, in XDR words
1253 *
1254 */
1255void rpc_prepare_reply_pages(struct rpc_rqst *req, struct page **pages,
1256 unsigned int base, unsigned int len,
1257 unsigned int hdrsize)
1258{
Trond Myklebust9ed5af262020-11-21 20:46:18 -05001259 hdrsize += RPC_REPHDRSIZE + req->rq_cred->cr_auth->au_ralign;
Chuck Lever02ef04e2019-02-11 11:25:25 -05001260
Chuck Levercf500ba2019-02-11 11:25:20 -05001261 xdr_inline_pages(&req->rq_rcv_buf, hdrsize << 2, pages, base, len);
Chuck Leverc509f15a2020-05-12 17:13:28 -04001262 trace_rpc_xdr_reply_pages(req->rq_task, &req->rq_rcv_buf);
Chuck Levercf500ba2019-02-11 11:25:20 -05001263}
1264EXPORT_SYMBOL_GPL(rpc_prepare_reply_pages);
1265
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266void
Trond Myklebust77de2c52007-10-25 18:40:21 -04001267rpc_call_start(struct rpc_task *task)
1268{
1269 task->tk_action = call_start;
1270}
1271EXPORT_SYMBOL_GPL(rpc_call_start);
1272
Chuck Levered394402006-08-22 20:06:17 -04001273/**
1274 * rpc_peeraddr - extract remote peer address from clnt's xprt
1275 * @clnt: RPC client structure
1276 * @buf: target buffer
Randy Dunlap65b6e422008-02-13 15:03:23 -08001277 * @bufsize: length of target buffer
Chuck Levered394402006-08-22 20:06:17 -04001278 *
1279 * Returns the number of bytes that are actually in the stored address.
1280 */
1281size_t rpc_peeraddr(struct rpc_clnt *clnt, struct sockaddr *buf, size_t bufsize)
1282{
1283 size_t bytes;
Trond Myklebust2446ab62012-03-01 17:00:56 -05001284 struct rpc_xprt *xprt;
Chuck Levered394402006-08-22 20:06:17 -04001285
Trond Myklebust2446ab62012-03-01 17:00:56 -05001286 rcu_read_lock();
1287 xprt = rcu_dereference(clnt->cl_xprt);
1288
1289 bytes = xprt->addrlen;
Chuck Levered394402006-08-22 20:06:17 -04001290 if (bytes > bufsize)
1291 bytes = bufsize;
Trond Myklebust2446ab62012-03-01 17:00:56 -05001292 memcpy(buf, &xprt->addr, bytes);
1293 rcu_read_unlock();
1294
1295 return bytes;
Chuck Levered394402006-08-22 20:06:17 -04001296}
Chuck Leverb86acd52006-08-22 20:06:22 -04001297EXPORT_SYMBOL_GPL(rpc_peeraddr);
Chuck Levered394402006-08-22 20:06:17 -04001298
Chuck Leverf425eba2006-08-22 20:06:18 -04001299/**
1300 * rpc_peeraddr2str - return remote peer address in printable format
1301 * @clnt: RPC client structure
1302 * @format: address format
1303 *
Trond Myklebust2446ab62012-03-01 17:00:56 -05001304 * NB: the lifetime of the memory referenced by the returned pointer is
1305 * the same as the rpc_xprt itself. As long as the caller uses this
1306 * pointer, it must hold the RCU read lock.
Chuck Leverf425eba2006-08-22 20:06:18 -04001307 */
Chuck Leverb454ae92008-01-07 18:34:48 -05001308const char *rpc_peeraddr2str(struct rpc_clnt *clnt,
1309 enum rpc_display_format_t format)
Chuck Leverf425eba2006-08-22 20:06:18 -04001310{
Trond Myklebust2446ab62012-03-01 17:00:56 -05001311 struct rpc_xprt *xprt;
1312
1313 xprt = rcu_dereference(clnt->cl_xprt);
Chuck Lever7559c7a2006-12-05 16:35:37 -05001314
1315 if (xprt->address_strings[format] != NULL)
1316 return xprt->address_strings[format];
1317 else
1318 return "unprintable";
Chuck Leverf425eba2006-08-22 20:06:18 -04001319}
Chuck Leverb86acd52006-08-22 20:06:22 -04001320EXPORT_SYMBOL_GPL(rpc_peeraddr2str);
Chuck Leverf425eba2006-08-22 20:06:18 -04001321
Chuck Lever2e738fd2012-03-01 17:01:14 -05001322static const struct sockaddr_in rpc_inaddr_loopback = {
1323 .sin_family = AF_INET,
1324 .sin_addr.s_addr = htonl(INADDR_ANY),
1325};
1326
1327static const struct sockaddr_in6 rpc_in6addr_loopback = {
1328 .sin6_family = AF_INET6,
1329 .sin6_addr = IN6ADDR_ANY_INIT,
1330};
1331
1332/*
1333 * Try a getsockname() on a connected datagram socket. Using a
1334 * connected datagram socket prevents leaving a socket in TIME_WAIT.
1335 * This conserves the ephemeral port number space.
1336 *
1337 * Returns zero and fills in "buf" if successful; otherwise, a
1338 * negative errno is returned.
1339 */
1340static int rpc_sockname(struct net *net, struct sockaddr *sap, size_t salen,
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001341 struct sockaddr *buf)
Chuck Lever2e738fd2012-03-01 17:01:14 -05001342{
1343 struct socket *sock;
1344 int err;
1345
1346 err = __sock_create(net, sap->sa_family,
1347 SOCK_DGRAM, IPPROTO_UDP, &sock, 1);
1348 if (err < 0) {
1349 dprintk("RPC: can't create UDP socket (%d)\n", err);
1350 goto out;
1351 }
1352
1353 switch (sap->sa_family) {
1354 case AF_INET:
1355 err = kernel_bind(sock,
1356 (struct sockaddr *)&rpc_inaddr_loopback,
1357 sizeof(rpc_inaddr_loopback));
1358 break;
1359 case AF_INET6:
1360 err = kernel_bind(sock,
1361 (struct sockaddr *)&rpc_in6addr_loopback,
1362 sizeof(rpc_in6addr_loopback));
1363 break;
1364 default:
1365 err = -EAFNOSUPPORT;
1366 goto out;
1367 }
1368 if (err < 0) {
1369 dprintk("RPC: can't bind UDP socket (%d)\n", err);
1370 goto out_release;
1371 }
1372
1373 err = kernel_connect(sock, sap, salen, 0);
1374 if (err < 0) {
1375 dprintk("RPC: can't connect UDP socket (%d)\n", err);
1376 goto out_release;
1377 }
1378
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001379 err = kernel_getsockname(sock, buf);
Chuck Lever2e738fd2012-03-01 17:01:14 -05001380 if (err < 0) {
1381 dprintk("RPC: getsockname failed (%d)\n", err);
1382 goto out_release;
1383 }
1384
1385 err = 0;
1386 if (buf->sa_family == AF_INET6) {
1387 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)buf;
1388 sin6->sin6_scope_id = 0;
1389 }
1390 dprintk("RPC: %s succeeded\n", __func__);
1391
1392out_release:
1393 sock_release(sock);
1394out:
1395 return err;
1396}
1397
1398/*
1399 * Scraping a connected socket failed, so we don't have a useable
1400 * local address. Fallback: generate an address that will prevent
1401 * the server from calling us back.
1402 *
1403 * Returns zero and fills in "buf" if successful; otherwise, a
1404 * negative errno is returned.
1405 */
1406static int rpc_anyaddr(int family, struct sockaddr *buf, size_t buflen)
1407{
1408 switch (family) {
1409 case AF_INET:
1410 if (buflen < sizeof(rpc_inaddr_loopback))
1411 return -EINVAL;
1412 memcpy(buf, &rpc_inaddr_loopback,
1413 sizeof(rpc_inaddr_loopback));
1414 break;
1415 case AF_INET6:
1416 if (buflen < sizeof(rpc_in6addr_loopback))
1417 return -EINVAL;
1418 memcpy(buf, &rpc_in6addr_loopback,
1419 sizeof(rpc_in6addr_loopback));
Trond Myklebust0b161e62015-12-30 18:14:06 -05001420 break;
Chuck Lever2e738fd2012-03-01 17:01:14 -05001421 default:
1422 dprintk("RPC: %s: address family not supported\n",
1423 __func__);
1424 return -EAFNOSUPPORT;
1425 }
1426 dprintk("RPC: %s: succeeded\n", __func__);
1427 return 0;
1428}
1429
1430/**
1431 * rpc_localaddr - discover local endpoint address for an RPC client
1432 * @clnt: RPC client structure
1433 * @buf: target buffer
1434 * @buflen: size of target buffer, in bytes
1435 *
1436 * Returns zero and fills in "buf" and "buflen" if successful;
1437 * otherwise, a negative errno is returned.
1438 *
1439 * This works even if the underlying transport is not currently connected,
1440 * or if the upper layer never previously provided a source address.
1441 *
1442 * The result of this function call is transient: multiple calls in
1443 * succession may give different results, depending on how local
1444 * networking configuration changes over time.
1445 */
1446int rpc_localaddr(struct rpc_clnt *clnt, struct sockaddr *buf, size_t buflen)
1447{
1448 struct sockaddr_storage address;
1449 struct sockaddr *sap = (struct sockaddr *)&address;
1450 struct rpc_xprt *xprt;
1451 struct net *net;
1452 size_t salen;
1453 int err;
1454
1455 rcu_read_lock();
1456 xprt = rcu_dereference(clnt->cl_xprt);
1457 salen = xprt->addrlen;
1458 memcpy(sap, &xprt->addr, salen);
1459 net = get_net(xprt->xprt_net);
1460 rcu_read_unlock();
1461
1462 rpc_set_port(sap, 0);
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001463 err = rpc_sockname(net, sap, salen, buf);
Chuck Lever2e738fd2012-03-01 17:01:14 -05001464 put_net(net);
1465 if (err != 0)
1466 /* Couldn't discover local address, return ANYADDR */
1467 return rpc_anyaddr(sap->sa_family, buf, buflen);
1468 return 0;
1469}
1470EXPORT_SYMBOL_GPL(rpc_localaddr);
1471
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472void
1473rpc_setbufsize(struct rpc_clnt *clnt, unsigned int sndsize, unsigned int rcvsize)
1474{
Trond Myklebust2446ab62012-03-01 17:00:56 -05001475 struct rpc_xprt *xprt;
1476
1477 rcu_read_lock();
1478 xprt = rcu_dereference(clnt->cl_xprt);
Chuck Lever470056c2005-08-25 16:25:56 -07001479 if (xprt->ops->set_buffer_size)
1480 xprt->ops->set_buffer_size(xprt, sndsize, rcvsize);
Trond Myklebust2446ab62012-03-01 17:00:56 -05001481 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482}
Trond Myklebuste8914c62007-07-14 15:39:59 -04001483EXPORT_SYMBOL_GPL(rpc_setbufsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484
Trond Myklebust2446ab62012-03-01 17:00:56 -05001485/**
Trond Myklebust2446ab62012-03-01 17:00:56 -05001486 * rpc_net_ns - Get the network namespace for this RPC client
1487 * @clnt: RPC client to query
1488 *
1489 */
1490struct net *rpc_net_ns(struct rpc_clnt *clnt)
1491{
1492 struct net *ret;
1493
1494 rcu_read_lock();
1495 ret = rcu_dereference(clnt->cl_xprt)->xprt_net;
1496 rcu_read_unlock();
1497 return ret;
1498}
1499EXPORT_SYMBOL_GPL(rpc_net_ns);
1500
1501/**
1502 * rpc_max_payload - Get maximum payload size for a transport, in bytes
1503 * @clnt: RPC client to query
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 *
1505 * For stream transports, this is one RPC record fragment (see RFC
1506 * 1831), as we don't support multi-record requests yet. For datagram
1507 * transports, this is the size of an IP packet minus the IP, UDP, and
1508 * RPC header sizes.
1509 */
1510size_t rpc_max_payload(struct rpc_clnt *clnt)
1511{
Trond Myklebust2446ab62012-03-01 17:00:56 -05001512 size_t ret;
1513
1514 rcu_read_lock();
1515 ret = rcu_dereference(clnt->cl_xprt)->max_payload;
1516 rcu_read_unlock();
1517 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518}
Chuck Leverb86acd52006-08-22 20:06:22 -04001519EXPORT_SYMBOL_GPL(rpc_max_payload);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
Chuck Lever35f5a422006-01-03 09:55:50 +01001521/**
Chuck Lever6b26cc82016-05-02 14:40:40 -04001522 * rpc_max_bc_payload - Get maximum backchannel payload size, in bytes
1523 * @clnt: RPC client to query
1524 */
1525size_t rpc_max_bc_payload(struct rpc_clnt *clnt)
1526{
1527 struct rpc_xprt *xprt;
1528 size_t ret;
1529
1530 rcu_read_lock();
1531 xprt = rcu_dereference(clnt->cl_xprt);
1532 ret = xprt->ops->bc_maxpayload(xprt);
1533 rcu_read_unlock();
1534 return ret;
1535}
1536EXPORT_SYMBOL_GPL(rpc_max_bc_payload);
1537
Trond Myklebust7402a4f2019-07-16 13:51:29 -04001538unsigned int rpc_num_bc_slots(struct rpc_clnt *clnt)
1539{
1540 struct rpc_xprt *xprt;
1541 unsigned int ret;
1542
1543 rcu_read_lock();
1544 xprt = rcu_dereference(clnt->cl_xprt);
1545 ret = xprt->ops->bc_num_slots(xprt);
1546 rcu_read_unlock();
1547 return ret;
1548}
1549EXPORT_SYMBOL_GPL(rpc_num_bc_slots);
1550
Chuck Lever6b26cc82016-05-02 14:40:40 -04001551/**
Chuck Lever35f5a422006-01-03 09:55:50 +01001552 * rpc_force_rebind - force transport to check that remote port is unchanged
1553 * @clnt: client to rebind
1554 *
1555 */
1556void rpc_force_rebind(struct rpc_clnt *clnt)
1557{
Trond Myklebust2446ab62012-03-01 17:00:56 -05001558 if (clnt->cl_autobind) {
1559 rcu_read_lock();
1560 xprt_clear_bound(rcu_dereference(clnt->cl_xprt));
1561 rcu_read_unlock();
1562 }
Chuck Lever35f5a422006-01-03 09:55:50 +01001563}
Chuck Leverb86acd52006-08-22 20:06:22 -04001564EXPORT_SYMBOL_GPL(rpc_force_rebind);
Chuck Lever35f5a422006-01-03 09:55:50 +01001565
Trond Myklebust9e6fa0b2019-04-07 13:58:45 -04001566static int
1567__rpc_restart_call(struct rpc_task *task, void (*action)(struct rpc_task *))
Andy Adamsonaae20062009-04-01 09:22:40 -04001568{
Trond Myklebust494314c2014-03-20 12:59:09 -04001569 task->tk_status = 0;
Trond Myklebust5ad64b32019-04-07 13:58:54 -04001570 task->tk_rpc_status = 0;
Trond Myklebust9e6fa0b2019-04-07 13:58:45 -04001571 task->tk_action = action;
Trond Myklebustf1f88fc2010-07-31 14:29:07 -04001572 return 1;
Andy Adamsonaae20062009-04-01 09:22:40 -04001573}
Andy Adamsonaae20062009-04-01 09:22:40 -04001574
1575/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 * Restart an (async) RPC call. Usually called from within the
1577 * exit handler.
1578 */
Trond Myklebustf1f88fc2010-07-31 14:29:07 -04001579int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580rpc_restart_call(struct rpc_task *task)
1581{
Trond Myklebust9e6fa0b2019-04-07 13:58:45 -04001582 return __rpc_restart_call(task, call_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583}
Trond Myklebuste8914c62007-07-14 15:39:59 -04001584EXPORT_SYMBOL_GPL(rpc_restart_call);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
Trond Myklebust9e6fa0b2019-04-07 13:58:45 -04001586/*
1587 * Restart an (async) RPC call from the call_prepare state.
1588 * Usually called from within the exit handler.
1589 */
1590int
1591rpc_restart_call_prepare(struct rpc_task *task)
1592{
1593 if (task->tk_ops->rpc_call_prepare != NULL)
1594 return __rpc_restart_call(task, rpc_prepare_task);
1595 return rpc_restart_call(task);
1596}
1597EXPORT_SYMBOL_GPL(rpc_restart_call_prepare);
1598
Jeff Laytonb4b9d2c2014-11-26 14:44:43 -05001599const char
1600*rpc_proc_name(const struct rpc_task *task)
Chuck Lever3748f1e2008-05-21 17:09:12 -04001601{
1602 const struct rpc_procinfo *proc = task->tk_msg.rpc_proc;
1603
1604 if (proc) {
1605 if (proc->p_name)
1606 return proc->p_name;
1607 else
1608 return "NULL";
1609 } else
1610 return "no proc";
1611}
Chuck Lever3748f1e2008-05-21 17:09:12 -04001612
Trond Myklebust5ad64b32019-04-07 13:58:54 -04001613static void
1614__rpc_call_rpcerror(struct rpc_task *task, int tk_status, int rpc_status)
1615{
Chuck Lever0125ecb2020-05-12 17:13:23 -04001616 trace_rpc_call_rpcerror(task, tk_status, rpc_status);
Trond Myklebust5ad64b32019-04-07 13:58:54 -04001617 task->tk_rpc_status = rpc_status;
1618 rpc_exit(task, tk_status);
1619}
1620
1621static void
1622rpc_call_rpcerror(struct rpc_task *task, int status)
1623{
1624 __rpc_call_rpcerror(task, status, status);
1625}
1626
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627/*
1628 * 0. Initial state
1629 *
1630 * Other FSM states can be visited zero or more times, but
1631 * this state is visited exactly once for each RPC.
1632 */
1633static void
1634call_start(struct rpc_task *task)
1635{
1636 struct rpc_clnt *clnt = task->tk_client;
Christoph Hellwig1c5876d2017-05-08 23:27:10 +02001637 int idx = task->tk_msg.rpc_proc->p_statidx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638
Chuck Leverc435da62017-11-03 13:46:14 -04001639 trace_rpc_request(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640
Christoph Hellwig1c5876d2017-05-08 23:27:10 +02001641 /* Increment call count (version might not be valid for ping) */
1642 if (clnt->cl_program->version[clnt->cl_vers])
1643 clnt->cl_program->version[clnt->cl_vers]->counts[idx]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 clnt->cl_stats->rpccnt++;
1645 task->tk_action = call_reserve;
Bill Baker0f90be12018-06-19 16:24:58 -05001646 rpc_task_set_transport(task, clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647}
1648
1649/*
1650 * 1. Reserve an RPC call slot
1651 */
1652static void
1653call_reserve(struct rpc_task *task)
1654{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 task->tk_status = 0;
1656 task->tk_action = call_reserveresult;
1657 xprt_reserve(task);
1658}
1659
Trond Myklebustba60eb22013-04-14 10:49:37 -04001660static void call_retry_reserve(struct rpc_task *task);
1661
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662/*
1663 * 1b. Grok the result of xprt_reserve()
1664 */
1665static void
1666call_reserveresult(struct rpc_task *task)
1667{
1668 int status = task->tk_status;
1669
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 /*
1671 * After a call to xprt_reserve(), we must have either
1672 * a request slot or else an error status.
1673 */
1674 task->tk_status = 0;
1675 if (status >= 0) {
1676 if (task->tk_rqstp) {
J. Bruce Fieldsf2d47d02010-09-12 19:55:25 -04001677 task->tk_action = call_refresh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 return;
1679 }
1680
Trond Myklebust5ad64b32019-04-07 13:58:54 -04001681 rpc_call_rpcerror(task, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 return;
1683 }
1684
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 switch (status) {
Trond Myklebust1afeaf52012-05-19 12:12:53 -04001686 case -ENOMEM:
1687 rpc_delay(task, HZ >> 2);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001688 fallthrough;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 case -EAGAIN: /* woken up; retry */
Trond Myklebustba60eb22013-04-14 10:49:37 -04001690 task->tk_action = call_retry_reserve;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 default:
Chuck Lever5cd8b0d2019-10-09 12:58:08 -04001693 rpc_call_rpcerror(task, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695}
1696
1697/*
Trond Myklebustba60eb22013-04-14 10:49:37 -04001698 * 1c. Retry reserving an RPC call slot
1699 */
1700static void
1701call_retry_reserve(struct rpc_task *task)
1702{
Trond Myklebustba60eb22013-04-14 10:49:37 -04001703 task->tk_status = 0;
1704 task->tk_action = call_reserveresult;
1705 xprt_retry_reserve(task);
1706}
1707
1708/*
J. Bruce Fields55576242010-09-12 19:55:25 -04001709 * 2. Bind and/or refresh the credentials
1710 */
1711static void
1712call_refresh(struct rpc_task *task)
1713{
J. Bruce Fields55576242010-09-12 19:55:25 -04001714 task->tk_action = call_refreshresult;
1715 task->tk_status = 0;
1716 task->tk_client->cl_stats->rpcauthrefresh++;
1717 rpcauth_refreshcred(task);
1718}
1719
1720/*
1721 * 2a. Process the results of a credential refresh
1722 */
1723static void
1724call_refreshresult(struct rpc_task *task)
1725{
1726 int status = task->tk_status;
1727
J. Bruce Fields55576242010-09-12 19:55:25 -04001728 task->tk_status = 0;
Trond Myklebust5fc43972010-11-20 11:13:31 -05001729 task->tk_action = call_refresh;
J. Bruce Fields55576242010-09-12 19:55:25 -04001730 switch (status) {
Trond Myklebust5fc43972010-11-20 11:13:31 -05001731 case 0:
Weston Andros Adamson6ff33b72013-12-17 12:16:11 -05001732 if (rpcauth_uptodatecred(task)) {
Trond Myklebust5fc43972010-11-20 11:13:31 -05001733 task->tk_action = call_allocate;
Weston Andros Adamson6ff33b72013-12-17 12:16:11 -05001734 return;
1735 }
1736 /* Use rate-limiting and a max number of retries if refresh
1737 * had status 0 but failed to update the cred.
1738 */
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001739 fallthrough;
J. Bruce Fields55576242010-09-12 19:55:25 -04001740 case -ETIMEDOUT:
1741 rpc_delay(task, 3*HZ);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001742 fallthrough;
Trond Myklebust5fc43972010-11-20 11:13:31 -05001743 case -EAGAIN:
1744 status = -EACCES;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001745 fallthrough;
Andy Adamsonf1ff0c22013-08-14 11:59:13 -04001746 case -EKEYEXPIRED:
Trond Myklebust5fc43972010-11-20 11:13:31 -05001747 if (!task->tk_cred_retry)
1748 break;
1749 task->tk_cred_retry--;
Chuck Lever7c8099f2020-07-08 16:10:03 -04001750 trace_rpc_retry_refresh_status(task);
Trond Myklebust5fc43972010-11-20 11:13:31 -05001751 return;
J. Bruce Fields55576242010-09-12 19:55:25 -04001752 }
Chuck Lever7c8099f2020-07-08 16:10:03 -04001753 trace_rpc_refresh_status(task);
Trond Myklebust5ad64b32019-04-07 13:58:54 -04001754 rpc_call_rpcerror(task, status);
J. Bruce Fields55576242010-09-12 19:55:25 -04001755}
1756
1757/*
1758 * 2b. Allocate the buffer. For details, see sched.c:rpc_malloc.
Chuck Lever02107142006-01-03 09:55:49 +01001759 * (Note: buffer memory is freed in xprt_release).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 */
1761static void
1762call_allocate(struct rpc_task *task)
1763{
Chuck Lever2c94b8e2019-02-11 11:25:41 -05001764 const struct rpc_auth *auth = task->tk_rqstp->rq_cred->cr_auth;
Chuck Lever02107142006-01-03 09:55:49 +01001765 struct rpc_rqst *req = task->tk_rqstp;
Trond Myklebusta4f08352013-01-08 09:10:21 -05001766 struct rpc_xprt *xprt = req->rq_xprt;
Christoph Hellwig499b4982017-05-12 15:36:49 +02001767 const struct rpc_procinfo *proc = task->tk_msg.rpc_proc;
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04001768 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769
Chuck Lever2bea90d2007-03-29 16:47:53 -04001770 task->tk_status = 0;
Trond Myklebust762e4e62018-08-24 16:28:28 -04001771 task->tk_action = call_encode;
Chuck Lever2bea90d2007-03-29 16:47:53 -04001772
Trond Myklebustaf6b61d2019-04-11 15:16:52 -04001773 if (req->rq_buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 return;
1775
Chuck Lever2bea90d2007-03-29 16:47:53 -04001776 if (proc->p_proc != 0) {
1777 BUG_ON(proc->p_arglen == 0);
1778 if (proc->p_decode != NULL)
1779 BUG_ON(proc->p_replen == 0);
1780 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781
Chuck Lever2bea90d2007-03-29 16:47:53 -04001782 /*
1783 * Calculate the size (in quads) of the RPC call
1784 * and reply headers, and convert both values
1785 * to byte sizes.
1786 */
Chuck Lever2c94b8e2019-02-11 11:25:41 -05001787 req->rq_callsize = RPC_CALLHDRSIZE + (auth->au_cslack << 1) +
1788 proc->p_arglen;
Chuck Lever2bea90d2007-03-29 16:47:53 -04001789 req->rq_callsize <<= 2;
Trond Myklebust51314962019-03-15 12:55:59 -04001790 /*
1791 * Note: the reply buffer must at minimum allocate enough space
1792 * for the 'struct accepted_reply' from RFC5531.
1793 */
1794 req->rq_rcvsize = RPC_REPHDRSIZE + auth->au_rslack + \
1795 max_t(size_t, proc->p_replen, 2);
Chuck Lever2bea90d2007-03-29 16:47:53 -04001796 req->rq_rcvsize <<= 2;
1797
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04001798 status = xprt->ops->buf_alloc(task);
Chuck Lever06e234c2020-07-08 16:09:11 -04001799 trace_rpc_buf_alloc(task, status);
Trond Myklebustaf6b61d2019-04-11 15:16:52 -04001800 if (status == 0)
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04001801 return;
1802 if (status != -ENOMEM) {
Trond Myklebust5ad64b32019-04-07 13:58:54 -04001803 rpc_call_rpcerror(task, status);
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04001804 return;
1805 }
Chuck Lever46121cf2007-01-31 12:14:08 -05001806
Trond Myklebust5afa91332011-06-17 10:14:59 -04001807 if (RPC_IS_ASYNC(task) || !fatal_signal_pending(current)) {
Trond Myklebustb6e9c712007-10-01 12:06:44 -04001808 task->tk_action = call_allocate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 rpc_delay(task, HZ>>4);
1810 return;
1811 }
1812
Trond Myklebust714fbc72019-09-12 08:06:51 -04001813 rpc_call_rpcerror(task, -ERESTARTSYS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814}
1815
Trond Myklebust7ebbbc62018-08-28 09:00:27 -04001816static int
Trond Myklebust940e3312005-11-09 21:45:24 -05001817rpc_task_need_encode(struct rpc_task *task)
1818{
Trond Myklebust762e4e62018-08-24 16:28:28 -04001819 return test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate) == 0 &&
1820 (!(task->tk_flags & RPC_TASK_SENT) ||
1821 !(task->tk_flags & RPC_TASK_NO_RETRANS_TIMEOUT) ||
1822 xprt_request_need_retransmit(task));
Trond Myklebust940e3312005-11-09 21:45:24 -05001823}
1824
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825static void
Chuck Leverb0e1c572008-05-21 17:09:19 -04001826rpc_xdr_encode(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 struct rpc_rqst *req = task->tk_rqstp;
Chuck Levere8680a22019-02-11 11:24:48 -05001829 struct xdr_stream xdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830
Chuck Leverb9c5bc02016-09-15 10:55:12 -04001831 xdr_buf_init(&req->rq_snd_buf,
1832 req->rq_buffer,
1833 req->rq_callsize);
1834 xdr_buf_init(&req->rq_rcv_buf,
Chuck Lever68778942016-09-15 10:55:37 -04001835 req->rq_rbuffer,
Chuck Leverb9c5bc02016-09-15 10:55:12 -04001836 req->rq_rcvsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837
Trond Myklebustcc204d02019-09-10 13:01:35 -04001838 req->rq_reply_bytes_recvd = 0;
Chuck Levere8680a22019-02-11 11:24:48 -05001839 req->rq_snd_buf.head[0].iov_len = 0;
1840 xdr_init_encode(&xdr, &req->rq_snd_buf,
1841 req->rq_snd_buf.head[0].iov_base, req);
Trond Myklebust75369082019-07-17 21:22:38 -04001842 xdr_free_bvec(&req->rq_snd_buf);
Chuck Levere8680a22019-02-11 11:24:48 -05001843 if (rpc_encode_header(task, &xdr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 return;
Chuck Leverb0e1c572008-05-21 17:09:19 -04001845
Chuck Levere8680a22019-02-11 11:24:48 -05001846 task->tk_status = rpcauth_wrap_req(task, &xdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847}
1848
1849/*
Trond Myklebust762e4e62018-08-24 16:28:28 -04001850 * 3. Encode arguments of an RPC call
1851 */
1852static void
1853call_encode(struct rpc_task *task)
1854{
1855 if (!rpc_task_need_encode(task))
1856 goto out;
Chuck Lever63870392020-07-08 16:09:37 -04001857
Trond Myklebustcc204d02019-09-10 13:01:35 -04001858 /* Dequeue task from the receive queue while we're encoding */
1859 xprt_request_dequeue_xprt(task);
Trond Myklebust762e4e62018-08-24 16:28:28 -04001860 /* Encode here so that rpcsec_gss can use correct sequence number. */
1861 rpc_xdr_encode(task);
1862 /* Did the encode result in an error condition? */
1863 if (task->tk_status != 0) {
1864 /* Was the error nonfatal? */
Trond Myklebust97b78ae2019-01-02 17:53:13 -05001865 switch (task->tk_status) {
1866 case -EAGAIN:
1867 case -ENOMEM:
Trond Myklebust762e4e62018-08-24 16:28:28 -04001868 rpc_delay(task, HZ >> 4);
Trond Myklebust97b78ae2019-01-02 17:53:13 -05001869 break;
1870 case -EKEYEXPIRED:
ZhangXiaoxu9c5948c2019-04-29 17:32:31 +08001871 if (!task->tk_cred_retry) {
1872 rpc_exit(task, task->tk_status);
1873 } else {
1874 task->tk_action = call_refresh;
1875 task->tk_cred_retry--;
Chuck Lever7c8099f2020-07-08 16:10:03 -04001876 trace_rpc_retry_refresh_status(task);
ZhangXiaoxu9c5948c2019-04-29 17:32:31 +08001877 }
Trond Myklebust97b78ae2019-01-02 17:53:13 -05001878 break;
1879 default:
Trond Myklebust5ad64b32019-04-07 13:58:54 -04001880 rpc_call_rpcerror(task, task->tk_status);
Trond Myklebust97b78ae2019-01-02 17:53:13 -05001881 }
Trond Myklebust762e4e62018-08-24 16:28:28 -04001882 return;
1883 }
1884
1885 /* Add task to reply queue before transmission to avoid races */
1886 if (rpc_reply_expected(task))
1887 xprt_request_enqueue_receive(task);
1888 xprt_request_enqueue_transmit(task);
1889out:
Trond Myklebustaf6b61d2019-04-11 15:16:52 -04001890 task->tk_action = call_transmit;
1891 /* Check that the connection is OK */
1892 if (!xprt_bound(task->tk_xprt))
1893 task->tk_action = call_bind;
1894 else if (!xprt_connected(task->tk_xprt))
1895 task->tk_action = call_connect;
Trond Myklebust762e4e62018-08-24 16:28:28 -04001896}
1897
1898/*
Trond Myklebust03e51d32019-03-10 11:17:13 -04001899 * Helpers to check if the task was already transmitted, and
1900 * to take action when that is the case.
1901 */
1902static bool
1903rpc_task_transmitted(struct rpc_task *task)
1904{
1905 return !test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate);
1906}
1907
1908static void
1909rpc_task_handle_transmitted(struct rpc_task *task)
1910{
1911 xprt_end_transmit(task);
1912 task->tk_action = call_transmit_status;
Trond Myklebust03e51d32019-03-10 11:17:13 -04001913}
1914
1915/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 * 4. Get the server port number if not yet set
1917 */
1918static void
1919call_bind(struct rpc_task *task)
1920{
Trond Myklebustad2368d2013-01-08 10:08:33 -05001921 struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922
Trond Myklebust03e51d32019-03-10 11:17:13 -04001923 if (rpc_task_transmitted(task)) {
1924 rpc_task_handle_transmitted(task);
1925 return;
1926 }
1927
Trond Myklebust009a82f2019-03-09 12:07:17 -05001928 if (xprt_bound(xprt)) {
1929 task->tk_action = call_connect;
Trond Myklebust009a82f2019-03-09 12:07:17 -05001930 return;
1931 }
1932
Trond Myklebust009a82f2019-03-09 12:07:17 -05001933 task->tk_action = call_bind_status;
Trond Myklebust4d6c6712019-03-10 12:22:39 -04001934 if (!xprt_prepare_transmit(task))
1935 return;
1936
Trond Myklebust009a82f2019-03-09 12:07:17 -05001937 xprt->ops->rpcbind(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938}
1939
1940/*
Chuck Leverda351872005-08-11 16:25:11 -04001941 * 4a. Sort out bind result
1942 */
1943static void
1944call_bind_status(struct rpc_task *task)
1945{
Trond Myklebustbd736ed2019-08-15 17:26:17 -04001946 struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
Chuck Lever906462a2007-09-11 18:00:47 -04001947 int status = -EIO;
Chuck Leverda351872005-08-11 16:25:11 -04001948
Trond Myklebust03e51d32019-03-10 11:17:13 -04001949 if (rpc_task_transmitted(task)) {
1950 rpc_task_handle_transmitted(task);
1951 return;
1952 }
1953
Trond Myklebustbd736ed2019-08-15 17:26:17 -04001954 if (task->tk_status >= 0)
1955 goto out_next;
1956 if (xprt_bound(xprt)) {
Chuck Leverda351872005-08-11 16:25:11 -04001957 task->tk_status = 0;
Trond Myklebustbd736ed2019-08-15 17:26:17 -04001958 goto out_next;
Chuck Leverda351872005-08-11 16:25:11 -04001959 }
1960
1961 switch (task->tk_status) {
Trond Myklebust381ba742008-07-07 12:18:53 -04001962 case -ENOMEM:
Trond Myklebust381ba742008-07-07 12:18:53 -04001963 rpc_delay(task, HZ >> 2);
Chuck Lever2429cbf2007-09-11 18:00:41 -04001964 goto retry_timeout;
Chuck Leverda351872005-08-11 16:25:11 -04001965 case -EACCES:
Chuck Lever42ebfc22020-07-08 16:10:14 -04001966 trace_rpcb_prog_unavail_err(task);
Chuck Leverb79dc8c2007-09-11 18:00:52 -04001967 /* fail immediately if this is an RPC ping */
1968 if (task->tk_msg.rpc_proc->p_proc == 0) {
1969 status = -EOPNOTSUPP;
1970 break;
1971 }
Trond Myklebust0b760112011-05-31 15:15:34 -04001972 if (task->tk_rebind_retry == 0)
1973 break;
1974 task->tk_rebind_retry--;
Chuck Leverea635a52005-10-06 23:12:58 -04001975 rpc_delay(task, 3*HZ);
Trond Myklebustda45828e2006-08-31 15:44:52 -04001976 goto retry_timeout;
Trond Myklebust80f455d2019-08-15 22:55:19 -04001977 case -ENOBUFS:
1978 rpc_delay(task, HZ >> 2);
1979 goto retry_timeout;
Trond Myklebust4d6c6712019-03-10 12:22:39 -04001980 case -EAGAIN:
1981 goto retry_timeout;
Chuck Leverda351872005-08-11 16:25:11 -04001982 case -ETIMEDOUT:
Chuck Lever42ebfc22020-07-08 16:10:14 -04001983 trace_rpcb_timeout_err(task);
Trond Myklebustda45828e2006-08-31 15:44:52 -04001984 goto retry_timeout;
Chuck Leverda351872005-08-11 16:25:11 -04001985 case -EPFNOSUPPORT:
Chuck Lever906462a2007-09-11 18:00:47 -04001986 /* server doesn't support any rpcbind version we know of */
Chuck Lever42ebfc22020-07-08 16:10:14 -04001987 trace_rpcb_bind_version_err(task);
Chuck Leverda351872005-08-11 16:25:11 -04001988 break;
1989 case -EPROTONOSUPPORT:
Chuck Lever42ebfc22020-07-08 16:10:14 -04001990 trace_rpcb_bind_version_err(task);
Trond Myklebustfdb63dc2014-03-17 12:57:31 -04001991 goto retry_timeout;
Chuck Lever012da152009-12-03 15:58:56 -05001992 case -ECONNREFUSED: /* connection problems */
1993 case -ECONNRESET:
Trond Myklebustdf277272013-12-31 13:11:43 -05001994 case -ECONNABORTED:
Chuck Lever012da152009-12-03 15:58:56 -05001995 case -ENOTCONN:
1996 case -EHOSTDOWN:
Trond Myklebusteb5b46f2017-11-30 07:21:33 -05001997 case -ENETDOWN:
Chuck Lever012da152009-12-03 15:58:56 -05001998 case -EHOSTUNREACH:
1999 case -ENETUNREACH:
2000 case -EPIPE:
Chuck Lever42ebfc22020-07-08 16:10:14 -04002001 trace_rpcb_unreachable_err(task);
Chuck Lever012da152009-12-03 15:58:56 -05002002 if (!RPC_IS_SOFTCONN(task)) {
2003 rpc_delay(task, 5*HZ);
2004 goto retry_timeout;
2005 }
2006 status = task->tk_status;
2007 break;
Chuck Leverda351872005-08-11 16:25:11 -04002008 default:
Chuck Lever42ebfc22020-07-08 16:10:14 -04002009 trace_rpcb_unrecognized_err(task);
Chuck Leverda351872005-08-11 16:25:11 -04002010 }
2011
Trond Myklebust5ad64b32019-04-07 13:58:54 -04002012 rpc_call_rpcerror(task, status);
Chuck Leverda351872005-08-11 16:25:11 -04002013 return;
Trond Myklebustbd736ed2019-08-15 17:26:17 -04002014out_next:
2015 task->tk_action = call_connect;
2016 return;
Trond Myklebustda45828e2006-08-31 15:44:52 -04002017retry_timeout:
Trond Myklebustfdb63dc2014-03-17 12:57:31 -04002018 task->tk_status = 0;
Trond Myklebust4d6c6712019-03-10 12:22:39 -04002019 task->tk_action = call_bind;
Trond Myklebustcea57782019-03-09 16:06:47 -05002020 rpc_check_timeout(task);
Chuck Leverda351872005-08-11 16:25:11 -04002021}
2022
2023/*
2024 * 4b. Connect to the RPC server
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 */
2026static void
2027call_connect(struct rpc_task *task)
2028{
Trond Myklebustad2368d2013-01-08 10:08:33 -05002029 struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030
Trond Myklebust03e51d32019-03-10 11:17:13 -04002031 if (rpc_task_transmitted(task)) {
2032 rpc_task_handle_transmitted(task);
2033 return;
2034 }
2035
Trond Myklebust009a82f2019-03-09 12:07:17 -05002036 if (xprt_connected(xprt)) {
2037 task->tk_action = call_transmit;
Trond Myklebust009a82f2019-03-09 12:07:17 -05002038 return;
2039 }
2040
Trond Myklebust009a82f2019-03-09 12:07:17 -05002041 task->tk_action = call_connect_status;
2042 if (task->tk_status < 0)
2043 return;
2044 if (task->tk_flags & RPC_TASK_NOCONNECT) {
Trond Myklebust5ad64b32019-04-07 13:58:54 -04002045 rpc_call_rpcerror(task, -ENOTCONN);
Trond Myklebust009a82f2019-03-09 12:07:17 -05002046 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 }
Trond Myklebust4d6c6712019-03-10 12:22:39 -04002048 if (!xprt_prepare_transmit(task))
2049 return;
Trond Myklebust009a82f2019-03-09 12:07:17 -05002050 xprt_connect(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051}
2052
2053/*
Chuck Leverda351872005-08-11 16:25:11 -04002054 * 4c. Sort out connect result
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 */
2056static void
2057call_connect_status(struct rpc_task *task)
2058{
Trond Myklebustbd736ed2019-08-15 17:26:17 -04002059 struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 struct rpc_clnt *clnt = task->tk_client;
2061 int status = task->tk_status;
2062
Trond Myklebust03e51d32019-03-10 11:17:13 -04002063 if (rpc_task_transmitted(task)) {
2064 rpc_task_handle_transmitted(task);
Trond Myklebust9bd11522018-11-30 12:48:47 -05002065 return;
2066 }
2067
Chuck Levere671edb2018-03-16 10:33:44 -04002068 trace_rpc_connect_status(task);
Trond Myklebustbd736ed2019-08-15 17:26:17 -04002069
2070 if (task->tk_status == 0) {
2071 clnt->cl_stats->netreconn++;
2072 goto out_next;
2073 }
2074 if (xprt_connected(xprt)) {
2075 task->tk_status = 0;
2076 goto out_next;
2077 }
2078
Trond Myklebust561ec162013-09-26 15:22:45 -04002079 task->tk_status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 switch (status) {
Trond Myklebust3ed5e2a2013-03-04 17:29:33 -05002081 case -ECONNREFUSED:
NeilBrownfd01b252017-08-18 17:12:51 +10002082 /* A positive refusal suggests a rebind is needed. */
2083 if (RPC_IS_SOFTCONN(task))
2084 break;
2085 if (clnt->cl_autobind) {
2086 rpc_force_rebind(clnt);
Trond Myklebust7b3fef82019-03-07 14:10:32 -05002087 goto out_retry;
NeilBrownfd01b252017-08-18 17:12:51 +10002088 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002089 fallthrough;
Trond Myklebust3ed5e2a2013-03-04 17:29:33 -05002090 case -ECONNRESET:
Trond Myklebustdf277272013-12-31 13:11:43 -05002091 case -ECONNABORTED:
Trond Myklebusteb5b46f2017-11-30 07:21:33 -05002092 case -ENETDOWN:
Trond Myklebust3ed5e2a2013-03-04 17:29:33 -05002093 case -ENETUNREACH:
Trond Myklebustdf277272013-12-31 13:11:43 -05002094 case -EHOSTUNREACH:
Trond Myklebust2fc193c2014-07-03 00:02:57 -04002095 case -EPIPE:
Chuck Leverb8457602019-12-23 10:28:44 -05002096 case -EPROTO:
NeilBrown2c2ee6d2016-11-23 14:44:58 +11002097 xprt_conditional_disconnect(task->tk_rqstp->rq_xprt,
2098 task->tk_rqstp->rq_connect_cookie);
Trond Myklebust3ed5e2a2013-03-04 17:29:33 -05002099 if (RPC_IS_SOFTCONN(task))
2100 break;
Steve Dickson1fa3e2e2014-03-20 11:23:03 -04002101 /* retry with existing socket, after a delay */
2102 rpc_delay(task, 3*HZ);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002103 fallthrough;
Trond Myklebust80f455d2019-08-15 22:55:19 -04002104 case -EADDRINUSE:
Trond Myklebust0445f922018-12-17 13:34:59 -05002105 case -ENOTCONN:
Trond Myklebust3ed5e2a2013-03-04 17:29:33 -05002106 case -EAGAIN:
Trond Myklebust485f2252014-03-17 12:51:44 -04002107 case -ETIMEDOUT:
Trond Myklebust7b3fef82019-03-07 14:10:32 -05002108 goto out_retry;
Trond Myklebust80f455d2019-08-15 22:55:19 -04002109 case -ENOBUFS:
2110 rpc_delay(task, HZ >> 2);
2111 goto out_retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 }
Trond Myklebust5ad64b32019-04-07 13:58:54 -04002113 rpc_call_rpcerror(task, status);
Trond Myklebust7b3fef82019-03-07 14:10:32 -05002114 return;
Trond Myklebustbd736ed2019-08-15 17:26:17 -04002115out_next:
2116 task->tk_action = call_transmit;
2117 return;
Trond Myklebust7b3fef82019-03-07 14:10:32 -05002118out_retry:
2119 /* Check for timeouts before looping back to call_bind */
2120 task->tk_action = call_bind;
2121 rpc_check_timeout(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122}
2123
2124/*
2125 * 5. Transmit the RPC request, and wait for reply
2126 */
2127static void
2128call_transmit(struct rpc_task *task)
2129{
Trond Myklebust03e51d32019-03-10 11:17:13 -04002130 if (rpc_task_transmitted(task)) {
2131 rpc_task_handle_transmitted(task);
2132 return;
2133 }
2134
Trond Myklebusted7dc972019-03-04 14:19:31 -05002135 task->tk_action = call_transmit_status;
Trond Myklebust009a82f2019-03-09 12:07:17 -05002136 if (!xprt_prepare_transmit(task))
2137 return;
2138 task->tk_status = 0;
Trond Myklebustc5445772018-09-03 23:39:27 -04002139 if (test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate)) {
Trond Myklebust009a82f2019-03-09 12:07:17 -05002140 if (!xprt_connected(task->tk_xprt)) {
2141 task->tk_status = -ENOTCONN;
Trond Myklebustc5445772018-09-03 23:39:27 -04002142 return;
Trond Myklebusted7dc972019-03-04 14:19:31 -05002143 }
Trond Myklebust009a82f2019-03-09 12:07:17 -05002144 xprt_transmit(task);
Trond Myklebustc5445772018-09-03 23:39:27 -04002145 }
Trond Myklebustc5445772018-09-03 23:39:27 -04002146 xprt_end_transmit(task);
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04002147}
2148
2149/*
2150 * 5a. Handle cleanup after a transmission
2151 */
2152static void
2153call_transmit_status(struct rpc_task *task)
2154{
2155 task->tk_action = call_status;
Chuck Lever206a1342009-12-03 15:58:56 -05002156
2157 /*
2158 * Common case: success. Force the compiler to put this
2159 * test first.
2160 */
Trond Myklebust009a82f2019-03-09 12:07:17 -05002161 if (rpc_task_transmitted(task)) {
Trond Myklebusta7b1a482019-04-15 11:54:13 -04002162 task->tk_status = 0;
2163 xprt_request_wait_receive(task);
Chuck Lever206a1342009-12-03 15:58:56 -05002164 return;
2165 }
2166
Trond Myklebust15f081c2009-03-11 14:37:57 -04002167 switch (task->tk_status) {
Trond Myklebust15f081c2009-03-11 14:37:57 -04002168 default:
Trond Myklebust75891f52018-09-03 17:37:36 -04002169 break;
Trond Myklebust78b576c2018-08-28 09:20:10 -04002170 case -EBADMSG:
Trond Myklebust762e4e62018-08-24 16:28:28 -04002171 task->tk_status = 0;
2172 task->tk_action = call_encode;
Trond Myklebust78b576c2018-08-28 09:20:10 -04002173 break;
Trond Myklebust15f081c2009-03-11 14:37:57 -04002174 /*
2175 * Special cases: if we've been waiting on the
2176 * socket's write_space() callback, or if the
2177 * socket just returned a connection error,
2178 * then hold onto the transport lock.
2179 */
Trond Myklebust78b576c2018-08-28 09:20:10 -04002180 case -ENOBUFS:
2181 rpc_delay(task, HZ>>2);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002182 fallthrough;
Trond Myklebustc5445772018-09-03 23:39:27 -04002183 case -EBADSLT:
Trond Myklebust78b576c2018-08-28 09:20:10 -04002184 case -EAGAIN:
2185 task->tk_action = call_transmit;
2186 task->tk_status = 0;
2187 break;
Trond Myklebust15f081c2009-03-11 14:37:57 -04002188 case -ECONNREFUSED:
Trond Myklebust15f081c2009-03-11 14:37:57 -04002189 case -EHOSTDOWN:
Trond Myklebusteb5b46f2017-11-30 07:21:33 -05002190 case -ENETDOWN:
Trond Myklebust15f081c2009-03-11 14:37:57 -04002191 case -EHOSTUNREACH:
2192 case -ENETUNREACH:
Jason Baron3dedbb52014-09-24 18:08:04 +00002193 case -EPERM:
Chuck Lever09a21c42009-12-03 15:58:56 -05002194 if (RPC_IS_SOFTCONN(task)) {
Chuck Levera25a4cb2018-03-16 10:33:55 -04002195 if (!task->tk_msg.rpc_proc->p_proc)
2196 trace_xprt_ping(task->tk_xprt,
2197 task->tk_status);
Trond Myklebust5ad64b32019-04-07 13:58:54 -04002198 rpc_call_rpcerror(task, task->tk_status);
Trond Myklebust7b3fef82019-03-07 14:10:32 -05002199 return;
Chuck Lever09a21c42009-12-03 15:58:56 -05002200 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002201 fallthrough;
Chuck Lever09a21c42009-12-03 15:58:56 -05002202 case -ECONNRESET:
Trond Myklebustdf277272013-12-31 13:11:43 -05002203 case -ECONNABORTED:
Trond Myklebust3913c782015-02-08 21:44:04 -05002204 case -EADDRINUSE:
Chuck Lever09a21c42009-12-03 15:58:56 -05002205 case -ENOTCONN:
Trond Myklebustc8485e42009-03-11 14:37:59 -04002206 case -EPIPE:
Trond Myklebusted7dc972019-03-04 14:19:31 -05002207 task->tk_action = call_bind;
2208 task->tk_status = 0;
Trond Myklebust7ebbbc62018-08-28 09:00:27 -04002209 break;
Trond Myklebust15f081c2009-03-11 14:37:57 -04002210 }
Trond Myklebust7b3fef82019-03-07 14:10:32 -05002211 rpc_check_timeout(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212}
2213
Trond Myklebust9e00abc2011-07-13 19:20:49 -04002214#if defined(CONFIG_SUNRPC_BACKCHANNEL)
Trond Myklebust477687e2019-03-05 07:30:48 -05002215static void call_bc_transmit(struct rpc_task *task);
2216static void call_bc_transmit_status(struct rpc_task *task);
2217
2218static void
2219call_bc_encode(struct rpc_task *task)
2220{
2221 xprt_request_enqueue_transmit(task);
2222 task->tk_action = call_bc_transmit;
2223}
2224
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04002225/*
2226 * 5b. Send the backchannel RPC reply. On error, drop the reply. In
2227 * addition, disconnect on connectivity errors.
2228 */
2229static void
2230call_bc_transmit(struct rpc_task *task)
2231{
Trond Myklebust477687e2019-03-05 07:30:48 -05002232 task->tk_action = call_bc_transmit_status;
2233 if (test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate)) {
2234 if (!xprt_prepare_transmit(task))
2235 return;
2236 task->tk_status = 0;
2237 xprt_transmit(task);
2238 }
2239 xprt_end_transmit(task);
2240}
2241
2242static void
2243call_bc_transmit_status(struct rpc_task *task)
2244{
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04002245 struct rpc_rqst *req = task->tk_rqstp;
2246
Trond Myklebusta7b1a482019-04-15 11:54:13 -04002247 if (rpc_task_transmitted(task))
2248 task->tk_status = 0;
2249
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04002250 switch (task->tk_status) {
2251 case 0:
2252 /* Success */
Trond Myklebusteb5b46f2017-11-30 07:21:33 -05002253 case -ENETDOWN:
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04002254 case -EHOSTDOWN:
2255 case -EHOSTUNREACH:
2256 case -ENETUNREACH:
Trond Myklebust38325912015-06-19 13:04:13 -04002257 case -ECONNRESET:
2258 case -ECONNREFUSED:
2259 case -EADDRINUSE:
2260 case -ENOTCONN:
2261 case -EPIPE:
2262 break;
Trond Myklebust477687e2019-03-05 07:30:48 -05002263 case -ENOBUFS:
2264 rpc_delay(task, HZ>>2);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002265 fallthrough;
Trond Myklebust477687e2019-03-05 07:30:48 -05002266 case -EBADSLT:
Trond Myklebustc5445772018-09-03 23:39:27 -04002267 case -EAGAIN:
Trond Myklebust477687e2019-03-05 07:30:48 -05002268 task->tk_status = 0;
2269 task->tk_action = call_bc_transmit;
2270 return;
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04002271 case -ETIMEDOUT:
2272 /*
2273 * Problem reaching the server. Disconnect and let the
2274 * forechannel reestablish the connection. The server will
2275 * have to retransmit the backchannel request and we'll
2276 * reprocess it. Since these ops are idempotent, there's no
2277 * need to cache our reply at this time.
2278 */
2279 printk(KERN_NOTICE "RPC: Could not send backchannel reply "
2280 "error: %d\n", task->tk_status);
Trond Myklebusta4f08352013-01-08 09:10:21 -05002281 xprt_conditional_disconnect(req->rq_xprt,
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04002282 req->rq_connect_cookie);
2283 break;
2284 default:
2285 /*
2286 * We were unable to reply and will have to drop the
2287 * request. The server should reconnect and retransmit.
2288 */
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04002289 printk(KERN_NOTICE "RPC: Could not send backchannel reply "
2290 "error: %d\n", task->tk_status);
2291 break;
2292 }
Trond Myklebust1193d582015-06-02 11:53:21 -04002293 task->tk_action = rpc_exit_task;
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04002294}
Trond Myklebust9e00abc2011-07-13 19:20:49 -04002295#endif /* CONFIG_SUNRPC_BACKCHANNEL */
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04002296
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297/*
2298 * 6. Sort out the RPC call status
2299 */
2300static void
2301call_status(struct rpc_task *task)
2302{
2303 struct rpc_clnt *clnt = task->tk_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 int status;
2305
Chuck Levera25a4cb2018-03-16 10:33:55 -04002306 if (!task->tk_msg.rpc_proc->p_proc)
2307 trace_xprt_ping(task->tk_xprt, task->tk_status);
2308
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 status = task->tk_status;
2310 if (status >= 0) {
2311 task->tk_action = call_decode;
2312 return;
2313 }
2314
Steve Dickson5753cba2012-02-06 10:08:08 -05002315 trace_rpc_call_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 task->tk_status = 0;
2317 switch(status) {
Trond Myklebust76303992006-08-30 14:32:49 -04002318 case -EHOSTDOWN:
Trond Myklebusteb5b46f2017-11-30 07:21:33 -05002319 case -ENETDOWN:
Trond Myklebust76303992006-08-30 14:32:49 -04002320 case -EHOSTUNREACH:
2321 case -ENETUNREACH:
Jason Baron3dedbb52014-09-24 18:08:04 +00002322 case -EPERM:
Trond Myklebustcea57782019-03-09 16:06:47 -05002323 if (RPC_IS_SOFTCONN(task))
2324 goto out_exit;
Trond Myklebust76303992006-08-30 14:32:49 -04002325 /*
2326 * Delay any retries for 3 seconds, then handle as if it
2327 * were a timeout.
2328 */
2329 rpc_delay(task, 3*HZ);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002330 fallthrough;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 case -ETIMEDOUT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 break;
2333 case -ECONNREFUSED:
Trond Myklebustdf277272013-12-31 13:11:43 -05002334 case -ECONNRESET:
2335 case -ECONNABORTED:
Olga Kornievskaiaec6017d2019-05-29 10:46:00 -04002336 case -ENOTCONN:
Chuck Lever35f5a422006-01-03 09:55:50 +01002337 rpc_force_rebind(clnt);
Trond Myklebustc82e5472019-08-16 08:58:48 -04002338 break;
Trond Myklebust3913c782015-02-08 21:44:04 -05002339 case -EADDRINUSE:
Trond Myklebustc8485e42009-03-11 14:37:59 -04002340 rpc_delay(task, 3*HZ);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002341 fallthrough;
Trond Myklebustc8485e42009-03-11 14:37:59 -04002342 case -EPIPE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 case -EAGAIN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 break;
2345 case -EIO:
2346 /* shutdown or soft timeout */
Trond Myklebustcea57782019-03-09 16:06:47 -05002347 goto out_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 default:
Olga Kornievskaiab6b61522008-06-09 16:51:31 -04002349 if (clnt->cl_chatty)
2350 printk("%s: RPC call returned error %d\n",
Trond Myklebust55909f22013-08-23 11:48:15 -04002351 clnt->cl_program->name, -status);
Trond Myklebustcea57782019-03-09 16:06:47 -05002352 goto out_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 }
Trond Myklebustcea57782019-03-09 16:06:47 -05002354 task->tk_action = call_encode;
Olga Kornievskaia88428cc42020-06-23 11:24:09 -04002355 if (status != -ECONNRESET && status != -ECONNABORTED)
2356 rpc_check_timeout(task);
Trond Myklebustcea57782019-03-09 16:06:47 -05002357 return;
2358out_exit:
Trond Myklebust5ad64b32019-04-07 13:58:54 -04002359 rpc_call_rpcerror(task, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360}
2361
Trond Myklebustd84dd3f2019-03-19 11:24:54 -04002362static bool
2363rpc_check_connected(const struct rpc_rqst *req)
2364{
2365 /* No allocated request or transport? return true */
2366 if (!req || !req->rq_xprt)
2367 return true;
2368 return xprt_connected(req->rq_xprt);
2369}
2370
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371static void
Trond Myklebust7b3fef82019-03-07 14:10:32 -05002372rpc_check_timeout(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373{
2374 struct rpc_clnt *clnt = task->tk_client;
2375
Chuck Leverce99aa62020-05-09 14:07:13 -04002376 if (RPC_SIGNALLED(task)) {
2377 rpc_call_rpcerror(task, -ERESTARTSYS);
2378 return;
2379 }
2380
Trond Myklebust7b3fef82019-03-07 14:10:32 -05002381 if (xprt_adjust_timeout(task->tk_rqstp) == 0)
2382 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383
Chuck Lever914cdcc2020-07-08 16:09:58 -04002384 trace_rpc_timeout_status(task);
Chuck Leveref759a22006-03-20 13:44:17 -05002385 task->tk_timeouts++;
2386
Trond Myklebustd84dd3f2019-03-19 11:24:54 -04002387 if (RPC_IS_SOFTCONN(task) && !rpc_check_connected(task->tk_rqstp)) {
Trond Myklebust5ad64b32019-04-07 13:58:54 -04002388 rpc_call_rpcerror(task, -ETIMEDOUT);
Chuck Lever3a28bec2009-12-03 15:58:56 -05002389 return;
2390 }
Trond Myklebustd84dd3f2019-03-19 11:24:54 -04002391
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 if (RPC_IS_SOFT(task)) {
Trond Myklebuste4ec48d2019-04-07 13:58:55 -04002393 /*
2394 * Once a "no retrans timeout" soft tasks (a.k.a NFSv4) has
2395 * been sent, it should time out only if the transport
2396 * connection gets terminally broken.
2397 */
2398 if ((task->tk_flags & RPC_TASK_NO_RETRANS_TIMEOUT) &&
2399 rpc_check_connected(task->tk_rqstp))
2400 return;
2401
Joe Perchescac5d072012-07-18 11:17:11 -07002402 if (clnt->cl_chatty) {
Trond Myklebust0729d992019-04-07 13:58:57 -04002403 pr_notice_ratelimited(
2404 "%s: server %s not responding, timed out\n",
Trond Myklebust55909f22013-08-23 11:48:15 -04002405 clnt->cl_program->name,
Trond Myklebustfb43d172016-01-30 16:39:26 -05002406 task->tk_xprt->servername);
Joe Perchescac5d072012-07-18 11:17:11 -07002407 }
Trond Myklebust7494d002011-04-24 14:28:45 -04002408 if (task->tk_flags & RPC_TASK_TIMEOUT)
Trond Myklebust5ad64b32019-04-07 13:58:54 -04002409 rpc_call_rpcerror(task, -ETIMEDOUT);
Trond Myklebust7494d002011-04-24 14:28:45 -04002410 else
Trond Myklebust5ad64b32019-04-07 13:58:54 -04002411 __rpc_call_rpcerror(task, -EIO, -ETIMEDOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 return;
2413 }
2414
Chuck Leverf518e35a2006-01-03 09:55:52 +01002415 if (!(task->tk_flags & RPC_CALL_MAJORSEEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 task->tk_flags |= RPC_CALL_MAJORSEEN;
Trond Myklebust4e0038b2012-03-01 17:01:05 -05002417 if (clnt->cl_chatty) {
Trond Myklebust0729d992019-04-07 13:58:57 -04002418 pr_notice_ratelimited(
2419 "%s: server %s not responding, still trying\n",
2420 clnt->cl_program->name,
2421 task->tk_xprt->servername);
Trond Myklebust4e0038b2012-03-01 17:01:05 -05002422 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 }
Chuck Lever35f5a422006-01-03 09:55:50 +01002424 rpc_force_rebind(clnt);
Trond Myklebustb48633b2008-04-22 16:47:55 -04002425 /*
2426 * Did our request time out due to an RPCSEC_GSS out-of-sequence
2427 * event? RFC2203 requires the server to drop all such requests.
2428 */
2429 rpcauth_invalcred(task);
Trond Myklebust7b3fef82019-03-07 14:10:32 -05002430}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431
Trond Myklebust7b3fef82019-03-07 14:10:32 -05002432/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 * 7. Decode the RPC reply
2434 */
2435static void
2436call_decode(struct rpc_task *task)
2437{
2438 struct rpc_clnt *clnt = task->tk_client;
2439 struct rpc_rqst *req = task->tk_rqstp;
Chuck Levera0584ee2019-02-11 11:24:58 -05002440 struct xdr_stream xdr;
Trond Myklebust9ba82882019-09-16 09:12:19 -04002441 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442
Chuck Levera0584ee2019-02-11 11:24:58 -05002443 if (!task->tk_msg.rpc_proc->p_decode) {
Trond Myklebust9ee94d32018-08-28 16:27:31 -04002444 task->tk_action = rpc_exit_task;
2445 return;
2446 }
2447
Chuck Leverf518e35a2006-01-03 09:55:52 +01002448 if (task->tk_flags & RPC_CALL_MAJORSEEN) {
Trond Myklebust4e0038b2012-03-01 17:01:05 -05002449 if (clnt->cl_chatty) {
Trond Myklebust0729d992019-04-07 13:58:57 -04002450 pr_notice_ratelimited("%s: server %s OK\n",
Trond Myklebust55909f22013-08-23 11:48:15 -04002451 clnt->cl_program->name,
Trond Myklebustfb43d172016-01-30 16:39:26 -05002452 task->tk_xprt->servername);
Trond Myklebust4e0038b2012-03-01 17:01:05 -05002453 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 task->tk_flags &= ~RPC_CALL_MAJORSEEN;
2455 }
2456
Trond Myklebust43ac3f22006-03-20 13:44:51 -05002457 /*
Trond Myklebust9ba82882019-09-16 09:12:19 -04002458 * Did we ever call xprt_complete_rqst()? If not, we should assume
2459 * the message is incomplete.
2460 */
2461 err = -EAGAIN;
2462 if (!req->rq_reply_bytes_recvd)
2463 goto out;
2464
Baptiste Lepersf8f7e0f2021-05-01 14:10:51 +10002465 /* Ensure that we see all writes made by xprt_complete_rqst()
2466 * before it changed req->rq_reply_bytes_recvd.
2467 */
2468 smp_rmb();
2469
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 req->rq_rcv_buf.len = req->rq_private_buf.len;
Chuck Leverc509f15a2020-05-12 17:13:28 -04002471 trace_rpc_xdr_recvfrom(task, &req->rq_rcv_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472
2473 /* Check that the softirq receive buffer is valid */
2474 WARN_ON(memcmp(&req->rq_rcv_buf, &req->rq_private_buf,
2475 sizeof(req->rq_rcv_buf)) != 0);
2476
Chuck Levera0584ee2019-02-11 11:24:58 -05002477 xdr_init_decode(&xdr, &req->rq_rcv_buf,
2478 req->rq_rcv_buf.head[0].iov_base, req);
Trond Myklebust9ba82882019-09-16 09:12:19 -04002479 err = rpc_decode_header(task, &xdr);
2480out:
2481 switch (err) {
Chuck Levera0584ee2019-02-11 11:24:58 -05002482 case 0:
2483 task->tk_action = rpc_exit_task;
2484 task->tk_status = rpcauth_unwrap_resp(task, &xdr);
Trond Myklebustabbcf282006-01-03 09:55:03 +01002485 return;
Chuck Levera0584ee2019-02-11 11:24:58 -05002486 case -EAGAIN:
Chuck Levera0584ee2019-02-11 11:24:58 -05002487 task->tk_status = 0;
Trond Myklebust7987b692019-05-29 12:49:52 -04002488 if (task->tk_client->cl_discrtry)
2489 xprt_conditional_disconnect(req->rq_xprt,
2490 req->rq_connect_cookie);
Trond Myklebustcea57782019-03-09 16:06:47 -05002491 task->tk_action = call_encode;
2492 rpc_check_timeout(task);
Trond Myklebust7987b692019-05-29 12:49:52 -04002493 break;
2494 case -EKEYREJECTED:
2495 task->tk_action = call_reserve;
2496 rpc_check_timeout(task);
2497 rpcauth_invalcred(task);
2498 /* Ensure we obtain a new XID if we retry! */
2499 xprt_release(task);
Trond Myklebust24b74bf2008-04-19 13:15:47 -04002500 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501}
2502
Chuck Levere8680a22019-02-11 11:24:48 -05002503static int
2504rpc_encode_header(struct rpc_task *task, struct xdr_stream *xdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505{
2506 struct rpc_clnt *clnt = task->tk_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 struct rpc_rqst *req = task->tk_rqstp;
Chuck Levere8680a22019-02-11 11:24:48 -05002508 __be32 *p;
2509 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510
Chuck Levere8680a22019-02-11 11:24:48 -05002511 error = -EMSGSIZE;
2512 p = xdr_reserve_space(xdr, RPC_CALLHDRSIZE << 2);
2513 if (!p)
2514 goto out_fail;
2515 *p++ = req->rq_xid;
2516 *p++ = rpc_call;
2517 *p++ = cpu_to_be32(RPC_VERSION);
2518 *p++ = cpu_to_be32(clnt->cl_prog);
2519 *p++ = cpu_to_be32(clnt->cl_vers);
2520 *p = cpu_to_be32(task->tk_msg.rpc_proc->p_proc);
Chuck Lever808012f2005-08-25 16:25:49 -07002521
Chuck Levere8680a22019-02-11 11:24:48 -05002522 error = rpcauth_marshcred(task, xdr);
2523 if (error < 0)
2524 goto out_fail;
2525 return 0;
2526out_fail:
2527 trace_rpc_bad_callhdr(task);
Trond Myklebust714fbc72019-09-12 08:06:51 -04002528 rpc_call_rpcerror(task, error);
Chuck Levere8680a22019-02-11 11:24:48 -05002529 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530}
2531
Chuck Levera0584ee2019-02-11 11:24:58 -05002532static noinline int
2533rpc_decode_header(struct rpc_task *task, struct xdr_stream *xdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534{
Trond Myklebust4e0038b2012-03-01 17:01:05 -05002535 struct rpc_clnt *clnt = task->tk_client;
Trond Myklebusteb90a162019-03-15 09:29:00 -04002536 int error;
Chuck Levera0584ee2019-02-11 11:24:58 -05002537 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538
Chuck Lever7f5667a2019-02-11 11:24:53 -05002539 /* RFC-1014 says that the representation of XDR data must be a
2540 * multiple of four bytes
2541 * - if it isn't pointer subtraction in the NFS client may give
2542 * undefined results
2543 */
2544 if (task->tk_rqstp->rq_rcv_buf.len & 3)
Trond Myklebusteb90a162019-03-15 09:29:00 -04002545 goto out_unparsable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546
Chuck Levera0584ee2019-02-11 11:24:58 -05002547 p = xdr_inline_decode(xdr, 3 * sizeof(*p));
2548 if (!p)
2549 goto out_unparsable;
Chuck Lever7f5667a2019-02-11 11:24:53 -05002550 p++; /* skip XID */
2551 if (*p++ != rpc_reply)
2552 goto out_unparsable;
2553 if (*p++ != rpc_msg_accepted)
2554 goto out_msg_denied;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04002555
Chuck Levera0584ee2019-02-11 11:24:58 -05002556 error = rpcauth_checkverf(task, xdr);
2557 if (error)
Chuck Lever7f5667a2019-02-11 11:24:53 -05002558 goto out_verifier;
2559
Chuck Levera0584ee2019-02-11 11:24:58 -05002560 p = xdr_inline_decode(xdr, sizeof(*p));
2561 if (!p)
Chuck Lever7f5667a2019-02-11 11:24:53 -05002562 goto out_unparsable;
Chuck Levera0584ee2019-02-11 11:24:58 -05002563 switch (*p) {
Chuck Lever7f5667a2019-02-11 11:24:53 -05002564 case rpc_success:
Chuck Levera0584ee2019-02-11 11:24:58 -05002565 return 0;
Chuck Lever7f5667a2019-02-11 11:24:53 -05002566 case rpc_prog_unavail:
2567 trace_rpc__prog_unavail(task);
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00002568 error = -EPFNOSUPPORT;
2569 goto out_err;
Chuck Lever7f5667a2019-02-11 11:24:53 -05002570 case rpc_prog_mismatch:
2571 trace_rpc__prog_mismatch(task);
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00002572 error = -EPROTONOSUPPORT;
2573 goto out_err;
Chuck Lever7f5667a2019-02-11 11:24:53 -05002574 case rpc_proc_unavail:
2575 trace_rpc__proc_unavail(task);
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00002576 error = -EOPNOTSUPP;
2577 goto out_err;
Chuck Lever7f5667a2019-02-11 11:24:53 -05002578 case rpc_garbage_args:
Trond Myklebust928d42f2019-03-15 10:12:30 -04002579 case rpc_system_err:
Chuck Lever7f5667a2019-02-11 11:24:53 -05002580 trace_rpc__garbage_args(task);
Trond Myklebusteb90a162019-03-15 09:29:00 -04002581 error = -EIO;
Chuck Lever7f5667a2019-02-11 11:24:53 -05002582 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 default:
Trond Myklebusteb90a162019-03-15 09:29:00 -04002584 goto out_unparsable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 }
2586
Trond Myklebustabbcf282006-01-03 09:55:03 +01002587out_garbage:
Trond Myklebust4e0038b2012-03-01 17:01:05 -05002588 clnt->cl_stats->rpcgarbage++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 if (task->tk_garb_retry) {
2590 task->tk_garb_retry--;
Trond Myklebust762e4e62018-08-24 16:28:28 -04002591 task->tk_action = call_encode;
Chuck Levera0584ee2019-02-11 11:24:58 -05002592 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594out_err:
Trond Myklebust714fbc72019-09-12 08:06:51 -04002595 rpc_call_rpcerror(task, error);
Chuck Levera0584ee2019-02-11 11:24:58 -05002596 return error;
Chuck Lever7f5667a2019-02-11 11:24:53 -05002597
Chuck Lever7f5667a2019-02-11 11:24:53 -05002598out_unparsable:
2599 trace_rpc__unparsable(task);
2600 error = -EIO;
Trond Myklebustabbcf282006-01-03 09:55:03 +01002601 goto out_garbage;
Chuck Lever7f5667a2019-02-11 11:24:53 -05002602
2603out_verifier:
2604 trace_rpc_bad_verifier(task);
Chuck Lever7f5667a2019-02-11 11:24:53 -05002605 goto out_garbage;
2606
2607out_msg_denied:
Trond Myklebusteb90a162019-03-15 09:29:00 -04002608 error = -EACCES;
Chuck Levera0584ee2019-02-11 11:24:58 -05002609 p = xdr_inline_decode(xdr, sizeof(*p));
2610 if (!p)
2611 goto out_unparsable;
Chuck Lever7f5667a2019-02-11 11:24:53 -05002612 switch (*p++) {
2613 case rpc_auth_error:
2614 break;
2615 case rpc_mismatch:
2616 trace_rpc__mismatch(task);
2617 error = -EPROTONOSUPPORT;
2618 goto out_err;
2619 default:
Trond Myklebusteb90a162019-03-15 09:29:00 -04002620 goto out_unparsable;
Chuck Lever7f5667a2019-02-11 11:24:53 -05002621 }
2622
Chuck Levera0584ee2019-02-11 11:24:58 -05002623 p = xdr_inline_decode(xdr, sizeof(*p));
2624 if (!p)
2625 goto out_unparsable;
Chuck Lever7f5667a2019-02-11 11:24:53 -05002626 switch (*p++) {
2627 case rpc_autherr_rejectedcred:
2628 case rpc_autherr_rejectedverf:
2629 case rpcsec_gsserr_credproblem:
2630 case rpcsec_gsserr_ctxproblem:
2631 if (!task->tk_cred_retry)
2632 break;
2633 task->tk_cred_retry--;
2634 trace_rpc__stale_creds(task);
Trond Myklebust7987b692019-05-29 12:49:52 -04002635 return -EKEYREJECTED;
Chuck Lever7f5667a2019-02-11 11:24:53 -05002636 case rpc_autherr_badcred:
2637 case rpc_autherr_badverf:
2638 /* possibly garbled cred/verf? */
2639 if (!task->tk_garb_retry)
2640 break;
2641 task->tk_garb_retry--;
2642 trace_rpc__bad_creds(task);
2643 task->tk_action = call_encode;
Chuck Levera0584ee2019-02-11 11:24:58 -05002644 return -EAGAIN;
Chuck Lever7f5667a2019-02-11 11:24:53 -05002645 case rpc_autherr_tooweak:
2646 trace_rpc__auth_tooweak(task);
2647 pr_warn("RPC: server %s requires stronger authentication.\n",
2648 task->tk_xprt->servername);
2649 break;
2650 default:
Trond Myklebusteb90a162019-03-15 09:29:00 -04002651 goto out_unparsable;
Chuck Lever7f5667a2019-02-11 11:24:53 -05002652 }
2653 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654}
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00002655
Christoph Hellwigc512f362017-05-08 09:31:19 +02002656static void rpcproc_encode_null(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
2657 const void *obj)
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00002658{
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00002659}
2660
Christoph Hellwig73c8dc12017-05-08 14:58:11 +02002661static int rpcproc_decode_null(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
2662 void *obj)
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00002663{
2664 return 0;
2665}
2666
Christoph Hellwig499b4982017-05-12 15:36:49 +02002667static const struct rpc_procinfo rpcproc_null = {
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00002668 .p_encode = rpcproc_encode_null,
2669 .p_decode = rpcproc_decode_null,
2670};
2671
Chuck Levercaabea82009-12-03 15:58:56 -05002672static int rpc_ping(struct rpc_clnt *clnt)
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00002673{
2674 struct rpc_message msg = {
2675 .rpc_proc = &rpcproc_null,
2676 };
2677 int err;
NeilBrowna68a72e2018-12-03 11:30:30 +11002678 err = rpc_call_sync(clnt, &msg, RPC_TASK_SOFT | RPC_TASK_SOFTCONN |
2679 RPC_TASK_NULLCREDS);
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00002680 return err;
2681}
Trond Myklebust188fef12007-06-16 14:18:40 -04002682
Trond Myklebust7f554892016-01-30 23:43:35 -05002683static
2684struct rpc_task *rpc_call_null_helper(struct rpc_clnt *clnt,
2685 struct rpc_xprt *xprt, struct rpc_cred *cred, int flags,
2686 const struct rpc_call_ops *ops, void *data)
Trond Myklebust5e1550d2007-06-23 10:17:16 -04002687{
2688 struct rpc_message msg = {
2689 .rpc_proc = &rpcproc_null,
Trond Myklebust5e1550d2007-06-23 10:17:16 -04002690 };
Trond Myklebust84115e12007-07-14 15:39:59 -04002691 struct rpc_task_setup task_setup_data = {
2692 .rpc_client = clnt,
Trond Myklebust7f554892016-01-30 23:43:35 -05002693 .rpc_xprt = xprt,
Trond Myklebust84115e12007-07-14 15:39:59 -04002694 .rpc_message = &msg,
NeilBrown1de7eea2018-12-03 11:30:30 +11002695 .rpc_op_cred = cred,
Trond Myklebust7f554892016-01-30 23:43:35 -05002696 .callback_ops = (ops != NULL) ? ops : &rpc_default_ops,
2697 .callback_data = data,
Chuck Lever841a2ed2020-05-12 17:13:55 -04002698 .flags = flags | RPC_TASK_SOFT | RPC_TASK_SOFTCONN |
2699 RPC_TASK_NULLCREDS,
Trond Myklebust84115e12007-07-14 15:39:59 -04002700 };
Trond Myklebust7f554892016-01-30 23:43:35 -05002701
Trond Myklebustc970aa82007-07-14 15:39:59 -04002702 return rpc_run_task(&task_setup_data);
Trond Myklebust5e1550d2007-06-23 10:17:16 -04002703}
Trond Myklebust7f554892016-01-30 23:43:35 -05002704
2705struct rpc_task *rpc_call_null(struct rpc_clnt *clnt, struct rpc_cred *cred, int flags)
2706{
2707 return rpc_call_null_helper(clnt, NULL, cred, flags, NULL, NULL);
2708}
Trond Myklebuste8914c62007-07-14 15:39:59 -04002709EXPORT_SYMBOL_GPL(rpc_call_null);
Trond Myklebust5e1550d2007-06-23 10:17:16 -04002710
Trond Myklebust7f554892016-01-30 23:43:35 -05002711struct rpc_cb_add_xprt_calldata {
2712 struct rpc_xprt_switch *xps;
2713 struct rpc_xprt *xprt;
2714};
2715
2716static void rpc_cb_add_xprt_done(struct rpc_task *task, void *calldata)
2717{
2718 struct rpc_cb_add_xprt_calldata *data = calldata;
2719
2720 if (task->tk_status == 0)
2721 rpc_xprt_switch_add_xprt(data->xps, data->xprt);
2722}
2723
2724static void rpc_cb_add_xprt_release(void *calldata)
2725{
2726 struct rpc_cb_add_xprt_calldata *data = calldata;
2727
2728 xprt_put(data->xprt);
2729 xprt_switch_put(data->xps);
2730 kfree(data);
2731}
2732
Trond Myklebustce272302016-07-24 17:06:28 -04002733static const struct rpc_call_ops rpc_cb_add_xprt_call_ops = {
Trond Myklebust7f554892016-01-30 23:43:35 -05002734 .rpc_call_done = rpc_cb_add_xprt_done,
2735 .rpc_release = rpc_cb_add_xprt_release,
2736};
2737
2738/**
2739 * rpc_clnt_test_and_add_xprt - Test and add a new transport to a rpc_clnt
2740 * @clnt: pointer to struct rpc_clnt
2741 * @xps: pointer to struct rpc_xprt_switch,
2742 * @xprt: pointer struct rpc_xprt
2743 * @dummy: unused
2744 */
2745int rpc_clnt_test_and_add_xprt(struct rpc_clnt *clnt,
2746 struct rpc_xprt_switch *xps, struct rpc_xprt *xprt,
2747 void *dummy)
2748{
2749 struct rpc_cb_add_xprt_calldata *data;
Trond Myklebust7f554892016-01-30 23:43:35 -05002750 struct rpc_task *task;
2751
2752 data = kmalloc(sizeof(*data), GFP_NOFS);
2753 if (!data)
2754 return -ENOMEM;
2755 data->xps = xprt_switch_get(xps);
2756 data->xprt = xprt_get(xprt);
Trond Myklebust612b41f2017-04-27 08:50:51 -04002757 if (rpc_xprt_switch_has_addr(data->xps, (struct sockaddr *)&xprt->addr)) {
2758 rpc_cb_add_xprt_release(data);
2759 goto success;
2760 }
Trond Myklebust7f554892016-01-30 23:43:35 -05002761
Chuck Lever841a2ed2020-05-12 17:13:55 -04002762 task = rpc_call_null_helper(clnt, xprt, NULL, RPC_TASK_ASYNC,
Trond Myklebust7f554892016-01-30 23:43:35 -05002763 &rpc_cb_add_xprt_call_ops, data);
Xiyu Yangefe57fd2020-04-20 23:14:19 +08002764
Trond Myklebust7f554892016-01-30 23:43:35 -05002765 rpc_put_task(task);
Trond Myklebust612b41f2017-04-27 08:50:51 -04002766success:
Trond Myklebust7f554892016-01-30 23:43:35 -05002767 return 1;
2768}
2769EXPORT_SYMBOL_GPL(rpc_clnt_test_and_add_xprt);
2770
2771/**
Andy Adamsonfda0ab42016-09-09 09:22:26 -04002772 * rpc_clnt_setup_test_and_add_xprt()
2773 *
2774 * This is an rpc_clnt_add_xprt setup() function which returns 1 so:
2775 * 1) caller of the test function must dereference the rpc_xprt_switch
2776 * and the rpc_xprt.
2777 * 2) test function must call rpc_xprt_switch_add_xprt, usually in
2778 * the rpc_call_done routine.
2779 *
2780 * Upon success (return of 1), the test function adds the new
2781 * transport to the rpc_clnt xprt switch
2782 *
2783 * @clnt: struct rpc_clnt to get the new transport
2784 * @xps: the rpc_xprt_switch to hold the new transport
2785 * @xprt: the rpc_xprt to test
2786 * @data: a struct rpc_add_xprt_test pointer that holds the test function
2787 * and test function call data
2788 */
2789int rpc_clnt_setup_test_and_add_xprt(struct rpc_clnt *clnt,
2790 struct rpc_xprt_switch *xps,
2791 struct rpc_xprt *xprt,
2792 void *data)
2793{
Andy Adamsonfda0ab42016-09-09 09:22:26 -04002794 struct rpc_task *task;
2795 struct rpc_add_xprt_test *xtest = (struct rpc_add_xprt_test *)data;
2796 int status = -EADDRINUSE;
2797
2798 xprt = xprt_get(xprt);
2799 xprt_switch_get(xps);
2800
2801 if (rpc_xprt_switch_has_addr(xps, (struct sockaddr *)&xprt->addr))
2802 goto out_err;
2803
2804 /* Test the connection */
Chuck Lever841a2ed2020-05-12 17:13:55 -04002805 task = rpc_call_null_helper(clnt, xprt, NULL, 0, NULL, NULL);
Andy Adamsonfda0ab42016-09-09 09:22:26 -04002806 if (IS_ERR(task)) {
2807 status = PTR_ERR(task);
2808 goto out_err;
2809 }
2810 status = task->tk_status;
2811 rpc_put_task(task);
2812
2813 if (status < 0)
2814 goto out_err;
2815
2816 /* rpc_xprt_switch and rpc_xprt are deferrenced by add_xprt_test() */
2817 xtest->add_xprt_test(clnt, xprt, xtest->data);
2818
Santosh kumar pradhan10e037d2018-12-19 12:29:57 +05302819 xprt_put(xprt);
2820 xprt_switch_put(xps);
2821
Andy Adamsonfda0ab42016-09-09 09:22:26 -04002822 /* so that rpc_clnt_add_xprt does not call rpc_xprt_switch_add_xprt */
2823 return 1;
2824out_err:
2825 xprt_put(xprt);
2826 xprt_switch_put(xps);
2827 pr_info("RPC: rpc_clnt_test_xprt failed: %d addr %s not added\n",
2828 status, xprt->address_strings[RPC_DISPLAY_ADDR]);
2829 return status;
2830}
2831EXPORT_SYMBOL_GPL(rpc_clnt_setup_test_and_add_xprt);
2832
2833/**
Trond Myklebust7f554892016-01-30 23:43:35 -05002834 * rpc_clnt_add_xprt - Add a new transport to a rpc_clnt
2835 * @clnt: pointer to struct rpc_clnt
2836 * @xprtargs: pointer to struct xprt_create
2837 * @setup: callback to test and/or set up the connection
2838 * @data: pointer to setup function data
2839 *
2840 * Creates a new transport using the parameters set in args and
2841 * adds it to clnt.
2842 * If ping is set, then test that connectivity succeeds before
2843 * adding the new transport.
2844 *
2845 */
2846int rpc_clnt_add_xprt(struct rpc_clnt *clnt,
2847 struct xprt_create *xprtargs,
2848 int (*setup)(struct rpc_clnt *,
2849 struct rpc_xprt_switch *,
2850 struct rpc_xprt *,
2851 void *),
2852 void *data)
2853{
2854 struct rpc_xprt_switch *xps;
2855 struct rpc_xprt *xprt;
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002856 unsigned long connect_timeout;
Trond Myklebust3851f1c2016-08-04 00:08:45 -04002857 unsigned long reconnect_timeout;
Trond Myklebuste6237b62019-10-17 11:13:54 -04002858 unsigned char resvport, reuseport;
Trond Myklebust7f554892016-01-30 23:43:35 -05002859 int ret = 0;
2860
2861 rcu_read_lock();
2862 xps = xprt_switch_get(rcu_dereference(clnt->cl_xpi.xpi_xpswitch));
2863 xprt = xprt_iter_xprt(&clnt->cl_xpi);
2864 if (xps == NULL || xprt == NULL) {
2865 rcu_read_unlock();
Lin Yib9622612019-06-10 10:16:56 +08002866 xprt_switch_put(xps);
Trond Myklebust7f554892016-01-30 23:43:35 -05002867 return -EAGAIN;
2868 }
2869 resvport = xprt->resvport;
Trond Myklebuste6237b62019-10-17 11:13:54 -04002870 reuseport = xprt->reuseport;
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002871 connect_timeout = xprt->connect_timeout;
Trond Myklebust3851f1c2016-08-04 00:08:45 -04002872 reconnect_timeout = xprt->max_reconnect_timeout;
Trond Myklebust7f554892016-01-30 23:43:35 -05002873 rcu_read_unlock();
2874
2875 xprt = xprt_create_transport(xprtargs);
2876 if (IS_ERR(xprt)) {
2877 ret = PTR_ERR(xprt);
2878 goto out_put_switch;
2879 }
2880 xprt->resvport = resvport;
Trond Myklebuste6237b62019-10-17 11:13:54 -04002881 xprt->reuseport = reuseport;
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002882 if (xprt->ops->set_connect_timeout != NULL)
2883 xprt->ops->set_connect_timeout(xprt,
2884 connect_timeout,
2885 reconnect_timeout);
Trond Myklebust7f554892016-01-30 23:43:35 -05002886
2887 rpc_xprt_switch_set_roundrobin(xps);
2888 if (setup) {
2889 ret = setup(clnt, xps, xprt, data);
2890 if (ret != 0)
2891 goto out_put_xprt;
2892 }
2893 rpc_xprt_switch_add_xprt(xps, xprt);
2894out_put_xprt:
2895 xprt_put(xprt);
2896out_put_switch:
2897 xprt_switch_put(xps);
2898 return ret;
2899}
2900EXPORT_SYMBOL_GPL(rpc_clnt_add_xprt);
2901
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002902struct connect_timeout_data {
2903 unsigned long connect_timeout;
2904 unsigned long reconnect_timeout;
2905};
2906
Trond Myklebust8d480322016-08-05 19:03:31 -04002907static int
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002908rpc_xprt_set_connect_timeout(struct rpc_clnt *clnt,
Trond Myklebust8d480322016-08-05 19:03:31 -04002909 struct rpc_xprt *xprt,
2910 void *data)
2911{
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002912 struct connect_timeout_data *timeo = data;
Trond Myklebust8d480322016-08-05 19:03:31 -04002913
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002914 if (xprt->ops->set_connect_timeout)
2915 xprt->ops->set_connect_timeout(xprt,
2916 timeo->connect_timeout,
2917 timeo->reconnect_timeout);
Trond Myklebust8d480322016-08-05 19:03:31 -04002918 return 0;
2919}
2920
2921void
Trond Myklebust26ae1022017-02-08 11:17:55 -05002922rpc_set_connect_timeout(struct rpc_clnt *clnt,
2923 unsigned long connect_timeout,
2924 unsigned long reconnect_timeout)
Trond Myklebust8d480322016-08-05 19:03:31 -04002925{
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002926 struct connect_timeout_data timeout = {
Trond Myklebust26ae1022017-02-08 11:17:55 -05002927 .connect_timeout = connect_timeout,
2928 .reconnect_timeout = reconnect_timeout,
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002929 };
Trond Myklebust8d480322016-08-05 19:03:31 -04002930 rpc_clnt_iterate_for_each_xprt(clnt,
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002931 rpc_xprt_set_connect_timeout,
2932 &timeout);
Trond Myklebust8d480322016-08-05 19:03:31 -04002933}
Trond Myklebust26ae1022017-02-08 11:17:55 -05002934EXPORT_SYMBOL_GPL(rpc_set_connect_timeout);
Trond Myklebust8d480322016-08-05 19:03:31 -04002935
Andy Adamson3b58a8a2016-09-09 09:22:23 -04002936void rpc_clnt_xprt_switch_put(struct rpc_clnt *clnt)
2937{
Anna Schumakerbb29dd82016-10-26 10:33:31 -04002938 rcu_read_lock();
Andy Adamson3b58a8a2016-09-09 09:22:23 -04002939 xprt_switch_put(rcu_dereference(clnt->cl_xpi.xpi_xpswitch));
Anna Schumakerbb29dd82016-10-26 10:33:31 -04002940 rcu_read_unlock();
Andy Adamson3b58a8a2016-09-09 09:22:23 -04002941}
2942EXPORT_SYMBOL_GPL(rpc_clnt_xprt_switch_put);
2943
Andy Adamsondd691712016-09-09 09:22:24 -04002944void rpc_clnt_xprt_switch_add_xprt(struct rpc_clnt *clnt, struct rpc_xprt *xprt)
2945{
Anna Schumakerbb29dd82016-10-26 10:33:31 -04002946 rcu_read_lock();
Andy Adamsondd691712016-09-09 09:22:24 -04002947 rpc_xprt_switch_add_xprt(rcu_dereference(clnt->cl_xpi.xpi_xpswitch),
2948 xprt);
Anna Schumakerbb29dd82016-10-26 10:33:31 -04002949 rcu_read_unlock();
Andy Adamsondd691712016-09-09 09:22:24 -04002950}
2951EXPORT_SYMBOL_GPL(rpc_clnt_xprt_switch_add_xprt);
2952
Andy Adamson39e5d2d2016-09-09 09:22:25 -04002953bool rpc_clnt_xprt_switch_has_addr(struct rpc_clnt *clnt,
2954 const struct sockaddr *sap)
2955{
2956 struct rpc_xprt_switch *xps;
2957 bool ret;
2958
Andy Adamson39e5d2d2016-09-09 09:22:25 -04002959 rcu_read_lock();
Anna Schumakerbb29dd82016-10-26 10:33:31 -04002960 xps = rcu_dereference(clnt->cl_xpi.xpi_xpswitch);
Andy Adamson39e5d2d2016-09-09 09:22:25 -04002961 ret = rpc_xprt_switch_has_addr(xps, sap);
2962 rcu_read_unlock();
2963 return ret;
2964}
2965EXPORT_SYMBOL_GPL(rpc_clnt_xprt_switch_has_addr);
2966
Jeff Laytonf895b252014-11-17 16:58:04 -05002967#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
Chuck Lever68a23ee2008-05-21 17:09:26 -04002968static void rpc_show_header(void)
2969{
Chuck Levercb3997b2008-05-21 17:09:41 -04002970 printk(KERN_INFO "-pid- flgs status -client- --rqstp- "
2971 "-timeout ---ops--\n");
Chuck Lever68a23ee2008-05-21 17:09:26 -04002972}
2973
Chuck Lever38e886e2008-05-21 17:09:33 -04002974static void rpc_show_task(const struct rpc_clnt *clnt,
2975 const struct rpc_task *task)
2976{
2977 const char *rpc_waitq = "none";
Chuck Lever38e886e2008-05-21 17:09:33 -04002978
2979 if (RPC_IS_QUEUED(task))
2980 rpc_waitq = rpc_qname(task->tk_waitqueue);
2981
Joe Perchesb3bceda2010-12-21 10:52:24 -05002982 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 -04002983 task->tk_pid, task->tk_flags, task->tk_status,
Trond Myklebust5efd1872019-04-07 13:58:50 -04002984 clnt, task->tk_rqstp, rpc_task_timeout(task), task->tk_ops,
Trond Myklebust55909f22013-08-23 11:48:15 -04002985 clnt->cl_program->name, clnt->cl_vers, rpc_proc_name(task),
Joe Perchesb3bceda2010-12-21 10:52:24 -05002986 task->tk_action, rpc_waitq);
Chuck Lever38e886e2008-05-21 17:09:33 -04002987}
2988
Stanislav Kinsbursky70abc492012-01-12 22:07:51 +04002989void rpc_show_tasks(struct net *net)
Trond Myklebust188fef12007-06-16 14:18:40 -04002990{
2991 struct rpc_clnt *clnt;
Chuck Lever38e886e2008-05-21 17:09:33 -04002992 struct rpc_task *task;
Chuck Lever68a23ee2008-05-21 17:09:26 -04002993 int header = 0;
Stanislav Kinsbursky70abc492012-01-12 22:07:51 +04002994 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
Trond Myklebust188fef12007-06-16 14:18:40 -04002995
Stanislav Kinsbursky70abc492012-01-12 22:07:51 +04002996 spin_lock(&sn->rpc_client_lock);
2997 list_for_each_entry(clnt, &sn->all_clients, cl_clients) {
Trond Myklebust188fef12007-06-16 14:18:40 -04002998 spin_lock(&clnt->cl_lock);
Chuck Lever38e886e2008-05-21 17:09:33 -04002999 list_for_each_entry(task, &clnt->cl_tasks, tk_task) {
Chuck Lever68a23ee2008-05-21 17:09:26 -04003000 if (!header) {
3001 rpc_show_header();
3002 header++;
3003 }
Chuck Lever38e886e2008-05-21 17:09:33 -04003004 rpc_show_task(clnt, task);
Trond Myklebust188fef12007-06-16 14:18:40 -04003005 }
3006 spin_unlock(&clnt->cl_lock);
3007 }
Stanislav Kinsbursky70abc492012-01-12 22:07:51 +04003008 spin_unlock(&sn->rpc_client_lock);
Trond Myklebust188fef12007-06-16 14:18:40 -04003009}
3010#endif
Jeff Layton3c87ef62015-06-03 16:14:25 -04003011
3012#if IS_ENABLED(CONFIG_SUNRPC_SWAP)
Trond Myklebust15001e52016-01-30 20:05:34 -05003013static int
3014rpc_clnt_swap_activate_callback(struct rpc_clnt *clnt,
3015 struct rpc_xprt *xprt,
3016 void *dummy)
3017{
3018 return xprt_enable_swap(xprt);
3019}
3020
Jeff Layton3c87ef62015-06-03 16:14:25 -04003021int
3022rpc_clnt_swap_activate(struct rpc_clnt *clnt)
3023{
Trond Myklebust15001e52016-01-30 20:05:34 -05003024 if (atomic_inc_return(&clnt->cl_swapper) == 1)
3025 return rpc_clnt_iterate_for_each_xprt(clnt,
3026 rpc_clnt_swap_activate_callback, NULL);
3027 return 0;
Jeff Layton3c87ef62015-06-03 16:14:25 -04003028}
3029EXPORT_SYMBOL_GPL(rpc_clnt_swap_activate);
3030
Trond Myklebust15001e52016-01-30 20:05:34 -05003031static int
3032rpc_clnt_swap_deactivate_callback(struct rpc_clnt *clnt,
3033 struct rpc_xprt *xprt,
3034 void *dummy)
3035{
3036 xprt_disable_swap(xprt);
3037 return 0;
3038}
3039
Jeff Layton3c87ef62015-06-03 16:14:25 -04003040void
3041rpc_clnt_swap_deactivate(struct rpc_clnt *clnt)
3042{
Trond Myklebust15001e52016-01-30 20:05:34 -05003043 if (atomic_dec_if_positive(&clnt->cl_swapper) == 0)
3044 rpc_clnt_iterate_for_each_xprt(clnt,
3045 rpc_clnt_swap_deactivate_callback, NULL);
Jeff Layton3c87ef62015-06-03 16:14:25 -04003046}
3047EXPORT_SYMBOL_GPL(rpc_clnt_swap_deactivate);
3048#endif /* CONFIG_SUNRPC_SWAP */