blob: c1f1afabd024230739bb1ec38747e433814cb39c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Chuck Lever55aa4f52005-08-11 16:25:47 -04002 * linux/net/sunrpc/clnt.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * This file contains the high-level RPC interface.
5 * It is modeled as a finite state machine to support both synchronous
6 * and asynchronous requests.
7 *
8 * - RPC header generation and argument serialization.
9 * - Credential refresh.
10 * - TCP connect handling.
11 * - Retry of operation when it is suspected the operation failed because
12 * of uid squashing on the server, or when the credentials were stale
13 * and need to be refreshed, or when a packet was damaged in transit.
14 * This may be have to be moved to the VFS layer.
15 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 * Copyright (C) 1992,1993 Rick Sladkey <jrs@world.std.com>
17 * Copyright (C) 1995,1996 Olaf Kirch <okir@monad.swb.de>
18 */
19
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
21#include <linux/module.h>
22#include <linux/types.h>
Chuck Levercb3997b2008-05-21 17:09:41 -040023#include <linux/kallsyms.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/mm.h>
Trond Myklebust23ac6582009-08-09 15:14:25 -040025#include <linux/namei.h>
26#include <linux/mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/slab.h>
Trond Myklebust40b00b6b2013-10-17 14:12:23 -040028#include <linux/rcupdate.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/utsname.h>
Chuck Lever11c556b2006-03-20 13:44:22 -050030#include <linux/workqueue.h>
Chuck Lever176e21e2011-05-09 15:22:44 -040031#include <linux/in.h>
Chuck Lever510deb02007-12-10 14:56:24 -050032#include <linux/in6.h>
Chuck Lever176e21e2011-05-09 15:22:44 -040033#include <linux/un.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
35#include <linux/sunrpc/clnt.h>
Jeff Layton59766872013-02-04 12:50:00 -050036#include <linux/sunrpc/addr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/sunrpc/rpc_pipe_fs.h>
Chuck Lever11c556b2006-03-20 13:44:22 -050038#include <linux/sunrpc/metrics.h>
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -040039#include <linux/sunrpc/bc_xprt.h>
Steve Dickson5753cba2012-02-06 10:08:08 -050040#include <trace/events/sunrpc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -040042#include "sunrpc.h"
Stanislav Kinsbursky70abc492012-01-12 22:07:51 +040043#include "netns.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Jeff Laytonf895b252014-11-17 16:58:04 -050045#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046# define RPCDBG_FACILITY RPCDBG_CALL
47#endif
48
Chuck Lever46121cf2007-01-31 12:14:08 -050049#define dprint_status(t) \
50 dprintk("RPC: %5u %s (status %d)\n", t->tk_pid, \
Harvey Harrison0dc47872008-03-05 20:47:47 -080051 __func__, t->tk_status)
Chuck Lever46121cf2007-01-31 12:14:08 -050052
Trond Myklebust188fef12007-06-16 14:18:40 -040053/*
54 * All RPC clients are linked into this list
55 */
Trond Myklebust188fef12007-06-16 14:18:40 -040056
Linus Torvalds1da177e2005-04-16 15:20:36 -070057static DECLARE_WAIT_QUEUE_HEAD(destroy_wait);
58
59
60static void call_start(struct rpc_task *task);
61static void call_reserve(struct rpc_task *task);
62static void call_reserveresult(struct rpc_task *task);
63static void call_allocate(struct rpc_task *task);
Trond Myklebust762e4e62018-08-24 16:28:28 -040064static void call_encode(struct rpc_task *task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065static void call_decode(struct rpc_task *task);
66static void call_bind(struct rpc_task *task);
Chuck Leverda351872005-08-11 16:25:11 -040067static void call_bind_status(struct rpc_task *task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068static void call_transmit(struct rpc_task *task);
69static void call_status(struct rpc_task *task);
Trond Myklebust940e3312005-11-09 21:45:24 -050070static void call_transmit_status(struct rpc_task *task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071static void call_refresh(struct rpc_task *task);
72static void call_refreshresult(struct rpc_task *task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073static void call_connect(struct rpc_task *task);
74static void call_connect_status(struct rpc_task *task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Chuck Levere8680a22019-02-11 11:24:48 -050076static int rpc_encode_header(struct rpc_task *task,
77 struct xdr_stream *xdr);
Chuck Levera0584ee2019-02-11 11:24:58 -050078static int rpc_decode_header(struct rpc_task *task,
79 struct xdr_stream *xdr);
Chuck Levercaabea82009-12-03 15:58:56 -050080static int rpc_ping(struct rpc_clnt *clnt);
Trond Myklebust7b3fef82019-03-07 14:10:32 -050081static void rpc_check_timeout(struct rpc_task *task);
Trond Myklebust64c91a12007-06-23 10:17:16 -040082
Trond Myklebust188fef12007-06-16 14:18:40 -040083static void rpc_register_client(struct rpc_clnt *clnt)
84{
Trond Myklebust2446ab62012-03-01 17:00:56 -050085 struct net *net = rpc_net_ns(clnt);
86 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
Stanislav Kinsbursky70abc492012-01-12 22:07:51 +040087
88 spin_lock(&sn->rpc_client_lock);
89 list_add(&clnt->cl_clients, &sn->all_clients);
90 spin_unlock(&sn->rpc_client_lock);
Trond Myklebust188fef12007-06-16 14:18:40 -040091}
92
93static void rpc_unregister_client(struct rpc_clnt *clnt)
94{
Trond Myklebust2446ab62012-03-01 17:00:56 -050095 struct net *net = rpc_net_ns(clnt);
96 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
Stanislav Kinsbursky70abc492012-01-12 22:07:51 +040097
98 spin_lock(&sn->rpc_client_lock);
Trond Myklebust188fef12007-06-16 14:18:40 -040099 list_del(&clnt->cl_clients);
Stanislav Kinsbursky70abc492012-01-12 22:07:51 +0400100 spin_unlock(&sn->rpc_client_lock);
Trond Myklebust188fef12007-06-16 14:18:40 -0400101}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400103static void __rpc_clnt_remove_pipedir(struct rpc_clnt *clnt)
104{
Trond Myklebustc36dcfe12013-08-26 17:44:26 -0400105 rpc_remove_client_dir(clnt);
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400106}
107
108static void rpc_clnt_remove_pipedir(struct rpc_clnt *clnt)
109{
Trond Myklebust2446ab62012-03-01 17:00:56 -0500110 struct net *net = rpc_net_ns(clnt);
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400111 struct super_block *pipefs_sb;
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400112
Trond Myklebust2446ab62012-03-01 17:00:56 -0500113 pipefs_sb = rpc_get_sb_net(net);
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400114 if (pipefs_sb) {
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400115 __rpc_clnt_remove_pipedir(clnt);
Trond Myklebust2446ab62012-03-01 17:00:56 -0500116 rpc_put_sb_net(net);
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400117 }
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400118}
119
120static struct dentry *rpc_setup_pipedir_sb(struct super_block *sb,
Trond Myklebust41b6b4d2013-08-23 13:03:50 -0400121 struct rpc_clnt *clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
Trond Myklebustf1345852005-09-23 11:08:25 -0400123 static uint32_t clntid;
Trond Myklebust41b6b4d2013-08-23 13:03:50 -0400124 const char *dir_name = clnt->cl_program->pipe_dir_name;
Trond Myklebust23ac6582009-08-09 15:14:25 -0400125 char name[15];
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400126 struct dentry *dir, *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400128 dir = rpc_d_lookup_sb(sb, dir_name);
Weston Andros Adamson922eeac2012-10-23 10:43:25 -0400129 if (dir == NULL) {
130 pr_info("RPC: pipefs directory doesn't exist: %s\n", dir_name);
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400131 return dir;
Weston Andros Adamson922eeac2012-10-23 10:43:25 -0400132 }
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400133 for (;;) {
Al Viroa95e6912013-07-14 16:43:54 +0400134 snprintf(name, sizeof(name), "clnt%x", (unsigned int)clntid++);
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400135 name[sizeof(name) - 1] = '\0';
Al Viroa95e6912013-07-14 16:43:54 +0400136 dentry = rpc_create_client_dir(dir, name, clnt);
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400137 if (!IS_ERR(dentry))
138 break;
Al Viroa95e6912013-07-14 16:43:54 +0400139 if (dentry == ERR_PTR(-EEXIST))
140 continue;
141 printk(KERN_INFO "RPC: Couldn't create pipefs entry"
142 " %s/%s, error %ld\n",
143 dir_name, name, PTR_ERR(dentry));
144 break;
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400145 }
146 dput(dir);
147 return dentry;
148}
149
150static int
Trond Myklebust41b6b4d2013-08-23 13:03:50 -0400151rpc_setup_pipedir(struct super_block *pipefs_sb, struct rpc_clnt *clnt)
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400152{
Stanislav Kinsbursky30507f52012-01-11 19:18:42 +0400153 struct dentry *dentry;
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400154
Trond Myklebustc36dcfe12013-08-26 17:44:26 -0400155 if (clnt->cl_program->pipe_dir_name != NULL) {
156 dentry = rpc_setup_pipedir_sb(pipefs_sb, clnt);
157 if (IS_ERR(dentry))
158 return PTR_ERR(dentry);
159 }
Trond Myklebust23ac6582009-08-09 15:14:25 -0400160 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161}
162
Trond Myklebust41b6b4d2013-08-23 13:03:50 -0400163static int rpc_clnt_skip_event(struct rpc_clnt *clnt, unsigned long event)
Stanislav Kinsburskyea8cfa02012-04-27 13:00:17 +0400164{
Trond Myklebust41b6b4d2013-08-23 13:03:50 -0400165 if (clnt->cl_program->pipe_dir_name == NULL)
Stanislav Kinsburskyea8cfa02012-04-27 13:00:17 +0400166 return 1;
Trond Myklebust41b6b4d2013-08-23 13:03:50 -0400167
Trond Myklebustc36dcfe12013-08-26 17:44:26 -0400168 switch (event) {
169 case RPC_PIPEFS_MOUNT:
170 if (clnt->cl_pipedir_objects.pdh_dentry != NULL)
171 return 1;
172 if (atomic_read(&clnt->cl_count) == 0)
173 return 1;
174 break;
175 case RPC_PIPEFS_UMOUNT:
176 if (clnt->cl_pipedir_objects.pdh_dentry == NULL)
177 return 1;
178 break;
179 }
Stanislav Kinsburskyea8cfa02012-04-27 13:00:17 +0400180 return 0;
181}
182
183static int __rpc_clnt_handle_event(struct rpc_clnt *clnt, unsigned long event,
184 struct super_block *sb)
Stanislav Kinsbursky80df9d22012-01-11 19:18:17 +0400185{
186 struct dentry *dentry;
Stanislav Kinsbursky80df9d22012-01-11 19:18:17 +0400187
188 switch (event) {
189 case RPC_PIPEFS_MOUNT:
Trond Myklebust41b6b4d2013-08-23 13:03:50 -0400190 dentry = rpc_setup_pipedir_sb(sb, clnt);
Weston Andros Adamson922eeac2012-10-23 10:43:25 -0400191 if (!dentry)
192 return -ENOENT;
Stanislav Kinsbursky80df9d22012-01-11 19:18:17 +0400193 if (IS_ERR(dentry))
194 return PTR_ERR(dentry);
Stanislav Kinsbursky80df9d22012-01-11 19:18:17 +0400195 break;
196 case RPC_PIPEFS_UMOUNT:
197 __rpc_clnt_remove_pipedir(clnt);
198 break;
199 default:
200 printk(KERN_ERR "%s: unknown event: %ld\n", __func__, event);
201 return -ENOTSUPP;
202 }
Amitoj Kaur Chawla2813b622016-08-08 14:43:49 +0530203 return 0;
Stanislav Kinsbursky80df9d22012-01-11 19:18:17 +0400204}
205
Stanislav Kinsburskyea8cfa02012-04-27 13:00:17 +0400206static int __rpc_pipefs_event(struct rpc_clnt *clnt, unsigned long event,
207 struct super_block *sb)
208{
209 int error = 0;
210
211 for (;; clnt = clnt->cl_parent) {
212 if (!rpc_clnt_skip_event(clnt, event))
213 error = __rpc_clnt_handle_event(clnt, event, sb);
214 if (error || clnt == clnt->cl_parent)
215 break;
216 }
217 return error;
218}
219
Stanislav Kinsburskyda3b4622012-02-27 22:05:29 +0400220static struct rpc_clnt *rpc_get_client_for_event(struct net *net, int event)
221{
222 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
223 struct rpc_clnt *clnt;
224
225 spin_lock(&sn->rpc_client_lock);
226 list_for_each_entry(clnt, &sn->all_clients, cl_clients) {
Stanislav Kinsburskyea8cfa02012-04-27 13:00:17 +0400227 if (rpc_clnt_skip_event(clnt, event))
Stanislav Kinsburskyda3b4622012-02-27 22:05:29 +0400228 continue;
Stanislav Kinsburskyda3b4622012-02-27 22:05:29 +0400229 spin_unlock(&sn->rpc_client_lock);
230 return clnt;
231 }
232 spin_unlock(&sn->rpc_client_lock);
233 return NULL;
234}
235
Stanislav Kinsbursky80df9d22012-01-11 19:18:17 +0400236static int rpc_pipefs_event(struct notifier_block *nb, unsigned long event,
237 void *ptr)
238{
239 struct super_block *sb = ptr;
240 struct rpc_clnt *clnt;
241 int error = 0;
Stanislav Kinsbursky80df9d22012-01-11 19:18:17 +0400242
Stanislav Kinsburskyda3b4622012-02-27 22:05:29 +0400243 while ((clnt = rpc_get_client_for_event(sb->s_fs_info, event))) {
Stanislav Kinsbursky80df9d22012-01-11 19:18:17 +0400244 error = __rpc_pipefs_event(clnt, event, sb);
245 if (error)
246 break;
247 }
Stanislav Kinsbursky80df9d22012-01-11 19:18:17 +0400248 return error;
249}
250
251static struct notifier_block rpc_clients_block = {
252 .notifier_call = rpc_pipefs_event,
Stanislav Kinsburskyeee173252012-01-10 16:13:19 +0400253 .priority = SUNRPC_PIPEFS_RPC_PRIO,
Stanislav Kinsbursky80df9d22012-01-11 19:18:17 +0400254};
255
256int rpc_clients_notifier_register(void)
257{
258 return rpc_pipefs_notifier_register(&rpc_clients_block);
259}
260
261void rpc_clients_notifier_unregister(void)
262{
263 return rpc_pipefs_notifier_unregister(&rpc_clients_block);
264}
265
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400266static struct rpc_xprt *rpc_clnt_set_transport(struct rpc_clnt *clnt,
267 struct rpc_xprt *xprt,
268 const struct rpc_timeout *timeout)
269{
270 struct rpc_xprt *old;
271
272 spin_lock(&clnt->cl_lock);
Trond Myklebust34751b92013-10-28 16:42:44 -0400273 old = rcu_dereference_protected(clnt->cl_xprt,
274 lockdep_is_held(&clnt->cl_lock));
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400275
276 if (!xprt_bound(xprt))
277 clnt->cl_autobind = 1;
278
279 clnt->cl_timeout = timeout;
280 rcu_assign_pointer(clnt->cl_xprt, xprt);
281 spin_unlock(&clnt->cl_lock);
282
283 return old;
284}
285
Trond Myklebustcbbb3442012-04-30 11:52:40 -0400286static void rpc_clnt_set_nodename(struct rpc_clnt *clnt, const char *nodename)
287{
Trond Myklebust03a9a422015-01-30 18:12:28 -0500288 clnt->cl_nodelen = strlcpy(clnt->cl_nodename,
289 nodename, sizeof(clnt->cl_nodename));
Trond Myklebustcbbb3442012-04-30 11:52:40 -0400290}
291
Chuck Leverd746e542013-10-17 14:12:17 -0400292static int rpc_client_register(struct rpc_clnt *clnt,
293 rpc_authflavor_t pseudoflavor,
294 const char *client_name)
Stanislav Kinsburskye73f4cc2013-06-24 11:52:52 +0400295{
Trond Myklebustc2190662013-08-26 19:23:04 -0400296 struct rpc_auth_create_args auth_args = {
Chuck Leverd746e542013-10-17 14:12:17 -0400297 .pseudoflavor = pseudoflavor,
298 .target_name = client_name,
Trond Myklebustc2190662013-08-26 19:23:04 -0400299 };
Stanislav Kinsburskye73f4cc2013-06-24 11:52:52 +0400300 struct rpc_auth *auth;
301 struct net *net = rpc_net_ns(clnt);
302 struct super_block *pipefs_sb;
Trond Myklebusteeee2452013-07-10 15:33:01 -0400303 int err;
Stanislav Kinsburskye73f4cc2013-06-24 11:52:52 +0400304
Jeff Laytonf9c72d12015-03-31 12:03:28 -0400305 rpc_clnt_debugfs_register(clnt);
Jeff Laytonb4b9d2c2014-11-26 14:44:43 -0500306
Stanislav Kinsburskye73f4cc2013-06-24 11:52:52 +0400307 pipefs_sb = rpc_get_sb_net(net);
308 if (pipefs_sb) {
Trond Myklebust41b6b4d2013-08-23 13:03:50 -0400309 err = rpc_setup_pipedir(pipefs_sb, clnt);
Stanislav Kinsburskye73f4cc2013-06-24 11:52:52 +0400310 if (err)
311 goto out;
312 }
313
Trond Myklebusteeee2452013-07-10 15:33:01 -0400314 rpc_register_client(clnt);
315 if (pipefs_sb)
316 rpc_put_sb_net(net);
317
Trond Myklebustc2190662013-08-26 19:23:04 -0400318 auth = rpcauth_create(&auth_args, clnt);
Stanislav Kinsburskye73f4cc2013-06-24 11:52:52 +0400319 if (IS_ERR(auth)) {
320 dprintk("RPC: Couldn't create auth handle (flavor %u)\n",
Chuck Leverd746e542013-10-17 14:12:17 -0400321 pseudoflavor);
Stanislav Kinsburskye73f4cc2013-06-24 11:52:52 +0400322 err = PTR_ERR(auth);
323 goto err_auth;
324 }
Trond Myklebusteeee2452013-07-10 15:33:01 -0400325 return 0;
326err_auth:
327 pipefs_sb = rpc_get_sb_net(net);
Trond Myklebust1540c5d2013-07-14 22:57:50 -0400328 rpc_unregister_client(clnt);
Trond Myklebusteeee2452013-07-10 15:33:01 -0400329 __rpc_clnt_remove_pipedir(clnt);
Stanislav Kinsburskye73f4cc2013-06-24 11:52:52 +0400330out:
331 if (pipefs_sb)
332 rpc_put_sb_net(net);
Jeff Laytonb4b9d2c2014-11-26 14:44:43 -0500333 rpc_clnt_debugfs_unregister(clnt);
Stanislav Kinsburskye73f4cc2013-06-24 11:52:52 +0400334 return err;
Stanislav Kinsburskye73f4cc2013-06-24 11:52:52 +0400335}
336
Trond Myklebust2f048db2013-09-04 21:51:44 -0400337static DEFINE_IDA(rpc_clids);
338
Kinglong Meec929ea02017-01-20 16:48:39 +0800339void rpc_cleanup_clids(void)
340{
341 ida_destroy(&rpc_clids);
342}
343
Trond Myklebust2f048db2013-09-04 21:51:44 -0400344static int rpc_alloc_clid(struct rpc_clnt *clnt)
345{
346 int clid;
347
348 clid = ida_simple_get(&rpc_clids, 0, 0, GFP_KERNEL);
349 if (clid < 0)
350 return clid;
351 clnt->cl_clid = clid;
352 return 0;
353}
354
355static void rpc_free_clid(struct rpc_clnt *clnt)
356{
357 ida_simple_remove(&rpc_clids, clnt->cl_clid);
358}
359
Trond Myklebust280ebcf2013-09-02 13:28:04 -0400360static struct rpc_clnt * rpc_new_client(const struct rpc_create_args *args,
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500361 struct rpc_xprt_switch *xps,
Trond Myklebust280ebcf2013-09-02 13:28:04 -0400362 struct rpc_xprt *xprt,
363 struct rpc_clnt *parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364{
Trond Myklebusta613fa12012-01-20 13:53:56 -0500365 const struct rpc_program *program = args->program;
366 const struct rpc_version *version;
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400367 struct rpc_clnt *clnt = NULL;
368 const struct rpc_timeout *timeout;
Trond Myklebust03a9a422015-01-30 18:12:28 -0500369 const char *nodename = args->nodename;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 int err;
Chuck Lever06b8d252007-09-11 18:00:20 -0400371
372 /* sanity check the name before trying to print it */
Chuck Lever46121cf2007-01-31 12:14:08 -0500373 dprintk("RPC: creating %s client for %s (xprt %p)\n",
Trond Myklebust698b6d02007-12-20 16:03:53 -0500374 program->name, args->servername, xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Trond Myklebust4ada5392007-06-14 17:26:17 -0400376 err = rpciod_up();
377 if (err)
378 goto out_no_rpciod;
Trond Myklebust698b6d02007-12-20 16:03:53 -0500379
Trond Myklebustf05c1242013-04-05 14:13:21 -0400380 err = -EINVAL;
Trond Myklebust698b6d02007-12-20 16:03:53 -0500381 if (args->version >= program->nrvers)
382 goto out_err;
383 version = program->version[args->version];
384 if (version == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 goto out_err;
386
387 err = -ENOMEM;
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700388 clnt = kzalloc(sizeof(*clnt), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 if (!clnt)
390 goto out_err;
Trond Myklebust280ebcf2013-09-02 13:28:04 -0400391 clnt->cl_parent = parent ? : clnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
Trond Myklebust2f048db2013-09-04 21:51:44 -0400393 err = rpc_alloc_clid(clnt);
394 if (err)
395 goto out_no_clid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
Trond Myklebust79caa5f2019-04-24 17:46:42 -0400397 clnt->cl_cred = get_cred(args->cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 clnt->cl_procinfo = version->procs;
399 clnt->cl_maxproc = version->nrprocs;
Benny Halevyd5b337b2008-09-28 09:21:26 +0300400 clnt->cl_prog = args->prognumber ? : program->number;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 clnt->cl_vers = version->number;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 clnt->cl_stats = program->stats;
Chuck Lever11c556b2006-03-20 13:44:22 -0500403 clnt->cl_metrics = rpc_alloc_iostats(clnt);
Trond Myklebust6739ffb2013-08-26 15:38:11 -0400404 rpc_init_pipe_dir_head(&clnt->cl_pipedir_objects);
Trond Myklebust23bf85b2006-11-21 10:40:23 -0500405 err = -ENOMEM;
406 if (clnt->cl_metrics == NULL)
407 goto out_no_stats;
Trond Myklebust3e32a5d2006-11-16 11:37:27 -0500408 clnt->cl_program = program;
Trond Myklebust6529eba2007-06-14 16:40:14 -0400409 INIT_LIST_HEAD(&clnt->cl_tasks);
Trond Myklebust4bef61f2007-06-16 14:17:01 -0400410 spin_lock_init(&clnt->cl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400412 timeout = xprt->timeout;
Trond Myklebustba7392b2007-12-20 16:03:55 -0500413 if (args->timeout != NULL) {
414 memcpy(&clnt->cl_timeout_default, args->timeout,
415 sizeof(clnt->cl_timeout_default));
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400416 timeout = &clnt->cl_timeout_default;
Trond Myklebustba7392b2007-12-20 16:03:55 -0500417 }
418
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400419 rpc_clnt_set_transport(clnt, xprt, timeout);
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500420 xprt_iter_init(&clnt->cl_xpi, xps);
421 xprt_switch_put(xps);
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400422
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 clnt->cl_rtt = &clnt->cl_rtt_default;
Trond Myklebustba7392b2007-12-20 16:03:55 -0500424 rpc_init_rtt(&clnt->cl_rtt_default, clnt->cl_timeout->to_initval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Trond Myklebust006abe82010-09-12 19:55:25 -0400426 atomic_set(&clnt->cl_count, 1);
Trond Myklebust34f52e32007-06-14 16:40:31 -0400427
Trond Myklebust03a9a422015-01-30 18:12:28 -0500428 if (nodename == NULL)
429 nodename = utsname()->nodename;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 /* save the nodename */
Trond Myklebust03a9a422015-01-30 18:12:28 -0500431 rpc_clnt_set_nodename(clnt, nodename);
Stanislav Kinsburskye73f4cc2013-06-24 11:52:52 +0400432
Chuck Leverd746e542013-10-17 14:12:17 -0400433 err = rpc_client_register(clnt, args->authflavor, args->client_name);
Stanislav Kinsburskye73f4cc2013-06-24 11:52:52 +0400434 if (err)
435 goto out_no_path;
Trond Myklebust280ebcf2013-09-02 13:28:04 -0400436 if (parent)
437 atomic_inc(&parent->cl_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 return clnt;
439
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440out_no_path:
Trond Myklebust23bf85b2006-11-21 10:40:23 -0500441 rpc_free_iostats(clnt->cl_metrics);
442out_no_stats:
Trond Myklebust79caa5f2019-04-24 17:46:42 -0400443 put_cred(clnt->cl_cred);
Trond Myklebust2f048db2013-09-04 21:51:44 -0400444 rpc_free_clid(clnt);
445out_no_clid:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 kfree(clnt);
447out_err:
Trond Myklebust4ada5392007-06-14 17:26:17 -0400448 rpciod_down();
449out_no_rpciod:
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500450 xprt_switch_put(xps);
Trond Myklebustf05c1242013-04-05 14:13:21 -0400451 xprt_put(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 return ERR_PTR(err);
453}
454
J. Bruce Fieldsd50039e2016-05-16 17:03:42 -0400455static struct rpc_clnt *rpc_create_xprt(struct rpc_create_args *args,
Kinglong Mee83ddfeb2014-03-24 11:58:59 +0800456 struct rpc_xprt *xprt)
457{
458 struct rpc_clnt *clnt = NULL;
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500459 struct rpc_xprt_switch *xps;
Kinglong Mee83ddfeb2014-03-24 11:58:59 +0800460
J. Bruce Fields39a9bea2016-05-17 12:38:21 -0400461 if (args->bc_xprt && args->bc_xprt->xpt_bc_xps) {
Chuck Lever16590a22016-08-22 14:57:42 -0400462 WARN_ON_ONCE(!(args->protocol & XPRT_TRANSPORT_BC));
J. Bruce Fields39a9bea2016-05-17 12:38:21 -0400463 xps = args->bc_xprt->xpt_bc_xps;
464 xprt_switch_get(xps);
465 } else {
466 xps = xprt_switch_alloc(xprt, GFP_KERNEL);
467 if (xps == NULL) {
468 xprt_put(xprt);
469 return ERR_PTR(-ENOMEM);
470 }
471 if (xprt->bc_xprt) {
472 xprt_switch_get(xps);
473 xprt->bc_xprt->xpt_bc_xps = xps;
474 }
J. Bruce Fields1208fd52016-05-20 17:07:17 -0400475 }
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500476 clnt = rpc_new_client(args, xps, xprt, NULL);
Kinglong Mee83ddfeb2014-03-24 11:58:59 +0800477 if (IS_ERR(clnt))
478 return clnt;
479
480 if (!(args->flags & RPC_CLNT_CREATE_NOPING)) {
481 int err = rpc_ping(clnt);
482 if (err != 0) {
483 rpc_shutdown_client(clnt);
484 return ERR_PTR(err);
485 }
486 }
487
488 clnt->cl_softrtry = 1;
Trond Myklebustae6ec912019-04-07 13:58:58 -0400489 if (args->flags & (RPC_CLNT_CREATE_HARDRTRY|RPC_CLNT_CREATE_SOFTERR)) {
Kinglong Mee83ddfeb2014-03-24 11:58:59 +0800490 clnt->cl_softrtry = 0;
Trond Myklebustae6ec912019-04-07 13:58:58 -0400491 if (args->flags & RPC_CLNT_CREATE_SOFTERR)
492 clnt->cl_softerr = 1;
493 }
Kinglong Mee83ddfeb2014-03-24 11:58:59 +0800494
495 if (args->flags & RPC_CLNT_CREATE_AUTOBIND)
496 clnt->cl_autobind = 1;
Trond Myklebust2aca5b82014-09-24 22:35:58 -0400497 if (args->flags & RPC_CLNT_CREATE_NO_RETRANS_TIMEOUT)
498 clnt->cl_noretranstimeo = 1;
Kinglong Mee83ddfeb2014-03-24 11:58:59 +0800499 if (args->flags & RPC_CLNT_CREATE_DISCRTRY)
500 clnt->cl_discrtry = 1;
501 if (!(args->flags & RPC_CLNT_CREATE_QUIET))
502 clnt->cl_chatty = 1;
503
504 return clnt;
505}
Kinglong Mee83ddfeb2014-03-24 11:58:59 +0800506
Ben Hutchings2c530402012-07-10 10:55:09 +0000507/**
Chuck Leverc2866762006-08-22 20:06:20 -0400508 * rpc_create - create an RPC client and transport with one call
509 * @args: rpc_clnt create argument structure
510 *
511 * Creates and initializes an RPC transport and an RPC client.
512 *
513 * It can ping the server in order to determine if it is up, and to see if
514 * it supports this program and version. RPC_CLNT_CREATE_NOPING disables
515 * this behavior so asynchronous tasks can also use rpc_create.
516 */
517struct rpc_clnt *rpc_create(struct rpc_create_args *args)
518{
519 struct rpc_xprt *xprt;
\"Talpey, Thomas\3c341b0b2007-09-10 13:47:07 -0400520 struct xprt_create xprtargs = {
Pavel Emelyanov9a23e332010-09-29 16:05:12 +0400521 .net = args->net,
\"Talpey, Thomas\4fa016e2007-09-10 13:47:57 -0400522 .ident = args->protocol,
Frank van Maarseveend3bc9a12007-07-09 22:23:35 +0200523 .srcaddr = args->saddress,
Frank van Maarseveen96802a02007-07-08 13:08:54 +0200524 .dstaddr = args->address,
525 .addrlen = args->addrsize,
Trond Myklebust4e0038b2012-03-01 17:01:05 -0500526 .servername = args->servername,
Alexandros Batsakisf300bab2009-09-10 17:33:30 +0300527 .bc_xprt = args->bc_xprt,
Frank van Maarseveen96802a02007-07-08 13:08:54 +0200528 };
Chuck Lever510deb02007-12-10 14:56:24 -0500529 char servername[48];
Chuck Leverc2866762006-08-22 20:06:20 -0400530
J. Bruce Fieldsd50039e2016-05-16 17:03:42 -0400531 if (args->bc_xprt) {
Chuck Lever16590a22016-08-22 14:57:42 -0400532 WARN_ON_ONCE(!(args->protocol & XPRT_TRANSPORT_BC));
J. Bruce Fieldsd50039e2016-05-16 17:03:42 -0400533 xprt = args->bc_xprt->xpt_bc_xprt;
534 if (xprt) {
535 xprt_get(xprt);
536 return rpc_create_xprt(args, xprt);
537 }
538 }
539
Trond Myklebustb7993ce2013-04-14 11:42:00 -0400540 if (args->flags & RPC_CLNT_CREATE_INFINITE_SLOTS)
541 xprtargs.flags |= XPRT_CREATE_INFINITE_SLOTS;
J. Bruce Fields33d90ac2013-04-11 15:06:36 -0400542 if (args->flags & RPC_CLNT_CREATE_NO_IDLE_TIMEOUT)
543 xprtargs.flags |= XPRT_CREATE_NO_IDLE_TIMEOUT;
Chuck Leverc2866762006-08-22 20:06:20 -0400544 /*
Chuck Lever43780b82007-07-01 12:13:22 -0400545 * If the caller chooses not to specify a hostname, whip
546 * up a string representation of the passed-in address.
547 */
Trond Myklebust4e0038b2012-03-01 17:01:05 -0500548 if (xprtargs.servername == NULL) {
Chuck Lever176e21e2011-05-09 15:22:44 -0400549 struct sockaddr_un *sun =
550 (struct sockaddr_un *)args->address;
Chuck Leverda09eb92011-05-09 15:22:25 -0400551 struct sockaddr_in *sin =
552 (struct sockaddr_in *)args->address;
553 struct sockaddr_in6 *sin6 =
554 (struct sockaddr_in6 *)args->address;
555
Chuck Lever510deb02007-12-10 14:56:24 -0500556 servername[0] = '\0';
557 switch (args->address->sa_family) {
Chuck Lever176e21e2011-05-09 15:22:44 -0400558 case AF_LOCAL:
559 snprintf(servername, sizeof(servername), "%s",
560 sun->sun_path);
561 break;
Chuck Leverda09eb92011-05-09 15:22:25 -0400562 case AF_INET:
Harvey Harrison21454aa2008-10-31 00:54:56 -0700563 snprintf(servername, sizeof(servername), "%pI4",
564 &sin->sin_addr.s_addr);
Chuck Lever510deb02007-12-10 14:56:24 -0500565 break;
Chuck Leverda09eb92011-05-09 15:22:25 -0400566 case AF_INET6:
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700567 snprintf(servername, sizeof(servername), "%pI6",
Chuck Leverda09eb92011-05-09 15:22:25 -0400568 &sin6->sin6_addr);
Chuck Lever510deb02007-12-10 14:56:24 -0500569 break;
Chuck Lever510deb02007-12-10 14:56:24 -0500570 default:
571 /* caller wants default server name, but
572 * address family isn't recognized. */
573 return ERR_PTR(-EINVAL);
574 }
Trond Myklebust4e0038b2012-03-01 17:01:05 -0500575 xprtargs.servername = servername;
Chuck Lever43780b82007-07-01 12:13:22 -0400576 }
577
Chuck Lever510deb02007-12-10 14:56:24 -0500578 xprt = xprt_create_transport(&xprtargs);
579 if (IS_ERR(xprt))
580 return (struct rpc_clnt *)xprt;
581
Chuck Lever43780b82007-07-01 12:13:22 -0400582 /*
Chuck Leverc2866762006-08-22 20:06:20 -0400583 * By default, kernel RPC client connects from a reserved port.
584 * CAP_NET_BIND_SERVICE will not be set for unprivileged requesters,
585 * but it is always enabled for rpciod, which handles the connect
586 * operation.
587 */
588 xprt->resvport = 1;
589 if (args->flags & RPC_CLNT_CREATE_NONPRIVPORT)
590 xprt->resvport = 0;
591
Kinglong Mee83ddfeb2014-03-24 11:58:59 +0800592 return rpc_create_xprt(args, xprt);
Chuck Leverc2866762006-08-22 20:06:20 -0400593}
Chuck Leverb86acd52006-08-22 20:06:22 -0400594EXPORT_SYMBOL_GPL(rpc_create);
Chuck Leverc2866762006-08-22 20:06:20 -0400595
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596/*
597 * This function clones the RPC client structure. It allows us to share the
598 * same transport while varying parameters such as the authentication
599 * flavour.
600 */
Chuck Lever1b63a752012-09-14 17:23:52 -0400601static struct rpc_clnt *__rpc_clone_client(struct rpc_create_args *args,
602 struct rpc_clnt *clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603{
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500604 struct rpc_xprt_switch *xps;
Trond Myklebust2446ab62012-03-01 17:00:56 -0500605 struct rpc_xprt *xprt;
Chuck Lever1b63a752012-09-14 17:23:52 -0400606 struct rpc_clnt *new;
607 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
Chuck Lever1b63a752012-09-14 17:23:52 -0400609 err = -ENOMEM;
Trond Myklebust2446ab62012-03-01 17:00:56 -0500610 rcu_read_lock();
611 xprt = xprt_get(rcu_dereference(clnt->cl_xprt));
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500612 xps = xprt_switch_get(rcu_dereference(clnt->cl_xpi.xpi_xpswitch));
Trond Myklebust2446ab62012-03-01 17:00:56 -0500613 rcu_read_unlock();
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500614 if (xprt == NULL || xps == NULL) {
615 xprt_put(xprt);
616 xprt_switch_put(xps);
Chuck Lever1b63a752012-09-14 17:23:52 -0400617 goto out_err;
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500618 }
Chuck Lever1b63a752012-09-14 17:23:52 -0400619 args->servername = xprt->servername;
Trond Myklebust03a9a422015-01-30 18:12:28 -0500620 args->nodename = clnt->cl_nodename;
Chuck Lever1b63a752012-09-14 17:23:52 -0400621
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500622 new = rpc_new_client(args, xps, xprt, clnt);
Chuck Lever1b63a752012-09-14 17:23:52 -0400623 if (IS_ERR(new)) {
624 err = PTR_ERR(new);
Chuck Levera58e0be2013-03-22 12:52:59 -0400625 goto out_err;
Chuck Lever1b63a752012-09-14 17:23:52 -0400626 }
627
Chuck Lever1b63a752012-09-14 17:23:52 -0400628 /* Turn off autobind on clones */
629 new->cl_autobind = 0;
630 new->cl_softrtry = clnt->cl_softrtry;
Trond Myklebustae6ec912019-04-07 13:58:58 -0400631 new->cl_softerr = clnt->cl_softerr;
Trond Myklebust2aca5b82014-09-24 22:35:58 -0400632 new->cl_noretranstimeo = clnt->cl_noretranstimeo;
Chuck Lever1b63a752012-09-14 17:23:52 -0400633 new->cl_discrtry = clnt->cl_discrtry;
634 new->cl_chatty = clnt->cl_chatty;
NeilBrown5e169232018-12-03 11:30:30 +1100635 new->cl_principal = clnt->cl_principal;
Trond Myklebust79caa5f2019-04-24 17:46:42 -0400636 new->cl_cred = get_cred(clnt->cl_cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 return new;
Chuck Lever1b63a752012-09-14 17:23:52 -0400638
Chuck Lever1b63a752012-09-14 17:23:52 -0400639out_err:
Harvey Harrison0dc47872008-03-05 20:47:47 -0800640 dprintk("RPC: %s: returned error %d\n", __func__, err);
Trond Myklebust3e32a5d2006-11-16 11:37:27 -0500641 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642}
Chuck Lever1b63a752012-09-14 17:23:52 -0400643
644/**
645 * rpc_clone_client - Clone an RPC client structure
646 *
647 * @clnt: RPC client whose parameters are copied
648 *
649 * Returns a fresh RPC client or an ERR_PTR.
650 */
651struct rpc_clnt *rpc_clone_client(struct rpc_clnt *clnt)
652{
653 struct rpc_create_args args = {
654 .program = clnt->cl_program,
655 .prognumber = clnt->cl_prog,
656 .version = clnt->cl_vers,
657 .authflavor = clnt->cl_auth->au_flavor,
Trond Myklebust79caa5f2019-04-24 17:46:42 -0400658 .cred = clnt->cl_cred,
Chuck Lever1b63a752012-09-14 17:23:52 -0400659 };
660 return __rpc_clone_client(&args, clnt);
661}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400662EXPORT_SYMBOL_GPL(rpc_clone_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
Chuck Leverba9b5842012-09-14 17:24:02 -0400664/**
665 * rpc_clone_client_set_auth - Clone an RPC client structure and set its auth
666 *
667 * @clnt: RPC client whose parameters are copied
Randy Dunlap7144bca2013-01-09 17:12:35 -0800668 * @flavor: security flavor for new client
Chuck Leverba9b5842012-09-14 17:24:02 -0400669 *
670 * Returns a fresh RPC client or an ERR_PTR.
671 */
672struct rpc_clnt *
673rpc_clone_client_set_auth(struct rpc_clnt *clnt, rpc_authflavor_t flavor)
674{
675 struct rpc_create_args args = {
676 .program = clnt->cl_program,
677 .prognumber = clnt->cl_prog,
678 .version = clnt->cl_vers,
679 .authflavor = flavor,
Trond Myklebust79caa5f2019-04-24 17:46:42 -0400680 .cred = clnt->cl_cred,
Chuck Leverba9b5842012-09-14 17:24:02 -0400681 };
682 return __rpc_clone_client(&args, clnt);
683}
684EXPORT_SYMBOL_GPL(rpc_clone_client_set_auth);
685
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400686/**
687 * rpc_switch_client_transport: switch the RPC transport on the fly
688 * @clnt: pointer to a struct rpc_clnt
689 * @args: pointer to the new transport arguments
690 * @timeout: pointer to the new timeout parameters
691 *
692 * This function allows the caller to switch the RPC transport for the
693 * rpc_clnt structure 'clnt' to allow it to connect to a mirrored NFS
694 * server, for instance. It assumes that the caller has ensured that
695 * there are no active RPC tasks by using some form of locking.
696 *
697 * Returns zero if "clnt" is now using the new xprt. Otherwise a
698 * negative errno is returned, and "clnt" continues to use the old
699 * xprt.
700 */
701int rpc_switch_client_transport(struct rpc_clnt *clnt,
702 struct xprt_create *args,
703 const struct rpc_timeout *timeout)
704{
705 const struct rpc_timeout *old_timeo;
706 rpc_authflavor_t pseudoflavor;
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500707 struct rpc_xprt_switch *xps, *oldxps;
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400708 struct rpc_xprt *xprt, *old;
709 struct rpc_clnt *parent;
710 int err;
711
712 xprt = xprt_create_transport(args);
713 if (IS_ERR(xprt)) {
714 dprintk("RPC: failed to create new xprt for clnt %p\n",
715 clnt);
716 return PTR_ERR(xprt);
717 }
718
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500719 xps = xprt_switch_alloc(xprt, GFP_KERNEL);
720 if (xps == NULL) {
721 xprt_put(xprt);
722 return -ENOMEM;
723 }
724
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400725 pseudoflavor = clnt->cl_auth->au_flavor;
726
727 old_timeo = clnt->cl_timeout;
728 old = rpc_clnt_set_transport(clnt, xprt, timeout);
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500729 oldxps = xprt_iter_xchg_switch(&clnt->cl_xpi, xps);
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400730
731 rpc_unregister_client(clnt);
732 __rpc_clnt_remove_pipedir(clnt);
Jeff Laytonb4b9d2c2014-11-26 14:44:43 -0500733 rpc_clnt_debugfs_unregister(clnt);
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400734
735 /*
736 * A new transport was created. "clnt" therefore
737 * becomes the root of a new cl_parent tree. clnt's
738 * children, if it has any, still point to the old xprt.
739 */
740 parent = clnt->cl_parent;
741 clnt->cl_parent = clnt;
742
743 /*
744 * The old rpc_auth cache cannot be re-used. GSS
745 * contexts in particular are between a single
746 * client and server.
747 */
748 err = rpc_client_register(clnt, pseudoflavor, NULL);
749 if (err)
750 goto out_revert;
751
752 synchronize_rcu();
753 if (parent != clnt)
754 rpc_release_client(parent);
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500755 xprt_switch_put(oldxps);
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400756 xprt_put(old);
757 dprintk("RPC: replaced xprt for clnt %p\n", clnt);
758 return 0;
759
760out_revert:
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500761 xps = xprt_iter_xchg_switch(&clnt->cl_xpi, oldxps);
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400762 rpc_clnt_set_transport(clnt, old, old_timeo);
763 clnt->cl_parent = parent;
764 rpc_client_register(clnt, pseudoflavor, NULL);
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500765 xprt_switch_put(xps);
Trond Myklebust40b00b6b2013-10-17 14:12:23 -0400766 xprt_put(xprt);
767 dprintk("RPC: failed to switch xprt for clnt %p\n", clnt);
768 return err;
769}
770EXPORT_SYMBOL_GPL(rpc_switch_client_transport);
771
Trond Myklebust32278862016-01-30 20:39:19 -0500772static
773int rpc_clnt_xprt_iter_init(struct rpc_clnt *clnt, struct rpc_xprt_iter *xpi)
774{
775 struct rpc_xprt_switch *xps;
776
777 rcu_read_lock();
778 xps = xprt_switch_get(rcu_dereference(clnt->cl_xpi.xpi_xpswitch));
779 rcu_read_unlock();
780 if (xps == NULL)
781 return -EAGAIN;
782 xprt_iter_init_listall(xpi, xps);
783 xprt_switch_put(xps);
784 return 0;
785}
786
787/**
788 * rpc_clnt_iterate_for_each_xprt - Apply a function to all transports
789 * @clnt: pointer to client
790 * @fn: function to apply
791 * @data: void pointer to function data
792 *
793 * Iterates through the list of RPC transports currently attached to the
794 * client and applies the function fn(clnt, xprt, data).
795 *
796 * On error, the iteration stops, and the function returns the error value.
797 */
798int rpc_clnt_iterate_for_each_xprt(struct rpc_clnt *clnt,
799 int (*fn)(struct rpc_clnt *, struct rpc_xprt *, void *),
800 void *data)
801{
802 struct rpc_xprt_iter xpi;
803 int ret;
804
805 ret = rpc_clnt_xprt_iter_init(clnt, &xpi);
806 if (ret)
807 return ret;
808 for (;;) {
809 struct rpc_xprt *xprt = xprt_iter_get_next(&xpi);
810
811 if (!xprt)
812 break;
813 ret = fn(clnt, xprt, data);
814 xprt_put(xprt);
815 if (ret < 0)
816 break;
817 }
818 xprt_iter_destroy(&xpi);
819 return ret;
820}
821EXPORT_SYMBOL_GPL(rpc_clnt_iterate_for_each_xprt);
822
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823/*
Trond Myklebust58f96122010-07-31 14:29:08 -0400824 * Kill all tasks for the given client.
825 * XXX: kill their descendants as well?
826 */
827void rpc_killall_tasks(struct rpc_clnt *clnt)
828{
829 struct rpc_task *rovr;
830
831
832 if (list_empty(&clnt->cl_tasks))
833 return;
834 dprintk("RPC: killing all tasks for client %p\n", clnt);
835 /*
836 * Spin lock all_tasks to prevent changes...
837 */
838 spin_lock(&clnt->cl_lock);
Trond Myklebustae67bd32019-04-07 13:58:44 -0400839 list_for_each_entry(rovr, &clnt->cl_tasks, tk_task)
840 rpc_signal_task(rovr);
Trond Myklebust58f96122010-07-31 14:29:08 -0400841 spin_unlock(&clnt->cl_lock);
842}
843EXPORT_SYMBOL_GPL(rpc_killall_tasks);
844
845/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 * Properly shut down an RPC client, terminating all outstanding
Trond Myklebust90c57552007-06-09 19:49:36 -0400847 * requests.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 */
Trond Myklebust4c402b42007-06-14 16:40:32 -0400849void rpc_shutdown_client(struct rpc_clnt *clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850{
Weston Andros Adamson168e4b32012-10-30 17:01:40 -0400851 might_sleep();
852
Trond Myklebust4e0038b2012-03-01 17:01:05 -0500853 dprintk_rcu("RPC: shutting down %s client for %s\n",
Trond Myklebust55909f22013-08-23 11:48:15 -0400854 clnt->cl_program->name,
Trond Myklebust4e0038b2012-03-01 17:01:05 -0500855 rcu_dereference(clnt->cl_xprt)->servername);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
Trond Myklebust34f52e32007-06-14 16:40:31 -0400857 while (!list_empty(&clnt->cl_tasks)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 rpc_killall_tasks(clnt);
Ingo Molnar532347e2006-01-09 20:52:53 -0800859 wait_event_timeout(destroy_wait,
Trond Myklebust34f52e32007-06-14 16:40:31 -0400860 list_empty(&clnt->cl_tasks), 1*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 }
862
Trond Myklebust4c402b42007-06-14 16:40:32 -0400863 rpc_release_client(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400865EXPORT_SYMBOL_GPL(rpc_shutdown_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
867/*
Trond Myklebust34f52e32007-06-14 16:40:31 -0400868 * Free an RPC client
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 */
Trond Myklebustd07ba842013-11-12 17:24:36 -0500870static struct rpc_clnt *
Trond Myklebust006abe82010-09-12 19:55:25 -0400871rpc_free_client(struct rpc_clnt *clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872{
Trond Myklebustd07ba842013-11-12 17:24:36 -0500873 struct rpc_clnt *parent = NULL;
874
Trond Myklebust4e0038b2012-03-01 17:01:05 -0500875 dprintk_rcu("RPC: destroying %s client for %s\n",
Trond Myklebust55909f22013-08-23 11:48:15 -0400876 clnt->cl_program->name,
Trond Myklebust4e0038b2012-03-01 17:01:05 -0500877 rcu_dereference(clnt->cl_xprt)->servername);
Trond Myklebust6eac7d32012-01-20 13:53:37 -0500878 if (clnt->cl_parent != clnt)
Trond Myklebustd07ba842013-11-12 17:24:36 -0500879 parent = clnt->cl_parent;
Jeff Laytonb4b9d2c2014-11-26 14:44:43 -0500880 rpc_clnt_debugfs_unregister(clnt);
Stanislav Kinsburskyf5131252012-01-11 19:18:26 +0400881 rpc_clnt_remove_pipedir(clnt);
Stanislav Kinsburskyadb6fa72013-06-26 10:15:14 +0400882 rpc_unregister_client(clnt);
Chuck Lever11c556b2006-03-20 13:44:22 -0500883 rpc_free_iostats(clnt->cl_metrics);
884 clnt->cl_metrics = NULL;
Trond Myklebust2446ab62012-03-01 17:00:56 -0500885 xprt_put(rcu_dereference_raw(clnt->cl_xprt));
Trond Myklebustad01b2c2016-01-30 14:17:26 -0500886 xprt_iter_destroy(&clnt->cl_xpi);
Trond Myklebust4ada5392007-06-14 17:26:17 -0400887 rpciod_down();
Trond Myklebust79caa5f2019-04-24 17:46:42 -0400888 put_cred(clnt->cl_cred);
Trond Myklebust2f048db2013-09-04 21:51:44 -0400889 rpc_free_clid(clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 kfree(clnt);
Trond Myklebustd07ba842013-11-12 17:24:36 -0500891 return parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892}
893
894/*
Trond Myklebust1dd17ec2007-06-26 16:57:41 -0400895 * Free an RPC client
896 */
Stephen Hemminger8fdee4c2018-07-24 12:29:15 -0700897static struct rpc_clnt *
Trond Myklebust006abe82010-09-12 19:55:25 -0400898rpc_free_auth(struct rpc_clnt *clnt)
Trond Myklebust1dd17ec2007-06-26 16:57:41 -0400899{
Trond Myklebustd07ba842013-11-12 17:24:36 -0500900 if (clnt->cl_auth == NULL)
901 return rpc_free_client(clnt);
Trond Myklebust1dd17ec2007-06-26 16:57:41 -0400902
903 /*
904 * Note: RPCSEC_GSS may need to send NULL RPC calls in order to
905 * release remaining GSS contexts. This mechanism ensures
906 * that it can do so safely.
907 */
Trond Myklebust006abe82010-09-12 19:55:25 -0400908 atomic_inc(&clnt->cl_count);
Trond Myklebust1dd17ec2007-06-26 16:57:41 -0400909 rpcauth_release(clnt->cl_auth);
910 clnt->cl_auth = NULL;
Trond Myklebust006abe82010-09-12 19:55:25 -0400911 if (atomic_dec_and_test(&clnt->cl_count))
Trond Myklebustd07ba842013-11-12 17:24:36 -0500912 return rpc_free_client(clnt);
913 return NULL;
Trond Myklebust1dd17ec2007-06-26 16:57:41 -0400914}
915
916/*
Trond Myklebust34f52e32007-06-14 16:40:31 -0400917 * Release reference to the RPC client
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 */
919void
920rpc_release_client(struct rpc_clnt *clnt)
921{
Trond Myklebust34f52e32007-06-14 16:40:31 -0400922 dprintk("RPC: rpc_release_client(%p)\n", clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
Trond Myklebustd07ba842013-11-12 17:24:36 -0500924 do {
925 if (list_empty(&clnt->cl_tasks))
926 wake_up(&destroy_wait);
927 if (!atomic_dec_and_test(&clnt->cl_count))
928 break;
929 clnt = rpc_free_auth(clnt);
930 } while (clnt != NULL);
Trond Myklebust34f52e32007-06-14 16:40:31 -0400931}
Simo Sorce1d658332012-05-25 18:09:55 -0400932EXPORT_SYMBOL_GPL(rpc_release_client);
Trond Myklebust34f52e32007-06-14 16:40:31 -0400933
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000934/**
935 * rpc_bind_new_program - bind a new RPC program to an existing client
Randy Dunlap65b6e422008-02-13 15:03:23 -0800936 * @old: old rpc_client
937 * @program: rpc program to set
938 * @vers: rpc program version
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000939 *
940 * Clones the rpc client and sets up a new RPC program. This is mainly
941 * of use for enabling different RPC programs to share the same transport.
942 * The Sun NFSv2/v3 ACL protocol can do this.
943 */
944struct rpc_clnt *rpc_bind_new_program(struct rpc_clnt *old,
Trond Myklebusta613fa12012-01-20 13:53:56 -0500945 const struct rpc_program *program,
Chuck Lever89eb21c2007-09-11 18:00:09 -0400946 u32 vers)
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000947{
Trond Myklebustf994c432012-11-01 12:14:14 -0400948 struct rpc_create_args args = {
949 .program = program,
950 .prognumber = program->number,
951 .version = vers,
952 .authflavor = old->cl_auth->au_flavor,
Trond Myklebust79caa5f2019-04-24 17:46:42 -0400953 .cred = old->cl_cred,
Trond Myklebustf994c432012-11-01 12:14:14 -0400954 };
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000955 struct rpc_clnt *clnt;
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000956 int err;
957
Trond Myklebustf994c432012-11-01 12:14:14 -0400958 clnt = __rpc_clone_client(&args, old);
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000959 if (IS_ERR(clnt))
960 goto out;
Chuck Levercaabea82009-12-03 15:58:56 -0500961 err = rpc_ping(clnt);
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000962 if (err != 0) {
963 rpc_shutdown_client(clnt);
964 clnt = ERR_PTR(err);
965 }
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800966out:
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000967 return clnt;
968}
Trond Myklebuste8914c62007-07-14 15:39:59 -0400969EXPORT_SYMBOL_GPL(rpc_bind_new_program);
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000970
Bill Baker0f90be12018-06-19 16:24:58 -0500971void rpc_task_release_transport(struct rpc_task *task)
972{
973 struct rpc_xprt *xprt = task->tk_xprt;
974
975 if (xprt) {
976 task->tk_xprt = NULL;
977 xprt_put(xprt);
978 }
979}
980EXPORT_SYMBOL_GPL(rpc_task_release_transport);
981
Trond Myklebust58f96122010-07-31 14:29:08 -0400982void rpc_task_release_client(struct rpc_task *task)
983{
984 struct rpc_clnt *clnt = task->tk_client;
985
986 if (clnt != NULL) {
987 /* Remove from client task list */
988 spin_lock(&clnt->cl_lock);
989 list_del(&task->tk_task);
990 spin_unlock(&clnt->cl_lock);
991 task->tk_client = NULL;
992
993 rpc_release_client(clnt);
994 }
Bill Baker0f90be12018-06-19 16:24:58 -0500995 rpc_task_release_transport(task);
996}
Trond Myklebustfb43d172016-01-30 16:39:26 -0500997
Bill Baker0f90be12018-06-19 16:24:58 -0500998static
999void rpc_task_set_transport(struct rpc_task *task, struct rpc_clnt *clnt)
1000{
1001 if (!task->tk_xprt)
1002 task->tk_xprt = xprt_iter_get_next(&clnt->cl_xpi);
Trond Myklebust58f96122010-07-31 14:29:08 -04001003}
1004
1005static
1006void rpc_task_set_client(struct rpc_task *task, struct rpc_clnt *clnt)
1007{
Trond Myklebustfb43d172016-01-30 16:39:26 -05001008
Trond Myklebust58f96122010-07-31 14:29:08 -04001009 if (clnt != NULL) {
Bill Baker0f90be12018-06-19 16:24:58 -05001010 rpc_task_set_transport(task, clnt);
Trond Myklebust58f96122010-07-31 14:29:08 -04001011 task->tk_client = clnt;
Trond Myklebust006abe82010-09-12 19:55:25 -04001012 atomic_inc(&clnt->cl_count);
Trond Myklebust58f96122010-07-31 14:29:08 -04001013 if (clnt->cl_softrtry)
1014 task->tk_flags |= RPC_TASK_SOFT;
Trond Myklebustae6ec912019-04-07 13:58:58 -04001015 if (clnt->cl_softerr)
1016 task->tk_flags |= RPC_TASK_TIMEOUT;
Trond Myklebust8a19a0b2013-09-24 12:00:27 -04001017 if (clnt->cl_noretranstimeo)
1018 task->tk_flags |= RPC_TASK_NO_RETRANS_TIMEOUT;
Jeff Layton3c87ef62015-06-03 16:14:25 -04001019 if (atomic_read(&clnt->cl_swapper))
1020 task->tk_flags |= RPC_TASK_SWAPPER;
Trond Myklebust58f96122010-07-31 14:29:08 -04001021 /* Add to the client's list of all tasks */
1022 spin_lock(&clnt->cl_lock);
1023 list_add_tail(&task->tk_task, &clnt->cl_tasks);
1024 spin_unlock(&clnt->cl_lock);
1025 }
1026}
1027
1028static void
1029rpc_task_set_rpc_message(struct rpc_task *task, const struct rpc_message *msg)
1030{
1031 if (msg != NULL) {
1032 task->tk_msg.rpc_proc = msg->rpc_proc;
1033 task->tk_msg.rpc_argp = msg->rpc_argp;
1034 task->tk_msg.rpc_resp = msg->rpc_resp;
Trond Myklebusta17c2152010-07-31 14:29:08 -04001035 if (msg->rpc_cred != NULL)
NeilBrowna52458b2018-12-03 11:30:31 +11001036 task->tk_msg.rpc_cred = get_cred(msg->rpc_cred);
Trond Myklebust58f96122010-07-31 14:29:08 -04001037 }
1038}
1039
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040/*
1041 * Default callback for async RPC calls
1042 */
1043static void
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001044rpc_default_callback(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045{
1046}
1047
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001048static const struct rpc_call_ops rpc_default_ops = {
1049 .rpc_call_done = rpc_default_callback,
1050};
1051
Trond Myklebustc970aa82007-07-14 15:39:59 -04001052/**
1053 * rpc_run_task - Allocate a new RPC task, then run rpc_execute against it
1054 * @task_setup_data: pointer to task initialisation data
1055 */
1056struct rpc_task *rpc_run_task(const struct rpc_task_setup *task_setup_data)
Trond Myklebust6e5b70e2007-06-12 10:02:37 -04001057{
Trond Myklebust19445b92010-04-16 16:41:10 -04001058 struct rpc_task *task;
Trond Myklebust6e5b70e2007-06-12 10:02:37 -04001059
Trond Myklebust84115e12007-07-14 15:39:59 -04001060 task = rpc_new_task(task_setup_data);
Trond Myklebust6e5b70e2007-06-12 10:02:37 -04001061
Trond Myklebust58f96122010-07-31 14:29:08 -04001062 rpc_task_set_client(task, task_setup_data->rpc_client);
1063 rpc_task_set_rpc_message(task, task_setup_data->rpc_message);
1064
Trond Myklebust58f96122010-07-31 14:29:08 -04001065 if (task->tk_action == NULL)
1066 rpc_call_start(task);
1067
Trond Myklebust6e5b70e2007-06-12 10:02:37 -04001068 atomic_inc(&task->tk_count);
1069 rpc_execute(task);
Trond Myklebust19445b92010-04-16 16:41:10 -04001070 return task;
Trond Myklebust6e5b70e2007-06-12 10:02:37 -04001071}
Trond Myklebustc970aa82007-07-14 15:39:59 -04001072EXPORT_SYMBOL_GPL(rpc_run_task);
Trond Myklebust6e5b70e2007-06-12 10:02:37 -04001073
1074/**
1075 * rpc_call_sync - Perform a synchronous RPC call
1076 * @clnt: pointer to RPC client
1077 * @msg: RPC call parameters
1078 * @flags: RPC call flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 */
Trond Myklebustcbc20052008-02-14 11:11:30 -05001080int rpc_call_sync(struct rpc_clnt *clnt, const struct rpc_message *msg, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081{
1082 struct rpc_task *task;
Trond Myklebust84115e12007-07-14 15:39:59 -04001083 struct rpc_task_setup task_setup_data = {
1084 .rpc_client = clnt,
1085 .rpc_message = msg,
1086 .callback_ops = &rpc_default_ops,
1087 .flags = flags,
1088 };
Trond Myklebust6e5b70e2007-06-12 10:02:37 -04001089 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
Weston Andros Adamson50d2bdb2012-11-01 16:04:40 -04001091 WARN_ON_ONCE(flags & RPC_TASK_ASYNC);
1092 if (flags & RPC_TASK_ASYNC) {
1093 rpc_release_calldata(task_setup_data.callback_ops,
1094 task_setup_data.callback_data);
1095 return -EINVAL;
1096 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
Trond Myklebustc970aa82007-07-14 15:39:59 -04001098 task = rpc_run_task(&task_setup_data);
Trond Myklebust6e5b70e2007-06-12 10:02:37 -04001099 if (IS_ERR(task))
1100 return PTR_ERR(task);
Trond Myklebuste60859a2006-01-03 09:55:10 +01001101 status = task->tk_status;
Trond Myklebustbde8f002007-01-24 11:54:53 -08001102 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 return status;
1104}
Trond Myklebuste8914c62007-07-14 15:39:59 -04001105EXPORT_SYMBOL_GPL(rpc_call_sync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
Trond Myklebust6e5b70e2007-06-12 10:02:37 -04001107/**
1108 * rpc_call_async - Perform an asynchronous RPC call
1109 * @clnt: pointer to RPC client
1110 * @msg: RPC call parameters
1111 * @flags: RPC call flags
Randy Dunlap65b6e422008-02-13 15:03:23 -08001112 * @tk_ops: RPC call ops
Trond Myklebust6e5b70e2007-06-12 10:02:37 -04001113 * @data: user call data
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 */
1115int
Trond Myklebustcbc20052008-02-14 11:11:30 -05001116rpc_call_async(struct rpc_clnt *clnt, const struct rpc_message *msg, int flags,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001117 const struct rpc_call_ops *tk_ops, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118{
1119 struct rpc_task *task;
Trond Myklebust84115e12007-07-14 15:39:59 -04001120 struct rpc_task_setup task_setup_data = {
1121 .rpc_client = clnt,
1122 .rpc_message = msg,
1123 .callback_ops = tk_ops,
1124 .callback_data = data,
1125 .flags = flags|RPC_TASK_ASYNC,
1126 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
Trond Myklebustc970aa82007-07-14 15:39:59 -04001128 task = rpc_run_task(&task_setup_data);
Trond Myklebust6e5b70e2007-06-12 10:02:37 -04001129 if (IS_ERR(task))
1130 return PTR_ERR(task);
1131 rpc_put_task(task);
1132 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133}
Trond Myklebuste8914c62007-07-14 15:39:59 -04001134EXPORT_SYMBOL_GPL(rpc_call_async);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001136#if defined(CONFIG_SUNRPC_BACKCHANNEL)
Trond Myklebust477687e2019-03-05 07:30:48 -05001137static void call_bc_encode(struct rpc_task *task);
1138
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001139/**
1140 * rpc_run_bc_task - Allocate a new RPC task for backchannel use, then run
1141 * rpc_execute against it
Jaswinder Singh Rajput7a73fdd2009-09-24 14:58:42 -04001142 * @req: RPC request
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001143 */
Trond Myklebust0f419792015-06-01 22:59:08 -04001144struct rpc_task *rpc_run_bc_task(struct rpc_rqst *req)
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001145{
1146 struct rpc_task *task;
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001147 struct rpc_task_setup task_setup_data = {
Trond Myklebust0f419792015-06-01 22:59:08 -04001148 .callback_ops = &rpc_default_ops,
Trond Myklebust762e4e62018-08-24 16:28:28 -04001149 .flags = RPC_TASK_SOFTCONN |
1150 RPC_TASK_NO_RETRANS_TIMEOUT,
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001151 };
1152
1153 dprintk("RPC: rpc_run_bc_task req= %p\n", req);
1154 /*
1155 * Create an rpc_task to send the data
1156 */
1157 task = rpc_new_task(&task_setup_data);
Trond Myklebust902c5882018-09-01 17:21:01 -04001158 xprt_init_bc_request(req, task);
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001159
Trond Myklebust477687e2019-03-05 07:30:48 -05001160 task->tk_action = call_bc_encode;
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001161 atomic_inc(&task->tk_count);
Weston Andros Adamson9a6478f2012-10-23 10:43:27 -04001162 WARN_ON_ONCE(atomic_read(&task->tk_count) != 2);
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001163 rpc_execute(task);
1164
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001165 dprintk("RPC: rpc_run_bc_task: task= %p\n", task);
1166 return task;
1167}
Trond Myklebust9e00abc2011-07-13 19:20:49 -04001168#endif /* CONFIG_SUNRPC_BACKCHANNEL */
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04001169
Chuck Levercf500ba2019-02-11 11:25:20 -05001170/**
1171 * rpc_prepare_reply_pages - Prepare to receive a reply data payload into pages
1172 * @req: RPC request to prepare
1173 * @pages: vector of struct page pointers
1174 * @base: offset in first page where receive should start, in bytes
1175 * @len: expected size of the upper layer data payload, in bytes
1176 * @hdrsize: expected size of upper layer reply header, in XDR words
1177 *
1178 */
1179void rpc_prepare_reply_pages(struct rpc_rqst *req, struct page **pages,
1180 unsigned int base, unsigned int len,
1181 unsigned int hdrsize)
1182{
Chuck Lever02ef04e2019-02-11 11:25:25 -05001183 /* Subtract one to force an extra word of buffer space for the
1184 * payload's XDR pad to fall into the rcv_buf's tail iovec.
1185 */
Chuck Lever35e77d22019-02-11 11:25:36 -05001186 hdrsize += RPC_REPHDRSIZE + req->rq_cred->cr_auth->au_ralign - 1;
Chuck Lever02ef04e2019-02-11 11:25:25 -05001187
Chuck Levercf500ba2019-02-11 11:25:20 -05001188 xdr_inline_pages(&req->rq_rcv_buf, hdrsize << 2, pages, base, len);
1189 trace_rpc_reply_pages(req);
1190}
1191EXPORT_SYMBOL_GPL(rpc_prepare_reply_pages);
1192
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193void
Trond Myklebust77de2c52007-10-25 18:40:21 -04001194rpc_call_start(struct rpc_task *task)
1195{
1196 task->tk_action = call_start;
1197}
1198EXPORT_SYMBOL_GPL(rpc_call_start);
1199
Chuck Levered394402006-08-22 20:06:17 -04001200/**
1201 * rpc_peeraddr - extract remote peer address from clnt's xprt
1202 * @clnt: RPC client structure
1203 * @buf: target buffer
Randy Dunlap65b6e422008-02-13 15:03:23 -08001204 * @bufsize: length of target buffer
Chuck Levered394402006-08-22 20:06:17 -04001205 *
1206 * Returns the number of bytes that are actually in the stored address.
1207 */
1208size_t rpc_peeraddr(struct rpc_clnt *clnt, struct sockaddr *buf, size_t bufsize)
1209{
1210 size_t bytes;
Trond Myklebust2446ab62012-03-01 17:00:56 -05001211 struct rpc_xprt *xprt;
Chuck Levered394402006-08-22 20:06:17 -04001212
Trond Myklebust2446ab62012-03-01 17:00:56 -05001213 rcu_read_lock();
1214 xprt = rcu_dereference(clnt->cl_xprt);
1215
1216 bytes = xprt->addrlen;
Chuck Levered394402006-08-22 20:06:17 -04001217 if (bytes > bufsize)
1218 bytes = bufsize;
Trond Myklebust2446ab62012-03-01 17:00:56 -05001219 memcpy(buf, &xprt->addr, bytes);
1220 rcu_read_unlock();
1221
1222 return bytes;
Chuck Levered394402006-08-22 20:06:17 -04001223}
Chuck Leverb86acd52006-08-22 20:06:22 -04001224EXPORT_SYMBOL_GPL(rpc_peeraddr);
Chuck Levered394402006-08-22 20:06:17 -04001225
Chuck Leverf425eba2006-08-22 20:06:18 -04001226/**
1227 * rpc_peeraddr2str - return remote peer address in printable format
1228 * @clnt: RPC client structure
1229 * @format: address format
1230 *
Trond Myklebust2446ab62012-03-01 17:00:56 -05001231 * NB: the lifetime of the memory referenced by the returned pointer is
1232 * the same as the rpc_xprt itself. As long as the caller uses this
1233 * pointer, it must hold the RCU read lock.
Chuck Leverf425eba2006-08-22 20:06:18 -04001234 */
Chuck Leverb454ae92008-01-07 18:34:48 -05001235const char *rpc_peeraddr2str(struct rpc_clnt *clnt,
1236 enum rpc_display_format_t format)
Chuck Leverf425eba2006-08-22 20:06:18 -04001237{
Trond Myklebust2446ab62012-03-01 17:00:56 -05001238 struct rpc_xprt *xprt;
1239
1240 xprt = rcu_dereference(clnt->cl_xprt);
Chuck Lever7559c7a2006-12-05 16:35:37 -05001241
1242 if (xprt->address_strings[format] != NULL)
1243 return xprt->address_strings[format];
1244 else
1245 return "unprintable";
Chuck Leverf425eba2006-08-22 20:06:18 -04001246}
Chuck Leverb86acd52006-08-22 20:06:22 -04001247EXPORT_SYMBOL_GPL(rpc_peeraddr2str);
Chuck Leverf425eba2006-08-22 20:06:18 -04001248
Chuck Lever2e738fd2012-03-01 17:01:14 -05001249static const struct sockaddr_in rpc_inaddr_loopback = {
1250 .sin_family = AF_INET,
1251 .sin_addr.s_addr = htonl(INADDR_ANY),
1252};
1253
1254static const struct sockaddr_in6 rpc_in6addr_loopback = {
1255 .sin6_family = AF_INET6,
1256 .sin6_addr = IN6ADDR_ANY_INIT,
1257};
1258
1259/*
1260 * Try a getsockname() on a connected datagram socket. Using a
1261 * connected datagram socket prevents leaving a socket in TIME_WAIT.
1262 * This conserves the ephemeral port number space.
1263 *
1264 * Returns zero and fills in "buf" if successful; otherwise, a
1265 * negative errno is returned.
1266 */
1267static int rpc_sockname(struct net *net, struct sockaddr *sap, size_t salen,
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001268 struct sockaddr *buf)
Chuck Lever2e738fd2012-03-01 17:01:14 -05001269{
1270 struct socket *sock;
1271 int err;
1272
1273 err = __sock_create(net, sap->sa_family,
1274 SOCK_DGRAM, IPPROTO_UDP, &sock, 1);
1275 if (err < 0) {
1276 dprintk("RPC: can't create UDP socket (%d)\n", err);
1277 goto out;
1278 }
1279
1280 switch (sap->sa_family) {
1281 case AF_INET:
1282 err = kernel_bind(sock,
1283 (struct sockaddr *)&rpc_inaddr_loopback,
1284 sizeof(rpc_inaddr_loopback));
1285 break;
1286 case AF_INET6:
1287 err = kernel_bind(sock,
1288 (struct sockaddr *)&rpc_in6addr_loopback,
1289 sizeof(rpc_in6addr_loopback));
1290 break;
1291 default:
1292 err = -EAFNOSUPPORT;
1293 goto out;
1294 }
1295 if (err < 0) {
1296 dprintk("RPC: can't bind UDP socket (%d)\n", err);
1297 goto out_release;
1298 }
1299
1300 err = kernel_connect(sock, sap, salen, 0);
1301 if (err < 0) {
1302 dprintk("RPC: can't connect UDP socket (%d)\n", err);
1303 goto out_release;
1304 }
1305
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001306 err = kernel_getsockname(sock, buf);
Chuck Lever2e738fd2012-03-01 17:01:14 -05001307 if (err < 0) {
1308 dprintk("RPC: getsockname failed (%d)\n", err);
1309 goto out_release;
1310 }
1311
1312 err = 0;
1313 if (buf->sa_family == AF_INET6) {
1314 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)buf;
1315 sin6->sin6_scope_id = 0;
1316 }
1317 dprintk("RPC: %s succeeded\n", __func__);
1318
1319out_release:
1320 sock_release(sock);
1321out:
1322 return err;
1323}
1324
1325/*
1326 * Scraping a connected socket failed, so we don't have a useable
1327 * local address. Fallback: generate an address that will prevent
1328 * the server from calling us back.
1329 *
1330 * Returns zero and fills in "buf" if successful; otherwise, a
1331 * negative errno is returned.
1332 */
1333static int rpc_anyaddr(int family, struct sockaddr *buf, size_t buflen)
1334{
1335 switch (family) {
1336 case AF_INET:
1337 if (buflen < sizeof(rpc_inaddr_loopback))
1338 return -EINVAL;
1339 memcpy(buf, &rpc_inaddr_loopback,
1340 sizeof(rpc_inaddr_loopback));
1341 break;
1342 case AF_INET6:
1343 if (buflen < sizeof(rpc_in6addr_loopback))
1344 return -EINVAL;
1345 memcpy(buf, &rpc_in6addr_loopback,
1346 sizeof(rpc_in6addr_loopback));
Trond Myklebust0b161e62015-12-30 18:14:06 -05001347 break;
Chuck Lever2e738fd2012-03-01 17:01:14 -05001348 default:
1349 dprintk("RPC: %s: address family not supported\n",
1350 __func__);
1351 return -EAFNOSUPPORT;
1352 }
1353 dprintk("RPC: %s: succeeded\n", __func__);
1354 return 0;
1355}
1356
1357/**
1358 * rpc_localaddr - discover local endpoint address for an RPC client
1359 * @clnt: RPC client structure
1360 * @buf: target buffer
1361 * @buflen: size of target buffer, in bytes
1362 *
1363 * Returns zero and fills in "buf" and "buflen" if successful;
1364 * otherwise, a negative errno is returned.
1365 *
1366 * This works even if the underlying transport is not currently connected,
1367 * or if the upper layer never previously provided a source address.
1368 *
1369 * The result of this function call is transient: multiple calls in
1370 * succession may give different results, depending on how local
1371 * networking configuration changes over time.
1372 */
1373int rpc_localaddr(struct rpc_clnt *clnt, struct sockaddr *buf, size_t buflen)
1374{
1375 struct sockaddr_storage address;
1376 struct sockaddr *sap = (struct sockaddr *)&address;
1377 struct rpc_xprt *xprt;
1378 struct net *net;
1379 size_t salen;
1380 int err;
1381
1382 rcu_read_lock();
1383 xprt = rcu_dereference(clnt->cl_xprt);
1384 salen = xprt->addrlen;
1385 memcpy(sap, &xprt->addr, salen);
1386 net = get_net(xprt->xprt_net);
1387 rcu_read_unlock();
1388
1389 rpc_set_port(sap, 0);
Denys Vlasenko9b2c45d2018-02-12 20:00:20 +01001390 err = rpc_sockname(net, sap, salen, buf);
Chuck Lever2e738fd2012-03-01 17:01:14 -05001391 put_net(net);
1392 if (err != 0)
1393 /* Couldn't discover local address, return ANYADDR */
1394 return rpc_anyaddr(sap->sa_family, buf, buflen);
1395 return 0;
1396}
1397EXPORT_SYMBOL_GPL(rpc_localaddr);
1398
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399void
1400rpc_setbufsize(struct rpc_clnt *clnt, unsigned int sndsize, unsigned int rcvsize)
1401{
Trond Myklebust2446ab62012-03-01 17:00:56 -05001402 struct rpc_xprt *xprt;
1403
1404 rcu_read_lock();
1405 xprt = rcu_dereference(clnt->cl_xprt);
Chuck Lever470056c2005-08-25 16:25:56 -07001406 if (xprt->ops->set_buffer_size)
1407 xprt->ops->set_buffer_size(xprt, sndsize, rcvsize);
Trond Myklebust2446ab62012-03-01 17:00:56 -05001408 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409}
Trond Myklebuste8914c62007-07-14 15:39:59 -04001410EXPORT_SYMBOL_GPL(rpc_setbufsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
Trond Myklebust2446ab62012-03-01 17:00:56 -05001412/**
Trond Myklebust2446ab62012-03-01 17:00:56 -05001413 * rpc_net_ns - Get the network namespace for this RPC client
1414 * @clnt: RPC client to query
1415 *
1416 */
1417struct net *rpc_net_ns(struct rpc_clnt *clnt)
1418{
1419 struct net *ret;
1420
1421 rcu_read_lock();
1422 ret = rcu_dereference(clnt->cl_xprt)->xprt_net;
1423 rcu_read_unlock();
1424 return ret;
1425}
1426EXPORT_SYMBOL_GPL(rpc_net_ns);
1427
1428/**
1429 * rpc_max_payload - Get maximum payload size for a transport, in bytes
1430 * @clnt: RPC client to query
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 *
1432 * For stream transports, this is one RPC record fragment (see RFC
1433 * 1831), as we don't support multi-record requests yet. For datagram
1434 * transports, this is the size of an IP packet minus the IP, UDP, and
1435 * RPC header sizes.
1436 */
1437size_t rpc_max_payload(struct rpc_clnt *clnt)
1438{
Trond Myklebust2446ab62012-03-01 17:00:56 -05001439 size_t ret;
1440
1441 rcu_read_lock();
1442 ret = rcu_dereference(clnt->cl_xprt)->max_payload;
1443 rcu_read_unlock();
1444 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445}
Chuck Leverb86acd52006-08-22 20:06:22 -04001446EXPORT_SYMBOL_GPL(rpc_max_payload);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
Chuck Lever35f5a422006-01-03 09:55:50 +01001448/**
Chuck Lever6b26cc82016-05-02 14:40:40 -04001449 * rpc_max_bc_payload - Get maximum backchannel payload size, in bytes
1450 * @clnt: RPC client to query
1451 */
1452size_t rpc_max_bc_payload(struct rpc_clnt *clnt)
1453{
1454 struct rpc_xprt *xprt;
1455 size_t ret;
1456
1457 rcu_read_lock();
1458 xprt = rcu_dereference(clnt->cl_xprt);
1459 ret = xprt->ops->bc_maxpayload(xprt);
1460 rcu_read_unlock();
1461 return ret;
1462}
1463EXPORT_SYMBOL_GPL(rpc_max_bc_payload);
1464
1465/**
Chuck Lever35f5a422006-01-03 09:55:50 +01001466 * rpc_force_rebind - force transport to check that remote port is unchanged
1467 * @clnt: client to rebind
1468 *
1469 */
1470void rpc_force_rebind(struct rpc_clnt *clnt)
1471{
Trond Myklebust2446ab62012-03-01 17:00:56 -05001472 if (clnt->cl_autobind) {
1473 rcu_read_lock();
1474 xprt_clear_bound(rcu_dereference(clnt->cl_xprt));
1475 rcu_read_unlock();
1476 }
Chuck Lever35f5a422006-01-03 09:55:50 +01001477}
Chuck Leverb86acd52006-08-22 20:06:22 -04001478EXPORT_SYMBOL_GPL(rpc_force_rebind);
Chuck Lever35f5a422006-01-03 09:55:50 +01001479
Trond Myklebust9e6fa0b2019-04-07 13:58:45 -04001480static int
1481__rpc_restart_call(struct rpc_task *task, void (*action)(struct rpc_task *))
Andy Adamsonaae20062009-04-01 09:22:40 -04001482{
Trond Myklebust494314c2014-03-20 12:59:09 -04001483 task->tk_status = 0;
Trond Myklebust5ad64b32019-04-07 13:58:54 -04001484 task->tk_rpc_status = 0;
Trond Myklebust9e6fa0b2019-04-07 13:58:45 -04001485 task->tk_action = action;
Trond Myklebustf1f88fc2010-07-31 14:29:07 -04001486 return 1;
Andy Adamsonaae20062009-04-01 09:22:40 -04001487}
Andy Adamsonaae20062009-04-01 09:22:40 -04001488
1489/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 * Restart an (async) RPC call. Usually called from within the
1491 * exit handler.
1492 */
Trond Myklebustf1f88fc2010-07-31 14:29:07 -04001493int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494rpc_restart_call(struct rpc_task *task)
1495{
Trond Myklebust9e6fa0b2019-04-07 13:58:45 -04001496 return __rpc_restart_call(task, call_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497}
Trond Myklebuste8914c62007-07-14 15:39:59 -04001498EXPORT_SYMBOL_GPL(rpc_restart_call);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499
Trond Myklebust9e6fa0b2019-04-07 13:58:45 -04001500/*
1501 * Restart an (async) RPC call from the call_prepare state.
1502 * Usually called from within the exit handler.
1503 */
1504int
1505rpc_restart_call_prepare(struct rpc_task *task)
1506{
1507 if (task->tk_ops->rpc_call_prepare != NULL)
1508 return __rpc_restart_call(task, rpc_prepare_task);
1509 return rpc_restart_call(task);
1510}
1511EXPORT_SYMBOL_GPL(rpc_restart_call_prepare);
1512
Jeff Laytonb4b9d2c2014-11-26 14:44:43 -05001513const char
1514*rpc_proc_name(const struct rpc_task *task)
Chuck Lever3748f1e2008-05-21 17:09:12 -04001515{
1516 const struct rpc_procinfo *proc = task->tk_msg.rpc_proc;
1517
1518 if (proc) {
1519 if (proc->p_name)
1520 return proc->p_name;
1521 else
1522 return "NULL";
1523 } else
1524 return "no proc";
1525}
Chuck Lever3748f1e2008-05-21 17:09:12 -04001526
Trond Myklebust5ad64b32019-04-07 13:58:54 -04001527static void
1528__rpc_call_rpcerror(struct rpc_task *task, int tk_status, int rpc_status)
1529{
1530 task->tk_rpc_status = rpc_status;
1531 rpc_exit(task, tk_status);
1532}
1533
1534static void
1535rpc_call_rpcerror(struct rpc_task *task, int status)
1536{
1537 __rpc_call_rpcerror(task, status, status);
1538}
1539
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540/*
1541 * 0. Initial state
1542 *
1543 * Other FSM states can be visited zero or more times, but
1544 * this state is visited exactly once for each RPC.
1545 */
1546static void
1547call_start(struct rpc_task *task)
1548{
1549 struct rpc_clnt *clnt = task->tk_client;
Christoph Hellwig1c5876d2017-05-08 23:27:10 +02001550 int idx = task->tk_msg.rpc_proc->p_statidx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551
Chuck Leverc435da62017-11-03 13:46:14 -04001552 trace_rpc_request(task);
Chuck Lever3748f1e2008-05-21 17:09:12 -04001553 dprintk("RPC: %5u call_start %s%d proc %s (%s)\n", task->tk_pid,
Trond Myklebust55909f22013-08-23 11:48:15 -04001554 clnt->cl_program->name, clnt->cl_vers,
Chuck Lever3748f1e2008-05-21 17:09:12 -04001555 rpc_proc_name(task),
Chuck Lever46121cf2007-01-31 12:14:08 -05001556 (RPC_IS_ASYNC(task) ? "async" : "sync"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
Christoph Hellwig1c5876d2017-05-08 23:27:10 +02001558 /* Increment call count (version might not be valid for ping) */
1559 if (clnt->cl_program->version[clnt->cl_vers])
1560 clnt->cl_program->version[clnt->cl_vers]->counts[idx]++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 clnt->cl_stats->rpccnt++;
1562 task->tk_action = call_reserve;
Bill Baker0f90be12018-06-19 16:24:58 -05001563 rpc_task_set_transport(task, clnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564}
1565
1566/*
1567 * 1. Reserve an RPC call slot
1568 */
1569static void
1570call_reserve(struct rpc_task *task)
1571{
Chuck Lever46121cf2007-01-31 12:14:08 -05001572 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 task->tk_status = 0;
1575 task->tk_action = call_reserveresult;
1576 xprt_reserve(task);
1577}
1578
Trond Myklebustba60eb22013-04-14 10:49:37 -04001579static void call_retry_reserve(struct rpc_task *task);
1580
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581/*
1582 * 1b. Grok the result of xprt_reserve()
1583 */
1584static void
1585call_reserveresult(struct rpc_task *task)
1586{
1587 int status = task->tk_status;
1588
Chuck Lever46121cf2007-01-31 12:14:08 -05001589 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590
1591 /*
1592 * After a call to xprt_reserve(), we must have either
1593 * a request slot or else an error status.
1594 */
1595 task->tk_status = 0;
1596 if (status >= 0) {
1597 if (task->tk_rqstp) {
J. Bruce Fieldsf2d47d02010-09-12 19:55:25 -04001598 task->tk_action = call_refresh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 return;
1600 }
1601
1602 printk(KERN_ERR "%s: status=%d, but no request slot, exiting\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08001603 __func__, status);
Trond Myklebust5ad64b32019-04-07 13:58:54 -04001604 rpc_call_rpcerror(task, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 return;
1606 }
1607
1608 /*
1609 * Even though there was an error, we may have acquired
1610 * a request slot somehow. Make sure not to leak it.
1611 */
1612 if (task->tk_rqstp) {
1613 printk(KERN_ERR "%s: status=%d, request allocated anyway\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08001614 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 xprt_release(task);
1616 }
1617
1618 switch (status) {
Trond Myklebust1afeaf52012-05-19 12:12:53 -04001619 case -ENOMEM:
1620 rpc_delay(task, HZ >> 2);
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05001621 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 case -EAGAIN: /* woken up; retry */
Trond Myklebustba60eb22013-04-14 10:49:37 -04001623 task->tk_action = call_retry_reserve;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 return;
1625 case -EIO: /* probably a shutdown */
1626 break;
1627 default:
1628 printk(KERN_ERR "%s: unrecognized error %d, exiting\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -08001629 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 break;
1631 }
Trond Myklebust5ad64b32019-04-07 13:58:54 -04001632 rpc_call_rpcerror(task, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633}
1634
1635/*
Trond Myklebustba60eb22013-04-14 10:49:37 -04001636 * 1c. Retry reserving an RPC call slot
1637 */
1638static void
1639call_retry_reserve(struct rpc_task *task)
1640{
1641 dprint_status(task);
1642
1643 task->tk_status = 0;
1644 task->tk_action = call_reserveresult;
1645 xprt_retry_reserve(task);
1646}
1647
1648/*
J. Bruce Fields55576242010-09-12 19:55:25 -04001649 * 2. Bind and/or refresh the credentials
1650 */
1651static void
1652call_refresh(struct rpc_task *task)
1653{
1654 dprint_status(task);
1655
1656 task->tk_action = call_refreshresult;
1657 task->tk_status = 0;
1658 task->tk_client->cl_stats->rpcauthrefresh++;
1659 rpcauth_refreshcred(task);
1660}
1661
1662/*
1663 * 2a. Process the results of a credential refresh
1664 */
1665static void
1666call_refreshresult(struct rpc_task *task)
1667{
1668 int status = task->tk_status;
1669
1670 dprint_status(task);
1671
1672 task->tk_status = 0;
Trond Myklebust5fc43972010-11-20 11:13:31 -05001673 task->tk_action = call_refresh;
J. Bruce Fields55576242010-09-12 19:55:25 -04001674 switch (status) {
Trond Myklebust5fc43972010-11-20 11:13:31 -05001675 case 0:
Weston Andros Adamson6ff33b72013-12-17 12:16:11 -05001676 if (rpcauth_uptodatecred(task)) {
Trond Myklebust5fc43972010-11-20 11:13:31 -05001677 task->tk_action = call_allocate;
Weston Andros Adamson6ff33b72013-12-17 12:16:11 -05001678 return;
1679 }
1680 /* Use rate-limiting and a max number of retries if refresh
1681 * had status 0 but failed to update the cred.
1682 */
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05001683 /* fall through */
J. Bruce Fields55576242010-09-12 19:55:25 -04001684 case -ETIMEDOUT:
1685 rpc_delay(task, 3*HZ);
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05001686 /* fall through */
Trond Myklebust5fc43972010-11-20 11:13:31 -05001687 case -EAGAIN:
1688 status = -EACCES;
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05001689 /* fall through */
Andy Adamsonf1ff0c22013-08-14 11:59:13 -04001690 case -EKEYEXPIRED:
Trond Myklebust5fc43972010-11-20 11:13:31 -05001691 if (!task->tk_cred_retry)
1692 break;
1693 task->tk_cred_retry--;
1694 dprintk("RPC: %5u %s: retry refresh creds\n",
1695 task->tk_pid, __func__);
1696 return;
J. Bruce Fields55576242010-09-12 19:55:25 -04001697 }
Trond Myklebust5fc43972010-11-20 11:13:31 -05001698 dprintk("RPC: %5u %s: refresh creds failed with error %d\n",
1699 task->tk_pid, __func__, status);
Trond Myklebust5ad64b32019-04-07 13:58:54 -04001700 rpc_call_rpcerror(task, status);
J. Bruce Fields55576242010-09-12 19:55:25 -04001701}
1702
1703/*
1704 * 2b. Allocate the buffer. For details, see sched.c:rpc_malloc.
Chuck Lever02107142006-01-03 09:55:49 +01001705 * (Note: buffer memory is freed in xprt_release).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 */
1707static void
1708call_allocate(struct rpc_task *task)
1709{
Chuck Lever2c94b8e2019-02-11 11:25:41 -05001710 const struct rpc_auth *auth = task->tk_rqstp->rq_cred->cr_auth;
Chuck Lever02107142006-01-03 09:55:49 +01001711 struct rpc_rqst *req = task->tk_rqstp;
Trond Myklebusta4f08352013-01-08 09:10:21 -05001712 struct rpc_xprt *xprt = req->rq_xprt;
Christoph Hellwig499b4982017-05-12 15:36:49 +02001713 const struct rpc_procinfo *proc = task->tk_msg.rpc_proc;
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04001714 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715
Chuck Lever46121cf2007-01-31 12:14:08 -05001716 dprint_status(task);
1717
Chuck Lever2bea90d2007-03-29 16:47:53 -04001718 task->tk_status = 0;
Trond Myklebust762e4e62018-08-24 16:28:28 -04001719 task->tk_action = call_encode;
Chuck Lever2bea90d2007-03-29 16:47:53 -04001720
Trond Myklebustaf6b61d2019-04-11 15:16:52 -04001721 if (req->rq_buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 return;
1723
Chuck Lever2bea90d2007-03-29 16:47:53 -04001724 if (proc->p_proc != 0) {
1725 BUG_ON(proc->p_arglen == 0);
1726 if (proc->p_decode != NULL)
1727 BUG_ON(proc->p_replen == 0);
1728 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729
Chuck Lever2bea90d2007-03-29 16:47:53 -04001730 /*
1731 * Calculate the size (in quads) of the RPC call
1732 * and reply headers, and convert both values
1733 * to byte sizes.
1734 */
Chuck Lever2c94b8e2019-02-11 11:25:41 -05001735 req->rq_callsize = RPC_CALLHDRSIZE + (auth->au_cslack << 1) +
1736 proc->p_arglen;
Chuck Lever2bea90d2007-03-29 16:47:53 -04001737 req->rq_callsize <<= 2;
Trond Myklebust51314962019-03-15 12:55:59 -04001738 /*
1739 * Note: the reply buffer must at minimum allocate enough space
1740 * for the 'struct accepted_reply' from RFC5531.
1741 */
1742 req->rq_rcvsize = RPC_REPHDRSIZE + auth->au_rslack + \
1743 max_t(size_t, proc->p_replen, 2);
Chuck Lever2bea90d2007-03-29 16:47:53 -04001744 req->rq_rcvsize <<= 2;
1745
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04001746 status = xprt->ops->buf_alloc(task);
Chuck Lever4a068252015-05-11 14:02:25 -04001747 xprt_inject_disconnect(xprt);
Trond Myklebustaf6b61d2019-04-11 15:16:52 -04001748 if (status == 0)
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04001749 return;
1750 if (status != -ENOMEM) {
Trond Myklebust5ad64b32019-04-07 13:58:54 -04001751 rpc_call_rpcerror(task, status);
Chuck Lever5fe6eaa2016-09-15 10:55:20 -04001752 return;
1753 }
Chuck Lever46121cf2007-01-31 12:14:08 -05001754
1755 dprintk("RPC: %5u rpc_buffer allocation failed\n", task->tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756
Trond Myklebust5afa91332011-06-17 10:14:59 -04001757 if (RPC_IS_ASYNC(task) || !fatal_signal_pending(current)) {
Trond Myklebustb6e9c712007-10-01 12:06:44 -04001758 task->tk_action = call_allocate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 rpc_delay(task, HZ>>4);
1760 return;
1761 }
1762
1763 rpc_exit(task, -ERESTARTSYS);
1764}
1765
Trond Myklebust7ebbbc62018-08-28 09:00:27 -04001766static int
Trond Myklebust940e3312005-11-09 21:45:24 -05001767rpc_task_need_encode(struct rpc_task *task)
1768{
Trond Myklebust762e4e62018-08-24 16:28:28 -04001769 return test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate) == 0 &&
1770 (!(task->tk_flags & RPC_TASK_SENT) ||
1771 !(task->tk_flags & RPC_TASK_NO_RETRANS_TIMEOUT) ||
1772 xprt_request_need_retransmit(task));
Trond Myklebust940e3312005-11-09 21:45:24 -05001773}
1774
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775static void
Chuck Leverb0e1c572008-05-21 17:09:19 -04001776rpc_xdr_encode(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 struct rpc_rqst *req = task->tk_rqstp;
Chuck Levere8680a22019-02-11 11:24:48 -05001779 struct xdr_stream xdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780
Chuck Leverb9c5bc02016-09-15 10:55:12 -04001781 xdr_buf_init(&req->rq_snd_buf,
1782 req->rq_buffer,
1783 req->rq_callsize);
1784 xdr_buf_init(&req->rq_rcv_buf,
Chuck Lever68778942016-09-15 10:55:37 -04001785 req->rq_rbuffer,
Chuck Leverb9c5bc02016-09-15 10:55:12 -04001786 req->rq_rcvsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787
Chuck Levere8680a22019-02-11 11:24:48 -05001788 req->rq_snd_buf.head[0].iov_len = 0;
1789 xdr_init_encode(&xdr, &req->rq_snd_buf,
1790 req->rq_snd_buf.head[0].iov_base, req);
1791 if (rpc_encode_header(task, &xdr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 return;
Chuck Leverb0e1c572008-05-21 17:09:19 -04001793
Chuck Levere8680a22019-02-11 11:24:48 -05001794 task->tk_status = rpcauth_wrap_req(task, &xdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795}
1796
1797/*
Trond Myklebust762e4e62018-08-24 16:28:28 -04001798 * 3. Encode arguments of an RPC call
1799 */
1800static void
1801call_encode(struct rpc_task *task)
1802{
1803 if (!rpc_task_need_encode(task))
1804 goto out;
Chuck Levere8680a22019-02-11 11:24:48 -05001805 dprint_status(task);
Trond Myklebust762e4e62018-08-24 16:28:28 -04001806 /* Encode here so that rpcsec_gss can use correct sequence number. */
1807 rpc_xdr_encode(task);
1808 /* Did the encode result in an error condition? */
1809 if (task->tk_status != 0) {
1810 /* Was the error nonfatal? */
Trond Myklebust97b78ae2019-01-02 17:53:13 -05001811 switch (task->tk_status) {
1812 case -EAGAIN:
1813 case -ENOMEM:
Trond Myklebust762e4e62018-08-24 16:28:28 -04001814 rpc_delay(task, HZ >> 4);
Trond Myklebust97b78ae2019-01-02 17:53:13 -05001815 break;
1816 case -EKEYEXPIRED:
ZhangXiaoxu9c5948c2019-04-29 17:32:31 +08001817 if (!task->tk_cred_retry) {
1818 rpc_exit(task, task->tk_status);
1819 } else {
1820 task->tk_action = call_refresh;
1821 task->tk_cred_retry--;
1822 dprintk("RPC: %5u %s: retry refresh creds\n",
1823 task->tk_pid, __func__);
1824 }
Trond Myklebust97b78ae2019-01-02 17:53:13 -05001825 break;
1826 default:
Trond Myklebust5ad64b32019-04-07 13:58:54 -04001827 rpc_call_rpcerror(task, task->tk_status);
Trond Myklebust97b78ae2019-01-02 17:53:13 -05001828 }
Trond Myklebust762e4e62018-08-24 16:28:28 -04001829 return;
Chuck Levere8680a22019-02-11 11:24:48 -05001830 } else {
1831 xprt_request_prepare(task->tk_rqstp);
Trond Myklebust762e4e62018-08-24 16:28:28 -04001832 }
1833
1834 /* Add task to reply queue before transmission to avoid races */
1835 if (rpc_reply_expected(task))
1836 xprt_request_enqueue_receive(task);
1837 xprt_request_enqueue_transmit(task);
1838out:
Trond Myklebustaf6b61d2019-04-11 15:16:52 -04001839 task->tk_action = call_transmit;
1840 /* Check that the connection is OK */
1841 if (!xprt_bound(task->tk_xprt))
1842 task->tk_action = call_bind;
1843 else if (!xprt_connected(task->tk_xprt))
1844 task->tk_action = call_connect;
Trond Myklebust762e4e62018-08-24 16:28:28 -04001845}
1846
1847/*
Trond Myklebust03e51d32019-03-10 11:17:13 -04001848 * Helpers to check if the task was already transmitted, and
1849 * to take action when that is the case.
1850 */
1851static bool
1852rpc_task_transmitted(struct rpc_task *task)
1853{
1854 return !test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate);
1855}
1856
1857static void
1858rpc_task_handle_transmitted(struct rpc_task *task)
1859{
1860 xprt_end_transmit(task);
1861 task->tk_action = call_transmit_status;
Trond Myklebust03e51d32019-03-10 11:17:13 -04001862}
1863
1864/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 * 4. Get the server port number if not yet set
1866 */
1867static void
1868call_bind(struct rpc_task *task)
1869{
Trond Myklebustad2368d2013-01-08 10:08:33 -05001870 struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871
Trond Myklebust03e51d32019-03-10 11:17:13 -04001872 if (rpc_task_transmitted(task)) {
1873 rpc_task_handle_transmitted(task);
1874 return;
1875 }
1876
Trond Myklebust009a82f2019-03-09 12:07:17 -05001877 if (xprt_bound(xprt)) {
1878 task->tk_action = call_connect;
Trond Myklebust009a82f2019-03-09 12:07:17 -05001879 return;
1880 }
1881
Chuck Lever46121cf2007-01-31 12:14:08 -05001882 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883
Trond Myklebust009a82f2019-03-09 12:07:17 -05001884 task->tk_action = call_bind_status;
Trond Myklebust4d6c6712019-03-10 12:22:39 -04001885 if (!xprt_prepare_transmit(task))
1886 return;
1887
Trond Myklebust009a82f2019-03-09 12:07:17 -05001888 xprt->ops->rpcbind(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889}
1890
1891/*
Chuck Leverda351872005-08-11 16:25:11 -04001892 * 4a. Sort out bind result
1893 */
1894static void
1895call_bind_status(struct rpc_task *task)
1896{
Chuck Lever906462a2007-09-11 18:00:47 -04001897 int status = -EIO;
Chuck Leverda351872005-08-11 16:25:11 -04001898
Trond Myklebust03e51d32019-03-10 11:17:13 -04001899 if (rpc_task_transmitted(task)) {
1900 rpc_task_handle_transmitted(task);
1901 return;
1902 }
1903
Chuck Leverda351872005-08-11 16:25:11 -04001904 if (task->tk_status >= 0) {
Chuck Lever46121cf2007-01-31 12:14:08 -05001905 dprint_status(task);
Chuck Leverda351872005-08-11 16:25:11 -04001906 task->tk_status = 0;
1907 task->tk_action = call_connect;
1908 return;
1909 }
1910
Steve Dickson5753cba2012-02-06 10:08:08 -05001911 trace_rpc_bind_status(task);
Chuck Leverda351872005-08-11 16:25:11 -04001912 switch (task->tk_status) {
Trond Myklebust381ba742008-07-07 12:18:53 -04001913 case -ENOMEM:
1914 dprintk("RPC: %5u rpcbind out of memory\n", task->tk_pid);
1915 rpc_delay(task, HZ >> 2);
Chuck Lever2429cbf2007-09-11 18:00:41 -04001916 goto retry_timeout;
Chuck Leverda351872005-08-11 16:25:11 -04001917 case -EACCES:
Chuck Lever46121cf2007-01-31 12:14:08 -05001918 dprintk("RPC: %5u remote rpcbind: RPC program/version "
1919 "unavailable\n", task->tk_pid);
Chuck Leverb79dc8c2007-09-11 18:00:52 -04001920 /* fail immediately if this is an RPC ping */
1921 if (task->tk_msg.rpc_proc->p_proc == 0) {
1922 status = -EOPNOTSUPP;
1923 break;
1924 }
Trond Myklebust0b760112011-05-31 15:15:34 -04001925 if (task->tk_rebind_retry == 0)
1926 break;
1927 task->tk_rebind_retry--;
Chuck Leverea635a52005-10-06 23:12:58 -04001928 rpc_delay(task, 3*HZ);
Trond Myklebustda45828e2006-08-31 15:44:52 -04001929 goto retry_timeout;
Trond Myklebust4d6c6712019-03-10 12:22:39 -04001930 case -EAGAIN:
1931 goto retry_timeout;
Chuck Leverda351872005-08-11 16:25:11 -04001932 case -ETIMEDOUT:
Chuck Lever46121cf2007-01-31 12:14:08 -05001933 dprintk("RPC: %5u rpcbind request timed out\n",
Chuck Leverda351872005-08-11 16:25:11 -04001934 task->tk_pid);
Trond Myklebustda45828e2006-08-31 15:44:52 -04001935 goto retry_timeout;
Chuck Leverda351872005-08-11 16:25:11 -04001936 case -EPFNOSUPPORT:
Chuck Lever906462a2007-09-11 18:00:47 -04001937 /* server doesn't support any rpcbind version we know of */
Chuck Lever012da152009-12-03 15:58:56 -05001938 dprintk("RPC: %5u unrecognized remote rpcbind service\n",
Chuck Leverda351872005-08-11 16:25:11 -04001939 task->tk_pid);
1940 break;
1941 case -EPROTONOSUPPORT:
Chuck Lever00a6e7b2007-03-29 16:48:33 -04001942 dprintk("RPC: %5u remote rpcbind version unavailable, retrying\n",
Chuck Leverda351872005-08-11 16:25:11 -04001943 task->tk_pid);
Trond Myklebustfdb63dc2014-03-17 12:57:31 -04001944 goto retry_timeout;
Chuck Lever012da152009-12-03 15:58:56 -05001945 case -ECONNREFUSED: /* connection problems */
1946 case -ECONNRESET:
Trond Myklebustdf277272013-12-31 13:11:43 -05001947 case -ECONNABORTED:
Chuck Lever012da152009-12-03 15:58:56 -05001948 case -ENOTCONN:
1949 case -EHOSTDOWN:
Trond Myklebusteb5b46f2017-11-30 07:21:33 -05001950 case -ENETDOWN:
Chuck Lever012da152009-12-03 15:58:56 -05001951 case -EHOSTUNREACH:
1952 case -ENETUNREACH:
Trond Myklebust3601c4a2014-06-30 13:42:19 -04001953 case -ENOBUFS:
Chuck Lever012da152009-12-03 15:58:56 -05001954 case -EPIPE:
1955 dprintk("RPC: %5u remote rpcbind unreachable: %d\n",
1956 task->tk_pid, task->tk_status);
1957 if (!RPC_IS_SOFTCONN(task)) {
1958 rpc_delay(task, 5*HZ);
1959 goto retry_timeout;
1960 }
1961 status = task->tk_status;
1962 break;
Chuck Leverda351872005-08-11 16:25:11 -04001963 default:
Chuck Lever46121cf2007-01-31 12:14:08 -05001964 dprintk("RPC: %5u unrecognized rpcbind error (%d)\n",
Chuck Leverda351872005-08-11 16:25:11 -04001965 task->tk_pid, -task->tk_status);
Chuck Leverda351872005-08-11 16:25:11 -04001966 }
1967
Trond Myklebust5ad64b32019-04-07 13:58:54 -04001968 rpc_call_rpcerror(task, status);
Chuck Leverda351872005-08-11 16:25:11 -04001969 return;
1970
Trond Myklebustda45828e2006-08-31 15:44:52 -04001971retry_timeout:
Trond Myklebustfdb63dc2014-03-17 12:57:31 -04001972 task->tk_status = 0;
Trond Myklebust4d6c6712019-03-10 12:22:39 -04001973 task->tk_action = call_bind;
Trond Myklebustcea57782019-03-09 16:06:47 -05001974 rpc_check_timeout(task);
Chuck Leverda351872005-08-11 16:25:11 -04001975}
1976
1977/*
1978 * 4b. Connect to the RPC server
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 */
1980static void
1981call_connect(struct rpc_task *task)
1982{
Trond Myklebustad2368d2013-01-08 10:08:33 -05001983 struct rpc_xprt *xprt = task->tk_rqstp->rq_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984
Trond Myklebust03e51d32019-03-10 11:17:13 -04001985 if (rpc_task_transmitted(task)) {
1986 rpc_task_handle_transmitted(task);
1987 return;
1988 }
1989
Trond Myklebust009a82f2019-03-09 12:07:17 -05001990 if (xprt_connected(xprt)) {
1991 task->tk_action = call_transmit;
Trond Myklebust009a82f2019-03-09 12:07:17 -05001992 return;
1993 }
1994
Chuck Lever46121cf2007-01-31 12:14:08 -05001995 dprintk("RPC: %5u call_connect xprt %p %s connected\n",
Chuck Leverda351872005-08-11 16:25:11 -04001996 task->tk_pid, xprt,
1997 (xprt_connected(xprt) ? "is" : "is not"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998
Trond Myklebust009a82f2019-03-09 12:07:17 -05001999 task->tk_action = call_connect_status;
2000 if (task->tk_status < 0)
2001 return;
2002 if (task->tk_flags & RPC_TASK_NOCONNECT) {
Trond Myklebust5ad64b32019-04-07 13:58:54 -04002003 rpc_call_rpcerror(task, -ENOTCONN);
Trond Myklebust009a82f2019-03-09 12:07:17 -05002004 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 }
Trond Myklebust4d6c6712019-03-10 12:22:39 -04002006 if (!xprt_prepare_transmit(task))
2007 return;
Trond Myklebust009a82f2019-03-09 12:07:17 -05002008 xprt_connect(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009}
2010
2011/*
Chuck Leverda351872005-08-11 16:25:11 -04002012 * 4c. Sort out connect result
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 */
2014static void
2015call_connect_status(struct rpc_task *task)
2016{
2017 struct rpc_clnt *clnt = task->tk_client;
2018 int status = task->tk_status;
2019
Trond Myklebust03e51d32019-03-10 11:17:13 -04002020 if (rpc_task_transmitted(task)) {
2021 rpc_task_handle_transmitted(task);
Trond Myklebust9bd11522018-11-30 12:48:47 -05002022 return;
2023 }
2024
Chuck Lever46121cf2007-01-31 12:14:08 -05002025 dprint_status(task);
Chuck Leverda351872005-08-11 16:25:11 -04002026
Chuck Levere671edb2018-03-16 10:33:44 -04002027 trace_rpc_connect_status(task);
Trond Myklebust561ec162013-09-26 15:22:45 -04002028 task->tk_status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 switch (status) {
Trond Myklebust3ed5e2a2013-03-04 17:29:33 -05002030 case -ECONNREFUSED:
NeilBrownfd01b252017-08-18 17:12:51 +10002031 /* A positive refusal suggests a rebind is needed. */
2032 if (RPC_IS_SOFTCONN(task))
2033 break;
2034 if (clnt->cl_autobind) {
2035 rpc_force_rebind(clnt);
Trond Myklebust7b3fef82019-03-07 14:10:32 -05002036 goto out_retry;
NeilBrownfd01b252017-08-18 17:12:51 +10002037 }
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05002038 /* fall through */
Trond Myklebust3ed5e2a2013-03-04 17:29:33 -05002039 case -ECONNRESET:
Trond Myklebustdf277272013-12-31 13:11:43 -05002040 case -ECONNABORTED:
Trond Myklebusteb5b46f2017-11-30 07:21:33 -05002041 case -ENETDOWN:
Trond Myklebust3ed5e2a2013-03-04 17:29:33 -05002042 case -ENETUNREACH:
Trond Myklebustdf277272013-12-31 13:11:43 -05002043 case -EHOSTUNREACH:
Trond Myklebust3913c782015-02-08 21:44:04 -05002044 case -EADDRINUSE:
Trond Myklebust3601c4a2014-06-30 13:42:19 -04002045 case -ENOBUFS:
Trond Myklebust2fc193c2014-07-03 00:02:57 -04002046 case -EPIPE:
NeilBrown2c2ee6d2016-11-23 14:44:58 +11002047 xprt_conditional_disconnect(task->tk_rqstp->rq_xprt,
2048 task->tk_rqstp->rq_connect_cookie);
Trond Myklebust3ed5e2a2013-03-04 17:29:33 -05002049 if (RPC_IS_SOFTCONN(task))
2050 break;
Steve Dickson1fa3e2e2014-03-20 11:23:03 -04002051 /* retry with existing socket, after a delay */
2052 rpc_delay(task, 3*HZ);
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05002053 /* fall through */
Trond Myklebust0445f922018-12-17 13:34:59 -05002054 case -ENOTCONN:
Trond Myklebust3ed5e2a2013-03-04 17:29:33 -05002055 case -EAGAIN:
Trond Myklebust485f2252014-03-17 12:51:44 -04002056 case -ETIMEDOUT:
Trond Myklebust7b3fef82019-03-07 14:10:32 -05002057 goto out_retry;
Trond Myklebust561ec162013-09-26 15:22:45 -04002058 case 0:
Trond Myklebust3ed5e2a2013-03-04 17:29:33 -05002059 clnt->cl_stats->netreconn++;
2060 task->tk_action = call_transmit;
2061 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 }
Trond Myklebust5ad64b32019-04-07 13:58:54 -04002063 rpc_call_rpcerror(task, status);
Trond Myklebust7b3fef82019-03-07 14:10:32 -05002064 return;
2065out_retry:
2066 /* Check for timeouts before looping back to call_bind */
2067 task->tk_action = call_bind;
2068 rpc_check_timeout(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069}
2070
2071/*
2072 * 5. Transmit the RPC request, and wait for reply
2073 */
2074static void
2075call_transmit(struct rpc_task *task)
2076{
Trond Myklebust03e51d32019-03-10 11:17:13 -04002077 if (rpc_task_transmitted(task)) {
2078 rpc_task_handle_transmitted(task);
2079 return;
2080 }
2081
Chuck Lever46121cf2007-01-31 12:14:08 -05002082 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083
Trond Myklebusted7dc972019-03-04 14:19:31 -05002084 task->tk_action = call_transmit_status;
Trond Myklebust009a82f2019-03-09 12:07:17 -05002085 if (!xprt_prepare_transmit(task))
2086 return;
2087 task->tk_status = 0;
Trond Myklebustc5445772018-09-03 23:39:27 -04002088 if (test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate)) {
Trond Myklebust009a82f2019-03-09 12:07:17 -05002089 if (!xprt_connected(task->tk_xprt)) {
2090 task->tk_status = -ENOTCONN;
Trond Myklebustc5445772018-09-03 23:39:27 -04002091 return;
Trond Myklebusted7dc972019-03-04 14:19:31 -05002092 }
Trond Myklebust009a82f2019-03-09 12:07:17 -05002093 xprt_transmit(task);
Trond Myklebustc5445772018-09-03 23:39:27 -04002094 }
Trond Myklebustc5445772018-09-03 23:39:27 -04002095 xprt_end_transmit(task);
Trond Myklebuste0ab53d2006-07-27 17:22:50 -04002096}
2097
2098/*
2099 * 5a. Handle cleanup after a transmission
2100 */
2101static void
2102call_transmit_status(struct rpc_task *task)
2103{
2104 task->tk_action = call_status;
Chuck Lever206a1342009-12-03 15:58:56 -05002105
2106 /*
2107 * Common case: success. Force the compiler to put this
2108 * test first.
2109 */
Trond Myklebust009a82f2019-03-09 12:07:17 -05002110 if (rpc_task_transmitted(task)) {
Trond Myklebusta7b1a482019-04-15 11:54:13 -04002111 task->tk_status = 0;
2112 xprt_request_wait_receive(task);
Chuck Lever206a1342009-12-03 15:58:56 -05002113 return;
2114 }
2115
Trond Myklebust15f081c2009-03-11 14:37:57 -04002116 switch (task->tk_status) {
Trond Myklebust15f081c2009-03-11 14:37:57 -04002117 default:
Chuck Lever206a1342009-12-03 15:58:56 -05002118 dprint_status(task);
Trond Myklebust75891f52018-09-03 17:37:36 -04002119 break;
Trond Myklebust78b576c2018-08-28 09:20:10 -04002120 case -EBADMSG:
Trond Myklebust762e4e62018-08-24 16:28:28 -04002121 task->tk_status = 0;
2122 task->tk_action = call_encode;
Trond Myklebust78b576c2018-08-28 09:20:10 -04002123 break;
Trond Myklebust15f081c2009-03-11 14:37:57 -04002124 /*
2125 * Special cases: if we've been waiting on the
2126 * socket's write_space() callback, or if the
2127 * socket just returned a connection error,
2128 * then hold onto the transport lock.
2129 */
Trond Myklebust78b576c2018-08-28 09:20:10 -04002130 case -ENOBUFS:
2131 rpc_delay(task, HZ>>2);
2132 /* fall through */
Trond Myklebustc5445772018-09-03 23:39:27 -04002133 case -EBADSLT:
Trond Myklebust78b576c2018-08-28 09:20:10 -04002134 case -EAGAIN:
2135 task->tk_action = call_transmit;
2136 task->tk_status = 0;
2137 break;
Trond Myklebust15f081c2009-03-11 14:37:57 -04002138 case -ECONNREFUSED:
Trond Myklebust15f081c2009-03-11 14:37:57 -04002139 case -EHOSTDOWN:
Trond Myklebusteb5b46f2017-11-30 07:21:33 -05002140 case -ENETDOWN:
Trond Myklebust15f081c2009-03-11 14:37:57 -04002141 case -EHOSTUNREACH:
2142 case -ENETUNREACH:
Jason Baron3dedbb52014-09-24 18:08:04 +00002143 case -EPERM:
Chuck Lever09a21c42009-12-03 15:58:56 -05002144 if (RPC_IS_SOFTCONN(task)) {
Chuck Levera25a4cb2018-03-16 10:33:55 -04002145 if (!task->tk_msg.rpc_proc->p_proc)
2146 trace_xprt_ping(task->tk_xprt,
2147 task->tk_status);
Trond Myklebust5ad64b32019-04-07 13:58:54 -04002148 rpc_call_rpcerror(task, task->tk_status);
Trond Myklebust7b3fef82019-03-07 14:10:32 -05002149 return;
Chuck Lever09a21c42009-12-03 15:58:56 -05002150 }
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05002151 /* fall through */
Chuck Lever09a21c42009-12-03 15:58:56 -05002152 case -ECONNRESET:
Trond Myklebustdf277272013-12-31 13:11:43 -05002153 case -ECONNABORTED:
Trond Myklebust3913c782015-02-08 21:44:04 -05002154 case -EADDRINUSE:
Chuck Lever09a21c42009-12-03 15:58:56 -05002155 case -ENOTCONN:
Trond Myklebustc8485e42009-03-11 14:37:59 -04002156 case -EPIPE:
Trond Myklebusted7dc972019-03-04 14:19:31 -05002157 task->tk_action = call_bind;
2158 task->tk_status = 0;
Trond Myklebust7ebbbc62018-08-28 09:00:27 -04002159 break;
Trond Myklebust15f081c2009-03-11 14:37:57 -04002160 }
Trond Myklebust7b3fef82019-03-07 14:10:32 -05002161 rpc_check_timeout(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162}
2163
Trond Myklebust9e00abc2011-07-13 19:20:49 -04002164#if defined(CONFIG_SUNRPC_BACKCHANNEL)
Trond Myklebust477687e2019-03-05 07:30:48 -05002165static void call_bc_transmit(struct rpc_task *task);
2166static void call_bc_transmit_status(struct rpc_task *task);
2167
2168static void
2169call_bc_encode(struct rpc_task *task)
2170{
2171 xprt_request_enqueue_transmit(task);
2172 task->tk_action = call_bc_transmit;
2173}
2174
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04002175/*
2176 * 5b. Send the backchannel RPC reply. On error, drop the reply. In
2177 * addition, disconnect on connectivity errors.
2178 */
2179static void
2180call_bc_transmit(struct rpc_task *task)
2181{
Trond Myklebust477687e2019-03-05 07:30:48 -05002182 task->tk_action = call_bc_transmit_status;
2183 if (test_bit(RPC_TASK_NEED_XMIT, &task->tk_runstate)) {
2184 if (!xprt_prepare_transmit(task))
2185 return;
2186 task->tk_status = 0;
2187 xprt_transmit(task);
2188 }
2189 xprt_end_transmit(task);
2190}
2191
2192static void
2193call_bc_transmit_status(struct rpc_task *task)
2194{
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04002195 struct rpc_rqst *req = task->tk_rqstp;
2196
Trond Myklebusta7b1a482019-04-15 11:54:13 -04002197 if (rpc_task_transmitted(task))
2198 task->tk_status = 0;
2199
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04002200 dprint_status(task);
Trond Myklebust477687e2019-03-05 07:30:48 -05002201
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04002202 switch (task->tk_status) {
2203 case 0:
2204 /* Success */
Trond Myklebusteb5b46f2017-11-30 07:21:33 -05002205 case -ENETDOWN:
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04002206 case -EHOSTDOWN:
2207 case -EHOSTUNREACH:
2208 case -ENETUNREACH:
Trond Myklebust38325912015-06-19 13:04:13 -04002209 case -ECONNRESET:
2210 case -ECONNREFUSED:
2211 case -EADDRINUSE:
2212 case -ENOTCONN:
2213 case -EPIPE:
2214 break;
Trond Myklebust477687e2019-03-05 07:30:48 -05002215 case -ENOBUFS:
2216 rpc_delay(task, HZ>>2);
2217 /* fall through */
2218 case -EBADSLT:
Trond Myklebustc5445772018-09-03 23:39:27 -04002219 case -EAGAIN:
Trond Myklebust477687e2019-03-05 07:30:48 -05002220 task->tk_status = 0;
2221 task->tk_action = call_bc_transmit;
2222 return;
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04002223 case -ETIMEDOUT:
2224 /*
2225 * Problem reaching the server. Disconnect and let the
2226 * forechannel reestablish the connection. The server will
2227 * have to retransmit the backchannel request and we'll
2228 * reprocess it. Since these ops are idempotent, there's no
2229 * need to cache our reply at this time.
2230 */
2231 printk(KERN_NOTICE "RPC: Could not send backchannel reply "
2232 "error: %d\n", task->tk_status);
Trond Myklebusta4f08352013-01-08 09:10:21 -05002233 xprt_conditional_disconnect(req->rq_xprt,
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04002234 req->rq_connect_cookie);
2235 break;
2236 default:
2237 /*
2238 * We were unable to reply and will have to drop the
2239 * request. The server should reconnect and retransmit.
2240 */
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04002241 printk(KERN_NOTICE "RPC: Could not send backchannel reply "
2242 "error: %d\n", task->tk_status);
2243 break;
2244 }
Trond Myklebust1193d582015-06-02 11:53:21 -04002245 task->tk_action = rpc_exit_task;
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04002246}
Trond Myklebust9e00abc2011-07-13 19:20:49 -04002247#endif /* CONFIG_SUNRPC_BACKCHANNEL */
Ricardo Labiaga55ae1aa2009-04-01 09:23:03 -04002248
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249/*
2250 * 6. Sort out the RPC call status
2251 */
2252static void
2253call_status(struct rpc_task *task)
2254{
2255 struct rpc_clnt *clnt = task->tk_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 int status;
2257
Chuck Levera25a4cb2018-03-16 10:33:55 -04002258 if (!task->tk_msg.rpc_proc->p_proc)
2259 trace_xprt_ping(task->tk_xprt, task->tk_status);
2260
Chuck Lever46121cf2007-01-31 12:14:08 -05002261 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262
2263 status = task->tk_status;
2264 if (status >= 0) {
2265 task->tk_action = call_decode;
2266 return;
2267 }
2268
Steve Dickson5753cba2012-02-06 10:08:08 -05002269 trace_rpc_call_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 task->tk_status = 0;
2271 switch(status) {
Trond Myklebust76303992006-08-30 14:32:49 -04002272 case -EHOSTDOWN:
Trond Myklebusteb5b46f2017-11-30 07:21:33 -05002273 case -ENETDOWN:
Trond Myklebust76303992006-08-30 14:32:49 -04002274 case -EHOSTUNREACH:
2275 case -ENETUNREACH:
Jason Baron3dedbb52014-09-24 18:08:04 +00002276 case -EPERM:
Trond Myklebustcea57782019-03-09 16:06:47 -05002277 if (RPC_IS_SOFTCONN(task))
2278 goto out_exit;
Trond Myklebust76303992006-08-30 14:32:49 -04002279 /*
2280 * Delay any retries for 3 seconds, then handle as if it
2281 * were a timeout.
2282 */
2283 rpc_delay(task, 3*HZ);
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05002284 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 case -ETIMEDOUT:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 break;
2287 case -ECONNREFUSED:
Trond Myklebustdf277272013-12-31 13:11:43 -05002288 case -ECONNRESET:
2289 case -ECONNABORTED:
Chuck Lever35f5a422006-01-03 09:55:50 +01002290 rpc_force_rebind(clnt);
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05002291 /* fall through */
Trond Myklebust3913c782015-02-08 21:44:04 -05002292 case -EADDRINUSE:
Trond Myklebustc8485e42009-03-11 14:37:59 -04002293 rpc_delay(task, 3*HZ);
Gustavo A. R. Silvae9d47632017-10-20 11:48:30 -05002294 /* fall through */
Trond Myklebustc8485e42009-03-11 14:37:59 -04002295 case -EPIPE:
2296 case -ENOTCONN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 case -EAGAIN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 break;
2299 case -EIO:
2300 /* shutdown or soft timeout */
Trond Myklebustcea57782019-03-09 16:06:47 -05002301 goto out_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 default:
Olga Kornievskaiab6b61522008-06-09 16:51:31 -04002303 if (clnt->cl_chatty)
2304 printk("%s: RPC call returned error %d\n",
Trond Myklebust55909f22013-08-23 11:48:15 -04002305 clnt->cl_program->name, -status);
Trond Myklebustcea57782019-03-09 16:06:47 -05002306 goto out_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 }
Trond Myklebustcea57782019-03-09 16:06:47 -05002308 task->tk_action = call_encode;
2309 rpc_check_timeout(task);
2310 return;
2311out_exit:
Trond Myklebust5ad64b32019-04-07 13:58:54 -04002312 rpc_call_rpcerror(task, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313}
2314
Trond Myklebustd84dd3f2019-03-19 11:24:54 -04002315static bool
2316rpc_check_connected(const struct rpc_rqst *req)
2317{
2318 /* No allocated request or transport? return true */
2319 if (!req || !req->rq_xprt)
2320 return true;
2321 return xprt_connected(req->rq_xprt);
2322}
2323
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324static void
Trond Myklebust7b3fef82019-03-07 14:10:32 -05002325rpc_check_timeout(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326{
2327 struct rpc_clnt *clnt = task->tk_client;
2328
Trond Myklebust7b3fef82019-03-07 14:10:32 -05002329 if (xprt_adjust_timeout(task->tk_rqstp) == 0)
2330 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331
Chuck Lever46121cf2007-01-31 12:14:08 -05002332 dprintk("RPC: %5u call_timeout (major)\n", task->tk_pid);
Chuck Leveref759a22006-03-20 13:44:17 -05002333 task->tk_timeouts++;
2334
Trond Myklebustd84dd3f2019-03-19 11:24:54 -04002335 if (RPC_IS_SOFTCONN(task) && !rpc_check_connected(task->tk_rqstp)) {
Trond Myklebust5ad64b32019-04-07 13:58:54 -04002336 rpc_call_rpcerror(task, -ETIMEDOUT);
Chuck Lever3a28bec2009-12-03 15:58:56 -05002337 return;
2338 }
Trond Myklebustd84dd3f2019-03-19 11:24:54 -04002339
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 if (RPC_IS_SOFT(task)) {
Trond Myklebuste4ec48d2019-04-07 13:58:55 -04002341 /*
2342 * Once a "no retrans timeout" soft tasks (a.k.a NFSv4) has
2343 * been sent, it should time out only if the transport
2344 * connection gets terminally broken.
2345 */
2346 if ((task->tk_flags & RPC_TASK_NO_RETRANS_TIMEOUT) &&
2347 rpc_check_connected(task->tk_rqstp))
2348 return;
2349
Joe Perchescac5d072012-07-18 11:17:11 -07002350 if (clnt->cl_chatty) {
Trond Myklebust0729d992019-04-07 13:58:57 -04002351 pr_notice_ratelimited(
2352 "%s: server %s not responding, timed out\n",
Trond Myklebust55909f22013-08-23 11:48:15 -04002353 clnt->cl_program->name,
Trond Myklebustfb43d172016-01-30 16:39:26 -05002354 task->tk_xprt->servername);
Joe Perchescac5d072012-07-18 11:17:11 -07002355 }
Trond Myklebust7494d002011-04-24 14:28:45 -04002356 if (task->tk_flags & RPC_TASK_TIMEOUT)
Trond Myklebust5ad64b32019-04-07 13:58:54 -04002357 rpc_call_rpcerror(task, -ETIMEDOUT);
Trond Myklebust7494d002011-04-24 14:28:45 -04002358 else
Trond Myklebust5ad64b32019-04-07 13:58:54 -04002359 __rpc_call_rpcerror(task, -EIO, -ETIMEDOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 return;
2361 }
2362
Chuck Leverf518e35a2006-01-03 09:55:52 +01002363 if (!(task->tk_flags & RPC_CALL_MAJORSEEN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 task->tk_flags |= RPC_CALL_MAJORSEEN;
Trond Myklebust4e0038b2012-03-01 17:01:05 -05002365 if (clnt->cl_chatty) {
Trond Myklebust0729d992019-04-07 13:58:57 -04002366 pr_notice_ratelimited(
2367 "%s: server %s not responding, still trying\n",
2368 clnt->cl_program->name,
2369 task->tk_xprt->servername);
Trond Myklebust4e0038b2012-03-01 17:01:05 -05002370 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 }
Chuck Lever35f5a422006-01-03 09:55:50 +01002372 rpc_force_rebind(clnt);
Trond Myklebustb48633b2008-04-22 16:47:55 -04002373 /*
2374 * Did our request time out due to an RPCSEC_GSS out-of-sequence
2375 * event? RFC2203 requires the server to drop all such requests.
2376 */
2377 rpcauth_invalcred(task);
Trond Myklebust7b3fef82019-03-07 14:10:32 -05002378}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379
Trond Myklebust7b3fef82019-03-07 14:10:32 -05002380/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 * 7. Decode the RPC reply
2382 */
2383static void
2384call_decode(struct rpc_task *task)
2385{
2386 struct rpc_clnt *clnt = task->tk_client;
2387 struct rpc_rqst *req = task->tk_rqstp;
Chuck Levera0584ee2019-02-11 11:24:58 -05002388 struct xdr_stream xdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389
Vasily Averin726fd6a2011-06-01 16:23:59 +04002390 dprint_status(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391
Chuck Levera0584ee2019-02-11 11:24:58 -05002392 if (!task->tk_msg.rpc_proc->p_decode) {
Trond Myklebust9ee94d32018-08-28 16:27:31 -04002393 task->tk_action = rpc_exit_task;
2394 return;
2395 }
2396
Chuck Leverf518e35a2006-01-03 09:55:52 +01002397 if (task->tk_flags & RPC_CALL_MAJORSEEN) {
Trond Myklebust4e0038b2012-03-01 17:01:05 -05002398 if (clnt->cl_chatty) {
Trond Myklebust0729d992019-04-07 13:58:57 -04002399 pr_notice_ratelimited("%s: server %s OK\n",
Trond Myklebust55909f22013-08-23 11:48:15 -04002400 clnt->cl_program->name,
Trond Myklebustfb43d172016-01-30 16:39:26 -05002401 task->tk_xprt->servername);
Trond Myklebust4e0038b2012-03-01 17:01:05 -05002402 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 task->tk_flags &= ~RPC_CALL_MAJORSEEN;
2404 }
2405
Trond Myklebust43ac3f22006-03-20 13:44:51 -05002406 /*
2407 * Ensure that we see all writes made by xprt_complete_rqst()
Ricardo Labiagadd2b63d2009-04-01 09:23:28 -04002408 * before it changed req->rq_reply_bytes_recvd.
Trond Myklebust43ac3f22006-03-20 13:44:51 -05002409 */
2410 smp_rmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 req->rq_rcv_buf.len = req->rq_private_buf.len;
2412
2413 /* Check that the softirq receive buffer is valid */
2414 WARN_ON(memcmp(&req->rq_rcv_buf, &req->rq_private_buf,
2415 sizeof(req->rq_rcv_buf)) != 0);
2416
Chuck Levera0584ee2019-02-11 11:24:58 -05002417 xdr_init_decode(&xdr, &req->rq_rcv_buf,
2418 req->rq_rcv_buf.head[0].iov_base, req);
2419 switch (rpc_decode_header(task, &xdr)) {
2420 case 0:
2421 task->tk_action = rpc_exit_task;
2422 task->tk_status = rpcauth_unwrap_resp(task, &xdr);
2423 dprintk("RPC: %5u %s result %d\n",
2424 task->tk_pid, __func__, task->tk_status);
Trond Myklebustabbcf282006-01-03 09:55:03 +01002425 return;
Chuck Levera0584ee2019-02-11 11:24:58 -05002426 case -EAGAIN:
Chuck Levera0584ee2019-02-11 11:24:58 -05002427 task->tk_status = 0;
2428 /* Note: rpc_decode_header() may have freed the RPC slot */
2429 if (task->tk_rqstp == req) {
2430 xdr_free_bvec(&req->rq_rcv_buf);
2431 req->rq_reply_bytes_recvd = 0;
2432 req->rq_rcv_buf.len = 0;
2433 if (task->tk_client->cl_discrtry)
2434 xprt_conditional_disconnect(req->rq_xprt,
2435 req->rq_connect_cookie);
2436 }
Trond Myklebustcea57782019-03-09 16:06:47 -05002437 task->tk_action = call_encode;
2438 rpc_check_timeout(task);
Trond Myklebust24b74bf2008-04-19 13:15:47 -04002439 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440}
2441
Chuck Levere8680a22019-02-11 11:24:48 -05002442static int
2443rpc_encode_header(struct rpc_task *task, struct xdr_stream *xdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444{
2445 struct rpc_clnt *clnt = task->tk_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 struct rpc_rqst *req = task->tk_rqstp;
Chuck Levere8680a22019-02-11 11:24:48 -05002447 __be32 *p;
2448 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449
Chuck Levere8680a22019-02-11 11:24:48 -05002450 error = -EMSGSIZE;
2451 p = xdr_reserve_space(xdr, RPC_CALLHDRSIZE << 2);
2452 if (!p)
2453 goto out_fail;
2454 *p++ = req->rq_xid;
2455 *p++ = rpc_call;
2456 *p++ = cpu_to_be32(RPC_VERSION);
2457 *p++ = cpu_to_be32(clnt->cl_prog);
2458 *p++ = cpu_to_be32(clnt->cl_vers);
2459 *p = cpu_to_be32(task->tk_msg.rpc_proc->p_proc);
Chuck Lever808012f2005-08-25 16:25:49 -07002460
Chuck Levere8680a22019-02-11 11:24:48 -05002461 error = rpcauth_marshcred(task, xdr);
2462 if (error < 0)
2463 goto out_fail;
2464 return 0;
2465out_fail:
2466 trace_rpc_bad_callhdr(task);
2467 rpc_exit(task, error);
2468 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469}
2470
Chuck Levera0584ee2019-02-11 11:24:58 -05002471static noinline int
2472rpc_decode_header(struct rpc_task *task, struct xdr_stream *xdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473{
Trond Myklebust4e0038b2012-03-01 17:01:05 -05002474 struct rpc_clnt *clnt = task->tk_client;
Trond Myklebusteb90a162019-03-15 09:29:00 -04002475 int error;
Chuck Levera0584ee2019-02-11 11:24:58 -05002476 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477
Chuck Lever7f5667a2019-02-11 11:24:53 -05002478 /* RFC-1014 says that the representation of XDR data must be a
2479 * multiple of four bytes
2480 * - if it isn't pointer subtraction in the NFS client may give
2481 * undefined results
2482 */
2483 if (task->tk_rqstp->rq_rcv_buf.len & 3)
Trond Myklebusteb90a162019-03-15 09:29:00 -04002484 goto out_unparsable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485
Chuck Levera0584ee2019-02-11 11:24:58 -05002486 p = xdr_inline_decode(xdr, 3 * sizeof(*p));
2487 if (!p)
2488 goto out_unparsable;
Chuck Lever7f5667a2019-02-11 11:24:53 -05002489 p++; /* skip XID */
2490 if (*p++ != rpc_reply)
2491 goto out_unparsable;
2492 if (*p++ != rpc_msg_accepted)
2493 goto out_msg_denied;
Ricardo Labiagaf4a2e412009-04-01 09:22:54 -04002494
Chuck Levera0584ee2019-02-11 11:24:58 -05002495 error = rpcauth_checkverf(task, xdr);
2496 if (error)
Chuck Lever7f5667a2019-02-11 11:24:53 -05002497 goto out_verifier;
2498
Chuck Levera0584ee2019-02-11 11:24:58 -05002499 p = xdr_inline_decode(xdr, sizeof(*p));
2500 if (!p)
Chuck Lever7f5667a2019-02-11 11:24:53 -05002501 goto out_unparsable;
Chuck Levera0584ee2019-02-11 11:24:58 -05002502 switch (*p) {
Chuck Lever7f5667a2019-02-11 11:24:53 -05002503 case rpc_success:
Chuck Levera0584ee2019-02-11 11:24:58 -05002504 return 0;
Chuck Lever7f5667a2019-02-11 11:24:53 -05002505 case rpc_prog_unavail:
2506 trace_rpc__prog_unavail(task);
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00002507 error = -EPFNOSUPPORT;
2508 goto out_err;
Chuck Lever7f5667a2019-02-11 11:24:53 -05002509 case rpc_prog_mismatch:
2510 trace_rpc__prog_mismatch(task);
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00002511 error = -EPROTONOSUPPORT;
2512 goto out_err;
Chuck Lever7f5667a2019-02-11 11:24:53 -05002513 case rpc_proc_unavail:
2514 trace_rpc__proc_unavail(task);
Andreas Gruenbachercdf47702005-06-22 17:16:23 +00002515 error = -EOPNOTSUPP;
2516 goto out_err;
Chuck Lever7f5667a2019-02-11 11:24:53 -05002517 case rpc_garbage_args:
Trond Myklebust928d42f2019-03-15 10:12:30 -04002518 case rpc_system_err:
Chuck Lever7f5667a2019-02-11 11:24:53 -05002519 trace_rpc__garbage_args(task);
Trond Myklebusteb90a162019-03-15 09:29:00 -04002520 error = -EIO;
Chuck Lever7f5667a2019-02-11 11:24:53 -05002521 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 default:
Trond Myklebusteb90a162019-03-15 09:29:00 -04002523 goto out_unparsable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 }
2525
Trond Myklebustabbcf282006-01-03 09:55:03 +01002526out_garbage:
Trond Myklebust4e0038b2012-03-01 17:01:05 -05002527 clnt->cl_stats->rpcgarbage++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 if (task->tk_garb_retry) {
2529 task->tk_garb_retry--;
Trond Myklebust762e4e62018-08-24 16:28:28 -04002530 task->tk_action = call_encode;
Chuck Levera0584ee2019-02-11 11:24:58 -05002531 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533out_err:
2534 rpc_exit(task, error);
Chuck Levera0584ee2019-02-11 11:24:58 -05002535 return error;
Chuck Lever7f5667a2019-02-11 11:24:53 -05002536
Chuck Lever7f5667a2019-02-11 11:24:53 -05002537out_unparsable:
2538 trace_rpc__unparsable(task);
2539 error = -EIO;
Trond Myklebustabbcf282006-01-03 09:55:03 +01002540 goto out_garbage;
Chuck Lever7f5667a2019-02-11 11:24:53 -05002541
2542out_verifier:
2543 trace_rpc_bad_verifier(task);
Chuck Lever7f5667a2019-02-11 11:24:53 -05002544 goto out_garbage;
2545
2546out_msg_denied:
Trond Myklebusteb90a162019-03-15 09:29:00 -04002547 error = -EACCES;
Chuck Levera0584ee2019-02-11 11:24:58 -05002548 p = xdr_inline_decode(xdr, sizeof(*p));
2549 if (!p)
2550 goto out_unparsable;
Chuck Lever7f5667a2019-02-11 11:24:53 -05002551 switch (*p++) {
2552 case rpc_auth_error:
2553 break;
2554 case rpc_mismatch:
2555 trace_rpc__mismatch(task);
2556 error = -EPROTONOSUPPORT;
2557 goto out_err;
2558 default:
Trond Myklebusteb90a162019-03-15 09:29:00 -04002559 goto out_unparsable;
Chuck Lever7f5667a2019-02-11 11:24:53 -05002560 }
2561
Chuck Levera0584ee2019-02-11 11:24:58 -05002562 p = xdr_inline_decode(xdr, sizeof(*p));
2563 if (!p)
2564 goto out_unparsable;
Chuck Lever7f5667a2019-02-11 11:24:53 -05002565 switch (*p++) {
2566 case rpc_autherr_rejectedcred:
2567 case rpc_autherr_rejectedverf:
2568 case rpcsec_gsserr_credproblem:
2569 case rpcsec_gsserr_ctxproblem:
2570 if (!task->tk_cred_retry)
2571 break;
2572 task->tk_cred_retry--;
2573 trace_rpc__stale_creds(task);
2574 rpcauth_invalcred(task);
2575 /* Ensure we obtain a new XID! */
2576 xprt_release(task);
2577 task->tk_action = call_reserve;
Chuck Levera0584ee2019-02-11 11:24:58 -05002578 return -EAGAIN;
Chuck Lever7f5667a2019-02-11 11:24:53 -05002579 case rpc_autherr_badcred:
2580 case rpc_autherr_badverf:
2581 /* possibly garbled cred/verf? */
2582 if (!task->tk_garb_retry)
2583 break;
2584 task->tk_garb_retry--;
2585 trace_rpc__bad_creds(task);
2586 task->tk_action = call_encode;
Chuck Levera0584ee2019-02-11 11:24:58 -05002587 return -EAGAIN;
Chuck Lever7f5667a2019-02-11 11:24:53 -05002588 case rpc_autherr_tooweak:
2589 trace_rpc__auth_tooweak(task);
2590 pr_warn("RPC: server %s requires stronger authentication.\n",
2591 task->tk_xprt->servername);
2592 break;
2593 default:
Trond Myklebusteb90a162019-03-15 09:29:00 -04002594 goto out_unparsable;
Chuck Lever7f5667a2019-02-11 11:24:53 -05002595 }
2596 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597}
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00002598
Christoph Hellwigc512f362017-05-08 09:31:19 +02002599static void rpcproc_encode_null(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
2600 const void *obj)
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00002601{
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00002602}
2603
Christoph Hellwig73c8dc12017-05-08 14:58:11 +02002604static int rpcproc_decode_null(struct rpc_rqst *rqstp, struct xdr_stream *xdr,
2605 void *obj)
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00002606{
2607 return 0;
2608}
2609
Christoph Hellwig499b4982017-05-12 15:36:49 +02002610static const struct rpc_procinfo rpcproc_null = {
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00002611 .p_encode = rpcproc_encode_null,
2612 .p_decode = rpcproc_decode_null,
2613};
2614
Chuck Levercaabea82009-12-03 15:58:56 -05002615static int rpc_ping(struct rpc_clnt *clnt)
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00002616{
2617 struct rpc_message msg = {
2618 .rpc_proc = &rpcproc_null,
2619 };
2620 int err;
NeilBrowna68a72e2018-12-03 11:30:30 +11002621 err = rpc_call_sync(clnt, &msg, RPC_TASK_SOFT | RPC_TASK_SOFTCONN |
2622 RPC_TASK_NULLCREDS);
Trond Myklebust5ee0ed72005-06-22 17:16:20 +00002623 return err;
2624}
Trond Myklebust188fef12007-06-16 14:18:40 -04002625
Trond Myklebust7f554892016-01-30 23:43:35 -05002626static
2627struct rpc_task *rpc_call_null_helper(struct rpc_clnt *clnt,
2628 struct rpc_xprt *xprt, struct rpc_cred *cred, int flags,
2629 const struct rpc_call_ops *ops, void *data)
Trond Myklebust5e1550d2007-06-23 10:17:16 -04002630{
2631 struct rpc_message msg = {
2632 .rpc_proc = &rpcproc_null,
Trond Myklebust5e1550d2007-06-23 10:17:16 -04002633 };
Trond Myklebust84115e12007-07-14 15:39:59 -04002634 struct rpc_task_setup task_setup_data = {
2635 .rpc_client = clnt,
Trond Myklebust7f554892016-01-30 23:43:35 -05002636 .rpc_xprt = xprt,
Trond Myklebust84115e12007-07-14 15:39:59 -04002637 .rpc_message = &msg,
NeilBrown1de7eea2018-12-03 11:30:30 +11002638 .rpc_op_cred = cred,
Trond Myklebust7f554892016-01-30 23:43:35 -05002639 .callback_ops = (ops != NULL) ? ops : &rpc_default_ops,
2640 .callback_data = data,
NeilBrowna52458b2018-12-03 11:30:31 +11002641 .flags = flags | RPC_TASK_NULLCREDS,
Trond Myklebust84115e12007-07-14 15:39:59 -04002642 };
Trond Myklebust7f554892016-01-30 23:43:35 -05002643
Trond Myklebustc970aa82007-07-14 15:39:59 -04002644 return rpc_run_task(&task_setup_data);
Trond Myklebust5e1550d2007-06-23 10:17:16 -04002645}
Trond Myklebust7f554892016-01-30 23:43:35 -05002646
2647struct rpc_task *rpc_call_null(struct rpc_clnt *clnt, struct rpc_cred *cred, int flags)
2648{
2649 return rpc_call_null_helper(clnt, NULL, cred, flags, NULL, NULL);
2650}
Trond Myklebuste8914c62007-07-14 15:39:59 -04002651EXPORT_SYMBOL_GPL(rpc_call_null);
Trond Myklebust5e1550d2007-06-23 10:17:16 -04002652
Trond Myklebust7f554892016-01-30 23:43:35 -05002653struct rpc_cb_add_xprt_calldata {
2654 struct rpc_xprt_switch *xps;
2655 struct rpc_xprt *xprt;
2656};
2657
2658static void rpc_cb_add_xprt_done(struct rpc_task *task, void *calldata)
2659{
2660 struct rpc_cb_add_xprt_calldata *data = calldata;
2661
2662 if (task->tk_status == 0)
2663 rpc_xprt_switch_add_xprt(data->xps, data->xprt);
2664}
2665
2666static void rpc_cb_add_xprt_release(void *calldata)
2667{
2668 struct rpc_cb_add_xprt_calldata *data = calldata;
2669
2670 xprt_put(data->xprt);
2671 xprt_switch_put(data->xps);
2672 kfree(data);
2673}
2674
Trond Myklebustce272302016-07-24 17:06:28 -04002675static const struct rpc_call_ops rpc_cb_add_xprt_call_ops = {
Trond Myklebust7f554892016-01-30 23:43:35 -05002676 .rpc_call_done = rpc_cb_add_xprt_done,
2677 .rpc_release = rpc_cb_add_xprt_release,
2678};
2679
2680/**
2681 * rpc_clnt_test_and_add_xprt - Test and add a new transport to a rpc_clnt
2682 * @clnt: pointer to struct rpc_clnt
2683 * @xps: pointer to struct rpc_xprt_switch,
2684 * @xprt: pointer struct rpc_xprt
2685 * @dummy: unused
2686 */
2687int rpc_clnt_test_and_add_xprt(struct rpc_clnt *clnt,
2688 struct rpc_xprt_switch *xps, struct rpc_xprt *xprt,
2689 void *dummy)
2690{
2691 struct rpc_cb_add_xprt_calldata *data;
Trond Myklebust7f554892016-01-30 23:43:35 -05002692 struct rpc_task *task;
2693
2694 data = kmalloc(sizeof(*data), GFP_NOFS);
2695 if (!data)
2696 return -ENOMEM;
2697 data->xps = xprt_switch_get(xps);
2698 data->xprt = xprt_get(xprt);
2699
NeilBrowna68a72e2018-12-03 11:30:30 +11002700 task = rpc_call_null_helper(clnt, xprt, NULL,
2701 RPC_TASK_SOFT|RPC_TASK_SOFTCONN|RPC_TASK_ASYNC|RPC_TASK_NULLCREDS,
Trond Myklebust7f554892016-01-30 23:43:35 -05002702 &rpc_cb_add_xprt_call_ops, data);
Trond Myklebust7f554892016-01-30 23:43:35 -05002703 if (IS_ERR(task))
2704 return PTR_ERR(task);
2705 rpc_put_task(task);
2706 return 1;
2707}
2708EXPORT_SYMBOL_GPL(rpc_clnt_test_and_add_xprt);
2709
2710/**
Andy Adamsonfda0ab42016-09-09 09:22:26 -04002711 * rpc_clnt_setup_test_and_add_xprt()
2712 *
2713 * This is an rpc_clnt_add_xprt setup() function which returns 1 so:
2714 * 1) caller of the test function must dereference the rpc_xprt_switch
2715 * and the rpc_xprt.
2716 * 2) test function must call rpc_xprt_switch_add_xprt, usually in
2717 * the rpc_call_done routine.
2718 *
2719 * Upon success (return of 1), the test function adds the new
2720 * transport to the rpc_clnt xprt switch
2721 *
2722 * @clnt: struct rpc_clnt to get the new transport
2723 * @xps: the rpc_xprt_switch to hold the new transport
2724 * @xprt: the rpc_xprt to test
2725 * @data: a struct rpc_add_xprt_test pointer that holds the test function
2726 * and test function call data
2727 */
2728int rpc_clnt_setup_test_and_add_xprt(struct rpc_clnt *clnt,
2729 struct rpc_xprt_switch *xps,
2730 struct rpc_xprt *xprt,
2731 void *data)
2732{
Andy Adamsonfda0ab42016-09-09 09:22:26 -04002733 struct rpc_task *task;
2734 struct rpc_add_xprt_test *xtest = (struct rpc_add_xprt_test *)data;
2735 int status = -EADDRINUSE;
2736
2737 xprt = xprt_get(xprt);
2738 xprt_switch_get(xps);
2739
2740 if (rpc_xprt_switch_has_addr(xps, (struct sockaddr *)&xprt->addr))
2741 goto out_err;
2742
2743 /* Test the connection */
NeilBrowna68a72e2018-12-03 11:30:30 +11002744 task = rpc_call_null_helper(clnt, xprt, NULL,
2745 RPC_TASK_SOFT | RPC_TASK_SOFTCONN | RPC_TASK_NULLCREDS,
Andy Adamsonfda0ab42016-09-09 09:22:26 -04002746 NULL, NULL);
Andy Adamsonfda0ab42016-09-09 09:22:26 -04002747 if (IS_ERR(task)) {
2748 status = PTR_ERR(task);
2749 goto out_err;
2750 }
2751 status = task->tk_status;
2752 rpc_put_task(task);
2753
2754 if (status < 0)
2755 goto out_err;
2756
2757 /* rpc_xprt_switch and rpc_xprt are deferrenced by add_xprt_test() */
2758 xtest->add_xprt_test(clnt, xprt, xtest->data);
2759
Santosh kumar pradhan10e037d2018-12-19 12:29:57 +05302760 xprt_put(xprt);
2761 xprt_switch_put(xps);
2762
Andy Adamsonfda0ab42016-09-09 09:22:26 -04002763 /* so that rpc_clnt_add_xprt does not call rpc_xprt_switch_add_xprt */
2764 return 1;
2765out_err:
2766 xprt_put(xprt);
2767 xprt_switch_put(xps);
2768 pr_info("RPC: rpc_clnt_test_xprt failed: %d addr %s not added\n",
2769 status, xprt->address_strings[RPC_DISPLAY_ADDR]);
2770 return status;
2771}
2772EXPORT_SYMBOL_GPL(rpc_clnt_setup_test_and_add_xprt);
2773
2774/**
Trond Myklebust7f554892016-01-30 23:43:35 -05002775 * rpc_clnt_add_xprt - Add a new transport to a rpc_clnt
2776 * @clnt: pointer to struct rpc_clnt
2777 * @xprtargs: pointer to struct xprt_create
2778 * @setup: callback to test and/or set up the connection
2779 * @data: pointer to setup function data
2780 *
2781 * Creates a new transport using the parameters set in args and
2782 * adds it to clnt.
2783 * If ping is set, then test that connectivity succeeds before
2784 * adding the new transport.
2785 *
2786 */
2787int rpc_clnt_add_xprt(struct rpc_clnt *clnt,
2788 struct xprt_create *xprtargs,
2789 int (*setup)(struct rpc_clnt *,
2790 struct rpc_xprt_switch *,
2791 struct rpc_xprt *,
2792 void *),
2793 void *data)
2794{
2795 struct rpc_xprt_switch *xps;
2796 struct rpc_xprt *xprt;
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002797 unsigned long connect_timeout;
Trond Myklebust3851f1c2016-08-04 00:08:45 -04002798 unsigned long reconnect_timeout;
Trond Myklebust7f554892016-01-30 23:43:35 -05002799 unsigned char resvport;
2800 int ret = 0;
2801
2802 rcu_read_lock();
2803 xps = xprt_switch_get(rcu_dereference(clnt->cl_xpi.xpi_xpswitch));
2804 xprt = xprt_iter_xprt(&clnt->cl_xpi);
2805 if (xps == NULL || xprt == NULL) {
2806 rcu_read_unlock();
2807 return -EAGAIN;
2808 }
2809 resvport = xprt->resvport;
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002810 connect_timeout = xprt->connect_timeout;
Trond Myklebust3851f1c2016-08-04 00:08:45 -04002811 reconnect_timeout = xprt->max_reconnect_timeout;
Trond Myklebust7f554892016-01-30 23:43:35 -05002812 rcu_read_unlock();
2813
2814 xprt = xprt_create_transport(xprtargs);
2815 if (IS_ERR(xprt)) {
2816 ret = PTR_ERR(xprt);
2817 goto out_put_switch;
2818 }
2819 xprt->resvport = resvport;
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002820 if (xprt->ops->set_connect_timeout != NULL)
2821 xprt->ops->set_connect_timeout(xprt,
2822 connect_timeout,
2823 reconnect_timeout);
Trond Myklebust7f554892016-01-30 23:43:35 -05002824
2825 rpc_xprt_switch_set_roundrobin(xps);
2826 if (setup) {
2827 ret = setup(clnt, xps, xprt, data);
2828 if (ret != 0)
2829 goto out_put_xprt;
2830 }
2831 rpc_xprt_switch_add_xprt(xps, xprt);
2832out_put_xprt:
2833 xprt_put(xprt);
2834out_put_switch:
2835 xprt_switch_put(xps);
2836 return ret;
2837}
2838EXPORT_SYMBOL_GPL(rpc_clnt_add_xprt);
2839
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002840struct connect_timeout_data {
2841 unsigned long connect_timeout;
2842 unsigned long reconnect_timeout;
2843};
2844
Trond Myklebust8d480322016-08-05 19:03:31 -04002845static int
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002846rpc_xprt_set_connect_timeout(struct rpc_clnt *clnt,
Trond Myklebust8d480322016-08-05 19:03:31 -04002847 struct rpc_xprt *xprt,
2848 void *data)
2849{
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002850 struct connect_timeout_data *timeo = data;
Trond Myklebust8d480322016-08-05 19:03:31 -04002851
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002852 if (xprt->ops->set_connect_timeout)
2853 xprt->ops->set_connect_timeout(xprt,
2854 timeo->connect_timeout,
2855 timeo->reconnect_timeout);
Trond Myklebust8d480322016-08-05 19:03:31 -04002856 return 0;
2857}
2858
2859void
Trond Myklebust26ae1022017-02-08 11:17:55 -05002860rpc_set_connect_timeout(struct rpc_clnt *clnt,
2861 unsigned long connect_timeout,
2862 unsigned long reconnect_timeout)
Trond Myklebust8d480322016-08-05 19:03:31 -04002863{
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002864 struct connect_timeout_data timeout = {
Trond Myklebust26ae1022017-02-08 11:17:55 -05002865 .connect_timeout = connect_timeout,
2866 .reconnect_timeout = reconnect_timeout,
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002867 };
Trond Myklebust8d480322016-08-05 19:03:31 -04002868 rpc_clnt_iterate_for_each_xprt(clnt,
Trond Myklebust7196dbb2017-02-08 11:17:54 -05002869 rpc_xprt_set_connect_timeout,
2870 &timeout);
Trond Myklebust8d480322016-08-05 19:03:31 -04002871}
Trond Myklebust26ae1022017-02-08 11:17:55 -05002872EXPORT_SYMBOL_GPL(rpc_set_connect_timeout);
Trond Myklebust8d480322016-08-05 19:03:31 -04002873
Andy Adamson3b58a8a2016-09-09 09:22:23 -04002874void rpc_clnt_xprt_switch_put(struct rpc_clnt *clnt)
2875{
Anna Schumakerbb29dd82016-10-26 10:33:31 -04002876 rcu_read_lock();
Andy Adamson3b58a8a2016-09-09 09:22:23 -04002877 xprt_switch_put(rcu_dereference(clnt->cl_xpi.xpi_xpswitch));
Anna Schumakerbb29dd82016-10-26 10:33:31 -04002878 rcu_read_unlock();
Andy Adamson3b58a8a2016-09-09 09:22:23 -04002879}
2880EXPORT_SYMBOL_GPL(rpc_clnt_xprt_switch_put);
2881
Andy Adamsondd691712016-09-09 09:22:24 -04002882void rpc_clnt_xprt_switch_add_xprt(struct rpc_clnt *clnt, struct rpc_xprt *xprt)
2883{
Anna Schumakerbb29dd82016-10-26 10:33:31 -04002884 rcu_read_lock();
Andy Adamsondd691712016-09-09 09:22:24 -04002885 rpc_xprt_switch_add_xprt(rcu_dereference(clnt->cl_xpi.xpi_xpswitch),
2886 xprt);
Anna Schumakerbb29dd82016-10-26 10:33:31 -04002887 rcu_read_unlock();
Andy Adamsondd691712016-09-09 09:22:24 -04002888}
2889EXPORT_SYMBOL_GPL(rpc_clnt_xprt_switch_add_xprt);
2890
Andy Adamson39e5d2d2016-09-09 09:22:25 -04002891bool rpc_clnt_xprt_switch_has_addr(struct rpc_clnt *clnt,
2892 const struct sockaddr *sap)
2893{
2894 struct rpc_xprt_switch *xps;
2895 bool ret;
2896
Andy Adamson39e5d2d2016-09-09 09:22:25 -04002897 rcu_read_lock();
Anna Schumakerbb29dd82016-10-26 10:33:31 -04002898 xps = rcu_dereference(clnt->cl_xpi.xpi_xpswitch);
Andy Adamson39e5d2d2016-09-09 09:22:25 -04002899 ret = rpc_xprt_switch_has_addr(xps, sap);
2900 rcu_read_unlock();
2901 return ret;
2902}
2903EXPORT_SYMBOL_GPL(rpc_clnt_xprt_switch_has_addr);
2904
Jeff Laytonf895b252014-11-17 16:58:04 -05002905#if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
Chuck Lever68a23ee2008-05-21 17:09:26 -04002906static void rpc_show_header(void)
2907{
Chuck Levercb3997b2008-05-21 17:09:41 -04002908 printk(KERN_INFO "-pid- flgs status -client- --rqstp- "
2909 "-timeout ---ops--\n");
Chuck Lever68a23ee2008-05-21 17:09:26 -04002910}
2911
Chuck Lever38e886e2008-05-21 17:09:33 -04002912static void rpc_show_task(const struct rpc_clnt *clnt,
2913 const struct rpc_task *task)
2914{
2915 const char *rpc_waitq = "none";
Chuck Lever38e886e2008-05-21 17:09:33 -04002916
2917 if (RPC_IS_QUEUED(task))
2918 rpc_waitq = rpc_qname(task->tk_waitqueue);
2919
Joe Perchesb3bceda2010-12-21 10:52:24 -05002920 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 -04002921 task->tk_pid, task->tk_flags, task->tk_status,
Trond Myklebust5efd1872019-04-07 13:58:50 -04002922 clnt, task->tk_rqstp, rpc_task_timeout(task), task->tk_ops,
Trond Myklebust55909f22013-08-23 11:48:15 -04002923 clnt->cl_program->name, clnt->cl_vers, rpc_proc_name(task),
Joe Perchesb3bceda2010-12-21 10:52:24 -05002924 task->tk_action, rpc_waitq);
Chuck Lever38e886e2008-05-21 17:09:33 -04002925}
2926
Stanislav Kinsbursky70abc492012-01-12 22:07:51 +04002927void rpc_show_tasks(struct net *net)
Trond Myklebust188fef12007-06-16 14:18:40 -04002928{
2929 struct rpc_clnt *clnt;
Chuck Lever38e886e2008-05-21 17:09:33 -04002930 struct rpc_task *task;
Chuck Lever68a23ee2008-05-21 17:09:26 -04002931 int header = 0;
Stanislav Kinsbursky70abc492012-01-12 22:07:51 +04002932 struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
Trond Myklebust188fef12007-06-16 14:18:40 -04002933
Stanislav Kinsbursky70abc492012-01-12 22:07:51 +04002934 spin_lock(&sn->rpc_client_lock);
2935 list_for_each_entry(clnt, &sn->all_clients, cl_clients) {
Trond Myklebust188fef12007-06-16 14:18:40 -04002936 spin_lock(&clnt->cl_lock);
Chuck Lever38e886e2008-05-21 17:09:33 -04002937 list_for_each_entry(task, &clnt->cl_tasks, tk_task) {
Chuck Lever68a23ee2008-05-21 17:09:26 -04002938 if (!header) {
2939 rpc_show_header();
2940 header++;
2941 }
Chuck Lever38e886e2008-05-21 17:09:33 -04002942 rpc_show_task(clnt, task);
Trond Myklebust188fef12007-06-16 14:18:40 -04002943 }
2944 spin_unlock(&clnt->cl_lock);
2945 }
Stanislav Kinsbursky70abc492012-01-12 22:07:51 +04002946 spin_unlock(&sn->rpc_client_lock);
Trond Myklebust188fef12007-06-16 14:18:40 -04002947}
2948#endif
Jeff Layton3c87ef62015-06-03 16:14:25 -04002949
2950#if IS_ENABLED(CONFIG_SUNRPC_SWAP)
Trond Myklebust15001e52016-01-30 20:05:34 -05002951static int
2952rpc_clnt_swap_activate_callback(struct rpc_clnt *clnt,
2953 struct rpc_xprt *xprt,
2954 void *dummy)
2955{
2956 return xprt_enable_swap(xprt);
2957}
2958
Jeff Layton3c87ef62015-06-03 16:14:25 -04002959int
2960rpc_clnt_swap_activate(struct rpc_clnt *clnt)
2961{
Trond Myklebust15001e52016-01-30 20:05:34 -05002962 if (atomic_inc_return(&clnt->cl_swapper) == 1)
2963 return rpc_clnt_iterate_for_each_xprt(clnt,
2964 rpc_clnt_swap_activate_callback, NULL);
2965 return 0;
Jeff Layton3c87ef62015-06-03 16:14:25 -04002966}
2967EXPORT_SYMBOL_GPL(rpc_clnt_swap_activate);
2968
Trond Myklebust15001e52016-01-30 20:05:34 -05002969static int
2970rpc_clnt_swap_deactivate_callback(struct rpc_clnt *clnt,
2971 struct rpc_xprt *xprt,
2972 void *dummy)
2973{
2974 xprt_disable_swap(xprt);
2975 return 0;
2976}
2977
Jeff Layton3c87ef62015-06-03 16:14:25 -04002978void
2979rpc_clnt_swap_deactivate(struct rpc_clnt *clnt)
2980{
Trond Myklebust15001e52016-01-30 20:05:34 -05002981 if (atomic_dec_if_positive(&clnt->cl_swapper) == 0)
2982 rpc_clnt_iterate_for_each_xprt(clnt,
2983 rpc_clnt_swap_deactivate_callback, NULL);
Jeff Layton3c87ef62015-06-03 16:14:25 -04002984}
2985EXPORT_SYMBOL_GPL(rpc_clnt_swap_deactivate);
2986#endif /* CONFIG_SUNRPC_SWAP */