blob: 612f0a641f4cf62eb20e1259cb2efe106b9f7823 [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Chuck Lever55aa4f52005-08-11 16:25:47 -04003 * linux/net/sunrpc/clnt.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * This file contains the high-level RPC interface.
6 * It is modeled as a finite state machine to support both synchronous
7 * and asynchronous requests.
8 *
9 * - RPC header generation and argument serialization.
10 * - Credential refresh.
11 * - TCP connect handling.
12 * - Retry of operation when it is suspected the operation failed because
13 * of uid squashing on the server, or when the credentials were stale
14 * and need to be refreshed, or when a packet was damaged in transit.
15 * This may be have to be moved to the VFS layer.
16 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 * Copyright (C) 1992,1993 Rick Sladkey <jrs@world.std.com>
18 * Copyright (C) 1995,1996 Olaf Kirch <okir@monad.swb.de>
19 */
20
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
22#include <linux/module.h>
23#include <linux/types.h>
Chuck Levercb3997b2008-05-21 17:09:41 -040024#include <linux/kallsyms.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/mm.h>
Trond Myklebust23ac6582009-08-09 15:14:25 -040026#include <linux/namei.h>
27#include <linux/mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/slab.h>
Trond Myklebust40b00b6b2013-10-17 14:12:23 -040029#include <linux/rcupdate.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/utsname.h>
Chuck Lever11c556b2006-03-20 13:44:22 -050031#include <linux/workqueue.h>
Chuck Lever176e21e2011-05-09 15:22:44 -040032#include <linux/in.h>
Chuck Lever510deb02007-12-10 14:56:24 -050033#include <linux/in6.h>
Chuck Lever176e21e2011-05-09 15:22:44 -040034#include <linux/un.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36#include <linux/sunrpc/clnt.h>
Jeff Layton59766872013-02-04 12:50:00 -050037#include <linux/sunrpc/addr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/sunrpc/rpc_pipe_fs.h>
Chuck Lever11c556b2006-03-20 13:44:22 -050039#include <linux/sunrpc/metrics.h>
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -040040#include <linux/sunrpc/bc_xprt.h>
Steve Dickson5753cba2012-02-06 10:08:08 -050041#include <trace/events/sunrpc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -040043#include "sunrpc.h"
Stanislav Kinsbursky70abc492012-01-12 22:07:51 +040044#include "netns.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Jeff Laytonf895b252014-11-17 16:58:04 -050046#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
Linus Torvalds1da177e2005-04-16 15:20:36 -070047# define RPCDBG_FACILITY RPCDBG_CALL
48#endif
49
Trond Myklebust188fef12007-06-16 14:18:40 -040050/*
51 * All RPC clients are linked into this list
52 */
Trond Myklebust188fef12007-06-16 14:18:40 -040053
Linus Torvalds1da177e2005-04-16 15:20:36 -070054static DECLARE_WAIT_QUEUE_HEAD(destroy_wait);
55
56
57static void call_start(struct rpc_task *task);
58static void call_reserve(struct rpc_task *task);
59static void call_reserveresult(struct rpc_task *task);
60static void call_allocate(struct rpc_task *task);
Trond Myklebust762e4e62018-08-24 16:28:28 -040061static void call_encode(struct rpc_task *task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062static void call_decode(struct rpc_task *task);
63static void call_bind(struct rpc_task *task);
Chuck Leverda351872005-08-11 16:25:11 -040064static void call_bind_status(struct rpc_task *task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065static void call_transmit(struct rpc_task *task);
66static void call_status(struct rpc_task *task);
Trond Myklebust940e3312005-11-09 21:45:24 -050067static void call_transmit_status(struct rpc_task *task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068static void call_refresh(struct rpc_task *task);
69static void call_refreshresult(struct rpc_task *task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070static void call_connect(struct rpc_task *task);
71static void call_connect_status(struct rpc_task *task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Chuck Levere8680a22019-02-11 11:24:48 -050073static int rpc_encode_header(struct rpc_task *task,
74 struct xdr_stream *xdr);
Chuck Levera0584ee2019-02-11 11:24:58 -050075static int rpc_decode_header(struct rpc_task *task,
76 struct xdr_stream *xdr);
Chuck Levercaabea82009-12-03 15:58:56 -050077static int rpc_ping(struct rpc_clnt *clnt);
Trond Myklebust7b3fef82019-03-07 14:10:32 -050078static void rpc_check_timeout(struct rpc_task *task);
Trond Myklebust64c91a12007-06-23 10:17:16 -040079
Trond Myklebust188fef12007-06-16 14:18:40 -040080static void rpc_register_client(struct rpc_clnt *clnt)
81{
Trond Myklebust2446ab62012-03-01 17:00:56 -050082 struct net *net = rpc_net_ns(clnt);
83 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
Stanislav Kinsbursky70abc492012-01-12 22:07:51 +040084
85 spin_lock(&sn->rpc_client_lock);
86 list_add(&clnt->cl_clients, &sn->all_clients);
87 spin_unlock(&sn->rpc_client_lock);
Trond Myklebust188fef12007-06-16 14:18:40 -040088}
89
90static void rpc_unregister_client(struct rpc_clnt *clnt)
91{
Trond Myklebust2446ab62012-03-01 17:00:56 -050092 struct net *net = rpc_net_ns(clnt);
93 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
Stanislav Kinsbursky70abc492012-01-12 22:07:51 +040094
95 spin_lock(&sn->rpc_client_lock);
Trond Myklebust188fef12007-06-16 14:18:40 -040096 list_del(&clnt->cl_clients);
Stanislav Kinsbursky70abc492012-01-12 22:07:51 +040097 spin_unlock(&sn->rpc_client_lock);
Trond Myklebust188fef12007-06-16 14:18:40 -040098}
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400100static void __rpc_clnt_remove_pipedir(struct rpc_clnt *clnt)
101{
Trond Myklebustc36dcfe12013-08-26 17:44:26 -0400102 rpc_remove_client_dir(clnt);
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400103}
104
105static void rpc_clnt_remove_pipedir(struct rpc_clnt *clnt)
106{
Trond Myklebust2446ab62012-03-01 17:00:56 -0500107 struct net *net = rpc_net_ns(clnt);
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400108 struct super_block *pipefs_sb;
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400109
Trond Myklebust2446ab62012-03-01 17:00:56 -0500110 pipefs_sb = rpc_get_sb_net(net);
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400111 if (pipefs_sb) {
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400112 __rpc_clnt_remove_pipedir(clnt);
Trond Myklebust2446ab62012-03-01 17:00:56 -0500113 rpc_put_sb_net(net);
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400114 }
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400115}
116
117static struct dentry *rpc_setup_pipedir_sb(struct super_block *sb,
Trond Myklebust41b6b4d2013-08-23 13:03:50 -0400118 struct rpc_clnt *clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119{
Trond Myklebustf1345852005-09-23 11:08:25 -0400120 static uint32_t clntid;
Trond Myklebust41b6b4d2013-08-23 13:03:50 -0400121 const char *dir_name = clnt->cl_program->pipe_dir_name;
Trond Myklebust23ac6582009-08-09 15:14:25 -0400122 char name[15];
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400123 struct dentry *dir, *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400125 dir = rpc_d_lookup_sb(sb, dir_name);
Weston Andros Adamson922eeac2012-10-23 10:43:25 -0400126 if (dir == NULL) {
127 pr_info("RPC: pipefs directory doesn't exist: %s\n", dir_name);
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400128 return dir;
Weston Andros Adamson922eeac2012-10-23 10:43:25 -0400129 }
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400130 for (;;) {
Al Viroa95e6912013-07-14 16:43:54 +0400131 snprintf(name, sizeof(name), "clnt%x", (unsigned int)clntid++);
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400132 name[sizeof(name) - 1] = '\0';
Al Viroa95e6912013-07-14 16:43:54 +0400133 dentry = rpc_create_client_dir(dir, name, clnt);
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400134 if (!IS_ERR(dentry))
135 break;
Al Viroa95e6912013-07-14 16:43:54 +0400136 if (dentry == ERR_PTR(-EEXIST))
137 continue;
138 printk(KERN_INFO "RPC: Couldn't create pipefs entry"
139 " %s/%s, error %ld\n",
140 dir_name, name, PTR_ERR(dentry));
141 break;
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400142 }
143 dput(dir);
144 return dentry;
145}
146
147static int
Trond Myklebust41b6b4d2013-08-23 13:03:50 -0400148rpc_setup_pipedir(struct super_block *pipefs_sb, struct rpc_clnt *clnt)
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400149{
Stanislav Kinsbursky30507f52012-01-11 19:18:42 +0400150 struct dentry *dentry;
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400151
Trond Myklebustc36dcfe12013-08-26 17:44:26 -0400152 if (clnt->cl_program->pipe_dir_name != NULL) {
153 dentry = rpc_setup_pipedir_sb(pipefs_sb, clnt);
154 if (IS_ERR(dentry))
155 return PTR_ERR(dentry);
156 }
Trond Myklebust23ac6582009-08-09 15:14:25 -0400157 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158}
159
Trond Myklebust41b6b4d2013-08-23 13:03:50 -0400160static int rpc_clnt_skip_event(struct rpc_clnt *clnt, unsigned long event)
Stanislav Kinsburskyea8cfa02012-04-27 13:00:17 +0400161{
Trond Myklebust41b6b4d2013-08-23 13:03:50 -0400162 if (clnt->cl_program->pipe_dir_name == NULL)
Stanislav Kinsburskyea8cfa02012-04-27 13:00:17 +0400163 return 1;
Trond Myklebust41b6b4d2013-08-23 13:03:50 -0400164
Trond Myklebustc36dcfe12013-08-26 17:44:26 -0400165 switch (event) {
166 case RPC_PIPEFS_MOUNT:
167 if (clnt->cl_pipedir_objects.pdh_dentry != NULL)
168 return 1;
169 if (atomic_read(&clnt->cl_count) == 0)
170 return 1;
171 break;
172 case RPC_PIPEFS_UMOUNT:
173 if (clnt->cl_pipedir_objects.pdh_dentry == NULL)
174 return 1;
175 break;
176 }
Stanislav Kinsburskyea8cfa02012-04-27 13:00:17 +0400177 return 0;
178}
179
180static int __rpc_clnt_handle_event(struct rpc_clnt *clnt, unsigned long event,
181 struct super_block *sb)
Stanislav Kinsbursky80df9d22012-01-11 19:18:17 +0400182{
183 struct dentry *dentry;
Stanislav Kinsbursky80df9d22012-01-11 19:18:17 +0400184
185 switch (event) {
186 case RPC_PIPEFS_MOUNT:
Trond Myklebust41b6b4d2013-08-23 13:03:50 -0400187 dentry = rpc_setup_pipedir_sb(sb, clnt);
Weston Andros Adamson922eeac2012-10-23 10:43:25 -0400188 if (!dentry)
189 return -ENOENT;
Stanislav Kinsbursky80df9d22012-01-11 19:18:17 +0400190 if (IS_ERR(dentry))
191 return PTR_ERR(dentry);
Stanislav Kinsbursky80df9d22012-01-11 19:18:17 +0400192 break;
193 case RPC_PIPEFS_UMOUNT:
194 __rpc_clnt_remove_pipedir(clnt);
195 break;
196 default:
197 printk(KERN_ERR "%s: unknown event: %ld\n", __func__, event);
198 return -ENOTSUPP;
199 }
Amitoj Kaur Chawla2813b622016-08-08 14:43:49 +0530200 return 0;
Stanislav Kinsbursky80df9d22012-01-11 19:18:17 +0400201}
202
Stanislav Kinsburskyea8cfa02012-04-27 13:00:17 +0400203static int __rpc_pipefs_event(struct rpc_clnt *clnt, unsigned long event,
204 struct super_block *sb)
205{
206 int error = 0;
207
208 for (;; clnt = clnt->cl_parent) {
209 if (!rpc_clnt_skip_event(clnt, event))
210 error = __rpc_clnt_handle_event(clnt, event, sb);
211 if (error || clnt == clnt->cl_parent)
212 break;
213 }
214 return error;
215}
216
Stanislav Kinsburskyda3b4622012-02-27 22:05:29 +0400217static struct rpc_clnt *rpc_get_client_for_event(struct net *net, int event)
218{
219 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
220 struct rpc_clnt *clnt;
221
222 spin_lock(&sn->rpc_client_lock);
223 list_for_each_entry(clnt, &sn->all_clients, cl_clients) {
Stanislav Kinsburskyea8cfa02012-04-27 13:00:17 +0400224 if (rpc_clnt_skip_event(clnt, event))
Stanislav Kinsburskyda3b4622012-02-27 22:05:29 +0400225 continue;
Stanislav Kinsburskyda3b4622012-02-27 22:05:29 +0400226 spin_unlock(&sn->rpc_client_lock);
227 return clnt;
228 }
229 spin_unlock(&sn->rpc_client_lock);
230 return NULL;
231}
232
Stanislav Kinsbursky80df9d22012-01-11 19:18:17 +0400233static int rpc_pipefs_event(struct notifier_block *nb, unsigned long event,
234 void *ptr)
235{
236 struct super_block *sb = ptr;
237 struct rpc_clnt *clnt;
238 int error = 0;
Stanislav Kinsbursky80df9d22012-01-11 19:18:17 +0400239
Stanislav Kinsburskyda3b4622012-02-27 22:05:29 +0400240 while ((clnt = rpc_get_client_for_event(sb->s_fs_info, event))) {
Stanislav Kinsbursky80df9d22012-01-11 19:18:17 +0400241 error = __rpc_pipefs_event(clnt, event, sb);
242 if (error)
243 break;
244 }
Stanislav Kinsbursky80df9d22012-01-11 19:18:17 +0400245 return error;
246}
247
248static struct notifier_block rpc_clients_block = {
249 .notifier_call = rpc_pipefs_event,
Stanislav Kinsburskyeee173252012-01-10 16:13:19 +0400250 .priority = SUNRPC_PIPEFS_RPC_PRIO,
Stanislav Kinsbursky80df9d22012-01-11 19:18:17 +0400251};
252
253int rpc_clients_notifier_register(void)
254{
255 return rpc_pipefs_notifier_register(&rpc_clients_block);
256}
257
258void rpc_clients_notifier_unregister(void)
259{
260 return rpc_pipefs_notifier_unregister(&rpc_clients_block);
261}
262
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400263static struct rpc_xprt *rpc_clnt_set_transport(struct rpc_clnt *clnt,
264 struct rpc_xprt *xprt,
265 const struct rpc_timeout *timeout)
266{
267 struct rpc_xprt *old;
268
269 spin_lock(&clnt->cl_lock);
Trond Myklebust34751b92013-10-28 16:42:44 -0400270 old = rcu_dereference_protected(clnt->cl_xprt,
271 lockdep_is_held(&clnt->cl_lock));
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400272
273 if (!xprt_bound(xprt))
274 clnt->cl_autobind = 1;
275
276 clnt->cl_timeout = timeout;
277 rcu_assign_pointer(clnt->cl_xprt, xprt);
278 spin_unlock(&clnt->cl_lock);
279
280 return old;
281}
282
Trond Myklebustcbbb3442012-04-30 11:52:40 -0400283static void rpc_clnt_set_nodename(struct rpc_clnt *clnt, const char *nodename)
284{
Trond Myklebust03a9a422015-01-30 18:12:28 -0500285 clnt->cl_nodelen = strlcpy(clnt->cl_nodename,
286 nodename, sizeof(clnt->cl_nodename));
Trond Myklebustcbbb3442012-04-30 11:52:40 -0400287}
288
Chuck Leverd746e542013-10-17 14:12:17 -0400289static int rpc_client_register(struct rpc_clnt *clnt,
290 rpc_authflavor_t pseudoflavor,
291 const char *client_name)
Stanislav Kinsburskye73f4cc2013-06-24 11:52:52 +0400292{
Trond Myklebustc2190662013-08-26 19:23:04 -0400293 struct rpc_auth_create_args auth_args = {
Chuck Leverd746e542013-10-17 14:12:17 -0400294 .pseudoflavor = pseudoflavor,
295 .target_name = client_name,
Trond Myklebustc2190662013-08-26 19:23:04 -0400296 };
Stanislav Kinsburskye73f4cc2013-06-24 11:52:52 +0400297 struct rpc_auth *auth;
298 struct net *net = rpc_net_ns(clnt);
299 struct super_block *pipefs_sb;
Trond Myklebusteeee2452013-07-10 15:33:01 -0400300 int err;
Stanislav Kinsburskye73f4cc2013-06-24 11:52:52 +0400301
Jeff Laytonf9c72d12015-03-31 12:03:28 -0400302 rpc_clnt_debugfs_register(clnt);
Jeff Laytonb4b9d2c2014-11-26 14:44:43 -0500303
Stanislav Kinsburskye73f4cc2013-06-24 11:52:52 +0400304 pipefs_sb = rpc_get_sb_net(net);
305 if (pipefs_sb) {
Trond Myklebust41b6b4d2013-08-23 13:03:50 -0400306 err = rpc_setup_pipedir(pipefs_sb, clnt);
Stanislav Kinsburskye73f4cc2013-06-24 11:52:52 +0400307 if (err)
308 goto out;
309 }
310
Trond Myklebusteeee2452013-07-10 15:33:01 -0400311 rpc_register_client(clnt);
312 if (pipefs_sb)
313 rpc_put_sb_net(net);
314
Trond Myklebustc2190662013-08-26 19:23:04 -0400315 auth = rpcauth_create(&auth_args, clnt);
Stanislav Kinsburskye73f4cc2013-06-24 11:52:52 +0400316 if (IS_ERR(auth)) {
317 dprintk("RPC: Couldn't create auth handle (flavor %u)\n",
Chuck Leverd746e542013-10-17 14:12:17 -0400318 pseudoflavor);
Stanislav Kinsburskye73f4cc2013-06-24 11:52:52 +0400319 err = PTR_ERR(auth);
320 goto err_auth;
321 }
Trond Myklebusteeee2452013-07-10 15:33:01 -0400322 return 0;
323err_auth:
324 pipefs_sb = rpc_get_sb_net(net);
Trond Myklebust1540c5d2013-07-14 22:57:50 -0400325 rpc_unregister_client(clnt);
Trond Myklebusteeee2452013-07-10 15:33:01 -0400326 __rpc_clnt_remove_pipedir(clnt);
Stanislav Kinsburskye73f4cc2013-06-24 11:52:52 +0400327out:
328 if (pipefs_sb)
329 rpc_put_sb_net(net);
Jeff Laytonb4b9d2c2014-11-26 14:44:43 -0500330 rpc_clnt_debugfs_unregister(clnt);
Stanislav Kinsburskye73f4cc2013-06-24 11:52:52 +0400331 return err;
Stanislav Kinsburskye73f4cc2013-06-24 11:52:52 +0400332}
333
Trond Myklebust2f048db2013-09-04 21:51:44 -0400334static DEFINE_IDA(rpc_clids);
335
Kinglong Meec929ea02017-01-20 16:48:39 +0800336void rpc_cleanup_clids(void)
337{
338 ida_destroy(&rpc_clids);
339}
340
Trond Myklebust2f048db2013-09-04 21:51:44 -0400341static int rpc_alloc_clid(struct rpc_clnt *clnt)
342{
343 int clid;
344
345 clid = ida_simple_get(&rpc_clids, 0, 0, GFP_KERNEL);
346 if (clid < 0)
347 return clid;
348 clnt->cl_clid = clid;
349 return 0;
350}
351
352static void rpc_free_clid(struct rpc_clnt *clnt)
353{
354 ida_simple_remove(&rpc_clids, clnt->cl_clid);
355}
356
Trond Myklebust280ebcf2013-09-02 13:28:04 -0400357static struct rpc_clnt * rpc_new_client(const struct rpc_create_args *args,
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500358 struct rpc_xprt_switch *xps,
Trond Myklebust280ebcf2013-09-02 13:28:04 -0400359 struct rpc_xprt *xprt,
360 struct rpc_clnt *parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361{
Trond Myklebusta613fa12012-01-20 13:53:56 -0500362 const struct rpc_program *program = args->program;
363 const struct rpc_version *version;
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400364 struct rpc_clnt *clnt = NULL;
365 const struct rpc_timeout *timeout;
Trond Myklebust03a9a422015-01-30 18:12:28 -0500366 const char *nodename = args->nodename;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 int err;
Chuck Lever06b8d252007-09-11 18:00:20 -0400368
Trond Myklebust4ada5392007-06-14 17:26:17 -0400369 err = rpciod_up();
370 if (err)
371 goto out_no_rpciod;
Trond Myklebust698b6d02007-12-20 16:03:53 -0500372
Trond Myklebustf05c1242013-04-05 14:13:21 -0400373 err = -EINVAL;
Trond Myklebust698b6d02007-12-20 16:03:53 -0500374 if (args->version >= program->nrvers)
375 goto out_err;
376 version = program->version[args->version];
377 if (version == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 goto out_err;
379
380 err = -ENOMEM;
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700381 clnt = kzalloc(sizeof(*clnt), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 if (!clnt)
383 goto out_err;
Trond Myklebust280ebcf2013-09-02 13:28:04 -0400384 clnt->cl_parent = parent ? : clnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
Trond Myklebust2f048db2013-09-04 21:51:44 -0400386 err = rpc_alloc_clid(clnt);
387 if (err)
388 goto out_no_clid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Trond Myklebust79caa5f2019-04-24 17:46:42 -0400390 clnt->cl_cred = get_cred(args->cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 clnt->cl_procinfo = version->procs;
392 clnt->cl_maxproc = version->nrprocs;
Benny Halevyd5b337b2008-09-28 09:21:26 +0300393 clnt->cl_prog = args->prognumber ? : program->number;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 clnt->cl_vers = version->number;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 clnt->cl_stats = program->stats;
Chuck Lever11c556b2006-03-20 13:44:22 -0500396 clnt->cl_metrics = rpc_alloc_iostats(clnt);
Trond Myklebust6739ffb2013-08-26 15:38:11 -0400397 rpc_init_pipe_dir_head(&clnt->cl_pipedir_objects);
Trond Myklebust23bf85b2006-11-21 10:40:23 -0500398 err = -ENOMEM;
399 if (clnt->cl_metrics == NULL)
400 goto out_no_stats;
Trond Myklebust3e32a5d2006-11-16 11:37:27 -0500401 clnt->cl_program = program;
Trond Myklebust6529eba2007-06-14 16:40:14 -0400402 INIT_LIST_HEAD(&clnt->cl_tasks);
Trond Myklebust4bef61f2007-06-16 14:17:01 -0400403 spin_lock_init(&clnt->cl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400405 timeout = xprt->timeout;
Trond Myklebustba7392b2007-12-20 16:03:55 -0500406 if (args->timeout != NULL) {
407 memcpy(&clnt->cl_timeout_default, args->timeout,
408 sizeof(clnt->cl_timeout_default));
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400409 timeout = &clnt->cl_timeout_default;
Trond Myklebustba7392b2007-12-20 16:03:55 -0500410 }
411
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400412 rpc_clnt_set_transport(clnt, xprt, timeout);
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500413 xprt_iter_init(&clnt->cl_xpi, xps);
414 xprt_switch_put(xps);
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400415
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 clnt->cl_rtt = &clnt->cl_rtt_default;
Trond Myklebustba7392b2007-12-20 16:03:55 -0500417 rpc_init_rtt(&clnt->cl_rtt_default, clnt->cl_timeout->to_initval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Trond Myklebust006abe82010-09-12 19:55:25 -0400419 atomic_set(&clnt->cl_count, 1);
Trond Myklebust34f52e32007-06-14 16:40:31 -0400420
Trond Myklebust03a9a422015-01-30 18:12:28 -0500421 if (nodename == NULL)
422 nodename = utsname()->nodename;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 /* save the nodename */
Trond Myklebust03a9a422015-01-30 18:12:28 -0500424 rpc_clnt_set_nodename(clnt, nodename);
Stanislav Kinsburskye73f4cc2013-06-24 11:52:52 +0400425
Chuck Leverd746e542013-10-17 14:12:17 -0400426 err = rpc_client_register(clnt, args->authflavor, args->client_name);
Stanislav Kinsburskye73f4cc2013-06-24 11:52:52 +0400427 if (err)
428 goto out_no_path;
Trond Myklebust280ebcf2013-09-02 13:28:04 -0400429 if (parent)
430 atomic_inc(&parent->cl_count);
Chuck Lever42aad0d2020-05-12 17:13:39 -0400431
432 trace_rpc_clnt_new(clnt, xprt, program->name, args->servername);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 return clnt;
434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435out_no_path:
Trond Myklebust23bf85b2006-11-21 10:40:23 -0500436 rpc_free_iostats(clnt->cl_metrics);
437out_no_stats:
Trond Myklebust79caa5f2019-04-24 17:46:42 -0400438 put_cred(clnt->cl_cred);
Trond Myklebust2f048db2013-09-04 21:51:44 -0400439 rpc_free_clid(clnt);
440out_no_clid:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 kfree(clnt);
442out_err:
Trond Myklebust4ada5392007-06-14 17:26:17 -0400443 rpciod_down();
444out_no_rpciod:
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500445 xprt_switch_put(xps);
Trond Myklebustf05c1242013-04-05 14:13:21 -0400446 xprt_put(xprt);
Chuck Lever42aad0d2020-05-12 17:13:39 -0400447 trace_rpc_clnt_new_err(program->name, args->servername, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 return ERR_PTR(err);
449}
450
J. Bruce Fieldsd50039e2016-05-16 17:03:42 -0400451static struct rpc_clnt *rpc_create_xprt(struct rpc_create_args *args,
Kinglong Mee83ddfeb2014-03-24 11:58:59 +0800452 struct rpc_xprt *xprt)
453{
454 struct rpc_clnt *clnt = NULL;
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500455 struct rpc_xprt_switch *xps;
Kinglong Mee83ddfeb2014-03-24 11:58:59 +0800456
J. Bruce Fields39a9bea2016-05-17 12:38:21 -0400457 if (args->bc_xprt && args->bc_xprt->xpt_bc_xps) {
Chuck Lever16590a22016-08-22 14:57:42 -0400458 WARN_ON_ONCE(!(args->protocol & XPRT_TRANSPORT_BC));
J. Bruce Fields39a9bea2016-05-17 12:38:21 -0400459 xps = args->bc_xprt->xpt_bc_xps;
460 xprt_switch_get(xps);
461 } else {
462 xps = xprt_switch_alloc(xprt, GFP_KERNEL);
463 if (xps == NULL) {
464 xprt_put(xprt);
465 return ERR_PTR(-ENOMEM);
466 }
467 if (xprt->bc_xprt) {
468 xprt_switch_get(xps);
469 xprt->bc_xprt->xpt_bc_xps = xps;
470 }
J. Bruce Fields1208fd52016-05-20 17:07:17 -0400471 }
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500472 clnt = rpc_new_client(args, xps, xprt, NULL);
Kinglong Mee83ddfeb2014-03-24 11:58:59 +0800473 if (IS_ERR(clnt))
474 return clnt;
475
476 if (!(args->flags & RPC_CLNT_CREATE_NOPING)) {
477 int err = rpc_ping(clnt);
478 if (err != 0) {
479 rpc_shutdown_client(clnt);
480 return ERR_PTR(err);
481 }
482 }
483
484 clnt->cl_softrtry = 1;
Trond Myklebustae6ec912019-04-07 13:58:58 -0400485 if (args->flags & (RPC_CLNT_CREATE_HARDRTRY|RPC_CLNT_CREATE_SOFTERR)) {
Kinglong Mee83ddfeb2014-03-24 11:58:59 +0800486 clnt->cl_softrtry = 0;
Trond Myklebustae6ec912019-04-07 13:58:58 -0400487 if (args->flags & RPC_CLNT_CREATE_SOFTERR)
488 clnt->cl_softerr = 1;
489 }
Kinglong Mee83ddfeb2014-03-24 11:58:59 +0800490
491 if (args->flags & RPC_CLNT_CREATE_AUTOBIND)
492 clnt->cl_autobind = 1;
Trond Myklebust2aca5b82014-09-24 22:35:58 -0400493 if (args->flags & RPC_CLNT_CREATE_NO_RETRANS_TIMEOUT)
494 clnt->cl_noretranstimeo = 1;
Kinglong Mee83ddfeb2014-03-24 11:58:59 +0800495 if (args->flags & RPC_CLNT_CREATE_DISCRTRY)
496 clnt->cl_discrtry = 1;
497 if (!(args->flags & RPC_CLNT_CREATE_QUIET))
498 clnt->cl_chatty = 1;
499
500 return clnt;
501}
Kinglong Mee83ddfeb2014-03-24 11:58:59 +0800502
Ben Hutchings2c530402012-07-10 10:55:09 +0000503/**
Chuck Leverc2866762006-08-22 20:06:20 -0400504 * rpc_create - create an RPC client and transport with one call
505 * @args: rpc_clnt create argument structure
506 *
507 * Creates and initializes an RPC transport and an RPC client.
508 *
509 * It can ping the server in order to determine if it is up, and to see if
510 * it supports this program and version. RPC_CLNT_CREATE_NOPING disables
511 * this behavior so asynchronous tasks can also use rpc_create.
512 */
513struct rpc_clnt *rpc_create(struct rpc_create_args *args)
514{
515 struct rpc_xprt *xprt;
\"Talpey, Thomas\3c341b0b2007-09-10 13:47:07 -0400516 struct xprt_create xprtargs = {
Pavel Emelyanov9a23e332010-09-29 16:05:12 +0400517 .net = args->net,
\"Talpey, Thomas\4fa016e2007-09-10 13:47:57 -0400518 .ident = args->protocol,
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +0200519 .srcaddr = args->saddress,
Frank van Maarseveen96802a02007-07-08 13:08:54 +0200520 .dstaddr = args->address,
521 .addrlen = args->addrsize,
Trond Myklebust4e0038b2012-03-01 17:01:05 -0500522 .servername = args->servername,
Alexandros Batsakisf300bab2009-09-10 17:33:30 +0300523 .bc_xprt = args->bc_xprt,
Frank van Maarseveen96802a02007-07-08 13:08:54 +0200524 };
Chuck Lever510deb02007-12-10 14:56:24 -0500525 char servername[48];
Trond Myklebust612b41f2017-04-27 08:50:51 -0400526 struct rpc_clnt *clnt;
527 int i;
Chuck Leverc2866762006-08-22 20:06:20 -0400528
J. Bruce Fieldsd50039e2016-05-16 17:03:42 -0400529 if (args->bc_xprt) {
Chuck Lever16590a22016-08-22 14:57:42 -0400530 WARN_ON_ONCE(!(args->protocol & XPRT_TRANSPORT_BC));
J. Bruce Fieldsd50039e2016-05-16 17:03:42 -0400531 xprt = args->bc_xprt->xpt_bc_xprt;
532 if (xprt) {
533 xprt_get(xprt);
534 return rpc_create_xprt(args, xprt);
535 }
536 }
537
Trond Myklebustb7993ce2013-04-14 11:42:00 -0400538 if (args->flags & RPC_CLNT_CREATE_INFINITE_SLOTS)
539 xprtargs.flags |= XPRT_CREATE_INFINITE_SLOTS;
J. Bruce Fields33d90ac2013-04-11 15:06:36 -0400540 if (args->flags & RPC_CLNT_CREATE_NO_IDLE_TIMEOUT)
541 xprtargs.flags |= XPRT_CREATE_NO_IDLE_TIMEOUT;
Chuck Leverc2866762006-08-22 20:06:20 -0400542 /*
Chuck Lever43780b82007-07-01 12:13:22 -0400543 * If the caller chooses not to specify a hostname, whip
544 * up a string representation of the passed-in address.
545 */
Trond Myklebust4e0038b2012-03-01 17:01:05 -0500546 if (xprtargs.servername == NULL) {
Chuck Lever176e21e2011-05-09 15:22:44 -0400547 struct sockaddr_un *sun =
548 (struct sockaddr_un *)args->address;
Chuck Leverda09eb92011-05-09 15:22:25 -0400549 struct sockaddr_in *sin =
550 (struct sockaddr_in *)args->address;
551 struct sockaddr_in6 *sin6 =
552 (struct sockaddr_in6 *)args->address;
553
Chuck Lever510deb02007-12-10 14:56:24 -0500554 servername[0] = '\0';
555 switch (args->address->sa_family) {
Chuck Lever176e21e2011-05-09 15:22:44 -0400556 case AF_LOCAL:
557 snprintf(servername, sizeof(servername), "%s",
558 sun->sun_path);
559 break;
Chuck Leverda09eb92011-05-09 15:22:25 -0400560 case AF_INET:
Harvey Harrison21454aa2008-10-31 00:54:56 -0700561 snprintf(servername, sizeof(servername), "%pI4",
562 &sin->sin_addr.s_addr);
Chuck Lever510deb02007-12-10 14:56:24 -0500563 break;
Chuck Leverda09eb92011-05-09 15:22:25 -0400564 case AF_INET6:
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700565 snprintf(servername, sizeof(servername), "%pI6",
Chuck Leverda09eb92011-05-09 15:22:25 -0400566 &sin6->sin6_addr);
Chuck Lever510deb02007-12-10 14:56:24 -0500567 break;
Chuck Lever510deb02007-12-10 14:56:24 -0500568 default:
569 /* caller wants default server name, but
570 * address family isn't recognized. */
571 return ERR_PTR(-EINVAL);
572 }
Trond Myklebust4e0038b2012-03-01 17:01:05 -0500573 xprtargs.servername = servername;
Chuck Lever43780b82007-07-01 12:13:22 -0400574 }
575
Chuck Lever510deb02007-12-10 14:56:24 -0500576 xprt = xprt_create_transport(&xprtargs);
577 if (IS_ERR(xprt))
578 return (struct rpc_clnt *)xprt;
579
Chuck Lever43780b82007-07-01 12:13:22 -0400580 /*
Chuck Leverc2866762006-08-22 20:06:20 -0400581 * By default, kernel RPC client connects from a reserved port.
582 * CAP_NET_BIND_SERVICE will not be set for unprivileged requesters,
583 * but it is always enabled for rpciod, which handles the connect
584 * operation.
585 */
586 xprt->resvport = 1;
587 if (args->flags & RPC_CLNT_CREATE_NONPRIVPORT)
588 xprt->resvport = 0;
Trond Myklebuste6237b62019-10-17 11:13:54 -0400589 xprt->reuseport = 0;
590 if (args->flags & RPC_CLNT_CREATE_REUSEPORT)
591 xprt->reuseport = 1;
Chuck Leverc2866762006-08-22 20:06:20 -0400592
Trond Myklebust612b41f2017-04-27 08:50:51 -0400593 clnt = rpc_create_xprt(args, xprt);
594 if (IS_ERR(clnt) || args->nconnect <= 1)
595 return clnt;
596
597 for (i = 0; i < args->nconnect - 1; i++) {
598 if (rpc_clnt_add_xprt(clnt, &xprtargs, NULL, NULL) < 0)
599 break;
600 }
601 return clnt;
Chuck Leverc2866762006-08-22 20:06:20 -0400602}
Chuck Leverb86acd52006-08-22 20:06:22 -0400603EXPORT_SYMBOL_GPL(rpc_create);
Chuck Leverc2866762006-08-22 20:06:20 -0400604
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605/*
606 * This function clones the RPC client structure. It allows us to share the
607 * same transport while varying parameters such as the authentication
608 * flavour.
609 */
Chuck Lever1b63a752012-09-14 17:23:52 -0400610static struct rpc_clnt *__rpc_clone_client(struct rpc_create_args *args,
611 struct rpc_clnt *clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612{
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500613 struct rpc_xprt_switch *xps;
Trond Myklebust2446ab62012-03-01 17:00:56 -0500614 struct rpc_xprt *xprt;
Chuck Lever1b63a752012-09-14 17:23:52 -0400615 struct rpc_clnt *new;
616 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
Chuck Lever1b63a752012-09-14 17:23:52 -0400618 err = -ENOMEM;
Trond Myklebust2446ab62012-03-01 17:00:56 -0500619 rcu_read_lock();
620 xprt = xprt_get(rcu_dereference(clnt->cl_xprt));
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500621 xps = xprt_switch_get(rcu_dereference(clnt->cl_xpi.xpi_xpswitch));
Trond Myklebust2446ab62012-03-01 17:00:56 -0500622 rcu_read_unlock();
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500623 if (xprt == NULL || xps == NULL) {
624 xprt_put(xprt);
625 xprt_switch_put(xps);
Chuck Lever1b63a752012-09-14 17:23:52 -0400626 goto out_err;
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500627 }
Chuck Lever1b63a752012-09-14 17:23:52 -0400628 args->servername = xprt->servername;
Trond Myklebust03a9a422015-01-30 18:12:28 -0500629 args->nodename = clnt->cl_nodename;
Chuck Lever1b63a752012-09-14 17:23:52 -0400630
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500631 new = rpc_new_client(args, xps, xprt, clnt);
Chuck Lever42aad0d2020-05-12 17:13:39 -0400632 if (IS_ERR(new))
633 return new;
Chuck Lever1b63a752012-09-14 17:23:52 -0400634
Chuck Lever1b63a752012-09-14 17:23:52 -0400635 /* Turn off autobind on clones */
636 new->cl_autobind = 0;
637 new->cl_softrtry = clnt->cl_softrtry;
Trond Myklebustae6ec912019-04-07 13:58:58 -0400638 new->cl_softerr = clnt->cl_softerr;
Trond Myklebust2aca5b82014-09-24 22:35:58 -0400639 new->cl_noretranstimeo = clnt->cl_noretranstimeo;
Chuck Lever1b63a752012-09-14 17:23:52 -0400640 new->cl_discrtry = clnt->cl_discrtry;
641 new->cl_chatty = clnt->cl_chatty;
NeilBrown5e169232018-12-03 11:30:30 +1100642 new->cl_principal = clnt->cl_principal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 return new;
Chuck Lever1b63a752012-09-14 17:23:52 -0400644
Chuck Lever1b63a752012-09-14 17:23:52 -0400645out_err:
Chuck Lever42aad0d2020-05-12 17:13:39 -0400646 trace_rpc_clnt_clone_err(clnt, err);
Trond Myklebust3e32a5d2006-11-16 11:37:27 -0500647 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648}
Chuck Lever1b63a752012-09-14 17:23:52 -0400649
650/**
651 * rpc_clone_client - Clone an RPC client structure
652 *
653 * @clnt: RPC client whose parameters are copied
654 *
655 * Returns a fresh RPC client or an ERR_PTR.
656 */
657struct rpc_clnt *rpc_clone_client(struct rpc_clnt *clnt)
658{
659 struct rpc_create_args args = {
660 .program = clnt->cl_program,
661 .prognumber = clnt->cl_prog,
662 .version = clnt->cl_vers,
663 .authflavor = clnt->cl_auth->au_flavor,
Trond Myklebust79caa5f2019-04-24 17:46:42 -0400664 .cred = clnt->cl_cred,
Chuck Lever1b63a752012-09-14 17:23:52 -0400665 };
666 return __rpc_clone_client(&args, clnt);
667}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400668EXPORT_SYMBOL_GPL(rpc_clone_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
Chuck Leverba9b5842012-09-14 17:24:02 -0400670/**
671 * rpc_clone_client_set_auth - Clone an RPC client structure and set its auth
672 *
673 * @clnt: RPC client whose parameters are copied
Randy Dunlap7144bca2013-01-09 17:12:35 -0800674 * @flavor: security flavor for new client
Chuck Leverba9b5842012-09-14 17:24:02 -0400675 *
676 * Returns a fresh RPC client or an ERR_PTR.
677 */
678struct rpc_clnt *
679rpc_clone_client_set_auth(struct rpc_clnt *clnt, rpc_authflavor_t flavor)
680{
681 struct rpc_create_args args = {
682 .program = clnt->cl_program,
683 .prognumber = clnt->cl_prog,
684 .version = clnt->cl_vers,
685 .authflavor = flavor,
Trond Myklebust79caa5f2019-04-24 17:46:42 -0400686 .cred = clnt->cl_cred,
Chuck Leverba9b5842012-09-14 17:24:02 -0400687 };
688 return __rpc_clone_client(&args, clnt);
689}
690EXPORT_SYMBOL_GPL(rpc_clone_client_set_auth);
691
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400692/**
693 * rpc_switch_client_transport: switch the RPC transport on the fly
694 * @clnt: pointer to a struct rpc_clnt
695 * @args: pointer to the new transport arguments
696 * @timeout: pointer to the new timeout parameters
697 *
698 * This function allows the caller to switch the RPC transport for the
699 * rpc_clnt structure 'clnt' to allow it to connect to a mirrored NFS
700 * server, for instance. It assumes that the caller has ensured that
701 * there are no active RPC tasks by using some form of locking.
702 *
703 * Returns zero if "clnt" is now using the new xprt. Otherwise a
704 * negative errno is returned, and "clnt" continues to use the old
705 * xprt.
706 */
707int rpc_switch_client_transport(struct rpc_clnt *clnt,
708 struct xprt_create *args,
709 const struct rpc_timeout *timeout)
710{
711 const struct rpc_timeout *old_timeo;
712 rpc_authflavor_t pseudoflavor;
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500713 struct rpc_xprt_switch *xps, *oldxps;
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400714 struct rpc_xprt *xprt, *old;
715 struct rpc_clnt *parent;
716 int err;
717
718 xprt = xprt_create_transport(args);
Chuck Lever42aad0d2020-05-12 17:13:39 -0400719 if (IS_ERR(xprt))
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400720 return PTR_ERR(xprt);
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400721
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500722 xps = xprt_switch_alloc(xprt, GFP_KERNEL);
723 if (xps == NULL) {
724 xprt_put(xprt);
725 return -ENOMEM;
726 }
727
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400728 pseudoflavor = clnt->cl_auth->au_flavor;
729
730 old_timeo = clnt->cl_timeout;
731 old = rpc_clnt_set_transport(clnt, xprt, timeout);
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500732 oldxps = xprt_iter_xchg_switch(&clnt->cl_xpi, xps);
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400733
734 rpc_unregister_client(clnt);
735 __rpc_clnt_remove_pipedir(clnt);
Jeff Laytonb4b9d2c2014-11-26 14:44:43 -0500736 rpc_clnt_debugfs_unregister(clnt);
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400737
738 /*
739 * A new transport was created. "clnt" therefore
740 * becomes the root of a new cl_parent tree. clnt's
741 * children, if it has any, still point to the old xprt.
742 */
743 parent = clnt->cl_parent;
744 clnt->cl_parent = clnt;
745
746 /*
747 * The old rpc_auth cache cannot be re-used. GSS
748 * contexts in particular are between a single
749 * client and server.
750 */
751 err = rpc_client_register(clnt, pseudoflavor, NULL);
752 if (err)
753 goto out_revert;
754
755 synchronize_rcu();
756 if (parent != clnt)
757 rpc_release_client(parent);
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500758 xprt_switch_put(oldxps);
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400759 xprt_put(old);
Chuck Lever42aad0d2020-05-12 17:13:39 -0400760 trace_rpc_clnt_replace_xprt(clnt);
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400761 return 0;
762
763out_revert:
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500764 xps = xprt_iter_xchg_switch(&clnt->cl_xpi, oldxps);
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400765 rpc_clnt_set_transport(clnt, old, old_timeo);
766 clnt->cl_parent = parent;
767 rpc_client_register(clnt, pseudoflavor, NULL);
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500768 xprt_switch_put(xps);
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400769 xprt_put(xprt);
Chuck Lever42aad0d2020-05-12 17:13:39 -0400770 trace_rpc_clnt_replace_xprt_err(clnt);
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400771 return err;
772}
773EXPORT_SYMBOL_GPL(rpc_switch_client_transport);
774
Trond Myklebust32278862016-01-30 20:39:19 -0500775static
776int rpc_clnt_xprt_iter_init(struct rpc_clnt *clnt, struct rpc_xprt_iter *xpi)
777{
778 struct rpc_xprt_switch *xps;
779
780 rcu_read_lock();
781 xps = xprt_switch_get(rcu_dereference(clnt->cl_xpi.xpi_xpswitch));
782 rcu_read_unlock();
783 if (xps == NULL)
784 return -EAGAIN;
785 xprt_iter_init_listall(xpi, xps);
786 xprt_switch_put(xps);
787 return 0;
788}
789
790/**
791 * rpc_clnt_iterate_for_each_xprt - Apply a function to all transports
792 * @clnt: pointer to client
793 * @fn: function to apply
794 * @data: void pointer to function data
795 *
796 * Iterates through the list of RPC transports currently attached to the
797 * client and applies the function fn(clnt, xprt, data).
798 *
799 * On error, the iteration stops, and the function returns the error value.
800 */
801int rpc_clnt_iterate_for_each_xprt(struct rpc_clnt *clnt,
802 int (*fn)(struct rpc_clnt *, struct rpc_xprt *, void *),
803 void *data)
804{
805 struct rpc_xprt_iter xpi;
806 int ret;
807
808 ret = rpc_clnt_xprt_iter_init(clnt, &xpi);
809 if (ret)
810 return ret;
811 for (;;) {
812 struct rpc_xprt *xprt = xprt_iter_get_next(&xpi);
813
814 if (!xprt)
815 break;
816 ret = fn(clnt, xprt, data);
817 xprt_put(xprt);
818 if (ret < 0)
819 break;
820 }
821 xprt_iter_destroy(&xpi);
822 return ret;
823}
824EXPORT_SYMBOL_GPL(rpc_clnt_iterate_for_each_xprt);
825
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826/*
Trond Myklebust58f96122010-07-31 14:29:08 -0400827 * Kill all tasks for the given client.
828 * XXX: kill their descendants as well?
829 */
830void rpc_killall_tasks(struct rpc_clnt *clnt)
831{
832 struct rpc_task *rovr;
833
834
835 if (list_empty(&clnt->cl_tasks))
836 return;
Chuck Lever42aad0d2020-05-12 17:13:39 -0400837
Trond Myklebust58f96122010-07-31 14:29:08 -0400838 /*
839 * Spin lock all_tasks to prevent changes...
840 */
Chuck Lever42aad0d2020-05-12 17:13:39 -0400841 trace_rpc_clnt_killall(clnt);
Trond Myklebust58f96122010-07-31 14:29:08 -0400842 spin_lock(&clnt->cl_lock);
Trond Myklebustae67bd32019-04-07 13:58:44 -0400843 list_for_each_entry(rovr, &clnt->cl_tasks, tk_task)
844 rpc_signal_task(rovr);
Trond Myklebust58f96122010-07-31 14:29:08 -0400845 spin_unlock(&clnt->cl_lock);
846}
847EXPORT_SYMBOL_GPL(rpc_killall_tasks);
848
849/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 * Properly shut down an RPC client, terminating all outstanding
Trond Myklebust90c57552007-06-09 19:49:36 -0400851 * requests.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 */
Trond Myklebust4c402b42007-06-14 16:40:32 -0400853void rpc_shutdown_client(struct rpc_clnt *clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854{
Weston Andros Adamson168e4b32012-10-30 17:01:40 -0400855 might_sleep();
856
Chuck Lever42aad0d2020-05-12 17:13:39 -0400857 trace_rpc_clnt_shutdown(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
Trond Myklebust34f52e32007-06-14 16:40:31 -0400859 while (!list_empty(&clnt->cl_tasks)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 rpc_killall_tasks(clnt);
Ingo Molnar532347e2006-01-09 20:52:53 -0800861 wait_event_timeout(destroy_wait,
Trond Myklebust34f52e32007-06-14 16:40:31 -0400862 list_empty(&clnt->cl_tasks), 1*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 }
864
Trond Myklebust4c402b42007-06-14 16:40:32 -0400865 rpc_release_client(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400867EXPORT_SYMBOL_GPL(rpc_shutdown_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
869/*
Trond Myklebust34f52e32007-06-14 16:40:31 -0400870 * Free an RPC client
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 */
NeilBrown7c4310f2020-04-03 14:33:41 +1100872static void rpc_free_client_work(struct work_struct *work)
873{
874 struct rpc_clnt *clnt = container_of(work, struct rpc_clnt, cl_work);
875
Chuck Lever42aad0d2020-05-12 17:13:39 -0400876 trace_rpc_clnt_free(clnt);
877
NeilBrown7c4310f2020-04-03 14:33:41 +1100878 /* These might block on processes that might allocate memory,
879 * so they cannot be called in rpciod, so they are handled separately
880 * here.
881 */
882 rpc_clnt_debugfs_unregister(clnt);
J. Bruce Fields933496e2020-05-13 18:38:40 -0400883 rpc_free_clid(clnt);
NeilBrown7c4310f2020-04-03 14:33:41 +1100884 rpc_clnt_remove_pipedir(clnt);
NeilBrown31e9a7f2020-05-09 07:25:11 +1000885 xprt_put(rcu_dereference_raw(clnt->cl_xprt));
NeilBrown7c4310f2020-04-03 14:33:41 +1100886
887 kfree(clnt);
888 rpciod_down();
889}
Trond Myklebustd07ba842013-11-12 17:24:36 -0500890static struct rpc_clnt *
Trond Myklebust006abe82010-09-12 19:55:25 -0400891rpc_free_client(struct rpc_clnt *clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892{
Trond Myklebustd07ba842013-11-12 17:24:36 -0500893 struct rpc_clnt *parent = NULL;
894
Chuck Lever42aad0d2020-05-12 17:13:39 -0400895 trace_rpc_clnt_release(clnt);
Trond Myklebust6eac7d32012-01-20 13:53:37 -0500896 if (clnt->cl_parent != clnt)
Trond Myklebustd07ba842013-11-12 17:24:36 -0500897 parent = clnt->cl_parent;
Stanislav Kinsburskyadb6fa72013-06-26 10:15:14 +0400898 rpc_unregister_client(clnt);
Chuck Lever11c556b2006-03-20 13:44:22 -0500899 rpc_free_iostats(clnt->cl_metrics);
900 clnt->cl_metrics = NULL;
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500901 xprt_iter_destroy(&clnt->cl_xpi);
Trond Myklebust79caa5f2019-04-24 17:46:42 -0400902 put_cred(clnt->cl_cred);
NeilBrown7c4310f2020-04-03 14:33:41 +1100903
904 INIT_WORK(&clnt->cl_work, rpc_free_client_work);
905 schedule_work(&clnt->cl_work);
Trond Myklebustd07ba842013-11-12 17:24:36 -0500906 return parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907}
908
909/*
Trond Myklebust1dd17ec2007-06-26 16:57:41 -0400910 * Free an RPC client
911 */
Stephen Hemminger8fdee4c2018-07-24 12:29:15 -0700912static struct rpc_clnt *
Trond Myklebust006abe82010-09-12 19:55:25 -0400913rpc_free_auth(struct rpc_clnt *clnt)
Trond Myklebust1dd17ec2007-06-26 16:57:41 -0400914{
Trond Myklebustd07ba842013-11-12 17:24:36 -0500915 if (clnt->cl_auth == NULL)
916 return rpc_free_client(clnt);
Trond Myklebust1dd17ec2007-06-26 16:57:41 -0400917
918 /*
919 * Note: RPCSEC_GSS may need to send NULL RPC calls in order to
920 * release remaining GSS contexts. This mechanism ensures
921 * that it can do so safely.
922 */
Trond Myklebust006abe82010-09-12 19:55:25 -0400923 atomic_inc(&clnt->cl_count);
Trond Myklebust1dd17ec2007-06-26 16:57:41 -0400924 rpcauth_release(clnt->cl_auth);
925 clnt->cl_auth = NULL;
Trond Myklebust006abe82010-09-12 19:55:25 -0400926 if (atomic_dec_and_test(&clnt->cl_count))
Trond Myklebustd07ba842013-11-12 17:24:36 -0500927 return rpc_free_client(clnt);
928 return NULL;
Trond Myklebust1dd17ec2007-06-26 16:57:41 -0400929}
930
931/*
Trond Myklebust34f52e32007-06-14 16:40:31 -0400932 * Release reference to the RPC client
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 */
934void
935rpc_release_client(struct rpc_clnt *clnt)
936{
Trond Myklebustd07ba842013-11-12 17:24:36 -0500937 do {
938 if (list_empty(&clnt->cl_tasks))
939 wake_up(&destroy_wait);
940 if (!atomic_dec_and_test(&clnt->cl_count))
941 break;
942 clnt = rpc_free_auth(clnt);
943 } while (clnt != NULL);
Trond Myklebust34f52e32007-06-14 16:40:31 -0400944}
Simo Sorce1d658332012-05-25 18:09:55 -0400945EXPORT_SYMBOL_GPL(rpc_release_client);
Trond Myklebust34f52e32007-06-14 16:40:31 -0400946
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000947/**
948 * rpc_bind_new_program - bind a new RPC program to an existing client
Randy Dunlap65b6e422008-02-13 15:03:23 -0800949 * @old: old rpc_client
950 * @program: rpc program to set
951 * @vers: rpc program version
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000952 *
953 * Clones the rpc client and sets up a new RPC program. This is mainly
954 * of use for enabling different RPC programs to share the same transport.
955 * The Sun NFSv2/v3 ACL protocol can do this.
956 */
957struct rpc_clnt *rpc_bind_new_program(struct rpc_clnt *old,
Trond Myklebusta613fa12012-01-20 13:53:56 -0500958 const struct rpc_program *program,
Chuck Lever89eb21c2007-09-11 18:00:09 -0400959 u32 vers)
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000960{
Trond Myklebustf994c432012-11-01 12:14:14 -0400961 struct rpc_create_args args = {
962 .program = program,
963 .prognumber = program->number,
964 .version = vers,
965 .authflavor = old->cl_auth->au_flavor,
Trond Myklebust79caa5f2019-04-24 17:46:42 -0400966 .cred = old->cl_cred,
Trond Myklebustf994c432012-11-01 12:14:14 -0400967 };
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000968 struct rpc_clnt *clnt;
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000969 int err;
970
Trond Myklebustf994c432012-11-01 12:14:14 -0400971 clnt = __rpc_clone_client(&args, old);
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000972 if (IS_ERR(clnt))
973 goto out;
Chuck Levercaabea82009-12-03 15:58:56 -0500974 err = rpc_ping(clnt);
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000975 if (err != 0) {
976 rpc_shutdown_client(clnt);
977 clnt = ERR_PTR(err);
978 }
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800979out:
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000980 return clnt;
981}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400982EXPORT_SYMBOL_GPL(rpc_bind_new_program);
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000983
Trond Myklebusta101b042019-07-11 16:33:12 -0400984struct rpc_xprt *
985rpc_task_get_xprt(struct rpc_clnt *clnt, struct rpc_xprt *xprt)
Trond Myklebust21f0ffa2017-04-28 10:52:42 -0400986{
987 struct rpc_xprt_switch *xps;
Trond Myklebust21f0ffa2017-04-28 10:52:42 -0400988
989 if (!xprt)
990 return NULL;
991 rcu_read_lock();
992 xps = rcu_dereference(clnt->cl_xpi.xpi_xpswitch);
993 atomic_long_inc(&xps->xps_queuelen);
994 rcu_read_unlock();
995 atomic_long_inc(&xprt->queuelen);
996
997 return xprt;
998}
999
1000static void
1001rpc_task_release_xprt(struct rpc_clnt *clnt, struct rpc_xprt *xprt)
1002{
1003 struct rpc_xprt_switch *xps;
1004
1005 atomic_long_dec(&xprt->queuelen);
1006 rcu_read_lock();
1007 xps = rcu_dereference(clnt->cl_xpi.xpi_xpswitch);
1008 atomic_long_dec(&xps->xps_queuelen);
1009 rcu_read_unlock();
1010
1011 xprt_put(xprt);
1012}
1013
Bill Baker0f90be12018-06-19 16:24:58 -05001014void rpc_task_release_transport(struct rpc_task *task)
1015{
1016 struct rpc_xprt *xprt = task->tk_xprt;
1017
1018 if (xprt) {
1019 task->tk_xprt = NULL;
Trond Myklebust21f0ffa2017-04-28 10:52:42 -04001020 if (task->tk_client)
1021 rpc_task_release_xprt(task->tk_client, xprt);
1022 else
1023 xprt_put(xprt);
Bill Baker0f90be12018-06-19 16:24:58 -05001024 }
1025}
1026EXPORT_SYMBOL_GPL(rpc_task_release_transport);
1027
Trond Myklebust58f96122010-07-31 14:29:08 -04001028void rpc_task_release_client(struct rpc_task *task)
1029{
1030 struct rpc_clnt *clnt = task->tk_client;
1031
Trond Myklebust21f0ffa2017-04-28 10:52:42 -04001032 rpc_task_release_transport(task);
Trond Myklebust58f96122010-07-31 14:29:08 -04001033 if (clnt != NULL) {
1034 /* Remove from client task list */
1035 spin_lock(&clnt->cl_lock);
1036 list_del(&task->tk_task);
1037 spin_unlock(&clnt->cl_lock);
1038 task->tk_client = NULL;
1039
1040 rpc_release_client(clnt);
1041 }
Bill Baker0f90be12018-06-19 16:24:58 -05001042}
Trond Myklebustfb43d172016-01-30 16:39:26 -05001043
Trond Myklebusta101b042019-07-11 16:33:12 -04001044static struct rpc_xprt *
1045rpc_task_get_first_xprt(struct rpc_clnt *clnt)
1046{
1047 struct rpc_xprt *xprt;
1048
1049 rcu_read_lock();
1050 xprt = xprt_get(rcu_dereference(clnt->cl_xprt));
1051 rcu_read_unlock();
1052 return rpc_task_get_xprt(clnt, xprt);
1053}
1054
1055static struct rpc_xprt *
1056rpc_task_get_next_xprt(struct rpc_clnt *clnt)
1057{
1058 return rpc_task_get_xprt(clnt, xprt_iter_get_next(&clnt->cl_xpi));
1059}
1060
Bill Baker0f90be12018-06-19 16:24:58 -05001061static
1062void rpc_task_set_transport(struct rpc_task *task, struct rpc_clnt *clnt)
1063{
NeilBrown5a0c2572019-05-30 10:41:28 +10001064 if (task->tk_xprt)
1065 return;
1066 if (task->tk_flags & RPC_TASK_NO_ROUND_ROBIN)
1067 task->tk_xprt = rpc_task_get_first_xprt(clnt);
1068 else
Trond Myklebusta101b042019-07-11 16:33:12 -04001069 task->tk_xprt = rpc_task_get_next_xprt(clnt);
Trond Myklebust58f96122010-07-31 14:29:08 -04001070}
1071
1072static
1073void rpc_task_set_client(struct rpc_task *task, struct rpc_clnt *clnt)
1074{
Trond Myklebustfb43d172016-01-30 16:39:26 -05001075
Trond Myklebust58f96122010-07-31 14:29:08 -04001076 if (clnt != NULL) {
Bill Baker0f90be12018-06-19 16:24:58 -05001077 rpc_task_set_transport(task, clnt);
Trond Myklebust58f96122010-07-31 14:29:08 -04001078 task->tk_client = clnt;
Trond Myklebust006abe82010-09-12 19:55:25 -04001079 atomic_inc(&clnt->cl_count);
Trond Myklebust58f96122010-07-31 14:29:08 -04001080 if (clnt->cl_softrtry)
1081 task->tk_flags |= RPC_TASK_SOFT;
Trond Myklebustae6ec912019-04-07 13:58:58 -04001082 if (clnt->cl_softerr)
1083 task->tk_flags |= RPC_TASK_TIMEOUT;
Trond Myklebust8a19a0b2013-09-24 12:00:27 -04001084 if (clnt->cl_noretranstimeo)
1085 task->tk_flags |= RPC_TASK_NO_RETRANS_TIMEOUT;
Jeff Layton3c87ef62015-06-03 16:14:25 -04001086 if (atomic_read(&clnt->cl_swapper))
1087 task->tk_flags |= RPC_TASK_SWAPPER;
Trond Myklebust58f96122010-07-31 14:29:08 -04001088 /* Add to the client's list of all tasks */
1089 spin_lock(&clnt->cl_lock);
1090 list_add_tail(&task->tk_task, &clnt->cl_tasks);
1091 spin_unlock(&clnt->cl_lock);
1092 }
1093}
1094
1095static void
1096rpc_task_set_rpc_message(struct rpc_task *task, const struct rpc_message *msg)
1097{
1098 if (msg != NULL) {
1099 task->tk_msg.rpc_proc = msg->rpc_proc;
1100 task->tk_msg.rpc_argp = msg->rpc_argp;
1101 task->tk_msg.rpc_resp = msg->rpc_resp;
Trond Myklebust7eac5262020-02-07 19:11:12 -05001102 task->tk_msg.rpc_cred = msg->rpc_cred;
1103 if (!(task->tk_flags & RPC_TASK_CRED_NOREF))
1104 get_cred(task->tk_msg.rpc_cred);
Trond Myklebust58f96122010-07-31 14:29:08 -04001105 }
1106}
1107
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108/*
1109 * Default callback for async RPC calls
1110 */
1111static void
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001112rpc_default_callback(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113{
1114}
1115
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001116static const struct rpc_call_ops rpc_default_ops = {
1117 .rpc_call_done = rpc_default_callback,
1118};
1119
Trond Myklebustc970aa82007-07-14 15:39:59 -04001120/**
1121 * rpc_run_task - Allocate a new RPC task, then run rpc_execute against it
1122 * @task_setup_data: pointer to task initialisation data
1123 */
1124struct rpc_task *rpc_run_task(const struct rpc_task_setup *task_setup_data)
Trond Myklebust6e5b70e2007-06-12 10:02:37 -04001125{
Trond Myklebust19445b92010-04-16 16:41:10 -04001126 struct rpc_task *task;
Trond Myklebust6e5b70e2007-06-12 10:02:37 -04001127
Trond Myklebust84115e12007-07-14 15:39:59 -04001128 task = rpc_new_task(task_setup_data);
Trond Myklebust6e5b70e2007-06-12 10:02:37 -04001129
Trond Myklebust263fb9c2020-02-07 19:16:34 -05001130 if (!RPC_IS_ASYNC(task))
1131 task->tk_flags |= RPC_TASK_CRED_NOREF;
1132
Trond Myklebust58f96122010-07-31 14:29:08 -04001133 rpc_task_set_client(task, task_setup_data->rpc_client);
1134 rpc_task_set_rpc_message(task, task_setup_data->rpc_message);
1135
Trond Myklebust58f96122010-07-31 14:29:08 -04001136 if (task->tk_action == NULL)
1137 rpc_call_start(task);
1138
Trond Myklebust6e5b70e2007-06-12 10:02:37 -04001139 atomic_inc(&task->tk_count);
1140 rpc_execute(task);
Trond Myklebust19445b92010-04-16 16:41:10 -04001141 return task;
Trond Myklebust6e5b70e2007-06-12 10:02:37 -04001142}
Trond Myklebustc970aa82007-07-14 15:39:59 -04001143EXPORT_SYMBOL_GPL(rpc_run_task);
Trond Myklebust6e5b70e2007-06-12 10:02:37 -04001144
1145/**
1146 * rpc_call_sync - Perform a synchronous RPC call
1147 * @clnt: pointer to RPC client
1148 * @msg: RPC call parameters
1149 * @flags: RPC call flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 */
Trond Myklebustcbc20052008-02-14 11:11:30 -05001151int rpc_call_sync(struct rpc_clnt *clnt, const struct rpc_message *msg, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152{
1153 struct rpc_task *task;
Trond Myklebust84115e12007-07-14 15:39:59 -04001154 struct rpc_task_setup task_setup_data = {
1155 .rpc_client = clnt,
1156 .rpc_message = msg,
1157 .callback_ops = &rpc_default_ops,
1158 .flags = flags,
1159 };
Trond Myklebust6e5b70e2007-06-12 10:02:37 -04001160 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
Weston Andros Adamson50d2bdb2012-11-01 16:04:40 -04001162 WARN_ON_ONCE(flags & RPC_TASK_ASYNC);
1163 if (flags & RPC_TASK_ASYNC) {
1164 rpc_release_calldata(task_setup_data.callback_ops,
1165 task_setup_data.callback_data);
1166 return -EINVAL;
1167 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
Trond Myklebustc970aa82007-07-14 15:39:59 -04001169 task = rpc_run_task(&task_setup_data);
Trond Myklebust6e5b70e2007-06-12 10:02:37 -04001170 if (IS_ERR(task))
1171 return PTR_ERR(task);
Trond Myklebuste60859a2006-01-03 09:55:10 +01001172 status = task->tk_status;
Trond Myklebustbde8f002007-01-24 11:54:53 -08001173 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 return status;
1175}
Trond Myklebuste8914c62007-07-14 15:39:59 -04001176EXPORT_SYMBOL_GPL(rpc_call_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
Trond Myklebust6e5b70e2007-06-12 10:02:37 -04001178/**
1179 * rpc_call_async - Perform an asynchronous RPC call
1180 * @clnt: pointer to RPC client
1181 * @msg: RPC call parameters
1182 * @flags: RPC call flags
Randy Dunlap65b6e422008-02-13 15:03:23 -08001183 * @tk_ops: RPC call ops
Trond Myklebust6e5b70e2007-06-12 10:02:37 -04001184 * @data: user call data
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 */
1186int
Trond Myklebustcbc20052008-02-14 11:11:30 -05001187rpc_call_async(struct rpc_clnt *clnt, const struct rpc_message *msg, int flags,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001188 const struct rpc_call_ops *tk_ops, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189{
1190 struct rpc_task *task;
Trond Myklebust84115e12007-07-14 15:39:59 -04001191 struct rpc_task_setup task_setup_data = {
1192 .rpc_client = clnt,
1193 .rpc_message = msg,
1194 .callback_ops = tk_ops,
1195 .callback_data = data,
1196 .flags = flags|RPC_TASK_ASYNC,
1197 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
Trond Myklebustc970aa82007-07-14 15:39:59 -04001199 task = rpc_run_task(&task_setup_data);
Trond Myklebust6e5b70e2007-06-12 10:02:37 -04001200 if (IS_ERR(task))
1201 return PTR_ERR(task);
1202 rpc_put_task(task);
1203 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204}
Trond Myklebuste8914c62007-07-14 15:39:59 -04001205EXPORT_SYMBOL_GPL(rpc_call_async);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001207#if defined(CONFIG_SUNRPC_BACKCHANNEL)
Trond Myklebust477687e2019-03-05 07:30:48 -05001208static void call_bc_encode(struct rpc_task *task);
1209
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001210/**
1211 * rpc_run_bc_task - Allocate a new RPC task for backchannel use, then run
1212 * rpc_execute against it
Jaswinder Singh Rajput7a73fdd2009-09-24 14:58:42 -04001213 * @req: RPC request
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001214 */
Trond Myklebust0f419792015-06-01 22:59:08 -04001215struct rpc_task *rpc_run_bc_task(struct rpc_rqst *req)
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001216{
1217 struct rpc_task *task;
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001218 struct rpc_task_setup task_setup_data = {
Trond Myklebust0f419792015-06-01 22:59:08 -04001219 .callback_ops = &rpc_default_ops,
Trond Myklebust762e4e62018-08-24 16:28:28 -04001220 .flags = RPC_TASK_SOFTCONN |
1221 RPC_TASK_NO_RETRANS_TIMEOUT,
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001222 };
1223
1224 dprintk("RPC: rpc_run_bc_task req= %p\n", req);
1225 /*
1226 * Create an rpc_task to send the data
1227 */
1228 task = rpc_new_task(&task_setup_data);
Trond Myklebust902c5882018-09-01 17:21:01 -04001229 xprt_init_bc_request(req, task);
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001230
Trond Myklebust477687e2019-03-05 07:30:48 -05001231 task->tk_action = call_bc_encode;
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001232 atomic_inc(&task->tk_count);
Weston Andros Adamson9a6478f2012-10-23 10:43:27 -04001233 WARN_ON_ONCE(atomic_read(&task->tk_count) != 2);
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001234 rpc_execute(task);
1235
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001236 dprintk("RPC: rpc_run_bc_task: task= %p\n", task);
1237 return task;
1238}
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001239#endif /* CONFIG_SUNRPC_BACKCHANNEL */
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001240
Chuck Levercf500ba2019-02-11 11:25:20 -05001241/**
1242 * rpc_prepare_reply_pages - Prepare to receive a reply data payload into pages
1243 * @req: RPC request to prepare
1244 * @pages: vector of struct page pointers
1245 * @base: offset in first page where receive should start, in bytes
1246 * @len: expected size of the upper layer data payload, in bytes
1247 * @hdrsize: expected size of upper layer reply header, in XDR words
1248 *
1249 */
1250void rpc_prepare_reply_pages(struct rpc_rqst *req, struct page **pages,
1251 unsigned int base, unsigned int len,
1252 unsigned int hdrsize)
1253{
Trond Myklebust9ed5af262020-11-21 20:46:18 -05001254 hdrsize += RPC_REPHDRSIZE + req->rq_cred->cr_auth->au_ralign;
Chuck Lever02ef04e2019-02-11 11:25:25 -05001255
Chuck Levercf500ba2019-02-11 11:25:20 -05001256 xdr_inline_pages(&req->rq_rcv_buf, hdrsize << 2, pages, base, len);
Chuck Leverc509f15a2020-05-12 17:13:28 -04001257 trace_rpc_xdr_reply_pages(req->rq_task, &req->rq_rcv_buf);
Chuck Levercf500ba2019-02-11 11:25:20 -05001258}
1259EXPORT_SYMBOL_GPL(rpc_prepare_reply_pages);
1260
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261void
Trond Myklebust77de2c52007-10-25 18:40:21 -04001262rpc_call_start(struct rpc_task *task)
1263{
1264 task->tk_action = call_start;
1265}
1266EXPORT_SYMBOL_GPL(rpc_call_start);
1267
Chuck Levered394402006-08-22 20:06:17 -04001268/**
1269 * rpc_peeraddr - extract remote peer address from clnt's xprt
1270 * @clnt: RPC client structure
1271 * @buf: target buffer
Randy Dunlap65b6e422008-02-13 15:03:23 -08001272 * @bufsize: length of target buffer
Chuck Levered394402006-08-22 20:06:17 -04001273 *
1274 * Returns the number of bytes that are actually in the stored address.
1275 */
1276size_t rpc_peeraddr(struct rpc_clnt *clnt, struct sockaddr *buf, size_t bufsize)
1277{
1278 size_t bytes;
Trond Myklebust2446ab62012-03-01 17:00:56 -05001279 struct rpc_xprt *xprt;
Chuck Levered394402006-08-22 20:06:17 -04001280
Trond Myklebust2446ab62012-03-01 17:00:56 -05001281 rcu_read_lock();
1282 xprt = rcu_dereference(clnt->cl_xprt);
1283
1284 bytes = xprt->addrlen;
Chuck Levered394402006-08-22 20:06:17 -04001285 if (bytes > bufsize)
1286 bytes = bufsize;
Trond Myklebust2446ab62012-03-01 17:00:56 -05001287 memcpy(buf, &xprt->addr, bytes);
1288 rcu_read_unlock();
1289
1290 return bytes;
Chuck Levered394402006-08-22 20:06:17 -04001291}
Chuck Leverb86acd52006-08-22 20:06:22 -04001292EXPORT_SYMBOL_GPL(rpc_peeraddr);
Chuck Levered394402006-08-22 20:06:17 -04001293
Chuck Leverf425eba2006-08-22 20:06:18 -04001294/**
1295 * rpc_peeraddr2str - return remote peer address in printable format
1296 * @clnt: RPC client structure
1297 * @format: address format
1298 *
Trond Myklebust2446ab62012-03-01 17:00:56 -05001299 * NB: the lifetime of the memory referenced by the returned pointer is
1300 * the same as the rpc_xprt itself. As long as the caller uses this
1301 * pointer, it must hold the RCU read lock.
Chuck Leverf425eba2006-08-22 20:06:18 -04001302 */
Chuck Leverb454ae92008-01-07 18:34:48 -05001303const char *rpc_peeraddr2str(struct rpc_clnt *clnt,
1304 enum rpc_display_format_t format)
Chuck Leverf425eba2006-08-22 20:06:18 -04001305{
Trond Myklebust2446ab62012-03-01 17:00:56 -05001306 struct rpc_xprt *xprt;
1307
1308 xprt = rcu_dereference(clnt->cl_xprt);
Chuck Lever7559c7a2006-12-05 16:35:37 -05001309
1310 if (xprt->address_strings[format] != NULL)
1311 return xprt->address_strings[format];
1312 else
1313 return "unprintable";
Chuck Leverf425eba2006-08-22 20:06:18 -04001314}
Chuck Leverb86acd52006-08-22 20:06:22 -04001315EXPORT_SYMBOL_GPL(rpc_peeraddr2str);
Chuck Leverf425eba2006-08-22 20:06:18 -04001316
Chuck Lever2e738fd2012-03-01 17:01:14 -05001317static const struct sockaddr_in rpc_inaddr_loopback = {
1318 .sin_family = AF_INET,
1319 .sin_addr.s_addr = htonl(INADDR_ANY),
1320};
1321
1322static const struct sockaddr_in6 rpc_in6addr_loopback = {
1323 .sin6_family = AF_INET6,
1324 .sin6_addr = IN6ADDR_ANY_INIT,
1325};
1326
1327/*
1328 * Try a getsockname() on a connected datagram socket. Using a
1329 * connected datagram socket prevents leaving a socket in TIME_WAIT.
1330 * This conserves the ephemeral port number space.
1331 *
1332 * Returns zero and fills in "buf" if successful; otherwise, a
1333 * negative errno is returned.
1334 */
1335static int rpc_sockname(struct net *net, struct sockaddr *sap, size_t salen,
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001336 struct sockaddr *buf)
Chuck Lever2e738fd2012-03-01 17:01:14 -05001337{
1338 struct socket *sock;
1339 int err;
1340
1341 err = __sock_create(net, sap->sa_family,
1342 SOCK_DGRAM, IPPROTO_UDP, &sock, 1);
1343 if (err < 0) {
1344 dprintk("RPC: can't create UDP socket (%d)\n", err);
1345 goto out;
1346 }
1347
1348 switch (sap->sa_family) {
1349 case AF_INET:
1350 err = kernel_bind(sock,
1351 (struct sockaddr *)&rpc_inaddr_loopback,
1352 sizeof(rpc_inaddr_loopback));
1353 break;
1354 case AF_INET6:
1355 err = kernel_bind(sock,
1356 (struct sockaddr *)&rpc_in6addr_loopback,
1357 sizeof(rpc_in6addr_loopback));
1358 break;
1359 default:
1360 err = -EAFNOSUPPORT;
1361 goto out;
1362 }
1363 if (err < 0) {
1364 dprintk("RPC: can't bind UDP socket (%d)\n", err);
1365 goto out_release;
1366 }
1367
1368 err = kernel_connect(sock, sap, salen, 0);
1369 if (err < 0) {
1370 dprintk("RPC: can't connect UDP socket (%d)\n", err);
1371 goto out_release;
1372 }
1373
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001374 err = kernel_getsockname(sock, buf);
Chuck Lever2e738fd2012-03-01 17:01:14 -05001375 if (err < 0) {
1376 dprintk("RPC: getsockname failed (%d)\n", err);
1377 goto out_release;
1378 }
1379
1380 err = 0;
1381 if (buf->sa_family == AF_INET6) {
1382 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)buf;
1383 sin6->sin6_scope_id = 0;
1384 }
1385 dprintk("RPC: %s succeeded\n", __func__);
1386
1387out_release:
1388 sock_release(sock);
1389out:
1390 return err;
1391}
1392
1393/*
1394 * Scraping a connected socket failed, so we don't have a useable
1395 * local address. Fallback: generate an address that will prevent
1396 * the server from calling us back.
1397 *
1398 * Returns zero and fills in "buf" if successful; otherwise, a
1399 * negative errno is returned.
1400 */
1401static int rpc_anyaddr(int family, struct sockaddr *buf, size_t buflen)
1402{
1403 switch (family) {
1404 case AF_INET:
1405 if (buflen < sizeof(rpc_inaddr_loopback))
1406 return -EINVAL;
1407 memcpy(buf, &rpc_inaddr_loopback,
1408 sizeof(rpc_inaddr_loopback));
1409 break;
1410 case AF_INET6:
1411 if (buflen < sizeof(rpc_in6addr_loopback))
1412 return -EINVAL;
1413 memcpy(buf, &rpc_in6addr_loopback,
1414 sizeof(rpc_in6addr_loopback));
Trond Myklebust0b161e62015-12-30 18:14:06 -05001415 break;
Chuck Lever2e738fd2012-03-01 17:01:14 -05001416 default:
1417 dprintk("RPC: %s: address family not supported\n",
1418 __func__);
1419 return -EAFNOSUPPORT;
1420 }
1421 dprintk("RPC: %s: succeeded\n", __func__);
1422 return 0;
1423}
1424
1425/**
1426 * rpc_localaddr - discover local endpoint address for an RPC client
1427 * @clnt: RPC client structure
1428 * @buf: target buffer
1429 * @buflen: size of target buffer, in bytes
1430 *
1431 * Returns zero and fills in "buf" and "buflen" if successful;
1432 * otherwise, a negative errno is returned.
1433 *
1434 * This works even if the underlying transport is not currently connected,
1435 * or if the upper layer never previously provided a source address.
1436 *
1437 * The result of this function call is transient: multiple calls in
1438 * succession may give different results, depending on how local
1439 * networking configuration changes over time.
1440 */
1441int rpc_localaddr(struct rpc_clnt *clnt, struct sockaddr *buf, size_t buflen)
1442{
1443 struct sockaddr_storage address;
1444 struct sockaddr *sap = (struct sockaddr *)&address;
1445 struct rpc_xprt *xprt;
1446 struct net *net;
1447 size_t salen;
1448 int err;
1449
1450 rcu_read_lock();
1451 xprt = rcu_dereference(clnt->cl_xprt);
1452 salen = xprt->addrlen;
1453 memcpy(sap, &xprt->addr, salen);
1454 net = get_net(xprt->xprt_net);
1455 rcu_read_unlock();
1456
1457 rpc_set_port(sap, 0);
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001458 err = rpc_sockname(net, sap, salen, buf);
Chuck Lever2e738fd2012-03-01 17:01:14 -05001459 put_net(net);
1460 if (err != 0)
1461 /* Couldn't discover local address, return ANYADDR */
1462 return rpc_anyaddr(sap->sa_family, buf, buflen);
1463 return 0;
1464}
1465EXPORT_SYMBOL_GPL(rpc_localaddr);
1466
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467void
1468rpc_setbufsize(struct rpc_clnt *clnt, unsigned int sndsize, unsigned int rcvsize)
1469{
Trond Myklebust2446ab62012-03-01 17:00:56 -05001470 struct rpc_xprt *xprt;
1471
1472 rcu_read_lock();
1473 xprt = rcu_dereference(clnt->cl_xprt);
Chuck Lever470056c2005-08-25 16:25:56 -07001474 if (xprt->ops->set_buffer_size)
1475 xprt->ops->set_buffer_size(xprt, sndsize, rcvsize);
Trond Myklebust2446ab62012-03-01 17:00:56 -05001476 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477}
Trond Myklebuste8914c62007-07-14 15:39:59 -04001478EXPORT_SYMBOL_GPL(rpc_setbufsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
Trond Myklebust2446ab62012-03-01 17:00:56 -05001480/**
Trond Myklebust2446ab62012-03-01 17:00:56 -05001481 * rpc_net_ns - Get the network namespace for this RPC client
1482 * @clnt: RPC client to query
1483 *
1484 */
1485struct net *rpc_net_ns(struct rpc_clnt *clnt)
1486{
1487 struct net *ret;
1488
1489 rcu_read_lock();
1490 ret = rcu_dereference(clnt->cl_xprt)->xprt_net;
1491 rcu_read_unlock();
1492 return ret;
1493}
1494EXPORT_SYMBOL_GPL(rpc_net_ns);
1495
1496/**
1497 * rpc_max_payload - Get maximum payload size for a transport, in bytes
1498 * @clnt: RPC client to query
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 *
1500 * For stream transports, this is one RPC record fragment (see RFC
1501 * 1831), as we don't support multi-record requests yet. For datagram
1502 * transports, this is the size of an IP packet minus the IP, UDP, and
1503 * RPC header sizes.
1504 */
1505size_t rpc_max_payload(struct rpc_clnt *clnt)
1506{
Trond Myklebust2446ab62012-03-01 17:00:56 -05001507 size_t ret;
1508
1509 rcu_read_lock();
1510 ret = rcu_dereference(clnt->cl_xprt)->max_payload;
1511 rcu_read_unlock();
1512 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513}
Chuck Leverb86acd52006-08-22 20:06:22 -04001514EXPORT_SYMBOL_GPL(rpc_max_payload);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515
Chuck Lever35f5a422006-01-03 09:55:50 +01001516/**
Chuck Lever6b26cc82016-05-02 14:40:40 -04001517 * rpc_max_bc_payload - Get maximum backchannel payload size, in bytes
1518 * @clnt: RPC client to query
1519 */
1520size_t rpc_max_bc_payload(struct rpc_clnt *clnt)
1521{
1522 struct rpc_xprt *xprt;
1523 size_t ret;
1524
1525 rcu_read_lock();
1526 xprt = rcu_dereference(clnt->cl_xprt);
1527 ret = xprt->ops->bc_maxpayload(xprt);
1528 rcu_read_unlock();
1529 return ret;
1530}
1531EXPORT_SYMBOL_GPL(rpc_max_bc_payload);
1532
Trond Myklebust7402a4f2019-07-16 13:51:29 -04001533unsigned int rpc_num_bc_slots(struct rpc_clnt *clnt)
1534{
1535 struct rpc_xprt *xprt;
1536 unsigned int ret;
1537
1538 rcu_read_lock();
1539 xprt = rcu_dereference(clnt->cl_xprt);
1540 ret = xprt->ops->bc_num_slots(xprt);
1541 rcu_read_unlock();
1542 return ret;
1543}
1544EXPORT_SYMBOL_GPL(rpc_num_bc_slots);
1545
Chuck Lever6b26cc82016-05-02 14:40:40 -04001546/**
Chuck Lever35f5a422006-01-03 09:55:50 +01001547 * rpc_force_rebind - force transport to check that remote port is unchanged
1548 * @clnt: client to rebind
1549 *
1550 */
1551void rpc_force_rebind(struct rpc_clnt *clnt)
1552{
Trond Myklebust2446ab62012-03-01 17:00:56 -05001553 if (clnt->cl_autobind) {
1554 rcu_read_lock();
1555 xprt_clear_bound(rcu_dereference(clnt->cl_xprt));
1556 rcu_read_unlock();
1557 }
Chuck Lever35f5a422006-01-03 09:55:50 +01001558}
Chuck Leverb86acd52006-08-22 20:06:22 -04001559EXPORT_SYMBOL_GPL(rpc_force_rebind);
Chuck Lever35f5a422006-01-03 09:55:50 +01001560
Trond Myklebust9e6fa0b2019-04-07 13:58:45 -04001561static int
1562__rpc_restart_call(struct rpc_task *task, void (*action)(struct rpc_task *))
Andy Adamsonaae20062009-04-01 09:22:40 -04001563{
Trond Myklebust494314c2014-03-20 12:59:09 -04001564 task->tk_status = 0;
Trond Myklebust5ad64b32019-04-07 13:58:54 -04001565 task->tk_rpc_status = 0;
Trond Myklebust9e6fa0b2019-04-07 13:58:45 -04001566 task->tk_action = action;
Trond Myklebustf1f88fc2010-07-31 14:29:07 -04001567 return 1;
Andy Adamsonaae20062009-04-01 09:22:40 -04001568}
Andy Adamsonaae20062009-04-01 09:22:40 -04001569
1570/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 * Restart an (async) RPC call. Usually called from within the
1572 * exit handler.
1573 */
Trond Myklebustf1f88fc2010-07-31 14:29:07 -04001574int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575rpc_restart_call(struct rpc_task *task)
1576{
Trond Myklebust9e6fa0b2019-04-07 13:58:45 -04001577 return __rpc_restart_call(task, call_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578}
Trond Myklebuste8914c62007-07-14 15:39:59 -04001579EXPORT_SYMBOL_GPL(rpc_restart_call);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580
Trond Myklebust9e6fa0b2019-04-07 13:58:45 -04001581/*
1582 * Restart an (async) RPC call from the call_prepare state.
1583 * Usually called from within the exit handler.
1584 */
1585int
1586rpc_restart_call_prepare(struct rpc_task *task)
1587{
1588 if (task->tk_ops->rpc_call_prepare != NULL)
1589 return __rpc_restart_call(task, rpc_prepare_task);
1590 return rpc_restart_call(task);
1591}
1592EXPORT_SYMBOL_GPL(rpc_restart_call_prepare);
1593
Jeff Laytonb4b9d2c2014-11-26 14:44:43 -05001594const char
1595*rpc_proc_name(const struct rpc_task *task)
Chuck Lever3748f1e2008-05-21 17:09:12 -04001596{
1597 const struct rpc_procinfo *proc = task->tk_msg.rpc_proc;
1598
1599 if (proc) {
1600 if (proc->p_name)
1601 return proc->p_name;
1602 else
1603 return "NULL";
1604 } else
1605 return "no proc";
1606}
Chuck Lever3748f1e2008-05-21 17:09:12 -04001607
Trond Myklebust5ad64b32019-04-07 13:58:54 -04001608static void
1609__rpc_call_rpcerror(struct rpc_task *task, int tk_status, int rpc_status)
1610{
Chuck Lever0125ecb2020-05-12 17:13:23 -04001611 trace_rpc_call_rpcerror(task, tk_status, rpc_status);
Trond Myklebust5ad64b32019-04-07 13:58:54 -04001612 task->tk_rpc_status = rpc_status;
1613 rpc_exit(task, tk_status);
1614}
1615
1616static void
1617rpc_call_rpcerror(struct rpc_task *task, int status)
1618{
1619 __rpc_call_rpcerror(task, status, status);
1620}
1621
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622/*
1623 * 0. Initial state
1624 *
1625 * Other FSM states can be visited zero or more times, but
1626 * this state is visited exactly once for each RPC.
1627 */
1628static void
1629call_start(struct rpc_task *task)
1630{
1631 struct rpc_clnt *clnt = task->tk_client;
Christoph Hellwig1c5876d2017-05-08 23:27:10 +02001632 int idx = task->tk_msg.rpc_proc->p_statidx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633
Chuck Leverc435da62017-11-03 13:46:14 -04001634 trace_rpc_request(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635
Christoph Hellwig1c5876d2017-05-08 23:27:10 +02001636 /* Increment call count (version might not be valid for ping) */
1637 if (clnt->cl_program->version[clnt->cl_vers])
1638 clnt->cl_program->version[clnt->cl_vers]->counts[idx]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 clnt->cl_stats->rpccnt++;
1640 task->tk_action = call_reserve;
Bill Baker0f90be12018-06-19 16:24:58 -05001641 rpc_task_set_transport(task, clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642}
1643
1644/*
1645 * 1. Reserve an RPC call slot
1646 */
1647static void
1648call_reserve(struct rpc_task *task)
1649{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 task->tk_status = 0;
1651 task->tk_action = call_reserveresult;
1652 xprt_reserve(task);
1653}
1654
Trond Myklebustba60eb22013-04-14 10:49:37 -04001655static void call_retry_reserve(struct rpc_task *task);
1656
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657/*
1658 * 1b. Grok the result of xprt_reserve()
1659 */
1660static void
1661call_reserveresult(struct rpc_task *task)
1662{
1663 int status = task->tk_status;
1664
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 /*
1666 * After a call to xprt_reserve(), we must have either
1667 * a request slot or else an error status.
1668 */
1669 task->tk_status = 0;
1670 if (status >= 0) {
1671 if (task->tk_rqstp) {
J. Bruce Fieldsf2d47d02010-09-12 19:55:25 -04001672 task->tk_action = call_refresh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 return;
1674 }
1675
Trond Myklebust5ad64b32019-04-07 13:58:54 -04001676 rpc_call_rpcerror(task, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 return;
1678 }
1679
1680 /*
1681 * Even though there was an error, we may have acquired
1682 * a request slot somehow. Make sure not to leak it.
1683 */
Chuck Lever5cd8b0d2019-10-09 12:58:08 -04001684 if (task->tk_rqstp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 xprt_release(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686
1687 switch (status) {
Trond Myklebust1afeaf52012-05-19 12:12:53 -04001688 case -ENOMEM:
1689 rpc_delay(task, HZ >> 2);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001690 fallthrough;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 case -EAGAIN: /* woken up; retry */
Trond Myklebustba60eb22013-04-14 10:49:37 -04001692 task->tk_action = call_retry_reserve;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 default:
Chuck Lever5cd8b0d2019-10-09 12:58:08 -04001695 rpc_call_rpcerror(task, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697}
1698
1699/*
Trond Myklebustba60eb22013-04-14 10:49:37 -04001700 * 1c. Retry reserving an RPC call slot
1701 */
1702static void
1703call_retry_reserve(struct rpc_task *task)
1704{
Trond Myklebustba60eb22013-04-14 10:49:37 -04001705 task->tk_status = 0;
1706 task->tk_action = call_reserveresult;
1707 xprt_retry_reserve(task);
1708}
1709
1710/*
J. Bruce Fields55576242010-09-12 19:55:25 -04001711 * 2. Bind and/or refresh the credentials
1712 */
1713static void
1714call_refresh(struct rpc_task *task)
1715{
J. Bruce Fields55576242010-09-12 19:55:25 -04001716 task->tk_action = call_refreshresult;
1717 task->tk_status = 0;
1718 task->tk_client->cl_stats->rpcauthrefresh++;
1719 rpcauth_refreshcred(task);
1720}
1721
1722/*
1723 * 2a. Process the results of a credential refresh
1724 */
1725static void
1726call_refreshresult(struct rpc_task *task)
1727{
1728 int status = task->tk_status;
1729
J. Bruce Fields55576242010-09-12 19:55:25 -04001730 task->tk_status = 0;
Trond Myklebust5fc43972010-11-20 11:13:31 -05001731 task->tk_action = call_refresh;
J. Bruce Fields55576242010-09-12 19:55:25 -04001732 switch (status) {
Trond Myklebust5fc43972010-11-20 11:13:31 -05001733 case 0:
Weston Andros Adamson6ff33b72013-12-17 12:16:11 -05001734 if (rpcauth_uptodatecred(task)) {
Trond Myklebust5fc43972010-11-20 11:13:31 -05001735 task->tk_action = call_allocate;
Weston Andros Adamson6ff33b72013-12-17 12:16:11 -05001736 return;
1737 }
1738 /* Use rate-limiting and a max number of retries if refresh
1739 * had status 0 but failed to update the cred.
1740 */
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001741 fallthrough;
J. Bruce Fields55576242010-09-12 19:55:25 -04001742 case -ETIMEDOUT:
1743 rpc_delay(task, 3*HZ);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001744 fallthrough;
Trond Myklebust5fc43972010-11-20 11:13:31 -05001745 case -EAGAIN:
1746 status = -EACCES;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001747 fallthrough;
Andy Adamsonf1ff0c22013-08-14 11:59:13 -04001748 case -EKEYEXPIRED:
Trond Myklebust5fc43972010-11-20 11:13:31 -05001749 if (!task->tk_cred_retry)
1750 break;
1751 task->tk_cred_retry--;
Chuck Lever7c8099f2020-07-08 16:10:03 -04001752 trace_rpc_retry_refresh_status(task);
Trond Myklebust5fc43972010-11-20 11:13:31 -05001753 return;
J. Bruce Fields55576242010-09-12 19:55:25 -04001754 }
Chuck Lever7c8099f2020-07-08 16:10:03 -04001755 trace_rpc_refresh_status(task);
Trond Myklebust5ad64b32019-04-07 13:58:54 -04001756 rpc_call_rpcerror(task, status);
J. Bruce Fields55576242010-09-12 19:55:25 -04001757}
1758
1759/*
1760 * 2b. Allocate the buffer. For details, see sched.c:rpc_malloc.
Chuck Lever02107142006-01-03 09:55:49 +01001761 * (Note: buffer memory is freed in xprt_release).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 */
1763static void
1764call_allocate(struct rpc_task *task)
1765{
Chuck Lever2c94b8e2019-02-11 11:25:41 -05001766 const struct rpc_auth *auth = task->tk_rqstp->rq_cred->cr_auth;
Chuck Lever02107142006-01-03 09:55:49 +01001767 struct rpc_rqst *req = task->tk_rqstp;
Trond Myklebusta4f08352013-01-08 09:10:21 -05001768 struct rpc_xprt *xprt = req->rq_xprt;
Christoph Hellwig499b4982017-05-12 15:36:49 +02001769 const struct rpc_procinfo *proc = task->tk_msg.rpc_proc;
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04001770 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771
Chuck Lever2bea90d2007-03-29 16:47:53 -04001772 task->tk_status = 0;
Trond Myklebust762e4e62018-08-24 16:28:28 -04001773 task->tk_action = call_encode;
Chuck Lever2bea90d2007-03-29 16:47:53 -04001774
Trond Myklebustaf6b61d2019-04-11 15:16:52 -04001775 if (req->rq_buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 return;
1777
Chuck Lever2bea90d2007-03-29 16:47:53 -04001778 if (proc->p_proc != 0) {
1779 BUG_ON(proc->p_arglen == 0);
1780 if (proc->p_decode != NULL)
1781 BUG_ON(proc->p_replen == 0);
1782 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783
Chuck Lever2bea90d2007-03-29 16:47:53 -04001784 /*
1785 * Calculate the size (in quads) of the RPC call
1786 * and reply headers, and convert both values
1787 * to byte sizes.
1788 */
Chuck Lever2c94b8e2019-02-11 11:25:41 -05001789 req->rq_callsize = RPC_CALLHDRSIZE + (auth->au_cslack << 1) +
1790 proc->p_arglen;
Chuck Lever2bea90d2007-03-29 16:47:53 -04001791 req->rq_callsize <<= 2;
Trond Myklebust51314962019-03-15 12:55:59 -04001792 /*
1793 * Note: the reply buffer must at minimum allocate enough space
1794 * for the 'struct accepted_reply' from RFC5531.
1795 */
1796 req->rq_rcvsize = RPC_REPHDRSIZE + auth->au_rslack + \
1797 max_t(size_t, proc->p_replen, 2);
Chuck Lever2bea90d2007-03-29 16:47:53 -04001798 req->rq_rcvsize <<= 2;
1799
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04001800 status = xprt->ops->buf_alloc(task);
Chuck Lever06e234c2020-07-08 16:09:11 -04001801 trace_rpc_buf_alloc(task, status);
Chuck Lever4a068252015-05-11 14:02:25 -04001802 xprt_inject_disconnect(xprt);
Trond Myklebustaf6b61d2019-04-11 15:16:52 -04001803 if (status == 0)
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04001804 return;
1805 if (status != -ENOMEM) {
Trond Myklebust5ad64b32019-04-07 13:58:54 -04001806 rpc_call_rpcerror(task, status);
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04001807 return;
1808 }
Chuck Lever46121cf2007-01-31 12:14:08 -05001809
Trond Myklebust5afa91332011-06-17 10:14:59 -04001810 if (RPC_IS_ASYNC(task) || !fatal_signal_pending(current)) {
Trond Myklebustb6e9c712007-10-01 12:06:44 -04001811 task->tk_action = call_allocate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 rpc_delay(task, HZ>>4);
1813 return;
1814 }
1815
Trond Myklebust714fbc72019-09-12 08:06:51 -04001816 rpc_call_rpcerror(task, -ERESTARTSYS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817}
1818
Trond Myklebust7ebbbc62018-08-28 09:00:27 -04001819static int
Trond Myklebust940e3312005-11-09 21:45:24 -05001820rpc_task_need_encode(struct rpc_task *task)
1821{
Trond Myklebust762e4e62018-08-24 16:28:28 -04001822 return test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate) == 0 &&
1823 (!(task->tk_flags & RPC_TASK_SENT) ||
1824 !(task->tk_flags & RPC_TASK_NO_RETRANS_TIMEOUT) ||
1825 xprt_request_need_retransmit(task));
Trond Myklebust940e3312005-11-09 21:45:24 -05001826}
1827
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828static void
Chuck Leverb0e1c572008-05-21 17:09:19 -04001829rpc_xdr_encode(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 struct rpc_rqst *req = task->tk_rqstp;
Chuck Levere8680a22019-02-11 11:24:48 -05001832 struct xdr_stream xdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833
Chuck Leverb9c5bc02016-09-15 10:55:12 -04001834 xdr_buf_init(&req->rq_snd_buf,
1835 req->rq_buffer,
1836 req->rq_callsize);
1837 xdr_buf_init(&req->rq_rcv_buf,
Chuck Lever68778942016-09-15 10:55:37 -04001838 req->rq_rbuffer,
Chuck Leverb9c5bc02016-09-15 10:55:12 -04001839 req->rq_rcvsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840
Trond Myklebustcc204d02019-09-10 13:01:35 -04001841 req->rq_reply_bytes_recvd = 0;
Chuck Levere8680a22019-02-11 11:24:48 -05001842 req->rq_snd_buf.head[0].iov_len = 0;
1843 xdr_init_encode(&xdr, &req->rq_snd_buf,
1844 req->rq_snd_buf.head[0].iov_base, req);
Trond Myklebust75369082019-07-17 21:22:38 -04001845 xdr_free_bvec(&req->rq_snd_buf);
Chuck Levere8680a22019-02-11 11:24:48 -05001846 if (rpc_encode_header(task, &xdr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 return;
Chuck Leverb0e1c572008-05-21 17:09:19 -04001848
Chuck Levere8680a22019-02-11 11:24:48 -05001849 task->tk_status = rpcauth_wrap_req(task, &xdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850}
1851
1852/*
Trond Myklebust762e4e62018-08-24 16:28:28 -04001853 * 3. Encode arguments of an RPC call
1854 */
1855static void
1856call_encode(struct rpc_task *task)
1857{
1858 if (!rpc_task_need_encode(task))
1859 goto out;
Chuck Lever63870392020-07-08 16:09:37 -04001860
Trond Myklebustcc204d02019-09-10 13:01:35 -04001861 /* Dequeue task from the receive queue while we're encoding */
1862 xprt_request_dequeue_xprt(task);
Trond Myklebust762e4e62018-08-24 16:28:28 -04001863 /* Encode here so that rpcsec_gss can use correct sequence number. */
1864 rpc_xdr_encode(task);
1865 /* Did the encode result in an error condition? */
1866 if (task->tk_status != 0) {
1867 /* Was the error nonfatal? */
Trond Myklebust97b78ae2019-01-02 17:53:13 -05001868 switch (task->tk_status) {
1869 case -EAGAIN:
1870 case -ENOMEM:
Trond Myklebust762e4e62018-08-24 16:28:28 -04001871 rpc_delay(task, HZ >> 4);
Trond Myklebust97b78ae2019-01-02 17:53:13 -05001872 break;
1873 case -EKEYEXPIRED:
ZhangXiaoxu9c5948c2019-04-29 17:32:31 +08001874 if (!task->tk_cred_retry) {
1875 rpc_exit(task, task->tk_status);
1876 } else {
1877 task->tk_action = call_refresh;
1878 task->tk_cred_retry--;
Chuck Lever7c8099f2020-07-08 16:10:03 -04001879 trace_rpc_retry_refresh_status(task);
ZhangXiaoxu9c5948c2019-04-29 17:32:31 +08001880 }
Trond Myklebust97b78ae2019-01-02 17:53:13 -05001881 break;
1882 default:
Trond Myklebust5ad64b32019-04-07 13:58:54 -04001883 rpc_call_rpcerror(task, task->tk_status);
Trond Myklebust97b78ae2019-01-02 17:53:13 -05001884 }
Trond Myklebust762e4e62018-08-24 16:28:28 -04001885 return;
1886 }
1887
1888 /* Add task to reply queue before transmission to avoid races */
1889 if (rpc_reply_expected(task))
1890 xprt_request_enqueue_receive(task);
1891 xprt_request_enqueue_transmit(task);
1892out:
Trond Myklebustaf6b61d2019-04-11 15:16:52 -04001893 task->tk_action = call_transmit;
1894 /* Check that the connection is OK */
1895 if (!xprt_bound(task->tk_xprt))
1896 task->tk_action = call_bind;
1897 else if (!xprt_connected(task->tk_xprt))
1898 task->tk_action = call_connect;
Trond Myklebust762e4e62018-08-24 16:28:28 -04001899}
1900
1901/*
Trond Myklebust03e51d32019-03-10 11:17:13 -04001902 * Helpers to check if the task was already transmitted, and
1903 * to take action when that is the case.
1904 */
1905static bool
1906rpc_task_transmitted(struct rpc_task *task)
1907{
1908 return !test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate);
1909}
1910
1911static void
1912rpc_task_handle_transmitted(struct rpc_task *task)
1913{
1914 xprt_end_transmit(task);
1915 task->tk_action = call_transmit_status;
Trond Myklebust03e51d32019-03-10 11:17:13 -04001916}
1917
1918/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 * 4. Get the server port number if not yet set
1920 */
1921static void
1922call_bind(struct rpc_task *task)
1923{
Trond Myklebustad2368d2013-01-08 10:08:33 -05001924 struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925
Trond Myklebust03e51d32019-03-10 11:17:13 -04001926 if (rpc_task_transmitted(task)) {
1927 rpc_task_handle_transmitted(task);
1928 return;
1929 }
1930
Trond Myklebust009a82f2019-03-09 12:07:17 -05001931 if (xprt_bound(xprt)) {
1932 task->tk_action = call_connect;
Trond Myklebust009a82f2019-03-09 12:07:17 -05001933 return;
1934 }
1935
Trond Myklebust009a82f2019-03-09 12:07:17 -05001936 task->tk_action = call_bind_status;
Trond Myklebust4d6c6712019-03-10 12:22:39 -04001937 if (!xprt_prepare_transmit(task))
1938 return;
1939
Trond Myklebust009a82f2019-03-09 12:07:17 -05001940 xprt->ops->rpcbind(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941}
1942
1943/*
Chuck Leverda351872005-08-11 16:25:11 -04001944 * 4a. Sort out bind result
1945 */
1946static void
1947call_bind_status(struct rpc_task *task)
1948{
Trond Myklebustbd736ed2019-08-15 17:26:17 -04001949 struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
Chuck Lever906462a2007-09-11 18:00:47 -04001950 int status = -EIO;
Chuck Leverda351872005-08-11 16:25:11 -04001951
Trond Myklebust03e51d32019-03-10 11:17:13 -04001952 if (rpc_task_transmitted(task)) {
1953 rpc_task_handle_transmitted(task);
1954 return;
1955 }
1956
Trond Myklebustbd736ed2019-08-15 17:26:17 -04001957 if (task->tk_status >= 0)
1958 goto out_next;
1959 if (xprt_bound(xprt)) {
Chuck Leverda351872005-08-11 16:25:11 -04001960 task->tk_status = 0;
Trond Myklebustbd736ed2019-08-15 17:26:17 -04001961 goto out_next;
Chuck Leverda351872005-08-11 16:25:11 -04001962 }
1963
1964 switch (task->tk_status) {
Trond Myklebust381ba742008-07-07 12:18:53 -04001965 case -ENOMEM:
Trond Myklebust381ba742008-07-07 12:18:53 -04001966 rpc_delay(task, HZ >> 2);
Chuck Lever2429cbf2007-09-11 18:00:41 -04001967 goto retry_timeout;
Chuck Leverda351872005-08-11 16:25:11 -04001968 case -EACCES:
Chuck Lever42ebfc22020-07-08 16:10:14 -04001969 trace_rpcb_prog_unavail_err(task);
Chuck Leverb79dc8c2007-09-11 18:00:52 -04001970 /* fail immediately if this is an RPC ping */
1971 if (task->tk_msg.rpc_proc->p_proc == 0) {
1972 status = -EOPNOTSUPP;
1973 break;
1974 }
Trond Myklebust0b760112011-05-31 15:15:34 -04001975 if (task->tk_rebind_retry == 0)
1976 break;
1977 task->tk_rebind_retry--;
Chuck Leverea635a52005-10-06 23:12:58 -04001978 rpc_delay(task, 3*HZ);
Trond Myklebustda45828e2006-08-31 15:44:52 -04001979 goto retry_timeout;
Trond Myklebust80f455d2019-08-15 22:55:19 -04001980 case -ENOBUFS:
1981 rpc_delay(task, HZ >> 2);
1982 goto retry_timeout;
Trond Myklebust4d6c6712019-03-10 12:22:39 -04001983 case -EAGAIN:
1984 goto retry_timeout;
Chuck Leverda351872005-08-11 16:25:11 -04001985 case -ETIMEDOUT:
Chuck Lever42ebfc22020-07-08 16:10:14 -04001986 trace_rpcb_timeout_err(task);
Trond Myklebustda45828e2006-08-31 15:44:52 -04001987 goto retry_timeout;
Chuck Leverda351872005-08-11 16:25:11 -04001988 case -EPFNOSUPPORT:
Chuck Lever906462a2007-09-11 18:00:47 -04001989 /* server doesn't support any rpcbind version we know of */
Chuck Lever42ebfc22020-07-08 16:10:14 -04001990 trace_rpcb_bind_version_err(task);
Chuck Leverda351872005-08-11 16:25:11 -04001991 break;
1992 case -EPROTONOSUPPORT:
Chuck Lever42ebfc22020-07-08 16:10:14 -04001993 trace_rpcb_bind_version_err(task);
Trond Myklebustfdb63dc2014-03-17 12:57:31 -04001994 goto retry_timeout;
Chuck Lever012da152009-12-03 15:58:56 -05001995 case -ECONNREFUSED: /* connection problems */
1996 case -ECONNRESET:
Trond Myklebustdf277272013-12-31 13:11:43 -05001997 case -ECONNABORTED:
Chuck Lever012da152009-12-03 15:58:56 -05001998 case -ENOTCONN:
1999 case -EHOSTDOWN:
Trond Myklebusteb5b46f2017-11-30 07:21:33 -05002000 case -ENETDOWN:
Chuck Lever012da152009-12-03 15:58:56 -05002001 case -EHOSTUNREACH:
2002 case -ENETUNREACH:
2003 case -EPIPE:
Chuck Lever42ebfc22020-07-08 16:10:14 -04002004 trace_rpcb_unreachable_err(task);
Chuck Lever012da152009-12-03 15:58:56 -05002005 if (!RPC_IS_SOFTCONN(task)) {
2006 rpc_delay(task, 5*HZ);
2007 goto retry_timeout;
2008 }
2009 status = task->tk_status;
2010 break;
Chuck Leverda351872005-08-11 16:25:11 -04002011 default:
Chuck Lever42ebfc22020-07-08 16:10:14 -04002012 trace_rpcb_unrecognized_err(task);
Chuck Leverda351872005-08-11 16:25:11 -04002013 }
2014
Trond Myklebust5ad64b32019-04-07 13:58:54 -04002015 rpc_call_rpcerror(task, status);
Chuck Leverda351872005-08-11 16:25:11 -04002016 return;
Trond Myklebustbd736ed2019-08-15 17:26:17 -04002017out_next:
2018 task->tk_action = call_connect;
2019 return;
Trond Myklebustda45828e2006-08-31 15:44:52 -04002020retry_timeout:
Trond Myklebustfdb63dc2014-03-17 12:57:31 -04002021 task->tk_status = 0;
Trond Myklebust4d6c6712019-03-10 12:22:39 -04002022 task->tk_action = call_bind;
Trond Myklebustcea57782019-03-09 16:06:47 -05002023 rpc_check_timeout(task);
Chuck Leverda351872005-08-11 16:25:11 -04002024}
2025
2026/*
2027 * 4b. Connect to the RPC server
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 */
2029static void
2030call_connect(struct rpc_task *task)
2031{
Trond Myklebustad2368d2013-01-08 10:08:33 -05002032 struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033
Trond Myklebust03e51d32019-03-10 11:17:13 -04002034 if (rpc_task_transmitted(task)) {
2035 rpc_task_handle_transmitted(task);
2036 return;
2037 }
2038
Trond Myklebust009a82f2019-03-09 12:07:17 -05002039 if (xprt_connected(xprt)) {
2040 task->tk_action = call_transmit;
Trond Myklebust009a82f2019-03-09 12:07:17 -05002041 return;
2042 }
2043
Trond Myklebust009a82f2019-03-09 12:07:17 -05002044 task->tk_action = call_connect_status;
2045 if (task->tk_status < 0)
2046 return;
2047 if (task->tk_flags & RPC_TASK_NOCONNECT) {
Trond Myklebust5ad64b32019-04-07 13:58:54 -04002048 rpc_call_rpcerror(task, -ENOTCONN);
Trond Myklebust009a82f2019-03-09 12:07:17 -05002049 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 }
Trond Myklebust4d6c6712019-03-10 12:22:39 -04002051 if (!xprt_prepare_transmit(task))
2052 return;
Trond Myklebust009a82f2019-03-09 12:07:17 -05002053 xprt_connect(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054}
2055
2056/*
Chuck Leverda351872005-08-11 16:25:11 -04002057 * 4c. Sort out connect result
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 */
2059static void
2060call_connect_status(struct rpc_task *task)
2061{
Trond Myklebustbd736ed2019-08-15 17:26:17 -04002062 struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 struct rpc_clnt *clnt = task->tk_client;
2064 int status = task->tk_status;
2065
Trond Myklebust03e51d32019-03-10 11:17:13 -04002066 if (rpc_task_transmitted(task)) {
2067 rpc_task_handle_transmitted(task);
Trond Myklebust9bd11522018-11-30 12:48:47 -05002068 return;
2069 }
2070
Chuck Levere671edb2018-03-16 10:33:44 -04002071 trace_rpc_connect_status(task);
Trond Myklebustbd736ed2019-08-15 17:26:17 -04002072
2073 if (task->tk_status == 0) {
2074 clnt->cl_stats->netreconn++;
2075 goto out_next;
2076 }
2077 if (xprt_connected(xprt)) {
2078 task->tk_status = 0;
2079 goto out_next;
2080 }
2081
Trond Myklebust561ec162013-09-26 15:22:45 -04002082 task->tk_status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 switch (status) {
Trond Myklebust3ed5e2a2013-03-04 17:29:33 -05002084 case -ECONNREFUSED:
NeilBrownfd01b252017-08-18 17:12:51 +10002085 /* A positive refusal suggests a rebind is needed. */
2086 if (RPC_IS_SOFTCONN(task))
2087 break;
2088 if (clnt->cl_autobind) {
2089 rpc_force_rebind(clnt);
Trond Myklebust7b3fef82019-03-07 14:10:32 -05002090 goto out_retry;
NeilBrownfd01b252017-08-18 17:12:51 +10002091 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002092 fallthrough;
Trond Myklebust3ed5e2a2013-03-04 17:29:33 -05002093 case -ECONNRESET:
Trond Myklebustdf277272013-12-31 13:11:43 -05002094 case -ECONNABORTED:
Trond Myklebusteb5b46f2017-11-30 07:21:33 -05002095 case -ENETDOWN:
Trond Myklebust3ed5e2a2013-03-04 17:29:33 -05002096 case -ENETUNREACH:
Trond Myklebustdf277272013-12-31 13:11:43 -05002097 case -EHOSTUNREACH:
Trond Myklebust2fc193c2014-07-03 00:02:57 -04002098 case -EPIPE:
Chuck Leverb8457602019-12-23 10:28:44 -05002099 case -EPROTO:
NeilBrown2c2ee6d2016-11-23 14:44:58 +11002100 xprt_conditional_disconnect(task->tk_rqstp->rq_xprt,
2101 task->tk_rqstp->rq_connect_cookie);
Trond Myklebust3ed5e2a2013-03-04 17:29:33 -05002102 if (RPC_IS_SOFTCONN(task))
2103 break;
Steve Dickson1fa3e2e2014-03-20 11:23:03 -04002104 /* retry with existing socket, after a delay */
2105 rpc_delay(task, 3*HZ);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002106 fallthrough;
Trond Myklebust80f455d2019-08-15 22:55:19 -04002107 case -EADDRINUSE:
Trond Myklebust0445f922018-12-17 13:34:59 -05002108 case -ENOTCONN:
Trond Myklebust3ed5e2a2013-03-04 17:29:33 -05002109 case -EAGAIN:
Trond Myklebust485f2252014-03-17 12:51:44 -04002110 case -ETIMEDOUT:
Trond Myklebust7b3fef82019-03-07 14:10:32 -05002111 goto out_retry;
Trond Myklebust80f455d2019-08-15 22:55:19 -04002112 case -ENOBUFS:
2113 rpc_delay(task, HZ >> 2);
2114 goto out_retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 }
Trond Myklebust5ad64b32019-04-07 13:58:54 -04002116 rpc_call_rpcerror(task, status);
Trond Myklebust7b3fef82019-03-07 14:10:32 -05002117 return;
Trond Myklebustbd736ed2019-08-15 17:26:17 -04002118out_next:
2119 task->tk_action = call_transmit;
2120 return;
Trond Myklebust7b3fef82019-03-07 14:10:32 -05002121out_retry:
2122 /* Check for timeouts before looping back to call_bind */
2123 task->tk_action = call_bind;
2124 rpc_check_timeout(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125}
2126
2127/*
2128 * 5. Transmit the RPC request, and wait for reply
2129 */
2130static void
2131call_transmit(struct rpc_task *task)
2132{
Trond Myklebust03e51d32019-03-10 11:17:13 -04002133 if (rpc_task_transmitted(task)) {
2134 rpc_task_handle_transmitted(task);
2135 return;
2136 }
2137
Trond Myklebusted7dc972019-03-04 14:19:31 -05002138 task->tk_action = call_transmit_status;
Trond Myklebust009a82f2019-03-09 12:07:17 -05002139 if (!xprt_prepare_transmit(task))
2140 return;
2141 task->tk_status = 0;
Trond Myklebustc5445772018-09-03 23:39:27 -04002142 if (test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate)) {
Trond Myklebust009a82f2019-03-09 12:07:17 -05002143 if (!xprt_connected(task->tk_xprt)) {
2144 task->tk_status = -ENOTCONN;
Trond Myklebustc5445772018-09-03 23:39:27 -04002145 return;
Trond Myklebusted7dc972019-03-04 14:19:31 -05002146 }
Trond Myklebust009a82f2019-03-09 12:07:17 -05002147 xprt_transmit(task);
Trond Myklebustc5445772018-09-03 23:39:27 -04002148 }
Trond Myklebustc5445772018-09-03 23:39:27 -04002149 xprt_end_transmit(task);
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04002150}
2151
2152/*
2153 * 5a. Handle cleanup after a transmission
2154 */
2155static void
2156call_transmit_status(struct rpc_task *task)
2157{
2158 task->tk_action = call_status;
Chuck Lever206a1342009-12-03 15:58:56 -05002159
2160 /*
2161 * Common case: success. Force the compiler to put this
2162 * test first.
2163 */
Trond Myklebust009a82f2019-03-09 12:07:17 -05002164 if (rpc_task_transmitted(task)) {
Trond Myklebusta7b1a482019-04-15 11:54:13 -04002165 task->tk_status = 0;
2166 xprt_request_wait_receive(task);
Chuck Lever206a1342009-12-03 15:58:56 -05002167 return;
2168 }
2169
Trond Myklebust15f081c2009-03-11 14:37:57 -04002170 switch (task->tk_status) {
Trond Myklebust15f081c2009-03-11 14:37:57 -04002171 default:
Trond Myklebust75891f52018-09-03 17:37:36 -04002172 break;
Trond Myklebust78b576c2018-08-28 09:20:10 -04002173 case -EBADMSG:
Trond Myklebust762e4e62018-08-24 16:28:28 -04002174 task->tk_status = 0;
2175 task->tk_action = call_encode;
Trond Myklebust78b576c2018-08-28 09:20:10 -04002176 break;
Trond Myklebust15f081c2009-03-11 14:37:57 -04002177 /*
2178 * Special cases: if we've been waiting on the
2179 * socket's write_space() callback, or if the
2180 * socket just returned a connection error,
2181 * then hold onto the transport lock.
2182 */
Trond Myklebust78b576c2018-08-28 09:20:10 -04002183 case -ENOBUFS:
2184 rpc_delay(task, HZ>>2);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002185 fallthrough;
Trond Myklebustc5445772018-09-03 23:39:27 -04002186 case -EBADSLT:
Trond Myklebust78b576c2018-08-28 09:20:10 -04002187 case -EAGAIN:
2188 task->tk_action = call_transmit;
2189 task->tk_status = 0;
2190 break;
Trond Myklebust15f081c2009-03-11 14:37:57 -04002191 case -ECONNREFUSED:
Trond Myklebust15f081c2009-03-11 14:37:57 -04002192 case -EHOSTDOWN:
Trond Myklebusteb5b46f2017-11-30 07:21:33 -05002193 case -ENETDOWN:
Trond Myklebust15f081c2009-03-11 14:37:57 -04002194 case -EHOSTUNREACH:
2195 case -ENETUNREACH:
Jason Baron3dedbb52014-09-24 18:08:04 +00002196 case -EPERM:
Chuck Lever09a21c42009-12-03 15:58:56 -05002197 if (RPC_IS_SOFTCONN(task)) {
Chuck Levera25a4cb2018-03-16 10:33:55 -04002198 if (!task->tk_msg.rpc_proc->p_proc)
2199 trace_xprt_ping(task->tk_xprt,
2200 task->tk_status);
Trond Myklebust5ad64b32019-04-07 13:58:54 -04002201 rpc_call_rpcerror(task, task->tk_status);
Trond Myklebust7b3fef82019-03-07 14:10:32 -05002202 return;
Chuck Lever09a21c42009-12-03 15:58:56 -05002203 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002204 fallthrough;
Chuck Lever09a21c42009-12-03 15:58:56 -05002205 case -ECONNRESET:
Trond Myklebustdf277272013-12-31 13:11:43 -05002206 case -ECONNABORTED:
Trond Myklebust3913c782015-02-08 21:44:04 -05002207 case -EADDRINUSE:
Chuck Lever09a21c42009-12-03 15:58:56 -05002208 case -ENOTCONN:
Trond Myklebustc8485e42009-03-11 14:37:59 -04002209 case -EPIPE:
Trond Myklebusted7dc972019-03-04 14:19:31 -05002210 task->tk_action = call_bind;
2211 task->tk_status = 0;
Trond Myklebust7ebbbc62018-08-28 09:00:27 -04002212 break;
Trond Myklebust15f081c2009-03-11 14:37:57 -04002213 }
Trond Myklebust7b3fef82019-03-07 14:10:32 -05002214 rpc_check_timeout(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215}
2216
Trond Myklebust9e00abc2011-07-13 19:20:49 -04002217#if defined(CONFIG_SUNRPC_BACKCHANNEL)
Trond Myklebust477687e2019-03-05 07:30:48 -05002218static void call_bc_transmit(struct rpc_task *task);
2219static void call_bc_transmit_status(struct rpc_task *task);
2220
2221static void
2222call_bc_encode(struct rpc_task *task)
2223{
2224 xprt_request_enqueue_transmit(task);
2225 task->tk_action = call_bc_transmit;
2226}
2227
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04002228/*
2229 * 5b. Send the backchannel RPC reply. On error, drop the reply. In
2230 * addition, disconnect on connectivity errors.
2231 */
2232static void
2233call_bc_transmit(struct rpc_task *task)
2234{
Trond Myklebust477687e2019-03-05 07:30:48 -05002235 task->tk_action = call_bc_transmit_status;
2236 if (test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate)) {
2237 if (!xprt_prepare_transmit(task))
2238 return;
2239 task->tk_status = 0;
2240 xprt_transmit(task);
2241 }
2242 xprt_end_transmit(task);
2243}
2244
2245static void
2246call_bc_transmit_status(struct rpc_task *task)
2247{
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04002248 struct rpc_rqst *req = task->tk_rqstp;
2249
Trond Myklebusta7b1a482019-04-15 11:54:13 -04002250 if (rpc_task_transmitted(task))
2251 task->tk_status = 0;
2252
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04002253 switch (task->tk_status) {
2254 case 0:
2255 /* Success */
Trond Myklebusteb5b46f2017-11-30 07:21:33 -05002256 case -ENETDOWN:
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04002257 case -EHOSTDOWN:
2258 case -EHOSTUNREACH:
2259 case -ENETUNREACH:
Trond Myklebust38325912015-06-19 13:04:13 -04002260 case -ECONNRESET:
2261 case -ECONNREFUSED:
2262 case -EADDRINUSE:
2263 case -ENOTCONN:
2264 case -EPIPE:
2265 break;
Trond Myklebust477687e2019-03-05 07:30:48 -05002266 case -ENOBUFS:
2267 rpc_delay(task, HZ>>2);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002268 fallthrough;
Trond Myklebust477687e2019-03-05 07:30:48 -05002269 case -EBADSLT:
Trond Myklebustc5445772018-09-03 23:39:27 -04002270 case -EAGAIN:
Trond Myklebust477687e2019-03-05 07:30:48 -05002271 task->tk_status = 0;
2272 task->tk_action = call_bc_transmit;
2273 return;
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04002274 case -ETIMEDOUT:
2275 /*
2276 * Problem reaching the server. Disconnect and let the
2277 * forechannel reestablish the connection. The server will
2278 * have to retransmit the backchannel request and we'll
2279 * reprocess it. Since these ops are idempotent, there's no
2280 * need to cache our reply at this time.
2281 */
2282 printk(KERN_NOTICE "RPC: Could not send backchannel reply "
2283 "error: %d\n", task->tk_status);
Trond Myklebusta4f08352013-01-08 09:10:21 -05002284 xprt_conditional_disconnect(req->rq_xprt,
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04002285 req->rq_connect_cookie);
2286 break;
2287 default:
2288 /*
2289 * We were unable to reply and will have to drop the
2290 * request. The server should reconnect and retransmit.
2291 */
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04002292 printk(KERN_NOTICE "RPC: Could not send backchannel reply "
2293 "error: %d\n", task->tk_status);
2294 break;
2295 }
Trond Myklebust1193d582015-06-02 11:53:21 -04002296 task->tk_action = rpc_exit_task;
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04002297}
Trond Myklebust9e00abc2011-07-13 19:20:49 -04002298#endif /* CONFIG_SUNRPC_BACKCHANNEL */
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04002299
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300/*
2301 * 6. Sort out the RPC call status
2302 */
2303static void
2304call_status(struct rpc_task *task)
2305{
2306 struct rpc_clnt *clnt = task->tk_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 int status;
2308
Chuck Levera25a4cb2018-03-16 10:33:55 -04002309 if (!task->tk_msg.rpc_proc->p_proc)
2310 trace_xprt_ping(task->tk_xprt, task->tk_status);
2311
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 status = task->tk_status;
2313 if (status >= 0) {
2314 task->tk_action = call_decode;
2315 return;
2316 }
2317
Steve Dickson5753cba2012-02-06 10:08:08 -05002318 trace_rpc_call_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 task->tk_status = 0;
2320 switch(status) {
Trond Myklebust76303992006-08-30 14:32:49 -04002321 case -EHOSTDOWN:
Trond Myklebusteb5b46f2017-11-30 07:21:33 -05002322 case -ENETDOWN:
Trond Myklebust76303992006-08-30 14:32:49 -04002323 case -EHOSTUNREACH:
2324 case -ENETUNREACH:
Jason Baron3dedbb52014-09-24 18:08:04 +00002325 case -EPERM:
Trond Myklebustcea57782019-03-09 16:06:47 -05002326 if (RPC_IS_SOFTCONN(task))
2327 goto out_exit;
Trond Myklebust76303992006-08-30 14:32:49 -04002328 /*
2329 * Delay any retries for 3 seconds, then handle as if it
2330 * were a timeout.
2331 */
2332 rpc_delay(task, 3*HZ);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002333 fallthrough;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 case -ETIMEDOUT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 break;
2336 case -ECONNREFUSED:
Trond Myklebustdf277272013-12-31 13:11:43 -05002337 case -ECONNRESET:
2338 case -ECONNABORTED:
Olga Kornievskaiaec6017d2019-05-29 10:46:00 -04002339 case -ENOTCONN:
Chuck Lever35f5a422006-01-03 09:55:50 +01002340 rpc_force_rebind(clnt);
Trond Myklebustc82e5472019-08-16 08:58:48 -04002341 break;
Trond Myklebust3913c782015-02-08 21:44:04 -05002342 case -EADDRINUSE:
Trond Myklebustc8485e42009-03-11 14:37:59 -04002343 rpc_delay(task, 3*HZ);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002344 fallthrough;
Trond Myklebustc8485e42009-03-11 14:37:59 -04002345 case -EPIPE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 case -EAGAIN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 break;
2348 case -EIO:
2349 /* shutdown or soft timeout */
Trond Myklebustcea57782019-03-09 16:06:47 -05002350 goto out_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 default:
Olga Kornievskaiab6b61522008-06-09 16:51:31 -04002352 if (clnt->cl_chatty)
2353 printk("%s: RPC call returned error %d\n",
Trond Myklebust55909f22013-08-23 11:48:15 -04002354 clnt->cl_program->name, -status);
Trond Myklebustcea57782019-03-09 16:06:47 -05002355 goto out_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 }
Trond Myklebustcea57782019-03-09 16:06:47 -05002357 task->tk_action = call_encode;
Olga Kornievskaia88428cc42020-06-23 11:24:09 -04002358 if (status != -ECONNRESET && status != -ECONNABORTED)
2359 rpc_check_timeout(task);
Trond Myklebustcea57782019-03-09 16:06:47 -05002360 return;
2361out_exit:
Trond Myklebust5ad64b32019-04-07 13:58:54 -04002362 rpc_call_rpcerror(task, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363}
2364
Trond Myklebustd84dd3f2019-03-19 11:24:54 -04002365static bool
2366rpc_check_connected(const struct rpc_rqst *req)
2367{
2368 /* No allocated request or transport? return true */
2369 if (!req || !req->rq_xprt)
2370 return true;
2371 return xprt_connected(req->rq_xprt);
2372}
2373
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374static void
Trond Myklebust7b3fef82019-03-07 14:10:32 -05002375rpc_check_timeout(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376{
2377 struct rpc_clnt *clnt = task->tk_client;
2378
Chuck Leverce99aa62020-05-09 14:07:13 -04002379 if (RPC_SIGNALLED(task)) {
2380 rpc_call_rpcerror(task, -ERESTARTSYS);
2381 return;
2382 }
2383
Trond Myklebust7b3fef82019-03-07 14:10:32 -05002384 if (xprt_adjust_timeout(task->tk_rqstp) == 0)
2385 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386
Chuck Lever914cdcc2020-07-08 16:09:58 -04002387 trace_rpc_timeout_status(task);
Chuck Leveref759a22006-03-20 13:44:17 -05002388 task->tk_timeouts++;
2389
Trond Myklebustd84dd3f2019-03-19 11:24:54 -04002390 if (RPC_IS_SOFTCONN(task) && !rpc_check_connected(task->tk_rqstp)) {
Trond Myklebust5ad64b32019-04-07 13:58:54 -04002391 rpc_call_rpcerror(task, -ETIMEDOUT);
Chuck Lever3a28bec2009-12-03 15:58:56 -05002392 return;
2393 }
Trond Myklebustd84dd3f2019-03-19 11:24:54 -04002394
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 if (RPC_IS_SOFT(task)) {
Trond Myklebuste4ec48d2019-04-07 13:58:55 -04002396 /*
2397 * Once a "no retrans timeout" soft tasks (a.k.a NFSv4) has
2398 * been sent, it should time out only if the transport
2399 * connection gets terminally broken.
2400 */
2401 if ((task->tk_flags & RPC_TASK_NO_RETRANS_TIMEOUT) &&
2402 rpc_check_connected(task->tk_rqstp))
2403 return;
2404
Joe Perchescac5d072012-07-18 11:17:11 -07002405 if (clnt->cl_chatty) {
Trond Myklebust0729d992019-04-07 13:58:57 -04002406 pr_notice_ratelimited(
2407 "%s: server %s not responding, timed out\n",
Trond Myklebust55909f22013-08-23 11:48:15 -04002408 clnt->cl_program->name,
Trond Myklebustfb43d172016-01-30 16:39:26 -05002409 task->tk_xprt->servername);
Joe Perchescac5d072012-07-18 11:17:11 -07002410 }
Trond Myklebust7494d002011-04-24 14:28:45 -04002411 if (task->tk_flags & RPC_TASK_TIMEOUT)
Trond Myklebust5ad64b32019-04-07 13:58:54 -04002412 rpc_call_rpcerror(task, -ETIMEDOUT);
Trond Myklebust7494d002011-04-24 14:28:45 -04002413 else
Trond Myklebust5ad64b32019-04-07 13:58:54 -04002414 __rpc_call_rpcerror(task, -EIO, -ETIMEDOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 return;
2416 }
2417
Chuck Leverf518e35a2006-01-03 09:55:52 +01002418 if (!(task->tk_flags & RPC_CALL_MAJORSEEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 task->tk_flags |= RPC_CALL_MAJORSEEN;
Trond Myklebust4e0038b2012-03-01 17:01:05 -05002420 if (clnt->cl_chatty) {
Trond Myklebust0729d992019-04-07 13:58:57 -04002421 pr_notice_ratelimited(
2422 "%s: server %s not responding, still trying\n",
2423 clnt->cl_program->name,
2424 task->tk_xprt->servername);
Trond Myklebust4e0038b2012-03-01 17:01:05 -05002425 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 }
Chuck Lever35f5a422006-01-03 09:55:50 +01002427 rpc_force_rebind(clnt);
Trond Myklebustb48633b2008-04-22 16:47:55 -04002428 /*
2429 * Did our request time out due to an RPCSEC_GSS out-of-sequence
2430 * event? RFC2203 requires the server to drop all such requests.
2431 */
2432 rpcauth_invalcred(task);
Trond Myklebust7b3fef82019-03-07 14:10:32 -05002433}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434
Trond Myklebust7b3fef82019-03-07 14:10:32 -05002435/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 * 7. Decode the RPC reply
2437 */
2438static void
2439call_decode(struct rpc_task *task)
2440{
2441 struct rpc_clnt *clnt = task->tk_client;
2442 struct rpc_rqst *req = task->tk_rqstp;
Chuck Levera0584ee2019-02-11 11:24:58 -05002443 struct xdr_stream xdr;
Trond Myklebust9ba82882019-09-16 09:12:19 -04002444 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445
Chuck Levera0584ee2019-02-11 11:24:58 -05002446 if (!task->tk_msg.rpc_proc->p_decode) {
Trond Myklebust9ee94d32018-08-28 16:27:31 -04002447 task->tk_action = rpc_exit_task;
2448 return;
2449 }
2450
Chuck Leverf518e35a2006-01-03 09:55:52 +01002451 if (task->tk_flags & RPC_CALL_MAJORSEEN) {
Trond Myklebust4e0038b2012-03-01 17:01:05 -05002452 if (clnt->cl_chatty) {
Trond Myklebust0729d992019-04-07 13:58:57 -04002453 pr_notice_ratelimited("%s: server %s OK\n",
Trond Myklebust55909f22013-08-23 11:48:15 -04002454 clnt->cl_program->name,
Trond Myklebustfb43d172016-01-30 16:39:26 -05002455 task->tk_xprt->servername);
Trond Myklebust4e0038b2012-03-01 17:01:05 -05002456 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 task->tk_flags &= ~RPC_CALL_MAJORSEEN;
2458 }
2459
Trond Myklebust43ac3f22006-03-20 13:44:51 -05002460 /*
2461 * Ensure that we see all writes made by xprt_complete_rqst()
Ricardo Labiagadd2b63d2009-04-01 09:23:28 -04002462 * before it changed req->rq_reply_bytes_recvd.
Trond Myklebust43ac3f22006-03-20 13:44:51 -05002463 */
2464 smp_rmb();
Trond Myklebust9ba82882019-09-16 09:12:19 -04002465
2466 /*
2467 * Did we ever call xprt_complete_rqst()? If not, we should assume
2468 * the message is incomplete.
2469 */
2470 err = -EAGAIN;
2471 if (!req->rq_reply_bytes_recvd)
2472 goto out;
2473
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 req->rq_rcv_buf.len = req->rq_private_buf.len;
Chuck Leverc509f15a2020-05-12 17:13:28 -04002475 trace_rpc_xdr_recvfrom(task, &req->rq_rcv_buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476
2477 /* Check that the softirq receive buffer is valid */
2478 WARN_ON(memcmp(&req->rq_rcv_buf, &req->rq_private_buf,
2479 sizeof(req->rq_rcv_buf)) != 0);
2480
Chuck Levera0584ee2019-02-11 11:24:58 -05002481 xdr_init_decode(&xdr, &req->rq_rcv_buf,
2482 req->rq_rcv_buf.head[0].iov_base, req);
Trond Myklebust9ba82882019-09-16 09:12:19 -04002483 err = rpc_decode_header(task, &xdr);
2484out:
2485 switch (err) {
Chuck Levera0584ee2019-02-11 11:24:58 -05002486 case 0:
2487 task->tk_action = rpc_exit_task;
2488 task->tk_status = rpcauth_unwrap_resp(task, &xdr);
Trond Myklebustabbcf282006-01-03 09:55:03 +01002489 return;
Chuck Levera0584ee2019-02-11 11:24:58 -05002490 case -EAGAIN:
Chuck Levera0584ee2019-02-11 11:24:58 -05002491 task->tk_status = 0;
Trond Myklebust7987b692019-05-29 12:49:52 -04002492 if (task->tk_client->cl_discrtry)
2493 xprt_conditional_disconnect(req->rq_xprt,
2494 req->rq_connect_cookie);
Trond Myklebustcea57782019-03-09 16:06:47 -05002495 task->tk_action = call_encode;
2496 rpc_check_timeout(task);
Trond Myklebust7987b692019-05-29 12:49:52 -04002497 break;
2498 case -EKEYREJECTED:
2499 task->tk_action = call_reserve;
2500 rpc_check_timeout(task);
2501 rpcauth_invalcred(task);
2502 /* Ensure we obtain a new XID if we retry! */
2503 xprt_release(task);
Trond Myklebust24b74bf2008-04-19 13:15:47 -04002504 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505}
2506
Chuck Levere8680a22019-02-11 11:24:48 -05002507static int
2508rpc_encode_header(struct rpc_task *task, struct xdr_stream *xdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509{
2510 struct rpc_clnt *clnt = task->tk_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 struct rpc_rqst *req = task->tk_rqstp;
Chuck Levere8680a22019-02-11 11:24:48 -05002512 __be32 *p;
2513 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514
Chuck Levere8680a22019-02-11 11:24:48 -05002515 error = -EMSGSIZE;
2516 p = xdr_reserve_space(xdr, RPC_CALLHDRSIZE << 2);
2517 if (!p)
2518 goto out_fail;
2519 *p++ = req->rq_xid;
2520 *p++ = rpc_call;
2521 *p++ = cpu_to_be32(RPC_VERSION);
2522 *p++ = cpu_to_be32(clnt->cl_prog);
2523 *p++ = cpu_to_be32(clnt->cl_vers);
2524 *p = cpu_to_be32(task->tk_msg.rpc_proc->p_proc);
Chuck Lever808012f2005-08-25 16:25:49 -07002525
Chuck Levere8680a22019-02-11 11:24:48 -05002526 error = rpcauth_marshcred(task, xdr);
2527 if (error < 0)
2528 goto out_fail;
2529 return 0;
2530out_fail:
2531 trace_rpc_bad_callhdr(task);
Trond Myklebust714fbc72019-09-12 08:06:51 -04002532 rpc_call_rpcerror(task, error);
Chuck Levere8680a22019-02-11 11:24:48 -05002533 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534}
2535
Chuck Levera0584ee2019-02-11 11:24:58 -05002536static noinline int
2537rpc_decode_header(struct rpc_task *task, struct xdr_stream *xdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538{
Trond Myklebust4e0038b2012-03-01 17:01:05 -05002539 struct rpc_clnt *clnt = task->tk_client;
Trond Myklebusteb90a162019-03-15 09:29:00 -04002540 int error;
Chuck Levera0584ee2019-02-11 11:24:58 -05002541 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542
Chuck Lever7f5667a2019-02-11 11:24:53 -05002543 /* RFC-1014 says that the representation of XDR data must be a
2544 * multiple of four bytes
2545 * - if it isn't pointer subtraction in the NFS client may give
2546 * undefined results
2547 */
2548 if (task->tk_rqstp->rq_rcv_buf.len & 3)
Trond Myklebusteb90a162019-03-15 09:29:00 -04002549 goto out_unparsable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550
Chuck Levera0584ee2019-02-11 11:24:58 -05002551 p = xdr_inline_decode(xdr, 3 * sizeof(*p));
2552 if (!p)
2553 goto out_unparsable;
Chuck Lever7f5667a2019-02-11 11:24:53 -05002554 p++; /* skip XID */
2555 if (*p++ != rpc_reply)
2556 goto out_unparsable;
2557 if (*p++ != rpc_msg_accepted)
2558 goto out_msg_denied;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04002559
Chuck Levera0584ee2019-02-11 11:24:58 -05002560 error = rpcauth_checkverf(task, xdr);
2561 if (error)
Chuck Lever7f5667a2019-02-11 11:24:53 -05002562 goto out_verifier;
2563
Chuck Levera0584ee2019-02-11 11:24:58 -05002564 p = xdr_inline_decode(xdr, sizeof(*p));
2565 if (!p)
Chuck Lever7f5667a2019-02-11 11:24:53 -05002566 goto out_unparsable;
Chuck Levera0584ee2019-02-11 11:24:58 -05002567 switch (*p) {
Chuck Lever7f5667a2019-02-11 11:24:53 -05002568 case rpc_success:
Chuck Levera0584ee2019-02-11 11:24:58 -05002569 return 0;
Chuck Lever7f5667a2019-02-11 11:24:53 -05002570 case rpc_prog_unavail:
2571 trace_rpc__prog_unavail(task);
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00002572 error = -EPFNOSUPPORT;
2573 goto out_err;
Chuck Lever7f5667a2019-02-11 11:24:53 -05002574 case rpc_prog_mismatch:
2575 trace_rpc__prog_mismatch(task);
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00002576 error = -EPROTONOSUPPORT;
2577 goto out_err;
Chuck Lever7f5667a2019-02-11 11:24:53 -05002578 case rpc_proc_unavail:
2579 trace_rpc__proc_unavail(task);
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00002580 error = -EOPNOTSUPP;
2581 goto out_err;
Chuck Lever7f5667a2019-02-11 11:24:53 -05002582 case rpc_garbage_args:
Trond Myklebust928d42f2019-03-15 10:12:30 -04002583 case rpc_system_err:
Chuck Lever7f5667a2019-02-11 11:24:53 -05002584 trace_rpc__garbage_args(task);
Trond Myklebusteb90a162019-03-15 09:29:00 -04002585 error = -EIO;
Chuck Lever7f5667a2019-02-11 11:24:53 -05002586 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 default:
Trond Myklebusteb90a162019-03-15 09:29:00 -04002588 goto out_unparsable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 }
2590
Trond Myklebustabbcf282006-01-03 09:55:03 +01002591out_garbage:
Trond Myklebust4e0038b2012-03-01 17:01:05 -05002592 clnt->cl_stats->rpcgarbage++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 if (task->tk_garb_retry) {
2594 task->tk_garb_retry--;
Trond Myklebust762e4e62018-08-24 16:28:28 -04002595 task->tk_action = call_encode;
Chuck Levera0584ee2019-02-11 11:24:58 -05002596 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598out_err:
Trond Myklebust714fbc72019-09-12 08:06:51 -04002599 rpc_call_rpcerror(task, error);
Chuck Levera0584ee2019-02-11 11:24:58 -05002600 return error;
Chuck Lever7f5667a2019-02-11 11:24:53 -05002601
Chuck Lever7f5667a2019-02-11 11:24:53 -05002602out_unparsable:
2603 trace_rpc__unparsable(task);
2604 error = -EIO;
Trond Myklebustabbcf282006-01-03 09:55:03 +01002605 goto out_garbage;
Chuck Lever7f5667a2019-02-11 11:24:53 -05002606
2607out_verifier:
2608 trace_rpc_bad_verifier(task);
Chuck Lever7f5667a2019-02-11 11:24:53 -05002609 goto out_garbage;
2610
2611out_msg_denied:
Trond Myklebusteb90a162019-03-15 09:29:00 -04002612 error = -EACCES;
Chuck Levera0584ee2019-02-11 11:24:58 -05002613 p = xdr_inline_decode(xdr, sizeof(*p));
2614 if (!p)
2615 goto out_unparsable;
Chuck Lever7f5667a2019-02-11 11:24:53 -05002616 switch (*p++) {
2617 case rpc_auth_error:
2618 break;
2619 case rpc_mismatch:
2620 trace_rpc__mismatch(task);
2621 error = -EPROTONOSUPPORT;
2622 goto out_err;
2623 default:
Trond Myklebusteb90a162019-03-15 09:29:00 -04002624 goto out_unparsable;
Chuck Lever7f5667a2019-02-11 11:24:53 -05002625 }
2626
Chuck Levera0584ee2019-02-11 11:24:58 -05002627 p = xdr_inline_decode(xdr, sizeof(*p));
2628 if (!p)
2629 goto out_unparsable;
Chuck Lever7f5667a2019-02-11 11:24:53 -05002630 switch (*p++) {
2631 case rpc_autherr_rejectedcred:
2632 case rpc_autherr_rejectedverf:
2633 case rpcsec_gsserr_credproblem:
2634 case rpcsec_gsserr_ctxproblem:
2635 if (!task->tk_cred_retry)
2636 break;
2637 task->tk_cred_retry--;
2638 trace_rpc__stale_creds(task);
Trond Myklebust7987b692019-05-29 12:49:52 -04002639 return -EKEYREJECTED;
Chuck Lever7f5667a2019-02-11 11:24:53 -05002640 case rpc_autherr_badcred:
2641 case rpc_autherr_badverf:
2642 /* possibly garbled cred/verf? */
2643 if (!task->tk_garb_retry)
2644 break;
2645 task->tk_garb_retry--;
2646 trace_rpc__bad_creds(task);
2647 task->tk_action = call_encode;
Chuck Levera0584ee2019-02-11 11:24:58 -05002648 return -EAGAIN;
Chuck Lever7f5667a2019-02-11 11:24:53 -05002649 case rpc_autherr_tooweak:
2650 trace_rpc__auth_tooweak(task);
2651 pr_warn("RPC: server %s requires stronger authentication.\n",
2652 task->tk_xprt->servername);
2653 break;
2654 default:
Trond Myklebusteb90a162019-03-15 09:29:00 -04002655 goto out_unparsable;
Chuck Lever7f5667a2019-02-11 11:24:53 -05002656 }
2657 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658}
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00002659
Christoph Hellwigc512f362017-05-08 09:31:19 +02002660static void rpcproc_encode_null(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
2661 const void *obj)
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00002662{
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00002663}
2664
Christoph Hellwig73c8dc12017-05-08 14:58:11 +02002665static int rpcproc_decode_null(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
2666 void *obj)
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00002667{
2668 return 0;
2669}
2670
Christoph Hellwig499b4982017-05-12 15:36:49 +02002671static const struct rpc_procinfo rpcproc_null = {
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00002672 .p_encode = rpcproc_encode_null,
2673 .p_decode = rpcproc_decode_null,
2674};
2675
Chuck Levercaabea82009-12-03 15:58:56 -05002676static int rpc_ping(struct rpc_clnt *clnt)
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00002677{
2678 struct rpc_message msg = {
2679 .rpc_proc = &rpcproc_null,
2680 };
2681 int err;
NeilBrowna68a72e2018-12-03 11:30:30 +11002682 err = rpc_call_sync(clnt, &msg, RPC_TASK_SOFT | RPC_TASK_SOFTCONN |
2683 RPC_TASK_NULLCREDS);
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00002684 return err;
2685}
Trond Myklebust188fef12007-06-16 14:18:40 -04002686
Trond Myklebust7f554892016-01-30 23:43:35 -05002687static
2688struct rpc_task *rpc_call_null_helper(struct rpc_clnt *clnt,
2689 struct rpc_xprt *xprt, struct rpc_cred *cred, int flags,
2690 const struct rpc_call_ops *ops, void *data)
Trond Myklebust5e1550d2007-06-23 10:17:16 -04002691{
2692 struct rpc_message msg = {
2693 .rpc_proc = &rpcproc_null,
Trond Myklebust5e1550d2007-06-23 10:17:16 -04002694 };
Trond Myklebust84115e12007-07-14 15:39:59 -04002695 struct rpc_task_setup task_setup_data = {
2696 .rpc_client = clnt,
Trond Myklebust7f554892016-01-30 23:43:35 -05002697 .rpc_xprt = xprt,
Trond Myklebust84115e12007-07-14 15:39:59 -04002698 .rpc_message = &msg,
NeilBrown1de7eea2018-12-03 11:30:30 +11002699 .rpc_op_cred = cred,
Trond Myklebust7f554892016-01-30 23:43:35 -05002700 .callback_ops = (ops != NULL) ? ops : &rpc_default_ops,
2701 .callback_data = data,
Chuck Lever841a2ed2020-05-12 17:13:55 -04002702 .flags = flags | RPC_TASK_SOFT | RPC_TASK_SOFTCONN |
2703 RPC_TASK_NULLCREDS,
Trond Myklebust84115e12007-07-14 15:39:59 -04002704 };
Trond Myklebust7f554892016-01-30 23:43:35 -05002705
Trond Myklebustc970aa82007-07-14 15:39:59 -04002706 return rpc_run_task(&task_setup_data);
Trond Myklebust5e1550d2007-06-23 10:17:16 -04002707}
Trond Myklebust7f554892016-01-30 23:43:35 -05002708
2709struct rpc_task *rpc_call_null(struct rpc_clnt *clnt, struct rpc_cred *cred, int flags)
2710{
2711 return rpc_call_null_helper(clnt, NULL, cred, flags, NULL, NULL);
2712}
Trond Myklebuste8914c62007-07-14 15:39:59 -04002713EXPORT_SYMBOL_GPL(rpc_call_null);
Trond Myklebust5e1550d2007-06-23 10:17:16 -04002714
Trond Myklebust7f554892016-01-30 23:43:35 -05002715struct rpc_cb_add_xprt_calldata {
2716 struct rpc_xprt_switch *xps;
2717 struct rpc_xprt *xprt;
2718};
2719
2720static void rpc_cb_add_xprt_done(struct rpc_task *task, void *calldata)
2721{
2722 struct rpc_cb_add_xprt_calldata *data = calldata;
2723
2724 if (task->tk_status == 0)
2725 rpc_xprt_switch_add_xprt(data->xps, data->xprt);
2726}
2727
2728static void rpc_cb_add_xprt_release(void *calldata)
2729{
2730 struct rpc_cb_add_xprt_calldata *data = calldata;
2731
2732 xprt_put(data->xprt);
2733 xprt_switch_put(data->xps);
2734 kfree(data);
2735}
2736
Trond Myklebustce272302016-07-24 17:06:28 -04002737static const struct rpc_call_ops rpc_cb_add_xprt_call_ops = {
Trond Myklebust7f554892016-01-30 23:43:35 -05002738 .rpc_call_done = rpc_cb_add_xprt_done,
2739 .rpc_release = rpc_cb_add_xprt_release,
2740};
2741
2742/**
2743 * rpc_clnt_test_and_add_xprt - Test and add a new transport to a rpc_clnt
2744 * @clnt: pointer to struct rpc_clnt
2745 * @xps: pointer to struct rpc_xprt_switch,
2746 * @xprt: pointer struct rpc_xprt
2747 * @dummy: unused
2748 */
2749int rpc_clnt_test_and_add_xprt(struct rpc_clnt *clnt,
2750 struct rpc_xprt_switch *xps, struct rpc_xprt *xprt,
2751 void *dummy)
2752{
2753 struct rpc_cb_add_xprt_calldata *data;
Trond Myklebust7f554892016-01-30 23:43:35 -05002754 struct rpc_task *task;
2755
2756 data = kmalloc(sizeof(*data), GFP_NOFS);
2757 if (!data)
2758 return -ENOMEM;
2759 data->xps = xprt_switch_get(xps);
2760 data->xprt = xprt_get(xprt);
Trond Myklebust612b41f2017-04-27 08:50:51 -04002761 if (rpc_xprt_switch_has_addr(data->xps, (struct sockaddr *)&xprt->addr)) {
2762 rpc_cb_add_xprt_release(data);
2763 goto success;
2764 }
Trond Myklebust7f554892016-01-30 23:43:35 -05002765
Chuck Lever841a2ed2020-05-12 17:13:55 -04002766 task = rpc_call_null_helper(clnt, xprt, NULL, RPC_TASK_ASYNC,
Trond Myklebust7f554892016-01-30 23:43:35 -05002767 &rpc_cb_add_xprt_call_ops, data);
Xiyu Yangefe57fd2020-04-20 23:14:19 +08002768
Trond Myklebust7f554892016-01-30 23:43:35 -05002769 rpc_put_task(task);
Trond Myklebust612b41f2017-04-27 08:50:51 -04002770success:
Trond Myklebust7f554892016-01-30 23:43:35 -05002771 return 1;
2772}
2773EXPORT_SYMBOL_GPL(rpc_clnt_test_and_add_xprt);
2774
2775/**
Andy Adamsonfda0ab42016-09-09 09:22:26 -04002776 * rpc_clnt_setup_test_and_add_xprt()
2777 *
2778 * This is an rpc_clnt_add_xprt setup() function which returns 1 so:
2779 * 1) caller of the test function must dereference the rpc_xprt_switch
2780 * and the rpc_xprt.
2781 * 2) test function must call rpc_xprt_switch_add_xprt, usually in
2782 * the rpc_call_done routine.
2783 *
2784 * Upon success (return of 1), the test function adds the new
2785 * transport to the rpc_clnt xprt switch
2786 *
2787 * @clnt: struct rpc_clnt to get the new transport
2788 * @xps: the rpc_xprt_switch to hold the new transport
2789 * @xprt: the rpc_xprt to test
2790 * @data: a struct rpc_add_xprt_test pointer that holds the test function
2791 * and test function call data
2792 */
2793int rpc_clnt_setup_test_and_add_xprt(struct rpc_clnt *clnt,
2794 struct rpc_xprt_switch *xps,
2795 struct rpc_xprt *xprt,
2796 void *data)
2797{
Andy Adamsonfda0ab42016-09-09 09:22:26 -04002798 struct rpc_task *task;
2799 struct rpc_add_xprt_test *xtest = (struct rpc_add_xprt_test *)data;
2800 int status = -EADDRINUSE;
2801
2802 xprt = xprt_get(xprt);
2803 xprt_switch_get(xps);
2804
2805 if (rpc_xprt_switch_has_addr(xps, (struct sockaddr *)&xprt->addr))
2806 goto out_err;
2807
2808 /* Test the connection */
Chuck Lever841a2ed2020-05-12 17:13:55 -04002809 task = rpc_call_null_helper(clnt, xprt, NULL, 0, NULL, NULL);
Andy Adamsonfda0ab42016-09-09 09:22:26 -04002810 if (IS_ERR(task)) {
2811 status = PTR_ERR(task);
2812 goto out_err;
2813 }
2814 status = task->tk_status;
2815 rpc_put_task(task);
2816
2817 if (status < 0)
2818 goto out_err;
2819
2820 /* rpc_xprt_switch and rpc_xprt are deferrenced by add_xprt_test() */
2821 xtest->add_xprt_test(clnt, xprt, xtest->data);
2822
Santosh kumar pradhan10e037d2018-12-19 12:29:57 +05302823 xprt_put(xprt);
2824 xprt_switch_put(xps);
2825
Andy Adamsonfda0ab42016-09-09 09:22:26 -04002826 /* so that rpc_clnt_add_xprt does not call rpc_xprt_switch_add_xprt */
2827 return 1;
2828out_err:
2829 xprt_put(xprt);
2830 xprt_switch_put(xps);
2831 pr_info("RPC: rpc_clnt_test_xprt failed: %d addr %s not added\n",
2832 status, xprt->address_strings[RPC_DISPLAY_ADDR]);
2833 return status;
2834}
2835EXPORT_SYMBOL_GPL(rpc_clnt_setup_test_and_add_xprt);
2836
2837/**
Trond Myklebust7f554892016-01-30 23:43:35 -05002838 * rpc_clnt_add_xprt - Add a new transport to a rpc_clnt
2839 * @clnt: pointer to struct rpc_clnt
2840 * @xprtargs: pointer to struct xprt_create
2841 * @setup: callback to test and/or set up the connection
2842 * @data: pointer to setup function data
2843 *
2844 * Creates a new transport using the parameters set in args and
2845 * adds it to clnt.
2846 * If ping is set, then test that connectivity succeeds before
2847 * adding the new transport.
2848 *
2849 */
2850int rpc_clnt_add_xprt(struct rpc_clnt *clnt,
2851 struct xprt_create *xprtargs,
2852 int (*setup)(struct rpc_clnt *,
2853 struct rpc_xprt_switch *,
2854 struct rpc_xprt *,
2855 void *),
2856 void *data)
2857{
2858 struct rpc_xprt_switch *xps;
2859 struct rpc_xprt *xprt;
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002860 unsigned long connect_timeout;
Trond Myklebust3851f1c2016-08-04 00:08:45 -04002861 unsigned long reconnect_timeout;
Trond Myklebuste6237b62019-10-17 11:13:54 -04002862 unsigned char resvport, reuseport;
Trond Myklebust7f554892016-01-30 23:43:35 -05002863 int ret = 0;
2864
2865 rcu_read_lock();
2866 xps = xprt_switch_get(rcu_dereference(clnt->cl_xpi.xpi_xpswitch));
2867 xprt = xprt_iter_xprt(&clnt->cl_xpi);
2868 if (xps == NULL || xprt == NULL) {
2869 rcu_read_unlock();
Lin Yib9622612019-06-10 10:16:56 +08002870 xprt_switch_put(xps);
Trond Myklebust7f554892016-01-30 23:43:35 -05002871 return -EAGAIN;
2872 }
2873 resvport = xprt->resvport;
Trond Myklebuste6237b62019-10-17 11:13:54 -04002874 reuseport = xprt->reuseport;
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002875 connect_timeout = xprt->connect_timeout;
Trond Myklebust3851f1c2016-08-04 00:08:45 -04002876 reconnect_timeout = xprt->max_reconnect_timeout;
Trond Myklebust7f554892016-01-30 23:43:35 -05002877 rcu_read_unlock();
2878
2879 xprt = xprt_create_transport(xprtargs);
2880 if (IS_ERR(xprt)) {
2881 ret = PTR_ERR(xprt);
2882 goto out_put_switch;
2883 }
2884 xprt->resvport = resvport;
Trond Myklebuste6237b62019-10-17 11:13:54 -04002885 xprt->reuseport = reuseport;
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002886 if (xprt->ops->set_connect_timeout != NULL)
2887 xprt->ops->set_connect_timeout(xprt,
2888 connect_timeout,
2889 reconnect_timeout);
Trond Myklebust7f554892016-01-30 23:43:35 -05002890
2891 rpc_xprt_switch_set_roundrobin(xps);
2892 if (setup) {
2893 ret = setup(clnt, xps, xprt, data);
2894 if (ret != 0)
2895 goto out_put_xprt;
2896 }
2897 rpc_xprt_switch_add_xprt(xps, xprt);
2898out_put_xprt:
2899 xprt_put(xprt);
2900out_put_switch:
2901 xprt_switch_put(xps);
2902 return ret;
2903}
2904EXPORT_SYMBOL_GPL(rpc_clnt_add_xprt);
2905
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002906struct connect_timeout_data {
2907 unsigned long connect_timeout;
2908 unsigned long reconnect_timeout;
2909};
2910
Trond Myklebust8d480322016-08-05 19:03:31 -04002911static int
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002912rpc_xprt_set_connect_timeout(struct rpc_clnt *clnt,
Trond Myklebust8d480322016-08-05 19:03:31 -04002913 struct rpc_xprt *xprt,
2914 void *data)
2915{
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002916 struct connect_timeout_data *timeo = data;
Trond Myklebust8d480322016-08-05 19:03:31 -04002917
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002918 if (xprt->ops->set_connect_timeout)
2919 xprt->ops->set_connect_timeout(xprt,
2920 timeo->connect_timeout,
2921 timeo->reconnect_timeout);
Trond Myklebust8d480322016-08-05 19:03:31 -04002922 return 0;
2923}
2924
2925void
Trond Myklebust26ae1022017-02-08 11:17:55 -05002926rpc_set_connect_timeout(struct rpc_clnt *clnt,
2927 unsigned long connect_timeout,
2928 unsigned long reconnect_timeout)
Trond Myklebust8d480322016-08-05 19:03:31 -04002929{
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002930 struct connect_timeout_data timeout = {
Trond Myklebust26ae1022017-02-08 11:17:55 -05002931 .connect_timeout = connect_timeout,
2932 .reconnect_timeout = reconnect_timeout,
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002933 };
Trond Myklebust8d480322016-08-05 19:03:31 -04002934 rpc_clnt_iterate_for_each_xprt(clnt,
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002935 rpc_xprt_set_connect_timeout,
2936 &timeout);
Trond Myklebust8d480322016-08-05 19:03:31 -04002937}
Trond Myklebust26ae1022017-02-08 11:17:55 -05002938EXPORT_SYMBOL_GPL(rpc_set_connect_timeout);
Trond Myklebust8d480322016-08-05 19:03:31 -04002939
Andy Adamson3b58a8a2016-09-09 09:22:23 -04002940void rpc_clnt_xprt_switch_put(struct rpc_clnt *clnt)
2941{
Anna Schumakerbb29dd82016-10-26 10:33:31 -04002942 rcu_read_lock();
Andy Adamson3b58a8a2016-09-09 09:22:23 -04002943 xprt_switch_put(rcu_dereference(clnt->cl_xpi.xpi_xpswitch));
Anna Schumakerbb29dd82016-10-26 10:33:31 -04002944 rcu_read_unlock();
Andy Adamson3b58a8a2016-09-09 09:22:23 -04002945}
2946EXPORT_SYMBOL_GPL(rpc_clnt_xprt_switch_put);
2947
Andy Adamsondd691712016-09-09 09:22:24 -04002948void rpc_clnt_xprt_switch_add_xprt(struct rpc_clnt *clnt, struct rpc_xprt *xprt)
2949{
Anna Schumakerbb29dd82016-10-26 10:33:31 -04002950 rcu_read_lock();
Andy Adamsondd691712016-09-09 09:22:24 -04002951 rpc_xprt_switch_add_xprt(rcu_dereference(clnt->cl_xpi.xpi_xpswitch),
2952 xprt);
Anna Schumakerbb29dd82016-10-26 10:33:31 -04002953 rcu_read_unlock();
Andy Adamsondd691712016-09-09 09:22:24 -04002954}
2955EXPORT_SYMBOL_GPL(rpc_clnt_xprt_switch_add_xprt);
2956
Andy Adamson39e5d2d2016-09-09 09:22:25 -04002957bool rpc_clnt_xprt_switch_has_addr(struct rpc_clnt *clnt,
2958 const struct sockaddr *sap)
2959{
2960 struct rpc_xprt_switch *xps;
2961 bool ret;
2962
Andy Adamson39e5d2d2016-09-09 09:22:25 -04002963 rcu_read_lock();
Anna Schumakerbb29dd82016-10-26 10:33:31 -04002964 xps = rcu_dereference(clnt->cl_xpi.xpi_xpswitch);
Andy Adamson39e5d2d2016-09-09 09:22:25 -04002965 ret = rpc_xprt_switch_has_addr(xps, sap);
2966 rcu_read_unlock();
2967 return ret;
2968}
2969EXPORT_SYMBOL_GPL(rpc_clnt_xprt_switch_has_addr);
2970
Jeff Laytonf895b252014-11-17 16:58:04 -05002971#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
Chuck Lever68a23ee2008-05-21 17:09:26 -04002972static void rpc_show_header(void)
2973{
Chuck Levercb3997b2008-05-21 17:09:41 -04002974 printk(KERN_INFO "-pid- flgs status -client- --rqstp- "
2975 "-timeout ---ops--\n");
Chuck Lever68a23ee2008-05-21 17:09:26 -04002976}
2977
Chuck Lever38e886e2008-05-21 17:09:33 -04002978static void rpc_show_task(const struct rpc_clnt *clnt,
2979 const struct rpc_task *task)
2980{
2981 const char *rpc_waitq = "none";
Chuck Lever38e886e2008-05-21 17:09:33 -04002982
2983 if (RPC_IS_QUEUED(task))
2984 rpc_waitq = rpc_qname(task->tk_waitqueue);
2985
Joe Perchesb3bceda2010-12-21 10:52:24 -05002986 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 -04002987 task->tk_pid, task->tk_flags, task->tk_status,
Trond Myklebust5efd1872019-04-07 13:58:50 -04002988 clnt, task->tk_rqstp, rpc_task_timeout(task), task->tk_ops,
Trond Myklebust55909f22013-08-23 11:48:15 -04002989 clnt->cl_program->name, clnt->cl_vers, rpc_proc_name(task),
Joe Perchesb3bceda2010-12-21 10:52:24 -05002990 task->tk_action, rpc_waitq);
Chuck Lever38e886e2008-05-21 17:09:33 -04002991}
2992
Stanislav Kinsbursky70abc492012-01-12 22:07:51 +04002993void rpc_show_tasks(struct net *net)
Trond Myklebust188fef12007-06-16 14:18:40 -04002994{
2995 struct rpc_clnt *clnt;
Chuck Lever38e886e2008-05-21 17:09:33 -04002996 struct rpc_task *task;
Chuck Lever68a23ee2008-05-21 17:09:26 -04002997 int header = 0;
Stanislav Kinsbursky70abc492012-01-12 22:07:51 +04002998 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
Trond Myklebust188fef12007-06-16 14:18:40 -04002999
Stanislav Kinsbursky70abc492012-01-12 22:07:51 +04003000 spin_lock(&sn->rpc_client_lock);
3001 list_for_each_entry(clnt, &sn->all_clients, cl_clients) {
Trond Myklebust188fef12007-06-16 14:18:40 -04003002 spin_lock(&clnt->cl_lock);
Chuck Lever38e886e2008-05-21 17:09:33 -04003003 list_for_each_entry(task, &clnt->cl_tasks, tk_task) {
Chuck Lever68a23ee2008-05-21 17:09:26 -04003004 if (!header) {
3005 rpc_show_header();
3006 header++;
3007 }
Chuck Lever38e886e2008-05-21 17:09:33 -04003008 rpc_show_task(clnt, task);
Trond Myklebust188fef12007-06-16 14:18:40 -04003009 }
3010 spin_unlock(&clnt->cl_lock);
3011 }
Stanislav Kinsbursky70abc492012-01-12 22:07:51 +04003012 spin_unlock(&sn->rpc_client_lock);
Trond Myklebust188fef12007-06-16 14:18:40 -04003013}
3014#endif
Jeff Layton3c87ef62015-06-03 16:14:25 -04003015
3016#if IS_ENABLED(CONFIG_SUNRPC_SWAP)
Trond Myklebust15001e52016-01-30 20:05:34 -05003017static int
3018rpc_clnt_swap_activate_callback(struct rpc_clnt *clnt,
3019 struct rpc_xprt *xprt,
3020 void *dummy)
3021{
3022 return xprt_enable_swap(xprt);
3023}
3024
Jeff Layton3c87ef62015-06-03 16:14:25 -04003025int
3026rpc_clnt_swap_activate(struct rpc_clnt *clnt)
3027{
Trond Myklebust15001e52016-01-30 20:05:34 -05003028 if (atomic_inc_return(&clnt->cl_swapper) == 1)
3029 return rpc_clnt_iterate_for_each_xprt(clnt,
3030 rpc_clnt_swap_activate_callback, NULL);
3031 return 0;
Jeff Layton3c87ef62015-06-03 16:14:25 -04003032}
3033EXPORT_SYMBOL_GPL(rpc_clnt_swap_activate);
3034
Trond Myklebust15001e52016-01-30 20:05:34 -05003035static int
3036rpc_clnt_swap_deactivate_callback(struct rpc_clnt *clnt,
3037 struct rpc_xprt *xprt,
3038 void *dummy)
3039{
3040 xprt_disable_swap(xprt);
3041 return 0;
3042}
3043
Jeff Layton3c87ef62015-06-03 16:14:25 -04003044void
3045rpc_clnt_swap_deactivate(struct rpc_clnt *clnt)
3046{
Trond Myklebust15001e52016-01-30 20:05:34 -05003047 if (atomic_dec_if_positive(&clnt->cl_swapper) == 0)
3048 rpc_clnt_iterate_for_each_xprt(clnt,
3049 rpc_clnt_swap_deactivate_callback, NULL);
Jeff Layton3c87ef62015-06-03 16:14:25 -04003050}
3051EXPORT_SYMBOL_GPL(rpc_clnt_swap_deactivate);
3052#endif /* CONFIG_SUNRPC_SWAP */